Merge tag 'devicetree-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/robh...
[linux-2.6-block.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 {
79c53a83 12 s64 __aligned(8) counter;
1a3b1d89
BG
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/**
8bf705d1 65 * arch_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
79c53a83 74static inline s64 arch_atomic64_cmpxchg(atomic64_t *v, s64 o, s64 n)
a7e926ab 75{
8bf705d1 76 return arch_cmpxchg64(&v->counter, o, n);
a7e926ab 77}
1a3b1d89
BG
78
79/**
8bf705d1 80 * arch_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 */
79c53a83 87static inline s64 arch_atomic64_xchg(atomic64_t *v, s64 n)
a7e926ab 88{
79c53a83 89 s64 o;
a7e926ab
LB
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/**
8bf705d1 99 * arch_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 */
79c53a83 105static inline void arch_atomic64_set(atomic64_t *v, s64 i)
a7e926ab
LB
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/**
8bf705d1 115 * arch_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 */
79c53a83 120static inline s64 arch_atomic64_read(const atomic64_t *v)
1a3b1d89 121{
79c53a83 122 s64 r;
819165fb 123 alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
a7e926ab 124 return r;
447a5647 125}
1a3b1d89
BG
126
127/**
8bf705d1 128 * arch_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 */
79c53a83 134static inline s64 arch_atomic64_add_return(s64 i, atomic64_t *v)
a7e926ab 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 */
79c53a83 145static inline s64 arch_atomic64_sub_return(s64 i, atomic64_t *v)
a7e926ab 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
79c53a83 153static inline s64 arch_atomic64_inc_return(atomic64_t *v)
a7e926ab 154{
79c53a83 155 s64 a;
819165fb
JB
156 alternative_atomic64(inc_return, "=&A" (a),
157 "S" (v) : "memory", "ecx");
a7e926ab
LB
158 return a;
159}
9837559d 160#define arch_atomic64_inc_return arch_atomic64_inc_return
a7e926ab 161
79c53a83 162static inline s64 arch_atomic64_dec_return(atomic64_t *v)
a7e926ab 163{
79c53a83 164 s64 a;
819165fb
JB
165 alternative_atomic64(dec_return, "=&A" (a),
166 "S" (v) : "memory", "ecx");
a7e926ab
LB
167 return a;
168}
9837559d 169#define arch_atomic64_dec_return arch_atomic64_dec_return
1a3b1d89
BG
170
171/**
8bf705d1 172 * arch_atomic64_add - add integer to atomic64 variable
a7e926ab
LB
173 * @i: integer value to add
174 * @v: pointer to type atomic64_t
1a3b1d89 175 *
a7e926ab 176 * Atomically adds @i to @v.
1a3b1d89 177 */
79c53a83 178static inline s64 arch_atomic64_add(s64 i, atomic64_t *v)
a7e926ab 179{
819165fb
JB
180 __alternative_atomic64(add, add_return,
181 ASM_OUTPUT2("+A" (i), "+c" (v)),
182 ASM_NO_INPUT_CLOBBER("memory"));
a7e926ab
LB
183 return i;
184}
1a3b1d89
BG
185
186/**
8bf705d1 187 * arch_atomic64_sub - subtract the atomic64 variable
a7e926ab
LB
188 * @i: integer value to subtract
189 * @v: pointer to type atomic64_t
1a3b1d89 190 *
a7e926ab 191 * Atomically subtracts @i from @v.
1a3b1d89 192 */
79c53a83 193static inline s64 arch_atomic64_sub(s64 i, atomic64_t *v)
a7e926ab 194{
819165fb
JB
195 __alternative_atomic64(sub, sub_return,
196 ASM_OUTPUT2("+A" (i), "+c" (v)),
197 ASM_NO_INPUT_CLOBBER("memory"));
a7e926ab
LB
198 return i;
199}
1a3b1d89 200
1a3b1d89 201/**
8bf705d1 202 * arch_atomic64_inc - increment atomic64 variable
a7e926ab 203 * @v: pointer to type atomic64_t
1a3b1d89 204 *
a7e926ab 205 * Atomically increments @v by 1.
1a3b1d89 206 */
8bf705d1 207static inline void arch_atomic64_inc(atomic64_t *v)
a7e926ab 208{
819165fb
JB
209 __alternative_atomic64(inc, inc_return, /* no output */,
210 "S" (v) : "memory", "eax", "ecx", "edx");
a7e926ab 211}
4331f4d5 212#define arch_atomic64_inc arch_atomic64_inc
1a3b1d89
BG
213
214/**
8bf705d1 215 * arch_atomic64_dec - decrement atomic64 variable
1f045978 216 * @v: pointer to type atomic64_t
1a3b1d89 217 *
1f045978 218 * Atomically decrements @v by 1.
1a3b1d89 219 */
8bf705d1 220static inline void arch_atomic64_dec(atomic64_t *v)
a7e926ab 221{
819165fb
JB
222 __alternative_atomic64(dec, dec_return, /* no output */,
223 "S" (v) : "memory", "eax", "ecx", "edx");
a7e926ab 224}
4331f4d5 225#define arch_atomic64_dec arch_atomic64_dec
1a3b1d89 226
a7e926ab 227/**
8bf705d1 228 * arch_atomic64_add_unless - add unless the number is a given value
a7e926ab
LB
229 * @v: pointer of type atomic64_t
230 * @a: the amount to add to v...
231 * @u: ...unless v is equal to u.
232 *
233 * Atomically adds @a to @v, so long as it was not @u.
819165fb 234 * Returns non-zero if the add was done, zero otherwise.
a7e926ab 235 */
79c53a83 236static inline int arch_atomic64_add_unless(atomic64_t *v, s64 a, s64 u)
a7e926ab
LB
237{
238 unsigned low = (unsigned)u;
239 unsigned high = (unsigned)(u >> 32);
819165fb 240 alternative_atomic64(add_unless,
cb8095bb
JB
241 ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
242 "S" (v) : "memory");
a7e926ab
LB
243 return (int)a;
244}
245
8bf705d1 246static inline int arch_atomic64_inc_not_zero(atomic64_t *v)
a7e926ab
LB
247{
248 int r;
819165fb
JB
249 alternative_atomic64(inc_not_zero, "=&a" (r),
250 "S" (v) : "ecx", "edx", "memory");
a7e926ab
LB
251 return r;
252}
4331f4d5 253#define arch_atomic64_inc_not_zero arch_atomic64_inc_not_zero
a7e926ab 254
79c53a83 255static inline s64 arch_atomic64_dec_if_positive(atomic64_t *v)
a7e926ab 256{
79c53a83 257 s64 r;
819165fb
JB
258 alternative_atomic64(dec_if_positive, "=&A" (r),
259 "S" (v) : "ecx", "memory");
a7e926ab
LB
260 return r;
261}
4331f4d5 262#define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
a7e926ab 263
819165fb
JB
264#undef alternative_atomic64
265#undef __alternative_atomic64
1a3b1d89 266
79c53a83 267static inline void arch_atomic64_and(s64 i, atomic64_t *v)
ba1c9f83 268{
79c53a83 269 s64 old, c = 0;
ba1c9f83 270
8bf705d1 271 while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
ba1c9f83 272 c = old;
7fc1845d
PZ
273}
274
79c53a83 275static inline s64 arch_atomic64_fetch_and(s64 i, atomic64_t *v)
ba1c9f83 276{
79c53a83 277 s64 old, c = 0;
ba1c9f83 278
8bf705d1 279 while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
ba1c9f83
DV
280 c = old;
281
282 return old;
a8bcccab
PZ
283}
284
79c53a83 285static inline void arch_atomic64_or(s64 i, atomic64_t *v)
ba1c9f83 286{
79c53a83 287 s64 old, c = 0;
a8bcccab 288
8bf705d1 289 while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
ba1c9f83
DV
290 c = old;
291}
292
79c53a83 293static inline s64 arch_atomic64_fetch_or(s64 i, atomic64_t *v)
ba1c9f83 294{
79c53a83 295 s64 old, c = 0;
ba1c9f83 296
8bf705d1 297 while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
ba1c9f83
DV
298 c = old;
299
300 return old;
301}
a8bcccab 302
79c53a83 303static inline void arch_atomic64_xor(s64 i, atomic64_t *v)
ba1c9f83 304{
79c53a83 305 s64 old, c = 0;
ba1c9f83 306
8bf705d1 307 while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
ba1c9f83
DV
308 c = old;
309}
a8bcccab 310
79c53a83 311static inline s64 arch_atomic64_fetch_xor(s64 i, atomic64_t *v)
ba1c9f83 312{
79c53a83 313 s64 old, c = 0;
ba1c9f83 314
8bf705d1 315 while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
ba1c9f83
DV
316 c = old;
317
318 return old;
319}
7fc1845d 320
79c53a83 321static inline s64 arch_atomic64_fetch_add(s64 i, atomic64_t *v)
ba1c9f83 322{
79c53a83 323 s64 old, c = 0;
ba1c9f83 324
8bf705d1 325 while ((old = arch_atomic64_cmpxchg(v, c, c + i)) != c)
ba1c9f83
DV
326 c = old;
327
328 return old;
329}
330
8bf705d1 331#define arch_atomic64_fetch_sub(i, v) arch_atomic64_fetch_add(-(i), (v))
7fc1845d 332
1a3b1d89 333#endif /* _ASM_X86_ATOMIC64_32_H */