blob: 12fb57413732e78c39437b469ba86be04a3ab449 [file] [log] [blame]
Brian Gerst1a3b1d82010-01-07 11:53:33 -05001#ifndef _ASM_X86_ATOMIC64_64_H
2#define _ASM_X86_ATOMIC64_64_H
3
4#include <linux/types.h>
5#include <asm/alternative.h>
6#include <asm/cmpxchg.h>
7
8/* The 64-bit atomic type */
9
10#define ATOMIC64_INIT(i) { (i) }
11
12/**
13 * atomic64_read - read atomic64 variable
14 * @v: pointer of type atomic64_t
15 *
16 * Atomically reads the value of @v.
17 * Doesn't imply a read memory barrier.
18 */
19static inline long atomic64_read(const atomic64_t *v)
20{
Peter Zijlstra62e8a322015-09-18 11:13:10 +020021 return READ_ONCE((v)->counter);
Brian Gerst1a3b1d82010-01-07 11:53:33 -050022}
23
24/**
25 * atomic64_set - set atomic64 variable
26 * @v: pointer to type atomic64_t
27 * @i: required value
28 *
29 * Atomically sets the value of @v to @i.
30 */
31static inline void atomic64_set(atomic64_t *v, long i)
32{
Peter Zijlstra62e8a322015-09-18 11:13:10 +020033 WRITE_ONCE(v->counter, i);
Brian Gerst1a3b1d82010-01-07 11:53:33 -050034}
35
36/**
37 * atomic64_add - add integer to atomic64 variable
38 * @i: integer value to add
39 * @v: pointer to type atomic64_t
40 *
41 * Atomically adds @i to @v.
42 */
Hagen Paul Pfeifer3462bd22015-04-20 23:27:11 +020043static __always_inline void atomic64_add(long i, atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -050044{
45 asm volatile(LOCK_PREFIX "addq %1,%0"
46 : "=m" (v->counter)
47 : "er" (i), "m" (v->counter));
48}
49
50/**
51 * atomic64_sub - subtract the atomic64 variable
52 * @i: integer value to subtract
53 * @v: pointer to type atomic64_t
54 *
55 * Atomically subtracts @i from @v.
56 */
57static inline void atomic64_sub(long i, atomic64_t *v)
58{
59 asm volatile(LOCK_PREFIX "subq %1,%0"
60 : "=m" (v->counter)
61 : "er" (i), "m" (v->counter));
62}
63
64/**
65 * atomic64_sub_and_test - subtract value from variable and test result
66 * @i: integer value to subtract
67 * @v: pointer to type atomic64_t
68 *
69 * Atomically subtracts @i from @v and returns
70 * true if the result is zero, or false for all
71 * other cases.
72 */
H. Peter Anvin117780e2016-06-08 12:38:38 -070073static inline bool atomic64_sub_and_test(long i, atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -050074{
H. Peter Anvin18fe5822016-06-08 12:38:39 -070075 GEN_BINARY_RMWcc(LOCK_PREFIX "subq", v->counter, "er", i, "%0", e);
Brian Gerst1a3b1d82010-01-07 11:53:33 -050076}
77
78/**
79 * atomic64_inc - increment atomic64 variable
80 * @v: pointer to type atomic64_t
81 *
82 * Atomically increments @v by 1.
83 */
Hagen Paul Pfeifer3462bd22015-04-20 23:27:11 +020084static __always_inline void atomic64_inc(atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -050085{
86 asm volatile(LOCK_PREFIX "incq %0"
87 : "=m" (v->counter)
88 : "m" (v->counter));
89}
90
91/**
92 * atomic64_dec - decrement atomic64 variable
93 * @v: pointer to type atomic64_t
94 *
95 * Atomically decrements @v by 1.
96 */
Hagen Paul Pfeifer3462bd22015-04-20 23:27:11 +020097static __always_inline void atomic64_dec(atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -050098{
99 asm volatile(LOCK_PREFIX "decq %0"
100 : "=m" (v->counter)
101 : "m" (v->counter));
102}
103
104/**
105 * atomic64_dec_and_test - decrement and test
106 * @v: pointer to type atomic64_t
107 *
108 * Atomically decrements @v by 1 and
109 * returns true if the result is 0, or false for all other
110 * cases.
111 */
H. Peter Anvin117780e2016-06-08 12:38:38 -0700112static inline bool atomic64_dec_and_test(atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500113{
H. Peter Anvin18fe5822016-06-08 12:38:39 -0700114 GEN_UNARY_RMWcc(LOCK_PREFIX "decq", v->counter, "%0", e);
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500115}
116
117/**
118 * atomic64_inc_and_test - increment and test
119 * @v: pointer to type atomic64_t
120 *
121 * Atomically increments @v by 1
122 * and returns true if the result is zero, or false for all
123 * other cases.
124 */
H. Peter Anvin117780e2016-06-08 12:38:38 -0700125static inline bool atomic64_inc_and_test(atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500126{
H. Peter Anvin18fe5822016-06-08 12:38:39 -0700127 GEN_UNARY_RMWcc(LOCK_PREFIX "incq", v->counter, "%0", e);
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500128}
129
130/**
131 * atomic64_add_negative - add and test if negative
132 * @i: integer value to add
133 * @v: pointer to type atomic64_t
134 *
135 * Atomically adds @i to @v and returns true
136 * if the result is negative, or false when
137 * result is greater than or equal to zero.
138 */
H. Peter Anvin117780e2016-06-08 12:38:38 -0700139static inline bool atomic64_add_negative(long i, atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500140{
H. Peter Anvin18fe5822016-06-08 12:38:39 -0700141 GEN_BINARY_RMWcc(LOCK_PREFIX "addq", v->counter, "er", i, "%0", s);
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500142}
143
144/**
145 * atomic64_add_return - add and return
146 * @i: integer value to add
147 * @v: pointer to type atomic64_t
148 *
149 * Atomically adds @i to @v and returns @i + @v
150 */
Hagen Paul Pfeifer3462bd22015-04-20 23:27:11 +0200151static __always_inline long atomic64_add_return(long i, atomic64_t *v)
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500152{
Jeremy Fitzhardinge8b8bc2f2011-08-23 16:59:58 -0700153 return i + xadd(&v->counter, i);
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500154}
155
156static inline long atomic64_sub_return(long i, atomic64_t *v)
157{
158 return atomic64_add_return(-i, v);
159}
160
Peter Zijlstraa8bccca2016-04-18 01:16:03 +0200161static inline long atomic64_fetch_add(long i, atomic64_t *v)
162{
163 return xadd(&v->counter, i);
164}
165
166static inline long atomic64_fetch_sub(long i, atomic64_t *v)
167{
168 return xadd(&v->counter, -i);
169}
170
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500171#define atomic64_inc_return(v) (atomic64_add_return(1, (v)))
172#define atomic64_dec_return(v) (atomic64_sub_return(1, (v)))
173
174static inline long atomic64_cmpxchg(atomic64_t *v, long old, long new)
175{
176 return cmpxchg(&v->counter, old, new);
177}
178
Peter Zijlstraa9ebf302017-02-01 16:39:38 +0100179#define atomic64_try_cmpxchg atomic64_try_cmpxchg
180static __always_inline bool atomic64_try_cmpxchg(atomic64_t *v, long *old, long new)
181{
182 return try_cmpxchg(&v->counter, old, new);
183}
184
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500185static inline long atomic64_xchg(atomic64_t *v, long new)
186{
187 return xchg(&v->counter, new);
188}
189
190/**
191 * atomic64_add_unless - add unless the number is a given value
192 * @v: pointer of type atomic64_t
193 * @a: the amount to add to v...
194 * @u: ...unless v is equal to u.
195 *
196 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700197 * Returns the old value of @v.
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500198 */
H. Peter Anvin117780e2016-06-08 12:38:38 -0700199static inline bool atomic64_add_unless(atomic64_t *v, long a, long u)
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500200{
201 long c, old;
202 c = atomic64_read(v);
203 for (;;) {
204 if (unlikely(c == (u)))
205 break;
206 old = atomic64_cmpxchg((v), c, c + (a));
207 if (likely(old == c))
208 break;
209 c = old;
210 }
211 return c != (u);
212}
213
214#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
215
Luca Barbierid7f6de12010-02-26 12:22:41 +0100216/*
217 * atomic64_dec_if_positive - decrement by 1 if old value positive
218 * @v: pointer of type atomic_t
219 *
220 * The function returns the old value of *v minus 1, even if
221 * the atomic variable, v, was not decremented.
222 */
223static inline long atomic64_dec_if_positive(atomic64_t *v)
224{
225 long c, old, dec;
226 c = atomic64_read(v);
227 for (;;) {
228 dec = c - 1;
229 if (unlikely(dec < 0))
230 break;
231 old = atomic64_cmpxchg((v), c, dec);
232 if (likely(old == c))
233 break;
234 c = old;
235 }
236 return dec;
237}
238
Peter Zijlstra7fc18452014-04-23 20:28:37 +0200239#define ATOMIC64_OP(op) \
240static inline void atomic64_##op(long i, atomic64_t *v) \
241{ \
242 asm volatile(LOCK_PREFIX #op"q %1,%0" \
243 : "+m" (v->counter) \
244 : "er" (i) \
245 : "memory"); \
246}
247
Peter Zijlstraa8bccca2016-04-18 01:16:03 +0200248#define ATOMIC64_FETCH_OP(op, c_op) \
249static inline long atomic64_fetch_##op(long i, atomic64_t *v) \
250{ \
251 long old, val = atomic64_read(v); \
252 for (;;) { \
253 old = atomic64_cmpxchg(v, val, val c_op i); \
254 if (old == val) \
255 break; \
256 val = old; \
257 } \
258 return old; \
259}
Peter Zijlstra7fc18452014-04-23 20:28:37 +0200260
Peter Zijlstraa8bccca2016-04-18 01:16:03 +0200261#define ATOMIC64_OPS(op, c_op) \
262 ATOMIC64_OP(op) \
263 ATOMIC64_FETCH_OP(op, c_op)
264
265ATOMIC64_OPS(and, &)
266ATOMIC64_OPS(or, |)
267ATOMIC64_OPS(xor, ^)
268
269#undef ATOMIC64_OPS
270#undef ATOMIC64_FETCH_OP
Peter Zijlstra7fc18452014-04-23 20:28:37 +0200271#undef ATOMIC64_OP
272
Brian Gerst1a3b1d82010-01-07 11:53:33 -0500273#endif /* _ASM_X86_ATOMIC64_64_H */