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