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