1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
6 * Anup Patel <anup.patel@wdc.com>
9 #include <linux/errno.h>
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/kvm_host.h>
14 #include <asm/cpufeature.h>
17 long kvm_arch_dev_ioctl(struct file *filp,
18 unsigned int ioctl, unsigned long arg)
23 int kvm_arch_hardware_enable(void)
25 unsigned long hideleg, hedeleg;
28 hedeleg |= (1UL << EXC_INST_MISALIGNED);
29 hedeleg |= (1UL << EXC_BREAKPOINT);
30 hedeleg |= (1UL << EXC_SYSCALL);
31 hedeleg |= (1UL << EXC_INST_PAGE_FAULT);
32 hedeleg |= (1UL << EXC_LOAD_PAGE_FAULT);
33 hedeleg |= (1UL << EXC_STORE_PAGE_FAULT);
34 csr_write(CSR_HEDELEG, hedeleg);
37 hideleg |= (1UL << IRQ_VS_SOFT);
38 hideleg |= (1UL << IRQ_VS_TIMER);
39 hideleg |= (1UL << IRQ_VS_EXT);
40 csr_write(CSR_HIDELEG, hideleg);
42 /* VS should access only the time counter directly. Everything else should trap */
43 csr_write(CSR_HCOUNTEREN, 0x02);
45 csr_write(CSR_HVIP, 0);
47 kvm_riscv_aia_enable();
52 void kvm_arch_hardware_disable(void)
54 kvm_riscv_aia_disable();
57 * After clearing the hideleg CSR, the host kernel will receive
58 * spurious interrupts if hvip CSR has pending interrupts and the
59 * corresponding enable bits in vsie CSR are asserted. To avoid it,
60 * hvip CSR and vsie CSR must be cleared before clearing hideleg CSR.
62 csr_write(CSR_VSIE, 0);
63 csr_write(CSR_HVIP, 0);
64 csr_write(CSR_HEDELEG, 0);
65 csr_write(CSR_HIDELEG, 0);
68 static int __init riscv_kvm_init(void)
73 if (!riscv_isa_extension_available(NULL, h)) {
74 kvm_info("hypervisor extension not available\n");
78 if (sbi_spec_is_0_1()) {
79 kvm_info("require SBI v0.2 or higher\n");
83 if (!sbi_probe_extension(SBI_EXT_RFENCE)) {
84 kvm_info("require SBI RFENCE extension\n");
88 kvm_riscv_gstage_mode_detect();
90 kvm_riscv_gstage_vmid_detect();
92 rc = kvm_riscv_aia_init();
93 if (rc && rc != -ENODEV)
96 kvm_info("hypervisor extension available\n");
98 switch (kvm_riscv_gstage_mode()) {
99 case HGATP_MODE_SV32X4:
102 case HGATP_MODE_SV39X4:
105 case HGATP_MODE_SV48X4:
108 case HGATP_MODE_SV57X4:
114 kvm_info("using %s G-stage page table format\n", str);
116 kvm_info("VMID %ld bits available\n", kvm_riscv_gstage_vmid_bits());
118 if (kvm_riscv_aia_available())
119 kvm_info("AIA available with %d guest external interrupts\n",
120 kvm_riscv_aia_nr_hgei);
122 rc = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
124 kvm_riscv_aia_exit();
130 module_init(riscv_kvm_init);
132 static void __exit riscv_kvm_exit(void)
134 kvm_riscv_aia_exit();
138 module_exit(riscv_kvm_exit);