pgtable-radix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 = pmdp_ptep(pmdp);
  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 = pmdp_ptep(pmdp);
  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 > 36), "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;
  164. rts_field = radix__get_tree_size();
  165. BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 36), "Partition table size too large.");
  166. partition_tb = early_alloc_pgtable(1UL << PATB_SIZE_SHIFT);
  167. partition_tb->patb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) |
  168. RADIX_PGD_INDEX_SIZE | PATB_HR);
  169. pr_info("Initializing Radix MMU\n");
  170. pr_info("Partition table %p\n", partition_tb);
  171. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  172. /*
  173. * update partition table control register,
  174. * 64 K size.
  175. */
  176. mtspr(SPRN_PTCR, __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  177. }
  178. void __init radix_init_native(void)
  179. {
  180. register_process_table = native_register_process_table;
  181. }
  182. static int __init get_idx_from_shift(unsigned int shift)
  183. {
  184. int idx = -1;
  185. switch (shift) {
  186. case 0xc:
  187. idx = MMU_PAGE_4K;
  188. break;
  189. case 0x10:
  190. idx = MMU_PAGE_64K;
  191. break;
  192. case 0x15:
  193. idx = MMU_PAGE_2M;
  194. break;
  195. case 0x1e:
  196. idx = MMU_PAGE_1G;
  197. break;
  198. }
  199. return idx;
  200. }
  201. static int __init radix_dt_scan_page_sizes(unsigned long node,
  202. const char *uname, int depth,
  203. void *data)
  204. {
  205. int size = 0;
  206. int shift, idx;
  207. unsigned int ap;
  208. const __be32 *prop;
  209. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  210. /* We are scanning "cpu" nodes only */
  211. if (type == NULL || strcmp(type, "cpu") != 0)
  212. return 0;
  213. prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
  214. if (!prop)
  215. return 0;
  216. pr_info("Page sizes from device-tree:\n");
  217. for (; size >= 4; size -= 4, ++prop) {
  218. struct mmu_psize_def *def;
  219. /* top 3 bit is AP encoding */
  220. shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
  221. ap = be32_to_cpu(prop[0]) >> 29;
  222. pr_info("Page size sift = %d AP=0x%x\n", shift, ap);
  223. idx = get_idx_from_shift(shift);
  224. if (idx < 0)
  225. continue;
  226. def = &mmu_psize_defs[idx];
  227. def->shift = shift;
  228. def->ap = ap;
  229. }
  230. /* needed ? */
  231. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  232. return 1;
  233. }
  234. void __init radix__early_init_devtree(void)
  235. {
  236. int rc;
  237. /*
  238. * Try to find the available page sizes in the device-tree
  239. */
  240. rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
  241. if (rc != 0) /* Found */
  242. goto found;
  243. /*
  244. * let's assume we have page 4k and 64k support
  245. */
  246. mmu_psize_defs[MMU_PAGE_4K].shift = 12;
  247. mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
  248. mmu_psize_defs[MMU_PAGE_64K].shift = 16;
  249. mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
  250. found:
  251. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  252. if (mmu_psize_defs[MMU_PAGE_2M].shift) {
  253. /*
  254. * map vmemmap using 2M if available
  255. */
  256. mmu_vmemmap_psize = MMU_PAGE_2M;
  257. }
  258. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  259. return;
  260. }
  261. static void update_hid_for_radix(void)
  262. {
  263. unsigned long hid0;
  264. unsigned long rb = 3UL << PPC_BITLSHIFT(53); /* IS = 3 */
  265. asm volatile("ptesync": : :"memory");
  266. /* prs = 0, ric = 2, rs = 0, r = 1 is = 3 */
  267. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  268. : : "r"(rb), "i"(1), "i"(0), "i"(2), "r"(0) : "memory");
  269. /* prs = 1, ric = 2, rs = 0, r = 1 is = 3 */
  270. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  271. : : "r"(rb), "i"(1), "i"(1), "i"(2), "r"(0) : "memory");
  272. asm volatile("eieio; tlbsync; ptesync; isync; slbia": : :"memory");
  273. /*
  274. * now switch the HID
  275. */
  276. hid0 = mfspr(SPRN_HID0);
  277. hid0 |= HID0_POWER9_RADIX;
  278. mtspr(SPRN_HID0, hid0);
  279. asm volatile("isync": : :"memory");
  280. /* Wait for it to happen */
  281. while (!(mfspr(SPRN_HID0) & HID0_POWER9_RADIX))
  282. cpu_relax();
  283. }
  284. void __init radix__early_init_mmu(void)
  285. {
  286. unsigned long lpcr;
  287. #ifdef CONFIG_PPC_64K_PAGES
  288. /* PAGE_SIZE mappings */
  289. mmu_virtual_psize = MMU_PAGE_64K;
  290. #else
  291. mmu_virtual_psize = MMU_PAGE_4K;
  292. #endif
  293. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  294. /* vmemmap mapping */
  295. mmu_vmemmap_psize = mmu_virtual_psize;
  296. #endif
  297. /*
  298. * initialize page table size
  299. */
  300. __pte_index_size = RADIX_PTE_INDEX_SIZE;
  301. __pmd_index_size = RADIX_PMD_INDEX_SIZE;
  302. __pud_index_size = RADIX_PUD_INDEX_SIZE;
  303. __pgd_index_size = RADIX_PGD_INDEX_SIZE;
  304. __pmd_cache_index = RADIX_PMD_INDEX_SIZE;
  305. __pte_table_size = RADIX_PTE_TABLE_SIZE;
  306. __pmd_table_size = RADIX_PMD_TABLE_SIZE;
  307. __pud_table_size = RADIX_PUD_TABLE_SIZE;
  308. __pgd_table_size = RADIX_PGD_TABLE_SIZE;
  309. __pmd_val_bits = RADIX_PMD_VAL_BITS;
  310. __pud_val_bits = RADIX_PUD_VAL_BITS;
  311. __pgd_val_bits = RADIX_PGD_VAL_BITS;
  312. __kernel_virt_start = RADIX_KERN_VIRT_START;
  313. __kernel_virt_size = RADIX_KERN_VIRT_SIZE;
  314. __vmalloc_start = RADIX_VMALLOC_START;
  315. __vmalloc_end = RADIX_VMALLOC_END;
  316. vmemmap = (struct page *)RADIX_VMEMMAP_BASE;
  317. ioremap_bot = IOREMAP_BASE;
  318. #ifdef CONFIG_PCI
  319. pci_io_base = ISA_IO_BASE;
  320. #endif
  321. /*
  322. * For now radix also use the same frag size
  323. */
  324. __pte_frag_nr = H_PTE_FRAG_NR;
  325. __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
  326. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  327. radix_init_native();
  328. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  329. update_hid_for_radix();
  330. lpcr = mfspr(SPRN_LPCR);
  331. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  332. radix_init_partition_table();
  333. }
  334. radix_init_pgtable();
  335. }
  336. void radix__early_init_mmu_secondary(void)
  337. {
  338. unsigned long lpcr;
  339. /*
  340. * update partition table control register and UPRT
  341. */
  342. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  343. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  344. update_hid_for_radix();
  345. lpcr = mfspr(SPRN_LPCR);
  346. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  347. mtspr(SPRN_PTCR,
  348. __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  349. }
  350. }
  351. void radix__mmu_cleanup_all(void)
  352. {
  353. unsigned long lpcr;
  354. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  355. lpcr = mfspr(SPRN_LPCR);
  356. mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
  357. mtspr(SPRN_PTCR, 0);
  358. radix__flush_tlb_all();
  359. }
  360. }
  361. void radix__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  362. phys_addr_t first_memblock_size)
  363. {
  364. /* We don't currently support the first MEMBLOCK not mapping 0
  365. * physical on those processors
  366. */
  367. BUG_ON(first_memblock_base != 0);
  368. /*
  369. * We limit the allocation that depend on ppc64_rma_size
  370. * to first_memblock_size. We also clamp it to 1GB to
  371. * avoid some funky things such as RTAS bugs.
  372. *
  373. * On radix config we really don't have a limitation
  374. * on real mode access. But keeping it as above works
  375. * well enough.
  376. */
  377. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  378. /*
  379. * Finally limit subsequent allocations. We really don't want
  380. * to limit the memblock allocations to rma_size. FIXME!! should
  381. * we even limit at all ?
  382. */
  383. memblock_set_current_limit(first_memblock_base + first_memblock_size);
  384. }
  385. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  386. int __meminit radix__vmemmap_create_mapping(unsigned long start,
  387. unsigned long page_size,
  388. unsigned long phys)
  389. {
  390. /* Create a PTE encoding */
  391. unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
  392. BUG_ON(radix__map_kernel_page(start, phys, __pgprot(flags), page_size));
  393. return 0;
  394. }
  395. #ifdef CONFIG_MEMORY_HOTPLUG
  396. void radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
  397. {
  398. /* FIXME!! intel does more. We should free page tables mapping vmemmap ? */
  399. }
  400. #endif
  401. #endif
  402. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  403. unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  404. pmd_t *pmdp, unsigned long clr,
  405. unsigned long set)
  406. {
  407. unsigned long old;
  408. #ifdef CONFIG_DEBUG_VM
  409. WARN_ON(!radix__pmd_trans_huge(*pmdp));
  410. assert_spin_locked(&mm->page_table_lock);
  411. #endif
  412. old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
  413. trace_hugepage_update(addr, old, clr, set);
  414. return old;
  415. }
  416. pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  417. pmd_t *pmdp)
  418. {
  419. pmd_t pmd;
  420. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  421. VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
  422. /*
  423. * khugepaged calls this for normal pmd
  424. */
  425. pmd = *pmdp;
  426. pmd_clear(pmdp);
  427. /*FIXME!! Verify whether we need this kick below */
  428. kick_all_cpus_sync();
  429. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  430. return pmd;
  431. }
  432. /*
  433. * For us pgtable_t is pte_t *. Inorder to save the deposisted
  434. * page table, we consider the allocated page table as a list
  435. * head. On withdraw we need to make sure we zero out the used
  436. * list_head memory area.
  437. */
  438. void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  439. pgtable_t pgtable)
  440. {
  441. struct list_head *lh = (struct list_head *) pgtable;
  442. assert_spin_locked(pmd_lockptr(mm, pmdp));
  443. /* FIFO */
  444. if (!pmd_huge_pte(mm, pmdp))
  445. INIT_LIST_HEAD(lh);
  446. else
  447. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  448. pmd_huge_pte(mm, pmdp) = pgtable;
  449. }
  450. pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  451. {
  452. pte_t *ptep;
  453. pgtable_t pgtable;
  454. struct list_head *lh;
  455. assert_spin_locked(pmd_lockptr(mm, pmdp));
  456. /* FIFO */
  457. pgtable = pmd_huge_pte(mm, pmdp);
  458. lh = (struct list_head *) pgtable;
  459. if (list_empty(lh))
  460. pmd_huge_pte(mm, pmdp) = NULL;
  461. else {
  462. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  463. list_del(lh);
  464. }
  465. ptep = (pte_t *) pgtable;
  466. *ptep = __pte(0);
  467. ptep++;
  468. *ptep = __pte(0);
  469. return pgtable;
  470. }
  471. pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
  472. unsigned long addr, pmd_t *pmdp)
  473. {
  474. pmd_t old_pmd;
  475. unsigned long old;
  476. old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  477. old_pmd = __pmd(old);
  478. /*
  479. * Serialize against find_linux_pte_or_hugepte which does lock-less
  480. * lookup in page tables with local interrupts disabled. For huge pages
  481. * it casts pmd_t to pte_t. Since format of pte_t is different from
  482. * pmd_t we want to prevent transit from pmd pointing to page table
  483. * to pmd pointing to huge page (and back) while interrupts are disabled.
  484. * We clear pmd to possibly replace it with page table pointer in
  485. * different code paths. So make sure we wait for the parallel
  486. * find_linux_pte_or_hugepage to finish.
  487. */
  488. kick_all_cpus_sync();
  489. return old_pmd;
  490. }
  491. int radix__has_transparent_hugepage(void)
  492. {
  493. /* For radix 2M at PMD level means thp */
  494. if (mmu_psize_defs[MMU_PAGE_2M].shift == PMD_SHIFT)
  495. return 1;
  496. return 0;
  497. }
  498. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */