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/sort.h>
19 #include <linux/highmem.h>
20 #include <linux/gfp.h>
22 #include <asm/mach-types.h>
23 #include <asm/sections.h>
24 #include <asm/setup.h>
25 #include <asm/sizes.h>
27 #include <asm/fixmap.h>
29 #include <asm/mach/arch.h>
30 #include <asm/mach/map.h>
34 static unsigned long phys_initrd_start __initdata = 0;
35 static unsigned long phys_initrd_size __initdata = 0;
37 static int __init early_initrd(char *p)
39 unsigned long start, size;
42 start = memparse(p, &endp);
44 size = memparse(endp + 1, NULL);
46 phys_initrd_start = start;
47 phys_initrd_size = size;
51 early_param("initrd", early_initrd);
53 static int __init parse_tag_initrd(const struct tag *tag)
55 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
56 "please update your bootloader.\n");
57 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
58 phys_initrd_size = tag->u.initrd.size;
62 __tagtable(ATAG_INITRD, parse_tag_initrd);
64 static int __init parse_tag_initrd2(const struct tag *tag)
66 phys_initrd_start = tag->u.initrd.start;
67 phys_initrd_size = tag->u.initrd.size;
71 __tagtable(ATAG_INITRD2, parse_tag_initrd2);
74 * This keeps memory configuration data used by a couple memory
75 * initialization functions, as well as show_mem() for the skipping
76 * of holes in the memory map. It is populated by arm_add_memory().
78 struct meminfo meminfo;
82 int free = 0, total = 0, reserved = 0;
83 int shared = 0, cached = 0, slab = 0, node, i;
84 struct meminfo * mi = &meminfo;
86 printk("Mem-info:\n");
88 for_each_online_node(node) {
89 for_each_nodebank (i,mi,node) {
90 struct membank *bank = &mi->bank[i];
91 unsigned int pfn1, pfn2;
92 struct page *page, *end;
94 pfn1 = bank_pfn_start(bank);
95 pfn2 = bank_pfn_end(bank);
97 page = pfn_to_page(pfn1);
98 end = pfn_to_page(pfn2 - 1) + 1;
102 if (PageReserved(page))
104 else if (PageSwapCache(page))
106 else if (PageSlab(page))
108 else if (!page_count(page))
111 shared += page_count(page) - 1;
113 } while (page < end);
117 printk("%d pages of RAM\n", total);
118 printk("%d free pages\n", free);
119 printk("%d reserved pages\n", reserved);
120 printk("%d slab pages\n", slab);
121 printk("%d pages shared\n", shared);
122 printk("%d pages swap cached\n", cached);
125 static void __init find_node_limits(int node, struct meminfo *mi,
126 unsigned long *min, unsigned long *max_low, unsigned long *max_high)
131 *max_low = *max_high = 0;
133 for_each_nodebank(i, mi, node) {
134 struct membank *bank = &mi->bank[i];
135 unsigned long start, end;
137 start = bank_pfn_start(bank);
138 end = bank_pfn_end(bank);
152 * FIXME: We really want to avoid allocating the bootmap bitmap
153 * over the top of the initrd. Hopefully, this is located towards
154 * the start of a bank, so if we allocate the bootmap bitmap at
155 * the end, we won't clash.
157 static unsigned int __init
158 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
160 unsigned int start_pfn, i, bootmap_pfn;
162 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
165 for_each_nodebank(i, mi, node) {
166 struct membank *bank = &mi->bank[i];
167 unsigned int start, end;
169 start = bank_pfn_start(bank);
170 end = bank_pfn_end(bank);
175 if (start < start_pfn)
181 if (end - start >= bootmap_pages) {
187 if (bootmap_pfn == 0)
193 static int __init check_initrd(struct meminfo *mi)
195 int initrd_node = -2;
196 #ifdef CONFIG_BLK_DEV_INITRD
197 unsigned long end = phys_initrd_start + phys_initrd_size;
200 * Make sure that the initrd is within a valid area of
203 if (phys_initrd_size) {
208 for (i = 0; i < mi->nr_banks; i++) {
209 struct membank *bank = &mi->bank[i];
210 if (bank_phys_start(bank) <= phys_initrd_start &&
211 end <= bank_phys_end(bank))
212 initrd_node = bank->node;
216 if (initrd_node == -1) {
217 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
218 "physical memory - disabling initrd\n",
219 phys_initrd_start, phys_initrd_size);
220 phys_initrd_start = phys_initrd_size = 0;
227 static inline void map_memory_bank(struct membank *bank)
232 map.pfn = bank_pfn_start(bank);
233 map.virtual = __phys_to_virt(bank_phys_start(bank));
234 map.length = bank_phys_size(bank);
235 map.type = MT_MEMORY;
237 create_mapping(&map);
241 static void __init bootmem_init_node(int node, struct meminfo *mi,
242 unsigned long start_pfn, unsigned long end_pfn)
244 unsigned long boot_pfn;
245 unsigned int boot_pages;
250 * Map the memory banks for this node.
252 for_each_nodebank(i, mi, node) {
253 struct membank *bank = &mi->bank[i];
256 map_memory_bank(bank);
260 * Allocate the bootmem bitmap page.
262 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
263 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
266 * Initialise the bootmem allocator for this node, handing the
267 * memory banks over to bootmem.
269 node_set_online(node);
270 pgdat = NODE_DATA(node);
271 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
273 for_each_nodebank(i, mi, node) {
274 struct membank *bank = &mi->bank[i];
276 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
280 * Reserve the bootmem bitmap for this node.
282 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
283 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
286 static void __init bootmem_reserve_initrd(int node)
288 #ifdef CONFIG_BLK_DEV_INITRD
289 pg_data_t *pgdat = NODE_DATA(node);
292 res = reserve_bootmem_node(pgdat, phys_initrd_start,
293 phys_initrd_size, BOOTMEM_EXCLUSIVE);
296 initrd_start = __phys_to_virt(phys_initrd_start);
297 initrd_end = initrd_start + phys_initrd_size;
300 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
301 "memory region - disabling initrd\n",
302 phys_initrd_start, phys_initrd_size);
307 static void __init bootmem_free_node(int node, struct meminfo *mi)
309 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
310 unsigned long min, max_low, max_high;
313 find_node_limits(node, mi, &min, &max_low, &max_high);
316 * initialise the zones within this node.
318 memset(zone_size, 0, sizeof(zone_size));
321 * The size of this node has already been determined. If we need
322 * to do anything fancy with the allocation of this memory to the
323 * zones, now is the time to do it.
325 zone_size[0] = max_low - min;
326 #ifdef CONFIG_HIGHMEM
327 zone_size[ZONE_HIGHMEM] = max_high - max_low;
331 * For each bank in this node, calculate the size of the holes.
332 * holes = node_size - sum(bank_sizes_in_node)
334 memcpy(zhole_size, zone_size, sizeof(zhole_size));
335 for_each_nodebank(i, mi, node) {
337 #ifdef CONFIG_HIGHMEM
338 if (mi->bank[i].highmem)
341 zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
345 * Adjust the sizes according to any special requirements for
348 arch_adjust_zones(node, zone_size, zhole_size);
350 free_area_init_node(node, zone_size, min, zhole_size);
353 #ifndef CONFIG_SPARSEMEM
354 int pfn_valid(unsigned long pfn)
356 struct meminfo *mi = &meminfo;
357 unsigned int left = 0, right = mi->nr_banks;
360 unsigned int mid = (right + left) / 2;
361 struct membank *bank = &mi->bank[mid];
363 if (pfn < bank_pfn_start(bank))
365 else if (pfn >= bank_pfn_end(bank))
369 } while (left < right);
372 EXPORT_SYMBOL(pfn_valid);
374 static void arm_memory_present(struct meminfo *mi, int node)
378 static void arm_memory_present(struct meminfo *mi, int node)
381 for_each_nodebank(i, mi, node) {
382 struct membank *bank = &mi->bank[i];
383 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
388 static int __init meminfo_cmp(const void *_a, const void *_b)
390 const struct membank *a = _a, *b = _b;
391 long cmp = bank_pfn_start(a) - bank_pfn_start(b);
392 return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
395 void __init bootmem_init(void)
397 struct meminfo *mi = &meminfo;
398 unsigned long min, max_low, max_high;
399 int node, initrd_node;
401 sort(&mi->bank, mi->nr_banks, sizeof(mi->bank[0]), meminfo_cmp, NULL);
404 * Locate which node contains the ramdisk image, if any.
406 initrd_node = check_initrd(mi);
408 max_low = max_high = 0;
411 * Run through each node initialising the bootmem allocator.
413 for_each_node(node) {
414 unsigned long node_low, node_high;
416 find_node_limits(node, mi, &min, &node_low, &node_high);
418 if (node_low > max_low)
420 if (node_high > max_high)
421 max_high = node_high;
424 * If there is no memory in this node, ignore it.
425 * (We can't have nodes which have no lowmem)
430 bootmem_init_node(node, mi, min, node_low);
433 * Reserve any special node zero regions.
436 reserve_node_zero(NODE_DATA(node));
439 * If the initrd is in this node, reserve its memory.
441 if (node == initrd_node)
442 bootmem_reserve_initrd(node);
445 * Sparsemem tries to allocate bootmem in memory_present(),
446 * so must be done after the fixed reservations
448 arm_memory_present(mi, node);
452 * sparse_init() needs the bootmem allocator up and running.
457 * Now free memory in each node - free_area_init_node needs
458 * the sparse mem_map arrays initialized by sparse_init()
459 * for memmap_init_zone(), otherwise all PFNs are invalid.
462 bootmem_free_node(node, mi);
464 high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
467 * This doesn't seem to be used by the Linux memory manager any
468 * more, but is used by ll_rw_block. If we can get rid of it, we
469 * also get rid of some of the stuff above as well.
471 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
472 * the system, not the maximum PFN.
474 max_low_pfn = max_low - PHYS_PFN_OFFSET;
475 max_pfn = max_high - PHYS_PFN_OFFSET;
478 static inline int free_area(unsigned long pfn, unsigned long end, char *s)
480 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
482 for (; pfn < end; pfn++) {
483 struct page *page = pfn_to_page(pfn);
484 ClearPageReserved(page);
485 init_page_count(page);
491 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
497 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
499 struct page *start_pg, *end_pg;
500 unsigned long pg, pgend;
503 * Convert start_pfn/end_pfn to a struct page pointer.
505 start_pg = pfn_to_page(start_pfn - 1) + 1;
506 end_pg = pfn_to_page(end_pfn);
509 * Convert to physical addresses, and
510 * round start upwards and end downwards.
512 pg = PAGE_ALIGN(__pa(start_pg));
513 pgend = __pa(end_pg) & PAGE_MASK;
516 * If there are free pages between these,
517 * free the section of the memmap array.
520 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
524 * The mem_map array can get very big. Free the unused area of the memory map.
526 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
528 unsigned long bank_start, prev_bank_end = 0;
532 * [FIXME] This relies on each bank being in address order. This
533 * may not be the case, especially if the user has provided the
534 * information on the command line.
536 for_each_nodebank(i, mi, node) {
537 struct membank *bank = &mi->bank[i];
539 bank_start = bank_pfn_start(bank);
540 if (bank_start < prev_bank_end) {
541 printk(KERN_ERR "MEM: unordered memory banks. "
542 "Not freeing memmap.\n");
547 * If we had a previous bank, and there is a space
548 * between the current bank and the previous, free it.
550 if (prev_bank_end && prev_bank_end != bank_start)
551 free_memmap(node, prev_bank_end, bank_start);
553 prev_bank_end = bank_pfn_end(bank);
558 * mem_init() marks the free areas in the mem_map and tells us how much
559 * memory is free. This is done after various parts of the system have
560 * claimed their memory after the kernel image.
562 void __init mem_init(void)
564 unsigned long reserved_pages, free_pages;
567 #ifndef CONFIG_DISCONTIGMEM
568 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
571 /* this will put all unused low memory onto the freelists */
572 for_each_online_node(node) {
573 pg_data_t *pgdat = NODE_DATA(node);
575 free_unused_memmap_node(node, &meminfo);
577 if (pgdat->node_spanned_pages != 0)
578 totalram_pages += free_all_bootmem_node(pgdat);
582 /* now that our DMA memory is actually so designated, we can free it */
583 totalram_pages += free_area(PHYS_PFN_OFFSET,
584 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
587 #ifdef CONFIG_HIGHMEM
588 /* set highmem page free */
589 for_each_online_node(node) {
590 for_each_nodebank (i, &meminfo, node) {
591 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
592 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
593 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
594 totalhigh_pages += free_area(start, end, NULL);
597 totalram_pages += totalhigh_pages;
600 reserved_pages = free_pages = 0;
602 for_each_online_node(node) {
603 for_each_nodebank(i, &meminfo, node) {
604 struct membank *bank = &meminfo.bank[i];
605 unsigned int pfn1, pfn2;
606 struct page *page, *end;
608 pfn1 = bank_pfn_start(bank);
609 pfn2 = bank_pfn_end(bank);
611 page = pfn_to_page(pfn1);
612 end = pfn_to_page(pfn2 - 1) + 1;
615 if (PageReserved(page))
617 else if (!page_count(page))
620 } while (page < end);
625 * Since our memory may not be contiguous, calculate the
626 * real number of pages we have in this system
628 printk(KERN_INFO "Memory:");
630 for (i = 0; i < meminfo.nr_banks; i++) {
631 num_physpages += bank_pfn_size(&meminfo.bank[i]);
632 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
634 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
636 printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
637 nr_free_pages() << (PAGE_SHIFT-10),
638 free_pages << (PAGE_SHIFT-10),
639 reserved_pages << (PAGE_SHIFT-10),
640 totalhigh_pages << (PAGE_SHIFT-10));
642 #define MLK(b, t) b, t, ((t) - (b)) >> 10
643 #define MLM(b, t) b, t, ((t) - (b)) >> 20
644 #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
646 printk(KERN_NOTICE "Virtual kernel memory layout:\n"
647 " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
648 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
650 " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
652 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
653 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
654 #ifdef CONFIG_HIGHMEM
655 " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
657 " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
658 " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
659 " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
660 " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
662 MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
664 MLK(FIXADDR_START, FIXADDR_TOP),
666 MLM(CONSISTENT_BASE, CONSISTENT_END),
668 MLM(VMALLOC_START, VMALLOC_END),
669 MLM(PAGE_OFFSET, (unsigned long)high_memory),
670 #ifdef CONFIG_HIGHMEM
671 MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
674 MLM(MODULES_VADDR, MODULES_END),
676 MLK_ROUNDUP(__init_begin, __init_end),
677 MLK_ROUNDUP(_text, _etext),
678 MLK_ROUNDUP(_data, _edata));
685 * Check boundaries twice: Some fundamental inconsistencies can
686 * be detected at build time already.
689 BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
690 BUG_ON(VMALLOC_END > CONSISTENT_BASE);
692 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
693 BUG_ON(TASK_SIZE > MODULES_VADDR);
696 #ifdef CONFIG_HIGHMEM
697 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
698 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
701 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
702 extern int sysctl_overcommit_memory;
704 * On a machine this small we won't get
705 * anywhere without overcommit, so turn
708 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
712 void free_initmem(void)
714 #ifdef CONFIG_HAVE_TCM
715 extern char *__tcm_start, *__tcm_end;
717 totalram_pages += free_area(__phys_to_pfn(__pa(__tcm_start)),
718 __phys_to_pfn(__pa(__tcm_end)),
722 if (!machine_is_integrator() && !machine_is_cintegrator())
723 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
724 __phys_to_pfn(__pa(__init_end)),
728 #ifdef CONFIG_BLK_DEV_INITRD
730 static int keep_initrd;
732 void free_initrd_mem(unsigned long start, unsigned long end)
735 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
736 __phys_to_pfn(__pa(end)),
740 static int __init keepinitrd_setup(char *__unused)
746 __setup("keepinitrd", keepinitrd_setup);