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 629ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) 639ed24f4bSMarc Zyngier { 649ed24f4bSMarc Zyngier struct vcpu_reset_state *reset_state; 659ed24f4bSMarc Zyngier struct kvm *kvm = source_vcpu->kvm; 669ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu = NULL; 679ed24f4bSMarc Zyngier unsigned long cpu_id; 689ed24f4bSMarc Zyngier 699ed24f4bSMarc Zyngier cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK; 709ed24f4bSMarc Zyngier if (vcpu_mode_is_32bit(source_vcpu)) 719ed24f4bSMarc Zyngier cpu_id &= ~((u32) 0); 729ed24f4bSMarc Zyngier 739ed24f4bSMarc Zyngier vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id); 749ed24f4bSMarc Zyngier 759ed24f4bSMarc Zyngier /* 769ed24f4bSMarc Zyngier * Make sure the caller requested a valid CPU and that the CPU is 779ed24f4bSMarc Zyngier * turned off. 789ed24f4bSMarc Zyngier */ 799ed24f4bSMarc Zyngier if (!vcpu) 809ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 819ed24f4bSMarc Zyngier if (!vcpu->arch.power_off) { 829ed24f4bSMarc Zyngier if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1) 839ed24f4bSMarc Zyngier return PSCI_RET_ALREADY_ON; 849ed24f4bSMarc Zyngier else 859ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 869ed24f4bSMarc Zyngier } 879ed24f4bSMarc Zyngier 889ed24f4bSMarc Zyngier reset_state = &vcpu->arch.reset_state; 899ed24f4bSMarc Zyngier 909ed24f4bSMarc Zyngier reset_state->pc = smccc_get_arg2(source_vcpu); 919ed24f4bSMarc Zyngier 929ed24f4bSMarc Zyngier /* Propagate caller endianness */ 939ed24f4bSMarc Zyngier reset_state->be = kvm_vcpu_is_be(source_vcpu); 949ed24f4bSMarc Zyngier 959ed24f4bSMarc Zyngier /* 969ed24f4bSMarc Zyngier * NOTE: We always update r0 (or x0) because for PSCI v0.1 97656012c7SFuad Tabba * the general purpose registers are undefined upon CPU_ON. 989ed24f4bSMarc Zyngier */ 999ed24f4bSMarc Zyngier reset_state->r0 = smccc_get_arg3(source_vcpu); 1009ed24f4bSMarc Zyngier 1019ed24f4bSMarc Zyngier WRITE_ONCE(reset_state->reset, true); 1029ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); 1039ed24f4bSMarc Zyngier 1049ed24f4bSMarc Zyngier /* 1059ed24f4bSMarc Zyngier * Make sure the reset request is observed if the change to 1069ed24f4bSMarc Zyngier * power_state is observed. 1079ed24f4bSMarc Zyngier */ 1089ed24f4bSMarc Zyngier smp_wmb(); 1099ed24f4bSMarc Zyngier 1109ed24f4bSMarc Zyngier vcpu->arch.power_off = false; 1119ed24f4bSMarc Zyngier kvm_vcpu_wake_up(vcpu); 1129ed24f4bSMarc Zyngier 1139ed24f4bSMarc Zyngier return PSCI_RET_SUCCESS; 1149ed24f4bSMarc Zyngier } 1159ed24f4bSMarc Zyngier 1169ed24f4bSMarc Zyngier static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu) 1179ed24f4bSMarc Zyngier { 1189ed24f4bSMarc Zyngier int i, matching_cpus = 0; 1199ed24f4bSMarc Zyngier unsigned long mpidr; 1209ed24f4bSMarc Zyngier unsigned long target_affinity; 1219ed24f4bSMarc Zyngier unsigned long target_affinity_mask; 1229ed24f4bSMarc Zyngier unsigned long lowest_affinity_level; 1239ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 1249ed24f4bSMarc Zyngier struct kvm_vcpu *tmp; 1259ed24f4bSMarc Zyngier 1269ed24f4bSMarc Zyngier target_affinity = smccc_get_arg1(vcpu); 1279ed24f4bSMarc Zyngier lowest_affinity_level = smccc_get_arg2(vcpu); 1289ed24f4bSMarc Zyngier 1299ed24f4bSMarc Zyngier /* Determine target affinity mask */ 1309ed24f4bSMarc Zyngier target_affinity_mask = psci_affinity_mask(lowest_affinity_level); 1319ed24f4bSMarc Zyngier if (!target_affinity_mask) 1329ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 1339ed24f4bSMarc Zyngier 1349ed24f4bSMarc Zyngier /* Ignore other bits of target affinity */ 1359ed24f4bSMarc Zyngier target_affinity &= target_affinity_mask; 1369ed24f4bSMarc Zyngier 1379ed24f4bSMarc Zyngier /* 1389ed24f4bSMarc Zyngier * If one or more VCPU matching target affinity are running 1399ed24f4bSMarc Zyngier * then ON else OFF 1409ed24f4bSMarc Zyngier */ 1419ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, tmp, kvm) { 1429ed24f4bSMarc Zyngier mpidr = kvm_vcpu_get_mpidr_aff(tmp); 1439ed24f4bSMarc Zyngier if ((mpidr & target_affinity_mask) == target_affinity) { 1449ed24f4bSMarc Zyngier matching_cpus++; 1459ed24f4bSMarc Zyngier if (!tmp->arch.power_off) 1469ed24f4bSMarc Zyngier return PSCI_0_2_AFFINITY_LEVEL_ON; 1479ed24f4bSMarc Zyngier } 1489ed24f4bSMarc Zyngier } 1499ed24f4bSMarc Zyngier 1509ed24f4bSMarc Zyngier if (!matching_cpus) 1519ed24f4bSMarc Zyngier return PSCI_RET_INVALID_PARAMS; 1529ed24f4bSMarc Zyngier 1539ed24f4bSMarc Zyngier return PSCI_0_2_AFFINITY_LEVEL_OFF; 1549ed24f4bSMarc Zyngier } 1559ed24f4bSMarc Zyngier 1569ed24f4bSMarc Zyngier static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type) 1579ed24f4bSMarc Zyngier { 1589ed24f4bSMarc Zyngier int i; 1599ed24f4bSMarc Zyngier struct kvm_vcpu *tmp; 1609ed24f4bSMarc Zyngier 1619ed24f4bSMarc Zyngier /* 1629ed24f4bSMarc Zyngier * The KVM ABI specifies that a system event exit may call KVM_RUN 1639ed24f4bSMarc Zyngier * again and may perform shutdown/reboot at a later time that when the 1649ed24f4bSMarc Zyngier * actual request is made. Since we are implementing PSCI and a 1659ed24f4bSMarc Zyngier * caller of PSCI reboot and shutdown expects that the system shuts 1669ed24f4bSMarc Zyngier * down or reboots immediately, let's make sure that VCPUs are not run 1679ed24f4bSMarc Zyngier * after this call is handled and before the VCPUs have been 1689ed24f4bSMarc Zyngier * re-initialized. 1699ed24f4bSMarc Zyngier */ 1709ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, tmp, vcpu->kvm) 1719ed24f4bSMarc Zyngier tmp->arch.power_off = true; 1729ed24f4bSMarc Zyngier kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP); 1739ed24f4bSMarc Zyngier 1749ed24f4bSMarc Zyngier memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); 1759ed24f4bSMarc Zyngier vcpu->run->system_event.type = type; 1769ed24f4bSMarc Zyngier vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; 1779ed24f4bSMarc Zyngier } 1789ed24f4bSMarc Zyngier 1799ed24f4bSMarc Zyngier static void kvm_psci_system_off(struct kvm_vcpu *vcpu) 1809ed24f4bSMarc Zyngier { 1819ed24f4bSMarc Zyngier kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN); 1829ed24f4bSMarc Zyngier } 1839ed24f4bSMarc Zyngier 1849ed24f4bSMarc Zyngier static void kvm_psci_system_reset(struct kvm_vcpu *vcpu) 1859ed24f4bSMarc Zyngier { 1869ed24f4bSMarc Zyngier kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET); 1879ed24f4bSMarc Zyngier } 1889ed24f4bSMarc Zyngier 1899ed24f4bSMarc Zyngier static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu) 1909ed24f4bSMarc Zyngier { 1919ed24f4bSMarc Zyngier int i; 1929ed24f4bSMarc Zyngier 1939ed24f4bSMarc Zyngier /* 1949ed24f4bSMarc Zyngier * Zero the input registers' upper 32 bits. They will be fully 1959ed24f4bSMarc Zyngier * zeroed on exit, so we're fine changing them in place. 1969ed24f4bSMarc Zyngier */ 1979ed24f4bSMarc Zyngier for (i = 1; i < 4; i++) 1989ed24f4bSMarc Zyngier vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i))); 1999ed24f4bSMarc Zyngier } 2009ed24f4bSMarc Zyngier 2019ed24f4bSMarc Zyngier static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn) 2029ed24f4bSMarc Zyngier { 2039ed24f4bSMarc Zyngier switch(fn) { 2049ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 2059ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 2069ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 2079ed24f4bSMarc Zyngier /* Disallow these functions for 32bit guests */ 2089ed24f4bSMarc Zyngier if (vcpu_mode_is_32bit(vcpu)) 2099ed24f4bSMarc Zyngier return PSCI_RET_NOT_SUPPORTED; 2109ed24f4bSMarc Zyngier break; 2119ed24f4bSMarc Zyngier } 2129ed24f4bSMarc Zyngier 2139ed24f4bSMarc Zyngier return 0; 2149ed24f4bSMarc Zyngier } 2159ed24f4bSMarc Zyngier 2169ed24f4bSMarc Zyngier static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) 2179ed24f4bSMarc Zyngier { 2189ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 2199ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 2209ed24f4bSMarc Zyngier unsigned long val; 2219ed24f4bSMarc Zyngier int ret = 1; 2229ed24f4bSMarc Zyngier 2239ed24f4bSMarc Zyngier val = kvm_psci_check_allowed_function(vcpu, psci_fn); 2249ed24f4bSMarc Zyngier if (val) 2259ed24f4bSMarc Zyngier goto out; 2269ed24f4bSMarc Zyngier 2279ed24f4bSMarc Zyngier switch (psci_fn) { 2289ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 2299ed24f4bSMarc Zyngier /* 2309ed24f4bSMarc Zyngier * Bits[31:16] = Major Version = 0 2319ed24f4bSMarc Zyngier * Bits[15:0] = Minor Version = 2 2329ed24f4bSMarc Zyngier */ 2339ed24f4bSMarc Zyngier val = KVM_ARM_PSCI_0_2; 2349ed24f4bSMarc Zyngier break; 2359ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_SUSPEND: 2369ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 2379ed24f4bSMarc Zyngier val = kvm_psci_vcpu_suspend(vcpu); 2389ed24f4bSMarc Zyngier break; 2399ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_OFF: 2409ed24f4bSMarc Zyngier kvm_psci_vcpu_off(vcpu); 2419ed24f4bSMarc Zyngier val = PSCI_RET_SUCCESS; 2429ed24f4bSMarc Zyngier break; 2439ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_ON: 2449ed24f4bSMarc Zyngier kvm_psci_narrow_to_32bit(vcpu); 2459ed24f4bSMarc Zyngier fallthrough; 2469ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 2479ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 2489ed24f4bSMarc Zyngier val = kvm_psci_vcpu_on(vcpu); 2499ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 2509ed24f4bSMarc Zyngier break; 2519ed24f4bSMarc Zyngier case PSCI_0_2_FN_AFFINITY_INFO: 2529ed24f4bSMarc Zyngier kvm_psci_narrow_to_32bit(vcpu); 2539ed24f4bSMarc Zyngier fallthrough; 2549ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 2559ed24f4bSMarc Zyngier val = kvm_psci_vcpu_affinity_info(vcpu); 2569ed24f4bSMarc Zyngier break; 2579ed24f4bSMarc Zyngier case PSCI_0_2_FN_MIGRATE_INFO_TYPE: 2589ed24f4bSMarc Zyngier /* 2599ed24f4bSMarc Zyngier * Trusted OS is MP hence does not require migration 2609ed24f4bSMarc Zyngier * or 2619ed24f4bSMarc Zyngier * Trusted OS is not present 2629ed24f4bSMarc Zyngier */ 2639ed24f4bSMarc Zyngier val = PSCI_0_2_TOS_MP; 2649ed24f4bSMarc Zyngier break; 2659ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_OFF: 2669ed24f4bSMarc Zyngier kvm_psci_system_off(vcpu); 2679ed24f4bSMarc Zyngier /* 268656012c7SFuad Tabba * We shouldn't be going back to guest VCPU after 2699ed24f4bSMarc Zyngier * receiving SYSTEM_OFF request. 2709ed24f4bSMarc Zyngier * 271656012c7SFuad Tabba * If user space accidentally/deliberately resumes 2729ed24f4bSMarc Zyngier * guest VCPU after SYSTEM_OFF request then guest 2739ed24f4bSMarc Zyngier * VCPU should see internal failure from PSCI return 2749ed24f4bSMarc Zyngier * value. To achieve this, we preload r0 (or x0) with 2759ed24f4bSMarc Zyngier * PSCI return value INTERNAL_FAILURE. 2769ed24f4bSMarc Zyngier */ 2779ed24f4bSMarc Zyngier val = PSCI_RET_INTERNAL_FAILURE; 2789ed24f4bSMarc Zyngier ret = 0; 2799ed24f4bSMarc Zyngier break; 2809ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_RESET: 2819ed24f4bSMarc Zyngier kvm_psci_system_reset(vcpu); 2829ed24f4bSMarc Zyngier /* 2839ed24f4bSMarc Zyngier * Same reason as SYSTEM_OFF for preloading r0 (or x0) 2849ed24f4bSMarc Zyngier * with PSCI return value INTERNAL_FAILURE. 2859ed24f4bSMarc Zyngier */ 2869ed24f4bSMarc Zyngier val = PSCI_RET_INTERNAL_FAILURE; 2879ed24f4bSMarc Zyngier ret = 0; 2889ed24f4bSMarc Zyngier break; 2899ed24f4bSMarc Zyngier default: 2909ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 2919ed24f4bSMarc Zyngier break; 2929ed24f4bSMarc Zyngier } 2939ed24f4bSMarc Zyngier 2949ed24f4bSMarc Zyngier out: 2959ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 2969ed24f4bSMarc Zyngier return ret; 2979ed24f4bSMarc Zyngier } 2989ed24f4bSMarc Zyngier 2999ed24f4bSMarc Zyngier static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu) 3009ed24f4bSMarc Zyngier { 3019ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 3029ed24f4bSMarc Zyngier u32 feature; 3039ed24f4bSMarc Zyngier unsigned long val; 3049ed24f4bSMarc Zyngier int ret = 1; 3059ed24f4bSMarc Zyngier 3069ed24f4bSMarc Zyngier switch(psci_fn) { 3079ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 3089ed24f4bSMarc Zyngier val = KVM_ARM_PSCI_1_0; 3099ed24f4bSMarc Zyngier break; 3109ed24f4bSMarc Zyngier case PSCI_1_0_FN_PSCI_FEATURES: 3119ed24f4bSMarc Zyngier feature = smccc_get_arg1(vcpu); 3129ed24f4bSMarc Zyngier val = kvm_psci_check_allowed_function(vcpu, feature); 3139ed24f4bSMarc Zyngier if (val) 3149ed24f4bSMarc Zyngier break; 3159ed24f4bSMarc Zyngier 3169ed24f4bSMarc Zyngier switch(feature) { 3179ed24f4bSMarc Zyngier case PSCI_0_2_FN_PSCI_VERSION: 3189ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_SUSPEND: 3199ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_SUSPEND: 3209ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_OFF: 3219ed24f4bSMarc Zyngier case PSCI_0_2_FN_CPU_ON: 3229ed24f4bSMarc Zyngier case PSCI_0_2_FN64_CPU_ON: 3239ed24f4bSMarc Zyngier case PSCI_0_2_FN_AFFINITY_INFO: 3249ed24f4bSMarc Zyngier case PSCI_0_2_FN64_AFFINITY_INFO: 3259ed24f4bSMarc Zyngier case PSCI_0_2_FN_MIGRATE_INFO_TYPE: 3269ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_OFF: 3279ed24f4bSMarc Zyngier case PSCI_0_2_FN_SYSTEM_RESET: 3289ed24f4bSMarc Zyngier case PSCI_1_0_FN_PSCI_FEATURES: 3299ed24f4bSMarc Zyngier case ARM_SMCCC_VERSION_FUNC_ID: 3309ed24f4bSMarc Zyngier val = 0; 3319ed24f4bSMarc Zyngier break; 3329ed24f4bSMarc Zyngier default: 3339ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3349ed24f4bSMarc Zyngier break; 3359ed24f4bSMarc Zyngier } 3369ed24f4bSMarc Zyngier break; 3379ed24f4bSMarc Zyngier default: 3389ed24f4bSMarc Zyngier return kvm_psci_0_2_call(vcpu); 3399ed24f4bSMarc Zyngier } 3409ed24f4bSMarc Zyngier 3419ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3429ed24f4bSMarc Zyngier return ret; 3439ed24f4bSMarc Zyngier } 3449ed24f4bSMarc Zyngier 3459ed24f4bSMarc Zyngier static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) 3469ed24f4bSMarc Zyngier { 3479ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 3489ed24f4bSMarc Zyngier u32 psci_fn = smccc_get_function(vcpu); 3499ed24f4bSMarc Zyngier unsigned long val; 3509ed24f4bSMarc Zyngier 3519ed24f4bSMarc Zyngier switch (psci_fn) { 3529ed24f4bSMarc Zyngier case KVM_PSCI_FN_CPU_OFF: 3539ed24f4bSMarc Zyngier kvm_psci_vcpu_off(vcpu); 3549ed24f4bSMarc Zyngier val = PSCI_RET_SUCCESS; 3559ed24f4bSMarc Zyngier break; 3569ed24f4bSMarc Zyngier case KVM_PSCI_FN_CPU_ON: 3579ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 3589ed24f4bSMarc Zyngier val = kvm_psci_vcpu_on(vcpu); 3599ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 3609ed24f4bSMarc Zyngier break; 3619ed24f4bSMarc Zyngier default: 3629ed24f4bSMarc Zyngier val = PSCI_RET_NOT_SUPPORTED; 3639ed24f4bSMarc Zyngier break; 3649ed24f4bSMarc Zyngier } 3659ed24f4bSMarc Zyngier 3669ed24f4bSMarc Zyngier smccc_set_retval(vcpu, val, 0, 0, 0); 3679ed24f4bSMarc Zyngier return 1; 3689ed24f4bSMarc Zyngier } 3699ed24f4bSMarc Zyngier 3709ed24f4bSMarc Zyngier /** 3719ed24f4bSMarc Zyngier * kvm_psci_call - handle PSCI call if r0 value is in range 3729ed24f4bSMarc Zyngier * @vcpu: Pointer to the VCPU struct 3739ed24f4bSMarc Zyngier * 3749ed24f4bSMarc Zyngier * Handle PSCI calls from guests through traps from HVC instructions. 3759ed24f4bSMarc Zyngier * The calling convention is similar to SMC calls to the secure world 3769ed24f4bSMarc Zyngier * where the function number is placed in r0. 3779ed24f4bSMarc Zyngier * 3789ed24f4bSMarc Zyngier * This function returns: > 0 (success), 0 (success but exit to user 3799ed24f4bSMarc Zyngier * space), and < 0 (errors) 3809ed24f4bSMarc Zyngier * 3819ed24f4bSMarc Zyngier * Errors: 3829ed24f4bSMarc Zyngier * -EINVAL: Unrecognized PSCI function 3839ed24f4bSMarc Zyngier */ 3849ed24f4bSMarc Zyngier int kvm_psci_call(struct kvm_vcpu *vcpu) 3859ed24f4bSMarc Zyngier { 3869ed24f4bSMarc Zyngier switch (kvm_psci_version(vcpu, vcpu->kvm)) { 3879ed24f4bSMarc Zyngier case KVM_ARM_PSCI_1_0: 3889ed24f4bSMarc Zyngier return kvm_psci_1_0_call(vcpu); 3899ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_2: 3909ed24f4bSMarc Zyngier return kvm_psci_0_2_call(vcpu); 3919ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_1: 3929ed24f4bSMarc Zyngier return kvm_psci_0_1_call(vcpu); 3939ed24f4bSMarc Zyngier default: 3949ed24f4bSMarc Zyngier return -EINVAL; 3959ed24f4bSMarc Zyngier }; 3969ed24f4bSMarc Zyngier } 3979ed24f4bSMarc Zyngier 3989ed24f4bSMarc Zyngier int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu) 3999ed24f4bSMarc Zyngier { 4009ed24f4bSMarc Zyngier return 3; /* PSCI version and two workaround registers */ 4019ed24f4bSMarc Zyngier } 4029ed24f4bSMarc Zyngier 4039ed24f4bSMarc Zyngier int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) 4049ed24f4bSMarc Zyngier { 4059ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++)) 4069ed24f4bSMarc Zyngier return -EFAULT; 4079ed24f4bSMarc Zyngier 4089ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++)) 4099ed24f4bSMarc Zyngier return -EFAULT; 4109ed24f4bSMarc Zyngier 4119ed24f4bSMarc Zyngier if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++)) 4129ed24f4bSMarc Zyngier return -EFAULT; 4139ed24f4bSMarc Zyngier 4149ed24f4bSMarc Zyngier return 0; 4159ed24f4bSMarc Zyngier } 4169ed24f4bSMarc Zyngier 4179ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_WIDTH 4 4189ed24f4bSMarc Zyngier #define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1) 4199ed24f4bSMarc Zyngier 4209ed24f4bSMarc Zyngier /* 4219ed24f4bSMarc Zyngier * Convert the workaround level into an easy-to-compare number, where higher 4229ed24f4bSMarc Zyngier * values mean better protection. 4239ed24f4bSMarc Zyngier */ 4249ed24f4bSMarc Zyngier static int get_kernel_wa_level(u64 regid) 4259ed24f4bSMarc Zyngier { 4269ed24f4bSMarc Zyngier switch (regid) { 4279ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 428d4647f0aSWill Deacon switch (arm64_get_spectre_v2_state()) { 429d4647f0aSWill Deacon case SPECTRE_VULNERABLE: 4309ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; 431d4647f0aSWill Deacon case SPECTRE_MITIGATED: 4329ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL; 433d4647f0aSWill Deacon case SPECTRE_UNAFFECTED: 4349ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED; 4359ed24f4bSMarc Zyngier } 4369ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL; 4379ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 43829e8910aSMarc Zyngier switch (arm64_get_ssbd_state()) { 43929e8910aSMarc Zyngier case ARM64_SSBD_FORCE_ENABLE: 44029e8910aSMarc Zyngier case ARM64_SSBD_MITIGATED: 44129e8910aSMarc Zyngier case ARM64_SSBD_KERNEL: 4429ed24f4bSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; 44329e8910aSMarc Zyngier case ARM64_SSBD_UNKNOWN: 44429e8910aSMarc Zyngier case ARM64_SSBD_FORCE_DISABLE: 4459ed24f4bSMarc Zyngier default: 44629e8910aSMarc Zyngier return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 4479ed24f4bSMarc Zyngier } 4489ed24f4bSMarc Zyngier } 4499ed24f4bSMarc Zyngier 4509ed24f4bSMarc Zyngier return -EINVAL; 4519ed24f4bSMarc Zyngier } 4529ed24f4bSMarc Zyngier 4539ed24f4bSMarc Zyngier int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 4549ed24f4bSMarc Zyngier { 4559ed24f4bSMarc Zyngier void __user *uaddr = (void __user *)(long)reg->addr; 4569ed24f4bSMarc Zyngier u64 val; 4579ed24f4bSMarc Zyngier 4589ed24f4bSMarc Zyngier switch (reg->id) { 4599ed24f4bSMarc Zyngier case KVM_REG_ARM_PSCI_VERSION: 4609ed24f4bSMarc Zyngier val = kvm_psci_version(vcpu, vcpu->kvm); 4619ed24f4bSMarc Zyngier break; 4629ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 4639ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 4649ed24f4bSMarc Zyngier val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK; 4659ed24f4bSMarc Zyngier break; 4669ed24f4bSMarc Zyngier default: 4679ed24f4bSMarc Zyngier return -ENOENT; 4689ed24f4bSMarc Zyngier } 4699ed24f4bSMarc Zyngier 4709ed24f4bSMarc Zyngier if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id))) 4719ed24f4bSMarc Zyngier return -EFAULT; 4729ed24f4bSMarc Zyngier 4739ed24f4bSMarc Zyngier return 0; 4749ed24f4bSMarc Zyngier } 4759ed24f4bSMarc Zyngier 4769ed24f4bSMarc Zyngier int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 4779ed24f4bSMarc Zyngier { 4789ed24f4bSMarc Zyngier void __user *uaddr = (void __user *)(long)reg->addr; 4799ed24f4bSMarc Zyngier u64 val; 4809ed24f4bSMarc Zyngier int wa_level; 4819ed24f4bSMarc Zyngier 4829ed24f4bSMarc Zyngier if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) 4839ed24f4bSMarc Zyngier return -EFAULT; 4849ed24f4bSMarc Zyngier 4859ed24f4bSMarc Zyngier switch (reg->id) { 4869ed24f4bSMarc Zyngier case KVM_REG_ARM_PSCI_VERSION: 4879ed24f4bSMarc Zyngier { 4889ed24f4bSMarc Zyngier bool wants_02; 4899ed24f4bSMarc Zyngier 4909ed24f4bSMarc Zyngier wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features); 4919ed24f4bSMarc Zyngier 4929ed24f4bSMarc Zyngier switch (val) { 4939ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_1: 4949ed24f4bSMarc Zyngier if (wants_02) 4959ed24f4bSMarc Zyngier return -EINVAL; 4969ed24f4bSMarc Zyngier vcpu->kvm->arch.psci_version = val; 4979ed24f4bSMarc Zyngier return 0; 4989ed24f4bSMarc Zyngier case KVM_ARM_PSCI_0_2: 4999ed24f4bSMarc Zyngier case KVM_ARM_PSCI_1_0: 5009ed24f4bSMarc Zyngier if (!wants_02) 5019ed24f4bSMarc Zyngier return -EINVAL; 5029ed24f4bSMarc Zyngier vcpu->kvm->arch.psci_version = val; 5039ed24f4bSMarc Zyngier return 0; 5049ed24f4bSMarc Zyngier } 5059ed24f4bSMarc Zyngier break; 5069ed24f4bSMarc Zyngier } 5079ed24f4bSMarc Zyngier 5089ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: 5099ed24f4bSMarc Zyngier if (val & ~KVM_REG_FEATURE_LEVEL_MASK) 5109ed24f4bSMarc Zyngier return -EINVAL; 5119ed24f4bSMarc Zyngier 5129ed24f4bSMarc Zyngier if (get_kernel_wa_level(reg->id) < val) 5139ed24f4bSMarc Zyngier return -EINVAL; 5149ed24f4bSMarc Zyngier 5159ed24f4bSMarc Zyngier return 0; 5169ed24f4bSMarc Zyngier 5179ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: 5189ed24f4bSMarc Zyngier if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | 5199ed24f4bSMarc Zyngier KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED)) 5209ed24f4bSMarc Zyngier return -EINVAL; 5219ed24f4bSMarc Zyngier 5229ed24f4bSMarc Zyngier /* The enabled bit must not be set unless the level is AVAIL. */ 52329e8910aSMarc Zyngier if ((val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED) && 52429e8910aSMarc Zyngier (val & KVM_REG_FEATURE_LEVEL_MASK) != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL) 5259ed24f4bSMarc Zyngier return -EINVAL; 5269ed24f4bSMarc Zyngier 5279ed24f4bSMarc Zyngier /* 52829e8910aSMarc Zyngier * Map all the possible incoming states to the only two we 52929e8910aSMarc Zyngier * really want to deal with. 5309ed24f4bSMarc Zyngier */ 53129e8910aSMarc Zyngier switch (val & KVM_REG_FEATURE_LEVEL_MASK) { 53229e8910aSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL: 53329e8910aSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN: 53429e8910aSMarc Zyngier wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL; 53529e8910aSMarc Zyngier break; 5369ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL: 5379ed24f4bSMarc Zyngier case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED: 53829e8910aSMarc Zyngier wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED; 5399ed24f4bSMarc Zyngier break; 54029e8910aSMarc Zyngier default: 54129e8910aSMarc Zyngier return -EINVAL; 5429ed24f4bSMarc Zyngier } 5439ed24f4bSMarc Zyngier 54429e8910aSMarc Zyngier /* 54529e8910aSMarc Zyngier * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the 54629e8910aSMarc Zyngier * other way around. 54729e8910aSMarc Zyngier */ 54829e8910aSMarc Zyngier if (get_kernel_wa_level(reg->id) < wa_level) 54929e8910aSMarc Zyngier return -EINVAL; 55029e8910aSMarc Zyngier 5519ed24f4bSMarc Zyngier return 0; 5529ed24f4bSMarc Zyngier default: 5539ed24f4bSMarc Zyngier return -ENOENT; 5549ed24f4bSMarc Zyngier } 5559ed24f4bSMarc Zyngier 5569ed24f4bSMarc Zyngier return -EINVAL; 5579ed24f4bSMarc Zyngier } 558