rpi3_bl31_setup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <libfdt.h>
  8. #include <platform_def.h>
  9. #include <common/bl_common.h>
  10. #include <lib/xlat_tables/xlat_mmu_helpers.h>
  11. #include <lib/xlat_tables/xlat_tables_defs.h>
  12. #include <plat/common/platform.h>
  13. #include <rpi_shared.h>
  14. /*
  15. * Placeholder variables for copying the arguments that have been passed to
  16. * BL31 from BL2.
  17. */
  18. static entry_point_info_t bl32_image_ep_info;
  19. static entry_point_info_t bl33_image_ep_info;
  20. /*******************************************************************************
  21. * Return a pointer to the 'entry_point_info' structure of the next image for
  22. * the security state specified. BL33 corresponds to the non-secure image type
  23. * while BL32 corresponds to the secure image type. A NULL pointer is returned
  24. * if the image does not exist.
  25. ******************************************************************************/
  26. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  27. {
  28. entry_point_info_t *next_image_info;
  29. assert(sec_state_is_valid(type) != 0);
  30. next_image_info = (type == NON_SECURE)
  31. ? &bl33_image_ep_info : &bl32_image_ep_info;
  32. /* None of the images can have 0x0 as the entrypoint. */
  33. if (next_image_info->pc) {
  34. return next_image_info;
  35. } else {
  36. return NULL;
  37. }
  38. }
  39. /*******************************************************************************
  40. * Return entrypoint of BL33.
  41. ******************************************************************************/
  42. uintptr_t plat_get_ns_image_entrypoint(void)
  43. {
  44. #ifdef PRELOADED_BL33_BASE
  45. return PRELOADED_BL33_BASE;
  46. #else
  47. return PLAT_RPI3_NS_IMAGE_OFFSET;
  48. #endif
  49. }
  50. /*******************************************************************************
  51. * Perform any BL31 early platform setup. Here is an opportunity to copy
  52. * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
  53. * they are lost (potentially). This needs to be done before the MMU is
  54. * initialized so that the memory layout can be used while creating page
  55. * tables. BL2 has flushed this information to memory, so we are guaranteed
  56. * to pick up good data.
  57. ******************************************************************************/
  58. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  59. u_register_t arg2, u_register_t arg3)
  60. {
  61. /* Initialize the console to provide early debug support */
  62. rpi3_console_init();
  63. /*
  64. * In debug builds, a special value is passed in 'arg1' to verify
  65. * platform parameters from BL2 to BL31. Not used in release builds.
  66. */
  67. assert(arg1 == RPI3_BL31_PLAT_PARAM_VAL);
  68. /* Check that params passed from BL2 are not NULL. */
  69. bl_params_t *params_from_bl2 = (bl_params_t *) arg0;
  70. assert(params_from_bl2 != NULL);
  71. assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
  72. assert(params_from_bl2->h.version >= VERSION_2);
  73. bl_params_node_t *bl_params = params_from_bl2->head;
  74. /*
  75. * Copy BL33 and BL32 (if present), entry point information.
  76. * They are stored in Secure RAM, in BL2's address space.
  77. */
  78. while (bl_params) {
  79. if (bl_params->image_id == BL32_IMAGE_ID) {
  80. bl32_image_ep_info = *bl_params->ep_info;
  81. }
  82. if (bl_params->image_id == BL33_IMAGE_ID) {
  83. bl33_image_ep_info = *bl_params->ep_info;
  84. }
  85. bl_params = bl_params->next_params_info;
  86. }
  87. if (bl33_image_ep_info.pc == 0) {
  88. panic();
  89. }
  90. #if RPI3_DIRECT_LINUX_BOOT
  91. # if RPI3_BL33_IN_AARCH32
  92. /*
  93. * According to the file ``Documentation/arm/Booting`` of the Linux
  94. * kernel tree, Linux expects:
  95. * r0 = 0
  96. * r1 = machine type number, optional in DT-only platforms (~0 if so)
  97. * r2 = Physical address of the device tree blob
  98. */
  99. VERBOSE("rpi3: Preparing to boot 32-bit Linux kernel\n");
  100. bl33_image_ep_info.args.arg0 = 0U;
  101. bl33_image_ep_info.args.arg1 = ~0U;
  102. bl33_image_ep_info.args.arg2 = (u_register_t) RPI3_PRELOADED_DTB_BASE;
  103. # else
  104. /*
  105. * According to the file ``Documentation/arm64/booting.txt`` of the
  106. * Linux kernel tree, Linux expects the physical address of the device
  107. * tree blob (DTB) in x0, while x1-x3 are reserved for future use and
  108. * must be 0.
  109. */
  110. VERBOSE("rpi3: Preparing to boot 64-bit Linux kernel\n");
  111. bl33_image_ep_info.args.arg0 = (u_register_t) RPI3_PRELOADED_DTB_BASE;
  112. bl33_image_ep_info.args.arg1 = 0ULL;
  113. bl33_image_ep_info.args.arg2 = 0ULL;
  114. bl33_image_ep_info.args.arg3 = 0ULL;
  115. # endif /* RPI3_BL33_IN_AARCH32 */
  116. #endif /* RPI3_DIRECT_LINUX_BOOT */
  117. }
  118. void bl31_plat_arch_setup(void)
  119. {
  120. rpi3_setup_page_tables(BL31_BASE, BL31_END - BL31_BASE,
  121. BL_CODE_BASE, BL_CODE_END,
  122. BL_RO_DATA_BASE, BL_RO_DATA_END
  123. #if USE_COHERENT_MEM
  124. , BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END
  125. #endif
  126. );
  127. enable_mmu_el3(0);
  128. }
  129. #ifdef RPI3_PRELOADED_DTB_BASE
  130. /*
  131. * Add information to the device tree (if any) about the reserved DRAM used by
  132. * the Trusted Firmware.
  133. */
  134. static void rpi3_dtb_add_mem_rsv(void)
  135. {
  136. int i, regions, rc;
  137. uint64_t addr, size;
  138. void *dtb = (void *)RPI3_PRELOADED_DTB_BASE;
  139. INFO("rpi3: Checking DTB...\n");
  140. /* Return if no device tree is detected */
  141. if (fdt_check_header(dtb) != 0)
  142. return;
  143. regions = fdt_num_mem_rsv(dtb);
  144. VERBOSE("rpi3: Found %d mem reserve region(s)\n", regions);
  145. /* We expect to find one reserved region that we can modify */
  146. if (regions < 1)
  147. return;
  148. /*
  149. * Look for the region that corresponds to the default boot firmware. It
  150. * starts at address 0, and it is not needed when the default firmware
  151. * is replaced by this port of the Trusted Firmware.
  152. */
  153. for (i = 0; i < regions; i++) {
  154. if (fdt_get_mem_rsv(dtb, i, &addr, &size) != 0)
  155. continue;
  156. if (addr != 0x0)
  157. continue;
  158. VERBOSE("rpi3: Firmware mem reserve region found\n");
  159. rc = fdt_del_mem_rsv(dtb, i);
  160. if (rc != 0) {
  161. INFO("rpi3: Can't remove mem reserve region (%d)\n", rc);
  162. }
  163. break;
  164. }
  165. if (i == regions) {
  166. VERBOSE("rpi3: Firmware mem reserve region not found\n");
  167. }
  168. /*
  169. * Reserve all SRAM. As said in the documentation, this isn't actually
  170. * secure memory, so it is needed to tell BL33 that this is a reserved
  171. * memory region. It doesn't guarantee it won't use it, though.
  172. */
  173. rc = fdt_add_mem_rsv(dtb, SEC_SRAM_BASE, SEC_SRAM_SIZE);
  174. if (rc != 0) {
  175. WARN("rpi3: Can't add mem reserve region (%d)\n", rc);
  176. }
  177. INFO("rpi3: Reserved 0x%llx - 0x%llx in DTB\n", SEC_SRAM_BASE,
  178. SEC_SRAM_BASE + SEC_SRAM_SIZE);
  179. }
  180. #endif
  181. void bl31_platform_setup(void)
  182. {
  183. #ifdef RPI3_PRELOADED_DTB_BASE
  184. /* Only modify a DTB if we know where to look for it */
  185. rpi3_dtb_add_mem_rsv();
  186. #endif
  187. }