xref: /linux/drivers/firmware/smccc/kvm_guest.c (revision 114143a595895c03fbefccfd8346fc51fb4908ed)
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>
7534aa1dcSPeter 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);
42*0ba5b4baSMarc Zyngier 
43*0ba5b4baSMarc Zyngier 	kvm_arch_init_hyp_services();
446e085e0aSWill Deacon }
456e085e0aSWill Deacon 
kvm_arm_hyp_service_available(u32 func_id)466e085e0aSWill Deacon bool kvm_arm_hyp_service_available(u32 func_id)
476e085e0aSWill Deacon {
486e085e0aSWill Deacon 	if (func_id >= ARM_SMCCC_KVM_NUM_FUNCS)
496e085e0aSWill Deacon 		return false;
506e085e0aSWill Deacon 
516e085e0aSWill Deacon 	return test_bit(func_id, __kvm_arm_hyp_services);
526e085e0aSWill Deacon }
536e085e0aSWill Deacon EXPORT_SYMBOL_GPL(kvm_arm_hyp_service_available);
54