1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_ATOMIC_LONG_H
3 #define _ASM_GENERIC_ATOMIC_LONG_H
4 /*
5  * Copyright (C) 2005 Silicon Graphics, Inc.
6  *	Christoph Lameter
7  *
8  * Allows to provide arch independent atomic definitions without the need to
9  * edit all arch specific atomic.h files.
10  */
11 
12 #include <asm/types.h>
13 
14 /*
15  * Suppport for atomic_long_t
16  *
17  * Casts for parameters are avoided for existing atomic functions in order to
18  * avoid issues with cast-as-lval under gcc 4.x and other limitations that the
19  * macros of a platform may have.
20  */
21 
22 #if BITS_PER_LONG == 64
23 
24 typedef atomic64_t atomic_long_t;
25 
26 #define ATOMIC_LONG_INIT(i)	ATOMIC64_INIT(i)
27 #define ATOMIC_LONG_PFX(x)	atomic64 ## x
28 #define ATOMIC_LONG_TYPE	s64
29 
30 #else
31 
32 typedef atomic_t atomic_long_t;
33 
34 #define ATOMIC_LONG_INIT(i)	ATOMIC_INIT(i)
35 #define ATOMIC_LONG_PFX(x)	atomic ## x
36 #define ATOMIC_LONG_TYPE	int
37 
38 #endif
39 
40 #define ATOMIC_LONG_READ_OP(mo)						\
41 static inline long atomic_long_read##mo(const atomic_long_t *l)		\
42 {									\
43 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
44 									\
45 	return (long)ATOMIC_LONG_PFX(_read##mo)(v);			\
46 }
47 ATOMIC_LONG_READ_OP()
ATOMIC_LONG_READ_OP(_acquire)48 ATOMIC_LONG_READ_OP(_acquire)
49 
50 #undef ATOMIC_LONG_READ_OP
51 
52 #define ATOMIC_LONG_SET_OP(mo)						\
53 static inline void atomic_long_set##mo(atomic_long_t *l, long i)	\
54 {									\
55 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
56 									\
57 	ATOMIC_LONG_PFX(_set##mo)(v, i);				\
58 }
59 ATOMIC_LONG_SET_OP()
60 ATOMIC_LONG_SET_OP(_release)
61 
62 #undef ATOMIC_LONG_SET_OP
63 
64 #define ATOMIC_LONG_ADD_SUB_OP(op, mo)					\
65 static inline long							\
66 atomic_long_##op##_return##mo(long i, atomic_long_t *l)			\
67 {									\
68 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
69 									\
70 	return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(i, v);		\
71 }
72 ATOMIC_LONG_ADD_SUB_OP(add,)
73 ATOMIC_LONG_ADD_SUB_OP(add, _relaxed)
74 ATOMIC_LONG_ADD_SUB_OP(add, _acquire)
75 ATOMIC_LONG_ADD_SUB_OP(add, _release)
76 ATOMIC_LONG_ADD_SUB_OP(sub,)
77 ATOMIC_LONG_ADD_SUB_OP(sub, _relaxed)
78 ATOMIC_LONG_ADD_SUB_OP(sub, _acquire)
79 ATOMIC_LONG_ADD_SUB_OP(sub, _release)
80 
81 #undef ATOMIC_LONG_ADD_SUB_OP
82 
83 #define atomic_long_cmpxchg_relaxed(l, old, new) \
84 	(ATOMIC_LONG_PFX(_cmpxchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(l), \
85 					   (old), (new)))
86 #define atomic_long_cmpxchg_acquire(l, old, new) \
87 	(ATOMIC_LONG_PFX(_cmpxchg_acquire)((ATOMIC_LONG_PFX(_t) *)(l), \
88 					   (old), (new)))
89 #define atomic_long_cmpxchg_release(l, old, new) \
90 	(ATOMIC_LONG_PFX(_cmpxchg_release)((ATOMIC_LONG_PFX(_t) *)(l), \
91 					   (old), (new)))
92 #define atomic_long_cmpxchg(l, old, new) \
93 	(ATOMIC_LONG_PFX(_cmpxchg)((ATOMIC_LONG_PFX(_t) *)(l), (old), (new)))
94 
95 
96 #define atomic_long_try_cmpxchg_relaxed(l, old, new) \
97 	(ATOMIC_LONG_PFX(_try_cmpxchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(l), \
98 					   (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
99 #define atomic_long_try_cmpxchg_acquire(l, old, new) \
100 	(ATOMIC_LONG_PFX(_try_cmpxchg_acquire)((ATOMIC_LONG_PFX(_t) *)(l), \
101 					   (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
102 #define atomic_long_try_cmpxchg_release(l, old, new) \
103 	(ATOMIC_LONG_PFX(_try_cmpxchg_release)((ATOMIC_LONG_PFX(_t) *)(l), \
104 					   (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
105 #define atomic_long_try_cmpxchg(l, old, new) \
106 	(ATOMIC_LONG_PFX(_try_cmpxchg)((ATOMIC_LONG_PFX(_t) *)(l), \
107 				       (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
108 
109 
110 #define atomic_long_xchg_relaxed(v, new) \
111 	(ATOMIC_LONG_PFX(_xchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
112 #define atomic_long_xchg_acquire(v, new) \
113 	(ATOMIC_LONG_PFX(_xchg_acquire)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
114 #define atomic_long_xchg_release(v, new) \
115 	(ATOMIC_LONG_PFX(_xchg_release)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
116 #define atomic_long_xchg(v, new) \
117 	(ATOMIC_LONG_PFX(_xchg)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
118 
119 static __always_inline void atomic_long_inc(atomic_long_t *l)
120 {
121 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
122 
123 	ATOMIC_LONG_PFX(_inc)(v);
124 }
125 
atomic_long_dec(atomic_long_t * l)126 static __always_inline void atomic_long_dec(atomic_long_t *l)
127 {
128 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
129 
130 	ATOMIC_LONG_PFX(_dec)(v);
131 }
132 
133 #define ATOMIC_LONG_FETCH_OP(op, mo)					\
134 static inline long							\
135 atomic_long_fetch_##op##mo(long i, atomic_long_t *l)			\
136 {									\
137 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
138 									\
139 	return (long)ATOMIC_LONG_PFX(_fetch_##op##mo)(i, v);		\
140 }
141 
142 ATOMIC_LONG_FETCH_OP(add, )
ATOMIC_LONG_FETCH_OP(add,_relaxed)143 ATOMIC_LONG_FETCH_OP(add, _relaxed)
144 ATOMIC_LONG_FETCH_OP(add, _acquire)
145 ATOMIC_LONG_FETCH_OP(add, _release)
146 ATOMIC_LONG_FETCH_OP(sub, )
147 ATOMIC_LONG_FETCH_OP(sub, _relaxed)
148 ATOMIC_LONG_FETCH_OP(sub, _acquire)
149 ATOMIC_LONG_FETCH_OP(sub, _release)
150 ATOMIC_LONG_FETCH_OP(and, )
151 ATOMIC_LONG_FETCH_OP(and, _relaxed)
152 ATOMIC_LONG_FETCH_OP(and, _acquire)
153 ATOMIC_LONG_FETCH_OP(and, _release)
154 ATOMIC_LONG_FETCH_OP(andnot, )
155 ATOMIC_LONG_FETCH_OP(andnot, _relaxed)
156 ATOMIC_LONG_FETCH_OP(andnot, _acquire)
157 ATOMIC_LONG_FETCH_OP(andnot, _release)
158 ATOMIC_LONG_FETCH_OP(or, )
159 ATOMIC_LONG_FETCH_OP(or, _relaxed)
160 ATOMIC_LONG_FETCH_OP(or, _acquire)
161 ATOMIC_LONG_FETCH_OP(or, _release)
162 ATOMIC_LONG_FETCH_OP(xor, )
163 ATOMIC_LONG_FETCH_OP(xor, _relaxed)
164 ATOMIC_LONG_FETCH_OP(xor, _acquire)
165 ATOMIC_LONG_FETCH_OP(xor, _release)
166 
167 #undef ATOMIC_LONG_FETCH_OP
168 
169 #define ATOMIC_LONG_FETCH_INC_DEC_OP(op, mo)					\
170 static inline long							\
171 atomic_long_fetch_##op##mo(atomic_long_t *l)				\
172 {									\
173 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
174 									\
175 	return (long)ATOMIC_LONG_PFX(_fetch_##op##mo)(v);		\
176 }
177 
178 ATOMIC_LONG_FETCH_INC_DEC_OP(inc,)
179 ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _relaxed)
180 ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _acquire)
181 ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _release)
182 ATOMIC_LONG_FETCH_INC_DEC_OP(dec,)
183 ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _relaxed)
184 ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _acquire)
185 ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _release)
186 
187 #undef ATOMIC_LONG_FETCH_INC_DEC_OP
188 
189 #define ATOMIC_LONG_OP(op)						\
190 static __always_inline void						\
191 atomic_long_##op(long i, atomic_long_t *l)				\
192 {									\
193 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
194 									\
195 	ATOMIC_LONG_PFX(_##op)(i, v);					\
196 }
197 
198 ATOMIC_LONG_OP(add)
199 ATOMIC_LONG_OP(sub)
200 ATOMIC_LONG_OP(and)
201 ATOMIC_LONG_OP(andnot)
202 ATOMIC_LONG_OP(or)
203 ATOMIC_LONG_OP(xor)
204 
205 #undef ATOMIC_LONG_OP
206 
207 static inline int atomic_long_sub_and_test(long i, atomic_long_t *l)
208 {
209 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
210 
211 	return ATOMIC_LONG_PFX(_sub_and_test)(i, v);
212 }
213 
atomic_long_dec_and_test(atomic_long_t * l)214 static inline int atomic_long_dec_and_test(atomic_long_t *l)
215 {
216 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
217 
218 	return ATOMIC_LONG_PFX(_dec_and_test)(v);
219 }
220 
atomic_long_inc_and_test(atomic_long_t * l)221 static inline int atomic_long_inc_and_test(atomic_long_t *l)
222 {
223 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
224 
225 	return ATOMIC_LONG_PFX(_inc_and_test)(v);
226 }
227 
atomic_long_add_negative(long i,atomic_long_t * l)228 static inline int atomic_long_add_negative(long i, atomic_long_t *l)
229 {
230 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
231 
232 	return ATOMIC_LONG_PFX(_add_negative)(i, v);
233 }
234 
235 #define ATOMIC_LONG_INC_DEC_OP(op, mo)					\
236 static inline long							\
237 atomic_long_##op##_return##mo(atomic_long_t *l)				\
238 {									\
239 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;		\
240 									\
241 	return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(v);		\
242 }
243 ATOMIC_LONG_INC_DEC_OP(inc,)
ATOMIC_LONG_INC_DEC_OP(inc,_relaxed)244 ATOMIC_LONG_INC_DEC_OP(inc, _relaxed)
245 ATOMIC_LONG_INC_DEC_OP(inc, _acquire)
246 ATOMIC_LONG_INC_DEC_OP(inc, _release)
247 ATOMIC_LONG_INC_DEC_OP(dec,)
248 ATOMIC_LONG_INC_DEC_OP(dec, _relaxed)
249 ATOMIC_LONG_INC_DEC_OP(dec, _acquire)
250 ATOMIC_LONG_INC_DEC_OP(dec, _release)
251 
252 #undef ATOMIC_LONG_INC_DEC_OP
253 
254 static inline long atomic_long_add_unless(atomic_long_t *l, long a, long u)
255 {
256 	ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
257 
258 	return (long)ATOMIC_LONG_PFX(_add_unless)(v, a, u);
259 }
260 
261 #define atomic_long_inc_not_zero(l) \
262 	ATOMIC_LONG_PFX(_inc_not_zero)((ATOMIC_LONG_PFX(_t) *)(l))
263 
264 #define atomic_long_cond_read_relaxed(v, c) \
265 	ATOMIC_LONG_PFX(_cond_read_relaxed)((ATOMIC_LONG_PFX(_t) *)(v), (c))
266 #define atomic_long_cond_read_acquire(v, c) \
267 	ATOMIC_LONG_PFX(_cond_read_acquire)((ATOMIC_LONG_PFX(_t) *)(v), (c))
268 
269 #endif  /*  _ASM_GENERIC_ATOMIC_LONG_H  */
270