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) }
23 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
25 #define __CS_LOOP(ptr, op_val, op_string) ({ \
26 typeof(ptr->counter) old_val, new_val; \
30 op_string " %1,%3\n" \
33 : "=&d" (old_val), "=&d" (new_val), \
34 "=Q" (((atomic_t *)(ptr))->counter) \
35 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
42 #define __CS_LOOP(ptr, op_val, op_string) ({ \
43 typeof(ptr->counter) old_val, new_val; \
47 op_string " %1,%4\n" \
50 : "=&d" (old_val), "=&d" (new_val), \
51 "=m" (((atomic_t *)(ptr))->counter) \
52 : "a" (ptr), "d" (op_val), \
53 "m" (((atomic_t *)(ptr))->counter) \
60 static inline int atomic_read(const atomic_t *v)
66 static inline void atomic_set(atomic_t *v, int i)
72 static __inline__ int atomic_add_return(int i, atomic_t * v)
74 return __CS_LOOP(v, i, "ar");
76 #define atomic_add(_i, _v) atomic_add_return(_i, _v)
77 #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
78 #define atomic_inc(_v) atomic_add_return(1, _v)
79 #define atomic_inc_return(_v) atomic_add_return(1, _v)
80 #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
82 static __inline__ int atomic_sub_return(int i, atomic_t * v)
84 return __CS_LOOP(v, i, "sr");
86 #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
87 #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
88 #define atomic_dec(_v) atomic_sub_return(1, _v)
89 #define atomic_dec_return(_v) atomic_sub_return(1, _v)
90 #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
92 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
94 __CS_LOOP(v, ~mask, "nr");
97 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
99 __CS_LOOP(v, mask, "or");
102 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
104 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
106 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
109 : "+d" (old), "=Q" (v->counter)
110 : "d" (new), "Q" (v->counter)
115 : "+d" (old), "=m" (v->counter)
116 : "a" (v), "d" (new), "m" (v->counter)
118 #endif /* __GNUC__ */
122 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
127 if (unlikely(c == u))
129 old = atomic_cmpxchg(v, c, c + a);
130 if (likely(old == c))
137 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
141 #define ATOMIC64_INIT(i) { (i) }
145 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
147 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
148 typeof(ptr->counter) old_val, new_val; \
152 op_string " %1,%3\n" \
155 : "=&d" (old_val), "=&d" (new_val), \
156 "=Q" (((atomic_t *)(ptr))->counter) \
157 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
158 : "cc", "memory" ); \
164 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
165 typeof(ptr->counter) old_val, new_val; \
169 op_string " %1,%4\n" \
170 " csg %0,%1,0(%3)\n" \
172 : "=&d" (old_val), "=&d" (new_val), \
173 "=m" (((atomic_t *)(ptr))->counter) \
174 : "a" (ptr), "d" (op_val), \
175 "m" (((atomic_t *)(ptr))->counter) \
176 : "cc", "memory" ); \
180 #endif /* __GNUC__ */
182 static inline long long atomic64_read(const atomic64_t *v)
188 static inline void atomic64_set(atomic64_t *v, long long i)
194 static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
196 return __CSG_LOOP(v, i, "agr");
199 static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
201 return __CSG_LOOP(v, i, "sgr");
204 static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
206 __CSG_LOOP(v, ~mask, "ngr");
209 static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
211 __CSG_LOOP(v, mask, "ogr");
214 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
216 static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
217 long long old, long long new)
219 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
222 : "+d" (old), "=Q" (v->counter)
223 : "d" (new), "Q" (v->counter)
228 : "+d" (old), "=m" (v->counter)
229 : "a" (v), "d" (new), "m" (v->counter)
231 #endif /* __GNUC__ */
237 #else /* CONFIG_64BIT */
243 static inline long long atomic64_read(const atomic64_t *v)
250 : "a" (&v->counter), "m" (v->counter)
255 static inline void atomic64_set(atomic64_t *v, long long i)
257 register_pair rp = {.pair = i};
262 : "d" (rp), "a" (&v->counter)
266 static inline long long atomic64_xchg(atomic64_t *v, long long new)
268 register_pair rp_new = {.pair = new};
269 register_pair rp_old;
273 "0: cds %0,%3,0(%2)\n"
275 : "=&d" (rp_old), "+m" (v->counter)
276 : "a" (&v->counter), "d" (rp_new)
281 static inline long long atomic64_cmpxchg(atomic64_t *v,
282 long long old, long long new)
284 register_pair rp_old = {.pair = old};
285 register_pair rp_new = {.pair = new};
289 : "+&d" (rp_old), "+m" (v->counter)
290 : "a" (&v->counter), "d" (rp_new)
296 static inline long long atomic64_add_return(long long i, atomic64_t *v)
301 old = atomic64_read(v);
303 } while (atomic64_cmpxchg(v, old, new) != old);
307 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
312 old = atomic64_read(v);
314 } while (atomic64_cmpxchg(v, old, new) != old);
318 static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
323 old = atomic64_read(v);
325 } while (atomic64_cmpxchg(v, old, new) != old);
328 static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
333 old = atomic64_read(v);
335 } while (atomic64_cmpxchg(v, old, new) != old);
338 #endif /* CONFIG_64BIT */
340 static __inline__ int atomic64_add_unless(atomic64_t *v,
341 long long a, long long u)
344 c = atomic64_read(v);
346 if (unlikely(c == u))
348 old = atomic64_cmpxchg(v, c, c + a);
349 if (likely(old == c))
356 #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
357 #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
358 #define atomic64_inc(_v) atomic64_add_return(1, _v)
359 #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
360 #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
361 #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
362 #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
363 #define atomic64_dec(_v) atomic64_sub_return(1, _v)
364 #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
365 #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
366 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
368 #define smp_mb__before_atomic_dec() smp_mb()
369 #define smp_mb__after_atomic_dec() smp_mb()
370 #define smp_mb__before_atomic_inc() smp_mb()
371 #define smp_mb__after_atomic_inc() smp_mb()
373 #include <asm-generic/atomic-long.h>
374 #endif /* __KERNEL__ */
375 #endif /* __ARCH_S390_ATOMIC__ */