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>
19 #define ATOMIC_INIT(i) { (i) }
21 #define __CS_LOOP(ptr, op_val, op_string) ({ \
22 int old_val, new_val; \
26 op_string " %1,%3\n" \
29 : "=&d" (old_val), "=&d" (new_val), \
30 "=Q" (((atomic_t *)(ptr))->counter) \
31 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
36 static inline int atomic_read(const atomic_t *v)
42 static inline void atomic_set(atomic_t *v, int i)
48 static inline int atomic_add_return(int i, atomic_t *v)
50 return __CS_LOOP(v, i, "ar");
52 #define atomic_add(_i, _v) atomic_add_return(_i, _v)
53 #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
54 #define atomic_inc(_v) atomic_add_return(1, _v)
55 #define atomic_inc_return(_v) atomic_add_return(1, _v)
56 #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
58 static inline int atomic_sub_return(int i, atomic_t *v)
60 return __CS_LOOP(v, i, "sr");
62 #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
63 #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
64 #define atomic_dec(_v) atomic_sub_return(1, _v)
65 #define atomic_dec_return(_v) atomic_sub_return(1, _v)
66 #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
68 static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
70 __CS_LOOP(v, ~mask, "nr");
73 static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
75 __CS_LOOP(v, mask, "or");
78 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
80 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
84 : "+d" (old), "=Q" (v->counter)
85 : "d" (new), "Q" (v->counter)
90 static inline int atomic_add_unless(atomic_t *v, int a, int u)
97 old = atomic_cmpxchg(v, c, c + a);
105 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
109 #define ATOMIC64_INIT(i) { (i) }
113 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
114 long long old_val, new_val; \
118 op_string " %1,%3\n" \
121 : "=&d" (old_val), "=&d" (new_val), \
122 "=Q" (((atomic_t *)(ptr))->counter) \
123 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
128 static inline long long atomic64_read(const atomic64_t *v)
134 static inline void atomic64_set(atomic64_t *v, long long i)
140 static inline long long atomic64_add_return(long long i, atomic64_t *v)
142 return __CSG_LOOP(v, i, "agr");
145 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
147 return __CSG_LOOP(v, i, "sgr");
150 static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
152 __CSG_LOOP(v, ~mask, "ngr");
155 static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
157 __CSG_LOOP(v, mask, "ogr");
160 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
162 static inline long long atomic64_cmpxchg(atomic64_t *v,
163 long long old, long long new)
167 : "+d" (old), "=Q" (v->counter)
168 : "d" (new), "Q" (v->counter)
175 #else /* CONFIG_64BIT */
181 static inline long long atomic64_read(const atomic64_t *v)
187 : "=&d" (rp) : "Q" (v->counter) );
191 static inline void atomic64_set(atomic64_t *v, long long i)
193 register_pair rp = {.pair = i};
197 : "=Q" (v->counter) : "d" (rp) );
200 static inline long long atomic64_xchg(atomic64_t *v, long long new)
202 register_pair rp_new = {.pair = new};
203 register_pair rp_old;
209 : "=&d" (rp_old), "=Q" (v->counter)
210 : "d" (rp_new), "Q" (v->counter)
215 static inline long long atomic64_cmpxchg(atomic64_t *v,
216 long long old, long long new)
218 register_pair rp_old = {.pair = old};
219 register_pair rp_new = {.pair = new};
223 : "+&d" (rp_old), "=Q" (v->counter)
224 : "d" (rp_new), "Q" (v->counter)
230 static inline long long atomic64_add_return(long long i, atomic64_t *v)
235 old = atomic64_read(v);
237 } while (atomic64_cmpxchg(v, old, new) != old);
241 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
246 old = atomic64_read(v);
248 } while (atomic64_cmpxchg(v, old, new) != old);
252 static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
257 old = atomic64_read(v);
259 } while (atomic64_cmpxchg(v, old, new) != old);
262 static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
267 old = atomic64_read(v);
269 } while (atomic64_cmpxchg(v, old, new) != old);
272 #endif /* CONFIG_64BIT */
274 static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
277 c = atomic64_read(v);
279 if (unlikely(c == u))
281 old = atomic64_cmpxchg(v, c, c + a);
282 if (likely(old == c))
289 #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
290 #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
291 #define atomic64_inc(_v) atomic64_add_return(1, _v)
292 #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
293 #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
294 #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
295 #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
296 #define atomic64_dec(_v) atomic64_sub_return(1, _v)
297 #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
298 #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
299 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
301 #define smp_mb__before_atomic_dec() smp_mb()
302 #define smp_mb__after_atomic_dec() smp_mb()
303 #define smp_mb__before_atomic_inc() smp_mb()
304 #define smp_mb__after_atomic_inc() smp_mb()
306 #include <asm-generic/atomic-long.h>
308 #endif /* __ARCH_S390_ATOMIC__ */