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 unsigned long wakeup_address; 4538dce70fSSoby Mathew 46*2204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC 47*2204afdeSSoby Mathew /* 48*2204afdeSSoby Mathew * The table storing the valid idle power states. Ensure that the 49*2204afdeSSoby Mathew * array entries are populated in ascending order of state-id to 50*2204afdeSSoby Mathew * enable us to use binary search during power state validation. 51*2204afdeSSoby Mathew * The table must be terminated by a NULL entry. 52*2204afdeSSoby Mathew */ 53*2204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = { 54*2204afdeSSoby Mathew /* State-id - 0x01 */ 55*2204afdeSSoby Mathew arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET, 56*2204afdeSSoby Mathew ARM_PWR_LVL0, PSTATE_TYPE_STANDBY), 57*2204afdeSSoby Mathew /* State-id - 0x02 */ 58*2204afdeSSoby Mathew arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF, 59*2204afdeSSoby Mathew ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN), 60*2204afdeSSoby Mathew /* State-id - 0x22 */ 61*2204afdeSSoby Mathew arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF, 62*2204afdeSSoby Mathew ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN), 63*2204afdeSSoby Mathew 0, 64*2204afdeSSoby Mathew }; 65*2204afdeSSoby Mathew #endif 66*2204afdeSSoby Mathew 67b4315306SDan Handley /******************************************************************************* 68b4315306SDan Handley * Private function to program the mailbox for a cpu before it is released 69b4315306SDan Handley * from reset. 70b4315306SDan Handley ******************************************************************************/ 71b4315306SDan Handley static void css_program_mailbox(uint64_t mpidr, uint64_t address) 72b4315306SDan Handley { 73b4315306SDan Handley uint64_t linear_id; 74b4315306SDan Handley uint64_t mbox; 75b4315306SDan Handley 7638dce70fSSoby Mathew linear_id = plat_arm_calc_core_pos(mpidr); 77b4315306SDan Handley mbox = TRUSTED_MAILBOXES_BASE + (linear_id << TRUSTED_MAILBOX_SHIFT); 78b4315306SDan Handley *((uint64_t *) mbox) = address; 79b4315306SDan Handley flush_dcache_range(mbox, sizeof(mbox)); 80b4315306SDan Handley } 81b4315306SDan Handley 82b4315306SDan Handley /******************************************************************************* 8338dce70fSSoby Mathew * Handler called when a power domain is about to be turned on. The 84b4315306SDan Handley * level and mpidr determine the affinity instance. 85b4315306SDan Handley ******************************************************************************/ 8638dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr) 87b4315306SDan Handley { 88b4315306SDan Handley /* 8938dce70fSSoby Mathew * SCP takes care of powering up parent power domains so we 90b4315306SDan Handley * only need to care about level 0 91b4315306SDan Handley */ 92b4315306SDan Handley 93b4315306SDan Handley /* 94b4315306SDan Handley * Setup mailbox with address for CPU entrypoint when it next powers up 95b4315306SDan Handley */ 9638dce70fSSoby Mathew css_program_mailbox(mpidr, wakeup_address); 97b4315306SDan Handley 98b4315306SDan Handley scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on, 99b4315306SDan Handley scpi_power_on); 100b4315306SDan Handley 101b4315306SDan Handley return PSCI_E_SUCCESS; 102b4315306SDan Handley } 103b4315306SDan Handley 104b4315306SDan Handley /******************************************************************************* 10538dce70fSSoby Mathew * Handler called when a power level has just been powered on after 10638dce70fSSoby Mathew * being turned off earlier. The target_state encodes the low power state that 10738dce70fSSoby Mathew * each level has woken up from. 108b4315306SDan Handley ******************************************************************************/ 10938dce70fSSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state) 110b4315306SDan Handley { 11138dce70fSSoby Mathew assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == 11238dce70fSSoby Mathew ARM_LOCAL_STATE_OFF); 113b4315306SDan Handley 114b4315306SDan Handley /* 115b4315306SDan Handley * Perform the common cluster specific operations i.e enable coherency 116b4315306SDan Handley * if this cluster was off. 117b4315306SDan Handley */ 11838dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL1] == 11938dce70fSSoby Mathew ARM_LOCAL_STATE_OFF) 12038dce70fSSoby Mathew cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1())); 121b4315306SDan Handley 122b4315306SDan Handley /* Enable the gic cpu interface */ 123b4315306SDan Handley arm_gic_cpuif_setup(); 124b4315306SDan Handley 125b4315306SDan Handley /* todo: Is this setup only needed after a cold boot? */ 126b4315306SDan Handley arm_gic_pcpu_distif_setup(); 127b4315306SDan Handley 128b4315306SDan Handley /* Clear the mailbox for this cpu. */ 12938dce70fSSoby Mathew css_program_mailbox(read_mpidr_el1(), 0); 130b4315306SDan Handley } 131b4315306SDan Handley 132b4315306SDan Handley /******************************************************************************* 133b4315306SDan Handley * Common function called while turning a cpu off or suspending it. It is called 134b4315306SDan Handley * from css_off() or css_suspend() when these functions in turn are called for 13538dce70fSSoby Mathew * power domain at the highest power level which will be powered down. It 13638dce70fSSoby Mathew * performs the actions common to the OFF and SUSPEND calls. 137b4315306SDan Handley ******************************************************************************/ 13838dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state) 139b4315306SDan Handley { 140b4315306SDan Handley uint32_t cluster_state = scpi_power_on; 141b4315306SDan Handley 142b4315306SDan Handley /* Prevent interrupts from spuriously waking up this cpu */ 143b4315306SDan Handley arm_gic_cpuif_deactivate(); 144b4315306SDan Handley 145b4315306SDan Handley /* Cluster is to be turned off, so disable coherency */ 14638dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL1] == 14738dce70fSSoby Mathew ARM_LOCAL_STATE_OFF) { 148b4315306SDan Handley cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr())); 149b4315306SDan Handley cluster_state = scpi_power_off; 150b4315306SDan Handley } 151b4315306SDan Handley 152b4315306SDan Handley /* 153b4315306SDan Handley * Ask the SCP to power down the appropriate components depending upon 154b4315306SDan Handley * their state. 155b4315306SDan Handley */ 156b4315306SDan Handley scpi_set_css_power_state(read_mpidr_el1(), 157b4315306SDan Handley scpi_power_off, 158b4315306SDan Handley cluster_state, 159b4315306SDan Handley scpi_power_on); 160b4315306SDan Handley } 161b4315306SDan Handley 162b4315306SDan Handley /******************************************************************************* 16338dce70fSSoby Mathew * Handler called when a power domain is about to be turned off. The 16438dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 165b4315306SDan Handley ******************************************************************************/ 16638dce70fSSoby Mathew static void css_pwr_domain_off(const psci_power_state_t *target_state) 167b4315306SDan Handley { 16838dce70fSSoby Mathew assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == 16938dce70fSSoby Mathew ARM_LOCAL_STATE_OFF); 170b4315306SDan Handley 17138dce70fSSoby Mathew css_power_down_common(target_state); 172b4315306SDan Handley } 173b4315306SDan Handley 174b4315306SDan Handley /******************************************************************************* 17538dce70fSSoby Mathew * Handler called when a power domain is about to be suspended. The 17638dce70fSSoby Mathew * target_state encodes the power state that each level should transition to. 177b4315306SDan Handley ******************************************************************************/ 17838dce70fSSoby Mathew static void css_pwr_domain_suspend(const psci_power_state_t *target_state) 179b4315306SDan Handley { 18038dce70fSSoby Mathew /* 18138dce70fSSoby Mathew * Juno has retention only at cpu level. Just return 18238dce70fSSoby Mathew * as nothing is to be done for retention. 18338dce70fSSoby Mathew */ 18438dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL0] == 18538dce70fSSoby Mathew ARM_LOCAL_STATE_RET) 186b4315306SDan Handley return; 187b4315306SDan Handley 18838dce70fSSoby Mathew assert(target_state->pwr_domain_state[ARM_PWR_LVL0] == 18938dce70fSSoby Mathew ARM_LOCAL_STATE_OFF); 19038dce70fSSoby Mathew 191b4315306SDan Handley /* 192b4315306SDan Handley * Setup mailbox with address for CPU entrypoint when it next powers up. 193b4315306SDan Handley */ 19438dce70fSSoby Mathew css_program_mailbox(read_mpidr_el1(), wakeup_address); 195b4315306SDan Handley 19638dce70fSSoby Mathew css_power_down_common(target_state); 197b4315306SDan Handley } 198b4315306SDan Handley 199b4315306SDan Handley /******************************************************************************* 20038dce70fSSoby Mathew * Handler called when a power domain has just been powered on after 20138dce70fSSoby Mathew * having been suspended earlier. The target_state encodes the low power state 20238dce70fSSoby Mathew * that each level has woken up from. 203b4315306SDan Handley * TODO: At the moment we reuse the on finisher and reinitialize the secure 204b4315306SDan Handley * context. Need to implement a separate suspend finisher. 205b4315306SDan Handley ******************************************************************************/ 20638dce70fSSoby Mathew static void css_pwr_domain_suspend_finish( 20738dce70fSSoby Mathew const psci_power_state_t *target_state) 208b4315306SDan Handley { 20938dce70fSSoby Mathew /* 21038dce70fSSoby Mathew * Return as nothing is to be done on waking up from retention. 21138dce70fSSoby Mathew */ 21238dce70fSSoby Mathew if (target_state->pwr_domain_state[ARM_PWR_LVL0] == 21338dce70fSSoby Mathew ARM_LOCAL_STATE_RET) 21438dce70fSSoby Mathew return; 21538dce70fSSoby Mathew 21638dce70fSSoby Mathew css_pwr_domain_on_finish(target_state); 217b4315306SDan Handley } 218b4315306SDan Handley 219b4315306SDan Handley /******************************************************************************* 220b4315306SDan Handley * Handlers to shutdown/reboot the system 221b4315306SDan Handley ******************************************************************************/ 222b4315306SDan Handley static void __dead2 css_system_off(void) 223b4315306SDan Handley { 224b4315306SDan Handley uint32_t response; 225b4315306SDan Handley 226b4315306SDan Handley /* Send the power down request to the SCP */ 227b4315306SDan Handley response = scpi_sys_power_state(scpi_system_shutdown); 228b4315306SDan Handley 229b4315306SDan Handley if (response != SCP_OK) { 230b4315306SDan Handley ERROR("CSS System Off: SCP error %u.\n", response); 231b4315306SDan Handley panic(); 232b4315306SDan Handley } 233b4315306SDan Handley wfi(); 234b4315306SDan Handley ERROR("CSS System Off: operation not handled.\n"); 235b4315306SDan Handley panic(); 236b4315306SDan Handley } 237b4315306SDan Handley 238b4315306SDan Handley static void __dead2 css_system_reset(void) 239b4315306SDan Handley { 240b4315306SDan Handley uint32_t response; 241b4315306SDan Handley 242b4315306SDan Handley /* Send the system reset request to the SCP */ 243b4315306SDan Handley response = scpi_sys_power_state(scpi_system_reboot); 244b4315306SDan Handley 245b4315306SDan Handley if (response != SCP_OK) { 246b4315306SDan Handley ERROR("CSS System Reset: SCP error %u.\n", response); 247b4315306SDan Handley panic(); 248b4315306SDan Handley } 249b4315306SDan Handley wfi(); 250b4315306SDan Handley ERROR("CSS System Reset: operation not handled.\n"); 251b4315306SDan Handley panic(); 252b4315306SDan Handley } 253b4315306SDan Handley 254b4315306SDan Handley /******************************************************************************* 25538dce70fSSoby Mathew * Handler called when the CPU power domain is about to enter standby. 256b4315306SDan Handley ******************************************************************************/ 25738dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state) 258b4315306SDan Handley { 259b4315306SDan Handley unsigned int scr; 260b4315306SDan Handley 26138dce70fSSoby Mathew assert(cpu_state == ARM_LOCAL_STATE_RET); 26238dce70fSSoby Mathew 263b4315306SDan Handley scr = read_scr_el3(); 264b4315306SDan Handley /* Enable PhysicalIRQ bit for NS world to wake the CPU */ 265b4315306SDan Handley write_scr_el3(scr | SCR_IRQ_BIT); 266b4315306SDan Handley isb(); 267b4315306SDan Handley dsb(); 268b4315306SDan Handley wfi(); 269b4315306SDan Handley 270b4315306SDan Handley /* 271b4315306SDan Handley * Restore SCR to the original value, synchronisation of scr_el3 is 272b4315306SDan Handley * done by eret while el3_exit to save some execution cycles. 273b4315306SDan Handley */ 274b4315306SDan Handley write_scr_el3(scr); 275b4315306SDan Handley } 276b4315306SDan Handley 277b4315306SDan Handley /******************************************************************************* 278b4315306SDan Handley * Export the platform handlers to enable psci to invoke them 279b4315306SDan Handley ******************************************************************************/ 28038dce70fSSoby Mathew static const plat_psci_ops_t css_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, 289b4315306SDan Handley .validate_power_state = arm_validate_power_state 290b4315306SDan Handley }; 291b4315306SDan Handley 292b4315306SDan Handley /******************************************************************************* 29338dce70fSSoby Mathew * Export the platform specific psci ops. 294b4315306SDan Handley ******************************************************************************/ 29538dce70fSSoby Mathew int plat_setup_psci_ops(uintptr_t sec_entrypoint, 29638dce70fSSoby Mathew const plat_psci_ops_t **psci_ops) 297b4315306SDan Handley { 29838dce70fSSoby Mathew *psci_ops = &css_ops; 29938dce70fSSoby Mathew 30038dce70fSSoby Mathew wakeup_address = sec_entrypoint; 30138dce70fSSoby Mathew flush_dcache_range((unsigned long)&wakeup_address, 30238dce70fSSoby Mathew sizeof(wakeup_address)); 303b4315306SDan Handley return 0; 304b4315306SDan Handley } 305