David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/mm/init.c |
| 4 | * |
| 5 | * Copyright (C) 1995-2005 Russell King |
| 6 | * Copyright (C) 2012 ARM Ltd. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/swap.h> |
| 13 | #include <linux/init.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | #include <linux/cache.h> |
| 15 | #include <linux/mman.h> |
| 16 | #include <linux/nodemask.h> |
| 17 | #include <linux/initrd.h> |
| 18 | #include <linux/gfp.h> |
| 19 | #include <linux/memblock.h> |
| 20 | #include <linux/sort.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_fdt.h> |
| 23 | #include <linux/dma-mapping.h> |
| 24 | #include <linux/dma-contiguous.h> |
| 25 | #include <linux/efi.h> |
| 26 | #include <linux/swiotlb.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/kexec.h> |
| 30 | #include <linux/crash_dump.h> |
| 31 | |
| 32 | #include <asm/boot.h> |
| 33 | #include <asm/fixmap.h> |
| 34 | #include <asm/kasan.h> |
| 35 | #include <asm/kernel-pgtable.h> |
| 36 | #include <asm/memory.h> |
| 37 | #include <asm/numa.h> |
| 38 | #include <asm/sections.h> |
| 39 | #include <asm/setup.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 40 | #include <linux/sizes.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | #include <asm/tlb.h> |
| 42 | #include <asm/alternative.h> |
| 43 | |
| 44 | /* |
| 45 | * We need to be able to catch inadvertent references to memstart_addr |
| 46 | * that occur (potentially in generic code) before arm64_memblock_init() |
| 47 | * executes, which assigns it its actual value. So use a default value |
| 48 | * that cannot be mistaken for a real physical address. |
| 49 | */ |
| 50 | s64 memstart_addr __ro_after_init = -1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 51 | EXPORT_SYMBOL(memstart_addr); |
| 52 | |
| 53 | s64 physvirt_offset __ro_after_init; |
| 54 | EXPORT_SYMBOL(physvirt_offset); |
| 55 | |
| 56 | struct page *vmemmap __ro_after_init; |
| 57 | EXPORT_SYMBOL(vmemmap); |
| 58 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 59 | phys_addr_t arm64_dma_phys_limit __ro_after_init; |
| 60 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_KEXEC_CORE |
| 62 | /* |
| 63 | * reserve_crashkernel() - reserves memory for crash kernel |
| 64 | * |
| 65 | * This function reserves memory area given in "crashkernel=" kernel command |
| 66 | * line parameter. The memory reserved is used by dump capture kernel when |
| 67 | * primary kernel is crashing. |
| 68 | */ |
| 69 | static void __init reserve_crashkernel(void) |
| 70 | { |
| 71 | unsigned long long crash_base, crash_size; |
| 72 | int ret; |
| 73 | |
| 74 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
| 75 | &crash_size, &crash_base); |
| 76 | /* no crashkernel= or invalid value specified */ |
| 77 | if (ret || !crash_size) |
| 78 | return; |
| 79 | |
| 80 | crash_size = PAGE_ALIGN(crash_size); |
| 81 | |
| 82 | if (crash_base == 0) { |
| 83 | /* Current arm64 boot protocol requires 2MB alignment */ |
| 84 | crash_base = memblock_find_in_range(0, ARCH_LOW_ADDRESS_LIMIT, |
| 85 | crash_size, SZ_2M); |
| 86 | if (crash_base == 0) { |
| 87 | pr_warn("cannot allocate crashkernel (size:0x%llx)\n", |
| 88 | crash_size); |
| 89 | return; |
| 90 | } |
| 91 | } else { |
| 92 | /* User specifies base address explicitly. */ |
| 93 | if (!memblock_is_region_memory(crash_base, crash_size)) { |
| 94 | pr_warn("cannot reserve crashkernel: region is not memory\n"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (memblock_is_region_reserved(crash_base, crash_size)) { |
| 99 | pr_warn("cannot reserve crashkernel: region overlaps reserved memory\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (!IS_ALIGNED(crash_base, SZ_2M)) { |
| 104 | pr_warn("cannot reserve crashkernel: base address is not 2MB aligned\n"); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | memblock_reserve(crash_base, crash_size); |
| 109 | |
| 110 | pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", |
| 111 | crash_base, crash_base + crash_size, crash_size >> 20); |
| 112 | |
| 113 | crashk_res.start = crash_base; |
| 114 | crashk_res.end = crash_base + crash_size - 1; |
| 115 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | #else |
| 117 | static void __init reserve_crashkernel(void) |
| 118 | { |
| 119 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 120 | #endif /* CONFIG_KEXEC_CORE */ |
| 121 | |
| 122 | #ifdef CONFIG_CRASH_DUMP |
| 123 | static int __init early_init_dt_scan_elfcorehdr(unsigned long node, |
| 124 | const char *uname, int depth, void *data) |
| 125 | { |
| 126 | const __be32 *reg; |
| 127 | int len; |
| 128 | |
| 129 | if (depth != 1 || strcmp(uname, "chosen") != 0) |
| 130 | return 0; |
| 131 | |
| 132 | reg = of_get_flat_dt_prop(node, "linux,elfcorehdr", &len); |
| 133 | if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells))) |
| 134 | return 1; |
| 135 | |
| 136 | elfcorehdr_addr = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 137 | elfcorehdr_size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 138 | |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * reserve_elfcorehdr() - reserves memory for elf core header |
| 144 | * |
| 145 | * This function reserves the memory occupied by an elf core header |
| 146 | * described in the device tree. This region contains all the |
| 147 | * information about primary kernel's core image and is used by a dump |
| 148 | * capture kernel to access the system memory on primary kernel. |
| 149 | */ |
| 150 | static void __init reserve_elfcorehdr(void) |
| 151 | { |
| 152 | of_scan_flat_dt(early_init_dt_scan_elfcorehdr, NULL); |
| 153 | |
| 154 | if (!elfcorehdr_size) |
| 155 | return; |
| 156 | |
| 157 | if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) { |
| 158 | pr_warn("elfcorehdr is overlapped\n"); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | memblock_reserve(elfcorehdr_addr, elfcorehdr_size); |
| 163 | |
| 164 | pr_info("Reserving %lldKB of memory at 0x%llx for elfcorehdr\n", |
| 165 | elfcorehdr_size >> 10, elfcorehdr_addr); |
| 166 | } |
| 167 | #else |
| 168 | static void __init reserve_elfcorehdr(void) |
| 169 | { |
| 170 | } |
| 171 | #endif /* CONFIG_CRASH_DUMP */ |
| 172 | /* |
| 173 | * Return the maximum physical address for ZONE_DMA32 (DMA_BIT_MASK(32)). It |
| 174 | * currently assumes that for memory starting above 4G, 32-bit devices will |
| 175 | * use a DMA offset. |
| 176 | */ |
| 177 | static phys_addr_t __init max_zone_dma_phys(void) |
| 178 | { |
| 179 | phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32); |
| 180 | return min(offset + (1ULL << 32), memblock_end_of_DRAM()); |
| 181 | } |
| 182 | |
| 183 | #ifdef CONFIG_NUMA |
| 184 | |
| 185 | static void __init zone_sizes_init(unsigned long min, unsigned long max) |
| 186 | { |
| 187 | unsigned long max_zone_pfns[MAX_NR_ZONES] = {0}; |
| 188 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 189 | #ifdef CONFIG_ZONE_DMA32 |
| 190 | max_zone_pfns[ZONE_DMA32] = PFN_DOWN(max_zone_dma_phys()); |
| 191 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 192 | max_zone_pfns[ZONE_NORMAL] = max; |
| 193 | |
| 194 | free_area_init_nodes(max_zone_pfns); |
| 195 | } |
| 196 | |
| 197 | #else |
| 198 | |
| 199 | static void __init zone_sizes_init(unsigned long min, unsigned long max) |
| 200 | { |
| 201 | struct memblock_region *reg; |
| 202 | unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; |
| 203 | unsigned long max_dma = min; |
| 204 | |
| 205 | memset(zone_size, 0, sizeof(zone_size)); |
| 206 | |
| 207 | /* 4GB maximum for 32-bit only capable devices */ |
| 208 | #ifdef CONFIG_ZONE_DMA32 |
| 209 | max_dma = PFN_DOWN(arm64_dma_phys_limit); |
| 210 | zone_size[ZONE_DMA32] = max_dma - min; |
| 211 | #endif |
| 212 | zone_size[ZONE_NORMAL] = max - max_dma; |
| 213 | |
| 214 | memcpy(zhole_size, zone_size, sizeof(zhole_size)); |
| 215 | |
| 216 | for_each_memblock(memory, reg) { |
| 217 | unsigned long start = memblock_region_memory_base_pfn(reg); |
| 218 | unsigned long end = memblock_region_memory_end_pfn(reg); |
| 219 | |
| 220 | if (start >= max) |
| 221 | continue; |
| 222 | |
| 223 | #ifdef CONFIG_ZONE_DMA32 |
| 224 | if (start < max_dma) { |
| 225 | unsigned long dma_end = min(end, max_dma); |
| 226 | zhole_size[ZONE_DMA32] -= dma_end - start; |
| 227 | } |
| 228 | #endif |
| 229 | if (end > max_dma) { |
| 230 | unsigned long normal_end = min(end, max); |
| 231 | unsigned long normal_start = max(start, max_dma); |
| 232 | zhole_size[ZONE_NORMAL] -= normal_end - normal_start; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | free_area_init_node(0, zone_size, min, zhole_size); |
| 237 | } |
| 238 | |
| 239 | #endif /* CONFIG_NUMA */ |
| 240 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 241 | int pfn_valid(unsigned long pfn) |
| 242 | { |
| 243 | phys_addr_t addr = pfn << PAGE_SHIFT; |
| 244 | |
| 245 | if ((addr >> PAGE_SHIFT) != pfn) |
| 246 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 247 | |
| 248 | #ifdef CONFIG_SPARSEMEM |
| 249 | if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS) |
| 250 | return 0; |
| 251 | |
| 252 | if (!valid_section(__nr_to_section(pfn_to_section_nr(pfn)))) |
| 253 | return 0; |
| 254 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 255 | return memblock_is_map_memory(addr); |
| 256 | } |
| 257 | EXPORT_SYMBOL(pfn_valid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 258 | |
| 259 | static phys_addr_t memory_limit = PHYS_ADDR_MAX; |
| 260 | |
| 261 | /* |
| 262 | * Limit the memory size that was specified via FDT. |
| 263 | */ |
| 264 | static int __init early_mem(char *p) |
| 265 | { |
| 266 | if (!p) |
| 267 | return 1; |
| 268 | |
| 269 | memory_limit = memparse(p, &p) & PAGE_MASK; |
| 270 | pr_notice("Memory limited to %lldMB\n", memory_limit >> 20); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | early_param("mem", early_mem); |
| 275 | |
| 276 | static int __init early_init_dt_scan_usablemem(unsigned long node, |
| 277 | const char *uname, int depth, void *data) |
| 278 | { |
| 279 | struct memblock_region *usablemem = data; |
| 280 | const __be32 *reg; |
| 281 | int len; |
| 282 | |
| 283 | if (depth != 1 || strcmp(uname, "chosen") != 0) |
| 284 | return 0; |
| 285 | |
| 286 | reg = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len); |
| 287 | if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells))) |
| 288 | return 1; |
| 289 | |
| 290 | usablemem->base = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 291 | usablemem->size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 292 | |
| 293 | return 1; |
| 294 | } |
| 295 | |
| 296 | static void __init fdt_enforce_memory_region(void) |
| 297 | { |
| 298 | struct memblock_region reg = { |
| 299 | .size = 0, |
| 300 | }; |
| 301 | |
| 302 | of_scan_flat_dt(early_init_dt_scan_usablemem, ®); |
| 303 | |
| 304 | if (reg.size) |
| 305 | memblock_cap_memory_range(reg.base, reg.size); |
| 306 | } |
| 307 | |
| 308 | void __init arm64_memblock_init(void) |
| 309 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 310 | const s64 linear_region_size = BIT(vabits_actual - 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 311 | |
| 312 | /* Handle linux,usable-memory-range property */ |
| 313 | fdt_enforce_memory_region(); |
| 314 | |
| 315 | /* Remove memory above our supported physical address size */ |
| 316 | memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX); |
| 317 | |
| 318 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 319 | * Select a suitable value for the base of physical memory. |
| 320 | */ |
| 321 | memstart_addr = round_down(memblock_start_of_DRAM(), |
| 322 | ARM64_MEMSTART_ALIGN); |
| 323 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 324 | physvirt_offset = PHYS_OFFSET - PAGE_OFFSET; |
| 325 | |
| 326 | vmemmap = ((struct page *)VMEMMAP_START - (memstart_addr >> PAGE_SHIFT)); |
| 327 | |
| 328 | /* |
| 329 | * If we are running with a 52-bit kernel VA config on a system that |
| 330 | * does not support it, we have to offset our vmemmap and physvirt_offset |
| 331 | * s.t. we avoid the 52-bit portion of the direct linear map |
| 332 | */ |
| 333 | if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52)) { |
| 334 | vmemmap += (_PAGE_OFFSET(48) - _PAGE_OFFSET(52)) >> PAGE_SHIFT; |
| 335 | physvirt_offset = PHYS_OFFSET - _PAGE_OFFSET(48); |
| 336 | } |
| 337 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 338 | /* |
| 339 | * Remove the memory that we will not be able to cover with the |
| 340 | * linear mapping. Take care not to clip the kernel which may be |
| 341 | * high in memory. |
| 342 | */ |
| 343 | memblock_remove(max_t(u64, memstart_addr + linear_region_size, |
| 344 | __pa_symbol(_end)), ULLONG_MAX); |
| 345 | if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) { |
| 346 | /* ensure that memstart_addr remains sufficiently aligned */ |
| 347 | memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size, |
| 348 | ARM64_MEMSTART_ALIGN); |
| 349 | memblock_remove(0, memstart_addr); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Apply the memory limit if it was set. Since the kernel may be loaded |
| 354 | * high up in memory, add back the kernel region that must be accessible |
| 355 | * via the linear mapping. |
| 356 | */ |
| 357 | if (memory_limit != PHYS_ADDR_MAX) { |
| 358 | memblock_mem_limit_remove_map(memory_limit); |
| 359 | memblock_add(__pa_symbol(_text), (u64)(_end - _text)); |
| 360 | } |
| 361 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 362 | if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 363 | /* |
| 364 | * Add back the memory we just removed if it results in the |
| 365 | * initrd to become inaccessible via the linear mapping. |
| 366 | * Otherwise, this is a no-op |
| 367 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 368 | u64 base = phys_initrd_start & PAGE_MASK; |
| 369 | u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * We can only add back the initrd memory if we don't end up |
| 373 | * with more memory than we can address via the linear mapping. |
| 374 | * It is up to the bootloader to position the kernel and the |
| 375 | * initrd reasonably close to each other (i.e., within 32 GB of |
| 376 | * each other) so that all granule/#levels combinations can |
| 377 | * always access both. |
| 378 | */ |
| 379 | if (WARN(base < memblock_start_of_DRAM() || |
| 380 | base + size > memblock_start_of_DRAM() + |
| 381 | linear_region_size, |
| 382 | "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 383 | phys_initrd_size = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 384 | } else { |
| 385 | memblock_remove(base, size); /* clear MEMBLOCK_ flags */ |
| 386 | memblock_add(base, size); |
| 387 | memblock_reserve(base, size); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { |
| 392 | extern u16 memstart_offset_seed; |
| 393 | u64 range = linear_region_size - |
| 394 | (memblock_end_of_DRAM() - memblock_start_of_DRAM()); |
| 395 | |
| 396 | /* |
| 397 | * If the size of the linear region exceeds, by a sufficient |
| 398 | * margin, the size of the region that the available physical |
| 399 | * memory spans, randomize the linear region as well. |
| 400 | */ |
| 401 | if (memstart_offset_seed > 0 && range >= ARM64_MEMSTART_ALIGN) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 402 | range /= ARM64_MEMSTART_ALIGN; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 403 | memstart_addr -= ARM64_MEMSTART_ALIGN * |
| 404 | ((range * memstart_offset_seed) >> 16); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Register the kernel text, kernel data, initrd, and initial |
| 410 | * pagetables with memblock. |
| 411 | */ |
| 412 | memblock_reserve(__pa_symbol(_text), _end - _text); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 413 | if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 414 | /* the generic initrd code expects virtual addresses */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 415 | initrd_start = __phys_to_virt(phys_initrd_start); |
| 416 | initrd_end = initrd_start + phys_initrd_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 417 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 418 | |
| 419 | early_init_fdt_scan_reserved_mem(); |
| 420 | |
| 421 | /* 4GB maximum for 32-bit only capable devices */ |
| 422 | if (IS_ENABLED(CONFIG_ZONE_DMA32)) |
| 423 | arm64_dma_phys_limit = max_zone_dma_phys(); |
| 424 | else |
| 425 | arm64_dma_phys_limit = PHYS_MASK + 1; |
| 426 | |
| 427 | reserve_crashkernel(); |
| 428 | |
| 429 | reserve_elfcorehdr(); |
| 430 | |
| 431 | high_memory = __va(memblock_end_of_DRAM() - 1) + 1; |
| 432 | |
| 433 | dma_contiguous_reserve(arm64_dma_phys_limit); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void __init bootmem_init(void) |
| 437 | { |
| 438 | unsigned long min, max; |
| 439 | |
| 440 | min = PFN_UP(memblock_start_of_DRAM()); |
| 441 | max = PFN_DOWN(memblock_end_of_DRAM()); |
| 442 | |
| 443 | early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT); |
| 444 | |
| 445 | max_pfn = max_low_pfn = max; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 446 | min_low_pfn = min; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 447 | |
| 448 | arm64_numa_init(); |
| 449 | /* |
| 450 | * Sparsemem tries to allocate bootmem in memory_present(), so must be |
| 451 | * done after the fixed reservations. |
| 452 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 453 | memblocks_present(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 454 | |
| 455 | sparse_init(); |
| 456 | zone_sizes_init(min, max); |
| 457 | |
| 458 | memblock_dump_all(); |
| 459 | } |
| 460 | |
| 461 | #ifndef CONFIG_SPARSEMEM_VMEMMAP |
| 462 | static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn) |
| 463 | { |
| 464 | struct page *start_pg, *end_pg; |
| 465 | unsigned long pg, pgend; |
| 466 | |
| 467 | /* |
| 468 | * Convert start_pfn/end_pfn to a struct page pointer. |
| 469 | */ |
| 470 | start_pg = pfn_to_page(start_pfn - 1) + 1; |
| 471 | end_pg = pfn_to_page(end_pfn - 1) + 1; |
| 472 | |
| 473 | /* |
| 474 | * Convert to physical addresses, and round start upwards and end |
| 475 | * downwards. |
| 476 | */ |
| 477 | pg = (unsigned long)PAGE_ALIGN(__pa(start_pg)); |
| 478 | pgend = (unsigned long)__pa(end_pg) & PAGE_MASK; |
| 479 | |
| 480 | /* |
| 481 | * If there are free pages between these, free the section of the |
| 482 | * memmap array. |
| 483 | */ |
| 484 | if (pg < pgend) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 485 | memblock_free(pg, pgend - pg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* |
| 489 | * The mem_map array can get very big. Free the unused area of the memory map. |
| 490 | */ |
| 491 | static void __init free_unused_memmap(void) |
| 492 | { |
| 493 | unsigned long start, prev_end = 0; |
| 494 | struct memblock_region *reg; |
| 495 | |
| 496 | for_each_memblock(memory, reg) { |
| 497 | start = __phys_to_pfn(reg->base); |
| 498 | |
| 499 | #ifdef CONFIG_SPARSEMEM |
| 500 | /* |
| 501 | * Take care not to free memmap entries that don't exist due |
| 502 | * to SPARSEMEM sections which aren't present. |
| 503 | */ |
| 504 | start = min(start, ALIGN(prev_end, PAGES_PER_SECTION)); |
| 505 | #endif |
| 506 | /* |
| 507 | * If we had a previous bank, and there is a space between the |
| 508 | * current bank and the previous, free it. |
| 509 | */ |
| 510 | if (prev_end && prev_end < start) |
| 511 | free_memmap(prev_end, start); |
| 512 | |
| 513 | /* |
| 514 | * Align up here since the VM subsystem insists that the |
| 515 | * memmap entries are valid from the bank end aligned to |
| 516 | * MAX_ORDER_NR_PAGES. |
| 517 | */ |
| 518 | prev_end = ALIGN(__phys_to_pfn(reg->base + reg->size), |
| 519 | MAX_ORDER_NR_PAGES); |
| 520 | } |
| 521 | |
| 522 | #ifdef CONFIG_SPARSEMEM |
| 523 | if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) |
| 524 | free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION)); |
| 525 | #endif |
| 526 | } |
| 527 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ |
| 528 | |
| 529 | /* |
| 530 | * mem_init() marks the free areas in the mem_map and tells us how much memory |
| 531 | * is free. This is done after various parts of the system have claimed their |
| 532 | * memory after the kernel image. |
| 533 | */ |
| 534 | void __init mem_init(void) |
| 535 | { |
| 536 | if (swiotlb_force == SWIOTLB_FORCE || |
| 537 | max_pfn > (arm64_dma_phys_limit >> PAGE_SHIFT)) |
| 538 | swiotlb_init(1); |
| 539 | else |
| 540 | swiotlb_force = SWIOTLB_NO_FORCE; |
| 541 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 542 | set_max_mapnr(max_pfn - PHYS_PFN_OFFSET); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 543 | |
| 544 | #ifndef CONFIG_SPARSEMEM_VMEMMAP |
| 545 | free_unused_memmap(); |
| 546 | #endif |
| 547 | /* this will put all unused low memory onto the freelists */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 548 | memblock_free_all(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 549 | |
| 550 | mem_init_print_info(NULL); |
| 551 | |
| 552 | /* |
| 553 | * Check boundaries twice: Some fundamental inconsistencies can be |
| 554 | * detected at build time already. |
| 555 | */ |
| 556 | #ifdef CONFIG_COMPAT |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 557 | BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 558 | #endif |
| 559 | |
| 560 | if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) { |
| 561 | extern int sysctl_overcommit_memory; |
| 562 | /* |
| 563 | * On a machine this small we won't get anywhere without |
| 564 | * overcommit, so turn it on by default. |
| 565 | */ |
| 566 | sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void free_initmem(void) |
| 571 | { |
| 572 | free_reserved_area(lm_alias(__init_begin), |
| 573 | lm_alias(__init_end), |
| 574 | 0, "unused kernel"); |
| 575 | /* |
| 576 | * Unmap the __init region but leave the VM area in place. This |
| 577 | * prevents the region from being reused for kernel modules, which |
| 578 | * is not supported by kallsyms. |
| 579 | */ |
| 580 | unmap_kernel_range((u64)__init_begin, (u64)(__init_end - __init_begin)); |
| 581 | } |
| 582 | |
| 583 | #ifdef CONFIG_BLK_DEV_INITRD |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | void __init free_initrd_mem(unsigned long start, unsigned long end) |
| 585 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 586 | unsigned long aligned_start, aligned_end; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 587 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 588 | aligned_start = __virt_to_phys(start) & PAGE_MASK; |
| 589 | aligned_end = PAGE_ALIGN(__virt_to_phys(end)); |
| 590 | memblock_free(aligned_start, aligned_end - aligned_start); |
| 591 | free_reserved_area((void *)start, (void *)end, 0, "initrd"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 592 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 593 | #endif |
| 594 | |
| 595 | /* |
| 596 | * Dump out memory limit information on panic. |
| 597 | */ |
| 598 | static int dump_mem_limit(struct notifier_block *self, unsigned long v, void *p) |
| 599 | { |
| 600 | if (memory_limit != PHYS_ADDR_MAX) { |
| 601 | pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20); |
| 602 | } else { |
| 603 | pr_emerg("Memory Limit: none\n"); |
| 604 | } |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static struct notifier_block mem_limit_notifier = { |
| 609 | .notifier_call = dump_mem_limit, |
| 610 | }; |
| 611 | |
| 612 | static int __init register_mem_limit_dumper(void) |
| 613 | { |
| 614 | atomic_notifier_chain_register(&panic_notifier_list, |
| 615 | &mem_limit_notifier); |
| 616 | return 0; |
| 617 | } |
| 618 | __initcall(register_mem_limit_dumper); |