pgtable-radix.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 > 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, 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 shift = %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. static void radix_init_amor(void)
  278. {
  279. /*
  280. * In HV mode, we init AMOR (Authority Mask Override Register) so that
  281. * the hypervisor and guest can setup IAMR (Instruction Authority Mask
  282. * Register), enable key 0 and set it to 1.
  283. *
  284. * AMOR = 0b1100 .... 0000 (Mask for key 0 is 11)
  285. */
  286. mtspr(SPRN_AMOR, (3ul << 62));
  287. }
  288. static void radix_init_iamr(void)
  289. {
  290. unsigned long iamr;
  291. /*
  292. * The IAMR should set to 0 on DD1.
  293. */
  294. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  295. iamr = 0;
  296. else
  297. iamr = (1ul << 62);
  298. /*
  299. * Radix always uses key0 of the IAMR to determine if an access is
  300. * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
  301. * fetch.
  302. */
  303. mtspr(SPRN_IAMR, iamr);
  304. }
  305. void __init radix__early_init_mmu(void)
  306. {
  307. unsigned long lpcr;
  308. #ifdef CONFIG_PPC_64K_PAGES
  309. /* PAGE_SIZE mappings */
  310. mmu_virtual_psize = MMU_PAGE_64K;
  311. #else
  312. mmu_virtual_psize = MMU_PAGE_4K;
  313. #endif
  314. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  315. /* vmemmap mapping */
  316. mmu_vmemmap_psize = mmu_virtual_psize;
  317. #endif
  318. /*
  319. * initialize page table size
  320. */
  321. __pte_index_size = RADIX_PTE_INDEX_SIZE;
  322. __pmd_index_size = RADIX_PMD_INDEX_SIZE;
  323. __pud_index_size = RADIX_PUD_INDEX_SIZE;
  324. __pgd_index_size = RADIX_PGD_INDEX_SIZE;
  325. __pmd_cache_index = RADIX_PMD_INDEX_SIZE;
  326. __pte_table_size = RADIX_PTE_TABLE_SIZE;
  327. __pmd_table_size = RADIX_PMD_TABLE_SIZE;
  328. __pud_table_size = RADIX_PUD_TABLE_SIZE;
  329. __pgd_table_size = RADIX_PGD_TABLE_SIZE;
  330. __pmd_val_bits = RADIX_PMD_VAL_BITS;
  331. __pud_val_bits = RADIX_PUD_VAL_BITS;
  332. __pgd_val_bits = RADIX_PGD_VAL_BITS;
  333. __kernel_virt_start = RADIX_KERN_VIRT_START;
  334. __kernel_virt_size = RADIX_KERN_VIRT_SIZE;
  335. __vmalloc_start = RADIX_VMALLOC_START;
  336. __vmalloc_end = RADIX_VMALLOC_END;
  337. vmemmap = (struct page *)RADIX_VMEMMAP_BASE;
  338. ioremap_bot = IOREMAP_BASE;
  339. #ifdef CONFIG_PCI
  340. pci_io_base = ISA_IO_BASE;
  341. #endif
  342. /*
  343. * For now radix also use the same frag size
  344. */
  345. __pte_frag_nr = H_PTE_FRAG_NR;
  346. __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
  347. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  348. radix_init_native();
  349. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  350. update_hid_for_radix();
  351. lpcr = mfspr(SPRN_LPCR);
  352. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  353. radix_init_partition_table();
  354. radix_init_amor();
  355. }
  356. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  357. radix_init_iamr();
  358. radix_init_pgtable();
  359. }
  360. void radix__early_init_mmu_secondary(void)
  361. {
  362. unsigned long lpcr;
  363. /*
  364. * update partition table control register and UPRT
  365. */
  366. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  367. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  368. update_hid_for_radix();
  369. lpcr = mfspr(SPRN_LPCR);
  370. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  371. mtspr(SPRN_PTCR,
  372. __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  373. radix_init_amor();
  374. }
  375. radix_init_iamr();
  376. }
  377. void radix__mmu_cleanup_all(void)
  378. {
  379. unsigned long lpcr;
  380. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  381. lpcr = mfspr(SPRN_LPCR);
  382. mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
  383. mtspr(SPRN_PTCR, 0);
  384. radix__flush_tlb_all();
  385. }
  386. }
  387. void radix__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  388. phys_addr_t first_memblock_size)
  389. {
  390. /* We don't currently support the first MEMBLOCK not mapping 0
  391. * physical on those processors
  392. */
  393. BUG_ON(first_memblock_base != 0);
  394. /*
  395. * We limit the allocation that depend on ppc64_rma_size
  396. * to first_memblock_size. We also clamp it to 1GB to
  397. * avoid some funky things such as RTAS bugs.
  398. *
  399. * On radix config we really don't have a limitation
  400. * on real mode access. But keeping it as above works
  401. * well enough.
  402. */
  403. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  404. /*
  405. * Finally limit subsequent allocations. We really don't want
  406. * to limit the memblock allocations to rma_size. FIXME!! should
  407. * we even limit at all ?
  408. */
  409. memblock_set_current_limit(first_memblock_base + first_memblock_size);
  410. }
  411. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  412. int __meminit radix__vmemmap_create_mapping(unsigned long start,
  413. unsigned long page_size,
  414. unsigned long phys)
  415. {
  416. /* Create a PTE encoding */
  417. unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
  418. BUG_ON(radix__map_kernel_page(start, phys, __pgprot(flags), page_size));
  419. return 0;
  420. }
  421. #ifdef CONFIG_MEMORY_HOTPLUG
  422. void radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
  423. {
  424. /* FIXME!! intel does more. We should free page tables mapping vmemmap ? */
  425. }
  426. #endif
  427. #endif
  428. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  429. unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  430. pmd_t *pmdp, unsigned long clr,
  431. unsigned long set)
  432. {
  433. unsigned long old;
  434. #ifdef CONFIG_DEBUG_VM
  435. WARN_ON(!radix__pmd_trans_huge(*pmdp));
  436. assert_spin_locked(&mm->page_table_lock);
  437. #endif
  438. old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
  439. trace_hugepage_update(addr, old, clr, set);
  440. return old;
  441. }
  442. pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  443. pmd_t *pmdp)
  444. {
  445. pmd_t pmd;
  446. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  447. VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
  448. /*
  449. * khugepaged calls this for normal pmd
  450. */
  451. pmd = *pmdp;
  452. pmd_clear(pmdp);
  453. /*FIXME!! Verify whether we need this kick below */
  454. kick_all_cpus_sync();
  455. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  456. return pmd;
  457. }
  458. /*
  459. * For us pgtable_t is pte_t *. Inorder to save the deposisted
  460. * page table, we consider the allocated page table as a list
  461. * head. On withdraw we need to make sure we zero out the used
  462. * list_head memory area.
  463. */
  464. void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  465. pgtable_t pgtable)
  466. {
  467. struct list_head *lh = (struct list_head *) pgtable;
  468. assert_spin_locked(pmd_lockptr(mm, pmdp));
  469. /* FIFO */
  470. if (!pmd_huge_pte(mm, pmdp))
  471. INIT_LIST_HEAD(lh);
  472. else
  473. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  474. pmd_huge_pte(mm, pmdp) = pgtable;
  475. }
  476. pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  477. {
  478. pte_t *ptep;
  479. pgtable_t pgtable;
  480. struct list_head *lh;
  481. assert_spin_locked(pmd_lockptr(mm, pmdp));
  482. /* FIFO */
  483. pgtable = pmd_huge_pte(mm, pmdp);
  484. lh = (struct list_head *) pgtable;
  485. if (list_empty(lh))
  486. pmd_huge_pte(mm, pmdp) = NULL;
  487. else {
  488. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  489. list_del(lh);
  490. }
  491. ptep = (pte_t *) pgtable;
  492. *ptep = __pte(0);
  493. ptep++;
  494. *ptep = __pte(0);
  495. return pgtable;
  496. }
  497. pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
  498. unsigned long addr, pmd_t *pmdp)
  499. {
  500. pmd_t old_pmd;
  501. unsigned long old;
  502. old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  503. old_pmd = __pmd(old);
  504. /*
  505. * Serialize against find_linux_pte_or_hugepte which does lock-less
  506. * lookup in page tables with local interrupts disabled. For huge pages
  507. * it casts pmd_t to pte_t. Since format of pte_t is different from
  508. * pmd_t we want to prevent transit from pmd pointing to page table
  509. * to pmd pointing to huge page (and back) while interrupts are disabled.
  510. * We clear pmd to possibly replace it with page table pointer in
  511. * different code paths. So make sure we wait for the parallel
  512. * find_linux_pte_or_hugepage to finish.
  513. */
  514. kick_all_cpus_sync();
  515. return old_pmd;
  516. }
  517. int radix__has_transparent_hugepage(void)
  518. {
  519. /* For radix 2M at PMD level means thp */
  520. if (mmu_psize_defs[MMU_PAGE_2M].shift == PMD_SHIFT)
  521. return 1;
  522. return 0;
  523. }
  524. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */