1 #ifndef __ARCH_S390_ATOMIC__
2 #define __ARCH_S390_ATOMIC__
4 #include <linux/compiler.h>
7 * include/asm-s390/atomic.h
10 * Copyright (C) 1999-2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
11 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
12 * Denis Joseph Barrow,
13 * Arnd Bergmann (arndb@de.ibm.com)
15 * Derived from "include/asm-i386/bitops.h"
16 * Copyright (C) 1992, Linus Torvalds
21 * Atomic operations that C can't guarantee us. Useful for
22 * resource counting etc..
23 * S390 uses 'Compare And Swap' for atomicity in SMP enviroment
28 } __attribute__ ((aligned (4))) atomic_t;
29 #define ATOMIC_INIT(i) { (i) }
33 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
35 #define __CS_LOOP(ptr, op_val, op_string) ({ \
36 typeof(ptr->counter) old_val, new_val; \
40 op_string " %1,%3\n" \
43 : "=&d" (old_val), "=&d" (new_val), \
44 "=Q" (((atomic_t *)(ptr))->counter) \
45 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
52 #define __CS_LOOP(ptr, op_val, op_string) ({ \
53 typeof(ptr->counter) old_val, new_val; \
57 op_string " %1,%4\n" \
60 : "=&d" (old_val), "=&d" (new_val), \
61 "=m" (((atomic_t *)(ptr))->counter) \
62 : "a" (ptr), "d" (op_val), \
63 "m" (((atomic_t *)(ptr))->counter) \
70 #define atomic_read(v) ((v)->counter)
71 #define atomic_set(v,i) (((v)->counter) = (i))
73 static __inline__ int atomic_add_return(int i, atomic_t * v)
75 return __CS_LOOP(v, i, "ar");
77 #define atomic_add(_i, _v) atomic_add_return(_i, _v)
78 #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
79 #define atomic_inc(_v) atomic_add_return(1, _v)
80 #define atomic_inc_return(_v) atomic_add_return(1, _v)
81 #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
83 static __inline__ int atomic_sub_return(int i, atomic_t * v)
85 return __CS_LOOP(v, i, "sr");
87 #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
88 #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
89 #define atomic_dec(_v) atomic_sub_return(1, _v)
90 #define atomic_dec_return(_v) atomic_sub_return(1, _v)
91 #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
93 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
95 __CS_LOOP(v, ~mask, "nr");
98 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
100 __CS_LOOP(v, mask, "or");
103 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
105 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
107 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
110 : "+d" (old), "=Q" (v->counter)
111 : "d" (new), "Q" (v->counter)
116 : "+d" (old), "=m" (v->counter)
117 : "a" (v), "d" (new), "m" (v->counter)
119 #endif /* __GNUC__ */
123 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
128 if (unlikely(c == u))
130 old = atomic_cmpxchg(v, c, c + a);
131 if (likely(old == c))
138 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
144 volatile long long counter;
145 } __attribute__ ((aligned (8))) atomic64_t;
146 #define ATOMIC64_INIT(i) { (i) }
148 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
150 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
151 typeof(ptr->counter) old_val, new_val; \
155 op_string " %1,%3\n" \
158 : "=&d" (old_val), "=&d" (new_val), \
159 "=Q" (((atomic_t *)(ptr))->counter) \
160 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
161 : "cc", "memory" ); \
167 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
168 typeof(ptr->counter) old_val, new_val; \
172 op_string " %1,%4\n" \
173 " csg %0,%1,0(%3)\n" \
175 : "=&d" (old_val), "=&d" (new_val), \
176 "=m" (((atomic_t *)(ptr))->counter) \
177 : "a" (ptr), "d" (op_val), \
178 "m" (((atomic_t *)(ptr))->counter) \
179 : "cc", "memory" ); \
183 #endif /* __GNUC__ */
185 #define atomic64_read(v) ((v)->counter)
186 #define atomic64_set(v,i) (((v)->counter) = (i))
188 static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
190 return __CSG_LOOP(v, i, "agr");
192 #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
193 #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
194 #define atomic64_inc(_v) atomic64_add_return(1, _v)
195 #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
196 #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
198 static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
200 return __CSG_LOOP(v, i, "sgr");
202 #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
203 #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
204 #define atomic64_dec(_v) atomic64_sub_return(1, _v)
205 #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
206 #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
208 static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
210 __CSG_LOOP(v, ~mask, "ngr");
213 static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
215 __CSG_LOOP(v, mask, "ogr");
218 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
220 static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
221 long long old, long long new)
223 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
226 : "+d" (old), "=Q" (v->counter)
227 : "d" (new), "Q" (v->counter)
232 : "+d" (old), "=m" (v->counter)
233 : "a" (v), "d" (new), "m" (v->counter)
235 #endif /* __GNUC__ */
239 static __inline__ int atomic64_add_unless(atomic64_t *v,
240 long long a, long long u)
243 c = atomic64_read(v);
245 if (unlikely(c == u))
247 old = atomic64_cmpxchg(v, c, c + a);
248 if (likely(old == c))
255 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
260 #define smp_mb__before_atomic_dec() smp_mb()
261 #define smp_mb__after_atomic_dec() smp_mb()
262 #define smp_mb__before_atomic_inc() smp_mb()
263 #define smp_mb__after_atomic_inc() smp_mb()
265 #include <asm-generic/atomic.h>
266 #endif /* __KERNEL__ */
267 #endif /* __ARCH_S390_ATOMIC__ */