xref: /rk3399_ARM-atf/bl2/bl2_main.c (revision 6ad2e461f0cd6de5aefd89fa0ba7acf2c293b8c2)
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 
3197043ac9SDan Handley #include <arch.h>
324f6ad66aSAchin Gupta #include <arch_helpers.h>
3397043ac9SDan Handley #include <assert.h>
344f6ad66aSAchin Gupta #include <bl_common.h>
3535e98e55SDan Handley #include <debug.h>
3697043ac9SDan Handley #include <platform.h>
375f0cdb05SDan Handley #include <platform_def.h>
385b827a8fSDan Handley #include "bl2_private.h"
394f6ad66aSAchin Gupta 
4093d81d64SSandrine Bailleux /*******************************************************************************
4193d81d64SSandrine Bailleux  * Load the BL3-0 image if there's one.
4293d81d64SSandrine Bailleux  * If a platform does not want to attempt to load BL3-0 image it must leave
4393d81d64SSandrine Bailleux  * BL30_BASE undefined.
4493d81d64SSandrine Bailleux  * Return 0 on success or if there's no BL3-0 image to load, a negative error
4593d81d64SSandrine Bailleux  * code otherwise.
4693d81d64SSandrine Bailleux  ******************************************************************************/
4793d81d64SSandrine Bailleux static int load_bl30(void)
4893d81d64SSandrine Bailleux {
4993d81d64SSandrine Bailleux 	int e = 0;
5093d81d64SSandrine Bailleux #ifdef BL30_BASE
5193d81d64SSandrine Bailleux 	meminfo_t bl30_mem_info;
5293d81d64SSandrine Bailleux 	image_info_t bl30_image_info;
5393d81d64SSandrine Bailleux 
5493d81d64SSandrine Bailleux 	/*
5593d81d64SSandrine Bailleux 	 * It is up to the platform to specify where BL3-0 should be loaded if
5693d81d64SSandrine Bailleux 	 * it exists. It could create space in the secure sram or point to a
5793d81d64SSandrine Bailleux 	 * completely different memory.
5893d81d64SSandrine Bailleux 	 *
5993d81d64SSandrine Bailleux 	 * The entry point information is not relevant in this case as the AP
6093d81d64SSandrine Bailleux 	 * won't execute the BL3-0 image.
6193d81d64SSandrine Bailleux 	 */
62*6ad2e461SDan Handley 	INFO("BL2: Loading BL3-0\n");
6393d81d64SSandrine Bailleux 	bl2_plat_get_bl30_meminfo(&bl30_mem_info);
6493d81d64SSandrine Bailleux 	e = load_image(&bl30_mem_info,
6593d81d64SSandrine Bailleux 		       BL30_IMAGE_NAME,
6693d81d64SSandrine Bailleux 		       BL30_BASE,
6793d81d64SSandrine Bailleux 		       &bl30_image_info,
6893d81d64SSandrine Bailleux 		       NULL);
6993d81d64SSandrine Bailleux 
7093d81d64SSandrine Bailleux 	if (e == 0) {
7193d81d64SSandrine Bailleux 		/* The subsequent handling of BL3-0 is platform specific */
7293d81d64SSandrine Bailleux 		bl2_plat_handle_bl30(&bl30_image_info);
7393d81d64SSandrine Bailleux 	}
7493d81d64SSandrine Bailleux #endif /* BL30_BASE */
7593d81d64SSandrine Bailleux 
7693d81d64SSandrine Bailleux 	return e;
7793d81d64SSandrine Bailleux }
7829fb905dSVikram Kanigiri 
7929fb905dSVikram Kanigiri /*******************************************************************************
8093d81d64SSandrine Bailleux  * Load the BL3-1 image.
8193d81d64SSandrine Bailleux  * The bl2_to_bl31_params and bl31_ep_info params will be updated with the
8293d81d64SSandrine Bailleux  * relevant BL3-1 information.
8393d81d64SSandrine Bailleux  * Return 0 on success, a negative error code otherwise.
844f6ad66aSAchin Gupta  ******************************************************************************/
8593d81d64SSandrine Bailleux static int load_bl31(bl31_params_t *bl2_to_bl31_params,
8693d81d64SSandrine Bailleux 		     entry_point_info_t *bl31_ep_info)
874f6ad66aSAchin Gupta {
88fb037bfbSDan Handley 	meminfo_t *bl2_tzram_layout;
894112bfa0SVikram Kanigiri 	int e;
904f6ad66aSAchin Gupta 
91*6ad2e461SDan Handley 	INFO("BL2: Loading BL3-1\n");
9293d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
9393d81d64SSandrine Bailleux 	assert(bl31_ep_info != NULL);
944f6ad66aSAchin Gupta 
954f6ad66aSAchin Gupta 	/* Find out how much free trusted ram remains after BL2 load */
96ee12f6f7SSandrine Bailleux 	bl2_tzram_layout = bl2_plat_sec_mem_layout();
974f6ad66aSAchin Gupta 
9893d81d64SSandrine Bailleux 	/* Set the X0 parameter to BL3-1 */
9903462671SAndrew Thoelke 	bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params;
10003462671SAndrew Thoelke 
1018f55dfb4SSandrine Bailleux 	/* Load the BL3-1 image */
1024112bfa0SVikram Kanigiri 	e = load_image(bl2_tzram_layout,
1034112bfa0SVikram Kanigiri 		       BL31_IMAGE_NAME,
1044112bfa0SVikram Kanigiri 		       BL31_BASE,
1054112bfa0SVikram Kanigiri 		       bl2_to_bl31_params->bl31_image_info,
1064112bfa0SVikram Kanigiri 		       bl31_ep_info);
1074f6ad66aSAchin Gupta 
10893d81d64SSandrine Bailleux 	if (e == 0)
1094112bfa0SVikram Kanigiri 		bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info,
1104112bfa0SVikram Kanigiri 					  bl31_ep_info);
111a3050ed5SAchin Gupta 
11293d81d64SSandrine Bailleux 	return e;
113561cd33eSHarry Liebel }
114e4d084eaSAchin Gupta 
11593d81d64SSandrine Bailleux /*******************************************************************************
11693d81d64SSandrine Bailleux  * Load the BL3-2 image if there's one.
11793d81d64SSandrine Bailleux  * The bl2_to_bl31_params param will be updated with the relevant BL3-2
11893d81d64SSandrine Bailleux  * information.
11993d81d64SSandrine Bailleux  * If a platform does not want to attempt to load BL3-2 image it must leave
12093d81d64SSandrine Bailleux  * BL32_BASE undefined.
12193d81d64SSandrine Bailleux  * Return 0 on success or if there's no BL3-2 image to load, a negative error
12293d81d64SSandrine Bailleux  * code otherwise.
12393d81d64SSandrine Bailleux  ******************************************************************************/
12493d81d64SSandrine Bailleux static int load_bl32(bl31_params_t *bl2_to_bl31_params)
12593d81d64SSandrine Bailleux {
12693d81d64SSandrine Bailleux 	int e = 0;
1271151c821SDan Handley #ifdef BL32_BASE
12893d81d64SSandrine Bailleux 	meminfo_t bl32_mem_info;
12993d81d64SSandrine Bailleux 
130*6ad2e461SDan Handley 	INFO("BL2: Loading BL3-2\n");
13193d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
13293d81d64SSandrine Bailleux 
13329fb905dSVikram Kanigiri 	/*
13493d81d64SSandrine Bailleux 	 * It is up to the platform to specify where BL3-2 should be loaded if
13593d81d64SSandrine Bailleux 	 * it exists. It could create space in the secure sram or point to a
1361151c821SDan Handley 	 * completely different memory.
13729fb905dSVikram Kanigiri 	 */
1386871c5d3SVikram Kanigiri 	bl2_plat_get_bl32_meminfo(&bl32_mem_info);
1396871c5d3SVikram Kanigiri 	e = load_image(&bl32_mem_info,
14029fb905dSVikram Kanigiri 		       BL32_IMAGE_NAME,
1414112bfa0SVikram Kanigiri 		       BL32_BASE,
1424112bfa0SVikram Kanigiri 		       bl2_to_bl31_params->bl32_image_info,
1434112bfa0SVikram Kanigiri 		       bl2_to_bl31_params->bl32_ep_info);
14429fb905dSVikram Kanigiri 
14593d81d64SSandrine Bailleux 	if (e == 0) {
1464112bfa0SVikram Kanigiri 		bl2_plat_set_bl32_ep_info(
1474112bfa0SVikram Kanigiri 			bl2_to_bl31_params->bl32_image_info,
1484112bfa0SVikram Kanigiri 			bl2_to_bl31_params->bl32_ep_info);
149a3050ed5SAchin Gupta 	}
1501151c821SDan Handley #endif /* BL32_BASE */
1514112bfa0SVikram Kanigiri 
15293d81d64SSandrine Bailleux 	return e;
15393d81d64SSandrine Bailleux }
15493d81d64SSandrine Bailleux 
15593d81d64SSandrine Bailleux /*******************************************************************************
15693d81d64SSandrine Bailleux  * Load the BL3-3 image.
15793d81d64SSandrine Bailleux  * The bl2_to_bl31_params param will be updated with the relevant BL3-3
15893d81d64SSandrine Bailleux  * information.
15993d81d64SSandrine Bailleux  * Return 0 on success, a negative error code otherwise.
16093d81d64SSandrine Bailleux  ******************************************************************************/
16193d81d64SSandrine Bailleux static int load_bl33(bl31_params_t *bl2_to_bl31_params)
16293d81d64SSandrine Bailleux {
16393d81d64SSandrine Bailleux 	meminfo_t bl33_mem_info;
16493d81d64SSandrine Bailleux 	int e;
16593d81d64SSandrine Bailleux 
166*6ad2e461SDan Handley 	INFO("BL2: Loading BL3-3\n");
16793d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
16893d81d64SSandrine Bailleux 
16993d81d64SSandrine Bailleux 	bl2_plat_get_bl33_meminfo(&bl33_mem_info);
17093d81d64SSandrine Bailleux 
17193d81d64SSandrine Bailleux 	/* Load the BL3-3 image in non-secure memory provided by the platform */
17293d81d64SSandrine Bailleux 	e = load_image(&bl33_mem_info,
17393d81d64SSandrine Bailleux 		       BL33_IMAGE_NAME,
17493d81d64SSandrine Bailleux 		       plat_get_ns_image_entrypoint(),
17593d81d64SSandrine Bailleux 		       bl2_to_bl31_params->bl33_image_info,
17693d81d64SSandrine Bailleux 		       bl2_to_bl31_params->bl33_ep_info);
17793d81d64SSandrine Bailleux 
17893d81d64SSandrine Bailleux 	if (e == 0)
17993d81d64SSandrine Bailleux 		bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info,
18093d81d64SSandrine Bailleux 					  bl2_to_bl31_params->bl33_ep_info);
18193d81d64SSandrine Bailleux 
18293d81d64SSandrine Bailleux 	return e;
18393d81d64SSandrine Bailleux }
18493d81d64SSandrine Bailleux 
18593d81d64SSandrine Bailleux /*******************************************************************************
18693d81d64SSandrine Bailleux  * The only thing to do in BL2 is to load further images and pass control to
18793d81d64SSandrine Bailleux  * BL3-1. The memory occupied by BL2 will be reclaimed by BL3-x stages. BL2 runs
18893d81d64SSandrine Bailleux  * entirely in S-EL1.
18993d81d64SSandrine Bailleux  ******************************************************************************/
19093d81d64SSandrine Bailleux void bl2_main(void)
19193d81d64SSandrine Bailleux {
19293d81d64SSandrine Bailleux 	bl31_params_t *bl2_to_bl31_params;
19393d81d64SSandrine Bailleux 	entry_point_info_t *bl31_ep_info;
19493d81d64SSandrine Bailleux 	int e;
19593d81d64SSandrine Bailleux 
196*6ad2e461SDan Handley 	NOTICE("BL2: %s\n", version_string);
197*6ad2e461SDan Handley 	NOTICE("BL2: %s\n", build_message);
198*6ad2e461SDan Handley 
19993d81d64SSandrine Bailleux 	/* Perform remaining generic architectural setup in S-EL1 */
20093d81d64SSandrine Bailleux 	bl2_arch_setup();
20193d81d64SSandrine Bailleux 
20293d81d64SSandrine Bailleux 	/* Perform platform setup in BL2 */
20393d81d64SSandrine Bailleux 	bl2_platform_setup();
20493d81d64SSandrine Bailleux 
20593d81d64SSandrine Bailleux 	/*
20693d81d64SSandrine Bailleux 	 * Load the subsequent bootloader images
20793d81d64SSandrine Bailleux 	 */
20893d81d64SSandrine Bailleux 	e = load_bl30();
20993d81d64SSandrine Bailleux 	if (e) {
21093d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-0 (%i)\n", e);
21193d81d64SSandrine Bailleux 		panic();
21293d81d64SSandrine Bailleux 	}
21393d81d64SSandrine Bailleux 
21493d81d64SSandrine Bailleux 	/*
21593d81d64SSandrine Bailleux 	 * Get a pointer to the memory the platform has set aside to pass
21693d81d64SSandrine Bailleux 	 * information to BL3-1.
21793d81d64SSandrine Bailleux 	 */
21893d81d64SSandrine Bailleux 	bl2_to_bl31_params = bl2_plat_get_bl31_params();
21993d81d64SSandrine Bailleux 	bl31_ep_info = bl2_plat_get_bl31_ep_info();
22093d81d64SSandrine Bailleux 
22193d81d64SSandrine Bailleux 	e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
22293d81d64SSandrine Bailleux 	if (e) {
22393d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-1 (%i)\n", e);
22493d81d64SSandrine Bailleux 		panic();
22593d81d64SSandrine Bailleux 	}
22693d81d64SSandrine Bailleux 
22793d81d64SSandrine Bailleux 	e = load_bl32(bl2_to_bl31_params);
22893d81d64SSandrine Bailleux 	if (e)
22993d81d64SSandrine Bailleux 		WARN("Failed to load BL3-2 (%i)\n", e);
23093d81d64SSandrine Bailleux 
23193d81d64SSandrine Bailleux 	e = load_bl33(bl2_to_bl31_params);
23293d81d64SSandrine Bailleux 	if (e) {
23393d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-3 (%i)\n", e);
23493d81d64SSandrine Bailleux 		panic();
23593d81d64SSandrine Bailleux 	}
23693d81d64SSandrine Bailleux 
23703462671SAndrew Thoelke 	/* Flush the params to be passed to memory */
23803462671SAndrew Thoelke 	bl2_plat_flush_bl31_params();
23903462671SAndrew Thoelke 
2404f6ad66aSAchin Gupta 	/*
24193d81d64SSandrine Bailleux 	 * Run BL3-1 via an SMC to BL1. Information on how to pass control to
24293d81d64SSandrine Bailleux 	 * the BL3-2 (if present) and BL3-3 software images will be passed to
24393d81d64SSandrine Bailleux 	 * BL3-1 as an argument.
2444f6ad66aSAchin Gupta 	 */
24503462671SAndrew Thoelke 	smc(RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
2464f6ad66aSAchin Gupta }
247