1b4315306SDan Handley /* 2638b034cSRoberto Vargas * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 3b4315306SDan Handley * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5b4315306SDan Handley */ 6b4315306SDan Handley 7785fb92bSSoby Mathew #include <assert.h> 8b4315306SDan Handley #include <errno.h> 9*09d40e0eSAntonio Nino Diaz 10b4315306SDan Handley #include <platform_def.h> 11*09d40e0eSAntonio Nino Diaz 12*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 13*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 14*09d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 15*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 16*09d40e0eSAntonio Nino Diaz 17*09d40e0eSAntonio Nino Diaz #include <css_pm.h> 18*09d40e0eSAntonio Nino Diaz #include <plat_arm.h> 19*09d40e0eSAntonio Nino Diaz 20b12a2b49SSoby Mathew #include "../drivers/scp/css_scp.h" 21f14d1886SSoby Mathew 22785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */ 23785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops 2438dce70fSSoby Mathew 252204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC 262204afdeSSoby Mathew /* 272204afdeSSoby Mathew * The table storing the valid idle power states. Ensure that the 282204afdeSSoby Mathew * array entries are populated in ascending order of state-id to 292204afdeSSoby Mathew * enable us to use binary search during power state validation. 302204afdeSSoby Mathew * The table must be terminated by a NULL entry. 312204afdeSSoby Mathew */ 322204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = { 335f3a6030SSoby Mathew /* State-id - 0x001 */ 345f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN, 355f3a6030SSoby Mathew ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY), 365f3a6030SSoby Mathew /* State-id - 0x002 */ 375f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN, 385f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN), 395f3a6030SSoby Mathew /* State-id - 0x022 */ 405f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF, 415f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN), 425f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1 435f3a6030SSoby Mathew /* State-id - 0x222 */ 445f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF, 455f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN), 465f3a6030SSoby Mathew #endif 472204afdeSSoby Mathew 0, 482204afdeSSoby Mathew }; 495f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */ 502204afdeSSoby Mathew 51c1bb8a05SSoby Mathew /* 52c1bb8a05SSoby Mathew * All the power management helpers in this file assume at least cluster power 53c1bb8a05SSoby Mathew * level is supported. 54c1bb8a05SSoby Mathew */ 55c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1, 56c1bb8a05SSoby Mathew assert_max_pwr_lvl_supported_mismatch); 57c1bb8a05SSoby Mathew 58abd2aba9SSoby Mathew /* 59abd2aba9SSoby Mathew * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL 60abd2aba9SSoby Mathew * assumed by the CSS layer. 61abd2aba9SSoby Mathew */ 62abd2aba9SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL, 63abd2aba9SSoby Mathew assert_max_pwr_lvl_higher_than_css_sys_lvl); 64abd2aba9SSoby Mathew 65b4315306SDan Handley /******************************************************************************* 6638dce70fSSoby Mathew * Handler called when a power domain is about to be turned on. The 67b4315306SDan Handley * level and mpidr determine the affinity instance. 68b4315306SDan Handley ******************************************************************************/ 6938dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr) 70b4315306SDan Handley { 71b12a2b49SSoby Mathew css_scp_on(mpidr); 72b4315306SDan Handley 73b4315306SDan Handley return PSCI_E_SUCCESS; 74b4315306SDan Handley } 75b4315306SDan Handley 76f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common( 77f14d1886SSoby Mathew const psci_power_state_t *target_state) 78b4315306SDan Handley { 79f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 80c1bb8a05SSoby Mathew 81e35a3fb5SSoby Mathew /* Enable the gic cpu interface */ 82e35a3fb5SSoby Mathew plat_arm_gic_cpuif_enable(); 83e35a3fb5SSoby Mathew 84b4315306SDan Handley /* 85b4315306SDan Handley * Perform the common cluster specific operations i.e enable coherency 86b4315306SDan Handley * if this cluster was off. 87b4315306SDan Handley */ 88f14d1886SSoby Mathew if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 896355f234SVikram Kanigiri plat_arm_interconnect_enter_coherency(); 90c1bb8a05SSoby Mathew } 91c1bb8a05SSoby Mathew 92f14d1886SSoby Mathew /******************************************************************************* 93f14d1886SSoby Mathew * Handler called when a power level has just been powered on after 94f14d1886SSoby Mathew * being turned off earlier. The target_state encodes the low power state that 95f14d1886SSoby Mathew * each level has woken up from. This handler would never be invoked with 96f14d1886SSoby Mathew * the system power domain uninitialized as either the primary would have taken 97f14d1886SSoby Mathew * care of it as part of cold boot or the first core awakened from system 98f14d1886SSoby Mathew * suspend would have already initialized it. 99f14d1886SSoby Mathew ******************************************************************************/ 100f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state) 101f14d1886SSoby Mathew { 102f14d1886SSoby Mathew /* Assert that the system power domain need not be initialized */ 1039b4c611cSNariman Poushin assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN); 104f14d1886SSoby Mathew 10527573c59SAchin Gupta /* Program the gic per-cpu distributor or re-distributor interface */ 10627573c59SAchin Gupta plat_arm_gic_pcpu_init(); 10727573c59SAchin Gupta 108e35a3fb5SSoby Mathew css_pwr_domain_on_finisher_common(target_state); 109b4315306SDan Handley } 110b4315306SDan Handley 111b4315306SDan Handley /******************************************************************************* 112b4315306SDan Handley * Common function called while turning a cpu off or suspending it. It is called 113b4315306SDan Handley * from css_off() or css_suspend() when these functions in turn are called for 11438dce70fSSoby Mathew * power domain at the highest power level which will be powered down. It 11538dce70fSSoby Mathew * performs the actions common to the OFF and SUSPEND calls. 116b4315306SDan Handley ******************************************************************************/ 11738dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state) 118b4315306SDan Handley { 119b4315306SDan Handley /* Prevent interrupts from spuriously waking up this cpu */ 12027573c59SAchin Gupta plat_arm_gic_cpuif_disable(); 121b4315306SDan Handley 122b4315306SDan Handley /* Cluster is to be turned off, so disable coherency */ 123b12a2b49SSoby Mathew if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 1246355f234SVikram Kanigiri plat_arm_interconnect_exit_coherency(); 125b4315306SDan Handley } 126b4315306SDan Handley 127b4315306SDan Handley /******************************************************************************* 12838dce70fSSoby Mathew * Handler called when a power domain is about to be turned off. The 12938dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 130b4315306SDan Handley ******************************************************************************/ 131785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state) 132b4315306SDan Handley { 133f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 13438dce70fSSoby Mathew css_power_down_common(target_state); 135b12a2b49SSoby Mathew css_scp_off(target_state); 136b4315306SDan Handley } 137b4315306SDan Handley 138b4315306SDan Handley /******************************************************************************* 13938dce70fSSoby Mathew * Handler called when a power domain is about to be suspended. The 14038dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 141b4315306SDan Handley ******************************************************************************/ 142785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state) 143b4315306SDan Handley { 14438dce70fSSoby Mathew /* 145f14d1886SSoby Mathew * CSS currently supports retention only at cpu level. Just return 14638dce70fSSoby Mathew * as nothing is to be done for retention. 14738dce70fSSoby Mathew */ 148f14d1886SSoby Mathew if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET) 149b4315306SDan Handley return; 150b4315306SDan Handley 151e35a3fb5SSoby Mathew 152f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 15338dce70fSSoby Mathew css_power_down_common(target_state); 154e35a3fb5SSoby Mathew 155e35a3fb5SSoby Mathew /* Perform system domain state saving if issuing system suspend */ 1569b4c611cSNariman Poushin if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) { 157e35a3fb5SSoby Mathew arm_system_pwr_domain_save(); 158e35a3fb5SSoby Mathew 159e35a3fb5SSoby Mathew /* Power off the Redistributor after having saved its context */ 160e35a3fb5SSoby Mathew plat_arm_gic_redistif_off(); 161e35a3fb5SSoby Mathew } 162e35a3fb5SSoby Mathew 163b12a2b49SSoby Mathew css_scp_suspend(target_state); 164b4315306SDan Handley } 165b4315306SDan Handley 166b4315306SDan Handley /******************************************************************************* 16738dce70fSSoby Mathew * Handler called when a power domain has just been powered on after 16838dce70fSSoby Mathew * having been suspended earlier. The target_state encodes the low power state 16938dce70fSSoby Mathew * that each level has woken up from. 170b4315306SDan Handley * TODO: At the moment we reuse the on finisher and reinitialize the secure 171b4315306SDan Handley * context. Need to implement a separate suspend finisher. 172b4315306SDan Handley ******************************************************************************/ 173785fb92bSSoby Mathew void css_pwr_domain_suspend_finish( 17438dce70fSSoby Mathew const psci_power_state_t *target_state) 175b4315306SDan Handley { 176f14d1886SSoby Mathew /* Return as nothing is to be done on waking up from retention. */ 177f14d1886SSoby Mathew if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET) 17838dce70fSSoby Mathew return; 17938dce70fSSoby Mathew 180f14d1886SSoby Mathew /* Perform system domain restore if woken up from system suspend */ 1819b4c611cSNariman Poushin if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) 182e35a3fb5SSoby Mathew /* 183e35a3fb5SSoby Mathew * At this point, the Distributor must be powered on to be ready 184e35a3fb5SSoby Mathew * to have its state restored. The Redistributor will be powered 185e35a3fb5SSoby Mathew * on as part of gicv3_rdistif_init_restore. 186e35a3fb5SSoby Mathew */ 187f14d1886SSoby Mathew arm_system_pwr_domain_resume(); 188f14d1886SSoby Mathew 189f14d1886SSoby Mathew css_pwr_domain_on_finisher_common(target_state); 190b4315306SDan Handley } 191b4315306SDan Handley 192b4315306SDan Handley /******************************************************************************* 193b4315306SDan Handley * Handlers to shutdown/reboot the system 194b4315306SDan Handley ******************************************************************************/ 195785fb92bSSoby Mathew void __dead2 css_system_off(void) 196b4315306SDan Handley { 197b12a2b49SSoby Mathew css_scp_sys_shutdown(); 198b4315306SDan Handley } 199b4315306SDan Handley 200785fb92bSSoby Mathew void __dead2 css_system_reset(void) 201b4315306SDan Handley { 202b12a2b49SSoby Mathew css_scp_sys_reboot(); 203b4315306SDan Handley } 204b4315306SDan Handley 205b4315306SDan Handley /******************************************************************************* 20638dce70fSSoby Mathew * Handler called when the CPU power domain is about to enter standby. 207b4315306SDan Handley ******************************************************************************/ 20838dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state) 209b4315306SDan Handley { 210b4315306SDan Handley unsigned int scr; 211b4315306SDan Handley 21238dce70fSSoby Mathew assert(cpu_state == ARM_LOCAL_STATE_RET); 21338dce70fSSoby Mathew 214b4315306SDan Handley scr = read_scr_el3(); 21568b105aeSDavid Wang /* 21668b105aeSDavid Wang * Enable the Non secure interrupt to wake the CPU. 21768b105aeSDavid Wang * In GICv3 affinity routing mode, the non secure group1 interrupts use 21868b105aeSDavid Wang * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ. 21968b105aeSDavid Wang * Enabling both the bits works for both GICv2 mode and GICv3 affinity 22068b105aeSDavid Wang * routing mode. 22168b105aeSDavid Wang */ 22268b105aeSDavid Wang write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT); 223b4315306SDan Handley isb(); 224b4315306SDan Handley dsb(); 225b4315306SDan Handley wfi(); 226b4315306SDan Handley 227b4315306SDan Handley /* 228b4315306SDan Handley * Restore SCR to the original value, synchronisation of scr_el3 is 229b4315306SDan Handley * done by eret while el3_exit to save some execution cycles. 230b4315306SDan Handley */ 231b4315306SDan Handley write_scr_el3(scr); 232b4315306SDan Handley } 233b4315306SDan Handley 234b4315306SDan Handley /******************************************************************************* 235c1bb8a05SSoby Mathew * Handler called to return the 'req_state' for system suspend. 236c1bb8a05SSoby Mathew ******************************************************************************/ 237c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state) 238c1bb8a05SSoby Mathew { 239c1bb8a05SSoby Mathew unsigned int i; 240c1bb8a05SSoby Mathew 241c1bb8a05SSoby Mathew /* 242c1bb8a05SSoby Mathew * System Suspend is supported only if the system power domain node 243c1bb8a05SSoby Mathew * is implemented. 244c1bb8a05SSoby Mathew */ 245abd2aba9SSoby Mathew assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL); 246c1bb8a05SSoby Mathew 247c1bb8a05SSoby Mathew for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++) 248c1bb8a05SSoby Mathew req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF; 249c1bb8a05SSoby Mathew } 250c1bb8a05SSoby Mathew 251c1bb8a05SSoby Mathew /******************************************************************************* 2523cc17aaeSJeenu Viswambharan * Handler to query CPU/cluster power states from SCP 2533cc17aaeSJeenu Viswambharan ******************************************************************************/ 2543cc17aaeSJeenu Viswambharan int css_node_hw_state(u_register_t mpidr, unsigned int power_level) 2553cc17aaeSJeenu Viswambharan { 256b12a2b49SSoby Mathew return css_scp_get_power_state(mpidr, power_level); 2573cc17aaeSJeenu Viswambharan } 2583cc17aaeSJeenu Viswambharan 259abd2aba9SSoby Mathew /* 260abd2aba9SSoby Mathew * The system power domain suspend is only supported only via 261abd2aba9SSoby Mathew * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain 262abd2aba9SSoby Mathew * will be downgraded to the lower level. 263abd2aba9SSoby Mathew */ 264abd2aba9SSoby Mathew static int css_validate_power_state(unsigned int power_state, 265abd2aba9SSoby Mathew psci_power_state_t *req_state) 266abd2aba9SSoby Mathew { 267abd2aba9SSoby Mathew int rc; 268abd2aba9SSoby Mathew rc = arm_validate_power_state(power_state, req_state); 269abd2aba9SSoby Mathew 270abd2aba9SSoby Mathew /* 2718e26307dSNariman Poushin * Ensure that we don't overrun the pwr_domain_state array in the case 2728e26307dSNariman Poushin * where the platform supported max power level is less than the system 2738e26307dSNariman Poushin * power level 2748e26307dSNariman Poushin */ 2758e26307dSNariman Poushin 2768e26307dSNariman Poushin #if (PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL) 2778e26307dSNariman Poushin 2788e26307dSNariman Poushin /* 279abd2aba9SSoby Mathew * Ensure that the system power domain level is never suspended 280abd2aba9SSoby Mathew * via PSCI CPU SUSPEND API. Currently system suspend is only 281abd2aba9SSoby Mathew * supported via PSCI SYSTEM SUSPEND API. 282abd2aba9SSoby Mathew */ 2838e26307dSNariman Poushin 2848e26307dSNariman Poushin req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] = 2858e26307dSNariman Poushin ARM_LOCAL_STATE_RUN; 2868e26307dSNariman Poushin #endif 2878e26307dSNariman Poushin 288abd2aba9SSoby Mathew return rc; 289abd2aba9SSoby Mathew } 290abd2aba9SSoby Mathew 291abd2aba9SSoby Mathew /* 292abd2aba9SSoby Mathew * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the 293abd2aba9SSoby Mathew * `css_validate_power_state`, we do not downgrade the system power 294abd2aba9SSoby Mathew * domain level request in `power_state` as it will be used to query the 295abd2aba9SSoby Mathew * PSCI_STAT_COUNT/RESIDENCY at the system power domain level. 296abd2aba9SSoby Mathew */ 297abd2aba9SSoby Mathew static int css_translate_power_state_by_mpidr(u_register_t mpidr, 298abd2aba9SSoby Mathew unsigned int power_state, 299abd2aba9SSoby Mathew psci_power_state_t *output_state) 300abd2aba9SSoby Mathew { 301abd2aba9SSoby Mathew return arm_validate_power_state(power_state, output_state); 302abd2aba9SSoby Mathew } 303abd2aba9SSoby Mathew 3043cc17aaeSJeenu Viswambharan /******************************************************************************* 305785fb92bSSoby Mathew * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard 306785fb92bSSoby Mathew * platform will take care of registering the handlers with PSCI. 307b4315306SDan Handley ******************************************************************************/ 3085486a965SSoby Mathew plat_psci_ops_t plat_arm_psci_pm_ops = { 30938dce70fSSoby Mathew .pwr_domain_on = css_pwr_domain_on, 31038dce70fSSoby Mathew .pwr_domain_on_finish = css_pwr_domain_on_finish, 31138dce70fSSoby Mathew .pwr_domain_off = css_pwr_domain_off, 31238dce70fSSoby Mathew .cpu_standby = css_cpu_standby, 31338dce70fSSoby Mathew .pwr_domain_suspend = css_pwr_domain_suspend, 31438dce70fSSoby Mathew .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish, 315b4315306SDan Handley .system_off = css_system_off, 316b4315306SDan Handley .system_reset = css_system_reset, 317abd2aba9SSoby Mathew .validate_power_state = css_validate_power_state, 31871e7a4e5SJeenu Viswambharan .validate_ns_entrypoint = arm_validate_psci_entrypoint, 319abd2aba9SSoby Mathew .translate_power_state_by_mpidr = css_translate_power_state_by_mpidr, 320abd2aba9SSoby Mathew .get_node_hw_state = css_node_hw_state, 321f145403cSRoberto Vargas .get_sys_suspend_power_state = css_get_sys_suspend_power_state, 322638b034cSRoberto Vargas 323638b034cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR) 324f145403cSRoberto Vargas .mem_protect_chk = arm_psci_mem_protect_chk, 325f145403cSRoberto Vargas .read_mem_protect = arm_psci_read_mem_protect, 326f145403cSRoberto Vargas .write_mem_protect = arm_nor_psci_write_mem_protect, 327f145403cSRoberto Vargas #endif 328b48ae263SRoberto Vargas #if CSS_USE_SCMI_SDS_DRIVER 329b48ae263SRoberto Vargas .system_reset2 = css_system_reset2, 330b48ae263SRoberto Vargas #endif 331b4315306SDan Handley }; 332