Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 1 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +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 <arch.h> |
| 32 | #include <arch_helpers.h> |
| 33 | #include <assert.h> |
| 34 | #include <bl_common.h> |
| 35 | #include <context.h> |
| 36 | #include <context_mgmt.h> |
| 37 | #include <platform.h> |
| 38 | #include <stddef.h> |
| 39 | #include "psci_private.h" |
| 40 | |
| 41 | /******************************************************************************* |
| 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 |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 45 | * of relying on platform defined constants. Using PSCI_NUM_PWR_DOMAINS will be |
| 46 | * an overkill. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 47 | ******************************************************************************/ |
| 48 | static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT]; |
| 49 | |
| 50 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 51 | * In a system, a certain number of power domain instances are present at a |
| 52 | * power level. The cumulative number of instances across all levels are |
| 53 | * stored in 'psci_pwr_domain_map'. The topology tree has been flattenned into |
| 54 | * this array. To retrieve nodes, information about the extents of each power |
| 55 | * level i.e. start index and end index needs to be present. |
| 56 | * 'psci_pwr_lvl_limits' stores this information. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 57 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 58 | pwr_lvl_limits_node_t psci_pwr_lvl_limits[MPIDR_MAX_AFFLVL + 1]; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 59 | |
| 60 | /****************************************************************************** |
| 61 | * Define the psci capability variable. |
| 62 | *****************************************************************************/ |
| 63 | uint32_t psci_caps; |
| 64 | |
| 65 | |
| 66 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 67 | * Routines for retrieving the node corresponding to a power domain instance |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 68 | * in the mpidr. The first one uses binary search to find the node corresponding |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 69 | * to the mpidr (key) at a particular power level. The second routine decides |
| 70 | * extents of the binary search at each power level. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 71 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 72 | static int psci_pwr_domain_map_get_idx(unsigned long key, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 73 | int min_idx, |
| 74 | int max_idx) |
| 75 | { |
| 76 | int mid; |
| 77 | |
| 78 | /* |
| 79 | * Terminating condition: If the max and min indices have crossed paths |
| 80 | * during the binary search then the key has not been found. |
| 81 | */ |
| 82 | if (max_idx < min_idx) |
| 83 | return PSCI_E_INVALID_PARAMS; |
| 84 | |
| 85 | /* |
| 86 | * Make sure we are within array limits. |
| 87 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 88 | assert(min_idx >= 0 && max_idx < PSCI_NUM_PWR_DOMAINS); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Bisect the array around 'mid' and then recurse into the array chunk |
| 92 | * where the key is likely to be found. The mpidrs in each node in the |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 93 | * 'psci_pwr_domain_map' for a given power level are stored in an |
| 94 | * ascending order which makes the binary search possible. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 95 | */ |
| 96 | mid = min_idx + ((max_idx - min_idx) >> 1); /* Divide by 2 */ |
| 97 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 98 | if (psci_pwr_domain_map[mid].mpidr > key) |
| 99 | return psci_pwr_domain_map_get_idx(key, min_idx, mid - 1); |
| 100 | else if (psci_pwr_domain_map[mid].mpidr < key) |
| 101 | return psci_pwr_domain_map_get_idx(key, mid + 1, max_idx); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 102 | else |
| 103 | return mid; |
| 104 | } |
| 105 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 106 | pwr_map_node_t *psci_get_pwr_map_node(unsigned long mpidr, int pwr_lvl) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 107 | { |
| 108 | int rc; |
| 109 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 110 | if (pwr_lvl > PLAT_MAX_PWR_LVL) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 111 | return NULL; |
| 112 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 113 | /* Right shift the mpidr to the required power level */ |
| 114 | mpidr = mpidr_mask_lower_afflvls(mpidr, pwr_lvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 115 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 116 | rc = psci_pwr_domain_map_get_idx(mpidr, |
| 117 | psci_pwr_lvl_limits[pwr_lvl].min, |
| 118 | psci_pwr_lvl_limits[pwr_lvl].max); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 119 | if (rc >= 0) |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 120 | return &psci_pwr_domain_map[rc]; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 121 | else |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | /******************************************************************************* |
| 126 | * This function populates an array with nodes corresponding to a given range of |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 127 | * power levels in an mpidr. It returns successfully only when the power |
| 128 | * levels are correct, the mpidr is valid i.e. no power level is absent from |
| 129 | * the topology tree & the power domain instance at level 0 is not absent. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 130 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 131 | int psci_get_pwr_map_nodes(unsigned long mpidr, |
| 132 | int start_pwrlvl, |
| 133 | int end_pwrlvl, |
| 134 | pwr_map_node_t *mpidr_nodes[]) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 135 | { |
| 136 | int rc = PSCI_E_INVALID_PARAMS, level; |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 137 | pwr_map_node_t *node; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 138 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 139 | rc = psci_check_pwrlvl_range(start_pwrlvl, end_pwrlvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 140 | if (rc != PSCI_E_SUCCESS) |
| 141 | return rc; |
| 142 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 143 | for (level = start_pwrlvl; level <= end_pwrlvl; level++) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 144 | |
| 145 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 146 | * Grab the node for each power level. No power level |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 147 | * can be missing as that would mean that the topology tree |
| 148 | * is corrupted. |
| 149 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 150 | node = psci_get_pwr_map_node(mpidr, level); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 151 | if (node == NULL) { |
| 152 | rc = PSCI_E_INVALID_PARAMS; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 157 | * Skip absent power levels unless it's power level 0. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 158 | * An absent cpu means that the mpidr is invalid. Save the |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 159 | * pointer to the node for the present power level |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 160 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 161 | if (!(node->state & PSCI_PWR_DOMAIN_PRESENT)) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 162 | if (level == MPIDR_AFFLVL0) { |
| 163 | rc = PSCI_E_INVALID_PARAMS; |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | mpidr_nodes[level] = NULL; |
| 168 | } else |
| 169 | mpidr_nodes[level] = node; |
| 170 | } |
| 171 | |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 176 | * Function which initializes the 'pwr_map_node' corresponding to a power |
| 177 | * domain instance. Each node has a unique mpidr, level and bakery lock. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 178 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 179 | static void psci_init_pwr_map_node(unsigned long mpidr, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 180 | int level, |
| 181 | unsigned int idx) |
| 182 | { |
| 183 | unsigned char state; |
| 184 | uint32_t linear_id; |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 185 | psci_pwr_domain_map[idx].mpidr = mpidr; |
| 186 | psci_pwr_domain_map[idx].level = level; |
| 187 | psci_lock_init(psci_pwr_domain_map, idx); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 188 | |
| 189 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 190 | * If an power domain instance is present then mark it as OFF |
| 191 | * to begin with. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 192 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 193 | state = plat_get_pwr_domain_state(level, mpidr); |
| 194 | psci_pwr_domain_map[idx].state = state; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 195 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 196 | /* |
| 197 | * Check if this is a CPU node and is present in which case certain |
| 198 | * other initialisations are required. |
| 199 | */ |
| 200 | if (level != MPIDR_AFFLVL0) |
| 201 | return; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 202 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 203 | if (!(state & PSCI_PWR_DOMAIN_PRESENT)) |
| 204 | return; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 205 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 206 | /* |
| 207 | * Mark the cpu as OFF. Higher power level reference counts |
| 208 | * have already been memset to 0 |
| 209 | */ |
| 210 | psci_set_state(&psci_pwr_domain_map[idx], PSCI_STATE_OFF); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 211 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 212 | /* |
| 213 | * Associate a non-secure context with this power |
| 214 | * instance through the context management library. |
| 215 | */ |
| 216 | linear_id = plat_core_pos_by_mpidr(mpidr); |
| 217 | assert(linear_id < PLATFORM_CORE_COUNT); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 218 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 219 | /* Invalidate the suspend context for the node */ |
| 220 | set_cpu_data_by_index(linear_id, |
| 221 | psci_svc_cpu_data.power_state, |
| 222 | PSCI_INVALID_DATA); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 223 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 224 | flush_cpu_data_by_index(linear_id, psci_svc_cpu_data); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 225 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 226 | cm_set_context_by_index(linear_id, |
| 227 | (void *) &psci_ns_context[linear_id], |
| 228 | NON_SECURE); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /******************************************************************************* |
| 232 | * Core routine used by the Breadth-First-Search algorithm to populate the |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 233 | * power domain tree. Each level in the tree corresponds to a power level. This |
| 234 | * routine's aim is to traverse to the target power level and populate nodes |
| 235 | * in the 'psci_pwr_domain_map' for all the siblings at that level. It uses the |
| 236 | * current power level to keep track of how many levels from the root of the |
| 237 | * tree have been traversed. If the current power level != target power level, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 238 | * then the platform is asked to return the number of children that each |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 239 | * power domain instance has at the current power level. Traversal is then done |
| 240 | * for each child at the next lower level i.e. current power level - 1. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 241 | * |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 242 | * CAUTION: This routine assumes that power domain instance ids are allocated |
| 243 | * in a monotonically increasing manner at each power level in a mpidr starting |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 244 | * from 0. If the platform breaks this assumption then this code will have to |
| 245 | * be reworked accordingly. |
| 246 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 247 | static unsigned int psci_init_pwr_map(unsigned long mpidr, |
| 248 | unsigned int pwrmap_idx, |
| 249 | int cur_pwrlvl, |
| 250 | int tgt_pwrlvl) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 251 | { |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 252 | unsigned int ctr, pwr_inst_count; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 253 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 254 | assert(cur_pwrlvl >= tgt_pwrlvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 255 | |
| 256 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 257 | * Find the number of siblings at the current power level & |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 258 | * assert if there are none 'cause then we have been invoked with |
| 259 | * an invalid mpidr. |
| 260 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 261 | pwr_inst_count = plat_get_pwr_domain_count(cur_pwrlvl, mpidr); |
| 262 | assert(pwr_inst_count); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 263 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 264 | if (tgt_pwrlvl < cur_pwrlvl) { |
| 265 | for (ctr = 0; ctr < pwr_inst_count; ctr++) { |
| 266 | mpidr = mpidr_set_pwr_domain_inst(mpidr, ctr, |
| 267 | cur_pwrlvl); |
| 268 | pwrmap_idx = psci_init_pwr_map(mpidr, |
| 269 | pwrmap_idx, |
| 270 | cur_pwrlvl - 1, |
| 271 | tgt_pwrlvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 272 | } |
| 273 | } else { |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 274 | for (ctr = 0; ctr < pwr_inst_count; ctr++, pwrmap_idx++) { |
| 275 | mpidr = mpidr_set_pwr_domain_inst(mpidr, ctr, |
| 276 | cur_pwrlvl); |
| 277 | psci_init_pwr_map_node(mpidr, cur_pwrlvl, pwrmap_idx); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 280 | /* pwrmap_idx is 1 greater than the max index of cur_pwrlvl */ |
| 281 | psci_pwr_lvl_limits[cur_pwrlvl].max = pwrmap_idx - 1; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 284 | return pwrmap_idx; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /******************************************************************************* |
| 288 | * This function initializes the topology tree by querying the platform. To do |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 289 | * so, it's helper routines implement a Breadth-First-Search. At each power |
| 290 | * level the platform conveys the number of power domain instances that exist |
| 291 | * i.e. the power instance count. The algorithm populates the |
| 292 | * psci_pwr_domain_map* recursively using this information. On a platform that |
| 293 | * implements two clusters of 4 cpus each, the populated pwr_map_array would |
| 294 | * look like this: |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 295 | * |
| 296 | * <- cpus cluster0 -><- cpus cluster1 -> |
| 297 | * --------------------------------------------------- |
| 298 | * | 0 | 1 | 0 | 1 | 2 | 3 | 0 | 1 | 2 | 3 | |
| 299 | * --------------------------------------------------- |
| 300 | * ^ ^ |
| 301 | * cluster __| cpu __| |
| 302 | * limit limit |
| 303 | * |
| 304 | * The first 2 entries are of the cluster nodes. The next 4 entries are of cpus |
| 305 | * within cluster 0. The last 4 entries are of cpus within cluster 1. |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 306 | * The 'psci_pwr_lvl_limits' array contains the max & min index of each power |
| 307 | * level within the 'psci_pwr_domain_map' array. This allows restricting search |
| 308 | * of a node at a power level between the indices in the limits array. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 309 | ******************************************************************************/ |
| 310 | int32_t psci_setup(void) |
| 311 | { |
| 312 | unsigned long mpidr = read_mpidr(); |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 313 | int pwrlvl, pwrmap_idx, max_pwrlvl; |
| 314 | pwr_map_node_t *node; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 315 | |
| 316 | psci_plat_pm_ops = NULL; |
| 317 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 318 | /* Find out the maximum power level that the platform implements */ |
| 319 | max_pwrlvl = PLAT_MAX_PWR_LVL; |
| 320 | assert(max_pwrlvl <= MPIDR_MAX_AFFLVL); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * This call traverses the topology tree with help from the platform and |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 324 | * populates the power map using a breadth-first-search recursively. |
| 325 | * We assume that the platform allocates power domain instance ids from |
| 326 | * 0 onwards at each power level in the mpidr. FIRST_MPIDR = 0.0.0.0 |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 327 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 328 | pwrmap_idx = 0; |
| 329 | for (pwrlvl = max_pwrlvl; pwrlvl >= MPIDR_AFFLVL0; pwrlvl--) { |
| 330 | pwrmap_idx = psci_init_pwr_map(FIRST_MPIDR, |
| 331 | pwrmap_idx, |
| 332 | max_pwrlvl, |
| 333 | pwrlvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | #if !USE_COHERENT_MEM |
| 337 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 338 | * The psci_pwr_domain_map only needs flushing when it's not allocated |
| 339 | * in coherent memory. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 340 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 341 | flush_dcache_range((uint64_t) &psci_pwr_domain_map, |
| 342 | sizeof(psci_pwr_domain_map)); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 343 | #endif |
| 344 | |
| 345 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 346 | * Set the bounds for number of instances of each level in the map. Also |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 347 | * flush out the entire array so that it's visible to subsequent power |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 348 | * management operations. The 'psci_pwr_lvl_limits' array is allocated |
| 349 | * in normal memory. It will be accessed when the mmu is off e.g. after |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 350 | * reset. Hence it needs to be flushed. |
| 351 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 352 | for (pwrlvl = MPIDR_AFFLVL0; pwrlvl < max_pwrlvl; pwrlvl++) { |
| 353 | psci_pwr_lvl_limits[pwrlvl].min = |
| 354 | psci_pwr_lvl_limits[pwrlvl + 1].max + 1; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 357 | flush_dcache_range((unsigned long) psci_pwr_lvl_limits, |
| 358 | sizeof(psci_pwr_lvl_limits)); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 359 | |
| 360 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 361 | * Mark the power domain instances in our mpidr as ON. No need to lock |
| 362 | * as this is the primary cpu. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 363 | */ |
| 364 | mpidr &= MPIDR_AFFINITY_MASK; |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 365 | for (pwrlvl = MPIDR_AFFLVL0; pwrlvl <= max_pwrlvl; pwrlvl++) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 366 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 367 | node = psci_get_pwr_map_node(mpidr, pwrlvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 368 | assert(node); |
| 369 | |
| 370 | /* Mark each present node as ON. */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 371 | if (node->state & PSCI_PWR_DOMAIN_PRESENT) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 372 | psci_set_state(node, PSCI_STATE_ON); |
| 373 | } |
| 374 | |
| 375 | platform_setup_pm(&psci_plat_pm_ops); |
| 376 | assert(psci_plat_pm_ops); |
| 377 | |
| 378 | /* Initialize the psci capability */ |
| 379 | psci_caps = PSCI_GENERIC_CAP; |
| 380 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 381 | if (psci_plat_pm_ops->pwr_domain_off) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 382 | psci_caps |= define_psci_cap(PSCI_CPU_OFF); |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 383 | if (psci_plat_pm_ops->pwr_domain_on && |
| 384 | psci_plat_pm_ops->pwr_domain_on_finish) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 385 | psci_caps |= define_psci_cap(PSCI_CPU_ON_AARCH64); |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 386 | if (psci_plat_pm_ops->pwr_domain_suspend && |
| 387 | psci_plat_pm_ops->pwr_domain_suspend_finish) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 388 | psci_caps |= define_psci_cap(PSCI_CPU_SUSPEND_AARCH64); |
| 389 | if (psci_plat_pm_ops->get_sys_suspend_power_state) |
| 390 | psci_caps |= define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64); |
| 391 | } |
| 392 | if (psci_plat_pm_ops->system_off) |
| 393 | psci_caps |= define_psci_cap(PSCI_SYSTEM_OFF); |
| 394 | if (psci_plat_pm_ops->system_reset) |
| 395 | psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET); |
| 396 | |
| 397 | return 0; |
| 398 | } |