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
38 prefetchw((const void *)ptr
);
41 #if __LINUX_ARM_ARCH__ >= 6
42 #ifndef CONFIG_CPU_V6 /* MIN ARCH >= V6K */
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("@ __xchg2\n"
55 "1: ldrexh %0, [%3]\n"
56 " strexh %1, %2, [%3]\n"
59 : "=&r" (ret
), "=&r" (tmp
)
65 asm volatile("@ __xchg4\n"
67 " strex %1, %2, [%3]\n"
70 : "=&r" (ret
), "=&r" (tmp
)
74 #elif defined(swp_is_buggy)
76 #error SMP is not supported on this platform
79 raw_local_irq_save(flags
);
80 ret
= *(volatile unsigned char *)ptr
;
81 *(volatile unsigned char *)ptr
= x
;
82 raw_local_irq_restore(flags
);
86 raw_local_irq_save(flags
);
87 ret
= *(volatile unsigned long *)ptr
;
88 *(volatile unsigned long *)ptr
= x
;
89 raw_local_irq_restore(flags
);
93 asm volatile("@ __xchg1\n"
100 asm volatile("@ __xchg4\n"
108 /* Cause a link-time error, the xchg() size is not supported */
109 __bad_xchg(ptr
, size
), ret
= 0;
116 #define xchg_relaxed(ptr, x) ({ \
117 (__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), \
121 #include <asm-generic/cmpxchg-local.h>
123 #if __LINUX_ARM_ARCH__ < 6
124 /* min ARCH < ARMv6 */
127 #error "SMP is not supported on this platform"
130 #define xchg xchg_relaxed
133 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
136 #define cmpxchg_local(ptr, o, n) ({ \
137 (__typeof(*ptr))__cmpxchg_local_generic((ptr), \
138 (unsigned long)(o), \
139 (unsigned long)(n), \
143 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
145 #include <asm-generic/cmpxchg.h>
147 #else /* min ARCH >= ARMv6 */
149 extern void __bad_cmpxchg(volatile void *ptr
, int size
);
152 * cmpxchg only support 32-bits operands on ARMv6.
155 static inline unsigned long __cmpxchg(volatile void *ptr
, unsigned long old
,
156 unsigned long new, int size
)
158 unsigned long oldval
, res
;
160 prefetchw((const void *)ptr
);
163 #ifndef CONFIG_CPU_V6 /* min ARCH >= ARMv6K */
166 asm volatile("@ __cmpxchg1\n"
170 " strexbeq %0, %4, [%2]\n"
171 : "=&r" (res
), "=&r" (oldval
)
172 : "r" (ptr
), "Ir" (old
), "r" (new)
178 asm volatile("@ __cmpxchg1\n"
182 " strexheq %0, %4, [%2]\n"
183 : "=&r" (res
), "=&r" (oldval
)
184 : "r" (ptr
), "Ir" (old
), "r" (new)
191 asm volatile("@ __cmpxchg4\n"
195 " strexeq %0, %4, [%2]\n"
196 : "=&r" (res
), "=&r" (oldval
)
197 : "r" (ptr
), "Ir" (old
), "r" (new)
202 __bad_cmpxchg(ptr
, size
);
209 #define cmpxchg_relaxed(ptr,o,n) ({ \
210 (__typeof__(*(ptr)))__cmpxchg((ptr), \
211 (unsigned long)(o), \
212 (unsigned long)(n), \
216 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
218 unsigned long new, int size
)
223 #ifdef CONFIG_CPU_V6 /* min ARCH == ARMv6 */
226 ret
= __cmpxchg_local_generic(ptr
, old
, new, size
);
230 ret
= __cmpxchg(ptr
, old
, new, size
);
236 #define cmpxchg_local(ptr, o, n) ({ \
237 (__typeof(*ptr))__cmpxchg_local((ptr), \
238 (unsigned long)(o), \
239 (unsigned long)(n), \
243 static inline unsigned long long __cmpxchg64(unsigned long long *ptr
,
244 unsigned long long old
,
245 unsigned long long new)
247 unsigned long long oldval
;
252 __asm__
__volatile__(
253 "1: ldrexd %1, %H1, [%3]\n"
257 " strexd %0, %5, %H5, [%3]\n"
261 : "=&r" (res
), "=&r" (oldval
), "+Qo" (*ptr
)
262 : "r" (ptr
), "r" (old
), "r" (new)
268 #define cmpxchg64_relaxed(ptr, o, n) ({ \
269 (__typeof__(*(ptr)))__cmpxchg64((ptr), \
270 (unsigned long long)(o), \
271 (unsigned long long)(n)); \
274 #define cmpxchg64_local(ptr, o, n) cmpxchg64_relaxed((ptr), (o), (n))
276 #endif /* __LINUX_ARM_ARCH__ >= 6 */
278 #endif /* __ASM_ARM_CMPXCHG_H */