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