RISC-V: KVM: Add SBI v0.2 base extension
authorAtish Patra <atish.patra@wdc.com>
Thu, 18 Nov 2021 08:39:10 +0000 (00:39 -0800)
committerAnup Patel <anup@brainfault.org>
Thu, 6 Jan 2022 09:38:29 +0000 (15:08 +0530)
SBI v0.2 base extension defined to allow backward compatibility and
probing of future extensions. This is also the only mandatory SBI
extension that must be implemented by SBI implementors.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Signed-off-by: Anup Patel <anup.patel@wdc.com>
arch/riscv/include/asm/kvm_vcpu_sbi.h
arch/riscv/include/asm/sbi.h
arch/riscv/kvm/Makefile
arch/riscv/kvm/vcpu_sbi.c
arch/riscv/kvm/vcpu_sbi_base.c [new file with mode: 0644]

index 704151969ceb1be6330bf81a46d1ba0a2e633db2..76e4e17a3e00232f8b8c95f886600166485b4cf8 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef __RISCV_KVM_VCPU_SBI_H__
 #define __RISCV_KVM_VCPU_SBI_H__
 
+#define KVM_SBI_IMPID 3
+
 #define KVM_SBI_VERSION_MAJOR 0
 #define KVM_SBI_VERSION_MINOR 2
 
index 0d42693cb65e9c50b3a3548f4ec931ae6dc8a66b..9e4c79fd49d74ea2e6411798f93e75ef2a4cde6e 100644 (file)
@@ -27,6 +27,14 @@ enum sbi_ext_id {
        SBI_EXT_IPI = 0x735049,
        SBI_EXT_RFENCE = 0x52464E43,
        SBI_EXT_HSM = 0x48534D,
+
+       /* Experimentals extensions must lie within this range */
+       SBI_EXT_EXPERIMENTAL_START = 0x08000000,
+       SBI_EXT_EXPERIMENTAL_END = 0x08FFFFFF,
+
+       /* Vendor extensions must lie within this range */
+       SBI_EXT_VENDOR_START = 0x09000000,
+       SBI_EXT_VENDOR_END = 0x09FFFFFF,
 };
 
 enum sbi_ext_base_fid {
index 892c60b07823da4aa846f236d26c67d023f4d67f..c90b3802ee6d91bff8f2bd1f2cb288d36ea61204 100644 (file)
@@ -20,4 +20,5 @@ kvm-y += vcpu_fp.o
 kvm-y += vcpu_switch.o
 kvm-y += vcpu_sbi.o
 kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o
+kvm-y += vcpu_sbi_base.o
 kvm-y += vcpu_timer.o
index a8e0191cd9fcc484d1e5c3844e6f577b32254288..915a044a0b4f8e918027cee8a295ebc8459bebdd 100644 (file)
@@ -39,9 +39,10 @@ static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01 = {
        .handler = NULL,
 };
 #endif
-
+extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base;
 static const struct kvm_vcpu_sbi_extension *sbi_ext[] = {
        &vcpu_sbi_ext_v01,
+       &vcpu_sbi_ext_base,
 };
 
 void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run)
diff --git a/arch/riscv/kvm/vcpu_sbi_base.c b/arch/riscv/kvm/vcpu_sbi_base.c
new file mode 100644 (file)
index 0000000..d1ec08f
--- /dev/null
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *     Atish Patra <atish.patra@wdc.com>
+ */
+
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/kvm_host.h>
+#include <asm/csr.h>
+#include <asm/sbi.h>
+#include <asm/kvm_vcpu_timer.h>
+#include <asm/kvm_vcpu_sbi.h>
+
+static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
+                                   unsigned long *out_val,
+                                   struct kvm_cpu_trap *trap, bool *exit)
+{
+       int ret = 0;
+       struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
+       struct sbiret ecall_ret;
+
+       switch (cp->a6) {
+       case SBI_EXT_BASE_GET_SPEC_VERSION:
+               *out_val = (KVM_SBI_VERSION_MAJOR <<
+                           SBI_SPEC_VERSION_MAJOR_SHIFT) |
+                           KVM_SBI_VERSION_MINOR;
+               break;
+       case SBI_EXT_BASE_GET_IMP_ID:
+               *out_val = KVM_SBI_IMPID;
+               break;
+       case SBI_EXT_BASE_GET_IMP_VERSION:
+               *out_val = 0;
+               break;
+       case SBI_EXT_BASE_PROBE_EXT:
+               if ((cp->a0 >= SBI_EXT_EXPERIMENTAL_START &&
+                    cp->a0 <= SBI_EXT_EXPERIMENTAL_END) ||
+                   (cp->a0 >= SBI_EXT_VENDOR_START &&
+                    cp->a0 <= SBI_EXT_VENDOR_END)) {
+                       /*
+                        * For experimental/vendor extensions
+                        * forward it to the userspace
+                        */
+                       kvm_riscv_vcpu_sbi_forward(vcpu, run);
+                       *exit = true;
+               } else
+                       *out_val = kvm_vcpu_sbi_find_ext(cp->a0) ? 1 : 0;
+               break;
+       case SBI_EXT_BASE_GET_MVENDORID:
+       case SBI_EXT_BASE_GET_MARCHID:
+       case SBI_EXT_BASE_GET_MIMPID:
+               ecall_ret = sbi_ecall(SBI_EXT_BASE, cp->a6, 0, 0, 0, 0, 0, 0);
+               if (!ecall_ret.error)
+                       *out_val = ecall_ret.value;
+               /*TODO: We are unnecessarily converting the error twice */
+               ret = sbi_err_map_linux_errno(ecall_ret.error);
+               break;
+       default:
+               ret = -EOPNOTSUPP;
+               break;
+       }
+
+       return ret;
+}
+
+const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base = {
+       .extid_start = SBI_EXT_BASE,
+       .extid_end = SBI_EXT_BASE,
+       .handler = kvm_sbi_ext_base_handler,
+};