1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_POWERPC_LOCAL_H
3 #define _ARCH_POWERPC_LOCAL_H
5 #include <linux/percpu.h>
6 #include <linux/atomic.h>
13 #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
15 #define local_read(l) atomic_long_read(&(l)->a)
16 #define local_set(l,i) atomic_long_set(&(l)->a, (i))
18 #define local_add(i,l) atomic_long_add((i),(&(l)->a))
19 #define local_sub(i,l) atomic_long_sub((i),(&(l)->a))
20 #define local_inc(l) atomic_long_inc(&(l)->a)
21 #define local_dec(l) atomic_long_dec(&(l)->a)
23 static __inline__ long local_add_return(long a, local_t *l)
28 "1:" PPC_LLARX(%0,0,%2,0) " # local_add_return\n\
31 PPC_STLCX "%0,0,%2 \n\
34 : "r" (a), "r" (&(l->a.counter))
40 #define local_add_negative(a, l) (local_add_return((a), (l)) < 0)
42 static __inline__ long local_sub_return(long a, local_t *l)
47 "1:" PPC_LLARX(%0,0,%2,0) " # local_sub_return\n\
50 PPC_STLCX "%0,0,%2 \n\
53 : "r" (a), "r" (&(l->a.counter))
59 static __inline__ long local_inc_return(local_t *l)
64 "1:" PPC_LLARX(%0,0,%1,0) " # local_inc_return\n\
67 PPC_STLCX "%0,0,%1 \n\
70 : "r" (&(l->a.counter))
71 : "cc", "xer", "memory");
77 * local_inc_and_test - increment and test
78 * @l: pointer of type local_t
80 * Atomically increments @l by 1
81 * and returns true if the result is zero, or false for all
84 #define local_inc_and_test(l) (local_inc_return(l) == 0)
86 static __inline__ long local_dec_return(local_t *l)
91 "1:" PPC_LLARX(%0,0,%1,0) " # local_dec_return\n\
97 : "r" (&(l->a.counter))
98 : "cc", "xer", "memory");
103 #define local_cmpxchg(l, o, n) \
104 (cmpxchg_local(&((l)->a.counter), (o), (n)))
105 #define local_xchg(l, n) (xchg_local(&((l)->a.counter), (n)))
108 * local_add_unless - add unless the number is a given value
109 * @l: pointer of type local_t
110 * @a: the amount to add to v...
111 * @u: ...unless v is equal to u.
113 * Atomically adds @a to @l, so long as it was not @u.
114 * Returns non-zero if @l was not @u, and zero otherwise.
116 static __inline__ int local_add_unless(local_t *l, long a, long u)
120 __asm__ __volatile__ (
121 "1:" PPC_LLARX(%0,0,%1,0) " # local_add_unless\n\
126 PPC_STLCX "%0,0,%1 \n\
131 : "r" (&(l->a.counter)), "r" (a), "r" (u)
137 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
139 #define local_sub_and_test(a, l) (local_sub_return((a), (l)) == 0)
140 #define local_dec_and_test(l) (local_dec_return((l)) == 0)
143 * Atomically test *l and decrement if it is greater than 0.
144 * The function returns the old value of *l minus 1.
146 static __inline__ long local_dec_if_positive(local_t *l)
150 __asm__ __volatile__(
151 "1:" PPC_LLARX(%0,0,%1,0) " # local_dec_if_positive\n\
156 PPC_STLCX "%0,0,%1\n\
160 : "r" (&(l->a.counter))
166 /* Use these for per-cpu local_t variables: on some archs they are
167 * much more efficient than these naive implementations. Note they take
168 * a variable, not an address.
171 #define __local_inc(l) ((l)->a.counter++)
172 #define __local_dec(l) ((l)->a.counter++)
173 #define __local_add(i,l) ((l)->a.counter+=(i))
174 #define __local_sub(i,l) ((l)->a.counter-=(i))
176 #endif /* _ARCH_POWERPC_LOCAL_H */