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 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 32 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <assert.h> |
| 34 | #include <string.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 35 | #include "psci_private.h" |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 36 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 37 | typedef int (*afflvl_off_handler_t)(unsigned long, aff_map_node_t *); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 38 | |
| 39 | /******************************************************************************* |
| 40 | * The next three functions implement a handler for each supported affinity |
| 41 | * level which is called when that affinity level is turned off. |
| 42 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 43 | static int psci_afflvl0_off(unsigned long mpidr, aff_map_node_t *cpu_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 44 | { |
| 45 | unsigned int index, plat_state; |
| 46 | int rc = PSCI_E_SUCCESS; |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 47 | unsigned long sctlr; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 48 | |
| 49 | assert(cpu_node->level == MPIDR_AFFLVL0); |
| 50 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 51 | /* State management: mark this cpu as turned off */ |
| 52 | psci_set_state(cpu_node, PSCI_STATE_OFF); |
| 53 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 54 | /* |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 55 | * Generic management: Get the index for clearing any lingering re-entry |
| 56 | * information and allow the secure world to switch itself off |
| 57 | */ |
| 58 | |
| 59 | /* |
| 60 | * Call the cpu off handler registered by the Secure Payload Dispatcher |
| 61 | * to let it do any bookeeping. Assume that the SPD always reports an |
| 62 | * E_DENIED error if SP refuse to power down |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 63 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 64 | if (psci_spd_pm && psci_spd_pm->svc_off) { |
| 65 | rc = psci_spd_pm->svc_off(0); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 66 | if (rc) |
| 67 | return rc; |
| 68 | } |
| 69 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 70 | index = cpu_node->data; |
| 71 | memset(&psci_ns_entry_info[index], 0, sizeof(psci_ns_entry_info[index])); |
| 72 | |
| 73 | /* |
| 74 | * Arch. management. Perform the necessary steps to flush all |
| 75 | * cpu caches. |
| 76 | * |
| 77 | * TODO: This power down sequence varies across cpus so it needs to be |
| 78 | * abstracted out on the basis of the MIDR like in cpu_reset_handler(). |
| 79 | * Do the bare minimal for the time being. Fix this before porting to |
| 80 | * Cortex models. |
| 81 | */ |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 82 | sctlr = read_sctlr_el3(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 83 | sctlr &= ~SCTLR_C_BIT; |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 84 | write_sctlr_el3(sctlr); |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 85 | isb(); /* ensure MMU disable takes immediate effect */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * CAUTION: This flush to the level of unification makes an assumption |
| 89 | * about the cache hierarchy at affinity level 0 (cpu) in the platform. |
| 90 | * Ideally the platform should tell psci which levels to flush to exit |
| 91 | * coherency. |
| 92 | */ |
| 93 | dcsw_op_louis(DCCISW); |
| 94 | |
| 95 | /* |
| 96 | * Plat. management: Perform platform specific actions to turn this |
| 97 | * cpu off e.g. exit cpu coherency, program the power controller etc. |
| 98 | */ |
| 99 | if (psci_plat_pm_ops->affinst_off) { |
| 100 | |
| 101 | /* Get the current physical state of this cpu */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 102 | plat_state = psci_get_phys_state(cpu_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 103 | rc = psci_plat_pm_ops->affinst_off(mpidr, |
| 104 | cpu_node->level, |
| 105 | plat_state); |
| 106 | } |
| 107 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 108 | return rc; |
| 109 | } |
| 110 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 111 | static int psci_afflvl1_off(unsigned long mpidr, aff_map_node_t *cluster_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 112 | { |
| 113 | int rc = PSCI_E_SUCCESS; |
| 114 | unsigned int plat_state; |
| 115 | |
| 116 | /* Sanity check the cluster level */ |
| 117 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 118 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 119 | /* State management: Decrement the cluster reference count */ |
| 120 | psci_set_state(cluster_node, PSCI_STATE_OFF); |
| 121 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 122 | /* |
| 123 | * Keep the physical state of this cluster handy to decide |
| 124 | * what action needs to be taken |
| 125 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 126 | plat_state = psci_get_phys_state(cluster_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Arch. Management. Flush all levels of caches to PoC if |
| 130 | * the cluster is to be shutdown |
| 131 | */ |
| 132 | if (plat_state == PSCI_STATE_OFF) |
| 133 | dcsw_op_all(DCCISW); |
| 134 | |
| 135 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 136 | * Plat. Management. Allow the platform to do its cluster |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 137 | * specific bookeeping e.g. turn off interconnect coherency, |
| 138 | * program the power controller etc. |
| 139 | */ |
| 140 | if (psci_plat_pm_ops->affinst_off) |
| 141 | rc = psci_plat_pm_ops->affinst_off(mpidr, |
| 142 | cluster_node->level, |
| 143 | plat_state); |
| 144 | |
| 145 | return rc; |
| 146 | } |
| 147 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 148 | static int psci_afflvl2_off(unsigned long mpidr, aff_map_node_t *system_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 149 | { |
| 150 | int rc = PSCI_E_SUCCESS; |
| 151 | unsigned int plat_state; |
| 152 | |
| 153 | /* Cannot go beyond this level */ |
| 154 | assert(system_node->level == MPIDR_AFFLVL2); |
| 155 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 156 | /* State management: Decrement the system reference count */ |
| 157 | psci_set_state(system_node, PSCI_STATE_OFF); |
| 158 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 159 | /* |
| 160 | * Keep the physical state of the system handy to decide what |
| 161 | * action needs to be taken |
| 162 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 163 | plat_state = psci_get_phys_state(system_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 164 | |
| 165 | /* No arch. and generic bookeeping to do here currently */ |
| 166 | |
| 167 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 168 | * Plat. Management : Allow the platform to do its bookeeping |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 169 | * at this affinity level |
| 170 | */ |
| 171 | if (psci_plat_pm_ops->affinst_off) |
| 172 | rc = psci_plat_pm_ops->affinst_off(mpidr, |
| 173 | system_node->level, |
| 174 | plat_state); |
| 175 | return rc; |
| 176 | } |
| 177 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 178 | static const afflvl_off_handler_t psci_afflvl_off_handlers[] = { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 179 | psci_afflvl0_off, |
| 180 | psci_afflvl1_off, |
| 181 | psci_afflvl2_off, |
| 182 | }; |
| 183 | |
| 184 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 185 | * This function takes an array of pointers to affinity instance nodes in the |
| 186 | * topology tree and calls the off handler for the corresponding affinity |
| 187 | * levels |
| 188 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 189 | static int psci_call_off_handlers(mpidr_aff_map_nodes_t mpidr_nodes, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 190 | int start_afflvl, |
| 191 | int end_afflvl, |
| 192 | unsigned long mpidr) |
| 193 | { |
| 194 | int rc = PSCI_E_INVALID_PARAMS, level; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 195 | aff_map_node_t *node; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 196 | |
| 197 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 198 | node = mpidr_nodes[level]; |
| 199 | if (node == NULL) |
| 200 | continue; |
| 201 | |
| 202 | /* |
| 203 | * TODO: In case of an error should there be a way |
| 204 | * of restoring what we might have torn down at |
| 205 | * lower affinity levels. |
| 206 | */ |
| 207 | rc = psci_afflvl_off_handlers[level](mpidr, node); |
| 208 | if (rc != PSCI_E_SUCCESS) |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | return rc; |
| 213 | } |
| 214 | |
| 215 | /******************************************************************************* |
| 216 | * Top level handler which is called when a cpu wants to power itself down. |
| 217 | * It's assumed that along with turning the cpu off, higher affinity levels will |
| 218 | * be turned off as far as possible. It traverses through all the affinity |
| 219 | * levels performing generic, architectural, platform setup and state management |
| 220 | * e.g. for a cluster that's to be powered off, it will call the platform |
| 221 | * specific code which will disable coherency at the interconnect level if the |
| 222 | * cpu is the last in the cluster. For a cpu it could mean programming the power |
| 223 | * the power controller etc. |
| 224 | * |
| 225 | * The state of all the relevant affinity levels is changed prior to calling the |
| 226 | * affinity level specific handlers as their actions would depend upon the state |
| 227 | * the affinity level is about to enter. |
| 228 | * |
| 229 | * The affinity level specific handlers are called in ascending order i.e. from |
| 230 | * the lowest to the highest affinity level implemented by the platform because |
| 231 | * to turn off affinity level X it is neccesary to turn off affinity level X - 1 |
| 232 | * first. |
| 233 | * |
| 234 | * CAUTION: This function is called with coherent stacks so that coherency can |
| 235 | * be turned off and caches can be flushed safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 236 | ******************************************************************************/ |
| 237 | int psci_afflvl_off(unsigned long mpidr, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 238 | int start_afflvl, |
| 239 | int end_afflvl) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 240 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 241 | int rc = PSCI_E_SUCCESS; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 242 | mpidr_aff_map_nodes_t mpidr_nodes; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 243 | |
| 244 | mpidr &= MPIDR_AFFINITY_MASK;; |
| 245 | |
| 246 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 247 | * Collect the pointers to the nodes in the topology tree for |
| 248 | * each affinity instance in the mpidr. If this function does |
| 249 | * not return successfully then either the mpidr or the affinity |
| 250 | * levels are incorrect. In either case, we cannot return back |
| 251 | * to the caller as it would not know what to do. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 252 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 253 | rc = psci_get_aff_map_nodes(mpidr, |
| 254 | start_afflvl, |
| 255 | end_afflvl, |
| 256 | mpidr_nodes); |
| 257 | assert (rc == PSCI_E_SUCCESS); |
| 258 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 259 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 260 | * This function acquires the lock corresponding to each affinity |
| 261 | * level so that by the time all locks are taken, the system topology |
| 262 | * is snapshot and state management can be done safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 263 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 264 | psci_acquire_afflvl_locks(mpidr, |
| 265 | start_afflvl, |
| 266 | end_afflvl, |
| 267 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 268 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 269 | /* Perform generic, architecture and platform specific handling */ |
| 270 | rc = psci_call_off_handlers(mpidr_nodes, |
| 271 | start_afflvl, |
| 272 | end_afflvl, |
| 273 | mpidr); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 274 | |
| 275 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 276 | * Release the locks corresponding to each affinity level in the |
| 277 | * reverse order to which they were acquired. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 278 | */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 279 | psci_release_afflvl_locks(mpidr, |
| 280 | start_afflvl, |
| 281 | end_afflvl, |
| 282 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 283 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 284 | return rc; |
| 285 | } |