1532ed618SSoby Mathew /* 2*0c836554SBoyan Karatotev * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 709d40e0eSAntonio Nino Diaz #include <assert.h> 809d40e0eSAntonio Nino Diaz #include <stddef.h> 909d40e0eSAntonio Nino Diaz 10532ed618SSoby Mathew #include <arch.h> 11532ed618SSoby Mathew #include <arch_helpers.h> 1209d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1309d40e0eSAntonio Nino Diaz #include <common/debug.h> 14532ed618SSoby Mathew #include <context.h> 1509d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 1609d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 1709d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub_events.h> 1809d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 1909d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 2009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2109d40e0eSAntonio Nino Diaz 22532ed618SSoby Mathew #include "psci_private.h" 23532ed618SSoby Mathew 24532ed618SSoby Mathew /******************************************************************************* 25532ed618SSoby Mathew * This function does generic and platform specific operations after a wake-up 26532ed618SSoby Mathew * from standby/retention states at multiple power levels. 27532ed618SSoby Mathew ******************************************************************************/ 285b33ad17SDeepika Bhavnani static void psci_suspend_to_standby_finisher(unsigned int cpu_idx, 29532ed618SSoby Mathew unsigned int end_pwrlvl) 30532ed618SSoby Mathew { 3174d27d00SAndrew F. Davis unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 3261eae524SAchin Gupta psci_power_state_t state_info; 3361eae524SAchin Gupta 3474d27d00SAndrew F. Davis /* Get the parent nodes */ 3574d27d00SAndrew F. Davis psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes); 3674d27d00SAndrew F. Davis 3774d27d00SAndrew F. Davis psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 38532ed618SSoby Mathew 39532ed618SSoby Mathew /* 4061eae524SAchin Gupta * Find out which retention states this CPU has exited from until the 4161eae524SAchin Gupta * 'end_pwrlvl'. The exit retention state could be deeper than the entry 4261eae524SAchin Gupta * state as a result of state coordination amongst other CPUs post wfi. 4361eae524SAchin Gupta */ 4461eae524SAchin Gupta psci_get_target_local_pwr_states(end_pwrlvl, &state_info); 4561eae524SAchin Gupta 46bfc87a8dSSoby Mathew #if ENABLE_PSCI_STAT 47bfc87a8dSSoby Mathew plat_psci_stat_accounting_stop(&state_info); 48bfc87a8dSSoby Mathew psci_stats_update_pwr_up(end_pwrlvl, &state_info); 49bfc87a8dSSoby Mathew #endif 50bfc87a8dSSoby Mathew 5161eae524SAchin Gupta /* 52532ed618SSoby Mathew * Plat. management: Allow the platform to do operations 53532ed618SSoby Mathew * on waking up from retention. 54532ed618SSoby Mathew */ 5561eae524SAchin Gupta psci_plat_pm_ops->pwr_domain_suspend_finish(&state_info); 56532ed618SSoby Mathew 57*0c836554SBoyan Karatotev /* This loses its meaning when not suspending, reset so it's correct for OFF */ 58*0c836554SBoyan Karatotev psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL); 59*0c836554SBoyan Karatotev 60532ed618SSoby Mathew /* 61532ed618SSoby Mathew * Set the requested and target state of this CPU and all the higher 62532ed618SSoby Mathew * power domain levels for this CPU to run. 63532ed618SSoby Mathew */ 64532ed618SSoby Mathew psci_set_pwr_domains_to_run(end_pwrlvl); 65532ed618SSoby Mathew 6674d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 67532ed618SSoby Mathew } 68532ed618SSoby Mathew 69532ed618SSoby Mathew /******************************************************************************* 70532ed618SSoby Mathew * This function does generic and platform specific suspend to power down 71532ed618SSoby Mathew * operations. 72532ed618SSoby Mathew ******************************************************************************/ 73532ed618SSoby Mathew static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl, 74621d64f8SAntonio Nino Diaz const entry_point_info_t *ep, 75621d64f8SAntonio Nino Diaz const psci_power_state_t *state_info) 76532ed618SSoby Mathew { 77532ed618SSoby Mathew unsigned int max_off_lvl = psci_find_max_off_lvl(state_info); 78532ed618SSoby Mathew 797593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_start); 807593252cSDimitris Papastamos 81606b7430SWing Li #if PSCI_OS_INIT_MODE 82606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL 83606b7430SWing Li end_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL; 84606b7430SWing Li #else 85606b7430SWing Li end_pwrlvl = PLAT_MAX_PWR_LVL; 86606b7430SWing Li #endif 87606b7430SWing Li #endif 88606b7430SWing Li 89532ed618SSoby Mathew /* Save PSCI target power level for the suspend finisher handler */ 90532ed618SSoby Mathew psci_set_suspend_pwrlvl(end_pwrlvl); 91532ed618SSoby Mathew 92532ed618SSoby Mathew /* 93a10d3632SJeenu Viswambharan * Flush the target power level as it might be accessed on power up with 94532ed618SSoby Mathew * Data cache disabled. 95532ed618SSoby Mathew */ 96a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl); 97532ed618SSoby Mathew 98532ed618SSoby Mathew /* 99532ed618SSoby Mathew * Call the cpu suspend handler registered by the Secure Payload 100532ed618SSoby Mathew * Dispatcher to let it do any book-keeping. If the handler encounters an 101532ed618SSoby Mathew * error, it's expected to assert within 102532ed618SSoby Mathew */ 103621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL)) 104532ed618SSoby Mathew psci_spd_pm->svc_suspend(max_off_lvl); 105532ed618SSoby Mathew 1061862d620SVarun Wadekar #if !HW_ASSISTED_COHERENCY 1071862d620SVarun Wadekar /* 1081862d620SVarun Wadekar * Plat. management: Allow the platform to perform any early 1091862d620SVarun Wadekar * actions required to power down the CPU. This might be useful for 1101862d620SVarun Wadekar * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these 1111862d620SVarun Wadekar * actions with data caches enabled. 1121862d620SVarun Wadekar */ 113621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL) 1141862d620SVarun Wadekar psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info); 1151862d620SVarun Wadekar #endif 1161862d620SVarun Wadekar 117532ed618SSoby Mathew /* 118532ed618SSoby Mathew * Store the re-entry information for the non-secure world. 119532ed618SSoby Mathew */ 120532ed618SSoby Mathew cm_init_my_context(ep); 121532ed618SSoby Mathew 1227941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1237941816aSdp-arm 1247941816aSdp-arm /* 1257941816aSdp-arm * Flush cache line so that even if CPU power down happens 1267941816aSdp-arm * the timestamp update is reflected in memory. 1277941816aSdp-arm */ 1287941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1297941816aSdp-arm RT_INSTR_ENTER_CFLUSH, 1307941816aSdp-arm PMF_CACHE_MAINT); 1317941816aSdp-arm #endif 1327941816aSdp-arm 133532ed618SSoby Mathew /* 134b0408e87SJeenu Viswambharan * Arch. management. Initiate power down sequence. 135532ed618SSoby Mathew * TODO : Introduce a mechanism to query the cache level to flush 136532ed618SSoby Mathew * and the cpu-ops power down to perform from the platform. 137532ed618SSoby Mathew */ 13865bbb935SPranav Madhu psci_pwrdown_cpu(max_off_lvl); 1397941816aSdp-arm 1407941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1417941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1427941816aSdp-arm RT_INSTR_EXIT_CFLUSH, 1437941816aSdp-arm PMF_NO_CACHE_MAINT); 1447941816aSdp-arm #endif 145532ed618SSoby Mathew } 146532ed618SSoby Mathew 147532ed618SSoby Mathew /******************************************************************************* 148532ed618SSoby Mathew * Top level handler which is called when a cpu wants to suspend its execution. 149532ed618SSoby Mathew * It is assumed that along with suspending the cpu power domain, power domains 150532ed618SSoby Mathew * at higher levels until the target power level will be suspended as well. It 151532ed618SSoby Mathew * coordinates with the platform to negotiate the target state for each of 152532ed618SSoby Mathew * the power domain level till the target power domain level. It then performs 153532ed618SSoby Mathew * generic, architectural, platform setup and state management required to 154532ed618SSoby Mathew * suspend that power domain level and power domain levels below it. 155532ed618SSoby Mathew * e.g. For a cpu that's to be suspended, it could mean programming the 156532ed618SSoby Mathew * power controller whereas for a cluster that's to be suspended, it will call 157532ed618SSoby Mathew * the platform specific code which will disable coherency at the interconnect 158532ed618SSoby Mathew * level if the cpu is the last in the cluster and also the program the power 159532ed618SSoby Mathew * controller. 160532ed618SSoby Mathew * 161532ed618SSoby Mathew * All the required parameter checks are performed at the beginning and after 162532ed618SSoby Mathew * the state transition has been done, no further error is expected and it is 163532ed618SSoby Mathew * not possible to undo any of the actions taken beyond that point. 164532ed618SSoby Mathew ******************************************************************************/ 165606b7430SWing Li int psci_cpu_suspend_start(const entry_point_info_t *ep, 166532ed618SSoby Mathew unsigned int end_pwrlvl, 167532ed618SSoby Mathew psci_power_state_t *state_info, 168532ed618SSoby Mathew unsigned int is_power_down_state) 169532ed618SSoby Mathew { 170606b7430SWing Li int rc = PSCI_E_SUCCESS; 171606b7430SWing Li bool skip_wfi = false; 1725b33ad17SDeepika Bhavnani unsigned int idx = plat_my_core_pos(); 17374d27d00SAndrew F. Davis unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 174532ed618SSoby Mathew 175532ed618SSoby Mathew /* 176532ed618SSoby Mathew * This function must only be called on platforms where the 177532ed618SSoby Mathew * CPU_SUSPEND platform hooks have been implemented. 178532ed618SSoby Mathew */ 179621d64f8SAntonio Nino Diaz assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) && 180621d64f8SAntonio Nino Diaz (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)); 181532ed618SSoby Mathew 18274d27d00SAndrew F. Davis /* Get the parent nodes */ 18374d27d00SAndrew F. Davis psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes); 18474d27d00SAndrew F. Davis 185532ed618SSoby Mathew /* 186532ed618SSoby Mathew * This function acquires the lock corresponding to each power 187532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 188532ed618SSoby Mathew * is snapshot and state management can be done safely. 189532ed618SSoby Mathew */ 19074d27d00SAndrew F. Davis psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 191532ed618SSoby Mathew 192532ed618SSoby Mathew /* 193532ed618SSoby Mathew * We check if there are any pending interrupts after the delay 194532ed618SSoby Mathew * introduced by lock contention to increase the chances of early 195532ed618SSoby Mathew * detection that a wake-up interrupt has fired. 196532ed618SSoby Mathew */ 197621d64f8SAntonio Nino Diaz if (read_isr_el1() != 0U) { 198606b7430SWing Li skip_wfi = true; 199532ed618SSoby Mathew goto exit; 200532ed618SSoby Mathew } 201532ed618SSoby Mathew 202606b7430SWing Li #if PSCI_OS_INIT_MODE 203606b7430SWing Li if (psci_suspend_mode == OS_INIT) { 204606b7430SWing Li /* 205606b7430SWing Li * This function validates the requested state info for 206606b7430SWing Li * OS-initiated mode. 207606b7430SWing Li */ 208606b7430SWing Li rc = psci_validate_state_coordination(end_pwrlvl, state_info); 209606b7430SWing Li if (rc != PSCI_E_SUCCESS) { 210606b7430SWing Li skip_wfi = true; 211606b7430SWing Li goto exit; 212606b7430SWing Li } 213606b7430SWing Li } else { 214606b7430SWing Li #endif 215532ed618SSoby Mathew /* 216532ed618SSoby Mathew * This function is passed the requested state info and 217532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 218532ed618SSoby Mathew * the end level specified. 219532ed618SSoby Mathew */ 220532ed618SSoby Mathew psci_do_state_coordination(end_pwrlvl, state_info); 221606b7430SWing Li #if PSCI_OS_INIT_MODE 222606b7430SWing Li } 223606b7430SWing Li #endif 224532ed618SSoby Mathew 225d3488614SWing Li #if PSCI_OS_INIT_MODE 226d3488614SWing Li if (psci_plat_pm_ops->pwr_domain_validate_suspend != NULL) { 227d3488614SWing Li rc = psci_plat_pm_ops->pwr_domain_validate_suspend(state_info); 228d3488614SWing Li if (rc != PSCI_E_SUCCESS) { 229d3488614SWing Li skip_wfi = true; 230d3488614SWing Li goto exit; 231d3488614SWing Li } 232d3488614SWing Li } 233d3488614SWing Li #endif 234d3488614SWing Li 235d3488614SWing Li /* Update the target state in the power domain nodes */ 236d3488614SWing Li psci_set_target_local_pwr_states(end_pwrlvl, state_info); 237d3488614SWing Li 238532ed618SSoby Mathew #if ENABLE_PSCI_STAT 239532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 240532ed618SSoby Mathew psci_stats_update_pwr_down(end_pwrlvl, state_info); 241532ed618SSoby Mathew #endif 242532ed618SSoby Mathew 243621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) 244532ed618SSoby Mathew psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info); 245532ed618SSoby Mathew 246532ed618SSoby Mathew /* 247532ed618SSoby Mathew * Plat. management: Allow the platform to perform the 248532ed618SSoby Mathew * necessary actions to turn off this cpu e.g. set the 249532ed618SSoby Mathew * platform defined mailbox with the psci entrypoint, 250532ed618SSoby Mathew * program the power controller etc. 251532ed618SSoby Mathew */ 252606b7430SWing Li 253532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend(state_info); 254532ed618SSoby Mathew 255532ed618SSoby Mathew #if ENABLE_PSCI_STAT 25604c1db1eSdp-arm plat_psci_stat_accounting_start(state_info); 257532ed618SSoby Mathew #endif 258532ed618SSoby Mathew 259532ed618SSoby Mathew exit: 260532ed618SSoby Mathew /* 261532ed618SSoby Mathew * Release the locks corresponding to each power level in the 262532ed618SSoby Mathew * reverse order to which they were acquired. 263532ed618SSoby Mathew */ 26474d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 26574d27d00SAndrew F. Davis 266606b7430SWing Li if (skip_wfi) { 267606b7430SWing Li return rc; 268606b7430SWing Li } 269532ed618SSoby Mathew 270621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) { 271872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 272872be88aSdp-arm 273872be88aSdp-arm /* 274872be88aSdp-arm * Update the timestamp with cache off. We assume this 275872be88aSdp-arm * timestamp can only be read from the current CPU and the 276872be88aSdp-arm * timestamp cache line will be flushed before return to 277872be88aSdp-arm * normal world on wakeup. 278872be88aSdp-arm */ 279872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 280872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 281872be88aSdp-arm PMF_NO_CACHE_MAINT); 282872be88aSdp-arm #endif 283872be88aSdp-arm 284532ed618SSoby Mathew /* The function calls below must not return */ 285621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) 286532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info); 287532ed618SSoby Mathew else 288532ed618SSoby Mathew psci_power_down_wfi(); 289532ed618SSoby Mathew } 290532ed618SSoby Mathew 291872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 292872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 293872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 294872be88aSdp-arm PMF_NO_CACHE_MAINT); 295872be88aSdp-arm #endif 296872be88aSdp-arm 297532ed618SSoby Mathew /* 298532ed618SSoby Mathew * We will reach here if only retention/standby states have been 299532ed618SSoby Mathew * requested at multiple power levels. This means that the cpu 300532ed618SSoby Mathew * context will be preserved. 301532ed618SSoby Mathew */ 302532ed618SSoby Mathew wfi(); 303532ed618SSoby Mathew 304872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 305872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 306872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 307872be88aSdp-arm PMF_NO_CACHE_MAINT); 308872be88aSdp-arm #endif 309872be88aSdp-arm 310532ed618SSoby Mathew /* 311532ed618SSoby Mathew * After we wake up from context retaining suspend, call the 312532ed618SSoby Mathew * context retaining suspend finisher. 313532ed618SSoby Mathew */ 31461eae524SAchin Gupta psci_suspend_to_standby_finisher(idx, end_pwrlvl); 315606b7430SWing Li 316606b7430SWing Li return rc; 317532ed618SSoby Mathew } 318532ed618SSoby Mathew 319532ed618SSoby Mathew /******************************************************************************* 320532ed618SSoby Mathew * The following functions finish an earlier suspend request. They 321532ed618SSoby Mathew * are called by the common finisher routine in psci_common.c. The `state_info` 322532ed618SSoby Mathew * is the psci_power_state from which this CPU has woken up from. 323532ed618SSoby Mathew ******************************************************************************/ 3245b33ad17SDeepika Bhavnani void psci_cpu_suspend_finish(unsigned int cpu_idx, const psci_power_state_t *state_info) 325532ed618SSoby Mathew { 326532ed618SSoby Mathew unsigned int counter_freq; 327532ed618SSoby Mathew unsigned int max_off_lvl; 328532ed618SSoby Mathew 329532ed618SSoby Mathew /* Ensure we have been woken up from a suspended state */ 330621d64f8SAntonio Nino Diaz assert((psci_get_aff_info_state() == AFF_STATE_ON) && 331621d64f8SAntonio Nino Diaz (is_local_state_off( 332621d64f8SAntonio Nino Diaz state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0)); 333532ed618SSoby Mathew 334532ed618SSoby Mathew /* 335532ed618SSoby Mathew * Plat. management: Perform the platform specific actions 336532ed618SSoby Mathew * before we change the state of the cpu e.g. enabling the 337532ed618SSoby Mathew * gic or zeroing the mailbox register. If anything goes 338532ed618SSoby Mathew * wrong then assert as there is no way to recover from this 339532ed618SSoby Mathew * situation. 340532ed618SSoby Mathew */ 341532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 342532ed618SSoby Mathew 343bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 344b0408e87SJeenu Viswambharan /* Arch. management: Enable the data cache, stack memory maintenance. */ 345532ed618SSoby Mathew psci_do_pwrup_cache_maintenance(); 346b0408e87SJeenu Viswambharan #endif 347532ed618SSoby Mathew 348532ed618SSoby Mathew /* Re-init the cntfrq_el0 register */ 349532ed618SSoby Mathew counter_freq = plat_get_syscnt_freq2(); 350532ed618SSoby Mathew write_cntfrq_el0(counter_freq); 351532ed618SSoby Mathew 352ed108b56SAlexei Fedorov #if ENABLE_PAUTH 353ed108b56SAlexei Fedorov /* Store APIAKey_EL1 key */ 354ed108b56SAlexei Fedorov set_cpu_data(apiakey[0], read_apiakeylo_el1()); 355ed108b56SAlexei Fedorov set_cpu_data(apiakey[1], read_apiakeyhi_el1()); 356ed108b56SAlexei Fedorov #endif /* ENABLE_PAUTH */ 357ed108b56SAlexei Fedorov 358532ed618SSoby Mathew /* 359532ed618SSoby Mathew * Call the cpu suspend finish handler registered by the Secure Payload 360532ed618SSoby Mathew * Dispatcher to let it do any bookeeping. If the handler encounters an 361532ed618SSoby Mathew * error, it's expected to assert within 362532ed618SSoby Mathew */ 363621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) { 364532ed618SSoby Mathew max_off_lvl = psci_find_max_off_lvl(state_info); 365532ed618SSoby Mathew assert(max_off_lvl != PSCI_INVALID_PWR_LVL); 366532ed618SSoby Mathew psci_spd_pm->svc_suspend_finish(max_off_lvl); 367532ed618SSoby Mathew } 368532ed618SSoby Mathew 369*0c836554SBoyan Karatotev /* This loses its meaning when not suspending, reset so it's correct for OFF */ 370*0c836554SBoyan Karatotev psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL); 371532ed618SSoby Mathew 3727593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_finish); 373532ed618SSoby Mathew } 374