1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_ARM_CMPXCHG_H
3 #define __ASM_ARM_CMPXCHG_H
5 #include <linux/irqflags.h>
6 #include <linux/prefetch.h>
7 #include <asm/barrier.h>
8 #include <linux/cmpxchg-emu.h>
10 #if defined(CONFIG_CPU_SA1100) || defined(CONFIG_CPU_SA110)
12 * On the StrongARM, "swp" is terminally broken since it bypasses the
13 * cache totally. This means that the cache becomes inconsistent, and,
14 * since we use normal loads/stores as well, this is really bad.
15 * Typically, this causes oopsen in filp_close, but could have other,
16 * more disastrous effects. There are two work-arounds:
17 * 1. Disable interrupts and emulate the atomic swap
18 * 2. Clean the cache, perform atomic swap, flush the cache
20 * We choose (1) since its the "easiest" to achieve here and is not
21 * dependent on the processor type.
23 * NOTE that this solution won't work on an SMP system, so explcitly
29 static inline unsigned long
30 __arch_xchg(unsigned long x, volatile void *ptr, int size)
32 extern void __bad_xchg(volatile void *, int);
37 #if __LINUX_ARM_ARCH__ >= 6
41 prefetchw((const void *)ptr);
44 #if __LINUX_ARM_ARCH__ >= 6
45 #ifndef CONFIG_CPU_V6 /* MIN ARCH >= V6K */
47 asm volatile("@ __xchg1\n"
48 "1: ldrexb %0, [%3]\n"
49 " strexb %1, %2, [%3]\n"
52 : "=&r" (ret), "=&r" (tmp)
57 asm volatile("@ __xchg2\n"
58 "1: ldrexh %0, [%3]\n"
59 " strexh %1, %2, [%3]\n"
62 : "=&r" (ret), "=&r" (tmp)
68 asm volatile("@ __xchg4\n"
70 " strex %1, %2, [%3]\n"
73 : "=&r" (ret), "=&r" (tmp)
77 #elif defined(swp_is_buggy)
79 #error SMP is not supported on this platform
82 raw_local_irq_save(flags);
83 ret = *(volatile unsigned char *)ptr;
84 *(volatile unsigned char *)ptr = x;
85 raw_local_irq_restore(flags);
89 raw_local_irq_save(flags);
90 ret = *(volatile unsigned long *)ptr;
91 *(volatile unsigned long *)ptr = x;
92 raw_local_irq_restore(flags);
96 asm volatile("@ __xchg1\n"
103 asm volatile("@ __xchg4\n"
111 /* Cause a link-time error, the xchg() size is not supported */
112 __bad_xchg(ptr, size), ret = 0;
119 #define arch_xchg_relaxed(ptr, x) ({ \
120 (__typeof__(*(ptr)))__arch_xchg((unsigned long)(x), (ptr), \
124 #include <asm-generic/cmpxchg-local.h>
126 #if __LINUX_ARM_ARCH__ < 6
127 /* min ARCH < ARMv6 */
130 #error "SMP is not supported on this platform"
133 #define arch_xchg arch_xchg_relaxed
136 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
139 #define arch_cmpxchg_local(ptr, o, n) ({ \
140 (__typeof(*ptr))__generic_cmpxchg_local((ptr), \
141 (unsigned long)(o), \
142 (unsigned long)(n), \
146 #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))
148 #include <asm-generic/cmpxchg.h>
150 #else /* min ARCH >= ARMv6 */
152 extern void __bad_cmpxchg(volatile void *ptr, int size);
155 * cmpxchg only support 32-bits operands on ARMv6.
158 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
159 unsigned long new, int size)
161 unsigned long oldval, res;
163 prefetchw((const void *)ptr);
166 #ifdef CONFIG_CPU_V6 /* ARCH == ARMv6 */
168 oldval = cmpxchg_emu_u8((volatile u8 *)ptr, old, new);
170 #else /* min ARCH > ARMv6 */
173 asm volatile("@ __cmpxchg1\n"
177 " strexbeq %0, %4, [%2]\n"
178 : "=&r" (res), "=&r" (oldval)
179 : "r" (ptr), "Ir" (old), "r" (new)
185 asm volatile("@ __cmpxchg1\n"
189 " strexheq %0, %4, [%2]\n"
190 : "=&r" (res), "=&r" (oldval)
191 : "r" (ptr), "Ir" (old), "r" (new)
198 asm volatile("@ __cmpxchg4\n"
202 " strexeq %0, %4, [%2]\n"
203 : "=&r" (res), "=&r" (oldval)
204 : "r" (ptr), "Ir" (old), "r" (new)
209 __bad_cmpxchg(ptr, size);
216 #define arch_cmpxchg_relaxed(ptr,o,n) ({ \
217 (__typeof__(*(ptr)))__cmpxchg((ptr), \
218 (unsigned long)(o), \
219 (unsigned long)(n), \
223 static inline unsigned long __cmpxchg_local(volatile void *ptr,
225 unsigned long new, int size)
230 #ifdef CONFIG_CPU_V6 /* min ARCH == ARMv6 */
233 ret = __generic_cmpxchg_local(ptr, old, new, size);
237 ret = __cmpxchg(ptr, old, new, size);
243 #define arch_cmpxchg_local(ptr, o, n) ({ \
244 (__typeof(*ptr))__cmpxchg_local((ptr), \
245 (unsigned long)(o), \
246 (unsigned long)(n), \
250 static inline unsigned long long __cmpxchg64(unsigned long long *ptr,
251 unsigned long long old,
252 unsigned long long new)
254 unsigned long long oldval;
259 __asm__ __volatile__(
260 "1: ldrexd %1, %H1, [%3]\n"
264 " strexd %0, %5, %H5, [%3]\n"
268 : "=&r" (res), "=&r" (oldval), "+Qo" (*ptr)
269 : "r" (ptr), "r" (old), "r" (new)
275 #define arch_cmpxchg64_relaxed(ptr, o, n) ({ \
276 (__typeof__(*(ptr)))__cmpxchg64((ptr), \
277 (unsigned long long)(o), \
278 (unsigned long long)(n)); \
281 #define arch_cmpxchg64_local(ptr, o, n) arch_cmpxchg64_relaxed((ptr), (o), (n))
283 #endif /* __LINUX_ARM_ARCH__ >= 6 */
285 #endif /* __ASM_ARM_CMPXCHG_H */