xref: /openbmc/linux/arch/loongarch/include/asm/local.h (revision 3e16184a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #ifndef _ARCH_LOONGARCH_LOCAL_H
6 #define _ARCH_LOONGARCH_LOCAL_H
7 
8 #include <linux/percpu.h>
9 #include <linux/bitops.h>
10 #include <linux/atomic.h>
11 #include <asm/cmpxchg.h>
12 
13 typedef struct {
14 	atomic_long_t a;
15 } local_t;
16 
17 #define LOCAL_INIT(i)	{ ATOMIC_LONG_INIT(i) }
18 
19 #define local_read(l)	atomic_long_read(&(l)->a)
20 #define local_set(l, i) atomic_long_set(&(l)->a, (i))
21 
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)
26 
27 /*
28  * Same as above, but return the result value
29  */
30 static inline long local_add_return(long i, local_t *l)
31 {
32 	unsigned long result;
33 
34 	__asm__ __volatile__(
35 	"   " __AMADD " %1, %2, %0      \n"
36 	: "+ZB" (l->a.counter), "=&r" (result)
37 	: "r" (i)
38 	: "memory");
39 	result = result + i;
40 
41 	return result;
42 }
43 
44 static inline long local_sub_return(long i, local_t *l)
45 {
46 	unsigned long result;
47 
48 	__asm__ __volatile__(
49 	"   " __AMADD "%1, %2, %0       \n"
50 	: "+ZB" (l->a.counter), "=&r" (result)
51 	: "r" (-i)
52 	: "memory");
53 
54 	result = result - i;
55 
56 	return result;
57 }
58 
59 static inline long local_cmpxchg(local_t *l, long old, long new)
60 {
61 	return cmpxchg_local(&l->a.counter, old, new);
62 }
63 
64 static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
65 {
66 	typeof(l->a.counter) *__old = (typeof(l->a.counter) *) old;
67 	return try_cmpxchg_local(&l->a.counter, __old, new);
68 }
69 
70 #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
71 
72 /**
73  * local_add_unless - add unless the number is a given value
74  * @l: pointer of type local_t
75  * @a: the amount to add to l...
76  * @u: ...unless l is equal to u.
77  *
78  * Atomically adds @a to @l, so long as it was not @u.
79  * Returns non-zero if @l was not @u, and zero otherwise.
80  */
81 #define local_add_unless(l, a, u)				\
82 ({								\
83 	long c, old;						\
84 	c = local_read(l);					\
85 	while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
86 		c = old;					\
87 	c != (u);						\
88 })
89 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
90 
91 #define local_dec_return(l) local_sub_return(1, (l))
92 #define local_inc_return(l) local_add_return(1, (l))
93 
94 /*
95  * local_sub_and_test - subtract value from variable and test result
96  * @i: integer value to subtract
97  * @l: pointer of type local_t
98  *
99  * Atomically subtracts @i from @l and returns
100  * true if the result is zero, or false for all
101  * other cases.
102  */
103 #define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
104 
105 /*
106  * local_inc_and_test - increment and test
107  * @l: pointer of type local_t
108  *
109  * Atomically increments @l by 1
110  * and returns true if the result is zero, or false for all
111  * other cases.
112  */
113 #define local_inc_and_test(l) (local_inc_return(l) == 0)
114 
115 /*
116  * local_dec_and_test - decrement by 1 and test
117  * @l: pointer of type local_t
118  *
119  * Atomically decrements @l by 1 and
120  * returns true if the result is 0, or false for all other
121  * cases.
122  */
123 #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
124 
125 /*
126  * local_add_negative - add and test if negative
127  * @l: pointer of type local_t
128  * @i: integer value to add
129  *
130  * Atomically adds @i to @l and returns true
131  * if the result is negative, or false when
132  * result is greater than or equal to zero.
133  */
134 #define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
135 
136 /* Use these for per-cpu local_t variables: on some archs they are
137  * much more efficient than these naive implementations.  Note they take
138  * a variable, not an address.
139  */
140 
141 #define __local_inc(l)		((l)->a.counter++)
142 #define __local_dec(l)		((l)->a.counter++)
143 #define __local_add(i, l)	((l)->a.counter += (i))
144 #define __local_sub(i, l)	((l)->a.counter -= (i))
145 
146 #endif /* _ARCH_LOONGARCH_LOCAL_H */
147