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 | 97043ac | 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 | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <assert.h> |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 34 | #include <runtime_svc.h> |
| 35 | #include <debug.h> |
Dan Handley | 35e98e5 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 36 | #include "psci_private.h" |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 37 | |
| 38 | /******************************************************************************* |
| 39 | * PSCI frontend api for servicing SMCs. Described in the PSCI spec. |
| 40 | ******************************************************************************/ |
| 41 | int psci_cpu_on(unsigned long target_cpu, |
| 42 | unsigned long entrypoint, |
| 43 | unsigned long context_id) |
| 44 | |
| 45 | { |
| 46 | int rc; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 47 | unsigned int start_afflvl, end_afflvl; |
Soby Mathew | 78879b9 | 2015-01-06 15:36:38 +0000 | [diff] [blame^] | 48 | entry_point_info_t ep; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 49 | |
| 50 | /* Determine if the cpu exists of not */ |
| 51 | rc = psci_validate_mpidr(target_cpu, MPIDR_AFFLVL0); |
| 52 | if (rc != PSCI_E_SUCCESS) { |
| 53 | goto exit; |
| 54 | } |
| 55 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 56 | /* |
Soby Mathew | 78879b9 | 2015-01-06 15:36:38 +0000 | [diff] [blame^] | 57 | * Verify and derive the re-entry information for |
| 58 | * the non-secure world from the non-secure state from |
| 59 | * where this call originated. |
| 60 | */ |
| 61 | rc = psci_get_ns_ep_info(&ep, entrypoint, context_id); |
| 62 | if (rc != PSCI_E_SUCCESS) |
| 63 | return rc; |
| 64 | |
| 65 | |
| 66 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 67 | * To turn this cpu on, specify which affinity |
| 68 | * levels need to be turned on |
| 69 | */ |
| 70 | start_afflvl = MPIDR_AFFLVL0; |
| 71 | end_afflvl = get_max_afflvl(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 72 | rc = psci_afflvl_on(target_cpu, |
Soby Mathew | 78879b9 | 2015-01-06 15:36:38 +0000 | [diff] [blame^] | 73 | &ep, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 74 | start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 75 | end_afflvl); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 76 | |
| 77 | exit: |
| 78 | return rc; |
| 79 | } |
| 80 | |
| 81 | unsigned int psci_version(void) |
| 82 | { |
| 83 | return PSCI_MAJOR_VER | PSCI_MINOR_VER; |
| 84 | } |
| 85 | |
| 86 | int psci_cpu_suspend(unsigned int power_state, |
| 87 | unsigned long entrypoint, |
| 88 | unsigned long context_id) |
| 89 | { |
| 90 | int rc; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 91 | unsigned int target_afflvl, pstate_type; |
Soby Mathew | 78879b9 | 2015-01-06 15:36:38 +0000 | [diff] [blame^] | 92 | entry_point_info_t ep; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 93 | |
Vikram Kanigiri | 759ec93 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 94 | /* Check SBZ bits in power state are zero */ |
| 95 | if (psci_validate_power_state(power_state)) |
| 96 | return PSCI_E_INVALID_PARAMS; |
| 97 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 98 | /* Sanity check the requested state */ |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 99 | target_afflvl = psci_get_pstate_afflvl(power_state); |
Soby Mathew | 264999f | 2014-10-02 17:24:19 +0100 | [diff] [blame] | 100 | if (target_afflvl > get_max_afflvl()) |
Vikram Kanigiri | d118f9f | 2014-03-21 11:57:10 +0000 | [diff] [blame] | 101 | return PSCI_E_INVALID_PARAMS; |
| 102 | |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 103 | /* Determine the 'state type' in the 'power_state' parameter */ |
Vikram Kanigiri | d118f9f | 2014-03-21 11:57:10 +0000 | [diff] [blame] | 104 | pstate_type = psci_get_pstate_type(power_state); |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Ensure that we have a platform specific handler for entering |
| 108 | * a standby state. |
| 109 | */ |
Vikram Kanigiri | d118f9f | 2014-03-21 11:57:10 +0000 | [diff] [blame] | 110 | if (pstate_type == PSTATE_TYPE_STANDBY) { |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 111 | if (!psci_plat_pm_ops->affinst_standby) |
Vikram Kanigiri | d118f9f | 2014-03-21 11:57:10 +0000 | [diff] [blame] | 112 | return PSCI_E_INVALID_PARAMS; |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 113 | |
| 114 | rc = psci_plat_pm_ops->affinst_standby(power_state); |
| 115 | assert(rc == PSCI_E_INVALID_PARAMS || rc == PSCI_E_SUCCESS); |
| 116 | return rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 119 | /* |
Soby Mathew | 78879b9 | 2015-01-06 15:36:38 +0000 | [diff] [blame^] | 120 | * Verify and derive the re-entry information for |
| 121 | * the non-secure world from the non-secure state from |
| 122 | * where this call originated. |
| 123 | */ |
| 124 | rc = psci_get_ns_ep_info(&ep, entrypoint, context_id); |
| 125 | if (rc != PSCI_E_SUCCESS) |
| 126 | return rc; |
| 127 | |
| 128 | /* |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 129 | * Do what is needed to enter the power down state. Upon success, |
| 130 | * enter the final wfi which will power down this cpu else return |
| 131 | * an error. |
| 132 | */ |
Soby Mathew | 78879b9 | 2015-01-06 15:36:38 +0000 | [diff] [blame^] | 133 | rc = psci_afflvl_suspend(&ep, |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 134 | power_state, |
| 135 | MPIDR_AFFLVL0, |
| 136 | target_afflvl); |
| 137 | if (rc == PSCI_E_SUCCESS) |
| 138 | psci_power_down_wfi(); |
| 139 | assert(rc == PSCI_E_INVALID_PARAMS); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | int psci_cpu_off(void) |
| 144 | { |
| 145 | int rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 146 | int target_afflvl = get_max_afflvl(); |
| 147 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 148 | /* |
| 149 | * Traverse from the highest to the lowest affinity level. When the |
| 150 | * lowest affinity level is hit, all the locks are acquired. State |
| 151 | * management is done immediately followed by cpu, cluster ... |
| 152 | * ..target_afflvl specific actions as this function unwinds back. |
| 153 | */ |
Andrew Thoelke | 56378aa | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 154 | rc = psci_afflvl_off(MPIDR_AFFLVL0, target_afflvl); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 155 | |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 156 | /* |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 157 | * Check if all actions needed to safely power down this cpu have |
| 158 | * successfully completed. Enter a wfi loop which will allow the |
| 159 | * power controller to physically power down this cpu. |
| 160 | */ |
| 161 | if (rc == PSCI_E_SUCCESS) |
| 162 | psci_power_down_wfi(); |
| 163 | |
| 164 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 165 | * The only error cpu_off can return is E_DENIED. So check if that's |
| 166 | * indeed the case. |
| 167 | */ |
Achin Gupta | 317ba09 | 2014-05-09 19:32:25 +0100 | [diff] [blame] | 168 | assert (rc == PSCI_E_DENIED); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 169 | |
| 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | int psci_affinity_info(unsigned long target_affinity, |
| 174 | unsigned int lowest_affinity_level) |
| 175 | { |
| 176 | int rc = PSCI_E_INVALID_PARAMS; |
| 177 | unsigned int aff_state; |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 178 | aff_map_node_t *node; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 179 | |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 180 | if (lowest_affinity_level > get_max_afflvl()) |
| 181 | return rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 182 | |
| 183 | node = psci_get_aff_map_node(target_affinity, lowest_affinity_level); |
| 184 | if (node && (node->state & PSCI_AFF_PRESENT)) { |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * TODO: For affinity levels higher than 0 i.e. cpu, the |
| 188 | * state will always be either ON or OFF. Need to investigate |
| 189 | * how critical is it to support ON_PENDING here. |
| 190 | */ |
| 191 | aff_state = psci_get_state(node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 192 | |
| 193 | /* A suspended cpu is available & on for the OS */ |
| 194 | if (aff_state == PSCI_STATE_SUSPEND) { |
| 195 | aff_state = PSCI_STATE_ON; |
| 196 | } |
| 197 | |
| 198 | rc = aff_state; |
| 199 | } |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 200 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 201 | return rc; |
| 202 | } |
| 203 | |
| 204 | /* Unimplemented */ |
| 205 | int psci_migrate(unsigned int target_cpu) |
| 206 | { |
| 207 | return PSCI_E_NOT_SUPPORTED; |
| 208 | } |
| 209 | |
| 210 | /* Unimplemented */ |
| 211 | unsigned int psci_migrate_info_type(void) |
| 212 | { |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 213 | return PSCI_TOS_NOT_PRESENT_MP; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | unsigned long psci_migrate_info_up_cpu(void) |
| 217 | { |
| 218 | /* |
| 219 | * Return value of this currently unsupported call depends upon |
| 220 | * what psci_migrate_info_type() returns. |
| 221 | */ |
| 222 | return PSCI_E_SUCCESS; |
| 223 | } |
| 224 | |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 225 | /******************************************************************************* |
| 226 | * PSCI top level handler for servicing SMCs. |
| 227 | ******************************************************************************/ |
| 228 | uint64_t psci_smc_handler(uint32_t smc_fid, |
| 229 | uint64_t x1, |
| 230 | uint64_t x2, |
| 231 | uint64_t x3, |
| 232 | uint64_t x4, |
| 233 | void *cookie, |
| 234 | void *handle, |
| 235 | uint64_t flags) |
| 236 | { |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 237 | if (is_caller_secure(flags)) |
| 238 | SMC_RET1(handle, SMC_UNK); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 239 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 240 | if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { |
| 241 | /* 32-bit PSCI function, clear top parameter bits */ |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 242 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 243 | x1 = (uint32_t)x1; |
| 244 | x2 = (uint32_t)x2; |
| 245 | x3 = (uint32_t)x3; |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 246 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 247 | switch (smc_fid) { |
| 248 | case PSCI_VERSION: |
| 249 | SMC_RET1(handle, psci_version()); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 250 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 251 | case PSCI_CPU_OFF: |
Achin Gupta | b51da82 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 252 | SMC_RET1(handle, psci_cpu_off()); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 253 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 254 | case PSCI_CPU_SUSPEND_AARCH32: |
Achin Gupta | b51da82 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 255 | SMC_RET1(handle, psci_cpu_suspend(x1, x2, x3)); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 256 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 257 | case PSCI_CPU_ON_AARCH32: |
| 258 | SMC_RET1(handle, psci_cpu_on(x1, x2, x3)); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 259 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 260 | case PSCI_AFFINITY_INFO_AARCH32: |
| 261 | SMC_RET1(handle, psci_affinity_info(x1, x2)); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 262 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 263 | case PSCI_MIG_AARCH32: |
| 264 | SMC_RET1(handle, psci_migrate(x1)); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 265 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 266 | case PSCI_MIG_INFO_TYPE: |
| 267 | SMC_RET1(handle, psci_migrate_info_type()); |
| 268 | |
| 269 | case PSCI_MIG_INFO_UP_CPU_AARCH32: |
| 270 | SMC_RET1(handle, psci_migrate_info_up_cpu()); |
| 271 | |
Juan Castillo | d5f1309 | 2014-08-12 11:17:06 +0100 | [diff] [blame] | 272 | case PSCI_SYSTEM_OFF: |
| 273 | psci_system_off(); |
| 274 | /* We should never return from psci_system_off() */ |
| 275 | |
| 276 | case PSCI_SYSTEM_RESET: |
| 277 | psci_system_reset(); |
| 278 | /* We should never return from psci_system_reset() */ |
| 279 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | } else { |
| 284 | /* 64-bit PSCI function */ |
| 285 | |
| 286 | switch (smc_fid) { |
| 287 | case PSCI_CPU_SUSPEND_AARCH64: |
Achin Gupta | b51da82 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 288 | SMC_RET1(handle, psci_cpu_suspend(x1, x2, x3)); |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 289 | |
| 290 | case PSCI_CPU_ON_AARCH64: |
| 291 | SMC_RET1(handle, psci_cpu_on(x1, x2, x3)); |
| 292 | |
| 293 | case PSCI_AFFINITY_INFO_AARCH64: |
| 294 | SMC_RET1(handle, psci_affinity_info(x1, x2)); |
| 295 | |
| 296 | case PSCI_MIG_AARCH64: |
| 297 | SMC_RET1(handle, psci_migrate(x1)); |
| 298 | |
| 299 | case PSCI_MIG_INFO_UP_CPU_AARCH64: |
| 300 | SMC_RET1(handle, psci_migrate_info_up_cpu()); |
| 301 | |
| 302 | default: |
| 303 | break; |
| 304 | } |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Andrew Thoelke | 5003eca | 2014-06-10 16:37:37 +0100 | [diff] [blame] | 307 | WARN("Unimplemented PSCI Call: 0x%x \n", smc_fid); |
| 308 | SMC_RET1(handle, SMC_UNK); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 309 | } |