1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ATOMIC64_32_H
3 #define _ASM_X86_ATOMIC64_32_H
4
5 #include <linux/compiler.h>
6 #include <linux/types.h>
7 //#include <asm/cmpxchg.h>
8
9 /* An 64bit atomic type */
10
11 typedef struct {
12 u64 __aligned(8) counter;
13 } atomic64_t;
14
15 #define ATOMIC64_INIT(val) { (val) }
16
17 #define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
18 #ifndef ATOMIC64_EXPORT
19 #define ATOMIC64_DECL_ONE __ATOMIC64_DECL
20 #else
21 #define ATOMIC64_DECL_ONE(sym) __ATOMIC64_DECL(sym); \
22 ATOMIC64_EXPORT(atomic64_##sym)
23 #endif
24
25 #ifdef CONFIG_X86_CMPXCHG64
26 #define __alternative_atomic64(f, g, out, in...) \
27 asm volatile("call %P[func]" \
28 : out : [func] "i" (atomic64_##g##_cx8), ## in)
29
30 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8)
31 #else
32 #define __alternative_atomic64(f, g, out, in...) \
33 alternative_call(atomic64_##f##_386, atomic64_##g##_cx8, \
34 X86_FEATURE_CX8, ASM_OUTPUT2(out), ## in)
35
36 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8); \
37 ATOMIC64_DECL_ONE(sym##_386)
38
39 ATOMIC64_DECL_ONE(add_386);
40 ATOMIC64_DECL_ONE(sub_386);
41 ATOMIC64_DECL_ONE(inc_386);
42 ATOMIC64_DECL_ONE(dec_386);
43 #endif
44
45 #define alternative_atomic64(f, out, in...) \
46 __alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)
47
48 ATOMIC64_DECL(read);
49 ATOMIC64_DECL(set);
50 ATOMIC64_DECL(xchg);
51 ATOMIC64_DECL(add_return);
52 ATOMIC64_DECL(sub_return);
53 ATOMIC64_DECL(inc_return);
54 ATOMIC64_DECL(dec_return);
55 ATOMIC64_DECL(dec_if_positive);
56 ATOMIC64_DECL(inc_not_zero);
57 ATOMIC64_DECL(add_unless);
58
59 #undef ATOMIC64_DECL
60 #undef ATOMIC64_DECL_ONE
61 #undef __ATOMIC64_DECL
62 #undef ATOMIC64_EXPORT
63
64 /**
65 * arch_atomic64_cmpxchg - cmpxchg atomic64 variable
66 * @v: pointer to type atomic64_t
67 * @o: expected value
68 * @n: new value
69 *
70 * Atomically sets @v to @n if it was equal to @o and returns
71 * the old value.
72 */
73
arch_atomic64_cmpxchg(atomic64_t * v,long long o,long long n)74 static inline long long arch_atomic64_cmpxchg(atomic64_t *v, long long o,
75 long long n)
76 {
77 return arch_cmpxchg64(&v->counter, o, n);
78 }
79
80 /**
81 * arch_atomic64_xchg - xchg atomic64 variable
82 * @v: pointer to type atomic64_t
83 * @n: value to assign
84 *
85 * Atomically xchgs the value of @v to @n and returns
86 * the old value.
87 */
arch_atomic64_xchg(atomic64_t * v,long long n)88 static inline long long arch_atomic64_xchg(atomic64_t *v, long long n)
89 {
90 long long o;
91 unsigned high = (unsigned)(n >> 32);
92 unsigned low = (unsigned)n;
93 alternative_atomic64(xchg, "=&A" (o),
94 "S" (v), "b" (low), "c" (high)
95 : "memory");
96 return o;
97 }
98
99 /**
100 * arch_atomic64_set - set atomic64 variable
101 * @v: pointer to type atomic64_t
102 * @i: value to assign
103 *
104 * Atomically sets the value of @v to @n.
105 */
arch_atomic64_set(atomic64_t * v,long long i)106 static inline void arch_atomic64_set(atomic64_t *v, long long i)
107 {
108 unsigned high = (unsigned)(i >> 32);
109 unsigned low = (unsigned)i;
110 alternative_atomic64(set, /* no output */,
111 "S" (v), "b" (low), "c" (high)
112 : "eax", "edx", "memory");
113 }
114
115 /**
116 * arch_atomic64_read - read atomic64 variable
117 * @v: pointer to type atomic64_t
118 *
119 * Atomically reads the value of @v and returns it.
120 */
arch_atomic64_read(const atomic64_t * v)121 static inline long long arch_atomic64_read(const atomic64_t *v)
122 {
123 long long r;
124 alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
125 return r;
126 }
127
128 /**
129 * arch_atomic64_add_return - add and return
130 * @i: integer value to add
131 * @v: pointer to type atomic64_t
132 *
133 * Atomically adds @i to @v and returns @i + *@v
134 */
arch_atomic64_add_return(long long i,atomic64_t * v)135 static inline long long arch_atomic64_add_return(long long i, atomic64_t *v)
136 {
137 alternative_atomic64(add_return,
138 ASM_OUTPUT2("+A" (i), "+c" (v)),
139 ASM_NO_INPUT_CLOBBER("memory"));
140 return i;
141 }
142
143 /*
144 * Other variants with different arithmetic operators:
145 */
arch_atomic64_sub_return(long long i,atomic64_t * v)146 static inline long long arch_atomic64_sub_return(long long i, atomic64_t *v)
147 {
148 alternative_atomic64(sub_return,
149 ASM_OUTPUT2("+A" (i), "+c" (v)),
150 ASM_NO_INPUT_CLOBBER("memory"));
151 return i;
152 }
153
arch_atomic64_inc_return(atomic64_t * v)154 static inline long long arch_atomic64_inc_return(atomic64_t *v)
155 {
156 long long a;
157 alternative_atomic64(inc_return, "=&A" (a),
158 "S" (v) : "memory", "ecx");
159 return a;
160 }
161 #define arch_atomic64_inc_return arch_atomic64_inc_return
162
arch_atomic64_dec_return(atomic64_t * v)163 static inline long long arch_atomic64_dec_return(atomic64_t *v)
164 {
165 long long a;
166 alternative_atomic64(dec_return, "=&A" (a),
167 "S" (v) : "memory", "ecx");
168 return a;
169 }
170 #define arch_atomic64_dec_return arch_atomic64_dec_return
171
172 /**
173 * arch_atomic64_add - add integer to atomic64 variable
174 * @i: integer value to add
175 * @v: pointer to type atomic64_t
176 *
177 * Atomically adds @i to @v.
178 */
arch_atomic64_add(long long i,atomic64_t * v)179 static inline long long arch_atomic64_add(long long i, atomic64_t *v)
180 {
181 __alternative_atomic64(add, add_return,
182 ASM_OUTPUT2("+A" (i), "+c" (v)),
183 ASM_NO_INPUT_CLOBBER("memory"));
184 return i;
185 }
186
187 /**
188 * arch_atomic64_sub - subtract the atomic64 variable
189 * @i: integer value to subtract
190 * @v: pointer to type atomic64_t
191 *
192 * Atomically subtracts @i from @v.
193 */
arch_atomic64_sub(long long i,atomic64_t * v)194 static inline long long arch_atomic64_sub(long long i, atomic64_t *v)
195 {
196 __alternative_atomic64(sub, sub_return,
197 ASM_OUTPUT2("+A" (i), "+c" (v)),
198 ASM_NO_INPUT_CLOBBER("memory"));
199 return i;
200 }
201
202 /**
203 * arch_atomic64_inc - increment atomic64 variable
204 * @v: pointer to type atomic64_t
205 *
206 * Atomically increments @v by 1.
207 */
arch_atomic64_inc(atomic64_t * v)208 static inline void arch_atomic64_inc(atomic64_t *v)
209 {
210 __alternative_atomic64(inc, inc_return, /* no output */,
211 "S" (v) : "memory", "eax", "ecx", "edx");
212 }
213 #define arch_atomic64_inc arch_atomic64_inc
214
215 /**
216 * arch_atomic64_dec - decrement atomic64 variable
217 * @v: pointer to type atomic64_t
218 *
219 * Atomically decrements @v by 1.
220 */
arch_atomic64_dec(atomic64_t * v)221 static inline void arch_atomic64_dec(atomic64_t *v)
222 {
223 __alternative_atomic64(dec, dec_return, /* no output */,
224 "S" (v) : "memory", "eax", "ecx", "edx");
225 }
226 #define arch_atomic64_dec arch_atomic64_dec
227
228 /**
229 * arch_atomic64_add_unless - add unless the number is a given value
230 * @v: pointer of type atomic64_t
231 * @a: the amount to add to v...
232 * @u: ...unless v is equal to u.
233 *
234 * Atomically adds @a to @v, so long as it was not @u.
235 * Returns non-zero if the add was done, zero otherwise.
236 */
arch_atomic64_add_unless(atomic64_t * v,long long a,long long u)237 static inline int arch_atomic64_add_unless(atomic64_t *v, long long a,
238 long long u)
239 {
240 unsigned low = (unsigned)u;
241 unsigned high = (unsigned)(u >> 32);
242 alternative_atomic64(add_unless,
243 ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
244 "S" (v) : "memory");
245 return (int)a;
246 }
247
arch_atomic64_inc_not_zero(atomic64_t * v)248 static inline int arch_atomic64_inc_not_zero(atomic64_t *v)
249 {
250 int r;
251 alternative_atomic64(inc_not_zero, "=&a" (r),
252 "S" (v) : "ecx", "edx", "memory");
253 return r;
254 }
255 #define arch_atomic64_inc_not_zero arch_atomic64_inc_not_zero
256
arch_atomic64_dec_if_positive(atomic64_t * v)257 static inline long long arch_atomic64_dec_if_positive(atomic64_t *v)
258 {
259 long long r;
260 alternative_atomic64(dec_if_positive, "=&A" (r),
261 "S" (v) : "ecx", "memory");
262 return r;
263 }
264 #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
265
266 #undef alternative_atomic64
267 #undef __alternative_atomic64
268
arch_atomic64_and(long long i,atomic64_t * v)269 static inline void arch_atomic64_and(long long i, atomic64_t *v)
270 {
271 long long old, c = 0;
272
273 while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
274 c = old;
275 }
276
arch_atomic64_fetch_and(long long i,atomic64_t * v)277 static inline long long arch_atomic64_fetch_and(long long i, atomic64_t *v)
278 {
279 long long old, c = 0;
280
281 while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
282 c = old;
283
284 return old;
285 }
286
arch_atomic64_or(long long i,atomic64_t * v)287 static inline void arch_atomic64_or(long long i, atomic64_t *v)
288 {
289 long long old, c = 0;
290
291 while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
292 c = old;
293 }
294
arch_atomic64_fetch_or(long long i,atomic64_t * v)295 static inline long long arch_atomic64_fetch_or(long long i, atomic64_t *v)
296 {
297 long long old, c = 0;
298
299 while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
300 c = old;
301
302 return old;
303 }
304
arch_atomic64_xor(long long i,atomic64_t * v)305 static inline void arch_atomic64_xor(long long i, atomic64_t *v)
306 {
307 long long old, c = 0;
308
309 while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
310 c = old;
311 }
312
arch_atomic64_fetch_xor(long long i,atomic64_t * v)313 static inline long long arch_atomic64_fetch_xor(long long i, atomic64_t *v)
314 {
315 long long old, c = 0;
316
317 while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
318 c = old;
319
320 return old;
321 }
322
arch_atomic64_fetch_add(long long i,atomic64_t * v)323 static inline long long arch_atomic64_fetch_add(long long i, atomic64_t *v)
324 {
325 long long old, c = 0;
326
327 while ((old = arch_atomic64_cmpxchg(v, c, c + i)) != c)
328 c = old;
329
330 return old;
331 }
332
333 #define arch_atomic64_fetch_sub(i, v) arch_atomic64_fetch_add(-(i), (v))
334
335 #endif /* _ASM_X86_ATOMIC64_32_H */
336