Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 1 | /* |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2016, 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> |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 9 | #include <arm_gic.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 10 | #include <assert.h> |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 11 | #include <console.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 13 | #include <plat_arm.h> |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 14 | #include <platform_def.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 15 | #include <psci.h> |
| 16 | |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 17 | /* Allow ARM Standard platforms to override this function */ |
| 18 | #pragma weak plat_arm_psci_override_pm_ops |
| 19 | |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 20 | /* Standard ARM platforms are expected to export plat_arm_psci_pm_ops */ |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 21 | extern plat_psci_ops_t plat_arm_psci_pm_ops; |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 22 | |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 23 | #if ARM_RECOM_STATE_ID_ENC |
| 24 | extern unsigned int arm_pm_idle_states[]; |
| 25 | #endif /* __ARM_RECOM_STATE_ID_ENC__ */ |
| 26 | |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 27 | #if !ARM_RECOM_STATE_ID_ENC |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 28 | /******************************************************************************* |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 29 | * ARM standard platform handler called to check the validity of the power state |
| 30 | * parameter. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 31 | ******************************************************************************/ |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 32 | int arm_validate_power_state(unsigned int power_state, |
| 33 | psci_power_state_t *req_state) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 34 | { |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 35 | int pstate = psci_get_pstate_type(power_state); |
| 36 | int pwr_lvl = psci_get_pstate_pwrlvl(power_state); |
| 37 | int i; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 38 | |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 39 | assert(req_state); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 40 | |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 41 | if (pwr_lvl > PLAT_MAX_PWR_LVL) |
| 42 | return PSCI_E_INVALID_PARAMS; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 43 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 44 | /* Sanity check the requested state */ |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 45 | if (pstate == PSTATE_TYPE_STANDBY) { |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 46 | /* |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 47 | * It's possible to enter standby only on power level 0 |
| 48 | * Ignore any other power level. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 49 | */ |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 50 | if (pwr_lvl != ARM_PWR_LVL0) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 51 | return PSCI_E_INVALID_PARAMS; |
Soby Mathew | fec4eb7 | 2015-07-01 16:16:20 +0100 | [diff] [blame] | 52 | |
| 53 | req_state->pwr_domain_state[ARM_PWR_LVL0] = |
| 54 | ARM_LOCAL_STATE_RET; |
| 55 | } else { |
| 56 | for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++) |
| 57 | req_state->pwr_domain_state[i] = |
| 58 | ARM_LOCAL_STATE_OFF; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
| 62 | * We expect the 'state id' to be zero. |
| 63 | */ |
| 64 | if (psci_get_pstate_id(power_state)) |
| 65 | return PSCI_E_INVALID_PARAMS; |
| 66 | |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 67 | return PSCI_E_SUCCESS; |
| 68 | } |
| 69 | |
| 70 | #else |
| 71 | /******************************************************************************* |
| 72 | * ARM standard platform handler called to check the validity of the power |
| 73 | * state parameter. The power state parameter has to be a composite power |
| 74 | * state. |
| 75 | ******************************************************************************/ |
| 76 | int arm_validate_power_state(unsigned int power_state, |
| 77 | psci_power_state_t *req_state) |
| 78 | { |
| 79 | unsigned int state_id; |
| 80 | int i; |
| 81 | |
| 82 | assert(req_state); |
| 83 | |
| 84 | /* |
| 85 | * Currently we are using a linear search for finding the matching |
| 86 | * entry in the idle power state array. This can be made a binary |
| 87 | * search if the number of entries justify the additional complexity. |
| 88 | */ |
| 89 | for (i = 0; !!arm_pm_idle_states[i]; i++) { |
| 90 | if (power_state == arm_pm_idle_states[i]) |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | /* Return error if entry not found in the idle state array */ |
| 95 | if (!arm_pm_idle_states[i]) |
| 96 | return PSCI_E_INVALID_PARAMS; |
| 97 | |
| 98 | i = 0; |
| 99 | state_id = psci_get_pstate_id(power_state); |
| 100 | |
| 101 | /* Parse the State ID and populate the state info parameter */ |
| 102 | while (state_id) { |
| 103 | req_state->pwr_domain_state[i++] = state_id & |
| 104 | ARM_LOCAL_PSTATE_MASK; |
| 105 | state_id >>= ARM_LOCAL_PSTATE_WIDTH; |
| 106 | } |
| 107 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 108 | return PSCI_E_SUCCESS; |
| 109 | } |
Soby Mathew | 7799cf7 | 2015-04-16 14:49:09 +0100 | [diff] [blame] | 110 | #endif /* __ARM_RECOM_STATE_ID_ENC__ */ |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 111 | |
| 112 | /******************************************************************************* |
| 113 | * ARM standard platform handler called to check the validity of the non secure |
| 114 | * entrypoint. |
| 115 | ******************************************************************************/ |
| 116 | int arm_validate_ns_entrypoint(uintptr_t entrypoint) |
| 117 | { |
| 118 | /* |
| 119 | * Check if the non secure entrypoint lies within the non |
| 120 | * secure DRAM. |
| 121 | */ |
| 122 | if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint < |
| 123 | (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) |
| 124 | return PSCI_E_SUCCESS; |
dp-arm | 84fc295 | 2017-05-03 12:14:10 +0100 | [diff] [blame] | 125 | #ifndef AARCH32 |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 126 | if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint < |
| 127 | (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) |
| 128 | return PSCI_E_SUCCESS; |
dp-arm | 84fc295 | 2017-05-03 12:14:10 +0100 | [diff] [blame] | 129 | #endif |
Soby Mathew | 0d9e852 | 2015-07-15 13:36:24 +0100 | [diff] [blame] | 130 | |
| 131 | return PSCI_E_INVALID_ADDRESS; |
| 132 | } |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 133 | |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 134 | /****************************************************************************** |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 135 | * Default definition on ARM standard platforms to override the plat_psci_ops. |
| 136 | *****************************************************************************/ |
| 137 | const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops) |
| 138 | { |
| 139 | return ops; |
| 140 | } |
| 141 | |
| 142 | /****************************************************************************** |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 143 | * Helper function to resume the platform from system suspend. Reinitialize |
| 144 | * the system components which are not in the Always ON power domain. |
| 145 | * TODO: Unify the platform setup when waking up from cold boot and system |
| 146 | * resume in arm_bl31_platform_setup(). |
| 147 | *****************************************************************************/ |
| 148 | void arm_system_pwr_domain_resume(void) |
| 149 | { |
Soby Mathew | 2fd66be | 2015-12-09 11:38:43 +0000 | [diff] [blame] | 150 | console_init(PLAT_ARM_BL31_RUN_UART_BASE, PLAT_ARM_BL31_RUN_UART_CLK_IN_HZ, |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 151 | ARM_CONSOLE_BAUDRATE); |
| 152 | |
| 153 | /* Assert system power domain is available on the platform */ |
| 154 | assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2); |
| 155 | |
Achin Gupta | 1fa7eb6 | 2015-11-03 14:18:34 +0000 | [diff] [blame] | 156 | /* |
| 157 | * TODO: On GICv3 systems, figure out whether the core that wakes up |
| 158 | * first from system suspend need to initialize the re-distributor |
| 159 | * interface of all the other suspended cores. |
| 160 | */ |
| 161 | plat_arm_gic_init(); |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 162 | plat_arm_security_setup(); |
Soby Mathew | 61e8d0b | 2015-10-12 17:32:29 +0100 | [diff] [blame] | 163 | arm_configure_sys_timer(); |
| 164 | } |
| 165 | |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 166 | /******************************************************************************* |
| 167 | * Private function to program the mailbox for a cpu before it is released |
| 168 | * from reset. This function assumes that the Trusted mail box base is within |
| 169 | * the ARM_SHARED_RAM region |
| 170 | ******************************************************************************/ |
Sandrine Bailleux | 03897bb | 2015-11-26 16:31:34 +0000 | [diff] [blame] | 171 | void arm_program_trusted_mailbox(uintptr_t address) |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 172 | { |
| 173 | uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE; |
| 174 | |
| 175 | *mailbox = address; |
| 176 | |
| 177 | /* |
| 178 | * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within |
| 179 | * ARM_SHARED_RAM region. |
| 180 | */ |
| 181 | assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) && |
| 182 | ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \ |
| 183 | (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE))); |
Soby Mathew | feac8fc | 2015-09-29 15:47:16 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /******************************************************************************* |
| 187 | * The ARM Standard platform definition of platform porting API |
| 188 | * `plat_setup_psci_ops`. |
| 189 | ******************************************************************************/ |
| 190 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 191 | const plat_psci_ops_t **psci_ops) |
| 192 | { |
Soby Mathew | 0b4c5a3 | 2016-10-21 17:51:22 +0100 | [diff] [blame] | 193 | *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] | 194 | |
| 195 | /* Setup mailbox with entry point. */ |
| 196 | arm_program_trusted_mailbox(sec_entrypoint); |
| 197 | return 0; |
| 198 | } |