Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ARCH_I386_ATOMIC__ |
| 2 | #define __ARCH_I386_ATOMIC__ |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/compiler.h> |
| 5 | #include <asm/processor.h> |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 6 | #include <asm/cmpxchg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * Atomic operations that C can't guarantee us. Useful for |
| 10 | * resource counting etc.. |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | /* |
| 14 | * Make sure gcc doesn't try to be clever and move things around |
| 15 | * on us. We need to use _exactly_ the address the user gave us, |
| 16 | * not some alias that contains the same information. |
| 17 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 18 | typedef struct { |
| 19 | int counter; |
| 20 | } atomic_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define ATOMIC_INIT(i) { (i) } |
| 23 | |
| 24 | /** |
| 25 | * atomic_read - read atomic variable |
| 26 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 27 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * Atomically reads the value of @v. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 29 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #define atomic_read(v) ((v)->counter) |
| 31 | |
| 32 | /** |
| 33 | * atomic_set - set atomic variable |
| 34 | * @v: pointer of type atomic_t |
| 35 | * @i: required value |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 36 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * Atomically sets the value of @v to @i. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 38 | */ |
| 39 | #define atomic_set(v, i) (((v)->counter) = (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * atomic_add - add integer to atomic variable |
| 43 | * @i: integer value to add |
| 44 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 45 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * Atomically adds @i to @v. |
| 47 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 48 | static inline void atomic_add(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 50 | asm volatile(LOCK_PREFIX "addl %1,%0" |
| 51 | : "+m" (v->counter) |
| 52 | : "ir" (i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
Robert P. J. Day | cc38682 | 2007-05-08 00:35:08 -0700 | [diff] [blame] | 56 | * atomic_sub - subtract integer from atomic variable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * @i: integer value to subtract |
| 58 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 59 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * Atomically subtracts @i from @v. |
| 61 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 62 | static inline void atomic_sub(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 64 | asm volatile(LOCK_PREFIX "subl %1,%0" |
| 65 | : "+m" (v->counter) |
| 66 | : "ir" (i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * atomic_sub_and_test - subtract value from variable and test result |
| 71 | * @i: integer value to subtract |
| 72 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 73 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * Atomically subtracts @i from @v and returns |
| 75 | * true if the result is zero, or false for all |
| 76 | * other cases. |
| 77 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 78 | static inline int atomic_sub_and_test(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | unsigned char c; |
| 81 | |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 82 | asm volatile(LOCK_PREFIX "subl %2,%0; sete %1" |
| 83 | : "+m" (v->counter), "=qm" (c) |
| 84 | : "ir" (i) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return c; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * atomic_inc - increment atomic variable |
| 90 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 91 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * Atomically increments @v by 1. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 93 | */ |
| 94 | static inline void atomic_inc(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 96 | asm volatile(LOCK_PREFIX "incl %0" |
| 97 | : "+m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * atomic_dec - decrement atomic variable |
| 102 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 103 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * Atomically decrements @v by 1. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 105 | */ |
| 106 | static inline void atomic_dec(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 108 | asm volatile(LOCK_PREFIX "decl %0" |
| 109 | : "+m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * atomic_dec_and_test - decrement and test |
| 114 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 115 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * Atomically decrements @v by 1 and |
| 117 | * returns true if the result is 0, or false for all other |
| 118 | * cases. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 119 | */ |
| 120 | static inline int atomic_dec_and_test(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | unsigned char c; |
| 123 | |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 124 | asm volatile(LOCK_PREFIX "decl %0; sete %1" |
| 125 | : "+m" (v->counter), "=qm" (c) |
| 126 | : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return c != 0; |
| 128 | } |
| 129 | |
| 130 | /** |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 131 | * atomic_inc_and_test - increment and test |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * @v: pointer of type atomic_t |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 133 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | * Atomically increments @v by 1 |
| 135 | * and returns true if the result is zero, or false for all |
| 136 | * other cases. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 137 | */ |
| 138 | static inline int atomic_inc_and_test(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
| 140 | unsigned char c; |
| 141 | |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 142 | asm volatile(LOCK_PREFIX "incl %0; sete %1" |
| 143 | : "+m" (v->counter), "=qm" (c) |
| 144 | : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return c != 0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * atomic_add_negative - add and test if negative |
| 150 | * @v: pointer of type atomic_t |
| 151 | * @i: integer value to add |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 152 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * Atomically adds @i to @v and returns true |
| 154 | * if the result is negative, or false when |
| 155 | * result is greater than or equal to zero. |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 156 | */ |
| 157 | static inline int atomic_add_negative(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
| 159 | unsigned char c; |
| 160 | |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 161 | asm volatile(LOCK_PREFIX "addl %2,%0; sets %1" |
| 162 | : "+m" (v->counter), "=qm" (c) |
| 163 | : "ir" (i) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return c; |
| 165 | } |
| 166 | |
| 167 | /** |
Robert P. J. Day | cc38682 | 2007-05-08 00:35:08 -0700 | [diff] [blame] | 168 | * atomic_add_return - add integer and return |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | * @v: pointer of type atomic_t |
| 170 | * @i: integer value to add |
| 171 | * |
| 172 | * Atomically adds @i to @v and returns @i + @v |
| 173 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 174 | static inline int atomic_add_return(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | int __i; |
| 177 | #ifdef CONFIG_M386 |
lepton | 1bb858f | 2006-04-18 22:21:10 -0700 | [diff] [blame] | 178 | unsigned long flags; |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 179 | if (unlikely(boot_cpu_data.x86 <= 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | goto no_xadd; |
| 181 | #endif |
| 182 | /* Modern 486+ processor */ |
| 183 | __i = i; |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 184 | asm volatile(LOCK_PREFIX "xaddl %0, %1" |
| 185 | : "+r" (i), "+m" (v->counter) |
| 186 | : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return i + __i; |
| 188 | |
| 189 | #ifdef CONFIG_M386 |
| 190 | no_xadd: /* Legacy 386 processor */ |
lepton | 1bb858f | 2006-04-18 22:21:10 -0700 | [diff] [blame] | 191 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | __i = atomic_read(v); |
| 193 | atomic_set(v, i + __i); |
lepton | 1bb858f | 2006-04-18 22:21:10 -0700 | [diff] [blame] | 194 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return i + __i; |
| 196 | #endif |
| 197 | } |
| 198 | |
Robert P. J. Day | cc38682 | 2007-05-08 00:35:08 -0700 | [diff] [blame] | 199 | /** |
| 200 | * atomic_sub_return - subtract integer and return |
| 201 | * @v: pointer of type atomic_t |
| 202 | * @i: integer value to subtract |
| 203 | * |
| 204 | * Atomically subtracts @i from @v and returns @v - @i |
| 205 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 206 | static inline int atomic_sub_return(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 208 | return atomic_add_return(-i, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Mathieu Desnoyers | e656e24 | 2007-05-08 00:34:20 -0700 | [diff] [blame] | 211 | #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) |
| 212 | #define atomic_xchg(v, new) (xchg(&((v)->counter), (new))) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 213 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 214 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 215 | * atomic_add_unless - add unless the number is already a given value |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 216 | * @v: pointer of type atomic_t |
| 217 | * @a: the amount to add to v... |
| 218 | * @u: ...unless v is equal to u. |
| 219 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 220 | * Atomically adds @a to @v, so long as @v was not already @u. |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 221 | * Returns non-zero if @v was not @u, and zero otherwise. |
| 222 | */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 223 | 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] | 224 | { |
| 225 | int c, old; |
| 226 | c = atomic_read(v); |
| 227 | for (;;) { |
| 228 | if (unlikely(c == (u))) |
| 229 | break; |
| 230 | old = atomic_cmpxchg((v), c, c + (a)); |
| 231 | if (likely(old == c)) |
| 232 | break; |
| 233 | c = old; |
| 234 | } |
| 235 | return c != (u); |
| 236 | } |
| 237 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 238 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 239 | |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 240 | #define atomic_inc_return(v) (atomic_add_return(1, v)) |
| 241 | #define atomic_dec_return(v) (atomic_sub_return(1, v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | /* These are x86-specific, used by some header files */ |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 244 | #define atomic_clear_mask(mask, addr) \ |
| 245 | asm volatile(LOCK_PREFIX "andl %0,%1" \ |
| 246 | : : "r" (~(mask)), "m" (*(addr)) : "memory") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Joe Perches | 78ff12e | 2008-03-23 01:01:41 -0700 | [diff] [blame] | 248 | #define atomic_set_mask(mask, addr) \ |
| 249 | asm volatile(LOCK_PREFIX "orl %0,%1" \ |
| 250 | : : "r" (mask), "m" (*(addr)) : "memory") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | /* Atomic operations are already serializing on x86 */ |
| 253 | #define smp_mb__before_atomic_dec() barrier() |
| 254 | #define smp_mb__after_atomic_dec() barrier() |
| 255 | #define smp_mb__before_atomic_inc() barrier() |
| 256 | #define smp_mb__after_atomic_inc() barrier() |
| 257 | |
Christoph Lameter | d3cb487 | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 258 | #include <asm-generic/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | #endif |