bl31_setup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2018-2020, 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/arm/dcc.h>
  14. #include <drivers/arm/pl011.h>
  15. #include <drivers/console.h>
  16. #include <lib/cpus/cpu_ops.h>
  17. #include <lib/mmio.h>
  18. #include <lib/xlat_tables/xlat_tables_v2.h>
  19. #include <plat/common/platform.h>
  20. #include <plat_arm.h>
  21. #include <plat_console.h>
  22. #include <scmi.h>
  23. #include <def.h>
  24. #include <plat_fdt.h>
  25. #include <plat_private.h>
  26. #include <plat_startup.h>
  27. #include <plat_xfer_list.h>
  28. #include <pm_api_sys.h>
  29. #include <pm_client.h>
  30. static entry_point_info_t bl32_image_ep_info;
  31. static entry_point_info_t bl33_image_ep_info;
  32. /*
  33. * Return a pointer to the 'entry_point_info' structure of the next image for
  34. * the security state specified. BL33 corresponds to the non-secure image type
  35. * while BL32 corresponds to the secure image type. A NULL pointer is returned
  36. * if the image does not exist.
  37. */
  38. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  39. {
  40. assert(sec_state_is_valid(type));
  41. if (type == NON_SECURE) {
  42. return &bl33_image_ep_info;
  43. }
  44. return &bl32_image_ep_info;
  45. }
  46. /*
  47. * Set the build time defaults,if we can't find any config data.
  48. */
  49. static inline void bl31_set_default_config(void)
  50. {
  51. bl32_image_ep_info.pc = BL32_BASE;
  52. bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
  53. #if defined(SPD_opteed)
  54. /* NS dtb addr passed to optee_os */
  55. bl32_image_ep_info.args.arg3 = XILINX_OF_BOARD_DTB_ADDR;
  56. #endif
  57. bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
  58. bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
  59. DISABLE_ALL_EXCEPTIONS);
  60. }
  61. /*
  62. * Perform any BL31 specific platform actions. Here is an opportunity to copy
  63. * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
  64. * are lost (potentially). This needs to be done before the MMU is initialized
  65. * so that the memory layout can be used while creating page tables.
  66. */
  67. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  68. u_register_t arg2, u_register_t arg3)
  69. {
  70. (void)arg0;
  71. (void)arg1;
  72. (void)arg2;
  73. (void)arg3;
  74. uint32_t uart_clock;
  75. int32_t rc;
  76. board_detection();
  77. /* FIXME */
  78. switch (platform_id) {
  79. case SPP:
  80. switch (platform_version) {
  81. case SPP_PSXC_MMI_V2_0:
  82. cpu_clock = 770000;
  83. break;
  84. case SPP_PSXC_MMI_V3_0:
  85. cpu_clock = 908000;
  86. break;
  87. default:
  88. panic();
  89. }
  90. break;
  91. case SPP_MMD:
  92. switch (platform_version) {
  93. case SPP_PSXC_ISP_AIE_V2_0:
  94. case SPP_PSXC_MMD_AIE_FRZ_EA:
  95. case SPP_PSXC_MMD_AIE_V3_0:
  96. cpu_clock = 760000;
  97. break;
  98. default:
  99. panic();
  100. }
  101. break;
  102. case EMU:
  103. case EMU_MMD:
  104. cpu_clock = 112203;
  105. break;
  106. case QEMU:
  107. /* Random values now */
  108. cpu_clock = 3333333;
  109. break;
  110. case SILICON:
  111. cpu_clock = 100000000;
  112. break;
  113. default:
  114. panic();
  115. }
  116. uart_clock = get_uart_clk();
  117. setup_console();
  118. NOTICE("TF-A running on %s %d.%d\n", board_name_decode(),
  119. platform_version / 10U, platform_version % 10U);
  120. /* Initialize the platform config for future decision making */
  121. config_setup();
  122. /*
  123. * Do initial security configuration to allow DRAM/device access. On
  124. * Base only DRAM security is programmable (via TrustZone), but
  125. * other platforms might have more programmable security devices
  126. * present.
  127. */
  128. /* Populate common information for BL32 and BL33 */
  129. SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
  130. SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
  131. SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
  132. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  133. rc = transfer_list_populate_ep_info(&bl32_image_ep_info, &bl33_image_ep_info);
  134. if (rc == TL_OPS_NON || rc == TL_OPS_CUS) {
  135. NOTICE("BL31: TL not found, using default config\n");
  136. bl31_set_default_config();
  137. }
  138. long rev_var = cpu_get_rev_var();
  139. INFO("CPU Revision = 0x%lx\n", rev_var);
  140. INFO("cpu_clock = %dHz, uart_clock = %dHz\n", cpu_clock, uart_clock);
  141. NOTICE("BL31: Executing from 0x%x\n", BL31_BASE);
  142. NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
  143. NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
  144. }
  145. static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
  146. int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
  147. {
  148. static uint32_t index;
  149. uint32_t i;
  150. /* Validate 'handler' and 'id' parameters */
  151. if ((handler == NULL) || (index >= MAX_INTR_EL3)) {
  152. return -EINVAL;
  153. }
  154. /* Check if a handler has already been registered */
  155. for (i = 0; i < index; i++) {
  156. if (id == type_el3_interrupt_table[i].id) {
  157. return -EALREADY;
  158. }
  159. }
  160. type_el3_interrupt_table[index].id = id;
  161. type_el3_interrupt_table[index].handler = handler;
  162. index++;
  163. return 0;
  164. }
  165. static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
  166. void *handle, void *cookie)
  167. {
  168. (void)id;
  169. uint32_t intr_id;
  170. uint32_t i;
  171. interrupt_type_handler_t handler = NULL;
  172. intr_id = plat_ic_get_pending_interrupt_id();
  173. for (i = 0; i < MAX_INTR_EL3; i++) {
  174. if (intr_id == type_el3_interrupt_table[i].id) {
  175. handler = type_el3_interrupt_table[i].handler;
  176. }
  177. }
  178. if (handler != NULL) {
  179. (void)handler(intr_id, flags, handle, cookie);
  180. }
  181. return 0;
  182. }
  183. void bl31_platform_setup(void)
  184. {
  185. prepare_dtb();
  186. /* Initialize the gic cpu and distributor interfaces */
  187. plat_gic_driver_init();
  188. plat_gic_init();
  189. if (platform_id != EMU) {
  190. init_scmi_server();
  191. }
  192. }
  193. void bl31_plat_runtime_setup(void)
  194. {
  195. uint64_t flags = 0;
  196. int32_t rc;
  197. set_interrupt_rm_flag(flags, NON_SECURE);
  198. rc = register_interrupt_type_handler(INTR_TYPE_EL3,
  199. rdo_el3_interrupt_handler, flags);
  200. if (rc != 0) {
  201. panic();
  202. }
  203. console_switch_state(CONSOLE_FLAG_RUNTIME);
  204. }
  205. /*
  206. * Perform the very early platform specific architectural setup here.
  207. */
  208. void bl31_plat_arch_setup(void)
  209. {
  210. const mmap_region_t bl_regions[] = {
  211. MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
  212. MT_MEMORY | MT_RW | MT_SECURE),
  213. MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
  214. MT_CODE | MT_SECURE),
  215. MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
  216. MT_RO_DATA | MT_SECURE),
  217. MAP_REGION_FLAT(SMT_BUFFER_BASE, 0x1000,
  218. MT_DEVICE | MT_RW | MT_NON_CACHEABLE | MT_EXECUTE_NEVER | MT_NS),
  219. {0}
  220. };
  221. setup_page_tables(bl_regions, plat_get_mmap());
  222. enable_mmu(0);
  223. }