blob: 85b46fba4229cc0334e05d5d9c5e66deff3fa69f [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_ATOMIC_32_H
2#define _ASM_X86_ATOMIC_32_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/compiler.h>
Matthew Wilcoxea4354672009-01-06 14:40:39 -08005#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <asm/processor.h>
Jeff Dikea436ed92007-05-08 00:35:02 -07007#include <asm/cmpxchg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9/*
10 * Atomic operations that C can't guarantee us. Useful for
11 * resource counting etc..
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define ATOMIC_INIT(i) { (i) }
15
16/**
17 * atomic_read - read atomic variable
18 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -070019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Atomically reads the value of @v.
Joe Perches78ff12e2008-03-23 01:01:41 -070021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define atomic_read(v) ((v)->counter)
23
24/**
25 * atomic_set - set atomic variable
26 * @v: pointer of type atomic_t
27 * @i: required value
Joe Perches78ff12e2008-03-23 01:01:41 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * Atomically sets the value of @v to @i.
Joe Perches78ff12e2008-03-23 01:01:41 -070030 */
31#define atomic_set(v, i) (((v)->counter) = (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/**
34 * atomic_add - add integer to atomic variable
35 * @i: integer value to add
36 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Atomically adds @i to @v.
39 */
Joe Perches78ff12e2008-03-23 01:01:41 -070040static inline void atomic_add(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Joe Perches78ff12e2008-03-23 01:01:41 -070042 asm volatile(LOCK_PREFIX "addl %1,%0"
43 : "+m" (v->counter)
44 : "ir" (i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47/**
Robert P. J. Daycc386822007-05-08 00:35:08 -070048 * atomic_sub - subtract integer from atomic variable
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * @i: integer value to subtract
50 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -070051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Atomically subtracts @i from @v.
53 */
Joe Perches78ff12e2008-03-23 01:01:41 -070054static inline void atomic_sub(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Joe Perches78ff12e2008-03-23 01:01:41 -070056 asm volatile(LOCK_PREFIX "subl %1,%0"
57 : "+m" (v->counter)
58 : "ir" (i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61/**
62 * atomic_sub_and_test - subtract value from variable and test result
63 * @i: integer value to subtract
64 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -070065 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * Atomically subtracts @i from @v and returns
67 * true if the result is zero, or false for all
68 * other cases.
69 */
Joe Perches78ff12e2008-03-23 01:01:41 -070070static inline int atomic_sub_and_test(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 unsigned char c;
73
Joe Perches78ff12e2008-03-23 01:01:41 -070074 asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
75 : "+m" (v->counter), "=qm" (c)
76 : "ir" (i) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return c;
78}
79
80/**
81 * atomic_inc - increment atomic variable
82 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -070083 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Atomically increments @v by 1.
Joe Perches78ff12e2008-03-23 01:01:41 -070085 */
86static inline void atomic_inc(atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Joe Perches78ff12e2008-03-23 01:01:41 -070088 asm volatile(LOCK_PREFIX "incl %0"
89 : "+m" (v->counter));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/**
93 * atomic_dec - decrement atomic variable
94 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -070095 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * Atomically decrements @v by 1.
Joe Perches78ff12e2008-03-23 01:01:41 -070097 */
98static inline void atomic_dec(atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Joe Perches78ff12e2008-03-23 01:01:41 -0700100 asm volatile(LOCK_PREFIX "decl %0"
101 : "+m" (v->counter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/**
105 * atomic_dec_and_test - decrement and test
106 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -0700107 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * Atomically decrements @v by 1 and
109 * returns true if the result is 0, or false for all other
110 * cases.
Joe Perches78ff12e2008-03-23 01:01:41 -0700111 */
112static inline int atomic_dec_and_test(atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 unsigned char c;
115
Joe Perches78ff12e2008-03-23 01:01:41 -0700116 asm volatile(LOCK_PREFIX "decl %0; sete %1"
117 : "+m" (v->counter), "=qm" (c)
118 : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return c != 0;
120}
121
122/**
Joe Perches78ff12e2008-03-23 01:01:41 -0700123 * atomic_inc_and_test - increment and test
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * @v: pointer of type atomic_t
Joe Perches78ff12e2008-03-23 01:01:41 -0700125 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * Atomically increments @v by 1
127 * and returns true if the result is zero, or false for all
128 * other cases.
Joe Perches78ff12e2008-03-23 01:01:41 -0700129 */
130static inline int atomic_inc_and_test(atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 unsigned char c;
133
Joe Perches78ff12e2008-03-23 01:01:41 -0700134 asm volatile(LOCK_PREFIX "incl %0; sete %1"
135 : "+m" (v->counter), "=qm" (c)
136 : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return c != 0;
138}
139
140/**
141 * atomic_add_negative - add and test if negative
142 * @v: pointer of type atomic_t
143 * @i: integer value to add
Joe Perches78ff12e2008-03-23 01:01:41 -0700144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * Atomically adds @i to @v and returns true
146 * if the result is negative, or false when
147 * result is greater than or equal to zero.
Joe Perches78ff12e2008-03-23 01:01:41 -0700148 */
149static inline int atomic_add_negative(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 unsigned char c;
152
Joe Perches78ff12e2008-03-23 01:01:41 -0700153 asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
154 : "+m" (v->counter), "=qm" (c)
155 : "ir" (i) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return c;
157}
158
159/**
Robert P. J. Daycc386822007-05-08 00:35:08 -0700160 * atomic_add_return - add integer and return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * @v: pointer of type atomic_t
162 * @i: integer value to add
163 *
164 * Atomically adds @i to @v and returns @i + @v
165 */
Joe Perches78ff12e2008-03-23 01:01:41 -0700166static inline int atomic_add_return(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int __i;
169#ifdef CONFIG_M386
lepton1bb858f2006-04-18 22:21:10 -0700170 unsigned long flags;
Joe Perches78ff12e2008-03-23 01:01:41 -0700171 if (unlikely(boot_cpu_data.x86 <= 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto no_xadd;
173#endif
174 /* Modern 486+ processor */
175 __i = i;
Joe Perches78ff12e2008-03-23 01:01:41 -0700176 asm volatile(LOCK_PREFIX "xaddl %0, %1"
177 : "+r" (i), "+m" (v->counter)
178 : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return i + __i;
180
181#ifdef CONFIG_M386
182no_xadd: /* Legacy 386 processor */
lepton1bb858f2006-04-18 22:21:10 -0700183 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 __i = atomic_read(v);
185 atomic_set(v, i + __i);
lepton1bb858f2006-04-18 22:21:10 -0700186 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return i + __i;
188#endif
189}
190
Robert P. J. Daycc386822007-05-08 00:35:08 -0700191/**
192 * atomic_sub_return - subtract integer and return
193 * @v: pointer of type atomic_t
194 * @i: integer value to subtract
195 *
196 * Atomically subtracts @i from @v and returns @v - @i
197 */
Joe Perches78ff12e2008-03-23 01:01:41 -0700198static inline int atomic_sub_return(int i, atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Joe Perches78ff12e2008-03-23 01:01:41 -0700200 return atomic_add_return(-i, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Mathieu Desnoyerse656e242007-05-08 00:34:20 -0700203#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
204#define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
Nick Piggin4a6dae62005-11-13 16:07:24 -0800205
Nick Piggin8426e1f2005-11-13 16:07:25 -0800206/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800207 * atomic_add_unless - add unless the number is already a given value
Nick Piggin8426e1f2005-11-13 16:07:25 -0800208 * @v: pointer of type atomic_t
209 * @a: the amount to add to v...
210 * @u: ...unless v is equal to u.
211 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800212 * Atomically adds @a to @v, so long as @v was not already @u.
Nick Piggin8426e1f2005-11-13 16:07:25 -0800213 * Returns non-zero if @v was not @u, and zero otherwise.
214 */
Joe Perches78ff12e2008-03-23 01:01:41 -0700215static inline int atomic_add_unless(atomic_t *v, int a, int u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700216{
217 int c, old;
218 c = atomic_read(v);
219 for (;;) {
220 if (unlikely(c == (u)))
221 break;
222 old = atomic_cmpxchg((v), c, c + (a));
223 if (likely(old == c))
224 break;
225 c = old;
226 }
227 return c != (u);
228}
229
Nick Piggin8426e1f2005-11-13 16:07:25 -0800230#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
231
Joe Perches78ff12e2008-03-23 01:01:41 -0700232#define atomic_inc_return(v) (atomic_add_return(1, v))
233#define atomic_dec_return(v) (atomic_sub_return(1, v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/* These are x86-specific, used by some header files */
Joe Perches78ff12e2008-03-23 01:01:41 -0700236#define atomic_clear_mask(mask, addr) \
237 asm volatile(LOCK_PREFIX "andl %0,%1" \
238 : : "r" (~(mask)), "m" (*(addr)) : "memory")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Joe Perches78ff12e2008-03-23 01:01:41 -0700240#define atomic_set_mask(mask, addr) \
241 asm volatile(LOCK_PREFIX "orl %0,%1" \
242 : : "r" (mask), "m" (*(addr)) : "memory")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/* Atomic operations are already serializing on x86 */
245#define smp_mb__before_atomic_dec() barrier()
246#define smp_mb__after_atomic_dec() barrier()
247#define smp_mb__before_atomic_inc() barrier()
248#define smp_mb__after_atomic_inc() barrier()
249
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800250#include <asm-generic/atomic.h>
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700251#endif /* _ASM_X86_ATOMIC_32_H */