xref: /freebsd/sys/contrib/ck/include/gcc/ppc/ck_pr.h (revision cbd30a72ca196976c1c700400ecd424baa1b9c16)
1 /*
2  * Copyright 2009-2015 Samy Al Bahra.
3  * Copyright 2012 João Fernandes.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef CK_PR_PPC_H
29 #define CK_PR_PPC_H
30 
31 #ifndef CK_PR_H
32 #error Do not include this file directly, use ck_pr.h
33 #endif
34 
35 #include <ck_cc.h>
36 #include <ck_md.h>
37 
38 /*
39  * The following represent supported atomic operations.
40  * These operations may be emulated.
41  */
42 #include "ck_f_pr.h"
43 
44 /*
45  * Minimum interface requirement met.
46  */
47 #define CK_F_PR
48 
49 /*
50  * This bounces the hardware thread from low to medium
51  * priority. I am unsure of the benefits of this approach
52  * but it is used by the Linux kernel.
53  */
54 CK_CC_INLINE static void
55 ck_pr_stall(void)
56 {
57 
58 	__asm__ __volatile__("or 1, 1, 1;"
59 			     "or 2, 2, 2;" ::: "memory");
60 	return;
61 }
62 
63 #define CK_PR_FENCE(T, I)				\
64 	CK_CC_INLINE static void			\
65 	ck_pr_fence_strict_##T(void)			\
66 	{						\
67 		__asm__ __volatile__(I ::: "memory");   \
68 	}
69 
70 CK_PR_FENCE(atomic, "lwsync")
71 CK_PR_FENCE(atomic_store, "lwsync")
72 CK_PR_FENCE(atomic_load, "sync")
73 CK_PR_FENCE(store_atomic, "lwsync")
74 CK_PR_FENCE(load_atomic, "lwsync")
75 CK_PR_FENCE(store, "lwsync")
76 CK_PR_FENCE(store_load, "sync")
77 CK_PR_FENCE(load, "lwsync")
78 CK_PR_FENCE(load_store, "lwsync")
79 CK_PR_FENCE(memory, "sync")
80 CK_PR_FENCE(acquire, "lwsync")
81 CK_PR_FENCE(release, "lwsync")
82 CK_PR_FENCE(acqrel, "lwsync")
83 CK_PR_FENCE(lock, "lwsync")
84 CK_PR_FENCE(unlock, "lwsync")
85 
86 #undef CK_PR_FENCE
87 
88 #define CK_PR_LOAD(S, M, T, C, I)					\
89 	CK_CC_INLINE static T						\
90 	ck_pr_md_load_##S(const M *target)				\
91 	{								\
92 		T r;							\
93 		__asm__ __volatile__(I "%U1%X1 %0, %1"			\
94 					: "=r" (r)			\
95 					: "m"  (*(const C *)target)	\
96 					: "memory");			\
97 		return (r);						\
98 	}
99 
100 CK_PR_LOAD(ptr, void, void *, uint32_t, "lwz")
101 
102 #define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, T, I)
103 
104 CK_PR_LOAD_S(32, uint32_t, "lwz")
105 CK_PR_LOAD_S(16, uint16_t, "lhz")
106 CK_PR_LOAD_S(8, uint8_t, "lbz")
107 CK_PR_LOAD_S(uint, unsigned int, "lwz")
108 CK_PR_LOAD_S(int, int, "lwz")
109 CK_PR_LOAD_S(short, short, "lhz")
110 CK_PR_LOAD_S(char, char, "lbz")
111 
112 #undef CK_PR_LOAD_S
113 #undef CK_PR_LOAD
114 
115 #define CK_PR_STORE(S, M, T, C, I)				\
116 	CK_CC_INLINE static void				\
117 	ck_pr_md_store_##S(M *target, T v)			\
118 	{							\
119 		__asm__ __volatile__(I "%U0%X0 %1, %0"		\
120 					: "=m" (*(C *)target)	\
121 					: "r" (v)		\
122 					: "memory");		\
123 		return;						\
124 	}
125 
126 CK_PR_STORE(ptr, void, const void *, uint32_t, "stw")
127 
128 #define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, T, I)
129 
130 CK_PR_STORE_S(32, uint32_t, "stw")
131 CK_PR_STORE_S(16, uint16_t, "sth")
132 CK_PR_STORE_S(8, uint8_t, "stb")
133 CK_PR_STORE_S(uint, unsigned int, "stw")
134 CK_PR_STORE_S(int, int, "stw")
135 CK_PR_STORE_S(short, short, "sth")
136 CK_PR_STORE_S(char, char, "stb")
137 
138 #undef CK_PR_STORE_S
139 #undef CK_PR_STORE
140 
141 #define CK_PR_CAS(N, T, M)						\
142 	CK_CC_INLINE static bool					\
143 	ck_pr_cas_##N##_value(M *target, T compare, T set, M *value)	\
144 	{								\
145 		T previous;						\
146 		__asm__ __volatile__("1:"				\
147 				     "lwarx %0, 0, %1;"			\
148 				     "cmpw  0, %0, %3;"			\
149 				     "bne-  2f;"			\
150 				     "stwcx. %2, 0, %1;"		\
151 				     "bne-  1b;"			\
152 				     "2:"				\
153 					: "=&r" (previous)		\
154 					: "r"   (target),		\
155 					  "r"   (set),			\
156 					  "r"   (compare)		\
157 					: "memory", "cc");		\
158 		*(T *)value = previous; 				\
159 		return (previous == compare);				\
160 	}								\
161 	CK_CC_INLINE static bool					\
162 	ck_pr_cas_##N(M *target, T compare, T set)			\
163 	{								\
164 		T previous;						\
165 		__asm__ __volatile__("1:"				\
166 				     "lwarx %0, 0, %1;"			\
167 				     "cmpw  0, %0, %3;"			\
168 				     "bne-  2f;"			\
169 				     "stwcx. %2, 0, %1;"		\
170 				     "bne-  1b;"			\
171 				     "2:"				\
172 					: "=&r" (previous)		\
173 					: "r"   (target),		\
174 					  "r"   (set),			\
175 					  "r"   (compare)		\
176 					: "memory", "cc");		\
177 		return (previous == compare);				\
178 	}
179 
180 CK_PR_CAS(ptr, void *, void)
181 #define CK_PR_CAS_S(a, b) CK_PR_CAS(a, b, b)
182 CK_PR_CAS_S(32, uint32_t)
183 CK_PR_CAS_S(uint, unsigned int)
184 CK_PR_CAS_S(int, int)
185 
186 #undef CK_PR_CAS_S
187 #undef CK_PR_CAS
188 
189 #define CK_PR_FAS(N, M, T, W)					\
190 	CK_CC_INLINE static T					\
191 	ck_pr_fas_##N(M *target, T v)				\
192 	{							\
193 		T previous;					\
194 		__asm__ __volatile__("1:"			\
195 				     "l" W "arx %0, 0, %1;"	\
196 				     "st" W "cx. %2, 0, %1;"	\
197 				     "bne- 1b;"			\
198 					: "=&r" (previous)	\
199 					: "r"   (target),	\
200 					  "r"   (v)		\
201 					: "memory", "cc");	\
202 		return (previous);				\
203 	}
204 
205 CK_PR_FAS(32, uint32_t, uint32_t, "w")
206 CK_PR_FAS(ptr, void, void *, "w")
207 CK_PR_FAS(int, int, int, "w")
208 CK_PR_FAS(uint, unsigned int, unsigned int, "w")
209 
210 #undef CK_PR_FAS
211 
212 #define CK_PR_UNARY(O, N, M, T, I, W)				\
213 	CK_CC_INLINE static void				\
214 	ck_pr_##O##_##N(M *target)				\
215 	{							\
216 		T previous;					\
217 		__asm__ __volatile__("1:"			\
218 				     "l" W "arx %0, 0, %1;"	\
219 				      I ";"			\
220 				     "st" W "cx. %0, 0, %1;"	\
221 				     "bne-  1b;"		\
222 					: "=&r" (previous)	\
223 					: "r"   (target)	\
224 					: "memory", "cc");	\
225 		return;						\
226 	}
227 
228 CK_PR_UNARY(inc, ptr, void, void *, "addic %0, %0, 1", "w")
229 CK_PR_UNARY(dec, ptr, void, void *, "addic %0, %0, -1", "w")
230 CK_PR_UNARY(not, ptr, void, void *, "not %0, %0", "w")
231 CK_PR_UNARY(neg, ptr, void, void *, "neg %0, %0", "w")
232 
233 #define CK_PR_UNARY_S(S, T, W)					\
234 	CK_PR_UNARY(inc, S, T, T, "addic %0, %0, 1", W)		\
235 	CK_PR_UNARY(dec, S, T, T, "addic %0, %0, -1", W)	\
236 	CK_PR_UNARY(not, S, T, T, "not %0, %0", W)		\
237 	CK_PR_UNARY(neg, S, T, T, "neg %0, %0", W)
238 
239 CK_PR_UNARY_S(32, uint32_t, "w")
240 CK_PR_UNARY_S(uint, unsigned int, "w")
241 CK_PR_UNARY_S(int, int, "w")
242 
243 #undef CK_PR_UNARY_S
244 #undef CK_PR_UNARY
245 
246 #define CK_PR_BINARY(O, N, M, T, I, W)				\
247 	CK_CC_INLINE static void				\
248 	ck_pr_##O##_##N(M *target, T delta)			\
249 	{							\
250 		T previous;					\
251 		__asm__ __volatile__("1:"			\
252 				     "l" W "arx %0, 0, %1;"	\
253 				      I " %0, %2, %0;"		\
254 				     "st" W "cx. %0, 0, %1;"	\
255 				     "bne-  1b;"		\
256 					: "=&r" (previous)	\
257 					: "r"   (target),	\
258 					  "r"   (delta)		\
259 					: "memory", "cc");	\
260 		return;						\
261 	}
262 
263 CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "w")
264 CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "w")
265 CK_PR_BINARY(or, ptr, void, uintptr_t, "or", "w")
266 CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "w")
267 CK_PR_BINARY(xor, ptr, void, uintptr_t, "xor", "w")
268 
269 #define CK_PR_BINARY_S(S, T, W)			\
270 	CK_PR_BINARY(and, S, T, T, "and", W)	\
271 	CK_PR_BINARY(add, S, T, T, "add", W)	\
272 	CK_PR_BINARY(or, S, T, T, "or", W)	\
273 	CK_PR_BINARY(sub, S, T, T, "subf", W)	\
274 	CK_PR_BINARY(xor, S, T, T, "xor", W)
275 
276 CK_PR_BINARY_S(32, uint32_t, "w")
277 CK_PR_BINARY_S(uint, unsigned int, "w")
278 CK_PR_BINARY_S(int, int, "w")
279 
280 #undef CK_PR_BINARY_S
281 #undef CK_PR_BINARY
282 
283 CK_CC_INLINE static void *
284 ck_pr_faa_ptr(void *target, uintptr_t delta)
285 {
286 	uintptr_t previous, r;
287 
288 	__asm__ __volatile__("1:"
289 			     "lwarx %0, 0, %2;"
290 			     "add %1, %3, %0;"
291 			     "stwcx. %1, 0, %2;"
292 			     "bne-  1b;"
293 				: "=&r" (previous),
294 				  "=&r" (r)
295 				: "r"   (target),
296 				  "r"   (delta)
297 				: "memory", "cc");
298 
299 	return (void *)(previous);
300 }
301 
302 #define CK_PR_FAA(S, T, W)						\
303 	CK_CC_INLINE static T						\
304 	ck_pr_faa_##S(T *target, T delta)				\
305 	{								\
306 		T previous, r;						\
307 		__asm__ __volatile__("1:"				\
308 				     "l" W "arx %0, 0, %2;"		\
309 				     "add %1, %3, %0;"			\
310 				     "st" W "cx. %1, 0, %2;"		\
311 				     "bne-  1b;"			\
312 					: "=&r" (previous),		\
313 					  "=&r" (r)			\
314 					: "r"   (target),		\
315 					  "r"   (delta)			\
316 					: "memory", "cc");		\
317 		return (previous);					\
318 	}
319 
320 CK_PR_FAA(32, uint32_t, "w")
321 CK_PR_FAA(uint, unsigned int, "w")
322 CK_PR_FAA(int, int, "w")
323 
324 #undef CK_PR_FAA
325 
326 #endif /* CK_PR_PPC_H */
327 
328