]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - arch/sparc64/mm/init.c
[S390] Fix yet another two section mismatches.
[linux-2.6.git] / arch / sparc64 / mm / init.c
index 88eb6f6be5620b2ccefc01816317c016ab774e3e..3010227fe24313ccddc630068c9503aa77b7c409 100644 (file)
@@ -5,7 +5,7 @@
  *  Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  */
  
-#include <linux/config.h>
+#include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/string.h>
 #include <linux/initrd.h>
 #include <linux/swap.h>
 #include <linux/pagemap.h>
+#include <linux/poison.h>
 #include <linux/fs.h>
 #include <linux/seq_file.h>
 #include <linux/kprobes.h>
 #include <linux/cache.h>
 #include <linux/sort.h>
+#include <linux/percpu.h>
 
 #include <asm/head.h>
 #include <asm/system.h>
 #include <asm/sections.h>
 #include <asm/tsb.h>
 #include <asm/hypervisor.h>
+#include <asm/prom.h>
+#include <asm/sstate.h>
+#include <asm/mdesc.h>
 
-extern void device_scan(void);
+#define MAX_PHYS_ADDRESS       (1UL << 42UL)
+#define KPTE_BITMAP_CHUNK_SZ   (256UL * 1024UL * 1024UL)
+#define KPTE_BITMAP_BYTES      \
+       ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
+
+unsigned long kern_linear_pte_xor[2] __read_mostly;
+
+/* A bitmap, one bit for every 256MB of physical memory.  If the bit
+ * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
+ * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
+ */
+unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
+
+#ifndef CONFIG_DEBUG_PAGEALLOC
+/* A special kernel TSB for 4MB and 256MB linear mappings.
+ * Space is allocated for this right after the trap table
+ * in arch/sparc64/kernel/head.S
+ */
+extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
+#endif
 
 #define MAX_BANKS      32
 
@@ -84,8 +108,6 @@ static void __init read_obp_memory(const char *property,
                prom_halt();
        }
 
-       *num_ents = ents;
-
        /* Sanitize what we got from the firmware, by page aligning
         * everything.
         */
@@ -104,29 +126,32 @@ static void __init read_obp_memory(const char *property,
                                size = 0UL;
                        base = new_base;
                }
+               if (size == 0UL) {
+                       /* If it is empty, simply get rid of it.
+                        * This simplifies the logic of the other
+                        * functions that process these arrays.
+                        */
+                       memmove(&regs[i], &regs[i + 1],
+                               (ents - i - 1) * sizeof(regs[0]));
+                       i--;
+                       ents--;
+                       continue;
+               }
                regs[i].phys_addr = base;
                regs[i].reg_size = size;
        }
+
+       *num_ents = ents;
+
        sort(regs, ents, sizeof(struct linux_prom64_registers),
             cmp_p64, NULL);
 }
 
 unsigned long *sparc64_valid_addr_bitmap __read_mostly;
 
-/* Ugly, but necessary... -DaveM */
-unsigned long phys_base __read_mostly;
+/* Kernel physical address base and size in bytes.  */
 unsigned long kern_base __read_mostly;
 unsigned long kern_size __read_mostly;
-unsigned long pfn_base __read_mostly;
-
-/* get_new_mmu_context() uses "cache + 1".  */
-DEFINE_SPINLOCK(ctx_alloc_lock);
-unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
-#define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
-unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
-
-/* References to special section boundaries */
-extern char  _start[], _end[];
 
 /* Initial ramdisk setup */
 extern unsigned long sparc_ramdisk_image64;
@@ -143,27 +168,6 @@ unsigned long sparc64_kern_sec_context __read_mostly;
 
 int bigkernel = 0;
 
-kmem_cache_t *pgtable_cache __read_mostly;
-
-static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
-{
-       clear_page(addr);
-}
-
-void pgtable_cache_init(void)
-{
-       pgtable_cache = kmem_cache_create("pgtable_cache",
-                                         PAGE_SIZE, PAGE_SIZE,
-                                         SLAB_HWCACHE_ALIGN |
-                                         SLAB_MUST_HWCACHE_ALIGN,
-                                         zero_ctor,
-                                         NULL);
-       if (!pgtable_cache) {
-               prom_printf("pgtable_cache_init(): Could not create!\n");
-               prom_halt();
-       }
-}
-
 #ifdef CONFIG_DEBUG_DCFLUSH
 atomic_t dcpage_flushes = ATOMIC_INIT(0);
 #ifdef CONFIG_SMP
@@ -171,8 +175,9 @@ atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
 #endif
 #endif
 
-__inline__ void flush_dcache_page_impl(struct page *page)
+inline void flush_dcache_page_impl(struct page *page)
 {
+       BUG_ON(tlb_type == hypervisor);
 #ifdef CONFIG_DEBUG_DCFLUSH
        atomic_inc(&dcpage_flushes);
 #endif
@@ -189,12 +194,9 @@ __inline__ void flush_dcache_page_impl(struct page *page)
 }
 
 #define PG_dcache_dirty                PG_arch_1
