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/compiler.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 int atomic_cmpxchg(atomic_t *ptr, int old, int new)
85 unsigned long oldval, res;
88 __asm__ __volatile__("@ atomic_cmpxchg\n"
92 "strexeq %0, %4, [%2]\n"
93 : "=&r" (res), "=&r" (oldval)
94 : "r" (&ptr->counter), "Ir" (old), "r" (new)
101 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
103 unsigned long tmp, tmp2;
105 __asm__ __volatile__("@ atomic_clear_mask\n"
108 " strex %1, %0, %2\n"
111 : "=&r" (tmp), "=&r" (tmp2)
112 : "r" (addr), "Ir" (mask)
116 #else /* ARM_ARCH_6 */
118 #include <asm/system.h>
121 #error SMP not supported on pre-ARMv6 CPUs
124 #define atomic_set(v,i) (((v)->counter) = (i))
126 static inline int atomic_add_return(int i, atomic_t *v)
131 raw_local_irq_save(flags);
133 v->counter = val += i;
134 raw_local_irq_restore(flags);
139 static inline int atomic_sub_return(int i, atomic_t *v)
144 raw_local_irq_save(flags);
146 v->counter = val -= i;
147 raw_local_irq_restore(flags);
152 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
157 raw_local_irq_save(flags);
159 if (likely(ret == old))
161 raw_local_irq_restore(flags);
166 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
170 raw_local_irq_save(flags);
172 raw_local_irq_restore(flags);
175 #endif /* __LINUX_ARM_ARCH__ */
177 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
179 static inline int atomic_add_unless(atomic_t *v, int a, int u)
184 while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
188 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
190 #define atomic_add(i, v) (void) atomic_add_return(i, v)
191 #define atomic_inc(v) (void) atomic_add_return(1, v)
192 #define atomic_sub(i, v) (void) atomic_sub_return(i, v)
193 #define atomic_dec(v) (void) atomic_sub_return(1, v)
195 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
196 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
197 #define atomic_inc_return(v) (atomic_add_return(1, v))
198 #define atomic_dec_return(v) (atomic_sub_return(1, v))
199 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
201 #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
203 /* Atomic operations are already serializing on ARM */
204 #define smp_mb__before_atomic_dec() barrier()
205 #define smp_mb__after_atomic_dec() barrier()
206 #define smp_mb__before_atomic_inc() barrier()
207 #define smp_mb__after_atomic_inc() barrier()
209 #include <asm-generic/atomic.h>