blob: ac26bd2e5308aae491c1b66634bd31c249067823 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Maksims Svecovs01cf14d2023-02-02 16:10:22 +00002 * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
Varun Wadekar2b287272022-09-13 12:38:47 +01003 * Copyright (c) 2022, NVIDIA Corporation. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00004 *
dp-arm82cb2c12017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00006 */
7
Dan Handley97043ac2014-04-09 13:14:54 +01008#include <assert.h>
Antonio Nino Diaz40daecc2018-10-25 16:52:26 +01009#include <stdbool.h>
Andrew Thoelke167a9352014-06-04 21:10:52 +010010#include <string.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011
12#include <platform_def.h>
13
14#include <arch.h>
15#include <arch_helpers.h>
Soby Mathewb7e398d2019-07-12 09:23:38 +010016#include <arch_features.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000017#include <bl31/interrupt_mgmt.h>
18#include <common/bl_common.h>
Claus Pedersen885e2682022-09-12 22:42:58 +000019#include <common/debug.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000020#include <context.h>
Zelalem Aweke8b95e842022-01-31 16:59:42 -060021#include <drivers/arm/gicv3.h>
Arvind Ram Prakashc4c6eae2024-08-05 16:11:42 -050022#include <lib/cpus/cpu_ops.h>
23#include <lib/cpus/errata.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000024#include <lib/el3_runtime/context_mgmt.h>
Elizabeth Ho461c0a52023-07-18 14:10:25 +010025#include <lib/el3_runtime/cpu_data.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000026#include <lib/el3_runtime/pubsub_events.h>
27#include <lib/extensions/amu.h>
johpow01744ad972022-01-28 17:06:20 -060028#include <lib/extensions/brbe.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000029#include <lib/extensions/mpam.h>
Boyan Karatotevc73686a2023-02-15 13:21:50 +000030#include <lib/extensions/pmuv3.h>
johpow01dc78e622021-07-08 14:14:00 -050031#include <lib/extensions/sme.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000032#include <lib/extensions/spe.h>
33#include <lib/extensions/sve.h>
Manish V Badarkhed4582d32021-06-29 11:44:20 +010034#include <lib/extensions/sys_reg_trace.h>
Manish V Badarkhe813524e2021-07-02 09:10:56 +010035#include <lib/extensions/trbe.h>
Manish V Badarkhe8fcd3d92021-07-08 09:33:18 +010036#include <lib/extensions/trf.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000037#include <lib/utils.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000038
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +010039#if ENABLE_FEAT_TWED
40/* Make sure delay value fits within the range(0-15) */
41CASSERT(((TWED_DELAY & ~SCR_TWEDEL_MASK) == 0U), assert_twed_delay_value_check);
42#endif /* ENABLE_FEAT_TWED */
Achin Gupta7aea9082014-02-01 07:51:28 +000043
Elizabeth Ho461c0a52023-07-18 14:10:25 +010044per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
45static bool has_secure_perworld_init;
46
Boyan Karatotev24a70732023-03-08 11:56:49 +000047static void manage_extensions_nonsecure(cpu_context_t *ctx);
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +010048static void manage_extensions_secure(cpu_context_t *ctx);
Elizabeth Ho461c0a52023-07-18 14:10:25 +010049static void manage_extensions_secure_per_world(void);
Zelalem Awekeb515f542022-04-08 16:48:05 -050050
51static void setup_el1_context(cpu_context_t *ctx, const struct entry_point_info *ep)
52{
53 u_register_t sctlr_elx, actlr_elx;
54
55 /*
56 * Initialise SCTLR_EL1 to the reset value corresponding to the target
57 * execution state setting all fields rather than relying on the hw.
58 * Some fields have architecturally UNKNOWN reset values and these are
59 * set to zero.
60 *
61 * SCTLR.EE: Endianness is taken from the entrypoint attributes.
62 *
63 * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
64 * required by PSCI specification)
65 */
66 sctlr_elx = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
67 if (GET_RW(ep->spsr) == MODE_RW_64) {
68 sctlr_elx |= SCTLR_EL1_RES1;
69 } else {
70 /*
71 * If the target execution state is AArch32 then the following
72 * fields need to be set.
73 *
74 * SCTRL_EL1.nTWE: Set to one so that EL0 execution of WFE
75 * instructions are not trapped to EL1.
76 *
77 * SCTLR_EL1.nTWI: Set to one so that EL0 execution of WFI
78 * instructions are not trapped to EL1.
79 *
80 * SCTLR_EL1.CP15BEN: Set to one to enable EL0 execution of the
81 * CP15DMB, CP15DSB, and CP15ISB instructions.
82 */
83 sctlr_elx |= SCTLR_AARCH32_EL1_RES1 | SCTLR_CP15BEN_BIT
84 | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
85 }
86
87#if ERRATA_A75_764081
88 /*
89 * If workaround of errata 764081 for Cortex-A75 is used then set
90 * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
91 */
92 sctlr_elx |= SCTLR_IESB_BIT;
93#endif
94 /* Store the initialised SCTLR_EL1 value in the cpu_context */
95 write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
96
97 /*
98 * Base the context ACTLR_EL1 on the current value, as it is
99 * implementation defined. The context restore process will write
100 * the value from the context to the actual register and can cause
101 * problems for processor cores that don't expect certain bits to
102 * be zero.
103 */
104 actlr_elx = read_actlr_el1();
105 write_ctx_reg((get_el1_sysregs_ctx(ctx)), (CTX_ACTLR_EL1), (actlr_elx));
106}
107
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600108/******************************************************************************
109 * This function performs initializations that are specific to SECURE state
110 * and updates the cpu context specified by 'ctx'.
111 *****************************************************************************/
112static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
Achin Gupta7aea9082014-02-01 07:51:28 +0000113{
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600114 u_register_t scr_el3;
115 el3_state_t *state;
116
117 state = get_el3state_ctx(ctx);
118 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
119
120#if defined(IMAGE_BL31) && !defined(SPD_spmd)
Achin Gupta7aea9082014-02-01 07:51:28 +0000121 /*
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600122 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
123 * indicated by the interrupt routing model for BL31.
Achin Gupta7aea9082014-02-01 07:51:28 +0000124 */
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600125 scr_el3 |= get_scr_el3_from_routing_model(SECURE);
126#endif
127
128#if !CTX_INCLUDE_MTE_REGS || ENABLE_ASSERTIONS
129 /* Get Memory Tagging Extension support level */
130 unsigned int mte = get_armv8_5_mte_support();
131#endif
132 /*
133 * Allow access to Allocation Tags when CTX_INCLUDE_MTE_REGS
134 * is set, or when MTE is only implemented at EL0.
135 */
136#if CTX_INCLUDE_MTE_REGS
137 assert((mte == MTE_IMPLEMENTED_ELX) || (mte == MTE_IMPLEMENTED_ASY));
138 scr_el3 |= SCR_ATA_BIT;
139#else
140 if (mte == MTE_IMPLEMENTED_EL0) {
141 scr_el3 |= SCR_ATA_BIT;
142 }
143#endif /* CTX_INCLUDE_MTE_REGS */
144
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600145 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
146
Zelalem Awekeb515f542022-04-08 16:48:05 -0500147 /*
148 * Initialize EL1 context registers unless SPMC is running
149 * at S-EL2.
150 */
151#if !SPMD_SPM_AT_SEL2
152 setup_el1_context(ctx, ep);
153#endif
154
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600155 manage_extensions_secure(ctx);
Elizabeth Ho461c0a52023-07-18 14:10:25 +0100156
157 /**
158 * manage_extensions_secure_per_world api has to be executed once,
159 * as the registers getting initialised, maintain constant value across
160 * all the cpus for the secure world.
161 * Henceforth, this check ensures that the registers are initialised once
162 * and avoids re-initialization from multiple cores.
163 */
164 if (!has_secure_perworld_init) {
165 manage_extensions_secure_per_world();
166 }
167
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600168}
169
170#if ENABLE_RME
171/******************************************************************************
172 * This function performs initializations that are specific to REALM state
173 * and updates the cpu context specified by 'ctx'.
174 *****************************************************************************/
175static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
176{
177 u_register_t scr_el3;
178 el3_state_t *state;
179
180 state = get_el3state_ctx(ctx);
181 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
182
Maksims Svecovs01cf14d2023-02-02 16:10:22 +0000183 scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
184
Andre Przywara7db710f2022-11-17 17:30:43 +0000185 if (is_feat_csv2_2_supported()) {
186 /* Enable access to the SCXTNUM_ELx registers. */
187 scr_el3 |= SCR_EnSCXT_BIT;
188 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600189
190 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
191}
192#endif /* ENABLE_RME */
193
194/******************************************************************************
195 * This function performs initializations that are specific to NON-SECURE state
196 * and updates the cpu context specified by 'ctx'.
197 *****************************************************************************/
198static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
199{
200 u_register_t scr_el3;
201 el3_state_t *state;
202
203 state = get_el3state_ctx(ctx);
204 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
205
206 /* SCR_NS: Set the NS bit */
207 scr_el3 |= SCR_NS_BIT;
208
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600209 /* Allow access to Allocation Tags when MTE is implemented. */
210 scr_el3 |= SCR_ATA_BIT;
211
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100212#if !CTX_INCLUDE_PAUTH_REGS
213 /*
214 * Pointer Authentication feature, if present, is always enabled by default
215 * for Non secure lower exception levels. We do not have an explicit
216 * flag to set it.
217 * CTX_INCLUDE_PAUTH_REGS flag, is explicitly used to enable for lower
218 * exception levels of secure and realm worlds.
219 *
220 * To prevent the leakage between the worlds during world switch,
221 * we enable it only for the non-secure world.
222 *
223 * If the Secure/realm world wants to use pointer authentication,
224 * CTX_INCLUDE_PAUTH_REGS must be explicitly set to 1, in which case
225 * it will be enabled globally for all the contexts.
226 *
227 * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
228 * other than EL3
229 *
230 * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
231 * than EL3
232 */
233 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
234
235#endif /* CTX_INCLUDE_PAUTH_REGS */
236
Manish Pandey46cc41d2022-10-10 11:43:08 +0100237#if HANDLE_EA_EL3_FIRST_NS
238 /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
239 scr_el3 |= SCR_EA_BIT;
240#endif
241
Manish Pandey00e8f792022-09-27 14:30:34 +0100242#if RAS_TRAP_NS_ERR_REC_ACCESS
243 /*
244 * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
245 * and RAS ERX registers from EL1 and EL2(from any security state)
246 * are trapped to EL3.
247 * Set here to trap only for NS EL1/EL2
248 *
249 */
250 scr_el3 |= SCR_TERR_BIT;
251#endif
252
Andre Przywara7db710f2022-11-17 17:30:43 +0000253 if (is_feat_csv2_2_supported()) {
254 /* Enable access to the SCXTNUM_ELx registers. */
255 scr_el3 |= SCR_EnSCXT_BIT;
256 }
Maksims Svecovs01cf14d2023-02-02 16:10:22 +0000257
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600258#ifdef IMAGE_BL31
259 /*
260 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
261 * indicated by the interrupt routing model for BL31.
262 */
263 scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
264#endif
265 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600266
Zelalem Awekeb515f542022-04-08 16:48:05 -0500267 /* Initialize EL1 context registers */
268 setup_el1_context(ctx, ep);
269
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600270 /* Initialize EL2 context registers */
271#if CTX_INCLUDE_EL2_REGS
272
273 /*
274 * Initialize SCTLR_EL2 context register using Endianness value
275 * taken from the entrypoint attribute.
276 */
277 u_register_t sctlr_el2 = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
278 sctlr_el2 |= SCTLR_EL2_RES1;
279 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_SCTLR_EL2,
280 sctlr_el2);
281
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600282 if (is_feat_hcx_supported()) {
283 /*
284 * Initialize register HCRX_EL2 with its init value.
285 * As the value of HCRX_EL2 is UNKNOWN on reset, there is a
286 * chance that this can lead to unexpected behavior in lower
287 * ELs that have not been updated since the introduction of
288 * this feature if not properly initialized, especially when
289 * it comes to those bits that enable/disable traps.
290 */
291 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HCRX_EL2,
292 HCRX_EL2_INIT_VAL);
293 }
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500294
295 if (is_feat_fgt_supported()) {
296 /*
297 * Initialize HFG*_EL2 registers with a default value so legacy
298 * systems unaware of FEAT_FGT do not get trapped due to their lack
299 * of initialization for this feature.
300 */
301 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGITR_EL2,
302 HFGITR_EL2_INIT_VAL);
303 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGRTR_EL2,
304 HFGRTR_EL2_INIT_VAL);
305 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGWTR_EL2,
306 HFGWTR_EL2_INIT_VAL);
307 }
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600308#endif /* CTX_INCLUDE_EL2_REGS */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000309
310 manage_extensions_nonsecure(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +0000311}
312
313/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600314 * The following function performs initialization of the cpu_context 'ctx'
315 * for first use that is common to all security states, and sets the
316 * initial entrypoint state as specified by the entry_point_info structure.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100317 *
Paul Beesley8aabea32019-01-11 18:26:51 +0000318 * The EE and ST attributes are used to configure the endianness and secure
Soby Mathew12d0d002015-04-09 13:40:55 +0100319 * timer availability for the new execution context.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100320 ******************************************************************************/
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600321static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke167a9352014-06-04 21:10:52 +0100322{
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000323 u_register_t scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100324 el3_state_t *state;
325 gp_regs_t *gp_regs;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100326
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100327 state = get_el3state_ctx(ctx);
328
Andrew Thoelke167a9352014-06-04 21:10:52 +0100329 /* Clear any residual register values from the context */
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000330 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke167a9352014-06-04 21:10:52 +0100331
332 /*
Boyan Karatotev5e8cc722023-05-23 12:04:00 +0100333 * The lower-EL context is zeroed so that no stale values leak to a world.
334 * It is assumed that an all-zero lower-EL context is good enough for it
335 * to boot correctly. However, there are very few registers where this
336 * is not true and some values need to be recreated.
337 */
338#if CTX_INCLUDE_EL2_REGS
339 el2_sysregs_t *el2_ctx = get_el2_sysregs_ctx(ctx);
340
341 /*
342 * These bits are set in the gicv3 driver. Losing them (especially the
343 * SRE bit) is problematic for all worlds. Henceforth recreate them.
344 */
345 u_register_t icc_sre_el2 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
346 ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
347 write_ctx_reg(el2_ctx, CTX_ICC_SRE_EL2, icc_sre_el2);
348#endif /* CTX_INCLUDE_EL2_REGS */
349
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +0100350 /* Start with a clean SCR_EL3 copy as all relevant values are set */
351 scr_el3 = SCR_RESET_VAL;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500352
David Cunado18f2efd2017-04-13 22:38:29 +0100353 /*
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100354 * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
355 * EL2, EL1 and EL0 are not trapped to EL3.
356 *
357 * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
358 * EL2, EL1 and EL0 are not trapped to EL3.
359 *
360 * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
361 * both Security states and both Execution states.
362 *
363 * SCR_EL3.SIF: Set to one to disable secure instruction execution from
364 * Non-secure memory.
365 */
366 scr_el3 &= ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT);
367
368 scr_el3 |= SCR_SIF_BIT;
369
370 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100371 * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
372 * Exception level as specified by SPSR.
373 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500374 if (GET_RW(ep->spsr) == MODE_RW_64) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100375 scr_el3 |= SCR_RW_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500376 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600377
David Cunado18f2efd2017-04-13 22:38:29 +0100378 /*
379 * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
Zelalem Awekeb515f542022-04-08 16:48:05 -0500380 * Secure timer registers to EL3, from AArch64 state only, if specified
381 * by the entrypoint attributes. If SEL2 is present and enabled, the ST
382 * bit always behaves as 1 (i.e. secure physical timer register access
383 * is not trapped)
David Cunado18f2efd2017-04-13 22:38:29 +0100384 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500385 if (EP_GET_ST(ep->h.attr) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100386 scr_el3 |= SCR_ST_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500387 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100388
johpow01cb4ec472021-08-04 19:38:18 -0500389 /*
390 * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
391 * SCR_EL3.HXEn.
392 */
Andre Przywarac5a3ebb2022-11-15 11:45:19 +0000393 if (is_feat_hcx_supported()) {
394 scr_el3 |= SCR_HXEn_BIT;
395 }
johpow01cb4ec472021-08-04 19:38:18 -0500396
Juan Pablo Condeff86e0b2022-07-12 16:40:29 -0400397 /*
398 * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
399 * registers are trapped to EL3.
400 */
401#if ENABLE_FEAT_RNG_TRAP
402 scr_el3 |= SCR_TRNDR_BIT;
403#endif
404
Jeenu Viswambharan1a7c1cf2017-12-08 12:13:51 +0000405#if FAULT_INJECTION_SUPPORT
406 /* Enable fault injection from lower ELs */
407 scr_el3 |= SCR_FIEN_BIT;
408#endif
409
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100410#if CTX_INCLUDE_PAUTH_REGS
411 /*
412 * Enable Pointer Authentication globally for all the worlds.
413 *
414 * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
415 * other than EL3
416 *
417 * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
418 * than EL3
419 */
420 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
421#endif /* CTX_INCLUDE_PAUTH_REGS */
422
Antonio Nino Diaz52839622019-01-31 11:58:00 +0000423 /*
Mark Brownd3331602023-03-14 20:13:03 +0000424 * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
425 */
426 if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
427 scr_el3 |= SCR_TCR2EN_BIT;
428 }
429
430 /*
Mark Brown062b6c62023-03-14 20:48:43 +0000431 * SCR_EL3.PIEN: Enable permission indirection and overlay
432 * registers for AArch64 if present.
433 */
434 if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
435 scr_el3 |= SCR_PIEN_BIT;
436 }
437
438 /*
Mark Brown688ab572023-03-14 21:33:04 +0000439 * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
440 */
441 if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
442 scr_el3 |= SCR_GCSEn_BIT;
443 }
444
445 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100446 * SCR_EL3.HCE: Enable HVC instructions if next execution state is
447 * AArch64 and next EL is EL2, or if next execution state is AArch32 and
448 * next mode is Hyp.
Jimmy Brisson110ee432020-04-16 10:47:56 -0500449 * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
450 * same conditions as HVC instructions and when the processor supports
451 * ARMv8.6-FGT.
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500452 * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
453 * CNTPOFF_EL2 register under the same conditions as HVC instructions
454 * and when the processor supports ECV.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100455 */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000456 if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
457 || ((GET_RW(ep->spsr) != MODE_RW_64)
458 && (GET_M32(ep->spsr) == MODE32_hyp))) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100459 scr_el3 |= SCR_HCE_BIT;
Jimmy Brisson110ee432020-04-16 10:47:56 -0500460
Andre Przywarace485952022-11-10 14:28:01 +0000461 if (is_feat_fgt_supported()) {
Jimmy Brisson110ee432020-04-16 10:47:56 -0500462 scr_el3 |= SCR_FGTEN_BIT;
463 }
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500464
Andre Przywarab8f03d22022-11-17 17:30:43 +0000465 if (is_feat_ecv_supported()) {
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500466 scr_el3 |= SCR_ECVEN_BIT;
467 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100468 }
469
johpow016cac7242020-04-22 14:05:13 -0500470 /* Enable WFE trap delay in SCR_EL3 if supported and configured */
Andre Przywara1223d2a2023-01-27 12:25:49 +0000471 if (is_feat_twed_supported()) {
472 /* Set delay in SCR_EL3 */
473 scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
474 scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
475 << SCR_TWEDEL_SHIFT);
johpow016cac7242020-04-22 14:05:13 -0500476
Andre Przywara1223d2a2023-01-27 12:25:49 +0000477 /* Enable WFE delay */
478 scr_el3 |= SCR_TWEDEn_BIT;
479 }
johpow016cac7242020-04-22 14:05:13 -0500480
Jayanth Dodderi Chidanand9f4b6252023-09-22 15:30:13 +0100481#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
482 /* Enable S-EL2 if FEAT_SEL2 is implemented for all the contexts. */
483 if (is_feat_sel2_supported()) {
484 scr_el3 |= SCR_EEL2_BIT;
485 }
486#endif /* (IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2) */
487
Arvind Ram Prakashedebefb2023-10-11 12:10:56 -0500488 if (is_feat_mpam_supported()) {
489 write_ctx_reg(get_el3state_ctx(ctx), CTX_MPAM3_EL3, \
490 MPAM3_EL3_RESET_VAL);
491 }
492
David Cunado18f2efd2017-04-13 22:38:29 +0100493 /*
Alexei Fedorove290a8f2019-08-13 15:17:53 +0100494 * Populate EL3 state so that we've the right context
495 * before doing ERET
496 */
Andrew Thoelke167a9352014-06-04 21:10:52 +0100497 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
498 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
499 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
500
501 /*
502 * Store the X0-X7 value from the entrypoint into the context
503 * Use memcpy as we are in control of the layout of the structures
504 */
505 gp_regs = get_gpregs_ctx(ctx);
506 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
507}
508
509/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600510 * Context management library initialization routine. This library is used by
511 * runtime services to share pointers to 'cpu_context' structures for secure
512 * non-secure and realm states. Management of the structures and their associated
513 * memory is not done by the context management library e.g. the PSCI service
514 * manages the cpu context used for entry from and exit to the non-secure state.
515 * The Secure payload dispatcher service manages the context(s) corresponding to
516 * the secure state. It also uses this library to get access to the non-secure
517 * state cpu context pointers.
518 * Lastly, this library provides the API to make SP_EL3 point to the cpu context
519 * which will be used for programming an entry into a lower EL. The same context
520 * will be used to save state upon exception entry from that EL.
521 ******************************************************************************/
522void __init cm_init(void)
523{
524 /*
Elyes Haouas1b491ee2023-02-13 09:14:48 +0100525 * The context management library has only global data to initialize, but
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600526 * that will be done when the BSS is zeroed out.
527 */
528}
529
530/*******************************************************************************
531 * This is the high-level function used to initialize the cpu_context 'ctx' for
532 * first use. It performs initializations that are common to all security states
533 * and initializations specific to the security state specified in 'ep'
534 ******************************************************************************/
535void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
536{
537 unsigned int security_state;
538
539 assert(ctx != NULL);
540
541 /*
542 * Perform initializations that are common
543 * to all security states
544 */
545 setup_context_common(ctx, ep);
546
547 security_state = GET_SECURITY_STATE(ep->h.attr);
548
549 /* Perform security state specific initializations */
550 switch (security_state) {
551 case SECURE:
552 setup_secure_context(ctx, ep);
553 break;
554#if ENABLE_RME
555 case REALM:
556 setup_realm_context(ctx, ep);
557 break;
558#endif
559 case NON_SECURE:
560 setup_ns_context(ctx, ep);
561 break;
562 default:
563 ERROR("Invalid security state\n");
564 panic();
565 break;
566 }
567}
568
569/*******************************************************************************
Boyan Karatotev24a70732023-03-08 11:56:49 +0000570 * Enable architecture extensions for EL3 execution. This function only updates
571 * registers in-place which are expected to either never change or be
572 * overwritten by el3_exit.
573 ******************************************************************************/
574#if IMAGE_BL31
575void cm_manage_extensions_el3(void)
576{
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000577 if (is_feat_spe_supported()) {
578 spe_init_el3();
579 }
580
Boyan Karatotev4085a022023-03-27 17:02:43 +0100581 if (is_feat_amu_supported()) {
582 amu_init_el3();
583 }
584
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000585 if (is_feat_sme_supported()) {
586 sme_init_el3();
587 }
588
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000589 if (is_feat_trbe_supported()) {
590 trbe_init_el3();
591 }
592
593 if (is_feat_brbe_supported()) {
594 brbe_init_el3();
595 }
596
597 if (is_feat_trf_supported()) {
598 trf_init_el3();
599 }
600
601 pmuv3_init_el3();
Boyan Karatotev24a70732023-03-08 11:56:49 +0000602}
603#endif /* IMAGE_BL31 */
604
605/*******************************************************************************
Elizabeth Ho461c0a52023-07-18 14:10:25 +0100606 * Initialise per_world_context for Non-Secure world.
607 * This function enables the architecture extensions, which have same value
608 * across the cores for the non-secure world.
609 ******************************************************************************/
610#if IMAGE_BL31
611void manage_extensions_nonsecure_per_world(void)
612{
613 if (is_feat_sme_supported()) {
614 sme_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
615 }
616
617 if (is_feat_sve_supported()) {
618 sve_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
619 }
620
621 if (is_feat_amu_supported()) {
622 amu_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
623 }
624
625 if (is_feat_sys_reg_trace_supported()) {
626 sys_reg_trace_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
627 }
628}
629#endif /* IMAGE_BL31 */
630
631/*******************************************************************************
632 * Initialise per_world_context for Secure world.
633 * This function enables the architecture extensions, which have same value
634 * across the cores for the secure world.
635 ******************************************************************************/
636
637static void manage_extensions_secure_per_world(void)
638{
639#if IMAGE_BL31
640 if (is_feat_sme_supported()) {
641
642 if (ENABLE_SME_FOR_SWD) {
643 /*
644 * Enable SME, SVE, FPU/SIMD in secure context, SPM must ensure
645 * SME, SVE, and FPU/SIMD context properly managed.
646 */
647 sme_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
648 } else {
649 /*
650 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
651 * world can safely use the associated registers.
652 */
653 sme_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
654 }
655 }
656 if (is_feat_sve_supported()) {
657 if (ENABLE_SVE_FOR_SWD) {
658 /*
659 * Enable SVE and FPU in secure context, SPM must ensure
660 * that the SVE and FPU register contexts are properly managed.
661 */
662 sve_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
663 } else {
664 /*
665 * Disable SVE and FPU in secure context so non-secure world
666 * can safely use them.
667 */
668 sve_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
669 }
670 }
671
672 /* NS can access this but Secure shouldn't */
673 if (is_feat_sys_reg_trace_supported()) {
674 sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
675 }
676
677 has_secure_perworld_init = true;
678#endif /* IMAGE_BL31 */
679}
680
681/*******************************************************************************
Boyan Karatotev24a70732023-03-08 11:56:49 +0000682 * Enable architecture extensions on first entry to Non-secure world.
683 ******************************************************************************/
684static void manage_extensions_nonsecure(cpu_context_t *ctx)
685{
686#if IMAGE_BL31
Boyan Karatotev4085a022023-03-27 17:02:43 +0100687 if (is_feat_amu_supported()) {
688 amu_enable(ctx);
689 }
690
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000691 if (is_feat_sme_supported()) {
692 sme_enable(ctx);
693 }
694
Arvind Ram Prakashedebefb2023-10-11 12:10:56 -0500695 if (is_feat_mpam_supported()) {
696 mpam_enable(ctx);
697 }
Boyan Karatotevc73686a2023-02-15 13:21:50 +0000698 pmuv3_enable(ctx);
Boyan Karatotev24a70732023-03-08 11:56:49 +0000699#endif /* IMAGE_BL31 */
700}
701
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000702/* TODO: move to lib/extensions/pauth when it has been ported to FEAT_STATE */
703static __unused void enable_pauth_el2(void)
704{
705 u_register_t hcr_el2 = read_hcr_el2();
706 /*
707 * For Armv8.3 pointer authentication feature, disable traps to EL2 when
708 * accessing key registers or using pointer authentication instructions
709 * from lower ELs.
710 */
711 hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
712
713 write_hcr_el2(hcr_el2);
714}
715
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500716#if INIT_UNUSED_NS_EL2
Boyan Karatotev24a70732023-03-08 11:56:49 +0000717/*******************************************************************************
718 * Enable architecture extensions in-place at EL2 on first entry to Non-secure
719 * world when EL2 is empty and unused.
720 ******************************************************************************/
721static void manage_extensions_nonsecure_el2_unused(void)
722{
723#if IMAGE_BL31
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000724 if (is_feat_spe_supported()) {
725 spe_init_el2_unused();
726 }
727
Boyan Karatotev4085a022023-03-27 17:02:43 +0100728 if (is_feat_amu_supported()) {
729 amu_init_el2_unused();
730 }
731
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000732 if (is_feat_mpam_supported()) {
733 mpam_init_el2_unused();
734 }
735
736 if (is_feat_trbe_supported()) {
737 trbe_init_el2_unused();
738 }
739
740 if (is_feat_sys_reg_trace_supported()) {
741 sys_reg_trace_init_el2_unused();
742 }
743
744 if (is_feat_trf_supported()) {
745 trf_init_el2_unused();
746 }
747
Boyan Karatotevc73686a2023-02-15 13:21:50 +0000748 pmuv3_init_el2_unused();
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000749
750 if (is_feat_sve_supported()) {
751 sve_init_el2_unused();
752 }
753
754 if (is_feat_sme_supported()) {
755 sme_init_el2_unused();
756 }
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000757
758#if ENABLE_PAUTH
759 enable_pauth_el2();
760#endif /* ENABLE_PAUTH */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000761#endif /* IMAGE_BL31 */
762}
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500763#endif /* INIT_UNUSED_NS_EL2 */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000764
765/*******************************************************************************
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100766 * Enable architecture extensions on first entry to Secure world.
767 ******************************************************************************/
johpow01dc78e622021-07-08 14:14:00 -0500768static void manage_extensions_secure(cpu_context_t *ctx)
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100769{
770#if IMAGE_BL31
Boyan Karatotev0d122942023-03-08 16:29:26 +0000771 if (is_feat_sme_supported()) {
772 if (ENABLE_SME_FOR_SWD) {
773 /*
774 * Enable SME, SVE, FPU/SIMD in secure context, secure manager
775 * must ensure SME, SVE, and FPU/SIMD context properly managed.
776 */
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000777 sme_init_el3();
Boyan Karatotev0d122942023-03-08 16:29:26 +0000778 sme_enable(ctx);
779 } else {
780 /*
781 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
782 * world can safely use the associated registers.
783 */
784 sme_disable(ctx);
785 }
786 }
johpow01dc78e622021-07-08 14:14:00 -0500787#endif /* IMAGE_BL31 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100788}
789
790/*******************************************************************************
Soby Mathew12d0d002015-04-09 13:40:55 +0100791 * The following function initializes the cpu_context for a CPU specified by
792 * its `cpu_idx` for first use, and sets the initial entrypoint state as
793 * specified by the entry_point_info structure.
794 ******************************************************************************/
795void cm_init_context_by_index(unsigned int cpu_idx,
796 const entry_point_info_t *ep)
797{
798 cpu_context_t *ctx;
799 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100800 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100801}
802
803/*******************************************************************************
804 * The following function initializes the cpu_context for the current CPU
805 * for first use, and sets the initial entrypoint state as specified by the
806 * entry_point_info structure.
807 ******************************************************************************/
808void cm_init_my_context(const entry_point_info_t *ep)
809{
810 cpu_context_t *ctx;
811 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100812 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100813}
814
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000815/* EL2 present but unused, need to disable safely. SCTLR_EL2 can be ignored */
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500816static void init_nonsecure_el2_unused(cpu_context_t *ctx)
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000817{
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500818#if INIT_UNUSED_NS_EL2
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000819 u_register_t hcr_el2 = HCR_RESET_VAL;
820 u_register_t mdcr_el2;
821 u_register_t scr_el3;
822
823 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
824
825 /* Set EL2 register width: Set HCR_EL2.RW to match SCR_EL3.RW */
826 if ((scr_el3 & SCR_RW_BIT) != 0U) {
827 hcr_el2 |= HCR_RW_BIT;
828 }
829
830 write_hcr_el2(hcr_el2);
831
832 /*
833 * Initialise CPTR_EL2 setting all fields rather than relying on the hw.
834 * All fields have architecturally UNKNOWN reset values.
835 */
836 write_cptr_el2(CPTR_EL2_RESET_VAL);
837
838 /*
839 * Initialise CNTHCTL_EL2. All fields are architecturally UNKNOWN on
840 * reset and are set to zero except for field(s) listed below.
841 *
842 * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to Hyp mode of
843 * Non-secure EL0 and EL1 accesses to the physical timer registers.
844 *
845 * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to Hyp mode of
846 * Non-secure EL0 and EL1 accesses to the physical counter registers.
847 */
848 write_cnthctl_el2(CNTHCTL_RESET_VAL | EL1PCEN_BIT | EL1PCTEN_BIT);
849
850 /*
851 * Initialise CNTVOFF_EL2 to zero as it resets to an architecturally
852 * UNKNOWN value.
853 */
854 write_cntvoff_el2(0);
855
856 /*
857 * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and MPIDR_EL1
858 * respectively.
859 */
860 write_vpidr_el2(read_midr_el1());
861 write_vmpidr_el2(read_mpidr_el1());
862
863 /*
864 * Initialise VTTBR_EL2. All fields are architecturally UNKNOWN on reset.
865 *
866 * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage 2 address
867 * translation is disabled, cache maintenance operations depend on the
868 * VMID.
869 *
870 * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address translation is
871 * disabled.
872 */
873 write_vttbr_el2(VTTBR_RESET_VAL &
874 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
875 (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
876
877 /*
878 * Initialise MDCR_EL2, setting all fields rather than relying on hw.
879 * Some fields are architecturally UNKNOWN on reset.
880 *
881 * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and EL1 System
882 * register accesses to the Debug ROM registers are not trapped to EL2.
883 *
884 * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1 System register
885 * accesses to the powerdown debug registers are not trapped to EL2.
886 *
887 * MDCR_EL2.TDA: Set to zero so that System register accesses to the
888 * debug registers do not trap to EL2.
889 *
890 * MDCR_EL2.TDE: Set to zero so that debug exceptions are not routed to
891 * EL2.
892 */
893 mdcr_el2 = MDCR_EL2_RESET_VAL &
894 ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT | MDCR_EL2_TDA_BIT |
895 MDCR_EL2_TDE_BIT);
896
897 write_mdcr_el2(mdcr_el2);
898
899 /*
900 * Initialise HSTR_EL2. All fields are architecturally UNKNOWN on reset.
901 *
902 * HSTR_EL2.T<n>: Set all these fields to zero so that Non-secure EL0 or
903 * EL1 accesses to System registers do not trap to EL2.
904 */
905 write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
906
907 /*
908 * Initialise CNTHP_CTL_EL2. All fields are architecturally UNKNOWN on
909 * reset.
910 *
911 * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2 physical timer
912 * and prevent timer interrupts.
913 */
914 write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT));
915
916 manage_extensions_nonsecure_el2_unused();
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500917#endif /* INIT_UNUSED_NS_EL2 */
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000918}
919
Soby Mathew12d0d002015-04-09 13:40:55 +0100920/*******************************************************************************
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500921 * Prepare the CPU system registers for first entry into realm, secure, or
922 * normal world.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100923 *
924 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
925 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
926 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
927 * For all entries, the EL1 registers are initialized from the cpu_context
928 ******************************************************************************/
929void cm_prepare_el3_exit(uint32_t security_state)
930{
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000931 u_register_t sctlr_elx, scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100932 cpu_context_t *ctx = cm_get_context(security_state);
933
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000934 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100935
936 if (security_state == NON_SECURE) {
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600937 uint64_t el2_implemented = el_implemented(2);
938
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000939 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000940 CTX_SCR_EL3);
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600941
942 if (((scr_el3 & SCR_HCE_BIT) != 0U)
943 || (el2_implemented != EL_IMPL_NONE)) {
944 /*
945 * If context is not being used for EL2, initialize
946 * HCRX_EL2 with its init value here.
947 */
948 if (is_feat_hcx_supported()) {
949 write_hcrx_el2(HCRX_EL2_INIT_VAL);
950 }
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500951
952 /*
953 * Initialize Fine-grained trap registers introduced
954 * by FEAT_FGT so all traps are initially disabled when
955 * switching to EL2 or a lower EL, preventing undesired
956 * behavior.
957 */
958 if (is_feat_fgt_supported()) {
959 /*
960 * Initialize HFG*_EL2 registers with a default
961 * value so legacy systems unaware of FEAT_FGT
962 * do not get trapped due to their lack of
963 * initialization for this feature.
964 */
965 write_hfgitr_el2(HFGITR_EL2_INIT_VAL);
966 write_hfgrtr_el2(HFGRTR_EL2_INIT_VAL);
967 write_hfgwtr_el2(HFGWTR_EL2_INIT_VAL);
968 }
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600969 }
970
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500971
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000972 if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100973 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
Max Shvetsov28259462020-02-17 16:15:47 +0000974 sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000975 CTX_SCTLR_EL1);
Ken Kuang2e09d4f2017-08-23 16:03:29 +0800976 sctlr_elx &= SCTLR_EE_BIT;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100977 sctlr_elx |= SCTLR_EL2_RES1;
Louis Mayencourt5f5d1ed2019-02-20 12:11:41 +0000978#if ERRATA_A75_764081
979 /*
980 * If workaround of errata 764081 for Cortex-A75 is used
981 * then set SCTLR_EL2.IESB to enable Implicit Error
982 * Synchronization Barrier.
983 */
984 sctlr_elx |= SCTLR_IESB_BIT;
985#endif
Andrew Thoelke167a9352014-06-04 21:10:52 +0100986 write_sctlr_el2(sctlr_elx);
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600987 } else if (el2_implemented != EL_IMPL_NONE) {
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000988 init_nonsecure_el2_unused(ctx);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100989 }
990 }
991
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +0100992 cm_el1_sysregs_context_restore(security_state);
993 cm_set_next_eret_context(security_state);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100994}
995
Max Shvetsov28f39f02020-02-25 13:56:19 +0000996#if CTX_INCLUDE_EL2_REGS
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000997
998static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
999{
Andre Przywarade8c4892023-02-15 15:56:15 +00001000 write_ctx_reg(ctx, CTX_HDFGRTR_EL2, read_hdfgrtr_el2());
1001 if (is_feat_amu_supported()) {
1002 write_ctx_reg(ctx, CTX_HAFGRTR_EL2, read_hafgrtr_el2());
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001003 }
Andre Przywarade8c4892023-02-15 15:56:15 +00001004 write_ctx_reg(ctx, CTX_HDFGWTR_EL2, read_hdfgwtr_el2());
1005 write_ctx_reg(ctx, CTX_HFGITR_EL2, read_hfgitr_el2());
1006 write_ctx_reg(ctx, CTX_HFGRTR_EL2, read_hfgrtr_el2());
1007 write_ctx_reg(ctx, CTX_HFGWTR_EL2, read_hfgwtr_el2());
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001008}
1009
1010static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
1011{
Andre Przywarade8c4892023-02-15 15:56:15 +00001012 write_hdfgrtr_el2(read_ctx_reg(ctx, CTX_HDFGRTR_EL2));
1013 if (is_feat_amu_supported()) {
1014 write_hafgrtr_el2(read_ctx_reg(ctx, CTX_HAFGRTR_EL2));
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001015 }
Andre Przywarade8c4892023-02-15 15:56:15 +00001016 write_hdfgwtr_el2(read_ctx_reg(ctx, CTX_HDFGWTR_EL2));
1017 write_hfgitr_el2(read_ctx_reg(ctx, CTX_HFGITR_EL2));
1018 write_hfgrtr_el2(read_ctx_reg(ctx, CTX_HFGRTR_EL2));
1019 write_hfgwtr_el2(read_ctx_reg(ctx, CTX_HFGWTR_EL2));
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001020}
1021
Andre Przywara9448f2b2022-11-17 16:42:09 +00001022static void el2_sysregs_context_save_mpam(el2_sysregs_t *ctx)
1023{
1024 u_register_t mpam_idr = read_mpamidr_el1();
1025
1026 write_ctx_reg(ctx, CTX_MPAM2_EL2, read_mpam2_el2());
1027
1028 /*
1029 * The context registers that we intend to save would be part of the
1030 * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
1031 */
1032 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
1033 return;
1034 }
1035
1036 /*
1037 * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
1038 * MPAMIDR_HAS_HCR_BIT == 1.
1039 */
1040 write_ctx_reg(ctx, CTX_MPAMHCR_EL2, read_mpamhcr_el2());
1041 write_ctx_reg(ctx, CTX_MPAMVPM0_EL2, read_mpamvpm0_el2());
1042 write_ctx_reg(ctx, CTX_MPAMVPMV_EL2, read_mpamvpmv_el2());
1043
1044 /*
1045 * The number of MPAMVPM registers is implementation defined, their
1046 * number is stored in the MPAMIDR_EL1 register.
1047 */
1048 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
1049 case 7:
1050 write_ctx_reg(ctx, CTX_MPAMVPM7_EL2, read_mpamvpm7_el2());
1051 __fallthrough;
1052 case 6:
1053 write_ctx_reg(ctx, CTX_MPAMVPM6_EL2, read_mpamvpm6_el2());
1054 __fallthrough;
1055 case 5:
1056 write_ctx_reg(ctx, CTX_MPAMVPM5_EL2, read_mpamvpm5_el2());
1057 __fallthrough;
1058 case 4:
1059 write_ctx_reg(ctx, CTX_MPAMVPM4_EL2, read_mpamvpm4_el2());
1060 __fallthrough;
1061 case 3:
1062 write_ctx_reg(ctx, CTX_MPAMVPM3_EL2, read_mpamvpm3_el2());
1063 __fallthrough;
1064 case 2:
1065 write_ctx_reg(ctx, CTX_MPAMVPM2_EL2, read_mpamvpm2_el2());
1066 __fallthrough;
1067 case 1:
1068 write_ctx_reg(ctx, CTX_MPAMVPM1_EL2, read_mpamvpm1_el2());
1069 break;
1070 }
1071}
1072
1073static void el2_sysregs_context_restore_mpam(el2_sysregs_t *ctx)
1074{
1075 u_register_t mpam_idr = read_mpamidr_el1();
1076
1077 write_mpam2_el2(read_ctx_reg(ctx, CTX_MPAM2_EL2));
1078
1079 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
1080 return;
1081 }
1082
1083 write_mpamhcr_el2(read_ctx_reg(ctx, CTX_MPAMHCR_EL2));
1084 write_mpamvpm0_el2(read_ctx_reg(ctx, CTX_MPAMVPM0_EL2));
1085 write_mpamvpmv_el2(read_ctx_reg(ctx, CTX_MPAMVPMV_EL2));
1086
1087 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
1088 case 7:
1089 write_mpamvpm7_el2(read_ctx_reg(ctx, CTX_MPAMVPM7_EL2));
1090 __fallthrough;
1091 case 6:
1092 write_mpamvpm6_el2(read_ctx_reg(ctx, CTX_MPAMVPM6_EL2));
1093 __fallthrough;
1094 case 5:
1095 write_mpamvpm5_el2(read_ctx_reg(ctx, CTX_MPAMVPM5_EL2));
1096 __fallthrough;
1097 case 4:
1098 write_mpamvpm4_el2(read_ctx_reg(ctx, CTX_MPAMVPM4_EL2));
1099 __fallthrough;
1100 case 3:
1101 write_mpamvpm3_el2(read_ctx_reg(ctx, CTX_MPAMVPM3_EL2));
1102 __fallthrough;
1103 case 2:
1104 write_mpamvpm2_el2(read_ctx_reg(ctx, CTX_MPAMVPM2_EL2));
1105 __fallthrough;
1106 case 1:
1107 write_mpamvpm1_el2(read_ctx_reg(ctx, CTX_MPAMVPM1_EL2));
1108 break;
1109 }
1110}
1111
Boyan Karatotevac58e572023-05-15 15:09:16 +01001112/* -----------------------------------------------------
1113 * The following registers are not added:
1114 * AMEVCNTVOFF0<n>_EL2
1115 * AMEVCNTVOFF1<n>_EL2
1116 * ICH_AP0R<n>_EL2
1117 * ICH_AP1R<n>_EL2
1118 * ICH_LR<n>_EL2
1119 * -----------------------------------------------------
1120 */
1121static void el2_sysregs_context_save_common(el2_sysregs_t *ctx)
1122{
1123 write_ctx_reg(ctx, CTX_ACTLR_EL2, read_actlr_el2());
1124 write_ctx_reg(ctx, CTX_AFSR0_EL2, read_afsr0_el2());
1125 write_ctx_reg(ctx, CTX_AFSR1_EL2, read_afsr1_el2());
1126 write_ctx_reg(ctx, CTX_AMAIR_EL2, read_amair_el2());
1127 write_ctx_reg(ctx, CTX_CNTHCTL_EL2, read_cnthctl_el2());
1128 write_ctx_reg(ctx, CTX_CNTVOFF_EL2, read_cntvoff_el2());
1129 write_ctx_reg(ctx, CTX_CPTR_EL2, read_cptr_el2());
1130 if (CTX_INCLUDE_AARCH32_REGS) {
1131 write_ctx_reg(ctx, CTX_DBGVCR32_EL2, read_dbgvcr32_el2());
1132 }
1133 write_ctx_reg(ctx, CTX_ELR_EL2, read_elr_el2());
1134 write_ctx_reg(ctx, CTX_ESR_EL2, read_esr_el2());
1135 write_ctx_reg(ctx, CTX_FAR_EL2, read_far_el2());
1136 write_ctx_reg(ctx, CTX_HACR_EL2, read_hacr_el2());
1137 write_ctx_reg(ctx, CTX_HCR_EL2, read_hcr_el2());
1138 write_ctx_reg(ctx, CTX_HPFAR_EL2, read_hpfar_el2());
1139 write_ctx_reg(ctx, CTX_HSTR_EL2, read_hstr_el2());
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001140
1141 /*
1142 * Set the NS bit to be able to access the ICC_SRE_EL2 register
1143 * TODO: remove with root context
1144 */
1145 u_register_t scr_el3 = read_scr_el3();
1146
1147 write_scr_el3(scr_el3 | SCR_NS_BIT);
1148 isb();
Boyan Karatotevac58e572023-05-15 15:09:16 +01001149 write_ctx_reg(ctx, CTX_ICC_SRE_EL2, read_icc_sre_el2());
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001150
1151 write_scr_el3(scr_el3);
1152 isb();
1153
Boyan Karatotevac58e572023-05-15 15:09:16 +01001154 write_ctx_reg(ctx, CTX_ICH_HCR_EL2, read_ich_hcr_el2());
1155 write_ctx_reg(ctx, CTX_ICH_VMCR_EL2, read_ich_vmcr_el2());
1156 write_ctx_reg(ctx, CTX_MAIR_EL2, read_mair_el2());
1157 write_ctx_reg(ctx, CTX_MDCR_EL2, read_mdcr_el2());
1158 write_ctx_reg(ctx, CTX_SCTLR_EL2, read_sctlr_el2());
1159 write_ctx_reg(ctx, CTX_SPSR_EL2, read_spsr_el2());
1160 write_ctx_reg(ctx, CTX_SP_EL2, read_sp_el2());
1161 write_ctx_reg(ctx, CTX_TCR_EL2, read_tcr_el2());
1162 write_ctx_reg(ctx, CTX_TPIDR_EL2, read_tpidr_el2());
1163 write_ctx_reg(ctx, CTX_TTBR0_EL2, read_ttbr0_el2());
1164 write_ctx_reg(ctx, CTX_VBAR_EL2, read_vbar_el2());
1165 write_ctx_reg(ctx, CTX_VMPIDR_EL2, read_vmpidr_el2());
1166 write_ctx_reg(ctx, CTX_VPIDR_EL2, read_vpidr_el2());
1167 write_ctx_reg(ctx, CTX_VTCR_EL2, read_vtcr_el2());
1168 write_ctx_reg(ctx, CTX_VTTBR_EL2, read_vttbr_el2());
1169}
1170
1171static void el2_sysregs_context_restore_common(el2_sysregs_t *ctx)
1172{
1173 write_actlr_el2(read_ctx_reg(ctx, CTX_ACTLR_EL2));
1174 write_afsr0_el2(read_ctx_reg(ctx, CTX_AFSR0_EL2));
1175 write_afsr1_el2(read_ctx_reg(ctx, CTX_AFSR1_EL2));
1176 write_amair_el2(read_ctx_reg(ctx, CTX_AMAIR_EL2));
1177 write_cnthctl_el2(read_ctx_reg(ctx, CTX_CNTHCTL_EL2));
1178 write_cntvoff_el2(read_ctx_reg(ctx, CTX_CNTVOFF_EL2));
1179 write_cptr_el2(read_ctx_reg(ctx, CTX_CPTR_EL2));
1180 if (CTX_INCLUDE_AARCH32_REGS) {
1181 write_dbgvcr32_el2(read_ctx_reg(ctx, CTX_DBGVCR32_EL2));
1182 }
1183 write_elr_el2(read_ctx_reg(ctx, CTX_ELR_EL2));
1184 write_esr_el2(read_ctx_reg(ctx, CTX_ESR_EL2));
1185 write_far_el2(read_ctx_reg(ctx, CTX_FAR_EL2));
1186 write_hacr_el2(read_ctx_reg(ctx, CTX_HACR_EL2));
1187 write_hcr_el2(read_ctx_reg(ctx, CTX_HCR_EL2));
1188 write_hpfar_el2(read_ctx_reg(ctx, CTX_HPFAR_EL2));
1189 write_hstr_el2(read_ctx_reg(ctx, CTX_HSTR_EL2));
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001190
1191 /*
1192 * Set the NS bit to be able to access the ICC_SRE_EL2 register
1193 * TODO: remove with root context
1194 */
1195 u_register_t scr_el3 = read_scr_el3();
1196
1197 write_scr_el3(scr_el3 | SCR_NS_BIT);
1198 isb();
Boyan Karatotevac58e572023-05-15 15:09:16 +01001199 write_icc_sre_el2(read_ctx_reg(ctx, CTX_ICC_SRE_EL2));
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001200
1201 write_scr_el3(scr_el3);
1202 isb();
1203
Boyan Karatotevac58e572023-05-15 15:09:16 +01001204 write_ich_hcr_el2(read_ctx_reg(ctx, CTX_ICH_HCR_EL2));
1205 write_ich_vmcr_el2(read_ctx_reg(ctx, CTX_ICH_VMCR_EL2));
1206 write_mair_el2(read_ctx_reg(ctx, CTX_MAIR_EL2));
1207 write_mdcr_el2(read_ctx_reg(ctx, CTX_MDCR_EL2));
1208 write_sctlr_el2(read_ctx_reg(ctx, CTX_SCTLR_EL2));
1209 write_spsr_el2(read_ctx_reg(ctx, CTX_SPSR_EL2));
1210 write_sp_el2(read_ctx_reg(ctx, CTX_SP_EL2));
1211 write_tcr_el2(read_ctx_reg(ctx, CTX_TCR_EL2));
1212 write_tpidr_el2(read_ctx_reg(ctx, CTX_TPIDR_EL2));
1213 write_ttbr0_el2(read_ctx_reg(ctx, CTX_TTBR0_EL2));
1214 write_vbar_el2(read_ctx_reg(ctx, CTX_VBAR_EL2));
1215 write_vmpidr_el2(read_ctx_reg(ctx, CTX_VMPIDR_EL2));
1216 write_vpidr_el2(read_ctx_reg(ctx, CTX_VPIDR_EL2));
1217 write_vtcr_el2(read_ctx_reg(ctx, CTX_VTCR_EL2));
1218 write_vttbr_el2(read_ctx_reg(ctx, CTX_VTTBR_EL2));
1219}
1220
Max Shvetsov28f39f02020-02-25 13:56:19 +00001221/*******************************************************************************
1222 * Save EL2 sysreg context
1223 ******************************************************************************/
1224void cm_el2_sysregs_context_save(uint32_t security_state)
1225{
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001226 cpu_context_t *ctx;
1227 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +00001228
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001229 ctx = cm_get_context(security_state);
1230 assert(ctx != NULL);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001231
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001232 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001233
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001234 el2_sysregs_context_save_common(el2_sysregs_ctx);
Zelalem Aweked20052f2022-04-04 17:42:48 -05001235#if CTX_INCLUDE_MTE_REGS
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001236 write_ctx_reg(el2_sysregs_ctx, CTX_TFSR_EL2, read_tfsr_el2());
Zelalem Aweked20052f2022-04-04 17:42:48 -05001237#endif
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001238 if (is_feat_mpam_supported()) {
1239 el2_sysregs_context_save_mpam(el2_sysregs_ctx);
1240 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001241
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001242 if (is_feat_fgt_supported()) {
1243 el2_sysregs_context_save_fgt(el2_sysregs_ctx);
1244 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001245
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001246 if (is_feat_ecv_v2_supported()) {
1247 write_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2, read_cntpoff_el2());
1248 }
Andre Przywarab8f03d22022-11-17 17:30:43 +00001249
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001250 if (is_feat_vhe_supported()) {
1251 write_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2, read_contextidr_el2());
1252 write_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2, read_ttbr1_el2());
1253 }
Andre Przywara6503ff22023-01-27 12:25:49 +00001254
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001255 if (is_feat_ras_supported()) {
1256 write_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2, read_vdisr_el2());
1257 write_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2, read_vsesr_el2());
1258 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001259
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001260 if (is_feat_nv2_supported()) {
1261 write_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2, read_vncr_el2());
1262 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001263
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001264 if (is_feat_trf_supported()) {
1265 write_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2, read_trfcr_el2());
1266 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001267
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001268 if (is_feat_csv2_2_supported()) {
1269 write_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2, read_scxtnum_el2());
1270 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001271
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001272 if (is_feat_hcx_supported()) {
1273 write_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2, read_hcrx_el2());
1274 }
1275 if (is_feat_tcr2_supported()) {
1276 write_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2, read_tcr2_el2());
1277 }
1278 if (is_feat_sxpie_supported()) {
1279 write_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2, read_pire0_el2());
1280 write_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2, read_pir_el2());
1281 }
1282 if (is_feat_s2pie_supported()) {
1283 write_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2, read_s2pir_el2());
1284 }
1285 if (is_feat_sxpoe_supported()) {
1286 write_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2, read_por_el2());
1287 }
1288 if (is_feat_gcs_supported()) {
1289 write_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2, read_gcspr_el2());
1290 write_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2, read_gcscr_el2());
Max Shvetsov28f39f02020-02-25 13:56:19 +00001291 }
1292}
1293
1294/*******************************************************************************
1295 * Restore EL2 sysreg context
1296 ******************************************************************************/
1297void cm_el2_sysregs_context_restore(uint32_t security_state)
1298{
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001299 cpu_context_t *ctx;
1300 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +00001301
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001302 ctx = cm_get_context(security_state);
1303 assert(ctx != NULL);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001304
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001305 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001306
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001307 el2_sysregs_context_restore_common(el2_sysregs_ctx);
Zelalem Aweked20052f2022-04-04 17:42:48 -05001308#if CTX_INCLUDE_MTE_REGS
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001309 write_tfsr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TFSR_EL2));
Zelalem Aweked20052f2022-04-04 17:42:48 -05001310#endif
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001311 if (is_feat_mpam_supported()) {
1312 el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
1313 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001314
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001315 if (is_feat_fgt_supported()) {
1316 el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
1317 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001318
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001319 if (is_feat_ecv_v2_supported()) {
1320 write_cntpoff_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2));
1321 }
Andre Przywarab8f03d22022-11-17 17:30:43 +00001322
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001323 if (is_feat_vhe_supported()) {
1324 write_contextidr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2));
1325 write_ttbr1_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2));
1326 }
Andre Przywara6503ff22023-01-27 12:25:49 +00001327
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001328 if (is_feat_ras_supported()) {
1329 write_vdisr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2));
1330 write_vsesr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2));
1331 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001332
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001333 if (is_feat_nv2_supported()) {
1334 write_vncr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2));
1335 }
1336 if (is_feat_trf_supported()) {
1337 write_trfcr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2));
1338 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001339
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001340 if (is_feat_csv2_2_supported()) {
1341 write_scxtnum_el2(read_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2));
1342 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001343
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001344 if (is_feat_hcx_supported()) {
1345 write_hcrx_el2(read_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2));
1346 }
1347 if (is_feat_tcr2_supported()) {
1348 write_tcr2_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2));
1349 }
1350 if (is_feat_sxpie_supported()) {
1351 write_pire0_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2));
1352 write_pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2));
1353 }
1354 if (is_feat_s2pie_supported()) {
1355 write_s2pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2));
1356 }
1357 if (is_feat_sxpoe_supported()) {
1358 write_por_el2(read_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2));
1359 }
1360 if (is_feat_gcs_supported()) {
1361 write_gcscr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2));
1362 write_gcspr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2));
Max Shvetsov28f39f02020-02-25 13:56:19 +00001363 }
1364}
1365#endif /* CTX_INCLUDE_EL2_REGS */
1366
Andrew Thoelke167a9352014-06-04 21:10:52 +01001367/*******************************************************************************
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001368 * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
1369 * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
1370 * updating EL1 and EL2 registers. Otherwise, it calls the generic
1371 * cm_prepare_el3_exit function.
1372 ******************************************************************************/
1373void cm_prepare_el3_exit_ns(void)
1374{
Arvind Ram Prakashc4c6eae2024-08-05 16:11:42 -05001375#ifdef IMAGE_BL31
1376#if ERRATA_A520_2938996 || ERRATA_X4_2726228
1377 cpu_context_t *trbe_ctx = cm_get_context(NON_SECURE);
1378
1379 assert(trbe_ctx != NULL);
1380 if (check_if_affected_core() == ERRATA_APPLIES) {
1381 if (is_feat_trbe_supported()) {
1382 trbe_disable(ctx);
1383 }
1384 }
1385#endif
1386#endif /* IMAGE_BL31 */
1387
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001388#if CTX_INCLUDE_EL2_REGS
Boyan Karatotev4085a022023-03-27 17:02:43 +01001389#if ENABLE_ASSERTIONS
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001390 cpu_context_t *ctx = cm_get_context(NON_SECURE);
1391 assert(ctx != NULL);
1392
Zelalem Awekeb515f542022-04-08 16:48:05 -05001393 /* Assert that EL2 is used. */
Boyan Karatotev4085a022023-03-27 17:02:43 +01001394 u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
Zelalem Awekeb515f542022-04-08 16:48:05 -05001395 assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
1396 (el_implemented(2U) != EL_IMPL_NONE));
Boyan Karatotev4085a022023-03-27 17:02:43 +01001397#endif /* ENABLE_ASSERTIONS */
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001398
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001399 /* Restore EL2 and EL1 sysreg contexts */
1400 cm_el2_sysregs_context_restore(NON_SECURE);
1401 cm_el1_sysregs_context_restore(NON_SECURE);
1402 cm_set_next_eret_context(NON_SECURE);
1403#else
1404 cm_prepare_el3_exit(NON_SECURE);
1405#endif /* CTX_INCLUDE_EL2_REGS */
1406}
1407
1408/*******************************************************************************
Soby Mathewfdfabec2014-07-04 16:02:26 +01001409 * The next four functions are used by runtime services to save and restore
1410 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +00001411 * state.
1412 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +00001413void cm_el1_sysregs_context_save(uint32_t security_state)
1414{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001415 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001416
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001417 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001418 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001419
Max Shvetsov28259462020-02-17 16:15:47 +00001420 el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +01001421
1422#if IMAGE_BL31
1423 if (security_state == SECURE)
1424 PUBLISH_EVENT(cm_exited_secure_world);
1425 else
1426 PUBLISH_EVENT(cm_exited_normal_world);
1427#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001428}
1429
1430void cm_el1_sysregs_context_restore(uint32_t security_state)
1431{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001432 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001433
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001434 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001435 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001436
Max Shvetsov28259462020-02-17 16:15:47 +00001437 el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +01001438
1439#if IMAGE_BL31
1440 if (security_state == SECURE)
1441 PUBLISH_EVENT(cm_entering_secure_world);
1442 else
1443 PUBLISH_EVENT(cm_entering_normal_world);
1444#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001445}
1446
1447/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001448 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
1449 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +00001450 ******************************************************************************/
Soby Mathew4c0d0392016-06-16 14:52:04 +01001451void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +00001452{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001453 cpu_context_t *ctx;
1454 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +00001455
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001456 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001457 assert(ctx != NULL);
Achin Gupta607084e2014-02-09 18:24:19 +00001458
1459 /* Populate EL3 state so that ERET jumps to the correct entry */
1460 state = get_el3state_ctx(ctx);
1461 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1462}
1463
1464/*******************************************************************************
Andrew Thoelke167a9352014-06-04 21:10:52 +01001465 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
1466 * pertaining to the given security state
1467 ******************************************************************************/
1468void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathew4c0d0392016-06-16 14:52:04 +01001469 uintptr_t entrypoint, uint32_t spsr)
Andrew Thoelke167a9352014-06-04 21:10:52 +01001470{
1471 cpu_context_t *ctx;
1472 el3_state_t *state;
1473
1474 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001475 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +01001476
1477 /* Populate EL3 state so that ERET jumps to the correct entry */
1478 state = get_el3state_ctx(ctx);
1479 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1480 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
1481}
1482
1483/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001484 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
1485 * pertaining to the given security state using the value and bit position
1486 * specified in the parameters. It preserves all other bits.
1487 ******************************************************************************/
1488void cm_write_scr_el3_bit(uint32_t security_state,
1489 uint32_t bit_pos,
1490 uint32_t value)
1491{
1492 cpu_context_t *ctx;
1493 el3_state_t *state;
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001494 u_register_t scr_el3;
Achin Guptac429b5e2014-05-04 18:38:28 +01001495
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001496 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001497 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001498
1499 /* Ensure that the bit position is a valid one */
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001500 assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001501
1502 /* Ensure that the 'value' is only a bit wide */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001503 assert(value <= 1U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001504
1505 /*
1506 * Get the SCR_EL3 value from the cpu context, clear the desired bit
1507 * and set it to its new value.
1508 */
1509 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001510 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001511 scr_el3 &= ~(1UL << bit_pos);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001512 scr_el3 |= (u_register_t)value << bit_pos;
Achin Guptac429b5e2014-05-04 18:38:28 +01001513 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
1514}
1515
1516/*******************************************************************************
1517 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
1518 * given security state.
1519 ******************************************************************************/
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001520u_register_t cm_get_scr_el3(uint32_t security_state)
Achin Guptac429b5e2014-05-04 18:38:28 +01001521{
1522 cpu_context_t *ctx;
1523 el3_state_t *state;
1524
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001525 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001526 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001527
1528 /* Populate EL3 state so that ERET jumps to the correct entry */
1529 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001530 return read_ctx_reg(state, CTX_SCR_EL3);
Achin Guptac429b5e2014-05-04 18:38:28 +01001531}
1532
1533/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001534 * This function is used to program the context that's used for exception
1535 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
1536 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +00001537 ******************************************************************************/
1538void cm_set_next_eret_context(uint32_t security_state)
1539{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001540 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001541
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001542 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001543 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001544
Andrew Thoelke167a9352014-06-04 21:10:52 +01001545 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +00001546}