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