1b4315306SDan Handley /* 2b4315306SDan Handley * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3b4315306SDan Handley * 4b4315306SDan Handley * Redistribution and use in source and binary forms, with or without 5b4315306SDan Handley * modification, are permitted provided that the following conditions are met: 6b4315306SDan Handley * 7b4315306SDan Handley * Redistributions of source code must retain the above copyright notice, this 8b4315306SDan Handley * list of conditions and the following disclaimer. 9b4315306SDan Handley * 10b4315306SDan Handley * Redistributions in binary form must reproduce the above copyright notice, 11b4315306SDan Handley * this list of conditions and the following disclaimer in the documentation 12b4315306SDan Handley * and/or other materials provided with the distribution. 13b4315306SDan Handley * 14b4315306SDan Handley * Neither the name of ARM nor the names of its contributors may be used 15b4315306SDan Handley * to endorse or promote products derived from this software without specific 16b4315306SDan Handley * prior written permission. 17b4315306SDan Handley * 18b4315306SDan Handley * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19b4315306SDan Handley * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20b4315306SDan Handley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21b4315306SDan Handley * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22b4315306SDan Handley * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23b4315306SDan Handley * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24b4315306SDan Handley * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25b4315306SDan Handley * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26b4315306SDan Handley * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27b4315306SDan Handley * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28b4315306SDan Handley * POSSIBILITY OF SUCH DAMAGE. 29b4315306SDan Handley */ 30b4315306SDan Handley 31b4315306SDan Handley #include <arch_helpers.h> 32785fb92bSSoby Mathew #include <assert.h> 33b4315306SDan Handley #include <arm_gic.h> 34c1bb8a05SSoby Mathew #include <cassert.h> 35b4315306SDan Handley #include <cci.h> 36785fb92bSSoby Mathew #include <css_pm.h> 37b4315306SDan Handley #include <debug.h> 38b4315306SDan Handley #include <errno.h> 39b4315306SDan Handley #include <plat_arm.h> 40b4315306SDan Handley #include <platform.h> 41b4315306SDan Handley #include <platform_def.h> 42b4315306SDan Handley #include "css_scpi.h" 43b4315306SDan Handley 44*f14d1886SSoby Mathew /* Macros to read the CSS power domain state */ 45*f14d1886SSoby Mathew #define CSS_CORE_PWR_STATE(state) (state)->pwr_domain_state[ARM_PWR_LVL0] 46*f14d1886SSoby Mathew #define CSS_CLUSTER_PWR_STATE(state) (state)->pwr_domain_state[ARM_PWR_LVL1] 47*f14d1886SSoby Mathew #define CSS_SYSTEM_PWR_STATE(state) ((PLAT_MAX_PWR_LVL > ARM_PWR_LVL1) ?\ 48*f14d1886SSoby Mathew (state)->pwr_domain_state[ARM_PWR_LVL2] : 0) 49*f14d1886SSoby Mathew 50785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */ 51785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops 5238dce70fSSoby Mathew 532204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC 542204afdeSSoby Mathew /* 552204afdeSSoby Mathew * The table storing the valid idle power states. Ensure that the 562204afdeSSoby Mathew * array entries are populated in ascending order of state-id to 572204afdeSSoby Mathew * enable us to use binary search during power state validation. 582204afdeSSoby Mathew * The table must be terminated by a NULL entry. 592204afdeSSoby Mathew */ 602204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = { 615f3a6030SSoby Mathew /* State-id - 0x001 */ 625f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN, 635f3a6030SSoby Mathew ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY), 645f3a6030SSoby Mathew /* State-id - 0x002 */ 655f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN, 665f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN), 675f3a6030SSoby Mathew /* State-id - 0x022 */ 685f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF, 695f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN), 705f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1 715f3a6030SSoby Mathew /* State-id - 0x222 */ 725f3a6030SSoby Mathew arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF, 735f3a6030SSoby Mathew ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN), 745f3a6030SSoby Mathew #endif 752204afdeSSoby Mathew 0, 762204afdeSSoby Mathew }; 775f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */ 782204afdeSSoby Mathew 79c1bb8a05SSoby Mathew /* 80c1bb8a05SSoby Mathew * All the power management helpers in this file assume at least cluster power 81c1bb8a05SSoby Mathew * level is supported. 82c1bb8a05SSoby Mathew */ 83c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1, 84c1bb8a05SSoby Mathew assert_max_pwr_lvl_supported_mismatch); 85c1bb8a05SSoby Mathew 86b4315306SDan Handley /******************************************************************************* 8738dce70fSSoby Mathew * Handler called when a power domain is about to be turned on. The 88b4315306SDan Handley * level and mpidr determine the affinity instance. 89b4315306SDan Handley ******************************************************************************/ 9038dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr) 91b4315306SDan Handley { 92b4315306SDan Handley /* 9338dce70fSSoby Mathew * SCP takes care of powering up parent power domains so we 94b4315306SDan Handley * only need to care about level 0 95b4315306SDan Handley */ 96b4315306SDan Handley scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on, 97b4315306SDan Handley scpi_power_on); 98b4315306SDan Handley 99b4315306SDan Handley return PSCI_E_SUCCESS; 100b4315306SDan Handley } 101b4315306SDan Handley 102*f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common( 103*f14d1886SSoby Mathew const psci_power_state_t *target_state) 104b4315306SDan Handley { 105*f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 106c1bb8a05SSoby Mathew 107b4315306SDan Handley /* 108b4315306SDan Handley * Perform the common cluster specific operations i.e enable coherency 109b4315306SDan Handley * if this cluster was off. 110b4315306SDan Handley */ 111*f14d1886SSoby Mathew if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 11238dce70fSSoby Mathew cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1())); 113c1bb8a05SSoby Mathew } 114c1bb8a05SSoby Mathew 115*f14d1886SSoby Mathew /******************************************************************************* 116*f14d1886SSoby Mathew * Handler called when a power level has just been powered on after 117*f14d1886SSoby Mathew * being turned off earlier. The target_state encodes the low power state that 118*f14d1886SSoby Mathew * each level has woken up from. This handler would never be invoked with 119*f14d1886SSoby Mathew * the system power domain uninitialized as either the primary would have taken 120*f14d1886SSoby Mathew * care of it as part of cold boot or the first core awakened from system 121*f14d1886SSoby Mathew * suspend would have already initialized it. 122*f14d1886SSoby Mathew ******************************************************************************/ 123*f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state) 124*f14d1886SSoby Mathew { 125*f14d1886SSoby Mathew /* Assert that the system power domain need not be initialized */ 126*f14d1886SSoby Mathew assert(CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_RUN); 127*f14d1886SSoby Mathew 128*f14d1886SSoby Mathew css_pwr_domain_on_finisher_common(target_state); 129*f14d1886SSoby Mathew 130b4315306SDan Handley /* Enable the gic cpu interface */ 131b4315306SDan Handley arm_gic_cpuif_setup(); 132*f14d1886SSoby Mathew /* Program the gic per-cpu distributor interface */ 133b4315306SDan Handley arm_gic_pcpu_distif_setup(); 134b4315306SDan Handley } 135b4315306SDan Handley 136b4315306SDan Handley /******************************************************************************* 137b4315306SDan Handley * Common function called while turning a cpu off or suspending it. It is called 138b4315306SDan Handley * from css_off() or css_suspend() when these functions in turn are called for 13938dce70fSSoby Mathew * power domain at the highest power level which will be powered down. It 14038dce70fSSoby Mathew * performs the actions common to the OFF and SUSPEND calls. 141b4315306SDan Handley ******************************************************************************/ 14238dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state) 143b4315306SDan Handley { 144b4315306SDan Handley uint32_t cluster_state = scpi_power_on; 145c1bb8a05SSoby Mathew uint32_t system_state = scpi_power_on; 146b4315306SDan Handley 147b4315306SDan Handley /* Prevent interrupts from spuriously waking up this cpu */ 148b4315306SDan Handley arm_gic_cpuif_deactivate(); 149b4315306SDan Handley 150*f14d1886SSoby Mathew /* Check if power down at system power domain level is requested */ 151*f14d1886SSoby Mathew if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 152c1bb8a05SSoby Mathew system_state = scpi_power_retention; 153c1bb8a05SSoby Mathew 154b4315306SDan Handley /* Cluster is to be turned off, so disable coherency */ 155*f14d1886SSoby Mathew if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) { 156b4315306SDan Handley cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr())); 157b4315306SDan Handley cluster_state = scpi_power_off; 158b4315306SDan Handley } 159b4315306SDan Handley 160b4315306SDan Handley /* 161b4315306SDan Handley * Ask the SCP to power down the appropriate components depending upon 162b4315306SDan Handley * their state. 163b4315306SDan Handley */ 164b4315306SDan Handley scpi_set_css_power_state(read_mpidr_el1(), 165b4315306SDan Handley scpi_power_off, 166b4315306SDan Handley cluster_state, 167c1bb8a05SSoby Mathew system_state); 168b4315306SDan Handley } 169b4315306SDan Handley 170b4315306SDan Handley /******************************************************************************* 17138dce70fSSoby Mathew * Handler called when a power domain is about to be turned off. The 17238dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 173b4315306SDan Handley ******************************************************************************/ 174785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state) 175b4315306SDan Handley { 176*f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 17738dce70fSSoby Mathew css_power_down_common(target_state); 178b4315306SDan Handley } 179b4315306SDan Handley 180b4315306SDan Handley /******************************************************************************* 18138dce70fSSoby Mathew * Handler called when a power domain is about to be suspended. The 18238dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 183b4315306SDan Handley ******************************************************************************/ 184785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state) 185b4315306SDan Handley { 18638dce70fSSoby Mathew /* 187*f14d1886SSoby Mathew * CSS currently supports retention only at cpu level. Just return 18838dce70fSSoby Mathew * as nothing is to be done for retention. 18938dce70fSSoby Mathew */ 190*f14d1886SSoby Mathew if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET) 191b4315306SDan Handley return; 192b4315306SDan Handley 193*f14d1886SSoby Mathew assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF); 19438dce70fSSoby Mathew css_power_down_common(target_state); 195b4315306SDan Handley } 196b4315306SDan Handley 197b4315306SDan Handley /******************************************************************************* 19838dce70fSSoby Mathew * Handler called when a power domain has just been powered on after 19938dce70fSSoby Mathew * having been suspended earlier. The target_state encodes the low power state 20038dce70fSSoby Mathew * that each level has woken up from. 201b4315306SDan Handley * TODO: At the moment we reuse the on finisher and reinitialize the secure 202b4315306SDan Handley * context. Need to implement a separate suspend finisher. 203b4315306SDan Handley ******************************************************************************/ 204785fb92bSSoby Mathew void css_pwr_domain_suspend_finish( 20538dce70fSSoby Mathew const psci_power_state_t *target_state) 206b4315306SDan Handley { 207*f14d1886SSoby Mathew /* Return as nothing is to be done on waking up from retention. */ 208*f14d1886SSoby Mathew if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET) 20938dce70fSSoby Mathew return; 21038dce70fSSoby Mathew 211*f14d1886SSoby Mathew /* Perform system domain restore if woken up from system suspend */ 212*f14d1886SSoby Mathew if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) 213*f14d1886SSoby Mathew arm_system_pwr_domain_resume(); 214*f14d1886SSoby Mathew else 215*f14d1886SSoby Mathew /* Enable the gic cpu interface */ 216*f14d1886SSoby Mathew arm_gic_cpuif_setup(); 217*f14d1886SSoby Mathew 218*f14d1886SSoby Mathew css_pwr_domain_on_finisher_common(target_state); 219b4315306SDan Handley } 220b4315306SDan Handley 221b4315306SDan Handley /******************************************************************************* 222b4315306SDan Handley * Handlers to shutdown/reboot the system 223b4315306SDan Handley ******************************************************************************/ 224785fb92bSSoby Mathew void __dead2 css_system_off(void) 225b4315306SDan Handley { 226b4315306SDan Handley uint32_t response; 227b4315306SDan Handley 228b4315306SDan Handley /* Send the power down request to the SCP */ 229b4315306SDan Handley response = scpi_sys_power_state(scpi_system_shutdown); 230b4315306SDan Handley 231b4315306SDan Handley if (response != SCP_OK) { 232b4315306SDan Handley ERROR("CSS System Off: SCP error %u.\n", response); 233b4315306SDan Handley panic(); 234b4315306SDan Handley } 235b4315306SDan Handley wfi(); 236b4315306SDan Handley ERROR("CSS System Off: operation not handled.\n"); 237b4315306SDan Handley panic(); 238b4315306SDan Handley } 239b4315306SDan Handley 240785fb92bSSoby Mathew void __dead2 css_system_reset(void) 241b4315306SDan Handley { 242b4315306SDan Handley uint32_t response; 243b4315306SDan Handley 244b4315306SDan Handley /* Send the system reset request to the SCP */ 245b4315306SDan Handley response = scpi_sys_power_state(scpi_system_reboot); 246b4315306SDan Handley 247b4315306SDan Handley if (response != SCP_OK) { 248b4315306SDan Handley ERROR("CSS System Reset: SCP error %u.\n", response); 249b4315306SDan Handley panic(); 250b4315306SDan Handley } 251b4315306SDan Handley wfi(); 252b4315306SDan Handley ERROR("CSS System Reset: operation not handled.\n"); 253b4315306SDan Handley panic(); 254b4315306SDan Handley } 255b4315306SDan Handley 256b4315306SDan Handley /******************************************************************************* 25738dce70fSSoby Mathew * Handler called when the CPU power domain is about to enter standby. 258b4315306SDan Handley ******************************************************************************/ 25938dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state) 260b4315306SDan Handley { 261b4315306SDan Handley unsigned int scr; 262b4315306SDan Handley 26338dce70fSSoby Mathew assert(cpu_state == ARM_LOCAL_STATE_RET); 26438dce70fSSoby Mathew 265b4315306SDan Handley scr = read_scr_el3(); 266b4315306SDan Handley /* Enable PhysicalIRQ bit for NS world to wake the CPU */ 267b4315306SDan Handley write_scr_el3(scr | SCR_IRQ_BIT); 268b4315306SDan Handley isb(); 269b4315306SDan Handley dsb(); 270b4315306SDan Handley wfi(); 271b4315306SDan Handley 272b4315306SDan Handley /* 273b4315306SDan Handley * Restore SCR to the original value, synchronisation of scr_el3 is 274b4315306SDan Handley * done by eret while el3_exit to save some execution cycles. 275b4315306SDan Handley */ 276b4315306SDan Handley write_scr_el3(scr); 277b4315306SDan Handley } 278b4315306SDan Handley 279b4315306SDan Handley /******************************************************************************* 280c1bb8a05SSoby Mathew * Handler called to return the 'req_state' for system suspend. 281c1bb8a05SSoby Mathew ******************************************************************************/ 282c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state) 283c1bb8a05SSoby Mathew { 284c1bb8a05SSoby Mathew unsigned int i; 285c1bb8a05SSoby Mathew 286c1bb8a05SSoby Mathew /* 287c1bb8a05SSoby Mathew * System Suspend is supported only if the system power domain node 288c1bb8a05SSoby Mathew * is implemented. 289c1bb8a05SSoby Mathew */ 290c1bb8a05SSoby Mathew assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2); 291c1bb8a05SSoby Mathew 292c1bb8a05SSoby Mathew for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++) 293c1bb8a05SSoby Mathew req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF; 294c1bb8a05SSoby Mathew } 295c1bb8a05SSoby Mathew 296c1bb8a05SSoby Mathew /******************************************************************************* 297785fb92bSSoby Mathew * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard 298785fb92bSSoby Mathew * platform will take care of registering the handlers with PSCI. 299b4315306SDan Handley ******************************************************************************/ 300785fb92bSSoby Mathew const plat_psci_ops_t plat_arm_psci_pm_ops = { 30138dce70fSSoby Mathew .pwr_domain_on = css_pwr_domain_on, 30238dce70fSSoby Mathew .pwr_domain_on_finish = css_pwr_domain_on_finish, 30338dce70fSSoby Mathew .pwr_domain_off = css_pwr_domain_off, 30438dce70fSSoby Mathew .cpu_standby = css_cpu_standby, 30538dce70fSSoby Mathew .pwr_domain_suspend = css_pwr_domain_suspend, 30638dce70fSSoby Mathew .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish, 307b4315306SDan Handley .system_off = css_system_off, 308b4315306SDan Handley .system_reset = css_system_reset, 309f9e858b1SSoby Mathew .validate_power_state = arm_validate_power_state, 310f9e858b1SSoby Mathew .validate_ns_entrypoint = arm_validate_ns_entrypoint 311b4315306SDan Handley }; 312