1532ed618SSoby Mathew /* 2621d64f8SAntonio Nino Diaz * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 7*09d40e0eSAntonio Nino Diaz #include <assert.h> 8*09d40e0eSAntonio Nino Diaz #include <stddef.h> 9*09d40e0eSAntonio Nino Diaz 10532ed618SSoby Mathew #include <arch.h> 11532ed618SSoby Mathew #include <arch_helpers.h> 12*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 13*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 14532ed618SSoby Mathew #include <context.h> 15*09d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 16*09d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 17*09d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub_events.h> 18*09d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 19*09d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 20*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 21*09d40e0eSAntonio 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 { 3161eae524SAchin Gupta psci_power_state_t state_info; 3261eae524SAchin Gupta 33532ed618SSoby Mathew psci_acquire_pwr_domain_locks(end_pwrlvl, 34532ed618SSoby Mathew cpu_idx); 35532ed618SSoby Mathew 36532ed618SSoby Mathew /* 3761eae524SAchin Gupta * Find out which retention states this CPU has exited from until the 3861eae524SAchin Gupta * 'end_pwrlvl'. The exit retention state could be deeper than the entry 3961eae524SAchin Gupta * state as a result of state coordination amongst other CPUs post wfi. 4061eae524SAchin Gupta */ 4161eae524SAchin Gupta psci_get_target_local_pwr_states(end_pwrlvl, &state_info); 4261eae524SAchin Gupta 43bfc87a8dSSoby Mathew #if ENABLE_PSCI_STAT 44bfc87a8dSSoby Mathew plat_psci_stat_accounting_stop(&state_info); 45bfc87a8dSSoby Mathew psci_stats_update_pwr_up(end_pwrlvl, &state_info); 46bfc87a8dSSoby Mathew #endif 47bfc87a8dSSoby Mathew 4861eae524SAchin Gupta /* 49532ed618SSoby Mathew * Plat. management: Allow the platform to do operations 50532ed618SSoby Mathew * on waking up from retention. 51532ed618SSoby Mathew */ 5261eae524SAchin Gupta psci_plat_pm_ops->pwr_domain_suspend_finish(&state_info); 53532ed618SSoby Mathew 54532ed618SSoby Mathew /* 55532ed618SSoby Mathew * Set the requested and target state of this CPU and all the higher 56532ed618SSoby Mathew * power domain levels for this CPU to run. 57532ed618SSoby Mathew */ 58532ed618SSoby Mathew psci_set_pwr_domains_to_run(end_pwrlvl); 59532ed618SSoby Mathew 60532ed618SSoby Mathew psci_release_pwr_domain_locks(end_pwrlvl, 61532ed618SSoby Mathew cpu_idx); 62532ed618SSoby Mathew } 63532ed618SSoby Mathew 64532ed618SSoby Mathew /******************************************************************************* 65532ed618SSoby Mathew * This function does generic and platform specific suspend to power down 66532ed618SSoby Mathew * operations. 67532ed618SSoby Mathew ******************************************************************************/ 68532ed618SSoby Mathew static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl, 69621d64f8SAntonio Nino Diaz const entry_point_info_t *ep, 70621d64f8SAntonio Nino Diaz const psci_power_state_t *state_info) 71532ed618SSoby Mathew { 72532ed618SSoby Mathew unsigned int max_off_lvl = psci_find_max_off_lvl(state_info); 73532ed618SSoby Mathew 747593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_start); 757593252cSDimitris Papastamos 76532ed618SSoby Mathew /* Save PSCI target power level for the suspend finisher handler */ 77532ed618SSoby Mathew psci_set_suspend_pwrlvl(end_pwrlvl); 78532ed618SSoby Mathew 79532ed618SSoby Mathew /* 80a10d3632SJeenu Viswambharan * Flush the target power level as it might be accessed on power up with 81532ed618SSoby Mathew * Data cache disabled. 82532ed618SSoby Mathew */ 83a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl); 84532ed618SSoby Mathew 85532ed618SSoby Mathew /* 86532ed618SSoby Mathew * Call the cpu suspend handler registered by the Secure Payload 87532ed618SSoby Mathew * Dispatcher to let it do any book-keeping. If the handler encounters an 88532ed618SSoby Mathew * error, it's expected to assert within 89532ed618SSoby Mathew */ 90621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL)) 91532ed618SSoby Mathew psci_spd_pm->svc_suspend(max_off_lvl); 92532ed618SSoby Mathew 931862d620SVarun Wadekar #if !HW_ASSISTED_COHERENCY 941862d620SVarun Wadekar /* 951862d620SVarun Wadekar * Plat. management: Allow the platform to perform any early 961862d620SVarun Wadekar * actions required to power down the CPU. This might be useful for 971862d620SVarun Wadekar * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these 981862d620SVarun Wadekar * actions with data caches enabled. 991862d620SVarun Wadekar */ 100621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL) 1011862d620SVarun Wadekar psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info); 1021862d620SVarun Wadekar #endif 1031862d620SVarun Wadekar 104532ed618SSoby Mathew /* 105532ed618SSoby Mathew * Store the re-entry information for the non-secure world. 106532ed618SSoby Mathew */ 107532ed618SSoby Mathew cm_init_my_context(ep); 108532ed618SSoby Mathew 1097941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1107941816aSdp-arm 1117941816aSdp-arm /* 1127941816aSdp-arm * Flush cache line so that even if CPU power down happens 1137941816aSdp-arm * the timestamp update is reflected in memory. 1147941816aSdp-arm */ 1157941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1167941816aSdp-arm RT_INSTR_ENTER_CFLUSH, 1177941816aSdp-arm PMF_CACHE_MAINT); 1187941816aSdp-arm #endif 1197941816aSdp-arm 120532ed618SSoby Mathew /* 121b0408e87SJeenu Viswambharan * Arch. management. Initiate power down sequence. 122532ed618SSoby Mathew * TODO : Introduce a mechanism to query the cache level to flush 123532ed618SSoby Mathew * and the cpu-ops power down to perform from the platform. 124532ed618SSoby Mathew */ 125b0408e87SJeenu Viswambharan psci_do_pwrdown_sequence(max_off_lvl); 1267941816aSdp-arm 1277941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1287941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1297941816aSdp-arm RT_INSTR_EXIT_CFLUSH, 1307941816aSdp-arm PMF_NO_CACHE_MAINT); 1317941816aSdp-arm #endif 132532ed618SSoby Mathew } 133532ed618SSoby Mathew 134532ed618SSoby Mathew /******************************************************************************* 135532ed618SSoby Mathew * Top level handler which is called when a cpu wants to suspend its execution. 136532ed618SSoby Mathew * It is assumed that along with suspending the cpu power domain, power domains 137532ed618SSoby Mathew * at higher levels until the target power level will be suspended as well. It 138532ed618SSoby Mathew * coordinates with the platform to negotiate the target state for each of 139532ed618SSoby Mathew * the power domain level till the target power domain level. It then performs 140532ed618SSoby Mathew * generic, architectural, platform setup and state management required to 141532ed618SSoby Mathew * suspend that power domain level and power domain levels below it. 142532ed618SSoby Mathew * e.g. For a cpu that's to be suspended, it could mean programming the 143532ed618SSoby Mathew * power controller whereas for a cluster that's to be suspended, it will call 144532ed618SSoby Mathew * the platform specific code which will disable coherency at the interconnect 145532ed618SSoby Mathew * level if the cpu is the last in the cluster and also the program the power 146532ed618SSoby Mathew * controller. 147532ed618SSoby Mathew * 148532ed618SSoby Mathew * All the required parameter checks are performed at the beginning and after 149532ed618SSoby Mathew * the state transition has been done, no further error is expected and it is 150532ed618SSoby Mathew * not possible to undo any of the actions taken beyond that point. 151532ed618SSoby Mathew ******************************************************************************/ 152621d64f8SAntonio Nino Diaz void psci_cpu_suspend_start(const entry_point_info_t *ep, 153532ed618SSoby Mathew unsigned int end_pwrlvl, 154532ed618SSoby Mathew psci_power_state_t *state_info, 155532ed618SSoby Mathew unsigned int is_power_down_state) 156532ed618SSoby Mathew { 157532ed618SSoby Mathew int skip_wfi = 0; 158621d64f8SAntonio Nino Diaz int idx = (int) plat_my_core_pos(); 159532ed618SSoby Mathew 160532ed618SSoby Mathew /* 161532ed618SSoby Mathew * This function must only be called on platforms where the 162532ed618SSoby Mathew * CPU_SUSPEND platform hooks have been implemented. 163532ed618SSoby Mathew */ 164621d64f8SAntonio Nino Diaz assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) && 165621d64f8SAntonio Nino Diaz (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)); 166532ed618SSoby Mathew 167532ed618SSoby Mathew /* 168532ed618SSoby Mathew * This function acquires the lock corresponding to each power 169532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 170532ed618SSoby Mathew * is snapshot and state management can be done safely. 171532ed618SSoby Mathew */ 172532ed618SSoby Mathew psci_acquire_pwr_domain_locks(end_pwrlvl, 173532ed618SSoby Mathew idx); 174532ed618SSoby Mathew 175532ed618SSoby Mathew /* 176532ed618SSoby Mathew * We check if there are any pending interrupts after the delay 177532ed618SSoby Mathew * introduced by lock contention to increase the chances of early 178532ed618SSoby Mathew * detection that a wake-up interrupt has fired. 179532ed618SSoby Mathew */ 180621d64f8SAntonio Nino Diaz if (read_isr_el1() != 0U) { 181532ed618SSoby Mathew skip_wfi = 1; 182532ed618SSoby Mathew goto exit; 183532ed618SSoby Mathew } 184532ed618SSoby Mathew 185532ed618SSoby Mathew /* 186532ed618SSoby Mathew * This function is passed the requested state info and 187532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 188532ed618SSoby Mathew * the end level specified. 189532ed618SSoby Mathew */ 190532ed618SSoby Mathew psci_do_state_coordination(end_pwrlvl, state_info); 191532ed618SSoby Mathew 192532ed618SSoby Mathew #if ENABLE_PSCI_STAT 193532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 194532ed618SSoby Mathew psci_stats_update_pwr_down(end_pwrlvl, state_info); 195532ed618SSoby Mathew #endif 196532ed618SSoby Mathew 197621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) 198532ed618SSoby Mathew psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info); 199532ed618SSoby Mathew 200532ed618SSoby Mathew /* 201532ed618SSoby Mathew * Plat. management: Allow the platform to perform the 202532ed618SSoby Mathew * necessary actions to turn off this cpu e.g. set the 203532ed618SSoby Mathew * platform defined mailbox with the psci entrypoint, 204532ed618SSoby Mathew * program the power controller etc. 205532ed618SSoby Mathew */ 206532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend(state_info); 207532ed618SSoby Mathew 208532ed618SSoby Mathew #if ENABLE_PSCI_STAT 20904c1db1eSdp-arm plat_psci_stat_accounting_start(state_info); 210532ed618SSoby Mathew #endif 211532ed618SSoby Mathew 212532ed618SSoby Mathew exit: 213532ed618SSoby Mathew /* 214532ed618SSoby Mathew * Release the locks corresponding to each power level in the 215532ed618SSoby Mathew * reverse order to which they were acquired. 216532ed618SSoby Mathew */ 217532ed618SSoby Mathew psci_release_pwr_domain_locks(end_pwrlvl, 218532ed618SSoby Mathew idx); 219621d64f8SAntonio Nino Diaz if (skip_wfi == 1) 220532ed618SSoby Mathew return; 221532ed618SSoby Mathew 222621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) { 223872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 224872be88aSdp-arm 225872be88aSdp-arm /* 226872be88aSdp-arm * Update the timestamp with cache off. We assume this 227872be88aSdp-arm * timestamp can only be read from the current CPU and the 228872be88aSdp-arm * timestamp cache line will be flushed before return to 229872be88aSdp-arm * normal world on wakeup. 230872be88aSdp-arm */ 231872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 232872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 233872be88aSdp-arm PMF_NO_CACHE_MAINT); 234872be88aSdp-arm #endif 235872be88aSdp-arm 236532ed618SSoby Mathew /* The function calls below must not return */ 237621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) 238532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info); 239532ed618SSoby Mathew else 240532ed618SSoby Mathew psci_power_down_wfi(); 241532ed618SSoby Mathew } 242532ed618SSoby Mathew 243872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 244872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 245872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 246872be88aSdp-arm PMF_NO_CACHE_MAINT); 247872be88aSdp-arm #endif 248872be88aSdp-arm 249532ed618SSoby Mathew /* 250532ed618SSoby Mathew * We will reach here if only retention/standby states have been 251532ed618SSoby Mathew * requested at multiple power levels. This means that the cpu 252532ed618SSoby Mathew * context will be preserved. 253532ed618SSoby Mathew */ 254532ed618SSoby Mathew wfi(); 255532ed618SSoby Mathew 256872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 257872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 258872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 259872be88aSdp-arm PMF_NO_CACHE_MAINT); 260872be88aSdp-arm #endif 261872be88aSdp-arm 262532ed618SSoby Mathew /* 263532ed618SSoby Mathew * After we wake up from context retaining suspend, call the 264532ed618SSoby Mathew * context retaining suspend finisher. 265532ed618SSoby Mathew */ 26661eae524SAchin Gupta psci_suspend_to_standby_finisher(idx, end_pwrlvl); 267532ed618SSoby Mathew } 268532ed618SSoby Mathew 269532ed618SSoby Mathew /******************************************************************************* 270532ed618SSoby Mathew * The following functions finish an earlier suspend request. They 271532ed618SSoby Mathew * are called by the common finisher routine in psci_common.c. The `state_info` 272532ed618SSoby Mathew * is the psci_power_state from which this CPU has woken up from. 273532ed618SSoby Mathew ******************************************************************************/ 274621d64f8SAntonio Nino Diaz void psci_cpu_suspend_finish(int cpu_idx, const psci_power_state_t *state_info) 275532ed618SSoby Mathew { 276532ed618SSoby Mathew unsigned int counter_freq; 277532ed618SSoby Mathew unsigned int max_off_lvl; 278532ed618SSoby Mathew 279532ed618SSoby Mathew /* Ensure we have been woken up from a suspended state */ 280621d64f8SAntonio Nino Diaz assert((psci_get_aff_info_state() == AFF_STATE_ON) && 281621d64f8SAntonio Nino Diaz (is_local_state_off( 282621d64f8SAntonio Nino Diaz state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0)); 283532ed618SSoby Mathew 284532ed618SSoby Mathew /* 285532ed618SSoby Mathew * Plat. management: Perform the platform specific actions 286532ed618SSoby Mathew * before we change the state of the cpu e.g. enabling the 287532ed618SSoby Mathew * gic or zeroing the mailbox register. If anything goes 288532ed618SSoby Mathew * wrong then assert as there is no way to recover from this 289532ed618SSoby Mathew * situation. 290532ed618SSoby Mathew */ 291532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 292532ed618SSoby Mathew 293bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 294b0408e87SJeenu Viswambharan /* Arch. management: Enable the data cache, stack memory maintenance. */ 295532ed618SSoby Mathew psci_do_pwrup_cache_maintenance(); 296b0408e87SJeenu Viswambharan #endif 297532ed618SSoby Mathew 298532ed618SSoby Mathew /* Re-init the cntfrq_el0 register */ 299532ed618SSoby Mathew counter_freq = plat_get_syscnt_freq2(); 300532ed618SSoby Mathew write_cntfrq_el0(counter_freq); 301532ed618SSoby Mathew 302532ed618SSoby Mathew /* 303532ed618SSoby Mathew * Call the cpu suspend finish handler registered by the Secure Payload 304532ed618SSoby Mathew * Dispatcher to let it do any bookeeping. If the handler encounters an 305532ed618SSoby Mathew * error, it's expected to assert within 306532ed618SSoby Mathew */ 307621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) { 308532ed618SSoby Mathew max_off_lvl = psci_find_max_off_lvl(state_info); 309532ed618SSoby Mathew assert(max_off_lvl != PSCI_INVALID_PWR_LVL); 310532ed618SSoby Mathew psci_spd_pm->svc_suspend_finish(max_off_lvl); 311532ed618SSoby Mathew } 312532ed618SSoby Mathew 313532ed618SSoby Mathew /* Invalidate the suspend level for the cpu */ 314532ed618SSoby Mathew psci_set_suspend_pwrlvl(PSCI_INVALID_PWR_LVL); 315532ed618SSoby Mathew 3167593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_finish); 3177593252cSDimitris Papastamos 318532ed618SSoby Mathew /* 319532ed618SSoby Mathew * Generic management: Now we just need to retrieve the 320532ed618SSoby Mathew * information that we had stashed away during the suspend 321532ed618SSoby Mathew * call to set this cpu on its way. 322532ed618SSoby Mathew */ 323532ed618SSoby Mathew cm_prepare_el3_exit(NON_SECURE); 324532ed618SSoby Mathew } 325