2021-09-27 11:40:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Western Digital Corporation or its affiliates.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kvm_host.h>
|
2023-10-31 06:45:52 +00:00
|
|
|
#include <asm/cpufeature.h>
|
2024-10-20 19:47:29 +00:00
|
|
|
#include <asm/kvm_nacl.h>
|
2021-09-27 11:40:01 +00:00
|
|
|
#include <asm/sbi.h>
|
|
|
|
|
|
|
|
long kvm_arch_dev_ioctl(struct file *filp,
|
|
|
|
unsigned int ioctl, unsigned long arg)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2024-08-30 04:35:54 +00:00
|
|
|
int kvm_arch_enable_virtualization_cpu(void)
|
2021-09-27 11:40:01 +00:00
|
|
|
{
|
2024-10-20 19:47:29 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = kvm_riscv_nacl_enable();
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2024-04-02 06:26:26 +00:00
|
|
|
csr_write(CSR_HEDELEG, KVM_HEDELEG_DEFAULT);
|
|
|
|
csr_write(CSR_HIDELEG, KVM_HIDELEG_DEFAULT);
|
2021-09-27 11:40:01 +00:00
|
|
|
|
2023-02-07 09:55:25 +00:00
|
|
|
/* VS should access only the time counter directly. Everything else should trap */
|
|
|
|
csr_write(CSR_HCOUNTEREN, 0x02);
|
2021-09-27 11:40:01 +00:00
|
|
|
|
|
|
|
csr_write(CSR_HVIP, 0);
|
|
|
|
|
2023-01-10 11:14:25 +00:00
|
|
|
kvm_riscv_aia_enable();
|
|
|
|
|
2021-09-27 11:40:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-30 04:35:54 +00:00
|
|
|
void kvm_arch_disable_virtualization_cpu(void)
|
2021-09-27 11:40:01 +00:00
|
|
|
{
|
2023-01-10 11:14:25 +00:00
|
|
|
kvm_riscv_aia_disable();
|
|
|
|
|
2021-12-27 03:05:14 +00:00
|
|
|
/*
|
|
|
|
* After clearing the hideleg CSR, the host kernel will receive
|
|
|
|
* spurious interrupts if hvip CSR has pending interrupts and the
|
|
|
|
* corresponding enable bits in vsie CSR are asserted. To avoid it,
|
|
|
|
* hvip CSR and vsie CSR must be cleared before clearing hideleg CSR.
|
|
|
|
*/
|
|
|
|
csr_write(CSR_VSIE, 0);
|
|
|
|
csr_write(CSR_HVIP, 0);
|
2021-09-27 11:40:01 +00:00
|
|
|
csr_write(CSR_HEDELEG, 0);
|
|
|
|
csr_write(CSR_HIDELEG, 0);
|
2024-10-20 19:47:29 +00:00
|
|
|
|
|
|
|
kvm_riscv_nacl_disable();
|
2021-09-27 11:40:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 02:58:37 +00:00
|
|
|
static void kvm_riscv_teardown(void)
|
|
|
|
{
|
|
|
|
kvm_riscv_aia_exit();
|
2024-10-20 19:47:29 +00:00
|
|
|
kvm_riscv_nacl_exit();
|
2024-10-15 02:58:37 +00:00
|
|
|
kvm_unregister_perf_callbacks();
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:09:08 +00:00
|
|
|
static int __init riscv_kvm_init(void)
|
2021-09-27 11:40:01 +00:00
|
|
|
{
|
2023-01-10 11:14:25 +00:00
|
|
|
int rc;
|
2024-10-20 19:47:29 +00:00
|
|
|
char slist[64];
|
2021-09-27 11:40:09 +00:00
|
|
|
const char *str;
|
|
|
|
|
2021-09-27 11:40:01 +00:00
|
|
|
if (!riscv_isa_extension_available(NULL, h)) {
|
|
|
|
kvm_info("hypervisor extension not available\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sbi_spec_is_0_1()) {
|
|
|
|
kvm_info("require SBI v0.2 or higher\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2023-04-27 16:36:26 +00:00
|
|
|
if (!sbi_probe_extension(SBI_EXT_RFENCE)) {
|
2021-09-27 11:40:01 +00:00
|
|
|
kvm_info("require SBI RFENCE extension\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2024-10-20 19:47:29 +00:00
|
|
|
rc = kvm_riscv_nacl_init();
|
|
|
|
if (rc && rc != -ENODEV)
|
|
|
|
return rc;
|
|
|
|
|
2022-05-09 05:13:30 +00:00
|
|
|
kvm_riscv_gstage_mode_detect();
|
2021-09-27 11:40:09 +00:00
|
|
|
|
2022-05-09 05:13:30 +00:00
|
|
|
kvm_riscv_gstage_vmid_detect();
|
2021-09-27 11:40:08 +00:00
|
|
|
|
2023-01-10 11:14:25 +00:00
|
|
|
rc = kvm_riscv_aia_init();
|
2024-10-20 19:47:29 +00:00
|
|
|
if (rc && rc != -ENODEV) {
|
|
|
|
kvm_riscv_nacl_exit();
|
2023-01-10 11:14:25 +00:00
|
|
|
return rc;
|
2024-10-20 19:47:29 +00:00
|
|
|
}
|
2023-01-10 11:14:25 +00:00
|
|
|
|
2021-09-27 11:40:01 +00:00
|
|
|
kvm_info("hypervisor extension available\n");
|
|
|
|
|
2024-10-20 19:47:29 +00:00
|
|
|
if (kvm_riscv_nacl_available()) {
|
|
|
|
rc = 0;
|
|
|
|
slist[0] = '\0';
|
|
|
|
if (kvm_riscv_nacl_sync_csr_available()) {
|
|
|
|
if (rc)
|
|
|
|
strcat(slist, ", ");
|
|
|
|
strcat(slist, "sync_csr");
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
if (kvm_riscv_nacl_sync_hfence_available()) {
|
|
|
|
if (rc)
|
|
|
|
strcat(slist, ", ");
|
|
|
|
strcat(slist, "sync_hfence");
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
if (kvm_riscv_nacl_sync_sret_available()) {
|
|
|
|
if (rc)
|
|
|
|
strcat(slist, ", ");
|
|
|
|
strcat(slist, "sync_sret");
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
if (kvm_riscv_nacl_autoswap_csr_available()) {
|
|
|
|
if (rc)
|
|
|
|
strcat(slist, ", ");
|
|
|
|
strcat(slist, "autoswap_csr");
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
kvm_info("using SBI nested acceleration with %s\n",
|
|
|
|
(rc) ? slist : "no features");
|
|
|
|
}
|
|
|
|
|
2022-05-09 05:13:30 +00:00
|
|
|
switch (kvm_riscv_gstage_mode()) {
|
2021-09-27 11:40:09 +00:00
|
|
|
case HGATP_MODE_SV32X4:
|
|
|
|
str = "Sv32x4";
|
|
|
|
break;
|
|
|
|
case HGATP_MODE_SV39X4:
|
|
|
|
str = "Sv39x4";
|
|
|
|
break;
|
|
|
|
case HGATP_MODE_SV48X4:
|
|
|
|
str = "Sv48x4";
|
|
|
|
break;
|
2022-05-09 05:13:39 +00:00
|
|
|
case HGATP_MODE_SV57X4:
|
|
|
|
str = "Sv57x4";
|
|
|
|
break;
|
2021-09-27 11:40:09 +00:00
|
|
|
default:
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
kvm_info("using %s G-stage page table format\n", str);
|
|
|
|
|
2022-05-09 05:13:30 +00:00
|
|
|
kvm_info("VMID %ld bits available\n", kvm_riscv_gstage_vmid_bits());
|
2021-09-27 11:40:08 +00:00
|
|
|
|
2023-01-10 11:14:25 +00:00
|
|
|
if (kvm_riscv_aia_available())
|
2023-06-15 07:33:44 +00:00
|
|
|
kvm_info("AIA available with %d guest external interrupts\n",
|
|
|
|
kvm_riscv_aia_nr_hgei);
|
2023-01-10 11:14:25 +00:00
|
|
|
|
2024-10-15 02:58:37 +00:00
|
|
|
kvm_register_perf_callbacks(NULL);
|
|
|
|
|
2023-01-10 11:14:25 +00:00
|
|
|
rc = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
|
|
|
|
if (rc) {
|
2024-10-15 02:58:37 +00:00
|
|
|
kvm_riscv_teardown();
|
2023-01-10 11:14:25 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-09-27 11:40:01 +00:00
|
|
|
}
|
|
|
|
module_init(riscv_kvm_init);
|
2022-12-07 03:46:02 +00:00
|
|
|
|
|
|
|
static void __exit riscv_kvm_exit(void)
|
|
|
|
{
|
2024-10-15 02:58:37 +00:00
|
|
|
kvm_riscv_teardown();
|
2023-01-10 11:14:25 +00:00
|
|
|
|
2022-12-07 03:46:02 +00:00
|
|
|
kvm_exit();
|
|
|
|
}
|
|
|
|
module_exit(riscv_kvm_exit);
|