5 #ifndef _ASM_PARISC_ATOMIC_H_
6 #define _ASM_PARISC_ATOMIC_H_
8 #include <linux/types.h>
9 #include <asm/cmpxchg.h>
10 #include <asm/barrier.h>
13 * Atomic operations that C can't guarantee us. Useful for
14 * resource counting etc..
16 * And probably incredibly slow on parisc. OTOH, we don't
17 * have to write any serious assembly. prumpf
21 #include <asm/spinlock.h>
22 #include <asm/cache.h> /* we use L1_CACHE_BYTES */
24 /* Use an array of spinlocks for our atomic_ts.
25 * Hash function to index into a different SPINLOCK.
26 * Since "a" is usually an address, use one spinlock per cacheline.
28 # define ATOMIC_HASH_SIZE 4
29 # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) (a))/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
31 extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
33 /* Can't use raw_spin_lock_irq because of #include problems, so
34 * this is the substitute */
35 #define _atomic_spin_lock_irqsave(l,f) do { \
36 arch_spinlock_t *s = ATOMIC_HASH(l); \
41 #define _atomic_spin_unlock_irqrestore(l,f) do { \
42 arch_spinlock_t *s = ATOMIC_HASH(l); \
43 arch_spin_unlock(s); \
44 local_irq_restore(f); \
49 # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
50 # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
54 * Note that we need not lock read accesses - aligned word writes/reads
55 * are atomic, so a reader never sees inconsistent values.
58 static __inline__ void atomic_set(atomic_t *v, int i)
61 _atomic_spin_lock_irqsave(v, flags);
65 _atomic_spin_unlock_irqrestore(v, flags);
68 #define atomic_set_release(v, i) atomic_set((v), (i))
70 static __inline__ int atomic_read(const atomic_t *v)
72 return READ_ONCE((v)->counter);
75 /* exported interface */
76 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
77 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
80 * __atomic_add_unless - add unless the number is a given value
81 * @v: pointer of type atomic_t
82 * @a: the amount to add to v...
83 * @u: ...unless v is equal to u.
85 * Atomically adds @a to @v, so long as it was not @u.
86 * Returns the old value of @v.
88 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
93 if (unlikely(c == (u)))
95 old = atomic_cmpxchg((v), c, c + (a));
103 #define ATOMIC_OP(op, c_op) \
104 static __inline__ void atomic_##op(int i, atomic_t *v) \
106 unsigned long flags; \
108 _atomic_spin_lock_irqsave(v, flags); \
110 _atomic_spin_unlock_irqrestore(v, flags); \
113 #define ATOMIC_OP_RETURN(op, c_op) \
114 static __inline__ int atomic_##op##_return(int i, atomic_t *v) \
116 unsigned long flags; \
119 _atomic_spin_lock_irqsave(v, flags); \
120 ret = (v->counter c_op i); \
121 _atomic_spin_unlock_irqrestore(v, flags); \
126 #define ATOMIC_FETCH_OP(op, c_op) \
127 static __inline__ int atomic_fetch_##op(int i, atomic_t *v) \
129 unsigned long flags; \
132 _atomic_spin_lock_irqsave(v, flags); \
135 _atomic_spin_unlock_irqrestore(v, flags); \
140 #define ATOMIC_OPS(op, c_op) \
141 ATOMIC_OP(op, c_op) \
142 ATOMIC_OP_RETURN(op, c_op) \
143 ATOMIC_FETCH_OP(op, c_op)
149 #define ATOMIC_OPS(op, c_op) \
150 ATOMIC_OP(op, c_op) \
151 ATOMIC_FETCH_OP(op, c_op)
158 #undef ATOMIC_FETCH_OP
159 #undef ATOMIC_OP_RETURN
162 #define atomic_inc(v) (atomic_add( 1,(v)))
163 #define atomic_dec(v) (atomic_add( -1,(v)))
165 #define atomic_inc_return(v) (atomic_add_return( 1,(v)))
166 #define atomic_dec_return(v) (atomic_add_return( -1,(v)))
168 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
171 * atomic_inc_and_test - increment and test
172 * @v: pointer of type atomic_t
174 * Atomically increments @v by 1
175 * and returns true if the result is zero, or false for all
178 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
180 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
182 #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
184 #define ATOMIC_INIT(i) { (i) }
188 #define ATOMIC64_INIT(i) { (i) }
190 #define ATOMIC64_OP(op, c_op) \
191 static __inline__ void atomic64_##op(s64 i, atomic64_t *v) \
193 unsigned long flags; \
195 _atomic_spin_lock_irqsave(v, flags); \
197 _atomic_spin_unlock_irqrestore(v, flags); \
200 #define ATOMIC64_OP_RETURN(op, c_op) \
201 static __inline__ s64 atomic64_##op##_return(s64 i, atomic64_t *v) \
203 unsigned long flags; \
206 _atomic_spin_lock_irqsave(v, flags); \
207 ret = (v->counter c_op i); \
208 _atomic_spin_unlock_irqrestore(v, flags); \
213 #define ATOMIC64_FETCH_OP(op, c_op) \
214 static __inline__ s64 atomic64_fetch_##op(s64 i, atomic64_t *v) \
216 unsigned long flags; \
219 _atomic_spin_lock_irqsave(v, flags); \
222 _atomic_spin_unlock_irqrestore(v, flags); \
227 #define ATOMIC64_OPS(op, c_op) \
228 ATOMIC64_OP(op, c_op) \
229 ATOMIC64_OP_RETURN(op, c_op) \
230 ATOMIC64_FETCH_OP(op, c_op)
232 ATOMIC64_OPS(add, +=)
233 ATOMIC64_OPS(sub, -=)
236 #define ATOMIC64_OPS(op, c_op) \
237 ATOMIC64_OP(op, c_op) \
238 ATOMIC64_FETCH_OP(op, c_op)
240 ATOMIC64_OPS(and, &=)
242 ATOMIC64_OPS(xor, ^=)
245 #undef ATOMIC64_FETCH_OP
246 #undef ATOMIC64_OP_RETURN
249 static __inline__ void
250 atomic64_set(atomic64_t *v, s64 i)
253 _atomic_spin_lock_irqsave(v, flags);
257 _atomic_spin_unlock_irqrestore(v, flags);
260 static __inline__ s64
261 atomic64_read(const atomic64_t *v)
263 return ACCESS_ONCE((v)->counter);
266 #define atomic64_inc(v) (atomic64_add( 1,(v)))
267 #define atomic64_dec(v) (atomic64_add( -1,(v)))
269 #define atomic64_inc_return(v) (atomic64_add_return( 1,(v)))
270 #define atomic64_dec_return(v) (atomic64_add_return( -1,(v)))
272 #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
274 #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
275 #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
276 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
278 /* exported interface */
279 #define atomic64_cmpxchg(v, o, n) \
280 ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
281 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
284 * atomic64_add_unless - add unless the number is a given value
285 * @v: pointer of type atomic64_t
286 * @a: the amount to add to v...
287 * @u: ...unless v is equal to u.
289 * Atomically adds @a to @v, so long as it was not @u.
290 * Returns the old value of @v.
292 static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
295 c = atomic64_read(v);
297 if (unlikely(c == (u)))
299 old = atomic64_cmpxchg((v), c, c + (a));
300 if (likely(old == c))
307 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
310 * atomic64_dec_if_positive - decrement by 1 if old value positive
311 * @v: pointer of type atomic_t
313 * The function returns the old value of *v minus 1, even if
314 * the atomic variable, v, was not decremented.
316 static inline long atomic64_dec_if_positive(atomic64_t *v)
319 c = atomic64_read(v);
322 if (unlikely(dec < 0))
324 old = atomic64_cmpxchg((v), c, dec);
325 if (likely(old == c))
332 #endif /* !CONFIG_64BIT */
335 #endif /* _ASM_PARISC_ATOMIC_H_ */