1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Page table handling routines for radix page table.
5 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
8 #define pr_fmt(fmt) "radix-mmu: " fmt
11 #include <linux/kernel.h>
12 #include <linux/sched/mm.h>
13 #include <linux/memblock.h>
15 #include <linux/of_fdt.h>
17 #include <linux/hugetlb.h>
18 #include <linux/string_helpers.h>
19 #include <linux/memory.h>
21 #include <asm/pgalloc.h>
22 #include <asm/mmu_context.h>
24 #include <asm/machdep.h>
26 #include <asm/firmware.h>
27 #include <asm/powernv.h>
28 #include <asm/sections.h>
30 #include <asm/trace.h>
31 #include <asm/uaccess.h>
32 #include <asm/ultravisor.h>
34 #include <trace/events/thp.h>
36 unsigned int mmu_base_pid;
37 unsigned long radix_mem_block_size __ro_after_init;
39 static __ref void *early_alloc_pgtable(unsigned long size, int nid,
40 unsigned long region_start, unsigned long region_end)
42 phys_addr_t min_addr = MEMBLOCK_LOW_LIMIT;
43 phys_addr_t max_addr = MEMBLOCK_ALLOC_ANYWHERE;
47 min_addr = region_start;
49 max_addr = region_end;
51 ptr = memblock_alloc_try_nid(size, size, min_addr, max_addr, nid);
54 panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa max_addr=%pa\n",
55 __func__, size, size, nid, &min_addr, &max_addr);
61 * When allocating pud or pmd pointers, we allocate a complete page
62 * of PAGE_SIZE rather than PUD_TABLE_SIZE or PMD_TABLE_SIZE. This
63 * is to ensure that the page obtained from the memblock allocator
64 * can be completely used as page table page and can be freed
65 * correctly when the page table entries are removed.
67 static int early_map_kernel_page(unsigned long ea, unsigned long pa,
69 unsigned int map_page_size,
71 unsigned long region_start, unsigned long region_end)
73 unsigned long pfn = pa >> PAGE_SHIFT;
80 pgdp = pgd_offset_k(ea);
81 p4dp = p4d_offset(pgdp, ea);
82 if (p4d_none(*p4dp)) {
83 pudp = early_alloc_pgtable(PAGE_SIZE, nid,
84 region_start, region_end);
85 p4d_populate(&init_mm, p4dp, pudp);
87 pudp = pud_offset(p4dp, ea);
88 if (map_page_size == PUD_SIZE) {
92 if (pud_none(*pudp)) {
93 pmdp = early_alloc_pgtable(PAGE_SIZE, nid, region_start,
95 pud_populate(&init_mm, pudp, pmdp);
97 pmdp = pmd_offset(pudp, ea);
98 if (map_page_size == PMD_SIZE) {
99 ptep = pmdp_ptep(pmdp);
102 if (!pmd_present(*pmdp)) {
103 ptep = early_alloc_pgtable(PAGE_SIZE, nid,
104 region_start, region_end);
105 pmd_populate_kernel(&init_mm, pmdp, ptep);
107 ptep = pte_offset_kernel(pmdp, ea);
110 set_pte_at(&init_mm, ea, ptep, pfn_pte(pfn, flags));
111 asm volatile("ptesync": : :"memory");
116 * nid, region_start, and region_end are hints to try to place the page
117 * table memory in the same node or region.
119 static int __map_kernel_page(unsigned long ea, unsigned long pa,
121 unsigned int map_page_size,
123 unsigned long region_start, unsigned long region_end)
125 unsigned long pfn = pa >> PAGE_SHIFT;
132 * Make sure task size is correct as per the max adddr
134 BUILD_BUG_ON(TASK_SIZE_USER64 > RADIX_PGTABLE_RANGE);
136 #ifdef CONFIG_PPC_64K_PAGES
137 BUILD_BUG_ON(RADIX_KERN_MAP_SIZE != (1UL << MAX_EA_BITS_PER_CONTEXT));
140 if (unlikely(!slab_is_available()))
141 return early_map_kernel_page(ea, pa, flags, map_page_size,
142 nid, region_start, region_end);
145 * Should make page table allocation functions be able to take a
146 * node, so we can place kernel page tables on the right nodes after
149 pgdp = pgd_offset_k(ea);
150 p4dp = p4d_offset(pgdp, ea);
151 pudp = pud_alloc(&init_mm, p4dp, ea);
154 if (map_page_size == PUD_SIZE) {
155 ptep = (pte_t *)pudp;
158 pmdp = pmd_alloc(&init_mm, pudp, ea);
161 if (map_page_size == PMD_SIZE) {
162 ptep = pmdp_ptep(pmdp);
165 ptep = pte_alloc_kernel(pmdp, ea);
170 set_pte_at(&init_mm, ea, ptep, pfn_pte(pfn, flags));
171 asm volatile("ptesync": : :"memory");
175 int radix__map_kernel_page(unsigned long ea, unsigned long pa,
177 unsigned int map_page_size)
179 return __map_kernel_page(ea, pa, flags, map_page_size, -1, 0, 0);
182 #ifdef CONFIG_STRICT_KERNEL_RWX
183 static void radix__change_memory_range(unsigned long start, unsigned long end,
193 start = ALIGN_DOWN(start, PAGE_SIZE);
194 end = PAGE_ALIGN(end); // aligns up
196 pr_debug("Changing flags on range %lx-%lx removing 0x%lx\n",
199 for (idx = start; idx < end; idx += PAGE_SIZE) {
200 pgdp = pgd_offset_k(idx);
201 p4dp = p4d_offset(pgdp, idx);
202 pudp = pud_alloc(&init_mm, p4dp, idx);
205 if (pud_is_leaf(*pudp)) {
206 ptep = (pte_t *)pudp;
209 pmdp = pmd_alloc(&init_mm, pudp, idx);
212 if (pmd_is_leaf(*pmdp)) {
213 ptep = pmdp_ptep(pmdp);
216 ptep = pte_alloc_kernel(pmdp, idx);
220 radix__pte_update(&init_mm, idx, ptep, clear, 0, 0);
223 radix__flush_tlb_kernel_range(start, end);
226 void radix__mark_rodata_ro(void)
228 unsigned long start, end;
230 start = (unsigned long)_stext;
231 end = (unsigned long)__init_begin;
233 radix__change_memory_range(start, end, _PAGE_WRITE);
236 void radix__mark_initmem_nx(void)
238 unsigned long start = (unsigned long)__init_begin;
239 unsigned long end = (unsigned long)__init_end;
241 radix__change_memory_range(start, end, _PAGE_EXEC);
243 #endif /* CONFIG_STRICT_KERNEL_RWX */
245 static inline void __meminit
246 print_mapping(unsigned long start, unsigned long end, unsigned long size, bool exec)
253 string_get_size(size, 1, STRING_UNITS_2, buf, sizeof(buf));
255 pr_info("Mapped 0x%016lx-0x%016lx with %s pages%s\n", start, end, buf,
256 exec ? " (exec)" : "");
259 static unsigned long next_boundary(unsigned long addr, unsigned long end)
261 #ifdef CONFIG_STRICT_KERNEL_RWX
262 if (addr < __pa_symbol(__init_begin))
263 return __pa_symbol(__init_begin);
268 static int __meminit create_physical_mapping(unsigned long start,
270 unsigned long max_mapping_size,
271 int nid, pgprot_t _prot)
273 unsigned long vaddr, addr, mapping_size = 0;
274 bool prev_exec, exec = false;
278 start = ALIGN(start, PAGE_SIZE);
279 end = ALIGN_DOWN(end, PAGE_SIZE);
280 for (addr = start; addr < end; addr += mapping_size) {
281 unsigned long gap, previous_size;
284 gap = next_boundary(addr, end) - addr;
285 if (gap > max_mapping_size)
286 gap = max_mapping_size;
287 previous_size = mapping_size;
290 if (IS_ALIGNED(addr, PUD_SIZE) && gap >= PUD_SIZE &&
291 mmu_psize_defs[MMU_PAGE_1G].shift) {
292 mapping_size = PUD_SIZE;
294 } else if (IS_ALIGNED(addr, PMD_SIZE) && gap >= PMD_SIZE &&
295 mmu_psize_defs[MMU_PAGE_2M].shift) {
296 mapping_size = PMD_SIZE;
299 mapping_size = PAGE_SIZE;
300 psize = mmu_virtual_psize;
303 vaddr = (unsigned long)__va(addr);
305 if (overlaps_kernel_text(vaddr, vaddr + mapping_size) ||
306 overlaps_interrupt_vector_text(vaddr, vaddr + mapping_size)) {
307 prot = PAGE_KERNEL_X;
314 if (mapping_size != previous_size || exec != prev_exec) {
315 print_mapping(start, addr, previous_size, prev_exec);
319 rc = __map_kernel_page(vaddr, addr, prot, mapping_size, nid, start, end);
323 update_page_count(psize, 1);
326 print_mapping(start, addr, mapping_size, exec);
330 static void __init radix_init_pgtable(void)
332 unsigned long rts_field;
333 phys_addr_t start, end;
336 /* We don't support slb for radix */
340 * Create the linear mapping
342 for_each_mem_range(i, &start, &end) {
344 * The memblock allocator is up at this point, so the
345 * page tables will be allocated within the range. No
346 * need or a node (which we don't have yet).
349 if (end >= RADIX_VMALLOC_START) {
350 pr_warn("Outside the supported range\n");
354 WARN_ON(create_physical_mapping(start, end,
355 radix_mem_block_size,
359 if (!cpu_has_feature(CPU_FTR_HVMODE) &&
360 cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) {
362 * Older versions of KVM on these machines prefer if the
363 * guest only uses the low 19 PID bits.
370 * Allocate Partition table and process table for the
373 BUG_ON(PRTB_SIZE_SHIFT > 36);
374 process_tb = early_alloc_pgtable(1UL << PRTB_SIZE_SHIFT, -1, 0, 0);
376 * Fill in the process table.
378 rts_field = radix__get_tree_size();
379 process_tb->prtb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE);
382 * The init_mm context is given the first available (non-zero) PID,
383 * which is the "guard PID" and contains no page table. PIDR should
384 * never be set to zero because that duplicates the kernel address
385 * space at the 0x0... offset (quadrant 0)!
387 * An arbitrary PID that may later be allocated by the PID allocator
388 * for userspace processes must not be used either, because that
389 * would cause stale user mappings for that PID on CPUs outside of
390 * the TLB invalidation scheme (because it won't be in mm_cpumask).
392 * So permanently carve out one PID for the purpose of a guard PID.
394 init_mm.context.id = mmu_base_pid;
398 static void __init radix_init_partition_table(void)
400 unsigned long rts_field, dw0, dw1;
402 mmu_partition_table_init();
403 rts_field = radix__get_tree_size();
404 dw0 = rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE | PATB_HR;
405 dw1 = __pa(process_tb) | (PRTB_SIZE_SHIFT - 12) | PATB_GR;
406 mmu_partition_table_set_entry(0, dw0, dw1, false);
408 pr_info("Initializing Radix MMU\n");
411 static int __init get_idx_from_shift(unsigned int shift)
432 static int __init radix_dt_scan_page_sizes(unsigned long node,
433 const char *uname, int depth,
440 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
442 /* We are scanning "cpu" nodes only */
443 if (type == NULL || strcmp(type, "cpu") != 0)
446 /* Grab page size encodings */
447 prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
451 pr_info("Page sizes from device-tree:\n");
452 for (; size >= 4; size -= 4, ++prop) {
454 struct mmu_psize_def *def;
456 /* top 3 bit is AP encoding */
457 shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
458 ap = be32_to_cpu(prop[0]) >> 29;
459 pr_info("Page size shift = %d AP=0x%x\n", shift, ap);
461 idx = get_idx_from_shift(shift);
465 def = &mmu_psize_defs[idx];
468 def->h_rpt_pgsize = psize_to_rpti_pgsize(idx);
472 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
476 #ifdef CONFIG_MEMORY_HOTPLUG
477 static int __init probe_memory_block_size(unsigned long node, const char *uname, int
480 unsigned long *mem_block_size = (unsigned long *)data;
487 if (strcmp(uname, "ibm,dynamic-reconfiguration-memory"))
490 prop = of_get_flat_dt_prop(node, "ibm,lmb-size", &len);
492 if (!prop || len < dt_root_size_cells * sizeof(__be32))
494 * Nothing in the device tree
496 *mem_block_size = MIN_MEMORY_BLOCK_SIZE;
498 *mem_block_size = of_read_number(prop, dt_root_size_cells);
502 static unsigned long __init radix_memory_block_size(void)
504 unsigned long mem_block_size = MIN_MEMORY_BLOCK_SIZE;
507 * OPAL firmware feature is set by now. Hence we are ok
508 * to test OPAL feature.
510 if (firmware_has_feature(FW_FEATURE_OPAL))
511 mem_block_size = 1UL * 1024 * 1024 * 1024;
513 of_scan_flat_dt(probe_memory_block_size, &mem_block_size);
515 return mem_block_size;
518 #else /* CONFIG_MEMORY_HOTPLUG */
520 static unsigned long __init radix_memory_block_size(void)
522 return 1UL * 1024 * 1024 * 1024;
525 #endif /* CONFIG_MEMORY_HOTPLUG */
528 void __init radix__early_init_devtree(void)
533 * Try to find the available page sizes in the device-tree
535 rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
538 * No page size details found in device tree.
539 * Let's assume we have page 4k and 64k support
541 mmu_psize_defs[MMU_PAGE_4K].shift = 12;
542 mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
543 mmu_psize_defs[MMU_PAGE_4K].h_rpt_pgsize =
544 psize_to_rpti_pgsize(MMU_PAGE_4K);
546 mmu_psize_defs[MMU_PAGE_64K].shift = 16;
547 mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
548 mmu_psize_defs[MMU_PAGE_64K].h_rpt_pgsize =
549 psize_to_rpti_pgsize(MMU_PAGE_64K);
553 * Max mapping size used when mapping pages. We don't use
554 * ppc_md.memory_block_size() here because this get called
555 * early and we don't have machine probe called yet. Also
556 * the pseries implementation only check for ibm,lmb-size.
557 * All hypervisor supporting radix do expose that device
560 radix_mem_block_size = radix_memory_block_size();
564 void __init radix__early_init_mmu(void)
568 #ifdef CONFIG_PPC_64S_HASH_MMU
569 #ifdef CONFIG_PPC_64K_PAGES
570 /* PAGE_SIZE mappings */
571 mmu_virtual_psize = MMU_PAGE_64K;
573 mmu_virtual_psize = MMU_PAGE_4K;
576 #ifdef CONFIG_SPARSEMEM_VMEMMAP
577 /* vmemmap mapping */
578 if (mmu_psize_defs[MMU_PAGE_2M].shift) {
580 * map vmemmap using 2M if available
582 mmu_vmemmap_psize = MMU_PAGE_2M;
584 mmu_vmemmap_psize = mmu_virtual_psize;
588 * initialize page table size
590 __pte_index_size = RADIX_PTE_INDEX_SIZE;
591 __pmd_index_size = RADIX_PMD_INDEX_SIZE;
592 __pud_index_size = RADIX_PUD_INDEX_SIZE;
593 __pgd_index_size = RADIX_PGD_INDEX_SIZE;
594 __pud_cache_index = RADIX_PUD_INDEX_SIZE;
595 __pte_table_size = RADIX_PTE_TABLE_SIZE;
596 __pmd_table_size = RADIX_PMD_TABLE_SIZE;
597 __pud_table_size = RADIX_PUD_TABLE_SIZE;
598 __pgd_table_size = RADIX_PGD_TABLE_SIZE;
600 __pmd_val_bits = RADIX_PMD_VAL_BITS;
601 __pud_val_bits = RADIX_PUD_VAL_BITS;
602 __pgd_val_bits = RADIX_PGD_VAL_BITS;
604 __kernel_virt_start = RADIX_KERN_VIRT_START;
605 __vmalloc_start = RADIX_VMALLOC_START;
606 __vmalloc_end = RADIX_VMALLOC_END;
607 __kernel_io_start = RADIX_KERN_IO_START;
608 __kernel_io_end = RADIX_KERN_IO_END;
609 vmemmap = (struct page *)RADIX_VMEMMAP_START;
610 ioremap_bot = IOREMAP_BASE;
613 pci_io_base = ISA_IO_BASE;
615 __pte_frag_nr = RADIX_PTE_FRAG_NR;
616 __pte_frag_size_shift = RADIX_PTE_FRAG_SIZE_SHIFT;
617 __pmd_frag_nr = RADIX_PMD_FRAG_NR;
618 __pmd_frag_size_shift = RADIX_PMD_FRAG_SIZE_SHIFT;
620 radix_init_pgtable();
622 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
623 lpcr = mfspr(SPRN_LPCR);
624 mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
625 radix_init_partition_table();
627 radix_init_pseries();
630 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
632 /* Switch to the guard PID before turning on MMU */
633 radix__switch_mmu_context(NULL, &init_mm);
637 void radix__early_init_mmu_secondary(void)
641 * update partition table control register and UPRT
643 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
644 lpcr = mfspr(SPRN_LPCR);
645 mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
647 set_ptcr_when_no_uv(__pa(partition_tb) |
648 (PATB_SIZE_SHIFT - 12));
651 radix__switch_mmu_context(NULL, &init_mm);
654 /* Make sure userspace can't change the AMR */
655 mtspr(SPRN_UAMOR, 0);
658 /* Called during kexec sequence with MMU off */
659 notrace void radix__mmu_cleanup_all(void)
663 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
664 lpcr = mfspr(SPRN_LPCR);
665 mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
666 set_ptcr_when_no_uv(0);
667 powernv_set_nmmu_ptcr(0);
668 radix__flush_tlb_all();
672 #ifdef CONFIG_MEMORY_HOTPLUG
673 static void free_pte_table(pte_t *pte_start, pmd_t *pmd)
678 for (i = 0; i < PTRS_PER_PTE; i++) {
684 pte_free_kernel(&init_mm, pte_start);
688 static void free_pmd_table(pmd_t *pmd_start, pud_t *pud)
693 for (i = 0; i < PTRS_PER_PMD; i++) {
699 pmd_free(&init_mm, pmd_start);
703 static void free_pud_table(pud_t *pud_start, p4d_t *p4d)
708 for (i = 0; i < PTRS_PER_PUD; i++) {
714 pud_free(&init_mm, pud_start);
718 static void remove_pte_table(pte_t *pte_start, unsigned long addr,
724 pte = pte_start + pte_index(addr);
725 for (; addr < end; addr = next, pte++) {
726 next = (addr + PAGE_SIZE) & PAGE_MASK;
730 if (!pte_present(*pte))
733 if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(next)) {
735 * The vmemmap_free() and remove_section_mapping()
736 * codepaths call us with aligned addresses.
738 WARN_ONCE(1, "%s: unaligned range\n", __func__);
742 pte_clear(&init_mm, addr, pte);
746 static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
753 pmd = pmd_start + pmd_index(addr);
754 for (; addr < end; addr = next, pmd++) {
755 next = pmd_addr_end(addr, end);
757 if (!pmd_present(*pmd))
760 if (pmd_is_leaf(*pmd)) {
761 if (!IS_ALIGNED(addr, PMD_SIZE) ||
762 !IS_ALIGNED(next, PMD_SIZE)) {
763 WARN_ONCE(1, "%s: unaligned range\n", __func__);
766 pte_clear(&init_mm, addr, (pte_t *)pmd);
770 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
771 remove_pte_table(pte_base, addr, next);
772 free_pte_table(pte_base, pmd);
776 static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
783 pud = pud_start + pud_index(addr);
784 for (; addr < end; addr = next, pud++) {
785 next = pud_addr_end(addr, end);
787 if (!pud_present(*pud))
790 if (pud_is_leaf(*pud)) {
791 if (!IS_ALIGNED(addr, PUD_SIZE) ||
792 !IS_ALIGNED(next, PUD_SIZE)) {
793 WARN_ONCE(1, "%s: unaligned range\n", __func__);
796 pte_clear(&init_mm, addr, (pte_t *)pud);
800 pmd_base = pud_pgtable(*pud);
801 remove_pmd_table(pmd_base, addr, next);
802 free_pmd_table(pmd_base, pud);
806 static void __meminit remove_pagetable(unsigned long start, unsigned long end)
808 unsigned long addr, next;
813 spin_lock(&init_mm.page_table_lock);
815 for (addr = start; addr < end; addr = next) {
816 next = pgd_addr_end(addr, end);
818 pgd = pgd_offset_k(addr);
819 p4d = p4d_offset(pgd, addr);
820 if (!p4d_present(*p4d))
823 if (p4d_is_leaf(*p4d)) {
824 if (!IS_ALIGNED(addr, P4D_SIZE) ||
825 !IS_ALIGNED(next, P4D_SIZE)) {
826 WARN_ONCE(1, "%s: unaligned range\n", __func__);
830 pte_clear(&init_mm, addr, (pte_t *)pgd);
834 pud_base = p4d_pgtable(*p4d);
835 remove_pud_table(pud_base, addr, next);
836 free_pud_table(pud_base, p4d);
839 spin_unlock(&init_mm.page_table_lock);
840 radix__flush_tlb_kernel_range(start, end);
843 int __meminit radix__create_section_mapping(unsigned long start,
844 unsigned long end, int nid,
847 if (end >= RADIX_VMALLOC_START) {
848 pr_warn("Outside the supported range\n");
852 return create_physical_mapping(__pa(start), __pa(end),
853 radix_mem_block_size, nid, prot);
856 int __meminit radix__remove_section_mapping(unsigned long start, unsigned long end)
858 remove_pagetable(start, end);
861 #endif /* CONFIG_MEMORY_HOTPLUG */
863 #ifdef CONFIG_SPARSEMEM_VMEMMAP
864 static int __map_kernel_page_nid(unsigned long ea, unsigned long pa,
865 pgprot_t flags, unsigned int map_page_size,
868 return __map_kernel_page(ea, pa, flags, map_page_size, nid, 0, 0);
871 int __meminit radix__vmemmap_create_mapping(unsigned long start,
872 unsigned long page_size,
875 /* Create a PTE encoding */
876 unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
877 int nid = early_pfn_to_nid(phys >> PAGE_SHIFT);
880 if ((start + page_size) >= RADIX_VMEMMAP_END) {
881 pr_warn("Outside the supported range\n");
885 ret = __map_kernel_page_nid(start, phys, __pgprot(flags), page_size, nid);
891 #ifdef CONFIG_MEMORY_HOTPLUG
892 void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
894 remove_pagetable(start, start + page_size);
899 #ifdef CONFIG_DEBUG_PAGEALLOC
900 void radix__kernel_map_pages(struct page *page, int numpages, int enable)
902 pr_warn_once("DEBUG_PAGEALLOC not supported in radix mode\n");
906 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
908 unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
909 pmd_t *pmdp, unsigned long clr,
914 #ifdef CONFIG_DEBUG_VM
915 WARN_ON(!radix__pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
916 assert_spin_locked(pmd_lockptr(mm, pmdp));
919 old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
920 trace_hugepage_update(addr, old, clr, set);
925 pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
931 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
932 VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
933 VM_BUG_ON(pmd_devmap(*pmdp));
935 * khugepaged calls this for normal pmd
941 * pmdp collapse_flush need to ensure that there are no parallel gup
942 * walk after this call. This is needed so that we can have stable
943 * page ref count when collapsing a page. We don't allow a collapse page
944 * if we have gup taken on the page. We can ensure that by sending IPI
945 * because gup walk happens with IRQ disabled.
947 serialize_against_pte_lookup(vma->vm_mm);
949 radix__flush_tlb_collapsed_pmd(vma->vm_mm, address);
955 * For us pgtable_t is pte_t *. Inorder to save the deposisted
956 * page table, we consider the allocated page table as a list
957 * head. On withdraw we need to make sure we zero out the used
958 * list_head memory area.
960 void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
963 struct list_head *lh = (struct list_head *) pgtable;
965 assert_spin_locked(pmd_lockptr(mm, pmdp));
968 if (!pmd_huge_pte(mm, pmdp))
971 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
972 pmd_huge_pte(mm, pmdp) = pgtable;
975 pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
979 struct list_head *lh;
981 assert_spin_locked(pmd_lockptr(mm, pmdp));
984 pgtable = pmd_huge_pte(mm, pmdp);
985 lh = (struct list_head *) pgtable;
987 pmd_huge_pte(mm, pmdp) = NULL;
989 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
992 ptep = (pte_t *) pgtable;
999 pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
1000 unsigned long addr, pmd_t *pmdp)
1005 old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
1006 old_pmd = __pmd(old);
1010 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1012 void radix__ptep_set_access_flags(struct vm_area_struct *vma, pte_t *ptep,
1013 pte_t entry, unsigned long address, int psize)
1015 struct mm_struct *mm = vma->vm_mm;
1016 unsigned long set = pte_val(entry) & (_PAGE_DIRTY | _PAGE_ACCESSED |
1017 _PAGE_RW | _PAGE_EXEC);
1019 unsigned long change = pte_val(entry) ^ pte_val(*ptep);
1021 * To avoid NMMU hang while relaxing access, we need mark
1022 * the pte invalid in between.
1024 if ((change & _PAGE_RW) && atomic_read(&mm->context.copros) > 0) {
1025 unsigned long old_pte, new_pte;
1027 old_pte = __radix_pte_update(ptep, _PAGE_PRESENT, _PAGE_INVALID);
1031 new_pte = old_pte | set;
1032 radix__flush_tlb_page_psize(mm, address, psize);
1033 __radix_pte_update(ptep, _PAGE_INVALID, new_pte);
1035 __radix_pte_update(ptep, 0, set);
1037 * Book3S does not require a TLB flush when relaxing access
1038 * restrictions when the address space is not attached to a
1039 * NMMU, because the core MMU will reload the pte after taking
1040 * an access fault, which is defined by the architecture.
1043 /* See ptesync comment in radix__set_pte_at */
1046 void radix__ptep_modify_prot_commit(struct vm_area_struct *vma,
1047 unsigned long addr, pte_t *ptep,
1048 pte_t old_pte, pte_t pte)
1050 struct mm_struct *mm = vma->vm_mm;
1053 * To avoid NMMU hang while relaxing access we need to flush the tlb before
1054 * we set the new value. We need to do this only for radix, because hash
1055 * translation does flush when updating the linux pte.
1057 if (is_pte_rw_upgrade(pte_val(old_pte), pte_val(pte)) &&
1058 (atomic_read(&mm->context.copros) > 0))
1059 radix__flush_tlb_page(vma, addr);
1061 set_pte_at(mm, addr, ptep, pte);
1064 int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
1066 pte_t *ptep = (pte_t *)pud;
1067 pte_t new_pud = pfn_pte(__phys_to_pfn(addr), prot);
1069 if (!radix_enabled())
1072 set_pte_at(&init_mm, 0 /* radix unused */, ptep, new_pud);
1077 int pud_clear_huge(pud_t *pud)
1079 if (pud_is_leaf(*pud)) {
1087 int pud_free_pmd_page(pud_t *pud, unsigned long addr)
1092 pmd = pud_pgtable(*pud);
1095 flush_tlb_kernel_range(addr, addr + PUD_SIZE);
1097 for (i = 0; i < PTRS_PER_PMD; i++) {
1098 if (!pmd_none(pmd[i])) {
1100 pte = (pte_t *)pmd_page_vaddr(pmd[i]);
1102 pte_free_kernel(&init_mm, pte);
1106 pmd_free(&init_mm, pmd);
1111 int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
1113 pte_t *ptep = (pte_t *)pmd;
1114 pte_t new_pmd = pfn_pte(__phys_to_pfn(addr), prot);
1116 if (!radix_enabled())
1119 set_pte_at(&init_mm, 0 /* radix unused */, ptep, new_pmd);
1124 int pmd_clear_huge(pmd_t *pmd)
1126 if (pmd_is_leaf(*pmd)) {
1134 int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
1138 pte = (pte_t *)pmd_page_vaddr(*pmd);
1141 flush_tlb_kernel_range(addr, addr + PMD_SIZE);
1143 pte_free_kernel(&init_mm, pte);