pgtable-radix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Page table handling routines for radix page table.
  3. *
  4. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/memblock.h>
  13. #include <linux/of_fdt.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/dma.h>
  17. #include <asm/machdep.h>
  18. #include <asm/mmu.h>
  19. #include <asm/firmware.h>
  20. #include <trace/events/thp.h>
  21. static int native_register_process_table(unsigned long base, unsigned long pg_sz,
  22. unsigned long table_size)
  23. {
  24. unsigned long patb1 = base | table_size | PATB_GR;
  25. partition_tb->patb1 = cpu_to_be64(patb1);
  26. return 0;
  27. }
  28. static __ref void *early_alloc_pgtable(unsigned long size)
  29. {
  30. void *pt;
  31. pt = __va(memblock_alloc_base(size, size, MEMBLOCK_ALLOC_ANYWHERE));
  32. memset(pt, 0, size);
  33. return pt;
  34. }
  35. int radix__map_kernel_page(unsigned long ea, unsigned long pa,
  36. pgprot_t flags,
  37. unsigned int map_page_size)
  38. {
  39. pgd_t *pgdp;
  40. pud_t *pudp;
  41. pmd_t *pmdp;
  42. pte_t *ptep;
  43. /*
  44. * Make sure task size is correct as per the max adddr
  45. */
  46. BUILD_BUG_ON(TASK_SIZE_USER64 > RADIX_PGTABLE_RANGE);
  47. if (slab_is_available()) {
  48. pgdp = pgd_offset_k(ea);
  49. pudp = pud_alloc(&init_mm, pgdp, ea);
  50. if (!pudp)
  51. return -ENOMEM;
  52. if (map_page_size == PUD_SIZE) {
  53. ptep = (pte_t *)pudp;
  54. goto set_the_pte;
  55. }
  56. pmdp = pmd_alloc(&init_mm, pudp, ea);
  57. if (!pmdp)
  58. return -ENOMEM;
  59. if (map_page_size == PMD_SIZE) {
  60. ptep = (pte_t *)pudp;
  61. goto set_the_pte;
  62. }
  63. ptep = pte_alloc_kernel(pmdp, ea);
  64. if (!ptep)
  65. return -ENOMEM;
  66. } else {
  67. pgdp = pgd_offset_k(ea);
  68. if (pgd_none(*pgdp)) {
  69. pudp = early_alloc_pgtable(PUD_TABLE_SIZE);
  70. BUG_ON(pudp == NULL);
  71. pgd_populate(&init_mm, pgdp, pudp);
  72. }
  73. pudp = pud_offset(pgdp, ea);
  74. if (map_page_size == PUD_SIZE) {
  75. ptep = (pte_t *)pudp;
  76. goto set_the_pte;
  77. }
  78. if (pud_none(*pudp)) {
  79. pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
  80. BUG_ON(pmdp == NULL);
  81. pud_populate(&init_mm, pudp, pmdp);
  82. }
  83. pmdp = pmd_offset(pudp, ea);
  84. if (map_page_size == PMD_SIZE) {
  85. ptep = (pte_t *)pudp;
  86. goto set_the_pte;
  87. }
  88. if (!pmd_present(*pmdp)) {
  89. ptep = early_alloc_pgtable(PAGE_SIZE);
  90. BUG_ON(ptep == NULL);
  91. pmd_populate_kernel(&init_mm, pmdp, ptep);
  92. }
  93. ptep = pte_offset_kernel(pmdp, ea);
  94. }
  95. set_the_pte:
  96. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, flags));
  97. smp_wmb();
  98. return 0;
  99. }
  100. static void __init radix_init_pgtable(void)
  101. {
  102. int loop_count;
  103. u64 base, end, start_addr;
  104. unsigned long rts_field;
  105. struct memblock_region *reg;
  106. unsigned long linear_page_size;
  107. /* We don't support slb for radix */
  108. mmu_slb_size = 0;
  109. /*
  110. * Create the linear mapping, using standard page size for now
  111. */
  112. loop_count = 0;
  113. for_each_memblock(memory, reg) {
  114. start_addr = reg->base;
  115. redo:
  116. if (loop_count < 1 && mmu_psize_defs[MMU_PAGE_1G].shift)
  117. linear_page_size = PUD_SIZE;
  118. else if (loop_count < 2 && mmu_psize_defs[MMU_PAGE_2M].shift)
  119. linear_page_size = PMD_SIZE;
  120. else
  121. linear_page_size = PAGE_SIZE;
  122. base = _ALIGN_UP(start_addr, linear_page_size);
  123. end = _ALIGN_DOWN(reg->base + reg->size, linear_page_size);
  124. pr_info("Mapping range 0x%lx - 0x%lx with 0x%lx\n",
  125. (unsigned long)base, (unsigned long)end,
  126. linear_page_size);
  127. while (base < end) {
  128. radix__map_kernel_page((unsigned long)__va(base),
  129. base, PAGE_KERNEL_X,
  130. linear_page_size);
  131. base += linear_page_size;
  132. }
  133. /*
  134. * map the rest using lower page size
  135. */
  136. if (end < reg->base + reg->size) {
  137. start_addr = end;
  138. loop_count++;
  139. goto redo;
  140. }
  141. }
  142. /*
  143. * Allocate Partition table and process table for the
  144. * host.
  145. */
  146. BUILD_BUG_ON_MSG((PRTB_SIZE_SHIFT > 23), "Process table size too large.");
  147. process_tb = early_alloc_pgtable(1UL << PRTB_SIZE_SHIFT);
  148. /*
  149. * Fill in the process table.
  150. */
  151. rts_field = radix__get_tree_size();
  152. process_tb->prtb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE);
  153. /*
  154. * Fill in the partition table. We are suppose to use effective address
  155. * of process table here. But our linear mapping also enable us to use
  156. * physical address here.
  157. */
  158. register_process_table(__pa(process_tb), 0, PRTB_SIZE_SHIFT - 12);
  159. pr_info("Process table %p and radix root for kernel: %p\n", process_tb, init_mm.pgd);
  160. }
  161. static void __init radix_init_partition_table(void)
  162. {
  163. unsigned long rts_field, dw0;
  164. mmu_partition_table_init();
  165. rts_field = radix__get_tree_size();
  166. dw0 = rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE | PATB_HR;
  167. mmu_partition_table_set_entry(0, dw0, 0);
  168. pr_info("Initializing Radix MMU\n");
  169. pr_info("Partition table %p\n", partition_tb);
  170. }
  171. void __init radix_init_native(void)
  172. {
  173. register_process_table = native_register_process_table;
  174. }
  175. static int __init get_idx_from_shift(unsigned int shift)
  176. {
  177. int idx = -1;
  178. switch (shift) {
  179. case 0xc:
  180. idx = MMU_PAGE_4K;
  181. break;
  182. case 0x10:
  183. idx = MMU_PAGE_64K;
  184. break;
  185. case 0x15:
  186. idx = MMU_PAGE_2M;
  187. break;
  188. case 0x1e:
  189. idx = MMU_PAGE_1G;
  190. break;
  191. }
  192. return idx;
  193. }
  194. static int __init radix_dt_scan_page_sizes(unsigned long node,
  195. const char *uname, int depth,
  196. void *data)
  197. {
  198. int size = 0;
  199. int shift, idx;
  200. unsigned int ap;
  201. const __be32 *prop;
  202. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  203. /* We are scanning "cpu" nodes only */
  204. if (type == NULL || strcmp(type, "cpu") != 0)
  205. return 0;
  206. prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
  207. if (!prop)
  208. return 0;
  209. pr_info("Page sizes from device-tree:\n");
  210. for (; size >= 4; size -= 4, ++prop) {
  211. struct mmu_psize_def *def;
  212. /* top 3 bit is AP encoding */
  213. shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
  214. ap = be32_to_cpu(prop[0]) >> 29;
  215. pr_info("Page size sift = %d AP=0x%x\n", shift, ap);
  216. idx = get_idx_from_shift(shift);
  217. if (idx < 0)
  218. continue;
  219. def = &mmu_psize_defs[idx];
  220. def->shift = shift;
  221. def->ap = ap;
  222. }
  223. /* needed ? */
  224. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  225. return 1;
  226. }
  227. void __init radix__early_init_devtree(void)
  228. {
  229. int rc;
  230. /*
  231. * Try to find the available page sizes in the device-tree
  232. */
  233. rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
  234. if (rc != 0) /* Found */
  235. goto found;
  236. /*
  237. * let's assume we have page 4k and 64k support
  238. */
  239. mmu_psize_defs[MMU_PAGE_4K].shift = 12;
  240. mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
  241. mmu_psize_defs[MMU_PAGE_64K].shift = 16;
  242. mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
  243. found:
  244. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  245. if (mmu_psize_defs[MMU_PAGE_2M].shift) {
  246. /*
  247. * map vmemmap using 2M if available
  248. */
  249. mmu_vmemmap_psize = MMU_PAGE_2M;
  250. }
  251. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  252. return;
  253. }
  254. static void update_hid_for_radix(void)
  255. {
  256. unsigned long hid0;
  257. unsigned long rb = 3UL << PPC_BITLSHIFT(53); /* IS = 3 */
  258. asm volatile("ptesync": : :"memory");
  259. /* prs = 0, ric = 2, rs = 0, r = 1 is = 3 */
  260. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  261. : : "r"(rb), "i"(1), "i"(0), "i"(2), "r"(0) : "memory");
  262. /* prs = 1, ric = 2, rs = 0, r = 1 is = 3 */
  263. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  264. : : "r"(rb), "i"(1), "i"(1), "i"(2), "r"(0) : "memory");
  265. asm volatile("eieio; tlbsync; ptesync; isync; slbia": : :"memory");
  266. /*
  267. * now switch the HID
  268. */
  269. hid0 = mfspr(SPRN_HID0);
  270. hid0 |= HID0_POWER9_RADIX;
  271. mtspr(SPRN_HID0, hid0);
  272. asm volatile("isync": : :"memory");
  273. /* Wait for it to happen */
  274. while (!(mfspr(SPRN_HID0) & HID0_POWER9_RADIX))
  275. cpu_relax();
  276. }
  277. void __init radix__early_init_mmu(void)
  278. {
  279. unsigned long lpcr;
  280. #ifdef CONFIG_PPC_64K_PAGES
  281. /* PAGE_SIZE mappings */
  282. mmu_virtual_psize = MMU_PAGE_64K;
  283. #else
  284. mmu_virtual_psize = MMU_PAGE_4K;
  285. #endif
  286. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  287. /* vmemmap mapping */
  288. mmu_vmemmap_psize = mmu_virtual_psize;
  289. #endif
  290. /*
  291. * initialize page table size
  292. */
  293. __pte_index_size = RADIX_PTE_INDEX_SIZE;
  294. __pmd_index_size = RADIX_PMD_INDEX_SIZE;
  295. __pud_index_size = RADIX_PUD_INDEX_SIZE;
  296. __pgd_index_size = RADIX_PGD_INDEX_SIZE;
  297. __pmd_cache_index = RADIX_PMD_INDEX_SIZE;
  298. __pte_table_size = RADIX_PTE_TABLE_SIZE;
  299. __pmd_table_size = RADIX_PMD_TABLE_SIZE;
  300. __pud_table_size = RADIX_PUD_TABLE_SIZE;
  301. __pgd_table_size = RADIX_PGD_TABLE_SIZE;
  302. __pmd_val_bits = RADIX_PMD_VAL_BITS;
  303. __pud_val_bits = RADIX_PUD_VAL_BITS;
  304. __pgd_val_bits = RADIX_PGD_VAL_BITS;
  305. __kernel_virt_start = RADIX_KERN_VIRT_START;
  306. __kernel_virt_size = RADIX_KERN_VIRT_SIZE;
  307. __vmalloc_start = RADIX_VMALLOC_START;
  308. __vmalloc_end = RADIX_VMALLOC_END;
  309. vmemmap = (struct page *)RADIX_VMEMMAP_BASE;
  310. ioremap_bot = IOREMAP_BASE;
  311. #ifdef CONFIG_PCI
  312. pci_io_base = ISA_IO_BASE;
  313. #endif
  314. /*
  315. * For now radix also use the same frag size
  316. */
  317. __pte_frag_nr = H_PTE_FRAG_NR;
  318. __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
  319. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  320. radix_init_native();
  321. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  322. update_hid_for_radix();
  323. lpcr = mfspr(SPRN_LPCR);
  324. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  325. radix_init_partition_table();
  326. }
  327. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  328. radix_init_pgtable();
  329. }
  330. void radix__early_init_mmu_secondary(void)
  331. {
  332. unsigned long lpcr;
  333. /*
  334. * update partition table control register and UPRT
  335. */
  336. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  337. lpcr = mfspr(SPRN_LPCR);
  338. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  339. mtspr(SPRN_PTCR,
  340. __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  341. }
  342. }
  343. void radix__mmu_cleanup_all(void)
  344. {
  345. unsigned long lpcr;
  346. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  347. lpcr = mfspr(SPRN_LPCR);
  348. mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
  349. mtspr(SPRN_PTCR, 0);
  350. radix__flush_tlb_all();
  351. }
  352. }
  353. void radix__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  354. phys_addr_t first_memblock_size)
  355. {
  356. /* We don't currently support the first MEMBLOCK not mapping 0
  357. * physical on those processors
  358. */
  359. BUG_ON(first_memblock_base != 0);
  360. /*
  361. * We limit the allocation that depend on ppc64_rma_size
  362. * to first_memblock_size. We also clamp it to 1GB to
  363. * avoid some funky things such as RTAS bugs.
  364. *
  365. * On radix config we really don't have a limitation
  366. * on real mode access. But keeping it as above works
  367. * well enough.
  368. */
  369. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  370. /*
  371. * Finally limit subsequent allocations. We really don't want
  372. * to limit the memblock allocations to rma_size. FIXME!! should
  373. * we even limit at all ?
  374. */
  375. memblock_set_current_limit(first_memblock_base + first_memblock_size);
  376. }
  377. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  378. int __meminit radix__vmemmap_create_mapping(unsigned long start,
  379. unsigned long page_size,
  380. unsigned long phys)
  381. {
  382. /* Create a PTE encoding */
  383. unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
  384. BUG_ON(radix__map_kernel_page(start, phys, __pgprot(flags), page_size));
  385. return 0;
  386. }
  387. #ifdef CONFIG_MEMORY_HOTPLUG
  388. void radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
  389. {
  390. /* FIXME!! intel does more. We should free page tables mapping vmemmap ? */
  391. }
  392. #endif
  393. #endif
  394. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  395. unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  396. pmd_t *pmdp, unsigned long clr,
  397. unsigned long set)
  398. {
  399. unsigned long old;
  400. #ifdef CONFIG_DEBUG_VM
  401. WARN_ON(!radix__pmd_trans_huge(*pmdp));
  402. assert_spin_locked(&mm->page_table_lock);
  403. #endif
  404. old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
  405. trace_hugepage_update(addr, old, clr, set);
  406. return old;
  407. }
  408. pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  409. pmd_t *pmdp)
  410. {
  411. pmd_t pmd;
  412. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  413. VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
  414. /*
  415. * khugepaged calls this for normal pmd
  416. */
  417. pmd = *pmdp;
  418. pmd_clear(pmdp);
  419. /*FIXME!! Verify whether we need this kick below */
  420. kick_all_cpus_sync();
  421. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  422. return pmd;
  423. }
  424. /*
  425. * For us pgtable_t is pte_t *. Inorder to save the deposisted
  426. * page table, we consider the allocated page table as a list
  427. * head. On withdraw we need to make sure we zero out the used
  428. * list_head memory area.
  429. */
  430. void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  431. pgtable_t pgtable)
  432. {
  433. struct list_head *lh = (struct list_head *) pgtable;
  434. assert_spin_locked(pmd_lockptr(mm, pmdp));
  435. /* FIFO */
  436. if (!pmd_huge_pte(mm, pmdp))
  437. INIT_LIST_HEAD(lh);
  438. else
  439. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  440. pmd_huge_pte(mm, pmdp) = pgtable;
  441. }
  442. pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  443. {
  444. pte_t *ptep;
  445. pgtable_t pgtable;
  446. struct list_head *lh;
  447. assert_spin_locked(pmd_lockptr(mm, pmdp));
  448. /* FIFO */
  449. pgtable = pmd_huge_pte(mm, pmdp);
  450. lh = (struct list_head *) pgtable;
  451. if (list_empty(lh))
  452. pmd_huge_pte(mm, pmdp) = NULL;
  453. else {
  454. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  455. list_del(lh);
  456. }
  457. ptep = (pte_t *) pgtable;
  458. *ptep = __pte(0);
  459. ptep++;
  460. *ptep = __pte(0);
  461. return pgtable;
  462. }
  463. pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
  464. unsigned long addr, pmd_t *pmdp)
  465. {
  466. pmd_t old_pmd;
  467. unsigned long old;
  468. old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  469. old_pmd = __pmd(old);
  470. /*
  471. * Serialize against find_linux_pte_or_hugepte which does lock-less
  472. * lookup in page tables with local interrupts disabled. For huge pages
  473. * it casts pmd_t to pte_t. Since format of pte_t is different from
  474. * pmd_t we want to prevent transit from pmd pointing to page table
  475. * to pmd pointing to huge page (and back) while interrupts are disabled.
  476. * We clear pmd to possibly replace it with page table pointer in
  477. * different code paths. So make sure we wait for the parallel
  478. * find_linux_pte_or_hugepage to finish.
  479. */
  480. kick_all_cpus_sync();
  481. return old_pmd;
  482. }
  483. int radix__has_transparent_hugepage(void)
  484. {
  485. /* For radix 2M at PMD level means thp */
  486. if (mmu_psize_defs[MMU_PAGE_2M].shift == PMD_SHIFT)
  487. return 1;
  488. return 0;
  489. }
  490. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */