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