-#define PG_dcache_cpu_shift    24
-#define PG_dcache_cpu_mask     (256 - 1)
-
-#if NR_CPUS > 256
-#error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
-#endif
+#define PG_dcache_cpu_shift    32UL
+#define PG_dcache_cpu_mask     \
+       ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
 
 #define dcache_dirty_cpu(page) \
        (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
@@ -250,50 +252,74 @@ static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long
 {
        unsigned long tsb_addr = (unsigned long) ent;
 
-       if (tlb_type == cheetah_plus)
+       if (tlb_type == cheetah_plus || tlb_type == hypervisor)
                tsb_addr = __pa(tsb_addr);
 
        __tsb_insert(tsb_addr, tag, pte);
 }
 
+unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
+unsigned long _PAGE_SZBITS __read_mostly;
+
 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
 {
        struct mm_struct *mm;
-       struct page *page;
-       unsigned long pfn;
-       unsigned long pg_flags;
-
-       pfn = pte_pfn(pte);
-       if (pfn_valid(pfn) &&
-           (page = pfn_to_page(pfn), page_mapping(page)) &&
-           ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
-               int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
-                          PG_dcache_cpu_mask);
-               int this_cpu = get_cpu();
-
-               /* This is just to optimize away some function calls
-                * in the SMP case.
-                */
-               if (cpu == this_cpu)
-                       flush_dcache_page_impl(page);
-               else
-                       smp_flush_dcache_page_impl(page, cpu);
+       struct tsb *tsb;
+       unsigned long tag, flags;
+       unsigned long tsb_index, tsb_hash_shift;
+
+       if (tlb_type != hypervisor) {
+               unsigned long pfn = pte_pfn(pte);
+               unsigned long pg_flags;
+               struct page *page;
+
+               if (pfn_valid(pfn) &&
+                   (page = pfn_to_page(pfn), page_mapping(page)) &&
+                   ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
+                       int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
+                                  PG_dcache_cpu_mask);
+                       int this_cpu = get_cpu();
+
+                       /* This is just to optimize away some function calls
+                        * in the SMP case.
+                        */
+                       if (cpu == this_cpu)
+                               flush_dcache_page_impl(page);
+                       else
+                               smp_flush_dcache_page_impl(page, cpu);
 
-               clear_dcache_dirty_cpu(page, cpu);
+                       clear_dcache_dirty_cpu(page, cpu);
 
-               put_cpu();
+                       put_cpu();
+               }
        }
 
        mm = vma->vm_mm;
-       if ((pte_val(pte) & _PAGE_ALL_SZ_BITS) == _PAGE_SZBITS) {
-               struct tsb *tsb;
-               unsigned long tag;
-
-               tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
-                                      (mm->context.tsb_nentries - 1UL)];
-               tag = (address >> 22UL) | CTX_HWBITS(mm->context) << 48UL;
-               tsb_insert(tsb, tag, pte_val(pte));
+
+       tsb_index = MM_TSB_BASE;
+       tsb_hash_shift = PAGE_SHIFT;
+
+       spin_lock_irqsave(&mm->context.lock, flags);
+
+#ifdef CONFIG_HUGETLB_PAGE
+       if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
+               if ((tlb_type == hypervisor &&
+                    (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
+                   (tlb_type != hypervisor &&
+                    (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
+                       tsb_index = MM_TSB_HUGE;
+                       tsb_hash_shift = HPAGE_SHIFT;
+               }
        }
+#endif
+
+       tsb = mm->context.tsb_block[tsb_index].tsb;
+       tsb += ((address >> tsb_hash_shift) &
+               (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
+       tag = (address >> 22UL);
+       tsb_insert(tsb, tag, pte_val(pte));
+
+       spin_unlock_irqrestore(&mm->context.lock, flags);
 }
 
 void flush_dcache_page(struct page *page)
@@ -301,6 +327,9 @@ void flush_dcache_page(struct page *page)
        struct address_space *mapping;
        int this_cpu;
 
+       if (tlb_type == hypervisor)
+               return;
+
        /* Do not bother with the expensive D-cache flush if it
         * is merely the zero page.  The 'bigcore' testcase in GDB
         * causes this case to run millions of times.
@@ -340,29 +369,70 @@ void __kprobes flush_icache_range(unsigned long start, unsigned long end)
        if (tlb_type == spitfire) {
                unsigned long kaddr;
 
-               for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
-                       __flush_icache_page(__get_phys(kaddr));
+               /* This code only runs on Spitfire cpus so this is
+                * why we can assume _PAGE_PADDR_4U.
+                */
+               for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
+                       unsigned long paddr, mask = _PAGE_PADDR_4U;
+
+                       if (kaddr >= PAGE_OFFSET)
+                               paddr = kaddr & mask;
+                       else {
+                               pgd_t *pgdp = pgd_offset_k(kaddr);
+                               pud_t *pudp = pud_offset(pgdp, kaddr);
+                               pmd_t *pmdp = pmd_offset(pudp, kaddr);
+                               pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
+
+                               paddr = pte_val(*ptep) & mask;
+                       }
+                       __flush_icache_page(paddr);
+               }
        }
 }
 
-unsigned long page_to_pfn(struct page *page)
-{
-       return (unsigned long) ((page - mem_map) + pfn_base);
-}
-
-struct page *pfn_to_page(unsigned long pfn)
-{
-       return (mem_map + (pfn - pfn_base));
-}
-
 void show_mem(void)
 {
-       printk("Mem-info:\n");
+       unsigned long total = 0, reserved = 0;
+       unsigned long shared = 0, cached = 0;
+       pg_data_t *pgdat;
+
+       printk(KERN_INFO "Mem-info:\n");
        show_free_areas();
-       printk("Free swap:       %6ldkB\n",
+       printk(KERN_INFO "Free swap:       %6ldkB\n",
               nr_swap_pages << (PAGE_SHIFT-10));
-       printk("%ld pages of RAM\n", num_physpages);
-       printk("%d free pages\n", nr_free_pages());
+       for_each_online_pgdat(pgdat) {
+               unsigned long i, flags;
+
+               pgdat_resize_lock(pgdat, &flags);
+               for (i = 0; i < pgdat->node_spanned_pages; i++) {
+                       struct page *page = pgdat_page_nr(pgdat, i);
+                       total++;
+                       if (PageReserved(page))
+                               reserved++;
+                       else if (PageSwapCache(page))
+                               cached++;
+                       else if (page_count(page))
+                               shared += page_count(page) - 1;
+               }
+               pgdat_resize_unlock(pgdat, &flags);
+       }
+
+       printk(KERN_INFO "%lu pages of RAM\n", total);
+       printk(KERN_INFO "%lu reserved pages\n", reserved);
+       printk(KERN_INFO "%lu pages shared\n", shared);
+       printk(KERN_INFO "%lu pages swap cached\n", cached);
+
+       printk(KERN_INFO "%lu pages dirty\n",
+              global_page_state(NR_FILE_DIRTY));
+       printk(KERN_INFO "%lu pages writeback\n",
+              global_page_state(NR_WRITEBACK));
+       printk(KERN_INFO "%lu pages mapped\n",
+              global_page_state(NR_FILE_MAPPED));
+       printk(KERN_INFO "%lu pages slab\n",
+               global_page_state(NR_SLAB_RECLAIMABLE) +
+               global_page_state(NR_SLAB_UNRECLAIMABLE));
+       printk(KERN_INFO "%lu pages pagetables\n",
+              global_page_state(NR_PAGETABLE));
 }
 
 void mmu_info(struct seq_file *m)
@@ -398,39 +468,9 @@ struct linux_prom_translation {
 struct linux_prom_translation prom_trans[512] __read_mostly;
 unsigned int prom_trans_ents __read_mostly;
 
-extern unsigned long prom_boot_page;
-extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
-extern int prom_get_mmu_ihandle(void);
-extern void register_prom_callbacks(void);
-
 /* Exported for SMP bootup purposes. */
 unsigned long kern_locked_tte_data;
 
-/*
- * Translate PROM's mapping we capture at boot time into physical address.
- * The second parameter is only set from prom_callback() invocations.
- */
-unsigned long prom_virt_to_phys(unsigned long promva, int *error)
-{
-       int i;
-
-       for (i = 0; i < prom_trans_ents; i++) {
-               struct linux_prom_translation *p = &prom_trans[i];
-
-               if (promva >= p->virt &&
-                   promva < (p->virt + p->size)) {
-                       unsigned long base = p->data & _PAGE_PADDR;
-
-                       if (error)
-                               *error = 0;
-                       return base + (promva & (8192 - 1));
-               }
-       }
-       if (error)
-               *error = 1;
-       return 0UL;
-}
-
 /* The obp translations are saved based on 8k pagesize, since obp can
  * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  * HI_OBP_ADDRESS range are handled in ktlb.S.
@@ -518,25 +558,17 @@ static void __init hypervisor_tlb_lock(unsigned long vaddr,
                                       unsigned long pte,
                                       unsigned long mmu)
 {
-       register unsigned long func asm("%o5");
-       register unsigned long arg0 asm("%o0");
-       register unsigned long arg1 asm("%o1");
-       register unsigned long arg2 asm("%o2");
-       register unsigned long arg3 asm("%o3");
-
-       func = HV_FAST_MMU_MAP_PERM_ADDR;
-       arg0 = vaddr;
-       arg1 = 0;
-       arg2 = pte;
-       arg3 = mmu;
-       __asm__ __volatile__("ta        0x80"
-                            : "=&r" (func), "=&r" (arg0),
-                              "=&r" (arg1), "=&r" (arg2),
-                              "=&r" (arg3)
-                            : "0" (func), "1" (arg0), "2" (arg1),
-                              "3" (arg2), "4" (arg3));
+       unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
+
+       if (ret != 0) {
+               prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
+                           "errors with %lx\n", vaddr, 0, pte, mmu, ret);
+               prom_halt();
+       }
 }
 
+static unsigned long kern_large_tte(unsigned long paddr);
+
 static void __init remap_kernel(void)
 {
        unsigned long phys_page, tte_vaddr, tte_data;
@@ -544,9 +576,7 @@ static void __init remap_kernel(void)
 
        tte_vaddr = (unsigned long) KERNBASE;
        phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
-       tte_data = (phys_page | (_PAGE_VALID | _PAGE_SZ4MB |
-                                _PAGE_CP | _PAGE_CV | _PAGE_P |
-                                _PAGE_L | _PAGE_W));
+       tte_data = kern_large_tte(phys_page);
 
        kern_locked_tte_data = tte_data;
 
@@ -591,10 +621,6 @@ static void __init inherit_prom_mappings(void)
        prom_printf("Remapping the kernel... ");
        remap_kernel();
        prom_printf("done.\n");
-
-       prom_printf("Registering callbacks... ");
-       register_prom_callbacks();
-       prom_printf("done.\n");
 }
 
 void prom_world(int enter)
@@ -631,62 +657,12 @@ void __flush_dcache_range(unsigned long start, unsigned long end)
 }
 #endif /* DCACHE_ALIASING_POSSIBLE */
 
-/* If not locked, zap it. */
-void __flush_tlb_all(void)
-{
-       unsigned long pstate;
-       int i;
-
-       __asm__ __volatile__("flushw\n\t"
-                            "rdpr      %%pstate, %0\n\t"
-                            "wrpr      %0, %1, %%pstate"
-                            : "=r" (pstate)
-                            : "i" (PSTATE_IE));
-       if (tlb_type == spitfire) {
-               for (i = 0; i < 64; i++) {
-                       /* Spitfire Errata #32 workaround */
-                       /* NOTE: Always runs on spitfire, so no
-                        *       cheetah+ page size encodings.
-                        */
-                       __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
-                                            "flush     %%g6"
-                                            : /* No outputs */
-                                            : "r" (0),
-                                            "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
-
-                       if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
-                               __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
-                                                    "membar #Sync"
-                                                    : /* no outputs */
-                                                    : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
-                               spitfire_put_dtlb_data(i, 0x0UL);
-                       }
-
-                       /* Spitfire Errata #32 workaround */
-                       /* NOTE: Always runs on spitfire, so no
-                        *       cheetah+ page size encodings.
-                        */
-                       __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
-                                            "flush     %%g6"
-                                            : /* No outputs */
-                                            : "r" (0),
-                                            "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
-
-                       if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
-                               __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
-                                                    "membar #Sync"
-                                                    : /* no outputs */
-                                                    : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
-                               spitfire_put_itlb_data(i, 0x0UL);
-                       }
-               }
-       } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
-               cheetah_flush_dtlb_all();
-               cheetah_flush_itlb_all();
-       }
-       __asm__ __volatile__("wrpr      %0, 0, %%pstate"
-                            : : "r" (pstate));
-}
+/* get_new_mmu_context() uses "cache + 1".  */
+DEFINE_SPINLOCK(ctx_alloc_lock);
+unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
+#define MAX_CTX_NR     (1UL << CTX_NR_BITS)
+#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
+DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
 
 /* Caller does TLB context flushing on local CPU if necessary.
  * The caller also ensures that CTX_VALID(mm->context) is false.
@@ -695,17 +671,21 @@ void __flush_tlb_all(void)
  * let the user have CTX 0 (nucleus) or we ever use a CTX
  * version of zero (and thus NO_CONTEXT would not be caught
  * by version mis-match tests in mmu_context.h).
+ *
+ * Always invoked with interrupts disabled.
  */
 void get_new_mmu_context(struct mm_struct *mm)
 {
        unsigned long ctx, new_ctx;
        unsigned long orig_pgsz_bits;
-       
+       unsigned long flags;
+       int new_version;
 
-       spin_lock(&ctx_alloc_lock);
+       spin_lock_irqsave(&ctx_alloc_lock, flags);
        orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
        ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
        new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
+       new_version = 0;
        if (new_ctx >= (1 << CTX_NR_BITS)) {
                new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
                if (new_ctx >= ctx) {
@@ -728,6 +708,7 @@ void get_new_mmu_context(struct mm_struct *mm)
                                mmu_context_bmap[i + 2] = 0;
                                mmu_context_bmap[i + 3] = 0;
                        }
+                       new_version = 1;
                        goto out;
                }
        }
@@ -736,101 +717,207 @@ void get_new_mmu_context(struct mm_struct *mm)
 out:
        tlb_context_cache = new_ctx;
        mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
-       spin_unlock(&ctx_alloc_lock);
+       spin_unlock_irqrestore(&ctx_alloc_lock, flags);
+
+       if (unlikely(new_version))
+               smp_new_mmu_context_version();
 }
 
