bl31_zynqmp_setup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2023-2024, Advanced Micro Devices, Inc. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <bl31/bl31.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <common/fdt_fixup.h>
  13. #include <common/fdt_wrappers.h>
  14. #include <lib/mmio.h>
  15. #include <lib/xlat_tables/xlat_tables_v2.h>
  16. #include <libfdt.h>
  17. #include <plat/arm/common/plat_arm.h>
  18. #include <plat/common/platform.h>
  19. #include <plat_console.h>
  20. #include <custom_svc.h>
  21. #include <plat_fdt.h>
  22. #include <plat_private.h>
  23. #include <plat_startup.h>
  24. #include <zynqmp_def.h>
  25. static entry_point_info_t bl32_image_ep_info;
  26. static entry_point_info_t bl33_image_ep_info;
  27. /*
  28. * Return a pointer to the 'entry_point_info' structure of the next image for
  29. * the security state specified. BL33 corresponds to the non-secure image type
  30. * while BL32 corresponds to the secure image type. A NULL pointer is returned
  31. * if the image does not exist.
  32. */
  33. struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type)
  34. {
  35. entry_point_info_t *next_image_info;
  36. assert(sec_state_is_valid(type));
  37. if (type == NON_SECURE) {
  38. next_image_info = &bl33_image_ep_info;
  39. } else {
  40. next_image_info = &bl32_image_ep_info;
  41. }
  42. return next_image_info;
  43. }
  44. /*
  45. * Set the build time defaults. We want to do this when doing a JTAG boot
  46. * or if we can't find any other config data.
  47. */
  48. static inline void bl31_set_default_config(void)
  49. {
  50. bl32_image_ep_info.pc = BL32_BASE;
  51. bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
  52. bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
  53. bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
  54. DISABLE_ALL_EXCEPTIONS);
  55. }
  56. /*
  57. * Perform any BL31 specific platform actions. Here is an opportunity to copy
  58. * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before they
  59. * are lost (potentially). This needs to be done before the MMU is initialized
  60. * so that the memory layout can be used while creating page tables.
  61. */
  62. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  63. u_register_t arg2, u_register_t arg3)
  64. {
  65. (void)arg0;
  66. (void)arg1;
  67. (void)arg2;
  68. (void)arg3;
  69. uint64_t tfa_handoff_addr;
  70. setup_console();
  71. /* Initialize the platform config for future decision making */
  72. zynqmp_config_setup();
  73. /*
  74. * Do initial security configuration to allow DRAM/device access. On
  75. * Base ZYNQMP only DRAM security is programmable (via TrustZone), but
  76. * other platforms might have more programmable security devices
  77. * present.
  78. */
  79. /* Populate common information for BL32 and BL33 */
  80. SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
  81. SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
  82. SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
  83. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  84. tfa_handoff_addr = mmio_read_32(PMU_GLOBAL_GEN_STORAGE6);
  85. if (zynqmp_get_bootmode() == ZYNQMP_BOOTMODE_JTAG) {
  86. bl31_set_default_config();
  87. } else {
  88. /* use parameters from XBL */
  89. enum xbl_handoff ret = xbl_handover(&bl32_image_ep_info,
  90. &bl33_image_ep_info,
  91. tfa_handoff_addr);
  92. if (ret != XBL_HANDOFF_SUCCESS) {
  93. panic();
  94. }
  95. }
  96. if (bl32_image_ep_info.pc != 0) {
  97. NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
  98. }
  99. if (bl33_image_ep_info.pc != 0) {
  100. NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
  101. }
  102. custom_early_setup();
  103. }
  104. #if ZYNQMP_WDT_RESTART
  105. static zynmp_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
  106. int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
  107. {
  108. static uint32_t index;
  109. uint32_t i;
  110. /* Validate 'handler' and 'id' parameters */
  111. if (!handler || index >= MAX_INTR_EL3) {
  112. return -EINVAL;
  113. }
  114. /* Check if a handler has already been registered */
  115. for (i = 0; i < index; i++) {
  116. if (id == type_el3_interrupt_table[i].id) {
  117. return -EALREADY;
  118. }
  119. }
  120. type_el3_interrupt_table[index].id = id;
  121. type_el3_interrupt_table[index].handler = handler;
  122. index++;
  123. return 0;
  124. }
  125. static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
  126. void *handle, void *cookie)
  127. {
  128. uint32_t intr_id;
  129. uint32_t i;
  130. interrupt_type_handler_t handler = NULL;
  131. intr_id = plat_ic_get_pending_interrupt_id();
  132. for (i = 0; i < MAX_INTR_EL3; i++) {
  133. if (intr_id == type_el3_interrupt_table[i].id) {
  134. handler = type_el3_interrupt_table[i].handler;
  135. }
  136. }
  137. if (handler != NULL) {
  138. return handler(intr_id, flags, handle, cookie);
  139. }
  140. return 0;
  141. }
  142. #endif
  143. void bl31_platform_setup(void)
  144. {
  145. prepare_dtb();
  146. /* Initialize the gic cpu and distributor interfaces */
  147. plat_arm_gic_driver_init();
  148. plat_arm_gic_init();
  149. }
  150. void bl31_plat_runtime_setup(void)
  151. {
  152. #if ZYNQMP_WDT_RESTART
  153. uint64_t flags = 0;
  154. uint64_t rc;
  155. set_interrupt_rm_flag(flags, NON_SECURE);
  156. rc = register_interrupt_type_handler(INTR_TYPE_EL3,
  157. rdo_el3_interrupt_handler, flags);
  158. if (rc) {
  159. panic();
  160. }
  161. #endif
  162. custom_runtime_setup();
  163. }
  164. /*
  165. * Perform the very early platform specific architectural setup here.
  166. */
  167. void bl31_plat_arch_setup(void)
  168. {
  169. plat_arm_interconnect_init();
  170. plat_arm_interconnect_enter_coherency();
  171. const mmap_region_t bl_regions[] = {
  172. #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE))
  173. MAP_REGION_FLAT(XILINX_OF_BOARD_DTB_ADDR, XILINX_OF_BOARD_DTB_MAX_SIZE,
  174. MT_MEMORY | MT_RW | MT_NS),
  175. #endif
  176. MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
  177. MT_MEMORY | MT_RW | MT_SECURE),
  178. MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
  179. MT_CODE | MT_SECURE),
  180. MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
  181. MT_RO_DATA | MT_SECURE),
  182. MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
  183. BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
  184. MT_DEVICE | MT_RW | MT_SECURE),
  185. {0}
  186. };
  187. custom_mmap_add();
  188. setup_page_tables(bl_regions, plat_get_mmap());
  189. enable_mmu(0);
  190. }