1b4315306SDan Handley /* 2abd2aba9SSoby Mathew * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 3b4315306SDan Handley * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5b4315306SDan Handley */ 6b4315306SDan Handley 7b4315306SDan Handley #include <arch_helpers.h> 8785fb92bSSoby Mathew #include <assert.h> 9c1bb8a05SSoby Mathew #include <cassert.h> 10785fb92bSSoby Mathew #include <css_pm.h> 11b4315306SDan Handley #include <debug.h> 12b4315306SDan Handley #include <errno.h> 13b4315306SDan Handley #include <plat_arm.h> 14b4315306SDan Handley #include <platform.h> 15b4315306SDan Handley #include <platform_def.h> 16b12a2b49SSoby Mathew #include "../drivers/scp/css_scp.h" 17f14d1886SSoby Mathew 18785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */ 19785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops 2038dce70fSSoby Mathew 212204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC 222204afdeSSoby Mathew /* 232204afdeSSoby Mathew * The table storing the valid idle power states. Ensure that the 242204afdeSSoby Mathew * array entries are populated in ascending order of state-id to 252204afdeSSoby Mathew * enable us to use binary search during power state validation. 262204afdeSSoby Mathew * The table must be terminated by a NULL entry. 272204afdeSSoby Mathew */ 282204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = { 295f3a6030SSoby Mathew /* State-id - 0x001 */ 305f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN, 315f3a6030SSoby Mathew ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY), 325f3a6030SSoby Mathew /* State-id - 0x002 */ 335f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN, 345f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN), 355f3a6030SSoby Mathew /* State-id - 0x022 */ 365f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF, 375f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN), 385f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1 395f3a6030SSoby Mathew /* State-id - 0x222 */ 405f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF, 415f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN), 425f3a6030SSoby Mathew #endif 432204afdeSSoby Mathew 0, 442204afdeSSoby Mathew }; 455f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */ 462204afdeSSoby Mathew 47c1bb8a05SSoby Mathew /* 48c1bb8a05SSoby Mathew * All the power management helpers in this file assume at least cluster power 49c1bb8a05SSoby Mathew * level is supported. 50c1bb8a05SSoby Mathew */ 51c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1, 52c1bb8a05SSoby Mathew assert_max_pwr_lvl_supported_mismatch); 53c1bb8a05SSoby Mathew 54abd2aba9SSoby Mathew /* 55abd2aba9SSoby Mathew * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL 56abd2aba9SSoby Mathew * assumed by the CSS layer. 57abd2aba9SSoby Mathew */ 58abd2aba9SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL, 59abd2aba9SSoby Mathew assert_max_pwr_lvl_higher_than_css_sys_lvl); 60abd2aba9SSoby Mathew 61b4315306SDan Handley /******************************************************************************* 6238dce70fSSoby Mathew * Handler called when a power domain is about to be turned on. The 63b4315306SDan Handley * level and mpidr determine the affinity instance. 64b4315306SDan Handley ******************************************************************************/ 6538dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr) 66b4315306SDan Handley { 67b12a2b49SSoby Mathew css_scp_on(mpidr); 68b4315306SDan Handley 69b4315306SDan Handley return PSCI_E_SUCCESS; 70b4315306SDan Handley } 71b4315306SDan Handley 72f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common( 73f14d1886SSoby Mathew const psci_power_state_t *target_state) 74b4315306SDan Handley { 75f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 76c1bb8a05SSoby Mathew 77b4315306SDan Handley /* 78b4315306SDan Handley * Perform the common cluster specific operations i.e enable coherency 79b4315306SDan Handley * if this cluster was off. 80b4315306SDan Handley */ 81f14d1886SSoby Mathew if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 826355f234SVikram Kanigiri plat_arm_interconnect_enter_coherency(); 83c1bb8a05SSoby Mathew } 84c1bb8a05SSoby Mathew 85f14d1886SSoby Mathew /******************************************************************************* 86f14d1886SSoby Mathew * Handler called when a power level has just been powered on after 87f14d1886SSoby Mathew * being turned off earlier. The target_state encodes the low power state that 88f14d1886SSoby Mathew * each level has woken up from. This handler would never be invoked with 89f14d1886SSoby Mathew * the system power domain uninitialized as either the primary would have taken 90f14d1886SSoby Mathew * care of it as part of cold boot or the first core awakened from system 91f14d1886SSoby Mathew * suspend would have already initialized it. 92f14d1886SSoby Mathew ******************************************************************************/ 93f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state) 94f14d1886SSoby Mathew { 95f14d1886SSoby Mathew /* Assert that the system power domain need not be initialized */ 96f14d1886SSoby Mathew assert(CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_RUN); 97f14d1886SSoby Mathew 98f14d1886SSoby Mathew css_pwr_domain_on_finisher_common(target_state); 99f14d1886SSoby Mathew 10027573c59SAchin Gupta /* Program the gic per-cpu distributor or re-distributor interface */ 10127573c59SAchin Gupta plat_arm_gic_pcpu_init(); 10227573c59SAchin Gupta 103b4315306SDan Handley /* Enable the gic cpu interface */ 10427573c59SAchin Gupta plat_arm_gic_cpuif_enable(); 105b4315306SDan Handley } 106b4315306SDan Handley 107b4315306SDan Handley /******************************************************************************* 108b4315306SDan Handley * Common function called while turning a cpu off or suspending it. It is called 109b4315306SDan Handley * from css_off() or css_suspend() when these functions in turn are called for 11038dce70fSSoby Mathew * power domain at the highest power level which will be powered down. It 11138dce70fSSoby Mathew * performs the actions common to the OFF and SUSPEND calls. 112b4315306SDan Handley ******************************************************************************/ 11338dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state) 114b4315306SDan Handley { 115b4315306SDan Handley /* Prevent interrupts from spuriously waking up this cpu */ 11627573c59SAchin Gupta plat_arm_gic_cpuif_disable(); 117b4315306SDan Handley 118b4315306SDan Handley /* Cluster is to be turned off, so disable coherency */ 119b12a2b49SSoby Mathew if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 1206355f234SVikram Kanigiri plat_arm_interconnect_exit_coherency(); 121b4315306SDan Handley } 122b4315306SDan Handley 123b4315306SDan Handley /******************************************************************************* 12438dce70fSSoby Mathew * Handler called when a power domain is about to be turned off. The 12538dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 126b4315306SDan Handley ******************************************************************************/ 127785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state) 128b4315306SDan Handley { 129f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 13038dce70fSSoby Mathew css_power_down_common(target_state); 131b12a2b49SSoby Mathew css_scp_off(target_state); 132b4315306SDan Handley } 133b4315306SDan Handley 134b4315306SDan Handley /******************************************************************************* 13538dce70fSSoby Mathew * Handler called when a power domain is about to be suspended. The 13638dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 137b4315306SDan Handley ******************************************************************************/ 138785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state) 139b4315306SDan Handley { 14038dce70fSSoby Mathew /* 141f14d1886SSoby Mathew * CSS currently supports retention only at cpu level. Just return 14238dce70fSSoby Mathew * as nothing is to be done for retention. 14338dce70fSSoby Mathew */ 144f14d1886SSoby Mathew if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET) 145b4315306SDan Handley return; 146b4315306SDan Handley 147f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 14838dce70fSSoby Mathew css_power_down_common(target_state); 149b12a2b49SSoby Mathew css_scp_suspend(target_state); 150b4315306SDan Handley } 151b4315306SDan Handley 152b4315306SDan Handley /******************************************************************************* 15338dce70fSSoby Mathew * Handler called when a power domain has just been powered on after 15438dce70fSSoby Mathew * having been suspended earlier. The target_state encodes the low power state 15538dce70fSSoby Mathew * that each level has woken up from. 156b4315306SDan Handley * TODO: At the moment we reuse the on finisher and reinitialize the secure 157b4315306SDan Handley * context. Need to implement a separate suspend finisher. 158b4315306SDan Handley ******************************************************************************/ 159785fb92bSSoby Mathew void css_pwr_domain_suspend_finish( 16038dce70fSSoby Mathew const psci_power_state_t *target_state) 161b4315306SDan Handley { 162f14d1886SSoby Mathew /* Return as nothing is to be done on waking up from retention. */ 163f14d1886SSoby Mathew if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET) 16438dce70fSSoby Mathew return; 16538dce70fSSoby Mathew 166f14d1886SSoby Mathew /* Perform system domain restore if woken up from system suspend */ 167f14d1886SSoby Mathew if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 168f14d1886SSoby Mathew arm_system_pwr_domain_resume(); 169f14d1886SSoby Mathew else 170f14d1886SSoby Mathew /* Enable the gic cpu interface */ 17127573c59SAchin Gupta plat_arm_gic_cpuif_enable(); 172f14d1886SSoby Mathew 173f14d1886SSoby Mathew css_pwr_domain_on_finisher_common(target_state); 174b4315306SDan Handley } 175b4315306SDan Handley 176b4315306SDan Handley /******************************************************************************* 177b4315306SDan Handley * Handlers to shutdown/reboot the system 178b4315306SDan Handley ******************************************************************************/ 179785fb92bSSoby Mathew void __dead2 css_system_off(void) 180b4315306SDan Handley { 181b12a2b49SSoby Mathew css_scp_sys_shutdown(); 182b4315306SDan Handley } 183b4315306SDan Handley 184785fb92bSSoby Mathew void __dead2 css_system_reset(void) 185b4315306SDan Handley { 186b12a2b49SSoby Mathew css_scp_sys_reboot(); 187b4315306SDan Handley } 188b4315306SDan Handley 189b4315306SDan Handley /******************************************************************************* 19038dce70fSSoby Mathew * Handler called when the CPU power domain is about to enter standby. 191b4315306SDan Handley ******************************************************************************/ 19238dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state) 193b4315306SDan Handley { 194b4315306SDan Handley unsigned int scr; 195b4315306SDan Handley 19638dce70fSSoby Mathew assert(cpu_state == ARM_LOCAL_STATE_RET); 19738dce70fSSoby Mathew 198b4315306SDan Handley scr = read_scr_el3(); 19968b105aeSDavid Wang /* 20068b105aeSDavid Wang * Enable the Non secure interrupt to wake the CPU. 20168b105aeSDavid Wang * In GICv3 affinity routing mode, the non secure group1 interrupts use 20268b105aeSDavid Wang * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ. 20368b105aeSDavid Wang * Enabling both the bits works for both GICv2 mode and GICv3 affinity 20468b105aeSDavid Wang * routing mode. 20568b105aeSDavid Wang */ 20668b105aeSDavid Wang write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT); 207b4315306SDan Handley isb(); 208b4315306SDan Handley dsb(); 209b4315306SDan Handley wfi(); 210b4315306SDan Handley 211b4315306SDan Handley /* 212b4315306SDan Handley * Restore SCR to the original value, synchronisation of scr_el3 is 213b4315306SDan Handley * done by eret while el3_exit to save some execution cycles. 214b4315306SDan Handley */ 215b4315306SDan Handley write_scr_el3(scr); 216b4315306SDan Handley } 217b4315306SDan Handley 218b4315306SDan Handley /******************************************************************************* 219c1bb8a05SSoby Mathew * Handler called to return the 'req_state' for system suspend. 220c1bb8a05SSoby Mathew ******************************************************************************/ 221c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state) 222c1bb8a05SSoby Mathew { 223c1bb8a05SSoby Mathew unsigned int i; 224c1bb8a05SSoby Mathew 225c1bb8a05SSoby Mathew /* 226c1bb8a05SSoby Mathew * System Suspend is supported only if the system power domain node 227c1bb8a05SSoby Mathew * is implemented. 228c1bb8a05SSoby Mathew */ 229abd2aba9SSoby Mathew assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL); 230c1bb8a05SSoby Mathew 231c1bb8a05SSoby Mathew for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++) 232c1bb8a05SSoby Mathew req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF; 233c1bb8a05SSoby Mathew } 234c1bb8a05SSoby Mathew 235c1bb8a05SSoby Mathew /******************************************************************************* 2363cc17aaeSJeenu Viswambharan * Handler to query CPU/cluster power states from SCP 2373cc17aaeSJeenu Viswambharan ******************************************************************************/ 2383cc17aaeSJeenu Viswambharan int css_node_hw_state(u_register_t mpidr, unsigned int power_level) 2393cc17aaeSJeenu Viswambharan { 240b12a2b49SSoby Mathew return css_scp_get_power_state(mpidr, power_level); 2413cc17aaeSJeenu Viswambharan } 2423cc17aaeSJeenu Viswambharan 243abd2aba9SSoby Mathew /* 244abd2aba9SSoby Mathew * The system power domain suspend is only supported only via 245abd2aba9SSoby Mathew * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain 246abd2aba9SSoby Mathew * will be downgraded to the lower level. 247abd2aba9SSoby Mathew */ 248abd2aba9SSoby Mathew static int css_validate_power_state(unsigned int power_state, 249abd2aba9SSoby Mathew psci_power_state_t *req_state) 250abd2aba9SSoby Mathew { 251abd2aba9SSoby Mathew int rc; 252abd2aba9SSoby Mathew rc = arm_validate_power_state(power_state, req_state); 253abd2aba9SSoby Mathew 254abd2aba9SSoby Mathew /* 255abd2aba9SSoby Mathew * Ensure that the system power domain level is never suspended 256abd2aba9SSoby Mathew * via PSCI CPU SUSPEND API. Currently system suspend is only 257abd2aba9SSoby Mathew * supported via PSCI SYSTEM SUSPEND API. 258abd2aba9SSoby Mathew */ 259abd2aba9SSoby Mathew req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] = ARM_LOCAL_STATE_RUN; 260abd2aba9SSoby Mathew return rc; 261abd2aba9SSoby Mathew } 262abd2aba9SSoby Mathew 263abd2aba9SSoby Mathew /* 264abd2aba9SSoby Mathew * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the 265abd2aba9SSoby Mathew * `css_validate_power_state`, we do not downgrade the system power 266abd2aba9SSoby Mathew * domain level request in `power_state` as it will be used to query the 267abd2aba9SSoby Mathew * PSCI_STAT_COUNT/RESIDENCY at the system power domain level. 268abd2aba9SSoby Mathew */ 269abd2aba9SSoby Mathew static int css_translate_power_state_by_mpidr(u_register_t mpidr, 270abd2aba9SSoby Mathew unsigned int power_state, 271abd2aba9SSoby Mathew psci_power_state_t *output_state) 272abd2aba9SSoby Mathew { 273abd2aba9SSoby Mathew return arm_validate_power_state(power_state, output_state); 274abd2aba9SSoby Mathew } 275abd2aba9SSoby Mathew 2763cc17aaeSJeenu Viswambharan /******************************************************************************* 277785fb92bSSoby Mathew * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard 278785fb92bSSoby Mathew * platform will take care of registering the handlers with PSCI. 279b4315306SDan Handley ******************************************************************************/ 2805486a965SSoby Mathew plat_psci_ops_t plat_arm_psci_pm_ops = { 28138dce70fSSoby Mathew .pwr_domain_on = css_pwr_domain_on, 28238dce70fSSoby Mathew .pwr_domain_on_finish = css_pwr_domain_on_finish, 28338dce70fSSoby Mathew .pwr_domain_off = css_pwr_domain_off, 28438dce70fSSoby Mathew .cpu_standby = css_cpu_standby, 28538dce70fSSoby Mathew .pwr_domain_suspend = css_pwr_domain_suspend, 28638dce70fSSoby Mathew .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish, 287b4315306SDan Handley .system_off = css_system_off, 288b4315306SDan Handley .system_reset = css_system_reset, 289abd2aba9SSoby Mathew .validate_power_state = css_validate_power_state, 2903cc17aaeSJeenu Viswambharan .validate_ns_entrypoint = arm_validate_ns_entrypoint, 291abd2aba9SSoby Mathew .translate_power_state_by_mpidr = css_translate_power_state_by_mpidr, 292abd2aba9SSoby Mathew .get_node_hw_state = css_node_hw_state, 293f145403cSRoberto Vargas .get_sys_suspend_power_state = css_get_sys_suspend_power_state, 294f145403cSRoberto Vargas /* 295f145403cSRoberto Vargas * mem_protect is not supported in RESET_TO_BL31 and RESET_TO_SP_MIN, 296f145403cSRoberto Vargas * as that would require mapping in all of NS DRAM into BL31 or BL32. 297f145403cSRoberto Vargas */ 298f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR) && !RESET_TO_BL31 && !RESET_TO_SP_MIN 299f145403cSRoberto Vargas .mem_protect_chk = arm_psci_mem_protect_chk, 300f145403cSRoberto Vargas .read_mem_protect = arm_psci_read_mem_protect, 301f145403cSRoberto Vargas .write_mem_protect = arm_nor_psci_write_mem_protect, 302f145403cSRoberto Vargas #endif 303*b48ae263SRoberto Vargas #if CSS_USE_SCMI_SDS_DRIVER 304*b48ae263SRoberto Vargas .system_reset2 = css_system_reset2, 305*b48ae263SRoberto Vargas #endif 306b4315306SDan Handley }; 307