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