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> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 16 | |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 17 | #include <asm/fixmap.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 18 | #include <asm/tlbflush.h> |
| 19 | #include <asm/sections.h> |
Palmer Dabbelt | 2d26825 | 2020-04-14 13:43:24 +0900 | [diff] [blame] | 20 | #include <asm/soc.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 21 | #include <asm/io.h> |
Zong Li | b422d28 | 2020-06-03 16:03:55 -0700 | [diff] [blame] | 22 | #include <asm/ptdump.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 23 | |
Paul Walmsley | ffaee27 | 2019-10-17 15:00:17 -0700 | [diff] [blame] | 24 | #include "../kernel/head.h" |
| 25 | |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 26 | unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] |
| 27 | __page_aligned_bss; |
| 28 | EXPORT_SYMBOL(empty_zero_page); |
| 29 | |
Anup Patel | d90d45d | 2019-06-07 06:01:29 +0000 | [diff] [blame] | 30 | extern char _start[]; |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 31 | void *dtb_early_va; |
Anup Patel | d90d45d | 2019-06-07 06:01:29 +0000 | [diff] [blame] | 32 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 33 | static void __init zone_sizes_init(void) |
| 34 | { |
Christoph Hellwig | 5ec9c4f | 2018-01-16 09:37:50 +0100 | [diff] [blame] | 35 | unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, }; |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 36 | |
Zong Li | d5fad48 | 2018-06-25 16:49:37 +0800 | [diff] [blame] | 37 | #ifdef CONFIG_ZONE_DMA32 |
Guo Ren | 28198c4 | 2019-01-12 16:16:27 +0800 | [diff] [blame] | 38 | max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G, |
| 39 | (unsigned long) PFN_PHYS(max_low_pfn))); |
Zong Li | d5fad48 | 2018-06-25 16:49:37 +0800 | [diff] [blame] | 40 | #endif |
Christoph Hellwig | 5ec9c4f | 2018-01-16 09:37:50 +0100 | [diff] [blame] | 41 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
| 42 | |
Mike Rapoport | 9691a07 | 2020-06-03 15:57:10 -0700 | [diff] [blame] | 43 | free_area_init(max_zone_pfns); |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 46 | static void setup_zero_page(void) |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 47 | { |
| 48 | memset((void *)empty_zero_page, 0, PAGE_SIZE); |
| 49 | } |
| 50 | |
Kefeng Wang | 8fa3cdf | 2020-05-14 19:53:35 +0800 | [diff] [blame] | 51 | #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM) |
Yash Shah | 2cc6c4a | 2019-11-18 05:58:34 +0000 | [diff] [blame] | 52 | static inline void print_mlk(char *name, unsigned long b, unsigned long t) |
| 53 | { |
| 54 | pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t, |
| 55 | (((t) - (b)) >> 10)); |
| 56 | } |
| 57 | |
| 58 | static inline void print_mlm(char *name, unsigned long b, unsigned long t) |
| 59 | { |
| 60 | pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t, |
| 61 | (((t) - (b)) >> 20)); |
| 62 | } |
| 63 | |
| 64 | static void print_vm_layout(void) |
| 65 | { |
| 66 | pr_notice("Virtual kernel memory layout:\n"); |
| 67 | print_mlk("fixmap", (unsigned long)FIXADDR_START, |
| 68 | (unsigned long)FIXADDR_TOP); |
| 69 | print_mlm("pci io", (unsigned long)PCI_IO_START, |
| 70 | (unsigned long)PCI_IO_END); |
| 71 | print_mlm("vmemmap", (unsigned long)VMEMMAP_START, |
| 72 | (unsigned long)VMEMMAP_END); |
| 73 | print_mlm("vmalloc", (unsigned long)VMALLOC_START, |
| 74 | (unsigned long)VMALLOC_END); |
| 75 | print_mlm("lowmem", (unsigned long)PAGE_OFFSET, |
| 76 | (unsigned long)high_memory); |
| 77 | } |
| 78 | #else |
| 79 | static void print_vm_layout(void) { } |
| 80 | #endif /* CONFIG_DEBUG_VM */ |
| 81 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 82 | void __init mem_init(void) |
| 83 | { |
| 84 | #ifdef CONFIG_FLATMEM |
| 85 | BUG_ON(!mem_map); |
| 86 | #endif /* CONFIG_FLATMEM */ |
| 87 | |
| 88 | high_memory = (void *)(__va(PFN_PHYS(max_low_pfn))); |
Mike Rapoport | c6ffc5c | 2018-10-30 15:09:30 -0700 | [diff] [blame] | 89 | memblock_free_all(); |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 90 | |
| 91 | mem_init_print_info(NULL); |
Yash Shah | 2cc6c4a | 2019-11-18 05:58:34 +0000 | [diff] [blame] | 92 | print_vm_layout(); |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 95 | #ifdef CONFIG_BLK_DEV_INITRD |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 96 | static void __init setup_initrd(void) |
| 97 | { |
Atish Patra | 4400231 | 2020-07-15 16:30:08 -0700 | [diff] [blame] | 98 | phys_addr_t start; |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 99 | unsigned long size; |
| 100 | |
Atish Patra | 4400231 | 2020-07-15 16:30:08 -0700 | [diff] [blame] | 101 | /* Ignore the virtul address computed during device tree parsing */ |
| 102 | initrd_start = initrd_end = 0; |
| 103 | |
| 104 | if (!phys_initrd_size) |
| 105 | return; |
| 106 | /* |
| 107 | * Round the memory region to page boundaries as per free_initrd_mem() |
| 108 | * This allows us to detect whether the pages overlapping the initrd |
| 109 | * are in use, but more importantly, reserves the entire set of pages |
| 110 | * as we don't want these pages allocated for other purposes. |
| 111 | */ |
| 112 | start = round_down(phys_initrd_start, PAGE_SIZE); |
| 113 | size = phys_initrd_size + (phys_initrd_start - start); |
| 114 | size = round_up(size, PAGE_SIZE); |
| 115 | |
| 116 | if (!memblock_is_region_memory(start, size)) { |
| 117 | pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", |
| 118 | (u64)start, size); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 119 | goto disable; |
| 120 | } |
| 121 | |
Atish Patra | 4400231 | 2020-07-15 16:30:08 -0700 | [diff] [blame] | 122 | if (memblock_is_region_reserved(start, size)) { |
| 123 | pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", |
| 124 | (u64)start, size); |
| 125 | goto disable; |
| 126 | } |
| 127 | |
| 128 | memblock_reserve(start, size); |
| 129 | /* Now convert initrd to virtual addresses */ |
| 130 | initrd_start = (unsigned long)__va(phys_initrd_start); |
| 131 | initrd_end = initrd_start + phys_initrd_size; |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 132 | initrd_below_start_ok = 1; |
| 133 | |
| 134 | pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n", |
| 135 | (void *)(initrd_start), size); |
| 136 | return; |
| 137 | disable: |
| 138 | pr_cont(" - disabling initrd\n"); |
| 139 | initrd_start = 0; |
| 140 | initrd_end = 0; |
| 141 | } |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 142 | #endif /* CONFIG_BLK_DEV_INITRD */ |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 143 | |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 144 | static phys_addr_t dtb_early_pa __initdata; |
| 145 | |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 146 | void __init setup_bootmem(void) |
| 147 | { |
| 148 | struct memblock_region *reg; |
| 149 | phys_addr_t mem_size = 0; |
Atish Patra | fa5a198 | 2020-07-15 16:30:09 -0700 | [diff] [blame] | 150 | phys_addr_t total_mem = 0; |
| 151 | phys_addr_t mem_start, end = 0; |
Zong Li | ac51e00 | 2020-01-02 11:12:40 +0800 | [diff] [blame] | 152 | phys_addr_t vmlinux_end = __pa_symbol(&_end); |
| 153 | phys_addr_t vmlinux_start = __pa_symbol(&_start); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 154 | |
| 155 | /* Find the memory region containing the kernel */ |
| 156 | for_each_memblock(memory, reg) { |
Atish Patra | fa5a198 | 2020-07-15 16:30:09 -0700 | [diff] [blame] | 157 | end = reg->base + reg->size; |
| 158 | if (!total_mem) |
| 159 | mem_start = reg->base; |
| 160 | if (reg->base <= vmlinux_start && vmlinux_end <= end) |
| 161 | BUG_ON(reg->size == 0); |
| 162 | total_mem = total_mem + reg->size; |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 163 | } |
Atish Patra | fa5a198 | 2020-07-15 16:30:09 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * Remove memblock from the end of usable area to the |
| 167 | * end of region |
| 168 | */ |
| 169 | mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET); |
| 170 | if (mem_start + mem_size < end) |
| 171 | memblock_remove(mem_start + mem_size, |
| 172 | end - mem_start - mem_size); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 173 | |
Anup Patel | d90d45d | 2019-06-07 06:01:29 +0000 | [diff] [blame] | 174 | /* Reserve from the start of the kernel to the end of the kernel */ |
| 175 | memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start); |
| 176 | |
Vincent Chen | c749bb2 | 2020-04-27 14:59:24 +0800 | [diff] [blame] | 177 | max_pfn = PFN_DOWN(memblock_end_of_DRAM()); |
| 178 | max_low_pfn = max_pfn; |
Atish Patra | d0d8aae | 2020-07-15 16:30:07 -0700 | [diff] [blame] | 179 | set_max_mapnr(max_low_pfn); |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 180 | |
| 181 | #ifdef CONFIG_BLK_DEV_INITRD |
| 182 | setup_initrd(); |
| 183 | #endif /* CONFIG_BLK_DEV_INITRD */ |
| 184 | |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 185 | /* |
| 186 | * Avoid using early_init_fdt_reserve_self() since __pa() does |
| 187 | * not work for DTB pointers that are fixmap addresses |
| 188 | */ |
| 189 | memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); |
| 190 | |
Anup Patel | 0651c26 | 2019-02-21 11:25:49 +0530 | [diff] [blame] | 191 | early_init_fdt_scan_reserved_mem(); |
| 192 | memblock_allow_resize(); |
| 193 | memblock_dump_all(); |
| 194 | |
| 195 | for_each_memblock(memory, reg) { |
| 196 | unsigned long start_pfn = memblock_region_memory_base_pfn(reg); |
| 197 | unsigned long end_pfn = memblock_region_memory_end_pfn(reg); |
| 198 | |
| 199 | memblock_set_node(PFN_PHYS(start_pfn), |
| 200 | PFN_PHYS(end_pfn - start_pfn), |
| 201 | &memblock.memory, 0); |
| 202 | } |
| 203 | } |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 204 | |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_MMU |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 206 | unsigned long va_pa_offset; |
| 207 | EXPORT_SYMBOL(va_pa_offset); |
| 208 | unsigned long pfn_base; |
| 209 | EXPORT_SYMBOL(pfn_base); |
| 210 | |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 211 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 212 | pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 213 | pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 214 | static bool mmu_enabled; |
| 215 | |
| 216 | #define MAX_EARLY_MAPPING_SIZE SZ_128M |
| 217 | |
| 218 | pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 219 | |
| 220 | void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) |
| 221 | { |
| 222 | unsigned long addr = __fix_to_virt(idx); |
| 223 | pte_t *ptep; |
| 224 | |
| 225 | BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); |
| 226 | |
| 227 | ptep = &fixmap_pte[pte_index(addr)]; |
| 228 | |
| 229 | if (pgprot_val(prot)) { |
| 230 | set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot)); |
| 231 | } else { |
| 232 | pte_clear(&init_mm, addr, ptep); |
| 233 | local_flush_tlb_page(addr); |
| 234 | } |
| 235 | } |
| 236 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 237 | static pte_t *__init get_pte_virt(phys_addr_t pa) |
| 238 | { |
| 239 | if (mmu_enabled) { |
| 240 | clear_fixmap(FIX_PTE); |
| 241 | return (pte_t *)set_fixmap_offset(FIX_PTE, pa); |
| 242 | } else { |
| 243 | return (pte_t *)((uintptr_t)pa); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static phys_addr_t __init alloc_pte(uintptr_t va) |
| 248 | { |
| 249 | /* |
| 250 | * We only create PMD or PGD early mappings so we |
| 251 | * should never reach here with MMU disabled. |
| 252 | */ |
| 253 | BUG_ON(!mmu_enabled); |
| 254 | |
| 255 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 256 | } |
| 257 | |
| 258 | static void __init create_pte_mapping(pte_t *ptep, |
| 259 | uintptr_t va, phys_addr_t pa, |
| 260 | phys_addr_t sz, pgprot_t prot) |
| 261 | { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 262 | uintptr_t pte_idx = pte_index(va); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 263 | |
| 264 | BUG_ON(sz != PAGE_SIZE); |
| 265 | |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 266 | if (pte_none(ptep[pte_idx])) |
| 267 | ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | #ifndef __PAGETABLE_PMD_FOLDED |
| 271 | |
| 272 | pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss; |
| 273 | pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; |
| 274 | |
| 275 | #if MAX_EARLY_MAPPING_SIZE < PGDIR_SIZE |
| 276 | #define NUM_EARLY_PMDS 1UL |
| 277 | #else |
| 278 | #define NUM_EARLY_PMDS (1UL + MAX_EARLY_MAPPING_SIZE / PGDIR_SIZE) |
| 279 | #endif |
| 280 | pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE); |
| 281 | |
| 282 | static pmd_t *__init get_pmd_virt(phys_addr_t pa) |
| 283 | { |
| 284 | if (mmu_enabled) { |
| 285 | clear_fixmap(FIX_PMD); |
| 286 | return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); |
| 287 | } else { |
| 288 | return (pmd_t *)((uintptr_t)pa); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static phys_addr_t __init alloc_pmd(uintptr_t va) |
| 293 | { |
| 294 | uintptr_t pmd_num; |
| 295 | |
| 296 | if (mmu_enabled) |
| 297 | return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); |
| 298 | |
| 299 | pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT; |
| 300 | BUG_ON(pmd_num >= NUM_EARLY_PMDS); |
| 301 | return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD]; |
| 302 | } |
| 303 | |
| 304 | static void __init create_pmd_mapping(pmd_t *pmdp, |
| 305 | uintptr_t va, phys_addr_t pa, |
| 306 | phys_addr_t sz, pgprot_t prot) |
| 307 | { |
| 308 | pte_t *ptep; |
| 309 | phys_addr_t pte_phys; |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 310 | uintptr_t pmd_idx = pmd_index(va); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 311 | |
| 312 | if (sz == PMD_SIZE) { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 313 | if (pmd_none(pmdp[pmd_idx])) |
| 314 | pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 318 | if (pmd_none(pmdp[pmd_idx])) { |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 319 | pte_phys = alloc_pte(va); |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 320 | pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 321 | ptep = get_pte_virt(pte_phys); |
| 322 | memset(ptep, 0, PAGE_SIZE); |
| 323 | } else { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 324 | pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx])); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 325 | ptep = get_pte_virt(pte_phys); |
| 326 | } |
| 327 | |
| 328 | create_pte_mapping(ptep, va, pa, sz, prot); |
| 329 | } |
| 330 | |
| 331 | #define pgd_next_t pmd_t |
| 332 | #define alloc_pgd_next(__va) alloc_pmd(__va) |
| 333 | #define get_pgd_next_virt(__pa) get_pmd_virt(__pa) |
| 334 | #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ |
| 335 | create_pmd_mapping(__nextp, __va, __pa, __sz, __prot) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 336 | #define fixmap_pgd_next fixmap_pmd |
| 337 | #else |
| 338 | #define pgd_next_t pte_t |
| 339 | #define alloc_pgd_next(__va) alloc_pte(__va) |
| 340 | #define get_pgd_next_virt(__pa) get_pte_virt(__pa) |
| 341 | #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ |
| 342 | create_pte_mapping(__nextp, __va, __pa, __sz, __prot) |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 343 | #define fixmap_pgd_next fixmap_pte |
| 344 | #endif |
| 345 | |
| 346 | static void __init create_pgd_mapping(pgd_t *pgdp, |
| 347 | uintptr_t va, phys_addr_t pa, |
| 348 | phys_addr_t sz, pgprot_t prot) |
| 349 | { |
| 350 | pgd_next_t *nextp; |
| 351 | phys_addr_t next_phys; |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 352 | uintptr_t pgd_idx = pgd_index(va); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 353 | |
| 354 | if (sz == PGDIR_SIZE) { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 355 | if (pgd_val(pgdp[pgd_idx]) == 0) |
| 356 | pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 360 | if (pgd_val(pgdp[pgd_idx]) == 0) { |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 361 | next_phys = alloc_pgd_next(va); |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 362 | pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 363 | nextp = get_pgd_next_virt(next_phys); |
| 364 | memset(nextp, 0, PAGE_SIZE); |
| 365 | } else { |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 366 | next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx])); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 367 | nextp = get_pgd_next_virt(next_phys); |
| 368 | } |
| 369 | |
| 370 | create_pgd_next_mapping(nextp, va, pa, sz, prot); |
| 371 | } |
| 372 | |
| 373 | static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) |
| 374 | { |
Zong Li | 0fdc636 | 2019-11-08 01:00:40 -0800 | [diff] [blame] | 375 | /* Upgrade to PMD_SIZE mappings whenever possible */ |
| 376 | if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1))) |
| 377 | return PAGE_SIZE; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 378 | |
Zong Li | 0fdc636 | 2019-11-08 01:00:40 -0800 | [diff] [blame] | 379 | return PMD_SIZE; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Anup Patel | 387181d | 2019-03-26 08:03:47 +0000 | [diff] [blame] | 382 | /* |
| 383 | * setup_vm() is called from head.S with MMU-off. |
| 384 | * |
| 385 | * Following requirements should be honoured for setup_vm() to work |
| 386 | * correctly: |
| 387 | * 1) It should use PC-relative addressing for accessing kernel symbols. |
| 388 | * To achieve this we always use GCC cmodel=medany. |
| 389 | * 2) The compiler instrumentation for FTRACE will not work for setup_vm() |
| 390 | * so disable compiler instrumentation when FTRACE is enabled. |
| 391 | * |
| 392 | * Currently, the above requirements are honoured by using custom CFLAGS |
| 393 | * for init.o in mm/Makefile. |
| 394 | */ |
| 395 | |
| 396 | #ifndef __riscv_cmodel_medany |
Paul Walmsley | 6a527b6 | 2019-10-17 14:45:58 -0700 | [diff] [blame] | 397 | #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] | 398 | #endif |
| 399 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 400 | asmlinkage void __init setup_vm(uintptr_t dtb_pa) |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 401 | { |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 402 | uintptr_t va, end_va; |
| 403 | uintptr_t load_pa = (uintptr_t)(&_start); |
| 404 | uintptr_t load_sz = (uintptr_t)(&_end) - load_pa; |
| 405 | uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 406 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 407 | va_pa_offset = PAGE_OFFSET - load_pa; |
| 408 | pfn_base = PFN_DOWN(load_pa); |
| 409 | |
| 410 | /* |
| 411 | * Enforce boot alignment requirements of RV32 and |
| 412 | * RV64 by only allowing PMD or PGD mappings. |
| 413 | */ |
| 414 | BUG_ON(map_size == PAGE_SIZE); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 415 | |
| 416 | /* Sanity check alignment and size */ |
| 417 | BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 418 | BUG_ON((load_pa % map_size) != 0); |
| 419 | BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE); |
| 420 | |
| 421 | /* Setup early PGD for fixmap */ |
| 422 | create_pgd_mapping(early_pg_dir, FIXADDR_START, |
| 423 | (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 424 | |
| 425 | #ifndef __PAGETABLE_PMD_FOLDED |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 426 | /* Setup fixmap PMD */ |
| 427 | create_pmd_mapping(fixmap_pmd, FIXADDR_START, |
| 428 | (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE); |
| 429 | /* Setup trampoline PGD and PMD */ |
| 430 | create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET, |
| 431 | (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE); |
| 432 | create_pmd_mapping(trampoline_pmd, PAGE_OFFSET, |
| 433 | load_pa, PMD_SIZE, PAGE_KERNEL_EXEC); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 434 | #else |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 435 | /* Setup trampoline PGD */ |
| 436 | create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET, |
| 437 | load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC); |
| 438 | #endif |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 439 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 440 | /* |
| 441 | * Setup early PGD covering entire kernel which will allows |
| 442 | * us to reach paging_init(). We map all memory banks later |
| 443 | * in setup_vm_final() below. |
| 444 | */ |
| 445 | end_va = PAGE_OFFSET + load_sz; |
| 446 | for (va = PAGE_OFFSET; va < end_va; va += map_size) |
| 447 | create_pgd_mapping(early_pg_dir, va, |
| 448 | load_pa + (va - PAGE_OFFSET), |
| 449 | map_size, PAGE_KERNEL_EXEC); |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 450 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 451 | /* Create fixed mapping for early FDT parsing */ |
| 452 | end_va = __fix_to_virt(FIX_FDT) + FIX_FDT_SIZE; |
| 453 | for (va = __fix_to_virt(FIX_FDT); va < end_va; va += PAGE_SIZE) |
| 454 | create_pte_mapping(fixmap_pte, va, |
| 455 | dtb_pa + (va - __fix_to_virt(FIX_FDT)), |
| 456 | PAGE_SIZE, PAGE_KERNEL); |
| 457 | |
| 458 | /* Save pointer to DTB for early FDT parsing */ |
| 459 | dtb_early_va = (void *)fix_to_virt(FIX_FDT) + (dtb_pa & ~PAGE_MASK); |
Albert Ou | 922b037 | 2019-09-27 16:14:18 -0700 | [diff] [blame] | 460 | /* Save physical address for memblock reservation */ |
| 461 | dtb_early_pa = dtb_pa; |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static void __init setup_vm_final(void) |
| 465 | { |
| 466 | uintptr_t va, map_size; |
| 467 | phys_addr_t pa, start, end; |
| 468 | struct memblock_region *reg; |
| 469 | |
| 470 | /* Set mmu_enabled flag */ |
| 471 | mmu_enabled = true; |
| 472 | |
| 473 | /* Setup swapper PGD for fixmap */ |
| 474 | create_pgd_mapping(swapper_pg_dir, FIXADDR_START, |
Zong Li | ac51e00 | 2020-01-02 11:12:40 +0800 | [diff] [blame] | 475 | __pa_symbol(fixmap_pgd_next), |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 476 | PGDIR_SIZE, PAGE_TABLE); |
| 477 | |
| 478 | /* Map all memory banks */ |
| 479 | for_each_memblock(memory, reg) { |
| 480 | start = reg->base; |
| 481 | end = start + reg->size; |
| 482 | |
| 483 | if (start >= end) |
| 484 | break; |
| 485 | if (memblock_is_nomap(reg)) |
| 486 | continue; |
| 487 | if (start <= __pa(PAGE_OFFSET) && |
| 488 | __pa(PAGE_OFFSET) < end) |
| 489 | start = __pa(PAGE_OFFSET); |
| 490 | |
| 491 | map_size = best_map_size(start, end - start); |
| 492 | for (pa = start; pa < end; pa += map_size) { |
| 493 | va = (uintptr_t)__va(pa); |
| 494 | create_pgd_mapping(swapper_pg_dir, va, pa, |
| 495 | map_size, PAGE_KERNEL_EXEC); |
| 496 | } |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 497 | } |
Anup Patel | f2c17aa | 2019-01-07 20:57:01 +0530 | [diff] [blame] | 498 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 499 | /* Clear fixmap PTE and PMD mappings */ |
| 500 | clear_fixmap(FIX_PTE); |
| 501 | clear_fixmap(FIX_PMD); |
| 502 | |
| 503 | /* Move to swapper page table */ |
Zong Li | ac51e00 | 2020-01-02 11:12:40 +0800 | [diff] [blame] | 504 | 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] | 505 | local_flush_tlb_all(); |
| 506 | } |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 507 | #else |
| 508 | asmlinkage void __init setup_vm(uintptr_t dtb_pa) |
| 509 | { |
Palmer Dabbelt | 2d26825 | 2020-04-14 13:43:24 +0900 | [diff] [blame] | 510 | #ifdef CONFIG_BUILTIN_DTB |
| 511 | dtb_early_va = soc_lookup_builtin_dtb(); |
| 512 | if (!dtb_early_va) { |
| 513 | /* Fallback to first available DTS */ |
| 514 | dtb_early_va = (void *) __dtb_start; |
| 515 | } |
| 516 | #else |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 517 | dtb_early_va = (void *)dtb_pa; |
Palmer Dabbelt | 2d26825 | 2020-04-14 13:43:24 +0900 | [diff] [blame] | 518 | #endif |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static inline void setup_vm_final(void) |
| 522 | { |
| 523 | } |
| 524 | #endif /* CONFIG_MMU */ |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 525 | |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 526 | #ifdef CONFIG_STRICT_KERNEL_RWX |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 527 | void mark_rodata_ro(void) |
| 528 | { |
| 529 | unsigned long text_start = (unsigned long)_text; |
| 530 | unsigned long text_end = (unsigned long)_etext; |
| 531 | unsigned long rodata_start = (unsigned long)__start_rodata; |
| 532 | unsigned long data_start = (unsigned long)_data; |
| 533 | unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn))); |
| 534 | |
| 535 | set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT); |
| 536 | set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); |
| 537 | set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); |
| 538 | set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT); |
Zong Li | b422d28 | 2020-06-03 16:03:55 -0700 | [diff] [blame] | 539 | |
| 540 | debug_checkwx(); |
Zong Li | d27c3c9 | 2020-03-10 00:55:41 +0800 | [diff] [blame] | 541 | } |
| 542 | #endif |
| 543 | |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 544 | void __init paging_init(void) |
| 545 | { |
| 546 | setup_vm_final(); |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 547 | memblocks_present(); |
| 548 | sparse_init(); |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 549 | setup_zero_page(); |
| 550 | zone_sizes_init(); |
Anup Patel | 6f1e9e9 | 2019-02-13 16:38:36 +0530 | [diff] [blame] | 551 | } |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 552 | |
Kefeng Wang | 9fe57d8 | 2019-10-23 11:23:02 +0800 | [diff] [blame] | 553 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 554 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, |
| 555 | struct vmem_altmap *altmap) |
| 556 | { |
Anshuman Khandual | 1d9cfee | 2020-08-06 23:23:19 -0700 | [diff] [blame^] | 557 | return vmemmap_populate_basepages(start, end, node, NULL); |
Logan Gunthorpe | d95f1a5 | 2019-08-28 15:40:54 -0600 | [diff] [blame] | 558 | } |
| 559 | #endif |