Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Atomic operations that C can't guarantee us. Useful for |
| 3 | * resource counting etc.. |
| 4 | * |
| 5 | * But use these as seldom as possible since they are much more slower |
| 6 | * than regular operations. |
| 7 | * |
| 8 | * This file is subject to the terms and conditions of the GNU General Public |
| 9 | * License. See the file "COPYING" in the main directory of this archive |
| 10 | * for more details. |
| 11 | * |
Ralf Baechle | e303e08 | 2006-11-30 01:14:50 +0000 | [diff] [blame] | 12 | * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #ifndef _ASM_ATOMIC_H |
| 15 | #define _ASM_ATOMIC_H |
| 16 | |
Ralf Baechle | 192ef36 | 2006-07-07 14:07:18 +0100 | [diff] [blame] | 17 | #include <linux/irqflags.h> |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 18 | #include <linux/types.h> |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 19 | #include <asm/barrier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/cpu-features.h> |
| 21 | #include <asm/war.h> |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 22 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #define ATOMIC_INIT(i) { (i) } |
| 25 | |
| 26 | /* |
| 27 | * atomic_read - read atomic variable |
| 28 | * @v: pointer of type atomic_t |
| 29 | * |
| 30 | * Atomically reads the value of @v. |
| 31 | */ |
Anton Blanchard | f3d46f9 | 2010-05-17 14:33:53 +1000 | [diff] [blame] | 32 | #define atomic_read(v) (*(volatile int *)&(v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * atomic_set - set atomic variable |
| 36 | * @v: pointer of type atomic_t |
| 37 | * @i: required value |
| 38 | * |
| 39 | * Atomically sets the value of @v to @i. |
| 40 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 41 | #define atomic_set(v, i) ((v)->counter = (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * atomic_add - add integer to atomic variable |
| 45 | * @i: integer value to add |
| 46 | * @v: pointer of type atomic_t |
| 47 | * |
| 48 | * Atomically adds @i to @v. |
| 49 | */ |
| 50 | static __inline__ void atomic_add(int i, atomic_t * v) |
| 51 | { |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 52 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 53 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 56 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | "1: ll %0, %1 # atomic_add \n" |
| 58 | " addu %0, %2 \n" |
| 59 | " sc %0, %1 \n" |
| 60 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 61 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | : "=&r" (temp), "=m" (v->counter) |
| 63 | : "Ir" (i), "m" (v->counter)); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 64 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 65 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 68 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | "1: ll %0, %1 # atomic_add \n" |
| 70 | " addu %0, %2 \n" |
| 71 | " sc %0, %1 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 72 | " beqz %0, 2f \n" |
| 73 | " .subsection 2 \n" |
| 74 | "2: b 1b \n" |
| 75 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 76 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | : "=&r" (temp), "=m" (v->counter) |
| 78 | : "Ir" (i), "m" (v->counter)); |
| 79 | } else { |
| 80 | unsigned long flags; |
| 81 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 82 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | v->counter += i; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 84 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * atomic_sub - subtract the atomic variable |
| 90 | * @i: integer value to subtract |
| 91 | * @v: pointer of type atomic_t |
| 92 | * |
| 93 | * Atomically subtracts @i from @v. |
| 94 | */ |
| 95 | static __inline__ void atomic_sub(int i, atomic_t * v) |
| 96 | { |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 97 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 98 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 101 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | "1: ll %0, %1 # atomic_sub \n" |
| 103 | " subu %0, %2 \n" |
| 104 | " sc %0, %1 \n" |
| 105 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 106 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | : "=&r" (temp), "=m" (v->counter) |
| 108 | : "Ir" (i), "m" (v->counter)); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 109 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 110 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 113 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | "1: ll %0, %1 # atomic_sub \n" |
| 115 | " subu %0, %2 \n" |
| 116 | " sc %0, %1 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 117 | " beqz %0, 2f \n" |
| 118 | " .subsection 2 \n" |
| 119 | "2: b 1b \n" |
| 120 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 121 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | : "=&r" (temp), "=m" (v->counter) |
| 123 | : "Ir" (i), "m" (v->counter)); |
| 124 | } else { |
| 125 | unsigned long flags; |
| 126 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 127 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | v->counter -= i; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 129 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Same as above, but return the result value |
| 135 | */ |
| 136 | static __inline__ int atomic_add_return(int i, atomic_t * v) |
| 137 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 138 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 140 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 141 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 142 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 143 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 146 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | "1: ll %1, %2 # atomic_add_return \n" |
| 148 | " addu %0, %1, %3 \n" |
| 149 | " sc %0, %2 \n" |
| 150 | " beqzl %0, 1b \n" |
| 151 | " addu %0, %1, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 152 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 154 | : "Ir" (i), "m" (v->counter) |
| 155 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 156 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 157 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 160 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | "1: ll %1, %2 # atomic_add_return \n" |
| 162 | " addu %0, %1, %3 \n" |
| 163 | " sc %0, %2 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 164 | " beqz %0, 2f \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | " addu %0, %1, %3 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 166 | " .subsection 2 \n" |
| 167 | "2: b 1b \n" |
| 168 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 169 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 171 | : "Ir" (i), "m" (v->counter) |
| 172 | : "memory"); |
| 173 | } else { |
| 174 | unsigned long flags; |
| 175 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 176 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | result = v->counter; |
| 178 | result += i; |
| 179 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 180 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 183 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return result; |
| 186 | } |
| 187 | |
| 188 | static __inline__ int atomic_sub_return(int i, atomic_t * v) |
| 189 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 190 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 192 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 193 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 194 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 195 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 198 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | "1: ll %1, %2 # atomic_sub_return \n" |
| 200 | " subu %0, %1, %3 \n" |
| 201 | " sc %0, %2 \n" |
| 202 | " beqzl %0, 1b \n" |
| 203 | " subu %0, %1, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 204 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 206 | : "Ir" (i), "m" (v->counter) |
| 207 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 208 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 209 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 212 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | "1: ll %1, %2 # atomic_sub_return \n" |
| 214 | " subu %0, %1, %3 \n" |
| 215 | " sc %0, %2 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 216 | " beqz %0, 2f \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | " subu %0, %1, %3 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 218 | " .subsection 2 \n" |
| 219 | "2: b 1b \n" |
| 220 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 221 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 223 | : "Ir" (i), "m" (v->counter) |
| 224 | : "memory"); |
| 225 | } else { |
| 226 | unsigned long flags; |
| 227 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 228 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | result = v->counter; |
| 230 | result -= i; |
| 231 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 232 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 235 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return result; |
| 238 | } |
| 239 | |
| 240 | /* |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 241 | * atomic_sub_if_positive - conditionally subtract integer from atomic variable |
| 242 | * @i: integer value to subtract |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | * @v: pointer of type atomic_t |
| 244 | * |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 245 | * Atomically test @v and subtract @i if @v is greater or equal than @i. |
| 246 | * The function returns the old value of @v minus @i. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | */ |
| 248 | static __inline__ int atomic_sub_if_positive(int i, atomic_t * v) |
| 249 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 250 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 252 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 253 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 254 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 255 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 258 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | "1: ll %1, %2 # atomic_sub_if_positive\n" |
| 260 | " subu %0, %1, %3 \n" |
| 261 | " bltz %0, 1f \n" |
| 262 | " sc %0, %2 \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 263 | " .set noreorder \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | " beqzl %0, 1b \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 265 | " subu %0, %1, %3 \n" |
| 266 | " .set reorder \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | "1: \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 268 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 270 | : "Ir" (i), "m" (v->counter) |
| 271 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 272 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 273 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 276 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | "1: ll %1, %2 # atomic_sub_if_positive\n" |
| 278 | " subu %0, %1, %3 \n" |
| 279 | " bltz %0, 1f \n" |
| 280 | " sc %0, %2 \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 281 | " .set noreorder \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 282 | " beqz %0, 2f \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 283 | " subu %0, %1, %3 \n" |
| 284 | " .set reorder \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 285 | " .subsection 2 \n" |
| 286 | "2: b 1b \n" |
| 287 | " .previous \n" |
Ralf Baechle | 5095202 | 2008-07-03 23:28:35 +0100 | [diff] [blame] | 288 | "1: \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 289 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 291 | : "Ir" (i), "m" (v->counter) |
| 292 | : "memory"); |
| 293 | } else { |
| 294 | unsigned long flags; |
| 295 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 296 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | result = v->counter; |
| 298 | result -= i; |
| 299 | if (result >= 0) |
| 300 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 301 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 304 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return result; |
| 307 | } |
| 308 | |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 309 | #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
| 310 | #define atomic_xchg(v, new) (xchg(&((v)->counter), (new))) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 311 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 312 | /** |
| 313 | * atomic_add_unless - add unless the number is a given value |
| 314 | * @v: pointer of type atomic_t |
| 315 | * @a: the amount to add to v... |
| 316 | * @u: ...unless v is equal to u. |
| 317 | * |
| 318 | * Atomically adds @a to @v, so long as it was not @u. |
| 319 | * Returns non-zero if @v was not @u, and zero otherwise. |
| 320 | */ |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 321 | static __inline__ int atomic_add_unless(atomic_t *v, int a, int u) |
| 322 | { |
| 323 | int c, old; |
| 324 | c = atomic_read(v); |
| 325 | for (;;) { |
| 326 | if (unlikely(c == (u))) |
| 327 | break; |
| 328 | old = atomic_cmpxchg((v), c, c + (a)); |
| 329 | if (likely(old == c)) |
| 330 | break; |
| 331 | c = old; |
| 332 | } |
| 333 | return c != (u); |
| 334 | } |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 335 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 336 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 337 | #define atomic_dec_return(v) atomic_sub_return(1, (v)) |
| 338 | #define atomic_inc_return(v) atomic_add_return(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * atomic_sub_and_test - subtract value from variable and test result |
| 342 | * @i: integer value to subtract |
| 343 | * @v: pointer of type atomic_t |
| 344 | * |
| 345 | * Atomically subtracts @i from @v and returns |
| 346 | * true if the result is zero, or false for all |
| 347 | * other cases. |
| 348 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 349 | #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * atomic_inc_and_test - increment and test |
| 353 | * @v: pointer of type atomic_t |
| 354 | * |
| 355 | * Atomically increments @v by 1 |
| 356 | * and returns true if the result is zero, or false for all |
| 357 | * other cases. |
| 358 | */ |
| 359 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
| 360 | |
| 361 | /* |
| 362 | * atomic_dec_and_test - decrement by 1 and test |
| 363 | * @v: pointer of type atomic_t |
| 364 | * |
| 365 | * Atomically decrements @v by 1 and |
| 366 | * returns true if the result is 0, or false for all other |
| 367 | * cases. |
| 368 | */ |
| 369 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
| 370 | |
| 371 | /* |
| 372 | * atomic_dec_if_positive - decrement by 1 if old value positive |
| 373 | * @v: pointer of type atomic_t |
| 374 | */ |
| 375 | #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v) |
| 376 | |
| 377 | /* |
| 378 | * atomic_inc - increment atomic variable |
| 379 | * @v: pointer of type atomic_t |
| 380 | * |
| 381 | * Atomically increments @v by 1. |
| 382 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 383 | #define atomic_inc(v) atomic_add(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * atomic_dec - decrement and test |
| 387 | * @v: pointer of type atomic_t |
| 388 | * |
| 389 | * Atomically decrements @v by 1. |
| 390 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 391 | #define atomic_dec(v) atomic_sub(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * atomic_add_negative - add and test if negative |
| 395 | * @v: pointer of type atomic_t |
| 396 | * @i: integer value to add |
| 397 | * |
| 398 | * Atomically adds @i to @v and returns true |
| 399 | * if the result is negative, or false when |
| 400 | * result is greater than or equal to zero. |
| 401 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 402 | #define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 404 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | #define ATOMIC64_INIT(i) { (i) } |
| 407 | |
| 408 | /* |
| 409 | * atomic64_read - read atomic variable |
| 410 | * @v: pointer of type atomic64_t |
| 411 | * |
| 412 | */ |
Anton Blanchard | f3d46f9 | 2010-05-17 14:33:53 +1000 | [diff] [blame] | 413 | #define atomic64_read(v) (*(volatile long *)&(v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * atomic64_set - set atomic variable |
| 417 | * @v: pointer of type atomic64_t |
| 418 | * @i: required value |
| 419 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 420 | #define atomic64_set(v, i) ((v)->counter = (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * atomic64_add - add integer to atomic variable |
| 424 | * @i: integer value to add |
| 425 | * @v: pointer of type atomic64_t |
| 426 | * |
| 427 | * Atomically adds @i to @v. |
| 428 | */ |
| 429 | static __inline__ void atomic64_add(long i, atomic64_t * v) |
| 430 | { |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 431 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 432 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 435 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | "1: lld %0, %1 # atomic64_add \n" |
| 437 | " addu %0, %2 \n" |
| 438 | " scd %0, %1 \n" |
| 439 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 440 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | : "=&r" (temp), "=m" (v->counter) |
| 442 | : "Ir" (i), "m" (v->counter)); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 443 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 444 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 447 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | "1: lld %0, %1 # atomic64_add \n" |
| 449 | " addu %0, %2 \n" |
| 450 | " scd %0, %1 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 451 | " beqz %0, 2f \n" |
| 452 | " .subsection 2 \n" |
| 453 | "2: b 1b \n" |
| 454 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 455 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | : "=&r" (temp), "=m" (v->counter) |
| 457 | : "Ir" (i), "m" (v->counter)); |
| 458 | } else { |
| 459 | unsigned long flags; |
| 460 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 461 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | v->counter += i; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 463 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * atomic64_sub - subtract the atomic variable |
| 469 | * @i: integer value to subtract |
| 470 | * @v: pointer of type atomic64_t |
| 471 | * |
| 472 | * Atomically subtracts @i from @v. |
| 473 | */ |
| 474 | static __inline__ void atomic64_sub(long i, atomic64_t * v) |
| 475 | { |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 476 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 477 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 480 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | "1: lld %0, %1 # atomic64_sub \n" |
| 482 | " subu %0, %2 \n" |
| 483 | " scd %0, %1 \n" |
| 484 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 485 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | : "=&r" (temp), "=m" (v->counter) |
| 487 | : "Ir" (i), "m" (v->counter)); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 488 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 489 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
| 491 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 492 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | "1: lld %0, %1 # atomic64_sub \n" |
| 494 | " subu %0, %2 \n" |
| 495 | " scd %0, %1 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 496 | " beqz %0, 2f \n" |
| 497 | " .subsection 2 \n" |
| 498 | "2: b 1b \n" |
| 499 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 500 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | : "=&r" (temp), "=m" (v->counter) |
| 502 | : "Ir" (i), "m" (v->counter)); |
| 503 | } else { |
| 504 | unsigned long flags; |
| 505 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 506 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | v->counter -= i; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 508 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Same as above, but return the result value |
| 514 | */ |
| 515 | static __inline__ long atomic64_add_return(long i, atomic64_t * v) |
| 516 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 517 | long result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 519 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 520 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 521 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 522 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 525 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | "1: lld %1, %2 # atomic64_add_return \n" |
| 527 | " addu %0, %1, %3 \n" |
| 528 | " scd %0, %2 \n" |
| 529 | " beqzl %0, 1b \n" |
| 530 | " addu %0, %1, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 531 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 533 | : "Ir" (i), "m" (v->counter) |
| 534 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 535 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 536 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 539 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | "1: lld %1, %2 # atomic64_add_return \n" |
| 541 | " addu %0, %1, %3 \n" |
| 542 | " scd %0, %2 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 543 | " beqz %0, 2f \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | " addu %0, %1, %3 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 545 | " .subsection 2 \n" |
| 546 | "2: b 1b \n" |
| 547 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 548 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 550 | : "Ir" (i), "m" (v->counter) |
| 551 | : "memory"); |
| 552 | } else { |
| 553 | unsigned long flags; |
| 554 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 555 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | result = v->counter; |
| 557 | result += i; |
| 558 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 559 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 562 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return result; |
| 565 | } |
| 566 | |
| 567 | static __inline__ long atomic64_sub_return(long i, atomic64_t * v) |
| 568 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 569 | long result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 571 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 572 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 573 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 574 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 577 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | "1: lld %1, %2 # atomic64_sub_return \n" |
| 579 | " subu %0, %1, %3 \n" |
| 580 | " scd %0, %2 \n" |
| 581 | " beqzl %0, 1b \n" |
| 582 | " subu %0, %1, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 583 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 585 | : "Ir" (i), "m" (v->counter) |
| 586 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 587 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 588 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 591 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | "1: lld %1, %2 # atomic64_sub_return \n" |
| 593 | " subu %0, %1, %3 \n" |
| 594 | " scd %0, %2 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 595 | " beqz %0, 2f \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | " subu %0, %1, %3 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 597 | " .subsection 2 \n" |
| 598 | "2: b 1b \n" |
| 599 | " .previous \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 600 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 602 | : "Ir" (i), "m" (v->counter) |
| 603 | : "memory"); |
| 604 | } else { |
| 605 | unsigned long flags; |
| 606 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 607 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | result = v->counter; |
| 609 | result -= i; |
| 610 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 611 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 614 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return result; |
| 617 | } |
| 618 | |
| 619 | /* |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 620 | * atomic64_sub_if_positive - conditionally subtract integer from atomic variable |
| 621 | * @i: integer value to subtract |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | * @v: pointer of type atomic64_t |
| 623 | * |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 624 | * Atomically test @v and subtract @i if @v is greater or equal than @i. |
| 625 | * The function returns the old value of @v minus @i. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | */ |
| 627 | static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v) |
| 628 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 629 | long result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 631 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 632 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 633 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 634 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 637 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | "1: lld %1, %2 # atomic64_sub_if_positive\n" |
| 639 | " dsubu %0, %1, %3 \n" |
| 640 | " bltz %0, 1f \n" |
| 641 | " scd %0, %2 \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 642 | " .set noreorder \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | " beqzl %0, 1b \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 644 | " dsubu %0, %1, %3 \n" |
| 645 | " .set reorder \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | "1: \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 647 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 649 | : "Ir" (i), "m" (v->counter) |
| 650 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 651 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 652 | long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 655 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | "1: lld %1, %2 # atomic64_sub_if_positive\n" |
| 657 | " dsubu %0, %1, %3 \n" |
| 658 | " bltz %0, 1f \n" |
| 659 | " scd %0, %2 \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 660 | " .set noreorder \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 661 | " beqz %0, 2f \n" |
Ralf Baechle | 92f22c1 | 2006-02-23 14:10:53 +0000 | [diff] [blame] | 662 | " dsubu %0, %1, %3 \n" |
| 663 | " .set reorder \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 664 | " .subsection 2 \n" |
| 665 | "2: b 1b \n" |
| 666 | " .previous \n" |
Ralf Baechle | 5095202 | 2008-07-03 23:28:35 +0100 | [diff] [blame] | 667 | "1: \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 668 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | : "=&r" (result), "=&r" (temp), "=m" (v->counter) |
| 670 | : "Ir" (i), "m" (v->counter) |
| 671 | : "memory"); |
| 672 | } else { |
| 673 | unsigned long flags; |
| 674 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 675 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | result = v->counter; |
| 677 | result -= i; |
| 678 | if (result >= 0) |
| 679 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 680 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 683 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | return result; |
| 686 | } |
| 687 | |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 688 | #define atomic64_cmpxchg(v, o, n) \ |
Atsushi Nemoto | 7b239bb | 2007-05-10 23:47:45 +0900 | [diff] [blame] | 689 | ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n))) |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 690 | #define atomic64_xchg(v, new) (xchg(&((v)->counter), (new))) |
| 691 | |
| 692 | /** |
| 693 | * atomic64_add_unless - add unless the number is a given value |
| 694 | * @v: pointer of type atomic64_t |
| 695 | * @a: the amount to add to v... |
| 696 | * @u: ...unless v is equal to u. |
| 697 | * |
| 698 | * Atomically adds @a to @v, so long as it was not @u. |
| 699 | * Returns non-zero if @v was not @u, and zero otherwise. |
| 700 | */ |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 701 | static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u) |
| 702 | { |
| 703 | long c, old; |
| 704 | c = atomic64_read(v); |
| 705 | for (;;) { |
| 706 | if (unlikely(c == (u))) |
| 707 | break; |
| 708 | old = atomic64_cmpxchg((v), c, c + (a)); |
| 709 | if (likely(old == c)) |
| 710 | break; |
| 711 | c = old; |
| 712 | } |
| 713 | return c != (u); |
| 714 | } |
| 715 | |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 716 | #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0) |
| 717 | |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 718 | #define atomic64_dec_return(v) atomic64_sub_return(1, (v)) |
| 719 | #define atomic64_inc_return(v) atomic64_add_return(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * atomic64_sub_and_test - subtract value from variable and test result |
| 723 | * @i: integer value to subtract |
| 724 | * @v: pointer of type atomic64_t |
| 725 | * |
| 726 | * Atomically subtracts @i from @v and returns |
| 727 | * true if the result is zero, or false for all |
| 728 | * other cases. |
| 729 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 730 | #define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * atomic64_inc_and_test - increment and test |
| 734 | * @v: pointer of type atomic64_t |
| 735 | * |
| 736 | * Atomically increments @v by 1 |
| 737 | * and returns true if the result is zero, or false for all |
| 738 | * other cases. |
| 739 | */ |
| 740 | #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0) |
| 741 | |
| 742 | /* |
| 743 | * atomic64_dec_and_test - decrement by 1 and test |
| 744 | * @v: pointer of type atomic64_t |
| 745 | * |
| 746 | * Atomically decrements @v by 1 and |
| 747 | * returns true if the result is 0, or false for all other |
| 748 | * cases. |
| 749 | */ |
| 750 | #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0) |
| 751 | |
| 752 | /* |
| 753 | * atomic64_dec_if_positive - decrement by 1 if old value positive |
| 754 | * @v: pointer of type atomic64_t |
| 755 | */ |
| 756 | #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v) |
| 757 | |
| 758 | /* |
| 759 | * atomic64_inc - increment atomic variable |
| 760 | * @v: pointer of type atomic64_t |
| 761 | * |
| 762 | * Atomically increments @v by 1. |
| 763 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 764 | #define atomic64_inc(v) atomic64_add(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * atomic64_dec - decrement and test |
| 768 | * @v: pointer of type atomic64_t |
| 769 | * |
| 770 | * Atomically decrements @v by 1. |
| 771 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 772 | #define atomic64_dec(v) atomic64_sub(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * atomic64_add_negative - add and test if negative |
| 776 | * @v: pointer of type atomic64_t |
| 777 | * @i: integer value to add |
| 778 | * |
| 779 | * Atomically adds @i to @v and returns true |
| 780 | * if the result is negative, or false when |
| 781 | * result is greater than or equal to zero. |
| 782 | */ |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 783 | #define atomic64_add_negative(i, v) (atomic64_add_return(i, (v)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 785 | #endif /* CONFIG_64BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * atomic*_return operations are serializing but not the non-*_return |
| 789 | * versions. |
| 790 | */ |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 791 | #define smp_mb__before_atomic_dec() smp_mb__before_llsc() |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 792 | #define smp_mb__after_atomic_dec() smp_llsc_mb() |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 793 | #define smp_mb__before_atomic_inc() smp_mb__before_llsc() |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 794 | #define smp_mb__after_atomic_inc() smp_llsc_mb() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Arnd Bergmann | 72099ed | 2009-05-13 22:56:29 +0000 | [diff] [blame] | 796 | #include <asm-generic/atomic-long.h> |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | #endif /* _ASM_ATOMIC_H */ |