mmu.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2013
  4. * David Feng <fenghua@phytium.com.cn>
  5. */
  6. #ifndef _ASM_ARMV8_MMU_H_
  7. #define _ASM_ARMV8_MMU_H_
  8. #include <linux/const.h>
  9. /*
  10. * block/section address mask and size definitions.
  11. */
  12. /* PAGE_SHIFT determines the page size */
  13. #undef PAGE_SIZE
  14. #define PAGE_SHIFT 12
  15. #define PAGE_SIZE (1 << PAGE_SHIFT)
  16. #define PAGE_MASK (~(PAGE_SIZE - 1))
  17. /***************************************************************/
  18. /*
  19. * Memory types
  20. */
  21. #define MT_DEVICE_NGNRNE 0
  22. #define MT_DEVICE_NGNRE 1
  23. #define MT_DEVICE_GRE 2
  24. #define MT_NORMAL_NC 3
  25. #define MT_NORMAL 4
  26. #define MEMORY_ATTRIBUTES ((0x00 << (MT_DEVICE_NGNRNE * 8)) | \
  27. (0x04 << (MT_DEVICE_NGNRE * 8)) | \
  28. (0x0c << (MT_DEVICE_GRE * 8)) | \
  29. (0x44 << (MT_NORMAL_NC * 8)) | \
  30. (UL(0xff) << (MT_NORMAL * 8)))
  31. /*
  32. * Hardware page table definitions.
  33. *
  34. */
  35. #define PTE_TYPE_MASK (3 << 0)
  36. #define PTE_TYPE_FAULT (0 << 0)
  37. #define PTE_TYPE_TABLE (3 << 0)
  38. #define PTE_TYPE_PAGE (3 << 0)
  39. #define PTE_TYPE_BLOCK (1 << 0)
  40. #define PTE_TYPE_VALID (1 << 0)
  41. #define PTE_TABLE_PXN (1UL << 59)
  42. #define PTE_TABLE_XN (1UL << 60)
  43. #define PTE_TABLE_AP (1UL << 61)
  44. #define PTE_TABLE_NS (1UL << 63)
  45. /*
  46. * Block
  47. */
  48. #define PTE_BLOCK_MEMTYPE(x) ((x) << 2)
  49. #define PTE_BLOCK_NS (1 << 5)
  50. #define PTE_BLOCK_NON_SHARE (0 << 8)
  51. #define PTE_BLOCK_OUTER_SHARE (2 << 8)
  52. #define PTE_BLOCK_INNER_SHARE (3 << 8)
  53. #define PTE_BLOCK_AF (1 << 10)
  54. #define PTE_BLOCK_NG (1 << 11)
  55. #define PTE_BLOCK_PXN (UL(1) << 53)
  56. #define PTE_BLOCK_UXN (UL(1) << 54)
  57. /*
  58. * AttrIndx[2:0]
  59. */
  60. #define PMD_ATTRINDX(t) ((t) << 2)
  61. #define PMD_ATTRINDX_MASK (7 << 2)
  62. #define PMD_ATTRMASK (PTE_BLOCK_PXN | \
  63. PTE_BLOCK_UXN | \
  64. PMD_ATTRINDX_MASK | \
  65. PTE_TYPE_VALID)
  66. /*
  67. * TCR flags.
  68. */
  69. #define TCR_T0SZ(x) ((64 - (x)) << 0)
  70. #define TCR_IRGN_NC (0 << 8)
  71. #define TCR_IRGN_WBWA (1 << 8)
  72. #define TCR_IRGN_WT (2 << 8)
  73. #define TCR_IRGN_WBNWA (3 << 8)
  74. #define TCR_IRGN_MASK (3 << 8)
  75. #define TCR_ORGN_NC (0 << 10)
  76. #define TCR_ORGN_WBWA (1 << 10)
  77. #define TCR_ORGN_WT (2 << 10)
  78. #define TCR_ORGN_WBNWA (3 << 10)
  79. #define TCR_ORGN_MASK (3 << 10)
  80. #define TCR_SHARED_NON (0 << 12)
  81. #define TCR_SHARED_OUTER (2 << 12)
  82. #define TCR_SHARED_INNER (3 << 12)
  83. #define TCR_TG0_4K (0 << 14)
  84. #define TCR_TG0_64K (1 << 14)
  85. #define TCR_TG0_16K (2 << 14)
  86. #define TCR_EPD1_DISABLE (1 << 23)
  87. #define TCR_EL1_RSVD (1 << 31)
  88. #define TCR_EL2_RSVD (1 << 31 | 1 << 23)
  89. #define TCR_EL3_RSVD (1 << 31 | 1 << 23)
  90. #ifndef __ASSEMBLY__
  91. static inline void set_ttbr_tcr_mair(int el, u64 table, u64 tcr, u64 attr)
  92. {
  93. asm volatile("dsb sy");
  94. if (el == 1) {
  95. asm volatile("msr ttbr0_el1, %0" : : "r" (table) : "memory");
  96. asm volatile("msr tcr_el1, %0" : : "r" (tcr) : "memory");
  97. asm volatile("msr mair_el1, %0" : : "r" (attr) : "memory");
  98. } else if (el == 2) {
  99. asm volatile("msr ttbr0_el2, %0" : : "r" (table) : "memory");
  100. asm volatile("msr tcr_el2, %0" : : "r" (tcr) : "memory");
  101. asm volatile("msr mair_el2, %0" : : "r" (attr) : "memory");
  102. } else if (el == 3) {
  103. asm volatile("msr ttbr0_el3, %0" : : "r" (table) : "memory");
  104. asm volatile("msr tcr_el3, %0" : : "r" (tcr) : "memory");
  105. asm volatile("msr mair_el3, %0" : : "r" (attr) : "memory");
  106. } else {
  107. hang();
  108. }
  109. asm volatile("isb");
  110. }
  111. struct mm_region {
  112. u64 virt;
  113. u64 phys;
  114. u64 size;
  115. u64 attrs;
  116. };
  117. extern struct mm_region *mem_map;
  118. void setup_pgtables(void);
  119. u64 get_tcr(int el, u64 *pips, u64 *pva_bits);
  120. #endif
  121. #endif /* _ASM_ARMV8_MMU_H_ */