xref: /rk3399_ARM-atf/bl31/bl31_main.c (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
14f6ad66aSAchin Gupta /*
20b32628eSAntonio Nino Diaz  * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
4*82cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta  */
64f6ad66aSAchin Gupta 
797043ac9SDan Handley #include <arch.h>
84f6ad66aSAchin Gupta #include <arch_helpers.h>
997043ac9SDan Handley #include <assert.h>
104f6ad66aSAchin Gupta #include <bl_common.h>
114f6ad66aSAchin Gupta #include <bl31.h>
120b32628eSAntonio Nino Diaz #include <console.h>
137aea9087SAchin Gupta #include <context_mgmt.h>
14b79af934SSoby Mathew #include <debug.h>
15dec5e0d1SDan Handley #include <platform.h>
16872be88aSdp-arm #include <pmf.h>
17872be88aSdp-arm #include <runtime_instr.h>
1897043ac9SDan Handley #include <runtime_svc.h>
19f05cb4a7SVikram Kanigiri #include <string.h>
207aea9087SAchin Gupta 
21872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
22872be88aSdp-arm PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
23872be88aSdp-arm 	RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
24872be88aSdp-arm #endif
25872be88aSdp-arm 
267f366605SJeenu Viswambharan /*******************************************************************************
277f366605SJeenu Viswambharan  * This function pointer is used to initialise the BL32 image. It's initialized
287f366605SJeenu Viswambharan  * by SPD calling bl31_register_bl32_init after setting up all things necessary
297f366605SJeenu Viswambharan  * for SP execution. In cases where both SPD and SP are absent, or when SPD
307f366605SJeenu Viswambharan  * finds it impossible to execute SP, this pointer is left as NULL
317f366605SJeenu Viswambharan  ******************************************************************************/
326871c5d3SVikram Kanigiri static int32_t (*bl32_init)(void);
337f366605SJeenu Viswambharan 
347aea9087SAchin Gupta /*******************************************************************************
3535ca3511SAchin Gupta  * Variable to indicate whether next image to execute after BL31 is BL33
3635ca3511SAchin Gupta  * (non-secure & default) or BL32 (secure).
3735ca3511SAchin Gupta  ******************************************************************************/
38faaa2e76SVikram Kanigiri static uint32_t next_image_type = NON_SECURE;
3935ca3511SAchin Gupta 
4058e946aeSSoby Mathew /*
4158e946aeSSoby Mathew  * Implement the ARM Standard Service function to get arguments for a
4258e946aeSSoby Mathew  * particular service.
4358e946aeSSoby Mathew  */
4458e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
4558e946aeSSoby Mathew {
4658e946aeSSoby Mathew 	/* Setup the arguments for PSCI Library */
4758e946aeSSoby Mathew 	DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, bl31_warm_entrypoint);
4858e946aeSSoby Mathew 
4958e946aeSSoby Mathew 	/* PSCI is the only ARM Standard Service implemented */
5058e946aeSSoby Mathew 	assert(svc_mask == PSCI_FID_MASK);
5158e946aeSSoby Mathew 
5258e946aeSSoby Mathew 	return (uintptr_t)&psci_args;
5358e946aeSSoby Mathew }
5458e946aeSSoby Mathew 
5535ca3511SAchin Gupta /*******************************************************************************
567aea9087SAchin Gupta  * Simple function to initialise all BL31 helper libraries.
577aea9087SAchin Gupta  ******************************************************************************/
584f2104ffSJuan Castillo void bl31_lib_init(void)
597aea9087SAchin Gupta {
607aea9087SAchin Gupta 	cm_init();
617aea9087SAchin Gupta }
624f6ad66aSAchin Gupta 
634f6ad66aSAchin Gupta /*******************************************************************************
644f6ad66aSAchin Gupta  * BL31 is responsible for setting up the runtime services for the primary cpu
6535ca3511SAchin Gupta  * before passing control to the bootloader or an Operating System. This
6635ca3511SAchin Gupta  * function calls runtime_svc_init() which initializes all registered runtime
6735ca3511SAchin Gupta  * services. The run time services would setup enough context for the core to
6835ca3511SAchin Gupta  * swtich to the next exception level. When this function returns, the core will
6935ca3511SAchin Gupta  * switch to the programmed exception level via. an ERET.
704f6ad66aSAchin Gupta  ******************************************************************************/
714f6ad66aSAchin Gupta void bl31_main(void)
724f6ad66aSAchin Gupta {
73d178637dSJuan Castillo 	NOTICE("BL31: %s\n", version_string);
74d178637dSJuan Castillo 	NOTICE("BL31: %s\n", build_message);
756ad2e461SDan Handley 
7678e61613SSoby Mathew 	/* Perform platform setup in BL31 */
774f6ad66aSAchin Gupta 	bl31_platform_setup();
784f6ad66aSAchin Gupta 
797aea9087SAchin Gupta 	/* Initialise helper libraries */
807aea9087SAchin Gupta 	bl31_lib_init();
814f6ad66aSAchin Gupta 
8258e946aeSSoby Mathew 	/* Initialize the runtime services e.g. psci. */
83d178637dSJuan Castillo 	INFO("BL31: Initializing runtime services\n");
847421b465SAchin Gupta 	runtime_svc_init();
854f6ad66aSAchin Gupta 
8635ca3511SAchin Gupta 	/*
877f366605SJeenu Viswambharan 	 * All the cold boot actions on the primary cpu are done. We now need to
887f366605SJeenu Viswambharan 	 * decide which is the next image (BL32 or BL33) and how to execute it.
897f366605SJeenu Viswambharan 	 * If the SPD runtime service is present, it would want to pass control
907f366605SJeenu Viswambharan 	 * to BL32 first in S-EL1. In that case, SPD would have registered a
917f366605SJeenu Viswambharan 	 * function to intialize bl32 where it takes responsibility of entering
927f366605SJeenu Viswambharan 	 * S-EL1 and returning control back to bl31_main. Once this is done we
937f366605SJeenu Viswambharan 	 * can prepare entry into BL33 as normal.
9435ca3511SAchin Gupta 	 */
9535ca3511SAchin Gupta 
967f366605SJeenu Viswambharan 	/*
976871c5d3SVikram Kanigiri 	 * If SPD had registerd an init hook, invoke it.
987f366605SJeenu Viswambharan 	 */
996ad2e461SDan Handley 	if (bl32_init) {
100d178637dSJuan Castillo 		INFO("BL31: Initializing BL32\n");
1016871c5d3SVikram Kanigiri 		(*bl32_init)();
1026ad2e461SDan Handley 	}
10335ca3511SAchin Gupta 	/*
10435ca3511SAchin Gupta 	 * We are ready to enter the next EL. Prepare entry into the image
10535ca3511SAchin Gupta 	 * corresponding to the desired security state after the next ERET.
10635ca3511SAchin Gupta 	 */
10735ca3511SAchin Gupta 	bl31_prepare_next_image_entry();
10878e61613SSoby Mathew 
1090b32628eSAntonio Nino Diaz 	console_flush();
1100b32628eSAntonio Nino Diaz 
11178e61613SSoby Mathew 	/*
11278e61613SSoby Mathew 	 * Perform any platform specific runtime setup prior to cold boot exit
11378e61613SSoby Mathew 	 * from BL31
11478e61613SSoby Mathew 	 */
11578e61613SSoby Mathew 	bl31_plat_runtime_setup();
11635ca3511SAchin Gupta }
11735ca3511SAchin Gupta 
11835ca3511SAchin Gupta /*******************************************************************************
11935ca3511SAchin Gupta  * Accessor functions to help runtime services decide which image should be
12035ca3511SAchin Gupta  * executed after BL31. This is BL33 or the non-secure bootloader image by
12135ca3511SAchin Gupta  * default but the Secure payload dispatcher could override this by requesting
12235ca3511SAchin Gupta  * an entry into BL32 (Secure payload) first. If it does so then it should use
12335ca3511SAchin Gupta  * the same API to program an entry into BL33 once BL32 initialisation is
12435ca3511SAchin Gupta  * complete.
12535ca3511SAchin Gupta  ******************************************************************************/
12635ca3511SAchin Gupta void bl31_set_next_image_type(uint32_t security_state)
12735ca3511SAchin Gupta {
128d3280bebSJuan Castillo 	assert(sec_state_is_valid(security_state));
12935ca3511SAchin Gupta 	next_image_type = security_state;
13035ca3511SAchin Gupta }
13135ca3511SAchin Gupta 
13235ca3511SAchin Gupta uint32_t bl31_get_next_image_type(void)
13335ca3511SAchin Gupta {
13435ca3511SAchin Gupta 	return next_image_type;
13535ca3511SAchin Gupta }
13635ca3511SAchin Gupta 
13735ca3511SAchin Gupta /*******************************************************************************
13835ca3511SAchin Gupta  * This function programs EL3 registers and performs other setup to enable entry
13935ca3511SAchin Gupta  * into the next image after BL31 at the next ERET.
14035ca3511SAchin Gupta  ******************************************************************************/
1414f2104ffSJuan Castillo void bl31_prepare_next_image_entry(void)
14235ca3511SAchin Gupta {
1434112bfa0SVikram Kanigiri 	entry_point_info_t *next_image_info;
144167a9357SAndrew Thoelke 	uint32_t image_type;
14535ca3511SAchin Gupta 
1468cd16e6bSSoby Mathew #if CTX_INCLUDE_AARCH32_REGS
1478cd16e6bSSoby Mathew 	/*
1488cd16e6bSSoby Mathew 	 * Ensure that the build flag to save AArch32 system registers in CPU
1498cd16e6bSSoby Mathew 	 * context is not set for AArch64-only platforms.
1508cd16e6bSSoby Mathew 	 */
1518cd16e6bSSoby Mathew 	if (((read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL1_SHIFT)
1528cd16e6bSSoby Mathew 			& ID_AA64PFR0_ELX_MASK) == 0x1) {
1538cd16e6bSSoby Mathew 		ERROR("EL1 supports AArch64-only. Please set build flag "
1548cd16e6bSSoby Mathew 				"CTX_INCLUDE_AARCH32_REGS = 0");
1558cd16e6bSSoby Mathew 		panic();
1568cd16e6bSSoby Mathew 	}
1578cd16e6bSSoby Mathew #endif
1588cd16e6bSSoby Mathew 
15935ca3511SAchin Gupta 	/* Determine which image to execute next */
16035ca3511SAchin Gupta 	image_type = bl31_get_next_image_type();
16135ca3511SAchin Gupta 
162caa84939SJeenu Viswambharan 	/* Program EL3 registers to enable entry into the next EL */
1639865ac15SDan Handley 	next_image_info = bl31_plat_get_next_image_ep_info(image_type);
16435ca3511SAchin Gupta 	assert(next_image_info);
165f05cb4a7SVikram Kanigiri 	assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr));
16635ca3511SAchin Gupta 
167d178637dSJuan Castillo 	INFO("BL31: Preparing for EL3 exit to %s world\n",
1686ad2e461SDan Handley 		(image_type == SECURE) ? "secure" : "normal");
16968a68c92SSandrine Bailleux 	print_entry_point_info(next_image_info);
17085a181ceSSoby Mathew 	cm_init_my_context(next_image_info);
171167a9357SAndrew Thoelke 	cm_prepare_el3_exit(image_type);
1724f6ad66aSAchin Gupta }
1737f366605SJeenu Viswambharan 
1747f366605SJeenu Viswambharan /*******************************************************************************
1757f366605SJeenu Viswambharan  * This function initializes the pointer to BL32 init function. This is expected
1767f366605SJeenu Viswambharan  * to be called by the SPD after it finishes all its initialization
1777f366605SJeenu Viswambharan  ******************************************************************************/
1786871c5d3SVikram Kanigiri void bl31_register_bl32_init(int32_t (*func)(void))
1797f366605SJeenu Viswambharan {
1807f366605SJeenu Viswambharan 	bl32_init = func;
1817f366605SJeenu Viswambharan }
182