Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 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 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 32 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <assert.h> |
| 34 | #include <bl_common.h> |
| 35 | #include <context.h> |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 36 | #include <context_mgmt.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 37 | #include <debug.h> |
| 38 | #include "psci_private.h" |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 39 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 40 | /* |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 41 | * SPD power management operations, expected to be supplied by the registered |
| 42 | * SPD on successful SP initialization |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 43 | */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 44 | const spd_pm_ops_t *psci_spd_pm; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 45 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 46 | /******************************************************************************* |
| 47 | * Arrays that contains information needs to resume a cpu's execution when woken |
| 48 | * out of suspend or off states. 'psci_ns_einfo_idx' keeps track of the next |
Achin Gupta | a59caa4 | 2013-12-05 14:21:04 +0000 | [diff] [blame] | 49 | * free index in the 'psci_ns_entry_info' & 'psci_suspend_context' arrays. Each |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 50 | * cpu is allocated a single entry in each array during startup. |
| 51 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 52 | suspend_context_t psci_suspend_context[PSCI_NUM_AFFS]; |
| 53 | ns_entry_info_t psci_ns_entry_info[PSCI_NUM_AFFS]; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 54 | unsigned int psci_ns_einfo_idx; |
| 55 | |
| 56 | /******************************************************************************* |
| 57 | * Grand array that holds the platform's topology information for state |
| 58 | * management of affinity instances. Each node (aff_map_node) in the array |
| 59 | * corresponds to an affinity instance e.g. cluster, cpu within an mpidr |
| 60 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 61 | aff_map_node_t psci_aff_map[PSCI_NUM_AFFS] |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 62 | __attribute__ ((section("tzfw_coherent_mem"))); |
| 63 | |
| 64 | /******************************************************************************* |
| 65 | * In a system, a certain number of affinity instances are present at an |
| 66 | * affinity level. The cumulative number of instances across all levels are |
| 67 | * stored in 'psci_aff_map'. The topology tree has been flattenned into this |
| 68 | * array. To retrieve nodes, information about the extents of each affinity |
| 69 | * level i.e. start index and end index needs to be present. 'psci_aff_limits' |
| 70 | * stores this information. |
| 71 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 72 | aff_limits_node_t psci_aff_limits[MPIDR_MAX_AFFLVL + 1]; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 73 | |
| 74 | /******************************************************************************* |
| 75 | * Pointer to functions exported by the platform to complete power mgmt. ops |
| 76 | ******************************************************************************/ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 77 | const plat_pm_ops_t *psci_plat_pm_ops; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 78 | |
| 79 | /******************************************************************************* |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 80 | * Routine to return the maximum affinity level to traverse to after a cpu has |
| 81 | * been physically powered up. It is expected to be called immediately after |
| 82 | * reset from assembler code. It has to find its 'aff_map_node' instead of |
| 83 | * getting it as an argument. |
| 84 | * TODO: Calling psci_get_aff_map_node() with the MMU disabled is slow. Add |
| 85 | * support to allow faster access to the target affinity level. |
| 86 | ******************************************************************************/ |
| 87 | int get_power_on_target_afflvl(unsigned long mpidr) |
| 88 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 89 | aff_map_node_t *node; |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 90 | unsigned int state; |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 91 | int afflvl; |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 92 | |
| 93 | /* Retrieve our node from the topology tree */ |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 94 | node = psci_get_aff_map_node(mpidr & MPIDR_AFFINITY_MASK, |
| 95 | MPIDR_AFFLVL0); |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 96 | assert(node); |
| 97 | |
| 98 | /* |
| 99 | * Return the maximum supported affinity level if this cpu was off. |
| 100 | * Call the handler in the suspend code if this cpu had been suspended. |
| 101 | * Any other state is invalid. |
| 102 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 103 | state = psci_get_state(node); |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 104 | if (state == PSCI_STATE_ON_PENDING) |
| 105 | return get_max_afflvl(); |
| 106 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 107 | if (state == PSCI_STATE_SUSPEND) { |
| 108 | afflvl = psci_get_aff_map_node_suspend_afflvl(node); |
| 109 | assert(afflvl != PSCI_INVALID_DATA); |
| 110 | return afflvl; |
| 111 | } |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 112 | return PSCI_E_INVALID_PARAMS; |
| 113 | } |
| 114 | |
| 115 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 116 | * Simple routine to retrieve the maximum affinity level supported by the |
| 117 | * platform and check that it makes sense. |
| 118 | ******************************************************************************/ |
| 119 | int get_max_afflvl() |
| 120 | { |
| 121 | int aff_lvl; |
| 122 | |
| 123 | aff_lvl = plat_get_max_afflvl(); |
| 124 | assert(aff_lvl <= MPIDR_MAX_AFFLVL && aff_lvl >= MPIDR_AFFLVL0); |
| 125 | |
| 126 | return aff_lvl; |
| 127 | } |
| 128 | |
| 129 | /******************************************************************************* |
| 130 | * Simple routine to set the id of an affinity instance at a given level in the |
| 131 | * mpidr. |
| 132 | ******************************************************************************/ |
| 133 | unsigned long mpidr_set_aff_inst(unsigned long mpidr, |
| 134 | unsigned char aff_inst, |
| 135 | int aff_lvl) |
| 136 | { |
| 137 | unsigned long aff_shift; |
| 138 | |
| 139 | assert(aff_lvl <= MPIDR_AFFLVL3); |
| 140 | |
| 141 | /* |
| 142 | * Decide the number of bits to shift by depending upon |
| 143 | * the affinity level |
| 144 | */ |
| 145 | aff_shift = get_afflvl_shift(aff_lvl); |
| 146 | |
| 147 | /* Clear the existing affinity instance & set the new one*/ |
| 148 | mpidr &= ~(MPIDR_AFFLVL_MASK << aff_shift); |
| 149 | mpidr |= aff_inst << aff_shift; |
| 150 | |
| 151 | return mpidr; |
| 152 | } |
| 153 | |
| 154 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 155 | * This function sanity checks a range of affinity levels. |
| 156 | ******************************************************************************/ |
| 157 | int psci_check_afflvl_range(int start_afflvl, int end_afflvl) |
| 158 | { |
| 159 | /* Sanity check the parameters passed */ |
| 160 | if (end_afflvl > MPIDR_MAX_AFFLVL) |
| 161 | return PSCI_E_INVALID_PARAMS; |
| 162 | |
| 163 | if (start_afflvl < MPIDR_AFFLVL0) |
| 164 | return PSCI_E_INVALID_PARAMS; |
| 165 | |
| 166 | if (end_afflvl < start_afflvl) |
| 167 | return PSCI_E_INVALID_PARAMS; |
| 168 | |
| 169 | return PSCI_E_SUCCESS; |
| 170 | } |
| 171 | |
| 172 | /******************************************************************************* |
| 173 | * This function is passed an array of pointers to affinity level nodes in the |
| 174 | * topology tree for an mpidr. It picks up locks for each affinity level bottom |
| 175 | * up in the range specified. |
| 176 | ******************************************************************************/ |
| 177 | void psci_acquire_afflvl_locks(unsigned long mpidr, |
| 178 | int start_afflvl, |
| 179 | int end_afflvl, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 180 | mpidr_aff_map_nodes_t mpidr_nodes) |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 181 | { |
| 182 | int level; |
| 183 | |
| 184 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 185 | if (mpidr_nodes[level] == NULL) |
| 186 | continue; |
| 187 | bakery_lock_get(mpidr, &mpidr_nodes[level]->lock); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /******************************************************************************* |
| 192 | * This function is passed an array of pointers to affinity level nodes in the |
| 193 | * topology tree for an mpidr. It releases the lock for each affinity level top |
| 194 | * down in the range specified. |
| 195 | ******************************************************************************/ |
| 196 | void psci_release_afflvl_locks(unsigned long mpidr, |
| 197 | int start_afflvl, |
| 198 | int end_afflvl, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 199 | mpidr_aff_map_nodes_t mpidr_nodes) |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 200 | { |
| 201 | int level; |
| 202 | |
| 203 | for (level = end_afflvl; level >= start_afflvl; level--) { |
| 204 | if (mpidr_nodes[level] == NULL) |
| 205 | continue; |
| 206 | bakery_lock_release(mpidr, &mpidr_nodes[level]->lock); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 211 | * Simple routine to determine whether an affinity instance at a given level |
| 212 | * in an mpidr exists or not. |
| 213 | ******************************************************************************/ |
| 214 | int psci_validate_mpidr(unsigned long mpidr, int level) |
| 215 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 216 | aff_map_node_t *node; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 217 | |
| 218 | node = psci_get_aff_map_node(mpidr, level); |
| 219 | if (node && (node->state & PSCI_AFF_PRESENT)) |
| 220 | return PSCI_E_SUCCESS; |
| 221 | else |
| 222 | return PSCI_E_INVALID_PARAMS; |
| 223 | } |
| 224 | |
| 225 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 226 | * This function retrieves all the stashed information needed to correctly |
| 227 | * resume a cpu's execution in the non-secure state after it has been physically |
| 228 | * powered on i.e. turned ON or resumed from SUSPEND |
| 229 | ******************************************************************************/ |
Achin Gupta | c8afc78 | 2013-11-25 18:45:02 +0000 | [diff] [blame] | 230 | void psci_get_ns_entry_info(unsigned int index) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 231 | { |
| 232 | unsigned long sctlr = 0, scr, el_status, id_aa64pfr0; |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 233 | uint64_t mpidr = read_mpidr(); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 234 | cpu_context_t *ns_entry_context; |
| 235 | gp_regs_t *ns_entry_gpregs; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 236 | |
| 237 | scr = read_scr(); |
| 238 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 239 | /* Find out which EL we are going to */ |
| 240 | id_aa64pfr0 = read_id_aa64pfr0_el1(); |
| 241 | el_status = (id_aa64pfr0 >> ID_AA64PFR0_EL2_SHIFT) & |
| 242 | ID_AA64PFR0_ELX_MASK; |
| 243 | |
| 244 | /* Restore endianess */ |
| 245 | if (psci_ns_entry_info[index].sctlr & SCTLR_EE_BIT) |
| 246 | sctlr |= SCTLR_EE_BIT; |
| 247 | else |
| 248 | sctlr &= ~SCTLR_EE_BIT; |
| 249 | |
| 250 | /* Turn off MMU and Caching */ |
| 251 | sctlr &= ~(SCTLR_M_BIT | SCTLR_C_BIT | SCTLR_M_BIT); |
| 252 | |
| 253 | /* Set the register width */ |
| 254 | if (psci_ns_entry_info[index].scr & SCR_RW_BIT) |
| 255 | scr |= SCR_RW_BIT; |
| 256 | else |
| 257 | scr &= ~SCR_RW_BIT; |
| 258 | |
| 259 | scr |= SCR_NS_BIT; |
| 260 | |
| 261 | if (el_status) |
| 262 | write_sctlr_el2(sctlr); |
| 263 | else |
| 264 | write_sctlr_el1(sctlr); |
| 265 | |
| 266 | /* Fulfill the cpu_on entry reqs. as per the psci spec */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 267 | ns_entry_context = (cpu_context_t *) cm_get_context(mpidr, NON_SECURE); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 268 | assert(ns_entry_context); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 269 | |
Achin Gupta | c8afc78 | 2013-11-25 18:45:02 +0000 | [diff] [blame] | 270 | /* |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 271 | * Setup general purpose registers to return the context id and |
| 272 | * prevent leakage of secure information into the normal world. |
Achin Gupta | c8afc78 | 2013-11-25 18:45:02 +0000 | [diff] [blame] | 273 | */ |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 274 | ns_entry_gpregs = get_gpregs_ctx(ns_entry_context); |
| 275 | write_ctx_reg(ns_entry_gpregs, |
| 276 | CTX_GPREG_X0, |
| 277 | psci_ns_entry_info[index].context_id); |
| 278 | |
| 279 | /* |
| 280 | * Tell the context management library to setup EL3 system registers to |
| 281 | * be able to ERET into the ns state, and SP_EL3 points to the right |
| 282 | * context to exit from EL3 correctly. |
| 283 | */ |
| 284 | cm_set_el3_eret_context(NON_SECURE, |
| 285 | psci_ns_entry_info[index].eret_info.entrypoint, |
| 286 | psci_ns_entry_info[index].eret_info.spsr, |
| 287 | scr); |
| 288 | |
| 289 | cm_set_next_eret_context(NON_SECURE); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /******************************************************************************* |
| 293 | * This function retrieves and stashes all the information needed to correctly |
| 294 | * resume a cpu's execution in the non-secure state after it has been physically |
| 295 | * powered on i.e. turned ON or resumed from SUSPEND. This is done prior to |
| 296 | * turning it on or before suspending it. |
| 297 | ******************************************************************************/ |
| 298 | int psci_set_ns_entry_info(unsigned int index, |
| 299 | unsigned long entrypoint, |
| 300 | unsigned long context_id) |
| 301 | { |
| 302 | int rc = PSCI_E_SUCCESS; |
| 303 | unsigned int rw, mode, ee, spsr = 0; |
| 304 | unsigned long id_aa64pfr0 = read_id_aa64pfr0_el1(), scr = read_scr(); |
| 305 | unsigned long el_status; |
Vikram Kanigiri | 9851e42 | 2014-05-13 14:42:08 +0100 | [diff] [blame] | 306 | unsigned long daif; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 307 | |
| 308 | /* Figure out what mode do we enter the non-secure world in */ |
| 309 | el_status = (id_aa64pfr0 >> ID_AA64PFR0_EL2_SHIFT) & |
| 310 | ID_AA64PFR0_ELX_MASK; |
| 311 | |
| 312 | /* |
| 313 | * Figure out whether the cpu enters the non-secure address space |
| 314 | * in aarch32 or aarch64 |
| 315 | */ |
| 316 | rw = scr & SCR_RW_BIT; |
| 317 | if (rw) { |
| 318 | |
| 319 | /* |
| 320 | * Check whether a Thumb entry point has been provided for an |
| 321 | * aarch64 EL |
| 322 | */ |
| 323 | if (entrypoint & 0x1) |
| 324 | return PSCI_E_INVALID_PARAMS; |
| 325 | |
| 326 | if (el_status && (scr & SCR_HCE_BIT)) { |
| 327 | mode = MODE_EL2; |
| 328 | ee = read_sctlr_el2() & SCTLR_EE_BIT; |
| 329 | } else { |
| 330 | mode = MODE_EL1; |
| 331 | ee = read_sctlr_el1() & SCTLR_EE_BIT; |
| 332 | } |
| 333 | |
Vikram Kanigiri | 9851e42 | 2014-05-13 14:42:08 +0100 | [diff] [blame] | 334 | spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 335 | |
| 336 | psci_ns_entry_info[index].sctlr |= ee; |
| 337 | psci_ns_entry_info[index].scr |= SCR_RW_BIT; |
| 338 | } else { |
| 339 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 340 | |
| 341 | if (el_status && (scr & SCR_HCE_BIT)) { |
Vikram Kanigiri | 9851e42 | 2014-05-13 14:42:08 +0100 | [diff] [blame] | 342 | mode = MODE32_hyp; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 343 | ee = read_sctlr_el2() & SCTLR_EE_BIT; |
| 344 | } else { |
Vikram Kanigiri | 9851e42 | 2014-05-13 14:42:08 +0100 | [diff] [blame] | 345 | mode = MODE32_svc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 346 | ee = read_sctlr_el1() & SCTLR_EE_BIT; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * TODO: Choose async. exception bits if HYP mode is not |
| 351 | * implemented according to the values of SCR.{AW, FW} bits |
| 352 | */ |
Vikram Kanigiri | 9851e42 | 2014-05-13 14:42:08 +0100 | [diff] [blame] | 353 | daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT; |
| 354 | |
| 355 | spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 356 | |
| 357 | /* Ensure that the CSPR.E and SCTLR.EE bits match */ |
| 358 | psci_ns_entry_info[index].sctlr |= ee; |
| 359 | psci_ns_entry_info[index].scr &= ~SCR_RW_BIT; |
| 360 | } |
| 361 | |
| 362 | psci_ns_entry_info[index].eret_info.entrypoint = entrypoint; |
| 363 | psci_ns_entry_info[index].eret_info.spsr = spsr; |
| 364 | psci_ns_entry_info[index].context_id = context_id; |
| 365 | |
| 366 | return rc; |
| 367 | } |
| 368 | |
| 369 | /******************************************************************************* |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 370 | * This function takes a pointer to an affinity node in the topology tree and |
| 371 | * returns its state. State of a non-leaf node needs to be calculated. |
| 372 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 373 | unsigned short psci_get_state(aff_map_node_t *node) |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 374 | { |
| 375 | assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL); |
| 376 | |
| 377 | /* A cpu node just contains the state which can be directly returned */ |
| 378 | if (node->level == MPIDR_AFFLVL0) |
| 379 | return (node->state >> PSCI_STATE_SHIFT) & PSCI_STATE_MASK; |
| 380 | |
| 381 | /* |
| 382 | * For an affinity level higher than a cpu, the state has to be |
| 383 | * calculated. It depends upon the value of the reference count |
| 384 | * which is managed by each node at the next lower affinity level |
| 385 | * e.g. for a cluster, each cpu increments/decrements the reference |
| 386 | * count. If the reference count is 0 then the affinity level is |
| 387 | * OFF else ON. |
| 388 | */ |
| 389 | if (node->ref_count) |
| 390 | return PSCI_STATE_ON; |
| 391 | else |
| 392 | return PSCI_STATE_OFF; |
| 393 | } |
| 394 | |
| 395 | /******************************************************************************* |
| 396 | * This function takes a pointer to an affinity node in the topology tree and |
| 397 | * a target state. State of a non-leaf node needs to be converted to a reference |
| 398 | * count. State of a leaf node can be set directly. |
| 399 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 400 | void psci_set_state(aff_map_node_t *node, unsigned short state) |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 401 | { |
| 402 | assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL); |
| 403 | |
| 404 | /* |
| 405 | * For an affinity level higher than a cpu, the state is used |
| 406 | * to decide whether the reference count is incremented or |
| 407 | * decremented. Entry into the ON_PENDING state does not have |
| 408 | * effect. |
| 409 | */ |
| 410 | if (node->level > MPIDR_AFFLVL0) { |
| 411 | switch (state) { |
| 412 | case PSCI_STATE_ON: |
| 413 | node->ref_count++; |
| 414 | break; |
| 415 | case PSCI_STATE_OFF: |
| 416 | case PSCI_STATE_SUSPEND: |
| 417 | node->ref_count--; |
| 418 | break; |
| 419 | case PSCI_STATE_ON_PENDING: |
| 420 | /* |
| 421 | * An affinity level higher than a cpu will not undergo |
| 422 | * a state change when it is about to be turned on |
| 423 | */ |
| 424 | return; |
| 425 | default: |
| 426 | assert(0); |
| 427 | } |
| 428 | } else { |
| 429 | node->state &= ~(PSCI_STATE_MASK << PSCI_STATE_SHIFT); |
| 430 | node->state |= (state & PSCI_STATE_MASK) << PSCI_STATE_SHIFT; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 435 | * An affinity level could be on, on_pending, suspended or off. These are the |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 436 | * logical states it can be in. Physically either it is off or on. When it is in |
| 437 | * the state on_pending then it is about to be turned on. It is not possible to |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 438 | * tell whether that's actually happenned or not. So we err on the side of |
| 439 | * caution & treat the affinity level as being turned off. |
| 440 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 441 | unsigned short psci_get_phys_state(aff_map_node_t *node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 442 | { |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 443 | unsigned int state; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 444 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 445 | state = psci_get_state(node); |
| 446 | return get_phys_state(state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 450 | * This function takes an array of pointers to affinity instance nodes in the |
| 451 | * topology tree and calls the physical power on handler for the corresponding |
| 452 | * affinity levels |
| 453 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 454 | static int psci_call_power_on_handlers(mpidr_aff_map_nodes_t mpidr_nodes, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 455 | int start_afflvl, |
| 456 | int end_afflvl, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 457 | afflvl_power_on_finisher_t *pon_handlers, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 458 | unsigned long mpidr) |
| 459 | { |
| 460 | int rc = PSCI_E_INVALID_PARAMS, level; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 461 | aff_map_node_t *node; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 462 | |
| 463 | for (level = end_afflvl; level >= start_afflvl; level--) { |
| 464 | node = mpidr_nodes[level]; |
| 465 | if (node == NULL) |
| 466 | continue; |
| 467 | |
| 468 | /* |
| 469 | * If we run into any trouble while powering up an |
| 470 | * affinity instance, then there is no recovery path |
| 471 | * so simply return an error and let the caller take |
| 472 | * care of the situation. |
| 473 | */ |
| 474 | rc = pon_handlers[level](mpidr, node); |
| 475 | if (rc != PSCI_E_SUCCESS) |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | return rc; |
| 480 | } |
| 481 | |
| 482 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 483 | * Generic handler which is called when a cpu is physically powered on. It |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 484 | * traverses through all the affinity levels performing generic, architectural, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 485 | * platform setup and state management e.g. for a cluster that's been powered |
| 486 | * on, it will call the platform specific code which will enable coherency at |
| 487 | * the interconnect level. For a cpu it could mean turning on the MMU etc. |
| 488 | * |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 489 | * The state of all the relevant affinity levels is changed after calling the |
| 490 | * affinity level specific handlers as their actions would depend upon the state |
| 491 | * the affinity level is exiting from. |
| 492 | * |
| 493 | * The affinity level specific handlers are called in descending order i.e. from |
| 494 | * the highest to the lowest affinity level implemented by the platform because |
| 495 | * to turn on affinity level X it is neccesary to turn on affinity level X + 1 |
| 496 | * first. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 497 | * |
| 498 | * CAUTION: This function is called with coherent stacks so that coherency and |
| 499 | * the mmu can be turned on safely. |
| 500 | ******************************************************************************/ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 501 | void psci_afflvl_power_on_finish(unsigned long mpidr, |
| 502 | int start_afflvl, |
| 503 | int end_afflvl, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 504 | afflvl_power_on_finisher_t *pon_handlers) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 505 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 506 | mpidr_aff_map_nodes_t mpidr_nodes; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 507 | int rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 508 | |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 509 | mpidr &= MPIDR_AFFINITY_MASK; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 510 | |
| 511 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 512 | * Collect the pointers to the nodes in the topology tree for |
| 513 | * each affinity instance in the mpidr. If this function does |
| 514 | * not return successfully then either the mpidr or the affinity |
| 515 | * levels are incorrect. Either case is an irrecoverable error. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 516 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 517 | rc = psci_get_aff_map_nodes(mpidr, |
| 518 | start_afflvl, |
| 519 | end_afflvl, |
| 520 | mpidr_nodes); |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 521 | if (rc != PSCI_E_SUCCESS) |
| 522 | panic(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 523 | |
| 524 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 525 | * This function acquires the lock corresponding to each affinity |
| 526 | * level so that by the time all locks are taken, the system topology |
| 527 | * is snapshot and state management can be done safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 528 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 529 | psci_acquire_afflvl_locks(mpidr, |
| 530 | start_afflvl, |
| 531 | end_afflvl, |
| 532 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 533 | |
| 534 | /* Perform generic, architecture and platform specific handling */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 535 | rc = psci_call_power_on_handlers(mpidr_nodes, |
| 536 | start_afflvl, |
| 537 | end_afflvl, |
| 538 | pon_handlers, |
| 539 | mpidr); |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 540 | if (rc != PSCI_E_SUCCESS) |
| 541 | panic(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 542 | |
| 543 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 544 | * This loop releases the lock corresponding to each affinity level |
| 545 | * in the reverse order to which they were acquired. |
| 546 | */ |
| 547 | psci_release_afflvl_locks(mpidr, |
| 548 | start_afflvl, |
| 549 | end_afflvl, |
| 550 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 551 | } |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 552 | |
| 553 | /******************************************************************************* |
| 554 | * This function initializes the set of hooks that PSCI invokes as part of power |
| 555 | * management operation. The power management hooks are expected to be provided |
| 556 | * by the SPD, after it finishes all its initialization |
| 557 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 558 | void psci_register_spd_pm_hook(const spd_pm_ops_t *pm) |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 559 | { |
| 560 | psci_spd_pm = pm; |
| 561 | } |