blob: ecb8bd363c8ea023df11b266b3adcd2ccbe75aad [file] [log] [blame]
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01001/*
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +01002 * Copyright (c) 2017-2018, 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
7#include <arch.h>
8#include <arch_helpers.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01009#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>
Antonio Nino Diazfa5dd1a2018-06-26 10:34:25 +010016#include <sp_res_desc.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010017#include <string.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010018#include <xlat_tables_v2.h>
19
20#include "spm_private.h"
21#include "spm_shim_private.h"
22
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010023/* Setup context of the Secure Partition */
Antonio Nino Diaz28759312018-05-22 16:26:48 +010024void spm_sp_setup(sp_context_t *sp_ctx)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010025{
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010026 cpu_context_t *ctx = &(sp_ctx->cpu_ctx);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010027
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010028 /*
29 * Initialize CPU context
30 * ----------------------
31 */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010032
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010033 entry_point_info_t ep_info = {0};
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010034
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010035 SET_PARAM_HEAD(&ep_info, PARAM_EP, VERSION_1, SECURE | EP_ST_ENABLE);
Antonio Nino Diaz268eb5d2018-06-11 11:01:54 +010036
37 /* Setup entrypoint and SPSR */
Antonio Nino Diazfa5dd1a2018-06-26 10:34:25 +010038 ep_info.pc = sp_ctx->rd.attribute.entrypoint;
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010039 ep_info.spsr = SPSR_64(MODE_EL0, MODE_SP_EL0, DISABLE_ALL_EXCEPTIONS);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010040
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010041 /*
Antonio Nino Diazfe7b2be2018-10-30 11:54:20 +000042 * X0: Unused (MBZ).
43 * X1: Unused (MBZ).
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010044 * X2: cookie value (Implementation Defined)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010045 * X3: cookie value (Implementation Defined)
Antonio Nino Diaz268eb5d2018-06-11 11:01:54 +010046 * X4 to X7 = 0
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010047 */
Antonio Nino Diazfe7b2be2018-10-30 11:54:20 +000048 ep_info.args.arg0 = 0;
49 ep_info.args.arg1 = 0;
Antonio Nino Diaz268eb5d2018-06-11 11:01:54 +010050 ep_info.args.arg2 = PLAT_SPM_COOKIE_0;
51 ep_info.args.arg3 = PLAT_SPM_COOKIE_1;
52
53 cm_setup_context(ctx, &ep_info);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010054
55 /*
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010056 * Setup translation tables
57 * ------------------------
58 */
59
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010060 /* This region contains the exception vectors used at S-EL1. */
61 const mmap_region_t sel1_exception_vectors =
62 MAP_REGION_FLAT(SPM_SHIM_EXCEPTIONS_START,
63 SPM_SHIM_EXCEPTIONS_SIZE,
64 MT_CODE | MT_SECURE | MT_PRIVILEGED);
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010065 mmap_add_region_ctx(sp_ctx->xlat_ctx_handle,
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010066 &sel1_exception_vectors);
67
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010068 mmap_add_ctx(sp_ctx->xlat_ctx_handle,
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010069 plat_get_secure_partition_mmap(NULL));
70
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010071 init_xlat_tables_ctx(sp_ctx->xlat_ctx_handle);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010072
73 /*
74 * MMU-related registers
75 * ---------------------
76 */
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010077 xlat_ctx_t *xlat_ctx = sp_ctx->xlat_ctx_handle;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010078
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010079 uint64_t mmu_cfg_params[MMU_CFG_PARAM_MAX];
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010080
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010081 setup_mmu_cfg((uint64_t *)&mmu_cfg_params, 0, xlat_ctx->base_table,
82 xlat_ctx->pa_max_address, xlat_ctx->va_max_address,
83 EL1_EL0_REGIME);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010084
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010085 write_ctx_reg(get_sysregs_ctx(ctx), CTX_MAIR_EL1,
86 mmu_cfg_params[MMU_CFG_MAIR]);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010087
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010088 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TCR_EL1,
89 mmu_cfg_params[MMU_CFG_TCR]);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010090
Antonio Nino Diazea4c6932018-07-11 13:07:06 +010091 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR0_EL1,
92 mmu_cfg_params[MMU_CFG_TTBR0]);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010093
94 /* Setup SCTLR_EL1 */
95 u_register_t sctlr_el1 = read_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1);
96
97 sctlr_el1 |=
98 /*SCTLR_EL1_RES1 |*/
99 /* Don't trap DC CVAU, DC CIVAC, DC CVAC, DC CVAP, or IC IVAU */
100 SCTLR_UCI_BIT |
101 /* RW regions at xlat regime EL1&0 are forced to be XN. */
102 SCTLR_WXN_BIT |
103 /* Don't trap to EL1 execution of WFI or WFE at EL0. */
104 SCTLR_NTWI_BIT | SCTLR_NTWE_BIT |
105 /* Don't trap to EL1 accesses to CTR_EL0 from EL0. */
106 SCTLR_UCT_BIT |
107 /* Don't trap to EL1 execution of DZ ZVA at EL0. */
108 SCTLR_DZE_BIT |
109 /* Enable SP Alignment check for EL0 */
110 SCTLR_SA0_BIT |
111 /* Allow cacheable data and instr. accesses to normal memory. */
112 SCTLR_C_BIT | SCTLR_I_BIT |
113 /* Alignment fault checking enabled when at EL1 and EL0. */
114 SCTLR_A_BIT |
115 /* Enable MMU. */
116 SCTLR_M_BIT
117 ;
118
119 sctlr_el1 &= ~(
120 /* Explicit data accesses at EL0 are little-endian. */
121 SCTLR_E0E_BIT |
122 /* Accesses to DAIF from EL0 are trapped to EL1. */
123 SCTLR_UMA_BIT
124 );
125
126 write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_el1);
127
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100128 /*
129 * Setup other system registers
130 * ----------------------------
131 */
132
133 /* Shim Exception Vector Base Address */
134 write_ctx_reg(get_sysregs_ctx(ctx), CTX_VBAR_EL1,
135 SPM_SHIM_EXCEPTIONS_PTR);
136
137 /*
Sandrine Bailleux811934e2018-05-09 14:45:34 +0200138 * FPEN: Allow the Secure Partition to access FP/SIMD registers.
139 * Note that SPM will not do any saving/restoring of these registers on
140 * behalf of the SP. This falls under the SP's responsibility.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100141 * TTA: Enable access to trace registers.
142 * ZEN (v8.2): Trap SVE instructions and access to SVE registers.
143 */
144 write_ctx_reg(get_sysregs_ctx(ctx), CTX_CPACR_EL1,
Sandrine Bailleux811934e2018-05-09 14:45:34 +0200145 CPACR_EL1_FPEN(CPACR_EL1_FP_TRAP_NONE));
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100146}