rpi3_common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <platform_def.h>
  8. #include <arch_helpers.h>
  9. #include <common/bl_common.h>
  10. #include <common/debug.h>
  11. #include <bl31/interrupt_mgmt.h>
  12. #include <drivers/console.h>
  13. #include <lib/xlat_tables/xlat_tables_v2.h>
  14. #include <rpi_hw.h>
  15. #include <rpi_shared.h>
  16. #define MAP_DEVICE0 MAP_REGION_FLAT(DEVICE0_BASE, \
  17. DEVICE0_SIZE, \
  18. MT_DEVICE | MT_RW | MT_SECURE)
  19. #ifdef SHARED_RAM_BASE
  20. #define MAP_SHARED_RAM MAP_REGION_FLAT(SHARED_RAM_BASE, \
  21. SHARED_RAM_SIZE, \
  22. MT_DEVICE | MT_RW | MT_SECURE)
  23. #endif
  24. #ifdef RPI3_PRELOADED_DTB_BASE
  25. #define MAP_NS_DTB MAP_REGION_FLAT(RPI3_PRELOADED_DTB_BASE, 0x10000, \
  26. MT_MEMORY | MT_RW | MT_NS)
  27. #endif
  28. #define MAP_NS_DRAM0 MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE, \
  29. MT_MEMORY | MT_RW | MT_NS)
  30. #define MAP_FIP MAP_REGION_FLAT(PLAT_RPI3_FIP_BASE, \
  31. PLAT_RPI3_FIP_MAX_SIZE, \
  32. MT_MEMORY | MT_RO | MT_NS)
  33. #define MAP_BL32_MEM MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE, \
  34. MT_MEMORY | MT_RW | MT_SECURE)
  35. #ifdef SPD_opteed
  36. #define MAP_OPTEE_PAGEABLE MAP_REGION_FLAT( \
  37. RPI3_OPTEE_PAGEABLE_LOAD_BASE, \
  38. RPI3_OPTEE_PAGEABLE_LOAD_SIZE, \
  39. MT_MEMORY | MT_RW | MT_SECURE)
  40. #endif
  41. /*
  42. * Table of regions for various BL stages to map using the MMU.
  43. */
  44. #ifdef IMAGE_BL1
  45. static const mmap_region_t plat_rpi3_mmap[] = {
  46. #ifdef MAP_SHARED_RAM
  47. MAP_SHARED_RAM,
  48. #endif
  49. MAP_DEVICE0,
  50. MAP_FIP,
  51. #ifdef SPD_opteed
  52. MAP_OPTEE_PAGEABLE,
  53. #endif
  54. {0}
  55. };
  56. #endif
  57. #ifdef IMAGE_BL2
  58. static const mmap_region_t plat_rpi3_mmap[] = {
  59. #ifdef MAP_SHARED_RAM
  60. MAP_SHARED_RAM,
  61. #endif
  62. MAP_DEVICE0,
  63. MAP_FIP,
  64. MAP_NS_DRAM0,
  65. #ifdef BL32_BASE
  66. MAP_BL32_MEM,
  67. #endif
  68. {0}
  69. };
  70. #endif
  71. #ifdef IMAGE_BL31
  72. static const mmap_region_t plat_rpi3_mmap[] = {
  73. #ifdef MAP_SHARED_RAM
  74. MAP_SHARED_RAM,
  75. #endif
  76. MAP_DEVICE0,
  77. #ifdef RPI3_PRELOADED_DTB_BASE
  78. MAP_NS_DTB,
  79. #endif
  80. #ifdef BL32_BASE
  81. MAP_BL32_MEM,
  82. #endif
  83. {0}
  84. };
  85. #endif
  86. /*******************************************************************************
  87. * Function that sets up the console
  88. ******************************************************************************/
  89. static console_t rpi3_console;
  90. void rpi3_console_init(void)
  91. {
  92. int console_scope = CONSOLE_FLAG_BOOT;
  93. int rc;
  94. if (RPI3_RUNTIME_UART != -1)
  95. console_scope |= CONSOLE_FLAG_RUNTIME;
  96. rc = rpi3_register_used_uart(&rpi3_console);
  97. if (rc == 0) {
  98. /*
  99. * The crash console doesn't use the multi console API, it uses
  100. * the core console functions directly. It is safe to call panic
  101. * and let it print debug information.
  102. */
  103. panic();
  104. }
  105. console_set_scope(&rpi3_console, console_scope);
  106. }
  107. /*******************************************************************************
  108. * Function that sets up the translation tables.
  109. ******************************************************************************/
  110. void rpi3_setup_page_tables(uintptr_t total_base, size_t total_size,
  111. uintptr_t code_start, uintptr_t code_limit,
  112. uintptr_t rodata_start, uintptr_t rodata_limit
  113. #if USE_COHERENT_MEM
  114. , uintptr_t coh_start, uintptr_t coh_limit
  115. #endif
  116. )
  117. {
  118. /*
  119. * Map the Trusted SRAM with appropriate memory attributes.
  120. * Subsequent mappings will adjust the attributes for specific regions.
  121. */
  122. VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
  123. (void *) total_base, (void *) (total_base + total_size));
  124. mmap_add_region(total_base, total_base,
  125. total_size,
  126. MT_MEMORY | MT_RW | MT_SECURE);
  127. /* Re-map the code section */
  128. VERBOSE("Code region: %p - %p\n",
  129. (void *) code_start, (void *) code_limit);
  130. mmap_add_region(code_start, code_start,
  131. code_limit - code_start,
  132. MT_CODE | MT_SECURE);
  133. /* Re-map the read-only data section */
  134. VERBOSE("Read-only data region: %p - %p\n",
  135. (void *) rodata_start, (void *) rodata_limit);
  136. mmap_add_region(rodata_start, rodata_start,
  137. rodata_limit - rodata_start,
  138. MT_RO_DATA | MT_SECURE);
  139. #if USE_COHERENT_MEM
  140. /* Re-map the coherent memory region */
  141. VERBOSE("Coherent region: %p - %p\n",
  142. (void *) coh_start, (void *) coh_limit);
  143. mmap_add_region(coh_start, coh_start,
  144. coh_limit - coh_start,
  145. MT_DEVICE | MT_RW | MT_SECURE);
  146. #endif
  147. mmap_add(plat_rpi3_mmap);
  148. init_xlat_tables();
  149. }
  150. /*******************************************************************************
  151. * Gets SPSR for BL32 entry
  152. ******************************************************************************/
  153. uint32_t rpi3_get_spsr_for_bl32_entry(void)
  154. {
  155. /*
  156. * The Secure Payload Dispatcher service is responsible for
  157. * setting the SPSR prior to entry into the BL32 image.
  158. */
  159. return 0;
  160. }
  161. /*******************************************************************************
  162. * Gets SPSR for BL33 entry
  163. ******************************************************************************/
  164. uint32_t rpi3_get_spsr_for_bl33_entry(void)
  165. {
  166. #if RPI3_BL33_IN_AARCH32
  167. INFO("BL33 will boot in Non-secure AArch32 Hypervisor mode\n");
  168. return SPSR_MODE32(MODE32_hyp, SPSR_T_ARM, SPSR_E_LITTLE,
  169. DISABLE_ALL_EXCEPTIONS);
  170. #else
  171. return SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  172. #endif
  173. }
  174. unsigned int plat_get_syscnt_freq2(void)
  175. {
  176. return SYS_COUNTER_FREQ_IN_TICKS;
  177. }
  178. uint32_t plat_ic_get_pending_interrupt_type(void)
  179. {
  180. ERROR("rpi3: Interrupt routed to EL3.\n");
  181. return INTR_TYPE_INVAL;
  182. }
  183. uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state)
  184. {
  185. assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) ||
  186. (type == INTR_TYPE_NS));
  187. assert(sec_state_is_valid(security_state));
  188. /* Non-secure interrupts are signalled on the IRQ line always. */
  189. if (type == INTR_TYPE_NS)
  190. return __builtin_ctz(SCR_IRQ_BIT);
  191. /* Secure interrupts are signalled on the FIQ line always. */
  192. return __builtin_ctz(SCR_FIQ_BIT);
  193. }