2 * Atomic xchg and cmpxchg operations.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 #ifndef _XTENSA_CMPXCHG_H
12 #define _XTENSA_CMPXCHG_H
16 #include <linux/stringify.h>
22 static inline unsigned long
23 __cmpxchg_u32(volatile int *p, int old, int new)
25 __asm__ __volatile__("rsil a15, "__stringify(LOCKLEVEL)"\n\t"
33 : "a" (p), "a" (old), "r" (new)
37 /* This function doesn't exist, so you'll get a linker error
38 * if something tries to do an invalid cmpxchg(). */
40 extern void __cmpxchg_called_with_bad_pointer(void);
42 static __inline__ unsigned long
43 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
46 case 4: return __cmpxchg_u32(ptr, old, new);
47 default: __cmpxchg_called_with_bad_pointer();
52 #define cmpxchg(ptr,o,n) \
53 ({ __typeof__(*(ptr)) _o_ = (o); \
54 __typeof__(*(ptr)) _n_ = (n); \
55 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
56 (unsigned long)_n_, sizeof (*(ptr))); \
59 #include <asm-generic/cmpxchg-local.h>
61 static inline unsigned long __cmpxchg_local(volatile void *ptr,
63 unsigned long new, int size)
67 return __cmpxchg_u32(ptr, old, new);
69 return __cmpxchg_local_generic(ptr, old, new, size);
76 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
79 #define cmpxchg_local(ptr, o, n) \
80 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
81 (unsigned long)(n), sizeof(*(ptr))))
82 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
87 * Note that a15 is used here because the register allocation
88 * done by the compiler is not guaranteed and a window overflow
89 * may not occur between the rsil and wsr instructions. By using
90 * a15 in the rsil, the machine is guaranteed to be in a state
91 * where no register reference will cause an overflow.
94 static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
97 __asm__ __volatile__("rsil a15, "__stringify(LOCKLEVEL)"\n\t"
108 #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
111 * This only works if the compiler isn't horribly bad at optimizing.
112 * gcc-2.5.8 reportedly can't handle this, but I define that one to
116 extern void __xchg_called_with_bad_pointer(void);
118 static __inline__ unsigned long
119 __xchg(unsigned long x, volatile void * ptr, int size)
123 return xchg_u32(ptr, x);
125 __xchg_called_with_bad_pointer();
129 #endif /* __ASSEMBLY__ */
131 #endif /* _XTENSA_CMPXCHG_H */