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>
28 #define TABLE_OFFSET (PTRS_PER_PTE)
30 #define TABLE_OFFSET 0
33 #define TABLE_SIZE ((TABLE_OFFSET + PTRS_PER_PTE) * sizeof(pte_t))
35 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
37 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
38 extern char _stext, _text, _etext, _end, __init_begin, __init_end;
39 extern unsigned long phys_initrd_start;
40 extern unsigned long phys_initrd_size;
43 * The sole use of this is to pass memory configuration
44 * data from paging_init to mem_init.
46 static struct meminfo meminfo __initdata = { 0, };
49 * empty_zero_page is a special page that is used for
50 * zero-initialized data and COW.
52 struct page *empty_zero_page;
56 int free = 0, total = 0, reserved = 0;
57 int shared = 0, cached = 0, slab = 0, node;
59 printk("Mem-info:\n");
61 printk("Free swap: %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
63 for (node = 0; node < numnodes; node++) {
64 struct page *page, *end;
66 page = NODE_MEM_MAP(node);
67 end = page + NODE_DATA(node)->node_spanned_pages;
71 if (PageReserved(page))
73 else if (PageSwapCache(page))
75 else if (PageSlab(page))
77 else if (!page_count(page))
80 shared += page_count(page) - 1;
85 printk("%d pages of RAM\n", total);
86 printk("%d free pages\n", free);
87 printk("%d reserved pages\n", reserved);
88 printk("%d slab pages\n", slab);
89 printk("%d pages shared\n", shared);
90 printk("%d pages swap cached\n", cached);
99 #define O_PFN_DOWN(x) ((x) >> PAGE_SHIFT)
100 #define V_PFN_DOWN(x) O_PFN_DOWN(__pa(x))
102 #define O_PFN_UP(x) (PAGE_ALIGN(x) >> PAGE_SHIFT)
103 #define V_PFN_UP(x) O_PFN_UP(__pa(x))
105 #define PFN_SIZE(x) ((x) >> PAGE_SHIFT)
106 #define PFN_RANGE(s,e) PFN_SIZE(PAGE_ALIGN((unsigned long)(e)) - \
107 (((unsigned long)(s)) & PAGE_MASK))
110 * FIXME: We really want to avoid allocating the bootmap bitmap
111 * over the top of the initrd. Hopefully, this is located towards
112 * the start of a bank, so if we allocate the bootmap bitmap at
113 * the end, we won't clash.
115 static unsigned int __init
116 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
118 unsigned int start_pfn, bank, bootmap_pfn;
120 start_pfn = V_PFN_UP(&_end);
123 for (bank = 0; bank < mi->nr_banks; bank ++) {
124 unsigned int start, end;
126 if (mi->bank[bank].node != node)
129 start = O_PFN_UP(mi->bank[bank].start);
130 end = O_PFN_DOWN(mi->bank[bank].size +
131 mi->bank[bank].start);
136 if (start < start_pfn)
142 if (end - start >= bootmap_pages) {
148 if (bootmap_pfn == 0)
155 * Scan the memory info structure and pull out:
156 * - the end of memory
157 * - the number of nodes
158 * - the pfn range of each node
159 * - the number of bootmem bitmap pages
161 static unsigned int __init
162 find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
164 unsigned int i, bootmem_pages = 0, memend_pfn = 0;
166 for (i = 0; i < MAX_NUMNODES; i++) {
169 np[i].bootmap_pages = 0;
172 for (i = 0; i < mi->nr_banks; i++) {
173 unsigned long start, end;
176 if (mi->bank[i].size == 0) {
178 * Mark this bank with an invalid node number
180 mi->bank[i].node = -1;
184 node = mi->bank[i].node;
186 if (node >= numnodes) {
190 * Make sure we haven't exceeded the maximum number
191 * of nodes that we have in this configuration. If
192 * we have, we're in trouble. (maybe we ought to
193 * limit, instead of bugging?)
195 if (numnodes > MAX_NUMNODES)
200 * Get the start and end pfns for this bank
202 start = O_PFN_UP(mi->bank[i].start);
203 end = O_PFN_DOWN(mi->bank[i].start + mi->bank[i].size);
205 if (np[node].start > start)
206 np[node].start = start;
208 if (np[node].end < end)
211 if (memend_pfn < end)
216 * Calculate the number of pages we require to
217 * store the bootmem bitmaps.
219 for (i = 0; i < numnodes; i++) {
223 np[i].bootmap_pages = bootmem_bootmap_pages(np[i].end -
225 bootmem_pages += np[i].bootmap_pages;
228 high_memory = __va(memend_pfn << PAGE_SHIFT);
231 * This doesn't seem to be used by the Linux memory
232 * manager any more. If we can get rid of it, we
233 * also get rid of some of the stuff above as well.
235 max_low_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
236 max_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
238 return bootmem_pages;
241 static int __init check_initrd(struct meminfo *mi)
243 int initrd_node = -2;
244 #ifdef CONFIG_BLK_DEV_INITRD
245 unsigned long end = phys_initrd_start + phys_initrd_size;
248 * Make sure that the initrd is within a valid area of
251 if (phys_initrd_size) {
256 for (i = 0; i < mi->nr_banks; i++) {
257 unsigned long bank_end;
259 bank_end = mi->bank[i].start + mi->bank[i].size;
261 if (mi->bank[i].start <= phys_initrd_start &&
263 initrd_node = mi->bank[i].node;
267 if (initrd_node == -1) {
268 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
269 "physical memory - disabling initrd\n",
270 phys_initrd_start, end);
271 phys_initrd_start = phys_initrd_size = 0;
279 * Reserve the various regions of node 0
281 static __init void reserve_node_zero(unsigned int bootmap_pfn, unsigned int bootmap_pages)
283 pg_data_t *pgdat = NODE_DATA(0);
284 unsigned long res_size = 0;
287 * Register the kernel text and data with bootmem.
288 * Note that this can only be in node 0.
290 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
294 * Reserve the page tables. These are already in use,
295 * and can only be in node 0.
297 reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
298 PTRS_PER_PGD * sizeof(pgd_t));
301 * And don't forget to reserve the allocator bitmap,
302 * which will be freed later.
304 reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
305 bootmap_pages << PAGE_SHIFT);
308 * Hmm... This should go elsewhere, but we really really need to
309 * stop things allocating the low memory; ideally we need a better
310 * implementation of GFP_DMA which does not assume that DMA-able
311 * memory starts at zero.
313 if (machine_is_integrator() || machine_is_cintegrator())
314 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
317 * These should likewise go elsewhere. They pre-reserve the
318 * screen memory region at the start of main system memory.
320 if (machine_is_edb7211())
321 res_size = 0x00020000;
322 if (machine_is_p720t())
323 res_size = 0x00014000;
327 * Because of the SA1111 DMA bug, we want to preserve our
328 * precious DMA-able memory...
330 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
333 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
337 * Register all available RAM in this node with the bootmem allocator.
339 static inline void free_bootmem_node_bank(int node, struct meminfo *mi)
341 pg_data_t *pgdat = NODE_DATA(node);
344 for (bank = 0; bank < mi->nr_banks; bank++)
345 if (mi->bank[bank].node == node)
346 free_bootmem_node(pgdat, mi->bank[bank].start,
347 mi->bank[bank].size);
351 * Initialise the bootmem allocator for all nodes. This is called
352 * early during the architecture specific initialisation.
354 void __init bootmem_init(struct meminfo *mi)
356 struct node_info node_info[MAX_NUMNODES], *np = node_info;
357 unsigned int bootmap_pages, bootmap_pfn, map_pg;
358 int node, initrd_node;
360 bootmap_pages = find_memend_and_nodes(mi, np);
361 bootmap_pfn = find_bootmap_pfn(0, mi, bootmap_pages);
362 initrd_node = check_initrd(mi);
364 map_pg = bootmap_pfn;
367 * Initialise the bootmem nodes.
369 * What we really want to do is:
371 * unmap_all_regions_except_kernel();
372 * for_each_node_in_reverse_order(node) {
374 * allocate_bootmem_map(node);
375 * init_bootmem_node(node);
376 * free_bootmem_node(node);
379 * but this is a 2.5-type change. For now, we just set
380 * the nodes up in reverse order.
382 * (we could also do with rolling bootmem_init and paging_init
383 * into one generic "memory_init" type function).
386 for (node = numnodes - 1; node >= 0; node--, np--) {
388 * If there are no pages in this node, ignore it.
389 * Note that node 0 must always have some pages.
398 * Initialise the bootmem allocator.
400 init_bootmem_node(NODE_DATA(node), map_pg, np->start, np->end);
401 free_bootmem_node_bank(node, mi);
402 map_pg += np->bootmap_pages;
405 * If this is node 0, we need to reserve some areas ASAP -
406 * we may use bootmem on node 0 to setup the other nodes.
409 reserve_node_zero(bootmap_pfn, bootmap_pages);
413 #ifdef CONFIG_BLK_DEV_INITRD
414 if (phys_initrd_size && initrd_node >= 0) {
415 reserve_bootmem_node(NODE_DATA(initrd_node), phys_initrd_start,
417 initrd_start = __phys_to_virt(phys_initrd_start);
418 initrd_end = initrd_start + phys_initrd_size;
422 if (map_pg != bootmap_pfn + bootmap_pages)
428 * paging_init() sets up the page tables, initialises the zone memory
429 * maps, and sets up the zero page, bad page and bad page tables.
431 void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
436 memcpy(&meminfo, mi, sizeof(meminfo));
439 * allocate the zero page. Note that we count on this going ok.
441 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
444 * initialise the page tables.
452 * initialise the zones within each node
454 for (node = 0; node < numnodes; node++) {
455 unsigned long zone_size[MAX_NR_ZONES];
456 unsigned long zhole_size[MAX_NR_ZONES];
457 struct bootmem_data *bdata;
462 * Initialise the zone size information.
464 for (i = 0; i < MAX_NR_ZONES; i++) {
469 pgdat = NODE_DATA(node);
470 bdata = pgdat->bdata;
473 * The size of this node has already been determined.
474 * If we need to do anything fancy with the allocation
475 * of this memory to the zones, now is the time to do
478 zone_size[0] = bdata->node_low_pfn -
479 (bdata->node_boot_start >> PAGE_SHIFT);
482 * If this zone has zero size, skip it.
488 * For each bank in this node, calculate the size of the
489 * holes. holes = node_size - sum(bank_sizes_in_node)
491 zhole_size[0] = zone_size[0];
492 for (i = 0; i < mi->nr_banks; i++) {
493 if (mi->bank[i].node != node)
496 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
500 * Adjust the sizes according to any special
501 * requirements for this machine type.
503 arch_adjust_zones(node, zone_size, zhole_size);
505 free_area_init_node(node, pgdat, 0, zone_size,
506 bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
509 #ifndef CONFIG_DISCONTIGMEM
510 mem_map = contig_page_data.node_mem_map;
514 * finish off the bad pages once
515 * the mem_map is initialised
517 memzero(zero_page, PAGE_SIZE);
518 empty_zero_page = virt_to_page(zero_page);
519 flush_dcache_page(empty_zero_page);
522 static inline void free_area(unsigned long addr, unsigned long end, char *s)
524 unsigned int size = (end - addr) >> 10;
526 for (; addr < end; addr += PAGE_SIZE) {
527 struct page *page = virt_to_page(addr);
528 ClearPageReserved(page);
529 set_page_count(page, 1);
535 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
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 codepages, datapages, initpages;
548 codepages = &_etext - &_text;
549 datapages = &_end - &_etext;
550 initpages = &__init_end - &__init_begin;
552 #ifndef CONFIG_DISCONTIGMEM
553 max_mapnr = virt_to_page(high_memory) - mem_map;
557 * We may have non-contiguous memory.
559 if (meminfo.nr_banks != 1)
560 create_memmap_holes(&meminfo);
562 /* this will put all unused low memory onto the freelists */
563 for (node = 0; node < numnodes; node++) {
564 pg_data_t *pgdat = NODE_DATA(node);
566 if (pgdat->node_spanned_pages != 0)
567 totalram_pages += free_all_bootmem_node(pgdat);
571 /* now that our DMA memory is actually so designated, we can free it */
572 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
576 * Since our memory may not be contiguous, calculate the
577 * real number of pages we have in this system
579 printk(KERN_INFO "Memory:");
582 for (i = 0; i < meminfo.nr_banks; i++) {
583 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
584 printk(" %ldMB", meminfo.bank[i].size >> 20);
587 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
588 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
589 "%dK data, %dK init)\n",
590 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
591 codepages >> 10, datapages >> 10, initpages >> 10);
593 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
594 extern int sysctl_overcommit_memory;
596 * On a machine this small we won't get
597 * anywhere without overcommit, so turn
600 sysctl_overcommit_memory = 1;
604 void free_initmem(void)
606 if (!machine_is_integrator() && !machine_is_cintegrator()) {
607 free_area((unsigned long)(&__init_begin),
608 (unsigned long)(&__init_end),
613 #ifdef CONFIG_BLK_DEV_INITRD
615 static int keep_initrd;
617 void free_initrd_mem(unsigned long start, unsigned long end)
620 free_area(start, end, "initrd");
623 static int __init keepinitrd_setup(char *__unused)
629 __setup("keepinitrd", keepinitrd_setup);