1532ed618SSoby Mathew /* 2*ed108b56SAlexei Fedorov * Copyright (c) 2013-2019, 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 ******************************************************************************/ 28621d64f8SAntonio Nino Diaz static void psci_suspend_to_standby_finisher(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 57532ed618SSoby Mathew /* 58532ed618SSoby Mathew * Set the requested and target state of this CPU and all the higher 59532ed618SSoby Mathew * power domain levels for this CPU to run. 60532ed618SSoby Mathew */ 61532ed618SSoby Mathew psci_set_pwr_domains_to_run(end_pwrlvl); 62532ed618SSoby Mathew 6374d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 64532ed618SSoby Mathew } 65532ed618SSoby Mathew 66532ed618SSoby Mathew /******************************************************************************* 67532ed618SSoby Mathew * This function does generic and platform specific suspend to power down 68532ed618SSoby Mathew * operations. 69532ed618SSoby Mathew ******************************************************************************/ 70532ed618SSoby Mathew static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl, 71621d64f8SAntonio Nino Diaz const entry_point_info_t *ep, 72621d64f8SAntonio Nino Diaz const psci_power_state_t *state_info) 73532ed618SSoby Mathew { 74532ed618SSoby Mathew unsigned int max_off_lvl = psci_find_max_off_lvl(state_info); 75532ed618SSoby Mathew 767593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_start); 777593252cSDimitris Papastamos 78532ed618SSoby Mathew /* Save PSCI target power level for the suspend finisher handler */ 79532ed618SSoby Mathew psci_set_suspend_pwrlvl(end_pwrlvl); 80532ed618SSoby Mathew 81532ed618SSoby Mathew /* 82a10d3632SJeenu Viswambharan * Flush the target power level as it might be accessed on power up with 83532ed618SSoby Mathew * Data cache disabled. 84532ed618SSoby Mathew */ 85a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl); 86532ed618SSoby Mathew 87532ed618SSoby Mathew /* 88532ed618SSoby Mathew * Call the cpu suspend handler registered by the Secure Payload 89532ed618SSoby Mathew * Dispatcher to let it do any book-keeping. If the handler encounters an 90532ed618SSoby Mathew * error, it's expected to assert within 91532ed618SSoby Mathew */ 92621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL)) 93532ed618SSoby Mathew psci_spd_pm->svc_suspend(max_off_lvl); 94532ed618SSoby Mathew 951862d620SVarun Wadekar #if !HW_ASSISTED_COHERENCY 961862d620SVarun Wadekar /* 971862d620SVarun Wadekar * Plat. management: Allow the platform to perform any early 981862d620SVarun Wadekar * actions required to power down the CPU. This might be useful for 991862d620SVarun Wadekar * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these 1001862d620SVarun Wadekar * actions with data caches enabled. 1011862d620SVarun Wadekar */ 102621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL) 1031862d620SVarun Wadekar psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info); 1041862d620SVarun Wadekar #endif 1051862d620SVarun Wadekar 106532ed618SSoby Mathew /* 107532ed618SSoby Mathew * Store the re-entry information for the non-secure world. 108532ed618SSoby Mathew */ 109532ed618SSoby Mathew cm_init_my_context(ep); 110532ed618SSoby Mathew 1117941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1127941816aSdp-arm 1137941816aSdp-arm /* 1147941816aSdp-arm * Flush cache line so that even if CPU power down happens 1157941816aSdp-arm * the timestamp update is reflected in memory. 1167941816aSdp-arm */ 1177941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1187941816aSdp-arm RT_INSTR_ENTER_CFLUSH, 1197941816aSdp-arm PMF_CACHE_MAINT); 1207941816aSdp-arm #endif 1217941816aSdp-arm 122532ed618SSoby Mathew /* 123b0408e87SJeenu Viswambharan * Arch. management. Initiate power down sequence. 124532ed618SSoby Mathew * TODO : Introduce a mechanism to query the cache level to flush 125532ed618SSoby Mathew * and the cpu-ops power down to perform from the platform. 126532ed618SSoby Mathew */ 127b0408e87SJeenu Viswambharan psci_do_pwrdown_sequence(max_off_lvl); 1287941816aSdp-arm 1297941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1307941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1317941816aSdp-arm RT_INSTR_EXIT_CFLUSH, 1327941816aSdp-arm PMF_NO_CACHE_MAINT); 1337941816aSdp-arm #endif 134532ed618SSoby Mathew } 135532ed618SSoby Mathew 136532ed618SSoby Mathew /******************************************************************************* 137532ed618SSoby Mathew * Top level handler which is called when a cpu wants to suspend its execution. 138532ed618SSoby Mathew * It is assumed that along with suspending the cpu power domain, power domains 139532ed618SSoby Mathew * at higher levels until the target power level will be suspended as well. It 140532ed618SSoby Mathew * coordinates with the platform to negotiate the target state for each of 141532ed618SSoby Mathew * the power domain level till the target power domain level. It then performs 142532ed618SSoby Mathew * generic, architectural, platform setup and state management required to 143532ed618SSoby Mathew * suspend that power domain level and power domain levels below it. 144532ed618SSoby Mathew * e.g. For a cpu that's to be suspended, it could mean programming the 145532ed618SSoby Mathew * power controller whereas for a cluster that's to be suspended, it will call 146532ed618SSoby Mathew * the platform specific code which will disable coherency at the interconnect 147532ed618SSoby Mathew * level if the cpu is the last in the cluster and also the program the power 148532ed618SSoby Mathew * controller. 149532ed618SSoby Mathew * 150532ed618SSoby Mathew * All the required parameter checks are performed at the beginning and after 151532ed618SSoby Mathew * the state transition has been done, no further error is expected and it is 152532ed618SSoby Mathew * not possible to undo any of the actions taken beyond that point. 153532ed618SSoby Mathew ******************************************************************************/ 154621d64f8SAntonio Nino Diaz void psci_cpu_suspend_start(const entry_point_info_t *ep, 155532ed618SSoby Mathew unsigned int end_pwrlvl, 156532ed618SSoby Mathew psci_power_state_t *state_info, 157532ed618SSoby Mathew unsigned int is_power_down_state) 158532ed618SSoby Mathew { 159532ed618SSoby Mathew int skip_wfi = 0; 160621d64f8SAntonio Nino Diaz int idx = (int) plat_my_core_pos(); 16174d27d00SAndrew F. Davis unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; 162532ed618SSoby Mathew 163532ed618SSoby Mathew /* 164532ed618SSoby Mathew * This function must only be called on platforms where the 165532ed618SSoby Mathew * CPU_SUSPEND platform hooks have been implemented. 166532ed618SSoby Mathew */ 167621d64f8SAntonio Nino Diaz assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) && 168621d64f8SAntonio Nino Diaz (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)); 169532ed618SSoby Mathew 17074d27d00SAndrew F. Davis /* Get the parent nodes */ 17174d27d00SAndrew F. Davis psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes); 17274d27d00SAndrew F. Davis 173532ed618SSoby Mathew /* 174532ed618SSoby Mathew * This function acquires the lock corresponding to each power 175532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 176532ed618SSoby Mathew * is snapshot and state management can be done safely. 177532ed618SSoby Mathew */ 17874d27d00SAndrew F. Davis psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); 179532ed618SSoby Mathew 180532ed618SSoby Mathew /* 181532ed618SSoby Mathew * We check if there are any pending interrupts after the delay 182532ed618SSoby Mathew * introduced by lock contention to increase the chances of early 183532ed618SSoby Mathew * detection that a wake-up interrupt has fired. 184532ed618SSoby Mathew */ 185621d64f8SAntonio Nino Diaz if (read_isr_el1() != 0U) { 186532ed618SSoby Mathew skip_wfi = 1; 187532ed618SSoby Mathew goto exit; 188532ed618SSoby Mathew } 189532ed618SSoby Mathew 190532ed618SSoby Mathew /* 191532ed618SSoby Mathew * This function is passed the requested state info and 192532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 193532ed618SSoby Mathew * the end level specified. 194532ed618SSoby Mathew */ 195532ed618SSoby Mathew psci_do_state_coordination(end_pwrlvl, state_info); 196532ed618SSoby Mathew 197532ed618SSoby Mathew #if ENABLE_PSCI_STAT 198532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 199532ed618SSoby Mathew psci_stats_update_pwr_down(end_pwrlvl, state_info); 200532ed618SSoby Mathew #endif 201532ed618SSoby Mathew 202621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) 203532ed618SSoby Mathew psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info); 204532ed618SSoby Mathew 205532ed618SSoby Mathew /* 206532ed618SSoby Mathew * Plat. management: Allow the platform to perform the 207532ed618SSoby Mathew * necessary actions to turn off this cpu e.g. set the 208532ed618SSoby Mathew * platform defined mailbox with the psci entrypoint, 209532ed618SSoby Mathew * program the power controller etc. 210532ed618SSoby Mathew */ 211532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend(state_info); 212532ed618SSoby Mathew 213532ed618SSoby Mathew #if ENABLE_PSCI_STAT 21404c1db1eSdp-arm plat_psci_stat_accounting_start(state_info); 215532ed618SSoby Mathew #endif 216532ed618SSoby Mathew 217532ed618SSoby Mathew exit: 218532ed618SSoby Mathew /* 219532ed618SSoby Mathew * Release the locks corresponding to each power level in the 220532ed618SSoby Mathew * reverse order to which they were acquired. 221532ed618SSoby Mathew */ 22274d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); 22374d27d00SAndrew F. Davis 224621d64f8SAntonio Nino Diaz if (skip_wfi == 1) 225532ed618SSoby Mathew return; 226532ed618SSoby Mathew 227621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) { 228872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 229872be88aSdp-arm 230872be88aSdp-arm /* 231872be88aSdp-arm * Update the timestamp with cache off. We assume this 232872be88aSdp-arm * timestamp can only be read from the current CPU and the 233872be88aSdp-arm * timestamp cache line will be flushed before return to 234872be88aSdp-arm * normal world on wakeup. 235872be88aSdp-arm */ 236872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 237872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 238872be88aSdp-arm PMF_NO_CACHE_MAINT); 239872be88aSdp-arm #endif 240872be88aSdp-arm 241532ed618SSoby Mathew /* The function calls below must not return */ 242621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) 243532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info); 244532ed618SSoby Mathew else 245532ed618SSoby Mathew psci_power_down_wfi(); 246532ed618SSoby Mathew } 247532ed618SSoby Mathew 248872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 249872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 250872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 251872be88aSdp-arm PMF_NO_CACHE_MAINT); 252872be88aSdp-arm #endif 253872be88aSdp-arm 254532ed618SSoby Mathew /* 255532ed618SSoby Mathew * We will reach here if only retention/standby states have been 256532ed618SSoby Mathew * requested at multiple power levels. This means that the cpu 257532ed618SSoby Mathew * context will be preserved. 258532ed618SSoby Mathew */ 259532ed618SSoby Mathew wfi(); 260532ed618SSoby Mathew 261872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 262872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 263872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 264872be88aSdp-arm PMF_NO_CACHE_MAINT); 265872be88aSdp-arm #endif 266872be88aSdp-arm 267532ed618SSoby Mathew /* 268532ed618SSoby Mathew * After we wake up from context retaining suspend, call the 269532ed618SSoby Mathew * context retaining suspend finisher. 270532ed618SSoby Mathew */ 27161eae524SAchin Gupta psci_suspend_to_standby_finisher(idx, end_pwrlvl); 272532ed618SSoby Mathew } 273532ed618SSoby Mathew 274532ed618SSoby Mathew /******************************************************************************* 275532ed618SSoby Mathew * The following functions finish an earlier suspend request. They 276532ed618SSoby Mathew * are called by the common finisher routine in psci_common.c. The `state_info` 277532ed618SSoby Mathew * is the psci_power_state from which this CPU has woken up from. 278532ed618SSoby Mathew ******************************************************************************/ 279621d64f8SAntonio Nino Diaz void psci_cpu_suspend_finish(int cpu_idx, const psci_power_state_t *state_info) 280532ed618SSoby Mathew { 281532ed618SSoby Mathew unsigned int counter_freq; 282532ed618SSoby Mathew unsigned int max_off_lvl; 283532ed618SSoby Mathew 284532ed618SSoby Mathew /* Ensure we have been woken up from a suspended state */ 285621d64f8SAntonio Nino Diaz assert((psci_get_aff_info_state() == AFF_STATE_ON) && 286621d64f8SAntonio Nino Diaz (is_local_state_off( 287621d64f8SAntonio Nino Diaz state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0)); 288532ed618SSoby Mathew 289532ed618SSoby Mathew /* 290532ed618SSoby Mathew * Plat. management: Perform the platform specific actions 291532ed618SSoby Mathew * before we change the state of the cpu e.g. enabling the 292532ed618SSoby Mathew * gic or zeroing the mailbox register. If anything goes 293532ed618SSoby Mathew * wrong then assert as there is no way to recover from this 294532ed618SSoby Mathew * situation. 295532ed618SSoby Mathew */ 296532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 297532ed618SSoby Mathew 298bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 299b0408e87SJeenu Viswambharan /* Arch. management: Enable the data cache, stack memory maintenance. */ 300532ed618SSoby Mathew psci_do_pwrup_cache_maintenance(); 301b0408e87SJeenu Viswambharan #endif 302532ed618SSoby Mathew 303532ed618SSoby Mathew /* Re-init the cntfrq_el0 register */ 304532ed618SSoby Mathew counter_freq = plat_get_syscnt_freq2(); 305532ed618SSoby Mathew write_cntfrq_el0(counter_freq); 306532ed618SSoby Mathew 307*ed108b56SAlexei Fedorov #if ENABLE_PAUTH 308*ed108b56SAlexei Fedorov /* Store APIAKey_EL1 key */ 309*ed108b56SAlexei Fedorov set_cpu_data(apiakey[0], read_apiakeylo_el1()); 310*ed108b56SAlexei Fedorov set_cpu_data(apiakey[1], read_apiakeyhi_el1()); 311*ed108b56SAlexei Fedorov #endif /* ENABLE_PAUTH */ 312*ed108b56SAlexei Fedorov 313532ed618SSoby Mathew /* 314532ed618SSoby Mathew * Call the cpu suspend finish handler registered by the Secure Payload 315532ed618SSoby Mathew * Dispatcher to let it do any bookeeping. If the handler encounters an 316532ed618SSoby Mathew * error, it's expected to assert within 317532ed618SSoby Mathew */ 318621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) { 319532ed618SSoby Mathew max_off_lvl = psci_find_max_off_lvl(state_info); 320532ed618SSoby Mathew assert(max_off_lvl != PSCI_INVALID_PWR_LVL); 321532ed618SSoby Mathew psci_spd_pm->svc_suspend_finish(max_off_lvl); 322532ed618SSoby Mathew } 323532ed618SSoby Mathew 324532ed618SSoby Mathew /* Invalidate the suspend level for the cpu */ 325532ed618SSoby Mathew psci_set_suspend_pwrlvl(PSCI_INVALID_PWR_LVL); 326532ed618SSoby Mathew 3277593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_finish); 3287593252cSDimitris Papastamos 329532ed618SSoby Mathew /* 330532ed618SSoby Mathew * Generic management: Now we just need to retrieve the 331532ed618SSoby Mathew * information that we had stashed away during the suspend 332532ed618SSoby Mathew * call to set this cpu on its way. 333532ed618SSoby Mathew */ 334532ed618SSoby Mathew cm_prepare_el3_exit(NON_SECURE); 335532ed618SSoby Mathew } 336