Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <arch_helpers.h> |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <common_def.h> |
| 11 | #include <context.h> |
| 12 | #include <context_mgmt.h> |
| 13 | #include <debug.h> |
| 14 | #include <platform_def.h> |
| 15 | #include <platform.h> |
| 16 | #include <secure_partition.h> |
| 17 | #include <string.h> |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 18 | #include <xlat_tables_v2.h> |
| 19 | |
| 20 | #include "spm_private.h" |
| 21 | #include "spm_shim_private.h" |
| 22 | |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 23 | /* Setup context of the Secure Partition */ |
Antonio Nino Diaz | 2875931 | 2018-05-22 16:26:48 +0100 | [diff] [blame] | 24 | void spm_sp_setup(sp_context_t *sp_ctx) |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 25 | { |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 26 | cpu_context_t *ctx = &(sp_ctx->cpu_ctx); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 27 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Initialize CPU context |
| 30 | * ---------------------- |
| 31 | */ |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 32 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 33 | entry_point_info_t ep_info = {0}; |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 34 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 35 | SET_PARAM_HEAD(&ep_info, PARAM_EP, VERSION_1, SECURE | EP_ST_ENABLE); |
Antonio Nino Diaz | 268eb5d | 2018-06-11 11:01:54 +0100 | [diff] [blame] | 36 | |
| 37 | /* Setup entrypoint and SPSR */ |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 38 | ep_info.pc = BL32_BASE; |
| 39 | ep_info.spsr = SPSR_64(MODE_EL0, MODE_SP_EL0, DISABLE_ALL_EXCEPTIONS); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 40 | |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 41 | /* |
| 42 | * X0: Virtual address of a buffer shared between EL3 and Secure EL0. |
| 43 | * The buffer will be mapped in the Secure EL1 translation regime |
| 44 | * with Normal IS WBWA attributes and RO data and Execute Never |
| 45 | * instruction access permissions. |
| 46 | * |
| 47 | * X1: Size of the buffer in bytes |
| 48 | * |
| 49 | * X2: cookie value (Implementation Defined) |
| 50 | * |
| 51 | * X3: cookie value (Implementation Defined) |
| 52 | * |
Antonio Nino Diaz | 268eb5d | 2018-06-11 11:01:54 +0100 | [diff] [blame] | 53 | * X4 to X7 = 0 |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 54 | */ |
Antonio Nino Diaz | 268eb5d | 2018-06-11 11:01:54 +0100 | [diff] [blame] | 55 | ep_info.args.arg0 = PLAT_SPM_BUF_BASE; |
| 56 | ep_info.args.arg1 = PLAT_SPM_BUF_SIZE; |
| 57 | ep_info.args.arg2 = PLAT_SPM_COOKIE_0; |
| 58 | ep_info.args.arg3 = PLAT_SPM_COOKIE_1; |
| 59 | |
| 60 | cm_setup_context(ctx, &ep_info); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * SP_EL0: A non-zero value will indicate to the SP that the SPM has |
| 64 | * initialized the stack pointer for the current CPU through |
| 65 | * implementation defined means. The value will be 0 otherwise. |
| 66 | */ |
| 67 | write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_SP_EL0, |
| 68 | PLAT_SP_IMAGE_STACK_BASE + PLAT_SP_IMAGE_STACK_PCPU_SIZE); |
| 69 | |
| 70 | /* |
| 71 | * Setup translation tables |
| 72 | * ------------------------ |
| 73 | */ |
| 74 | |
| 75 | #if ENABLE_ASSERTIONS |
| 76 | |
| 77 | /* Get max granularity supported by the platform. */ |
Antonio Nino Diaz | 4413ad5 | 2018-06-11 13:40:32 +0100 | [diff] [blame] | 78 | unsigned int max_granule = xlat_arch_get_max_supported_granule_size(); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 79 | |
Antonio Nino Diaz | 4413ad5 | 2018-06-11 13:40:32 +0100 | [diff] [blame] | 80 | VERBOSE("Max translation granule size supported: %u KiB\n", |
| 81 | max_granule / 1024U); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 82 | |
Antonio Nino Diaz | 4413ad5 | 2018-06-11 13:40:32 +0100 | [diff] [blame] | 83 | unsigned int max_granule_mask = max_granule - 1U; |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 84 | |
| 85 | /* Base must be aligned to the max granularity */ |
Antonio Nino Diaz | 4413ad5 | 2018-06-11 13:40:32 +0100 | [diff] [blame] | 86 | assert((ARM_SP_IMAGE_NS_BUF_BASE & max_granule_mask) == 0); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 87 | |
| 88 | /* Size must be a multiple of the max granularity */ |
Antonio Nino Diaz | 4413ad5 | 2018-06-11 13:40:32 +0100 | [diff] [blame] | 89 | assert((ARM_SP_IMAGE_NS_BUF_SIZE & max_granule_mask) == 0); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 90 | |
| 91 | #endif /* ENABLE_ASSERTIONS */ |
| 92 | |
| 93 | /* This region contains the exception vectors used at S-EL1. */ |
| 94 | const mmap_region_t sel1_exception_vectors = |
| 95 | MAP_REGION_FLAT(SPM_SHIM_EXCEPTIONS_START, |
| 96 | SPM_SHIM_EXCEPTIONS_SIZE, |
| 97 | MT_CODE | MT_SECURE | MT_PRIVILEGED); |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 98 | mmap_add_region_ctx(sp_ctx->xlat_ctx_handle, |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 99 | &sel1_exception_vectors); |
| 100 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 101 | mmap_add_ctx(sp_ctx->xlat_ctx_handle, |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 102 | plat_get_secure_partition_mmap(NULL)); |
| 103 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 104 | init_xlat_tables_ctx(sp_ctx->xlat_ctx_handle); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * MMU-related registers |
| 108 | * --------------------- |
| 109 | */ |
| 110 | |
| 111 | /* Set attributes in the right indices of the MAIR */ |
| 112 | u_register_t mair_el1 = |
| 113 | MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX) | |
| 114 | MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, ATTR_IWBWA_OWBWA_NTR_INDEX) | |
| 115 | MAIR_ATTR_SET(ATTR_NON_CACHEABLE, ATTR_NON_CACHEABLE_INDEX); |
| 116 | |
| 117 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_MAIR_EL1, mair_el1); |
| 118 | |
| 119 | /* Setup TCR_EL1. */ |
| 120 | u_register_t tcr_ps_bits = tcr_physical_addr_size_bits(PLAT_PHY_ADDR_SPACE_SIZE); |
| 121 | |
| 122 | u_register_t tcr_el1 = |
| 123 | /* Size of region addressed by TTBR0_EL1 = 2^(64-T0SZ) bytes. */ |
| 124 | (64 - __builtin_ctzl(PLAT_VIRT_ADDR_SPACE_SIZE)) | |
| 125 | /* Inner and outer WBWA, shareable. */ |
| 126 | TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA | |
| 127 | /* Set the granularity to 4KB. */ |
| 128 | TCR_TG0_4K | |
| 129 | /* Limit Intermediate Physical Address Size. */ |
| 130 | tcr_ps_bits << TCR_EL1_IPS_SHIFT | |
| 131 | /* Disable translations using TBBR1_EL1. */ |
| 132 | TCR_EPD1_BIT |
| 133 | /* The remaining fields related to TBBR1_EL1 are left as zero. */ |
| 134 | ; |
| 135 | |
| 136 | tcr_el1 &= ~( |
| 137 | /* Enable translations using TBBR0_EL1 */ |
| 138 | TCR_EPD0_BIT |
| 139 | ); |
| 140 | |
| 141 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_TCR_EL1, tcr_el1); |
| 142 | |
| 143 | /* Setup SCTLR_EL1 */ |
| 144 | u_register_t sctlr_el1 = read_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1); |
| 145 | |
| 146 | sctlr_el1 |= |
| 147 | /*SCTLR_EL1_RES1 |*/ |
| 148 | /* Don't trap DC CVAU, DC CIVAC, DC CVAC, DC CVAP, or IC IVAU */ |
| 149 | SCTLR_UCI_BIT | |
| 150 | /* RW regions at xlat regime EL1&0 are forced to be XN. */ |
| 151 | SCTLR_WXN_BIT | |
| 152 | /* Don't trap to EL1 execution of WFI or WFE at EL0. */ |
| 153 | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT | |
| 154 | /* Don't trap to EL1 accesses to CTR_EL0 from EL0. */ |
| 155 | SCTLR_UCT_BIT | |
| 156 | /* Don't trap to EL1 execution of DZ ZVA at EL0. */ |
| 157 | SCTLR_DZE_BIT | |
| 158 | /* Enable SP Alignment check for EL0 */ |
| 159 | SCTLR_SA0_BIT | |
| 160 | /* Allow cacheable data and instr. accesses to normal memory. */ |
| 161 | SCTLR_C_BIT | SCTLR_I_BIT | |
| 162 | /* Alignment fault checking enabled when at EL1 and EL0. */ |
| 163 | SCTLR_A_BIT | |
| 164 | /* Enable MMU. */ |
| 165 | SCTLR_M_BIT |
| 166 | ; |
| 167 | |
| 168 | sctlr_el1 &= ~( |
| 169 | /* Explicit data accesses at EL0 are little-endian. */ |
| 170 | SCTLR_E0E_BIT | |
| 171 | /* Accesses to DAIF from EL0 are trapped to EL1. */ |
| 172 | SCTLR_UMA_BIT |
| 173 | ); |
| 174 | |
| 175 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_el1); |
| 176 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 177 | uint64_t *xlat_base = |
| 178 | ((xlat_ctx_t *)sp_ctx->xlat_ctx_handle)->base_table; |
| 179 | |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 180 | /* Point TTBR0_EL1 at the tables of the context created for the SP. */ |
| 181 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR0_EL1, |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 182 | (u_register_t)xlat_base); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Setup other system registers |
| 186 | * ---------------------------- |
| 187 | */ |
| 188 | |
| 189 | /* Shim Exception Vector Base Address */ |
| 190 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_VBAR_EL1, |
| 191 | SPM_SHIM_EXCEPTIONS_PTR); |
| 192 | |
| 193 | /* |
Sandrine Bailleux | 811934e | 2018-05-09 14:45:34 +0200 | [diff] [blame] | 194 | * FPEN: Allow the Secure Partition to access FP/SIMD registers. |
| 195 | * Note that SPM will not do any saving/restoring of these registers on |
| 196 | * behalf of the SP. This falls under the SP's responsibility. |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 197 | * TTA: Enable access to trace registers. |
| 198 | * ZEN (v8.2): Trap SVE instructions and access to SVE registers. |
| 199 | */ |
| 200 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_CPACR_EL1, |
Sandrine Bailleux | 811934e | 2018-05-09 14:45:34 +0200 | [diff] [blame] | 201 | CPACR_EL1_FPEN(CPACR_EL1_FP_TRAP_NONE)); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * Prepare information in buffer shared between EL3 and S-EL0 |
| 205 | * ---------------------------------------------------------- |
| 206 | */ |
| 207 | |
| 208 | void *shared_buf_ptr = (void *) PLAT_SPM_BUF_BASE; |
| 209 | |
| 210 | /* Copy the boot information into the shared buffer with the SP. */ |
| 211 | assert((uintptr_t)shared_buf_ptr + sizeof(secure_partition_boot_info_t) |
| 212 | <= (PLAT_SPM_BUF_BASE + PLAT_SPM_BUF_SIZE)); |
| 213 | |
| 214 | assert(PLAT_SPM_BUF_BASE <= (UINTPTR_MAX - PLAT_SPM_BUF_SIZE + 1)); |
| 215 | |
| 216 | const secure_partition_boot_info_t *sp_boot_info = |
| 217 | plat_get_secure_partition_boot_info(NULL); |
| 218 | |
| 219 | assert(sp_boot_info != NULL); |
| 220 | |
| 221 | memcpy((void *) shared_buf_ptr, (const void *) sp_boot_info, |
| 222 | sizeof(secure_partition_boot_info_t)); |
| 223 | |
| 224 | /* Pointer to the MP information from the platform port. */ |
| 225 | secure_partition_mp_info_t *sp_mp_info = |
| 226 | ((secure_partition_boot_info_t *) shared_buf_ptr)->mp_info; |
| 227 | |
| 228 | assert(sp_mp_info != NULL); |
| 229 | |
| 230 | /* |
| 231 | * Point the shared buffer MP information pointer to where the info will |
| 232 | * be populated, just after the boot info. |
| 233 | */ |
| 234 | ((secure_partition_boot_info_t *) shared_buf_ptr)->mp_info = |
Antonio Nino Diaz | 0dcebaf | 2017-11-10 12:25:49 +0000 | [diff] [blame] | 235 | (secure_partition_mp_info_t *) ((uintptr_t)shared_buf_ptr |
| 236 | + sizeof(secure_partition_boot_info_t)); |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Update the shared buffer pointer to where the MP information for the |
| 240 | * payload will be populated |
| 241 | */ |
| 242 | shared_buf_ptr = ((secure_partition_boot_info_t *) shared_buf_ptr)->mp_info; |
| 243 | |
| 244 | /* |
| 245 | * Copy the cpu information into the shared buffer area after the boot |
| 246 | * information. |
| 247 | */ |
| 248 | assert(sp_boot_info->num_cpus <= PLATFORM_CORE_COUNT); |
| 249 | |
| 250 | assert((uintptr_t)shared_buf_ptr |
| 251 | <= (PLAT_SPM_BUF_BASE + PLAT_SPM_BUF_SIZE - |
| 252 | (sp_boot_info->num_cpus * sizeof(*sp_mp_info)))); |
| 253 | |
| 254 | memcpy(shared_buf_ptr, (const void *) sp_mp_info, |
| 255 | sp_boot_info->num_cpus * sizeof(*sp_mp_info)); |
| 256 | |
| 257 | /* |
| 258 | * Calculate the linear indices of cores in boot information for the |
| 259 | * secure partition and flag the primary CPU |
| 260 | */ |
| 261 | sp_mp_info = (secure_partition_mp_info_t *) shared_buf_ptr; |
| 262 | |
| 263 | for (unsigned int index = 0; index < sp_boot_info->num_cpus; index++) { |
| 264 | u_register_t mpidr = sp_mp_info[index].mpidr; |
| 265 | |
| 266 | sp_mp_info[index].linear_id = plat_core_pos_by_mpidr(mpidr); |
| 267 | if (plat_my_core_pos() == sp_mp_info[index].linear_id) |
| 268 | sp_mp_info[index].flags |= MP_INFO_FLAG_PRIMARY_CPU; |
| 269 | } |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 270 | } |