ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm / atomic.h
1 /*
2  *  linux/include/asm-arm/atomic.h
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
14 #include <linux/config.h>
15
16 typedef struct { volatile int counter; } atomic_t;
17
18 #define ATOMIC_INIT(i)  { (i) }
19
20 #ifdef __KERNEL__
21
22 #define atomic_read(v)  ((v)->counter)
23
24 #if __LINUX_ARM_ARCH__ >= 6
25
26 /*
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.
32  */
33 static inline void atomic_set(atomic_t *v, int i)
34 {
35         unsigned long tmp;
36
37         __asm__ __volatile__("@ atomic_set\n"
38 "1:     ldrex   %0, [%1]\n"
39 "       strex   %0, %2, [%1]\n"
40 "       teq     %0, #0\n"
41 "       bne     1b"
42         : "=&r" (tmp)
43         : "r" (&v->counter), "r" (i)
44         : "cc");
45 }
46
47 static inline void atomic_add(int i, atomic_t *v)
48 {
49         unsigned long tmp, tmp2;
50
51         __asm__ __volatile__("@ atomic_add\n"
52 "1:     ldrex   %0, [%2]\n"
53 "       add     %0, %0, %3\n"
54 "       strex   %1, %0, [%2]\n"
55 "       teq     %1, #0\n"
56 "       bne     1b"
57         : "=&r" (tmp), "=&r" (tmp2)
58         : "r" (&v->counter), "Ir" (i)
59         : "cc");
60 }
61
62 static inline void atomic_sub(int i, atomic_t *v)
63 {
64         unsigned long tmp, tmp2;
65
66         __asm__ __volatile__("@ atomic_sub\n"
67 "1:     ldrex   %0, [%2]\n"
68 "       sub     %0, %0, %3\n"
69 "       strex   %1, %0, [%2]\n"
70 "       teq     %1, #0\n"
71 "       bne     1b"
72         : "=&r" (tmp), "=&r" (tmp2)
73         : "r" (&v->counter), "Ir" (i)
74         : "cc");
75 }
76
77 #define atomic_inc(v)   atomic_add(1, v)
78 #define atomic_dec(v)   atomic_sub(1, v)
79
80 static inline int atomic_dec_and_test(atomic_t *v)
81 {
82         unsigned long tmp;
83         int result;
84
85         __asm__ __volatile__("@ atomic_dec_and_test\n"
86 "1:     ldrex   %0, [%2]\n"
87 "       sub     %0, %0, #1\n"
88 "       strex   %1, %0, [%2]\n"
89 "       teq     %1, #0\n"
90 "       bne     1b"
91         : "=&r" (result), "=&r" (tmp)
92         : "r" (&v->counter)
93         : "cc");
94
95         return result == 0;
96 }
97
98 static inline int atomic_add_negative(int i, atomic_t *v)
99 {
100         unsigned long tmp;
101         int result;
102
103         __asm__ __volatile__("@ atomic_add_negative\n"
104 "1:     ldrex   %0, [%2]\n"
105 "       add     %0, %0, %3\n"
106 "       strex   %1, %0, [%2]\n"
107 "       teq     %1, #0\n"
108 "       bne     1b"
109         : "=&r" (result), "=&r" (tmp)
110         : "r" (&v->counter), "Ir" (i)
111         : "cc");
112
113         return result < 0;
114 }
115
116 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
117 {
118         unsigned long tmp, tmp2;
119
120         __asm__ __volatile__("@ atomic_clear_mask\n"
121 "1:     ldrex   %0, %2\n"
122 "       bic     %0, %0, %3\n"
123 "       strex   %1, %0, %2\n"
124 "       teq     %1, #0\n"
125 "       bne     1b"
126         : "=&r" (tmp), "=&r" (tmp2)
127         : "r" (addr), "Ir" (mask)
128         : "cc");
129 }
130
131 #else /* ARM_ARCH_6 */
132
133 #include <asm/system.h>
134
135 #ifdef CONFIG_SMP
136 #error SMP not supported on pre-ARMv6 CPUs
137 #endif
138
139 #define atomic_set(v,i) (((v)->counter) = (i))
140
141 static inline void atomic_add(int i, atomic_t *v)
142 {
143         unsigned long flags;
144
145         local_irq_save(flags);
146         v->counter += i;
147         local_irq_restore(flags);
148 }
149
150 static inline void atomic_sub(int i, atomic_t *v)
151 {
152         unsigned long flags;
153
154         local_irq_save(flags);
155         v->counter -= i;
156         local_irq_restore(flags);
157 }
158
159 static inline void atomic_inc(atomic_t *v)
160 {
161         unsigned long flags;
162
163         local_irq_save(flags);
164         v->counter += 1;
165         local_irq_restore(flags);
166 }
167
168 static inline void atomic_dec(atomic_t *v)
169 {
170         unsigned long flags;
171
172         local_irq_save(flags);
173         v->counter -= 1;
174         local_irq_restore(flags);
175 }
176
177 static inline int atomic_dec_and_test(atomic_t *v)
178 {
179         unsigned long flags;
180         int val;
181
182         local_irq_save(flags);
183         val = v->counter;
184         v->counter = val -= 1;
185         local_irq_restore(flags);
186
187         return val == 0;
188 }
189
190 static inline int atomic_add_negative(int i, atomic_t *v)
191 {
192         unsigned long flags;
193         int val;
194
195         local_irq_save(flags);
196         val = v->counter;
197         v->counter = val += i;
198         local_irq_restore(flags);
199
200         return val < 0;
201 }
202
203 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
204 {
205         unsigned long flags;
206
207         local_irq_save(flags);
208         *addr &= ~mask;
209         local_irq_restore(flags);
210 }
211
212 #endif /* __LINUX_ARM_ARCH__ */
213
214 /* Atomic operations are already serializing on ARM */
215 #define smp_mb__before_atomic_dec()     barrier()
216 #define smp_mb__after_atomic_dec()      barrier()
217 #define smp_mb__before_atomic_inc()     barrier()
218 #define smp_mb__after_atomic_inc()      barrier()
219
220 #endif
221 #endif