xref: /linux/arch/arm64/kvm/psci.c (revision b171f9bbb130cb323f2101edd32da2a25d43ebfa)
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 
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 
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 
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 
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;
659ed24f4bSMarc Zyngier 	unsigned long cpu_id;
669ed24f4bSMarc Zyngier 
67e10ecb4dSOliver Upton 	cpu_id = smccc_get_arg1(source_vcpu);
68e10ecb4dSOliver Upton 	if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
69e10ecb4dSOliver Upton 		return PSCI_RET_INVALID_PARAMS;
709ed24f4bSMarc Zyngier 
719ed24f4bSMarc Zyngier 	vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
729ed24f4bSMarc Zyngier 
739ed24f4bSMarc Zyngier 	/*
749ed24f4bSMarc Zyngier 	 * Make sure the caller requested a valid CPU and that the CPU is
759ed24f4bSMarc Zyngier 	 * turned off.
769ed24f4bSMarc Zyngier 	 */
779ed24f4bSMarc Zyngier 	if (!vcpu)
789ed24f4bSMarc Zyngier 		return PSCI_RET_INVALID_PARAMS;
79*b171f9bbSOliver Upton 	if (!kvm_arm_vcpu_stopped(vcpu)) {
80dfefa04aSOliver Upton 		if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
819ed24f4bSMarc Zyngier 			return PSCI_RET_ALREADY_ON;
829ed24f4bSMarc Zyngier 		else
839ed24f4bSMarc Zyngier 			return PSCI_RET_INVALID_PARAMS;
849ed24f4bSMarc Zyngier 	}
859ed24f4bSMarc Zyngier 
869ed24f4bSMarc Zyngier 	reset_state = &vcpu->arch.reset_state;
879ed24f4bSMarc Zyngier 
889ed24f4bSMarc Zyngier 	reset_state->pc = smccc_get_arg2(source_vcpu);
899ed24f4bSMarc Zyngier 
909ed24f4bSMarc Zyngier 	/* Propagate caller endianness */
919ed24f4bSMarc Zyngier 	reset_state->be = kvm_vcpu_is_be(source_vcpu);
929ed24f4bSMarc Zyngier 
939ed24f4bSMarc Zyngier 	/*
949ed24f4bSMarc Zyngier 	 * NOTE: We always update r0 (or x0) because for PSCI v0.1
95656012c7SFuad Tabba 	 * the general purpose registers are undefined upon CPU_ON.
969ed24f4bSMarc Zyngier 	 */
979ed24f4bSMarc Zyngier 	reset_state->r0 = smccc_get_arg3(source_vcpu);
989ed24f4bSMarc Zyngier 
999ed24f4bSMarc Zyngier 	WRITE_ONCE(reset_state->reset, true);
1009ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
1019ed24f4bSMarc Zyngier 
1029ed24f4bSMarc Zyngier 	/*
103*b171f9bbSOliver Upton 	 * Make sure the reset request is observed if the RUNNABLE mp_state is
104*b171f9bbSOliver Upton 	 * observed.
1059ed24f4bSMarc Zyngier 	 */
1069ed24f4bSMarc Zyngier 	smp_wmb();
1079ed24f4bSMarc Zyngier 
108*b171f9bbSOliver Upton 	vcpu->arch.mp_state.mp_state = KVM_MP_STATE_RUNNABLE;
1099ed24f4bSMarc Zyngier 	kvm_vcpu_wake_up(vcpu);
1109ed24f4bSMarc Zyngier 
1119ed24f4bSMarc Zyngier 	return PSCI_RET_SUCCESS;
1129ed24f4bSMarc Zyngier }
1139ed24f4bSMarc Zyngier 
1149ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
1159ed24f4bSMarc Zyngier {
11646808a4cSMarc Zyngier 	int matching_cpus = 0;
11746808a4cSMarc Zyngier 	unsigned long i, mpidr;
1189ed24f4bSMarc Zyngier 	unsigned long target_affinity;
1199ed24f4bSMarc Zyngier 	unsigned long target_affinity_mask;
1209ed24f4bSMarc Zyngier 	unsigned long lowest_affinity_level;
1219ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
1229ed24f4bSMarc Zyngier 	struct kvm_vcpu *tmp;
1239ed24f4bSMarc Zyngier 
1249ed24f4bSMarc Zyngier 	target_affinity = smccc_get_arg1(vcpu);
1259ed24f4bSMarc Zyngier 	lowest_affinity_level = smccc_get_arg2(vcpu);
1269ed24f4bSMarc Zyngier 
127e10ecb4dSOliver Upton 	if (!kvm_psci_valid_affinity(vcpu, target_affinity))
128e10ecb4dSOliver Upton 		return PSCI_RET_INVALID_PARAMS;
129e10ecb4dSOliver Upton 
1309ed24f4bSMarc Zyngier 	/* Determine target affinity mask */
1319ed24f4bSMarc Zyngier 	target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
1329ed24f4bSMarc Zyngier 	if (!target_affinity_mask)
1339ed24f4bSMarc Zyngier 		return PSCI_RET_INVALID_PARAMS;
1349ed24f4bSMarc Zyngier 
1359ed24f4bSMarc Zyngier 	/* Ignore other bits of target affinity */
1369ed24f4bSMarc Zyngier 	target_affinity &= target_affinity_mask;
1379ed24f4bSMarc Zyngier 
1389ed24f4bSMarc Zyngier 	/*
1399ed24f4bSMarc Zyngier 	 * If one or more VCPU matching target affinity are running
1409ed24f4bSMarc Zyngier 	 * then ON else OFF
1419ed24f4bSMarc Zyngier 	 */
1429ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, tmp, kvm) {
1439ed24f4bSMarc Zyngier 		mpidr = kvm_vcpu_get_mpidr_aff(tmp);
1449ed24f4bSMarc Zyngier 		if ((mpidr & target_affinity_mask) == target_affinity) {
1459ed24f4bSMarc Zyngier 			matching_cpus++;
146*b171f9bbSOliver Upton 			if (!kvm_arm_vcpu_stopped(tmp))
1479ed24f4bSMarc Zyngier 				return PSCI_0_2_AFFINITY_LEVEL_ON;
1489ed24f4bSMarc Zyngier 		}
1499ed24f4bSMarc Zyngier 	}
1509ed24f4bSMarc Zyngier 
1519ed24f4bSMarc Zyngier 	if (!matching_cpus)
1529ed24f4bSMarc Zyngier 		return PSCI_RET_INVALID_PARAMS;
1539ed24f4bSMarc Zyngier 
1549ed24f4bSMarc Zyngier 	return PSCI_0_2_AFFINITY_LEVEL_OFF;
1559ed24f4bSMarc Zyngier }
1569ed24f4bSMarc Zyngier 
15734739fd9SWill Deacon static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
1589ed24f4bSMarc Zyngier {
15946808a4cSMarc Zyngier 	unsigned long i;
1609ed24f4bSMarc Zyngier 	struct kvm_vcpu *tmp;
1619ed24f4bSMarc Zyngier 
1629ed24f4bSMarc Zyngier 	/*
1639ed24f4bSMarc Zyngier 	 * The KVM ABI specifies that a system event exit may call KVM_RUN
1649ed24f4bSMarc Zyngier 	 * again and may perform shutdown/reboot at a later time that when the
1659ed24f4bSMarc Zyngier 	 * actual request is made.  Since we are implementing PSCI and a
1669ed24f4bSMarc Zyngier 	 * caller of PSCI reboot and shutdown expects that the system shuts
1679ed24f4bSMarc Zyngier 	 * down or reboots immediately, let's make sure that VCPUs are not run
1689ed24f4bSMarc Zyngier 	 * after this call is handled and before the VCPUs have been
1699ed24f4bSMarc Zyngier 	 * re-initialized.
1709ed24f4bSMarc Zyngier 	 */
1719ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, tmp, vcpu->kvm)
172*b171f9bbSOliver Upton 		tmp->arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
1739ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
1749ed24f4bSMarc Zyngier 
1759ed24f4bSMarc Zyngier 	memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
1769ed24f4bSMarc Zyngier 	vcpu->run->system_event.type = type;
177d495f942SPaolo Bonzini 	vcpu->run->system_event.ndata = 1;
178d495f942SPaolo Bonzini 	vcpu->run->system_event.data[0] = flags;
1799ed24f4bSMarc Zyngier 	vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
1809ed24f4bSMarc Zyngier }
1819ed24f4bSMarc Zyngier 
1829ed24f4bSMarc Zyngier static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
1839ed24f4bSMarc Zyngier {
18434739fd9SWill Deacon 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0);
1859ed24f4bSMarc Zyngier }
1869ed24f4bSMarc Zyngier 
1879ed24f4bSMarc Zyngier static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
1889ed24f4bSMarc Zyngier {
18934739fd9SWill Deacon 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0);
19034739fd9SWill Deacon }
19134739fd9SWill Deacon 
19234739fd9SWill Deacon static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu)
19334739fd9SWill Deacon {
19434739fd9SWill Deacon 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET,
19534739fd9SWill Deacon 				 KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2);
1969ed24f4bSMarc Zyngier }
1979ed24f4bSMarc Zyngier 
1989ed24f4bSMarc Zyngier static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu)
1999ed24f4bSMarc Zyngier {
2009ed24f4bSMarc Zyngier 	int i;
2019ed24f4bSMarc Zyngier 
2029ed24f4bSMarc Zyngier 	/*
2039ed24f4bSMarc Zyngier 	 * Zero the input registers' upper 32 bits. They will be fully
2049ed24f4bSMarc Zyngier 	 * zeroed on exit, so we're fine changing them in place.
2059ed24f4bSMarc Zyngier 	 */
2069ed24f4bSMarc Zyngier 	for (i = 1; i < 4; i++)
2079ed24f4bSMarc Zyngier 		vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i)));
2089ed24f4bSMarc Zyngier }
2099ed24f4bSMarc Zyngier 
2109ed24f4bSMarc Zyngier static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
2119ed24f4bSMarc Zyngier {
2122da0aebcSOliver Upton 	/*
2132da0aebcSOliver Upton 	 * Prevent 32 bit guests from calling 64 bit PSCI functions.
2142da0aebcSOliver Upton 	 */
2152da0aebcSOliver Upton 	if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
2169ed24f4bSMarc Zyngier 		return PSCI_RET_NOT_SUPPORTED;
2179ed24f4bSMarc Zyngier 
2189ed24f4bSMarc Zyngier 	return 0;
2199ed24f4bSMarc Zyngier }
2209ed24f4bSMarc Zyngier 
2219ed24f4bSMarc Zyngier static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
2229ed24f4bSMarc Zyngier {
2239ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
2249ed24f4bSMarc Zyngier 	u32 psci_fn = smccc_get_function(vcpu);
2259ed24f4bSMarc Zyngier 	unsigned long val;
2269ed24f4bSMarc Zyngier 	int ret = 1;
2279ed24f4bSMarc Zyngier 
2289ed24f4bSMarc Zyngier 	switch (psci_fn) {
2299ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_PSCI_VERSION:
2309ed24f4bSMarc Zyngier 		/*
2319ed24f4bSMarc Zyngier 		 * Bits[31:16] = Major Version = 0
2329ed24f4bSMarc Zyngier 		 * Bits[15:0] = Minor Version = 2
2339ed24f4bSMarc Zyngier 		 */
2349ed24f4bSMarc Zyngier 		val = KVM_ARM_PSCI_0_2;
2359ed24f4bSMarc Zyngier 		break;
2369ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_CPU_SUSPEND:
2379ed24f4bSMarc Zyngier 	case PSCI_0_2_FN64_CPU_SUSPEND:
2389ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_suspend(vcpu);
2399ed24f4bSMarc Zyngier 		break;
2409ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_CPU_OFF:
2411e579429SOliver Upton 		kvm_arm_vcpu_power_off(vcpu);
2429ed24f4bSMarc Zyngier 		val = PSCI_RET_SUCCESS;
2439ed24f4bSMarc Zyngier 		break;
2449ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_CPU_ON:
2459ed24f4bSMarc Zyngier 		kvm_psci_narrow_to_32bit(vcpu);
2469ed24f4bSMarc Zyngier 		fallthrough;
2479ed24f4bSMarc Zyngier 	case PSCI_0_2_FN64_CPU_ON:
2489ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
2499ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_on(vcpu);
2509ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
2519ed24f4bSMarc Zyngier 		break;
2529ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_AFFINITY_INFO:
2539ed24f4bSMarc Zyngier 		kvm_psci_narrow_to_32bit(vcpu);
2549ed24f4bSMarc Zyngier 		fallthrough;
2559ed24f4bSMarc Zyngier 	case PSCI_0_2_FN64_AFFINITY_INFO:
2569ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_affinity_info(vcpu);
2579ed24f4bSMarc Zyngier 		break;
2589ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
2599ed24f4bSMarc Zyngier 		/*
2609ed24f4bSMarc Zyngier 		 * Trusted OS is MP hence does not require migration
2619ed24f4bSMarc Zyngier 	         * or
2629ed24f4bSMarc Zyngier 		 * Trusted OS is not present
2639ed24f4bSMarc Zyngier 		 */
2649ed24f4bSMarc Zyngier 		val = PSCI_0_2_TOS_MP;
2659ed24f4bSMarc Zyngier 		break;
2669ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_SYSTEM_OFF:
2679ed24f4bSMarc Zyngier 		kvm_psci_system_off(vcpu);
2689ed24f4bSMarc Zyngier 		/*
269656012c7SFuad Tabba 		 * We shouldn't be going back to guest VCPU after
2709ed24f4bSMarc Zyngier 		 * receiving SYSTEM_OFF request.
2719ed24f4bSMarc Zyngier 		 *
272656012c7SFuad Tabba 		 * If user space accidentally/deliberately resumes
2739ed24f4bSMarc Zyngier 		 * guest VCPU after SYSTEM_OFF request then guest
2749ed24f4bSMarc Zyngier 		 * VCPU should see internal failure from PSCI return
2759ed24f4bSMarc Zyngier 		 * value. To achieve this, we preload r0 (or x0) with
2769ed24f4bSMarc Zyngier 		 * PSCI return value INTERNAL_FAILURE.
2779ed24f4bSMarc Zyngier 		 */
2789ed24f4bSMarc Zyngier 		val = PSCI_RET_INTERNAL_FAILURE;
2799ed24f4bSMarc Zyngier 		ret = 0;
2809ed24f4bSMarc Zyngier 		break;
2819ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_SYSTEM_RESET:
2829ed24f4bSMarc Zyngier 		kvm_psci_system_reset(vcpu);
2839ed24f4bSMarc Zyngier 		/*
2849ed24f4bSMarc Zyngier 		 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
2859ed24f4bSMarc Zyngier 		 * with PSCI return value INTERNAL_FAILURE.
2869ed24f4bSMarc Zyngier 		 */
2879ed24f4bSMarc Zyngier 		val = PSCI_RET_INTERNAL_FAILURE;
2889ed24f4bSMarc Zyngier 		ret = 0;
2899ed24f4bSMarc Zyngier 		break;
2909ed24f4bSMarc Zyngier 	default:
2919ed24f4bSMarc Zyngier 		val = PSCI_RET_NOT_SUPPORTED;
2929ed24f4bSMarc Zyngier 		break;
2939ed24f4bSMarc Zyngier 	}
2949ed24f4bSMarc Zyngier 
2959ed24f4bSMarc Zyngier 	smccc_set_retval(vcpu, val, 0, 0, 0);
2969ed24f4bSMarc Zyngier 	return ret;
2979ed24f4bSMarc Zyngier }
2989ed24f4bSMarc Zyngier 
299512865d8SWill Deacon static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
3009ed24f4bSMarc Zyngier {
3015bc2cb95SOliver Upton 	unsigned long val = PSCI_RET_NOT_SUPPORTED;
3029ed24f4bSMarc Zyngier 	u32 psci_fn = smccc_get_function(vcpu);
303d43583b8SWill Deacon 	u32 arg;
3049ed24f4bSMarc Zyngier 	int ret = 1;
3059ed24f4bSMarc Zyngier 
3069ed24f4bSMarc Zyngier 	switch(psci_fn) {
3079ed24f4bSMarc Zyngier 	case PSCI_0_2_FN_PSCI_VERSION:
308512865d8SWill Deacon 		val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1;
3099ed24f4bSMarc Zyngier 		break;
3109ed24f4bSMarc Zyngier 	case PSCI_1_0_FN_PSCI_FEATURES:
311d43583b8SWill Deacon 		arg = smccc_get_arg1(vcpu);
312d43583b8SWill Deacon 		val = kvm_psci_check_allowed_function(vcpu, arg);
3139ed24f4bSMarc Zyngier 		if (val)
3149ed24f4bSMarc Zyngier 			break;
3159ed24f4bSMarc Zyngier 
3165bc2cb95SOliver Upton 		val = PSCI_RET_NOT_SUPPORTED;
3175bc2cb95SOliver Upton 
318d43583b8SWill Deacon 		switch(arg) {
3199ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_PSCI_VERSION:
3209ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_CPU_SUSPEND:
3219ed24f4bSMarc Zyngier 		case PSCI_0_2_FN64_CPU_SUSPEND:
3229ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_CPU_OFF:
3239ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_CPU_ON:
3249ed24f4bSMarc Zyngier 		case PSCI_0_2_FN64_CPU_ON:
3259ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_AFFINITY_INFO:
3269ed24f4bSMarc Zyngier 		case PSCI_0_2_FN64_AFFINITY_INFO:
3279ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
3289ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_SYSTEM_OFF:
3299ed24f4bSMarc Zyngier 		case PSCI_0_2_FN_SYSTEM_RESET:
3309ed24f4bSMarc Zyngier 		case PSCI_1_0_FN_PSCI_FEATURES:
3319ed24f4bSMarc Zyngier 		case ARM_SMCCC_VERSION_FUNC_ID:
3329ed24f4bSMarc Zyngier 			val = 0;
3339ed24f4bSMarc Zyngier 			break;
334d43583b8SWill Deacon 		case PSCI_1_1_FN_SYSTEM_RESET2:
335d43583b8SWill Deacon 		case PSCI_1_1_FN64_SYSTEM_RESET2:
3365bc2cb95SOliver Upton 			if (minor >= 1)
337d43583b8SWill Deacon 				val = 0;
338d43583b8SWill Deacon 			break;
339d43583b8SWill Deacon 		}
3409ed24f4bSMarc Zyngier 		break;
341d43583b8SWill Deacon 	case PSCI_1_1_FN_SYSTEM_RESET2:
342d43583b8SWill Deacon 		kvm_psci_narrow_to_32bit(vcpu);
343d43583b8SWill Deacon 		fallthrough;
344d43583b8SWill Deacon 	case PSCI_1_1_FN64_SYSTEM_RESET2:
345d43583b8SWill Deacon 		if (minor >= 1) {
346d43583b8SWill Deacon 			arg = smccc_get_arg1(vcpu);
347d43583b8SWill Deacon 
3489d3e7b7cSWill Deacon 			if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET ||
3499d3e7b7cSWill Deacon 			    arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) {
35034739fd9SWill Deacon 				kvm_psci_system_reset2(vcpu);
3519d3e7b7cSWill Deacon 				vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE);
3529d3e7b7cSWill Deacon 				return 0;
353d43583b8SWill Deacon 			}
3549d3e7b7cSWill Deacon 
3559d3e7b7cSWill Deacon 			val = PSCI_RET_INVALID_PARAMS;
356d43583b8SWill Deacon 			break;
357ae82047eSChangcheng Deng 		}
3585bc2cb95SOliver Upton 		break;
3599ed24f4bSMarc Zyngier 	default:
3609ed24f4bSMarc Zyngier 		return kvm_psci_0_2_call(vcpu);
3619ed24f4bSMarc Zyngier 	}
3629ed24f4bSMarc Zyngier 
3639ed24f4bSMarc Zyngier 	smccc_set_retval(vcpu, val, 0, 0, 0);
3649ed24f4bSMarc Zyngier 	return ret;
3659ed24f4bSMarc Zyngier }
3669ed24f4bSMarc Zyngier 
3679ed24f4bSMarc Zyngier static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
3689ed24f4bSMarc Zyngier {
3699ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
3709ed24f4bSMarc Zyngier 	u32 psci_fn = smccc_get_function(vcpu);
3719ed24f4bSMarc Zyngier 	unsigned long val;
3729ed24f4bSMarc Zyngier 
3739ed24f4bSMarc Zyngier 	switch (psci_fn) {
3749ed24f4bSMarc Zyngier 	case KVM_PSCI_FN_CPU_OFF:
3751e579429SOliver Upton 		kvm_arm_vcpu_power_off(vcpu);
3769ed24f4bSMarc Zyngier 		val = PSCI_RET_SUCCESS;
3779ed24f4bSMarc Zyngier 		break;
3789ed24f4bSMarc Zyngier 	case KVM_PSCI_FN_CPU_ON:
3799ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
3809ed24f4bSMarc Zyngier 		val = kvm_psci_vcpu_on(vcpu);
3819ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
3829ed24f4bSMarc Zyngier 		break;
3839ed24f4bSMarc Zyngier 	default:
3849ed24f4bSMarc Zyngier 		val = PSCI_RET_NOT_SUPPORTED;
3859ed24f4bSMarc Zyngier 		break;
3869ed24f4bSMarc Zyngier 	}
3879ed24f4bSMarc Zyngier 
3889ed24f4bSMarc Zyngier 	smccc_set_retval(vcpu, val, 0, 0, 0);
3899ed24f4bSMarc Zyngier 	return 1;
3909ed24f4bSMarc Zyngier }
3919ed24f4bSMarc Zyngier 
3929ed24f4bSMarc Zyngier /**
3939ed24f4bSMarc Zyngier  * kvm_psci_call - handle PSCI call if r0 value is in range
3949ed24f4bSMarc Zyngier  * @vcpu: Pointer to the VCPU struct
3959ed24f4bSMarc Zyngier  *
3969ed24f4bSMarc Zyngier  * Handle PSCI calls from guests through traps from HVC instructions.
3979ed24f4bSMarc Zyngier  * The calling convention is similar to SMC calls to the secure world
3989ed24f4bSMarc Zyngier  * where the function number is placed in r0.
3999ed24f4bSMarc Zyngier  *
4009ed24f4bSMarc Zyngier  * This function returns: > 0 (success), 0 (success but exit to user
4019ed24f4bSMarc Zyngier  * space), and < 0 (errors)
4029ed24f4bSMarc Zyngier  *
4039ed24f4bSMarc Zyngier  * Errors:
4049ed24f4bSMarc Zyngier  * -EINVAL: Unrecognized PSCI function
4059ed24f4bSMarc Zyngier  */
4069ed24f4bSMarc Zyngier int kvm_psci_call(struct kvm_vcpu *vcpu)
4079ed24f4bSMarc Zyngier {
408827c2ab3SOliver Upton 	u32 psci_fn = smccc_get_function(vcpu);
409827c2ab3SOliver Upton 	unsigned long val;
410827c2ab3SOliver Upton 
411827c2ab3SOliver Upton 	val = kvm_psci_check_allowed_function(vcpu, psci_fn);
412827c2ab3SOliver Upton 	if (val) {
413827c2ab3SOliver Upton 		smccc_set_retval(vcpu, val, 0, 0, 0);
414827c2ab3SOliver Upton 		return 1;
415827c2ab3SOliver Upton 	}
416827c2ab3SOliver Upton 
417dfefa04aSOliver Upton 	switch (kvm_psci_version(vcpu)) {
418512865d8SWill Deacon 	case KVM_ARM_PSCI_1_1:
419512865d8SWill Deacon 		return kvm_psci_1_x_call(vcpu, 1);
4209ed24f4bSMarc Zyngier 	case KVM_ARM_PSCI_1_0:
421512865d8SWill Deacon 		return kvm_psci_1_x_call(vcpu, 0);
4229ed24f4bSMarc Zyngier 	case KVM_ARM_PSCI_0_2:
4239ed24f4bSMarc Zyngier 		return kvm_psci_0_2_call(vcpu);
4249ed24f4bSMarc Zyngier 	case KVM_ARM_PSCI_0_1:
4259ed24f4bSMarc Zyngier 		return kvm_psci_0_1_call(vcpu);
4269ed24f4bSMarc Zyngier 	default:
4279ed24f4bSMarc Zyngier 		return -EINVAL;
428ae82047eSChangcheng Deng 	}
4299ed24f4bSMarc Zyngier }
4309ed24f4bSMarc Zyngier 
4319ed24f4bSMarc Zyngier int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
4329ed24f4bSMarc Zyngier {
433a5905d6aSJames Morse 	return 4;		/* PSCI version and three workaround registers */
4349ed24f4bSMarc Zyngier }
4359ed24f4bSMarc Zyngier 
4369ed24f4bSMarc Zyngier int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
4379ed24f4bSMarc Zyngier {
4389ed24f4bSMarc Zyngier 	if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++))
4399ed24f4bSMarc Zyngier 		return -EFAULT;
4409ed24f4bSMarc Zyngier 
4419ed24f4bSMarc Zyngier 	if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++))
4429ed24f4bSMarc Zyngier 		return -EFAULT;
4439ed24f4bSMarc Zyngier 
4449ed24f4bSMarc Zyngier 	if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++))
4459ed24f4bSMarc Zyngier 		return -EFAULT;
4469ed24f4bSMarc Zyngier 
447a5905d6aSJames Morse 	if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3, uindices++))
448a5905d6aSJames Morse 		return -EFAULT;
449a5905d6aSJames Morse 
4509ed24f4bSMarc Zyngier 	return 0;
4519ed24f4bSMarc Zyngier }
4529ed24f4bSMarc Zyngier 
4539ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_WIDTH	4
4549ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_MASK	(BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
4559ed24f4bSMarc Zyngier 
4569ed24f4bSMarc Zyngier /*
4579ed24f4bSMarc Zyngier  * Convert the workaround level into an easy-to-compare number, where higher
4589ed24f4bSMarc Zyngier  * values mean better protection.
4599ed24f4bSMarc Zyngier  */
4609ed24f4bSMarc Zyngier static int get_kernel_wa_level(u64 regid)
4619ed24f4bSMarc Zyngier {
4629ed24f4bSMarc Zyngier 	switch (regid) {
4639ed24f4bSMarc Zyngier 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
464d4647f0aSWill Deacon 		switch (arm64_get_spectre_v2_state()) {
465d4647f0aSWill Deacon 		case SPECTRE_VULNERABLE:
4669ed24f4bSMarc Zyngier 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
467d4647f0aSWill Deacon 		case SPECTRE_MITIGATED:
4689ed24f4bSMarc Zyngier 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
469d4647f0aSWill Deacon 		case SPECTRE_UNAFFECTED:
4709ed24f4bSMarc Zyngier 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED;
4719ed24f4bSMarc Zyngier 		}
4729ed24f4bSMarc Zyngier 		return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
4739ed24f4bSMarc Zyngier 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
474d63d975aSMarc Zyngier 		switch (arm64_get_spectre_v4_state()) {
475d63d975aSMarc Zyngier 		case SPECTRE_MITIGATED:
476d63d975aSMarc Zyngier 			/*
477d63d975aSMarc Zyngier 			 * As for the hypercall discovery, we pretend we
478d63d975aSMarc Zyngier 			 * don't have any FW mitigation if SSBS is there at
479d63d975aSMarc Zyngier 			 * all times.
480d63d975aSMarc Zyngier 			 */
481d63d975aSMarc Zyngier 			if (cpus_have_final_cap(ARM64_SSBS))
482d63d975aSMarc Zyngier 				return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
483d63d975aSMarc Zyngier 			fallthrough;
484d63d975aSMarc Zyngier 		case SPECTRE_UNAFFECTED:
4859ed24f4bSMarc Zyngier 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
486d63d975aSMarc Zyngier 		case SPECTRE_VULNERABLE:
48729e8910aSMarc Zyngier 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
4889ed24f4bSMarc Zyngier 		}
489a5905d6aSJames Morse 		break;
490a5905d6aSJames Morse 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3:
491a5905d6aSJames Morse 		switch (arm64_get_spectre_bhb_state()) {
492a5905d6aSJames Morse 		case SPECTRE_VULNERABLE:
493a5905d6aSJames Morse 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL;
494a5905d6aSJames Morse 		case SPECTRE_MITIGATED:
495a5905d6aSJames Morse 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_AVAIL;
496a5905d6aSJames Morse 		case SPECTRE_UNAFFECTED:
497a5905d6aSJames Morse 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_REQUIRED;
498a5905d6aSJames Morse 		}
499a5905d6aSJames Morse 		return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL;
5009ed24f4bSMarc Zyngier 	}
5019ed24f4bSMarc Zyngier 
5029ed24f4bSMarc Zyngier 	return -EINVAL;
5039ed24f4bSMarc Zyngier }
5049ed24f4bSMarc Zyngier 
5059ed24f4bSMarc Zyngier int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
5069ed24f4bSMarc Zyngier {
5079ed24f4bSMarc Zyngier 	void __user *uaddr = (void __user *)(long)reg->addr;
5089ed24f4bSMarc Zyngier 	u64 val;
5099ed24f4bSMarc Zyngier 
5109ed24f4bSMarc Zyngier 	switch (reg->id) {
5119ed24f4bSMarc Zyngier 	case KVM_REG_ARM_PSCI_VERSION:
512dfefa04aSOliver Upton 		val = kvm_psci_version(vcpu);
5139ed24f4bSMarc Zyngier 		break;
5149ed24f4bSMarc Zyngier 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
5159ed24f4bSMarc Zyngier 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
516a5905d6aSJames Morse 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3:
5179ed24f4bSMarc Zyngier 		val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
5189ed24f4bSMarc Zyngier 		break;
5199ed24f4bSMarc Zyngier 	default:
5209ed24f4bSMarc Zyngier 		return -ENOENT;
5219ed24f4bSMarc Zyngier 	}
5229ed24f4bSMarc Zyngier 
5239ed24f4bSMarc Zyngier 	if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
5249ed24f4bSMarc Zyngier 		return -EFAULT;
5259ed24f4bSMarc Zyngier 
5269ed24f4bSMarc Zyngier 	return 0;
5279ed24f4bSMarc Zyngier }
5289ed24f4bSMarc Zyngier 
5299ed24f4bSMarc Zyngier int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
5309ed24f4bSMarc Zyngier {
5319ed24f4bSMarc Zyngier 	void __user *uaddr = (void __user *)(long)reg->addr;
5329ed24f4bSMarc Zyngier 	u64 val;
5339ed24f4bSMarc Zyngier 	int wa_level;
5349ed24f4bSMarc Zyngier 
5359ed24f4bSMarc Zyngier 	if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
5369ed24f4bSMarc Zyngier 		return -EFAULT;
5379ed24f4bSMarc Zyngier 
5389ed24f4bSMarc Zyngier 	switch (reg->id) {
5399ed24f4bSMarc Zyngier 	case KVM_REG_ARM_PSCI_VERSION:
5409ed24f4bSMarc Zyngier 	{
5419ed24f4bSMarc Zyngier 		bool wants_02;
5429ed24f4bSMarc Zyngier 
5439ed24f4bSMarc Zyngier 		wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
5449ed24f4bSMarc Zyngier 
5459ed24f4bSMarc Zyngier 		switch (val) {
5469ed24f4bSMarc Zyngier 		case KVM_ARM_PSCI_0_1:
5479ed24f4bSMarc Zyngier 			if (wants_02)
5489ed24f4bSMarc Zyngier 				return -EINVAL;
5499ed24f4bSMarc Zyngier 			vcpu->kvm->arch.psci_version = val;
5509ed24f4bSMarc Zyngier 			return 0;
5519ed24f4bSMarc Zyngier 		case KVM_ARM_PSCI_0_2:
5529ed24f4bSMarc Zyngier 		case KVM_ARM_PSCI_1_0:
553512865d8SWill Deacon 		case KVM_ARM_PSCI_1_1:
5549ed24f4bSMarc Zyngier 			if (!wants_02)
5559ed24f4bSMarc Zyngier 				return -EINVAL;
5569ed24f4bSMarc Zyngier 			vcpu->kvm->arch.psci_version = val;
5579ed24f4bSMarc Zyngier 			return 0;
5589ed24f4bSMarc Zyngier 		}
5599ed24f4bSMarc Zyngier 		break;
5609ed24f4bSMarc Zyngier 	}
5619ed24f4bSMarc Zyngier 
5629ed24f4bSMarc Zyngier 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
563a5905d6aSJames Morse 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3:
5649ed24f4bSMarc Zyngier 		if (val & ~KVM_REG_FEATURE_LEVEL_MASK)
5659ed24f4bSMarc Zyngier 			return -EINVAL;
5669ed24f4bSMarc Zyngier 
5679ed24f4bSMarc Zyngier 		if (get_kernel_wa_level(reg->id) < val)
5689ed24f4bSMarc Zyngier 			return -EINVAL;
5699ed24f4bSMarc Zyngier 
5709ed24f4bSMarc Zyngier 		return 0;
5719ed24f4bSMarc Zyngier 
5729ed24f4bSMarc Zyngier 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
5739ed24f4bSMarc Zyngier 		if (val & ~(KVM_REG_FEATURE_LEVEL_MASK |
5749ed24f4bSMarc Zyngier 			    KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED))
5759ed24f4bSMarc Zyngier 			return -EINVAL;
5769ed24f4bSMarc Zyngier 
5779ed24f4bSMarc Zyngier 		/* The enabled bit must not be set unless the level is AVAIL. */
57829e8910aSMarc Zyngier 		if ((val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED) &&
57929e8910aSMarc Zyngier 		    (val & KVM_REG_FEATURE_LEVEL_MASK) != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL)
5809ed24f4bSMarc Zyngier 			return -EINVAL;
5819ed24f4bSMarc Zyngier 
5829ed24f4bSMarc Zyngier 		/*
58329e8910aSMarc Zyngier 		 * Map all the possible incoming states to the only two we
58429e8910aSMarc Zyngier 		 * really want to deal with.
5859ed24f4bSMarc Zyngier 		 */
58629e8910aSMarc Zyngier 		switch (val & KVM_REG_FEATURE_LEVEL_MASK) {
58729e8910aSMarc Zyngier 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL:
58829e8910aSMarc Zyngier 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN:
58929e8910aSMarc Zyngier 			wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
59029e8910aSMarc Zyngier 			break;
5919ed24f4bSMarc Zyngier 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
5929ed24f4bSMarc Zyngier 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
59329e8910aSMarc Zyngier 			wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
5949ed24f4bSMarc Zyngier 			break;
59529e8910aSMarc Zyngier 		default:
59629e8910aSMarc Zyngier 			return -EINVAL;
5979ed24f4bSMarc Zyngier 		}
5989ed24f4bSMarc Zyngier 
59929e8910aSMarc Zyngier 		/*
60029e8910aSMarc Zyngier 		 * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the
60129e8910aSMarc Zyngier 		 * other way around.
60229e8910aSMarc Zyngier 		 */
60329e8910aSMarc Zyngier 		if (get_kernel_wa_level(reg->id) < wa_level)
60429e8910aSMarc Zyngier 			return -EINVAL;
60529e8910aSMarc Zyngier 
6069ed24f4bSMarc Zyngier 		return 0;
6079ed24f4bSMarc Zyngier 	default:
6089ed24f4bSMarc Zyngier 		return -ENOENT;
6099ed24f4bSMarc Zyngier 	}
6109ed24f4bSMarc Zyngier 
6119ed24f4bSMarc Zyngier 	return -EINVAL;
6129ed24f4bSMarc Zyngier }
613