xref: /qemu/target/arm/tcg/psci.c (revision 58803318e5a546b2eb0efd7a053ed36b6c29ae6f)
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  */
1874c21bd0SPeter Maydell #include "qemu/osdep.h"
19a9c94277SMarkus Armbruster #include "cpu.h"
20a9c94277SMarkus Armbruster #include "exec/helper-proto.h"
21a9c94277SMarkus Armbruster #include "kvm-consts.h"
22a9c94277SMarkus Armbruster #include "sysemu/sysemu.h"
2398128601SRob Herring #include "internals.h"
24825482adSJean-Christophe DUBOIS #include "arm-powerctl.h"
2563c91552SPaolo Bonzini #include "exec/exec-all.h"
2698128601SRob Herring 
2798128601SRob Herring bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
2898128601SRob Herring {
2998128601SRob Herring     /* Return true if the r0/x0 value indicates a PSCI call and
3098128601SRob Herring      * the exception type matches the configured PSCI conduit. This is
3198128601SRob Herring      * called before the SMC/HVC instruction is executed, to decide whether
3298128601SRob Herring      * we should treat it as a PSCI call or with the architecturally
3398128601SRob Herring      * defined behaviour for an SMC or HVC (which might be UNDEF or trap
3498128601SRob Herring      * to EL2 or to EL3).
3598128601SRob Herring      */
3698128601SRob Herring     CPUARMState *env = &cpu->env;
3798128601SRob Herring     uint64_t param = is_a64(env) ? env->xregs[0] : env->regs[0];
3898128601SRob Herring 
3998128601SRob Herring     switch (excp_type) {
4098128601SRob Herring     case EXCP_HVC:
4198128601SRob Herring         if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_HVC) {
4298128601SRob Herring             return false;
4398128601SRob Herring         }
4498128601SRob Herring         break;
4598128601SRob Herring     case EXCP_SMC:
4698128601SRob Herring         if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
4798128601SRob Herring             return false;
4898128601SRob Herring         }
4998128601SRob Herring         break;
5098128601SRob Herring     default:
5198128601SRob Herring         return false;
5298128601SRob Herring     }
5398128601SRob Herring 
5498128601SRob Herring     switch (param) {
5598128601SRob Herring     case QEMU_PSCI_0_2_FN_PSCI_VERSION:
5698128601SRob Herring     case QEMU_PSCI_0_2_FN_MIGRATE_INFO_TYPE:
5798128601SRob Herring     case QEMU_PSCI_0_2_FN_AFFINITY_INFO:
5898128601SRob Herring     case QEMU_PSCI_0_2_FN64_AFFINITY_INFO:
5998128601SRob Herring     case QEMU_PSCI_0_2_FN_SYSTEM_RESET:
6098128601SRob Herring     case QEMU_PSCI_0_2_FN_SYSTEM_OFF:
6198128601SRob Herring     case QEMU_PSCI_0_1_FN_CPU_ON:
6298128601SRob Herring     case QEMU_PSCI_0_2_FN_CPU_ON:
6398128601SRob Herring     case QEMU_PSCI_0_2_FN64_CPU_ON:
6498128601SRob Herring     case QEMU_PSCI_0_1_FN_CPU_OFF:
6598128601SRob Herring     case QEMU_PSCI_0_2_FN_CPU_OFF:
6698128601SRob Herring     case QEMU_PSCI_0_1_FN_CPU_SUSPEND:
6798128601SRob Herring     case QEMU_PSCI_0_2_FN_CPU_SUSPEND:
6898128601SRob Herring     case QEMU_PSCI_0_2_FN64_CPU_SUSPEND:
6998128601SRob Herring     case QEMU_PSCI_0_1_FN_MIGRATE:
7098128601SRob Herring     case QEMU_PSCI_0_2_FN_MIGRATE:
7198128601SRob Herring         return true;
7298128601SRob Herring     default:
7398128601SRob Herring         return false;
7498128601SRob Herring     }
7598128601SRob Herring }
7698128601SRob Herring 
7798128601SRob Herring void arm_handle_psci_call(ARMCPU *cpu)
7898128601SRob Herring {
7998128601SRob Herring     /*
8098128601SRob Herring      * This function partially implements the logic for dispatching Power State
8198128601SRob Herring      * Coordination Interface (PSCI) calls (as described in ARM DEN 0022B.b),
8298128601SRob Herring      * to the extent required for bringing up and taking down secondary cores,
8398128601SRob Herring      * and for handling reset and poweroff requests.
8498128601SRob Herring      * Additional information about the calling convention used is available in
8598128601SRob Herring      * the document 'SMC Calling Convention' (ARM DEN 0028)
8698128601SRob Herring      */
8798128601SRob Herring     CPUARMState *env = &cpu->env;
8898128601SRob Herring     uint64_t param[4];
8998128601SRob Herring     uint64_t context_id, mpidr;
9098128601SRob Herring     target_ulong entry;
9198128601SRob Herring     int32_t ret = 0;
9298128601SRob Herring     int i;
9398128601SRob Herring 
9498128601SRob Herring     for (i = 0; i < 4; i++) {
9598128601SRob Herring         /*
9698128601SRob Herring          * All PSCI functions take explicit 32-bit or native int sized
9798128601SRob Herring          * arguments so we can simply zero-extend all arguments regardless
9898128601SRob Herring          * of which exact function we are about to call.
9998128601SRob Herring          */
10098128601SRob Herring         param[i] = is_a64(env) ? env->xregs[i] : env->regs[i];
10198128601SRob Herring     }
10298128601SRob Herring 
10398128601SRob Herring     if ((param[0] & QEMU_PSCI_0_2_64BIT) && !is_a64(env)) {
10498128601SRob Herring         ret = QEMU_PSCI_RET_INVALID_PARAMS;
10598128601SRob Herring         goto err;
10698128601SRob Herring     }
10798128601SRob Herring 
10898128601SRob Herring     switch (param[0]) {
10998128601SRob Herring         CPUState *target_cpu_state;
11098128601SRob Herring         ARMCPU *target_cpu;
11198128601SRob Herring 
11298128601SRob Herring     case QEMU_PSCI_0_2_FN_PSCI_VERSION:
11398128601SRob Herring         ret = QEMU_PSCI_0_2_RET_VERSION_0_2;
11498128601SRob Herring         break;
11598128601SRob Herring     case QEMU_PSCI_0_2_FN_MIGRATE_INFO_TYPE:
11698128601SRob Herring         ret = QEMU_PSCI_0_2_RET_TOS_MIGRATION_NOT_REQUIRED; /* No trusted OS */
11798128601SRob Herring         break;
11898128601SRob Herring     case QEMU_PSCI_0_2_FN_AFFINITY_INFO:
11998128601SRob Herring     case QEMU_PSCI_0_2_FN64_AFFINITY_INFO:
12098128601SRob Herring         mpidr = param[1];
12198128601SRob Herring 
12298128601SRob Herring         switch (param[2]) {
12398128601SRob Herring         case 0:
124825482adSJean-Christophe DUBOIS             target_cpu_state = arm_get_cpu_by_id(mpidr);
12598128601SRob Herring             if (!target_cpu_state) {
12698128601SRob Herring                 ret = QEMU_PSCI_RET_INVALID_PARAMS;
12798128601SRob Herring                 break;
12898128601SRob Herring             }
12998128601SRob Herring             target_cpu = ARM_CPU(target_cpu_state);
130062ba099SAlex Bennée 
131062ba099SAlex Bennée             g_assert(qemu_mutex_iothread_locked());
132062ba099SAlex Bennée             ret = target_cpu->power_state;
13398128601SRob Herring             break;
13498128601SRob Herring         default:
13598128601SRob Herring             /* Everything above affinity level 0 is always on. */
13698128601SRob Herring             ret = 0;
13798128601SRob Herring         }
13898128601SRob Herring         break;
13998128601SRob Herring     case QEMU_PSCI_0_2_FN_SYSTEM_RESET:
140cf83f140SEric Blake         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
14198128601SRob Herring         /* QEMU reset and shutdown are async requests, but PSCI
14298128601SRob Herring          * mandates that we never return from the reset/shutdown
14398128601SRob Herring          * call, so power the CPU off now so it doesn't execute
14498128601SRob Herring          * anything further.
14598128601SRob Herring          */
14698128601SRob Herring         goto cpu_off;
14798128601SRob Herring     case QEMU_PSCI_0_2_FN_SYSTEM_OFF:
148cf83f140SEric Blake         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
14998128601SRob Herring         goto cpu_off;
15098128601SRob Herring     case QEMU_PSCI_0_1_FN_CPU_ON:
15198128601SRob Herring     case QEMU_PSCI_0_2_FN_CPU_ON:
15298128601SRob Herring     case QEMU_PSCI_0_2_FN64_CPU_ON:
1533f591a20SPeter Maydell     {
1543f591a20SPeter Maydell         /* The PSCI spec mandates that newly brought up CPUs start
1553f591a20SPeter Maydell          * in the highest exception level which exists and is enabled
1563f591a20SPeter Maydell          * on the calling CPU. Since the QEMU PSCI implementation is
1573f591a20SPeter Maydell          * acting as a "fake EL3" or "fake EL2" firmware, this for us
1583f591a20SPeter Maydell          * means that we want to start at the highest NS exception level
1593f591a20SPeter Maydell          * that we are providing to the guest.
1603f591a20SPeter Maydell          * The execution mode should be that which is currently in use
1613f591a20SPeter Maydell          * by the same exception level on the calling CPU.
1623f591a20SPeter Maydell          * The CPU should be started with the context_id value
1633f591a20SPeter Maydell          * in x0 (if AArch64) or r0 (if AArch32).
1643f591a20SPeter Maydell          */
1653f591a20SPeter Maydell         int target_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1;
1663f591a20SPeter Maydell         bool target_aarch64 = arm_el_is_aa64(env, target_el);
1673f591a20SPeter Maydell 
16898128601SRob Herring         mpidr = param[1];
16998128601SRob Herring         entry = param[2];
17098128601SRob Herring         context_id = param[3];
1713f591a20SPeter Maydell         ret = arm_set_cpu_on(mpidr, entry, context_id,
1723f591a20SPeter Maydell                              target_el, target_aarch64);
17398128601SRob Herring         break;
1743f591a20SPeter Maydell     }
17598128601SRob Herring     case QEMU_PSCI_0_1_FN_CPU_OFF:
17698128601SRob Herring     case QEMU_PSCI_0_2_FN_CPU_OFF:
17798128601SRob Herring         goto cpu_off;
17898128601SRob Herring     case QEMU_PSCI_0_1_FN_CPU_SUSPEND:
17998128601SRob Herring     case QEMU_PSCI_0_2_FN_CPU_SUSPEND:
18098128601SRob Herring     case QEMU_PSCI_0_2_FN64_CPU_SUSPEND:
18198128601SRob Herring         /* Affinity levels are not supported in QEMU */
18298128601SRob Herring         if (param[1] & 0xfffe0000) {
18398128601SRob Herring             ret = QEMU_PSCI_RET_INVALID_PARAMS;
18498128601SRob Herring             break;
18598128601SRob Herring         }
18698128601SRob Herring         /* Powerdown is not supported, we always go into WFI */
18798128601SRob Herring         if (is_a64(env)) {
18898128601SRob Herring             env->xregs[0] = 0;
18998128601SRob Herring         } else {
19098128601SRob Herring             env->regs[0] = 0;
19198128601SRob Herring         }
192*58803318SStefano Stabellini         helper_wfi(env, 4);
19398128601SRob Herring         break;
19498128601SRob Herring     case QEMU_PSCI_0_1_FN_MIGRATE:
19598128601SRob Herring     case QEMU_PSCI_0_2_FN_MIGRATE:
19698128601SRob Herring         ret = QEMU_PSCI_RET_NOT_SUPPORTED;
19798128601SRob Herring         break;
19898128601SRob Herring     default:
19998128601SRob Herring         g_assert_not_reached();
20098128601SRob Herring     }
20198128601SRob Herring 
20298128601SRob Herring err:
20398128601SRob Herring     if (is_a64(env)) {
20498128601SRob Herring         env->xregs[0] = ret;
20598128601SRob Herring     } else {
20698128601SRob Herring         env->regs[0] = ret;
20798128601SRob Herring     }
20898128601SRob Herring     return;
20998128601SRob Herring 
21098128601SRob Herring cpu_off:
211825482adSJean-Christophe DUBOIS     ret = arm_set_cpu_off(cpu->mp_affinity);
21298128601SRob Herring     /* notreached */
213825482adSJean-Christophe DUBOIS     /* sanity check in case something failed */
214825482adSJean-Christophe DUBOIS     assert(ret == QEMU_ARM_POWERCTL_RET_SUCCESS);
21598128601SRob Herring }
216