x86: Sync asm/atomic_32.h and asm/atomic_64.h
[linux-2.6-block.git] / arch / x86 / include / asm / atomic_32.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_ATOMIC_32_H
2#define _ASM_X86_ATOMIC_32_H
1da177e4 3
1da177e4 4#include <linux/compiler.h>
ea435467 5#include <linux/types.h>
1da177e4 6#include <asm/processor.h>
3ce59bb8 7#include <asm/alternative.h>
a436ed9c 8#include <asm/cmpxchg.h>
1da177e4
LT
9
10/*
11 * Atomic operations that C can't guarantee us. Useful for
12 * resource counting etc..
13 */
14
1da177e4
LT
15#define ATOMIC_INIT(i) { (i) }
16
17/**
18 * atomic_read - read atomic variable
19 * @v: pointer of type atomic_t
78ff12ee 20 *
1da177e4 21 * Atomically reads the value of @v.
78ff12ee 22 */
32171208
IM
23static inline int atomic_read(const atomic_t *v)
24{
25 return v->counter;
26}
1da177e4
LT
27
28/**
29 * atomic_set - set atomic variable
30 * @v: pointer of type atomic_t
31 * @i: required value
78ff12ee 32 *
1da177e4 33 * Atomically sets the value of @v to @i.
78ff12ee 34 */
8e049ef0
PM
35static inline void atomic_set(atomic_t *v, int i)
36{
37 v->counter = i;
38}
1da177e4
LT
39
40/**
41 * atomic_add - add integer to atomic variable
42 * @i: integer value to add
43 * @v: pointer of type atomic_t
78ff12ee 44 *
1da177e4
LT
45 * Atomically adds @i to @v.
46 */
78ff12ee 47static inline void atomic_add(int i, atomic_t *v)
1da177e4 48{
78ff12ee
JP
49 asm volatile(LOCK_PREFIX "addl %1,%0"
50 : "+m" (v->counter)
51 : "ir" (i));
1da177e4
LT
52}
53
54/**
cc38682f 55 * atomic_sub - subtract integer from atomic variable
1da177e4
LT
56 * @i: integer value to subtract
57 * @v: pointer of type atomic_t
78ff12ee 58 *
1da177e4
LT
59 * Atomically subtracts @i from @v.
60 */
78ff12ee 61static inline void atomic_sub(int i, atomic_t *v)
1da177e4 62{
78ff12ee
JP
63 asm volatile(LOCK_PREFIX "subl %1,%0"
64 : "+m" (v->counter)
65 : "ir" (i));
1da177e4
LT
66}
67
68/**
69 * atomic_sub_and_test - subtract value from variable and test result
70 * @i: integer value to subtract
71 * @v: pointer of type atomic_t
78ff12ee 72 *
1da177e4
LT
73 * Atomically subtracts @i from @v and returns
74 * true if the result is zero, or false for all
75 * other cases.
76 */
78ff12ee 77static inline int atomic_sub_and_test(int i, atomic_t *v)
1da177e4
LT
78{
79 unsigned char c;
80
78ff12ee
JP
81 asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
82 : "+m" (v->counter), "=qm" (c)
83 : "ir" (i) : "memory");
1da177e4
LT
84 return c;
85}
86
87/**
88 * atomic_inc - increment atomic variable
89 * @v: pointer of type atomic_t
78ff12ee 90 *
1da177e4 91 * Atomically increments @v by 1.
78ff12ee
JP
92 */
93static inline void atomic_inc(atomic_t *v)
1da177e4 94{
78ff12ee
JP
95 asm volatile(LOCK_PREFIX "incl %0"
96 : "+m" (v->counter));
1da177e4
LT
97}
98
99/**
100 * atomic_dec - decrement atomic variable
101 * @v: pointer of type atomic_t
78ff12ee 102 *
1da177e4 103 * Atomically decrements @v by 1.
78ff12ee
JP
104 */
105static inline void atomic_dec(atomic_t *v)
1da177e4 106{
78ff12ee
JP
107 asm volatile(LOCK_PREFIX "decl %0"
108 : "+m" (v->counter));
1da177e4
LT
109}
110
111/**
112 * atomic_dec_and_test - decrement and test
113 * @v: pointer of type atomic_t
78ff12ee 114 *
1da177e4
LT
115 * Atomically decrements @v by 1 and
116 * returns true if the result is 0, or false for all other
117 * cases.
78ff12ee
JP
118 */
119static inline int atomic_dec_and_test(atomic_t *v)
1da177e4
LT
120{
121 unsigned char c;
122
78ff12ee
JP
123 asm volatile(LOCK_PREFIX "decl %0; sete %1"
124 : "+m" (v->counter), "=qm" (c)
125 : : "memory");
1da177e4
LT
126 return c != 0;
127}
128
129/**
78ff12ee 130 * atomic_inc_and_test - increment and test
1da177e4 131 * @v: pointer of type atomic_t
78ff12ee 132 *
1da177e4
LT
133 * Atomically increments @v by 1
134 * and returns true if the result is zero, or false for all
135 * other cases.
78ff12ee
JP
136 */
137static inline int atomic_inc_and_test(atomic_t *v)
1da177e4
LT
138{
139 unsigned char c;
140
78ff12ee
JP
141 asm volatile(LOCK_PREFIX "incl %0; sete %1"
142 : "+m" (v->counter), "=qm" (c)
143 : : "memory");
1da177e4
LT
144 return c != 0;
145}
146
147/**
148 * atomic_add_negative - add and test if negative
1da177e4 149 * @i: integer value to add
3ce59bb8 150 * @v: pointer of type atomic_t
78ff12ee 151 *
1da177e4
LT
152 * Atomically adds @i to @v and returns true
153 * if the result is negative, or false when
154 * result is greater than or equal to zero.
78ff12ee
JP
155 */
156static inline int atomic_add_negative(int i, atomic_t *v)
1da177e4
LT
157{
158 unsigned char c;
159
78ff12ee
JP
160 asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
161 : "+m" (v->counter), "=qm" (c)
162 : "ir" (i) : "memory");
1da177e4
LT
163 return c;
164}
165
166/**
cc38682f 167 * atomic_add_return - add integer and return
1da177e4 168 * @i: integer value to add
3ce59bb8 169 * @v: pointer of type atomic_t
1da177e4
LT
170 *
171 * Atomically adds @i to @v and returns @i + @v
172 */
78ff12ee 173static inline int atomic_add_return(int i, atomic_t *v)
1da177e4
LT
174{
175 int __i;
176#ifdef CONFIG_M386
1bb858f2 177 unsigned long flags;
78ff12ee 178 if (unlikely(boot_cpu_data.x86 <= 3))
1da177e4
LT
179 goto no_xadd;
180#endif
181 /* Modern 486+ processor */
182 __i = i;
78ff12ee
JP
183 asm volatile(LOCK_PREFIX "xaddl %0, %1"
184 : "+r" (i), "+m" (v->counter)
185 : : "memory");
1da177e4
LT
186 return i + __i;
187
188#ifdef CONFIG_M386
189no_xadd: /* Legacy 386 processor */
1bb858f2 190 local_irq_save(flags);
1da177e4
LT
191 __i = atomic_read(v);
192 atomic_set(v, i + __i);
1bb858f2 193 local_irq_restore(flags);
1da177e4
LT
194 return i + __i;
195#endif
196}
197
cc38682f
RD
198/**
199 * atomic_sub_return - subtract integer and return
200 * @v: pointer of type atomic_t
201 * @i: integer value to subtract
202 *
203 * Atomically subtracts @i from @v and returns @v - @i
204 */
78ff12ee 205static inline int atomic_sub_return(int i, atomic_t *v)
1da177e4 206{
78ff12ee 207 return atomic_add_return(-i, v);
1da177e4
LT
208}
209
3ce59bb8
BG
210#define atomic_inc_return(v) (atomic_add_return(1, v))
211#define atomic_dec_return(v) (atomic_sub_return(1, v))
212
8e049ef0
PM
213static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
214{
215 return cmpxchg(&v->counter, old, new);
216}
217
218static inline int atomic_xchg(atomic_t *v, int new)
219{
220 return xchg(&v->counter, new);
221}
4a6dae6d 222
8426e1f6 223/**
72fd4a35 224 * atomic_add_unless - add unless the number is already a given value
8426e1f6
NP
225 * @v: pointer of type atomic_t
226 * @a: the amount to add to v...
227 * @u: ...unless v is equal to u.
228 *
72fd4a35 229 * Atomically adds @a to @v, so long as @v was not already @u.
8426e1f6
NP
230 * Returns non-zero if @v was not @u, and zero otherwise.
231 */
78ff12ee 232static inline int atomic_add_unless(atomic_t *v, int a, int u)
2856f5e3
MD
233{
234 int c, old;
235 c = atomic_read(v);
236 for (;;) {
237 if (unlikely(c == (u)))
238 break;
239 old = atomic_cmpxchg((v), c, c + (a));
240 if (likely(old == c))
241 break;
242 c = old;
243 }
244 return c != (u);
245}
246
8426e1f6
NP
247#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
248
3ce59bb8
BG
249/**
250 * atomic_inc_short - increment of a short integer
251 * @v: pointer to type int
252 *
253 * Atomically adds 1 to @v
254 * Returns the new value of @u
255 */
256static inline short int atomic_inc_short(short int *v)
257{
258 asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
259 return *v;
260}
261
262#ifdef CONFIG_X86_64
263/**
264 * atomic_or_long - OR of two long integers
265 * @v1: pointer to type unsigned long
266 * @v2: pointer to type unsigned long
267 *
268 * Atomically ORs @v1 and @v2
269 * Returns the result of the OR
270 */
271static inline void atomic_or_long(unsigned long *v1, unsigned long v2)
272{
273 asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2));
274}
275#endif
1da177e4
LT
276
277/* These are x86-specific, used by some header files */
78ff12ee
JP
278#define atomic_clear_mask(mask, addr) \
279 asm volatile(LOCK_PREFIX "andl %0,%1" \
280 : : "r" (~(mask)), "m" (*(addr)) : "memory")
1da177e4 281
78ff12ee 282#define atomic_set_mask(mask, addr) \
3ce59bb8
BG
283 asm volatile(LOCK_PREFIX "orl %0,%1" \
284 : : "r" ((unsigned)(mask)), "m" (*(addr)) \
285 : "memory")
1da177e4
LT
286
287/* Atomic operations are already serializing on x86 */
288#define smp_mb__before_atomic_dec() barrier()
289#define smp_mb__after_atomic_dec() barrier()
290#define smp_mb__before_atomic_inc() barrier()
291#define smp_mb__after_atomic_inc() barrier()
292
1a3b1d89 293#include <asm/atomic64_32.h>
72099ed2 294#include <asm-generic/atomic-long.h>
1965aae3 295#endif /* _ASM_X86_ATOMIC_32_H */