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 <assert.h> 32b4315306SDan Handley #include <arch_helpers.h> 33b4315306SDan Handley #include <arm_gic.h> 34b4315306SDan Handley #include <cci.h> 35b4315306SDan Handley #include <css_def.h> 36b4315306SDan Handley #include <debug.h> 37b4315306SDan Handley #include <errno.h> 38b4315306SDan Handley #include <plat_arm.h> 39b4315306SDan Handley #include <platform.h> 40b4315306SDan Handley #include <platform_def.h> 41b4315306SDan Handley #include <psci.h> 42b4315306SDan Handley #include "css_scpi.h" 43b4315306SDan Handley 4438dce70fSSoby Mathew 452204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC 462204afdeSSoby Mathew /* 472204afdeSSoby Mathew * The table storing the valid idle power states. Ensure that the 482204afdeSSoby Mathew * array entries are populated in ascending order of state-id to 492204afdeSSoby Mathew * enable us to use binary search during power state validation. 502204afdeSSoby Mathew * The table must be terminated by a NULL entry. 512204afdeSSoby Mathew */ 522204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = { 532204afdeSSoby Mathew /* State-id - 0x01 */ 542204afdeSSoby Mathew arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET, 552204afdeSSoby Mathew ARM_PWR_LVL0, PSTATE_TYPE_STANDBY), 562204afdeSSoby Mathew /* State-id - 0x02 */ 572204afdeSSoby Mathew arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF, 582204afdeSSoby Mathew ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN), 592204afdeSSoby Mathew /* State-id - 0x22 */ 602204afdeSSoby Mathew arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF, 612204afdeSSoby Mathew ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN), 622204afdeSSoby Mathew 0, 632204afdeSSoby Mathew }; 642204afdeSSoby Mathew #endif 652204afdeSSoby Mathew 66b4315306SDan Handley /******************************************************************************* 67b4315306SDan Handley * Private function to program the mailbox for a cpu before it is released 68b4315306SDan Handley * from reset. 69b4315306SDan Handley ******************************************************************************/ 70804040d1SSandrine Bailleux static void css_program_mailbox(uintptr_t address) 71b4315306SDan Handley { 72804040d1SSandrine Bailleux uintptr_t *mailbox = (void *) TRUSTED_MAILBOX_BASE; 73804040d1SSandrine Bailleux *mailbox = address; 74804040d1SSandrine Bailleux flush_dcache_range((uintptr_t) mailbox, sizeof(*mailbox)); 75b4315306SDan Handley } 76b4315306SDan Handley 77b4315306SDan Handley /******************************************************************************* 7838dce70fSSoby Mathew * Handler called when a power domain is about to be turned on. The 79b4315306SDan Handley * level and mpidr determine the affinity instance. 80b4315306SDan Handley ******************************************************************************/ 8138dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr) 82b4315306SDan Handley { 83b4315306SDan Handley /* 8438dce70fSSoby Mathew * SCP takes care of powering up parent power domains so we 85b4315306SDan Handley * only need to care about level 0 86b4315306SDan Handley */ 87b4315306SDan Handley scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on, 88b4315306SDan Handley scpi_power_on); 89b4315306SDan Handley 90b4315306SDan Handley return PSCI_E_SUCCESS; 91b4315306SDan Handley } 92b4315306SDan Handley 93b4315306SDan Handley /******************************************************************************* 9438dce70fSSoby Mathew * Handler called when a power level has just been powered on after 9538dce70fSSoby Mathew * being turned off earlier. The target_state encodes the low power state that 9638dce70fSSoby Mathew * each level has woken up from. 97b4315306SDan Handley ******************************************************************************/ 9838dce70fSSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state) 99b4315306SDan Handley { 10038dce70fSSoby Mathew assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == 10138dce70fSSoby Mathew ARM_LOCAL_STATE_OFF); 102b4315306SDan Handley 103b4315306SDan Handley /* 104b4315306SDan Handley * Perform the common cluster specific operations i.e enable coherency 105b4315306SDan Handley * if this cluster was off. 106b4315306SDan Handley */ 10738dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL1] == 10838dce70fSSoby Mathew ARM_LOCAL_STATE_OFF) 10938dce70fSSoby Mathew cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1())); 110b4315306SDan Handley 111b4315306SDan Handley /* Enable the gic cpu interface */ 112b4315306SDan Handley arm_gic_cpuif_setup(); 113b4315306SDan Handley 114b4315306SDan Handley /* todo: Is this setup only needed after a cold boot? */ 115b4315306SDan Handley arm_gic_pcpu_distif_setup(); 116b4315306SDan Handley } 117b4315306SDan Handley 118b4315306SDan Handley /******************************************************************************* 119b4315306SDan Handley * Common function called while turning a cpu off or suspending it. It is called 120b4315306SDan Handley * from css_off() or css_suspend() when these functions in turn are called for 12138dce70fSSoby Mathew * power domain at the highest power level which will be powered down. It 12238dce70fSSoby Mathew * performs the actions common to the OFF and SUSPEND calls. 123b4315306SDan Handley ******************************************************************************/ 12438dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state) 125b4315306SDan Handley { 126b4315306SDan Handley uint32_t cluster_state = scpi_power_on; 127b4315306SDan Handley 128b4315306SDan Handley /* Prevent interrupts from spuriously waking up this cpu */ 129b4315306SDan Handley arm_gic_cpuif_deactivate(); 130b4315306SDan Handley 131b4315306SDan Handley /* Cluster is to be turned off, so disable coherency */ 13238dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL1] == 13338dce70fSSoby Mathew ARM_LOCAL_STATE_OFF) { 134b4315306SDan Handley cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr())); 135b4315306SDan Handley cluster_state = scpi_power_off; 136b4315306SDan Handley } 137b4315306SDan Handley 138b4315306SDan Handley /* 139b4315306SDan Handley * Ask the SCP to power down the appropriate components depending upon 140b4315306SDan Handley * their state. 141b4315306SDan Handley */ 142b4315306SDan Handley scpi_set_css_power_state(read_mpidr_el1(), 143b4315306SDan Handley scpi_power_off, 144b4315306SDan Handley cluster_state, 145b4315306SDan Handley scpi_power_on); 146b4315306SDan Handley } 147b4315306SDan Handley 148b4315306SDan Handley /******************************************************************************* 14938dce70fSSoby Mathew * Handler called when a power domain is about to be turned off. The 15038dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 151b4315306SDan Handley ******************************************************************************/ 15238dce70fSSoby Mathew static void css_pwr_domain_off(const psci_power_state_t *target_state) 153b4315306SDan Handley { 15438dce70fSSoby Mathew assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == 15538dce70fSSoby Mathew ARM_LOCAL_STATE_OFF); 156b4315306SDan Handley 15738dce70fSSoby Mathew css_power_down_common(target_state); 158b4315306SDan Handley } 159b4315306SDan Handley 160b4315306SDan Handley /******************************************************************************* 16138dce70fSSoby Mathew * Handler called when a power domain is about to be suspended. The 16238dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 163b4315306SDan Handley ******************************************************************************/ 16438dce70fSSoby Mathew static void css_pwr_domain_suspend(const psci_power_state_t *target_state) 165b4315306SDan Handley { 16638dce70fSSoby Mathew /* 16738dce70fSSoby Mathew * Juno has retention only at cpu level. Just return 16838dce70fSSoby Mathew * as nothing is to be done for retention. 16938dce70fSSoby Mathew */ 17038dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL0] == 17138dce70fSSoby Mathew ARM_LOCAL_STATE_RET) 172b4315306SDan Handley return; 173b4315306SDan Handley 17438dce70fSSoby Mathew assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == 17538dce70fSSoby Mathew ARM_LOCAL_STATE_OFF); 17638dce70fSSoby Mathew 17738dce70fSSoby Mathew css_power_down_common(target_state); 178b4315306SDan Handley } 179b4315306SDan Handley 180b4315306SDan Handley /******************************************************************************* 18138dce70fSSoby Mathew * Handler called when a power domain has just been powered on after 18238dce70fSSoby Mathew * having been suspended earlier. The target_state encodes the low power state 18338dce70fSSoby Mathew * that each level has woken up from. 184b4315306SDan Handley * TODO: At the moment we reuse the on finisher and reinitialize the secure 185b4315306SDan Handley * context. Need to implement a separate suspend finisher. 186b4315306SDan Handley ******************************************************************************/ 18738dce70fSSoby Mathew static void css_pwr_domain_suspend_finish( 18838dce70fSSoby Mathew const psci_power_state_t *target_state) 189b4315306SDan Handley { 19038dce70fSSoby Mathew /* 19138dce70fSSoby Mathew * Return as nothing is to be done on waking up from retention. 19238dce70fSSoby Mathew */ 19338dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL0] == 19438dce70fSSoby Mathew ARM_LOCAL_STATE_RET) 19538dce70fSSoby Mathew return; 19638dce70fSSoby Mathew 19738dce70fSSoby Mathew css_pwr_domain_on_finish(target_state); 198b4315306SDan Handley } 199b4315306SDan Handley 200b4315306SDan Handley /******************************************************************************* 201b4315306SDan Handley * Handlers to shutdown/reboot the system 202b4315306SDan Handley ******************************************************************************/ 203b4315306SDan Handley static void __dead2 css_system_off(void) 204b4315306SDan Handley { 205b4315306SDan Handley uint32_t response; 206b4315306SDan Handley 207b4315306SDan Handley /* Send the power down request to the SCP */ 208b4315306SDan Handley response = scpi_sys_power_state(scpi_system_shutdown); 209b4315306SDan Handley 210b4315306SDan Handley if (response != SCP_OK) { 211b4315306SDan Handley ERROR("CSS System Off: SCP error %u.\n", response); 212b4315306SDan Handley panic(); 213b4315306SDan Handley } 214b4315306SDan Handley wfi(); 215b4315306SDan Handley ERROR("CSS System Off: operation not handled.\n"); 216b4315306SDan Handley panic(); 217b4315306SDan Handley } 218b4315306SDan Handley 219b4315306SDan Handley static void __dead2 css_system_reset(void) 220b4315306SDan Handley { 221b4315306SDan Handley uint32_t response; 222b4315306SDan Handley 223b4315306SDan Handley /* Send the system reset request to the SCP */ 224b4315306SDan Handley response = scpi_sys_power_state(scpi_system_reboot); 225b4315306SDan Handley 226b4315306SDan Handley if (response != SCP_OK) { 227b4315306SDan Handley ERROR("CSS System Reset: SCP error %u.\n", response); 228b4315306SDan Handley panic(); 229b4315306SDan Handley } 230b4315306SDan Handley wfi(); 231b4315306SDan Handley ERROR("CSS System Reset: operation not handled.\n"); 232b4315306SDan Handley panic(); 233b4315306SDan Handley } 234b4315306SDan Handley 235b4315306SDan Handley /******************************************************************************* 23638dce70fSSoby Mathew * Handler called when the CPU power domain is about to enter standby. 237b4315306SDan Handley ******************************************************************************/ 23838dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state) 239b4315306SDan Handley { 240b4315306SDan Handley unsigned int scr; 241b4315306SDan Handley 24238dce70fSSoby Mathew assert(cpu_state == ARM_LOCAL_STATE_RET); 24338dce70fSSoby Mathew 244b4315306SDan Handley scr = read_scr_el3(); 245b4315306SDan Handley /* Enable PhysicalIRQ bit for NS world to wake the CPU */ 246b4315306SDan Handley write_scr_el3(scr | SCR_IRQ_BIT); 247b4315306SDan Handley isb(); 248b4315306SDan Handley dsb(); 249b4315306SDan Handley wfi(); 250b4315306SDan Handley 251b4315306SDan Handley /* 252b4315306SDan Handley * Restore SCR to the original value, synchronisation of scr_el3 is 253b4315306SDan Handley * done by eret while el3_exit to save some execution cycles. 254b4315306SDan Handley */ 255b4315306SDan Handley write_scr_el3(scr); 256b4315306SDan Handley } 257b4315306SDan Handley 258b4315306SDan Handley /******************************************************************************* 259b4315306SDan Handley * Export the platform handlers to enable psci to invoke them 260b4315306SDan Handley ******************************************************************************/ 26138dce70fSSoby Mathew static const plat_psci_ops_t css_ops = { 26238dce70fSSoby Mathew .pwr_domain_on = css_pwr_domain_on, 26338dce70fSSoby Mathew .pwr_domain_on_finish = css_pwr_domain_on_finish, 26438dce70fSSoby Mathew .pwr_domain_off = css_pwr_domain_off, 26538dce70fSSoby Mathew .cpu_standby = css_cpu_standby, 26638dce70fSSoby Mathew .pwr_domain_suspend = css_pwr_domain_suspend, 26738dce70fSSoby Mathew .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish, 268b4315306SDan Handley .system_off = css_system_off, 269b4315306SDan Handley .system_reset = css_system_reset, 270*f9e858b1SSoby Mathew .validate_power_state = arm_validate_power_state, 271*f9e858b1SSoby Mathew .validate_ns_entrypoint = arm_validate_ns_entrypoint 272b4315306SDan Handley }; 273b4315306SDan Handley 274b4315306SDan Handley /******************************************************************************* 27538dce70fSSoby Mathew * Export the platform specific psci ops. 276b4315306SDan Handley ******************************************************************************/ 27738dce70fSSoby Mathew int plat_setup_psci_ops(uintptr_t sec_entrypoint, 27838dce70fSSoby Mathew const plat_psci_ops_t **psci_ops) 279b4315306SDan Handley { 28038dce70fSSoby Mathew *psci_ops = &css_ops; 28138dce70fSSoby Mathew 282804040d1SSandrine Bailleux /* Setup mailbox with entry point. */ 283804040d1SSandrine Bailleux css_program_mailbox(sec_entrypoint); 284b4315306SDan Handley return 0; 285b4315306SDan Handley } 286