VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / arm / mm / init.c
1 /*
2  *  linux/arch/arm/mm/init.c
3  *
4  *  Copyright (C) 1995-2002 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/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>
18
19 #include <asm/mach-types.h>
20 #include <asm/hardware.h>
21 #include <asm/setup.h>
22 #include <asm/tlb.h>
23
24 #include <asm/mach/arch.h>
25 #include <asm/mach/map.h>
26
27 #define TABLE_SIZE      (2 * PTRS_PER_PTE * sizeof(pte_t))
28
29 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
30
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;
35
36 /*
37  * The sole use of this is to pass memory configuration
38  * data from paging_init to mem_init.
39  */
40 static struct meminfo meminfo __initdata = { 0, };
41
42 /*
43  * empty_zero_page is a special page that is used for
44  * zero-initialized data and COW.
45  */
46 struct page *empty_zero_page;
47
48 void show_mem(void)
49 {
50         int free = 0, total = 0, reserved = 0;
51         int shared = 0, cached = 0, slab = 0, node;
52
53         printk("Mem-info:\n");
54         show_free_areas();
55         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
56
57         for (node = 0; node < numnodes; node++) {
58                 struct page *page, *end;
59
60                 page = NODE_MEM_MAP(node);
61                 end  = page + NODE_DATA(node)->node_spanned_pages;
62
63                 do {
64                         total++;
65                         if (PageReserved(page))
66                                 reserved++;
67                         else if (PageSwapCache(page))
68                                 cached++;
69                         else if (PageSlab(page))
70                                 slab++;
71                         else if (!page_count(page))
72                                 free++;
73                         else
74                                 shared += page_count(page) - 1;
75                         page++;
76                 } while (page < end);
77         }
78
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);
85 }
86
87 struct node_info {
88         unsigned int start;
89         unsigned int end;
90         int bootmap_pages;
91 };
92
93 #define O_PFN_DOWN(x)   ((x) >> PAGE_SHIFT)
94 #define V_PFN_DOWN(x)   O_PFN_DOWN(__pa(x))
95
96 #define O_PFN_UP(x)     (PAGE_ALIGN(x) >> PAGE_SHIFT)
97 #define V_PFN_UP(x)     O_PFN_UP(__pa(x))
98
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))
102
103 /*
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.
108  */
109 static unsigned int __init
110 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
111 {
112         unsigned int start_pfn, bank, bootmap_pfn;
113
114         start_pfn   = V_PFN_UP(&_end);
115         bootmap_pfn = 0;
116
117         for (bank = 0; bank < mi->nr_banks; bank ++) {
118                 unsigned int start, end;
119
120                 if (mi->bank[bank].node != node)
121                         continue;
122
123                 start = O_PFN_UP(mi->bank[bank].start);
124                 end   = O_PFN_DOWN(mi->bank[bank].size +
125                                    mi->bank[bank].start);
126
127                 if (end < start_pfn)
128                         continue;
129
130                 if (start < start_pfn)
131                         start = start_pfn;
132
133                 if (end <= start)
134                         continue;
135
136                 if (end - start >= bootmap_pages) {
137                         bootmap_pfn = start;
138                         break;
139                 }
140         }
141
142         if (bootmap_pfn == 0)
143                 BUG();
144
145         return bootmap_pfn;
146 }
147
148 /*
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
154  */
155 static unsigned int __init
156 find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
157 {
158         unsigned int i, bootmem_pages = 0, memend_pfn = 0;
159
160         for (i = 0; i < MAX_NUMNODES; i++) {
161                 np[i].start = -1U;
162                 np[i].end = 0;
163                 np[i].bootmap_pages = 0;
164         }
165
166         for (i = 0; i < mi->nr_banks; i++) {
167                 unsigned long start, end;
168                 int node;
169
170                 if (mi->bank[i].size == 0) {
171                         /*
172                          * Mark this bank with an invalid node number
173                          */
174                         mi->bank[i].node = -1;
175                         continue;
176                 }
177
178                 node = mi->bank[i].node;
179
180                 if (node >= numnodes) {
181                         numnodes = node + 1;
182
183                         /*
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?)
188                          */
189                         if (numnodes > MAX_NUMNODES)
190                                 BUG();
191                 }
192
193                 /*
194                  * Get the start and end pfns for this bank
195                  */
196                 start = O_PFN_UP(mi->bank[i].start);
197                 end   = O_PFN_DOWN(mi->bank[i].start + mi->bank[i].size);
198
199                 if (np[node].start > start)
200                         np[node].start = start;
201
202                 if (np[node].end < end)
203                         np[node].end = end;
204
205                 if (memend_pfn < end)
206                         memend_pfn = end;
207         }
208
209         /*
210          * Calculate the number of pages we require to
211          * store the bootmem bitmaps.
212          */
213         for (i = 0; i < numnodes; i++) {
214                 if (np[i].end == 0)
215                         continue;
216
217                 np[i].bootmap_pages = bootmem_bootmap_pages(np[i].end -
218                                                             np[i].start);
219                 bootmem_pages += np[i].bootmap_pages;
220         }
221
222         high_memory = __va(memend_pfn << PAGE_SHIFT);
223
224         /*
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.
228          */
229         max_low_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
230         max_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
231
232         return bootmem_pages;
233 }
234
235 static int __init check_initrd(struct meminfo *mi)
236 {
237         int initrd_node = -2;
238 #ifdef CONFIG_BLK_DEV_INITRD
239         unsigned long end = phys_initrd_start + phys_initrd_size;
240
241         /*
242          * Make sure that the initrd is within a valid area of
243          * memory.
244          */
245         if (phys_initrd_size) {
246                 unsigned int i;
247
248                 initrd_node = -1;
249
250                 for (i = 0; i < mi->nr_banks; i++) {
251                         unsigned long bank_end;
252
253                         bank_end = mi->bank[i].start + mi->bank[i].size;
254
255                         if (mi->bank[i].start <= phys_initrd_start &&
256                             end <= bank_end)
257                                 initrd_node = mi->bank[i].node;
258                 }
259         }
260
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;
266         }
267 #endif
268
269         return initrd_node;
270 }
271
272 /*
273  * Reserve the various regions of node 0
274  */
275 static __init void reserve_node_zero(unsigned int bootmap_pfn, unsigned int bootmap_pages)
276 {
277         pg_data_t *pgdat = NODE_DATA(0);
278         unsigned long res_size = 0;
279
280         /*
281          * Register the kernel text and data with bootmem.
282          * Note that this can only be in node 0.
283          */
284         reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
285
286         /*
287          * Reserve the page tables.  These are already in use,
288          * and can only be in node 0.
289          */
290         reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
291                              PTRS_PER_PGD * sizeof(pgd_t));
292
293         /*
294          * And don't forget to reserve the allocator bitmap,
295          * which will be freed later.
296          */
297         reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
298                              bootmap_pages << PAGE_SHIFT);
299
300         /*
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.
305          */
306         if (machine_is_integrator() || machine_is_cintegrator())
307                 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
308
309         /*
310          * These should likewise go elsewhere.  They pre-reserve the
311          * screen memory region at the start of main system memory.
312          */
313         if (machine_is_edb7211())
314                 res_size = 0x00020000;
315         if (machine_is_p720t())
316                 res_size = 0x00014000;
317
318 #ifdef CONFIG_SA1111
319         /*
320          * Because of the SA1111 DMA bug, we want to preserve our
321          * precious DMA-able memory...
322          */
323         res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
324 #endif
325         if (res_size)
326                 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
327 }
328
329 /*
330  * Register all available RAM in this node with the bootmem allocator.
331  */
332 static inline void free_bootmem_node_bank(int node, struct meminfo *mi)
333 {
334         pg_data_t *pgdat = NODE_DATA(node);
335         int bank;
336
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);
341 }
342
343 /*
344  * Initialise the bootmem allocator for all nodes.  This is called
345  * early during the architecture specific initialisation.
346  */
347 void __init bootmem_init(struct meminfo *mi)
348 {
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;
352
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);
356
357         map_pg = bootmap_pfn;
358
359         /*
360          * Initialise the bootmem nodes.
361          *
362          * What we really want to do is:
363          *
364          *   unmap_all_regions_except_kernel();
365          *   for_each_node_in_reverse_order(node) {
366          *     map_node(node);
367          *     allocate_bootmem_map(node);
368          *     init_bootmem_node(node);
369          *     free_bootmem_node(node);
370          *   }
371          *
372          * but this is a 2.5-type change.  For now, we just set
373          * the nodes up in reverse order.
374          *
375          * (we could also do with rolling bootmem_init and paging_init
376          * into one generic "memory_init" type function).
377          */
378         np += numnodes - 1;
379         for (node = numnodes - 1; node >= 0; node--, np--) {
380                 /*
381                  * If there are no pages in this node, ignore it.
382                  * Note that node 0 must always have some pages.
383                  */
384                 if (np->end == 0) {
385                         if (node == 0)
386                                 BUG();
387                         continue;
388                 }
389
390                 /*
391                  * Initialise the bootmem allocator.
392                  */
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;
396
397                 /*
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.
400                  */
401                 if (node == 0)
402                         reserve_node_zero(bootmap_pfn, bootmap_pages);
403         }
404
405
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,
409                                      phys_initrd_size);
410                 initrd_start = __phys_to_virt(phys_initrd_start);
411                 initrd_end = initrd_start + phys_initrd_size;
412         }
413 #endif
414
415         if (map_pg != bootmap_pfn + bootmap_pages)
416                 BUG();
417
418 }
419
420 /*
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.
423  */
424 void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
425 {
426         void *zero_page;
427         int node;
428
429         memcpy(&meminfo, mi, sizeof(meminfo));
430
431         /*
432          * allocate the zero page.  Note that we count on this going ok.
433          */
434         zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
435
436         /*
437          * initialise the page tables.
438          */
439         memtable_init(mi);
440         if (mdesc->map_io)
441                 mdesc->map_io();
442         flush_tlb_all();
443
444         /*
445          * initialise the zones within each node
446          */
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;
451                 pg_data_t *pgdat;
452                 int i;
453
454                 /*
455                  * Initialise the zone size information.
456                  */
457                 for (i = 0; i < MAX_NR_ZONES; i++) {
458                         zone_size[i]  = 0;
459                         zhole_size[i] = 0;
460                 }
461
462                 pgdat = NODE_DATA(node);
463                 bdata = pgdat->bdata;
464
465                 /*
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
469                  * it.
470                  */
471                 zone_size[0] = bdata->node_low_pfn -
472                                 (bdata->node_boot_start >> PAGE_SHIFT);
473
474                 /*
475                  * If this zone has zero size, skip it.
476                  */
477                 if (!zone_size[0])
478                         continue;
479
480                 /*
481                  * For each bank in this node, calculate the size of the
482                  * holes.  holes = node_size - sum(bank_sizes_in_node)
483                  */
484                 zhole_size[0] = zone_size[0];
485                 for (i = 0; i < mi->nr_banks; i++) {
486                         if (mi->bank[i].node != node)
487                                 continue;
488
489                         zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
490                 }
491
492                 /*
493                  * Adjust the sizes according to any special
494                  * requirements for this machine type.
495                  */
496                 arch_adjust_zones(node, zone_size, zhole_size);
497
498                 free_area_init_node(node, pgdat, NULL, zone_size,
499                                 bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
500         }
501
502 #ifndef CONFIG_DISCONTIGMEM
503         mem_map = contig_page_data.node_mem_map;
504 #endif
505
506         /*
507          * finish off the bad pages once
508          * the mem_map is initialised
509          */
510         memzero(zero_page, PAGE_SIZE);
511         empty_zero_page = virt_to_page(zero_page);
512         flush_dcache_page(empty_zero_page);
513 }
514
515 static inline void free_area(unsigned long addr, unsigned long end, char *s)
516 {
517         unsigned int size = (end - addr) >> 10;
518
519         for (; addr < end; addr += PAGE_SIZE) {
520                 struct page *page = virt_to_page(addr);
521                 ClearPageReserved(page);
522                 set_page_count(page, 1);
523                 free_page(addr);
524                 totalram_pages++;
525         }
526
527         if (size && s)
528                 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
529 }
530
531 /*
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.
535  */
536 void __init mem_init(void)
537 {
538         unsigned int codepages, datapages, initpages;
539         int i, node;
540
541         codepages = &_etext - &_text;
542         datapages = &_end - &_etext;
543         initpages = &__init_end - &__init_begin;
544
545 #ifndef CONFIG_DISCONTIGMEM
546         max_mapnr   = virt_to_page(high_memory) - mem_map;
547 #endif
548
549         /*
550          * We may have non-contiguous memory.
551          */
552         if (meminfo.nr_banks != 1)
553                 create_memmap_holes(&meminfo);
554
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);
558
559                 if (pgdat->node_spanned_pages != 0)
560                         totalram_pages += free_all_bootmem_node(pgdat);
561         }
562
563 #ifdef CONFIG_SA1111
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);
566 #endif
567
568         /*
569          * Since our memory may not be contiguous, calculate the
570          * real number of pages we have in this system
571          */
572         printk(KERN_INFO "Memory:");
573
574         num_physpages = 0;
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);
578         }
579
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);
585
586         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
587                 extern int sysctl_overcommit_memory;
588                 /*
589                  * On a machine this small we won't get
590                  * anywhere without overcommit, so turn
591                  * it on by default.
592                  */
593                 sysctl_overcommit_memory = 1;
594         }
595 }
596
597 void free_initmem(void)
598 {
599         if (!machine_is_integrator() && !machine_is_cintegrator()) {
600                 free_area((unsigned long)(&__init_begin),
601                           (unsigned long)(&__init_end),
602                           "init");
603         }
604 }
605
606 #ifdef CONFIG_BLK_DEV_INITRD
607
608 static int keep_initrd;
609
610 void free_initrd_mem(unsigned long start, unsigned long end)
611 {
612         if (!keep_initrd)
613                 free_area(start, end, "initrd");
614 }
615
616 static int __init keepinitrd_setup(char *__unused)
617 {
618         keep_initrd = 1;
619         return 1;
620 }
621
622 __setup("keepinitrd", keepinitrd_setup);
623 #endif