1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <assert.h> 8*91f16700Schasinglulu #include <stddef.h> 9*91f16700Schasinglulu 10*91f16700Schasinglulu #include <arch.h> 11*91f16700Schasinglulu #include <arch_helpers.h> 12*91f16700Schasinglulu #include <common/bl_common.h> 13*91f16700Schasinglulu #include <common/debug.h> 14*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h> 15*91f16700Schasinglulu #include <lib/el3_runtime/pubsub_events.h> 16*91f16700Schasinglulu #include <plat/common/platform.h> 17*91f16700Schasinglulu 18*91f16700Schasinglulu #include "psci_private.h" 19*91f16700Schasinglulu 20*91f16700Schasinglulu /* 21*91f16700Schasinglulu * Helper functions for the CPU level spinlocks 22*91f16700Schasinglulu */ 23*91f16700Schasinglulu static inline void psci_spin_lock_cpu(unsigned int idx) 24*91f16700Schasinglulu { 25*91f16700Schasinglulu spin_lock(&psci_cpu_pd_nodes[idx].cpu_lock); 26*91f16700Schasinglulu } 27*91f16700Schasinglulu 28*91f16700Schasinglulu static inline void psci_spin_unlock_cpu(unsigned int idx) 29*91f16700Schasinglulu { 30*91f16700Schasinglulu spin_unlock(&psci_cpu_pd_nodes[idx].cpu_lock); 31*91f16700Schasinglulu } 32*91f16700Schasinglulu 33*91f16700Schasinglulu /******************************************************************************* 34*91f16700Schasinglulu * This function checks whether a cpu which has been requested to be turned on 35*91f16700Schasinglulu * is OFF to begin with. 36*91f16700Schasinglulu ******************************************************************************/ 37*91f16700Schasinglulu static int cpu_on_validate_state(aff_info_state_t aff_state) 38*91f16700Schasinglulu { 39*91f16700Schasinglulu if (aff_state == AFF_STATE_ON) 40*91f16700Schasinglulu return PSCI_E_ALREADY_ON; 41*91f16700Schasinglulu 42*91f16700Schasinglulu if (aff_state == AFF_STATE_ON_PENDING) 43*91f16700Schasinglulu return PSCI_E_ON_PENDING; 44*91f16700Schasinglulu 45*91f16700Schasinglulu assert(aff_state == AFF_STATE_OFF); 46*91f16700Schasinglulu return PSCI_E_SUCCESS; 47*91f16700Schasinglulu } 48*91f16700Schasinglulu 49*91f16700Schasinglulu /******************************************************************************* 50*91f16700Schasinglulu * Generic handler which is called to physically power on a cpu identified by 51*91f16700Schasinglulu * its mpidr. It performs the generic, architectural, platform setup and state 52*91f16700Schasinglulu * management to power on the target cpu e.g. it will ensure that 53*91f16700Schasinglulu * enough information is stashed for it to resume execution in the non-secure 54*91f16700Schasinglulu * security state. 55*91f16700Schasinglulu * 56*91f16700Schasinglulu * The state of all the relevant power domains are changed after calling the 57*91f16700Schasinglulu * platform handler as it can return error. 58*91f16700Schasinglulu ******************************************************************************/ 59*91f16700Schasinglulu int psci_cpu_on_start(u_register_t target_cpu, 60*91f16700Schasinglulu const entry_point_info_t *ep) 61*91f16700Schasinglulu { 62*91f16700Schasinglulu int rc; 63*91f16700Schasinglulu aff_info_state_t target_aff_state; 64*91f16700Schasinglulu unsigned int target_idx = (unsigned int)plat_core_pos_by_mpidr(target_cpu); 65*91f16700Schasinglulu 66*91f16700Schasinglulu /* 67*91f16700Schasinglulu * This function must only be called on platforms where the 68*91f16700Schasinglulu * CPU_ON platform hooks have been implemented. 69*91f16700Schasinglulu */ 70*91f16700Schasinglulu assert((psci_plat_pm_ops->pwr_domain_on != NULL) && 71*91f16700Schasinglulu (psci_plat_pm_ops->pwr_domain_on_finish != NULL)); 72*91f16700Schasinglulu 73*91f16700Schasinglulu /* Protect against multiple CPUs trying to turn ON the same target CPU */ 74*91f16700Schasinglulu psci_spin_lock_cpu(target_idx); 75*91f16700Schasinglulu 76*91f16700Schasinglulu /* 77*91f16700Schasinglulu * Generic management: Ensure that the cpu is off to be 78*91f16700Schasinglulu * turned on. 79*91f16700Schasinglulu * Perform cache maintanence ahead of reading the target CPU state to 80*91f16700Schasinglulu * ensure that the data is not stale. 81*91f16700Schasinglulu * There is a theoretical edge case where the cache may contain stale 82*91f16700Schasinglulu * data for the target CPU data - this can occur under the following 83*91f16700Schasinglulu * conditions: 84*91f16700Schasinglulu * - the target CPU is in another cluster from the current 85*91f16700Schasinglulu * - the target CPU was the last CPU to shutdown on its cluster 86*91f16700Schasinglulu * - the cluster was removed from coherency as part of the CPU shutdown 87*91f16700Schasinglulu * 88*91f16700Schasinglulu * In this case the cache maintenace that was performed as part of the 89*91f16700Schasinglulu * target CPUs shutdown was not seen by the current CPU's cluster. And 90*91f16700Schasinglulu * so the cache may contain stale data for the target CPU. 91*91f16700Schasinglulu */ 92*91f16700Schasinglulu flush_cpu_data_by_index(target_idx, 93*91f16700Schasinglulu psci_svc_cpu_data.aff_info_state); 94*91f16700Schasinglulu rc = cpu_on_validate_state(psci_get_aff_info_state_by_idx(target_idx)); 95*91f16700Schasinglulu if (rc != PSCI_E_SUCCESS) 96*91f16700Schasinglulu goto exit; 97*91f16700Schasinglulu 98*91f16700Schasinglulu /* 99*91f16700Schasinglulu * Call the cpu on handler registered by the Secure Payload Dispatcher 100*91f16700Schasinglulu * to let it do any bookeeping. If the handler encounters an error, it's 101*91f16700Schasinglulu * expected to assert within 102*91f16700Schasinglulu */ 103*91f16700Schasinglulu if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_on != NULL)) 104*91f16700Schasinglulu psci_spd_pm->svc_on(target_cpu); 105*91f16700Schasinglulu 106*91f16700Schasinglulu /* 107*91f16700Schasinglulu * Set the Affinity info state of the target cpu to ON_PENDING. 108*91f16700Schasinglulu * Flush aff_info_state as it will be accessed with caches 109*91f16700Schasinglulu * turned OFF. 110*91f16700Schasinglulu */ 111*91f16700Schasinglulu psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING); 112*91f16700Schasinglulu flush_cpu_data_by_index(target_idx, 113*91f16700Schasinglulu psci_svc_cpu_data.aff_info_state); 114*91f16700Schasinglulu 115*91f16700Schasinglulu /* 116*91f16700Schasinglulu * The cache line invalidation by the target CPU after setting the 117*91f16700Schasinglulu * state to OFF (see psci_do_cpu_off()), could cause the update to 118*91f16700Schasinglulu * aff_info_state to be invalidated. Retry the update if the target 119*91f16700Schasinglulu * CPU aff_info_state is not ON_PENDING. 120*91f16700Schasinglulu */ 121*91f16700Schasinglulu target_aff_state = psci_get_aff_info_state_by_idx(target_idx); 122*91f16700Schasinglulu if (target_aff_state != AFF_STATE_ON_PENDING) { 123*91f16700Schasinglulu assert(target_aff_state == AFF_STATE_OFF); 124*91f16700Schasinglulu psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING); 125*91f16700Schasinglulu flush_cpu_data_by_index(target_idx, 126*91f16700Schasinglulu psci_svc_cpu_data.aff_info_state); 127*91f16700Schasinglulu 128*91f16700Schasinglulu assert(psci_get_aff_info_state_by_idx(target_idx) == 129*91f16700Schasinglulu AFF_STATE_ON_PENDING); 130*91f16700Schasinglulu } 131*91f16700Schasinglulu 132*91f16700Schasinglulu /* 133*91f16700Schasinglulu * Perform generic, architecture and platform specific handling. 134*91f16700Schasinglulu */ 135*91f16700Schasinglulu /* 136*91f16700Schasinglulu * Plat. management: Give the platform the current state 137*91f16700Schasinglulu * of the target cpu to allow it to perform the necessary 138*91f16700Schasinglulu * steps to power on. 139*91f16700Schasinglulu */ 140*91f16700Schasinglulu rc = psci_plat_pm_ops->pwr_domain_on(target_cpu); 141*91f16700Schasinglulu assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL)); 142*91f16700Schasinglulu 143*91f16700Schasinglulu if (rc == PSCI_E_SUCCESS) 144*91f16700Schasinglulu /* Store the re-entry information for the non-secure world. */ 145*91f16700Schasinglulu cm_init_context_by_index(target_idx, ep); 146*91f16700Schasinglulu else { 147*91f16700Schasinglulu /* Restore the state on error. */ 148*91f16700Schasinglulu psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_OFF); 149*91f16700Schasinglulu flush_cpu_data_by_index(target_idx, 150*91f16700Schasinglulu psci_svc_cpu_data.aff_info_state); 151*91f16700Schasinglulu } 152*91f16700Schasinglulu 153*91f16700Schasinglulu exit: 154*91f16700Schasinglulu psci_spin_unlock_cpu(target_idx); 155*91f16700Schasinglulu return rc; 156*91f16700Schasinglulu } 157*91f16700Schasinglulu 158*91f16700Schasinglulu /******************************************************************************* 159*91f16700Schasinglulu * The following function finish an earlier power on request. They 160*91f16700Schasinglulu * are called by the common finisher routine in psci_common.c. The `state_info` 161*91f16700Schasinglulu * is the psci_power_state from which this CPU has woken up from. 162*91f16700Schasinglulu ******************************************************************************/ 163*91f16700Schasinglulu void psci_cpu_on_finish(unsigned int cpu_idx, const psci_power_state_t *state_info) 164*91f16700Schasinglulu { 165*91f16700Schasinglulu /* 166*91f16700Schasinglulu * Plat. management: Perform the platform specific actions 167*91f16700Schasinglulu * for this cpu e.g. enabling the gic or zeroing the mailbox 168*91f16700Schasinglulu * register. The actual state of this cpu has already been 169*91f16700Schasinglulu * changed. 170*91f16700Schasinglulu */ 171*91f16700Schasinglulu psci_plat_pm_ops->pwr_domain_on_finish(state_info); 172*91f16700Schasinglulu 173*91f16700Schasinglulu #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 174*91f16700Schasinglulu /* 175*91f16700Schasinglulu * Arch. management: Enable data cache and manage stack memory 176*91f16700Schasinglulu */ 177*91f16700Schasinglulu psci_do_pwrup_cache_maintenance(); 178*91f16700Schasinglulu #endif 179*91f16700Schasinglulu 180*91f16700Schasinglulu /* 181*91f16700Schasinglulu * Plat. management: Perform any platform specific actions which 182*91f16700Schasinglulu * can only be done with the cpu and the cluster guaranteed to 183*91f16700Schasinglulu * be coherent. 184*91f16700Schasinglulu */ 185*91f16700Schasinglulu if (psci_plat_pm_ops->pwr_domain_on_finish_late != NULL) 186*91f16700Schasinglulu psci_plat_pm_ops->pwr_domain_on_finish_late(state_info); 187*91f16700Schasinglulu 188*91f16700Schasinglulu /* 189*91f16700Schasinglulu * All the platform specific actions for turning this cpu 190*91f16700Schasinglulu * on have completed. Perform enough arch.initialization 191*91f16700Schasinglulu * to run in the non-secure address space. 192*91f16700Schasinglulu */ 193*91f16700Schasinglulu psci_arch_setup(); 194*91f16700Schasinglulu 195*91f16700Schasinglulu /* 196*91f16700Schasinglulu * Lock the CPU spin lock to make sure that the context initialization 197*91f16700Schasinglulu * is done. Since the lock is only used in this function to create 198*91f16700Schasinglulu * a synchronization point with cpu_on_start(), it can be released 199*91f16700Schasinglulu * immediately. 200*91f16700Schasinglulu */ 201*91f16700Schasinglulu psci_spin_lock_cpu(cpu_idx); 202*91f16700Schasinglulu psci_spin_unlock_cpu(cpu_idx); 203*91f16700Schasinglulu 204*91f16700Schasinglulu /* Ensure we have been explicitly woken up by another cpu */ 205*91f16700Schasinglulu assert(psci_get_aff_info_state() == AFF_STATE_ON_PENDING); 206*91f16700Schasinglulu 207*91f16700Schasinglulu /* 208*91f16700Schasinglulu * Call the cpu on finish handler registered by the Secure Payload 209*91f16700Schasinglulu * Dispatcher to let it do any bookeeping. If the handler encounters an 210*91f16700Schasinglulu * error, it's expected to assert within 211*91f16700Schasinglulu */ 212*91f16700Schasinglulu if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_on_finish != NULL)) 213*91f16700Schasinglulu psci_spd_pm->svc_on_finish(0); 214*91f16700Schasinglulu 215*91f16700Schasinglulu PUBLISH_EVENT(psci_cpu_on_finish); 216*91f16700Schasinglulu 217*91f16700Schasinglulu /* Populate the mpidr field within the cpu node array */ 218*91f16700Schasinglulu /* This needs to be done only once */ 219*91f16700Schasinglulu psci_cpu_pd_nodes[cpu_idx].mpidr = read_mpidr() & MPIDR_AFFINITY_MASK; 220*91f16700Schasinglulu } 221