blob: 852e1fee72b00b4fd8746362715073379e9f5138 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/atomic.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1996 Russell King.
5 * Copyright (C) 2002 Deep Blue Solutions Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#ifndef __ASM_ARM_ATOMIC_H
12#define __ASM_ARM_ATOMIC_H
13
Russell King8dc39b82005-11-16 17:23:57 +000014#include <linux/compiler.h>
Will Deaconf38d9992013-07-04 11:43:18 +010015#include <linux/prefetch.h>
Matthew Wilcoxea4354672009-01-06 14:40:39 -080016#include <linux/types.h>
David Howells9f97da72012-03-28 18:30:01 +010017#include <linux/irqflags.h>
18#include <asm/barrier.h>
19#include <asm/cmpxchg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define ATOMIC_INIT(i) { (i) }
22
23#ifdef __KERNEL__
24
Catalin Marinas200b8122009-09-18 23:27:05 +010025/*
26 * On ARM, ordinary assignment (str instruction) doesn't clear the local
27 * strex/ldrex monitor on some implementations. The reason we can use it for
28 * atomic_set() is the clrex or dummy strex done on every exception return.
29 */
Peter Zijlstra62e8a322015-09-18 11:13:10 +020030#define atomic_read(v) READ_ONCE((v)->counter)
31#define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#if __LINUX_ARM_ARCH__ >= 6
34
35/*
36 * ARMv6 UP and SMP safe atomic ops. We use load exclusive and
37 * store exclusive to ensure that these are atomic. We may loop
Catalin Marinas200b8122009-09-18 23:27:05 +010038 * to ensure that the update happens.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
Russell Kingbac4e962009-05-25 20:58:00 +010040
Peter Zijlstraaee9a552014-03-23 16:38:18 +010041#define ATOMIC_OP(op, c_op, asm_op) \
42static inline void atomic_##op(int i, atomic_t *v) \
43{ \
44 unsigned long tmp; \
45 int result; \
46 \
47 prefetchw(&v->counter); \
48 __asm__ __volatile__("@ atomic_" #op "\n" \
49"1: ldrex %0, [%3]\n" \
50" " #asm_op " %0, %0, %4\n" \
51" strex %1, %0, [%3]\n" \
52" teq %1, #0\n" \
53" bne 1b" \
54 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
55 : "r" (&v->counter), "Ir" (i) \
56 : "cc"); \
57} \
Russell Kingbac4e962009-05-25 20:58:00 +010058
Peter Zijlstraaee9a552014-03-23 16:38:18 +010059#define ATOMIC_OP_RETURN(op, c_op, asm_op) \
Will Deacon0ca326d2015-08-06 17:54:44 +010060static inline int atomic_##op##_return_relaxed(int i, atomic_t *v) \
Peter Zijlstraaee9a552014-03-23 16:38:18 +010061{ \
62 unsigned long tmp; \
63 int result; \
64 \
Peter Zijlstraaee9a552014-03-23 16:38:18 +010065 prefetchw(&v->counter); \
66 \
67 __asm__ __volatile__("@ atomic_" #op "_return\n" \
68"1: ldrex %0, [%3]\n" \
69" " #asm_op " %0, %0, %4\n" \
70" strex %1, %0, [%3]\n" \
71" teq %1, #0\n" \
72" bne 1b" \
73 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
74 : "r" (&v->counter), "Ir" (i) \
75 : "cc"); \
76 \
Peter Zijlstraaee9a552014-03-23 16:38:18 +010077 return result; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Peter Zijlstra6da068c2016-04-18 01:10:52 +020080#define ATOMIC_FETCH_OP(op, c_op, asm_op) \
81static inline int atomic_fetch_##op##_relaxed(int i, atomic_t *v) \
82{ \
83 unsigned long tmp; \
84 int result, val; \
85 \
86 prefetchw(&v->counter); \
87 \
88 __asm__ __volatile__("@ atomic_fetch_" #op "\n" \
89"1: ldrex %0, [%4]\n" \
90" " #asm_op " %1, %0, %5\n" \
91" strex %2, %1, [%4]\n" \
92" teq %2, #0\n" \
93" bne 1b" \
94 : "=&r" (result), "=&r" (val), "=&r" (tmp), "+Qo" (v->counter) \
95 : "r" (&v->counter), "Ir" (i) \
96 : "cc"); \
97 \
98 return result; \
99}
100
Will Deacon0ca326d2015-08-06 17:54:44 +0100101#define atomic_add_return_relaxed atomic_add_return_relaxed
102#define atomic_sub_return_relaxed atomic_sub_return_relaxed
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200103#define atomic_fetch_add_relaxed atomic_fetch_add_relaxed
104#define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed
105
106#define atomic_fetch_and_relaxed atomic_fetch_and_relaxed
107#define atomic_fetch_andnot_relaxed atomic_fetch_andnot_relaxed
108#define atomic_fetch_or_relaxed atomic_fetch_or_relaxed
109#define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed
Will Deacon0ca326d2015-08-06 17:54:44 +0100110
111static inline int atomic_cmpxchg_relaxed(atomic_t *ptr, int old, int new)
Nick Piggin4a6dae62005-11-13 16:07:24 -0800112{
Chen Gang4dcc1cf2013-10-26 15:07:25 +0100113 int oldval;
114 unsigned long res;
Nick Piggin4a6dae62005-11-13 16:07:24 -0800115
Will Deaconc32ffce02014-02-21 17:01:48 +0100116 prefetchw(&ptr->counter);
Russell Kingbac4e962009-05-25 20:58:00 +0100117
Nick Piggin4a6dae62005-11-13 16:07:24 -0800118 do {
119 __asm__ __volatile__("@ atomic_cmpxchg\n"
Will Deacon398aa662010-07-08 10:59:16 +0100120 "ldrex %1, [%3]\n"
Nicolas Pitrea7d06832005-11-16 15:05:11 +0000121 "mov %0, #0\n"
Will Deacon398aa662010-07-08 10:59:16 +0100122 "teq %1, %4\n"
123 "strexeq %0, %5, [%3]\n"
124 : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
Nick Piggin4a6dae62005-11-13 16:07:24 -0800125 : "r" (&ptr->counter), "Ir" (old), "r" (new)
126 : "cc");
127 } while (res);
128
129 return oldval;
130}
Will Deacon0ca326d2015-08-06 17:54:44 +0100131#define atomic_cmpxchg_relaxed atomic_cmpxchg_relaxed
Nick Piggin4a6dae62005-11-13 16:07:24 -0800132
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100133static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
Will Deacondb38ee82014-02-21 17:01:48 +0100134{
135 int oldval, newval;
136 unsigned long tmp;
137
138 smp_mb();
139 prefetchw(&v->counter);
140
141 __asm__ __volatile__ ("@ atomic_add_unless\n"
142"1: ldrex %0, [%4]\n"
143" teq %0, %5\n"
144" beq 2f\n"
145" add %1, %0, %6\n"
146" strex %2, %1, [%4]\n"
147" teq %2, #0\n"
148" bne 1b\n"
149"2:"
150 : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter)
151 : "r" (&v->counter), "r" (u), "r" (a)
152 : "cc");
153
154 if (oldval != u)
155 smp_mb();
156
157 return oldval;
158}
Mark Rutlandeccc2da2018-06-21 13:13:09 +0100159#define atomic_fetch_add_unless atomic_fetch_add_unless
Will Deacondb38ee82014-02-21 17:01:48 +0100160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#else /* ARM_ARCH_6 */
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#ifdef CONFIG_SMP
164#error SMP not supported on pre-ARMv6 CPUs
165#endif
166
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100167#define ATOMIC_OP(op, c_op, asm_op) \
168static inline void atomic_##op(int i, atomic_t *v) \
169{ \
170 unsigned long flags; \
171 \
172 raw_local_irq_save(flags); \
173 v->counter c_op i; \
174 raw_local_irq_restore(flags); \
175} \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100177#define ATOMIC_OP_RETURN(op, c_op, asm_op) \
178static inline int atomic_##op##_return(int i, atomic_t *v) \
179{ \
180 unsigned long flags; \
181 int val; \
182 \
183 raw_local_irq_save(flags); \
184 v->counter c_op i; \
185 val = v->counter; \
186 raw_local_irq_restore(flags); \
187 \
188 return val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200191#define ATOMIC_FETCH_OP(op, c_op, asm_op) \
192static inline int atomic_fetch_##op(int i, atomic_t *v) \
193{ \
194 unsigned long flags; \
195 int val; \
196 \
197 raw_local_irq_save(flags); \
198 val = v->counter; \
199 v->counter c_op i; \
200 raw_local_irq_restore(flags); \
201 \
202 return val; \
203}
204
Nick Piggin4a6dae62005-11-13 16:07:24 -0800205static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
206{
207 int ret;
208 unsigned long flags;
209
Lennert Buytenhek8dd5c842006-09-16 10:47:18 +0100210 raw_local_irq_save(flags);
Nick Piggin4a6dae62005-11-13 16:07:24 -0800211 ret = v->counter;
212 if (likely(ret == old))
213 v->counter = new;
Lennert Buytenhek8dd5c842006-09-16 10:47:18 +0100214 raw_local_irq_restore(flags);
Nick Piggin4a6dae62005-11-13 16:07:24 -0800215
216 return ret;
217}
218
Will Deacondb38ee82014-02-21 17:01:48 +0100219#endif /* __LINUX_ARM_ARCH__ */
220
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100221#define ATOMIC_OPS(op, c_op, asm_op) \
222 ATOMIC_OP(op, c_op, asm_op) \
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200223 ATOMIC_OP_RETURN(op, c_op, asm_op) \
224 ATOMIC_FETCH_OP(op, c_op, asm_op)
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100225
226ATOMIC_OPS(add, +=, add)
227ATOMIC_OPS(sub, -=, sub)
228
Peter Zijlstra12589792014-04-23 20:04:39 +0200229#define atomic_andnot atomic_andnot
230
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200231#undef ATOMIC_OPS
232#define ATOMIC_OPS(op, c_op, asm_op) \
233 ATOMIC_OP(op, c_op, asm_op) \
234 ATOMIC_FETCH_OP(op, c_op, asm_op)
235
236ATOMIC_OPS(and, &=, and)
237ATOMIC_OPS(andnot, &= ~, bic)
238ATOMIC_OPS(or, |=, orr)
239ATOMIC_OPS(xor, ^=, eor)
Peter Zijlstra12589792014-04-23 20:04:39 +0200240
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100241#undef ATOMIC_OPS
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200242#undef ATOMIC_FETCH_OP
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100243#undef ATOMIC_OP_RETURN
244#undef ATOMIC_OP
245
Will Deacondb38ee82014-02-21 17:01:48 +0100246#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
247
Russell Kingbac4e962009-05-25 20:58:00 +0100248#define atomic_inc(v) atomic_add(1, v)
249#define atomic_dec(v) atomic_sub(1, v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251#define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
252#define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
Will Deacon6e490b02015-10-07 15:10:38 +0100253#define atomic_inc_return_relaxed(v) (atomic_add_return_relaxed(1, v))
254#define atomic_dec_return_relaxed(v) (atomic_sub_return_relaxed(1, v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
256
257#define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
258
Will Deacon24b44a62010-01-20 19:05:07 +0100259#ifndef CONFIG_GENERIC_ATOMIC64
260typedef struct {
Chen Gang237f1232013-10-26 15:07:04 +0100261 long long counter;
Will Deacon24b44a62010-01-20 19:05:07 +0100262} atomic64_t;
263
264#define ATOMIC64_INIT(i) { (i) }
265
Will Deacon4fd75912013-03-28 11:25:03 +0100266#ifdef CONFIG_ARM_LPAE
Chen Gang237f1232013-10-26 15:07:04 +0100267static inline long long atomic64_read(const atomic64_t *v)
Will Deacon4fd75912013-03-28 11:25:03 +0100268{
Chen Gang237f1232013-10-26 15:07:04 +0100269 long long result;
Will Deacon4fd75912013-03-28 11:25:03 +0100270
271 __asm__ __volatile__("@ atomic64_read\n"
272" ldrd %0, %H0, [%1]"
273 : "=&r" (result)
274 : "r" (&v->counter), "Qo" (v->counter)
275 );
276
277 return result;
278}
279
Chen Gang237f1232013-10-26 15:07:04 +0100280static inline void atomic64_set(atomic64_t *v, long long i)
Will Deacon4fd75912013-03-28 11:25:03 +0100281{
282 __asm__ __volatile__("@ atomic64_set\n"
283" strd %2, %H2, [%1]"
284 : "=Qo" (v->counter)
285 : "r" (&v->counter), "r" (i)
286 );
287}
288#else
Chen Gang237f1232013-10-26 15:07:04 +0100289static inline long long atomic64_read(const atomic64_t *v)
Will Deacon24b44a62010-01-20 19:05:07 +0100290{
Chen Gang237f1232013-10-26 15:07:04 +0100291 long long result;
Will Deacon24b44a62010-01-20 19:05:07 +0100292
293 __asm__ __volatile__("@ atomic64_read\n"
294" ldrexd %0, %H0, [%1]"
295 : "=&r" (result)
Will Deacon398aa662010-07-08 10:59:16 +0100296 : "r" (&v->counter), "Qo" (v->counter)
Will Deacon24b44a62010-01-20 19:05:07 +0100297 );
298
299 return result;
300}
301
Chen Gang237f1232013-10-26 15:07:04 +0100302static inline void atomic64_set(atomic64_t *v, long long i)
Will Deacon24b44a62010-01-20 19:05:07 +0100303{
Chen Gang237f1232013-10-26 15:07:04 +0100304 long long tmp;
Will Deacon24b44a62010-01-20 19:05:07 +0100305
Will Deaconf38d9992013-07-04 11:43:18 +0100306 prefetchw(&v->counter);
Will Deacon24b44a62010-01-20 19:05:07 +0100307 __asm__ __volatile__("@ atomic64_set\n"
Will Deacon398aa662010-07-08 10:59:16 +0100308"1: ldrexd %0, %H0, [%2]\n"
309" strexd %0, %3, %H3, [%2]\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100310" teq %0, #0\n"
311" bne 1b"
Will Deacon398aa662010-07-08 10:59:16 +0100312 : "=&r" (tmp), "=Qo" (v->counter)
Will Deacon24b44a62010-01-20 19:05:07 +0100313 : "r" (&v->counter), "r" (i)
314 : "cc");
315}
Will Deacon4fd75912013-03-28 11:25:03 +0100316#endif
Will Deacon24b44a62010-01-20 19:05:07 +0100317
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100318#define ATOMIC64_OP(op, op1, op2) \
319static inline void atomic64_##op(long long i, atomic64_t *v) \
320{ \
321 long long result; \
322 unsigned long tmp; \
323 \
324 prefetchw(&v->counter); \
325 __asm__ __volatile__("@ atomic64_" #op "\n" \
326"1: ldrexd %0, %H0, [%3]\n" \
327" " #op1 " %Q0, %Q0, %Q4\n" \
328" " #op2 " %R0, %R0, %R4\n" \
329" strexd %1, %0, %H0, [%3]\n" \
330" teq %1, #0\n" \
331" bne 1b" \
332 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
333 : "r" (&v->counter), "r" (i) \
334 : "cc"); \
335} \
Will Deacon24b44a62010-01-20 19:05:07 +0100336
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100337#define ATOMIC64_OP_RETURN(op, op1, op2) \
Will Deacon0ca326d2015-08-06 17:54:44 +0100338static inline long long \
339atomic64_##op##_return_relaxed(long long i, atomic64_t *v) \
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100340{ \
341 long long result; \
342 unsigned long tmp; \
343 \
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100344 prefetchw(&v->counter); \
345 \
346 __asm__ __volatile__("@ atomic64_" #op "_return\n" \
347"1: ldrexd %0, %H0, [%3]\n" \
348" " #op1 " %Q0, %Q0, %Q4\n" \
349" " #op2 " %R0, %R0, %R4\n" \
350" strexd %1, %0, %H0, [%3]\n" \
351" teq %1, #0\n" \
352" bne 1b" \
353 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
354 : "r" (&v->counter), "r" (i) \
355 : "cc"); \
356 \
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100357 return result; \
Will Deacon24b44a62010-01-20 19:05:07 +0100358}
359
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200360#define ATOMIC64_FETCH_OP(op, op1, op2) \
361static inline long long \
362atomic64_fetch_##op##_relaxed(long long i, atomic64_t *v) \
363{ \
364 long long result, val; \
365 unsigned long tmp; \
366 \
367 prefetchw(&v->counter); \
368 \
369 __asm__ __volatile__("@ atomic64_fetch_" #op "\n" \
370"1: ldrexd %0, %H0, [%4]\n" \
371" " #op1 " %Q1, %Q0, %Q5\n" \
372" " #op2 " %R1, %R0, %R5\n" \
373" strexd %2, %1, %H1, [%4]\n" \
374" teq %2, #0\n" \
375" bne 1b" \
376 : "=&r" (result), "=&r" (val), "=&r" (tmp), "+Qo" (v->counter) \
377 : "r" (&v->counter), "r" (i) \
378 : "cc"); \
379 \
380 return result; \
381}
382
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100383#define ATOMIC64_OPS(op, op1, op2) \
384 ATOMIC64_OP(op, op1, op2) \
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200385 ATOMIC64_OP_RETURN(op, op1, op2) \
386 ATOMIC64_FETCH_OP(op, op1, op2)
Will Deacon24b44a62010-01-20 19:05:07 +0100387
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100388ATOMIC64_OPS(add, adds, adc)
389ATOMIC64_OPS(sub, subs, sbc)
Will Deacon24b44a62010-01-20 19:05:07 +0100390
Will Deacon0ca326d2015-08-06 17:54:44 +0100391#define atomic64_add_return_relaxed atomic64_add_return_relaxed
392#define atomic64_sub_return_relaxed atomic64_sub_return_relaxed
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200393#define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed
394#define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed
395
396#undef ATOMIC64_OPS
397#define ATOMIC64_OPS(op, op1, op2) \
398 ATOMIC64_OP(op, op1, op2) \
399 ATOMIC64_FETCH_OP(op, op1, op2)
Will Deacon0ca326d2015-08-06 17:54:44 +0100400
Peter Zijlstra12589792014-04-23 20:04:39 +0200401#define atomic64_andnot atomic64_andnot
402
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200403ATOMIC64_OPS(and, and, and)
404ATOMIC64_OPS(andnot, bic, bic)
405ATOMIC64_OPS(or, orr, orr)
406ATOMIC64_OPS(xor, eor, eor)
407
408#define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed
409#define atomic64_fetch_andnot_relaxed atomic64_fetch_andnot_relaxed
410#define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed
411#define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed
Peter Zijlstra12589792014-04-23 20:04:39 +0200412
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100413#undef ATOMIC64_OPS
Peter Zijlstra6da068c2016-04-18 01:10:52 +0200414#undef ATOMIC64_FETCH_OP
Peter Zijlstraaee9a552014-03-23 16:38:18 +0100415#undef ATOMIC64_OP_RETURN
416#undef ATOMIC64_OP
Will Deacon24b44a62010-01-20 19:05:07 +0100417
Will Deacon0ca326d2015-08-06 17:54:44 +0100418static inline long long
419atomic64_cmpxchg_relaxed(atomic64_t *ptr, long long old, long long new)
Will Deacon24b44a62010-01-20 19:05:07 +0100420{
Chen Gang237f1232013-10-26 15:07:04 +0100421 long long oldval;
Will Deacon24b44a62010-01-20 19:05:07 +0100422 unsigned long res;
423
Will Deaconc32ffce02014-02-21 17:01:48 +0100424 prefetchw(&ptr->counter);
Will Deacon24b44a62010-01-20 19:05:07 +0100425
426 do {
427 __asm__ __volatile__("@ atomic64_cmpxchg\n"
Will Deacon398aa662010-07-08 10:59:16 +0100428 "ldrexd %1, %H1, [%3]\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100429 "mov %0, #0\n"
Will Deacon398aa662010-07-08 10:59:16 +0100430 "teq %1, %4\n"
431 "teqeq %H1, %H4\n"
432 "strexdeq %0, %5, %H5, [%3]"
433 : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
Will Deacon24b44a62010-01-20 19:05:07 +0100434 : "r" (&ptr->counter), "r" (old), "r" (new)
435 : "cc");
436 } while (res);
437
Will Deacon24b44a62010-01-20 19:05:07 +0100438 return oldval;
439}
Will Deacon0ca326d2015-08-06 17:54:44 +0100440#define atomic64_cmpxchg_relaxed atomic64_cmpxchg_relaxed
Will Deacon24b44a62010-01-20 19:05:07 +0100441
Will Deacon0ca326d2015-08-06 17:54:44 +0100442static inline long long atomic64_xchg_relaxed(atomic64_t *ptr, long long new)
Will Deacon24b44a62010-01-20 19:05:07 +0100443{
Chen Gang237f1232013-10-26 15:07:04 +0100444 long long result;
Will Deacon24b44a62010-01-20 19:05:07 +0100445 unsigned long tmp;
446
Will Deaconc32ffce02014-02-21 17:01:48 +0100447 prefetchw(&ptr->counter);
Will Deacon24b44a62010-01-20 19:05:07 +0100448
449 __asm__ __volatile__("@ atomic64_xchg\n"
Will Deacon398aa662010-07-08 10:59:16 +0100450"1: ldrexd %0, %H0, [%3]\n"
451" strexd %1, %4, %H4, [%3]\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100452" teq %1, #0\n"
453" bne 1b"
Will Deacon398aa662010-07-08 10:59:16 +0100454 : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
Will Deacon24b44a62010-01-20 19:05:07 +0100455 : "r" (&ptr->counter), "r" (new)
456 : "cc");
457
Will Deacon24b44a62010-01-20 19:05:07 +0100458 return result;
459}
Will Deacon0ca326d2015-08-06 17:54:44 +0100460#define atomic64_xchg_relaxed atomic64_xchg_relaxed
Will Deacon24b44a62010-01-20 19:05:07 +0100461
Chen Gang237f1232013-10-26 15:07:04 +0100462static inline long long atomic64_dec_if_positive(atomic64_t *v)
Will Deacon24b44a62010-01-20 19:05:07 +0100463{
Chen Gang237f1232013-10-26 15:07:04 +0100464 long long result;
Will Deacon24b44a62010-01-20 19:05:07 +0100465 unsigned long tmp;
466
467 smp_mb();
Will Deaconc32ffce02014-02-21 17:01:48 +0100468 prefetchw(&v->counter);
Will Deacon24b44a62010-01-20 19:05:07 +0100469
470 __asm__ __volatile__("@ atomic64_dec_if_positive\n"
Will Deacon398aa662010-07-08 10:59:16 +0100471"1: ldrexd %0, %H0, [%3]\n"
Victor Kamensky2245f922013-07-26 09:28:53 -0700472" subs %Q0, %Q0, #1\n"
473" sbc %R0, %R0, #0\n"
474" teq %R0, #0\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100475" bmi 2f\n"
Will Deacon398aa662010-07-08 10:59:16 +0100476" strexd %1, %0, %H0, [%3]\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100477" teq %1, #0\n"
478" bne 1b\n"
479"2:"
Will Deacon398aa662010-07-08 10:59:16 +0100480 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
Will Deacon24b44a62010-01-20 19:05:07 +0100481 : "r" (&v->counter)
482 : "cc");
483
484 smp_mb();
485
486 return result;
487}
488
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100489static inline long long atomic64_fetch_add_unless(atomic64_t *v, long long a,
490 long long u)
Will Deacon24b44a62010-01-20 19:05:07 +0100491{
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100492 long long oldval, newval;
Will Deacon24b44a62010-01-20 19:05:07 +0100493 unsigned long tmp;
Will Deacon24b44a62010-01-20 19:05:07 +0100494
495 smp_mb();
Will Deaconc32ffce02014-02-21 17:01:48 +0100496 prefetchw(&v->counter);
Will Deacon24b44a62010-01-20 19:05:07 +0100497
498 __asm__ __volatile__("@ atomic64_add_unless\n"
Will Deacon398aa662010-07-08 10:59:16 +0100499"1: ldrexd %0, %H0, [%4]\n"
500" teq %0, %5\n"
501" teqeq %H0, %H5\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100502" beq 2f\n"
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100503" adds %Q1, %Q0, %Q6\n"
504" adc %R1, %R0, %R6\n"
505" strexd %2, %1, %H1, [%4]\n"
Will Deacon24b44a62010-01-20 19:05:07 +0100506" teq %2, #0\n"
507" bne 1b\n"
508"2:"
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100509 : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter)
Will Deacon24b44a62010-01-20 19:05:07 +0100510 : "r" (&v->counter), "r" (u), "r" (a)
511 : "cc");
512
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100513 if (oldval != u)
Will Deacon24b44a62010-01-20 19:05:07 +0100514 smp_mb();
515
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100516 return oldval;
Will Deacon24b44a62010-01-20 19:05:07 +0100517}
Mark Rutlandfee8ca92018-06-21 13:13:14 +0100518#define atomic64_fetch_add_unless atomic64_fetch_add_unless
Will Deacon24b44a62010-01-20 19:05:07 +0100519
520#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
521#define atomic64_inc(v) atomic64_add(1LL, (v))
Will Deacon6e490b02015-10-07 15:10:38 +0100522#define atomic64_inc_return_relaxed(v) atomic64_add_return_relaxed(1LL, (v))
Will Deacon24b44a62010-01-20 19:05:07 +0100523#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
524#define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
525#define atomic64_dec(v) atomic64_sub(1LL, (v))
Will Deacon6e490b02015-10-07 15:10:38 +0100526#define atomic64_dec_return_relaxed(v) atomic64_sub_return_relaxed(1LL, (v))
Will Deacon24b44a62010-01-20 19:05:07 +0100527#define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
Will Deacon24b44a62010-01-20 19:05:07 +0100528
Arun Sharma78477772011-07-26 16:09:08 -0700529#endif /* !CONFIG_GENERIC_ATOMIC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#endif
531#endif