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 <debug.h> |
| 38 | #include <platform.h> |
| 39 | #include <string.h> |
| 40 | #include "psci_private.h" |
| 41 | |
| 42 | /* |
| 43 | * SPD power management operations, expected to be supplied by the registered |
| 44 | * SPD on successful SP initialization |
| 45 | */ |
| 46 | const spd_pm_ops_t *psci_spd_pm; |
| 47 | |
| 48 | /******************************************************************************* |
| 49 | * Grand array that holds the platform's topology information for state |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 50 | * management of power domain instances. Each node (pwr_map_node) in the array |
| 51 | * corresponds to a power domain instance e.g. cluster, cpu within an mpidr |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 52 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 53 | pwr_map_node_t psci_pwr_domain_map[PSCI_NUM_PWR_DOMAINS] |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 54 | #if USE_COHERENT_MEM |
| 55 | __attribute__ ((section("tzfw_coherent_mem"))) |
| 56 | #endif |
| 57 | ; |
| 58 | |
| 59 | /******************************************************************************* |
| 60 | * Pointer to functions exported by the platform to complete power mgmt. ops |
| 61 | ******************************************************************************/ |
| 62 | const plat_pm_ops_t *psci_plat_pm_ops; |
| 63 | |
| 64 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 65 | * Check that the maximum power level supported by the platform makes sense |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 66 | * ****************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 67 | CASSERT(PLAT_MAX_PWR_LVL <= MPIDR_MAX_AFFLVL && \ |
| 68 | PLAT_MAX_PWR_LVL >= MPIDR_AFFLVL0, \ |
| 69 | assert_platform_max_pwrlvl_check); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 70 | |
| 71 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 72 | * This function is passed an array of pointers to power domain nodes in the |
| 73 | * topology tree for an mpidr. It iterates through the nodes to find the |
| 74 | * highest power level where the power domain is marked as physically powered |
| 75 | * off. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 76 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 77 | uint32_t psci_find_max_phys_off_pwrlvl(uint32_t start_pwrlvl, |
| 78 | uint32_t end_pwrlvl, |
| 79 | pwr_map_node_t *mpidr_nodes[]) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 80 | { |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 81 | uint32_t max_pwrlvl = PSCI_INVALID_DATA; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 82 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 83 | for (; start_pwrlvl <= end_pwrlvl; start_pwrlvl++) { |
| 84 | if (mpidr_nodes[start_pwrlvl] == NULL) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 85 | continue; |
| 86 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 87 | if (psci_get_phys_state(mpidr_nodes[start_pwrlvl]) == |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 88 | PSCI_STATE_OFF) |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 89 | max_pwrlvl = start_pwrlvl; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 92 | return max_pwrlvl; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /******************************************************************************* |
| 96 | * This function verifies that the all the other cores in the system have been |
| 97 | * turned OFF and the current CPU is the last running CPU in the system. |
| 98 | * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false) |
| 99 | * otherwise. |
| 100 | ******************************************************************************/ |
| 101 | unsigned int psci_is_last_on_cpu(void) |
| 102 | { |
| 103 | unsigned long mpidr = read_mpidr_el1() & MPIDR_AFFINITY_MASK; |
| 104 | unsigned int i; |
| 105 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 106 | for (i = psci_pwr_lvl_limits[MPIDR_AFFLVL0].min; |
| 107 | i <= psci_pwr_lvl_limits[MPIDR_AFFLVL0].max; i++) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 108 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 109 | assert(psci_pwr_domain_map[i].level == MPIDR_AFFLVL0); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 110 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 111 | if (!(psci_pwr_domain_map[i].state & PSCI_AFF_PRESENT)) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 112 | continue; |
| 113 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 114 | if (psci_pwr_domain_map[i].mpidr == mpidr) { |
| 115 | assert(psci_get_state(&psci_pwr_domain_map[i]) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 116 | == PSCI_STATE_ON); |
| 117 | continue; |
| 118 | } |
| 119 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 120 | if (psci_get_state(&psci_pwr_domain_map[i]) != PSCI_STATE_OFF) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 128 | * Routine to return the maximum power level to traverse to after a cpu has |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 129 | * been physically powered up. It is expected to be called immediately after |
| 130 | * reset from assembler code. |
| 131 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 132 | int get_power_on_target_pwrlvl(void) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 133 | { |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 134 | int pwrlvl; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 135 | |
| 136 | #if DEBUG |
| 137 | unsigned int state; |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 138 | pwr_map_node_t *node; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 139 | |
| 140 | /* Retrieve our node from the topology tree */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 141 | node = psci_get_pwr_map_node(read_mpidr_el1() & MPIDR_AFFINITY_MASK, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 142 | MPIDR_AFFLVL0); |
| 143 | assert(node); |
| 144 | |
| 145 | /* |
| 146 | * Sanity check the state of the cpu. It should be either suspend or "on |
| 147 | * pending" |
| 148 | */ |
| 149 | state = psci_get_state(node); |
| 150 | assert(state == PSCI_STATE_SUSPEND || state == PSCI_STATE_ON_PENDING); |
| 151 | #endif |
| 152 | |
| 153 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 154 | * Assume that this cpu was suspended and retrieve its target power |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 155 | * level. If it is invalid then it could only have been turned off |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 156 | * earlier. PLAT_MAX_PWR_LVL will be the highest power level a |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 157 | * cpu can be turned off to. |
| 158 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 159 | pwrlvl = psci_get_suspend_pwrlvl(); |
| 160 | if (pwrlvl == PSCI_INVALID_DATA) |
| 161 | pwrlvl = PLAT_MAX_PWR_LVL; |
| 162 | return pwrlvl; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 166 | * Simple routine to set the id of a power domain instance at a given level |
| 167 | * in the mpidr. The assumption is that the affinity level and the power |
| 168 | * level are the same. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 169 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 170 | unsigned long mpidr_set_pwr_domain_inst(unsigned long mpidr, |
| 171 | unsigned char pwr_inst, |
| 172 | int pwr_lvl) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 173 | { |
| 174 | unsigned long aff_shift; |
| 175 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 176 | assert(pwr_lvl <= MPIDR_AFFLVL3); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * Decide the number of bits to shift by depending upon |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 180 | * the power level |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 181 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 182 | aff_shift = get_afflvl_shift(pwr_lvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 183 | |
| 184 | /* Clear the existing affinity instance & set the new one*/ |
| 185 | mpidr &= ~(((unsigned long)MPIDR_AFFLVL_MASK) << aff_shift); |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 186 | mpidr |= ((unsigned long)pwr_inst) << aff_shift; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 187 | |
| 188 | return mpidr; |
| 189 | } |
| 190 | |
| 191 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 192 | * This function sanity checks a range of power levels. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 193 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 194 | int psci_check_pwrlvl_range(int start_pwrlvl, int end_pwrlvl) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 195 | { |
| 196 | /* Sanity check the parameters passed */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 197 | if (end_pwrlvl > PLAT_MAX_PWR_LVL) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 198 | return PSCI_E_INVALID_PARAMS; |
| 199 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 200 | if (start_pwrlvl < MPIDR_AFFLVL0) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 201 | return PSCI_E_INVALID_PARAMS; |
| 202 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 203 | if (end_pwrlvl < start_pwrlvl) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 204 | return PSCI_E_INVALID_PARAMS; |
| 205 | |
| 206 | return PSCI_E_SUCCESS; |
| 207 | } |
| 208 | |
| 209 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 210 | * This function is passed an array of pointers to power domain nodes in the |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 211 | * topology tree for an mpidr and the state which each node should transition |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 212 | * to. It updates the state of each node between the specified power levels. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 213 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 214 | void psci_do_state_coordination(uint32_t start_pwrlvl, |
| 215 | uint32_t end_pwrlvl, |
| 216 | pwr_map_node_t *mpidr_nodes[], |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 217 | uint32_t state) |
| 218 | { |
| 219 | uint32_t level; |
| 220 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 221 | for (level = start_pwrlvl; level <= end_pwrlvl; level++) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 222 | if (mpidr_nodes[level] == NULL) |
| 223 | continue; |
| 224 | psci_set_state(mpidr_nodes[level], state); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 229 | * This function is passed an array of pointers to power domain nodes in the |
| 230 | * topology tree for an mpidr. It picks up locks for each power level bottom |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 231 | * up in the range specified. |
| 232 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 233 | void psci_acquire_pwr_domain_locks(int start_pwrlvl, |
| 234 | int end_pwrlvl, |
| 235 | pwr_map_node_t *mpidr_nodes[]) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 236 | { |
| 237 | int level; |
| 238 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 239 | for (level = start_pwrlvl; level <= end_pwrlvl; level++) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 240 | if (mpidr_nodes[level] == NULL) |
| 241 | continue; |
| 242 | |
| 243 | psci_lock_get(mpidr_nodes[level]); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 248 | * This function is passed an array of pointers to power domain nodes in the |
| 249 | * topology tree for an mpidr. It releases the lock for each power level top |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 250 | * down in the range specified. |
| 251 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 252 | void psci_release_pwr_domain_locks(int start_pwrlvl, |
| 253 | int end_pwrlvl, |
| 254 | pwr_map_node_t *mpidr_nodes[]) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 255 | { |
| 256 | int level; |
| 257 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 258 | for (level = end_pwrlvl; level >= start_pwrlvl; level--) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 259 | if (mpidr_nodes[level] == NULL) |
| 260 | continue; |
| 261 | |
| 262 | psci_lock_release(mpidr_nodes[level]); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 267 | * Simple routine to determine whether an power domain instance at a given |
| 268 | * level in an mpidr exists or not. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 269 | ******************************************************************************/ |
| 270 | int psci_validate_mpidr(unsigned long mpidr, int level) |
| 271 | { |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 272 | pwr_map_node_t *node; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 273 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 274 | node = psci_get_pwr_map_node(mpidr, level); |
| 275 | if (node && (node->state & PSCI_PWR_DOMAIN_PRESENT)) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 276 | return PSCI_E_SUCCESS; |
| 277 | else |
| 278 | return PSCI_E_INVALID_PARAMS; |
| 279 | } |
| 280 | |
| 281 | /******************************************************************************* |
| 282 | * This function determines the full entrypoint information for the requested |
| 283 | * PSCI entrypoint on power on/resume and returns it. |
| 284 | ******************************************************************************/ |
| 285 | int psci_get_ns_ep_info(entry_point_info_t *ep, |
| 286 | uint64_t entrypoint, uint64_t context_id) |
| 287 | { |
| 288 | uint32_t ep_attr, mode, sctlr, daif, ee; |
| 289 | uint32_t ns_scr_el3 = read_scr_el3(); |
| 290 | uint32_t ns_sctlr_el1 = read_sctlr_el1(); |
| 291 | |
| 292 | sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1; |
| 293 | ee = 0; |
| 294 | |
| 295 | ep_attr = NON_SECURE | EP_ST_DISABLE; |
| 296 | if (sctlr & SCTLR_EE_BIT) { |
| 297 | ep_attr |= EP_EE_BIG; |
| 298 | ee = 1; |
| 299 | } |
| 300 | SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr); |
| 301 | |
| 302 | ep->pc = entrypoint; |
| 303 | memset(&ep->args, 0, sizeof(ep->args)); |
| 304 | ep->args.arg0 = context_id; |
| 305 | |
| 306 | /* |
| 307 | * Figure out whether the cpu enters the non-secure address space |
| 308 | * in aarch32 or aarch64 |
| 309 | */ |
| 310 | if (ns_scr_el3 & SCR_RW_BIT) { |
| 311 | |
| 312 | /* |
| 313 | * Check whether a Thumb entry point has been provided for an |
| 314 | * aarch64 EL |
| 315 | */ |
| 316 | if (entrypoint & 0x1) |
| 317 | return PSCI_E_INVALID_PARAMS; |
| 318 | |
| 319 | mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1; |
| 320 | |
| 321 | ep->spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
| 322 | } else { |
| 323 | |
| 324 | mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc; |
| 325 | |
| 326 | /* |
| 327 | * TODO: Choose async. exception bits if HYP mode is not |
| 328 | * implemented according to the values of SCR.{AW, FW} bits |
| 329 | */ |
| 330 | daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT; |
| 331 | |
| 332 | ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif); |
| 333 | } |
| 334 | |
| 335 | return PSCI_E_SUCCESS; |
| 336 | } |
| 337 | |
| 338 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 339 | * This function takes a pointer to a power domain node in the topology tree |
| 340 | * and returns its state. State of a non-leaf node needs to be calculated. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 341 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 342 | unsigned short psci_get_state(pwr_map_node_t *node) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 343 | { |
| 344 | #if !USE_COHERENT_MEM |
| 345 | flush_dcache_range((uint64_t) node, sizeof(*node)); |
| 346 | #endif |
| 347 | |
| 348 | assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL); |
| 349 | |
| 350 | /* A cpu node just contains the state which can be directly returned */ |
| 351 | if (node->level == MPIDR_AFFLVL0) |
| 352 | return (node->state >> PSCI_STATE_SHIFT) & PSCI_STATE_MASK; |
| 353 | |
| 354 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 355 | * For a power level higher than a cpu, the state has to be |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 356 | * calculated. It depends upon the value of the reference count |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 357 | * which is managed by each node at the next lower power level |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 358 | * e.g. for a cluster, each cpu increments/decrements the reference |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 359 | * count. If the reference count is 0 then the power level is |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 360 | * OFF else ON. |
| 361 | */ |
| 362 | if (node->ref_count) |
| 363 | return PSCI_STATE_ON; |
| 364 | else |
| 365 | return PSCI_STATE_OFF; |
| 366 | } |
| 367 | |
| 368 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 369 | * This function takes a pointer to a power domain node in the topology |
| 370 | * tree and a target state. State of a non-leaf node needs to be converted |
| 371 | * to a reference count. State of a leaf node can be set directly. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 372 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 373 | void psci_set_state(pwr_map_node_t *node, unsigned short state) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 374 | { |
| 375 | assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL); |
| 376 | |
| 377 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 378 | * For a power level higher than a cpu, the state is used |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 379 | * to decide whether the reference count is incremented or |
| 380 | * decremented. Entry into the ON_PENDING state does not have |
| 381 | * effect. |
| 382 | */ |
| 383 | if (node->level > MPIDR_AFFLVL0) { |
| 384 | switch (state) { |
| 385 | case PSCI_STATE_ON: |
| 386 | node->ref_count++; |
| 387 | break; |
| 388 | case PSCI_STATE_OFF: |
| 389 | case PSCI_STATE_SUSPEND: |
| 390 | node->ref_count--; |
| 391 | break; |
| 392 | case PSCI_STATE_ON_PENDING: |
| 393 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 394 | * A power level higher than a cpu will not undergo |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 395 | * a state change when it is about to be turned on |
| 396 | */ |
| 397 | return; |
| 398 | default: |
| 399 | assert(0); |
| 400 | } |
| 401 | } else { |
| 402 | node->state &= ~(PSCI_STATE_MASK << PSCI_STATE_SHIFT); |
| 403 | node->state |= (state & PSCI_STATE_MASK) << PSCI_STATE_SHIFT; |
| 404 | } |
| 405 | |
| 406 | #if !USE_COHERENT_MEM |
| 407 | flush_dcache_range((uint64_t) node, sizeof(*node)); |
| 408 | #endif |
| 409 | } |
| 410 | |
| 411 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 412 | * A power domain could be on, on_pending, suspended or off. These are the |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 413 | * logical states it can be in. Physically either it is off or on. When it is in |
| 414 | * the state on_pending then it is about to be turned on. It is not possible to |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 415 | * tell whether that's actually happened or not. So we err on the side of |
| 416 | * caution & treat the power domain as being turned off. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 417 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 418 | unsigned short psci_get_phys_state(pwr_map_node_t *node) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 419 | { |
| 420 | unsigned int state; |
| 421 | |
| 422 | state = psci_get_state(node); |
| 423 | return get_phys_state(state); |
| 424 | } |
| 425 | |
| 426 | /******************************************************************************* |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 427 | * Generic handler which is called when a cpu is physically powered on. It |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 428 | * traverses the node information and finds the highest power level powered |
Soby Mathew | 6590ce2 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 429 | * off and performs generic, architectural, platform setup and state management |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 430 | * to power on that power level and power levels below it. |
Soby Mathew | 6590ce2 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 431 | * e.g. For a cpu that's been powered on, it will call the platform specific |
| 432 | * code to enable the gic cpu interface and for a cluster it will enable |
| 433 | * coherency at the interconnect level in addition to gic cpu interface. |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 434 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 435 | void psci_power_up_finish(int end_pwrlvl, |
| 436 | pwrlvl_power_on_finisher_t pon_handler) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 437 | { |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 438 | mpidr_pwr_map_nodes_t mpidr_nodes; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 439 | int rc; |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 440 | unsigned int max_phys_off_pwrlvl; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 441 | |
| 442 | |
| 443 | /* |
| 444 | * Collect the pointers to the nodes in the topology tree for |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 445 | * each power domain instances in the mpidr. If this function does |
| 446 | * not return successfully then either the mpidr or the power |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 447 | * levels are incorrect. Either case is an irrecoverable error. |
| 448 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 449 | rc = psci_get_pwr_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK, |
Soby Mathew | 6590ce2 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 450 | MPIDR_AFFLVL0, |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 451 | end_pwrlvl, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 452 | mpidr_nodes); |
| 453 | if (rc != PSCI_E_SUCCESS) |
| 454 | panic(); |
| 455 | |
| 456 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 457 | * This function acquires the lock corresponding to each power |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 458 | * level so that by the time all locks are taken, the system topology |
| 459 | * is snapshot and state management can be done safely. |
| 460 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 461 | psci_acquire_pwr_domain_locks(MPIDR_AFFLVL0, |
| 462 | end_pwrlvl, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 463 | mpidr_nodes); |
| 464 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 465 | max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(MPIDR_AFFLVL0, |
| 466 | end_pwrlvl, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 467 | mpidr_nodes); |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 468 | assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 469 | |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 470 | /* Perform generic, architecture and platform specific handling */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 471 | pon_handler(mpidr_nodes, max_phys_off_pwrlvl); |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 472 | |
| 473 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 474 | * This function updates the state of each power instance |
| 475 | * corresponding to the mpidr in the range of power levels |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 476 | * specified. |
| 477 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 478 | psci_do_state_coordination(MPIDR_AFFLVL0, |
| 479 | end_pwrlvl, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 480 | mpidr_nodes, |
| 481 | PSCI_STATE_ON); |
| 482 | |
| 483 | /* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 484 | * This loop releases the lock corresponding to each power level |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 485 | * in the reverse order to which they were acquired. |
| 486 | */ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 487 | psci_release_pwr_domain_locks(MPIDR_AFFLVL0, |
| 488 | end_pwrlvl, |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 489 | mpidr_nodes); |
| 490 | } |
| 491 | |
| 492 | /******************************************************************************* |
| 493 | * This function initializes the set of hooks that PSCI invokes as part of power |
| 494 | * management operation. The power management hooks are expected to be provided |
| 495 | * by the SPD, after it finishes all its initialization |
| 496 | ******************************************************************************/ |
| 497 | void psci_register_spd_pm_hook(const spd_pm_ops_t *pm) |
| 498 | { |
| 499 | assert(pm); |
| 500 | psci_spd_pm = pm; |
| 501 | |
| 502 | if (pm->svc_migrate) |
| 503 | psci_caps |= define_psci_cap(PSCI_MIG_AARCH64); |
| 504 | |
| 505 | if (pm->svc_migrate_info) |
| 506 | psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64) |
| 507 | | define_psci_cap(PSCI_MIG_INFO_TYPE); |
| 508 | } |
| 509 | |
| 510 | /******************************************************************************* |
| 511 | * This function invokes the migrate info hook in the spd_pm_ops. It performs |
| 512 | * the necessary return value validation. If the Secure Payload is UP and |
| 513 | * migrate capable, it returns the mpidr of the CPU on which the Secure payload |
| 514 | * is resident through the mpidr parameter. Else the value of the parameter on |
| 515 | * return is undefined. |
| 516 | ******************************************************************************/ |
| 517 | int psci_spd_migrate_info(uint64_t *mpidr) |
| 518 | { |
| 519 | int rc; |
| 520 | |
| 521 | if (!psci_spd_pm || !psci_spd_pm->svc_migrate_info) |
| 522 | return PSCI_E_NOT_SUPPORTED; |
| 523 | |
| 524 | rc = psci_spd_pm->svc_migrate_info(mpidr); |
| 525 | |
| 526 | assert(rc == PSCI_TOS_UP_MIG_CAP || rc == PSCI_TOS_NOT_UP_MIG_CAP \ |
| 527 | || rc == PSCI_TOS_NOT_PRESENT_MP || rc == PSCI_E_NOT_SUPPORTED); |
| 528 | |
| 529 | return rc; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | /******************************************************************************* |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 534 | * This function prints the state of all power domains present in the |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 535 | * system |
| 536 | ******************************************************************************/ |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 537 | void psci_print_power_domain_map(void) |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 538 | { |
| 539 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 540 | pwr_map_node_t *node; |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 541 | unsigned int idx; |
| 542 | /* This array maps to the PSCI_STATE_X definitions in psci.h */ |
| 543 | static const char *psci_state_str[] = { |
| 544 | "ON", |
| 545 | "OFF", |
| 546 | "ON_PENDING", |
| 547 | "SUSPEND" |
| 548 | }; |
| 549 | |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 550 | INFO("PSCI Power Domain Map:\n"); |
| 551 | for (idx = 0; idx < PSCI_NUM_PWR_DOMAINS; idx++) { |
| 552 | node = &psci_pwr_domain_map[idx]; |
| 553 | if (!(node->state & PSCI_PWR_DOMAIN_PRESENT)) { |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 554 | continue; |
| 555 | } |
Soby Mathew | 4067dc3 | 2015-05-05 16:33:16 +0100 | [diff] [blame^] | 556 | INFO(" pwrInst: Level %u, MPID 0x%lx, State %s\n", |
Soby Mathew | b48349e | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 557 | node->level, node->mpidr, |
| 558 | psci_state_str[psci_get_state(node)]); |
| 559 | } |
| 560 | #endif |
| 561 | } |