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/initrd.h>
19 #include <asm/mach-types.h>
20 #include <asm/hardware.h>
21 #include <asm/setup.h>
24 #include <asm/mach/arch.h>
25 #include <asm/mach/map.h>
27 #define TABLE_SIZE (2 * PTRS_PER_PTE * sizeof(pte_t))
29 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
31 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
32 extern char _stext, _text, _etext, _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 (node = 0; node < numnodes; 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);
93 #define O_PFN_DOWN(x) ((x) >> PAGE_SHIFT)
94 #define V_PFN_DOWN(x) O_PFN_DOWN(__pa(x))
96 #define O_PFN_UP(x) (PAGE_ALIGN(x) >> PAGE_SHIFT)
97 #define V_PFN_UP(x) O_PFN_UP(__pa(x))
99 #define PFN_SIZE(x) ((x) >> PAGE_SHIFT)
100 #define PFN_RANGE(s,e) PFN_SIZE(PAGE_ALIGN((unsigned long)(e)) - \
101 (((unsigned long)(s)) & PAGE_MASK))
104 * FIXME: We really want to avoid allocating the bootmap bitmap
105 * over the top of the initrd. Hopefully, this is located towards
106 * the start of a bank, so if we allocate the bootmap bitmap at
107 * the end, we won't clash.
109 static unsigned int __init
110 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
112 unsigned int start_pfn, bank, bootmap_pfn;
114 start_pfn = V_PFN_UP(&_end);
117 for (bank = 0; bank < mi->nr_banks; bank ++) {
118 unsigned int start, end;
120 if (mi->bank[bank].node != node)
123 start = O_PFN_UP(mi->bank[bank].start);
124 end = O_PFN_DOWN(mi->bank[bank].size +
125 mi->bank[bank].start);
130 if (start < start_pfn)
136 if (end - start >= bootmap_pages) {
142 if (bootmap_pfn == 0)
149 * Scan the memory info structure and pull out:
150 * - the end of memory
151 * - the number of nodes
152 * - the pfn range of each node
153 * - the number of bootmem bitmap pages
155 static unsigned int __init
156 find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
158 unsigned int i, bootmem_pages = 0, memend_pfn = 0;
160 for (i = 0; i < MAX_NUMNODES; i++) {
163 np[i].bootmap_pages = 0;
166 for (i = 0; i < mi->nr_banks; i++) {
167 unsigned long start, end;
170 if (mi->bank[i].size == 0) {
172 * Mark this bank with an invalid node number
174 mi->bank[i].node = -1;
178 node = mi->bank[i].node;
180 if (node >= numnodes) {
184 * Make sure we haven't exceeded the maximum number
185 * of nodes that we have in this configuration. If
186 * we have, we're in trouble. (maybe we ought to
187 * limit, instead of bugging?)
189 if (numnodes > MAX_NUMNODES)
194 * Get the start and end pfns for this bank
196 start = O_PFN_UP(mi->bank[i].start);
197 end = O_PFN_DOWN(mi->bank[i].start + mi->bank[i].size);
199 if (np[node].start > start)
200 np[node].start = start;
202 if (np[node].end < end)
205 if (memend_pfn < end)
210 * Calculate the number of pages we require to
211 * store the bootmem bitmaps.
213 for (i = 0; i < numnodes; i++) {
217 np[i].bootmap_pages = bootmem_bootmap_pages(np[i].end -
219 bootmem_pages += np[i].bootmap_pages;
222 high_memory = __va(memend_pfn << PAGE_SHIFT);
225 * This doesn't seem to be used by the Linux memory
226 * manager any more. If we can get rid of it, we
227 * also get rid of some of the stuff above as well.
229 max_low_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
230 max_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
232 return bootmem_pages;
235 static int __init check_initrd(struct meminfo *mi)
237 int initrd_node = -2;
238 #ifdef CONFIG_BLK_DEV_INITRD
239 unsigned long end = phys_initrd_start + phys_initrd_size;
242 * Make sure that the initrd is within a valid area of
245 if (phys_initrd_size) {
250 for (i = 0; i < mi->nr_banks; i++) {
251 unsigned long bank_end;
253 bank_end = mi->bank[i].start + mi->bank[i].size;
255 if (mi->bank[i].start <= phys_initrd_start &&
257 initrd_node = mi->bank[i].node;
261 if (initrd_node == -1) {
262 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
263 "physical memory - disabling initrd\n",
264 phys_initrd_start, end);
265 phys_initrd_start = phys_initrd_size = 0;
273 * Reserve the various regions of node 0
275 static __init void reserve_node_zero(unsigned int bootmap_pfn, unsigned int bootmap_pages)
277 pg_data_t *pgdat = NODE_DATA(0);
278 unsigned long res_size = 0;
281 * Register the kernel text and data with bootmem.
282 * Note that this can only be in node 0.
284 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
287 * Reserve the page tables. These are already in use,
288 * and can only be in node 0.
290 reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
291 PTRS_PER_PGD * sizeof(pgd_t));
294 * And don't forget to reserve the allocator bitmap,
295 * which will be freed later.
297 reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
298 bootmap_pages << PAGE_SHIFT);
301 * Hmm... This should go elsewhere, but we really really need to
302 * stop things allocating the low memory; ideally we need a better
303 * implementation of GFP_DMA which does not assume that DMA-able
304 * memory starts at zero.
306 if (machine_is_integrator() || machine_is_cintegrator())
307 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
310 * These should likewise go elsewhere. They pre-reserve the
311 * screen memory region at the start of main system memory.
313 if (machine_is_edb7211())
314 res_size = 0x00020000;
315 if (machine_is_p720t())
316 res_size = 0x00014000;
320 * Because of the SA1111 DMA bug, we want to preserve our
321 * precious DMA-able memory...
323 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
326 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
330 * Register all available RAM in this node with the bootmem allocator.
332 static inline void free_bootmem_node_bank(int node, struct meminfo *mi)
334 pg_data_t *pgdat = NODE_DATA(node);
337 for (bank = 0; bank < mi->nr_banks; bank++)
338 if (mi->bank[bank].node == node)
339 free_bootmem_node(pgdat, mi->bank[bank].start,
340 mi->bank[bank].size);
344 * Initialise the bootmem allocator for all nodes. This is called
345 * early during the architecture specific initialisation.
347 void __init bootmem_init(struct meminfo *mi)
349 struct node_info node_info[MAX_NUMNODES], *np = node_info;
350 unsigned int bootmap_pages, bootmap_pfn, map_pg;
351 int node, initrd_node;
353 bootmap_pages = find_memend_and_nodes(mi, np);
354 bootmap_pfn = find_bootmap_pfn(0, mi, bootmap_pages);
355 initrd_node = check_initrd(mi);
357 map_pg = bootmap_pfn;
360 * Initialise the bootmem nodes.
362 * What we really want to do is:
364 * unmap_all_regions_except_kernel();
365 * for_each_node_in_reverse_order(node) {
367 * allocate_bootmem_map(node);
368 * init_bootmem_node(node);
369 * free_bootmem_node(node);
372 * but this is a 2.5-type change. For now, we just set
373 * the nodes up in reverse order.
375 * (we could also do with rolling bootmem_init and paging_init
376 * into one generic "memory_init" type function).
379 for (node = numnodes - 1; node >= 0; node--, np--) {
381 * If there are no pages in this node, ignore it.
382 * Note that node 0 must always have some pages.
391 * Initialise the bootmem allocator.
393 init_bootmem_node(NODE_DATA(node), map_pg, np->start, np->end);
394 free_bootmem_node_bank(node, mi);
395 map_pg += np->bootmap_pages;
398 * If this is node 0, we need to reserve some areas ASAP -
399 * we may use bootmem on node 0 to setup the other nodes.
402 reserve_node_zero(bootmap_pfn, bootmap_pages);
406 #ifdef CONFIG_BLK_DEV_INITRD
407 if (phys_initrd_size && initrd_node >= 0) {
408 reserve_bootmem_node(NODE_DATA(initrd_node), phys_initrd_start,
410 initrd_start = __phys_to_virt(phys_initrd_start);
411 initrd_end = initrd_start + phys_initrd_size;
415 if (map_pg != bootmap_pfn + bootmap_pages)
421 * paging_init() sets up the page tables, initialises the zone memory
422 * maps, and sets up the zero page, bad page and bad page tables.
424 void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
429 memcpy(&meminfo, mi, sizeof(meminfo));
432 * allocate the zero page. Note that we count on this going ok.
434 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
437 * initialise the page tables.
445 * initialise the zones within each node
447 for (node = 0; node < numnodes; node++) {
448 unsigned long zone_size[MAX_NR_ZONES];
449 unsigned long zhole_size[MAX_NR_ZONES];
450 struct bootmem_data *bdata;
455 * Initialise the zone size information.
457 for (i = 0; i < MAX_NR_ZONES; i++) {
462 pgdat = NODE_DATA(node);
463 bdata = pgdat->bdata;
466 * The size of this node has already been determined.
467 * If we need to do anything fancy with the allocation
468 * of this memory to the zones, now is the time to do
471 zone_size[0] = bdata->node_low_pfn -
472 (bdata->node_boot_start >> PAGE_SHIFT);
475 * If this zone has zero size, skip it.
481 * For each bank in this node, calculate the size of the
482 * holes. holes = node_size - sum(bank_sizes_in_node)
484 zhole_size[0] = zone_size[0];
485 for (i = 0; i < mi->nr_banks; i++) {
486 if (mi->bank[i].node != node)
489 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
493 * Adjust the sizes according to any special
494 * requirements for this machine type.
496 arch_adjust_zones(node, zone_size, zhole_size);
498 free_area_init_node(node, pgdat, NULL, zone_size,
499 bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
502 #ifndef CONFIG_DISCONTIGMEM
503 mem_map = contig_page_data.node_mem_map;
507 * finish off the bad pages once
508 * the mem_map is initialised
510 memzero(zero_page, PAGE_SIZE);
511 empty_zero_page = virt_to_page(zero_page);
512 flush_dcache_page(empty_zero_page);
515 static inline void free_area(unsigned long addr, unsigned long end, char *s)
517 unsigned int size = (end - addr) >> 10;
519 for (; addr < end; addr += PAGE_SIZE) {
520 struct page *page = virt_to_page(addr);
521 ClearPageReserved(page);
522 set_page_count(page, 1);
528 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
532 * mem_init() marks the free areas in the mem_map and tells us how much
533 * memory is free. This is done after various parts of the system have
534 * claimed their memory after the kernel image.
536 void __init mem_init(void)
538 unsigned int codepages, datapages, initpages;
541 codepages = &_etext - &_text;
542 datapages = &_end - &_etext;
543 initpages = &__init_end - &__init_begin;
545 #ifndef CONFIG_DISCONTIGMEM
546 max_mapnr = virt_to_page(high_memory) - mem_map;
550 * We may have non-contiguous memory.
552 if (meminfo.nr_banks != 1)
553 create_memmap_holes(&meminfo);
555 /* this will put all unused low memory onto the freelists */
556 for (node = 0; node < numnodes; node++) {
557 pg_data_t *pgdat = NODE_DATA(node);
559 if (pgdat->node_spanned_pages != 0)
560 totalram_pages += free_all_bootmem_node(pgdat);
564 /* now that our DMA memory is actually so designated, we can free it */
565 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
569 * Since our memory may not be contiguous, calculate the
570 * real number of pages we have in this system
572 printk(KERN_INFO "Memory:");
575 for (i = 0; i < meminfo.nr_banks; i++) {
576 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
577 printk(" %ldMB", meminfo.bank[i].size >> 20);
580 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
581 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
582 "%dK data, %dK init)\n",
583 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
584 codepages >> 10, datapages >> 10, initpages >> 10);
586 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
587 extern int sysctl_overcommit_memory;
589 * On a machine this small we won't get
590 * anywhere without overcommit, so turn
593 sysctl_overcommit_memory = 1;
597 void free_initmem(void)
599 if (!machine_is_integrator() && !machine_is_cintegrator()) {
600 free_area((unsigned long)(&__init_begin),
601 (unsigned long)(&__init_end),
606 #ifdef CONFIG_BLK_DEV_INITRD
608 static int keep_initrd;
610 void free_initrd_mem(unsigned long start, unsigned long end)
613 free_area(start, end, "initrd");
616 static int __init keepinitrd_setup(char *__unused)
622 __setup("keepinitrd", keepinitrd_setup);