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 /* Kernel physical address base and size in bytes. */
134 unsigned long kern_base __read_mostly
;
135 unsigned long kern_size __read_mostly
;
137 /* get_new_mmu_context() uses "cache + 1". */
138 DEFINE_SPINLOCK(ctx_alloc_lock
);
139 unsigned long tlb_context_cache
= CTX_FIRST_VERSION
- 1;
140 #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
141 unsigned long mmu_context_bmap
[CTX_BMAP_SLOTS
];
143 /* References to special section boundaries */
144 extern char _start
[], _end
[];
146 /* Initial ramdisk setup */
147 extern unsigned long sparc_ramdisk_image64
;
148 extern unsigned int sparc_ramdisk_image
;
149 extern unsigned int sparc_ramdisk_size
;
151 struct page
*mem_map_zero __read_mostly
;
153 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly
;
155 unsigned long sparc64_kern_pri_context __read_mostly
;
156 unsigned long sparc64_kern_pri_nuc_bits __read_mostly
;
157 unsigned long sparc64_kern_sec_context __read_mostly
;
161 kmem_cache_t
*pgtable_cache __read_mostly
;
163 static void zero_ctor(void *addr
, kmem_cache_t
*cache
, unsigned long flags
)
168 extern void tsb_cache_init(void);
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("Could not create pgtable_cache\n");
185 #ifdef CONFIG_DEBUG_DCFLUSH
186 atomic_t dcpage_flushes
= ATOMIC_INIT(0);
188 atomic_t dcpage_flushes_xcall
= ATOMIC_INIT(0);
192 inline void flush_dcache_page_impl(struct page
*page
)
194 BUG_ON(tlb_type
== hypervisor
);
195 #ifdef CONFIG_DEBUG_DCFLUSH
196 atomic_inc(&dcpage_flushes
);
199 #ifdef DCACHE_ALIASING_POSSIBLE
200 __flush_dcache_page(page_address(page
),
201 ((tlb_type
== spitfire
) &&
202 page_mapping(page
) != NULL
));
204 if (page_mapping(page
) != NULL
&&
205 tlb_type
== spitfire
)
206 __flush_icache_page(__pa(page_address(page
)));
210 #define PG_dcache_dirty PG_arch_1
211 #define PG_dcache_cpu_shift 24UL
212 #define PG_dcache_cpu_mask (256UL - 1UL)
215 #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
218 #define dcache_dirty_cpu(page) \
219 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
221 static __inline__
void set_dcache_dirty(struct page
*page
, int this_cpu
)
223 unsigned long mask
= this_cpu
;
224 unsigned long non_cpu_bits
;
226 non_cpu_bits
= ~(PG_dcache_cpu_mask
<< PG_dcache_cpu_shift
);
227 mask
= (mask
<< PG_dcache_cpu_shift
) | (1UL << PG_dcache_dirty
);
229 __asm__
__volatile__("1:\n\t"
231 "and %%g7, %1, %%g1\n\t"
232 "or %%g1, %0, %%g1\n\t"
233 "casx [%2], %%g7, %%g1\n\t"
235 "membar #StoreLoad | #StoreStore\n\t"
236 "bne,pn %%xcc, 1b\n\t"
239 : "r" (mask
), "r" (non_cpu_bits
), "r" (&page
->flags
)
243 static __inline__
void clear_dcache_dirty_cpu(struct page
*page
, unsigned long cpu
)
245 unsigned long mask
= (1UL << PG_dcache_dirty
);
247 __asm__
__volatile__("! test_and_clear_dcache_dirty\n"
250 "srlx %%g7, %4, %%g1\n\t"
251 "and %%g1, %3, %%g1\n\t"
253 "bne,pn %%icc, 2f\n\t"
254 " andn %%g7, %1, %%g1\n\t"
255 "casx [%2], %%g7, %%g1\n\t"
257 "membar #StoreLoad | #StoreStore\n\t"
258 "bne,pn %%xcc, 1b\n\t"
262 : "r" (cpu
), "r" (mask
), "r" (&page
->flags
),
263 "i" (PG_dcache_cpu_mask
),
264 "i" (PG_dcache_cpu_shift
)
268 static inline void tsb_insert(struct tsb
*ent
, unsigned long tag
, unsigned long pte
)
270 unsigned long tsb_addr
= (unsigned long) ent
;
272 if (tlb_type
== cheetah_plus
|| tlb_type
== hypervisor
)
273 tsb_addr
= __pa(tsb_addr
);
275 __tsb_insert(tsb_addr
, tag
, pte
);
278 unsigned long _PAGE_ALL_SZ_BITS __read_mostly
;
279 unsigned long _PAGE_SZBITS __read_mostly
;
281 void update_mmu_cache(struct vm_area_struct
*vma
, unsigned long address
, pte_t pte
)
283 struct mm_struct
*mm
;
285 unsigned long tag
, flags
;
286 unsigned long tsb_index
, tsb_hash_shift
;
288 if (tlb_type
!= hypervisor
) {
289 unsigned long pfn
= pte_pfn(pte
);
290 unsigned long pg_flags
;
293 if (pfn_valid(pfn
) &&
294 (page
= pfn_to_page(pfn
), page_mapping(page
)) &&
295 ((pg_flags
= page
->flags
) & (1UL << PG_dcache_dirty
))) {
296 int cpu
= ((pg_flags
>> PG_dcache_cpu_shift
) &
298 int this_cpu
= get_cpu();
300 /* This is just to optimize away some function calls
304 flush_dcache_page_impl(page
);
306 smp_flush_dcache_page_impl(page
, cpu
);
308 clear_dcache_dirty_cpu(page
, cpu
);
316 tsb_index
= MM_TSB_BASE
;
317 tsb_hash_shift
= PAGE_SHIFT
;
319 spin_lock_irqsave(&mm
->context
.lock
, flags
);
321 #ifdef CONFIG_HUGETLB_PAGE
322 if (mm
->context
.tsb_block
[MM_TSB_HUGE
].tsb
!= NULL
) {
323 if ((tlb_type
== hypervisor
&&
324 (pte_val(pte
) & _PAGE_SZALL_4V
) == _PAGE_SZHUGE_4V
) ||
325 (tlb_type
!= hypervisor
&&
326 (pte_val(pte
) & _PAGE_SZALL_4U
) == _PAGE_SZHUGE_4U
)) {
327 tsb_index
= MM_TSB_HUGE
;
328 tsb_hash_shift
= HPAGE_SHIFT
;
333 tsb
= mm
->context
.tsb_block
[tsb_index
].tsb
;
334 tsb
+= ((address
>> tsb_hash_shift
) &
335 (mm
->context
.tsb_block
[tsb_index
].tsb_nentries
- 1UL));
336 tag
= (address
>> 22UL);
337 tsb_insert(tsb
, tag
, pte_val(pte
));
339 spin_unlock_irqrestore(&mm
->context
.lock
, flags
);
342 void flush_dcache_page(struct page
*page
)
344 struct address_space
*mapping
;
347 if (tlb_type
== hypervisor
)
350 /* Do not bother with the expensive D-cache flush if it
351 * is merely the zero page. The 'bigcore' testcase in GDB
352 * causes this case to run millions of times.
354 if (page
== ZERO_PAGE(0))
357 this_cpu
= get_cpu();
359 mapping
= page_mapping(page
);
360 if (mapping
&& !mapping_mapped(mapping
)) {
361 int dirty
= test_bit(PG_dcache_dirty
, &page
->flags
);
363 int dirty_cpu
= dcache_dirty_cpu(page
);
365 if (dirty_cpu
== this_cpu
)
367 smp_flush_dcache_page_impl(page
, dirty_cpu
);
369 set_dcache_dirty(page
, this_cpu
);
371 /* We could delay the flush for the !page_mapping
372 * case too. But that case is for exec env/arg
373 * pages and those are %99 certainly going to get
374 * faulted into the tlb (and thus flushed) anyways.
376 flush_dcache_page_impl(page
);
383 void __kprobes
flush_icache_range(unsigned long start
, unsigned long end
)
385 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
386 if (tlb_type
== spitfire
) {
389 for (kaddr
= start
; kaddr
< end
; kaddr
+= PAGE_SIZE
)
390 __flush_icache_page(__get_phys(kaddr
));
396 printk("Mem-info:\n");
398 printk("Free swap: %6ldkB\n",
399 nr_swap_pages
<< (PAGE_SHIFT
-10));
400 printk("%ld pages of RAM\n", num_physpages
);
401 printk("%d free pages\n", nr_free_pages());
404 void mmu_info(struct seq_file
*m
)
406 if (tlb_type
== cheetah
)
407 seq_printf(m
, "MMU Type\t: Cheetah\n");
408 else if (tlb_type
== cheetah_plus
)
409 seq_printf(m
, "MMU Type\t: Cheetah+\n");
410 else if (tlb_type
== spitfire
)
411 seq_printf(m
, "MMU Type\t: Spitfire\n");
412 else if (tlb_type
== hypervisor
)
413 seq_printf(m
, "MMU Type\t: Hypervisor (sun4v)\n");
415 seq_printf(m
, "MMU Type\t: ???\n");
417 #ifdef CONFIG_DEBUG_DCFLUSH
418 seq_printf(m
, "DCPageFlushes\t: %d\n",
419 atomic_read(&dcpage_flushes
));
421 seq_printf(m
, "DCPageFlushesXC\t: %d\n",
422 atomic_read(&dcpage_flushes_xcall
));
423 #endif /* CONFIG_SMP */
424 #endif /* CONFIG_DEBUG_DCFLUSH */
427 struct linux_prom_translation
{
433 /* Exported for kernel TLB miss handling in ktlb.S */
434 struct linux_prom_translation prom_trans
[512] __read_mostly
;
435 unsigned int prom_trans_ents __read_mostly
;
437 /* Exported for SMP bootup purposes. */
438 unsigned long kern_locked_tte_data
;
440 /* The obp translations are saved based on 8k pagesize, since obp can
441 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
442 * HI_OBP_ADDRESS range are handled in ktlb.S.
444 static inline int in_obp_range(unsigned long vaddr
)
446 return (vaddr
>= LOW_OBP_ADDRESS
&&
447 vaddr
< HI_OBP_ADDRESS
);
450 static int cmp_ptrans(const void *a
, const void *b
)
452 const struct linux_prom_translation
*x
= a
, *y
= b
;
454 if (x
->virt
> y
->virt
)
456 if (x
->virt
< y
->virt
)
461 /* Read OBP translations property into 'prom_trans[]'. */
462 static void __init
read_obp_translations(void)
464 int n
, node
, ents
, first
, last
, i
;
466 node
= prom_finddevice("/virtual-memory");
467 n
= prom_getproplen(node
, "translations");
468 if (unlikely(n
== 0 || n
== -1)) {
469 prom_printf("prom_mappings: Couldn't get size.\n");
472 if (unlikely(n
> sizeof(prom_trans
))) {
473 prom_printf("prom_mappings: Size %Zd is too big.\n", n
);
477 if ((n
= prom_getproperty(node
, "translations",
478 (char *)&prom_trans
[0],
479 sizeof(prom_trans
))) == -1) {
480 prom_printf("prom_mappings: Couldn't get property.\n");
484 n
= n
/ sizeof(struct linux_prom_translation
);
488 sort(prom_trans
, ents
, sizeof(struct linux_prom_translation
),
491 /* Now kick out all the non-OBP entries. */
492 for (i
= 0; i
< ents
; i
++) {
493 if (in_obp_range(prom_trans
[i
].virt
))
497 for (; i
< ents
; i
++) {
498 if (!in_obp_range(prom_trans
[i
].virt
))
503 for (i
= 0; i
< (last
- first
); i
++) {
504 struct linux_prom_translation
*src
= &prom_trans
[i
+ first
];
505 struct linux_prom_translation
*dest
= &prom_trans
[i
];
509 for (; i
< ents
; i
++) {
510 struct linux_prom_translation
*dest
= &prom_trans
[i
];
511 dest
->virt
= dest
->size
= dest
->data
= 0x0UL
;
514 prom_trans_ents
= last
- first
;
516 if (tlb_type
== spitfire
) {
517 /* Clear diag TTE bits. */
518 for (i
= 0; i
< prom_trans_ents
; i
++)
519 prom_trans
[i
].data
&= ~0x0003fe0000000000UL
;
523 static void __init
hypervisor_tlb_lock(unsigned long vaddr
,
527 register unsigned long func
asm("%o5");
528 register unsigned long arg0
asm("%o0");
529 register unsigned long arg1
asm("%o1");
530 register unsigned long arg2
asm("%o2");
531 register unsigned long arg3
asm("%o3");
533 func
= HV_FAST_MMU_MAP_PERM_ADDR
;
538 __asm__
__volatile__("ta 0x80"
539 : "=&r" (func
), "=&r" (arg0
),
540 "=&r" (arg1
), "=&r" (arg2
),
542 : "0" (func
), "1" (arg0
), "2" (arg1
),
543 "3" (arg2
), "4" (arg3
));
545 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
546 "errors with %lx\n", vaddr
, 0, pte
, mmu
, arg0
);
551 static unsigned long kern_large_tte(unsigned long paddr
);
553 static void __init
remap_kernel(void)
555 unsigned long phys_page
, tte_vaddr
, tte_data
;
556 int tlb_ent
= sparc64_highest_locked_tlbent();
558 tte_vaddr
= (unsigned long) KERNBASE
;
559 phys_page
= (prom_boot_mapping_phys_low
>> 22UL) << 22UL;
560 tte_data
= kern_large_tte(phys_page
);
562 kern_locked_tte_data
= tte_data
;
564 /* Now lock us into the TLBs via Hypervisor or OBP. */
565 if (tlb_type
== hypervisor
) {
566 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_DMMU
);
567 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_IMMU
);
569 tte_vaddr
+= 0x400000;
570 tte_data
+= 0x400000;
571 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_DMMU
);
572 hypervisor_tlb_lock(tte_vaddr
, tte_data
, HV_MMU_IMMU
);
575 prom_dtlb_load(tlb_ent
, tte_data
, tte_vaddr
);
576 prom_itlb_load(tlb_ent
, tte_data
, tte_vaddr
);
579 prom_dtlb_load(tlb_ent
,
581 tte_vaddr
+ 0x400000);
582 prom_itlb_load(tlb_ent
,
584 tte_vaddr
+ 0x400000);
586 sparc64_highest_unlocked_tlb_ent
= tlb_ent
- 1;
588 if (tlb_type
== cheetah_plus
) {
589 sparc64_kern_pri_context
= (CTX_CHEETAH_PLUS_CTX0
|
590 CTX_CHEETAH_PLUS_NUC
);
591 sparc64_kern_pri_nuc_bits
= CTX_CHEETAH_PLUS_NUC
;
592 sparc64_kern_sec_context
= CTX_CHEETAH_PLUS_CTX0
;
597 static void __init
inherit_prom_mappings(void)
599 read_obp_translations();
601 /* Now fixup OBP's idea about where we really are mapped. */
602 prom_printf("Remapping the kernel... ");
604 prom_printf("done.\n");
607 void prom_world(int enter
)
610 set_fs((mm_segment_t
) { get_thread_current_ds() });
612 __asm__
__volatile__("flushw");
615 #ifdef DCACHE_ALIASING_POSSIBLE
616 void __flush_dcache_range(unsigned long start
, unsigned long end
)
620 if (tlb_type
== spitfire
) {
623 for (va
= start
; va
< end
; va
+= 32) {
624 spitfire_put_dcache_tag(va
& 0x3fe0, 0x0);
628 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
631 for (va
= start
; va
< end
; va
+= 32)
632 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
636 "i" (ASI_DCACHE_INVALIDATE
));
639 #endif /* DCACHE_ALIASING_POSSIBLE */
641 /* Caller does TLB context flushing on local CPU if necessary.
642 * The caller also ensures that CTX_VALID(mm->context) is false.
644 * We must be careful about boundary cases so that we never
645 * let the user have CTX 0 (nucleus) or we ever use a CTX
646 * version of zero (and thus NO_CONTEXT would not be caught
647 * by version mis-match tests in mmu_context.h).
649 * Always invoked with interrupts disabled.
651 void get_new_mmu_context(struct mm_struct
*mm
)
653 unsigned long ctx
, new_ctx
;
654 unsigned long orig_pgsz_bits
;
658 spin_lock_irqsave(&ctx_alloc_lock
, flags
);
659 orig_pgsz_bits
= (mm
->context
.sparc64_ctx_val
& CTX_PGSZ_MASK
);
660 ctx
= (tlb_context_cache
+ 1) & CTX_NR_MASK
;
661 new_ctx
= find_next_zero_bit(mmu_context_bmap
, 1 << CTX_NR_BITS
, ctx
);
663 if (new_ctx
>= (1 << CTX_NR_BITS
)) {
664 new_ctx
= find_next_zero_bit(mmu_context_bmap
, ctx
, 1);
665 if (new_ctx
>= ctx
) {
667 new_ctx
= (tlb_context_cache
& CTX_VERSION_MASK
) +
670 new_ctx
= CTX_FIRST_VERSION
;
672 /* Don't call memset, for 16 entries that's just
675 mmu_context_bmap
[0] = 3;
676 mmu_context_bmap
[1] = 0;
677 mmu_context_bmap
[2] = 0;
678 mmu_context_bmap
[3] = 0;
679 for (i
= 4; i
< CTX_BMAP_SLOTS
; i
+= 4) {
680 mmu_context_bmap
[i
+ 0] = 0;
681 mmu_context_bmap
[i
+ 1] = 0;
682 mmu_context_bmap
[i
+ 2] = 0;
683 mmu_context_bmap
[i
+ 3] = 0;
689 mmu_context_bmap
[new_ctx
>>6] |= (1UL << (new_ctx
& 63));
690 new_ctx
|= (tlb_context_cache
& CTX_VERSION_MASK
);
692 tlb_context_cache
= new_ctx
;
693 mm
->context
.sparc64_ctx_val
= new_ctx
| orig_pgsz_bits
;
694 spin_unlock_irqrestore(&ctx_alloc_lock
, flags
);
696 if (unlikely(new_version
))
697 smp_new_mmu_context_version();
700 void sparc_ultra_dump_itlb(void)
704 if (tlb_type
== spitfire
) {
705 printk ("Contents of itlb: ");
706 for (slot
= 0; slot
< 14; slot
++) printk (" ");
707 printk ("%2x:%016lx,%016lx\n",
709 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
710 for (slot
= 1; slot
< 64; slot
+=3) {
711 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
713 spitfire_get_itlb_tag(slot
), spitfire_get_itlb_data(slot
),
715 spitfire_get_itlb_tag(slot
+1), spitfire_get_itlb_data(slot
+1),
717 spitfire_get_itlb_tag(slot
+2), spitfire_get_itlb_data(slot
+2));
719 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
720 printk ("Contents of itlb0:\n");
721 for (slot
= 0; slot
< 16; slot
+=2) {
722 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
724 cheetah_get_litlb_tag(slot
), cheetah_get_litlb_data(slot
),
726 cheetah_get_litlb_tag(slot
+1), cheetah_get_litlb_data(slot
+1));
728 printk ("Contents of itlb2:\n");
729 for (slot
= 0; slot
< 128; slot
+=2) {
730 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
732 cheetah_get_itlb_tag(slot
), cheetah_get_itlb_data(slot
),
734 cheetah_get_itlb_tag(slot
+1), cheetah_get_itlb_data(slot
+1));
739 void sparc_ultra_dump_dtlb(void)
743 if (tlb_type
== spitfire
) {
744 printk ("Contents of dtlb: ");
745 for (slot
= 0; slot
< 14; slot
++) printk (" ");
746 printk ("%2x:%016lx,%016lx\n", 0,
747 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
748 for (slot
= 1; slot
< 64; slot
+=3) {
749 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
751 spitfire_get_dtlb_tag(slot
), spitfire_get_dtlb_data(slot
),
753 spitfire_get_dtlb_tag(slot
+1), spitfire_get_dtlb_data(slot
+1),
755 spitfire_get_dtlb_tag(slot
+2), spitfire_get_dtlb_data(slot
+2));
757 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
758 printk ("Contents of dtlb0:\n");
759 for (slot
= 0; slot
< 16; slot
+=2) {
760 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
762 cheetah_get_ldtlb_tag(slot
), cheetah_get_ldtlb_data(slot
),
764 cheetah_get_ldtlb_tag(slot
+1), cheetah_get_ldtlb_data(slot
+1));
766 printk ("Contents of dtlb2:\n");
767 for (slot
= 0; slot
< 512; slot
+=2) {
768 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
770 cheetah_get_dtlb_tag(slot
, 2), cheetah_get_dtlb_data(slot
, 2),
772 cheetah_get_dtlb_tag(slot
+1, 2), cheetah_get_dtlb_data(slot
+1, 2));
774 if (tlb_type
== cheetah_plus
) {
775 printk ("Contents of dtlb3:\n");
776 for (slot
= 0; slot
< 512; slot
+=2) {
777 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
779 cheetah_get_dtlb_tag(slot
, 3), cheetah_get_dtlb_data(slot
, 3),
781 cheetah_get_dtlb_tag(slot
+1, 3), cheetah_get_dtlb_data(slot
+1, 3));
787 extern unsigned long cmdline_memory_size
;
789 /* Find a free area for the bootmem map, avoiding the kernel image
790 * and the initial ramdisk.
792 static unsigned long __init
choose_bootmap_pfn(unsigned long start_pfn
,
793 unsigned long end_pfn
)
795 unsigned long avoid_start
, avoid_end
, bootmap_size
;
798 bootmap_size
= ((end_pfn
- start_pfn
) + 7) / 8;
799 bootmap_size
= ALIGN(bootmap_size
, sizeof(long));
801 avoid_start
= avoid_end
= 0;
802 #ifdef CONFIG_BLK_DEV_INITRD
803 avoid_start
= initrd_start
;
804 avoid_end
= PAGE_ALIGN(initrd_end
);
807 #ifdef CONFIG_DEBUG_BOOTMEM
808 prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
809 kern_base
, PAGE_ALIGN(kern_base
+ kern_size
),
810 avoid_start
, avoid_end
);
812 for (i
= 0; i
< pavail_ents
; i
++) {
813 unsigned long start
, end
;
815 start
= pavail
[i
].phys_addr
;
816 end
= start
+ pavail
[i
].reg_size
;
818 while (start
< end
) {
819 if (start
>= kern_base
&&
820 start
< PAGE_ALIGN(kern_base
+ kern_size
)) {
821 start
= PAGE_ALIGN(kern_base
+ kern_size
);
824 if (start
>= avoid_start
&& start
< avoid_end
) {
829 if ((end
- start
) < bootmap_size
)
832 if (start
< kern_base
&&
833 (start
+ bootmap_size
) > kern_base
) {
834 start
= PAGE_ALIGN(kern_base
+ kern_size
);
838 if (start
< avoid_start
&&
839 (start
+ bootmap_size
) > avoid_start
) {
844 /* OK, it doesn't overlap anything, use it. */
845 #ifdef CONFIG_DEBUG_BOOTMEM
846 prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
847 start
>> PAGE_SHIFT
, start
);
849 return start
>> PAGE_SHIFT
;
853 prom_printf("Cannot find free area for bootmap, aborting.\n");
857 static unsigned long __init
bootmem_init(unsigned long *pages_avail
,
858 unsigned long phys_base
)
860 unsigned long bootmap_size
, end_pfn
;
861 unsigned long end_of_phys_memory
= 0UL;
862 unsigned long bootmap_pfn
, bytes_avail
, size
;
865 #ifdef CONFIG_DEBUG_BOOTMEM
866 prom_printf("bootmem_init: Scan pavail, ");
870 for (i
= 0; i
< pavail_ents
; i
++) {
871 end_of_phys_memory
= pavail
[i
].phys_addr
+
873 bytes_avail
+= pavail
[i
].reg_size
;
874 if (cmdline_memory_size
) {
875 if (bytes_avail
> cmdline_memory_size
) {
876 unsigned long slack
= bytes_avail
- cmdline_memory_size
;
878 bytes_avail
-= slack
;
879 end_of_phys_memory
-= slack
;
881 pavail
[i
].reg_size
-= slack
;
882 if ((long)pavail
[i
].reg_size
<= 0L) {
883 pavail
[i
].phys_addr
= 0xdeadbeefUL
;
884 pavail
[i
].reg_size
= 0UL;
887 pavail
[i
+1].reg_size
= 0Ul;
888 pavail
[i
+1].phys_addr
= 0xdeadbeefUL
;
896 *pages_avail
= bytes_avail
>> PAGE_SHIFT
;
898 end_pfn
= end_of_phys_memory
>> PAGE_SHIFT
;
900 #ifdef CONFIG_BLK_DEV_INITRD
901 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
902 if (sparc_ramdisk_image
|| sparc_ramdisk_image64
) {
903 unsigned long ramdisk_image
= sparc_ramdisk_image
?
904 sparc_ramdisk_image
: sparc_ramdisk_image64
;
905 if (ramdisk_image
>= (unsigned long)_end
- 2 * PAGE_SIZE
)
906 ramdisk_image
-= KERNBASE
;
907 initrd_start
= ramdisk_image
+ phys_base
;
908 initrd_end
= initrd_start
+ sparc_ramdisk_size
;
909 if (initrd_end
> end_of_phys_memory
) {
910 printk(KERN_CRIT
"initrd extends beyond end of memory "
911 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
912 initrd_end
, end_of_phys_memory
);
918 /* Initialize the boot-time allocator. */
919 max_pfn
= max_low_pfn
= end_pfn
;
920 min_low_pfn
= (phys_base
>> PAGE_SHIFT
);
922 bootmap_pfn
= choose_bootmap_pfn(min_low_pfn
, end_pfn
);
924 #ifdef CONFIG_DEBUG_BOOTMEM
925 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
926 min_low_pfn
, bootmap_pfn
, max_low_pfn
);
928 bootmap_size
= init_bootmem_node(NODE_DATA(0), bootmap_pfn
,
929 min_low_pfn
, end_pfn
);
931 /* Now register the available physical memory with the
934 for (i
= 0; i
< pavail_ents
; i
++) {
935 #ifdef CONFIG_DEBUG_BOOTMEM
936 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
937 i
, pavail
[i
].phys_addr
, pavail
[i
].reg_size
);
939 free_bootmem(pavail
[i
].phys_addr
, pavail
[i
].reg_size
);
942 #ifdef CONFIG_BLK_DEV_INITRD
944 size
= initrd_end
- initrd_start
;
946 /* Resert the initrd image area. */
947 #ifdef CONFIG_DEBUG_BOOTMEM
948 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
949 initrd_start
, initrd_end
);
951 reserve_bootmem(initrd_start
, size
);
952 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
954 initrd_start
+= PAGE_OFFSET
;
955 initrd_end
+= PAGE_OFFSET
;
958 /* Reserve the kernel text/data/bss. */
959 #ifdef CONFIG_DEBUG_BOOTMEM
960 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base
, kern_size
);
962 reserve_bootmem(kern_base
, kern_size
);
963 *pages_avail
-= PAGE_ALIGN(kern_size
) >> PAGE_SHIFT
;
965 /* Reserve the bootmem map. We do not account for it
966 * in pages_avail because we will release that memory
967 * in free_all_bootmem.
970 #ifdef CONFIG_DEBUG_BOOTMEM
971 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
972 (bootmap_pfn
<< PAGE_SHIFT
), size
);
974 reserve_bootmem((bootmap_pfn
<< PAGE_SHIFT
), size
);
975 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
977 for (i
= 0; i
< pavail_ents
; i
++) {
978 unsigned long start_pfn
, end_pfn
;
980 start_pfn
= pavail
[i
].phys_addr
>> PAGE_SHIFT
;
981 end_pfn
= (start_pfn
+ (pavail
[i
].reg_size
>> PAGE_SHIFT
));
982 #ifdef CONFIG_DEBUG_BOOTMEM
983 prom_printf("memory_present(0, %lx, %lx)\n",
986 memory_present(0, start_pfn
, end_pfn
);
994 static struct linux_prom64_registers pall
[MAX_BANKS
] __initdata
;
995 static int pall_ents __initdata
;
997 #ifdef CONFIG_DEBUG_PAGEALLOC
998 static unsigned long kernel_map_range(unsigned long pstart
, unsigned long pend
, pgprot_t prot
)
1000 unsigned long vstart
= PAGE_OFFSET
+ pstart
;
1001 unsigned long vend
= PAGE_OFFSET
+ pend
;
1002 unsigned long alloc_bytes
= 0UL;
1004 if ((vstart
& ~PAGE_MASK
) || (vend
& ~PAGE_MASK
)) {
1005 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
1010 while (vstart
< vend
) {
1011 unsigned long this_end
, paddr
= __pa(vstart
);
1012 pgd_t
*pgd
= pgd_offset_k(vstart
);
1017 pud
= pud_offset(pgd
, vstart
);
1018 if (pud_none(*pud
)) {
1021 new = __alloc_bootmem(PAGE_SIZE
, PAGE_SIZE
, PAGE_SIZE
);
1022 alloc_bytes
+= PAGE_SIZE
;
1023 pud_populate(&init_mm
, pud
, new);
1026 pmd
= pmd_offset(pud
, vstart
);
1027 if (!pmd_present(*pmd
)) {
1030 new = __alloc_bootmem(PAGE_SIZE
, PAGE_SIZE
, PAGE_SIZE
);
1031 alloc_bytes
+= PAGE_SIZE
;
1032 pmd_populate_kernel(&init_mm
, pmd
, new);
1035 pte
= pte_offset_kernel(pmd
, vstart
);
1036 this_end
= (vstart
+ PMD_SIZE
) & PMD_MASK
;
1037 if (this_end
> vend
)
1040 while (vstart
< this_end
) {
1041 pte_val(*pte
) = (paddr
| pgprot_val(prot
));
1043 vstart
+= PAGE_SIZE
;
1052 extern unsigned int kvmap_linear_patch
[1];
1053 #endif /* CONFIG_DEBUG_PAGEALLOC */
1055 static void __init
mark_kpte_bitmap(unsigned long start
, unsigned long end
)
1057 const unsigned long shift_256MB
= 28;
1058 const unsigned long mask_256MB
= ((1UL << shift_256MB
) - 1UL);
1059 const unsigned long size_256MB
= (1UL << shift_256MB
);
1061 while (start
< end
) {
1064 remains
= end
- start
;
1065 if (remains
< size_256MB
)
1068 if (start
& mask_256MB
) {
1069 start
= (start
+ size_256MB
) & ~mask_256MB
;
1073 while (remains
>= size_256MB
) {
1074 unsigned long index
= start
>> shift_256MB
;
1076 __set_bit(index
, kpte_linear_bitmap
);
1078 start
+= size_256MB
;
1079 remains
-= size_256MB
;
1084 static void __init
kernel_physical_mapping_init(void)
1087 #ifdef CONFIG_DEBUG_PAGEALLOC
1088 unsigned long mem_alloced
= 0UL;
1091 read_obp_memory("reg", &pall
[0], &pall_ents
);
1093 for (i
= 0; i
< pall_ents
; i
++) {
1094 unsigned long phys_start
, phys_end
;
1096 phys_start
= pall
[i
].phys_addr
;
1097 phys_end
= phys_start
+ pall
[i
].reg_size
;
1099 mark_kpte_bitmap(phys_start
, phys_end
);
1101 #ifdef CONFIG_DEBUG_PAGEALLOC
1102 mem_alloced
+= kernel_map_range(phys_start
, phys_end
,
1107 #ifdef CONFIG_DEBUG_PAGEALLOC
1108 printk("Allocated %ld bytes for kernel page tables.\n",
1111 kvmap_linear_patch
[0] = 0x01000000; /* nop */
1112 flushi(&kvmap_linear_patch
[0]);
1118 #ifdef CONFIG_DEBUG_PAGEALLOC
1119 void kernel_map_pages(struct page
*page
, int numpages
, int enable
)
1121 unsigned long phys_start
= page_to_pfn(page
) << PAGE_SHIFT
;
1122 unsigned long phys_end
= phys_start
+ (numpages
* PAGE_SIZE
);
1124 kernel_map_range(phys_start
, phys_end
,
1125 (enable
? PAGE_KERNEL
: __pgprot(0)));
1127 flush_tsb_kernel_range(PAGE_OFFSET
+ phys_start
,
1128 PAGE_OFFSET
+ phys_end
);
1130 /* we should perform an IPI and flush all tlbs,
1131 * but that can deadlock->flush only current cpu.
1133 __flush_tlb_kernel_range(PAGE_OFFSET
+ phys_start
,
1134 PAGE_OFFSET
+ phys_end
);
1138 unsigned long __init
find_ecache_flush_span(unsigned long size
)
1142 for (i
= 0; i
< pavail_ents
; i
++) {
1143 if (pavail
[i
].reg_size
>= size
)
1144 return pavail
[i
].phys_addr
;
1150 static void __init
tsb_phys_patch(void)
1152 struct tsb_ldquad_phys_patch_entry
*pquad
;
1153 struct tsb_phys_patch_entry
*p
;
1155 pquad
= &__tsb_ldquad_phys_patch
;
1156 while (pquad
< &__tsb_ldquad_phys_patch_end
) {
1157 unsigned long addr
= pquad
->addr
;
1159 if (tlb_type
== hypervisor
)
1160 *(unsigned int *) addr
= pquad
->sun4v_insn
;
1162 *(unsigned int *) addr
= pquad
->sun4u_insn
;
1164 __asm__
__volatile__("flush %0"
1171 p
= &__tsb_phys_patch
;
1172 while (p
< &__tsb_phys_patch_end
) {
1173 unsigned long addr
= p
->addr
;
1175 *(unsigned int *) addr
= p
->insn
;
1177 __asm__
__volatile__("flush %0"
1185 /* Don't mark as init, we give this to the Hypervisor. */
1186 static struct hv_tsb_descr ktsb_descr
[2];
1187 extern struct tsb swapper_tsb
[KERNEL_TSB_NENTRIES
];
1189 static void __init
sun4v_ktsb_init(void)
1191 unsigned long ktsb_pa
;
1193 /* First KTSB for PAGE_SIZE mappings. */
1194 ktsb_pa
= kern_base
+ ((unsigned long)&swapper_tsb
[0] - KERNBASE
);
1196 switch (PAGE_SIZE
) {
1199 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_8K
;
1200 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_8K
;
1204 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_64K
;
1205 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_64K
;
1209 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_512K
;
1210 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_512K
;
1213 case 4 * 1024 * 1024:
1214 ktsb_descr
[0].pgsz_idx
= HV_PGSZ_IDX_4MB
;
1215 ktsb_descr
[0].pgsz_mask
= HV_PGSZ_MASK_4MB
;
1219 ktsb_descr
[0].assoc
= 1;
1220 ktsb_descr
[0].num_ttes
= KERNEL_TSB_NENTRIES
;
1221 ktsb_descr
[0].ctx_idx
= 0;
1222 ktsb_descr
[0].tsb_base
= ktsb_pa
;
1223 ktsb_descr
[0].resv
= 0;
1225 /* Second KTSB for 4MB/256MB mappings. */
1226 ktsb_pa
= (kern_base
+
1227 ((unsigned long)&swapper_4m_tsb
[0] - KERNBASE
));
1229 ktsb_descr
[1].pgsz_idx
= HV_PGSZ_IDX_4MB
;
1230 ktsb_descr
[1].pgsz_mask
= (HV_PGSZ_MASK_4MB
|
1231 HV_PGSZ_MASK_256MB
);
1232 ktsb_descr
[1].assoc
= 1;
1233 ktsb_descr
[1].num_ttes
= KERNEL_TSB4M_NENTRIES
;
1234 ktsb_descr
[1].ctx_idx
= 0;
1235 ktsb_descr
[1].tsb_base
= ktsb_pa
;
1236 ktsb_descr
[1].resv
= 0;
1239 void __cpuinit
sun4v_ktsb_register(void)
1241 register unsigned long func
asm("%o5");
1242 register unsigned long arg0
asm("%o0");
1243 register unsigned long arg1
asm("%o1");
1246 pa
= kern_base
+ ((unsigned long)&ktsb_descr
[0] - KERNBASE
);
1248 func
= HV_FAST_MMU_TSB_CTX0
;
1251 __asm__
__volatile__("ta %6"
1252 : "=&r" (func
), "=&r" (arg0
), "=&r" (arg1
)
1253 : "0" (func
), "1" (arg0
), "2" (arg1
),
1254 "i" (HV_FAST_TRAP
));
1257 /* paging_init() sets up the page tables */
1259 extern void cheetah_ecache_flush_init(void);
1260 extern void sun4v_patch_tlb_handlers(void);
1262 static unsigned long last_valid_pfn
;
1263 pgd_t swapper_pg_dir
[2048];
1265 static void sun4u_pgprot_init(void);
1266 static void sun4v_pgprot_init(void);
1268 void __init
paging_init(void)
1270 unsigned long end_pfn
, pages_avail
, shift
, phys_base
;
1271 unsigned long real_end
, i
;
1273 kern_base
= (prom_boot_mapping_phys_low
>> 22UL) << 22UL;
1274 kern_size
= (unsigned long)&_end
- (unsigned long)KERNBASE
;
1276 /* Invalidate both kernel TSBs. */
1277 memset(swapper_tsb
, 0x40, sizeof(swapper_tsb
));
1278 memset(swapper_4m_tsb
, 0x40, sizeof(swapper_4m_tsb
));
1280 if (tlb_type
== hypervisor
)
1281 sun4v_pgprot_init();
1283 sun4u_pgprot_init();
1285 if (tlb_type
== cheetah_plus
||
1286 tlb_type
== hypervisor
)
1289 if (tlb_type
== hypervisor
) {
1290 sun4v_patch_tlb_handlers();
1294 /* Find available physical memory... */
1295 read_obp_memory("available", &pavail
[0], &pavail_ents
);
1297 phys_base
= 0xffffffffffffffffUL
;
1298 for (i
= 0; i
< pavail_ents
; i
++)
1299 phys_base
= min(phys_base
, pavail
[i
].phys_addr
);
1301 set_bit(0, mmu_context_bmap
);
1303 shift
= kern_base
+ PAGE_OFFSET
- ((unsigned long)KERNBASE
);
1305 real_end
= (unsigned long)_end
;
1306 if ((real_end
> ((unsigned long)KERNBASE
+ 0x400000)))
1308 if ((real_end
> ((unsigned long)KERNBASE
+ 0x800000))) {
1309 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1313 /* Set kernel pgd to upper alias so physical page computations
1316 init_mm
.pgd
+= ((shift
) / (sizeof(pgd_t
)));
1318 memset(swapper_low_pmd_dir
, 0, sizeof(swapper_low_pmd_dir
));
1320 /* Now can init the kernel/bad page tables. */
1321 pud_set(pud_offset(&swapper_pg_dir
[0], 0),
1322 swapper_low_pmd_dir
+ (shift
/ sizeof(pgd_t
)));
1324 inherit_prom_mappings();
1326 /* Ok, we can use our TLB miss and window trap handlers safely. */
1331 if (tlb_type
== hypervisor
)
1332 sun4v_ktsb_register();
1334 /* Setup bootmem... */
1336 last_valid_pfn
= end_pfn
= bootmem_init(&pages_avail
, phys_base
);
1338 max_mapnr
= last_valid_pfn
;
1340 kernel_physical_mapping_init();
1343 unsigned long zones_size
[MAX_NR_ZONES
];
1344 unsigned long zholes_size
[MAX_NR_ZONES
];
1347 for (znum
= 0; znum
< MAX_NR_ZONES
; znum
++)
1348 zones_size
[znum
] = zholes_size
[znum
] = 0;
1350 zones_size
[ZONE_DMA
] = end_pfn
;
1351 zholes_size
[ZONE_DMA
] = end_pfn
- pages_avail
;
1353 free_area_init_node(0, &contig_page_data
, zones_size
,
1354 __pa(PAGE_OFFSET
) >> PAGE_SHIFT
,
1361 static void __init
taint_real_pages(void)
1365 read_obp_memory("available", &pavail_rescan
[0], &pavail_rescan_ents
);
1367 /* Find changes discovered in the physmem available rescan and
1368 * reserve the lost portions in the bootmem maps.
1370 for (i
= 0; i
< pavail_ents
; i
++) {
1371 unsigned long old_start
, old_end
;
1373 old_start
= pavail
[i
].phys_addr
;
1374 old_end
= old_start
+
1376 while (old_start
< old_end
) {
1379 for (n
= 0; pavail_rescan_ents
; n
++) {
1380 unsigned long new_start
, new_end
;
1382 new_start
= pavail_rescan
[n
].phys_addr
;
1383 new_end
= new_start
+
1384 pavail_rescan
[n
].reg_size
;
1386 if (new_start
<= old_start
&&
1387 new_end
>= (old_start
+ PAGE_SIZE
)) {
1388 set_bit(old_start
>> 22,
1389 sparc64_valid_addr_bitmap
);
1393 reserve_bootmem(old_start
, PAGE_SIZE
);
1396 old_start
+= PAGE_SIZE
;
1401 void __init
mem_init(void)
1403 unsigned long codepages
, datapages
, initpages
;
1404 unsigned long addr
, last
;
1407 i
= last_valid_pfn
>> ((22 - PAGE_SHIFT
) + 6);
1409 sparc64_valid_addr_bitmap
= (unsigned long *) alloc_bootmem(i
<< 3);
1410 if (sparc64_valid_addr_bitmap
== NULL
) {
1411 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1414 memset(sparc64_valid_addr_bitmap
, 0, i
<< 3);
1416 addr
= PAGE_OFFSET
+ kern_base
;
1417 last
= PAGE_ALIGN(kern_size
) + addr
;
1418 while (addr
< last
) {
1419 set_bit(__pa(addr
) >> 22, sparc64_valid_addr_bitmap
);
1425 high_memory
= __va(last_valid_pfn
<< PAGE_SHIFT
);
1427 #ifdef CONFIG_DEBUG_BOOTMEM
1428 prom_printf("mem_init: Calling free_all_bootmem().\n");
1430 totalram_pages
= num_physpages
= free_all_bootmem() - 1;
1433 * Set up the zero page, mark it reserved, so that page count
1434 * is not manipulated when freeing the page from user ptes.
1436 mem_map_zero
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
1437 if (mem_map_zero
== NULL
) {
1438 prom_printf("paging_init: Cannot alloc zero page.\n");
1441 SetPageReserved(mem_map_zero
);
1443 codepages
= (((unsigned long) _etext
) - ((unsigned long) _start
));
1444 codepages
= PAGE_ALIGN(codepages
) >> PAGE_SHIFT
;
1445 datapages
= (((unsigned long) _edata
) - ((unsigned long) _etext
));
1446 datapages
= PAGE_ALIGN(datapages
) >> PAGE_SHIFT
;
1447 initpages
= (((unsigned long) __init_end
) - ((unsigned long) __init_begin
));
1448 initpages
= PAGE_ALIGN(initpages
) >> PAGE_SHIFT
;
1450 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1451 nr_free_pages() << (PAGE_SHIFT
-10),
1452 codepages
<< (PAGE_SHIFT
-10),
1453 datapages
<< (PAGE_SHIFT
-10),
1454 initpages
<< (PAGE_SHIFT
-10),
1455 PAGE_OFFSET
, (last_valid_pfn
<< PAGE_SHIFT
));
1457 if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
1458 cheetah_ecache_flush_init();
1461 void free_initmem(void)
1463 unsigned long addr
, initend
;
1466 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1468 addr
= PAGE_ALIGN((unsigned long)(__init_begin
));
1469 initend
= (unsigned long)(__init_end
) & PAGE_MASK
;
1470 for (; addr
< initend
; addr
+= PAGE_SIZE
) {
1475 ((unsigned long) __va(kern_base
)) -
1476 ((unsigned long) KERNBASE
));
1477 memset((void *)addr
, 0xcc, PAGE_SIZE
);
1478 p
= virt_to_page(page
);
1480 ClearPageReserved(p
);
1488 #ifdef CONFIG_BLK_DEV_INITRD
1489 void free_initrd_mem(unsigned long start
, unsigned long end
)
1492 printk ("Freeing initrd memory: %ldk freed\n", (end
- start
) >> 10);
1493 for (; start
< end
; start
+= PAGE_SIZE
) {
1494 struct page
*p
= virt_to_page(start
);
1496 ClearPageReserved(p
);
1505 #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1506 #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1507 #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1508 #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1509 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1510 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1512 pgprot_t PAGE_KERNEL __read_mostly
;
1513 EXPORT_SYMBOL(PAGE_KERNEL
);
1515 pgprot_t PAGE_KERNEL_LOCKED __read_mostly
;
1516 pgprot_t PAGE_COPY __read_mostly
;
1518 pgprot_t PAGE_SHARED __read_mostly
;
1519 EXPORT_SYMBOL(PAGE_SHARED
);
1521 pgprot_t PAGE_EXEC __read_mostly
;
1522 unsigned long pg_iobits __read_mostly
;
1524 unsigned long _PAGE_IE __read_mostly
;
1526 unsigned long _PAGE_E __read_mostly
;
1527 EXPORT_SYMBOL(_PAGE_E
);
1529 unsigned long _PAGE_CACHE __read_mostly
;
1530 EXPORT_SYMBOL(_PAGE_CACHE
);
1532 static void prot_init_common(unsigned long page_none
,
1533 unsigned long page_shared
,
1534 unsigned long page_copy
,
1535 unsigned long page_readonly
,
1536 unsigned long page_exec_bit
)
1538 PAGE_COPY
= __pgprot(page_copy
);
1539 PAGE_SHARED
= __pgprot(page_shared
);
1541 protection_map
[0x0] = __pgprot(page_none
);
1542 protection_map
[0x1] = __pgprot(page_readonly
& ~page_exec_bit
);
1543 protection_map
[0x2] = __pgprot(page_copy
& ~page_exec_bit
);
1544 protection_map
[0x3] = __pgprot(page_copy
& ~page_exec_bit
);
1545 protection_map
[0x4] = __pgprot(page_readonly
);
1546 protection_map
[0x5] = __pgprot(page_readonly
);
1547 protection_map
[0x6] = __pgprot(page_copy
);
1548 protection_map
[0x7] = __pgprot(page_copy
);
1549 protection_map
[0x8] = __pgprot(page_none
);
1550 protection_map
[0x9] = __pgprot(page_readonly
& ~page_exec_bit
);
1551 protection_map
[0xa] = __pgprot(page_shared
& ~page_exec_bit
);
1552 protection_map
[0xb] = __pgprot(page_shared
& ~page_exec_bit
);
1553 protection_map
[0xc] = __pgprot(page_readonly
);
1554 protection_map
[0xd] = __pgprot(page_readonly
);
1555 protection_map
[0xe] = __pgprot(page_shared
);
1556 protection_map
[0xf] = __pgprot(page_shared
);
1559 static void __init
sun4u_pgprot_init(void)
1561 unsigned long page_none
, page_shared
, page_copy
, page_readonly
;
1562 unsigned long page_exec_bit
;
1564 PAGE_KERNEL
= __pgprot (_PAGE_PRESENT_4U
| _PAGE_VALID
|
1565 _PAGE_CACHE_4U
| _PAGE_P_4U
|
1566 __ACCESS_BITS_4U
| __DIRTY_BITS_4U
|
1568 PAGE_KERNEL_LOCKED
= __pgprot (_PAGE_PRESENT_4U
| _PAGE_VALID
|
1569 _PAGE_CACHE_4U
| _PAGE_P_4U
|
1570 __ACCESS_BITS_4U
| __DIRTY_BITS_4U
|
1571 _PAGE_EXEC_4U
| _PAGE_L_4U
);
1572 PAGE_EXEC
= __pgprot(_PAGE_EXEC_4U
);
1574 _PAGE_IE
= _PAGE_IE_4U
;
1575 _PAGE_E
= _PAGE_E_4U
;
1576 _PAGE_CACHE
= _PAGE_CACHE_4U
;
1578 pg_iobits
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| __DIRTY_BITS_4U
|
1579 __ACCESS_BITS_4U
| _PAGE_E_4U
);
1581 kern_linear_pte_xor
[0] = (_PAGE_VALID
| _PAGE_SZ4MB_4U
) ^
1583 kern_linear_pte_xor
[0] |= (_PAGE_CP_4U
| _PAGE_CV_4U
|
1584 _PAGE_P_4U
| _PAGE_W_4U
);
1586 /* XXX Should use 256MB on Panther. XXX */
1587 kern_linear_pte_xor
[1] = kern_linear_pte_xor
[0];
1589 _PAGE_SZBITS
= _PAGE_SZBITS_4U
;
1590 _PAGE_ALL_SZ_BITS
= (_PAGE_SZ4MB_4U
| _PAGE_SZ512K_4U
|
1591 _PAGE_SZ64K_4U
| _PAGE_SZ8K_4U
|
1592 _PAGE_SZ32MB_4U
| _PAGE_SZ256MB_4U
);
1595 page_none
= _PAGE_PRESENT_4U
| _PAGE_ACCESSED_4U
| _PAGE_CACHE_4U
;
1596 page_shared
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| _PAGE_CACHE_4U
|
1597 __ACCESS_BITS_4U
| _PAGE_WRITE_4U
| _PAGE_EXEC_4U
);
1598 page_copy
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| _PAGE_CACHE_4U
|
1599 __ACCESS_BITS_4U
| _PAGE_EXEC_4U
);
1600 page_readonly
= (_PAGE_VALID
| _PAGE_PRESENT_4U
| _PAGE_CACHE_4U
|
1601 __ACCESS_BITS_4U
| _PAGE_EXEC_4U
);
1603 page_exec_bit
= _PAGE_EXEC_4U
;
1605 prot_init_common(page_none
, page_shared
, page_copy
, page_readonly
,
1609 static void __init
sun4v_pgprot_init(void)
1611 unsigned long page_none
, page_shared
, page_copy
, page_readonly
;
1612 unsigned long page_exec_bit
;
1614 PAGE_KERNEL
= __pgprot (_PAGE_PRESENT_4V
| _PAGE_VALID
|
1615 _PAGE_CACHE_4V
| _PAGE_P_4V
|
1616 __ACCESS_BITS_4V
| __DIRTY_BITS_4V
|
1618 PAGE_KERNEL_LOCKED
= PAGE_KERNEL
;
1619 PAGE_EXEC
= __pgprot(_PAGE_EXEC_4V
);
1621 _PAGE_IE
= _PAGE_IE_4V
;
1622 _PAGE_E
= _PAGE_E_4V
;
1623 _PAGE_CACHE
= _PAGE_CACHE_4V
;
1625 kern_linear_pte_xor
[0] = (_PAGE_VALID
| _PAGE_SZ4MB_4V
) ^
1627 kern_linear_pte_xor
[0] |= (_PAGE_CP_4V
| _PAGE_CV_4V
|
1628 _PAGE_P_4V
| _PAGE_W_4V
);
1630 kern_linear_pte_xor
[1] = (_PAGE_VALID
| _PAGE_SZ256MB_4V
) ^
1632 kern_linear_pte_xor
[1] |= (_PAGE_CP_4V
| _PAGE_CV_4V
|
1633 _PAGE_P_4V
| _PAGE_W_4V
);
1635 pg_iobits
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| __DIRTY_BITS_4V
|
1636 __ACCESS_BITS_4V
| _PAGE_E_4V
);
1638 _PAGE_SZBITS
= _PAGE_SZBITS_4V
;
1639 _PAGE_ALL_SZ_BITS
= (_PAGE_SZ16GB_4V
| _PAGE_SZ2GB_4V
|
1640 _PAGE_SZ256MB_4V
| _PAGE_SZ32MB_4V
|
1641 _PAGE_SZ4MB_4V
| _PAGE_SZ512K_4V
|
1642 _PAGE_SZ64K_4V
| _PAGE_SZ8K_4V
);
1644 page_none
= _PAGE_PRESENT_4V
| _PAGE_ACCESSED_4V
| _PAGE_CACHE_4V
;
1645 page_shared
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| _PAGE_CACHE_4V
|
1646 __ACCESS_BITS_4V
| _PAGE_WRITE_4V
| _PAGE_EXEC_4V
);
1647 page_copy
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| _PAGE_CACHE_4V
|
1648 __ACCESS_BITS_4V
| _PAGE_EXEC_4V
);
1649 page_readonly
= (_PAGE_VALID
| _PAGE_PRESENT_4V
| _PAGE_CACHE_4V
|
1650 __ACCESS_BITS_4V
| _PAGE_EXEC_4V
);
1652 page_exec_bit
= _PAGE_EXEC_4V
;
1654 prot_init_common(page_none
, page_shared
, page_copy
, page_readonly
,
1658 unsigned long pte_sz_bits(unsigned long sz
)
1660 if (tlb_type
== hypervisor
) {
1664 return _PAGE_SZ8K_4V
;
1666 return _PAGE_SZ64K_4V
;
1668 return _PAGE_SZ512K_4V
;
1669 case 4 * 1024 * 1024:
1670 return _PAGE_SZ4MB_4V
;
1676 return _PAGE_SZ8K_4U
;
1678 return _PAGE_SZ64K_4U
;
1680 return _PAGE_SZ512K_4U
;
1681 case 4 * 1024 * 1024:
1682 return _PAGE_SZ4MB_4U
;
1687 pte_t
mk_pte_io(unsigned long page
, pgprot_t prot
, int space
, unsigned long page_size
)
1691 pte_val(pte
) = page
| pgprot_val(pgprot_noncached(prot
));
1692 pte_val(pte
) |= (((unsigned long)space
) << 32);
1693 pte_val(pte
) |= pte_sz_bits(page_size
);
1698 static unsigned long kern_large_tte(unsigned long paddr
)
1702 val
= (_PAGE_VALID
| _PAGE_SZ4MB_4U
|
1703 _PAGE_CP_4U
| _PAGE_CV_4U
| _PAGE_P_4U
|
1704 _PAGE_EXEC_4U
| _PAGE_L_4U
| _PAGE_W_4U
);
1705 if (tlb_type
== hypervisor
)
1706 val
= (_PAGE_VALID
| _PAGE_SZ4MB_4V
|
1707 _PAGE_CP_4V
| _PAGE_CV_4V
| _PAGE_P_4V
|
1708 _PAGE_EXEC_4V
| _PAGE_W_4V
);
1714 * Translate PROM's mapping we capture at boot time into physical address.
1715 * The second parameter is only set from prom_callback() invocations.
1717 unsigned long prom_virt_to_phys(unsigned long promva
, int *error
)
1722 mask
= _PAGE_PADDR_4U
;
1723 if (tlb_type
== hypervisor
)
1724 mask
= _PAGE_PADDR_4V
;
1726 for (i
= 0; i
< prom_trans_ents
; i
++) {
1727 struct linux_prom_translation
*p
= &prom_trans
[i
];
1729 if (promva
>= p
->virt
&&
1730 promva
< (p
->virt
+ p
->size
)) {
1731 unsigned long base
= p
->data
& mask
;
1735 return base
+ (promva
& (8192 - 1));
1743 /* XXX We should kill off this ugly thing at so me point. XXX */
1744 unsigned long sun4u_get_pte(unsigned long addr
)
1750 unsigned long mask
= _PAGE_PADDR_4U
;
1752 if (tlb_type
== hypervisor
)
1753 mask
= _PAGE_PADDR_4V
;
1755 if (addr
>= PAGE_OFFSET
)
1758 if ((addr
>= LOW_OBP_ADDRESS
) && (addr
< HI_OBP_ADDRESS
))
1759 return prom_virt_to_phys(addr
, NULL
);
1761 pgdp
= pgd_offset_k(addr
);
1762 pudp
= pud_offset(pgdp
, addr
);
1763 pmdp
= pmd_offset(pudp
, addr
);
1764 ptep
= pte_offset_kernel(pmdp
, addr
);
1766 return pte_val(*ptep
) & mask
;
1769 /* If not locked, zap it. */
1770 void __flush_tlb_all(void)
1772 unsigned long pstate
;
1775 __asm__
__volatile__("flushw\n\t"
1776 "rdpr %%pstate, %0\n\t"
1777 "wrpr %0, %1, %%pstate"
1780 if (tlb_type
== spitfire
) {
1781 for (i
= 0; i
< 64; i
++) {
1782 /* Spitfire Errata #32 workaround */
1783 /* NOTE: Always runs on spitfire, so no
1784 * cheetah+ page size encodings.
1786 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1790 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1792 if (!(spitfire_get_dtlb_data(i
) & _PAGE_L_4U
)) {
1793 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1796 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
1797 spitfire_put_dtlb_data(i
, 0x0UL
);
1800 /* Spitfire Errata #32 workaround */
1801 /* NOTE: Always runs on spitfire, so no
1802 * cheetah+ page size encodings.
1804 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1808 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1810 if (!(spitfire_get_itlb_data(i
) & _PAGE_L_4U
)) {
1811 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1814 : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
1815 spitfire_put_itlb_data(i
, 0x0UL
);
1818 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1819 cheetah_flush_dtlb_all();
1820 cheetah_flush_itlb_all();
1822 __asm__
__volatile__("wrpr %0, 0, %%pstate"
1826 #ifdef CONFIG_MEMORY_HOTPLUG
1828 void online_page(struct page
*page
)
1830 ClearPageReserved(page
);
1831 init_page_count(page
);
1837 int remove_memory(u64 start
, u64 size
)
1842 #endif /* CONFIG_MEMORY_HOTPLUG */