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