1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-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 #include <string.h>
9*54fd6939SJiyong Park
10*54fd6939SJiyong Park #include <arch.h>
11*54fd6939SJiyong Park #include <arch_features.h>
12*54fd6939SJiyong Park #include <arch_helpers.h>
13*54fd6939SJiyong Park #include <bl31/bl31.h>
14*54fd6939SJiyong Park #include <bl31/ehf.h>
15*54fd6939SJiyong Park #include <common/bl_common.h>
16*54fd6939SJiyong Park #include <common/debug.h>
17*54fd6939SJiyong Park #include <common/runtime_svc.h>
18*54fd6939SJiyong Park #include <drivers/console.h>
19*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
20*54fd6939SJiyong Park #include <lib/pmf/pmf.h>
21*54fd6939SJiyong Park #include <lib/runtime_instr.h>
22*54fd6939SJiyong Park #include <plat/common/platform.h>
23*54fd6939SJiyong Park #include <services/std_svc.h>
24*54fd6939SJiyong Park
25*54fd6939SJiyong Park #if ENABLE_RUNTIME_INSTRUMENTATION
26*54fd6939SJiyong Park PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
27*54fd6939SJiyong Park RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
28*54fd6939SJiyong Park #endif
29*54fd6939SJiyong Park
30*54fd6939SJiyong Park /*******************************************************************************
31*54fd6939SJiyong Park * This function pointer is used to initialise the BL32 image. It's initialized
32*54fd6939SJiyong Park * by SPD calling bl31_register_bl32_init after setting up all things necessary
33*54fd6939SJiyong Park * for SP execution. In cases where both SPD and SP are absent, or when SPD
34*54fd6939SJiyong Park * finds it impossible to execute SP, this pointer is left as NULL
35*54fd6939SJiyong Park ******************************************************************************/
36*54fd6939SJiyong Park static int32_t (*bl32_init)(void);
37*54fd6939SJiyong Park
38*54fd6939SJiyong Park /*****************************************************************************
39*54fd6939SJiyong Park * Function used to initialise RMM if RME is enabled
40*54fd6939SJiyong Park *****************************************************************************/
41*54fd6939SJiyong Park #if ENABLE_RME
42*54fd6939SJiyong Park static int32_t (*rmm_init)(void);
43*54fd6939SJiyong Park #endif
44*54fd6939SJiyong Park
45*54fd6939SJiyong Park /*******************************************************************************
46*54fd6939SJiyong Park * Variable to indicate whether next image to execute after BL31 is BL33
47*54fd6939SJiyong Park * (non-secure & default) or BL32 (secure).
48*54fd6939SJiyong Park ******************************************************************************/
49*54fd6939SJiyong Park static uint32_t next_image_type = NON_SECURE;
50*54fd6939SJiyong Park
51*54fd6939SJiyong Park #ifdef SUPPORT_UNKNOWN_MPID
52*54fd6939SJiyong Park /*
53*54fd6939SJiyong Park * Flag to know whether an unsupported MPID has been detected. To avoid having it
54*54fd6939SJiyong Park * landing on the .bss section, it is initialized to a non-zero value, this way
55*54fd6939SJiyong Park * we avoid potential WAW hazards during system bring up.
56*54fd6939SJiyong Park * */
57*54fd6939SJiyong Park volatile uint32_t unsupported_mpid_flag = 1;
58*54fd6939SJiyong Park #endif
59*54fd6939SJiyong Park
60*54fd6939SJiyong Park /*
61*54fd6939SJiyong Park * Implement the ARM Standard Service function to get arguments for a
62*54fd6939SJiyong Park * particular service.
63*54fd6939SJiyong Park */
get_arm_std_svc_args(unsigned int svc_mask)64*54fd6939SJiyong Park uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
65*54fd6939SJiyong Park {
66*54fd6939SJiyong Park /* Setup the arguments for PSCI Library */
67*54fd6939SJiyong Park DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, bl31_warm_entrypoint);
68*54fd6939SJiyong Park
69*54fd6939SJiyong Park /* PSCI is the only ARM Standard Service implemented */
70*54fd6939SJiyong Park assert(svc_mask == PSCI_FID_MASK);
71*54fd6939SJiyong Park
72*54fd6939SJiyong Park return (uintptr_t)&psci_args;
73*54fd6939SJiyong Park }
74*54fd6939SJiyong Park
75*54fd6939SJiyong Park /*******************************************************************************
76*54fd6939SJiyong Park * Simple function to initialise all BL31 helper libraries.
77*54fd6939SJiyong Park ******************************************************************************/
bl31_lib_init(void)78*54fd6939SJiyong Park void __init bl31_lib_init(void)
79*54fd6939SJiyong Park {
80*54fd6939SJiyong Park cm_init();
81*54fd6939SJiyong Park }
82*54fd6939SJiyong Park
83*54fd6939SJiyong Park /*******************************************************************************
84*54fd6939SJiyong Park * Setup function for BL31.
85*54fd6939SJiyong Park ******************************************************************************/
bl31_setup(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)86*54fd6939SJiyong Park void bl31_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
87*54fd6939SJiyong Park u_register_t arg3)
88*54fd6939SJiyong Park {
89*54fd6939SJiyong Park /* Perform early platform-specific setup */
90*54fd6939SJiyong Park bl31_early_platform_setup2(arg0, arg1, arg2, arg3);
91*54fd6939SJiyong Park
92*54fd6939SJiyong Park /* Perform late platform-specific setup */
93*54fd6939SJiyong Park bl31_plat_arch_setup();
94*54fd6939SJiyong Park
95*54fd6939SJiyong Park #if ENABLE_FEAT_HCX
96*54fd6939SJiyong Park /*
97*54fd6939SJiyong Park * Assert that FEAT_HCX is supported on this system, without this check
98*54fd6939SJiyong Park * an exception would occur during context save/restore if enabled but
99*54fd6939SJiyong Park * not supported.
100*54fd6939SJiyong Park */
101*54fd6939SJiyong Park assert(is_feat_hcx_present());
102*54fd6939SJiyong Park #endif /* ENABLE_FEAT_HCX */
103*54fd6939SJiyong Park
104*54fd6939SJiyong Park #if CTX_INCLUDE_PAUTH_REGS
105*54fd6939SJiyong Park /*
106*54fd6939SJiyong Park * Assert that the ARMv8.3-PAuth registers are present or an access
107*54fd6939SJiyong Park * fault will be triggered when they are being saved or restored.
108*54fd6939SJiyong Park */
109*54fd6939SJiyong Park assert(is_armv8_3_pauth_present());
110*54fd6939SJiyong Park #endif /* CTX_INCLUDE_PAUTH_REGS */
111*54fd6939SJiyong Park }
112*54fd6939SJiyong Park
113*54fd6939SJiyong Park /*******************************************************************************
114*54fd6939SJiyong Park * BL31 is responsible for setting up the runtime services for the primary cpu
115*54fd6939SJiyong Park * before passing control to the bootloader or an Operating System. This
116*54fd6939SJiyong Park * function calls runtime_svc_init() which initializes all registered runtime
117*54fd6939SJiyong Park * services. The run time services would setup enough context for the core to
118*54fd6939SJiyong Park * switch to the next exception level. When this function returns, the core will
119*54fd6939SJiyong Park * switch to the programmed exception level via an ERET.
120*54fd6939SJiyong Park ******************************************************************************/
bl31_main(void)121*54fd6939SJiyong Park void bl31_main(void)
122*54fd6939SJiyong Park {
123*54fd6939SJiyong Park NOTICE("BL31: %s\n", version_string);
124*54fd6939SJiyong Park NOTICE("BL31: %s\n", build_message);
125*54fd6939SJiyong Park
126*54fd6939SJiyong Park #ifdef SUPPORT_UNKNOWN_MPID
127*54fd6939SJiyong Park if (unsupported_mpid_flag == 0) {
128*54fd6939SJiyong Park NOTICE("Unsupported MPID detected!\n");
129*54fd6939SJiyong Park }
130*54fd6939SJiyong Park #endif
131*54fd6939SJiyong Park
132*54fd6939SJiyong Park /* Perform platform setup in BL31 */
133*54fd6939SJiyong Park bl31_platform_setup();
134*54fd6939SJiyong Park
135*54fd6939SJiyong Park /* Initialise helper libraries */
136*54fd6939SJiyong Park bl31_lib_init();
137*54fd6939SJiyong Park
138*54fd6939SJiyong Park #if EL3_EXCEPTION_HANDLING
139*54fd6939SJiyong Park INFO("BL31: Initialising Exception Handling Framework\n");
140*54fd6939SJiyong Park ehf_init();
141*54fd6939SJiyong Park #endif
142*54fd6939SJiyong Park
143*54fd6939SJiyong Park /* Initialize the runtime services e.g. psci. */
144*54fd6939SJiyong Park INFO("BL31: Initializing runtime services\n");
145*54fd6939SJiyong Park runtime_svc_init();
146*54fd6939SJiyong Park
147*54fd6939SJiyong Park /*
148*54fd6939SJiyong Park * All the cold boot actions on the primary cpu are done. We now need to
149*54fd6939SJiyong Park * decide which is the next image and how to execute it.
150*54fd6939SJiyong Park * If the SPD runtime service is present, it would want to pass control
151*54fd6939SJiyong Park * to BL32 first in S-EL1. In that case, SPD would have registered a
152*54fd6939SJiyong Park * function to initialize bl32 where it takes responsibility of entering
153*54fd6939SJiyong Park * S-EL1 and returning control back to bl31_main. Similarly, if RME is
154*54fd6939SJiyong Park * enabled and a function is registered to initialize RMM, control is
155*54fd6939SJiyong Park * transferred to RMM in R-EL2. After RMM initialization, control is
156*54fd6939SJiyong Park * returned back to bl31_main. Once this is done we can prepare entry
157*54fd6939SJiyong Park * into BL33 as normal.
158*54fd6939SJiyong Park */
159*54fd6939SJiyong Park
160*54fd6939SJiyong Park /*
161*54fd6939SJiyong Park * If SPD had registered an init hook, invoke it.
162*54fd6939SJiyong Park */
163*54fd6939SJiyong Park if (bl32_init != NULL) {
164*54fd6939SJiyong Park INFO("BL31: Initializing BL32\n");
165*54fd6939SJiyong Park
166*54fd6939SJiyong Park int32_t rc = (*bl32_init)();
167*54fd6939SJiyong Park
168*54fd6939SJiyong Park if (rc == 0) {
169*54fd6939SJiyong Park WARN("BL31: BL32 initialization failed\n");
170*54fd6939SJiyong Park }
171*54fd6939SJiyong Park }
172*54fd6939SJiyong Park
173*54fd6939SJiyong Park /*
174*54fd6939SJiyong Park * If RME is enabled and init hook is registered, initialize RMM
175*54fd6939SJiyong Park * in R-EL2.
176*54fd6939SJiyong Park */
177*54fd6939SJiyong Park #if ENABLE_RME
178*54fd6939SJiyong Park if (rmm_init != NULL) {
179*54fd6939SJiyong Park INFO("BL31: Initializing RMM\n");
180*54fd6939SJiyong Park
181*54fd6939SJiyong Park int32_t rc = (*rmm_init)();
182*54fd6939SJiyong Park
183*54fd6939SJiyong Park if (rc == 0) {
184*54fd6939SJiyong Park WARN("BL31: RMM initialization failed\n");
185*54fd6939SJiyong Park }
186*54fd6939SJiyong Park }
187*54fd6939SJiyong Park #endif
188*54fd6939SJiyong Park
189*54fd6939SJiyong Park /*
190*54fd6939SJiyong Park * We are ready to enter the next EL. Prepare entry into the image
191*54fd6939SJiyong Park * corresponding to the desired security state after the next ERET.
192*54fd6939SJiyong Park */
193*54fd6939SJiyong Park bl31_prepare_next_image_entry();
194*54fd6939SJiyong Park
195*54fd6939SJiyong Park console_flush();
196*54fd6939SJiyong Park
197*54fd6939SJiyong Park /*
198*54fd6939SJiyong Park * Perform any platform specific runtime setup prior to cold boot exit
199*54fd6939SJiyong Park * from BL31
200*54fd6939SJiyong Park */
201*54fd6939SJiyong Park bl31_plat_runtime_setup();
202*54fd6939SJiyong Park }
203*54fd6939SJiyong Park
204*54fd6939SJiyong Park /*******************************************************************************
205*54fd6939SJiyong Park * Accessor functions to help runtime services decide which image should be
206*54fd6939SJiyong Park * executed after BL31. This is BL33 or the non-secure bootloader image by
207*54fd6939SJiyong Park * default but the Secure payload dispatcher could override this by requesting
208*54fd6939SJiyong Park * an entry into BL32 (Secure payload) first. If it does so then it should use
209*54fd6939SJiyong Park * the same API to program an entry into BL33 once BL32 initialisation is
210*54fd6939SJiyong Park * complete.
211*54fd6939SJiyong Park ******************************************************************************/
bl31_set_next_image_type(uint32_t security_state)212*54fd6939SJiyong Park void bl31_set_next_image_type(uint32_t security_state)
213*54fd6939SJiyong Park {
214*54fd6939SJiyong Park assert(sec_state_is_valid(security_state));
215*54fd6939SJiyong Park next_image_type = security_state;
216*54fd6939SJiyong Park }
217*54fd6939SJiyong Park
bl31_get_next_image_type(void)218*54fd6939SJiyong Park uint32_t bl31_get_next_image_type(void)
219*54fd6939SJiyong Park {
220*54fd6939SJiyong Park return next_image_type;
221*54fd6939SJiyong Park }
222*54fd6939SJiyong Park
223*54fd6939SJiyong Park /*******************************************************************************
224*54fd6939SJiyong Park * This function programs EL3 registers and performs other setup to enable entry
225*54fd6939SJiyong Park * into the next image after BL31 at the next ERET.
226*54fd6939SJiyong Park ******************************************************************************/
bl31_prepare_next_image_entry(void)227*54fd6939SJiyong Park void __init bl31_prepare_next_image_entry(void)
228*54fd6939SJiyong Park {
229*54fd6939SJiyong Park entry_point_info_t *next_image_info;
230*54fd6939SJiyong Park uint32_t image_type;
231*54fd6939SJiyong Park
232*54fd6939SJiyong Park #if CTX_INCLUDE_AARCH32_REGS
233*54fd6939SJiyong Park /*
234*54fd6939SJiyong Park * Ensure that the build flag to save AArch32 system registers in CPU
235*54fd6939SJiyong Park * context is not set for AArch64-only platforms.
236*54fd6939SJiyong Park */
237*54fd6939SJiyong Park if (el_implemented(1) == EL_IMPL_A64ONLY) {
238*54fd6939SJiyong Park ERROR("EL1 supports AArch64-only. Please set build flag "
239*54fd6939SJiyong Park "CTX_INCLUDE_AARCH32_REGS = 0\n");
240*54fd6939SJiyong Park panic();
241*54fd6939SJiyong Park }
242*54fd6939SJiyong Park #endif
243*54fd6939SJiyong Park
244*54fd6939SJiyong Park /* Determine which image to execute next */
245*54fd6939SJiyong Park image_type = bl31_get_next_image_type();
246*54fd6939SJiyong Park
247*54fd6939SJiyong Park /* Program EL3 registers to enable entry into the next EL */
248*54fd6939SJiyong Park next_image_info = bl31_plat_get_next_image_ep_info(image_type);
249*54fd6939SJiyong Park assert(next_image_info != NULL);
250*54fd6939SJiyong Park assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr));
251*54fd6939SJiyong Park
252*54fd6939SJiyong Park INFO("BL31: Preparing for EL3 exit to %s world\n",
253*54fd6939SJiyong Park (image_type == SECURE) ? "secure" : "normal");
254*54fd6939SJiyong Park print_entry_point_info(next_image_info);
255*54fd6939SJiyong Park cm_init_my_context(next_image_info);
256*54fd6939SJiyong Park cm_prepare_el3_exit(image_type);
257*54fd6939SJiyong Park }
258*54fd6939SJiyong Park
259*54fd6939SJiyong Park /*******************************************************************************
260*54fd6939SJiyong Park * This function initializes the pointer to BL32 init function. This is expected
261*54fd6939SJiyong Park * to be called by the SPD after it finishes all its initialization
262*54fd6939SJiyong Park ******************************************************************************/
bl31_register_bl32_init(int32_t (* func)(void))263*54fd6939SJiyong Park void bl31_register_bl32_init(int32_t (*func)(void))
264*54fd6939SJiyong Park {
265*54fd6939SJiyong Park bl32_init = func;
266*54fd6939SJiyong Park }
267*54fd6939SJiyong Park
268*54fd6939SJiyong Park #if ENABLE_RME
269*54fd6939SJiyong Park /*******************************************************************************
270*54fd6939SJiyong Park * This function initializes the pointer to RMM init function. This is expected
271*54fd6939SJiyong Park * to be called by the RMMD after it finishes all its initialization
272*54fd6939SJiyong Park ******************************************************************************/
bl31_register_rmm_init(int32_t (* func)(void))273*54fd6939SJiyong Park void bl31_register_rmm_init(int32_t (*func)(void))
274*54fd6939SJiyong Park {
275*54fd6939SJiyong Park rmm_init = func;
276*54fd6939SJiyong Park }
277*54fd6939SJiyong Park #endif
278