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
24typedef 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
32typedef 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) \
41static 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}
47ATOMIC_LONG_READ_OP()
48ATOMIC_LONG_READ_OP(_acquire)
49
50#undef ATOMIC_LONG_READ_OP
51
52#define ATOMIC_LONG_SET_OP(mo) \
53static 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}
59ATOMIC_LONG_SET_OP()
60ATOMIC_LONG_SET_OP(_release)
61
62#undef ATOMIC_LONG_SET_OP
63
64#define ATOMIC_LONG_ADD_SUB_OP(op, mo) \
65static inline long \
66atomic_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}
72ATOMIC_LONG_ADD_SUB_OP(add,)
73ATOMIC_LONG_ADD_SUB_OP(add, _relaxed)
74ATOMIC_LONG_ADD_SUB_OP(add, _acquire)
75ATOMIC_LONG_ADD_SUB_OP(add, _release)
76ATOMIC_LONG_ADD_SUB_OP(sub,)
77ATOMIC_LONG_ADD_SUB_OP(sub, _relaxed)
78ATOMIC_LONG_ADD_SUB_OP(sub, _acquire)
79ATOMIC_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
119static __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
126static __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) \
134static inline long \
135atomic_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
142ATOMIC_LONG_FETCH_OP(add, )
143ATOMIC_LONG_FETCH_OP(add, _relaxed)
144ATOMIC_LONG_FETCH_OP(add, _acquire)
145ATOMIC_LONG_FETCH_OP(add, _release)
146ATOMIC_LONG_FETCH_OP(sub, )
147ATOMIC_LONG_FETCH_OP(sub, _relaxed)
148ATOMIC_LONG_FETCH_OP(sub, _acquire)
149ATOMIC_LONG_FETCH_OP(sub, _release)
150ATOMIC_LONG_FETCH_OP(and, )
151ATOMIC_LONG_FETCH_OP(and, _relaxed)
152ATOMIC_LONG_FETCH_OP(and, _acquire)
153ATOMIC_LONG_FETCH_OP(and, _release)
154ATOMIC_LONG_FETCH_OP(andnot, )
155ATOMIC_LONG_FETCH_OP(andnot, _relaxed)
156ATOMIC_LONG_FETCH_OP(andnot, _acquire)
157ATOMIC_LONG_FETCH_OP(andnot, _release)
158ATOMIC_LONG_FETCH_OP(or, )
159ATOMIC_LONG_FETCH_OP(or, _relaxed)
160ATOMIC_LONG_FETCH_OP(or, _acquire)
161ATOMIC_LONG_FETCH_OP(or, _release)
162ATOMIC_LONG_FETCH_OP(xor, )
163ATOMIC_LONG_FETCH_OP(xor, _relaxed)
164ATOMIC_LONG_FETCH_OP(xor, _acquire)
165ATOMIC_LONG_FETCH_OP(xor, _release)
166
167#undef ATOMIC_LONG_FETCH_OP
168
169#define ATOMIC_LONG_FETCH_INC_DEC_OP(op, mo) \
170static inline long \
171atomic_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
178ATOMIC_LONG_FETCH_INC_DEC_OP(inc,)
179ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _relaxed)
180ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _acquire)
181ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _release)
182ATOMIC_LONG_FETCH_INC_DEC_OP(dec,)
183ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _relaxed)
184ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _acquire)
185ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _release)
186
187#undef ATOMIC_LONG_FETCH_INC_DEC_OP
188
189#define ATOMIC_LONG_OP(op) \
190static __always_inline void \
191atomic_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
198ATOMIC_LONG_OP(add)
199ATOMIC_LONG_OP(sub)
200ATOMIC_LONG_OP(and)
201ATOMIC_LONG_OP(andnot)
202ATOMIC_LONG_OP(or)
203ATOMIC_LONG_OP(xor)
204
205#undef ATOMIC_LONG_OP
206
207static 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
214static 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
221static 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
228static 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) \
236static inline long \
237atomic_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}
243ATOMIC_LONG_INC_DEC_OP(inc,)
244ATOMIC_LONG_INC_DEC_OP(inc, _relaxed)
245ATOMIC_LONG_INC_DEC_OP(inc, _acquire)
246ATOMIC_LONG_INC_DEC_OP(inc, _release)
247ATOMIC_LONG_INC_DEC_OP(dec,)
248ATOMIC_LONG_INC_DEC_OP(dec, _relaxed)
249ATOMIC_LONG_INC_DEC_OP(dec, _acquire)
250ATOMIC_LONG_INC_DEC_OP(dec, _release)
251
252#undef ATOMIC_LONG_INC_DEC_OP
253
254static 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