blob: b491d5e963cfd38d63a37b93c933996eb991adde [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ARCH_S390_ATOMIC__
2#define __ARCH_S390_ATOMIC__
3
Heiko Carstens12751052009-09-11 10:28:34 +02004/*
5 * Copyright 1999,2009 IBM Corp.
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Denis Joseph Barrow,
8 * Arnd Bergmann <arndb@de.ibm.com>,
9 *
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.
13 *
14 */
15
Dave Jones5bd1db62006-04-10 22:53:51 -070016#include <linux/compiler.h>
Matthew Wilcoxea4354672009-01-06 14:40:39 -080017#include <linux/types.h>
Dave Jones5bd1db62006-04-10 22:53:51 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define ATOMIC_INIT(i) { (i) }
20
21#ifdef __KERNEL__
22
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020023#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define __CS_LOOP(ptr, op_val, op_string) ({ \
26 typeof(ptr->counter) old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020027 asm volatile( \
28 " l %0,%2\n" \
29 "0: lr %1,%0\n" \
30 op_string " %1,%3\n" \
31 " cs %0,%1,%2\n" \
32 " jl 0b" \
33 : "=&d" (old_val), "=&d" (new_val), \
34 "=Q" (((atomic_t *)(ptr))->counter) \
35 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
36 : "cc", "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 new_val; \
38})
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020039
40#else /* __GNUC__ */
41
42#define __CS_LOOP(ptr, op_val, op_string) ({ \
43 typeof(ptr->counter) old_val, new_val; \
44 asm volatile( \
45 " l %0,0(%3)\n" \
46 "0: lr %1,%0\n" \
47 op_string " %1,%4\n" \
48 " cs %0,%1,0(%3)\n" \
49 " jl 0b" \
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) \
54 : "cc", "memory"); \
55 new_val; \
56})
57
58#endif /* __GNUC__ */
59
Heiko Carstensc51b9622007-08-22 13:51:45 +020060static inline int atomic_read(const atomic_t *v)
61{
62 barrier();
63 return v->counter;
64}
65
66static inline void atomic_set(atomic_t *v, int i)
67{
68 v->counter = i;
69 barrier();
70}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static __inline__ int atomic_add_return(int i, atomic_t * v)
73{
74 return __CS_LOOP(v, i, "ar");
75}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080076#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)
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static __inline__ int atomic_sub_return(int i, atomic_t * v)
83{
84 return __CS_LOOP(v, i, "sr");
85}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080086#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
93{
94 __CS_LOOP(v, ~mask, "nr");
95}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
98{
99 __CS_LOOP(v, mask, "or");
100}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800101
Ingo Molnarffbf6702006-01-09 15:59:17 -0800102#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
103
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800104static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
105{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200106#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
107 asm volatile(
108 " cs %0,%2,%1"
109 : "+d" (old), "=Q" (v->counter)
110 : "d" (new), "Q" (v->counter)
111 : "cc", "memory");
112#else /* __GNUC__ */
113 asm volatile(
114 " cs %0,%3,0(%2)"
115 : "+d" (old), "=m" (v->counter)
116 : "a" (v), "d" (new), "m" (v->counter)
117 : "cc", "memory");
118#endif /* __GNUC__ */
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800119 return old;
120}
121
122static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
123{
124 int c, old;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800125 c = atomic_read(v);
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800126 for (;;) {
127 if (unlikely(c == u))
128 break;
129 old = atomic_cmpxchg(v, c, c + a);
130 if (likely(old == c))
131 break;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800132 c = old;
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800133 }
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800134 return c != u;
135}
136
137#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#undef __CS_LOOP
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define ATOMIC64_INIT(i) { (i) }
142
Heiko Carstens12751052009-09-11 10:28:34 +0200143#ifdef CONFIG_64BIT
144
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200145#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define __CSG_LOOP(ptr, op_val, op_string) ({ \
148 typeof(ptr->counter) old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200149 asm volatile( \
150 " lg %0,%2\n" \
151 "0: lgr %1,%0\n" \
152 op_string " %1,%3\n" \
153 " csg %0,%1,%2\n" \
154 " jl 0b" \
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" ); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 new_val; \
160})
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200161
162#else /* __GNUC__ */
163
164#define __CSG_LOOP(ptr, op_val, op_string) ({ \
165 typeof(ptr->counter) old_val, new_val; \
166 asm volatile( \
167 " lg %0,0(%3)\n" \
168 "0: lgr %1,%0\n" \
169 op_string " %1,%4\n" \
170 " csg %0,%1,0(%3)\n" \
171 " jl 0b" \
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" ); \
177 new_val; \
178})
179
180#endif /* __GNUC__ */
181
Heiko Carstensc51b9622007-08-22 13:51:45 +0200182static inline long long atomic64_read(const atomic64_t *v)
183{
184 barrier();
185 return v->counter;
186}
187
188static inline void atomic64_set(atomic64_t *v, long long i)
189{
190 v->counter = i;
191 barrier();
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Heiko Carstens46ee0582005-07-27 11:44:59 -0700194static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 return __CSG_LOOP(v, i, "agr");
197}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800198
199static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800201 return __CSG_LOOP(v, i, "sgr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
205{
206 __CSG_LOOP(v, ~mask, "ngr");
207}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
210{
211 __CSG_LOOP(v, mask, "ogr");
212}
213
Mathieu Desnoyers3a5f10e2007-02-21 10:55:59 +0100214#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
215
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800216static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
217 long long old, long long new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200219#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
220 asm volatile(
221 " csg %0,%2,%1"
222 : "+d" (old), "=Q" (v->counter)
223 : "d" (new), "Q" (v->counter)
224 : "cc", "memory");
225#else /* __GNUC__ */
226 asm volatile(
227 " csg %0,%3,0(%2)"
228 : "+d" (old), "=m" (v->counter)
229 : "a" (v), "d" (new), "m" (v->counter)
230 : "cc", "memory");
231#endif /* __GNUC__ */
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800232 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Heiko Carstens12751052009-09-11 10:28:34 +0200235#undef __CSG_LOOP
236
237#else /* CONFIG_64BIT */
238
239typedef struct {
240 long long counter;
241} atomic64_t;
242
243static inline long long atomic64_read(const atomic64_t *v)
244{
245 register_pair rp;
246
247 asm volatile(
248 " lm %0,%N0,0(%1)"
249 : "=&d" (rp)
250 : "a" (&v->counter), "m" (v->counter)
251 );
252 return rp.pair;
253}
254
255static inline void atomic64_set(atomic64_t *v, long long i)
256{
257 register_pair rp = {.pair = i};
258
259 asm volatile(
260 " stm %1,%N1,0(%2)"
261 : "=m" (v->counter)
262 : "d" (rp), "a" (&v->counter)
263 );
264}
265
266static inline long long atomic64_xchg(atomic64_t *v, long long new)
267{
268 register_pair rp_new = {.pair = new};
269 register_pair rp_old;
270
271 asm volatile(
272 " lm %0,%N0,0(%2)\n"
273 "0: cds %0,%3,0(%2)\n"
274 " jl 0b\n"
275 : "=&d" (rp_old), "+m" (v->counter)
276 : "a" (&v->counter), "d" (rp_new)
277 : "cc");
278 return rp_old.pair;
279}
280
281static inline long long atomic64_cmpxchg(atomic64_t *v,
282 long long old, long long new)
283{
284 register_pair rp_old = {.pair = old};
285 register_pair rp_new = {.pair = new};
286
287 asm volatile(
288 " cds %0,%3,0(%2)"
289 : "+&d" (rp_old), "+m" (v->counter)
290 : "a" (&v->counter), "d" (rp_new)
291 : "cc");
292 return rp_old.pair;
293}
294
295
296static inline long long atomic64_add_return(long long i, atomic64_t *v)
297{
298 long long old, new;
299
300 do {
301 old = atomic64_read(v);
302 new = old + i;
303 } while (atomic64_cmpxchg(v, old, new) != old);
304 return new;
305}
306
307static inline long long atomic64_sub_return(long long i, atomic64_t *v)
308{
309 long long old, new;
310
311 do {
312 old = atomic64_read(v);
313 new = old - i;
314 } while (atomic64_cmpxchg(v, old, new) != old);
315 return new;
316}
317
318static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
319{
320 long long old, new;
321
322 do {
323 old = atomic64_read(v);
324 new = old | mask;
325 } while (atomic64_cmpxchg(v, old, new) != old);
326}
327
328static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
329{
330 long long old, new;
331
332 do {
333 old = atomic64_read(v);
334 new = old & mask;
335 } while (atomic64_cmpxchg(v, old, new) != old);
336}
337
338#endif /* CONFIG_64BIT */
339
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800340static __inline__ int atomic64_add_unless(atomic64_t *v,
341 long long a, long long u)
342{
343 long long c, old;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800344 c = atomic64_read(v);
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800345 for (;;) {
346 if (unlikely(c == u))
347 break;
348 old = atomic64_cmpxchg(v, c, c + a);
349 if (likely(old == c))
350 break;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800351 c = old;
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800352 }
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800353 return c != u;
354}
355
Heiko Carstens12751052009-09-11 10:28:34 +0200356#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)
Nick Piggin8426e1f2005-11-13 16:07:25 -0800367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#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()
372
Arnd Bergmann72099ed2009-05-13 22:56:29 +0000373#include <asm-generic/atomic-long.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#endif /* __KERNEL__ */
375#endif /* __ARCH_S390_ATOMIC__ */