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> 251da177e4SLinus Torvalds 26b4b20ad8SRussell King #include <asm/cp15.h> 271da177e4SLinus Torvalds #include <asm/mach-types.h> 28716a3dc2SRussell King #include <asm/memblock.h> 2993c02ab4SGrant Likely #include <asm/prom.h> 3037efe642SRussell King #include <asm/sections.h> 311da177e4SLinus Torvalds #include <asm/setup.h> 321e6b4811SKees Cook #include <asm/system_info.h> 331da177e4SLinus Torvalds #include <asm/tlb.h> 34db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <asm/mach/arch.h> 371da177e4SLinus Torvalds #include <asm/mach/map.h> 381da177e4SLinus Torvalds 391b2e2b73SRussell King #include "mm.h" 401b2e2b73SRussell King 41b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU 42b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask) 43b4b20ad8SRussell King { 44b4b20ad8SRussell King cr_alignment = cr_alignment & ~mask; 45b4b20ad8SRussell King return cr_alignment; 46b4b20ad8SRussell King } 47b4b20ad8SRussell King #endif 48b4b20ad8SRussell King 49de22cc6eSVitaly Andrianov static phys_addr_t phys_initrd_start __initdata = 0; 50012d1f4aSRussell King static unsigned long phys_initrd_size __initdata = 0; 51012d1f4aSRussell King 522b0d8c25SJeremy Kerr static int __init early_initrd(char *p) 53012d1f4aSRussell King { 54de22cc6eSVitaly Andrianov phys_addr_t start; 55de22cc6eSVitaly Andrianov unsigned long size; 562b0d8c25SJeremy Kerr char *endp; 57012d1f4aSRussell King 582b0d8c25SJeremy Kerr start = memparse(p, &endp); 592b0d8c25SJeremy Kerr if (*endp == ',') { 602b0d8c25SJeremy Kerr size = memparse(endp + 1, NULL); 61012d1f4aSRussell King 62012d1f4aSRussell King phys_initrd_start = start; 63012d1f4aSRussell King phys_initrd_size = size; 64012d1f4aSRussell King } 652b0d8c25SJeremy Kerr return 0; 66012d1f4aSRussell King } 672b0d8c25SJeremy Kerr early_param("initrd", early_initrd); 68012d1f4aSRussell King 69012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag) 70012d1f4aSRussell King { 714ed89f22SRussell King pr_warn("ATAG_INITRD is deprecated; " 72012d1f4aSRussell King "please update your bootloader.\n"); 73012d1f4aSRussell King phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 74012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 75012d1f4aSRussell King return 0; 76012d1f4aSRussell King } 77012d1f4aSRussell King 78012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd); 79012d1f4aSRussell King 80012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag) 81012d1f4aSRussell King { 82012d1f4aSRussell King phys_initrd_start = tag->u.initrd.start; 83012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 84012d1f4aSRussell King return 0; 85012d1f4aSRussell King } 86012d1f4aSRussell King 87012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2); 881da177e4SLinus Torvalds 89f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low, 90f25b4b4cSRussell King unsigned long *max_high) 91dde5828fSRussell King { 921c2f87c2SLaura Abbott *max_low = PFN_DOWN(memblock_get_current_limit()); 931c2f87c2SLaura Abbott *min = PFN_UP(memblock_start_of_DRAM()); 941c2f87c2SLaura Abbott *max_high = PFN_DOWN(memblock_end_of_DRAM()); 95dde5828fSRussell King } 96dde5828fSRussell King 97be20902bSRussell King #ifdef CONFIG_ZONE_DMA 9865032018SNicolas Pitre 99364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly; 10065032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size); 10165032018SNicolas Pitre 102022ae537SRussell King /* 103022ae537SRussell King * The DMA mask corresponding to the maximum bus address allocatable 104022ae537SRussell King * using GFP_DMA. The default here places no restriction on DMA 105022ae537SRussell King * allocations. This must be the smallest DMA mask in the system, 106022ae537SRussell King * so a successful GFP_DMA allocation will always satisfy this. 107022ae537SRussell King */ 1084986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit; 1094dcfa600SRussell King unsigned long arm_dma_pfn_limit; 110022ae537SRussell King 111be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole, 112be20902bSRussell King unsigned long dma_size) 113be20902bSRussell King { 114be20902bSRussell King if (size[0] <= dma_size) 115be20902bSRussell King return; 116be20902bSRussell King 117be20902bSRussell King size[ZONE_NORMAL] = size[0] - dma_size; 118be20902bSRussell King size[ZONE_DMA] = dma_size; 119be20902bSRussell King hole[ZONE_NORMAL] = hole[0]; 120be20902bSRussell King hole[ZONE_DMA] = 0; 121be20902bSRussell King } 122be20902bSRussell King #endif 123be20902bSRussell King 124ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc) 125c7909509SMarek Szyprowski { 126c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA 127c7909509SMarek Szyprowski if (mdesc->dma_zone_size) { 128c7909509SMarek Szyprowski arm_dma_zone_size = mdesc->dma_zone_size; 1296bcac805SRussell King arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1; 130c7909509SMarek Szyprowski } else 131c7909509SMarek Szyprowski arm_dma_limit = 0xffffffff; 1324dcfa600SRussell King arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT; 133c7909509SMarek Szyprowski #endif 134c7909509SMarek Szyprowski } 135c7909509SMarek Szyprowski 13684f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low, 137a2c54d2aSRussell King unsigned long max_high) 138b7a69ac3SRussell King { 139b7a69ac3SRussell King unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 140a2c54d2aSRussell King struct memblock_region *reg; 141b7a69ac3SRussell King 14290072059SRussell King /* 143be370302SRussell King * initialise the zones. 14490072059SRussell King */ 14590072059SRussell King memset(zone_size, 0, sizeof(zone_size)); 14690072059SRussell King 14790072059SRussell King /* 148be370302SRussell King * The memory size has already been determined. If we need 149be370302SRussell King * to do anything fancy with the allocation of this memory 150be370302SRussell King * to the zones, now is the time to do it. 15190072059SRussell King */ 152dde5828fSRussell King zone_size[0] = max_low - min; 153dde5828fSRussell King #ifdef CONFIG_HIGHMEM 154dde5828fSRussell King zone_size[ZONE_HIGHMEM] = max_high - max_low; 155dde5828fSRussell King #endif 15690072059SRussell King 15790072059SRussell King /* 158be370302SRussell King * Calculate the size of the holes. 159be370302SRussell King * holes = node_size - sum(bank_sizes) 16090072059SRussell King */ 161dde5828fSRussell King memcpy(zhole_size, zone_size, sizeof(zhole_size)); 162a2c54d2aSRussell King for_each_memblock(memory, reg) { 163a2c54d2aSRussell King unsigned long start = memblock_region_memory_base_pfn(reg); 164a2c54d2aSRussell King unsigned long end = memblock_region_memory_end_pfn(reg); 165a2c54d2aSRussell King 166a2c54d2aSRussell King if (start < max_low) { 167a2c54d2aSRussell King unsigned long low_end = min(end, max_low); 168a2c54d2aSRussell King zhole_size[0] -= low_end - start; 169a2c54d2aSRussell King } 170dde5828fSRussell King #ifdef CONFIG_HIGHMEM 171a2c54d2aSRussell King if (end > max_low) { 172a2c54d2aSRussell King unsigned long high_start = max(start, max_low); 173a2c54d2aSRussell King zhole_size[ZONE_HIGHMEM] -= end - high_start; 174a2c54d2aSRussell King } 175dde5828fSRussell King #endif 176dde5828fSRussell King } 17790072059SRussell King 17865032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA 17990072059SRussell King /* 18090072059SRussell King * Adjust the sizes according to any special requirements for 18190072059SRussell King * this machine type. 18290072059SRussell King */ 183c7909509SMarek Szyprowski if (arm_dma_zone_size) 184be20902bSRussell King arm_adjust_dma_zone(zone_size, zhole_size, 18565032018SNicolas Pitre arm_dma_zone_size >> PAGE_SHIFT); 186be20902bSRussell King #endif 18790072059SRussell King 188be370302SRussell King free_area_init_node(0, zone_size, min, zhole_size); 18990072059SRussell King } 19090072059SRussell King 1917b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID 192b7cfda9fSRussell King int pfn_valid(unsigned long pfn) 193b7cfda9fSRussell King { 194fb492c91SMark Rutland return memblock_is_memory(__pfn_to_phys(pfn)); 195b7cfda9fSRussell King } 196b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid); 1977b7bf499SWill Deacon #endif 198657e12fdSRussell King 1997b7bf499SWill Deacon #ifndef CONFIG_SPARSEMEM 20014904927SStephen Boyd static void __init arm_memory_present(void) 201657e12fdSRussell King { 202657e12fdSRussell King } 203657e12fdSRussell King #else 20414904927SStephen Boyd static void __init arm_memory_present(void) 205657e12fdSRussell King { 206719c1514SBenjamin Herrenschmidt struct memblock_region *reg; 207719c1514SBenjamin Herrenschmidt 2087c996361SYinghai Lu for_each_memblock(memory, reg) 209c7fc2de0SYinghai Lu memory_present(0, memblock_region_memory_base_pfn(reg), 210c7fc2de0SYinghai Lu memblock_region_memory_end_pfn(reg)); 211657e12fdSRussell King } 212b7cfda9fSRussell King #endif 213b7cfda9fSRussell King 214716a3dc2SRussell King static bool arm_memblock_steal_permitted = true; 215716a3dc2SRussell King 216bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align) 217716a3dc2SRussell King { 218716a3dc2SRussell King phys_addr_t phys; 219716a3dc2SRussell King 220716a3dc2SRussell King BUG_ON(!arm_memblock_steal_permitted); 221716a3dc2SRussell King 2227ac68a4cSRussell King phys = memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE); 223716a3dc2SRussell King memblock_free(phys, size); 224716a3dc2SRussell King memblock_remove(phys, size); 225716a3dc2SRussell King 226716a3dc2SRussell King return phys; 227716a3dc2SRussell King } 228716a3dc2SRussell King 2291c2f87c2SLaura Abbott void __init arm_memblock_init(const struct machine_desc *mdesc) 2302778f620SRussell King { 2312778f620SRussell King /* Register the kernel text, kernel data and initrd with memblock. */ 2322778f620SRussell King #ifdef CONFIG_XIP_KERNEL 233842eab40SRussell King memblock_reserve(__pa(_sdata), _end - _sdata); 2342778f620SRussell King #else 2352778f620SRussell King memblock_reserve(__pa(_stext), _end - _stext); 2362778f620SRussell King #endif 2372778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD 23865939301SRob Herring /* FDT scan will populate initrd_start */ 2394c235cb9SBen Peddell if (initrd_start && !phys_initrd_size) { 24065939301SRob Herring phys_initrd_start = __virt_to_phys(initrd_start); 24165939301SRob Herring phys_initrd_size = initrd_end - initrd_start; 24265939301SRob Herring } 2434c235cb9SBen Peddell initrd_start = initrd_end = 0; 244b0a2679dSRussell King if (phys_initrd_size && 2458f4b8c76SRussell King !memblock_is_region_memory(phys_initrd_start, phys_initrd_size)) { 246de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n", 247de22cc6eSVitaly Andrianov (u64)phys_initrd_start, phys_initrd_size); 2488f4b8c76SRussell King phys_initrd_start = phys_initrd_size = 0; 2498f4b8c76SRussell King } 2508f4b8c76SRussell King if (phys_initrd_size && 251b0a2679dSRussell King memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) { 252de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n", 253de22cc6eSVitaly Andrianov (u64)phys_initrd_start, phys_initrd_size); 254b0a2679dSRussell King phys_initrd_start = phys_initrd_size = 0; 255b0a2679dSRussell King } 2562778f620SRussell King if (phys_initrd_size) { 2572778f620SRussell King memblock_reserve(phys_initrd_start, phys_initrd_size); 2582778f620SRussell King 2592778f620SRussell King /* Now convert initrd to virtual addresses */ 2602778f620SRussell King initrd_start = __phys_to_virt(phys_initrd_start); 2612778f620SRussell King initrd_end = initrd_start + phys_initrd_size; 2622778f620SRussell King } 2632778f620SRussell King #endif 2642778f620SRussell King 2652778f620SRussell King arm_mm_memblock_reserve(); 2662778f620SRussell King 2678d717a52SRussell King /* reserve any platform specific memblock areas */ 2688d717a52SRussell King if (mdesc->reserve) 2698d717a52SRussell King mdesc->reserve(); 2708d717a52SRussell King 271*24bbd929SArd Biesheuvel early_init_fdt_reserve_self(); 272bcedb5f9SMarek Szyprowski early_init_fdt_scan_reserved_mem(); 273bcedb5f9SMarek Szyprowski 27499a468d7SGeorge G. Davis /* reserve memory for DMA contiguous allocations */ 27595b0e655SMarek Szyprowski dma_contiguous_reserve(arm_dma_limit); 276c7909509SMarek Szyprowski 277716a3dc2SRussell King arm_memblock_steal_permitted = false; 2782778f620SRussell King memblock_dump_all(); 2792778f620SRussell King } 2802778f620SRussell King 2818d717a52SRussell King void __init bootmem_init(void) 28290072059SRussell King { 283dde5828fSRussell King unsigned long min, max_low, max_high; 28490072059SRussell King 2858e58caefSGrygorii Strashko memblock_allow_resize(); 286dde5828fSRussell King max_low = max_high = 0; 287dde5828fSRussell King 288f25b4b4cSRussell King find_limits(&min, &max_low, &max_high); 289dde5828fSRussell King 290d30eae47SVladimir Murzin early_memtest((phys_addr_t)min << PAGE_SHIFT, 291d30eae47SVladimir Murzin (phys_addr_t)max_low << PAGE_SHIFT); 292d30eae47SVladimir Murzin 293b7a69ac3SRussell King /* 294657e12fdSRussell King * Sparsemem tries to allocate bootmem in memory_present(), 295657e12fdSRussell King * so must be done after the fixed reservations 296657e12fdSRussell King */ 297eda2e5dcSRussell King arm_memory_present(); 29890072059SRussell King 299b7a69ac3SRussell King /* 300b7a69ac3SRussell King * sparse_init() needs the bootmem allocator up and running. 301b7a69ac3SRussell King */ 302b7a69ac3SRussell King sparse_init(); 303b7a69ac3SRussell King 304b7a69ac3SRussell King /* 305be370302SRussell King * Now free the memory - free_area_init_node needs 306b7a69ac3SRussell King * the sparse mem_map arrays initialized by sparse_init() 307b7a69ac3SRussell King * for memmap_init_zone(), otherwise all PFNs are invalid. 308b7a69ac3SRussell King */ 30984f452b1SSantosh Shilimkar zone_sizes_init(min, max_low, max_high); 310b7a69ac3SRussell King 31190072059SRussell King /* 31290072059SRussell King * This doesn't seem to be used by the Linux memory manager any 31390072059SRussell King * more, but is used by ll_rw_block. If we can get rid of it, we 31490072059SRussell King * also get rid of some of the stuff above as well. 31590072059SRussell King */ 31626ba47b1SSantosh Shilimkar min_low_pfn = min; 31726ba47b1SSantosh Shilimkar max_low_pfn = max_low; 31826ba47b1SSantosh Shilimkar max_pfn = max_high; 31990072059SRussell King } 32090072059SRussell King 32154d52573SStephen Boyd /* 32254d52573SStephen Boyd * Poison init memory with an undefined instruction (ARM) or a branch to an 32354d52573SStephen Boyd * undefined instruction (Thumb). 32454d52573SStephen Boyd */ 32554d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count) 32654d52573SStephen Boyd { 32754d52573SStephen Boyd u32 *p = (u32 *)s; 328bf912d99SJamie Iles for (; count != 0; count -= 4) 32954d52573SStephen Boyd *p++ = 0xe7fddef0; 33054d52573SStephen Boyd } 33154d52573SStephen Boyd 332a013053dSRussell King static inline void 333be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn) 334a013053dSRussell King { 335a013053dSRussell King struct page *start_pg, *end_pg; 33656bc6286SVitaly Andrianov phys_addr_t pg, pgend; 337a013053dSRussell King 338a013053dSRussell King /* 339a013053dSRussell King * Convert start_pfn/end_pfn to a struct page pointer. 340a013053dSRussell King */ 3413257f43dSCatalin Marinas start_pg = pfn_to_page(start_pfn - 1) + 1; 3429af386c8SWill Deacon end_pg = pfn_to_page(end_pfn - 1) + 1; 343a013053dSRussell King 344a013053dSRussell King /* 345a013053dSRussell King * Convert to physical addresses, and 346a013053dSRussell King * round start upwards and end downwards. 347a013053dSRussell King */ 34856bc6286SVitaly Andrianov pg = PAGE_ALIGN(__pa(start_pg)); 34956bc6286SVitaly Andrianov pgend = __pa(end_pg) & PAGE_MASK; 350a013053dSRussell King 351a013053dSRussell King /* 352a013053dSRussell King * If there are free pages between these, 353a013053dSRussell King * free the section of the memmap array. 354a013053dSRussell King */ 355a013053dSRussell King if (pg < pgend) 356cfb66586SSantosh Shilimkar memblock_free_early(pg, pgend - pg); 357a013053dSRussell King } 358a013053dSRussell King 359a013053dSRussell King /* 360a013053dSRussell King * The mem_map array can get very big. Free the unused area of the memory map. 361a013053dSRussell King */ 3621c2f87c2SLaura Abbott static void __init free_unused_memmap(void) 363a013053dSRussell King { 3641c2f87c2SLaura Abbott unsigned long start, prev_end = 0; 3651c2f87c2SLaura Abbott struct memblock_region *reg; 366a013053dSRussell King 367a013053dSRussell King /* 3683260e529SMichael Bohan * This relies on each bank being in address order. 3693260e529SMichael Bohan * The banks are sorted previously in bootmem_init(). 370a013053dSRussell King */ 3711c2f87c2SLaura Abbott for_each_memblock(memory, reg) { 3721c2f87c2SLaura Abbott start = memblock_region_memory_base_pfn(reg); 373a013053dSRussell King 3749af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 3759af386c8SWill Deacon /* 3769af386c8SWill Deacon * Take care not to free memmap entries that don't exist 3779af386c8SWill Deacon * due to SPARSEMEM sections which aren't present. 3789af386c8SWill Deacon */ 3791c2f87c2SLaura Abbott start = min(start, 3801c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 381002ea9eeSLinus Walleij #else 382002ea9eeSLinus Walleij /* 383002ea9eeSLinus Walleij * Align down here since the VM subsystem insists that the 384002ea9eeSLinus Walleij * memmap entries are valid from the bank start aligned to 385002ea9eeSLinus Walleij * MAX_ORDER_NR_PAGES. 386002ea9eeSLinus Walleij */ 3871c2f87c2SLaura Abbott start = round_down(start, MAX_ORDER_NR_PAGES); 3889af386c8SWill Deacon #endif 389a013053dSRussell King /* 390a013053dSRussell King * If we had a previous bank, and there is a space 391a013053dSRussell King * between the current bank and the previous, free it. 392a013053dSRussell King */ 3931c2f87c2SLaura Abbott if (prev_end && prev_end < start) 3941c2f87c2SLaura Abbott free_memmap(prev_end, start); 395a013053dSRussell King 3963260e529SMichael Bohan /* 3973260e529SMichael Bohan * Align up here since the VM subsystem insists that the 3983260e529SMichael Bohan * memmap entries are valid from the bank end aligned to 3993260e529SMichael Bohan * MAX_ORDER_NR_PAGES. 4003260e529SMichael Bohan */ 4011c2f87c2SLaura Abbott prev_end = ALIGN(memblock_region_memory_end_pfn(reg), 4021c2f87c2SLaura Abbott MAX_ORDER_NR_PAGES); 403a013053dSRussell King } 4049af386c8SWill Deacon 4059af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 4061c2f87c2SLaura Abbott if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) 4071c2f87c2SLaura Abbott free_memmap(prev_end, 4081c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 4099af386c8SWill Deacon #endif 410a013053dSRussell King } 411a013053dSRussell King 41283db0384SJiang Liu #ifdef CONFIG_HIGHMEM 41383db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end) 41483db0384SJiang Liu { 415dd6911efSJiang Liu for (; pfn < end; pfn++) 416dd6911efSJiang Liu free_highmem_page(pfn_to_page(pfn)); 41783db0384SJiang Liu } 41883db0384SJiang Liu #endif 41983db0384SJiang Liu 420d0e775afSRussell King static void __init free_highpages(void) 421d0e775afSRussell King { 422d0e775afSRussell King #ifdef CONFIG_HIGHMEM 42326ba47b1SSantosh Shilimkar unsigned long max_low = max_low_pfn; 424df4f14c7SRussell King struct memblock_region *mem, *res; 425d0e775afSRussell King 426d0e775afSRussell King /* set highmem page free */ 427df4f14c7SRussell King for_each_memblock(memory, mem) { 428df4f14c7SRussell King unsigned long start = memblock_region_memory_base_pfn(mem); 429df4f14c7SRussell King unsigned long end = memblock_region_memory_end_pfn(mem); 430df4f14c7SRussell King 431df4f14c7SRussell King /* Ignore complete lowmem entries */ 432df4f14c7SRussell King if (end <= max_low) 433df4f14c7SRussell King continue; 434df4f14c7SRussell King 435df4f14c7SRussell King /* Truncate partial highmem entries */ 436df4f14c7SRussell King if (start < max_low) 437df4f14c7SRussell King start = max_low; 438df4f14c7SRussell King 439df4f14c7SRussell King /* Find and exclude any reserved regions */ 440df4f14c7SRussell King for_each_memblock(reserved, res) { 441df4f14c7SRussell King unsigned long res_start, res_end; 442df4f14c7SRussell King 443df4f14c7SRussell King res_start = memblock_region_reserved_base_pfn(res); 444df4f14c7SRussell King res_end = memblock_region_reserved_end_pfn(res); 445df4f14c7SRussell King 446df4f14c7SRussell King if (res_end < start) 447df4f14c7SRussell King continue; 448df4f14c7SRussell King if (res_start < start) 449df4f14c7SRussell King res_start = start; 450df4f14c7SRussell King if (res_start > end) 451df4f14c7SRussell King res_start = end; 452df4f14c7SRussell King if (res_end > end) 453df4f14c7SRussell King res_end = end; 454df4f14c7SRussell King if (res_start != start) 45583db0384SJiang Liu free_area_high(start, res_start); 456df4f14c7SRussell King start = res_end; 457df4f14c7SRussell King if (start == end) 458df4f14c7SRussell King break; 459df4f14c7SRussell King } 460df4f14c7SRussell King 461df4f14c7SRussell King /* And now free anything which remains */ 462df4f14c7SRussell King if (start < end) 46383db0384SJiang Liu free_area_high(start, end); 464d0e775afSRussell King } 465d0e775afSRussell King #endif 466d0e775afSRussell King } 467d0e775afSRussell King 4681da177e4SLinus Torvalds /* 4691da177e4SLinus Torvalds * mem_init() marks the free areas in the mem_map and tells us how much 4701da177e4SLinus Torvalds * memory is free. This is done after various parts of the system have 4711da177e4SLinus Torvalds * claimed their memory after the kernel image. 4721da177e4SLinus Torvalds */ 4731da177e4SLinus Torvalds void __init mem_init(void) 4741da177e4SLinus Torvalds { 4751dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM 4761dbd30e9SLinus Walleij /* These pointers are filled in on TCM detection */ 4771dbd30e9SLinus Walleij extern u32 dtcm_end; 4781dbd30e9SLinus Walleij extern u32 itcm_end; 4791dbd30e9SLinus Walleij #endif 4801da177e4SLinus Torvalds 481b3ba41f2SSantosh Shilimkar set_max_mapnr(pfn_to_page(max_pfn) - mem_map); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* this will put all unused low memory onto the freelists */ 4841c2f87c2SLaura Abbott free_unused_memmap(); 4850c988534SJiang Liu free_all_bootmem(); 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds #ifdef CONFIG_SA1111 4881da177e4SLinus Torvalds /* now that our DMA memory is actually so designated, we can free it */ 489bfd65dd9SLinus Torvalds free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL); 4901da177e4SLinus Torvalds #endif 4911da177e4SLinus Torvalds 492d0e775afSRussell King free_highpages(); 4933835f6cbSNicolas Pitre 4942450c973SJiang Liu mem_init_print_info(NULL); 4951da177e4SLinus Torvalds 496db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10 497db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20 498db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K) 499db9ef1afSFenkart/Bostandzhyan 5004ed89f22SRussell King pr_notice("Virtual kernel memory layout:\n" 501db9ef1afSFenkart/Bostandzhyan " vector : 0x%08lx - 0x%08lx (%4ld kB)\n" 50207d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 50307d2a5c7SLinus Walleij " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 50407d2a5c7SLinus Walleij " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 50507d2a5c7SLinus Walleij #endif 506db9ef1afSFenkart/Bostandzhyan " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 507db9ef1afSFenkart/Bostandzhyan " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" 508db9ef1afSFenkart/Bostandzhyan " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" 509db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 510db9ef1afSFenkart/Bostandzhyan " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n" 511db9ef1afSFenkart/Bostandzhyan #endif 512d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES 513db9ef1afSFenkart/Bostandzhyan " modules : 0x%08lx - 0x%08lx (%4ld MB)\n" 514d9277d51SUwe Kleine-König #endif 515178c3dfeSRussell King " .text : 0x%p" " - 0x%p" " (%4td kB)\n" 516178c3dfeSRussell King " .init : 0x%p" " - 0x%p" " (%4td kB)\n" 517178c3dfeSRussell King " .data : 0x%p" " - 0x%p" " (%4td kB)\n" 518178c3dfeSRussell King " .bss : 0x%p" " - 0x%p" " (%4td kB)\n", 519db9ef1afSFenkart/Bostandzhyan 520db9ef1afSFenkart/Bostandzhyan MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) + 521db9ef1afSFenkart/Bostandzhyan (PAGE_SIZE)), 52207d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 5231dbd30e9SLinus Walleij MLK(DTCM_OFFSET, (unsigned long) dtcm_end), 5241dbd30e9SLinus Walleij MLK(ITCM_OFFSET, (unsigned long) itcm_end), 52507d2a5c7SLinus Walleij #endif 526b615bbbfSMark Salter MLK(FIXADDR_START, FIXADDR_END), 527c931b4f6SFenkart/Bostandzhyan MLM(VMALLOC_START, VMALLOC_END), 528db9ef1afSFenkart/Bostandzhyan MLM(PAGE_OFFSET, (unsigned long)high_memory), 529db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 530db9ef1afSFenkart/Bostandzhyan MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) * 531db9ef1afSFenkart/Bostandzhyan (PAGE_SIZE)), 532db9ef1afSFenkart/Bostandzhyan #endif 533d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES 534db9ef1afSFenkart/Bostandzhyan MLM(MODULES_VADDR, MODULES_END), 535d9277d51SUwe Kleine-König #endif 536db9ef1afSFenkart/Bostandzhyan 537db9ef1afSFenkart/Bostandzhyan MLK_ROUNDUP(_text, _etext), 5383835d69aSRussell King MLK_ROUNDUP(__init_begin, __init_end), 53945f6d7e0SRabin Vincent MLK_ROUNDUP(_sdata, _edata), 54045f6d7e0SRabin Vincent MLK_ROUNDUP(__bss_start, __bss_stop)); 541db9ef1afSFenkart/Bostandzhyan 542db9ef1afSFenkart/Bostandzhyan #undef MLK 543db9ef1afSFenkart/Bostandzhyan #undef MLM 544db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP 545db9ef1afSFenkart/Bostandzhyan 546a1839272SFenkart/Bostandzhyan /* 547a1839272SFenkart/Bostandzhyan * Check boundaries twice: Some fundamental inconsistencies can 548a1839272SFenkart/Bostandzhyan * be detected at build time already. 549a1839272SFenkart/Bostandzhyan */ 550a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU 551a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 552a1839272SFenkart/Bostandzhyan BUG_ON(TASK_SIZE > MODULES_VADDR); 553a1839272SFenkart/Bostandzhyan #endif 554a1839272SFenkart/Bostandzhyan 555a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 556a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 557a1839272SFenkart/Bostandzhyan BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 558a1839272SFenkart/Bostandzhyan #endif 559a1839272SFenkart/Bostandzhyan 5602450c973SJiang Liu if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) { 5611da177e4SLinus Torvalds extern int sysctl_overcommit_memory; 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * On a machine this small we won't get 5641da177e4SLinus Torvalds * anywhere without overcommit, so turn 5651da177e4SLinus Torvalds * it on by default. 5661da177e4SLinus Torvalds */ 5671da177e4SLinus Torvalds sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711e6b4811SKees Cook #ifdef CONFIG_ARM_KERNMEM_PERMS 5721e6b4811SKees Cook struct section_perm { 5731e6b4811SKees Cook unsigned long start; 5741e6b4811SKees Cook unsigned long end; 5751e6b4811SKees Cook pmdval_t mask; 5761e6b4811SKees Cook pmdval_t prot; 57780d6b0c2SKees Cook pmdval_t clear; 5781e6b4811SKees Cook }; 5791e6b4811SKees Cook 58080d6b0c2SKees Cook static struct section_perm nx_perms[] = { 5811e6b4811SKees Cook /* Make pages tables, etc before _stext RW (set NX). */ 5821e6b4811SKees Cook { 5831e6b4811SKees Cook .start = PAGE_OFFSET, 5841e6b4811SKees Cook .end = (unsigned long)_stext, 5851e6b4811SKees Cook .mask = ~PMD_SECT_XN, 5861e6b4811SKees Cook .prot = PMD_SECT_XN, 5871e6b4811SKees Cook }, 5881e6b4811SKees Cook /* Make init RW (set NX). */ 5891e6b4811SKees Cook { 5901e6b4811SKees Cook .start = (unsigned long)__init_begin, 5911e6b4811SKees Cook .end = (unsigned long)_sdata, 5921e6b4811SKees Cook .mask = ~PMD_SECT_XN, 5931e6b4811SKees Cook .prot = PMD_SECT_XN, 5941e6b4811SKees Cook }, 59580d6b0c2SKees Cook #ifdef CONFIG_DEBUG_RODATA 59680d6b0c2SKees Cook /* Make rodata NX (set RO in ro_perms below). */ 59780d6b0c2SKees Cook { 59880d6b0c2SKees Cook .start = (unsigned long)__start_rodata, 59980d6b0c2SKees Cook .end = (unsigned long)__init_begin, 60080d6b0c2SKees Cook .mask = ~PMD_SECT_XN, 60180d6b0c2SKees Cook .prot = PMD_SECT_XN, 60280d6b0c2SKees Cook }, 60380d6b0c2SKees Cook #endif 6041e6b4811SKees Cook }; 6051e6b4811SKees Cook 60680d6b0c2SKees Cook #ifdef CONFIG_DEBUG_RODATA 60780d6b0c2SKees Cook static struct section_perm ro_perms[] = { 60880d6b0c2SKees Cook /* Make kernel code and rodata RX (set RO). */ 60980d6b0c2SKees Cook { 61080d6b0c2SKees Cook .start = (unsigned long)_stext, 61180d6b0c2SKees Cook .end = (unsigned long)__init_begin, 61280d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE 6131e347922SVictor Kamensky .mask = ~L_PMD_SECT_RDONLY, 6141e347922SVictor Kamensky .prot = L_PMD_SECT_RDONLY, 61580d6b0c2SKees Cook #else 61680d6b0c2SKees Cook .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE), 61780d6b0c2SKees Cook .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE, 61880d6b0c2SKees Cook .clear = PMD_SECT_AP_WRITE, 61980d6b0c2SKees Cook #endif 62080d6b0c2SKees Cook }, 62180d6b0c2SKees Cook }; 62280d6b0c2SKees Cook #endif 62380d6b0c2SKees Cook 6241e6b4811SKees Cook /* 6251e6b4811SKees Cook * Updates section permissions only for the current mm (sections are 6261e6b4811SKees Cook * copied into each mm). During startup, this is the init_mm. Is only 6271e6b4811SKees Cook * safe to be called with preemption disabled, as under stop_machine(). 6281e6b4811SKees Cook */ 6291e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask, 6301e6b4811SKees Cook pmdval_t prot) 6311e6b4811SKees Cook { 6321e6b4811SKees Cook struct mm_struct *mm; 6331e6b4811SKees Cook pmd_t *pmd; 6341e6b4811SKees Cook 6351e6b4811SKees Cook mm = current->active_mm; 6361e6b4811SKees Cook pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr); 6371e6b4811SKees Cook 6381e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE 6391e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 6401e6b4811SKees Cook #else 6411e6b4811SKees Cook if (addr & SECTION_SIZE) 6421e6b4811SKees Cook pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot); 6431e6b4811SKees Cook else 6441e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 6451e6b4811SKees Cook #endif 6461e6b4811SKees Cook flush_pmd_entry(pmd); 6471e6b4811SKees Cook local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE); 6481e6b4811SKees Cook } 6491e6b4811SKees Cook 6501e6b4811SKees Cook /* Make sure extended page tables are in use. */ 6511e6b4811SKees Cook static inline bool arch_has_strict_perms(void) 6521e6b4811SKees Cook { 6531e6b4811SKees Cook if (cpu_architecture() < CPU_ARCH_ARMv6) 6541e6b4811SKees Cook return false; 6551e6b4811SKees Cook 6561e6b4811SKees Cook return !!(get_cr() & CR_XP); 6571e6b4811SKees Cook } 6581e6b4811SKees Cook 6591e6b4811SKees Cook #define set_section_perms(perms, field) { \ 6601e6b4811SKees Cook size_t i; \ 6611e6b4811SKees Cook unsigned long addr; \ 6621e6b4811SKees Cook \ 6631e6b4811SKees Cook if (!arch_has_strict_perms()) \ 6641e6b4811SKees Cook return; \ 6651e6b4811SKees Cook \ 6661e6b4811SKees Cook for (i = 0; i < ARRAY_SIZE(perms); i++) { \ 6671e6b4811SKees Cook if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) || \ 6681e6b4811SKees Cook !IS_ALIGNED(perms[i].end, SECTION_SIZE)) { \ 6691e6b4811SKees Cook pr_err("BUG: section %lx-%lx not aligned to %lx\n", \ 6701e6b4811SKees Cook perms[i].start, perms[i].end, \ 6711e6b4811SKees Cook SECTION_SIZE); \ 6721e6b4811SKees Cook continue; \ 6731e6b4811SKees Cook } \ 6741e6b4811SKees Cook \ 6751e6b4811SKees Cook for (addr = perms[i].start; \ 6761e6b4811SKees Cook addr < perms[i].end; \ 6771e6b4811SKees Cook addr += SECTION_SIZE) \ 6781e6b4811SKees Cook section_update(addr, perms[i].mask, \ 6791e6b4811SKees Cook perms[i].field); \ 6801e6b4811SKees Cook } \ 6811e6b4811SKees Cook } 6821e6b4811SKees Cook 6831e6b4811SKees Cook static inline void fix_kernmem_perms(void) 6841e6b4811SKees Cook { 6851e6b4811SKees Cook set_section_perms(nx_perms, prot); 6861e6b4811SKees Cook } 68780d6b0c2SKees Cook 68880d6b0c2SKees Cook #ifdef CONFIG_DEBUG_RODATA 68980d6b0c2SKees Cook void mark_rodata_ro(void) 69080d6b0c2SKees Cook { 69180d6b0c2SKees Cook set_section_perms(ro_perms, prot); 69280d6b0c2SKees Cook } 69380d6b0c2SKees Cook 69480d6b0c2SKees Cook void set_kernel_text_rw(void) 69580d6b0c2SKees Cook { 69680d6b0c2SKees Cook set_section_perms(ro_perms, clear); 69780d6b0c2SKees Cook } 69880d6b0c2SKees Cook 69980d6b0c2SKees Cook void set_kernel_text_ro(void) 70080d6b0c2SKees Cook { 70180d6b0c2SKees Cook set_section_perms(ro_perms, prot); 70280d6b0c2SKees Cook } 70380d6b0c2SKees Cook #endif /* CONFIG_DEBUG_RODATA */ 70480d6b0c2SKees Cook 7051e6b4811SKees Cook #else 7061e6b4811SKees Cook static inline void fix_kernmem_perms(void) { } 7071e6b4811SKees Cook #endif /* CONFIG_ARM_KERNMEM_PERMS */ 7081e6b4811SKees Cook 7091e6b4811SKees Cook void free_tcmmem(void) 7101da177e4SLinus Torvalds { 711bc581770SLinus Walleij #ifdef CONFIG_HAVE_TCM 712ea208f64SLinus Walleij extern char __tcm_start, __tcm_end; 713bc581770SLinus Walleij 71454d52573SStephen Boyd poison_init_mem(&__tcm_start, &__tcm_end - &__tcm_start); 715dbe67df4SJiang Liu free_reserved_area(&__tcm_start, &__tcm_end, -1, "TCM link"); 716bc581770SLinus Walleij #endif 7171e6b4811SKees Cook } 7181e6b4811SKees Cook 7191e6b4811SKees Cook void free_initmem(void) 7201e6b4811SKees Cook { 7211e6b4811SKees Cook fix_kernmem_perms(); 7221e6b4811SKees Cook free_tcmmem(); 723bc581770SLinus Walleij 72454d52573SStephen Boyd poison_init_mem(__init_begin, __init_end - __init_begin); 7256db015e4SNicolas Pitre if (!machine_is_integrator() && !machine_is_cintegrator()) 726dbe67df4SJiang Liu free_initmem_default(-1); 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds static int keep_initrd; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end) 7341da177e4SLinus Torvalds { 73554d52573SStephen Boyd if (!keep_initrd) { 736421520baSYalin Wang if (start == initrd_start) 737421520baSYalin Wang start = round_down(start, PAGE_SIZE); 738421520baSYalin Wang if (end == initrd_end) 739421520baSYalin Wang end = round_up(end, PAGE_SIZE); 740421520baSYalin Wang 74154d52573SStephen Boyd poison_init_mem((void *)start, PAGE_ALIGN(end) - start); 742dbe67df4SJiang Liu free_reserved_area((void *)start, (void *)end, -1, "initrd"); 7431da177e4SLinus Torvalds } 74454d52573SStephen Boyd } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds keep_initrd = 1; 7491da177e4SLinus Torvalds return 1; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup); 7531da177e4SLinus Torvalds #endif 754