2 * linux/arch/arm/mm/init.c
4 * Copyright (C) 1995-2005 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/swap.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/mman.h>
16 #include <linux/nodemask.h>
17 #include <linux/initrd.h>
18 #include <linux/of_fdt.h>
19 #include <linux/highmem.h>
20 #include <linux/gfp.h>
21 #include <linux/memblock.h>
22 #include <linux/sort.h>
24 #include <asm/mach-types.h>
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/sizes.h>
29 #include <asm/fixmap.h>
31 #include <asm/mach/arch.h>
32 #include <asm/mach/map.h>
36 static unsigned long phys_initrd_start __initdata = 0;
37 static unsigned long phys_initrd_size __initdata = 0;
39 static int __init early_initrd(char *p)
41 unsigned long start, size;
44 start = memparse(p, &endp);
46 size = memparse(endp + 1, NULL);
48 phys_initrd_start = start;
49 phys_initrd_size = size;
53 early_param("initrd", early_initrd);
55 static int __init parse_tag_initrd(const struct tag *tag)
57 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
58 "please update your bootloader.\n");
59 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
60 phys_initrd_size = tag->u.initrd.size;
64 __tagtable(ATAG_INITRD, parse_tag_initrd);
66 static int __init parse_tag_initrd2(const struct tag *tag)
68 phys_initrd_start = tag->u.initrd.start;
69 phys_initrd_size = tag->u.initrd.size;
73 __tagtable(ATAG_INITRD2, parse_tag_initrd2);
75 #ifdef CONFIG_OF_FLATTREE
76 void __init early_init_dt_setup_initrd_arch(unsigned long start, unsigned long end)
78 phys_initrd_start = start;
79 phys_initrd_size = end - start;
81 #endif /* CONFIG_OF_FLATTREE */
84 * This keeps memory configuration data used by a couple memory
85 * initialization functions, as well as show_mem() for the skipping
86 * of holes in the memory map. It is populated by arm_add_memory().
88 struct meminfo meminfo;
90 void show_mem(unsigned int filter)
92 int free = 0, total = 0, reserved = 0;
93 int shared = 0, cached = 0, slab = 0, i;
94 struct meminfo * mi = &meminfo;
96 printk("Mem-info:\n");
99 for_each_bank (i, mi) {
100 struct membank *bank = &mi->bank[i];
101 unsigned int pfn1, pfn2;
102 struct page *page, *end;
104 pfn1 = bank_pfn_start(bank);
105 pfn2 = bank_pfn_end(bank);
107 page = pfn_to_page(pfn1);
108 end = pfn_to_page(pfn2 - 1) + 1;
112 if (PageReserved(page))
114 else if (PageSwapCache(page))
116 else if (PageSlab(page))
118 else if (!page_count(page))
121 shared += page_count(page) - 1;
123 } while (page < end);
126 printk("%d pages of RAM\n", total);
127 printk("%d free pages\n", free);
128 printk("%d reserved pages\n", reserved);
129 printk("%d slab pages\n", slab);
130 printk("%d pages shared\n", shared);
131 printk("%d pages swap cached\n", cached);
134 static void __init find_limits(unsigned long *min, unsigned long *max_low,
135 unsigned long *max_high)
137 struct meminfo *mi = &meminfo;
141 *max_low = *max_high = 0;
143 for_each_bank (i, mi) {
144 struct membank *bank = &mi->bank[i];
145 unsigned long start, end;
147 start = bank_pfn_start(bank);
148 end = bank_pfn_end(bank);
161 static void __init arm_bootmem_init(unsigned long start_pfn,
162 unsigned long end_pfn)
164 struct memblock_region *reg;
165 unsigned int boot_pages;
170 * Allocate the bootmem bitmap page. This must be in a region
171 * of memory which has already been mapped.
173 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
174 bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
175 __pfn_to_phys(end_pfn));
178 * Initialise the bootmem allocator, handing the
179 * memory banks over to bootmem.
182 pgdat = NODE_DATA(0);
183 init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
185 /* Free the lowmem regions from memblock into bootmem. */
186 for_each_memblock(memory, reg) {
187 unsigned long start = memblock_region_memory_base_pfn(reg);
188 unsigned long end = memblock_region_memory_end_pfn(reg);
195 free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT);
198 /* Reserve the lowmem memblock reserved regions in bootmem. */
199 for_each_memblock(reserved, reg) {
200 unsigned long start = memblock_region_reserved_base_pfn(reg);
201 unsigned long end = memblock_region_reserved_end_pfn(reg);
208 reserve_bootmem(__pfn_to_phys(start),
209 (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT);
213 static void __init arm_bootmem_free(unsigned long min, unsigned long max_low,
214 unsigned long max_high)
216 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
217 struct memblock_region *reg;
220 * initialise the zones.
222 memset(zone_size, 0, sizeof(zone_size));
225 * The memory size has already been determined. If we need
226 * to do anything fancy with the allocation of this memory
227 * to the zones, now is the time to do it.
229 zone_size[0] = max_low - min;
230 #ifdef CONFIG_HIGHMEM
231 zone_size[ZONE_HIGHMEM] = max_high - max_low;
235 * Calculate the size of the holes.
236 * holes = node_size - sum(bank_sizes)
238 memcpy(zhole_size, zone_size, sizeof(zhole_size));
239 for_each_memblock(memory, reg) {
240 unsigned long start = memblock_region_memory_base_pfn(reg);
241 unsigned long end = memblock_region_memory_end_pfn(reg);
243 if (start < max_low) {
244 unsigned long low_end = min(end, max_low);
245 zhole_size[0] -= low_end - start;
247 #ifdef CONFIG_HIGHMEM
249 unsigned long high_start = max(start, max_low);
250 zhole_size[ZONE_HIGHMEM] -= end - high_start;
256 * Adjust the sizes according to any special requirements for
259 arch_adjust_zones(zone_size, zhole_size);
261 free_area_init_node(0, zone_size, min, zhole_size);
264 #ifndef CONFIG_SPARSEMEM
265 int pfn_valid(unsigned long pfn)
267 return memblock_is_memory(pfn << PAGE_SHIFT);
269 EXPORT_SYMBOL(pfn_valid);
271 static void arm_memory_present(void)
275 static void arm_memory_present(void)
277 struct memblock_region *reg;
279 for_each_memblock(memory, reg)
280 memory_present(0, memblock_region_memory_base_pfn(reg),
281 memblock_region_memory_end_pfn(reg));
285 static int __init meminfo_cmp(const void *_a, const void *_b)
287 const struct membank *a = _a, *b = _b;
288 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
289 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
292 void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc)
296 sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]), meminfo_cmp, NULL);
299 for (i = 0; i < mi->nr_banks; i++)
300 memblock_add(mi->bank[i].start, mi->bank[i].size);
302 /* Register the kernel text, kernel data and initrd with memblock. */
303 #ifdef CONFIG_XIP_KERNEL
304 memblock_reserve(__pa(_sdata), _end - _sdata);
306 memblock_reserve(__pa(_stext), _end - _stext);
308 #ifdef CONFIG_BLK_DEV_INITRD
309 if (phys_initrd_size &&
310 memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) {
311 pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region - disabling initrd\n",
312 phys_initrd_start, phys_initrd_size);
313 phys_initrd_start = phys_initrd_size = 0;
315 if (phys_initrd_size) {
316 memblock_reserve(phys_initrd_start, phys_initrd_size);
318 /* Now convert initrd to virtual addresses */
319 initrd_start = __phys_to_virt(phys_initrd_start);
320 initrd_end = initrd_start + phys_initrd_size;
324 arm_mm_memblock_reserve();
326 /* reserve any platform specific memblock areas */
334 void __init bootmem_init(void)
336 unsigned long min, max_low, max_high;
338 max_low = max_high = 0;
340 find_limits(&min, &max_low, &max_high);
342 arm_bootmem_init(min, max_low);
345 * Sparsemem tries to allocate bootmem in memory_present(),
346 * so must be done after the fixed reservations
348 arm_memory_present();
351 * sparse_init() needs the bootmem allocator up and running.
356 * Now free the memory - free_area_init_node needs
357 * the sparse mem_map arrays initialized by sparse_init()
358 * for memmap_init_zone(), otherwise all PFNs are invalid.
360 arm_bootmem_free(min, max_low, max_high);
362 high_memory = __va(((phys_addr_t)max_low << PAGE_SHIFT) - 1) + 1;
365 * This doesn't seem to be used by the Linux memory manager any
366 * more, but is used by ll_rw_block. If we can get rid of it, we
367 * also get rid of some of the stuff above as well.
369 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
370 * the system, not the maximum PFN.
372 max_low_pfn = max_low - PHYS_PFN_OFFSET;
373 max_pfn = max_high - PHYS_PFN_OFFSET;
376 static inline int free_area(unsigned long pfn, unsigned long end, char *s)
378 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
380 for (; pfn < end; pfn++) {
381 struct page *page = pfn_to_page(pfn);
382 ClearPageReserved(page);
383 init_page_count(page);
389 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
395 free_memmap(unsigned long start_pfn, unsigned long end_pfn)
397 struct page *start_pg, *end_pg;
398 unsigned long pg, pgend;
401 * Convert start_pfn/end_pfn to a struct page pointer.
403 start_pg = pfn_to_page(start_pfn - 1) + 1;
404 end_pg = pfn_to_page(end_pfn);
407 * Convert to physical addresses, and
408 * round start upwards and end downwards.
410 pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
411 pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
414 * If there are free pages between these,
415 * free the section of the memmap array.
418 free_bootmem(pg, pgend - pg);
422 * The mem_map array can get very big. Free the unused area of the memory map.
424 static void __init free_unused_memmap(struct meminfo *mi)
426 unsigned long bank_start, prev_bank_end = 0;
430 * This relies on each bank being in address order.
431 * The banks are sorted previously in bootmem_init().
433 for_each_bank(i, mi) {
434 struct membank *bank = &mi->bank[i];
436 bank_start = bank_pfn_start(bank);
439 * If we had a previous bank, and there is a space
440 * between the current bank and the previous, free it.
442 if (prev_bank_end && prev_bank_end < bank_start)
443 free_memmap(prev_bank_end, bank_start);
446 * Align up here since the VM subsystem insists that the
447 * memmap entries are valid from the bank end aligned to
448 * MAX_ORDER_NR_PAGES.
450 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
454 static void __init free_highpages(void)
456 #ifdef CONFIG_HIGHMEM
457 unsigned long max_low = max_low_pfn + PHYS_PFN_OFFSET;
458 struct memblock_region *mem, *res;
460 /* set highmem page free */
461 for_each_memblock(memory, mem) {
462 unsigned long start = memblock_region_memory_base_pfn(mem);
463 unsigned long end = memblock_region_memory_end_pfn(mem);
465 /* Ignore complete lowmem entries */
469 /* Truncate partial highmem entries */
473 /* Find and exclude any reserved regions */
474 for_each_memblock(reserved, res) {
475 unsigned long res_start, res_end;
477 res_start = memblock_region_reserved_base_pfn(res);
478 res_end = memblock_region_reserved_end_pfn(res);
482 if (res_start < start)
488 if (res_start != start)
489 totalhigh_pages += free_area(start, res_start,
496 /* And now free anything which remains */
498 totalhigh_pages += free_area(start, end, NULL);
500 totalram_pages += totalhigh_pages;
505 * mem_init() marks the free areas in the mem_map and tells us how much
506 * memory is free. This is done after various parts of the system have
507 * claimed their memory after the kernel image.
509 void __init mem_init(void)
511 unsigned long reserved_pages, free_pages;
512 struct memblock_region *reg;
514 #ifdef CONFIG_HAVE_TCM
515 /* These pointers are filled in on TCM detection */
520 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
522 /* this will put all unused low memory onto the freelists */
523 free_unused_memmap(&meminfo);
525 totalram_pages += free_all_bootmem();
528 /* now that our DMA memory is actually so designated, we can free it */
529 totalram_pages += free_area(PHYS_PFN_OFFSET,
530 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
535 reserved_pages = free_pages = 0;
537 for_each_bank(i, &meminfo) {
538 struct membank *bank = &meminfo.bank[i];
539 unsigned int pfn1, pfn2;
540 struct page *page, *end;
542 pfn1 = bank_pfn_start(bank);
543 pfn2 = bank_pfn_end(bank);
545 page = pfn_to_page(pfn1);
546 end = pfn_to_page(pfn2 - 1) + 1;
549 if (PageReserved(page))
551 else if (!page_count(page))
554 } while (page < end);
558 * Since our memory may not be contiguous, calculate the
559 * real number of pages we have in this system
561 printk(KERN_INFO "Memory:");
563 for_each_memblock(memory, reg) {
564 unsigned long pages = memblock_region_memory_end_pfn(reg) -
565 memblock_region_memory_base_pfn(reg);
566 num_physpages += pages;
567 printk(" %ldMB", pages >> (20 - PAGE_SHIFT));
569 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
571 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
572 nr_free_pages() << (PAGE_SHIFT-10),
573 free_pages << (PAGE_SHIFT-10),
574 reserved_pages << (PAGE_SHIFT-10),
575 totalhigh_pages << (PAGE_SHIFT-10));
577 #define MLK(b, t) b, t, ((t) - (b)) >> 10
578 #define MLM(b, t) b, t, ((t) - (b)) >> 20
579 #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
581 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
582 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
583 #ifdef CONFIG_HAVE_TCM
584 " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n"
585 " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n"
587 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
589 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
591 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
592 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
593 #ifdef CONFIG_HIGHMEM
594 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
596 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
597 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
598 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
599 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
601 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
603 #ifdef CONFIG_HAVE_TCM
604 MLK(DTCM_OFFSET, (unsigned long) dtcm_end),
605 MLK(ITCM_OFFSET, (unsigned long) itcm_end),
607 MLK(FIXADDR_START, FIXADDR_TOP),
609 MLM(CONSISTENT_BASE, CONSISTENT_END),
611 MLM(VMALLOC_START, VMALLOC_END),
612 MLM(PAGE_OFFSET, (unsigned long)high_memory),
613 #ifdef CONFIG_HIGHMEM
614 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
617 MLM(MODULES_VADDR, MODULES_END),
619 MLK_ROUNDUP(__init_begin, __init_end),
620 MLK_ROUNDUP(_text, _etext),
621 MLK_ROUNDUP(_sdata, _edata));
628 * Check boundaries twice: Some fundamental inconsistencies can
629 * be detected at build time already.
632 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
633 BUG_ON(VMALLOC_END > CONSISTENT_BASE);
635 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
636 BUG_ON(TASK_SIZE > MODULES_VADDR);
639 #ifdef CONFIG_HIGHMEM
640 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
641 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
644 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
645 extern int sysctl_overcommit_memory;
647 * On a machine this small we won't get
648 * anywhere without overcommit, so turn
651 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
655 void free_initmem(void)
657 #ifdef CONFIG_HAVE_TCM
658 extern char __tcm_start, __tcm_end;
660 totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)),
661 __phys_to_pfn(__pa(&__tcm_end)),
665 if (!machine_is_integrator() && !machine_is_cintegrator())
666 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
667 __phys_to_pfn(__pa(__init_end)),
671 #ifdef CONFIG_BLK_DEV_INITRD
673 static int keep_initrd;
675 void free_initrd_mem(unsigned long start, unsigned long end)
678 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
679 __phys_to_pfn(__pa(end)),
683 static int __init keepinitrd_setup(char *__unused)
689 __setup("keepinitrd", keepinitrd_setup);