xlat_tables_arch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <platform_def.h>
  9. #include <arch.h>
  10. #include <arch_features.h>
  11. #include <arch_helpers.h>
  12. #include <lib/cassert.h>
  13. #include <lib/utils_def.h>
  14. #include <lib/xlat_tables/xlat_tables_v2.h>
  15. #include "../xlat_tables_private.h"
  16. #if (ARM_ARCH_MAJOR == 7) && !defined(ARMV7_SUPPORTS_LARGE_PAGE_ADDRESSING)
  17. #error ARMv7 target does not support LPAE MMU descriptors
  18. #endif
  19. /*
  20. * Returns true if the provided granule size is supported, false otherwise.
  21. */
  22. bool xlat_arch_is_granule_size_supported(size_t size)
  23. {
  24. /*
  25. * The library uses the long descriptor translation table format, which
  26. * supports 4 KiB pages only.
  27. */
  28. return size == PAGE_SIZE_4KB;
  29. }
  30. size_t xlat_arch_get_max_supported_granule_size(void)
  31. {
  32. return PAGE_SIZE_4KB;
  33. }
  34. /*
  35. * Determine the physical address space encoded in the 'attr' parameter.
  36. *
  37. * The physical address will fall into one of two spaces; secure or
  38. * nonsecure.
  39. */
  40. uint32_t xlat_arch_get_pas(uint32_t attr)
  41. {
  42. uint32_t pas = MT_PAS(attr);
  43. if (pas == MT_NS) {
  44. return LOWER_ATTRS(NS);
  45. } else { /* MT_SECURE */
  46. return 0U;
  47. }
  48. }
  49. #if ENABLE_ASSERTIONS
  50. unsigned long long xlat_arch_get_max_supported_pa(void)
  51. {
  52. /* Physical address space size for long descriptor format. */
  53. return (1ULL << 40) - 1ULL;
  54. }
  55. /*
  56. * Return minimum virtual address space size supported by the architecture
  57. */
  58. uintptr_t xlat_get_min_virt_addr_space_size(void)
  59. {
  60. return MIN_VIRT_ADDR_SPACE_SIZE;
  61. }
  62. #endif /* ENABLE_ASSERTIONS*/
  63. bool is_mmu_enabled_ctx(const xlat_ctx_t *ctx)
  64. {
  65. if (ctx->xlat_regime == EL1_EL0_REGIME) {
  66. assert(xlat_arch_current_el() == 1U);
  67. return (read_sctlr() & SCTLR_M_BIT) != 0U;
  68. } else {
  69. assert(ctx->xlat_regime == EL2_REGIME);
  70. assert(xlat_arch_current_el() == 2U);
  71. return (read_hsctlr() & HSCTLR_M_BIT) != 0U;
  72. }
  73. }
  74. bool is_dcache_enabled(void)
  75. {
  76. if (IS_IN_EL2()) {
  77. return (read_hsctlr() & HSCTLR_C_BIT) != 0U;
  78. } else {
  79. return (read_sctlr() & SCTLR_C_BIT) != 0U;
  80. }
  81. }
  82. uint64_t xlat_arch_regime_get_xn_desc(int xlat_regime)
  83. {
  84. if (xlat_regime == EL1_EL0_REGIME) {
  85. return UPPER_ATTRS(XN) | UPPER_ATTRS(PXN);
  86. } else {
  87. assert(xlat_regime == EL2_REGIME);
  88. return UPPER_ATTRS(XN);
  89. }
  90. }
  91. void xlat_arch_tlbi_va(uintptr_t va, int xlat_regime)
  92. {
  93. /*
  94. * Ensure the translation table write has drained into memory before
  95. * invalidating the TLB entry.
  96. */
  97. dsbishst();
  98. if (xlat_regime == EL1_EL0_REGIME) {
  99. tlbimvaais(TLBI_ADDR(va));
  100. } else {
  101. assert(xlat_regime == EL2_REGIME);
  102. tlbimvahis(TLBI_ADDR(va));
  103. }
  104. }
  105. void xlat_arch_tlbi_va_sync(void)
  106. {
  107. /* Invalidate all entries from branch predictors. */
  108. bpiallis();
  109. /*
  110. * A TLB maintenance instruction can complete at any time after
  111. * it is issued, but is only guaranteed to be complete after the
  112. * execution of DSB by the PE that executed the TLB maintenance
  113. * instruction. After the TLB invalidate instruction is
  114. * complete, no new memory accesses using the invalidated TLB
  115. * entries will be observed by any observer of the system
  116. * domain. See section D4.8.2 of the ARMv8 (issue k), paragraph
  117. * "Ordering and completion of TLB maintenance instructions".
  118. */
  119. dsbish();
  120. /*
  121. * The effects of a completed TLB maintenance instruction are
  122. * only guaranteed to be visible on the PE that executed the
  123. * instruction after the execution of an ISB instruction by the
  124. * PE that executed the TLB maintenance instruction.
  125. */
  126. isb();
  127. }
  128. unsigned int xlat_arch_current_el(void)
  129. {
  130. if (IS_IN_HYP()) {
  131. return 2U;
  132. } else {
  133. assert(IS_IN_SVC() || IS_IN_MON());
  134. /*
  135. * If EL3 is in AArch32 mode, all secure PL1 modes (Monitor,
  136. * System, SVC, Abort, UND, IRQ and FIQ modes) execute at EL3.
  137. *
  138. * The PL1&0 translation regime in AArch32 behaves like the
  139. * EL1&0 regime in AArch64 except for the XN bits, but we set
  140. * and unset them at the same time, so there's no difference in
  141. * practice.
  142. */
  143. return 1U;
  144. }
  145. }
  146. /*******************************************************************************
  147. * Function for enabling the MMU in PL1 or PL2, assuming that the page tables
  148. * have already been created.
  149. ******************************************************************************/
  150. void setup_mmu_cfg(uint64_t *params, unsigned int flags,
  151. const uint64_t *base_table, unsigned long long max_pa,
  152. uintptr_t max_va, __unused int xlat_regime)
  153. {
  154. uint64_t mair, ttbr0;
  155. uint32_t ttbcr;
  156. /* Set attributes in the right indices of the MAIR */
  157. mair = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);
  158. mair |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,
  159. ATTR_IWBWA_OWBWA_NTR_INDEX);
  160. mair |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE,
  161. ATTR_NON_CACHEABLE_INDEX);
  162. /*
  163. * Configure the control register for stage 1 of the PL1&0 or EL2
  164. * translation regimes.
  165. */
  166. /* Use the Long-descriptor translation table format. */
  167. ttbcr = TTBCR_EAE_BIT;
  168. if (xlat_regime == EL1_EL0_REGIME) {
  169. assert(IS_IN_SVC() || IS_IN_MON());
  170. /*
  171. * Disable translation table walk for addresses that are
  172. * translated using TTBR1. Therefore, only TTBR0 is used.
  173. */
  174. ttbcr |= TTBCR_EPD1_BIT;
  175. } else {
  176. assert(xlat_regime == EL2_REGIME);
  177. assert(IS_IN_HYP());
  178. /*
  179. * Set HTCR bits as well. Set HTTBR table properties
  180. * as Inner & outer WBWA & shareable.
  181. */
  182. ttbcr |= HTCR_RES1 |
  183. HTCR_SH0_INNER_SHAREABLE | HTCR_RGN0_OUTER_WBA |
  184. HTCR_RGN0_INNER_WBA;
  185. }
  186. /*
  187. * Limit the input address ranges and memory region sizes translated
  188. * using TTBR0 to the given virtual address space size, if smaller than
  189. * 32 bits.
  190. */
  191. if (max_va != UINT32_MAX) {
  192. uintptr_t virtual_addr_space_size = max_va + 1U;
  193. assert(virtual_addr_space_size >=
  194. xlat_get_min_virt_addr_space_size());
  195. assert(IS_POWER_OF_TWO(virtual_addr_space_size));
  196. /*
  197. * __builtin_ctzll(0) is undefined but here we are guaranteed
  198. * that virtual_addr_space_size is in the range [1, UINT32_MAX].
  199. */
  200. int t0sz = 32 - __builtin_ctzll(virtual_addr_space_size);
  201. ttbcr |= (uint32_t) t0sz;
  202. }
  203. /*
  204. * Set the cacheability and shareability attributes for memory
  205. * associated with translation table walks using TTBR0.
  206. */
  207. if ((flags & XLAT_TABLE_NC) != 0U) {
  208. /* Inner & outer non-cacheable non-shareable. */
  209. ttbcr |= TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC |
  210. TTBCR_RGN0_INNER_NC;
  211. } else {
  212. /* Inner & outer WBWA & shareable. */
  213. ttbcr |= TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA |
  214. TTBCR_RGN0_INNER_WBA;
  215. }
  216. /* Set TTBR0 bits as well */
  217. ttbr0 = (uint64_t)(uintptr_t) base_table;
  218. if (is_feat_ttcnp_present()) {
  219. /* Enable CnP bit so as to share page tables with all PEs. */
  220. ttbr0 |= TTBR_CNP_BIT;
  221. }
  222. /* Now populate MMU configuration */
  223. params[MMU_CFG_MAIR] = mair;
  224. params[MMU_CFG_TCR] = (uint64_t) ttbcr;
  225. params[MMU_CFG_TTBR0] = ttbr0;
  226. }