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 (cpu_has_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 (cpu_has_llsc) { \
37 __asm__ __volatile__( \
41 "1: " ld " %0, %2 # __cmpxchg_asm \n" \
42 " bne %0, %z3, 2f \n" \
53 : "=&r" (__ret), "=R" (*m) \
54 : "R" (*m), "Jr" (old), "Jr" (new) \
57 unsigned long __flags; \
59 raw_local_irq_save(__flags); \
63 raw_local_irq_restore(__flags); \
70 * This function doesn't exist, so you'll get a linker error
71 * if something tries to do an invalid cmpxchg().
73 extern void __cmpxchg_called_with_bad_pointer(void);
75 #define __cmpxchg(ptr, old, new, barrier) \
77 __typeof__(ptr) __ptr = (ptr); \
78 __typeof__(*(ptr)) __old = (old); \
79 __typeof__(*(ptr)) __new = (new); \
80 __typeof__(*(ptr)) __res = 0; \
84 switch (sizeof(*(__ptr))) { \
86 __res = __cmpxchg_asm("ll", "sc", __ptr, __old, __new); \
89 if (sizeof(long) == 8) { \
90 __res = __cmpxchg_asm("lld", "scd", __ptr, \
95 __cmpxchg_called_with_bad_pointer(); \
104 #define cmpxchg(ptr, old, new) __cmpxchg(ptr, old, new, smp_llsc_mb())
105 #define cmpxchg_local(ptr, old, new) __cmpxchg(ptr, old, new, )
107 #endif /* __ASM_CMPXCHG_H */