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> |
Achin Gupta | 0a9f747 | 2014-02-09 17:48:12 +0000 | [diff] [blame] | 34 | #include <debug.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 35 | #include <arch_helpers.h> |
| 36 | #include <console.h> |
| 37 | #include <platform.h> |
| 38 | #include <psci.h> |
| 39 | #include <psci_private.h> |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 40 | #include <context_mgmt.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 41 | |
| 42 | typedef int (*afflvl_suspend_handler)(unsigned long, |
| 43 | aff_map_node *, |
| 44 | unsigned long, |
| 45 | unsigned long, |
| 46 | unsigned int); |
| 47 | |
| 48 | /******************************************************************************* |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 49 | * This function sets the affinity level till which the current cpu is being |
| 50 | * powered down to during a cpu_suspend call |
| 51 | ******************************************************************************/ |
| 52 | void psci_set_suspend_afflvl(aff_map_node *node, int afflvl) |
| 53 | { |
| 54 | /* |
| 55 | * Check that nobody else is calling this function on our behalf & |
| 56 | * this information is being set only in the cpu node |
| 57 | */ |
| 58 | assert(node->mpidr == (read_mpidr() & MPIDR_AFFINITY_MASK)); |
| 59 | assert(node->level == MPIDR_AFFLVL0); |
| 60 | |
| 61 | /* |
| 62 | * Store the affinity level we are powering down to in our context. |
| 63 | * The cache flush in the suspend code will ensure that this info |
| 64 | * is available immediately upon resuming. |
| 65 | */ |
| 66 | psci_suspend_context[node->data].suspend_level = afflvl; |
| 67 | } |
| 68 | |
| 69 | /******************************************************************************* |
| 70 | * This function gets the affinity level till which the current cpu was powered |
| 71 | * down during a cpu_suspend call. |
| 72 | ******************************************************************************/ |
| 73 | int psci_get_suspend_afflvl(aff_map_node *node) |
| 74 | { |
| 75 | /* Return the target affinity level */ |
| 76 | return psci_suspend_context[node->data].suspend_level; |
| 77 | } |
| 78 | |
| 79 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 80 | * The next three functions implement a handler for each supported affinity |
| 81 | * level which is called when that affinity level is about to be suspended. |
| 82 | ******************************************************************************/ |
| 83 | static int psci_afflvl0_suspend(unsigned long mpidr, |
| 84 | aff_map_node *cpu_node, |
| 85 | unsigned long ns_entrypoint, |
| 86 | unsigned long context_id, |
| 87 | unsigned int power_state) |
| 88 | { |
| 89 | unsigned int index, plat_state; |
| 90 | unsigned long psci_entrypoint, sctlr = read_sctlr(); |
Achin Gupta | 0a9f747 | 2014-02-09 17:48:12 +0000 | [diff] [blame] | 91 | el3_state *saved_el3_state; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 92 | int rc = PSCI_E_SUCCESS; |
| 93 | |
| 94 | /* Sanity check to safeguard against data corruption */ |
| 95 | assert(cpu_node->level == MPIDR_AFFLVL0); |
| 96 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 97 | /* |
| 98 | * Generic management: Store the re-entry information for the non-secure |
| 99 | * world and allow the secure world to suspend itself |
| 100 | */ |
| 101 | |
| 102 | /* |
| 103 | * Call the cpu suspend handler registered by the Secure Payload |
| 104 | * Dispatcher to let it do any bookeeping. If the handler encounters an |
| 105 | * error, it's expected to assert within |
| 106 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 107 | if (psci_spd_pm && psci_spd_pm->svc_suspend) |
| 108 | psci_spd_pm->svc_suspend(power_state); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 109 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 110 | /* State management: mark this cpu as suspended */ |
| 111 | psci_set_state(cpu_node, PSCI_STATE_SUSPEND); |
| 112 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 113 | /* |
| 114 | * Generic management: Store the re-entry information for the |
| 115 | * non-secure world |
| 116 | */ |
| 117 | index = cpu_node->data; |
| 118 | rc = psci_set_ns_entry_info(index, ns_entrypoint, context_id); |
| 119 | if (rc != PSCI_E_SUCCESS) |
| 120 | return rc; |
| 121 | |
| 122 | /* |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 123 | * Arch. management: Save the EL3 state in the 'cpu_context' |
| 124 | * structure that has been allocated for this cpu, flush the |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 125 | * L1 caches and exit intra-cluster coherency et al |
| 126 | */ |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 127 | cm_el3_sysregs_context_save(NON_SECURE); |
| 128 | rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 129 | |
Achin Gupta | 0a9f747 | 2014-02-09 17:48:12 +0000 | [diff] [blame] | 130 | /* |
| 131 | * The EL3 state to PoC since it will be accessed after a |
| 132 | * reset with the caches turned off |
| 133 | */ |
| 134 | saved_el3_state = get_el3state_ctx(cm_get_context(mpidr, NON_SECURE)); |
| 135 | flush_dcache_range((uint64_t) saved_el3_state, sizeof(*saved_el3_state)); |
| 136 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 137 | /* Set the secure world (EL3) re-entry point after BL1 */ |
| 138 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
| 139 | |
| 140 | /* |
| 141 | * Arch. management. Perform the necessary steps to flush all |
| 142 | * cpu caches. |
| 143 | * |
| 144 | * TODO: This power down sequence varies across cpus so it needs to be |
| 145 | * abstracted out on the basis of the MIDR like in cpu_reset_handler(). |
| 146 | * Do the bare minimal for the time being. Fix this before porting to |
| 147 | * Cortex models. |
| 148 | */ |
| 149 | sctlr &= ~SCTLR_C_BIT; |
| 150 | write_sctlr(sctlr); |
| 151 | |
| 152 | /* |
| 153 | * CAUTION: This flush to the level of unification makes an assumption |
| 154 | * about the cache hierarchy at affinity level 0 (cpu) in the platform. |
| 155 | * Ideally the platform should tell psci which levels to flush to exit |
| 156 | * coherency. |
| 157 | */ |
| 158 | dcsw_op_louis(DCCISW); |
| 159 | |
| 160 | /* |
| 161 | * Plat. management: Allow the platform to perform the |
| 162 | * necessary actions to turn off this cpu e.g. set the |
| 163 | * platform defined mailbox with the psci entrypoint, |
| 164 | * program the power controller etc. |
| 165 | */ |
| 166 | if (psci_plat_pm_ops->affinst_suspend) { |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 167 | plat_state = psci_get_phys_state(cpu_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 168 | rc = psci_plat_pm_ops->affinst_suspend(mpidr, |
| 169 | psci_entrypoint, |
| 170 | ns_entrypoint, |
| 171 | cpu_node->level, |
| 172 | plat_state); |
| 173 | } |
| 174 | |
| 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | static int psci_afflvl1_suspend(unsigned long mpidr, |
| 179 | aff_map_node *cluster_node, |
| 180 | unsigned long ns_entrypoint, |
| 181 | unsigned long context_id, |
| 182 | unsigned int power_state) |
| 183 | { |
| 184 | int rc = PSCI_E_SUCCESS; |
| 185 | unsigned int plat_state; |
| 186 | unsigned long psci_entrypoint; |
| 187 | |
| 188 | /* Sanity check the cluster level */ |
| 189 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 190 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 191 | /* State management: Decrement the cluster reference count */ |
| 192 | psci_set_state(cluster_node, PSCI_STATE_SUSPEND); |
| 193 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Keep the physical state of this cluster handy to decide |
| 196 | * what action needs to be taken |
| 197 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 198 | plat_state = psci_get_phys_state(cluster_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Arch. management: Flush all levels of caches to PoC if the |
| 202 | * cluster is to be shutdown |
| 203 | */ |
| 204 | if (plat_state == PSCI_STATE_OFF) |
| 205 | dcsw_op_all(DCCISW); |
| 206 | |
| 207 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 208 | * Plat. Management. Allow the platform to do its cluster |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 209 | * specific bookeeping e.g. turn off interconnect coherency, |
| 210 | * program the power controller etc. |
| 211 | */ |
| 212 | if (psci_plat_pm_ops->affinst_suspend) { |
| 213 | |
| 214 | /* |
| 215 | * Sending the psci entrypoint is currently redundant |
| 216 | * beyond affinity level 0 but one never knows what a |
| 217 | * platform might do. Also it allows us to keep the |
| 218 | * platform handler prototype the same. |
| 219 | */ |
| 220 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 221 | rc = psci_plat_pm_ops->affinst_suspend(mpidr, |
| 222 | psci_entrypoint, |
| 223 | ns_entrypoint, |
| 224 | cluster_node->level, |
| 225 | plat_state); |
| 226 | } |
| 227 | |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static int psci_afflvl2_suspend(unsigned long mpidr, |
| 233 | aff_map_node *system_node, |
| 234 | unsigned long ns_entrypoint, |
| 235 | unsigned long context_id, |
| 236 | unsigned int power_state) |
| 237 | { |
| 238 | int rc = PSCI_E_SUCCESS; |
| 239 | unsigned int plat_state; |
| 240 | unsigned long psci_entrypoint; |
| 241 | |
| 242 | /* Cannot go beyond this */ |
| 243 | assert(system_node->level == MPIDR_AFFLVL2); |
| 244 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 245 | /* State management: Decrement the system reference count */ |
| 246 | psci_set_state(system_node, PSCI_STATE_SUSPEND); |
| 247 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 248 | /* |
| 249 | * Keep the physical state of the system handy to decide what |
| 250 | * action needs to be taken |
| 251 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 252 | plat_state = psci_get_phys_state(system_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 253 | |
| 254 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 255 | * Plat. Management : Allow the platform to do its bookeeping |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 256 | * at this affinity level |
| 257 | */ |
| 258 | if (psci_plat_pm_ops->affinst_suspend) { |
| 259 | |
| 260 | /* |
| 261 | * Sending the psci entrypoint is currently redundant |
| 262 | * beyond affinity level 0 but one never knows what a |
| 263 | * platform might do. Also it allows us to keep the |
| 264 | * platform handler prototype the same. |
| 265 | */ |
| 266 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 267 | rc = psci_plat_pm_ops->affinst_suspend(mpidr, |
| 268 | psci_entrypoint, |
| 269 | ns_entrypoint, |
| 270 | system_node->level, |
| 271 | plat_state); |
| 272 | } |
| 273 | |
| 274 | return rc; |
| 275 | } |
| 276 | |
| 277 | static const afflvl_suspend_handler psci_afflvl_suspend_handlers[] = { |
| 278 | psci_afflvl0_suspend, |
| 279 | psci_afflvl1_suspend, |
| 280 | psci_afflvl2_suspend, |
| 281 | }; |
| 282 | |
| 283 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 284 | * This function takes an array of pointers to affinity instance nodes in the |
| 285 | * topology tree and calls the suspend handler for the corresponding affinity |
| 286 | * levels |
| 287 | ******************************************************************************/ |
| 288 | static int psci_call_suspend_handlers(mpidr_aff_map_nodes mpidr_nodes, |
| 289 | int start_afflvl, |
| 290 | int end_afflvl, |
| 291 | unsigned long mpidr, |
| 292 | unsigned long entrypoint, |
| 293 | unsigned long context_id, |
| 294 | unsigned int power_state) |
| 295 | { |
| 296 | int rc = PSCI_E_INVALID_PARAMS, level; |
| 297 | aff_map_node *node; |
| 298 | |
| 299 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 300 | node = mpidr_nodes[level]; |
| 301 | if (node == NULL) |
| 302 | continue; |
| 303 | |
| 304 | /* |
| 305 | * TODO: In case of an error should there be a way |
| 306 | * of restoring what we might have torn down at |
| 307 | * lower affinity levels. |
| 308 | */ |
| 309 | rc = psci_afflvl_suspend_handlers[level](mpidr, |
| 310 | node, |
| 311 | entrypoint, |
| 312 | context_id, |
| 313 | power_state); |
| 314 | if (rc != PSCI_E_SUCCESS) |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | return rc; |
| 319 | } |
| 320 | |
| 321 | /******************************************************************************* |
| 322 | * Top level handler which is called when a cpu wants to suspend its execution. |
| 323 | * It is assumed that along with turning the cpu off, higher affinity levels |
| 324 | * until the target affinity level will be turned off as well. It traverses |
| 325 | * through all the affinity levels performing generic, architectural, platform |
| 326 | * setup and state management e.g. for a cluster that's to be suspended, it will |
| 327 | * call the platform specific code which will disable coherency at the |
| 328 | * interconnect level if the cpu is the last in the cluster. For a cpu it could |
| 329 | * mean programming the power controller etc. |
| 330 | * |
| 331 | * The state of all the relevant affinity levels is changed prior to calling the |
| 332 | * affinity level specific handlers as their actions would depend upon the state |
| 333 | * the affinity level is about to enter. |
| 334 | * |
| 335 | * The affinity level specific handlers are called in ascending order i.e. from |
| 336 | * the lowest to the highest affinity level implemented by the platform because |
| 337 | * to turn off affinity level X it is neccesary to turn off affinity level X - 1 |
| 338 | * first. |
| 339 | * |
| 340 | * CAUTION: This function is called with coherent stacks so that coherency can |
| 341 | * be turned off and caches can be flushed safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 342 | ******************************************************************************/ |
| 343 | int psci_afflvl_suspend(unsigned long mpidr, |
| 344 | unsigned long entrypoint, |
| 345 | unsigned long context_id, |
| 346 | unsigned int power_state, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 347 | int start_afflvl, |
| 348 | int end_afflvl) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 349 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 350 | int rc = PSCI_E_SUCCESS; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 351 | mpidr_aff_map_nodes mpidr_nodes; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 352 | |
| 353 | mpidr &= MPIDR_AFFINITY_MASK; |
| 354 | |
| 355 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 356 | * Collect the pointers to the nodes in the topology tree for |
| 357 | * each affinity instance in the mpidr. If this function does |
| 358 | * not return successfully then either the mpidr or the affinity |
| 359 | * levels are incorrect. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 360 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 361 | rc = psci_get_aff_map_nodes(mpidr, |
| 362 | start_afflvl, |
| 363 | end_afflvl, |
| 364 | mpidr_nodes); |
| 365 | if (rc != PSCI_E_SUCCESS) |
| 366 | return rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 367 | |
| 368 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 369 | * This function acquires the lock corresponding to each affinity |
| 370 | * level so that by the time all locks are taken, the system topology |
| 371 | * is snapshot and state management can be done safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 372 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 373 | psci_acquire_afflvl_locks(mpidr, |
| 374 | start_afflvl, |
| 375 | end_afflvl, |
| 376 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 377 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 378 | |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 379 | /* Save the affinity level till which this cpu can be powered down */ |
| 380 | psci_set_suspend_afflvl(mpidr_nodes[MPIDR_AFFLVL0], end_afflvl); |
| 381 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 382 | /* Perform generic, architecture and platform specific handling */ |
| 383 | rc = psci_call_suspend_handlers(mpidr_nodes, |
| 384 | start_afflvl, |
| 385 | end_afflvl, |
| 386 | mpidr, |
| 387 | entrypoint, |
| 388 | context_id, |
| 389 | power_state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 390 | |
| 391 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 392 | * Release the locks corresponding to each affinity level in the |
| 393 | * reverse order to which they were acquired. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 394 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 395 | psci_release_afflvl_locks(mpidr, |
| 396 | start_afflvl, |
| 397 | end_afflvl, |
| 398 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 399 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 400 | return rc; |
| 401 | } |
| 402 | |
| 403 | /******************************************************************************* |
| 404 | * The following functions finish an earlier affinity suspend request. They |
| 405 | * are called by the common finisher routine in psci_common.c. |
| 406 | ******************************************************************************/ |
| 407 | static unsigned int psci_afflvl0_suspend_finish(unsigned long mpidr, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 408 | aff_map_node *cpu_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 409 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 410 | unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 411 | int32_t suspend_level; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 412 | |
| 413 | assert(cpu_node->level == MPIDR_AFFLVL0); |
| 414 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 415 | /* Ensure we have been woken up from a suspended state */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 416 | state = psci_get_state(cpu_node); |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 417 | assert(state == PSCI_STATE_SUSPEND); |
| 418 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 419 | /* |
| 420 | * Plat. management: Perform the platform specific actions |
| 421 | * before we change the state of the cpu e.g. enabling the |
| 422 | * gic or zeroing the mailbox register. If anything goes |
| 423 | * wrong then assert as there is no way to recover from this |
| 424 | * situation. |
| 425 | */ |
| 426 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 427 | |
| 428 | /* Get the physical state of this cpu */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 429 | plat_state = get_phys_state(state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 430 | rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr, |
| 431 | cpu_node->level, |
| 432 | plat_state); |
| 433 | assert(rc == PSCI_E_SUCCESS); |
| 434 | } |
| 435 | |
| 436 | /* Get the index for restoring the re-entry information */ |
| 437 | index = cpu_node->data; |
| 438 | |
| 439 | /* |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 440 | * Arch. management: Restore the stashed EL3 architectural |
| 441 | * context from the 'cpu_context' structure for this cpu. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 442 | */ |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 443 | cm_el3_sysregs_context_restore(NON_SECURE); |
| 444 | rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 445 | |
| 446 | /* |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 447 | * Use the more complex exception vectors to enable SPD |
| 448 | * initialisation. SP_EL3 should point to a 'cpu_context' |
| 449 | * structure which has an exception stack allocated. The |
| 450 | * non-secure context should have been set on this cpu |
| 451 | * prior to suspension. |
| 452 | */ |
| 453 | assert(cm_get_context(mpidr, NON_SECURE)); |
| 454 | cm_set_next_eret_context(NON_SECURE); |
| 455 | write_vbar_el3((uint64_t) runtime_exceptions); |
| 456 | |
| 457 | /* |
| 458 | * Call the cpu suspend finish handler registered by the Secure Payload |
| 459 | * Dispatcher to let it do any bookeeping. If the handler encounters an |
| 460 | * error, it's expected to assert within |
| 461 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 462 | if (psci_spd_pm && psci_spd_pm->svc_suspend) { |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 463 | suspend_level = psci_get_suspend_afflvl(cpu_node); |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 464 | psci_spd_pm->svc_suspend_finish(suspend_level); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 468 | * Generic management: Now we just need to retrieve the |
| 469 | * information that we had stashed away during the suspend |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 470 | * call to set this cpu on its way. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 471 | */ |
Achin Gupta | c8afc78 | 2013-11-25 18:45:02 +0000 | [diff] [blame] | 472 | psci_get_ns_entry_info(index); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 473 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 474 | /* State management: mark this cpu as on */ |
| 475 | psci_set_state(cpu_node, PSCI_STATE_ON); |
| 476 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 477 | /* Clean caches before re-entering normal world */ |
| 478 | dcsw_op_louis(DCCSW); |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
| 483 | static unsigned int psci_afflvl1_suspend_finish(unsigned long mpidr, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 484 | aff_map_node *cluster_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 485 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 486 | unsigned int plat_state, rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 487 | |
| 488 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 489 | |
| 490 | /* |
| 491 | * Plat. management: Perform the platform specific actions |
| 492 | * as per the old state of the cluster e.g. enabling |
| 493 | * coherency at the interconnect depends upon the state with |
| 494 | * which this cluster was powered up. If anything goes wrong |
| 495 | * then assert as there is no way to recover from this |
| 496 | * situation. |
| 497 | */ |
| 498 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 499 | |
| 500 | /* Get the physical state of this cpu */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 501 | plat_state = psci_get_phys_state(cluster_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 502 | rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr, |
| 503 | cluster_node->level, |
| 504 | plat_state); |
| 505 | assert(rc == PSCI_E_SUCCESS); |
| 506 | } |
| 507 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 508 | /* State management: Increment the cluster reference count */ |
| 509 | psci_set_state(cluster_node, PSCI_STATE_ON); |
| 510 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 511 | return rc; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | static unsigned int psci_afflvl2_suspend_finish(unsigned long mpidr, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 516 | aff_map_node *system_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 517 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 518 | unsigned int plat_state, rc = PSCI_E_SUCCESS;; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 519 | |
| 520 | /* Cannot go beyond this affinity level */ |
| 521 | assert(system_node->level == MPIDR_AFFLVL2); |
| 522 | |
| 523 | /* |
| 524 | * Currently, there are no architectural actions to perform |
| 525 | * at the system level. |
| 526 | */ |
| 527 | |
| 528 | /* |
| 529 | * Plat. management: Perform the platform specific actions |
| 530 | * as per the old state of the cluster e.g. enabling |
| 531 | * coherency at the interconnect depends upon the state with |
| 532 | * which this cluster was powered up. If anything goes wrong |
| 533 | * then assert as there is no way to recover from this |
| 534 | * situation. |
| 535 | */ |
| 536 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 537 | |
| 538 | /* Get the physical state of the system */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 539 | plat_state = psci_get_phys_state(system_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 540 | rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr, |
| 541 | system_node->level, |
| 542 | plat_state); |
| 543 | assert(rc == PSCI_E_SUCCESS); |
| 544 | } |
| 545 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 546 | /* State management: Increment the system reference count */ |
| 547 | psci_set_state(system_node, PSCI_STATE_ON); |
| 548 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 549 | return rc; |
| 550 | } |
| 551 | |
| 552 | const afflvl_power_on_finisher psci_afflvl_suspend_finishers[] = { |
| 553 | psci_afflvl0_suspend_finish, |
| 554 | psci_afflvl1_suspend_finish, |
| 555 | psci_afflvl2_suspend_finish, |
| 556 | }; |
| 557 | |