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