1532ed618SSoby Mathew /* 2*44ee7714SBoyan Karatotev * Copyright (c) 2013-2025, 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 ******************************************************************************/ 28*44ee7714SBoyan Karatotev static void psci_cpu_suspend_to_standby_finish(unsigned int cpu_idx, 29*44ee7714SBoyan Karatotev unsigned int end_pwrlvl, 30*44ee7714SBoyan Karatotev psci_power_state_t *state_info) 31532ed618SSoby Mathew { 3261eae524SAchin Gupta /* 33532ed618SSoby Mathew * Plat. management: Allow the platform to do operations 34532ed618SSoby Mathew * on waking up from retention. 35532ed618SSoby Mathew */ 36*44ee7714SBoyan Karatotev psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 37532ed618SSoby Mathew 380c836554SBoyan Karatotev /* This loses its meaning when not suspending, reset so it's correct for OFF */ 390c836554SBoyan Karatotev psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL); 40532ed618SSoby Mathew } 41532ed618SSoby Mathew 42532ed618SSoby Mathew /******************************************************************************* 43532ed618SSoby Mathew * This function does generic and platform specific suspend to power down 44532ed618SSoby Mathew * operations. 45532ed618SSoby Mathew ******************************************************************************/ 46532ed618SSoby Mathew static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl, 47621d64f8SAntonio Nino Diaz const entry_point_info_t *ep, 48621d64f8SAntonio Nino Diaz const psci_power_state_t *state_info) 49532ed618SSoby Mathew { 50532ed618SSoby Mathew unsigned int max_off_lvl = psci_find_max_off_lvl(state_info); 51532ed618SSoby Mathew 527593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_start); 537593252cSDimitris Papastamos 54606b7430SWing Li #if PSCI_OS_INIT_MODE 55606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL 56606b7430SWing Li end_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL; 57606b7430SWing Li #else 58606b7430SWing Li end_pwrlvl = PLAT_MAX_PWR_LVL; 59606b7430SWing Li #endif 60606b7430SWing Li #endif 61606b7430SWing Li 62532ed618SSoby Mathew /* Save PSCI target power level for the suspend finisher handler */ 63532ed618SSoby Mathew psci_set_suspend_pwrlvl(end_pwrlvl); 64532ed618SSoby Mathew 65532ed618SSoby Mathew /* 66a10d3632SJeenu Viswambharan * Flush the target power level as it might be accessed on power up with 67532ed618SSoby Mathew * Data cache disabled. 68532ed618SSoby Mathew */ 69a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl); 70532ed618SSoby Mathew 71532ed618SSoby Mathew /* 72532ed618SSoby Mathew * Call the cpu suspend handler registered by the Secure Payload 73532ed618SSoby Mathew * Dispatcher to let it do any book-keeping. If the handler encounters an 74532ed618SSoby Mathew * error, it's expected to assert within 75532ed618SSoby Mathew */ 76621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL)) 77532ed618SSoby Mathew psci_spd_pm->svc_suspend(max_off_lvl); 78532ed618SSoby Mathew 791862d620SVarun Wadekar #if !HW_ASSISTED_COHERENCY 801862d620SVarun Wadekar /* 811862d620SVarun Wadekar * Plat. management: Allow the platform to perform any early 821862d620SVarun Wadekar * actions required to power down the CPU. This might be useful for 831862d620SVarun Wadekar * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these 841862d620SVarun Wadekar * actions with data caches enabled. 851862d620SVarun Wadekar */ 86621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL) 871862d620SVarun Wadekar psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info); 881862d620SVarun Wadekar #endif 891862d620SVarun Wadekar 90532ed618SSoby Mathew /* 91532ed618SSoby Mathew * Store the re-entry information for the non-secure world. 92532ed618SSoby Mathew */ 93532ed618SSoby Mathew cm_init_my_context(ep); 94532ed618SSoby Mathew 957941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 967941816aSdp-arm 977941816aSdp-arm /* 987941816aSdp-arm * Flush cache line so that even if CPU power down happens 997941816aSdp-arm * the timestamp update is reflected in memory. 1007941816aSdp-arm */ 1017941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1027941816aSdp-arm RT_INSTR_ENTER_CFLUSH, 1037941816aSdp-arm PMF_CACHE_MAINT); 1047941816aSdp-arm #endif 1057941816aSdp-arm 106532ed618SSoby Mathew /* 107b0408e87SJeenu Viswambharan * Arch. management. Initiate power down sequence. 108532ed618SSoby Mathew * TODO : Introduce a mechanism to query the cache level to flush 109532ed618SSoby Mathew * and the cpu-ops power down to perform from the platform. 110532ed618SSoby Mathew */ 11165bbb935SPranav Madhu psci_pwrdown_cpu(max_off_lvl); 1127941816aSdp-arm 1137941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1147941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1157941816aSdp-arm RT_INSTR_EXIT_CFLUSH, 1167941816aSdp-arm PMF_NO_CACHE_MAINT); 1177941816aSdp-arm #endif 118532ed618SSoby Mathew } 119532ed618SSoby Mathew 120532ed618SSoby Mathew /******************************************************************************* 121532ed618SSoby Mathew * Top level handler which is called when a cpu wants to suspend its execution. 122532ed618SSoby Mathew * It is assumed that along with suspending the cpu power domain, power domains 123532ed618SSoby Mathew * at higher levels until the target power level will be suspended as well. It 124532ed618SSoby Mathew * coordinates with the platform to negotiate the target state for each of 125532ed618SSoby Mathew * the power domain level till the target power domain level. It then performs 126532ed618SSoby Mathew * generic, architectural, platform setup and state management required to 127532ed618SSoby Mathew * suspend that power domain level and power domain levels below it. 128532ed618SSoby Mathew * e.g. For a cpu that's to be suspended, it could mean programming the 129532ed618SSoby Mathew * power controller whereas for a cluster that's to be suspended, it will call 130532ed618SSoby Mathew * the platform specific code which will disable coherency at the interconnect 131532ed618SSoby Mathew * level if the cpu is the last in the cluster and also the program the power 132532ed618SSoby Mathew * controller. 133532ed618SSoby Mathew * 134532ed618SSoby Mathew * All the required parameter checks are performed at the beginning and after 135532ed618SSoby Mathew * the state transition has been done, no further error is expected and it is 136532ed618SSoby Mathew * not possible to undo any of the actions taken beyond that point. 137532ed618SSoby Mathew ******************************************************************************/ 138606b7430SWing Li int psci_cpu_suspend_start(const entry_point_info_t *ep, 139532ed618SSoby Mathew unsigned int end_pwrlvl, 140532ed618SSoby Mathew psci_power_state_t *state_info, 141532ed618SSoby Mathew unsigned int is_power_down_state) 142532ed618SSoby Mathew { 143606b7430SWing Li int rc = PSCI_E_SUCCESS; 144606b7430SWing Li bool skip_wfi = false; 1455b33ad17SDeepika Bhavnani unsigned int idx = plat_my_core_pos(); 14674d27d00SAndrew F. Davis unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 147532ed618SSoby Mathew 148532ed618SSoby Mathew /* 149532ed618SSoby Mathew * This function must only be called on platforms where the 150532ed618SSoby Mathew * CPU_SUSPEND platform hooks have been implemented. 151532ed618SSoby Mathew */ 152621d64f8SAntonio Nino Diaz assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) && 153621d64f8SAntonio Nino Diaz (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)); 154532ed618SSoby Mathew 15574d27d00SAndrew F. Davis /* Get the parent nodes */ 15674d27d00SAndrew F. Davis psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes); 15774d27d00SAndrew F. Davis 158532ed618SSoby Mathew /* 159532ed618SSoby Mathew * This function acquires the lock corresponding to each power 160532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 161532ed618SSoby Mathew * is snapshot and state management can be done safely. 162532ed618SSoby Mathew */ 16374d27d00SAndrew F. Davis psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 164532ed618SSoby Mathew 165532ed618SSoby Mathew /* 166532ed618SSoby Mathew * We check if there are any pending interrupts after the delay 167532ed618SSoby Mathew * introduced by lock contention to increase the chances of early 168532ed618SSoby Mathew * detection that a wake-up interrupt has fired. 169532ed618SSoby Mathew */ 170621d64f8SAntonio Nino Diaz if (read_isr_el1() != 0U) { 171606b7430SWing Li skip_wfi = true; 172532ed618SSoby Mathew goto exit; 173532ed618SSoby Mathew } 174532ed618SSoby Mathew 175606b7430SWing Li #if PSCI_OS_INIT_MODE 176606b7430SWing Li if (psci_suspend_mode == OS_INIT) { 177606b7430SWing Li /* 178606b7430SWing Li * This function validates the requested state info for 179606b7430SWing Li * OS-initiated mode. 180606b7430SWing Li */ 181606b7430SWing Li rc = psci_validate_state_coordination(end_pwrlvl, state_info); 182606b7430SWing Li if (rc != PSCI_E_SUCCESS) { 183606b7430SWing Li skip_wfi = true; 184606b7430SWing Li goto exit; 185606b7430SWing Li } 186606b7430SWing Li } else { 187606b7430SWing Li #endif 188532ed618SSoby Mathew /* 189532ed618SSoby Mathew * This function is passed the requested state info and 190532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 191532ed618SSoby Mathew * the end level specified. 192532ed618SSoby Mathew */ 193532ed618SSoby Mathew psci_do_state_coordination(end_pwrlvl, state_info); 194606b7430SWing Li #if PSCI_OS_INIT_MODE 195606b7430SWing Li } 196606b7430SWing Li #endif 197532ed618SSoby Mathew 198d3488614SWing Li #if PSCI_OS_INIT_MODE 199d3488614SWing Li if (psci_plat_pm_ops->pwr_domain_validate_suspend != NULL) { 200d3488614SWing Li rc = psci_plat_pm_ops->pwr_domain_validate_suspend(state_info); 201d3488614SWing Li if (rc != PSCI_E_SUCCESS) { 202d3488614SWing Li skip_wfi = true; 203d3488614SWing Li goto exit; 204d3488614SWing Li } 205d3488614SWing Li } 206d3488614SWing Li #endif 207d3488614SWing Li 208d3488614SWing Li /* Update the target state in the power domain nodes */ 209d3488614SWing Li psci_set_target_local_pwr_states(end_pwrlvl, state_info); 210d3488614SWing Li 211532ed618SSoby Mathew #if ENABLE_PSCI_STAT 212532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 213532ed618SSoby Mathew psci_stats_update_pwr_down(end_pwrlvl, state_info); 214532ed618SSoby Mathew #endif 215532ed618SSoby Mathew 216621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) 217532ed618SSoby Mathew psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info); 218532ed618SSoby Mathew 219532ed618SSoby Mathew /* 220532ed618SSoby Mathew * Plat. management: Allow the platform to perform the 221532ed618SSoby Mathew * necessary actions to turn off this cpu e.g. set the 222532ed618SSoby Mathew * platform defined mailbox with the psci entrypoint, 223532ed618SSoby Mathew * program the power controller etc. 224532ed618SSoby Mathew */ 225606b7430SWing Li 226532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend(state_info); 227532ed618SSoby Mathew 228532ed618SSoby Mathew #if ENABLE_PSCI_STAT 22904c1db1eSdp-arm plat_psci_stat_accounting_start(state_info); 230532ed618SSoby Mathew #endif 231532ed618SSoby Mathew 232532ed618SSoby Mathew exit: 233532ed618SSoby Mathew /* 234532ed618SSoby Mathew * Release the locks corresponding to each power level in the 235532ed618SSoby Mathew * reverse order to which they were acquired. 236532ed618SSoby Mathew */ 23774d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 23874d27d00SAndrew F. Davis 239606b7430SWing Li if (skip_wfi) { 240606b7430SWing Li return rc; 241606b7430SWing Li } 242532ed618SSoby Mathew 243621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) { 244872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 245872be88aSdp-arm 246872be88aSdp-arm /* 247872be88aSdp-arm * Update the timestamp with cache off. We assume this 248872be88aSdp-arm * timestamp can only be read from the current CPU and the 249872be88aSdp-arm * timestamp cache line will be flushed before return to 250872be88aSdp-arm * normal world on wakeup. 251872be88aSdp-arm */ 252872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 253872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 254872be88aSdp-arm PMF_NO_CACHE_MAINT); 255872be88aSdp-arm #endif 256872be88aSdp-arm 257532ed618SSoby Mathew /* The function calls below must not return */ 258621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) 259532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info); 260532ed618SSoby Mathew else 261532ed618SSoby Mathew psci_power_down_wfi(); 262532ed618SSoby Mathew } 263532ed618SSoby Mathew 264872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 265872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 266872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 267872be88aSdp-arm PMF_NO_CACHE_MAINT); 268872be88aSdp-arm #endif 269872be88aSdp-arm 270532ed618SSoby Mathew /* 271532ed618SSoby Mathew * We will reach here if only retention/standby states have been 272532ed618SSoby Mathew * requested at multiple power levels. This means that the cpu 273532ed618SSoby Mathew * context will be preserved. 274532ed618SSoby Mathew */ 275532ed618SSoby Mathew wfi(); 276532ed618SSoby Mathew 277872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 278872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 279872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 280872be88aSdp-arm PMF_NO_CACHE_MAINT); 281872be88aSdp-arm #endif 282872be88aSdp-arm 283*44ee7714SBoyan Karatotev psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 284*44ee7714SBoyan Karatotev /* 285*44ee7714SBoyan Karatotev * Find out which retention states this CPU has exited from until the 286*44ee7714SBoyan Karatotev * 'end_pwrlvl'. The exit retention state could be deeper than the entry 287*44ee7714SBoyan Karatotev * state as a result of state coordination amongst other CPUs post wfi. 288*44ee7714SBoyan Karatotev */ 289*44ee7714SBoyan Karatotev psci_get_target_local_pwr_states(end_pwrlvl, state_info); 290*44ee7714SBoyan Karatotev 291*44ee7714SBoyan Karatotev #if ENABLE_PSCI_STAT 292*44ee7714SBoyan Karatotev plat_psci_stat_accounting_stop(state_info); 293*44ee7714SBoyan Karatotev psci_stats_update_pwr_up(end_pwrlvl, state_info); 294*44ee7714SBoyan Karatotev #endif 295*44ee7714SBoyan Karatotev 296532ed618SSoby Mathew /* 297532ed618SSoby Mathew * After we wake up from context retaining suspend, call the 298532ed618SSoby Mathew * context retaining suspend finisher. 299532ed618SSoby Mathew */ 300*44ee7714SBoyan Karatotev psci_cpu_suspend_to_standby_finish(idx, end_pwrlvl, state_info); 301*44ee7714SBoyan Karatotev 302*44ee7714SBoyan Karatotev /* 303*44ee7714SBoyan Karatotev * Set the requested and target state of this CPU and all the higher 304*44ee7714SBoyan Karatotev * power domain levels for this CPU to run. 305*44ee7714SBoyan Karatotev */ 306*44ee7714SBoyan Karatotev psci_set_pwr_domains_to_run(end_pwrlvl); 307*44ee7714SBoyan Karatotev 308*44ee7714SBoyan Karatotev psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 309606b7430SWing Li 310606b7430SWing Li return rc; 311532ed618SSoby Mathew } 312532ed618SSoby Mathew 313532ed618SSoby Mathew /******************************************************************************* 314532ed618SSoby Mathew * The following functions finish an earlier suspend request. They 315532ed618SSoby Mathew * are called by the common finisher routine in psci_common.c. The `state_info` 316532ed618SSoby Mathew * is the psci_power_state from which this CPU has woken up from. 317532ed618SSoby Mathew ******************************************************************************/ 318*44ee7714SBoyan Karatotev void psci_cpu_suspend_to_powerdown_finish(unsigned int cpu_idx, const psci_power_state_t *state_info) 319532ed618SSoby Mathew { 320532ed618SSoby Mathew unsigned int counter_freq; 321532ed618SSoby Mathew unsigned int max_off_lvl; 322532ed618SSoby Mathew 323532ed618SSoby Mathew /* Ensure we have been woken up from a suspended state */ 324621d64f8SAntonio Nino Diaz assert((psci_get_aff_info_state() == AFF_STATE_ON) && 325621d64f8SAntonio Nino Diaz (is_local_state_off( 326621d64f8SAntonio Nino Diaz state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0)); 327532ed618SSoby Mathew 328532ed618SSoby Mathew /* 329532ed618SSoby Mathew * Plat. management: Perform the platform specific actions 330532ed618SSoby Mathew * before we change the state of the cpu e.g. enabling the 331532ed618SSoby Mathew * gic or zeroing the mailbox register. If anything goes 332532ed618SSoby Mathew * wrong then assert as there is no way to recover from this 333532ed618SSoby Mathew * situation. 334532ed618SSoby Mathew */ 335532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 336532ed618SSoby Mathew 337bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 338b0408e87SJeenu Viswambharan /* Arch. management: Enable the data cache, stack memory maintenance. */ 339532ed618SSoby Mathew psci_do_pwrup_cache_maintenance(); 340b0408e87SJeenu Viswambharan #endif 341532ed618SSoby Mathew 342532ed618SSoby Mathew /* Re-init the cntfrq_el0 register */ 343532ed618SSoby Mathew counter_freq = plat_get_syscnt_freq2(); 344532ed618SSoby Mathew write_cntfrq_el0(counter_freq); 345532ed618SSoby Mathew 346ed108b56SAlexei Fedorov #if ENABLE_PAUTH 347ed108b56SAlexei Fedorov /* Store APIAKey_EL1 key */ 348ed108b56SAlexei Fedorov set_cpu_data(apiakey[0], read_apiakeylo_el1()); 349ed108b56SAlexei Fedorov set_cpu_data(apiakey[1], read_apiakeyhi_el1()); 350ed108b56SAlexei Fedorov #endif /* ENABLE_PAUTH */ 351ed108b56SAlexei Fedorov 352532ed618SSoby Mathew /* 353532ed618SSoby Mathew * Call the cpu suspend finish handler registered by the Secure Payload 354532ed618SSoby Mathew * Dispatcher to let it do any bookeeping. If the handler encounters an 355532ed618SSoby Mathew * error, it's expected to assert within 356532ed618SSoby Mathew */ 357621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) { 358532ed618SSoby Mathew max_off_lvl = psci_find_max_off_lvl(state_info); 359532ed618SSoby Mathew assert(max_off_lvl != PSCI_INVALID_PWR_LVL); 360532ed618SSoby Mathew psci_spd_pm->svc_suspend_finish(max_off_lvl); 361532ed618SSoby Mathew } 362532ed618SSoby Mathew 3630c836554SBoyan Karatotev /* This loses its meaning when not suspending, reset so it's correct for OFF */ 3640c836554SBoyan Karatotev psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL); 365532ed618SSoby Mathew 3667593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_finish); 367532ed618SSoby Mathew } 368