blob: 2524fb60fbc28518ad7635092ac1bdd5d62114fd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _ASM_IA64_ATOMIC_H
3#define _ASM_IA64_ATOMIC_H
4
5/*
6 * Atomic operations that C can't guarantee us. Useful for
7 * resource counting etc..
8 *
9 * NOTE: don't mess with the types below! The "unsigned long" and
10 * "int" types were carefully placed so as to ensure proper operation
11 * of the macros.
12 *
13 * Copyright (C) 1998, 1999, 2002-2003 Hewlett-Packard Co
14 * David Mosberger-Tang <davidm@hpl.hp.com>
15 */
16#include <linux/types.h>
17
18#include <asm/intrinsics.h>
Peter Zijlstra0cd64ef2014-03-13 19:00:36 +010019#include <asm/barrier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Tony Lucka1193652012-07-26 10:55:26 -070022#define ATOMIC_INIT(i) { (i) }
23#define ATOMIC64_INIT(i) { (i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Peter Zijlstra62e8a322015-09-18 11:13:10 +020025#define atomic_read(v) READ_ONCE((v)->counter)
26#define atomic64_read(v) READ_ONCE((v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Peter Zijlstra62e8a322015-09-18 11:13:10 +020028#define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
29#define atomic64_set(v,i) WRITE_ONCE(((v)->counter), (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Peter Zijlstra08be2da2014-03-23 18:20:30 +010031#define ATOMIC_OP(op, c_op) \
32static __inline__ int \
33ia64_atomic_##op (int i, atomic_t *v) \
34{ \
35 __s32 old, new; \
36 CMPXCHG_BUGCHECK_DECL \
37 \
38 do { \
39 CMPXCHG_BUGCHECK(v); \
40 old = atomic_read(v); \
41 new = old c_op i; \
42 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \
43 return new; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Peter Zijlstracc1025072016-04-18 01:16:07 +020046#define ATOMIC_FETCH_OP(op, c_op) \
47static __inline__ int \
48ia64_atomic_fetch_##op (int i, atomic_t *v) \
49{ \
50 __s32 old, new; \
51 CMPXCHG_BUGCHECK_DECL \
52 \
53 do { \
54 CMPXCHG_BUGCHECK(v); \
55 old = atomic_read(v); \
56 new = old c_op i; \
57 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \
58 return old; \
59}
60
61#define ATOMIC_OPS(op, c_op) \
62 ATOMIC_OP(op, c_op) \
63 ATOMIC_FETCH_OP(op, c_op)
64
65ATOMIC_OPS(add, +)
66ATOMIC_OPS(sub, -)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Matthew Wilcox4b664e72018-01-18 13:52:17 -080068#ifdef __OPTIMIZE__
Matthew Wilcox2879b652018-02-19 09:41:26 -080069#define __ia64_atomic_const(i) \
70 static const int __ia64_atomic_p = __builtin_constant_p(i) ? \
Matthew Wilcox4b664e72018-01-18 13:52:17 -080071 ((i) == 1 || (i) == 4 || (i) == 8 || (i) == 16 || \
Matthew Wilcox2879b652018-02-19 09:41:26 -080072 (i) == -1 || (i) == -4 || (i) == -8 || (i) == -16) : 0;\
73 __ia64_atomic_p
Matthew Wilcox4b664e72018-01-18 13:52:17 -080074#else
Matthew Wilcox2879b652018-02-19 09:41:26 -080075#define __ia64_atomic_const(i) 0
Matthew Wilcox4b664e72018-01-18 13:52:17 -080076#endif
Peter Zijlstra08be2da2014-03-23 18:20:30 +010077
Matthew Wilcox2879b652018-02-19 09:41:26 -080078#define atomic_add_return(i,v) \
79({ \
80 int __ia64_aar_i = (i); \
81 __ia64_atomic_const(i) \
82 ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
83 : ia64_atomic_add(__ia64_aar_i, v); \
84})
85
86#define atomic_sub_return(i,v) \
87({ \
88 int __ia64_asr_i = (i); \
89 __ia64_atomic_const(i) \
90 ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
91 : ia64_atomic_sub(__ia64_asr_i, v); \
92})
93
Peter Zijlstracc1025072016-04-18 01:16:07 +020094#define atomic_fetch_add(i,v) \
95({ \
96 int __ia64_aar_i = (i); \
Matthew Wilcox2879b652018-02-19 09:41:26 -080097 __ia64_atomic_const(i) \
Peter Zijlstracc1025072016-04-18 01:16:07 +020098 ? ia64_fetchadd(__ia64_aar_i, &(v)->counter, acq) \
99 : ia64_atomic_fetch_add(__ia64_aar_i, v); \
100})
Peter Zijlstra70ed4732014-04-23 20:00:01 +0200101
Peter Zijlstracc1025072016-04-18 01:16:07 +0200102#define atomic_fetch_sub(i,v) \
103({ \
104 int __ia64_asr_i = (i); \
Matthew Wilcox2879b652018-02-19 09:41:26 -0800105 __ia64_atomic_const(i) \
Peter Zijlstracc1025072016-04-18 01:16:07 +0200106 ? ia64_fetchadd(-__ia64_asr_i, &(v)->counter, acq) \
107 : ia64_atomic_fetch_sub(__ia64_asr_i, v); \
108})
Peter Zijlstra70ed4732014-04-23 20:00:01 +0200109
Peter Zijlstracc1025072016-04-18 01:16:07 +0200110ATOMIC_FETCH_OP(and, &)
111ATOMIC_FETCH_OP(or, |)
112ATOMIC_FETCH_OP(xor, ^)
113
114#define atomic_and(i,v) (void)ia64_atomic_fetch_and(i,v)
115#define atomic_or(i,v) (void)ia64_atomic_fetch_or(i,v)
116#define atomic_xor(i,v) (void)ia64_atomic_fetch_xor(i,v)
117
118#define atomic_fetch_and(i,v) ia64_atomic_fetch_and(i,v)
119#define atomic_fetch_or(i,v) ia64_atomic_fetch_or(i,v)
120#define atomic_fetch_xor(i,v) ia64_atomic_fetch_xor(i,v)
121
122#undef ATOMIC_OPS
123#undef ATOMIC_FETCH_OP
Peter Zijlstra70ed4732014-04-23 20:00:01 +0200124#undef ATOMIC_OP
125
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100126#define ATOMIC64_OP(op, c_op) \
127static __inline__ long \
128ia64_atomic64_##op (__s64 i, atomic64_t *v) \
129{ \
130 __s64 old, new; \
131 CMPXCHG_BUGCHECK_DECL \
132 \
133 do { \
134 CMPXCHG_BUGCHECK(v); \
135 old = atomic64_read(v); \
136 new = old c_op i; \
137 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \
138 return new; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Peter Zijlstracc1025072016-04-18 01:16:07 +0200141#define ATOMIC64_FETCH_OP(op, c_op) \
142static __inline__ long \
143ia64_atomic64_fetch_##op (__s64 i, atomic64_t *v) \
144{ \
145 __s64 old, new; \
146 CMPXCHG_BUGCHECK_DECL \
147 \
148 do { \
149 CMPXCHG_BUGCHECK(v); \
150 old = atomic64_read(v); \
151 new = old c_op i; \
152 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \
153 return old; \
154}
155
156#define ATOMIC64_OPS(op, c_op) \
157 ATOMIC64_OP(op, c_op) \
158 ATOMIC64_FETCH_OP(op, c_op)
159
160ATOMIC64_OPS(add, +)
161ATOMIC64_OPS(sub, -)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100163#define atomic64_add_return(i,v) \
164({ \
165 long __ia64_aar_i = (i); \
Matthew Wilcox2879b652018-02-19 09:41:26 -0800166 __ia64_atomic_const(i) \
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100167 ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
168 : ia64_atomic64_add(__ia64_aar_i, v); \
169})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100171#define atomic64_sub_return(i,v) \
172({ \
173 long __ia64_asr_i = (i); \
Matthew Wilcox2879b652018-02-19 09:41:26 -0800174 __ia64_atomic_const(i) \
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100175 ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
176 : ia64_atomic64_sub(__ia64_asr_i, v); \
177})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Peter Zijlstracc1025072016-04-18 01:16:07 +0200179#define atomic64_fetch_add(i,v) \
180({ \
181 long __ia64_aar_i = (i); \
Matthew Wilcox2879b652018-02-19 09:41:26 -0800182 __ia64_atomic_const(i) \
Peter Zijlstracc1025072016-04-18 01:16:07 +0200183 ? ia64_fetchadd(__ia64_aar_i, &(v)->counter, acq) \
184 : ia64_atomic64_fetch_add(__ia64_aar_i, v); \
185})
Peter Zijlstra70ed4732014-04-23 20:00:01 +0200186
Peter Zijlstracc1025072016-04-18 01:16:07 +0200187#define atomic64_fetch_sub(i,v) \
188({ \
189 long __ia64_asr_i = (i); \
Matthew Wilcox2879b652018-02-19 09:41:26 -0800190 __ia64_atomic_const(i) \
Peter Zijlstracc1025072016-04-18 01:16:07 +0200191 ? ia64_fetchadd(-__ia64_asr_i, &(v)->counter, acq) \
192 : ia64_atomic64_fetch_sub(__ia64_asr_i, v); \
193})
Peter Zijlstra70ed4732014-04-23 20:00:01 +0200194
Peter Zijlstracc1025072016-04-18 01:16:07 +0200195ATOMIC64_FETCH_OP(and, &)
196ATOMIC64_FETCH_OP(or, |)
197ATOMIC64_FETCH_OP(xor, ^)
198
199#define atomic64_and(i,v) (void)ia64_atomic64_fetch_and(i,v)
200#define atomic64_or(i,v) (void)ia64_atomic64_fetch_or(i,v)
201#define atomic64_xor(i,v) (void)ia64_atomic64_fetch_xor(i,v)
202
203#define atomic64_fetch_and(i,v) ia64_atomic64_fetch_and(i,v)
204#define atomic64_fetch_or(i,v) ia64_atomic64_fetch_or(i,v)
205#define atomic64_fetch_xor(i,v) ia64_atomic64_fetch_xor(i,v)
206
207#undef ATOMIC64_OPS
208#undef ATOMIC64_FETCH_OP
Peter Zijlstra70ed4732014-04-23 20:00:01 +0200209#undef ATOMIC64_OP
210
Mathieu Desnoyers81979132007-05-08 00:34:22 -0700211#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
Ingo Molnarffbf6702006-01-09 15:59:17 -0800212#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
Nick Piggin4a6dae62005-11-13 16:07:24 -0800213
Mathieu Desnoyers81979132007-05-08 00:34:22 -0700214#define atomic64_cmpxchg(v, old, new) \
215 (cmpxchg(&((v)->counter), old, new))
216#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
217
Arun Sharmaf24219b2011-07-26 16:09:07 -0700218static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700219{
220 int c, old;
221 c = atomic_read(v);
222 for (;;) {
223 if (unlikely(c == (u)))
224 break;
225 old = atomic_cmpxchg((v), c, c + (a));
226 if (likely(old == c))
227 break;
228 c = old;
229 }
Arun Sharmaf24219b2011-07-26 16:09:07 -0700230 return c;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700231}
232
Nick Piggin8426e1f2005-11-13 16:07:25 -0800233
Tony Luck01d69a82010-08-13 16:41:07 -0700234static __inline__ long atomic64_add_unless(atomic64_t *v, long a, long u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700235{
236 long c, old;
237 c = atomic64_read(v);
238 for (;;) {
239 if (unlikely(c == (u)))
240 break;
241 old = atomic64_cmpxchg((v), c, c + (a));
242 if (likely(old == c))
243 break;
244 c = old;
245 }
246 return c != (u);
247}
248
Mathieu Desnoyers81979132007-05-08 00:34:22 -0700249#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
250
Vineet Gupta445ed0a2016-10-07 17:02:07 -0700251static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
252{
253 long c, old, dec;
254 c = atomic64_read(v);
255 for (;;) {
256 dec = c - 1;
257 if (unlikely(dec < 0))
258 break;
259 old = atomic64_cmpxchg((v), c, dec);
260 if (likely(old == c))
261 break;
262 c = old;
263 }
264 return dec;
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * Atomically add I to V and return TRUE if the resulting value is
269 * negative.
270 */
271static __inline__ int
272atomic_add_negative (int i, atomic_t *v)
273{
274 return atomic_add_return(i, v) < 0;
275}
276
Tony Luck01d69a82010-08-13 16:41:07 -0700277static __inline__ long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278atomic64_add_negative (__s64 i, atomic64_t *v)
279{
280 return atomic64_add_return(i, v) < 0;
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#define atomic_dec_return(v) atomic_sub_return(1, (v))
284#define atomic_inc_return(v) atomic_add_return(1, (v))
285#define atomic64_dec_return(v) atomic64_sub_return(1, (v))
286#define atomic64_inc_return(v) atomic64_add_return(1, (v))
287
288#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
289#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
290#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
291#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
292#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
293#define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
294
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100295#define atomic_add(i,v) (void)atomic_add_return((i), (v))
296#define atomic_sub(i,v) (void)atomic_sub_return((i), (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#define atomic_inc(v) atomic_add(1, (v))
298#define atomic_dec(v) atomic_sub(1, (v))
299
Peter Zijlstra08be2da2014-03-23 18:20:30 +0100300#define atomic64_add(i,v) (void)atomic64_add_return((i), (v))
301#define atomic64_sub(i,v) (void)atomic64_sub_return((i), (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#define atomic64_inc(v) atomic64_add(1, (v))
303#define atomic64_dec(v) atomic64_sub(1, (v))
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif /* _ASM_IA64_ATOMIC_H */