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/highmem.h>
20 #include <asm/mach-types.h>
21 #include <asm/sections.h>
22 #include <asm/setup.h>
23 #include <asm/sizes.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach/map.h>
31 static unsigned long phys_initrd_start __initdata = 0;
32 static unsigned long phys_initrd_size __initdata = 0;
34 static void __init early_initrd(char **p)
36 unsigned long start, size;
38 start = memparse(*p, p);
40 size = memparse((*p) + 1, p);
42 phys_initrd_start = start;
43 phys_initrd_size = size;
46 __early_param("initrd=", early_initrd);
48 static int __init parse_tag_initrd(const struct tag *tag)
50 printk(KERN_WARNING "ATAG_INITRD is deprecated; "
51 "please update your bootloader.\n");
52 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
53 phys_initrd_size = tag->u.initrd.size;
57 __tagtable(ATAG_INITRD, parse_tag_initrd);
59 static int __init parse_tag_initrd2(const struct tag *tag)
61 phys_initrd_start = tag->u.initrd.start;
62 phys_initrd_size = tag->u.initrd.size;
66 __tagtable(ATAG_INITRD2, parse_tag_initrd2);
69 * This keeps memory configuration data used by a couple memory
70 * initialization functions, as well as show_mem() for the skipping
71 * of holes in the memory map. It is populated by arm_add_memory().
73 struct meminfo meminfo;
77 int free = 0, total = 0, reserved = 0;
78 int shared = 0, cached = 0, slab = 0, node, i;
79 struct meminfo * mi = &meminfo;
81 printk("Mem-info:\n");
83 for_each_online_node(node) {
84 pg_data_t *n = NODE_DATA(node);
85 struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
87 for_each_nodebank (i,mi,node) {
88 struct membank *bank = &mi->bank[i];
89 unsigned int pfn1, pfn2;
90 struct page *page, *end;
92 pfn1 = bank_pfn_start(bank);
93 pfn2 = bank_pfn_end(bank);
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, i, bootmap_pfn;
134 start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
137 for_each_nodebank(i, mi, node) {
138 struct membank *bank = &mi->bank[i];
139 unsigned int start, end;
141 start = bank_pfn_start(bank);
142 end = bank_pfn_end(bank);
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 struct membank *bank = &mi->bank[i];
182 if (bank_phys_start(bank) <= phys_initrd_start &&
183 end <= bank_phys_end(bank))
184 initrd_node = bank->node;
188 if (initrd_node == -1) {
189 printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
190 "physical memory - disabling initrd\n",
191 phys_initrd_start, phys_initrd_size);
192 phys_initrd_start = phys_initrd_size = 0;
199 static inline void map_memory_bank(struct membank *bank)
204 map.pfn = bank_pfn_start(bank);
205 map.virtual = __phys_to_virt(bank_phys_start(bank));
206 map.length = bank_phys_size(bank);
207 map.type = MT_MEMORY;
209 create_mapping(&map);
213 static unsigned long __init bootmem_init_node(int node, struct meminfo *mi)
215 unsigned long start_pfn, end_pfn, boot_pfn;
216 unsigned int boot_pages;
224 * Calculate the pfn range, and map the memory banks for this node.
226 for_each_nodebank(i, mi, node) {
227 struct membank *bank = &mi->bank[i];
228 unsigned long start, end;
230 start = bank_pfn_start(bank);
231 end = bank_pfn_end(bank);
233 if (start_pfn > start)
238 map_memory_bank(bank);
242 * If there is no memory in this node, ignore it.
248 * Allocate the bootmem bitmap page.
250 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
251 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
254 * Initialise the bootmem allocator for this node, handing the
255 * memory banks over to bootmem.
257 node_set_online(node);
258 pgdat = NODE_DATA(node);
259 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
261 for_each_nodebank(i, mi, node) {
262 struct membank *bank = &mi->bank[i];
263 free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
264 memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
268 * Reserve the bootmem bitmap for this node.
270 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
271 boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
276 static void __init bootmem_reserve_initrd(int node)
278 #ifdef CONFIG_BLK_DEV_INITRD
279 pg_data_t *pgdat = NODE_DATA(node);
282 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);
297 static void __init bootmem_free_node(int node, struct meminfo *mi)
299 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
300 unsigned long start_pfn, end_pfn;
301 pg_data_t *pgdat = NODE_DATA(node);
304 start_pfn = pgdat->bdata->node_min_pfn;
305 end_pfn = pgdat->bdata->node_low_pfn;
308 * initialise the zones within this node.
310 memset(zone_size, 0, sizeof(zone_size));
311 memset(zhole_size, 0, sizeof(zhole_size));
314 * The size of this node has already been determined. If we need
315 * to do anything fancy with the allocation of this memory to the
316 * zones, now is the time to do it.
318 zone_size[0] = end_pfn - start_pfn;
321 * For each bank in this node, calculate the size of the holes.
322 * holes = node_size - sum(bank_sizes_in_node)
324 zhole_size[0] = zone_size[0];
325 for_each_nodebank(i, mi, node)
326 zhole_size[0] -= bank_pfn_size(&mi->bank[i]);
329 * Adjust the sizes according to any special requirements for
332 arch_adjust_zones(node, zone_size, zhole_size);
334 free_area_init_node(node, zone_size, start_pfn, zhole_size);
337 void __init bootmem_init(void)
339 struct meminfo *mi = &meminfo;
340 unsigned long memend_pfn = 0;
341 int node, initrd_node;
344 * Locate which node contains the ramdisk image, if any.
346 initrd_node = check_initrd(mi);
349 * Run through each node initialising the bootmem allocator.
351 for_each_node(node) {
352 unsigned long end_pfn = bootmem_init_node(node, mi);
355 * Reserve any special node zero regions.
358 reserve_node_zero(NODE_DATA(node));
361 * If the initrd is in this node, reserve its memory.
363 if (node == initrd_node)
364 bootmem_reserve_initrd(node);
367 * Remember the highest memory PFN.
369 if (end_pfn > memend_pfn)
370 memend_pfn = end_pfn;
374 * sparse_init() needs the bootmem allocator up and running.
379 * Now free memory in each node - free_area_init_node needs
380 * the sparse mem_map arrays initialized by sparse_init()
381 * for memmap_init_zone(), otherwise all PFNs are invalid.
384 bootmem_free_node(node, mi);
386 high_memory = __va((memend_pfn << PAGE_SHIFT) - 1) + 1;
389 * This doesn't seem to be used by the Linux memory manager any
390 * more, but is used by ll_rw_block. If we can get rid of it, we
391 * also get rid of some of the stuff above as well.
393 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
394 * the system, not the maximum PFN.
396 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
399 static inline int free_area(unsigned long pfn, unsigned long end, char *s)
401 unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
403 for (; pfn < end; pfn++) {
404 struct page *page = pfn_to_page(pfn);
405 ClearPageReserved(page);
406 init_page_count(page);
412 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
418 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
420 struct page *start_pg, *end_pg;
421 unsigned long pg, pgend;
424 * Convert start_pfn/end_pfn to a struct page pointer.
426 start_pg = pfn_to_page(start_pfn);
427 end_pg = pfn_to_page(end_pfn);
430 * Convert to physical addresses, and
431 * round start upwards and end downwards.
433 pg = PAGE_ALIGN(__pa(start_pg));
434 pgend = __pa(end_pg) & PAGE_MASK;
437 * If there are free pages between these,
438 * free the section of the memmap array.
441 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
445 * The mem_map array can get very big. Free the unused area of the memory map.
447 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
449 unsigned long bank_start, prev_bank_end = 0;
453 * [FIXME] This relies on each bank being in address order. This
454 * may not be the case, especially if the user has provided the
455 * information on the command line.
457 for_each_nodebank(i, mi, node) {
458 struct membank *bank = &mi->bank[i];
460 bank_start = bank_pfn_start(bank);
461 if (bank_start < prev_bank_end) {
462 printk(KERN_ERR "MEM: unordered memory banks. "
463 "Not freeing memmap.\n");
468 * If we had a previous bank, and there is a space
469 * between the current bank and the previous, free it.
471 if (prev_bank_end && prev_bank_end != bank_start)
472 free_memmap(node, prev_bank_end, bank_start);
474 prev_bank_end = bank_pfn_end(bank);
479 * mem_init() marks the free areas in the mem_map and tells us how much
480 * memory is free. This is done after various parts of the system have
481 * claimed their memory after the kernel image.
483 void __init mem_init(void)
485 unsigned int codesize, datasize, initsize;
488 #ifndef CONFIG_DISCONTIGMEM
489 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
492 /* this will put all unused low memory onto the freelists */
493 for_each_online_node(node) {
494 pg_data_t *pgdat = NODE_DATA(node);
496 free_unused_memmap_node(node, &meminfo);
498 if (pgdat->node_spanned_pages != 0)
499 totalram_pages += free_all_bootmem_node(pgdat);
503 /* now that our DMA memory is actually so designated, we can free it */
504 totalram_pages += free_area(PHYS_PFN_OFFSET,
505 __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
508 #ifdef CONFIG_HIGHMEM
509 /* set highmem page free */
510 for_each_online_node(node) {
511 for_each_nodebank (i, &meminfo, node) {
512 unsigned long start = bank_pfn_start(&meminfo.bank[i]);
513 unsigned long end = bank_pfn_end(&meminfo.bank[i]);
514 if (start >= max_low_pfn + PHYS_PFN_OFFSET)
515 totalhigh_pages += free_area(start, end, NULL);
518 totalram_pages += totalhigh_pages;
522 * Since our memory may not be contiguous, calculate the
523 * real number of pages we have in this system
525 printk(KERN_INFO "Memory:");
527 for (i = 0; i < meminfo.nr_banks; i++) {
528 num_physpages += bank_pfn_size(&meminfo.bank[i]);
529 printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
531 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
533 codesize = _etext - _text;
534 datasize = _end - _data;
535 initsize = __init_end - __init_begin;
537 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
538 "%dK data, %dK init, %luK highmem)\n",
539 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
540 codesize >> 10, datasize >> 10, initsize >> 10,
541 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
543 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
544 extern int sysctl_overcommit_memory;
546 * On a machine this small we won't get
547 * anywhere without overcommit, so turn
550 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
554 void free_initmem(void)
556 if (!machine_is_integrator() && !machine_is_cintegrator())
557 totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
558 __phys_to_pfn(__pa(__init_end)),
562 #ifdef CONFIG_BLK_DEV_INITRD
564 static int keep_initrd;
566 void free_initrd_mem(unsigned long start, unsigned long end)
569 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
570 __phys_to_pfn(__pa(end)),
574 static int __init keepinitrd_setup(char *__unused)
580 __setup("keepinitrd", keepinitrd_setup);