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/setup.h>
21 #include <asm/sizes.h>
24 #include <asm/mach/arch.h>
25 #include <asm/mach/map.h>
29 static unsigned long phys_initrd_start __initdata = 0;
30 static unsigned long phys_initrd_size __initdata = 0;
32 static void __init early_initrd(char **p)
34 unsigned long start, size;
36 start = memparse(*p, p);
38 size = memparse((*p) + 1, p);
40 phys_initrd_start = start;
41 phys_initrd_size = size;
44 __early_param("initrd=", early_initrd);
46 static int __init parse_tag_initrd(const struct tag *tag)
48 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
49 "please update your bootloader.\n");
50 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
51 phys_initrd_size = tag->u.initrd.size;
55 __tagtable(ATAG_INITRD, parse_tag_initrd);
57 static int __init parse_tag_initrd2(const struct tag *tag)
59 phys_initrd_start = tag->u.initrd.start;
60 phys_initrd_size = tag->u.initrd.size;
64 __tagtable(ATAG_INITRD2, parse_tag_initrd2);
67 * This is used to pass memory configuration data from paging_init
68 * to mem_init, and by show_mem() to skip holes in the memory map.
70 static struct meminfo meminfo = { 0, };
74 int free = 0, total = 0, reserved = 0;
75 int shared = 0, cached = 0, slab = 0, node, i;
76 struct meminfo * mi = &meminfo;
78 printk("Mem-info:\n");
80 for_each_online_node(node) {
81 pg_data_t *n = NODE_DATA(node);
82 struct page *map = n->node_mem_map - n->node_start_pfn;
84 for_each_nodebank (i,mi,node) {
85 struct membank *bank = &mi->bank[i];
86 unsigned int pfn1, pfn2;
87 struct page *page, *end;
89 pfn1 = bank_pfn_start(bank);
90 pfn2 = bank_pfn_end(bank);
97 if (PageReserved(page))
99 else if (PageSwapCache(page))
101 else if (PageSlab(page))
103 else if (!page_count(page))
106 shared += page_count(page) - 1;
108 } while (page < end);
112 printk("%d pages of RAM\n", total);
113 printk("%d free pages\n", free);
114 printk("%d reserved pages\n", reserved);
115 printk("%d slab pages\n", slab);
116 printk("%d pages shared\n", shared);
117 printk("%d pages swap cached\n", cached);
121 * FIXME: We really want to avoid allocating the bootmap bitmap
122 * over the top of the initrd. Hopefully, this is located towards
123 * the start of a bank, so if we allocate the bootmap bitmap at
124 * the end, we won't clash.
126 static unsigned int __init
127 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
129 unsigned int start_pfn, i, bootmap_pfn;
131 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
134 for_each_nodebank(i, mi, node) {
135 struct membank *bank = &mi->bank[i];
136 unsigned int start, end;
138 start = bank_pfn_start(bank);
139 end = bank_pfn_end(bank);
144 if (start < start_pfn)
150 if (end - start >= bootmap_pages) {
156 if (bootmap_pfn == 0)
162 static int __init check_initrd(struct meminfo *mi)
164 int initrd_node = -2;
165 #ifdef CONFIG_BLK_DEV_INITRD
166 unsigned long end = phys_initrd_start + phys_initrd_size;
169 * Make sure that the initrd is within a valid area of
172 if (phys_initrd_size) {
177 for (i = 0; i < mi->nr_banks; i++) {
178 struct membank *bank = &mi->bank[i];
179 if (bank_phys_start(bank) <= phys_initrd_start &&
180 end <= bank_phys_end(bank))
181 initrd_node = bank->node;
185 if (initrd_node == -1) {
186 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
187 "physical memory - disabling initrd\n",
188 phys_initrd_start, phys_initrd_size);
189 phys_initrd_start = phys_initrd_size = 0;
196 static inline void map_memory_bank(struct membank *bank)
201 map.pfn = bank_pfn_start(bank);
202 map.virtual = __phys_to_virt(bank_phys_start(bank));
203 map.length = bank_phys_size(bank);
204 map.type = MT_MEMORY;
206 create_mapping(&map);
210 static unsigned long __init
211 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
213 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
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));
266 * Reserve the bootmem bitmap for this node.
268 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
269 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
272 * Reserve any special node zero regions.
275 reserve_node_zero(pgdat);
277 #ifdef CONFIG_BLK_DEV_INITRD
279 * If the initrd is in this node, reserve its memory.
281 if (node == initrd_node) {
282 int res = reserve_bootmem_node(pgdat, phys_initrd_start,
283 phys_initrd_size, BOOTMEM_EXCLUSIVE);
286 initrd_start = __phys_to_virt(phys_initrd_start);
287 initrd_end = initrd_start + phys_initrd_size;
290 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
291 "memory region - disabling initrd\n",
292 phys_initrd_start, phys_initrd_size);
298 * initialise the zones within this node.
300 memset(zone_size, 0, sizeof(zone_size));
301 memset(zhole_size, 0, sizeof(zhole_size));
304 * The size of this node has already been determined. If we need
305 * to do anything fancy with the allocation of this memory to the
306 * zones, now is the time to do it.
308 zone_size[0] = end_pfn - start_pfn;
311 * For each bank in this node, calculate the size of the holes.
312 * holes = node_size - sum(bank_sizes_in_node)
314 zhole_size[0] = zone_size[0];
315 for_each_nodebank(i, mi, node)
316 zhole_size[0] -= bank_pfn_size(&mi->bank[i]);
319 * Adjust the sizes according to any special requirements for
322 arch_adjust_zones(node, zone_size, zhole_size);
324 free_area_init_node(node, zone_size, start_pfn, zhole_size);
329 void __init bootmem_init(struct meminfo *mi)
331 unsigned long memend_pfn = 0;
332 int node, initrd_node;
334 memcpy(&meminfo, mi, sizeof(meminfo));
337 * Locate which node contains the ramdisk image, if any.
339 initrd_node = check_initrd(mi);
342 * Run through each node initialising the bootmem allocator.
344 for_each_node(node) {
345 unsigned long end_pfn;
347 end_pfn = bootmem_init_node(node, initrd_node, mi);
350 * Remember the highest memory PFN.
352 if (end_pfn > memend_pfn)
353 memend_pfn = end_pfn;
356 high_memory = __va(memend_pfn << PAGE_SHIFT);
359 * This doesn't seem to be used by the Linux memory manager any
360 * more, but is used by ll_rw_block. If we can get rid of it, we
361 * also get rid of some of the stuff above as well.
363 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
364 * the system, not the maximum PFN.
366 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
369 static inline void free_area(unsigned long addr, unsigned long end, char *s)
371 unsigned int size = (end - addr) >> 10;
373 for (; addr < end; addr += PAGE_SIZE) {
374 struct page *page = virt_to_page(addr);
375 ClearPageReserved(page);
376 init_page_count(page);
382 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
386 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
388 struct page *start_pg, *end_pg;
389 unsigned long pg, pgend;
392 * Convert start_pfn/end_pfn to a struct page pointer.
394 start_pg = pfn_to_page(start_pfn);
395 end_pg = pfn_to_page(end_pfn);
398 * Convert to physical addresses, and
399 * round start upwards and end downwards.
401 pg = PAGE_ALIGN(__pa(start_pg));
402 pgend = __pa(end_pg) & PAGE_MASK;
405 * If there are free pages between these,
406 * free the section of the memmap array.
409 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
413 * The mem_map array can get very big. Free the unused area of the memory map.
415 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
417 unsigned long bank_start, prev_bank_end = 0;
421 * [FIXME] This relies on each bank being in address order. This
422 * may not be the case, especially if the user has provided the
423 * information on the command line.
425 for_each_nodebank(i, mi, node) {
426 struct membank *bank = &mi->bank[i];
428 bank_start = bank_pfn_start(bank);
429 if (bank_start < prev_bank_end) {
430 printk(KERN_ERR "MEM: unordered memory banks. "
431 "Not freeing memmap.\n");
436 * If we had a previous bank, and there is a space
437 * between the current bank and the previous, free it.
439 if (prev_bank_end && prev_bank_end != bank_start)
440 free_memmap(node, prev_bank_end, bank_start);
442 prev_bank_end = bank_pfn_end(bank);
447 * mem_init() marks the free areas in the mem_map and tells us how much
448 * memory is free. This is done after various parts of the system have
449 * claimed their memory after the kernel image.
451 void __init mem_init(void)
453 unsigned int codepages, datapages, initpages;
456 codepages = &_etext - &_text;
457 datapages = &_end - &__data_start;
458 initpages = &__init_end - &__init_begin;
460 #ifndef CONFIG_DISCONTIGMEM
461 max_mapnr = virt_to_page(high_memory) - mem_map;
464 /* this will put all unused low memory onto the freelists */
465 for_each_online_node(node) {
466 pg_data_t *pgdat = NODE_DATA(node);
468 free_unused_memmap_node(node, &meminfo);
470 if (pgdat->node_spanned_pages != 0)
471 totalram_pages += free_all_bootmem_node(pgdat);
475 /* now that our DMA memory is actually so designated, we can free it */
476 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
480 * Since our memory may not be contiguous, calculate the
481 * real number of pages we have in this system
483 printk(KERN_INFO "Memory:");
486 for (i = 0; i < meminfo.nr_banks; i++) {
487 num_physpages += bank_pfn_size(&meminfo.bank[i]);
488 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
491 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
492 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
493 "%dK data, %dK init)\n",
494 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
495 codepages >> 10, datapages >> 10, initpages >> 10);
497 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
498 extern int sysctl_overcommit_memory;
500 * On a machine this small we won't get
501 * anywhere without overcommit, so turn
504 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
508 void free_initmem(void)
510 if (!machine_is_integrator() && !machine_is_cintegrator()) {
511 free_area((unsigned long)(&__init_begin),
512 (unsigned long)(&__init_end),
517 #ifdef CONFIG_BLK_DEV_INITRD
519 static int keep_initrd;
521 void free_initrd_mem(unsigned long start, unsigned long end)
524 free_area(start, end, "initrd");
527 static int __init keepinitrd_setup(char *__unused)
533 __setup("keepinitrd", keepinitrd_setup);