1 #ifndef _ASM_X86_ATOMIC64_32_H
2 #define _ASM_X86_ATOMIC64_32_H
4 #include <linux/compiler.h>
5 #include <linux/types.h>
6 //#include <asm/cmpxchg.h>
8 /* An 64bit atomic type */
11 u64 __aligned(8) counter;
14 #define ATOMIC64_INIT(val) { (val) }
16 #define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
17 #ifndef ATOMIC64_EXPORT
18 #define ATOMIC64_DECL_ONE __ATOMIC64_DECL
20 #define ATOMIC64_DECL_ONE(sym) __ATOMIC64_DECL(sym); \
21 ATOMIC64_EXPORT(atomic64_##sym)
24 #ifdef CONFIG_X86_CMPXCHG64
25 #define __alternative_atomic64(f, g, out, in...) \
26 asm volatile("call %P[func]" \
27 : out : [func] "i" (atomic64_##g##_cx8), ## in)
29 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8)
31 #define __alternative_atomic64(f, g, out, in...) \
32 alternative_call(atomic64_##f##_386, atomic64_##g##_cx8, \
33 X86_FEATURE_CX8, ASM_OUTPUT2(out), ## in)
35 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8); \
36 ATOMIC64_DECL_ONE(sym##_386)
38 ATOMIC64_DECL_ONE(add_386);
39 ATOMIC64_DECL_ONE(sub_386);
40 ATOMIC64_DECL_ONE(inc_386);
41 ATOMIC64_DECL_ONE(dec_386);
44 #define alternative_atomic64(f, out, in...) \
45 __alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)
50 ATOMIC64_DECL(add_return);
51 ATOMIC64_DECL(sub_return);
52 ATOMIC64_DECL(inc_return);
53 ATOMIC64_DECL(dec_return);
54 ATOMIC64_DECL(dec_if_positive);
55 ATOMIC64_DECL(inc_not_zero);
56 ATOMIC64_DECL(add_unless);
59 #undef ATOMIC64_DECL_ONE
60 #undef __ATOMIC64_DECL
61 #undef ATOMIC64_EXPORT
64 * atomic64_cmpxchg - cmpxchg atomic64 variable
65 * @v: pointer to type atomic64_t
69 * Atomically sets @v to @n if it was equal to @o and returns
73 static inline long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
75 return cmpxchg64(&v->counter, o, n);
79 * atomic64_xchg - xchg atomic64 variable
80 * @v: pointer to type atomic64_t
83 * Atomically xchgs the value of @v to @n and returns
86 static inline long long atomic64_xchg(atomic64_t *v, long long n)
89 unsigned high = (unsigned)(n >> 32);
90 unsigned low = (unsigned)n;
91 alternative_atomic64(xchg, "=&A" (o),
92 "S" (v), "b" (low), "c" (high)
98 * atomic64_set - set atomic64 variable
99 * @v: pointer to type atomic64_t
100 * @i: value to assign
102 * Atomically sets the value of @v to @n.
104 static inline void atomic64_set(atomic64_t *v, long long i)
106 unsigned high = (unsigned)(i >> 32);
107 unsigned low = (unsigned)i;
108 alternative_atomic64(set, /* no output */,
109 "S" (v), "b" (low), "c" (high)
110 : "eax", "edx", "memory");
114 * atomic64_read - read atomic64 variable
115 * @v: pointer to type atomic64_t
117 * Atomically reads the value of @v and returns it.
119 static inline long long atomic64_read(const atomic64_t *v)
122 alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
127 * atomic64_add_return - add and return
128 * @i: integer value to add
129 * @v: pointer to type atomic64_t
131 * Atomically adds @i to @v and returns @i + *@v
133 static inline long long atomic64_add_return(long long i, atomic64_t *v)
135 alternative_atomic64(add_return,
136 ASM_OUTPUT2("+A" (i), "+c" (v)),
137 ASM_NO_INPUT_CLOBBER("memory"));
142 * Other variants with different arithmetic operators:
144 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
146 alternative_atomic64(sub_return,
147 ASM_OUTPUT2("+A" (i), "+c" (v)),
148 ASM_NO_INPUT_CLOBBER("memory"));
152 static inline long long atomic64_inc_return(atomic64_t *v)
155 alternative_atomic64(inc_return, "=&A" (a),
156 "S" (v) : "memory", "ecx");
160 static inline long long atomic64_dec_return(atomic64_t *v)
163 alternative_atomic64(dec_return, "=&A" (a),
164 "S" (v) : "memory", "ecx");
169 * atomic64_add - add integer to atomic64 variable
170 * @i: integer value to add
171 * @v: pointer to type atomic64_t
173 * Atomically adds @i to @v.
175 static inline long long atomic64_add(long long i, atomic64_t *v)
177 __alternative_atomic64(add, add_return,
178 ASM_OUTPUT2("+A" (i), "+c" (v)),
179 ASM_NO_INPUT_CLOBBER("memory"));
184 * atomic64_sub - subtract the atomic64 variable
185 * @i: integer value to subtract
186 * @v: pointer to type atomic64_t
188 * Atomically subtracts @i from @v.
190 static inline long long atomic64_sub(long long i, atomic64_t *v)
192 __alternative_atomic64(sub, sub_return,
193 ASM_OUTPUT2("+A" (i), "+c" (v)),
194 ASM_NO_INPUT_CLOBBER("memory"));
199 * atomic64_sub_and_test - subtract value from variable and test result
200 * @i: integer value to subtract
201 * @v: pointer to type atomic64_t
203 * Atomically subtracts @i from @v and returns
204 * true if the result is zero, or false for all
207 static inline int atomic64_sub_and_test(long long i, atomic64_t *v)
209 return atomic64_sub_return(i, v) == 0;
213 * atomic64_inc - increment atomic64 variable
214 * @v: pointer to type atomic64_t
216 * Atomically increments @v by 1.
218 static inline void atomic64_inc(atomic64_t *v)
220 __alternative_atomic64(inc, inc_return, /* no output */,
221 "S" (v) : "memory", "eax", "ecx", "edx");
225 * atomic64_dec - decrement atomic64 variable
226 * @v: pointer to type atomic64_t
228 * Atomically decrements @v by 1.
230 static inline void atomic64_dec(atomic64_t *v)
232 __alternative_atomic64(dec, dec_return, /* no output */,
233 "S" (v) : "memory", "eax", "ecx", "edx");
237 * atomic64_dec_and_test - decrement and test
238 * @v: pointer to type atomic64_t
240 * Atomically decrements @v by 1 and
241 * returns true if the result is 0, or false for all other
244 static inline int atomic64_dec_and_test(atomic64_t *v)
246 return atomic64_dec_return(v) == 0;
250 * atomic64_inc_and_test - increment and test
251 * @v: pointer to type atomic64_t
253 * Atomically increments @v by 1
254 * and returns true if the result is zero, or false for all
257 static inline int atomic64_inc_and_test(atomic64_t *v)
259 return atomic64_inc_return(v) == 0;
263 * atomic64_add_negative - add and test if negative
264 * @i: integer value to add
265 * @v: pointer to type atomic64_t
267 * Atomically adds @i to @v and returns true
268 * if the result is negative, or false when
269 * result is greater than or equal to zero.
271 static inline int atomic64_add_negative(long long i, atomic64_t *v)
273 return atomic64_add_return(i, v) < 0;
277 * atomic64_add_unless - add unless the number is a given value
278 * @v: pointer of type atomic64_t
279 * @a: the amount to add to v...
280 * @u: ...unless v is equal to u.
282 * Atomically adds @a to @v, so long as it was not @u.
283 * Returns non-zero if the add was done, zero otherwise.
285 static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
287 unsigned low = (unsigned)u;
288 unsigned high = (unsigned)(u >> 32);
289 alternative_atomic64(add_unless,
290 ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
296 static inline int atomic64_inc_not_zero(atomic64_t *v)
299 alternative_atomic64(inc_not_zero, "=&a" (r),
300 "S" (v) : "ecx", "edx", "memory");
304 static inline long long atomic64_dec_if_positive(atomic64_t *v)
307 alternative_atomic64(dec_if_positive, "=&A" (r),
308 "S" (v) : "ecx", "memory");
312 #undef alternative_atomic64
313 #undef __alternative_atomic64
315 #define ATOMIC64_OP(op, c_op) \
316 static inline void atomic64_##op(long long i, atomic64_t *v) \
318 long long old, c = 0; \
319 while ((old = atomic64_cmpxchg(v, c, c c_op i)) != c) \
323 #define ATOMIC64_FETCH_OP(op, c_op) \
324 static inline long long atomic64_fetch_##op(long long i, atomic64_t *v) \
326 long long old, c = 0; \
327 while ((old = atomic64_cmpxchg(v, c, c c_op i)) != c) \
332 ATOMIC64_FETCH_OP(add, +)
334 #define atomic64_fetch_sub(i, v) atomic64_fetch_add(-(i), (v))
336 #define ATOMIC64_OPS(op, c_op) \
337 ATOMIC64_OP(op, c_op) \
338 ATOMIC64_FETCH_OP(op, c_op)
345 #undef ATOMIC64_FETCH_OP
348 #endif /* _ASM_X86_ATOMIC64_32_H */