1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ATOMIC64_32_H
3 #define _ASM_X86_ATOMIC64_32_H
5 #include <linux/compiler.h>
6 #include <linux/types.h>
7 //#include <asm/cmpxchg.h>
9 /* An 64bit atomic type */
12 u64 __aligned(8) counter;
15 #define ATOMIC64_INIT(val) { (val) }
17 #define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
18 #ifndef ATOMIC64_EXPORT
19 #define ATOMIC64_DECL_ONE __ATOMIC64_DECL
21 #define ATOMIC64_DECL_ONE(sym) __ATOMIC64_DECL(sym); \
22 ATOMIC64_EXPORT(atomic64_##sym)
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)
30 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8)
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)
36 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8); \
37 ATOMIC64_DECL_ONE(sym##_386)
39 ATOMIC64_DECL_ONE(add_386);
40 ATOMIC64_DECL_ONE(sub_386);
41 ATOMIC64_DECL_ONE(inc_386);
42 ATOMIC64_DECL_ONE(dec_386);
45 #define alternative_atomic64(f, out, in...) \
46 __alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)
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);
60 #undef ATOMIC64_DECL_ONE
61 #undef __ATOMIC64_DECL
62 #undef ATOMIC64_EXPORT
65 * arch_atomic64_cmpxchg - cmpxchg atomic64 variable
66 * @v: pointer to type atomic64_t
70 * Atomically sets @v to @n if it was equal to @o and returns
74 static inline long long arch_atomic64_cmpxchg(atomic64_t *v, long long o,
77 return arch_cmpxchg64(&v->counter, o, n);
81 * arch_atomic64_xchg - xchg atomic64 variable
82 * @v: pointer to type atomic64_t
85 * Atomically xchgs the value of @v to @n and returns
88 static inline long long arch_atomic64_xchg(atomic64_t *v, long long n)
91 unsigned high = (unsigned)(n >> 32);
92 unsigned low = (unsigned)n;
93 alternative_atomic64(xchg, "=&A" (o),
94 "S" (v), "b" (low), "c" (high)
100 * arch_atomic64_set - set atomic64 variable
101 * @v: pointer to type atomic64_t
102 * @i: value to assign
104 * Atomically sets the value of @v to @n.
106 static inline void arch_atomic64_set(atomic64_t *v, long long i)
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");
116 * arch_atomic64_read - read atomic64 variable
117 * @v: pointer to type atomic64_t
119 * Atomically reads the value of @v and returns it.
121 static inline long long arch_atomic64_read(const atomic64_t *v)
124 alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
129 * arch_atomic64_add_return - add and return
130 * @i: integer value to add
131 * @v: pointer to type atomic64_t
133 * Atomically adds @i to @v and returns @i + *@v
135 static inline long long arch_atomic64_add_return(long long i, atomic64_t *v)
137 alternative_atomic64(add_return,
138 ASM_OUTPUT2("+A" (i), "+c" (v)),
139 ASM_NO_INPUT_CLOBBER("memory"));
144 * Other variants with different arithmetic operators:
146 static inline long long arch_atomic64_sub_return(long long i, atomic64_t *v)
148 alternative_atomic64(sub_return,
149 ASM_OUTPUT2("+A" (i), "+c" (v)),
150 ASM_NO_INPUT_CLOBBER("memory"));
154 static inline long long arch_atomic64_inc_return(atomic64_t *v)
157 alternative_atomic64(inc_return, "=&A" (a),
158 "S" (v) : "memory", "ecx");
161 #define arch_atomic64_inc_return arch_atomic64_inc_return
163 static inline long long arch_atomic64_dec_return(atomic64_t *v)
166 alternative_atomic64(dec_return, "=&A" (a),
167 "S" (v) : "memory", "ecx");
170 #define arch_atomic64_dec_return arch_atomic64_dec_return
173 * arch_atomic64_add - add integer to atomic64 variable
174 * @i: integer value to add
175 * @v: pointer to type atomic64_t
177 * Atomically adds @i to @v.
179 static inline long long arch_atomic64_add(long long i, atomic64_t *v)
181 __alternative_atomic64(add, add_return,
182 ASM_OUTPUT2("+A" (i), "+c" (v)),
183 ASM_NO_INPUT_CLOBBER("memory"));
188 * arch_atomic64_sub - subtract the atomic64 variable
189 * @i: integer value to subtract
190 * @v: pointer to type atomic64_t
192 * Atomically subtracts @i from @v.
194 static inline long long arch_atomic64_sub(long long i, atomic64_t *v)
196 __alternative_atomic64(sub, sub_return,
197 ASM_OUTPUT2("+A" (i), "+c" (v)),
198 ASM_NO_INPUT_CLOBBER("memory"));
203 * arch_atomic64_inc - increment atomic64 variable
204 * @v: pointer to type atomic64_t
206 * Atomically increments @v by 1.
208 static inline void arch_atomic64_inc(atomic64_t *v)
210 __alternative_atomic64(inc, inc_return, /* no output */,
211 "S" (v) : "memory", "eax", "ecx", "edx");
213 #define arch_atomic64_inc arch_atomic64_inc
216 * arch_atomic64_dec - decrement atomic64 variable
217 * @v: pointer to type atomic64_t
219 * Atomically decrements @v by 1.
221 static inline void arch_atomic64_dec(atomic64_t *v)
223 __alternative_atomic64(dec, dec_return, /* no output */,
224 "S" (v) : "memory", "eax", "ecx", "edx");
226 #define arch_atomic64_dec arch_atomic64_dec
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.
234 * Atomically adds @a to @v, so long as it was not @u.
235 * Returns non-zero if the add was done, zero otherwise.
237 static inline int arch_atomic64_add_unless(atomic64_t *v, long long a,
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)),
248 static inline int arch_atomic64_inc_not_zero(atomic64_t *v)
251 alternative_atomic64(inc_not_zero, "=&a" (r),
252 "S" (v) : "ecx", "edx", "memory");
255 #define arch_atomic64_inc_not_zero arch_atomic64_inc_not_zero
257 static inline long long arch_atomic64_dec_if_positive(atomic64_t *v)
260 alternative_atomic64(dec_if_positive, "=&A" (r),
261 "S" (v) : "ecx", "memory");
264 #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
266 #undef alternative_atomic64
267 #undef __alternative_atomic64
269 static inline void arch_atomic64_and(long long i, atomic64_t *v)
271 long long old, c = 0;
273 while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
277 static inline long long arch_atomic64_fetch_and(long long i, atomic64_t *v)
279 long long old, c = 0;
281 while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
287 static inline void arch_atomic64_or(long long i, atomic64_t *v)
289 long long old, c = 0;
291 while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
295 static inline long long arch_atomic64_fetch_or(long long i, atomic64_t *v)
297 long long old, c = 0;
299 while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
305 static inline void arch_atomic64_xor(long long i, atomic64_t *v)
307 long long old, c = 0;
309 while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
313 static inline long long arch_atomic64_fetch_xor(long long i, atomic64_t *v)
315 long long old, c = 0;
317 while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
323 static inline long long arch_atomic64_fetch_add(long long i, atomic64_t *v)
325 long long old, c = 0;
327 while ((old = arch_atomic64_cmpxchg(v, c, c + i)) != c)
333 #define arch_atomic64_fetch_sub(i, v) arch_atomic64_fetch_add(-(i), (v))
335 #endif /* _ASM_X86_ATOMIC64_32_H */