arm_common.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2015-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/debug.h>
  11. #include <common/romlib.h>
  12. #include <common/par.h>
  13. #include <lib/extensions/sysreg128.h>
  14. #include <lib/mmio.h>
  15. #include <lib/smccc.h>
  16. #include <lib/xlat_tables/xlat_tables_compat.h>
  17. #include <services/arm_arch_svc.h>
  18. #include <plat/arm/common/plat_arm.h>
  19. #include <plat/common/platform.h>
  20. /* Weak definitions may be overridden in specific ARM standard platform */
  21. #pragma weak plat_get_ns_image_entrypoint
  22. #pragma weak plat_arm_get_mmap
  23. /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid
  24. * conflicts with the definition in plat/common. */
  25. #pragma weak plat_get_syscnt_freq2
  26. /* Get ARM SOC-ID */
  27. #pragma weak plat_arm_get_soc_id
  28. /*******************************************************************************
  29. * Changes the memory attributes for the region of mapped memory where the BL
  30. * image's translation tables are located such that the tables will have
  31. * read-only permissions.
  32. ******************************************************************************/
  33. #if PLAT_RO_XLAT_TABLES
  34. void arm_xlat_make_tables_readonly(void)
  35. {
  36. int rc = xlat_make_tables_readonly();
  37. if (rc != 0) {
  38. ERROR("Failed to make translation tables read-only at EL%u.\n",
  39. get_current_el());
  40. panic();
  41. }
  42. INFO("Translation tables are now read-only at EL%u.\n",
  43. get_current_el());
  44. }
  45. #endif
  46. void arm_setup_romlib(void)
  47. {
  48. #if USE_ROMLIB
  49. if (!rom_lib_init(ROMLIB_VERSION))
  50. panic();
  51. #endif
  52. }
  53. uintptr_t plat_get_ns_image_entrypoint(void)
  54. {
  55. #ifdef PRELOADED_BL33_BASE
  56. return PRELOADED_BL33_BASE;
  57. #else
  58. return PLAT_ARM_NS_IMAGE_BASE;
  59. #endif
  60. }
  61. /*******************************************************************************
  62. * Gets SPSR for BL32 entry
  63. ******************************************************************************/
  64. uint32_t arm_get_spsr_for_bl32_entry(void)
  65. {
  66. /*
  67. * The Secure Payload Dispatcher service is responsible for
  68. * setting the SPSR prior to entry into the BL32 image.
  69. */
  70. return 0;
  71. }
  72. /*******************************************************************************
  73. * Gets SPSR for BL33 entry
  74. ******************************************************************************/
  75. #ifdef __aarch64__
  76. uint32_t arm_get_spsr_for_bl33_entry(void)
  77. {
  78. unsigned int mode;
  79. uint32_t spsr;
  80. /* Figure out what mode we enter the non-secure world in */
  81. mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
  82. /*
  83. * TODO: Consider the possibility of specifying the SPSR in
  84. * the FIP ToC and allowing the platform to have a say as
  85. * well.
  86. */
  87. spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  88. return spsr;
  89. }
  90. #else
  91. /*******************************************************************************
  92. * Gets SPSR for BL33 entry
  93. ******************************************************************************/
  94. uint32_t arm_get_spsr_for_bl33_entry(void)
  95. {
  96. unsigned int hyp_status, mode, spsr;
  97. hyp_status = GET_VIRT_EXT(read_id_pfr1());
  98. mode = (hyp_status) ? MODE32_hyp : MODE32_svc;
  99. /*
  100. * TODO: Consider the possibility of specifying the SPSR in
  101. * the FIP ToC and allowing the platform to have a say as
  102. * well.
  103. */
  104. spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1,
  105. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  106. return spsr;
  107. }
  108. #endif /* __aarch64__ */
  109. /*******************************************************************************
  110. * Configures access to the system counter timer module.
  111. ******************************************************************************/
  112. #ifdef ARM_SYS_TIMCTL_BASE
  113. void arm_configure_sys_timer(void)
  114. {
  115. unsigned int reg_val;
  116. /* Read the frequency of the system counter */
  117. unsigned int freq_val = plat_get_syscnt_freq2();
  118. #if ARM_CONFIG_CNTACR
  119. reg_val = (1U << CNTACR_RPCT_SHIFT) | (1U << CNTACR_RVCT_SHIFT);
  120. reg_val |= (1U << CNTACR_RFRQ_SHIFT) | (1U << CNTACR_RVOFF_SHIFT);
  121. reg_val |= (1U << CNTACR_RWVT_SHIFT) | (1U << CNTACR_RWPT_SHIFT);
  122. mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val);
  123. #endif /* ARM_CONFIG_CNTACR */
  124. reg_val = (1U << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID));
  125. mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val);
  126. /*
  127. * Initialize CNTFRQ register in CNTCTLBase frame. The CNTFRQ
  128. * system register initialized during psci_arch_setup() is different
  129. * from this and has to be updated independently.
  130. */
  131. mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTCTLBASE_CNTFRQ, freq_val);
  132. #if defined(PLAT_juno) || defined(PLAT_n1sdp) || defined(PLAT_morello)
  133. /*
  134. * Initialize CNTFRQ register in Non-secure CNTBase frame.
  135. * This is required for Juno, N1SDP and Morello because they do not
  136. * follow ARM ARM in that the value updated in CNTFRQ is not
  137. * reflected in CNTBASEN_CNTFRQ. Hence update the value manually.
  138. */
  139. mmio_write_32(ARM_SYS_CNT_BASE_NS + CNTBASEN_CNTFRQ, freq_val);
  140. #endif
  141. }
  142. #endif /* ARM_SYS_TIMCTL_BASE */
  143. /*******************************************************************************
  144. * Returns ARM platform specific memory map regions.
  145. ******************************************************************************/
  146. const mmap_region_t *plat_arm_get_mmap(void)
  147. {
  148. return plat_arm_mmap;
  149. }
  150. #ifdef ARM_SYS_CNTCTL_BASE
  151. unsigned int plat_get_syscnt_freq2(void)
  152. {
  153. unsigned int counter_base_frequency;
  154. /* Read the frequency from Frequency modes table */
  155. counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF);
  156. /* The first entry of the frequency modes table must not be 0 */
  157. if (counter_base_frequency == 0U)
  158. panic();
  159. return counter_base_frequency;
  160. }
  161. #endif /* ARM_SYS_CNTCTL_BASE */
  162. #if SDEI_SUPPORT
  163. /*
  164. * Translate SDEI entry point to PA, and perform standard ARM entry point
  165. * validation on it.
  166. */
  167. int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode)
  168. {
  169. uint64_t pa;
  170. sysreg_t par;
  171. u_register_t scr_el3;
  172. /* Doing Non-secure address translation requires SCR_EL3.NS set */
  173. scr_el3 = read_scr_el3();
  174. write_scr_el3(scr_el3 | SCR_NS_BIT);
  175. isb();
  176. assert((client_mode == MODE_EL2) || (client_mode == MODE_EL1));
  177. if (client_mode == MODE_EL2) {
  178. /*
  179. * Translate entry point to Physical Address using the EL2
  180. * translation regime.
  181. */
  182. ats1e2r(ep);
  183. } else {
  184. /*
  185. * Translate entry point to Physical Address using the EL1&0
  186. * translation regime, including stage 2.
  187. */
  188. AT(ats12e1r, ep);
  189. }
  190. isb();
  191. par = read_par_el1();
  192. /* Restore original SCRL_EL3 */
  193. write_scr_el3(scr_el3);
  194. isb();
  195. /* If the translation resulted in fault, return failure */
  196. if ((par & PAR_F_MASK) != 0)
  197. return -1;
  198. /* Extract Physical Address from PAR */
  199. pa = get_par_el1_pa(par);
  200. /* Perform NS entry point validation on the physical address */
  201. return arm_validate_ns_entrypoint(pa);
  202. }
  203. #endif
  204. const mmap_region_t *plat_get_addr_mmap(void)
  205. {
  206. return plat_arm_mmap;
  207. }
  208. #if ENABLE_RME
  209. void arm_gpt_setup(void)
  210. {
  211. /*
  212. * It is to be noted that any Arm platform that reuses arm_gpt_setup
  213. * must implement plat_arm_get_gpt_info within its platform code
  214. */
  215. const arm_gpt_info_t *arm_gpt_info =
  216. plat_arm_get_gpt_info();
  217. if (arm_gpt_info == NULL) {
  218. ERROR("arm_gpt_info not initialized!!\n");
  219. panic();
  220. }
  221. /* Initialize entire protected space to GPT_GPI_ANY. */
  222. if (gpt_init_l0_tables(arm_gpt_info->pps, arm_gpt_info->l0_base,
  223. arm_gpt_info->l0_size) < 0) {
  224. ERROR("gpt_init_l0_tables() failed!\n");
  225. panic();
  226. }
  227. /* Carve out defined PAS ranges. */
  228. if (gpt_init_pas_l1_tables(arm_gpt_info->pgs,
  229. arm_gpt_info->l1_base,
  230. arm_gpt_info->l1_size,
  231. arm_gpt_info->pas_region_base,
  232. arm_gpt_info->pas_region_count) < 0) {
  233. ERROR("gpt_init_pas_l1_tables() failed!\n");
  234. panic();
  235. }
  236. INFO("Enabling Granule Protection Checks\n");
  237. if (gpt_enable() < 0) {
  238. ERROR("gpt_enable() failed!\n");
  239. panic();
  240. }
  241. }
  242. #endif /* ENABLE_RME */