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