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