1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2019 Western Digital Corporation or its affiliates. 4 * 5 * Authors: 6 * Anup Patel <anup.patel@wdc.com> 7 */ 8 9 #include <linux/errno.h> 10 #include <linux/err.h> 11 #include <linux/module.h> 12 #include <linux/kvm_host.h> 13 #include <asm/csr.h> 14 #include <asm/hwcap.h> 15 #include <asm/sbi.h> 16 17 long kvm_arch_dev_ioctl(struct file *filp, 18 unsigned int ioctl, unsigned long arg) 19 { 20 return -EINVAL; 21 } 22 23 int kvm_arch_check_processor_compat(void *opaque) 24 { 25 return 0; 26 } 27 28 int kvm_arch_hardware_enable(void) 29 { 30 unsigned long hideleg, hedeleg; 31 32 hedeleg = 0; 33 hedeleg |= (1UL << EXC_INST_MISALIGNED); 34 hedeleg |= (1UL << EXC_BREAKPOINT); 35 hedeleg |= (1UL << EXC_SYSCALL); 36 hedeleg |= (1UL << EXC_INST_PAGE_FAULT); 37 hedeleg |= (1UL << EXC_LOAD_PAGE_FAULT); 38 hedeleg |= (1UL << EXC_STORE_PAGE_FAULT); 39 csr_write(CSR_HEDELEG, hedeleg); 40 41 hideleg = 0; 42 hideleg |= (1UL << IRQ_VS_SOFT); 43 hideleg |= (1UL << IRQ_VS_TIMER); 44 hideleg |= (1UL << IRQ_VS_EXT); 45 csr_write(CSR_HIDELEG, hideleg); 46 47 csr_write(CSR_HCOUNTEREN, -1UL); 48 49 csr_write(CSR_HVIP, 0); 50 51 return 0; 52 } 53 54 void kvm_arch_hardware_disable(void) 55 { 56 /* 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. 61 */ 62 csr_write(CSR_VSIE, 0); 63 csr_write(CSR_HVIP, 0); 64 csr_write(CSR_HEDELEG, 0); 65 csr_write(CSR_HIDELEG, 0); 66 } 67 68 static int __init riscv_kvm_init(void) 69 { 70 const char *str; 71 72 if (!riscv_isa_extension_available(NULL, h)) { 73 kvm_info("hypervisor extension not available\n"); 74 return -ENODEV; 75 } 76 77 if (sbi_spec_is_0_1()) { 78 kvm_info("require SBI v0.2 or higher\n"); 79 return -ENODEV; 80 } 81 82 if (sbi_probe_extension(SBI_EXT_RFENCE) <= 0) { 83 kvm_info("require SBI RFENCE extension\n"); 84 return -ENODEV; 85 } 86 87 kvm_riscv_gstage_mode_detect(); 88 89 kvm_riscv_gstage_vmid_detect(); 90 91 kvm_info("hypervisor extension available\n"); 92 93 switch (kvm_riscv_gstage_mode()) { 94 case HGATP_MODE_SV32X4: 95 str = "Sv32x4"; 96 break; 97 case HGATP_MODE_SV39X4: 98 str = "Sv39x4"; 99 break; 100 case HGATP_MODE_SV48X4: 101 str = "Sv48x4"; 102 break; 103 case HGATP_MODE_SV57X4: 104 str = "Sv57x4"; 105 break; 106 default: 107 return -ENODEV; 108 } 109 kvm_info("using %s G-stage page table format\n", str); 110 111 kvm_info("VMID %ld bits available\n", kvm_riscv_gstage_vmid_bits()); 112 113 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 114 } 115 module_init(riscv_kvm_init); 116 117 static void __exit riscv_kvm_exit(void) 118 { 119 kvm_exit(); 120 } 121 module_exit(riscv_kvm_exit); 122