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, };
72 #define for_each_nodebank(iter,mi,no) \
73 for (iter = 0; iter < mi->nr_banks; iter++) \
74 if (mi->bank[iter].node == no)
78 int free = 0, total = 0, reserved = 0;
79 int shared = 0, cached = 0, slab = 0, node, i;
80 struct meminfo * mi = &meminfo;
82 printk("Mem-info:\n");
84 for_each_online_node(node) {
85 pg_data_t *n = NODE_DATA(node);
86 struct page *map = n->node_mem_map - n->node_start_pfn;
88 for_each_nodebank (i,mi,node) {
89 unsigned int pfn1, pfn2;
90 struct page *page, *end;
92 pfn1 = __phys_to_pfn(mi->bank[i].start);
93 pfn2 = __phys_to_pfn(mi->bank[i].size + mi->bank[i].start);
100 if (PageReserved(page))
102 else if (PageSwapCache(page))
104 else if (PageSlab(page))
106 else if (!page_count(page))
109 shared += page_count(page) - 1;
111 } while (page < end);
115 printk("%d pages of RAM\n", total);
116 printk("%d free pages\n", free);
117 printk("%d reserved pages\n", reserved);
118 printk("%d slab pages\n", slab);
119 printk("%d pages shared\n", shared);
120 printk("%d pages swap cached\n", cached);
124 * FIXME: We really want to avoid allocating the bootmap bitmap
125 * over the top of the initrd. Hopefully, this is located towards
126 * the start of a bank, so if we allocate the bootmap bitmap at
127 * the end, we won't clash.
129 static unsigned int __init
130 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
132 unsigned int start_pfn, bank, bootmap_pfn;
134 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
137 for_each_nodebank(bank, mi, node) {
138 unsigned int start, end;
140 start = mi->bank[bank].start >> PAGE_SHIFT;
141 end = (mi->bank[bank].size +
142 mi->bank[bank].start) >> PAGE_SHIFT;
147 if (start < start_pfn)
153 if (end - start >= bootmap_pages) {
159 if (bootmap_pfn == 0)
165 static int __init check_initrd(struct meminfo *mi)
167 int initrd_node = -2;
168 #ifdef CONFIG_BLK_DEV_INITRD
169 unsigned long end = phys_initrd_start + phys_initrd_size;
172 * Make sure that the initrd is within a valid area of
175 if (phys_initrd_size) {
180 for (i = 0; i < mi->nr_banks; i++) {
181 unsigned long bank_end;
183 bank_end = mi->bank[i].start + mi->bank[i].size;
185 if (mi->bank[i].start <= phys_initrd_start &&
187 initrd_node = mi->bank[i].node;
191 if (initrd_node == -1) {
192 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
193 "physical memory - disabling initrd\n",
194 phys_initrd_start, phys_initrd_size);
195 phys_initrd_start = phys_initrd_size = 0;
202 static inline void map_memory_bank(struct membank *bank)
207 map.pfn = __phys_to_pfn(bank->start);
208 map.virtual = __phys_to_virt(bank->start);
209 map.length = bank->size;
210 map.type = MT_MEMORY;
212 create_mapping(&map);
216 static unsigned long __init
217 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
219 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
220 unsigned long start_pfn, end_pfn, boot_pfn;
221 unsigned int boot_pages;
229 * Calculate the pfn range, and map the memory banks for this node.
231 for_each_nodebank(i, mi, node) {
232 struct membank *bank = &mi->bank[i];
233 unsigned long start, end;
235 start = bank->start >> PAGE_SHIFT;
236 end = (bank->start + bank->size) >> PAGE_SHIFT;
238 if (start_pfn > start)
243 map_memory_bank(bank);
247 * If there is no memory in this node, ignore it.
253 * Allocate the bootmem bitmap page.
255 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
256 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
259 * Initialise the bootmem allocator for this node, handing the
260 * memory banks over to bootmem.
262 node_set_online(node);
263 pgdat = NODE_DATA(node);
264 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
266 for_each_nodebank(i, mi, node)
267 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
270 * Reserve the bootmem bitmap for this node.
272 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
273 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
276 * Reserve any special node zero regions.
279 reserve_node_zero(pgdat);
281 #ifdef CONFIG_BLK_DEV_INITRD
283 * If the initrd is in this node, reserve its memory.
285 if (node == initrd_node) {
286 int res = reserve_bootmem_node(pgdat, phys_initrd_start,
287 phys_initrd_size, BOOTMEM_EXCLUSIVE);
290 initrd_start = __phys_to_virt(phys_initrd_start);
291 initrd_end = initrd_start + phys_initrd_size;
294 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
295 "memory region - disabling initrd\n",
296 phys_initrd_start, phys_initrd_size);
302 * initialise the zones within this node.
304 memset(zone_size, 0, sizeof(zone_size));
305 memset(zhole_size, 0, sizeof(zhole_size));
308 * The size of this node has already been determined. If we need
309 * to do anything fancy with the allocation of this memory to the
310 * zones, now is the time to do it.
312 zone_size[0] = end_pfn - start_pfn;
315 * For each bank in this node, calculate the size of the holes.
316 * holes = node_size - sum(bank_sizes_in_node)
318 zhole_size[0] = zone_size[0];
319 for_each_nodebank(i, mi, node)
320 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
323 * Adjust the sizes according to any special requirements for
326 arch_adjust_zones(node, zone_size, zhole_size);
328 free_area_init_node(node, zone_size, start_pfn, zhole_size);
333 void __init bootmem_init(struct meminfo *mi)
335 unsigned long memend_pfn = 0;
336 int node, initrd_node;
338 memcpy(&meminfo, mi, sizeof(meminfo));
341 * Locate which node contains the ramdisk image, if any.
343 initrd_node = check_initrd(mi);
346 * Run through each node initialising the bootmem allocator.
348 for_each_node(node) {
349 unsigned long end_pfn;
351 end_pfn = bootmem_init_node(node, initrd_node, mi);
354 * Remember the highest memory PFN.
356 if (end_pfn > memend_pfn)
357 memend_pfn = end_pfn;
360 high_memory = __va(memend_pfn << PAGE_SHIFT);
363 * This doesn't seem to be used by the Linux memory manager any
364 * more, but is used by ll_rw_block. If we can get rid of it, we
365 * also get rid of some of the stuff above as well.
367 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
368 * the system, not the maximum PFN.
370 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
373 static inline void free_area(unsigned long addr, unsigned long end, char *s)
375 unsigned int size = (end - addr) >> 10;
377 for (; addr < end; addr += PAGE_SIZE) {
378 struct page *page = virt_to_page(addr);
379 ClearPageReserved(page);
380 init_page_count(page);
386 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
390 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
392 struct page *start_pg, *end_pg;
393 unsigned long pg, pgend;
396 * Convert start_pfn/end_pfn to a struct page pointer.
398 start_pg = pfn_to_page(start_pfn);
399 end_pg = pfn_to_page(end_pfn);
402 * Convert to physical addresses, and
403 * round start upwards and end downwards.
405 pg = PAGE_ALIGN(__pa(start_pg));
406 pgend = __pa(end_pg) & PAGE_MASK;
409 * If there are free pages between these,
410 * free the section of the memmap array.
413 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
417 * The mem_map array can get very big. Free the unused area of the memory map.
419 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
421 unsigned long bank_start, prev_bank_end = 0;
425 * [FIXME] This relies on each bank being in address order. This
426 * may not be the case, especially if the user has provided the
427 * information on the command line.
429 for_each_nodebank(i, mi, node) {
430 bank_start = mi->bank[i].start >> PAGE_SHIFT;
431 if (bank_start < prev_bank_end) {
432 printk(KERN_ERR "MEM: unordered memory banks. "
433 "Not freeing memmap.\n");
438 * If we had a previous bank, and there is a space
439 * between the current bank and the previous, free it.
441 if (prev_bank_end && prev_bank_end != bank_start)
442 free_memmap(node, prev_bank_end, bank_start);
444 prev_bank_end = (mi->bank[i].start +
445 mi->bank[i].size) >> PAGE_SHIFT;
450 * mem_init() marks the free areas in the mem_map and tells us how much
451 * memory is free. This is done after various parts of the system have
452 * claimed their memory after the kernel image.
454 void __init mem_init(void)
456 unsigned int codepages, datapages, initpages;
459 codepages = &_etext - &_text;
460 datapages = &_end - &__data_start;
461 initpages = &__init_end - &__init_begin;
463 #ifndef CONFIG_DISCONTIGMEM
464 max_mapnr = virt_to_page(high_memory) - mem_map;
467 /* this will put all unused low memory onto the freelists */
468 for_each_online_node(node) {
469 pg_data_t *pgdat = NODE_DATA(node);
471 free_unused_memmap_node(node, &meminfo);
473 if (pgdat->node_spanned_pages != 0)
474 totalram_pages += free_all_bootmem_node(pgdat);
478 /* now that our DMA memory is actually so designated, we can free it */
479 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
483 * Since our memory may not be contiguous, calculate the
484 * real number of pages we have in this system
486 printk(KERN_INFO "Memory:");
489 for (i = 0; i < meminfo.nr_banks; i++) {
490 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
491 printk(" %ldMB", meminfo.bank[i].size >> 20);
494 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
495 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
496 "%dK data, %dK init)\n",
497 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
498 codepages >> 10, datapages >> 10, initpages >> 10);
500 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
501 extern int sysctl_overcommit_memory;
503 * On a machine this small we won't get
504 * anywhere without overcommit, so turn
507 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
511 void free_initmem(void)
513 if (!machine_is_integrator() && !machine_is_cintegrator()) {
514 free_area((unsigned long)(&__init_begin),
515 (unsigned long)(&__init_end),
520 #ifdef CONFIG_BLK_DEV_INITRD
522 static int keep_initrd;
524 void free_initrd_mem(unsigned long start, unsigned long end)
527 free_area(start, end, "initrd");
530 static int __init keepinitrd_setup(char *__unused)
536 __setup("keepinitrd", keepinitrd_setup);