xref: /openbmc/linux/arch/riscv/include/uapi/asm/kvm.h (revision 66c98360)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
4  *
5  * Authors:
6  *     Anup Patel <anup.patel@wdc.com>
7  */
8 
9 #ifndef __LINUX_KVM_RISCV_H
10 #define __LINUX_KVM_RISCV_H
11 
12 #ifndef __ASSEMBLY__
13 
14 #include <linux/types.h>
15 #include <asm/bitsperlong.h>
16 #include <asm/ptrace.h>
17 
18 #define __KVM_HAVE_READONLY_MEM
19 
20 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
21 
22 #define KVM_INTERRUPT_SET	-1U
23 #define KVM_INTERRUPT_UNSET	-2U
24 
25 /* for KVM_GET_REGS and KVM_SET_REGS */
26 struct kvm_regs {
27 };
28 
29 /* for KVM_GET_FPU and KVM_SET_FPU */
30 struct kvm_fpu {
31 };
32 
33 /* KVM Debug exit structure */
34 struct kvm_debug_exit_arch {
35 };
36 
37 /* for KVM_SET_GUEST_DEBUG */
38 struct kvm_guest_debug_arch {
39 };
40 
41 /* definition of registers in kvm_run */
42 struct kvm_sync_regs {
43 };
44 
45 /* for KVM_GET_SREGS and KVM_SET_SREGS */
46 struct kvm_sregs {
47 };
48 
49 /* CONFIG registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
50 struct kvm_riscv_config {
51 	unsigned long isa;
52 	unsigned long zicbom_block_size;
53 	unsigned long mvendorid;
54 	unsigned long marchid;
55 	unsigned long mimpid;
56 	unsigned long zicboz_block_size;
57 };
58 
59 /* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
60 struct kvm_riscv_core {
61 	struct user_regs_struct regs;
62 	unsigned long mode;
63 };
64 
65 /* Possible privilege modes for kvm_riscv_core */
66 #define KVM_RISCV_MODE_S	1
67 #define KVM_RISCV_MODE_U	0
68 
69 /* General CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
70 struct kvm_riscv_csr {
71 	unsigned long sstatus;
72 	unsigned long sie;
73 	unsigned long stvec;
74 	unsigned long sscratch;
75 	unsigned long sepc;
76 	unsigned long scause;
77 	unsigned long stval;
78 	unsigned long sip;
79 	unsigned long satp;
80 	unsigned long scounteren;
81 };
82 
83 /* AIA CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
84 struct kvm_riscv_aia_csr {
85 	unsigned long siselect;
86 	unsigned long iprio1;
87 	unsigned long iprio2;
88 	unsigned long sieh;
89 	unsigned long siph;
90 	unsigned long iprio1h;
91 	unsigned long iprio2h;
92 };
93 
94 /* TIMER registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
95 struct kvm_riscv_timer {
96 	__u64 frequency;
97 	__u64 time;
98 	__u64 compare;
99 	__u64 state;
100 };
101 
102 /*
103  * ISA extension IDs specific to KVM. This is not the same as the host ISA
104  * extension IDs as that is internal to the host and should not be exposed
105  * to the guest. This should always be contiguous to keep the mapping simple
106  * in KVM implementation.
107  */
108 enum KVM_RISCV_ISA_EXT_ID {
109 	KVM_RISCV_ISA_EXT_A = 0,
110 	KVM_RISCV_ISA_EXT_C,
111 	KVM_RISCV_ISA_EXT_D,
112 	KVM_RISCV_ISA_EXT_F,
113 	KVM_RISCV_ISA_EXT_H,
114 	KVM_RISCV_ISA_EXT_I,
115 	KVM_RISCV_ISA_EXT_M,
116 	KVM_RISCV_ISA_EXT_SVPBMT,
117 	KVM_RISCV_ISA_EXT_SSTC,
118 	KVM_RISCV_ISA_EXT_SVINVAL,
119 	KVM_RISCV_ISA_EXT_ZIHINTPAUSE,
120 	KVM_RISCV_ISA_EXT_ZICBOM,
121 	KVM_RISCV_ISA_EXT_ZICBOZ,
122 	KVM_RISCV_ISA_EXT_ZBB,
123 	KVM_RISCV_ISA_EXT_SSAIA,
124 	KVM_RISCV_ISA_EXT_MAX,
125 };
126 
127 /*
128  * SBI extension IDs specific to KVM. This is not the same as the SBI
129  * extension IDs defined by the RISC-V SBI specification.
130  */
131 enum KVM_RISCV_SBI_EXT_ID {
132 	KVM_RISCV_SBI_EXT_V01 = 0,
133 	KVM_RISCV_SBI_EXT_TIME,
134 	KVM_RISCV_SBI_EXT_IPI,
135 	KVM_RISCV_SBI_EXT_RFENCE,
136 	KVM_RISCV_SBI_EXT_SRST,
137 	KVM_RISCV_SBI_EXT_HSM,
138 	KVM_RISCV_SBI_EXT_PMU,
139 	KVM_RISCV_SBI_EXT_EXPERIMENTAL,
140 	KVM_RISCV_SBI_EXT_VENDOR,
141 	KVM_RISCV_SBI_EXT_MAX,
142 };
143 
144 /* Possible states for kvm_riscv_timer */
145 #define KVM_RISCV_TIMER_STATE_OFF	0
146 #define KVM_RISCV_TIMER_STATE_ON	1
147 
148 #define KVM_REG_SIZE(id)		\
149 	(1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
150 
151 /* If you need to interpret the index values, here is the key: */
152 #define KVM_REG_RISCV_TYPE_MASK		0x00000000FF000000
153 #define KVM_REG_RISCV_TYPE_SHIFT	24
154 #define KVM_REG_RISCV_SUBTYPE_MASK	0x0000000000FF0000
155 #define KVM_REG_RISCV_SUBTYPE_SHIFT	16
156 
157 /* Config registers are mapped as type 1 */
158 #define KVM_REG_RISCV_CONFIG		(0x01 << KVM_REG_RISCV_TYPE_SHIFT)
159 #define KVM_REG_RISCV_CONFIG_REG(name)	\
160 	(offsetof(struct kvm_riscv_config, name) / sizeof(unsigned long))
161 
162 /* Core registers are mapped as type 2 */
163 #define KVM_REG_RISCV_CORE		(0x02 << KVM_REG_RISCV_TYPE_SHIFT)
164 #define KVM_REG_RISCV_CORE_REG(name)	\
165 		(offsetof(struct kvm_riscv_core, name) / sizeof(unsigned long))
166 
167 /* Control and status registers are mapped as type 3 */
168 #define KVM_REG_RISCV_CSR		(0x03 << KVM_REG_RISCV_TYPE_SHIFT)
169 #define KVM_REG_RISCV_CSR_GENERAL	(0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT)
170 #define KVM_REG_RISCV_CSR_AIA		(0x1 << KVM_REG_RISCV_SUBTYPE_SHIFT)
171 #define KVM_REG_RISCV_CSR_REG(name)	\
172 		(offsetof(struct kvm_riscv_csr, name) / sizeof(unsigned long))
173 #define KVM_REG_RISCV_CSR_AIA_REG(name)	\
174 	(offsetof(struct kvm_riscv_aia_csr, name) / sizeof(unsigned long))
175 
176 /* Timer registers are mapped as type 4 */
177 #define KVM_REG_RISCV_TIMER		(0x04 << KVM_REG_RISCV_TYPE_SHIFT)
178 #define KVM_REG_RISCV_TIMER_REG(name)	\
179 		(offsetof(struct kvm_riscv_timer, name) / sizeof(__u64))
180 
181 /* F extension registers are mapped as type 5 */
182 #define KVM_REG_RISCV_FP_F		(0x05 << KVM_REG_RISCV_TYPE_SHIFT)
183 #define KVM_REG_RISCV_FP_F_REG(name)	\
184 		(offsetof(struct __riscv_f_ext_state, name) / sizeof(__u32))
185 
186 /* D extension registers are mapped as type 6 */
187 #define KVM_REG_RISCV_FP_D		(0x06 << KVM_REG_RISCV_TYPE_SHIFT)
188 #define KVM_REG_RISCV_FP_D_REG(name)	\
189 		(offsetof(struct __riscv_d_ext_state, name) / sizeof(__u64))
190 
191 /* ISA Extension registers are mapped as type 7 */
192 #define KVM_REG_RISCV_ISA_EXT		(0x07 << KVM_REG_RISCV_TYPE_SHIFT)
193 
194 /* SBI extension registers are mapped as type 8 */
195 #define KVM_REG_RISCV_SBI_EXT		(0x08 << KVM_REG_RISCV_TYPE_SHIFT)
196 #define KVM_REG_RISCV_SBI_SINGLE	(0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT)
197 #define KVM_REG_RISCV_SBI_MULTI_EN	(0x1 << KVM_REG_RISCV_SUBTYPE_SHIFT)
198 #define KVM_REG_RISCV_SBI_MULTI_DIS	(0x2 << KVM_REG_RISCV_SUBTYPE_SHIFT)
199 #define KVM_REG_RISCV_SBI_MULTI_REG(__ext_id)	\
200 		((__ext_id) / __BITS_PER_LONG)
201 #define KVM_REG_RISCV_SBI_MULTI_MASK(__ext_id)	\
202 		(1UL << ((__ext_id) % __BITS_PER_LONG))
203 #define KVM_REG_RISCV_SBI_MULTI_REG_LAST	\
204 		KVM_REG_RISCV_SBI_MULTI_REG(KVM_RISCV_SBI_EXT_MAX - 1)
205 
206 #endif
207 
208 #endif /* __LINUX_KVM_RISCV_H */
209