rpi4_bl31_setup.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <inttypes.h>
  8. #include <stdint.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <drivers/arm/gicv2.h>
  12. #include <lib/mmio.h>
  13. #include <lib/xlat_tables/xlat_mmu_helpers.h>
  14. #include <lib/xlat_tables/xlat_tables_defs.h>
  15. #include <lib/xlat_tables/xlat_tables_v2.h>
  16. #include <plat/common/platform.h>
  17. #include <platform_def.h>
  18. #include <rpi_shared.h>
  19. /*
  20. * Fields at the beginning of armstub8.bin.
  21. * While building the BL31 image, we put the stub magic into the binary.
  22. * The GPU firmware detects this at boot time, clears that field as a
  23. * confirmation and puts the kernel and DT address in the following words.
  24. */
  25. extern uint32_t stub_magic;
  26. extern uint32_t dtb_ptr32;
  27. extern uint32_t kernel_entry32;
  28. static const gicv2_driver_data_t rpi4_gic_data = {
  29. .gicd_base = RPI4_GIC_GICD_BASE,
  30. .gicc_base = RPI4_GIC_GICC_BASE,
  31. };
  32. /*
  33. * To be filled by the code below. At the moment BL32 is not supported.
  34. * In the future these might be passed down from BL2.
  35. */
  36. static entry_point_info_t bl32_image_ep_info;
  37. static entry_point_info_t bl33_image_ep_info;
  38. /*******************************************************************************
  39. * Return a pointer to the 'entry_point_info' structure of the next image for
  40. * the security state specified. BL33 corresponds to the non-secure image type
  41. * while BL32 corresponds to the secure image type. A NULL pointer is returned
  42. * if the image does not exist.
  43. ******************************************************************************/
  44. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  45. {
  46. entry_point_info_t *next_image_info;
  47. assert(sec_state_is_valid(type) != 0);
  48. next_image_info = (type == NON_SECURE)
  49. ? &bl33_image_ep_info : &bl32_image_ep_info;
  50. /* None of the images can have 0x0 as the entrypoint. */
  51. if (next_image_info->pc) {
  52. return next_image_info;
  53. } else {
  54. return NULL;
  55. }
  56. }
  57. uintptr_t plat_get_ns_image_entrypoint(void)
  58. {
  59. #ifdef PRELOADED_BL33_BASE
  60. return PRELOADED_BL33_BASE;
  61. #else
  62. /* Cleared by the GPU if kernel address is valid. */
  63. if (stub_magic == 0)
  64. return kernel_entry32;
  65. WARN("Stub magic failure, using default kernel address 0x80000\n");
  66. return 0x80000;
  67. #endif
  68. }
  69. uintptr_t rpi4_get_dtb_address(void)
  70. {
  71. #ifdef RPI3_PRELOADED_DTB_BASE
  72. return RPI3_PRELOADED_DTB_BASE;
  73. #else
  74. /* Cleared by the GPU if DTB address is valid. */
  75. if (stub_magic == 0)
  76. return dtb_ptr32;
  77. WARN("Stub magic failure, DTB address unknown\n");
  78. return 0;
  79. #endif
  80. }
  81. static void ldelay(register_t delay)
  82. {
  83. __asm__ volatile (
  84. "1:\tcbz %0, 2f\n\t"
  85. "sub %0, %0, #1\n\t"
  86. "b 1b\n"
  87. "2:"
  88. : "=&r" (delay) : "0" (delay)
  89. );
  90. }
  91. /*******************************************************************************
  92. * Perform any BL31 early platform setup. Here is an opportunity to copy
  93. * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
  94. * they are lost (potentially). This needs to be done before the MMU is
  95. * initialized so that the memory layout can be used while creating page
  96. * tables. BL2 has flushed this information to memory, so we are guaranteed
  97. * to pick up good data.
  98. ******************************************************************************/
  99. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  100. u_register_t arg2, u_register_t arg3)
  101. {
  102. /*
  103. * LOCAL_CONTROL:
  104. * Bit 9 clear: Increment by 1 (vs. 2).
  105. * Bit 8 clear: Timer source is 19.2MHz crystal (vs. APB).
  106. */
  107. mmio_write_32(RPI4_LOCAL_CONTROL_BASE_ADDRESS, 0);
  108. /* LOCAL_PRESCALER; divide-by (0x80000000 / register_val) == 1 */
  109. mmio_write_32(RPI4_LOCAL_CONTROL_PRESCALER, 0x80000000);
  110. /* Early GPU firmware revisions need a little break here. */
  111. ldelay(100000);
  112. /* Initialize the console to provide early debug support. */
  113. rpi3_console_init();
  114. bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
  115. bl33_image_ep_info.spsr = rpi3_get_spsr_for_bl33_entry();
  116. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  117. #if RPI3_DIRECT_LINUX_BOOT
  118. # if RPI3_BL33_IN_AARCH32
  119. /*
  120. * According to the file ``Documentation/arm/Booting`` of the Linux
  121. * kernel tree, Linux expects:
  122. * r0 = 0
  123. * r1 = machine type number, optional in DT-only platforms (~0 if so)
  124. * r2 = Physical address of the device tree blob
  125. */
  126. VERBOSE("rpi: Preparing to boot 32-bit Linux kernel\n");
  127. bl33_image_ep_info.args.arg0 = 0U;
  128. bl33_image_ep_info.args.arg1 = ~0U;
  129. bl33_image_ep_info.args.arg2 = rpi4_get_dtb_address();
  130. # else
  131. /*
  132. * According to the file ``Documentation/arm64/booting.txt`` of the
  133. * Linux kernel tree, Linux expects the physical address of the device
  134. * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
  135. * must be 0.
  136. */
  137. VERBOSE("rpi: Preparing to boot 64-bit Linux kernel\n");
  138. bl33_image_ep_info.args.arg0 = rpi4_get_dtb_address();
  139. bl33_image_ep_info.args.arg1 = 0ULL;
  140. bl33_image_ep_info.args.arg2 = 0ULL;
  141. bl33_image_ep_info.args.arg3 = 0ULL;
  142. # endif /* RPI3_BL33_IN_AARCH32 */
  143. #endif /* RPI3_DIRECT_LINUX_BOOT */
  144. }
  145. void bl31_plat_arch_setup(void)
  146. {
  147. /*
  148. * Is the dtb_ptr32 pointer valid? If yes, map the DTB region.
  149. * We map the 2MB region the DTB start address lives in, plus
  150. * the next 2MB, to have enough room for expansion.
  151. */
  152. if (stub_magic == 0) {
  153. unsigned long long dtb_region = dtb_ptr32;
  154. dtb_region &= ~0x1fffff; /* Align to 2 MB. */
  155. mmap_add_region(dtb_region, dtb_region, 4U << 20,
  156. MT_MEMORY | MT_RW | MT_NS);
  157. }
  158. /*
  159. * Add the first page of memory, which holds the stub magic,
  160. * the kernel and the DT address.
  161. * This also holds the secondary CPU's entrypoints and mailboxes.
  162. */
  163. mmap_add_region(0, 0, 4096, MT_NON_CACHEABLE | MT_RW | MT_SECURE);
  164. rpi3_setup_page_tables(BL31_BASE, BL31_END - BL31_BASE,
  165. BL_CODE_BASE, BL_CODE_END,
  166. BL_RO_DATA_BASE, BL_RO_DATA_END
  167. #if USE_COHERENT_MEM
  168. , BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END
  169. #endif
  170. );
  171. enable_mmu_el3(0);
  172. }
  173. void bl31_platform_setup(void)
  174. {
  175. /* Configure the interrupt controller */
  176. gicv2_driver_init(&rpi4_gic_data);
  177. gicv2_distif_init();
  178. gicv2_pcpu_distif_init();
  179. gicv2_cpuif_enable();
  180. plat_rpi_bl31_custom_setup();
  181. }