This commit was generated by cvs2svn to compensate for changes in r925,
[linux-2.6.git] / arch / ppc64 / 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  *
10  *  Derived from "arch/i386/mm/init.c"
11  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
12  *
13  *  Dave Engebretsen <engebret@us.ibm.com>
14  *      Rework for PPC64 port.
15  *
16  *  This program is free software; you can redistribute it and/or
17  *  modify it under the terms of the GNU General Public License
18  *  as published by the Free Software Foundation; either version
19  *  2 of the License, or (at your option) any later version.
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/mman.h>
31 #include <linux/mm.h>
32 #include <linux/swap.h>
33 #include <linux/stddef.h>
34 #include <linux/vmalloc.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/bootmem.h>
38 #include <linux/highmem.h>
39 #include <linux/idr.h>
40 #include <linux/nodemask.h>
41 #include <linux/module.h>
42
43 #include <asm/pgalloc.h>
44 #include <asm/page.h>
45 #include <asm/abs_addr.h>
46 #include <asm/prom.h>
47 #include <asm/lmb.h>
48 #include <asm/rtas.h>
49 #include <asm/io.h>
50 #include <asm/mmu_context.h>
51 #include <asm/pgtable.h>
52 #include <asm/mmu.h>
53 #include <asm/uaccess.h>
54 #include <asm/smp.h>
55 #include <asm/machdep.h>
56 #include <asm/tlb.h>
57 #include <asm/eeh.h>
58 #include <asm/processor.h>
59 #include <asm/mmzone.h>
60 #include <asm/cputable.h>
61 #include <asm/ppcdebug.h>
62 #include <asm/sections.h>
63 #include <asm/system.h>
64 #include <asm/iommu.h>
65 #include <asm/abs_addr.h>
66 #include <asm/vdso.h>
67 #include <asm/imalloc.h>
68
69 int mem_init_done;
70 unsigned long ioremap_bot = IMALLOC_BASE;
71 static unsigned long phbs_io_bot = PHBS_IO_BASE;
72
73 extern pgd_t swapper_pg_dir[];
74 extern struct task_struct *current_set[NR_CPUS];
75
76 extern pgd_t ioremap_dir[];
77 pgd_t * ioremap_pgd = (pgd_t *)&ioremap_dir;
78
79 unsigned long klimit = (unsigned long)_end;
80
81 unsigned long _SDR1=0;
82 unsigned long _ASR=0;
83
84 /* max amount of RAM to use */
85 unsigned long __max_memory;
86
87 /* info on what we think the IO hole is */
88 unsigned long   io_hole_start;
89 unsigned long   io_hole_size;
90
91 void show_mem(void)
92 {
93         unsigned long total = 0, reserved = 0;
94         unsigned long shared = 0, cached = 0;
95         struct page *page;
96         pg_data_t *pgdat;
97         unsigned long i;
98
99         printk("Mem-info:\n");
100         show_free_areas();
101         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
102         for_each_pgdat(pgdat) {
103                 for (i = 0; i < pgdat->node_spanned_pages; i++) {
104                         page = pgdat->node_mem_map + i;
105                         total++;
106                         if (PageReserved(page))
107                                 reserved++;
108                         else if (PageSwapCache(page))
109                                 cached++;
110                         else if (page_count(page))
111                                 shared += page_count(page) - 1;
112                 }
113         }
114         printk("%ld pages of RAM\n", total);
115         printk("%ld reserved pages\n", reserved);
116         printk("%ld pages shared\n", shared);
117         printk("%ld pages swap cached\n", cached);
118 }
119 EXPORT_SYMBOL_GPL(show_mem);
120
121 #ifdef CONFIG_PPC_ISERIES
122
123 void __iomem *ioremap(unsigned long addr, unsigned long size)
124 {
125         return (void __iomem *)addr;
126 }
127
128 extern void __iomem *__ioremap(unsigned long addr, unsigned long size,
129                        unsigned long flags)
130 {
131         return (void __iomem *)addr;
132 }
133
134 void iounmap(volatile void __iomem *addr)
135 {
136         return;
137 }
138
139 #else
140
141 static void unmap_im_area_pte(pmd_t *pmd, unsigned long addr,
142                                   unsigned long end)
143 {
144         pte_t *pte;
145
146         pte = pte_offset_kernel(pmd, addr);
147         do {
148                 pte_t ptent = ptep_get_and_clear(&ioremap_mm, addr, pte);
149                 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
150         } while (pte++, addr += PAGE_SIZE, addr != end);
151 }
152
153 static inline void unmap_im_area_pmd(pud_t *pud, unsigned long addr,
154                                      unsigned long end)
155 {
156         pmd_t *pmd;
157         unsigned long next;
158
159         pmd = pmd_offset(pud, addr);
160         do {
161                 next = pmd_addr_end(addr, end);
162                 if (pmd_none_or_clear_bad(pmd))
163                         continue;
164                 unmap_im_area_pte(pmd, addr, next);
165         } while (pmd++, addr = next, addr != end);
166 }
167
168 static inline void unmap_im_area_pud(pgd_t *pgd, unsigned long addr,
169                                      unsigned long end)
170 {
171         pud_t *pud;
172         unsigned long next;
173
174         pud = pud_offset(pgd, addr);
175         do {
176                 next = pud_addr_end(addr, end);
177                 if (pud_none_or_clear_bad(pud))
178                         continue;
179                 unmap_im_area_pmd(pud, addr, next);
180         } while (pud++, addr = next, addr != end);
181 }
182
183 static void unmap_im_area(unsigned long addr, unsigned long end)
184 {
185         struct mm_struct *mm = &ioremap_mm;
186         unsigned long next;
187         pgd_t *pgd;
188
189         spin_lock(&mm->page_table_lock);
190
191         pgd = pgd_offset_i(addr);
192         flush_cache_vunmap(addr, end);
193         do {
194                 next = pgd_addr_end(addr, end);
195                 if (pgd_none_or_clear_bad(pgd))
196                         continue;
197                 unmap_im_area_pud(pgd, addr, next);
198         } while (pgd++, addr = next, addr != end);
199         flush_tlb_kernel_range(start, end);
200
201         spin_unlock(&mm->page_table_lock);
202 }
203
204 /*
205  * map_io_page currently only called by __ioremap
206  * map_io_page adds an entry to the ioremap page table
207  * and adds an entry to the HPT, possibly bolting it
208  */
209 static int map_io_page(unsigned long ea, unsigned long pa, int flags)
210 {
211         pgd_t *pgdp;
212         pud_t *pudp;
213         pmd_t *pmdp;
214         pte_t *ptep;
215         unsigned long vsid;
216
217         if (mem_init_done) {
218                 spin_lock(&ioremap_mm.page_table_lock);
219                 pgdp = pgd_offset_i(ea);
220                 pudp = pud_alloc(&ioremap_mm, pgdp, ea);
221                 if (!pudp)
222                         return -ENOMEM;
223                 pmdp = pmd_alloc(&ioremap_mm, pudp, ea);
224                 if (!pmdp)
225                         return -ENOMEM;
226                 ptep = pte_alloc_kernel(&ioremap_mm, pmdp, ea);
227                 if (!ptep)
228                         return -ENOMEM;
229                 pa = abs_to_phys(pa);
230                 set_pte_at(&ioremap_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
231                                                           __pgprot(flags)));
232                 spin_unlock(&ioremap_mm.page_table_lock);
233         } else {
234                 unsigned long va, vpn, hash, hpteg;
235
236                 /*
237                  * If the mm subsystem is not fully up, we cannot create a
238                  * linux page table entry for this mapping.  Simply bolt an
239                  * entry in the hardware page table.
240                  */
241                 vsid = get_kernel_vsid(ea);
242                 va = (vsid << 28) | (ea & 0xFFFFFFF);
243                 vpn = va >> PAGE_SHIFT;
244
245                 hash = hpt_hash(vpn, 0);
246
247                 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
248
249                 /* Panic if a pte grpup is full */
250                 if (ppc_md.hpte_insert(hpteg, va, pa >> PAGE_SHIFT, 0,
251                                        _PAGE_NO_CACHE|_PAGE_GUARDED|PP_RWXX,
252                                        1, 0) == -1) {
253                         panic("map_io_page: could not insert mapping");
254                 }
255         }
256         return 0;
257 }
258
259
260 static void __iomem * __ioremap_com(unsigned long addr, unsigned long pa,
261                             unsigned long ea, unsigned long size,
262                             unsigned long flags)
263 {
264         unsigned long i;
265
266         if ((flags & _PAGE_PRESENT) == 0)
267                 flags |= pgprot_val(PAGE_KERNEL);
268
269         for (i = 0; i < size; i += PAGE_SIZE)
270                 if (map_io_page(ea+i, pa+i, flags))
271                         goto failure;
272
273         return (void __iomem *) (ea + (addr & ~PAGE_MASK));
274  failure:
275         if (mem_init_done)
276                 unmap_im_area(ea, ea + size);
277         return NULL;
278 }
279
280
281 void __iomem *
282 ioremap(unsigned long addr, unsigned long size)
283 {
284         return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED);
285 }
286
287 void __iomem * __ioremap(unsigned long addr, unsigned long size,
288                          unsigned long flags)
289 {
290         unsigned long pa, ea;
291         void __iomem *ret;
292
293         /*
294          * Choose an address to map it to.
295          * Once the imalloc system is running, we use it.
296          * Before that, we map using addresses going
297          * up from ioremap_bot.  imalloc will use
298          * the addresses from ioremap_bot through
299          * IMALLOC_END (0xE000001fffffffff)
300          * 
301          */
302         pa = addr & PAGE_MASK;
303         size = PAGE_ALIGN(addr + size) - pa;
304
305         if (size == 0)
306                 return NULL;
307
308         if (mem_init_done) {
309                 struct vm_struct *area;
310                 area = im_get_free_area(size);
311                 if (area == NULL)
312                         return NULL;
313                 ea = (unsigned long)(area->addr);
314                 ret = __ioremap_com(addr, pa, ea, size, flags);
315                 if (!ret)
316                         im_free(area->addr);
317         } else {
318                 ea = ioremap_bot;
319                 ret = __ioremap_com(addr, pa, ea, size, flags);
320                 if (ret)
321                         ioremap_bot += size;
322         }
323         return ret;
324 }
325
326 #define IS_PAGE_ALIGNED(_val) ((_val) == ((_val) & PAGE_MASK))
327
328 int __ioremap_explicit(unsigned long pa, unsigned long ea,
329                        unsigned long size, unsigned long flags)
330 {
331         struct vm_struct *area;
332         void __iomem *ret;
333         
334         /* For now, require page-aligned values for pa, ea, and size */
335         if (!IS_PAGE_ALIGNED(pa) || !IS_PAGE_ALIGNED(ea) ||
336             !IS_PAGE_ALIGNED(size)) {
337                 printk(KERN_ERR "unaligned value in %s\n", __FUNCTION__);
338                 return 1;
339         }
340         
341         if (!mem_init_done) {
342                 /* Two things to consider in this case:
343                  * 1) No records will be kept (imalloc, etc) that the region
344                  *    has been remapped
345                  * 2) It won't be easy to iounmap() the region later (because
346                  *    of 1)
347                  */
348                 ;
349         } else {
350                 area = im_get_area(ea, size,
351                         IM_REGION_UNUSED|IM_REGION_SUBSET|IM_REGION_EXISTS);
352                 if (area == NULL) {
353                         /* Expected when PHB-dlpar is in play */
354                         return 1;
355                 }
356                 if (ea != (unsigned long) area->addr) {
357                         printk(KERN_ERR "unexpected addr return from "
358                                "im_get_area\n");
359                         return 1;
360                 }
361         }
362         
363         ret = __ioremap_com(pa, pa, ea, size, flags);
364         if (ret == NULL) {
365                 printk(KERN_ERR "ioremap_explicit() allocation failure !\n");
366                 return 1;
367         }
368         if (ret != (void *) ea) {
369                 printk(KERN_ERR "__ioremap_com() returned unexpected addr\n");
370                 return 1;
371         }
372
373         return 0;
374 }
375
376 /*  
377  * Unmap an IO region and remove it from imalloc'd list.
378  * Access to IO memory should be serialized by driver.
379  * This code is modeled after vmalloc code - unmap_vm_area()
380  *
381  * XXX  what about calls before mem_init_done (ie python_countermeasures())
382  */
383 void iounmap(volatile void __iomem *token)
384 {
385         unsigned long address, size;
386         void *addr;
387
388         if (!mem_init_done)
389                 return;
390         
391         addr = (void *) ((unsigned long __force) token & PAGE_MASK);
392         
393         if ((size = im_free(addr)) == 0)
394                 return;
395
396         address = (unsigned long)addr; 
397         unmap_im_area(address, address + size);
398 }
399
400 static int iounmap_subset_regions(unsigned long addr, unsigned long size)
401 {
402         struct vm_struct *area;
403
404         /* Check whether subsets of this region exist */
405         area = im_get_area(addr, size, IM_REGION_SUPERSET);
406         if (area == NULL)
407                 return 1;
408
409         while (area) {
410                 iounmap((void __iomem *) area->addr);
411                 area = im_get_area(addr, size,
412                                 IM_REGION_SUPERSET);
413         }
414
415         return 0;
416 }
417
418 int iounmap_explicit(volatile void __iomem *start, unsigned long size)
419 {
420         struct vm_struct *area;
421         unsigned long addr;
422         int rc;
423         
424         addr = (unsigned long __force) start & PAGE_MASK;
425
426         /* Verify that the region either exists or is a subset of an existing
427          * region.  In the latter case, split the parent region to create 
428          * the exact region 
429          */
430         area = im_get_area(addr, size, 
431                             IM_REGION_EXISTS | IM_REGION_SUBSET);
432         if (area == NULL) {
433                 /* Determine whether subset regions exist.  If so, unmap */
434                 rc = iounmap_subset_regions(addr, size);
435                 if (rc) {
436                         printk(KERN_ERR
437                                "%s() cannot unmap nonexistent range 0x%lx\n",
438                                 __FUNCTION__, addr);
439                         return 1;
440                 }
441         } else {
442                 iounmap((void __iomem *) area->addr);
443         }
444         /*
445          * FIXME! This can't be right:
446         iounmap(area->addr);
447          * Maybe it should be "iounmap(area);"
448          */
449         return 0;
450 }
451
452 #endif
453
454 EXPORT_SYMBOL(ioremap);
455 EXPORT_SYMBOL(__ioremap);
456 EXPORT_SYMBOL(iounmap);
457
458 void free_initmem(void)
459 {
460         unsigned long addr;
461
462         addr = (unsigned long)__init_begin;
463         for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) {
464                 ClearPageReserved(virt_to_page(addr));
465                 set_page_count(virt_to_page(addr), 1);
466                 free_page(addr);
467                 totalram_pages++;
468         }
469         printk ("Freeing unused kernel memory: %luk freed\n",
470                 ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10);
471 }
472
473 #ifdef CONFIG_BLK_DEV_INITRD
474 void free_initrd_mem(unsigned long start, unsigned long end)
475 {
476         if (start < end)
477                 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
478         for (; start < end; start += PAGE_SIZE) {
479                 ClearPageReserved(virt_to_page(start));
480                 set_page_count(virt_to_page(start), 1);
481                 free_page(start);
482                 totalram_pages++;
483         }
484 }
485 #endif
486
487 static DEFINE_SPINLOCK(mmu_context_lock);
488 static DEFINE_IDR(mmu_context_idr);
489
490 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
491 {
492         int index;
493         int err;
494
495 #ifdef CONFIG_HUGETLB_PAGE
496         /* We leave htlb_segs as it was, but for a fork, we need to
497          * clear the huge_pgdir. */
498         mm->context.huge_pgdir = NULL;
499 #endif
500
501 again:
502         if (!idr_pre_get(&mmu_context_idr, GFP_KERNEL))
503                 return -ENOMEM;
504
505         spin_lock(&mmu_context_lock);
506         err = idr_get_new_above(&mmu_context_idr, NULL, 1, &index);
507         spin_unlock(&mmu_context_lock);
508
509         if (err == -EAGAIN)
510                 goto again;
511         else if (err)
512                 return err;
513
514         if (index > MAX_CONTEXT) {
515                 idr_remove(&mmu_context_idr, index);
516                 return -ENOMEM;
517         }
518
519         mm->context.id = index;
520
521         return 0;
522 }
523
524 void destroy_context(struct mm_struct *mm)
525 {
526         spin_lock(&mmu_context_lock);
527         idr_remove(&mmu_context_idr, mm->context.id);
528         spin_unlock(&mmu_context_lock);
529
530         mm->context.id = NO_CONTEXT;
531
532         hugetlb_mm_free_pgd(mm);
533 }
534
535 /*
536  * Do very early mm setup.
537  */
538 void __init mm_init_ppc64(void)
539 {
540 #ifndef CONFIG_PPC_ISERIES
541         unsigned long i;
542 #endif
543
544         ppc64_boot_msg(0x100, "MM Init");
545
546         /* This is the story of the IO hole... please, keep seated,
547          * unfortunately, we are out of oxygen masks at the moment.
548          * So we need some rough way to tell where your big IO hole
549          * is. On pmac, it's between 2G and 4G, on POWER3, it's around
550          * that area as well, on POWER4 we don't have one, etc...
551          * We need that as a "hint" when sizing the TCE table on POWER3
552          * So far, the simplest way that seem work well enough for us it
553          * to just assume that the first discontinuity in our physical
554          * RAM layout is the IO hole. That may not be correct in the future
555          * (and isn't on iSeries but then we don't care ;)
556          */
557
558 #ifndef CONFIG_PPC_ISERIES
559         for (i = 1; i < lmb.memory.cnt; i++) {
560                 unsigned long base, prevbase, prevsize;
561
562                 prevbase = lmb.memory.region[i-1].physbase;
563                 prevsize = lmb.memory.region[i-1].size;
564                 base = lmb.memory.region[i].physbase;
565                 if (base > (prevbase + prevsize)) {
566                         io_hole_start = prevbase + prevsize;
567                         io_hole_size = base  - (prevbase + prevsize);
568                         break;
569                 }
570         }
571 #endif /* CONFIG_PPC_ISERIES */
572         if (io_hole_start)
573                 printk("IO Hole assumed to be %lx -> %lx\n",
574                        io_hole_start, io_hole_start + io_hole_size - 1);
575
576         ppc64_boot_msg(0x100, "MM Init Done");
577 }
578
579 /*
580  * This is called by /dev/mem to know if a given address has to
581  * be mapped non-cacheable or not
582  */
583 int page_is_ram(unsigned long pfn)
584 {
585         int i;
586         unsigned long paddr = (pfn << PAGE_SHIFT);
587
588         for (i=0; i < lmb.memory.cnt; i++) {
589                 unsigned long base;
590
591 #ifdef CONFIG_MSCHUNKS
592                 base = lmb.memory.region[i].physbase;
593 #else
594                 base = lmb.memory.region[i].base;
595 #endif
596                 if ((paddr >= base) &&
597                         (paddr < (base + lmb.memory.region[i].size))) {
598                         return 1;
599                 }
600         }
601
602         return 0;
603 }
604 EXPORT_SYMBOL(page_is_ram);
605
606 unsigned long next_ram_page(unsigned long pfn)
607 {
608         int i;
609         unsigned long paddr, base;
610         unsigned long best_base = (ULONG_MAX << PAGE_SHIFT);
611
612         pfn++;
613         paddr = (pfn << PAGE_SHIFT);
614                                                                                 
615         for (i=0; i < lmb.memory.cnt; i++) {
616 #ifdef CONFIG_MSCHUNKS
617                 base = lmb.memory.region[i].physbase;
618 #else
619                 base = lmb.memory.region[i].base;
620 #endif
621                 if ((paddr >= base)
622                     && (paddr < (base + lmb.memory.region[i].size)))
623                         return (paddr >> PAGE_SHIFT);
624                 if ((paddr < base) && (base < best_base))
625                         best_base = base;
626         }
627         if (best_base < (ULONG_MAX << PAGE_SHIFT))
628                 return (best_base >> PAGE_SHIFT);
629         else
630                 return ULONG_MAX;
631 }
632 EXPORT_SYMBOL_GPL(next_ram_page);
633
634 /*
635  * Initialize the bootmem system and give it all the memory we
636  * have available.
637  */
638 #ifndef CONFIG_DISCONTIGMEM
639 void __init do_init_bootmem(void)
640 {
641         unsigned long i;
642         unsigned long start, bootmap_pages;
643         unsigned long total_pages = lmb_end_of_DRAM() >> PAGE_SHIFT;
644         int boot_mapsize;
645
646         /*
647          * Find an area to use for the bootmem bitmap.  Calculate the size of
648          * bitmap required as (Total Memory) / PAGE_SIZE / BITS_PER_BYTE.
649          * Add 1 additional page in case the address isn't page-aligned.
650          */
651         bootmap_pages = bootmem_bootmap_pages(total_pages);
652
653         start = abs_to_phys(lmb_alloc(bootmap_pages<<PAGE_SHIFT, PAGE_SIZE));
654         BUG_ON(!start);
655
656         boot_mapsize = init_bootmem(start >> PAGE_SHIFT, total_pages);
657
658         max_pfn = max_low_pfn;
659
660         /* add all physical memory to the bootmem map. Also find the first */
661         for (i=0; i < lmb.memory.cnt; i++) {
662                 unsigned long physbase, size;
663
664                 physbase = lmb.memory.region[i].physbase;
665                 size = lmb.memory.region[i].size;
666                 free_bootmem(physbase, size);
667         }
668
669         /* reserve the sections we're already using */
670         for (i=0; i < lmb.reserved.cnt; i++) {
671                 unsigned long physbase = lmb.reserved.region[i].physbase;
672                 unsigned long size = lmb.reserved.region[i].size;
673
674                 reserve_bootmem(physbase, size);
675         }
676 }
677
678 /*
679  * paging_init() sets up the page tables - in fact we've already done this.
680  */
681 void __init paging_init(void)
682 {
683         unsigned long zones_size[MAX_NR_ZONES];
684         unsigned long zholes_size[MAX_NR_ZONES];
685         unsigned long total_ram = lmb_phys_mem_size();
686         unsigned long top_of_ram = lmb_end_of_DRAM();
687
688         printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
689                top_of_ram, total_ram);
690         printk(KERN_INFO "Memory hole size: %ldMB\n",
691                (top_of_ram - total_ram) >> 20);
692         /*
693          * All pages are DMA-able so we put them all in the DMA zone.
694          */
695         memset(zones_size, 0, sizeof(zones_size));
696         memset(zholes_size, 0, sizeof(zholes_size));
697
698         zones_size[ZONE_DMA] = top_of_ram >> PAGE_SHIFT;
699         zholes_size[ZONE_DMA] = (top_of_ram - total_ram) >> PAGE_SHIFT;
700
701         free_area_init_node(0, NODE_DATA(0), zones_size,
702                             __pa(PAGE_OFFSET) >> PAGE_SHIFT, zholes_size);
703 }
704 #endif /* CONFIG_DISCONTIGMEM */
705
706 static struct kcore_list kcore_vmem;
707
708 static int __init setup_kcore(void)
709 {
710         int i;
711
712         for (i=0; i < lmb.memory.cnt; i++) {
713                 unsigned long physbase, size;
714                 struct kcore_list *kcore_mem;
715
716                 physbase = lmb.memory.region[i].physbase;
717                 size = lmb.memory.region[i].size;
718
719                 /* GFP_ATOMIC to avoid might_sleep warnings during boot */
720                 kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC);
721                 if (!kcore_mem)
722                         panic("mem_init: kmalloc failed\n");
723
724                 kclist_add(kcore_mem, __va(physbase), size);
725         }
726
727         kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
728
729         return 0;
730 }
731 module_init(setup_kcore);
732
733 void __init mem_init(void)
734 {
735 #ifdef CONFIG_DISCONTIGMEM
736         int nid;
737 #endif
738         pg_data_t *pgdat;
739         unsigned long i;
740         struct page *page;
741         unsigned long reservedpages = 0, codesize, initsize, datasize, bsssize;
742
743         num_physpages = max_low_pfn;    /* RAM is assumed contiguous */
744         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
745
746 #ifdef CONFIG_DISCONTIGMEM
747         for_each_online_node(nid) {
748                 if (NODE_DATA(nid)->node_spanned_pages != 0) {
749                         printk("freeing bootmem node %x\n", nid);
750                         totalram_pages +=
751                                 free_all_bootmem_node(NODE_DATA(nid));
752                 }
753         }
754 #else
755         max_mapnr = num_physpages;
756         totalram_pages += free_all_bootmem();
757 #endif
758
759 #ifdef CONFIG_PPC_PSERIES
760         /* Mark the RTAS pages as PG_reserved so userspace can mmap them */
761         if (rtas_rmo_buf) {
762                 unsigned long pfn, start_pfn, end_pfn;
763
764                 start_pfn = rtas_rmo_buf >> PAGE_SHIFT;
765                 end_pfn = (rtas_rmo_buf + RTAS_RMOBUF_MAX) >>  PAGE_SHIFT;
766                 for (pfn = start_pfn; pfn < end_pfn; pfn++)
767                         SetPageReserved(pfn_to_page(pfn));
768         }
769 #endif
770
771         for_each_pgdat(pgdat) {
772                 for (i = 0; i < pgdat->node_spanned_pages; i++) {
773                         page = pgdat->node_mem_map + i;
774                         if (PageReserved(page))
775                                 reservedpages++;
776                 }
777         }
778
779         codesize = (unsigned long)&_etext - (unsigned long)&_stext;
780         initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin;
781         datasize = (unsigned long)&_edata - (unsigned long)&__init_end;
782         bsssize = (unsigned long)&__bss_stop - (unsigned long)&__bss_start;
783
784         printk(KERN_INFO "Memory: %luk/%luk available (%luk kernel code, "
785                "%luk reserved, %luk data, %luk bss, %luk init)\n",
786                 (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
787                 num_physpages << (PAGE_SHIFT-10),
788                 codesize >> 10,
789                 reservedpages << (PAGE_SHIFT-10),
790                 datasize >> 10,
791                 bsssize >> 10,
792                 initsize >> 10);
793
794         mem_init_done = 1;
795
796 #ifdef CONFIG_PPC_ISERIES
797         iommu_vio_init();
798 #endif
799         /* Initialize the vDSO */
800         vdso_init();
801 }
802
803 /*
804  * This is called when a page has been modified by the kernel.
805  * It just marks the page as not i-cache clean.  We do the i-cache
806  * flush later when the page is given to a user process, if necessary.
807  */
808 void flush_dcache_page(struct page *page)
809 {
810         if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
811                 return;
812         /* avoid an atomic op if possible */
813         if (test_bit(PG_arch_1, &page->flags))
814                 clear_bit(PG_arch_1, &page->flags);
815 }
816 EXPORT_SYMBOL(flush_dcache_page);
817
818 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
819 {
820         clear_page(page);
821
822         if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
823                 return;
824         /*
825          * We shouldnt have to do this, but some versions of glibc
826          * require it (ld.so assumes zero filled pages are icache clean)
827          * - Anton
828          */
829
830         /* avoid an atomic op if possible */
831         if (test_bit(PG_arch_1, &pg->flags))
832                 clear_bit(PG_arch_1, &pg->flags);
833 }
834 EXPORT_SYMBOL(clear_user_page);
835
836 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
837                     struct page *pg)
838 {
839         copy_page(vto, vfrom);
840
841         /*
842          * We should be able to use the following optimisation, however
843          * there are two problems.
844          * Firstly a bug in some versions of binutils meant PLT sections
845          * were not marked executable.
846          * Secondly the first word in the GOT section is blrl, used
847          * to establish the GOT address. Until recently the GOT was
848          * not marked executable.
849          * - Anton
850          */
851 #if 0
852         if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
853                 return;
854 #endif
855
856         if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
857                 return;
858
859         /* avoid an atomic op if possible */
860         if (test_bit(PG_arch_1, &pg->flags))
861                 clear_bit(PG_arch_1, &pg->flags);
862 }
863
864 void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
865                              unsigned long addr, int len)
866 {
867         unsigned long maddr;
868
869         maddr = (unsigned long)page_address(page) + (addr & ~PAGE_MASK);
870         flush_icache_range(maddr, maddr + len);
871 }
872 EXPORT_SYMBOL(flush_icache_user_range);
873
874 /*
875  * This is called at the end of handling a user page fault, when the
876  * fault has been handled by updating a PTE in the linux page tables.
877  * We use it to preload an HPTE into the hash table corresponding to
878  * the updated linux PTE.
879  * 
880  * This must always be called with the mm->page_table_lock held
881  */
882 void update_mmu_cache(struct vm_area_struct *vma, unsigned long ea,
883                       pte_t pte)
884 {
885         unsigned long vsid;
886         void *pgdir;
887         pte_t *ptep;
888         int local = 0;
889         cpumask_t tmp;
890         unsigned long flags;
891
892         /* handle i-cache coherency */
893         if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE) &&
894             !cpu_has_feature(CPU_FTR_NOEXECUTE)) {
895                 unsigned long pfn = pte_pfn(pte);
896                 if (pfn_valid(pfn)) {
897                         struct page *page = pfn_to_page(pfn);
898                         if (!PageReserved(page)
899                             && !test_bit(PG_arch_1, &page->flags)) {
900                                 __flush_dcache_icache(page_address(page));
901                                 set_bit(PG_arch_1, &page->flags);
902                         }
903                 }
904         }
905
906         /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
907         if (!pte_young(pte))
908                 return;
909
910         pgdir = vma->vm_mm->pgd;
911         if (pgdir == NULL)
912                 return;
913
914         ptep = find_linux_pte(pgdir, ea);
915         if (!ptep)
916                 return;
917
918         vsid = get_vsid(vma->vm_mm->context.id, ea);
919
920         local_irq_save(flags);
921         tmp = cpumask_of_cpu(smp_processor_id());
922         if (cpus_equal(vma->vm_mm->cpu_vm_mask, tmp))
923                 local = 1;
924
925         __hash_page(ea, pte_val(pte) & (_PAGE_USER|_PAGE_RW), vsid, ptep,
926                     0x300, local);
927         local_irq_restore(flags);
928 }
929
930 void __iomem * reserve_phb_iospace(unsigned long size)
931 {
932         void __iomem *virt_addr;
933                 
934         if (phbs_io_bot >= IMALLOC_BASE) 
935                 panic("reserve_phb_iospace(): phb io space overflow\n");
936                         
937         virt_addr = (void __iomem *) phbs_io_bot;
938         phbs_io_bot += size;
939
940         return virt_addr;
941 }
942
943 kmem_cache_t *zero_cache;
944
945 static void zero_ctor(void *pte, kmem_cache_t *cache, unsigned long flags)
946 {
947         memset(pte, 0, PAGE_SIZE);
948 }
949
950 void pgtable_cache_init(void)
951 {
952         zero_cache = kmem_cache_create("zero",
953                                 PAGE_SIZE,
954                                 0,
955                                 SLAB_HWCACHE_ALIGN | SLAB_MUST_HWCACHE_ALIGN,
956                                 zero_ctor,
957                                 NULL);
958         if (!zero_cache)
959                 panic("pgtable_cache_init(): could not create zero_cache!\n");
960 }
961
962 pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr,
963                               unsigned long size, pgprot_t vma_prot)
964 {
965         if (ppc_md.phys_mem_access_prot)
966                 return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot);
967
968         if (!page_is_ram(addr >> PAGE_SHIFT))
969                 vma_prot = __pgprot(pgprot_val(vma_prot)
970                                     | _PAGE_GUARDED | _PAGE_NO_CACHE);
971         return vma_prot;
972 }
973 EXPORT_SYMBOL(phys_mem_access_prot);