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/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/setup.h>
23 #include <asm/sizes.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach/map.h>
29 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
31 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
32 extern void _stext, _text, _etext, __data_start, _end, __init_begin, __init_end;
33 extern unsigned long phys_initrd_start;
34 extern unsigned long phys_initrd_size;
37 * The sole use of this is to pass memory configuration
38 * data from paging_init to mem_init.
40 static struct meminfo meminfo __initdata = { 0, };
43 * empty_zero_page is a special page that is used for
44 * zero-initialized data and COW.
46 struct page *empty_zero_page;
50 int free = 0, total = 0, reserved = 0;
51 int shared = 0, cached = 0, slab = 0, node;
53 printk("Mem-info:\n");
55 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
57 for_each_online_node(node) {
58 struct page *page, *end;
60 page = NODE_MEM_MAP(node);
61 end = page + NODE_DATA(node)->node_spanned_pages;
65 if (PageReserved(page))
67 else if (PageSwapCache(page))
69 else if (PageSlab(page))
71 else if (!page_count(page))
74 shared += page_count(page) - 1;
79 printk("%d pages of RAM\n", total);
80 printk("%d free pages\n", free);
81 printk("%d reserved pages\n", reserved);
82 printk("%d slab pages\n", slab);
83 printk("%d pages shared\n", shared);
84 printk("%d pages swap cached\n", cached);
87 static inline pmd_t *pmd_off(pgd_t *pgd, unsigned long virt)
89 return pmd_offset(pgd, virt);
92 static inline pmd_t *pmd_off_k(unsigned long virt)
94 return pmd_off(pgd_offset_k(virt), virt);
97 #define for_each_nodebank(iter,mi,no) \
98 for (iter = 0; iter < mi->nr_banks; iter++) \
99 if (mi->bank[iter].node == no)
102 * FIXME: We really want to avoid allocating the bootmap bitmap
103 * over the top of the initrd. Hopefully, this is located towards
104 * the start of a bank, so if we allocate the bootmap bitmap at
105 * the end, we won't clash.
107 static unsigned int __init
108 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
110 unsigned int start_pfn, bank, bootmap_pfn;
112 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
115 for_each_nodebank(bank, mi, node) {
116 unsigned int start, end;
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)
143 static int __init check_initrd(struct meminfo *mi)
145 int initrd_node = -2;
146 #ifdef CONFIG_BLK_DEV_INITRD
147 unsigned long end = phys_initrd_start + phys_initrd_size;
150 * Make sure that the initrd is within a valid area of
153 if (phys_initrd_size) {
158 for (i = 0; i < mi->nr_banks; i++) {
159 unsigned long bank_end;
161 bank_end = mi->bank[i].start + mi->bank[i].size;
163 if (mi->bank[i].start <= phys_initrd_start &&
165 initrd_node = mi->bank[i].node;
169 if (initrd_node == -1) {
170 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
171 "physical memory - disabling initrd\n",
172 phys_initrd_start, end);
173 phys_initrd_start = phys_initrd_size = 0;
181 * Reserve the various regions of node 0
183 static __init void reserve_node_zero(pg_data_t *pgdat)
185 unsigned long res_size = 0;
188 * Register the kernel text and data with bootmem.
189 * Note that this can only be in node 0.
191 #ifdef CONFIG_XIP_KERNEL
192 reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start);
194 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
198 * Reserve the page tables. These are already in use,
199 * and can only be in node 0.
201 reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
202 PTRS_PER_PGD * sizeof(pgd_t));
205 * Hmm... This should go elsewhere, but we really really need to
206 * stop things allocating the low memory; ideally we need a better
207 * implementation of GFP_DMA which does not assume that DMA-able
208 * memory starts at zero.
210 if (machine_is_integrator() || machine_is_cintegrator())
211 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
214 * These should likewise go elsewhere. They pre-reserve the
215 * screen memory region at the start of main system memory.
217 if (machine_is_edb7211())
218 res_size = 0x00020000;
219 if (machine_is_p720t())
220 res_size = 0x00014000;
224 * Because of the SA1111 DMA bug, we want to preserve our
225 * precious DMA-able memory...
227 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
230 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
233 void __init build_mem_type_table(void);
234 void __init create_mapping(struct map_desc *md);
236 static unsigned long __init
237 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
239 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
240 unsigned long start_pfn, end_pfn, boot_pfn;
241 unsigned int boot_pages;
249 * Calculate the pfn range, and map the memory banks for this node.
251 for_each_nodebank(i, mi, node) {
252 unsigned long start, end;
255 start = mi->bank[i].start >> PAGE_SHIFT;
256 end = (mi->bank[i].start + mi->bank[i].size) >> PAGE_SHIFT;
258 if (start_pfn > start)
263 map.pfn = __phys_to_pfn(mi->bank[i].start);
264 map.virtual = __phys_to_virt(mi->bank[i].start);
265 map.length = mi->bank[i].size;
266 map.type = MT_MEMORY;
268 create_mapping(&map);
272 * If there is no memory in this node, ignore it.
278 * Allocate the bootmem bitmap page.
280 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
281 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
284 * Initialise the bootmem allocator for this node, handing the
285 * memory banks over to bootmem.
287 node_set_online(node);
288 pgdat = NODE_DATA(node);
289 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
291 for_each_nodebank(i, mi, node)
292 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
295 * Reserve the bootmem bitmap for this node.
297 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
298 boot_pages << PAGE_SHIFT);
300 #ifdef CONFIG_BLK_DEV_INITRD
302 * If the initrd is in this node, reserve its memory.
304 if (node == initrd_node) {
305 reserve_bootmem_node(pgdat, phys_initrd_start,
307 initrd_start = __phys_to_virt(phys_initrd_start);
308 initrd_end = initrd_start + phys_initrd_size;
313 * Finally, reserve any node zero regions.
316 reserve_node_zero(pgdat);
319 * initialise the zones within this node.
321 memset(zone_size, 0, sizeof(zone_size));
322 memset(zhole_size, 0, sizeof(zhole_size));
325 * The size of this node has already been determined. If we need
326 * to do anything fancy with the allocation of this memory to the
327 * zones, now is the time to do it.
329 zone_size[0] = end_pfn - start_pfn;
332 * For each bank in this node, calculate the size of the holes.
333 * holes = node_size - sum(bank_sizes_in_node)
335 zhole_size[0] = zone_size[0];
336 for_each_nodebank(i, mi, node)
337 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
340 * Adjust the sizes according to any special requirements for
343 arch_adjust_zones(node, zone_size, zhole_size);
345 free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
350 static void __init bootmem_init(struct meminfo *mi)
352 unsigned long addr, memend_pfn = 0;
353 int node, initrd_node, i;
356 * Invalidate the node number for empty or invalid memory banks
358 for (i = 0; i < mi->nr_banks; i++)
359 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
360 mi->bank[i].node = -1;
362 memcpy(&meminfo, mi, sizeof(meminfo));
365 * Clear out all the mappings below the kernel image.
367 for (addr = 0; addr < MODULE_START; addr += PGDIR_SIZE)
368 pmd_clear(pmd_off_k(addr));
369 #ifdef CONFIG_XIP_KERNEL
370 /* The XIP kernel is mapped in the module area -- skip over it */
371 addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
373 for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
374 pmd_clear(pmd_off_k(addr));
377 * Clear out all the kernel space mappings, except for the first
378 * memory bank, up to the end of the vmalloc region.
380 for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
381 addr < VMALLOC_END; addr += PGDIR_SIZE)
382 pmd_clear(pmd_off_k(addr));
385 * Locate which node contains the ramdisk image, if any.
387 initrd_node = check_initrd(mi);
390 * Run through each node initialising the bootmem allocator.
392 for_each_node(node) {
393 unsigned long end_pfn;
395 end_pfn = bootmem_init_node(node, initrd_node, mi);
398 * Remember the highest memory PFN.
400 if (end_pfn > memend_pfn)
401 memend_pfn = end_pfn;
404 high_memory = __va(memend_pfn << PAGE_SHIFT);
407 * This doesn't seem to be used by the Linux memory manager any
408 * more, but is used by ll_rw_block. If we can get rid of it, we
409 * also get rid of some of the stuff above as well.
411 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
412 * the system, not the maximum PFN.
414 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
418 * Set up device the mappings. Since we clear out the page tables for all
419 * mappings above VMALLOC_END, we will remove any debug device mappings.
420 * This means you have to be careful how you debug this function, or any
421 * called function. This means you can't use any function or debugging
422 * method which may touch any device, otherwise the kernel _will_ crash.
424 static void __init devicemaps_init(struct machine_desc *mdesc)
431 * Allocate the vector page early.
433 vectors = alloc_bootmem_low_pages(PAGE_SIZE);
436 for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
437 pmd_clear(pmd_off_k(addr));
440 * Map the kernel if it is XIP.
441 * It is always first in the modulearea.
443 #ifdef CONFIG_XIP_KERNEL
444 map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & PGDIR_MASK);
445 map.virtual = MODULE_START;
446 map.length = ((unsigned long)&_etext - map.virtual + ~PGDIR_MASK) & PGDIR_MASK;
448 create_mapping(&map);
452 * Map the cache flushing regions.
455 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
456 map.virtual = FLUSH_BASE;
458 map.type = MT_CACHECLEAN;
459 create_mapping(&map);
461 #ifdef FLUSH_BASE_MINICACHE
462 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
463 map.virtual = FLUSH_BASE_MINICACHE;
465 map.type = MT_MINICLEAN;
466 create_mapping(&map);
470 * Create a mapping for the machine vectors at the high-vectors
471 * location (0xffff0000). If we aren't using high-vectors, also
472 * create a mapping at the low-vectors virtual address.
474 map.pfn = __phys_to_pfn(virt_to_phys(vectors));
475 map.virtual = 0xffff0000;
476 map.length = PAGE_SIZE;
477 map.type = MT_HIGH_VECTORS;
478 create_mapping(&map);
480 if (!vectors_high()) {
482 map.type = MT_LOW_VECTORS;
483 create_mapping(&map);
487 * Ask the machine support to map in the statically mapped devices.
493 * Finally flush the caches and tlb to ensure that we're in a
494 * consistent state wrt the writebuffer. This also ensures that
495 * any write-allocated cache lines in the vector page are written
496 * back. After this point, we can start to touch devices again.
498 local_flush_tlb_all();
503 * paging_init() sets up the page tables, initialises the zone memory
504 * maps, and sets up the zero page, bad page and bad page tables.
506 void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
510 build_mem_type_table();
512 devicemaps_init(mdesc);
514 top_pmd = pmd_off_k(0xffff0000);
517 * allocate the zero page. Note that we count on this going ok.
519 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
520 memzero(zero_page, PAGE_SIZE);
521 empty_zero_page = virt_to_page(zero_page);
522 flush_dcache_page(empty_zero_page);
525 static inline void free_area(unsigned long addr, unsigned long end, char *s)
527 unsigned int size = (end - addr) >> 10;
529 for (; addr < end; addr += PAGE_SIZE) {
530 struct page *page = virt_to_page(addr);
531 ClearPageReserved(page);
532 init_page_count(page);
538 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
542 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
544 struct page *start_pg, *end_pg;
545 unsigned long pg, pgend;
548 * Convert start_pfn/end_pfn to a struct page pointer.
550 start_pg = pfn_to_page(start_pfn);
551 end_pg = pfn_to_page(end_pfn);
554 * Convert to physical addresses, and
555 * round start upwards and end downwards.
557 pg = PAGE_ALIGN(__pa(start_pg));
558 pgend = __pa(end_pg) & PAGE_MASK;
561 * If there are free pages between these,
562 * free the section of the memmap array.
565 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
569 * The mem_map array can get very big. Free the unused area of the memory map.
571 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
573 unsigned long bank_start, prev_bank_end = 0;
577 * [FIXME] This relies on each bank being in address order. This
578 * may not be the case, especially if the user has provided the
579 * information on the command line.
581 for_each_nodebank(i, mi, node) {
582 bank_start = mi->bank[i].start >> PAGE_SHIFT;
583 if (bank_start < prev_bank_end) {
584 printk(KERN_ERR "MEM: unordered memory banks. "
585 "Not freeing memmap.\n");
590 * If we had a previous bank, and there is a space
591 * between the current bank and the previous, free it.
593 if (prev_bank_end && prev_bank_end != bank_start)
594 free_memmap(node, prev_bank_end, bank_start);
596 prev_bank_end = (mi->bank[i].start +
597 mi->bank[i].size) >> PAGE_SHIFT;
602 * mem_init() marks the free areas in the mem_map and tells us how much
603 * memory is free. This is done after various parts of the system have
604 * claimed their memory after the kernel image.
606 void __init mem_init(void)
608 unsigned int codepages, datapages, initpages;
611 codepages = &_etext - &_text;
612 datapages = &_end - &__data_start;
613 initpages = &__init_end - &__init_begin;
615 #ifndef CONFIG_DISCONTIGMEM
616 max_mapnr = virt_to_page(high_memory) - mem_map;
619 /* this will put all unused low memory onto the freelists */
620 for_each_online_node(node) {
621 pg_data_t *pgdat = NODE_DATA(node);
623 free_unused_memmap_node(node, &meminfo);
625 if (pgdat->node_spanned_pages != 0)
626 totalram_pages += free_all_bootmem_node(pgdat);
630 /* now that our DMA memory is actually so designated, we can free it */
631 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
635 * Since our memory may not be contiguous, calculate the
636 * real number of pages we have in this system
638 printk(KERN_INFO "Memory:");
641 for (i = 0; i < meminfo.nr_banks; i++) {
642 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
643 printk(" %ldMB", meminfo.bank[i].size >> 20);
646 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
647 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
648 "%dK data, %dK init)\n",
649 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
650 codepages >> 10, datapages >> 10, initpages >> 10);
652 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
653 extern int sysctl_overcommit_memory;
655 * On a machine this small we won't get
656 * anywhere without overcommit, so turn
659 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
663 void free_initmem(void)
665 if (!machine_is_integrator() && !machine_is_cintegrator()) {
666 free_area((unsigned long)(&__init_begin),
667 (unsigned long)(&__init_end),
672 #ifdef CONFIG_BLK_DEV_INITRD
674 static int keep_initrd;
676 void free_initrd_mem(unsigned long start, unsigned long end)
679 free_area(start, end, "initrd");
682 static int __init keepinitrd_setup(char *__unused)
688 __setup("keepinitrd", keepinitrd_setup);