• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_MIPS_LOCAL_H
3 #define _ARCH_MIPS_LOCAL_H
4 
5 #include <linux/percpu.h>
6 #include <linux/bitops.h>
7 #include <linux/atomic.h>
8 #include <asm/asm.h>
9 #include <asm/cmpxchg.h>
10 #include <asm/compiler.h>
11 #include <asm/war.h>
12 
13 typedef struct
14 {
15 	atomic_long_t a;
16 } local_t;
17 
18 #define LOCAL_INIT(i)	{ ATOMIC_LONG_INIT(i) }
19 
20 #define local_read(l)	atomic_long_read(&(l)->a)
21 #define local_set(l, i) atomic_long_set(&(l)->a, (i))
22 
23 #define local_add(i, l) atomic_long_add((i), (&(l)->a))
24 #define local_sub(i, l) atomic_long_sub((i), (&(l)->a))
25 #define local_inc(l)	atomic_long_inc(&(l)->a)
26 #define local_dec(l)	atomic_long_dec(&(l)->a)
27 
28 /*
29  * Same as above, but return the result value
30  */
local_add_return(long i,local_t * l)31 static __inline__ long local_add_return(long i, local_t * l)
32 {
33 	unsigned long result;
34 
35 	if (kernel_uses_llsc && IS_ENABLED(CONFIG_WAR_R10000_LLSC)) {
36 		unsigned long temp;
37 
38 		__asm__ __volatile__(
39 		"	.set	push					\n"
40 		"	.set	arch=r4000				\n"
41 			__SYNC(full, loongson3_war) "			\n"
42 		"1:"	__LL	"%1, %2		# local_add_return	\n"
43 			__stringify(LONG_ADDU)	"	%0, %1, %3	\n"
44 			__SC	"%0, %2					\n"
45 		"	beqzl	%0, 1b					\n"
46 		"	addu	%0, %1, %3				\n"
47 		"	.set	pop					\n"
48 		: "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
49 		: "Ir" (i), "m" (l->a.counter)
50 		: "memory");
51 	} else if (kernel_uses_llsc) {
52 		unsigned long temp;
53 
54 		__asm__ __volatile__(
55 		"	.set	push					\n"
56 		"	.set	"MIPS_ISA_ARCH_LEVEL"			\n"
57 			__SYNC(full, loongson3_war) "			\n"
58 		"1:"	__LL	"%1, %2		# local_add_return	\n"
59 			__stringify(LONG_ADDU)	"	%0, %1, %3	\n"
60 			__SC	"%0, %2					\n"
61 		"	beqz	%0, 1b					\n"
62 		"	addu	%0, %1, %3				\n"
63 		"	.set	pop					\n"
64 		: "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
65 		: "Ir" (i), "m" (l->a.counter)
66 		: "memory");
67 	} else {
68 		unsigned long flags;
69 
70 		local_irq_save(flags);
71 		result = l->a.counter;
72 		result += i;
73 		l->a.counter = result;
74 		local_irq_restore(flags);
75 	}
76 
77 	return result;
78 }
79 
local_sub_return(long i,local_t * l)80 static __inline__ long local_sub_return(long i, local_t * l)
81 {
82 	unsigned long result;
83 
84 	if (kernel_uses_llsc && IS_ENABLED(CONFIG_WAR_R10000_LLSC)) {
85 		unsigned long temp;
86 
87 		__asm__ __volatile__(
88 		"	.set	push					\n"
89 		"	.set	arch=r4000				\n"
90 			__SYNC(full, loongson3_war) "			\n"
91 		"1:"	__LL	"%1, %2		# local_sub_return	\n"
92 			__stringify(LONG_SUBU)	"	%0, %1, %3	\n"
93 			__SC	"%0, %2					\n"
94 		"	beqzl	%0, 1b					\n"
95 		"	subu	%0, %1, %3				\n"
96 		"	.set	pop					\n"
97 		: "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
98 		: "Ir" (i), "m" (l->a.counter)
99 		: "memory");
100 	} else if (kernel_uses_llsc) {
101 		unsigned long temp;
102 
103 		__asm__ __volatile__(
104 		"	.set	push					\n"
105 		"	.set	"MIPS_ISA_ARCH_LEVEL"			\n"
106 			__SYNC(full, loongson3_war) "			\n"
107 		"1:"	__LL	"%1, %2		# local_sub_return	\n"
108 			__stringify(LONG_SUBU)	"	%0, %1, %3	\n"
109 			__SC	"%0, %2					\n"
110 		"	beqz	%0, 1b					\n"
111 		"	subu	%0, %1, %3				\n"
112 		"	.set	pop					\n"
113 		: "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
114 		: "Ir" (i), "m" (l->a.counter)
115 		: "memory");
116 	} else {
117 		unsigned long flags;
118 
119 		local_irq_save(flags);
120 		result = l->a.counter;
121 		result -= i;
122 		l->a.counter = result;
123 		local_irq_restore(flags);
124 	}
125 
126 	return result;
127 }
128 
129 #define local_cmpxchg(l, o, n) \
130 	((long)cmpxchg_local(&((l)->a.counter), (o), (n)))
131 #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
132 
133 /**
134  * local_add_unless - add unless the number is a given value
135  * @l: pointer of type local_t
136  * @a: the amount to add to l...
137  * @u: ...unless l is equal to u.
138  *
139  * Atomically adds @a to @l, so long as it was not @u.
140  * Returns non-zero if @l was not @u, and zero otherwise.
141  */
142 #define local_add_unless(l, a, u)				\
143 ({								\
144 	long c, old;						\
145 	c = local_read(l);					\
146 	while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
147 		c = old;					\
148 	c != (u);						\
149 })
150 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
151 
152 #define local_dec_return(l) local_sub_return(1, (l))
153 #define local_inc_return(l) local_add_return(1, (l))
154 
155 /*
156  * local_sub_and_test - subtract value from variable and test result
157  * @i: integer value to subtract
158  * @l: pointer of type local_t
159  *
160  * Atomically subtracts @i from @l and returns
161  * true if the result is zero, or false for all
162  * other cases.
163  */
164 #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
165 
166 /*
167  * local_inc_and_test - increment and test
168  * @l: pointer of type local_t
169  *
170  * Atomically increments @l by 1
171  * and returns true if the result is zero, or false for all
172  * other cases.
173  */
174 #define local_inc_and_test(l) (local_inc_return(l) == 0)
175 
176 /*
177  * local_dec_and_test - decrement by 1 and test
178  * @l: pointer of type local_t
179  *
180  * Atomically decrements @l by 1 and
181  * returns true if the result is 0, or false for all other
182  * cases.
183  */
184 #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
185 
186 /*
187  * local_add_negative - add and test if negative
188  * @l: pointer of type local_t
189  * @i: integer value to add
190  *
191  * Atomically adds @i to @l and returns true
192  * if the result is negative, or false when
193  * result is greater than or equal to zero.
194  */
195 #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
196 
197 /* Use these for per-cpu local_t variables: on some archs they are
198  * much more efficient than these naive implementations.  Note they take
199  * a variable, not an address.
200  */
201 
202 #define __local_inc(l)		((l)->a.counter++)
203 #define __local_dec(l)		((l)->a.counter++)
204 #define __local_add(i, l)	((l)->a.counter+=(i))
205 #define __local_sub(i, l)	((l)->a.counter-=(i))
206 
207 #endif /* _ARCH_MIPS_LOCAL_H */
208