Debian Codegraph @

ulib/arch/arm/atomic.h

raw
1 //#include <linux/irqflags.h>
2 //#include <linux/prefetch.h>
3 //#include <asm/barrier.h>
4
5 #if defined(CONFIG_CPU_SA1100) || defined(CONFIG_CPU_SA110)
6 /*
7 * On the StrongARM, "swp" is terminally broken since it bypasses the
8 * cache totally. This means that the cache becomes inconsistent, and,
9 * since we use normal loads/stores as well, this is really bad.
10 * Typically, this causes oopsen in filp_close, but could have other,
11 * more disastrous effects. There are two work-arounds:
12 * 1. Disable interrupts and emulate the atomic swap
13 * 2. Clean the cache, perform atomic swap, flush the cache
14 *
15 * We choose (1) since its the "easiest" to achieve here and is not
16 * dependent on the processor type.
17 *
18 * NOTE that this solution won't work on an SMP system, so explcitly
19 * forbid it here.
20 */
21 #error "StrongARM unimplemented"
22 #define swp_is_buggy
23 #endif
24
25 #define barrier() asm volatile ("" ::: "memory")
26 #define smp_mb() barrier()
27 #define prefetchw(x) __builtin_prefetch(x,1)
28
29 extern void __bad_xchg(volatile void *, int);
30
31 static inline unsigned long __xchg(
32 unsigned long x, volatile void *ptr, int size
33 ) {
34 unsigned long ret;
35 #ifdef swp_is_buggy
36 unsigned long flags;
37 #endif
38 #if __ARM_ARCH >= 6
39 unsigned int tmp;
40 #endif
41
42 smp_mb();
43 prefetchw((const void *)ptr);
44
45 switch (size) {
46 #if __ARM_ARCH >= 6
47 case 1:
48 asm volatile("@ __xchg1\n"
49 "1: ldrexb %0, [%3]\n"
50 " strexb %1, %2, [%3]\n"
51 " teq %1, #0\n"
52 " bne 1b"
53 : "=&r" (ret), "=&r" (tmp)
54 : "r" (x), "r" (ptr)
55 : "memory", "cc");
56 break;
57 case 4:
58 asm volatile("@ __xchg4\n"
59 "1: ldrex %0, [%3]\n"
60 " strex %1, %2, [%3]\n"
61 " teq %1, #0\n"
62 " bne 1b"
63 : "=&r" (ret), "=&r" (tmp)
64 : "r" (x), "r" (ptr)
65 : "memory", "cc");
66 break;
67 #elif defined(swp_is_buggy)
68 #ifdef CONFIG_SMP
69 #error SMP is not supported on this platform
70 #endif
71 case 1:
72 raw_local_irq_save(flags);
73 ret = *(volatile unsigned char *)ptr;
74 *(volatile unsigned char *)ptr = x;
75 raw_local_irq_restore(flags);
76 break;
77
78 case 4:
79 raw_local_irq_save(flags);
80 ret = *(volatile unsigned long *)ptr;
81 *(volatile unsigned long *)ptr = x;
82 raw_local_irq_restore(flags);
83 break;
84 #else
85 case 1:
86 asm volatile("@ __xchg1\n"
87 " swpb %0, %1, [%2]"
88 : "=&r" (ret)
89 : "r" (x), "r" (ptr)
90 : "memory", "cc");
91 break;
92 case 4:
93 asm volatile("@ __xchg4\n"
94 " swp %0, %1, [%2]"
95 : "=&r" (ret)
96 : "r" (x), "r" (ptr)
97 : "memory", "cc");
98 break;
99 #endif
100 default:
101 __bad_xchg(ptr, size), ret = 0;
102 break;
103 }
104 smp_mb();
105
106 return ret;
107 }
108
109 #define xchg(ptr,x) \
110 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
111
112 //#include <asm-generic/cmpxchg-local.h>
113
114 #if __ARM_ARCH < 6
115 /* min ARCH < ARMv6 */
116
117 #ifdef CONFIG_SMP
118 #error "SMP is not supported on this platform"
119 #endif
120
121 /*
122 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
123 * them available.
124 #define cmpxchg_local(ptr, o, n) \
125 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
126 (unsigned long)(n), sizeof(*(ptr))))
127 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
128 */
129
130 #ifndef CONFIG_SMP
131 //#include <asm-generic/cmpxchg.h>
132 #endif
133
134 #else /* min ARCH >= ARMv6 */
135
136 extern void __bad_cmpxchg(volatile void *ptr, int size);
137
138 /*
139 * cmpxchg only support 32-bits operands on ARMv6.
140 */
141
142 static inline unsigned long __cmpxchg(
143 volatile void *ptr, unsigned long old, unsigned long new, int size
144 ) {
145 unsigned long oldval, res;
146
147 prefetchw((const void *)ptr);
148
149 switch (size) {
150 #ifndef CONFIG_CPU_V6 /* min ARCH >= ARMv6K */
151 case 1:
152 do {
153 asm volatile("@ __cmpxchg1\n"
154 " ldrexb %1, [%2]\n"
155 " mov %0, #0\n"
156 " teq %1, %3\n"
157 #ifdef __thumb__
158 " IT eq\n"
159 #endif
160 " strexbeq %0, %4, [%2]\n"
161 : "=&r" (res), "=&r" (oldval)
162 : "r" (ptr), "Ir" (old), "r" (new)
163 : "memory", "cc");
164 } while (res);
165 break;
166 case 2:
167 do {
168 asm volatile("@ __cmpxchg1\n"
169 " ldrexh %1, [%2]\n"
170 " mov %0, #0\n"
171 " teq %1, %3\n"
172 #ifdef __thumb__
173 " IT eq\n"
174 #endif
175 " strexheq %0, %4, [%2]\n"
176 : "=&r" (res), "=&r" (oldval)
177 : "r" (ptr), "Ir" (old), "r" (new)
178 : "memory", "cc");
179 } while (res);
180 break;
181 #endif
182 case 4:
183 do {
184 asm volatile("@ __cmpxchg4\n"
185 " ldrex %1, [%2]\n"
186 " mov %0, #0\n"
187 " teq %1, %3\n"
188 #ifdef __thumb__
189 " IT eq\n"
190 #endif
191 " strexeq %0, %4, [%2]\n"
192 : "=&r" (res), "=&r" (oldval)
193 : "r" (ptr), "Ir" (old), "r" (new)
194 : "memory", "cc");
195 } while (res);
196 break;
197 default:
198 __bad_cmpxchg(ptr, size);
199 oldval = 0;
200 }
201
202 return oldval;
203 }
204
205 static inline unsigned long __cmpxchg_mb(
206 volatile void *ptr, unsigned long old, unsigned long new, int size
207 ) {
208 unsigned long ret;
209
210 smp_mb();
211 ret = __cmpxchg(ptr, old, new, size);
212 smp_mb();
213
214 return ret;
215 }
216
217 #define cmpxchg(ptr,o,n) ( \
218 (__typeof__(*(ptr)))__cmpxchg_mb( \
219 (ptr), (unsigned long)(o), (unsigned long)(n), sizeof(*(ptr)) \
220 ) \
221 )
222
223 /*
224 static inline unsigned long __cmpxchg_local(
225 volatile void *ptr, unsigned long old, unsigned long new, int size
226 ) {
227 unsigned long ret;
228
229 switch (size) {
230 #ifdef CONFIG_CPU_V6 *//* min ARCH == ARMv6 *//*
231 case 1:
232 case 2:
233 ret = __cmpxchg_local_generic(ptr, old, new, size);
234 break;
235 #endif
236 default:
237 ret = __cmpxchg(ptr, old, new, size);
238 }
239
240 return ret;
241 }
242
243 static inline unsigned long long __cmpxchg64(
244 unsigned long long *ptr, unsigned long long old, unsigned long long new
245 ) {
246 unsigned long long oldval;
247 unsigned long res;
248
249 prefetchw(ptr);
250
251 __asm__ __volatile__(
252 "1: ldrexd %1, %H1, [%3]\n"
253 " teq %1, %4\n"
254 " teqeq %H1, %H4\n"
255 " bne 2f\n"
256 " strexd %0, %5, %H5, [%3]\n"
257 " teq %0, #0\n"
258 " bne 1b\n"
259 "2:"
260 : "=&r" (res), "=&r" (oldval), "+Qo" (*ptr)
261 : "r" (ptr), "r" (old), "r" (new)
262 : "cc"
263 );
264
265 return oldval;
266 }
267
268 static inline unsigned long long __cmpxchg64_mb(
269 unsigned long long *ptr, unsigned long long old, unsigned long long new
270 ) {
271 unsigned long long ret;
272
273 smp_mb();
274 ret = __cmpxchg64(ptr, old, new);
275 smp_mb();
276
277 return ret;
278 }
279
280 #define cmpxchg_local(ptr,o,n) \
281 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), \
282 (unsigned long)(o), \
283 (unsigned long)(n), \
284 sizeof(*(ptr))))
285
286 #define cmpxchg64(ptr, o, n) \
287 ((__typeof__(*(ptr)))__cmpxchg64_mb((ptr), \
288 (unsigned long long)(o), \
289 (unsigned long long)(n)))
290
291 #define cmpxchg64_relaxed(ptr, o, n) \
292 ((__typeof__(*(ptr)))__cmpxchg64((ptr), \
293 (unsigned long long)(o), \
294 (unsigned long long)(n)))
295
296 #define cmpxchg64_local(ptr, o, n) cmpxchg64_relaxed((ptr), (o), (n))
297 */
298
299 #endif /* __ARM_ARCH >= 6 */
300
301 #define xdec(ptr) ({ \
302 __typeof__(ptr) _ptr = (ptr); \
303 __typeof__(*(ptr)) _ret, _old; \
304 do { \
305 _old = *_ptr; \
306 _ret = cmpxchg(_ptr, _old, _old-1); \
307 } while (_ret != _old); \
308 _ret; \
309 })