1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * Copyright (c) 2023, NVIDIA Corporation. All rights reserved. 4*91f16700Schasinglulu * 5*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 6*91f16700Schasinglulu */ 7*91f16700Schasinglulu 8*91f16700Schasinglulu #include <assert.h> 9*91f16700Schasinglulu #include <string.h> 10*91f16700Schasinglulu 11*91f16700Schasinglulu #include <arch.h> 12*91f16700Schasinglulu #include <arch_helpers.h> 13*91f16700Schasinglulu #include <common/debug.h> 14*91f16700Schasinglulu #include <lib/pmf/pmf.h> 15*91f16700Schasinglulu #include <lib/runtime_instr.h> 16*91f16700Schasinglulu #include <plat/common/platform.h> 17*91f16700Schasinglulu 18*91f16700Schasinglulu #include "psci_private.h" 19*91f16700Schasinglulu 20*91f16700Schasinglulu /****************************************************************************** 21*91f16700Schasinglulu * Construct the psci_power_state to request power OFF at all power levels. 22*91f16700Schasinglulu ******************************************************************************/ 23*91f16700Schasinglulu static void psci_set_power_off_state(psci_power_state_t *state_info) 24*91f16700Schasinglulu { 25*91f16700Schasinglulu unsigned int lvl; 26*91f16700Schasinglulu 27*91f16700Schasinglulu for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++) 28*91f16700Schasinglulu state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE; 29*91f16700Schasinglulu } 30*91f16700Schasinglulu 31*91f16700Schasinglulu /****************************************************************************** 32*91f16700Schasinglulu * Top level handler which is called when a cpu wants to power itself down. 33*91f16700Schasinglulu * It's assumed that along with turning the cpu power domain off, power 34*91f16700Schasinglulu * domains at higher levels will be turned off as far as possible. It finds 35*91f16700Schasinglulu * the highest level where a domain has to be powered off by traversing the 36*91f16700Schasinglulu * node information and then performs generic, architectural, platform setup 37*91f16700Schasinglulu * and state management required to turn OFF that power domain and domains 38*91f16700Schasinglulu * below it. e.g. For a cpu that's to be powered OFF, it could mean programming 39*91f16700Schasinglulu * the power controller whereas for a cluster that's to be powered off, it will 40*91f16700Schasinglulu * call the platform specific code which will disable coherency at the 41*91f16700Schasinglulu * interconnect level if the cpu is the last in the cluster and also the 42*91f16700Schasinglulu * program the power controller. 43*91f16700Schasinglulu ******************************************************************************/ 44*91f16700Schasinglulu int psci_do_cpu_off(unsigned int end_pwrlvl) 45*91f16700Schasinglulu { 46*91f16700Schasinglulu int rc = PSCI_E_SUCCESS; 47*91f16700Schasinglulu unsigned int idx = plat_my_core_pos(); 48*91f16700Schasinglulu psci_power_state_t state_info; 49*91f16700Schasinglulu unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 50*91f16700Schasinglulu 51*91f16700Schasinglulu /* 52*91f16700Schasinglulu * This function must only be called on platforms where the 53*91f16700Schasinglulu * CPU_OFF platform hooks have been implemented. 54*91f16700Schasinglulu */ 55*91f16700Schasinglulu assert(psci_plat_pm_ops->pwr_domain_off != NULL); 56*91f16700Schasinglulu 57*91f16700Schasinglulu /* Construct the psci_power_state for CPU_OFF */ 58*91f16700Schasinglulu psci_set_power_off_state(&state_info); 59*91f16700Schasinglulu 60*91f16700Schasinglulu /* 61*91f16700Schasinglulu * Call the platform provided early CPU_OFF handler to allow 62*91f16700Schasinglulu * platforms to perform any housekeeping activities before 63*91f16700Schasinglulu * actually powering the CPU off. PSCI_E_DENIED indicates that 64*91f16700Schasinglulu * the CPU off sequence should be aborted at this time. 65*91f16700Schasinglulu */ 66*91f16700Schasinglulu if (psci_plat_pm_ops->pwr_domain_off_early) { 67*91f16700Schasinglulu rc = psci_plat_pm_ops->pwr_domain_off_early(&state_info); 68*91f16700Schasinglulu if (rc == PSCI_E_DENIED) { 69*91f16700Schasinglulu return rc; 70*91f16700Schasinglulu } 71*91f16700Schasinglulu } 72*91f16700Schasinglulu 73*91f16700Schasinglulu /* 74*91f16700Schasinglulu * Get the parent nodes here, this is important to do before we 75*91f16700Schasinglulu * initiate the power down sequence as after that point the core may 76*91f16700Schasinglulu * have exited coherency and its cache may be disabled, any access to 77*91f16700Schasinglulu * shared memory after that (such as the parent node lookup in 78*91f16700Schasinglulu * psci_cpu_pd_nodes) can cause coherency issues on some platforms. 79*91f16700Schasinglulu */ 80*91f16700Schasinglulu psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes); 81*91f16700Schasinglulu 82*91f16700Schasinglulu /* 83*91f16700Schasinglulu * This function acquires the lock corresponding to each power 84*91f16700Schasinglulu * level so that by the time all locks are taken, the system topology 85*91f16700Schasinglulu * is snapshot and state management can be done safely. 86*91f16700Schasinglulu */ 87*91f16700Schasinglulu psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 88*91f16700Schasinglulu 89*91f16700Schasinglulu /* 90*91f16700Schasinglulu * Call the cpu off handler registered by the Secure Payload Dispatcher 91*91f16700Schasinglulu * to let it do any bookkeeping. Assume that the SPD always reports an 92*91f16700Schasinglulu * E_DENIED error if SP refuse to power down 93*91f16700Schasinglulu */ 94*91f16700Schasinglulu if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) { 95*91f16700Schasinglulu rc = psci_spd_pm->svc_off(0); 96*91f16700Schasinglulu if (rc != 0) 97*91f16700Schasinglulu goto exit; 98*91f16700Schasinglulu } 99*91f16700Schasinglulu 100*91f16700Schasinglulu /* 101*91f16700Schasinglulu * This function is passed the requested state info and 102*91f16700Schasinglulu * it returns the negotiated state info for each power level upto 103*91f16700Schasinglulu * the end level specified. 104*91f16700Schasinglulu */ 105*91f16700Schasinglulu psci_do_state_coordination(end_pwrlvl, &state_info); 106*91f16700Schasinglulu 107*91f16700Schasinglulu /* Update the target state in the power domain nodes */ 108*91f16700Schasinglulu psci_set_target_local_pwr_states(end_pwrlvl, &state_info); 109*91f16700Schasinglulu 110*91f16700Schasinglulu #if ENABLE_PSCI_STAT 111*91f16700Schasinglulu /* Update the last cpu for each level till end_pwrlvl */ 112*91f16700Schasinglulu psci_stats_update_pwr_down(end_pwrlvl, &state_info); 113*91f16700Schasinglulu #endif 114*91f16700Schasinglulu 115*91f16700Schasinglulu #if ENABLE_RUNTIME_INSTRUMENTATION 116*91f16700Schasinglulu 117*91f16700Schasinglulu /* 118*91f16700Schasinglulu * Flush cache line so that even if CPU power down happens 119*91f16700Schasinglulu * the timestamp update is reflected in memory. 120*91f16700Schasinglulu */ 121*91f16700Schasinglulu PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 122*91f16700Schasinglulu RT_INSTR_ENTER_CFLUSH, 123*91f16700Schasinglulu PMF_CACHE_MAINT); 124*91f16700Schasinglulu #endif 125*91f16700Schasinglulu 126*91f16700Schasinglulu /* 127*91f16700Schasinglulu * Arch. management. Initiate power down sequence. 128*91f16700Schasinglulu */ 129*91f16700Schasinglulu psci_pwrdown_cpu(psci_find_max_off_lvl(&state_info)); 130*91f16700Schasinglulu 131*91f16700Schasinglulu #if ENABLE_RUNTIME_INSTRUMENTATION 132*91f16700Schasinglulu PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 133*91f16700Schasinglulu RT_INSTR_EXIT_CFLUSH, 134*91f16700Schasinglulu PMF_NO_CACHE_MAINT); 135*91f16700Schasinglulu #endif 136*91f16700Schasinglulu 137*91f16700Schasinglulu /* 138*91f16700Schasinglulu * Plat. management: Perform platform specific actions to turn this 139*91f16700Schasinglulu * cpu off e.g. exit cpu coherency, program the power controller etc. 140*91f16700Schasinglulu */ 141*91f16700Schasinglulu psci_plat_pm_ops->pwr_domain_off(&state_info); 142*91f16700Schasinglulu 143*91f16700Schasinglulu #if ENABLE_PSCI_STAT 144*91f16700Schasinglulu plat_psci_stat_accounting_start(&state_info); 145*91f16700Schasinglulu #endif 146*91f16700Schasinglulu 147*91f16700Schasinglulu exit: 148*91f16700Schasinglulu /* 149*91f16700Schasinglulu * Release the locks corresponding to each power level in the 150*91f16700Schasinglulu * reverse order to which they were acquired. 151*91f16700Schasinglulu */ 152*91f16700Schasinglulu psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 153*91f16700Schasinglulu 154*91f16700Schasinglulu /* 155*91f16700Schasinglulu * Check if all actions needed to safely power down this cpu have 156*91f16700Schasinglulu * successfully completed. 157*91f16700Schasinglulu */ 158*91f16700Schasinglulu if (rc == PSCI_E_SUCCESS) { 159*91f16700Schasinglulu /* 160*91f16700Schasinglulu * Set the affinity info state to OFF. When caches are disabled, 161*91f16700Schasinglulu * this writes directly to main memory, so cache maintenance is 162*91f16700Schasinglulu * required to ensure that later cached reads of aff_info_state 163*91f16700Schasinglulu * return AFF_STATE_OFF. A dsbish() ensures ordering of the 164*91f16700Schasinglulu * update to the affinity info state prior to cache line 165*91f16700Schasinglulu * invalidation. 166*91f16700Schasinglulu */ 167*91f16700Schasinglulu psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state); 168*91f16700Schasinglulu psci_set_aff_info_state(AFF_STATE_OFF); 169*91f16700Schasinglulu psci_dsbish(); 170*91f16700Schasinglulu psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state); 171*91f16700Schasinglulu 172*91f16700Schasinglulu #if ENABLE_RUNTIME_INSTRUMENTATION 173*91f16700Schasinglulu 174*91f16700Schasinglulu /* 175*91f16700Schasinglulu * Update the timestamp with cache off. We assume this 176*91f16700Schasinglulu * timestamp can only be read from the current CPU and the 177*91f16700Schasinglulu * timestamp cache line will be flushed before return to 178*91f16700Schasinglulu * normal world on wakeup. 179*91f16700Schasinglulu */ 180*91f16700Schasinglulu PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 181*91f16700Schasinglulu RT_INSTR_ENTER_HW_LOW_PWR, 182*91f16700Schasinglulu PMF_NO_CACHE_MAINT); 183*91f16700Schasinglulu #endif 184*91f16700Schasinglulu 185*91f16700Schasinglulu if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) { 186*91f16700Schasinglulu /* This function must not return */ 187*91f16700Schasinglulu psci_plat_pm_ops->pwr_domain_pwr_down_wfi(&state_info); 188*91f16700Schasinglulu } else { 189*91f16700Schasinglulu /* 190*91f16700Schasinglulu * Enter a wfi loop which will allow the power 191*91f16700Schasinglulu * controller to physically power down this cpu. 192*91f16700Schasinglulu */ 193*91f16700Schasinglulu psci_power_down_wfi(); 194*91f16700Schasinglulu } 195*91f16700Schasinglulu } 196*91f16700Schasinglulu 197*91f16700Schasinglulu return rc; 198*91f16700Schasinglulu } 199