xref: /kvm-unit-tests/lib/riscv/asm/csr.h (revision 386ed5c2f9e98a2605817748c44c0cefa60dc88e)
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_SSTATUS		0x100
7 #define CSR_STVEC		0x105
8 #define CSR_SSCRATCH		0x140
9 #define CSR_SEPC		0x141
10 #define CSR_SCAUSE		0x142
11 #define CSR_STVAL		0x143
12 #define CSR_SATP		0x180
13 
14 /* Exception cause high bit - is an interrupt if set */
15 #define CAUSE_IRQ_FLAG		(_AC(1, UL) << (__riscv_xlen - 1))
16 
17 /* Exception causes */
18 #define EXC_INST_MISALIGNED	0
19 #define EXC_INST_ACCESS		1
20 #define EXC_INST_ILLEGAL	2
21 #define EXC_BREAKPOINT		3
22 #define EXC_LOAD_MISALIGNED	4
23 #define EXC_LOAD_ACCESS		5
24 #define EXC_STORE_MISALIGNED	6
25 #define EXC_STORE_ACCESS	7
26 #define EXC_SYSCALL		8
27 #define EXC_HYPERVISOR_SYSCALL	9
28 #define EXC_SUPERVISOR_SYSCALL	10
29 #define EXC_INST_PAGE_FAULT	12
30 #define EXC_LOAD_PAGE_FAULT	13
31 #define EXC_STORE_PAGE_FAULT	15
32 #define EXC_INST_GUEST_PAGE_FAULT	20
33 #define EXC_LOAD_GUEST_PAGE_FAULT	21
34 #define EXC_VIRTUAL_INST_FAULT		22
35 #define EXC_STORE_GUEST_PAGE_FAULT	23
36 
37 #ifndef __ASSEMBLY__
38 
39 #define csr_swap(csr, val)					\
40 ({								\
41 	unsigned long __v = (unsigned long)(val);		\
42 	__asm__ __volatile__ ("csrrw %0, " __ASM_STR(csr) ", %1"\
43 				: "=r" (__v) : "rK" (__v)	\
44 				: "memory");			\
45 	__v;							\
46 })
47 
48 #define csr_read(csr)						\
49 ({								\
50 	register unsigned long __v;				\
51 	__asm__ __volatile__ ("csrr %0, " __ASM_STR(csr)	\
52 				: "=r" (__v) :			\
53 				: "memory");			\
54 	__v;							\
55 })
56 
57 #define csr_write(csr, val)					\
58 ({								\
59 	unsigned long __v = (unsigned long)(val);		\
60 	__asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0"	\
61 				: : "rK" (__v)			\
62 				: "memory");			\
63 })
64 
65 #define csr_read_set(csr, val)					\
66 ({								\
67 	unsigned long __v = (unsigned long)(val);		\
68 	__asm__ __volatile__ ("csrrs %0, " __ASM_STR(csr) ", %1"\
69 				: "=r" (__v) : "rK" (__v)	\
70 				: "memory");			\
71 	__v;							\
72 })
73 
74 #define csr_set(csr, val)					\
75 ({								\
76 	unsigned long __v = (unsigned long)(val);		\
77 	__asm__ __volatile__ ("csrs " __ASM_STR(csr) ", %0"	\
78 				: : "rK" (__v)			\
79 				: "memory");			\
80 })
81 
82 #define csr_read_clear(csr, val)				\
83 ({								\
84 	unsigned long __v = (unsigned long)(val);		\
85 	__asm__ __volatile__ ("csrrc %0, " __ASM_STR(csr) ", %1"\
86 				: "=r" (__v) : "rK" (__v)	\
87 				: "memory");			\
88 	__v;							\
89 })
90 
91 #define csr_clear(csr, val)					\
92 ({								\
93 	unsigned long __v = (unsigned long)(val);		\
94 	__asm__ __volatile__ ("csrc " __ASM_STR(csr) ", %0"	\
95 				: : "rK" (__v)			\
96 				: "memory");			\
97 })
98 
99 #endif /* !__ASSEMBLY__ */
100 #endif /* _ASMRISCV_CSR_H_ */
101