blob: e8c95609842436771f6c7c024c7b2830706987e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _ALPHA_ATOMIC_H
2#define _ALPHA_ATOMIC_H
3
Matthew Wilcoxea4354672009-01-06 14:40:39 -08004#include <linux/types.h>
Andrew Morton0db9ae42005-10-24 23:05:58 -07005#include <asm/barrier.h>
Paul Gortmaker5ba840f2012-04-02 16:04:13 -04006#include <asm/cmpxchg.h>
Andrew Morton0db9ae42005-10-24 23:05:58 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008/*
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
Mel Gorman67a806d2012-08-19 14:41:03 +120017#define ATOMIC_INIT(i) { (i) }
18#define ATOMIC64_INIT(i) { (i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Pranith Kumar22910592014-09-23 10:29:50 -040020#define atomic_read(v) ACCESS_ONCE((v)->counter)
21#define atomic64_read(v) ACCESS_ONCE((v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define atomic_set(v,i) ((v)->counter = (i))
24#define atomic64_set(v,i) ((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
Peter Zijlstra212d3be2014-04-23 20:07:47 +020032#define ATOMIC_OP(op, asm_op) \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010033static __inline__ void atomic_##op(int i, atomic_t * v) \
34{ \
35 unsigned long temp; \
36 __asm__ __volatile__( \
37 "1: ldl_l %0,%1\n" \
Peter Zijlstra212d3be2014-04-23 20:07:47 +020038 " " #asm_op " %0,%2,%0\n" \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010039 " 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
Peter Zijlstra212d3be2014-04-23 20:07:47 +020048#define ATOMIC_OP_RETURN(op, asm_op) \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010049static 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" \
Peter Zijlstra212d3be2014-04-23 20:07:47 +020055 " " #asm_op " %0,%3,%2\n" \
56 " " #asm_op " %0,%3,%0\n" \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010057 " 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; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Peter Zijlstra212d3be2014-04-23 20:07:47 +020068#define ATOMIC64_OP(op, asm_op) \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010069static __inline__ void atomic64_##op(long i, atomic64_t * v) \
70{ \
71 unsigned long temp; \
72 __asm__ __volatile__( \
73 "1: ldq_l %0,%1\n" \
Peter Zijlstra212d3be2014-04-23 20:07:47 +020074 " " #asm_op " %0,%2,%0\n" \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010075 " stq_c %0,%1\n" \
76 " beq %0,2f\n" \
77 ".subsection 2\n" \
78 "2: br 1b\n" \
79 ".previous" \
80 :"=&r" (temp), "=m" (v->counter) \
81 :"Ir" (i), "m" (v->counter)); \
82} \
83
Peter Zijlstra212d3be2014-04-23 20:07:47 +020084#define ATOMIC64_OP_RETURN(op, asm_op) \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010085static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \
86{ \
87 long temp, result; \
88 smp_mb(); \
89 __asm__ __volatile__( \
90 "1: ldq_l %0,%1\n" \
Peter Zijlstra212d3be2014-04-23 20:07:47 +020091 " " #asm_op " %0,%3,%2\n" \
92 " " #asm_op " %0,%3,%0\n" \
Peter Zijlstrab93c7b82014-03-23 16:25:53 +010093 " stq_c %0,%1\n" \
94 " beq %0,2f\n" \
95 ".subsection 2\n" \
96 "2: br 1b\n" \
97 ".previous" \
98 :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
99 :"Ir" (i), "m" (v->counter) : "memory"); \
100 smp_mb(); \
101 return result; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Peter Zijlstra212d3be2014-04-23 20:07:47 +0200104#define ATOMIC_OPS(op) \
105 ATOMIC_OP(op, op##l) \
106 ATOMIC_OP_RETURN(op, op##l) \
107 ATOMIC64_OP(op, op##q) \
108 ATOMIC64_OP_RETURN(op, op##q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Peter Zijlstrab93c7b82014-03-23 16:25:53 +0100110ATOMIC_OPS(add)
111ATOMIC_OPS(sub)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Peter Zijlstra212d3be2014-04-23 20:07:47 +0200113#define atomic_andnot atomic_andnot
114#define atomic64_andnot atomic64_andnot
115
116ATOMIC_OP(and, and)
117ATOMIC_OP(andnot, bic)
118ATOMIC_OP(or, bis)
119ATOMIC_OP(xor, xor)
120ATOMIC64_OP(and, and)
121ATOMIC64_OP(andnot, bic)
122ATOMIC64_OP(or, bis)
123ATOMIC64_OP(xor, xor)
124
Peter Zijlstrab93c7b82014-03-23 16:25:53 +0100125#undef ATOMIC_OPS
126#undef ATOMIC64_OP_RETURN
127#undef ATOMIC64_OP
128#undef ATOMIC_OP_RETURN
129#undef ATOMIC_OP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700131#define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
132#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
133
134#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
Ingo Molnarffbf6702006-01-09 15:59:17 -0800135#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
Nick Piggin4a6dae62005-11-13 16:07:24 -0800136
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700137/**
Arun Sharmaf24219b2011-07-26 16:09:07 -0700138 * __atomic_add_unless - add unless the number is a given value
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700139 * @v: pointer of type atomic_t
140 * @a: the amount to add to v...
141 * @u: ...unless v is equal to u.
142 *
143 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700144 * Returns the old value of @v.
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700145 */
Arun Sharmaf24219b2011-07-26 16:09:07 -0700146static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700147{
Richard Henderson6da75392013-07-11 07:42:14 -0700148 int c, new, old;
149 smp_mb();
150 __asm__ __volatile__(
151 "1: ldl_l %[old],%[mem]\n"
152 " cmpeq %[old],%[u],%[c]\n"
153 " addl %[old],%[a],%[new]\n"
154 " bne %[c],2f\n"
155 " stl_c %[new],%[mem]\n"
156 " beq %[new],3f\n"
157 "2:\n"
158 ".subsection 2\n"
159 "3: br 1b\n"
160 ".previous"
161 : [old] "=&r"(old), [new] "=&r"(new), [c] "=&r"(c)
162 : [mem] "m"(*v), [a] "rI"(a), [u] "rI"((long)u)
163 : "memory");
164 smp_mb();
165 return old;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700166}
167
Nick Piggin8426e1f2005-11-13 16:07:25 -0800168
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700169/**
170 * atomic64_add_unless - add unless the number is a given value
171 * @v: pointer of type atomic64_t
172 * @a: the amount to add to v...
173 * @u: ...unless v is equal to u.
174 *
175 * Atomically adds @a to @v, so long as it was not @u.
Richard Henderson6da75392013-07-11 07:42:14 -0700176 * Returns true iff @v was not @u.
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700177 */
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700178static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
179{
Richard Henderson6da75392013-07-11 07:42:14 -0700180 long c, tmp;
181 smp_mb();
182 __asm__ __volatile__(
183 "1: ldq_l %[tmp],%[mem]\n"
184 " cmpeq %[tmp],%[u],%[c]\n"
185 " addq %[tmp],%[a],%[tmp]\n"
186 " bne %[c],2f\n"
187 " stq_c %[tmp],%[mem]\n"
188 " beq %[tmp],3f\n"
189 "2:\n"
190 ".subsection 2\n"
191 "3: br 1b\n"
192 ".previous"
193 : [tmp] "=&r"(tmp), [c] "=&r"(c)
194 : [mem] "m"(*v), [a] "rI"(a), [u] "rI"(u)
195 : "memory");
196 smp_mb();
197 return !c;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700198}
199
Richard Henderson748a76b2013-07-11 08:04:20 -0700200/*
201 * atomic64_dec_if_positive - decrement by 1 if old value positive
202 * @v: pointer of type atomic_t
203 *
204 * The function returns the old value of *v minus 1, even if
205 * the atomic variable, v, was not decremented.
206 */
207static inline long atomic64_dec_if_positive(atomic64_t *v)
208{
209 long old, tmp;
210 smp_mb();
211 __asm__ __volatile__(
212 "1: ldq_l %[old],%[mem]\n"
213 " subq %[old],1,%[tmp]\n"
214 " ble %[old],2f\n"
215 " stq_c %[tmp],%[mem]\n"
216 " beq %[tmp],3f\n"
217 "2:\n"
218 ".subsection 2\n"
219 "3: br 1b\n"
220 ".previous"
221 : [old] "=&r"(old), [tmp] "=&r"(tmp)
222 : [mem] "m"(*v)
223 : "memory");
224 smp_mb();
225 return old - 1;
226}
227
Mathieu Desnoyerse96e6992007-05-08 00:34:18 -0700228#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
229
Hugh Dickins7c72aaf2005-11-23 13:37:40 -0800230#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
231#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define atomic_dec_return(v) atomic_sub_return(1,(v))
234#define atomic64_dec_return(v) atomic64_sub_return(1,(v))
235
236#define atomic_inc_return(v) atomic_add_return(1,(v))
237#define atomic64_inc_return(v) atomic64_add_return(1,(v))
238
239#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
240#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
241
242#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
Hugh Dickins7c72aaf2005-11-23 13:37:40 -0800243#define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
246#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
247
248#define atomic_inc(v) atomic_add(1,(v))
249#define atomic64_inc(v) atomic64_add(1,(v))
250
251#define atomic_dec(v) atomic_sub(1,(v))
252#define atomic64_dec(v) atomic64_sub(1,(v))
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#endif /* _ALPHA_ATOMIC_H */