mmu.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * (C) Copyright 2013
  3. * David Feng <fenghua@phytium.com.cn>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _ASM_ARMV8_MMU_H_
  8. #define _ASM_ARMV8_MMU_H_
  9. #ifdef __ASSEMBLY__
  10. #define _AC(X, Y) X
  11. #else
  12. #define _AC(X, Y) (X##Y)
  13. #endif
  14. #define UL(x) _AC(x, UL)
  15. /***************************************************************/
  16. /*
  17. * The following definitions are related each other, shoud be
  18. * calculated specifically.
  19. */
  20. #ifndef CONFIG_SYS_FULL_VA
  21. #define VA_BITS (42) /* 42 bits virtual address */
  22. #else
  23. #define VA_BITS CONFIG_SYS_VA_BITS
  24. #define PTL2_BITS CONFIG_SYS_PTL2_BITS
  25. #endif
  26. /* PAGE_SHIFT determines the page size */
  27. #undef PAGE_SIZE
  28. #define PAGE_SHIFT 16
  29. #define PAGE_SIZE (1 << PAGE_SHIFT)
  30. #define PAGE_MASK (~(PAGE_SIZE-1))
  31. /*
  32. * block/section address mask and size definitions.
  33. */
  34. #ifndef CONFIG_SYS_FULL_VA
  35. #define SECTION_SHIFT 29
  36. #define SECTION_SIZE (UL(1) << SECTION_SHIFT)
  37. #define SECTION_MASK (~(SECTION_SIZE-1))
  38. #else
  39. #define BLOCK_SHIFT CONFIG_SYS_BLOCK_SHIFT
  40. #define BLOCK_SIZE (UL(1) << BLOCK_SHIFT)
  41. #define BLOCK_MASK (~(BLOCK_SIZE-1))
  42. #endif
  43. /***************************************************************/
  44. /*
  45. * Memory types
  46. */
  47. #define MT_DEVICE_NGNRNE 0
  48. #define MT_DEVICE_NGNRE 1
  49. #define MT_DEVICE_GRE 2
  50. #define MT_NORMAL_NC 3
  51. #define MT_NORMAL 4
  52. #define MEMORY_ATTRIBUTES ((0x00 << (MT_DEVICE_NGNRNE * 8)) | \
  53. (0x04 << (MT_DEVICE_NGNRE * 8)) | \
  54. (0x0c << (MT_DEVICE_GRE * 8)) | \
  55. (0x44 << (MT_NORMAL_NC * 8)) | \
  56. (UL(0xff) << (MT_NORMAL * 8)))
  57. /*
  58. * Hardware page table definitions.
  59. *
  60. */
  61. #ifdef CONFIG_SYS_FULL_VA
  62. /*
  63. * Level 1 descriptor (PGD).
  64. */
  65. #define PTL1_TYPE_MASK (3 << 0)
  66. #define PTL1_TYPE_TABLE (3 << 0)
  67. #define PTL1_TABLE_PXN (1UL << 59)
  68. #define PTL1_TABLE_XN (1UL << 60)
  69. #define PTL1_TABLE_AP (1UL << 61)
  70. #define PTL1_TABLE_NS (1UL << 63)
  71. /*
  72. * Level 2 descriptor (PMD).
  73. */
  74. #define PTL2_TYPE_MASK (3 << 0)
  75. #define PTL2_TYPE_FAULT (0 << 0)
  76. #define PTL2_TYPE_TABLE (3 << 0)
  77. #define PTL2_TYPE_BLOCK (1 << 0)
  78. /*
  79. * Block
  80. */
  81. #define PTL2_MEMTYPE(x) ((x) << 2)
  82. #define PTL2_BLOCK_NON_SHARE (0 << 8)
  83. #define PTL2_BLOCK_OUTER_SHARE (2 << 8)
  84. #define PTL2_BLOCK_INNER_SHARE (3 << 8)
  85. #define PTL2_BLOCK_AF (1 << 10)
  86. #define PTL2_BLOCK_NG (1 << 11)
  87. #define PTL2_BLOCK_PXN (UL(1) << 53)
  88. #define PTL2_BLOCK_UXN (UL(1) << 54)
  89. #else
  90. /*
  91. * Level 2 descriptor (PMD).
  92. */
  93. #define PMD_TYPE_MASK (3 << 0)
  94. #define PMD_TYPE_FAULT (0 << 0)
  95. #define PMD_TYPE_TABLE (3 << 0)
  96. #define PMD_TYPE_SECT (1 << 0)
  97. /*
  98. * Section
  99. */
  100. #define PMD_SECT_NS (1 << 5)
  101. #define PMD_SECT_NON_SHARE (0 << 8)
  102. #define PMD_SECT_OUTER_SHARE (2 << 8)
  103. #define PMD_SECT_INNER_SHARE (3 << 8)
  104. #define PMD_SECT_AF (1 << 10)
  105. #define PMD_SECT_NG (1 << 11)
  106. #define PMD_SECT_PXN (UL(1) << 53)
  107. #define PMD_SECT_UXN (UL(1) << 54)
  108. #endif
  109. /*
  110. * AttrIndx[2:0]
  111. */
  112. #define PMD_ATTRINDX(t) ((t) << 2)
  113. #define PMD_ATTRINDX_MASK (7 << 2)
  114. /*
  115. * TCR flags.
  116. */
  117. #define TCR_T0SZ(x) ((64 - (x)) << 0)
  118. #define TCR_IRGN_NC (0 << 8)
  119. #define TCR_IRGN_WBWA (1 << 8)
  120. #define TCR_IRGN_WT (2 << 8)
  121. #define TCR_IRGN_WBNWA (3 << 8)
  122. #define TCR_IRGN_MASK (3 << 8)
  123. #define TCR_ORGN_NC (0 << 10)
  124. #define TCR_ORGN_WBWA (1 << 10)
  125. #define TCR_ORGN_WT (2 << 10)
  126. #define TCR_ORGN_WBNWA (3 << 10)
  127. #define TCR_ORGN_MASK (3 << 10)
  128. #define TCR_SHARED_NON (0 << 12)
  129. #define TCR_SHARED_OUTER (2 << 12)
  130. #define TCR_SHARED_INNER (3 << 12)
  131. #define TCR_TG0_4K (0 << 14)
  132. #define TCR_TG0_64K (1 << 14)
  133. #define TCR_TG0_16K (2 << 14)
  134. #ifndef CONFIG_SYS_FULL_VA
  135. #define TCR_EL1_IPS_BITS (UL(3) << 32) /* 42 bits physical address */
  136. #define TCR_EL2_IPS_BITS (3 << 16) /* 42 bits physical address */
  137. #define TCR_EL3_IPS_BITS (3 << 16) /* 42 bits physical address */
  138. #else
  139. #define TCR_EL1_IPS_BITS CONFIG_SYS_TCR_EL1_IPS_BITS
  140. #define TCR_EL2_IPS_BITS CONFIG_SYS_TCR_EL2_IPS_BITS
  141. #define TCR_EL3_IPS_BITS CONFIG_SYS_TCR_EL3_IPS_BITS
  142. #endif
  143. /* PTWs cacheable, inner/outer WBWA and inner shareable */
  144. #define TCR_FLAGS (TCR_TG0_64K | \
  145. TCR_SHARED_INNER | \
  146. TCR_ORGN_WBWA | \
  147. TCR_IRGN_WBWA | \
  148. TCR_T0SZ(VA_BITS))
  149. #define TCR_EL1_RSVD (1 << 31)
  150. #define TCR_EL2_RSVD (1 << 31 | 1 << 23)
  151. #define TCR_EL3_RSVD (1 << 31 | 1 << 23)
  152. #ifndef __ASSEMBLY__
  153. #ifndef CONFIG_SYS_FULL_VA
  154. void set_pgtable_section(u64 *page_table, u64 index,
  155. u64 section, u64 memory_type,
  156. u64 attribute);
  157. void set_pgtable_table(u64 *page_table, u64 index,
  158. u64 *table_addr);
  159. #endif
  160. static inline void set_ttbr_tcr_mair(int el, u64 table, u64 tcr, u64 attr)
  161. {
  162. asm volatile("dsb sy");
  163. if (el == 1) {
  164. asm volatile("msr ttbr0_el1, %0" : : "r" (table) : "memory");
  165. asm volatile("msr tcr_el1, %0" : : "r" (tcr) : "memory");
  166. asm volatile("msr mair_el1, %0" : : "r" (attr) : "memory");
  167. } else if (el == 2) {
  168. asm volatile("msr ttbr0_el2, %0" : : "r" (table) : "memory");
  169. asm volatile("msr tcr_el2, %0" : : "r" (tcr) : "memory");
  170. asm volatile("msr mair_el2, %0" : : "r" (attr) : "memory");
  171. } else if (el == 3) {
  172. asm volatile("msr ttbr0_el3, %0" : : "r" (table) : "memory");
  173. asm volatile("msr tcr_el3, %0" : : "r" (tcr) : "memory");
  174. asm volatile("msr mair_el3, %0" : : "r" (attr) : "memory");
  175. } else {
  176. hang();
  177. }
  178. asm volatile("isb");
  179. }
  180. struct mm_region {
  181. u64 base;
  182. u64 size;
  183. u64 attrs;
  184. };
  185. #endif
  186. #endif /* _ASM_ARMV8_MMU_H_ */