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