2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2003, 06, 07 by Ralf Baechle (ralf@linux-mips.org)
8 #ifndef __ASM_CMPXCHG_H
9 #define __ASM_CMPXCHG_H
11 #include <linux/bug.h>
12 #include <linux/irqflags.h>
13 #include <asm/compiler.h>
19 * These functions doesn't exist, so if they are called you'll either:
21 * - Get an error at compile-time due to __compiletime_error, if supported by
26 * - Get an error at link-time due to the call to the missing function.
28 extern unsigned long __cmpxchg_called_with_bad_pointer(void)
29 __compiletime_error("Bad argument size for cmpxchg");
30 extern unsigned long __cmpxchg64_unsupported(void)
31 __compiletime_error("cmpxchg64 not available; cpu_has_64bits may be false");
32 extern unsigned long __xchg_called_with_bad_pointer(void)
33 __compiletime_error("Bad argument size for xchg");
35 #define __xchg_asm(ld, st, m, val) \
37 __typeof(*(m)) __ret; \
39 if (kernel_uses_llsc) { \
40 __asm__ __volatile__( \
44 " .set " MIPS_ISA_ARCH_LEVEL " \n" \
45 " " __SYNC(full, loongson3_war) " \n" \
46 "1: " ld " %0, %2 # __xchg_asm \n" \
49 " .set " MIPS_ISA_ARCH_LEVEL " \n" \
51 "\t" __SC_BEQZ "$1, 1b \n" \
53 : "=&r" (__ret), "=" GCC_OFF_SMALL_ASM() (*m) \
54 : GCC_OFF_SMALL_ASM() (*m), "Jr" (val) \
57 unsigned long __flags; \
59 raw_local_irq_save(__flags); \
62 raw_local_irq_restore(__flags); \
68 extern unsigned long __xchg_small(volatile void *ptr
, unsigned long val
,
71 static __always_inline
72 unsigned long __xchg(volatile void *ptr
, unsigned long x
, int size
)
77 return __xchg_small(ptr
, x
, size
);
80 return __xchg_asm("ll", "sc", (volatile u32
*)ptr
, x
);
83 if (!IS_ENABLED(CONFIG_64BIT
))
84 return __xchg_called_with_bad_pointer();
86 return __xchg_asm("lld", "scd", (volatile u64
*)ptr
, x
);
89 return __xchg_called_with_bad_pointer();
93 #define xchg(ptr, x) \
95 __typeof__(*(ptr)) __res; \
98 * In the Loongson3 workaround case __xchg_asm() already \
99 * contains a completion barrier prior to the LL, so we don't \
100 * need to emit an extra one here. \
102 if (!__SYNC_loongson3_war) \
103 smp_mb__before_llsc(); \
105 __res = (__typeof__(*(ptr))) \
106 __xchg((ptr), (unsigned long)(x), sizeof(*(ptr))); \
113 #define __cmpxchg_asm(ld, st, m, old, new) \
115 __typeof(*(m)) __ret; \
117 if (kernel_uses_llsc) { \
118 __asm__ __volatile__( \
122 " .set "MIPS_ISA_ARCH_LEVEL" \n" \
123 " " __SYNC(full, loongson3_war) " \n" \
124 "1: " ld " %0, %2 # __cmpxchg_asm \n" \
125 " bne %0, %z3, 2f \n" \
128 " .set "MIPS_ISA_ARCH_LEVEL" \n" \
129 " " st " $1, %1 \n" \
130 "\t" __SC_BEQZ "$1, 1b \n" \
132 "2: " __SYNC(full, loongson3_war) " \n" \
133 : "=&r" (__ret), "=" GCC_OFF_SMALL_ASM() (*m) \
134 : GCC_OFF_SMALL_ASM() (*m), "Jr" (old), "Jr" (new) \
137 unsigned long __flags; \
139 raw_local_irq_save(__flags); \
143 raw_local_irq_restore(__flags); \
149 extern unsigned long __cmpxchg_small(volatile void *ptr
, unsigned long old
,
150 unsigned long new, unsigned int size
);
152 static __always_inline
153 unsigned long __cmpxchg(volatile void *ptr
, unsigned long old
,
154 unsigned long new, unsigned int size
)
159 return __cmpxchg_small(ptr
, old
, new, size
);
162 return __cmpxchg_asm("ll", "sc", (volatile u32
*)ptr
,
166 /* lld/scd are only available for MIPS64 */
167 if (!IS_ENABLED(CONFIG_64BIT
))
168 return __cmpxchg_called_with_bad_pointer();
170 return __cmpxchg_asm("lld", "scd", (volatile u64
*)ptr
,
174 return __cmpxchg_called_with_bad_pointer();
178 #define cmpxchg_local(ptr, old, new) \
179 ((__typeof__(*(ptr))) \
181 (unsigned long)(__typeof__(*(ptr)))(old), \
182 (unsigned long)(__typeof__(*(ptr)))(new), \
185 #define cmpxchg(ptr, old, new) \
187 __typeof__(*(ptr)) __res; \
190 * In the Loongson3 workaround case __cmpxchg_asm() already \
191 * contains a completion barrier prior to the LL, so we don't \
192 * need to emit an extra one here. \
194 if (!__SYNC_loongson3_war) \
195 smp_mb__before_llsc(); \
197 __res = cmpxchg_local((ptr), (old), (new)); \
200 * In the Loongson3 workaround case __cmpxchg_asm() already \
201 * contains a completion barrier after the SC, so we don't \
202 * need to emit an extra one here. \
204 if (!__SYNC_loongson3_war) \
211 #define cmpxchg64_local(ptr, o, n) \
213 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
214 cmpxchg_local((ptr), (o), (n)); \
217 #define cmpxchg64(ptr, o, n) \
219 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
220 cmpxchg((ptr), (o), (n)); \
224 # include <asm-generic/cmpxchg-local.h>
225 # define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
229 static inline unsigned long __cmpxchg64(volatile void *ptr
,
230 unsigned long long old
,
231 unsigned long long new)
233 unsigned long long tmp
, ret
;
237 * The assembly below has to combine 32 bit values into a 64 bit
238 * register, and split 64 bit values from one register into two. If we
239 * were to take an interrupt in the middle of this we'd only save the
240 * least significant 32 bits of each register & probably clobber the
241 * most significant 32 bits of the 64 bit values we're using. In order
242 * to avoid this we must disable interrupts.
244 local_irq_save(flags
);
248 " .set " MIPS_ISA_ARCH_LEVEL
" \n"
249 /* Load 64 bits from ptr */
250 " " __SYNC(full
, loongson3_war
) " \n"
251 "1: lld %L0, %3 # __cmpxchg64 \n"
253 * Split the 64 bit value we loaded into the 2 registers that hold the
256 " dsra %M0, %L0, 32 \n"
257 " sll %L0, %L0, 0 \n"
259 * Compare ret against old, breaking out of the loop if they don't
262 " bne %M0, %M4, 2f \n"
263 " bne %L0, %L4, 2f \n"
265 * Combine the 32 bit halves from the 2 registers that hold the new
266 * variable into a single 64 bit register.
268 # if MIPS_ISA_REV >= 2
270 " dins %L1, %M5, 32, 32 \n"
272 " dsll %L1, %L5, 32 \n"
273 " dsrl %L1, %L1, 32 \n"
275 " dsll $at, %M5, 32 \n"
276 " or %L1, %L1, $at \n"
279 /* Attempt to store new at ptr */
281 /* If we failed, loop! */
282 "\t" __SC_BEQZ
"%L1, 1b \n"
284 "2: " __SYNC(full
, loongson3_war
) " \n"
287 "=" GCC_OFF_SMALL_ASM() (*(unsigned long long *)ptr
)
288 : GCC_OFF_SMALL_ASM() (*(unsigned long long *)ptr
),
293 local_irq_restore(flags
);
297 # define cmpxchg64(ptr, o, n) ({ \
298 unsigned long long __old = (__typeof__(*(ptr)))(o); \
299 unsigned long long __new = (__typeof__(*(ptr)))(n); \
300 __typeof__(*(ptr)) __res; \
303 * We can only use cmpxchg64 if we know that the CPU supports \
304 * 64-bits, ie. lld & scd. Our call to __cmpxchg64_unsupported \
305 * will cause a build error unless cpu_has_64bits is a \
306 * compile-time constant 1. \
308 if (cpu_has_64bits && kernel_uses_llsc) { \
309 smp_mb__before_llsc(); \
310 __res = __cmpxchg64((ptr), __old, __new); \
313 __res = __cmpxchg64_unsupported(); \
319 # else /* !CONFIG_SMP */
320 # define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
321 # endif /* !CONFIG_SMP */
322 #endif /* !CONFIG_64BIT */
324 #endif /* __ASM_CMPXCHG_H */