xref: /rk3399_ARM-atf/bl31/bl31_main.c (revision caa84939a4d8b1189dea8619ccc57bdb3026b125)
1 /*
2  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <arch_helpers.h>
35 #include <console.h>
36 #include <platform.h>
37 #include <semihosting.h>
38 #include <bl_common.h>
39 #include <bl31.h>
40 #include <runtime_svc.h>
41 #include <context_mgmt.h>
42 
43 /*******************************************************************************
44  * Simple function to initialise all BL31 helper libraries.
45  ******************************************************************************/
46 void bl31_lib_init()
47 {
48 	cm_init();
49 }
50 
51 /*******************************************************************************
52  * BL31 is responsible for setting up the runtime services for the primary cpu
53  * before passing control to the bootloader (UEFI) or Linux. This function calls
54  * runtime_svc_init() which initializes all registered runtime services. The run
55  * time services would setup enough context for the core to swtich to the next
56  * exception level. When this function returns, the core will switch to the
57  * programmed exception level via. an ERET.
58  ******************************************************************************/
59 void bl31_main(void)
60 {
61 	el_change_info *next_image_info;
62 	uint32_t scr;
63 
64 	/* Perform remaining generic architectural setup from EL3 */
65 	bl31_arch_setup();
66 
67 	/* Perform platform setup in BL1 */
68 	bl31_platform_setup();
69 
70 #if defined (__GNUC__)
71 	printf("BL31 Built : %s, %s\n\r", __TIME__, __DATE__);
72 #endif
73 	/* Initialise helper libraries */
74 	bl31_lib_init();
75 
76 	/* Initialize the runtime services e.g. psci */
77 	runtime_svc_init();
78 
79 	/* Clean caches before re-entering normal world */
80 	dcsw_op_all(DCCSW);
81 
82 	/*
83 	 * Setup minimal architectural state of the next highest EL to
84 	 * allow execution in it immediately upon entering it.
85 	 */
86 	bl31_arch_next_el_setup();
87 
88 	/* Program EL3 registers to enable entry into the next EL */
89 	next_image_info = bl31_get_next_image_info();
90 	scr = read_scr();
91 	if (next_image_info->security_state == NON_SECURE)
92 		scr |= SCR_NS_BIT;
93 
94 	/*
95 	 * Tell the context mgmt. library to ensure that SP_EL3 points to
96 	 * the right context to exit from EL3 correctly.
97 	 */
98 	cm_set_el3_eret_context(next_image_info->security_state,
99 			next_image_info->entrypoint,
100 			next_image_info->spsr,
101 			scr);
102 
103 	/* Finally set the next context */
104 	cm_set_next_eret_context(next_image_info->security_state);
105 }
106