pgtable-radix.c 15 KB

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