1b4315306SDan Handley /* 2e6937287SZelalem * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 3b4315306SDan Handley * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5b4315306SDan Handley */ 6b4315306SDan Handley 7b4315306SDan Handley #include <assert.h> 809d40e0eSAntonio Nino Diaz 9785fb92bSSoby Mathew #include <platform_def.h> 1009d40e0eSAntonio Nino Diaz 1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1209d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 13bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 1409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 1509d40e0eSAntonio Nino Diaz 162a246d2eSDimitris Papastamos /* Allow ARM Standard platforms to override these functions */ 172a246d2eSDimitris Papastamos #pragma weak plat_arm_program_trusted_mailbox 185486a965SSoby Mathew 192204afdeSSoby Mathew #if !ARM_RECOM_STATE_ID_ENC 20b4315306SDan Handley /******************************************************************************* 21b4315306SDan Handley * ARM standard platform handler called to check the validity of the power state 22b4315306SDan Handley * parameter. 23b4315306SDan Handley ******************************************************************************/ 2438dce70fSSoby Mathew int arm_validate_power_state(unsigned int power_state, 2538dce70fSSoby Mathew psci_power_state_t *req_state) 26b4315306SDan Handley { 272bc3dba9SAntonio Nino Diaz unsigned int pstate = psci_get_pstate_type(power_state); 282bc3dba9SAntonio Nino Diaz unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state); 292bc3dba9SAntonio Nino Diaz unsigned int i; 3038dce70fSSoby Mathew 31e02f469fSSathees Balya assert(req_state != NULL); 3238dce70fSSoby Mathew 3338dce70fSSoby Mathew if (pwr_lvl > PLAT_MAX_PWR_LVL) 34b4315306SDan Handley return PSCI_E_INVALID_PARAMS; 3538dce70fSSoby Mathew 3638dce70fSSoby Mathew /* Sanity check the requested state */ 3738dce70fSSoby Mathew if (pstate == PSTATE_TYPE_STANDBY) { 3838dce70fSSoby Mathew /* 3938dce70fSSoby Mathew * It's possible to enter standby only on power level 0 4038dce70fSSoby Mathew * Ignore any other power level. 4138dce70fSSoby Mathew */ 4238dce70fSSoby Mathew if (pwr_lvl != ARM_PWR_LVL0) 4338dce70fSSoby Mathew return PSCI_E_INVALID_PARAMS; 4438dce70fSSoby Mathew 4538dce70fSSoby Mathew req_state->pwr_domain_state[ARM_PWR_LVL0] = 4638dce70fSSoby Mathew ARM_LOCAL_STATE_RET; 4738dce70fSSoby Mathew } else { 4838dce70fSSoby Mathew for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++) 4938dce70fSSoby Mathew req_state->pwr_domain_state[i] = 5038dce70fSSoby Mathew ARM_LOCAL_STATE_OFF; 51b4315306SDan Handley } 52b4315306SDan Handley 53b4315306SDan Handley /* 54b4315306SDan Handley * We expect the 'state id' to be zero. 55b4315306SDan Handley */ 562bc3dba9SAntonio Nino Diaz if (psci_get_pstate_id(power_state) != 0U) 57b4315306SDan Handley return PSCI_E_INVALID_PARAMS; 58b4315306SDan Handley 59b4315306SDan Handley return PSCI_E_SUCCESS; 60b4315306SDan Handley } 612204afdeSSoby Mathew 622204afdeSSoby Mathew #else 632204afdeSSoby Mathew /******************************************************************************* 642204afdeSSoby Mathew * ARM standard platform handler called to check the validity of the power 652204afdeSSoby Mathew * state parameter. The power state parameter has to be a composite power 662204afdeSSoby Mathew * state. 672204afdeSSoby Mathew ******************************************************************************/ 682204afdeSSoby Mathew int arm_validate_power_state(unsigned int power_state, 692204afdeSSoby Mathew psci_power_state_t *req_state) 702204afdeSSoby Mathew { 712204afdeSSoby Mathew unsigned int state_id; 722204afdeSSoby Mathew int i; 732204afdeSSoby Mathew 74e02f469fSSathees Balya assert(req_state != NULL); 752204afdeSSoby Mathew 762204afdeSSoby Mathew /* 772204afdeSSoby Mathew * Currently we are using a linear search for finding the matching 782204afdeSSoby Mathew * entry in the idle power state array. This can be made a binary 792204afdeSSoby Mathew * search if the number of entries justify the additional complexity. 802204afdeSSoby Mathew */ 812204afdeSSoby Mathew for (i = 0; !!arm_pm_idle_states[i]; i++) { 822204afdeSSoby Mathew if (power_state == arm_pm_idle_states[i]) 832204afdeSSoby Mathew break; 842204afdeSSoby Mathew } 852204afdeSSoby Mathew 862204afdeSSoby Mathew /* Return error if entry not found in the idle state array */ 872204afdeSSoby Mathew if (!arm_pm_idle_states[i]) 882204afdeSSoby Mathew return PSCI_E_INVALID_PARAMS; 892204afdeSSoby Mathew 902204afdeSSoby Mathew i = 0; 912204afdeSSoby Mathew state_id = psci_get_pstate_id(power_state); 922204afdeSSoby Mathew 932204afdeSSoby Mathew /* Parse the State ID and populate the state info parameter */ 942204afdeSSoby Mathew while (state_id) { 952204afdeSSoby Mathew req_state->pwr_domain_state[i++] = state_id & 962204afdeSSoby Mathew ARM_LOCAL_PSTATE_MASK; 972204afdeSSoby Mathew state_id >>= ARM_LOCAL_PSTATE_WIDTH; 982204afdeSSoby Mathew } 992204afdeSSoby Mathew 1002204afdeSSoby Mathew return PSCI_E_SUCCESS; 1012204afdeSSoby Mathew } 1022204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */ 103f9e858b1SSoby Mathew 104f9e858b1SSoby Mathew /******************************************************************************* 105f9e858b1SSoby Mathew * ARM standard platform handler called to check the validity of the non secure 10671e7a4e5SJeenu Viswambharan * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise. 107f9e858b1SSoby Mathew ******************************************************************************/ 108f9e858b1SSoby Mathew int arm_validate_ns_entrypoint(uintptr_t entrypoint) 109f9e858b1SSoby Mathew { 110f9e858b1SSoby Mathew /* 111f9e858b1SSoby Mathew * Check if the non secure entrypoint lies within the non 112f9e858b1SSoby Mathew * secure DRAM. 113f9e858b1SSoby Mathew */ 114f9e858b1SSoby Mathew if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint < 11571e7a4e5SJeenu Viswambharan (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) { 11671e7a4e5SJeenu Viswambharan return 0; 11771e7a4e5SJeenu Viswambharan } 118402b3cf8SJulius Werner #ifdef __aarch64__ 119f9e858b1SSoby Mathew if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint < 12071e7a4e5SJeenu Viswambharan (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) { 12171e7a4e5SJeenu Viswambharan return 0; 12271e7a4e5SJeenu Viswambharan } 1237c7dffd8Sdp-arm #endif 124f9e858b1SSoby Mathew 12571e7a4e5SJeenu Viswambharan return -1; 12671e7a4e5SJeenu Viswambharan } 12771e7a4e5SJeenu Viswambharan 12871e7a4e5SJeenu Viswambharan int arm_validate_psci_entrypoint(uintptr_t entrypoint) 12971e7a4e5SJeenu Viswambharan { 130e02f469fSSathees Balya return (arm_validate_ns_entrypoint(entrypoint) == 0) ? PSCI_E_SUCCESS : 13171e7a4e5SJeenu Viswambharan PSCI_E_INVALID_ADDRESS; 132f9e858b1SSoby Mathew } 133785fb92bSSoby Mathew 134c1bb8a05SSoby Mathew /****************************************************************************** 135e35a3fb5SSoby Mathew * Helper function to save the platform state before a system suspend. Save the 136e35a3fb5SSoby Mathew * state of the system components which are not in the Always ON power domain. 137e35a3fb5SSoby Mathew *****************************************************************************/ 138e35a3fb5SSoby Mathew void arm_system_pwr_domain_save(void) 139e35a3fb5SSoby Mathew { 140e35a3fb5SSoby Mathew /* Assert system power domain is available on the platform */ 141e35a3fb5SSoby Mathew assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2); 142e35a3fb5SSoby Mathew 143e35a3fb5SSoby Mathew plat_arm_gic_save(); 144e35a3fb5SSoby Mathew 145e35a3fb5SSoby Mathew /* 14688a0523eSAntonio Nino Diaz * Unregister console now so that it is not registered for a second 14788a0523eSAntonio Nino Diaz * time during resume. 14888a0523eSAntonio Nino Diaz */ 14988a0523eSAntonio Nino Diaz arm_console_runtime_end(); 15088a0523eSAntonio Nino Diaz 15188a0523eSAntonio Nino Diaz /* 152e35a3fb5SSoby Mathew * All the other peripheral which are configured by ARM TF are 153e35a3fb5SSoby Mathew * re-initialized on resume from system suspend. Hence we 154e35a3fb5SSoby Mathew * don't save their state here. 155e35a3fb5SSoby Mathew */ 156e35a3fb5SSoby Mathew } 157e35a3fb5SSoby Mathew 158e35a3fb5SSoby Mathew /****************************************************************************** 159c1bb8a05SSoby Mathew * Helper function to resume the platform from system suspend. Reinitialize 160c1bb8a05SSoby Mathew * the system components which are not in the Always ON power domain. 161c1bb8a05SSoby Mathew * TODO: Unify the platform setup when waking up from cold boot and system 162c1bb8a05SSoby Mathew * resume in arm_bl31_platform_setup(). 163c1bb8a05SSoby Mathew *****************************************************************************/ 164c1bb8a05SSoby Mathew void arm_system_pwr_domain_resume(void) 165c1bb8a05SSoby Mathew { 16688a0523eSAntonio Nino Diaz /* Initialize the console */ 16788a0523eSAntonio Nino Diaz arm_console_runtime_init(); 168c1bb8a05SSoby Mathew 169c1bb8a05SSoby Mathew /* Assert system power domain is available on the platform */ 170c1bb8a05SSoby Mathew assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2); 171c1bb8a05SSoby Mathew 172e35a3fb5SSoby Mathew plat_arm_gic_resume(); 173e35a3fb5SSoby Mathew 174c1bb8a05SSoby Mathew plat_arm_security_setup(); 175c1bb8a05SSoby Mathew arm_configure_sys_timer(); 176c1bb8a05SSoby Mathew } 177c1bb8a05SSoby Mathew 178785fb92bSSoby Mathew /******************************************************************************* 1792a246d2eSDimitris Papastamos * ARM platform function to program the mailbox for a cpu before it is released 180785fb92bSSoby Mathew * from reset. This function assumes that the Trusted mail box base is within 181785fb92bSSoby Mathew * the ARM_SHARED_RAM region 182785fb92bSSoby Mathew ******************************************************************************/ 1832a246d2eSDimitris Papastamos void plat_arm_program_trusted_mailbox(uintptr_t address) 184785fb92bSSoby Mathew { 185785fb92bSSoby Mathew uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE; 186785fb92bSSoby Mathew 187785fb92bSSoby Mathew *mailbox = address; 188785fb92bSSoby Mathew 189785fb92bSSoby Mathew /* 190785fb92bSSoby Mathew * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within 191785fb92bSSoby Mathew * ARM_SHARED_RAM region. 192785fb92bSSoby Mathew */ 193785fb92bSSoby Mathew assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) && 194*9a90d720SElyes Haouas ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= 195785fb92bSSoby Mathew (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE))); 196785fb92bSSoby Mathew } 197785fb92bSSoby Mathew 198785fb92bSSoby Mathew /******************************************************************************* 199785fb92bSSoby Mathew * The ARM Standard platform definition of platform porting API 200785fb92bSSoby Mathew * `plat_setup_psci_ops`. 201785fb92bSSoby Mathew ******************************************************************************/ 2024d010d0dSDaniel Boulby int __init plat_setup_psci_ops(uintptr_t sec_entrypoint, 203785fb92bSSoby Mathew const plat_psci_ops_t **psci_ops) 204785fb92bSSoby Mathew { 2055486a965SSoby Mathew *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops); 206785fb92bSSoby Mathew 207785fb92bSSoby Mathew /* Setup mailbox with entry point. */ 2082a246d2eSDimitris Papastamos plat_arm_program_trusted_mailbox(sec_entrypoint); 209785fb92bSSoby Mathew return 0; 210785fb92bSSoby Mathew } 211