2 * Based on arch/arm/mm/mmu.c
4 * Copyright (C) 1995-2005 Russell King
5 * Copyright (C) 2012 ARM Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/export.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/libfdt.h>
25 #include <linux/mman.h>
26 #include <linux/nodemask.h>
27 #include <linux/memblock.h>
30 #include <linux/slab.h>
31 #include <linux/stop_machine.h>
33 #include <asm/barrier.h>
34 #include <asm/cputype.h>
35 #include <asm/fixmap.h>
36 #include <asm/kasan.h>
37 #include <asm/kernel-pgtable.h>
38 #include <asm/sections.h>
39 #include <asm/setup.h>
40 #include <asm/sizes.h>
42 #include <asm/memblock.h>
43 #include <asm/mmu_context.h>
47 u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
50 * Empty_zero_page is a special page that is used for zero-initialized data
53 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
54 EXPORT_SYMBOL(empty_zero_page);
56 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
57 unsigned long size, pgprot_t vma_prot)
60 return pgprot_noncached(vma_prot);
61 else if (file->f_flags & O_SYNC)
62 return pgprot_writecombine(vma_prot);
65 EXPORT_SYMBOL(phys_mem_access_prot);
67 static phys_addr_t __init early_pgtable_alloc(void)
72 phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
76 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
77 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
80 ptr = pte_set_fixmap(phys);
82 memset(ptr, 0, PAGE_SIZE);
85 * Implicit barriers also ensure the zeroed page is visible to the page
94 * remap a PMD into pages
96 static void split_pmd(pmd_t *pmd, pte_t *pte)
98 unsigned long pfn = pmd_pfn(*pmd);
103 * Need to have the least restrictive permissions available
104 * permissions will be fixed up later
106 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
108 } while (pte++, i++, i < PTRS_PER_PTE);
111 static void alloc_init_pte(pmd_t *pmd, unsigned long addr,
112 unsigned long end, unsigned long pfn,
114 phys_addr_t (*pgtable_alloc)(void))
118 if (pmd_none(*pmd) || pmd_sect(*pmd)) {
119 phys_addr_t pte_phys;
120 BUG_ON(!pgtable_alloc);
121 pte_phys = pgtable_alloc();
122 pte = pte_set_fixmap(pte_phys);
125 __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
129 BUG_ON(pmd_bad(*pmd));
131 pte = pte_set_fixmap_offset(pmd, addr);
133 set_pte(pte, pfn_pte(pfn, prot));
135 } while (pte++, addr += PAGE_SIZE, addr != end);
140 static void split_pud(pud_t *old_pud, pmd_t *pmd)
142 unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT;
143 pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr);
147 set_pmd(pmd, __pmd(addr | pgprot_val(prot)));
149 } while (pmd++, i++, i < PTRS_PER_PMD);
152 #ifdef CONFIG_DEBUG_PAGEALLOC
153 static bool block_mappings_allowed(phys_addr_t (*pgtable_alloc)(void))
157 * If debug_page_alloc is enabled we must map the linear map
158 * using pages. However, other mappings created by
159 * create_mapping_noalloc must use sections in some cases. Allow
160 * sections to be used in those cases, where no pgtable_alloc
161 * function is provided.
163 return !pgtable_alloc || !debug_pagealloc_enabled();
166 static bool block_mappings_allowed(phys_addr_t (*pgtable_alloc)(void))
172 static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
173 phys_addr_t phys, pgprot_t prot,
174 phys_addr_t (*pgtable_alloc)(void))
180 * Check for initial section mappings in the pgd/pud and remove them.
182 if (pud_none(*pud) || pud_sect(*pud)) {
183 phys_addr_t pmd_phys;
184 BUG_ON(!pgtable_alloc);
185 pmd_phys = pgtable_alloc();
186 pmd = pmd_set_fixmap(pmd_phys);
187 if (pud_sect(*pud)) {
189 * need to have the 1G of mappings continue to be
194 __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
198 BUG_ON(pud_bad(*pud));
200 pmd = pmd_set_fixmap_offset(pud, addr);
202 next = pmd_addr_end(addr, end);
203 /* try section mapping first */
204 if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
205 block_mappings_allowed(pgtable_alloc)) {
207 set_pmd(pmd, __pmd(phys |
208 pgprot_val(mk_sect_prot(prot))));
210 * Check for previous table entries created during
211 * boot (__create_page_tables) and flush them.
213 if (!pmd_none(old_pmd)) {
215 if (pmd_table(old_pmd)) {
216 phys_addr_t table = pmd_page_paddr(old_pmd);
217 if (!WARN_ON_ONCE(slab_is_available()))
218 memblock_free(table, PAGE_SIZE);
222 alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
223 prot, pgtable_alloc);
226 } while (pmd++, addr = next, addr != end);
231 static inline bool use_1G_block(unsigned long addr, unsigned long next,
234 if (PAGE_SHIFT != 12)
237 if (((addr | next | phys) & ~PUD_MASK) != 0)
243 static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
244 phys_addr_t phys, pgprot_t prot,
245 phys_addr_t (*pgtable_alloc)(void))
250 if (pgd_none(*pgd)) {
251 phys_addr_t pud_phys;
252 BUG_ON(!pgtable_alloc);
253 pud_phys = pgtable_alloc();
254 __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
256 BUG_ON(pgd_bad(*pgd));
258 pud = pud_set_fixmap_offset(pgd, addr);
260 next = pud_addr_end(addr, end);
263 * For 4K granule only, attempt to put down a 1GB block
265 if (use_1G_block(addr, next, phys) &&
266 block_mappings_allowed(pgtable_alloc)) {
267 pud_t old_pud = *pud;
268 set_pud(pud, __pud(phys |
269 pgprot_val(mk_sect_prot(prot))));
272 * If we have an old value for a pud, it will
273 * be pointing to a pmd table that we no longer
274 * need (from swapper_pg_dir).
276 * Look up the old pmd table and free it.
278 if (!pud_none(old_pud)) {
280 if (pud_table(old_pud)) {
281 phys_addr_t table = pud_page_paddr(old_pud);
282 if (!WARN_ON_ONCE(slab_is_available()))
283 memblock_free(table, PAGE_SIZE);
287 alloc_init_pmd(pud, addr, next, phys, prot,
291 } while (pud++, addr = next, addr != end);
297 * Create the page directory entries and any necessary page tables for the
298 * mapping specified by 'md'.
300 static void init_pgd(pgd_t *pgd, phys_addr_t phys, unsigned long virt,
301 phys_addr_t size, pgprot_t prot,
302 phys_addr_t (*pgtable_alloc)(void))
304 unsigned long addr, length, end, next;
307 * If the virtual and physical address don't have the same offset
308 * within a page, we cannot map the region as the caller expects.
310 if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
314 addr = virt & PAGE_MASK;
315 length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
319 next = pgd_addr_end(addr, end);
320 alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc);
322 } while (pgd++, addr = next, addr != end);
325 static phys_addr_t late_pgtable_alloc(void)
327 void *ptr = (void *)__get_free_page(PGALLOC_GFP);
330 /* Ensure the zeroed page is visible to the page table walker */
335 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
336 unsigned long virt, phys_addr_t size,
338 phys_addr_t (*alloc)(void))
340 init_pgd(pgd_offset_raw(pgdir, virt), phys, virt, size, prot, alloc);
344 * This function can only be used to modify existing table entries,
345 * without allocating new levels of table. Note that this permits the
346 * creation of new section or page entries.
348 static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
349 phys_addr_t size, pgprot_t prot)
351 if (virt < VMALLOC_START) {
352 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
356 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
360 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
361 unsigned long virt, phys_addr_t size,
364 __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
368 static void create_mapping_late(phys_addr_t phys, unsigned long virt,
369 phys_addr_t size, pgprot_t prot)
371 if (virt < VMALLOC_START) {
372 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
377 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
381 static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
384 unsigned long kernel_start = __pa(_stext);
385 unsigned long kernel_end = __pa(_end);
388 * The kernel itself is mapped at page granularity. Map all other
389 * memory, making sure we don't overwrite the existing kernel mappings.
392 /* No overlap with the kernel. */
393 if (end < kernel_start || start >= kernel_end) {
394 __create_pgd_mapping(pgd, start, __phys_to_virt(start),
395 end - start, PAGE_KERNEL,
396 early_pgtable_alloc);
401 * This block overlaps the kernel mapping. Map the portion(s) which
404 if (start < kernel_start)
405 __create_pgd_mapping(pgd, start,
406 __phys_to_virt(start),
407 kernel_start - start, PAGE_KERNEL,
408 early_pgtable_alloc);
409 if (kernel_end < end)
410 __create_pgd_mapping(pgd, kernel_end,
411 __phys_to_virt(kernel_end),
412 end - kernel_end, PAGE_KERNEL,
413 early_pgtable_alloc);
416 static void __init map_mem(pgd_t *pgd)
418 struct memblock_region *reg;
420 /* map all the memory banks */
421 for_each_memblock(memory, reg) {
422 phys_addr_t start = reg->base;
423 phys_addr_t end = start + reg->size;
428 __map_memblock(pgd, start, end);
432 #ifdef CONFIG_DEBUG_RODATA
433 void mark_rodata_ro(void)
435 create_mapping_late(__pa(_stext), (unsigned long)_stext,
436 (unsigned long)_etext - (unsigned long)_stext,
442 void fixup_init(void)
444 create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin,
445 (unsigned long)__init_end - (unsigned long)__init_begin,
449 static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end,
452 phys_addr_t pa_start = __pa(va_start);
453 unsigned long size = va_end - va_start;
455 BUG_ON(!PAGE_ALIGNED(pa_start));
456 BUG_ON(!PAGE_ALIGNED(size));
458 __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
459 early_pgtable_alloc);
463 * Create fine-grained mappings for the kernel.
465 static void __init map_kernel(pgd_t *pgd)
468 map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC);
469 map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC);
470 map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL);
473 * The fixmap falls in a separate pgd to the kernel, and doesn't live
474 * in the carveout for the swapper_pg_dir. We can simply re-use the
475 * existing dir for the fixmap.
477 set_pgd(pgd_offset_raw(pgd, FIXADDR_START), *pgd_offset_k(FIXADDR_START));
479 kasan_copy_shadow(pgd);
483 * paging_init() sets up the page tables, initialises the zone memory
484 * maps and sets up the zero page.
486 void __init paging_init(void)
488 phys_addr_t pgd_phys = early_pgtable_alloc();
489 pgd_t *pgd = pgd_set_fixmap(pgd_phys);
495 * We want to reuse the original swapper_pg_dir so we don't have to
496 * communicate the new address to non-coherent secondaries in
497 * secondary_entry, and so cpu_switch_mm can generate the address with
498 * adrp+add rather than a load from some global variable.
500 * To do this we need to go via a temporary pgd.
502 cpu_replace_ttbr1(__va(pgd_phys));
503 memcpy(swapper_pg_dir, pgd, PAGE_SIZE);
504 cpu_replace_ttbr1(swapper_pg_dir);
507 memblock_free(pgd_phys, PAGE_SIZE);
510 * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
513 memblock_free(__pa(swapper_pg_dir) + PAGE_SIZE,
514 SWAPPER_DIR_SIZE - PAGE_SIZE);
520 * Check whether a kernel address is valid (derived from arch/x86/).
522 int kern_addr_valid(unsigned long addr)
529 if ((((long)addr) >> VA_BITS) != -1UL)
532 pgd = pgd_offset_k(addr);
536 pud = pud_offset(pgd, addr);
541 return pfn_valid(pud_pfn(*pud));
543 pmd = pmd_offset(pud, addr);
548 return pfn_valid(pmd_pfn(*pmd));
550 pte = pte_offset_kernel(pmd, addr);
554 return pfn_valid(pte_pfn(*pte));
556 #ifdef CONFIG_SPARSEMEM_VMEMMAP
557 #if !ARM64_SWAPPER_USES_SECTION_MAPS
558 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
560 return vmemmap_populate_basepages(start, end, node);
562 #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */
563 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
565 unsigned long addr = start;
572 next = pmd_addr_end(addr, end);
574 pgd = vmemmap_pgd_populate(addr, node);
578 pud = vmemmap_pud_populate(pgd, addr, node);
582 pmd = pmd_offset(pud, addr);
583 if (pmd_none(*pmd)) {
586 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
590 set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
592 vmemmap_verify((pte_t *)pmd, node, addr, next);
593 } while (addr = next, addr != end);
597 #endif /* CONFIG_ARM64_64K_PAGES */
598 void vmemmap_free(unsigned long start, unsigned long end)
601 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
603 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
604 #if CONFIG_PGTABLE_LEVELS > 2
605 static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss;
607 #if CONFIG_PGTABLE_LEVELS > 3
608 static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss;
611 static inline pud_t * fixmap_pud(unsigned long addr)
613 pgd_t *pgd = pgd_offset_k(addr);
615 BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
617 return pud_offset(pgd, addr);
620 static inline pmd_t * fixmap_pmd(unsigned long addr)
622 pud_t *pud = fixmap_pud(addr);
624 BUG_ON(pud_none(*pud) || pud_bad(*pud));
626 return pmd_offset(pud, addr);
629 static inline pte_t * fixmap_pte(unsigned long addr)
631 pmd_t *pmd = fixmap_pmd(addr);
633 BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
635 return pte_offset_kernel(pmd, addr);
638 void __init early_fixmap_init(void)
643 unsigned long addr = FIXADDR_START;
645 pgd = pgd_offset_k(addr);
646 pgd_populate(&init_mm, pgd, bm_pud);
647 pud = pud_offset(pgd, addr);
648 pud_populate(&init_mm, pud, bm_pmd);
649 pmd = pmd_offset(pud, addr);
650 pmd_populate_kernel(&init_mm, pmd, bm_pte);
653 * The boot-ioremap range spans multiple pmds, for which
654 * we are not preparted:
656 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
657 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
659 if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
660 || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
662 pr_warn("pmd %p != %p, %p\n",
663 pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
664 fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
665 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
666 fix_to_virt(FIX_BTMAP_BEGIN));
667 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
668 fix_to_virt(FIX_BTMAP_END));
670 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
671 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
675 void __set_fixmap(enum fixed_addresses idx,
676 phys_addr_t phys, pgprot_t flags)
678 unsigned long addr = __fix_to_virt(idx);
681 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
683 pte = fixmap_pte(addr);
685 if (pgprot_val(flags)) {
686 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
688 pte_clear(&init_mm, addr, pte);
689 flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
693 void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
695 const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
696 pgprot_t prot = PAGE_KERNEL_RO;
701 * Check whether the physical FDT address is set and meets the minimum
702 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
703 * at least 8 bytes so that we can always access the size field of the
704 * FDT header after mapping the first chunk, double check here if that
705 * is indeed the case.
707 BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
708 if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
712 * Make sure that the FDT region can be mapped without the need to
713 * allocate additional translation table pages, so that it is safe
714 * to call create_mapping_noalloc() this early.
716 * On 64k pages, the FDT will be mapped using PTEs, so we need to
717 * be in the same PMD as the rest of the fixmap.
718 * On 4k pages, we'll use section mappings for the FDT so we only
719 * have to be in the same PUD.
721 BUILD_BUG_ON(dt_virt_base % SZ_2M);
723 BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
724 __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
726 offset = dt_phys % SWAPPER_BLOCK_SIZE;
727 dt_virt = (void *)dt_virt_base + offset;
729 /* map the first chunk so we can read the size from the header */
730 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
731 dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
733 if (fdt_check_header(dt_virt) != 0)
736 size = fdt_totalsize(dt_virt);
737 if (size > MAX_FDT_SIZE)
740 if (offset + size > SWAPPER_BLOCK_SIZE)
741 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
742 round_up(offset + size, SWAPPER_BLOCK_SIZE), prot);
744 memblock_reserve(dt_phys, size);