xref: /rk3399_ARM-atf/bl1/bl1_main.c (revision a14988c6613d396293fd13eb052178a7e8e0d036)
14f6ad66aSAchin Gupta /*
2466bb285SZelalem  * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta  */
64f6ad66aSAchin Gupta 
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1197043ac9SDan Handley #include <arch.h>
12ed108b56SAlexei Fedorov #include <arch_features.h>
134f6ad66aSAchin Gupta #include <arch_helpers.h>
1409d40e0eSAntonio Nino Diaz #include <bl1/bl1.h>
1509d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1609d40e0eSAntonio Nino Diaz #include <common/debug.h>
1709d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_mod.h>
1809d40e0eSAntonio Nino Diaz #include <drivers/console.h>
1909d40e0eSAntonio Nino Diaz #include <lib/cpus/errata_report.h>
2009d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
22085e80ecSAntonio Nino Diaz #include <smccc_helpers.h>
2309d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
2409d40e0eSAntonio Nino Diaz 
252a4b4b71SIsla Mitchell #include "bl1_private.h"
2648bfb88eSYatharth Kochar 
277baff11fSYatharth Kochar static void bl1_load_bl2(void);
2829fb905dSVikram Kanigiri 
29530ceda5SAlexei Fedorov #if ENABLE_PAUTH
30530ceda5SAlexei Fedorov uint64_t bl1_apiakey[2];
31530ceda5SAlexei Fedorov #endif
32530ceda5SAlexei Fedorov 
338f55dfb4SSandrine Bailleux /*******************************************************************************
34101d01e2SSoby Mathew  * Helper utility to calculate the BL2 memory layout taking into consideration
35101d01e2SSoby Mathew  * the BL1 RW data assuming that it is at the top of the memory layout.
368f55dfb4SSandrine Bailleux  ******************************************************************************/
37101d01e2SSoby Mathew void bl1_calc_bl2_mem_layout(const meminfo_t *bl1_mem_layout,
388f55dfb4SSandrine Bailleux 			meminfo_t *bl2_mem_layout)
398f55dfb4SSandrine Bailleux {
408f55dfb4SSandrine Bailleux 	assert(bl1_mem_layout != NULL);
418f55dfb4SSandrine Bailleux 	assert(bl2_mem_layout != NULL);
428f55dfb4SSandrine Bailleux 
4342019bf4SYatharth Kochar 	/*
4442019bf4SYatharth Kochar 	 * Remove BL1 RW data from the scope of memory visible to BL2.
4542019bf4SYatharth Kochar 	 * This is assuming BL1 RW data is at the top of bl1_mem_layout.
4642019bf4SYatharth Kochar 	 */
4742019bf4SYatharth Kochar 	assert(BL1_RW_BASE > bl1_mem_layout->total_base);
4842019bf4SYatharth Kochar 	bl2_mem_layout->total_base = bl1_mem_layout->total_base;
4942019bf4SYatharth Kochar 	bl2_mem_layout->total_size = BL1_RW_BASE - bl1_mem_layout->total_base;
508f55dfb4SSandrine Bailleux 
51ee006a79SDeepika Bhavnani 	flush_dcache_range((uintptr_t)bl2_mem_layout, sizeof(meminfo_t));
528f55dfb4SSandrine Bailleux }
5329fb905dSVikram Kanigiri 
5429fb905dSVikram Kanigiri /*******************************************************************************
55cd7d6b0eSAntonio Nino Diaz  * Setup function for BL1.
56cd7d6b0eSAntonio Nino Diaz  ******************************************************************************/
57cd7d6b0eSAntonio Nino Diaz void bl1_setup(void)
58cd7d6b0eSAntonio Nino Diaz {
59cd7d6b0eSAntonio Nino Diaz 	/* Perform early platform-specific setup */
60cd7d6b0eSAntonio Nino Diaz 	bl1_early_platform_setup();
61cd7d6b0eSAntonio Nino Diaz 
62cd7d6b0eSAntonio Nino Diaz 	/* Perform late platform-specific setup */
63cd7d6b0eSAntonio Nino Diaz 	bl1_plat_arch_setup();
64ed108b56SAlexei Fedorov 
65ed108b56SAlexei Fedorov #if CTX_INCLUDE_PAUTH_REGS
66ed108b56SAlexei Fedorov 	/*
67ed108b56SAlexei Fedorov 	 * Assert that the ARMv8.3-PAuth registers are present or an access
68ed108b56SAlexei Fedorov 	 * fault will be triggered when they are being saved or restored.
69ed108b56SAlexei Fedorov 	 */
70ed108b56SAlexei Fedorov 	assert(is_armv8_3_pauth_present());
71ed108b56SAlexei Fedorov #endif /* CTX_INCLUDE_PAUTH_REGS */
72cd7d6b0eSAntonio Nino Diaz }
73cd7d6b0eSAntonio Nino Diaz 
74cd7d6b0eSAntonio Nino Diaz /*******************************************************************************
754f6ad66aSAchin Gupta  * Function to perform late architectural and platform specific initialization.
767baff11fSYatharth Kochar  * It also queries the platform to load and run next BL image. Only called
777baff11fSYatharth Kochar  * by the primary cpu after a cold boot.
784f6ad66aSAchin Gupta  ******************************************************************************/
794f6ad66aSAchin Gupta void bl1_main(void)
804f6ad66aSAchin Gupta {
817baff11fSYatharth Kochar 	unsigned int image_id;
827baff11fSYatharth Kochar 
836ad2e461SDan Handley 	/* Announce our arrival */
846ad2e461SDan Handley 	NOTICE(FIRMWARE_WELCOME_STR);
856ad2e461SDan Handley 	NOTICE("BL1: %s\n", version_string);
866ad2e461SDan Handley 	NOTICE("BL1: %s\n", build_message);
876ad2e461SDan Handley 
883443a702SJohn Powell 	INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE, (void *)BL1_RAM_LIMIT);
896ad2e461SDan Handley 
9010bcd761SJeenu Viswambharan 	print_errata_status();
914f6ad66aSAchin Gupta 
92aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
93f3b4914bSYatharth Kochar 	u_register_t val;
944f6ad66aSAchin Gupta 	/*
954f6ad66aSAchin Gupta 	 * Ensure that MMU/Caches and coherency are turned on
964f6ad66aSAchin Gupta 	 */
97402b3cf8SJulius Werner #ifdef __aarch64__
98ce4c820dSDan Handley 	val = read_sctlr_el3();
99402b3cf8SJulius Werner #else
100402b3cf8SJulius Werner 	val = read_sctlr();
101f3b4914bSYatharth Kochar #endif
1023443a702SJohn Powell 	assert((val & SCTLR_M_BIT) != 0);
1033443a702SJohn Powell 	assert((val & SCTLR_C_BIT) != 0);
1043443a702SJohn Powell 	assert((val & SCTLR_I_BIT) != 0);
105ce4c820dSDan Handley 	/*
106ce4c820dSDan Handley 	 * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the
107ce4c820dSDan Handley 	 * provided platform value
108ce4c820dSDan Handley 	 */
109ce4c820dSDan Handley 	val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK;
110ce4c820dSDan Handley 	/*
111ce4c820dSDan Handley 	 * If CWG is zero, then no CWG information is available but we can
112ce4c820dSDan Handley 	 * at least check the platform value is less than the architectural
113ce4c820dSDan Handley 	 * maximum.
114ce4c820dSDan Handley 	 */
115ce4c820dSDan Handley 	if (val != 0)
116ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE == SIZE_FROM_LOG2_WORDS(val));
117ce4c820dSDan Handley 	else
118ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE <= MAX_CACHE_LINE_SIZE);
119aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
1204f6ad66aSAchin Gupta 
1214f6ad66aSAchin Gupta 	/* Perform remaining generic architectural setup from EL3 */
1224f6ad66aSAchin Gupta 	bl1_arch_setup();
1234f6ad66aSAchin Gupta 
1247baff11fSYatharth Kochar #if TRUSTED_BOARD_BOOT
1257baff11fSYatharth Kochar 	/* Initialize authentication module */
1267baff11fSYatharth Kochar 	auth_mod_init();
1277baff11fSYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
1287baff11fSYatharth Kochar 
1294f6ad66aSAchin Gupta 	/* Perform platform setup in BL1. */
1304f6ad66aSAchin Gupta 	bl1_platform_setup();
1314f6ad66aSAchin Gupta 
132530ceda5SAlexei Fedorov #if ENABLE_PAUTH
133530ceda5SAlexei Fedorov 	/* Store APIAKey_EL1 key */
134530ceda5SAlexei Fedorov 	bl1_apiakey[0] = read_apiakeylo_el1();
135530ceda5SAlexei Fedorov 	bl1_apiakey[1] = read_apiakeyhi_el1();
136530ceda5SAlexei Fedorov #endif /* ENABLE_PAUTH */
137530ceda5SAlexei Fedorov 
1387baff11fSYatharth Kochar 	/* Get the image id of next image to load and run. */
1397baff11fSYatharth Kochar 	image_id = bl1_plat_get_next_image_id();
1407baff11fSYatharth Kochar 
14148bfb88eSYatharth Kochar 	/*
14248bfb88eSYatharth Kochar 	 * We currently interpret any image id other than
14348bfb88eSYatharth Kochar 	 * BL2_IMAGE_ID as the start of firmware update.
14448bfb88eSYatharth Kochar 	 */
1457baff11fSYatharth Kochar 	if (image_id == BL2_IMAGE_ID)
1467baff11fSYatharth Kochar 		bl1_load_bl2();
14748bfb88eSYatharth Kochar 	else
14848bfb88eSYatharth Kochar 		NOTICE("BL1-FWU: *******FWU Process Started*******\n");
1497baff11fSYatharth Kochar 
1507baff11fSYatharth Kochar 	bl1_prepare_next_image(image_id);
1510b32628eSAntonio Nino Diaz 
1520b32628eSAntonio Nino Diaz 	console_flush();
1537baff11fSYatharth Kochar }
1547baff11fSYatharth Kochar 
1557baff11fSYatharth Kochar /*******************************************************************************
1567baff11fSYatharth Kochar  * This function locates and loads the BL2 raw binary image in the trusted SRAM.
1577baff11fSYatharth Kochar  * Called by the primary cpu after a cold boot.
1587baff11fSYatharth Kochar  * TODO: Add support for alternative image load mechanism e.g using virtio/elf
1597baff11fSYatharth Kochar  * loader etc.
1607baff11fSYatharth Kochar  ******************************************************************************/
161ce3f9a6dSRoberto Vargas static void bl1_load_bl2(void)
1627baff11fSYatharth Kochar {
1633443a702SJohn Powell 	image_desc_t *desc;
1643443a702SJohn Powell 	image_info_t *info;
1657baff11fSYatharth Kochar 	int err;
1667baff11fSYatharth Kochar 
1677baff11fSYatharth Kochar 	/* Get the image descriptor */
1683443a702SJohn Powell 	desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
1693443a702SJohn Powell 	assert(desc != NULL);
1707baff11fSYatharth Kochar 
1717baff11fSYatharth Kochar 	/* Get the image info */
1723443a702SJohn Powell 	info = &desc->image_info;
17316948ae1SJuan Castillo 	INFO("BL1: Loading BL2\n");
17416948ae1SJuan Castillo 
175566034fcSSoby Mathew 	err = bl1_plat_handle_pre_image_load(BL2_IMAGE_ID);
1763443a702SJohn Powell 	if (err != 0) {
17711f001cbSMasahiro Yamada 		ERROR("Failure in pre image load handling of BL2 (%d)\n", err);
17811f001cbSMasahiro Yamada 		plat_error_handler(err);
17911f001cbSMasahiro Yamada 	}
18011f001cbSMasahiro Yamada 
1813443a702SJohn Powell 	err = load_auth_image(BL2_IMAGE_ID, info);
1823443a702SJohn Powell 	if (err != 0) {
1836ad2e461SDan Handley 		ERROR("Failed to load BL2 firmware.\n");
18440fc6cd1SJuan Castillo 		plat_error_handler(err);
1854112bfa0SVikram Kanigiri 	}
18601df3c14SJuan Castillo 
18711f001cbSMasahiro Yamada 	/* Allow platform to handle image information. */
188566034fcSSoby Mathew 	err = bl1_plat_handle_post_image_load(BL2_IMAGE_ID);
1893443a702SJohn Powell 	if (err != 0) {
19011f001cbSMasahiro Yamada 		ERROR("Failure in post image load handling of BL2 (%d)\n", err);
19111f001cbSMasahiro Yamada 		plat_error_handler(err);
19211f001cbSMasahiro Yamada 	}
19311f001cbSMasahiro Yamada 
1947baff11fSYatharth Kochar 	NOTICE("BL1: Booting BL2\n");
1954f6ad66aSAchin Gupta }
1964f6ad66aSAchin Gupta 
1974f6ad66aSAchin Gupta /*******************************************************************************
198f3b4914bSYatharth Kochar  * Function called just before handing over to the next BL to inform the user
199f3b4914bSYatharth Kochar  * about the boot progress. In debug mode, also print details about the BL
200f3b4914bSYatharth Kochar  * image's execution context.
2014f6ad66aSAchin Gupta  ******************************************************************************/
202f3b4914bSYatharth Kochar void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info)
2034f6ad66aSAchin Gupta {
204402b3cf8SJulius Werner #ifdef __aarch64__
205d178637dSJuan Castillo 	NOTICE("BL1: Booting BL31\n");
206402b3cf8SJulius Werner #else
207402b3cf8SJulius Werner 	NOTICE("BL1: Booting BL32\n");
208402b3cf8SJulius Werner #endif /* __aarch64__ */
209f3b4914bSYatharth Kochar 	print_entry_point_info(bl_ep_info);
2104f6ad66aSAchin Gupta }
21135e8c766SSandrine Bailleux 
21235e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT
21335e8c766SSandrine Bailleux void print_debug_loop_message(void)
21435e8c766SSandrine Bailleux {
21535e8c766SSandrine Bailleux 	NOTICE("BL1: Debug loop, spinning forever\n");
21635e8c766SSandrine Bailleux 	NOTICE("BL1: Please connect the debugger to continue\n");
21735e8c766SSandrine Bailleux }
21835e8c766SSandrine Bailleux #endif
21948bfb88eSYatharth Kochar 
22048bfb88eSYatharth Kochar /*******************************************************************************
22148bfb88eSYatharth Kochar  * Top level handler for servicing BL1 SMCs.
22248bfb88eSYatharth Kochar  ******************************************************************************/
2232fe75a2dSZelalem u_register_t bl1_smc_handler(unsigned int smc_fid,
2242fe75a2dSZelalem 	u_register_t x1,
2252fe75a2dSZelalem 	u_register_t x2,
2262fe75a2dSZelalem 	u_register_t x3,
2272fe75a2dSZelalem 	u_register_t x4,
22848bfb88eSYatharth Kochar 	void *cookie,
22948bfb88eSYatharth Kochar 	void *handle,
23048bfb88eSYatharth Kochar 	unsigned int flags)
23148bfb88eSYatharth Kochar {
232*a14988c6SJimmy Brisson 	/* BL1 Service UUID */
233*a14988c6SJimmy Brisson 	DEFINE_SVC_UUID2(bl1_svc_uid,
234*a14988c6SJimmy Brisson 		U(0xd46739fd), 0xcb72, 0x9a4d, 0xb5, 0x75,
235*a14988c6SJimmy Brisson 		0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a);
236*a14988c6SJimmy Brisson 
23748bfb88eSYatharth Kochar 
23848bfb88eSYatharth Kochar #if TRUSTED_BOARD_BOOT
23948bfb88eSYatharth Kochar 	/*
24048bfb88eSYatharth Kochar 	 * Dispatch FWU calls to FWU SMC handler and return its return
24148bfb88eSYatharth Kochar 	 * value
24248bfb88eSYatharth Kochar 	 */
24348bfb88eSYatharth Kochar 	if (is_fwu_fid(smc_fid)) {
24448bfb88eSYatharth Kochar 		return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
24548bfb88eSYatharth Kochar 			handle, flags);
24648bfb88eSYatharth Kochar 	}
24748bfb88eSYatharth Kochar #endif
24848bfb88eSYatharth Kochar 
24948bfb88eSYatharth Kochar 	switch (smc_fid) {
25048bfb88eSYatharth Kochar 	case BL1_SMC_CALL_COUNT:
25148bfb88eSYatharth Kochar 		SMC_RET1(handle, BL1_NUM_SMC_CALLS);
25248bfb88eSYatharth Kochar 
25348bfb88eSYatharth Kochar 	case BL1_SMC_UID:
25448bfb88eSYatharth Kochar 		SMC_UUID_RET(handle, bl1_svc_uid);
25548bfb88eSYatharth Kochar 
25648bfb88eSYatharth Kochar 	case BL1_SMC_VERSION:
25748bfb88eSYatharth Kochar 		SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER);
25848bfb88eSYatharth Kochar 
25948bfb88eSYatharth Kochar 	default:
26048bfb88eSYatharth Kochar 		WARN("Unimplemented BL1 SMC Call: 0x%x\n", smc_fid);
26148bfb88eSYatharth Kochar 		SMC_RET1(handle, SMC_UNK);
26248bfb88eSYatharth Kochar 	}
2633443a702SJohn Powell }
264a4409008Sdp-arm 
265a4409008Sdp-arm /*******************************************************************************
266a4409008Sdp-arm  * BL1 SMC wrapper.  This function is only used in AArch32 mode to ensure ABI
267a4409008Sdp-arm  * compliance when invoking bl1_smc_handler.
268a4409008Sdp-arm  ******************************************************************************/
2692fe75a2dSZelalem u_register_t bl1_smc_wrapper(uint32_t smc_fid,
270a4409008Sdp-arm 	void *cookie,
271a4409008Sdp-arm 	void *handle,
272a4409008Sdp-arm 	unsigned int flags)
273a4409008Sdp-arm {
2742fe75a2dSZelalem 	u_register_t x1, x2, x3, x4;
275a4409008Sdp-arm 
276466bb285SZelalem 	assert(handle != NULL);
277a4409008Sdp-arm 
278a4409008Sdp-arm 	get_smc_params_from_ctx(handle, x1, x2, x3, x4);
279a4409008Sdp-arm 	return bl1_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
280a4409008Sdp-arm }
281