blob: 76daea117181e9df886da524ebfb4147f54ff84a [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>
Heiko Carstens2ddb3ec2010-05-26 23:26:18 +020018#include <asm/system.h>
Dave Jones5bd1db62006-04-10 22:53:51 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define ATOMIC_INIT(i) { (i) }
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define __CS_LOOP(ptr, op_val, op_string) ({ \
Martin Schwidefsky39475172009-12-07 12:52:05 +010023 int old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020024 asm volatile( \
25 " l %0,%2\n" \
26 "0: lr %1,%0\n" \
27 op_string " %1,%3\n" \
28 " cs %0,%1,%2\n" \
29 " jl 0b" \
30 : "=&d" (old_val), "=&d" (new_val), \
31 "=Q" (((atomic_t *)(ptr))->counter) \
32 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
33 : "cc", "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 new_val; \
35})
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020036
Heiko Carstensc51b9622007-08-22 13:51:45 +020037static inline int atomic_read(const atomic_t *v)
38{
39 barrier();
40 return v->counter;
41}
42
43static inline void atomic_set(atomic_t *v, int i)
44{
45 v->counter = i;
46 barrier();
47}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Heiko Carstensbfe33492009-09-11 10:28:35 +020049static inline int atomic_add_return(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 return __CS_LOOP(v, i, "ar");
52}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080053#define atomic_add(_i, _v) atomic_add_return(_i, _v)
54#define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
55#define atomic_inc(_v) atomic_add_return(1, _v)
56#define atomic_inc_return(_v) atomic_add_return(1, _v)
57#define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
58
Heiko Carstensbfe33492009-09-11 10:28:35 +020059static inline int atomic_sub_return(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 return __CS_LOOP(v, i, "sr");
62}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080063#define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
64#define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
65#define atomic_dec(_v) atomic_sub_return(1, _v)
66#define atomic_dec_return(_v) atomic_sub_return(1, _v)
67#define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Heiko Carstensbfe33492009-09-11 10:28:35 +020069static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Heiko Carstensbfe33492009-09-11 10:28:35 +020071 __CS_LOOP(v, ~mask, "nr");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080073
Heiko Carstensbfe33492009-09-11 10:28:35 +020074static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Heiko Carstensbfe33492009-09-11 10:28:35 +020076 __CS_LOOP(v, mask, "or");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Martin Schwidefsky973bd992006-01-06 00:19:07 -080078
Ingo Molnarffbf6702006-01-09 15:59:17 -080079#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
80
Heiko Carstensbfe33492009-09-11 10:28:35 +020081static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
Martin Schwidefsky973bd992006-01-06 00:19:07 -080082{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020083 asm volatile(
84 " cs %0,%2,%1"
85 : "+d" (old), "=Q" (v->counter)
86 : "d" (new), "Q" (v->counter)
87 : "cc", "memory");
Martin Schwidefsky973bd992006-01-06 00:19:07 -080088 return old;
89}
90
Heiko Carstensbfe33492009-09-11 10:28:35 +020091static inline int atomic_add_unless(atomic_t *v, int a, int u)
Martin Schwidefsky973bd992006-01-06 00:19:07 -080092{
93 int c, old;
Martin Schwidefsky973bd992006-01-06 00:19:07 -080094 c = atomic_read(v);
Nick Piggin0b2fcfd2006-03-23 03:01:02 -080095 for (;;) {
96 if (unlikely(c == u))
97 break;
98 old = atomic_cmpxchg(v, c, c + a);
99 if (likely(old == c))
100 break;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800101 c = old;
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800102 }
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800103 return c != u;
104}
105
106#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#undef __CS_LOOP
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define ATOMIC64_INIT(i) { (i) }
111
Heiko Carstens12751052009-09-11 10:28:34 +0200112#ifdef CONFIG_64BIT
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define __CSG_LOOP(ptr, op_val, op_string) ({ \
Martin Schwidefsky39475172009-12-07 12:52:05 +0100115 long long old_val, new_val; \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200116 asm volatile( \
117 " lg %0,%2\n" \
118 "0: lgr %1,%0\n" \
119 op_string " %1,%3\n" \
120 " csg %0,%1,%2\n" \
121 " jl 0b" \
122 : "=&d" (old_val), "=&d" (new_val), \
123 "=Q" (((atomic_t *)(ptr))->counter) \
124 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
Heiko Carstensbfe33492009-09-11 10:28:35 +0200125 : "cc", "memory"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 new_val; \
127})
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200128
Heiko Carstensc51b9622007-08-22 13:51:45 +0200129static inline long long atomic64_read(const atomic64_t *v)
130{
131 barrier();
132 return v->counter;
133}
134
135static inline void atomic64_set(atomic64_t *v, long long i)
136{
137 v->counter = i;
138 barrier();
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Heiko Carstensbfe33492009-09-11 10:28:35 +0200141static inline long long atomic64_add_return(long long i, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 return __CSG_LOOP(v, i, "agr");
144}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800145
Heiko Carstensbfe33492009-09-11 10:28:35 +0200146static inline long long atomic64_sub_return(long long i, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800148 return __CSG_LOOP(v, i, "sgr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800150
Heiko Carstensbfe33492009-09-11 10:28:35 +0200151static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Heiko Carstensbfe33492009-09-11 10:28:35 +0200153 __CSG_LOOP(v, ~mask, "ngr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800155
Heiko Carstensbfe33492009-09-11 10:28:35 +0200156static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Heiko Carstensbfe33492009-09-11 10:28:35 +0200158 __CSG_LOOP(v, mask, "ogr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Mathieu Desnoyers3a5f10e2007-02-21 10:55:59 +0100161#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
162
Heiko Carstensbfe33492009-09-11 10:28:35 +0200163static inline long long atomic64_cmpxchg(atomic64_t *v,
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800164 long long old, long long new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200166 asm volatile(
167 " csg %0,%2,%1"
168 : "+d" (old), "=Q" (v->counter)
169 : "d" (new), "Q" (v->counter)
170 : "cc", "memory");
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800171 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Heiko Carstens12751052009-09-11 10:28:34 +0200174#undef __CSG_LOOP
175
176#else /* CONFIG_64BIT */
177
178typedef struct {
179 long long counter;
180} atomic64_t;
181
182static inline long long atomic64_read(const atomic64_t *v)
183{
184 register_pair rp;
185
186 asm volatile(
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100187 " lm %0,%N0,%1"
188 : "=&d" (rp) : "Q" (v->counter) );
Heiko Carstens12751052009-09-11 10:28:34 +0200189 return rp.pair;
190}
191
192static inline void atomic64_set(atomic64_t *v, long long i)
193{
194 register_pair rp = {.pair = i};
195
196 asm volatile(
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100197 " stm %1,%N1,%0"
198 : "=Q" (v->counter) : "d" (rp) );
Heiko Carstens12751052009-09-11 10:28:34 +0200199}
200
201static inline long long atomic64_xchg(atomic64_t *v, long long new)
202{
203 register_pair rp_new = {.pair = new};
204 register_pair rp_old;
205
206 asm volatile(
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100207 " lm %0,%N0,%1\n"
208 "0: cds %0,%2,%1\n"
Heiko Carstens12751052009-09-11 10:28:34 +0200209 " jl 0b\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100210 : "=&d" (rp_old), "=Q" (v->counter)
211 : "d" (rp_new), "Q" (v->counter)
Heiko Carstens12751052009-09-11 10:28:34 +0200212 : "cc");
213 return rp_old.pair;
214}
215
216static inline long long atomic64_cmpxchg(atomic64_t *v,
217 long long old, long long new)
218{
219 register_pair rp_old = {.pair = old};
220 register_pair rp_new = {.pair = new};
221
222 asm volatile(
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100223 " cds %0,%2,%1"
224 : "+&d" (rp_old), "=Q" (v->counter)
225 : "d" (rp_new), "Q" (v->counter)
Heiko Carstens12751052009-09-11 10:28:34 +0200226 : "cc");
227 return rp_old.pair;
228}
229
230
231static inline long long atomic64_add_return(long long i, atomic64_t *v)
232{
233 long long old, new;
234
235 do {
236 old = atomic64_read(v);
237 new = old + i;
238 } while (atomic64_cmpxchg(v, old, new) != old);
239 return new;
240}
241
242static inline long long atomic64_sub_return(long long i, atomic64_t *v)
243{
244 long long old, new;
245
246 do {
247 old = atomic64_read(v);
248 new = old - i;
249 } while (atomic64_cmpxchg(v, old, new) != old);
250 return new;
251}
252
253static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
254{
255 long long old, new;
256
257 do {
258 old = atomic64_read(v);
259 new = old | mask;
260 } while (atomic64_cmpxchg(v, old, new) != old);
261}
262
263static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
264{
265 long long old, new;
266
267 do {
268 old = atomic64_read(v);
269 new = old & mask;
270 } while (atomic64_cmpxchg(v, old, new) != old);
271}
272
273#endif /* CONFIG_64BIT */
274
Heiko Carstensbfe33492009-09-11 10:28:35 +0200275static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800276{
277 long long c, old;
Heiko Carstens2ddb3ec2010-05-26 23:26:18 +0200278
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800279 c = atomic64_read(v);
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800280 for (;;) {
281 if (unlikely(c == u))
282 break;
283 old = atomic64_cmpxchg(v, c, c + a);
284 if (likely(old == c))
285 break;
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800286 c = old;
Nick Piggin0b2fcfd2006-03-23 03:01:02 -0800287 }
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800288 return c != u;
289}
290
Heiko Carstens2ddb3ec2010-05-26 23:26:18 +0200291static inline long long atomic64_dec_if_positive(atomic64_t *v)
292{
293 long long c, old, dec;
294
295 c = atomic64_read(v);
296 for (;;) {
297 dec = c - 1;
298 if (unlikely(dec < 0))
299 break;
300 old = atomic64_cmpxchg((v), c, dec);
301 if (likely(old == c))
302 break;
303 c = old;
304 }
305 return dec;
306}
307
Heiko Carstens12751052009-09-11 10:28:34 +0200308#define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
309#define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
310#define atomic64_inc(_v) atomic64_add_return(1, _v)
311#define atomic64_inc_return(_v) atomic64_add_return(1, _v)
312#define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
313#define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
314#define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
315#define atomic64_dec(_v) atomic64_sub_return(1, _v)
316#define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
317#define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
318#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
Nick Piggin8426e1f2005-11-13 16:07:25 -0800319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#define smp_mb__before_atomic_dec() smp_mb()
321#define smp_mb__after_atomic_dec() smp_mb()
322#define smp_mb__before_atomic_inc() smp_mb()
323#define smp_mb__after_atomic_inc() smp_mb()
324
Arnd Bergmann72099ed2009-05-13 22:56:29 +0000325#include <asm-generic/atomic-long.h>
Heiko Carstensbfe33492009-09-11 10:28:35 +0200326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#endif /* __ARCH_S390_ATOMIC__ */