xref: /rk3399_ARM-atf/bl31/bl31_main.c (revision 0b32628eddfef95b51909816f625e6f37cd21cb8)
14f6ad66aSAchin Gupta /*
2*0b32628eSAntonio Nino Diaz  * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
44f6ad66aSAchin Gupta  * Redistribution and use in source and binary forms, with or without
54f6ad66aSAchin Gupta  * modification, are permitted provided that the following conditions are met:
64f6ad66aSAchin Gupta  *
74f6ad66aSAchin Gupta  * Redistributions of source code must retain the above copyright notice, this
84f6ad66aSAchin Gupta  * list of conditions and the following disclaimer.
94f6ad66aSAchin Gupta  *
104f6ad66aSAchin Gupta  * Redistributions in binary form must reproduce the above copyright notice,
114f6ad66aSAchin Gupta  * this list of conditions and the following disclaimer in the documentation
124f6ad66aSAchin Gupta  * and/or other materials provided with the distribution.
134f6ad66aSAchin Gupta  *
144f6ad66aSAchin Gupta  * Neither the name of ARM nor the names of its contributors may be used
154f6ad66aSAchin Gupta  * to endorse or promote products derived from this software without specific
164f6ad66aSAchin Gupta  * prior written permission.
174f6ad66aSAchin Gupta  *
184f6ad66aSAchin Gupta  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194f6ad66aSAchin Gupta  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204f6ad66aSAchin Gupta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214f6ad66aSAchin Gupta  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224f6ad66aSAchin Gupta  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234f6ad66aSAchin Gupta  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244f6ad66aSAchin Gupta  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254f6ad66aSAchin Gupta  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264f6ad66aSAchin Gupta  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274f6ad66aSAchin Gupta  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284f6ad66aSAchin Gupta  * POSSIBILITY OF SUCH DAMAGE.
294f6ad66aSAchin Gupta  */
304f6ad66aSAchin Gupta 
3197043ac9SDan Handley #include <arch.h>
324f6ad66aSAchin Gupta #include <arch_helpers.h>
3397043ac9SDan Handley #include <assert.h>
344f6ad66aSAchin Gupta #include <bl_common.h>
354f6ad66aSAchin Gupta #include <bl31.h>
36*0b32628eSAntonio Nino Diaz #include <console.h>
377aea9087SAchin Gupta #include <context_mgmt.h>
38b79af934SSoby Mathew #include <debug.h>
39dec5e0d1SDan Handley #include <platform.h>
40872be88aSdp-arm #include <pmf.h>
41872be88aSdp-arm #include <runtime_instr.h>
4297043ac9SDan Handley #include <runtime_svc.h>
43f05cb4a7SVikram Kanigiri #include <string.h>
447aea9087SAchin Gupta 
45872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
46872be88aSdp-arm PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
47872be88aSdp-arm 	RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
48872be88aSdp-arm #endif
49872be88aSdp-arm 
507f366605SJeenu Viswambharan /*******************************************************************************
517f366605SJeenu Viswambharan  * This function pointer is used to initialise the BL32 image. It's initialized
527f366605SJeenu Viswambharan  * by SPD calling bl31_register_bl32_init after setting up all things necessary
537f366605SJeenu Viswambharan  * for SP execution. In cases where both SPD and SP are absent, or when SPD
547f366605SJeenu Viswambharan  * finds it impossible to execute SP, this pointer is left as NULL
557f366605SJeenu Viswambharan  ******************************************************************************/
566871c5d3SVikram Kanigiri static int32_t (*bl32_init)(void);
577f366605SJeenu Viswambharan 
587aea9087SAchin Gupta /*******************************************************************************
5935ca3511SAchin Gupta  * Variable to indicate whether next image to execute after BL31 is BL33
6035ca3511SAchin Gupta  * (non-secure & default) or BL32 (secure).
6135ca3511SAchin Gupta  ******************************************************************************/
62faaa2e76SVikram Kanigiri static uint32_t next_image_type = NON_SECURE;
6335ca3511SAchin Gupta 
6458e946aeSSoby Mathew /*
6558e946aeSSoby Mathew  * Implement the ARM Standard Service function to get arguments for a
6658e946aeSSoby Mathew  * particular service.
6758e946aeSSoby Mathew  */
6858e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
6958e946aeSSoby Mathew {
7058e946aeSSoby Mathew 	/* Setup the arguments for PSCI Library */
7158e946aeSSoby Mathew 	DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, bl31_warm_entrypoint);
7258e946aeSSoby Mathew 
7358e946aeSSoby Mathew 	/* PSCI is the only ARM Standard Service implemented */
7458e946aeSSoby Mathew 	assert(svc_mask == PSCI_FID_MASK);
7558e946aeSSoby Mathew 
7658e946aeSSoby Mathew 	return (uintptr_t)&psci_args;
7758e946aeSSoby Mathew }
7858e946aeSSoby Mathew 
7935ca3511SAchin Gupta /*******************************************************************************
807aea9087SAchin Gupta  * Simple function to initialise all BL31 helper libraries.
817aea9087SAchin Gupta  ******************************************************************************/
824f2104ffSJuan Castillo void bl31_lib_init(void)
837aea9087SAchin Gupta {
847aea9087SAchin Gupta 	cm_init();
857aea9087SAchin Gupta }
864f6ad66aSAchin Gupta 
874f6ad66aSAchin Gupta /*******************************************************************************
884f6ad66aSAchin Gupta  * BL31 is responsible for setting up the runtime services for the primary cpu
8935ca3511SAchin Gupta  * before passing control to the bootloader or an Operating System. This
9035ca3511SAchin Gupta  * function calls runtime_svc_init() which initializes all registered runtime
9135ca3511SAchin Gupta  * services. The run time services would setup enough context for the core to
9235ca3511SAchin Gupta  * swtich to the next exception level. When this function returns, the core will
9335ca3511SAchin Gupta  * switch to the programmed exception level via. an ERET.
944f6ad66aSAchin Gupta  ******************************************************************************/
954f6ad66aSAchin Gupta void bl31_main(void)
964f6ad66aSAchin Gupta {
97d178637dSJuan Castillo 	NOTICE("BL31: %s\n", version_string);
98d178637dSJuan Castillo 	NOTICE("BL31: %s\n", build_message);
996ad2e461SDan Handley 
10078e61613SSoby Mathew 	/* Perform platform setup in BL31 */
1014f6ad66aSAchin Gupta 	bl31_platform_setup();
1024f6ad66aSAchin Gupta 
1037aea9087SAchin Gupta 	/* Initialise helper libraries */
1047aea9087SAchin Gupta 	bl31_lib_init();
1054f6ad66aSAchin Gupta 
10658e946aeSSoby Mathew 	/* Initialize the runtime services e.g. psci. */
107d178637dSJuan Castillo 	INFO("BL31: Initializing runtime services\n");
1087421b465SAchin Gupta 	runtime_svc_init();
1094f6ad66aSAchin Gupta 
11035ca3511SAchin Gupta 	/*
1117f366605SJeenu Viswambharan 	 * All the cold boot actions on the primary cpu are done. We now need to
1127f366605SJeenu Viswambharan 	 * decide which is the next image (BL32 or BL33) and how to execute it.
1137f366605SJeenu Viswambharan 	 * If the SPD runtime service is present, it would want to pass control
1147f366605SJeenu Viswambharan 	 * to BL32 first in S-EL1. In that case, SPD would have registered a
1157f366605SJeenu Viswambharan 	 * function to intialize bl32 where it takes responsibility of entering
1167f366605SJeenu Viswambharan 	 * S-EL1 and returning control back to bl31_main. Once this is done we
1177f366605SJeenu Viswambharan 	 * can prepare entry into BL33 as normal.
11835ca3511SAchin Gupta 	 */
11935ca3511SAchin Gupta 
1207f366605SJeenu Viswambharan 	/*
1216871c5d3SVikram Kanigiri 	 * If SPD had registerd an init hook, invoke it.
1227f366605SJeenu Viswambharan 	 */
1236ad2e461SDan Handley 	if (bl32_init) {
124d178637dSJuan Castillo 		INFO("BL31: Initializing BL32\n");
1256871c5d3SVikram Kanigiri 		(*bl32_init)();
1266ad2e461SDan Handley 	}
12735ca3511SAchin Gupta 	/*
12835ca3511SAchin Gupta 	 * We are ready to enter the next EL. Prepare entry into the image
12935ca3511SAchin Gupta 	 * corresponding to the desired security state after the next ERET.
13035ca3511SAchin Gupta 	 */
13135ca3511SAchin Gupta 	bl31_prepare_next_image_entry();
13278e61613SSoby Mathew 
133*0b32628eSAntonio Nino Diaz 	console_flush();
134*0b32628eSAntonio Nino Diaz 
13578e61613SSoby Mathew 	/*
13678e61613SSoby Mathew 	 * Perform any platform specific runtime setup prior to cold boot exit
13778e61613SSoby Mathew 	 * from BL31
13878e61613SSoby Mathew 	 */
13978e61613SSoby Mathew 	bl31_plat_runtime_setup();
14035ca3511SAchin Gupta }
14135ca3511SAchin Gupta 
14235ca3511SAchin Gupta /*******************************************************************************
14335ca3511SAchin Gupta  * Accessor functions to help runtime services decide which image should be
14435ca3511SAchin Gupta  * executed after BL31. This is BL33 or the non-secure bootloader image by
14535ca3511SAchin Gupta  * default but the Secure payload dispatcher could override this by requesting
14635ca3511SAchin Gupta  * an entry into BL32 (Secure payload) first. If it does so then it should use
14735ca3511SAchin Gupta  * the same API to program an entry into BL33 once BL32 initialisation is
14835ca3511SAchin Gupta  * complete.
14935ca3511SAchin Gupta  ******************************************************************************/
15035ca3511SAchin Gupta void bl31_set_next_image_type(uint32_t security_state)
15135ca3511SAchin Gupta {
152d3280bebSJuan Castillo 	assert(sec_state_is_valid(security_state));
15335ca3511SAchin Gupta 	next_image_type = security_state;
15435ca3511SAchin Gupta }
15535ca3511SAchin Gupta 
15635ca3511SAchin Gupta uint32_t bl31_get_next_image_type(void)
15735ca3511SAchin Gupta {
15835ca3511SAchin Gupta 	return next_image_type;
15935ca3511SAchin Gupta }
16035ca3511SAchin Gupta 
16135ca3511SAchin Gupta /*******************************************************************************
16235ca3511SAchin Gupta  * This function programs EL3 registers and performs other setup to enable entry
16335ca3511SAchin Gupta  * into the next image after BL31 at the next ERET.
16435ca3511SAchin Gupta  ******************************************************************************/
1654f2104ffSJuan Castillo void bl31_prepare_next_image_entry(void)
16635ca3511SAchin Gupta {
1674112bfa0SVikram Kanigiri 	entry_point_info_t *next_image_info;
168167a9357SAndrew Thoelke 	uint32_t image_type;
16935ca3511SAchin Gupta 
1708cd16e6bSSoby Mathew #if CTX_INCLUDE_AARCH32_REGS
1718cd16e6bSSoby Mathew 	/*
1728cd16e6bSSoby Mathew 	 * Ensure that the build flag to save AArch32 system registers in CPU
1738cd16e6bSSoby Mathew 	 * context is not set for AArch64-only platforms.
1748cd16e6bSSoby Mathew 	 */
1758cd16e6bSSoby Mathew 	if (((read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL1_SHIFT)
1768cd16e6bSSoby Mathew 			& ID_AA64PFR0_ELX_MASK) == 0x1) {
1778cd16e6bSSoby Mathew 		ERROR("EL1 supports AArch64-only. Please set build flag "
1788cd16e6bSSoby Mathew 				"CTX_INCLUDE_AARCH32_REGS = 0");
1798cd16e6bSSoby Mathew 		panic();
1808cd16e6bSSoby Mathew 	}
1818cd16e6bSSoby Mathew #endif
1828cd16e6bSSoby Mathew 
18335ca3511SAchin Gupta 	/* Determine which image to execute next */
18435ca3511SAchin Gupta 	image_type = bl31_get_next_image_type();
18535ca3511SAchin Gupta 
186caa84939SJeenu Viswambharan 	/* Program EL3 registers to enable entry into the next EL */
1879865ac15SDan Handley 	next_image_info = bl31_plat_get_next_image_ep_info(image_type);
18835ca3511SAchin Gupta 	assert(next_image_info);
189f05cb4a7SVikram Kanigiri 	assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr));
19035ca3511SAchin Gupta 
191d178637dSJuan Castillo 	INFO("BL31: Preparing for EL3 exit to %s world\n",
1926ad2e461SDan Handley 		(image_type == SECURE) ? "secure" : "normal");
19368a68c92SSandrine Bailleux 	print_entry_point_info(next_image_info);
19485a181ceSSoby Mathew 	cm_init_my_context(next_image_info);
195167a9357SAndrew Thoelke 	cm_prepare_el3_exit(image_type);
1964f6ad66aSAchin Gupta }
1977f366605SJeenu Viswambharan 
1987f366605SJeenu Viswambharan /*******************************************************************************
1997f366605SJeenu Viswambharan  * This function initializes the pointer to BL32 init function. This is expected
2007f366605SJeenu Viswambharan  * to be called by the SPD after it finishes all its initialization
2017f366605SJeenu Viswambharan  ******************************************************************************/
2026871c5d3SVikram Kanigiri void bl31_register_bl32_init(int32_t (*func)(void))
2037f366605SJeenu Viswambharan {
2047f366605SJeenu Viswambharan 	bl32_init = func;
2057f366605SJeenu Viswambharan }
206