xref: /rk3399_ARM-atf/bl1/bl1_main.c (revision 3443a7027d78a9ccebc6940f0a69300ec7c1ed44)
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 
2748bfb88eSYatharth Kochar /* BL1 Service UUID */
2803364865SRoberto Vargas DEFINE_SVC_UUID2(bl1_svc_uid,
29466bb285SZelalem 	U(0xd46739fd), 0xcb72, 0x9a4d, 0xb5, 0x75,
3048bfb88eSYatharth Kochar 	0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a);
314f6ad66aSAchin Gupta 
327baff11fSYatharth Kochar static void bl1_load_bl2(void);
3329fb905dSVikram Kanigiri 
34530ceda5SAlexei Fedorov #if ENABLE_PAUTH
35530ceda5SAlexei Fedorov uint64_t bl1_apiakey[2];
36530ceda5SAlexei Fedorov #endif
37530ceda5SAlexei Fedorov 
388f55dfb4SSandrine Bailleux /*******************************************************************************
39101d01e2SSoby Mathew  * Helper utility to calculate the BL2 memory layout taking into consideration
40101d01e2SSoby Mathew  * the BL1 RW data assuming that it is at the top of the memory layout.
418f55dfb4SSandrine Bailleux  ******************************************************************************/
42101d01e2SSoby Mathew void bl1_calc_bl2_mem_layout(const meminfo_t *bl1_mem_layout,
438f55dfb4SSandrine Bailleux 			meminfo_t *bl2_mem_layout)
448f55dfb4SSandrine Bailleux {
458f55dfb4SSandrine Bailleux 	assert(bl1_mem_layout != NULL);
468f55dfb4SSandrine Bailleux 	assert(bl2_mem_layout != NULL);
478f55dfb4SSandrine Bailleux 
4842019bf4SYatharth Kochar 	/*
4942019bf4SYatharth Kochar 	 * Remove BL1 RW data from the scope of memory visible to BL2.
5042019bf4SYatharth Kochar 	 * This is assuming BL1 RW data is at the top of bl1_mem_layout.
5142019bf4SYatharth Kochar 	 */
5242019bf4SYatharth Kochar 	assert(BL1_RW_BASE > bl1_mem_layout->total_base);
5342019bf4SYatharth Kochar 	bl2_mem_layout->total_base = bl1_mem_layout->total_base;
5442019bf4SYatharth Kochar 	bl2_mem_layout->total_size = BL1_RW_BASE - bl1_mem_layout->total_base;
558f55dfb4SSandrine Bailleux 
56ee006a79SDeepika Bhavnani 	flush_dcache_range((uintptr_t)bl2_mem_layout, sizeof(meminfo_t));
578f55dfb4SSandrine Bailleux }
5829fb905dSVikram Kanigiri 
5929fb905dSVikram Kanigiri /*******************************************************************************
60cd7d6b0eSAntonio Nino Diaz  * Setup function for BL1.
61cd7d6b0eSAntonio Nino Diaz  ******************************************************************************/
62cd7d6b0eSAntonio Nino Diaz void bl1_setup(void)
63cd7d6b0eSAntonio Nino Diaz {
64cd7d6b0eSAntonio Nino Diaz 	/* Perform early platform-specific setup */
65cd7d6b0eSAntonio Nino Diaz 	bl1_early_platform_setup();
66cd7d6b0eSAntonio Nino Diaz 
67cd7d6b0eSAntonio Nino Diaz 	/* Perform late platform-specific setup */
68cd7d6b0eSAntonio Nino Diaz 	bl1_plat_arch_setup();
69ed108b56SAlexei Fedorov 
70ed108b56SAlexei Fedorov #if CTX_INCLUDE_PAUTH_REGS
71ed108b56SAlexei Fedorov 	/*
72ed108b56SAlexei Fedorov 	 * Assert that the ARMv8.3-PAuth registers are present or an access
73ed108b56SAlexei Fedorov 	 * fault will be triggered when they are being saved or restored.
74ed108b56SAlexei Fedorov 	 */
75ed108b56SAlexei Fedorov 	assert(is_armv8_3_pauth_present());
76ed108b56SAlexei Fedorov #endif /* CTX_INCLUDE_PAUTH_REGS */
77cd7d6b0eSAntonio Nino Diaz }
78cd7d6b0eSAntonio Nino Diaz 
79cd7d6b0eSAntonio Nino Diaz /*******************************************************************************
804f6ad66aSAchin Gupta  * Function to perform late architectural and platform specific initialization.
817baff11fSYatharth Kochar  * It also queries the platform to load and run next BL image. Only called
827baff11fSYatharth Kochar  * by the primary cpu after a cold boot.
834f6ad66aSAchin Gupta  ******************************************************************************/
844f6ad66aSAchin Gupta void bl1_main(void)
854f6ad66aSAchin Gupta {
867baff11fSYatharth Kochar 	unsigned int image_id;
877baff11fSYatharth Kochar 
886ad2e461SDan Handley 	/* Announce our arrival */
896ad2e461SDan Handley 	NOTICE(FIRMWARE_WELCOME_STR);
906ad2e461SDan Handley 	NOTICE("BL1: %s\n", version_string);
916ad2e461SDan Handley 	NOTICE("BL1: %s\n", build_message);
926ad2e461SDan Handley 
93*3443a702SJohn Powell 	INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE, (void *)BL1_RAM_LIMIT);
946ad2e461SDan Handley 
9510bcd761SJeenu Viswambharan 	print_errata_status();
964f6ad66aSAchin Gupta 
97aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
98f3b4914bSYatharth Kochar 	u_register_t val;
994f6ad66aSAchin Gupta 	/*
1004f6ad66aSAchin Gupta 	 * Ensure that MMU/Caches and coherency are turned on
1014f6ad66aSAchin Gupta 	 */
102402b3cf8SJulius Werner #ifdef __aarch64__
103ce4c820dSDan Handley 	val = read_sctlr_el3();
104402b3cf8SJulius Werner #else
105402b3cf8SJulius Werner 	val = read_sctlr();
106f3b4914bSYatharth Kochar #endif
107*3443a702SJohn Powell 	assert((val & SCTLR_M_BIT) != 0);
108*3443a702SJohn Powell 	assert((val & SCTLR_C_BIT) != 0);
109*3443a702SJohn Powell 	assert((val & SCTLR_I_BIT) != 0);
110ce4c820dSDan Handley 	/*
111ce4c820dSDan Handley 	 * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the
112ce4c820dSDan Handley 	 * provided platform value
113ce4c820dSDan Handley 	 */
114ce4c820dSDan Handley 	val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK;
115ce4c820dSDan Handley 	/*
116ce4c820dSDan Handley 	 * If CWG is zero, then no CWG information is available but we can
117ce4c820dSDan Handley 	 * at least check the platform value is less than the architectural
118ce4c820dSDan Handley 	 * maximum.
119ce4c820dSDan Handley 	 */
120ce4c820dSDan Handley 	if (val != 0)
121ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE == SIZE_FROM_LOG2_WORDS(val));
122ce4c820dSDan Handley 	else
123ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE <= MAX_CACHE_LINE_SIZE);
124aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
1254f6ad66aSAchin Gupta 
1264f6ad66aSAchin Gupta 	/* Perform remaining generic architectural setup from EL3 */
1274f6ad66aSAchin Gupta 	bl1_arch_setup();
1284f6ad66aSAchin Gupta 
1297baff11fSYatharth Kochar #if TRUSTED_BOARD_BOOT
1307baff11fSYatharth Kochar 	/* Initialize authentication module */
1317baff11fSYatharth Kochar 	auth_mod_init();
1327baff11fSYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
1337baff11fSYatharth Kochar 
1344f6ad66aSAchin Gupta 	/* Perform platform setup in BL1. */
1354f6ad66aSAchin Gupta 	bl1_platform_setup();
1364f6ad66aSAchin Gupta 
137530ceda5SAlexei Fedorov #if ENABLE_PAUTH
138530ceda5SAlexei Fedorov 	/* Store APIAKey_EL1 key */
139530ceda5SAlexei Fedorov 	bl1_apiakey[0] = read_apiakeylo_el1();
140530ceda5SAlexei Fedorov 	bl1_apiakey[1] = read_apiakeyhi_el1();
141530ceda5SAlexei Fedorov #endif /* ENABLE_PAUTH */
142530ceda5SAlexei Fedorov 
1437baff11fSYatharth Kochar 	/* Get the image id of next image to load and run. */
1447baff11fSYatharth Kochar 	image_id = bl1_plat_get_next_image_id();
1457baff11fSYatharth Kochar 
14648bfb88eSYatharth Kochar 	/*
14748bfb88eSYatharth Kochar 	 * We currently interpret any image id other than
14848bfb88eSYatharth Kochar 	 * BL2_IMAGE_ID as the start of firmware update.
14948bfb88eSYatharth Kochar 	 */
1507baff11fSYatharth Kochar 	if (image_id == BL2_IMAGE_ID)
1517baff11fSYatharth Kochar 		bl1_load_bl2();
15248bfb88eSYatharth Kochar 	else
15348bfb88eSYatharth Kochar 		NOTICE("BL1-FWU: *******FWU Process Started*******\n");
1547baff11fSYatharth Kochar 
1557baff11fSYatharth Kochar 	bl1_prepare_next_image(image_id);
1560b32628eSAntonio Nino Diaz 
1570b32628eSAntonio Nino Diaz 	console_flush();
1587baff11fSYatharth Kochar }
1597baff11fSYatharth Kochar 
1607baff11fSYatharth Kochar /*******************************************************************************
1617baff11fSYatharth Kochar  * This function locates and loads the BL2 raw binary image in the trusted SRAM.
1627baff11fSYatharth Kochar  * Called by the primary cpu after a cold boot.
1637baff11fSYatharth Kochar  * TODO: Add support for alternative image load mechanism e.g using virtio/elf
1647baff11fSYatharth Kochar  * loader etc.
1657baff11fSYatharth Kochar  ******************************************************************************/
166ce3f9a6dSRoberto Vargas static void bl1_load_bl2(void)
1677baff11fSYatharth Kochar {
168*3443a702SJohn Powell 	image_desc_t *desc;
169*3443a702SJohn Powell 	image_info_t *info;
1707baff11fSYatharth Kochar 	int err;
1717baff11fSYatharth Kochar 
1727baff11fSYatharth Kochar 	/* Get the image descriptor */
173*3443a702SJohn Powell 	desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
174*3443a702SJohn Powell 	assert(desc != NULL);
1757baff11fSYatharth Kochar 
1767baff11fSYatharth Kochar 	/* Get the image info */
177*3443a702SJohn Powell 	info = &desc->image_info;
17816948ae1SJuan Castillo 	INFO("BL1: Loading BL2\n");
17916948ae1SJuan Castillo 
180566034fcSSoby Mathew 	err = bl1_plat_handle_pre_image_load(BL2_IMAGE_ID);
181*3443a702SJohn Powell 	if (err != 0) {
18211f001cbSMasahiro Yamada 		ERROR("Failure in pre image load handling of BL2 (%d)\n", err);
18311f001cbSMasahiro Yamada 		plat_error_handler(err);
18411f001cbSMasahiro Yamada 	}
18511f001cbSMasahiro Yamada 
186*3443a702SJohn Powell 	err = load_auth_image(BL2_IMAGE_ID, info);
187*3443a702SJohn Powell 	if (err != 0) {
1886ad2e461SDan Handley 		ERROR("Failed to load BL2 firmware.\n");
18940fc6cd1SJuan Castillo 		plat_error_handler(err);
1904112bfa0SVikram Kanigiri 	}
19101df3c14SJuan Castillo 
19211f001cbSMasahiro Yamada 	/* Allow platform to handle image information. */
193566034fcSSoby Mathew 	err = bl1_plat_handle_post_image_load(BL2_IMAGE_ID);
194*3443a702SJohn Powell 	if (err != 0) {
19511f001cbSMasahiro Yamada 		ERROR("Failure in post image load handling of BL2 (%d)\n", err);
19611f001cbSMasahiro Yamada 		plat_error_handler(err);
19711f001cbSMasahiro Yamada 	}
19811f001cbSMasahiro Yamada 
1997baff11fSYatharth Kochar 	NOTICE("BL1: Booting BL2\n");
2004f6ad66aSAchin Gupta }
2014f6ad66aSAchin Gupta 
2024f6ad66aSAchin Gupta /*******************************************************************************
203f3b4914bSYatharth Kochar  * Function called just before handing over to the next BL to inform the user
204f3b4914bSYatharth Kochar  * about the boot progress. In debug mode, also print details about the BL
205f3b4914bSYatharth Kochar  * image's execution context.
2064f6ad66aSAchin Gupta  ******************************************************************************/
207f3b4914bSYatharth Kochar void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info)
2084f6ad66aSAchin Gupta {
209402b3cf8SJulius Werner #ifdef __aarch64__
210d178637dSJuan Castillo 	NOTICE("BL1: Booting BL31\n");
211402b3cf8SJulius Werner #else
212402b3cf8SJulius Werner 	NOTICE("BL1: Booting BL32\n");
213402b3cf8SJulius Werner #endif /* __aarch64__ */
214f3b4914bSYatharth Kochar 	print_entry_point_info(bl_ep_info);
2154f6ad66aSAchin Gupta }
21635e8c766SSandrine Bailleux 
21735e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT
21835e8c766SSandrine Bailleux void print_debug_loop_message(void)
21935e8c766SSandrine Bailleux {
22035e8c766SSandrine Bailleux 	NOTICE("BL1: Debug loop, spinning forever\n");
22135e8c766SSandrine Bailleux 	NOTICE("BL1: Please connect the debugger to continue\n");
22235e8c766SSandrine Bailleux }
22335e8c766SSandrine Bailleux #endif
22448bfb88eSYatharth Kochar 
22548bfb88eSYatharth Kochar /*******************************************************************************
22648bfb88eSYatharth Kochar  * Top level handler for servicing BL1 SMCs.
22748bfb88eSYatharth Kochar  ******************************************************************************/
2282fe75a2dSZelalem u_register_t bl1_smc_handler(unsigned int smc_fid,
2292fe75a2dSZelalem 	u_register_t x1,
2302fe75a2dSZelalem 	u_register_t x2,
2312fe75a2dSZelalem 	u_register_t x3,
2322fe75a2dSZelalem 	u_register_t x4,
23348bfb88eSYatharth Kochar 	void *cookie,
23448bfb88eSYatharth Kochar 	void *handle,
23548bfb88eSYatharth Kochar 	unsigned int flags)
23648bfb88eSYatharth Kochar {
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 	}
263*3443a702SJohn 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