-void sparc_ultra_dump_itlb(void)
+/* Find a free area for the bootmem map, avoiding the kernel image
+ * and the initial ramdisk.
+ */
+static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
+                                              unsigned long end_pfn)
 {
-        int slot;
+       unsigned long avoid_start, avoid_end, bootmap_size;
+       int i;
 
-       if (tlb_type == spitfire) {
-               printk ("Contents of itlb: ");
-               for (slot = 0; slot < 14; slot++) printk ("    ");
-               printk ("%2x:%016lx,%016lx\n",
-                       0,
-                       spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
-               for (slot = 1; slot < 64; slot+=3) {
-                       printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n", 
-                               slot,
-                               spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
-                               slot+1,
-                               spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
-                               slot+2,
-                               spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
-               }
-       } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
-               printk ("Contents of itlb0:\n");
-               for (slot = 0; slot < 16; slot+=2) {
-                       printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
-                               slot,
-                               cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
-                               slot+1,
-                               cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
-               }
-               printk ("Contents of itlb2:\n");
-               for (slot = 0; slot < 128; slot+=2) {
-                       printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
-                               slot,
-                               cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
-                               slot+1,
-                               cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
+       bootmap_size = bootmem_bootmap_pages(end_pfn - start_pfn);
+       bootmap_size <<= PAGE_SHIFT;
+
+       avoid_start = avoid_end = 0;
+#ifdef CONFIG_BLK_DEV_INITRD
+       avoid_start = initrd_start;
+       avoid_end = PAGE_ALIGN(initrd_end);
+#endif
+
+#ifdef CONFIG_DEBUG_BOOTMEM
+       prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
+                   kern_base, PAGE_ALIGN(kern_base + kern_size),
+                   avoid_start, avoid_end);
+#endif
+       for (i = 0; i < pavail_ents; i++) {
+               unsigned long start, end;
+
+               start = pavail[i].phys_addr;
+               end = start + pavail[i].reg_size;
+
+               while (start < end) {
+                       if (start >= kern_base &&
+                           start < PAGE_ALIGN(kern_base + kern_size)) {
+                               start = PAGE_ALIGN(kern_base + kern_size);
+                               continue;
+                       }
+                       if (start >= avoid_start && start < avoid_end) {
+                               start = avoid_end;
+                               continue;
+                       }
+
+                       if ((end - start) < bootmap_size)
+                               break;
+
+                       if (start < kern_base &&
+                           (start + bootmap_size) > kern_base) {
+                               start = PAGE_ALIGN(kern_base + kern_size);
+                               continue;
+                       }
+
+                       if (start < avoid_start &&
+                           (start + bootmap_size) > avoid_start) {
+                               start = avoid_end;
+                               continue;
+                       }
+
+                       /* OK, it doesn't overlap anything, use it.  */
+#ifdef CONFIG_DEBUG_BOOTMEM
+                       prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
+                                   start >> PAGE_SHIFT, start);
+#endif
+                       return start >> PAGE_SHIFT;
                }
        }
+
+       prom_printf("Cannot find free area for bootmap, aborting.\n");
+       prom_halt();
 }
 
