Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <arch_helpers.h> |
| 32 | #include <assert.h> |
| 33 | #include <debug.h> |
| 34 | #include <gicv2.h> |
| 35 | #include <platform_def.h> |
| 36 | #include <platform.h> |
| 37 | #include <psci.h> |
| 38 | |
| 39 | /* |
| 40 | * The secure entry point to be used on warm reset. |
| 41 | */ |
| 42 | static unsigned long secure_entrypoint; |
| 43 | |
| 44 | /* Make composite power state parameter till power level 0 */ |
| 45 | #if PSCI_EXTENDED_STATE_ID |
| 46 | |
| 47 | #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ |
| 48 | (((lvl0_state) << PSTATE_ID_SHIFT) | \ |
| 49 | ((type) << PSTATE_TYPE_SHIFT)) |
| 50 | #else |
| 51 | #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ |
| 52 | (((lvl0_state) << PSTATE_ID_SHIFT) | \ |
| 53 | ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \ |
| 54 | ((type) << PSTATE_TYPE_SHIFT)) |
| 55 | #endif /* PSCI_EXTENDED_STATE_ID */ |
| 56 | |
| 57 | |
| 58 | #define qemu_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \ |
| 59 | (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \ |
| 60 | qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type)) |
| 61 | |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * The table storing the valid idle power states. Ensure that the |
| 66 | * array entries are populated in ascending order of state-id to |
| 67 | * enable us to use binary search during power state validation. |
| 68 | * The table must be terminated by a NULL entry. |
| 69 | */ |
| 70 | static const unsigned int qemu_pm_idle_states[] = { |
| 71 | /* State-id - 0x01 */ |
| 72 | qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET, |
| 73 | MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY), |
| 74 | /* State-id - 0x02 */ |
| 75 | qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF, |
| 76 | MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN), |
| 77 | /* State-id - 0x22 */ |
| 78 | qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF, |
| 79 | MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN), |
| 80 | 0, |
| 81 | }; |
| 82 | |
| 83 | /******************************************************************************* |
| 84 | * Platform handler called to check the validity of the power state |
| 85 | * parameter. The power state parameter has to be a composite power state. |
| 86 | ******************************************************************************/ |
| 87 | static int qemu_validate_power_state(unsigned int power_state, |
| 88 | psci_power_state_t *req_state) |
| 89 | { |
| 90 | unsigned int state_id; |
| 91 | int i; |
| 92 | |
| 93 | assert(req_state); |
| 94 | |
| 95 | /* |
| 96 | * Currently we are using a linear search for finding the matching |
| 97 | * entry in the idle power state array. This can be made a binary |
| 98 | * search if the number of entries justify the additional complexity. |
| 99 | */ |
| 100 | for (i = 0; !!qemu_pm_idle_states[i]; i++) { |
| 101 | if (power_state == qemu_pm_idle_states[i]) |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | /* Return error if entry not found in the idle state array */ |
| 106 | if (!qemu_pm_idle_states[i]) |
| 107 | return PSCI_E_INVALID_PARAMS; |
| 108 | |
| 109 | i = 0; |
| 110 | state_id = psci_get_pstate_id(power_state); |
| 111 | |
| 112 | /* Parse the State ID and populate the state info parameter */ |
| 113 | while (state_id) { |
| 114 | req_state->pwr_domain_state[i++] = state_id & |
| 115 | PLAT_LOCAL_PSTATE_MASK; |
| 116 | state_id >>= PLAT_LOCAL_PSTATE_WIDTH; |
| 117 | } |
| 118 | |
| 119 | return PSCI_E_SUCCESS; |
| 120 | } |
| 121 | |
| 122 | /******************************************************************************* |
| 123 | * Platform handler called to check the validity of the non secure |
| 124 | * entrypoint. |
| 125 | ******************************************************************************/ |
| 126 | static int qemu_validate_ns_entrypoint(uintptr_t entrypoint) |
| 127 | { |
| 128 | /* |
| 129 | * Check if the non secure entrypoint lies within the non |
| 130 | * secure DRAM. |
| 131 | */ |
| 132 | if ((entrypoint >= NS_DRAM0_BASE) && |
| 133 | (entrypoint < (NS_DRAM0_BASE + NS_DRAM0_SIZE))) |
| 134 | return PSCI_E_SUCCESS; |
| 135 | return PSCI_E_INVALID_ADDRESS; |
| 136 | } |
| 137 | |
| 138 | /******************************************************************************* |
| 139 | * Platform handler called when a CPU is about to enter standby. |
| 140 | ******************************************************************************/ |
| 141 | static void qemu_cpu_standby(plat_local_state_t cpu_state) |
| 142 | { |
| 143 | |
| 144 | assert(cpu_state == PLAT_LOCAL_STATE_RET); |
| 145 | |
| 146 | /* |
| 147 | * Enter standby state |
| 148 | * dsb is good practice before using wfi to enter low power states |
| 149 | */ |
| 150 | dsb(); |
| 151 | wfi(); |
| 152 | } |
| 153 | |
| 154 | /******************************************************************************* |
| 155 | * Platform handler called when a power domain is about to be turned on. The |
| 156 | * mpidr determines the CPU to be turned on. |
| 157 | ******************************************************************************/ |
| 158 | static int qemu_pwr_domain_on(u_register_t mpidr) |
| 159 | { |
| 160 | int rc = PSCI_E_SUCCESS; |
| 161 | unsigned pos = plat_core_pos_by_mpidr(mpidr); |
| 162 | uint64_t *hold_base = (uint64_t *)PLAT_QEMU_HOLD_BASE; |
| 163 | |
| 164 | hold_base[pos] = PLAT_QEMU_HOLD_STATE_GO; |
| 165 | sev(); |
| 166 | |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | /******************************************************************************* |
| 171 | * Platform handler called when a power domain is about to be turned off. The |
| 172 | * target_state encodes the power state that each level should transition to. |
| 173 | ******************************************************************************/ |
| 174 | void qemu_pwr_domain_off(const psci_power_state_t *target_state) |
| 175 | { |
| 176 | assert(0); |
| 177 | } |
| 178 | |
| 179 | /******************************************************************************* |
| 180 | * Platform handler called when a power domain is about to be suspended. The |
| 181 | * target_state encodes the power state that each level should transition to. |
| 182 | ******************************************************************************/ |
| 183 | void qemu_pwr_domain_suspend(const psci_power_state_t *target_state) |
| 184 | { |
| 185 | assert(0); |
| 186 | } |
| 187 | |
| 188 | /******************************************************************************* |
| 189 | * Platform handler called when a power domain has just been powered on after |
| 190 | * being turned off earlier. The target_state encodes the low power state that |
| 191 | * each level has woken up from. |
| 192 | ******************************************************************************/ |
| 193 | void qemu_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 194 | { |
| 195 | assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] == |
| 196 | PLAT_LOCAL_STATE_OFF); |
| 197 | |
| 198 | /* TODO: This setup is needed only after a cold boot */ |
| 199 | gicv2_pcpu_distif_init(); |
| 200 | |
| 201 | /* Enable the gic cpu interface */ |
| 202 | gicv2_cpuif_enable(); |
| 203 | } |
| 204 | |
| 205 | /******************************************************************************* |
| 206 | * Platform handler called when a power domain has just been powered on after |
| 207 | * having been suspended earlier. The target_state encodes the low power state |
| 208 | * that each level has woken up from. |
| 209 | ******************************************************************************/ |
| 210 | void qemu_pwr_domain_suspend_finish(const psci_power_state_t *target_state) |
| 211 | { |
| 212 | assert(0); |
| 213 | } |
| 214 | |
| 215 | /******************************************************************************* |
| 216 | * Platform handlers to shutdown/reboot the system |
| 217 | ******************************************************************************/ |
| 218 | static void __dead2 qemu_system_off(void) |
| 219 | { |
| 220 | ERROR("QEMU System Off: operation not handled.\n"); |
| 221 | panic(); |
| 222 | } |
| 223 | |
| 224 | static void __dead2 qemu_system_reset(void) |
| 225 | { |
| 226 | ERROR("QEMU System Reset: operation not handled.\n"); |
| 227 | panic(); |
| 228 | } |
| 229 | |
| 230 | static const plat_psci_ops_t plat_qemu_psci_pm_ops = { |
| 231 | .cpu_standby = qemu_cpu_standby, |
| 232 | .pwr_domain_on = qemu_pwr_domain_on, |
| 233 | .pwr_domain_off = qemu_pwr_domain_off, |
| 234 | .pwr_domain_suspend = qemu_pwr_domain_suspend, |
| 235 | .pwr_domain_on_finish = qemu_pwr_domain_on_finish, |
| 236 | .pwr_domain_suspend_finish = qemu_pwr_domain_suspend_finish, |
| 237 | .system_off = qemu_system_off, |
| 238 | .system_reset = qemu_system_reset, |
| 239 | .validate_power_state = qemu_validate_power_state, |
| 240 | .validate_ns_entrypoint = qemu_validate_ns_entrypoint |
| 241 | }; |
| 242 | |
| 243 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 244 | const plat_psci_ops_t **psci_ops) |
| 245 | { |
| 246 | uintptr_t *mailbox = (void *) PLAT_QEMU_TRUSTED_MAILBOX_BASE; |
| 247 | |
| 248 | *mailbox = sec_entrypoint; |
| 249 | secure_entrypoint = (unsigned long) sec_entrypoint; |
| 250 | *psci_ops = &plat_qemu_psci_pm_ops; |
| 251 | |
| 252 | return 0; |
| 253 | } |