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/sort.h>
19 #include <linux/highmem.h>
21 #include <asm/mach-types.h>
22 #include <asm/sections.h>
23 #include <asm/setup.h>
24 #include <asm/sizes.h>
27 #include <asm/mach/arch.h>
28 #include <asm/mach/map.h>
32 static unsigned long phys_initrd_start __initdata
= 0;
33 static unsigned long phys_initrd_size __initdata
= 0;
35 static void __init
early_initrd(char **p
)
37 unsigned long start
, size
;
39 start
= memparse(*p
, p
);
41 size
= memparse((*p
) + 1, p
);
43 phys_initrd_start
= start
;
44 phys_initrd_size
= size
;
47 __early_param("initrd=", early_initrd
);
49 static int __init
parse_tag_initrd(const struct tag
*tag
)
51 printk(KERN_WARNING
"ATAG_INITRD is deprecated; "
52 "please update your bootloader.\n");
53 phys_initrd_start
= __virt_to_phys(tag
->u
.initrd
.start
);
54 phys_initrd_size
= tag
->u
.initrd
.size
;
58 __tagtable(ATAG_INITRD
, parse_tag_initrd
);
60 static int __init
parse_tag_initrd2(const struct tag
*tag
)
62 phys_initrd_start
= tag
->u
.initrd
.start
;
63 phys_initrd_size
= tag
->u
.initrd
.size
;
67 __tagtable(ATAG_INITRD2
, parse_tag_initrd2
);
70 * This keeps memory configuration data used by a couple memory
71 * initialization functions, as well as show_mem() for the skipping
72 * of holes in the memory map. It is populated by arm_add_memory().
74 struct meminfo meminfo
;
78 int free
= 0, total
= 0, reserved
= 0;
79 int shared
= 0, cached
= 0, slab
= 0, node
, i
;
80 struct meminfo
* mi
= &meminfo
;
82 printk("Mem-info:\n");
84 for_each_online_node(node
) {
85 pg_data_t
*n
= NODE_DATA(node
);
86 struct page
*map
= pgdat_page_nr(n
, 0) - n
->node_start_pfn
;
88 for_each_nodebank (i
,mi
,node
) {
89 struct membank
*bank
= &mi
->bank
[i
];
90 unsigned int pfn1
, pfn2
;
91 struct page
*page
, *end
;
93 pfn1
= bank_pfn_start(bank
);
94 pfn2
= bank_pfn_end(bank
);
101 if (PageReserved(page
))
103 else if (PageSwapCache(page
))
105 else if (PageSlab(page
))
107 else if (!page_count(page
))
110 shared
+= page_count(page
) - 1;
112 } while (page
< end
);
116 printk("%d pages of RAM\n", total
);
117 printk("%d free pages\n", free
);
118 printk("%d reserved pages\n", reserved
);
119 printk("%d slab pages\n", slab
);
120 printk("%d pages shared\n", shared
);
121 printk("%d pages swap cached\n", cached
);
124 static void __init
find_node_limits(int node
, struct meminfo
*mi
,
125 unsigned long *min
, unsigned long *max_low
, unsigned long *max_high
)
130 *max_low
= *max_high
= 0;
132 for_each_nodebank(i
, mi
, node
) {
133 struct membank
*bank
= &mi
->bank
[i
];
134 unsigned long start
, end
;
136 start
= bank_pfn_start(bank
);
137 end
= bank_pfn_end(bank
);
151 * FIXME: We really want to avoid allocating the bootmap bitmap
152 * over the top of the initrd. Hopefully, this is located towards
153 * the start of a bank, so if we allocate the bootmap bitmap at
154 * the end, we won't clash.
156 static unsigned int __init
157 find_bootmap_pfn(int node
, struct meminfo
*mi
, unsigned int bootmap_pages
)
159 unsigned int start_pfn
, i
, bootmap_pfn
;
161 start_pfn
= PAGE_ALIGN(__pa(_end
)) >> PAGE_SHIFT
;
164 for_each_nodebank(i
, mi
, node
) {
165 struct membank
*bank
= &mi
->bank
[i
];
166 unsigned int start
, end
;
168 start
= bank_pfn_start(bank
);
169 end
= bank_pfn_end(bank
);
174 if (start
< start_pfn
)
180 if (end
- start
>= bootmap_pages
) {
186 if (bootmap_pfn
== 0)
192 static int __init
check_initrd(struct meminfo
*mi
)
194 int initrd_node
= -2;
195 #ifdef CONFIG_BLK_DEV_INITRD
196 unsigned long end
= phys_initrd_start
+ phys_initrd_size
;
199 * Make sure that the initrd is within a valid area of
202 if (phys_initrd_size
) {
207 for (i
= 0; i
< mi
->nr_banks
; i
++) {
208 struct membank
*bank
= &mi
->bank
[i
];
209 if (bank_phys_start(bank
) <= phys_initrd_start
&&
210 end
<= bank_phys_end(bank
))
211 initrd_node
= bank
->node
;
215 if (initrd_node
== -1) {
216 printk(KERN_ERR
"INITRD: 0x%08lx+0x%08lx extends beyond "
217 "physical memory - disabling initrd\n",
218 phys_initrd_start
, phys_initrd_size
);
219 phys_initrd_start
= phys_initrd_size
= 0;
226 static inline void map_memory_bank(struct membank
*bank
)
231 map
.pfn
= bank_pfn_start(bank
);
232 map
.virtual = __phys_to_virt(bank_phys_start(bank
));
233 map
.length
= bank_phys_size(bank
);
234 map
.type
= MT_MEMORY
;
236 create_mapping(&map
);
240 static void __init
bootmem_init_node(int node
, struct meminfo
*mi
,
241 unsigned long start_pfn
, unsigned long end_pfn
)
243 unsigned long boot_pfn
;
244 unsigned int boot_pages
;
249 * Map the memory banks for this node.
251 for_each_nodebank(i
, mi
, node
) {
252 struct membank
*bank
= &mi
->bank
[i
];
255 map_memory_bank(bank
);
259 * Allocate the bootmem bitmap page.
261 boot_pages
= bootmem_bootmap_pages(end_pfn
- start_pfn
);
262 boot_pfn
= find_bootmap_pfn(node
, mi
, boot_pages
);
265 * Initialise the bootmem allocator for this node, handing the
266 * memory banks over to bootmem.
268 node_set_online(node
);
269 pgdat
= NODE_DATA(node
);
270 init_bootmem_node(pgdat
, boot_pfn
, start_pfn
, end_pfn
);
272 for_each_nodebank(i
, mi
, node
) {
273 struct membank
*bank
= &mi
->bank
[i
];
275 free_bootmem_node(pgdat
, bank_phys_start(bank
), bank_phys_size(bank
));
276 memory_present(node
, bank_pfn_start(bank
), bank_pfn_end(bank
));
280 * Reserve the bootmem bitmap for this node.
282 reserve_bootmem_node(pgdat
, boot_pfn
<< PAGE_SHIFT
,
283 boot_pages
<< PAGE_SHIFT
, BOOTMEM_DEFAULT
);
286 static void __init
bootmem_reserve_initrd(int node
)
288 #ifdef CONFIG_BLK_DEV_INITRD
289 pg_data_t
*pgdat
= NODE_DATA(node
);
292 res
= reserve_bootmem_node(pgdat
, phys_initrd_start
,
293 phys_initrd_size
, BOOTMEM_EXCLUSIVE
);
296 initrd_start
= __phys_to_virt(phys_initrd_start
);
297 initrd_end
= initrd_start
+ phys_initrd_size
;
300 "INITRD: 0x%08lx+0x%08lx overlaps in-use "
301 "memory region - disabling initrd\n",
302 phys_initrd_start
, phys_initrd_size
);
307 static void __init
bootmem_free_node(int node
, struct meminfo
*mi
)
309 unsigned long zone_size
[MAX_NR_ZONES
], zhole_size
[MAX_NR_ZONES
];
310 unsigned long min
, max_low
, max_high
;
313 find_node_limits(node
, mi
, &min
, &max_low
, &max_high
);
316 * initialise the zones within this node.
318 memset(zone_size
, 0, sizeof(zone_size
));
321 * The size of this node has already been determined. If we need
322 * to do anything fancy with the allocation of this memory to the
323 * zones, now is the time to do it.
325 zone_size
[0] = max_low
- min
;
326 #ifdef CONFIG_HIGHMEM
327 zone_size
[ZONE_HIGHMEM
] = max_high
- max_low
;
331 * For each bank in this node, calculate the size of the holes.
332 * holes = node_size - sum(bank_sizes_in_node)
334 memcpy(zhole_size
, zone_size
, sizeof(zhole_size
));
335 for_each_nodebank(i
, mi
, node
) {
337 #ifdef CONFIG_HIGHMEM
338 if (mi
->bank
[i
].highmem
)
341 zhole_size
[idx
] -= bank_pfn_size(&mi
->bank
[i
]);
345 * Adjust the sizes according to any special requirements for
348 arch_adjust_zones(node
, zone_size
, zhole_size
);
350 free_area_init_node(node
, zone_size
, min
, zhole_size
);
353 #ifndef CONFIG_SPARSEMEM
354 int pfn_valid(unsigned long pfn
)
356 struct meminfo
*mi
= &meminfo
;
357 unsigned int left
= 0, right
= mi
->nr_banks
;
360 unsigned int mid
= (right
+ left
) / 2;
361 struct membank
*bank
= &mi
->bank
[mid
];
363 if (pfn
< bank_pfn_start(bank
))
365 else if (pfn
>= bank_pfn_end(bank
))
369 } while (left
< right
);
372 EXPORT_SYMBOL(pfn_valid
);
375 static int __init
meminfo_cmp(const void *_a
, const void *_b
)
377 const struct membank
*a
= _a
, *b
= _b
;
378 long cmp
= bank_pfn_start(a
) - bank_pfn_start(b
);
379 return cmp
< 0 ? -1 : cmp
> 0 ? 1 : 0;
382 void __init
bootmem_init(void)
384 struct meminfo
*mi
= &meminfo
;
385 unsigned long min
, max_low
, max_high
;
386 int node
, initrd_node
;
388 sort(&mi
->bank
, mi
->nr_banks
, sizeof(mi
->bank
[0]), meminfo_cmp
, NULL
);
391 * Locate which node contains the ramdisk image, if any.
393 initrd_node
= check_initrd(mi
);
395 max_low
= max_high
= 0;
398 * Run through each node initialising the bootmem allocator.
400 for_each_node(node
) {
401 unsigned long node_low
, node_high
;
403 find_node_limits(node
, mi
, &min
, &node_low
, &node_high
);
405 if (node_low
> max_low
)
407 if (node_high
> max_high
)
408 max_high
= node_high
;
411 * If there is no memory in this node, ignore it.
412 * (We can't have nodes which have no lowmem)
417 bootmem_init_node(node
, mi
, min
, node_low
);
420 * Reserve any special node zero regions.
423 reserve_node_zero(NODE_DATA(node
));
426 * If the initrd is in this node, reserve its memory.
428 if (node
== initrd_node
)
429 bootmem_reserve_initrd(node
);
433 * sparse_init() needs the bootmem allocator up and running.
438 * Now free memory in each node - free_area_init_node needs
439 * the sparse mem_map arrays initialized by sparse_init()
440 * for memmap_init_zone(), otherwise all PFNs are invalid.
443 bootmem_free_node(node
, mi
);
445 high_memory
= __va((max_low
<< PAGE_SHIFT
) - 1) + 1;
448 * This doesn't seem to be used by the Linux memory manager any
449 * more, but is used by ll_rw_block. If we can get rid of it, we
450 * also get rid of some of the stuff above as well.
452 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
453 * the system, not the maximum PFN.
455 max_low_pfn
= max_low
- PHYS_PFN_OFFSET
;
456 max_pfn
= max_high
- PHYS_PFN_OFFSET
;
459 static inline int free_area(unsigned long pfn
, unsigned long end
, char *s
)
461 unsigned int pages
= 0, size
= (end
- pfn
) << (PAGE_SHIFT
- 10);
463 for (; pfn
< end
; pfn
++) {
464 struct page
*page
= pfn_to_page(pfn
);
465 ClearPageReserved(page
);
466 init_page_count(page
);
472 printk(KERN_INFO
"Freeing %s memory: %dK\n", s
, size
);
478 free_memmap(int node
, unsigned long start_pfn
, unsigned long end_pfn
)
480 struct page
*start_pg
, *end_pg
;
481 unsigned long pg
, pgend
;
484 * Convert start_pfn/end_pfn to a struct page pointer.
486 start_pg
= pfn_to_page(start_pfn
- 1) + 1;
487 end_pg
= pfn_to_page(end_pfn
);
490 * Convert to physical addresses, and
491 * round start upwards and end downwards.
493 pg
= PAGE_ALIGN(__pa(start_pg
));
494 pgend
= __pa(end_pg
) & PAGE_MASK
;
497 * If there are free pages between these,
498 * free the section of the memmap array.
501 free_bootmem_node(NODE_DATA(node
), pg
, pgend
- pg
);
505 * The mem_map array can get very big. Free the unused area of the memory map.
507 static void __init
free_unused_memmap_node(int node
, struct meminfo
*mi
)
509 unsigned long bank_start
, prev_bank_end
= 0;
513 * [FIXME] This relies on each bank being in address order. This
514 * may not be the case, especially if the user has provided the
515 * information on the command line.
517 for_each_nodebank(i
, mi
, node
) {
518 struct membank
*bank
= &mi
->bank
[i
];
520 bank_start
= bank_pfn_start(bank
);
521 if (bank_start
< prev_bank_end
) {
522 printk(KERN_ERR
"MEM: unordered memory banks. "
523 "Not freeing memmap.\n");
528 * If we had a previous bank, and there is a space
529 * between the current bank and the previous, free it.
531 if (prev_bank_end
&& prev_bank_end
!= bank_start
)
532 free_memmap(node
, prev_bank_end
, bank_start
);
534 prev_bank_end
= bank_pfn_end(bank
);
539 * mem_init() marks the free areas in the mem_map and tells us how much
540 * memory is free. This is done after various parts of the system have
541 * claimed their memory after the kernel image.
543 void __init
mem_init(void)
545 unsigned int codesize
, datasize
, initsize
;
548 #ifndef CONFIG_DISCONTIGMEM
549 max_mapnr
= pfn_to_page(max_pfn
+ PHYS_PFN_OFFSET
) - mem_map
;
552 /* this will put all unused low memory onto the freelists */
553 for_each_online_node(node
) {
554 pg_data_t
*pgdat
= NODE_DATA(node
);
556 free_unused_memmap_node(node
, &meminfo
);
558 if (pgdat
->node_spanned_pages
!= 0)
559 totalram_pages
+= free_all_bootmem_node(pgdat
);
563 /* now that our DMA memory is actually so designated, we can free it */
564 totalram_pages
+= free_area(PHYS_PFN_OFFSET
,
565 __phys_to_pfn(__pa(swapper_pg_dir
)), NULL
);
568 #ifdef CONFIG_HIGHMEM
569 /* set highmem page free */
570 for_each_online_node(node
) {
571 for_each_nodebank (i
, &meminfo
, node
) {
572 unsigned long start
= bank_pfn_start(&meminfo
.bank
[i
]);
573 unsigned long end
= bank_pfn_end(&meminfo
.bank
[i
]);
574 if (start
>= max_low_pfn
+ PHYS_PFN_OFFSET
)
575 totalhigh_pages
+= free_area(start
, end
, NULL
);
578 totalram_pages
+= totalhigh_pages
;
582 * Since our memory may not be contiguous, calculate the
583 * real number of pages we have in this system
585 printk(KERN_INFO
"Memory:");
587 for (i
= 0; i
< meminfo
.nr_banks
; i
++) {
588 num_physpages
+= bank_pfn_size(&meminfo
.bank
[i
]);
589 printk(" %ldMB", bank_phys_size(&meminfo
.bank
[i
]) >> 20);
591 printk(" = %luMB total\n", num_physpages
>> (20 - PAGE_SHIFT
));
593 codesize
= _etext
- _text
;
594 datasize
= _end
- _data
;
595 initsize
= __init_end
- __init_begin
;
597 printk(KERN_NOTICE
"Memory: %luKB available (%dK code, "
598 "%dK data, %dK init, %luK highmem)\n",
599 nr_free_pages() << (PAGE_SHIFT
-10), codesize
>> 10,
600 datasize
>> 10, initsize
>> 10,
601 (unsigned long) (totalhigh_pages
<< (PAGE_SHIFT
-10)));
603 if (PAGE_SIZE
>= 16384 && num_physpages
<= 128) {
604 extern int sysctl_overcommit_memory
;
606 * On a machine this small we won't get
607 * anywhere without overcommit, so turn
610 sysctl_overcommit_memory
= OVERCOMMIT_ALWAYS
;
614 void free_initmem(void)
616 #ifdef CONFIG_HAVE_TCM
617 extern char *__tcm_start
, *__tcm_end
;
619 totalram_pages
+= free_area(__phys_to_pfn(__pa(__tcm_start
)),
620 __phys_to_pfn(__pa(__tcm_end
)),
624 if (!machine_is_integrator() && !machine_is_cintegrator())
625 totalram_pages
+= free_area(__phys_to_pfn(__pa(__init_begin
)),
626 __phys_to_pfn(__pa(__init_end
)),
630 #ifdef CONFIG_BLK_DEV_INITRD
632 static int keep_initrd
;
634 void free_initrd_mem(unsigned long start
, unsigned long end
)
637 totalram_pages
+= free_area(__phys_to_pfn(__pa(start
)),
638 __phys_to_pfn(__pa(end
)),
642 static int __init
keepinitrd_setup(char *__unused
)
648 __setup("keepinitrd", keepinitrd_setup
);