xref: /aosp_15_r20/external/arm-trusted-firmware/bl1/aarch64/bl1_context_mgmt.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2015-2021, Arm Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park 
9*54fd6939SJiyong Park #include <arch_helpers.h>
10*54fd6939SJiyong Park #include <context.h>
11*54fd6939SJiyong Park #include <common/debug.h>
12*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
13*54fd6939SJiyong Park #include <plat/common/platform.h>
14*54fd6939SJiyong Park 
15*54fd6939SJiyong Park #include "../bl1_private.h"
16*54fd6939SJiyong Park 
17*54fd6939SJiyong Park /* Following contains the cpu context pointers. */
18*54fd6939SJiyong Park static void *bl1_cpu_context_ptr[2];
19*54fd6939SJiyong Park entry_point_info_t *bl2_ep_info;
20*54fd6939SJiyong Park 
21*54fd6939SJiyong Park 
cm_get_context(uint32_t security_state)22*54fd6939SJiyong Park void *cm_get_context(uint32_t security_state)
23*54fd6939SJiyong Park {
24*54fd6939SJiyong Park 	assert(sec_state_is_valid(security_state));
25*54fd6939SJiyong Park 	return bl1_cpu_context_ptr[security_state];
26*54fd6939SJiyong Park }
27*54fd6939SJiyong Park 
cm_set_context(void * context,uint32_t security_state)28*54fd6939SJiyong Park void cm_set_context(void *context, uint32_t security_state)
29*54fd6939SJiyong Park {
30*54fd6939SJiyong Park 	assert(sec_state_is_valid(security_state));
31*54fd6939SJiyong Park 	bl1_cpu_context_ptr[security_state] = context;
32*54fd6939SJiyong Park }
33*54fd6939SJiyong Park 
34*54fd6939SJiyong Park #if ENABLE_RME
35*54fd6939SJiyong Park /*******************************************************************************
36*54fd6939SJiyong Park  * This function prepares the entry point information to run BL2 in Root world,
37*54fd6939SJiyong Park  * i.e. EL3, for the case when FEAT_RME is enabled.
38*54fd6939SJiyong Park  ******************************************************************************/
bl1_prepare_next_image(unsigned int image_id)39*54fd6939SJiyong Park void bl1_prepare_next_image(unsigned int image_id)
40*54fd6939SJiyong Park {
41*54fd6939SJiyong Park 	image_desc_t *bl2_desc;
42*54fd6939SJiyong Park 
43*54fd6939SJiyong Park 	assert(image_id == BL2_IMAGE_ID);
44*54fd6939SJiyong Park 
45*54fd6939SJiyong Park 	/* Get the image descriptor. */
46*54fd6939SJiyong Park 	bl2_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
47*54fd6939SJiyong Park 	assert(bl2_desc != NULL);
48*54fd6939SJiyong Park 
49*54fd6939SJiyong Park 	/* Get the entry point info. */
50*54fd6939SJiyong Park 	bl2_ep_info = &bl2_desc->ep_info;
51*54fd6939SJiyong Park 
52*54fd6939SJiyong Park 	bl2_ep_info->spsr = (uint32_t)SPSR_64(MODE_EL3, MODE_SP_ELX,
53*54fd6939SJiyong Park 						DISABLE_ALL_EXCEPTIONS);
54*54fd6939SJiyong Park 
55*54fd6939SJiyong Park 	/*
56*54fd6939SJiyong Park 	 * Flush cache since bl2_ep_info is accessed after MMU is disabled
57*54fd6939SJiyong Park 	 * before jumping to BL2.
58*54fd6939SJiyong Park 	 */
59*54fd6939SJiyong Park 	flush_dcache_range((uintptr_t)bl2_ep_info, sizeof(entry_point_info_t));
60*54fd6939SJiyong Park 
61*54fd6939SJiyong Park 	/* Indicate that image is in execution state. */
62*54fd6939SJiyong Park 	bl2_desc->state = IMAGE_STATE_EXECUTED;
63*54fd6939SJiyong Park 
64*54fd6939SJiyong Park 	/* Print debug info and flush the console before running BL2. */
65*54fd6939SJiyong Park 	print_entry_point_info(bl2_ep_info);
66*54fd6939SJiyong Park }
67*54fd6939SJiyong Park #else
68*54fd6939SJiyong Park /*******************************************************************************
69*54fd6939SJiyong Park  * This function prepares the context for Secure/Normal world images.
70*54fd6939SJiyong Park  * Normal world images are transitioned to EL2(if supported) else EL1.
71*54fd6939SJiyong Park  ******************************************************************************/
bl1_prepare_next_image(unsigned int image_id)72*54fd6939SJiyong Park void bl1_prepare_next_image(unsigned int image_id)
73*54fd6939SJiyong Park {
74*54fd6939SJiyong Park 
75*54fd6939SJiyong Park 	/*
76*54fd6939SJiyong Park 	 * Following array will be used for context management.
77*54fd6939SJiyong Park 	 * There are 2 instances, for the Secure and Non-Secure contexts.
78*54fd6939SJiyong Park 	 */
79*54fd6939SJiyong Park 	static cpu_context_t bl1_cpu_context[2];
80*54fd6939SJiyong Park 
81*54fd6939SJiyong Park 	unsigned int security_state, mode = MODE_EL1;
82*54fd6939SJiyong Park 	image_desc_t *desc;
83*54fd6939SJiyong Park 	entry_point_info_t *next_bl_ep;
84*54fd6939SJiyong Park 
85*54fd6939SJiyong Park #if CTX_INCLUDE_AARCH32_REGS
86*54fd6939SJiyong Park 	/*
87*54fd6939SJiyong Park 	 * Ensure that the build flag to save AArch32 system registers in CPU
88*54fd6939SJiyong Park 	 * context is not set for AArch64-only platforms.
89*54fd6939SJiyong Park 	 */
90*54fd6939SJiyong Park 	if (el_implemented(1) == EL_IMPL_A64ONLY) {
91*54fd6939SJiyong Park 		ERROR("EL1 supports AArch64-only. Please set build flag "
92*54fd6939SJiyong Park 				"CTX_INCLUDE_AARCH32_REGS = 0\n");
93*54fd6939SJiyong Park 		panic();
94*54fd6939SJiyong Park 	}
95*54fd6939SJiyong Park #endif
96*54fd6939SJiyong Park 
97*54fd6939SJiyong Park 	/* Get the image descriptor. */
98*54fd6939SJiyong Park 	desc = bl1_plat_get_image_desc(image_id);
99*54fd6939SJiyong Park 	assert(desc != NULL);
100*54fd6939SJiyong Park 
101*54fd6939SJiyong Park 	/* Get the entry point info. */
102*54fd6939SJiyong Park 	next_bl_ep = &desc->ep_info;
103*54fd6939SJiyong Park 
104*54fd6939SJiyong Park 	/* Get the image security state. */
105*54fd6939SJiyong Park 	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
106*54fd6939SJiyong Park 
107*54fd6939SJiyong Park 	/* Setup the Secure/Non-Secure context if not done already. */
108*54fd6939SJiyong Park 	if (cm_get_context(security_state) == NULL)
109*54fd6939SJiyong Park 		cm_set_context(&bl1_cpu_context[security_state], security_state);
110*54fd6939SJiyong Park 
111*54fd6939SJiyong Park 	/* Prepare the SPSR for the next BL image. */
112*54fd6939SJiyong Park 	if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) {
113*54fd6939SJiyong Park 		mode = MODE_EL2;
114*54fd6939SJiyong Park 	}
115*54fd6939SJiyong Park 
116*54fd6939SJiyong Park 	next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
117*54fd6939SJiyong Park 		(uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
118*54fd6939SJiyong Park 
119*54fd6939SJiyong Park 	/* Allow platform to make change */
120*54fd6939SJiyong Park 	bl1_plat_set_ep_info(image_id, next_bl_ep);
121*54fd6939SJiyong Park 
122*54fd6939SJiyong Park 	/* Prepare the context for the next BL image. */
123*54fd6939SJiyong Park 	cm_init_my_context(next_bl_ep);
124*54fd6939SJiyong Park 	cm_prepare_el3_exit(security_state);
125*54fd6939SJiyong Park 
126*54fd6939SJiyong Park 	/* Indicate that image is in execution state. */
127*54fd6939SJiyong Park 	desc->state = IMAGE_STATE_EXECUTED;
128*54fd6939SJiyong Park 
129*54fd6939SJiyong Park 	print_entry_point_info(next_bl_ep);
130*54fd6939SJiyong Park }
131*54fd6939SJiyong Park #endif /* ENABLE_RME */
132