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