]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/include/asm/atomic64_32.h
Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / atomic64_32.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1a3b1d89
BG
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>
1a3b1d89
BG
7//#include <asm/cmpxchg.h>
8
9/* An 64bit atomic type */
10
11typedef struct {
12 u64 __aligned(8) counter;
13} atomic64_t;
14
15#define ATOMIC64_INIT(val) { (val) }
16
819165fb
JB
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
a7e926ab 25#ifdef CONFIG_X86_CMPXCHG64
819165fb
JB
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)
a7e926ab 31#else
819165fb
JB
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
39ATOMIC64_DECL_ONE(add_386);
40ATOMIC64_DECL_ONE(sub_386);
41ATOMIC64_DECL_ONE(inc_386);
42ATOMIC64_DECL_ONE(dec_386);
a7e926ab
LB
43#endif
44
819165fb
JB
45#define alternative_atomic64(f, out, in...) \
46 __alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)
47
48ATOMIC64_DECL(read);
49ATOMIC64_DECL(set);
50ATOMIC64_DECL(xchg);
51ATOMIC64_DECL(add_return);
52ATOMIC64_DECL(sub_return);
53ATOMIC64_DECL(inc_return);
54ATOMIC64_DECL(dec_return);
55ATOMIC64_DECL(dec_if_positive);
56ATOMIC64_DECL(inc_not_zero);
57ATOMIC64_DECL(add_unless);
58
59#undef ATOMIC64_DECL
60#undef ATOMIC64_DECL_ONE
61#undef __ATOMIC64_DECL
62#undef ATOMIC64_EXPORT
a7e926ab
LB
63
64/**
65 * atomic64_cmpxchg - cmpxchg atomic64 variable
1f045978 66 * @v: pointer to type atomic64_t
a7e926ab
LB
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
74static inline long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
75{
76 return cmpxchg64(&v->counter, o, n);
77}
1a3b1d89
BG
78
79/**
80 * atomic64_xchg - xchg atomic64 variable
a7e926ab
LB
81 * @v: pointer to type atomic64_t
82 * @n: value to assign
1a3b1d89 83 *
a7e926ab 84 * Atomically xchgs the value of @v to @n and returns
1a3b1d89
BG
85 * the old value.
86 */
a7e926ab
LB
87static inline long long atomic64_xchg(atomic64_t *v, long long n)
88{
89 long long o;
90 unsigned high = (unsigned)(n >> 32);
91 unsigned low = (unsigned)n;
819165fb
JB
92 alternative_atomic64(xchg, "=&A" (o),
93 "S" (v), "b" (low), "c" (high)
94 : "memory");
a7e926ab
LB
95 return o;
96}
1a3b1d89
BG
97
98/**
99 * atomic64_set - set atomic64 variable
a7e926ab 100 * @v: pointer to type atomic64_t
1f045978 101 * @i: value to assign
1a3b1d89 102 *
a7e926ab 103 * Atomically sets the value of @v to @n.
1a3b1d89 104 */
a7e926ab
LB
105static inline void atomic64_set(atomic64_t *v, long long i)
106{
107 unsigned high = (unsigned)(i >> 32);
108 unsigned low = (unsigned)i;
819165fb
JB
109 alternative_atomic64(set, /* no output */,
110 "S" (v), "b" (low), "c" (high)
111 : "eax", "edx", "memory");
a7e926ab 112}
1a3b1d89
BG
113
114/**
115 * atomic64_read - read atomic64 variable
a7e926ab 116 * @v: pointer to type atomic64_t
1a3b1d89 117 *
a7e926ab 118 * Atomically reads the value of @v and returns it.
1a3b1d89 119 */
8030c36d 120static inline long long atomic64_read(const atomic64_t *v)
1a3b1d89 121{
a7e926ab 122 long long r;
819165fb 123 alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
a7e926ab
LB
124 return r;
125 }
1a3b1d89
BG
126
127/**
128 * atomic64_add_return - add and return
a7e926ab
LB
129 * @i: integer value to add
130 * @v: pointer to type atomic64_t
1a3b1d89 131 *
a7e926ab 132 * Atomically adds @i to @v and returns @i + *@v
1a3b1d89 133 */
a7e926ab
LB
134static inline long long atomic64_add_return(long long i, atomic64_t *v)
135{
819165fb
JB
136 alternative_atomic64(add_return,
137 ASM_OUTPUT2("+A" (i), "+c" (v)),
138 ASM_NO_INPUT_CLOBBER("memory"));
a7e926ab
LB
139 return i;
140}
1a3b1d89
BG
141
142/*
143 * Other variants with different arithmetic operators:
144 */
a7e926ab
LB
145static inline long long atomic64_sub_return(long long i, atomic64_t *v)
146{
819165fb
JB
147 alternative_atomic64(sub_return,
148 ASM_OUTPUT2("+A" (i), "+c" (v)),
149 ASM_NO_INPUT_CLOBBER("memory"));
a7e926ab
LB
150 return i;
151}
152
153static inline long long atomic64_inc_return(atomic64_t *v)
154{
155 long long a;
819165fb
JB
156 alternative_atomic64(inc_return, "=&A" (a),
157 "S" (v) : "memory", "ecx");
a7e926ab
LB
158 return a;
159}
160
161static inline long long atomic64_dec_return(atomic64_t *v)
162{
163 long long a;
819165fb
JB
164 alternative_atomic64(dec_return, "=&A" (a),
165 "S" (v) : "memory", "ecx");
a7e926ab
LB
166 return a;
167}
1a3b1d89
BG
168
169/**
170 * atomic64_add - add integer to atomic64 variable
a7e926ab
LB
171 * @i: integer value to add
172 * @v: pointer to type atomic64_t
1a3b1d89 173 *
a7e926ab 174 * Atomically adds @i to @v.
1a3b1d89 175 */
a7e926ab
LB
176static inline long long atomic64_add(long long i, atomic64_t *v)
177{
819165fb
JB
178 __alternative_atomic64(add, add_return,
179 ASM_OUTPUT2("+A" (i), "+c" (v)),
180 ASM_NO_INPUT_CLOBBER("memory"));
a7e926ab
LB
181 return i;
182}
1a3b1d89
BG
183
184/**
185 * atomic64_sub - subtract the atomic64 variable
a7e926ab
LB
186 * @i: integer value to subtract
187 * @v: pointer to type atomic64_t
1a3b1d89 188 *
a7e926ab 189 * Atomically subtracts @i from @v.
1a3b1d89 190 */
a7e926ab
LB
191static inline long long atomic64_sub(long long i, atomic64_t *v)
192{
819165fb
JB
193 __alternative_atomic64(sub, sub_return,
194 ASM_OUTPUT2("+A" (i), "+c" (v)),
195 ASM_NO_INPUT_CLOBBER("memory"));
a7e926ab
LB
196 return i;
197}
1a3b1d89
BG
198
199/**
200 * atomic64_sub_and_test - subtract value from variable and test result
a7e926ab
LB
201 * @i: integer value to subtract
202 * @v: pointer to type atomic64_t
1f045978 203 *
a7e926ab 204 * Atomically subtracts @i from @v and returns
1a3b1d89
BG
205 * true if the result is zero, or false for all
206 * other cases.
207 */
a7e926ab
LB
208static inline int atomic64_sub_and_test(long long i, atomic64_t *v)
209{
210 return atomic64_sub_return(i, v) == 0;
211}
1a3b1d89
BG
212
213/**
214 * atomic64_inc - increment atomic64 variable
a7e926ab 215 * @v: pointer to type atomic64_t
1a3b1d89 216 *
a7e926ab 217 * Atomically increments @v by 1.
1a3b1d89 218 */
a7e926ab
LB
219static inline void atomic64_inc(atomic64_t *v)
220{
819165fb
JB
221 __alternative_atomic64(inc, inc_return, /* no output */,
222 "S" (v) : "memory", "eax", "ecx", "edx");
a7e926ab 223}
1a3b1d89
BG
224
225/**
226 * atomic64_dec - decrement atomic64 variable
1f045978 227 * @v: pointer to type atomic64_t
1a3b1d89 228 *
1f045978 229 * Atomically decrements @v by 1.
1a3b1d89 230 */
a7e926ab
LB
231static inline void atomic64_dec(atomic64_t *v)
232{
819165fb
JB
233 __alternative_atomic64(dec, dec_return, /* no output */,
234 "S" (v) : "memory", "eax", "ecx", "edx");
a7e926ab 235}
1a3b1d89
BG
236
237/**
238 * atomic64_dec_and_test - decrement and test
a7e926ab 239 * @v: pointer to type atomic64_t
1a3b1d89 240 *
a7e926ab 241 * Atomically decrements @v by 1 and
1a3b1d89
BG
242 * returns true if the result is 0, or false for all other
243 * cases.
244 */
a7e926ab
LB
245static inline int atomic64_dec_and_test(atomic64_t *v)
246{
247 return atomic64_dec_return(v) == 0;
248}
1a3b1d89
BG
249
250/**
251 * atomic64_inc_and_test - increment and test
a7e926ab 252 * @v: pointer to type atomic64_t
1a3b1d89 253 *
a7e926ab 254 * Atomically increments @v by 1
1a3b1d89
BG
255 * and returns true if the result is zero, or false for all
256 * other cases.
257 */
a7e926ab
LB
258static inline int atomic64_inc_and_test(atomic64_t *v)
259{
260 return atomic64_inc_return(v) == 0;
261}
1a3b1d89
BG
262
263/**
264 * atomic64_add_negative - add and test if negative
a7e926ab
LB
265 * @i: integer value to add
266 * @v: pointer to type atomic64_t
1a3b1d89 267 *
a7e926ab 268 * Atomically adds @i to @v and returns true
1a3b1d89
BG
269 * if the result is negative, or false when
270 * result is greater than or equal to zero.
271 */
a7e926ab
LB
272static inline int atomic64_add_negative(long long i, atomic64_t *v)
273{
274 return atomic64_add_return(i, v) < 0;
275}
276
277/**
278 * atomic64_add_unless - add unless the number is a given value
279 * @v: pointer of type atomic64_t
280 * @a: the amount to add to v...
281 * @u: ...unless v is equal to u.
282 *
283 * Atomically adds @a to @v, so long as it was not @u.
819165fb 284 * Returns non-zero if the add was done, zero otherwise.
a7e926ab
LB
285 */
286static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
287{
288 unsigned low = (unsigned)u;
289 unsigned high = (unsigned)(u >> 32);
819165fb 290 alternative_atomic64(add_unless,
cb8095bb
JB
291 ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
292 "S" (v) : "memory");
a7e926ab
LB
293 return (int)a;
294}
295
296
297static inline int atomic64_inc_not_zero(atomic64_t *v)
298{
299 int r;
819165fb
JB
300 alternative_atomic64(inc_not_zero, "=&a" (r),
301 "S" (v) : "ecx", "edx", "memory");
a7e926ab
LB
302 return r;
303}
304
305static inline long long atomic64_dec_if_positive(atomic64_t *v)
306{
307 long long r;
819165fb
JB
308 alternative_atomic64(dec_if_positive, "=&A" (r),
309 "S" (v) : "ecx", "memory");
a7e926ab
LB
310 return r;
311}
312
819165fb
JB
313#undef alternative_atomic64
314#undef __alternative_atomic64
1a3b1d89 315
ba1c9f83
DV
316static inline void atomic64_and(long long i, atomic64_t *v)
317{
318 long long old, c = 0;
319
320 while ((old = atomic64_cmpxchg(v, c, c & i)) != c)
321 c = old;
7fc1845d
PZ
322}
323
ba1c9f83
DV
324static inline long long atomic64_fetch_and(long long i, atomic64_t *v)
325{
326 long long old, c = 0;
327
328 while ((old = atomic64_cmpxchg(v, c, c & i)) != c)
329 c = old;
330
331 return old;
a8bcccab
PZ
332}
333
ba1c9f83
DV
334static inline void atomic64_or(long long i, atomic64_t *v)
335{
336 long long old, c = 0;
a8bcccab 337
ba1c9f83
DV
338 while ((old = atomic64_cmpxchg(v, c, c | i)) != c)
339 c = old;
340}
341
342static inline long long atomic64_fetch_or(long long i, atomic64_t *v)
343{
344 long long old, c = 0;
345
346 while ((old = atomic64_cmpxchg(v, c, c | i)) != c)
347 c = old;
348
349 return old;
350}
a8bcccab 351
ba1c9f83
DV
352static inline void atomic64_xor(long long i, atomic64_t *v)
353{
354 long long old, c = 0;
355
356 while ((old = atomic64_cmpxchg(v, c, c ^ i)) != c)
357 c = old;
358}
a8bcccab 359
ba1c9f83
DV
360static inline long long atomic64_fetch_xor(long long i, atomic64_t *v)
361{
362 long long old, c = 0;
363
364 while ((old = atomic64_cmpxchg(v, c, c ^ i)) != c)
365 c = old;
366
367 return old;
368}
7fc1845d 369
ba1c9f83
DV
370static inline long long atomic64_fetch_add(long long i, atomic64_t *v)
371{
372 long long old, c = 0;
373
374 while ((old = atomic64_cmpxchg(v, c, c + i)) != c)
375 c = old;
376
377 return old;
378}
379
380#define atomic64_fetch_sub(i, v) atomic64_fetch_add(-(i), (v))
7fc1845d 381
1a3b1d89 382#endif /* _ASM_X86_ATOMIC64_32_H */