1 #ifndef __ARCH_S390_ATOMIC__
2 #define __ARCH_S390_ATOMIC__
5 * Copyright 1999,2009 IBM Corp.
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
8 * Arnd Bergmann <arndb@de.ibm.com>,
10 * Atomic operations that C can't guarantee us.
11 * Useful for resource counting etc.
12 * s390 uses 'Compare And Swap' for atomicity in SMP enviroment.
16 #include <linux/compiler.h>
17 #include <linux/types.h>
18 #include <asm/system.h>
20 #define ATOMIC_INIT(i) { (i) }
22 #define __CS_LOOP(ptr, op_val, op_string) ({ \
23 int old_val, new_val; \
27 op_string " %1,%3\n" \
30 : "=&d" (old_val), "=&d" (new_val), \
31 "=Q" (((atomic_t *)(ptr))->counter) \
32 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
37 static inline int atomic_read(const atomic_t *v)
39 return ACCESS_ONCE(v->counter);
42 static inline void atomic_set(atomic_t *v, int i)
47 static inline int atomic_add_return(int i, atomic_t *v)
49 return __CS_LOOP(v, i, "ar");
51 #define atomic_add(_i, _v) atomic_add_return(_i, _v)
52 #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
53 #define atomic_inc(_v) atomic_add_return(1, _v)
54 #define atomic_inc_return(_v) atomic_add_return(1, _v)
55 #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
57 static inline int atomic_sub_return(int i, atomic_t *v)
59 return __CS_LOOP(v, i, "sr");
61 #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
62 #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
63 #define atomic_dec(_v) atomic_sub_return(1, _v)
64 #define atomic_dec_return(_v) atomic_sub_return(1, _v)
65 #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
67 static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
69 __CS_LOOP(v, ~mask, "nr");
72 static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
74 __CS_LOOP(v, mask, "or");
77 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
79 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
83 : "+d" (old), "=Q" (v->counter)
84 : "d" (new), "Q" (v->counter)
89 static inline int atomic_add_unless(atomic_t *v, int a, int u)
96 old = atomic_cmpxchg(v, c, c + a);
104 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
108 #define ATOMIC64_INIT(i) { (i) }
112 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
113 long long old_val, new_val; \
117 op_string " %1,%3\n" \
120 : "=&d" (old_val), "=&d" (new_val), \
121 "=Q" (((atomic_t *)(ptr))->counter) \
122 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
127 static inline long long atomic64_read(const atomic64_t *v)
129 return ACCESS_ONCE(v->counter);
132 static inline void atomic64_set(atomic64_t *v, long long i)
137 static inline long long atomic64_add_return(long long i, atomic64_t *v)
139 return __CSG_LOOP(v, i, "agr");
142 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
144 return __CSG_LOOP(v, i, "sgr");
147 static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
149 __CSG_LOOP(v, ~mask, "ngr");
152 static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
154 __CSG_LOOP(v, mask, "ogr");
157 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
159 static inline long long atomic64_cmpxchg(atomic64_t *v,
160 long long old, long long new)
164 : "+d" (old), "=Q" (v->counter)
165 : "d" (new), "Q" (v->counter)
172 #else /* CONFIG_64BIT */
178 static inline long long atomic64_read(const atomic64_t *v)
184 : "=&d" (rp) : "Q" (v->counter) );
188 static inline void atomic64_set(atomic64_t *v, long long i)
190 register_pair rp = {.pair = i};
194 : "=Q" (v->counter) : "d" (rp) );
197 static inline long long atomic64_xchg(atomic64_t *v, long long new)
199 register_pair rp_new = {.pair = new};
200 register_pair rp_old;
206 : "=&d" (rp_old), "=Q" (v->counter)
207 : "d" (rp_new), "Q" (v->counter)
212 static inline long long atomic64_cmpxchg(atomic64_t *v,
213 long long old, long long new)
215 register_pair rp_old = {.pair = old};
216 register_pair rp_new = {.pair = new};
220 : "+&d" (rp_old), "=Q" (v->counter)
221 : "d" (rp_new), "Q" (v->counter)
227 static inline long long atomic64_add_return(long long i, atomic64_t *v)
232 old = atomic64_read(v);
234 } while (atomic64_cmpxchg(v, old, new) != old);
238 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
243 old = atomic64_read(v);
245 } while (atomic64_cmpxchg(v, old, new) != old);
249 static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
254 old = atomic64_read(v);
256 } while (atomic64_cmpxchg(v, old, new) != old);
259 static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
264 old = atomic64_read(v);
266 } while (atomic64_cmpxchg(v, old, new) != old);
269 #endif /* CONFIG_64BIT */
271 static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
275 c = atomic64_read(v);
277 if (unlikely(c == u))
279 old = atomic64_cmpxchg(v, c, c + a);
280 if (likely(old == c))
287 static inline long long atomic64_dec_if_positive(atomic64_t *v)
289 long long c, old, dec;
291 c = atomic64_read(v);
294 if (unlikely(dec < 0))
296 old = atomic64_cmpxchg((v), c, dec);
297 if (likely(old == c))
304 #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
305 #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
306 #define atomic64_inc(_v) atomic64_add_return(1, _v)
307 #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
308 #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
309 #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
310 #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
311 #define atomic64_dec(_v) atomic64_sub_return(1, _v)
312 #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
313 #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
314 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
316 #define smp_mb__before_atomic_dec() smp_mb()
317 #define smp_mb__after_atomic_dec() smp_mb()
318 #define smp_mb__before_atomic_inc() smp_mb()
319 #define smp_mb__after_atomic_inc() smp_mb()
321 #include <asm-generic/atomic-long.h>
323 #endif /* __ARCH_S390_ATOMIC__ */