xref: /rk3399_ARM-atf/bl31/bl31_main.c (revision ed8f06ddda52bc0333f79e9ff798419e67771ae5)
14f6ad66aSAchin Gupta /*
2*ed8f06ddSthagon01-arm  * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta  */
64f6ad66aSAchin Gupta 
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <string.h>
909d40e0eSAntonio Nino Diaz 
1097043ac9SDan Handley #include <arch.h>
11ed108b56SAlexei Fedorov #include <arch_features.h>
124f6ad66aSAchin Gupta #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <bl31/bl31.h>
1409d40e0eSAntonio Nino Diaz #include <bl31/ehf.h>
1509d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1609d40e0eSAntonio Nino Diaz #include <common/debug.h>
176a0da736SJayanth Dodderi Chidanand #include <common/feat_detect.h>
1809d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
1909d40e0eSAntonio Nino Diaz #include <drivers/console.h>
20*ed8f06ddSthagon01-arm #include <lib/bootmarker_capture.h>
2109d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
2209d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
2309d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
2409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2509d40e0eSAntonio Nino Diaz #include <services/std_svc.h>
267aea9087SAchin Gupta 
27872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
28872be88aSdp-arm 	PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
29872be88aSdp-arm 		RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
30872be88aSdp-arm #endif
31872be88aSdp-arm 
32*ed8f06ddSthagon01-arm #if ENABLE_RUNTIME_INSTRUMENTATION
33*ed8f06ddSthagon01-arm 	PMF_REGISTER_SERVICE(bl_svc, PMF_RT_INSTR_SVC_ID,
34*ed8f06ddSthagon01-arm 		BL_TOTAL_IDS, PMF_DUMP_ENABLE)
35*ed8f06ddSthagon01-arm #endif
36*ed8f06ddSthagon01-arm 
377f366605SJeenu Viswambharan /*******************************************************************************
387f366605SJeenu Viswambharan  * This function pointer is used to initialise the BL32 image. It's initialized
397f366605SJeenu Viswambharan  * by SPD calling bl31_register_bl32_init after setting up all things necessary
407f366605SJeenu Viswambharan  * for SP execution. In cases where both SPD and SP are absent, or when SPD
417f366605SJeenu Viswambharan  * finds it impossible to execute SP, this pointer is left as NULL
427f366605SJeenu Viswambharan  ******************************************************************************/
436871c5d3SVikram Kanigiri static int32_t (*bl32_init)(void);
447f366605SJeenu Viswambharan 
455b18de09SZelalem Aweke /*****************************************************************************
465b18de09SZelalem Aweke  * Function used to initialise RMM if RME is enabled
475b18de09SZelalem Aweke  *****************************************************************************/
485b18de09SZelalem Aweke #if ENABLE_RME
495b18de09SZelalem Aweke static int32_t (*rmm_init)(void);
505b18de09SZelalem Aweke #endif
515b18de09SZelalem Aweke 
527aea9087SAchin Gupta /*******************************************************************************
5335ca3511SAchin Gupta  * Variable to indicate whether next image to execute after BL31 is BL33
5435ca3511SAchin Gupta  * (non-secure & default) or BL32 (secure).
5535ca3511SAchin Gupta  ******************************************************************************/
56faaa2e76SVikram Kanigiri static uint32_t next_image_type = NON_SECURE;
5735ca3511SAchin Gupta 
581994e562SJavier Almansa Sobrino #ifdef SUPPORT_UNKNOWN_MPID
591994e562SJavier Almansa Sobrino /*
601994e562SJavier Almansa Sobrino  * Flag to know whether an unsupported MPID has been detected. To avoid having it
611994e562SJavier Almansa Sobrino  * landing on the .bss section, it is initialized to a non-zero value, this way
621994e562SJavier Almansa Sobrino  * we avoid potential WAW hazards during system bring up.
631994e562SJavier Almansa Sobrino  * */
641994e562SJavier Almansa Sobrino volatile uint32_t unsupported_mpid_flag = 1;
651994e562SJavier Almansa Sobrino #endif
661994e562SJavier Almansa Sobrino 
6758e946aeSSoby Mathew /*
6858e946aeSSoby Mathew  * Implement the ARM Standard Service function to get arguments for a
6958e946aeSSoby Mathew  * particular service.
7058e946aeSSoby Mathew  */
7158e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
7258e946aeSSoby Mathew {
7358e946aeSSoby Mathew 	/* Setup the arguments for PSCI Library */
7458e946aeSSoby Mathew 	DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, bl31_warm_entrypoint);
7558e946aeSSoby Mathew 
7658e946aeSSoby Mathew 	/* PSCI is the only ARM Standard Service implemented */
7758e946aeSSoby Mathew 	assert(svc_mask == PSCI_FID_MASK);
7858e946aeSSoby Mathew 
7958e946aeSSoby Mathew 	return (uintptr_t)&psci_args;
8058e946aeSSoby Mathew }
8158e946aeSSoby Mathew 
8235ca3511SAchin Gupta /*******************************************************************************
837aea9087SAchin Gupta  * Simple function to initialise all BL31 helper libraries.
847aea9087SAchin Gupta  ******************************************************************************/
8587c85134SDaniel Boulby void __init bl31_lib_init(void)
867aea9087SAchin Gupta {
877aea9087SAchin Gupta 	cm_init();
887aea9087SAchin Gupta }
894f6ad66aSAchin Gupta 
904f6ad66aSAchin Gupta /*******************************************************************************
9188cfd9a6SAntonio Nino Diaz  * Setup function for BL31.
9288cfd9a6SAntonio Nino Diaz  ******************************************************************************/
9388cfd9a6SAntonio Nino Diaz void bl31_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
9488cfd9a6SAntonio Nino Diaz 		u_register_t arg3)
9588cfd9a6SAntonio Nino Diaz {
9688cfd9a6SAntonio Nino Diaz 	/* Perform early platform-specific setup */
9788cfd9a6SAntonio Nino Diaz 	bl31_early_platform_setup2(arg0, arg1, arg2, arg3);
9888cfd9a6SAntonio Nino Diaz 
9988cfd9a6SAntonio Nino Diaz 	/* Perform late platform-specific setup */
10088cfd9a6SAntonio Nino Diaz 	bl31_plat_arch_setup();
101ed108b56SAlexei Fedorov 
102ed108b56SAlexei Fedorov #if CTX_INCLUDE_PAUTH_REGS
103ed108b56SAlexei Fedorov 	/*
104ed108b56SAlexei Fedorov 	 * Assert that the ARMv8.3-PAuth registers are present or an access
105ed108b56SAlexei Fedorov 	 * fault will be triggered when they are being saved or restored.
106ed108b56SAlexei Fedorov 	 */
107ed108b56SAlexei Fedorov 	assert(is_armv8_3_pauth_present());
108ed108b56SAlexei Fedorov #endif /* CTX_INCLUDE_PAUTH_REGS */
10988cfd9a6SAntonio Nino Diaz }
11088cfd9a6SAntonio Nino Diaz 
11188cfd9a6SAntonio Nino Diaz /*******************************************************************************
1124f6ad66aSAchin Gupta  * BL31 is responsible for setting up the runtime services for the primary cpu
11335ca3511SAchin Gupta  * before passing control to the bootloader or an Operating System. This
11435ca3511SAchin Gupta  * function calls runtime_svc_init() which initializes all registered runtime
11535ca3511SAchin Gupta  * services. The run time services would setup enough context for the core to
1168aabea33SPaul Beesley  * switch to the next exception level. When this function returns, the core will
11773308618SAntonio Nino Diaz  * switch to the programmed exception level via an ERET.
1184f6ad66aSAchin Gupta  ******************************************************************************/
1194f6ad66aSAchin Gupta void bl31_main(void)
1204f6ad66aSAchin Gupta {
12124a70738SBoyan Karatotev 	/* Init registers that never change for the lifetime of TF-A */
12224a70738SBoyan Karatotev 	cm_manage_extensions_el3();
12324a70738SBoyan Karatotev 
124d178637dSJuan Castillo 	NOTICE("BL31: %s\n", version_string);
125d178637dSJuan Castillo 	NOTICE("BL31: %s\n", build_message);
1266ad2e461SDan Handley 
1276a0da736SJayanth Dodderi Chidanand #if FEATURE_DETECTION
1286a0da736SJayanth Dodderi Chidanand 	/* Detect if features enabled during compilation are supported by PE. */
1296a0da736SJayanth Dodderi Chidanand 	detect_arch_features();
1306a0da736SJayanth Dodderi Chidanand #endif /* FEATURE_DETECTION */
1316a0da736SJayanth Dodderi Chidanand 
132*ed8f06ddSthagon01-arm #if ENABLE_RUNTIME_INSTRUMENTATION
133*ed8f06ddSthagon01-arm 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL31_ENTRY, PMF_CACHE_MAINT);
134*ed8f06ddSthagon01-arm #endif
135*ed8f06ddSthagon01-arm 
1361994e562SJavier Almansa Sobrino #ifdef SUPPORT_UNKNOWN_MPID
1371994e562SJavier Almansa Sobrino 	if (unsupported_mpid_flag == 0) {
1381994e562SJavier Almansa Sobrino 		NOTICE("Unsupported MPID detected!\n");
1391994e562SJavier Almansa Sobrino 	}
1401994e562SJavier Almansa Sobrino #endif
1411994e562SJavier Almansa Sobrino 
14278e61613SSoby Mathew 	/* Perform platform setup in BL31 */
1434f6ad66aSAchin Gupta 	bl31_platform_setup();
1444f6ad66aSAchin Gupta 
1457aea9087SAchin Gupta 	/* Initialise helper libraries */
1467aea9087SAchin Gupta 	bl31_lib_init();
1474f6ad66aSAchin Gupta 
14821b818c0SJeenu Viswambharan #if EL3_EXCEPTION_HANDLING
14921b818c0SJeenu Viswambharan 	INFO("BL31: Initialising Exception Handling Framework\n");
15021b818c0SJeenu Viswambharan 	ehf_init();
15121b818c0SJeenu Viswambharan #endif
15221b818c0SJeenu Viswambharan 
15358e946aeSSoby Mathew 	/* Initialize the runtime services e.g. psci. */
154d178637dSJuan Castillo 	INFO("BL31: Initializing runtime services\n");
1557421b465SAchin Gupta 	runtime_svc_init();
1564f6ad66aSAchin Gupta 
15735ca3511SAchin Gupta 	/*
1587f366605SJeenu Viswambharan 	 * All the cold boot actions on the primary cpu are done. We now need to
1595b18de09SZelalem Aweke 	 * decide which is the next image and how to execute it.
1607f366605SJeenu Viswambharan 	 * If the SPD runtime service is present, it would want to pass control
1617f366605SJeenu Viswambharan 	 * to BL32 first in S-EL1. In that case, SPD would have registered a
1628aabea33SPaul Beesley 	 * function to initialize bl32 where it takes responsibility of entering
1635b18de09SZelalem Aweke 	 * S-EL1 and returning control back to bl31_main. Similarly, if RME is
1645b18de09SZelalem Aweke 	 * enabled and a function is registered to initialize RMM, control is
1655b18de09SZelalem Aweke 	 * transferred to RMM in R-EL2. After RMM initialization, control is
1665b18de09SZelalem Aweke 	 * returned back to bl31_main. Once this is done we can prepare entry
1675b18de09SZelalem Aweke 	 * into BL33 as normal.
16835ca3511SAchin Gupta 	 */
16935ca3511SAchin Gupta 
1707f366605SJeenu Viswambharan 	/*
1718aabea33SPaul Beesley 	 * If SPD had registered an init hook, invoke it.
1727f366605SJeenu Viswambharan 	 */
173c9512bcaSAntonio Nino Diaz 	if (bl32_init != NULL) {
174d178637dSJuan Castillo 		INFO("BL31: Initializing BL32\n");
175c9512bcaSAntonio Nino Diaz 
176889e3d1cSPrasad Kummari 		console_flush();
177c9512bcaSAntonio Nino Diaz 		int32_t rc = (*bl32_init)();
178c9512bcaSAntonio Nino Diaz 
1795b18de09SZelalem Aweke 		if (rc == 0) {
18074ad948fSAntonio Nino Diaz 			WARN("BL31: BL32 initialization failed\n");
1816ad2e461SDan Handley 		}
1825b18de09SZelalem Aweke 	}
1835b18de09SZelalem Aweke 
1845b18de09SZelalem Aweke 	/*
1855b18de09SZelalem Aweke 	 * If RME is enabled and init hook is registered, initialize RMM
1865b18de09SZelalem Aweke 	 * in R-EL2.
1875b18de09SZelalem Aweke 	 */
1885b18de09SZelalem Aweke #if ENABLE_RME
1895b18de09SZelalem Aweke 	if (rmm_init != NULL) {
1905b18de09SZelalem Aweke 		INFO("BL31: Initializing RMM\n");
1915b18de09SZelalem Aweke 
192889e3d1cSPrasad Kummari 		console_flush();
1935b18de09SZelalem Aweke 		int32_t rc = (*rmm_init)();
1945b18de09SZelalem Aweke 
1955b18de09SZelalem Aweke 		if (rc == 0) {
1965b18de09SZelalem Aweke 			WARN("BL31: RMM initialization failed\n");
1975b18de09SZelalem Aweke 		}
1985b18de09SZelalem Aweke 	}
1995b18de09SZelalem Aweke #endif
2005b18de09SZelalem Aweke 
20135ca3511SAchin Gupta 	/*
20235ca3511SAchin Gupta 	 * We are ready to enter the next EL. Prepare entry into the image
20335ca3511SAchin Gupta 	 * corresponding to the desired security state after the next ERET.
20435ca3511SAchin Gupta 	 */
20535ca3511SAchin Gupta 	bl31_prepare_next_image_entry();
20678e61613SSoby Mathew 
2070b32628eSAntonio Nino Diaz 	console_flush();
2080b32628eSAntonio Nino Diaz 
20978e61613SSoby Mathew 	/*
21078e61613SSoby Mathew 	 * Perform any platform specific runtime setup prior to cold boot exit
21178e61613SSoby Mathew 	 * from BL31
21278e61613SSoby Mathew 	 */
21378e61613SSoby Mathew 	bl31_plat_runtime_setup();
214*ed8f06ddSthagon01-arm 
215*ed8f06ddSthagon01-arm #if ENABLE_RUNTIME_INSTRUMENTATION
216*ed8f06ddSthagon01-arm 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL31_EXIT, PMF_CACHE_MAINT);
217*ed8f06ddSthagon01-arm 	console_flush();
218*ed8f06ddSthagon01-arm #endif
21935ca3511SAchin Gupta }
22035ca3511SAchin Gupta 
22135ca3511SAchin Gupta /*******************************************************************************
22235ca3511SAchin Gupta  * Accessor functions to help runtime services decide which image should be
22335ca3511SAchin Gupta  * executed after BL31. This is BL33 or the non-secure bootloader image by
22435ca3511SAchin Gupta  * default but the Secure payload dispatcher could override this by requesting
22535ca3511SAchin Gupta  * an entry into BL32 (Secure payload) first. If it does so then it should use
22635ca3511SAchin Gupta  * the same API to program an entry into BL33 once BL32 initialisation is
22735ca3511SAchin Gupta  * complete.
22835ca3511SAchin Gupta  ******************************************************************************/
22935ca3511SAchin Gupta void bl31_set_next_image_type(uint32_t security_state)
23035ca3511SAchin Gupta {
231d3280bebSJuan Castillo 	assert(sec_state_is_valid(security_state));
23235ca3511SAchin Gupta 	next_image_type = security_state;
23335ca3511SAchin Gupta }
23435ca3511SAchin Gupta 
23535ca3511SAchin Gupta uint32_t bl31_get_next_image_type(void)
23635ca3511SAchin Gupta {
23735ca3511SAchin Gupta 	return next_image_type;
23835ca3511SAchin Gupta }
23935ca3511SAchin Gupta 
24035ca3511SAchin Gupta /*******************************************************************************
24135ca3511SAchin Gupta  * This function programs EL3 registers and performs other setup to enable entry
24235ca3511SAchin Gupta  * into the next image after BL31 at the next ERET.
24335ca3511SAchin Gupta  ******************************************************************************/
24487c85134SDaniel Boulby void __init bl31_prepare_next_image_entry(void)
24535ca3511SAchin Gupta {
2464112bfa0SVikram Kanigiri 	entry_point_info_t *next_image_info;
247167a9357SAndrew Thoelke 	uint32_t image_type;
24835ca3511SAchin Gupta 
2498cd16e6bSSoby Mathew #if CTX_INCLUDE_AARCH32_REGS
2508cd16e6bSSoby Mathew 	/*
2518cd16e6bSSoby Mathew 	 * Ensure that the build flag to save AArch32 system registers in CPU
2528cd16e6bSSoby Mathew 	 * context is not set for AArch64-only platforms.
2538cd16e6bSSoby Mathew 	 */
254a0fee747SAntonio Nino Diaz 	if (el_implemented(1) == EL_IMPL_A64ONLY) {
2558cd16e6bSSoby Mathew 		ERROR("EL1 supports AArch64-only. Please set build flag "
256a0fee747SAntonio Nino Diaz 				"CTX_INCLUDE_AARCH32_REGS = 0\n");
2578cd16e6bSSoby Mathew 		panic();
2588cd16e6bSSoby Mathew 	}
2598cd16e6bSSoby Mathew #endif
2608cd16e6bSSoby Mathew 
26135ca3511SAchin Gupta 	/* Determine which image to execute next */
26235ca3511SAchin Gupta 	image_type = bl31_get_next_image_type();
26335ca3511SAchin Gupta 
264caa84939SJeenu Viswambharan 	/* Program EL3 registers to enable entry into the next EL */
2659865ac15SDan Handley 	next_image_info = bl31_plat_get_next_image_ep_info(image_type);
266c9512bcaSAntonio Nino Diaz 	assert(next_image_info != NULL);
267f05cb4a7SVikram Kanigiri 	assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr));
26835ca3511SAchin Gupta 
269d178637dSJuan Castillo 	INFO("BL31: Preparing for EL3 exit to %s world\n",
2706ad2e461SDan Handley 		(image_type == SECURE) ? "secure" : "normal");
27168a68c92SSandrine Bailleux 	print_entry_point_info(next_image_info);
27285a181ceSSoby Mathew 	cm_init_my_context(next_image_info);
2738b95e848SZelalem Aweke 
2748b95e848SZelalem Aweke 	/*
2758b95e848SZelalem Aweke 	* If we are entering the Non-secure world, use
2768b95e848SZelalem Aweke 	* 'cm_prepare_el3_exit_ns' to exit.
2778b95e848SZelalem Aweke 	*/
2788b95e848SZelalem Aweke 	if (image_type == NON_SECURE) {
2798b95e848SZelalem Aweke 		cm_prepare_el3_exit_ns();
2808b95e848SZelalem Aweke 	} else {
281167a9357SAndrew Thoelke 		cm_prepare_el3_exit(image_type);
2824f6ad66aSAchin Gupta 	}
2838b95e848SZelalem Aweke }
2847f366605SJeenu Viswambharan 
2857f366605SJeenu Viswambharan /*******************************************************************************
2867f366605SJeenu Viswambharan  * This function initializes the pointer to BL32 init function. This is expected
2877f366605SJeenu Viswambharan  * to be called by the SPD after it finishes all its initialization
2887f366605SJeenu Viswambharan  ******************************************************************************/
2896871c5d3SVikram Kanigiri void bl31_register_bl32_init(int32_t (*func)(void))
2907f366605SJeenu Viswambharan {
2917f366605SJeenu Viswambharan 	bl32_init = func;
2927f366605SJeenu Viswambharan }
2935b18de09SZelalem Aweke 
2945b18de09SZelalem Aweke #if ENABLE_RME
2955b18de09SZelalem Aweke /*******************************************************************************
2965b18de09SZelalem Aweke  * This function initializes the pointer to RMM init function. This is expected
2975b18de09SZelalem Aweke  * to be called by the RMMD after it finishes all its initialization
2985b18de09SZelalem Aweke  ******************************************************************************/
2995b18de09SZelalem Aweke void bl31_register_rmm_init(int32_t (*func)(void))
3005b18de09SZelalem Aweke {
3015b18de09SZelalem Aweke 	rmm_init = func;
3025b18de09SZelalem Aweke }
3035b18de09SZelalem Aweke #endif
304