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>
19 #include <asm/mach-types.h>
20 #include <asm/sections.h>
21 #include <asm/setup.h>
22 #include <asm/sizes.h>
25 #include <asm/mach/arch.h>
26 #include <asm/mach/map.h>
30 static unsigned long phys_initrd_start __initdata = 0;
31 static unsigned long phys_initrd_size __initdata = 0;
33 static void __init early_initrd(char **p)
35 unsigned long start, size;
37 start = memparse(*p, p);
39 size = memparse((*p) + 1, p);
41 phys_initrd_start = start;
42 phys_initrd_size = size;
45 __early_param("initrd=", early_initrd);
47 static int __init parse_tag_initrd(const struct tag *tag)
49 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
50 "please update your bootloader.\n");
51 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
52 phys_initrd_size = tag->u.initrd.size;
56 __tagtable(ATAG_INITRD, parse_tag_initrd);
58 static int __init parse_tag_initrd2(const struct tag *tag)
60 phys_initrd_start = tag->u.initrd.start;
61 phys_initrd_size = tag->u.initrd.size;
65 __tagtable(ATAG_INITRD2, parse_tag_initrd2);
68 * This keeps memory configuration data used by a couple memory
69 * initialization functions, as well as show_mem() for the skipping
70 * of holes in the memory map. It is populated by arm_add_memory().
72 struct meminfo meminfo;
76 int free = 0, total = 0, reserved = 0;
77 int shared = 0, cached = 0, slab = 0, node, i;
78 struct meminfo * mi = &meminfo;
80 printk("Mem-info:\n");
82 for_each_online_node(node) {
83 pg_data_t *n = NODE_DATA(node);
84 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
86 for_each_nodebank (i,mi,node) {
87 struct membank *bank = &mi->bank[i];
88 unsigned int pfn1, pfn2;
89 struct page *page, *end;
91 pfn1 = bank_pfn_start(bank);
92 pfn2 = bank_pfn_end(bank);
99 if (PageReserved(page))
101 else if (PageSwapCache(page))
103 else if (PageSlab(page))
105 else if (!page_count(page))
108 shared += page_count(page) - 1;
110 } while (page < end);
114 printk("%d pages of RAM\n", total);
115 printk("%d free pages\n", free);
116 printk("%d reserved pages\n", reserved);
117 printk("%d slab pages\n", slab);
118 printk("%d pages shared\n", shared);
119 printk("%d pages swap cached\n", cached);
123 * FIXME: We really want to avoid allocating the bootmap bitmap
124 * over the top of the initrd. Hopefully, this is located towards
125 * the start of a bank, so if we allocate the bootmap bitmap at
126 * the end, we won't clash.
128 static unsigned int __init
129 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
131 unsigned int start_pfn, i, bootmap_pfn;
133 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
136 for_each_nodebank(i, mi, node) {
137 struct membank *bank = &mi->bank[i];
138 unsigned int start, end;
140 start = bank_pfn_start(bank);
141 end = bank_pfn_end(bank);
146 if (start < start_pfn)
152 if (end - start >= bootmap_pages) {
158 if (bootmap_pfn == 0)
164 static int __init check_initrd(struct meminfo *mi)
166 int initrd_node = -2;
167 #ifdef CONFIG_BLK_DEV_INITRD
168 unsigned long end = phys_initrd_start + phys_initrd_size;
171 * Make sure that the initrd is within a valid area of
174 if (phys_initrd_size) {
179 for (i = 0; i < mi->nr_banks; i++) {
180 struct membank *bank = &mi->bank[i];
181 if (bank_phys_start(bank) <= phys_initrd_start &&
182 end <= bank_phys_end(bank))
183 initrd_node = bank->node;
187 if (initrd_node == -1) {
188 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
189 "physical memory - disabling initrd\n",
190 phys_initrd_start, phys_initrd_size);
191 phys_initrd_start = phys_initrd_size = 0;
198 static inline void map_memory_bank(struct membank *bank)
203 map.pfn = bank_pfn_start(bank);
204 map.virtual = __phys_to_virt(bank_phys_start(bank));
205 map.length = bank_phys_size(bank);
206 map.type = MT_MEMORY;
208 create_mapping(&map);
212 static unsigned long __init bootmem_init_node(int node, struct meminfo *mi)
214 unsigned long start_pfn, end_pfn, boot_pfn;
215 unsigned int boot_pages;
223 * Calculate the pfn range, and map the memory banks for this node.
225 for_each_nodebank(i, mi, node) {
226 struct membank *bank = &mi->bank[i];
227 unsigned long start, end;
229 start = bank_pfn_start(bank);
230 end = bank_pfn_end(bank);
232 if (start_pfn > start)
237 map_memory_bank(bank);
241 * If there is no memory in this node, ignore it.
247 * Allocate the bootmem bitmap page.
249 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
250 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
253 * Initialise the bootmem allocator for this node, handing the
254 * memory banks over to bootmem.
256 node_set_online(node);
257 pgdat = NODE_DATA(node);
258 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
260 for_each_nodebank(i, mi, node) {
261 struct membank *bank = &mi->bank[i];
262 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
263 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
267 * Reserve the bootmem bitmap for this node.
269 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
270 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
275 static void __init bootmem_reserve_initrd(int node)
277 #ifdef CONFIG_BLK_DEV_INITRD
278 pg_data_t *pgdat = NODE_DATA(node);
281 res = reserve_bootmem_node(pgdat, phys_initrd_start,
282 phys_initrd_size, BOOTMEM_EXCLUSIVE);
285 initrd_start = __phys_to_virt(phys_initrd_start);
286 initrd_end = initrd_start + phys_initrd_size;
289 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
290 "memory region - disabling initrd\n",
291 phys_initrd_start, phys_initrd_size);
296 static void __init bootmem_free_node(int node, struct meminfo *mi)
298 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
299 unsigned long start_pfn, end_pfn;
300 pg_data_t *pgdat = NODE_DATA(node);
303 start_pfn = pgdat->bdata->node_min_pfn;
304 end_pfn = pgdat->bdata->node_low_pfn;
307 * initialise the zones within this node.
309 memset(zone_size, 0, sizeof(zone_size));
310 memset(zhole_size, 0, sizeof(zhole_size));
313 * The size of this node has already been determined. If we need
314 * to do anything fancy with the allocation of this memory to the
315 * zones, now is the time to do it.
317 zone_size[0] = end_pfn - start_pfn;
320 * For each bank in this node, calculate the size of the holes.
321 * holes = node_size - sum(bank_sizes_in_node)
323 zhole_size[0] = zone_size[0];
324 for_each_nodebank(i, mi, node)
325 zhole_size[0] -= bank_pfn_size(&mi->bank[i]);
328 * Adjust the sizes according to any special requirements for
331 arch_adjust_zones(node, zone_size, zhole_size);
333 free_area_init_node(node, zone_size, start_pfn, zhole_size);
336 void __init bootmem_init(void)
338 struct meminfo *mi = &meminfo;
339 unsigned long memend_pfn = 0;
340 int node, initrd_node;
343 * Locate which node contains the ramdisk image, if any.
345 initrd_node = check_initrd(mi);
348 * Run through each node initialising the bootmem allocator.
350 for_each_node(node) {
351 unsigned long end_pfn = bootmem_init_node(node, mi);
354 * Reserve any special node zero regions.
357 reserve_node_zero(NODE_DATA(node));
360 * If the initrd is in this node, reserve its memory.
362 if (node == initrd_node)
363 bootmem_reserve_initrd(node);
366 * Remember the highest memory PFN.
368 if (end_pfn > memend_pfn)
369 memend_pfn = end_pfn;
373 * sparse_init() needs the bootmem allocator up and running.
378 * Now free memory in each node - free_area_init_node needs
379 * the sparse mem_map arrays initialized by sparse_init()
380 * for memmap_init_zone(), otherwise all PFNs are invalid.
383 bootmem_free_node(node, mi);
385 high_memory = __va(memend_pfn << PAGE_SHIFT);
388 * This doesn't seem to be used by the Linux memory manager any
389 * more, but is used by ll_rw_block. If we can get rid of it, we
390 * also get rid of some of the stuff above as well.
392 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
393 * the system, not the maximum PFN.
395 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
398 static inline int free_area(unsigned long pfn, unsigned long end, char *s)
400 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
402 for (; pfn < end; pfn++) {
403 struct page *page = pfn_to_page(pfn);
404 ClearPageReserved(page);
405 init_page_count(page);
411 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
417 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
419 struct page *start_pg, *end_pg;
420 unsigned long pg, pgend;
423 * Convert start_pfn/end_pfn to a struct page pointer.
425 start_pg = pfn_to_page(start_pfn);
426 end_pg = pfn_to_page(end_pfn);
429 * Convert to physical addresses, and
430 * round start upwards and end downwards.
432 pg = PAGE_ALIGN(__pa(start_pg));
433 pgend = __pa(end_pg) & PAGE_MASK;
436 * If there are free pages between these,
437 * free the section of the memmap array.
440 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
444 * The mem_map array can get very big. Free the unused area of the memory map.
446 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
448 unsigned long bank_start, prev_bank_end = 0;
452 * [FIXME] This relies on each bank being in address order. This
453 * may not be the case, especially if the user has provided the
454 * information on the command line.
456 for_each_nodebank(i, mi, node) {
457 struct membank *bank = &mi->bank[i];
459 bank_start = bank_pfn_start(bank);
460 if (bank_start < prev_bank_end) {
461 printk(KERN_ERR "MEM: unordered memory banks. "
462 "Not freeing memmap.\n");
467 * If we had a previous bank, and there is a space
468 * between the current bank and the previous, free it.
470 if (prev_bank_end && prev_bank_end != bank_start)
471 free_memmap(node, prev_bank_end, bank_start);
473 prev_bank_end = bank_pfn_end(bank);
478 * mem_init() marks the free areas in the mem_map and tells us how much
479 * memory is free. This is done after various parts of the system have
480 * claimed their memory after the kernel image.
482 void __init mem_init(void)
484 unsigned int codesize, datasize, initsize;
487 #ifndef CONFIG_DISCONTIGMEM
488 max_mapnr = virt_to_page(high_memory) - mem_map;
491 /* this will put all unused low memory onto the freelists */
492 for_each_online_node(node) {
493 pg_data_t *pgdat = NODE_DATA(node);
495 free_unused_memmap_node(node, &meminfo);
497 if (pgdat->node_spanned_pages != 0)
498 totalram_pages += free_all_bootmem_node(pgdat);
502 /* now that our DMA memory is actually so designated, we can free it */
503 totalram_pages += free_area(PHYS_PFN_OFFSET,
504 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
508 * Since our memory may not be contiguous, calculate the
509 * real number of pages we have in this system
511 printk(KERN_INFO "Memory:");
513 for (i = 0; i < meminfo.nr_banks; i++) {
514 num_physpages += bank_pfn_size(&meminfo.bank[i]);
515 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
517 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
519 codesize = _etext - _text;
520 datasize = _end - _data;
521 initsize = __init_end - __init_begin;
523 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
524 "%dK data, %dK init)\n",
525 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
526 codesize >> 10, datasize >> 10, initsize >> 10);
528 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
529 extern int sysctl_overcommit_memory;
531 * On a machine this small we won't get
532 * anywhere without overcommit, so turn
535 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
539 void free_initmem(void)
541 if (!machine_is_integrator() && !machine_is_cintegrator())
542 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
543 __phys_to_pfn(__pa(__init_end)),
547 #ifdef CONFIG_BLK_DEV_INITRD
549 static int keep_initrd;
551 void free_initrd_mem(unsigned long start, unsigned long end)
554 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
555 __phys_to_pfn(__pa(end)),
559 static int __init keepinitrd_setup(char *__unused)
565 __setup("keepinitrd", keepinitrd_setup);