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