-void sparc_ultra_dump_dtlb(void)
+static void __init trim_pavail(unsigned long *cur_size_p,
+                              unsigned long *end_of_phys_p)
 {
-        int slot;
+       unsigned long to_trim = *cur_size_p - cmdline_memory_size;
+       unsigned long avoid_start, avoid_end;
+       int i;
 
-       if (tlb_type == spitfire) {
-               printk ("Contents of dtlb: ");
-               for (slot = 0; slot < 14; slot++) printk ("    ");
-               printk ("%2x:%016lx,%016lx\n", 0,
-                       spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
-               for (slot = 1; slot < 64; slot+=3) {
-                       printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n", 
-                               slot,
-                               spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
-                               slot+1,
-                               spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
-                               slot+2,
-                               spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
+       to_trim = PAGE_ALIGN(to_trim);
+
+       avoid_start = avoid_end = 0;
+#ifdef CONFIG_BLK_DEV_INITRD
+       avoid_start = initrd_start;
+       avoid_end = PAGE_ALIGN(initrd_end);
+#endif
+
+       /* Trim some pavail[] entries in order to satisfy the
+        * requested "mem=xxx" kernel command line specification.
+        *
+        * We must not trim off the kernel image area nor the
+        * initial ramdisk range (if any).  Also, we must not trim
+        * any pavail[] entry down to zero in order to preserve
+        * the invariant that all pavail[] entries have a non-zero
+        * size which is assumed by all of the code in here.
+        */
+       for (i = 0; i < pavail_ents; i++) {
+               unsigned long start, end, kern_end;
+               unsigned long trim_low, trim_high, n;
+
+               kern_end = PAGE_ALIGN(kern_base + kern_size);
+
+               trim_low = start = pavail[i].phys_addr;
+               trim_high = end = start + pavail[i].reg_size;
+
+               if (kern_base >= start &&
+                   kern_base < end) {
+                       trim_low = kern_base;
+                       if (kern_end >= end)
+                               continue;
                }
-       } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
-               printk ("Contents of dtlb0:\n");
-               for (slot = 0; slot < 16; slot+=2) {
-                       printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
-                               slot,
-                               cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
-                               slot+1,
-                               cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
+               if (kern_end >= start &&
+                   kern_end < end) {
+                       trim_high = kern_end;
                }
-               printk ("Contents of dtlb2:\n");
-               for (slot = 0; slot < 512; slot+=2) {
-                       printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
-                               slot,
-                               cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
-                               slot+1,
-                               cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
+               if (avoid_start &&
+                   avoid_start >= start &&
+                   avoid_start < end) {
+                       if (trim_low > avoid_start)
+                               trim_low = avoid_start;
+                       if (avoid_end >= end)
+                               continue;
                }
-               if (tlb_type == cheetah_plus) {
-                       printk ("Contents of dtlb3:\n");
-                       for (slot = 0; slot < 512; slot+=2) {
-                               printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
-                                       slot,
-                                       cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
-                                       slot+1,
-                                       cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
+               if (avoid_end &&
+                   avoid_end >= start &&
+                   avoid_end < end) {
+                       if (trim_high < avoid_end)
+                               trim_high = avoid_end;
+               }
+
+               if (trim_high <= trim_low)
+                       continue;
+
+               if (trim_low == start && trim_high == end) {
+                       /* Whole chunk is available for trimming.
+                        * Trim all except one page, in order to keep
+                        * entry non-empty.
+                        */
+                       n = (end - start) - PAGE_SIZE;
+                       if (n > to_trim)
+                               n = to_trim;
+
+                       if (n) {
+                               pavail[i].phys_addr += n;
+                               pavail[i].reg_size -= n;
+                               to_trim -= n;
+                       }
+               } else {
+                       n = (trim_low - start);
+                       if (n > to_trim)
+                               n = to_trim;
+
+                       if (n) {
+                               pavail[i].phys_addr += n;
+                               pavail[i].reg_size -= n;
+                               to_trim -= n;
+                       }
+                       if (to_trim) {
+                               n = end - trim_high;
+                               if (n > to_trim)
+                                       n = to_trim;
+                               if (n) {
+                                       pavail[i].reg_size -= n;
+                                       to_trim -= n;
+                               }
                        }
                }
+
+               if (!to_trim)
+                       break;
        }
-}
 
-extern unsigned long cmdline_memory_size;
+       /* Recalculate.  */
+       *cur_size_p = 0UL;
+       for (i = 0; i < pavail_ents; i++) {
+               *end_of_phys_p = pavail[i].phys_addr +
+                       pavail[i].reg_size;
+               *cur_size_p += pavail[i].reg_size;
+       }
+}
 
-unsigned long __init bootmem_init(unsigned long *pages_avail)
+/* About pages_avail, this is the value we will use to calculate
+ * the zholes_size[] argument given to free_area_init_node().  The
+ * page allocator uses this to calculate nr_kernel_pages,
+ * nr_all_pages and zone->present_pages.  On NUMA it is used
+ * to calculate zone->min_unmapped_pages and zone->min_slab_pages.
+ *
+ * So this number should really be set to what the page allocator
+ * actually ends up with.  This means:
+ * 1) It should include bootmem map pages, we'll release those.
+ * 2) It should not include the kernel image, except for the
+ *    __init sections which we will also release.
+ * 3) It should include the initrd image, since we'll release
+ *    that too.
+ */
+static unsigned long __init bootmem_init(unsigned long *pages_avail,
+                                        unsigned long phys_base)
 {
-       unsigned long bootmap_size, start_pfn, end_pfn;
+       unsigned long bootmap_size, end_pfn;
        unsigned long end_of_phys_memory = 0UL;
        unsigned long bootmap_pfn, bytes_avail, size;
        int i;
@@ -844,47 +931,20 @@ unsigned long __init bootmem_init(unsigned long *pages_avail)
                end_of_phys_memory = pavail[i].phys_addr +
                        pavail[i].reg_size;
                bytes_avail += pavail[i].reg_size;
-               if (cmdline_memory_size) {
-                       if (bytes_avail > cmdline_memory_size) {
-                               unsigned long slack = bytes_avail - cmdline_memory_size;
-
-                               bytes_avail -= slack;
-                               end_of_phys_memory -= slack;
-
-                               pavail[i].reg_size -= slack;
-                               if ((long)pavail[i].reg_size <= 0L) {
-                                       pavail[i].phys_addr = 0xdeadbeefUL;
-                                       pavail[i].reg_size = 0UL;
-                                       pavail_ents = i;
-                               } else {
-                                       pavail[i+1].reg_size = 0Ul;
-                                       pavail[i+1].phys_addr = 0xdeadbeefUL;
-                                       pavail_ents = i + 1;
-                               }
-                               break;
-                       }
-               }
        }
 
-       *pages_avail = bytes_avail >> PAGE_SHIFT;
-
-       /* Start with page aligned address of last symbol in kernel
-        * image.  The kernel is hard mapped below PAGE_OFFSET in a
-        * 4MB locked TLB translation.
+       /* Determine the location of the initial ramdisk before trying
+        * to honor the "mem=xxx" command line argument.  We must know
+        * where the kernel image and the ramdisk image are so that we
+        * do not trim those two areas from the physical memory map.
         */
-       start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
-
-       bootmap_pfn = start_pfn;
-
-       end_pfn = end_of_phys_memory >> PAGE_SHIFT;
 
 #ifdef CONFIG_BLK_DEV_INITRD
        /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
        if (sparc_ramdisk_image || sparc_ramdisk_image64) {
                unsigned long ramdisk_image = sparc_ramdisk_image ?
                        sparc_ramdisk_image : sparc_ramdisk_image64;
-               if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
-                       ramdisk_image -= KERNBASE;
+               ramdisk_image -= KERNBASE;
                initrd_start = ramdisk_image + phys_base;
                initrd_end = initrd_start + sparc_ramdisk_size;
                if (initrd_end > end_of_phys_memory) {
@@ -892,23 +952,32 @@ unsigned long __init bootmem_init(unsigned long *pages_avail)
                                         "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
                               initrd_end, end_of_phys_memory);
                        initrd_start = 0;
-               }
-               if (initrd_start) {
-                       if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
-                           initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
-                               bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
+                       initrd_end = 0;
                }
        }
 #endif 
+
+       if (cmdline_memory_size &&
+           bytes_avail > cmdline_memory_size)
+               trim_pavail(&bytes_avail,
+                           &end_of_phys_memory);
+
+       *pages_avail = bytes_avail >> PAGE_SHIFT;
+
+       end_pfn = end_of_phys_memory >> PAGE_SHIFT;
+
        /* Initialize the boot-time allocator. */
        max_pfn = max_low_pfn = end_pfn;
-       min_low_pfn = pfn_base;
+       min_low_pfn = (phys_base >> PAGE_SHIFT);
+
+       bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
 
 #ifdef CONFIG_DEBUG_BOOTMEM
        prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
                    min_low_pfn, bootmap_pfn, max_low_pfn);
 #endif
-       bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
+       bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
+                                        min_low_pfn, end_pfn);
 
        /* Now register the available physical memory with the
         * allocator.
@@ -925,13 +994,12 @@ unsigned long __init bootmem_init(unsigned long *pages_avail)
        if (initrd_start) {
                size = initrd_end - initrd_start;
 
-               /* Resert the initrd image area. */
+               /* Reserve the initrd image area. */
 #ifdef CONFIG_DEBUG_BOOTMEM
                prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
                        initrd_start, initrd_end);
 #endif
                reserve_bootmem(initrd_start, size);
-               *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
 
                initrd_start += PAGE_OFFSET;
                initrd_end += PAGE_OFFSET;
@@ -944,6 +1012,11 @@ unsigned long __init bootmem_init(unsigned long *pages_avail)
        reserve_bootmem(kern_base, kern_size);
        *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
 
+       /* Add back in the initmem pages. */
+       size = ((unsigned long)(__init_end) & PAGE_MASK) -
+               PAGE_ALIGN((unsigned long)__init_begin);
+       *pages_avail += size >> PAGE_SHIFT;
+
        /* Reserve the bootmem map.   We do not account for it
         * in pages_avail because we will release that memory
         * in free_all_bootmem.
@@ -954,11 +1027,27 @@ unsigned long __init bootmem_init(unsigned long *pages_avail)
                    (bootmap_pfn << PAGE_SHIFT), size);
 #endif
        reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
-       *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
+
+       for (i = 0; i < pavail_ents; i++) {
+               unsigned long start_pfn, end_pfn;
+
+               start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
+               end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
+#ifdef CONFIG_DEBUG_BOOTMEM
+               prom_printf("memory_present(0, %lx, %lx)\n",
+                           start_pfn, end_pfn);
+#endif
+               memory_present(0, start_pfn, end_pfn);
+       }
+
+       sparse_init();
 
        return end_pfn;
 }
 
+static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
+static int pall_ents __initdata;
+
 #ifdef CONFIG_DEBUG_PAGEALLOC
 static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
 {
@@ -1014,14 +1103,44 @@ static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend,
        return alloc_bytes;
 }
 
-static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
-static int pall_ents __initdata;
-
 extern unsigned int kvmap_linear_patch[1];
+#endif /* CONFIG_DEBUG_PAGEALLOC */
+
+static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
+{
+       const unsigned long shift_256MB = 28;
+       const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
+       const unsigned long size_256MB = (1UL << shift_256MB);
+
+       while (start < end) {
+               long remains;
+
+               remains = end - start;
+               if (remains < size_256MB)
+                       break;
+
+               if (start & mask_256MB) {
+                       start = (start + size_256MB) & ~mask_256MB;
+                       continue;
+               }
+
+               while (remains >= size_256MB) {
+                       unsigned long index = start >> shift_256MB;
+
+                       __set_bit(index, kpte_linear_bitmap);
+
+                       start += size_256MB;
+                       remains -= size_256MB;
+               }
+       }
+}
 
 static void __init kernel_physical_mapping_init(void)
 {
-       unsigned long i, mem_alloced = 0UL;
+       unsigned long i;
+#ifdef CONFIG_DEBUG_PAGEALLOC
+       unsigned long mem_alloced = 0UL;
+#endif
 
        read_obp_memory("reg", &pall[0], &pall_ents);
 
@@ -1030,10 +1149,16 @@ static void __init kernel_physical_mapping_init(void)
 
                phys_start = pall[i].phys_addr;
                phys_end = phys_start + pall[i].reg_size;
+
+               mark_kpte_bitmap(phys_start, phys_end);
+
+#ifdef CONFIG_DEBUG_PAGEALLOC
                mem_alloced += kernel_map_range(phys_start, phys_end,
                                                PAGE_KERNEL);
+#endif
        }
 
+#ifdef CONFIG_DEBUG_PAGEALLOC
        printk("Allocated %ld bytes for kernel page tables.\n",
               mem_alloced);
 
@@ -1041,8 +1166,10 @@ static void __init kernel_physical_mapping_init(void)
        flushi(&kvmap_linear_patch[0]);
 
        __flush_tlb_all();
+#endif
 }
 
