1532ed618SSoby Mathew /* 2*621d64f8SAntonio 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 7532ed618SSoby Mathew #include <arch.h> 8532ed618SSoby Mathew #include <arch_helpers.h> 92a4b4b71SIsla Mitchell #include <assert.h> 102a4b4b71SIsla Mitchell #include <bl_common.h> 11532ed618SSoby Mathew #include <context.h> 12532ed618SSoby Mathew #include <context_mgmt.h> 13532ed618SSoby Mathew #include <cpu_data.h> 14532ed618SSoby Mathew #include <debug.h> 15532ed618SSoby Mathew #include <platform.h> 16872be88aSdp-arm #include <pmf.h> 177593252cSDimitris Papastamos #include <pubsub_events.h> 18872be88aSdp-arm #include <runtime_instr.h> 19532ed618SSoby Mathew #include <stddef.h> 20532ed618SSoby Mathew #include "psci_private.h" 21532ed618SSoby Mathew 22532ed618SSoby Mathew /******************************************************************************* 23532ed618SSoby Mathew * This function does generic and platform specific operations after a wake-up 24532ed618SSoby Mathew * from standby/retention states at multiple power levels. 25532ed618SSoby Mathew ******************************************************************************/ 26*621d64f8SAntonio Nino Diaz static void psci_suspend_to_standby_finisher(int cpu_idx, 27532ed618SSoby Mathew unsigned int end_pwrlvl) 28532ed618SSoby Mathew { 2961eae524SAchin Gupta psci_power_state_t state_info; 3061eae524SAchin Gupta 31532ed618SSoby Mathew psci_acquire_pwr_domain_locks(end_pwrlvl, 32532ed618SSoby Mathew cpu_idx); 33532ed618SSoby Mathew 34532ed618SSoby Mathew /* 3561eae524SAchin Gupta * Find out which retention states this CPU has exited from until the 3661eae524SAchin Gupta * 'end_pwrlvl'. The exit retention state could be deeper than the entry 3761eae524SAchin Gupta * state as a result of state coordination amongst other CPUs post wfi. 3861eae524SAchin Gupta */ 3961eae524SAchin Gupta psci_get_target_local_pwr_states(end_pwrlvl, &state_info); 4061eae524SAchin Gupta 41bfc87a8dSSoby Mathew #if ENABLE_PSCI_STAT 42bfc87a8dSSoby Mathew plat_psci_stat_accounting_stop(&state_info); 43bfc87a8dSSoby Mathew psci_stats_update_pwr_up(end_pwrlvl, &state_info); 44bfc87a8dSSoby Mathew #endif 45bfc87a8dSSoby Mathew 4661eae524SAchin Gupta /* 47532ed618SSoby Mathew * Plat. management: Allow the platform to do operations 48532ed618SSoby Mathew * on waking up from retention. 49532ed618SSoby Mathew */ 5061eae524SAchin Gupta psci_plat_pm_ops->pwr_domain_suspend_finish(&state_info); 51532ed618SSoby Mathew 52532ed618SSoby Mathew /* 53532ed618SSoby Mathew * Set the requested and target state of this CPU and all the higher 54532ed618SSoby Mathew * power domain levels for this CPU to run. 55532ed618SSoby Mathew */ 56532ed618SSoby Mathew psci_set_pwr_domains_to_run(end_pwrlvl); 57532ed618SSoby Mathew 58532ed618SSoby Mathew psci_release_pwr_domain_locks(end_pwrlvl, 59532ed618SSoby Mathew cpu_idx); 60532ed618SSoby Mathew } 61532ed618SSoby Mathew 62532ed618SSoby Mathew /******************************************************************************* 63532ed618SSoby Mathew * This function does generic and platform specific suspend to power down 64532ed618SSoby Mathew * operations. 65532ed618SSoby Mathew ******************************************************************************/ 66532ed618SSoby Mathew static void psci_suspend_to_pwrdown_start(unsigned int end_pwrlvl, 67*621d64f8SAntonio Nino Diaz const entry_point_info_t *ep, 68*621d64f8SAntonio Nino Diaz const psci_power_state_t *state_info) 69532ed618SSoby Mathew { 70532ed618SSoby Mathew unsigned int max_off_lvl = psci_find_max_off_lvl(state_info); 71532ed618SSoby Mathew 727593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_start); 737593252cSDimitris Papastamos 74532ed618SSoby Mathew /* Save PSCI target power level for the suspend finisher handler */ 75532ed618SSoby Mathew psci_set_suspend_pwrlvl(end_pwrlvl); 76532ed618SSoby Mathew 77532ed618SSoby Mathew /* 78a10d3632SJeenu Viswambharan * Flush the target power level as it might be accessed on power up with 79532ed618SSoby Mathew * Data cache disabled. 80532ed618SSoby Mathew */ 81a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl); 82532ed618SSoby Mathew 83532ed618SSoby Mathew /* 84532ed618SSoby Mathew * Call the cpu suspend handler registered by the Secure Payload 85532ed618SSoby Mathew * Dispatcher to let it do any book-keeping. If the handler encounters an 86532ed618SSoby Mathew * error, it's expected to assert within 87532ed618SSoby Mathew */ 88*621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL)) 89532ed618SSoby Mathew psci_spd_pm->svc_suspend(max_off_lvl); 90532ed618SSoby Mathew 911862d620SVarun Wadekar #if !HW_ASSISTED_COHERENCY 921862d620SVarun Wadekar /* 931862d620SVarun Wadekar * Plat. management: Allow the platform to perform any early 941862d620SVarun Wadekar * actions required to power down the CPU. This might be useful for 951862d620SVarun Wadekar * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these 961862d620SVarun Wadekar * actions with data caches enabled. 971862d620SVarun Wadekar */ 98*621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL) 991862d620SVarun Wadekar psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info); 1001862d620SVarun Wadekar #endif 1011862d620SVarun Wadekar 102532ed618SSoby Mathew /* 103532ed618SSoby Mathew * Store the re-entry information for the non-secure world. 104532ed618SSoby Mathew */ 105532ed618SSoby Mathew cm_init_my_context(ep); 106532ed618SSoby Mathew 1077941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1087941816aSdp-arm 1097941816aSdp-arm /* 1107941816aSdp-arm * Flush cache line so that even if CPU power down happens 1117941816aSdp-arm * the timestamp update is reflected in memory. 1127941816aSdp-arm */ 1137941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1147941816aSdp-arm RT_INSTR_ENTER_CFLUSH, 1157941816aSdp-arm PMF_CACHE_MAINT); 1167941816aSdp-arm #endif 1177941816aSdp-arm 118532ed618SSoby Mathew /* 119b0408e87SJeenu Viswambharan * Arch. management. Initiate power down sequence. 120532ed618SSoby Mathew * TODO : Introduce a mechanism to query the cache level to flush 121532ed618SSoby Mathew * and the cpu-ops power down to perform from the platform. 122532ed618SSoby Mathew */ 123b0408e87SJeenu Viswambharan psci_do_pwrdown_sequence(max_off_lvl); 1247941816aSdp-arm 1257941816aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 1267941816aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 1277941816aSdp-arm RT_INSTR_EXIT_CFLUSH, 1287941816aSdp-arm PMF_NO_CACHE_MAINT); 1297941816aSdp-arm #endif 130532ed618SSoby Mathew } 131532ed618SSoby Mathew 132532ed618SSoby Mathew /******************************************************************************* 133532ed618SSoby Mathew * Top level handler which is called when a cpu wants to suspend its execution. 134532ed618SSoby Mathew * It is assumed that along with suspending the cpu power domain, power domains 135532ed618SSoby Mathew * at higher levels until the target power level will be suspended as well. It 136532ed618SSoby Mathew * coordinates with the platform to negotiate the target state for each of 137532ed618SSoby Mathew * the power domain level till the target power domain level. It then performs 138532ed618SSoby Mathew * generic, architectural, platform setup and state management required to 139532ed618SSoby Mathew * suspend that power domain level and power domain levels below it. 140532ed618SSoby Mathew * e.g. For a cpu that's to be suspended, it could mean programming the 141532ed618SSoby Mathew * power controller whereas for a cluster that's to be suspended, it will call 142532ed618SSoby Mathew * the platform specific code which will disable coherency at the interconnect 143532ed618SSoby Mathew * level if the cpu is the last in the cluster and also the program the power 144532ed618SSoby Mathew * controller. 145532ed618SSoby Mathew * 146532ed618SSoby Mathew * All the required parameter checks are performed at the beginning and after 147532ed618SSoby Mathew * the state transition has been done, no further error is expected and it is 148532ed618SSoby Mathew * not possible to undo any of the actions taken beyond that point. 149532ed618SSoby Mathew ******************************************************************************/ 150*621d64f8SAntonio Nino Diaz void psci_cpu_suspend_start(const entry_point_info_t *ep, 151532ed618SSoby Mathew unsigned int end_pwrlvl, 152532ed618SSoby Mathew psci_power_state_t *state_info, 153532ed618SSoby Mathew unsigned int is_power_down_state) 154532ed618SSoby Mathew { 155532ed618SSoby Mathew int skip_wfi = 0; 156*621d64f8SAntonio Nino Diaz int idx = (int) plat_my_core_pos(); 157532ed618SSoby Mathew 158532ed618SSoby Mathew /* 159532ed618SSoby Mathew * This function must only be called on platforms where the 160532ed618SSoby Mathew * CPU_SUSPEND platform hooks have been implemented. 161532ed618SSoby Mathew */ 162*621d64f8SAntonio Nino Diaz assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) && 163*621d64f8SAntonio Nino Diaz (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)); 164532ed618SSoby Mathew 165532ed618SSoby Mathew /* 166532ed618SSoby Mathew * This function acquires the lock corresponding to each power 167532ed618SSoby Mathew * level so that by the time all locks are taken, the system topology 168532ed618SSoby Mathew * is snapshot and state management can be done safely. 169532ed618SSoby Mathew */ 170532ed618SSoby Mathew psci_acquire_pwr_domain_locks(end_pwrlvl, 171532ed618SSoby Mathew idx); 172532ed618SSoby Mathew 173532ed618SSoby Mathew /* 174532ed618SSoby Mathew * We check if there are any pending interrupts after the delay 175532ed618SSoby Mathew * introduced by lock contention to increase the chances of early 176532ed618SSoby Mathew * detection that a wake-up interrupt has fired. 177532ed618SSoby Mathew */ 178*621d64f8SAntonio Nino Diaz if (read_isr_el1() != 0U) { 179532ed618SSoby Mathew skip_wfi = 1; 180532ed618SSoby Mathew goto exit; 181532ed618SSoby Mathew } 182532ed618SSoby Mathew 183532ed618SSoby Mathew /* 184532ed618SSoby Mathew * This function is passed the requested state info and 185532ed618SSoby Mathew * it returns the negotiated state info for each power level upto 186532ed618SSoby Mathew * the end level specified. 187532ed618SSoby Mathew */ 188532ed618SSoby Mathew psci_do_state_coordination(end_pwrlvl, state_info); 189532ed618SSoby Mathew 190532ed618SSoby Mathew #if ENABLE_PSCI_STAT 191532ed618SSoby Mathew /* Update the last cpu for each level till end_pwrlvl */ 192532ed618SSoby Mathew psci_stats_update_pwr_down(end_pwrlvl, state_info); 193532ed618SSoby Mathew #endif 194532ed618SSoby Mathew 195*621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) 196532ed618SSoby Mathew psci_suspend_to_pwrdown_start(end_pwrlvl, ep, state_info); 197532ed618SSoby Mathew 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 exit: 211532ed618SSoby Mathew /* 212532ed618SSoby Mathew * Release the locks corresponding to each power level in the 213532ed618SSoby Mathew * reverse order to which they were acquired. 214532ed618SSoby Mathew */ 215532ed618SSoby Mathew psci_release_pwr_domain_locks(end_pwrlvl, 216532ed618SSoby Mathew idx); 217*621d64f8SAntonio Nino Diaz if (skip_wfi == 1) 218532ed618SSoby Mathew return; 219532ed618SSoby Mathew 220*621d64f8SAntonio Nino Diaz if (is_power_down_state != 0U) { 221872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 222872be88aSdp-arm 223872be88aSdp-arm /* 224872be88aSdp-arm * Update the timestamp with cache off. We assume this 225872be88aSdp-arm * timestamp can only be read from the current CPU and the 226872be88aSdp-arm * timestamp cache line will be flushed before return to 227872be88aSdp-arm * normal world on wakeup. 228872be88aSdp-arm */ 229872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 230872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 231872be88aSdp-arm PMF_NO_CACHE_MAINT); 232872be88aSdp-arm #endif 233872be88aSdp-arm 234532ed618SSoby Mathew /* The function calls below must not return */ 235*621d64f8SAntonio Nino Diaz if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) 236532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_pwr_down_wfi(state_info); 237532ed618SSoby Mathew else 238532ed618SSoby Mathew psci_power_down_wfi(); 239532ed618SSoby Mathew } 240532ed618SSoby Mathew 241872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 242872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 243872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 244872be88aSdp-arm PMF_NO_CACHE_MAINT); 245872be88aSdp-arm #endif 246872be88aSdp-arm 247532ed618SSoby Mathew /* 248532ed618SSoby Mathew * We will reach here if only retention/standby states have been 249532ed618SSoby Mathew * requested at multiple power levels. This means that the cpu 250532ed618SSoby Mathew * context will be preserved. 251532ed618SSoby Mathew */ 252532ed618SSoby Mathew wfi(); 253532ed618SSoby Mathew 254872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 255872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 256872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 257872be88aSdp-arm PMF_NO_CACHE_MAINT); 258872be88aSdp-arm #endif 259872be88aSdp-arm 260532ed618SSoby Mathew /* 261532ed618SSoby Mathew * After we wake up from context retaining suspend, call the 262532ed618SSoby Mathew * context retaining suspend finisher. 263532ed618SSoby Mathew */ 26461eae524SAchin Gupta psci_suspend_to_standby_finisher(idx, end_pwrlvl); 265532ed618SSoby Mathew } 266532ed618SSoby Mathew 267532ed618SSoby Mathew /******************************************************************************* 268532ed618SSoby Mathew * The following functions finish an earlier suspend request. They 269532ed618SSoby Mathew * are called by the common finisher routine in psci_common.c. The `state_info` 270532ed618SSoby Mathew * is the psci_power_state from which this CPU has woken up from. 271532ed618SSoby Mathew ******************************************************************************/ 272*621d64f8SAntonio Nino Diaz void psci_cpu_suspend_finish(int cpu_idx, const psci_power_state_t *state_info) 273532ed618SSoby Mathew { 274532ed618SSoby Mathew unsigned int counter_freq; 275532ed618SSoby Mathew unsigned int max_off_lvl; 276532ed618SSoby Mathew 277532ed618SSoby Mathew /* Ensure we have been woken up from a suspended state */ 278*621d64f8SAntonio Nino Diaz assert((psci_get_aff_info_state() == AFF_STATE_ON) && 279*621d64f8SAntonio Nino Diaz (is_local_state_off( 280*621d64f8SAntonio Nino Diaz state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0)); 281532ed618SSoby Mathew 282532ed618SSoby Mathew /* 283532ed618SSoby Mathew * Plat. management: Perform the platform specific actions 284532ed618SSoby Mathew * before we change the state of the cpu e.g. enabling the 285532ed618SSoby Mathew * gic or zeroing the mailbox register. If anything goes 286532ed618SSoby Mathew * wrong then assert as there is no way to recover from this 287532ed618SSoby Mathew * situation. 288532ed618SSoby Mathew */ 289532ed618SSoby Mathew psci_plat_pm_ops->pwr_domain_suspend_finish(state_info); 290532ed618SSoby Mathew 291bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 292b0408e87SJeenu Viswambharan /* Arch. management: Enable the data cache, stack memory maintenance. */ 293532ed618SSoby Mathew psci_do_pwrup_cache_maintenance(); 294b0408e87SJeenu Viswambharan #endif 295532ed618SSoby Mathew 296532ed618SSoby Mathew /* Re-init the cntfrq_el0 register */ 297532ed618SSoby Mathew counter_freq = plat_get_syscnt_freq2(); 298532ed618SSoby Mathew write_cntfrq_el0(counter_freq); 299532ed618SSoby Mathew 300532ed618SSoby Mathew /* 301532ed618SSoby Mathew * Call the cpu suspend finish handler registered by the Secure Payload 302532ed618SSoby Mathew * Dispatcher to let it do any bookeeping. If the handler encounters an 303532ed618SSoby Mathew * error, it's expected to assert within 304532ed618SSoby Mathew */ 305*621d64f8SAntonio Nino Diaz if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) { 306532ed618SSoby Mathew max_off_lvl = psci_find_max_off_lvl(state_info); 307532ed618SSoby Mathew assert(max_off_lvl != PSCI_INVALID_PWR_LVL); 308532ed618SSoby Mathew psci_spd_pm->svc_suspend_finish(max_off_lvl); 309532ed618SSoby Mathew } 310532ed618SSoby Mathew 311532ed618SSoby Mathew /* Invalidate the suspend level for the cpu */ 312532ed618SSoby Mathew psci_set_suspend_pwrlvl(PSCI_INVALID_PWR_LVL); 313532ed618SSoby Mathew 3147593252cSDimitris Papastamos PUBLISH_EVENT(psci_suspend_pwrdown_finish); 3157593252cSDimitris Papastamos 316532ed618SSoby Mathew /* 317532ed618SSoby Mathew * Generic management: Now we just need to retrieve the 318532ed618SSoby Mathew * information that we had stashed away during the suspend 319532ed618SSoby Mathew * call to set this cpu on its way. 320532ed618SSoby Mathew */ 321532ed618SSoby Mathew cm_prepare_el3_exit(NON_SECURE); 322532ed618SSoby Mathew } 323