pgtable-bits.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 1994 - 2002 by Ralf Baechle
  4. * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
  5. * Copyright (C) 2002 Maciej W. Rozycki
  6. */
  7. #ifndef _ASM_PGTABLE_BITS_H
  8. #define _ASM_PGTABLE_BITS_H
  9. /*
  10. * Note that we shift the lower 32bits of each EntryLo[01] entry
  11. * 6 bits to the left. That way we can convert the PFN into the
  12. * physical address by a single 'and' operation and gain 6 additional
  13. * bits for storing information which isn't present in a normal
  14. * MIPS page table.
  15. *
  16. * Similar to the Alpha port, we need to keep track of the ref
  17. * and mod bits in software. We have a software "yeah you can read
  18. * from this page" bit, and a hardware one which actually lets the
  19. * process read from the page. On the same token we have a software
  20. * writable bit and the real hardware one which actually lets the
  21. * process write to the page, this keeps a mod bit via the hardware
  22. * dirty bit.
  23. *
  24. * Certain revisions of the R4000 and R5000 have a bug where if a
  25. * certain sequence occurs in the last 3 instructions of an executable
  26. * page, and the following page is not mapped, the cpu can do
  27. * unpredictable things. The code (when it is written) to deal with
  28. * this problem will be in the update_mmu_cache() code for the r4k.
  29. */
  30. #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
  31. /*
  32. * The following bits are implemented by the TLB hardware
  33. */
  34. #define _PAGE_NO_EXEC_SHIFT 0
  35. #define _PAGE_NO_EXEC (1 << _PAGE_NO_EXEC_SHIFT)
  36. #define _PAGE_NO_READ_SHIFT (_PAGE_NO_EXEC_SHIFT + 1)
  37. #define _PAGE_NO_READ (1 << _PAGE_NO_READ_SHIFT)
  38. #define _PAGE_GLOBAL_SHIFT (_PAGE_NO_READ_SHIFT + 1)
  39. #define _PAGE_GLOBAL (1 << _PAGE_GLOBAL_SHIFT)
  40. #define _PAGE_VALID_SHIFT (_PAGE_GLOBAL_SHIFT + 1)
  41. #define _PAGE_VALID (1 << _PAGE_VALID_SHIFT)
  42. #define _PAGE_DIRTY_SHIFT (_PAGE_VALID_SHIFT + 1)
  43. #define _PAGE_DIRTY (1 << _PAGE_DIRTY_SHIFT)
  44. #define _CACHE_SHIFT (_PAGE_DIRTY_SHIFT + 1)
  45. #define _CACHE_MASK (7 << _CACHE_SHIFT)
  46. /*
  47. * The following bits are implemented in software
  48. */
  49. #define _PAGE_PRESENT_SHIFT (24)
  50. #define _PAGE_PRESENT (1 << _PAGE_PRESENT_SHIFT)
  51. #define _PAGE_READ_SHIFT (_PAGE_PRESENT_SHIFT + 1)
  52. #define _PAGE_READ (1 << _PAGE_READ_SHIFT)
  53. #define _PAGE_WRITE_SHIFT (_PAGE_READ_SHIFT + 1)
  54. #define _PAGE_WRITE (1 << _PAGE_WRITE_SHIFT)
  55. #define _PAGE_ACCESSED_SHIFT (_PAGE_WRITE_SHIFT + 1)
  56. #define _PAGE_ACCESSED (1 << _PAGE_ACCESSED_SHIFT)
  57. #define _PAGE_MODIFIED_SHIFT (_PAGE_ACCESSED_SHIFT + 1)
  58. #define _PAGE_MODIFIED (1 << _PAGE_MODIFIED_SHIFT)
  59. #define _PFN_SHIFT (PAGE_SHIFT - 12 + _CACHE_SHIFT + 3)
  60. /*
  61. * Bits for extended EntryLo0/EntryLo1 registers
  62. */
  63. #define _PFNX_MASK 0xffffff
  64. #elif defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  65. /*
  66. * The following bits are implemented in software
  67. */
  68. #define _PAGE_PRESENT_SHIFT (0)
  69. #define _PAGE_PRESENT (1 << _PAGE_PRESENT_SHIFT)
  70. #define _PAGE_READ_SHIFT (_PAGE_PRESENT_SHIFT + 1)
  71. #define _PAGE_READ (1 << _PAGE_READ_SHIFT)
  72. #define _PAGE_WRITE_SHIFT (_PAGE_READ_SHIFT + 1)
  73. #define _PAGE_WRITE (1 << _PAGE_WRITE_SHIFT)
  74. #define _PAGE_ACCESSED_SHIFT (_PAGE_WRITE_SHIFT + 1)
  75. #define _PAGE_ACCESSED (1 << _PAGE_ACCESSED_SHIFT)
  76. #define _PAGE_MODIFIED_SHIFT (_PAGE_ACCESSED_SHIFT + 1)
  77. #define _PAGE_MODIFIED (1 << _PAGE_MODIFIED_SHIFT)
  78. /*
  79. * The following bits are implemented by the TLB hardware
  80. */
  81. #define _PAGE_GLOBAL_SHIFT (_PAGE_MODIFIED_SHIFT + 4)
  82. #define _PAGE_GLOBAL (1 << _PAGE_GLOBAL_SHIFT)
  83. #define _PAGE_VALID_SHIFT (_PAGE_GLOBAL_SHIFT + 1)
  84. #define _PAGE_VALID (1 << _PAGE_VALID_SHIFT)
  85. #define _PAGE_DIRTY_SHIFT (_PAGE_VALID_SHIFT + 1)
  86. #define _PAGE_DIRTY (1 << _PAGE_DIRTY_SHIFT)
  87. #define _CACHE_UNCACHED_SHIFT (_PAGE_DIRTY_SHIFT + 1)
  88. #define _CACHE_UNCACHED (1 << _CACHE_UNCACHED_SHIFT)
  89. #define _CACHE_MASK _CACHE_UNCACHED
  90. #define _PFN_SHIFT PAGE_SHIFT
  91. #else
  92. /*
  93. * Below are the "Normal" R4K cases
  94. */
  95. /*
  96. * The following bits are implemented in software
  97. */
  98. #define _PAGE_PRESENT_SHIFT 0
  99. #define _PAGE_PRESENT (1 << _PAGE_PRESENT_SHIFT)
  100. /* R2 or later cores check for RI/XI support to determine _PAGE_READ */
  101. #if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6)
  102. #define _PAGE_WRITE_SHIFT (_PAGE_PRESENT_SHIFT + 1)
  103. #define _PAGE_WRITE (1 << _PAGE_WRITE_SHIFT)
  104. #else
  105. #define _PAGE_READ_SHIFT (_PAGE_PRESENT_SHIFT + 1)
  106. #define _PAGE_READ (1 << _PAGE_READ_SHIFT)
  107. #define _PAGE_WRITE_SHIFT (_PAGE_READ_SHIFT + 1)
  108. #define _PAGE_WRITE (1 << _PAGE_WRITE_SHIFT)
  109. #endif
  110. #define _PAGE_ACCESSED_SHIFT (_PAGE_WRITE_SHIFT + 1)
  111. #define _PAGE_ACCESSED (1 << _PAGE_ACCESSED_SHIFT)
  112. #define _PAGE_MODIFIED_SHIFT (_PAGE_ACCESSED_SHIFT + 1)
  113. #define _PAGE_MODIFIED (1 << _PAGE_MODIFIED_SHIFT)
  114. #if defined(CONFIG_64BIT) && defined(CONFIG_MIPS_HUGE_TLB_SUPPORT)
  115. /* Huge TLB page */
  116. #define _PAGE_HUGE_SHIFT (_PAGE_MODIFIED_SHIFT + 1)
  117. #define _PAGE_HUGE (1 << _PAGE_HUGE_SHIFT)
  118. #define _PAGE_SPLITTING_SHIFT (_PAGE_HUGE_SHIFT + 1)
  119. #define _PAGE_SPLITTING (1 << _PAGE_SPLITTING_SHIFT)
  120. #endif /* CONFIG_64BIT && CONFIG_MIPS_HUGE_TLB_SUPPORT */
  121. #if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6)
  122. /* XI - page cannot be executed */
  123. #ifdef _PAGE_SPLITTING_SHIFT
  124. #define _PAGE_NO_EXEC_SHIFT (_PAGE_SPLITTING_SHIFT + 1)
  125. #else
  126. #define _PAGE_NO_EXEC_SHIFT (_PAGE_MODIFIED_SHIFT + 1)
  127. #endif
  128. #define _PAGE_NO_EXEC (cpu_has_rixi ? (1 << _PAGE_NO_EXEC_SHIFT) : 0)
  129. /* RI - page cannot be read */
  130. #define _PAGE_READ_SHIFT (_PAGE_NO_EXEC_SHIFT + 1)
  131. #define _PAGE_READ (cpu_has_rixi ? 0 : (1 << _PAGE_READ_SHIFT))
  132. #define _PAGE_NO_READ_SHIFT _PAGE_READ_SHIFT
  133. #define _PAGE_NO_READ (cpu_has_rixi ? (1 << _PAGE_READ_SHIFT) : 0)
  134. #endif /* defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) */
  135. #if defined(_PAGE_NO_READ_SHIFT)
  136. #define _PAGE_GLOBAL_SHIFT (_PAGE_NO_READ_SHIFT + 1)
  137. #elif defined(_PAGE_SPLITTING_SHIFT)
  138. #define _PAGE_GLOBAL_SHIFT (_PAGE_SPLITTING_SHIFT + 1)
  139. #else
  140. #define _PAGE_GLOBAL_SHIFT (_PAGE_MODIFIED_SHIFT + 1)
  141. #endif
  142. #define _PAGE_GLOBAL (1 << _PAGE_GLOBAL_SHIFT)
  143. #define _PAGE_VALID_SHIFT (_PAGE_GLOBAL_SHIFT + 1)
  144. #define _PAGE_VALID (1 << _PAGE_VALID_SHIFT)
  145. #define _PAGE_DIRTY_SHIFT (_PAGE_VALID_SHIFT + 1)
  146. #define _PAGE_DIRTY (1 << _PAGE_DIRTY_SHIFT)
  147. #define _CACHE_SHIFT (_PAGE_DIRTY_SHIFT + 1)
  148. #define _CACHE_MASK (7 << _CACHE_SHIFT)
  149. #define _PFN_SHIFT (PAGE_SHIFT - 12 + _CACHE_SHIFT + 3)
  150. #endif /* defined(CONFIG_PHYS_ADDR_T_64BIT && defined(CONFIG_CPU_MIPS32) */
  151. #ifndef _PAGE_NO_EXEC
  152. #define _PAGE_NO_EXEC 0
  153. #endif
  154. #ifndef _PAGE_NO_READ
  155. #define _PAGE_NO_READ 0
  156. #endif
  157. #define _PAGE_SILENT_READ _PAGE_VALID
  158. #define _PAGE_SILENT_WRITE _PAGE_DIRTY
  159. #define _PFN_MASK (~((1 << (_PFN_SHIFT)) - 1))
  160. /*
  161. * The final layouts of the PTE bits are:
  162. *
  163. * 64-bit, R1 or earlier: CCC D V G [S H] M A W R P
  164. * 32-bit, R1 or earler: CCC D V G M A W R P
  165. * 64-bit, R2 or later: CCC D V G RI/R XI [S H] M A W P
  166. * 32-bit, R2 or later: CCC D V G RI/R XI M A W P
  167. */
  168. #ifndef __ASSEMBLY__
  169. /*
  170. * pte_to_entrylo converts a page table entry (PTE) into a Mips
  171. * entrylo0/1 value.
  172. */
  173. static inline uint64_t pte_to_entrylo(unsigned long pte_val)
  174. {
  175. #if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6)
  176. if (cpu_has_rixi) {
  177. int sa;
  178. #ifdef CONFIG_32BIT
  179. sa = 31 - _PAGE_NO_READ_SHIFT;
  180. #else
  181. sa = 63 - _PAGE_NO_READ_SHIFT;
  182. #endif
  183. /*
  184. * C has no way to express that this is a DSRL
  185. * _PAGE_NO_EXEC_SHIFT followed by a ROTR 2. Luckily
  186. * in the fast path this is done in assembly
  187. */
  188. return (pte_val >> _PAGE_GLOBAL_SHIFT) |
  189. ((pte_val & (_PAGE_NO_EXEC | _PAGE_NO_READ)) << sa);
  190. }
  191. #endif
  192. return pte_val >> _PAGE_GLOBAL_SHIFT;
  193. }
  194. #endif
  195. /*
  196. * Cache attributes
  197. */
  198. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  199. #define _CACHE_CACHABLE_NONCOHERENT 0
  200. #define _CACHE_UNCACHED_ACCELERATED _CACHE_UNCACHED
  201. #elif defined(CONFIG_CPU_SB1)
  202. /* No penalty for being coherent on the SB1, so just
  203. use it for "noncoherent" spaces, too. Shouldn't hurt. */
  204. #define _CACHE_CACHABLE_NONCOHERENT (5<<_CACHE_SHIFT)
  205. #elif defined(CONFIG_CPU_LOONGSON3)
  206. /* Using COHERENT flag for NONCOHERENT doesn't hurt. */
  207. #define _CACHE_CACHABLE_NONCOHERENT (3<<_CACHE_SHIFT) /* LOONGSON */
  208. #define _CACHE_CACHABLE_COHERENT (3<<_CACHE_SHIFT) /* LOONGSON-3 */
  209. #elif defined(CONFIG_MACH_INGENIC)
  210. /* Ingenic uses the WA bit to achieve write-combine memory writes */
  211. #define _CACHE_UNCACHED_ACCELERATED (1<<_CACHE_SHIFT)
  212. #endif
  213. #ifndef _CACHE_CACHABLE_NO_WA
  214. #define _CACHE_CACHABLE_NO_WA (0<<_CACHE_SHIFT)
  215. #endif
  216. #ifndef _CACHE_CACHABLE_WA
  217. #define _CACHE_CACHABLE_WA (1<<_CACHE_SHIFT)
  218. #endif
  219. #ifndef _CACHE_UNCACHED
  220. #define _CACHE_UNCACHED (2<<_CACHE_SHIFT)
  221. #endif
  222. #ifndef _CACHE_CACHABLE_NONCOHERENT
  223. #define _CACHE_CACHABLE_NONCOHERENT (3<<_CACHE_SHIFT)
  224. #endif
  225. #ifndef _CACHE_CACHABLE_CE
  226. #define _CACHE_CACHABLE_CE (4<<_CACHE_SHIFT)
  227. #endif
  228. #ifndef _CACHE_CACHABLE_COW
  229. #define _CACHE_CACHABLE_COW (5<<_CACHE_SHIFT)
  230. #endif
  231. #ifndef _CACHE_CACHABLE_CUW
  232. #define _CACHE_CACHABLE_CUW (6<<_CACHE_SHIFT)
  233. #endif
  234. #ifndef _CACHE_UNCACHED_ACCELERATED
  235. #define _CACHE_UNCACHED_ACCELERATED (7<<_CACHE_SHIFT)
  236. #endif
  237. #define __READABLE (_PAGE_SILENT_READ | _PAGE_READ | _PAGE_ACCESSED)
  238. #define __WRITEABLE (_PAGE_SILENT_WRITE | _PAGE_WRITE | _PAGE_MODIFIED)
  239. #define _PAGE_CHG_MASK (_PAGE_ACCESSED | _PAGE_MODIFIED | \
  240. _PFN_MASK | _CACHE_MASK)
  241. #endif /* _ASM_PGTABLE_BITS_H */