mmu.h 3.6 KB

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