d703b67bc7b9e7161f362396dc3cacf4f273647a
[powerpc.git] / arch / sparc64 / mm / init.c
1 /*  $Id: init.c,v 1.209 2002/02/09 19:49:31 davem Exp $
2  *  arch/sparc64/mm/init.c
3  *
4  *  Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6  */
7  
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/mm.h>
16 #include <linux/hugetlb.h>
17 #include <linux/slab.h>
18 #include <linux/initrd.h>
19 #include <linux/swap.h>
20 #include <linux/pagemap.h>
21 #include <linux/fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/kprobes.h>
24 #include <linux/cache.h>
25 #include <linux/sort.h>
26
27 #include <asm/head.h>
28 #include <asm/system.h>
29 #include <asm/page.h>
30 #include <asm/pgalloc.h>
31 #include <asm/pgtable.h>
32 #include <asm/oplib.h>
33 #include <asm/iommu.h>
34 #include <asm/io.h>
35 #include <asm/uaccess.h>
36 #include <asm/mmu_context.h>
37 #include <asm/tlbflush.h>
38 #include <asm/dma.h>
39 #include <asm/starfire.h>
40 #include <asm/tlb.h>
41 #include <asm/spitfire.h>
42 #include <asm/sections.h>
43 #include <asm/tsb.h>
44 #include <asm/hypervisor.h>
45
46 extern void device_scan(void);
47
48 #define MAX_PHYS_ADDRESS        (1UL << 42UL)
49 #define KPTE_BITMAP_CHUNK_SZ    (256UL * 1024UL * 1024UL)
50 #define KPTE_BITMAP_BYTES       \
51         ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
52
53 unsigned long kern_linear_pte_xor[2] __read_mostly;
54
55 /* A bitmap, one bit for every 256MB of physical memory.  If the bit
56  * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
57  * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
58  */
59 unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
60
61 /* A special kernel TSB for 4MB and 256MB linear mappings.  */
62 struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
63
64 #define MAX_BANKS       32
65
66 static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
67 static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
68 static int pavail_ents __initdata;
69 static int pavail_rescan_ents __initdata;
70
71 static int cmp_p64(const void *a, const void *b)
72 {
73         const struct linux_prom64_registers *x = a, *y = b;
74
75         if (x->phys_addr > y->phys_addr)
76                 return 1;
77         if (x->phys_addr < y->phys_addr)
78                 return -1;
79         return 0;
80 }
81
82 static void __init read_obp_memory(const char *property,
83                                    struct linux_prom64_registers *regs,
84                                    int *num_ents)
85 {
86         int node = prom_finddevice("/memory");
87         int prop_size = prom_getproplen(node, property);
88         int ents, ret, i;
89
90         ents = prop_size / sizeof(struct linux_prom64_registers);
91         if (ents > MAX_BANKS) {
92                 prom_printf("The machine has more %s property entries than "
93                             "this kernel can support (%d).\n",
94                             property, MAX_BANKS);
95                 prom_halt();
96         }
97
98         ret = prom_getproperty(node, property, (char *) regs, prop_size);
99         if (ret == -1) {
100                 prom_printf("Couldn't get %s property from /memory.\n");
101                 prom_halt();
102         }
103
104         *num_ents = ents;
105
106         /* Sanitize what we got from the firmware, by page aligning
107          * everything.
108          */
109         for (i = 0; i < ents; i++) {
110                 unsigned long base, size;
111
112                 base = regs[i].phys_addr;
113                 size = regs[i].reg_size;
114
115                 size &= PAGE_MASK;
116                 if (base & ~PAGE_MASK) {
117                         unsigned long new_base = PAGE_ALIGN(base);
118
119                         size -= new_base - base;
120                         if ((long) size < 0L)
121                                 size = 0UL;
122                         base = new_base;
123                 }
124                 regs[i].phys_addr = base;
125                 regs[i].reg_size = size;
126         }
127         sort(regs, ents, sizeof(struct linux_prom64_registers),
128              cmp_p64, NULL);
129 }
130
131 unsigned long *sparc64_valid_addr_bitmap __read_mostly;
132
133 /* Kernel physical address base and size in bytes.  */
134 unsigned long kern_base __read_mostly;
135 unsigned long kern_size __read_mostly;
136
137 /* get_new_mmu_context() uses "cache + 1".  */
138 DEFINE_SPINLOCK(ctx_alloc_lock);
139 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
140 #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
141 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
142
143 /* References to special section boundaries */
144 extern char  _start[], _end[];
145
146 /* Initial ramdisk setup */
147 extern unsigned long sparc_ramdisk_image64;
148 extern unsigned int sparc_ramdisk_image;
149 extern unsigned int sparc_ramdisk_size;
150
151 struct page *mem_map_zero __read_mostly;
152
153 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
154
155 unsigned long sparc64_kern_pri_context __read_mostly;
156 unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
157 unsigned long sparc64_kern_sec_context __read_mostly;
158
159 int bigkernel = 0;
160
161 kmem_cache_t *pgtable_cache __read_mostly;
162
163 static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
164 {
165         clear_page(addr);
166 }
167
168 void pgtable_cache_init(void)
169 {
170         pgtable_cache = kmem_cache_create("pgtable_cache",
171                                           PAGE_SIZE, PAGE_SIZE,
172                                           SLAB_HWCACHE_ALIGN |
173                                           SLAB_MUST_HWCACHE_ALIGN,
174                                           zero_ctor,
175                                           NULL);
176         if (!pgtable_cache) {
177                 prom_printf("pgtable_cache_init(): Could not create!\n");
178                 prom_halt();
179         }
180 }
181
182 #ifdef CONFIG_DEBUG_DCFLUSH
183 atomic_t dcpage_flushes = ATOMIC_INIT(0);
184 #ifdef CONFIG_SMP
185 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
186 #endif
187 #endif
188
189 inline void flush_dcache_page_impl(struct page *page)
190 {
191         BUG_ON(tlb_type == hypervisor);
192 #ifdef CONFIG_DEBUG_DCFLUSH
193         atomic_inc(&dcpage_flushes);
194 #endif
195
196 #ifdef DCACHE_ALIASING_POSSIBLE
197         __flush_dcache_page(page_address(page),
198                             ((tlb_type == spitfire) &&
199                              page_mapping(page) != NULL));
200 #else
201         if (page_mapping(page) != NULL &&
202             tlb_type == spitfire)
203                 __flush_icache_page(__pa(page_address(page)));
204 #endif
205 }
206
207 #define PG_dcache_dirty         PG_arch_1
208 #define PG_dcache_cpu_shift     24UL
209 #define PG_dcache_cpu_mask      (256UL - 1UL)
210
211 #if NR_CPUS > 256
212 #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
213 #endif
214
215 #define dcache_dirty_cpu(page) \
216         (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
217
218 static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
219 {
220         unsigned long mask = this_cpu;
221         unsigned long non_cpu_bits;
222
223         non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
224         mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
225
226         __asm__ __volatile__("1:\n\t"
227                              "ldx       [%2], %%g7\n\t"
228                              "and       %%g7, %1, %%g1\n\t"
229                              "or        %%g1, %0, %%g1\n\t"
230                              "casx      [%2], %%g7, %%g1\n\t"
231                              "cmp       %%g7, %%g1\n\t"
232                              "membar    #StoreLoad | #StoreStore\n\t"
233                              "bne,pn    %%xcc, 1b\n\t"
234                              " nop"
235                              : /* no outputs */
236                              : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
237                              : "g1", "g7");
238 }
239
240 static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
241 {
242         unsigned long mask = (1UL << PG_dcache_dirty);
243
244         __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
245                              "1:\n\t"
246                              "ldx       [%2], %%g7\n\t"
247                              "srlx      %%g7, %4, %%g1\n\t"
248                              "and       %%g1, %3, %%g1\n\t"
249                              "cmp       %%g1, %0\n\t"
250                              "bne,pn    %%icc, 2f\n\t"
251                              " andn     %%g7, %1, %%g1\n\t"
252                              "casx      [%2], %%g7, %%g1\n\t"
253                              "cmp       %%g7, %%g1\n\t"
254                              "membar    #StoreLoad | #StoreStore\n\t"
255                              "bne,pn    %%xcc, 1b\n\t"
256                              " nop\n"
257                              "2:"
258                              : /* no outputs */
259                              : "r" (cpu), "r" (mask), "r" (&page->flags),
260                                "i" (PG_dcache_cpu_mask),
261                                "i" (PG_dcache_cpu_shift)
262                              : "g1", "g7");
263 }
264
265 static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
266 {
267         unsigned long tsb_addr = (unsigned long) ent;
268
269         if (tlb_type == cheetah_plus || tlb_type == hypervisor)
270                 tsb_addr = __pa(tsb_addr);
271
272         __tsb_insert(tsb_addr, tag, pte);
273 }
274
275 unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
276 unsigned long _PAGE_SZBITS __read_mostly;
277
278 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
279 {
280         struct mm_struct *mm;
281         struct tsb *tsb;
282         unsigned long tag, flags;
283
284         if (tlb_type != hypervisor) {
285                 unsigned long pfn = pte_pfn(pte);
286                 unsigned long pg_flags;
287                 struct page *page;
288
289                 if (pfn_valid(pfn) &&
290                     (page = pfn_to_page(pfn), page_mapping(page)) &&
291                     ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
292                         int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
293                                    PG_dcache_cpu_mask);
294                         int this_cpu = get_cpu();
295
296                         /* This is just to optimize away some function calls
297                          * in the SMP case.
298                          */
299                         if (cpu == this_cpu)
300                                 flush_dcache_page_impl(page);
301                         else
302                                 smp_flush_dcache_page_impl(page, cpu);
303
304                         clear_dcache_dirty_cpu(page, cpu);
305
306                         put_cpu();
307                 }
308         }
309
310         mm = vma->vm_mm;
311
312         spin_lock_irqsave(&mm->context.lock, flags);
313
314         tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
315                                (mm->context.tsb_nentries - 1UL)];
316         tag = (address >> 22UL);
317         tsb_insert(tsb, tag, pte_val(pte));
318
319         spin_unlock_irqrestore(&mm->context.lock, flags);
320 }
321
322 void flush_dcache_page(struct page *page)
323 {
324         struct address_space *mapping;
325         int this_cpu;
326
327         if (tlb_type == hypervisor)
328                 return;
329
330         /* Do not bother with the expensive D-cache flush if it
331          * is merely the zero page.  The 'bigcore' testcase in GDB
332          * causes this case to run millions of times.
333          */
334         if (page == ZERO_PAGE(0))
335                 return;
336
337         this_cpu = get_cpu();
338
339         mapping = page_mapping(page);
340         if (mapping && !mapping_mapped(mapping)) {
341                 int dirty = test_bit(PG_dcache_dirty, &page->flags);
342                 if (dirty) {
343                         int dirty_cpu = dcache_dirty_cpu(page);
344
345                         if (dirty_cpu == this_cpu)
346                                 goto out;
347                         smp_flush_dcache_page_impl(page, dirty_cpu);
348                 }
349                 set_dcache_dirty(page, this_cpu);
350         } else {
351                 /* We could delay the flush for the !page_mapping
352                  * case too.  But that case is for exec env/arg
353                  * pages and those are %99 certainly going to get
354                  * faulted into the tlb (and thus flushed) anyways.
355                  */
356                 flush_dcache_page_impl(page);
357         }
358
359 out:
360         put_cpu();
361 }
362
363 void __kprobes flush_icache_range(unsigned long start, unsigned long end)
364 {
365         /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
366         if (tlb_type == spitfire) {
367                 unsigned long kaddr;
368
369                 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
370                         __flush_icache_page(__get_phys(kaddr));
371         }
372 }
373
374 void show_mem(void)
375 {
376         printk("Mem-info:\n");
377         show_free_areas();
378         printk("Free swap:       %6ldkB\n",
379                nr_swap_pages << (PAGE_SHIFT-10));
380         printk("%ld pages of RAM\n", num_physpages);
381         printk("%d free pages\n", nr_free_pages());
382 }
383
384 void mmu_info(struct seq_file *m)
385 {
386         if (tlb_type == cheetah)
387                 seq_printf(m, "MMU Type\t: Cheetah\n");
388         else if (tlb_type == cheetah_plus)
389                 seq_printf(m, "MMU Type\t: Cheetah+\n");
390         else if (tlb_type == spitfire)
391                 seq_printf(m, "MMU Type\t: Spitfire\n");
392         else if (tlb_type == hypervisor)
393                 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
394         else
395                 seq_printf(m, "MMU Type\t: ???\n");
396
397 #ifdef CONFIG_DEBUG_DCFLUSH
398         seq_printf(m, "DCPageFlushes\t: %d\n",
399                    atomic_read(&dcpage_flushes));
400 #ifdef CONFIG_SMP
401         seq_printf(m, "DCPageFlushesXC\t: %d\n",
402                    atomic_read(&dcpage_flushes_xcall));
403 #endif /* CONFIG_SMP */
404 #endif /* CONFIG_DEBUG_DCFLUSH */
405 }
406
407 struct linux_prom_translation {
408         unsigned long virt;
409         unsigned long size;
410         unsigned long data;
411 };
412
413 /* Exported for kernel TLB miss handling in ktlb.S */
414 struct linux_prom_translation prom_trans[512] __read_mostly;
415 unsigned int prom_trans_ents __read_mostly;
416
417 /* Exported for SMP bootup purposes. */
418 unsigned long kern_locked_tte_data;
419
420 /* The obp translations are saved based on 8k pagesize, since obp can
421  * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
422  * HI_OBP_ADDRESS range are handled in ktlb.S.
423  */
424 static inline int in_obp_range(unsigned long vaddr)
425 {
426         return (vaddr >= LOW_OBP_ADDRESS &&
427                 vaddr < HI_OBP_ADDRESS);
428 }
429
430 static int cmp_ptrans(const void *a, const void *b)
431 {
432         const struct linux_prom_translation *x = a, *y = b;
433
434         if (x->virt > y->virt)
435                 return 1;
436         if (x->virt < y->virt)
437                 return -1;
438         return 0;
439 }
440
441 /* Read OBP translations property into 'prom_trans[]'.  */
442 static void __init read_obp_translations(void)
443 {
444         int n, node, ents, first, last, i;
445
446         node = prom_finddevice("/virtual-memory");
447         n = prom_getproplen(node, "translations");
448         if (unlikely(n == 0 || n == -1)) {
449                 prom_printf("prom_mappings: Couldn't get size.\n");
450                 prom_halt();
451         }
452         if (unlikely(n > sizeof(prom_trans))) {
453                 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
454                 prom_halt();
455         }
456
457         if ((n = prom_getproperty(node, "translations",
458                                   (char *)&prom_trans[0],
459                                   sizeof(prom_trans))) == -1) {
460                 prom_printf("prom_mappings: Couldn't get property.\n");
461                 prom_halt();
462         }
463
464         n = n / sizeof(struct linux_prom_translation);
465
466         ents = n;
467
468         sort(prom_trans, ents, sizeof(struct linux_prom_translation),
469              cmp_ptrans, NULL);
470
471         /* Now kick out all the non-OBP entries.  */
472         for (i = 0; i < ents; i++) {
473                 if (in_obp_range(prom_trans[i].virt))
474                         break;
475         }
476         first = i;
477         for (; i < ents; i++) {
478                 if (!in_obp_range(prom_trans[i].virt))
479                         break;
480         }
481         last = i;
482
483         for (i = 0; i < (last - first); i++) {
484                 struct linux_prom_translation *src = &prom_trans[i + first];
485                 struct linux_prom_translation *dest = &prom_trans[i];
486
487                 *dest = *src;
488         }
489         for (; i < ents; i++) {
490                 struct linux_prom_translation *dest = &prom_trans[i];
491                 dest->virt = dest->size = dest->data = 0x0UL;
492         }
493
494         prom_trans_ents = last - first;
495
496         if (tlb_type == spitfire) {
497                 /* Clear diag TTE bits. */
498                 for (i = 0; i < prom_trans_ents; i++)
499                         prom_trans[i].data &= ~0x0003fe0000000000UL;
500         }
501 }
502
503 static void __init hypervisor_tlb_lock(unsigned long vaddr,
504                                        unsigned long pte,
505                                        unsigned long mmu)
506 {
507         register unsigned long func asm("%o5");
508         register unsigned long arg0 asm("%o0");
509         register unsigned long arg1 asm("%o1");
510         register unsigned long arg2 asm("%o2");
511         register unsigned long arg3 asm("%o3");
512
513         func = HV_FAST_MMU_MAP_PERM_ADDR;
514         arg0 = vaddr;
515         arg1 = 0;
516         arg2 = pte;
517         arg3 = mmu;
518         __asm__ __volatile__("ta        0x80"
519                              : "=&r" (func), "=&r" (arg0),
520                                "=&r" (arg1), "=&r" (arg2),
521                                "=&r" (arg3)
522                              : "0" (func), "1" (arg0), "2" (arg1),
523                                "3" (arg2), "4" (arg3));
524         if (arg0 != 0) {
525                 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
526                             "errors with %lx\n", vaddr, 0, pte, mmu, arg0);
527                 prom_halt();
528         }
529 }
530
531 static unsigned long kern_large_tte(unsigned long paddr);
532
533 static void __init remap_kernel(void)
534 {
535         unsigned long phys_page, tte_vaddr, tte_data;
536         int tlb_ent = sparc64_highest_locked_tlbent();
537
538         tte_vaddr = (unsigned long) KERNBASE;
539         phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
540         tte_data = kern_large_tte(phys_page);
541
542         kern_locked_tte_data = tte_data;
543
544         /* Now lock us into the TLBs via Hypervisor or OBP. */
545         if (tlb_type == hypervisor) {
546                 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
547                 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
548                 if (bigkernel) {
549                         tte_vaddr += 0x400000;
550                         tte_data += 0x400000;
551                         hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
552                         hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
553                 }
554         } else {
555                 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
556                 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
557                 if (bigkernel) {
558                         tlb_ent -= 1;
559                         prom_dtlb_load(tlb_ent,
560                                        tte_data + 0x400000, 
561                                        tte_vaddr + 0x400000);
562                         prom_itlb_load(tlb_ent,
563                                        tte_data + 0x400000, 
564                                        tte_vaddr + 0x400000);
565                 }
566                 sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
567         }
568         if (tlb_type == cheetah_plus) {
569                 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
570                                             CTX_CHEETAH_PLUS_NUC);
571                 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
572                 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
573         }
574 }
575
576
577 static void __init inherit_prom_mappings(void)
578 {
579         read_obp_translations();
580
581         /* Now fixup OBP's idea about where we really are mapped. */
582         prom_printf("Remapping the kernel... ");
583         remap_kernel();
584         prom_printf("done.\n");
585 }
586
587 void prom_world(int enter)
588 {
589         if (!enter)
590                 set_fs((mm_segment_t) { get_thread_current_ds() });
591
592         __asm__ __volatile__("flushw");
593 }
594
595 #ifdef DCACHE_ALIASING_POSSIBLE
596 void __flush_dcache_range(unsigned long start, unsigned long end)
597 {
598         unsigned long va;
599
600         if (tlb_type == spitfire) {
601                 int n = 0;
602
603                 for (va = start; va < end; va += 32) {
604                         spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
605                         if (++n >= 512)
606                                 break;
607                 }
608         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
609                 start = __pa(start);
610                 end = __pa(end);
611                 for (va = start; va < end; va += 32)
612                         __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
613                                              "membar #Sync"
614                                              : /* no outputs */
615                                              : "r" (va),
616                                                "i" (ASI_DCACHE_INVALIDATE));
617         }
618 }
619 #endif /* DCACHE_ALIASING_POSSIBLE */
620
621 /* Caller does TLB context flushing on local CPU if necessary.
622  * The caller also ensures that CTX_VALID(mm->context) is false.
623  *
624  * We must be careful about boundary cases so that we never
625  * let the user have CTX 0 (nucleus) or we ever use a CTX
626  * version of zero (and thus NO_CONTEXT would not be caught
627  * by version mis-match tests in mmu_context.h).
628  *
629  * Always invoked with interrupts disabled.
630  */
631 void get_new_mmu_context(struct mm_struct *mm)
632 {
633         unsigned long ctx, new_ctx;
634         unsigned long orig_pgsz_bits;
635         unsigned long flags;
636         int new_version;
637
638         spin_lock_irqsave(&ctx_alloc_lock, flags);
639         orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
640         ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
641         new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
642         new_version = 0;
643         if (new_ctx >= (1 << CTX_NR_BITS)) {
644                 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
645                 if (new_ctx >= ctx) {
646                         int i;
647                         new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
648                                 CTX_FIRST_VERSION;
649                         if (new_ctx == 1)
650                                 new_ctx = CTX_FIRST_VERSION;
651
652                         /* Don't call memset, for 16 entries that's just
653                          * plain silly...
654                          */
655                         mmu_context_bmap[0] = 3;
656                         mmu_context_bmap[1] = 0;
657                         mmu_context_bmap[2] = 0;
658                         mmu_context_bmap[3] = 0;
659                         for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
660                                 mmu_context_bmap[i + 0] = 0;
661                                 mmu_context_bmap[i + 1] = 0;
662                                 mmu_context_bmap[i + 2] = 0;
663                                 mmu_context_bmap[i + 3] = 0;
664                         }
665                         new_version = 1;
666                         goto out;
667                 }
668         }
669         mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
670         new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
671 out:
672         tlb_context_cache = new_ctx;
673         mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
674         spin_unlock_irqrestore(&ctx_alloc_lock, flags);
675
676         if (unlikely(new_version))
677                 smp_new_mmu_context_version();
678 }
679
680 void sparc_ultra_dump_itlb(void)
681 {
682         int slot;
683
684         if (tlb_type == spitfire) {
685                 printk ("Contents of itlb: ");
686                 for (slot = 0; slot < 14; slot++) printk ("    ");
687                 printk ("%2x:%016lx,%016lx\n",
688                         0,
689                         spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
690                 for (slot = 1; slot < 64; slot+=3) {
691                         printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n", 
692                                 slot,
693                                 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
694                                 slot+1,
695                                 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
696                                 slot+2,
697                                 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
698                 }
699         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
700                 printk ("Contents of itlb0:\n");
701                 for (slot = 0; slot < 16; slot+=2) {
702                         printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
703                                 slot,
704                                 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
705                                 slot+1,
706                                 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
707                 }
708                 printk ("Contents of itlb2:\n");
709                 for (slot = 0; slot < 128; slot+=2) {
710                         printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
711                                 slot,
712                                 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
713                                 slot+1,
714                                 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
715                 }
716         }
717 }
718
719 void sparc_ultra_dump_dtlb(void)
720 {
721         int slot;
722
723         if (tlb_type == spitfire) {
724                 printk ("Contents of dtlb: ");
725                 for (slot = 0; slot < 14; slot++) printk ("    ");
726                 printk ("%2x:%016lx,%016lx\n", 0,
727                         spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
728                 for (slot = 1; slot < 64; slot+=3) {
729                         printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n", 
730                                 slot,
731                                 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
732                                 slot+1,
733                                 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
734                                 slot+2,
735                                 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
736                 }
737         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
738                 printk ("Contents of dtlb0:\n");
739                 for (slot = 0; slot < 16; slot+=2) {
740                         printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
741                                 slot,
742                                 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
743                                 slot+1,
744                                 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
745                 }
746                 printk ("Contents of dtlb2:\n");
747                 for (slot = 0; slot < 512; slot+=2) {
748                         printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
749                                 slot,
750                                 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
751                                 slot+1,
752                                 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
753                 }
754                 if (tlb_type == cheetah_plus) {
755                         printk ("Contents of dtlb3:\n");
756                         for (slot = 0; slot < 512; slot+=2) {
757                                 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
758                                         slot,
759                                         cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
760                                         slot+1,
761                                         cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
762                         }
763                 }
764         }
765 }
766
767 extern unsigned long cmdline_memory_size;
768
769 /* Find a free area for the bootmem map, avoiding the kernel image
770  * and the initial ramdisk.
771  */
772 static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
773                                                unsigned long end_pfn)
774 {
775         unsigned long avoid_start, avoid_end, bootmap_size;
776         int i;
777
778         bootmap_size = ((end_pfn - start_pfn) + 7) / 8;
779         bootmap_size = ALIGN(bootmap_size, sizeof(long));
780
781         avoid_start = avoid_end = 0;
782 #ifdef CONFIG_BLK_DEV_INITRD
783         avoid_start = initrd_start;
784         avoid_end = PAGE_ALIGN(initrd_end);
785 #endif
786
787 #ifdef CONFIG_DEBUG_BOOTMEM
788         prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
789                     kern_base, PAGE_ALIGN(kern_base + kern_size),
790                     avoid_start, avoid_end);
791 #endif
792         for (i = 0; i < pavail_ents; i++) {
793                 unsigned long start, end;
794
795                 start = pavail[i].phys_addr;
796                 end = start + pavail[i].reg_size;
797
798                 while (start < end) {
799                         if (start >= kern_base &&
800                             start < PAGE_ALIGN(kern_base + kern_size)) {
801                                 start = PAGE_ALIGN(kern_base + kern_size);
802                                 continue;
803                         }
804                         if (start >= avoid_start && start < avoid_end) {
805                                 start = avoid_end;
806                                 continue;
807                         }
808
809                         if ((end - start) < bootmap_size)
810                                 break;
811
812                         if (start < kern_base &&
813                             (start + bootmap_size) > kern_base) {
814                                 start = PAGE_ALIGN(kern_base + kern_size);
815                                 continue;
816                         }
817
818                         if (start < avoid_start &&
819                             (start + bootmap_size) > avoid_start) {
820                                 start = avoid_end;
821                                 continue;
822                         }
823
824                         /* OK, it doesn't overlap anything, use it.  */
825 #ifdef CONFIG_DEBUG_BOOTMEM
826                         prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
827                                     start >> PAGE_SHIFT, start);
828 #endif
829                         return start >> PAGE_SHIFT;
830                 }
831         }
832
833         prom_printf("Cannot find free area for bootmap, aborting.\n");
834         prom_halt();
835 }
836
837 static unsigned long __init bootmem_init(unsigned long *pages_avail,
838                                          unsigned long phys_base)
839 {
840         unsigned long bootmap_size, end_pfn;
841         unsigned long end_of_phys_memory = 0UL;
842         unsigned long bootmap_pfn, bytes_avail, size;
843         int i;
844
845 #ifdef CONFIG_DEBUG_BOOTMEM
846         prom_printf("bootmem_init: Scan pavail, ");
847 #endif
848
849         bytes_avail = 0UL;
850         for (i = 0; i < pavail_ents; i++) {
851                 end_of_phys_memory = pavail[i].phys_addr +
852                         pavail[i].reg_size;
853                 bytes_avail += pavail[i].reg_size;
854                 if (cmdline_memory_size) {
855                         if (bytes_avail > cmdline_memory_size) {
856                                 unsigned long slack = bytes_avail - cmdline_memory_size;
857
858                                 bytes_avail -= slack;
859                                 end_of_phys_memory -= slack;
860
861                                 pavail[i].reg_size -= slack;
862                                 if ((long)pavail[i].reg_size <= 0L) {
863                                         pavail[i].phys_addr = 0xdeadbeefUL;
864                                         pavail[i].reg_size = 0UL;
865                                         pavail_ents = i;
866                                 } else {
867                                         pavail[i+1].reg_size = 0Ul;
868                                         pavail[i+1].phys_addr = 0xdeadbeefUL;
869                                         pavail_ents = i + 1;
870                                 }
871                                 break;
872                         }
873                 }
874         }
875
876         *pages_avail = bytes_avail >> PAGE_SHIFT;
877
878         end_pfn = end_of_phys_memory >> PAGE_SHIFT;
879
880 #ifdef CONFIG_BLK_DEV_INITRD
881         /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
882         if (sparc_ramdisk_image || sparc_ramdisk_image64) {
883                 unsigned long ramdisk_image = sparc_ramdisk_image ?
884                         sparc_ramdisk_image : sparc_ramdisk_image64;
885                 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
886                         ramdisk_image -= KERNBASE;
887                 initrd_start = ramdisk_image + phys_base;
888                 initrd_end = initrd_start + sparc_ramdisk_size;
889                 if (initrd_end > end_of_phys_memory) {
890                         printk(KERN_CRIT "initrd extends beyond end of memory "
891                                          "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
892                                initrd_end, end_of_phys_memory);
893                         initrd_start = 0;
894                         initrd_end = 0;
895                 }
896         }
897 #endif  
898         /* Initialize the boot-time allocator. */
899         max_pfn = max_low_pfn = end_pfn;
900         min_low_pfn = (phys_base >> PAGE_SHIFT);
901
902         bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
903
904 #ifdef CONFIG_DEBUG_BOOTMEM
905         prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
906                     min_low_pfn, bootmap_pfn, max_low_pfn);
907 #endif
908         bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
909                                          min_low_pfn, end_pfn);
910
911         /* Now register the available physical memory with the
912          * allocator.
913          */
914         for (i = 0; i < pavail_ents; i++) {
915 #ifdef CONFIG_DEBUG_BOOTMEM
916                 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
917                             i, pavail[i].phys_addr, pavail[i].reg_size);
918 #endif
919                 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
920         }
921
922 #ifdef CONFIG_BLK_DEV_INITRD
923         if (initrd_start) {
924                 size = initrd_end - initrd_start;
925
926                 /* Resert the initrd image area. */
927 #ifdef CONFIG_DEBUG_BOOTMEM
928                 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
929                         initrd_start, initrd_end);
930 #endif
931                 reserve_bootmem(initrd_start, size);
932                 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
933
934                 initrd_start += PAGE_OFFSET;
935                 initrd_end += PAGE_OFFSET;
936         }
937 #endif
938         /* Reserve the kernel text/data/bss. */
939 #ifdef CONFIG_DEBUG_BOOTMEM
940         prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
941 #endif
942         reserve_bootmem(kern_base, kern_size);
943         *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
944
945         /* Reserve the bootmem map.   We do not account for it
946          * in pages_avail because we will release that memory
947          * in free_all_bootmem.
948          */
949         size = bootmap_size;
950 #ifdef CONFIG_DEBUG_BOOTMEM
951         prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
952                     (bootmap_pfn << PAGE_SHIFT), size);
953 #endif
954         reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
955         *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
956
957         for (i = 0; i < pavail_ents; i++) {
958                 unsigned long start_pfn, end_pfn;
959
960                 start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
961                 end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
962 #ifdef CONFIG_DEBUG_BOOTMEM
963                 prom_printf("memory_present(0, %lx, %lx)\n",
964                             start_pfn, end_pfn);
965 #endif
966                 memory_present(0, start_pfn, end_pfn);
967         }
968
969         sparse_init();
970
971         return end_pfn;
972 }
973
974 static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
975 static int pall_ents __initdata;
976
977 #ifdef CONFIG_DEBUG_PAGEALLOC
978 static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
979 {
980         unsigned long vstart = PAGE_OFFSET + pstart;
981         unsigned long vend = PAGE_OFFSET + pend;
982         unsigned long alloc_bytes = 0UL;
983
984         if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
985                 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
986                             vstart, vend);
987                 prom_halt();
988         }
989
990         while (vstart < vend) {
991                 unsigned long this_end, paddr = __pa(vstart);
992                 pgd_t *pgd = pgd_offset_k(vstart);
993                 pud_t *pud;
994                 pmd_t *pmd;
995                 pte_t *pte;
996
997                 pud = pud_offset(pgd, vstart);
998                 if (pud_none(*pud)) {
999                         pmd_t *new;
1000
1001                         new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1002                         alloc_bytes += PAGE_SIZE;
1003                         pud_populate(&init_mm, pud, new);
1004                 }
1005
1006                 pmd = pmd_offset(pud, vstart);
1007                 if (!pmd_present(*pmd)) {
1008                         pte_t *new;
1009
1010                         new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1011                         alloc_bytes += PAGE_SIZE;
1012                         pmd_populate_kernel(&init_mm, pmd, new);
1013                 }
1014
1015                 pte = pte_offset_kernel(pmd, vstart);
1016                 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1017                 if (this_end > vend)
1018                         this_end = vend;
1019
1020                 while (vstart < this_end) {
1021                         pte_val(*pte) = (paddr | pgprot_val(prot));
1022
1023                         vstart += PAGE_SIZE;
1024                         paddr += PAGE_SIZE;
1025                         pte++;
1026                 }
1027         }
1028
1029         return alloc_bytes;
1030 }
1031
1032 extern unsigned int kvmap_linear_patch[1];
1033 #endif /* CONFIG_DEBUG_PAGEALLOC */
1034
1035 static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
1036 {
1037         const unsigned long shift_256MB = 28;
1038         const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
1039         const unsigned long size_256MB = (1UL << shift_256MB);
1040
1041         while (start < end) {
1042                 long remains;
1043
1044                 remains = end - start;
1045                 if (remains < size_256MB)
1046                         break;
1047
1048                 if (start & mask_256MB) {
1049                         start = (start + size_256MB) & ~mask_256MB;
1050                         continue;
1051                 }
1052
1053                 while (remains >= size_256MB) {
1054                         unsigned long index = start >> shift_256MB;
1055
1056                         __set_bit(index, kpte_linear_bitmap);
1057
1058                         start += size_256MB;
1059                         remains -= size_256MB;
1060                 }
1061         }
1062 }
1063
1064 static void __init kernel_physical_mapping_init(void)
1065 {
1066         unsigned long i;
1067 #ifdef CONFIG_DEBUG_PAGEALLOC
1068         unsigned long mem_alloced = 0UL;
1069 #endif
1070
1071         read_obp_memory("reg", &pall[0], &pall_ents);
1072
1073         for (i = 0; i < pall_ents; i++) {
1074                 unsigned long phys_start, phys_end;
1075
1076                 phys_start = pall[i].phys_addr;
1077                 phys_end = phys_start + pall[i].reg_size;
1078
1079                 mark_kpte_bitmap(phys_start, phys_end);
1080
1081 #ifdef CONFIG_DEBUG_PAGEALLOC
1082                 mem_alloced += kernel_map_range(phys_start, phys_end,
1083                                                 PAGE_KERNEL);
1084 #endif
1085         }
1086
1087 #ifdef CONFIG_DEBUG_PAGEALLOC
1088         printk("Allocated %ld bytes for kernel page tables.\n",
1089                mem_alloced);
1090
1091         kvmap_linear_patch[0] = 0x01000000; /* nop */
1092         flushi(&kvmap_linear_patch[0]);
1093
1094         __flush_tlb_all();
1095 #endif
1096 }
1097
1098 #ifdef CONFIG_DEBUG_PAGEALLOC
1099 void kernel_map_pages(struct page *page, int numpages, int enable)
1100 {
1101         unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1102         unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1103
1104         kernel_map_range(phys_start, phys_end,
1105                          (enable ? PAGE_KERNEL : __pgprot(0)));
1106
1107         flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1108                                PAGE_OFFSET + phys_end);
1109
1110         /* we should perform an IPI and flush all tlbs,
1111          * but that can deadlock->flush only current cpu.
1112          */
1113         __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1114                                  PAGE_OFFSET + phys_end);
1115 }
1116 #endif
1117
1118 unsigned long __init find_ecache_flush_span(unsigned long size)
1119 {
1120         int i;
1121
1122         for (i = 0; i < pavail_ents; i++) {
1123                 if (pavail[i].reg_size >= size)
1124                         return pavail[i].phys_addr;
1125         }
1126
1127         return ~0UL;
1128 }
1129
1130 static void __init tsb_phys_patch(void)
1131 {
1132         struct tsb_ldquad_phys_patch_entry *pquad;
1133         struct tsb_phys_patch_entry *p;
1134
1135         pquad = &__tsb_ldquad_phys_patch;
1136         while (pquad < &__tsb_ldquad_phys_patch_end) {
1137                 unsigned long addr = pquad->addr;
1138
1139                 if (tlb_type == hypervisor)
1140                         *(unsigned int *) addr = pquad->sun4v_insn;
1141                 else
1142                         *(unsigned int *) addr = pquad->sun4u_insn;
1143                 wmb();
1144                 __asm__ __volatile__("flush     %0"
1145                                      : /* no outputs */
1146                                      : "r" (addr));
1147
1148                 pquad++;
1149         }
1150
1151         p = &__tsb_phys_patch;
1152         while (p < &__tsb_phys_patch_end) {
1153                 unsigned long addr = p->addr;
1154
1155                 *(unsigned int *) addr = p->insn;
1156                 wmb();
1157                 __asm__ __volatile__("flush     %0"
1158                                      : /* no outputs */
1159                                      : "r" (addr));
1160
1161                 p++;
1162         }
1163 }
1164
1165 /* Don't mark as init, we give this to the Hypervisor.  */
1166 static struct hv_tsb_descr ktsb_descr[2];
1167 extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1168
1169 static void __init sun4v_ktsb_init(void)
1170 {
1171         unsigned long ktsb_pa;
1172
1173         /* First KTSB for PAGE_SIZE mappings.  */
1174         ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1175
1176         switch (PAGE_SIZE) {
1177         case 8 * 1024:
1178         default:
1179                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1180                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1181                 break;
1182
1183         case 64 * 1024:
1184                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1185                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1186                 break;
1187
1188         case 512 * 1024:
1189                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1190                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1191                 break;
1192
1193         case 4 * 1024 * 1024:
1194                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1195                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1196                 break;
1197         };
1198
1199         ktsb_descr[0].assoc = 1;
1200         ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1201         ktsb_descr[0].ctx_idx = 0;
1202         ktsb_descr[0].tsb_base = ktsb_pa;
1203         ktsb_descr[0].resv = 0;
1204
1205         /* Second KTSB for 4MB/256MB mappings.  */
1206         ktsb_pa = (kern_base +
1207                    ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1208
1209         ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1210         ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1211                                    HV_PGSZ_MASK_256MB);
1212         ktsb_descr[1].assoc = 1;
1213         ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1214         ktsb_descr[1].ctx_idx = 0;
1215         ktsb_descr[1].tsb_base = ktsb_pa;
1216         ktsb_descr[1].resv = 0;
1217 }
1218
1219 void __cpuinit sun4v_ktsb_register(void)
1220 {
1221         register unsigned long func asm("%o5");
1222         register unsigned long arg0 asm("%o0");
1223         register unsigned long arg1 asm("%o1");
1224         unsigned long pa;
1225
1226         pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1227
1228         func = HV_FAST_MMU_TSB_CTX0;
1229         arg0 = 2;
1230         arg1 = pa;
1231         __asm__ __volatile__("ta        %6"
1232                              : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
1233                              : "0" (func), "1" (arg0), "2" (arg1),
1234                                "i" (HV_FAST_TRAP));
1235 }
1236
1237 /* paging_init() sets up the page tables */
1238
1239 extern void cheetah_ecache_flush_init(void);
1240 extern void sun4v_patch_tlb_handlers(void);
1241
1242 static unsigned long last_valid_pfn;
1243 pgd_t swapper_pg_dir[2048];
1244
1245 static void sun4u_pgprot_init(void);
1246 static void sun4v_pgprot_init(void);
1247
1248 void __init paging_init(void)
1249 {
1250         unsigned long end_pfn, pages_avail, shift, phys_base;
1251         unsigned long real_end, i;
1252
1253         kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1254         kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1255
1256         /* Invalidate both kernel TSBs.  */
1257         memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
1258         memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
1259
1260         if (tlb_type == hypervisor)
1261                 sun4v_pgprot_init();
1262         else
1263                 sun4u_pgprot_init();
1264
1265         if (tlb_type == cheetah_plus ||
1266             tlb_type == hypervisor)
1267                 tsb_phys_patch();
1268
1269         if (tlb_type == hypervisor) {
1270                 sun4v_patch_tlb_handlers();
1271                 sun4v_ktsb_init();
1272         }
1273
1274         /* Find available physical memory... */
1275         read_obp_memory("available", &pavail[0], &pavail_ents);
1276
1277         phys_base = 0xffffffffffffffffUL;
1278         for (i = 0; i < pavail_ents; i++)
1279                 phys_base = min(phys_base, pavail[i].phys_addr);
1280
1281         set_bit(0, mmu_context_bmap);
1282
1283         shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1284
1285         real_end = (unsigned long)_end;
1286         if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1287                 bigkernel = 1;
1288         if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1289                 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1290                 prom_halt();
1291         }
1292
1293         /* Set kernel pgd to upper alias so physical page computations
1294          * work.
1295          */
1296         init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1297         
1298         memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
1299
1300         /* Now can init the kernel/bad page tables. */
1301         pud_set(pud_offset(&swapper_pg_dir[0], 0),
1302                 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
1303         
1304         inherit_prom_mappings();
1305         
1306         /* Ok, we can use our TLB miss and window trap handlers safely.  */
1307         setup_tba();
1308
1309         __flush_tlb_all();
1310
1311         if (tlb_type == hypervisor)
1312                 sun4v_ktsb_register();
1313
1314         /* Setup bootmem... */
1315         pages_avail = 0;
1316         last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
1317
1318         max_mapnr = last_valid_pfn;
1319
1320         kernel_physical_mapping_init();
1321
1322         {
1323                 unsigned long zones_size[MAX_NR_ZONES];
1324                 unsigned long zholes_size[MAX_NR_ZONES];
1325                 int znum;
1326
1327                 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1328                         zones_size[znum] = zholes_size[znum] = 0;
1329
1330                 zones_size[ZONE_DMA] = end_pfn;
1331                 zholes_size[ZONE_DMA] = end_pfn - pages_avail;
1332
1333                 free_area_init_node(0, &contig_page_data, zones_size,
1334                                     __pa(PAGE_OFFSET) >> PAGE_SHIFT,
1335                                     zholes_size);
1336         }
1337
1338         device_scan();
1339 }
1340
1341 static void __init taint_real_pages(void)
1342 {
1343         int i;
1344
1345         read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
1346
1347         /* Find changes discovered in the physmem available rescan and
1348          * reserve the lost portions in the bootmem maps.
1349          */
1350         for (i = 0; i < pavail_ents; i++) {
1351                 unsigned long old_start, old_end;
1352
1353                 old_start = pavail[i].phys_addr;
1354                 old_end = old_start +
1355                         pavail[i].reg_size;
1356                 while (old_start < old_end) {
1357                         int n;
1358
1359                         for (n = 0; pavail_rescan_ents; n++) {
1360                                 unsigned long new_start, new_end;
1361
1362                                 new_start = pavail_rescan[n].phys_addr;
1363                                 new_end = new_start +
1364                                         pavail_rescan[n].reg_size;
1365
1366                                 if (new_start <= old_start &&
1367                                     new_end >= (old_start + PAGE_SIZE)) {
1368                                         set_bit(old_start >> 22,
1369                                                 sparc64_valid_addr_bitmap);
1370                                         goto do_next_page;
1371                                 }
1372                         }
1373                         reserve_bootmem(old_start, PAGE_SIZE);
1374
1375                 do_next_page:
1376                         old_start += PAGE_SIZE;
1377                 }
1378         }
1379 }
1380
1381 void __init mem_init(void)
1382 {
1383         unsigned long codepages, datapages, initpages;
1384         unsigned long addr, last;
1385         int i;
1386
1387         i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1388         i += 1;
1389         sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
1390         if (sparc64_valid_addr_bitmap == NULL) {
1391                 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1392                 prom_halt();
1393         }
1394         memset(sparc64_valid_addr_bitmap, 0, i << 3);
1395
1396         addr = PAGE_OFFSET + kern_base;
1397         last = PAGE_ALIGN(kern_size) + addr;
1398         while (addr < last) {
1399                 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1400                 addr += PAGE_SIZE;
1401         }
1402
1403         taint_real_pages();
1404
1405         high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1406
1407 #ifdef CONFIG_DEBUG_BOOTMEM
1408         prom_printf("mem_init: Calling free_all_bootmem().\n");
1409 #endif
1410         totalram_pages = num_physpages = free_all_bootmem() - 1;
1411
1412         /*
1413          * Set up the zero page, mark it reserved, so that page count
1414          * is not manipulated when freeing the page from user ptes.
1415          */
1416         mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1417         if (mem_map_zero == NULL) {
1418                 prom_printf("paging_init: Cannot alloc zero page.\n");
1419                 prom_halt();
1420         }
1421         SetPageReserved(mem_map_zero);
1422
1423         codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1424         codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1425         datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1426         datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1427         initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1428         initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1429
1430         printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1431                nr_free_pages() << (PAGE_SHIFT-10),
1432                codepages << (PAGE_SHIFT-10),
1433                datapages << (PAGE_SHIFT-10), 
1434                initpages << (PAGE_SHIFT-10), 
1435                PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1436
1437         if (tlb_type == cheetah || tlb_type == cheetah_plus)
1438                 cheetah_ecache_flush_init();
1439 }
1440
1441 void free_initmem(void)
1442 {
1443         unsigned long addr, initend;
1444
1445         /*
1446          * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1447          */
1448         addr = PAGE_ALIGN((unsigned long)(__init_begin));
1449         initend = (unsigned long)(__init_end) & PAGE_MASK;
1450         for (; addr < initend; addr += PAGE_SIZE) {
1451                 unsigned long page;
1452                 struct page *p;
1453
1454                 page = (addr +
1455                         ((unsigned long) __va(kern_base)) -
1456                         ((unsigned long) KERNBASE));
1457                 memset((void *)addr, 0xcc, PAGE_SIZE);
1458                 p = virt_to_page(page);
1459
1460                 ClearPageReserved(p);
1461                 set_page_count(p, 1);
1462                 __free_page(p);
1463                 num_physpages++;
1464                 totalram_pages++;
1465         }
1466 }
1467
1468 #ifdef CONFIG_BLK_DEV_INITRD
1469 void free_initrd_mem(unsigned long start, unsigned long end)
1470 {
1471         if (start < end)
1472                 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1473         for (; start < end; start += PAGE_SIZE) {
1474                 struct page *p = virt_to_page(start);
1475
1476                 ClearPageReserved(p);
1477                 set_page_count(p, 1);
1478                 __free_page(p);
1479                 num_physpages++;
1480                 totalram_pages++;
1481         }
1482 }
1483 #endif
1484
1485 #define _PAGE_CACHE_4U  (_PAGE_CP_4U | _PAGE_CV_4U)
1486 #define _PAGE_CACHE_4V  (_PAGE_CP_4V | _PAGE_CV_4V)
1487 #define __DIRTY_BITS_4U  (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1488 #define __DIRTY_BITS_4V  (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1489 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1490 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1491
1492 pgprot_t PAGE_KERNEL __read_mostly;
1493 EXPORT_SYMBOL(PAGE_KERNEL);
1494
1495 pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
1496 pgprot_t PAGE_COPY __read_mostly;
1497
1498 pgprot_t PAGE_SHARED __read_mostly;
1499 EXPORT_SYMBOL(PAGE_SHARED);
1500
1501 pgprot_t PAGE_EXEC __read_mostly;
1502 unsigned long pg_iobits __read_mostly;
1503
1504 unsigned long _PAGE_IE __read_mostly;
1505
1506 unsigned long _PAGE_E __read_mostly;
1507 EXPORT_SYMBOL(_PAGE_E);
1508
1509 unsigned long _PAGE_CACHE __read_mostly;
1510 EXPORT_SYMBOL(_PAGE_CACHE);
1511
1512 static void prot_init_common(unsigned long page_none,
1513                              unsigned long page_shared,
1514                              unsigned long page_copy,
1515                              unsigned long page_readonly,
1516                              unsigned long page_exec_bit)
1517 {
1518         PAGE_COPY = __pgprot(page_copy);
1519         PAGE_SHARED = __pgprot(page_shared);
1520
1521         protection_map[0x0] = __pgprot(page_none);
1522         protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
1523         protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
1524         protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
1525         protection_map[0x4] = __pgprot(page_readonly);
1526         protection_map[0x5] = __pgprot(page_readonly);
1527         protection_map[0x6] = __pgprot(page_copy);
1528         protection_map[0x7] = __pgprot(page_copy);
1529         protection_map[0x8] = __pgprot(page_none);
1530         protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
1531         protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
1532         protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
1533         protection_map[0xc] = __pgprot(page_readonly);
1534         protection_map[0xd] = __pgprot(page_readonly);
1535         protection_map[0xe] = __pgprot(page_shared);
1536         protection_map[0xf] = __pgprot(page_shared);
1537 }
1538
1539 static void __init sun4u_pgprot_init(void)
1540 {
1541         unsigned long page_none, page_shared, page_copy, page_readonly;
1542         unsigned long page_exec_bit;
1543
1544         PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1545                                 _PAGE_CACHE_4U | _PAGE_P_4U |
1546                                 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1547                                 _PAGE_EXEC_4U);
1548         PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1549                                        _PAGE_CACHE_4U | _PAGE_P_4U |
1550                                        __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1551                                        _PAGE_EXEC_4U | _PAGE_L_4U);
1552         PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
1553
1554         _PAGE_IE = _PAGE_IE_4U;
1555         _PAGE_E = _PAGE_E_4U;
1556         _PAGE_CACHE = _PAGE_CACHE_4U;
1557
1558         pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
1559                      __ACCESS_BITS_4U | _PAGE_E_4U);
1560
1561         kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
1562                 0xfffff80000000000;
1563         kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
1564                                    _PAGE_P_4U | _PAGE_W_4U);
1565
1566         /* XXX Should use 256MB on Panther. XXX */
1567         kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
1568
1569         _PAGE_SZBITS = _PAGE_SZBITS_4U;
1570         _PAGE_ALL_SZ_BITS =  (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
1571                               _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
1572                               _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
1573
1574
1575         page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
1576         page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1577                        __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
1578         page_copy   = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1579                        __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1580         page_readonly   = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1581                            __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1582
1583         page_exec_bit = _PAGE_EXEC_4U;
1584
1585         prot_init_common(page_none, page_shared, page_copy, page_readonly,
1586                          page_exec_bit);
1587 }
1588
1589 static void __init sun4v_pgprot_init(void)
1590 {
1591         unsigned long page_none, page_shared, page_copy, page_readonly;
1592         unsigned long page_exec_bit;
1593
1594         PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
1595                                 _PAGE_CACHE_4V | _PAGE_P_4V |
1596                                 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
1597                                 _PAGE_EXEC_4V);
1598         PAGE_KERNEL_LOCKED = PAGE_KERNEL;
1599         PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
1600
1601         _PAGE_IE = _PAGE_IE_4V;
1602         _PAGE_E = _PAGE_E_4V;
1603         _PAGE_CACHE = _PAGE_CACHE_4V;
1604
1605         kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
1606                 0xfffff80000000000;
1607         kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1608                                    _PAGE_P_4V | _PAGE_W_4V);
1609
1610         kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
1611                 0xfffff80000000000;
1612         kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1613                                    _PAGE_P_4V | _PAGE_W_4V);
1614
1615         pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
1616                      __ACCESS_BITS_4V | _PAGE_E_4V);
1617
1618         _PAGE_SZBITS = _PAGE_SZBITS_4V;
1619         _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
1620                              _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
1621                              _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
1622                              _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
1623
1624         page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
1625         page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1626                        __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
1627         page_copy   = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1628                        __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1629         page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1630                          __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1631
1632         page_exec_bit = _PAGE_EXEC_4V;
1633
1634         prot_init_common(page_none, page_shared, page_copy, page_readonly,
1635                          page_exec_bit);
1636 }
1637
1638 unsigned long pte_sz_bits(unsigned long sz)
1639 {
1640         if (tlb_type == hypervisor) {
1641                 switch (sz) {
1642                 case 8 * 1024:
1643                 default:
1644                         return _PAGE_SZ8K_4V;
1645                 case 64 * 1024:
1646                         return _PAGE_SZ64K_4V;
1647                 case 512 * 1024:
1648                         return _PAGE_SZ512K_4V;
1649                 case 4 * 1024 * 1024:
1650                         return _PAGE_SZ4MB_4V;
1651                 };
1652         } else {
1653                 switch (sz) {
1654                 case 8 * 1024:
1655                 default:
1656                         return _PAGE_SZ8K_4U;
1657                 case 64 * 1024:
1658                         return _PAGE_SZ64K_4U;
1659                 case 512 * 1024:
1660                         return _PAGE_SZ512K_4U;
1661                 case 4 * 1024 * 1024:
1662                         return _PAGE_SZ4MB_4U;
1663                 };
1664         }
1665 }
1666
1667 pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
1668 {
1669         pte_t pte;
1670
1671         pte_val(pte)  = page | pgprot_val(pgprot_noncached(prot));
1672         pte_val(pte) |= (((unsigned long)space) << 32);
1673         pte_val(pte) |= pte_sz_bits(page_size);
1674
1675         return pte;
1676 }
1677
1678 static unsigned long kern_large_tte(unsigned long paddr)
1679 {
1680         unsigned long val;
1681
1682         val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1683                _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
1684                _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
1685         if (tlb_type == hypervisor)
1686                 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1687                        _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
1688                        _PAGE_EXEC_4V | _PAGE_W_4V);
1689
1690         return val | paddr;
1691 }
1692
1693 /*
1694  * Translate PROM's mapping we capture at boot time into physical address.
1695  * The second parameter is only set from prom_callback() invocations.
1696  */
1697 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
1698 {
1699         unsigned long mask;
1700         int i;
1701
1702         mask = _PAGE_PADDR_4U;
1703         if (tlb_type == hypervisor)
1704                 mask = _PAGE_PADDR_4V;
1705
1706         for (i = 0; i < prom_trans_ents; i++) {
1707                 struct linux_prom_translation *p = &prom_trans[i];
1708
1709                 if (promva >= p->virt &&
1710                     promva < (p->virt + p->size)) {
1711                         unsigned long base = p->data & mask;
1712
1713                         if (error)
1714                                 *error = 0;
1715                         return base + (promva & (8192 - 1));
1716                 }
1717         }
1718         if (error)
1719                 *error = 1;
1720         return 0UL;
1721 }
1722
1723 /* XXX We should kill off this ugly thing at so me point. XXX */
1724 unsigned long sun4u_get_pte(unsigned long addr)
1725 {
1726         pgd_t *pgdp;
1727         pud_t *pudp;
1728         pmd_t *pmdp;
1729         pte_t *ptep;
1730         unsigned long mask = _PAGE_PADDR_4U;
1731
1732         if (tlb_type == hypervisor)
1733                 mask = _PAGE_PADDR_4V;
1734
1735         if (addr >= PAGE_OFFSET)
1736                 return addr & mask;
1737
1738         if ((addr >= LOW_OBP_ADDRESS) && (addr < HI_OBP_ADDRESS))
1739                 return prom_virt_to_phys(addr, NULL);
1740
1741         pgdp = pgd_offset_k(addr);
1742         pudp = pud_offset(pgdp, addr);
1743         pmdp = pmd_offset(pudp, addr);
1744         ptep = pte_offset_kernel(pmdp, addr);
1745
1746         return pte_val(*ptep) & mask;
1747 }
1748
1749 /* If not locked, zap it. */
1750 void __flush_tlb_all(void)
1751 {
1752         unsigned long pstate;
1753         int i;
1754
1755         __asm__ __volatile__("flushw\n\t"
1756                              "rdpr      %%pstate, %0\n\t"
1757                              "wrpr      %0, %1, %%pstate"
1758                              : "=r" (pstate)
1759                              : "i" (PSTATE_IE));
1760         if (tlb_type == spitfire) {
1761                 for (i = 0; i < 64; i++) {
1762                         /* Spitfire Errata #32 workaround */
1763                         /* NOTE: Always runs on spitfire, so no
1764                          *       cheetah+ page size encodings.
1765                          */
1766                         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
1767                                              "flush     %%g6"
1768                                              : /* No outputs */
1769                                              : "r" (0),
1770                                              "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1771
1772                         if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
1773                                 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1774                                                      "membar #Sync"
1775                                                      : /* no outputs */
1776                                                      : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1777                                 spitfire_put_dtlb_data(i, 0x0UL);
1778                         }
1779
1780                         /* Spitfire Errata #32 workaround */
1781                         /* NOTE: Always runs on spitfire, so no
1782                          *       cheetah+ page size encodings.
1783                          */
1784                         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
1785                                              "flush     %%g6"
1786                                              : /* No outputs */
1787                                              : "r" (0),
1788                                              "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1789
1790                         if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
1791                                 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1792                                                      "membar #Sync"
1793                                                      : /* no outputs */
1794                                                      : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1795                                 spitfire_put_itlb_data(i, 0x0UL);
1796                         }
1797                 }
1798         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1799                 cheetah_flush_dtlb_all();
1800                 cheetah_flush_itlb_all();
1801         }
1802         __asm__ __volatile__("wrpr      %0, 0, %%pstate"
1803                              : : "r" (pstate));
1804 }