xref: /rk3399_ARM-atf/plat/rpi/common/rpi3_common.c (revision 7e848540159ba8fbb0577c76e4dc0c5bbc542489)
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>
171c800903SAbhi 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,
78*9acaadedSAbhi Singh #if MEASURED_BOOT
79*9acaadedSAbhi Singh 	RPI3_MAP_BL1_RW,
80*9acaadedSAbhi Singh #endif
814f2b9848SAndre Przywara 	MAP_NS_DRAM0,
824f2b9848SAndre Przywara #ifdef BL32_BASE
834f2b9848SAndre Przywara 	MAP_BL32_MEM,
844f2b9848SAndre Przywara #endif
854f2b9848SAndre Przywara 	{0}
864f2b9848SAndre Przywara };
874f2b9848SAndre Przywara #endif
884f2b9848SAndre Przywara 
894f2b9848SAndre Przywara #ifdef IMAGE_BL31
904f2b9848SAndre Przywara static const mmap_region_t plat_rpi3_mmap[] = {
91a95e6415SAndre Przywara #ifdef MAP_SHARED_RAM
924f2b9848SAndre Przywara 	MAP_SHARED_RAM,
93a95e6415SAndre Przywara #endif
944f2b9848SAndre Przywara 	MAP_DEVICE0,
954f2b9848SAndre Przywara #ifdef RPI3_PRELOADED_DTB_BASE
964f2b9848SAndre Przywara 	MAP_NS_DTB,
974f2b9848SAndre Przywara #endif
984f2b9848SAndre Przywara #ifdef BL32_BASE
994f2b9848SAndre Przywara 	MAP_BL32_MEM,
1004f2b9848SAndre Przywara #endif
1014f2b9848SAndre Przywara 	{0}
1024f2b9848SAndre Przywara };
1034f2b9848SAndre Przywara #endif
1044f2b9848SAndre Przywara 
1054f2b9848SAndre Przywara /*******************************************************************************
1064f2b9848SAndre Przywara  * Function that sets up the console
1074f2b9848SAndre Przywara  ******************************************************************************/
10898964f05SAndre Przywara static console_t rpi3_console;
1094f2b9848SAndre Przywara 
rpi3_console_init(void)110795aefe5SAndre Przywara void rpi3_console_init(void)
1114f2b9848SAndre Przywara {
1124f2b9848SAndre Przywara 	int console_scope = CONSOLE_FLAG_BOOT;
113795aefe5SAndre Przywara 	int rc;
114795aefe5SAndre Przywara 
115795aefe5SAndre Przywara 	if (RPI3_RUNTIME_UART != -1)
1164f2b9848SAndre Przywara 		console_scope |= CONSOLE_FLAG_RUNTIME;
117795aefe5SAndre Przywara 
118b5029782SMario Bălănică 	rc = rpi3_register_used_uart(&rpi3_console);
119795aefe5SAndre Przywara 
1204f2b9848SAndre Przywara 	if (rc == 0) {
1214f2b9848SAndre Przywara 		/*
1224f2b9848SAndre Przywara 		 * The crash console doesn't use the multi console API, it uses
1234f2b9848SAndre Przywara 		 * the core console functions directly. It is safe to call panic
1244f2b9848SAndre Przywara 		 * and let it print debug information.
1254f2b9848SAndre Przywara 		 */
1264f2b9848SAndre Przywara 		panic();
1274f2b9848SAndre Przywara 	}
1284f2b9848SAndre Przywara 
12998964f05SAndre Przywara 	console_set_scope(&rpi3_console, console_scope);
1304f2b9848SAndre Przywara }
1314f2b9848SAndre Przywara 
1324f2b9848SAndre Przywara /*******************************************************************************
1334f2b9848SAndre Przywara  * Function that sets up the translation tables.
1344f2b9848SAndre Przywara  ******************************************************************************/
rpi3_setup_page_tables(uintptr_t total_base,size_t total_size,uintptr_t code_start,uintptr_t code_limit,uintptr_t rodata_start,uintptr_t rodata_limit,uintptr_t coh_start,uintptr_t coh_limit)1354f2b9848SAndre Przywara void rpi3_setup_page_tables(uintptr_t total_base, size_t total_size,
1364f2b9848SAndre Przywara 			    uintptr_t code_start, uintptr_t code_limit,
1374f2b9848SAndre Przywara 			    uintptr_t rodata_start, uintptr_t rodata_limit
1384f2b9848SAndre Przywara #if USE_COHERENT_MEM
1394f2b9848SAndre Przywara 			    , uintptr_t coh_start, uintptr_t coh_limit
1404f2b9848SAndre Przywara #endif
1414f2b9848SAndre Przywara 			    )
1424f2b9848SAndre Przywara {
1434f2b9848SAndre Przywara 	/*
1444f2b9848SAndre Przywara 	 * Map the Trusted SRAM with appropriate memory attributes.
1454f2b9848SAndre Przywara 	 * Subsequent mappings will adjust the attributes for specific regions.
1464f2b9848SAndre Przywara 	 */
1474f2b9848SAndre Przywara 	VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
1484f2b9848SAndre Przywara 		(void *) total_base, (void *) (total_base + total_size));
1494f2b9848SAndre Przywara 	mmap_add_region(total_base, total_base,
1504f2b9848SAndre Przywara 			total_size,
1514f2b9848SAndre Przywara 			MT_MEMORY | MT_RW | MT_SECURE);
1524f2b9848SAndre Przywara 
1534f2b9848SAndre Przywara 	/* Re-map the code section */
1544f2b9848SAndre Przywara 	VERBOSE("Code region: %p - %p\n",
1554f2b9848SAndre Przywara 		(void *) code_start, (void *) code_limit);
1564f2b9848SAndre Przywara 	mmap_add_region(code_start, code_start,
1574f2b9848SAndre Przywara 			code_limit - code_start,
1584f2b9848SAndre Przywara 			MT_CODE | MT_SECURE);
1594f2b9848SAndre Przywara 
1604f2b9848SAndre Przywara 	/* Re-map the read-only data section */
1614f2b9848SAndre Przywara 	VERBOSE("Read-only data region: %p - %p\n",
1624f2b9848SAndre Przywara 		(void *) rodata_start, (void *) rodata_limit);
1634f2b9848SAndre Przywara 	mmap_add_region(rodata_start, rodata_start,
1644f2b9848SAndre Przywara 			rodata_limit - rodata_start,
1654f2b9848SAndre Przywara 			MT_RO_DATA | MT_SECURE);
1664f2b9848SAndre Przywara 
1674f2b9848SAndre Przywara #if USE_COHERENT_MEM
1684f2b9848SAndre Przywara 	/* Re-map the coherent memory region */
1694f2b9848SAndre Przywara 	VERBOSE("Coherent region: %p - %p\n",
1704f2b9848SAndre Przywara 		(void *) coh_start, (void *) coh_limit);
1714f2b9848SAndre Przywara 	mmap_add_region(coh_start, coh_start,
1724f2b9848SAndre Przywara 			coh_limit - coh_start,
1734f2b9848SAndre Przywara 			MT_DEVICE | MT_RW | MT_SECURE);
1744f2b9848SAndre Przywara #endif
1754f2b9848SAndre Przywara 
1764f2b9848SAndre Przywara 	mmap_add(plat_rpi3_mmap);
1774f2b9848SAndre Przywara 
1784f2b9848SAndre Przywara 	init_xlat_tables();
1794f2b9848SAndre Przywara }
1804f2b9848SAndre Przywara 
1814f2b9848SAndre Przywara /*******************************************************************************
1824f2b9848SAndre Przywara  * Gets SPSR for BL32 entry
1834f2b9848SAndre Przywara  ******************************************************************************/
rpi3_get_spsr_for_bl32_entry(void)1844f2b9848SAndre Przywara uint32_t rpi3_get_spsr_for_bl32_entry(void)
1854f2b9848SAndre Przywara {
1864f2b9848SAndre Przywara 	/*
1874f2b9848SAndre Przywara 	 * The Secure Payload Dispatcher service is responsible for
1884f2b9848SAndre Przywara 	 * setting the SPSR prior to entry into the BL32 image.
1894f2b9848SAndre Przywara 	 */
1904f2b9848SAndre Przywara 	return 0;
1914f2b9848SAndre Przywara }
1924f2b9848SAndre Przywara 
1934f2b9848SAndre Przywara /*******************************************************************************
1944f2b9848SAndre Przywara  * Gets SPSR for BL33 entry
1954f2b9848SAndre Przywara  ******************************************************************************/
rpi3_get_spsr_for_bl33_entry(void)1964f2b9848SAndre Przywara uint32_t rpi3_get_spsr_for_bl33_entry(void)
1974f2b9848SAndre Przywara {
1984f2b9848SAndre Przywara #if RPI3_BL33_IN_AARCH32
1994f2b9848SAndre Przywara 	INFO("BL33 will boot in Non-secure AArch32 Hypervisor mode\n");
2004f2b9848SAndre Przywara 	return SPSR_MODE32(MODE32_hyp, SPSR_T_ARM, SPSR_E_LITTLE,
2014f2b9848SAndre Przywara 			   DISABLE_ALL_EXCEPTIONS);
2024f2b9848SAndre Przywara #else
2034f2b9848SAndre Przywara 	return SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
2044f2b9848SAndre Przywara #endif
2054f2b9848SAndre Przywara }
2064f2b9848SAndre Przywara 
plat_get_syscnt_freq2(void)2074f2b9848SAndre Przywara unsigned int plat_get_syscnt_freq2(void)
2084f2b9848SAndre Przywara {
2094f2b9848SAndre Przywara 	return SYS_COUNTER_FREQ_IN_TICKS;
2104f2b9848SAndre Przywara }
2114f2b9848SAndre Przywara 
plat_ic_get_pending_interrupt_type(void)2124f2b9848SAndre Przywara uint32_t plat_ic_get_pending_interrupt_type(void)
2134f2b9848SAndre Przywara {
2144f2b9848SAndre Przywara 	ERROR("rpi3: Interrupt routed to EL3.\n");
2154f2b9848SAndre Przywara 	return INTR_TYPE_INVAL;
2164f2b9848SAndre Przywara }
2174f2b9848SAndre Przywara 
plat_interrupt_type_to_line(uint32_t type,uint32_t security_state)2184f2b9848SAndre Przywara uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state)
2194f2b9848SAndre Przywara {
2204f2b9848SAndre Przywara 	assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) ||
2214f2b9848SAndre Przywara 	       (type == INTR_TYPE_NS));
2224f2b9848SAndre Przywara 
2234f2b9848SAndre Przywara 	assert(sec_state_is_valid(security_state));
2244f2b9848SAndre Przywara 
2254f2b9848SAndre Przywara 	/* Non-secure interrupts are signalled on the IRQ line always. */
2264f2b9848SAndre Przywara 	if (type == INTR_TYPE_NS)
2274f2b9848SAndre Przywara 		return __builtin_ctz(SCR_IRQ_BIT);
2284f2b9848SAndre Przywara 
2294f2b9848SAndre Przywara 	/* Secure interrupts are signalled on the FIQ line always. */
2304f2b9848SAndre Przywara 	return  __builtin_ctz(SCR_FIQ_BIT);
2314f2b9848SAndre Przywara }
2321c800903SAbhi Singh 
2331c800903SAbhi Singh #if MEASURED_BOOT || TRUSTED_BOARD_BOOT
plat_get_mbedtls_heap(void ** heap_addr,size_t * heap_size)2341c800903SAbhi Singh int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
2351c800903SAbhi Singh {
2361c800903SAbhi Singh 	return get_mbedtls_heap_helper(heap_addr, heap_size);
2371c800903SAbhi Singh }
2381c800903SAbhi Singh #endif
239