1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_ATOMIC_H_
3 #define _ASM_POWERPC_ATOMIC_H_
6 * PowerPC atomic operations
10 #include <linux/types.h>
11 #include <asm/cmpxchg.h>
12 #include <asm/barrier.h>
15 * Since *_return_relaxed and {cmp}xchg_relaxed are implemented with
16 * a "bne-" instruction at the end, so an isync is enough as a acquire barrier
17 * on the platform without lwsync.
19 #define __atomic_acquire_fence() \
20 __asm__ __volatile__(PPC_ACQUIRE_BARRIER "" : : : "memory")
22 #define __atomic_release_fence() \
23 __asm__ __volatile__(PPC_RELEASE_BARRIER "" : : : "memory")
25 static __inline__ int atomic_read(const atomic_t *v)
29 __asm__ __volatile__("lwz%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
34 static __inline__ void atomic_set(atomic_t *v, int i)
36 __asm__ __volatile__("stw%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
39 #define ATOMIC_OP(op, asm_op) \
40 static __inline__ void atomic_##op(int a, atomic_t *v) \
44 __asm__ __volatile__( \
45 "1: lwarx %0,0,%3 # atomic_" #op "\n" \
46 #asm_op " %0,%2,%0\n" \
47 " stwcx. %0,0,%3 \n" \
49 : "=&r" (t), "+m" (v->counter) \
50 : "r" (a), "r" (&v->counter) \
54 #define ATOMIC_OP_RETURN_RELAXED(op, asm_op) \
55 static inline int atomic_##op##_return_relaxed(int a, atomic_t *v) \
59 __asm__ __volatile__( \
60 "1: lwarx %0,0,%3 # atomic_" #op "_return_relaxed\n" \
61 #asm_op " %0,%2,%0\n" \
64 : "=&r" (t), "+m" (v->counter) \
65 : "r" (a), "r" (&v->counter) \
71 #define ATOMIC_FETCH_OP_RELAXED(op, asm_op) \
72 static inline int atomic_fetch_##op##_relaxed(int a, atomic_t *v) \
76 __asm__ __volatile__( \
77 "1: lwarx %0,0,%4 # atomic_fetch_" #op "_relaxed\n" \
78 #asm_op " %1,%3,%0\n" \
81 : "=&r" (res), "=&r" (t), "+m" (v->counter) \
82 : "r" (a), "r" (&v->counter) \
88 #define ATOMIC_OPS(op, asm_op) \
89 ATOMIC_OP(op, asm_op) \
90 ATOMIC_OP_RETURN_RELAXED(op, asm_op) \
91 ATOMIC_FETCH_OP_RELAXED(op, asm_op)
96 #define atomic_add_return_relaxed atomic_add_return_relaxed
97 #define atomic_sub_return_relaxed atomic_sub_return_relaxed
99 #define atomic_fetch_add_relaxed atomic_fetch_add_relaxed
100 #define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed
103 #define ATOMIC_OPS(op, asm_op) \
104 ATOMIC_OP(op, asm_op) \
105 ATOMIC_FETCH_OP_RELAXED(op, asm_op)
111 #define atomic_fetch_and_relaxed atomic_fetch_and_relaxed
112 #define atomic_fetch_or_relaxed atomic_fetch_or_relaxed
113 #define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed
116 #undef ATOMIC_FETCH_OP_RELAXED
117 #undef ATOMIC_OP_RETURN_RELAXED
120 static __inline__ void atomic_inc(atomic_t *v)
124 __asm__ __volatile__(
125 "1: lwarx %0,0,%2 # atomic_inc\n\
129 : "=&r" (t), "+m" (v->counter)
133 #define atomic_inc atomic_inc
135 static __inline__ int atomic_inc_return_relaxed(atomic_t *v)
139 __asm__ __volatile__(
140 "1: lwarx %0,0,%2 # atomic_inc_return_relaxed\n"
144 : "=&r" (t), "+m" (v->counter)
151 static __inline__ void atomic_dec(atomic_t *v)
155 __asm__ __volatile__(
156 "1: lwarx %0,0,%2 # atomic_dec\n\
160 : "=&r" (t), "+m" (v->counter)
164 #define atomic_dec atomic_dec
166 static __inline__ int atomic_dec_return_relaxed(atomic_t *v)
170 __asm__ __volatile__(
171 "1: lwarx %0,0,%2 # atomic_dec_return_relaxed\n"
175 : "=&r" (t), "+m" (v->counter)
182 #define atomic_inc_return_relaxed atomic_inc_return_relaxed
183 #define atomic_dec_return_relaxed atomic_dec_return_relaxed
185 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
186 #define atomic_cmpxchg_relaxed(v, o, n) \
187 cmpxchg_relaxed(&((v)->counter), (o), (n))
188 #define atomic_cmpxchg_acquire(v, o, n) \
189 cmpxchg_acquire(&((v)->counter), (o), (n))
191 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
192 #define atomic_xchg_relaxed(v, new) xchg_relaxed(&((v)->counter), (new))
195 * Don't want to override the generic atomic_try_cmpxchg_acquire, because
196 * we add a lock hint to the lwarx, which may not be wanted for the
197 * _acquire case (and is not used by the other _acquire variants so it
198 * would be a surprise).
200 static __always_inline bool
201 atomic_try_cmpxchg_lock(atomic_t *v, int *old, int new)
205 __asm__ __volatile__ (
206 "1:\t" PPC_LWARX(%0,0,%2,1) " # atomic_try_cmpxchg_acquire \n"
211 "\t" PPC_ACQUIRE_BARRIER " \n"
213 : "=&r" (r), "+m" (v->counter)
214 : "r" (&v->counter), "r" (o), "r" (new)
217 if (unlikely(r != o))
219 return likely(r == o);
223 * atomic_fetch_add_unless - add unless the number is a given value
224 * @v: pointer of type atomic_t
225 * @a: the amount to add to v...
226 * @u: ...unless v is equal to u.
228 * Atomically adds @a to @v, so long as it was not @u.
229 * Returns the old value of @v.
231 static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
235 __asm__ __volatile__ (
236 PPC_ATOMIC_ENTRY_BARRIER
237 "1: lwarx %0,0,%1 # atomic_fetch_add_unless\n\
243 PPC_ATOMIC_EXIT_BARRIER
247 : "r" (&v->counter), "r" (a), "r" (u)
252 #define atomic_fetch_add_unless atomic_fetch_add_unless
255 * atomic_inc_not_zero - increment unless the number is zero
256 * @v: pointer of type atomic_t
258 * Atomically increments @v by 1, so long as @v is non-zero.
259 * Returns non-zero if @v was non-zero, and zero otherwise.
261 static __inline__ int atomic_inc_not_zero(atomic_t *v)
265 __asm__ __volatile__ (
266 PPC_ATOMIC_ENTRY_BARRIER
267 "1: lwarx %0,0,%2 # atomic_inc_not_zero\n\
273 PPC_ATOMIC_EXIT_BARRIER
276 : "=&r" (t1), "=&r" (t2)
278 : "cc", "xer", "memory");
282 #define atomic_inc_not_zero(v) atomic_inc_not_zero((v))
285 * Atomically test *v and decrement if it is greater than 0.
286 * The function returns the old value of *v minus 1, even if
287 * the atomic variable, v, was not decremented.
289 static __inline__ int atomic_dec_if_positive(atomic_t *v)
293 __asm__ __volatile__(
294 PPC_ATOMIC_ENTRY_BARRIER
295 "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\
301 PPC_ATOMIC_EXIT_BARRIER
309 #define atomic_dec_if_positive atomic_dec_if_positive
313 #define ATOMIC64_INIT(i) { (i) }
315 static __inline__ s64 atomic64_read(const atomic64_t *v)
319 __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
324 static __inline__ void atomic64_set(atomic64_t *v, s64 i)
326 __asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
329 #define ATOMIC64_OP(op, asm_op) \
330 static __inline__ void atomic64_##op(s64 a, atomic64_t *v) \
334 __asm__ __volatile__( \
335 "1: ldarx %0,0,%3 # atomic64_" #op "\n" \
336 #asm_op " %0,%2,%0\n" \
337 " stdcx. %0,0,%3 \n" \
339 : "=&r" (t), "+m" (v->counter) \
340 : "r" (a), "r" (&v->counter) \
344 #define ATOMIC64_OP_RETURN_RELAXED(op, asm_op) \
346 atomic64_##op##_return_relaxed(s64 a, atomic64_t *v) \
350 __asm__ __volatile__( \
351 "1: ldarx %0,0,%3 # atomic64_" #op "_return_relaxed\n" \
352 #asm_op " %0,%2,%0\n" \
353 " stdcx. %0,0,%3\n" \
355 : "=&r" (t), "+m" (v->counter) \
356 : "r" (a), "r" (&v->counter) \
362 #define ATOMIC64_FETCH_OP_RELAXED(op, asm_op) \
364 atomic64_fetch_##op##_relaxed(s64 a, atomic64_t *v) \
368 __asm__ __volatile__( \
369 "1: ldarx %0,0,%4 # atomic64_fetch_" #op "_relaxed\n" \
370 #asm_op " %1,%3,%0\n" \
371 " stdcx. %1,0,%4\n" \
373 : "=&r" (res), "=&r" (t), "+m" (v->counter) \
374 : "r" (a), "r" (&v->counter) \
380 #define ATOMIC64_OPS(op, asm_op) \
381 ATOMIC64_OP(op, asm_op) \
382 ATOMIC64_OP_RETURN_RELAXED(op, asm_op) \
383 ATOMIC64_FETCH_OP_RELAXED(op, asm_op)
385 ATOMIC64_OPS(add, add)
386 ATOMIC64_OPS(sub, subf)
388 #define atomic64_add_return_relaxed atomic64_add_return_relaxed
389 #define atomic64_sub_return_relaxed atomic64_sub_return_relaxed
391 #define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed
392 #define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed
395 #define ATOMIC64_OPS(op, asm_op) \
396 ATOMIC64_OP(op, asm_op) \
397 ATOMIC64_FETCH_OP_RELAXED(op, asm_op)
399 ATOMIC64_OPS(and, and)
401 ATOMIC64_OPS(xor, xor)
403 #define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed
404 #define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed
405 #define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed
408 #undef ATOMIC64_FETCH_OP_RELAXED
409 #undef ATOMIC64_OP_RETURN_RELAXED
412 static __inline__ void atomic64_inc(atomic64_t *v)
416 __asm__ __volatile__(
417 "1: ldarx %0,0,%2 # atomic64_inc\n\
421 : "=&r" (t), "+m" (v->counter)
425 #define atomic64_inc atomic64_inc
427 static __inline__ s64 atomic64_inc_return_relaxed(atomic64_t *v)
431 __asm__ __volatile__(
432 "1: ldarx %0,0,%2 # atomic64_inc_return_relaxed\n"
436 : "=&r" (t), "+m" (v->counter)
443 static __inline__ void atomic64_dec(atomic64_t *v)
447 __asm__ __volatile__(
448 "1: ldarx %0,0,%2 # atomic64_dec\n\
452 : "=&r" (t), "+m" (v->counter)
456 #define atomic64_dec atomic64_dec
458 static __inline__ s64 atomic64_dec_return_relaxed(atomic64_t *v)
462 __asm__ __volatile__(
463 "1: ldarx %0,0,%2 # atomic64_dec_return_relaxed\n"
467 : "=&r" (t), "+m" (v->counter)
474 #define atomic64_inc_return_relaxed atomic64_inc_return_relaxed
475 #define atomic64_dec_return_relaxed atomic64_dec_return_relaxed
478 * Atomically test *v and decrement if it is greater than 0.
479 * The function returns the old value of *v minus 1.
481 static __inline__ s64 atomic64_dec_if_positive(atomic64_t *v)
485 __asm__ __volatile__(
486 PPC_ATOMIC_ENTRY_BARRIER
487 "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\
492 PPC_ATOMIC_EXIT_BARRIER
496 : "cc", "xer", "memory");
500 #define atomic64_dec_if_positive atomic64_dec_if_positive
502 #define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
503 #define atomic64_cmpxchg_relaxed(v, o, n) \
504 cmpxchg_relaxed(&((v)->counter), (o), (n))
505 #define atomic64_cmpxchg_acquire(v, o, n) \
506 cmpxchg_acquire(&((v)->counter), (o), (n))
508 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
509 #define atomic64_xchg_relaxed(v, new) xchg_relaxed(&((v)->counter), (new))
512 * atomic64_fetch_add_unless - add unless the number is a given value
513 * @v: pointer of type atomic64_t
514 * @a: the amount to add to v...
515 * @u: ...unless v is equal to u.
517 * Atomically adds @a to @v, so long as it was not @u.
518 * Returns the old value of @v.
520 static __inline__ s64 atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
524 __asm__ __volatile__ (
525 PPC_ATOMIC_ENTRY_BARRIER
526 "1: ldarx %0,0,%1 # atomic64_fetch_add_unless\n\
532 PPC_ATOMIC_EXIT_BARRIER
536 : "r" (&v->counter), "r" (a), "r" (u)
541 #define atomic64_fetch_add_unless atomic64_fetch_add_unless
544 * atomic_inc64_not_zero - increment unless the number is zero
545 * @v: pointer of type atomic64_t
547 * Atomically increments @v by 1, so long as @v is non-zero.
548 * Returns non-zero if @v was non-zero, and zero otherwise.
550 static __inline__ int atomic64_inc_not_zero(atomic64_t *v)
554 __asm__ __volatile__ (
555 PPC_ATOMIC_ENTRY_BARRIER
556 "1: ldarx %0,0,%2 # atomic64_inc_not_zero\n\
562 PPC_ATOMIC_EXIT_BARRIER
565 : "=&r" (t1), "=&r" (t2)
567 : "cc", "xer", "memory");
571 #define atomic64_inc_not_zero(v) atomic64_inc_not_zero((v))
573 #endif /* __powerpc64__ */
575 #endif /* __KERNEL__ */
576 #endif /* _ASM_POWERPC_ATOMIC_H_ */