]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - arch/xtensa/include/asm/cmpxchg.h
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
;
37 : "=&a" (result
), "=&a" (tmp
)
38 : "a" (new), "a" (p
), "a" (old
)
43 #elif XCHAL_HAVE_S32C1I
45 " wsr %2, scompare1\n"
55 " rsil a15, "__stringify(TOPLEVEL
)"\n"
63 : "a" (p
), "a" (old
), "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__(
137 : "=&a" (result
), "=&a" (tmp
)
143 #elif XCHAL_HAVE_S32C1I
144 unsigned long tmp
, result
;
145 __asm__
__volatile__(
146 "1: l32i %1, %2, 0\n"
148 " wsr %1, scompare1\n"
149 " s32c1i %0, %2, 0\n"
151 : "=&a" (result
), "=&a" (tmp
)
158 __asm__
__volatile__(
159 " rsil a15, "__stringify(TOPLEVEL
)"\n"
171 #define xchg(ptr,x) \
172 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
174 static inline u32
xchg_small(volatile void *ptr
, u32 x
, int size
)
176 int off
= (unsigned long)ptr
% sizeof(u32
);
177 volatile u32
*p
= ptr
- off
;
179 int bitoff
= (sizeof(u32
) - size
- off
) * BITS_PER_BYTE
;
181 int bitoff
= off
* BITS_PER_BYTE
;
183 u32 bitmask
= ((0x1 << size
* BITS_PER_BYTE
) - 1) << bitoff
;
188 oldv
= READ_ONCE(*p
);
189 ret
= (oldv
& bitmask
) >> bitoff
;
190 newv
= (oldv
& ~bitmask
) | (x
<< bitoff
);
191 } while (__cmpxchg_u32(p
, oldv
, newv
) != oldv
);
197 * This only works if the compiler isn't horribly bad at optimizing.
198 * gcc-2.5.8 reportedly can't handle this, but I define that one to
202 extern void __xchg_called_with_bad_pointer(void);
204 static __inline__
unsigned long
205 __xchg(unsigned long x
, volatile void * ptr
, int size
)
209 return xchg_small(ptr
, x
, 1);
211 return xchg_small(ptr
, x
, 2);
213 return xchg_u32(ptr
, x
);
215 __xchg_called_with_bad_pointer();
220 #endif /* __ASSEMBLY__ */
222 #endif /* _XTENSA_CMPXCHG_H */