pgtable-hwdef.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_PGTABLE_HWDEF_H
  17. #define __ASM_PGTABLE_HWDEF_H
  18. /*
  19. * Number of page-table levels required to address 'va_bits' wide
  20. * address, without section mapping. We resolve the top (va_bits - PAGE_SHIFT)
  21. * bits with (PAGE_SHIFT - 3) bits at each page table level. Hence:
  22. *
  23. * levels = DIV_ROUND_UP((va_bits - PAGE_SHIFT), (PAGE_SHIFT - 3))
  24. *
  25. * where DIV_ROUND_UP(n, d) => (((n) + (d) - 1) / (d))
  26. *
  27. * We cannot include linux/kernel.h which defines DIV_ROUND_UP here
  28. * due to build issues. So we open code DIV_ROUND_UP here:
  29. *
  30. * ((((va_bits) - PAGE_SHIFT) + (PAGE_SHIFT - 3) - 1) / (PAGE_SHIFT - 3))
  31. *
  32. * which gets simplified as :
  33. */
  34. #define ARM64_HW_PGTABLE_LEVELS(va_bits) (((va_bits) - 4) / (PAGE_SHIFT - 3))
  35. /*
  36. * Size mapped by an entry at level n ( 0 <= n <= 3)
  37. * We map (PAGE_SHIFT - 3) at all translation levels and PAGE_SHIFT bits
  38. * in the final page. The maximum number of translation levels supported by
  39. * the architecture is 4. Hence, starting at at level n, we have further
  40. * ((4 - n) - 1) levels of translation excluding the offset within the page.
  41. * So, the total number of bits mapped by an entry at level n is :
  42. *
  43. * ((4 - n) - 1) * (PAGE_SHIFT - 3) + PAGE_SHIFT
  44. *
  45. * Rearranging it a bit we get :
  46. * (4 - n) * (PAGE_SHIFT - 3) + 3
  47. */
  48. #define ARM64_HW_PGTABLE_LEVEL_SHIFT(n) ((PAGE_SHIFT - 3) * (4 - (n)) + 3)
  49. #define PTRS_PER_PTE (1 << (PAGE_SHIFT - 3))
  50. /*
  51. * PMD_SHIFT determines the size a level 2 page table entry can map.
  52. */
  53. #if CONFIG_PGTABLE_LEVELS > 2
  54. #define PMD_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(2)
  55. #define PMD_SIZE (_AC(1, UL) << PMD_SHIFT)
  56. #define PMD_MASK (~(PMD_SIZE-1))
  57. #define PTRS_PER_PMD PTRS_PER_PTE
  58. #endif
  59. /*
  60. * PUD_SHIFT determines the size a level 1 page table entry can map.
  61. */
  62. #if CONFIG_PGTABLE_LEVELS > 3
  63. #define PUD_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(1)
  64. #define PUD_SIZE (_AC(1, UL) << PUD_SHIFT)
  65. #define PUD_MASK (~(PUD_SIZE-1))
  66. #define PTRS_PER_PUD PTRS_PER_PTE
  67. #endif
  68. /*
  69. * PGDIR_SHIFT determines the size a top-level page table entry can map
  70. * (depending on the configuration, this level can be 0, 1 or 2).
  71. */
  72. #define PGDIR_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(4 - CONFIG_PGTABLE_LEVELS)
  73. #define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT)
  74. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  75. #define PTRS_PER_PGD (1 << (VA_BITS - PGDIR_SHIFT))
  76. /*
  77. * Section address mask and size definitions.
  78. */
  79. #define SECTION_SHIFT PMD_SHIFT
  80. #define SECTION_SIZE (_AC(1, UL) << SECTION_SHIFT)
  81. #define SECTION_MASK (~(SECTION_SIZE-1))
  82. /*
  83. * Contiguous page definitions.
  84. */
  85. #define CONT_PTES (_AC(1, UL) << CONT_SHIFT)
  86. /* the the numerical offset of the PTE within a range of CONT_PTES */
  87. #define CONT_RANGE_OFFSET(addr) (((addr)>>PAGE_SHIFT)&(CONT_PTES-1))
  88. /*
  89. * Hardware page table definitions.
  90. *
  91. * Level 1 descriptor (PUD).
  92. */
  93. #define PUD_TYPE_TABLE (_AT(pudval_t, 3) << 0)
  94. #define PUD_TABLE_BIT (_AT(pgdval_t, 1) << 1)
  95. #define PUD_TYPE_MASK (_AT(pgdval_t, 3) << 0)
  96. #define PUD_TYPE_SECT (_AT(pgdval_t, 1) << 0)
  97. /*
  98. * Level 2 descriptor (PMD).
  99. */
  100. #define PMD_TYPE_MASK (_AT(pmdval_t, 3) << 0)
  101. #define PMD_TYPE_FAULT (_AT(pmdval_t, 0) << 0)
  102. #define PMD_TYPE_TABLE (_AT(pmdval_t, 3) << 0)
  103. #define PMD_TYPE_SECT (_AT(pmdval_t, 1) << 0)
  104. #define PMD_TABLE_BIT (_AT(pmdval_t, 1) << 1)
  105. /*
  106. * Section
  107. */
  108. #define PMD_SECT_VALID (_AT(pmdval_t, 1) << 0)
  109. #define PMD_SECT_USER (_AT(pmdval_t, 1) << 6) /* AP[1] */
  110. #define PMD_SECT_RDONLY (_AT(pmdval_t, 1) << 7) /* AP[2] */
  111. #define PMD_SECT_S (_AT(pmdval_t, 3) << 8)
  112. #define PMD_SECT_AF (_AT(pmdval_t, 1) << 10)
  113. #define PMD_SECT_NG (_AT(pmdval_t, 1) << 11)
  114. #define PMD_SECT_CONT (_AT(pmdval_t, 1) << 52)
  115. #define PMD_SECT_PXN (_AT(pmdval_t, 1) << 53)
  116. #define PMD_SECT_UXN (_AT(pmdval_t, 1) << 54)
  117. /*
  118. * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers).
  119. */
  120. #define PMD_ATTRINDX(t) (_AT(pmdval_t, (t)) << 2)
  121. #define PMD_ATTRINDX_MASK (_AT(pmdval_t, 7) << 2)
  122. /*
  123. * Level 3 descriptor (PTE).
  124. */
  125. #define PTE_TYPE_MASK (_AT(pteval_t, 3) << 0)
  126. #define PTE_TYPE_FAULT (_AT(pteval_t, 0) << 0)
  127. #define PTE_TYPE_PAGE (_AT(pteval_t, 3) << 0)
  128. #define PTE_TABLE_BIT (_AT(pteval_t, 1) << 1)
  129. #define PTE_USER (_AT(pteval_t, 1) << 6) /* AP[1] */
  130. #define PTE_RDONLY (_AT(pteval_t, 1) << 7) /* AP[2] */
  131. #define PTE_SHARED (_AT(pteval_t, 3) << 8) /* SH[1:0], inner shareable */
  132. #define PTE_AF (_AT(pteval_t, 1) << 10) /* Access Flag */
  133. #define PTE_NG (_AT(pteval_t, 1) << 11) /* nG */
  134. #define PTE_DBM (_AT(pteval_t, 1) << 51) /* Dirty Bit Management */
  135. #define PTE_CONT (_AT(pteval_t, 1) << 52) /* Contiguous range */
  136. #define PTE_PXN (_AT(pteval_t, 1) << 53) /* Privileged XN */
  137. #define PTE_UXN (_AT(pteval_t, 1) << 54) /* User XN */
  138. /*
  139. * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers).
  140. */
  141. #define PTE_ATTRINDX(t) (_AT(pteval_t, (t)) << 2)
  142. #define PTE_ATTRINDX_MASK (_AT(pteval_t, 7) << 2)
  143. /*
  144. * 2nd stage PTE definitions
  145. */
  146. #define PTE_S2_RDONLY (_AT(pteval_t, 1) << 6) /* HAP[2:1] */
  147. #define PTE_S2_RDWR (_AT(pteval_t, 3) << 6) /* HAP[2:1] */
  148. #define PMD_S2_RDONLY (_AT(pmdval_t, 1) << 6) /* HAP[2:1] */
  149. #define PMD_S2_RDWR (_AT(pmdval_t, 3) << 6) /* HAP[2:1] */
  150. /*
  151. * Memory Attribute override for Stage-2 (MemAttr[3:0])
  152. */
  153. #define PTE_S2_MEMATTR(t) (_AT(pteval_t, (t)) << 2)
  154. #define PTE_S2_MEMATTR_MASK (_AT(pteval_t, 0xf) << 2)
  155. /*
  156. * EL2/HYP PTE/PMD definitions
  157. */
  158. #define PMD_HYP PMD_SECT_USER
  159. #define PTE_HYP PTE_USER
  160. /*
  161. * Highest possible physical address supported.
  162. */
  163. #define PHYS_MASK_SHIFT (48)
  164. #define PHYS_MASK ((UL(1) << PHYS_MASK_SHIFT) - 1)
  165. /*
  166. * TCR flags.
  167. */
  168. #define TCR_T0SZ_OFFSET 0
  169. #define TCR_T1SZ_OFFSET 16
  170. #define TCR_T0SZ(x) ((UL(64) - (x)) << TCR_T0SZ_OFFSET)
  171. #define TCR_T1SZ(x) ((UL(64) - (x)) << TCR_T1SZ_OFFSET)
  172. #define TCR_TxSZ(x) (TCR_T0SZ(x) | TCR_T1SZ(x))
  173. #define TCR_TxSZ_WIDTH 6
  174. #define TCR_IRGN_NC ((UL(0) << 8) | (UL(0) << 24))
  175. #define TCR_IRGN_WBWA ((UL(1) << 8) | (UL(1) << 24))
  176. #define TCR_IRGN_WT ((UL(2) << 8) | (UL(2) << 24))
  177. #define TCR_IRGN_WBnWA ((UL(3) << 8) | (UL(3) << 24))
  178. #define TCR_IRGN_MASK ((UL(3) << 8) | (UL(3) << 24))
  179. #define TCR_ORGN_NC ((UL(0) << 10) | (UL(0) << 26))
  180. #define TCR_ORGN_WBWA ((UL(1) << 10) | (UL(1) << 26))
  181. #define TCR_ORGN_WT ((UL(2) << 10) | (UL(2) << 26))
  182. #define TCR_ORGN_WBnWA ((UL(3) << 10) | (UL(3) << 26))
  183. #define TCR_ORGN_MASK ((UL(3) << 10) | (UL(3) << 26))
  184. #define TCR_SHARED ((UL(3) << 12) | (UL(3) << 28))
  185. #define TCR_TG0_4K (UL(0) << 14)
  186. #define TCR_TG0_64K (UL(1) << 14)
  187. #define TCR_TG0_16K (UL(2) << 14)
  188. #define TCR_TG1_16K (UL(1) << 30)
  189. #define TCR_TG1_4K (UL(2) << 30)
  190. #define TCR_TG1_64K (UL(3) << 30)
  191. #define TCR_ASID16 (UL(1) << 36)
  192. #define TCR_TBI0 (UL(1) << 37)
  193. #define TCR_HA (UL(1) << 39)
  194. #define TCR_HD (UL(1) << 40)
  195. #endif