blob: 7472d6170a7e062febcd468d482790e3596b3cdd [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Govindraj Raja57bdc572025-02-04 16:04:27 +01002 * Copyright (c) 2013-2025, 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
Zelalem Awekeb515f542022-04-08 16:48:05 -050087 /*
88 * If workaround of errata 764081 for Cortex-A75 is used then set
89 * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
90 */
Sona Mathew33a04462024-07-10 18:04:40 -050091 if (errata_a75_764081_applies()) {
92 sctlr_elx |= SCTLR_IESB_BIT;
93 }
94
Zelalem Awekeb515f542022-04-08 16:48:05 -050095 /* Store the initialised SCTLR_EL1 value in the cpu_context */
96 write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
97
98 /*
99 * Base the context ACTLR_EL1 on the current value, as it is
100 * implementation defined. The context restore process will write
101 * the value from the context to the actual register and can cause
102 * problems for processor cores that don't expect certain bits to
103 * be zero.
104 */
105 actlr_elx = read_actlr_el1();
106 write_ctx_reg((get_el1_sysregs_ctx(ctx)), (CTX_ACTLR_EL1), (actlr_elx));
107}
108
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600109/******************************************************************************
110 * This function performs initializations that are specific to SECURE state
111 * and updates the cpu context specified by 'ctx'.
112 *****************************************************************************/
113static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
Achin Gupta7aea9082014-02-01 07:51:28 +0000114{
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600115 u_register_t scr_el3;
116 el3_state_t *state;
117
118 state = get_el3state_ctx(ctx);
119 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
120
121#if defined(IMAGE_BL31) && !defined(SPD_spmd)
Achin Gupta7aea9082014-02-01 07:51:28 +0000122 /*
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600123 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
124 * indicated by the interrupt routing model for BL31.
Achin Gupta7aea9082014-02-01 07:51:28 +0000125 */
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600126 scr_el3 |= get_scr_el3_from_routing_model(SECURE);
127#endif
128
129#if !CTX_INCLUDE_MTE_REGS || ENABLE_ASSERTIONS
130 /* Get Memory Tagging Extension support level */
131 unsigned int mte = get_armv8_5_mte_support();
132#endif
133 /*
134 * Allow access to Allocation Tags when CTX_INCLUDE_MTE_REGS
135 * is set, or when MTE is only implemented at EL0.
136 */
137#if CTX_INCLUDE_MTE_REGS
138 assert((mte == MTE_IMPLEMENTED_ELX) || (mte == MTE_IMPLEMENTED_ASY));
139 scr_el3 |= SCR_ATA_BIT;
140#else
141 if (mte == MTE_IMPLEMENTED_EL0) {
142 scr_el3 |= SCR_ATA_BIT;
143 }
144#endif /* CTX_INCLUDE_MTE_REGS */
145
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600146 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
147
Zelalem Awekeb515f542022-04-08 16:48:05 -0500148 /*
149 * Initialize EL1 context registers unless SPMC is running
150 * at S-EL2.
151 */
152#if !SPMD_SPM_AT_SEL2
153 setup_el1_context(ctx, ep);
154#endif
155
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600156 manage_extensions_secure(ctx);
Elizabeth Ho461c0a52023-07-18 14:10:25 +0100157
158 /**
159 * manage_extensions_secure_per_world api has to be executed once,
160 * as the registers getting initialised, maintain constant value across
161 * all the cpus for the secure world.
162 * Henceforth, this check ensures that the registers are initialised once
163 * and avoids re-initialization from multiple cores.
164 */
165 if (!has_secure_perworld_init) {
166 manage_extensions_secure_per_world();
167 }
168
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600169}
170
171#if ENABLE_RME
172/******************************************************************************
173 * This function performs initializations that are specific to REALM state
174 * and updates the cpu context specified by 'ctx'.
175 *****************************************************************************/
176static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
177{
178 u_register_t scr_el3;
179 el3_state_t *state;
180
181 state = get_el3state_ctx(ctx);
182 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
183
Maksims Svecovs01cf14d2023-02-02 16:10:22 +0000184 scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
185
Andre Przywara7db710f2022-11-17 17:30:43 +0000186 if (is_feat_csv2_2_supported()) {
187 /* Enable access to the SCXTNUM_ELx registers. */
188 scr_el3 |= SCR_EnSCXT_BIT;
189 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600190
191 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
192}
193#endif /* ENABLE_RME */
194
195/******************************************************************************
196 * This function performs initializations that are specific to NON-SECURE state
197 * and updates the cpu context specified by 'ctx'.
198 *****************************************************************************/
199static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
200{
201 u_register_t scr_el3;
202 el3_state_t *state;
203
204 state = get_el3state_ctx(ctx);
205 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
206
207 /* SCR_NS: Set the NS bit */
208 scr_el3 |= SCR_NS_BIT;
209
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600210 /* Allow access to Allocation Tags when MTE is implemented. */
211 scr_el3 |= SCR_ATA_BIT;
212
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100213#if !CTX_INCLUDE_PAUTH_REGS
214 /*
215 * Pointer Authentication feature, if present, is always enabled by default
216 * for Non secure lower exception levels. We do not have an explicit
217 * flag to set it.
218 * CTX_INCLUDE_PAUTH_REGS flag, is explicitly used to enable for lower
219 * exception levels of secure and realm worlds.
220 *
221 * To prevent the leakage between the worlds during world switch,
222 * we enable it only for the non-secure world.
223 *
224 * If the Secure/realm world wants to use pointer authentication,
225 * CTX_INCLUDE_PAUTH_REGS must be explicitly set to 1, in which case
226 * it will be enabled globally for all the contexts.
227 *
228 * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
229 * other than EL3
230 *
231 * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
232 * than EL3
233 */
234 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
235
236#endif /* CTX_INCLUDE_PAUTH_REGS */
237
Manish Pandey46cc41d2022-10-10 11:43:08 +0100238#if HANDLE_EA_EL3_FIRST_NS
239 /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
240 scr_el3 |= SCR_EA_BIT;
241#endif
242
Manish Pandey00e8f792022-09-27 14:30:34 +0100243#if RAS_TRAP_NS_ERR_REC_ACCESS
244 /*
245 * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
246 * and RAS ERX registers from EL1 and EL2(from any security state)
247 * are trapped to EL3.
248 * Set here to trap only for NS EL1/EL2
249 *
250 */
251 scr_el3 |= SCR_TERR_BIT;
252#endif
253
Andre Przywara7db710f2022-11-17 17:30:43 +0000254 if (is_feat_csv2_2_supported()) {
255 /* Enable access to the SCXTNUM_ELx registers. */
256 scr_el3 |= SCR_EnSCXT_BIT;
257 }
Maksims Svecovs01cf14d2023-02-02 16:10:22 +0000258
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600259#ifdef IMAGE_BL31
260 /*
261 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
262 * indicated by the interrupt routing model for BL31.
263 */
264 scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
265#endif
266 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600267
Zelalem Awekeb515f542022-04-08 16:48:05 -0500268 /* Initialize EL1 context registers */
269 setup_el1_context(ctx, ep);
270
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600271 /* Initialize EL2 context registers */
272#if CTX_INCLUDE_EL2_REGS
273
274 /*
275 * Initialize SCTLR_EL2 context register using Endianness value
276 * taken from the entrypoint attribute.
277 */
278 u_register_t sctlr_el2 = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
279 sctlr_el2 |= SCTLR_EL2_RES1;
280 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_SCTLR_EL2,
281 sctlr_el2);
282
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600283 if (is_feat_hcx_supported()) {
284 /*
285 * Initialize register HCRX_EL2 with its init value.
286 * As the value of HCRX_EL2 is UNKNOWN on reset, there is a
287 * chance that this can lead to unexpected behavior in lower
288 * ELs that have not been updated since the introduction of
289 * this feature if not properly initialized, especially when
290 * it comes to those bits that enable/disable traps.
291 */
292 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HCRX_EL2,
293 HCRX_EL2_INIT_VAL);
294 }
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500295
296 if (is_feat_fgt_supported()) {
297 /*
298 * Initialize HFG*_EL2 registers with a default value so legacy
299 * systems unaware of FEAT_FGT do not get trapped due to their lack
300 * of initialization for this feature.
301 */
302 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGITR_EL2,
303 HFGITR_EL2_INIT_VAL);
304 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGRTR_EL2,
305 HFGRTR_EL2_INIT_VAL);
306 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGWTR_EL2,
307 HFGWTR_EL2_INIT_VAL);
308 }
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600309#endif /* CTX_INCLUDE_EL2_REGS */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000310
311 manage_extensions_nonsecure(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +0000312}
313
314/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600315 * The following function performs initialization of the cpu_context 'ctx'
316 * for first use that is common to all security states, and sets the
317 * initial entrypoint state as specified by the entry_point_info structure.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100318 *
Paul Beesley8aabea32019-01-11 18:26:51 +0000319 * The EE and ST attributes are used to configure the endianness and secure
Soby Mathew12d0d002015-04-09 13:40:55 +0100320 * timer availability for the new execution context.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100321 ******************************************************************************/
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600322static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke167a9352014-06-04 21:10:52 +0100323{
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000324 u_register_t scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100325 el3_state_t *state;
326 gp_regs_t *gp_regs;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100327
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100328 state = get_el3state_ctx(ctx);
329
Andrew Thoelke167a9352014-06-04 21:10:52 +0100330 /* Clear any residual register values from the context */
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000331 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke167a9352014-06-04 21:10:52 +0100332
333 /*
Boyan Karatotev5e8cc722023-05-23 12:04:00 +0100334 * The lower-EL context is zeroed so that no stale values leak to a world.
335 * It is assumed that an all-zero lower-EL context is good enough for it
336 * to boot correctly. However, there are very few registers where this
337 * is not true and some values need to be recreated.
338 */
339#if CTX_INCLUDE_EL2_REGS
340 el2_sysregs_t *el2_ctx = get_el2_sysregs_ctx(ctx);
341
342 /*
343 * These bits are set in the gicv3 driver. Losing them (especially the
344 * SRE bit) is problematic for all worlds. Henceforth recreate them.
345 */
346 u_register_t icc_sre_el2 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
347 ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
348 write_ctx_reg(el2_ctx, CTX_ICC_SRE_EL2, icc_sre_el2);
349#endif /* CTX_INCLUDE_EL2_REGS */
350
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +0100351 /* Start with a clean SCR_EL3 copy as all relevant values are set */
352 scr_el3 = SCR_RESET_VAL;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500353
David Cunado18f2efd2017-04-13 22:38:29 +0100354 /*
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100355 * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
356 * EL2, EL1 and EL0 are not trapped to EL3.
357 *
358 * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
359 * EL2, EL1 and EL0 are not trapped to EL3.
360 *
361 * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
362 * both Security states and both Execution states.
363 *
364 * SCR_EL3.SIF: Set to one to disable secure instruction execution from
365 * Non-secure memory.
366 */
367 scr_el3 &= ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT);
368
369 scr_el3 |= SCR_SIF_BIT;
370
371 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100372 * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
373 * Exception level as specified by SPSR.
374 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500375 if (GET_RW(ep->spsr) == MODE_RW_64) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100376 scr_el3 |= SCR_RW_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500377 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600378
David Cunado18f2efd2017-04-13 22:38:29 +0100379 /*
380 * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
Zelalem Awekeb515f542022-04-08 16:48:05 -0500381 * Secure timer registers to EL3, from AArch64 state only, if specified
382 * by the entrypoint attributes. If SEL2 is present and enabled, the ST
383 * bit always behaves as 1 (i.e. secure physical timer register access
384 * is not trapped)
David Cunado18f2efd2017-04-13 22:38:29 +0100385 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500386 if (EP_GET_ST(ep->h.attr) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100387 scr_el3 |= SCR_ST_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500388 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100389
johpow01cb4ec472021-08-04 19:38:18 -0500390 /*
391 * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
392 * SCR_EL3.HXEn.
393 */
Andre Przywarac5a3ebb2022-11-15 11:45:19 +0000394 if (is_feat_hcx_supported()) {
395 scr_el3 |= SCR_HXEn_BIT;
396 }
johpow01cb4ec472021-08-04 19:38:18 -0500397
Juan Pablo Condeff86e0b2022-07-12 16:40:29 -0400398 /*
399 * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
400 * registers are trapped to EL3.
401 */
402#if ENABLE_FEAT_RNG_TRAP
403 scr_el3 |= SCR_TRNDR_BIT;
404#endif
405
Jeenu Viswambharan1a7c1cf2017-12-08 12:13:51 +0000406#if FAULT_INJECTION_SUPPORT
407 /* Enable fault injection from lower ELs */
408 scr_el3 |= SCR_FIEN_BIT;
409#endif
410
Boyan Karatotevf0c96a22023-04-20 11:00:50 +0100411#if CTX_INCLUDE_PAUTH_REGS
412 /*
413 * Enable Pointer Authentication globally for all the worlds.
414 *
415 * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
416 * other than EL3
417 *
418 * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
419 * than EL3
420 */
421 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
422#endif /* CTX_INCLUDE_PAUTH_REGS */
423
Antonio Nino Diaz52839622019-01-31 11:58:00 +0000424 /*
Mark Brownd3331602023-03-14 20:13:03 +0000425 * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
426 */
427 if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
428 scr_el3 |= SCR_TCR2EN_BIT;
429 }
430
431 /*
Mark Brown062b6c62023-03-14 20:48:43 +0000432 * SCR_EL3.PIEN: Enable permission indirection and overlay
433 * registers for AArch64 if present.
434 */
435 if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
436 scr_el3 |= SCR_PIEN_BIT;
437 }
438
439 /*
Mark Brown688ab572023-03-14 21:33:04 +0000440 * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
441 */
442 if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
443 scr_el3 |= SCR_GCSEn_BIT;
444 }
445
446 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100447 * SCR_EL3.HCE: Enable HVC instructions if next execution state is
448 * AArch64 and next EL is EL2, or if next execution state is AArch32 and
449 * next mode is Hyp.
Jimmy Brisson110ee432020-04-16 10:47:56 -0500450 * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
451 * same conditions as HVC instructions and when the processor supports
452 * ARMv8.6-FGT.
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500453 * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
454 * CNTPOFF_EL2 register under the same conditions as HVC instructions
455 * and when the processor supports ECV.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100456 */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000457 if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
458 || ((GET_RW(ep->spsr) != MODE_RW_64)
459 && (GET_M32(ep->spsr) == MODE32_hyp))) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100460 scr_el3 |= SCR_HCE_BIT;
Jimmy Brisson110ee432020-04-16 10:47:56 -0500461
Andre Przywarace485952022-11-10 14:28:01 +0000462 if (is_feat_fgt_supported()) {
Jimmy Brisson110ee432020-04-16 10:47:56 -0500463 scr_el3 |= SCR_FGTEN_BIT;
464 }
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500465
Andre Przywarab8f03d22022-11-17 17:30:43 +0000466 if (is_feat_ecv_supported()) {
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500467 scr_el3 |= SCR_ECVEN_BIT;
468 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100469 }
470
johpow016cac7242020-04-22 14:05:13 -0500471 /* Enable WFE trap delay in SCR_EL3 if supported and configured */
Andre Przywara1223d2a2023-01-27 12:25:49 +0000472 if (is_feat_twed_supported()) {
473 /* Set delay in SCR_EL3 */
474 scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
475 scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
476 << SCR_TWEDEL_SHIFT);
johpow016cac7242020-04-22 14:05:13 -0500477
Andre Przywara1223d2a2023-01-27 12:25:49 +0000478 /* Enable WFE delay */
479 scr_el3 |= SCR_TWEDEn_BIT;
480 }
johpow016cac7242020-04-22 14:05:13 -0500481
Jayanth Dodderi Chidanand9f4b6252023-09-22 15:30:13 +0100482#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
483 /* Enable S-EL2 if FEAT_SEL2 is implemented for all the contexts. */
484 if (is_feat_sel2_supported()) {
485 scr_el3 |= SCR_EEL2_BIT;
486 }
487#endif /* (IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2) */
488
Arvind Ram Prakashedebefb2023-10-11 12:10:56 -0500489 if (is_feat_mpam_supported()) {
490 write_ctx_reg(get_el3state_ctx(ctx), CTX_MPAM3_EL3, \
491 MPAM3_EL3_RESET_VAL);
492 }
493
David Cunado18f2efd2017-04-13 22:38:29 +0100494 /*
Alexei Fedorove290a8f2019-08-13 15:17:53 +0100495 * Populate EL3 state so that we've the right context
496 * before doing ERET
497 */
Andrew Thoelke167a9352014-06-04 21:10:52 +0100498 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
499 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
500 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
501
502 /*
503 * Store the X0-X7 value from the entrypoint into the context
504 * Use memcpy as we are in control of the layout of the structures
505 */
506 gp_regs = get_gpregs_ctx(ctx);
507 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
508}
509
510/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600511 * Context management library initialization routine. This library is used by
512 * runtime services to share pointers to 'cpu_context' structures for secure
513 * non-secure and realm states. Management of the structures and their associated
514 * memory is not done by the context management library e.g. the PSCI service
515 * manages the cpu context used for entry from and exit to the non-secure state.
516 * The Secure payload dispatcher service manages the context(s) corresponding to
517 * the secure state. It also uses this library to get access to the non-secure
518 * state cpu context pointers.
519 * Lastly, this library provides the API to make SP_EL3 point to the cpu context
520 * which will be used for programming an entry into a lower EL. The same context
521 * will be used to save state upon exception entry from that EL.
522 ******************************************************************************/
523void __init cm_init(void)
524{
525 /*
Elyes Haouas1b491ee2023-02-13 09:14:48 +0100526 * The context management library has only global data to initialize, but
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600527 * that will be done when the BSS is zeroed out.
528 */
529}
530
531/*******************************************************************************
532 * This is the high-level function used to initialize the cpu_context 'ctx' for
533 * first use. It performs initializations that are common to all security states
534 * and initializations specific to the security state specified in 'ep'
535 ******************************************************************************/
536void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
537{
538 unsigned int security_state;
539
540 assert(ctx != NULL);
541
542 /*
543 * Perform initializations that are common
544 * to all security states
545 */
546 setup_context_common(ctx, ep);
547
548 security_state = GET_SECURITY_STATE(ep->h.attr);
549
550 /* Perform security state specific initializations */
551 switch (security_state) {
552 case SECURE:
553 setup_secure_context(ctx, ep);
554 break;
555#if ENABLE_RME
556 case REALM:
557 setup_realm_context(ctx, ep);
558 break;
559#endif
560 case NON_SECURE:
561 setup_ns_context(ctx, ep);
562 break;
563 default:
564 ERROR("Invalid security state\n");
565 panic();
566 break;
567 }
568}
569
570/*******************************************************************************
Boyan Karatotev24a70732023-03-08 11:56:49 +0000571 * Enable architecture extensions for EL3 execution. This function only updates
572 * registers in-place which are expected to either never change or be
573 * overwritten by el3_exit.
574 ******************************************************************************/
575#if IMAGE_BL31
576void cm_manage_extensions_el3(void)
577{
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000578 if (is_feat_spe_supported()) {
579 spe_init_el3();
580 }
581
Boyan Karatotev4085a022023-03-27 17:02:43 +0100582 if (is_feat_amu_supported()) {
583 amu_init_el3();
584 }
585
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000586 if (is_feat_sme_supported()) {
587 sme_init_el3();
588 }
589
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000590 if (is_feat_trbe_supported()) {
591 trbe_init_el3();
592 }
593
594 if (is_feat_brbe_supported()) {
595 brbe_init_el3();
596 }
597
598 if (is_feat_trf_supported()) {
599 trf_init_el3();
600 }
601
602 pmuv3_init_el3();
Boyan Karatotev24a70732023-03-08 11:56:49 +0000603}
604#endif /* IMAGE_BL31 */
605
606/*******************************************************************************
Elizabeth Ho461c0a52023-07-18 14:10:25 +0100607 * Initialise per_world_context for Non-Secure world.
608 * This function enables the architecture extensions, which have same value
609 * across the cores for the non-secure world.
610 ******************************************************************************/
611#if IMAGE_BL31
612void manage_extensions_nonsecure_per_world(void)
613{
614 if (is_feat_sme_supported()) {
615 sme_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
616 }
617
618 if (is_feat_sve_supported()) {
619 sve_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
620 }
621
622 if (is_feat_amu_supported()) {
623 amu_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
624 }
625
626 if (is_feat_sys_reg_trace_supported()) {
627 sys_reg_trace_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
628 }
629}
630#endif /* IMAGE_BL31 */
631
632/*******************************************************************************
633 * Initialise per_world_context for Secure world.
634 * This function enables the architecture extensions, which have same value
635 * across the cores for the secure world.
636 ******************************************************************************/
637
638static void manage_extensions_secure_per_world(void)
639{
640#if IMAGE_BL31
641 if (is_feat_sme_supported()) {
642
643 if (ENABLE_SME_FOR_SWD) {
644 /*
645 * Enable SME, SVE, FPU/SIMD in secure context, SPM must ensure
646 * SME, SVE, and FPU/SIMD context properly managed.
647 */
648 sme_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
649 } else {
650 /*
651 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
652 * world can safely use the associated registers.
653 */
654 sme_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
655 }
656 }
657 if (is_feat_sve_supported()) {
658 if (ENABLE_SVE_FOR_SWD) {
659 /*
660 * Enable SVE and FPU in secure context, SPM must ensure
661 * that the SVE and FPU register contexts are properly managed.
662 */
663 sve_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
664 } else {
665 /*
666 * Disable SVE and FPU in secure context so non-secure world
667 * can safely use them.
668 */
669 sve_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
670 }
671 }
672
673 /* NS can access this but Secure shouldn't */
674 if (is_feat_sys_reg_trace_supported()) {
675 sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
676 }
677
678 has_secure_perworld_init = true;
679#endif /* IMAGE_BL31 */
680}
681
682/*******************************************************************************
Boyan Karatotev24a70732023-03-08 11:56:49 +0000683 * Enable architecture extensions on first entry to Non-secure world.
684 ******************************************************************************/
685static void manage_extensions_nonsecure(cpu_context_t *ctx)
686{
687#if IMAGE_BL31
Boyan Karatotev4085a022023-03-27 17:02:43 +0100688 if (is_feat_amu_supported()) {
689 amu_enable(ctx);
690 }
691
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000692 if (is_feat_sme_supported()) {
693 sme_enable(ctx);
694 }
695
Arvind Ram Prakashedebefb2023-10-11 12:10:56 -0500696 if (is_feat_mpam_supported()) {
697 mpam_enable(ctx);
698 }
Boyan Karatotevc73686a2023-02-15 13:21:50 +0000699 pmuv3_enable(ctx);
Boyan Karatotev24a70732023-03-08 11:56:49 +0000700#endif /* IMAGE_BL31 */
701}
702
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000703/* TODO: move to lib/extensions/pauth when it has been ported to FEAT_STATE */
704static __unused void enable_pauth_el2(void)
705{
706 u_register_t hcr_el2 = read_hcr_el2();
707 /*
708 * For Armv8.3 pointer authentication feature, disable traps to EL2 when
709 * accessing key registers or using pointer authentication instructions
710 * from lower ELs.
711 */
712 hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
713
714 write_hcr_el2(hcr_el2);
715}
716
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500717#if INIT_UNUSED_NS_EL2
Boyan Karatotev24a70732023-03-08 11:56:49 +0000718/*******************************************************************************
719 * Enable architecture extensions in-place at EL2 on first entry to Non-secure
720 * world when EL2 is empty and unused.
721 ******************************************************************************/
722static void manage_extensions_nonsecure_el2_unused(void)
723{
724#if IMAGE_BL31
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000725 if (is_feat_spe_supported()) {
726 spe_init_el2_unused();
727 }
728
Boyan Karatotev4085a022023-03-27 17:02:43 +0100729 if (is_feat_amu_supported()) {
730 amu_init_el2_unused();
731 }
732
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000733 if (is_feat_mpam_supported()) {
734 mpam_init_el2_unused();
735 }
736
737 if (is_feat_trbe_supported()) {
738 trbe_init_el2_unused();
739 }
740
741 if (is_feat_sys_reg_trace_supported()) {
742 sys_reg_trace_init_el2_unused();
743 }
744
745 if (is_feat_trf_supported()) {
746 trf_init_el2_unused();
747 }
748
Boyan Karatotevc73686a2023-02-15 13:21:50 +0000749 pmuv3_init_el2_unused();
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000750
751 if (is_feat_sve_supported()) {
752 sve_init_el2_unused();
753 }
754
755 if (is_feat_sme_supported()) {
756 sme_init_el2_unused();
757 }
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000758
759#if ENABLE_PAUTH
760 enable_pauth_el2();
761#endif /* ENABLE_PAUTH */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000762#endif /* IMAGE_BL31 */
763}
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500764#endif /* INIT_UNUSED_NS_EL2 */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000765
766/*******************************************************************************
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100767 * Enable architecture extensions on first entry to Secure world.
768 ******************************************************************************/
johpow01dc78e622021-07-08 14:14:00 -0500769static void manage_extensions_secure(cpu_context_t *ctx)
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100770{
771#if IMAGE_BL31
Boyan Karatotev0d122942023-03-08 16:29:26 +0000772 if (is_feat_sme_supported()) {
773 if (ENABLE_SME_FOR_SWD) {
774 /*
775 * Enable SME, SVE, FPU/SIMD in secure context, secure manager
776 * must ensure SME, SVE, and FPU/SIMD context properly managed.
777 */
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000778 sme_init_el3();
Boyan Karatotev0d122942023-03-08 16:29:26 +0000779 sme_enable(ctx);
780 } else {
781 /*
782 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
783 * world can safely use the associated registers.
784 */
785 sme_disable(ctx);
786 }
787 }
johpow01dc78e622021-07-08 14:14:00 -0500788#endif /* IMAGE_BL31 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100789}
790
791/*******************************************************************************
Soby Mathew12d0d002015-04-09 13:40:55 +0100792 * The following function initializes the cpu_context for a CPU specified by
793 * its `cpu_idx` for first use, and sets the initial entrypoint state as
794 * specified by the entry_point_info structure.
795 ******************************************************************************/
796void cm_init_context_by_index(unsigned int cpu_idx,
797 const entry_point_info_t *ep)
798{
799 cpu_context_t *ctx;
800 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100801 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100802}
803
804/*******************************************************************************
805 * The following function initializes the cpu_context for the current CPU
806 * for first use, and sets the initial entrypoint state as specified by the
807 * entry_point_info structure.
808 ******************************************************************************/
809void cm_init_my_context(const entry_point_info_t *ep)
810{
811 cpu_context_t *ctx;
812 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100813 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100814}
815
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000816/* EL2 present but unused, need to disable safely. SCTLR_EL2 can be ignored */
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500817static void init_nonsecure_el2_unused(cpu_context_t *ctx)
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000818{
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500819#if INIT_UNUSED_NS_EL2
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000820 u_register_t hcr_el2 = HCR_RESET_VAL;
821 u_register_t mdcr_el2;
822 u_register_t scr_el3;
823
824 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
825
826 /* Set EL2 register width: Set HCR_EL2.RW to match SCR_EL3.RW */
827 if ((scr_el3 & SCR_RW_BIT) != 0U) {
828 hcr_el2 |= HCR_RW_BIT;
829 }
830
831 write_hcr_el2(hcr_el2);
832
833 /*
834 * Initialise CPTR_EL2 setting all fields rather than relying on the hw.
835 * All fields have architecturally UNKNOWN reset values.
836 */
837 write_cptr_el2(CPTR_EL2_RESET_VAL);
838
839 /*
840 * Initialise CNTHCTL_EL2. All fields are architecturally UNKNOWN on
841 * reset and are set to zero except for field(s) listed below.
842 *
843 * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to Hyp mode of
844 * Non-secure EL0 and EL1 accesses to the physical timer registers.
845 *
846 * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to Hyp mode of
847 * Non-secure EL0 and EL1 accesses to the physical counter registers.
848 */
849 write_cnthctl_el2(CNTHCTL_RESET_VAL | EL1PCEN_BIT | EL1PCTEN_BIT);
850
851 /*
852 * Initialise CNTVOFF_EL2 to zero as it resets to an architecturally
853 * UNKNOWN value.
854 */
855 write_cntvoff_el2(0);
856
857 /*
858 * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and MPIDR_EL1
859 * respectively.
860 */
861 write_vpidr_el2(read_midr_el1());
862 write_vmpidr_el2(read_mpidr_el1());
863
864 /*
865 * Initialise VTTBR_EL2. All fields are architecturally UNKNOWN on reset.
866 *
867 * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage 2 address
868 * translation is disabled, cache maintenance operations depend on the
869 * VMID.
870 *
871 * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address translation is
872 * disabled.
873 */
874 write_vttbr_el2(VTTBR_RESET_VAL &
875 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
876 (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
877
878 /*
879 * Initialise MDCR_EL2, setting all fields rather than relying on hw.
880 * Some fields are architecturally UNKNOWN on reset.
881 *
882 * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and EL1 System
883 * register accesses to the Debug ROM registers are not trapped to EL2.
884 *
885 * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1 System register
886 * accesses to the powerdown debug registers are not trapped to EL2.
887 *
888 * MDCR_EL2.TDA: Set to zero so that System register accesses to the
889 * debug registers do not trap to EL2.
890 *
891 * MDCR_EL2.TDE: Set to zero so that debug exceptions are not routed to
892 * EL2.
893 */
894 mdcr_el2 = MDCR_EL2_RESET_VAL &
895 ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT | MDCR_EL2_TDA_BIT |
896 MDCR_EL2_TDE_BIT);
897
898 write_mdcr_el2(mdcr_el2);
899
900 /*
901 * Initialise HSTR_EL2. All fields are architecturally UNKNOWN on reset.
902 *
903 * HSTR_EL2.T<n>: Set all these fields to zero so that Non-secure EL0 or
904 * EL1 accesses to System registers do not trap to EL2.
905 */
906 write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
907
908 /*
909 * Initialise CNTHP_CTL_EL2. All fields are architecturally UNKNOWN on
910 * reset.
911 *
912 * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2 physical timer
913 * and prevent timer interrupts.
914 */
915 write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT));
916
917 manage_extensions_nonsecure_el2_unused();
Arvind Ram Prakash183329a2023-08-15 16:28:06 -0500918#endif /* INIT_UNUSED_NS_EL2 */
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000919}
920
Soby Mathew12d0d002015-04-09 13:40:55 +0100921/*******************************************************************************
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500922 * Prepare the CPU system registers for first entry into realm, secure, or
923 * normal world.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100924 *
925 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
926 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
927 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
928 * For all entries, the EL1 registers are initialized from the cpu_context
929 ******************************************************************************/
930void cm_prepare_el3_exit(uint32_t security_state)
931{
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000932 u_register_t sctlr_elx, scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100933 cpu_context_t *ctx = cm_get_context(security_state);
934
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000935 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100936
937 if (security_state == NON_SECURE) {
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600938 uint64_t el2_implemented = el_implemented(2);
939
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000940 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000941 CTX_SCR_EL3);
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600942
943 if (((scr_el3 & SCR_HCE_BIT) != 0U)
944 || (el2_implemented != EL_IMPL_NONE)) {
945 /*
946 * If context is not being used for EL2, initialize
947 * HCRX_EL2 with its init value here.
948 */
949 if (is_feat_hcx_supported()) {
950 write_hcrx_el2(HCRX_EL2_INIT_VAL);
951 }
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500952
953 /*
954 * Initialize Fine-grained trap registers introduced
955 * by FEAT_FGT so all traps are initially disabled when
956 * switching to EL2 or a lower EL, preventing undesired
957 * behavior.
958 */
959 if (is_feat_fgt_supported()) {
960 /*
961 * Initialize HFG*_EL2 registers with a default
962 * value so legacy systems unaware of FEAT_FGT
963 * do not get trapped due to their lack of
964 * initialization for this feature.
965 */
966 write_hfgitr_el2(HFGITR_EL2_INIT_VAL);
967 write_hfgrtr_el2(HFGRTR_EL2_INIT_VAL);
968 write_hfgwtr_el2(HFGWTR_EL2_INIT_VAL);
969 }
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600970 }
971
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500972
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000973 if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100974 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
Max Shvetsov28259462020-02-17 16:15:47 +0000975 sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000976 CTX_SCTLR_EL1);
Ken Kuang2e09d4f2017-08-23 16:03:29 +0800977 sctlr_elx &= SCTLR_EE_BIT;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100978 sctlr_elx |= SCTLR_EL2_RES1;
Louis Mayencourt5f5d1ed2019-02-20 12:11:41 +0000979 /*
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 */
Sona Mathew33a04462024-07-10 18:04:40 -0500984 if (errata_a75_764081_applies()) {
985 sctlr_elx |= SCTLR_IESB_BIT;
986 }
987
Andrew Thoelke167a9352014-06-04 21:10:52 +0100988 write_sctlr_el2(sctlr_elx);
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600989 } else if (el2_implemented != EL_IMPL_NONE) {
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000990 init_nonsecure_el2_unused(ctx);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100991 }
992 }
993
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +0100994 cm_el1_sysregs_context_restore(security_state);
995 cm_set_next_eret_context(security_state);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100996}
997
Max Shvetsov28f39f02020-02-25 13:56:19 +0000998#if CTX_INCLUDE_EL2_REGS
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000999
1000static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
1001{
Andre Przywarade8c4892023-02-15 15:56:15 +00001002 write_ctx_reg(ctx, CTX_HDFGRTR_EL2, read_hdfgrtr_el2());
1003 if (is_feat_amu_supported()) {
1004 write_ctx_reg(ctx, CTX_HAFGRTR_EL2, read_hafgrtr_el2());
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001005 }
Andre Przywarade8c4892023-02-15 15:56:15 +00001006 write_ctx_reg(ctx, CTX_HDFGWTR_EL2, read_hdfgwtr_el2());
1007 write_ctx_reg(ctx, CTX_HFGITR_EL2, read_hfgitr_el2());
1008 write_ctx_reg(ctx, CTX_HFGRTR_EL2, read_hfgrtr_el2());
1009 write_ctx_reg(ctx, CTX_HFGWTR_EL2, read_hfgwtr_el2());
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001010}
1011
1012static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
1013{
Andre Przywarade8c4892023-02-15 15:56:15 +00001014 write_hdfgrtr_el2(read_ctx_reg(ctx, CTX_HDFGRTR_EL2));
1015 if (is_feat_amu_supported()) {
1016 write_hafgrtr_el2(read_ctx_reg(ctx, CTX_HAFGRTR_EL2));
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001017 }
Andre Przywarade8c4892023-02-15 15:56:15 +00001018 write_hdfgwtr_el2(read_ctx_reg(ctx, CTX_HDFGWTR_EL2));
1019 write_hfgitr_el2(read_ctx_reg(ctx, CTX_HFGITR_EL2));
1020 write_hfgrtr_el2(read_ctx_reg(ctx, CTX_HFGRTR_EL2));
1021 write_hfgwtr_el2(read_ctx_reg(ctx, CTX_HFGWTR_EL2));
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001022}
1023
Andre Przywara9448f2b2022-11-17 16:42:09 +00001024static void el2_sysregs_context_save_mpam(el2_sysregs_t *ctx)
1025{
1026 u_register_t mpam_idr = read_mpamidr_el1();
1027
1028 write_ctx_reg(ctx, CTX_MPAM2_EL2, read_mpam2_el2());
1029
1030 /*
1031 * The context registers that we intend to save would be part of the
1032 * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
1033 */
1034 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
1035 return;
1036 }
1037
1038 /*
1039 * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
1040 * MPAMIDR_HAS_HCR_BIT == 1.
1041 */
1042 write_ctx_reg(ctx, CTX_MPAMHCR_EL2, read_mpamhcr_el2());
1043 write_ctx_reg(ctx, CTX_MPAMVPM0_EL2, read_mpamvpm0_el2());
1044 write_ctx_reg(ctx, CTX_MPAMVPMV_EL2, read_mpamvpmv_el2());
1045
1046 /*
1047 * The number of MPAMVPM registers is implementation defined, their
1048 * number is stored in the MPAMIDR_EL1 register.
1049 */
1050 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
1051 case 7:
1052 write_ctx_reg(ctx, CTX_MPAMVPM7_EL2, read_mpamvpm7_el2());
1053 __fallthrough;
1054 case 6:
1055 write_ctx_reg(ctx, CTX_MPAMVPM6_EL2, read_mpamvpm6_el2());
1056 __fallthrough;
1057 case 5:
1058 write_ctx_reg(ctx, CTX_MPAMVPM5_EL2, read_mpamvpm5_el2());
1059 __fallthrough;
1060 case 4:
1061 write_ctx_reg(ctx, CTX_MPAMVPM4_EL2, read_mpamvpm4_el2());
1062 __fallthrough;
1063 case 3:
1064 write_ctx_reg(ctx, CTX_MPAMVPM3_EL2, read_mpamvpm3_el2());
1065 __fallthrough;
1066 case 2:
1067 write_ctx_reg(ctx, CTX_MPAMVPM2_EL2, read_mpamvpm2_el2());
1068 __fallthrough;
1069 case 1:
1070 write_ctx_reg(ctx, CTX_MPAMVPM1_EL2, read_mpamvpm1_el2());
1071 break;
1072 }
1073}
1074
1075static void el2_sysregs_context_restore_mpam(el2_sysregs_t *ctx)
1076{
1077 u_register_t mpam_idr = read_mpamidr_el1();
1078
1079 write_mpam2_el2(read_ctx_reg(ctx, CTX_MPAM2_EL2));
1080
1081 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
1082 return;
1083 }
1084
1085 write_mpamhcr_el2(read_ctx_reg(ctx, CTX_MPAMHCR_EL2));
1086 write_mpamvpm0_el2(read_ctx_reg(ctx, CTX_MPAMVPM0_EL2));
1087 write_mpamvpmv_el2(read_ctx_reg(ctx, CTX_MPAMVPMV_EL2));
1088
1089 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
1090 case 7:
1091 write_mpamvpm7_el2(read_ctx_reg(ctx, CTX_MPAMVPM7_EL2));
1092 __fallthrough;
1093 case 6:
1094 write_mpamvpm6_el2(read_ctx_reg(ctx, CTX_MPAMVPM6_EL2));
1095 __fallthrough;
1096 case 5:
1097 write_mpamvpm5_el2(read_ctx_reg(ctx, CTX_MPAMVPM5_EL2));
1098 __fallthrough;
1099 case 4:
1100 write_mpamvpm4_el2(read_ctx_reg(ctx, CTX_MPAMVPM4_EL2));
1101 __fallthrough;
1102 case 3:
1103 write_mpamvpm3_el2(read_ctx_reg(ctx, CTX_MPAMVPM3_EL2));
1104 __fallthrough;
1105 case 2:
1106 write_mpamvpm2_el2(read_ctx_reg(ctx, CTX_MPAMVPM2_EL2));
1107 __fallthrough;
1108 case 1:
1109 write_mpamvpm1_el2(read_ctx_reg(ctx, CTX_MPAMVPM1_EL2));
1110 break;
1111 }
1112}
1113
Manish Pandeyf823cc72025-02-04 15:59:06 +01001114/* ---------------------------------------------------------------------------
1115 * The following registers are not added:
1116 * ICH_AP0R<n>_EL2
1117 * ICH_AP1R<n>_EL2
1118 * ICH_LR<n>_EL2
1119 *
1120 * NOTE: For a system with S-EL2 present but not enabled, accessing
1121 * ICC_SRE_EL2 is undefined from EL3. To workaround this change the
1122 * SCR_EL3.NS = 1 before accessing this register.
1123 * ---------------------------------------------------------------------------
1124 */
Govindraj Raja57bdc572025-02-04 16:04:27 +01001125static void el2_sysregs_context_save_gic(el2_sysregs_t *ctx, uint32_t security_state)
Manish Pandeyf823cc72025-02-04 15:59:06 +01001126{
Govindraj Raja57bdc572025-02-04 16:04:27 +01001127 u_register_t scr_el3 = read_scr_el3();
1128
Manish Pandeyf823cc72025-02-04 15:59:06 +01001129#if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
1130 write_ctx_reg(ctx, CTX_ICC_SRE_EL2, read_icc_sre_el2());
1131#else
Manish Pandeyf823cc72025-02-04 15:59:06 +01001132 write_scr_el3(scr_el3 | SCR_NS_BIT);
1133 isb();
1134
1135 write_ctx_reg(ctx, CTX_ICC_SRE_EL2, read_icc_sre_el2());
1136
1137 write_scr_el3(scr_el3);
1138 isb();
1139
1140#endif
1141 write_ctx_reg(ctx, CTX_ICH_HCR_EL2, read_ich_hcr_el2());
Govindraj Raja57bdc572025-02-04 16:04:27 +01001142
1143 if (errata_ich_vmcr_el2_applies()) {
1144 if (security_state == SECURE) {
1145 write_scr_el3(scr_el3 & ~SCR_NS_BIT);
1146 } else {
1147 write_scr_el3(scr_el3 | SCR_NS_BIT);
1148 }
1149 isb();
1150 }
1151
Manish Pandeyf823cc72025-02-04 15:59:06 +01001152 write_ctx_reg(ctx, CTX_ICH_VMCR_EL2, read_ich_vmcr_el2());
Govindraj Raja57bdc572025-02-04 16:04:27 +01001153
1154 if (errata_ich_vmcr_el2_applies()) {
1155 write_scr_el3(scr_el3);
1156 isb();
1157 }
Manish Pandeyf823cc72025-02-04 15:59:06 +01001158}
1159
Govindraj Raja57bdc572025-02-04 16:04:27 +01001160static void el2_sysregs_context_restore_gic(el2_sysregs_t *ctx, uint32_t security_state)
Manish Pandeyf823cc72025-02-04 15:59:06 +01001161{
Govindraj Raja57bdc572025-02-04 16:04:27 +01001162 u_register_t scr_el3 = read_scr_el3();
1163
Manish Pandeyf823cc72025-02-04 15:59:06 +01001164#if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
1165 write_icc_sre_el2(read_ctx_reg(ctx, CTX_ICC_SRE_EL2));
1166#else
Manish Pandeyf823cc72025-02-04 15:59:06 +01001167 write_scr_el3(scr_el3 | SCR_NS_BIT);
1168 isb();
1169
1170 write_icc_sre_el2(read_ctx_reg(ctx, CTX_ICC_SRE_EL2));
1171
1172 write_scr_el3(scr_el3);
1173 isb();
1174#endif
1175 write_ich_hcr_el2(read_ctx_reg(ctx, CTX_ICH_HCR_EL2));
Govindraj Raja57bdc572025-02-04 16:04:27 +01001176
1177 if (errata_ich_vmcr_el2_applies()) {
1178 if (security_state == SECURE) {
1179 write_scr_el3(scr_el3 & ~SCR_NS_BIT);
1180 } else {
1181 write_scr_el3(scr_el3 | SCR_NS_BIT);
1182 }
1183 isb();
1184 }
1185
Manish Pandeyf823cc72025-02-04 15:59:06 +01001186 write_ich_vmcr_el2(read_ctx_reg(ctx, CTX_ICH_VMCR_EL2));
Govindraj Raja57bdc572025-02-04 16:04:27 +01001187
1188 if (errata_ich_vmcr_el2_applies()) {
1189 write_scr_el3(scr_el3);
1190 isb();
1191 }
Manish Pandeyf823cc72025-02-04 15:59:06 +01001192}
1193
Boyan Karatotevac58e572023-05-15 15:09:16 +01001194/* -----------------------------------------------------
1195 * The following registers are not added:
1196 * AMEVCNTVOFF0<n>_EL2
1197 * AMEVCNTVOFF1<n>_EL2
Boyan Karatotevac58e572023-05-15 15:09:16 +01001198 * -----------------------------------------------------
1199 */
1200static void el2_sysregs_context_save_common(el2_sysregs_t *ctx)
1201{
1202 write_ctx_reg(ctx, CTX_ACTLR_EL2, read_actlr_el2());
1203 write_ctx_reg(ctx, CTX_AFSR0_EL2, read_afsr0_el2());
1204 write_ctx_reg(ctx, CTX_AFSR1_EL2, read_afsr1_el2());
1205 write_ctx_reg(ctx, CTX_AMAIR_EL2, read_amair_el2());
1206 write_ctx_reg(ctx, CTX_CNTHCTL_EL2, read_cnthctl_el2());
1207 write_ctx_reg(ctx, CTX_CNTVOFF_EL2, read_cntvoff_el2());
1208 write_ctx_reg(ctx, CTX_CPTR_EL2, read_cptr_el2());
1209 if (CTX_INCLUDE_AARCH32_REGS) {
1210 write_ctx_reg(ctx, CTX_DBGVCR32_EL2, read_dbgvcr32_el2());
1211 }
1212 write_ctx_reg(ctx, CTX_ELR_EL2, read_elr_el2());
1213 write_ctx_reg(ctx, CTX_ESR_EL2, read_esr_el2());
1214 write_ctx_reg(ctx, CTX_FAR_EL2, read_far_el2());
1215 write_ctx_reg(ctx, CTX_HACR_EL2, read_hacr_el2());
1216 write_ctx_reg(ctx, CTX_HCR_EL2, read_hcr_el2());
1217 write_ctx_reg(ctx, CTX_HPFAR_EL2, read_hpfar_el2());
1218 write_ctx_reg(ctx, CTX_HSTR_EL2, read_hstr_el2());
Boyan Karatotevac58e572023-05-15 15:09:16 +01001219 write_ctx_reg(ctx, CTX_MAIR_EL2, read_mair_el2());
1220 write_ctx_reg(ctx, CTX_MDCR_EL2, read_mdcr_el2());
1221 write_ctx_reg(ctx, CTX_SCTLR_EL2, read_sctlr_el2());
1222 write_ctx_reg(ctx, CTX_SPSR_EL2, read_spsr_el2());
1223 write_ctx_reg(ctx, CTX_SP_EL2, read_sp_el2());
1224 write_ctx_reg(ctx, CTX_TCR_EL2, read_tcr_el2());
1225 write_ctx_reg(ctx, CTX_TPIDR_EL2, read_tpidr_el2());
1226 write_ctx_reg(ctx, CTX_TTBR0_EL2, read_ttbr0_el2());
1227 write_ctx_reg(ctx, CTX_VBAR_EL2, read_vbar_el2());
1228 write_ctx_reg(ctx, CTX_VMPIDR_EL2, read_vmpidr_el2());
1229 write_ctx_reg(ctx, CTX_VPIDR_EL2, read_vpidr_el2());
1230 write_ctx_reg(ctx, CTX_VTCR_EL2, read_vtcr_el2());
1231 write_ctx_reg(ctx, CTX_VTTBR_EL2, read_vttbr_el2());
1232}
1233
1234static void el2_sysregs_context_restore_common(el2_sysregs_t *ctx)
1235{
1236 write_actlr_el2(read_ctx_reg(ctx, CTX_ACTLR_EL2));
1237 write_afsr0_el2(read_ctx_reg(ctx, CTX_AFSR0_EL2));
1238 write_afsr1_el2(read_ctx_reg(ctx, CTX_AFSR1_EL2));
1239 write_amair_el2(read_ctx_reg(ctx, CTX_AMAIR_EL2));
1240 write_cnthctl_el2(read_ctx_reg(ctx, CTX_CNTHCTL_EL2));
1241 write_cntvoff_el2(read_ctx_reg(ctx, CTX_CNTVOFF_EL2));
1242 write_cptr_el2(read_ctx_reg(ctx, CTX_CPTR_EL2));
1243 if (CTX_INCLUDE_AARCH32_REGS) {
1244 write_dbgvcr32_el2(read_ctx_reg(ctx, CTX_DBGVCR32_EL2));
1245 }
1246 write_elr_el2(read_ctx_reg(ctx, CTX_ELR_EL2));
1247 write_esr_el2(read_ctx_reg(ctx, CTX_ESR_EL2));
1248 write_far_el2(read_ctx_reg(ctx, CTX_FAR_EL2));
1249 write_hacr_el2(read_ctx_reg(ctx, CTX_HACR_EL2));
1250 write_hcr_el2(read_ctx_reg(ctx, CTX_HCR_EL2));
1251 write_hpfar_el2(read_ctx_reg(ctx, CTX_HPFAR_EL2));
1252 write_hstr_el2(read_ctx_reg(ctx, CTX_HSTR_EL2));
Boyan Karatotevac58e572023-05-15 15:09:16 +01001253 write_mair_el2(read_ctx_reg(ctx, CTX_MAIR_EL2));
1254 write_mdcr_el2(read_ctx_reg(ctx, CTX_MDCR_EL2));
1255 write_sctlr_el2(read_ctx_reg(ctx, CTX_SCTLR_EL2));
1256 write_spsr_el2(read_ctx_reg(ctx, CTX_SPSR_EL2));
1257 write_sp_el2(read_ctx_reg(ctx, CTX_SP_EL2));
1258 write_tcr_el2(read_ctx_reg(ctx, CTX_TCR_EL2));
1259 write_tpidr_el2(read_ctx_reg(ctx, CTX_TPIDR_EL2));
1260 write_ttbr0_el2(read_ctx_reg(ctx, CTX_TTBR0_EL2));
1261 write_vbar_el2(read_ctx_reg(ctx, CTX_VBAR_EL2));
1262 write_vmpidr_el2(read_ctx_reg(ctx, CTX_VMPIDR_EL2));
1263 write_vpidr_el2(read_ctx_reg(ctx, CTX_VPIDR_EL2));
1264 write_vtcr_el2(read_ctx_reg(ctx, CTX_VTCR_EL2));
1265 write_vttbr_el2(read_ctx_reg(ctx, CTX_VTTBR_EL2));
1266}
1267
Max Shvetsov28f39f02020-02-25 13:56:19 +00001268/*******************************************************************************
1269 * Save EL2 sysreg context
1270 ******************************************************************************/
1271void cm_el2_sysregs_context_save(uint32_t security_state)
1272{
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001273 cpu_context_t *ctx;
1274 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +00001275
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001276 ctx = cm_get_context(security_state);
1277 assert(ctx != NULL);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001278
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001279 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001280
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001281 el2_sysregs_context_save_common(el2_sysregs_ctx);
Govindraj Raja57bdc572025-02-04 16:04:27 +01001282 el2_sysregs_context_save_gic(el2_sysregs_ctx, security_state);
Manish Pandeyf823cc72025-02-04 15:59:06 +01001283
Zelalem Aweked20052f2022-04-04 17:42:48 -05001284#if CTX_INCLUDE_MTE_REGS
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001285 write_ctx_reg(el2_sysregs_ctx, CTX_TFSR_EL2, read_tfsr_el2());
Zelalem Aweked20052f2022-04-04 17:42:48 -05001286#endif
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001287 if (is_feat_mpam_supported()) {
1288 el2_sysregs_context_save_mpam(el2_sysregs_ctx);
1289 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001290
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001291 if (is_feat_fgt_supported()) {
1292 el2_sysregs_context_save_fgt(el2_sysregs_ctx);
1293 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001294
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001295 if (is_feat_ecv_v2_supported()) {
1296 write_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2, read_cntpoff_el2());
1297 }
Andre Przywarab8f03d22022-11-17 17:30:43 +00001298
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001299 if (is_feat_vhe_supported()) {
1300 write_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2, read_contextidr_el2());
1301 write_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2, read_ttbr1_el2());
1302 }
Andre Przywara6503ff22023-01-27 12:25:49 +00001303
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001304 if (is_feat_ras_supported()) {
1305 write_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2, read_vdisr_el2());
1306 write_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2, read_vsesr_el2());
1307 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001308
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001309 if (is_feat_nv2_supported()) {
1310 write_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2, read_vncr_el2());
1311 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001312
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001313 if (is_feat_trf_supported()) {
1314 write_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2, read_trfcr_el2());
1315 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001316
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001317 if (is_feat_csv2_2_supported()) {
1318 write_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2, read_scxtnum_el2());
1319 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001320
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001321 if (is_feat_hcx_supported()) {
1322 write_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2, read_hcrx_el2());
1323 }
1324 if (is_feat_tcr2_supported()) {
1325 write_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2, read_tcr2_el2());
1326 }
1327 if (is_feat_sxpie_supported()) {
1328 write_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2, read_pire0_el2());
1329 write_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2, read_pir_el2());
1330 }
1331 if (is_feat_s2pie_supported()) {
1332 write_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2, read_s2pir_el2());
1333 }
1334 if (is_feat_sxpoe_supported()) {
1335 write_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2, read_por_el2());
1336 }
1337 if (is_feat_gcs_supported()) {
1338 write_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2, read_gcspr_el2());
1339 write_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2, read_gcscr_el2());
Max Shvetsov28f39f02020-02-25 13:56:19 +00001340 }
1341}
1342
1343/*******************************************************************************
1344 * Restore EL2 sysreg context
1345 ******************************************************************************/
1346void cm_el2_sysregs_context_restore(uint32_t security_state)
1347{
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001348 cpu_context_t *ctx;
1349 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +00001350
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001351 ctx = cm_get_context(security_state);
1352 assert(ctx != NULL);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001353
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001354 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
Max Shvetsov28f39f02020-02-25 13:56:19 +00001355
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001356 el2_sysregs_context_restore_common(el2_sysregs_ctx);
Govindraj Raja57bdc572025-02-04 16:04:27 +01001357 el2_sysregs_context_restore_gic(el2_sysregs_ctx, security_state);
Manish Pandeyf823cc72025-02-04 15:59:06 +01001358
Zelalem Aweked20052f2022-04-04 17:42:48 -05001359#if CTX_INCLUDE_MTE_REGS
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001360 write_tfsr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TFSR_EL2));
Zelalem Aweked20052f2022-04-04 17:42:48 -05001361#endif
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001362 if (is_feat_mpam_supported()) {
1363 el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
1364 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001365
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001366 if (is_feat_fgt_supported()) {
1367 el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
1368 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001369
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001370 if (is_feat_ecv_v2_supported()) {
1371 write_cntpoff_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2));
1372 }
Andre Przywarab8f03d22022-11-17 17:30:43 +00001373
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001374 if (is_feat_vhe_supported()) {
1375 write_contextidr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2));
1376 write_ttbr1_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2));
1377 }
Andre Przywara6503ff22023-01-27 12:25:49 +00001378
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001379 if (is_feat_ras_supported()) {
1380 write_vdisr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2));
1381 write_vsesr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2));
1382 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001383
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001384 if (is_feat_nv2_supported()) {
1385 write_vncr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2));
1386 }
1387 if (is_feat_trf_supported()) {
1388 write_trfcr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2));
1389 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001390
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001391 if (is_feat_csv2_2_supported()) {
1392 write_scxtnum_el2(read_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2));
1393 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001394
Boyan Karatotev5c52d7e2023-05-22 15:53:58 +01001395 if (is_feat_hcx_supported()) {
1396 write_hcrx_el2(read_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2));
1397 }
1398 if (is_feat_tcr2_supported()) {
1399 write_tcr2_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2));
1400 }
1401 if (is_feat_sxpie_supported()) {
1402 write_pire0_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2));
1403 write_pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2));
1404 }
1405 if (is_feat_s2pie_supported()) {
1406 write_s2pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2));
1407 }
1408 if (is_feat_sxpoe_supported()) {
1409 write_por_el2(read_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2));
1410 }
1411 if (is_feat_gcs_supported()) {
1412 write_gcscr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2));
1413 write_gcspr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2));
Max Shvetsov28f39f02020-02-25 13:56:19 +00001414 }
1415}
1416#endif /* CTX_INCLUDE_EL2_REGS */
1417
Andrew Thoelke167a9352014-06-04 21:10:52 +01001418/*******************************************************************************
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001419 * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
1420 * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
1421 * updating EL1 and EL2 registers. Otherwise, it calls the generic
1422 * cm_prepare_el3_exit function.
1423 ******************************************************************************/
1424void cm_prepare_el3_exit_ns(void)
1425{
Arvind Ram Prakashc4c6eae2024-08-05 16:11:42 -05001426#ifdef IMAGE_BL31
1427#if ERRATA_A520_2938996 || ERRATA_X4_2726228
Arvind Ram Prakashc4c6eae2024-08-05 16:11:42 -05001428 if (check_if_affected_core() == ERRATA_APPLIES) {
1429 if (is_feat_trbe_supported()) {
Arvind Ram Prakashc7d94652024-07-19 11:39:49 -05001430 trbe_disable();
Arvind Ram Prakashc4c6eae2024-08-05 16:11:42 -05001431 }
1432 }
1433#endif
1434#endif /* IMAGE_BL31 */
1435
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001436#if CTX_INCLUDE_EL2_REGS
Boyan Karatotev4085a022023-03-27 17:02:43 +01001437#if ENABLE_ASSERTIONS
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001438 cpu_context_t *ctx = cm_get_context(NON_SECURE);
1439 assert(ctx != NULL);
1440
Zelalem Awekeb515f542022-04-08 16:48:05 -05001441 /* Assert that EL2 is used. */
Boyan Karatotev4085a022023-03-27 17:02:43 +01001442 u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
Zelalem Awekeb515f542022-04-08 16:48:05 -05001443 assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
1444 (el_implemented(2U) != EL_IMPL_NONE));
Boyan Karatotev4085a022023-03-27 17:02:43 +01001445#endif /* ENABLE_ASSERTIONS */
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001446
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001447 /* Restore EL2 and EL1 sysreg contexts */
1448 cm_el2_sysregs_context_restore(NON_SECURE);
1449 cm_el1_sysregs_context_restore(NON_SECURE);
1450 cm_set_next_eret_context(NON_SECURE);
1451#else
1452 cm_prepare_el3_exit(NON_SECURE);
1453#endif /* CTX_INCLUDE_EL2_REGS */
1454}
1455
1456/*******************************************************************************
Soby Mathewfdfabec2014-07-04 16:02:26 +01001457 * The next four functions are used by runtime services to save and restore
1458 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +00001459 * state.
1460 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +00001461void cm_el1_sysregs_context_save(uint32_t security_state)
1462{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001463 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001464
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001465 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001466 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001467
Max Shvetsov28259462020-02-17 16:15:47 +00001468 el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +01001469
1470#if IMAGE_BL31
1471 if (security_state == SECURE)
1472 PUBLISH_EVENT(cm_exited_secure_world);
1473 else
1474 PUBLISH_EVENT(cm_exited_normal_world);
1475#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001476}
1477
1478void cm_el1_sysregs_context_restore(uint32_t security_state)
1479{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001480 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001481
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001482 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001483 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001484
Max Shvetsov28259462020-02-17 16:15:47 +00001485 el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +01001486
1487#if IMAGE_BL31
1488 if (security_state == SECURE)
1489 PUBLISH_EVENT(cm_entering_secure_world);
1490 else
1491 PUBLISH_EVENT(cm_entering_normal_world);
1492#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001493}
1494
1495/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001496 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
1497 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +00001498 ******************************************************************************/
Soby Mathew4c0d0392016-06-16 14:52:04 +01001499void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +00001500{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001501 cpu_context_t *ctx;
1502 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +00001503
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001504 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001505 assert(ctx != NULL);
Achin Gupta607084e2014-02-09 18:24:19 +00001506
1507 /* Populate EL3 state so that ERET jumps to the correct entry */
1508 state = get_el3state_ctx(ctx);
1509 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1510}
1511
1512/*******************************************************************************
Andrew Thoelke167a9352014-06-04 21:10:52 +01001513 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
1514 * pertaining to the given security state
1515 ******************************************************************************/
1516void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathew4c0d0392016-06-16 14:52:04 +01001517 uintptr_t entrypoint, uint32_t spsr)
Andrew Thoelke167a9352014-06-04 21:10:52 +01001518{
1519 cpu_context_t *ctx;
1520 el3_state_t *state;
1521
1522 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001523 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +01001524
1525 /* Populate EL3 state so that ERET jumps to the correct entry */
1526 state = get_el3state_ctx(ctx);
1527 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1528 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
1529}
1530
1531/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001532 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
1533 * pertaining to the given security state using the value and bit position
1534 * specified in the parameters. It preserves all other bits.
1535 ******************************************************************************/
1536void cm_write_scr_el3_bit(uint32_t security_state,
1537 uint32_t bit_pos,
1538 uint32_t value)
1539{
1540 cpu_context_t *ctx;
1541 el3_state_t *state;
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001542 u_register_t scr_el3;
Achin Guptac429b5e2014-05-04 18:38:28 +01001543
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001544 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001545 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001546
1547 /* Ensure that the bit position is a valid one */
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001548 assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001549
1550 /* Ensure that the 'value' is only a bit wide */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001551 assert(value <= 1U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001552
1553 /*
1554 * Get the SCR_EL3 value from the cpu context, clear the desired bit
1555 * and set it to its new value.
1556 */
1557 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001558 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001559 scr_el3 &= ~(1UL << bit_pos);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001560 scr_el3 |= (u_register_t)value << bit_pos;
Achin Guptac429b5e2014-05-04 18:38:28 +01001561 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
1562}
1563
1564/*******************************************************************************
1565 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
1566 * given security state.
1567 ******************************************************************************/
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001568u_register_t cm_get_scr_el3(uint32_t security_state)
Achin Guptac429b5e2014-05-04 18:38:28 +01001569{
1570 cpu_context_t *ctx;
1571 el3_state_t *state;
1572
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001573 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001574 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001575
1576 /* Populate EL3 state so that ERET jumps to the correct entry */
1577 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001578 return read_ctx_reg(state, CTX_SCR_EL3);
Achin Guptac429b5e2014-05-04 18:38:28 +01001579}
1580
1581/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001582 * This function is used to program the context that's used for exception
1583 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
1584 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +00001585 ******************************************************************************/
1586void cm_set_next_eret_context(uint32_t security_state)
1587{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001588 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001589
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001590 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001591 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001592
Andrew Thoelke167a9352014-06-04 21:10:52 +01001593 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +00001594}