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 549ed24f4bSMarc Zyngier static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu) 559ed24f4bSMarc Zyngier { 569ed24f4bSMarc Zyngier vcpu->arch.power_off = true; 579ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_SLEEP, vcpu); 589ed24f4bSMarc Zyngier kvm_vcpu_kick(vcpu); 599ed24f4bSMarc Zyngier } 609ed24f4bSMarc Zyngier 61e10ecb4dSOliver Upton static inline bool kvm_psci_valid_affinity(struct kvm_vcpu *vcpu, 62e10ecb4dSOliver Upton unsigned long affinity) 63e10ecb4dSOliver Upton { 64e10ecb4dSOliver Upton return !(affinity & ~MPIDR_HWID_BITMASK); 65e10ecb4dSOliver Upton } 66e10ecb4dSOliver Upton 679ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) 689ed24f4bSMarc Zyngier { 699ed24f4bSMarc Zyngier struct vcpu_reset_state *reset_state; 709ed24f4bSMarc Zyngier struct kvm *kvm = source_vcpu->kvm; 719ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu = NULL; 729ed24f4bSMarc Zyngier unsigned long cpu_id; 739ed24f4bSMarc Zyngier 74e10ecb4dSOliver Upton cpu_id = smccc_get_arg1(source_vcpu); 75e10ecb4dSOliver Upton if (!kvm_psci_valid_affinity(source_vcpu, cpu_id)) 76e10ecb4dSOliver Upton return PSCI_RET_INVALID_PARAMS; 779ed24f4bSMarc Zyngier 789ed24f4bSMarc Zyngier vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id); 799ed24f4bSMarc Zyngier 809ed24f4bSMarc Zyngier /* 819ed24f4bSMarc Zyngier * Make sure the caller requested a valid CPU and that the CPU is 829ed24f4bSMarc Zyngier * turned off. 839ed24f4bSMarc Zyngier */ 849ed24f4bSMarc Zyngier if (!vcpu) 859ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 869ed24f4bSMarc Zyngier if (!vcpu->arch.power_off) { 87dfefa04aSOliver Upton if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1) 889ed24f4bSMarc Zyngier return PSCI_RET_ALREADY_ON; 899ed24f4bSMarc Zyngier else 909ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 919ed24f4bSMarc Zyngier } 929ed24f4bSMarc Zyngier 939ed24f4bSMarc Zyngier reset_state = &vcpu->arch.reset_state; 949ed24f4bSMarc Zyngier 959ed24f4bSMarc Zyngier reset_state->pc = smccc_get_arg2(source_vcpu); 969ed24f4bSMarc Zyngier 979ed24f4bSMarc Zyngier /* Propagate caller endianness */ 989ed24f4bSMarc Zyngier reset_state->be = kvm_vcpu_is_be(source_vcpu); 999ed24f4bSMarc Zyngier 1009ed24f4bSMarc Zyngier /* 1019ed24f4bSMarc Zyngier * NOTE: We always update r0 (or x0) because for PSCI v0.1 102656012c7SFuad Tabba * the general purpose registers are undefined upon CPU_ON. 1039ed24f4bSMarc Zyngier */ 1049ed24f4bSMarc Zyngier reset_state->r0 = smccc_get_arg3(source_vcpu); 1059ed24f4bSMarc Zyngier 1069ed24f4bSMarc Zyngier WRITE_ONCE(reset_state->reset, true); 1079ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); 1089ed24f4bSMarc Zyngier 1099ed24f4bSMarc Zyngier /* 1109ed24f4bSMarc Zyngier * Make sure the reset request is observed if the change to 111dda0190dSFuad Tabba * power_off is observed. 1129ed24f4bSMarc Zyngier */ 1139ed24f4bSMarc Zyngier smp_wmb(); 1149ed24f4bSMarc Zyngier 1159ed24f4bSMarc Zyngier vcpu->arch.power_off = false; 1169ed24f4bSMarc Zyngier kvm_vcpu_wake_up(vcpu); 1179ed24f4bSMarc Zyngier 1189ed24f4bSMarc Zyngier return PSCI_RET_SUCCESS; 1199ed24f4bSMarc Zyngier } 1209ed24f4bSMarc Zyngier 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++; 1539ed24f4bSMarc Zyngier if (!tmp->arch.power_off) 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 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 */ 1789ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, tmp, vcpu->kvm) 1799ed24f4bSMarc Zyngier tmp->arch.power_off = true; 1809ed24f4bSMarc Zyngier kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP); 1819ed24f4bSMarc Zyngier 1829ed24f4bSMarc Zyngier memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); 1839ed24f4bSMarc Zyngier vcpu->run->system_event.type = type; 18434739fd9SWill Deacon vcpu->run->system_event.flags = flags; 1859ed24f4bSMarc Zyngier vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; 1869ed24f4bSMarc Zyngier } 1879ed24f4bSMarc Zyngier 1889ed24f4bSMarc Zyngier static void kvm_psci_system_off(struct kvm_vcpu *vcpu) 1899ed24f4bSMarc Zyngier { 19034739fd9SWill Deacon kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0); 1919ed24f4bSMarc Zyngier } 1929ed24f4bSMarc Zyngier 1939ed24f4bSMarc Zyngier static void kvm_psci_system_reset(struct kvm_vcpu *vcpu) 1949ed24f4bSMarc Zyngier { 19534739fd9SWill Deacon kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0); 19634739fd9SWill Deacon } 19734739fd9SWill Deacon 19834739fd9SWill Deacon static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu) 19934739fd9SWill Deacon { 20034739fd9SWill Deacon kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 20134739fd9SWill Deacon KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2); 2029ed24f4bSMarc Zyngier } 2039ed24f4bSMarc Zyngier 2049ed24f4bSMarc Zyngier static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu) 2059ed24f4bSMarc Zyngier { 2069ed24f4bSMarc Zyngier int i; 2079ed24f4bSMarc Zyngier 2089ed24f4bSMarc Zyngier /* 2099ed24f4bSMarc Zyngier * Zero the input registers' upper 32 bits. They will be fully 2109ed24f4bSMarc Zyngier * zeroed on exit, so we're fine changing them in place. 2119ed24f4bSMarc Zyngier */ 2129ed24f4bSMarc Zyngier for (i = 1; i < 4; i++) 2139ed24f4bSMarc Zyngier vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i))); 2149ed24f4bSMarc Zyngier } 2159ed24f4bSMarc Zyngier 2169ed24f4bSMarc Zyngier static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn) 2179ed24f4bSMarc Zyngier { 218*2da0aebcSOliver Upton /* 219*2da0aebcSOliver Upton * Prevent 32 bit guests from calling 64 bit PSCI functions. 220*2da0aebcSOliver Upton */ 221*2da0aebcSOliver Upton if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu)) 2229ed24f4bSMarc Zyngier return PSCI_RET_NOT_SUPPORTED; 2239ed24f4bSMarc Zyngier 2249ed24f4bSMarc Zyngier return 0; 2259ed24f4bSMarc Zyngier } 2269ed24f4bSMarc Zyngier 2279ed24f4bSMarc Zyngier static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) 2289ed24f4bSMarc Zyngier { 2299ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 2309ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 2319ed24f4bSMarc Zyngier unsigned long val; 2329ed24f4bSMarc Zyngier int ret = 1; 2339ed24f4bSMarc Zyngier 2349ed24f4bSMarc Zyngier val = kvm_psci_check_allowed_function(vcpu, psci_fn); 2359ed24f4bSMarc Zyngier if (val) 2369ed24f4bSMarc Zyngier goto out; 2379ed24f4bSMarc Zyngier 2389ed24f4bSMarc Zyngier switch (psci_fn) { 2399ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 2409ed24f4bSMarc Zyngier /* 2419ed24f4bSMarc Zyngier * Bits[31:16] = Major Version = 0 2429ed24f4bSMarc Zyngier * Bits[15:0] = Minor Version = 2 2439ed24f4bSMarc Zyngier */ 2449ed24f4bSMarc Zyngier val = KVM_ARM_PSCI_0_2; 2459ed24f4bSMarc Zyngier break; 2469ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_SUSPEND: 2479ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 2489ed24f4bSMarc Zyngier val = kvm_psci_vcpu_suspend(vcpu); 2499ed24f4bSMarc Zyngier break; 2509ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_OFF: 2519ed24f4bSMarc Zyngier kvm_psci_vcpu_off(vcpu); 2529ed24f4bSMarc Zyngier val = PSCI_RET_SUCCESS; 2539ed24f4bSMarc Zyngier break; 2549ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_ON: 2559ed24f4bSMarc Zyngier kvm_psci_narrow_to_32bit(vcpu); 2569ed24f4bSMarc Zyngier fallthrough; 2579ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 2589ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 2599ed24f4bSMarc Zyngier val = kvm_psci_vcpu_on(vcpu); 2609ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 2619ed24f4bSMarc Zyngier break; 2629ed24f4bSMarc Zyngier case PSCI_0_2_FN_AFFINITY_INFO: 2639ed24f4bSMarc Zyngier kvm_psci_narrow_to_32bit(vcpu); 2649ed24f4bSMarc Zyngier fallthrough; 2659ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 2669ed24f4bSMarc Zyngier val = kvm_psci_vcpu_affinity_info(vcpu); 2679ed24f4bSMarc Zyngier break; 2689ed24f4bSMarc Zyngier case PSCI_0_2_FN_MIGRATE_INFO_TYPE: 2699ed24f4bSMarc Zyngier /* 2709ed24f4bSMarc Zyngier * Trusted OS is MP hence does not require migration 2719ed24f4bSMarc Zyngier * or 2729ed24f4bSMarc Zyngier * Trusted OS is not present 2739ed24f4bSMarc Zyngier */ 2749ed24f4bSMarc Zyngier val = PSCI_0_2_TOS_MP; 2759ed24f4bSMarc Zyngier break; 2769ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_OFF: 2779ed24f4bSMarc Zyngier kvm_psci_system_off(vcpu); 2789ed24f4bSMarc Zyngier /* 279656012c7SFuad Tabba * We shouldn't be going back to guest VCPU after 2809ed24f4bSMarc Zyngier * receiving SYSTEM_OFF request. 2819ed24f4bSMarc Zyngier * 282656012c7SFuad Tabba * If user space accidentally/deliberately resumes 2839ed24f4bSMarc Zyngier * guest VCPU after SYSTEM_OFF request then guest 2849ed24f4bSMarc Zyngier * VCPU should see internal failure from PSCI return 2859ed24f4bSMarc Zyngier * value. To achieve this, we preload r0 (or x0) with 2869ed24f4bSMarc Zyngier * PSCI return value INTERNAL_FAILURE. 2879ed24f4bSMarc Zyngier */ 2889ed24f4bSMarc Zyngier val = PSCI_RET_INTERNAL_FAILURE; 2899ed24f4bSMarc Zyngier ret = 0; 2909ed24f4bSMarc Zyngier break; 2919ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_RESET: 2929ed24f4bSMarc Zyngier kvm_psci_system_reset(vcpu); 2939ed24f4bSMarc Zyngier /* 2949ed24f4bSMarc Zyngier * Same reason as SYSTEM_OFF for preloading r0 (or x0) 2959ed24f4bSMarc Zyngier * with PSCI return value INTERNAL_FAILURE. 2969ed24f4bSMarc Zyngier */ 2979ed24f4bSMarc Zyngier val = PSCI_RET_INTERNAL_FAILURE; 2989ed24f4bSMarc Zyngier ret = 0; 2999ed24f4bSMarc Zyngier break; 3009ed24f4bSMarc Zyngier default: 3019ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3029ed24f4bSMarc Zyngier break; 3039ed24f4bSMarc Zyngier } 3049ed24f4bSMarc Zyngier 3059ed24f4bSMarc Zyngier out: 3069ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3079ed24f4bSMarc Zyngier return ret; 3089ed24f4bSMarc Zyngier } 3099ed24f4bSMarc Zyngier 310512865d8SWill Deacon static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor) 3119ed24f4bSMarc Zyngier { 3129ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 313d43583b8SWill Deacon u32 arg; 3149ed24f4bSMarc Zyngier unsigned long val; 3159ed24f4bSMarc Zyngier int ret = 1; 3169ed24f4bSMarc Zyngier 317512865d8SWill Deacon if (minor > 1) 318512865d8SWill Deacon return -EINVAL; 319512865d8SWill Deacon 3209ed24f4bSMarc Zyngier switch(psci_fn) { 3219ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 322512865d8SWill Deacon val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1; 3239ed24f4bSMarc Zyngier break; 3249ed24f4bSMarc Zyngier case PSCI_1_0_FN_PSCI_FEATURES: 325d43583b8SWill Deacon arg = smccc_get_arg1(vcpu); 326d43583b8SWill Deacon val = kvm_psci_check_allowed_function(vcpu, arg); 3279ed24f4bSMarc Zyngier if (val) 3289ed24f4bSMarc Zyngier break; 3299ed24f4bSMarc Zyngier 330d43583b8SWill Deacon switch(arg) { 3319ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 3329ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_SUSPEND: 3339ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 3349ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_OFF: 3359ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_ON: 3369ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 3379ed24f4bSMarc Zyngier case PSCI_0_2_FN_AFFINITY_INFO: 3389ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 3399ed24f4bSMarc Zyngier case PSCI_0_2_FN_MIGRATE_INFO_TYPE: 3409ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_OFF: 3419ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_RESET: 3429ed24f4bSMarc Zyngier case PSCI_1_0_FN_PSCI_FEATURES: 3439ed24f4bSMarc Zyngier case ARM_SMCCC_VERSION_FUNC_ID: 3449ed24f4bSMarc Zyngier val = 0; 3459ed24f4bSMarc Zyngier break; 346d43583b8SWill Deacon case PSCI_1_1_FN_SYSTEM_RESET2: 347d43583b8SWill Deacon case PSCI_1_1_FN64_SYSTEM_RESET2: 348d43583b8SWill Deacon if (minor >= 1) { 349d43583b8SWill Deacon val = 0; 350d43583b8SWill Deacon break; 351d43583b8SWill Deacon } 352d43583b8SWill Deacon fallthrough; 3539ed24f4bSMarc Zyngier default: 3549ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3559ed24f4bSMarc Zyngier break; 3569ed24f4bSMarc Zyngier } 3579ed24f4bSMarc Zyngier break; 358d43583b8SWill Deacon case PSCI_1_1_FN_SYSTEM_RESET2: 359d43583b8SWill Deacon kvm_psci_narrow_to_32bit(vcpu); 360d43583b8SWill Deacon fallthrough; 361d43583b8SWill Deacon case PSCI_1_1_FN64_SYSTEM_RESET2: 362d43583b8SWill Deacon if (minor >= 1) { 363d43583b8SWill Deacon arg = smccc_get_arg1(vcpu); 364d43583b8SWill Deacon 3659d3e7b7cSWill Deacon if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET || 3669d3e7b7cSWill Deacon arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) { 36734739fd9SWill Deacon kvm_psci_system_reset2(vcpu); 3689d3e7b7cSWill Deacon vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE); 3699d3e7b7cSWill Deacon return 0; 370d43583b8SWill Deacon } 3719d3e7b7cSWill Deacon 3729d3e7b7cSWill Deacon val = PSCI_RET_INVALID_PARAMS; 373d43583b8SWill Deacon break; 374ae82047eSChangcheng Deng } 375d43583b8SWill Deacon fallthrough; 3769ed24f4bSMarc Zyngier default: 3779ed24f4bSMarc Zyngier return kvm_psci_0_2_call(vcpu); 3789ed24f4bSMarc Zyngier } 3799ed24f4bSMarc Zyngier 3809ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3819ed24f4bSMarc Zyngier return ret; 3829ed24f4bSMarc Zyngier } 3839ed24f4bSMarc Zyngier 3849ed24f4bSMarc Zyngier static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) 3859ed24f4bSMarc Zyngier { 3869ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 3879ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 3889ed24f4bSMarc Zyngier unsigned long val; 3899ed24f4bSMarc Zyngier 3909ed24f4bSMarc Zyngier switch (psci_fn) { 3919ed24f4bSMarc Zyngier case KVM_PSCI_FN_CPU_OFF: 3929ed24f4bSMarc Zyngier kvm_psci_vcpu_off(vcpu); 3939ed24f4bSMarc Zyngier val = PSCI_RET_SUCCESS; 3949ed24f4bSMarc Zyngier break; 3959ed24f4bSMarc Zyngier case KVM_PSCI_FN_CPU_ON: 3969ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 3979ed24f4bSMarc Zyngier val = kvm_psci_vcpu_on(vcpu); 3989ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 3999ed24f4bSMarc Zyngier break; 4009ed24f4bSMarc Zyngier default: 4019ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 4029ed24f4bSMarc Zyngier break; 4039ed24f4bSMarc Zyngier } 4049ed24f4bSMarc Zyngier 4059ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 4069ed24f4bSMarc Zyngier return 1; 4079ed24f4bSMarc Zyngier } 4089ed24f4bSMarc Zyngier 4099ed24f4bSMarc Zyngier /** 4109ed24f4bSMarc Zyngier * kvm_psci_call - handle PSCI call if r0 value is in range 4119ed24f4bSMarc Zyngier * @vcpu: Pointer to the VCPU struct 4129ed24f4bSMarc Zyngier * 4139ed24f4bSMarc Zyngier * Handle PSCI calls from guests through traps from HVC instructions. 4149ed24f4bSMarc Zyngier * The calling convention is similar to SMC calls to the secure world 4159ed24f4bSMarc Zyngier * where the function number is placed in r0. 4169ed24f4bSMarc Zyngier * 4179ed24f4bSMarc Zyngier * This function returns: > 0 (success), 0 (success but exit to user 4189ed24f4bSMarc Zyngier * space), and < 0 (errors) 4199ed24f4bSMarc Zyngier * 4209ed24f4bSMarc Zyngier * Errors: 4219ed24f4bSMarc Zyngier * -EINVAL: Unrecognized PSCI function 4229ed24f4bSMarc Zyngier */ 4239ed24f4bSMarc Zyngier int kvm_psci_call(struct kvm_vcpu *vcpu) 4249ed24f4bSMarc Zyngier { 425dfefa04aSOliver Upton switch (kvm_psci_version(vcpu)) { 426512865d8SWill Deacon case KVM_ARM_PSCI_1_1: 427512865d8SWill Deacon return kvm_psci_1_x_call(vcpu, 1); 4289ed24f4bSMarc Zyngier case KVM_ARM_PSCI_1_0: 429512865d8SWill Deacon return kvm_psci_1_x_call(vcpu, 0); 4309ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_2: 4319ed24f4bSMarc Zyngier return kvm_psci_0_2_call(vcpu); 4329ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_1: 4339ed24f4bSMarc Zyngier return kvm_psci_0_1_call(vcpu); 4349ed24f4bSMarc Zyngier default: 4359ed24f4bSMarc Zyngier return -EINVAL; 436ae82047eSChangcheng Deng } 4379ed24f4bSMarc Zyngier } 4389ed24f4bSMarc Zyngier 4399ed24f4bSMarc Zyngier int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu) 4409ed24f4bSMarc Zyngier { 441a5905d6aSJames Morse return 4; /* PSCI version and three workaround registers */ 4429ed24f4bSMarc Zyngier } 4439ed24f4bSMarc Zyngier 4449ed24f4bSMarc Zyngier int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) 4459ed24f4bSMarc Zyngier { 4469ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++)) 4479ed24f4bSMarc Zyngier return -EFAULT; 4489ed24f4bSMarc Zyngier 4499ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++)) 4509ed24f4bSMarc Zyngier return -EFAULT; 4519ed24f4bSMarc Zyngier 4529ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++)) 4539ed24f4bSMarc Zyngier return -EFAULT; 4549ed24f4bSMarc Zyngier 455a5905d6aSJames Morse if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3, uindices++)) 456a5905d6aSJames Morse return -EFAULT; 457a5905d6aSJames Morse 4589ed24f4bSMarc Zyngier return 0; 4599ed24f4bSMarc Zyngier } 4609ed24f4bSMarc Zyngier 4619ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_WIDTH 4 4629ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1) 4639ed24f4bSMarc Zyngier 4649ed24f4bSMarc Zyngier /* 4659ed24f4bSMarc Zyngier * Convert the workaround level into an easy-to-compare number, where higher 4669ed24f4bSMarc Zyngier * values mean better protection. 4679ed24f4bSMarc Zyngier */ 4689ed24f4bSMarc Zyngier static int get_kernel_wa_level(u64 regid) 4699ed24f4bSMarc Zyngier { 4709ed24f4bSMarc Zyngier switch (regid) { 4719ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 472d4647f0aSWill Deacon switch (arm64_get_spectre_v2_state()) { 473d4647f0aSWill Deacon case SPECTRE_VULNERABLE: 4749ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; 475d4647f0aSWill Deacon case SPECTRE_MITIGATED: 4769ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL; 477d4647f0aSWill Deacon case SPECTRE_UNAFFECTED: 4789ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED; 4799ed24f4bSMarc Zyngier } 4809ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; 4819ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 482d63d975aSMarc Zyngier switch (arm64_get_spectre_v4_state()) { 483d63d975aSMarc Zyngier case SPECTRE_MITIGATED: 484d63d975aSMarc Zyngier /* 485d63d975aSMarc Zyngier * As for the hypercall discovery, we pretend we 486d63d975aSMarc Zyngier * don't have any FW mitigation if SSBS is there at 487d63d975aSMarc Zyngier * all times. 488d63d975aSMarc Zyngier */ 489d63d975aSMarc Zyngier if (cpus_have_final_cap(ARM64_SSBS)) 490d63d975aSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 491d63d975aSMarc Zyngier fallthrough; 492d63d975aSMarc Zyngier case SPECTRE_UNAFFECTED: 4939ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; 494d63d975aSMarc Zyngier case SPECTRE_VULNERABLE: 49529e8910aSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 4969ed24f4bSMarc Zyngier } 497a5905d6aSJames Morse break; 498a5905d6aSJames Morse case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3: 499a5905d6aSJames Morse switch (arm64_get_spectre_bhb_state()) { 500a5905d6aSJames Morse case SPECTRE_VULNERABLE: 501a5905d6aSJames Morse return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL; 502a5905d6aSJames Morse case SPECTRE_MITIGATED: 503a5905d6aSJames Morse return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_AVAIL; 504a5905d6aSJames Morse case SPECTRE_UNAFFECTED: 505a5905d6aSJames Morse return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_REQUIRED; 506a5905d6aSJames Morse } 507a5905d6aSJames Morse return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL; 5089ed24f4bSMarc Zyngier } 5099ed24f4bSMarc Zyngier 5109ed24f4bSMarc Zyngier return -EINVAL; 5119ed24f4bSMarc Zyngier } 5129ed24f4bSMarc Zyngier 5139ed24f4bSMarc Zyngier int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 5149ed24f4bSMarc Zyngier { 5159ed24f4bSMarc Zyngier void __user *uaddr = (void __user *)(long)reg->addr; 5169ed24f4bSMarc Zyngier u64 val; 5179ed24f4bSMarc Zyngier 5189ed24f4bSMarc Zyngier switch (reg->id) { 5199ed24f4bSMarc Zyngier case KVM_REG_ARM_PSCI_VERSION: 520dfefa04aSOliver Upton val = kvm_psci_version(vcpu); 5219ed24f4bSMarc Zyngier break; 5229ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 5239ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 524a5905d6aSJames Morse case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3: 5259ed24f4bSMarc Zyngier val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK; 5269ed24f4bSMarc Zyngier break; 5279ed24f4bSMarc Zyngier default: 5289ed24f4bSMarc Zyngier return -ENOENT; 5299ed24f4bSMarc Zyngier } 5309ed24f4bSMarc Zyngier 5319ed24f4bSMarc Zyngier if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id))) 5329ed24f4bSMarc Zyngier return -EFAULT; 5339ed24f4bSMarc Zyngier 5349ed24f4bSMarc Zyngier return 0; 5359ed24f4bSMarc Zyngier } 5369ed24f4bSMarc Zyngier 5379ed24f4bSMarc Zyngier int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 5389ed24f4bSMarc Zyngier { 5399ed24f4bSMarc Zyngier void __user *uaddr = (void __user *)(long)reg->addr; 5409ed24f4bSMarc Zyngier u64 val; 5419ed24f4bSMarc Zyngier int wa_level; 5429ed24f4bSMarc Zyngier 5439ed24f4bSMarc Zyngier if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) 5449ed24f4bSMarc Zyngier return -EFAULT; 5459ed24f4bSMarc Zyngier 5469ed24f4bSMarc Zyngier switch (reg->id) { 5479ed24f4bSMarc Zyngier case KVM_REG_ARM_PSCI_VERSION: 5489ed24f4bSMarc Zyngier { 5499ed24f4bSMarc Zyngier bool wants_02; 5509ed24f4bSMarc Zyngier 5519ed24f4bSMarc Zyngier wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features); 5529ed24f4bSMarc Zyngier 5539ed24f4bSMarc Zyngier switch (val) { 5549ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_1: 5559ed24f4bSMarc Zyngier if (wants_02) 5569ed24f4bSMarc Zyngier return -EINVAL; 5579ed24f4bSMarc Zyngier vcpu->kvm->arch.psci_version = val; 5589ed24f4bSMarc Zyngier return 0; 5599ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_2: 5609ed24f4bSMarc Zyngier case KVM_ARM_PSCI_1_0: 561512865d8SWill Deacon case KVM_ARM_PSCI_1_1: 5629ed24f4bSMarc Zyngier if (!wants_02) 5639ed24f4bSMarc Zyngier return -EINVAL; 5649ed24f4bSMarc Zyngier vcpu->kvm->arch.psci_version = val; 5659ed24f4bSMarc Zyngier return 0; 5669ed24f4bSMarc Zyngier } 5679ed24f4bSMarc Zyngier break; 5689ed24f4bSMarc Zyngier } 5699ed24f4bSMarc Zyngier 5709ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 571a5905d6aSJames Morse case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3: 5729ed24f4bSMarc Zyngier if (val & ~KVM_REG_FEATURE_LEVEL_MASK) 5739ed24f4bSMarc Zyngier return -EINVAL; 5749ed24f4bSMarc Zyngier 5759ed24f4bSMarc Zyngier if (get_kernel_wa_level(reg->id) < val) 5769ed24f4bSMarc Zyngier return -EINVAL; 5779ed24f4bSMarc Zyngier 5789ed24f4bSMarc Zyngier return 0; 5799ed24f4bSMarc Zyngier 5809ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 5819ed24f4bSMarc Zyngier if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | 5829ed24f4bSMarc Zyngier KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED)) 5839ed24f4bSMarc Zyngier return -EINVAL; 5849ed24f4bSMarc Zyngier 5859ed24f4bSMarc Zyngier /* The enabled bit must not be set unless the level is AVAIL. */ 58629e8910aSMarc Zyngier if ((val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED) && 58729e8910aSMarc Zyngier (val & KVM_REG_FEATURE_LEVEL_MASK) != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL) 5889ed24f4bSMarc Zyngier return -EINVAL; 5899ed24f4bSMarc Zyngier 5909ed24f4bSMarc Zyngier /* 59129e8910aSMarc Zyngier * Map all the possible incoming states to the only two we 59229e8910aSMarc Zyngier * really want to deal with. 5939ed24f4bSMarc Zyngier */ 59429e8910aSMarc Zyngier switch (val & KVM_REG_FEATURE_LEVEL_MASK) { 59529e8910aSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL: 59629e8910aSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN: 59729e8910aSMarc Zyngier wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 59829e8910aSMarc Zyngier break; 5999ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL: 6009ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED: 60129e8910aSMarc Zyngier wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; 6029ed24f4bSMarc Zyngier break; 60329e8910aSMarc Zyngier default: 60429e8910aSMarc Zyngier return -EINVAL; 6059ed24f4bSMarc Zyngier } 6069ed24f4bSMarc Zyngier 60729e8910aSMarc Zyngier /* 60829e8910aSMarc Zyngier * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the 60929e8910aSMarc Zyngier * other way around. 61029e8910aSMarc Zyngier */ 61129e8910aSMarc Zyngier if (get_kernel_wa_level(reg->id) < wa_level) 61229e8910aSMarc Zyngier return -EINVAL; 61329e8910aSMarc Zyngier 6149ed24f4bSMarc Zyngier return 0; 6159ed24f4bSMarc Zyngier default: 6169ed24f4bSMarc Zyngier return -ENOENT; 6179ed24f4bSMarc Zyngier } 6189ed24f4bSMarc Zyngier 6199ed24f4bSMarc Zyngier return -EINVAL; 6209ed24f4bSMarc Zyngier } 621