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