Debian Codegraph @

include/gcc/riscv64/ck_pr.h

raw
1 /*
2 * Copyright 2009-2016 Samy Al Bahra.
3 * Copyright 2013-2016 Olivier Houchard.
4 * All rights reserved.
5 * Copyright 2022 The FreeBSD Foundation.
6 *
7 * Portions of this software were developed by Mitchell Horne
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef CK_PR_RISCV64_H
33 #define CK_PR_RISCV64_H
34
35 #ifndef CK_PR_H
36 #error Do not include this file directly, use ck_pr.h
37 #endif
38
39 #include <ck_cc.h>
40 #include <ck_md.h>
41
42 #if !defined(__riscv_xlen) || __riscv_xlen != 64
43 #error "only for riscv64!"
44 #endif
45
46 /*
47 * The following represent supported atomic operations.
48 * These operations may be emulated.
49 */
50 #include "ck_f_pr.h"
51
52 /*
53 * Minimum interface requirement met.
54 */
55 #define CK_F_PR
56
57 CK_CC_INLINE static void
58 ck_pr_stall(void)
59 {
60
61 __asm__ __volatile__("" ::: "memory");
62 return;
63 }
64
65 /*
66 * The FENCE instruction is defined in terms of predecessor and successor bits.
67 * This allows for greater granularity in specifying whether reads (loads) or
68 * writes (stores) may pass over either side of the fence.
69 *
70 * e.g. "fence r,rw" creates a barrier with acquire semantics.
71 *
72 * Note that atomic memory operations (AMOs) are defined by the RISC-V spec to
73 * act as both a load and store memory operation (read-modify-write, in other
74 * words). Thus, any of r, w, or rw will enforce ordering on an AMO.
75 */
76 #define CK_FENCE(p, s) __asm __volatile("fence " #p "," #s ::: "memory");
77 #define CK_FENCE_RW_RW CK_FENCE(rw,rw)
78
79 #define CK_PR_FENCE(T, I) \
80 CK_CC_INLINE static void \
81 ck_pr_fence_strict_##T(void) \
82 { \
83 I; \
84 }
85
86 CK_PR_FENCE(atomic, CK_FENCE_RW_RW)
87 CK_PR_FENCE(atomic_store, CK_FENCE(rw,w))
88 CK_PR_FENCE(atomic_load, CK_FENCE(rw,r))
89 CK_PR_FENCE(store_atomic, CK_FENCE(w,rw))
90 CK_PR_FENCE(load_atomic, CK_FENCE(r,rw))
91 CK_PR_FENCE(store, CK_FENCE(w,w))
92 CK_PR_FENCE(store_load, CK_FENCE(w,r))
93 CK_PR_FENCE(load, CK_FENCE(r,r))
94 CK_PR_FENCE(load_store, CK_FENCE(r,w))
95 CK_PR_FENCE(memory, CK_FENCE_RW_RW)
96 CK_PR_FENCE(acquire, CK_FENCE(r,rw))
97 CK_PR_FENCE(release, CK_FENCE(rw,w))
98 CK_PR_FENCE(acqrel, CK_FENCE_RW_RW)
99 CK_PR_FENCE(lock, CK_FENCE_RW_RW)
100 CK_PR_FENCE(unlock, CK_FENCE_RW_RW)
101
102 #undef CK_PR_FENCE
103
104 #undef CK_FENCE_RW_RW
105 #undef CK_FENCE
106
107 /*
108 * ck_pr_load(3)
109 */
110 #define CK_PR_LOAD(S, M, T, I) \
111 CK_CC_INLINE static T \
112 ck_pr_md_load_##S(const M *target) \
113 { \
114 long r = 0; \
115 __asm__ __volatile__(I " %0, 0(%1)\n" \
116 : "=r" (r) \
117 : "r" (target) \
118 : "memory"); \
119 return ((T)r); \
120 }
121 #define CK_PR_LOAD_S(S, T, I) CK_PR_LOAD(S, T, T, I)
122
123 CK_PR_LOAD(ptr, void, void *, "ld")
124 CK_PR_LOAD_S(64, uint64_t, "ld")
125 CK_PR_LOAD_S(32, uint32_t, "lwu")
126 CK_PR_LOAD_S(16, uint16_t, "lhu")
127 CK_PR_LOAD_S(8, uint8_t, "lbu")
128 CK_PR_LOAD_S(uint, unsigned int, "lwu")
129 CK_PR_LOAD_S(int, int, "lw")
130 CK_PR_LOAD_S(short, short, "lh")
131 CK_PR_LOAD_S(char, char, "lb")
132 #ifndef CK_PR_DISABLE_DOUBLE
133 CK_PR_LOAD_S(double, double, "ld")
134 #endif
135
136 #undef CK_PR_LOAD_S
137 #undef CK_PR_LOAD
138
139 /*
140 * ck_pr_store(3)
141 */
142 #define CK_PR_STORE(S, M, T, I) \
143 CK_CC_INLINE static void \
144 ck_pr_md_store_##S(M *target, T val) \
145 { \
146 __asm__ __volatile__(I " %1, 0(%0)" \
147 : \
148 : "r" (target), \
149 "r" (val) \
150 : "memory"); \
151 }
152 #define CK_PR_STORE_S(S, T, I) CK_PR_STORE(S, T, T, I)
153
154 CK_PR_STORE(ptr, void, const void *, "sd")
155 CK_PR_STORE_S(64, uint64_t, "sd")
156 CK_PR_STORE_S(32, uint32_t, "sw")
157 CK_PR_STORE_S(16, uint16_t, "sh")
158 CK_PR_STORE_S(8, uint8_t, "sb")
159 CK_PR_STORE_S(uint, unsigned int, "sw")
160 CK_PR_STORE_S(int, int, "sw")
161 CK_PR_STORE_S(short, short, "sh")
162 CK_PR_STORE_S(char, char, "sb")
163 #ifndef CK_PR_DISABLE_DOUBLE
164 CK_PR_STORE_S(double, double, "sd")
165 #endif
166
167 #undef CK_PR_STORE_S
168 #undef CK_PR_STORE
169
170 /*
171 * ck_pr_cas(3)
172 *
173 * NB: 'S' is to cast compare to a signed 32-bit integer, so the value will be
174 * sign-extended when passed to inline asm. GCC does this sign extension
175 * implicitly, while clang does not. It is necessary because lr.w sign-extends
176 * the value read from memory, so compare must match that to avoid looping
177 * unconditionally.
178 */
179 #define CK_PR_CAS(N, M, T, C, S, W) \
180 CK_CC_INLINE static bool \
181 ck_pr_cas_##N##_value(M *target, T compare, T set, M *value) \
182 { \
183 T previous; \
184 int tmp; \
185 __asm__ __volatile__("1:" \
186 "li %[tmp], 1\n" \
187 "lr." W " %[p], %[t]\n" \
188 "bne %[p], %[c], 2f\n" \
189 "sc." W " %[tmp], %[s], %[t]\n" \
190 "bnez %[tmp], 1b\n" \
191 "2:" \
192 : [p]"=&r" (previous), \
193 [tmp]"=&r" (tmp), \
194 [t]"+A" (*(C *)target) \
195 : [s]"r" (set), \
196 [c]"r" ((long)(S)compare) \
197 : "memory"); \
198 *(T *)value = previous; \
199 return (tmp == 0); \
200 } \
201 CK_CC_INLINE static bool \
202 ck_pr_cas_##N(M *target, T compare, T set) \
203 { \
204 T previous; \
205 int tmp; \
206 __asm__ __volatile__("1:" \
207 "li %[tmp], 1\n" \
208 "lr." W " %[p], %[t]\n" \
209 "bne %[p], %[c], 2f\n" \
210 "sc." W " %[tmp], %[s], %[t]\n" \
211 "bnez %[tmp], 1b\n" \
212 "2:" \
213 : [p]"=&r" (previous), \
214 [tmp]"=&r" (tmp), \
215 [t]"+A" (*(C *)target) \
216 : [s]"r" (set), \
217 [c]"r" ((long)(S)compare) \
218 : "memory"); \
219 return (tmp == 0); \
220 }
221 #define CK_PR_CAS_S(N, T, W) CK_PR_CAS(N, T, T, T, T, W)
222 #define CK_PR_CAS_32_S(N, T, W) CK_PR_CAS(N, T, T, T, int32_t, W)
223
224 CK_PR_CAS(ptr, void, void *, uint64_t, uint64_t, "d")
225 CK_PR_CAS_S(64, uint64_t, "d")
226 CK_PR_CAS_32_S(32, uint32_t, "w")
227 CK_PR_CAS_32_S(uint, unsigned int, "w")
228 CK_PR_CAS_32_S(int, int, "w")
229 #ifndef CK_PR_DISABLE_DOUBLE
230 CK_PR_CAS_S(double, double, "d")
231 #endif
232
233 #undef CK_PR_CAS_S
234 #undef CK_PR_CAS
235
236 /*
237 * ck_pr_faa(3)
238 */
239 #define CK_PR_FAA(N, M, T, C, W) \
240 CK_CC_INLINE static T \
241 ck_pr_faa_##N(M *target, T delta) \
242 { \
243 T previous; \
244 __asm__ __volatile__("amoadd." W " %0, %2, %1\n" \
245 : "=&r" (previous), \
246 "+A" (*(C *)target) \
247 : "r" (delta) \
248 : "memory"); \
249 return (previous); \
250 }
251 #define CK_PR_FAA_S(N, T, W) CK_PR_FAA(N, T, T, T, W)
252
253 CK_PR_FAA(ptr, void, void *, uint64_t, "d")
254 CK_PR_FAA_S(64, uint64_t, "d")
255 CK_PR_FAA_S(32, uint32_t, "w")
256 CK_PR_FAA_S(uint, unsigned int, "w")
257 CK_PR_FAA_S(int, int, "w")
258
259 #undef CK_PR_FAA_S
260 #undef CK_PR_FAA
261
262 /*
263 * ck_pr_fas(3)
264 */
265 #define CK_PR_FAS(N, M, T, C, W) \
266 CK_CC_INLINE static T \
267 ck_pr_fas_##N(M *target, T val) \
268 { \
269 T previous; \
270 __asm__ __volatile__("amoswap." W " %0, %2, %1\n" \
271 : "=&r" (previous), \
272 "+A" (*(C *)target) \
273 : "r" (val) \
274 : "memory"); \
275 return (previous); \
276 }
277 #define CK_PR_FAS_S(N, T, W) CK_PR_FAS(N, T, T, T, W)
278
279 CK_PR_FAS(ptr, void, void *, uint64_t, "d")
280 CK_PR_FAS_S(64, uint64_t, "d")
281 CK_PR_FAS_S(32, uint32_t, "w")
282 CK_PR_FAS_S(int, int, "w")
283 CK_PR_FAS_S(uint, unsigned int, "w")
284
285 #undef CK_PR_FAS_S
286 #undef CK_PR_FAS
287
288 /*
289 * ck_pr_add(3)
290 */
291 #define CK_PR_ADD(N, M, T, C, W) \
292 CK_CC_INLINE static void \
293 ck_pr_add_##N(M *target, T val) \
294 { \
295 __asm__ __volatile__("amoadd." W " zero, %1, %0\n" \
296 : "+A" (*(C *)target) \
297 : "r" (val) \
298 : "memory"); \
299 } \
300 CK_CC_INLINE static bool \
301 ck_pr_add_##N##_is_zero(M *target, T val) \
302 { \
303 T previous; \
304 __asm__ __volatile__("amoadd." W " %0, %2, %1\n" \
305 : "=&r" (previous), \
306 "+A" (*(C *)target) \
307 : "r" (val) \
308 : "memory"); \
309 return (((C)previous + (C)val) == 0); \
310 }
311 #define CK_PR_ADD_S(N, T, W) CK_PR_ADD(N, T, T, T, W)
312
313 CK_PR_ADD(ptr, void, void *, uint64_t, "d")
314 CK_PR_ADD_S(64, uint64_t, "d")
315 CK_PR_ADD_S(32, uint32_t, "w")
316 CK_PR_ADD_S(uint, unsigned int, "w")
317 CK_PR_ADD_S(int, int, "w")
318
319 #undef CK_PR_ADD_S
320 #undef CK_PR_ADD
321
322 /*
323 * ck_pr_inc(3)
324 *
325 * Implemented in terms of ck_pr_add(3); RISC-V has no atomic inc or dec
326 * instructions.
327 */
328 #define CK_PR_INC(N, M, T, W) \
329 CK_CC_INLINE static void \
330 ck_pr_inc_##N(M *target) \
331 { \
332 ck_pr_add_##N(target, (T)1); \
333 } \
334 CK_CC_INLINE static bool \
335 ck_pr_inc_##N##_is_zero(M *target) \
336 { \
337 return (ck_pr_add_##N##_is_zero(target, (T)1)); \
338 }
339 #define CK_PR_INC_S(N, T, W) CK_PR_INC(N, T, T, W)
340
341 CK_PR_INC(ptr, void, void *, "d")
342 CK_PR_INC_S(64, uint64_t, "d")
343 CK_PR_INC_S(32, uint32_t, "w")
344 CK_PR_INC_S(uint, unsigned int, "w")
345 CK_PR_INC_S(int, int, "w")
346
347 #undef CK_PR_INC_S
348 #undef CK_PR_INC
349
350 /*
351 * ck_pr_sub(3)
352 */
353 #define CK_PR_SUB(N, M, T, C, W) \
354 CK_CC_INLINE static void \
355 ck_pr_sub_##N(M *target, T val) \
356 { \
357 __asm__ __volatile__("amoadd." W " zero, %1, %0\n" \
358 : "+A" (*(C *)target) \
359 : "r" (-(C)val) \
360 : "memory"); \
361 } \
362 CK_CC_INLINE static bool \
363 ck_pr_sub_##N##_is_zero(M *target, T val) \
364 { \
365 T previous; \
366 __asm__ __volatile__("amoadd." W " %0, %2, %1\n" \
367 : "=&r" (previous), \
368 "+A" (*(C *)target) \
369 : "r" (-(C)val) \
370 : "memory"); \
371 return (((C)previous - (C)val) == 0); \
372 }
373 #define CK_PR_SUB_S(N, T, W) CK_PR_SUB(N, T, T, T, W)
374
375 CK_PR_SUB(ptr, void, void *, uint64_t, "d")
376 CK_PR_SUB_S(64, uint64_t, "d")
377 CK_PR_SUB_S(32, uint32_t, "w")
378 CK_PR_SUB_S(uint, unsigned int, "w")
379 CK_PR_SUB_S(int, int, "w")
380
381 #undef CK_PR_SUB_S
382 #undef CK_PR_SUB
383
384 /*
385 * ck_pr_dec(3)
386 */
387 #define CK_PR_DEC(N, M, T, W) \
388 CK_CC_INLINE static void \
389 ck_pr_dec_##N(M *target) \
390 { \
391 ck_pr_sub_##N(target, (T)1); \
392 } \
393 CK_CC_INLINE static bool \
394 ck_pr_dec_##N##_is_zero(M *target) \
395 { \
396 return (ck_pr_sub_##N##_is_zero(target, (T)1)); \
397 }
398 #define CK_PR_DEC_S(N, T, W) CK_PR_DEC(N, T, T, W)
399
400 CK_PR_DEC(ptr, void, void *, "d")
401 CK_PR_DEC_S(64, uint64_t, "d")
402 CK_PR_DEC_S(32, uint32_t, "w")
403 CK_PR_DEC_S(uint, unsigned int, "w")
404 CK_PR_DEC_S(int, int, "w")
405
406 #undef CK_PR_DEC_S
407 #undef CK_PR_DEC
408
409 /*
410 * ck_pr_neg(3)
411 */
412 #define CK_PR_NEG(N, M, T, C, W) \
413 CK_CC_INLINE static void \
414 ck_pr_neg_##N(M *target) \
415 { \
416 __asm__ __volatile__("1:" \
417 "lr." W " t0, %0\n" \
418 "sub t0, zero, t0\n" \
419 "sc." W " t1, t0, %0\n" \
420 "bnez t1, 1b\n" \
421 : "+A" (*(C *)target) \
422 : \
423 : "t0", "t1", "memory"); \
424 }
425 #define CK_PR_NEG_S(N, T, W) CK_PR_NEG(N, T, T, T, W)
426
427 CK_PR_NEG(ptr, void, void *, uint64_t, "d")
428 CK_PR_NEG_S(64, uint64_t, "d")
429 CK_PR_NEG_S(32, uint32_t, "w")
430 CK_PR_NEG_S(uint, unsigned int, "w")
431 CK_PR_NEG_S(int, int, "w")
432
433 #undef CK_PR_NEG_S
434 #undef CK_PR_NEG
435
436 /*
437 * ck_pr_not(3)
438 */
439 #define CK_PR_NOT(N, M, T, C, W) \
440 CK_CC_INLINE static void \
441 ck_pr_not_##N(M *target) \
442 { \
443 __asm__ __volatile__("1:" \
444 "lr." W " t0, %0\n" \
445 "not t0, t0\n" \
446 "sc." W " t1, t0, %0\n" \
447 "bnez t1, 1b\n" \
448 : "+A" (*(C *)target) \
449 : \
450 : "t0", "t1", "memory"); \
451 }
452 #define CK_PR_NOT_S(N, T, W) CK_PR_NOT(N, T, T, T, W)
453
454 CK_PR_NOT(ptr, void, void *, uint64_t, "d")
455 CK_PR_NOT_S(64, uint64_t, "d")
456 CK_PR_NOT_S(32, uint32_t, "w")
457 CK_PR_NOT_S(uint, unsigned int, "w")
458 CK_PR_NOT_S(int, int, "w")
459
460 #undef CK_PR_NOT_S
461 #undef CK_PR_NOT
462
463 /*
464 * ck_pr_and(3), ck_pr_or(3), and ck_pr_xor(3)
465 */
466 #define CK_PR_BINARY(O, N, M, T, C, I, W) \
467 CK_CC_INLINE static void \
468 ck_pr_##O##_##N(M *target, T delta) \
469 { \
470 __asm__ __volatile__(I "." W " zero, %1, %0\n" \
471 : "+A" (*(C *)target) \
472 : "r" (delta) \
473 : "memory"); \
474 }
475
476 CK_PR_BINARY(and, ptr, void, void *, uint64_t, "amoand", "d")
477 CK_PR_BINARY(or, ptr, void, void *, uint64_t, "amoor", "d")
478 CK_PR_BINARY(xor, ptr, void, void *, uint64_t, "amoxor", "d")
479
480 #define CK_PR_BINARY_S(S, T, W) \
481 CK_PR_BINARY(and, S, T, T, T, "amoand", W) \
482 CK_PR_BINARY(or, S, T, T, T, "amoor", W) \
483 CK_PR_BINARY(xor, S, T, T, T, "amoxor", W) \
484
485 CK_PR_BINARY_S(64, uint64_t, "d")
486 CK_PR_BINARY_S(32, uint32_t, "w")
487 CK_PR_BINARY_S(uint, unsigned int, "w")
488 CK_PR_BINARY_S(int, int, "w")
489
490 #undef CK_PR_BINARY_S
491 #undef CK_PR_BINARY
492
493 /*
494 * ck_pr_btc(3), ck_pr_btr(3), and ck_pr_bts(3)
495 */
496 #define CK_PR_BTX(K, S, I, W, M, C, O) \
497 CK_CC_INLINE static bool \
498 ck_pr_##K##_##S(M *target, unsigned int idx) \
499 { \
500 C ret; \
501 C mask = (C)0x1 << idx; \
502 __asm__ __volatile__(I "." W " %1, %2, %0\n" \
503 : "+A" (*(C *)target), \
504 "=r" (ret) \
505 : "r" (O(mask)) \
506 : "memory", "cc"); \
507 return ((ret & mask) != 0); \
508 }
509
510 #define CK_PR_BTC(S, W, M, C) CK_PR_BTX(btc, S, "amoxor", W, M, C, 0+)
511 #define CK_PR_BTC_S(S, W, T) CK_PR_BTC(S, W, T, T)
512
513 CK_PR_BTC(ptr, "d", void, uint64_t)
514 CK_PR_BTC_S(64, "d", uint64_t)
515 CK_PR_BTC_S(32, "w", uint32_t)
516 CK_PR_BTC_S(uint, "w", unsigned int)
517 CK_PR_BTC_S(int, "w", int)
518
519 #undef CK_PR_BTC_S
520 #undef CK_PR_BTC
521
522 #define CK_PR_BTR(S, W, M, C) CK_PR_BTX(btr, S, "amoand", W, M, C, ~)
523 #define CK_PR_BTR_S(S, W, T) CK_PR_BTR(S, W, T, T)
524
525 CK_PR_BTR(ptr, "d", void, uint64_t)
526 CK_PR_BTR_S(64, "d", uint64_t)
527 CK_PR_BTR_S(32, "w", uint32_t)
528 CK_PR_BTR_S(uint, "w", unsigned int)
529 CK_PR_BTR_S(int, "w", int)
530
531 #undef CK_PR_BTR_S
532 #undef CK_PR_BTR
533
534 #define CK_PR_BTS(S, W, M, C) CK_PR_BTX(bts, S, "amoor", W, M, C, 0+)
535 #define CK_PR_BTS_S(S, W, T) CK_PR_BTS(S, W, T, T)
536
537 CK_PR_BTS(ptr, "d", void, uint64_t)
538 CK_PR_BTS_S(64, "d", uint64_t)
539 CK_PR_BTS_S(32, "w", uint32_t)
540 CK_PR_BTS_S(uint, "w", unsigned int)
541 CK_PR_BTS_S(int, "w", int)
542
543 #undef CK_PR_BTS_S
544 #undef CK_PR_BTS
545
546 #undef CK_PR_BTX
547
548 #endif /* CK_PR_RISCV64_H */