xref: /openbmc/linux/arch/riscv/kvm/vcpu_sbi_base.c (revision 8ffd3d5e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021 Western Digital Corporation or its affiliates.
4  *
5  * Authors:
6  *     Atish Patra <atish.patra@wdc.com>
7  */
8 
9 #include <linux/errno.h>
10 #include <linux/err.h>
11 #include <linux/kvm_host.h>
12 #include <linux/version.h>
13 #include <asm/sbi.h>
14 #include <asm/kvm_vcpu_sbi.h>
15 
16 static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
17 				    unsigned long *out_val,
18 				    struct kvm_cpu_trap *trap, bool *exit)
19 {
20 	int ret = 0;
21 	struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
22 
23 	switch (cp->a6) {
24 	case SBI_EXT_BASE_GET_SPEC_VERSION:
25 		*out_val = (KVM_SBI_VERSION_MAJOR <<
26 			    SBI_SPEC_VERSION_MAJOR_SHIFT) |
27 			    KVM_SBI_VERSION_MINOR;
28 		break;
29 	case SBI_EXT_BASE_GET_IMP_ID:
30 		*out_val = KVM_SBI_IMPID;
31 		break;
32 	case SBI_EXT_BASE_GET_IMP_VERSION:
33 		*out_val = LINUX_VERSION_CODE;
34 		break;
35 	case SBI_EXT_BASE_PROBE_EXT:
36 		if ((cp->a0 >= SBI_EXT_EXPERIMENTAL_START &&
37 		     cp->a0 <= SBI_EXT_EXPERIMENTAL_END) ||
38 		    (cp->a0 >= SBI_EXT_VENDOR_START &&
39 		     cp->a0 <= SBI_EXT_VENDOR_END)) {
40 			/*
41 			 * For experimental/vendor extensions
42 			 * forward it to the userspace
43 			 */
44 			kvm_riscv_vcpu_sbi_forward(vcpu, run);
45 			*exit = true;
46 		} else
47 			*out_val = kvm_vcpu_sbi_find_ext(cp->a0) ? 1 : 0;
48 		break;
49 	case SBI_EXT_BASE_GET_MVENDORID:
50 		*out_val = vcpu->arch.mvendorid;
51 		break;
52 	case SBI_EXT_BASE_GET_MARCHID:
53 		*out_val = vcpu->arch.marchid;
54 		break;
55 	case SBI_EXT_BASE_GET_MIMPID:
56 		*out_val = vcpu->arch.mimpid;
57 		break;
58 	default:
59 		ret = -EOPNOTSUPP;
60 		break;
61 	}
62 
63 	return ret;
64 }
65 
66 const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base = {
67 	.extid_start = SBI_EXT_BASE,
68 	.extid_end = SBI_EXT_BASE,
69 	.handler = kvm_sbi_ext_base_handler,
70 };
71 
72 static int kvm_sbi_ext_forward_handler(struct kvm_vcpu *vcpu,
73 					struct kvm_run *run,
74 					unsigned long *out_val,
75 					struct kvm_cpu_trap *utrap,
76 					bool *exit)
77 {
78 	/*
79 	 * Both SBI experimental and vendor extensions are
80 	 * unconditionally forwarded to userspace.
81 	 */
82 	kvm_riscv_vcpu_sbi_forward(vcpu, run);
83 	*exit = true;
84 	return 0;
85 }
86 
87 const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental = {
88 	.extid_start = SBI_EXT_EXPERIMENTAL_START,
89 	.extid_end = SBI_EXT_EXPERIMENTAL_END,
90 	.handler = kvm_sbi_ext_forward_handler,
91 };
92 
93 const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor = {
94 	.extid_start = SBI_EXT_VENDOR_START,
95 	.extid_end = SBI_EXT_VENDOR_END,
96 	.handler = kvm_sbi_ext_forward_handler,
97 };
98