1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm/include/asm/atomic.h
5 * Copyright (C) 1996 Russell King.
6 * Copyright (C) 2002 Deep Blue Solutions Ltd.
8 #ifndef __ASM_ARM_ATOMIC_H
9 #define __ASM_ARM_ATOMIC_H
11 #include <linux/compiler.h>
12 #include <linux/prefetch.h>
13 #include <linux/types.h>
14 #include <linux/irqflags.h>
15 #include <asm/barrier.h>
16 #include <asm/cmpxchg.h>
21 * On ARM, ordinary assignment (str instruction) doesn't clear the local
22 * strex/ldrex monitor on some implementations. The reason we can use it for
23 * atomic_set() is the clrex or dummy strex done on every exception return.
25 #define arch_atomic_read(v) READ_ONCE((v)->counter)
26 #define arch_atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
28 #if __LINUX_ARM_ARCH__ >= 6
31 * ARMv6 UP and SMP safe atomic ops. We use load exclusive and
32 * store exclusive to ensure that these are atomic. We may loop
33 * to ensure that the update happens.
36 #define ATOMIC_OP(op, c_op, asm_op) \
37 static inline void arch_atomic_##op(int i, atomic_t *v) \
42 prefetchw(&v->counter); \
43 __asm__ __volatile__("@ atomic_" #op "\n" \
44 "1: ldrex %0, [%3]\n" \
45 " " #asm_op " %0, %0, %4\n" \
46 " strex %1, %0, [%3]\n" \
49 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
50 : "r" (&v->counter), "Ir" (i) \
54 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
55 static inline int arch_atomic_##op##_return_relaxed(int i, atomic_t *v) \
60 prefetchw(&v->counter); \
62 __asm__ __volatile__("@ atomic_" #op "_return\n" \
63 "1: ldrex %0, [%3]\n" \
64 " " #asm_op " %0, %0, %4\n" \
65 " strex %1, %0, [%3]\n" \
68 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
69 : "r" (&v->counter), "Ir" (i) \
75 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
76 static inline int arch_atomic_fetch_##op##_relaxed(int i, atomic_t *v) \
81 prefetchw(&v->counter); \
83 __asm__ __volatile__("@ atomic_fetch_" #op "\n" \
84 "1: ldrex %0, [%4]\n" \
85 " " #asm_op " %1, %0, %5\n" \
86 " strex %2, %1, [%4]\n" \
89 : "=&r" (result), "=&r" (val), "=&r" (tmp), "+Qo" (v->counter) \
90 : "r" (&v->counter), "Ir" (i) \
96 #define arch_atomic_add_return_relaxed arch_atomic_add_return_relaxed
97 #define arch_atomic_sub_return_relaxed arch_atomic_sub_return_relaxed
98 #define arch_atomic_fetch_add_relaxed arch_atomic_fetch_add_relaxed
99 #define arch_atomic_fetch_sub_relaxed arch_atomic_fetch_sub_relaxed
101 #define arch_atomic_fetch_and_relaxed arch_atomic_fetch_and_relaxed
102 #define arch_atomic_fetch_andnot_relaxed arch_atomic_fetch_andnot_relaxed
103 #define arch_atomic_fetch_or_relaxed arch_atomic_fetch_or_relaxed
104 #define arch_atomic_fetch_xor_relaxed arch_atomic_fetch_xor_relaxed
106 static inline int arch_atomic_cmpxchg_relaxed(atomic_t *ptr, int old, int new)
111 prefetchw(&ptr->counter);
114 __asm__ __volatile__("@ atomic_cmpxchg\n"
118 "strexeq %0, %5, [%3]\n"
119 : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
120 : "r" (&ptr->counter), "Ir" (old), "r" (new)
126 #define arch_atomic_cmpxchg_relaxed arch_atomic_cmpxchg_relaxed
128 static inline int arch_atomic_fetch_add_unless(atomic_t *v, int a, int u)
134 prefetchw(&v->counter);
136 __asm__ __volatile__ ("@ atomic_add_unless\n"
137 "1: ldrex %0, [%4]\n"
141 " strex %2, %1, [%4]\n"
145 : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter)
146 : "r" (&v->counter), "r" (u), "r" (a)
154 #define arch_atomic_fetch_add_unless arch_atomic_fetch_add_unless
156 #else /* ARM_ARCH_6 */
159 #error SMP not supported on pre-ARMv6 CPUs
162 #define ATOMIC_OP(op, c_op, asm_op) \
163 static inline void arch_atomic_##op(int i, atomic_t *v) \
165 unsigned long flags; \
167 raw_local_irq_save(flags); \
169 raw_local_irq_restore(flags); \
172 #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
173 static inline int arch_atomic_##op##_return(int i, atomic_t *v) \
175 unsigned long flags; \
178 raw_local_irq_save(flags); \
181 raw_local_irq_restore(flags); \
186 #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
187 static inline int arch_atomic_fetch_##op(int i, atomic_t *v) \
189 unsigned long flags; \
192 raw_local_irq_save(flags); \
195 raw_local_irq_restore(flags); \
200 #define arch_atomic_add_return arch_atomic_add_return
201 #define arch_atomic_sub_return arch_atomic_sub_return
202 #define arch_atomic_fetch_add arch_atomic_fetch_add
203 #define arch_atomic_fetch_sub arch_atomic_fetch_sub
205 #define arch_atomic_fetch_and arch_atomic_fetch_and
206 #define arch_atomic_fetch_andnot arch_atomic_fetch_andnot
207 #define arch_atomic_fetch_or arch_atomic_fetch_or
208 #define arch_atomic_fetch_xor arch_atomic_fetch_xor
210 static inline int arch_atomic_cmpxchg(atomic_t *v, int old, int new)
215 raw_local_irq_save(flags);
217 if (likely(ret == old))
219 raw_local_irq_restore(flags);
223 #define arch_atomic_cmpxchg arch_atomic_cmpxchg
225 #endif /* __LINUX_ARM_ARCH__ */
227 #define ATOMIC_OPS(op, c_op, asm_op) \
228 ATOMIC_OP(op, c_op, asm_op) \
229 ATOMIC_OP_RETURN(op, c_op, asm_op) \
230 ATOMIC_FETCH_OP(op, c_op, asm_op)
232 ATOMIC_OPS(add, +=, add)
233 ATOMIC_OPS(sub, -=, sub)
235 #define arch_atomic_andnot arch_atomic_andnot
238 #define ATOMIC_OPS(op, c_op, asm_op) \
239 ATOMIC_OP(op, c_op, asm_op) \
240 ATOMIC_FETCH_OP(op, c_op, asm_op)
242 ATOMIC_OPS(and, &=, and)
243 ATOMIC_OPS(andnot, &= ~, bic)
244 ATOMIC_OPS(or, |=, orr)
245 ATOMIC_OPS(xor, ^=, eor)
248 #undef ATOMIC_FETCH_OP
249 #undef ATOMIC_OP_RETURN
252 #ifndef CONFIG_GENERIC_ATOMIC64
257 #define ATOMIC64_INIT(i) { (i) }
259 #ifdef CONFIG_ARM_LPAE
260 static inline s64 arch_atomic64_read(const atomic64_t *v)
264 __asm__ __volatile__("@ atomic64_read\n"
265 " ldrd %0, %H0, [%1]"
267 : "r" (&v->counter), "Qo" (v->counter)
273 static inline void arch_atomic64_set(atomic64_t *v, s64 i)
275 __asm__ __volatile__("@ atomic64_set\n"
276 " strd %2, %H2, [%1]"
278 : "r" (&v->counter), "r" (i)
282 static inline s64 arch_atomic64_read(const atomic64_t *v)
286 __asm__ __volatile__("@ atomic64_read\n"
287 " ldrexd %0, %H0, [%1]"
289 : "r" (&v->counter), "Qo" (v->counter)
295 static inline void arch_atomic64_set(atomic64_t *v, s64 i)
299 prefetchw(&v->counter);
300 __asm__ __volatile__("@ atomic64_set\n"
301 "1: ldrexd %0, %H0, [%2]\n"
302 " strexd %0, %3, %H3, [%2]\n"
305 : "=&r" (tmp), "=Qo" (v->counter)
306 : "r" (&v->counter), "r" (i)
311 #define ATOMIC64_OP(op, op1, op2) \
312 static inline void arch_atomic64_##op(s64 i, atomic64_t *v) \
317 prefetchw(&v->counter); \
318 __asm__ __volatile__("@ atomic64_" #op "\n" \
319 "1: ldrexd %0, %H0, [%3]\n" \
320 " " #op1 " %Q0, %Q0, %Q4\n" \
321 " " #op2 " %R0, %R0, %R4\n" \
322 " strexd %1, %0, %H0, [%3]\n" \
325 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
326 : "r" (&v->counter), "r" (i) \
330 #define ATOMIC64_OP_RETURN(op, op1, op2) \
332 arch_atomic64_##op##_return_relaxed(s64 i, atomic64_t *v) \
337 prefetchw(&v->counter); \
339 __asm__ __volatile__("@ atomic64_" #op "_return\n" \
340 "1: ldrexd %0, %H0, [%3]\n" \
341 " " #op1 " %Q0, %Q0, %Q4\n" \
342 " " #op2 " %R0, %R0, %R4\n" \
343 " strexd %1, %0, %H0, [%3]\n" \
346 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
347 : "r" (&v->counter), "r" (i) \
353 #define ATOMIC64_FETCH_OP(op, op1, op2) \
355 arch_atomic64_fetch_##op##_relaxed(s64 i, atomic64_t *v) \
360 prefetchw(&v->counter); \
362 __asm__ __volatile__("@ atomic64_fetch_" #op "\n" \
363 "1: ldrexd %0, %H0, [%4]\n" \
364 " " #op1 " %Q1, %Q0, %Q5\n" \
365 " " #op2 " %R1, %R0, %R5\n" \
366 " strexd %2, %1, %H1, [%4]\n" \
369 : "=&r" (result), "=&r" (val), "=&r" (tmp), "+Qo" (v->counter) \
370 : "r" (&v->counter), "r" (i) \
376 #define ATOMIC64_OPS(op, op1, op2) \
377 ATOMIC64_OP(op, op1, op2) \
378 ATOMIC64_OP_RETURN(op, op1, op2) \
379 ATOMIC64_FETCH_OP(op, op1, op2)
381 ATOMIC64_OPS(add, adds, adc)
382 ATOMIC64_OPS(sub, subs, sbc)
384 #define arch_atomic64_add_return_relaxed arch_atomic64_add_return_relaxed
385 #define arch_atomic64_sub_return_relaxed arch_atomic64_sub_return_relaxed
386 #define arch_atomic64_fetch_add_relaxed arch_atomic64_fetch_add_relaxed
387 #define arch_atomic64_fetch_sub_relaxed arch_atomic64_fetch_sub_relaxed
390 #define ATOMIC64_OPS(op, op1, op2) \
391 ATOMIC64_OP(op, op1, op2) \
392 ATOMIC64_FETCH_OP(op, op1, op2)
394 #define arch_atomic64_andnot arch_atomic64_andnot
396 ATOMIC64_OPS(and, and, and)
397 ATOMIC64_OPS(andnot, bic, bic)
398 ATOMIC64_OPS(or, orr, orr)
399 ATOMIC64_OPS(xor, eor, eor)
401 #define arch_atomic64_fetch_and_relaxed arch_atomic64_fetch_and_relaxed
402 #define arch_atomic64_fetch_andnot_relaxed arch_atomic64_fetch_andnot_relaxed
403 #define arch_atomic64_fetch_or_relaxed arch_atomic64_fetch_or_relaxed
404 #define arch_atomic64_fetch_xor_relaxed arch_atomic64_fetch_xor_relaxed
407 #undef ATOMIC64_FETCH_OP
408 #undef ATOMIC64_OP_RETURN
411 static inline s64 arch_atomic64_cmpxchg_relaxed(atomic64_t *ptr, s64 old, s64 new)
416 prefetchw(&ptr->counter);
419 __asm__ __volatile__("@ atomic64_cmpxchg\n"
420 "ldrexd %1, %H1, [%3]\n"
424 "strexdeq %0, %5, %H5, [%3]"
425 : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
426 : "r" (&ptr->counter), "r" (old), "r" (new)
432 #define arch_atomic64_cmpxchg_relaxed arch_atomic64_cmpxchg_relaxed
434 static inline s64 arch_atomic64_xchg_relaxed(atomic64_t *ptr, s64 new)
439 prefetchw(&ptr->counter);
441 __asm__ __volatile__("@ atomic64_xchg\n"
442 "1: ldrexd %0, %H0, [%3]\n"
443 " strexd %1, %4, %H4, [%3]\n"
446 : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
447 : "r" (&ptr->counter), "r" (new)
452 #define arch_atomic64_xchg_relaxed arch_atomic64_xchg_relaxed
454 static inline s64 arch_atomic64_dec_if_positive(atomic64_t *v)
460 prefetchw(&v->counter);
462 __asm__ __volatile__("@ atomic64_dec_if_positive\n"
463 "1: ldrexd %0, %H0, [%3]\n"
464 " subs %Q0, %Q0, #1\n"
465 " sbc %R0, %R0, #0\n"
468 " strexd %1, %0, %H0, [%3]\n"
472 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
480 #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
482 static inline s64 arch_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
488 prefetchw(&v->counter);
490 __asm__ __volatile__("@ atomic64_add_unless\n"
491 "1: ldrexd %0, %H0, [%4]\n"
495 " adds %Q1, %Q0, %Q6\n"
496 " adc %R1, %R0, %R6\n"
497 " strexd %2, %1, %H1, [%4]\n"
501 : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter)
502 : "r" (&v->counter), "r" (u), "r" (a)
510 #define arch_atomic64_fetch_add_unless arch_atomic64_fetch_add_unless
512 #endif /* !CONFIG_GENERIC_ATOMIC64 */