1 // SPDX-License-Identifier: GPL-2.0
2
3 #define pr_fmt(fmt) "smccc: KVM: " fmt
4
5 #include <linux/arm-smccc.h>
6 #include <linux/bitmap.h>
7 #include <linux/cache.h>
8 #include <linux/kernel.h>
9 #include <linux/string.h>
10
11 #include <asm/hypervisor.h>
12
kvm_arm_init_hyp_services(void)13 void __weak kvm_arm_init_hyp_services(void) {}
14
15 static DECLARE_BITMAP(__kvm_arm_hyp_services, ARM_SMCCC_KVM_NUM_FUNCS) __ro_after_init = { };
16
kvm_init_hyp_services(void)17 void __init kvm_init_hyp_services(void)
18 {
19 struct arm_smccc_res res;
20 u32 val[4];
21
22 if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_HVC)
23 return;
24
25 arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, &res);
26 if (res.a0 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 ||
27 res.a1 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 ||
28 res.a2 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 ||
29 res.a3 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3)
30 return;
31
32 memset(&res, 0, sizeof(res));
33 arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID, &res);
34
35 val[0] = lower_32_bits(res.a0);
36 val[1] = lower_32_bits(res.a1);
37 val[2] = lower_32_bits(res.a2);
38 val[3] = lower_32_bits(res.a3);
39
40 bitmap_from_arr32(__kvm_arm_hyp_services, val, ARM_SMCCC_KVM_NUM_FUNCS);
41
42 pr_info("hypervisor services detected (0x%08lx 0x%08lx 0x%08lx 0x%08lx)\n",
43 res.a3, res.a2, res.a1, res.a0);
44
45 kvm_arm_init_hyp_services();
46 }
47
kvm_arm_hyp_service_available(u32 func_id)48 bool kvm_arm_hyp_service_available(u32 func_id)
49 {
50 if (func_id >= ARM_SMCCC_KVM_NUM_FUNCS)
51 return false;
52
53 return test_bit(func_id, __kvm_arm_hyp_services);
54 }
55 EXPORT_SYMBOL_GPL(kvm_arm_hyp_service_available);
56