sq_bl31_setup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2018-2024, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <platform_def.h>
  8. #include <arch.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <drivers/arm/pl011.h>
  13. #include <lib/mmio.h>
  14. #include <sq_common.h>
  15. static console_t console;
  16. static entry_point_info_t bl32_image_ep_info;
  17. static entry_point_info_t bl33_image_ep_info;
  18. IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_START__, SPM_SHIM_EXCEPTIONS_START);
  19. IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_END__, SPM_SHIM_EXCEPTIONS_END);
  20. IMPORT_SYM(uintptr_t, __SPM_SHIM_EXCEPTIONS_LMA__, SPM_SHIM_EXCEPTIONS_LMA);
  21. unsigned int plat_get_syscnt_freq2(void)
  22. {
  23. unsigned int counter_base_frequency;
  24. /* Read the frequency from Frequency modes table */
  25. counter_base_frequency = mmio_read_32(SQ_SYS_CNTCTL_BASE + CNTFID_OFF);
  26. /* The first entry of the frequency modes table must not be 0 */
  27. if (counter_base_frequency == 0)
  28. panic();
  29. return counter_base_frequency;
  30. }
  31. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  32. {
  33. assert(sec_state_is_valid(type));
  34. return type == NON_SECURE ? &bl33_image_ep_info : &bl32_image_ep_info;
  35. }
  36. #if !RESET_TO_BL31
  37. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  38. u_register_t arg2, u_register_t arg3)
  39. {
  40. void *from_bl2 = (void *) arg0;
  41. bl_params_node_t *bl_params = ((bl_params_t *) from_bl2)->head;
  42. /* Initialize the console to provide early debug support */
  43. (void)console_pl011_register(PLAT_SQ_BOOT_UART_BASE,
  44. PLAT_SQ_BOOT_UART_CLK_IN_HZ,
  45. SQ_CONSOLE_BAUDRATE, &console);
  46. console_set_scope(&console, CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME);
  47. /* Initialize power controller before setting up topology */
  48. plat_sq_pwrc_setup();
  49. while (bl_params) {
  50. if (bl_params->image_id == BL32_IMAGE_ID)
  51. bl32_image_ep_info = *bl_params->ep_info;
  52. if (bl_params->image_id == BL33_IMAGE_ID)
  53. bl33_image_ep_info = *bl_params->ep_info;
  54. bl_params = bl_params->next_params_info;
  55. }
  56. }
  57. #else
  58. /*******************************************************************************
  59. * Gets SPSR for BL32 entry
  60. ******************************************************************************/
  61. uint32_t sq_get_spsr_for_bl32_entry(void)
  62. {
  63. /*
  64. * The Secure Payload Dispatcher service is responsible for
  65. * setting the SPSR prior to entry into the BL32 image.
  66. */
  67. return 0;
  68. }
  69. /*******************************************************************************
  70. * Gets SPSR for BL33 entry
  71. ******************************************************************************/
  72. uint32_t sq_get_spsr_for_bl33_entry(void)
  73. {
  74. unsigned long el_status;
  75. unsigned int mode;
  76. uint32_t spsr;
  77. /* Figure out what mode we enter the non-secure world in */
  78. el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
  79. el_status &= ID_AA64PFR0_ELX_MASK;
  80. mode = (el_status) ? MODE_EL2 : MODE_EL1;
  81. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  82. return spsr;
  83. }
  84. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  85. u_register_t arg2, u_register_t arg3)
  86. {
  87. /* Initialize the console to provide early debug support */
  88. (void)console_pl011_register(PLAT_SQ_BOOT_UART_BASE,
  89. PLAT_SQ_BOOT_UART_CLK_IN_HZ,
  90. SQ_CONSOLE_BAUDRATE, &console);
  91. console_set_scope(&console, CONSOLE_FLAG_BOOT | CONSOLE_FLAG_RUNTIME);
  92. /* There are no parameters from BL2 if BL31 is a reset vector */
  93. assert(arg0 == 0U);
  94. assert(arg1 == 0U);
  95. /* Initialize power controller before setting up topology */
  96. plat_sq_pwrc_setup();
  97. #ifdef SPD_opteed
  98. struct draminfo di = {0};
  99. sq_scp_get_draminfo(&di);
  100. /*
  101. * Check if OP-TEE has been loaded in Secure RAM allocated
  102. * from DRAM1 region
  103. */
  104. if ((di.base1 + di.size1) <= BL32_BASE) {
  105. NOTICE("OP-TEE has been loaded by SCP firmware\n");
  106. /* Populate entry point information for BL32 */
  107. SET_PARAM_HEAD(&bl32_image_ep_info,
  108. PARAM_EP,
  109. VERSION_1,
  110. 0);
  111. SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
  112. bl32_image_ep_info.pc = BL32_BASE;
  113. bl32_image_ep_info.spsr = sq_get_spsr_for_bl32_entry();
  114. } else {
  115. NOTICE("OP-TEE has not been loaded by SCP firmware\n");
  116. }
  117. #endif /* SPD_opteed */
  118. /* Populate entry point information for BL33 */
  119. SET_PARAM_HEAD(&bl33_image_ep_info,
  120. PARAM_EP,
  121. VERSION_1,
  122. 0);
  123. /*
  124. * Tell BL31 where the non-trusted software image
  125. * is located and the entry state information
  126. */
  127. bl33_image_ep_info.pc = PRELOADED_BL33_BASE;
  128. bl33_image_ep_info.spsr = sq_get_spsr_for_bl33_entry();
  129. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  130. }
  131. #endif
  132. static void sq_configure_sys_timer(void)
  133. {
  134. unsigned int reg_val;
  135. unsigned int freq_val = plat_get_syscnt_freq2();
  136. reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT);
  137. reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT);
  138. reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT);
  139. mmio_write_32(SQ_SYS_TIMCTL_BASE +
  140. CNTACR_BASE(PLAT_SQ_NSTIMER_FRAME_ID), reg_val);
  141. reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_SQ_NSTIMER_FRAME_ID));
  142. mmio_write_32(SQ_SYS_TIMCTL_BASE + CNTNSAR, reg_val);
  143. /* Initialize CNTFRQ register in CNTCTLBase frame */
  144. mmio_write_32(SQ_SYS_TIMCTL_BASE + CNTCTLBASE_CNTFRQ, freq_val);
  145. /*
  146. * Initialize CNTFRQ register in Non-secure CNTBase frame.
  147. * This is required for SynQuacer, because it does not
  148. * follow ARM ARM in that the value updated in CNTFRQ is not
  149. * reflected in CNTBASEN_CNTFRQ. Hence update the value manually.
  150. */
  151. mmio_write_32(SQ_SYS_CNT_BASE_NS + CNTBASEN_CNTFRQ, freq_val);
  152. }
  153. void bl31_platform_setup(void)
  154. {
  155. /* Initialize the CCN interconnect */
  156. plat_sq_interconnect_init();
  157. plat_sq_interconnect_enter_coherency();
  158. /* Initialize the GIC driver, cpu and distributor interfaces */
  159. sq_gic_driver_init();
  160. sq_gic_init();
  161. /* Enable and initialize the System level generic timer */
  162. mmio_write_32(SQ_SYS_CNTCTL_BASE + CNTCR_OFF,
  163. CNTCR_FCREQ(0U) | CNTCR_EN);
  164. /* Allow access to the System counter timer module */
  165. sq_configure_sys_timer();
  166. }
  167. void bl31_plat_runtime_setup(void)
  168. {
  169. struct draminfo *di = (struct draminfo *)(unsigned long)DRAMINFO_BASE;
  170. sq_scp_get_draminfo(di);
  171. }
  172. void bl31_plat_arch_setup(void)
  173. {
  174. static const mmap_region_t secure_partition_mmap[] = {
  175. #if SPM_MM
  176. MAP_REGION_FLAT(PLAT_SPM_BUF_BASE,
  177. PLAT_SPM_BUF_SIZE,
  178. MT_RW_DATA | MT_SECURE),
  179. MAP_REGION_FLAT(PLAT_SQ_SP_PRIV_BASE,
  180. PLAT_SQ_SP_PRIV_SIZE,
  181. MT_RW_DATA | MT_SECURE),
  182. #endif
  183. #if !RESET_TO_BL31
  184. MAP_REGION_FLAT(BL2_MAILBOX_BASE,
  185. BL2_MAILBOX_SIZE,
  186. MT_RW | MT_SECURE),
  187. #endif
  188. {0},
  189. };
  190. sq_mmap_setup(BL31_BASE, BL31_SIZE, secure_partition_mmap);
  191. enable_mmu_el3(XLAT_TABLE_NC);
  192. #if SPM_MM
  193. memcpy((void *)SPM_SHIM_EXCEPTIONS_START,
  194. (void *)SPM_SHIM_EXCEPTIONS_LMA,
  195. (uintptr_t)SPM_SHIM_EXCEPTIONS_END -
  196. (uintptr_t)SPM_SHIM_EXCEPTIONS_START);
  197. #endif
  198. }
  199. void bl31_plat_enable_mmu(uint32_t flags)
  200. {
  201. enable_mmu_el3(flags | XLAT_TABLE_NC);
  202. }