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