+#ifdef CONFIG_DEBUG_PAGEALLOC
 void kernel_map_pages(struct page *page, int numpages, int enable)
 {
        unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
@@ -1109,28 +1236,137 @@ static void __init tsb_phys_patch(void)
        }
 }
 
+/* Don't mark as init, we give this to the Hypervisor.  */
+#ifndef CONFIG_DEBUG_PAGEALLOC
+#define NUM_KTSB_DESCR 2
+#else
+#define NUM_KTSB_DESCR 1
+#endif
+static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
+extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
+
+static void __init sun4v_ktsb_init(void)
+{
+       unsigned long ktsb_pa;
+
+       /* First KTSB for PAGE_SIZE mappings.  */
+       ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
+
+       switch (PAGE_SIZE) {
+       case 8 * 1024:
+       default:
+               ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
+               ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
+               break;
+
+       case 64 * 1024:
+               ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
+               ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
+               break;
+
+       case 512 * 1024:
+               ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
+               ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
+               break;
+
+       case 4 * 1024 * 1024:
+               ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
+               ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
+               break;
+       };
+
+       ktsb_descr[0].assoc = 1;
+       ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
+       ktsb_descr[0].ctx_idx = 0;
+       ktsb_descr[0].tsb_base = ktsb_pa;
+       ktsb_descr[0].resv = 0;
+
+#ifndef CONFIG_DEBUG_PAGEALLOC
+       /* Second KTSB for 4MB/256MB mappings.  */
+       ktsb_pa = (kern_base +
+                  ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
+
+       ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
+       ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
+                                  HV_PGSZ_MASK_256MB);
+       ktsb_descr[1].assoc = 1;
+       ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
+       ktsb_descr[1].ctx_idx = 0;
+       ktsb_descr[1].tsb_base = ktsb_pa;
+       ktsb_descr[1].resv = 0;
+#endif
+}
+
+void __cpuinit sun4v_ktsb_register(void)
+{
+       unsigned long pa, ret;
+
+       pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
+
+       ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
+       if (ret != 0) {
+               prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
+                           "errors with %lx\n", pa, ret);
+               prom_halt();
+       }
+}
+
 /* paging_init() sets up the page tables */
 
 extern void cheetah_ecache_flush_init(void);
 extern void sun4v_patch_tlb_handlers(void);
 
