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> |
Soby Mathew | 2ed46e9 | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 37 | #include <platform.h> |
Dan Handley | bcd60ba | 2014-04-17 18:53:42 +0100 | [diff] [blame] | 38 | #include <runtime_svc.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 39 | #include <stddef.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 40 | #include "psci_private.h" |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 41 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 42 | typedef int (*afflvl_suspend_handler_t)(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 | |
| 60 | /* |
Andrew Thoelke | e9a0d11 | 2014-06-20 00:38:03 +0100 | [diff] [blame] | 61 | * Save PSCI power state parameter for the core in suspend context. |
| 62 | * The node is in always-coherent RAM so it does not need to be flushed |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 63 | */ |
Andrew Thoelke | e9a0d11 | 2014-06-20 00:38:03 +0100 | [diff] [blame] | 64 | node->power_state = power_state; |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /******************************************************************************* |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 68 | * This function gets the affinity level till which a cpu is powered down |
| 69 | * during a cpu_suspend call. Returns PSCI_INVALID_DATA if the |
| 70 | * power state saved for the node is invalid |
| 71 | ******************************************************************************/ |
| 72 | int psci_get_suspend_afflvl(unsigned long mpidr) |
| 73 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 74 | aff_map_node_t *node; |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 75 | |
| 76 | node = psci_get_aff_map_node(mpidr & MPIDR_AFFINITY_MASK, |
| 77 | MPIDR_AFFLVL0); |
| 78 | assert(node); |
| 79 | |
| 80 | return psci_get_aff_map_node_suspend_afflvl(node); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /******************************************************************************* |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 85 | * 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] | 86 | * down during a cpu_suspend call. Returns PSCI_INVALID_DATA if the |
| 87 | * power state saved for the node is invalid |
| 88 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 89 | 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] | 90 | { |
| 91 | unsigned int power_state; |
| 92 | |
| 93 | assert(node->level == MPIDR_AFFLVL0); |
| 94 | |
Andrew Thoelke | e9a0d11 | 2014-06-20 00:38:03 +0100 | [diff] [blame] | 95 | power_state = node->power_state; |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 96 | return ((power_state == PSCI_INVALID_DATA) ? |
| 97 | power_state : psci_get_pstate_afflvl(power_state)); |
| 98 | } |
| 99 | |
| 100 | /******************************************************************************* |
| 101 | * This function gets the state id of a cpu stored in suspend context |
| 102 | * while powering down during a cpu_suspend call. Returns 0xFFFFFFFF |
| 103 | * if the power state saved for the node is invalid |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 104 | ******************************************************************************/ |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 105 | int psci_get_suspend_stateid(unsigned long mpidr) |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 106 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 107 | aff_map_node_t *node; |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 108 | unsigned int power_state; |
| 109 | |
| 110 | node = psci_get_aff_map_node(mpidr & MPIDR_AFFINITY_MASK, |
| 111 | MPIDR_AFFLVL0); |
| 112 | assert(node); |
| 113 | assert(node->level == MPIDR_AFFLVL0); |
| 114 | |
Andrew Thoelke | e9a0d11 | 2014-06-20 00:38:03 +0100 | [diff] [blame] | 115 | power_state = node->power_state; |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 116 | return ((power_state == PSCI_INVALID_DATA) ? |
| 117 | power_state : psci_get_pstate_id(power_state)); |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 121 | * The next three functions implement a handler for each supported affinity |
| 122 | * level which is called when that affinity level is about to be suspended. |
| 123 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 124 | static int psci_afflvl0_suspend(aff_map_node_t *cpu_node, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 125 | unsigned long ns_entrypoint, |
| 126 | unsigned long context_id, |
| 127 | unsigned int power_state) |
| 128 | { |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 129 | unsigned int plat_state; |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 130 | unsigned long psci_entrypoint; |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 131 | uint32_t ns_scr_el3 = read_scr_el3(); |
| 132 | uint32_t ns_sctlr_el1 = read_sctlr_el1(); |
| 133 | int rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 134 | |
| 135 | /* Sanity check to safeguard against data corruption */ |
| 136 | assert(cpu_node->level == MPIDR_AFFLVL0); |
| 137 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 138 | /* Save PSCI power state parameter for the core in suspend context */ |
| 139 | psci_set_suspend_power_state(cpu_node, power_state); |
| 140 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 141 | /* |
| 142 | * Generic management: Store the re-entry information for the non-secure |
| 143 | * world and allow the secure world to suspend itself |
| 144 | */ |
| 145 | |
| 146 | /* |
| 147 | * Call the cpu suspend handler registered by the Secure Payload |
| 148 | * Dispatcher to let it do any bookeeping. If the handler encounters an |
| 149 | * error, it's expected to assert within |
| 150 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 151 | if (psci_spd_pm && psci_spd_pm->svc_suspend) |
| 152 | psci_spd_pm->svc_suspend(power_state); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 153 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 154 | /* State management: mark this cpu as suspended */ |
| 155 | psci_set_state(cpu_node, PSCI_STATE_SUSPEND); |
| 156 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 157 | /* |
| 158 | * Generic management: Store the re-entry information for the |
| 159 | * non-secure world |
| 160 | */ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 161 | rc = psci_save_ns_entry(read_mpidr_el1(), ns_entrypoint, context_id, |
| 162 | ns_scr_el3, ns_sctlr_el1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 163 | if (rc != PSCI_E_SUCCESS) |
| 164 | return rc; |
| 165 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 166 | /* Set the secure world (EL3) re-entry point after BL1 */ |
| 167 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
| 168 | |
| 169 | /* |
| 170 | * Arch. management. Perform the necessary steps to flush all |
| 171 | * cpu caches. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 172 | */ |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 173 | psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL0); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 174 | |
| 175 | /* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 176 | * Plat. management: Allow the platform to perform the |
| 177 | * necessary actions to turn off this cpu e.g. set the |
| 178 | * platform defined mailbox with the psci entrypoint, |
| 179 | * program the power controller etc. |
| 180 | */ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 181 | rc = PSCI_E_SUCCESS; |
| 182 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 183 | if (psci_plat_pm_ops->affinst_suspend) { |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 184 | plat_state = psci_get_phys_state(cpu_node); |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 185 | rc = psci_plat_pm_ops->affinst_suspend(read_mpidr_el1(), |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 186 | psci_entrypoint, |
| 187 | ns_entrypoint, |
| 188 | cpu_node->level, |
| 189 | plat_state); |
| 190 | } |
| 191 | |
| 192 | return rc; |
| 193 | } |
| 194 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 195 | static int psci_afflvl1_suspend(aff_map_node_t *cluster_node, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 196 | unsigned long ns_entrypoint, |
| 197 | unsigned long context_id, |
| 198 | unsigned int power_state) |
| 199 | { |
| 200 | int rc = PSCI_E_SUCCESS; |
| 201 | unsigned int plat_state; |
| 202 | unsigned long psci_entrypoint; |
| 203 | |
| 204 | /* Sanity check the cluster level */ |
| 205 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 206 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 207 | /* State management: Decrement the cluster reference count */ |
| 208 | psci_set_state(cluster_node, PSCI_STATE_SUSPEND); |
| 209 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 210 | /* |
| 211 | * Keep the physical state of this cluster handy to decide |
| 212 | * what action needs to be taken |
| 213 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 214 | plat_state = psci_get_phys_state(cluster_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Arch. management: Flush all levels of caches to PoC if the |
| 218 | * cluster is to be shutdown |
| 219 | */ |
| 220 | if (plat_state == PSCI_STATE_OFF) |
| 221 | dcsw_op_all(DCCISW); |
| 222 | |
| 223 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 224 | * Plat. Management. Allow the platform to do its cluster |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 225 | * specific bookeeping e.g. turn off interconnect coherency, |
| 226 | * program the power controller etc. |
| 227 | */ |
| 228 | if (psci_plat_pm_ops->affinst_suspend) { |
| 229 | |
| 230 | /* |
| 231 | * Sending the psci entrypoint is currently redundant |
| 232 | * beyond affinity level 0 but one never knows what a |
| 233 | * platform might do. Also it allows us to keep the |
| 234 | * platform handler prototype the same. |
| 235 | */ |
| 236 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 237 | rc = psci_plat_pm_ops->affinst_suspend(read_mpidr_el1(), |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 238 | psci_entrypoint, |
| 239 | ns_entrypoint, |
| 240 | cluster_node->level, |
| 241 | plat_state); |
| 242 | } |
| 243 | |
| 244 | return rc; |
| 245 | } |
| 246 | |
| 247 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 248 | static int psci_afflvl2_suspend(aff_map_node_t *system_node, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 249 | unsigned long ns_entrypoint, |
| 250 | unsigned long context_id, |
| 251 | unsigned int power_state) |
| 252 | { |
| 253 | int rc = PSCI_E_SUCCESS; |
| 254 | unsigned int plat_state; |
| 255 | unsigned long psci_entrypoint; |
| 256 | |
| 257 | /* Cannot go beyond this */ |
| 258 | assert(system_node->level == MPIDR_AFFLVL2); |
| 259 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 260 | /* State management: Decrement the system reference count */ |
| 261 | psci_set_state(system_node, PSCI_STATE_SUSPEND); |
| 262 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 263 | /* |
| 264 | * Keep the physical state of the system handy to decide what |
| 265 | * action needs to be taken |
| 266 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 267 | plat_state = psci_get_phys_state(system_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 268 | |
| 269 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 270 | * Plat. Management : Allow the platform to do its bookeeping |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 271 | * at this affinity level |
| 272 | */ |
| 273 | if (psci_plat_pm_ops->affinst_suspend) { |
| 274 | |
| 275 | /* |
| 276 | * Sending the psci entrypoint is currently redundant |
| 277 | * beyond affinity level 0 but one never knows what a |
| 278 | * platform might do. Also it allows us to keep the |
| 279 | * platform handler prototype the same. |
| 280 | */ |
| 281 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 282 | rc = psci_plat_pm_ops->affinst_suspend(read_mpidr_el1(), |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 283 | psci_entrypoint, |
| 284 | ns_entrypoint, |
| 285 | system_node->level, |
| 286 | plat_state); |
| 287 | } |
| 288 | |
| 289 | return rc; |
| 290 | } |
| 291 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 292 | static const afflvl_suspend_handler_t psci_afflvl_suspend_handlers[] = { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 293 | psci_afflvl0_suspend, |
| 294 | psci_afflvl1_suspend, |
| 295 | psci_afflvl2_suspend, |
| 296 | }; |
| 297 | |
| 298 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 299 | * This function takes an array of pointers to affinity instance nodes in the |
| 300 | * topology tree and calls the suspend handler for the corresponding affinity |
| 301 | * levels |
| 302 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 303 | 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] | 304 | int start_afflvl, |
| 305 | int end_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 306 | unsigned long entrypoint, |
| 307 | unsigned long context_id, |
| 308 | unsigned int power_state) |
| 309 | { |
| 310 | int rc = PSCI_E_INVALID_PARAMS, level; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 311 | aff_map_node_t *node; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 312 | |
| 313 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 314 | node = mpidr_nodes[level]; |
| 315 | if (node == NULL) |
| 316 | continue; |
| 317 | |
| 318 | /* |
| 319 | * TODO: In case of an error should there be a way |
| 320 | * of restoring what we might have torn down at |
| 321 | * lower affinity levels. |
| 322 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 323 | rc = psci_afflvl_suspend_handlers[level](node, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 324 | entrypoint, |
| 325 | context_id, |
| 326 | power_state); |
| 327 | if (rc != PSCI_E_SUCCESS) |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | return rc; |
| 332 | } |
| 333 | |
| 334 | /******************************************************************************* |
| 335 | * Top level handler which is called when a cpu wants to suspend its execution. |
| 336 | * It is assumed that along with turning the cpu off, higher affinity levels |
| 337 | * until the target affinity level will be turned off as well. It traverses |
| 338 | * through all the affinity levels performing generic, architectural, platform |
| 339 | * setup and state management e.g. for a cluster that's to be suspended, it will |
| 340 | * call the platform specific code which will disable coherency at the |
| 341 | * interconnect level if the cpu is the last in the cluster. For a cpu it could |
| 342 | * mean programming the power controller etc. |
| 343 | * |
| 344 | * The state of all the relevant affinity levels is changed prior to calling the |
| 345 | * affinity level specific handlers as their actions would depend upon the state |
| 346 | * the affinity level is about to enter. |
| 347 | * |
| 348 | * The affinity level specific handlers are called in ascending order i.e. from |
| 349 | * the lowest to the highest affinity level implemented by the platform because |
| 350 | * to turn off affinity level X it is neccesary to turn off affinity level X - 1 |
| 351 | * first. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 352 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 353 | int psci_afflvl_suspend(unsigned long entrypoint, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 354 | unsigned long context_id, |
| 355 | unsigned int power_state, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 356 | int start_afflvl, |
| 357 | int end_afflvl) |
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 | int rc = PSCI_E_SUCCESS; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 360 | mpidr_aff_map_nodes_t mpidr_nodes; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 361 | |
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 | * Collect the pointers to the nodes in the topology tree for |
| 364 | * each affinity instance in the mpidr. If this function does |
| 365 | * not return successfully then either the mpidr or the affinity |
| 366 | * levels are incorrect. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 367 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 368 | rc = psci_get_aff_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 369 | start_afflvl, |
| 370 | end_afflvl, |
| 371 | mpidr_nodes); |
| 372 | if (rc != PSCI_E_SUCCESS) |
| 373 | return rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 374 | |
| 375 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 376 | * This function acquires the lock corresponding to each affinity |
| 377 | * level so that by the time all locks are taken, the system topology |
| 378 | * is snapshot and state management can be done safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 379 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 380 | psci_acquire_afflvl_locks(start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 381 | end_afflvl, |
| 382 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 383 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 384 | /* Perform generic, architecture and platform specific handling */ |
| 385 | rc = psci_call_suspend_handlers(mpidr_nodes, |
| 386 | start_afflvl, |
| 387 | end_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 388 | entrypoint, |
| 389 | context_id, |
| 390 | power_state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 391 | |
| 392 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 393 | * Release the locks corresponding to each affinity level in the |
| 394 | * reverse order to which they were acquired. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 395 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 396 | psci_release_afflvl_locks(start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 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 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 407 | static unsigned int psci_afflvl0_suspend_finish(aff_map_node_t *cpu_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 408 | { |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 409 | unsigned int plat_state, state, rc; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 410 | int32_t suspend_level; |
Soby Mathew | 2ed46e9 | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 411 | uint64_t counter_freq; |
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); |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 430 | rc = psci_plat_pm_ops->affinst_suspend_finish(read_mpidr_el1(), |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 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 */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 437 | /* |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 438 | * Arch. management: Enable the data cache, manage stack memory and |
| 439 | * restore the stashed EL3 architectural context from the 'cpu_context' |
| 440 | * structure for this cpu. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 441 | */ |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 442 | psci_do_pwrup_cache_maintenance(); |
Soby Mathew | 2ed46e9 | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 443 | |
| 444 | /* Re-init the cntfrq_el0 register */ |
| 445 | counter_freq = plat_get_syscnt_freq(); |
| 446 | write_cntfrq_el0(counter_freq); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 447 | |
| 448 | /* |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 449 | * Call the cpu suspend finish handler registered by the Secure Payload |
| 450 | * Dispatcher to let it do any bookeeping. If the handler encounters an |
| 451 | * error, it's expected to assert within |
| 452 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 453 | if (psci_spd_pm && psci_spd_pm->svc_suspend) { |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 454 | suspend_level = psci_get_aff_map_node_suspend_afflvl(cpu_node); |
| 455 | assert (suspend_level != PSCI_INVALID_DATA); |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 456 | psci_spd_pm->svc_suspend_finish(suspend_level); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 459 | /* Invalidate the suspend context for the node */ |
| 460 | psci_set_suspend_power_state(cpu_node, PSCI_INVALID_DATA); |
| 461 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 462 | /* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 463 | * Generic management: Now we just need to retrieve the |
| 464 | * information that we had stashed away during the suspend |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 465 | * call to set this cpu on its way. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 466 | */ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 467 | cm_prepare_el3_exit(NON_SECURE); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 468 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 469 | /* State management: mark this cpu as on */ |
| 470 | psci_set_state(cpu_node, PSCI_STATE_ON); |
| 471 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 472 | /* Clean caches before re-entering normal world */ |
| 473 | dcsw_op_louis(DCCSW); |
| 474 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 475 | rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 476 | return rc; |
| 477 | } |
| 478 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 479 | static unsigned int psci_afflvl1_suspend_finish(aff_map_node_t *cluster_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 480 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 481 | unsigned int plat_state, rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 482 | |
| 483 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 484 | |
| 485 | /* |
| 486 | * Plat. management: Perform the platform specific actions |
| 487 | * as per the old state of the cluster e.g. enabling |
| 488 | * coherency at the interconnect depends upon the state with |
| 489 | * which this cluster was powered up. If anything goes wrong |
| 490 | * then assert as there is no way to recover from this |
| 491 | * situation. |
| 492 | */ |
| 493 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 494 | |
| 495 | /* Get the physical state of this cpu */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 496 | plat_state = psci_get_phys_state(cluster_node); |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 497 | rc = psci_plat_pm_ops->affinst_suspend_finish(read_mpidr_el1(), |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 498 | cluster_node->level, |
| 499 | plat_state); |
| 500 | assert(rc == PSCI_E_SUCCESS); |
| 501 | } |
| 502 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 503 | /* State management: Increment the cluster reference count */ |
| 504 | psci_set_state(cluster_node, PSCI_STATE_ON); |
| 505 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 510 | static unsigned int psci_afflvl2_suspend_finish(aff_map_node_t *system_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 511 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 512 | unsigned int plat_state, rc = PSCI_E_SUCCESS;; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 513 | |
| 514 | /* Cannot go beyond this affinity level */ |
| 515 | assert(system_node->level == MPIDR_AFFLVL2); |
| 516 | |
| 517 | /* |
| 518 | * Currently, there are no architectural actions to perform |
| 519 | * at the system level. |
| 520 | */ |
| 521 | |
| 522 | /* |
| 523 | * Plat. management: Perform the platform specific actions |
| 524 | * as per the old state of the cluster e.g. enabling |
| 525 | * coherency at the interconnect depends upon the state with |
| 526 | * which this cluster was powered up. If anything goes wrong |
| 527 | * then assert as there is no way to recover from this |
| 528 | * situation. |
| 529 | */ |
| 530 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 531 | |
| 532 | /* Get the physical state of the system */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 533 | plat_state = psci_get_phys_state(system_node); |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 534 | rc = psci_plat_pm_ops->affinst_suspend_finish(read_mpidr_el1(), |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 535 | system_node->level, |
| 536 | plat_state); |
| 537 | assert(rc == PSCI_E_SUCCESS); |
| 538 | } |
| 539 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 540 | /* State management: Increment the system reference count */ |
| 541 | psci_set_state(system_node, PSCI_STATE_ON); |
| 542 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 543 | return rc; |
| 544 | } |
| 545 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 546 | const afflvl_power_on_finisher_t psci_afflvl_suspend_finishers[] = { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 547 | psci_afflvl0_suspend_finish, |
| 548 | psci_afflvl1_suspend_finish, |
| 549 | psci_afflvl2_suspend_finish, |
| 550 | }; |