xref: /rk3399_ARM-atf/bl31/bl31_main.c (revision 35ca35119d9dc51f1665184ab6db5e2861c213b4)
14f6ad66aSAchin Gupta /*
2e83b0cadSDan Handley  * Copyright (c) 2013-2014, 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 
314f6ad66aSAchin Gupta #include <stdio.h>
324f6ad66aSAchin Gupta #include <string.h>
334f6ad66aSAchin Gupta #include <assert.h>
344f6ad66aSAchin Gupta #include <arch_helpers.h>
354f6ad66aSAchin Gupta #include <console.h>
364f6ad66aSAchin Gupta #include <platform.h>
374f6ad66aSAchin Gupta #include <semihosting.h>
384f6ad66aSAchin Gupta #include <bl_common.h>
394f6ad66aSAchin Gupta #include <bl31.h>
404f6ad66aSAchin Gupta #include <runtime_svc.h>
417aea9087SAchin Gupta #include <context_mgmt.h>
427aea9087SAchin Gupta 
437aea9087SAchin Gupta /*******************************************************************************
44*35ca3511SAchin Gupta  * Variable to indicate whether next image to execute after BL31 is BL33
45*35ca3511SAchin Gupta  * (non-secure & default) or BL32 (secure).
46*35ca3511SAchin Gupta  ******************************************************************************/
47*35ca3511SAchin Gupta static uint32_t next_image_type = NON_SECURE;
48*35ca3511SAchin Gupta 
49*35ca3511SAchin Gupta /*******************************************************************************
507aea9087SAchin Gupta  * Simple function to initialise all BL31 helper libraries.
517aea9087SAchin Gupta  ******************************************************************************/
527aea9087SAchin Gupta void bl31_lib_init()
537aea9087SAchin Gupta {
547aea9087SAchin Gupta 	cm_init();
557aea9087SAchin Gupta }
564f6ad66aSAchin Gupta 
574f6ad66aSAchin Gupta /*******************************************************************************
584f6ad66aSAchin Gupta  * BL31 is responsible for setting up the runtime services for the primary cpu
59*35ca3511SAchin Gupta  * before passing control to the bootloader or an Operating System. This
60*35ca3511SAchin Gupta  * function calls runtime_svc_init() which initializes all registered runtime
61*35ca3511SAchin Gupta  * services. The run time services would setup enough context for the core to
62*35ca3511SAchin Gupta  * swtich to the next exception level. When this function returns, the core will
63*35ca3511SAchin Gupta  * switch to the programmed exception level via. an ERET.
644f6ad66aSAchin Gupta  ******************************************************************************/
654f6ad66aSAchin Gupta void bl31_main(void)
664f6ad66aSAchin Gupta {
67*35ca3511SAchin Gupta #if DEBUG
68*35ca3511SAchin Gupta 	unsigned long mpidr = read_mpidr();
69*35ca3511SAchin Gupta #endif
704f6ad66aSAchin Gupta 
714f6ad66aSAchin Gupta 	/* Perform remaining generic architectural setup from EL3 */
724f6ad66aSAchin Gupta 	bl31_arch_setup();
734f6ad66aSAchin Gupta 
744f6ad66aSAchin Gupta 	/* Perform platform setup in BL1 */
754f6ad66aSAchin Gupta 	bl31_platform_setup();
764f6ad66aSAchin Gupta 
774f6ad66aSAchin Gupta #if defined (__GNUC__)
784f6ad66aSAchin Gupta 	printf("BL31 Built : %s, %s\n\r", __TIME__, __DATE__);
794f6ad66aSAchin Gupta #endif
807aea9087SAchin Gupta 	/* Initialise helper libraries */
817aea9087SAchin Gupta 	bl31_lib_init();
824f6ad66aSAchin Gupta 
834f6ad66aSAchin Gupta 	/* Initialize the runtime services e.g. psci */
847421b465SAchin Gupta 	runtime_svc_init();
854f6ad66aSAchin Gupta 
864f6ad66aSAchin Gupta 	/* Clean caches before re-entering normal world */
874f6ad66aSAchin Gupta 	dcsw_op_all(DCCSW);
884f6ad66aSAchin Gupta 
89caa84939SJeenu Viswambharan 	/*
90*35ca3511SAchin Gupta 	 * Use the more complex exception vectors now that context
91*35ca3511SAchin Gupta 	 * management is setup. SP_EL3 should point to a 'cpu_context'
92*35ca3511SAchin Gupta 	 * structure which has an exception stack allocated.  The PSCI
93*35ca3511SAchin Gupta 	 * service should have set the context.
94*35ca3511SAchin Gupta 	 */
95*35ca3511SAchin Gupta 	assert(cm_get_context(mpidr, NON_SECURE));
96*35ca3511SAchin Gupta 	cm_set_next_eret_context(NON_SECURE);
97*35ca3511SAchin Gupta 	write_vbar_el3((uint64_t) runtime_exceptions);
98*35ca3511SAchin Gupta 
99*35ca3511SAchin Gupta 	/*
100*35ca3511SAchin Gupta 	 * All the cold boot actions on the primary cpu are done. We
101*35ca3511SAchin Gupta 	 * now need to decide which is the next image (BL32 or BL33)
102*35ca3511SAchin Gupta 	 * and how to execute it. If the SPD runtime service is
103*35ca3511SAchin Gupta 	 * present, it would want to pass control to BL32 first in
104*35ca3511SAchin Gupta 	 * S-EL1. It will export the bl32_init() routine where it takes
105*35ca3511SAchin Gupta 	 * responsibility of entering S-EL1 and returning control back
106*35ca3511SAchin Gupta 	 * to bl31_main. Once this is done we can prepare entry into
107*35ca3511SAchin Gupta 	 * BL33 as normal.
108*35ca3511SAchin Gupta 	 */
109*35ca3511SAchin Gupta 
110*35ca3511SAchin Gupta 	/* Tell BL32 about it memory extents as well */
111*35ca3511SAchin Gupta 	if (bl32_init)
112*35ca3511SAchin Gupta 		bl32_init(bl31_plat_get_bl32_mem_layout());
113*35ca3511SAchin Gupta 
114*35ca3511SAchin Gupta 	/*
115*35ca3511SAchin Gupta 	 * We are ready to enter the next EL. Prepare entry into the image
116*35ca3511SAchin Gupta 	 * corresponding to the desired security state after the next ERET.
117*35ca3511SAchin Gupta 	 */
118*35ca3511SAchin Gupta 	bl31_prepare_next_image_entry();
119*35ca3511SAchin Gupta }
120*35ca3511SAchin Gupta 
121*35ca3511SAchin Gupta /*******************************************************************************
122*35ca3511SAchin Gupta  * Accessor functions to help runtime services decide which image should be
123*35ca3511SAchin Gupta  * executed after BL31. This is BL33 or the non-secure bootloader image by
124*35ca3511SAchin Gupta  * default but the Secure payload dispatcher could override this by requesting
125*35ca3511SAchin Gupta  * an entry into BL32 (Secure payload) first. If it does so then it should use
126*35ca3511SAchin Gupta  * the same API to program an entry into BL33 once BL32 initialisation is
127*35ca3511SAchin Gupta  * complete.
128*35ca3511SAchin Gupta  ******************************************************************************/
129*35ca3511SAchin Gupta void bl31_set_next_image_type(uint32_t security_state)
130*35ca3511SAchin Gupta {
131*35ca3511SAchin Gupta 	assert(security_state == NON_SECURE || security_state == SECURE);
132*35ca3511SAchin Gupta 	next_image_type = security_state;
133*35ca3511SAchin Gupta }
134*35ca3511SAchin Gupta 
135*35ca3511SAchin Gupta uint32_t bl31_get_next_image_type(void)
136*35ca3511SAchin Gupta {
137*35ca3511SAchin Gupta 	return next_image_type;
138*35ca3511SAchin Gupta }
139*35ca3511SAchin Gupta 
140*35ca3511SAchin Gupta /*******************************************************************************
141*35ca3511SAchin Gupta  * This function programs EL3 registers and performs other setup to enable entry
142*35ca3511SAchin Gupta  * into the next image after BL31 at the next ERET.
143*35ca3511SAchin Gupta  ******************************************************************************/
144*35ca3511SAchin Gupta void bl31_prepare_next_image_entry()
145*35ca3511SAchin Gupta {
146*35ca3511SAchin Gupta 	el_change_info *next_image_info;
147*35ca3511SAchin Gupta 	uint32_t scr, image_type;
148*35ca3511SAchin Gupta 
149*35ca3511SAchin Gupta 	/* Determine which image to execute next */
150*35ca3511SAchin Gupta 	image_type = bl31_get_next_image_type();
151*35ca3511SAchin Gupta 
152*35ca3511SAchin Gupta 	/*
153caa84939SJeenu Viswambharan 	 * Setup minimal architectural state of the next highest EL to
154caa84939SJeenu Viswambharan 	 * allow execution in it immediately upon entering it.
155caa84939SJeenu Viswambharan 	 */
156*35ca3511SAchin Gupta 	bl31_next_el_arch_setup(image_type);
1574f6ad66aSAchin Gupta 
158caa84939SJeenu Viswambharan 	/* Program EL3 registers to enable entry into the next EL */
159*35ca3511SAchin Gupta 	next_image_info = bl31_get_next_image_info(image_type);
160*35ca3511SAchin Gupta 	assert(next_image_info);
161*35ca3511SAchin Gupta 
162caa84939SJeenu Viswambharan 	scr = read_scr();
163*35ca3511SAchin Gupta 	if (image_type == NON_SECURE)
164caa84939SJeenu Viswambharan 		scr |= SCR_NS_BIT;
165caa84939SJeenu Viswambharan 
166caa84939SJeenu Viswambharan 	/*
167caa84939SJeenu Viswambharan 	 * Tell the context mgmt. library to ensure that SP_EL3 points to
168caa84939SJeenu Viswambharan 	 * the right context to exit from EL3 correctly.
169caa84939SJeenu Viswambharan 	 */
170caa84939SJeenu Viswambharan 	cm_set_el3_eret_context(next_image_info->security_state,
171caa84939SJeenu Viswambharan 			next_image_info->entrypoint,
172caa84939SJeenu Viswambharan 			next_image_info->spsr,
173caa84939SJeenu Viswambharan 			scr);
174caa84939SJeenu Viswambharan 
175caa84939SJeenu Viswambharan 	/* Finally set the next context */
176caa84939SJeenu Viswambharan 	cm_set_next_eret_context(next_image_info->security_state);
1774f6ad66aSAchin Gupta }
178