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/irqflags.h>
13 #define __HAVE_ARCH_CMPXCHG 1
15 #define __cmpxchg_asm(ld, st, m, old, new) \
17 __typeof(*(m)) __ret; \
19 if (kernel_uses_llsc && R10000_LLSC_WAR) { \
20 __asm__ __volatile__( \
24 "1: " ld " %0, %2 # __cmpxchg_asm \n" \
25 " bne %0, %z3, 2f \n" \
33 : "=&r" (__ret), "=R" (*m) \
34 : "R" (*m), "Jr" (old), "Jr" (new) \
36 } else if (kernel_uses_llsc) { \
37 __asm__ __volatile__( \
41 "1: " ld " %0, %2 # __cmpxchg_asm \n" \
42 " bne %0, %z3, 2f \n" \
50 : "=&r" (__ret), "=R" (*m) \
51 : "R" (*m), "Jr" (old), "Jr" (new) \
54 unsigned long __flags; \
56 raw_local_irq_save(__flags); \
60 raw_local_irq_restore(__flags); \
67 * This function doesn't exist, so you'll get a linker error
68 * if something tries to do an invalid cmpxchg().
70 extern void __cmpxchg_called_with_bad_pointer(void);
72 #define __cmpxchg(ptr, old, new, pre_barrier, post_barrier) \
74 __typeof__(ptr) __ptr = (ptr); \
75 __typeof__(*(ptr)) __old = (old); \
76 __typeof__(*(ptr)) __new = (new); \
77 __typeof__(*(ptr)) __res = 0; \
81 switch (sizeof(*(__ptr))) { \
83 __res = __cmpxchg_asm("ll", "sc", __ptr, __old, __new); \
86 if (sizeof(long) == 8) { \
87 __res = __cmpxchg_asm("lld", "scd", __ptr, \
92 __cmpxchg_called_with_bad_pointer(); \
101 #define cmpxchg(ptr, old, new) __cmpxchg(ptr, old, new, smp_mb__before_llsc(), smp_llsc_mb())
102 #define cmpxchg_local(ptr, old, new) __cmpxchg(ptr, old, new, , )
104 #define cmpxchg64(ptr, o, n) \
106 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
107 cmpxchg((ptr), (o), (n)); \
111 #define cmpxchg64_local(ptr, o, n) \
113 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
114 cmpxchg_local((ptr), (o), (n)); \
117 #include <asm-generic/cmpxchg-local.h>
118 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
121 #endif /* __ASM_CMPXCHG_H */