Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <granule.h> |
| 7 | #include <psci.h> |
| 8 | #include <realm.h> |
| 9 | #include <rec.h> |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 10 | #include <rsi-handler.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 11 | #include <smc-rmi.h> |
| 12 | #include <smc.h> |
| 13 | #include <stdint.h> |
| 14 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 15 | /* |
| 16 | * Copy @count GPRs from @rec to @rec_exit. |
| 17 | * The remaining @rec_exit.gprs[] values are zero filled. |
| 18 | */ |
| 19 | static void forward_args_to_host(unsigned int count, struct rec *rec, |
| 20 | struct rmi_rec_exit *rec_exit) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 22 | unsigned int i; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 23 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 24 | assert(count <= 4U); |
| 25 | |
| 26 | for (i = 0U; i < count; ++i) { |
| 27 | rec_exit->gprs[i] = rec->regs[i]; |
| 28 | } |
| 29 | |
| 30 | for (i = count; i < REC_EXIT_NR_GPRS; ++i) { |
| 31 | rec_exit->gprs[i] = 0UL; |
| 32 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 33 | } |
| 34 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 35 | static void psci_version(struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 36 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 37 | const unsigned long version_1_1 = (1UL << 16) | 1UL; |
| 38 | |
| 39 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 40 | res->smc_res.x[0] = version_1_1; |
| 41 | } |
| 42 | |
| 43 | static void psci_cpu_suspend(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 44 | struct rsi_result *res) |
| 45 | { |
| 46 | res->action = UPDATE_REC_EXIT_TO_HOST; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 47 | |
| 48 | /* |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 49 | * We treat all target power states as suspend requests, |
| 50 | * so all we need to do is forward the FID to the NS hypervisor, |
| 51 | * and we can ignore all the parameters. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 52 | */ |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 53 | forward_args_to_host(1U, rec, rec_exit); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 54 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 55 | /* |
| 56 | * The exit to the Host is just a notification; the Host does not need |
| 57 | * to complete a PSCI request before the next call to RMI_REC_ENTER. |
| 58 | * We therefore update the REC immediately with the results of the PSCI |
| 59 | * command. |
| 60 | */ |
| 61 | res->smc_res.x[0] = PSCI_RETURN_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 62 | } |
| 63 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 64 | static void psci_cpu_off(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 65 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 66 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 67 | res->action = UPDATE_REC_EXIT_TO_HOST; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * It should be fine to set this flag without holding a lock on the |
| 71 | * REC or without explicit memory barriers or ordering semantics |
| 72 | * operations, because we already ensure that a REC can only be in an |
| 73 | * executing state once at any given time, and we're in this execution |
| 74 | * context already, and we will be holding a reference count on the |
| 75 | * REC at this point, which will be dropped and re-evaluated with |
| 76 | * proper barriers before any CPU can evaluate the runnable field |
| 77 | * after this change. |
| 78 | */ |
| 79 | rec->runnable = false; |
| 80 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 81 | /* Notify the Host, passing the FID only. */ |
| 82 | forward_args_to_host(1U, rec, rec_exit); |
| 83 | |
| 84 | /* |
| 85 | * The exit to the Host is just a notification; the Host does not need |
| 86 | * to complete a PSCI request before the next call to RMI_REC_ENTER. |
| 87 | * We therefore update the REC immediately with the results of the PSCI |
| 88 | * command. |
| 89 | */ |
| 90 | res->smc_res.x[0] = PSCI_RETURN_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void psci_reset_rec(struct rec *rec, unsigned long caller_sctlr_el1) |
| 94 | { |
| 95 | /* Set execution level to EL1 (AArch64) and mask exceptions */ |
| 96 | rec->pstate = SPSR_EL2_MODE_EL1h | |
| 97 | SPSR_EL2_nRW_AARCH64 | |
| 98 | SPSR_EL2_F_BIT | |
| 99 | SPSR_EL2_I_BIT | |
| 100 | SPSR_EL2_A_BIT | |
| 101 | SPSR_EL2_D_BIT; |
| 102 | |
| 103 | /* Disable stage 1 MMU and caches */ |
| 104 | rec->sysregs.sctlr_el1 = SCTLR_EL1_FLAGS; |
| 105 | |
| 106 | /* Set the endianness of the target to that of the caller */ |
Arvind Ram Prakash | bd36a1b | 2022-12-15 12:16:36 -0600 | [diff] [blame] | 107 | rec->sysregs.sctlr_el1 |= caller_sctlr_el1 & SCTLR_ELx_EE_BIT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static unsigned long rd_map_read_rec_count(struct granule *g_rd) |
| 111 | { |
| 112 | unsigned long rec_count; |
| 113 | struct rd *rd = granule_map(g_rd, SLOT_RD); |
AlexeiFedorov | 4716542 | 2023-09-13 11:47:57 +0100 | [diff] [blame] | 114 | |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 115 | assert(rd != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 116 | |
| 117 | rec_count = get_rd_rec_count_unlocked(rd); |
| 118 | buffer_unmap(rd); |
| 119 | return rec_count; |
| 120 | } |
| 121 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 122 | static void psci_cpu_on(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 123 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 124 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 125 | unsigned long target_cpu = rec->regs[1]; |
| 126 | unsigned long entry_point_address = rec->regs[2]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 127 | unsigned long target_rec_idx; |
| 128 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 129 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 130 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 131 | /* Check that entry_point_address is a Protected Realm Address */ |
| 132 | if (!addr_in_rec_par(rec, entry_point_address)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 133 | res->smc_res.x[0] = PSCI_RETURN_INVALID_ADDRESS; |
| 134 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Get REC index from MPIDR */ |
| 138 | target_rec_idx = mpidr_to_rec_idx(target_cpu); |
| 139 | |
| 140 | /* |
| 141 | * Check that the target_cpu is a valid value. |
| 142 | * Note that the RMM enforces that the REC are created with |
| 143 | * consecutively increasing indexes starting from zero. |
| 144 | */ |
| 145 | if (target_rec_idx >= rd_map_read_rec_count(rec->realm_info.g_rd)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 146 | res->smc_res.x[0] = PSCI_RETURN_INVALID_PARAMS; |
| 147 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* Check if we're trying to turn ourselves on */ |
| 151 | if (target_rec_idx == rec->rec_idx) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 152 | res->smc_res.x[0] = PSCI_RETURN_ALREADY_ON; |
| 153 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 154 | } |
| 155 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 156 | /* Record that a PSCI request is outstanding */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 157 | rec->psci_info.pending = true; |
| 158 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 159 | /* |
| 160 | * Notify the Host, passing the FID and MPIDR arguments. |
| 161 | * Leave REC registers unchanged; these will be read and updated by |
| 162 | * psci_complete_request. |
| 163 | */ |
| 164 | forward_args_to_host(2U, rec, rec_exit); |
| 165 | res->action = EXIT_TO_HOST; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 168 | static void psci_affinity_info(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 169 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 170 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 171 | unsigned long target_affinity = rec->regs[1]; |
| 172 | unsigned long lowest_affinity_level = rec->regs[2]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 173 | unsigned long target_rec_idx; |
| 174 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 175 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 176 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 177 | if (lowest_affinity_level != 0UL) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 178 | res->smc_res.x[0] = PSCI_RETURN_INVALID_PARAMS; |
| 179 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Get REC index from MPIDR */ |
| 183 | target_rec_idx = mpidr_to_rec_idx(target_affinity); |
| 184 | |
| 185 | /* |
| 186 | * Check that the target_affinity is a valid value. |
| 187 | * Note that the RMM enforces that the REC are created with |
| 188 | * consecutively increasing indexes starting from zero. |
| 189 | */ |
| 190 | if (target_rec_idx >= rd_map_read_rec_count(rec->realm_info.g_rd)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 191 | res->smc_res.x[0] = PSCI_RETURN_INVALID_PARAMS; |
| 192 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Check if the vCPU targets itself */ |
| 196 | if (target_rec_idx == rec->rec_idx) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 197 | res->smc_res.x[0] = PSCI_AFFINITY_INFO_ON; |
| 198 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 199 | } |
| 200 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 201 | /* Record that a PSCI request is outstanding */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 202 | rec->psci_info.pending = true; |
| 203 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 204 | /* |
| 205 | * Notify the Host, passing the FID and MPIDR arguments. |
| 206 | * Leave REC registers unchanged; these will be read and updated |
| 207 | * by psci_complete_request. |
| 208 | */ |
| 209 | forward_args_to_host(2U, rec, rec_exit); |
| 210 | |
| 211 | res->action = EXIT_TO_HOST; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Turning a system off or requesting a reboot of a realm is enforced by the |
| 216 | * RMM by preventing execution of a REC after the function has run. Reboot |
| 217 | * functionality must be provided by the host hypervisor by creating a new |
| 218 | * Realm with associated attestation, measurement etc. |
| 219 | */ |
| 220 | static void system_off_reboot(struct rec *rec) |
| 221 | { |
| 222 | struct rd *rd; |
| 223 | struct granule *g_rd = rec->realm_info.g_rd; |
| 224 | |
| 225 | /* |
| 226 | * The RECs (and, consequently, the PSCI calls) run without any |
| 227 | * RMM lock held. Therefore, we cannot cause a deadlock when we acquire |
| 228 | * the rd lock here before we set the Realm's new state. |
| 229 | */ |
| 230 | granule_lock(g_rd, GRANULE_STATE_RD); |
| 231 | rd = granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 232 | assert(rd != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 233 | |
| 234 | set_rd_state(rd, REALM_STATE_SYSTEM_OFF); |
| 235 | |
| 236 | buffer_unmap(rd); |
| 237 | granule_unlock(g_rd); |
| 238 | |
| 239 | /* TODO: Invalidate all stage 2 entris to ensure REC exits */ |
| 240 | } |
| 241 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 242 | static void psci_system_off_reset(struct rec *rec, |
| 243 | struct rmi_rec_exit *rec_exit, |
| 244 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 245 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 246 | system_off_reboot(rec); |
| 247 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 248 | /* Notify the Host, passing the FID only */ |
| 249 | forward_args_to_host(1U, rec, rec_exit); |
| 250 | |
| 251 | res->action = EXIT_TO_HOST; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 252 | } |
| 253 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 254 | static void psci_features(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 255 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 256 | unsigned int psci_func_id = (unsigned int)rec->regs[1]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 257 | |
| 258 | switch (psci_func_id) { |
| 259 | case SMC32_PSCI_CPU_SUSPEND: |
| 260 | case SMC64_PSCI_CPU_SUSPEND: |
| 261 | case SMC32_PSCI_CPU_OFF: |
| 262 | case SMC32_PSCI_CPU_ON: |
| 263 | case SMC64_PSCI_CPU_ON: |
| 264 | case SMC32_PSCI_AFFINITY_INFO: |
| 265 | case SMC64_PSCI_AFFINITY_INFO: |
| 266 | case SMC32_PSCI_SYSTEM_OFF: |
| 267 | case SMC32_PSCI_SYSTEM_RESET: |
| 268 | case SMC32_PSCI_FEATURES: |
| 269 | case SMCCC_VERSION: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 270 | res->smc_res.x[0] = PSCI_RETURN_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 271 | break; |
| 272 | default: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 273 | res->smc_res.x[0] = PSCI_RETURN_NOT_SUPPORTED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 274 | } |
| 275 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 276 | res->action = UPDATE_REC_RETURN_TO_REALM; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 277 | } |
| 278 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 279 | void handle_psci(struct rec *rec, |
| 280 | struct rmi_rec_exit *rec_exit, |
| 281 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 282 | { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 283 | unsigned int function_id = (unsigned int)rec->regs[0]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 284 | |
| 285 | switch (function_id) { |
| 286 | case SMC32_PSCI_VERSION: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 287 | psci_version(res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 288 | break; |
| 289 | case SMC32_PSCI_CPU_SUSPEND: |
| 290 | case SMC64_PSCI_CPU_SUSPEND: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 291 | psci_cpu_suspend(rec, rec_exit, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 292 | break; |
| 293 | case SMC32_PSCI_CPU_OFF: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 294 | psci_cpu_off(rec, rec_exit, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 295 | break; |
| 296 | case SMC32_PSCI_CPU_ON: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 297 | case SMC64_PSCI_CPU_ON: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 298 | psci_cpu_on(rec, rec_exit, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 299 | break; |
| 300 | case SMC32_PSCI_AFFINITY_INFO: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 301 | case SMC64_PSCI_AFFINITY_INFO: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 302 | psci_affinity_info(rec, rec_exit, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 303 | break; |
| 304 | case SMC32_PSCI_SYSTEM_OFF: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 305 | case SMC32_PSCI_SYSTEM_RESET: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 306 | psci_system_off_reset(rec, rec_exit, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 307 | break; |
| 308 | case SMC32_PSCI_FEATURES: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 309 | psci_features(rec, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 310 | break; |
| 311 | default: |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 312 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 313 | res->smc_res.x[0] = PSCI_RETURN_NOT_SUPPORTED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | |
AlexeiFedorov | e5dcae2 | 2023-08-29 12:58:18 +0100 | [diff] [blame] | 317 | if (((unsigned int)res->action & FLAG_EXIT_TO_HOST) != 0U) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 318 | rec_exit->exit_reason = RMI_EXIT_PSCI; |
| 319 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
| 323 | * In the following two functions, it is only safe to access the runnable field |
| 324 | * on the target_rec once the target_rec is no longer running on another PE and |
| 325 | * all writes performed by the other PE as part of smc_rec_enter is also |
| 326 | * guaranteed to be observed here, which we know when we read a zero refcount |
| 327 | * on the target rec using acquire semantics paired with the release semantics |
| 328 | * on the reference count in smc_rec_enter. If we observe a non-zero refcount |
| 329 | * it simply means that the target_rec is running and we can return the |
| 330 | * corresponding value. |
| 331 | */ |
| 332 | static unsigned long complete_psci_cpu_on(struct rec *target_rec, |
| 333 | unsigned long entry_point_address, |
| 334 | unsigned long caller_sctlr_el1) |
| 335 | { |
| 336 | if ((granule_refcount_read_acquire(target_rec->g_rec) != 0UL) || |
| 337 | target_rec->runnable) { |
| 338 | return PSCI_RETURN_ALREADY_ON; |
| 339 | } |
| 340 | |
| 341 | psci_reset_rec(target_rec, caller_sctlr_el1); |
| 342 | target_rec->pc = entry_point_address; |
| 343 | target_rec->runnable = true; |
| 344 | return PSCI_RETURN_SUCCESS; |
| 345 | } |
| 346 | |
| 347 | static unsigned long complete_psci_affinity_info(struct rec *target_rec) |
| 348 | { |
| 349 | if ((granule_refcount_read_acquire(target_rec->g_rec) != 0UL) || |
| 350 | target_rec->runnable) { |
| 351 | return PSCI_AFFINITY_INFO_ON; |
| 352 | } |
| 353 | |
| 354 | return PSCI_AFFINITY_INFO_OFF; |
| 355 | } |
| 356 | |
| 357 | unsigned long psci_complete_request(struct rec *calling_rec, |
| 358 | struct rec *target_rec) |
| 359 | { |
| 360 | unsigned long ret = PSCI_RETURN_NOT_SUPPORTED; |
| 361 | unsigned long mpidr = calling_rec->regs[1]; |
| 362 | |
| 363 | if (!calling_rec->psci_info.pending) { |
| 364 | return RMI_ERROR_INPUT; |
| 365 | } |
| 366 | |
| 367 | if (calling_rec->realm_info.g_rd != target_rec->realm_info.g_rd) { |
| 368 | return RMI_ERROR_INPUT; |
| 369 | } |
| 370 | |
| 371 | if (mpidr_to_rec_idx(mpidr) != target_rec->rec_idx) { |
| 372 | return RMI_ERROR_INPUT; |
| 373 | } |
| 374 | |
| 375 | switch (calling_rec->regs[0]) { |
| 376 | case SMC32_PSCI_CPU_ON: |
| 377 | case SMC64_PSCI_CPU_ON: |
| 378 | ret = complete_psci_cpu_on(target_rec, |
| 379 | calling_rec->regs[2], |
| 380 | calling_rec->sysregs.sctlr_el1); |
| 381 | break; |
| 382 | case SMC32_PSCI_AFFINITY_INFO: |
| 383 | case SMC64_PSCI_AFFINITY_INFO: |
| 384 | ret = complete_psci_affinity_info(target_rec); |
| 385 | break; |
| 386 | default: |
| 387 | assert(false); |
| 388 | } |
| 389 | |
| 390 | calling_rec->regs[0] = ret; |
| 391 | calling_rec->regs[1] = 0; |
| 392 | calling_rec->regs[2] = 0; |
| 393 | calling_rec->regs[3] = 0; |
| 394 | calling_rec->psci_info.pending = false; |
| 395 | |
| 396 | return RMI_SUCCESS; |
| 397 | } |