1 #ifndef __ASM_ARM_CMPXCHG_H
2 #define __ASM_ARM_CMPXCHG_H
4 #include <linux/irqflags.h>
5 #include <linux/prefetch.h>
6 #include <asm/barrier.h>
8 #if defined(CONFIG_CPU_SA1100) || defined(CONFIG_CPU_SA110)
10 * On the StrongARM, "swp" is terminally broken since it bypasses the
11 * cache totally. This means that the cache becomes inconsistent, and,
12 * since we use normal loads/stores as well, this is really bad.
13 * Typically, this causes oopsen in filp_close, but could have other,
14 * more disastrous effects. There are two work-arounds:
15 * 1. Disable interrupts and emulate the atomic swap
16 * 2. Clean the cache, perform atomic swap, flush the cache
18 * We choose (1) since its the "easiest" to achieve here and is not
19 * dependent on the processor type.
21 * NOTE that this solution won't work on an SMP system, so explcitly
27 static inline unsigned long __xchg(unsigned long x
, volatile void *ptr
, int size
)
29 extern void __bad_xchg(volatile void *, int);
34 #if __LINUX_ARM_ARCH__ >= 6
39 prefetchw((const void *)ptr
);
42 #if __LINUX_ARM_ARCH__ >= 6
44 asm volatile("@ __xchg1\n"
45 "1: ldrexb %0, [%3]\n"
46 " strexb %1, %2, [%3]\n"
49 : "=&r" (ret
), "=&r" (tmp
)
54 asm volatile("@ __xchg4\n"
56 " strex %1, %2, [%3]\n"
59 : "=&r" (ret
), "=&r" (tmp
)
63 #elif defined(swp_is_buggy)
65 #error SMP is not supported on this platform
68 raw_local_irq_save(flags
);
69 ret
= *(volatile unsigned char *)ptr
;
70 *(volatile unsigned char *)ptr
= x
;
71 raw_local_irq_restore(flags
);
75 raw_local_irq_save(flags
);
76 ret
= *(volatile unsigned long *)ptr
;
77 *(volatile unsigned long *)ptr
= x
;
78 raw_local_irq_restore(flags
);
82 asm volatile("@ __xchg1\n"
89 asm volatile("@ __xchg4\n"
97 __bad_xchg(ptr
, size
), ret
= 0;
105 #define xchg(ptr,x) \
106 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
108 #include <asm-generic/cmpxchg-local.h>
110 #if __LINUX_ARM_ARCH__ < 6
111 /* min ARCH < ARMv6 */
114 #error "SMP is not supported on this platform"
118 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
121 #define cmpxchg_local(ptr, o, n) \
122 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
123 (unsigned long)(n), sizeof(*(ptr))))
124 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
127 #include <asm-generic/cmpxchg.h>
130 #else /* min ARCH >= ARMv6 */
132 extern void __bad_cmpxchg(volatile void *ptr
, int size
);
135 * cmpxchg only support 32-bits operands on ARMv6.
138 static inline unsigned long __cmpxchg(volatile void *ptr
, unsigned long old
,
139 unsigned long new, int size
)
141 unsigned long oldval
, res
;
143 prefetchw((const void *)ptr
);
146 #ifndef CONFIG_CPU_V6 /* min ARCH >= ARMv6K */
149 asm volatile("@ __cmpxchg1\n"
153 " strexbeq %0, %4, [%2]\n"
154 : "=&r" (res
), "=&r" (oldval
)
155 : "r" (ptr
), "Ir" (old
), "r" (new)
161 asm volatile("@ __cmpxchg1\n"
165 " strexheq %0, %4, [%2]\n"
166 : "=&r" (res
), "=&r" (oldval
)
167 : "r" (ptr
), "Ir" (old
), "r" (new)
174 asm volatile("@ __cmpxchg4\n"
178 " strexeq %0, %4, [%2]\n"
179 : "=&r" (res
), "=&r" (oldval
)
180 : "r" (ptr
), "Ir" (old
), "r" (new)
185 __bad_cmpxchg(ptr
, size
);
192 static inline unsigned long __cmpxchg_mb(volatile void *ptr
, unsigned long old
,
193 unsigned long new, int size
)
198 ret
= __cmpxchg(ptr
, old
, new, size
);
204 #define cmpxchg(ptr,o,n) \
205 ((__typeof__(*(ptr)))__cmpxchg_mb((ptr), \
206 (unsigned long)(o), \
207 (unsigned long)(n), \
210 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
212 unsigned long new, int size
)
217 #ifdef CONFIG_CPU_V6 /* min ARCH == ARMv6 */
220 ret
= __cmpxchg_local_generic(ptr
, old
, new, size
);
224 ret
= __cmpxchg(ptr
, old
, new, size
);
230 static inline unsigned long long __cmpxchg64(unsigned long long *ptr
,
231 unsigned long long old
,
232 unsigned long long new)
234 unsigned long long oldval
;
239 __asm__
__volatile__(
240 "1: ldrexd %1, %H1, [%3]\n"
244 " strexd %0, %5, %H5, [%3]\n"
248 : "=&r" (res
), "=&r" (oldval
), "+Qo" (*ptr
)
249 : "r" (ptr
), "r" (old
), "r" (new)
255 static inline unsigned long long __cmpxchg64_mb(unsigned long long *ptr
,
256 unsigned long long old
,
257 unsigned long long new)
259 unsigned long long ret
;
262 ret
= __cmpxchg64(ptr
, old
, new);
268 #define cmpxchg_local(ptr,o,n) \
269 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), \
270 (unsigned long)(o), \
271 (unsigned long)(n), \
274 #define cmpxchg64(ptr, o, n) \
275 ((__typeof__(*(ptr)))__cmpxchg64_mb((ptr), \
276 (unsigned long long)(o), \
277 (unsigned long long)(n)))
279 #define cmpxchg64_relaxed(ptr, o, n) \
280 ((__typeof__(*(ptr)))__cmpxchg64((ptr), \
281 (unsigned long long)(o), \
282 (unsigned long long)(n)))
284 #define cmpxchg64_local(ptr, o, n) cmpxchg64_relaxed((ptr), (o), (n))
286 #endif /* __LINUX_ARM_ARCH__ >= 6 */
288 #endif /* __ASM_ARM_CMPXCHG_H */