VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ppc / mm / init.c
1 /*
2  *  PowerPC version
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *
5  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
6  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
7  *    Copyright (C) 1996 Paul Mackerras
8  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
9  *  PPC44x/36-bit changes by Matt Porter (mporter@mvista.com)
10  *
11  *  Derived from "arch/i386/mm/init.c"
12  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
13  *
14  *  This program is free software; you can redistribute it and/or
15  *  modify it under the terms of the GNU General Public License
16  *  as published by the Free Software Foundation; either version
17  *  2 of the License, or (at your option) any later version.
18  *
19  */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/mm.h>
29 #include <linux/stddef.h>
30 #include <linux/init.h>
31 #include <linux/bootmem.h>
32 #include <linux/highmem.h>
33 #include <linux/initrd.h>
34
35 #include <asm/pgalloc.h>
36 #include <asm/prom.h>
37 #include <asm/io.h>
38 #include <asm/mmu_context.h>
39 #include <asm/pgtable.h>
40 #include <asm/mmu.h>
41 #include <asm/smp.h>
42 #include <asm/machdep.h>
43 #include <asm/btext.h>
44 #include <asm/tlb.h>
45 #include <asm/bootinfo.h>
46
47 #include "mem_pieces.h"
48 #include "mmu_decl.h"
49
50 #if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
51 /* The ammount of lowmem must be within 0xF0000000 - KERNELBASE. */
52 #if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
53 #error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
54 #endif
55 #endif
56 #define MAX_LOW_MEM     CONFIG_LOWMEM_SIZE
57
58 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
59
60 unsigned long total_memory;
61 unsigned long total_lowmem;
62
63 unsigned long ppc_memstart;
64 unsigned long ppc_memoffset = PAGE_OFFSET;
65
66 int mem_init_done;
67 int init_bootmem_done;
68 int boot_mapsize;
69 #ifdef CONFIG_PPC_PMAC
70 unsigned long agp_special_page;
71 #endif
72
73 extern char _end[];
74 extern char etext[], _stext[];
75 extern char __init_begin, __init_end;
76 extern char __prep_begin, __prep_end;
77 extern char __chrp_begin, __chrp_end;
78 extern char __pmac_begin, __pmac_end;
79 extern char __openfirmware_begin, __openfirmware_end;
80
81 #ifdef CONFIG_HIGHMEM
82 pte_t *kmap_pte;
83 pgprot_t kmap_prot;
84
85 EXPORT_SYMBOL(kmap_prot);
86 EXPORT_SYMBOL(kmap_pte);
87 #endif
88
89 void MMU_init(void);
90 void set_phys_avail(unsigned long total_ram);
91
92 /* XXX should be in current.h  -- paulus */
93 extern struct task_struct *current_set[NR_CPUS];
94
95 char *klimit = _end;
96 struct mem_pieces phys_avail;
97
98 extern char *sysmap;
99 extern unsigned long sysmap_size;
100
101 /*
102  * this tells the system to map all of ram with the segregs
103  * (i.e. page tables) instead of the bats.
104  * -- Cort
105  */
106 int __map_without_bats;
107
108 /* max amount of RAM to use */
109 unsigned long __max_memory;
110 /* max amount of low RAM to map in */
111 unsigned long __max_low_memory = MAX_LOW_MEM;
112
113 void show_mem(void)
114 {
115         int i,free = 0,total = 0,reserved = 0;
116         int shared = 0, cached = 0;
117         int highmem = 0;
118
119         printk("Mem-info:\n");
120         show_free_areas();
121         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
122         i = max_mapnr;
123         while (i-- > 0) {
124                 total++;
125                 if (PageHighMem(mem_map+i))
126                         highmem++;
127                 if (PageReserved(mem_map+i))
128                         reserved++;
129                 else if (PageSwapCache(mem_map+i))
130                         cached++;
131                 else if (!page_count(mem_map+i))
132                         free++;
133                 else
134                         shared += page_count(mem_map+i) - 1;
135         }
136         printk("%d pages of RAM\n",total);
137         printk("%d pages of HIGHMEM\n", highmem);
138         printk("%d free pages\n",free);
139         printk("%d reserved pages\n",reserved);
140         printk("%d pages shared\n",shared);
141         printk("%d pages swap cached\n",cached);
142 }
143
144 /* Free up now-unused memory */
145 static void free_sec(unsigned long start, unsigned long end, const char *name)
146 {
147         unsigned long cnt = 0;
148
149         while (start < end) {
150                 ClearPageReserved(virt_to_page(start));
151                 set_page_count(virt_to_page(start), 1);
152                 free_page(start);
153                 cnt++;
154                 start += PAGE_SIZE;
155         }
156         if (cnt) {
157                 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
158                 totalram_pages += cnt;
159         }
160 }
161
162 void free_initmem(void)
163 {
164 #define FREESEC(TYPE) \
165         free_sec((unsigned long)(&__ ## TYPE ## _begin), \
166                  (unsigned long)(&__ ## TYPE ## _end), \
167                  #TYPE);
168
169         printk ("Freeing unused kernel memory:");
170         FREESEC(init);
171         if (_machine != _MACH_Pmac)
172                 FREESEC(pmac);
173         if (_machine != _MACH_chrp)
174                 FREESEC(chrp);
175         if (_machine != _MACH_prep)
176                 FREESEC(prep);
177         if (!have_of)
178                 FREESEC(openfirmware);
179         printk("\n");
180 #undef FREESEC
181 }
182
183 #ifdef CONFIG_BLK_DEV_INITRD
184 void free_initrd_mem(unsigned long start, unsigned long end)
185 {
186         printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
187
188         for (; start < end; start += PAGE_SIZE) {
189                 ClearPageReserved(virt_to_page(start));
190                 set_page_count(virt_to_page(start), 1);
191                 free_page(start);
192                 totalram_pages++;
193         }
194 }
195 #endif
196
197 /*
198  * Check for command-line options that affect what MMU_init will do.
199  */
200 void MMU_setup(void)
201 {
202         /* Check for nobats option (used in mapin_ram). */
203         if (strstr(cmd_line, "nobats")) {
204                 __map_without_bats = 1;
205         }
206
207         /* Look for mem= option on command line */
208         if (strstr(cmd_line, "mem=")) {
209                 char *p, *q;
210                 unsigned long maxmem = 0;
211
212                 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
213                         q = p + 4;
214                         if (p > cmd_line && p[-1] != ' ')
215                                 continue;
216                         maxmem = simple_strtoul(q, &q, 0);
217                         if (*q == 'k' || *q == 'K') {
218                                 maxmem <<= 10;
219                                 ++q;
220                         } else if (*q == 'm' || *q == 'M') {
221                                 maxmem <<= 20;
222                                 ++q;
223                         }
224                 }
225                 __max_memory = maxmem;
226         }
227 }
228
229 /*
230  * MMU_init sets up the basic memory mappings for the kernel,
231  * including both RAM and possibly some I/O regions,
232  * and sets up the page tables and the MMU hardware ready to go.
233  */
234 void __init MMU_init(void)
235 {
236         if (ppc_md.progress)
237                 ppc_md.progress("MMU:enter", 0x111);
238
239         /* parse args from command line */
240         MMU_setup();
241
242         /*
243          * Figure out how much memory we have, how much
244          * is lowmem, and how much is highmem.  If we were
245          * passed the total memory size from the bootloader,
246          * just use it.
247          */
248         if (boot_mem_size)
249                 total_memory = boot_mem_size;
250         else
251                 total_memory = ppc_md.find_end_of_memory();
252
253         if (__max_memory && total_memory > __max_memory)
254                 total_memory = __max_memory;
255         total_lowmem = total_memory;
256 #ifdef CONFIG_FSL_BOOKE
257         /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
258          * entries, so we need to adjust lowmem to match the amount we can map
259          * in the fixed entries */
260         adjust_total_lowmem();
261 #endif /* CONFIG_FSL_BOOKE */
262         if (total_lowmem > __max_low_memory) {
263                 total_lowmem = __max_low_memory;
264 #ifndef CONFIG_HIGHMEM
265                 total_memory = total_lowmem;
266 #endif /* CONFIG_HIGHMEM */
267         }
268         set_phys_avail(total_lowmem);
269
270         /* Initialize the MMU hardware */
271         if (ppc_md.progress)
272                 ppc_md.progress("MMU:hw init", 0x300);
273         MMU_init_hw();
274
275         /* Map in all of RAM starting at KERNELBASE */
276         if (ppc_md.progress)
277                 ppc_md.progress("MMU:mapin", 0x301);
278         mapin_ram();
279
280 #ifdef CONFIG_HIGHMEM
281         ioremap_base = PKMAP_BASE;
282 #else
283         ioremap_base = 0xfe000000UL;    /* for now, could be 0xfffff000 */
284 #endif /* CONFIG_HIGHMEM */
285         ioremap_bot = ioremap_base;
286
287         /* Map in I/O resources */
288         if (ppc_md.progress)
289                 ppc_md.progress("MMU:setio", 0x302);
290         if (ppc_md.setup_io_mappings)
291                 ppc_md.setup_io_mappings();
292
293         /* Initialize the context management stuff */
294         mmu_context_init();
295
296         if (ppc_md.progress)
297                 ppc_md.progress("MMU:exit", 0x211);
298
299 #ifdef CONFIG_BOOTX_TEXT
300         /* By default, we are no longer mapped */
301         boot_text_mapped = 0;
302         /* Must be done last, or ppc_md.progress will die. */
303         map_boot_text();
304 #endif
305 }
306
307 /* This is only called until mem_init is done. */
308 void __init *early_get_page(void)
309 {
310         void *p;
311
312         if (init_bootmem_done) {
313                 p = alloc_bootmem_pages(PAGE_SIZE);
314         } else {
315                 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
316         }
317         return p;
318 }
319
320 /*
321  * Initialize the bootmem system and give it all the memory we
322  * have available.
323  */
324 void __init do_init_bootmem(void)
325 {
326         unsigned long start, size;
327         int i;
328
329         /*
330          * Find an area to use for the bootmem bitmap.
331          * We look for the first area which is at least
332          * 128kB in length (128kB is enough for a bitmap
333          * for 4GB of memory, using 4kB pages), plus 1 page
334          * (in case the address isn't page-aligned).
335          */
336         start = 0;
337         size = 0;
338         for (i = 0; i < phys_avail.n_regions; ++i) {
339                 unsigned long a = phys_avail.regions[i].address;
340                 unsigned long s = phys_avail.regions[i].size;
341                 if (s <= size)
342                         continue;
343                 start = a;
344                 size = s;
345                 if (s >= 33 * PAGE_SIZE)
346                         break;
347         }
348         start = PAGE_ALIGN(start);
349
350         min_low_pfn = start >> PAGE_SHIFT;
351         max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
352         max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
353         boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
354                                          PPC_MEMSTART >> PAGE_SHIFT,
355                                          max_low_pfn);
356
357         /* remove the bootmem bitmap from the available memory */
358         mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
359
360         /* add everything in phys_avail into the bootmem map */
361         for (i = 0; i < phys_avail.n_regions; ++i)
362                 free_bootmem(phys_avail.regions[i].address,
363                              phys_avail.regions[i].size);
364
365         init_bootmem_done = 1;
366 }
367
368 /*
369  * paging_init() sets up the page tables - in fact we've already done this.
370  */
371 void __init paging_init(void)
372 {
373         unsigned long zones_size[MAX_NR_ZONES], i;
374
375 #ifdef CONFIG_HIGHMEM
376         map_page(PKMAP_BASE, 0, 0);     /* XXX gross */
377         pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
378                         (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
379         map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
380         kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
381                         (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
382         kmap_prot = PAGE_KERNEL;
383 #endif /* CONFIG_HIGHMEM */
384
385         /*
386          * All pages are DMA-able so we put them all in the DMA zone.
387          */
388         zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
389         for (i = 1; i < MAX_NR_ZONES; i++)
390                 zones_size[i] = 0;
391
392 #ifdef CONFIG_HIGHMEM
393         zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
394 #endif /* CONFIG_HIGHMEM */
395
396         free_area_init(zones_size);
397 }
398
399 void __init mem_init(void)
400 {
401         unsigned long addr;
402         int codepages = 0;
403         int datapages = 0;
404         int initpages = 0;
405 #ifdef CONFIG_HIGHMEM
406         unsigned long highmem_mapnr;
407
408         highmem_mapnr = total_lowmem >> PAGE_SHIFT;
409         highmem_start_page = mem_map + highmem_mapnr;
410 #endif /* CONFIG_HIGHMEM */
411         max_mapnr = total_memory >> PAGE_SHIFT;
412
413         high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
414         num_physpages = max_mapnr;      /* RAM is assumed contiguous */
415
416         totalram_pages += free_all_bootmem();
417
418 #ifdef CONFIG_BLK_DEV_INITRD
419         /* if we are booted from BootX with an initial ramdisk,
420            make sure the ramdisk pages aren't reserved. */
421         if (initrd_start) {
422                 for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
423                         ClearPageReserved(virt_to_page(addr));
424         }
425 #endif /* CONFIG_BLK_DEV_INITRD */
426
427 #ifdef CONFIG_PPC_OF
428         /* mark the RTAS pages as reserved */
429         if ( rtas_data )
430                 for (addr = (ulong)__va(rtas_data);
431                      addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
432                      addr += PAGE_SIZE)
433                         SetPageReserved(virt_to_page(addr));
434 #endif
435 #ifdef CONFIG_PPC_PMAC
436         if (agp_special_page)
437                 SetPageReserved(virt_to_page(agp_special_page));
438 #endif
439         if ( sysmap )
440                 for (addr = (unsigned long)sysmap;
441                      addr < PAGE_ALIGN((unsigned long)sysmap+sysmap_size) ;
442                      addr += PAGE_SIZE)
443                         SetPageReserved(virt_to_page(addr));
444
445         for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
446              addr += PAGE_SIZE) {
447                 if (!PageReserved(virt_to_page(addr)))
448                         continue;
449                 if (addr < (ulong) etext)
450                         codepages++;
451                 else if (addr >= (unsigned long)&__init_begin
452                          && addr < (unsigned long)&__init_end)
453                         initpages++;
454                 else if (addr < (ulong) klimit)
455                         datapages++;
456         }
457
458 #ifdef CONFIG_HIGHMEM
459         {
460                 unsigned long pfn;
461
462                 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
463                         struct page *page = mem_map + pfn;
464
465                         ClearPageReserved(page);
466                         set_bit(PG_highmem, &page->flags);
467                         set_page_count(page, 1);
468                         __free_page(page);
469                         totalhigh_pages++;
470                 }
471                 totalram_pages += totalhigh_pages;
472         }
473 #endif /* CONFIG_HIGHMEM */
474
475         printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
476                (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
477                codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
478                initpages<< (PAGE_SHIFT-10),
479                (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
480         if (sysmap)
481                 printk("System.map loaded at 0x%08x for debugger, size: %ld bytes\n",
482                         (unsigned int)sysmap, sysmap_size);
483 #ifdef CONFIG_PPC_PMAC
484         if (agp_special_page)
485                 printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
486 #endif
487
488         /* Make sure all our pagetable pages have page->mapping
489            and page->index set correctly. */
490         for (addr = KERNELBASE; addr != 0; addr += PGDIR_SIZE) {
491                 struct page *pg;
492                 pmd_t *pmd = pmd_offset(pgd_offset_k(addr), addr);
493                 if (pmd_present(*pmd)) {
494                         pg = pmd_page(*pmd);
495                         pg->mapping = (void *) &init_mm;
496                         pg->index = addr;
497                 }
498         }
499
500         mem_init_done = 1;
501 }
502
503 /*
504  * Set phys_avail to the amount of physical memory,
505  * less the kernel text/data/bss.
506  */
507 void __init
508 set_phys_avail(unsigned long total_memory)
509 {
510         unsigned long kstart, ksize;
511
512         /*
513          * Initially, available physical memory is equivalent to all
514          * physical memory.
515          */
516
517         phys_avail.regions[0].address = PPC_MEMSTART;
518         phys_avail.regions[0].size = total_memory;
519         phys_avail.n_regions = 1;
520
521         /*
522          * Map out the kernel text/data/bss from the available physical
523          * memory.
524          */
525
526         kstart = __pa(_stext);  /* should be 0 */
527         ksize = PAGE_ALIGN(klimit - _stext);
528
529         mem_pieces_remove(&phys_avail, kstart, ksize, 0);
530         mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
531
532 #if defined(CONFIG_BLK_DEV_INITRD)
533         /* Remove the init RAM disk from the available memory. */
534         if (initrd_start) {
535                 mem_pieces_remove(&phys_avail, __pa(initrd_start),
536                                   initrd_end - initrd_start, 1);
537         }
538 #endif /* CONFIG_BLK_DEV_INITRD */
539 #ifdef CONFIG_PPC_OF
540         /* remove the RTAS pages from the available memory */
541         if (rtas_data)
542                 mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
543 #endif
544         /* remove the sysmap pages from the available memory */
545         if (sysmap)
546                 mem_pieces_remove(&phys_avail, __pa(sysmap), sysmap_size, 1);
547 #ifdef CONFIG_PPC_PMAC
548         /* Because of some uninorth weirdness, we need a page of
549          * memory as high as possible (it must be outside of the
550          * bus address seen as the AGP aperture). It will be used
551          * by the r128 DRM driver
552          *
553          * FIXME: We need to make sure that page doesn't overlap any of the\
554          * above. This could be done by improving mem_pieces_find to be able
555          * to do a backward search from the end of the list.
556          */
557         if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
558                 agp_special_page = (total_memory - PAGE_SIZE);
559                 mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
560                 agp_special_page = (unsigned long)__va(agp_special_page);
561         }
562 #endif /* CONFIG_PPC_PMAC */
563 }
564
565 /* Mark some memory as reserved by removing it from phys_avail. */
566 void __init reserve_phys_mem(unsigned long start, unsigned long size)
567 {
568         mem_pieces_remove(&phys_avail, start, size, 1);
569 }
570
571 /*
572  * This is called when a page has been modified by the kernel.
573  * It just marks the page as not i-cache clean.  We do the i-cache
574  * flush later when the page is given to a user process, if necessary.
575  */
576 void flush_dcache_page(struct page *page)
577 {
578         clear_bit(PG_arch_1, &page->flags);
579 }
580
581 void flush_dcache_icache_page(struct page *page)
582 {
583 #ifdef CONFIG_BOOKE
584         __flush_dcache_icache(kmap(page));
585         kunmap(page);
586 #else
587         __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
588 #endif
589
590 }
591 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
592 {
593         clear_page(page);
594         clear_bit(PG_arch_1, &pg->flags);
595 }
596
597 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
598                     struct page *pg)
599 {
600         copy_page(vto, vfrom);
601         clear_bit(PG_arch_1, &pg->flags);
602 }
603
604 void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
605                              unsigned long addr, int len)
606 {
607         unsigned long maddr;
608
609         maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
610         flush_icache_range(maddr, maddr + len);
611         kunmap(page);
612 }
613
614 /*
615  * This is called at the end of handling a user page fault, when the
616  * fault has been handled by updating a PTE in the linux page tables.
617  * We use it to preload an HPTE into the hash table corresponding to
618  * the updated linux PTE.
619  */
620 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
621                       pte_t pte)
622 {
623         /* handle i-cache coherency */
624         unsigned long pfn = pte_pfn(pte);
625
626         if (pfn_valid(pfn)) {
627                 struct page *page = pfn_to_page(pfn);
628                 if (!PageReserved(page)
629                     && !test_bit(PG_arch_1, &page->flags)) {
630                         if (vma->vm_mm == current->active_mm)
631                                 __flush_dcache_icache((void *) address);
632                         else
633                                 flush_dcache_icache_page(page);
634                         set_bit(PG_arch_1, &page->flags);
635                 }
636         }
637
638 #ifdef CONFIG_PPC_STD_MMU
639         /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
640         if (Hash != 0 && pte_young(pte)) {
641                 struct mm_struct *mm;
642                 pmd_t *pmd;
643
644                 mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
645                 pmd = pmd_offset(pgd_offset(mm, address), address);
646                 if (!pmd_none(*pmd))
647                         add_hash_page(mm->context, address, pmd_val(*pmd));
648         }
649 #endif
650 }