Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 1 | /* |
Hongbo Zhang | 32338ec | 2018-04-19 13:06:07 +0800 | [diff] [blame] | 2 | * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved. |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 8 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <arch_helpers.h> |
| 11 | #include <common/debug.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <lib/psci/psci.h> |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 13 | #include <lib/semihosting.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <plat/common/platform.h> |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 15 | #include <drivers/gpio.h> |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 16 | |
Hongbo Zhang | 32338ec | 2018-04-19 13:06:07 +0800 | [diff] [blame] | 17 | #include "qemu_private.h" |
| 18 | |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 19 | #define ADP_STOPPED_APPLICATION_EXIT 0x20026 |
| 20 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 21 | /* |
| 22 | * The secure entry point to be used on warm reset. |
| 23 | */ |
| 24 | static unsigned long secure_entrypoint; |
| 25 | |
| 26 | /* Make composite power state parameter till power level 0 */ |
| 27 | #if PSCI_EXTENDED_STATE_ID |
| 28 | |
| 29 | #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ |
| 30 | (((lvl0_state) << PSTATE_ID_SHIFT) | \ |
| 31 | ((type) << PSTATE_TYPE_SHIFT)) |
| 32 | #else |
| 33 | #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ |
| 34 | (((lvl0_state) << PSTATE_ID_SHIFT) | \ |
| 35 | ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \ |
| 36 | ((type) << PSTATE_TYPE_SHIFT)) |
| 37 | #endif /* PSCI_EXTENDED_STATE_ID */ |
| 38 | |
| 39 | |
| 40 | #define qemu_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \ |
| 41 | (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \ |
| 42 | qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type)) |
| 43 | |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * The table storing the valid idle power states. Ensure that the |
| 48 | * array entries are populated in ascending order of state-id to |
| 49 | * enable us to use binary search during power state validation. |
| 50 | * The table must be terminated by a NULL entry. |
| 51 | */ |
| 52 | static const unsigned int qemu_pm_idle_states[] = { |
| 53 | /* State-id - 0x01 */ |
| 54 | qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET, |
| 55 | MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY), |
| 56 | /* State-id - 0x02 */ |
| 57 | qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF, |
| 58 | MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN), |
| 59 | /* State-id - 0x22 */ |
| 60 | qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF, |
| 61 | MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN), |
| 62 | 0, |
| 63 | }; |
| 64 | |
| 65 | /******************************************************************************* |
| 66 | * Platform handler called to check the validity of the power state |
| 67 | * parameter. The power state parameter has to be a composite power state. |
| 68 | ******************************************************************************/ |
| 69 | static int qemu_validate_power_state(unsigned int power_state, |
| 70 | psci_power_state_t *req_state) |
| 71 | { |
| 72 | unsigned int state_id; |
| 73 | int i; |
| 74 | |
| 75 | assert(req_state); |
| 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; !!qemu_pm_idle_states[i]; i++) { |
| 83 | if (power_state == qemu_pm_idle_states[i]) |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | /* Return error if entry not found in the idle state array */ |
| 88 | if (!qemu_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 | PLAT_LOCAL_PSTATE_MASK; |
| 98 | state_id >>= PLAT_LOCAL_PSTATE_WIDTH; |
| 99 | } |
| 100 | |
| 101 | return PSCI_E_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | /******************************************************************************* |
| 105 | * Platform handler called to check the validity of the non secure |
| 106 | * entrypoint. |
| 107 | ******************************************************************************/ |
| 108 | static int qemu_validate_ns_entrypoint(uintptr_t entrypoint) |
| 109 | { |
| 110 | /* |
| 111 | * Check if the non secure entrypoint lies within the non |
| 112 | * secure DRAM. |
| 113 | */ |
| 114 | if ((entrypoint >= NS_DRAM0_BASE) && |
| 115 | (entrypoint < (NS_DRAM0_BASE + NS_DRAM0_SIZE))) |
| 116 | return PSCI_E_SUCCESS; |
| 117 | return PSCI_E_INVALID_ADDRESS; |
| 118 | } |
| 119 | |
| 120 | /******************************************************************************* |
| 121 | * Platform handler called when a CPU is about to enter standby. |
| 122 | ******************************************************************************/ |
| 123 | static void qemu_cpu_standby(plat_local_state_t cpu_state) |
| 124 | { |
| 125 | |
| 126 | assert(cpu_state == PLAT_LOCAL_STATE_RET); |
| 127 | |
| 128 | /* |
| 129 | * Enter standby state |
| 130 | * dsb is good practice before using wfi to enter low power states |
| 131 | */ |
| 132 | dsb(); |
| 133 | wfi(); |
| 134 | } |
| 135 | |
| 136 | /******************************************************************************* |
| 137 | * Platform handler called when a power domain is about to be turned on. The |
| 138 | * mpidr determines the CPU to be turned on. |
| 139 | ******************************************************************************/ |
| 140 | static int qemu_pwr_domain_on(u_register_t mpidr) |
| 141 | { |
| 142 | int rc = PSCI_E_SUCCESS; |
| 143 | unsigned pos = plat_core_pos_by_mpidr(mpidr); |
| 144 | uint64_t *hold_base = (uint64_t *)PLAT_QEMU_HOLD_BASE; |
| 145 | |
| 146 | hold_base[pos] = PLAT_QEMU_HOLD_STATE_GO; |
| 147 | sev(); |
| 148 | |
| 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | /******************************************************************************* |
| 153 | * Platform handler called when a power domain is about to be turned off. The |
| 154 | * target_state encodes the power state that each level should transition to. |
| 155 | ******************************************************************************/ |
Andrew Walbran | 8fe72b9 | 2020-01-23 16:22:44 +0000 | [diff] [blame] | 156 | static void qemu_pwr_domain_off(const psci_power_state_t *target_state) |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 157 | { |
Andrew Walbran | 8fe72b9 | 2020-01-23 16:22:44 +0000 | [diff] [blame] | 158 | qemu_pwr_gic_off(); |
| 159 | } |
| 160 | |
| 161 | void __dead2 plat_secondary_cold_boot_setup(void); |
| 162 | |
| 163 | static void __dead2 |
| 164 | qemu_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state) |
| 165 | { |
| 166 | disable_mmu_el3(); |
| 167 | plat_secondary_cold_boot_setup(); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /******************************************************************************* |
| 171 | * Platform handler called when a power domain is about to be suspended. The |
| 172 | * target_state encodes the power state that each level should transition to. |
| 173 | ******************************************************************************/ |
| 174 | void qemu_pwr_domain_suspend(const psci_power_state_t *target_state) |
| 175 | { |
| 176 | assert(0); |
| 177 | } |
| 178 | |
| 179 | /******************************************************************************* |
| 180 | * Platform handler called when a power domain has just been powered on after |
| 181 | * being turned off earlier. The target_state encodes the low power state that |
| 182 | * each level has woken up from. |
| 183 | ******************************************************************************/ |
| 184 | void qemu_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 185 | { |
| 186 | assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] == |
| 187 | PLAT_LOCAL_STATE_OFF); |
| 188 | |
Hongbo Zhang | 32338ec | 2018-04-19 13:06:07 +0800 | [diff] [blame] | 189 | qemu_pwr_gic_on_finish(); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /******************************************************************************* |
| 193 | * Platform handler called when a power domain has just been powered on after |
| 194 | * having been suspended earlier. The target_state encodes the low power state |
| 195 | * that each level has woken up from. |
| 196 | ******************************************************************************/ |
| 197 | void qemu_pwr_domain_suspend_finish(const psci_power_state_t *target_state) |
| 198 | { |
| 199 | assert(0); |
| 200 | } |
| 201 | |
| 202 | /******************************************************************************* |
| 203 | * Platform handlers to shutdown/reboot the system |
| 204 | ******************************************************************************/ |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 205 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 206 | static void __dead2 qemu_system_off(void) |
| 207 | { |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 208 | #ifdef SECURE_GPIO_BASE |
| 209 | ERROR("QEMU System Power off: with GPIO.\n"); |
| 210 | gpio_set_direction(SECURE_GPIO_POWEROFF, GPIO_DIR_OUT); |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 211 | gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_LOW); |
Maxim Uvarov | f8848b4 | 2021-07-08 11:59:18 +0300 | [diff] [blame] | 212 | gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_HIGH); |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 213 | #else |
Andrew Walbran | f3a6839 | 2020-01-15 14:18:04 +0000 | [diff] [blame] | 214 | semihosting_exit(ADP_STOPPED_APPLICATION_EXIT, 0); |
| 215 | ERROR("QEMU System Off: semihosting call unexpectedly returned.\n"); |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 216 | #endif |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 217 | panic(); |
| 218 | } |
| 219 | |
| 220 | static void __dead2 qemu_system_reset(void) |
| 221 | { |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 222 | ERROR("QEMU System Reset: with GPIO.\n"); |
| 223 | #ifdef SECURE_GPIO_BASE |
| 224 | gpio_set_direction(SECURE_GPIO_RESET, GPIO_DIR_OUT); |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 225 | gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_LOW); |
Maxim Uvarov | f8848b4 | 2021-07-08 11:59:18 +0300 | [diff] [blame] | 226 | gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_HIGH); |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 227 | #else |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 228 | ERROR("QEMU System Reset: operation not handled.\n"); |
Maxim Uvarov | a0b85c2 | 2020-12-14 10:17:44 +0000 | [diff] [blame] | 229 | #endif |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 230 | panic(); |
| 231 | } |
| 232 | |
| 233 | static const plat_psci_ops_t plat_qemu_psci_pm_ops = { |
| 234 | .cpu_standby = qemu_cpu_standby, |
| 235 | .pwr_domain_on = qemu_pwr_domain_on, |
| 236 | .pwr_domain_off = qemu_pwr_domain_off, |
Andrew Walbran | 8fe72b9 | 2020-01-23 16:22:44 +0000 | [diff] [blame] | 237 | .pwr_domain_pwr_down_wfi = qemu_pwr_domain_pwr_down_wfi, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 238 | .pwr_domain_suspend = qemu_pwr_domain_suspend, |
| 239 | .pwr_domain_on_finish = qemu_pwr_domain_on_finish, |
| 240 | .pwr_domain_suspend_finish = qemu_pwr_domain_suspend_finish, |
| 241 | .system_off = qemu_system_off, |
| 242 | .system_reset = qemu_system_reset, |
| 243 | .validate_power_state = qemu_validate_power_state, |
| 244 | .validate_ns_entrypoint = qemu_validate_ns_entrypoint |
| 245 | }; |
| 246 | |
| 247 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 248 | const plat_psci_ops_t **psci_ops) |
| 249 | { |
| 250 | uintptr_t *mailbox = (void *) PLAT_QEMU_TRUSTED_MAILBOX_BASE; |
| 251 | |
| 252 | *mailbox = sec_entrypoint; |
| 253 | secure_entrypoint = (unsigned long) sec_entrypoint; |
| 254 | *psci_ops = &plat_qemu_psci_pm_ops; |
| 255 | |
| 256 | return 0; |
| 257 | } |