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 * This is used to pass memory configuration data from paging_init
36 * to mem_init, and by show_mem() to skip holes in the memory map.
38 static struct meminfo meminfo = { 0, };
40 #define for_each_nodebank(iter,mi,no) \
41 for (iter = 0; iter < mi->nr_banks; iter++) \
42 if (mi->bank[iter].node == no)
46 int free = 0, total = 0, reserved = 0;
47 int shared = 0, cached = 0, slab = 0, node, i;
48 struct meminfo * mi = &meminfo;
50 printk("Mem-info:\n");
52 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
54 for_each_online_node(node) {
55 pg_data_t *n = NODE_DATA(node);
56 struct page *map = n->node_mem_map - n->node_start_pfn;
58 for_each_nodebank (i,mi,node) {
59 unsigned int pfn1, pfn2;
60 struct page *page, *end;
62 pfn1 = __phys_to_pfn(mi->bank[i].start);
63 pfn2 = __phys_to_pfn(mi->bank[i].size + mi->bank[i].start);
70 if (PageReserved(page))
72 else if (PageSwapCache(page))
74 else if (PageSlab(page))
76 else if (!page_count(page))
79 shared += page_count(page) - 1;
85 printk("%d pages of RAM\n", total);
86 printk("%d free pages\n", free);
87 printk("%d reserved pages\n", reserved);
88 printk("%d slab pages\n", slab);
89 printk("%d pages shared\n", shared);
90 printk("%d pages swap cached\n", cached);
94 * FIXME: We really want to avoid allocating the bootmap bitmap
95 * over the top of the initrd. Hopefully, this is located towards
96 * the start of a bank, so if we allocate the bootmap bitmap at
97 * the end, we won't clash.
99 static unsigned int __init
100 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
102 unsigned int start_pfn, bank, bootmap_pfn;
104 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
107 for_each_nodebank(bank, mi, node) {
108 unsigned int start, end;
110 start = mi->bank[bank].start >> PAGE_SHIFT;
111 end = (mi->bank[bank].size +
112 mi->bank[bank].start) >> PAGE_SHIFT;
117 if (start < start_pfn)
123 if (end - start >= bootmap_pages) {
129 if (bootmap_pfn == 0)
135 static int __init check_initrd(struct meminfo *mi)
137 int initrd_node = -2;
138 #ifdef CONFIG_BLK_DEV_INITRD
139 unsigned long end = phys_initrd_start + phys_initrd_size;
142 * Make sure that the initrd is within a valid area of
145 if (phys_initrd_size) {
150 for (i = 0; i < mi->nr_banks; i++) {
151 unsigned long bank_end;
153 bank_end = mi->bank[i].start + mi->bank[i].size;
155 if (mi->bank[i].start <= phys_initrd_start &&
157 initrd_node = mi->bank[i].node;
161 if (initrd_node == -1) {
162 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
163 "physical memory - disabling initrd\n",
164 phys_initrd_start, end);
165 phys_initrd_start = phys_initrd_size = 0;
172 static inline void map_memory_bank(struct membank *bank)
177 map.pfn = __phys_to_pfn(bank->start);
178 map.virtual = __phys_to_virt(bank->start);
179 map.length = bank->size;
180 map.type = MT_MEMORY;
182 create_mapping(&map);
186 static unsigned long __init
187 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
189 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
190 unsigned long start_pfn, end_pfn, boot_pfn;
191 unsigned int boot_pages;
199 * Calculate the pfn range, and map the memory banks for this node.
201 for_each_nodebank(i, mi, node) {
202 struct membank *bank = &mi->bank[i];
203 unsigned long start, end;
205 start = bank->start >> PAGE_SHIFT;
206 end = (bank->start + bank->size) >> PAGE_SHIFT;
208 if (start_pfn > start)
213 map_memory_bank(bank);
217 * If there is no memory in this node, ignore it.
223 * Allocate the bootmem bitmap page.
225 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
226 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
229 * Initialise the bootmem allocator for this node, handing the
230 * memory banks over to bootmem.
232 node_set_online(node);
233 pgdat = NODE_DATA(node);
234 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
236 for_each_nodebank(i, mi, node)
237 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
240 * Reserve the bootmem bitmap for this node.
242 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
243 boot_pages << PAGE_SHIFT);
245 #ifdef CONFIG_BLK_DEV_INITRD
247 * If the initrd is in this node, reserve its memory.
249 if (node == initrd_node) {
250 reserve_bootmem_node(pgdat, phys_initrd_start,
252 initrd_start = __phys_to_virt(phys_initrd_start);
253 initrd_end = initrd_start + phys_initrd_size;
258 * Finally, reserve any node zero regions.
261 reserve_node_zero(pgdat);
264 * initialise the zones within this node.
266 memset(zone_size, 0, sizeof(zone_size));
267 memset(zhole_size, 0, sizeof(zhole_size));
270 * The size of this node has already been determined. If we need
271 * to do anything fancy with the allocation of this memory to the
272 * zones, now is the time to do it.
274 zone_size[0] = end_pfn - start_pfn;
277 * For each bank in this node, calculate the size of the holes.
278 * holes = node_size - sum(bank_sizes_in_node)
280 zhole_size[0] = zone_size[0];
281 for_each_nodebank(i, mi, node)
282 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
285 * Adjust the sizes according to any special requirements for
288 arch_adjust_zones(node, zone_size, zhole_size);
290 free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
295 void __init bootmem_init(struct meminfo *mi)
297 unsigned long memend_pfn = 0;
298 int node, initrd_node, i;
301 * Invalidate the node number for empty or invalid memory banks
303 for (i = 0; i < mi->nr_banks; i++)
304 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
305 mi->bank[i].node = -1;
307 memcpy(&meminfo, mi, sizeof(meminfo));
310 * Locate which node contains the ramdisk image, if any.
312 initrd_node = check_initrd(mi);
315 * Run through each node initialising the bootmem allocator.
317 for_each_node(node) {
318 unsigned long end_pfn;
320 end_pfn = bootmem_init_node(node, initrd_node, mi);
323 * Remember the highest memory PFN.
325 if (end_pfn > memend_pfn)
326 memend_pfn = end_pfn;
329 high_memory = __va(memend_pfn << PAGE_SHIFT);
332 * This doesn't seem to be used by the Linux memory manager any
333 * more, but is used by ll_rw_block. If we can get rid of it, we
334 * also get rid of some of the stuff above as well.
336 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
337 * the system, not the maximum PFN.
339 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
342 static inline void free_area(unsigned long addr, unsigned long end, char *s)
344 unsigned int size = (end - addr) >> 10;
346 for (; addr < end; addr += PAGE_SIZE) {
347 struct page *page = virt_to_page(addr);
348 ClearPageReserved(page);
349 init_page_count(page);
355 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
359 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
361 struct page *start_pg, *end_pg;
362 unsigned long pg, pgend;
365 * Convert start_pfn/end_pfn to a struct page pointer.
367 start_pg = pfn_to_page(start_pfn);
368 end_pg = pfn_to_page(end_pfn);
371 * Convert to physical addresses, and
372 * round start upwards and end downwards.
374 pg = PAGE_ALIGN(__pa(start_pg));
375 pgend = __pa(end_pg) & PAGE_MASK;
378 * If there are free pages between these,
379 * free the section of the memmap array.
382 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
386 * The mem_map array can get very big. Free the unused area of the memory map.
388 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
390 unsigned long bank_start, prev_bank_end = 0;
394 * [FIXME] This relies on each bank being in address order. This
395 * may not be the case, especially if the user has provided the
396 * information on the command line.
398 for_each_nodebank(i, mi, node) {
399 bank_start = mi->bank[i].start >> PAGE_SHIFT;
400 if (bank_start < prev_bank_end) {
401 printk(KERN_ERR "MEM: unordered memory banks. "
402 "Not freeing memmap.\n");
407 * If we had a previous bank, and there is a space
408 * between the current bank and the previous, free it.
410 if (prev_bank_end && prev_bank_end != bank_start)
411 free_memmap(node, prev_bank_end, bank_start);
413 prev_bank_end = (mi->bank[i].start +
414 mi->bank[i].size) >> PAGE_SHIFT;
419 * mem_init() marks the free areas in the mem_map and tells us how much
420 * memory is free. This is done after various parts of the system have
421 * claimed their memory after the kernel image.
423 void __init mem_init(void)
425 unsigned int codepages, datapages, initpages;
428 codepages = &_etext - &_text;
429 datapages = &_end - &__data_start;
430 initpages = &__init_end - &__init_begin;
432 #ifndef CONFIG_DISCONTIGMEM
433 max_mapnr = virt_to_page(high_memory) - mem_map;
436 /* this will put all unused low memory onto the freelists */
437 for_each_online_node(node) {
438 pg_data_t *pgdat = NODE_DATA(node);
440 free_unused_memmap_node(node, &meminfo);
442 if (pgdat->node_spanned_pages != 0)
443 totalram_pages += free_all_bootmem_node(pgdat);
447 /* now that our DMA memory is actually so designated, we can free it */
448 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
452 * Since our memory may not be contiguous, calculate the
453 * real number of pages we have in this system
455 printk(KERN_INFO "Memory:");
458 for (i = 0; i < meminfo.nr_banks; i++) {
459 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
460 printk(" %ldMB", meminfo.bank[i].size >> 20);
463 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
464 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
465 "%dK data, %dK init)\n",
466 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
467 codepages >> 10, datapages >> 10, initpages >> 10);
469 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
470 extern int sysctl_overcommit_memory;
472 * On a machine this small we won't get
473 * anywhere without overcommit, so turn
476 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
480 void free_initmem(void)
482 if (!machine_is_integrator() && !machine_is_cintegrator()) {
483 free_area((unsigned long)(&__init_begin),
484 (unsigned long)(&__init_end),
489 #ifdef CONFIG_BLK_DEV_INITRD
491 static int keep_initrd;
493 void free_initrd_mem(unsigned long start, unsigned long end)
496 free_area(start, end, "initrd");
499 static int __init keepinitrd_setup(char *__unused)
505 __setup("keepinitrd", keepinitrd_setup);