1 #ifndef __ARCH_S390_ATOMIC__
2 #define __ARCH_S390_ATOMIC__
5 * Copyright 1999,2009 IBM Corp.
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 environment.
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)
43 : "=d" (c) : "Q" (v->counter));
47 static inline void atomic_set(atomic_t *v, int i)
51 : "=Q" (v->counter) : "d" (i));
54 static inline int atomic_add_return(int i, atomic_t *v)
56 return __CS_LOOP(v, i, "ar");
58 #define atomic_add(_i, _v) atomic_add_return(_i, _v)
59 #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
60 #define atomic_inc(_v) atomic_add_return(1, _v)
61 #define atomic_inc_return(_v) atomic_add_return(1, _v)
62 #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
64 static inline int atomic_sub_return(int i, atomic_t *v)
66 return __CS_LOOP(v, i, "sr");
68 #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
69 #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
70 #define atomic_dec(_v) atomic_sub_return(1, _v)
71 #define atomic_dec_return(_v) atomic_sub_return(1, _v)
72 #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
74 static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
76 __CS_LOOP(v, ~mask, "nr");
79 static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
81 __CS_LOOP(v, mask, "or");
84 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
86 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
90 : "+d" (old), "=Q" (v->counter)
91 : "d" (new), "Q" (v->counter)
96 static inline int atomic_add_unless(atomic_t *v, int a, int u)
101 if (unlikely(c == u))
103 old = atomic_cmpxchg(v, c, c + a);
104 if (likely(old == c))
111 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
115 #define ATOMIC64_INIT(i) { (i) }
119 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
120 long long old_val, new_val; \
124 op_string " %1,%3\n" \
127 : "=&d" (old_val), "=&d" (new_val), \
128 "=Q" (((atomic_t *)(ptr))->counter) \
129 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
134 static inline long long atomic64_read(const atomic64_t *v)
140 : "=d" (c) : "Q" (v->counter));
144 static inline void atomic64_set(atomic64_t *v, long long i)
148 : "=Q" (v->counter) : "d" (i));
151 static inline long long atomic64_add_return(long long i, atomic64_t *v)
153 return __CSG_LOOP(v, i, "agr");
156 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
158 return __CSG_LOOP(v, i, "sgr");
161 static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
163 __CSG_LOOP(v, ~mask, "ngr");
166 static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
168 __CSG_LOOP(v, mask, "ogr");
171 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
173 static inline long long atomic64_cmpxchg(atomic64_t *v,
174 long long old, long long new)
178 : "+d" (old), "=Q" (v->counter)
179 : "d" (new), "Q" (v->counter)
186 #else /* CONFIG_64BIT */
192 static inline long long atomic64_read(const atomic64_t *v)
198 : "=&d" (rp) : "Q" (v->counter) );
202 static inline void atomic64_set(atomic64_t *v, long long i)
204 register_pair rp = {.pair = i};
208 : "=Q" (v->counter) : "d" (rp) );
211 static inline long long atomic64_xchg(atomic64_t *v, long long new)
213 register_pair rp_new = {.pair = new};
214 register_pair rp_old;
220 : "=&d" (rp_old), "=Q" (v->counter)
221 : "d" (rp_new), "Q" (v->counter)
226 static inline long long atomic64_cmpxchg(atomic64_t *v,
227 long long old, long long new)
229 register_pair rp_old = {.pair = old};
230 register_pair rp_new = {.pair = new};
234 : "+&d" (rp_old), "=Q" (v->counter)
235 : "d" (rp_new), "Q" (v->counter)
241 static inline long long atomic64_add_return(long long i, atomic64_t *v)
246 old = atomic64_read(v);
248 } while (atomic64_cmpxchg(v, old, new) != old);
252 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
257 old = atomic64_read(v);
259 } while (atomic64_cmpxchg(v, old, new) != old);
263 static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
268 old = atomic64_read(v);
270 } while (atomic64_cmpxchg(v, old, new) != old);
273 static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
278 old = atomic64_read(v);
280 } while (atomic64_cmpxchg(v, old, new) != old);
283 #endif /* CONFIG_64BIT */
285 static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
289 c = atomic64_read(v);
291 if (unlikely(c == u))
293 old = atomic64_cmpxchg(v, c, c + a);
294 if (likely(old == c))
301 static inline long long atomic64_dec_if_positive(atomic64_t *v)
303 long long c, old, dec;
305 c = atomic64_read(v);
308 if (unlikely(dec < 0))
310 old = atomic64_cmpxchg((v), c, dec);
311 if (likely(old == c))
318 #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
319 #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
320 #define atomic64_inc(_v) atomic64_add_return(1, _v)
321 #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
322 #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
323 #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
324 #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
325 #define atomic64_dec(_v) atomic64_sub_return(1, _v)
326 #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
327 #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
328 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
330 #define smp_mb__before_atomic_dec() smp_mb()
331 #define smp_mb__after_atomic_dec() smp_mb()
332 #define smp_mb__before_atomic_inc() smp_mb()
333 #define smp_mb__after_atomic_inc() smp_mb()
335 #include <asm-generic/atomic-long.h>
337 #endif /* __ARCH_S390_ATOMIC__ */