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