mmu.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_OUTER_SHARE (2 << 8)
  59. #define PMD_SECT_INNER_SHARE (3 << 8)
  60. #define PMD_SECT_AF (1 << 10)
  61. #define PMD_SECT_NG (1 << 11)
  62. #define PMD_SECT_PXN (UL(1) << 53)
  63. #define PMD_SECT_UXN (UL(1) << 54)
  64. /*
  65. * AttrIndx[2:0]
  66. */
  67. #define PMD_ATTRINDX(t) ((t) << 2)
  68. #define PMD_ATTRINDX_MASK (7 << 2)
  69. /*
  70. * TCR flags.
  71. */
  72. #define TCR_T0SZ(x) ((64 - (x)) << 0)
  73. #define TCR_IRGN_NC (0 << 8)
  74. #define TCR_IRGN_WBWA (1 << 8)
  75. #define TCR_IRGN_WT (2 << 8)
  76. #define TCR_IRGN_WBNWA (3 << 8)
  77. #define TCR_IRGN_MASK (3 << 8)
  78. #define TCR_ORGN_NC (0 << 10)
  79. #define TCR_ORGN_WBWA (1 << 10)
  80. #define TCR_ORGN_WT (2 << 10)
  81. #define TCR_ORGN_WBNWA (3 << 10)
  82. #define TCR_ORGN_MASK (3 << 10)
  83. #define TCR_SHARED_NON (0 << 12)
  84. #define TCR_SHARED_OUTER (1 << 12)
  85. #define TCR_SHARED_INNER (2 << 12)
  86. #define TCR_TG0_4K (0 << 14)
  87. #define TCR_TG0_64K (1 << 14)
  88. #define TCR_TG0_16K (2 << 14)
  89. #define TCR_EL1_IPS_BITS (UL(3) << 32) /* 42 bits physical address */
  90. #define TCR_EL2_IPS_BITS (3 << 16) /* 42 bits physical address */
  91. #define TCR_EL3_IPS_BITS (3 << 16) /* 42 bits physical address */
  92. /* PTWs cacheable, inner/outer WBWA and non-shareable */
  93. #define TCR_FLAGS (TCR_TG0_64K | \
  94. TCR_SHARED_NON | \
  95. TCR_ORGN_WBWA | \
  96. TCR_IRGN_WBWA | \
  97. TCR_T0SZ(VA_BITS))
  98. #ifndef __ASSEMBLY__
  99. void set_pgtable_section(u64 *page_table, u64 index,
  100. u64 section, u64 memory_type);
  101. static inline void set_ttbr_tcr_mair(int el, u64 table, u64 tcr, u64 attr)
  102. {
  103. asm volatile("dsb sy");
  104. if (el == 1) {
  105. asm volatile("msr ttbr0_el1, %0" : : "r" (table) : "memory");
  106. asm volatile("msr tcr_el1, %0" : : "r" (tcr) : "memory");
  107. asm volatile("msr mair_el1, %0" : : "r" (attr) : "memory");
  108. } else if (el == 2) {
  109. asm volatile("msr ttbr0_el2, %0" : : "r" (table) : "memory");
  110. asm volatile("msr tcr_el2, %0" : : "r" (tcr) : "memory");
  111. asm volatile("msr mair_el2, %0" : : "r" (attr) : "memory");
  112. } else if (el == 3) {
  113. asm volatile("msr ttbr0_el3, %0" : : "r" (table) : "memory");
  114. asm volatile("msr tcr_el3, %0" : : "r" (tcr) : "memory");
  115. asm volatile("msr mair_el3, %0" : : "r" (attr) : "memory");
  116. } else {
  117. hang();
  118. }
  119. asm volatile("isb");
  120. }
  121. #endif
  122. #endif /* _ASM_ARMV8_MMU_H_ */