1 /* 2 * Copyright (C) 2012 Regents of the University of California 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation, version 2. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #include <linux/init.h> 15 #include <linux/mm.h> 16 #include <linux/memblock.h> 17 #include <linux/initrd.h> 18 #include <linux/swap.h> 19 #include <linux/sizes.h> 20 #include <linux/of_fdt.h> 21 22 #include <asm/fixmap.h> 23 #include <asm/tlbflush.h> 24 #include <asm/sections.h> 25 #include <asm/pgtable.h> 26 #include <asm/io.h> 27 28 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] 29 __page_aligned_bss; 30 EXPORT_SYMBOL(empty_zero_page); 31 32 static void __init zone_sizes_init(void) 33 { 34 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, }; 35 36 #ifdef CONFIG_ZONE_DMA32 37 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G, 38 (unsigned long) PFN_PHYS(max_low_pfn))); 39 #endif 40 max_zone_pfns[ZONE_NORMAL] = max_low_pfn; 41 42 free_area_init_nodes(max_zone_pfns); 43 } 44 45 void setup_zero_page(void) 46 { 47 memset((void *)empty_zero_page, 0, PAGE_SIZE); 48 } 49 50 void __init paging_init(void) 51 { 52 setup_zero_page(); 53 local_flush_tlb_all(); 54 zone_sizes_init(); 55 } 56 57 void __init mem_init(void) 58 { 59 #ifdef CONFIG_FLATMEM 60 BUG_ON(!mem_map); 61 #endif /* CONFIG_FLATMEM */ 62 63 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn))); 64 memblock_free_all(); 65 66 mem_init_print_info(NULL); 67 } 68 69 void free_initmem(void) 70 { 71 free_initmem_default(0); 72 } 73 74 #ifdef CONFIG_BLK_DEV_INITRD 75 static void __init setup_initrd(void) 76 { 77 unsigned long size; 78 79 if (initrd_start >= initrd_end) { 80 pr_info("initrd not found or empty"); 81 goto disable; 82 } 83 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) { 84 pr_err("initrd extends beyond end of memory"); 85 goto disable; 86 } 87 88 size = initrd_end - initrd_start; 89 memblock_reserve(__pa(initrd_start), size); 90 initrd_below_start_ok = 1; 91 92 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n", 93 (void *)(initrd_start), size); 94 return; 95 disable: 96 pr_cont(" - disabling initrd\n"); 97 initrd_start = 0; 98 initrd_end = 0; 99 } 100 101 void __init free_initrd_mem(unsigned long start, unsigned long end) 102 { 103 free_reserved_area((void *)start, (void *)end, -1, "initrd"); 104 } 105 #endif /* CONFIG_BLK_DEV_INITRD */ 106 107 void __init setup_bootmem(void) 108 { 109 struct memblock_region *reg; 110 phys_addr_t mem_size = 0; 111 112 /* Find the memory region containing the kernel */ 113 for_each_memblock(memory, reg) { 114 phys_addr_t vmlinux_end = __pa(_end); 115 phys_addr_t end = reg->base + reg->size; 116 117 if (reg->base <= vmlinux_end && vmlinux_end <= end) { 118 /* 119 * Reserve from the start of the region to the end of 120 * the kernel 121 */ 122 memblock_reserve(reg->base, vmlinux_end - reg->base); 123 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET); 124 125 /* 126 * Remove memblock from the end of usable area to the 127 * end of region 128 */ 129 if (reg->base + mem_size < end) 130 memblock_remove(reg->base + mem_size, 131 end - reg->base - mem_size); 132 } 133 } 134 BUG_ON(mem_size == 0); 135 136 set_max_mapnr(PFN_DOWN(mem_size)); 137 max_low_pfn = PFN_DOWN(memblock_end_of_DRAM()); 138 139 #ifdef CONFIG_BLK_DEV_INITRD 140 setup_initrd(); 141 #endif /* CONFIG_BLK_DEV_INITRD */ 142 143 early_init_fdt_reserve_self(); 144 early_init_fdt_scan_reserved_mem(); 145 memblock_allow_resize(); 146 memblock_dump_all(); 147 148 for_each_memblock(memory, reg) { 149 unsigned long start_pfn = memblock_region_memory_base_pfn(reg); 150 unsigned long end_pfn = memblock_region_memory_end_pfn(reg); 151 152 memblock_set_node(PFN_PHYS(start_pfn), 153 PFN_PHYS(end_pfn - start_pfn), 154 &memblock.memory, 0); 155 } 156 } 157 158 unsigned long va_pa_offset; 159 EXPORT_SYMBOL(va_pa_offset); 160 unsigned long pfn_base; 161 EXPORT_SYMBOL(pfn_base); 162 163 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; 164 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); 165 166 #ifndef __PAGETABLE_PMD_FOLDED 167 #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT) 168 pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss; 169 pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); 170 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; 171 #endif 172 173 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; 174 175 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) 176 { 177 unsigned long addr = __fix_to_virt(idx); 178 pte_t *ptep; 179 180 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 181 182 ptep = &fixmap_pte[pte_index(addr)]; 183 184 if (pgprot_val(prot)) { 185 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot)); 186 } else { 187 pte_clear(&init_mm, addr, ptep); 188 local_flush_tlb_page(addr); 189 } 190 } 191 192 /* 193 * setup_vm() is called from head.S with MMU-off. 194 * 195 * Following requirements should be honoured for setup_vm() to work 196 * correctly: 197 * 1) It should use PC-relative addressing for accessing kernel symbols. 198 * To achieve this we always use GCC cmodel=medany. 199 * 2) The compiler instrumentation for FTRACE will not work for setup_vm() 200 * so disable compiler instrumentation when FTRACE is enabled. 201 * 202 * Currently, the above requirements are honoured by using custom CFLAGS 203 * for init.o in mm/Makefile. 204 */ 205 206 #ifndef __riscv_cmodel_medany 207 #error "setup_vm() is called from head.S before relocate so it should " 208 "not use absolute addressing." 209 #endif 210 211 asmlinkage void __init setup_vm(void) 212 { 213 extern char _start; 214 uintptr_t i; 215 uintptr_t pa = (uintptr_t) &_start; 216 pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC); 217 218 va_pa_offset = PAGE_OFFSET - pa; 219 pfn_base = PFN_DOWN(pa); 220 221 /* Sanity check alignment and size */ 222 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); 223 BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0); 224 225 #ifndef __PAGETABLE_PMD_FOLDED 226 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] = 227 pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd), 228 __pgprot(_PAGE_TABLE)); 229 trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot); 230 231 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) { 232 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i; 233 234 swapper_pg_dir[o] = 235 pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i, 236 __pgprot(_PAGE_TABLE)); 237 } 238 for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++) 239 swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot); 240 241 swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] = 242 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pmd), 243 __pgprot(_PAGE_TABLE)); 244 fixmap_pmd[(FIXADDR_START >> PMD_SHIFT) % PTRS_PER_PMD] = 245 pfn_pmd(PFN_DOWN((uintptr_t)fixmap_pte), 246 __pgprot(_PAGE_TABLE)); 247 #else 248 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] = 249 pfn_pgd(PFN_DOWN(pa), prot); 250 251 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) { 252 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i; 253 254 swapper_pg_dir[o] = 255 pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot); 256 } 257 258 swapper_pg_dir[(FIXADDR_START >> PGDIR_SHIFT) % PTRS_PER_PGD] = 259 pfn_pgd(PFN_DOWN((uintptr_t)fixmap_pte), 260 __pgprot(_PAGE_TABLE)); 261 #endif 262 } 263