locking/atomic: Remove linux/atomic.h:atomic_fetch_or()
[cascardo/linux.git] / arch / alpha / include / asm / atomic.h
1 #ifndef _ALPHA_ATOMIC_H
2 #define _ALPHA_ATOMIC_H
3
4 #include <linux/types.h>
5 #include <asm/barrier.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  * But use these as seldom as possible since they are much slower
13  * than regular operations.
14  */
15
16
17 #define ATOMIC_INIT(i)          { (i) }
18 #define ATOMIC64_INIT(i)        { (i) }
19
20 #define atomic_read(v)          READ_ONCE((v)->counter)
21 #define atomic64_read(v)        READ_ONCE((v)->counter)
22
23 #define atomic_set(v,i)         WRITE_ONCE((v)->counter, (i))
24 #define atomic64_set(v,i)       WRITE_ONCE((v)->counter, (i))
25
26 /*
27  * To get proper branch prediction for the main line, we must branch
28  * forward to code at the end of this object's .text section, then
29  * branch back to restart the operation.
30  */
31
32 #define ATOMIC_OP(op, asm_op)                                           \
33 static __inline__ void atomic_##op(int i, atomic_t * v)                 \
34 {                                                                       \
35         unsigned long temp;                                             \
36         __asm__ __volatile__(                                           \
37         "1:     ldl_l %0,%1\n"                                          \
38         "       " #asm_op " %0,%2,%0\n"                                 \
39         "       stl_c %0,%1\n"                                          \
40         "       beq %0,2f\n"                                            \
41         ".subsection 2\n"                                               \
42         "2:     br 1b\n"                                                \
43         ".previous"                                                     \
44         :"=&r" (temp), "=m" (v->counter)                                \
45         :"Ir" (i), "m" (v->counter));                                   \
46 }                                                                       \
47
48 #define ATOMIC_OP_RETURN(op, asm_op)                                    \
49 static inline int atomic_##op##_return(int i, atomic_t *v)              \
50 {                                                                       \
51         long temp, result;                                              \
52         smp_mb();                                                       \
53         __asm__ __volatile__(                                           \
54         "1:     ldl_l %0,%1\n"                                          \
55         "       " #asm_op " %0,%3,%2\n"                                 \
56         "       " #asm_op " %0,%3,%0\n"                                 \
57         "       stl_c %0,%1\n"                                          \
58         "       beq %0,2f\n"                                            \
59         ".subsection 2\n"                                               \
60         "2:     br 1b\n"                                                \
61         ".previous"                                                     \
62         :"=&r" (temp), "=m" (v->counter), "=&r" (result)                \
63         :"Ir" (i), "m" (v->counter) : "memory");                        \
64         smp_mb();                                                       \
65         return result;                                                  \
66 }
67
68 #define ATOMIC_FETCH_OP(op, asm_op)                                     \
69 static inline int atomic_fetch_##op(int i, atomic_t *v)                 \
70 {                                                                       \
71         long temp, result;                                              \
72         smp_mb();                                                       \
73         __asm__ __volatile__(                                           \
74         "1:     ldl_l %2,%1\n"                                          \
75         "       " #asm_op " %2,%3,%0\n"                                 \
76         "       stl_c %0,%1\n"                                          \
77         "       beq %0,2f\n"                                            \
78         ".subsection 2\n"                                               \
79         "2:     br 1b\n"                                                \
80         ".previous"                                                     \
81         :"=&r" (temp), "=m" (v->counter), "=&r" (result)                \
82         :"Ir" (i), "m" (v->counter) : "memory");                        \
83         smp_mb();                                                       \
84         return result;                                                  \
85 }
86
87 #define ATOMIC64_OP(op, asm_op)                                         \
88 static __inline__ void atomic64_##op(long i, atomic64_t * v)            \
89 {                                                                       \
90         unsigned long temp;                                             \
91         __asm__ __volatile__(                                           \
92         "1:     ldq_l %0,%1\n"                                          \
93         "       " #asm_op " %0,%2,%0\n"                                 \
94         "       stq_c %0,%1\n"                                          \
95         "       beq %0,2f\n"                                            \
96         ".subsection 2\n"                                               \
97         "2:     br 1b\n"                                                \
98         ".previous"                                                     \
99         :"=&r" (temp), "=m" (v->counter)                                \
100         :"Ir" (i), "m" (v->counter));                                   \
101 }                                                                       \
102
103 #define ATOMIC64_OP_RETURN(op, asm_op)                                  \
104 static __inline__ long atomic64_##op##_return(long i, atomic64_t * v)   \
105 {                                                                       \
106         long temp, result;                                              \
107         smp_mb();                                                       \
108         __asm__ __volatile__(                                           \
109         "1:     ldq_l %0,%1\n"                                          \
110         "       " #asm_op " %0,%3,%2\n"                                 \
111         "       " #asm_op " %0,%3,%0\n"                                 \
112         "       stq_c %0,%1\n"                                          \
113         "       beq %0,2f\n"                                            \
114         ".subsection 2\n"                                               \
115         "2:     br 1b\n"                                                \
116         ".previous"                                                     \
117         :"=&r" (temp), "=m" (v->counter), "=&r" (result)                \
118         :"Ir" (i), "m" (v->counter) : "memory");                        \
119         smp_mb();                                                       \
120         return result;                                                  \
121 }
122
123 #define ATOMIC64_FETCH_OP(op, asm_op)                                   \
124 static __inline__ long atomic64_fetch_##op(long i, atomic64_t * v)      \
125 {                                                                       \
126         long temp, result;                                              \
127         smp_mb();                                                       \
128         __asm__ __volatile__(                                           \
129         "1:     ldq_l %2,%1\n"                                          \
130         "       " #asm_op " %2,%3,%0\n"                                 \
131         "       stq_c %0,%1\n"                                          \
132         "       beq %0,2f\n"                                            \
133         ".subsection 2\n"                                               \
134         "2:     br 1b\n"                                                \
135         ".previous"                                                     \
136         :"=&r" (temp), "=m" (v->counter), "=&r" (result)                \
137         :"Ir" (i), "m" (v->counter) : "memory");                        \
138         smp_mb();                                                       \
139         return result;                                                  \
140 }
141
142 #define ATOMIC_OPS(op)                                                  \
143         ATOMIC_OP(op, op##l)                                            \
144         ATOMIC_OP_RETURN(op, op##l)                                     \
145         ATOMIC_FETCH_OP(op, op##l)                                      \
146         ATOMIC64_OP(op, op##q)                                          \
147         ATOMIC64_OP_RETURN(op, op##q)                                   \
148         ATOMIC64_FETCH_OP(op, op##q)
149
150 ATOMIC_OPS(add)
151 ATOMIC_OPS(sub)
152
153 #define atomic_andnot atomic_andnot
154 #define atomic64_andnot atomic64_andnot
155
156 #undef ATOMIC_OPS
157 #define ATOMIC_OPS(op, asm)                                             \
158         ATOMIC_OP(op, asm)                                              \
159         ATOMIC_FETCH_OP(op, asm)                                        \
160         ATOMIC64_OP(op, asm)                                            \
161         ATOMIC64_FETCH_OP(op, asm)
162
163 ATOMIC_OPS(and, and)
164 ATOMIC_OPS(andnot, bic)
165 ATOMIC_OPS(or, bis)
166 ATOMIC_OPS(xor, xor)
167
168 #undef ATOMIC_OPS
169 #undef ATOMIC64_FETCH_OP
170 #undef ATOMIC64_OP_RETURN
171 #undef ATOMIC64_OP
172 #undef ATOMIC_FETCH_OP
173 #undef ATOMIC_OP_RETURN
174 #undef ATOMIC_OP
175
176 #define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
177 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
178
179 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
180 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
181
182 /**
183  * __atomic_add_unless - add unless the number is a given value
184  * @v: pointer of type atomic_t
185  * @a: the amount to add to v...
186  * @u: ...unless v is equal to u.
187  *
188  * Atomically adds @a to @v, so long as it was not @u.
189  * Returns the old value of @v.
190  */
191 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
192 {
193         int c, new, old;
194         smp_mb();
195         __asm__ __volatile__(
196         "1:     ldl_l   %[old],%[mem]\n"
197         "       cmpeq   %[old],%[u],%[c]\n"
198         "       addl    %[old],%[a],%[new]\n"
199         "       bne     %[c],2f\n"
200         "       stl_c   %[new],%[mem]\n"
201         "       beq     %[new],3f\n"
202         "2:\n"
203         ".subsection 2\n"
204         "3:     br      1b\n"
205         ".previous"
206         : [old] "=&r"(old), [new] "=&r"(new), [c] "=&r"(c)
207         : [mem] "m"(*v), [a] "rI"(a), [u] "rI"((long)u)
208         : "memory");
209         smp_mb();
210         return old;
211 }
212
213
214 /**
215  * atomic64_add_unless - add unless the number is a given value
216  * @v: pointer of type atomic64_t
217  * @a: the amount to add to v...
218  * @u: ...unless v is equal to u.
219  *
220  * Atomically adds @a to @v, so long as it was not @u.
221  * Returns true iff @v was not @u.
222  */
223 static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
224 {
225         long c, tmp;
226         smp_mb();
227         __asm__ __volatile__(
228         "1:     ldq_l   %[tmp],%[mem]\n"
229         "       cmpeq   %[tmp],%[u],%[c]\n"
230         "       addq    %[tmp],%[a],%[tmp]\n"
231         "       bne     %[c],2f\n"
232         "       stq_c   %[tmp],%[mem]\n"
233         "       beq     %[tmp],3f\n"
234         "2:\n"
235         ".subsection 2\n"
236         "3:     br      1b\n"
237         ".previous"
238         : [tmp] "=&r"(tmp), [c] "=&r"(c)
239         : [mem] "m"(*v), [a] "rI"(a), [u] "rI"(u)
240         : "memory");
241         smp_mb();
242         return !c;
243 }
244
245 /*
246  * atomic64_dec_if_positive - decrement by 1 if old value positive
247  * @v: pointer of type atomic_t
248  *
249  * The function returns the old value of *v minus 1, even if
250  * the atomic variable, v, was not decremented.
251  */
252 static inline long atomic64_dec_if_positive(atomic64_t *v)
253 {
254         long old, tmp;
255         smp_mb();
256         __asm__ __volatile__(
257         "1:     ldq_l   %[old],%[mem]\n"
258         "       subq    %[old],1,%[tmp]\n"
259         "       ble     %[old],2f\n"
260         "       stq_c   %[tmp],%[mem]\n"
261         "       beq     %[tmp],3f\n"
262         "2:\n"
263         ".subsection 2\n"
264         "3:     br      1b\n"
265         ".previous"
266         : [old] "=&r"(old), [tmp] "=&r"(tmp)
267         : [mem] "m"(*v)
268         : "memory");
269         smp_mb();
270         return old - 1;
271 }
272
273 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
274
275 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
276 #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
277
278 #define atomic_dec_return(v) atomic_sub_return(1,(v))
279 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
280
281 #define atomic_inc_return(v) atomic_add_return(1,(v))
282 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
283
284 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
285 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
286
287 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
288 #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
289
290 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
291 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
292
293 #define atomic_inc(v) atomic_add(1,(v))
294 #define atomic64_inc(v) atomic64_add(1,(v))
295
296 #define atomic_dec(v) atomic_sub(1,(v))
297 #define atomic64_dec(v) atomic64_sub(1,(v))
298
299 #endif /* _ALPHA_ATOMIC_H */