xref: /linux/arch/riscv/include/asm/kvm_host.h (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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/hwcap.h>
17 #include <asm/kvm_aia.h>
18 #include <asm/ptrace.h>
19 #include <asm/kvm_vcpu_fp.h>
20 #include <asm/kvm_vcpu_insn.h>
21 #include <asm/kvm_vcpu_sbi.h>
22 #include <asm/kvm_vcpu_timer.h>
23 #include <asm/kvm_vcpu_pmu.h>
24 
25 #define KVM_MAX_VCPUS			1024
26 
27 #define KVM_HALT_POLL_NS_DEFAULT	500000
28 
29 #define KVM_VCPU_MAX_FEATURES		0
30 
31 #define KVM_IRQCHIP_NUM_PINS		1024
32 
33 #define KVM_REQ_SLEEP \
34 	KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
35 #define KVM_REQ_VCPU_RESET		KVM_ARCH_REQ(1)
36 #define KVM_REQ_UPDATE_HGATP		KVM_ARCH_REQ(2)
37 #define KVM_REQ_FENCE_I			\
38 	KVM_ARCH_REQ_FLAGS(3, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
39 #define KVM_REQ_HFENCE_GVMA_VMID_ALL	KVM_REQ_TLB_FLUSH
40 #define KVM_REQ_HFENCE_VVMA_ALL		\
41 	KVM_ARCH_REQ_FLAGS(4, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
42 #define KVM_REQ_HFENCE			\
43 	KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
44 #define KVM_REQ_STEAL_UPDATE		KVM_ARCH_REQ(6)
45 
46 #define KVM_HEDELEG_DEFAULT		(BIT(EXC_INST_MISALIGNED) | \
47 					 BIT(EXC_BREAKPOINT)      | \
48 					 BIT(EXC_SYSCALL)         | \
49 					 BIT(EXC_INST_PAGE_FAULT) | \
50 					 BIT(EXC_LOAD_PAGE_FAULT) | \
51 					 BIT(EXC_STORE_PAGE_FAULT))
52 
53 #define KVM_HIDELEG_DEFAULT		(BIT(IRQ_VS_SOFT)  | \
54 					 BIT(IRQ_VS_TIMER) | \
55 					 BIT(IRQ_VS_EXT))
56 
57 enum kvm_riscv_hfence_type {
58 	KVM_RISCV_HFENCE_UNKNOWN = 0,
59 	KVM_RISCV_HFENCE_GVMA_VMID_GPA,
60 	KVM_RISCV_HFENCE_VVMA_ASID_GVA,
61 	KVM_RISCV_HFENCE_VVMA_ASID_ALL,
62 	KVM_RISCV_HFENCE_VVMA_GVA,
63 };
64 
65 struct kvm_riscv_hfence {
66 	enum kvm_riscv_hfence_type type;
67 	unsigned long asid;
68 	unsigned long order;
69 	gpa_t addr;
70 	gpa_t size;
71 };
72 
73 #define KVM_RISCV_VCPU_MAX_HFENCE	64
74 
75 struct kvm_vm_stat {
76 	struct kvm_vm_stat_generic generic;
77 };
78 
79 struct kvm_vcpu_stat {
80 	struct kvm_vcpu_stat_generic generic;
81 	u64 ecall_exit_stat;
82 	u64 wfi_exit_stat;
83 	u64 mmio_exit_user;
84 	u64 mmio_exit_kernel;
85 	u64 csr_exit_user;
86 	u64 csr_exit_kernel;
87 	u64 signal_exits;
88 	u64 exits;
89 };
90 
91 struct kvm_arch_memory_slot {
92 };
93 
94 struct kvm_vmid {
95 	/*
96 	 * Writes to vmid_version and vmid happen with vmid_lock held
97 	 * whereas reads happen without any lock held.
98 	 */
99 	unsigned long vmid_version;
100 	unsigned long vmid;
101 };
102 
103 struct kvm_arch {
104 	/* G-stage vmid */
105 	struct kvm_vmid vmid;
106 
107 	/* G-stage page table */
108 	pgd_t *pgd;
109 	phys_addr_t pgd_phys;
110 
111 	/* Guest Timer */
112 	struct kvm_guest_timer timer;
113 
114 	/* AIA Guest/VM context */
115 	struct kvm_aia aia;
116 };
117 
118 struct kvm_cpu_trap {
119 	unsigned long sepc;
120 	unsigned long scause;
121 	unsigned long stval;
122 	unsigned long htval;
123 	unsigned long htinst;
124 };
125 
126 struct kvm_cpu_context {
127 	unsigned long zero;
128 	unsigned long ra;
129 	unsigned long sp;
130 	unsigned long gp;
131 	unsigned long tp;
132 	unsigned long t0;
133 	unsigned long t1;
134 	unsigned long t2;
135 	unsigned long s0;
136 	unsigned long s1;
137 	unsigned long a0;
138 	unsigned long a1;
139 	unsigned long a2;
140 	unsigned long a3;
141 	unsigned long a4;
142 	unsigned long a5;
143 	unsigned long a6;
144 	unsigned long a7;
145 	unsigned long s2;
146 	unsigned long s3;
147 	unsigned long s4;
148 	unsigned long s5;
149 	unsigned long s6;
150 	unsigned long s7;
151 	unsigned long s8;
152 	unsigned long s9;
153 	unsigned long s10;
154 	unsigned long s11;
155 	unsigned long t3;
156 	unsigned long t4;
157 	unsigned long t5;
158 	unsigned long t6;
159 	unsigned long sepc;
160 	unsigned long sstatus;
161 	unsigned long hstatus;
162 	union __riscv_fp_state fp;
163 	struct __riscv_v_ext_state vector;
164 };
165 
166 struct kvm_vcpu_csr {
167 	unsigned long vsstatus;
168 	unsigned long vsie;
169 	unsigned long vstvec;
170 	unsigned long vsscratch;
171 	unsigned long vsepc;
172 	unsigned long vscause;
173 	unsigned long vstval;
174 	unsigned long hvip;
175 	unsigned long vsatp;
176 	unsigned long scounteren;
177 	unsigned long senvcfg;
178 };
179 
180 struct kvm_vcpu_config {
181 	u64 henvcfg;
182 	u64 hstateen0;
183 	unsigned long hedeleg;
184 };
185 
186 struct kvm_vcpu_smstateen_csr {
187 	unsigned long sstateen0;
188 };
189 
190 struct kvm_vcpu_arch {
191 	/* VCPU ran at least once */
192 	bool ran_atleast_once;
193 
194 	/* Last Host CPU on which Guest VCPU exited */
195 	int last_exit_cpu;
196 
197 	/* ISA feature bits (similar to MISA) */
198 	DECLARE_BITMAP(isa, RISCV_ISA_EXT_MAX);
199 
200 	/* Vendor, Arch, and Implementation details */
201 	unsigned long mvendorid;
202 	unsigned long marchid;
203 	unsigned long mimpid;
204 
205 	/* SSCRATCH, STVEC, and SCOUNTEREN of Host */
206 	unsigned long host_sscratch;
207 	unsigned long host_stvec;
208 	unsigned long host_scounteren;
209 	unsigned long host_senvcfg;
210 	unsigned long host_sstateen0;
211 
212 	/* CPU context of Host */
213 	struct kvm_cpu_context host_context;
214 
215 	/* CPU context of Guest VCPU */
216 	struct kvm_cpu_context guest_context;
217 
218 	/* CPU CSR context of Guest VCPU */
219 	struct kvm_vcpu_csr guest_csr;
220 
221 	/* CPU Smstateen CSR context of Guest VCPU */
222 	struct kvm_vcpu_smstateen_csr smstateen_csr;
223 
224 	/* CPU context upon Guest VCPU reset */
225 	struct kvm_cpu_context guest_reset_context;
226 	spinlock_t reset_cntx_lock;
227 
228 	/* CPU CSR context upon Guest VCPU reset */
229 	struct kvm_vcpu_csr guest_reset_csr;
230 
231 	/*
232 	 * VCPU interrupts
233 	 *
234 	 * We have a lockless approach for tracking pending VCPU interrupts
235 	 * implemented using atomic bitops. The irqs_pending bitmap represent
236 	 * pending interrupts whereas irqs_pending_mask represent bits changed
237 	 * in irqs_pending. Our approach is modeled around multiple producer
238 	 * and single consumer problem where the consumer is the VCPU itself.
239 	 */
240 #define KVM_RISCV_VCPU_NR_IRQS	64
241 	DECLARE_BITMAP(irqs_pending, KVM_RISCV_VCPU_NR_IRQS);
242 	DECLARE_BITMAP(irqs_pending_mask, KVM_RISCV_VCPU_NR_IRQS);
243 
244 	/* VCPU Timer */
245 	struct kvm_vcpu_timer timer;
246 
247 	/* HFENCE request queue */
248 	spinlock_t hfence_lock;
249 	unsigned long hfence_head;
250 	unsigned long hfence_tail;
251 	struct kvm_riscv_hfence hfence_queue[KVM_RISCV_VCPU_MAX_HFENCE];
252 
253 	/* MMIO instruction details */
254 	struct kvm_mmio_decode mmio_decode;
255 
256 	/* CSR instruction details */
257 	struct kvm_csr_decode csr_decode;
258 
259 	/* SBI context */
260 	struct kvm_vcpu_sbi_context sbi_context;
261 
262 	/* AIA VCPU context */
263 	struct kvm_vcpu_aia aia_context;
264 
265 	/* Cache pages needed to program page tables with spinlock held */
266 	struct kvm_mmu_memory_cache mmu_page_cache;
267 
268 	/* VCPU power state */
269 	struct kvm_mp_state mp_state;
270 	spinlock_t mp_state_lock;
271 
272 	/* Don't run the VCPU (blocked) */
273 	bool pause;
274 
275 	/* Performance monitoring context */
276 	struct kvm_pmu pmu_context;
277 
278 	/* 'static' configurations which are set only once */
279 	struct kvm_vcpu_config cfg;
280 
281 	/* SBI steal-time accounting */
282 	struct {
283 		gpa_t shmem;
284 		u64 last_steal;
285 	} sta;
286 };
287 
288 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
289 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
290 
291 #define KVM_RISCV_GSTAGE_TLB_MIN_ORDER		12
292 
293 void kvm_riscv_local_hfence_gvma_vmid_gpa(unsigned long vmid,
294 					  gpa_t gpa, gpa_t gpsz,
295 					  unsigned long order);
296 void kvm_riscv_local_hfence_gvma_vmid_all(unsigned long vmid);
297 void kvm_riscv_local_hfence_gvma_gpa(gpa_t gpa, gpa_t gpsz,
298 				     unsigned long order);
299 void kvm_riscv_local_hfence_gvma_all(void);
300 void kvm_riscv_local_hfence_vvma_asid_gva(unsigned long vmid,
301 					  unsigned long asid,
302 					  unsigned long gva,
303 					  unsigned long gvsz,
304 					  unsigned long order);
305 void kvm_riscv_local_hfence_vvma_asid_all(unsigned long vmid,
306 					  unsigned long asid);
307 void kvm_riscv_local_hfence_vvma_gva(unsigned long vmid,
308 				     unsigned long gva, unsigned long gvsz,
309 				     unsigned long order);
310 void kvm_riscv_local_hfence_vvma_all(unsigned long vmid);
311 
312 void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu);
313 
314 void kvm_riscv_fence_i_process(struct kvm_vcpu *vcpu);
315 void kvm_riscv_hfence_gvma_vmid_all_process(struct kvm_vcpu *vcpu);
316 void kvm_riscv_hfence_vvma_all_process(struct kvm_vcpu *vcpu);
317 void kvm_riscv_hfence_process(struct kvm_vcpu *vcpu);
318 
319 void kvm_riscv_fence_i(struct kvm *kvm,
320 		       unsigned long hbase, unsigned long hmask);
321 void kvm_riscv_hfence_gvma_vmid_gpa(struct kvm *kvm,
322 				    unsigned long hbase, unsigned long hmask,
323 				    gpa_t gpa, gpa_t gpsz,
324 				    unsigned long order);
325 void kvm_riscv_hfence_gvma_vmid_all(struct kvm *kvm,
326 				    unsigned long hbase, unsigned long hmask);
327 void kvm_riscv_hfence_vvma_asid_gva(struct kvm *kvm,
328 				    unsigned long hbase, unsigned long hmask,
329 				    unsigned long gva, unsigned long gvsz,
330 				    unsigned long order, unsigned long asid);
331 void kvm_riscv_hfence_vvma_asid_all(struct kvm *kvm,
332 				    unsigned long hbase, unsigned long hmask,
333 				    unsigned long asid);
334 void kvm_riscv_hfence_vvma_gva(struct kvm *kvm,
335 			       unsigned long hbase, unsigned long hmask,
336 			       unsigned long gva, unsigned long gvsz,
337 			       unsigned long order);
338 void kvm_riscv_hfence_vvma_all(struct kvm *kvm,
339 			       unsigned long hbase, unsigned long hmask);
340 
341 int kvm_riscv_gstage_ioremap(struct kvm *kvm, gpa_t gpa,
342 			     phys_addr_t hpa, unsigned long size,
343 			     bool writable, bool in_atomic);
344 void kvm_riscv_gstage_iounmap(struct kvm *kvm, gpa_t gpa,
345 			      unsigned long size);
346 int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu,
347 			 struct kvm_memory_slot *memslot,
348 			 gpa_t gpa, unsigned long hva, bool is_write);
349 int kvm_riscv_gstage_alloc_pgd(struct kvm *kvm);
350 void kvm_riscv_gstage_free_pgd(struct kvm *kvm);
351 void kvm_riscv_gstage_update_hgatp(struct kvm_vcpu *vcpu);
352 void __init kvm_riscv_gstage_mode_detect(void);
353 unsigned long __init kvm_riscv_gstage_mode(void);
354 int kvm_riscv_gstage_gpa_bits(void);
355 
356 void __init kvm_riscv_gstage_vmid_detect(void);
357 unsigned long kvm_riscv_gstage_vmid_bits(void);
358 int kvm_riscv_gstage_vmid_init(struct kvm *kvm);
359 bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid);
360 void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu);
361 
362 int kvm_riscv_setup_default_irq_routing(struct kvm *kvm, u32 lines);
363 
364 void __kvm_riscv_unpriv_trap(void);
365 
366 unsigned long kvm_riscv_vcpu_unpriv_read(struct kvm_vcpu *vcpu,
367 					 bool read_insn,
368 					 unsigned long guest_addr,
369 					 struct kvm_cpu_trap *trap);
370 void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
371 				  struct kvm_cpu_trap *trap);
372 int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
373 			struct kvm_cpu_trap *trap);
374 
375 void __kvm_riscv_switch_to(struct kvm_vcpu_arch *vcpu_arch);
376 
377 void kvm_riscv_vcpu_setup_isa(struct kvm_vcpu *vcpu);
378 unsigned long kvm_riscv_vcpu_num_regs(struct kvm_vcpu *vcpu);
379 int kvm_riscv_vcpu_copy_reg_indices(struct kvm_vcpu *vcpu,
380 				    u64 __user *uindices);
381 int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu,
382 			   const struct kvm_one_reg *reg);
383 int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu,
384 			   const struct kvm_one_reg *reg);
385 
386 int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq);
387 int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq);
388 void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu);
389 void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu);
390 bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask);
391 void __kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu);
392 void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu);
393 void __kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu);
394 void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu);
395 bool kvm_riscv_vcpu_stopped(struct kvm_vcpu *vcpu);
396 
397 void kvm_riscv_vcpu_sbi_sta_reset(struct kvm_vcpu *vcpu);
398 void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu);
399 
400 #endif /* __RISCV_KVM_HOST_H__ */
401