mmu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains the routines for handling the MMU on those
  4. * PowerPC implementations where the MMU substantially follows the
  5. * architecture specification. This includes the 6xx, 7xx, 7xxx,
  6. * and 8260 implementations but excludes the 8xx and 4xx.
  7. * -- paulus
  8. *
  9. * Derived from arch/ppc/mm/init.c:
  10. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  11. *
  12. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  13. * and Cort Dougan (PReP) ([email protected])
  14. * Copyright (C) 1996 Paul Mackerras
  15. *
  16. * Derived from "arch/i386/mm/init.c"
  17. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/highmem.h>
  23. #include <linux/memblock.h>
  24. #include <asm/mmu.h>
  25. #include <asm/machdep.h>
  26. #include <asm/code-patching.h>
  27. #include <asm/sections.h>
  28. #include <mm/mmu_decl.h>
  29. u8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0};
  30. static struct hash_pte __initdata *Hash = (struct hash_pte *)early_hash;
  31. static unsigned long __initdata Hash_size, Hash_mask;
  32. static unsigned int __initdata hash_mb, hash_mb2;
  33. unsigned long __initdata _SDR1;
  34. struct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */
  35. static struct batrange { /* stores address ranges mapped by BATs */
  36. unsigned long start;
  37. unsigned long limit;
  38. phys_addr_t phys;
  39. } bat_addrs[8];
  40. #ifdef CONFIG_SMP
  41. unsigned long mmu_hash_lock;
  42. #endif
  43. /*
  44. * Return PA for this VA if it is mapped by a BAT, or 0
  45. */
  46. phys_addr_t v_block_mapped(unsigned long va)
  47. {
  48. int b;
  49. for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
  50. if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
  51. return bat_addrs[b].phys + (va - bat_addrs[b].start);
  52. return 0;
  53. }
  54. /*
  55. * Return VA for a given PA or 0 if not mapped
  56. */
  57. unsigned long p_block_mapped(phys_addr_t pa)
  58. {
  59. int b;
  60. for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
  61. if (pa >= bat_addrs[b].phys
  62. && pa < (bat_addrs[b].limit-bat_addrs[b].start)
  63. +bat_addrs[b].phys)
  64. return bat_addrs[b].start+(pa-bat_addrs[b].phys);
  65. return 0;
  66. }
  67. int __init find_free_bat(void)
  68. {
  69. int b;
  70. int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
  71. for (b = 0; b < n; b++) {
  72. struct ppc_bat *bat = BATS[b];
  73. if (!(bat[1].batu & 3))
  74. return b;
  75. }
  76. return -1;
  77. }
  78. /*
  79. * This function calculates the size of the larger block usable to map the
  80. * beginning of an area based on the start address and size of that area:
  81. * - max block size is 256 on 6xx.
  82. * - base address must be aligned to the block size. So the maximum block size
  83. * is identified by the lowest bit set to 1 in the base address (for instance
  84. * if base is 0x16000000, max size is 0x02000000).
  85. * - block size has to be a power of two. This is calculated by finding the
  86. * highest bit set to 1.
  87. */
  88. unsigned int bat_block_size(unsigned long base, unsigned long top)
  89. {
  90. unsigned int max_size = SZ_256M;
  91. unsigned int base_shift = (ffs(base) - 1) & 31;
  92. unsigned int block_shift = (fls(top - base) - 1) & 31;
  93. return min3(max_size, 1U << base_shift, 1U << block_shift);
  94. }
  95. /*
  96. * Set up one of the IBAT (block address translation) register pairs.
  97. * The parameters are not checked; in particular size must be a power
  98. * of 2 between 128k and 256M.
  99. */
  100. static void setibat(int index, unsigned long virt, phys_addr_t phys,
  101. unsigned int size, pgprot_t prot)
  102. {
  103. unsigned int bl = (size >> 17) - 1;
  104. int wimgxpp;
  105. struct ppc_bat *bat = BATS[index];
  106. unsigned long flags = pgprot_val(prot);
  107. if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
  108. flags &= ~_PAGE_COHERENT;
  109. wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
  110. bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
  111. bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
  112. if (flags & _PAGE_USER)
  113. bat[0].batu |= 1; /* Vp = 1 */
  114. }
  115. static void clearibat(int index)
  116. {
  117. struct ppc_bat *bat = BATS[index];
  118. bat[0].batu = 0;
  119. bat[0].batl = 0;
  120. }
  121. static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
  122. {
  123. int idx;
  124. while ((idx = find_free_bat()) != -1 && base != top) {
  125. unsigned int size = bat_block_size(base, top);
  126. if (size < 128 << 10)
  127. break;
  128. setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
  129. base += size;
  130. }
  131. return base;
  132. }
  133. unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
  134. {
  135. unsigned long done;
  136. unsigned long border = (unsigned long)__srwx_boundary - PAGE_OFFSET;
  137. unsigned long size;
  138. size = roundup_pow_of_two((unsigned long)_einittext - PAGE_OFFSET);
  139. setibat(0, PAGE_OFFSET, 0, size, PAGE_KERNEL_X);
  140. if (debug_pagealloc_enabled_or_kfence()) {
  141. pr_debug_once("Read-Write memory mapped without BATs\n");
  142. if (base >= border)
  143. return base;
  144. if (top >= border)
  145. top = border;
  146. }
  147. if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
  148. return __mmu_mapin_ram(base, top);
  149. done = __mmu_mapin_ram(base, border);
  150. if (done != border)
  151. return done;
  152. return __mmu_mapin_ram(border, top);
  153. }
  154. static bool is_module_segment(unsigned long addr)
  155. {
  156. if (!IS_ENABLED(CONFIG_MODULES))
  157. return false;
  158. if (addr < ALIGN_DOWN(MODULES_VADDR, SZ_256M))
  159. return false;
  160. if (addr > ALIGN(MODULES_END, SZ_256M) - 1)
  161. return false;
  162. return true;
  163. }
  164. void mmu_mark_initmem_nx(void)
  165. {
  166. int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
  167. int i;
  168. unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
  169. unsigned long top = ALIGN((unsigned long)_etext - PAGE_OFFSET, SZ_128K);
  170. unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
  171. unsigned long size;
  172. for (i = 0; i < nb - 1 && base < top;) {
  173. size = bat_block_size(base, top);
  174. setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
  175. base += size;
  176. }
  177. if (base < top) {
  178. size = bat_block_size(base, top);
  179. if ((top - base) > size) {
  180. size <<= 1;
  181. if (strict_kernel_rwx_enabled() && base + size > border)
  182. pr_warn("Some RW data is getting mapped X. "
  183. "Adjust CONFIG_DATA_SHIFT to avoid that.\n");
  184. }
  185. setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
  186. base += size;
  187. }
  188. for (; i < nb; i++)
  189. clearibat(i);
  190. update_bats();
  191. for (i = TASK_SIZE >> 28; i < 16; i++) {
  192. /* Do not set NX on VM space for modules */
  193. if (is_module_segment(i << 28))
  194. continue;
  195. mtsr(mfsr(i << 28) | 0x10000000, i << 28);
  196. }
  197. }
  198. void mmu_mark_rodata_ro(void)
  199. {
  200. int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
  201. int i;
  202. for (i = 0; i < nb; i++) {
  203. struct ppc_bat *bat = BATS[i];
  204. if (bat_addrs[i].start < (unsigned long)__end_rodata)
  205. bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
  206. }
  207. update_bats();
  208. }
  209. /*
  210. * Set up one of the D BAT (block address translation) register pairs.
  211. * The parameters are not checked; in particular size must be a power
  212. * of 2 between 128k and 256M.
  213. */
  214. void __init setbat(int index, unsigned long virt, phys_addr_t phys,
  215. unsigned int size, pgprot_t prot)
  216. {
  217. unsigned int bl;
  218. int wimgxpp;
  219. struct ppc_bat *bat;
  220. unsigned long flags = pgprot_val(prot);
  221. if (index == -1)
  222. index = find_free_bat();
  223. if (index == -1) {
  224. pr_err("%s: no BAT available for mapping 0x%llx\n", __func__,
  225. (unsigned long long)phys);
  226. return;
  227. }
  228. bat = BATS[index];
  229. if ((flags & _PAGE_NO_CACHE) ||
  230. (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
  231. flags &= ~_PAGE_COHERENT;
  232. bl = (size >> 17) - 1;
  233. /* Do DBAT first */
  234. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  235. | _PAGE_COHERENT | _PAGE_GUARDED);
  236. wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
  237. bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
  238. bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
  239. if (flags & _PAGE_USER)
  240. bat[1].batu |= 1; /* Vp = 1 */
  241. if (flags & _PAGE_GUARDED) {
  242. /* G bit must be zero in IBATs */
  243. flags &= ~_PAGE_EXEC;
  244. }
  245. bat_addrs[index].start = virt;
  246. bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
  247. bat_addrs[index].phys = phys;
  248. }
  249. /*
  250. * Preload a translation in the hash table
  251. */
  252. static void hash_preload(struct mm_struct *mm, unsigned long ea)
  253. {
  254. pmd_t *pmd;
  255. if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
  256. return;
  257. pmd = pmd_off(mm, ea);
  258. if (!pmd_none(*pmd))
  259. add_hash_page(mm->context.id, ea, pmd_val(*pmd));
  260. }
  261. /*
  262. * This is called at the end of handling a user page fault, when the
  263. * fault has been handled by updating a PTE in the linux page tables.
  264. * We use it to preload an HPTE into the hash table corresponding to
  265. * the updated linux PTE.
  266. *
  267. * This must always be called with the pte lock held.
  268. */
  269. void __update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
  270. pte_t *ptep)
  271. {
  272. /*
  273. * We don't need to worry about _PAGE_PRESENT here because we are
  274. * called with either mm->page_table_lock held or ptl lock held
  275. */
  276. /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
  277. if (!pte_young(*ptep) || address >= TASK_SIZE)
  278. return;
  279. /* We have to test for regs NULL since init will get here first thing at boot */
  280. if (!current->thread.regs)
  281. return;
  282. /* We also avoid filling the hash if not coming from a fault */
  283. if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
  284. return;
  285. hash_preload(vma->vm_mm, address);
  286. }
  287. /*
  288. * Initialize the hash table and patch the instructions in hashtable.S.
  289. */
  290. void __init MMU_init_hw(void)
  291. {
  292. unsigned int n_hpteg, lg_n_hpteg;
  293. if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
  294. return;
  295. if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
  296. #define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
  297. #define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
  298. #define MIN_N_HPTEG 1024 /* min 64kB hash table */
  299. /*
  300. * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
  301. * This is less than the recommended amount, but then
  302. * Linux ain't AIX.
  303. */
  304. n_hpteg = total_memory / (PAGE_SIZE * 8);
  305. if (n_hpteg < MIN_N_HPTEG)
  306. n_hpteg = MIN_N_HPTEG;
  307. lg_n_hpteg = __ilog2(n_hpteg);
  308. if (n_hpteg & (n_hpteg - 1)) {
  309. ++lg_n_hpteg; /* round up if not power of 2 */
  310. n_hpteg = 1 << lg_n_hpteg;
  311. }
  312. Hash_size = n_hpteg << LG_HPTEG_SIZE;
  313. /*
  314. * Find some memory for the hash table.
  315. */
  316. if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
  317. Hash = memblock_alloc(Hash_size, Hash_size);
  318. if (!Hash)
  319. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  320. __func__, Hash_size, Hash_size);
  321. _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
  322. pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
  323. (unsigned long long)(total_memory >> 20), Hash_size >> 10);
  324. Hash_mask = n_hpteg - 1;
  325. hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
  326. if (lg_n_hpteg > 16)
  327. hash_mb2 = 16 - LG_HPTEG_SIZE;
  328. }
  329. void __init MMU_init_hw_patch(void)
  330. {
  331. unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
  332. unsigned int hash = (unsigned int)Hash - PAGE_OFFSET;
  333. if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
  334. return;
  335. if (ppc_md.progress)
  336. ppc_md.progress("hash:patch", 0x345);
  337. if (ppc_md.progress)
  338. ppc_md.progress("hash:done", 0x205);
  339. /* WARNING: Make sure nothing can trigger a KASAN check past this point */
  340. /*
  341. * Patch up the instructions in hashtable.S:create_hpte
  342. */
  343. modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16);
  344. modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
  345. modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
  346. modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
  347. modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
  348. /*
  349. * Patch up the instructions in hashtable.S:flush_hash_page
  350. */
  351. modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16);
  352. modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
  353. modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
  354. modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
  355. }
  356. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  357. phys_addr_t first_memblock_size)
  358. {
  359. /* We don't currently support the first MEMBLOCK not mapping 0
  360. * physical on those processors
  361. */
  362. BUG_ON(first_memblock_base != 0);
  363. memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_256M));
  364. }
  365. void __init print_system_hash_info(void)
  366. {
  367. pr_info("Hash_size = 0x%lx\n", Hash_size);
  368. if (Hash_mask)
  369. pr_info("Hash_mask = 0x%lx\n", Hash_mask);
  370. }
  371. void __init early_init_mmu(void)
  372. {
  373. }