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