2 * linux/include/asm-arm/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/config.h>
16 typedef struct { volatile int counter; } atomic_t;
18 #define ATOMIC_INIT(i) { (i) }
22 #define atomic_read(v) ((v)->counter)
24 #if __LINUX_ARM_ARCH__ >= 6
27 * ARMv6 UP and SMP safe atomic ops. We use load exclusive and
28 * store exclusive to ensure that these are atomic. We may loop
29 * to ensure that the update happens. Writing to 'v->counter'
30 * without using the following operations WILL break the atomic
31 * nature of these ops.
33 static inline void atomic_set(atomic_t *v, int i)
37 __asm__ __volatile__("@ atomic_set\n"
39 " strex %0, %2, [%1]\n"
43 : "r" (&v->counter), "r" (i)
47 static inline int atomic_add_return(int i, atomic_t *v)
52 __asm__ __volatile__("@ atomic_add_return\n"
55 " strex %1, %0, [%2]\n"
58 : "=&r" (result), "=&r" (tmp)
59 : "r" (&v->counter), "Ir" (i)
65 static inline int atomic_sub_return(int i, atomic_t *v)
70 __asm__ __volatile__("@ atomic_sub_return\n"
73 " strex %1, %0, [%2]\n"
76 : "=&r" (result), "=&r" (tmp)
77 : "r" (&v->counter), "Ir" (i)
83 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
85 unsigned long tmp, tmp2;
87 __asm__ __volatile__("@ atomic_clear_mask\n"
93 : "=&r" (tmp), "=&r" (tmp2)
94 : "r" (addr), "Ir" (mask)
98 #else /* ARM_ARCH_6 */
100 #include <asm/system.h>
103 #error SMP not supported on pre-ARMv6 CPUs
106 #define atomic_set(v,i) (((v)->counter) = (i))
108 static inline int atomic_add_return(int i, atomic_t *v)
113 local_irq_save(flags);
115 v->counter = val += i;
116 local_irq_restore(flags);
121 static inline int atomic_sub_return(int i, atomic_t *v)
126 local_irq_save(flags);
128 v->counter = val -= i;
129 local_irq_restore(flags);
134 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
138 local_irq_save(flags);
140 local_irq_restore(flags);
143 #endif /* __LINUX_ARM_ARCH__ */
145 #define atomic_add(i, v) (void) atomic_add_return(i, v)
146 #define atomic_inc(v) (void) atomic_add_return(1, v)
147 #define atomic_sub(i, v) (void) atomic_sub_return(i, v)
148 #define atomic_dec(v) (void) atomic_sub_return(1, v)
150 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
151 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
152 #define atomic_inc_return(v) (atomic_add_return(1, v))
153 #define atomic_dec_return(v) (atomic_sub_return(1, v))
154 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
156 #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
158 /* Atomic operations are already serializing on ARM */
159 #define smp_mb__before_atomic_dec() barrier()
160 #define smp_mb__after_atomic_dec() barrier()
161 #define smp_mb__before_atomic_inc() barrier()
162 #define smp_mb__after_atomic_inc() barrier()