xref: /kvm-unit-tests/lib/riscv/asm/csr.h (revision 22f287f4024419702c8024174139d1b46d12f2be)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _ASMRISCV_CSR_H_
3 #define _ASMRISCV_CSR_H_
4 #include <linux/const.h>
5 
6 #define CSR_SSCRATCH		0x140
7 
8 #ifndef __ASSEMBLY__
9 
10 #define csr_swap(csr, val)					\
11 ({								\
12 	unsigned long __v = (unsigned long)(val);		\
13 	__asm__ __volatile__ ("csrrw %0, " __ASM_STR(csr) ", %1"\
14 				: "=r" (__v) : "rK" (__v)	\
15 				: "memory");			\
16 	__v;							\
17 })
18 
19 #define csr_read(csr)						\
20 ({								\
21 	register unsigned long __v;				\
22 	__asm__ __volatile__ ("csrr %0, " __ASM_STR(csr)	\
23 				: "=r" (__v) :			\
24 				: "memory");			\
25 	__v;							\
26 })
27 
28 #define csr_write(csr, val)					\
29 ({								\
30 	unsigned long __v = (unsigned long)(val);		\
31 	__asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0"	\
32 				: : "rK" (__v)			\
33 				: "memory");			\
34 })
35 
36 #define csr_read_set(csr, val)					\
37 ({								\
38 	unsigned long __v = (unsigned long)(val);		\
39 	__asm__ __volatile__ ("csrrs %0, " __ASM_STR(csr) ", %1"\
40 				: "=r" (__v) : "rK" (__v)	\
41 				: "memory");			\
42 	__v;							\
43 })
44 
45 #define csr_set(csr, val)					\
46 ({								\
47 	unsigned long __v = (unsigned long)(val);		\
48 	__asm__ __volatile__ ("csrs " __ASM_STR(csr) ", %0"	\
49 				: : "rK" (__v)			\
50 				: "memory");			\
51 })
52 
53 #define csr_read_clear(csr, val)				\
54 ({								\
55 	unsigned long __v = (unsigned long)(val);		\
56 	__asm__ __volatile__ ("csrrc %0, " __ASM_STR(csr) ", %1"\
57 				: "=r" (__v) : "rK" (__v)	\
58 				: "memory");			\
59 	__v;							\
60 })
61 
62 #define csr_clear(csr, val)					\
63 ({								\
64 	unsigned long __v = (unsigned long)(val);		\
65 	__asm__ __volatile__ ("csrc " __ASM_STR(csr) ", %0"	\
66 				: : "rK" (__v)			\
67 				: "memory");			\
68 })
69 
70 #endif /* !__ASSEMBLY__ */
71 #endif /* _ASMRISCV_CSR_H_ */
72