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 csr_write(CSR_HEDELEG, KVM_HEDELEG_DEFAULT);
26 csr_write(CSR_HIDELEG, KVM_HIDELEG_DEFAULT);
28 /* VS should access only the time counter directly. Everything else should trap */
29 csr_write(CSR_HCOUNTEREN, 0x02);
31 csr_write(CSR_HVIP, 0);
33 kvm_riscv_aia_enable();
38 void kvm_arch_hardware_disable(void)
40 kvm_riscv_aia_disable();
43 * After clearing the hideleg CSR, the host kernel will receive
44 * spurious interrupts if hvip CSR has pending interrupts and the
45 * corresponding enable bits in vsie CSR are asserted. To avoid it,
46 * hvip CSR and vsie CSR must be cleared before clearing hideleg CSR.
48 csr_write(CSR_VSIE, 0);
49 csr_write(CSR_HVIP, 0);
50 csr_write(CSR_HEDELEG, 0);
51 csr_write(CSR_HIDELEG, 0);
54 static int __init riscv_kvm_init(void)
59 if (!riscv_isa_extension_available(NULL, h)) {
60 kvm_info("hypervisor extension not available\n");
64 if (sbi_spec_is_0_1()) {
65 kvm_info("require SBI v0.2 or higher\n");
69 if (!sbi_probe_extension(SBI_EXT_RFENCE)) {
70 kvm_info("require SBI RFENCE extension\n");
74 kvm_riscv_gstage_mode_detect();
76 kvm_riscv_gstage_vmid_detect();
78 rc = kvm_riscv_aia_init();
79 if (rc && rc != -ENODEV)
82 kvm_info("hypervisor extension available\n");
84 switch (kvm_riscv_gstage_mode()) {
85 case HGATP_MODE_SV32X4:
88 case HGATP_MODE_SV39X4:
91 case HGATP_MODE_SV48X4:
94 case HGATP_MODE_SV57X4:
100 kvm_info("using %s G-stage page table format\n", str);
102 kvm_info("VMID %ld bits available\n", kvm_riscv_gstage_vmid_bits());
104 if (kvm_riscv_aia_available())
105 kvm_info("AIA available with %d guest external interrupts\n",
106 kvm_riscv_aia_nr_hgei);
108 rc = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
110 kvm_riscv_aia_exit();
116 module_init(riscv_kvm_init);
118 static void __exit riscv_kvm_exit(void)
120 kvm_riscv_aia_exit();
124 module_exit(riscv_kvm_exit);