Thomas Gleixner | 50acfb2 | 2019-05-29 07:18:00 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Regents of the University of California |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 4 | * Copyright (C) 2019 Western Digital Corporation or its affiliates. |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/mm.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 9 | #include <linux/memblock.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 10 | #include <linux/initrd.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 11 | #include <linux/swap.h> |
Christoph Hellwig | 5ec9c4f | 2018-01-16 09:37:50 +0100 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 13 | #include <linux/of_fdt.h> |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 14 | #include <linux/libfdt.h> |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 15 | #include <linux/set_memory.h> |
Kefeng Wang | da81558 | 2020-10-31 14:01:12 +0800 | [diff] [blame] | 16 | #include <linux/dma-map-ops.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 17 | |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 18 | #include <asm/fixmap.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 19 | #include <asm/tlbflush.h> |
| 20 | #include <asm/sections.h> |
Palmer Dabbelt | 2d26825 | 2020-04-14 13:43:24 +0900 | [diff] [blame] | 21 | #include <asm/soc.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 22 | #include <asm/io.h> |
Zong Li | b422d28 | 2020-06-03 16:03:55 -0700 | [diff] [blame] | 23 | #include <asm/ptdump.h> |
Atish Patra | 4f0e8ee | 2020-11-18 16:38:29 -0800 | [diff] [blame] | 24 | #include <asm/numa.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 25 | |
Paul Walmsley | ffaee27 | 2019-10-17 15:00:17 -0700 | [diff] [blame] | 26 | #include "../kernel/head.h" |
| 27 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 28 | unsigned long kernel_virt_addr = KERNEL_LINK_ADDR; |
| 29 | EXPORT_SYMBOL(kernel_virt_addr); |
| 30 | |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 31 | unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] |
| 32 | __page_aligned_bss; |
| 33 | EXPORT_SYMBOL(empty_zero_page); |
| 34 | |
Anup Patel | d90d45d | 2019-06-07 06:01:29 +0000 | [diff] [blame] | 35 | extern char _start[]; |
Anup Patel | 8f3a2b4 | 2020-09-17 15:37:10 -0700 | [diff] [blame] | 36 | #define DTB_EARLY_BASE_VA PGDIR_SIZE |
| 37 | void *dtb_early_va __initdata; |
| 38 | uintptr_t dtb_early_pa __initdata; |
Anup Patel | d90d45d | 2019-06-07 06:01:29 +0000 | [diff] [blame] | 39 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 40 | struct pt_alloc_ops { |
| 41 | pte_t *(*get_pte_virt)(phys_addr_t pa); |
| 42 | phys_addr_t (*alloc_pte)(uintptr_t va); |
| 43 | #ifndef __PAGETABLE_PMD_FOLDED |
| 44 | pmd_t *(*get_pmd_virt)(phys_addr_t pa); |
| 45 | phys_addr_t (*alloc_pmd)(uintptr_t va); |
| 46 | #endif |
| 47 | }; |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 48 | |
Kefeng Wang | da81558 | 2020-10-31 14:01:12 +0800 | [diff] [blame] | 49 | static phys_addr_t dma32_phys_limit __ro_after_init; |
| 50 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 51 | static void __init zone_sizes_init(void) |
| 52 | { |
Christoph Hellwig | 5ec9c4f | 2018-01-16 09:37:50 +0100 | [diff] [blame] | 53 | unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, }; |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 54 | |
Zong Li | d5fad48 | 2018-06-25 16:49:37 +0800 | [diff] [blame] | 55 | #ifdef CONFIG_ZONE_DMA32 |
Kefeng Wang | da81558 | 2020-10-31 14:01:12 +0800 | [diff] [blame] | 56 | max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit); |
Zong Li | d5fad48 | 2018-06-25 16:49:37 +0800 | [diff] [blame] | 57 | #endif |
Christoph Hellwig | 5ec9c4f | 2018-01-16 09:37:50 +0100 | [diff] [blame] | 58 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
| 59 | |
Mike Rapoport | 9691a07 | 2020-06-03 15:57:10 -0700 | [diff] [blame] | 60 | free_area_init(max_zone_pfns); |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jisheng Zhang | 1987501 | 2021-03-30 02:22:21 +0800 | [diff] [blame^] | 63 | static void __init setup_zero_page(void) |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 64 | { |
| 65 | memset((void *)empty_zero_page, 0, PAGE_SIZE); |
| 66 | } |
| 67 | |
Kefeng Wang | 8fa3cdf | 2020-05-14 19:53:35 +0800 | [diff] [blame] | 68 | #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM) |
Yash Shah | 2cc6c4a | 2019-11-18 05:58:34 +0000 | [diff] [blame] | 69 | static inline void print_mlk(char *name, unsigned long b, unsigned long t) |
| 70 | { |
| 71 | pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t, |
| 72 | (((t) - (b)) >> 10)); |
| 73 | } |
| 74 | |
| 75 | static inline void print_mlm(char *name, unsigned long b, unsigned long t) |
| 76 | { |
| 77 | pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t, |
| 78 | (((t) - (b)) >> 20)); |
| 79 | } |
| 80 | |
Jisheng Zhang | 1987501 | 2021-03-30 02:22:21 +0800 | [diff] [blame^] | 81 | static void __init print_vm_layout(void) |
Yash Shah | 2cc6c4a | 2019-11-18 05:58:34 +0000 | [diff] [blame] | 82 | { |
| 83 | pr_notice("Virtual kernel memory layout:\n"); |
| 84 | print_mlk("fixmap", (unsigned long)FIXADDR_START, |
| 85 | (unsigned long)FIXADDR_TOP); |
| 86 | print_mlm("pci io", (unsigned long)PCI_IO_START, |
| 87 | (unsigned long)PCI_IO_END); |
| 88 | print_mlm("vmemmap", (unsigned long)VMEMMAP_START, |
| 89 | (unsigned long)VMEMMAP_END); |
| 90 | print_mlm("vmalloc", (unsigned long)VMALLOC_START, |
| 91 | (unsigned long)VMALLOC_END); |
| 92 | print_mlm("lowmem", (unsigned long)PAGE_OFFSET, |
| 93 | (unsigned long)high_memory); |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 94 | #ifdef CONFIG_64BIT |
| 95 | print_mlm("kernel", (unsigned long)KERNEL_LINK_ADDR, |
| 96 | (unsigned long)ADDRESS_SPACE_END); |
| 97 | #endif |
Yash Shah | 2cc6c4a | 2019-11-18 05:58:34 +0000 | [diff] [blame] | 98 | } |
| 99 | #else |
| 100 | static void print_vm_layout(void) { } |
| 101 | #endif /* CONFIG_DEBUG_VM */ |
| 102 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 103 | void __init mem_init(void) |
| 104 | { |
| 105 | #ifdef CONFIG_FLATMEM |
| 106 | BUG_ON(!mem_map); |
| 107 | #endif /* CONFIG_FLATMEM */ |
| 108 | |
| 109 | high_memory = (void *)(__va(PFN_PHYS(max_low_pfn))); |
Mike Rapoport | c6ffc5c | 2018-10-30 15:09:30 -0700 | [diff] [blame] | 110 | memblock_free_all(); |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 111 | |
| 112 | mem_init_print_info(NULL); |
Yash Shah | 2cc6c4a | 2019-11-18 05:58:34 +0000 | [diff] [blame] | 113 | print_vm_layout(); |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 116 | void __init setup_bootmem(void) |
| 117 | { |
Zong Li | ac51e00 | 2020-01-02 11:12:40 +0800 | [diff] [blame] | 118 | phys_addr_t vmlinux_end = __pa_symbol(&_end); |
| 119 | phys_addr_t vmlinux_start = __pa_symbol(&_start); |
Kefeng Wang | dd2d082 | 2021-02-09 09:01:51 +0800 | [diff] [blame] | 120 | phys_addr_t dram_end = memblock_end_of_DRAM(); |
Atish Patra | abb8e86 | 2021-01-11 15:45:02 -0800 | [diff] [blame] | 121 | phys_addr_t max_mapped_addr = __pa(~(ulong)0); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 122 | |
Kefeng Wang | dd2d082 | 2021-02-09 09:01:51 +0800 | [diff] [blame] | 123 | /* The maximal physical memory size is -PAGE_OFFSET. */ |
Atish Patra | de043da | 2020-12-18 16:13:56 -0800 | [diff] [blame] | 124 | memblock_enforce_memory_limit(-PAGE_OFFSET); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 125 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 126 | /* |
| 127 | * Reserve from the start of the kernel to the end of the kernel |
| 128 | * and make sure we align the reservation on PMD_SIZE since we will |
| 129 | * map the kernel in the linear mapping as read-only: we do not want |
| 130 | * any allocation to happen between _end and the next pmd aligned page. |
| 131 | */ |
| 132 | memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK); |
Anup Patel | d90d45d | 2019-06-07 06:01:29 +0000 | [diff] [blame] | 133 | |
Atish Patra | abb8e86 | 2021-01-11 15:45:02 -0800 | [diff] [blame] | 134 | /* |
| 135 | * memblock allocator is not aware of the fact that last 4K bytes of |
| 136 | * the addressable memory can not be mapped because of IS_ERR_VALUE |
| 137 | * macro. Make sure that last 4k bytes are not usable by memblock |
| 138 | * if end of dram is equal to maximum addressable memory. |
| 139 | */ |
| 140 | if (max_mapped_addr == (dram_end - 1)) |
| 141 | memblock_set_current_limit(max_mapped_addr - 4096); |
| 142 | |
Kefeng Wang | f6e5aed | 2021-02-25 14:54:17 +0800 | [diff] [blame] | 143 | min_low_pfn = PFN_UP(memblock_start_of_DRAM()); |
| 144 | max_low_pfn = max_pfn = PFN_DOWN(dram_end); |
| 145 | |
Kefeng Wang | da81558 | 2020-10-31 14:01:12 +0800 | [diff] [blame] | 146 | dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn)); |
Guo Ren | 336e8eb | 2021-01-21 14:31:17 +0800 | [diff] [blame] | 147 | set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 148 | |
Kefeng Wang | aec33b5 | 2021-01-15 13:46:06 +0800 | [diff] [blame] | 149 | reserve_initrd_mem(); |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 150 | /* |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 151 | * If DTB is built in, no need to reserve its memblock. |
| 152 | * Otherwise, do reserve it but avoid using |
| 153 | * early_init_fdt_reserve_self() since __pa() does |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 154 | * not work for DTB pointers that are fixmap addresses |
| 155 | */ |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 156 | if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) |
| 157 | memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 158 | |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 159 | early_init_fdt_scan_reserved_mem(); |
Kefeng Wang | da81558 | 2020-10-31 14:01:12 +0800 | [diff] [blame] | 160 | dma_contiguous_reserve(dma32_phys_limit); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 161 | memblock_allow_resize(); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 162 | } |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 163 | |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 164 | #ifdef CONFIG_MMU |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 165 | static struct pt_alloc_ops pt_ops; |
| 166 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 167 | /* Offset between linear mapping virtual address and kernel load address */ |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 168 | unsigned long va_pa_offset; |
| 169 | EXPORT_SYMBOL(va_pa_offset); |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 170 | #ifdef CONFIG_64BIT |
| 171 | /* Offset between kernel mapping virtual address and kernel load address */ |
| 172 | unsigned long va_kernel_pa_offset; |
| 173 | EXPORT_SYMBOL(va_kernel_pa_offset); |
| 174 | #endif |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 175 | unsigned long pfn_base; |
| 176 | EXPORT_SYMBOL(pfn_base); |
| 177 | |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 178 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 179 | pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 180 | pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 181 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 182 | pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 183 | |
| 184 | void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) |
| 185 | { |
| 186 | unsigned long addr = __fix_to_virt(idx); |
| 187 | pte_t *ptep; |
| 188 | |
| 189 | BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); |
| 190 | |
| 191 | ptep = &fixmap_pte[pte_index(addr)]; |
| 192 | |
Greentime Hu | 21190b7 | 2020-08-04 11:02:05 +0800 | [diff] [blame] | 193 | if (pgprot_val(prot)) |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 194 | set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot)); |
Greentime Hu | 21190b7 | 2020-08-04 11:02:05 +0800 | [diff] [blame] | 195 | else |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 196 | pte_clear(&init_mm, addr, ptep); |
Greentime Hu | 21190b7 | 2020-08-04 11:02:05 +0800 | [diff] [blame] | 197 | local_flush_tlb_page(addr); |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 198 | } |
| 199 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 200 | static inline pte_t *__init get_pte_virt_early(phys_addr_t pa) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 201 | { |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 202 | return (pte_t *)((uintptr_t)pa); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 205 | static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa) |
| 206 | { |
| 207 | clear_fixmap(FIX_PTE); |
| 208 | return (pte_t *)set_fixmap_offset(FIX_PTE, pa); |
| 209 | } |
| 210 | |
| 211 | static inline pte_t *get_pte_virt_late(phys_addr_t pa) |
| 212 | { |
| 213 | return (pte_t *) __va(pa); |
| 214 | } |
| 215 | |
| 216 | static inline phys_addr_t __init alloc_pte_early(uintptr_t va) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 217 | { |
| 218 | /* |
| 219 | * We only create PMD or PGD early mappings so we |
| 220 | * should never reach here with MMU disabled. |
| 221 | */ |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 222 | BUG(); |
| 223 | } |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 224 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 225 | static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va) |
| 226 | { |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 227 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 228 | } |
| 229 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 230 | static phys_addr_t alloc_pte_late(uintptr_t va) |
| 231 | { |
| 232 | unsigned long vaddr; |
| 233 | |
| 234 | vaddr = __get_free_page(GFP_KERNEL); |
| 235 | if (!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr))) |
| 236 | BUG(); |
| 237 | return __pa(vaddr); |
| 238 | } |
| 239 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 240 | static void __init create_pte_mapping(pte_t *ptep, |
| 241 | uintptr_t va, phys_addr_t pa, |
| 242 | phys_addr_t sz, pgprot_t prot) |
| 243 | { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 244 | uintptr_t pte_idx = pte_index(va); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 245 | |
| 246 | BUG_ON(sz != PAGE_SIZE); |
| 247 | |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 248 | if (pte_none(ptep[pte_idx])) |
| 249 | ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | #ifndef __PAGETABLE_PMD_FOLDED |
| 253 | |
| 254 | pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss; |
| 255 | pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; |
Alexandre Ghiti | 0f02de4 | 2021-02-21 09:22:33 -0500 | [diff] [blame] | 256 | pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE); |
Anup Patel | 1074dd4 | 2020-11-04 12:07:13 +0530 | [diff] [blame] | 257 | pmd_t early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 258 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 259 | static pmd_t *__init get_pmd_virt_early(phys_addr_t pa) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 260 | { |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 261 | /* Before MMU is enabled */ |
| 262 | return (pmd_t *)((uintptr_t)pa); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 265 | static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa) |
| 266 | { |
| 267 | clear_fixmap(FIX_PMD); |
| 268 | return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); |
| 269 | } |
| 270 | |
| 271 | static pmd_t *get_pmd_virt_late(phys_addr_t pa) |
| 272 | { |
| 273 | return (pmd_t *) __va(pa); |
| 274 | } |
| 275 | |
| 276 | static phys_addr_t __init alloc_pmd_early(uintptr_t va) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 277 | { |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 278 | BUG_ON((va - kernel_virt_addr) >> PGDIR_SHIFT); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 279 | |
Alexandre Ghiti | 0f02de4 | 2021-02-21 09:22:33 -0500 | [diff] [blame] | 280 | return (uintptr_t)early_pmd; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 283 | static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va) |
| 284 | { |
| 285 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 286 | } |
| 287 | |
| 288 | static phys_addr_t alloc_pmd_late(uintptr_t va) |
| 289 | { |
| 290 | unsigned long vaddr; |
| 291 | |
| 292 | vaddr = __get_free_page(GFP_KERNEL); |
| 293 | BUG_ON(!vaddr); |
| 294 | return __pa(vaddr); |
| 295 | } |
| 296 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 297 | static void __init create_pmd_mapping(pmd_t *pmdp, |
| 298 | uintptr_t va, phys_addr_t pa, |
| 299 | phys_addr_t sz, pgprot_t prot) |
| 300 | { |
| 301 | pte_t *ptep; |
| 302 | phys_addr_t pte_phys; |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 303 | uintptr_t pmd_idx = pmd_index(va); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 304 | |
| 305 | if (sz == PMD_SIZE) { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 306 | if (pmd_none(pmdp[pmd_idx])) |
| 307 | pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 311 | if (pmd_none(pmdp[pmd_idx])) { |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 312 | pte_phys = pt_ops.alloc_pte(va); |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 313 | pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE); |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 314 | ptep = pt_ops.get_pte_virt(pte_phys); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 315 | memset(ptep, 0, PAGE_SIZE); |
| 316 | } else { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 317 | pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx])); |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 318 | ptep = pt_ops.get_pte_virt(pte_phys); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | create_pte_mapping(ptep, va, pa, sz, prot); |
| 322 | } |
| 323 | |
| 324 | #define pgd_next_t pmd_t |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 325 | #define alloc_pgd_next(__va) pt_ops.alloc_pmd(__va) |
| 326 | #define get_pgd_next_virt(__pa) pt_ops.get_pmd_virt(__pa) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 327 | #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ |
| 328 | create_pmd_mapping(__nextp, __va, __pa, __sz, __prot) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 329 | #define fixmap_pgd_next fixmap_pmd |
| 330 | #else |
| 331 | #define pgd_next_t pte_t |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 332 | #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va) |
| 333 | #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 334 | #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ |
| 335 | create_pte_mapping(__nextp, __va, __pa, __sz, __prot) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 336 | #define fixmap_pgd_next fixmap_pte |
| 337 | #endif |
| 338 | |
Atish Patra | b91540d | 2020-09-17 15:37:15 -0700 | [diff] [blame] | 339 | void __init create_pgd_mapping(pgd_t *pgdp, |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 340 | uintptr_t va, phys_addr_t pa, |
| 341 | phys_addr_t sz, pgprot_t prot) |
| 342 | { |
| 343 | pgd_next_t *nextp; |
| 344 | phys_addr_t next_phys; |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 345 | uintptr_t pgd_idx = pgd_index(va); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 346 | |
| 347 | if (sz == PGDIR_SIZE) { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 348 | if (pgd_val(pgdp[pgd_idx]) == 0) |
| 349 | pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 353 | if (pgd_val(pgdp[pgd_idx]) == 0) { |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 354 | next_phys = alloc_pgd_next(va); |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 355 | pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 356 | nextp = get_pgd_next_virt(next_phys); |
| 357 | memset(nextp, 0, PAGE_SIZE); |
| 358 | } else { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 359 | next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx])); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 360 | nextp = get_pgd_next_virt(next_phys); |
| 361 | } |
| 362 | |
| 363 | create_pgd_next_mapping(nextp, va, pa, sz, prot); |
| 364 | } |
| 365 | |
| 366 | static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) |
| 367 | { |
Zong Li | 0fdc636 | 2019-11-08 01:00:40 -0800 | [diff] [blame] | 368 | /* Upgrade to PMD_SIZE mappings whenever possible */ |
| 369 | if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1))) |
| 370 | return PAGE_SIZE; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 371 | |
Zong Li | 0fdc636 | 2019-11-08 01:00:40 -0800 | [diff] [blame] | 372 | return PMD_SIZE; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 375 | /* |
| 376 | * setup_vm() is called from head.S with MMU-off. |
| 377 | * |
| 378 | * Following requirements should be honoured for setup_vm() to work |
| 379 | * correctly: |
| 380 | * 1) It should use PC-relative addressing for accessing kernel symbols. |
| 381 | * To achieve this we always use GCC cmodel=medany. |
| 382 | * 2) The compiler instrumentation for FTRACE will not work for setup_vm() |
| 383 | * so disable compiler instrumentation when FTRACE is enabled. |
| 384 | * |
| 385 | * Currently, the above requirements are honoured by using custom CFLAGS |
| 386 | * for init.o in mm/Makefile. |
| 387 | */ |
| 388 | |
| 389 | #ifndef __riscv_cmodel_medany |
Paul Walmsley | 6a527b6 | 2019-10-17 14:45:58 -0700 | [diff] [blame] | 390 | #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing." |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 391 | #endif |
| 392 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 393 | uintptr_t load_pa, load_sz; |
| 394 | |
| 395 | static void __init create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size) |
| 396 | { |
| 397 | uintptr_t va, end_va; |
| 398 | |
| 399 | end_va = kernel_virt_addr + load_sz; |
| 400 | for (va = kernel_virt_addr; va < end_va; va += map_size) |
| 401 | create_pgd_mapping(pgdir, va, |
| 402 | load_pa + (va - kernel_virt_addr), |
| 403 | map_size, PAGE_KERNEL_EXEC); |
| 404 | } |
| 405 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 406 | asmlinkage void __init setup_vm(uintptr_t dtb_pa) |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 407 | { |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 408 | uintptr_t pa; |
Alexandre Ghiti | 0f02de4 | 2021-02-21 09:22:33 -0500 | [diff] [blame] | 409 | uintptr_t map_size; |
Atish Patra | 6262f66 | 2020-09-17 15:37:11 -0700 | [diff] [blame] | 410 | #ifndef __PAGETABLE_PMD_FOLDED |
| 411 | pmd_t fix_bmap_spmd, fix_bmap_epmd; |
| 412 | #endif |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 413 | load_pa = (uintptr_t)(&_start); |
| 414 | load_sz = (uintptr_t)(&_end) - load_pa; |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 415 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 416 | va_pa_offset = PAGE_OFFSET - load_pa; |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 417 | #ifdef CONFIG_64BIT |
| 418 | va_kernel_pa_offset = kernel_virt_addr - load_pa; |
| 419 | #endif |
| 420 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 421 | pfn_base = PFN_DOWN(load_pa); |
| 422 | |
| 423 | /* |
| 424 | * Enforce boot alignment requirements of RV32 and |
| 425 | * RV64 by only allowing PMD or PGD mappings. |
| 426 | */ |
Alexandre Ghiti | 0f02de4 | 2021-02-21 09:22:33 -0500 | [diff] [blame] | 427 | map_size = PMD_SIZE; |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 428 | |
| 429 | /* Sanity check alignment and size */ |
| 430 | BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 431 | BUG_ON((load_pa % map_size) != 0); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 432 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 433 | pt_ops.alloc_pte = alloc_pte_early; |
| 434 | pt_ops.get_pte_virt = get_pte_virt_early; |
| 435 | #ifndef __PAGETABLE_PMD_FOLDED |
| 436 | pt_ops.alloc_pmd = alloc_pmd_early; |
| 437 | pt_ops.get_pmd_virt = get_pmd_virt_early; |
| 438 | #endif |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 439 | /* Setup early PGD for fixmap */ |
| 440 | create_pgd_mapping(early_pg_dir, FIXADDR_START, |
| 441 | (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 442 | |
| 443 | #ifndef __PAGETABLE_PMD_FOLDED |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 444 | /* Setup fixmap PMD */ |
| 445 | create_pmd_mapping(fixmap_pmd, FIXADDR_START, |
| 446 | (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE); |
| 447 | /* Setup trampoline PGD and PMD */ |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 448 | create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr, |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 449 | (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE); |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 450 | create_pmd_mapping(trampoline_pmd, kernel_virt_addr, |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 451 | load_pa, PMD_SIZE, PAGE_KERNEL_EXEC); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 452 | #else |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 453 | /* Setup trampoline PGD */ |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 454 | create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr, |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 455 | load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC); |
| 456 | #endif |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 457 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 458 | /* |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 459 | * Setup early PGD covering entire kernel which will allow |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 460 | * us to reach paging_init(). We map all memory banks later |
| 461 | * in setup_vm_final() below. |
| 462 | */ |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 463 | create_kernel_page_table(early_pg_dir, map_size); |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 464 | |
Anup Patel | 1074dd4 | 2020-11-04 12:07:13 +0530 | [diff] [blame] | 465 | #ifndef __PAGETABLE_PMD_FOLDED |
| 466 | /* Setup early PMD for DTB */ |
| 467 | create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA, |
| 468 | (uintptr_t)early_dtb_pmd, PGDIR_SIZE, PAGE_TABLE); |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 469 | #ifndef CONFIG_BUILTIN_DTB |
Anup Patel | 1074dd4 | 2020-11-04 12:07:13 +0530 | [diff] [blame] | 470 | /* Create two consecutive PMD mappings for FDT early scan */ |
| 471 | pa = dtb_pa & ~(PMD_SIZE - 1); |
| 472 | create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA, |
| 473 | pa, PMD_SIZE, PAGE_KERNEL); |
| 474 | create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE, |
| 475 | pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL); |
| 476 | dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1)); |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 477 | #else /* CONFIG_BUILTIN_DTB */ |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 478 | #ifdef CONFIG_64BIT |
| 479 | /* |
| 480 | * __va can't be used since it would return a linear mapping address |
| 481 | * whereas dtb_early_va will be used before setup_vm_final installs |
| 482 | * the linear mapping. |
| 483 | */ |
| 484 | dtb_early_va = kernel_mapping_pa_to_va(dtb_pa); |
| 485 | #else |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 486 | dtb_early_va = __va(dtb_pa); |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 487 | #endif /* CONFIG_64BIT */ |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 488 | #endif /* CONFIG_BUILTIN_DTB */ |
Anup Patel | 1074dd4 | 2020-11-04 12:07:13 +0530 | [diff] [blame] | 489 | #else |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 490 | #ifndef CONFIG_BUILTIN_DTB |
Anup Patel | 8f3a2b4 | 2020-09-17 15:37:10 -0700 | [diff] [blame] | 491 | /* Create two consecutive PGD mappings for FDT early scan */ |
| 492 | pa = dtb_pa & ~(PGDIR_SIZE - 1); |
| 493 | create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA, |
| 494 | pa, PGDIR_SIZE, PAGE_KERNEL); |
| 495 | create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA + PGDIR_SIZE, |
| 496 | pa + PGDIR_SIZE, PGDIR_SIZE, PAGE_KERNEL); |
| 497 | dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1)); |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 498 | #else /* CONFIG_BUILTIN_DTB */ |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 499 | #ifdef CONFIG_64BIT |
| 500 | dtb_early_va = kernel_mapping_pa_to_va(dtb_pa); |
| 501 | #else |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 502 | dtb_early_va = __va(dtb_pa); |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 503 | #endif /* CONFIG_64BIT */ |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 504 | #endif /* CONFIG_BUILTIN_DTB */ |
Anup Patel | 1074dd4 | 2020-11-04 12:07:13 +0530 | [diff] [blame] | 505 | #endif |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 506 | dtb_early_pa = dtb_pa; |
Atish Patra | 6262f66 | 2020-09-17 15:37:11 -0700 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap |
| 510 | * range can not span multiple pmds. |
| 511 | */ |
| 512 | BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) |
| 513 | != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); |
| 514 | |
| 515 | #ifndef __PAGETABLE_PMD_FOLDED |
| 516 | /* |
| 517 | * Early ioremap fixmap is already created as it lies within first 2MB |
| 518 | * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END |
| 519 | * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn |
| 520 | * the user if not. |
| 521 | */ |
| 522 | fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))]; |
| 523 | fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))]; |
| 524 | if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) { |
| 525 | WARN_ON(1); |
| 526 | pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n", |
| 527 | pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd)); |
| 528 | pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", |
| 529 | fix_to_virt(FIX_BTMAP_BEGIN)); |
| 530 | pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", |
| 531 | fix_to_virt(FIX_BTMAP_END)); |
| 532 | |
| 533 | pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); |
| 534 | pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); |
| 535 | } |
| 536 | #endif |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 539 | #ifdef CONFIG_64BIT |
| 540 | void protect_kernel_linear_mapping_text_rodata(void) |
| 541 | { |
| 542 | unsigned long text_start = (unsigned long)lm_alias(_start); |
| 543 | unsigned long init_text_start = (unsigned long)lm_alias(__init_text_begin); |
| 544 | unsigned long rodata_start = (unsigned long)lm_alias(__start_rodata); |
| 545 | unsigned long data_start = (unsigned long)lm_alias(_data); |
| 546 | |
| 547 | set_memory_ro(text_start, (init_text_start - text_start) >> PAGE_SHIFT); |
| 548 | set_memory_nx(text_start, (init_text_start - text_start) >> PAGE_SHIFT); |
| 549 | |
| 550 | set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); |
| 551 | set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); |
| 552 | } |
| 553 | #endif |
| 554 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 555 | static void __init setup_vm_final(void) |
| 556 | { |
| 557 | uintptr_t va, map_size; |
| 558 | phys_addr_t pa, start, end; |
Mike Rapoport | b10d6bc | 2020-10-13 16:58:08 -0700 | [diff] [blame] | 559 | u64 i; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 560 | |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 561 | /** |
| 562 | * MMU is enabled at this point. But page table setup is not complete yet. |
| 563 | * fixmap page table alloc functions should be used at this point |
| 564 | */ |
| 565 | pt_ops.alloc_pte = alloc_pte_fixmap; |
| 566 | pt_ops.get_pte_virt = get_pte_virt_fixmap; |
| 567 | #ifndef __PAGETABLE_PMD_FOLDED |
| 568 | pt_ops.alloc_pmd = alloc_pmd_fixmap; |
| 569 | pt_ops.get_pmd_virt = get_pmd_virt_fixmap; |
| 570 | #endif |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 571 | /* Setup swapper PGD for fixmap */ |
| 572 | create_pgd_mapping(swapper_pg_dir, FIXADDR_START, |
Zong Li | ac51e00 | 2020-01-02 11:12:40 +0800 | [diff] [blame] | 573 | __pa_symbol(fixmap_pgd_next), |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 574 | PGDIR_SIZE, PAGE_TABLE); |
| 575 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 576 | /* Map all memory banks in the linear mapping */ |
Mike Rapoport | b10d6bc | 2020-10-13 16:58:08 -0700 | [diff] [blame] | 577 | for_each_mem_range(i, &start, &end) { |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 578 | if (start >= end) |
| 579 | break; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 580 | if (start <= __pa(PAGE_OFFSET) && |
| 581 | __pa(PAGE_OFFSET) < end) |
| 582 | start = __pa(PAGE_OFFSET); |
| 583 | |
| 584 | map_size = best_map_size(start, end - start); |
| 585 | for (pa = start; pa < end; pa += map_size) { |
| 586 | va = (uintptr_t)__va(pa); |
| 587 | create_pgd_mapping(swapper_pg_dir, va, pa, |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 588 | map_size, |
| 589 | #ifdef CONFIG_64BIT |
| 590 | PAGE_KERNEL |
| 591 | #else |
| 592 | PAGE_KERNEL_EXEC |
| 593 | #endif |
| 594 | ); |
| 595 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 596 | } |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 597 | } |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 598 | |
Alexandre Ghiti | 2bfc6cd | 2021-04-11 12:41:44 -0400 | [diff] [blame] | 599 | #ifdef CONFIG_64BIT |
| 600 | /* Map the kernel */ |
| 601 | create_kernel_page_table(swapper_pg_dir, PMD_SIZE); |
| 602 | #endif |
| 603 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 604 | /* Clear fixmap PTE and PMD mappings */ |
| 605 | clear_fixmap(FIX_PTE); |
| 606 | clear_fixmap(FIX_PMD); |
| 607 | |
| 608 | /* Move to swapper page table */ |
Zong Li | ac51e00 | 2020-01-02 11:12:40 +0800 | [diff] [blame] | 609 | csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 610 | local_flush_tlb_all(); |
Atish Patra | e8dcb61 | 2020-09-17 15:37:12 -0700 | [diff] [blame] | 611 | |
| 612 | /* generic page allocation functions must be used to setup page table */ |
| 613 | pt_ops.alloc_pte = alloc_pte_late; |
| 614 | pt_ops.get_pte_virt = get_pte_virt_late; |
| 615 | #ifndef __PAGETABLE_PMD_FOLDED |
| 616 | pt_ops.alloc_pmd = alloc_pmd_late; |
| 617 | pt_ops.get_pmd_virt = get_pmd_virt_late; |
| 618 | #endif |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 619 | } |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 620 | #else |
| 621 | asmlinkage void __init setup_vm(uintptr_t dtb_pa) |
| 622 | { |
| 623 | dtb_early_va = (void *)dtb_pa; |
Atish Patra | a78c6f5 | 2020-10-01 12:04:56 -0700 | [diff] [blame] | 624 | dtb_early_pa = dtb_pa; |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static inline void setup_vm_final(void) |
| 628 | { |
| 629 | } |
| 630 | #endif /* CONFIG_MMU */ |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 631 | |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 632 | #ifdef CONFIG_STRICT_KERNEL_RWX |
Jisheng Zhang | 1987501 | 2021-03-30 02:22:21 +0800 | [diff] [blame^] | 633 | void __init protect_kernel_text_data(void) |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 634 | { |
Atish Patra | 19a0086 | 2020-11-04 16:04:38 -0800 | [diff] [blame] | 635 | unsigned long text_start = (unsigned long)_start; |
| 636 | unsigned long init_text_start = (unsigned long)__init_text_begin; |
| 637 | unsigned long init_data_start = (unsigned long)__init_data_begin; |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 638 | unsigned long rodata_start = (unsigned long)__start_rodata; |
| 639 | unsigned long data_start = (unsigned long)_data; |
| 640 | unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn))); |
| 641 | |
Atish Patra | 19a0086 | 2020-11-04 16:04:38 -0800 | [diff] [blame] | 642 | set_memory_ro(text_start, (init_text_start - text_start) >> PAGE_SHIFT); |
| 643 | set_memory_ro(init_text_start, (init_data_start - init_text_start) >> PAGE_SHIFT); |
| 644 | set_memory_nx(init_data_start, (rodata_start - init_data_start) >> PAGE_SHIFT); |
| 645 | /* rodata section is marked readonly in mark_rodata_ro */ |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 646 | set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); |
| 647 | set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT); |
Atish Patra | 19a0086 | 2020-11-04 16:04:38 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void mark_rodata_ro(void) |
| 651 | { |
| 652 | unsigned long rodata_start = (unsigned long)__start_rodata; |
| 653 | unsigned long data_start = (unsigned long)_data; |
| 654 | |
| 655 | set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); |
Zong Li | b422d28 | 2020-06-03 16:03:55 -0700 | [diff] [blame] | 656 | |
| 657 | debug_checkwx(); |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 658 | } |
| 659 | #endif |
| 660 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 661 | void __init paging_init(void) |
| 662 | { |
| 663 | setup_vm_final(); |
| 664 | setup_zero_page(); |
Atish Patra | cbd34f4 | 2020-11-18 16:38:27 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void __init misc_mem_init(void) |
| 668 | { |
Kefeng Wang | f6e5aed | 2021-02-25 14:54:17 +0800 | [diff] [blame] | 669 | early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT); |
Atish Patra | 4f0e8ee | 2020-11-18 16:38:29 -0800 | [diff] [blame] | 670 | arch_numa_init(); |
Atish Patra | cbd34f4 | 2020-11-18 16:38:27 -0800 | [diff] [blame] | 671 | sparse_init(); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 672 | zone_sizes_init(); |
Atish Patra | 4f0e8ee | 2020-11-18 16:38:29 -0800 | [diff] [blame] | 673 | memblock_dump_all(); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 674 | } |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 675 | |
Kefeng Wang | 9fe57d8 | 2019-10-23 11:23:02 +0800 | [diff] [blame] | 676 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 677 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, |
| 678 | struct vmem_altmap *altmap) |
| 679 | { |
Anshuman Khandual | 1d9cfee | 2020-08-06 23:23:19 -0700 | [diff] [blame] | 680 | return vmemmap_populate_basepages(start, end, node, NULL); |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 681 | } |
| 682 | #endif |