1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2005, Paul Mackerras, IBM Corporation.
4 * Copyright 2009, Benjamin Herrenschmidt, IBM Corporation.
5 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
8 #include <linux/sched.h>
9 #include <linux/memblock.h>
10 #include <asm/pgalloc.h>
13 #include <asm/code-patching.h>
15 #include <mm/mmu_decl.h>
17 #ifdef CONFIG_SPARSEMEM_VMEMMAP
19 * On Book3E CPUs, the vmemmap is currently mapped in the top half of
20 * the vmalloc space using normal page tables, though the size of
21 * pages encoded in the PTEs can be different
23 int __meminit vmemmap_create_mapping(unsigned long start,
24 unsigned long page_size,
27 /* Create a PTE encoding without page size */
28 unsigned long i, flags = _PAGE_PRESENT | _PAGE_ACCESSED |
31 /* PTEs only contain page size encodings up to 32M */
32 BUG_ON(mmu_psize_defs[mmu_vmemmap_psize].enc > 0xf);
34 /* Encode the size in the PTE */
35 flags |= mmu_psize_defs[mmu_vmemmap_psize].enc << 8;
37 /* For each PTE for that area, map things. Note that we don't
38 * increment phys because all PTEs are of the large size and
39 * thus must have the low bits clear
41 for (i = 0; i < page_size; i += PAGE_SIZE)
42 BUG_ON(map_kernel_page(start + i, phys, __pgprot(flags)));
47 #ifdef CONFIG_MEMORY_HOTPLUG
48 void vmemmap_remove_mapping(unsigned long start,
49 unsigned long page_size)
53 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
55 static void __init *early_alloc_pgtable(unsigned long size)
59 ptr = memblock_alloc_try_nid(size, size, MEMBLOCK_LOW_LIMIT,
60 __pa(MAX_DMA_ADDRESS), NUMA_NO_NODE);
63 panic("%s: Failed to allocate %lu bytes align=0x%lx max_addr=%lx\n",
64 __func__, size, size, __pa(MAX_DMA_ADDRESS));
70 * map_kernel_page currently only called by __ioremap
71 * map_kernel_page adds an entry to the ioremap page table
72 * and adds an entry to the HPT, possibly bolting it
74 int __ref map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot)
82 BUILD_BUG_ON(TASK_SIZE_USER64 > PGTABLE_RANGE);
83 if (slab_is_available()) {
84 pgdp = pgd_offset_k(ea);
85 p4dp = p4d_offset(pgdp, ea);
86 pudp = pud_alloc(&init_mm, p4dp, ea);
89 pmdp = pmd_alloc(&init_mm, pudp, ea);
92 ptep = pte_alloc_kernel(pmdp, ea);
96 pgdp = pgd_offset_k(ea);
97 p4dp = p4d_offset(pgdp, ea);
98 if (p4d_none(*p4dp)) {
99 pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
100 p4d_populate(&init_mm, p4dp, pmdp);
102 pudp = pud_offset(p4dp, ea);
103 if (pud_none(*pudp)) {
104 pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
105 pud_populate(&init_mm, pudp, pmdp);
107 pmdp = pmd_offset(pudp, ea);
108 if (!pmd_present(*pmdp)) {
109 ptep = early_alloc_pgtable(PAGE_SIZE);
110 pmd_populate_kernel(&init_mm, pmdp, ptep);
112 ptep = pte_offset_kernel(pmdp, ea);
114 set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, prot));
120 void __patch_exception(int exc, unsigned long addr)
122 unsigned int *ibase = &interrupt_base_book3e;
125 * Our exceptions vectors start with a NOP and -then- a branch
126 * to deal with single stepping from userspace which stops on
127 * the second instruction. Thus we need to patch the second
128 * instruction of the exception, not the first one.
131 patch_branch(ibase + (exc / 4) + 1, addr, 0);