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/bits.h>
17 #include <linux/stringify.h>
23 static inline unsigned long
24 __cmpxchg_u32(volatile int *p
, int old
, int new)
26 #if XCHAL_HAVE_EXCLUSIVE
27 unsigned long tmp
, result
;
30 "1: l32ex %[result], %[addr]\n"
31 " bne %[result], %[cmp], 2f\n"
32 " mov %[tmp], %[new]\n"
33 " s32ex %[tmp], %[addr]\n"
37 : [result
] "=&a" (result
), [tmp
] "=&a" (tmp
)
38 : [new] "a" (new), [addr
] "a" (p
), [cmp
] "a" (old
)
43 #elif XCHAL_HAVE_S32C1I
45 " wsr %[cmp], scompare1\n"
46 " s32c1i %[new], %[mem]\n"
47 : [new] "+a" (new), [mem
] "+m" (*p
)
55 " rsil a15, "__stringify(TOPLEVEL
)"\n"
56 " l32i %[old], %[mem]\n"
57 " bne %[old], %[cmp], 1f\n"
58 " s32i %[new], %[mem]\n"
62 : [old
] "=&a" (old
), [mem
] "+m" (*p
)
63 : [cmp
] "a" (old
), [new] "r" (new)
68 /* This function doesn't exist, so you'll get a linker error
69 * if something tries to do an invalid cmpxchg(). */
71 extern void __cmpxchg_called_with_bad_pointer(void);
73 static __inline__
unsigned long
74 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new, int size
)
77 case 4: return __cmpxchg_u32(ptr
, old
, new);
78 default: __cmpxchg_called_with_bad_pointer();
83 #define cmpxchg(ptr,o,n) \
84 ({ __typeof__(*(ptr)) _o_ = (o); \
85 __typeof__(*(ptr)) _n_ = (n); \
86 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
87 (unsigned long)_n_, sizeof (*(ptr))); \
90 #include <asm-generic/cmpxchg-local.h>
92 static inline unsigned long __cmpxchg_local(volatile void *ptr
,
94 unsigned long new, int size
)
98 return __cmpxchg_u32(ptr
, old
, new);
100 return __cmpxchg_local_generic(ptr
, old
, new, size
);
107 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
110 #define cmpxchg_local(ptr, o, n) \
111 ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
112 (unsigned long)(n), sizeof(*(ptr))))
113 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
114 #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
119 * Note that a15 is used here because the register allocation
120 * done by the compiler is not guaranteed and a window overflow
121 * may not occur between the rsil and wsr instructions. By using
122 * a15 in the rsil, the machine is guaranteed to be in a state
123 * where no register reference will cause an overflow.
126 static inline unsigned long xchg_u32(volatile int * m
, unsigned long val
)
128 #if XCHAL_HAVE_EXCLUSIVE
129 unsigned long tmp
, result
;
131 __asm__
__volatile__(
132 "1: l32ex %[result], %[addr]\n"
133 " mov %[tmp], %[val]\n"
134 " s32ex %[tmp], %[addr]\n"
137 : [result
] "=&a" (result
), [tmp
] "=&a" (tmp
)
138 : [val
] "a" (val
), [addr
] "a" (m
)
143 #elif XCHAL_HAVE_S32C1I
144 unsigned long tmp
, result
;
145 __asm__
__volatile__(
146 "1: l32i %[tmp], %[mem]\n"
147 " mov %[result], %[val]\n"
148 " wsr %[tmp], scompare1\n"
149 " s32c1i %[result], %[mem]\n"
150 " bne %[result], %[tmp], 1b\n"
151 : [result
] "=&a" (result
), [tmp
] "=&a" (tmp
),
159 __asm__
__volatile__(
160 " rsil a15, "__stringify(TOPLEVEL
)"\n"
161 " l32i %[tmp], %[mem]\n"
162 " s32i %[val], %[mem]\n"
165 : [tmp
] "=&a" (tmp
), [mem
] "+m" (*m
)
172 #define xchg(ptr,x) \
173 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
175 static inline u32
xchg_small(volatile void *ptr
, u32 x
, int size
)
177 int off
= (unsigned long)ptr
% sizeof(u32
);
178 volatile u32
*p
= ptr
- off
;
180 int bitoff
= (sizeof(u32
) - size
- off
) * BITS_PER_BYTE
;
182 int bitoff
= off
* BITS_PER_BYTE
;
184 u32 bitmask
= ((0x1 << size
* BITS_PER_BYTE
) - 1) << bitoff
;
189 oldv
= READ_ONCE(*p
);
190 ret
= (oldv
& bitmask
) >> bitoff
;
191 newv
= (oldv
& ~bitmask
) | (x
<< bitoff
);
192 } while (__cmpxchg_u32(p
, oldv
, newv
) != oldv
);
198 * This only works if the compiler isn't horribly bad at optimizing.
199 * gcc-2.5.8 reportedly can't handle this, but I define that one to
203 extern void __xchg_called_with_bad_pointer(void);
205 static __inline__
unsigned long
206 __xchg(unsigned long x
, volatile void * ptr
, int size
)
210 return xchg_small(ptr
, x
, 1);
212 return xchg_small(ptr
, x
, 2);
214 return xchg_u32(ptr
, x
);
216 __xchg_called_with_bad_pointer();
221 #endif /* __ASSEMBLY__ */
223 #endif /* _XTENSA_CMPXCHG_H */