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