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 <bl_common.h> |
| 35 | #include <context.h> |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 36 | #include <context_mgmt.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 37 | #include <debug.h> |
Dan Handley | ed6ff95 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 38 | #include <platform.h> |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 39 | #include <string.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 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 42 | /* |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 43 | * SPD power management operations, expected to be supplied by the registered |
| 44 | * SPD on successful SP initialization |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 45 | */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 46 | const spd_pm_ops_t *psci_spd_pm; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 47 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 48 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 49 | * Grand array that holds the platform's topology information for state |
| 50 | * management of affinity instances. Each node (aff_map_node) in the array |
| 51 | * corresponds to an affinity instance e.g. cluster, cpu within an mpidr |
| 52 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 53 | aff_map_node_t psci_aff_map[PSCI_NUM_AFFS] |
Soby Mathew | 2ae2043 | 2015-01-08 18:02:44 +0000 | [diff] [blame] | 54 | #if USE_COHERENT_MEM |
| 55 | __attribute__ ((section("tzfw_coherent_mem"))) |
| 56 | #endif |
| 57 | ; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 58 | |
| 59 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 60 | * Pointer to functions exported by the platform to complete power mgmt. ops |
| 61 | ******************************************************************************/ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 62 | const plat_pm_ops_t *psci_plat_pm_ops; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 63 | |
| 64 | /******************************************************************************* |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 65 | * This function is passed an array of pointers to affinity level nodes in the |
| 66 | * topology tree for an mpidr. It iterates through the nodes to find the highest |
| 67 | * affinity level which is marked as physically powered off. |
| 68 | ******************************************************************************/ |
| 69 | uint32_t psci_find_max_phys_off_afflvl(uint32_t start_afflvl, |
| 70 | uint32_t end_afflvl, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 71 | aff_map_node_t *mpidr_nodes[]) |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 72 | { |
| 73 | uint32_t max_afflvl = PSCI_INVALID_DATA; |
| 74 | |
| 75 | for (; start_afflvl <= end_afflvl; start_afflvl++) { |
| 76 | if (mpidr_nodes[start_afflvl] == NULL) |
| 77 | continue; |
| 78 | |
| 79 | if (psci_get_phys_state(mpidr_nodes[start_afflvl]) == |
| 80 | PSCI_STATE_OFF) |
| 81 | max_afflvl = start_afflvl; |
| 82 | } |
| 83 | |
| 84 | return max_afflvl; |
| 85 | } |
| 86 | |
| 87 | /******************************************************************************* |
| 88 | * This function saves the highest affinity level which is in OFF state. The |
| 89 | * affinity instance with which the level is associated is determined by the |
| 90 | * caller. |
| 91 | ******************************************************************************/ |
| 92 | void psci_set_max_phys_off_afflvl(uint32_t afflvl) |
| 93 | { |
| 94 | set_cpu_data(psci_svc_cpu_data.max_phys_off_afflvl, afflvl); |
| 95 | |
| 96 | /* |
| 97 | * Ensure that the saved value is flushed to main memory and any |
| 98 | * speculatively pre-fetched stale copies are invalidated from the |
| 99 | * caches of other cpus in the same coherency domain. This ensures that |
| 100 | * the value can be safely read irrespective of the state of the data |
| 101 | * cache. |
| 102 | */ |
| 103 | flush_cpu_data(psci_svc_cpu_data.max_phys_off_afflvl); |
| 104 | } |
| 105 | |
| 106 | /******************************************************************************* |
| 107 | * This function reads the saved highest affinity level which is in OFF |
| 108 | * state. The affinity instance with which the level is associated is determined |
| 109 | * by the caller. |
| 110 | ******************************************************************************/ |
| 111 | uint32_t psci_get_max_phys_off_afflvl(void) |
| 112 | { |
| 113 | /* |
| 114 | * Ensure that the last update of this value in this cpu's cache is |
| 115 | * flushed to main memory and any speculatively pre-fetched stale copies |
| 116 | * are invalidated from the caches of other cpus in the same coherency |
| 117 | * domain. This ensures that the value is always read from the main |
| 118 | * memory when it was written before the data cache was enabled. |
| 119 | */ |
| 120 | flush_cpu_data(psci_svc_cpu_data.max_phys_off_afflvl); |
| 121 | return get_cpu_data(psci_svc_cpu_data.max_phys_off_afflvl); |
| 122 | } |
| 123 | |
| 124 | /******************************************************************************* |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 125 | * Routine to return the maximum affinity level to traverse to after a cpu has |
| 126 | * been physically powered up. It is expected to be called immediately after |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 127 | * reset from assembler code. |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 128 | ******************************************************************************/ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 129 | int get_power_on_target_afflvl() |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 130 | { |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 131 | int afflvl; |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 132 | |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 133 | #if DEBUG |
| 134 | unsigned int state; |
| 135 | aff_map_node_t *node; |
| 136 | |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 137 | /* Retrieve our node from the topology tree */ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 138 | node = psci_get_aff_map_node(read_mpidr_el1() & MPIDR_AFFINITY_MASK, |
| 139 | MPIDR_AFFLVL0); |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 140 | assert(node); |
| 141 | |
| 142 | /* |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 143 | * Sanity check the state of the cpu. It should be either suspend or "on |
| 144 | * pending" |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 145 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 146 | state = psci_get_state(node); |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 147 | assert(state == PSCI_STATE_SUSPEND || state == PSCI_STATE_ON_PENDING); |
| 148 | #endif |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 149 | |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 150 | /* |
| 151 | * Assume that this cpu was suspended and retrieve its target affinity |
| 152 | * level. If it is invalid then it could only have been turned off |
| 153 | * earlier. get_max_afflvl() will return the highest affinity level a |
| 154 | * cpu can be turned off to. |
| 155 | */ |
| 156 | afflvl = psci_get_suspend_afflvl(); |
| 157 | if (afflvl == PSCI_INVALID_DATA) |
| 158 | afflvl = get_max_afflvl(); |
| 159 | return afflvl; |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 163 | * Simple routine to retrieve the maximum affinity level supported by the |
| 164 | * platform and check that it makes sense. |
| 165 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 166 | int get_max_afflvl(void) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 167 | { |
| 168 | int aff_lvl; |
| 169 | |
| 170 | aff_lvl = plat_get_max_afflvl(); |
| 171 | assert(aff_lvl <= MPIDR_MAX_AFFLVL && aff_lvl >= MPIDR_AFFLVL0); |
| 172 | |
| 173 | return aff_lvl; |
| 174 | } |
| 175 | |
| 176 | /******************************************************************************* |
| 177 | * Simple routine to set the id of an affinity instance at a given level in the |
| 178 | * mpidr. |
| 179 | ******************************************************************************/ |
| 180 | unsigned long mpidr_set_aff_inst(unsigned long mpidr, |
| 181 | unsigned char aff_inst, |
| 182 | int aff_lvl) |
| 183 | { |
| 184 | unsigned long aff_shift; |
| 185 | |
| 186 | assert(aff_lvl <= MPIDR_AFFLVL3); |
| 187 | |
| 188 | /* |
| 189 | * Decide the number of bits to shift by depending upon |
| 190 | * the affinity level |
| 191 | */ |
| 192 | aff_shift = get_afflvl_shift(aff_lvl); |
| 193 | |
| 194 | /* Clear the existing affinity instance & set the new one*/ |
| 195 | mpidr &= ~(MPIDR_AFFLVL_MASK << aff_shift); |
| 196 | mpidr |= aff_inst << aff_shift; |
| 197 | |
| 198 | return mpidr; |
| 199 | } |
| 200 | |
| 201 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 202 | * This function sanity checks a range of affinity levels. |
| 203 | ******************************************************************************/ |
| 204 | int psci_check_afflvl_range(int start_afflvl, int end_afflvl) |
| 205 | { |
| 206 | /* Sanity check the parameters passed */ |
Soby Mathew | 2b69750 | 2014-10-02 17:24:19 +0100 | [diff] [blame] | 207 | if (end_afflvl > get_max_afflvl()) |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 208 | return PSCI_E_INVALID_PARAMS; |
| 209 | |
| 210 | if (start_afflvl < MPIDR_AFFLVL0) |
| 211 | return PSCI_E_INVALID_PARAMS; |
| 212 | |
| 213 | if (end_afflvl < start_afflvl) |
| 214 | return PSCI_E_INVALID_PARAMS; |
| 215 | |
| 216 | return PSCI_E_SUCCESS; |
| 217 | } |
| 218 | |
| 219 | /******************************************************************************* |
| 220 | * This function is passed an array of pointers to affinity level nodes in the |
Achin Gupta | cab78e4 | 2014-07-28 00:09:01 +0100 | [diff] [blame] | 221 | * topology tree for an mpidr and the state which each node should transition |
| 222 | * to. It updates the state of each node between the specified affinity levels. |
| 223 | ******************************************************************************/ |
| 224 | void psci_do_afflvl_state_mgmt(uint32_t start_afflvl, |
| 225 | uint32_t end_afflvl, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 226 | aff_map_node_t *mpidr_nodes[], |
Achin Gupta | cab78e4 | 2014-07-28 00:09:01 +0100 | [diff] [blame] | 227 | uint32_t state) |
| 228 | { |
| 229 | uint32_t level; |
| 230 | |
| 231 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 232 | if (mpidr_nodes[level] == NULL) |
| 233 | continue; |
| 234 | psci_set_state(mpidr_nodes[level], state); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /******************************************************************************* |
| 239 | * This function is passed an array of pointers to affinity level nodes in the |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 240 | * topology tree for an mpidr. It picks up locks for each affinity level bottom |
| 241 | * up in the range specified. |
| 242 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 243 | void psci_acquire_afflvl_locks(int start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 244 | int end_afflvl, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 245 | aff_map_node_t *mpidr_nodes[]) |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 246 | { |
| 247 | int level; |
| 248 | |
| 249 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 250 | if (mpidr_nodes[level] == NULL) |
| 251 | continue; |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 252 | |
| 253 | psci_lock_get(mpidr_nodes[level]); |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /******************************************************************************* |
| 258 | * This function is passed an array of pointers to affinity level nodes in the |
| 259 | * topology tree for an mpidr. It releases the lock for each affinity level top |
| 260 | * down in the range specified. |
| 261 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 262 | void psci_release_afflvl_locks(int start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 263 | int end_afflvl, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 264 | aff_map_node_t *mpidr_nodes[]) |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 265 | { |
| 266 | int level; |
| 267 | |
| 268 | for (level = end_afflvl; level >= start_afflvl; level--) { |
| 269 | if (mpidr_nodes[level] == NULL) |
| 270 | continue; |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 271 | |
| 272 | psci_lock_release(mpidr_nodes[level]); |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 277 | * Simple routine to determine whether an affinity instance at a given level |
| 278 | * in an mpidr exists or not. |
| 279 | ******************************************************************************/ |
| 280 | int psci_validate_mpidr(unsigned long mpidr, int level) |
| 281 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 282 | aff_map_node_t *node; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 283 | |
| 284 | node = psci_get_aff_map_node(mpidr, level); |
| 285 | if (node && (node->state & PSCI_AFF_PRESENT)) |
| 286 | return PSCI_E_SUCCESS; |
| 287 | else |
| 288 | return PSCI_E_INVALID_PARAMS; |
| 289 | } |
| 290 | |
| 291 | /******************************************************************************* |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 292 | * This function determines the full entrypoint information for the requested |
| 293 | * PSCI entrypoint on power on/resume and saves this in the non-secure CPU |
| 294 | * cpu_context, ready for when the core boots. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 295 | ******************************************************************************/ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 296 | int psci_save_ns_entry(uint64_t mpidr, |
| 297 | uint64_t entrypoint, uint64_t context_id, |
| 298 | uint32_t ns_scr_el3, uint32_t ns_sctlr_el1) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 299 | { |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 300 | uint32_t ep_attr, mode, sctlr, daif, ee; |
| 301 | entry_point_info_t ep; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 302 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 303 | sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1; |
| 304 | ee = 0; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 305 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 306 | ep_attr = NON_SECURE | EP_ST_DISABLE; |
| 307 | if (sctlr & SCTLR_EE_BIT) { |
| 308 | ep_attr |= EP_EE_BIG; |
| 309 | ee = 1; |
| 310 | } |
| 311 | SET_PARAM_HEAD(&ep, PARAM_EP, VERSION_1, ep_attr); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 312 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 313 | ep.pc = entrypoint; |
| 314 | memset(&ep.args, 0, sizeof(ep.args)); |
| 315 | ep.args.arg0 = context_id; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * Figure out whether the cpu enters the non-secure address space |
| 319 | * in aarch32 or aarch64 |
| 320 | */ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 321 | if (ns_scr_el3 & SCR_RW_BIT) { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * Check whether a Thumb entry point has been provided for an |
| 325 | * aarch64 EL |
| 326 | */ |
| 327 | if (entrypoint & 0x1) |
| 328 | return PSCI_E_INVALID_PARAMS; |
| 329 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 330 | mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 331 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 332 | ep.spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 333 | } else { |
| 334 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 335 | mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * TODO: Choose async. exception bits if HYP mode is not |
| 339 | * implemented according to the values of SCR.{AW, FW} bits |
| 340 | */ |
Vikram Kanigiri | 9851e42 | 2014-05-13 14:42:08 +0100 | [diff] [blame] | 341 | daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT; |
| 342 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 343 | ep.spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 346 | /* initialise an entrypoint to set up the CPU context */ |
| 347 | cm_init_context(mpidr, &ep); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 348 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 349 | return PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /******************************************************************************* |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 353 | * This function takes a pointer to an affinity node in the topology tree and |
| 354 | * returns its state. State of a non-leaf node needs to be calculated. |
| 355 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 356 | unsigned short psci_get_state(aff_map_node_t *node) |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 357 | { |
Soby Mathew | 2ae2043 | 2015-01-08 18:02:44 +0000 | [diff] [blame] | 358 | #if !USE_COHERENT_MEM |
| 359 | flush_dcache_range((uint64_t) node, sizeof(*node)); |
| 360 | #endif |
| 361 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 362 | assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL); |
| 363 | |
| 364 | /* A cpu node just contains the state which can be directly returned */ |
| 365 | if (node->level == MPIDR_AFFLVL0) |
| 366 | return (node->state >> PSCI_STATE_SHIFT) & PSCI_STATE_MASK; |
| 367 | |
| 368 | /* |
| 369 | * For an affinity level higher than a cpu, the state has to be |
| 370 | * calculated. It depends upon the value of the reference count |
| 371 | * which is managed by each node at the next lower affinity level |
| 372 | * e.g. for a cluster, each cpu increments/decrements the reference |
| 373 | * count. If the reference count is 0 then the affinity level is |
| 374 | * OFF else ON. |
| 375 | */ |
| 376 | if (node->ref_count) |
| 377 | return PSCI_STATE_ON; |
| 378 | else |
| 379 | return PSCI_STATE_OFF; |
| 380 | } |
| 381 | |
| 382 | /******************************************************************************* |
| 383 | * This function takes a pointer to an affinity node in the topology tree and |
| 384 | * a target state. State of a non-leaf node needs to be converted to a reference |
| 385 | * count. State of a leaf node can be set directly. |
| 386 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 387 | void psci_set_state(aff_map_node_t *node, unsigned short state) |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 388 | { |
| 389 | assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL); |
| 390 | |
| 391 | /* |
| 392 | * For an affinity level higher than a cpu, the state is used |
| 393 | * to decide whether the reference count is incremented or |
| 394 | * decremented. Entry into the ON_PENDING state does not have |
| 395 | * effect. |
| 396 | */ |
| 397 | if (node->level > MPIDR_AFFLVL0) { |
| 398 | switch (state) { |
| 399 | case PSCI_STATE_ON: |
| 400 | node->ref_count++; |
| 401 | break; |
| 402 | case PSCI_STATE_OFF: |
| 403 | case PSCI_STATE_SUSPEND: |
| 404 | node->ref_count--; |
| 405 | break; |
| 406 | case PSCI_STATE_ON_PENDING: |
| 407 | /* |
| 408 | * An affinity level higher than a cpu will not undergo |
| 409 | * a state change when it is about to be turned on |
| 410 | */ |
| 411 | return; |
| 412 | default: |
| 413 | assert(0); |
| 414 | } |
| 415 | } else { |
| 416 | node->state &= ~(PSCI_STATE_MASK << PSCI_STATE_SHIFT); |
| 417 | node->state |= (state & PSCI_STATE_MASK) << PSCI_STATE_SHIFT; |
| 418 | } |
Soby Mathew | 2ae2043 | 2015-01-08 18:02:44 +0000 | [diff] [blame] | 419 | |
| 420 | #if !USE_COHERENT_MEM |
| 421 | flush_dcache_range((uint64_t) node, sizeof(*node)); |
| 422 | #endif |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 426 | * An affinity level could be on, on_pending, suspended or off. These are the |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 427 | * logical states it can be in. Physically either it is off or on. When it is in |
| 428 | * the state on_pending then it is about to be turned on. It is not possible to |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 429 | * tell whether that's actually happenned or not. So we err on the side of |
| 430 | * caution & treat the affinity level as being turned off. |
| 431 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 432 | unsigned short psci_get_phys_state(aff_map_node_t *node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 433 | { |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 434 | unsigned int state; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 435 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 436 | state = psci_get_state(node); |
| 437 | return get_phys_state(state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 441 | * This function takes an array of pointers to affinity instance nodes in the |
| 442 | * topology tree and calls the physical power on handler for the corresponding |
| 443 | * affinity levels |
| 444 | ******************************************************************************/ |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 445 | static int psci_call_power_on_handlers(aff_map_node_t *mpidr_nodes[], |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 446 | int start_afflvl, |
| 447 | int end_afflvl, |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 448 | afflvl_power_on_finisher_t *pon_handlers) |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 449 | { |
| 450 | int rc = PSCI_E_INVALID_PARAMS, level; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 451 | aff_map_node_t *node; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 452 | |
| 453 | for (level = end_afflvl; level >= start_afflvl; level--) { |
| 454 | node = mpidr_nodes[level]; |
| 455 | if (node == NULL) |
| 456 | continue; |
| 457 | |
| 458 | /* |
| 459 | * If we run into any trouble while powering up an |
| 460 | * affinity instance, then there is no recovery path |
| 461 | * so simply return an error and let the caller take |
| 462 | * care of the situation. |
| 463 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 464 | rc = pon_handlers[level](node); |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 465 | if (rc != PSCI_E_SUCCESS) |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | return rc; |
| 470 | } |
| 471 | |
| 472 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 473 | * Generic handler which is called when a cpu is physically powered on. It |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 474 | * traverses through all the affinity levels performing generic, architectural, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 475 | * platform setup and state management e.g. for a cluster that's been powered |
| 476 | * on, it will call the platform specific code which will enable coherency at |
| 477 | * the interconnect level. For a cpu it could mean turning on the MMU etc. |
| 478 | * |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 479 | * The state of all the relevant affinity levels is changed after calling the |
| 480 | * affinity level specific handlers as their actions would depend upon the state |
| 481 | * the affinity level is exiting from. |
| 482 | * |
| 483 | * The affinity level specific handlers are called in descending order i.e. from |
| 484 | * the highest to the lowest affinity level implemented by the platform because |
| 485 | * to turn on affinity level X it is neccesary to turn on affinity level X + 1 |
| 486 | * first. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 487 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 488 | void psci_afflvl_power_on_finish(int start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 489 | int end_afflvl, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 490 | afflvl_power_on_finisher_t *pon_handlers) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 491 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 492 | mpidr_aff_map_nodes_t mpidr_nodes; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 493 | int rc; |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 494 | unsigned int max_phys_off_afflvl; |
| 495 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 496 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 497 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 498 | * Collect the pointers to the nodes in the topology tree for |
| 499 | * each affinity instance in the mpidr. If this function does |
| 500 | * not return successfully then either the mpidr or the affinity |
| 501 | * levels are incorrect. Either case is an irrecoverable error. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 502 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 503 | rc = psci_get_aff_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 504 | start_afflvl, |
| 505 | end_afflvl, |
| 506 | mpidr_nodes); |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 507 | if (rc != PSCI_E_SUCCESS) |
| 508 | panic(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 509 | |
| 510 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 511 | * This function acquires the lock corresponding to each affinity |
| 512 | * level so that by the time all locks are taken, the system topology |
| 513 | * is snapshot and state management can be done safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 514 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 515 | psci_acquire_afflvl_locks(start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 516 | end_afflvl, |
| 517 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 518 | |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 519 | max_phys_off_afflvl = psci_find_max_phys_off_afflvl(start_afflvl, |
| 520 | end_afflvl, |
| 521 | mpidr_nodes); |
| 522 | assert(max_phys_off_afflvl != PSCI_INVALID_DATA); |
| 523 | |
| 524 | /* |
| 525 | * Stash the highest affinity level that will come out of the OFF or |
| 526 | * SUSPEND states. |
| 527 | */ |
| 528 | psci_set_max_phys_off_afflvl(max_phys_off_afflvl); |
| 529 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 530 | /* Perform generic, architecture and platform specific handling */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 531 | rc = psci_call_power_on_handlers(mpidr_nodes, |
| 532 | start_afflvl, |
| 533 | end_afflvl, |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 534 | pon_handlers); |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 535 | if (rc != PSCI_E_SUCCESS) |
| 536 | panic(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 537 | |
| 538 | /* |
Achin Gupta | cab78e4 | 2014-07-28 00:09:01 +0100 | [diff] [blame] | 539 | * This function updates the state of each affinity instance |
| 540 | * corresponding to the mpidr in the range of affinity levels |
| 541 | * specified. |
| 542 | */ |
| 543 | psci_do_afflvl_state_mgmt(start_afflvl, |
| 544 | end_afflvl, |
| 545 | mpidr_nodes, |
| 546 | PSCI_STATE_ON); |
| 547 | |
| 548 | /* |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 549 | * Invalidate the entry for the highest affinity level stashed earlier. |
| 550 | * This ensures that any reads of this variable outside the power |
| 551 | * up/down sequences return PSCI_INVALID_DATA |
| 552 | */ |
| 553 | psci_set_max_phys_off_afflvl(PSCI_INVALID_DATA); |
| 554 | |
| 555 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 556 | * This loop releases the lock corresponding to each affinity level |
| 557 | * in the reverse order to which they were acquired. |
| 558 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 559 | psci_release_afflvl_locks(start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 560 | end_afflvl, |
| 561 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 562 | } |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 563 | |
| 564 | /******************************************************************************* |
| 565 | * This function initializes the set of hooks that PSCI invokes as part of power |
| 566 | * management operation. The power management hooks are expected to be provided |
| 567 | * by the SPD, after it finishes all its initialization |
| 568 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 569 | void psci_register_spd_pm_hook(const spd_pm_ops_t *pm) |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 570 | { |
| 571 | psci_spd_pm = pm; |
| 572 | } |
Juan Castillo | 4dc4a47 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 573 | |
| 574 | /******************************************************************************* |
| 575 | * This function prints the state of all affinity instances present in the |
| 576 | * system |
| 577 | ******************************************************************************/ |
| 578 | void psci_print_affinity_map(void) |
| 579 | { |
| 580 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
| 581 | aff_map_node_t *node; |
| 582 | unsigned int idx; |
| 583 | /* This array maps to the PSCI_STATE_X definitions in psci.h */ |
| 584 | static const char *psci_state_str[] = { |
| 585 | "ON", |
| 586 | "OFF", |
| 587 | "ON_PENDING", |
| 588 | "SUSPEND" |
| 589 | }; |
| 590 | |
| 591 | INFO("PSCI Affinity Map:\n"); |
| 592 | for (idx = 0; idx < PSCI_NUM_AFFS ; idx++) { |
| 593 | node = &psci_aff_map[idx]; |
| 594 | if (!(node->state & PSCI_AFF_PRESENT)) { |
| 595 | continue; |
| 596 | } |
| 597 | INFO(" AffInst: Level %u, MPID 0x%lx, State %s\n", |
| 598 | node->level, node->mpidr, |
| 599 | psci_state_str[psci_get_state(node)]); |
| 600 | } |
| 601 | #endif |
| 602 | } |