+extern void cpu_probe(void);
+extern void central_probe(void);
+
 static unsigned long last_valid_pfn;
 pgd_t swapper_pg_dir[2048];
 
+static void sun4u_pgprot_init(void);
+static void sun4v_pgprot_init(void);
+
 void __init paging_init(void)
 {
-       unsigned long end_pfn, pages_avail, shift;
+       unsigned long end_pfn, pages_avail, shift, phys_base;
        unsigned long real_end, i;
 
+       /* These build time checkes make sure that the dcache_dirty_cpu()
+        * page->flags usage will work.
+        *
+        * When a page gets marked as dcache-dirty, we store the
+        * cpu number starting at bit 32 in the page->flags.  Also,
+        * functions like clear_dcache_dirty_cpu use the cpu mask
+        * in 13-bit signed-immediate instruction fields.
+        */
+       BUILD_BUG_ON(FLAGS_RESERVED != 32);
+       BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
+                    ilog2(roundup_pow_of_two(NR_CPUS)) > FLAGS_RESERVED);
+       BUILD_BUG_ON(NR_CPUS > 4096);
+
        kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
        kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
 
+       sstate_booting();
+
+       /* Invalidate both kernel TSBs.  */
+       memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
+#ifndef CONFIG_DEBUG_PAGEALLOC
+       memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
+#endif
+
+       if (tlb_type == hypervisor)
+               sun4v_pgprot_init();
+       else
+               sun4u_pgprot_init();
+
        if (tlb_type == cheetah_plus ||
            tlb_type == hypervisor)
                tsb_phys_patch();
 
-       if (tlb_type == hypervisor)
+       if (tlb_type == hypervisor) {
                sun4v_patch_tlb_handlers();
+               sun4v_ktsb_init();
+       }
 
        /* Find available physical memory... */
        read_obp_memory("available", &pavail[0], &pavail_ents);
@@ -1139,8 +1375,6 @@ void __init paging_init(void)
        for (i = 0; i < pavail_ents; i++)
                phys_base = min(phys_base, pavail[i].phys_addr);
 
-       pfn_base = phys_base >> PAGE_SHIFT;
-
        set_bit(0, mmu_context_bmap);
 
        shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
@@ -1171,32 +1405,44 @@ void __init paging_init(void)
 
        __flush_tlb_all();
 
+       if (tlb_type == hypervisor)
+               sun4v_ktsb_register();
+
        /* Setup bootmem... */
        pages_avail = 0;
-       last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
+       last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
+
+       max_mapnr = last_valid_pfn;
 
-#ifdef CONFIG_DEBUG_PAGEALLOC
        kernel_physical_mapping_init();
