1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_MIPS_LOCAL_H
3 #define _ARCH_MIPS_LOCAL_H
5 #include <linux/percpu.h>
6 #include <linux/bitops.h>
7 #include <linux/atomic.h>
8 #include <asm/cmpxchg.h>
9 #include <asm/compiler.h>
17 #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
19 #define local_read(l) atomic_long_read(&(l)->a)
20 #define local_set(l, i) atomic_long_set(&(l)->a, (i))
22 #define local_add(i, l) atomic_long_add((i), (&(l)->a))
23 #define local_sub(i, l) atomic_long_sub((i), (&(l)->a))
24 #define local_inc(l) atomic_long_inc(&(l)->a)
25 #define local_dec(l) atomic_long_dec(&(l)->a)
28 * Same as above, but return the result value
30 static __inline__ long local_add_return(long i, local_t * l)
34 if (kernel_uses_llsc && R10000_LLSC_WAR) {
40 "1:" __LL "%1, %2 # local_add_return \n"
46 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
47 : "Ir" (i), "m" (l->a.counter)
49 } else if (kernel_uses_llsc) {
54 " .set "MIPS_ISA_ARCH_LEVEL" \n"
55 "1:" __LL "%1, %2 # local_add_return \n"
61 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
62 : "Ir" (i), "m" (l->a.counter)
67 local_irq_save(flags);
68 result = l->a.counter;
70 l->a.counter = result;
71 local_irq_restore(flags);
77 static __inline__ long local_sub_return(long i, local_t * l)
81 if (kernel_uses_llsc && R10000_LLSC_WAR) {
87 "1:" __LL "%1, %2 # local_sub_return \n"
93 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
94 : "Ir" (i), "m" (l->a.counter)
96 } else if (kernel_uses_llsc) {
101 " .set "MIPS_ISA_ARCH_LEVEL" \n"
102 "1:" __LL "%1, %2 # local_sub_return \n"
103 " subu %0, %1, %3 \n"
106 " subu %0, %1, %3 \n"
108 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
109 : "Ir" (i), "m" (l->a.counter)
114 local_irq_save(flags);
115 result = l->a.counter;
117 l->a.counter = result;
118 local_irq_restore(flags);
124 #define local_cmpxchg(l, o, n) \
125 ((long)cmpxchg_local(&((l)->a.counter), (o), (n)))
126 #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
129 * local_add_unless - add unless the number is a given value
130 * @l: pointer of type local_t
131 * @a: the amount to add to l...
132 * @u: ...unless l is equal to u.
134 * Atomically adds @a to @l, so long as it was not @u.
135 * Returns non-zero if @l was not @u, and zero otherwise.
137 #define local_add_unless(l, a, u) \
141 while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
145 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
147 #define local_dec_return(l) local_sub_return(1, (l))
148 #define local_inc_return(l) local_add_return(1, (l))
151 * local_sub_and_test - subtract value from variable and test result
152 * @i: integer value to subtract
153 * @l: pointer of type local_t
155 * Atomically subtracts @i from @l and returns
156 * true if the result is zero, or false for all
159 #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
162 * local_inc_and_test - increment and test
163 * @l: pointer of type local_t
165 * Atomically increments @l by 1
166 * and returns true if the result is zero, or false for all
169 #define local_inc_and_test(l) (local_inc_return(l) == 0)
172 * local_dec_and_test - decrement by 1 and test
173 * @l: pointer of type local_t
175 * Atomically decrements @l by 1 and
176 * returns true if the result is 0, or false for all other
179 #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
182 * local_add_negative - add and test if negative
183 * @l: pointer of type local_t
184 * @i: integer value to add
186 * Atomically adds @i to @l and returns true
187 * if the result is negative, or false when
188 * result is greater than or equal to zero.
190 #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
192 /* Use these for per-cpu local_t variables: on some archs they are
193 * much more efficient than these naive implementations. Note they take
194 * a variable, not an address.
197 #define __local_inc(l) ((l)->a.counter++)
198 #define __local_dec(l) ((l)->a.counter++)
199 #define __local_add(i, l) ((l)->a.counter+=(i))
200 #define __local_sub(i, l) ((l)->a.counter-=(i))
202 #endif /* _ARCH_MIPS_LOCAL_H */