Merge branch 'master' of /home/sam/kernel/linux-2.6/
[pandora-kernel.git] / arch / arm / mm / init.c
1 /*
2  *  linux/arch/arm/mm/init.c
3  *
4  *  Copyright (C) 1995-2005 Russell King
5  *
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.
9  */
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/ptrace.h>
13 #include <linux/swap.h>
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/mman.h>
17 #include <linux/nodemask.h>
18 #include <linux/initrd.h>
19
20 #include <asm/mach-types.h>
21 #include <asm/setup.h>
22 #include <asm/sizes.h>
23 #include <asm/tlb.h>
24
25 #include <asm/mach/arch.h>
26 #include <asm/mach/map.h>
27
28 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
29
30 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
31 extern void _stext, _text, _etext, __data_start, _end, __init_begin, __init_end;
32 extern unsigned long phys_initrd_start;
33 extern unsigned long phys_initrd_size;
34
35 /*
36  * The sole use of this is to pass memory configuration
37  * data from paging_init to mem_init.
38  */
39 static struct meminfo meminfo __initdata = { 0, };
40
41 /*
42  * empty_zero_page is a special page that is used for
43  * zero-initialized data and COW.
44  */
45 struct page *empty_zero_page;
46
47 void show_mem(void)
48 {
49         int free = 0, total = 0, reserved = 0;
50         int shared = 0, cached = 0, slab = 0, node;
51
52         printk("Mem-info:\n");
53         show_free_areas();
54         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
55
56         for_each_online_node(node) {
57                 struct page *page, *end;
58
59                 page = NODE_MEM_MAP(node);
60                 end  = page + NODE_DATA(node)->node_spanned_pages;
61
62                 do {
63                         total++;
64                         if (PageReserved(page))
65                                 reserved++;
66                         else if (PageSwapCache(page))
67                                 cached++;
68                         else if (PageSlab(page))
69                                 slab++;
70                         else if (!page_count(page))
71                                 free++;
72                         else
73                                 shared += page_count(page) - 1;
74                         page++;
75                 } while (page < end);
76         }
77
78         printk("%d pages of RAM\n", total);
79         printk("%d free pages\n", free);
80         printk("%d reserved pages\n", reserved);
81         printk("%d slab pages\n", slab);
82         printk("%d pages shared\n", shared);
83         printk("%d pages swap cached\n", cached);
84 }
85
86 static inline pmd_t *pmd_off(pgd_t *pgd, unsigned long virt)
87 {
88         return pmd_offset(pgd, virt);
89 }
90
91 static inline pmd_t *pmd_off_k(unsigned long virt)
92 {
93         return pmd_off(pgd_offset_k(virt), virt);
94 }
95
96 #define for_each_nodebank(iter,mi,no)                   \
97         for (iter = 0; iter < mi->nr_banks; iter++)     \
98                 if (mi->bank[iter].node == no)
99
100 /*
101  * FIXME: We really want to avoid allocating the bootmap bitmap
102  * over the top of the initrd.  Hopefully, this is located towards
103  * the start of a bank, so if we allocate the bootmap bitmap at
104  * the end, we won't clash.
105  */
106 static unsigned int __init
107 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
108 {
109         unsigned int start_pfn, bank, bootmap_pfn;
110
111         start_pfn   = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
112         bootmap_pfn = 0;
113
114         for_each_nodebank(bank, mi, node) {
115                 unsigned int start, end;
116
117                 start = mi->bank[bank].start >> PAGE_SHIFT;
118                 end   = (mi->bank[bank].size +
119                          mi->bank[bank].start) >> PAGE_SHIFT;
120
121                 if (end < start_pfn)
122                         continue;
123
124                 if (start < start_pfn)
125                         start = start_pfn;
126
127                 if (end <= start)
128                         continue;
129
130                 if (end - start >= bootmap_pages) {
131                         bootmap_pfn = start;
132                         break;
133                 }
134         }
135
136         if (bootmap_pfn == 0)
137                 BUG();
138
139         return bootmap_pfn;
140 }
141
142 static int __init check_initrd(struct meminfo *mi)
143 {
144         int initrd_node = -2;
145 #ifdef CONFIG_BLK_DEV_INITRD
146         unsigned long end = phys_initrd_start + phys_initrd_size;
147
148         /*
149          * Make sure that the initrd is within a valid area of
150          * memory.
151          */
152         if (phys_initrd_size) {
153                 unsigned int i;
154
155                 initrd_node = -1;
156
157                 for (i = 0; i < mi->nr_banks; i++) {
158                         unsigned long bank_end;
159
160                         bank_end = mi->bank[i].start + mi->bank[i].size;
161
162                         if (mi->bank[i].start <= phys_initrd_start &&
163                             end <= bank_end)
164                                 initrd_node = mi->bank[i].node;
165                 }
166         }
167
168         if (initrd_node == -1) {
169                 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
170                        "physical memory - disabling initrd\n",
171                        phys_initrd_start, end);
172                 phys_initrd_start = phys_initrd_size = 0;
173         }
174 #endif
175
176         return initrd_node;
177 }
178
179 /*
180  * Reserve the various regions of node 0
181  */
182 static __init void reserve_node_zero(pg_data_t *pgdat)
183 {
184         unsigned long res_size = 0;
185
186         /*
187          * Register the kernel text and data with bootmem.
188          * Note that this can only be in node 0.
189          */
190 #ifdef CONFIG_XIP_KERNEL
191         reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start);
192 #else
193         reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
194 #endif
195
196         /*
197          * Reserve the page tables.  These are already in use,
198          * and can only be in node 0.
199          */
200         reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
201                              PTRS_PER_PGD * sizeof(pgd_t));
202
203         /*
204          * Hmm... This should go elsewhere, but we really really need to
205          * stop things allocating the low memory; ideally we need a better
206          * implementation of GFP_DMA which does not assume that DMA-able
207          * memory starts at zero.
208          */
209         if (machine_is_integrator() || machine_is_cintegrator())
210                 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
211
212         /*
213          * These should likewise go elsewhere.  They pre-reserve the
214          * screen memory region at the start of main system memory.
215          */
216         if (machine_is_edb7211())
217                 res_size = 0x00020000;
218         if (machine_is_p720t())
219                 res_size = 0x00014000;
220
221 #ifdef CONFIG_SA1111
222         /*
223          * Because of the SA1111 DMA bug, we want to preserve our
224          * precious DMA-able memory...
225          */
226         res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
227 #endif
228         if (res_size)
229                 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
230 }
231
232 void __init build_mem_type_table(void);
233 void __init create_mapping(struct map_desc *md);
234
235 static unsigned long __init
236 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
237 {
238         unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
239         unsigned long start_pfn, end_pfn, boot_pfn;
240         unsigned int boot_pages;
241         pg_data_t *pgdat;
242         int i;
243
244         start_pfn = -1UL;
245         end_pfn = 0;
246
247         /*
248          * Calculate the pfn range, and map the memory banks for this node.
249          */
250         for_each_nodebank(i, mi, node) {
251                 unsigned long start, end;
252                 struct map_desc map;
253
254                 start = mi->bank[i].start >> PAGE_SHIFT;
255                 end = (mi->bank[i].start + mi->bank[i].size) >> PAGE_SHIFT;
256
257                 if (start_pfn > start)
258                         start_pfn = start;
259                 if (end_pfn < end)
260                         end_pfn = end;
261
262                 map.pfn = __phys_to_pfn(mi->bank[i].start);
263                 map.virtual = __phys_to_virt(mi->bank[i].start);
264                 map.length = mi->bank[i].size;
265                 map.type = MT_MEMORY;
266
267                 create_mapping(&map);
268         }
269
270         /*
271          * If there is no memory in this node, ignore it.
272          */
273         if (end_pfn == 0)
274                 return end_pfn;
275
276         /*
277          * Allocate the bootmem bitmap page.
278          */
279         boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
280         boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
281
282         /*
283          * Initialise the bootmem allocator for this node, handing the
284          * memory banks over to bootmem.
285          */
286         node_set_online(node);
287         pgdat = NODE_DATA(node);
288         init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
289
290         for_each_nodebank(i, mi, node)
291                 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
292
293         /*
294          * Reserve the bootmem bitmap for this node.
295          */
296         reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
297                              boot_pages << PAGE_SHIFT);
298
299 #ifdef CONFIG_BLK_DEV_INITRD
300         /*
301          * If the initrd is in this node, reserve its memory.
302          */
303         if (node == initrd_node) {
304                 reserve_bootmem_node(pgdat, phys_initrd_start,
305                                      phys_initrd_size);
306                 initrd_start = __phys_to_virt(phys_initrd_start);
307                 initrd_end = initrd_start + phys_initrd_size;
308         }
309 #endif
310
311         /*
312          * Finally, reserve any node zero regions.
313          */
314         if (node == 0)
315                 reserve_node_zero(pgdat);
316
317         /*
318          * initialise the zones within this node.
319          */
320         memset(zone_size, 0, sizeof(zone_size));
321         memset(zhole_size, 0, sizeof(zhole_size));
322
323         /*
324          * The size of this node has already been determined.  If we need
325          * to do anything fancy with the allocation of this memory to the
326          * zones, now is the time to do it.
327          */
328         zone_size[0] = end_pfn - start_pfn;
329
330         /*
331          * For each bank in this node, calculate the size of the holes.
332          *  holes = node_size - sum(bank_sizes_in_node)
333          */
334         zhole_size[0] = zone_size[0];
335         for_each_nodebank(i, mi, node)
336                 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
337
338         /*
339          * Adjust the sizes according to any special requirements for
340          * this machine type.
341          */
342         arch_adjust_zones(node, zone_size, zhole_size);
343
344         free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
345
346         return end_pfn;
347 }
348
349 static void __init bootmem_init(struct meminfo *mi)
350 {
351         unsigned long addr, memend_pfn = 0;
352         int node, initrd_node, i;
353
354         /*
355          * Invalidate the node number for empty or invalid memory banks
356          */
357         for (i = 0; i < mi->nr_banks; i++)
358                 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
359                         mi->bank[i].node = -1;
360
361         memcpy(&meminfo, mi, sizeof(meminfo));
362
363         /*
364          * Clear out all the mappings below the kernel image.
365          */
366         for (addr = 0; addr < MODULE_START; addr += PGDIR_SIZE)
367                 pmd_clear(pmd_off_k(addr));
368 #ifdef CONFIG_XIP_KERNEL
369         /* The XIP kernel is mapped in the module area -- skip over it */
370         addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
371 #endif
372         for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
373                 pmd_clear(pmd_off_k(addr));
374
375         /*
376          * Clear out all the kernel space mappings, except for the first
377          * memory bank, up to the end of the vmalloc region.
378          */
379         for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
380              addr < VMALLOC_END; addr += PGDIR_SIZE)
381                 pmd_clear(pmd_off_k(addr));
382
383         /*
384          * Locate which node contains the ramdisk image, if any.
385          */
386         initrd_node = check_initrd(mi);
387
388         /*
389          * Run through each node initialising the bootmem allocator.
390          */
391         for_each_node(node) {
392                 unsigned long end_pfn;
393
394                 end_pfn = bootmem_init_node(node, initrd_node, mi);
395
396                 /*
397                  * Remember the highest memory PFN.
398                  */
399                 if (end_pfn > memend_pfn)
400                         memend_pfn = end_pfn;
401         }
402
403         high_memory = __va(memend_pfn << PAGE_SHIFT);
404
405         /*
406          * This doesn't seem to be used by the Linux memory manager any
407          * more, but is used by ll_rw_block.  If we can get rid of it, we
408          * also get rid of some of the stuff above as well.
409          *
410          * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
411          * the system, not the maximum PFN.
412          */
413         max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
414 }
415
416 /*
417  * Set up device the mappings.  Since we clear out the page tables for all
418  * mappings above VMALLOC_END, we will remove any debug device mappings.
419  * This means you have to be careful how you debug this function, or any
420  * called function.  This means you can't use any function or debugging
421  * method which may touch any device, otherwise the kernel _will_ crash.
422  */
423 static void __init devicemaps_init(struct machine_desc *mdesc)
424 {
425         struct map_desc map;
426         unsigned long addr;
427         void *vectors;
428
429         /*
430          * Allocate the vector page early.
431          */
432         vectors = alloc_bootmem_low_pages(PAGE_SIZE);
433         BUG_ON(!vectors);
434
435         for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
436                 pmd_clear(pmd_off_k(addr));
437
438         /*
439          * Map the kernel if it is XIP.
440          * It is always first in the modulearea.
441          */
442 #ifdef CONFIG_XIP_KERNEL
443         map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & PGDIR_MASK);
444         map.virtual = MODULE_START;
445         map.length = ((unsigned long)&_etext - map.virtual + ~PGDIR_MASK) & PGDIR_MASK;
446         map.type = MT_ROM;
447         create_mapping(&map);
448 #endif
449
450         /*
451          * Map the cache flushing regions.
452          */
453 #ifdef FLUSH_BASE
454         map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
455         map.virtual = FLUSH_BASE;
456         map.length = SZ_1M;
457         map.type = MT_CACHECLEAN;
458         create_mapping(&map);
459 #endif
460 #ifdef FLUSH_BASE_MINICACHE
461         map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
462         map.virtual = FLUSH_BASE_MINICACHE;
463         map.length = SZ_1M;
464         map.type = MT_MINICLEAN;
465         create_mapping(&map);
466 #endif
467
468         /*
469          * Create a mapping for the machine vectors at the high-vectors
470          * location (0xffff0000).  If we aren't using high-vectors, also
471          * create a mapping at the low-vectors virtual address.
472          */
473         map.pfn = __phys_to_pfn(virt_to_phys(vectors));
474         map.virtual = 0xffff0000;
475         map.length = PAGE_SIZE;
476         map.type = MT_HIGH_VECTORS;
477         create_mapping(&map);
478
479         if (!vectors_high()) {
480                 map.virtual = 0;
481                 map.type = MT_LOW_VECTORS;
482                 create_mapping(&map);
483         }
484
485         /*
486          * Ask the machine support to map in the statically mapped devices.
487          */
488         if (mdesc->map_io)
489                 mdesc->map_io();
490
491         /*
492          * Finally flush the caches and tlb to ensure that we're in a
493          * consistent state wrt the writebuffer.  This also ensures that
494          * any write-allocated cache lines in the vector page are written
495          * back.  After this point, we can start to touch devices again.
496          */
497         local_flush_tlb_all();
498         flush_cache_all();
499 }
500
501 /*
502  * paging_init() sets up the page tables, initialises the zone memory
503  * maps, and sets up the zero page, bad page and bad page tables.
504  */
505 void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
506 {
507         void *zero_page;
508
509         build_mem_type_table();
510         bootmem_init(mi);
511         devicemaps_init(mdesc);
512
513         top_pmd = pmd_off_k(0xffff0000);
514
515         /*
516          * allocate the zero page.  Note that we count on this going ok.
517          */
518         zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
519         memzero(zero_page, PAGE_SIZE);
520         empty_zero_page = virt_to_page(zero_page);
521         flush_dcache_page(empty_zero_page);
522 }
523
524 static inline void free_area(unsigned long addr, unsigned long end, char *s)
525 {
526         unsigned int size = (end - addr) >> 10;
527
528         for (; addr < end; addr += PAGE_SIZE) {
529                 struct page *page = virt_to_page(addr);
530                 ClearPageReserved(page);
531                 init_page_count(page);
532                 free_page(addr);
533                 totalram_pages++;
534         }
535
536         if (size && s)
537                 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
538 }
539
540 static inline void
541 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
542 {
543         struct page *start_pg, *end_pg;
544         unsigned long pg, pgend;
545
546         /*
547          * Convert start_pfn/end_pfn to a struct page pointer.
548          */
549         start_pg = pfn_to_page(start_pfn);
550         end_pg = pfn_to_page(end_pfn);
551
552         /*
553          * Convert to physical addresses, and
554          * round start upwards and end downwards.
555          */
556         pg = PAGE_ALIGN(__pa(start_pg));
557         pgend = __pa(end_pg) & PAGE_MASK;
558
559         /*
560          * If there are free pages between these,
561          * free the section of the memmap array.
562          */
563         if (pg < pgend)
564                 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
565 }
566
567 /*
568  * The mem_map array can get very big.  Free the unused area of the memory map.
569  */
570 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
571 {
572         unsigned long bank_start, prev_bank_end = 0;
573         unsigned int i;
574
575         /*
576          * [FIXME] This relies on each bank being in address order.  This
577          * may not be the case, especially if the user has provided the
578          * information on the command line.
579          */
580         for_each_nodebank(i, mi, node) {
581                 bank_start = mi->bank[i].start >> PAGE_SHIFT;
582                 if (bank_start < prev_bank_end) {
583                         printk(KERN_ERR "MEM: unordered memory banks.  "
584                                 "Not freeing memmap.\n");
585                         break;
586                 }
587
588                 /*
589                  * If we had a previous bank, and there is a space
590                  * between the current bank and the previous, free it.
591                  */
592                 if (prev_bank_end && prev_bank_end != bank_start)
593                         free_memmap(node, prev_bank_end, bank_start);
594
595                 prev_bank_end = (mi->bank[i].start +
596                                  mi->bank[i].size) >> PAGE_SHIFT;
597         }
598 }
599
600 /*
601  * mem_init() marks the free areas in the mem_map and tells us how much
602  * memory is free.  This is done after various parts of the system have
603  * claimed their memory after the kernel image.
604  */
605 void __init mem_init(void)
606 {
607         unsigned int codepages, datapages, initpages;
608         int i, node;
609
610         codepages = &_etext - &_text;
611         datapages = &_end - &__data_start;
612         initpages = &__init_end - &__init_begin;
613
614 #ifndef CONFIG_DISCONTIGMEM
615         max_mapnr   = virt_to_page(high_memory) - mem_map;
616 #endif
617
618         /* this will put all unused low memory onto the freelists */
619         for_each_online_node(node) {
620                 pg_data_t *pgdat = NODE_DATA(node);
621
622                 free_unused_memmap_node(node, &meminfo);
623
624                 if (pgdat->node_spanned_pages != 0)
625                         totalram_pages += free_all_bootmem_node(pgdat);
626         }
627
628 #ifdef CONFIG_SA1111
629         /* now that our DMA memory is actually so designated, we can free it */
630         free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
631 #endif
632
633         /*
634          * Since our memory may not be contiguous, calculate the
635          * real number of pages we have in this system
636          */
637         printk(KERN_INFO "Memory:");
638
639         num_physpages = 0;
640         for (i = 0; i < meminfo.nr_banks; i++) {
641                 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
642                 printk(" %ldMB", meminfo.bank[i].size >> 20);
643         }
644
645         printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
646         printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
647                 "%dK data, %dK init)\n",
648                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
649                 codepages >> 10, datapages >> 10, initpages >> 10);
650
651         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
652                 extern int sysctl_overcommit_memory;
653                 /*
654                  * On a machine this small we won't get
655                  * anywhere without overcommit, so turn
656                  * it on by default.
657                  */
658                 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
659         }
660 }
661
662 void free_initmem(void)
663 {
664         if (!machine_is_integrator() && !machine_is_cintegrator()) {
665                 free_area((unsigned long)(&__init_begin),
666                           (unsigned long)(&__init_end),
667                           "init");
668         }
669 }
670
671 #ifdef CONFIG_BLK_DEV_INITRD
672
673 static int keep_initrd;
674
675 void free_initrd_mem(unsigned long start, unsigned long end)
676 {
677         if (!keep_initrd)
678                 free_area(start, end, "initrd");
679 }
680
681 static int __init keepinitrd_setup(char *__unused)
682 {
683         keep_initrd = 1;
684         return 1;
685 }
686
687 __setup("keepinitrd", keepinitrd_setup);
688 #endif