Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | 4baa992 | 2008-08-02 10:55:55 +0100 | [diff] [blame] | 2 | * arch/arm/include/asm/atomic.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1996 Russell King. |
| 5 | * Copyright (C) 2002 Deep Blue Solutions Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #ifndef __ASM_ARM_ATOMIC_H |
| 12 | #define __ASM_ARM_ATOMIC_H |
| 13 | |
Russell King | 8dc39b8 | 2005-11-16 17:23:57 +0000 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 15 | #include <linux/types.h> |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 16 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #define ATOMIC_INIT(i) { (i) } |
| 19 | |
| 20 | #ifdef __KERNEL__ |
| 21 | |
Catalin Marinas | 200b812 | 2009-09-18 23:27:05 +0100 | [diff] [blame] | 22 | /* |
| 23 | * On ARM, ordinary assignment (str instruction) doesn't clear the local |
| 24 | * strex/ldrex monitor on some implementations. The reason we can use it for |
| 25 | * atomic_set() is the clrex or dummy strex done on every exception return. |
| 26 | */ |
Anton Blanchard | f3d46f9 | 2010-05-17 14:33:53 +1000 | [diff] [blame] | 27 | #define atomic_read(v) (*(volatile int *)&(v)->counter) |
Catalin Marinas | 200b812 | 2009-09-18 23:27:05 +0100 | [diff] [blame] | 28 | #define atomic_set(v,i) (((v)->counter) = (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #if __LINUX_ARM_ARCH__ >= 6 |
| 31 | |
| 32 | /* |
| 33 | * ARMv6 UP and SMP safe atomic ops. We use load exclusive and |
| 34 | * store exclusive to ensure that these are atomic. We may loop |
Catalin Marinas | 200b812 | 2009-09-18 23:27:05 +0100 | [diff] [blame] | 35 | * to ensure that the update happens. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | */ |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 37 | static inline void atomic_add(int i, atomic_t *v) |
| 38 | { |
| 39 | unsigned long tmp; |
| 40 | int result; |
| 41 | |
| 42 | __asm__ __volatile__("@ atomic_add\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 43 | "1: ldrex %0, [%3]\n" |
| 44 | " add %0, %0, %4\n" |
| 45 | " strex %1, %0, [%3]\n" |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 46 | " teq %1, #0\n" |
| 47 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 48 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 49 | : "r" (&v->counter), "Ir" (i) |
| 50 | : "cc"); |
| 51 | } |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static inline int atomic_add_return(int i, atomic_t *v) |
| 54 | { |
| 55 | unsigned long tmp; |
| 56 | int result; |
| 57 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 58 | smp_mb(); |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | __asm__ __volatile__("@ atomic_add_return\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 61 | "1: ldrex %0, [%3]\n" |
| 62 | " add %0, %0, %4\n" |
| 63 | " strex %1, %0, [%3]\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | " teq %1, #0\n" |
| 65 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 66 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | : "r" (&v->counter), "Ir" (i) |
| 68 | : "cc"); |
| 69 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 70 | smp_mb(); |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return result; |
| 73 | } |
| 74 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 75 | static inline void atomic_sub(int i, atomic_t *v) |
| 76 | { |
| 77 | unsigned long tmp; |
| 78 | int result; |
| 79 | |
| 80 | __asm__ __volatile__("@ atomic_sub\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 81 | "1: ldrex %0, [%3]\n" |
| 82 | " sub %0, %0, %4\n" |
| 83 | " strex %1, %0, [%3]\n" |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 84 | " teq %1, #0\n" |
| 85 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 86 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 87 | : "r" (&v->counter), "Ir" (i) |
| 88 | : "cc"); |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static inline int atomic_sub_return(int i, atomic_t *v) |
| 92 | { |
| 93 | unsigned long tmp; |
| 94 | int result; |
| 95 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 96 | smp_mb(); |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | __asm__ __volatile__("@ atomic_sub_return\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 99 | "1: ldrex %0, [%3]\n" |
| 100 | " sub %0, %0, %4\n" |
| 101 | " strex %1, %0, [%3]\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | " teq %1, #0\n" |
| 103 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 104 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | : "r" (&v->counter), "Ir" (i) |
| 106 | : "cc"); |
| 107 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 108 | smp_mb(); |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return result; |
| 111 | } |
| 112 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 113 | static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new) |
| 114 | { |
Russell King | 49ee57a | 2005-11-16 18:03:10 +0000 | [diff] [blame] | 115 | unsigned long oldval, res; |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 116 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 117 | smp_mb(); |
| 118 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 119 | do { |
| 120 | __asm__ __volatile__("@ atomic_cmpxchg\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 121 | "ldrex %1, [%3]\n" |
Nicolas Pitre | a7d0683 | 2005-11-16 15:05:11 +0000 | [diff] [blame] | 122 | "mov %0, #0\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 123 | "teq %1, %4\n" |
| 124 | "strexeq %0, %5, [%3]\n" |
| 125 | : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 126 | : "r" (&ptr->counter), "Ir" (old), "r" (new) |
| 127 | : "cc"); |
| 128 | } while (res); |
| 129 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 130 | smp_mb(); |
| 131 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 132 | return oldval; |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr) |
| 136 | { |
| 137 | unsigned long tmp, tmp2; |
| 138 | |
| 139 | __asm__ __volatile__("@ atomic_clear_mask\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 140 | "1: ldrex %0, [%3]\n" |
| 141 | " bic %0, %0, %4\n" |
| 142 | " strex %1, %0, [%3]\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | " teq %1, #0\n" |
| 144 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 145 | : "=&r" (tmp), "=&r" (tmp2), "+Qo" (*addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | : "r" (addr), "Ir" (mask) |
| 147 | : "cc"); |
| 148 | } |
| 149 | |
| 150 | #else /* ARM_ARCH_6 */ |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #ifdef CONFIG_SMP |
| 153 | #error SMP not supported on pre-ARMv6 CPUs |
| 154 | #endif |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | static inline int atomic_add_return(int i, atomic_t *v) |
| 157 | { |
| 158 | unsigned long flags; |
| 159 | int val; |
| 160 | |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 161 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | val = v->counter; |
| 163 | v->counter = val += i; |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 164 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | return val; |
| 167 | } |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 168 | #define atomic_add(i, v) (void) atomic_add_return(i, v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | static inline int atomic_sub_return(int i, atomic_t *v) |
| 171 | { |
| 172 | unsigned long flags; |
| 173 | int val; |
| 174 | |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 175 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | val = v->counter; |
| 177 | v->counter = val -= i; |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 178 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | return val; |
| 181 | } |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 182 | #define atomic_sub(i, v) (void) atomic_sub_return(i, v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 184 | static inline int atomic_cmpxchg(atomic_t *v, int old, int new) |
| 185 | { |
| 186 | int ret; |
| 187 | unsigned long flags; |
| 188 | |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 189 | raw_local_irq_save(flags); |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 190 | ret = v->counter; |
| 191 | if (likely(ret == old)) |
| 192 | v->counter = new; |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 193 | raw_local_irq_restore(flags); |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr) |
| 199 | { |
| 200 | unsigned long flags; |
| 201 | |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 202 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | *addr &= ~mask; |
Lennert Buytenhek | 8dd5c84 | 2006-09-16 10:47:18 +0100 | [diff] [blame] | 204 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | #endif /* __LINUX_ARM_ARCH__ */ |
| 208 | |
Ingo Molnar | ffbf670 | 2006-01-09 15:59:17 -0800 | [diff] [blame] | 209 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
| 210 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 211 | static inline int atomic_add_unless(atomic_t *v, int a, int u) |
| 212 | { |
| 213 | int c, old; |
| 214 | |
| 215 | c = atomic_read(v); |
| 216 | while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c) |
| 217 | c = old; |
| 218 | return c != u; |
| 219 | } |
| 220 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 221 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 222 | #define atomic_inc(v) atomic_add(1, v) |
| 223 | #define atomic_dec(v) atomic_sub(1, v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0) |
| 226 | #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0) |
| 227 | #define atomic_inc_return(v) (atomic_add_return(1, v)) |
| 228 | #define atomic_dec_return(v) (atomic_sub_return(1, v)) |
| 229 | #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0) |
| 230 | |
| 231 | #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0) |
| 232 | |
Russell King | bac4e96 | 2009-05-25 20:58:00 +0100 | [diff] [blame] | 233 | #define smp_mb__before_atomic_dec() smp_mb() |
| 234 | #define smp_mb__after_atomic_dec() smp_mb() |
| 235 | #define smp_mb__before_atomic_inc() smp_mb() |
| 236 | #define smp_mb__after_atomic_inc() smp_mb() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 238 | #ifndef CONFIG_GENERIC_ATOMIC64 |
| 239 | typedef struct { |
| 240 | u64 __aligned(8) counter; |
| 241 | } atomic64_t; |
| 242 | |
| 243 | #define ATOMIC64_INIT(i) { (i) } |
| 244 | |
| 245 | static inline u64 atomic64_read(atomic64_t *v) |
| 246 | { |
| 247 | u64 result; |
| 248 | |
| 249 | __asm__ __volatile__("@ atomic64_read\n" |
| 250 | " ldrexd %0, %H0, [%1]" |
| 251 | : "=&r" (result) |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 252 | : "r" (&v->counter), "Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 253 | ); |
| 254 | |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | static inline void atomic64_set(atomic64_t *v, u64 i) |
| 259 | { |
| 260 | u64 tmp; |
| 261 | |
| 262 | __asm__ __volatile__("@ atomic64_set\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 263 | "1: ldrexd %0, %H0, [%2]\n" |
| 264 | " strexd %0, %3, %H3, [%2]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 265 | " teq %0, #0\n" |
| 266 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 267 | : "=&r" (tmp), "=Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 268 | : "r" (&v->counter), "r" (i) |
| 269 | : "cc"); |
| 270 | } |
| 271 | |
| 272 | static inline void atomic64_add(u64 i, atomic64_t *v) |
| 273 | { |
| 274 | u64 result; |
| 275 | unsigned long tmp; |
| 276 | |
| 277 | __asm__ __volatile__("@ atomic64_add\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 278 | "1: ldrexd %0, %H0, [%3]\n" |
| 279 | " adds %0, %0, %4\n" |
| 280 | " adc %H0, %H0, %H4\n" |
| 281 | " strexd %1, %0, %H0, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 282 | " teq %1, #0\n" |
| 283 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 284 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 285 | : "r" (&v->counter), "r" (i) |
| 286 | : "cc"); |
| 287 | } |
| 288 | |
| 289 | static inline u64 atomic64_add_return(u64 i, atomic64_t *v) |
| 290 | { |
| 291 | u64 result; |
| 292 | unsigned long tmp; |
| 293 | |
| 294 | smp_mb(); |
| 295 | |
| 296 | __asm__ __volatile__("@ atomic64_add_return\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 297 | "1: ldrexd %0, %H0, [%3]\n" |
| 298 | " adds %0, %0, %4\n" |
| 299 | " adc %H0, %H0, %H4\n" |
| 300 | " strexd %1, %0, %H0, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 301 | " teq %1, #0\n" |
| 302 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 303 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 304 | : "r" (&v->counter), "r" (i) |
| 305 | : "cc"); |
| 306 | |
| 307 | smp_mb(); |
| 308 | |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | static inline void atomic64_sub(u64 i, atomic64_t *v) |
| 313 | { |
| 314 | u64 result; |
| 315 | unsigned long tmp; |
| 316 | |
| 317 | __asm__ __volatile__("@ atomic64_sub\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 318 | "1: ldrexd %0, %H0, [%3]\n" |
| 319 | " subs %0, %0, %4\n" |
| 320 | " sbc %H0, %H0, %H4\n" |
| 321 | " strexd %1, %0, %H0, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 322 | " teq %1, #0\n" |
| 323 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 324 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 325 | : "r" (&v->counter), "r" (i) |
| 326 | : "cc"); |
| 327 | } |
| 328 | |
| 329 | static inline u64 atomic64_sub_return(u64 i, atomic64_t *v) |
| 330 | { |
| 331 | u64 result; |
| 332 | unsigned long tmp; |
| 333 | |
| 334 | smp_mb(); |
| 335 | |
| 336 | __asm__ __volatile__("@ atomic64_sub_return\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 337 | "1: ldrexd %0, %H0, [%3]\n" |
| 338 | " subs %0, %0, %4\n" |
| 339 | " sbc %H0, %H0, %H4\n" |
| 340 | " strexd %1, %0, %H0, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 341 | " teq %1, #0\n" |
| 342 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 343 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 344 | : "r" (&v->counter), "r" (i) |
| 345 | : "cc"); |
| 346 | |
| 347 | smp_mb(); |
| 348 | |
| 349 | return result; |
| 350 | } |
| 351 | |
| 352 | static inline u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old, u64 new) |
| 353 | { |
| 354 | u64 oldval; |
| 355 | unsigned long res; |
| 356 | |
| 357 | smp_mb(); |
| 358 | |
| 359 | do { |
| 360 | __asm__ __volatile__("@ atomic64_cmpxchg\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 361 | "ldrexd %1, %H1, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 362 | "mov %0, #0\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 363 | "teq %1, %4\n" |
| 364 | "teqeq %H1, %H4\n" |
| 365 | "strexdeq %0, %5, %H5, [%3]" |
| 366 | : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 367 | : "r" (&ptr->counter), "r" (old), "r" (new) |
| 368 | : "cc"); |
| 369 | } while (res); |
| 370 | |
| 371 | smp_mb(); |
| 372 | |
| 373 | return oldval; |
| 374 | } |
| 375 | |
| 376 | static inline u64 atomic64_xchg(atomic64_t *ptr, u64 new) |
| 377 | { |
| 378 | u64 result; |
| 379 | unsigned long tmp; |
| 380 | |
| 381 | smp_mb(); |
| 382 | |
| 383 | __asm__ __volatile__("@ atomic64_xchg\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 384 | "1: ldrexd %0, %H0, [%3]\n" |
| 385 | " strexd %1, %4, %H4, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 386 | " teq %1, #0\n" |
| 387 | " bne 1b" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 388 | : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 389 | : "r" (&ptr->counter), "r" (new) |
| 390 | : "cc"); |
| 391 | |
| 392 | smp_mb(); |
| 393 | |
| 394 | return result; |
| 395 | } |
| 396 | |
| 397 | static inline u64 atomic64_dec_if_positive(atomic64_t *v) |
| 398 | { |
| 399 | u64 result; |
| 400 | unsigned long tmp; |
| 401 | |
| 402 | smp_mb(); |
| 403 | |
| 404 | __asm__ __volatile__("@ atomic64_dec_if_positive\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 405 | "1: ldrexd %0, %H0, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 406 | " subs %0, %0, #1\n" |
| 407 | " sbc %H0, %H0, #0\n" |
| 408 | " teq %H0, #0\n" |
| 409 | " bmi 2f\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 410 | " strexd %1, %0, %H0, [%3]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 411 | " teq %1, #0\n" |
| 412 | " bne 1b\n" |
| 413 | "2:" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 414 | : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 415 | : "r" (&v->counter) |
| 416 | : "cc"); |
| 417 | |
| 418 | smp_mb(); |
| 419 | |
| 420 | return result; |
| 421 | } |
| 422 | |
| 423 | static inline int atomic64_add_unless(atomic64_t *v, u64 a, u64 u) |
| 424 | { |
| 425 | u64 val; |
| 426 | unsigned long tmp; |
| 427 | int ret = 1; |
| 428 | |
| 429 | smp_mb(); |
| 430 | |
| 431 | __asm__ __volatile__("@ atomic64_add_unless\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 432 | "1: ldrexd %0, %H0, [%4]\n" |
| 433 | " teq %0, %5\n" |
| 434 | " teqeq %H0, %H5\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 435 | " moveq %1, #0\n" |
| 436 | " beq 2f\n" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 437 | " adds %0, %0, %6\n" |
| 438 | " adc %H0, %H0, %H6\n" |
| 439 | " strexd %2, %0, %H0, [%4]\n" |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 440 | " teq %2, #0\n" |
| 441 | " bne 1b\n" |
| 442 | "2:" |
Will Deacon | 398aa66 | 2010-07-08 10:59:16 +0100 | [diff] [blame] | 443 | : "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter) |
Will Deacon | 24b44a6 | 2010-01-20 19:05:07 +0100 | [diff] [blame] | 444 | : "r" (&v->counter), "r" (u), "r" (a) |
| 445 | : "cc"); |
| 446 | |
| 447 | if (ret) |
| 448 | smp_mb(); |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0) |
| 454 | #define atomic64_inc(v) atomic64_add(1LL, (v)) |
| 455 | #define atomic64_inc_return(v) atomic64_add_return(1LL, (v)) |
| 456 | #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0) |
| 457 | #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0) |
| 458 | #define atomic64_dec(v) atomic64_sub(1LL, (v)) |
| 459 | #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v)) |
| 460 | #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0) |
| 461 | #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL) |
| 462 | |
| 463 | #else /* !CONFIG_GENERIC_ATOMIC64 */ |
| 464 | #include <asm-generic/atomic64.h> |
| 465 | #endif |
Arnd Bergmann | 72099ed | 2009-05-13 22:56:29 +0000 | [diff] [blame] | 466 | #include <asm-generic/atomic-long.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | #endif |
| 468 | #endif |