xref: /linux/arch/csky/include/asm/cmpxchg.h (revision bb29b957eaaa15be33243550d9ee576c2d601669)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef __ASM_CSKY_CMPXCHG_H
4 #define __ASM_CSKY_CMPXCHG_H
5 
6 #ifdef CONFIG_SMP
7 #include <linux/bug.h>
8 #include <asm/barrier.h>
9 #include <linux/cmpxchg-emu.h>
10 
11 #define __xchg_relaxed(new, ptr, size)				\
12 ({								\
13 	__typeof__(ptr) __ptr = (ptr);				\
14 	__typeof__(new) __new = (new);				\
15 	__typeof__(*(ptr)) __ret;				\
16 	unsigned long tmp;					\
17 	switch (size) {						\
18 	case 2: {						\
19 		u32 ret;					\
20 		u32 shif = ((ulong)__ptr & 2) ? 16 : 0;		\
21 		u32 mask = 0xffff << shif;			\
22 		__ptr = (__typeof__(ptr))((ulong)__ptr & ~2);	\
23 		__asm__ __volatile__ (				\
24 			"1:	ldex.w %0, (%4)\n"		\
25 			"	and    %1, %0, %2\n"		\
26 			"	or     %1, %1, %3\n"		\
27 			"	stex.w %1, (%4)\n"		\
28 			"	bez    %1, 1b\n"		\
29 			: "=&r" (ret), "=&r" (tmp)		\
30 			: "r" (~mask),				\
31 			  "r" ((u32)__new << shif),		\
32 			  "r" (__ptr)				\
33 			: "memory");				\
34 		__ret = (__typeof__(*(ptr)))			\
35 			((ret & mask) >> shif);			\
36 		break;						\
37 	}							\
38 	case 4:							\
39 		asm volatile (					\
40 		"1:	ldex.w		%0, (%3) \n"		\
41 		"	mov		%1, %2   \n"		\
42 		"	stex.w		%1, (%3) \n"		\
43 		"	bez		%1, 1b   \n"		\
44 			: "=&r" (__ret), "=&r" (tmp)		\
45 			: "r" (__new), "r"(__ptr)		\
46 			:);					\
47 		break;						\
48 	default:						\
49 		BUILD_BUG();					\
50 	}							\
51 	__ret;							\
52 })
53 
54 #define arch_xchg_relaxed(ptr, x) \
55 		(__xchg_relaxed((x), (ptr), sizeof(*(ptr))))
56 
57 #define __cmpxchg_relaxed(ptr, old, new, size)			\
58 ({								\
59 	__typeof__(ptr) __ptr = (ptr);				\
60 	__typeof__(new) __new = (new);				\
61 	__typeof__(new) __tmp;					\
62 	__typeof__(old) __old = (old);				\
63 	__typeof__(*(ptr)) __ret;				\
64 	switch (size) {						\
65 	case 1:							\
66 		__ret = (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, (uintptr_t)__old, (uintptr_t)__new); \
67 		break;							\
68 	case 2:							\
69 		__ret = (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *)__ptr, (unsigned long)__old, (unsigned long)__new); \
70 		break;						\
71 	case 4:							\
72 		asm volatile (					\
73 		"1:	ldex.w		%0, (%3) \n"		\
74 		"	cmpne		%0, %4   \n"		\
75 		"	bt		2f       \n"		\
76 		"	mov		%1, %2   \n"		\
77 		"	stex.w		%1, (%3) \n"		\
78 		"	bez		%1, 1b   \n"		\
79 		"2:				 \n"		\
80 			: "=&r" (__ret), "=&r" (__tmp)		\
81 			: "r" (__new), "r"(__ptr), "r"(__old)	\
82 			:);					\
83 		break;						\
84 	default:						\
85 		BUILD_BUG();					\
86 	}							\
87 	__ret;							\
88 })
89 
90 #define arch_cmpxchg_relaxed(ptr, o, n) \
91 	(__cmpxchg_relaxed((ptr), (o), (n), sizeof(*(ptr))))
92 
93 #define __cmpxchg_acquire(ptr, old, new, size)			\
94 ({								\
95 	__typeof__(ptr) __ptr = (ptr);				\
96 	__typeof__(new) __new = (new);				\
97 	__typeof__(new) __tmp;					\
98 	__typeof__(old) __old = (old);				\
99 	__typeof__(*(ptr)) __ret;				\
100 	switch (size) {						\
101 	case 1:							\
102 		__ret = (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, (uintptr_t)__old, (uintptr_t)__new); \
103 		break;							\
104 	case 2:							\
105 		__ret = (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *)__ptr, (unsigned long)__old, (unsigned long)__new); \
106 		break;						\
107 	case 4:							\
108 		asm volatile (					\
109 		"1:	ldex.w		%0, (%3) \n"		\
110 		"	cmpne		%0, %4   \n"		\
111 		"	bt		2f       \n"		\
112 		"	mov		%1, %2   \n"		\
113 		"	stex.w		%1, (%3) \n"		\
114 		"	bez		%1, 1b   \n"		\
115 		ACQUIRE_FENCE					\
116 		"2:				 \n"		\
117 			: "=&r" (__ret), "=&r" (__tmp)		\
118 			: "r" (__new), "r"(__ptr), "r"(__old)	\
119 			:);					\
120 		break;						\
121 	default:						\
122 		BUILD_BUG();					\
123 	}							\
124 	__ret;							\
125 })
126 
127 #define arch_cmpxchg_acquire(ptr, o, n) \
128 	(__cmpxchg_acquire((ptr), (o), (n), sizeof(*(ptr))))
129 
130 #define __cmpxchg(ptr, old, new, size)				\
131 ({								\
132 	__typeof__(ptr) __ptr = (ptr);				\
133 	__typeof__(new) __new = (new);				\
134 	__typeof__(new) __tmp;					\
135 	__typeof__(old) __old = (old);				\
136 	__typeof__(*(ptr)) __ret;				\
137 	switch (size) {						\
138 	case 1:							\
139 		__ret = (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, (uintptr_t)__old, (uintptr_t)__new); \
140 		break;							\
141 	case 2:							\
142 		__ret = (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *)__ptr, (unsigned long)__old, (unsigned long)__new); \
143 		break;						\
144 	case 4:							\
145 		asm volatile (					\
146 		RELEASE_FENCE					\
147 		"1:	ldex.w		%0, (%3) \n"		\
148 		"	cmpne		%0, %4   \n"		\
149 		"	bt		2f       \n"		\
150 		"	mov		%1, %2   \n"		\
151 		"	stex.w		%1, (%3) \n"		\
152 		"	bez		%1, 1b   \n"		\
153 		FULL_FENCE					\
154 		"2:				 \n"		\
155 			: "=&r" (__ret), "=&r" (__tmp)		\
156 			: "r" (__new), "r"(__ptr), "r"(__old)	\
157 			:);					\
158 		break;						\
159 	default:						\
160 		BUILD_BUG();					\
161 	}							\
162 	__ret;							\
163 })
164 
165 #define arch_cmpxchg(ptr, o, n)					\
166 	(__cmpxchg((ptr), (o), (n), sizeof(*(ptr))))
167 
168 #define arch_cmpxchg_local(ptr, o, n)				\
169 	(__cmpxchg_relaxed((ptr), (o), (n), sizeof(*(ptr))))
170 #else
171 #include <asm-generic/cmpxchg.h>
172 #endif
173 
174 #endif /* __ASM_CSKY_CMPXCHG_H */
175