1c1cc1552SCatalin Marinas /* 2c1cc1552SCatalin Marinas * Based on arch/arm/mm/init.c 3c1cc1552SCatalin Marinas * 4c1cc1552SCatalin Marinas * Copyright (C) 1995-2005 Russell King 5c1cc1552SCatalin Marinas * Copyright (C) 2012 ARM Ltd. 6c1cc1552SCatalin Marinas * 7c1cc1552SCatalin Marinas * This program is free software; you can redistribute it and/or modify 8c1cc1552SCatalin Marinas * it under the terms of the GNU General Public License version 2 as 9c1cc1552SCatalin Marinas * published by the Free Software Foundation. 10c1cc1552SCatalin Marinas * 11c1cc1552SCatalin Marinas * This program is distributed in the hope that it will be useful, 12c1cc1552SCatalin Marinas * but WITHOUT ANY WARRANTY; without even the implied warranty of 13c1cc1552SCatalin Marinas * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14c1cc1552SCatalin Marinas * GNU General Public License for more details. 15c1cc1552SCatalin Marinas * 16c1cc1552SCatalin Marinas * You should have received a copy of the GNU General Public License 17c1cc1552SCatalin Marinas * along with this program. If not, see <http://www.gnu.org/licenses/>. 18c1cc1552SCatalin Marinas */ 19c1cc1552SCatalin Marinas 20c1cc1552SCatalin Marinas #include <linux/kernel.h> 21c1cc1552SCatalin Marinas #include <linux/export.h> 22c1cc1552SCatalin Marinas #include <linux/errno.h> 23c1cc1552SCatalin Marinas #include <linux/swap.h> 24c1cc1552SCatalin Marinas #include <linux/init.h> 25c1cc1552SCatalin Marinas #include <linux/bootmem.h> 26c1cc1552SCatalin Marinas #include <linux/mman.h> 27c1cc1552SCatalin Marinas #include <linux/nodemask.h> 28c1cc1552SCatalin Marinas #include <linux/initrd.h> 29c1cc1552SCatalin Marinas #include <linux/gfp.h> 30c1cc1552SCatalin Marinas #include <linux/memblock.h> 31c1cc1552SCatalin Marinas #include <linux/sort.h> 32c1cc1552SCatalin Marinas #include <linux/of_fdt.h> 3319e7640dSCatalin Marinas #include <linux/dma-mapping.h> 346ac2104dSLaura Abbott #include <linux/dma-contiguous.h> 35c1cc1552SCatalin Marinas 36c1cc1552SCatalin Marinas #include <asm/sections.h> 37c1cc1552SCatalin Marinas #include <asm/setup.h> 38c1cc1552SCatalin Marinas #include <asm/sizes.h> 39c1cc1552SCatalin Marinas #include <asm/tlb.h> 40c1cc1552SCatalin Marinas 41c1cc1552SCatalin Marinas #include "mm.h" 42c1cc1552SCatalin Marinas 43c1cc1552SCatalin Marinas phys_addr_t memstart_addr __read_mostly = 0; 44c1cc1552SCatalin Marinas 45ec2eaa73SRob Herring #ifdef CONFIG_BLK_DEV_INITRD 46c1cc1552SCatalin Marinas static int __init early_initrd(char *p) 47c1cc1552SCatalin Marinas { 48c1cc1552SCatalin Marinas unsigned long start, size; 49c1cc1552SCatalin Marinas char *endp; 50c1cc1552SCatalin Marinas 51c1cc1552SCatalin Marinas start = memparse(p, &endp); 52c1cc1552SCatalin Marinas if (*endp == ',') { 53c1cc1552SCatalin Marinas size = memparse(endp + 1, NULL); 54c1cc1552SCatalin Marinas 55ec2eaa73SRob Herring initrd_start = (unsigned long)__va(start); 56ec2eaa73SRob Herring initrd_end = (unsigned long)__va(start + size); 57c1cc1552SCatalin Marinas } 58c1cc1552SCatalin Marinas return 0; 59c1cc1552SCatalin Marinas } 60c1cc1552SCatalin Marinas early_param("initrd", early_initrd); 61ec2eaa73SRob Herring #endif 62c1cc1552SCatalin Marinas 63*d50314a6SCatalin Marinas /* 64*d50314a6SCatalin Marinas * Return the maximum physical address for ZONE_DMA (DMA_BIT_MASK(32)). It 65*d50314a6SCatalin Marinas * currently assumes that for memory starting above 4G, 32-bit devices will 66*d50314a6SCatalin Marinas * use a DMA offset. 67*d50314a6SCatalin Marinas */ 68*d50314a6SCatalin Marinas static phys_addr_t max_zone_dma_phys(void) 69*d50314a6SCatalin Marinas { 70*d50314a6SCatalin Marinas phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32); 71*d50314a6SCatalin Marinas return min(offset + (1ULL << 32), memblock_end_of_DRAM()); 72*d50314a6SCatalin Marinas } 73*d50314a6SCatalin Marinas 74c1cc1552SCatalin Marinas static void __init zone_sizes_init(unsigned long min, unsigned long max) 75c1cc1552SCatalin Marinas { 76c1cc1552SCatalin Marinas struct memblock_region *reg; 77c1cc1552SCatalin Marinas unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 7819e7640dSCatalin Marinas unsigned long max_dma = min; 79c1cc1552SCatalin Marinas 80c1cc1552SCatalin Marinas memset(zone_size, 0, sizeof(zone_size)); 81c1cc1552SCatalin Marinas 82c1cc1552SCatalin Marinas /* 4GB maximum for 32-bit only capable devices */ 8319e7640dSCatalin Marinas if (IS_ENABLED(CONFIG_ZONE_DMA)) { 84*d50314a6SCatalin Marinas max_dma = PFN_DOWN(max_zone_dma_phys()); 8519e7640dSCatalin Marinas zone_size[ZONE_DMA] = max_dma - min; 8619e7640dSCatalin Marinas } 8719e7640dSCatalin Marinas zone_size[ZONE_NORMAL] = max - max_dma; 88c1cc1552SCatalin Marinas 89c1cc1552SCatalin Marinas memcpy(zhole_size, zone_size, sizeof(zhole_size)); 90c1cc1552SCatalin Marinas 91c1cc1552SCatalin Marinas for_each_memblock(memory, reg) { 92c1cc1552SCatalin Marinas unsigned long start = memblock_region_memory_base_pfn(reg); 93c1cc1552SCatalin Marinas unsigned long end = memblock_region_memory_end_pfn(reg); 94c1cc1552SCatalin Marinas 95c1cc1552SCatalin Marinas if (start >= max) 96c1cc1552SCatalin Marinas continue; 9719e7640dSCatalin Marinas 9819e7640dSCatalin Marinas if (IS_ENABLED(CONFIG_ZONE_DMA) && start < max_dma) { 9919e7640dSCatalin Marinas unsigned long dma_end = min(end, max_dma); 10019e7640dSCatalin Marinas zhole_size[ZONE_DMA] -= dma_end - start; 101c1cc1552SCatalin Marinas } 10219e7640dSCatalin Marinas 10319e7640dSCatalin Marinas if (end > max_dma) { 104c1cc1552SCatalin Marinas unsigned long normal_end = min(end, max); 10519e7640dSCatalin Marinas unsigned long normal_start = max(start, max_dma); 106c1cc1552SCatalin Marinas zhole_size[ZONE_NORMAL] -= normal_end - normal_start; 107c1cc1552SCatalin Marinas } 108c1cc1552SCatalin Marinas } 109c1cc1552SCatalin Marinas 110c1cc1552SCatalin Marinas free_area_init_node(0, zone_size, min, zhole_size); 111c1cc1552SCatalin Marinas } 112c1cc1552SCatalin Marinas 113c1cc1552SCatalin Marinas #ifdef CONFIG_HAVE_ARCH_PFN_VALID 114c1cc1552SCatalin Marinas int pfn_valid(unsigned long pfn) 115c1cc1552SCatalin Marinas { 116c1cc1552SCatalin Marinas return memblock_is_memory(pfn << PAGE_SHIFT); 117c1cc1552SCatalin Marinas } 118c1cc1552SCatalin Marinas EXPORT_SYMBOL(pfn_valid); 119c1cc1552SCatalin Marinas #endif 120c1cc1552SCatalin Marinas 121c1cc1552SCatalin Marinas #ifndef CONFIG_SPARSEMEM 122c1cc1552SCatalin Marinas static void arm64_memory_present(void) 123c1cc1552SCatalin Marinas { 124c1cc1552SCatalin Marinas } 125c1cc1552SCatalin Marinas #else 126c1cc1552SCatalin Marinas static void arm64_memory_present(void) 127c1cc1552SCatalin Marinas { 128c1cc1552SCatalin Marinas struct memblock_region *reg; 129c1cc1552SCatalin Marinas 130c1cc1552SCatalin Marinas for_each_memblock(memory, reg) 131c1cc1552SCatalin Marinas memory_present(0, memblock_region_memory_base_pfn(reg), 132c1cc1552SCatalin Marinas memblock_region_memory_end_pfn(reg)); 133c1cc1552SCatalin Marinas } 134c1cc1552SCatalin Marinas #endif 135c1cc1552SCatalin Marinas 136c1cc1552SCatalin Marinas void __init arm64_memblock_init(void) 137c1cc1552SCatalin Marinas { 1382d5a5612SCatalin Marinas phys_addr_t dma_phys_limit = 0; 1392d5a5612SCatalin Marinas 140c1cc1552SCatalin Marinas /* Register the kernel text, kernel data and initrd with memblock */ 141c1cc1552SCatalin Marinas memblock_reserve(__pa(_text), _end - _text); 142c1cc1552SCatalin Marinas #ifdef CONFIG_BLK_DEV_INITRD 143ec2eaa73SRob Herring if (initrd_start) 144ec2eaa73SRob Herring memblock_reserve(__virt_to_phys(initrd_start), initrd_end - initrd_start); 145c1cc1552SCatalin Marinas #endif 146c1cc1552SCatalin Marinas 147c1cc1552SCatalin Marinas /* 148c1cc1552SCatalin Marinas * Reserve the page tables. These are already in use, 149c1cc1552SCatalin Marinas * and can only be in node 0. 150c1cc1552SCatalin Marinas */ 151c1cc1552SCatalin Marinas memblock_reserve(__pa(swapper_pg_dir), SWAPPER_DIR_SIZE); 152c1cc1552SCatalin Marinas memblock_reserve(__pa(idmap_pg_dir), IDMAP_DIR_SIZE); 153c1cc1552SCatalin Marinas 1549bf14b7cSMarek Szyprowski early_init_fdt_scan_reserved_mem(); 1552d5a5612SCatalin Marinas 1562d5a5612SCatalin Marinas /* 4GB maximum for 32-bit only capable devices */ 1572d5a5612SCatalin Marinas if (IS_ENABLED(CONFIG_ZONE_DMA)) 158*d50314a6SCatalin Marinas dma_phys_limit = max_zone_dma_phys(); 1592d5a5612SCatalin Marinas dma_contiguous_reserve(dma_phys_limit); 1606ac2104dSLaura Abbott 161c1cc1552SCatalin Marinas memblock_allow_resize(); 162c1cc1552SCatalin Marinas memblock_dump_all(); 163c1cc1552SCatalin Marinas } 164c1cc1552SCatalin Marinas 165c1cc1552SCatalin Marinas void __init bootmem_init(void) 166c1cc1552SCatalin Marinas { 167c1cc1552SCatalin Marinas unsigned long min, max; 168c1cc1552SCatalin Marinas 169c1cc1552SCatalin Marinas min = PFN_UP(memblock_start_of_DRAM()); 170c1cc1552SCatalin Marinas max = PFN_DOWN(memblock_end_of_DRAM()); 171c1cc1552SCatalin Marinas 172c1cc1552SCatalin Marinas /* 173c1cc1552SCatalin Marinas * Sparsemem tries to allocate bootmem in memory_present(), so must be 174c1cc1552SCatalin Marinas * done after the fixed reservations. 175c1cc1552SCatalin Marinas */ 176c1cc1552SCatalin Marinas arm64_memory_present(); 177c1cc1552SCatalin Marinas 178c1cc1552SCatalin Marinas sparse_init(); 179c1cc1552SCatalin Marinas zone_sizes_init(min, max); 180c1cc1552SCatalin Marinas 181c1cc1552SCatalin Marinas high_memory = __va((max << PAGE_SHIFT) - 1) + 1; 182c1cc1552SCatalin Marinas max_pfn = max_low_pfn = max; 183c1cc1552SCatalin Marinas } 184c1cc1552SCatalin Marinas 185c1cc1552SCatalin Marinas #ifndef CONFIG_SPARSEMEM_VMEMMAP 186c1cc1552SCatalin Marinas static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn) 187c1cc1552SCatalin Marinas { 188c1cc1552SCatalin Marinas struct page *start_pg, *end_pg; 189c1cc1552SCatalin Marinas unsigned long pg, pgend; 190c1cc1552SCatalin Marinas 191c1cc1552SCatalin Marinas /* 192c1cc1552SCatalin Marinas * Convert start_pfn/end_pfn to a struct page pointer. 193c1cc1552SCatalin Marinas */ 194c1cc1552SCatalin Marinas start_pg = pfn_to_page(start_pfn - 1) + 1; 195c1cc1552SCatalin Marinas end_pg = pfn_to_page(end_pfn - 1) + 1; 196c1cc1552SCatalin Marinas 197c1cc1552SCatalin Marinas /* 198c1cc1552SCatalin Marinas * Convert to physical addresses, and round start upwards and end 199c1cc1552SCatalin Marinas * downwards. 200c1cc1552SCatalin Marinas */ 201c1cc1552SCatalin Marinas pg = (unsigned long)PAGE_ALIGN(__pa(start_pg)); 202c1cc1552SCatalin Marinas pgend = (unsigned long)__pa(end_pg) & PAGE_MASK; 203c1cc1552SCatalin Marinas 204c1cc1552SCatalin Marinas /* 205c1cc1552SCatalin Marinas * If there are free pages between these, free the section of the 206c1cc1552SCatalin Marinas * memmap array. 207c1cc1552SCatalin Marinas */ 208c1cc1552SCatalin Marinas if (pg < pgend) 209c1cc1552SCatalin Marinas free_bootmem(pg, pgend - pg); 210c1cc1552SCatalin Marinas } 211c1cc1552SCatalin Marinas 212c1cc1552SCatalin Marinas /* 213c1cc1552SCatalin Marinas * The mem_map array can get very big. Free the unused area of the memory map. 214c1cc1552SCatalin Marinas */ 215c1cc1552SCatalin Marinas static void __init free_unused_memmap(void) 216c1cc1552SCatalin Marinas { 217c1cc1552SCatalin Marinas unsigned long start, prev_end = 0; 218c1cc1552SCatalin Marinas struct memblock_region *reg; 219c1cc1552SCatalin Marinas 220c1cc1552SCatalin Marinas for_each_memblock(memory, reg) { 221c1cc1552SCatalin Marinas start = __phys_to_pfn(reg->base); 222c1cc1552SCatalin Marinas 223c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM 224c1cc1552SCatalin Marinas /* 225c1cc1552SCatalin Marinas * Take care not to free memmap entries that don't exist due 226c1cc1552SCatalin Marinas * to SPARSEMEM sections which aren't present. 227c1cc1552SCatalin Marinas */ 228c1cc1552SCatalin Marinas start = min(start, ALIGN(prev_end, PAGES_PER_SECTION)); 229c1cc1552SCatalin Marinas #endif 230c1cc1552SCatalin Marinas /* 231c1cc1552SCatalin Marinas * If we had a previous bank, and there is a space between the 232c1cc1552SCatalin Marinas * current bank and the previous, free it. 233c1cc1552SCatalin Marinas */ 234c1cc1552SCatalin Marinas if (prev_end && prev_end < start) 235c1cc1552SCatalin Marinas free_memmap(prev_end, start); 236c1cc1552SCatalin Marinas 237c1cc1552SCatalin Marinas /* 238c1cc1552SCatalin Marinas * Align up here since the VM subsystem insists that the 239c1cc1552SCatalin Marinas * memmap entries are valid from the bank end aligned to 240c1cc1552SCatalin Marinas * MAX_ORDER_NR_PAGES. 241c1cc1552SCatalin Marinas */ 242c1cc1552SCatalin Marinas prev_end = ALIGN(start + __phys_to_pfn(reg->size), 243c1cc1552SCatalin Marinas MAX_ORDER_NR_PAGES); 244c1cc1552SCatalin Marinas } 245c1cc1552SCatalin Marinas 246c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM 247c1cc1552SCatalin Marinas if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) 248c1cc1552SCatalin Marinas free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION)); 249c1cc1552SCatalin Marinas #endif 250c1cc1552SCatalin Marinas } 251c1cc1552SCatalin Marinas #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 252c1cc1552SCatalin Marinas 253c1cc1552SCatalin Marinas /* 254c1cc1552SCatalin Marinas * mem_init() marks the free areas in the mem_map and tells us how much memory 255c1cc1552SCatalin Marinas * is free. This is done after various parts of the system have claimed their 256c1cc1552SCatalin Marinas * memory after the kernel image. 257c1cc1552SCatalin Marinas */ 258c1cc1552SCatalin Marinas void __init mem_init(void) 259c1cc1552SCatalin Marinas { 260c1cc1552SCatalin Marinas max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map; 261c1cc1552SCatalin Marinas 262c1cc1552SCatalin Marinas #ifndef CONFIG_SPARSEMEM_VMEMMAP 263c1cc1552SCatalin Marinas free_unused_memmap(); 264c1cc1552SCatalin Marinas #endif 265bee4ebd1SJiang Liu /* this will put all unused low memory onto the freelists */ 2660c988534SJiang Liu free_all_bootmem(); 267c1cc1552SCatalin Marinas 2686879ea83SJiang Liu mem_init_print_info(NULL); 269c1cc1552SCatalin Marinas 270c1cc1552SCatalin Marinas #define MLK(b, t) b, t, ((t) - (b)) >> 10 271c1cc1552SCatalin Marinas #define MLM(b, t) b, t, ((t) - (b)) >> 20 272c1cc1552SCatalin Marinas #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K) 273c1cc1552SCatalin Marinas 274c1cc1552SCatalin Marinas pr_notice("Virtual kernel memory layout:\n" 275c1cc1552SCatalin Marinas " vmalloc : 0x%16lx - 0x%16lx (%6ld MB)\n" 276c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP 277c1cc1552SCatalin Marinas " vmemmap : 0x%16lx - 0x%16lx (%6ld MB)\n" 278c1cc1552SCatalin Marinas #endif 279c1cc1552SCatalin Marinas " modules : 0x%16lx - 0x%16lx (%6ld MB)\n" 280c1cc1552SCatalin Marinas " memory : 0x%16lx - 0x%16lx (%6ld MB)\n" 281c1cc1552SCatalin Marinas " .init : 0x%p" " - 0x%p" " (%6ld kB)\n" 282c1cc1552SCatalin Marinas " .text : 0x%p" " - 0x%p" " (%6ld kB)\n" 283c1cc1552SCatalin Marinas " .data : 0x%p" " - 0x%p" " (%6ld kB)\n", 284c1cc1552SCatalin Marinas MLM(VMALLOC_START, VMALLOC_END), 285c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP 286c1cc1552SCatalin Marinas MLM((unsigned long)virt_to_page(PAGE_OFFSET), 287c1cc1552SCatalin Marinas (unsigned long)virt_to_page(high_memory)), 288c1cc1552SCatalin Marinas #endif 289c1cc1552SCatalin Marinas MLM(MODULES_VADDR, MODULES_END), 290c1cc1552SCatalin Marinas MLM(PAGE_OFFSET, (unsigned long)high_memory), 291c1cc1552SCatalin Marinas 292c1cc1552SCatalin Marinas MLK_ROUNDUP(__init_begin, __init_end), 293c1cc1552SCatalin Marinas MLK_ROUNDUP(_text, _etext), 294c1cc1552SCatalin Marinas MLK_ROUNDUP(_sdata, _edata)); 295c1cc1552SCatalin Marinas 296c1cc1552SCatalin Marinas #undef MLK 297c1cc1552SCatalin Marinas #undef MLM 298c1cc1552SCatalin Marinas #undef MLK_ROUNDUP 299c1cc1552SCatalin Marinas 300c1cc1552SCatalin Marinas /* 301c1cc1552SCatalin Marinas * Check boundaries twice: Some fundamental inconsistencies can be 302c1cc1552SCatalin Marinas * detected at build time already. 303c1cc1552SCatalin Marinas */ 304c1cc1552SCatalin Marinas #ifdef CONFIG_COMPAT 305c1cc1552SCatalin Marinas BUILD_BUG_ON(TASK_SIZE_32 > TASK_SIZE_64); 306c1cc1552SCatalin Marinas #endif 307c1cc1552SCatalin Marinas BUILD_BUG_ON(TASK_SIZE_64 > MODULES_VADDR); 308c1cc1552SCatalin Marinas BUG_ON(TASK_SIZE_64 > MODULES_VADDR); 309c1cc1552SCatalin Marinas 310bee4ebd1SJiang Liu if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) { 311c1cc1552SCatalin Marinas extern int sysctl_overcommit_memory; 312c1cc1552SCatalin Marinas /* 313c1cc1552SCatalin Marinas * On a machine this small we won't get anywhere without 314c1cc1552SCatalin Marinas * overcommit, so turn it on by default. 315c1cc1552SCatalin Marinas */ 316c1cc1552SCatalin Marinas sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 317c1cc1552SCatalin Marinas } 318c1cc1552SCatalin Marinas } 319c1cc1552SCatalin Marinas 320c1cc1552SCatalin Marinas void free_initmem(void) 321c1cc1552SCatalin Marinas { 3229af5b807SJiang Liu free_initmem_default(0); 323c1cc1552SCatalin Marinas } 324c1cc1552SCatalin Marinas 325c1cc1552SCatalin Marinas #ifdef CONFIG_BLK_DEV_INITRD 326c1cc1552SCatalin Marinas 327c1cc1552SCatalin Marinas static int keep_initrd; 328c1cc1552SCatalin Marinas 329c1cc1552SCatalin Marinas void free_initrd_mem(unsigned long start, unsigned long end) 330c1cc1552SCatalin Marinas { 3319af5b807SJiang Liu if (!keep_initrd) 3329af5b807SJiang Liu free_reserved_area((void *)start, (void *)end, 0, "initrd"); 333c1cc1552SCatalin Marinas } 334c1cc1552SCatalin Marinas 335c1cc1552SCatalin Marinas static int __init keepinitrd_setup(char *__unused) 336c1cc1552SCatalin Marinas { 337c1cc1552SCatalin Marinas keep_initrd = 1; 338c1cc1552SCatalin Marinas return 1; 339c1cc1552SCatalin Marinas } 340c1cc1552SCatalin Marinas 341c1cc1552SCatalin Marinas __setup("keepinitrd", keepinitrd_setup); 342c1cc1552SCatalin Marinas #endif 343