xref: /openbmc/linux/arch/arm64/kvm/psci.c (revision a189884b)
19ed24f4bSMarc Zyngier // SPDX-License-Identifier: GPL-2.0-only
29ed24f4bSMarc Zyngier /*
39ed24f4bSMarc Zyngier  * Copyright (C) 2012 - ARM Ltd
49ed24f4bSMarc Zyngier  * Author: Marc Zyngier <marc.zyngier@arm.com>
59ed24f4bSMarc Zyngier  */
69ed24f4bSMarc Zyngier 
79ed24f4bSMarc Zyngier #include <linux/arm-smccc.h>
89ed24f4bSMarc Zyngier #include <linux/preempt.h>
99ed24f4bSMarc Zyngier #include <linux/kvm_host.h>
109ed24f4bSMarc Zyngier #include <linux/uaccess.h>
119ed24f4bSMarc Zyngier #include <linux/wait.h>
129ed24f4bSMarc Zyngier 
139ed24f4bSMarc Zyngier #include <asm/cputype.h>
149ed24f4bSMarc Zyngier #include <asm/kvm_emulate.h>
159ed24f4bSMarc Zyngier 
169ed24f4bSMarc Zyngier #include <kvm/arm_psci.h>
179ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h>
189ed24f4bSMarc Zyngier 
199ed24f4bSMarc Zyngier /*
209ed24f4bSMarc Zyngier  * This is an implementation of the Power State Coordination Interface
219ed24f4bSMarc Zyngier  * as described in ARM document number ARM DEN 0022A.
229ed24f4bSMarc Zyngier  */
239ed24f4bSMarc Zyngier 
249ed24f4bSMarc Zyngier #define AFFINITY_MASK(level)	~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
259ed24f4bSMarc Zyngier 
psci_affinity_mask(unsigned long affinity_level)269ed24f4bSMarc Zyngier static unsigned long psci_affinity_mask(unsigned long affinity_level)
279ed24f4bSMarc Zyngier {
289ed24f4bSMarc Zyngier 	if (affinity_level <= 3)
299ed24f4bSMarc Zyngier 		return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
309ed24f4bSMarc Zyngier 
319ed24f4bSMarc Zyngier 	return 0;
329ed24f4bSMarc Zyngier }
339ed24f4bSMarc Zyngier 
kvm_psci_vcpu_suspend(struct kvm_vcpu * vcpu)349ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
359ed24f4bSMarc Zyngier {
369ed24f4bSMarc Zyngier 	/*
379ed24f4bSMarc Zyngier 	 * NOTE: For simplicity, we make VCPU suspend emulation to be
389ed24f4bSMarc Zyngier 	 * same-as WFI (Wait-for-interrupt) emulation.
399ed24f4bSMarc Zyngier 	 *
409ed24f4bSMarc Zyngier 	 * This means for KVM the wakeup events are interrupts and
419ed24f4bSMarc Zyngier 	 * this is consistent with intended use of StateID as described
429ed24f4bSMarc Zyngier 	 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
439ed24f4bSMarc Zyngier 	 *
449ed24f4bSMarc Zyngier 	 * Further, we also treat power-down request to be same as
459ed24f4bSMarc Zyngier 	 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
469ed24f4bSMarc Zyngier 	 * specification (ARM DEN 0022A). This means all suspend states
479ed24f4bSMarc Zyngier 	 * for KVM will preserve the register state.
489ed24f4bSMarc Zyngier 	 */
49a867e9d0SOliver Upton 	kvm_vcpu_wfi(vcpu);
509ed24f4bSMarc Zyngier 
519ed24f4bSMarc Zyngier 	return PSCI_RET_SUCCESS;
529ed24f4bSMarc Zyngier }
539ed24f4bSMarc Zyngier 
kvm_psci_valid_affinity(struct kvm_vcpu * vcpu,unsigned long affinity)54e10ecb4dSOliver Upton static inline bool kvm_psci_valid_affinity(struct kvm_vcpu *vcpu,
55e10ecb4dSOliver Upton 					   unsigned long affinity)
56e10ecb4dSOliver Upton {
57e10ecb4dSOliver Upton 	return !(affinity & ~MPIDR_HWID_BITMASK);
58e10ecb4dSOliver Upton }
59e10ecb4dSOliver Upton 
kvm_psci_vcpu_on(struct kvm_vcpu * source_vcpu)609ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
619ed24f4bSMarc Zyngier {
629ed24f4bSMarc Zyngier 	struct vcpu_reset_state *reset_state;
639ed24f4bSMarc Zyngier 	struct kvm *kvm = source_vcpu->kvm;
649ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = NULL;
650acc7239SOliver Upton 	int ret = PSCI_RET_SUCCESS;
669ed24f4bSMarc Zyngier 	unsigned long cpu_id;
679ed24f4bSMarc Zyngier 
68e10ecb4dSOliver Upton 	cpu_id = smccc_get_arg1(source_vcpu);
69e10ecb4dSOliver Upton 	if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
70e10ecb4dSOliver Upton 		return PSCI_RET_INVALID_PARAMS;
719ed24f4bSMarc Zyngier 
729ed24f4bSMarc Zyngier 	vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
739ed24f4bSMarc Zyngier 
749ed24f4bSMarc Zyngier 	/*
759ed24f4bSMarc Zyngier 	 * Make sure the caller requested a valid CPU and that the CPU is
769ed24f4bSMarc Zyngier 	 * turned off.
779ed24f4bSMarc Zyngier 	 */
789ed24f4bSMarc Zyngier 	if (!vcpu)
799ed24f4bSMarc Zyngier 		return PSCI_RET_INVALID_PARAMS;
800acc7239SOliver Upton 
810acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
82b171f9bbSOliver Upton 	if (!kvm_arm_vcpu_stopped(vcpu)) {
83dfefa04aSOliver Upton 		if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
840acc7239SOliver Upton 			ret = PSCI_RET_ALREADY_ON;
859ed24f4bSMarc Zyngier 		else
860acc7239SOliver Upton 			ret = PSCI_RET_INVALID_PARAMS;
870acc7239SOliver Upton 
880acc7239SOliver Upton 		goto out_unlock;
899ed24f4bSMarc Zyngier 	}
909ed24f4bSMarc Zyngier 
919ed24f4bSMarc Zyngier 	reset_state = &vcpu->arch.reset_state;
929ed24f4bSMarc Zyngier 
939ed24f4bSMarc Zyngier 	reset_state->pc = smccc_get_arg2(source_vcpu);
949ed24f4bSMarc Zyngier 
959ed24f4bSMarc Zyngier 	/* Propagate caller endianness */
969ed24f4bSMarc Zyngier 	reset_state->be = kvm_vcpu_is_be(source_vcpu);
979ed24f4bSMarc Zyngier 
989ed24f4bSMarc Zyngier 	/*
999ed24f4bSMarc Zyngier 	 * NOTE: We always update r0 (or x0) because for PSCI v0.1
100656012c7SFuad Tabba 	 * the general purpose registers are undefined upon CPU_ON.
1019ed24f4bSMarc Zyngier 	 */
1029ed24f4bSMarc Zyngier 	reset_state->r0 = smccc_get_arg3(source_vcpu);
1039ed24f4bSMarc Zyngier 
1040acc7239SOliver Upton 	reset_state->reset = true;
1059ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
1069ed24f4bSMarc Zyngier 
1079ed24f4bSMarc Zyngier 	/*
108b171f9bbSOliver Upton 	 * Make sure the reset request is observed if the RUNNABLE mp_state is
109b171f9bbSOliver Upton 	 * observed.
1109ed24f4bSMarc Zyngier 	 */
1119ed24f4bSMarc Zyngier 	smp_wmb();
1129ed24f4bSMarc Zyngier 
113*a189884bSReiji Watanabe 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
1149ed24f4bSMarc Zyngier 	kvm_vcpu_wake_up(vcpu);
1159ed24f4bSMarc Zyngier 
1160acc7239SOliver Upton out_unlock:
1170acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
1180acc7239SOliver Upton 	return ret;
1199ed24f4bSMarc Zyngier }
1209ed24f4bSMarc Zyngier 
kvm_psci_vcpu_affinity_info(struct kvm_vcpu * vcpu)1219ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
1229ed24f4bSMarc Zyngier {
12346808a4cSMarc Zyngier 	int matching_cpus = 0;
12446808a4cSMarc Zyngier 	unsigned long i, mpidr;
1259ed24f4bSMarc Zyngier 	unsigned long target_affinity;
1269ed24f4bSMarc Zyngier 	unsigned long target_affinity_mask;
1279ed24f4bSMarc Zyngier 	unsigned long lowest_affinity_level;
1289ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
1299ed24f4bSMarc Zyngier 	struct kvm_vcpu *tmp;
1309ed24f4bSMarc Zyngier 
1319ed24f4bSMarc Zyngier 	target_affinity = smccc_get_arg1(vcpu);
1329ed24f4bSMarc Zyngier 	lowest_affinity_level = smccc_get_arg2(vcpu);
1339ed24f4bSMarc Zyngier 
134e10ecb4dSOliver Upton 	if (!kvm_psci_valid_affinity(vcpu, target_affinity))
135e10ecb4dSOliver Upton 		return PSCI_RET_INVALID_PARAMS;
136e10ecb4dSOliver Upton 
1379ed24f4bSMarc Zyngier 	/* Determine target affinity mask */
1389ed24f4bSMarc Zyngier 	target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
1399ed24f4bSMarc Zyngier 	if (!target_affinity_mask)
1409ed24f4bSMarc Zyngier 		return PSCI_RET_INVALID_PARAMS;
1419ed24f4bSMarc Zyngier 
1429ed24f4bSMarc Zyngier 	/* Ignore other bits of target affinity */
1439ed24f4bSMarc Zyngier 	target_affinity &= target_affinity_mask;
1449ed24f4bSMarc Zyngier 
1459ed24f4bSMarc Zyngier 	/*
1469ed24f4bSMarc Zyngier 	 * If one or more VCPU matching target affinity are running
1479ed24f4bSMarc Zyngier 	 * then ON else OFF
1489ed24f4bSMarc Zyngier 	 */
1499ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, tmp, kvm) {
1509ed24f4bSMarc Zyngier 		mpidr = kvm_vcpu_get_mpidr_aff(tmp);
1519ed24f4bSMarc Zyngier 		if ((mpidr & target_affinity_mask) == target_affinity) {
1529ed24f4bSMarc Zyngier 			matching_cpus++;
153b171f9bbSOliver Upton 			if (!kvm_arm_vcpu_stopped(tmp))
1549ed24f4bSMarc Zyngier 				return PSCI_0_2_AFFINITY_LEVEL_ON;
1559ed24f4bSMarc Zyngier 		}
1569ed24f4bSMarc Zyngier 	}
1579ed24f4bSMarc Zyngier 
1589ed24f4bSMarc Zyngier 	if (!matching_cpus)
1599ed24f4bSMarc Zyngier 		return PSCI_RET_INVALID_PARAMS;
1609ed24f4bSMarc Zyngier 
1619ed24f4bSMarc Zyngier 	return PSCI_0_2_AFFINITY_LEVEL_OFF;
1629ed24f4bSMarc Zyngier }
1639ed24f4bSMarc Zyngier 
kvm_prepare_system_event(struct kvm_vcpu * vcpu,u32 type,u64 flags)16434739fd9SWill Deacon static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
1659ed24f4bSMarc Zyngier {
16646808a4cSMarc Zyngier 	unsigned long i;
1679ed24f4bSMarc Zyngier 	struct kvm_vcpu *tmp;
1689ed24f4bSMarc Zyngier 
1699ed24f4bSMarc Zyngier 	/*
1709ed24f4bSMarc Zyngier 	 * The KVM ABI specifies that a system event exit may call KVM_RUN
1719ed24f4bSMarc Zyngier 	 * again and may perform shutdown/reboot at a later time that when the
1729ed24f4bSMarc Zyngier 	 * actual request is made.  Since we are implementing PSCI and a
1739ed24f4bSMarc Zyngier 	 * caller of PSCI reboot and shutdown expects that the system shuts
1749ed24f4bSMarc Zyngier 	 * down or reboots immediately, let's make sure that VCPUs are not run
1759ed24f4bSMarc Zyngier 	 * after this call is handled and before the VCPUs have been
1769ed24f4bSMarc Zyngier 	 * re-initialized.
1779ed24f4bSMarc Zyngier 	 */
1780acc7239SOliver Upton 	kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
1790acc7239SOliver Upton 		spin_lock(&tmp->arch.mp_state_lock);
1800acc7239SOliver Upton 		WRITE_ONCE(tmp->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
1810acc7239SOliver Upton 		spin_unlock(&tmp->arch.mp_state_lock);
1820acc7239SOliver Upton 	}
1839ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
1849ed24f4bSMarc Zyngier 
1859ed24f4bSMarc Zyngier 	memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
1869ed24f4bSMarc Zyngier 	vcpu->run->system_event.type = type;
187d495f942SPaolo Bonzini 	vcpu->run->system_event.ndata = 1;
188d495f942SPaolo Bonzini 	vcpu->run->system_event.data[0] = flags;
1899ed24f4bSMarc Zyngier 	vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
1909ed24f4bSMarc Zyngier }
1919ed24f4bSMarc Zyngier 
kvm_psci_system_off(struct kvm_vcpu * vcpu)1929ed24f4bSMarc Zyngier static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
1939ed24f4bSMarc Zyngier {
19434739fd9SWill Deacon 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0);
1959ed24f4bSMarc Zyngier }
1969ed24f4bSMarc Zyngier 
kvm_psci_system_reset(struct kvm_vcpu * vcpu)1979ed24f4bSMarc Zyngier static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
1989ed24f4bSMarc Zyngier {
19934739fd9SWill Deacon 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0);
20034739fd9SWill Deacon }
20134739fd9SWill Deacon 
kvm_psci_system_reset2(struct kvm_vcpu * vcpu)20234739fd9SWill Deacon static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu)
20334739fd9SWill Deacon {
20434739fd9SWill Deacon 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET,
20534739fd9SWill Deacon 				 KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2);
2069ed24f4bSMarc Zyngier }
2079ed24f4bSMarc Zyngier 
kvm_psci_system_suspend(struct kvm_vcpu * vcpu)208bfbab445SOliver Upton static void kvm_psci_system_suspend(struct kvm_vcpu *vcpu)
209bfbab445SOliver Upton {
210bfbab445SOliver Upton 	struct kvm_run *run = vcpu->run;
211bfbab445SOliver Upton 
212bfbab445SOliver Upton 	memset(&run->system_event, 0, sizeof(vcpu->run->system_event));
213bfbab445SOliver Upton 	run->system_event.type = KVM_SYSTEM_EVENT_SUSPEND;
214bfbab445SOliver Upton 	run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
215bfbab445SOliver Upton }
216bfbab445SOliver Upton 
kvm_psci_narrow_to_32bit(struct kvm_vcpu * vcpu)2179ed24f4bSMarc Zyngier static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu)
2189ed24f4bSMarc Zyngier {
2199ed24f4bSMarc Zyngier 	int i;
2209ed24f4bSMarc Zyngier 
2219ed24f4bSMarc Zyngier 	/*
2229ed24f4bSMarc Zyngier 	 * Zero the input registers' upper 32 bits. They will be fully
2239ed24f4bSMarc Zyngier 	 * zeroed on exit, so we're fine changing them in place.
2249ed24f4bSMarc Zyngier 	 */
2259ed24f4bSMarc Zyngier 	for (i = 1; i < 4; i++)
2269ed24f4bSMarc Zyngier 		vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i)));
2279ed24f4bSMarc Zyngier }
2289ed24f4bSMarc Zyngier 
kvm_psci_check_allowed_function(struct kvm_vcpu * vcpu,u32 fn)2299ed24f4bSMarc Zyngier static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
2309ed24f4bSMarc Zyngier {
2312da0aebcSOliver Upton 	/*
2322da0aebcSOliver Upton 	 * Prevent 32 bit guests from calling 64 bit PSCI functions.
2332da0aebcSOliver Upton 	 */
2342da0aebcSOliver Upton 	if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
2359ed24f4bSMarc Zyngier 		return PSCI_RET_NOT_SUPPORTED;
2369ed24f4bSMarc Zyngier 
2379ed24f4bSMarc Zyngier 	return 0;
2389ed24f4bSMarc Zyngier }
2399ed24f4bSMarc Zyngier 
kvm_psci_0_2_call(struct kvm_vcpu * vcpu)2409ed24f4bSMarc Zyngier static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
2419ed24f4bSMarc Zyngier {
2429ed24f4bSMarc Zyngier 	u32 psci_fn = smccc_get_function(vcpu);
2439ed24f4bSMarc Zyngier 	unsigned long val;
2449ed24f4bSMarc Zyngier 	int ret = 1;
2459ed24f4bSMarc Zyngier 
2469ed24f4bSMarc Zyngier 	switch (psci_fn) {
2479ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_PSCI_VERSION:
2489ed24f4bSMarc Zyngier 		/*
2499ed24f4bSMarc Zyngier 		 * Bits[31:16] = Major Version = 0
2509ed24f4bSMarc Zyngier 		 * Bits[15:0] = Minor Version = 2
2519ed24f4bSMarc Zyngier 		 */
2529ed24f4bSMarc Zyngier 		val = KVM_ARM_PSCI_0_2;
2539ed24f4bSMarc Zyngier 		break;
2549ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_CPU_SUSPEND:
2559ed24f4bSMarc Zyngier 	case PSCI_0_2_FN64_CPU_SUSPEND:
2569ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_suspend(vcpu);
2579ed24f4bSMarc Zyngier 		break;
2589ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_CPU_OFF:
2591e579429SOliver Upton 		kvm_arm_vcpu_power_off(vcpu);
2609ed24f4bSMarc Zyngier 		val = PSCI_RET_SUCCESS;
2619ed24f4bSMarc Zyngier 		break;
2629ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_CPU_ON:
2639ed24f4bSMarc Zyngier 		kvm_psci_narrow_to_32bit(vcpu);
2649ed24f4bSMarc Zyngier 		fallthrough;
2659ed24f4bSMarc Zyngier 	case PSCI_0_2_FN64_CPU_ON:
2669ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_on(vcpu);
2679ed24f4bSMarc Zyngier 		break;
2689ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_AFFINITY_INFO:
2699ed24f4bSMarc Zyngier 		kvm_psci_narrow_to_32bit(vcpu);
2709ed24f4bSMarc Zyngier 		fallthrough;
2719ed24f4bSMarc Zyngier 	case PSCI_0_2_FN64_AFFINITY_INFO:
2729ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_affinity_info(vcpu);
2739ed24f4bSMarc Zyngier 		break;
2749ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
2759ed24f4bSMarc Zyngier 		/*
2769ed24f4bSMarc Zyngier 		 * Trusted OS is MP hence does not require migration
2779ed24f4bSMarc Zyngier 	         * or
2789ed24f4bSMarc Zyngier 		 * Trusted OS is not present
2799ed24f4bSMarc Zyngier 		 */
2809ed24f4bSMarc Zyngier 		val = PSCI_0_2_TOS_MP;
2819ed24f4bSMarc Zyngier 		break;
2829ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_SYSTEM_OFF:
2839ed24f4bSMarc Zyngier 		kvm_psci_system_off(vcpu);
2849ed24f4bSMarc Zyngier 		/*
285656012c7SFuad Tabba 		 * We shouldn't be going back to guest VCPU after
2869ed24f4bSMarc Zyngier 		 * receiving SYSTEM_OFF request.
2879ed24f4bSMarc Zyngier 		 *
288656012c7SFuad Tabba 		 * If user space accidentally/deliberately resumes
2899ed24f4bSMarc Zyngier 		 * guest VCPU after SYSTEM_OFF request then guest
2909ed24f4bSMarc Zyngier 		 * VCPU should see internal failure from PSCI return
2919ed24f4bSMarc Zyngier 		 * value. To achieve this, we preload r0 (or x0) with
2929ed24f4bSMarc Zyngier 		 * PSCI return value INTERNAL_FAILURE.
2939ed24f4bSMarc Zyngier 		 */
2949ed24f4bSMarc Zyngier 		val = PSCI_RET_INTERNAL_FAILURE;
2959ed24f4bSMarc Zyngier 		ret = 0;
2969ed24f4bSMarc Zyngier 		break;
2979ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_SYSTEM_RESET:
2989ed24f4bSMarc Zyngier 		kvm_psci_system_reset(vcpu);
2999ed24f4bSMarc Zyngier 		/*
3009ed24f4bSMarc Zyngier 		 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
3019ed24f4bSMarc Zyngier 		 * with PSCI return value INTERNAL_FAILURE.
3029ed24f4bSMarc Zyngier 		 */
3039ed24f4bSMarc Zyngier 		val = PSCI_RET_INTERNAL_FAILURE;
3049ed24f4bSMarc Zyngier 		ret = 0;
3059ed24f4bSMarc Zyngier 		break;
3069ed24f4bSMarc Zyngier 	default:
3079ed24f4bSMarc Zyngier 		val = PSCI_RET_NOT_SUPPORTED;
3089ed24f4bSMarc Zyngier 		break;
3099ed24f4bSMarc Zyngier 	}
3109ed24f4bSMarc Zyngier 
3119ed24f4bSMarc Zyngier 	smccc_set_retval(vcpu, val, 0, 0, 0);
3129ed24f4bSMarc Zyngier 	return ret;
3139ed24f4bSMarc Zyngier }
3149ed24f4bSMarc Zyngier 
kvm_psci_1_x_call(struct kvm_vcpu * vcpu,u32 minor)315512865d8SWill Deacon static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
3169ed24f4bSMarc Zyngier {
3175bc2cb95SOliver Upton 	unsigned long val = PSCI_RET_NOT_SUPPORTED;
3189ed24f4bSMarc Zyngier 	u32 psci_fn = smccc_get_function(vcpu);
319bfbab445SOliver Upton 	struct kvm *kvm = vcpu->kvm;
320d43583b8SWill Deacon 	u32 arg;
3219ed24f4bSMarc Zyngier 	int ret = 1;
3229ed24f4bSMarc Zyngier 
3239ed24f4bSMarc Zyngier 	switch(psci_fn) {
3249ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_PSCI_VERSION:
325512865d8SWill Deacon 		val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1;
3269ed24f4bSMarc Zyngier 		break;
3279ed24f4bSMarc Zyngier 	case PSCI_1_0_FN_PSCI_FEATURES:
328d43583b8SWill Deacon 		arg = smccc_get_arg1(vcpu);
329d43583b8SWill Deacon 		val = kvm_psci_check_allowed_function(vcpu, arg);
3309ed24f4bSMarc Zyngier 		if (val)
3319ed24f4bSMarc Zyngier 			break;
3329ed24f4bSMarc Zyngier 
3335bc2cb95SOliver Upton 		val = PSCI_RET_NOT_SUPPORTED;
3345bc2cb95SOliver Upton 
335d43583b8SWill Deacon 		switch(arg) {
3369ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_PSCI_VERSION:
3379ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_CPU_SUSPEND:
3389ed24f4bSMarc Zyngier 		case PSCI_0_2_FN64_CPU_SUSPEND:
3399ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_CPU_OFF:
3409ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_CPU_ON:
3419ed24f4bSMarc Zyngier 		case PSCI_0_2_FN64_CPU_ON:
3429ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_AFFINITY_INFO:
3439ed24f4bSMarc Zyngier 		case PSCI_0_2_FN64_AFFINITY_INFO:
3449ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
3459ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_SYSTEM_OFF:
3469ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_SYSTEM_RESET:
3479ed24f4bSMarc Zyngier 		case PSCI_1_0_FN_PSCI_FEATURES:
3489ed24f4bSMarc Zyngier 		case ARM_SMCCC_VERSION_FUNC_ID:
3499ed24f4bSMarc Zyngier 			val = 0;
3509ed24f4bSMarc Zyngier 			break;
351bfbab445SOliver Upton 		case PSCI_1_0_FN_SYSTEM_SUSPEND:
352bfbab445SOliver Upton 		case PSCI_1_0_FN64_SYSTEM_SUSPEND:
353bfbab445SOliver Upton 			if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags))
354bfbab445SOliver Upton 				val = 0;
355bfbab445SOliver Upton 			break;
356d43583b8SWill Deacon 		case PSCI_1_1_FN_SYSTEM_RESET2:
357d43583b8SWill Deacon 		case PSCI_1_1_FN64_SYSTEM_RESET2:
3585bc2cb95SOliver Upton 			if (minor >= 1)
359d43583b8SWill Deacon 				val = 0;
360d43583b8SWill Deacon 			break;
361d43583b8SWill Deacon 		}
3629ed24f4bSMarc Zyngier 		break;
363bfbab445SOliver Upton 	case PSCI_1_0_FN_SYSTEM_SUSPEND:
364bfbab445SOliver Upton 		kvm_psci_narrow_to_32bit(vcpu);
365bfbab445SOliver Upton 		fallthrough;
366bfbab445SOliver Upton 	case PSCI_1_0_FN64_SYSTEM_SUSPEND:
367bfbab445SOliver Upton 		/*
368bfbab445SOliver Upton 		 * Return directly to userspace without changing the vCPU's
369bfbab445SOliver Upton 		 * registers. Userspace depends on reading the SMCCC parameters
370bfbab445SOliver Upton 		 * to implement SYSTEM_SUSPEND.
371bfbab445SOliver Upton 		 */
372bfbab445SOliver Upton 		if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags)) {
373bfbab445SOliver Upton 			kvm_psci_system_suspend(vcpu);
374bfbab445SOliver Upton 			return 0;
3759ed24f4bSMarc Zyngier 		}
3769ed24f4bSMarc Zyngier 		break;
377d43583b8SWill Deacon 	case PSCI_1_1_FN_SYSTEM_RESET2:
378d43583b8SWill Deacon 		kvm_psci_narrow_to_32bit(vcpu);
379d43583b8SWill Deacon 		fallthrough;
380d43583b8SWill Deacon 	case PSCI_1_1_FN64_SYSTEM_RESET2:
381d43583b8SWill Deacon 		if (minor >= 1) {
382d43583b8SWill Deacon 			arg = smccc_get_arg1(vcpu);
383d43583b8SWill Deacon 
3849d3e7b7cSWill Deacon 			if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET ||
3859d3e7b7cSWill Deacon 			    arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) {
38634739fd9SWill Deacon 				kvm_psci_system_reset2(vcpu);
3879d3e7b7cSWill Deacon 				vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE);
3889d3e7b7cSWill Deacon 				return 0;
389d43583b8SWill Deacon 			}
3909d3e7b7cSWill Deacon 
3919d3e7b7cSWill Deacon 			val = PSCI_RET_INVALID_PARAMS;
392d43583b8SWill Deacon 			break;
393ae82047eSChangcheng Deng 		}
3945bc2cb95SOliver Upton 		break;
3959ed24f4bSMarc Zyngier 	default:
3969ed24f4bSMarc Zyngier 		return kvm_psci_0_2_call(vcpu);
3979ed24f4bSMarc Zyngier 	}
3989ed24f4bSMarc Zyngier 
3999ed24f4bSMarc Zyngier 	smccc_set_retval(vcpu, val, 0, 0, 0);
4009ed24f4bSMarc Zyngier 	return ret;
4019ed24f4bSMarc Zyngier }
4029ed24f4bSMarc Zyngier 
kvm_psci_0_1_call(struct kvm_vcpu * vcpu)4039ed24f4bSMarc Zyngier static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
4049ed24f4bSMarc Zyngier {
4059ed24f4bSMarc Zyngier 	u32 psci_fn = smccc_get_function(vcpu);
4069ed24f4bSMarc Zyngier 	unsigned long val;
4079ed24f4bSMarc Zyngier 
4089ed24f4bSMarc Zyngier 	switch (psci_fn) {
4099ed24f4bSMarc Zyngier 	case KVM_PSCI_FN_CPU_OFF:
4101e579429SOliver Upton 		kvm_arm_vcpu_power_off(vcpu);
4119ed24f4bSMarc Zyngier 		val = PSCI_RET_SUCCESS;
4129ed24f4bSMarc Zyngier 		break;
4139ed24f4bSMarc Zyngier 	case KVM_PSCI_FN_CPU_ON:
4149ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_on(vcpu);
4159ed24f4bSMarc Zyngier 		break;
4169ed24f4bSMarc Zyngier 	default:
4179ed24f4bSMarc Zyngier 		val = PSCI_RET_NOT_SUPPORTED;
4189ed24f4bSMarc Zyngier 		break;
4199ed24f4bSMarc Zyngier 	}
4209ed24f4bSMarc Zyngier 
4219ed24f4bSMarc Zyngier 	smccc_set_retval(vcpu, val, 0, 0, 0);
4229ed24f4bSMarc Zyngier 	return 1;
4239ed24f4bSMarc Zyngier }
4249ed24f4bSMarc Zyngier 
4259ed24f4bSMarc Zyngier /**
4269ed24f4bSMarc Zyngier  * kvm_psci_call - handle PSCI call if r0 value is in range
4279ed24f4bSMarc Zyngier  * @vcpu: Pointer to the VCPU struct
4289ed24f4bSMarc Zyngier  *
4299ed24f4bSMarc Zyngier  * Handle PSCI calls from guests through traps from HVC instructions.
4309ed24f4bSMarc Zyngier  * The calling convention is similar to SMC calls to the secure world
4319ed24f4bSMarc Zyngier  * where the function number is placed in r0.
4329ed24f4bSMarc Zyngier  *
4339ed24f4bSMarc Zyngier  * This function returns: > 0 (success), 0 (success but exit to user
4349ed24f4bSMarc Zyngier  * space), and < 0 (errors)
4359ed24f4bSMarc Zyngier  *
4369ed24f4bSMarc Zyngier  * Errors:
4379ed24f4bSMarc Zyngier  * -EINVAL: Unrecognized PSCI function
4389ed24f4bSMarc Zyngier  */
kvm_psci_call(struct kvm_vcpu * vcpu)4399ed24f4bSMarc Zyngier int kvm_psci_call(struct kvm_vcpu *vcpu)
4409ed24f4bSMarc Zyngier {
441827c2ab3SOliver Upton 	u32 psci_fn = smccc_get_function(vcpu);
442827c2ab3SOliver Upton 	int version = kvm_psci_version(vcpu);
443827c2ab3SOliver Upton 	unsigned long val;
444827c2ab3SOliver Upton 
445827c2ab3SOliver Upton 	val = kvm_psci_check_allowed_function(vcpu, psci_fn);
446827c2ab3SOliver Upton 	if (val) {
447827c2ab3SOliver Upton 		smccc_set_retval(vcpu, val, 0, 0, 0);
448827c2ab3SOliver Upton 		return 1;
449827c2ab3SOliver Upton 	}
450dfefa04aSOliver Upton 
451512865d8SWill Deacon 	switch (version) {
452512865d8SWill Deacon 	case KVM_ARM_PSCI_1_1:
4539ed24f4bSMarc Zyngier 		return kvm_psci_1_x_call(vcpu, 1);
454512865d8SWill Deacon 	case KVM_ARM_PSCI_1_0:
4559ed24f4bSMarc Zyngier 		return kvm_psci_1_x_call(vcpu, 0);
4569ed24f4bSMarc Zyngier 	case KVM_ARM_PSCI_0_2:
4579ed24f4bSMarc Zyngier 		return kvm_psci_0_2_call(vcpu);
4589ed24f4bSMarc Zyngier 	case KVM_ARM_PSCI_0_1:
4599ed24f4bSMarc Zyngier 		return kvm_psci_0_1_call(vcpu);
4609ed24f4bSMarc Zyngier 	default:
461ae82047eSChangcheng Deng 		WARN_ONCE(1, "Unknown PSCI version %d", version);
4629ed24f4bSMarc Zyngier 		smccc_set_retval(vcpu, SMCCC_RET_NOT_SUPPORTED, 0, 0, 0);
463 		return 1;
464 	}
465 }
466