xref: /openbmc/linux/arch/riscv/include/asm/kvm_host.h (revision 1dd0dd0b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
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 __RISCV_KVM_HOST_H__
10 #define __RISCV_KVM_HOST_H__
11 
12 #include <linux/types.h>
13 #include <linux/kvm.h>
14 #include <linux/kvm_types.h>
15 #include <linux/spinlock.h>
16 #include <asm/csr.h>
17 #include <asm/hwcap.h>
18 #include <asm/kvm_vcpu_fp.h>
19 #include <asm/kvm_vcpu_insn.h>
20 #include <asm/kvm_vcpu_timer.h>
21 
22 #define KVM_MAX_VCPUS			1024
23 
24 #define KVM_HALT_POLL_NS_DEFAULT	500000
25 
26 #define KVM_VCPU_MAX_FEATURES		0
27 
28 #define KVM_REQ_SLEEP \
29 	KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
30 #define KVM_REQ_VCPU_RESET		KVM_ARCH_REQ(1)
31 #define KVM_REQ_UPDATE_HGATP		KVM_ARCH_REQ(2)
32 #define KVM_REQ_FENCE_I			\
33 	KVM_ARCH_REQ_FLAGS(3, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
34 #define KVM_REQ_HFENCE_GVMA_VMID_ALL	KVM_REQ_TLB_FLUSH
35 #define KVM_REQ_HFENCE_VVMA_ALL		\
36 	KVM_ARCH_REQ_FLAGS(4, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
37 #define KVM_REQ_HFENCE			\
38 	KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
39 
40 enum kvm_riscv_hfence_type {
41 	KVM_RISCV_HFENCE_UNKNOWN = 0,
42 	KVM_RISCV_HFENCE_GVMA_VMID_GPA,
43 	KVM_RISCV_HFENCE_VVMA_ASID_GVA,
44 	KVM_RISCV_HFENCE_VVMA_ASID_ALL,
45 	KVM_RISCV_HFENCE_VVMA_GVA,
46 };
47 
48 struct kvm_riscv_hfence {
49 	enum kvm_riscv_hfence_type type;
50 	unsigned long asid;
51 	unsigned long order;
52 	gpa_t addr;
53 	gpa_t size;
54 };
55 
56 #define KVM_RISCV_VCPU_MAX_HFENCE	64
57 
58 struct kvm_vm_stat {
59 	struct kvm_vm_stat_generic generic;
60 };
61 
62 struct kvm_vcpu_stat {
63 	struct kvm_vcpu_stat_generic generic;
64 	u64 ecall_exit_stat;
65 	u64 wfi_exit_stat;
66 	u64 mmio_exit_user;
67 	u64 mmio_exit_kernel;
68 	u64 csr_exit_user;
69 	u64 csr_exit_kernel;
70 	u64 exits;
71 };
72 
73 struct kvm_arch_memory_slot {
74 };
75 
76 struct kvm_vmid {
77 	/*
78 	 * Writes to vmid_version and vmid happen with vmid_lock held
79 	 * whereas reads happen without any lock held.
80 	 */
81 	unsigned long vmid_version;
82 	unsigned long vmid;
83 };
84 
85 struct kvm_arch {
86 	/* G-stage vmid */
87 	struct kvm_vmid vmid;
88 
89 	/* G-stage page table */
90 	pgd_t *pgd;
91 	phys_addr_t pgd_phys;
92 
93 	/* Guest Timer */
94 	struct kvm_guest_timer timer;
95 };
96 
97 struct kvm_sbi_context {
98 	int return_handled;
99 };
100 
101 struct kvm_cpu_trap {
102 	unsigned long sepc;
103 	unsigned long scause;
104 	unsigned long stval;
105 	unsigned long htval;
106 	unsigned long htinst;
107 };
108 
109 struct kvm_cpu_context {
110 	unsigned long zero;
111 	unsigned long ra;
112 	unsigned long sp;
113 	unsigned long gp;
114 	unsigned long tp;
115 	unsigned long t0;
116 	unsigned long t1;
117 	unsigned long t2;
118 	unsigned long s0;
119 	unsigned long s1;
120 	unsigned long a0;
121 	unsigned long a1;
122 	unsigned long a2;
123 	unsigned long a3;
124 	unsigned long a4;
125 	unsigned long a5;
126 	unsigned long a6;
127 	unsigned long a7;
128 	unsigned long s2;
129 	unsigned long s3;
130 	unsigned long s4;
131 	unsigned long s5;
132 	unsigned long s6;
133 	unsigned long s7;
134 	unsigned long s8;
135 	unsigned long s9;
136 	unsigned long s10;
137 	unsigned long s11;
138 	unsigned long t3;
139 	unsigned long t4;
140 	unsigned long t5;
141 	unsigned long t6;
142 	unsigned long sepc;
143 	unsigned long sstatus;
144 	unsigned long hstatus;
145 	union __riscv_fp_state fp;
146 };
147 
148 struct kvm_vcpu_csr {
149 	unsigned long vsstatus;
150 	unsigned long vsie;
151 	unsigned long vstvec;
152 	unsigned long vsscratch;
153 	unsigned long vsepc;
154 	unsigned long vscause;
155 	unsigned long vstval;
156 	unsigned long hvip;
157 	unsigned long vsatp;
158 	unsigned long scounteren;
159 };
160 
161 struct kvm_vcpu_arch {
162 	/* VCPU ran at least once */
163 	bool ran_atleast_once;
164 
165 	/* Last Host CPU on which Guest VCPU exited */
166 	int last_exit_cpu;
167 
168 	/* ISA feature bits (similar to MISA) */
169 	DECLARE_BITMAP(isa, RISCV_ISA_EXT_MAX);
170 
171 	/* SSCRATCH, STVEC, and SCOUNTEREN of Host */
172 	unsigned long host_sscratch;
173 	unsigned long host_stvec;
174 	unsigned long host_scounteren;
175 
176 	/* CPU context of Host */
177 	struct kvm_cpu_context host_context;
178 
179 	/* CPU context of Guest VCPU */
180 	struct kvm_cpu_context guest_context;
181 
182 	/* CPU CSR context of Guest VCPU */
183 	struct kvm_vcpu_csr guest_csr;
184 
185 	/* CPU context upon Guest VCPU reset */
186 	struct kvm_cpu_context guest_reset_context;
187 
188 	/* CPU CSR context upon Guest VCPU reset */
189 	struct kvm_vcpu_csr guest_reset_csr;
190 
191 	/*
192 	 * VCPU interrupts
193 	 *
194 	 * We have a lockless approach for tracking pending VCPU interrupts
195 	 * implemented using atomic bitops. The irqs_pending bitmap represent
196 	 * pending interrupts whereas irqs_pending_mask represent bits changed
197 	 * in irqs_pending. Our approach is modeled around multiple producer
198 	 * and single consumer problem where the consumer is the VCPU itself.
199 	 */
200 	unsigned long irqs_pending;
201 	unsigned long irqs_pending_mask;
202 
203 	/* VCPU Timer */
204 	struct kvm_vcpu_timer timer;
205 
206 	/* HFENCE request queue */
207 	spinlock_t hfence_lock;
208 	unsigned long hfence_head;
209 	unsigned long hfence_tail;
210 	struct kvm_riscv_hfence hfence_queue[KVM_RISCV_VCPU_MAX_HFENCE];
211 
212 	/* MMIO instruction details */
213 	struct kvm_mmio_decode mmio_decode;
214 
215 	/* CSR instruction details */
216 	struct kvm_csr_decode csr_decode;
217 
218 	/* SBI context */
219 	struct kvm_sbi_context sbi_context;
220 
221 	/* Cache pages needed to program page tables with spinlock held */
222 	struct kvm_mmu_memory_cache mmu_page_cache;
223 
224 	/* VCPU power-off state */
225 	bool power_off;
226 
227 	/* Don't run the VCPU (blocked) */
228 	bool pause;
229 };
230 
231 static inline void kvm_arch_hardware_unsetup(void) {}
232 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
233 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
234 
235 #define KVM_ARCH_WANT_MMU_NOTIFIER
236 
237 #define KVM_RISCV_GSTAGE_TLB_MIN_ORDER		12
238 
239 void kvm_riscv_local_hfence_gvma_vmid_gpa(unsigned long vmid,
240 					  gpa_t gpa, gpa_t gpsz,
241 					  unsigned long order);
242 void kvm_riscv_local_hfence_gvma_vmid_all(unsigned long vmid);
243 void kvm_riscv_local_hfence_gvma_gpa(gpa_t gpa, gpa_t gpsz,
244 				     unsigned long order);
245 void kvm_riscv_local_hfence_gvma_all(void);
246 void kvm_riscv_local_hfence_vvma_asid_gva(unsigned long vmid,
247 					  unsigned long asid,
248 					  unsigned long gva,
249 					  unsigned long gvsz,
250 					  unsigned long order);
251 void kvm_riscv_local_hfence_vvma_asid_all(unsigned long vmid,
252 					  unsigned long asid);
253 void kvm_riscv_local_hfence_vvma_gva(unsigned long vmid,
254 				     unsigned long gva, unsigned long gvsz,
255 				     unsigned long order);
256 void kvm_riscv_local_hfence_vvma_all(unsigned long vmid);
257 
258 void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu);
259 
260 void kvm_riscv_fence_i_process(struct kvm_vcpu *vcpu);
261 void kvm_riscv_hfence_gvma_vmid_all_process(struct kvm_vcpu *vcpu);
262 void kvm_riscv_hfence_vvma_all_process(struct kvm_vcpu *vcpu);
263 void kvm_riscv_hfence_process(struct kvm_vcpu *vcpu);
264 
265 void kvm_riscv_fence_i(struct kvm *kvm,
266 		       unsigned long hbase, unsigned long hmask);
267 void kvm_riscv_hfence_gvma_vmid_gpa(struct kvm *kvm,
268 				    unsigned long hbase, unsigned long hmask,
269 				    gpa_t gpa, gpa_t gpsz,
270 				    unsigned long order);
271 void kvm_riscv_hfence_gvma_vmid_all(struct kvm *kvm,
272 				    unsigned long hbase, unsigned long hmask);
273 void kvm_riscv_hfence_vvma_asid_gva(struct kvm *kvm,
274 				    unsigned long hbase, unsigned long hmask,
275 				    unsigned long gva, unsigned long gvsz,
276 				    unsigned long order, unsigned long asid);
277 void kvm_riscv_hfence_vvma_asid_all(struct kvm *kvm,
278 				    unsigned long hbase, unsigned long hmask,
279 				    unsigned long asid);
280 void kvm_riscv_hfence_vvma_gva(struct kvm *kvm,
281 			       unsigned long hbase, unsigned long hmask,
282 			       unsigned long gva, unsigned long gvsz,
283 			       unsigned long order);
284 void kvm_riscv_hfence_vvma_all(struct kvm *kvm,
285 			       unsigned long hbase, unsigned long hmask);
286 
287 int kvm_riscv_gstage_ioremap(struct kvm *kvm, gpa_t gpa,
288 			     phys_addr_t hpa, unsigned long size,
289 			     bool writable, bool in_atomic);
290 void kvm_riscv_gstage_iounmap(struct kvm *kvm, gpa_t gpa,
291 			      unsigned long size);
292 int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu,
293 			 struct kvm_memory_slot *memslot,
294 			 gpa_t gpa, unsigned long hva, bool is_write);
295 int kvm_riscv_gstage_alloc_pgd(struct kvm *kvm);
296 void kvm_riscv_gstage_free_pgd(struct kvm *kvm);
297 void kvm_riscv_gstage_update_hgatp(struct kvm_vcpu *vcpu);
298 void kvm_riscv_gstage_mode_detect(void);
299 unsigned long kvm_riscv_gstage_mode(void);
300 int kvm_riscv_gstage_gpa_bits(void);
301 
302 void kvm_riscv_gstage_vmid_detect(void);
303 unsigned long kvm_riscv_gstage_vmid_bits(void);
304 int kvm_riscv_gstage_vmid_init(struct kvm *kvm);
305 bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid);
306 void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu);
307 
308 void __kvm_riscv_unpriv_trap(void);
309 
310 unsigned long kvm_riscv_vcpu_unpriv_read(struct kvm_vcpu *vcpu,
311 					 bool read_insn,
312 					 unsigned long guest_addr,
313 					 struct kvm_cpu_trap *trap);
314 void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
315 				  struct kvm_cpu_trap *trap);
316 int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
317 			struct kvm_cpu_trap *trap);
318 
319 void __kvm_riscv_switch_to(struct kvm_vcpu_arch *vcpu_arch);
320 
321 int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq);
322 int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq);
323 void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu);
324 void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu);
325 bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, unsigned long mask);
326 void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu);
327 void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu);
328 
329 int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run);
330 int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
331 
332 #endif /* __RISCV_KVM_HOST_H__ */
333