2 * linux/arch/arm/mm/init.c
4 * Copyright (C) 1995-2002 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/config.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/ptrace.h>
14 #include <linux/swap.h>
15 #include <linux/init.h>
16 #include <linux/bootmem.h>
17 #include <linux/mman.h>
18 #include <linux/nodemask.h>
19 #include <linux/initrd.h>
21 #include <asm/mach-types.h>
22 #include <asm/hardware.h>
23 #include <asm/setup.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach/map.h>
29 #define TABLE_SIZE (2 * PTRS_PER_PTE * sizeof(pte_t))
31 DEFINE_PER_CPU(struct mmu_gather
, mmu_gathers
);
33 extern pgd_t swapper_pg_dir
[PTRS_PER_PGD
];
34 extern void _stext
, _text
, _etext
, __data_start
, _end
, __init_begin
, __init_end
;
35 extern unsigned long phys_initrd_start
;
36 extern unsigned long phys_initrd_size
;
39 * The sole use of this is to pass memory configuration
40 * data from paging_init to mem_init.
42 static struct meminfo meminfo __initdata
= { 0, };
45 * empty_zero_page is a special page that is used for
46 * zero-initialized data and COW.
48 struct page
*empty_zero_page
;
52 int free
= 0, total
= 0, reserved
= 0;
53 int shared
= 0, cached
= 0, slab
= 0, node
;
55 printk("Mem-info:\n");
57 printk("Free swap: %6ldkB\n", nr_swap_pages
<<(PAGE_SHIFT
-10));
59 for_each_online_node(node
) {
60 struct page
*page
, *end
;
62 page
= NODE_MEM_MAP(node
);
63 end
= page
+ NODE_DATA(node
)->node_spanned_pages
;
67 if (PageReserved(page
))
69 else if (PageSwapCache(page
))
71 else if (PageSlab(page
))
73 else if (!page_count(page
))
76 shared
+= page_count(page
) - 1;
81 printk("%d pages of RAM\n", total
);
82 printk("%d free pages\n", free
);
83 printk("%d reserved pages\n", reserved
);
84 printk("%d slab pages\n", slab
);
85 printk("%d pages shared\n", shared
);
86 printk("%d pages swap cached\n", cached
);
95 #define O_PFN_DOWN(x) ((x) >> PAGE_SHIFT)
96 #define O_PFN_UP(x) (PAGE_ALIGN(x) >> PAGE_SHIFT)
99 * FIXME: We really want to avoid allocating the bootmap bitmap
100 * over the top of the initrd. Hopefully, this is located towards
101 * the start of a bank, so if we allocate the bootmap bitmap at
102 * the end, we won't clash.
104 static unsigned int __init
105 find_bootmap_pfn(int node
, struct meminfo
*mi
, unsigned int bootmap_pages
)
107 unsigned int start_pfn
, bank
, bootmap_pfn
;
109 start_pfn
= O_PFN_UP(__pa(&_end
));
112 for (bank
= 0; bank
< mi
->nr_banks
; bank
++) {
113 unsigned int start
, end
;
115 if (mi
->bank
[bank
].node
!= node
)
118 start
= mi
->bank
[bank
].start
>> PAGE_SHIFT
;
119 end
= (mi
->bank
[bank
].size
+
120 mi
->bank
[bank
].start
) >> PAGE_SHIFT
;
125 if (start
< start_pfn
)
131 if (end
- start
>= bootmap_pages
) {
137 if (bootmap_pfn
== 0)
144 * Scan the memory info structure and pull out:
145 * - the end of memory
146 * - the number of nodes
147 * - the pfn range of each node
148 * - the number of bootmem bitmap pages
150 static unsigned int __init
151 find_memend_and_nodes(struct meminfo
*mi
, struct node_info
*np
)
153 unsigned int i
, bootmem_pages
= 0, memend_pfn
= 0;
155 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
158 np
[i
].bootmap_pages
= 0;
161 for (i
= 0; i
< mi
->nr_banks
; i
++) {
162 unsigned long start
, end
;
165 if (mi
->bank
[i
].size
== 0) {
167 * Mark this bank with an invalid node number
169 mi
->bank
[i
].node
= -1;
173 node
= mi
->bank
[i
].node
;
176 * Make sure we haven't exceeded the maximum number of nodes
177 * that we have in this configuration. If we have, we're in
178 * trouble. (maybe we ought to limit, instead of bugging?)
180 if (node
>= MAX_NUMNODES
)
182 node_set_online(node
);
185 * Get the start and end pfns for this bank
187 start
= mi
->bank
[i
].start
>> PAGE_SHIFT
;
188 end
= (mi
->bank
[i
].start
+ mi
->bank
[i
].size
) >> PAGE_SHIFT
;
190 if (np
[node
].start
> start
)
191 np
[node
].start
= start
;
193 if (np
[node
].end
< end
)
196 if (memend_pfn
< end
)
201 * Calculate the number of pages we require to
202 * store the bootmem bitmaps.
204 for_each_online_node(i
) {
208 np
[i
].bootmap_pages
= bootmem_bootmap_pages(np
[i
].end
-
210 bootmem_pages
+= np
[i
].bootmap_pages
;
213 high_memory
= __va(memend_pfn
<< PAGE_SHIFT
);
216 * This doesn't seem to be used by the Linux memory
217 * manager any more. If we can get rid of it, we
218 * also get rid of some of the stuff above as well.
220 * Note: max_low_pfn and max_pfn reflect the number
221 * of _pages_ in the system, not the maximum PFN.
223 max_low_pfn
= memend_pfn
- O_PFN_DOWN(PHYS_OFFSET
);
224 max_pfn
= memend_pfn
- O_PFN_DOWN(PHYS_OFFSET
);
226 return bootmem_pages
;
229 static int __init
check_initrd(struct meminfo
*mi
)
231 int initrd_node
= -2;
232 #ifdef CONFIG_BLK_DEV_INITRD
233 unsigned long end
= phys_initrd_start
+ phys_initrd_size
;
236 * Make sure that the initrd is within a valid area of
239 if (phys_initrd_size
) {
244 for (i
= 0; i
< mi
->nr_banks
; i
++) {
245 unsigned long bank_end
;
247 bank_end
= mi
->bank
[i
].start
+ mi
->bank
[i
].size
;
249 if (mi
->bank
[i
].start
<= phys_initrd_start
&&
251 initrd_node
= mi
->bank
[i
].node
;
255 if (initrd_node
== -1) {
256 printk(KERN_ERR
"initrd (0x%08lx - 0x%08lx) extends beyond "
257 "physical memory - disabling initrd\n",
258 phys_initrd_start
, end
);
259 phys_initrd_start
= phys_initrd_size
= 0;
267 * Reserve the various regions of node 0
269 static __init
void reserve_node_zero(unsigned int bootmap_pfn
, unsigned int bootmap_pages
)
271 pg_data_t
*pgdat
= NODE_DATA(0);
272 unsigned long res_size
= 0;
275 * Register the kernel text and data with bootmem.
276 * Note that this can only be in node 0.
278 #ifdef CONFIG_XIP_KERNEL
279 reserve_bootmem_node(pgdat
, __pa(&__data_start
), &_end
- &__data_start
);
281 reserve_bootmem_node(pgdat
, __pa(&_stext
), &_end
- &_stext
);
285 * Reserve the page tables. These are already in use,
286 * and can only be in node 0.
288 reserve_bootmem_node(pgdat
, __pa(swapper_pg_dir
),
289 PTRS_PER_PGD
* sizeof(pgd_t
));
292 * And don't forget to reserve the allocator bitmap,
293 * which will be freed later.
295 reserve_bootmem_node(pgdat
, bootmap_pfn
<< PAGE_SHIFT
,
296 bootmap_pages
<< PAGE_SHIFT
);
299 * Hmm... This should go elsewhere, but we really really need to
300 * stop things allocating the low memory; ideally we need a better
301 * implementation of GFP_DMA which does not assume that DMA-able
302 * memory starts at zero.
304 if (machine_is_integrator() || machine_is_cintegrator())
305 res_size
= __pa(swapper_pg_dir
) - PHYS_OFFSET
;
308 * These should likewise go elsewhere. They pre-reserve the
309 * screen memory region at the start of main system memory.
311 if (machine_is_edb7211())
312 res_size
= 0x00020000;
313 if (machine_is_p720t())
314 res_size
= 0x00014000;
318 * Because of the SA1111 DMA bug, we want to preserve our
319 * precious DMA-able memory...
321 res_size
= __pa(swapper_pg_dir
) - PHYS_OFFSET
;
324 reserve_bootmem_node(pgdat
, PHYS_OFFSET
, res_size
);
328 * Register all available RAM in this node with the bootmem allocator.
330 static inline void free_bootmem_node_bank(int node
, struct meminfo
*mi
)
332 pg_data_t
*pgdat
= NODE_DATA(node
);
335 for (bank
= 0; bank
< mi
->nr_banks
; bank
++)
336 if (mi
->bank
[bank
].node
== node
)
337 free_bootmem_node(pgdat
, mi
->bank
[bank
].start
,
338 mi
->bank
[bank
].size
);
342 * Initialise the bootmem allocator for all nodes. This is called
343 * early during the architecture specific initialisation.
345 static void __init
bootmem_init(struct meminfo
*mi
)
347 struct node_info node_info
[MAX_NUMNODES
], *np
= node_info
;
348 unsigned int bootmap_pages
, bootmap_pfn
, map_pg
;
349 int node
, initrd_node
;
351 bootmap_pages
= find_memend_and_nodes(mi
, np
);
352 bootmap_pfn
= find_bootmap_pfn(0, mi
, bootmap_pages
);
353 initrd_node
= check_initrd(mi
);
355 map_pg
= bootmap_pfn
;
358 * Initialise the bootmem nodes.
360 * What we really want to do is:
362 * unmap_all_regions_except_kernel();
363 * for_each_node_in_reverse_order(node) {
365 * allocate_bootmem_map(node);
366 * init_bootmem_node(node);
367 * free_bootmem_node(node);
370 * but this is a 2.5-type change. For now, we just set
371 * the nodes up in reverse order.
373 * (we could also do with rolling bootmem_init and paging_init
374 * into one generic "memory_init" type function).
376 np
+= num_online_nodes() - 1;
377 for (node
= num_online_nodes() - 1; node
>= 0; node
--, np
--) {
379 * If there are no pages in this node, ignore it.
380 * Note that node 0 must always have some pages.
382 if (np
->end
== 0 || !node_online(node
)) {
389 * Initialise the bootmem allocator.
391 init_bootmem_node(NODE_DATA(node
), map_pg
, np
->start
, np
->end
);
392 free_bootmem_node_bank(node
, mi
);
393 map_pg
+= np
->bootmap_pages
;
396 * If this is node 0, we need to reserve some areas ASAP -
397 * we may use bootmem on node 0 to setup the other nodes.
400 reserve_node_zero(bootmap_pfn
, bootmap_pages
);
404 #ifdef CONFIG_BLK_DEV_INITRD
405 if (phys_initrd_size
&& initrd_node
>= 0) {
406 reserve_bootmem_node(NODE_DATA(initrd_node
), phys_initrd_start
,
408 initrd_start
= __phys_to_virt(phys_initrd_start
);
409 initrd_end
= initrd_start
+ phys_initrd_size
;
413 BUG_ON(map_pg
!= bootmap_pfn
+ bootmap_pages
);
417 * paging_init() sets up the page tables, initialises the zone memory
418 * maps, and sets up the zero page, bad page and bad page tables.
420 void __init
paging_init(struct meminfo
*mi
, struct machine_desc
*mdesc
)
427 memcpy(&meminfo
, mi
, sizeof(meminfo
));
430 * allocate the zero page. Note that we count on this going ok.
432 zero_page
= alloc_bootmem_low_pages(PAGE_SIZE
);
435 * initialise the page tables.
440 local_flush_tlb_all();
443 * initialise the zones within each node
445 for_each_online_node(node
) {
446 unsigned long zone_size
[MAX_NR_ZONES
];
447 unsigned long zhole_size
[MAX_NR_ZONES
];
448 struct bootmem_data
*bdata
;
453 * Initialise the zone size information.
455 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
460 pgdat
= NODE_DATA(node
);
461 bdata
= pgdat
->bdata
;
464 * The size of this node has already been determined.
465 * If we need to do anything fancy with the allocation
466 * of this memory to the zones, now is the time to do
469 zone_size
[0] = bdata
->node_low_pfn
-
470 (bdata
->node_boot_start
>> PAGE_SHIFT
);
473 * If this zone has zero size, skip it.
479 * For each bank in this node, calculate the size of the
480 * holes. holes = node_size - sum(bank_sizes_in_node)
482 zhole_size
[0] = zone_size
[0];
483 for (i
= 0; i
< mi
->nr_banks
; i
++) {
484 if (mi
->bank
[i
].node
!= node
)
487 zhole_size
[0] -= mi
->bank
[i
].size
>> PAGE_SHIFT
;
491 * Adjust the sizes according to any special
492 * requirements for this machine type.
494 arch_adjust_zones(node
, zone_size
, zhole_size
);
496 free_area_init_node(node
, pgdat
, zone_size
,
497 bdata
->node_boot_start
>> PAGE_SHIFT
, zhole_size
);
501 * finish off the bad pages once
502 * the mem_map is initialised
504 memzero(zero_page
, PAGE_SIZE
);
505 empty_zero_page
= virt_to_page(zero_page
);
506 flush_dcache_page(empty_zero_page
);
509 static inline void free_area(unsigned long addr
, unsigned long end
, char *s
)
511 unsigned int size
= (end
- addr
) >> 10;
513 for (; addr
< end
; addr
+= PAGE_SIZE
) {
514 struct page
*page
= virt_to_page(addr
);
515 ClearPageReserved(page
);
516 set_page_count(page
, 1);
522 printk(KERN_INFO
"Freeing %s memory: %dK\n", s
, size
);
526 free_memmap(int node
, unsigned long start_pfn
, unsigned long end_pfn
)
528 struct page
*start_pg
, *end_pg
;
529 unsigned long pg
, pgend
;
532 * Convert start_pfn/end_pfn to a struct page pointer.
534 start_pg
= pfn_to_page(start_pfn
);
535 end_pg
= pfn_to_page(end_pfn
);
538 * Convert to physical addresses, and
539 * round start upwards and end downwards.
541 pg
= PAGE_ALIGN(__pa(start_pg
));
542 pgend
= __pa(end_pg
) & PAGE_MASK
;
545 * If there are free pages between these,
546 * free the section of the memmap array.
549 free_bootmem_node(NODE_DATA(node
), pg
, pgend
- pg
);
553 * The mem_map array can get very big. Free the unused area of the memory map.
555 static void __init
free_unused_memmap_node(int node
, struct meminfo
*mi
)
557 unsigned long bank_start
, prev_bank_end
= 0;
561 * [FIXME] This relies on each bank being in address order. This
562 * may not be the case, especially if the user has provided the
563 * information on the command line.
565 for (i
= 0; i
< mi
->nr_banks
; i
++) {
566 if (mi
->bank
[i
].size
== 0 || mi
->bank
[i
].node
!= node
)
569 bank_start
= mi
->bank
[i
].start
>> PAGE_SHIFT
;
570 if (bank_start
< prev_bank_end
) {
571 printk(KERN_ERR
"MEM: unordered memory banks. "
572 "Not freeing memmap.\n");
577 * If we had a previous bank, and there is a space
578 * between the current bank and the previous, free it.
580 if (prev_bank_end
&& prev_bank_end
!= bank_start
)
581 free_memmap(node
, prev_bank_end
, bank_start
);
583 prev_bank_end
= (mi
->bank
[i
].start
+
584 mi
->bank
[i
].size
) >> PAGE_SHIFT
;
589 * mem_init() marks the free areas in the mem_map and tells us how much
590 * memory is free. This is done after various parts of the system have
591 * claimed their memory after the kernel image.
593 void __init
mem_init(void)
595 unsigned int codepages
, datapages
, initpages
;
598 codepages
= &_etext
- &_text
;
599 datapages
= &_end
- &__data_start
;
600 initpages
= &__init_end
- &__init_begin
;
602 #ifndef CONFIG_DISCONTIGMEM
603 max_mapnr
= virt_to_page(high_memory
) - mem_map
;
606 /* this will put all unused low memory onto the freelists */
607 for_each_online_node(node
) {
608 pg_data_t
*pgdat
= NODE_DATA(node
);
610 free_unused_memmap_node(node
, &meminfo
);
612 if (pgdat
->node_spanned_pages
!= 0)
613 totalram_pages
+= free_all_bootmem_node(pgdat
);
617 /* now that our DMA memory is actually so designated, we can free it */
618 free_area(PAGE_OFFSET
, (unsigned long)swapper_pg_dir
, NULL
);
622 * Since our memory may not be contiguous, calculate the
623 * real number of pages we have in this system
625 printk(KERN_INFO
"Memory:");
628 for (i
= 0; i
< meminfo
.nr_banks
; i
++) {
629 num_physpages
+= meminfo
.bank
[i
].size
>> PAGE_SHIFT
;
630 printk(" %ldMB", meminfo
.bank
[i
].size
>> 20);
633 printk(" = %luMB total\n", num_physpages
>> (20 - PAGE_SHIFT
));
634 printk(KERN_NOTICE
"Memory: %luKB available (%dK code, "
635 "%dK data, %dK init)\n",
636 (unsigned long) nr_free_pages() << (PAGE_SHIFT
-10),
637 codepages
>> 10, datapages
>> 10, initpages
>> 10);
639 if (PAGE_SIZE
>= 16384 && num_physpages
<= 128) {
640 extern int sysctl_overcommit_memory
;
642 * On a machine this small we won't get
643 * anywhere without overcommit, so turn
646 sysctl_overcommit_memory
= OVERCOMMIT_ALWAYS
;
650 void free_initmem(void)
652 if (!machine_is_integrator() && !machine_is_cintegrator()) {
653 free_area((unsigned long)(&__init_begin
),
654 (unsigned long)(&__init_end
),
659 #ifdef CONFIG_BLK_DEV_INITRD
661 static int keep_initrd
;
663 void free_initrd_mem(unsigned long start
, unsigned long end
)
666 free_area(start
, end
, "initrd");
669 static int __init
keepinitrd_setup(char *__unused
)
675 __setup("keepinitrd", keepinitrd_setup
);