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
);
123 static void __init
find_node_limits(int node
, struct meminfo
*mi
,
124 unsigned long *min
, unsigned long *max_low
, unsigned long *max_high
)
129 *max_low
= *max_high
= 0;
131 for_each_nodebank(i
, mi
, node
) {
132 struct membank
*bank
= &mi
->bank
[i
];
133 unsigned long start
, end
;
135 start
= bank_pfn_start(bank
);
136 end
= bank_pfn_end(bank
);
150 * FIXME: We really want to avoid allocating the bootmap bitmap
151 * over the top of the initrd. Hopefully, this is located towards
152 * the start of a bank, so if we allocate the bootmap bitmap at
153 * the end, we won't clash.
155 static unsigned int __init
156 find_bootmap_pfn(int node
, struct meminfo
*mi
, unsigned int bootmap_pages
)
158 unsigned int start_pfn
, i
, bootmap_pfn
;
160 start_pfn
= PAGE_ALIGN(__pa(_end
)) >> PAGE_SHIFT
;
163 for_each_nodebank(i
, mi
, node
) {
164 struct membank
*bank
= &mi
->bank
[i
];
165 unsigned int start
, end
;
167 start
= bank_pfn_start(bank
);
168 end
= bank_pfn_end(bank
);
173 if (start
< start_pfn
)
179 if (end
- start
>= bootmap_pages
) {
185 if (bootmap_pfn
== 0)
191 static int __init
check_initrd(struct meminfo
*mi
)
193 int initrd_node
= -2;
194 #ifdef CONFIG_BLK_DEV_INITRD
195 unsigned long end
= phys_initrd_start
+ phys_initrd_size
;
198 * Make sure that the initrd is within a valid area of
201 if (phys_initrd_size
) {
206 for (i
= 0; i
< mi
->nr_banks
; i
++) {
207 struct membank
*bank
= &mi
->bank
[i
];
208 if (bank_phys_start(bank
) <= phys_initrd_start
&&
209 end
<= bank_phys_end(bank
))
210 initrd_node
= bank
->node
;
214 if (initrd_node
== -1) {
215 printk(KERN_ERR
"INITRD: 0x%08lx+0x%08lx extends beyond "
216 "physical memory - disabling initrd\n",
217 phys_initrd_start
, phys_initrd_size
);
218 phys_initrd_start
= phys_initrd_size
= 0;
225 static inline void map_memory_bank(struct membank
*bank
)
230 map
.pfn
= bank_pfn_start(bank
);
231 map
.virtual = __phys_to_virt(bank_phys_start(bank
));
232 map
.length
= bank_phys_size(bank
);
233 map
.type
= MT_MEMORY
;
235 create_mapping(&map
);
239 static void __init
bootmem_init_node(int node
, struct meminfo
*mi
,
240 unsigned long start_pfn
, unsigned long end_pfn
)
242 unsigned long boot_pfn
;
243 unsigned int boot_pages
;
248 * Map the memory banks for this node.
250 for_each_nodebank(i
, mi
, node
) {
251 struct membank
*bank
= &mi
->bank
[i
];
254 map_memory_bank(bank
);
258 * Allocate the bootmem bitmap page.
260 boot_pages
= bootmem_bootmap_pages(end_pfn
- start_pfn
);
261 boot_pfn
= find_bootmap_pfn(node
, mi
, boot_pages
);
264 * Initialise the bootmem allocator for this node, handing the
265 * memory banks over to bootmem.
267 node_set_online(node
);
268 pgdat
= NODE_DATA(node
);
269 init_bootmem_node(pgdat
, boot_pfn
, start_pfn
, end_pfn
);
271 for_each_nodebank(i
, mi
, node
) {
272 struct membank
*bank
= &mi
->bank
[i
];
274 free_bootmem_node(pgdat
, bank_phys_start(bank
), bank_phys_size(bank
));
275 memory_present(node
, bank_pfn_start(bank
), bank_pfn_end(bank
));
279 * Reserve the bootmem bitmap for this node.
281 reserve_bootmem_node(pgdat
, boot_pfn
<< PAGE_SHIFT
,
282 boot_pages
<< PAGE_SHIFT
, BOOTMEM_DEFAULT
);
285 static void __init
bootmem_reserve_initrd(int node
)
287 #ifdef CONFIG_BLK_DEV_INITRD
288 pg_data_t
*pgdat
= NODE_DATA(node
);
291 res
= reserve_bootmem_node(pgdat
, phys_initrd_start
,
292 phys_initrd_size
, BOOTMEM_EXCLUSIVE
);
295 initrd_start
= __phys_to_virt(phys_initrd_start
);
296 initrd_end
= initrd_start
+ phys_initrd_size
;
299 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
300 "memory region - disabling initrd\n",
301 phys_initrd_start
, phys_initrd_size
);
306 static void __init
bootmem_free_node(int node
, struct meminfo
*mi
)
308 unsigned long zone_size
[MAX_NR_ZONES
], zhole_size
[MAX_NR_ZONES
];
309 unsigned long min
, max_low
, max_high
;
312 find_node_limits(node
, mi
, &min
, &max_low
, &max_high
);
315 * initialise the zones within this node.
317 memset(zone_size
, 0, sizeof(zone_size
));
320 * The size of this node has already been determined. If we need
321 * to do anything fancy with the allocation of this memory to the
322 * zones, now is the time to do it.
324 zone_size
[0] = max_low
- min
;
325 #ifdef CONFIG_HIGHMEM
326 zone_size
[ZONE_HIGHMEM
] = max_high
- max_low
;
330 * For each bank in this node, calculate the size of the holes.
331 * holes = node_size - sum(bank_sizes_in_node)
333 memcpy(zhole_size
, zone_size
, sizeof(zhole_size
));
334 for_each_nodebank(i
, mi
, node
) {
336 #ifdef CONFIG_HIGHMEM
337 if (mi
->bank
[i
].highmem
)
340 zhole_size
[idx
] -= bank_pfn_size(&mi
->bank
[i
]);
344 * Adjust the sizes according to any special requirements for
347 arch_adjust_zones(node
, zone_size
, zhole_size
);
349 free_area_init_node(node
, zone_size
, min
, zhole_size
);
352 void __init
bootmem_init(void)
354 struct meminfo
*mi
= &meminfo
;
355 unsigned long min
, max_low
, max_high
;
356 int node
, initrd_node
;
359 * Locate which node contains the ramdisk image, if any.
361 initrd_node
= check_initrd(mi
);
363 max_low
= max_high
= 0;
366 * Run through each node initialising the bootmem allocator.
368 for_each_node(node
) {
369 unsigned long node_low
, node_high
;
371 find_node_limits(node
, mi
, &min
, &node_low
, &node_high
);
373 if (node_low
> max_low
)
375 if (node_high
> max_high
)
376 max_high
= node_high
;
379 * If there is no memory in this node, ignore it.
380 * (We can't have nodes which have no lowmem)
385 bootmem_init_node(node
, mi
, min
, node_low
);
388 * Reserve any special node zero regions.
391 reserve_node_zero(NODE_DATA(node
));
394 * If the initrd is in this node, reserve its memory.
396 if (node
== initrd_node
)
397 bootmem_reserve_initrd(node
);
401 * sparse_init() needs the bootmem allocator up and running.
406 * Now free memory in each node - free_area_init_node needs
407 * the sparse mem_map arrays initialized by sparse_init()
408 * for memmap_init_zone(), otherwise all PFNs are invalid.
411 bootmem_free_node(node
, mi
);
413 high_memory
= __va((max_low
<< PAGE_SHIFT
) - 1) + 1;
416 * This doesn't seem to be used by the Linux memory manager any
417 * more, but is used by ll_rw_block. If we can get rid of it, we
418 * also get rid of some of the stuff above as well.
420 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
421 * the system, not the maximum PFN.
423 max_low_pfn
= max_low
- PHYS_PFN_OFFSET
;
424 max_pfn
= max_high
- PHYS_PFN_OFFSET
;
427 static inline int free_area(unsigned long pfn
, unsigned long end
, char *s
)
429 unsigned int pages
= 0, size
= (end
- pfn
) << (PAGE_SHIFT
- 10);
431 for (; pfn
< end
; pfn
++) {
432 struct page
*page
= pfn_to_page(pfn
);
433 ClearPageReserved(page
);
434 init_page_count(page
);
440 printk(KERN_INFO
"Freeing %s memory: %dK\n", s
, size
);
446 free_memmap(int node
, unsigned long start_pfn
, unsigned long end_pfn
)
448 struct page
*start_pg
, *end_pg
;
449 unsigned long pg
, pgend
;
452 * Convert start_pfn/end_pfn to a struct page pointer.
454 start_pg
= pfn_to_page(start_pfn
);
455 end_pg
= pfn_to_page(end_pfn
);
458 * Convert to physical addresses, and
459 * round start upwards and end downwards.
461 pg
= PAGE_ALIGN(__pa(start_pg
));
462 pgend
= __pa(end_pg
) & PAGE_MASK
;
465 * If there are free pages between these,
466 * free the section of the memmap array.
469 free_bootmem_node(NODE_DATA(node
), pg
, pgend
- pg
);
473 * The mem_map array can get very big. Free the unused area of the memory map.
475 static void __init
free_unused_memmap_node(int node
, struct meminfo
*mi
)
477 unsigned long bank_start
, prev_bank_end
= 0;
481 * [FIXME] This relies on each bank being in address order. This
482 * may not be the case, especially if the user has provided the
483 * information on the command line.
485 for_each_nodebank(i
, mi
, node
) {
486 struct membank
*bank
= &mi
->bank
[i
];
488 bank_start
= bank_pfn_start(bank
);
489 if (bank_start
< prev_bank_end
) {
490 printk(KERN_ERR
"MEM: unordered memory banks. "
491 "Not freeing memmap.\n");
496 * If we had a previous bank, and there is a space
497 * between the current bank and the previous, free it.
499 if (prev_bank_end
&& prev_bank_end
!= bank_start
)
500 free_memmap(node
, prev_bank_end
, bank_start
);
502 prev_bank_end
= bank_pfn_end(bank
);
507 * mem_init() marks the free areas in the mem_map and tells us how much
508 * memory is free. This is done after various parts of the system have
509 * claimed their memory after the kernel image.
511 void __init
mem_init(void)
513 unsigned int codesize
, datasize
, initsize
;
516 #ifndef CONFIG_DISCONTIGMEM
517 max_mapnr
= pfn_to_page(max_pfn
+ PHYS_PFN_OFFSET
) - mem_map
;
520 /* this will put all unused low memory onto the freelists */
521 for_each_online_node(node
) {
522 pg_data_t
*pgdat
= NODE_DATA(node
);
524 free_unused_memmap_node(node
, &meminfo
);
526 if (pgdat
->node_spanned_pages
!= 0)
527 totalram_pages
+= free_all_bootmem_node(pgdat
);
531 /* now that our DMA memory is actually so designated, we can free it */
532 totalram_pages
+= free_area(PHYS_PFN_OFFSET
,
533 __phys_to_pfn(__pa(swapper_pg_dir
)), NULL
);
536 #ifdef CONFIG_HIGHMEM
537 /* set highmem page free */
538 for_each_online_node(node
) {
539 for_each_nodebank (i
, &meminfo
, node
) {
540 unsigned long start
= bank_pfn_start(&meminfo
.bank
[i
]);
541 unsigned long end
= bank_pfn_end(&meminfo
.bank
[i
]);
542 if (start
>= max_low_pfn
+ PHYS_PFN_OFFSET
)
543 totalhigh_pages
+= free_area(start
, end
, NULL
);
546 totalram_pages
+= totalhigh_pages
;
550 * Since our memory may not be contiguous, calculate the
551 * real number of pages we have in this system
553 printk(KERN_INFO
"Memory:");
555 for (i
= 0; i
< meminfo
.nr_banks
; i
++) {
556 num_physpages
+= bank_pfn_size(&meminfo
.bank
[i
]);
557 printk(" %ldMB", bank_phys_size(&meminfo
.bank
[i
]) >> 20);
559 printk(" = %luMB total\n", num_physpages
>> (20 - PAGE_SHIFT
));
561 codesize
= _etext
- _text
;
562 datasize
= _end
- _data
;
563 initsize
= __init_end
- __init_begin
;
565 printk(KERN_NOTICE
"Memory: %luKB available (%dK code, "
566 "%dK data, %dK init, %luK highmem)\n",
567 (unsigned long) nr_free_pages() << (PAGE_SHIFT
-10),
568 codesize
>> 10, datasize
>> 10, initsize
>> 10,
569 (unsigned long) (totalhigh_pages
<< (PAGE_SHIFT
-10)));
571 if (PAGE_SIZE
>= 16384 && num_physpages
<= 128) {
572 extern int sysctl_overcommit_memory
;
574 * On a machine this small we won't get
575 * anywhere without overcommit, so turn
578 sysctl_overcommit_memory
= OVERCOMMIT_ALWAYS
;
582 void free_initmem(void)
584 if (!machine_is_integrator() && !machine_is_cintegrator())
585 totalram_pages
+= free_area(__phys_to_pfn(__pa(__init_begin
)),
586 __phys_to_pfn(__pa(__init_end
)),
590 #ifdef CONFIG_BLK_DEV_INITRD
592 static int keep_initrd
;
594 void free_initrd_mem(unsigned long start
, unsigned long end
)
597 totalram_pages
+= free_area(__phys_to_pfn(__pa(start
)),
598 __phys_to_pfn(__pa(end
)),
602 static int __init
keepinitrd_setup(char *__unused
)
608 __setup("keepinitrd", keepinitrd_setup
);