Becky Bruce | feaf7cf | 2005-09-22 14:20:04 -0500 | [diff] [blame] | 1 | #ifndef _ASM_POWERPC_ATOMIC_H_ |
| 2 | #define _ASM_POWERPC_ATOMIC_H_ |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | /* |
| 5 | * PowerPC atomic operations |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #ifdef __KERNEL__ |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <asm/cmpxchg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Becky Bruce | feaf7cf | 2005-09-22 14:20:04 -0500 | [diff] [blame] | 12 | #define ATOMIC_INIT(i) { (i) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Segher Boessenkool | 9f0cbea | 2007-08-11 10:15:30 +1000 | [diff] [blame] | 14 | static __inline__ int atomic_read(const atomic_t *v) |
| 15 | { |
| 16 | int t; |
| 17 | |
| 18 | __asm__ __volatile__("lwz%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter)); |
| 19 | |
| 20 | return t; |
| 21 | } |
| 22 | |
| 23 | static __inline__ void atomic_set(atomic_t *v, int i) |
| 24 | { |
| 25 | __asm__ __volatile__("stw%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i)); |
| 26 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static __inline__ void atomic_add(int a, atomic_t *v) |
| 29 | { |
| 30 | int t; |
| 31 | |
| 32 | __asm__ __volatile__( |
| 33 | "1: lwarx %0,0,%3 # atomic_add\n\ |
| 34 | add %0,%2,%0\n" |
| 35 | PPC405_ERR77(0,%3) |
| 36 | " stwcx. %0,0,%3 \n\ |
| 37 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 38 | : "=&r" (t), "+m" (v->counter) |
| 39 | : "r" (a), "r" (&v->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | : "cc"); |
| 41 | } |
| 42 | |
| 43 | static __inline__ int atomic_add_return(int a, atomic_t *v) |
| 44 | { |
| 45 | int t; |
| 46 | |
| 47 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 48 | PPC_ATOMIC_ENTRY_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | "1: lwarx %0,0,%2 # atomic_add_return\n\ |
| 50 | add %0,%1,%0\n" |
| 51 | PPC405_ERR77(0,%2) |
| 52 | " stwcx. %0,0,%2 \n\ |
| 53 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 54 | PPC_ATOMIC_EXIT_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | : "=&r" (t) |
| 56 | : "r" (a), "r" (&v->counter) |
| 57 | : "cc", "memory"); |
| 58 | |
| 59 | return t; |
| 60 | } |
| 61 | |
| 62 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
| 63 | |
| 64 | static __inline__ void atomic_sub(int a, atomic_t *v) |
| 65 | { |
| 66 | int t; |
| 67 | |
| 68 | __asm__ __volatile__( |
| 69 | "1: lwarx %0,0,%3 # atomic_sub\n\ |
| 70 | subf %0,%2,%0\n" |
| 71 | PPC405_ERR77(0,%3) |
| 72 | " stwcx. %0,0,%3 \n\ |
| 73 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 74 | : "=&r" (t), "+m" (v->counter) |
| 75 | : "r" (a), "r" (&v->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | : "cc"); |
| 77 | } |
| 78 | |
| 79 | static __inline__ int atomic_sub_return(int a, atomic_t *v) |
| 80 | { |
| 81 | int t; |
| 82 | |
| 83 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 84 | PPC_ATOMIC_ENTRY_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | "1: lwarx %0,0,%2 # atomic_sub_return\n\ |
| 86 | subf %0,%1,%0\n" |
| 87 | PPC405_ERR77(0,%2) |
| 88 | " stwcx. %0,0,%2 \n\ |
| 89 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 90 | PPC_ATOMIC_EXIT_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | : "=&r" (t) |
| 92 | : "r" (a), "r" (&v->counter) |
| 93 | : "cc", "memory"); |
| 94 | |
| 95 | return t; |
| 96 | } |
| 97 | |
| 98 | static __inline__ void atomic_inc(atomic_t *v) |
| 99 | { |
| 100 | int t; |
| 101 | |
| 102 | __asm__ __volatile__( |
| 103 | "1: lwarx %0,0,%2 # atomic_inc\n\ |
| 104 | addic %0,%0,1\n" |
| 105 | PPC405_ERR77(0,%2) |
| 106 | " stwcx. %0,0,%2 \n\ |
| 107 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 108 | : "=&r" (t), "+m" (v->counter) |
| 109 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 110 | : "cc", "xer"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static __inline__ int atomic_inc_return(atomic_t *v) |
| 114 | { |
| 115 | int t; |
| 116 | |
| 117 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 118 | PPC_ATOMIC_ENTRY_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | "1: lwarx %0,0,%1 # atomic_inc_return\n\ |
| 120 | addic %0,%0,1\n" |
| 121 | PPC405_ERR77(0,%1) |
| 122 | " stwcx. %0,0,%1 \n\ |
| 123 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 124 | PPC_ATOMIC_EXIT_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | : "=&r" (t) |
| 126 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 127 | : "cc", "xer", "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | return t; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * atomic_inc_and_test - increment and test |
| 134 | * @v: pointer of type atomic_t |
| 135 | * |
| 136 | * Atomically increments @v by 1 |
| 137 | * and returns true if the result is zero, or false for all |
| 138 | * other cases. |
| 139 | */ |
| 140 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
| 141 | |
| 142 | static __inline__ void atomic_dec(atomic_t *v) |
| 143 | { |
| 144 | int t; |
| 145 | |
| 146 | __asm__ __volatile__( |
| 147 | "1: lwarx %0,0,%2 # atomic_dec\n\ |
| 148 | addic %0,%0,-1\n" |
| 149 | PPC405_ERR77(0,%2)\ |
| 150 | " stwcx. %0,0,%2\n\ |
| 151 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 152 | : "=&r" (t), "+m" (v->counter) |
| 153 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 154 | : "cc", "xer"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static __inline__ int atomic_dec_return(atomic_t *v) |
| 158 | { |
| 159 | int t; |
| 160 | |
| 161 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 162 | PPC_ATOMIC_ENTRY_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | "1: lwarx %0,0,%1 # atomic_dec_return\n\ |
| 164 | addic %0,%0,-1\n" |
| 165 | PPC405_ERR77(0,%1) |
| 166 | " stwcx. %0,0,%1\n\ |
| 167 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 168 | PPC_ATOMIC_EXIT_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | : "=&r" (t) |
| 170 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 171 | : "cc", "xer", "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | return t; |
| 174 | } |
| 175 | |
Mathieu Desnoyers | f46e477 | 2007-05-08 00:34:27 -0700 | [diff] [blame] | 176 | #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
Ingo Molnar | ffbf670 | 2006-01-09 15:59:17 -0800 | [diff] [blame] | 177 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 178 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 179 | /** |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 180 | * __atomic_add_unless - add unless the number is a given value |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 181 | * @v: pointer of type atomic_t |
| 182 | * @a: the amount to add to v... |
| 183 | * @u: ...unless v is equal to u. |
| 184 | * |
| 185 | * Atomically adds @a to @v, so long as it was not @u. |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 186 | * Returns the old value of @v. |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 187 | */ |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 188 | static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u) |
Nick Piggin | f055aff | 2006-02-20 10:41:40 +0100 | [diff] [blame] | 189 | { |
| 190 | int t; |
| 191 | |
| 192 | __asm__ __volatile__ ( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 193 | PPC_ATOMIC_ENTRY_BARRIER |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 194 | "1: lwarx %0,0,%1 # __atomic_add_unless\n\ |
Nick Piggin | f055aff | 2006-02-20 10:41:40 +0100 | [diff] [blame] | 195 | cmpw 0,%0,%3 \n\ |
| 196 | beq- 2f \n\ |
| 197 | add %0,%2,%0 \n" |
| 198 | PPC405_ERR77(0,%2) |
| 199 | " stwcx. %0,0,%1 \n\ |
| 200 | bne- 1b \n" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 201 | PPC_ATOMIC_EXIT_BARRIER |
Nick Piggin | f055aff | 2006-02-20 10:41:40 +0100 | [diff] [blame] | 202 | " subf %0,%2,%0 \n\ |
| 203 | 2:" |
| 204 | : "=&r" (t) |
| 205 | : "r" (&v->counter), "r" (a), "r" (u) |
| 206 | : "cc", "memory"); |
| 207 | |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 208 | return t; |
Nick Piggin | f055aff | 2006-02-20 10:41:40 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Anton Blanchard | a6cf7ed | 2012-02-29 21:12:16 +0000 | [diff] [blame] | 211 | /** |
| 212 | * atomic_inc_not_zero - increment unless the number is zero |
| 213 | * @v: pointer of type atomic_t |
| 214 | * |
| 215 | * Atomically increments @v by 1, so long as @v is non-zero. |
| 216 | * Returns non-zero if @v was non-zero, and zero otherwise. |
| 217 | */ |
| 218 | static __inline__ int atomic_inc_not_zero(atomic_t *v) |
| 219 | { |
| 220 | int t1, t2; |
| 221 | |
| 222 | __asm__ __volatile__ ( |
| 223 | PPC_ATOMIC_ENTRY_BARRIER |
| 224 | "1: lwarx %0,0,%2 # atomic_inc_not_zero\n\ |
| 225 | cmpwi 0,%0,0\n\ |
| 226 | beq- 2f\n\ |
| 227 | addic %1,%0,1\n" |
| 228 | PPC405_ERR77(0,%2) |
| 229 | " stwcx. %1,0,%2\n\ |
| 230 | bne- 1b\n" |
| 231 | PPC_ATOMIC_EXIT_BARRIER |
| 232 | "\n\ |
| 233 | 2:" |
| 234 | : "=&r" (t1), "=&r" (t2) |
| 235 | : "r" (&v->counter) |
| 236 | : "cc", "xer", "memory"); |
| 237 | |
| 238 | return t1; |
| 239 | } |
| 240 | #define atomic_inc_not_zero(v) atomic_inc_not_zero((v)) |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | #define atomic_sub_and_test(a, v) (atomic_sub_return((a), (v)) == 0) |
| 243 | #define atomic_dec_and_test(v) (atomic_dec_return((v)) == 0) |
| 244 | |
| 245 | /* |
| 246 | * Atomically test *v and decrement if it is greater than 0. |
Robert Jennings | 434f98c | 2007-01-17 10:50:20 -0600 | [diff] [blame] | 247 | * The function returns the old value of *v minus 1, even if |
| 248 | * the atomic variable, v, was not decremented. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | */ |
| 250 | static __inline__ int atomic_dec_if_positive(atomic_t *v) |
| 251 | { |
| 252 | int t; |
| 253 | |
| 254 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 255 | PPC_ATOMIC_ENTRY_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\ |
Robert Jennings | 434f98c | 2007-01-17 10:50:20 -0600 | [diff] [blame] | 257 | cmpwi %0,1\n\ |
| 258 | addi %0,%0,-1\n\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | blt- 2f\n" |
| 260 | PPC405_ERR77(0,%1) |
| 261 | " stwcx. %0,0,%1\n\ |
| 262 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 263 | PPC_ATOMIC_EXIT_BARRIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | "\n\ |
Robert Jennings | 434f98c | 2007-01-17 10:50:20 -0600 | [diff] [blame] | 265 | 2:" : "=&b" (t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | : "r" (&v->counter) |
| 267 | : "cc", "memory"); |
| 268 | |
| 269 | return t; |
| 270 | } |
| 271 | |
Becky Bruce | feaf7cf | 2005-09-22 14:20:04 -0500 | [diff] [blame] | 272 | #define smp_mb__before_atomic_dec() smp_mb() |
| 273 | #define smp_mb__after_atomic_dec() smp_mb() |
| 274 | #define smp_mb__before_atomic_inc() smp_mb() |
| 275 | #define smp_mb__after_atomic_inc() smp_mb() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 277 | #ifdef __powerpc64__ |
| 278 | |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 279 | #define ATOMIC64_INIT(i) { (i) } |
| 280 | |
Segher Boessenkool | 9f0cbea | 2007-08-11 10:15:30 +1000 | [diff] [blame] | 281 | static __inline__ long atomic64_read(const atomic64_t *v) |
| 282 | { |
| 283 | long t; |
| 284 | |
| 285 | __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter)); |
| 286 | |
| 287 | return t; |
| 288 | } |
| 289 | |
| 290 | static __inline__ void atomic64_set(atomic64_t *v, long i) |
| 291 | { |
| 292 | __asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i)); |
| 293 | } |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 294 | |
| 295 | static __inline__ void atomic64_add(long a, atomic64_t *v) |
| 296 | { |
| 297 | long t; |
| 298 | |
| 299 | __asm__ __volatile__( |
| 300 | "1: ldarx %0,0,%3 # atomic64_add\n\ |
| 301 | add %0,%2,%0\n\ |
| 302 | stdcx. %0,0,%3 \n\ |
| 303 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 304 | : "=&r" (t), "+m" (v->counter) |
| 305 | : "r" (a), "r" (&v->counter) |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 306 | : "cc"); |
| 307 | } |
| 308 | |
| 309 | static __inline__ long atomic64_add_return(long a, atomic64_t *v) |
| 310 | { |
| 311 | long t; |
| 312 | |
| 313 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 314 | PPC_ATOMIC_ENTRY_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 315 | "1: ldarx %0,0,%2 # atomic64_add_return\n\ |
| 316 | add %0,%1,%0\n\ |
| 317 | stdcx. %0,0,%2 \n\ |
| 318 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 319 | PPC_ATOMIC_EXIT_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 320 | : "=&r" (t) |
| 321 | : "r" (a), "r" (&v->counter) |
| 322 | : "cc", "memory"); |
| 323 | |
| 324 | return t; |
| 325 | } |
| 326 | |
| 327 | #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0) |
| 328 | |
| 329 | static __inline__ void atomic64_sub(long a, atomic64_t *v) |
| 330 | { |
| 331 | long t; |
| 332 | |
| 333 | __asm__ __volatile__( |
| 334 | "1: ldarx %0,0,%3 # atomic64_sub\n\ |
| 335 | subf %0,%2,%0\n\ |
| 336 | stdcx. %0,0,%3 \n\ |
| 337 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 338 | : "=&r" (t), "+m" (v->counter) |
| 339 | : "r" (a), "r" (&v->counter) |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 340 | : "cc"); |
| 341 | } |
| 342 | |
| 343 | static __inline__ long atomic64_sub_return(long a, atomic64_t *v) |
| 344 | { |
| 345 | long t; |
| 346 | |
| 347 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 348 | PPC_ATOMIC_ENTRY_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 349 | "1: ldarx %0,0,%2 # atomic64_sub_return\n\ |
| 350 | subf %0,%1,%0\n\ |
| 351 | stdcx. %0,0,%2 \n\ |
| 352 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 353 | PPC_ATOMIC_EXIT_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 354 | : "=&r" (t) |
| 355 | : "r" (a), "r" (&v->counter) |
| 356 | : "cc", "memory"); |
| 357 | |
| 358 | return t; |
| 359 | } |
| 360 | |
| 361 | static __inline__ void atomic64_inc(atomic64_t *v) |
| 362 | { |
| 363 | long t; |
| 364 | |
| 365 | __asm__ __volatile__( |
| 366 | "1: ldarx %0,0,%2 # atomic64_inc\n\ |
| 367 | addic %0,%0,1\n\ |
| 368 | stdcx. %0,0,%2 \n\ |
| 369 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 370 | : "=&r" (t), "+m" (v->counter) |
| 371 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 372 | : "cc", "xer"); |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static __inline__ long atomic64_inc_return(atomic64_t *v) |
| 376 | { |
| 377 | long t; |
| 378 | |
| 379 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 380 | PPC_ATOMIC_ENTRY_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 381 | "1: ldarx %0,0,%1 # atomic64_inc_return\n\ |
| 382 | addic %0,%0,1\n\ |
| 383 | stdcx. %0,0,%1 \n\ |
| 384 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 385 | PPC_ATOMIC_EXIT_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 386 | : "=&r" (t) |
| 387 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 388 | : "cc", "xer", "memory"); |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 389 | |
| 390 | return t; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * atomic64_inc_and_test - increment and test |
| 395 | * @v: pointer of type atomic64_t |
| 396 | * |
| 397 | * Atomically increments @v by 1 |
| 398 | * and returns true if the result is zero, or false for all |
| 399 | * other cases. |
| 400 | */ |
| 401 | #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0) |
| 402 | |
| 403 | static __inline__ void atomic64_dec(atomic64_t *v) |
| 404 | { |
| 405 | long t; |
| 406 | |
| 407 | __asm__ __volatile__( |
| 408 | "1: ldarx %0,0,%2 # atomic64_dec\n\ |
| 409 | addic %0,%0,-1\n\ |
| 410 | stdcx. %0,0,%2\n\ |
| 411 | bne- 1b" |
Linus Torvalds | e2a3d40 | 2006-07-08 15:00:28 -0700 | [diff] [blame] | 412 | : "=&r" (t), "+m" (v->counter) |
| 413 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 414 | : "cc", "xer"); |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static __inline__ long atomic64_dec_return(atomic64_t *v) |
| 418 | { |
| 419 | long t; |
| 420 | |
| 421 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 422 | PPC_ATOMIC_ENTRY_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 423 | "1: ldarx %0,0,%1 # atomic64_dec_return\n\ |
| 424 | addic %0,%0,-1\n\ |
| 425 | stdcx. %0,0,%1\n\ |
| 426 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 427 | PPC_ATOMIC_EXIT_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 428 | : "=&r" (t) |
| 429 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 430 | : "cc", "xer", "memory"); |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 431 | |
| 432 | return t; |
| 433 | } |
| 434 | |
| 435 | #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0) |
| 436 | #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0) |
| 437 | |
| 438 | /* |
| 439 | * Atomically test *v and decrement if it is greater than 0. |
| 440 | * The function returns the old value of *v minus 1. |
| 441 | */ |
| 442 | static __inline__ long atomic64_dec_if_positive(atomic64_t *v) |
| 443 | { |
| 444 | long t; |
| 445 | |
| 446 | __asm__ __volatile__( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 447 | PPC_ATOMIC_ENTRY_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 448 | "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\ |
| 449 | addic. %0,%0,-1\n\ |
| 450 | blt- 2f\n\ |
| 451 | stdcx. %0,0,%1\n\ |
| 452 | bne- 1b" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 453 | PPC_ATOMIC_EXIT_BARRIER |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 454 | "\n\ |
| 455 | 2:" : "=&r" (t) |
| 456 | : "r" (&v->counter) |
Paul Mackerras | efc3624 | 2008-11-05 18:39:27 +0000 | [diff] [blame] | 457 | : "cc", "xer", "memory"); |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 458 | |
| 459 | return t; |
| 460 | } |
| 461 | |
Mathieu Desnoyers | f46e477 | 2007-05-08 00:34:27 -0700 | [diff] [blame] | 462 | #define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
Mathieu Desnoyers | 41806ef | 2007-01-25 11:15:52 -0500 | [diff] [blame] | 463 | #define atomic64_xchg(v, new) (xchg(&((v)->counter), new)) |
| 464 | |
| 465 | /** |
| 466 | * atomic64_add_unless - add unless the number is a given value |
| 467 | * @v: pointer of type atomic64_t |
| 468 | * @a: the amount to add to v... |
| 469 | * @u: ...unless v is equal to u. |
| 470 | * |
| 471 | * Atomically adds @a to @v, so long as it was not @u. |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 472 | * Returns the old value of @v. |
Mathieu Desnoyers | 41806ef | 2007-01-25 11:15:52 -0500 | [diff] [blame] | 473 | */ |
| 474 | static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u) |
| 475 | { |
| 476 | long t; |
| 477 | |
| 478 | __asm__ __volatile__ ( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 479 | PPC_ATOMIC_ENTRY_BARRIER |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 480 | "1: ldarx %0,0,%1 # __atomic_add_unless\n\ |
Mathieu Desnoyers | 41806ef | 2007-01-25 11:15:52 -0500 | [diff] [blame] | 481 | cmpd 0,%0,%3 \n\ |
| 482 | beq- 2f \n\ |
| 483 | add %0,%2,%0 \n" |
| 484 | " stdcx. %0,0,%1 \n\ |
| 485 | bne- 1b \n" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 486 | PPC_ATOMIC_EXIT_BARRIER |
Mathieu Desnoyers | 41806ef | 2007-01-25 11:15:52 -0500 | [diff] [blame] | 487 | " subf %0,%2,%0 \n\ |
| 488 | 2:" |
| 489 | : "=&r" (t) |
| 490 | : "r" (&v->counter), "r" (a), "r" (u) |
| 491 | : "cc", "memory"); |
| 492 | |
| 493 | return t != u; |
| 494 | } |
| 495 | |
Anton Blanchard | a6cf7ed | 2012-02-29 21:12:16 +0000 | [diff] [blame] | 496 | /** |
| 497 | * atomic_inc64_not_zero - increment unless the number is zero |
| 498 | * @v: pointer of type atomic64_t |
| 499 | * |
| 500 | * Atomically increments @v by 1, so long as @v is non-zero. |
| 501 | * Returns non-zero if @v was non-zero, and zero otherwise. |
| 502 | */ |
| 503 | static __inline__ long atomic64_inc_not_zero(atomic64_t *v) |
| 504 | { |
| 505 | long t1, t2; |
| 506 | |
| 507 | __asm__ __volatile__ ( |
| 508 | PPC_ATOMIC_ENTRY_BARRIER |
| 509 | "1: ldarx %0,0,%2 # atomic64_inc_not_zero\n\ |
| 510 | cmpdi 0,%0,0\n\ |
| 511 | beq- 2f\n\ |
| 512 | addic %1,%0,1\n\ |
| 513 | stdcx. %1,0,%2\n\ |
| 514 | bne- 1b\n" |
| 515 | PPC_ATOMIC_EXIT_BARRIER |
| 516 | "\n\ |
| 517 | 2:" |
| 518 | : "=&r" (t1), "=&r" (t2) |
| 519 | : "r" (&v->counter) |
| 520 | : "cc", "xer", "memory"); |
| 521 | |
| 522 | return t1; |
| 523 | } |
Mathieu Desnoyers | 41806ef | 2007-01-25 11:15:52 -0500 | [diff] [blame] | 524 | |
Stephen Rothwell | 06a98db | 2005-11-10 15:51:14 +1100 | [diff] [blame] | 525 | #endif /* __powerpc64__ */ |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | #endif /* __KERNEL__ */ |
Becky Bruce | feaf7cf | 2005-09-22 14:20:04 -0500 | [diff] [blame] | 528 | #endif /* _ASM_POWERPC_ATOMIC_H_ */ |