Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 1 | /* |
Daniel Boulby | 9460a23 | 2021-12-09 11:20:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2022, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 9 | #include <inttypes.h> |
| 10 | #include <stdint.h> |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 11 | #include <string.h> |
| 12 | |
| 13 | #include <arch_helpers.h> |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 14 | #include <arch/aarch64/arch_features.h> |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 15 | #include <bl31/bl31.h> |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 16 | #include <bl31/interrupt_mgmt.h> |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 17 | #include <common/debug.h> |
| 18 | #include <common/runtime_svc.h> |
| 19 | #include <lib/el3_runtime/context_mgmt.h> |
| 20 | #include <lib/smccc.h> |
| 21 | #include <lib/spinlock.h> |
| 22 | #include <lib/utils.h> |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 23 | #include <plat/common/common_def.h> |
| 24 | #include <plat/common/platform.h> |
| 25 | #include <platform_def.h> |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 26 | #include <services/ffa_svc.h> |
Marc Bonnici | 1c33cc3 | 2021-11-29 17:57:03 +0000 | [diff] [blame^] | 27 | #include <services/spmc_svc.h> |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 28 | #include <services/spmd_svc.h> |
| 29 | #include <smccc_helpers.h> |
| 30 | #include "spmd_private.h" |
| 31 | |
| 32 | /******************************************************************************* |
| 33 | * SPM Core context information. |
| 34 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 35 | static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT]; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 36 | |
| 37 | /******************************************************************************* |
Marc Bonnici | 1c33cc3 | 2021-11-29 17:57:03 +0000 | [diff] [blame^] | 38 | * SPM Core attribute information is read from its manifest if the SPMC is not |
| 39 | * at EL3. Else, it is populated from the SPMC directly. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 40 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 41 | static spmc_manifest_attribute_t spmc_attrs; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 42 | |
| 43 | /******************************************************************************* |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 44 | * SPM Core entry point information. Discovered on the primary core and reused |
| 45 | * on secondary cores. |
| 46 | ******************************************************************************/ |
| 47 | static entry_point_info_t *spmc_ep_info; |
| 48 | |
| 49 | /******************************************************************************* |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 50 | * SPM Core context on CPU based on mpidr. |
| 51 | ******************************************************************************/ |
| 52 | spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr) |
| 53 | { |
Max Shvetsov | f80c64d | 2020-08-25 11:50:18 +0100 | [diff] [blame] | 54 | int core_idx = plat_core_pos_by_mpidr(mpidr); |
| 55 | |
| 56 | if (core_idx < 0) { |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 57 | ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx); |
Max Shvetsov | f80c64d | 2020-08-25 11:50:18 +0100 | [diff] [blame] | 58 | panic(); |
| 59 | } |
| 60 | |
| 61 | return &spm_core_context[core_idx]; |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 65 | * SPM Core context on current CPU get helper. |
| 66 | ******************************************************************************/ |
| 67 | spmd_spm_core_context_t *spmd_get_context(void) |
| 68 | { |
Olivier Deprez | 73ef0dc | 2020-06-19 15:33:41 +0200 | [diff] [blame] | 69 | return spmd_get_context_by_mpidr(read_mpidr()); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /******************************************************************************* |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 73 | * SPM Core ID getter. |
| 74 | ******************************************************************************/ |
| 75 | uint16_t spmd_spmc_id_get(void) |
| 76 | { |
| 77 | return spmc_attrs.spmc_id; |
| 78 | } |
| 79 | |
| 80 | /******************************************************************************* |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 81 | * Static function declaration. |
| 82 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 83 | static int32_t spmd_init(void); |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 84 | static int spmd_spmc_init(void *pm_addr); |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 85 | static uint64_t spmd_ffa_error_return(void *handle, |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 86 | int error_code); |
| 87 | static uint64_t spmd_smc_forward(uint32_t smc_fid, |
| 88 | bool secure_origin, |
| 89 | uint64_t x1, |
| 90 | uint64_t x2, |
| 91 | uint64_t x3, |
| 92 | uint64_t x4, |
| 93 | void *handle); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 94 | |
Daniel Boulby | 9460a23 | 2021-12-09 11:20:13 +0000 | [diff] [blame] | 95 | /****************************************************************************** |
| 96 | * Builds an SPMD to SPMC direct message request. |
| 97 | *****************************************************************************/ |
| 98 | void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func, |
| 99 | unsigned long long message) |
| 100 | { |
| 101 | write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); |
| 102 | write_ctx_reg(gpregs, CTX_GPREG_X1, |
| 103 | (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | |
| 104 | spmd_spmc_id_get()); |
| 105 | write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func); |
| 106 | write_ctx_reg(gpregs, CTX_GPREG_X3, message); |
| 107 | } |
| 108 | |
| 109 | |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 110 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 111 | * This function takes an SPMC context pointer and performs a synchronous |
| 112 | * SPMC entry. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 113 | ******************************************************************************/ |
| 114 | uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx) |
| 115 | { |
| 116 | uint64_t rc; |
| 117 | |
| 118 | assert(spmc_ctx != NULL); |
| 119 | |
| 120 | cm_set_context(&(spmc_ctx->cpu_ctx), SECURE); |
| 121 | |
| 122 | /* Restore the context assigned above */ |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 123 | #if SPMD_SPM_AT_SEL2 |
Max Shvetsov | bdf502d | 2020-02-25 13:56:19 +0000 | [diff] [blame] | 124 | cm_el2_sysregs_context_restore(SECURE); |
Olivier Deprez | 9a2e5be | 2021-05-21 18:00:04 +0200 | [diff] [blame] | 125 | #else |
| 126 | cm_el1_sysregs_context_restore(SECURE); |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 127 | #endif |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 128 | cm_set_next_eret_context(SECURE); |
| 129 | |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 130 | /* Enter SPMC */ |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 131 | rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx); |
| 132 | |
| 133 | /* Save secure state */ |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 134 | #if SPMD_SPM_AT_SEL2 |
Max Shvetsov | bdf502d | 2020-02-25 13:56:19 +0000 | [diff] [blame] | 135 | cm_el2_sysregs_context_save(SECURE); |
Olivier Deprez | 9a2e5be | 2021-05-21 18:00:04 +0200 | [diff] [blame] | 136 | #else |
| 137 | cm_el1_sysregs_context_save(SECURE); |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 138 | #endif |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 139 | |
| 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 144 | * This function returns to the place where spmd_spm_core_sync_entry() was |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 145 | * called originally. |
| 146 | ******************************************************************************/ |
| 147 | __dead2 void spmd_spm_core_sync_exit(uint64_t rc) |
| 148 | { |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 149 | spmd_spm_core_context_t *ctx = spmd_get_context(); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 150 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 151 | /* Get current CPU context from SPMC context */ |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 152 | assert(cm_get_context(SECURE) == &(ctx->cpu_ctx)); |
| 153 | |
| 154 | /* |
| 155 | * The SPMD must have initiated the original request through a |
| 156 | * synchronous entry into SPMC. Jump back to the original C runtime |
| 157 | * context with the value of rc in x0; |
| 158 | */ |
| 159 | spmd_spm_core_exit(ctx->c_rt_ctx, rc); |
| 160 | |
| 161 | panic(); |
| 162 | } |
| 163 | |
| 164 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 165 | * Jump to the SPM Core for the first time. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 166 | ******************************************************************************/ |
| 167 | static int32_t spmd_init(void) |
| 168 | { |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 169 | spmd_spm_core_context_t *ctx = spmd_get_context(); |
| 170 | uint64_t rc; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 171 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 172 | VERBOSE("SPM Core init start.\n"); |
Olivier Deprez | 7c01633 | 2019-10-28 09:03:13 +0000 | [diff] [blame] | 173 | |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 174 | /* Primary boot core enters the SPMC for initialization. */ |
| 175 | ctx->state = SPMC_STATE_ON_PENDING; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 176 | |
| 177 | rc = spmd_spm_core_sync_entry(ctx); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 178 | if (rc != 0ULL) { |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 179 | ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 180 | return 0; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Olivier Deprez | 7c01633 | 2019-10-28 09:03:13 +0000 | [diff] [blame] | 183 | ctx->state = SPMC_STATE_ON; |
| 184 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 185 | VERBOSE("SPM Core init end.\n"); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 186 | |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | /******************************************************************************* |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 191 | * spmd_secure_interrupt_handler |
| 192 | * Enter the SPMC for further handling of the secure interrupt by the SPMC |
| 193 | * itself or a Secure Partition. |
| 194 | ******************************************************************************/ |
| 195 | static uint64_t spmd_secure_interrupt_handler(uint32_t id, |
| 196 | uint32_t flags, |
| 197 | void *handle, |
| 198 | void *cookie) |
| 199 | { |
| 200 | spmd_spm_core_context_t *ctx = spmd_get_context(); |
| 201 | gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx); |
| 202 | unsigned int linear_id = plat_my_core_pos(); |
| 203 | int64_t rc; |
| 204 | |
| 205 | /* Sanity check the security state when the exception was generated */ |
| 206 | assert(get_interrupt_src_ss(flags) == NON_SECURE); |
| 207 | |
| 208 | /* Sanity check the pointer to this cpu's context */ |
| 209 | assert(handle == cm_get_context(NON_SECURE)); |
| 210 | |
| 211 | /* Save the non-secure context before entering SPMC */ |
| 212 | cm_el1_sysregs_context_save(NON_SECURE); |
| 213 | #if SPMD_SPM_AT_SEL2 |
| 214 | cm_el2_sysregs_context_save(NON_SECURE); |
| 215 | #endif |
| 216 | |
| 217 | /* Convey the event to the SPMC through the FFA_INTERRUPT interface. */ |
| 218 | write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT); |
| 219 | write_ctx_reg(gpregs, CTX_GPREG_X1, 0); |
| 220 | write_ctx_reg(gpregs, CTX_GPREG_X2, 0); |
| 221 | write_ctx_reg(gpregs, CTX_GPREG_X3, 0); |
| 222 | write_ctx_reg(gpregs, CTX_GPREG_X4, 0); |
| 223 | write_ctx_reg(gpregs, CTX_GPREG_X5, 0); |
| 224 | write_ctx_reg(gpregs, CTX_GPREG_X6, 0); |
| 225 | write_ctx_reg(gpregs, CTX_GPREG_X7, 0); |
| 226 | |
| 227 | /* Mark current core as handling a secure interrupt. */ |
| 228 | ctx->secure_interrupt_ongoing = true; |
| 229 | |
| 230 | rc = spmd_spm_core_sync_entry(ctx); |
| 231 | if (rc != 0ULL) { |
Olivier Deprez | ba100f2 | 2021-11-09 12:37:20 +0100 | [diff] [blame] | 232 | ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id); |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | ctx->secure_interrupt_ongoing = false; |
| 236 | |
| 237 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 238 | #if SPMD_SPM_AT_SEL2 |
| 239 | cm_el2_sysregs_context_restore(NON_SECURE); |
| 240 | #endif |
| 241 | cm_set_next_eret_context(NON_SECURE); |
| 242 | |
| 243 | SMC_RET0(&ctx->cpu_ctx); |
| 244 | } |
| 245 | |
| 246 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 247 | * Loads SPMC manifest and inits SPMC. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 248 | ******************************************************************************/ |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 249 | static int spmd_spmc_init(void *pm_addr) |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 250 | { |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 251 | cpu_context_t *cpu_ctx; |
| 252 | unsigned int core_id; |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 253 | uint32_t ep_attr, flags; |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 254 | int rc; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 255 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 256 | /* Load the SPM Core manifest */ |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 257 | rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 258 | if (rc != 0) { |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 259 | WARN("No or invalid SPM Core manifest image provided by BL2\n"); |
| 260 | return rc; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 264 | * Ensure that the SPM Core version is compatible with the SPM |
| 265 | * Dispatcher version. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 266 | */ |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 267 | if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) || |
| 268 | (spmc_attrs.minor_version > FFA_VERSION_MINOR)) { |
| 269 | WARN("Unsupported FFA version (%u.%u)\n", |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 270 | spmc_attrs.major_version, spmc_attrs.minor_version); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 271 | return -EINVAL; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 272 | } |
| 273 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 274 | VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version, |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 275 | spmc_attrs.minor_version); |
| 276 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 277 | VERBOSE("SPM Core run time EL%x.\n", |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 278 | SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 279 | |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 280 | /* Validate the SPMC ID, Ensure high bit is set */ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 281 | if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) & |
| 282 | SPMC_SECURE_ID_MASK) == 0U) { |
| 283 | WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id); |
| 284 | return -EINVAL; |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 287 | /* Validate the SPM Core execution state */ |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 288 | if ((spmc_attrs.exec_state != MODE_RW_64) && |
| 289 | (spmc_attrs.exec_state != MODE_RW_32)) { |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 290 | WARN("Unsupported %s%x.\n", "SPM Core execution state 0x", |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 291 | spmc_attrs.exec_state); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 292 | return -EINVAL; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 295 | VERBOSE("%s%x.\n", "SPM Core execution state 0x", |
| 296 | spmc_attrs.exec_state); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 297 | |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 298 | #if SPMD_SPM_AT_SEL2 |
| 299 | /* Ensure manifest has not requested AArch32 state in S-EL2 */ |
| 300 | if (spmc_attrs.exec_state == MODE_RW_32) { |
| 301 | WARN("AArch32 state at S-EL2 is not supported.\n"); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 302 | return -EINVAL; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Check if S-EL2 is supported on this system if S-EL2 |
| 307 | * is required for SPM |
| 308 | */ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 309 | if (!is_armv8_4_sel2_present()) { |
| 310 | WARN("SPM Core run time S-EL2 is not supported.\n"); |
| 311 | return -EINVAL; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 312 | } |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 313 | #endif /* SPMD_SPM_AT_SEL2 */ |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 314 | |
| 315 | /* Initialise an entrypoint to set up the CPU context */ |
| 316 | ep_attr = SECURE | EP_ST_ENABLE; |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 317 | if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) { |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 318 | ep_attr |= EP_EE_BIG; |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 321 | SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 322 | |
| 323 | /* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 324 | * Populate SPSR for SPM Core based upon validated parameters from the |
| 325 | * manifest. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 326 | */ |
| 327 | if (spmc_attrs.exec_state == MODE_RW_32) { |
| 328 | spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, |
| 329 | SPSR_E_LITTLE, |
| 330 | DAIF_FIQ_BIT | |
| 331 | DAIF_IRQ_BIT | |
| 332 | DAIF_ABT_BIT); |
| 333 | } else { |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 334 | |
| 335 | #if SPMD_SPM_AT_SEL2 |
| 336 | static const uint32_t runtime_el = MODE_EL2; |
| 337 | #else |
| 338 | static const uint32_t runtime_el = MODE_EL1; |
| 339 | #endif |
| 340 | spmc_ep_info->spsr = SPSR_64(runtime_el, |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 341 | MODE_SP_ELX, |
| 342 | DISABLE_ALL_EXCEPTIONS); |
| 343 | } |
| 344 | |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 345 | /* Set an initial SPMC context state for all cores. */ |
| 346 | for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) { |
| 347 | spm_core_context[core_id].state = SPMC_STATE_OFF; |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 348 | |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 349 | /* Setup an initial cpu context for the SPMC. */ |
| 350 | cpu_ctx = &spm_core_context[core_id].cpu_ctx; |
| 351 | cm_setup_context(cpu_ctx, spmc_ep_info); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 352 | |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 353 | /* |
| 354 | * Pass the core linear ID to the SPMC through x4. |
| 355 | * (TF-A implementation defined behavior helping |
| 356 | * a legacy TOS migration to adopt FF-A). |
| 357 | */ |
| 358 | write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id); |
| 359 | } |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 360 | |
Olivier Deprez | 9afca12 | 2019-10-28 09:15:52 +0000 | [diff] [blame] | 361 | /* Register power management hooks with PSCI */ |
| 362 | psci_register_spd_pm_hook(&spmd_pm); |
| 363 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 364 | /* Register init function for deferred init. */ |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 365 | bl31_register_bl32_init(&spmd_init); |
| 366 | |
Olivier Deprez | 4ab7a4a | 2021-06-21 09:47:13 +0200 | [diff] [blame] | 367 | INFO("SPM Core setup done.\n"); |
| 368 | |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 369 | /* |
| 370 | * Register an interrupt handler routing secure interrupts to SPMD |
| 371 | * while the NWd is running. |
| 372 | */ |
| 373 | flags = 0; |
| 374 | set_interrupt_rm_flag(flags, NON_SECURE); |
| 375 | rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, |
| 376 | spmd_secure_interrupt_handler, |
| 377 | flags); |
| 378 | if (rc != 0) { |
| 379 | panic(); |
| 380 | } |
| 381 | |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 382 | return 0; |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 383 | } |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 384 | |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 385 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 386 | * Initialize context of SPM Core. |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 387 | ******************************************************************************/ |
| 388 | int spmd_setup(void) |
| 389 | { |
| 390 | int rc; |
Marc Bonnici | 1c33cc3 | 2021-11-29 17:57:03 +0000 | [diff] [blame^] | 391 | void *spmc_manifest; |
| 392 | |
| 393 | /* |
| 394 | * If the SPMC is at EL3, then just initialise it directly. The |
| 395 | * shenanigans of when it is at a lower EL are not needed. |
| 396 | */ |
| 397 | if (is_spmc_at_el3()) { |
| 398 | /* Allow the SPMC to populate its attributes directly. */ |
| 399 | spmc_populate_attrs(&spmc_attrs); |
| 400 | |
| 401 | rc = spmc_setup(); |
| 402 | if (rc != 0) { |
| 403 | ERROR("SPMC initialisation failed 0x%x.\n", rc); |
| 404 | } |
| 405 | return rc; |
| 406 | } |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 407 | |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 408 | spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 409 | if (spmc_ep_info == NULL) { |
| 410 | WARN("No SPM Core image provided by BL2 boot loader.\n"); |
| 411 | return -EINVAL; |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* Under no circumstances will this parameter be 0 */ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 415 | assert(spmc_ep_info->pc != 0ULL); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 419 | * be used as a manifest for the SPM Core at the next lower EL/mode. |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 420 | */ |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 421 | spmc_manifest = (void *)spmc_ep_info->args.arg0; |
| 422 | if (spmc_manifest == NULL) { |
| 423 | ERROR("Invalid or absent SPM Core manifest.\n"); |
| 424 | return -EINVAL; |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Load manifest, init SPMC */ |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 428 | rc = spmd_spmc_init(spmc_manifest); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 429 | if (rc != 0) { |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 430 | WARN("Booting device without SPM initialization.\n"); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 433 | return rc; |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /******************************************************************************* |
| 437 | * Forward SMC to the other security state |
| 438 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 439 | static uint64_t spmd_smc_forward(uint32_t smc_fid, |
| 440 | bool secure_origin, |
| 441 | uint64_t x1, |
| 442 | uint64_t x2, |
| 443 | uint64_t x3, |
| 444 | uint64_t x4, |
| 445 | void *handle) |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 446 | { |
Olivier Deprez | ebc3477 | 2020-04-16 16:59:21 +0200 | [diff] [blame] | 447 | unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE; |
| 448 | unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE; |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 449 | |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 450 | /* Save incoming security state */ |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 451 | #if SPMD_SPM_AT_SEL2 |
Olivier Deprez | 9a2e5be | 2021-05-21 18:00:04 +0200 | [diff] [blame] | 452 | if (secure_state_in == NON_SECURE) { |
| 453 | cm_el1_sysregs_context_save(secure_state_in); |
| 454 | } |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 455 | cm_el2_sysregs_context_save(secure_state_in); |
Olivier Deprez | 9a2e5be | 2021-05-21 18:00:04 +0200 | [diff] [blame] | 456 | #else |
| 457 | cm_el1_sysregs_context_save(secure_state_in); |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 458 | #endif |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 459 | |
| 460 | /* Restore outgoing security state */ |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 461 | #if SPMD_SPM_AT_SEL2 |
Olivier Deprez | 9a2e5be | 2021-05-21 18:00:04 +0200 | [diff] [blame] | 462 | if (secure_state_out == NON_SECURE) { |
| 463 | cm_el1_sysregs_context_restore(secure_state_out); |
| 464 | } |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 465 | cm_el2_sysregs_context_restore(secure_state_out); |
Olivier Deprez | 9a2e5be | 2021-05-21 18:00:04 +0200 | [diff] [blame] | 466 | #else |
| 467 | cm_el1_sysregs_context_restore(secure_state_out); |
Max Shvetsov | e7fd80e | 2020-02-25 13:55:00 +0000 | [diff] [blame] | 468 | #endif |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 469 | cm_set_next_eret_context(secure_state_out); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 470 | |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 471 | SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4, |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 472 | SMC_GET_GP(handle, CTX_GPREG_X5), |
| 473 | SMC_GET_GP(handle, CTX_GPREG_X6), |
| 474 | SMC_GET_GP(handle, CTX_GPREG_X7)); |
| 475 | } |
| 476 | |
| 477 | /******************************************************************************* |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 478 | * Return FFA_ERROR with specified error code |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 479 | ******************************************************************************/ |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 480 | static uint64_t spmd_ffa_error_return(void *handle, int error_code) |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 481 | { |
J-Alves | 64ff993 | 2021-03-01 10:26:59 +0000 | [diff] [blame] | 482 | SMC_RET8(handle, (uint32_t) FFA_ERROR, |
| 483 | FFA_TARGET_INFO_MBZ, (uint32_t)error_code, |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 484 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 485 | FFA_PARAM_MBZ, FFA_PARAM_MBZ); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 488 | /******************************************************************************* |
| 489 | * spmd_check_address_in_binary_image |
| 490 | ******************************************************************************/ |
| 491 | bool spmd_check_address_in_binary_image(uint64_t address) |
| 492 | { |
| 493 | assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size)); |
| 494 | |
| 495 | return ((address >= spmc_attrs.load_address) && |
| 496 | (address < (spmc_attrs.load_address + spmc_attrs.binary_size))); |
| 497 | } |
| 498 | |
Olivier Deprez | ebc3477 | 2020-04-16 16:59:21 +0200 | [diff] [blame] | 499 | /****************************************************************************** |
| 500 | * spmd_is_spmc_message |
| 501 | *****************************************************************************/ |
| 502 | static bool spmd_is_spmc_message(unsigned int ep) |
| 503 | { |
| 504 | return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID) |
| 505 | && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id)); |
| 506 | } |
| 507 | |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 508 | /****************************************************************************** |
| 509 | * spmd_handle_spmc_message |
| 510 | *****************************************************************************/ |
Olivier Deprez | c7631a5 | 2020-03-23 09:53:06 +0100 | [diff] [blame] | 511 | static int spmd_handle_spmc_message(unsigned long long msg, |
| 512 | unsigned long long parm1, unsigned long long parm2, |
| 513 | unsigned long long parm3, unsigned long long parm4) |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 514 | { |
| 515 | VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__, |
| 516 | msg, parm1, parm2, parm3, parm4); |
| 517 | |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 518 | return -EINVAL; |
| 519 | } |
| 520 | |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 521 | /******************************************************************************* |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 522 | * This function handles all SMCs in the range reserved for FFA. Each call is |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 523 | * either forwarded to the other security state or handled by the SPM dispatcher |
| 524 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 525 | uint64_t spmd_smc_handler(uint32_t smc_fid, |
| 526 | uint64_t x1, |
| 527 | uint64_t x2, |
| 528 | uint64_t x3, |
| 529 | uint64_t x4, |
| 530 | void *cookie, |
| 531 | void *handle, |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 532 | uint64_t flags) |
| 533 | { |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 534 | unsigned int linear_id = plat_my_core_pos(); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 535 | spmd_spm_core_context_t *ctx = spmd_get_context(); |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 536 | bool secure_origin; |
| 537 | int32_t ret; |
J-Alves | 4c95c70 | 2020-05-26 14:03:05 +0100 | [diff] [blame] | 538 | uint32_t input_version; |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 539 | |
| 540 | /* Determine which security state this SMC originated from */ |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 541 | secure_origin = is_caller_secure(flags); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 542 | |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 543 | VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 |
| 544 | " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n", |
| 545 | linear_id, smc_fid, x1, x2, x3, x4, |
| 546 | SMC_GET_GP(handle, CTX_GPREG_X5), |
| 547 | SMC_GET_GP(handle, CTX_GPREG_X6), |
| 548 | SMC_GET_GP(handle, CTX_GPREG_X7)); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 549 | |
| 550 | switch (smc_fid) { |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 551 | case FFA_ERROR: |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 552 | /* |
| 553 | * Check if this is the first invocation of this interface on |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 554 | * this CPU. If so, then indicate that the SPM Core initialised |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 555 | * unsuccessfully. |
| 556 | */ |
Olivier Deprez | 7c01633 | 2019-10-28 09:03:13 +0000 | [diff] [blame] | 557 | if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 558 | spmd_spm_core_sync_exit(x2); |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 559 | } |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 560 | |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 561 | return spmd_smc_forward(smc_fid, secure_origin, |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 562 | x1, x2, x3, x4, handle); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 563 | break; /* not reached */ |
| 564 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 565 | case FFA_VERSION: |
J-Alves | 4c95c70 | 2020-05-26 14:03:05 +0100 | [diff] [blame] | 566 | input_version = (uint32_t)(0xFFFFFFFF & x1); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 567 | /* |
J-Alves | 4c95c70 | 2020-05-26 14:03:05 +0100 | [diff] [blame] | 568 | * If caller is secure and SPMC was initialized, |
| 569 | * return FFA_VERSION of SPMD. |
| 570 | * If caller is non secure and SPMC was initialized, |
| 571 | * return SPMC's version. |
| 572 | * Sanity check to "input_version". |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 573 | */ |
J-Alves | 4c95c70 | 2020-05-26 14:03:05 +0100 | [diff] [blame] | 574 | if ((input_version & FFA_VERSION_BIT31_MASK) || |
| 575 | (ctx->state == SPMC_STATE_RESET)) { |
| 576 | ret = FFA_ERROR_NOT_SUPPORTED; |
| 577 | } else if (!secure_origin) { |
Daniel Boulby | 9460a23 | 2021-12-09 11:20:13 +0000 | [diff] [blame] | 578 | gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx); |
| 579 | uint64_t rc; |
| 580 | |
| 581 | if (spmc_attrs.major_version == 1 && |
| 582 | spmc_attrs.minor_version == 0) { |
| 583 | ret = MAKE_FFA_VERSION(spmc_attrs.major_version, |
| 584 | spmc_attrs.minor_version); |
| 585 | SMC_RET8(handle, (uint32_t)ret, |
| 586 | FFA_TARGET_INFO_MBZ, |
| 587 | FFA_TARGET_INFO_MBZ, |
| 588 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 589 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 590 | FFA_PARAM_MBZ); |
| 591 | break; |
| 592 | } |
| 593 | /* Save non-secure system registers context */ |
| 594 | cm_el1_sysregs_context_save(NON_SECURE); |
| 595 | #if SPMD_SPM_AT_SEL2 |
| 596 | cm_el2_sysregs_context_save(NON_SECURE); |
| 597 | #endif |
| 598 | |
| 599 | /* |
| 600 | * The incoming request has FFA_VERSION as X0 smc_fid |
| 601 | * and requested version in x1. Prepare a direct request |
| 602 | * from SPMD to SPMC with FFA_VERSION framework function |
| 603 | * identifier in X2 and requested version in X3. |
| 604 | */ |
| 605 | spmd_build_spmc_message(gpregs, |
| 606 | SPMD_FWK_MSG_FFA_VERSION_REQ, |
| 607 | input_version); |
| 608 | |
| 609 | rc = spmd_spm_core_sync_entry(ctx); |
| 610 | |
| 611 | if ((rc != 0ULL) || |
| 612 | (SMC_GET_GP(gpregs, CTX_GPREG_X0) != |
| 613 | FFA_MSG_SEND_DIRECT_RESP_SMC32) || |
| 614 | (SMC_GET_GP(gpregs, CTX_GPREG_X2) != |
| 615 | (SPMD_FWK_MSG_BIT | |
| 616 | SPMD_FWK_MSG_FFA_VERSION_RESP))) { |
| 617 | ERROR("Failed to forward FFA_VERSION\n"); |
| 618 | ret = FFA_ERROR_NOT_SUPPORTED; |
| 619 | } else { |
| 620 | ret = SMC_GET_GP(gpregs, CTX_GPREG_X3); |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Return here after SPMC has handled FFA_VERSION. |
| 625 | * The returned SPMC version is held in X3. |
| 626 | * Forward this version in X0 to the non-secure caller. |
| 627 | */ |
| 628 | return spmd_smc_forward(ret, true, FFA_PARAM_MBZ, |
| 629 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 630 | FFA_PARAM_MBZ, gpregs); |
J-Alves | 4c95c70 | 2020-05-26 14:03:05 +0100 | [diff] [blame] | 631 | } else { |
J-Alves | 64ff993 | 2021-03-01 10:26:59 +0000 | [diff] [blame] | 632 | ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, |
| 633 | FFA_VERSION_MINOR); |
J-Alves | 4c95c70 | 2020-05-26 14:03:05 +0100 | [diff] [blame] | 634 | } |
| 635 | |
J-Alves | 64ff993 | 2021-03-01 10:26:59 +0000 | [diff] [blame] | 636 | SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ, |
| 637 | FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 638 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 639 | break; /* not reached */ |
| 640 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 641 | case FFA_FEATURES: |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 642 | /* |
| 643 | * This is an optional interface. Do the minimal checks and |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 644 | * forward to SPM Core which will handle it if implemented. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 645 | */ |
| 646 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 647 | /* Forward SMC from Normal world to the SPM Core */ |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 648 | if (!secure_origin) { |
| 649 | return spmd_smc_forward(smc_fid, secure_origin, |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 650 | x1, x2, x3, x4, handle); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 651 | } |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 652 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 653 | /* |
| 654 | * Return success if call was from secure world i.e. all |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 655 | * FFA functions are supported. This is essentially a |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 656 | * nop. |
| 657 | */ |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 658 | SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4, |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 659 | SMC_GET_GP(handle, CTX_GPREG_X5), |
| 660 | SMC_GET_GP(handle, CTX_GPREG_X6), |
| 661 | SMC_GET_GP(handle, CTX_GPREG_X7)); |
| 662 | |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 663 | break; /* not reached */ |
| 664 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 665 | case FFA_ID_GET: |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 666 | /* |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 667 | * Returns the ID of the calling FFA component. |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 668 | */ |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 669 | if (!secure_origin) { |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 670 | SMC_RET8(handle, FFA_SUCCESS_SMC32, |
| 671 | FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID, |
| 672 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 673 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 674 | FFA_PARAM_MBZ); |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 675 | } |
| 676 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 677 | SMC_RET8(handle, FFA_SUCCESS_SMC32, |
| 678 | FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, |
| 679 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 680 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 681 | FFA_PARAM_MBZ); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 682 | |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 683 | break; /* not reached */ |
| 684 | |
Olivier Deprez | eae4596 | 2021-01-19 15:06:47 +0100 | [diff] [blame] | 685 | case FFA_SECONDARY_EP_REGISTER_SMC64: |
| 686 | if (secure_origin) { |
| 687 | ret = spmd_pm_secondary_ep_register(x1); |
| 688 | |
| 689 | if (ret < 0) { |
| 690 | SMC_RET8(handle, FFA_ERROR_SMC64, |
| 691 | FFA_TARGET_INFO_MBZ, ret, |
| 692 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 693 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 694 | FFA_PARAM_MBZ); |
| 695 | } else { |
| 696 | SMC_RET8(handle, FFA_SUCCESS_SMC64, |
| 697 | FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, |
| 698 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 699 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 700 | FFA_PARAM_MBZ); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); |
| 705 | break; /* Not reached */ |
| 706 | |
Daniel Boulby | 27f35df | 2021-02-03 12:13:19 +0000 | [diff] [blame] | 707 | case FFA_SPM_ID_GET: |
| 708 | if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) { |
| 709 | return spmd_ffa_error_return(handle, |
| 710 | FFA_ERROR_NOT_SUPPORTED); |
| 711 | } |
| 712 | /* |
| 713 | * Returns the ID of the SPMC or SPMD depending on the FF-A |
| 714 | * instance where this function is invoked |
| 715 | */ |
| 716 | if (!secure_origin) { |
| 717 | SMC_RET8(handle, FFA_SUCCESS_SMC32, |
| 718 | FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, |
| 719 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 720 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 721 | FFA_PARAM_MBZ); |
| 722 | } |
| 723 | SMC_RET8(handle, FFA_SUCCESS_SMC32, |
| 724 | FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID, |
| 725 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 726 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 727 | FFA_PARAM_MBZ); |
| 728 | |
| 729 | break; /* not reached */ |
| 730 | |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 731 | case FFA_MSG_SEND_DIRECT_REQ_SMC32: |
| 732 | if (secure_origin && spmd_is_spmc_message(x1)) { |
| 733 | ret = spmd_handle_spmc_message(x3, x4, |
| 734 | SMC_GET_GP(handle, CTX_GPREG_X5), |
| 735 | SMC_GET_GP(handle, CTX_GPREG_X6), |
| 736 | SMC_GET_GP(handle, CTX_GPREG_X7)); |
| 737 | |
| 738 | SMC_RET8(handle, FFA_SUCCESS_SMC32, |
| 739 | FFA_TARGET_INFO_MBZ, ret, |
| 740 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 741 | FFA_PARAM_MBZ, FFA_PARAM_MBZ, |
| 742 | FFA_PARAM_MBZ); |
| 743 | } else { |
| 744 | /* Forward direct message to the other world */ |
| 745 | return spmd_smc_forward(smc_fid, secure_origin, |
| 746 | x1, x2, x3, x4, handle); |
| 747 | } |
| 748 | break; /* Not reached */ |
| 749 | |
| 750 | case FFA_MSG_SEND_DIRECT_RESP_SMC32: |
| 751 | if (secure_origin && spmd_is_spmc_message(x1)) { |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 752 | spmd_spm_core_sync_exit(0ULL); |
Olivier Deprez | 33e4412 | 2020-04-16 17:54:27 +0200 | [diff] [blame] | 753 | } else { |
| 754 | /* Forward direct message to the other world */ |
| 755 | return spmd_smc_forward(smc_fid, secure_origin, |
| 756 | x1, x2, x3, x4, handle); |
| 757 | } |
| 758 | break; /* Not reached */ |
| 759 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 760 | case FFA_RX_RELEASE: |
| 761 | case FFA_RXTX_MAP_SMC32: |
| 762 | case FFA_RXTX_MAP_SMC64: |
| 763 | case FFA_RXTX_UNMAP: |
Ruari Phipps | 93dff70 | 2020-07-28 10:33:35 +0100 | [diff] [blame] | 764 | case FFA_PARTITION_INFO_GET: |
J-Alves | 2621cfd | 2021-03-11 17:46:47 +0000 | [diff] [blame] | 765 | #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED |
| 766 | case FFA_NOTIFICATION_BITMAP_CREATE: |
| 767 | case FFA_NOTIFICATION_BITMAP_DESTROY: |
| 768 | case FFA_NOTIFICATION_BIND: |
| 769 | case FFA_NOTIFICATION_UNBIND: |
| 770 | case FFA_NOTIFICATION_SET: |
| 771 | case FFA_NOTIFICATION_GET: |
| 772 | case FFA_NOTIFICATION_INFO_GET: |
| 773 | case FFA_NOTIFICATION_INFO_GET_SMC64: |
Federico Recanati | eecb4b0 | 2022-02-03 17:22:37 +0100 | [diff] [blame] | 774 | case FFA_MSG_SEND2: |
J-Alves | 2621cfd | 2021-03-11 17:46:47 +0000 | [diff] [blame] | 775 | #endif |
Federico Recanati | eecb4b0 | 2022-02-03 17:22:37 +0100 | [diff] [blame] | 776 | case FFA_MSG_RUN: |
Ruari Phipps | 93dff70 | 2020-07-28 10:33:35 +0100 | [diff] [blame] | 777 | /* |
Federico Recanati | eecb4b0 | 2022-02-03 17:22:37 +0100 | [diff] [blame] | 778 | * Above calls should be invoked only by the Normal world and |
| 779 | * must not be forwarded from Secure world to Normal world. |
Ruari Phipps | 93dff70 | 2020-07-28 10:33:35 +0100 | [diff] [blame] | 780 | */ |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 781 | if (secure_origin) { |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 782 | return spmd_ffa_error_return(handle, |
Ruari Phipps | 93dff70 | 2020-07-28 10:33:35 +0100 | [diff] [blame] | 783 | FFA_ERROR_NOT_SUPPORTED); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* Fall through to forward the call to the other world */ |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 787 | case FFA_MSG_SEND: |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 788 | case FFA_MSG_SEND_DIRECT_REQ_SMC64: |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 789 | case FFA_MSG_SEND_DIRECT_RESP_SMC64: |
| 790 | case FFA_MEM_DONATE_SMC32: |
| 791 | case FFA_MEM_DONATE_SMC64: |
| 792 | case FFA_MEM_LEND_SMC32: |
| 793 | case FFA_MEM_LEND_SMC64: |
| 794 | case FFA_MEM_SHARE_SMC32: |
| 795 | case FFA_MEM_SHARE_SMC64: |
| 796 | case FFA_MEM_RETRIEVE_REQ_SMC32: |
| 797 | case FFA_MEM_RETRIEVE_REQ_SMC64: |
| 798 | case FFA_MEM_RETRIEVE_RESP: |
| 799 | case FFA_MEM_RELINQUISH: |
| 800 | case FFA_MEM_RECLAIM: |
| 801 | case FFA_SUCCESS_SMC32: |
| 802 | case FFA_SUCCESS_SMC64: |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 803 | /* |
| 804 | * TODO: Assume that no requests originate from EL3 at the |
| 805 | * moment. This will change if a SP service is required in |
| 806 | * response to secure interrupts targeted to EL3. Until then |
| 807 | * simply forward the call to the Normal world. |
| 808 | */ |
| 809 | |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 810 | return spmd_smc_forward(smc_fid, secure_origin, |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 811 | x1, x2, x3, x4, handle); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 812 | break; /* not reached */ |
| 813 | |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 814 | case FFA_MSG_WAIT: |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 815 | /* |
| 816 | * Check if this is the first invocation of this interface on |
| 817 | * this CPU from the Secure world. If so, then indicate that the |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 818 | * SPM Core initialised successfully. |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 819 | */ |
Olivier Deprez | 7c01633 | 2019-10-28 09:03:13 +0000 | [diff] [blame] | 820 | if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 821 | spmd_spm_core_sync_exit(0ULL); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 824 | /* Fall through to forward the call to the other world */ |
Olivier Deprez | ae18caf | 2021-04-02 11:09:10 +0200 | [diff] [blame] | 825 | case FFA_INTERRUPT: |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 826 | case FFA_MSG_YIELD: |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 827 | /* This interface must be invoked only by the Secure world */ |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 828 | if (!secure_origin) { |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 829 | return spmd_ffa_error_return(handle, |
| 830 | FFA_ERROR_NOT_SUPPORTED); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 831 | } |
| 832 | |
Olivier Deprez | 41ff36a | 2019-12-23 16:21:12 +0100 | [diff] [blame] | 833 | return spmd_smc_forward(smc_fid, secure_origin, |
Max Shvetsov | 745889c | 2020-02-27 14:54:21 +0000 | [diff] [blame] | 834 | x1, x2, x3, x4, handle); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 835 | break; /* not reached */ |
| 836 | |
Olivier Deprez | a664c49 | 2020-08-05 11:27:42 +0200 | [diff] [blame] | 837 | case FFA_NORMAL_WORLD_RESUME: |
| 838 | if (secure_origin && ctx->secure_interrupt_ongoing) { |
| 839 | spmd_spm_core_sync_exit(0ULL); |
| 840 | } else { |
| 841 | return spmd_ffa_error_return(handle, FFA_ERROR_DENIED); |
| 842 | } |
| 843 | break; /* Not reached */ |
| 844 | |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 845 | default: |
| 846 | WARN("SPM: Unsupported call 0x%08x\n", smc_fid); |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 847 | return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); |
Achin Gupta | 86f2353 | 2019-10-11 15:41:16 +0100 | [diff] [blame] | 848 | } |
| 849 | } |