198128601SRob Herring /* 298128601SRob Herring * Copyright (C) 2014 - Linaro 398128601SRob Herring * Author: Rob Herring <rob.herring@linaro.org> 498128601SRob Herring * 598128601SRob Herring * This program is free software; you can redistribute it and/or modify 698128601SRob Herring * it under the terms of the GNU General Public License as published by 798128601SRob Herring * the Free Software Foundation; either version 2 of the License, or 898128601SRob Herring * (at your option) any later version. 998128601SRob Herring * 1098128601SRob Herring * This program is distributed in the hope that it will be useful, 1198128601SRob Herring * but WITHOUT ANY WARRANTY; without even the implied warranty of 1298128601SRob Herring * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1398128601SRob Herring * GNU General Public License for more details. 1498128601SRob Herring * 1598128601SRob Herring * You should have received a copy of the GNU General Public License 1698128601SRob Herring * along with this program; if not, see <http://www.gnu.org/licenses/>. 1798128601SRob Herring */ 18db725815SMarkus Armbruster 1974c21bd0SPeter Maydell #include "qemu/osdep.h" 20a9c94277SMarkus Armbruster #include "cpu.h" 21a9c94277SMarkus Armbruster #include "exec/helper-proto.h" 22a9c94277SMarkus Armbruster #include "kvm-consts.h" 23db725815SMarkus Armbruster #include "qemu/main-loop.h" 24*54d31236SMarkus Armbruster #include "sysemu/runstate.h" 2598128601SRob Herring #include "internals.h" 26825482adSJean-Christophe DUBOIS #include "arm-powerctl.h" 2798128601SRob Herring 2898128601SRob Herring bool arm_is_psci_call(ARMCPU *cpu, int excp_type) 2998128601SRob Herring { 3098128601SRob Herring /* Return true if the r0/x0 value indicates a PSCI call and 3198128601SRob Herring * the exception type matches the configured PSCI conduit. This is 3298128601SRob Herring * called before the SMC/HVC instruction is executed, to decide whether 3398128601SRob Herring * we should treat it as a PSCI call or with the architecturally 3498128601SRob Herring * defined behaviour for an SMC or HVC (which might be UNDEF or trap 3598128601SRob Herring * to EL2 or to EL3). 3698128601SRob Herring */ 3798128601SRob Herring CPUARMState *env = &cpu->env; 3898128601SRob Herring uint64_t param = is_a64(env) ? env->xregs[0] : env->regs[0]; 3998128601SRob Herring 4098128601SRob Herring switch (excp_type) { 4198128601SRob Herring case EXCP_HVC: 4298128601SRob Herring if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_HVC) { 4398128601SRob Herring return false; 4498128601SRob Herring } 4598128601SRob Herring break; 4698128601SRob Herring case EXCP_SMC: 4798128601SRob Herring if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 4898128601SRob Herring return false; 4998128601SRob Herring } 5098128601SRob Herring break; 5198128601SRob Herring default: 5298128601SRob Herring return false; 5398128601SRob Herring } 5498128601SRob Herring 5598128601SRob Herring switch (param) { 5698128601SRob Herring case QEMU_PSCI_0_2_FN_PSCI_VERSION: 5798128601SRob Herring case QEMU_PSCI_0_2_FN_MIGRATE_INFO_TYPE: 5898128601SRob Herring case QEMU_PSCI_0_2_FN_AFFINITY_INFO: 5998128601SRob Herring case QEMU_PSCI_0_2_FN64_AFFINITY_INFO: 6098128601SRob Herring case QEMU_PSCI_0_2_FN_SYSTEM_RESET: 6198128601SRob Herring case QEMU_PSCI_0_2_FN_SYSTEM_OFF: 6298128601SRob Herring case QEMU_PSCI_0_1_FN_CPU_ON: 6398128601SRob Herring case QEMU_PSCI_0_2_FN_CPU_ON: 6498128601SRob Herring case QEMU_PSCI_0_2_FN64_CPU_ON: 6598128601SRob Herring case QEMU_PSCI_0_1_FN_CPU_OFF: 6698128601SRob Herring case QEMU_PSCI_0_2_FN_CPU_OFF: 6798128601SRob Herring case QEMU_PSCI_0_1_FN_CPU_SUSPEND: 6898128601SRob Herring case QEMU_PSCI_0_2_FN_CPU_SUSPEND: 6998128601SRob Herring case QEMU_PSCI_0_2_FN64_CPU_SUSPEND: 7098128601SRob Herring case QEMU_PSCI_0_1_FN_MIGRATE: 7198128601SRob Herring case QEMU_PSCI_0_2_FN_MIGRATE: 7298128601SRob Herring return true; 7398128601SRob Herring default: 7498128601SRob Herring return false; 7598128601SRob Herring } 7698128601SRob Herring } 7798128601SRob Herring 7898128601SRob Herring void arm_handle_psci_call(ARMCPU *cpu) 7998128601SRob Herring { 8098128601SRob Herring /* 8198128601SRob Herring * This function partially implements the logic for dispatching Power State 8298128601SRob Herring * Coordination Interface (PSCI) calls (as described in ARM DEN 0022B.b), 8398128601SRob Herring * to the extent required for bringing up and taking down secondary cores, 8498128601SRob Herring * and for handling reset and poweroff requests. 8598128601SRob Herring * Additional information about the calling convention used is available in 8698128601SRob Herring * the document 'SMC Calling Convention' (ARM DEN 0028) 8798128601SRob Herring */ 8898128601SRob Herring CPUARMState *env = &cpu->env; 8998128601SRob Herring uint64_t param[4]; 9098128601SRob Herring uint64_t context_id, mpidr; 9198128601SRob Herring target_ulong entry; 9298128601SRob Herring int32_t ret = 0; 9398128601SRob Herring int i; 9498128601SRob Herring 9598128601SRob Herring for (i = 0; i < 4; i++) { 9698128601SRob Herring /* 9798128601SRob Herring * All PSCI functions take explicit 32-bit or native int sized 9898128601SRob Herring * arguments so we can simply zero-extend all arguments regardless 9998128601SRob Herring * of which exact function we are about to call. 10098128601SRob Herring */ 10198128601SRob Herring param[i] = is_a64(env) ? env->xregs[i] : env->regs[i]; 10298128601SRob Herring } 10398128601SRob Herring 10498128601SRob Herring if ((param[0] & QEMU_PSCI_0_2_64BIT) && !is_a64(env)) { 10598128601SRob Herring ret = QEMU_PSCI_RET_INVALID_PARAMS; 10698128601SRob Herring goto err; 10798128601SRob Herring } 10898128601SRob Herring 10998128601SRob Herring switch (param[0]) { 11098128601SRob Herring CPUState *target_cpu_state; 11198128601SRob Herring ARMCPU *target_cpu; 11298128601SRob Herring 11398128601SRob Herring case QEMU_PSCI_0_2_FN_PSCI_VERSION: 11498128601SRob Herring ret = QEMU_PSCI_0_2_RET_VERSION_0_2; 11598128601SRob Herring break; 11698128601SRob Herring case QEMU_PSCI_0_2_FN_MIGRATE_INFO_TYPE: 11798128601SRob Herring ret = QEMU_PSCI_0_2_RET_TOS_MIGRATION_NOT_REQUIRED; /* No trusted OS */ 11898128601SRob Herring break; 11998128601SRob Herring case QEMU_PSCI_0_2_FN_AFFINITY_INFO: 12098128601SRob Herring case QEMU_PSCI_0_2_FN64_AFFINITY_INFO: 12198128601SRob Herring mpidr = param[1]; 12298128601SRob Herring 12398128601SRob Herring switch (param[2]) { 12498128601SRob Herring case 0: 125825482adSJean-Christophe DUBOIS target_cpu_state = arm_get_cpu_by_id(mpidr); 12698128601SRob Herring if (!target_cpu_state) { 12798128601SRob Herring ret = QEMU_PSCI_RET_INVALID_PARAMS; 12898128601SRob Herring break; 12998128601SRob Herring } 13098128601SRob Herring target_cpu = ARM_CPU(target_cpu_state); 131062ba099SAlex Bennée 132062ba099SAlex Bennée g_assert(qemu_mutex_iothread_locked()); 133062ba099SAlex Bennée ret = target_cpu->power_state; 13498128601SRob Herring break; 13598128601SRob Herring default: 13698128601SRob Herring /* Everything above affinity level 0 is always on. */ 13798128601SRob Herring ret = 0; 13898128601SRob Herring } 13998128601SRob Herring break; 14098128601SRob Herring case QEMU_PSCI_0_2_FN_SYSTEM_RESET: 141cf83f140SEric Blake qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 14298128601SRob Herring /* QEMU reset and shutdown are async requests, but PSCI 14398128601SRob Herring * mandates that we never return from the reset/shutdown 14498128601SRob Herring * call, so power the CPU off now so it doesn't execute 14598128601SRob Herring * anything further. 14698128601SRob Herring */ 14798128601SRob Herring goto cpu_off; 14898128601SRob Herring case QEMU_PSCI_0_2_FN_SYSTEM_OFF: 149cf83f140SEric Blake qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 15098128601SRob Herring goto cpu_off; 15198128601SRob Herring case QEMU_PSCI_0_1_FN_CPU_ON: 15298128601SRob Herring case QEMU_PSCI_0_2_FN_CPU_ON: 15398128601SRob Herring case QEMU_PSCI_0_2_FN64_CPU_ON: 1543f591a20SPeter Maydell { 1553f591a20SPeter Maydell /* The PSCI spec mandates that newly brought up CPUs start 1563f591a20SPeter Maydell * in the highest exception level which exists and is enabled 1573f591a20SPeter Maydell * on the calling CPU. Since the QEMU PSCI implementation is 1583f591a20SPeter Maydell * acting as a "fake EL3" or "fake EL2" firmware, this for us 1593f591a20SPeter Maydell * means that we want to start at the highest NS exception level 1603f591a20SPeter Maydell * that we are providing to the guest. 1613f591a20SPeter Maydell * The execution mode should be that which is currently in use 1623f591a20SPeter Maydell * by the same exception level on the calling CPU. 1633f591a20SPeter Maydell * The CPU should be started with the context_id value 1643f591a20SPeter Maydell * in x0 (if AArch64) or r0 (if AArch32). 1653f591a20SPeter Maydell */ 1663f591a20SPeter Maydell int target_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1; 1673f591a20SPeter Maydell bool target_aarch64 = arm_el_is_aa64(env, target_el); 1683f591a20SPeter Maydell 16998128601SRob Herring mpidr = param[1]; 17098128601SRob Herring entry = param[2]; 17198128601SRob Herring context_id = param[3]; 1723f591a20SPeter Maydell ret = arm_set_cpu_on(mpidr, entry, context_id, 1733f591a20SPeter Maydell target_el, target_aarch64); 17498128601SRob Herring break; 1753f591a20SPeter Maydell } 17698128601SRob Herring case QEMU_PSCI_0_1_FN_CPU_OFF: 17798128601SRob Herring case QEMU_PSCI_0_2_FN_CPU_OFF: 17898128601SRob Herring goto cpu_off; 17998128601SRob Herring case QEMU_PSCI_0_1_FN_CPU_SUSPEND: 18098128601SRob Herring case QEMU_PSCI_0_2_FN_CPU_SUSPEND: 18198128601SRob Herring case QEMU_PSCI_0_2_FN64_CPU_SUSPEND: 18298128601SRob Herring /* Affinity levels are not supported in QEMU */ 18398128601SRob Herring if (param[1] & 0xfffe0000) { 18498128601SRob Herring ret = QEMU_PSCI_RET_INVALID_PARAMS; 18598128601SRob Herring break; 18698128601SRob Herring } 18798128601SRob Herring /* Powerdown is not supported, we always go into WFI */ 18898128601SRob Herring if (is_a64(env)) { 18998128601SRob Herring env->xregs[0] = 0; 19098128601SRob Herring } else { 19198128601SRob Herring env->regs[0] = 0; 19298128601SRob Herring } 19358803318SStefano Stabellini helper_wfi(env, 4); 19498128601SRob Herring break; 19598128601SRob Herring case QEMU_PSCI_0_1_FN_MIGRATE: 19698128601SRob Herring case QEMU_PSCI_0_2_FN_MIGRATE: 19798128601SRob Herring ret = QEMU_PSCI_RET_NOT_SUPPORTED; 19898128601SRob Herring break; 19998128601SRob Herring default: 20098128601SRob Herring g_assert_not_reached(); 20198128601SRob Herring } 20298128601SRob Herring 20398128601SRob Herring err: 20498128601SRob Herring if (is_a64(env)) { 20598128601SRob Herring env->xregs[0] = ret; 20698128601SRob Herring } else { 20798128601SRob Herring env->regs[0] = ret; 20898128601SRob Herring } 20998128601SRob Herring return; 21098128601SRob Herring 21198128601SRob Herring cpu_off: 212825482adSJean-Christophe DUBOIS ret = arm_set_cpu_off(cpu->mp_affinity); 21398128601SRob Herring /* notreached */ 214825482adSJean-Christophe DUBOIS /* sanity check in case something failed */ 215825482adSJean-Christophe DUBOIS assert(ret == QEMU_ARM_POWERCTL_RET_SUCCESS); 21698128601SRob Herring } 217