11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/arch/arm/mm/init.c 31da177e4SLinus Torvalds * 490072059SRussell King * Copyright (C) 1995-2005 Russell King 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 71da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 81da177e4SLinus Torvalds * published by the Free Software Foundation. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds #include <linux/kernel.h> 111da177e4SLinus Torvalds #include <linux/errno.h> 121da177e4SLinus Torvalds #include <linux/swap.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/bootmem.h> 151da177e4SLinus Torvalds #include <linux/mman.h> 16dc28094bSPaul Gortmaker #include <linux/export.h> 171da177e4SLinus Torvalds #include <linux/nodemask.h> 181da177e4SLinus Torvalds #include <linux/initrd.h> 199eb8f674SGrant Likely #include <linux/of_fdt.h> 203835f6cbSNicolas Pitre #include <linux/highmem.h> 215a0e3ad6STejun Heo #include <linux/gfp.h> 222778f620SRussell King #include <linux/memblock.h> 23c7909509SMarek Szyprowski #include <linux/dma-contiguous.h> 24158e8bfeSAlessandro Rubini #include <linux/sizes.h> 2508925c2fSLaura Abbott #include <linux/stop_machine.h> 261da177e4SLinus Torvalds 27b4b20ad8SRussell King #include <asm/cp15.h> 281da177e4SLinus Torvalds #include <asm/mach-types.h> 29716a3dc2SRussell King #include <asm/memblock.h> 30d2ca5f24SAfzal Mohammed #include <asm/memory.h> 3193c02ab4SGrant Likely #include <asm/prom.h> 3237efe642SRussell King #include <asm/sections.h> 331da177e4SLinus Torvalds #include <asm/setup.h> 341e6b4811SKees Cook #include <asm/system_info.h> 351da177e4SLinus Torvalds #include <asm/tlb.h> 36db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <asm/mach/arch.h> 391da177e4SLinus Torvalds #include <asm/mach/map.h> 401da177e4SLinus Torvalds 411b2e2b73SRussell King #include "mm.h" 421b2e2b73SRussell King 43b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU 44b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask) 45b4b20ad8SRussell King { 46b4b20ad8SRussell King cr_alignment = cr_alignment & ~mask; 47b4b20ad8SRussell King return cr_alignment; 48b4b20ad8SRussell King } 49b4b20ad8SRussell King #endif 50b4b20ad8SRussell King 51de22cc6eSVitaly Andrianov static phys_addr_t phys_initrd_start __initdata = 0; 52012d1f4aSRussell King static unsigned long phys_initrd_size __initdata = 0; 53012d1f4aSRussell King 542b0d8c25SJeremy Kerr static int __init early_initrd(char *p) 55012d1f4aSRussell King { 56de22cc6eSVitaly Andrianov phys_addr_t start; 57de22cc6eSVitaly Andrianov unsigned long size; 582b0d8c25SJeremy Kerr char *endp; 59012d1f4aSRussell King 602b0d8c25SJeremy Kerr start = memparse(p, &endp); 612b0d8c25SJeremy Kerr if (*endp == ',') { 622b0d8c25SJeremy Kerr size = memparse(endp + 1, NULL); 63012d1f4aSRussell King 64012d1f4aSRussell King phys_initrd_start = start; 65012d1f4aSRussell King phys_initrd_size = size; 66012d1f4aSRussell King } 672b0d8c25SJeremy Kerr return 0; 68012d1f4aSRussell King } 692b0d8c25SJeremy Kerr early_param("initrd", early_initrd); 70012d1f4aSRussell King 71012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag) 72012d1f4aSRussell King { 734ed89f22SRussell King pr_warn("ATAG_INITRD is deprecated; " 74012d1f4aSRussell King "please update your bootloader.\n"); 75012d1f4aSRussell King phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 76012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 77012d1f4aSRussell King return 0; 78012d1f4aSRussell King } 79012d1f4aSRussell King 80012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd); 81012d1f4aSRussell King 82012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag) 83012d1f4aSRussell King { 84012d1f4aSRussell King phys_initrd_start = tag->u.initrd.start; 85012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 86012d1f4aSRussell King return 0; 87012d1f4aSRussell King } 88012d1f4aSRussell King 89012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2); 901da177e4SLinus Torvalds 91f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low, 92f25b4b4cSRussell King unsigned long *max_high) 93dde5828fSRussell King { 941c2f87c2SLaura Abbott *max_low = PFN_DOWN(memblock_get_current_limit()); 951c2f87c2SLaura Abbott *min = PFN_UP(memblock_start_of_DRAM()); 961c2f87c2SLaura Abbott *max_high = PFN_DOWN(memblock_end_of_DRAM()); 97dde5828fSRussell King } 98dde5828fSRussell King 99be20902bSRussell King #ifdef CONFIG_ZONE_DMA 10065032018SNicolas Pitre 101364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly; 10265032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size); 10365032018SNicolas Pitre 104022ae537SRussell King /* 105022ae537SRussell King * The DMA mask corresponding to the maximum bus address allocatable 106022ae537SRussell King * using GFP_DMA. The default here places no restriction on DMA 107022ae537SRussell King * allocations. This must be the smallest DMA mask in the system, 108022ae537SRussell King * so a successful GFP_DMA allocation will always satisfy this. 109022ae537SRussell King */ 1104986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit; 1114dcfa600SRussell King unsigned long arm_dma_pfn_limit; 112022ae537SRussell King 113be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole, 114be20902bSRussell King unsigned long dma_size) 115be20902bSRussell King { 116be20902bSRussell King if (size[0] <= dma_size) 117be20902bSRussell King return; 118be20902bSRussell King 119be20902bSRussell King size[ZONE_NORMAL] = size[0] - dma_size; 120be20902bSRussell King size[ZONE_DMA] = dma_size; 121be20902bSRussell King hole[ZONE_NORMAL] = hole[0]; 122be20902bSRussell King hole[ZONE_DMA] = 0; 123be20902bSRussell King } 124be20902bSRussell King #endif 125be20902bSRussell King 126ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc) 127c7909509SMarek Szyprowski { 128c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA 129c7909509SMarek Szyprowski if (mdesc->dma_zone_size) { 130c7909509SMarek Szyprowski arm_dma_zone_size = mdesc->dma_zone_size; 1316bcac805SRussell King arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1; 132c7909509SMarek Szyprowski } else 133c7909509SMarek Szyprowski arm_dma_limit = 0xffffffff; 1344dcfa600SRussell King arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT; 135c7909509SMarek Szyprowski #endif 136c7909509SMarek Szyprowski } 137c7909509SMarek Szyprowski 13884f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low, 139a2c54d2aSRussell King unsigned long max_high) 140b7a69ac3SRussell King { 141b7a69ac3SRussell King unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 142a2c54d2aSRussell King struct memblock_region *reg; 143b7a69ac3SRussell King 14490072059SRussell King /* 145be370302SRussell King * initialise the zones. 14690072059SRussell King */ 14790072059SRussell King memset(zone_size, 0, sizeof(zone_size)); 14890072059SRussell King 14990072059SRussell King /* 150be370302SRussell King * The memory size has already been determined. If we need 151be370302SRussell King * to do anything fancy with the allocation of this memory 152be370302SRussell King * to the zones, now is the time to do it. 15390072059SRussell King */ 154dde5828fSRussell King zone_size[0] = max_low - min; 155dde5828fSRussell King #ifdef CONFIG_HIGHMEM 156dde5828fSRussell King zone_size[ZONE_HIGHMEM] = max_high - max_low; 157dde5828fSRussell King #endif 15890072059SRussell King 15990072059SRussell King /* 160be370302SRussell King * Calculate the size of the holes. 161be370302SRussell King * holes = node_size - sum(bank_sizes) 16290072059SRussell King */ 163dde5828fSRussell King memcpy(zhole_size, zone_size, sizeof(zhole_size)); 164a2c54d2aSRussell King for_each_memblock(memory, reg) { 165a2c54d2aSRussell King unsigned long start = memblock_region_memory_base_pfn(reg); 166a2c54d2aSRussell King unsigned long end = memblock_region_memory_end_pfn(reg); 167a2c54d2aSRussell King 168a2c54d2aSRussell King if (start < max_low) { 169a2c54d2aSRussell King unsigned long low_end = min(end, max_low); 170a2c54d2aSRussell King zhole_size[0] -= low_end - start; 171a2c54d2aSRussell King } 172dde5828fSRussell King #ifdef CONFIG_HIGHMEM 173a2c54d2aSRussell King if (end > max_low) { 174a2c54d2aSRussell King unsigned long high_start = max(start, max_low); 175a2c54d2aSRussell King zhole_size[ZONE_HIGHMEM] -= end - high_start; 176a2c54d2aSRussell King } 177dde5828fSRussell King #endif 178dde5828fSRussell King } 17990072059SRussell King 18065032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA 18190072059SRussell King /* 18290072059SRussell King * Adjust the sizes according to any special requirements for 18390072059SRussell King * this machine type. 18490072059SRussell King */ 185c7909509SMarek Szyprowski if (arm_dma_zone_size) 186be20902bSRussell King arm_adjust_dma_zone(zone_size, zhole_size, 18765032018SNicolas Pitre arm_dma_zone_size >> PAGE_SHIFT); 188be20902bSRussell King #endif 18990072059SRussell King 190be370302SRussell King free_area_init_node(0, zone_size, min, zhole_size); 19190072059SRussell King } 19290072059SRussell King 1937b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID 194b7cfda9fSRussell King int pfn_valid(unsigned long pfn) 195b7cfda9fSRussell King { 19609414d00SArd Biesheuvel return memblock_is_map_memory(__pfn_to_phys(pfn)); 197b7cfda9fSRussell King } 198b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid); 1997b7bf499SWill Deacon #endif 200657e12fdSRussell King 2017b7bf499SWill Deacon #ifndef CONFIG_SPARSEMEM 20214904927SStephen Boyd static void __init arm_memory_present(void) 203657e12fdSRussell King { 204657e12fdSRussell King } 205657e12fdSRussell King #else 20614904927SStephen Boyd static void __init arm_memory_present(void) 207657e12fdSRussell King { 208719c1514SBenjamin Herrenschmidt struct memblock_region *reg; 209719c1514SBenjamin Herrenschmidt 2107c996361SYinghai Lu for_each_memblock(memory, reg) 211c7fc2de0SYinghai Lu memory_present(0, memblock_region_memory_base_pfn(reg), 212c7fc2de0SYinghai Lu memblock_region_memory_end_pfn(reg)); 213657e12fdSRussell King } 214b7cfda9fSRussell King #endif 215b7cfda9fSRussell King 216716a3dc2SRussell King static bool arm_memblock_steal_permitted = true; 217716a3dc2SRussell King 218bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align) 219716a3dc2SRussell King { 220716a3dc2SRussell King phys_addr_t phys; 221716a3dc2SRussell King 222716a3dc2SRussell King BUG_ON(!arm_memblock_steal_permitted); 223716a3dc2SRussell King 2247ac68a4cSRussell King phys = memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE); 225716a3dc2SRussell King memblock_free(phys, size); 226716a3dc2SRussell King memblock_remove(phys, size); 227716a3dc2SRussell King 228716a3dc2SRussell King return phys; 229716a3dc2SRussell King } 230716a3dc2SRussell King 231*39286248SRussell King static void __init arm_initrd_init(void) 2322778f620SRussell King { 2332778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD 23465939301SRob Herring /* FDT scan will populate initrd_start */ 2354c235cb9SBen Peddell if (initrd_start && !phys_initrd_size) { 23665939301SRob Herring phys_initrd_start = __virt_to_phys(initrd_start); 23765939301SRob Herring phys_initrd_size = initrd_end - initrd_start; 23865939301SRob Herring } 2394c235cb9SBen Peddell initrd_start = initrd_end = 0; 240b0a2679dSRussell King if (phys_initrd_size && 2418f4b8c76SRussell King !memblock_is_region_memory(phys_initrd_start, phys_initrd_size)) { 242de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n", 243de22cc6eSVitaly Andrianov (u64)phys_initrd_start, phys_initrd_size); 2448f4b8c76SRussell King phys_initrd_start = phys_initrd_size = 0; 2458f4b8c76SRussell King } 2468f4b8c76SRussell King if (phys_initrd_size && 247b0a2679dSRussell King memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) { 248de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n", 249de22cc6eSVitaly Andrianov (u64)phys_initrd_start, phys_initrd_size); 250b0a2679dSRussell King phys_initrd_start = phys_initrd_size = 0; 251b0a2679dSRussell King } 2522778f620SRussell King if (phys_initrd_size) { 2532778f620SRussell King memblock_reserve(phys_initrd_start, phys_initrd_size); 2542778f620SRussell King 2552778f620SRussell King /* Now convert initrd to virtual addresses */ 2562778f620SRussell King initrd_start = __phys_to_virt(phys_initrd_start); 2572778f620SRussell King initrd_end = initrd_start + phys_initrd_size; 2582778f620SRussell King } 2592778f620SRussell King #endif 260*39286248SRussell King } 261*39286248SRussell King 262*39286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc) 263*39286248SRussell King { 264*39286248SRussell King /* Register the kernel text, kernel data and initrd with memblock. */ 265*39286248SRussell King memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START); 266*39286248SRussell King 267*39286248SRussell King arm_initrd_init(); 2682778f620SRussell King 2692778f620SRussell King arm_mm_memblock_reserve(); 2702778f620SRussell King 2718d717a52SRussell King /* reserve any platform specific memblock areas */ 2728d717a52SRussell King if (mdesc->reserve) 2738d717a52SRussell King mdesc->reserve(); 2748d717a52SRussell King 27524bbd929SArd Biesheuvel early_init_fdt_reserve_self(); 276bcedb5f9SMarek Szyprowski early_init_fdt_scan_reserved_mem(); 277bcedb5f9SMarek Szyprowski 27899a468d7SGeorge G. Davis /* reserve memory for DMA contiguous allocations */ 27995b0e655SMarek Szyprowski dma_contiguous_reserve(arm_dma_limit); 280c7909509SMarek Szyprowski 281716a3dc2SRussell King arm_memblock_steal_permitted = false; 2822778f620SRussell King memblock_dump_all(); 2832778f620SRussell King } 2842778f620SRussell King 2858d717a52SRussell King void __init bootmem_init(void) 28690072059SRussell King { 287dde5828fSRussell King unsigned long min, max_low, max_high; 28890072059SRussell King 2898e58caefSGrygorii Strashko memblock_allow_resize(); 290dde5828fSRussell King max_low = max_high = 0; 291dde5828fSRussell King 292f25b4b4cSRussell King find_limits(&min, &max_low, &max_high); 293dde5828fSRussell King 294d30eae47SVladimir Murzin early_memtest((phys_addr_t)min << PAGE_SHIFT, 295d30eae47SVladimir Murzin (phys_addr_t)max_low << PAGE_SHIFT); 296d30eae47SVladimir Murzin 297b7a69ac3SRussell King /* 298657e12fdSRussell King * Sparsemem tries to allocate bootmem in memory_present(), 299657e12fdSRussell King * so must be done after the fixed reservations 300657e12fdSRussell King */ 301eda2e5dcSRussell King arm_memory_present(); 30290072059SRussell King 303b7a69ac3SRussell King /* 304b7a69ac3SRussell King * sparse_init() needs the bootmem allocator up and running. 305b7a69ac3SRussell King */ 306b7a69ac3SRussell King sparse_init(); 307b7a69ac3SRussell King 308b7a69ac3SRussell King /* 309be370302SRussell King * Now free the memory - free_area_init_node needs 310b7a69ac3SRussell King * the sparse mem_map arrays initialized by sparse_init() 311b7a69ac3SRussell King * for memmap_init_zone(), otherwise all PFNs are invalid. 312b7a69ac3SRussell King */ 31384f452b1SSantosh Shilimkar zone_sizes_init(min, max_low, max_high); 314b7a69ac3SRussell King 31590072059SRussell King /* 31690072059SRussell King * This doesn't seem to be used by the Linux memory manager any 31790072059SRussell King * more, but is used by ll_rw_block. If we can get rid of it, we 31890072059SRussell King * also get rid of some of the stuff above as well. 31990072059SRussell King */ 32026ba47b1SSantosh Shilimkar min_low_pfn = min; 32126ba47b1SSantosh Shilimkar max_low_pfn = max_low; 32226ba47b1SSantosh Shilimkar max_pfn = max_high; 32390072059SRussell King } 32490072059SRussell King 32554d52573SStephen Boyd /* 32654d52573SStephen Boyd * Poison init memory with an undefined instruction (ARM) or a branch to an 32754d52573SStephen Boyd * undefined instruction (Thumb). 32854d52573SStephen Boyd */ 32954d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count) 33054d52573SStephen Boyd { 33154d52573SStephen Boyd u32 *p = (u32 *)s; 332bf912d99SJamie Iles for (; count != 0; count -= 4) 33354d52573SStephen Boyd *p++ = 0xe7fddef0; 33454d52573SStephen Boyd } 33554d52573SStephen Boyd 336a013053dSRussell King static inline void 337be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn) 338a013053dSRussell King { 339a013053dSRussell King struct page *start_pg, *end_pg; 34056bc6286SVitaly Andrianov phys_addr_t pg, pgend; 341a013053dSRussell King 342a013053dSRussell King /* 343a013053dSRussell King * Convert start_pfn/end_pfn to a struct page pointer. 344a013053dSRussell King */ 3453257f43dSCatalin Marinas start_pg = pfn_to_page(start_pfn - 1) + 1; 3469af386c8SWill Deacon end_pg = pfn_to_page(end_pfn - 1) + 1; 347a013053dSRussell King 348a013053dSRussell King /* 349a013053dSRussell King * Convert to physical addresses, and 350a013053dSRussell King * round start upwards and end downwards. 351a013053dSRussell King */ 35256bc6286SVitaly Andrianov pg = PAGE_ALIGN(__pa(start_pg)); 35356bc6286SVitaly Andrianov pgend = __pa(end_pg) & PAGE_MASK; 354a013053dSRussell King 355a013053dSRussell King /* 356a013053dSRussell King * If there are free pages between these, 357a013053dSRussell King * free the section of the memmap array. 358a013053dSRussell King */ 359a013053dSRussell King if (pg < pgend) 360cfb66586SSantosh Shilimkar memblock_free_early(pg, pgend - pg); 361a013053dSRussell King } 362a013053dSRussell King 363a013053dSRussell King /* 364a013053dSRussell King * The mem_map array can get very big. Free the unused area of the memory map. 365a013053dSRussell King */ 3661c2f87c2SLaura Abbott static void __init free_unused_memmap(void) 367a013053dSRussell King { 3681c2f87c2SLaura Abbott unsigned long start, prev_end = 0; 3691c2f87c2SLaura Abbott struct memblock_region *reg; 370a013053dSRussell King 371a013053dSRussell King /* 3723260e529SMichael Bohan * This relies on each bank being in address order. 3733260e529SMichael Bohan * The banks are sorted previously in bootmem_init(). 374a013053dSRussell King */ 3751c2f87c2SLaura Abbott for_each_memblock(memory, reg) { 3761c2f87c2SLaura Abbott start = memblock_region_memory_base_pfn(reg); 377a013053dSRussell King 3789af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 3799af386c8SWill Deacon /* 3809af386c8SWill Deacon * Take care not to free memmap entries that don't exist 3819af386c8SWill Deacon * due to SPARSEMEM sections which aren't present. 3829af386c8SWill Deacon */ 3831c2f87c2SLaura Abbott start = min(start, 3841c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 385002ea9eeSLinus Walleij #else 386002ea9eeSLinus Walleij /* 387002ea9eeSLinus Walleij * Align down here since the VM subsystem insists that the 388002ea9eeSLinus Walleij * memmap entries are valid from the bank start aligned to 389002ea9eeSLinus Walleij * MAX_ORDER_NR_PAGES. 390002ea9eeSLinus Walleij */ 3911c2f87c2SLaura Abbott start = round_down(start, MAX_ORDER_NR_PAGES); 3929af386c8SWill Deacon #endif 393a013053dSRussell King /* 394a013053dSRussell King * If we had a previous bank, and there is a space 395a013053dSRussell King * between the current bank and the previous, free it. 396a013053dSRussell King */ 3971c2f87c2SLaura Abbott if (prev_end && prev_end < start) 3981c2f87c2SLaura Abbott free_memmap(prev_end, start); 399a013053dSRussell King 4003260e529SMichael Bohan /* 4013260e529SMichael Bohan * Align up here since the VM subsystem insists that the 4023260e529SMichael Bohan * memmap entries are valid from the bank end aligned to 4033260e529SMichael Bohan * MAX_ORDER_NR_PAGES. 4043260e529SMichael Bohan */ 4051c2f87c2SLaura Abbott prev_end = ALIGN(memblock_region_memory_end_pfn(reg), 4061c2f87c2SLaura Abbott MAX_ORDER_NR_PAGES); 407a013053dSRussell King } 4089af386c8SWill Deacon 4099af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 4101c2f87c2SLaura Abbott if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) 4111c2f87c2SLaura Abbott free_memmap(prev_end, 4121c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 4139af386c8SWill Deacon #endif 414a013053dSRussell King } 415a013053dSRussell King 41683db0384SJiang Liu #ifdef CONFIG_HIGHMEM 41783db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end) 41883db0384SJiang Liu { 419dd6911efSJiang Liu for (; pfn < end; pfn++) 420dd6911efSJiang Liu free_highmem_page(pfn_to_page(pfn)); 42183db0384SJiang Liu } 42283db0384SJiang Liu #endif 42383db0384SJiang Liu 424d0e775afSRussell King static void __init free_highpages(void) 425d0e775afSRussell King { 426d0e775afSRussell King #ifdef CONFIG_HIGHMEM 42726ba47b1SSantosh Shilimkar unsigned long max_low = max_low_pfn; 428df4f14c7SRussell King struct memblock_region *mem, *res; 429d0e775afSRussell King 430d0e775afSRussell King /* set highmem page free */ 431df4f14c7SRussell King for_each_memblock(memory, mem) { 432df4f14c7SRussell King unsigned long start = memblock_region_memory_base_pfn(mem); 433df4f14c7SRussell King unsigned long end = memblock_region_memory_end_pfn(mem); 434df4f14c7SRussell King 435df4f14c7SRussell King /* Ignore complete lowmem entries */ 436df4f14c7SRussell King if (end <= max_low) 437df4f14c7SRussell King continue; 438df4f14c7SRussell King 43909414d00SArd Biesheuvel if (memblock_is_nomap(mem)) 44009414d00SArd Biesheuvel continue; 44109414d00SArd Biesheuvel 442df4f14c7SRussell King /* Truncate partial highmem entries */ 443df4f14c7SRussell King if (start < max_low) 444df4f14c7SRussell King start = max_low; 445df4f14c7SRussell King 446df4f14c7SRussell King /* Find and exclude any reserved regions */ 447df4f14c7SRussell King for_each_memblock(reserved, res) { 448df4f14c7SRussell King unsigned long res_start, res_end; 449df4f14c7SRussell King 450df4f14c7SRussell King res_start = memblock_region_reserved_base_pfn(res); 451df4f14c7SRussell King res_end = memblock_region_reserved_end_pfn(res); 452df4f14c7SRussell King 453df4f14c7SRussell King if (res_end < start) 454df4f14c7SRussell King continue; 455df4f14c7SRussell King if (res_start < start) 456df4f14c7SRussell King res_start = start; 457df4f14c7SRussell King if (res_start > end) 458df4f14c7SRussell King res_start = end; 459df4f14c7SRussell King if (res_end > end) 460df4f14c7SRussell King res_end = end; 461df4f14c7SRussell King if (res_start != start) 46283db0384SJiang Liu free_area_high(start, res_start); 463df4f14c7SRussell King start = res_end; 464df4f14c7SRussell King if (start == end) 465df4f14c7SRussell King break; 466df4f14c7SRussell King } 467df4f14c7SRussell King 468df4f14c7SRussell King /* And now free anything which remains */ 469df4f14c7SRussell King if (start < end) 47083db0384SJiang Liu free_area_high(start, end); 471d0e775afSRussell King } 472d0e775afSRussell King #endif 473d0e775afSRussell King } 474d0e775afSRussell King 4751da177e4SLinus Torvalds /* 4761da177e4SLinus Torvalds * mem_init() marks the free areas in the mem_map and tells us how much 4771da177e4SLinus Torvalds * memory is free. This is done after various parts of the system have 4781da177e4SLinus Torvalds * claimed their memory after the kernel image. 4791da177e4SLinus Torvalds */ 4801da177e4SLinus Torvalds void __init mem_init(void) 4811da177e4SLinus Torvalds { 4821dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM 4831dbd30e9SLinus Walleij /* These pointers are filled in on TCM detection */ 4841dbd30e9SLinus Walleij extern u32 dtcm_end; 4851dbd30e9SLinus Walleij extern u32 itcm_end; 4861dbd30e9SLinus Walleij #endif 4871da177e4SLinus Torvalds 488b3ba41f2SSantosh Shilimkar set_max_mapnr(pfn_to_page(max_pfn) - mem_map); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* this will put all unused low memory onto the freelists */ 4911c2f87c2SLaura Abbott free_unused_memmap(); 4920c988534SJiang Liu free_all_bootmem(); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds #ifdef CONFIG_SA1111 4951da177e4SLinus Torvalds /* now that our DMA memory is actually so designated, we can free it */ 496bfd65dd9SLinus Torvalds free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL); 4971da177e4SLinus Torvalds #endif 4981da177e4SLinus Torvalds 499d0e775afSRussell King free_highpages(); 5003835f6cbSNicolas Pitre 5012450c973SJiang Liu mem_init_print_info(NULL); 5021da177e4SLinus Torvalds 503db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10 504db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20 505db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K) 506db9ef1afSFenkart/Bostandzhyan 5074ed89f22SRussell King pr_notice("Virtual kernel memory layout:\n" 508db9ef1afSFenkart/Bostandzhyan " vector : 0x%08lx - 0x%08lx (%4ld kB)\n" 50907d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 51007d2a5c7SLinus Walleij " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 51107d2a5c7SLinus Walleij " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 51207d2a5c7SLinus Walleij #endif 513db9ef1afSFenkart/Bostandzhyan " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 514db9ef1afSFenkart/Bostandzhyan " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" 515db9ef1afSFenkart/Bostandzhyan " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" 516db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 517db9ef1afSFenkart/Bostandzhyan " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n" 518db9ef1afSFenkart/Bostandzhyan #endif 519d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES 520db9ef1afSFenkart/Bostandzhyan " modules : 0x%08lx - 0x%08lx (%4ld MB)\n" 521d9277d51SUwe Kleine-König #endif 522178c3dfeSRussell King " .text : 0x%p" " - 0x%p" " (%4td kB)\n" 523178c3dfeSRussell King " .init : 0x%p" " - 0x%p" " (%4td kB)\n" 524178c3dfeSRussell King " .data : 0x%p" " - 0x%p" " (%4td kB)\n" 525178c3dfeSRussell King " .bss : 0x%p" " - 0x%p" " (%4td kB)\n", 526db9ef1afSFenkart/Bostandzhyan 527d2ca5f24SAfzal Mohammed MLK(VECTORS_BASE, VECTORS_BASE + PAGE_SIZE), 52807d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 5291dbd30e9SLinus Walleij MLK(DTCM_OFFSET, (unsigned long) dtcm_end), 5301dbd30e9SLinus Walleij MLK(ITCM_OFFSET, (unsigned long) itcm_end), 53107d2a5c7SLinus Walleij #endif 532b615bbbfSMark Salter MLK(FIXADDR_START, FIXADDR_END), 533c931b4f6SFenkart/Bostandzhyan MLM(VMALLOC_START, VMALLOC_END), 534db9ef1afSFenkart/Bostandzhyan MLM(PAGE_OFFSET, (unsigned long)high_memory), 535db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 536db9ef1afSFenkart/Bostandzhyan MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) * 537db9ef1afSFenkart/Bostandzhyan (PAGE_SIZE)), 538db9ef1afSFenkart/Bostandzhyan #endif 539d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES 540db9ef1afSFenkart/Bostandzhyan MLM(MODULES_VADDR, MODULES_END), 541d9277d51SUwe Kleine-König #endif 542db9ef1afSFenkart/Bostandzhyan 543db9ef1afSFenkart/Bostandzhyan MLK_ROUNDUP(_text, _etext), 5443835d69aSRussell King MLK_ROUNDUP(__init_begin, __init_end), 54545f6d7e0SRabin Vincent MLK_ROUNDUP(_sdata, _edata), 54645f6d7e0SRabin Vincent MLK_ROUNDUP(__bss_start, __bss_stop)); 547db9ef1afSFenkart/Bostandzhyan 548db9ef1afSFenkart/Bostandzhyan #undef MLK 549db9ef1afSFenkart/Bostandzhyan #undef MLM 550db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP 551db9ef1afSFenkart/Bostandzhyan 552a1839272SFenkart/Bostandzhyan /* 553a1839272SFenkart/Bostandzhyan * Check boundaries twice: Some fundamental inconsistencies can 554a1839272SFenkart/Bostandzhyan * be detected at build time already. 555a1839272SFenkart/Bostandzhyan */ 556a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU 557a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 558a1839272SFenkart/Bostandzhyan BUG_ON(TASK_SIZE > MODULES_VADDR); 559a1839272SFenkart/Bostandzhyan #endif 560a1839272SFenkart/Bostandzhyan 561a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 562a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 563a1839272SFenkart/Bostandzhyan BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 564a1839272SFenkart/Bostandzhyan #endif 565a1839272SFenkart/Bostandzhyan 5662450c973SJiang Liu if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) { 5671da177e4SLinus Torvalds extern int sysctl_overcommit_memory; 5681da177e4SLinus Torvalds /* 5691da177e4SLinus Torvalds * On a machine this small we won't get 5701da177e4SLinus Torvalds * anywhere without overcommit, so turn 5711da177e4SLinus Torvalds * it on by default. 5721da177e4SLinus Torvalds */ 5731da177e4SLinus Torvalds sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 57725362dc4SKees Cook #ifdef CONFIG_DEBUG_RODATA 5781e6b4811SKees Cook struct section_perm { 57925362dc4SKees Cook const char *name; 5801e6b4811SKees Cook unsigned long start; 5811e6b4811SKees Cook unsigned long end; 5821e6b4811SKees Cook pmdval_t mask; 5831e6b4811SKees Cook pmdval_t prot; 58480d6b0c2SKees Cook pmdval_t clear; 5851e6b4811SKees Cook }; 5861e6b4811SKees Cook 58764ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */ 58864ac2e74SKees Cook extern char __start_rodata_section_aligned[]; 58964ac2e74SKees Cook 59080d6b0c2SKees Cook static struct section_perm nx_perms[] = { 5911e6b4811SKees Cook /* Make pages tables, etc before _stext RW (set NX). */ 5921e6b4811SKees Cook { 59325362dc4SKees Cook .name = "pre-text NX", 5941e6b4811SKees Cook .start = PAGE_OFFSET, 5951e6b4811SKees Cook .end = (unsigned long)_stext, 5961e6b4811SKees Cook .mask = ~PMD_SECT_XN, 5971e6b4811SKees Cook .prot = PMD_SECT_XN, 5981e6b4811SKees Cook }, 5991e6b4811SKees Cook /* Make init RW (set NX). */ 6001e6b4811SKees Cook { 60125362dc4SKees Cook .name = "init NX", 6021e6b4811SKees Cook .start = (unsigned long)__init_begin, 6031e6b4811SKees Cook .end = (unsigned long)_sdata, 6041e6b4811SKees Cook .mask = ~PMD_SECT_XN, 6051e6b4811SKees Cook .prot = PMD_SECT_XN, 6061e6b4811SKees Cook }, 60780d6b0c2SKees Cook /* Make rodata NX (set RO in ro_perms below). */ 60880d6b0c2SKees Cook { 60925362dc4SKees Cook .name = "rodata NX", 61064ac2e74SKees Cook .start = (unsigned long)__start_rodata_section_aligned, 61180d6b0c2SKees Cook .end = (unsigned long)__init_begin, 61280d6b0c2SKees Cook .mask = ~PMD_SECT_XN, 61380d6b0c2SKees Cook .prot = PMD_SECT_XN, 61480d6b0c2SKees Cook }, 6151e6b4811SKees Cook }; 6161e6b4811SKees Cook 61780d6b0c2SKees Cook static struct section_perm ro_perms[] = { 61880d6b0c2SKees Cook /* Make kernel code and rodata RX (set RO). */ 61980d6b0c2SKees Cook { 62025362dc4SKees Cook .name = "text/rodata RO", 62180d6b0c2SKees Cook .start = (unsigned long)_stext, 62280d6b0c2SKees Cook .end = (unsigned long)__init_begin, 62380d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE 6241e347922SVictor Kamensky .mask = ~L_PMD_SECT_RDONLY, 6251e347922SVictor Kamensky .prot = L_PMD_SECT_RDONLY, 62680d6b0c2SKees Cook #else 62780d6b0c2SKees Cook .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE), 62880d6b0c2SKees Cook .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE, 62980d6b0c2SKees Cook .clear = PMD_SECT_AP_WRITE, 63080d6b0c2SKees Cook #endif 63180d6b0c2SKees Cook }, 63280d6b0c2SKees Cook }; 63380d6b0c2SKees Cook 6341e6b4811SKees Cook /* 6351e6b4811SKees Cook * Updates section permissions only for the current mm (sections are 6361e6b4811SKees Cook * copied into each mm). During startup, this is the init_mm. Is only 6371e6b4811SKees Cook * safe to be called with preemption disabled, as under stop_machine(). 6381e6b4811SKees Cook */ 6391e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask, 64008925c2fSLaura Abbott pmdval_t prot, struct mm_struct *mm) 6411e6b4811SKees Cook { 6421e6b4811SKees Cook pmd_t *pmd; 6431e6b4811SKees Cook 6441e6b4811SKees Cook pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr); 6451e6b4811SKees Cook 6461e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE 6471e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 6481e6b4811SKees Cook #else 6491e6b4811SKees Cook if (addr & SECTION_SIZE) 6501e6b4811SKees Cook pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot); 6511e6b4811SKees Cook else 6521e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 6531e6b4811SKees Cook #endif 6541e6b4811SKees Cook flush_pmd_entry(pmd); 6551e6b4811SKees Cook local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE); 6561e6b4811SKees Cook } 6571e6b4811SKees Cook 6581e6b4811SKees Cook /* Make sure extended page tables are in use. */ 6591e6b4811SKees Cook static inline bool arch_has_strict_perms(void) 6601e6b4811SKees Cook { 6611e6b4811SKees Cook if (cpu_architecture() < CPU_ARCH_ARMv6) 6621e6b4811SKees Cook return false; 6631e6b4811SKees Cook 6641e6b4811SKees Cook return !!(get_cr() & CR_XP); 6651e6b4811SKees Cook } 6661e6b4811SKees Cook 66708925c2fSLaura Abbott void set_section_perms(struct section_perm *perms, int n, bool set, 66808925c2fSLaura Abbott struct mm_struct *mm) 66908925c2fSLaura Abbott { 67008925c2fSLaura Abbott size_t i; 67108925c2fSLaura Abbott unsigned long addr; 67208925c2fSLaura Abbott 67308925c2fSLaura Abbott if (!arch_has_strict_perms()) 67408925c2fSLaura Abbott return; 67508925c2fSLaura Abbott 67608925c2fSLaura Abbott for (i = 0; i < n; i++) { 67708925c2fSLaura Abbott if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) || 67808925c2fSLaura Abbott !IS_ALIGNED(perms[i].end, SECTION_SIZE)) { 67925362dc4SKees Cook pr_err("BUG: %s section %lx-%lx not aligned to %lx\n", 68025362dc4SKees Cook perms[i].name, perms[i].start, perms[i].end, 68108925c2fSLaura Abbott SECTION_SIZE); 68208925c2fSLaura Abbott continue; 6831e6b4811SKees Cook } 6841e6b4811SKees Cook 68508925c2fSLaura Abbott for (addr = perms[i].start; 68608925c2fSLaura Abbott addr < perms[i].end; 68708925c2fSLaura Abbott addr += SECTION_SIZE) 68808925c2fSLaura Abbott section_update(addr, perms[i].mask, 68908925c2fSLaura Abbott set ? perms[i].prot : perms[i].clear, mm); 69008925c2fSLaura Abbott } 69108925c2fSLaura Abbott 69208925c2fSLaura Abbott } 69308925c2fSLaura Abbott 69408925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n) 6951e6b4811SKees Cook { 69608925c2fSLaura Abbott struct task_struct *t, *s; 69708925c2fSLaura Abbott 69808925c2fSLaura Abbott read_lock(&tasklist_lock); 69908925c2fSLaura Abbott for_each_process(t) { 70008925c2fSLaura Abbott if (t->flags & PF_KTHREAD) 70108925c2fSLaura Abbott continue; 70208925c2fSLaura Abbott for_each_thread(t, s) 70308925c2fSLaura Abbott set_section_perms(perms, n, true, s->mm); 70408925c2fSLaura Abbott } 70508925c2fSLaura Abbott read_unlock(&tasklist_lock); 70608925c2fSLaura Abbott set_section_perms(perms, n, true, current->active_mm); 70708925c2fSLaura Abbott set_section_perms(perms, n, true, &init_mm); 70808925c2fSLaura Abbott } 70908925c2fSLaura Abbott 71008925c2fSLaura Abbott int __fix_kernmem_perms(void *unused) 71108925c2fSLaura Abbott { 71208925c2fSLaura Abbott update_sections_early(nx_perms, ARRAY_SIZE(nx_perms)); 71308925c2fSLaura Abbott return 0; 71408925c2fSLaura Abbott } 71508925c2fSLaura Abbott 71608925c2fSLaura Abbott void fix_kernmem_perms(void) 71708925c2fSLaura Abbott { 71808925c2fSLaura Abbott stop_machine(__fix_kernmem_perms, NULL, NULL); 7191e6b4811SKees Cook } 72080d6b0c2SKees Cook 72108925c2fSLaura Abbott int __mark_rodata_ro(void *unused) 72208925c2fSLaura Abbott { 72308925c2fSLaura Abbott update_sections_early(ro_perms, ARRAY_SIZE(ro_perms)); 72408925c2fSLaura Abbott return 0; 72508925c2fSLaura Abbott } 72608925c2fSLaura Abbott 72780d6b0c2SKees Cook void mark_rodata_ro(void) 72880d6b0c2SKees Cook { 72908925c2fSLaura Abbott stop_machine(__mark_rodata_ro, NULL, NULL); 73080d6b0c2SKees Cook } 73180d6b0c2SKees Cook 73280d6b0c2SKees Cook void set_kernel_text_rw(void) 73380d6b0c2SKees Cook { 73408925c2fSLaura Abbott set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false, 73508925c2fSLaura Abbott current->active_mm); 73680d6b0c2SKees Cook } 73780d6b0c2SKees Cook 73880d6b0c2SKees Cook void set_kernel_text_ro(void) 73980d6b0c2SKees Cook { 74008925c2fSLaura Abbott set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true, 74108925c2fSLaura Abbott current->active_mm); 74280d6b0c2SKees Cook } 74380d6b0c2SKees Cook 7441e6b4811SKees Cook #else 7451e6b4811SKees Cook static inline void fix_kernmem_perms(void) { } 74625362dc4SKees Cook #endif /* CONFIG_DEBUG_RODATA */ 7471e6b4811SKees Cook 7481e6b4811SKees Cook void free_tcmmem(void) 7491da177e4SLinus Torvalds { 750bc581770SLinus Walleij #ifdef CONFIG_HAVE_TCM 751ea208f64SLinus Walleij extern char __tcm_start, __tcm_end; 752bc581770SLinus Walleij 75354d52573SStephen Boyd poison_init_mem(&__tcm_start, &__tcm_end - &__tcm_start); 754dbe67df4SJiang Liu free_reserved_area(&__tcm_start, &__tcm_end, -1, "TCM link"); 755bc581770SLinus Walleij #endif 7561e6b4811SKees Cook } 7571e6b4811SKees Cook 7581e6b4811SKees Cook void free_initmem(void) 7591e6b4811SKees Cook { 7601e6b4811SKees Cook fix_kernmem_perms(); 7611e6b4811SKees Cook free_tcmmem(); 762bc581770SLinus Walleij 76354d52573SStephen Boyd poison_init_mem(__init_begin, __init_end - __init_begin); 7646db015e4SNicolas Pitre if (!machine_is_integrator() && !machine_is_cintegrator()) 765dbe67df4SJiang Liu free_initmem_default(-1); 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds static int keep_initrd; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end) 7731da177e4SLinus Torvalds { 77454d52573SStephen Boyd if (!keep_initrd) { 775421520baSYalin Wang if (start == initrd_start) 776421520baSYalin Wang start = round_down(start, PAGE_SIZE); 777421520baSYalin Wang if (end == initrd_end) 778421520baSYalin Wang end = round_up(end, PAGE_SIZE); 779421520baSYalin Wang 78054d52573SStephen Boyd poison_init_mem((void *)start, PAGE_ALIGN(end) - start); 781dbe67df4SJiang Liu free_reserved_area((void *)start, (void *)end, -1, "initrd"); 7821da177e4SLinus Torvalds } 78354d52573SStephen Boyd } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused) 7861da177e4SLinus Torvalds { 7871da177e4SLinus Torvalds keep_initrd = 1; 7881da177e4SLinus Torvalds return 1; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup); 7921da177e4SLinus Torvalds #endif 793