2 * arch/arm/include/asm/atomic.h
4 * Copyright (C) 1996 Russell King.
5 * Copyright (C) 2002 Deep Blue Solutions Ltd.
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.
11 #ifndef __ASM_ARM_ATOMIC_H
12 #define __ASM_ARM_ATOMIC_H
14 #include <linux/compiler.h>
15 #include <linux/types.h>
16 #include <asm/system.h>
18 #define ATOMIC_INIT(i) { (i) }
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.
27 #define atomic_read(v) (*(volatile int *)&(v)->counter)
28 #define atomic_set(v,i) (((v)->counter) = (i))
30 #if __LINUX_ARM_ARCH__ >= 6
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
35 * to ensure that the update happens.
37 static inline void atomic_add(int i, atomic_t *v)
42 __asm__ __volatile__("@ atomic_add\n"
45 " strex %1, %0, [%3]\n"
48 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
49 : "r" (&v->counter), "Ir" (i)
53 static inline int atomic_add_return(int i, atomic_t *v)
60 __asm__ __volatile__("@ atomic_add_return\n"
63 " strex %1, %0, [%3]\n"
66 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
67 : "r" (&v->counter), "Ir" (i)
75 static inline void atomic_sub(int i, atomic_t *v)
80 __asm__ __volatile__("@ atomic_sub\n"
83 " strex %1, %0, [%3]\n"
86 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
87 : "r" (&v->counter), "Ir" (i)
91 static inline int atomic_sub_return(int i, atomic_t *v)
98 __asm__ __volatile__("@ atomic_sub_return\n"
101 " strex %1, %0, [%3]\n"
104 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
105 : "r" (&v->counter), "Ir" (i)
113 static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
115 unsigned long oldval, res;
120 __asm__ __volatile__("@ atomic_cmpxchg\n"
124 "strexeq %0, %5, [%3]\n"
125 : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
126 : "r" (&ptr->counter), "Ir" (old), "r" (new)
135 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
137 unsigned long tmp, tmp2;
139 __asm__ __volatile__("@ atomic_clear_mask\n"
140 "1: ldrex %0, [%3]\n"
142 " strex %1, %0, [%3]\n"
145 : "=&r" (tmp), "=&r" (tmp2), "+Qo" (*addr)
146 : "r" (addr), "Ir" (mask)
150 #else /* ARM_ARCH_6 */
153 #error SMP not supported on pre-ARMv6 CPUs
156 static inline int atomic_add_return(int i, atomic_t *v)
161 raw_local_irq_save(flags);
163 v->counter = val += i;
164 raw_local_irq_restore(flags);
168 #define atomic_add(i, v) (void) atomic_add_return(i, v)
170 static inline int atomic_sub_return(int i, atomic_t *v)
175 raw_local_irq_save(flags);
177 v->counter = val -= i;
178 raw_local_irq_restore(flags);
182 #define atomic_sub(i, v) (void) atomic_sub_return(i, v)
184 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
189 raw_local_irq_save(flags);
191 if (likely(ret == old))
193 raw_local_irq_restore(flags);
198 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
202 raw_local_irq_save(flags);
204 raw_local_irq_restore(flags);
207 #endif /* __LINUX_ARM_ARCH__ */
209 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
211 static inline int atomic_add_unless(atomic_t *v, int a, int u)
216 while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
220 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
222 #define atomic_inc(v) atomic_add(1, v)
223 #define atomic_dec(v) atomic_sub(1, v)
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)
231 #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
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()
238 #ifndef CONFIG_GENERIC_ATOMIC64
240 u64 __aligned(8) counter;
243 #define ATOMIC64_INIT(i) { (i) }
245 static inline u64 atomic64_read(atomic64_t *v)
249 __asm__ __volatile__("@ atomic64_read\n"
250 " ldrexd %0, %H0, [%1]"
252 : "r" (&v->counter), "Qo" (v->counter)
258 static inline void atomic64_set(atomic64_t *v, u64 i)
262 __asm__ __volatile__("@ atomic64_set\n"
263 "1: ldrexd %0, %H0, [%2]\n"
264 " strexd %0, %3, %H3, [%2]\n"
267 : "=&r" (tmp), "=Qo" (v->counter)
268 : "r" (&v->counter), "r" (i)
272 static inline void atomic64_add(u64 i, atomic64_t *v)
277 __asm__ __volatile__("@ atomic64_add\n"
278 "1: ldrexd %0, %H0, [%3]\n"
280 " adc %H0, %H0, %H4\n"
281 " strexd %1, %0, %H0, [%3]\n"
284 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
285 : "r" (&v->counter), "r" (i)
289 static inline u64 atomic64_add_return(u64 i, atomic64_t *v)
296 __asm__ __volatile__("@ atomic64_add_return\n"
297 "1: ldrexd %0, %H0, [%3]\n"
299 " adc %H0, %H0, %H4\n"
300 " strexd %1, %0, %H0, [%3]\n"
303 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
304 : "r" (&v->counter), "r" (i)
312 static inline void atomic64_sub(u64 i, atomic64_t *v)
317 __asm__ __volatile__("@ atomic64_sub\n"
318 "1: ldrexd %0, %H0, [%3]\n"
320 " sbc %H0, %H0, %H4\n"
321 " strexd %1, %0, %H0, [%3]\n"
324 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
325 : "r" (&v->counter), "r" (i)
329 static inline u64 atomic64_sub_return(u64 i, atomic64_t *v)
336 __asm__ __volatile__("@ atomic64_sub_return\n"
337 "1: ldrexd %0, %H0, [%3]\n"
339 " sbc %H0, %H0, %H4\n"
340 " strexd %1, %0, %H0, [%3]\n"
343 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
344 : "r" (&v->counter), "r" (i)
352 static inline u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old, u64 new)
360 __asm__ __volatile__("@ atomic64_cmpxchg\n"
361 "ldrexd %1, %H1, [%3]\n"
365 "strexdeq %0, %5, %H5, [%3]"
366 : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
367 : "r" (&ptr->counter), "r" (old), "r" (new)
376 static inline u64 atomic64_xchg(atomic64_t *ptr, u64 new)
383 __asm__ __volatile__("@ atomic64_xchg\n"
384 "1: ldrexd %0, %H0, [%3]\n"
385 " strexd %1, %4, %H4, [%3]\n"
388 : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
389 : "r" (&ptr->counter), "r" (new)
397 static inline u64 atomic64_dec_if_positive(atomic64_t *v)
404 __asm__ __volatile__("@ atomic64_dec_if_positive\n"
405 "1: ldrexd %0, %H0, [%3]\n"
407 " sbc %H0, %H0, #0\n"
410 " strexd %1, %0, %H0, [%3]\n"
414 : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
423 static inline int atomic64_add_unless(atomic64_t *v, u64 a, u64 u)
431 __asm__ __volatile__("@ atomic64_add_unless\n"
432 "1: ldrexd %0, %H0, [%4]\n"
438 " adc %H0, %H0, %H6\n"
439 " strexd %2, %0, %H0, [%4]\n"
443 : "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter)
444 : "r" (&v->counter), "r" (u), "r" (a)
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)
463 #else /* !CONFIG_GENERIC_ATOMIC64 */
464 #include <asm-generic/atomic64.h>
466 #include <asm-generic/atomic-long.h>