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>
9 #include <asm/cmpxchg.h>
10 #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
) {
39 " .set "MIPS_ISA_ARCH_LEVEL
" \n"
40 __SYNC(full
, loongson3_war
) " \n"
41 "1:" __stringify(LONG_LL
) " %1, %2 \n"
42 __stringify(LONG_ADDU
) " %0, %1, %3 \n"
43 __stringify(LONG_SC
) " %0, %2 \n"
44 __stringify(SC_BEQZ
) " %0, 1b \n"
45 __stringify(LONG_ADDU
) " %0, %1, %3 \n"
47 : "=&r" (result
), "=&r" (temp
), "=m" (l
->a
.counter
)
48 : "Ir" (i
), "m" (l
->a
.counter
)
53 local_irq_save(flags
);
54 result
= l
->a
.counter
;
56 l
->a
.counter
= result
;
57 local_irq_restore(flags
);
63 static __inline__
long local_sub_return(long i
, local_t
* l
)
67 if (kernel_uses_llsc
) {
72 " .set "MIPS_ISA_ARCH_LEVEL
" \n"
73 __SYNC(full
, loongson3_war
) " \n"
74 "1:" __stringify(LONG_LL
) " %1, %2 \n"
75 __stringify(LONG_SUBU
) " %0, %1, %3 \n"
76 __stringify(LONG_SUBU
) " %0, %1, %3 \n"
77 __stringify(LONG_SC
) " %0, %2 \n"
78 __stringify(SC_BEQZ
) " %0, 1b \n"
79 __stringify(LONG_SUBU
) " %0, %1, %3 \n"
81 : "=&r" (result
), "=&r" (temp
), "=m" (l
->a
.counter
)
82 : "Ir" (i
), "m" (l
->a
.counter
)
87 local_irq_save(flags
);
88 result
= l
->a
.counter
;
90 l
->a
.counter
= result
;
91 local_irq_restore(flags
);
97 static __inline__
long local_cmpxchg(local_t
*l
, long old
, long new)
99 return cmpxchg_local(&l
->a
.counter
, old
, new);
102 static __inline__
bool local_try_cmpxchg(local_t
*l
, long *old
, long new)
104 return try_cmpxchg_local(&l
->a
.counter
,
105 (typeof(l
->a
.counter
) *) old
, new);
108 #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
111 * local_add_unless - add unless the number is already a given value
112 * @l: pointer of type local_t
113 * @a: the amount to add to l...
114 * @u: ...unless l is equal to u.
116 * Atomically adds @a to @l, if @v was not already @u.
117 * Returns true if the addition was done.
119 static __inline__
bool
120 local_add_unless(local_t
*l
, long a
, long u
)
122 long c
= local_read(l
);
125 if (unlikely(c
== u
))
127 } while (!local_try_cmpxchg(l
, &c
, c
+ a
));
132 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
134 #define local_dec_return(l) local_sub_return(1, (l))
135 #define local_inc_return(l) local_add_return(1, (l))
138 * local_sub_and_test - subtract value from variable and test result
139 * @i: integer value to subtract
140 * @l: pointer of type local_t
142 * Atomically subtracts @i from @l and returns
143 * true if the result is zero, or false for all
146 #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
149 * local_inc_and_test - increment and test
150 * @l: pointer of type local_t
152 * Atomically increments @l by 1
153 * and returns true if the result is zero, or false for all
156 #define local_inc_and_test(l) (local_inc_return(l) == 0)
159 * local_dec_and_test - decrement by 1 and test
160 * @l: pointer of type local_t
162 * Atomically decrements @l by 1 and
163 * returns true if the result is 0, or false for all other
166 #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
169 * local_add_negative - add and test if negative
170 * @l: pointer of type local_t
171 * @i: integer value to add
173 * Atomically adds @i to @l and returns true
174 * if the result is negative, or false when
175 * result is greater than or equal to zero.
177 #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
179 /* Use these for per-cpu local_t variables: on some archs they are
180 * much more efficient than these naive implementations. Note they take
181 * a variable, not an address.
184 #define __local_inc(l) ((l)->a.counter++)
185 #define __local_dec(l) ((l)->a.counter++)
186 #define __local_add(i, l) ((l)->a.counter+=(i))
187 #define __local_sub(i, l) ((l)->a.counter-=(i))
189 #endif /* _ARCH_MIPS_LOCAL_H */