mmu.h 3.5 KB

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