xref: /rk3399_ARM-atf/plat/rpi/common/rpi3_common.c (revision 1c8009035ef0a730a98112c40a9f3d0c2490f76a)
14f2b9848SAndre Przywara /*
2b5029782SMario Bălănică  * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
34f2b9848SAndre Przywara  *
44f2b9848SAndre Przywara  * SPDX-License-Identifier: BSD-3-Clause
54f2b9848SAndre Przywara  */
64f2b9848SAndre Przywara 
74f2b9848SAndre Przywara #include <assert.h>
84f2b9848SAndre Przywara 
94f2b9848SAndre Przywara #include <platform_def.h>
104f2b9848SAndre Przywara 
114f2b9848SAndre Przywara #include <arch_helpers.h>
124f2b9848SAndre Przywara #include <common/bl_common.h>
134f2b9848SAndre Przywara #include <common/debug.h>
144f2b9848SAndre Przywara #include <bl31/interrupt_mgmt.h>
154f2b9848SAndre Przywara #include <drivers/console.h>
164f2b9848SAndre Przywara #include <lib/xlat_tables/xlat_tables_v2.h>
17*1c800903SAbhi Singh #include <plat/common/platform.h>
184f2b9848SAndre Przywara 
194f2b9848SAndre Przywara #include <rpi_hw.h>
204f2b9848SAndre Przywara #include <rpi_shared.h>
214f2b9848SAndre Przywara 
224f2b9848SAndre Przywara #define MAP_DEVICE0	MAP_REGION_FLAT(DEVICE0_BASE,			\
234f2b9848SAndre Przywara 					DEVICE0_SIZE,			\
244f2b9848SAndre Przywara 					MT_DEVICE | MT_RW | MT_SECURE)
254f2b9848SAndre Przywara 
26a95e6415SAndre Przywara #ifdef SHARED_RAM_BASE
274f2b9848SAndre Przywara #define MAP_SHARED_RAM	MAP_REGION_FLAT(SHARED_RAM_BASE,		\
284f2b9848SAndre Przywara 					SHARED_RAM_SIZE,		\
294f2b9848SAndre Przywara 					MT_DEVICE | MT_RW | MT_SECURE)
30a95e6415SAndre Przywara #endif
314f2b9848SAndre Przywara 
324f2b9848SAndre Przywara #ifdef RPI3_PRELOADED_DTB_BASE
334f2b9848SAndre Przywara #define MAP_NS_DTB	MAP_REGION_FLAT(RPI3_PRELOADED_DTB_BASE, 0x10000, \
344f2b9848SAndre Przywara 					MT_MEMORY | MT_RW | MT_NS)
354f2b9848SAndre Przywara #endif
364f2b9848SAndre Przywara 
374f2b9848SAndre Przywara #define MAP_NS_DRAM0	MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE,	\
384f2b9848SAndre Przywara 					MT_MEMORY | MT_RW | MT_NS)
394f2b9848SAndre Przywara 
404f2b9848SAndre Przywara #define MAP_FIP		MAP_REGION_FLAT(PLAT_RPI3_FIP_BASE,		\
414f2b9848SAndre Przywara 					PLAT_RPI3_FIP_MAX_SIZE,		\
424f2b9848SAndre Przywara 					MT_MEMORY | MT_RO | MT_NS)
434f2b9848SAndre Przywara 
444f2b9848SAndre Przywara #define MAP_BL32_MEM	MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE,	\
454f2b9848SAndre Przywara 					MT_MEMORY | MT_RW | MT_SECURE)
464f2b9848SAndre Przywara 
474f2b9848SAndre Przywara #ifdef SPD_opteed
484f2b9848SAndre Przywara #define MAP_OPTEE_PAGEABLE	MAP_REGION_FLAT(		\
494f2b9848SAndre Przywara 				RPI3_OPTEE_PAGEABLE_LOAD_BASE,	\
504f2b9848SAndre Przywara 				RPI3_OPTEE_PAGEABLE_LOAD_SIZE,	\
514f2b9848SAndre Przywara 				MT_MEMORY | MT_RW | MT_SECURE)
524f2b9848SAndre Przywara #endif
534f2b9848SAndre Przywara 
544f2b9848SAndre Przywara /*
554f2b9848SAndre Przywara  * Table of regions for various BL stages to map using the MMU.
564f2b9848SAndre Przywara  */
574f2b9848SAndre Przywara #ifdef IMAGE_BL1
584f2b9848SAndre Przywara static const mmap_region_t plat_rpi3_mmap[] = {
59a95e6415SAndre Przywara #ifdef MAP_SHARED_RAM
604f2b9848SAndre Przywara 	MAP_SHARED_RAM,
61a95e6415SAndre Przywara #endif
624f2b9848SAndre Przywara 	MAP_DEVICE0,
634f2b9848SAndre Przywara 	MAP_FIP,
644f2b9848SAndre Przywara #ifdef SPD_opteed
654f2b9848SAndre Przywara 	MAP_OPTEE_PAGEABLE,
664f2b9848SAndre Przywara #endif
674f2b9848SAndre Przywara 	{0}
684f2b9848SAndre Przywara };
694f2b9848SAndre Przywara #endif
704f2b9848SAndre Przywara 
714f2b9848SAndre Przywara #ifdef IMAGE_BL2
724f2b9848SAndre Przywara static const mmap_region_t plat_rpi3_mmap[] = {
73a95e6415SAndre Przywara #ifdef MAP_SHARED_RAM
744f2b9848SAndre Przywara 	MAP_SHARED_RAM,
75a95e6415SAndre Przywara #endif
764f2b9848SAndre Przywara 	MAP_DEVICE0,
774f2b9848SAndre Przywara 	MAP_FIP,
784f2b9848SAndre Przywara 	MAP_NS_DRAM0,
794f2b9848SAndre Przywara #ifdef BL32_BASE
804f2b9848SAndre Przywara 	MAP_BL32_MEM,
814f2b9848SAndre Przywara #endif
824f2b9848SAndre Przywara 	{0}
834f2b9848SAndre Przywara };
844f2b9848SAndre Przywara #endif
854f2b9848SAndre Przywara 
864f2b9848SAndre Przywara #ifdef IMAGE_BL31
874f2b9848SAndre Przywara static const mmap_region_t plat_rpi3_mmap[] = {
88a95e6415SAndre Przywara #ifdef MAP_SHARED_RAM
894f2b9848SAndre Przywara 	MAP_SHARED_RAM,
90a95e6415SAndre Przywara #endif
914f2b9848SAndre Przywara 	MAP_DEVICE0,
924f2b9848SAndre Przywara #ifdef RPI3_PRELOADED_DTB_BASE
934f2b9848SAndre Przywara 	MAP_NS_DTB,
944f2b9848SAndre Przywara #endif
954f2b9848SAndre Przywara #ifdef BL32_BASE
964f2b9848SAndre Przywara 	MAP_BL32_MEM,
974f2b9848SAndre Przywara #endif
984f2b9848SAndre Przywara 	{0}
994f2b9848SAndre Przywara };
1004f2b9848SAndre Przywara #endif
1014f2b9848SAndre Przywara 
1024f2b9848SAndre Przywara /*******************************************************************************
1034f2b9848SAndre Przywara  * Function that sets up the console
1044f2b9848SAndre Przywara  ******************************************************************************/
10598964f05SAndre Przywara static console_t rpi3_console;
1064f2b9848SAndre Przywara 
107795aefe5SAndre Przywara void rpi3_console_init(void)
1084f2b9848SAndre Przywara {
1094f2b9848SAndre Przywara 	int console_scope = CONSOLE_FLAG_BOOT;
110795aefe5SAndre Przywara 	int rc;
111795aefe5SAndre Przywara 
112795aefe5SAndre Przywara 	if (RPI3_RUNTIME_UART != -1)
1134f2b9848SAndre Przywara 		console_scope |= CONSOLE_FLAG_RUNTIME;
114795aefe5SAndre Przywara 
115b5029782SMario Bălănică 	rc = rpi3_register_used_uart(&rpi3_console);
116795aefe5SAndre Przywara 
1174f2b9848SAndre Przywara 	if (rc == 0) {
1184f2b9848SAndre Przywara 		/*
1194f2b9848SAndre Przywara 		 * The crash console doesn't use the multi console API, it uses
1204f2b9848SAndre Przywara 		 * the core console functions directly. It is safe to call panic
1214f2b9848SAndre Przywara 		 * and let it print debug information.
1224f2b9848SAndre Przywara 		 */
1234f2b9848SAndre Przywara 		panic();
1244f2b9848SAndre Przywara 	}
1254f2b9848SAndre Przywara 
12698964f05SAndre Przywara 	console_set_scope(&rpi3_console, console_scope);
1274f2b9848SAndre Przywara }
1284f2b9848SAndre Przywara 
1294f2b9848SAndre Przywara /*******************************************************************************
1304f2b9848SAndre Przywara  * Function that sets up the translation tables.
1314f2b9848SAndre Przywara  ******************************************************************************/
1324f2b9848SAndre Przywara void rpi3_setup_page_tables(uintptr_t total_base, size_t total_size,
1334f2b9848SAndre Przywara 			    uintptr_t code_start, uintptr_t code_limit,
1344f2b9848SAndre Przywara 			    uintptr_t rodata_start, uintptr_t rodata_limit
1354f2b9848SAndre Przywara #if USE_COHERENT_MEM
1364f2b9848SAndre Przywara 			    , uintptr_t coh_start, uintptr_t coh_limit
1374f2b9848SAndre Przywara #endif
1384f2b9848SAndre Przywara 			    )
1394f2b9848SAndre Przywara {
1404f2b9848SAndre Przywara 	/*
1414f2b9848SAndre Przywara 	 * Map the Trusted SRAM with appropriate memory attributes.
1424f2b9848SAndre Przywara 	 * Subsequent mappings will adjust the attributes for specific regions.
1434f2b9848SAndre Przywara 	 */
1444f2b9848SAndre Przywara 	VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
1454f2b9848SAndre Przywara 		(void *) total_base, (void *) (total_base + total_size));
1464f2b9848SAndre Przywara 	mmap_add_region(total_base, total_base,
1474f2b9848SAndre Przywara 			total_size,
1484f2b9848SAndre Przywara 			MT_MEMORY | MT_RW | MT_SECURE);
1494f2b9848SAndre Przywara 
1504f2b9848SAndre Przywara 	/* Re-map the code section */
1514f2b9848SAndre Przywara 	VERBOSE("Code region: %p - %p\n",
1524f2b9848SAndre Przywara 		(void *) code_start, (void *) code_limit);
1534f2b9848SAndre Przywara 	mmap_add_region(code_start, code_start,
1544f2b9848SAndre Przywara 			code_limit - code_start,
1554f2b9848SAndre Przywara 			MT_CODE | MT_SECURE);
1564f2b9848SAndre Przywara 
1574f2b9848SAndre Przywara 	/* Re-map the read-only data section */
1584f2b9848SAndre Przywara 	VERBOSE("Read-only data region: %p - %p\n",
1594f2b9848SAndre Przywara 		(void *) rodata_start, (void *) rodata_limit);
1604f2b9848SAndre Przywara 	mmap_add_region(rodata_start, rodata_start,
1614f2b9848SAndre Przywara 			rodata_limit - rodata_start,
1624f2b9848SAndre Przywara 			MT_RO_DATA | MT_SECURE);
1634f2b9848SAndre Przywara 
1644f2b9848SAndre Przywara #if USE_COHERENT_MEM
1654f2b9848SAndre Przywara 	/* Re-map the coherent memory region */
1664f2b9848SAndre Przywara 	VERBOSE("Coherent region: %p - %p\n",
1674f2b9848SAndre Przywara 		(void *) coh_start, (void *) coh_limit);
1684f2b9848SAndre Przywara 	mmap_add_region(coh_start, coh_start,
1694f2b9848SAndre Przywara 			coh_limit - coh_start,
1704f2b9848SAndre Przywara 			MT_DEVICE | MT_RW | MT_SECURE);
1714f2b9848SAndre Przywara #endif
1724f2b9848SAndre Przywara 
1734f2b9848SAndre Przywara 	mmap_add(plat_rpi3_mmap);
1744f2b9848SAndre Przywara 
1754f2b9848SAndre Przywara 	init_xlat_tables();
1764f2b9848SAndre Przywara }
1774f2b9848SAndre Przywara 
1784f2b9848SAndre Przywara /*******************************************************************************
1794f2b9848SAndre Przywara  * Gets SPSR for BL32 entry
1804f2b9848SAndre Przywara  ******************************************************************************/
1814f2b9848SAndre Przywara uint32_t rpi3_get_spsr_for_bl32_entry(void)
1824f2b9848SAndre Przywara {
1834f2b9848SAndre Przywara 	/*
1844f2b9848SAndre Przywara 	 * The Secure Payload Dispatcher service is responsible for
1854f2b9848SAndre Przywara 	 * setting the SPSR prior to entry into the BL32 image.
1864f2b9848SAndre Przywara 	 */
1874f2b9848SAndre Przywara 	return 0;
1884f2b9848SAndre Przywara }
1894f2b9848SAndre Przywara 
1904f2b9848SAndre Przywara /*******************************************************************************
1914f2b9848SAndre Przywara  * Gets SPSR for BL33 entry
1924f2b9848SAndre Przywara  ******************************************************************************/
1934f2b9848SAndre Przywara uint32_t rpi3_get_spsr_for_bl33_entry(void)
1944f2b9848SAndre Przywara {
1954f2b9848SAndre Przywara #if RPI3_BL33_IN_AARCH32
1964f2b9848SAndre Przywara 	INFO("BL33 will boot in Non-secure AArch32 Hypervisor mode\n");
1974f2b9848SAndre Przywara 	return SPSR_MODE32(MODE32_hyp, SPSR_T_ARM, SPSR_E_LITTLE,
1984f2b9848SAndre Przywara 			   DISABLE_ALL_EXCEPTIONS);
1994f2b9848SAndre Przywara #else
2004f2b9848SAndre Przywara 	return SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
2014f2b9848SAndre Przywara #endif
2024f2b9848SAndre Przywara }
2034f2b9848SAndre Przywara 
2044f2b9848SAndre Przywara unsigned int plat_get_syscnt_freq2(void)
2054f2b9848SAndre Przywara {
2064f2b9848SAndre Przywara 	return SYS_COUNTER_FREQ_IN_TICKS;
2074f2b9848SAndre Przywara }
2084f2b9848SAndre Przywara 
2094f2b9848SAndre Przywara uint32_t plat_ic_get_pending_interrupt_type(void)
2104f2b9848SAndre Przywara {
2114f2b9848SAndre Przywara 	ERROR("rpi3: Interrupt routed to EL3.\n");
2124f2b9848SAndre Przywara 	return INTR_TYPE_INVAL;
2134f2b9848SAndre Przywara }
2144f2b9848SAndre Przywara 
2154f2b9848SAndre Przywara uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state)
2164f2b9848SAndre Przywara {
2174f2b9848SAndre Przywara 	assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) ||
2184f2b9848SAndre Przywara 	       (type == INTR_TYPE_NS));
2194f2b9848SAndre Przywara 
2204f2b9848SAndre Przywara 	assert(sec_state_is_valid(security_state));
2214f2b9848SAndre Przywara 
2224f2b9848SAndre Przywara 	/* Non-secure interrupts are signalled on the IRQ line always. */
2234f2b9848SAndre Przywara 	if (type == INTR_TYPE_NS)
2244f2b9848SAndre Przywara 		return __builtin_ctz(SCR_IRQ_BIT);
2254f2b9848SAndre Przywara 
2264f2b9848SAndre Przywara 	/* Secure interrupts are signalled on the FIQ line always. */
2274f2b9848SAndre Przywara 	return  __builtin_ctz(SCR_FIQ_BIT);
2284f2b9848SAndre Przywara }
229*1c800903SAbhi Singh 
230*1c800903SAbhi Singh #if MEASURED_BOOT || TRUSTED_BOARD_BOOT
231*1c800903SAbhi Singh int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
232*1c800903SAbhi Singh {
233*1c800903SAbhi Singh 	return get_mbedtls_heap_helper(heap_addr, heap_size);
234*1c800903SAbhi Singh }
235*1c800903SAbhi Singh #endif
236