xref: /openbmc/linux/arch/xtensa/include/asm/cmpxchg.h (revision 367e5927)
1 /*
2  * Atomic xchg and cmpxchg operations.
3  *
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
6  * for more details.
7  *
8  * Copyright (C) 2001 - 2005 Tensilica Inc.
9  */
10 
11 #ifndef _XTENSA_CMPXCHG_H
12 #define _XTENSA_CMPXCHG_H
13 
14 #ifndef __ASSEMBLY__
15 
16 #include <linux/bits.h>
17 #include <linux/stringify.h>
18 
19 /*
20  * cmpxchg
21  */
22 
23 static inline unsigned long
24 __cmpxchg_u32(volatile int *p, int old, int new)
25 {
26 #if XCHAL_HAVE_S32C1I
27 	__asm__ __volatile__(
28 			"       wsr     %2, scompare1\n"
29 			"       s32c1i  %0, %1, 0\n"
30 			: "+a" (new)
31 			: "a" (p), "a" (old)
32 			: "memory"
33 			);
34 
35 	return new;
36 #else
37 	__asm__ __volatile__(
38 			"       rsil    a15, "__stringify(TOPLEVEL)"\n"
39 			"       l32i    %0, %1, 0\n"
40 			"       bne     %0, %2, 1f\n"
41 			"       s32i    %3, %1, 0\n"
42 			"1:\n"
43 			"       wsr     a15, ps\n"
44 			"       rsync\n"
45 			: "=&a" (old)
46 			: "a" (p), "a" (old), "r" (new)
47 			: "a15", "memory");
48 	return old;
49 #endif
50 }
51 /* This function doesn't exist, so you'll get a linker error
52  * if something tries to do an invalid cmpxchg(). */
53 
54 extern void __cmpxchg_called_with_bad_pointer(void);
55 
56 static __inline__ unsigned long
57 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
58 {
59 	switch (size) {
60 	case 4:  return __cmpxchg_u32(ptr, old, new);
61 	default: __cmpxchg_called_with_bad_pointer();
62 		 return old;
63 	}
64 }
65 
66 #define cmpxchg(ptr,o,n)						      \
67 	({ __typeof__(*(ptr)) _o_ = (o);				      \
68 	   __typeof__(*(ptr)) _n_ = (n);				      \
69 	   (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_,	      \
70 	   			        (unsigned long)_n_, sizeof (*(ptr))); \
71 	})
72 
73 #include <asm-generic/cmpxchg-local.h>
74 
75 static inline unsigned long __cmpxchg_local(volatile void *ptr,
76 				      unsigned long old,
77 				      unsigned long new, int size)
78 {
79 	switch (size) {
80 	case 4:
81 		return __cmpxchg_u32(ptr, old, new);
82 	default:
83 		return __cmpxchg_local_generic(ptr, old, new, size);
84 	}
85 
86 	return old;
87 }
88 
89 /*
90  * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
91  * them available.
92  */
93 #define cmpxchg_local(ptr, o, n)				  	       \
94 	((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
95 			(unsigned long)(n), sizeof(*(ptr))))
96 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
97 #define cmpxchg64(ptr, o, n)    cmpxchg64_local((ptr), (o), (n))
98 
99 /*
100  * xchg_u32
101  *
102  * Note that a15 is used here because the register allocation
103  * done by the compiler is not guaranteed and a window overflow
104  * may not occur between the rsil and wsr instructions. By using
105  * a15 in the rsil, the machine is guaranteed to be in a state
106  * where no register reference will cause an overflow.
107  */
108 
109 static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
110 {
111 #if XCHAL_HAVE_S32C1I
112 	unsigned long tmp, result;
113 	__asm__ __volatile__(
114 			"1:     l32i    %1, %2, 0\n"
115 			"       mov     %0, %3\n"
116 			"       wsr     %1, scompare1\n"
117 			"       s32c1i  %0, %2, 0\n"
118 			"       bne     %0, %1, 1b\n"
119 			: "=&a" (result), "=&a" (tmp)
120 			: "a" (m), "a" (val)
121 			: "memory"
122 			);
123 	return result;
124 #else
125 	unsigned long tmp;
126 	__asm__ __volatile__(
127 			"       rsil    a15, "__stringify(TOPLEVEL)"\n"
128 			"       l32i    %0, %1, 0\n"
129 			"       s32i    %2, %1, 0\n"
130 			"       wsr     a15, ps\n"
131 			"       rsync\n"
132 			: "=&a" (tmp)
133 			: "a" (m), "a" (val)
134 			: "a15", "memory");
135 	return tmp;
136 #endif
137 }
138 
139 #define xchg(ptr,x) \
140 	((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
141 
142 static inline u32 xchg_small(volatile void *ptr, u32 x, int size)
143 {
144 	int off = (unsigned long)ptr % sizeof(u32);
145 	volatile u32 *p = ptr - off;
146 #ifdef __BIG_ENDIAN
147 	int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
148 #else
149 	int bitoff = off * BITS_PER_BYTE;
150 #endif
151 	u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
152 	u32 oldv, newv;
153 	u32 ret;
154 
155 	do {
156 		oldv = READ_ONCE(*p);
157 		ret = (oldv & bitmask) >> bitoff;
158 		newv = (oldv & ~bitmask) | (x << bitoff);
159 	} while (__cmpxchg_u32(p, oldv, newv) != oldv);
160 
161 	return ret;
162 }
163 
164 /*
165  * This only works if the compiler isn't horribly bad at optimizing.
166  * gcc-2.5.8 reportedly can't handle this, but I define that one to
167  * be dead anyway.
168  */
169 
170 extern void __xchg_called_with_bad_pointer(void);
171 
172 static __inline__ unsigned long
173 __xchg(unsigned long x, volatile void * ptr, int size)
174 {
175 	switch (size) {
176 	case 1:
177 		return xchg_small(ptr, x, 1);
178 	case 2:
179 		return xchg_small(ptr, x, 2);
180 	case 4:
181 		return xchg_u32(ptr, x);
182 	default:
183 		__xchg_called_with_bad_pointer();
184 		return x;
185 	}
186 }
187 
188 #endif /* __ASSEMBLY__ */
189 
190 #endif /* _XTENSA_CMPXCHG_H */
191