-#endif
+
+       real_setup_per_cpu_areas();
+
+       prom_build_devicetree();
+
+       if (tlb_type == hypervisor)
+               sun4v_mdesc_init();
 
        {
                unsigned long zones_size[MAX_NR_ZONES];
                unsigned long zholes_size[MAX_NR_ZONES];
-               unsigned long npages;
                int znum;
 
                for (znum = 0; znum < MAX_NR_ZONES; znum++)
                        zones_size[znum] = zholes_size[znum] = 0;
 
-               npages = end_pfn - pfn_base;
-               zones_size[ZONE_DMA] = npages;
-               zholes_size[ZONE_DMA] = npages - pages_avail;
+               zones_size[ZONE_NORMAL] = end_pfn;
+               zholes_size[ZONE_NORMAL] = end_pfn - pages_avail;
 
                free_area_init_node(0, &contig_page_data, zones_size,
-                                   phys_base >> PAGE_SHIFT, zholes_size);
+                                   __pa(PAGE_OFFSET) >> PAGE_SHIFT,
+                                   zholes_size);
        }
 
-       device_scan();
+       prom_printf("Booting Linux...\n");
+
+       central_probe();
+       cpu_probe();
 }
 
 static void __init taint_real_pages(void)
@@ -1217,7 +1463,7 @@ static void __init taint_real_pages(void)
                while (old_start < old_end) {
                        int n;
 
-                       for (n = 0; pavail_rescan_ents; n++) {
+                       for (n = 0; n < pavail_rescan_ents; n++) {
                                unsigned long new_start, new_end;
 
                                new_start = pavail_rescan[n].phys_addr;
@@ -1239,6 +1485,32 @@ static void __init taint_real_pages(void)
        }
 }
 
+int __init page_in_phys_avail(unsigned long paddr)
+{
+       int i;
+
+       paddr &= PAGE_MASK;
+
+       for (i = 0; i < pavail_rescan_ents; i++) {
+               unsigned long start, end;
+
+               start = pavail_rescan[i].phys_addr;
+               end = start + pavail_rescan[i].reg_size;
+
+               if (paddr >= start && paddr < end)
+                       return 1;
+       }
+       if (paddr >= kern_base && paddr < (kern_base + kern_size))
+               return 1;
+#ifdef CONFIG_BLK_DEV_INITRD
+       if (paddr >= __pa(initrd_start) &&
+           paddr < __pa(PAGE_ALIGN(initrd_end)))
+               return 1;
+#endif
+
+       return 0;
+}
+
 void __init mem_init(void)
 {
        unsigned long codepages, datapages, initpages;
@@ -1263,12 +1535,15 @@ void __init mem_init(void)
 
        taint_real_pages();
 
-       max_mapnr = last_valid_pfn - pfn_base;
        high_memory = __va(last_valid_pfn << PAGE_SHIFT);
 
 #ifdef CONFIG_DEBUG_BOOTMEM
        prom_printf("mem_init: Calling free_all_bootmem().\n");
 #endif
+
+       /* We subtract one to account for the mem_map_zero page
+        * allocated below.
+        */
        totalram_pages = num_physpages = free_all_bootmem() - 1;
 
        /*
@@ -1289,7 +1564,7 @@ void __init mem_init(void)
        initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
        initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
 
-       printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
+       printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
               nr_free_pages() << (PAGE_SHIFT-10),
               codepages << (PAGE_SHIFT-10),
               datapages << (PAGE_SHIFT-10), 
@@ -1316,11 +1591,11 @@ void free_initmem(void)
                page = (addr +
                        ((unsigned long) __va(kern_base)) -
                        ((unsigned long) KERNBASE));
-               memset((void *)addr, 0xcc, PAGE_SIZE);
+               memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
                p = virt_to_page(page);
 
                ClearPageReserved(p);
-               set_page_count(p, 1);
+               init_page_count(p);
                __free_page(p);
                num_physpages++;
                totalram_pages++;
@@ -1336,10 +1611,309 @@ void free_initrd_mem(unsigned long start, unsigned long end)
                struct page *p = virt_to_page(start);
 
                ClearPageReserved(p);
-               set_page_count(p, 1);
+               init_page_count(p);
                __free_page(p);
                num_physpages++;
                totalram_pages++;
        }
 }
 #endif
+
+#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
+#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
+#define __DIRTY_BITS_4U         (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
+#define __DIRTY_BITS_4V         (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
+#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
+#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
+
+pgprot_t PAGE_KERNEL __read_mostly;
+EXPORT_SYMBOL(PAGE_KERNEL);
+
+pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
+pgprot_t PAGE_COPY __read_mostly;
+
+pgprot_t PAGE_SHARED __read_mostly;
+EXPORT_SYMBOL(PAGE_SHARED);
+
+pgprot_t PAGE_EXEC __read_mostly;
+unsigned long pg_iobits __read_mostly;
+
+unsigned long _PAGE_IE __read_mostly;
+EXPORT_SYMBOL(_PAGE_IE);
+
+unsigned long _PAGE_E __read_mostly;
+EXPORT_SYMBOL(_PAGE_E);
+
+unsigned long _PAGE_CACHE __read_mostly;
+EXPORT_SYMBOL(_PAGE_CACHE);
+
+static void prot_init_common(unsigned long page_none,
+                            unsigned long page_shared,
+                            unsigned long page_copy,
+                            unsigned long page_readonly,
+                            unsigned long page_exec_bit)
+{
+       PAGE_COPY = __pgprot(page_copy);
+       PAGE_SHARED = __pgprot(page_shared);
+
+       protection_map[0x0] = __pgprot(page_none);
+       protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
+       protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
+       protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
+       protection_map[0x4] = __pgprot(page_readonly);
+       protection_map[0x5] = __pgprot(page_readonly);
+       protection_map[0x6] = __pgprot(page_copy);
+       protection_map[0x7] = __pgprot(page_copy);
+       protection_map[0x8] = __pgprot(page_none);
+       protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
+       protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
+       protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
+       protection_map[0xc] = __pgprot(page_readonly);
+       protection_map[0xd] = __pgprot(page_readonly);
+       protection_map[0xe] = __pgprot(page_shared);
+       protection_map[0xf] = __pgprot(page_shared);
+}
+
+static void __init sun4u_pgprot_init(void)
+{
+       unsigned long page_none, page_shared, page_copy, page_readonly;
+       unsigned long page_exec_bit;
+
+       PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
+                               _PAGE_CACHE_4U | _PAGE_P_4U |
+                               __ACCESS_BITS_4U | __DIRTY_BITS_4U |
+                               _PAGE_EXEC_4U);
+       PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
+                                      _PAGE_CACHE_4U | _PAGE_P_4U |
+                                      __ACCESS_BITS_4U | __DIRTY_BITS_4U |
+                                      _PAGE_EXEC_4U | _PAGE_L_4U);
+       PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
+
+       _PAGE_IE = _PAGE_IE_4U;
+       _PAGE_E = _PAGE_E_4U;
+       _PAGE_CACHE = _PAGE_CACHE_4U;
+
+       pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
+                    __ACCESS_BITS_4U | _PAGE_E_4U);
+
+#ifdef CONFIG_DEBUG_PAGEALLOC
+       kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
+               0xfffff80000000000;
+#else
+       kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
+               0xfffff80000000000;
+#endif
+       kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
+                                  _PAGE_P_4U | _PAGE_W_4U);
+
+       /* XXX Should use 256MB on Panther. XXX */
+       kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
+
+       _PAGE_SZBITS = _PAGE_SZBITS_4U;
+       _PAGE_ALL_SZ_BITS =  (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
+                             _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
+                             _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
+
+
+       page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
+       page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
+                      __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
+       page_copy   = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
+                      __ACCESS_BITS_4U | _PAGE_EXEC_4U);
+       page_readonly   = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
+                          __ACCESS_BITS_4U | _PAGE_EXEC_4U);
+
+       page_exec_bit = _PAGE_EXEC_4U;
+
+       prot_init_common(page_none, page_shared, page_copy, page_readonly,
+                        page_exec_bit);
+}
+
+static void __init sun4v_pgprot_init(void)
+{
+       unsigned long page_none, page_shared, page_copy, page_readonly;
+       unsigned long page_exec_bit;
+
+       PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
+                               _PAGE_CACHE_4V | _PAGE_P_4V |
+                               __ACCESS_BITS_4V | __DIRTY_BITS_4V |
+                               _PAGE_EXEC_4V);
+       PAGE_KERNEL_LOCKED = PAGE_KERNEL;
+       PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
+
+       _PAGE_IE = _PAGE_IE_4V;
+       _PAGE_E = _PAGE_E_4V;
+       _PAGE_CACHE = _PAGE_CACHE_4V;
+
+#ifdef CONFIG_DEBUG_PAGEALLOC
+       kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
+               0xfffff80000000000;
+#else
+       kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
+               0xfffff80000000000;
+#endif
+       kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
+                                  _PAGE_P_4V | _PAGE_W_4V);
+
+#ifdef CONFIG_DEBUG_PAGEALLOC
+       kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
+               0xfffff80000000000;
+#else
+       kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
+               0xfffff80000000000;
+#endif
+       kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
+                                  _PAGE_P_4V | _PAGE_W_4V);
+
+       pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
+                    __ACCESS_BITS_4V | _PAGE_E_4V);
+
+       _PAGE_SZBITS = _PAGE_SZBITS_4V;
+       _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
+                            _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
+                            _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
+                            _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
+
+       page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
+       page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
+                      __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
+       page_copy   = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
+                      __ACCESS_BITS_4V | _PAGE_EXEC_4V);
+       page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
+                        __ACCESS_BITS_4V | _PAGE_EXEC_4V);
+
+       page_exec_bit = _PAGE_EXEC_4V;
+
+       prot_init_common(page_none, page_shared, page_copy, page_readonly,
+                        page_exec_bit);
+}
+
+unsigned long pte_sz_bits(unsigned long sz)
+{
+       if (tlb_type == hypervisor) {
+               switch (sz) {
+               case 8 * 1024:
+               default:
+                       return _PAGE_SZ8K_4V;
+               case 64 * 1024:
+                       return _PAGE_SZ64K_4V;
+               case 512 * 1024:
+                       return _PAGE_SZ512K_4V;
+               case 4 * 1024 * 1024:
+                       return _PAGE_SZ4MB_4V;
+               };
+       } else {
+               switch (sz) {
+               case 8 * 1024:
+               default:
+                       return _PAGE_SZ8K_4U;
+               case 64 * 1024:
+                       return _PAGE_SZ64K_4U;
+               case 512 * 1024:
+                       return _PAGE_SZ512K_4U;
+               case 4 * 1024 * 1024:
+                       return _PAGE_SZ4MB_4U;
+               };
+       }
+}
+
+pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
+{
+       pte_t pte;
+
+       pte_val(pte)  = page | pgprot_val(pgprot_noncached(prot));
+       pte_val(pte) |= (((unsigned long)space) << 32);
+       pte_val(pte) |= pte_sz_bits(page_size);
+
+       return pte;
+}
+
+static unsigned long kern_large_tte(unsigned long paddr)
+{
+       unsigned long val;
+
+       val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
+              _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
+              _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
+       if (tlb_type == hypervisor)
+               val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
+                      _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
+                      _PAGE_EXEC_4V | _PAGE_W_4V);
+
+       return val | paddr;
+}
+
+/* If not locked, zap it. */
+void __flush_tlb_all(void)
+{
+       unsigned long pstate;
+       int i;
+
+       __asm__ __volatile__("flushw\n\t"
+                            "rdpr      %%pstate, %0\n\t"
+                            "wrpr      %0, %1, %%pstate"
+                            : "=r" (pstate)
+                            : "i" (PSTATE_IE));
+       if (tlb_type == spitfire) {
+               for (i = 0; i < 64; i++) {
+                       /* Spitfire Errata #32 workaround */
+                       /* NOTE: Always runs on spitfire, so no
+                        *       cheetah+ page size encodings.
+                        */
+                       __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
+                                            "flush     %%g6"
+                                            : /* No outputs */
+                                            : "r" (0),
+                                            "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
+
+                       if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
+                               __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
+                                                    "membar #Sync"
+                                                    : /* no outputs */
+                                                    : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
+                               spitfire_put_dtlb_data(i, 0x0UL);
+                       }
+
+                       /* Spitfire Errata #32 workaround */
+                       /* NOTE: Always runs on spitfire, so no
+                        *       cheetah+ page size encodings.
+                        */
+                       __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
+                                            "flush     %%g6"
+                                            : /* No outputs */
+                                            : "r" (0),
+                                            "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
+
+                       if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
+                               __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
+                                                    "membar #Sync"
+                                                    : /* no outputs */
+                                                    : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
+                               spitfire_put_itlb_data(i, 0x0UL);
+                       }
+               }
+       } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
+               cheetah_flush_dtlb_all();
+               cheetah_flush_itlb_all();
+       }
+       __asm__ __volatile__("wrpr      %0, 0, %%pstate"
+                            : : "r" (pstate));
+}
+
+#ifdef CONFIG_MEMORY_HOTPLUG
+
+void online_page(struct page *page)
+{
+       ClearPageReserved(page);
+       init_page_count(page);
+       __free_page(page);
+       totalram_pages++;
+       num_physpages++;
+}
+
+int remove_memory(u64 start, u64 size)
+{
+       return -EINVAL;
+}
+
+#endif /* CONFIG_MEMORY_HOTPLUG */