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)
27 " wsr %2, scompare1\n"
37 " rsil a15, "__stringify(TOPLEVEL)"\n"
45 : "a" (p), "a" (old), "r" (new)
50 /* This function doesn't exist, so you'll get a linker error
51 * if something tries to do an invalid cmpxchg(). */
53 extern void __cmpxchg_called_with_bad_pointer(void);
55 static __inline__ unsigned long
56 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
59 case 4: return __cmpxchg_u32(ptr, old, new);
60 default: __cmpxchg_called_with_bad_pointer();
65 #define cmpxchg(ptr,o,n) \
66 ({ __typeof__(*(ptr)) _o_ = (o); \
67 __typeof__(*(ptr)) _n_ = (n); \
68 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
69 (unsigned long)_n_, sizeof (*(ptr))); \
72 #include <asm-generic/cmpxchg-local.h>
74 static inline unsigned long __cmpxchg_local(volatile void *ptr,
76 unsigned long new, int size)
80 return __cmpxchg_u32(ptr, old, new);
82 return __cmpxchg_local_generic(ptr, old, new, size);
89 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
92 #define cmpxchg_local(ptr, o, n) \
93 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
94 (unsigned long)(n), sizeof(*(ptr))))
95 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
96 #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
101 * Note that a15 is used here because the register allocation
102 * done by the compiler is not guaranteed and a window overflow
103 * may not occur between the rsil and wsr instructions. By using
104 * a15 in the rsil, the machine is guaranteed to be in a state
105 * where no register reference will cause an overflow.
108 static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
110 #if XCHAL_HAVE_S32C1I
111 unsigned long tmp, result;
112 __asm__ __volatile__(
113 "1: l32i %1, %2, 0\n"
115 " wsr %1, scompare1\n"
116 " s32c1i %0, %2, 0\n"
118 : "=&a" (result), "=&a" (tmp)
125 __asm__ __volatile__(
126 " rsil a15, "__stringify(TOPLEVEL)"\n"
138 #define xchg(ptr,x) \
139 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
142 * This only works if the compiler isn't horribly bad at optimizing.
143 * gcc-2.5.8 reportedly can't handle this, but I define that one to
147 extern void __xchg_called_with_bad_pointer(void);
149 static __inline__ unsigned long
150 __xchg(unsigned long x, volatile void * ptr, int size)
154 return xchg_u32(ptr, x);
156 __xchg_called_with_bad_pointer();
160 #endif /* __ASSEMBLY__ */
162 #endif /* _XTENSA_CMPXCHG_H */