1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2013-2023, 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 <string.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 <context.h> 15*91f16700Schasinglulu #include <drivers/delay_timer.h> 16*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h> 17*91f16700Schasinglulu #include <lib/utils.h> 18*91f16700Schasinglulu #include <plat/common/platform.h> 19*91f16700Schasinglulu 20*91f16700Schasinglulu #include "psci_private.h" 21*91f16700Schasinglulu 22*91f16700Schasinglulu /* 23*91f16700Schasinglulu * SPD power management operations, expected to be supplied by the registered 24*91f16700Schasinglulu * SPD on successful SP initialization 25*91f16700Schasinglulu */ 26*91f16700Schasinglulu const spd_pm_ops_t *psci_spd_pm; 27*91f16700Schasinglulu 28*91f16700Schasinglulu /* 29*91f16700Schasinglulu * PSCI requested local power state map. This array is used to store the local 30*91f16700Schasinglulu * power states requested by a CPU for power levels from level 1 to 31*91f16700Schasinglulu * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power 32*91f16700Schasinglulu * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a 33*91f16700Schasinglulu * CPU are the same. 34*91f16700Schasinglulu * 35*91f16700Schasinglulu * During state coordination, the platform is passed an array containing the 36*91f16700Schasinglulu * local states requested for a particular non cpu power domain by each cpu 37*91f16700Schasinglulu * within the domain. 38*91f16700Schasinglulu * 39*91f16700Schasinglulu * TODO: Dense packing of the requested states will cause cache thrashing 40*91f16700Schasinglulu * when multiple power domains write to it. If we allocate the requested 41*91f16700Schasinglulu * states at each power level in a cache-line aligned per-domain memory, 42*91f16700Schasinglulu * the cache thrashing can be avoided. 43*91f16700Schasinglulu */ 44*91f16700Schasinglulu static plat_local_state_t 45*91f16700Schasinglulu psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT]; 46*91f16700Schasinglulu 47*91f16700Schasinglulu unsigned int psci_plat_core_count; 48*91f16700Schasinglulu 49*91f16700Schasinglulu /******************************************************************************* 50*91f16700Schasinglulu * Arrays that hold the platform's power domain tree information for state 51*91f16700Schasinglulu * management of power domains. 52*91f16700Schasinglulu * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain 53*91f16700Schasinglulu * which is an ancestor of a CPU power domain. 54*91f16700Schasinglulu * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain 55*91f16700Schasinglulu ******************************************************************************/ 56*91f16700Schasinglulu non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS] 57*91f16700Schasinglulu #if USE_COHERENT_MEM 58*91f16700Schasinglulu __section(".tzfw_coherent_mem") 59*91f16700Schasinglulu #endif 60*91f16700Schasinglulu ; 61*91f16700Schasinglulu 62*91f16700Schasinglulu /* Lock for PSCI state coordination */ 63*91f16700Schasinglulu DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]); 64*91f16700Schasinglulu 65*91f16700Schasinglulu cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT]; 66*91f16700Schasinglulu 67*91f16700Schasinglulu /******************************************************************************* 68*91f16700Schasinglulu * Pointer to functions exported by the platform to complete power mgmt. ops 69*91f16700Schasinglulu ******************************************************************************/ 70*91f16700Schasinglulu const plat_psci_ops_t *psci_plat_pm_ops; 71*91f16700Schasinglulu 72*91f16700Schasinglulu /****************************************************************************** 73*91f16700Schasinglulu * Check that the maximum power level supported by the platform makes sense 74*91f16700Schasinglulu *****************************************************************************/ 75*91f16700Schasinglulu CASSERT((PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL) && 76*91f16700Schasinglulu (PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL), 77*91f16700Schasinglulu assert_platform_max_pwrlvl_check); 78*91f16700Schasinglulu 79*91f16700Schasinglulu #if PSCI_OS_INIT_MODE 80*91f16700Schasinglulu /******************************************************************************* 81*91f16700Schasinglulu * The power state coordination mode used in CPU_SUSPEND. 82*91f16700Schasinglulu * Defaults to platform-coordinated mode. 83*91f16700Schasinglulu ******************************************************************************/ 84*91f16700Schasinglulu suspend_mode_t psci_suspend_mode = PLAT_COORD; 85*91f16700Schasinglulu #endif 86*91f16700Schasinglulu 87*91f16700Schasinglulu /* 88*91f16700Schasinglulu * The plat_local_state used by the platform is one of these types: RUN, 89*91f16700Schasinglulu * RETENTION and OFF. The platform can define further sub-states for each type 90*91f16700Schasinglulu * apart from RUN. This categorization is done to verify the sanity of the 91*91f16700Schasinglulu * psci_power_state passed by the platform and to print debug information. The 92*91f16700Schasinglulu * categorization is done on the basis of the following conditions: 93*91f16700Schasinglulu * 94*91f16700Schasinglulu * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN. 95*91f16700Schasinglulu * 96*91f16700Schasinglulu * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is 97*91f16700Schasinglulu * STATE_TYPE_RETN. 98*91f16700Schasinglulu * 99*91f16700Schasinglulu * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is 100*91f16700Schasinglulu * STATE_TYPE_OFF. 101*91f16700Schasinglulu */ 102*91f16700Schasinglulu typedef enum plat_local_state_type { 103*91f16700Schasinglulu STATE_TYPE_RUN = 0, 104*91f16700Schasinglulu STATE_TYPE_RETN, 105*91f16700Schasinglulu STATE_TYPE_OFF 106*91f16700Schasinglulu } plat_local_state_type_t; 107*91f16700Schasinglulu 108*91f16700Schasinglulu /* Function used to categorize plat_local_state. */ 109*91f16700Schasinglulu static plat_local_state_type_t find_local_state_type(plat_local_state_t state) 110*91f16700Schasinglulu { 111*91f16700Schasinglulu if (state != 0U) { 112*91f16700Schasinglulu if (state > PLAT_MAX_RET_STATE) { 113*91f16700Schasinglulu return STATE_TYPE_OFF; 114*91f16700Schasinglulu } else { 115*91f16700Schasinglulu return STATE_TYPE_RETN; 116*91f16700Schasinglulu } 117*91f16700Schasinglulu } else { 118*91f16700Schasinglulu return STATE_TYPE_RUN; 119*91f16700Schasinglulu } 120*91f16700Schasinglulu } 121*91f16700Schasinglulu 122*91f16700Schasinglulu /****************************************************************************** 123*91f16700Schasinglulu * Check that the maximum retention level supported by the platform is less 124*91f16700Schasinglulu * than the maximum off level. 125*91f16700Schasinglulu *****************************************************************************/ 126*91f16700Schasinglulu CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE, 127*91f16700Schasinglulu assert_platform_max_off_and_retn_state_check); 128*91f16700Schasinglulu 129*91f16700Schasinglulu /****************************************************************************** 130*91f16700Schasinglulu * This function ensures that the power state parameter in a CPU_SUSPEND request 131*91f16700Schasinglulu * is valid. If so, it returns the requested states for each power level. 132*91f16700Schasinglulu *****************************************************************************/ 133*91f16700Schasinglulu int psci_validate_power_state(unsigned int power_state, 134*91f16700Schasinglulu psci_power_state_t *state_info) 135*91f16700Schasinglulu { 136*91f16700Schasinglulu /* Check SBZ bits in power state are zero */ 137*91f16700Schasinglulu if (psci_check_power_state(power_state) != 0U) 138*91f16700Schasinglulu return PSCI_E_INVALID_PARAMS; 139*91f16700Schasinglulu 140*91f16700Schasinglulu assert(psci_plat_pm_ops->validate_power_state != NULL); 141*91f16700Schasinglulu 142*91f16700Schasinglulu /* Validate the power_state using platform pm_ops */ 143*91f16700Schasinglulu return psci_plat_pm_ops->validate_power_state(power_state, state_info); 144*91f16700Schasinglulu } 145*91f16700Schasinglulu 146*91f16700Schasinglulu /****************************************************************************** 147*91f16700Schasinglulu * This function retrieves the `psci_power_state_t` for system suspend from 148*91f16700Schasinglulu * the platform. 149*91f16700Schasinglulu *****************************************************************************/ 150*91f16700Schasinglulu void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info) 151*91f16700Schasinglulu { 152*91f16700Schasinglulu /* 153*91f16700Schasinglulu * Assert that the required pm_ops hook is implemented to ensure that 154*91f16700Schasinglulu * the capability detected during psci_setup() is valid. 155*91f16700Schasinglulu */ 156*91f16700Schasinglulu assert(psci_plat_pm_ops->get_sys_suspend_power_state != NULL); 157*91f16700Schasinglulu 158*91f16700Schasinglulu /* 159*91f16700Schasinglulu * Query the platform for the power_state required for system suspend 160*91f16700Schasinglulu */ 161*91f16700Schasinglulu psci_plat_pm_ops->get_sys_suspend_power_state(state_info); 162*91f16700Schasinglulu } 163*91f16700Schasinglulu 164*91f16700Schasinglulu #if PSCI_OS_INIT_MODE 165*91f16700Schasinglulu /******************************************************************************* 166*91f16700Schasinglulu * This function verifies that all the other cores at the 'end_pwrlvl' have been 167*91f16700Schasinglulu * idled and the current CPU is the last running CPU at the 'end_pwrlvl'. 168*91f16700Schasinglulu * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false) 169*91f16700Schasinglulu * otherwise. 170*91f16700Schasinglulu ******************************************************************************/ 171*91f16700Schasinglulu static bool psci_is_last_cpu_to_idle_at_pwrlvl(unsigned int end_pwrlvl) 172*91f16700Schasinglulu { 173*91f16700Schasinglulu unsigned int my_idx, lvl, parent_idx; 174*91f16700Schasinglulu unsigned int cpu_start_idx, ncpus, cpu_idx; 175*91f16700Schasinglulu plat_local_state_t local_state; 176*91f16700Schasinglulu 177*91f16700Schasinglulu if (end_pwrlvl == PSCI_CPU_PWR_LVL) { 178*91f16700Schasinglulu return true; 179*91f16700Schasinglulu } 180*91f16700Schasinglulu 181*91f16700Schasinglulu my_idx = plat_my_core_pos(); 182*91f16700Schasinglulu 183*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL; lvl <= end_pwrlvl; lvl++) { 184*91f16700Schasinglulu parent_idx = psci_cpu_pd_nodes[my_idx].parent_node; 185*91f16700Schasinglulu } 186*91f16700Schasinglulu 187*91f16700Schasinglulu cpu_start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx; 188*91f16700Schasinglulu ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus; 189*91f16700Schasinglulu 190*91f16700Schasinglulu for (cpu_idx = cpu_start_idx; cpu_idx < cpu_start_idx + ncpus; 191*91f16700Schasinglulu cpu_idx++) { 192*91f16700Schasinglulu local_state = psci_get_cpu_local_state_by_idx(cpu_idx); 193*91f16700Schasinglulu if (cpu_idx == my_idx) { 194*91f16700Schasinglulu assert(is_local_state_run(local_state) != 0); 195*91f16700Schasinglulu continue; 196*91f16700Schasinglulu } 197*91f16700Schasinglulu 198*91f16700Schasinglulu if (is_local_state_run(local_state) != 0) { 199*91f16700Schasinglulu return false; 200*91f16700Schasinglulu } 201*91f16700Schasinglulu } 202*91f16700Schasinglulu 203*91f16700Schasinglulu return true; 204*91f16700Schasinglulu } 205*91f16700Schasinglulu #endif 206*91f16700Schasinglulu 207*91f16700Schasinglulu /******************************************************************************* 208*91f16700Schasinglulu * This function verifies that all the other cores in the system have been 209*91f16700Schasinglulu * turned OFF and the current CPU is the last running CPU in the system. 210*91f16700Schasinglulu * Returns true, if the current CPU is the last ON CPU or false otherwise. 211*91f16700Schasinglulu ******************************************************************************/ 212*91f16700Schasinglulu bool psci_is_last_on_cpu(void) 213*91f16700Schasinglulu { 214*91f16700Schasinglulu unsigned int cpu_idx, my_idx = plat_my_core_pos(); 215*91f16700Schasinglulu 216*91f16700Schasinglulu for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) { 217*91f16700Schasinglulu if (cpu_idx == my_idx) { 218*91f16700Schasinglulu assert(psci_get_aff_info_state() == AFF_STATE_ON); 219*91f16700Schasinglulu continue; 220*91f16700Schasinglulu } 221*91f16700Schasinglulu 222*91f16700Schasinglulu if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF) { 223*91f16700Schasinglulu VERBOSE("core=%u other than current core=%u %s\n", 224*91f16700Schasinglulu cpu_idx, my_idx, "running in the system"); 225*91f16700Schasinglulu return false; 226*91f16700Schasinglulu } 227*91f16700Schasinglulu } 228*91f16700Schasinglulu 229*91f16700Schasinglulu return true; 230*91f16700Schasinglulu } 231*91f16700Schasinglulu 232*91f16700Schasinglulu /******************************************************************************* 233*91f16700Schasinglulu * This function verifies that all cores in the system have been turned ON. 234*91f16700Schasinglulu * Returns true, if all CPUs are ON or false otherwise. 235*91f16700Schasinglulu ******************************************************************************/ 236*91f16700Schasinglulu static bool psci_are_all_cpus_on(void) 237*91f16700Schasinglulu { 238*91f16700Schasinglulu unsigned int cpu_idx; 239*91f16700Schasinglulu 240*91f16700Schasinglulu for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) { 241*91f16700Schasinglulu if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_OFF) { 242*91f16700Schasinglulu return false; 243*91f16700Schasinglulu } 244*91f16700Schasinglulu } 245*91f16700Schasinglulu 246*91f16700Schasinglulu return true; 247*91f16700Schasinglulu } 248*91f16700Schasinglulu 249*91f16700Schasinglulu /******************************************************************************* 250*91f16700Schasinglulu * Routine to return the maximum power level to traverse to after a cpu has 251*91f16700Schasinglulu * been physically powered up. It is expected to be called immediately after 252*91f16700Schasinglulu * reset from assembler code. 253*91f16700Schasinglulu ******************************************************************************/ 254*91f16700Schasinglulu static unsigned int get_power_on_target_pwrlvl(void) 255*91f16700Schasinglulu { 256*91f16700Schasinglulu unsigned int pwrlvl; 257*91f16700Schasinglulu 258*91f16700Schasinglulu /* 259*91f16700Schasinglulu * Assume that this cpu was suspended and retrieve its target power 260*91f16700Schasinglulu * level. If it is invalid then it could only have been turned off 261*91f16700Schasinglulu * earlier. PLAT_MAX_PWR_LVL will be the highest power level a 262*91f16700Schasinglulu * cpu can be turned off to. 263*91f16700Schasinglulu */ 264*91f16700Schasinglulu pwrlvl = psci_get_suspend_pwrlvl(); 265*91f16700Schasinglulu if (pwrlvl == PSCI_INVALID_PWR_LVL) 266*91f16700Schasinglulu pwrlvl = PLAT_MAX_PWR_LVL; 267*91f16700Schasinglulu assert(pwrlvl < PSCI_INVALID_PWR_LVL); 268*91f16700Schasinglulu return pwrlvl; 269*91f16700Schasinglulu } 270*91f16700Schasinglulu 271*91f16700Schasinglulu /****************************************************************************** 272*91f16700Schasinglulu * Helper function to update the requested local power state array. This array 273*91f16700Schasinglulu * does not store the requested state for the CPU power level. Hence an 274*91f16700Schasinglulu * assertion is added to prevent us from accessing the CPU power level. 275*91f16700Schasinglulu *****************************************************************************/ 276*91f16700Schasinglulu static void psci_set_req_local_pwr_state(unsigned int pwrlvl, 277*91f16700Schasinglulu unsigned int cpu_idx, 278*91f16700Schasinglulu plat_local_state_t req_pwr_state) 279*91f16700Schasinglulu { 280*91f16700Schasinglulu assert(pwrlvl > PSCI_CPU_PWR_LVL); 281*91f16700Schasinglulu if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) && 282*91f16700Schasinglulu (cpu_idx < psci_plat_core_count)) { 283*91f16700Schasinglulu psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx] = req_pwr_state; 284*91f16700Schasinglulu } 285*91f16700Schasinglulu } 286*91f16700Schasinglulu 287*91f16700Schasinglulu /****************************************************************************** 288*91f16700Schasinglulu * This function initializes the psci_req_local_pwr_states. 289*91f16700Schasinglulu *****************************************************************************/ 290*91f16700Schasinglulu void __init psci_init_req_local_pwr_states(void) 291*91f16700Schasinglulu { 292*91f16700Schasinglulu /* Initialize the requested state of all non CPU power domains as OFF */ 293*91f16700Schasinglulu unsigned int pwrlvl; 294*91f16700Schasinglulu unsigned int core; 295*91f16700Schasinglulu 296*91f16700Schasinglulu for (pwrlvl = 0U; pwrlvl < PLAT_MAX_PWR_LVL; pwrlvl++) { 297*91f16700Schasinglulu for (core = 0; core < psci_plat_core_count; core++) { 298*91f16700Schasinglulu psci_req_local_pwr_states[pwrlvl][core] = 299*91f16700Schasinglulu PLAT_MAX_OFF_STATE; 300*91f16700Schasinglulu } 301*91f16700Schasinglulu } 302*91f16700Schasinglulu } 303*91f16700Schasinglulu 304*91f16700Schasinglulu /****************************************************************************** 305*91f16700Schasinglulu * Helper function to return a reference to an array containing the local power 306*91f16700Schasinglulu * states requested by each cpu for a power domain at 'pwrlvl'. The size of the 307*91f16700Schasinglulu * array will be the number of cpu power domains of which this power domain is 308*91f16700Schasinglulu * an ancestor. These requested states will be used to determine a suitable 309*91f16700Schasinglulu * target state for this power domain during psci state coordination. An 310*91f16700Schasinglulu * assertion is added to prevent us from accessing the CPU power level. 311*91f16700Schasinglulu *****************************************************************************/ 312*91f16700Schasinglulu static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl, 313*91f16700Schasinglulu unsigned int cpu_idx) 314*91f16700Schasinglulu { 315*91f16700Schasinglulu assert(pwrlvl > PSCI_CPU_PWR_LVL); 316*91f16700Schasinglulu 317*91f16700Schasinglulu if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) && 318*91f16700Schasinglulu (cpu_idx < psci_plat_core_count)) { 319*91f16700Schasinglulu return &psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx]; 320*91f16700Schasinglulu } else 321*91f16700Schasinglulu return NULL; 322*91f16700Schasinglulu } 323*91f16700Schasinglulu 324*91f16700Schasinglulu #if PSCI_OS_INIT_MODE 325*91f16700Schasinglulu /****************************************************************************** 326*91f16700Schasinglulu * Helper function to save a copy of the psci_req_local_pwr_states (prev) for a 327*91f16700Schasinglulu * CPU (cpu_idx), and update psci_req_local_pwr_states with the new requested 328*91f16700Schasinglulu * local power states (state_info). 329*91f16700Schasinglulu *****************************************************************************/ 330*91f16700Schasinglulu void psci_update_req_local_pwr_states(unsigned int end_pwrlvl, 331*91f16700Schasinglulu unsigned int cpu_idx, 332*91f16700Schasinglulu psci_power_state_t *state_info, 333*91f16700Schasinglulu plat_local_state_t *prev) 334*91f16700Schasinglulu { 335*91f16700Schasinglulu unsigned int lvl; 336*91f16700Schasinglulu #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL 337*91f16700Schasinglulu unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL; 338*91f16700Schasinglulu #else 339*91f16700Schasinglulu unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL; 340*91f16700Schasinglulu #endif 341*91f16700Schasinglulu plat_local_state_t req_state; 342*91f16700Schasinglulu 343*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) { 344*91f16700Schasinglulu /* Save the previous requested local power state */ 345*91f16700Schasinglulu prev[lvl - 1U] = *psci_get_req_local_pwr_states(lvl, cpu_idx); 346*91f16700Schasinglulu 347*91f16700Schasinglulu /* Update the new requested local power state */ 348*91f16700Schasinglulu if (lvl <= end_pwrlvl) { 349*91f16700Schasinglulu req_state = state_info->pwr_domain_state[lvl]; 350*91f16700Schasinglulu } else { 351*91f16700Schasinglulu req_state = state_info->pwr_domain_state[end_pwrlvl]; 352*91f16700Schasinglulu } 353*91f16700Schasinglulu psci_set_req_local_pwr_state(lvl, cpu_idx, req_state); 354*91f16700Schasinglulu } 355*91f16700Schasinglulu } 356*91f16700Schasinglulu 357*91f16700Schasinglulu /****************************************************************************** 358*91f16700Schasinglulu * Helper function to restore the previously saved requested local power states 359*91f16700Schasinglulu * (prev) for a CPU (cpu_idx) to psci_req_local_pwr_states. 360*91f16700Schasinglulu *****************************************************************************/ 361*91f16700Schasinglulu void psci_restore_req_local_pwr_states(unsigned int cpu_idx, 362*91f16700Schasinglulu plat_local_state_t *prev) 363*91f16700Schasinglulu { 364*91f16700Schasinglulu unsigned int lvl; 365*91f16700Schasinglulu #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL 366*91f16700Schasinglulu unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL; 367*91f16700Schasinglulu #else 368*91f16700Schasinglulu unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL; 369*91f16700Schasinglulu #endif 370*91f16700Schasinglulu 371*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) { 372*91f16700Schasinglulu /* Restore the previous requested local power state */ 373*91f16700Schasinglulu psci_set_req_local_pwr_state(lvl, cpu_idx, prev[lvl - 1U]); 374*91f16700Schasinglulu } 375*91f16700Schasinglulu } 376*91f16700Schasinglulu #endif 377*91f16700Schasinglulu 378*91f16700Schasinglulu /* 379*91f16700Schasinglulu * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent 380*91f16700Schasinglulu * memory. 381*91f16700Schasinglulu * 382*91f16700Schasinglulu * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory, 383*91f16700Schasinglulu * it's accessed by both cached and non-cached participants. To serve the common 384*91f16700Schasinglulu * minimum, perform a cache flush before read and after write so that non-cached 385*91f16700Schasinglulu * participants operate on latest data in main memory. 386*91f16700Schasinglulu * 387*91f16700Schasinglulu * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent 388*91f16700Schasinglulu * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent. 389*91f16700Schasinglulu * In both cases, no cache operations are required. 390*91f16700Schasinglulu */ 391*91f16700Schasinglulu 392*91f16700Schasinglulu /* 393*91f16700Schasinglulu * Retrieve local state of non-CPU power domain node from a non-cached CPU, 394*91f16700Schasinglulu * after any required cache maintenance operation. 395*91f16700Schasinglulu */ 396*91f16700Schasinglulu static plat_local_state_t get_non_cpu_pd_node_local_state( 397*91f16700Schasinglulu unsigned int parent_idx) 398*91f16700Schasinglulu { 399*91f16700Schasinglulu #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 400*91f16700Schasinglulu flush_dcache_range( 401*91f16700Schasinglulu (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx], 402*91f16700Schasinglulu sizeof(psci_non_cpu_pd_nodes[parent_idx])); 403*91f16700Schasinglulu #endif 404*91f16700Schasinglulu return psci_non_cpu_pd_nodes[parent_idx].local_state; 405*91f16700Schasinglulu } 406*91f16700Schasinglulu 407*91f16700Schasinglulu /* 408*91f16700Schasinglulu * Update local state of non-CPU power domain node from a cached CPU; perform 409*91f16700Schasinglulu * any required cache maintenance operation afterwards. 410*91f16700Schasinglulu */ 411*91f16700Schasinglulu static void set_non_cpu_pd_node_local_state(unsigned int parent_idx, 412*91f16700Schasinglulu plat_local_state_t state) 413*91f16700Schasinglulu { 414*91f16700Schasinglulu psci_non_cpu_pd_nodes[parent_idx].local_state = state; 415*91f16700Schasinglulu #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 416*91f16700Schasinglulu flush_dcache_range( 417*91f16700Schasinglulu (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx], 418*91f16700Schasinglulu sizeof(psci_non_cpu_pd_nodes[parent_idx])); 419*91f16700Schasinglulu #endif 420*91f16700Schasinglulu } 421*91f16700Schasinglulu 422*91f16700Schasinglulu /****************************************************************************** 423*91f16700Schasinglulu * Helper function to return the current local power state of each power domain 424*91f16700Schasinglulu * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This 425*91f16700Schasinglulu * function will be called after a cpu is powered on to find the local state 426*91f16700Schasinglulu * each power domain has emerged from. 427*91f16700Schasinglulu *****************************************************************************/ 428*91f16700Schasinglulu void psci_get_target_local_pwr_states(unsigned int end_pwrlvl, 429*91f16700Schasinglulu psci_power_state_t *target_state) 430*91f16700Schasinglulu { 431*91f16700Schasinglulu unsigned int parent_idx, lvl; 432*91f16700Schasinglulu plat_local_state_t *pd_state = target_state->pwr_domain_state; 433*91f16700Schasinglulu 434*91f16700Schasinglulu pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state(); 435*91f16700Schasinglulu parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node; 436*91f16700Schasinglulu 437*91f16700Schasinglulu /* Copy the local power state from node to state_info */ 438*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) { 439*91f16700Schasinglulu pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx); 440*91f16700Schasinglulu parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 441*91f16700Schasinglulu } 442*91f16700Schasinglulu 443*91f16700Schasinglulu /* Set the the higher levels to RUN */ 444*91f16700Schasinglulu for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) 445*91f16700Schasinglulu target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN; 446*91f16700Schasinglulu } 447*91f16700Schasinglulu 448*91f16700Schasinglulu /****************************************************************************** 449*91f16700Schasinglulu * Helper function to set the target local power state that each power domain 450*91f16700Schasinglulu * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will 451*91f16700Schasinglulu * enter. This function will be called after coordination of requested power 452*91f16700Schasinglulu * states has been done for each power level. 453*91f16700Schasinglulu *****************************************************************************/ 454*91f16700Schasinglulu void psci_set_target_local_pwr_states(unsigned int end_pwrlvl, 455*91f16700Schasinglulu const psci_power_state_t *target_state) 456*91f16700Schasinglulu { 457*91f16700Schasinglulu unsigned int parent_idx, lvl; 458*91f16700Schasinglulu const plat_local_state_t *pd_state = target_state->pwr_domain_state; 459*91f16700Schasinglulu 460*91f16700Schasinglulu psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]); 461*91f16700Schasinglulu 462*91f16700Schasinglulu /* 463*91f16700Schasinglulu * Need to flush as local_state might be accessed with Data Cache 464*91f16700Schasinglulu * disabled during power on 465*91f16700Schasinglulu */ 466*91f16700Schasinglulu psci_flush_cpu_data(psci_svc_cpu_data.local_state); 467*91f16700Schasinglulu 468*91f16700Schasinglulu parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node; 469*91f16700Schasinglulu 470*91f16700Schasinglulu /* Copy the local_state from state_info */ 471*91f16700Schasinglulu for (lvl = 1U; lvl <= end_pwrlvl; lvl++) { 472*91f16700Schasinglulu set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]); 473*91f16700Schasinglulu parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 474*91f16700Schasinglulu } 475*91f16700Schasinglulu } 476*91f16700Schasinglulu 477*91f16700Schasinglulu /******************************************************************************* 478*91f16700Schasinglulu * PSCI helper function to get the parent nodes corresponding to a cpu_index. 479*91f16700Schasinglulu ******************************************************************************/ 480*91f16700Schasinglulu void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx, 481*91f16700Schasinglulu unsigned int end_lvl, 482*91f16700Schasinglulu unsigned int *node_index) 483*91f16700Schasinglulu { 484*91f16700Schasinglulu unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node; 485*91f16700Schasinglulu unsigned int i; 486*91f16700Schasinglulu unsigned int *node = node_index; 487*91f16700Schasinglulu 488*91f16700Schasinglulu for (i = PSCI_CPU_PWR_LVL + 1U; i <= end_lvl; i++) { 489*91f16700Schasinglulu *node = parent_node; 490*91f16700Schasinglulu node++; 491*91f16700Schasinglulu parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node; 492*91f16700Schasinglulu } 493*91f16700Schasinglulu } 494*91f16700Schasinglulu 495*91f16700Schasinglulu /****************************************************************************** 496*91f16700Schasinglulu * This function is invoked post CPU power up and initialization. It sets the 497*91f16700Schasinglulu * affinity info state, target power state and requested power state for the 498*91f16700Schasinglulu * current CPU and all its ancestor power domains to RUN. 499*91f16700Schasinglulu *****************************************************************************/ 500*91f16700Schasinglulu void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl) 501*91f16700Schasinglulu { 502*91f16700Schasinglulu unsigned int parent_idx, cpu_idx = plat_my_core_pos(), lvl; 503*91f16700Schasinglulu parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 504*91f16700Schasinglulu 505*91f16700Schasinglulu /* Reset the local_state to RUN for the non cpu power domains. */ 506*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) { 507*91f16700Schasinglulu set_non_cpu_pd_node_local_state(parent_idx, 508*91f16700Schasinglulu PSCI_LOCAL_STATE_RUN); 509*91f16700Schasinglulu psci_set_req_local_pwr_state(lvl, 510*91f16700Schasinglulu cpu_idx, 511*91f16700Schasinglulu PSCI_LOCAL_STATE_RUN); 512*91f16700Schasinglulu parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 513*91f16700Schasinglulu } 514*91f16700Schasinglulu 515*91f16700Schasinglulu /* Set the affinity info state to ON */ 516*91f16700Schasinglulu psci_set_aff_info_state(AFF_STATE_ON); 517*91f16700Schasinglulu 518*91f16700Schasinglulu psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN); 519*91f16700Schasinglulu psci_flush_cpu_data(psci_svc_cpu_data); 520*91f16700Schasinglulu } 521*91f16700Schasinglulu 522*91f16700Schasinglulu /****************************************************************************** 523*91f16700Schasinglulu * This function is used in platform-coordinated mode. 524*91f16700Schasinglulu * 525*91f16700Schasinglulu * This function is passed the local power states requested for each power 526*91f16700Schasinglulu * domain (state_info) between the current CPU domain and its ancestors until 527*91f16700Schasinglulu * the target power level (end_pwrlvl). It updates the array of requested power 528*91f16700Schasinglulu * states with this information. 529*91f16700Schasinglulu * 530*91f16700Schasinglulu * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it 531*91f16700Schasinglulu * retrieves the states requested by all the cpus of which the power domain at 532*91f16700Schasinglulu * that level is an ancestor. It passes this information to the platform to 533*91f16700Schasinglulu * coordinate and return the target power state. If the target state for a level 534*91f16700Schasinglulu * is RUN then subsequent levels are not considered. At the CPU level, state 535*91f16700Schasinglulu * coordination is not required. Hence, the requested and the target states are 536*91f16700Schasinglulu * the same. 537*91f16700Schasinglulu * 538*91f16700Schasinglulu * The 'state_info' is updated with the target state for each level between the 539*91f16700Schasinglulu * CPU and the 'end_pwrlvl' and returned to the caller. 540*91f16700Schasinglulu * 541*91f16700Schasinglulu * This function will only be invoked with data cache enabled and while 542*91f16700Schasinglulu * powering down a core. 543*91f16700Schasinglulu *****************************************************************************/ 544*91f16700Schasinglulu void psci_do_state_coordination(unsigned int end_pwrlvl, 545*91f16700Schasinglulu psci_power_state_t *state_info) 546*91f16700Schasinglulu { 547*91f16700Schasinglulu unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos(); 548*91f16700Schasinglulu unsigned int start_idx; 549*91f16700Schasinglulu unsigned int ncpus; 550*91f16700Schasinglulu plat_local_state_t target_state, *req_states; 551*91f16700Schasinglulu 552*91f16700Schasinglulu assert(end_pwrlvl <= PLAT_MAX_PWR_LVL); 553*91f16700Schasinglulu parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 554*91f16700Schasinglulu 555*91f16700Schasinglulu /* For level 0, the requested state will be equivalent 556*91f16700Schasinglulu to target state */ 557*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) { 558*91f16700Schasinglulu 559*91f16700Schasinglulu /* First update the requested power state */ 560*91f16700Schasinglulu psci_set_req_local_pwr_state(lvl, cpu_idx, 561*91f16700Schasinglulu state_info->pwr_domain_state[lvl]); 562*91f16700Schasinglulu 563*91f16700Schasinglulu /* Get the requested power states for this power level */ 564*91f16700Schasinglulu start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx; 565*91f16700Schasinglulu req_states = psci_get_req_local_pwr_states(lvl, start_idx); 566*91f16700Schasinglulu 567*91f16700Schasinglulu /* 568*91f16700Schasinglulu * Let the platform coordinate amongst the requested states at 569*91f16700Schasinglulu * this power level and return the target local power state. 570*91f16700Schasinglulu */ 571*91f16700Schasinglulu ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus; 572*91f16700Schasinglulu target_state = plat_get_target_pwr_state(lvl, 573*91f16700Schasinglulu req_states, 574*91f16700Schasinglulu ncpus); 575*91f16700Schasinglulu 576*91f16700Schasinglulu state_info->pwr_domain_state[lvl] = target_state; 577*91f16700Schasinglulu 578*91f16700Schasinglulu /* Break early if the negotiated target power state is RUN */ 579*91f16700Schasinglulu if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0) 580*91f16700Schasinglulu break; 581*91f16700Schasinglulu 582*91f16700Schasinglulu parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node; 583*91f16700Schasinglulu } 584*91f16700Schasinglulu 585*91f16700Schasinglulu /* 586*91f16700Schasinglulu * This is for cases when we break out of the above loop early because 587*91f16700Schasinglulu * the target power state is RUN at a power level < end_pwlvl. 588*91f16700Schasinglulu * We update the requested power state from state_info and then 589*91f16700Schasinglulu * set the target state as RUN. 590*91f16700Schasinglulu */ 591*91f16700Schasinglulu for (lvl = lvl + 1U; lvl <= end_pwrlvl; lvl++) { 592*91f16700Schasinglulu psci_set_req_local_pwr_state(lvl, cpu_idx, 593*91f16700Schasinglulu state_info->pwr_domain_state[lvl]); 594*91f16700Schasinglulu state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN; 595*91f16700Schasinglulu 596*91f16700Schasinglulu } 597*91f16700Schasinglulu } 598*91f16700Schasinglulu 599*91f16700Schasinglulu #if PSCI_OS_INIT_MODE 600*91f16700Schasinglulu /****************************************************************************** 601*91f16700Schasinglulu * This function is used in OS-initiated mode. 602*91f16700Schasinglulu * 603*91f16700Schasinglulu * This function is passed the local power states requested for each power 604*91f16700Schasinglulu * domain (state_info) between the current CPU domain and its ancestors until 605*91f16700Schasinglulu * the target power level (end_pwrlvl), and ensures the requested power states 606*91f16700Schasinglulu * are valid. It updates the array of requested power states with this 607*91f16700Schasinglulu * information. 608*91f16700Schasinglulu * 609*91f16700Schasinglulu * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it 610*91f16700Schasinglulu * retrieves the states requested by all the cpus of which the power domain at 611*91f16700Schasinglulu * that level is an ancestor. It passes this information to the platform to 612*91f16700Schasinglulu * coordinate and return the target power state. If the requested state does 613*91f16700Schasinglulu * not match the target state, the request is denied. 614*91f16700Schasinglulu * 615*91f16700Schasinglulu * The 'state_info' is not modified. 616*91f16700Schasinglulu * 617*91f16700Schasinglulu * This function will only be invoked with data cache enabled and while 618*91f16700Schasinglulu * powering down a core. 619*91f16700Schasinglulu *****************************************************************************/ 620*91f16700Schasinglulu int psci_validate_state_coordination(unsigned int end_pwrlvl, 621*91f16700Schasinglulu psci_power_state_t *state_info) 622*91f16700Schasinglulu { 623*91f16700Schasinglulu int rc = PSCI_E_SUCCESS; 624*91f16700Schasinglulu unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos(); 625*91f16700Schasinglulu unsigned int start_idx; 626*91f16700Schasinglulu unsigned int ncpus; 627*91f16700Schasinglulu plat_local_state_t target_state, *req_states; 628*91f16700Schasinglulu plat_local_state_t prev[PLAT_MAX_PWR_LVL]; 629*91f16700Schasinglulu 630*91f16700Schasinglulu assert(end_pwrlvl <= PLAT_MAX_PWR_LVL); 631*91f16700Schasinglulu parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node; 632*91f16700Schasinglulu 633*91f16700Schasinglulu /* 634*91f16700Schasinglulu * Save a copy of the previous requested local power states and update 635*91f16700Schasinglulu * the new requested local power states. 636*91f16700Schasinglulu */ 637*91f16700Schasinglulu psci_update_req_local_pwr_states(end_pwrlvl, cpu_idx, state_info, prev); 638*91f16700Schasinglulu 639*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) { 640*91f16700Schasinglulu /* Get the requested power states for this power level */ 641*91f16700Schasinglulu start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx; 642*91f16700Schasinglulu req_states = psci_get_req_local_pwr_states(lvl, start_idx); 643*91f16700Schasinglulu 644*91f16700Schasinglulu /* 645*91f16700Schasinglulu * Let the platform coordinate amongst the requested states at 646*91f16700Schasinglulu * this power level and return the target local power state. 647*91f16700Schasinglulu */ 648*91f16700Schasinglulu ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus; 649*91f16700Schasinglulu target_state = plat_get_target_pwr_state(lvl, 650*91f16700Schasinglulu req_states, 651*91f16700Schasinglulu ncpus); 652*91f16700Schasinglulu 653*91f16700Schasinglulu /* 654*91f16700Schasinglulu * Verify that the requested power state matches the target 655*91f16700Schasinglulu * local power state. 656*91f16700Schasinglulu */ 657*91f16700Schasinglulu if (state_info->pwr_domain_state[lvl] != target_state) { 658*91f16700Schasinglulu if (target_state == PSCI_LOCAL_STATE_RUN) { 659*91f16700Schasinglulu rc = PSCI_E_DENIED; 660*91f16700Schasinglulu } else { 661*91f16700Schasinglulu rc = PSCI_E_INVALID_PARAMS; 662*91f16700Schasinglulu } 663*91f16700Schasinglulu goto exit; 664*91f16700Schasinglulu } 665*91f16700Schasinglulu } 666*91f16700Schasinglulu 667*91f16700Schasinglulu /* 668*91f16700Schasinglulu * Verify that the current core is the last running core at the 669*91f16700Schasinglulu * specified power level. 670*91f16700Schasinglulu */ 671*91f16700Schasinglulu lvl = state_info->last_at_pwrlvl; 672*91f16700Schasinglulu if (!psci_is_last_cpu_to_idle_at_pwrlvl(lvl)) { 673*91f16700Schasinglulu rc = PSCI_E_DENIED; 674*91f16700Schasinglulu } 675*91f16700Schasinglulu 676*91f16700Schasinglulu exit: 677*91f16700Schasinglulu if (rc != PSCI_E_SUCCESS) { 678*91f16700Schasinglulu /* Restore the previous requested local power states. */ 679*91f16700Schasinglulu psci_restore_req_local_pwr_states(cpu_idx, prev); 680*91f16700Schasinglulu return rc; 681*91f16700Schasinglulu } 682*91f16700Schasinglulu 683*91f16700Schasinglulu return rc; 684*91f16700Schasinglulu } 685*91f16700Schasinglulu #endif 686*91f16700Schasinglulu 687*91f16700Schasinglulu /****************************************************************************** 688*91f16700Schasinglulu * This function validates a suspend request by making sure that if a standby 689*91f16700Schasinglulu * state is requested then no power level is turned off and the highest power 690*91f16700Schasinglulu * level is placed in a standby/retention state. 691*91f16700Schasinglulu * 692*91f16700Schasinglulu * It also ensures that the state level X will enter is not shallower than the 693*91f16700Schasinglulu * state level X + 1 will enter. 694*91f16700Schasinglulu * 695*91f16700Schasinglulu * This validation will be enabled only for DEBUG builds as the platform is 696*91f16700Schasinglulu * expected to perform these validations as well. 697*91f16700Schasinglulu *****************************************************************************/ 698*91f16700Schasinglulu int psci_validate_suspend_req(const psci_power_state_t *state_info, 699*91f16700Schasinglulu unsigned int is_power_down_state) 700*91f16700Schasinglulu { 701*91f16700Schasinglulu unsigned int max_off_lvl, target_lvl, max_retn_lvl; 702*91f16700Schasinglulu plat_local_state_t state; 703*91f16700Schasinglulu plat_local_state_type_t req_state_type, deepest_state_type; 704*91f16700Schasinglulu int i; 705*91f16700Schasinglulu 706*91f16700Schasinglulu /* Find the target suspend power level */ 707*91f16700Schasinglulu target_lvl = psci_find_target_suspend_lvl(state_info); 708*91f16700Schasinglulu if (target_lvl == PSCI_INVALID_PWR_LVL) 709*91f16700Schasinglulu return PSCI_E_INVALID_PARAMS; 710*91f16700Schasinglulu 711*91f16700Schasinglulu /* All power domain levels are in a RUN state to begin with */ 712*91f16700Schasinglulu deepest_state_type = STATE_TYPE_RUN; 713*91f16700Schasinglulu 714*91f16700Schasinglulu for (i = (int) target_lvl; i >= (int) PSCI_CPU_PWR_LVL; i--) { 715*91f16700Schasinglulu state = state_info->pwr_domain_state[i]; 716*91f16700Schasinglulu req_state_type = find_local_state_type(state); 717*91f16700Schasinglulu 718*91f16700Schasinglulu /* 719*91f16700Schasinglulu * While traversing from the highest power level to the lowest, 720*91f16700Schasinglulu * the state requested for lower levels has to be the same or 721*91f16700Schasinglulu * deeper i.e. equal to or greater than the state at the higher 722*91f16700Schasinglulu * levels. If this condition is true, then the requested state 723*91f16700Schasinglulu * becomes the deepest state encountered so far. 724*91f16700Schasinglulu */ 725*91f16700Schasinglulu if (req_state_type < deepest_state_type) 726*91f16700Schasinglulu return PSCI_E_INVALID_PARAMS; 727*91f16700Schasinglulu deepest_state_type = req_state_type; 728*91f16700Schasinglulu } 729*91f16700Schasinglulu 730*91f16700Schasinglulu /* Find the highest off power level */ 731*91f16700Schasinglulu max_off_lvl = psci_find_max_off_lvl(state_info); 732*91f16700Schasinglulu 733*91f16700Schasinglulu /* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */ 734*91f16700Schasinglulu max_retn_lvl = PSCI_INVALID_PWR_LVL; 735*91f16700Schasinglulu if (target_lvl != max_off_lvl) 736*91f16700Schasinglulu max_retn_lvl = target_lvl; 737*91f16700Schasinglulu 738*91f16700Schasinglulu /* 739*91f16700Schasinglulu * If this is not a request for a power down state then max off level 740*91f16700Schasinglulu * has to be invalid and max retention level has to be a valid power 741*91f16700Schasinglulu * level. 742*91f16700Schasinglulu */ 743*91f16700Schasinglulu if ((is_power_down_state == 0U) && 744*91f16700Schasinglulu ((max_off_lvl != PSCI_INVALID_PWR_LVL) || 745*91f16700Schasinglulu (max_retn_lvl == PSCI_INVALID_PWR_LVL))) 746*91f16700Schasinglulu return PSCI_E_INVALID_PARAMS; 747*91f16700Schasinglulu 748*91f16700Schasinglulu return PSCI_E_SUCCESS; 749*91f16700Schasinglulu } 750*91f16700Schasinglulu 751*91f16700Schasinglulu /****************************************************************************** 752*91f16700Schasinglulu * This function finds the highest power level which will be powered down 753*91f16700Schasinglulu * amongst all the power levels specified in the 'state_info' structure 754*91f16700Schasinglulu *****************************************************************************/ 755*91f16700Schasinglulu unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info) 756*91f16700Schasinglulu { 757*91f16700Schasinglulu int i; 758*91f16700Schasinglulu 759*91f16700Schasinglulu for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) { 760*91f16700Schasinglulu if (is_local_state_off(state_info->pwr_domain_state[i]) != 0) 761*91f16700Schasinglulu return (unsigned int) i; 762*91f16700Schasinglulu } 763*91f16700Schasinglulu 764*91f16700Schasinglulu return PSCI_INVALID_PWR_LVL; 765*91f16700Schasinglulu } 766*91f16700Schasinglulu 767*91f16700Schasinglulu /****************************************************************************** 768*91f16700Schasinglulu * This functions finds the level of the highest power domain which will be 769*91f16700Schasinglulu * placed in a low power state during a suspend operation. 770*91f16700Schasinglulu *****************************************************************************/ 771*91f16700Schasinglulu unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info) 772*91f16700Schasinglulu { 773*91f16700Schasinglulu int i; 774*91f16700Schasinglulu 775*91f16700Schasinglulu for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) { 776*91f16700Schasinglulu if (is_local_state_run(state_info->pwr_domain_state[i]) == 0) 777*91f16700Schasinglulu return (unsigned int) i; 778*91f16700Schasinglulu } 779*91f16700Schasinglulu 780*91f16700Schasinglulu return PSCI_INVALID_PWR_LVL; 781*91f16700Schasinglulu } 782*91f16700Schasinglulu 783*91f16700Schasinglulu /******************************************************************************* 784*91f16700Schasinglulu * This function is passed the highest level in the topology tree that the 785*91f16700Schasinglulu * operation should be applied to and a list of node indexes. It picks up locks 786*91f16700Schasinglulu * from the node index list in order of increasing power domain level in the 787*91f16700Schasinglulu * range specified. 788*91f16700Schasinglulu ******************************************************************************/ 789*91f16700Schasinglulu void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl, 790*91f16700Schasinglulu const unsigned int *parent_nodes) 791*91f16700Schasinglulu { 792*91f16700Schasinglulu unsigned int parent_idx; 793*91f16700Schasinglulu unsigned int level; 794*91f16700Schasinglulu 795*91f16700Schasinglulu /* No locking required for level 0. Hence start locking from level 1 */ 796*91f16700Schasinglulu for (level = PSCI_CPU_PWR_LVL + 1U; level <= end_pwrlvl; level++) { 797*91f16700Schasinglulu parent_idx = parent_nodes[level - 1U]; 798*91f16700Schasinglulu psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]); 799*91f16700Schasinglulu } 800*91f16700Schasinglulu } 801*91f16700Schasinglulu 802*91f16700Schasinglulu /******************************************************************************* 803*91f16700Schasinglulu * This function is passed the highest level in the topology tree that the 804*91f16700Schasinglulu * operation should be applied to and a list of node indexes. It releases the 805*91f16700Schasinglulu * locks in order of decreasing power domain level in the range specified. 806*91f16700Schasinglulu ******************************************************************************/ 807*91f16700Schasinglulu void psci_release_pwr_domain_locks(unsigned int end_pwrlvl, 808*91f16700Schasinglulu const unsigned int *parent_nodes) 809*91f16700Schasinglulu { 810*91f16700Schasinglulu unsigned int parent_idx; 811*91f16700Schasinglulu unsigned int level; 812*91f16700Schasinglulu 813*91f16700Schasinglulu /* Unlock top down. No unlocking required for level 0. */ 814*91f16700Schasinglulu for (level = end_pwrlvl; level >= (PSCI_CPU_PWR_LVL + 1U); level--) { 815*91f16700Schasinglulu parent_idx = parent_nodes[level - 1U]; 816*91f16700Schasinglulu psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]); 817*91f16700Schasinglulu } 818*91f16700Schasinglulu } 819*91f16700Schasinglulu 820*91f16700Schasinglulu /******************************************************************************* 821*91f16700Schasinglulu * This function determines the full entrypoint information for the requested 822*91f16700Schasinglulu * PSCI entrypoint on power on/resume and returns it. 823*91f16700Schasinglulu ******************************************************************************/ 824*91f16700Schasinglulu #ifdef __aarch64__ 825*91f16700Schasinglulu static int psci_get_ns_ep_info(entry_point_info_t *ep, 826*91f16700Schasinglulu uintptr_t entrypoint, 827*91f16700Schasinglulu u_register_t context_id) 828*91f16700Schasinglulu { 829*91f16700Schasinglulu u_register_t ep_attr, sctlr; 830*91f16700Schasinglulu unsigned int daif, ee, mode; 831*91f16700Schasinglulu u_register_t ns_scr_el3 = read_scr_el3(); 832*91f16700Schasinglulu u_register_t ns_sctlr_el1 = read_sctlr_el1(); 833*91f16700Schasinglulu 834*91f16700Schasinglulu sctlr = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? 835*91f16700Schasinglulu read_sctlr_el2() : ns_sctlr_el1; 836*91f16700Schasinglulu ee = 0; 837*91f16700Schasinglulu 838*91f16700Schasinglulu ep_attr = NON_SECURE | EP_ST_DISABLE; 839*91f16700Schasinglulu if ((sctlr & SCTLR_EE_BIT) != 0U) { 840*91f16700Schasinglulu ep_attr |= EP_EE_BIG; 841*91f16700Schasinglulu ee = 1; 842*91f16700Schasinglulu } 843*91f16700Schasinglulu SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr); 844*91f16700Schasinglulu 845*91f16700Schasinglulu ep->pc = entrypoint; 846*91f16700Schasinglulu zeromem(&ep->args, sizeof(ep->args)); 847*91f16700Schasinglulu ep->args.arg0 = context_id; 848*91f16700Schasinglulu 849*91f16700Schasinglulu /* 850*91f16700Schasinglulu * Figure out whether the cpu enters the non-secure address space 851*91f16700Schasinglulu * in aarch32 or aarch64 852*91f16700Schasinglulu */ 853*91f16700Schasinglulu if ((ns_scr_el3 & SCR_RW_BIT) != 0U) { 854*91f16700Schasinglulu 855*91f16700Schasinglulu /* 856*91f16700Schasinglulu * Check whether a Thumb entry point has been provided for an 857*91f16700Schasinglulu * aarch64 EL 858*91f16700Schasinglulu */ 859*91f16700Schasinglulu if ((entrypoint & 0x1UL) != 0UL) 860*91f16700Schasinglulu return PSCI_E_INVALID_ADDRESS; 861*91f16700Schasinglulu 862*91f16700Schasinglulu mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? MODE_EL2 : MODE_EL1; 863*91f16700Schasinglulu 864*91f16700Schasinglulu ep->spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX, 865*91f16700Schasinglulu DISABLE_ALL_EXCEPTIONS); 866*91f16700Schasinglulu } else { 867*91f16700Schasinglulu 868*91f16700Schasinglulu mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? 869*91f16700Schasinglulu MODE32_hyp : MODE32_svc; 870*91f16700Schasinglulu 871*91f16700Schasinglulu /* 872*91f16700Schasinglulu * TODO: Choose async. exception bits if HYP mode is not 873*91f16700Schasinglulu * implemented according to the values of SCR.{AW, FW} bits 874*91f16700Schasinglulu */ 875*91f16700Schasinglulu daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT; 876*91f16700Schasinglulu 877*91f16700Schasinglulu ep->spsr = SPSR_MODE32((uint64_t)mode, entrypoint & 0x1, ee, 878*91f16700Schasinglulu daif); 879*91f16700Schasinglulu } 880*91f16700Schasinglulu 881*91f16700Schasinglulu return PSCI_E_SUCCESS; 882*91f16700Schasinglulu } 883*91f16700Schasinglulu #else /* !__aarch64__ */ 884*91f16700Schasinglulu static int psci_get_ns_ep_info(entry_point_info_t *ep, 885*91f16700Schasinglulu uintptr_t entrypoint, 886*91f16700Schasinglulu u_register_t context_id) 887*91f16700Schasinglulu { 888*91f16700Schasinglulu u_register_t ep_attr; 889*91f16700Schasinglulu unsigned int aif, ee, mode; 890*91f16700Schasinglulu u_register_t scr = read_scr(); 891*91f16700Schasinglulu u_register_t ns_sctlr, sctlr; 892*91f16700Schasinglulu 893*91f16700Schasinglulu /* Switch to non secure state */ 894*91f16700Schasinglulu write_scr(scr | SCR_NS_BIT); 895*91f16700Schasinglulu isb(); 896*91f16700Schasinglulu ns_sctlr = read_sctlr(); 897*91f16700Schasinglulu 898*91f16700Schasinglulu sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr; 899*91f16700Schasinglulu 900*91f16700Schasinglulu /* Return to original state */ 901*91f16700Schasinglulu write_scr(scr); 902*91f16700Schasinglulu isb(); 903*91f16700Schasinglulu ee = 0; 904*91f16700Schasinglulu 905*91f16700Schasinglulu ep_attr = NON_SECURE | EP_ST_DISABLE; 906*91f16700Schasinglulu if (sctlr & SCTLR_EE_BIT) { 907*91f16700Schasinglulu ep_attr |= EP_EE_BIG; 908*91f16700Schasinglulu ee = 1; 909*91f16700Schasinglulu } 910*91f16700Schasinglulu SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr); 911*91f16700Schasinglulu 912*91f16700Schasinglulu ep->pc = entrypoint; 913*91f16700Schasinglulu zeromem(&ep->args, sizeof(ep->args)); 914*91f16700Schasinglulu ep->args.arg0 = context_id; 915*91f16700Schasinglulu 916*91f16700Schasinglulu mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc; 917*91f16700Schasinglulu 918*91f16700Schasinglulu /* 919*91f16700Schasinglulu * TODO: Choose async. exception bits if HYP mode is not 920*91f16700Schasinglulu * implemented according to the values of SCR.{AW, FW} bits 921*91f16700Schasinglulu */ 922*91f16700Schasinglulu aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT; 923*91f16700Schasinglulu 924*91f16700Schasinglulu ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif); 925*91f16700Schasinglulu 926*91f16700Schasinglulu return PSCI_E_SUCCESS; 927*91f16700Schasinglulu } 928*91f16700Schasinglulu 929*91f16700Schasinglulu #endif /* __aarch64__ */ 930*91f16700Schasinglulu 931*91f16700Schasinglulu /******************************************************************************* 932*91f16700Schasinglulu * This function validates the entrypoint with the platform layer if the 933*91f16700Schasinglulu * appropriate pm_ops hook is exported by the platform and returns the 934*91f16700Schasinglulu * 'entry_point_info'. 935*91f16700Schasinglulu ******************************************************************************/ 936*91f16700Schasinglulu int psci_validate_entry_point(entry_point_info_t *ep, 937*91f16700Schasinglulu uintptr_t entrypoint, 938*91f16700Schasinglulu u_register_t context_id) 939*91f16700Schasinglulu { 940*91f16700Schasinglulu int rc; 941*91f16700Schasinglulu 942*91f16700Schasinglulu /* Validate the entrypoint using platform psci_ops */ 943*91f16700Schasinglulu if (psci_plat_pm_ops->validate_ns_entrypoint != NULL) { 944*91f16700Schasinglulu rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint); 945*91f16700Schasinglulu if (rc != PSCI_E_SUCCESS) 946*91f16700Schasinglulu return PSCI_E_INVALID_ADDRESS; 947*91f16700Schasinglulu } 948*91f16700Schasinglulu 949*91f16700Schasinglulu /* 950*91f16700Schasinglulu * Verify and derive the re-entry information for 951*91f16700Schasinglulu * the non-secure world from the non-secure state from 952*91f16700Schasinglulu * where this call originated. 953*91f16700Schasinglulu */ 954*91f16700Schasinglulu rc = psci_get_ns_ep_info(ep, entrypoint, context_id); 955*91f16700Schasinglulu return rc; 956*91f16700Schasinglulu } 957*91f16700Schasinglulu 958*91f16700Schasinglulu /******************************************************************************* 959*91f16700Schasinglulu * Generic handler which is called when a cpu is physically powered on. It 960*91f16700Schasinglulu * traverses the node information and finds the highest power level powered 961*91f16700Schasinglulu * off and performs generic, architectural, platform setup and state management 962*91f16700Schasinglulu * to power on that power level and power levels below it. 963*91f16700Schasinglulu * e.g. For a cpu that's been powered on, it will call the platform specific 964*91f16700Schasinglulu * code to enable the gic cpu interface and for a cluster it will enable 965*91f16700Schasinglulu * coherency at the interconnect level in addition to gic cpu interface. 966*91f16700Schasinglulu ******************************************************************************/ 967*91f16700Schasinglulu void psci_warmboot_entrypoint(void) 968*91f16700Schasinglulu { 969*91f16700Schasinglulu unsigned int end_pwrlvl; 970*91f16700Schasinglulu unsigned int cpu_idx = plat_my_core_pos(); 971*91f16700Schasinglulu unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 972*91f16700Schasinglulu psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} }; 973*91f16700Schasinglulu 974*91f16700Schasinglulu /* Init registers that never change for the lifetime of TF-A */ 975*91f16700Schasinglulu cm_manage_extensions_el3(); 976*91f16700Schasinglulu 977*91f16700Schasinglulu /* 978*91f16700Schasinglulu * Verify that we have been explicitly turned ON or resumed from 979*91f16700Schasinglulu * suspend. 980*91f16700Schasinglulu */ 981*91f16700Schasinglulu if (psci_get_aff_info_state() == AFF_STATE_OFF) { 982*91f16700Schasinglulu ERROR("Unexpected affinity info state.\n"); 983*91f16700Schasinglulu panic(); 984*91f16700Schasinglulu } 985*91f16700Schasinglulu 986*91f16700Schasinglulu /* 987*91f16700Schasinglulu * Get the maximum power domain level to traverse to after this cpu 988*91f16700Schasinglulu * has been physically powered up. 989*91f16700Schasinglulu */ 990*91f16700Schasinglulu end_pwrlvl = get_power_on_target_pwrlvl(); 991*91f16700Schasinglulu 992*91f16700Schasinglulu /* Get the parent nodes */ 993*91f16700Schasinglulu psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes); 994*91f16700Schasinglulu 995*91f16700Schasinglulu /* 996*91f16700Schasinglulu * This function acquires the lock corresponding to each power level so 997*91f16700Schasinglulu * that by the time all locks are taken, the system topology is snapshot 998*91f16700Schasinglulu * and state management can be done safely. 999*91f16700Schasinglulu */ 1000*91f16700Schasinglulu psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 1001*91f16700Schasinglulu 1002*91f16700Schasinglulu psci_get_target_local_pwr_states(end_pwrlvl, &state_info); 1003*91f16700Schasinglulu 1004*91f16700Schasinglulu #if ENABLE_PSCI_STAT 1005*91f16700Schasinglulu plat_psci_stat_accounting_stop(&state_info); 1006*91f16700Schasinglulu #endif 1007*91f16700Schasinglulu 1008*91f16700Schasinglulu /* 1009*91f16700Schasinglulu * This CPU could be resuming from suspend or it could have just been 1010*91f16700Schasinglulu * turned on. To distinguish between these 2 cases, we examine the 1011*91f16700Schasinglulu * affinity state of the CPU: 1012*91f16700Schasinglulu * - If the affinity state is ON_PENDING then it has just been 1013*91f16700Schasinglulu * turned on. 1014*91f16700Schasinglulu * - Else it is resuming from suspend. 1015*91f16700Schasinglulu * 1016*91f16700Schasinglulu * Depending on the type of warm reset identified, choose the right set 1017*91f16700Schasinglulu * of power management handler and perform the generic, architecture 1018*91f16700Schasinglulu * and platform specific handling. 1019*91f16700Schasinglulu */ 1020*91f16700Schasinglulu if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING) 1021*91f16700Schasinglulu psci_cpu_on_finish(cpu_idx, &state_info); 1022*91f16700Schasinglulu else 1023*91f16700Schasinglulu psci_cpu_suspend_finish(cpu_idx, &state_info); 1024*91f16700Schasinglulu 1025*91f16700Schasinglulu /* 1026*91f16700Schasinglulu * Generic management: Now we just need to retrieve the 1027*91f16700Schasinglulu * information that we had stashed away during the cpu_on 1028*91f16700Schasinglulu * call to set this cpu on its way. 1029*91f16700Schasinglulu */ 1030*91f16700Schasinglulu cm_prepare_el3_exit_ns(); 1031*91f16700Schasinglulu 1032*91f16700Schasinglulu /* 1033*91f16700Schasinglulu * Set the requested and target state of this CPU and all the higher 1034*91f16700Schasinglulu * power domains which are ancestors of this CPU to run. 1035*91f16700Schasinglulu */ 1036*91f16700Schasinglulu psci_set_pwr_domains_to_run(end_pwrlvl); 1037*91f16700Schasinglulu 1038*91f16700Schasinglulu #if ENABLE_PSCI_STAT 1039*91f16700Schasinglulu /* 1040*91f16700Schasinglulu * Update PSCI stats. 1041*91f16700Schasinglulu * Caches are off when writing stats data on the power down path. 1042*91f16700Schasinglulu * Since caches are now enabled, it's necessary to do cache 1043*91f16700Schasinglulu * maintenance before reading that same data. 1044*91f16700Schasinglulu */ 1045*91f16700Schasinglulu psci_stats_update_pwr_up(end_pwrlvl, &state_info); 1046*91f16700Schasinglulu #endif 1047*91f16700Schasinglulu 1048*91f16700Schasinglulu /* 1049*91f16700Schasinglulu * This loop releases the lock corresponding to each power level 1050*91f16700Schasinglulu * in the reverse order to which they were acquired. 1051*91f16700Schasinglulu */ 1052*91f16700Schasinglulu psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 1053*91f16700Schasinglulu } 1054*91f16700Schasinglulu 1055*91f16700Schasinglulu /******************************************************************************* 1056*91f16700Schasinglulu * This function initializes the set of hooks that PSCI invokes as part of power 1057*91f16700Schasinglulu * management operation. The power management hooks are expected to be provided 1058*91f16700Schasinglulu * by the SPD, after it finishes all its initialization 1059*91f16700Schasinglulu ******************************************************************************/ 1060*91f16700Schasinglulu void psci_register_spd_pm_hook(const spd_pm_ops_t *pm) 1061*91f16700Schasinglulu { 1062*91f16700Schasinglulu assert(pm != NULL); 1063*91f16700Schasinglulu psci_spd_pm = pm; 1064*91f16700Schasinglulu 1065*91f16700Schasinglulu if (pm->svc_migrate != NULL) 1066*91f16700Schasinglulu psci_caps |= define_psci_cap(PSCI_MIG_AARCH64); 1067*91f16700Schasinglulu 1068*91f16700Schasinglulu if (pm->svc_migrate_info != NULL) 1069*91f16700Schasinglulu psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64) 1070*91f16700Schasinglulu | define_psci_cap(PSCI_MIG_INFO_TYPE); 1071*91f16700Schasinglulu } 1072*91f16700Schasinglulu 1073*91f16700Schasinglulu /******************************************************************************* 1074*91f16700Schasinglulu * This function invokes the migrate info hook in the spd_pm_ops. It performs 1075*91f16700Schasinglulu * the necessary return value validation. If the Secure Payload is UP and 1076*91f16700Schasinglulu * migrate capable, it returns the mpidr of the CPU on which the Secure payload 1077*91f16700Schasinglulu * is resident through the mpidr parameter. Else the value of the parameter on 1078*91f16700Schasinglulu * return is undefined. 1079*91f16700Schasinglulu ******************************************************************************/ 1080*91f16700Schasinglulu int psci_spd_migrate_info(u_register_t *mpidr) 1081*91f16700Schasinglulu { 1082*91f16700Schasinglulu int rc; 1083*91f16700Schasinglulu 1084*91f16700Schasinglulu if ((psci_spd_pm == NULL) || (psci_spd_pm->svc_migrate_info == NULL)) 1085*91f16700Schasinglulu return PSCI_E_NOT_SUPPORTED; 1086*91f16700Schasinglulu 1087*91f16700Schasinglulu rc = psci_spd_pm->svc_migrate_info(mpidr); 1088*91f16700Schasinglulu 1089*91f16700Schasinglulu assert((rc == PSCI_TOS_UP_MIG_CAP) || (rc == PSCI_TOS_NOT_UP_MIG_CAP) || 1090*91f16700Schasinglulu (rc == PSCI_TOS_NOT_PRESENT_MP) || (rc == PSCI_E_NOT_SUPPORTED)); 1091*91f16700Schasinglulu 1092*91f16700Schasinglulu return rc; 1093*91f16700Schasinglulu } 1094*91f16700Schasinglulu 1095*91f16700Schasinglulu 1096*91f16700Schasinglulu /******************************************************************************* 1097*91f16700Schasinglulu * This function prints the state of all power domains present in the 1098*91f16700Schasinglulu * system 1099*91f16700Schasinglulu ******************************************************************************/ 1100*91f16700Schasinglulu void psci_print_power_domain_map(void) 1101*91f16700Schasinglulu { 1102*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_INFO 1103*91f16700Schasinglulu unsigned int idx; 1104*91f16700Schasinglulu plat_local_state_t state; 1105*91f16700Schasinglulu plat_local_state_type_t state_type; 1106*91f16700Schasinglulu 1107*91f16700Schasinglulu /* This array maps to the PSCI_STATE_X definitions in psci.h */ 1108*91f16700Schasinglulu static const char * const psci_state_type_str[] = { 1109*91f16700Schasinglulu "ON", 1110*91f16700Schasinglulu "RETENTION", 1111*91f16700Schasinglulu "OFF", 1112*91f16700Schasinglulu }; 1113*91f16700Schasinglulu 1114*91f16700Schasinglulu INFO("PSCI Power Domain Map:\n"); 1115*91f16700Schasinglulu for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - psci_plat_core_count); 1116*91f16700Schasinglulu idx++) { 1117*91f16700Schasinglulu state_type = find_local_state_type( 1118*91f16700Schasinglulu psci_non_cpu_pd_nodes[idx].local_state); 1119*91f16700Schasinglulu INFO(" Domain Node : Level %u, parent_node %u," 1120*91f16700Schasinglulu " State %s (0x%x)\n", 1121*91f16700Schasinglulu psci_non_cpu_pd_nodes[idx].level, 1122*91f16700Schasinglulu psci_non_cpu_pd_nodes[idx].parent_node, 1123*91f16700Schasinglulu psci_state_type_str[state_type], 1124*91f16700Schasinglulu psci_non_cpu_pd_nodes[idx].local_state); 1125*91f16700Schasinglulu } 1126*91f16700Schasinglulu 1127*91f16700Schasinglulu for (idx = 0; idx < psci_plat_core_count; idx++) { 1128*91f16700Schasinglulu state = psci_get_cpu_local_state_by_idx(idx); 1129*91f16700Schasinglulu state_type = find_local_state_type(state); 1130*91f16700Schasinglulu INFO(" CPU Node : MPID 0x%llx, parent_node %u," 1131*91f16700Schasinglulu " State %s (0x%x)\n", 1132*91f16700Schasinglulu (unsigned long long)psci_cpu_pd_nodes[idx].mpidr, 1133*91f16700Schasinglulu psci_cpu_pd_nodes[idx].parent_node, 1134*91f16700Schasinglulu psci_state_type_str[state_type], 1135*91f16700Schasinglulu psci_get_cpu_local_state_by_idx(idx)); 1136*91f16700Schasinglulu } 1137*91f16700Schasinglulu #endif 1138*91f16700Schasinglulu } 1139*91f16700Schasinglulu 1140*91f16700Schasinglulu /****************************************************************************** 1141*91f16700Schasinglulu * Return whether any secondaries were powered up with CPU_ON call. A CPU that 1142*91f16700Schasinglulu * have ever been powered up would have set its MPDIR value to something other 1143*91f16700Schasinglulu * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to 1144*91f16700Schasinglulu * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is 1145*91f16700Schasinglulu * meaningful only when called on the primary CPU during early boot. 1146*91f16700Schasinglulu *****************************************************************************/ 1147*91f16700Schasinglulu int psci_secondaries_brought_up(void) 1148*91f16700Schasinglulu { 1149*91f16700Schasinglulu unsigned int idx, n_valid = 0U; 1150*91f16700Schasinglulu 1151*91f16700Schasinglulu for (idx = 0U; idx < ARRAY_SIZE(psci_cpu_pd_nodes); idx++) { 1152*91f16700Schasinglulu if (psci_cpu_pd_nodes[idx].mpidr != PSCI_INVALID_MPIDR) 1153*91f16700Schasinglulu n_valid++; 1154*91f16700Schasinglulu } 1155*91f16700Schasinglulu 1156*91f16700Schasinglulu assert(n_valid > 0U); 1157*91f16700Schasinglulu 1158*91f16700Schasinglulu return (n_valid > 1U) ? 1 : 0; 1159*91f16700Schasinglulu } 1160*91f16700Schasinglulu 1161*91f16700Schasinglulu /******************************************************************************* 1162*91f16700Schasinglulu * Initiate power down sequence, by calling power down operations registered for 1163*91f16700Schasinglulu * this CPU. 1164*91f16700Schasinglulu ******************************************************************************/ 1165*91f16700Schasinglulu void psci_pwrdown_cpu(unsigned int power_level) 1166*91f16700Schasinglulu { 1167*91f16700Schasinglulu #if HW_ASSISTED_COHERENCY 1168*91f16700Schasinglulu /* 1169*91f16700Schasinglulu * With hardware-assisted coherency, the CPU drivers only initiate the 1170*91f16700Schasinglulu * power down sequence, without performing cache-maintenance operations 1171*91f16700Schasinglulu * in software. Data caches enabled both before and after this call. 1172*91f16700Schasinglulu */ 1173*91f16700Schasinglulu prepare_cpu_pwr_dwn(power_level); 1174*91f16700Schasinglulu #else 1175*91f16700Schasinglulu /* 1176*91f16700Schasinglulu * Without hardware-assisted coherency, the CPU drivers disable data 1177*91f16700Schasinglulu * caches, then perform cache-maintenance operations in software. 1178*91f16700Schasinglulu * 1179*91f16700Schasinglulu * This also calls prepare_cpu_pwr_dwn() to initiate power down 1180*91f16700Schasinglulu * sequence, but that function will return with data caches disabled. 1181*91f16700Schasinglulu * We must ensure that the stack memory is flushed out to memory before 1182*91f16700Schasinglulu * we start popping from it again. 1183*91f16700Schasinglulu */ 1184*91f16700Schasinglulu psci_do_pwrdown_cache_maintenance(power_level); 1185*91f16700Schasinglulu #endif 1186*91f16700Schasinglulu } 1187*91f16700Schasinglulu 1188*91f16700Schasinglulu /******************************************************************************* 1189*91f16700Schasinglulu * This function invokes the callback 'stop_func()' with the 'mpidr' of each 1190*91f16700Schasinglulu * online PE. Caller can pass suitable method to stop a remote core. 1191*91f16700Schasinglulu * 1192*91f16700Schasinglulu * 'wait_ms' is the timeout value in milliseconds for the other cores to 1193*91f16700Schasinglulu * transition to power down state. Passing '0' makes it non-blocking. 1194*91f16700Schasinglulu * 1195*91f16700Schasinglulu * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the 1196*91f16700Schasinglulu * given timeout. 1197*91f16700Schasinglulu ******************************************************************************/ 1198*91f16700Schasinglulu int psci_stop_other_cores(unsigned int wait_ms, 1199*91f16700Schasinglulu void (*stop_func)(u_register_t mpidr)) 1200*91f16700Schasinglulu { 1201*91f16700Schasinglulu unsigned int idx, this_cpu_idx; 1202*91f16700Schasinglulu 1203*91f16700Schasinglulu this_cpu_idx = plat_my_core_pos(); 1204*91f16700Schasinglulu 1205*91f16700Schasinglulu /* Invoke stop_func for each core */ 1206*91f16700Schasinglulu for (idx = 0U; idx < psci_plat_core_count; idx++) { 1207*91f16700Schasinglulu /* skip current CPU */ 1208*91f16700Schasinglulu if (idx == this_cpu_idx) { 1209*91f16700Schasinglulu continue; 1210*91f16700Schasinglulu } 1211*91f16700Schasinglulu 1212*91f16700Schasinglulu /* Check if the CPU is ON */ 1213*91f16700Schasinglulu if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) { 1214*91f16700Schasinglulu (*stop_func)(psci_cpu_pd_nodes[idx].mpidr); 1215*91f16700Schasinglulu } 1216*91f16700Schasinglulu } 1217*91f16700Schasinglulu 1218*91f16700Schasinglulu /* Need to wait for other cores to shutdown */ 1219*91f16700Schasinglulu if (wait_ms != 0U) { 1220*91f16700Schasinglulu while ((wait_ms-- != 0U) && (!psci_is_last_on_cpu())) { 1221*91f16700Schasinglulu mdelay(1U); 1222*91f16700Schasinglulu } 1223*91f16700Schasinglulu 1224*91f16700Schasinglulu if (!psci_is_last_on_cpu()) { 1225*91f16700Schasinglulu WARN("Failed to stop all cores!\n"); 1226*91f16700Schasinglulu psci_print_power_domain_map(); 1227*91f16700Schasinglulu return PSCI_E_DENIED; 1228*91f16700Schasinglulu } 1229*91f16700Schasinglulu } 1230*91f16700Schasinglulu 1231*91f16700Schasinglulu return PSCI_E_SUCCESS; 1232*91f16700Schasinglulu } 1233*91f16700Schasinglulu 1234*91f16700Schasinglulu /******************************************************************************* 1235*91f16700Schasinglulu * This function verifies that all the other cores in the system have been 1236*91f16700Schasinglulu * turned OFF and the current CPU is the last running CPU in the system. 1237*91f16700Schasinglulu * Returns true if the current CPU is the last ON CPU or false otherwise. 1238*91f16700Schasinglulu * 1239*91f16700Schasinglulu * This API has following differences with psci_is_last_on_cpu 1240*91f16700Schasinglulu * 1. PSCI states are locked 1241*91f16700Schasinglulu ******************************************************************************/ 1242*91f16700Schasinglulu bool psci_is_last_on_cpu_safe(void) 1243*91f16700Schasinglulu { 1244*91f16700Schasinglulu unsigned int this_core = plat_my_core_pos(); 1245*91f16700Schasinglulu unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 1246*91f16700Schasinglulu 1247*91f16700Schasinglulu psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes); 1248*91f16700Schasinglulu 1249*91f16700Schasinglulu psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes); 1250*91f16700Schasinglulu 1251*91f16700Schasinglulu if (!psci_is_last_on_cpu()) { 1252*91f16700Schasinglulu psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes); 1253*91f16700Schasinglulu return false; 1254*91f16700Schasinglulu } 1255*91f16700Schasinglulu 1256*91f16700Schasinglulu psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes); 1257*91f16700Schasinglulu 1258*91f16700Schasinglulu return true; 1259*91f16700Schasinglulu } 1260*91f16700Schasinglulu 1261*91f16700Schasinglulu /******************************************************************************* 1262*91f16700Schasinglulu * This function verifies that all cores in the system have been turned ON. 1263*91f16700Schasinglulu * Returns true, if all CPUs are ON or false otherwise. 1264*91f16700Schasinglulu * 1265*91f16700Schasinglulu * This API has following differences with psci_are_all_cpus_on 1266*91f16700Schasinglulu * 1. PSCI states are locked 1267*91f16700Schasinglulu ******************************************************************************/ 1268*91f16700Schasinglulu bool psci_are_all_cpus_on_safe(void) 1269*91f16700Schasinglulu { 1270*91f16700Schasinglulu unsigned int this_core = plat_my_core_pos(); 1271*91f16700Schasinglulu unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 1272*91f16700Schasinglulu 1273*91f16700Schasinglulu psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes); 1274*91f16700Schasinglulu 1275*91f16700Schasinglulu psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes); 1276*91f16700Schasinglulu 1277*91f16700Schasinglulu if (!psci_are_all_cpus_on()) { 1278*91f16700Schasinglulu psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes); 1279*91f16700Schasinglulu return false; 1280*91f16700Schasinglulu } 1281*91f16700Schasinglulu 1282*91f16700Schasinglulu psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes); 1283*91f16700Schasinglulu 1284*91f16700Schasinglulu return true; 1285*91f16700Schasinglulu } 1286