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 */ 499ed24f4bSMarc Zyngier kvm_vcpu_block(vcpu); 509ed24f4bSMarc Zyngier kvm_clear_request(KVM_REQ_UNHALT, vcpu); 519ed24f4bSMarc Zyngier 529ed24f4bSMarc Zyngier return PSCI_RET_SUCCESS; 539ed24f4bSMarc Zyngier } 549ed24f4bSMarc Zyngier 559ed24f4bSMarc Zyngier static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu) 569ed24f4bSMarc Zyngier { 579ed24f4bSMarc Zyngier vcpu->arch.power_off = true; 589ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_SLEEP, vcpu); 599ed24f4bSMarc Zyngier kvm_vcpu_kick(vcpu); 609ed24f4bSMarc Zyngier } 619ed24f4bSMarc Zyngier 62e10ecb4dSOliver Upton static inline bool kvm_psci_valid_affinity(struct kvm_vcpu *vcpu, 63e10ecb4dSOliver Upton unsigned long affinity) 64e10ecb4dSOliver Upton { 65e10ecb4dSOliver Upton return !(affinity & ~MPIDR_HWID_BITMASK); 66e10ecb4dSOliver Upton } 67e10ecb4dSOliver Upton 689ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) 699ed24f4bSMarc Zyngier { 709ed24f4bSMarc Zyngier struct vcpu_reset_state *reset_state; 719ed24f4bSMarc Zyngier struct kvm *kvm = source_vcpu->kvm; 729ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu = NULL; 739ed24f4bSMarc Zyngier unsigned long cpu_id; 749ed24f4bSMarc Zyngier 75e10ecb4dSOliver Upton cpu_id = smccc_get_arg1(source_vcpu); 76e10ecb4dSOliver Upton if (!kvm_psci_valid_affinity(source_vcpu, cpu_id)) 77e10ecb4dSOliver Upton return PSCI_RET_INVALID_PARAMS; 789ed24f4bSMarc Zyngier 799ed24f4bSMarc Zyngier vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id); 809ed24f4bSMarc Zyngier 819ed24f4bSMarc Zyngier /* 829ed24f4bSMarc Zyngier * Make sure the caller requested a valid CPU and that the CPU is 839ed24f4bSMarc Zyngier * turned off. 849ed24f4bSMarc Zyngier */ 859ed24f4bSMarc Zyngier if (!vcpu) 869ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 879ed24f4bSMarc Zyngier if (!vcpu->arch.power_off) { 889ed24f4bSMarc Zyngier if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1) 899ed24f4bSMarc Zyngier return PSCI_RET_ALREADY_ON; 909ed24f4bSMarc Zyngier else 919ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 929ed24f4bSMarc Zyngier } 939ed24f4bSMarc Zyngier 949ed24f4bSMarc Zyngier reset_state = &vcpu->arch.reset_state; 959ed24f4bSMarc Zyngier 969ed24f4bSMarc Zyngier reset_state->pc = smccc_get_arg2(source_vcpu); 979ed24f4bSMarc Zyngier 989ed24f4bSMarc Zyngier /* Propagate caller endianness */ 999ed24f4bSMarc Zyngier reset_state->be = kvm_vcpu_is_be(source_vcpu); 1009ed24f4bSMarc Zyngier 1019ed24f4bSMarc Zyngier /* 1029ed24f4bSMarc Zyngier * NOTE: We always update r0 (or x0) because for PSCI v0.1 103656012c7SFuad Tabba * the general purpose registers are undefined upon CPU_ON. 1049ed24f4bSMarc Zyngier */ 1059ed24f4bSMarc Zyngier reset_state->r0 = smccc_get_arg3(source_vcpu); 1069ed24f4bSMarc Zyngier 1079ed24f4bSMarc Zyngier WRITE_ONCE(reset_state->reset, true); 1089ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); 1099ed24f4bSMarc Zyngier 1109ed24f4bSMarc Zyngier /* 1119ed24f4bSMarc Zyngier * Make sure the reset request is observed if the change to 112*dda0190dSFuad Tabba * power_off is observed. 1139ed24f4bSMarc Zyngier */ 1149ed24f4bSMarc Zyngier smp_wmb(); 1159ed24f4bSMarc Zyngier 1169ed24f4bSMarc Zyngier vcpu->arch.power_off = false; 1179ed24f4bSMarc Zyngier kvm_vcpu_wake_up(vcpu); 1189ed24f4bSMarc Zyngier 1199ed24f4bSMarc Zyngier return PSCI_RET_SUCCESS; 1209ed24f4bSMarc Zyngier } 1219ed24f4bSMarc Zyngier 1229ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu) 1239ed24f4bSMarc Zyngier { 1249ed24f4bSMarc Zyngier int i, matching_cpus = 0; 1259ed24f4bSMarc Zyngier unsigned long mpidr; 1269ed24f4bSMarc Zyngier unsigned long target_affinity; 1279ed24f4bSMarc Zyngier unsigned long target_affinity_mask; 1289ed24f4bSMarc Zyngier unsigned long lowest_affinity_level; 1299ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 1309ed24f4bSMarc Zyngier struct kvm_vcpu *tmp; 1319ed24f4bSMarc Zyngier 1329ed24f4bSMarc Zyngier target_affinity = smccc_get_arg1(vcpu); 1339ed24f4bSMarc Zyngier lowest_affinity_level = smccc_get_arg2(vcpu); 1349ed24f4bSMarc Zyngier 135e10ecb4dSOliver Upton if (!kvm_psci_valid_affinity(vcpu, target_affinity)) 136e10ecb4dSOliver Upton return PSCI_RET_INVALID_PARAMS; 137e10ecb4dSOliver Upton 1389ed24f4bSMarc Zyngier /* Determine target affinity mask */ 1399ed24f4bSMarc Zyngier target_affinity_mask = psci_affinity_mask(lowest_affinity_level); 1409ed24f4bSMarc Zyngier if (!target_affinity_mask) 1419ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 1429ed24f4bSMarc Zyngier 1439ed24f4bSMarc Zyngier /* Ignore other bits of target affinity */ 1449ed24f4bSMarc Zyngier target_affinity &= target_affinity_mask; 1459ed24f4bSMarc Zyngier 1469ed24f4bSMarc Zyngier /* 1479ed24f4bSMarc Zyngier * If one or more VCPU matching target affinity are running 1489ed24f4bSMarc Zyngier * then ON else OFF 1499ed24f4bSMarc Zyngier */ 1509ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, tmp, kvm) { 1519ed24f4bSMarc Zyngier mpidr = kvm_vcpu_get_mpidr_aff(tmp); 1529ed24f4bSMarc Zyngier if ((mpidr & target_affinity_mask) == target_affinity) { 1539ed24f4bSMarc Zyngier matching_cpus++; 1549ed24f4bSMarc Zyngier if (!tmp->arch.power_off) 1559ed24f4bSMarc Zyngier return PSCI_0_2_AFFINITY_LEVEL_ON; 1569ed24f4bSMarc Zyngier } 1579ed24f4bSMarc Zyngier } 1589ed24f4bSMarc Zyngier 1599ed24f4bSMarc Zyngier if (!matching_cpus) 1609ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 1619ed24f4bSMarc Zyngier 1629ed24f4bSMarc Zyngier return PSCI_0_2_AFFINITY_LEVEL_OFF; 1639ed24f4bSMarc Zyngier } 1649ed24f4bSMarc Zyngier 1659ed24f4bSMarc Zyngier static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type) 1669ed24f4bSMarc Zyngier { 1679ed24f4bSMarc Zyngier int i; 1689ed24f4bSMarc Zyngier struct kvm_vcpu *tmp; 1699ed24f4bSMarc Zyngier 1709ed24f4bSMarc Zyngier /* 1719ed24f4bSMarc Zyngier * The KVM ABI specifies that a system event exit may call KVM_RUN 1729ed24f4bSMarc Zyngier * again and may perform shutdown/reboot at a later time that when the 1739ed24f4bSMarc Zyngier * actual request is made. Since we are implementing PSCI and a 1749ed24f4bSMarc Zyngier * caller of PSCI reboot and shutdown expects that the system shuts 1759ed24f4bSMarc Zyngier * down or reboots immediately, let's make sure that VCPUs are not run 1769ed24f4bSMarc Zyngier * after this call is handled and before the VCPUs have been 1779ed24f4bSMarc Zyngier * re-initialized. 1789ed24f4bSMarc Zyngier */ 1799ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, tmp, vcpu->kvm) 1809ed24f4bSMarc Zyngier tmp->arch.power_off = true; 1819ed24f4bSMarc Zyngier kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP); 1829ed24f4bSMarc Zyngier 1839ed24f4bSMarc Zyngier memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); 1849ed24f4bSMarc Zyngier vcpu->run->system_event.type = type; 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 { 1909ed24f4bSMarc Zyngier kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN); 1919ed24f4bSMarc Zyngier } 1929ed24f4bSMarc Zyngier 1939ed24f4bSMarc Zyngier static void kvm_psci_system_reset(struct kvm_vcpu *vcpu) 1949ed24f4bSMarc Zyngier { 1959ed24f4bSMarc Zyngier kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET); 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 { 2129ed24f4bSMarc Zyngier switch(fn) { 2139ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 2149ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 2159ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 2169ed24f4bSMarc Zyngier /* Disallow these functions for 32bit guests */ 2179ed24f4bSMarc Zyngier if (vcpu_mode_is_32bit(vcpu)) 2189ed24f4bSMarc Zyngier return PSCI_RET_NOT_SUPPORTED; 2199ed24f4bSMarc Zyngier break; 2209ed24f4bSMarc Zyngier } 2219ed24f4bSMarc Zyngier 2229ed24f4bSMarc Zyngier return 0; 2239ed24f4bSMarc Zyngier } 2249ed24f4bSMarc Zyngier 2259ed24f4bSMarc Zyngier static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) 2269ed24f4bSMarc Zyngier { 2279ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 2289ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 2299ed24f4bSMarc Zyngier unsigned long val; 2309ed24f4bSMarc Zyngier int ret = 1; 2319ed24f4bSMarc Zyngier 2329ed24f4bSMarc Zyngier val = kvm_psci_check_allowed_function(vcpu, psci_fn); 2339ed24f4bSMarc Zyngier if (val) 2349ed24f4bSMarc Zyngier goto out; 2359ed24f4bSMarc Zyngier 2369ed24f4bSMarc Zyngier switch (psci_fn) { 2379ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 2389ed24f4bSMarc Zyngier /* 2399ed24f4bSMarc Zyngier * Bits[31:16] = Major Version = 0 2409ed24f4bSMarc Zyngier * Bits[15:0] = Minor Version = 2 2419ed24f4bSMarc Zyngier */ 2429ed24f4bSMarc Zyngier val = KVM_ARM_PSCI_0_2; 2439ed24f4bSMarc Zyngier break; 2449ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_SUSPEND: 2459ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 2469ed24f4bSMarc Zyngier val = kvm_psci_vcpu_suspend(vcpu); 2479ed24f4bSMarc Zyngier break; 2489ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_OFF: 2499ed24f4bSMarc Zyngier kvm_psci_vcpu_off(vcpu); 2509ed24f4bSMarc Zyngier val = PSCI_RET_SUCCESS; 2519ed24f4bSMarc Zyngier break; 2529ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_ON: 2539ed24f4bSMarc Zyngier kvm_psci_narrow_to_32bit(vcpu); 2549ed24f4bSMarc Zyngier fallthrough; 2559ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 2569ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 2579ed24f4bSMarc Zyngier val = kvm_psci_vcpu_on(vcpu); 2589ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 2599ed24f4bSMarc Zyngier break; 2609ed24f4bSMarc Zyngier case PSCI_0_2_FN_AFFINITY_INFO: 2619ed24f4bSMarc Zyngier kvm_psci_narrow_to_32bit(vcpu); 2629ed24f4bSMarc Zyngier fallthrough; 2639ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 2649ed24f4bSMarc Zyngier val = kvm_psci_vcpu_affinity_info(vcpu); 2659ed24f4bSMarc Zyngier break; 2669ed24f4bSMarc Zyngier case PSCI_0_2_FN_MIGRATE_INFO_TYPE: 2679ed24f4bSMarc Zyngier /* 2689ed24f4bSMarc Zyngier * Trusted OS is MP hence does not require migration 2699ed24f4bSMarc Zyngier * or 2709ed24f4bSMarc Zyngier * Trusted OS is not present 2719ed24f4bSMarc Zyngier */ 2729ed24f4bSMarc Zyngier val = PSCI_0_2_TOS_MP; 2739ed24f4bSMarc Zyngier break; 2749ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_OFF: 2759ed24f4bSMarc Zyngier kvm_psci_system_off(vcpu); 2769ed24f4bSMarc Zyngier /* 277656012c7SFuad Tabba * We shouldn't be going back to guest VCPU after 2789ed24f4bSMarc Zyngier * receiving SYSTEM_OFF request. 2799ed24f4bSMarc Zyngier * 280656012c7SFuad Tabba * If user space accidentally/deliberately resumes 2819ed24f4bSMarc Zyngier * guest VCPU after SYSTEM_OFF request then guest 2829ed24f4bSMarc Zyngier * VCPU should see internal failure from PSCI return 2839ed24f4bSMarc Zyngier * value. To achieve this, we preload r0 (or x0) with 2849ed24f4bSMarc Zyngier * PSCI return value INTERNAL_FAILURE. 2859ed24f4bSMarc Zyngier */ 2869ed24f4bSMarc Zyngier val = PSCI_RET_INTERNAL_FAILURE; 2879ed24f4bSMarc Zyngier ret = 0; 2889ed24f4bSMarc Zyngier break; 2899ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_RESET: 2909ed24f4bSMarc Zyngier kvm_psci_system_reset(vcpu); 2919ed24f4bSMarc Zyngier /* 2929ed24f4bSMarc Zyngier * Same reason as SYSTEM_OFF for preloading r0 (or x0) 2939ed24f4bSMarc Zyngier * with PSCI return value INTERNAL_FAILURE. 2949ed24f4bSMarc Zyngier */ 2959ed24f4bSMarc Zyngier val = PSCI_RET_INTERNAL_FAILURE; 2969ed24f4bSMarc Zyngier ret = 0; 2979ed24f4bSMarc Zyngier break; 2989ed24f4bSMarc Zyngier default: 2999ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3009ed24f4bSMarc Zyngier break; 3019ed24f4bSMarc Zyngier } 3029ed24f4bSMarc Zyngier 3039ed24f4bSMarc Zyngier out: 3049ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3059ed24f4bSMarc Zyngier return ret; 3069ed24f4bSMarc Zyngier } 3079ed24f4bSMarc Zyngier 3089ed24f4bSMarc Zyngier static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu) 3099ed24f4bSMarc Zyngier { 3109ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 3119ed24f4bSMarc Zyngier u32 feature; 3129ed24f4bSMarc Zyngier unsigned long val; 3139ed24f4bSMarc Zyngier int ret = 1; 3149ed24f4bSMarc Zyngier 3159ed24f4bSMarc Zyngier switch(psci_fn) { 3169ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 3179ed24f4bSMarc Zyngier val = KVM_ARM_PSCI_1_0; 3189ed24f4bSMarc Zyngier break; 3199ed24f4bSMarc Zyngier case PSCI_1_0_FN_PSCI_FEATURES: 3209ed24f4bSMarc Zyngier feature = smccc_get_arg1(vcpu); 3219ed24f4bSMarc Zyngier val = kvm_psci_check_allowed_function(vcpu, feature); 3229ed24f4bSMarc Zyngier if (val) 3239ed24f4bSMarc Zyngier break; 3249ed24f4bSMarc Zyngier 3259ed24f4bSMarc Zyngier switch(feature) { 3269ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 3279ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_SUSPEND: 3289ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 3299ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_OFF: 3309ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_ON: 3319ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 3329ed24f4bSMarc Zyngier case PSCI_0_2_FN_AFFINITY_INFO: 3339ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 3349ed24f4bSMarc Zyngier case PSCI_0_2_FN_MIGRATE_INFO_TYPE: 3359ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_OFF: 3369ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_RESET: 3379ed24f4bSMarc Zyngier case PSCI_1_0_FN_PSCI_FEATURES: 3389ed24f4bSMarc Zyngier case ARM_SMCCC_VERSION_FUNC_ID: 3399ed24f4bSMarc Zyngier val = 0; 3409ed24f4bSMarc Zyngier break; 3419ed24f4bSMarc Zyngier default: 3429ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3439ed24f4bSMarc Zyngier break; 3449ed24f4bSMarc Zyngier } 3459ed24f4bSMarc Zyngier break; 3469ed24f4bSMarc Zyngier default: 3479ed24f4bSMarc Zyngier return kvm_psci_0_2_call(vcpu); 3489ed24f4bSMarc Zyngier } 3499ed24f4bSMarc Zyngier 3509ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3519ed24f4bSMarc Zyngier return ret; 3529ed24f4bSMarc Zyngier } 3539ed24f4bSMarc Zyngier 3549ed24f4bSMarc Zyngier static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) 3559ed24f4bSMarc Zyngier { 3569ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 3579ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 3589ed24f4bSMarc Zyngier unsigned long val; 3599ed24f4bSMarc Zyngier 3609ed24f4bSMarc Zyngier switch (psci_fn) { 3619ed24f4bSMarc Zyngier case KVM_PSCI_FN_CPU_OFF: 3629ed24f4bSMarc Zyngier kvm_psci_vcpu_off(vcpu); 3639ed24f4bSMarc Zyngier val = PSCI_RET_SUCCESS; 3649ed24f4bSMarc Zyngier break; 3659ed24f4bSMarc Zyngier case KVM_PSCI_FN_CPU_ON: 3669ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 3679ed24f4bSMarc Zyngier val = kvm_psci_vcpu_on(vcpu); 3689ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 3699ed24f4bSMarc Zyngier break; 3709ed24f4bSMarc Zyngier default: 3719ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3729ed24f4bSMarc Zyngier break; 3739ed24f4bSMarc Zyngier } 3749ed24f4bSMarc Zyngier 3759ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3769ed24f4bSMarc Zyngier return 1; 3779ed24f4bSMarc Zyngier } 3789ed24f4bSMarc Zyngier 3799ed24f4bSMarc Zyngier /** 3809ed24f4bSMarc Zyngier * kvm_psci_call - handle PSCI call if r0 value is in range 3819ed24f4bSMarc Zyngier * @vcpu: Pointer to the VCPU struct 3829ed24f4bSMarc Zyngier * 3839ed24f4bSMarc Zyngier * Handle PSCI calls from guests through traps from HVC instructions. 3849ed24f4bSMarc Zyngier * The calling convention is similar to SMC calls to the secure world 3859ed24f4bSMarc Zyngier * where the function number is placed in r0. 3869ed24f4bSMarc Zyngier * 3879ed24f4bSMarc Zyngier * This function returns: > 0 (success), 0 (success but exit to user 3889ed24f4bSMarc Zyngier * space), and < 0 (errors) 3899ed24f4bSMarc Zyngier * 3909ed24f4bSMarc Zyngier * Errors: 3919ed24f4bSMarc Zyngier * -EINVAL: Unrecognized PSCI function 3929ed24f4bSMarc Zyngier */ 3939ed24f4bSMarc Zyngier int kvm_psci_call(struct kvm_vcpu *vcpu) 3949ed24f4bSMarc Zyngier { 3959ed24f4bSMarc Zyngier switch (kvm_psci_version(vcpu, vcpu->kvm)) { 3969ed24f4bSMarc Zyngier case KVM_ARM_PSCI_1_0: 3979ed24f4bSMarc Zyngier return kvm_psci_1_0_call(vcpu); 3989ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_2: 3999ed24f4bSMarc Zyngier return kvm_psci_0_2_call(vcpu); 4009ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_1: 4019ed24f4bSMarc Zyngier return kvm_psci_0_1_call(vcpu); 4029ed24f4bSMarc Zyngier default: 4039ed24f4bSMarc Zyngier return -EINVAL; 4049ed24f4bSMarc Zyngier }; 4059ed24f4bSMarc Zyngier } 4069ed24f4bSMarc Zyngier 4079ed24f4bSMarc Zyngier int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu) 4089ed24f4bSMarc Zyngier { 4099ed24f4bSMarc Zyngier return 3; /* PSCI version and two workaround registers */ 4109ed24f4bSMarc Zyngier } 4119ed24f4bSMarc Zyngier 4129ed24f4bSMarc Zyngier int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) 4139ed24f4bSMarc Zyngier { 4149ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++)) 4159ed24f4bSMarc Zyngier return -EFAULT; 4169ed24f4bSMarc Zyngier 4179ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++)) 4189ed24f4bSMarc Zyngier return -EFAULT; 4199ed24f4bSMarc Zyngier 4209ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++)) 4219ed24f4bSMarc Zyngier return -EFAULT; 4229ed24f4bSMarc Zyngier 4239ed24f4bSMarc Zyngier return 0; 4249ed24f4bSMarc Zyngier } 4259ed24f4bSMarc Zyngier 4269ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_WIDTH 4 4279ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1) 4289ed24f4bSMarc Zyngier 4299ed24f4bSMarc Zyngier /* 4309ed24f4bSMarc Zyngier * Convert the workaround level into an easy-to-compare number, where higher 4319ed24f4bSMarc Zyngier * values mean better protection. 4329ed24f4bSMarc Zyngier */ 4339ed24f4bSMarc Zyngier static int get_kernel_wa_level(u64 regid) 4349ed24f4bSMarc Zyngier { 4359ed24f4bSMarc Zyngier switch (regid) { 4369ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 437d4647f0aSWill Deacon switch (arm64_get_spectre_v2_state()) { 438d4647f0aSWill Deacon case SPECTRE_VULNERABLE: 4399ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; 440d4647f0aSWill Deacon case SPECTRE_MITIGATED: 4419ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL; 442d4647f0aSWill Deacon case SPECTRE_UNAFFECTED: 4439ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED; 4449ed24f4bSMarc Zyngier } 4459ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; 4469ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 447d63d975aSMarc Zyngier switch (arm64_get_spectre_v4_state()) { 448d63d975aSMarc Zyngier case SPECTRE_MITIGATED: 449d63d975aSMarc Zyngier /* 450d63d975aSMarc Zyngier * As for the hypercall discovery, we pretend we 451d63d975aSMarc Zyngier * don't have any FW mitigation if SSBS is there at 452d63d975aSMarc Zyngier * all times. 453d63d975aSMarc Zyngier */ 454d63d975aSMarc Zyngier if (cpus_have_final_cap(ARM64_SSBS)) 455d63d975aSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 456d63d975aSMarc Zyngier fallthrough; 457d63d975aSMarc Zyngier case SPECTRE_UNAFFECTED: 4589ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; 459d63d975aSMarc Zyngier case SPECTRE_VULNERABLE: 46029e8910aSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 4619ed24f4bSMarc Zyngier } 4629ed24f4bSMarc Zyngier } 4639ed24f4bSMarc Zyngier 4649ed24f4bSMarc Zyngier return -EINVAL; 4659ed24f4bSMarc Zyngier } 4669ed24f4bSMarc Zyngier 4679ed24f4bSMarc Zyngier int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 4689ed24f4bSMarc Zyngier { 4699ed24f4bSMarc Zyngier void __user *uaddr = (void __user *)(long)reg->addr; 4709ed24f4bSMarc Zyngier u64 val; 4719ed24f4bSMarc Zyngier 4729ed24f4bSMarc Zyngier switch (reg->id) { 4739ed24f4bSMarc Zyngier case KVM_REG_ARM_PSCI_VERSION: 4749ed24f4bSMarc Zyngier val = kvm_psci_version(vcpu, vcpu->kvm); 4759ed24f4bSMarc Zyngier break; 4769ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 4779ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 4789ed24f4bSMarc Zyngier val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK; 4799ed24f4bSMarc Zyngier break; 4809ed24f4bSMarc Zyngier default: 4819ed24f4bSMarc Zyngier return -ENOENT; 4829ed24f4bSMarc Zyngier } 4839ed24f4bSMarc Zyngier 4849ed24f4bSMarc Zyngier if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id))) 4859ed24f4bSMarc Zyngier return -EFAULT; 4869ed24f4bSMarc Zyngier 4879ed24f4bSMarc Zyngier return 0; 4889ed24f4bSMarc Zyngier } 4899ed24f4bSMarc Zyngier 4909ed24f4bSMarc Zyngier int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 4919ed24f4bSMarc Zyngier { 4929ed24f4bSMarc Zyngier void __user *uaddr = (void __user *)(long)reg->addr; 4939ed24f4bSMarc Zyngier u64 val; 4949ed24f4bSMarc Zyngier int wa_level; 4959ed24f4bSMarc Zyngier 4969ed24f4bSMarc Zyngier if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) 4979ed24f4bSMarc Zyngier return -EFAULT; 4989ed24f4bSMarc Zyngier 4999ed24f4bSMarc Zyngier switch (reg->id) { 5009ed24f4bSMarc Zyngier case KVM_REG_ARM_PSCI_VERSION: 5019ed24f4bSMarc Zyngier { 5029ed24f4bSMarc Zyngier bool wants_02; 5039ed24f4bSMarc Zyngier 5049ed24f4bSMarc Zyngier wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features); 5059ed24f4bSMarc Zyngier 5069ed24f4bSMarc Zyngier switch (val) { 5079ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_1: 5089ed24f4bSMarc Zyngier if (wants_02) 5099ed24f4bSMarc Zyngier return -EINVAL; 5109ed24f4bSMarc Zyngier vcpu->kvm->arch.psci_version = val; 5119ed24f4bSMarc Zyngier return 0; 5129ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_2: 5139ed24f4bSMarc Zyngier case KVM_ARM_PSCI_1_0: 5149ed24f4bSMarc Zyngier if (!wants_02) 5159ed24f4bSMarc Zyngier return -EINVAL; 5169ed24f4bSMarc Zyngier vcpu->kvm->arch.psci_version = val; 5179ed24f4bSMarc Zyngier return 0; 5189ed24f4bSMarc Zyngier } 5199ed24f4bSMarc Zyngier break; 5209ed24f4bSMarc Zyngier } 5219ed24f4bSMarc Zyngier 5229ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 5239ed24f4bSMarc Zyngier if (val & ~KVM_REG_FEATURE_LEVEL_MASK) 5249ed24f4bSMarc Zyngier return -EINVAL; 5259ed24f4bSMarc Zyngier 5269ed24f4bSMarc Zyngier if (get_kernel_wa_level(reg->id) < val) 5279ed24f4bSMarc Zyngier return -EINVAL; 5289ed24f4bSMarc Zyngier 5299ed24f4bSMarc Zyngier return 0; 5309ed24f4bSMarc Zyngier 5319ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 5329ed24f4bSMarc Zyngier if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | 5339ed24f4bSMarc Zyngier KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED)) 5349ed24f4bSMarc Zyngier return -EINVAL; 5359ed24f4bSMarc Zyngier 5369ed24f4bSMarc Zyngier /* The enabled bit must not be set unless the level is AVAIL. */ 53729e8910aSMarc Zyngier if ((val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED) && 53829e8910aSMarc Zyngier (val & KVM_REG_FEATURE_LEVEL_MASK) != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL) 5399ed24f4bSMarc Zyngier return -EINVAL; 5409ed24f4bSMarc Zyngier 5419ed24f4bSMarc Zyngier /* 54229e8910aSMarc Zyngier * Map all the possible incoming states to the only two we 54329e8910aSMarc Zyngier * really want to deal with. 5449ed24f4bSMarc Zyngier */ 54529e8910aSMarc Zyngier switch (val & KVM_REG_FEATURE_LEVEL_MASK) { 54629e8910aSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL: 54729e8910aSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN: 54829e8910aSMarc Zyngier wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 54929e8910aSMarc Zyngier break; 5509ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL: 5519ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED: 55229e8910aSMarc Zyngier wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; 5539ed24f4bSMarc Zyngier break; 55429e8910aSMarc Zyngier default: 55529e8910aSMarc Zyngier return -EINVAL; 5569ed24f4bSMarc Zyngier } 5579ed24f4bSMarc Zyngier 55829e8910aSMarc Zyngier /* 55929e8910aSMarc Zyngier * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the 56029e8910aSMarc Zyngier * other way around. 56129e8910aSMarc Zyngier */ 56229e8910aSMarc Zyngier if (get_kernel_wa_level(reg->id) < wa_level) 56329e8910aSMarc Zyngier return -EINVAL; 56429e8910aSMarc Zyngier 5659ed24f4bSMarc Zyngier return 0; 5669ed24f4bSMarc Zyngier default: 5679ed24f4bSMarc Zyngier return -ENOENT; 5689ed24f4bSMarc Zyngier } 5699ed24f4bSMarc Zyngier 5709ed24f4bSMarc Zyngier return -EINVAL; 5719ed24f4bSMarc Zyngier } 572