mmu.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 (42) /* 42 bits virtual address */
  21. /* PAGE_SHIFT determines the page size */
  22. #undef PAGE_SIZE
  23. #define PAGE_SHIFT 16
  24. #define PAGE_SIZE (1 << PAGE_SHIFT)
  25. #define PAGE_MASK (~(PAGE_SIZE-1))
  26. /*
  27. * section address mask and size definitions.
  28. */
  29. #define SECTION_SHIFT 29
  30. #define SECTION_SIZE (UL(1) << SECTION_SHIFT)
  31. #define SECTION_MASK (~(SECTION_SIZE-1))
  32. /***************************************************************/
  33. /*
  34. * Memory types
  35. */
  36. #define MT_DEVICE_NGNRNE 0
  37. #define MT_DEVICE_NGNRE 1
  38. #define MT_DEVICE_GRE 2
  39. #define MT_NORMAL_NC 3
  40. #define MT_NORMAL 4
  41. #define MEMORY_ATTRIBUTES ((0x00 << (MT_DEVICE_NGNRNE*8)) | \
  42. (0x04 << (MT_DEVICE_NGNRE*8)) | \
  43. (0x0c << (MT_DEVICE_GRE*8)) | \
  44. (0x44 << (MT_NORMAL_NC*8)) | \
  45. (UL(0xff) << (MT_NORMAL*8)))
  46. /*
  47. * Hardware page table definitions.
  48. *
  49. * Level 2 descriptor (PMD).
  50. */
  51. #define PMD_TYPE_MASK (3 << 0)
  52. #define PMD_TYPE_FAULT (0 << 0)
  53. #define PMD_TYPE_TABLE (3 << 0)
  54. #define PMD_TYPE_SECT (1 << 0)
  55. /*
  56. * Section
  57. */
  58. #define PMD_SECT_NS (1 << 5)
  59. #define PMD_SECT_NON_SHARE (0 << 8)
  60. #define PMD_SECT_OUTER_SHARE (2 << 8)
  61. #define PMD_SECT_INNER_SHARE (3 << 8)
  62. #define PMD_SECT_AF (1 << 10)
  63. #define PMD_SECT_NG (1 << 11)
  64. #define PMD_SECT_PXN (UL(1) << 53)
  65. #define PMD_SECT_UXN (UL(1) << 54)
  66. /*
  67. * AttrIndx[2:0]
  68. */
  69. #define PMD_ATTRINDX(t) ((t) << 2)
  70. #define PMD_ATTRINDX_MASK (7 << 2)
  71. /*
  72. * TCR flags.
  73. */
  74. #define TCR_T0SZ(x) ((64 - (x)) << 0)
  75. #define TCR_IRGN_NC (0 << 8)
  76. #define TCR_IRGN_WBWA (1 << 8)
  77. #define TCR_IRGN_WT (2 << 8)
  78. #define TCR_IRGN_WBNWA (3 << 8)
  79. #define TCR_IRGN_MASK (3 << 8)
  80. #define TCR_ORGN_NC (0 << 10)
  81. #define TCR_ORGN_WBWA (1 << 10)
  82. #define TCR_ORGN_WT (2 << 10)
  83. #define TCR_ORGN_WBNWA (3 << 10)
  84. #define TCR_ORGN_MASK (3 << 10)
  85. #define TCR_SHARED_NON (0 << 12)
  86. #define TCR_SHARED_OUTER (2 << 12)
  87. #define TCR_SHARED_INNER (3 << 12)
  88. #define TCR_TG0_4K (0 << 14)
  89. #define TCR_TG0_64K (1 << 14)
  90. #define TCR_TG0_16K (2 << 14)
  91. #define TCR_EL1_IPS_BITS (UL(3) << 32) /* 42 bits physical address */
  92. #define TCR_EL2_IPS_BITS (3 << 16) /* 42 bits physical address */
  93. #define TCR_EL3_IPS_BITS (3 << 16) /* 42 bits physical address */
  94. /* PTWs cacheable, inner/outer WBWA and inner shareable */
  95. #define TCR_FLAGS (TCR_TG0_64K | \
  96. TCR_SHARED_INNER | \
  97. TCR_ORGN_WBWA | \
  98. TCR_IRGN_WBWA | \
  99. TCR_T0SZ(VA_BITS))
  100. #define TCR_EL1_RSVD (1 << 31)
  101. #define TCR_EL2_RSVD (1 << 31 | 1 << 23)
  102. #define TCR_EL3_RSVD (1 << 31 | 1 << 23)
  103. #ifndef __ASSEMBLY__
  104. void set_pgtable_section(u64 *page_table, u64 index,
  105. u64 section, u64 memory_type,
  106. u64 share);
  107. void set_pgtable_table(u64 *page_table, u64 index,
  108. u64 *table_addr);
  109. static inline void set_ttbr_tcr_mair(int el, u64 table, u64 tcr, u64 attr)
  110. {
  111. asm volatile("dsb sy");
  112. if (el == 1) {
  113. asm volatile("msr ttbr0_el1, %0" : : "r" (table) : "memory");
  114. asm volatile("msr tcr_el1, %0" : : "r" (tcr) : "memory");
  115. asm volatile("msr mair_el1, %0" : : "r" (attr) : "memory");
  116. } else if (el == 2) {
  117. asm volatile("msr ttbr0_el2, %0" : : "r" (table) : "memory");
  118. asm volatile("msr tcr_el2, %0" : : "r" (tcr) : "memory");
  119. asm volatile("msr mair_el2, %0" : : "r" (attr) : "memory");
  120. } else if (el == 3) {
  121. asm volatile("msr ttbr0_el3, %0" : : "r" (table) : "memory");
  122. asm volatile("msr tcr_el3, %0" : : "r" (tcr) : "memory");
  123. asm volatile("msr mair_el3, %0" : : "r" (attr) : "memory");
  124. } else {
  125. hang();
  126. }
  127. asm volatile("isb");
  128. }
  129. #endif
  130. #endif /* _ASM_ARMV8_MMU_H_ */