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> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include <asm/mach-types.h> 25*716a3dc2SRussell King #include <asm/memblock.h> 2693c02ab4SGrant Likely #include <asm/prom.h> 2737efe642SRussell King #include <asm/sections.h> 281da177e4SLinus Torvalds #include <asm/setup.h> 2974d02fb9SRussell King #include <asm/sizes.h> 301da177e4SLinus Torvalds #include <asm/tlb.h> 31db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <asm/mach/arch.h> 341da177e4SLinus Torvalds #include <asm/mach/map.h> 351c16d242STejun Heo #include <asm/memblock.h> 361da177e4SLinus Torvalds 371b2e2b73SRussell King #include "mm.h" 381b2e2b73SRussell King 39012d1f4aSRussell King static unsigned long phys_initrd_start __initdata = 0; 40012d1f4aSRussell King static unsigned long phys_initrd_size __initdata = 0; 41012d1f4aSRussell King 422b0d8c25SJeremy Kerr static int __init early_initrd(char *p) 43012d1f4aSRussell King { 44012d1f4aSRussell King unsigned long start, size; 452b0d8c25SJeremy Kerr char *endp; 46012d1f4aSRussell King 472b0d8c25SJeremy Kerr start = memparse(p, &endp); 482b0d8c25SJeremy Kerr if (*endp == ',') { 492b0d8c25SJeremy Kerr size = memparse(endp + 1, NULL); 50012d1f4aSRussell King 51012d1f4aSRussell King phys_initrd_start = start; 52012d1f4aSRussell King phys_initrd_size = size; 53012d1f4aSRussell King } 542b0d8c25SJeremy Kerr return 0; 55012d1f4aSRussell King } 562b0d8c25SJeremy Kerr early_param("initrd", early_initrd); 57012d1f4aSRussell King 58012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag) 59012d1f4aSRussell King { 60012d1f4aSRussell King printk(KERN_WARNING "ATAG_INITRD is deprecated; " 61012d1f4aSRussell King "please update your bootloader.\n"); 62012d1f4aSRussell King phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 63012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 64012d1f4aSRussell King return 0; 65012d1f4aSRussell King } 66012d1f4aSRussell King 67012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd); 68012d1f4aSRussell King 69012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag) 70012d1f4aSRussell King { 71012d1f4aSRussell King phys_initrd_start = tag->u.initrd.start; 72012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 73012d1f4aSRussell King return 0; 74012d1f4aSRussell King } 75012d1f4aSRussell King 76012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2); 771da177e4SLinus Torvalds 789eb8f674SGrant Likely #ifdef CONFIG_OF_FLATTREE 799eb8f674SGrant Likely void __init early_init_dt_setup_initrd_arch(unsigned long start, unsigned long end) 809eb8f674SGrant Likely { 819eb8f674SGrant Likely phys_initrd_start = start; 829eb8f674SGrant Likely phys_initrd_size = end - start; 839eb8f674SGrant Likely } 849eb8f674SGrant Likely #endif /* CONFIG_OF_FLATTREE */ 859eb8f674SGrant Likely 861da177e4SLinus Torvalds /* 874b5f32ceSNicolas Pitre * This keeps memory configuration data used by a couple memory 884b5f32ceSNicolas Pitre * initialization functions, as well as show_mem() for the skipping 894b5f32ceSNicolas Pitre * of holes in the memory map. It is populated by arm_add_memory(). 901da177e4SLinus Torvalds */ 914b5f32ceSNicolas Pitre struct meminfo meminfo; 925e709827SRay Lehtiniemi 93b2b755b5SDavid Rientjes void show_mem(unsigned int filter) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds int free = 0, total = 0, reserved = 0; 96be370302SRussell King int shared = 0, cached = 0, slab = 0, i; 975e709827SRay Lehtiniemi struct meminfo * mi = &meminfo; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds printk("Mem-info:\n"); 1007bf02ea2SDavid Rientjes show_free_areas(filter); 101be370302SRussell King 102be370302SRussell King for_each_bank (i, mi) { 103d2a38ef9SRussell King struct membank *bank = &mi->bank[i]; 1045e709827SRay Lehtiniemi unsigned int pfn1, pfn2; 1051da177e4SLinus Torvalds struct page *page, *end; 1061da177e4SLinus Torvalds 107d2a38ef9SRussell King pfn1 = bank_pfn_start(bank); 108d2a38ef9SRussell King pfn2 = bank_pfn_end(bank); 1095e709827SRay Lehtiniemi 110ea056df7SCatalin Marinas page = pfn_to_page(pfn1); 111ea056df7SCatalin Marinas end = pfn_to_page(pfn2 - 1) + 1; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds do { 1141da177e4SLinus Torvalds total++; 1151da177e4SLinus Torvalds if (PageReserved(page)) 1161da177e4SLinus Torvalds reserved++; 1171da177e4SLinus Torvalds else if (PageSwapCache(page)) 1181da177e4SLinus Torvalds cached++; 1191da177e4SLinus Torvalds else if (PageSlab(page)) 1201da177e4SLinus Torvalds slab++; 1211da177e4SLinus Torvalds else if (!page_count(page)) 1221da177e4SLinus Torvalds free++; 1231da177e4SLinus Torvalds else 1241da177e4SLinus Torvalds shared += page_count(page) - 1; 1251da177e4SLinus Torvalds page++; 1261da177e4SLinus Torvalds } while (page < end); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds printk("%d pages of RAM\n", total); 1301da177e4SLinus Torvalds printk("%d free pages\n", free); 1311da177e4SLinus Torvalds printk("%d reserved pages\n", reserved); 1321da177e4SLinus Torvalds printk("%d slab pages\n", slab); 1331da177e4SLinus Torvalds printk("%d pages shared\n", shared); 1341da177e4SLinus Torvalds printk("%d pages swap cached\n", cached); 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 137f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low, 138f25b4b4cSRussell King unsigned long *max_high) 139dde5828fSRussell King { 140f25b4b4cSRussell King struct meminfo *mi = &meminfo; 141dde5828fSRussell King int i; 142dde5828fSRussell King 14327a3f0e9SNicolas Pitre /* This assumes the meminfo array is properly sorted */ 14427a3f0e9SNicolas Pitre *min = bank_pfn_start(&mi->bank[0]); 14527a3f0e9SNicolas Pitre for_each_bank (i, mi) 14627a3f0e9SNicolas Pitre if (mi->bank[i].highmem) 14727a3f0e9SNicolas Pitre break; 14827a3f0e9SNicolas Pitre *max_low = bank_pfn_end(&mi->bank[i - 1]); 14927a3f0e9SNicolas Pitre *max_high = bank_pfn_end(&mi->bank[mi->nr_banks - 1]); 150dde5828fSRussell King } 151dde5828fSRussell King 152a801d276SRussell King static void __init arm_bootmem_init(unsigned long start_pfn, 153a801d276SRussell King unsigned long end_pfn) 1541da177e4SLinus Torvalds { 155719c1514SBenjamin Herrenschmidt struct memblock_region *reg; 15690072059SRussell King unsigned int boot_pages; 1572778f620SRussell King phys_addr_t bitmap; 15890072059SRussell King pg_data_t *pgdat; 1591da177e4SLinus Torvalds 16090072059SRussell King /* 1612778f620SRussell King * Allocate the bootmem bitmap page. This must be in a region 1622778f620SRussell King * of memory which has already been mapped. 1631da177e4SLinus Torvalds */ 16490072059SRussell King boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn); 1652778f620SRussell King bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES, 1662778f620SRussell King __pfn_to_phys(end_pfn)); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* 169be370302SRussell King * Initialise the bootmem allocator, handing the 17090072059SRussell King * memory banks over to bootmem. 1711da177e4SLinus Torvalds */ 172be370302SRussell King node_set_online(0); 173be370302SRussell King pgdat = NODE_DATA(0); 1742778f620SRussell King init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn); 17590072059SRussell King 176a801d276SRussell King /* Free the lowmem regions from memblock into bootmem. */ 177a801d276SRussell King for_each_memblock(memory, reg) { 178a801d276SRussell King unsigned long start = memblock_region_memory_base_pfn(reg); 179a801d276SRussell King unsigned long end = memblock_region_memory_end_pfn(reg); 180a801d276SRussell King 181a801d276SRussell King if (end >= end_pfn) 182a801d276SRussell King end = end_pfn; 183a801d276SRussell King if (start >= end) 184a801d276SRussell King break; 185a801d276SRussell King 186a801d276SRussell King free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT); 187d2a38ef9SRussell King } 1881da177e4SLinus Torvalds 189a801d276SRussell King /* Reserve the lowmem memblock reserved regions in bootmem. */ 190719c1514SBenjamin Herrenschmidt for_each_memblock(reserved, reg) { 191a801d276SRussell King unsigned long start = memblock_region_reserved_base_pfn(reg); 192a801d276SRussell King unsigned long end = memblock_region_reserved_end_pfn(reg); 193a801d276SRussell King 194a801d276SRussell King if (end >= end_pfn) 195a801d276SRussell King end = end_pfn; 196a801d276SRussell King if (start >= end) 197a801d276SRussell King break; 198a801d276SRussell King 199a801d276SRussell King reserve_bootmem(__pfn_to_phys(start), 200a801d276SRussell King (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT); 201b7a69ac3SRussell King } 202b7a69ac3SRussell King } 203b7a69ac3SRussell King 204be20902bSRussell King #ifdef CONFIG_ZONE_DMA 20565032018SNicolas Pitre 20665032018SNicolas Pitre unsigned long arm_dma_zone_size __read_mostly; 20765032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size); 20865032018SNicolas Pitre 209022ae537SRussell King /* 210022ae537SRussell King * The DMA mask corresponding to the maximum bus address allocatable 211022ae537SRussell King * using GFP_DMA. The default here places no restriction on DMA 212022ae537SRussell King * allocations. This must be the smallest DMA mask in the system, 213022ae537SRussell King * so a successful GFP_DMA allocation will always satisfy this. 214022ae537SRussell King */ 215022ae537SRussell King u32 arm_dma_limit; 216022ae537SRussell King 217be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole, 218be20902bSRussell King unsigned long dma_size) 219be20902bSRussell King { 220be20902bSRussell King if (size[0] <= dma_size) 221be20902bSRussell King return; 222be20902bSRussell King 223be20902bSRussell King size[ZONE_NORMAL] = size[0] - dma_size; 224be20902bSRussell King size[ZONE_DMA] = dma_size; 225be20902bSRussell King hole[ZONE_NORMAL] = hole[0]; 226be20902bSRussell King hole[ZONE_DMA] = 0; 227be20902bSRussell King } 228be20902bSRussell King #endif 229be20902bSRussell King 230a2c54d2aSRussell King static void __init arm_bootmem_free(unsigned long min, unsigned long max_low, 231a2c54d2aSRussell King unsigned long max_high) 232b7a69ac3SRussell King { 233b7a69ac3SRussell King unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 234a2c54d2aSRussell King struct memblock_region *reg; 235b7a69ac3SRussell King 23690072059SRussell King /* 237be370302SRussell King * initialise the zones. 23890072059SRussell King */ 23990072059SRussell King memset(zone_size, 0, sizeof(zone_size)); 24090072059SRussell King 24190072059SRussell King /* 242be370302SRussell King * The memory size has already been determined. If we need 243be370302SRussell King * to do anything fancy with the allocation of this memory 244be370302SRussell King * to the zones, now is the time to do it. 24590072059SRussell King */ 246dde5828fSRussell King zone_size[0] = max_low - min; 247dde5828fSRussell King #ifdef CONFIG_HIGHMEM 248dde5828fSRussell King zone_size[ZONE_HIGHMEM] = max_high - max_low; 249dde5828fSRussell King #endif 25090072059SRussell King 25190072059SRussell King /* 252be370302SRussell King * Calculate the size of the holes. 253be370302SRussell King * holes = node_size - sum(bank_sizes) 25490072059SRussell King */ 255dde5828fSRussell King memcpy(zhole_size, zone_size, sizeof(zhole_size)); 256a2c54d2aSRussell King for_each_memblock(memory, reg) { 257a2c54d2aSRussell King unsigned long start = memblock_region_memory_base_pfn(reg); 258a2c54d2aSRussell King unsigned long end = memblock_region_memory_end_pfn(reg); 259a2c54d2aSRussell King 260a2c54d2aSRussell King if (start < max_low) { 261a2c54d2aSRussell King unsigned long low_end = min(end, max_low); 262a2c54d2aSRussell King zhole_size[0] -= low_end - start; 263a2c54d2aSRussell King } 264dde5828fSRussell King #ifdef CONFIG_HIGHMEM 265a2c54d2aSRussell King if (end > max_low) { 266a2c54d2aSRussell King unsigned long high_start = max(start, max_low); 267a2c54d2aSRussell King zhole_size[ZONE_HIGHMEM] -= end - high_start; 268a2c54d2aSRussell King } 269dde5828fSRussell King #endif 270dde5828fSRussell King } 27190072059SRussell King 27265032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA 27390072059SRussell King /* 27490072059SRussell King * Adjust the sizes according to any special requirements for 27590072059SRussell King * this machine type. 27690072059SRussell King */ 27765032018SNicolas Pitre if (arm_dma_zone_size) { 278be20902bSRussell King arm_adjust_dma_zone(zone_size, zhole_size, 27965032018SNicolas Pitre arm_dma_zone_size >> PAGE_SHIFT); 28065032018SNicolas Pitre arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1; 28165032018SNicolas Pitre } else 28265032018SNicolas Pitre arm_dma_limit = 0xffffffff; 283be20902bSRussell King #endif 28490072059SRussell King 285be370302SRussell King free_area_init_node(0, zone_size, min, zhole_size); 28690072059SRussell King } 28790072059SRussell King 2887b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID 289b7cfda9fSRussell King int pfn_valid(unsigned long pfn) 290b7cfda9fSRussell King { 291fb492c91SMark Rutland return memblock_is_memory(__pfn_to_phys(pfn)); 292b7cfda9fSRussell King } 293b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid); 2947b7bf499SWill Deacon #endif 295657e12fdSRussell King 2967b7bf499SWill Deacon #ifndef CONFIG_SPARSEMEM 297eda2e5dcSRussell King static void arm_memory_present(void) 298657e12fdSRussell King { 299657e12fdSRussell King } 300657e12fdSRussell King #else 301eda2e5dcSRussell King static void arm_memory_present(void) 302657e12fdSRussell King { 303719c1514SBenjamin Herrenschmidt struct memblock_region *reg; 304719c1514SBenjamin Herrenschmidt 3057c996361SYinghai Lu for_each_memblock(memory, reg) 306c7fc2de0SYinghai Lu memory_present(0, memblock_region_memory_base_pfn(reg), 307c7fc2de0SYinghai Lu memblock_region_memory_end_pfn(reg)); 308657e12fdSRussell King } 309b7cfda9fSRussell King #endif 310b7cfda9fSRussell King 311*716a3dc2SRussell King static bool arm_memblock_steal_permitted = true; 312*716a3dc2SRussell King 313*716a3dc2SRussell King phys_addr_t arm_memblock_steal(phys_addr_t size, phys_addr_t align) 314*716a3dc2SRussell King { 315*716a3dc2SRussell King phys_addr_t phys; 316*716a3dc2SRussell King 317*716a3dc2SRussell King BUG_ON(!arm_memblock_steal_permitted); 318*716a3dc2SRussell King 319*716a3dc2SRussell King phys = memblock_alloc(size, align); 320*716a3dc2SRussell King memblock_free(phys, size); 321*716a3dc2SRussell King memblock_remove(phys, size); 322*716a3dc2SRussell King 323*716a3dc2SRussell King return phys; 324*716a3dc2SRussell King } 325*716a3dc2SRussell King 3268d717a52SRussell King void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc) 3272778f620SRussell King { 3282778f620SRussell King int i; 3292778f620SRussell King 3302778f620SRussell King for (i = 0; i < mi->nr_banks; i++) 3312778f620SRussell King memblock_add(mi->bank[i].start, mi->bank[i].size); 3322778f620SRussell King 3332778f620SRussell King /* Register the kernel text, kernel data and initrd with memblock. */ 3342778f620SRussell King #ifdef CONFIG_XIP_KERNEL 335842eab40SRussell King memblock_reserve(__pa(_sdata), _end - _sdata); 3362778f620SRussell King #else 3372778f620SRussell King memblock_reserve(__pa(_stext), _end - _stext); 3382778f620SRussell King #endif 3392778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD 340b0a2679dSRussell King if (phys_initrd_size && 3418f4b8c76SRussell King !memblock_is_region_memory(phys_initrd_start, phys_initrd_size)) { 3428f4b8c76SRussell King pr_err("INITRD: 0x%08lx+0x%08lx is not a memory region - disabling initrd\n", 3438f4b8c76SRussell King phys_initrd_start, phys_initrd_size); 3448f4b8c76SRussell King phys_initrd_start = phys_initrd_size = 0; 3458f4b8c76SRussell King } 3468f4b8c76SRussell King if (phys_initrd_size && 347b0a2679dSRussell King memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) { 348b0a2679dSRussell King pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region - disabling initrd\n", 349b0a2679dSRussell King phys_initrd_start, phys_initrd_size); 350b0a2679dSRussell King phys_initrd_start = phys_initrd_size = 0; 351b0a2679dSRussell King } 3522778f620SRussell King if (phys_initrd_size) { 3532778f620SRussell King memblock_reserve(phys_initrd_start, phys_initrd_size); 3542778f620SRussell King 3552778f620SRussell King /* Now convert initrd to virtual addresses */ 3562778f620SRussell King initrd_start = __phys_to_virt(phys_initrd_start); 3572778f620SRussell King initrd_end = initrd_start + phys_initrd_size; 3582778f620SRussell King } 3592778f620SRussell King #endif 3602778f620SRussell King 3612778f620SRussell King arm_mm_memblock_reserve(); 36293c02ab4SGrant Likely arm_dt_memblock_reserve(); 3632778f620SRussell King 3648d717a52SRussell King /* reserve any platform specific memblock areas */ 3658d717a52SRussell King if (mdesc->reserve) 3668d717a52SRussell King mdesc->reserve(); 3678d717a52SRussell King 368*716a3dc2SRussell King arm_memblock_steal_permitted = false; 3691aadc056STejun Heo memblock_allow_resize(); 3702778f620SRussell King memblock_dump_all(); 3712778f620SRussell King } 3722778f620SRussell King 3738d717a52SRussell King void __init bootmem_init(void) 37490072059SRussell King { 375dde5828fSRussell King unsigned long min, max_low, max_high; 37690072059SRussell King 377dde5828fSRussell King max_low = max_high = 0; 378dde5828fSRussell King 379f25b4b4cSRussell King find_limits(&min, &max_low, &max_high); 380dde5828fSRussell King 381a801d276SRussell King arm_bootmem_init(min, max_low); 382dde5828fSRussell King 383b7a69ac3SRussell King /* 384657e12fdSRussell King * Sparsemem tries to allocate bootmem in memory_present(), 385657e12fdSRussell King * so must be done after the fixed reservations 386657e12fdSRussell King */ 387eda2e5dcSRussell King arm_memory_present(); 38890072059SRussell King 389b7a69ac3SRussell King /* 390b7a69ac3SRussell King * sparse_init() needs the bootmem allocator up and running. 391b7a69ac3SRussell King */ 392b7a69ac3SRussell King sparse_init(); 393b7a69ac3SRussell King 394b7a69ac3SRussell King /* 395be370302SRussell King * Now free the memory - free_area_init_node needs 396b7a69ac3SRussell King * the sparse mem_map arrays initialized by sparse_init() 397b7a69ac3SRussell King * for memmap_init_zone(), otherwise all PFNs are invalid. 398b7a69ac3SRussell King */ 399a2c54d2aSRussell King arm_bootmem_free(min, max_low, max_high); 400b7a69ac3SRussell King 40190072059SRussell King /* 40290072059SRussell King * This doesn't seem to be used by the Linux memory manager any 40390072059SRussell King * more, but is used by ll_rw_block. If we can get rid of it, we 40490072059SRussell King * also get rid of some of the stuff above as well. 40590072059SRussell King * 40690072059SRussell King * Note: max_low_pfn and max_pfn reflect the number of _pages_ in 40790072059SRussell King * the system, not the maximum PFN. 40890072059SRussell King */ 409dde5828fSRussell King max_low_pfn = max_low - PHYS_PFN_OFFSET; 410dde5828fSRussell King max_pfn = max_high - PHYS_PFN_OFFSET; 41190072059SRussell King } 41290072059SRussell King 4136db015e4SNicolas Pitre static inline int free_area(unsigned long pfn, unsigned long end, char *s) 4141da177e4SLinus Torvalds { 4156db015e4SNicolas Pitre unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10); 4161da177e4SLinus Torvalds 4176db015e4SNicolas Pitre for (; pfn < end; pfn++) { 4186db015e4SNicolas Pitre struct page *page = pfn_to_page(pfn); 4191da177e4SLinus Torvalds ClearPageReserved(page); 4207835e98bSNick Piggin init_page_count(page); 4216db015e4SNicolas Pitre __free_page(page); 4226db015e4SNicolas Pitre pages++; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds if (size && s) 4261da177e4SLinus Torvalds printk(KERN_INFO "Freeing %s memory: %dK\n", s, size); 4276db015e4SNicolas Pitre 4286db015e4SNicolas Pitre return pages; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 43154d52573SStephen Boyd /* 43254d52573SStephen Boyd * Poison init memory with an undefined instruction (ARM) or a branch to an 43354d52573SStephen Boyd * undefined instruction (Thumb). 43454d52573SStephen Boyd */ 43554d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count) 43654d52573SStephen Boyd { 43754d52573SStephen Boyd u32 *p = (u32 *)s; 438bf912d99SJamie Iles for (; count != 0; count -= 4) 43954d52573SStephen Boyd *p++ = 0xe7fddef0; 44054d52573SStephen Boyd } 44154d52573SStephen Boyd 442a013053dSRussell King static inline void 443be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn) 444a013053dSRussell King { 445a013053dSRussell King struct page *start_pg, *end_pg; 446a013053dSRussell King unsigned long pg, pgend; 447a013053dSRussell King 448a013053dSRussell King /* 449a013053dSRussell King * Convert start_pfn/end_pfn to a struct page pointer. 450a013053dSRussell King */ 4513257f43dSCatalin Marinas start_pg = pfn_to_page(start_pfn - 1) + 1; 4529af386c8SWill Deacon end_pg = pfn_to_page(end_pfn - 1) + 1; 453a013053dSRussell King 454a013053dSRussell King /* 455a013053dSRussell King * Convert to physical addresses, and 456a013053dSRussell King * round start upwards and end downwards. 457a013053dSRussell King */ 458cae6292bSWill Deacon pg = (unsigned long)PAGE_ALIGN(__pa(start_pg)); 459cae6292bSWill Deacon pgend = (unsigned long)__pa(end_pg) & PAGE_MASK; 460a013053dSRussell King 461a013053dSRussell King /* 462a013053dSRussell King * If there are free pages between these, 463a013053dSRussell King * free the section of the memmap array. 464a013053dSRussell King */ 465a013053dSRussell King if (pg < pgend) 466be370302SRussell King free_bootmem(pg, pgend - pg); 467a013053dSRussell King } 468a013053dSRussell King 469a013053dSRussell King /* 470a013053dSRussell King * The mem_map array can get very big. Free the unused area of the memory map. 471a013053dSRussell King */ 472be370302SRussell King static void __init free_unused_memmap(struct meminfo *mi) 473a013053dSRussell King { 474a013053dSRussell King unsigned long bank_start, prev_bank_end = 0; 475a013053dSRussell King unsigned int i; 476a013053dSRussell King 477a013053dSRussell King /* 4783260e529SMichael Bohan * This relies on each bank being in address order. 4793260e529SMichael Bohan * The banks are sorted previously in bootmem_init(). 480a013053dSRussell King */ 481be370302SRussell King for_each_bank(i, mi) { 482d2a38ef9SRussell King struct membank *bank = &mi->bank[i]; 483d2a38ef9SRussell King 484d2a38ef9SRussell King bank_start = bank_pfn_start(bank); 485a013053dSRussell King 4869af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 4879af386c8SWill Deacon /* 4889af386c8SWill Deacon * Take care not to free memmap entries that don't exist 4899af386c8SWill Deacon * due to SPARSEMEM sections which aren't present. 4909af386c8SWill Deacon */ 4919af386c8SWill Deacon bank_start = min(bank_start, 4929af386c8SWill Deacon ALIGN(prev_bank_end, PAGES_PER_SECTION)); 493002ea9eeSLinus Walleij #else 494002ea9eeSLinus Walleij /* 495002ea9eeSLinus Walleij * Align down here since the VM subsystem insists that the 496002ea9eeSLinus Walleij * memmap entries are valid from the bank start aligned to 497002ea9eeSLinus Walleij * MAX_ORDER_NR_PAGES. 498002ea9eeSLinus Walleij */ 499002ea9eeSLinus Walleij bank_start = round_down(bank_start, MAX_ORDER_NR_PAGES); 5009af386c8SWill Deacon #endif 501a013053dSRussell King /* 502a013053dSRussell King * If we had a previous bank, and there is a space 503a013053dSRussell King * between the current bank and the previous, free it. 504a013053dSRussell King */ 5053260e529SMichael Bohan if (prev_bank_end && prev_bank_end < bank_start) 506be370302SRussell King free_memmap(prev_bank_end, bank_start); 507a013053dSRussell King 5083260e529SMichael Bohan /* 5093260e529SMichael Bohan * Align up here since the VM subsystem insists that the 5103260e529SMichael Bohan * memmap entries are valid from the bank end aligned to 5113260e529SMichael Bohan * MAX_ORDER_NR_PAGES. 5123260e529SMichael Bohan */ 5133260e529SMichael Bohan prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES); 514a013053dSRussell King } 5159af386c8SWill Deacon 5169af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 5179af386c8SWill Deacon if (!IS_ALIGNED(prev_bank_end, PAGES_PER_SECTION)) 5189af386c8SWill Deacon free_memmap(prev_bank_end, 5199af386c8SWill Deacon ALIGN(prev_bank_end, PAGES_PER_SECTION)); 5209af386c8SWill Deacon #endif 521a013053dSRussell King } 522a013053dSRussell King 523d0e775afSRussell King static void __init free_highpages(void) 524d0e775afSRussell King { 525d0e775afSRussell King #ifdef CONFIG_HIGHMEM 526df4f14c7SRussell King unsigned long max_low = max_low_pfn + PHYS_PFN_OFFSET; 527df4f14c7SRussell King struct memblock_region *mem, *res; 528d0e775afSRussell King 529d0e775afSRussell King /* set highmem page free */ 530df4f14c7SRussell King for_each_memblock(memory, mem) { 531df4f14c7SRussell King unsigned long start = memblock_region_memory_base_pfn(mem); 532df4f14c7SRussell King unsigned long end = memblock_region_memory_end_pfn(mem); 533df4f14c7SRussell King 534df4f14c7SRussell King /* Ignore complete lowmem entries */ 535df4f14c7SRussell King if (end <= max_low) 536df4f14c7SRussell King continue; 537df4f14c7SRussell King 538df4f14c7SRussell King /* Truncate partial highmem entries */ 539df4f14c7SRussell King if (start < max_low) 540df4f14c7SRussell King start = max_low; 541df4f14c7SRussell King 542df4f14c7SRussell King /* Find and exclude any reserved regions */ 543df4f14c7SRussell King for_each_memblock(reserved, res) { 544df4f14c7SRussell King unsigned long res_start, res_end; 545df4f14c7SRussell King 546df4f14c7SRussell King res_start = memblock_region_reserved_base_pfn(res); 547df4f14c7SRussell King res_end = memblock_region_reserved_end_pfn(res); 548df4f14c7SRussell King 549df4f14c7SRussell King if (res_end < start) 550df4f14c7SRussell King continue; 551df4f14c7SRussell King if (res_start < start) 552df4f14c7SRussell King res_start = start; 553df4f14c7SRussell King if (res_start > end) 554df4f14c7SRussell King res_start = end; 555df4f14c7SRussell King if (res_end > end) 556df4f14c7SRussell King res_end = end; 557df4f14c7SRussell King if (res_start != start) 558df4f14c7SRussell King totalhigh_pages += free_area(start, res_start, 559df4f14c7SRussell King NULL); 560df4f14c7SRussell King start = res_end; 561df4f14c7SRussell King if (start == end) 562df4f14c7SRussell King break; 563df4f14c7SRussell King } 564df4f14c7SRussell King 565df4f14c7SRussell King /* And now free anything which remains */ 566df4f14c7SRussell King if (start < end) 567d0e775afSRussell King totalhigh_pages += free_area(start, end, NULL); 568d0e775afSRussell King } 569d0e775afSRussell King totalram_pages += totalhigh_pages; 570d0e775afSRussell King #endif 571d0e775afSRussell King } 572d0e775afSRussell King 5731da177e4SLinus Torvalds /* 5741da177e4SLinus Torvalds * mem_init() marks the free areas in the mem_map and tells us how much 5751da177e4SLinus Torvalds * memory is free. This is done after various parts of the system have 5761da177e4SLinus Torvalds * claimed their memory after the kernel image. 5771da177e4SLinus Torvalds */ 5781da177e4SLinus Torvalds void __init mem_init(void) 5791da177e4SLinus Torvalds { 580db9ef1afSFenkart/Bostandzhyan unsigned long reserved_pages, free_pages; 58147ea3c15SRussell King struct memblock_region *reg; 582be370302SRussell King int i; 5831dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM 5841dbd30e9SLinus Walleij /* These pointers are filled in on TCM detection */ 5851dbd30e9SLinus Walleij extern u32 dtcm_end; 5861dbd30e9SLinus Walleij extern u32 itcm_end; 5871dbd30e9SLinus Walleij #endif 5881da177e4SLinus Torvalds 5893835f6cbSNicolas Pitre max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* this will put all unused low memory onto the freelists */ 592be370302SRussell King free_unused_memmap(&meminfo); 5931da177e4SLinus Torvalds 594be370302SRussell King totalram_pages += free_all_bootmem(); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds #ifdef CONFIG_SA1111 5971da177e4SLinus Torvalds /* now that our DMA memory is actually so designated, we can free it */ 5986db015e4SNicolas Pitre totalram_pages += free_area(PHYS_PFN_OFFSET, 5996db015e4SNicolas Pitre __phys_to_pfn(__pa(swapper_pg_dir)), NULL); 6001da177e4SLinus Torvalds #endif 6011da177e4SLinus Torvalds 602d0e775afSRussell King free_highpages(); 6033835f6cbSNicolas Pitre 604db9ef1afSFenkart/Bostandzhyan reserved_pages = free_pages = 0; 605db9ef1afSFenkart/Bostandzhyan 606be370302SRussell King for_each_bank(i, &meminfo) { 607db9ef1afSFenkart/Bostandzhyan struct membank *bank = &meminfo.bank[i]; 608db9ef1afSFenkart/Bostandzhyan unsigned int pfn1, pfn2; 609db9ef1afSFenkart/Bostandzhyan struct page *page, *end; 610db9ef1afSFenkart/Bostandzhyan 611db9ef1afSFenkart/Bostandzhyan pfn1 = bank_pfn_start(bank); 612db9ef1afSFenkart/Bostandzhyan pfn2 = bank_pfn_end(bank); 613db9ef1afSFenkart/Bostandzhyan 614ea056df7SCatalin Marinas page = pfn_to_page(pfn1); 615ea056df7SCatalin Marinas end = pfn_to_page(pfn2 - 1) + 1; 616db9ef1afSFenkart/Bostandzhyan 617db9ef1afSFenkart/Bostandzhyan do { 618db9ef1afSFenkart/Bostandzhyan if (PageReserved(page)) 619db9ef1afSFenkart/Bostandzhyan reserved_pages++; 620db9ef1afSFenkart/Bostandzhyan else if (!page_count(page)) 621db9ef1afSFenkart/Bostandzhyan free_pages++; 622db9ef1afSFenkart/Bostandzhyan page++; 623db9ef1afSFenkart/Bostandzhyan } while (page < end); 624db9ef1afSFenkart/Bostandzhyan } 625db9ef1afSFenkart/Bostandzhyan 6261da177e4SLinus Torvalds /* 6271da177e4SLinus Torvalds * Since our memory may not be contiguous, calculate the 6281da177e4SLinus Torvalds * real number of pages we have in this system 6291da177e4SLinus Torvalds */ 6301da177e4SLinus Torvalds printk(KERN_INFO "Memory:"); 6311da177e4SLinus Torvalds num_physpages = 0; 63247ea3c15SRussell King for_each_memblock(memory, reg) { 63347ea3c15SRussell King unsigned long pages = memblock_region_memory_end_pfn(reg) - 63447ea3c15SRussell King memblock_region_memory_base_pfn(reg); 63547ea3c15SRussell King num_physpages += pages; 63647ea3c15SRussell King printk(" %ldMB", pages >> (20 - PAGE_SHIFT)); 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT)); 6396db015e4SNicolas Pitre 640db9ef1afSFenkart/Bostandzhyan printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n", 641db9ef1afSFenkart/Bostandzhyan nr_free_pages() << (PAGE_SHIFT-10), 642db9ef1afSFenkart/Bostandzhyan free_pages << (PAGE_SHIFT-10), 643db9ef1afSFenkart/Bostandzhyan reserved_pages << (PAGE_SHIFT-10), 6444b529401SAndreas Fenkart totalhigh_pages << (PAGE_SHIFT-10)); 6451da177e4SLinus Torvalds 646db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10 647db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20 648db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K) 649db9ef1afSFenkart/Bostandzhyan 650db9ef1afSFenkart/Bostandzhyan printk(KERN_NOTICE "Virtual kernel memory layout:\n" 651db9ef1afSFenkart/Bostandzhyan " vector : 0x%08lx - 0x%08lx (%4ld kB)\n" 65207d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 65307d2a5c7SLinus Walleij " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 65407d2a5c7SLinus Walleij " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 65507d2a5c7SLinus Walleij #endif 656db9ef1afSFenkart/Bostandzhyan " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 657db9ef1afSFenkart/Bostandzhyan " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" 658db9ef1afSFenkart/Bostandzhyan " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" 659db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 660db9ef1afSFenkart/Bostandzhyan " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n" 661db9ef1afSFenkart/Bostandzhyan #endif 662db9ef1afSFenkart/Bostandzhyan " modules : 0x%08lx - 0x%08lx (%4ld MB)\n" 663db9ef1afSFenkart/Bostandzhyan " .text : 0x%p" " - 0x%p" " (%4d kB)\n" 6643835d69aSRussell King " .init : 0x%p" " - 0x%p" " (%4d kB)\n" 66545f6d7e0SRabin Vincent " .data : 0x%p" " - 0x%p" " (%4d kB)\n" 66645f6d7e0SRabin Vincent " .bss : 0x%p" " - 0x%p" " (%4d kB)\n", 667db9ef1afSFenkart/Bostandzhyan 668db9ef1afSFenkart/Bostandzhyan MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) + 669db9ef1afSFenkart/Bostandzhyan (PAGE_SIZE)), 67007d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 6711dbd30e9SLinus Walleij MLK(DTCM_OFFSET, (unsigned long) dtcm_end), 6721dbd30e9SLinus Walleij MLK(ITCM_OFFSET, (unsigned long) itcm_end), 67307d2a5c7SLinus Walleij #endif 674db9ef1afSFenkart/Bostandzhyan MLK(FIXADDR_START, FIXADDR_TOP), 675c931b4f6SFenkart/Bostandzhyan MLM(VMALLOC_START, VMALLOC_END), 676db9ef1afSFenkart/Bostandzhyan MLM(PAGE_OFFSET, (unsigned long)high_memory), 677db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 678db9ef1afSFenkart/Bostandzhyan MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) * 679db9ef1afSFenkart/Bostandzhyan (PAGE_SIZE)), 680db9ef1afSFenkart/Bostandzhyan #endif 681db9ef1afSFenkart/Bostandzhyan MLM(MODULES_VADDR, MODULES_END), 682db9ef1afSFenkart/Bostandzhyan 683db9ef1afSFenkart/Bostandzhyan MLK_ROUNDUP(_text, _etext), 6843835d69aSRussell King MLK_ROUNDUP(__init_begin, __init_end), 68545f6d7e0SRabin Vincent MLK_ROUNDUP(_sdata, _edata), 68645f6d7e0SRabin Vincent MLK_ROUNDUP(__bss_start, __bss_stop)); 687db9ef1afSFenkart/Bostandzhyan 688db9ef1afSFenkart/Bostandzhyan #undef MLK 689db9ef1afSFenkart/Bostandzhyan #undef MLM 690db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP 691db9ef1afSFenkart/Bostandzhyan 692a1839272SFenkart/Bostandzhyan /* 693a1839272SFenkart/Bostandzhyan * Check boundaries twice: Some fundamental inconsistencies can 694a1839272SFenkart/Bostandzhyan * be detected at build time already. 695a1839272SFenkart/Bostandzhyan */ 696a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU 697a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 698a1839272SFenkart/Bostandzhyan BUG_ON(TASK_SIZE > MODULES_VADDR); 699a1839272SFenkart/Bostandzhyan #endif 700a1839272SFenkart/Bostandzhyan 701a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 702a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 703a1839272SFenkart/Bostandzhyan BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 704a1839272SFenkart/Bostandzhyan #endif 705a1839272SFenkart/Bostandzhyan 7061da177e4SLinus Torvalds if (PAGE_SIZE >= 16384 && num_physpages <= 128) { 7071da177e4SLinus Torvalds extern int sysctl_overcommit_memory; 7081da177e4SLinus Torvalds /* 7091da177e4SLinus Torvalds * On a machine this small we won't get 7101da177e4SLinus Torvalds * anywhere without overcommit, so turn 7111da177e4SLinus Torvalds * it on by default. 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds void free_initmem(void) 7181da177e4SLinus Torvalds { 719bc581770SLinus Walleij #ifdef CONFIG_HAVE_TCM 720ea208f64SLinus Walleij extern char __tcm_start, __tcm_end; 721bc581770SLinus Walleij 72254d52573SStephen Boyd poison_init_mem(&__tcm_start, &__tcm_end - &__tcm_start); 723ea208f64SLinus Walleij totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)), 724ea208f64SLinus Walleij __phys_to_pfn(__pa(&__tcm_end)), 725bc581770SLinus Walleij "TCM link"); 726bc581770SLinus Walleij #endif 727bc581770SLinus Walleij 72854d52573SStephen Boyd poison_init_mem(__init_begin, __init_end - __init_begin); 7296db015e4SNicolas Pitre if (!machine_is_integrator() && !machine_is_cintegrator()) 73037efe642SRussell King totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)), 73137efe642SRussell King __phys_to_pfn(__pa(__init_end)), 7321da177e4SLinus Torvalds "init"); 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds static int keep_initrd; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end) 7401da177e4SLinus Torvalds { 74154d52573SStephen Boyd if (!keep_initrd) { 74254d52573SStephen Boyd poison_init_mem((void *)start, PAGE_ALIGN(end) - start); 7436db015e4SNicolas Pitre totalram_pages += free_area(__phys_to_pfn(__pa(start)), 7446db015e4SNicolas Pitre __phys_to_pfn(__pa(end)), 7456db015e4SNicolas Pitre "initrd"); 7461da177e4SLinus Torvalds } 74754d52573SStephen Boyd } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused) 7501da177e4SLinus Torvalds { 7511da177e4SLinus Torvalds keep_initrd = 1; 7521da177e4SLinus Torvalds return 1; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup); 7561da177e4SLinus Torvalds #endif 757