2 * Based on arch/arm/include/asm/cmpxchg.h
4 * Copyright (C) 2012 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef __ASM_CMPXCHG_H
19 #define __ASM_CMPXCHG_H
21 #include <linux/bug.h>
23 #include <asm/atomic.h>
24 #include <asm/barrier.h>
28 * We need separate acquire parameters for ll/sc and lse, since the full
29 * barrier case is generated as release+dmb for the former and
30 * acquire+release for the latter.
32 #define __XCHG_CASE(w, sz, name, mb, nop_lse, acq, acq_lse, rel, cl) \
33 static inline unsigned long __xchg_case_##name(unsigned long x, \
36 unsigned long ret, tmp; \
38 asm volatile(ARM64_LSE_ATOMIC_INSN( \
40 " prfm pstl1strm, %2\n" \
41 "1: ld" #acq "xr" #sz "\t%" #w "0, %2\n" \
42 " st" #rel "xr" #sz "\t%w1, %" #w "3, %2\n" \
46 " swp" #acq_lse #rel #sz "\t%" #w "3, %" #w "0, %2\n" \
49 : "=&r" (ret), "=&r" (tmp), "+Q" (*(u8 *)ptr) \
56 __XCHG_CASE(w, b, 1, , , , , , )
57 __XCHG_CASE(w, h, 2, , , , , , )
58 __XCHG_CASE(w, , 4, , , , , , )
59 __XCHG_CASE( , , 8, , , , , , )
60 __XCHG_CASE(w, b, acq_1, , , a, a, , "memory")
61 __XCHG_CASE(w, h, acq_2, , , a, a, , "memory")
62 __XCHG_CASE(w, , acq_4, , , a, a, , "memory")
63 __XCHG_CASE( , , acq_8, , , a, a, , "memory")
64 __XCHG_CASE(w, b, rel_1, , , , , l, "memory")
65 __XCHG_CASE(w, h, rel_2, , , , , l, "memory")
66 __XCHG_CASE(w, , rel_4, , , , , l, "memory")
67 __XCHG_CASE( , , rel_8, , , , , l, "memory")
68 __XCHG_CASE(w, b, mb_1, dmb ish, nop, , a, l, "memory")
69 __XCHG_CASE(w, h, mb_2, dmb ish, nop, , a, l, "memory")
70 __XCHG_CASE(w, , mb_4, dmb ish, nop, , a, l, "memory")
71 __XCHG_CASE( , , mb_8, dmb ish, nop, , a, l, "memory")
75 #define __XCHG_GEN(sfx) \
76 static inline unsigned long __xchg##sfx(unsigned long x, \
82 return __xchg_case##sfx##_1(x, ptr); \
84 return __xchg_case##sfx##_2(x, ptr); \
86 return __xchg_case##sfx##_4(x, ptr); \
88 return __xchg_case##sfx##_8(x, ptr); \
103 #define __xchg_wrapper(sfx, ptr, x) \
105 __typeof__(*(ptr)) __ret; \
106 __ret = (__typeof__(*(ptr))) \
107 __xchg##sfx((unsigned long)(x), (ptr), sizeof(*(ptr))); \
112 #define xchg_relaxed(...) __xchg_wrapper( , __VA_ARGS__)
113 #define xchg_acquire(...) __xchg_wrapper(_acq, __VA_ARGS__)
114 #define xchg_release(...) __xchg_wrapper(_rel, __VA_ARGS__)
115 #define xchg(...) __xchg_wrapper( _mb, __VA_ARGS__)
117 #define __CMPXCHG_GEN(sfx) \
118 static inline unsigned long __cmpxchg##sfx(volatile void *ptr, \
125 return __cmpxchg_case##sfx##_1(ptr, (u8)old, new); \
127 return __cmpxchg_case##sfx##_2(ptr, (u16)old, new); \
129 return __cmpxchg_case##sfx##_4(ptr, old, new); \
131 return __cmpxchg_case##sfx##_8(ptr, old, new); \
146 #define __cmpxchg_wrapper(sfx, ptr, o, n) \
148 __typeof__(*(ptr)) __ret; \
149 __ret = (__typeof__(*(ptr))) \
150 __cmpxchg##sfx((ptr), (unsigned long)(o), \
151 (unsigned long)(n), sizeof(*(ptr))); \
156 #define cmpxchg_relaxed(...) __cmpxchg_wrapper( , __VA_ARGS__)
157 #define cmpxchg_acquire(...) __cmpxchg_wrapper(_acq, __VA_ARGS__)
158 #define cmpxchg_release(...) __cmpxchg_wrapper(_rel, __VA_ARGS__)
159 #define cmpxchg(...) __cmpxchg_wrapper( _mb, __VA_ARGS__)
160 #define cmpxchg_local cmpxchg_relaxed
163 #define cmpxchg64_relaxed cmpxchg_relaxed
164 #define cmpxchg64_acquire cmpxchg_acquire
165 #define cmpxchg64_release cmpxchg_release
166 #define cmpxchg64 cmpxchg
167 #define cmpxchg64_local cmpxchg_local
170 #define system_has_cmpxchg_double() 1
172 #define __cmpxchg_double_check(ptr1, ptr2) \
174 if (sizeof(*(ptr1)) != 8) \
176 VM_BUG_ON((unsigned long *)(ptr2) - (unsigned long *)(ptr1) != 1); \
179 #define cmpxchg_double(ptr1, ptr2, o1, o2, n1, n2) \
182 __cmpxchg_double_check(ptr1, ptr2); \
183 __ret = !__cmpxchg_double_mb((unsigned long)(o1), (unsigned long)(o2), \
184 (unsigned long)(n1), (unsigned long)(n2), \
189 #define cmpxchg_double_local(ptr1, ptr2, o1, o2, n1, n2) \
192 __cmpxchg_double_check(ptr1, ptr2); \
193 __ret = !__cmpxchg_double((unsigned long)(o1), (unsigned long)(o2), \
194 (unsigned long)(n1), (unsigned long)(n2), \
199 /* this_cpu_cmpxchg */
200 #define _protect_cmpxchg_local(pcp, o, n) \
202 typeof(*raw_cpu_ptr(&(pcp))) __ret; \
204 __ret = cmpxchg_local(raw_cpu_ptr(&(pcp)), o, n); \
209 #define this_cpu_cmpxchg_1(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
210 #define this_cpu_cmpxchg_2(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
211 #define this_cpu_cmpxchg_4(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
212 #define this_cpu_cmpxchg_8(ptr, o, n) _protect_cmpxchg_local(ptr, o, n)
214 #define this_cpu_cmpxchg_double_8(ptr1, ptr2, o1, o2, n1, n2) \
218 __ret = cmpxchg_double_local( raw_cpu_ptr(&(ptr1)), \
219 raw_cpu_ptr(&(ptr2)), \
225 #define __CMPWAIT_CASE(w, sz, name) \
226 static inline void __cmpwait_case_##name(volatile void *ptr, \
232 " ldxr" #sz "\t%" #w "[tmp], %[v]\n" \
233 " eor %" #w "[tmp], %" #w "[tmp], %" #w "[val]\n" \
234 " cbnz %" #w "[tmp], 1f\n" \
237 : [tmp] "=&r" (tmp), [v] "+Q" (*(unsigned long *)ptr) \
238 : [val] "r" (val)); \
241 __CMPWAIT_CASE(w, b, 1);
242 __CMPWAIT_CASE(w, h, 2);
243 __CMPWAIT_CASE(w, , 4);
244 __CMPWAIT_CASE( , , 8);
246 #undef __CMPWAIT_CASE
248 #define __CMPWAIT_GEN(sfx) \
249 static inline void __cmpwait##sfx(volatile void *ptr, \
255 return __cmpwait_case##sfx##_1(ptr, (u8)val); \
257 return __cmpwait_case##sfx##_2(ptr, (u16)val); \
259 return __cmpwait_case##sfx##_4(ptr, val); \
261 return __cmpwait_case##sfx##_8(ptr, val); \
273 #define __cmpwait_relaxed(ptr, val) \
274 __cmpwait((ptr), (unsigned long)(val), sizeof(*(ptr)))
276 #endif /* __ASM_CMPXCHG_H */