bl31_versal_setup.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2018-2022, Xilinx, Inc. All rights reserved.
  4. * Copyright (c) 2022-2024, Advanced Micro Devices, Inc. All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <bl31/bl31.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <drivers/generic_delay_timer.h>
  14. #include <lib/mmio.h>
  15. #include <lib/xlat_tables/xlat_tables_v2.h>
  16. #include <plat/common/platform.h>
  17. #include <plat_arm.h>
  18. #include <plat_console.h>
  19. #include <plat_clkfunc.h>
  20. #include <plat_fdt.h>
  21. #include <plat_private.h>
  22. #include <plat_startup.h>
  23. #include "pm_api_sys.h"
  24. #include "pm_client.h"
  25. #include <pm_ipi.h>
  26. #include <versal_def.h>
  27. static entry_point_info_t bl32_image_ep_info;
  28. static entry_point_info_t bl33_image_ep_info;
  29. /*
  30. * Return a pointer to the 'entry_point_info' structure of the next image for
  31. * the security state specified. BL33 corresponds to the non-secure image type
  32. * while BL32 corresponds to the secure image type. A NULL pointer is returned
  33. * if the image does not exist.
  34. */
  35. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  36. {
  37. assert(sec_state_is_valid(type));
  38. if (type == NON_SECURE) {
  39. return &bl33_image_ep_info;
  40. }
  41. return &bl32_image_ep_info;
  42. }
  43. /*
  44. * Set the build time defaults,if we can't find any config data.
  45. */
  46. static inline void bl31_set_default_config(void)
  47. {
  48. bl32_image_ep_info.pc = (uintptr_t)BL32_BASE;
  49. bl32_image_ep_info.spsr = (uint32_t)arm_get_spsr_for_bl32_entry();
  50. bl33_image_ep_info.pc = (uintptr_t)plat_get_ns_image_entrypoint();
  51. bl33_image_ep_info.spsr = (uint32_t)SPSR_64(MODE_EL2, MODE_SP_ELX,
  52. DISABLE_ALL_EXCEPTIONS);
  53. }
  54. /*
  55. * Perform any BL31 specific platform actions. Here is an opportunity to copy
  56. * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
  57. * are lost (potentially). This needs to be done before the MMU is initialized
  58. * so that the memory layout can be used while creating page tables.
  59. */
  60. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  61. u_register_t arg2, u_register_t arg3)
  62. {
  63. (void)arg0;
  64. (void)arg1;
  65. (void)arg2;
  66. (void)arg3;
  67. uint64_t tfa_handoff_addr;
  68. uint32_t payload[PAYLOAD_ARG_CNT], max_size = HANDOFF_PARAMS_MAX_SIZE;
  69. enum pm_ret_status ret_status;
  70. const uint64_t addr[HANDOFF_PARAMS_MAX_SIZE];
  71. /*
  72. * Do initial security configuration to allow DRAM/device access. On
  73. * Base VERSAL only DRAM security is programmable (via TrustZone), but
  74. * other platforms might have more programmable security devices
  75. * present.
  76. */
  77. versal_config_setup();
  78. /* Initialize the platform config for future decision making */
  79. board_detection();
  80. switch (platform_id) {
  81. case VERSAL_SPP:
  82. cpu_clock = 2720000;
  83. break;
  84. case VERSAL_EMU:
  85. cpu_clock = 212000;
  86. break;
  87. case VERSAL_QEMU:
  88. case VERSAL_SILICON:
  89. cpu_clock = 100000000;
  90. break;
  91. default:
  92. panic();
  93. }
  94. set_cnt_freq();
  95. generic_delay_timer_init();
  96. setup_console();
  97. NOTICE("TF-A running on %s %d\n", board_name_decode(), platform_version);
  98. /* Populate common information for BL32 and BL33 */
  99. SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
  100. SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
  101. SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
  102. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  103. PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, 1, PM_LOAD_GET_HANDOFF_PARAMS,
  104. (uintptr_t)addr >> 32U, (uintptr_t)addr, max_size);
  105. ret_status = pm_ipi_send_sync(primary_proc, payload, NULL, 0);
  106. if (ret_status == PM_RET_SUCCESS) {
  107. INFO("BL31: GET_HANDOFF_PARAMS call success=%d\n", ret_status);
  108. tfa_handoff_addr = (uintptr_t)&addr;
  109. } else {
  110. ERROR("BL31: GET_HANDOFF_PARAMS Failed, read tfa_handoff_addr from reg\n");
  111. tfa_handoff_addr = mmio_read_32(PMC_GLOBAL_GLOB_GEN_STORAGE4);
  112. }
  113. enum xbl_handoff ret = xbl_handover(&bl32_image_ep_info,
  114. &bl33_image_ep_info,
  115. tfa_handoff_addr);
  116. if ((ret == XBL_HANDOFF_NO_STRUCT) || (ret == XBL_HANDOFF_INVAL_STRUCT)) {
  117. bl31_set_default_config();
  118. } else if (ret == XBL_HANDOFF_TOO_MANY_PARTS) {
  119. ERROR("BL31: Error too many partitions %u\n", ret);
  120. } else if (ret != XBL_HANDOFF_SUCCESS) {
  121. panic();
  122. } else {
  123. INFO("BL31: PLM to TF-A handover success %u\n", ret);
  124. }
  125. NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
  126. NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
  127. }
  128. static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
  129. int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
  130. {
  131. static uint32_t index;
  132. uint32_t i;
  133. /* Validate 'handler' and 'id' parameters */
  134. if ((handler == NULL) || (index >= MAX_INTR_EL3)) {
  135. return -EINVAL;
  136. }
  137. /* Check if a handler has already been registered */
  138. for (i = 0; i < index; i++) {
  139. if (id == type_el3_interrupt_table[i].id) {
  140. return -EALREADY;
  141. }
  142. }
  143. type_el3_interrupt_table[index].id = id;
  144. type_el3_interrupt_table[index].handler = handler;
  145. index++;
  146. return 0;
  147. }
  148. static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
  149. void *handle, void *cookie)
  150. {
  151. (void)id;
  152. uint32_t intr_id;
  153. uint32_t i;
  154. interrupt_type_handler_t handler = NULL;
  155. intr_id = plat_ic_get_pending_interrupt_id();
  156. for (i = 0; i < MAX_INTR_EL3; i++) {
  157. if (intr_id == type_el3_interrupt_table[i].id) {
  158. handler = type_el3_interrupt_table[i].handler;
  159. }
  160. }
  161. if (handler != NULL) {
  162. return handler(intr_id, flags, handle, cookie);
  163. }
  164. return 0;
  165. }
  166. void bl31_platform_setup(void)
  167. {
  168. prepare_dtb();
  169. /* Initialize the gic cpu and distributor interfaces */
  170. plat_versal_gic_driver_init();
  171. plat_versal_gic_init();
  172. }
  173. void bl31_plat_runtime_setup(void)
  174. {
  175. uint64_t flags = 0;
  176. int32_t rc;
  177. set_interrupt_rm_flag(flags, NON_SECURE);
  178. rc = register_interrupt_type_handler(INTR_TYPE_EL3,
  179. rdo_el3_interrupt_handler, flags);
  180. if (rc != 0) {
  181. panic();
  182. }
  183. }
  184. /*
  185. * Perform the very early platform specific architectural setup here.
  186. */
  187. void bl31_plat_arch_setup(void)
  188. {
  189. plat_arm_interconnect_init();
  190. plat_arm_interconnect_enter_coherency();
  191. const mmap_region_t bl_regions[] = {
  192. #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE) && \
  193. (!defined(PLAT_XLAT_TABLES_DYNAMIC)))
  194. MAP_REGION_FLAT(XILINX_OF_BOARD_DTB_ADDR, XILINX_OF_BOARD_DTB_MAX_SIZE,
  195. MT_MEMORY | MT_RW | MT_NS),
  196. #endif
  197. MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
  198. MT_MEMORY | MT_RW | MT_SECURE),
  199. MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
  200. MT_CODE | MT_SECURE),
  201. MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
  202. MT_RO_DATA | MT_SECURE),
  203. MAP_REGION_FLAT(BL_COHERENT_RAM_BASE,
  204. BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE,
  205. MT_DEVICE | MT_RW | MT_SECURE),
  206. {0}
  207. };
  208. setup_page_tables(bl_regions, plat_get_mmap());
  209. enable_mmu(0);
  210. }