mmu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_POWERPC_MMU_H_
  3. #define _ASM_POWERPC_MMU_H_
  4. #ifdef __KERNEL__
  5. #include <linux/types.h>
  6. #include <asm/asm-const.h>
  7. /*
  8. * MMU features bit definitions
  9. */
  10. /*
  11. * MMU families
  12. */
  13. #define MMU_FTR_HPTE_TABLE ASM_CONST(0x00000001)
  14. #define MMU_FTR_TYPE_8xx ASM_CONST(0x00000002)
  15. #define MMU_FTR_TYPE_40x ASM_CONST(0x00000004)
  16. #define MMU_FTR_TYPE_44x ASM_CONST(0x00000008)
  17. #define MMU_FTR_TYPE_FSL_E ASM_CONST(0x00000010)
  18. #define MMU_FTR_TYPE_47x ASM_CONST(0x00000020)
  19. /* Radix page table supported and enabled */
  20. #define MMU_FTR_TYPE_RADIX ASM_CONST(0x00000040)
  21. /*
  22. * Individual features below.
  23. */
  24. /*
  25. * Supports KUAP feature
  26. * key 0 controlling userspace addresses on radix
  27. * Key 3 on hash
  28. */
  29. #define MMU_FTR_BOOK3S_KUAP ASM_CONST(0x00000200)
  30. /*
  31. * Supports KUEP feature
  32. * key 0 controlling userspace addresses on radix
  33. * Key 3 on hash
  34. */
  35. #define MMU_FTR_BOOK3S_KUEP ASM_CONST(0x00000400)
  36. /*
  37. * Support for memory protection keys.
  38. */
  39. #define MMU_FTR_PKEY ASM_CONST(0x00000800)
  40. /* Guest Translation Shootdown Enable */
  41. #define MMU_FTR_GTSE ASM_CONST(0x00001000)
  42. /*
  43. * Support for 68 bit VA space. We added that from ISA 2.05
  44. */
  45. #define MMU_FTR_68_BIT_VA ASM_CONST(0x00002000)
  46. /*
  47. * Kernel read only support.
  48. * We added the ppp value 0b110 in ISA 2.04.
  49. */
  50. #define MMU_FTR_KERNEL_RO ASM_CONST(0x00004000)
  51. /*
  52. * We need to clear top 16bits of va (from the remaining 64 bits )in
  53. * tlbie* instructions
  54. */
  55. #define MMU_FTR_TLBIE_CROP_VA ASM_CONST(0x00008000)
  56. /* Enable use of high BAT registers */
  57. #define MMU_FTR_USE_HIGH_BATS ASM_CONST(0x00010000)
  58. /* Enable >32-bit physical addresses on 32-bit processor, only used
  59. * by CONFIG_PPC_BOOK3S_32 currently as BookE supports that from day 1
  60. */
  61. #define MMU_FTR_BIG_PHYS ASM_CONST(0x00020000)
  62. /* Enable use of broadcast TLB invalidations. We don't always set it
  63. * on processors that support it due to other constraints with the
  64. * use of such invalidations
  65. */
  66. #define MMU_FTR_USE_TLBIVAX_BCAST ASM_CONST(0x00040000)
  67. /* Enable use of tlbilx invalidate instructions.
  68. */
  69. #define MMU_FTR_USE_TLBILX ASM_CONST(0x00080000)
  70. /* This indicates that the processor cannot handle multiple outstanding
  71. * broadcast tlbivax or tlbsync. This makes the code use a spinlock
  72. * around such invalidate forms.
  73. */
  74. #define MMU_FTR_LOCK_BCAST_INVAL ASM_CONST(0x00100000)
  75. /* This indicates that the processor doesn't handle way selection
  76. * properly and needs SW to track and update the LRU state. This
  77. * is specific to an errata on e300c2/c3/c4 class parts
  78. */
  79. #define MMU_FTR_NEED_DTLB_SW_LRU ASM_CONST(0x00200000)
  80. /* Doesn't support the B bit (1T segment) in SLBIE
  81. */
  82. #define MMU_FTR_NO_SLBIE_B ASM_CONST(0x02000000)
  83. /* Support 16M large pages
  84. */
  85. #define MMU_FTR_16M_PAGE ASM_CONST(0x04000000)
  86. /* Supports TLBIEL variant
  87. */
  88. #define MMU_FTR_TLBIEL ASM_CONST(0x08000000)
  89. /* Supports tlbies w/o locking
  90. */
  91. #define MMU_FTR_LOCKLESS_TLBIE ASM_CONST(0x10000000)
  92. /* Large pages can be marked CI
  93. */
  94. #define MMU_FTR_CI_LARGE_PAGE ASM_CONST(0x20000000)
  95. /* 1T segments available
  96. */
  97. #define MMU_FTR_1T_SEGMENT ASM_CONST(0x40000000)
  98. // NX paste RMA reject in DSI
  99. #define MMU_FTR_NX_DSI ASM_CONST(0x80000000)
  100. /* MMU feature bit sets for various CPUs */
  101. #define MMU_FTRS_DEFAULT_HPTE_ARCH_V2 (MMU_FTR_HPTE_TABLE | MMU_FTR_TLBIEL | MMU_FTR_16M_PAGE)
  102. #define MMU_FTRS_POWER MMU_FTRS_DEFAULT_HPTE_ARCH_V2
  103. #define MMU_FTRS_PPC970 MMU_FTRS_POWER | MMU_FTR_TLBIE_CROP_VA
  104. #define MMU_FTRS_POWER5 MMU_FTRS_POWER | MMU_FTR_LOCKLESS_TLBIE
  105. #define MMU_FTRS_POWER6 MMU_FTRS_POWER5 | MMU_FTR_KERNEL_RO | MMU_FTR_68_BIT_VA
  106. #define MMU_FTRS_POWER7 MMU_FTRS_POWER6
  107. #define MMU_FTRS_POWER8 MMU_FTRS_POWER6
  108. #define MMU_FTRS_POWER9 MMU_FTRS_POWER6
  109. #define MMU_FTRS_POWER10 MMU_FTRS_POWER6
  110. #define MMU_FTRS_CELL MMU_FTRS_DEFAULT_HPTE_ARCH_V2 | \
  111. MMU_FTR_CI_LARGE_PAGE
  112. #define MMU_FTRS_PA6T MMU_FTRS_DEFAULT_HPTE_ARCH_V2 | \
  113. MMU_FTR_CI_LARGE_PAGE | MMU_FTR_NO_SLBIE_B
  114. #ifndef __ASSEMBLY__
  115. #include <linux/bug.h>
  116. #include <asm/cputable.h>
  117. #include <asm/page.h>
  118. typedef pte_t *pgtable_t;
  119. #ifdef CONFIG_PPC_E500
  120. #include <asm/percpu.h>
  121. DECLARE_PER_CPU(int, next_tlbcam_idx);
  122. #endif
  123. enum {
  124. MMU_FTRS_POSSIBLE =
  125. #if defined(CONFIG_PPC_BOOK3S_604)
  126. MMU_FTR_HPTE_TABLE |
  127. #endif
  128. #ifdef CONFIG_PPC_8xx
  129. MMU_FTR_TYPE_8xx |
  130. #endif
  131. #ifdef CONFIG_40x
  132. MMU_FTR_TYPE_40x |
  133. #endif
  134. #ifdef CONFIG_PPC_47x
  135. MMU_FTR_TYPE_47x | MMU_FTR_USE_TLBIVAX_BCAST | MMU_FTR_LOCK_BCAST_INVAL |
  136. #elif defined(CONFIG_44x)
  137. MMU_FTR_TYPE_44x |
  138. #endif
  139. #ifdef CONFIG_PPC_E500
  140. MMU_FTR_TYPE_FSL_E | MMU_FTR_BIG_PHYS | MMU_FTR_USE_TLBILX |
  141. #endif
  142. #ifdef CONFIG_PPC_BOOK3S_32
  143. MMU_FTR_USE_HIGH_BATS |
  144. #endif
  145. #ifdef CONFIG_PPC_83xx
  146. MMU_FTR_NEED_DTLB_SW_LRU |
  147. #endif
  148. #ifdef CONFIG_PPC_BOOK3S_64
  149. MMU_FTR_KERNEL_RO |
  150. #ifdef CONFIG_PPC_64S_HASH_MMU
  151. MMU_FTR_NO_SLBIE_B | MMU_FTR_16M_PAGE | MMU_FTR_TLBIEL |
  152. MMU_FTR_LOCKLESS_TLBIE | MMU_FTR_CI_LARGE_PAGE |
  153. MMU_FTR_1T_SEGMENT | MMU_FTR_TLBIE_CROP_VA |
  154. MMU_FTR_68_BIT_VA | MMU_FTR_HPTE_TABLE |
  155. #endif
  156. #ifdef CONFIG_PPC_RADIX_MMU
  157. MMU_FTR_TYPE_RADIX |
  158. MMU_FTR_GTSE | MMU_FTR_NX_DSI |
  159. #endif /* CONFIG_PPC_RADIX_MMU */
  160. #endif
  161. #ifdef CONFIG_PPC_KUAP
  162. MMU_FTR_BOOK3S_KUAP |
  163. #endif /* CONFIG_PPC_KUAP */
  164. #ifdef CONFIG_PPC_MEM_KEYS
  165. MMU_FTR_PKEY |
  166. #endif
  167. #ifdef CONFIG_PPC_KUEP
  168. MMU_FTR_BOOK3S_KUEP |
  169. #endif /* CONFIG_PPC_KUAP */
  170. 0,
  171. };
  172. #if defined(CONFIG_PPC_BOOK3S_604) && !defined(CONFIG_PPC_BOOK3S_603)
  173. #define MMU_FTRS_ALWAYS MMU_FTR_HPTE_TABLE
  174. #endif
  175. #ifdef CONFIG_PPC_8xx
  176. #define MMU_FTRS_ALWAYS MMU_FTR_TYPE_8xx
  177. #endif
  178. #ifdef CONFIG_40x
  179. #define MMU_FTRS_ALWAYS MMU_FTR_TYPE_40x
  180. #endif
  181. #ifdef CONFIG_PPC_47x
  182. #define MMU_FTRS_ALWAYS MMU_FTR_TYPE_47x
  183. #elif defined(CONFIG_44x)
  184. #define MMU_FTRS_ALWAYS MMU_FTR_TYPE_44x
  185. #endif
  186. #ifdef CONFIG_PPC_E500
  187. #define MMU_FTRS_ALWAYS MMU_FTR_TYPE_FSL_E
  188. #endif
  189. /* BOOK3S_64 options */
  190. #if defined(CONFIG_PPC_RADIX_MMU) && !defined(CONFIG_PPC_64S_HASH_MMU)
  191. #define MMU_FTRS_ALWAYS MMU_FTR_TYPE_RADIX
  192. #elif !defined(CONFIG_PPC_RADIX_MMU) && defined(CONFIG_PPC_64S_HASH_MMU)
  193. #define MMU_FTRS_ALWAYS MMU_FTR_HPTE_TABLE
  194. #endif
  195. #ifndef MMU_FTRS_ALWAYS
  196. #define MMU_FTRS_ALWAYS 0
  197. #endif
  198. static __always_inline bool early_mmu_has_feature(unsigned long feature)
  199. {
  200. if (MMU_FTRS_ALWAYS & feature)
  201. return true;
  202. return !!(MMU_FTRS_POSSIBLE & cur_cpu_spec->mmu_features & feature);
  203. }
  204. #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECKS
  205. #include <linux/jump_label.h>
  206. #define NUM_MMU_FTR_KEYS 32
  207. extern struct static_key_true mmu_feature_keys[NUM_MMU_FTR_KEYS];
  208. extern void mmu_feature_keys_init(void);
  209. static __always_inline bool mmu_has_feature(unsigned long feature)
  210. {
  211. int i;
  212. #ifndef __clang__ /* clang can't cope with this */
  213. BUILD_BUG_ON(!__builtin_constant_p(feature));
  214. #endif
  215. #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECK_DEBUG
  216. if (!static_key_initialized) {
  217. printk("Warning! mmu_has_feature() used prior to jump label init!\n");
  218. dump_stack();
  219. return early_mmu_has_feature(feature);
  220. }
  221. #endif
  222. if (MMU_FTRS_ALWAYS & feature)
  223. return true;
  224. if (!(MMU_FTRS_POSSIBLE & feature))
  225. return false;
  226. i = __builtin_ctzl(feature);
  227. return static_branch_likely(&mmu_feature_keys[i]);
  228. }
  229. static inline void mmu_clear_feature(unsigned long feature)
  230. {
  231. int i;
  232. i = __builtin_ctzl(feature);
  233. cur_cpu_spec->mmu_features &= ~feature;
  234. static_branch_disable(&mmu_feature_keys[i]);
  235. }
  236. #else
  237. static inline void mmu_feature_keys_init(void)
  238. {
  239. }
  240. static __always_inline bool mmu_has_feature(unsigned long feature)
  241. {
  242. return early_mmu_has_feature(feature);
  243. }
  244. static inline void mmu_clear_feature(unsigned long feature)
  245. {
  246. cur_cpu_spec->mmu_features &= ~feature;
  247. }
  248. #endif /* CONFIG_JUMP_LABEL */
  249. extern unsigned int __start___mmu_ftr_fixup, __stop___mmu_ftr_fixup;
  250. #ifdef CONFIG_PPC64
  251. /* This is our real memory area size on ppc64 server, on embedded, we
  252. * make it match the size our of bolted TLB area
  253. */
  254. extern u64 ppc64_rma_size;
  255. /* Cleanup function used by kexec */
  256. extern void mmu_cleanup_all(void);
  257. extern void radix__mmu_cleanup_all(void);
  258. /* Functions for creating and updating partition table on POWER9 */
  259. extern void mmu_partition_table_init(void);
  260. extern void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0,
  261. unsigned long dw1, bool flush);
  262. #endif /* CONFIG_PPC64 */
  263. struct mm_struct;
  264. #ifdef CONFIG_DEBUG_VM
  265. extern void assert_pte_locked(struct mm_struct *mm, unsigned long addr);
  266. #else /* CONFIG_DEBUG_VM */
  267. static inline void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
  268. {
  269. }
  270. #endif /* !CONFIG_DEBUG_VM */
  271. static __always_inline bool radix_enabled(void)
  272. {
  273. return mmu_has_feature(MMU_FTR_TYPE_RADIX);
  274. }
  275. static __always_inline bool early_radix_enabled(void)
  276. {
  277. return early_mmu_has_feature(MMU_FTR_TYPE_RADIX);
  278. }
  279. #ifdef CONFIG_STRICT_KERNEL_RWX
  280. static inline bool strict_kernel_rwx_enabled(void)
  281. {
  282. return rodata_enabled;
  283. }
  284. #else
  285. static inline bool strict_kernel_rwx_enabled(void)
  286. {
  287. return false;
  288. }
  289. #endif
  290. static inline bool strict_module_rwx_enabled(void)
  291. {
  292. return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
  293. }
  294. #endif /* !__ASSEMBLY__ */
  295. /* The kernel use the constants below to index in the page sizes array.
  296. * The use of fixed constants for this purpose is better for performances
  297. * of the low level hash refill handlers.
  298. *
  299. * A non supported page size has a "shift" field set to 0
  300. *
  301. * Any new page size being implemented can get a new entry in here. Whether
  302. * the kernel will use it or not is a different matter though. The actual page
  303. * size used by hugetlbfs is not defined here and may be made variable
  304. *
  305. * Note: This array ended up being a false good idea as it's growing to the
  306. * point where I wonder if we should replace it with something different,
  307. * to think about, feedback welcome. --BenH.
  308. */
  309. /* These are #defines as they have to be used in assembly */
  310. #define MMU_PAGE_4K 0
  311. #define MMU_PAGE_16K 1
  312. #define MMU_PAGE_64K 2
  313. #define MMU_PAGE_64K_AP 3 /* "Admixed pages" (hash64 only) */
  314. #define MMU_PAGE_256K 4
  315. #define MMU_PAGE_512K 5
  316. #define MMU_PAGE_1M 6
  317. #define MMU_PAGE_2M 7
  318. #define MMU_PAGE_4M 8
  319. #define MMU_PAGE_8M 9
  320. #define MMU_PAGE_16M 10
  321. #define MMU_PAGE_64M 11
  322. #define MMU_PAGE_256M 12
  323. #define MMU_PAGE_1G 13
  324. #define MMU_PAGE_16G 14
  325. #define MMU_PAGE_64G 15
  326. /*
  327. * N.B. we need to change the type of hpte_page_sizes if this gets to be > 16
  328. * Also we need to change he type of mm_context.low/high_slices_psize.
  329. */
  330. #define MMU_PAGE_COUNT 16
  331. #ifdef CONFIG_PPC_BOOK3S_64
  332. #include <asm/book3s/64/mmu.h>
  333. #else /* CONFIG_PPC_BOOK3S_64 */
  334. #ifndef __ASSEMBLY__
  335. /* MMU initialization */
  336. extern void early_init_mmu(void);
  337. extern void early_init_mmu_secondary(void);
  338. extern void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  339. phys_addr_t first_memblock_size);
  340. static inline void mmu_early_init_devtree(void) { }
  341. static inline void pkey_early_init_devtree(void) {}
  342. extern void *abatron_pteptrs[2];
  343. #endif /* __ASSEMBLY__ */
  344. #endif
  345. #if defined(CONFIG_PPC_BOOK3S_32)
  346. /* 32-bit classic hash table MMU */
  347. #include <asm/book3s/32/mmu-hash.h>
  348. #elif defined(CONFIG_PPC_MMU_NOHASH)
  349. #include <asm/nohash/mmu.h>
  350. #endif
  351. #endif /* __KERNEL__ */
  352. #endif /* _ASM_POWERPC_MMU_H_ */