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/ptrace.h>
13 #include <linux/swap.h>
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/mman.h>
17 #include <linux/nodemask.h>
18 #include <linux/initrd.h>
20 #include <asm/mach-types.h>
21 #include <asm/setup.h>
22 #include <asm/sizes.h>
25 #include <asm/mach/arch.h>
26 #include <asm/mach/map.h>
30 extern void _text, _etext, __data_start, _end, __init_begin, __init_end;
31 extern unsigned long phys_initrd_start;
32 extern unsigned long phys_initrd_size;
35 * The sole use of this is to pass memory configuration
36 * data from paging_init to mem_init.
38 static struct meminfo meminfo __initdata = { 0, };
42 int free = 0, total = 0, reserved = 0;
43 int shared = 0, cached = 0, slab = 0, node;
45 printk("Mem-info:\n");
47 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
49 for_each_online_node(node) {
50 struct page *page, *end;
52 page = NODE_MEM_MAP(node);
53 end = page + NODE_DATA(node)->node_spanned_pages;
57 if (PageReserved(page))
59 else if (PageSwapCache(page))
61 else if (PageSlab(page))
63 else if (!page_count(page))
66 shared += page_count(page) - 1;
71 printk("%d pages of RAM\n", total);
72 printk("%d free pages\n", free);
73 printk("%d reserved pages\n", reserved);
74 printk("%d slab pages\n", slab);
75 printk("%d pages shared\n", shared);
76 printk("%d pages swap cached\n", cached);
79 #define for_each_nodebank(iter,mi,no) \
80 for (iter = 0; iter < mi->nr_banks; iter++) \
81 if (mi->bank[iter].node == no)
84 * FIXME: We really want to avoid allocating the bootmap bitmap
85 * over the top of the initrd. Hopefully, this is located towards
86 * the start of a bank, so if we allocate the bootmap bitmap at
87 * the end, we won't clash.
89 static unsigned int __init
90 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
92 unsigned int start_pfn, bank, bootmap_pfn;
94 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
97 for_each_nodebank(bank, mi, node) {
98 unsigned int start, end;
100 start = mi->bank[bank].start >> PAGE_SHIFT;
101 end = (mi->bank[bank].size +
102 mi->bank[bank].start) >> PAGE_SHIFT;
107 if (start < start_pfn)
113 if (end - start >= bootmap_pages) {
119 if (bootmap_pfn == 0)
125 static int __init check_initrd(struct meminfo *mi)
127 int initrd_node = -2;
128 #ifdef CONFIG_BLK_DEV_INITRD
129 unsigned long end = phys_initrd_start + phys_initrd_size;
132 * Make sure that the initrd is within a valid area of
135 if (phys_initrd_size) {
140 for (i = 0; i < mi->nr_banks; i++) {
141 unsigned long bank_end;
143 bank_end = mi->bank[i].start + mi->bank[i].size;
145 if (mi->bank[i].start <= phys_initrd_start &&
147 initrd_node = mi->bank[i].node;
151 if (initrd_node == -1) {
152 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
153 "physical memory - disabling initrd\n",
154 phys_initrd_start, end);
155 phys_initrd_start = phys_initrd_size = 0;
162 static inline void map_memory_bank(struct membank *bank)
167 map.pfn = __phys_to_pfn(bank->start);
168 map.virtual = __phys_to_virt(bank->start);
169 map.length = bank->size;
170 map.type = MT_MEMORY;
172 create_mapping(&map);
176 static unsigned long __init
177 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
179 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
180 unsigned long start_pfn, end_pfn, boot_pfn;
181 unsigned int boot_pages;
189 * Calculate the pfn range, and map the memory banks for this node.
191 for_each_nodebank(i, mi, node) {
192 struct membank *bank = &mi->bank[i];
193 unsigned long start, end;
195 start = bank->start >> PAGE_SHIFT;
196 end = (bank->start + bank->size) >> PAGE_SHIFT;
198 if (start_pfn > start)
203 map_memory_bank(bank);
207 * If there is no memory in this node, ignore it.
213 * Allocate the bootmem bitmap page.
215 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
216 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
219 * Initialise the bootmem allocator for this node, handing the
220 * memory banks over to bootmem.
222 node_set_online(node);
223 pgdat = NODE_DATA(node);
224 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
226 for_each_nodebank(i, mi, node)
227 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
230 * Reserve the bootmem bitmap for this node.
232 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
233 boot_pages << PAGE_SHIFT);
235 #ifdef CONFIG_BLK_DEV_INITRD
237 * If the initrd is in this node, reserve its memory.
239 if (node == initrd_node) {
240 reserve_bootmem_node(pgdat, phys_initrd_start,
242 initrd_start = __phys_to_virt(phys_initrd_start);
243 initrd_end = initrd_start + phys_initrd_size;
248 * Finally, reserve any node zero regions.
251 reserve_node_zero(pgdat);
254 * initialise the zones within this node.
256 memset(zone_size, 0, sizeof(zone_size));
257 memset(zhole_size, 0, sizeof(zhole_size));
260 * The size of this node has already been determined. If we need
261 * to do anything fancy with the allocation of this memory to the
262 * zones, now is the time to do it.
264 zone_size[0] = end_pfn - start_pfn;
267 * For each bank in this node, calculate the size of the holes.
268 * holes = node_size - sum(bank_sizes_in_node)
270 zhole_size[0] = zone_size[0];
271 for_each_nodebank(i, mi, node)
272 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
275 * Adjust the sizes according to any special requirements for
278 arch_adjust_zones(node, zone_size, zhole_size);
280 free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
285 void __init bootmem_init(struct meminfo *mi)
287 unsigned long memend_pfn = 0;
288 int node, initrd_node, i;
291 * Invalidate the node number for empty or invalid memory banks
293 for (i = 0; i < mi->nr_banks; i++)
294 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
295 mi->bank[i].node = -1;
297 memcpy(&meminfo, mi, sizeof(meminfo));
300 * Locate which node contains the ramdisk image, if any.
302 initrd_node = check_initrd(mi);
305 * Run through each node initialising the bootmem allocator.
307 for_each_node(node) {
308 unsigned long end_pfn;
310 end_pfn = bootmem_init_node(node, initrd_node, mi);
313 * Remember the highest memory PFN.
315 if (end_pfn > memend_pfn)
316 memend_pfn = end_pfn;
319 high_memory = __va(memend_pfn << PAGE_SHIFT);
322 * This doesn't seem to be used by the Linux memory manager any
323 * more, but is used by ll_rw_block. If we can get rid of it, we
324 * also get rid of some of the stuff above as well.
326 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
327 * the system, not the maximum PFN.
329 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
332 static inline void free_area(unsigned long addr, unsigned long end, char *s)
334 unsigned int size = (end - addr) >> 10;
336 for (; addr < end; addr += PAGE_SIZE) {
337 struct page *page = virt_to_page(addr);
338 ClearPageReserved(page);
339 init_page_count(page);
345 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
349 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
351 struct page *start_pg, *end_pg;
352 unsigned long pg, pgend;
355 * Convert start_pfn/end_pfn to a struct page pointer.
357 start_pg = pfn_to_page(start_pfn);
358 end_pg = pfn_to_page(end_pfn);
361 * Convert to physical addresses, and
362 * round start upwards and end downwards.
364 pg = PAGE_ALIGN(__pa(start_pg));
365 pgend = __pa(end_pg) & PAGE_MASK;
368 * If there are free pages between these,
369 * free the section of the memmap array.
372 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
376 * The mem_map array can get very big. Free the unused area of the memory map.
378 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
380 unsigned long bank_start, prev_bank_end = 0;
384 * [FIXME] This relies on each bank being in address order. This
385 * may not be the case, especially if the user has provided the
386 * information on the command line.
388 for_each_nodebank(i, mi, node) {
389 bank_start = mi->bank[i].start >> PAGE_SHIFT;
390 if (bank_start < prev_bank_end) {
391 printk(KERN_ERR "MEM: unordered memory banks. "
392 "Not freeing memmap.\n");
397 * If we had a previous bank, and there is a space
398 * between the current bank and the previous, free it.
400 if (prev_bank_end && prev_bank_end != bank_start)
401 free_memmap(node, prev_bank_end, bank_start);
403 prev_bank_end = (mi->bank[i].start +
404 mi->bank[i].size) >> PAGE_SHIFT;
409 * mem_init() marks the free areas in the mem_map and tells us how much
410 * memory is free. This is done after various parts of the system have
411 * claimed their memory after the kernel image.
413 void __init mem_init(void)
415 unsigned int codepages, datapages, initpages;
418 codepages = &_etext - &_text;
419 datapages = &_end - &__data_start;
420 initpages = &__init_end - &__init_begin;
422 #ifndef CONFIG_DISCONTIGMEM
423 max_mapnr = virt_to_page(high_memory) - mem_map;
426 /* this will put all unused low memory onto the freelists */
427 for_each_online_node(node) {
428 pg_data_t *pgdat = NODE_DATA(node);
430 free_unused_memmap_node(node, &meminfo);
432 if (pgdat->node_spanned_pages != 0)
433 totalram_pages += free_all_bootmem_node(pgdat);
437 /* now that our DMA memory is actually so designated, we can free it */
438 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
442 * Since our memory may not be contiguous, calculate the
443 * real number of pages we have in this system
445 printk(KERN_INFO "Memory:");
448 for (i = 0; i < meminfo.nr_banks; i++) {
449 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
450 printk(" %ldMB", meminfo.bank[i].size >> 20);
453 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
454 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
455 "%dK data, %dK init)\n",
456 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
457 codepages >> 10, datapages >> 10, initpages >> 10);
459 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
460 extern int sysctl_overcommit_memory;
462 * On a machine this small we won't get
463 * anywhere without overcommit, so turn
466 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
470 void free_initmem(void)
472 if (!machine_is_integrator() && !machine_is_cintegrator()) {
473 free_area((unsigned long)(&__init_begin),
474 (unsigned long)(&__init_end),
479 #ifdef CONFIG_BLK_DEV_INITRD
481 static int keep_initrd;
483 void free_initrd_mem(unsigned long start, unsigned long end)
486 free_area(start, end, "initrd");
489 static int __init keepinitrd_setup(char *__unused)
495 __setup("keepinitrd", keepinitrd_setup);