radix_pgtable.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Page table handling routines for radix page table.
  4. *
  5. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  6. */
  7. #define pr_fmt(fmt) "radix-mmu: " fmt
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/memblock.h>
  12. #include <linux/of.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/mm.h>
  15. #include <linux/hugetlb.h>
  16. #include <linux/string_helpers.h>
  17. #include <linux/memory.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/dma.h>
  21. #include <asm/machdep.h>
  22. #include <asm/mmu.h>
  23. #include <asm/firmware.h>
  24. #include <asm/powernv.h>
  25. #include <asm/sections.h>
  26. #include <asm/smp.h>
  27. #include <asm/trace.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/ultravisor.h>
  30. #include <asm/set_memory.h>
  31. #include <trace/events/thp.h>
  32. #include <mm/mmu_decl.h>
  33. unsigned int mmu_base_pid;
  34. unsigned long radix_mem_block_size __ro_after_init;
  35. static __ref void *early_alloc_pgtable(unsigned long size, int nid,
  36. unsigned long region_start, unsigned long region_end)
  37. {
  38. phys_addr_t min_addr = MEMBLOCK_LOW_LIMIT;
  39. phys_addr_t max_addr = MEMBLOCK_ALLOC_ANYWHERE;
  40. void *ptr;
  41. if (region_start)
  42. min_addr = region_start;
  43. if (region_end)
  44. max_addr = region_end;
  45. ptr = memblock_alloc_try_nid(size, size, min_addr, max_addr, nid);
  46. if (!ptr)
  47. panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa max_addr=%pa\n",
  48. __func__, size, size, nid, &min_addr, &max_addr);
  49. return ptr;
  50. }
  51. /*
  52. * When allocating pud or pmd pointers, we allocate a complete page
  53. * of PAGE_SIZE rather than PUD_TABLE_SIZE or PMD_TABLE_SIZE. This
  54. * is to ensure that the page obtained from the memblock allocator
  55. * can be completely used as page table page and can be freed
  56. * correctly when the page table entries are removed.
  57. */
  58. static int early_map_kernel_page(unsigned long ea, unsigned long pa,
  59. pgprot_t flags,
  60. unsigned int map_page_size,
  61. int nid,
  62. unsigned long region_start, unsigned long region_end)
  63. {
  64. unsigned long pfn = pa >> PAGE_SHIFT;
  65. pgd_t *pgdp;
  66. p4d_t *p4dp;
  67. pud_t *pudp;
  68. pmd_t *pmdp;
  69. pte_t *ptep;
  70. pgdp = pgd_offset_k(ea);
  71. p4dp = p4d_offset(pgdp, ea);
  72. if (p4d_none(*p4dp)) {
  73. pudp = early_alloc_pgtable(PAGE_SIZE, nid,
  74. region_start, region_end);
  75. p4d_populate(&init_mm, p4dp, pudp);
  76. }
  77. pudp = pud_offset(p4dp, ea);
  78. if (map_page_size == PUD_SIZE) {
  79. ptep = (pte_t *)pudp;
  80. goto set_the_pte;
  81. }
  82. if (pud_none(*pudp)) {
  83. pmdp = early_alloc_pgtable(PAGE_SIZE, nid, region_start,
  84. region_end);
  85. pud_populate(&init_mm, pudp, pmdp);
  86. }
  87. pmdp = pmd_offset(pudp, ea);
  88. if (map_page_size == PMD_SIZE) {
  89. ptep = pmdp_ptep(pmdp);
  90. goto set_the_pte;
  91. }
  92. if (!pmd_present(*pmdp)) {
  93. ptep = early_alloc_pgtable(PAGE_SIZE, nid,
  94. region_start, region_end);
  95. pmd_populate_kernel(&init_mm, pmdp, ptep);
  96. }
  97. ptep = pte_offset_kernel(pmdp, ea);
  98. set_the_pte:
  99. set_pte_at(&init_mm, ea, ptep, pfn_pte(pfn, flags));
  100. asm volatile("ptesync": : :"memory");
  101. return 0;
  102. }
  103. /*
  104. * nid, region_start, and region_end are hints to try to place the page
  105. * table memory in the same node or region.
  106. */
  107. static int __map_kernel_page(unsigned long ea, unsigned long pa,
  108. pgprot_t flags,
  109. unsigned int map_page_size,
  110. int nid,
  111. unsigned long region_start, unsigned long region_end)
  112. {
  113. unsigned long pfn = pa >> PAGE_SHIFT;
  114. pgd_t *pgdp;
  115. p4d_t *p4dp;
  116. pud_t *pudp;
  117. pmd_t *pmdp;
  118. pte_t *ptep;
  119. /*
  120. * Make sure task size is correct as per the max adddr
  121. */
  122. BUILD_BUG_ON(TASK_SIZE_USER64 > RADIX_PGTABLE_RANGE);
  123. #ifdef CONFIG_PPC_64K_PAGES
  124. BUILD_BUG_ON(RADIX_KERN_MAP_SIZE != (1UL << MAX_EA_BITS_PER_CONTEXT));
  125. #endif
  126. if (unlikely(!slab_is_available()))
  127. return early_map_kernel_page(ea, pa, flags, map_page_size,
  128. nid, region_start, region_end);
  129. /*
  130. * Should make page table allocation functions be able to take a
  131. * node, so we can place kernel page tables on the right nodes after
  132. * boot.
  133. */
  134. pgdp = pgd_offset_k(ea);
  135. p4dp = p4d_offset(pgdp, ea);
  136. pudp = pud_alloc(&init_mm, p4dp, ea);
  137. if (!pudp)
  138. return -ENOMEM;
  139. if (map_page_size == PUD_SIZE) {
  140. ptep = (pte_t *)pudp;
  141. goto set_the_pte;
  142. }
  143. pmdp = pmd_alloc(&init_mm, pudp, ea);
  144. if (!pmdp)
  145. return -ENOMEM;
  146. if (map_page_size == PMD_SIZE) {
  147. ptep = pmdp_ptep(pmdp);
  148. goto set_the_pte;
  149. }
  150. ptep = pte_alloc_kernel(pmdp, ea);
  151. if (!ptep)
  152. return -ENOMEM;
  153. set_the_pte:
  154. set_pte_at(&init_mm, ea, ptep, pfn_pte(pfn, flags));
  155. asm volatile("ptesync": : :"memory");
  156. return 0;
  157. }
  158. int radix__map_kernel_page(unsigned long ea, unsigned long pa,
  159. pgprot_t flags,
  160. unsigned int map_page_size)
  161. {
  162. return __map_kernel_page(ea, pa, flags, map_page_size, -1, 0, 0);
  163. }
  164. #ifdef CONFIG_STRICT_KERNEL_RWX
  165. static void radix__change_memory_range(unsigned long start, unsigned long end,
  166. unsigned long clear)
  167. {
  168. unsigned long idx;
  169. pgd_t *pgdp;
  170. p4d_t *p4dp;
  171. pud_t *pudp;
  172. pmd_t *pmdp;
  173. pte_t *ptep;
  174. start = ALIGN_DOWN(start, PAGE_SIZE);
  175. end = PAGE_ALIGN(end); // aligns up
  176. pr_debug("Changing flags on range %lx-%lx removing 0x%lx\n",
  177. start, end, clear);
  178. for (idx = start; idx < end; idx += PAGE_SIZE) {
  179. pgdp = pgd_offset_k(idx);
  180. p4dp = p4d_offset(pgdp, idx);
  181. pudp = pud_alloc(&init_mm, p4dp, idx);
  182. if (!pudp)
  183. continue;
  184. if (pud_is_leaf(*pudp)) {
  185. ptep = (pte_t *)pudp;
  186. goto update_the_pte;
  187. }
  188. pmdp = pmd_alloc(&init_mm, pudp, idx);
  189. if (!pmdp)
  190. continue;
  191. if (pmd_is_leaf(*pmdp)) {
  192. ptep = pmdp_ptep(pmdp);
  193. goto update_the_pte;
  194. }
  195. ptep = pte_alloc_kernel(pmdp, idx);
  196. if (!ptep)
  197. continue;
  198. update_the_pte:
  199. radix__pte_update(&init_mm, idx, ptep, clear, 0, 0);
  200. }
  201. radix__flush_tlb_kernel_range(start, end);
  202. }
  203. void radix__mark_rodata_ro(void)
  204. {
  205. unsigned long start, end;
  206. start = (unsigned long)_stext;
  207. end = (unsigned long)__end_rodata;
  208. radix__change_memory_range(start, end, _PAGE_WRITE);
  209. for (start = PAGE_OFFSET; start < (unsigned long)_stext; start += PAGE_SIZE) {
  210. end = start + PAGE_SIZE;
  211. if (overlaps_interrupt_vector_text(start, end))
  212. radix__change_memory_range(start, end, _PAGE_WRITE);
  213. else
  214. break;
  215. }
  216. }
  217. void radix__mark_initmem_nx(void)
  218. {
  219. unsigned long start = (unsigned long)__init_begin;
  220. unsigned long end = (unsigned long)__init_end;
  221. radix__change_memory_range(start, end, _PAGE_EXEC);
  222. }
  223. #endif /* CONFIG_STRICT_KERNEL_RWX */
  224. static inline void __meminit
  225. print_mapping(unsigned long start, unsigned long end, unsigned long size, bool exec)
  226. {
  227. char buf[10];
  228. if (end <= start)
  229. return;
  230. string_get_size(size, 1, STRING_UNITS_2, buf, sizeof(buf));
  231. pr_info("Mapped 0x%016lx-0x%016lx with %s pages%s\n", start, end, buf,
  232. exec ? " (exec)" : "");
  233. }
  234. static unsigned long next_boundary(unsigned long addr, unsigned long end)
  235. {
  236. #ifdef CONFIG_STRICT_KERNEL_RWX
  237. unsigned long stext_phys;
  238. stext_phys = __pa_symbol(_stext);
  239. // Relocatable kernel running at non-zero real address
  240. if (stext_phys != 0) {
  241. // The end of interrupts code at zero is a rodata boundary
  242. unsigned long end_intr = __pa_symbol(__end_interrupts) - stext_phys;
  243. if (addr < end_intr)
  244. return end_intr;
  245. // Start of relocated kernel text is a rodata boundary
  246. if (addr < stext_phys)
  247. return stext_phys;
  248. }
  249. if (addr < __pa_symbol(__srwx_boundary))
  250. return __pa_symbol(__srwx_boundary);
  251. #endif
  252. return end;
  253. }
  254. static int __meminit create_physical_mapping(unsigned long start,
  255. unsigned long end,
  256. int nid, pgprot_t _prot)
  257. {
  258. unsigned long vaddr, addr, mapping_size = 0;
  259. bool prev_exec, exec = false;
  260. pgprot_t prot;
  261. int psize;
  262. unsigned long max_mapping_size = radix_mem_block_size;
  263. if (debug_pagealloc_enabled_or_kfence())
  264. max_mapping_size = PAGE_SIZE;
  265. start = ALIGN(start, PAGE_SIZE);
  266. end = ALIGN_DOWN(end, PAGE_SIZE);
  267. for (addr = start; addr < end; addr += mapping_size) {
  268. unsigned long gap, previous_size;
  269. int rc;
  270. gap = next_boundary(addr, end) - addr;
  271. if (gap > max_mapping_size)
  272. gap = max_mapping_size;
  273. previous_size = mapping_size;
  274. prev_exec = exec;
  275. if (IS_ALIGNED(addr, PUD_SIZE) && gap >= PUD_SIZE &&
  276. mmu_psize_defs[MMU_PAGE_1G].shift) {
  277. mapping_size = PUD_SIZE;
  278. psize = MMU_PAGE_1G;
  279. } else if (IS_ALIGNED(addr, PMD_SIZE) && gap >= PMD_SIZE &&
  280. mmu_psize_defs[MMU_PAGE_2M].shift) {
  281. mapping_size = PMD_SIZE;
  282. psize = MMU_PAGE_2M;
  283. } else {
  284. mapping_size = PAGE_SIZE;
  285. psize = mmu_virtual_psize;
  286. }
  287. vaddr = (unsigned long)__va(addr);
  288. if (overlaps_kernel_text(vaddr, vaddr + mapping_size) ||
  289. overlaps_interrupt_vector_text(vaddr, vaddr + mapping_size)) {
  290. prot = PAGE_KERNEL_X;
  291. exec = true;
  292. } else {
  293. prot = _prot;
  294. exec = false;
  295. }
  296. if (mapping_size != previous_size || exec != prev_exec) {
  297. print_mapping(start, addr, previous_size, prev_exec);
  298. start = addr;
  299. }
  300. rc = __map_kernel_page(vaddr, addr, prot, mapping_size, nid, start, end);
  301. if (rc)
  302. return rc;
  303. update_page_count(psize, 1);
  304. }
  305. print_mapping(start, addr, mapping_size, exec);
  306. return 0;
  307. }
  308. static void __init radix_init_pgtable(void)
  309. {
  310. unsigned long rts_field;
  311. phys_addr_t start, end;
  312. u64 i;
  313. /* We don't support slb for radix */
  314. slb_set_size(0);
  315. /*
  316. * Create the linear mapping
  317. */
  318. for_each_mem_range(i, &start, &end) {
  319. /*
  320. * The memblock allocator is up at this point, so the
  321. * page tables will be allocated within the range. No
  322. * need or a node (which we don't have yet).
  323. */
  324. if (end >= RADIX_VMALLOC_START) {
  325. pr_warn("Outside the supported range\n");
  326. continue;
  327. }
  328. WARN_ON(create_physical_mapping(start, end,
  329. -1, PAGE_KERNEL));
  330. }
  331. if (!cpu_has_feature(CPU_FTR_HVMODE) &&
  332. cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) {
  333. /*
  334. * Older versions of KVM on these machines prefer if the
  335. * guest only uses the low 19 PID bits.
  336. */
  337. mmu_pid_bits = 19;
  338. }
  339. mmu_base_pid = 1;
  340. /*
  341. * Allocate Partition table and process table for the
  342. * host.
  343. */
  344. BUG_ON(PRTB_SIZE_SHIFT > 36);
  345. process_tb = early_alloc_pgtable(1UL << PRTB_SIZE_SHIFT, -1, 0, 0);
  346. /*
  347. * Fill in the process table.
  348. */
  349. rts_field = radix__get_tree_size();
  350. process_tb->prtb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE);
  351. /*
  352. * The init_mm context is given the first available (non-zero) PID,
  353. * which is the "guard PID" and contains no page table. PIDR should
  354. * never be set to zero because that duplicates the kernel address
  355. * space at the 0x0... offset (quadrant 0)!
  356. *
  357. * An arbitrary PID that may later be allocated by the PID allocator
  358. * for userspace processes must not be used either, because that
  359. * would cause stale user mappings for that PID on CPUs outside of
  360. * the TLB invalidation scheme (because it won't be in mm_cpumask).
  361. *
  362. * So permanently carve out one PID for the purpose of a guard PID.
  363. */
  364. init_mm.context.id = mmu_base_pid;
  365. mmu_base_pid++;
  366. }
  367. static void __init radix_init_partition_table(void)
  368. {
  369. unsigned long rts_field, dw0, dw1;
  370. mmu_partition_table_init();
  371. rts_field = radix__get_tree_size();
  372. dw0 = rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE | PATB_HR;
  373. dw1 = __pa(process_tb) | (PRTB_SIZE_SHIFT - 12) | PATB_GR;
  374. mmu_partition_table_set_entry(0, dw0, dw1, false);
  375. pr_info("Initializing Radix MMU\n");
  376. }
  377. static int __init get_idx_from_shift(unsigned int shift)
  378. {
  379. int idx = -1;
  380. switch (shift) {
  381. case 0xc:
  382. idx = MMU_PAGE_4K;
  383. break;
  384. case 0x10:
  385. idx = MMU_PAGE_64K;
  386. break;
  387. case 0x15:
  388. idx = MMU_PAGE_2M;
  389. break;
  390. case 0x1e:
  391. idx = MMU_PAGE_1G;
  392. break;
  393. }
  394. return idx;
  395. }
  396. static int __init radix_dt_scan_page_sizes(unsigned long node,
  397. const char *uname, int depth,
  398. void *data)
  399. {
  400. int size = 0;
  401. int shift, idx;
  402. unsigned int ap;
  403. const __be32 *prop;
  404. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  405. /* We are scanning "cpu" nodes only */
  406. if (type == NULL || strcmp(type, "cpu") != 0)
  407. return 0;
  408. /* Grab page size encodings */
  409. prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
  410. if (!prop)
  411. return 0;
  412. pr_info("Page sizes from device-tree:\n");
  413. for (; size >= 4; size -= 4, ++prop) {
  414. struct mmu_psize_def *def;
  415. /* top 3 bit is AP encoding */
  416. shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
  417. ap = be32_to_cpu(prop[0]) >> 29;
  418. pr_info("Page size shift = %d AP=0x%x\n", shift, ap);
  419. idx = get_idx_from_shift(shift);
  420. if (idx < 0)
  421. continue;
  422. def = &mmu_psize_defs[idx];
  423. def->shift = shift;
  424. def->ap = ap;
  425. def->h_rpt_pgsize = psize_to_rpti_pgsize(idx);
  426. }
  427. /* needed ? */
  428. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  429. return 1;
  430. }
  431. #ifdef CONFIG_MEMORY_HOTPLUG
  432. static int __init probe_memory_block_size(unsigned long node, const char *uname, int
  433. depth, void *data)
  434. {
  435. unsigned long *mem_block_size = (unsigned long *)data;
  436. const __be32 *prop;
  437. int len;
  438. if (depth != 1)
  439. return 0;
  440. if (strcmp(uname, "ibm,dynamic-reconfiguration-memory"))
  441. return 0;
  442. prop = of_get_flat_dt_prop(node, "ibm,lmb-size", &len);
  443. if (!prop || len < dt_root_size_cells * sizeof(__be32))
  444. /*
  445. * Nothing in the device tree
  446. */
  447. *mem_block_size = MIN_MEMORY_BLOCK_SIZE;
  448. else
  449. *mem_block_size = of_read_number(prop, dt_root_size_cells);
  450. return 1;
  451. }
  452. static unsigned long __init radix_memory_block_size(void)
  453. {
  454. unsigned long mem_block_size = MIN_MEMORY_BLOCK_SIZE;
  455. /*
  456. * OPAL firmware feature is set by now. Hence we are ok
  457. * to test OPAL feature.
  458. */
  459. if (firmware_has_feature(FW_FEATURE_OPAL))
  460. mem_block_size = 1UL * 1024 * 1024 * 1024;
  461. else
  462. of_scan_flat_dt(probe_memory_block_size, &mem_block_size);
  463. return mem_block_size;
  464. }
  465. #else /* CONFIG_MEMORY_HOTPLUG */
  466. static unsigned long __init radix_memory_block_size(void)
  467. {
  468. return 1UL * 1024 * 1024 * 1024;
  469. }
  470. #endif /* CONFIG_MEMORY_HOTPLUG */
  471. void __init radix__early_init_devtree(void)
  472. {
  473. int rc;
  474. /*
  475. * Try to find the available page sizes in the device-tree
  476. */
  477. rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
  478. if (!rc) {
  479. /*
  480. * No page size details found in device tree.
  481. * Let's assume we have page 4k and 64k support
  482. */
  483. mmu_psize_defs[MMU_PAGE_4K].shift = 12;
  484. mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
  485. mmu_psize_defs[MMU_PAGE_4K].h_rpt_pgsize =
  486. psize_to_rpti_pgsize(MMU_PAGE_4K);
  487. mmu_psize_defs[MMU_PAGE_64K].shift = 16;
  488. mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
  489. mmu_psize_defs[MMU_PAGE_64K].h_rpt_pgsize =
  490. psize_to_rpti_pgsize(MMU_PAGE_64K);
  491. }
  492. /*
  493. * Max mapping size used when mapping pages. We don't use
  494. * ppc_md.memory_block_size() here because this get called
  495. * early and we don't have machine probe called yet. Also
  496. * the pseries implementation only check for ibm,lmb-size.
  497. * All hypervisor supporting radix do expose that device
  498. * tree node.
  499. */
  500. radix_mem_block_size = radix_memory_block_size();
  501. return;
  502. }
  503. void __init radix__early_init_mmu(void)
  504. {
  505. unsigned long lpcr;
  506. #ifdef CONFIG_PPC_64S_HASH_MMU
  507. #ifdef CONFIG_PPC_64K_PAGES
  508. /* PAGE_SIZE mappings */
  509. mmu_virtual_psize = MMU_PAGE_64K;
  510. #else
  511. mmu_virtual_psize = MMU_PAGE_4K;
  512. #endif
  513. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  514. /* vmemmap mapping */
  515. if (mmu_psize_defs[MMU_PAGE_2M].shift) {
  516. /*
  517. * map vmemmap using 2M if available
  518. */
  519. mmu_vmemmap_psize = MMU_PAGE_2M;
  520. } else
  521. mmu_vmemmap_psize = mmu_virtual_psize;
  522. #endif
  523. #endif
  524. /*
  525. * initialize page table size
  526. */
  527. __pte_index_size = RADIX_PTE_INDEX_SIZE;
  528. __pmd_index_size = RADIX_PMD_INDEX_SIZE;
  529. __pud_index_size = RADIX_PUD_INDEX_SIZE;
  530. __pgd_index_size = RADIX_PGD_INDEX_SIZE;
  531. __pud_cache_index = RADIX_PUD_INDEX_SIZE;
  532. __pte_table_size = RADIX_PTE_TABLE_SIZE;
  533. __pmd_table_size = RADIX_PMD_TABLE_SIZE;
  534. __pud_table_size = RADIX_PUD_TABLE_SIZE;
  535. __pgd_table_size = RADIX_PGD_TABLE_SIZE;
  536. __pmd_val_bits = RADIX_PMD_VAL_BITS;
  537. __pud_val_bits = RADIX_PUD_VAL_BITS;
  538. __pgd_val_bits = RADIX_PGD_VAL_BITS;
  539. __kernel_virt_start = RADIX_KERN_VIRT_START;
  540. __vmalloc_start = RADIX_VMALLOC_START;
  541. __vmalloc_end = RADIX_VMALLOC_END;
  542. __kernel_io_start = RADIX_KERN_IO_START;
  543. __kernel_io_end = RADIX_KERN_IO_END;
  544. vmemmap = (struct page *)RADIX_VMEMMAP_START;
  545. ioremap_bot = IOREMAP_BASE;
  546. #ifdef CONFIG_PCI
  547. pci_io_base = ISA_IO_BASE;
  548. #endif
  549. __pte_frag_nr = RADIX_PTE_FRAG_NR;
  550. __pte_frag_size_shift = RADIX_PTE_FRAG_SIZE_SHIFT;
  551. __pmd_frag_nr = RADIX_PMD_FRAG_NR;
  552. __pmd_frag_size_shift = RADIX_PMD_FRAG_SIZE_SHIFT;
  553. radix_init_pgtable();
  554. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  555. lpcr = mfspr(SPRN_LPCR);
  556. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  557. radix_init_partition_table();
  558. } else {
  559. radix_init_pseries();
  560. }
  561. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  562. /* Switch to the guard PID before turning on MMU */
  563. radix__switch_mmu_context(NULL, &init_mm);
  564. tlbiel_all();
  565. }
  566. void radix__early_init_mmu_secondary(void)
  567. {
  568. unsigned long lpcr;
  569. /*
  570. * update partition table control register and UPRT
  571. */
  572. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  573. lpcr = mfspr(SPRN_LPCR);
  574. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  575. set_ptcr_when_no_uv(__pa(partition_tb) |
  576. (PATB_SIZE_SHIFT - 12));
  577. }
  578. radix__switch_mmu_context(NULL, &init_mm);
  579. tlbiel_all();
  580. /* Make sure userspace can't change the AMR */
  581. mtspr(SPRN_UAMOR, 0);
  582. }
  583. /* Called during kexec sequence with MMU off */
  584. notrace void radix__mmu_cleanup_all(void)
  585. {
  586. unsigned long lpcr;
  587. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  588. lpcr = mfspr(SPRN_LPCR);
  589. mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
  590. set_ptcr_when_no_uv(0);
  591. powernv_set_nmmu_ptcr(0);
  592. radix__flush_tlb_all();
  593. }
  594. }
  595. #ifdef CONFIG_MEMORY_HOTPLUG
  596. static void free_pte_table(pte_t *pte_start, pmd_t *pmd)
  597. {
  598. pte_t *pte;
  599. int i;
  600. for (i = 0; i < PTRS_PER_PTE; i++) {
  601. pte = pte_start + i;
  602. if (!pte_none(*pte))
  603. return;
  604. }
  605. pte_free_kernel(&init_mm, pte_start);
  606. pmd_clear(pmd);
  607. }
  608. static void free_pmd_table(pmd_t *pmd_start, pud_t *pud)
  609. {
  610. pmd_t *pmd;
  611. int i;
  612. for (i = 0; i < PTRS_PER_PMD; i++) {
  613. pmd = pmd_start + i;
  614. if (!pmd_none(*pmd))
  615. return;
  616. }
  617. pmd_free(&init_mm, pmd_start);
  618. pud_clear(pud);
  619. }
  620. static void free_pud_table(pud_t *pud_start, p4d_t *p4d)
  621. {
  622. pud_t *pud;
  623. int i;
  624. for (i = 0; i < PTRS_PER_PUD; i++) {
  625. pud = pud_start + i;
  626. if (!pud_none(*pud))
  627. return;
  628. }
  629. pud_free(&init_mm, pud_start);
  630. p4d_clear(p4d);
  631. }
  632. static void remove_pte_table(pte_t *pte_start, unsigned long addr,
  633. unsigned long end, bool direct)
  634. {
  635. unsigned long next, pages = 0;
  636. pte_t *pte;
  637. pte = pte_start + pte_index(addr);
  638. for (; addr < end; addr = next, pte++) {
  639. next = (addr + PAGE_SIZE) & PAGE_MASK;
  640. if (next > end)
  641. next = end;
  642. if (!pte_present(*pte))
  643. continue;
  644. if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(next)) {
  645. /*
  646. * The vmemmap_free() and remove_section_mapping()
  647. * codepaths call us with aligned addresses.
  648. */
  649. WARN_ONCE(1, "%s: unaligned range\n", __func__);
  650. continue;
  651. }
  652. pte_clear(&init_mm, addr, pte);
  653. pages++;
  654. }
  655. if (direct)
  656. update_page_count(mmu_virtual_psize, -pages);
  657. }
  658. static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
  659. unsigned long end, bool direct)
  660. {
  661. unsigned long next, pages = 0;
  662. pte_t *pte_base;
  663. pmd_t *pmd;
  664. pmd = pmd_start + pmd_index(addr);
  665. for (; addr < end; addr = next, pmd++) {
  666. next = pmd_addr_end(addr, end);
  667. if (!pmd_present(*pmd))
  668. continue;
  669. if (pmd_is_leaf(*pmd)) {
  670. if (!IS_ALIGNED(addr, PMD_SIZE) ||
  671. !IS_ALIGNED(next, PMD_SIZE)) {
  672. WARN_ONCE(1, "%s: unaligned range\n", __func__);
  673. continue;
  674. }
  675. pte_clear(&init_mm, addr, (pte_t *)pmd);
  676. pages++;
  677. continue;
  678. }
  679. pte_base = (pte_t *)pmd_page_vaddr(*pmd);
  680. remove_pte_table(pte_base, addr, next, direct);
  681. free_pte_table(pte_base, pmd);
  682. }
  683. if (direct)
  684. update_page_count(MMU_PAGE_2M, -pages);
  685. }
  686. static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
  687. unsigned long end, bool direct)
  688. {
  689. unsigned long next, pages = 0;
  690. pmd_t *pmd_base;
  691. pud_t *pud;
  692. pud = pud_start + pud_index(addr);
  693. for (; addr < end; addr = next, pud++) {
  694. next = pud_addr_end(addr, end);
  695. if (!pud_present(*pud))
  696. continue;
  697. if (pud_is_leaf(*pud)) {
  698. if (!IS_ALIGNED(addr, PUD_SIZE) ||
  699. !IS_ALIGNED(next, PUD_SIZE)) {
  700. WARN_ONCE(1, "%s: unaligned range\n", __func__);
  701. continue;
  702. }
  703. pte_clear(&init_mm, addr, (pte_t *)pud);
  704. pages++;
  705. continue;
  706. }
  707. pmd_base = pud_pgtable(*pud);
  708. remove_pmd_table(pmd_base, addr, next, direct);
  709. free_pmd_table(pmd_base, pud);
  710. }
  711. if (direct)
  712. update_page_count(MMU_PAGE_1G, -pages);
  713. }
  714. static void __meminit remove_pagetable(unsigned long start, unsigned long end,
  715. bool direct)
  716. {
  717. unsigned long addr, next;
  718. pud_t *pud_base;
  719. pgd_t *pgd;
  720. p4d_t *p4d;
  721. spin_lock(&init_mm.page_table_lock);
  722. for (addr = start; addr < end; addr = next) {
  723. next = pgd_addr_end(addr, end);
  724. pgd = pgd_offset_k(addr);
  725. p4d = p4d_offset(pgd, addr);
  726. if (!p4d_present(*p4d))
  727. continue;
  728. if (p4d_is_leaf(*p4d)) {
  729. if (!IS_ALIGNED(addr, P4D_SIZE) ||
  730. !IS_ALIGNED(next, P4D_SIZE)) {
  731. WARN_ONCE(1, "%s: unaligned range\n", __func__);
  732. continue;
  733. }
  734. pte_clear(&init_mm, addr, (pte_t *)pgd);
  735. continue;
  736. }
  737. pud_base = p4d_pgtable(*p4d);
  738. remove_pud_table(pud_base, addr, next, direct);
  739. free_pud_table(pud_base, p4d);
  740. }
  741. spin_unlock(&init_mm.page_table_lock);
  742. radix__flush_tlb_kernel_range(start, end);
  743. }
  744. int __meminit radix__create_section_mapping(unsigned long start,
  745. unsigned long end, int nid,
  746. pgprot_t prot)
  747. {
  748. if (end >= RADIX_VMALLOC_START) {
  749. pr_warn("Outside the supported range\n");
  750. return -1;
  751. }
  752. return create_physical_mapping(__pa(start), __pa(end),
  753. nid, prot);
  754. }
  755. int __meminit radix__remove_section_mapping(unsigned long start, unsigned long end)
  756. {
  757. remove_pagetable(start, end, true);
  758. return 0;
  759. }
  760. #endif /* CONFIG_MEMORY_HOTPLUG */
  761. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  762. static int __map_kernel_page_nid(unsigned long ea, unsigned long pa,
  763. pgprot_t flags, unsigned int map_page_size,
  764. int nid)
  765. {
  766. return __map_kernel_page(ea, pa, flags, map_page_size, nid, 0, 0);
  767. }
  768. int __meminit radix__vmemmap_create_mapping(unsigned long start,
  769. unsigned long page_size,
  770. unsigned long phys)
  771. {
  772. /* Create a PTE encoding */
  773. unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
  774. int nid = early_pfn_to_nid(phys >> PAGE_SHIFT);
  775. int ret;
  776. if ((start + page_size) >= RADIX_VMEMMAP_END) {
  777. pr_warn("Outside the supported range\n");
  778. return -1;
  779. }
  780. ret = __map_kernel_page_nid(start, phys, __pgprot(flags), page_size, nid);
  781. BUG_ON(ret);
  782. return 0;
  783. }
  784. #ifdef CONFIG_MEMORY_HOTPLUG
  785. void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
  786. {
  787. remove_pagetable(start, start + page_size, false);
  788. }
  789. #endif
  790. #endif
  791. #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE)
  792. void radix__kernel_map_pages(struct page *page, int numpages, int enable)
  793. {
  794. unsigned long addr;
  795. addr = (unsigned long)page_address(page);
  796. if (enable)
  797. set_memory_p(addr, numpages);
  798. else
  799. set_memory_np(addr, numpages);
  800. }
  801. #endif
  802. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  803. unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  804. pmd_t *pmdp, unsigned long clr,
  805. unsigned long set)
  806. {
  807. unsigned long old;
  808. #ifdef CONFIG_DEBUG_VM
  809. WARN_ON(!radix__pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
  810. assert_spin_locked(pmd_lockptr(mm, pmdp));
  811. #endif
  812. old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
  813. trace_hugepage_update(addr, old, clr, set);
  814. return old;
  815. }
  816. pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  817. pmd_t *pmdp)
  818. {
  819. pmd_t pmd;
  820. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  821. VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
  822. VM_BUG_ON(pmd_devmap(*pmdp));
  823. /*
  824. * khugepaged calls this for normal pmd
  825. */
  826. pmd = *pmdp;
  827. pmd_clear(pmdp);
  828. radix__flush_tlb_collapsed_pmd(vma->vm_mm, address);
  829. return pmd;
  830. }
  831. /*
  832. * For us pgtable_t is pte_t *. Inorder to save the deposisted
  833. * page table, we consider the allocated page table as a list
  834. * head. On withdraw we need to make sure we zero out the used
  835. * list_head memory area.
  836. */
  837. void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  838. pgtable_t pgtable)
  839. {
  840. struct list_head *lh = (struct list_head *) pgtable;
  841. assert_spin_locked(pmd_lockptr(mm, pmdp));
  842. /* FIFO */
  843. if (!pmd_huge_pte(mm, pmdp))
  844. INIT_LIST_HEAD(lh);
  845. else
  846. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  847. pmd_huge_pte(mm, pmdp) = pgtable;
  848. }
  849. pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  850. {
  851. pte_t *ptep;
  852. pgtable_t pgtable;
  853. struct list_head *lh;
  854. assert_spin_locked(pmd_lockptr(mm, pmdp));
  855. /* FIFO */
  856. pgtable = pmd_huge_pte(mm, pmdp);
  857. lh = (struct list_head *) pgtable;
  858. if (list_empty(lh))
  859. pmd_huge_pte(mm, pmdp) = NULL;
  860. else {
  861. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  862. list_del(lh);
  863. }
  864. ptep = (pte_t *) pgtable;
  865. *ptep = __pte(0);
  866. ptep++;
  867. *ptep = __pte(0);
  868. return pgtable;
  869. }
  870. pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
  871. unsigned long addr, pmd_t *pmdp)
  872. {
  873. pmd_t old_pmd;
  874. unsigned long old;
  875. old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  876. old_pmd = __pmd(old);
  877. return old_pmd;
  878. }
  879. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  880. void radix__ptep_set_access_flags(struct vm_area_struct *vma, pte_t *ptep,
  881. pte_t entry, unsigned long address, int psize)
  882. {
  883. struct mm_struct *mm = vma->vm_mm;
  884. unsigned long set = pte_val(entry) & (_PAGE_DIRTY | _PAGE_SOFT_DIRTY |
  885. _PAGE_ACCESSED | _PAGE_RW | _PAGE_EXEC);
  886. unsigned long change = pte_val(entry) ^ pte_val(*ptep);
  887. /*
  888. * On POWER9, the NMMU is not able to relax PTE access permissions
  889. * for a translation with a TLB. The PTE must be invalidated, TLB
  890. * flushed before the new PTE is installed.
  891. *
  892. * This only needs to be done for radix, because hash translation does
  893. * flush when updating the linux pte (and we don't support NMMU
  894. * accelerators on HPT on POWER9 anyway XXX: do we?).
  895. *
  896. * POWER10 (and P9P) NMMU does behave as per ISA.
  897. */
  898. if (!cpu_has_feature(CPU_FTR_ARCH_31) && (change & _PAGE_RW) &&
  899. atomic_read(&mm->context.copros) > 0) {
  900. unsigned long old_pte, new_pte;
  901. old_pte = __radix_pte_update(ptep, _PAGE_PRESENT, _PAGE_INVALID);
  902. new_pte = old_pte | set;
  903. radix__flush_tlb_page_psize(mm, address, psize);
  904. __radix_pte_update(ptep, _PAGE_INVALID, new_pte);
  905. } else {
  906. __radix_pte_update(ptep, 0, set);
  907. /*
  908. * Book3S does not require a TLB flush when relaxing access
  909. * restrictions when the address space (modulo the POWER9 nest
  910. * MMU issue above) because the MMU will reload the PTE after
  911. * taking an access fault, as defined by the architecture. See
  912. * "Setting a Reference or Change Bit or Upgrading Access
  913. * Authority (PTE Subject to Atomic Hardware Updates)" in
  914. * Power ISA Version 3.1B.
  915. */
  916. }
  917. /* See ptesync comment in radix__set_pte_at */
  918. }
  919. void radix__ptep_modify_prot_commit(struct vm_area_struct *vma,
  920. unsigned long addr, pte_t *ptep,
  921. pte_t old_pte, pte_t pte)
  922. {
  923. struct mm_struct *mm = vma->vm_mm;
  924. /*
  925. * POWER9 NMMU must flush the TLB after clearing the PTE before
  926. * installing a PTE with more relaxed access permissions, see
  927. * radix__ptep_set_access_flags.
  928. */
  929. if (!cpu_has_feature(CPU_FTR_ARCH_31) &&
  930. is_pte_rw_upgrade(pte_val(old_pte), pte_val(pte)) &&
  931. (atomic_read(&mm->context.copros) > 0))
  932. radix__flush_tlb_page(vma, addr);
  933. set_pte_at(mm, addr, ptep, pte);
  934. }
  935. int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
  936. {
  937. pte_t *ptep = (pte_t *)pud;
  938. pte_t new_pud = pfn_pte(__phys_to_pfn(addr), prot);
  939. if (!radix_enabled())
  940. return 0;
  941. set_pte_at(&init_mm, 0 /* radix unused */, ptep, new_pud);
  942. return 1;
  943. }
  944. int pud_clear_huge(pud_t *pud)
  945. {
  946. if (pud_is_leaf(*pud)) {
  947. pud_clear(pud);
  948. return 1;
  949. }
  950. return 0;
  951. }
  952. int pud_free_pmd_page(pud_t *pud, unsigned long addr)
  953. {
  954. pmd_t *pmd;
  955. int i;
  956. pmd = pud_pgtable(*pud);
  957. pud_clear(pud);
  958. flush_tlb_kernel_range(addr, addr + PUD_SIZE);
  959. for (i = 0; i < PTRS_PER_PMD; i++) {
  960. if (!pmd_none(pmd[i])) {
  961. pte_t *pte;
  962. pte = (pte_t *)pmd_page_vaddr(pmd[i]);
  963. pte_free_kernel(&init_mm, pte);
  964. }
  965. }
  966. pmd_free(&init_mm, pmd);
  967. return 1;
  968. }
  969. int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
  970. {
  971. pte_t *ptep = (pte_t *)pmd;
  972. pte_t new_pmd = pfn_pte(__phys_to_pfn(addr), prot);
  973. if (!radix_enabled())
  974. return 0;
  975. set_pte_at(&init_mm, 0 /* radix unused */, ptep, new_pmd);
  976. return 1;
  977. }
  978. int pmd_clear_huge(pmd_t *pmd)
  979. {
  980. if (pmd_is_leaf(*pmd)) {
  981. pmd_clear(pmd);
  982. return 1;
  983. }
  984. return 0;
  985. }
  986. int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  987. {
  988. pte_t *pte;
  989. pte = (pte_t *)pmd_page_vaddr(*pmd);
  990. pmd_clear(pmd);
  991. flush_tlb_kernel_range(addr, addr + PMD_SIZE);
  992. pte_free_kernel(&init_mm, pte);
  993. return 1;
  994. }