1 /* $Id: init.c,v 1.209 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/init.c
4 * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
16 #include <linux/hugetlb.h>
17 #include <linux/slab.h>
18 #include <linux/initrd.h>
19 #include <linux/swap.h>
20 #include <linux/pagemap.h>
22 #include <linux/seq_file.h>
23 #include <linux/kprobes.h>
24 #include <linux/cache.h>
25 #include <linux/sort.h>
28 #include <asm/system.h>
30 #include <asm/pgalloc.h>
31 #include <asm/pgtable.h>
32 #include <asm/oplib.h>
33 #include <asm/iommu.h>
35 #include <asm/uaccess.h>
36 #include <asm/mmu_context.h>
37 #include <asm/tlbflush.h>
39 #include <asm/starfire.h>
41 #include <asm/spitfire.h>
42 #include <asm/sections.h>
44 #include <asm/hypervisor.h>
46 extern void device_scan(void);
50 static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
51 static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
52 static int pavail_ents __initdata;
53 static int pavail_rescan_ents __initdata;
55 static int cmp_p64(const void *a, const void *b)
57 const struct linux_prom64_registers *x = a, *y = b;
59 if (x->phys_addr > y->phys_addr)
61 if (x->phys_addr < y->phys_addr)
66 static void __init read_obp_memory(const char *property,
67 struct linux_prom64_registers *regs,
70 int node = prom_finddevice("/memory");
71 int prop_size = prom_getproplen(node, property);
74 ents = prop_size / sizeof(struct linux_prom64_registers);
75 if (ents > MAX_BANKS) {
76 prom_printf("The machine has more %s property entries than "
77 "this kernel can support (%d).\n",
82 ret = prom_getproperty(node, property, (char *) regs, prop_size);
84 prom_printf("Couldn't get %s property from /memory.\n");
90 /* Sanitize what we got from the firmware, by page aligning
93 for (i = 0; i < ents; i++) {
94 unsigned long base, size;
96 base = regs[i].phys_addr;
97 size = regs[i].reg_size;
100 if (base & ~PAGE_MASK) {
101 unsigned long new_base = PAGE_ALIGN(base);
103 size -= new_base - base;
104 if ((long) size < 0L)
108 regs[i].phys_addr = base;
109 regs[i].reg_size = size;
111 sort(regs, ents, sizeof(struct linux_prom64_registers),
115 unsigned long *sparc64_valid_addr_bitmap __read_mostly;
117 /* Ugly, but necessary... -DaveM */
118 unsigned long phys_base __read_mostly;
119 unsigned long kern_base __read_mostly;
120 unsigned long kern_size __read_mostly;
121 unsigned long pfn_base __read_mostly;
122 unsigned long kern_linear_pte_xor __read_mostly;
124 /* get_new_mmu_context() uses "cache + 1". */
125 DEFINE_SPINLOCK(ctx_alloc_lock);
126 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
127 #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
128 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
130 /* References to special section boundaries */
131 extern char _start[], _end[];
133 /* Initial ramdisk setup */
134 extern unsigned long sparc_ramdisk_image64;
135 extern unsigned int sparc_ramdisk_image;
136 extern unsigned int sparc_ramdisk_size;
138 struct page *mem_map_zero __read_mostly;
140 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
142 unsigned long sparc64_kern_pri_context __read_mostly;
143 unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
144 unsigned long sparc64_kern_sec_context __read_mostly;
148 kmem_cache_t *pgtable_cache __read_mostly;
150 static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
155 void pgtable_cache_init(void)
157 pgtable_cache = kmem_cache_create("pgtable_cache",
158 PAGE_SIZE, PAGE_SIZE,
160 SLAB_MUST_HWCACHE_ALIGN,
163 if (!pgtable_cache) {
164 prom_printf("pgtable_cache_init(): Could not create!\n");
169 #ifdef CONFIG_DEBUG_DCFLUSH
170 atomic_t dcpage_flushes = ATOMIC_INIT(0);
172 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
176 __inline__ void flush_dcache_page_impl(struct page *page)
178 #ifdef CONFIG_DEBUG_DCFLUSH
179 atomic_inc(&dcpage_flushes);
182 #ifdef DCACHE_ALIASING_POSSIBLE
183 __flush_dcache_page(page_address(page),
184 ((tlb_type == spitfire) &&
185 page_mapping(page) != NULL));
187 if (page_mapping(page) != NULL &&
188 tlb_type == spitfire)
189 __flush_icache_page(__pa(page_address(page)));
193 #define PG_dcache_dirty PG_arch_1
194 #define PG_dcache_cpu_shift 24
195 #define PG_dcache_cpu_mask (256 - 1)
198 #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
201 #define dcache_dirty_cpu(page) \
202 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
204 static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
206 unsigned long mask = this_cpu;
207 unsigned long non_cpu_bits;
209 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
210 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
212 __asm__ __volatile__("1:\n\t"
214 "and %%g7, %1, %%g1\n\t"
215 "or %%g1, %0, %%g1\n\t"
216 "casx [%2], %%g7, %%g1\n\t"
218 "membar #StoreLoad | #StoreStore\n\t"
219 "bne,pn %%xcc, 1b\n\t"
222 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
226 static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
228 unsigned long mask = (1UL << PG_dcache_dirty);
230 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
233 "srlx %%g7, %4, %%g1\n\t"
234 "and %%g1, %3, %%g1\n\t"
236 "bne,pn %%icc, 2f\n\t"
237 " andn %%g7, %1, %%g1\n\t"
238 "casx [%2], %%g7, %%g1\n\t"
240 "membar #StoreLoad | #StoreStore\n\t"
241 "bne,pn %%xcc, 1b\n\t"
245 : "r" (cpu), "r" (mask), "r" (&page->flags),
246 "i" (PG_dcache_cpu_mask),
247 "i" (PG_dcache_cpu_shift)
251 static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
253 unsigned long tsb_addr = (unsigned long) ent;
255 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
256 tsb_addr = __pa(tsb_addr);
258 __tsb_insert(tsb_addr, tag, pte);
261 unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
262 unsigned long _PAGE_SZBITS __read_mostly;
264 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
266 struct mm_struct *mm;
269 unsigned long pg_flags;
272 if (pfn_valid(pfn) &&
273 (page = pfn_to_page(pfn), page_mapping(page)) &&
274 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
275 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
277 int this_cpu = get_cpu();
279 /* This is just to optimize away some function calls
283 flush_dcache_page_impl(page);
285 smp_flush_dcache_page_impl(page, cpu);
287 clear_dcache_dirty_cpu(page, cpu);
293 if ((pte_val(pte) & _PAGE_ALL_SZ_BITS) == _PAGE_SZBITS) {
297 tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
298 (mm->context.tsb_nentries - 1UL)];
299 tag = (address >> 22UL);
300 tsb_insert(tsb, tag, pte_val(pte));
304 void flush_dcache_page(struct page *page)
306 struct address_space *mapping;
309 /* Do not bother with the expensive D-cache flush if it
310 * is merely the zero page. The 'bigcore' testcase in GDB
311 * causes this case to run millions of times.
313 if (page == ZERO_PAGE(0))
316 this_cpu = get_cpu();
318 mapping = page_mapping(page);
319 if (mapping && !mapping_mapped(mapping)) {
320 int dirty = test_bit(PG_dcache_dirty, &page->flags);
322 int dirty_cpu = dcache_dirty_cpu(page);
324 if (dirty_cpu == this_cpu)
326 smp_flush_dcache_page_impl(page, dirty_cpu);
328 set_dcache_dirty(page, this_cpu);
330 /* We could delay the flush for the !page_mapping
331 * case too. But that case is for exec env/arg
332 * pages and those are %99 certainly going to get
333 * faulted into the tlb (and thus flushed) anyways.
335 flush_dcache_page_impl(page);
342 void __kprobes flush_icache_range(unsigned long start, unsigned long end)
344 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
345 if (tlb_type == spitfire) {
348 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
349 __flush_icache_page(__get_phys(kaddr));
353 unsigned long page_to_pfn(struct page *page)
355 return (unsigned long) ((page - mem_map) + pfn_base);
358 struct page *pfn_to_page(unsigned long pfn)
360 return (mem_map + (pfn - pfn_base));
365 printk("Mem-info:\n");
367 printk("Free swap: %6ldkB\n",
368 nr_swap_pages << (PAGE_SHIFT-10));
369 printk("%ld pages of RAM\n", num_physpages);
370 printk("%d free pages\n", nr_free_pages());
373 void mmu_info(struct seq_file *m)
375 if (tlb_type == cheetah)
376 seq_printf(m, "MMU Type\t: Cheetah\n");
377 else if (tlb_type == cheetah_plus)
378 seq_printf(m, "MMU Type\t: Cheetah+\n");
379 else if (tlb_type == spitfire)
380 seq_printf(m, "MMU Type\t: Spitfire\n");
381 else if (tlb_type == hypervisor)
382 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
384 seq_printf(m, "MMU Type\t: ???\n");
386 #ifdef CONFIG_DEBUG_DCFLUSH
387 seq_printf(m, "DCPageFlushes\t: %d\n",
388 atomic_read(&dcpage_flushes));
390 seq_printf(m, "DCPageFlushesXC\t: %d\n",
391 atomic_read(&dcpage_flushes_xcall));
392 #endif /* CONFIG_SMP */
393 #endif /* CONFIG_DEBUG_DCFLUSH */
396 struct linux_prom_translation {
402 /* Exported for kernel TLB miss handling in ktlb.S */
403 struct linux_prom_translation prom_trans[512] __read_mostly;
404 unsigned int prom_trans_ents __read_mostly;
406 /* Exported for SMP bootup purposes. */
407 unsigned long kern_locked_tte_data;
409 /* The obp translations are saved based on 8k pagesize, since obp can
410 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
411 * HI_OBP_ADDRESS range are handled in ktlb.S.
413 static inline int in_obp_range(unsigned long vaddr)
415 return (vaddr >= LOW_OBP_ADDRESS &&
416 vaddr < HI_OBP_ADDRESS);
419 static int cmp_ptrans(const void *a, const void *b)
421 const struct linux_prom_translation *x = a, *y = b;
423 if (x->virt > y->virt)
425 if (x->virt < y->virt)
430 /* Read OBP translations property into 'prom_trans[]'. */
431 static void __init read_obp_translations(void)
433 int n, node, ents, first, last, i;
435 node = prom_finddevice("/virtual-memory");
436 n = prom_getproplen(node, "translations");
437 if (unlikely(n == 0 || n == -1)) {
438 prom_printf("prom_mappings: Couldn't get size.\n");
441 if (unlikely(n > sizeof(prom_trans))) {
442 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
446 if ((n = prom_getproperty(node, "translations",
447 (char *)&prom_trans[0],
448 sizeof(prom_trans))) == -1) {
449 prom_printf("prom_mappings: Couldn't get property.\n");
453 n = n / sizeof(struct linux_prom_translation);
457 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
460 /* Now kick out all the non-OBP entries. */
461 for (i = 0; i < ents; i++) {
462 if (in_obp_range(prom_trans[i].virt))
466 for (; i < ents; i++) {
467 if (!in_obp_range(prom_trans[i].virt))
472 for (i = 0; i < (last - first); i++) {
473 struct linux_prom_translation *src = &prom_trans[i + first];
474 struct linux_prom_translation *dest = &prom_trans[i];
478 for (; i < ents; i++) {
479 struct linux_prom_translation *dest = &prom_trans[i];
480 dest->virt = dest->size = dest->data = 0x0UL;
483 prom_trans_ents = last - first;
485 if (tlb_type == spitfire) {
486 /* Clear diag TTE bits. */
487 for (i = 0; i < prom_trans_ents; i++)
488 prom_trans[i].data &= ~0x0003fe0000000000UL;
492 static void __init hypervisor_tlb_lock(unsigned long vaddr,
496 register unsigned long func asm("%o5");
497 register unsigned long arg0 asm("%o0");
498 register unsigned long arg1 asm("%o1");
499 register unsigned long arg2 asm("%o2");
500 register unsigned long arg3 asm("%o3");
502 func = HV_FAST_MMU_MAP_PERM_ADDR;
507 __asm__ __volatile__("ta 0x80"
508 : "=&r" (func), "=&r" (arg0),
509 "=&r" (arg1), "=&r" (arg2),
511 : "0" (func), "1" (arg0), "2" (arg1),
512 "3" (arg2), "4" (arg3));
514 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
515 "errors with %lx\n", vaddr, 0, pte, mmu, arg0);
520 static unsigned long kern_large_tte(unsigned long paddr);
522 static void __init remap_kernel(void)
524 unsigned long phys_page, tte_vaddr, tte_data;
525 int tlb_ent = sparc64_highest_locked_tlbent();
527 tte_vaddr = (unsigned long) KERNBASE;
528 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
529 tte_data = kern_large_tte(phys_page);
531 kern_locked_tte_data = tte_data;
533 /* Now lock us into the TLBs via Hypervisor or OBP. */
534 if (tlb_type == hypervisor) {
535 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
536 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
538 tte_vaddr += 0x400000;
539 tte_data += 0x400000;
540 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
541 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
544 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
545 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
548 prom_dtlb_load(tlb_ent,
550 tte_vaddr + 0x400000);
551 prom_itlb_load(tlb_ent,
553 tte_vaddr + 0x400000);
555 sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
557 if (tlb_type == cheetah_plus) {
558 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
559 CTX_CHEETAH_PLUS_NUC);
560 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
561 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
566 static void __init inherit_prom_mappings(void)
568 read_obp_translations();
570 /* Now fixup OBP's idea about where we really are mapped. */
571 prom_printf("Remapping the kernel... ");
573 prom_printf("done.\n");
576 void prom_world(int enter)
579 set_fs((mm_segment_t) { get_thread_current_ds() });
581 __asm__ __volatile__("flushw");
584 #ifdef DCACHE_ALIASING_POSSIBLE
585 void __flush_dcache_range(unsigned long start, unsigned long end)
589 if (tlb_type == spitfire) {
592 for (va = start; va < end; va += 32) {
593 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
597 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
600 for (va = start; va < end; va += 32)
601 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
605 "i" (ASI_DCACHE_INVALIDATE));
608 #endif /* DCACHE_ALIASING_POSSIBLE */
610 /* Caller does TLB context flushing on local CPU if necessary.
611 * The caller also ensures that CTX_VALID(mm->context) is false.
613 * We must be careful about boundary cases so that we never
614 * let the user have CTX 0 (nucleus) or we ever use a CTX
615 * version of zero (and thus NO_CONTEXT would not be caught
616 * by version mis-match tests in mmu_context.h).
618 void get_new_mmu_context(struct mm_struct *mm)
620 unsigned long ctx, new_ctx;
621 unsigned long orig_pgsz_bits;
624 spin_lock(&ctx_alloc_lock);
625 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
626 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
627 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
628 if (new_ctx >= (1 << CTX_NR_BITS)) {
629 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
630 if (new_ctx >= ctx) {
632 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
635 new_ctx = CTX_FIRST_VERSION;
637 /* Don't call memset, for 16 entries that's just
640 mmu_context_bmap[0] = 3;
641 mmu_context_bmap[1] = 0;
642 mmu_context_bmap[2] = 0;
643 mmu_context_bmap[3] = 0;
644 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
645 mmu_context_bmap[i + 0] = 0;
646 mmu_context_bmap[i + 1] = 0;
647 mmu_context_bmap[i + 2] = 0;
648 mmu_context_bmap[i + 3] = 0;
653 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
654 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
656 tlb_context_cache = new_ctx;
657 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
658 spin_unlock(&ctx_alloc_lock);
661 void sparc_ultra_dump_itlb(void)
665 if (tlb_type == spitfire) {
666 printk ("Contents of itlb: ");
667 for (slot = 0; slot < 14; slot++) printk (" ");
668 printk ("%2x:%016lx,%016lx\n",
670 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
671 for (slot = 1; slot < 64; slot+=3) {
672 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
674 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
676 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
678 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
680 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
681 printk ("Contents of itlb0:\n");
682 for (slot = 0; slot < 16; slot+=2) {
683 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
685 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
687 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
689 printk ("Contents of itlb2:\n");
690 for (slot = 0; slot < 128; slot+=2) {
691 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
693 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
695 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
700 void sparc_ultra_dump_dtlb(void)
704 if (tlb_type == spitfire) {
705 printk ("Contents of dtlb: ");
706 for (slot = 0; slot < 14; slot++) printk (" ");
707 printk ("%2x:%016lx,%016lx\n", 0,
708 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
709 for (slot = 1; slot < 64; slot+=3) {
710 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
712 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
714 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
716 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
718 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
719 printk ("Contents of dtlb0:\n");
720 for (slot = 0; slot < 16; slot+=2) {
721 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
723 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
725 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
727 printk ("Contents of dtlb2:\n");
728 for (slot = 0; slot < 512; slot+=2) {
729 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
731 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
733 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
735 if (tlb_type == cheetah_plus) {
736 printk ("Contents of dtlb3:\n");
737 for (slot = 0; slot < 512; slot+=2) {
738 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
740 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
742 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
748 extern unsigned long cmdline_memory_size;
750 unsigned long __init bootmem_init(unsigned long *pages_avail)
752 unsigned long bootmap_size, start_pfn, end_pfn;
753 unsigned long end_of_phys_memory = 0UL;
754 unsigned long bootmap_pfn, bytes_avail, size;
757 #ifdef CONFIG_DEBUG_BOOTMEM
758 prom_printf("bootmem_init: Scan pavail, ");
762 for (i = 0; i < pavail_ents; i++) {
763 end_of_phys_memory = pavail[i].phys_addr +
765 bytes_avail += pavail[i].reg_size;
766 if (cmdline_memory_size) {
767 if (bytes_avail > cmdline_memory_size) {
768 unsigned long slack = bytes_avail - cmdline_memory_size;
770 bytes_avail -= slack;
771 end_of_phys_memory -= slack;
773 pavail[i].reg_size -= slack;
774 if ((long)pavail[i].reg_size <= 0L) {
775 pavail[i].phys_addr = 0xdeadbeefUL;
776 pavail[i].reg_size = 0UL;
779 pavail[i+1].reg_size = 0Ul;
780 pavail[i+1].phys_addr = 0xdeadbeefUL;
788 *pages_avail = bytes_avail >> PAGE_SHIFT;
790 /* Start with page aligned address of last symbol in kernel
791 * image. The kernel is hard mapped below PAGE_OFFSET in a
792 * 4MB locked TLB translation.
794 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
796 bootmap_pfn = start_pfn;
798 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
800 #ifdef CONFIG_BLK_DEV_INITRD
801 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
802 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
803 unsigned long ramdisk_image = sparc_ramdisk_image ?
804 sparc_ramdisk_image : sparc_ramdisk_image64;
805 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
806 ramdisk_image -= KERNBASE;
807 initrd_start = ramdisk_image + phys_base;
808 initrd_end = initrd_start + sparc_ramdisk_size;
809 if (initrd_end > end_of_phys_memory) {
810 printk(KERN_CRIT "initrd extends beyond end of memory "
811 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
812 initrd_end, end_of_phys_memory);
816 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
817 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
818 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
822 /* Initialize the boot-time allocator. */
823 max_pfn = max_low_pfn = end_pfn;
824 min_low_pfn = pfn_base;
826 #ifdef CONFIG_DEBUG_BOOTMEM
827 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
828 min_low_pfn, bootmap_pfn, max_low_pfn);
830 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
832 /* Now register the available physical memory with the
835 for (i = 0; i < pavail_ents; i++) {
836 #ifdef CONFIG_DEBUG_BOOTMEM
837 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
838 i, pavail[i].phys_addr, pavail[i].reg_size);
840 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
843 #ifdef CONFIG_BLK_DEV_INITRD
845 size = initrd_end - initrd_start;
847 /* Resert the initrd image area. */
848 #ifdef CONFIG_DEBUG_BOOTMEM
849 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
850 initrd_start, initrd_end);
852 reserve_bootmem(initrd_start, size);
853 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
855 initrd_start += PAGE_OFFSET;
856 initrd_end += PAGE_OFFSET;
859 /* Reserve the kernel text/data/bss. */
860 #ifdef CONFIG_DEBUG_BOOTMEM
861 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
863 reserve_bootmem(kern_base, kern_size);
864 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
866 /* Reserve the bootmem map. We do not account for it
867 * in pages_avail because we will release that memory
868 * in free_all_bootmem.
871 #ifdef CONFIG_DEBUG_BOOTMEM
872 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
873 (bootmap_pfn << PAGE_SHIFT), size);
875 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
876 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
881 #ifdef CONFIG_DEBUG_PAGEALLOC
882 static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
884 unsigned long vstart = PAGE_OFFSET + pstart;
885 unsigned long vend = PAGE_OFFSET + pend;
886 unsigned long alloc_bytes = 0UL;
888 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
889 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
894 while (vstart < vend) {
895 unsigned long this_end, paddr = __pa(vstart);
896 pgd_t *pgd = pgd_offset_k(vstart);
901 pud = pud_offset(pgd, vstart);
902 if (pud_none(*pud)) {
905 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
906 alloc_bytes += PAGE_SIZE;
907 pud_populate(&init_mm, pud, new);
910 pmd = pmd_offset(pud, vstart);
911 if (!pmd_present(*pmd)) {
914 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
915 alloc_bytes += PAGE_SIZE;
916 pmd_populate_kernel(&init_mm, pmd, new);
919 pte = pte_offset_kernel(pmd, vstart);
920 this_end = (vstart + PMD_SIZE) & PMD_MASK;
924 while (vstart < this_end) {
925 pte_val(*pte) = (paddr | pgprot_val(prot));
936 static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
937 static int pall_ents __initdata;
939 extern unsigned int kvmap_linear_patch[1];
941 static void __init kernel_physical_mapping_init(void)
943 unsigned long i, mem_alloced = 0UL;
945 read_obp_memory("reg", &pall[0], &pall_ents);
947 for (i = 0; i < pall_ents; i++) {
948 unsigned long phys_start, phys_end;
950 phys_start = pall[i].phys_addr;
951 phys_end = phys_start + pall[i].reg_size;
952 mem_alloced += kernel_map_range(phys_start, phys_end,
956 printk("Allocated %ld bytes for kernel page tables.\n",
959 kvmap_linear_patch[0] = 0x01000000; /* nop */
960 flushi(&kvmap_linear_patch[0]);
965 void kernel_map_pages(struct page *page, int numpages, int enable)
967 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
968 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
970 kernel_map_range(phys_start, phys_end,
971 (enable ? PAGE_KERNEL : __pgprot(0)));
973 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
974 PAGE_OFFSET + phys_end);
976 /* we should perform an IPI and flush all tlbs,
977 * but that can deadlock->flush only current cpu.
979 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
980 PAGE_OFFSET + phys_end);
984 unsigned long __init find_ecache_flush_span(unsigned long size)
988 for (i = 0; i < pavail_ents; i++) {
989 if (pavail[i].reg_size >= size)
990 return pavail[i].phys_addr;
996 static void __init tsb_phys_patch(void)
998 struct tsb_ldquad_phys_patch_entry *pquad;
999 struct tsb_phys_patch_entry *p;
1001 pquad = &__tsb_ldquad_phys_patch;
1002 while (pquad < &__tsb_ldquad_phys_patch_end) {
1003 unsigned long addr = pquad->addr;
1005 if (tlb_type == hypervisor)
1006 *(unsigned int *) addr = pquad->sun4v_insn;
1008 *(unsigned int *) addr = pquad->sun4u_insn;
1010 __asm__ __volatile__("flush %0"
1017 p = &__tsb_phys_patch;
1018 while (p < &__tsb_phys_patch_end) {
1019 unsigned long addr = p->addr;
1021 *(unsigned int *) addr = p->insn;
1023 __asm__ __volatile__("flush %0"
1031 /* Don't mark as init, we give this to the Hypervisor. */
1032 static struct hv_tsb_descr ktsb_descr[2];
1033 extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1035 static void __init sun4v_ktsb_init(void)
1037 unsigned long ktsb_pa;
1039 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1041 switch (PAGE_SIZE) {
1044 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1045 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1049 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1050 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1054 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1055 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1058 case 4 * 1024 * 1024:
1059 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1060 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1064 ktsb_descr[0].assoc = 1;
1065 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1066 ktsb_descr[0].ctx_idx = 0;
1067 ktsb_descr[0].tsb_base = ktsb_pa;
1068 ktsb_descr[0].resv = 0;
1070 /* XXX When we have a kernel large page size TSB, describe
1071 * XXX it in ktsb_descr[1] here.
1075 void __cpuinit sun4v_ktsb_register(void)
1077 register unsigned long func asm("%o5");
1078 register unsigned long arg0 asm("%o0");
1079 register unsigned long arg1 asm("%o1");
1082 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1084 func = HV_FAST_MMU_TSB_CTX0;
1085 /* XXX set arg0 to 2 when we use ktsb_descr[1], see above XXX */
1088 __asm__ __volatile__("ta %6"
1089 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
1090 : "0" (func), "1" (arg0), "2" (arg1),
1091 "i" (HV_FAST_TRAP));
1094 /* paging_init() sets up the page tables */
1096 extern void cheetah_ecache_flush_init(void);
1097 extern void sun4v_patch_tlb_handlers(void);
1099 static unsigned long last_valid_pfn;
1100 pgd_t swapper_pg_dir[2048];
1102 static void sun4u_pgprot_init(void);
1103 static void sun4v_pgprot_init(void);
1105 void __init paging_init(void)
1107 unsigned long end_pfn, pages_avail, shift;
1108 unsigned long real_end, i;
1110 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1111 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1113 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
1115 if (tlb_type == hypervisor)
1116 sun4v_pgprot_init();
1118 sun4u_pgprot_init();
1120 if (tlb_type == cheetah_plus ||
1121 tlb_type == hypervisor)
1124 if (tlb_type == hypervisor) {
1125 sun4v_patch_tlb_handlers();
1129 /* Find available physical memory... */
1130 read_obp_memory("available", &pavail[0], &pavail_ents);
1132 phys_base = 0xffffffffffffffffUL;
1133 for (i = 0; i < pavail_ents; i++)
1134 phys_base = min(phys_base, pavail[i].phys_addr);
1136 pfn_base = phys_base >> PAGE_SHIFT;
1138 set_bit(0, mmu_context_bmap);
1140 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1142 real_end = (unsigned long)_end;
1143 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1145 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1146 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1150 /* Set kernel pgd to upper alias so physical page computations
1153 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1155 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
1157 /* Now can init the kernel/bad page tables. */
1158 pud_set(pud_offset(&swapper_pg_dir[0], 0),
1159 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
1161 inherit_prom_mappings();
1163 /* Ok, we can use our TLB miss and window trap handlers safely. */
1168 if (tlb_type == hypervisor)
1169 sun4v_ktsb_register();
1171 /* Setup bootmem... */
1173 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1175 #ifdef CONFIG_DEBUG_PAGEALLOC
1176 kernel_physical_mapping_init();
1180 unsigned long zones_size[MAX_NR_ZONES];
1181 unsigned long zholes_size[MAX_NR_ZONES];
1182 unsigned long npages;
1185 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1186 zones_size[znum] = zholes_size[znum] = 0;
1188 npages = end_pfn - pfn_base;
1189 zones_size[ZONE_DMA] = npages;
1190 zholes_size[ZONE_DMA] = npages - pages_avail;
1192 free_area_init_node(0, &contig_page_data, zones_size,
1193 phys_base >> PAGE_SHIFT, zholes_size);
1199 static void __init taint_real_pages(void)
1203 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
1205 /* Find changes discovered in the physmem available rescan and
1206 * reserve the lost portions in the bootmem maps.
1208 for (i = 0; i < pavail_ents; i++) {
1209 unsigned long old_start, old_end;
1211 old_start = pavail[i].phys_addr;
1212 old_end = old_start +
1214 while (old_start < old_end) {
1217 for (n = 0; pavail_rescan_ents; n++) {
1218 unsigned long new_start, new_end;
1220 new_start = pavail_rescan[n].phys_addr;
1221 new_end = new_start +
1222 pavail_rescan[n].reg_size;
1224 if (new_start <= old_start &&
1225 new_end >= (old_start + PAGE_SIZE)) {
1226 set_bit(old_start >> 22,
1227 sparc64_valid_addr_bitmap);
1231 reserve_bootmem(old_start, PAGE_SIZE);
1234 old_start += PAGE_SIZE;
1239 void __init mem_init(void)
1241 unsigned long codepages, datapages, initpages;
1242 unsigned long addr, last;
1245 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1247 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
1248 if (sparc64_valid_addr_bitmap == NULL) {
1249 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1252 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1254 addr = PAGE_OFFSET + kern_base;
1255 last = PAGE_ALIGN(kern_size) + addr;
1256 while (addr < last) {
1257 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1263 max_mapnr = last_valid_pfn - pfn_base;
1264 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1266 #ifdef CONFIG_DEBUG_BOOTMEM
1267 prom_printf("mem_init: Calling free_all_bootmem().\n");
1269 totalram_pages = num_physpages = free_all_bootmem() - 1;
1272 * Set up the zero page, mark it reserved, so that page count
1273 * is not manipulated when freeing the page from user ptes.
1275 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1276 if (mem_map_zero == NULL) {
1277 prom_printf("paging_init: Cannot alloc zero page.\n");
1280 SetPageReserved(mem_map_zero);
1282 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1283 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1284 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1285 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1286 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1287 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1289 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1290 nr_free_pages() << (PAGE_SHIFT-10),
1291 codepages << (PAGE_SHIFT-10),
1292 datapages << (PAGE_SHIFT-10),
1293 initpages << (PAGE_SHIFT-10),
1294 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1296 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1297 cheetah_ecache_flush_init();
1300 void free_initmem(void)
1302 unsigned long addr, initend;
1305 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1307 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1308 initend = (unsigned long)(__init_end) & PAGE_MASK;
1309 for (; addr < initend; addr += PAGE_SIZE) {
1314 ((unsigned long) __va(kern_base)) -
1315 ((unsigned long) KERNBASE));
1316 memset((void *)addr, 0xcc, PAGE_SIZE);
1317 p = virt_to_page(page);
1319 ClearPageReserved(p);
1320 set_page_count(p, 1);
1327 #ifdef CONFIG_BLK_DEV_INITRD
1328 void free_initrd_mem(unsigned long start, unsigned long end)
1331 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1332 for (; start < end; start += PAGE_SIZE) {
1333 struct page *p = virt_to_page(start);
1335 ClearPageReserved(p);
1336 set_page_count(p, 1);
1344 #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1345 #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1346 #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1347 #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1348 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1349 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1351 pgprot_t PAGE_KERNEL __read_mostly;
1352 EXPORT_SYMBOL(PAGE_KERNEL);
1354 pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
1355 pgprot_t PAGE_COPY __read_mostly;
1356 pgprot_t PAGE_EXEC __read_mostly;
1357 unsigned long pg_iobits __read_mostly;
1359 unsigned long _PAGE_IE __read_mostly;
1360 unsigned long _PAGE_E __read_mostly;
1361 unsigned long _PAGE_CACHE __read_mostly;
1363 static void prot_init_common(unsigned long page_none,
1364 unsigned long page_shared,
1365 unsigned long page_copy,
1366 unsigned long page_readonly,
1367 unsigned long page_exec_bit)
1369 PAGE_COPY = __pgprot(page_copy);
1371 protection_map[0x0] = __pgprot(page_none);
1372 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
1373 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
1374 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
1375 protection_map[0x4] = __pgprot(page_readonly);
1376 protection_map[0x5] = __pgprot(page_readonly);
1377 protection_map[0x6] = __pgprot(page_copy);
1378 protection_map[0x7] = __pgprot(page_copy);
1379 protection_map[0x8] = __pgprot(page_none);
1380 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
1381 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
1382 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
1383 protection_map[0xc] = __pgprot(page_readonly);
1384 protection_map[0xd] = __pgprot(page_readonly);
1385 protection_map[0xe] = __pgprot(page_shared);
1386 protection_map[0xf] = __pgprot(page_shared);
1389 static void __init sun4u_pgprot_init(void)
1391 unsigned long page_none, page_shared, page_copy, page_readonly;
1392 unsigned long page_exec_bit;
1394 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1395 _PAGE_CACHE_4U | _PAGE_P_4U |
1396 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1398 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1399 _PAGE_CACHE_4U | _PAGE_P_4U |
1400 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1401 _PAGE_EXEC_4U | _PAGE_L_4U);
1402 PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
1404 _PAGE_IE = _PAGE_IE_4U;
1405 _PAGE_E = _PAGE_E_4U;
1406 _PAGE_CACHE = _PAGE_CACHE_4U;
1408 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
1409 __ACCESS_BITS_4U | _PAGE_E_4U);
1411 kern_linear_pte_xor = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
1413 kern_linear_pte_xor |= (_PAGE_CP_4U | _PAGE_CV_4U |
1414 _PAGE_P_4U | _PAGE_W_4U);
1416 _PAGE_SZBITS = _PAGE_SZBITS_4U;
1417 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
1418 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
1419 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
1422 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
1423 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1424 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
1425 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1426 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1427 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1428 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1430 page_exec_bit = _PAGE_EXEC_4U;
1432 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1436 static void __init sun4v_pgprot_init(void)
1438 unsigned long page_none, page_shared, page_copy, page_readonly;
1439 unsigned long page_exec_bit;
1441 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
1442 _PAGE_CACHE_4V | _PAGE_P_4V |
1443 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
1445 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
1446 PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
1448 _PAGE_IE = _PAGE_IE_4V;
1449 _PAGE_E = _PAGE_E_4V;
1450 _PAGE_CACHE = _PAGE_CACHE_4V;
1452 kern_linear_pte_xor = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
1454 kern_linear_pte_xor |= (_PAGE_CP_4V | _PAGE_CV_4V |
1455 _PAGE_P_4V | _PAGE_W_4V);
1457 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
1458 __ACCESS_BITS_4V | _PAGE_E_4V);
1460 _PAGE_SZBITS = _PAGE_SZBITS_4V;
1461 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
1462 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
1463 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
1464 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
1466 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
1467 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1468 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
1469 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1470 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1471 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1472 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1474 page_exec_bit = _PAGE_EXEC_4V;
1476 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1480 unsigned long pte_sz_bits(unsigned long sz)
1482 if (tlb_type == hypervisor) {
1486 return _PAGE_SZ8K_4V;
1488 return _PAGE_SZ64K_4V;
1490 return _PAGE_SZ512K_4V;
1491 case 4 * 1024 * 1024:
1492 return _PAGE_SZ4MB_4V;
1498 return _PAGE_SZ8K_4U;
1500 return _PAGE_SZ64K_4U;
1502 return _PAGE_SZ512K_4U;
1503 case 4 * 1024 * 1024:
1504 return _PAGE_SZ4MB_4U;
1509 pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
1513 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
1514 pte_val(pte) |= (((unsigned long)space) << 32);
1515 pte_val(pte) |= pte_sz_bits(page_size);
1520 static unsigned long kern_large_tte(unsigned long paddr)
1524 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1525 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
1526 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
1527 if (tlb_type == hypervisor)
1528 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1529 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
1530 _PAGE_EXEC_4V | _PAGE_W_4V);
1536 * Translate PROM's mapping we capture at boot time into physical address.
1537 * The second parameter is only set from prom_callback() invocations.
1539 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
1544 mask = _PAGE_PADDR_4U;
1545 if (tlb_type == hypervisor)
1546 mask = _PAGE_PADDR_4V;
1548 for (i = 0; i < prom_trans_ents; i++) {
1549 struct linux_prom_translation *p = &prom_trans[i];
1551 if (promva >= p->virt &&
1552 promva < (p->virt + p->size)) {
1553 unsigned long base = p->data & mask;
1557 return base + (promva & (8192 - 1));
1565 /* XXX We should kill off this ugly thing at so me point. XXX */
1566 unsigned long sun4u_get_pte(unsigned long addr)
1572 unsigned long mask = _PAGE_PADDR_4U;
1574 if (tlb_type == hypervisor)
1575 mask = _PAGE_PADDR_4V;
1577 if (addr >= PAGE_OFFSET)
1580 if ((addr >= LOW_OBP_ADDRESS) && (addr < HI_OBP_ADDRESS))
1581 return prom_virt_to_phys(addr, NULL);
1583 pgdp = pgd_offset_k(addr);
1584 pudp = pud_offset(pgdp, addr);
1585 pmdp = pmd_offset(pudp, addr);
1586 ptep = pte_offset_kernel(pmdp, addr);
1588 return pte_val(*ptep) & mask;
1591 /* If not locked, zap it. */
1592 void __flush_tlb_all(void)
1594 unsigned long pstate;
1597 __asm__ __volatile__("flushw\n\t"
1598 "rdpr %%pstate, %0\n\t"
1599 "wrpr %0, %1, %%pstate"
1602 if (tlb_type == spitfire) {
1603 for (i = 0; i < 64; i++) {
1604 /* Spitfire Errata #32 workaround */
1605 /* NOTE: Always runs on spitfire, so no
1606 * cheetah+ page size encodings.
1608 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1612 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1614 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
1615 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1618 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1619 spitfire_put_dtlb_data(i, 0x0UL);
1622 /* Spitfire Errata #32 workaround */
1623 /* NOTE: Always runs on spitfire, so no
1624 * cheetah+ page size encodings.
1626 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1630 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1632 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
1633 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1636 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1637 spitfire_put_itlb_data(i, 0x0UL);
1640 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1641 cheetah_flush_dtlb_all();
1642 cheetah_flush_itlb_all();
1644 __asm__ __volatile__("wrpr %0, 0, %%pstate"