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);
48 #define MAX_PHYS_ADDRESS (1UL << 42UL)
49 #define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
50 #define KPTE_BITMAP_BYTES \
51 ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
53 unsigned long kern_linear_pte_xor
[2] __read_mostly
;
55 /* A bitmap, one bit for every 256MB of physical memory. If the bit
56 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
57 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
59 unsigned long kpte_linear_bitmap
[KPTE_BITMAP_BYTES
/ sizeof(unsigned long)];
61 /* A special kernel TSB for 4MB and 256MB linear mappings. */
62 struct tsb swapper_4m_tsb
[KERNEL_TSB4M_NENTRIES
];
66 static struct linux_prom64_registers pavail
[MAX_BANKS
] __initdata
;
67 static struct linux_prom64_registers pavail_rescan
[MAX_BANKS
] __initdata
;
68 static int pavail_ents __initdata
;
69 static int pavail_rescan_ents __initdata
;
71 static int cmp_p64(const void *a
, const void *b
)
73 const struct linux_prom64_registers
*x
= a
, *y
= b
;
75 if (x
->phys_addr
> y
->phys_addr
)
77 if (x
->phys_addr
< y
->phys_addr
)
82 static void __init
read_obp_memory(const char *property
,
83 struct linux_prom64_registers
*regs
,
86 int node
= prom_finddevice("/memory");
87 int prop_size
= prom_getproplen(node
, property
);
90 ents
= prop_size
/ sizeof(struct linux_prom64_registers
);
91 if (ents
> MAX_BANKS
) {
92 prom_printf("The machine has more %s property entries than "
93 "this kernel can support (%d).\n",
98 ret
= prom_getproperty(node
, property
, (char *) regs
, prop_size
);
100 prom_printf("Couldn't get %s property from /memory.\n");
106 /* Sanitize what we got from the firmware, by page aligning
109 for (i
= 0; i
< ents
; i
++) {
110 unsigned long base
, size
;
112 base
= regs
[i
].phys_addr
;
113 size
= regs
[i
].reg_size
;
116 if (base
& ~PAGE_MASK
) {
117 unsigned long new_base
= PAGE_ALIGN(base
);
119 size
-= new_base
- base
;
120 if ((long) size
< 0L)
124 regs
[i
].phys_addr
= base
;
125 regs
[i
].reg_size
= size
;
127 sort(regs
, ents
, sizeof(struct linux_prom64_registers
),
131 unsigned long *sparc64_valid_addr_bitmap __read_mostly
;
133 /* Ugly, but necessary... -DaveM */
134 unsigned long phys_base __read_mostly
;
135 unsigned long kern_base __read_mostly
;
136 unsigned long kern_size __read_mostly
;
137 unsigned long pfn_base __read_mostly
;
139 /* get_new_mmu_context() uses "cache + 1". */
140 DEFINE_SPINLOCK(ctx_alloc_lock
);
141 unsigned long tlb_context_cache
= CTX_FIRST_VERSION
- 1;
142 #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
143 unsigned long mmu_context_bmap
[CTX_BMAP_SLOTS
];
145 /* References to special section boundaries */
146 extern char _start
[], _end
[];
148 /* Initial ramdisk setup */
149 extern unsigned long sparc_ramdisk_image64
;
150 extern unsigned int sparc_ramdisk_image
;
151 extern unsigned int sparc_ramdisk_size
;
153 struct page
*mem_map_zero __read_mostly
;
155 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly
;
157 unsigned long sparc64_kern_pri_context __read_mostly
;
158 unsigned long sparc64_kern_pri_nuc_bits __read_mostly
;
159 unsigned long sparc64_kern_sec_context __read_mostly
;
163 kmem_cache_t
*pgtable_cache __read_mostly
;
165 static void zero_ctor(void *addr
, kmem_cache_t
*cache
, unsigned long flags
)
170 void pgtable_cache_init(void)
172 pgtable_cache
= kmem_cache_create("pgtable_cache",
173 PAGE_SIZE
, PAGE_SIZE
,
175 SLAB_MUST_HWCACHE_ALIGN
,
178 if (!pgtable_cache
) {
179 prom_printf("pgtable_cache_init(): Could not create!\n");
184 #ifdef CONFIG_DEBUG_DCFLUSH
185 atomic_t dcpage_flushes
= ATOMIC_INIT(0);
187 atomic_t dcpage_flushes_xcall
= ATOMIC_INIT(0);
191 __inline__
void flush_dcache_page_impl(struct page
*page
)
193 #ifdef CONFIG_DEBUG_DCFLUSH
194 atomic_inc(&dcpage_flushes
);
197 #ifdef DCACHE_ALIASING_POSSIBLE
198 __flush_dcache_page(page_address(page
),
199 ((tlb_type
== spitfire
) &&
200 page_mapping(page
) != NULL
));
202 if (page_mapping(page
) != NULL
&&
203 tlb_type
== spitfire
)
204 __flush_icache_page(__pa(page_address(page
)));
208 #define PG_dcache_dirty PG_arch_1
209 #define PG_dcache_cpu_shift 24
210 #define PG_dcache_cpu_mask (256 - 1)
213 #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
216 #define dcache_dirty_cpu(page) \
217 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
219 static __inline__
void set_dcache_dirty(struct page
*page
, int this_cpu
)
221 unsigned long mask
= this_cpu
;
222 unsigned long non_cpu_bits
;
224 non_cpu_bits
= ~(PG_dcache_cpu_mask
<< PG_dcache_cpu_shift
);
225 mask
= (mask
<< PG_dcache_cpu_shift
) | (1UL << PG_dcache_dirty
);
227 __asm__
__volatile__("1:\n\t"
229 "and %%g7, %1, %%g1\n\t"
230 "or %%g1, %0, %%g1\n\t"
231 "casx [%2], %%g7, %%g1\n\t"
233 "membar #StoreLoad | #StoreStore\n\t"
234 "bne,pn %%xcc, 1b\n\t"
237 : "r" (mask
), "r" (non_cpu_bits
), "r" (&page
->flags
)
241 static __inline__
void clear_dcache_dirty_cpu(struct page
*page
, unsigned long cpu
)
243 unsigned long mask
= (1UL << PG_dcache_dirty
);
245 __asm__
__volatile__("! test_and_clear_dcache_dirty\n"
248 "srlx %%g7, %4, %%g1\n\t"
249 "and %%g1, %3, %%g1\n\t"
251 "bne,pn %%icc, 2f\n\t"
252 " andn %%g7, %1, %%g1\n\t"
253 "casx [%2], %%g7, %%g1\n\t"
255 "membar #StoreLoad | #StoreStore\n\t"
256 "bne,pn %%xcc, 1b\n\t"
260 : "r" (cpu
), "r" (mask
), "r" (&page
->flags
),
261 "i" (PG_dcache_cpu_mask
),
262 "i" (PG_dcache_cpu_shift
)
266 static inline void tsb_insert(struct tsb
*ent
, unsigned long tag
, unsigned long pte
)
268 unsigned long tsb_addr
= (unsigned long) ent
;
270 if (tlb_type
== cheetah_plus
|| tlb_type
== hypervisor
)
271 tsb_addr
= __pa(tsb_addr
);
273 __tsb_insert(tsb_addr
, tag
, pte
);
276 unsigned long _PAGE_ALL_SZ_BITS __read_mostly
;
277 unsigned long _PAGE_SZBITS __read_mostly
;
279 void update_mmu_cache(struct vm_area_struct
*vma
, unsigned long address
, pte_t pte
)
281 struct mm_struct
*mm
;
284 unsigned long pg_flags
;
287 if (pfn_valid(pfn
) &&
288 (page
= pfn_to_page(pfn
), page_mapping(page
)) &&
289 ((pg_flags
= page
->flags
) & (1UL << PG_dcache_dirty
))) {
290 int cpu
= ((pg_flags
>> PG_dcache_cpu_shift
) &
292 int this_cpu
= get_cpu();
294 /* This is just to optimize away some function calls
298 flush_dcache_page_impl(page
);
300 smp_flush_dcache_page_impl(page
, cpu
);
302 clear_dcache_dirty_cpu(page
, cpu
);
308 if ((pte_val(pte
) & _PAGE_ALL_SZ_BITS
) == _PAGE_SZBITS
) {
312 tsb
= &mm
->context
.tsb
[(address
>> PAGE_SHIFT
) &
313 (mm
->context
.tsb_nentries
- 1UL)];
314 tag
= (address
>> 22UL);
315 tsb_insert(tsb
, tag
, pte_val(pte
));
319 void flush_dcache_page(struct page
*page
)
321 struct address_space
*mapping
;
324 /* Do not bother with the expensive D-cache flush if it
325 * is merely the zero page. The 'bigcore' testcase in GDB
326 * causes this case to run millions of times.
328 if (page
== ZERO_PAGE(0))
331 this_cpu
= get_cpu();
333 mapping
= page_mapping(page
);
334 if (mapping
&& !mapping_mapped(mapping
)) {
335 int dirty
= test_bit(PG_dcache_dirty
, &page
->flags
);
337 int dirty_cpu
= dcache_dirty_cpu(page
);
339 if (dirty_cpu
== this_cpu
)
341 smp_flush_dcache_page_impl(page
, dirty_cpu
);
343 set_dcache_dirty(page
, this_cpu
);
345 /* We could delay the flush for the !page_mapping
346 * case too. But that case is for exec env/arg
347 * pages and those are %99 certainly going to get
348 * faulted into the tlb (and thus flushed) anyways.
350 flush_dcache_page_impl(page
);
357 void __kprobes
flush_icache_range(unsigned long start
, unsigned long end
)
359 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
360 if (tlb_type
== spitfire
) {
363 for (kaddr
= start
; kaddr
< end
; kaddr
+= PAGE_SIZE
)
364 __flush_icache_page(__get_phys(kaddr
));
368 unsigned long page_to_pfn(struct page
*page
)
370 return (unsigned long) ((page
- mem_map
) + pfn_base
);
373 struct page
*pfn_to_page(unsigned long pfn
)
375 return (mem_map
+ (pfn
- pfn_base
));
380 printk("Mem-info:\n");
382 printk("Free swap: %6ldkB\n",
383 nr_swap_pages
<< (PAGE_SHIFT
-10));
384 printk("%ld pages of RAM\n", num_physpages
);
385 printk("%d free pages\n", nr_free_pages());
388 void mmu_info(struct seq_file
*m
)
390 if (tlb_type
== cheetah
)
391 seq_printf(m
, "MMU Type\t: Cheetah\n");
392 else if (tlb_type
== cheetah_plus
)
393 seq_printf(m
, "MMU Type\t: Cheetah+\n");
394 else if (tlb_type
== spitfire
)
395 seq_printf(m
, "MMU Type\t: Spitfire\n");
396 else if (tlb_type
== hypervisor
)
397 seq_printf(m
, "MMU Type\t: Hypervisor (sun4v)\n");
399 seq_printf(m
, "MMU Type\t: ???\n");
401 #ifdef CONFIG_DEBUG_DCFLUSH
402 seq_printf(m
, "DCPageFlushes\t: %d\n",
403 atomic_read(&dcpage_flushes
));
405 seq_printf(m
, "DCPageFlushesXC\t: %d\n",
406 atomic_read(&dcpage_flushes_xcall
));
407 #endif /* CONFIG_SMP */
408 #endif /* CONFIG_DEBUG_DCFLUSH */
411 struct linux_prom_translation
{
417 /* Exported for kernel TLB miss handling in ktlb.S */
418 struct linux_prom_translation prom_trans
[512] __read_mostly
;
419 unsigned int prom_trans_ents __read_mostly
;
421 /* Exported for SMP bootup purposes. */
422 unsigned long kern_locked_tte_data
;
424 /* The obp translations are saved based on 8k pagesize, since obp can
425 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
426 * HI_OBP_ADDRESS range are handled in ktlb.S.
428 static inline int in_obp_range(unsigned long vaddr
)
430 return (vaddr
>= LOW_OBP_ADDRESS
&&
431 vaddr
< HI_OBP_ADDRESS
);
434 static int cmp_ptrans(const void *a
, const void *b
)
436 const struct linux_prom_translation
*x
= a
, *y
= b
;
438 if (x
->virt
> y
->virt
)
440 if (x
->virt
< y
->virt
)
445 /* Read OBP translations property into 'prom_trans[]'. */
446 static void __init
read_obp_translations(void)
448 int n
, node
, ents
, first
, last
, i
;
450 node
= prom_finddevice("/virtual-memory");
451 n
= prom_getproplen(node
, "translations");
452 if (unlikely(n
== 0 || n
== -1)) {
453 prom_printf("prom_mappings: Couldn't get size.\n");
456 if (unlikely(n
> sizeof(prom_trans
))) {
457 prom_printf("prom_mappings: Size %Zd is too big.\n", n
);
461 if ((n
= prom_getproperty(node
, "translations",
462 (char *)&prom_trans
[0],
463 sizeof(prom_trans
))) == -1) {
464 prom_printf("prom_mappings: Couldn't get property.\n");
468 n
= n
/ sizeof(struct linux_prom_translation
);
472 sort(prom_trans
, ents
, sizeof(struct linux_prom_translation
),
475 /* Now kick out all the non-OBP entries. */
476 for (i
= 0; i
< ents
; i
++) {
477 if (in_obp_range(prom_trans
[i
].virt
))
481 for (; i
< ents
; i
++) {
482 if (!in_obp_range(prom_trans
[i
].virt
))
487 for (i
= 0; i
< (last
- first
); i
++) {
488 struct linux_prom_translation
*src
= &prom_trans
[i
+ first
];
489 struct linux_prom_translation
*dest
= &prom_trans
[i
];
493 for (; i
< ents
; i
++) {
494 struct linux_prom_translation
*dest
= &prom_trans
[i
];
495 dest
->virt
= dest
->size
= dest
->data
= 0x0UL
;
498 prom_trans_ents
= last
- first
;
500 if (tlb_type
== spitfire
) {
501 /* Clear diag TTE bits. */
502 for (i
= 0; i
< prom_trans_ents
; i
++)
503 prom_trans
[i
].data
&= ~0x0003fe0000000000UL
;
507 static void __init
hypervisor_tlb_lock(unsigned long vaddr
,
511 register unsigned long func
asm("%o5");
512 register unsigned long arg0
asm("%o0");
513 register unsigned long arg1
asm("%o1");
514 register unsigned long arg2
asm("%o2");
515 register unsigned long arg3
asm("%o3");
517 func
= HV_FAST_MMU_MAP_PERM_ADDR
;
522 __asm__
__volatile__("ta 0x80"
523 : "=&r" (func
), "=&r" (arg0
),
524 "=&r" (arg1
), "=&r" (arg2
),
526 : "0" (func
), "1" (arg0
), "2" (arg1
),
527 "3" (arg2
), "4" (arg3
));
529 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
530 "errors with %lx\n", vaddr
, 0, pte
, mmu
, arg0
);
535 static unsigned long kern_large_tte(unsigned long paddr
);
537 static void __init
remap_kernel(void)
539 unsigned long phys_page
, tte_vaddr
, tte_data
;
540 int tlb_ent
= sparc64_highest_locked_tlbent();
542 tte_vaddr
= (unsigned long) KERNBASE
;
543 phys_page
= (prom_boot_mapping_phys_low
>> 22UL) << 22UL;
544 tte_data
= kern_large_tte(phys_page
);
546 kern_locked_tte_data
= tte_data
;
548 /* Now lock us into the TLBs via Hypervisor or OBP. */
549 if (tlb_type
== hypervisor
) {
550 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_DMMU
);
551 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_IMMU
);
553 tte_vaddr
+= 0x400000;
554 tte_data
+= 0x400000;
555 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_DMMU
);
556 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_IMMU
);
559 prom_dtlb_load(tlb_ent
, tte_data
, tte_vaddr
);
560 prom_itlb_load(tlb_ent
, tte_data
, tte_vaddr
);
563 prom_dtlb_load(tlb_ent
,
565 tte_vaddr
+ 0x400000);
566 prom_itlb_load(tlb_ent
,
568 tte_vaddr
+ 0x400000);
570 sparc64_highest_unlocked_tlb_ent
= tlb_ent
- 1;
572 if (tlb_type
== cheetah_plus
) {
573 sparc64_kern_pri_context
= (CTX_CHEETAH_PLUS_CTX0
|
574 CTX_CHEETAH_PLUS_NUC
);
575 sparc64_kern_pri_nuc_bits
= CTX_CHEETAH_PLUS_NUC
;
576 sparc64_kern_sec_context
= CTX_CHEETAH_PLUS_CTX0
;
581 static void __init
inherit_prom_mappings(void)
583 read_obp_translations();
585 /* Now fixup OBP's idea about where we really are mapped. */
586 prom_printf("Remapping the kernel... ");
588 prom_printf("done.\n");
591 void prom_world(int enter
)
594 set_fs((mm_segment_t
) { get_thread_current_ds() });
596 __asm__
__volatile__("flushw");
599 #ifdef DCACHE_ALIASING_POSSIBLE
600 void __flush_dcache_range(unsigned long start
, unsigned long end
)
604 if (tlb_type
== spitfire
) {
607 for (va
= start
; va
< end
; va
+= 32) {
608 spitfire_put_dcache_tag(va
& 0x3fe0, 0x0);
612 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
615 for (va
= start
; va
< end
; va
+= 32)
616 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
620 "i" (ASI_DCACHE_INVALIDATE
));
623 #endif /* DCACHE_ALIASING_POSSIBLE */
625 /* Caller does TLB context flushing on local CPU if necessary.
626 * The caller also ensures that CTX_VALID(mm->context) is false.
628 * We must be careful about boundary cases so that we never
629 * let the user have CTX 0 (nucleus) or we ever use a CTX
630 * version of zero (and thus NO_CONTEXT would not be caught
631 * by version mis-match tests in mmu_context.h).
633 void get_new_mmu_context(struct mm_struct
*mm
)
635 unsigned long ctx
, new_ctx
;
636 unsigned long orig_pgsz_bits
;
639 spin_lock(&ctx_alloc_lock
);
640 orig_pgsz_bits
= (mm
->context
.sparc64_ctx_val
& CTX_PGSZ_MASK
);
641 ctx
= (tlb_context_cache
+ 1) & CTX_NR_MASK
;
642 new_ctx
= find_next_zero_bit(mmu_context_bmap
, 1 << CTX_NR_BITS
, ctx
);
643 if (new_ctx
>= (1 << CTX_NR_BITS
)) {
644 new_ctx
= find_next_zero_bit(mmu_context_bmap
, ctx
, 1);
645 if (new_ctx
>= ctx
) {
647 new_ctx
= (tlb_context_cache
& CTX_VERSION_MASK
) +
650 new_ctx
= CTX_FIRST_VERSION
;
652 /* Don't call memset, for 16 entries that's just
655 mmu_context_bmap
[0] = 3;
656 mmu_context_bmap
[1] = 0;
657 mmu_context_bmap
[2] = 0;
658 mmu_context_bmap
[3] = 0;
659 for (i
= 4; i
< CTX_BMAP_SLOTS
; i
+= 4) {
660 mmu_context_bmap
[i
+ 0] = 0;
661 mmu_context_bmap
[i
+ 1] = 0;
662 mmu_context_bmap
[i
+ 2] = 0;
663 mmu_context_bmap
[i
+ 3] = 0;
668 mmu_context_bmap
[new_ctx
>>6] |= (1UL << (new_ctx
& 63));
669 new_ctx
|= (tlb_context_cache
& CTX_VERSION_MASK
);
671 tlb_context_cache
= new_ctx
;
672 mm
->context
.sparc64_ctx_val
= new_ctx
| orig_pgsz_bits
;
673 spin_unlock(&ctx_alloc_lock
);
676 void sparc_ultra_dump_itlb(void)
680 if (tlb_type
== spitfire
) {
681 printk ("Contents of itlb: ");
682 for (slot
= 0; slot
< 14; slot
++) printk (" ");
683 printk ("%2x:%016lx,%016lx\n",
685 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
686 for (slot
= 1; slot
< 64; slot
+=3) {
687 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
689 spitfire_get_itlb_tag(slot
), spitfire_get_itlb_data(slot
),
691 spitfire_get_itlb_tag(slot
+1), spitfire_get_itlb_data(slot
+1),
693 spitfire_get_itlb_tag(slot
+2), spitfire_get_itlb_data(slot
+2));
695 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
696 printk ("Contents of itlb0:\n");
697 for (slot
= 0; slot
< 16; slot
+=2) {
698 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
700 cheetah_get_litlb_tag(slot
), cheetah_get_litlb_data(slot
),
702 cheetah_get_litlb_tag(slot
+1), cheetah_get_litlb_data(slot
+1));
704 printk ("Contents of itlb2:\n");
705 for (slot
= 0; slot
< 128; slot
+=2) {
706 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
708 cheetah_get_itlb_tag(slot
), cheetah_get_itlb_data(slot
),
710 cheetah_get_itlb_tag(slot
+1), cheetah_get_itlb_data(slot
+1));
715 void sparc_ultra_dump_dtlb(void)
719 if (tlb_type
== spitfire
) {
720 printk ("Contents of dtlb: ");
721 for (slot
= 0; slot
< 14; slot
++) printk (" ");
722 printk ("%2x:%016lx,%016lx\n", 0,
723 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
724 for (slot
= 1; slot
< 64; slot
+=3) {
725 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
727 spitfire_get_dtlb_tag(slot
), spitfire_get_dtlb_data(slot
),
729 spitfire_get_dtlb_tag(slot
+1), spitfire_get_dtlb_data(slot
+1),
731 spitfire_get_dtlb_tag(slot
+2), spitfire_get_dtlb_data(slot
+2));
733 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
734 printk ("Contents of dtlb0:\n");
735 for (slot
= 0; slot
< 16; slot
+=2) {
736 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
738 cheetah_get_ldtlb_tag(slot
), cheetah_get_ldtlb_data(slot
),
740 cheetah_get_ldtlb_tag(slot
+1), cheetah_get_ldtlb_data(slot
+1));
742 printk ("Contents of dtlb2:\n");
743 for (slot
= 0; slot
< 512; slot
+=2) {
744 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
746 cheetah_get_dtlb_tag(slot
, 2), cheetah_get_dtlb_data(slot
, 2),
748 cheetah_get_dtlb_tag(slot
+1, 2), cheetah_get_dtlb_data(slot
+1, 2));
750 if (tlb_type
== cheetah_plus
) {
751 printk ("Contents of dtlb3:\n");
752 for (slot
= 0; slot
< 512; slot
+=2) {
753 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
755 cheetah_get_dtlb_tag(slot
, 3), cheetah_get_dtlb_data(slot
, 3),
757 cheetah_get_dtlb_tag(slot
+1, 3), cheetah_get_dtlb_data(slot
+1, 3));
763 extern unsigned long cmdline_memory_size
;
765 unsigned long __init
bootmem_init(unsigned long *pages_avail
)
767 unsigned long bootmap_size
, start_pfn
, end_pfn
;
768 unsigned long end_of_phys_memory
= 0UL;
769 unsigned long bootmap_pfn
, bytes_avail
, size
;
772 #ifdef CONFIG_DEBUG_BOOTMEM
773 prom_printf("bootmem_init: Scan pavail, ");
777 for (i
= 0; i
< pavail_ents
; i
++) {
778 end_of_phys_memory
= pavail
[i
].phys_addr
+
780 bytes_avail
+= pavail
[i
].reg_size
;
781 if (cmdline_memory_size
) {
782 if (bytes_avail
> cmdline_memory_size
) {
783 unsigned long slack
= bytes_avail
- cmdline_memory_size
;
785 bytes_avail
-= slack
;
786 end_of_phys_memory
-= slack
;
788 pavail
[i
].reg_size
-= slack
;
789 if ((long)pavail
[i
].reg_size
<= 0L) {
790 pavail
[i
].phys_addr
= 0xdeadbeefUL
;
791 pavail
[i
].reg_size
= 0UL;
794 pavail
[i
+1].reg_size
= 0Ul;
795 pavail
[i
+1].phys_addr
= 0xdeadbeefUL
;
803 *pages_avail
= bytes_avail
>> PAGE_SHIFT
;
805 /* Start with page aligned address of last symbol in kernel
806 * image. The kernel is hard mapped below PAGE_OFFSET in a
807 * 4MB locked TLB translation.
809 start_pfn
= PAGE_ALIGN(kern_base
+ kern_size
) >> PAGE_SHIFT
;
811 bootmap_pfn
= start_pfn
;
813 end_pfn
= end_of_phys_memory
>> PAGE_SHIFT
;
815 #ifdef CONFIG_BLK_DEV_INITRD
816 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
817 if (sparc_ramdisk_image
|| sparc_ramdisk_image64
) {
818 unsigned long ramdisk_image
= sparc_ramdisk_image
?
819 sparc_ramdisk_image
: sparc_ramdisk_image64
;
820 if (ramdisk_image
>= (unsigned long)_end
- 2 * PAGE_SIZE
)
821 ramdisk_image
-= KERNBASE
;
822 initrd_start
= ramdisk_image
+ phys_base
;
823 initrd_end
= initrd_start
+ sparc_ramdisk_size
;
824 if (initrd_end
> end_of_phys_memory
) {
825 printk(KERN_CRIT
"initrd extends beyond end of memory "
826 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
827 initrd_end
, end_of_phys_memory
);
831 if (initrd_start
>= (start_pfn
<< PAGE_SHIFT
) &&
832 initrd_start
< (start_pfn
<< PAGE_SHIFT
) + 2 * PAGE_SIZE
)
833 bootmap_pfn
= PAGE_ALIGN (initrd_end
) >> PAGE_SHIFT
;
837 /* Initialize the boot-time allocator. */
838 max_pfn
= max_low_pfn
= end_pfn
;
839 min_low_pfn
= pfn_base
;
841 #ifdef CONFIG_DEBUG_BOOTMEM
842 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
843 min_low_pfn
, bootmap_pfn
, max_low_pfn
);
845 bootmap_size
= init_bootmem_node(NODE_DATA(0), bootmap_pfn
, pfn_base
, end_pfn
);
847 /* Now register the available physical memory with the
850 for (i
= 0; i
< pavail_ents
; i
++) {
851 #ifdef CONFIG_DEBUG_BOOTMEM
852 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
853 i
, pavail
[i
].phys_addr
, pavail
[i
].reg_size
);
855 free_bootmem(pavail
[i
].phys_addr
, pavail
[i
].reg_size
);
858 #ifdef CONFIG_BLK_DEV_INITRD
860 size
= initrd_end
- initrd_start
;
862 /* Resert the initrd image area. */
863 #ifdef CONFIG_DEBUG_BOOTMEM
864 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
865 initrd_start
, initrd_end
);
867 reserve_bootmem(initrd_start
, size
);
868 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
870 initrd_start
+= PAGE_OFFSET
;
871 initrd_end
+= PAGE_OFFSET
;
874 /* Reserve the kernel text/data/bss. */
875 #ifdef CONFIG_DEBUG_BOOTMEM
876 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base
, kern_size
);
878 reserve_bootmem(kern_base
, kern_size
);
879 *pages_avail
-= PAGE_ALIGN(kern_size
) >> PAGE_SHIFT
;
881 /* Reserve the bootmem map. We do not account for it
882 * in pages_avail because we will release that memory
883 * in free_all_bootmem.
886 #ifdef CONFIG_DEBUG_BOOTMEM
887 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
888 (bootmap_pfn
<< PAGE_SHIFT
), size
);
890 reserve_bootmem((bootmap_pfn
<< PAGE_SHIFT
), size
);
891 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
896 static struct linux_prom64_registers pall
[MAX_BANKS
] __initdata
;
897 static int pall_ents __initdata
;
899 #ifdef CONFIG_DEBUG_PAGEALLOC
900 static unsigned long kernel_map_range(unsigned long pstart
, unsigned long pend
, pgprot_t prot
)
902 unsigned long vstart
= PAGE_OFFSET
+ pstart
;
903 unsigned long vend
= PAGE_OFFSET
+ pend
;
904 unsigned long alloc_bytes
= 0UL;
906 if ((vstart
& ~PAGE_MASK
) || (vend
& ~PAGE_MASK
)) {
907 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
912 while (vstart
< vend
) {
913 unsigned long this_end
, paddr
= __pa(vstart
);
914 pgd_t
*pgd
= pgd_offset_k(vstart
);
919 pud
= pud_offset(pgd
, vstart
);
920 if (pud_none(*pud
)) {
923 new = __alloc_bootmem(PAGE_SIZE
, PAGE_SIZE
, PAGE_SIZE
);
924 alloc_bytes
+= PAGE_SIZE
;
925 pud_populate(&init_mm
, pud
, new);
928 pmd
= pmd_offset(pud
, vstart
);
929 if (!pmd_present(*pmd
)) {
932 new = __alloc_bootmem(PAGE_SIZE
, PAGE_SIZE
, PAGE_SIZE
);
933 alloc_bytes
+= PAGE_SIZE
;
934 pmd_populate_kernel(&init_mm
, pmd
, new);
937 pte
= pte_offset_kernel(pmd
, vstart
);
938 this_end
= (vstart
+ PMD_SIZE
) & PMD_MASK
;
942 while (vstart
< this_end
) {
943 pte_val(*pte
) = (paddr
| pgprot_val(prot
));
954 extern unsigned int kvmap_linear_patch
[1];
955 #endif /* CONFIG_DEBUG_PAGEALLOC */
957 static void __init
mark_kpte_bitmap(unsigned long start
, unsigned long end
)
959 const unsigned long shift_256MB
= 28;
960 const unsigned long mask_256MB
= ((1UL << shift_256MB
) - 1UL);
961 const unsigned long size_256MB
= (1UL << shift_256MB
);
963 while (start
< end
) {
966 if (start
& mask_256MB
) {
967 start
= (start
+ size_256MB
) & ~mask_256MB
;
971 remains
= end
- start
;
972 while (remains
>= size_256MB
) {
973 unsigned long index
= start
>> shift_256MB
;
975 __set_bit(index
, kpte_linear_bitmap
);
978 remains
-= size_256MB
;
983 static void __init
kernel_physical_mapping_init(void)
986 #ifdef CONFIG_DEBUG_PAGEALLOC
987 unsigned long mem_alloced
= 0UL;
990 read_obp_memory("reg", &pall
[0], &pall_ents
);
992 for (i
= 0; i
< pall_ents
; i
++) {
993 unsigned long phys_start
, phys_end
;
995 phys_start
= pall
[i
].phys_addr
;
996 phys_end
= phys_start
+ pall
[i
].reg_size
;
998 mark_kpte_bitmap(phys_start
, phys_end
);
1000 #ifdef CONFIG_DEBUG_PAGEALLOC
1001 mem_alloced
+= kernel_map_range(phys_start
, phys_end
,
1006 #ifdef CONFIG_DEBUG_PAGEALLOC
1007 printk("Allocated %ld bytes for kernel page tables.\n",
1010 kvmap_linear_patch
[0] = 0x01000000; /* nop */
1011 flushi(&kvmap_linear_patch
[0]);
1017 #ifdef CONFIG_DEBUG_PAGEALLOC
1018 void kernel_map_pages(struct page
*page
, int numpages
, int enable
)
1020 unsigned long phys_start
= page_to_pfn(page
) << PAGE_SHIFT
;
1021 unsigned long phys_end
= phys_start
+ (numpages
* PAGE_SIZE
);
1023 kernel_map_range(phys_start
, phys_end
,
1024 (enable
? PAGE_KERNEL
: __pgprot(0)));
1026 flush_tsb_kernel_range(PAGE_OFFSET
+ phys_start
,
1027 PAGE_OFFSET
+ phys_end
);
1029 /* we should perform an IPI and flush all tlbs,
1030 * but that can deadlock->flush only current cpu.
1032 __flush_tlb_kernel_range(PAGE_OFFSET
+ phys_start
,
1033 PAGE_OFFSET
+ phys_end
);
1037 unsigned long __init
find_ecache_flush_span(unsigned long size
)
1041 for (i
= 0; i
< pavail_ents
; i
++) {
1042 if (pavail
[i
].reg_size
>= size
)
1043 return pavail
[i
].phys_addr
;
1049 static void __init
tsb_phys_patch(void)
1051 struct tsb_ldquad_phys_patch_entry
*pquad
;
1052 struct tsb_phys_patch_entry
*p
;
1054 pquad
= &__tsb_ldquad_phys_patch
;
1055 while (pquad
< &__tsb_ldquad_phys_patch_end
) {
1056 unsigned long addr
= pquad
->addr
;
1058 if (tlb_type
== hypervisor
)
1059 *(unsigned int *) addr
= pquad
->sun4v_insn
;
1061 *(unsigned int *) addr
= pquad
->sun4u_insn
;
1063 __asm__
__volatile__("flush %0"
1070 p
= &__tsb_phys_patch
;
1071 while (p
< &__tsb_phys_patch_end
) {
1072 unsigned long addr
= p
->addr
;
1074 *(unsigned int *) addr
= p
->insn
;
1076 __asm__
__volatile__("flush %0"
1084 /* Don't mark as init, we give this to the Hypervisor. */
1085 static struct hv_tsb_descr ktsb_descr
[2];
1086 extern struct tsb swapper_tsb
[KERNEL_TSB_NENTRIES
];
1088 static void __init
sun4v_ktsb_init(void)
1090 unsigned long ktsb_pa
;
1092 /* First KTSB for PAGE_SIZE mappings. */
1093 ktsb_pa
= kern_base
+ ((unsigned long)&swapper_tsb
[0] - KERNBASE
);
1095 switch (PAGE_SIZE
) {
1098 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_8K
;
1099 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_8K
;
1103 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_64K
;
1104 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_64K
;
1108 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_512K
;
1109 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_512K
;
1112 case 4 * 1024 * 1024:
1113 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_4MB
;
1114 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_4MB
;
1118 ktsb_descr
[0].assoc
= 1;
1119 ktsb_descr
[0].num_ttes
= KERNEL_TSB_NENTRIES
;
1120 ktsb_descr
[0].ctx_idx
= 0;
1121 ktsb_descr
[0].tsb_base
= ktsb_pa
;
1122 ktsb_descr
[0].resv
= 0;
1124 /* Second KTSB for 4MB/256MB mappings. */
1125 ktsb_pa
= (kern_base
+
1126 ((unsigned long)&swapper_4m_tsb
[0] - KERNBASE
));
1128 ktsb_descr
[1].pgsz_idx
= HV_PGSZ_IDX_4MB
;
1129 ktsb_descr
[1].pgsz_mask
= (HV_PGSZ_MASK_4MB
|
1130 HV_PGSZ_MASK_256MB
);
1131 ktsb_descr
[1].assoc
= 1;
1132 ktsb_descr
[1].num_ttes
= KERNEL_TSB4M_NENTRIES
;
1133 ktsb_descr
[1].ctx_idx
= 0;
1134 ktsb_descr
[1].tsb_base
= ktsb_pa
;
1135 ktsb_descr
[1].resv
= 0;
1138 void __cpuinit
sun4v_ktsb_register(void)
1140 register unsigned long func
asm("%o5");
1141 register unsigned long arg0
asm("%o0");
1142 register unsigned long arg1
asm("%o1");
1145 pa
= kern_base
+ ((unsigned long)&ktsb_descr
[0] - KERNBASE
);
1147 func
= HV_FAST_MMU_TSB_CTX0
;
1150 __asm__
__volatile__("ta %6"
1151 : "=&r" (func
), "=&r" (arg0
), "=&r" (arg1
)
1152 : "0" (func
), "1" (arg0
), "2" (arg1
),
1153 "i" (HV_FAST_TRAP
));
1156 /* paging_init() sets up the page tables */
1158 extern void cheetah_ecache_flush_init(void);
1159 extern void sun4v_patch_tlb_handlers(void);
1161 static unsigned long last_valid_pfn
;
1162 pgd_t swapper_pg_dir
[2048];
1164 static void sun4u_pgprot_init(void);
1165 static void sun4v_pgprot_init(void);
1167 void __init
paging_init(void)
1169 unsigned long end_pfn
, pages_avail
, shift
;
1170 unsigned long real_end
, i
;
1172 kern_base
= (prom_boot_mapping_phys_low
>> 22UL) << 22UL;
1173 kern_size
= (unsigned long)&_end
- (unsigned long)KERNBASE
;
1175 /* Invalidate both kernel TSBs. */
1176 memset(swapper_tsb
, 0x40, sizeof(swapper_tsb
));
1177 memset(swapper_4m_tsb
, 0x40, sizeof(swapper_4m_tsb
));
1179 if (tlb_type
== hypervisor
)
1180 sun4v_pgprot_init();
1182 sun4u_pgprot_init();
1184 if (tlb_type
== cheetah_plus
||
1185 tlb_type
== hypervisor
)
1188 if (tlb_type
== hypervisor
) {
1189 sun4v_patch_tlb_handlers();
1193 /* Find available physical memory... */
1194 read_obp_memory("available", &pavail
[0], &pavail_ents
);
1196 phys_base
= 0xffffffffffffffffUL
;
1197 for (i
= 0; i
< pavail_ents
; i
++)
1198 phys_base
= min(phys_base
, pavail
[i
].phys_addr
);
1200 pfn_base
= phys_base
>> PAGE_SHIFT
;
1202 set_bit(0, mmu_context_bmap
);
1204 shift
= kern_base
+ PAGE_OFFSET
- ((unsigned long)KERNBASE
);
1206 real_end
= (unsigned long)_end
;
1207 if ((real_end
> ((unsigned long)KERNBASE
+ 0x400000)))
1209 if ((real_end
> ((unsigned long)KERNBASE
+ 0x800000))) {
1210 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1214 /* Set kernel pgd to upper alias so physical page computations
1217 init_mm
.pgd
+= ((shift
) / (sizeof(pgd_t
)));
1219 memset(swapper_low_pmd_dir
, 0, sizeof(swapper_low_pmd_dir
));
1221 /* Now can init the kernel/bad page tables. */
1222 pud_set(pud_offset(&swapper_pg_dir
[0], 0),
1223 swapper_low_pmd_dir
+ (shift
/ sizeof(pgd_t
)));
1225 inherit_prom_mappings();
1227 /* Ok, we can use our TLB miss and window trap handlers safely. */
1232 if (tlb_type
== hypervisor
)
1233 sun4v_ktsb_register();
1235 /* Setup bootmem... */
1237 last_valid_pfn
= end_pfn
= bootmem_init(&pages_avail
);
1239 kernel_physical_mapping_init();
1242 unsigned long zones_size
[MAX_NR_ZONES
];
1243 unsigned long zholes_size
[MAX_NR_ZONES
];
1244 unsigned long npages
;
1247 for (znum
= 0; znum
< MAX_NR_ZONES
; znum
++)
1248 zones_size
[znum
] = zholes_size
[znum
] = 0;
1250 npages
= end_pfn
- pfn_base
;
1251 zones_size
[ZONE_DMA
] = npages
;
1252 zholes_size
[ZONE_DMA
] = npages
- pages_avail
;
1254 free_area_init_node(0, &contig_page_data
, zones_size
,
1255 phys_base
>> PAGE_SHIFT
, zholes_size
);
1261 static void __init
taint_real_pages(void)
1265 read_obp_memory("available", &pavail_rescan
[0], &pavail_rescan_ents
);
1267 /* Find changes discovered in the physmem available rescan and
1268 * reserve the lost portions in the bootmem maps.
1270 for (i
= 0; i
< pavail_ents
; i
++) {
1271 unsigned long old_start
, old_end
;
1273 old_start
= pavail
[i
].phys_addr
;
1274 old_end
= old_start
+
1276 while (old_start
< old_end
) {
1279 for (n
= 0; pavail_rescan_ents
; n
++) {
1280 unsigned long new_start
, new_end
;
1282 new_start
= pavail_rescan
[n
].phys_addr
;
1283 new_end
= new_start
+
1284 pavail_rescan
[n
].reg_size
;
1286 if (new_start
<= old_start
&&
1287 new_end
>= (old_start
+ PAGE_SIZE
)) {
1288 set_bit(old_start
>> 22,
1289 sparc64_valid_addr_bitmap
);
1293 reserve_bootmem(old_start
, PAGE_SIZE
);
1296 old_start
+= PAGE_SIZE
;
1301 void __init
mem_init(void)
1303 unsigned long codepages
, datapages
, initpages
;
1304 unsigned long addr
, last
;
1307 i
= last_valid_pfn
>> ((22 - PAGE_SHIFT
) + 6);
1309 sparc64_valid_addr_bitmap
= (unsigned long *) alloc_bootmem(i
<< 3);
1310 if (sparc64_valid_addr_bitmap
== NULL
) {
1311 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1314 memset(sparc64_valid_addr_bitmap
, 0, i
<< 3);
1316 addr
= PAGE_OFFSET
+ kern_base
;
1317 last
= PAGE_ALIGN(kern_size
) + addr
;
1318 while (addr
< last
) {
1319 set_bit(__pa(addr
) >> 22, sparc64_valid_addr_bitmap
);
1325 max_mapnr
= last_valid_pfn
- pfn_base
;
1326 high_memory
= __va(last_valid_pfn
<< PAGE_SHIFT
);
1328 #ifdef CONFIG_DEBUG_BOOTMEM
1329 prom_printf("mem_init: Calling free_all_bootmem().\n");
1331 totalram_pages
= num_physpages
= free_all_bootmem() - 1;
1334 * Set up the zero page, mark it reserved, so that page count
1335 * is not manipulated when freeing the page from user ptes.
1337 mem_map_zero
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
1338 if (mem_map_zero
== NULL
) {
1339 prom_printf("paging_init: Cannot alloc zero page.\n");
1342 SetPageReserved(mem_map_zero
);
1344 codepages
= (((unsigned long) _etext
) - ((unsigned long) _start
));
1345 codepages
= PAGE_ALIGN(codepages
) >> PAGE_SHIFT
;
1346 datapages
= (((unsigned long) _edata
) - ((unsigned long) _etext
));
1347 datapages
= PAGE_ALIGN(datapages
) >> PAGE_SHIFT
;
1348 initpages
= (((unsigned long) __init_end
) - ((unsigned long) __init_begin
));
1349 initpages
= PAGE_ALIGN(initpages
) >> PAGE_SHIFT
;
1351 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1352 nr_free_pages() << (PAGE_SHIFT
-10),
1353 codepages
<< (PAGE_SHIFT
-10),
1354 datapages
<< (PAGE_SHIFT
-10),
1355 initpages
<< (PAGE_SHIFT
-10),
1356 PAGE_OFFSET
, (last_valid_pfn
<< PAGE_SHIFT
));
1358 if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
1359 cheetah_ecache_flush_init();
1362 void free_initmem(void)
1364 unsigned long addr
, initend
;
1367 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1369 addr
= PAGE_ALIGN((unsigned long)(__init_begin
));
1370 initend
= (unsigned long)(__init_end
) & PAGE_MASK
;
1371 for (; addr
< initend
; addr
+= PAGE_SIZE
) {
1376 ((unsigned long) __va(kern_base
)) -
1377 ((unsigned long) KERNBASE
));
1378 memset((void *)addr
, 0xcc, PAGE_SIZE
);
1379 p
= virt_to_page(page
);
1381 ClearPageReserved(p
);
1382 set_page_count(p
, 1);
1389 #ifdef CONFIG_BLK_DEV_INITRD
1390 void free_initrd_mem(unsigned long start
, unsigned long end
)
1393 printk ("Freeing initrd memory: %ldk freed\n", (end
- start
) >> 10);
1394 for (; start
< end
; start
+= PAGE_SIZE
) {
1395 struct page
*p
= virt_to_page(start
);
1397 ClearPageReserved(p
);
1398 set_page_count(p
, 1);
1406 #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1407 #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1408 #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1409 #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1410 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1411 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1413 pgprot_t PAGE_KERNEL __read_mostly
;
1414 EXPORT_SYMBOL(PAGE_KERNEL
);
1416 pgprot_t PAGE_KERNEL_LOCKED __read_mostly
;
1417 pgprot_t PAGE_COPY __read_mostly
;
1419 pgprot_t PAGE_SHARED __read_mostly
;
1420 EXPORT_SYMBOL(PAGE_SHARED
);
1422 pgprot_t PAGE_EXEC __read_mostly
;
1423 unsigned long pg_iobits __read_mostly
;
1425 unsigned long _PAGE_IE __read_mostly
;
1427 unsigned long _PAGE_E __read_mostly
;
1428 EXPORT_SYMBOL(_PAGE_E
);
1430 unsigned long _PAGE_CACHE __read_mostly
;
1431 EXPORT_SYMBOL(_PAGE_CACHE
);
1433 static void prot_init_common(unsigned long page_none
,
1434 unsigned long page_shared
,
1435 unsigned long page_copy
,
1436 unsigned long page_readonly
,
1437 unsigned long page_exec_bit
)
1439 PAGE_COPY
= __pgprot(page_copy
);
1440 PAGE_SHARED
= __pgprot(page_shared
);
1442 protection_map
[0x0] = __pgprot(page_none
);
1443 protection_map
[0x1] = __pgprot(page_readonly
& ~page_exec_bit
);
1444 protection_map
[0x2] = __pgprot(page_copy
& ~page_exec_bit
);
1445 protection_map
[0x3] = __pgprot(page_copy
& ~page_exec_bit
);
1446 protection_map
[0x4] = __pgprot(page_readonly
);
1447 protection_map
[0x5] = __pgprot(page_readonly
);
1448 protection_map
[0x6] = __pgprot(page_copy
);
1449 protection_map
[0x7] = __pgprot(page_copy
);
1450 protection_map
[0x8] = __pgprot(page_none
);
1451 protection_map
[0x9] = __pgprot(page_readonly
& ~page_exec_bit
);
1452 protection_map
[0xa] = __pgprot(page_shared
& ~page_exec_bit
);
1453 protection_map
[0xb] = __pgprot(page_shared
& ~page_exec_bit
);
1454 protection_map
[0xc] = __pgprot(page_readonly
);
1455 protection_map
[0xd] = __pgprot(page_readonly
);
1456 protection_map
[0xe] = __pgprot(page_shared
);
1457 protection_map
[0xf] = __pgprot(page_shared
);
1460 static void __init
sun4u_pgprot_init(void)
1462 unsigned long page_none
, page_shared
, page_copy
, page_readonly
;
1463 unsigned long page_exec_bit
;
1465 PAGE_KERNEL
= __pgprot (_PAGE_PRESENT_4U
| _PAGE_VALID
|
1466 _PAGE_CACHE_4U
| _PAGE_P_4U
|
1467 __ACCESS_BITS_4U
| __DIRTY_BITS_4U
|
1469 PAGE_KERNEL_LOCKED
= __pgprot (_PAGE_PRESENT_4U
| _PAGE_VALID
|
1470 _PAGE_CACHE_4U
| _PAGE_P_4U
|
1471 __ACCESS_BITS_4U
| __DIRTY_BITS_4U
|
1472 _PAGE_EXEC_4U
| _PAGE_L_4U
);
1473 PAGE_EXEC
= __pgprot(_PAGE_EXEC_4U
);
1475 _PAGE_IE
= _PAGE_IE_4U
;
1476 _PAGE_E
= _PAGE_E_4U
;
1477 _PAGE_CACHE
= _PAGE_CACHE_4U
;
1479 pg_iobits
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| __DIRTY_BITS_4U
|
1480 __ACCESS_BITS_4U
| _PAGE_E_4U
);
1482 kern_linear_pte_xor
[0] = (_PAGE_VALID
| _PAGE_SZ4MB_4U
) ^
1484 kern_linear_pte_xor
[0] |= (_PAGE_CP_4U
| _PAGE_CV_4U
|
1485 _PAGE_P_4U
| _PAGE_W_4U
);
1487 /* XXX Should use 256MB on Panther. XXX */
1488 kern_linear_pte_xor
[1] = kern_linear_pte_xor
[0];
1490 _PAGE_SZBITS
= _PAGE_SZBITS_4U
;
1491 _PAGE_ALL_SZ_BITS
= (_PAGE_SZ4MB_4U
| _PAGE_SZ512K_4U
|
1492 _PAGE_SZ64K_4U
| _PAGE_SZ8K_4U
|
1493 _PAGE_SZ32MB_4U
| _PAGE_SZ256MB_4U
);
1496 page_none
= _PAGE_PRESENT_4U
| _PAGE_ACCESSED_4U
| _PAGE_CACHE_4U
;
1497 page_shared
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| _PAGE_CACHE_4U
|
1498 __ACCESS_BITS_4U
| _PAGE_WRITE_4U
| _PAGE_EXEC_4U
);
1499 page_copy
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| _PAGE_CACHE_4U
|
1500 __ACCESS_BITS_4U
| _PAGE_EXEC_4U
);
1501 page_readonly
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| _PAGE_CACHE_4U
|
1502 __ACCESS_BITS_4U
| _PAGE_EXEC_4U
);
1504 page_exec_bit
= _PAGE_EXEC_4U
;
1506 prot_init_common(page_none
, page_shared
, page_copy
, page_readonly
,
1510 static void __init
sun4v_pgprot_init(void)
1512 unsigned long page_none
, page_shared
, page_copy
, page_readonly
;
1513 unsigned long page_exec_bit
;
1515 PAGE_KERNEL
= __pgprot (_PAGE_PRESENT_4V
| _PAGE_VALID
|
1516 _PAGE_CACHE_4V
| _PAGE_P_4V
|
1517 __ACCESS_BITS_4V
| __DIRTY_BITS_4V
|
1519 PAGE_KERNEL_LOCKED
= PAGE_KERNEL
;
1520 PAGE_EXEC
= __pgprot(_PAGE_EXEC_4V
);
1522 _PAGE_IE
= _PAGE_IE_4V
;
1523 _PAGE_E
= _PAGE_E_4V
;
1524 _PAGE_CACHE
= _PAGE_CACHE_4V
;
1526 kern_linear_pte_xor
[0] = (_PAGE_VALID
| _PAGE_SZ4MB_4V
) ^
1528 kern_linear_pte_xor
[0] |= (_PAGE_CP_4V
| _PAGE_CV_4V
|
1529 _PAGE_P_4V
| _PAGE_W_4V
);
1531 kern_linear_pte_xor
[1] = (_PAGE_VALID
| _PAGE_SZ256MB_4V
) ^
1533 kern_linear_pte_xor
[1] |= (_PAGE_CP_4V
| _PAGE_CV_4V
|
1534 _PAGE_P_4V
| _PAGE_W_4V
);
1536 pg_iobits
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| __DIRTY_BITS_4V
|
1537 __ACCESS_BITS_4V
| _PAGE_E_4V
);
1539 _PAGE_SZBITS
= _PAGE_SZBITS_4V
;
1540 _PAGE_ALL_SZ_BITS
= (_PAGE_SZ16GB_4V
| _PAGE_SZ2GB_4V
|
1541 _PAGE_SZ256MB_4V
| _PAGE_SZ32MB_4V
|
1542 _PAGE_SZ4MB_4V
| _PAGE_SZ512K_4V
|
1543 _PAGE_SZ64K_4V
| _PAGE_SZ8K_4V
);
1545 page_none
= _PAGE_PRESENT_4V
| _PAGE_ACCESSED_4V
| _PAGE_CACHE_4V
;
1546 page_shared
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| _PAGE_CACHE_4V
|
1547 __ACCESS_BITS_4V
| _PAGE_WRITE_4V
| _PAGE_EXEC_4V
);
1548 page_copy
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| _PAGE_CACHE_4V
|
1549 __ACCESS_BITS_4V
| _PAGE_EXEC_4V
);
1550 page_readonly
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| _PAGE_CACHE_4V
|
1551 __ACCESS_BITS_4V
| _PAGE_EXEC_4V
);
1553 page_exec_bit
= _PAGE_EXEC_4V
;
1555 prot_init_common(page_none
, page_shared
, page_copy
, page_readonly
,
1559 unsigned long pte_sz_bits(unsigned long sz
)
1561 if (tlb_type
== hypervisor
) {
1565 return _PAGE_SZ8K_4V
;
1567 return _PAGE_SZ64K_4V
;
1569 return _PAGE_SZ512K_4V
;
1570 case 4 * 1024 * 1024:
1571 return _PAGE_SZ4MB_4V
;
1577 return _PAGE_SZ8K_4U
;
1579 return _PAGE_SZ64K_4U
;
1581 return _PAGE_SZ512K_4U
;
1582 case 4 * 1024 * 1024:
1583 return _PAGE_SZ4MB_4U
;
1588 pte_t
mk_pte_io(unsigned long page
, pgprot_t prot
, int space
, unsigned long page_size
)
1592 pte_val(pte
) = page
| pgprot_val(pgprot_noncached(prot
));
1593 pte_val(pte
) |= (((unsigned long)space
) << 32);
1594 pte_val(pte
) |= pte_sz_bits(page_size
);
1599 static unsigned long kern_large_tte(unsigned long paddr
)
1603 val
= (_PAGE_VALID
| _PAGE_SZ4MB_4U
|
1604 _PAGE_CP_4U
| _PAGE_CV_4U
| _PAGE_P_4U
|
1605 _PAGE_EXEC_4U
| _PAGE_L_4U
| _PAGE_W_4U
);
1606 if (tlb_type
== hypervisor
)
1607 val
= (_PAGE_VALID
| _PAGE_SZ4MB_4V
|
1608 _PAGE_CP_4V
| _PAGE_CV_4V
| _PAGE_P_4V
|
1609 _PAGE_EXEC_4V
| _PAGE_W_4V
);
1615 * Translate PROM's mapping we capture at boot time into physical address.
1616 * The second parameter is only set from prom_callback() invocations.
1618 unsigned long prom_virt_to_phys(unsigned long promva
, int *error
)
1623 mask
= _PAGE_PADDR_4U
;
1624 if (tlb_type
== hypervisor
)
1625 mask
= _PAGE_PADDR_4V
;
1627 for (i
= 0; i
< prom_trans_ents
; i
++) {
1628 struct linux_prom_translation
*p
= &prom_trans
[i
];
1630 if (promva
>= p
->virt
&&
1631 promva
< (p
->virt
+ p
->size
)) {
1632 unsigned long base
= p
->data
& mask
;
1636 return base
+ (promva
& (8192 - 1));
1644 /* XXX We should kill off this ugly thing at so me point. XXX */
1645 unsigned long sun4u_get_pte(unsigned long addr
)
1651 unsigned long mask
= _PAGE_PADDR_4U
;
1653 if (tlb_type
== hypervisor
)
1654 mask
= _PAGE_PADDR_4V
;
1656 if (addr
>= PAGE_OFFSET
)
1659 if ((addr
>= LOW_OBP_ADDRESS
) && (addr
< HI_OBP_ADDRESS
))
1660 return prom_virt_to_phys(addr
, NULL
);
1662 pgdp
= pgd_offset_k(addr
);
1663 pudp
= pud_offset(pgdp
, addr
);
1664 pmdp
= pmd_offset(pudp
, addr
);
1665 ptep
= pte_offset_kernel(pmdp
, addr
);
1667 return pte_val(*ptep
) & mask
;
1670 /* If not locked, zap it. */
1671 void __flush_tlb_all(void)
1673 unsigned long pstate
;
1676 __asm__
__volatile__("flushw\n\t"
1677 "rdpr %%pstate, %0\n\t"
1678 "wrpr %0, %1, %%pstate"
1681 if (tlb_type
== spitfire
) {
1682 for (i
= 0; i
< 64; i
++) {
1683 /* Spitfire Errata #32 workaround */
1684 /* NOTE: Always runs on spitfire, so no
1685 * cheetah+ page size encodings.
1687 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1691 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1693 if (!(spitfire_get_dtlb_data(i
) & _PAGE_L_4U
)) {
1694 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1697 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
1698 spitfire_put_dtlb_data(i
, 0x0UL
);
1701 /* Spitfire Errata #32 workaround */
1702 /* NOTE: Always runs on spitfire, so no
1703 * cheetah+ page size encodings.
1705 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1709 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1711 if (!(spitfire_get_itlb_data(i
) & _PAGE_L_4U
)) {
1712 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1715 : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
1716 spitfire_put_itlb_data(i
, 0x0UL
);
1719 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1720 cheetah_flush_dtlb_all();
1721 cheetah_flush_itlb_all();
1723 __asm__
__volatile__("wrpr %0, 0, %%pstate"