xref: /linux/rust/helpers/atomic_ext.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <asm/barrier.h>
4 #include <asm/rwonce.h>
5 #include <linux/atomic.h>
6 
7 #define GEN_READ_HELPER(tname, type)						\
8 __rust_helper type rust_helper_atomic_##tname##_read(type *ptr)			\
9 {										\
10 	return READ_ONCE(*ptr);							\
11 }
12 
13 #define GEN_SET_HELPER(tname, type)						\
14 __rust_helper void rust_helper_atomic_##tname##_set(type *ptr, type val)	\
15 {										\
16 	WRITE_ONCE(*ptr, val);							\
17 }
18 
19 #define GEN_READ_ACQUIRE_HELPER(tname, type)					\
20 __rust_helper type rust_helper_atomic_##tname##_read_acquire(type *ptr)		\
21 {										\
22 	return smp_load_acquire(ptr);						\
23 }
24 
25 #define GEN_SET_RELEASE_HELPER(tname, type)					\
26 __rust_helper void rust_helper_atomic_##tname##_set_release(type *ptr, type val)\
27 {										\
28 	smp_store_release(ptr, val);						\
29 }
30 
31 #define GEN_READ_SET_HELPERS(tname, type)					\
32 	GEN_READ_HELPER(tname, type)						\
33 	GEN_SET_HELPER(tname, type)						\
34 	GEN_READ_ACQUIRE_HELPER(tname, type)					\
35 	GEN_SET_RELEASE_HELPER(tname, type)					\
36 
37 GEN_READ_SET_HELPERS(i8, s8)
38 GEN_READ_SET_HELPERS(i16, s16)
39 GEN_READ_SET_HELPERS(ptr, const void *)
40 
41 /*
42  * xchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the
43  * architecture provding xchg() support for i8 and i16.
44  *
45  * The architectures that currently support Rust (x86_64, armv7,
46  * arm64, riscv, and loongarch) satisfy these requirements.
47  */
48 #define GEN_XCHG_HELPER(tname, type, suffix)					\
49 __rust_helper type								\
50 rust_helper_atomic_##tname##_xchg##suffix(type *ptr, type new)			\
51 {										\
52 	return xchg##suffix(ptr, new);					\
53 }
54 
55 #define GEN_XCHG_HELPERS(tname, type)						\
56 	GEN_XCHG_HELPER(tname, type, )						\
57 	GEN_XCHG_HELPER(tname, type, _acquire)					\
58 	GEN_XCHG_HELPER(tname, type, _release)					\
59 	GEN_XCHG_HELPER(tname, type, _relaxed)					\
60 
61 GEN_XCHG_HELPERS(i8, s8)
62 GEN_XCHG_HELPERS(i16, s16)
63 GEN_XCHG_HELPERS(ptr, const void *)
64 
65 /*
66  * try_cmpxchg helpers depend on ARCH_SUPPORTS_ATOMIC_RMW and on the
67  * architecture provding try_cmpxchg() support for i8 and i16.
68  *
69  * The architectures that currently support Rust (x86_64, armv7,
70  * arm64, riscv, and loongarch) satisfy these requirements.
71  */
72 #define GEN_TRY_CMPXCHG_HELPER(tname, type, suffix)				\
73 __rust_helper bool								\
74 rust_helper_atomic_##tname##_try_cmpxchg##suffix(type *ptr, type *old, type new)\
75 {										\
76 	return try_cmpxchg##suffix(ptr, old, new);				\
77 }
78 
79 #define GEN_TRY_CMPXCHG_HELPERS(tname, type)					\
80 	GEN_TRY_CMPXCHG_HELPER(tname, type, )					\
81 	GEN_TRY_CMPXCHG_HELPER(tname, type, _acquire)				\
82 	GEN_TRY_CMPXCHG_HELPER(tname, type, _release)				\
83 	GEN_TRY_CMPXCHG_HELPER(tname, type, _relaxed)				\
84 
85 GEN_TRY_CMPXCHG_HELPERS(i8, s8)
86 GEN_TRY_CMPXCHG_HELPERS(i16, s16)
87 GEN_TRY_CMPXCHG_HELPERS(ptr, const void *)
88