blob: 2a113d6a7dfd1fe95d63ba1c73edd49449f8c4e0 [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
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020021#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define __CS_LOOP(ptr, op_val, op_string) ({ \
Martin Schwidefsky39475172009-12-07 12:52:05 +010024 int old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020025 asm volatile( \
26 " l %0,%2\n" \
27 "0: lr %1,%0\n" \
28 op_string " %1,%3\n" \
29 " cs %0,%1,%2\n" \
30 " jl 0b" \
31 : "=&d" (old_val), "=&d" (new_val), \
32 "=Q" (((atomic_t *)(ptr))->counter) \
33 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
34 : "cc", "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 new_val; \
36})
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020037
38#else /* __GNUC__ */
39
40#define __CS_LOOP(ptr, op_val, op_string) ({ \
Martin Schwidefsky39475172009-12-07 12:52:05 +010041 int old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020042 asm volatile( \
43 " l %0,0(%3)\n" \
44 "0: lr %1,%0\n" \
45 op_string " %1,%4\n" \
46 " cs %0,%1,0(%3)\n" \
47 " jl 0b" \
48 : "=&d" (old_val), "=&d" (new_val), \
49 "=m" (((atomic_t *)(ptr))->counter) \
50 : "a" (ptr), "d" (op_val), \
51 "m" (((atomic_t *)(ptr))->counter) \
52 : "cc", "memory"); \
53 new_val; \
54})
55
56#endif /* __GNUC__ */
57
Heiko Carstensc51b9622007-08-22 13:51:45 +020058static inline int atomic_read(const atomic_t *v)
59{
60 barrier();
61 return v->counter;
62}
63
64static inline void atomic_set(atomic_t *v, int i)
65{
66 v->counter = i;
67 barrier();
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Heiko Carstensbfe3349b2009-09-11 10:28:35 +020070static inline int atomic_add_return(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 return __CS_LOOP(v, i, "ar");
73}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080074#define atomic_add(_i, _v) atomic_add_return(_i, _v)
75#define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
76#define atomic_inc(_v) atomic_add_return(1, _v)
77#define atomic_inc_return(_v) atomic_add_return(1, _v)
78#define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
79
Heiko Carstensbfe3349b2009-09-11 10:28:35 +020080static inline int atomic_sub_return(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 return __CS_LOOP(v, i, "sr");
83}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080084#define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
85#define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
86#define atomic_dec(_v) atomic_sub_return(1, _v)
87#define atomic_dec_return(_v) atomic_sub_return(1, _v)
88#define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Heiko Carstensbfe3349b2009-09-11 10:28:35 +020090static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Heiko Carstensbfe3349b2009-09-11 10:28:35 +020092 __CS_LOOP(v, ~mask, "nr");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080094
Heiko Carstensbfe3349b2009-09-11 10:28:35 +020095static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Heiko Carstensbfe3349b2009-09-11 10:28:35 +020097 __CS_LOOP(v, mask, "or");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080099
Ingo Molnarffbf6702006-01-09 15:59:17 -0800100#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
101
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200102static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800103{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200104#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
105 asm volatile(
106 " cs %0,%2,%1"
107 : "+d" (old), "=Q" (v->counter)
108 : "d" (new), "Q" (v->counter)
109 : "cc", "memory");
110#else /* __GNUC__ */
111 asm volatile(
112 " cs %0,%3,0(%2)"
113 : "+d" (old), "=m" (v->counter)
114 : "a" (v), "d" (new), "m" (v->counter)
115 : "cc", "memory");
116#endif /* __GNUC__ */
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800117 return old;
118}
119
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200120static inline int atomic_add_unless(atomic_t *v, int a, int u)
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800121{
122 int c, old;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800123 c = atomic_read(v);
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800124 for (;;) {
125 if (unlikely(c == u))
126 break;
127 old = atomic_cmpxchg(v, c, c + a);
128 if (likely(old == c))
129 break;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800130 c = old;
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800131 }
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800132 return c != u;
133}
134
135#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#undef __CS_LOOP
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define ATOMIC64_INIT(i) { (i) }
140
Heiko Carstens12751052009-09-11 10:28:34 +0200141#ifdef CONFIG_64BIT
142
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200143#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define __CSG_LOOP(ptr, op_val, op_string) ({ \
Martin Schwidefsky39475172009-12-07 12:52:05 +0100146 long long old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200147 asm volatile( \
148 " lg %0,%2\n" \
149 "0: lgr %1,%0\n" \
150 op_string " %1,%3\n" \
151 " csg %0,%1,%2\n" \
152 " jl 0b" \
153 : "=&d" (old_val), "=&d" (new_val), \
154 "=Q" (((atomic_t *)(ptr))->counter) \
155 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200156 : "cc", "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 new_val; \
158})
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200159
160#else /* __GNUC__ */
161
162#define __CSG_LOOP(ptr, op_val, op_string) ({ \
Martin Schwidefsky39475172009-12-07 12:52:05 +0100163 long long old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200164 asm volatile( \
165 " lg %0,0(%3)\n" \
166 "0: lgr %1,%0\n" \
167 op_string " %1,%4\n" \
168 " csg %0,%1,0(%3)\n" \
169 " jl 0b" \
170 : "=&d" (old_val), "=&d" (new_val), \
171 "=m" (((atomic_t *)(ptr))->counter) \
172 : "a" (ptr), "d" (op_val), \
173 "m" (((atomic_t *)(ptr))->counter) \
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200174 : "cc", "memory"); \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200175 new_val; \
176})
177
178#endif /* __GNUC__ */
179
Heiko Carstensc51b9622007-08-22 13:51:45 +0200180static inline long long atomic64_read(const atomic64_t *v)
181{
182 barrier();
183 return v->counter;
184}
185
186static inline void atomic64_set(atomic64_t *v, long long i)
187{
188 v->counter = i;
189 barrier();
190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200192static inline long long atomic64_add_return(long long i, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 return __CSG_LOOP(v, i, "agr");
195}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800196
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200197static inline long long atomic64_sub_return(long long i, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800199 return __CSG_LOOP(v, i, "sgr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800201
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200202static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200204 __CSG_LOOP(v, ~mask, "ngr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800206
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200207static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200209 __CSG_LOOP(v, mask, "ogr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Mathieu Desnoyers3a5f10e2007-02-21 10:55:59 +0100212#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
213
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200214static inline long long atomic64_cmpxchg(atomic64_t *v,
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800215 long long old, long long new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200217#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
218 asm volatile(
219 " csg %0,%2,%1"
220 : "+d" (old), "=Q" (v->counter)
221 : "d" (new), "Q" (v->counter)
222 : "cc", "memory");
223#else /* __GNUC__ */
224 asm volatile(
225 " csg %0,%3,0(%2)"
226 : "+d" (old), "=m" (v->counter)
227 : "a" (v), "d" (new), "m" (v->counter)
228 : "cc", "memory");
229#endif /* __GNUC__ */
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800230 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Heiko Carstens12751052009-09-11 10:28:34 +0200233#undef __CSG_LOOP
234
235#else /* CONFIG_64BIT */
236
237typedef struct {
238 long long counter;
239} atomic64_t;
240
241static inline long long atomic64_read(const atomic64_t *v)
242{
243 register_pair rp;
244
245 asm volatile(
246 " lm %0,%N0,0(%1)"
247 : "=&d" (rp)
248 : "a" (&v->counter), "m" (v->counter)
249 );
250 return rp.pair;
251}
252
253static inline void atomic64_set(atomic64_t *v, long long i)
254{
255 register_pair rp = {.pair = i};
256
257 asm volatile(
258 " stm %1,%N1,0(%2)"
259 : "=m" (v->counter)
260 : "d" (rp), "a" (&v->counter)
261 );
262}
263
264static inline long long atomic64_xchg(atomic64_t *v, long long new)
265{
266 register_pair rp_new = {.pair = new};
267 register_pair rp_old;
268
269 asm volatile(
270 " lm %0,%N0,0(%2)\n"
271 "0: cds %0,%3,0(%2)\n"
272 " jl 0b\n"
273 : "=&d" (rp_old), "+m" (v->counter)
274 : "a" (&v->counter), "d" (rp_new)
275 : "cc");
276 return rp_old.pair;
277}
278
279static inline long long atomic64_cmpxchg(atomic64_t *v,
280 long long old, long long new)
281{
282 register_pair rp_old = {.pair = old};
283 register_pair rp_new = {.pair = new};
284
285 asm volatile(
286 " cds %0,%3,0(%2)"
287 : "+&d" (rp_old), "+m" (v->counter)
288 : "a" (&v->counter), "d" (rp_new)
289 : "cc");
290 return rp_old.pair;
291}
292
293
294static inline long long atomic64_add_return(long long i, atomic64_t *v)
295{
296 long long old, new;
297
298 do {
299 old = atomic64_read(v);
300 new = old + i;
301 } while (atomic64_cmpxchg(v, old, new) != old);
302 return new;
303}
304
305static inline long long atomic64_sub_return(long long i, atomic64_t *v)
306{
307 long long old, new;
308
309 do {
310 old = atomic64_read(v);
311 new = old - i;
312 } while (atomic64_cmpxchg(v, old, new) != old);
313 return new;
314}
315
316static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
317{
318 long long old, new;
319
320 do {
321 old = atomic64_read(v);
322 new = old | mask;
323 } while (atomic64_cmpxchg(v, old, new) != old);
324}
325
326static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
327{
328 long long old, new;
329
330 do {
331 old = atomic64_read(v);
332 new = old & mask;
333 } while (atomic64_cmpxchg(v, old, new) != old);
334}
335
336#endif /* CONFIG_64BIT */
337
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200338static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800339{
340 long long c, old;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800341 c = atomic64_read(v);
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800342 for (;;) {
343 if (unlikely(c == u))
344 break;
345 old = atomic64_cmpxchg(v, c, c + a);
346 if (likely(old == c))
347 break;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800348 c = old;
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800349 }
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800350 return c != u;
351}
352
Heiko Carstens12751052009-09-11 10:28:34 +0200353#define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
354#define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
355#define atomic64_inc(_v) atomic64_add_return(1, _v)
356#define atomic64_inc_return(_v) atomic64_add_return(1, _v)
357#define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
358#define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
359#define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
360#define atomic64_dec(_v) atomic64_sub_return(1, _v)
361#define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
362#define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
363#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
Nick Piggin8426e1f2005-11-13 16:07:25 -0800364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#define smp_mb__before_atomic_dec() smp_mb()
366#define smp_mb__after_atomic_dec() smp_mb()
367#define smp_mb__before_atomic_inc() smp_mb()
368#define smp_mb__after_atomic_inc() smp_mb()
369
Arnd Bergmann72099ed2009-05-13 22:56:29 +0000370#include <asm-generic/atomic-long.h>
Heiko Carstensbfe3349b2009-09-11 10:28:35 +0200371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#endif /* __ARCH_S390_ATOMIC__ */