1532ed618SSoby Mathew /* 244ee7714SBoyan 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> 155d893410SBoyan Karatotev #include <drivers/arm/gic.h> 1609d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 1709d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 1809d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub_events.h> 1909d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 2009d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2209d40e0eSAntonio Nino Diaz 23532ed618SSoby Mathew #include "psci_private.h" 24532ed618SSoby Mathew 25532ed618SSoby Mathew /******************************************************************************* 26532ed618SSoby Mathew * This function does generic and platform specific operations after a wake-up 27532ed618SSoby Mathew * from standby/retention states at multiple power levels. 28532ed618SSoby Mathew ******************************************************************************/ 292b5e00d4SBoyan Karatotev static void psci_cpu_suspend_to_standby_finish(unsigned int end_pwrlvl, 3044ee7714SBoyan 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 */ 3644ee7714SBoyan 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 ******************************************************************************/ 4683ec7e45SBoyan Karatotev static void psci_suspend_to_pwrdown_start(unsigned int idx, 4783ec7e45SBoyan Karatotev unsigned int end_pwrlvl, 482b5e00d4SBoyan Karatotev unsigned int max_off_lvl, 49621d64f8SAntonio Nino Diaz const psci_power_state_t *state_info) 50532ed618SSoby Mathew { 5183ec7e45SBoyan Karatotev PUBLISH_EVENT_ARG(psci_suspend_pwrdown_start, &idx); 527593252cSDimitris Papastamos 53606b7430SWing Li #if PSCI_OS_INIT_MODE 54606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL 55606b7430SWing Li end_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL; 56606b7430SWing Li #else 57606b7430SWing Li end_pwrlvl = PLAT_MAX_PWR_LVL; 58606b7430SWing Li #endif 59606b7430SWing Li #endif 60606b7430SWing Li 61532ed618SSoby Mathew /* Save PSCI target power level for the suspend finisher handler */ 62532ed618SSoby Mathew psci_set_suspend_pwrlvl(end_pwrlvl); 63532ed618SSoby Mathew 64532ed618SSoby Mathew /* 65a10d3632SJeenu Viswambharan * Flush the target power level as it might be accessed on power up with 66532ed618SSoby Mathew * Data cache disabled. 67532ed618SSoby Mathew */ 68a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl); 69532ed618SSoby Mathew 70532ed618SSoby Mathew /* 71532ed618SSoby Mathew * Call the cpu suspend handler registered by the Secure Payload 72532ed618SSoby Mathew * Dispatcher to let it do any book-keeping. If the handler encounters an 73532ed618SSoby Mathew * error, it's expected to assert within 74532ed618SSoby Mathew */ 75*bac32cc4SSaivardhan Thatikonda if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL)) { 76532ed618SSoby Mathew psci_spd_pm->svc_suspend(max_off_lvl); 77*bac32cc4SSaivardhan Thatikonda } 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 */ 86*bac32cc4SSaivardhan Thatikonda if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL) { 871862d620SVarun Wadekar psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info); 88*bac32cc4SSaivardhan Thatikonda } 891862d620SVarun Wadekar #endif 90532ed618SSoby Mathew /* 91b0408e87SJeenu Viswambharan * Arch. management. Initiate power down sequence. 92532ed618SSoby Mathew */ 932b5e00d4SBoyan Karatotev psci_pwrdown_cpu_start(max_off_lvl); 94532ed618SSoby Mathew } 95532ed618SSoby Mathew 96532ed618SSoby Mathew /******************************************************************************* 97532ed618SSoby Mathew * Top level handler which is called when a cpu wants to suspend its execution. 98532ed618SSoby Mathew * It is assumed that along with suspending the cpu power domain, power domains 99532ed618SSoby Mathew * at higher levels until the target power level will be suspended as well. It 100532ed618SSoby Mathew * coordinates with the platform to negotiate the target state for each of 101532ed618SSoby Mathew * the power domain level till the target power domain level. It then performs 102532ed618SSoby Mathew * generic, architectural, platform setup and state management required to 103532ed618SSoby Mathew * suspend that power domain level and power domain levels below it. 104532ed618SSoby Mathew * e.g. For a cpu that's to be suspended, it could mean programming the 105532ed618SSoby Mathew * power controller whereas for a cluster that's to be suspended, it will call 106532ed618SSoby Mathew * the platform specific code which will disable coherency at the interconnect 107532ed618SSoby Mathew * level if the cpu is the last in the cluster and also the program the power 108532ed618SSoby Mathew * controller. 109532ed618SSoby Mathew * 110532ed618SSoby Mathew * All the required parameter checks are performed at the beginning and after 111532ed618SSoby Mathew * the state transition has been done, no further error is expected and it is 112532ed618SSoby Mathew * not possible to undo any of the actions taken beyond that point. 113532ed618SSoby Mathew ******************************************************************************/ 1143b802105SBoyan Karatotev int psci_cpu_suspend_start(unsigned int idx, 115532ed618SSoby Mathew unsigned int end_pwrlvl, 116532ed618SSoby Mathew psci_power_state_t *state_info, 117532ed618SSoby Mathew unsigned int is_power_down_state) 118532ed618SSoby Mathew { 119606b7430SWing Li int rc = PSCI_E_SUCCESS; 12074d27d00SAndrew F. Davis unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 1212b5e00d4SBoyan Karatotev unsigned int max_off_lvl = 0; 122532ed618SSoby Mathew 123532ed618SSoby Mathew /* 124532ed618SSoby Mathew * This function must only be called on platforms where the 125532ed618SSoby Mathew * CPU_SUSPEND platform hooks have been implemented. 126532ed618SSoby Mathew */ 127621d64f8SAntonio Nino Diaz assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) && 128621d64f8SAntonio Nino Diaz (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)); 129532ed618SSoby Mathew 13074d27d00SAndrew F. Davis /* Get the parent nodes */ 13174d27d00SAndrew F. Davis psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes); 13274d27d00SAndrew F. Davis 133532ed618SSoby Mathew /* 134532ed618SSoby Mathew * This function acquires the lock corresponding to each power 135532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 136532ed618SSoby Mathew * is snapshot and state management can be done safely. 137532ed618SSoby Mathew */ 13874d27d00SAndrew F. Davis psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 139532ed618SSoby Mathew 140532ed618SSoby Mathew /* 141532ed618SSoby Mathew * We check if there are any pending interrupts after the delay 142532ed618SSoby Mathew * introduced by lock contention to increase the chances of early 143532ed618SSoby Mathew * detection that a wake-up interrupt has fired. 144532ed618SSoby Mathew */ 145621d64f8SAntonio Nino Diaz if (read_isr_el1() != 0U) { 1460839cfc9SMaheedhar Bollapalli goto suspend_exit; 147532ed618SSoby Mathew } 148532ed618SSoby Mathew 149606b7430SWing Li #if PSCI_OS_INIT_MODE 150606b7430SWing Li if (psci_suspend_mode == OS_INIT) { 151606b7430SWing Li /* 152606b7430SWing Li * This function validates the requested state info for 153606b7430SWing Li * OS-initiated mode. 154606b7430SWing Li */ 1553b802105SBoyan Karatotev rc = psci_validate_state_coordination(idx, end_pwrlvl, state_info); 156606b7430SWing Li if (rc != PSCI_E_SUCCESS) { 1570839cfc9SMaheedhar Bollapalli goto suspend_exit; 158606b7430SWing Li } 159606b7430SWing Li } else { 160606b7430SWing Li #endif 161532ed618SSoby Mathew /* 162532ed618SSoby Mathew * This function is passed the requested state info and 163532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 164532ed618SSoby Mathew * the end level specified. 165532ed618SSoby Mathew */ 1663b802105SBoyan Karatotev psci_do_state_coordination(idx, end_pwrlvl, state_info); 167606b7430SWing Li #if PSCI_OS_INIT_MODE 168606b7430SWing Li } 169606b7430SWing Li #endif 170532ed618SSoby Mathew 171d3488614SWing Li #if PSCI_OS_INIT_MODE 172d3488614SWing Li if (psci_plat_pm_ops->pwr_domain_validate_suspend != NULL) { 173d3488614SWing Li rc = psci_plat_pm_ops->pwr_domain_validate_suspend(state_info); 174d3488614SWing Li if (rc != PSCI_E_SUCCESS) { 1750839cfc9SMaheedhar Bollapalli goto suspend_exit; 176d3488614SWing Li } 177d3488614SWing Li } 178d3488614SWing Li #endif 179d3488614SWing Li 180d3488614SWing Li /* Update the target state in the power domain nodes */ 1813b802105SBoyan Karatotev psci_set_target_local_pwr_states(idx, end_pwrlvl, state_info); 182d3488614SWing Li 183532ed618SSoby Mathew #if ENABLE_PSCI_STAT 184532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 1853b802105SBoyan Karatotev psci_stats_update_pwr_down(idx, end_pwrlvl, state_info); 186532ed618SSoby Mathew #endif 187532ed618SSoby Mathew 1882b5e00d4SBoyan Karatotev if (is_power_down_state != 0U) { 1892b5e00d4SBoyan Karatotev max_off_lvl = psci_find_max_off_lvl(state_info); 190ef738d19SManish Pandey psci_suspend_to_pwrdown_start(idx, end_pwrlvl, end_pwrlvl, state_info); 1912b5e00d4SBoyan Karatotev } 192532ed618SSoby Mathew 1935d893410SBoyan Karatotev #if USE_GIC_DRIVER 1945d893410SBoyan Karatotev /* turn the GIC off before we hand off to the platform */ 1955d893410SBoyan Karatotev gic_cpuif_disable(idx); 1965d893410SBoyan Karatotev #endif /* USE_GIC_DRIVER */ 1975d893410SBoyan Karatotev 198532ed618SSoby Mathew /* 199532ed618SSoby Mathew * Plat. management: Allow the platform to perform the 200532ed618SSoby Mathew * necessary actions to turn off this cpu e.g. set the 201532ed618SSoby Mathew * platform defined mailbox with the psci entrypoint, 202532ed618SSoby Mathew * program the power controller etc. 203532ed618SSoby Mathew */ 204532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend(state_info); 205532ed618SSoby Mathew 206532ed618SSoby Mathew #if ENABLE_PSCI_STAT 20704c1db1eSdp-arm plat_psci_stat_accounting_start(state_info); 208532ed618SSoby Mathew #endif 209532ed618SSoby Mathew 210532ed618SSoby Mathew /* 211532ed618SSoby Mathew * Release the locks corresponding to each power level in the 212532ed618SSoby Mathew * reverse order to which they were acquired. 213532ed618SSoby Mathew */ 21474d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 21574d27d00SAndrew F. Davis 216872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 217872be88aSdp-arm /* 218872be88aSdp-arm * Update the timestamp with cache off. We assume this 219872be88aSdp-arm * timestamp can only be read from the current CPU and the 220872be88aSdp-arm * timestamp cache line will be flushed before return to 221872be88aSdp-arm * normal world on wakeup. 222872be88aSdp-arm */ 223872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 224872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 225872be88aSdp-arm PMF_NO_CACHE_MAINT); 226872be88aSdp-arm #endif 227872be88aSdp-arm 2282b5e00d4SBoyan Karatotev if (is_power_down_state != 0U) { 229db5fe4f4SBoyan Karatotev if (psci_plat_pm_ops->pwr_domain_pwr_down != NULL) { 2302b5e00d4SBoyan Karatotev /* This function may not return */ 231db5fe4f4SBoyan Karatotev psci_plat_pm_ops->pwr_domain_pwr_down(state_info); 232532ed618SSoby Mathew } 233532ed618SSoby Mathew 2342b5e00d4SBoyan Karatotev psci_pwrdown_cpu_end_wakeup(max_off_lvl); 2352b5e00d4SBoyan Karatotev } else { 236532ed618SSoby Mathew /* 237532ed618SSoby Mathew * We will reach here if only retention/standby states have been 238532ed618SSoby Mathew * requested at multiple power levels. This means that the cpu 239532ed618SSoby Mathew * context will be preserved. 240532ed618SSoby Mathew */ 241532ed618SSoby Mathew wfi(); 2422b5e00d4SBoyan Karatotev } 243532ed618SSoby Mathew 244872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 245872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 246872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 247872be88aSdp-arm PMF_NO_CACHE_MAINT); 248872be88aSdp-arm #endif 249872be88aSdp-arm 25044ee7714SBoyan Karatotev psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 25144ee7714SBoyan Karatotev /* 25244ee7714SBoyan Karatotev * Find out which retention states this CPU has exited from until the 25344ee7714SBoyan Karatotev * 'end_pwrlvl'. The exit retention state could be deeper than the entry 25444ee7714SBoyan Karatotev * state as a result of state coordination amongst other CPUs post wfi. 25544ee7714SBoyan Karatotev */ 2563b802105SBoyan Karatotev psci_get_target_local_pwr_states(idx, end_pwrlvl, state_info); 25744ee7714SBoyan Karatotev 25844ee7714SBoyan Karatotev #if ENABLE_PSCI_STAT 25944ee7714SBoyan Karatotev plat_psci_stat_accounting_stop(state_info); 2603b802105SBoyan Karatotev psci_stats_update_pwr_up(idx, end_pwrlvl, state_info); 26144ee7714SBoyan Karatotev #endif 26244ee7714SBoyan Karatotev 263532ed618SSoby Mathew /* 2642b5e00d4SBoyan Karatotev * Waking up means we've retained all context. Call the finishers to put 2652b5e00d4SBoyan Karatotev * the system back to a usable state. 266532ed618SSoby Mathew */ 2672b5e00d4SBoyan Karatotev if (is_power_down_state != 0U) { 26804c39e46SBoyan Karatotev psci_cpu_suspend_to_powerdown_finish(idx, max_off_lvl, state_info, true); 2692b5e00d4SBoyan Karatotev } else { 2702b5e00d4SBoyan Karatotev psci_cpu_suspend_to_standby_finish(end_pwrlvl, state_info); 2712b5e00d4SBoyan Karatotev } 27244ee7714SBoyan Karatotev 2735d893410SBoyan Karatotev #if USE_GIC_DRIVER 2745d893410SBoyan Karatotev /* Turn GIC on after platform has had a chance to do state management */ 2755d893410SBoyan Karatotev gic_cpuif_enable(idx); 2765d893410SBoyan Karatotev #endif /* USE_GIC_DRIVER */ 2775d893410SBoyan Karatotev 27844ee7714SBoyan Karatotev /* 27944ee7714SBoyan Karatotev * Set the requested and target state of this CPU and all the higher 28044ee7714SBoyan Karatotev * power domain levels for this CPU to run. 28144ee7714SBoyan Karatotev */ 2823b802105SBoyan Karatotev psci_set_pwr_domains_to_run(idx, end_pwrlvl); 28344ee7714SBoyan Karatotev 2840839cfc9SMaheedhar Bollapalli suspend_exit: 28544ee7714SBoyan Karatotev psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 286606b7430SWing Li 287606b7430SWing Li return rc; 288532ed618SSoby Mathew } 289532ed618SSoby Mathew 290532ed618SSoby Mathew /******************************************************************************* 291532ed618SSoby Mathew * The following functions finish an earlier suspend request. They 292532ed618SSoby Mathew * are called by the common finisher routine in psci_common.c. The `state_info` 293532ed618SSoby Mathew * is the psci_power_state from which this CPU has woken up from. 294532ed618SSoby Mathew ******************************************************************************/ 29504c39e46SBoyan Karatotev void psci_cpu_suspend_to_powerdown_finish(unsigned int cpu_idx, unsigned int max_off_lvl, const psci_power_state_t *state_info, bool abandon) 296532ed618SSoby Mathew { 297532ed618SSoby Mathew unsigned int counter_freq; 298532ed618SSoby Mathew 299532ed618SSoby Mathew /* Ensure we have been woken up from a suspended state */ 300621d64f8SAntonio Nino Diaz assert((psci_get_aff_info_state() == AFF_STATE_ON) && 301621d64f8SAntonio Nino Diaz (is_local_state_off( 302621d64f8SAntonio Nino Diaz state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0)); 303532ed618SSoby Mathew 304532ed618SSoby Mathew /* 305532ed618SSoby Mathew * Plat. management: Perform the platform specific actions 306532ed618SSoby Mathew * before we change the state of the cpu e.g. enabling the 307532ed618SSoby Mathew * gic or zeroing the mailbox register. If anything goes 308532ed618SSoby Mathew * wrong then assert as there is no way to recover from this 309532ed618SSoby Mathew * situation. 310532ed618SSoby Mathew */ 311532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 312532ed618SSoby Mathew 313bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 314b0408e87SJeenu Viswambharan /* Arch. management: Enable the data cache, stack memory maintenance. */ 315532ed618SSoby Mathew psci_do_pwrup_cache_maintenance(); 316b0408e87SJeenu Viswambharan #endif 317532ed618SSoby Mathew 3185d893410SBoyan Karatotev #if USE_GIC_DRIVER 3195d893410SBoyan Karatotev /* GIC on after platform has had its say and MMU is on */ 3205d893410SBoyan Karatotev gic_cpuif_enable(cpu_idx); 3215d893410SBoyan Karatotev #endif /* USE_GIC_DRIVER */ 3225d893410SBoyan Karatotev 323fd914fc8SBoyan Karatotev if (!abandon) { 324532ed618SSoby Mathew /* Re-init the cntfrq_el0 register */ 325532ed618SSoby Mathew counter_freq = plat_get_syscnt_freq2(); 326532ed618SSoby Mathew write_cntfrq_el0(counter_freq); 327fd914fc8SBoyan Karatotev } 328532ed618SSoby Mathew 329532ed618SSoby Mathew /* 330532ed618SSoby Mathew * Call the cpu suspend finish handler registered by the Secure Payload 331532ed618SSoby Mathew * Dispatcher to let it do any bookeeping. If the handler encounters an 332532ed618SSoby Mathew * error, it's expected to assert within 333532ed618SSoby Mathew */ 334621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) { 33504c39e46SBoyan Karatotev psci_spd_pm->svc_suspend_finish(max_off_lvl, abandon); 336532ed618SSoby Mathew } 337532ed618SSoby Mathew 3380c836554SBoyan Karatotev /* This loses its meaning when not suspending, reset so it's correct for OFF */ 3390c836554SBoyan Karatotev psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL); 340532ed618SSoby Mathew 34183ec7e45SBoyan Karatotev PUBLISH_EVENT_ARG(psci_suspend_pwrdown_finish, &cpu_idx); 342532ed618SSoby Mathew } 343