blob: 2ed44d134b0fb5ce05d3f861a3066993a95b062b [file] [log] [blame]
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01001/*
Antonio Nino Diaz37f97a52019-03-27 11:10:31 +00002 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8#include <string.h>
9
10#include <platform_def.h>
11
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010012#include <arch.h>
13#include <arch_helpers.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010014#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <common/debug.h>
16#include <lib/el3_runtime/context_mgmt.h>
17#include <lib/xlat_tables/xlat_tables_v2.h>
18#include <plat/common/common_def.h>
19#include <plat/common/platform.h>
20#include <services/sp_res_desc.h>
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +000021#include <sprt_host.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010022
23#include "spm_private.h"
24#include "spm_shim_private.h"
25
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010026/* Setup context of the Secure Partition */
Antonio Nino Diaz28759312018-05-22 16:26:48 +010027void spm_sp_setup(sp_context_t *sp_ctx)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010028{
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010029 cpu_context_t *ctx = &(sp_ctx->cpu_ctx);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010030
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010031 /*
32 * Initialize CPU context
33 * ----------------------
34 */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010035
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010036 entry_point_info_t ep_info = {0};
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010037
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010038 SET_PARAM_HEAD(&ep_info, PARAM_EP, VERSION_1, SECURE | EP_ST_ENABLE);
Antonio Nino Diaz268eb5d2018-06-11 11:01:54 +010039
40 /* Setup entrypoint and SPSR */
Antonio Nino Diazfa5dd1a2018-06-26 10:34:25 +010041 ep_info.pc = sp_ctx->rd.attribute.entrypoint;
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010042 ep_info.spsr = SPSR_64(MODE_EL0, MODE_SP_EL0, DISABLE_ALL_EXCEPTIONS);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010043
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010044 /*
Antonio Nino Diazfe7b2be2018-10-30 11:54:20 +000045 * X0: Unused (MBZ).
46 * X1: Unused (MBZ).
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010047 * X2: cookie value (Implementation Defined)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010048 * X3: cookie value (Implementation Defined)
Antonio Nino Diaz268eb5d2018-06-11 11:01:54 +010049 * X4 to X7 = 0
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010050 */
Antonio Nino Diazfe7b2be2018-10-30 11:54:20 +000051 ep_info.args.arg0 = 0;
52 ep_info.args.arg1 = 0;
Antonio Nino Diaz268eb5d2018-06-11 11:01:54 +010053 ep_info.args.arg2 = PLAT_SPM_COOKIE_0;
54 ep_info.args.arg3 = PLAT_SPM_COOKIE_1;
55
56 cm_setup_context(ctx, &ep_info);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010057
58 /*
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010059 * Setup translation tables
60 * ------------------------
61 */
62
Antonio Nino Diaz18e312d2019-03-27 13:04:46 +000063 /* Assign translation tables context. */
64 spm_sp_xlat_context_alloc(sp_ctx);
65
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000066 sp_map_memory_regions(sp_ctx);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010067
68 /*
69 * MMU-related registers
70 * ---------------------
71 */
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010072 xlat_ctx_t *xlat_ctx = sp_ctx->xlat_ctx_handle;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010073
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010074 uint64_t mmu_cfg_params[MMU_CFG_PARAM_MAX];
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010075
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010076 setup_mmu_cfg((uint64_t *)&mmu_cfg_params, 0, xlat_ctx->base_table,
77 xlat_ctx->pa_max_address, xlat_ctx->va_max_address,
78 EL1_EL0_REGIME);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010079
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010080 write_ctx_reg(get_sysregs_ctx(ctx), CTX_MAIR_EL1,
81 mmu_cfg_params[MMU_CFG_MAIR]);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010082
Antonio Nino Diaz37f97a52019-03-27 11:10:31 +000083 /* Enable translations using TTBR1_EL1 */
84 int t1sz = 64 - __builtin_ctzll(SPM_SHIM_XLAT_VIRT_ADDR_SPACE_SIZE);
85 mmu_cfg_params[MMU_CFG_TCR] &= ~TCR_EPD1_BIT;
86 mmu_cfg_params[MMU_CFG_TCR] |=
87 ((uint64_t)t1sz << TCR_T1SZ_SHIFT) |
88 TCR_SH1_INNER_SHAREABLE |
89 TCR_RGN1_OUTER_WBA | TCR_RGN1_INNER_WBA |
90 TCR_TG1_4K;
91
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010092 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TCR_EL1,
93 mmu_cfg_params[MMU_CFG_TCR]);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010094
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010095 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR0_EL1,
96 mmu_cfg_params[MMU_CFG_TTBR0]);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010097
Antonio Nino Diaz37f97a52019-03-27 11:10:31 +000098 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR1_EL1,
99 (uint64_t)spm_exceptions_xlat_get_base_table());
100
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100101 /* Setup SCTLR_EL1 */
102 u_register_t sctlr_el1 = read_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1);
103
104 sctlr_el1 |=
105 /*SCTLR_EL1_RES1 |*/
106 /* Don't trap DC CVAU, DC CIVAC, DC CVAC, DC CVAP, or IC IVAU */
107 SCTLR_UCI_BIT |
108 /* RW regions at xlat regime EL1&0 are forced to be XN. */
109 SCTLR_WXN_BIT |
110 /* Don't trap to EL1 execution of WFI or WFE at EL0. */
111 SCTLR_NTWI_BIT | SCTLR_NTWE_BIT |
112 /* Don't trap to EL1 accesses to CTR_EL0 from EL0. */
113 SCTLR_UCT_BIT |
114 /* Don't trap to EL1 execution of DZ ZVA at EL0. */
115 SCTLR_DZE_BIT |
116 /* Enable SP Alignment check for EL0 */
117 SCTLR_SA0_BIT |
118 /* Allow cacheable data and instr. accesses to normal memory. */
119 SCTLR_C_BIT | SCTLR_I_BIT |
120 /* Alignment fault checking enabled when at EL1 and EL0. */
121 SCTLR_A_BIT |
122 /* Enable MMU. */
123 SCTLR_M_BIT
124 ;
125
126 sctlr_el1 &= ~(
127 /* Explicit data accesses at EL0 are little-endian. */
128 SCTLR_E0E_BIT |
129 /* Accesses to DAIF from EL0 are trapped to EL1. */
130 SCTLR_UMA_BIT
131 );
132
133 write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_el1);
134
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100135 /*
136 * Setup other system registers
137 * ----------------------------
138 */
139
Antonio Nino Diaz37f97a52019-03-27 11:10:31 +0000140 /*
141 * Shim exception vector base address. It is mapped at the start of the
142 * address space accessed by TTBR1_EL1, which means that the base
143 * address of the exception vectors depends on the size of the address
144 * space specified in TCR_EL1.T1SZ.
145 */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100146 write_ctx_reg(get_sysregs_ctx(ctx), CTX_VBAR_EL1,
Antonio Nino Diaz37f97a52019-03-27 11:10:31 +0000147 UINT64_MAX - (SPM_SHIM_XLAT_VIRT_ADDR_SPACE_SIZE - 1ULL));
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100148
149 /*
Sandrine Bailleux811934e2018-05-09 14:45:34 +0200150 * FPEN: Allow the Secure Partition to access FP/SIMD registers.
151 * Note that SPM will not do any saving/restoring of these registers on
152 * behalf of the SP. This falls under the SP's responsibility.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100153 * TTA: Enable access to trace registers.
154 * ZEN (v8.2): Trap SVE instructions and access to SVE registers.
155 */
156 write_ctx_reg(get_sysregs_ctx(ctx), CTX_CPACR_EL1,
Sandrine Bailleux811934e2018-05-09 14:45:34 +0200157 CPACR_EL1_FPEN(CPACR_EL1_FP_TRAP_NONE));
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000158
159 /*
160 * Prepare shared buffers
161 * ----------------------
162 */
163
164 /* Initialize SPRT queues */
165 sprt_initialize_queues((void *)sp_ctx->spm_sp_buffer_base,
166 sp_ctx->spm_sp_buffer_size);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100167}