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 | |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <assert.h> |
| 34 | #include <arch_helpers.h> |
| 35 | #include <console.h> |
| 36 | #include <platform.h> |
| 37 | #include <psci_private.h> |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 38 | #include <context_mgmt.h> |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 39 | #include <runtime_svc.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 40 | |
| 41 | /******************************************************************************* |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 42 | * Per cpu non-secure contexts used to program the architectural state prior |
| 43 | * return to the normal world. |
| 44 | * TODO: Use the memory allocator to set aside memory for the contexts instead |
| 45 | * of relying on platform defined constants. Using PSCI_NUM_AFFS will be an |
| 46 | * overkill. |
| 47 | ******************************************************************************/ |
| 48 | static cpu_context psci_ns_context[PLATFORM_CORE_COUNT]; |
| 49 | |
| 50 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 51 | * Routines for retrieving the node corresponding to an affinity level instance |
| 52 | * in the mpidr. The first one uses binary search to find the node corresponding |
| 53 | * to the mpidr (key) at a particular affinity level. The second routine decides |
| 54 | * extents of the binary search at each affinity level. |
| 55 | ******************************************************************************/ |
| 56 | static int psci_aff_map_get_idx(unsigned long key, |
| 57 | int min_idx, |
| 58 | int max_idx) |
| 59 | { |
| 60 | int mid; |
| 61 | |
| 62 | /* |
| 63 | * Terminating condition: If the max and min indices have crossed paths |
| 64 | * during the binary search then the key has not been found. |
| 65 | */ |
| 66 | if (max_idx < min_idx) |
| 67 | return PSCI_E_INVALID_PARAMS; |
| 68 | |
| 69 | /* |
| 70 | * Bisect the array around 'mid' and then recurse into the array chunk |
| 71 | * where the key is likely to be found. The mpidrs in each node in the |
| 72 | * 'psci_aff_map' for a given affinity level are stored in an ascending |
| 73 | * order which makes the binary search possible. |
| 74 | */ |
| 75 | mid = min_idx + ((max_idx - min_idx) >> 1); /* Divide by 2 */ |
| 76 | if (psci_aff_map[mid].mpidr > key) |
| 77 | return psci_aff_map_get_idx(key, min_idx, mid - 1); |
| 78 | else if (psci_aff_map[mid].mpidr < key) |
| 79 | return psci_aff_map_get_idx(key, mid + 1, max_idx); |
| 80 | else |
| 81 | return mid; |
| 82 | } |
| 83 | |
| 84 | aff_map_node *psci_get_aff_map_node(unsigned long mpidr, int aff_lvl) |
| 85 | { |
| 86 | int rc; |
| 87 | |
| 88 | /* Right shift the mpidr to the required affinity level */ |
| 89 | mpidr = mpidr_mask_lower_afflvls(mpidr, aff_lvl); |
| 90 | |
| 91 | rc = psci_aff_map_get_idx(mpidr, |
| 92 | psci_aff_limits[aff_lvl].min, |
| 93 | psci_aff_limits[aff_lvl].max); |
| 94 | if (rc >= 0) |
| 95 | return &psci_aff_map[rc]; |
| 96 | else |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 101 | * This function populates an array with nodes corresponding to a given range of |
| 102 | * affinity levels in an mpidr. It returns successfully only when the affinity |
| 103 | * levels are correct, the mpidr is valid i.e. no affinity level is absent from |
| 104 | * the topology tree & the affinity instance at level 0 is not absent. |
| 105 | ******************************************************************************/ |
| 106 | int psci_get_aff_map_nodes(unsigned long mpidr, |
| 107 | int start_afflvl, |
| 108 | int end_afflvl, |
| 109 | mpidr_aff_map_nodes mpidr_nodes) |
| 110 | { |
| 111 | int rc = PSCI_E_INVALID_PARAMS, level; |
| 112 | aff_map_node *node; |
| 113 | |
| 114 | rc = psci_check_afflvl_range(start_afflvl, end_afflvl); |
| 115 | if (rc != PSCI_E_SUCCESS) |
| 116 | return rc; |
| 117 | |
| 118 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 119 | |
| 120 | /* |
| 121 | * Grab the node for each affinity level. No affinity level |
| 122 | * can be missing as that would mean that the topology tree |
| 123 | * is corrupted. |
| 124 | */ |
| 125 | node = psci_get_aff_map_node(mpidr, level); |
| 126 | if (node == NULL) { |
| 127 | rc = PSCI_E_INVALID_PARAMS; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Skip absent affinity levels unless it's afffinity level 0. |
| 133 | * An absent cpu means that the mpidr is invalid. Save the |
| 134 | * pointer to the node for the present affinity level |
| 135 | */ |
| 136 | if (!(node->state & PSCI_AFF_PRESENT)) { |
| 137 | if (level == MPIDR_AFFLVL0) { |
| 138 | rc = PSCI_E_INVALID_PARAMS; |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | mpidr_nodes[level] = NULL; |
| 143 | } else |
| 144 | mpidr_nodes[level] = node; |
| 145 | } |
| 146 | |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 151 | * Function which initializes the 'aff_map_node' corresponding to an affinity |
| 152 | * level instance. Each node has a unique mpidr, level and bakery lock. The data |
| 153 | * field is opaque and holds affinity level specific data e.g. for affinity |
| 154 | * level 0 it contains the index into arrays that hold the secure/non-secure |
| 155 | * state for a cpu that's been turned on/off |
| 156 | ******************************************************************************/ |
| 157 | static void psci_init_aff_map_node(unsigned long mpidr, |
| 158 | int level, |
| 159 | unsigned int idx) |
| 160 | { |
| 161 | unsigned char state; |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 162 | uint32_t linear_id; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 163 | psci_aff_map[idx].mpidr = mpidr; |
| 164 | psci_aff_map[idx].level = level; |
| 165 | bakery_lock_init(&psci_aff_map[idx].lock); |
| 166 | |
| 167 | /* |
| 168 | * If an affinity instance is present then mark it as OFF to begin with. |
| 169 | */ |
| 170 | state = plat_get_aff_state(level, mpidr); |
| 171 | psci_aff_map[idx].state = state; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 172 | |
| 173 | if (level == MPIDR_AFFLVL0) { |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * Mark the cpu as OFF. Higher affinity level reference counts |
| 177 | * have already been memset to 0 |
| 178 | */ |
| 179 | if (state & PSCI_AFF_PRESENT) |
| 180 | psci_set_state(&psci_aff_map[idx], PSCI_STATE_OFF); |
| 181 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 182 | /* Ensure that we have not overflowed the psci_ns_einfo array */ |
| 183 | assert(psci_ns_einfo_idx < PSCI_NUM_AFFS); |
| 184 | |
| 185 | psci_aff_map[idx].data = psci_ns_einfo_idx; |
| 186 | psci_ns_einfo_idx++; |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * Associate a non-secure context with this affinity |
| 190 | * instance through the context management library. |
| 191 | */ |
| 192 | linear_id = platform_get_core_pos(mpidr); |
| 193 | assert(linear_id < PLATFORM_CORE_COUNT); |
| 194 | |
| 195 | cm_set_context(mpidr, |
| 196 | (void *) &psci_ns_context[linear_id], |
| 197 | NON_SECURE); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 198 | |
| 199 | /* Initialize exception stack in the context */ |
| 200 | cm_init_exception_stack(mpidr, NON_SECURE); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | /******************************************************************************* |
| 207 | * Core routine used by the Breadth-First-Search algorithm to populate the |
| 208 | * affinity tree. Each level in the tree corresponds to an affinity level. This |
| 209 | * routine's aim is to traverse to the target affinity level and populate nodes |
| 210 | * in the 'psci_aff_map' for all the siblings at that level. It uses the current |
| 211 | * affinity level to keep track of how many levels from the root of the tree |
| 212 | * have been traversed. If the current affinity level != target affinity level, |
| 213 | * then the platform is asked to return the number of children that each |
| 214 | * affinity instance has at the current affinity level. Traversal is then done |
| 215 | * for each child at the next lower level i.e. current affinity level - 1. |
| 216 | * |
| 217 | * CAUTION: This routine assumes that affinity instance ids are allocated in a |
| 218 | * monotonically increasing manner at each affinity level in a mpidr starting |
| 219 | * from 0. If the platform breaks this assumption then this code will have to |
| 220 | * be reworked accordingly. |
| 221 | ******************************************************************************/ |
| 222 | static unsigned int psci_init_aff_map(unsigned long mpidr, |
| 223 | unsigned int affmap_idx, |
| 224 | int cur_afflvl, |
| 225 | int tgt_afflvl) |
| 226 | { |
| 227 | unsigned int ctr, aff_count; |
| 228 | |
| 229 | assert(cur_afflvl >= tgt_afflvl); |
| 230 | |
| 231 | /* |
| 232 | * Find the number of siblings at the current affinity level & |
| 233 | * assert if there are none 'cause then we have been invoked with |
| 234 | * an invalid mpidr. |
| 235 | */ |
| 236 | aff_count = plat_get_aff_count(cur_afflvl, mpidr); |
| 237 | assert(aff_count); |
| 238 | |
| 239 | if (tgt_afflvl < cur_afflvl) { |
| 240 | for (ctr = 0; ctr < aff_count; ctr++) { |
| 241 | mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl); |
| 242 | affmap_idx = psci_init_aff_map(mpidr, |
| 243 | affmap_idx, |
| 244 | cur_afflvl - 1, |
| 245 | tgt_afflvl); |
| 246 | } |
| 247 | } else { |
| 248 | for (ctr = 0; ctr < aff_count; ctr++, affmap_idx++) { |
| 249 | mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl); |
| 250 | psci_init_aff_map_node(mpidr, cur_afflvl, affmap_idx); |
| 251 | } |
| 252 | |
| 253 | /* affmap_idx is 1 greater than the max index of cur_afflvl */ |
| 254 | psci_aff_limits[cur_afflvl].max = affmap_idx - 1; |
| 255 | } |
| 256 | |
| 257 | return affmap_idx; |
| 258 | } |
| 259 | |
| 260 | /******************************************************************************* |
| 261 | * This function initializes the topology tree by querying the platform. To do |
| 262 | * so, it's helper routines implement a Breadth-First-Search. At each affinity |
| 263 | * level the platform conveys the number of affinity instances that exist i.e. |
| 264 | * the affinity count. The algorithm populates the psci_aff_map recursively |
| 265 | * using this information. On a platform that implements two clusters of 4 cpus |
| 266 | * each, the populated aff_map_array would look like this: |
| 267 | * |
| 268 | * <- cpus cluster0 -><- cpus cluster1 -> |
| 269 | * --------------------------------------------------- |
| 270 | * | 0 | 1 | 0 | 1 | 2 | 3 | 0 | 1 | 2 | 3 | |
| 271 | * --------------------------------------------------- |
| 272 | * ^ ^ |
| 273 | * cluster __| cpu __| |
| 274 | * limit limit |
| 275 | * |
| 276 | * The first 2 entries are of the cluster nodes. The next 4 entries are of cpus |
| 277 | * within cluster 0. The last 4 entries are of cpus within cluster 1. |
| 278 | * The 'psci_aff_limits' array contains the max & min index of each affinity |
| 279 | * level within the 'psci_aff_map' array. This allows restricting search of a |
| 280 | * node at an affinity level between the indices in the limits array. |
| 281 | ******************************************************************************/ |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 282 | int32_t psci_setup(void) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 283 | { |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 284 | unsigned long mpidr = read_mpidr(); |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 285 | int afflvl, affmap_idx, max_afflvl; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 286 | aff_map_node *node; |
| 287 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 288 | psci_ns_einfo_idx = 0; |
| 289 | psci_plat_pm_ops = NULL; |
| 290 | |
| 291 | /* Find out the maximum affinity level that the platform implements */ |
| 292 | max_afflvl = get_max_afflvl(); |
| 293 | assert(max_afflvl <= MPIDR_MAX_AFFLVL); |
| 294 | |
| 295 | /* |
| 296 | * This call traverses the topology tree with help from the platform and |
| 297 | * populates the affinity map using a breadth-first-search recursively. |
| 298 | * We assume that the platform allocates affinity instance ids from 0 |
| 299 | * onwards at each affinity level in the mpidr. FIRST_MPIDR = 0.0.0.0 |
| 300 | */ |
| 301 | affmap_idx = 0; |
| 302 | for (afflvl = max_afflvl; afflvl >= MPIDR_AFFLVL0; afflvl--) { |
| 303 | affmap_idx = psci_init_aff_map(FIRST_MPIDR, |
| 304 | affmap_idx, |
| 305 | max_afflvl, |
| 306 | afflvl); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Set the bounds for the affinity counts of each level in the map. Also |
| 311 | * flush out the entire array so that it's visible to subsequent power |
| 312 | * management operations. The 'psci_aff_map' array is allocated in |
| 313 | * coherent memory so does not need flushing. The 'psci_aff_limits' |
| 314 | * array is allocated in normal memory. It will be accessed when the mmu |
| 315 | * is off e.g. after reset. Hence it needs to be flushed. |
| 316 | */ |
| 317 | for (afflvl = MPIDR_AFFLVL0; afflvl < max_afflvl; afflvl++) { |
| 318 | psci_aff_limits[afflvl].min = |
| 319 | psci_aff_limits[afflvl + 1].max + 1; |
| 320 | } |
| 321 | |
| 322 | flush_dcache_range((unsigned long) psci_aff_limits, |
| 323 | sizeof(psci_aff_limits)); |
| 324 | |
| 325 | /* |
| 326 | * Mark the affinity instances in our mpidr as ON. No need to lock as |
| 327 | * this is the primary cpu. |
| 328 | */ |
| 329 | mpidr &= MPIDR_AFFINITY_MASK; |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 330 | for (afflvl = MPIDR_AFFLVL0; afflvl <= max_afflvl; afflvl++) { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 331 | |
| 332 | node = psci_get_aff_map_node(mpidr, afflvl); |
| 333 | assert(node); |
| 334 | |
| 335 | /* Mark each present node as ON. */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 336 | if (node->state & PSCI_AFF_PRESENT) |
| 337 | psci_set_state(node, PSCI_STATE_ON); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 338 | } |
| 339 | |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 340 | platform_setup_pm(&psci_plat_pm_ops); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 341 | assert(psci_plat_pm_ops); |
| 342 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 343 | return 0; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 344 | } |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 345 | |
| 346 | /* Register PSCI as a run time service */ |
| 347 | DECLARE_RT_SVC( |
| 348 | psci, |
| 349 | |
| 350 | OEN_STD_START, |
| 351 | OEN_STD_END, |
| 352 | SMC_TYPE_FAST, |
| 353 | psci_setup, |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 354 | psci_smc_handler |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 355 | ); |