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 extern void _text
, _etext
, __data_start
, _end
, __init_begin
, __init_end
;
30 extern unsigned long phys_initrd_start
;
31 extern unsigned long phys_initrd_size
;
34 * This is used to pass memory configuration data from paging_init
35 * to mem_init, and by show_mem() to skip holes in the memory map.
37 static struct meminfo meminfo
= { 0, };
39 #define for_each_nodebank(iter,mi,no) \
40 for (iter = 0; iter < mi->nr_banks; iter++) \
41 if (mi->bank[iter].node == no)
45 int free
= 0, total
= 0, reserved
= 0;
46 int shared
= 0, cached
= 0, slab
= 0, node
, i
;
47 struct meminfo
* mi
= &meminfo
;
49 printk("Mem-info:\n");
51 printk("Free swap: %6ldkB\n", nr_swap_pages
<<(PAGE_SHIFT
-10));
53 for_each_online_node(node
) {
54 pg_data_t
*n
= NODE_DATA(node
);
55 struct page
*map
= n
->node_mem_map
- n
->node_start_pfn
;
57 for_each_nodebank (i
,mi
,node
) {
58 unsigned int pfn1
, pfn2
;
59 struct page
*page
, *end
;
61 pfn1
= __phys_to_pfn(mi
->bank
[i
].start
);
62 pfn2
= __phys_to_pfn(mi
->bank
[i
].size
+ mi
->bank
[i
].start
);
69 if (PageReserved(page
))
71 else if (PageSwapCache(page
))
73 else if (PageSlab(page
))
75 else if (!page_count(page
))
78 shared
+= page_count(page
) - 1;
84 printk("%d pages of RAM\n", total
);
85 printk("%d free pages\n", free
);
86 printk("%d reserved pages\n", reserved
);
87 printk("%d slab pages\n", slab
);
88 printk("%d pages shared\n", shared
);
89 printk("%d pages swap cached\n", cached
);
93 * FIXME: We really want to avoid allocating the bootmap bitmap
94 * over the top of the initrd. Hopefully, this is located towards
95 * the start of a bank, so if we allocate the bootmap bitmap at
96 * the end, we won't clash.
98 static unsigned int __init
99 find_bootmap_pfn(int node
, struct meminfo
*mi
, unsigned int bootmap_pages
)
101 unsigned int start_pfn
, bank
, bootmap_pfn
;
103 start_pfn
= PAGE_ALIGN(__pa(&_end
)) >> PAGE_SHIFT
;
106 for_each_nodebank(bank
, mi
, node
) {
107 unsigned int start
, end
;
109 start
= mi
->bank
[bank
].start
>> PAGE_SHIFT
;
110 end
= (mi
->bank
[bank
].size
+
111 mi
->bank
[bank
].start
) >> PAGE_SHIFT
;
116 if (start
< start_pfn
)
122 if (end
- start
>= bootmap_pages
) {
128 if (bootmap_pfn
== 0)
134 static int __init
check_initrd(struct meminfo
*mi
)
136 int initrd_node
= -2;
137 #ifdef CONFIG_BLK_DEV_INITRD
138 unsigned long end
= phys_initrd_start
+ phys_initrd_size
;
141 * Make sure that the initrd is within a valid area of
144 if (phys_initrd_size
) {
149 for (i
= 0; i
< mi
->nr_banks
; i
++) {
150 unsigned long bank_end
;
152 bank_end
= mi
->bank
[i
].start
+ mi
->bank
[i
].size
;
154 if (mi
->bank
[i
].start
<= phys_initrd_start
&&
156 initrd_node
= mi
->bank
[i
].node
;
160 if (initrd_node
== -1) {
161 printk(KERN_ERR
"initrd (0x%08lx - 0x%08lx) extends beyond "
162 "physical memory - disabling initrd\n",
163 phys_initrd_start
, end
);
164 phys_initrd_start
= phys_initrd_size
= 0;
171 static inline void map_memory_bank(struct membank
*bank
)
176 map
.pfn
= __phys_to_pfn(bank
->start
);
177 map
.virtual = __phys_to_virt(bank
->start
);
178 map
.length
= bank
->size
;
179 map
.type
= MT_MEMORY
;
181 create_mapping(&map
);
185 static unsigned long __init
186 bootmem_init_node(int node
, int initrd_node
, struct meminfo
*mi
)
188 unsigned long zone_size
[MAX_NR_ZONES
], zhole_size
[MAX_NR_ZONES
];
189 unsigned long start_pfn
, end_pfn
, boot_pfn
;
190 unsigned int boot_pages
;
198 * Calculate the pfn range, and map the memory banks for this node.
200 for_each_nodebank(i
, mi
, node
) {
201 struct membank
*bank
= &mi
->bank
[i
];
202 unsigned long start
, end
;
204 start
= bank
->start
>> PAGE_SHIFT
;
205 end
= (bank
->start
+ bank
->size
) >> PAGE_SHIFT
;
207 if (start_pfn
> start
)
212 map_memory_bank(bank
);
216 * If there is no memory in this node, ignore it.
222 * Allocate the bootmem bitmap page.
224 boot_pages
= bootmem_bootmap_pages(end_pfn
- start_pfn
);
225 boot_pfn
= find_bootmap_pfn(node
, mi
, boot_pages
);
228 * Initialise the bootmem allocator for this node, handing the
229 * memory banks over to bootmem.
231 node_set_online(node
);
232 pgdat
= NODE_DATA(node
);
233 init_bootmem_node(pgdat
, boot_pfn
, start_pfn
, end_pfn
);
235 for_each_nodebank(i
, mi
, node
)
236 free_bootmem_node(pgdat
, mi
->bank
[i
].start
, mi
->bank
[i
].size
);
239 * Reserve the bootmem bitmap for this node.
241 reserve_bootmem_node(pgdat
, boot_pfn
<< PAGE_SHIFT
,
242 boot_pages
<< PAGE_SHIFT
);
244 #ifdef CONFIG_BLK_DEV_INITRD
246 * If the initrd is in this node, reserve its memory.
248 if (node
== initrd_node
) {
249 reserve_bootmem_node(pgdat
, phys_initrd_start
,
251 initrd_start
= __phys_to_virt(phys_initrd_start
);
252 initrd_end
= initrd_start
+ phys_initrd_size
;
257 * Finally, reserve any node zero regions.
260 reserve_node_zero(pgdat
);
263 * initialise the zones within this node.
265 memset(zone_size
, 0, sizeof(zone_size
));
266 memset(zhole_size
, 0, sizeof(zhole_size
));
269 * The size of this node has already been determined. If we need
270 * to do anything fancy with the allocation of this memory to the
271 * zones, now is the time to do it.
273 zone_size
[0] = end_pfn
- start_pfn
;
276 * For each bank in this node, calculate the size of the holes.
277 * holes = node_size - sum(bank_sizes_in_node)
279 zhole_size
[0] = zone_size
[0];
280 for_each_nodebank(i
, mi
, node
)
281 zhole_size
[0] -= mi
->bank
[i
].size
>> PAGE_SHIFT
;
284 * Adjust the sizes according to any special requirements for
287 arch_adjust_zones(node
, zone_size
, zhole_size
);
289 free_area_init_node(node
, pgdat
, zone_size
, start_pfn
, zhole_size
);
294 void __init
bootmem_init(struct meminfo
*mi
)
296 unsigned long memend_pfn
= 0;
297 int node
, initrd_node
, i
;
300 * Invalidate the node number for empty or invalid memory banks
302 for (i
= 0; i
< mi
->nr_banks
; i
++)
303 if (mi
->bank
[i
].size
== 0 || mi
->bank
[i
].node
>= MAX_NUMNODES
)
304 mi
->bank
[i
].node
= -1;
306 memcpy(&meminfo
, mi
, sizeof(meminfo
));
309 * Locate which node contains the ramdisk image, if any.
311 initrd_node
= check_initrd(mi
);
314 * Run through each node initialising the bootmem allocator.
316 for_each_node(node
) {
317 unsigned long end_pfn
;
319 end_pfn
= bootmem_init_node(node
, initrd_node
, mi
);
322 * Remember the highest memory PFN.
324 if (end_pfn
> memend_pfn
)
325 memend_pfn
= end_pfn
;
328 high_memory
= __va(memend_pfn
<< PAGE_SHIFT
);
331 * This doesn't seem to be used by the Linux memory manager any
332 * more, but is used by ll_rw_block. If we can get rid of it, we
333 * also get rid of some of the stuff above as well.
335 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
336 * the system, not the maximum PFN.
338 max_pfn
= max_low_pfn
= memend_pfn
- PHYS_PFN_OFFSET
;
341 static inline void free_area(unsigned long addr
, unsigned long end
, char *s
)
343 unsigned int size
= (end
- addr
) >> 10;
345 for (; addr
< end
; addr
+= PAGE_SIZE
) {
346 struct page
*page
= virt_to_page(addr
);
347 ClearPageReserved(page
);
348 init_page_count(page
);
354 printk(KERN_INFO
"Freeing %s memory: %dK\n", s
, size
);
358 free_memmap(int node
, unsigned long start_pfn
, unsigned long end_pfn
)
360 struct page
*start_pg
, *end_pg
;
361 unsigned long pg
, pgend
;
364 * Convert start_pfn/end_pfn to a struct page pointer.
366 start_pg
= pfn_to_page(start_pfn
);
367 end_pg
= pfn_to_page(end_pfn
);
370 * Convert to physical addresses, and
371 * round start upwards and end downwards.
373 pg
= PAGE_ALIGN(__pa(start_pg
));
374 pgend
= __pa(end_pg
) & PAGE_MASK
;
377 * If there are free pages between these,
378 * free the section of the memmap array.
381 free_bootmem_node(NODE_DATA(node
), pg
, pgend
- pg
);
385 * The mem_map array can get very big. Free the unused area of the memory map.
387 static void __init
free_unused_memmap_node(int node
, struct meminfo
*mi
)
389 unsigned long bank_start
, prev_bank_end
= 0;
393 * [FIXME] This relies on each bank being in address order. This
394 * may not be the case, especially if the user has provided the
395 * information on the command line.
397 for_each_nodebank(i
, mi
, node
) {
398 bank_start
= mi
->bank
[i
].start
>> PAGE_SHIFT
;
399 if (bank_start
< prev_bank_end
) {
400 printk(KERN_ERR
"MEM: unordered memory banks. "
401 "Not freeing memmap.\n");
406 * If we had a previous bank, and there is a space
407 * between the current bank and the previous, free it.
409 if (prev_bank_end
&& prev_bank_end
!= bank_start
)
410 free_memmap(node
, prev_bank_end
, bank_start
);
412 prev_bank_end
= (mi
->bank
[i
].start
+
413 mi
->bank
[i
].size
) >> PAGE_SHIFT
;
418 * mem_init() marks the free areas in the mem_map and tells us how much
419 * memory is free. This is done after various parts of the system have
420 * claimed their memory after the kernel image.
422 void __init
mem_init(void)
424 unsigned int codepages
, datapages
, initpages
;
427 codepages
= &_etext
- &_text
;
428 datapages
= &_end
- &__data_start
;
429 initpages
= &__init_end
- &__init_begin
;
431 #ifndef CONFIG_DISCONTIGMEM
432 max_mapnr
= virt_to_page(high_memory
) - mem_map
;
435 /* this will put all unused low memory onto the freelists */
436 for_each_online_node(node
) {
437 pg_data_t
*pgdat
= NODE_DATA(node
);
439 free_unused_memmap_node(node
, &meminfo
);
441 if (pgdat
->node_spanned_pages
!= 0)
442 totalram_pages
+= free_all_bootmem_node(pgdat
);
446 /* now that our DMA memory is actually so designated, we can free it */
447 free_area(PAGE_OFFSET
, (unsigned long)swapper_pg_dir
, NULL
);
451 * Since our memory may not be contiguous, calculate the
452 * real number of pages we have in this system
454 printk(KERN_INFO
"Memory:");
457 for (i
= 0; i
< meminfo
.nr_banks
; i
++) {
458 num_physpages
+= meminfo
.bank
[i
].size
>> PAGE_SHIFT
;
459 printk(" %ldMB", meminfo
.bank
[i
].size
>> 20);
462 printk(" = %luMB total\n", num_physpages
>> (20 - PAGE_SHIFT
));
463 printk(KERN_NOTICE
"Memory: %luKB available (%dK code, "
464 "%dK data, %dK init)\n",
465 (unsigned long) nr_free_pages() << (PAGE_SHIFT
-10),
466 codepages
>> 10, datapages
>> 10, initpages
>> 10);
468 if (PAGE_SIZE
>= 16384 && num_physpages
<= 128) {
469 extern int sysctl_overcommit_memory
;
471 * On a machine this small we won't get
472 * anywhere without overcommit, so turn
475 sysctl_overcommit_memory
= OVERCOMMIT_ALWAYS
;
479 void free_initmem(void)
481 if (!machine_is_integrator() && !machine_is_cintegrator()) {
482 free_area((unsigned long)(&__init_begin
),
483 (unsigned long)(&__init_end
),
488 #ifdef CONFIG_BLK_DEV_INITRD
490 static int keep_initrd
;
492 void free_initrd_mem(unsigned long start
, unsigned long end
)
495 free_area(start
, end
, "initrd");
498 static int __init
keepinitrd_setup(char *__unused
)
504 __setup("keepinitrd", keepinitrd_setup
);