Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _ALPHA_ATOMIC_H |
| 2 | #define _ALPHA_ATOMIC_H |
| 3 | |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 4 | #include <linux/types.h> |
Andrew Morton | 0db9ae4 | 2005-10-24 23:05:58 -0700 | [diff] [blame] | 5 | #include <asm/barrier.h> |
Paul Gortmaker | 5ba840f | 2012-04-02 16:04:13 -0400 | [diff] [blame] | 6 | #include <asm/cmpxchg.h> |
Andrew Morton | 0db9ae4 | 2005-10-24 23:05:58 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Mel Gorman | 67a806d | 2012-08-19 14:41:03 +1200 | [diff] [blame] | 17 | #define ATOMIC_INIT(i) { (i) } |
| 18 | #define ATOMIC64_INIT(i) { (i) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Pranith Kumar | 2291059 | 2014-09-23 10:29:50 -0400 | [diff] [blame] | 20 | #define atomic_read(v) ACCESS_ONCE((v)->counter) |
| 21 | #define atomic64_read(v) ACCESS_ONCE((v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 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 Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 32 | #define ATOMIC_OP(op, asm_op) \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 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" \ |
Peter Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 38 | " " #asm_op " %0,%2,%0\n" \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 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 | |
Peter Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 48 | #define ATOMIC_OP_RETURN(op, asm_op) \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 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" \ |
Peter Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 55 | " " #asm_op " %0,%3,%2\n" \ |
| 56 | " " #asm_op " %0,%3,%0\n" \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 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; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Peter Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 68 | #define ATOMIC64_OP(op, asm_op) \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 69 | static __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 Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 74 | " " #asm_op " %0,%2,%0\n" \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 75 | " 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 Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 84 | #define ATOMIC64_OP_RETURN(op, asm_op) \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 85 | static __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 Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 91 | " " #asm_op " %0,%3,%2\n" \ |
| 92 | " " #asm_op " %0,%3,%0\n" \ |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 93 | " 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Peter Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 104 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 110 | ATOMIC_OPS(add) |
| 111 | ATOMIC_OPS(sub) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Peter Zijlstra | 212d3be | 2014-04-23 20:07:47 +0200 | [diff] [blame] | 113 | #define atomic_andnot atomic_andnot |
| 114 | #define atomic64_andnot atomic64_andnot |
| 115 | |
| 116 | ATOMIC_OP(and, and) |
| 117 | ATOMIC_OP(andnot, bic) |
| 118 | ATOMIC_OP(or, bis) |
| 119 | ATOMIC_OP(xor, xor) |
| 120 | ATOMIC64_OP(and, and) |
| 121 | ATOMIC64_OP(andnot, bic) |
| 122 | ATOMIC64_OP(or, bis) |
| 123 | ATOMIC64_OP(xor, xor) |
| 124 | |
Peter Zijlstra | b93c7b8 | 2014-03-23 16:25:53 +0100 | [diff] [blame] | 125 | #undef ATOMIC_OPS |
| 126 | #undef ATOMIC64_OP_RETURN |
| 127 | #undef ATOMIC64_OP |
| 128 | #undef ATOMIC_OP_RETURN |
| 129 | #undef ATOMIC_OP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Mathieu Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 131 | #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 Molnar | ffbf670 | 2006-01-09 15:59:17 -0800 | [diff] [blame] | 135 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 136 | |
Mathieu Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 137 | /** |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 138 | * __atomic_add_unless - add unless the number is a given value |
Mathieu Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 139 | * @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 Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 144 | * Returns the old value of @v. |
Mathieu Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 145 | */ |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 146 | static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u) |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 147 | { |
Richard Henderson | 6da7539 | 2013-07-11 07:42:14 -0700 | [diff] [blame] | 148 | 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 Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 168 | |
Mathieu Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 169 | /** |
| 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 Henderson | 6da7539 | 2013-07-11 07:42:14 -0700 | [diff] [blame] | 176 | * Returns true iff @v was not @u. |
Mathieu Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 177 | */ |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 178 | static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u) |
| 179 | { |
Richard Henderson | 6da7539 | 2013-07-11 07:42:14 -0700 | [diff] [blame] | 180 | 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 Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Richard Henderson | 748a76b | 2013-07-11 08:04:20 -0700 | [diff] [blame] | 200 | /* |
| 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 | */ |
| 207 | static 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 Desnoyers | e96e699 | 2007-05-08 00:34:18 -0700 | [diff] [blame] | 228 | #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0) |
| 229 | |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 230 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | #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 Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 243 | #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0) |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | #endif /* _ALPHA_ATOMIC_H */ |