pgtable-radix.c 13 KB

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