Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 23ede6a | 2018-06-19 09:29:36 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 8 | #include <arm_def.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <errno.h> |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 11 | #include <plat_arm.h> |
Soby Mathew | 9ca2806 | 2017-10-11 16:08:58 +0100 | [diff] [blame] | 12 | #include <platform.h> |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 13 | #include <platform_def.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 14 | #include <psci.h> |
| 15 | |
Dimitris Papastamos | d7a3651 | 2018-06-18 13:01:06 +0100 | [diff] [blame] | 16 | /* Allow ARM Standard platforms to override these functions */ |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 17 | #pragma weak plat_arm_psci_override_pm_ops |
Dimitris Papastamos | d7a3651 | 2018-06-18 13:01:06 +0100 | [diff] [blame] | 18 | #pragma weak plat_arm_program_trusted_mailbox |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 19 | |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 20 | #if !ARM_RECOM_STATE_ID_ENC |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 21 | /******************************************************************************* |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 22 | * ARM standard platform handler called to check the validity of the power state |
| 23 | * parameter. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 24 | ******************************************************************************/ |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 25 | int arm_validate_power_state(unsigned int power_state, |
| 26 | psci_power_state_t *req_state) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 27 | { |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 28 | unsigned int pstate = psci_get_pstate_type(power_state); |
| 29 | unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state); |
| 30 | unsigned int i; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 31 | |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 32 | assert(req_state != NULL); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 33 | |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 34 | if (pwr_lvl > PLAT_MAX_PWR_LVL) |
| 35 | return PSCI_E_INVALID_PARAMS; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 36 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 37 | /* Sanity check the requested state */ |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 38 | if (pstate == PSTATE_TYPE_STANDBY) { |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 39 | /* |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 40 | * It's possible to enter standby only on power level 0 |
| 41 | * Ignore any other power level. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 42 | */ |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 43 | if (pwr_lvl != ARM_PWR_LVL0) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 44 | return PSCI_E_INVALID_PARAMS; |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 45 | |
| 46 | req_state->pwr_domain_state[ARM_PWR_LVL0] = |
| 47 | ARM_LOCAL_STATE_RET; |
| 48 | } else { |
| 49 | for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++) |
| 50 | req_state->pwr_domain_state[i] = |
| 51 | ARM_LOCAL_STATE_OFF; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * We expect the 'state id' to be zero. |
| 56 | */ |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 57 | if (psci_get_pstate_id(power_state) != 0U) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 58 | return PSCI_E_INVALID_PARAMS; |
| 59 | |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 60 | return PSCI_E_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | #else |
| 64 | /******************************************************************************* |
| 65 | * ARM standard platform handler called to check the validity of the power |
| 66 | * state parameter. The power state parameter has to be a composite power |
| 67 | * state. |
| 68 | ******************************************************************************/ |
| 69 | int arm_validate_power_state(unsigned int power_state, |
| 70 | psci_power_state_t *req_state) |
| 71 | { |
| 72 | unsigned int state_id; |
| 73 | int i; |
| 74 | |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 75 | assert(req_state != NULL); |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Currently we are using a linear search for finding the matching |
| 79 | * entry in the idle power state array. This can be made a binary |
| 80 | * search if the number of entries justify the additional complexity. |
| 81 | */ |
| 82 | for (i = 0; !!arm_pm_idle_states[i]; i++) { |
| 83 | if (power_state == arm_pm_idle_states[i]) |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | /* Return error if entry not found in the idle state array */ |
| 88 | if (!arm_pm_idle_states[i]) |
| 89 | return PSCI_E_INVALID_PARAMS; |
| 90 | |
| 91 | i = 0; |
| 92 | state_id = psci_get_pstate_id(power_state); |
| 93 | |
| 94 | /* Parse the State ID and populate the state info parameter */ |
| 95 | while (state_id) { |
| 96 | req_state->pwr_domain_state[i++] = state_id & |
| 97 | ARM_LOCAL_PSTATE_MASK; |
| 98 | state_id >>= ARM_LOCAL_PSTATE_WIDTH; |
| 99 | } |
| 100 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 101 | return PSCI_E_SUCCESS; |
| 102 | } |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 103 | #endif /* __ARM_RECOM_STATE_ID_ENC__ */ |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 104 | |
| 105 | /******************************************************************************* |
| 106 | * ARM standard platform handler called to check the validity of the non secure |
Jeenu Viswambharan | 59424d8 | 2017-09-19 09:27:18 +0100 | [diff] [blame] | 107 | * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise. |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 108 | ******************************************************************************/ |
| 109 | int arm_validate_ns_entrypoint(uintptr_t entrypoint) |
| 110 | { |
| 111 | /* |
| 112 | * Check if the non secure entrypoint lies within the non |
| 113 | * secure DRAM. |
| 114 | */ |
| 115 | if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint < |
Jeenu Viswambharan | 59424d8 | 2017-09-19 09:27:18 +0100 | [diff] [blame] | 116 | (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) { |
| 117 | return 0; |
| 118 | } |
dp-arm | 84fc295 | 2017-05-03 12:14:10 +0100 | [diff] [blame] | 119 | #ifndef AARCH32 |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 120 | if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint < |
Jeenu Viswambharan | 59424d8 | 2017-09-19 09:27:18 +0100 | [diff] [blame] | 121 | (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) { |
| 122 | return 0; |
| 123 | } |
dp-arm | 84fc295 | 2017-05-03 12:14:10 +0100 | [diff] [blame] | 124 | #endif |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 125 | |
Jeenu Viswambharan | 59424d8 | 2017-09-19 09:27:18 +0100 | [diff] [blame] | 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | int arm_validate_psci_entrypoint(uintptr_t entrypoint) |
| 130 | { |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 131 | return (arm_validate_ns_entrypoint(entrypoint) == 0) ? PSCI_E_SUCCESS : |
Jeenu Viswambharan | 59424d8 | 2017-09-19 09:27:18 +0100 | [diff] [blame] | 132 | PSCI_E_INVALID_ADDRESS; |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 133 | } |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 134 | |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 135 | /****************************************************************************** |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 136 | * Default definition on ARM standard platforms to override the plat_psci_ops. |
| 137 | *****************************************************************************/ |
| 138 | const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops) |
| 139 | { |
| 140 | return ops; |
| 141 | } |
| 142 | |
| 143 | /****************************************************************************** |
Soby Mathew | 9ca2806 | 2017-10-11 16:08:58 +0100 | [diff] [blame] | 144 | * Helper function to save the platform state before a system suspend. Save the |
| 145 | * state of the system components which are not in the Always ON power domain. |
| 146 | *****************************************************************************/ |
| 147 | void arm_system_pwr_domain_save(void) |
| 148 | { |
| 149 | /* Assert system power domain is available on the platform */ |
| 150 | assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2); |
| 151 | |
| 152 | plat_arm_gic_save(); |
| 153 | |
| 154 | /* |
Antonio Nino Diaz | 23ede6a | 2018-06-19 09:29:36 +0100 | [diff] [blame] | 155 | * Unregister console now so that it is not registered for a second |
| 156 | * time during resume. |
| 157 | */ |
| 158 | arm_console_runtime_end(); |
| 159 | |
| 160 | /* |
Soby Mathew | 9ca2806 | 2017-10-11 16:08:58 +0100 | [diff] [blame] | 161 | * All the other peripheral which are configured by ARM TF are |
| 162 | * re-initialized on resume from system suspend. Hence we |
| 163 | * don't save their state here. |
| 164 | */ |
| 165 | } |
| 166 | |
| 167 | /****************************************************************************** |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 168 | * Helper function to resume the platform from system suspend. Reinitialize |
| 169 | * the system components which are not in the Always ON power domain. |
| 170 | * TODO: Unify the platform setup when waking up from cold boot and system |
| 171 | * resume in arm_bl31_platform_setup(). |
| 172 | *****************************************************************************/ |
| 173 | void arm_system_pwr_domain_resume(void) |
| 174 | { |
Antonio Nino Diaz | 23ede6a | 2018-06-19 09:29:36 +0100 | [diff] [blame] | 175 | /* Initialize the console */ |
| 176 | arm_console_runtime_init(); |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 177 | |
| 178 | /* Assert system power domain is available on the platform */ |
| 179 | assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2); |
| 180 | |
Soby Mathew | 9ca2806 | 2017-10-11 16:08:58 +0100 | [diff] [blame] | 181 | plat_arm_gic_resume(); |
| 182 | |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 183 | plat_arm_security_setup(); |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 184 | arm_configure_sys_timer(); |
| 185 | } |
| 186 | |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 187 | /******************************************************************************* |
Dimitris Papastamos | d7a3651 | 2018-06-18 13:01:06 +0100 | [diff] [blame] | 188 | * ARM platform function to program the mailbox for a cpu before it is released |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 189 | * from reset. This function assumes that the Trusted mail box base is within |
| 190 | * the ARM_SHARED_RAM region |
| 191 | ******************************************************************************/ |
Dimitris Papastamos | d7a3651 | 2018-06-18 13:01:06 +0100 | [diff] [blame] | 192 | void plat_arm_program_trusted_mailbox(uintptr_t address) |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 193 | { |
| 194 | uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE; |
| 195 | |
| 196 | *mailbox = address; |
| 197 | |
| 198 | /* |
| 199 | * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within |
| 200 | * ARM_SHARED_RAM region. |
| 201 | */ |
| 202 | assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) && |
| 203 | ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \ |
| 204 | (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE))); |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /******************************************************************************* |
| 208 | * The ARM Standard platform definition of platform porting API |
| 209 | * `plat_setup_psci_ops`. |
| 210 | ******************************************************************************/ |
Daniel Boulby | f45a4bb | 2018-09-18 13:26:03 +0100 | [diff] [blame] | 211 | int __init plat_setup_psci_ops(uintptr_t sec_entrypoint, |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 212 | const plat_psci_ops_t **psci_ops) |
| 213 | { |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 214 | *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops); |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 215 | |
| 216 | /* Setup mailbox with entry point. */ |
Dimitris Papastamos | d7a3651 | 2018-06-18 13:01:06 +0100 | [diff] [blame] | 217 | plat_arm_program_trusted_mailbox(sec_entrypoint); |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 218 | return 0; |
| 219 | } |