16e085e0aSWill Deacon // SPDX-License-Identifier: GPL-2.0
26e085e0aSWill Deacon 
36e085e0aSWill Deacon #define pr_fmt(fmt) "smccc: KVM: " fmt
46e085e0aSWill Deacon 
56e085e0aSWill Deacon #include <linux/arm-smccc.h>
66e085e0aSWill Deacon #include <linux/bitmap.h>
7*534aa1dcSPeter Collingbourne #include <linux/cache.h>
86e085e0aSWill Deacon #include <linux/kernel.h>
96e085e0aSWill Deacon #include <linux/string.h>
106e085e0aSWill Deacon 
116e085e0aSWill Deacon #include <asm/hypervisor.h>
126e085e0aSWill Deacon 
136e085e0aSWill Deacon static DECLARE_BITMAP(__kvm_arm_hyp_services, ARM_SMCCC_KVM_NUM_FUNCS) __ro_after_init = { };
146e085e0aSWill Deacon 
kvm_init_hyp_services(void)156e085e0aSWill Deacon void __init kvm_init_hyp_services(void)
166e085e0aSWill Deacon {
176e085e0aSWill Deacon 	struct arm_smccc_res res;
186e085e0aSWill Deacon 	u32 val[4];
196e085e0aSWill Deacon 
206e085e0aSWill Deacon 	if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_HVC)
216e085e0aSWill Deacon 		return;
226e085e0aSWill Deacon 
236e085e0aSWill Deacon 	arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, &res);
246e085e0aSWill Deacon 	if (res.a0 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 ||
256e085e0aSWill Deacon 	    res.a1 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 ||
266e085e0aSWill Deacon 	    res.a2 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 ||
276e085e0aSWill Deacon 	    res.a3 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3)
286e085e0aSWill Deacon 		return;
296e085e0aSWill Deacon 
306e085e0aSWill Deacon 	memset(&res, 0, sizeof(res));
316e085e0aSWill Deacon 	arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID, &res);
326e085e0aSWill Deacon 
336e085e0aSWill Deacon 	val[0] = lower_32_bits(res.a0);
346e085e0aSWill Deacon 	val[1] = lower_32_bits(res.a1);
356e085e0aSWill Deacon 	val[2] = lower_32_bits(res.a2);
366e085e0aSWill Deacon 	val[3] = lower_32_bits(res.a3);
376e085e0aSWill Deacon 
386e085e0aSWill Deacon 	bitmap_from_arr32(__kvm_arm_hyp_services, val, ARM_SMCCC_KVM_NUM_FUNCS);
396e085e0aSWill Deacon 
406e085e0aSWill Deacon 	pr_info("hypervisor services detected (0x%08lx 0x%08lx 0x%08lx 0x%08lx)\n",
416e085e0aSWill Deacon 		 res.a3, res.a2, res.a1, res.a0);
426e085e0aSWill Deacon }
436e085e0aSWill Deacon 
kvm_arm_hyp_service_available(u32 func_id)446e085e0aSWill Deacon bool kvm_arm_hyp_service_available(u32 func_id)
456e085e0aSWill Deacon {
466e085e0aSWill Deacon 	if (func_id >= ARM_SMCCC_KVM_NUM_FUNCS)
476e085e0aSWill Deacon 		return false;
486e085e0aSWill Deacon 
496e085e0aSWill Deacon 	return test_bit(func_id, __kvm_arm_hyp_services);
506e085e0aSWill Deacon }
516e085e0aSWill Deacon EXPORT_SYMBOL_GPL(kvm_arm_hyp_service_available);
52