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/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/string.h>
12 #include <linux/init.h>
13 #include <linux/bootmem.h>
15 #include <linux/hugetlb.h>
16 #include <linux/slab.h>
17 #include <linux/initrd.h>
18 #include <linux/swap.h>
19 #include <linux/pagemap.h>
21 #include <linux/seq_file.h>
22 #include <linux/kprobes.h>
23 #include <linux/cache.h>
24 #include <linux/sort.h>
27 #include <asm/system.h>
29 #include <asm/pgalloc.h>
30 #include <asm/pgtable.h>
31 #include <asm/oplib.h>
32 #include <asm/iommu.h>
34 #include <asm/uaccess.h>
35 #include <asm/mmu_context.h>
36 #include <asm/tlbflush.h>
38 #include <asm/starfire.h>
40 #include <asm/spitfire.h>
41 #include <asm/sections.h>
43 extern void device_scan(void);
47 static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
48 static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
49 static int pavail_ents __initdata;
50 static int pavail_rescan_ents __initdata;
52 static int cmp_p64(const void *a, const void *b)
54 const struct linux_prom64_registers *x = a, *y = b;
56 if (x->phys_addr > y->phys_addr)
58 if (x->phys_addr < y->phys_addr)
63 static void __init read_obp_memory(const char *property,
64 struct linux_prom64_registers *regs,
67 int node = prom_finddevice("/memory");
68 int prop_size = prom_getproplen(node, property);
71 ents = prop_size / sizeof(struct linux_prom64_registers);
72 if (ents > MAX_BANKS) {
73 prom_printf("The machine has more %s property entries than "
74 "this kernel can support (%d).\n",
79 ret = prom_getproperty(node, property, (char *) regs, prop_size);
81 prom_printf("Couldn't get %s property from /memory.\n");
87 /* Sanitize what we got from the firmware, by page aligning
90 for (i = 0; i < ents; i++) {
91 unsigned long base, size;
93 base = regs[i].phys_addr;
94 size = regs[i].reg_size;
97 if (base & ~PAGE_MASK) {
98 unsigned long new_base = PAGE_ALIGN(base);
100 size -= new_base - base;
101 if ((long) size < 0L)
105 regs[i].phys_addr = base;
106 regs[i].reg_size = size;
108 sort(regs, ents, sizeof(struct linux_prom64_registers),
112 unsigned long *sparc64_valid_addr_bitmap __read_mostly;
114 /* Ugly, but necessary... -DaveM */
115 unsigned long phys_base __read_mostly;
116 unsigned long kern_base __read_mostly;
117 unsigned long kern_size __read_mostly;
118 unsigned long pfn_base __read_mostly;
120 /* get_new_mmu_context() uses "cache + 1". */
121 DEFINE_SPINLOCK(ctx_alloc_lock);
122 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
123 #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
124 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
126 /* References to special section boundaries */
127 extern char _start[], _end[];
129 /* Initial ramdisk setup */
130 extern unsigned long sparc_ramdisk_image64;
131 extern unsigned int sparc_ramdisk_image;
132 extern unsigned int sparc_ramdisk_size;
134 struct page *mem_map_zero __read_mostly;
138 /* XXX Tune this... */
139 #define PGT_CACHE_LOW 25
140 #define PGT_CACHE_HIGH 50
142 void check_pgt_cache(void)
145 if (pgtable_cache_size > PGT_CACHE_HIGH) {
148 free_pgd_slow(get_pgd_fast());
149 if (pte_quicklist[0])
150 free_pte_slow(pte_alloc_one_fast(NULL, 0));
151 if (pte_quicklist[1])
152 free_pte_slow(pte_alloc_one_fast(NULL, 1 << (PAGE_SHIFT + 10)));
153 } while (pgtable_cache_size > PGT_CACHE_LOW);
158 #ifdef CONFIG_DEBUG_DCFLUSH
159 atomic_t dcpage_flushes = ATOMIC_INIT(0);
161 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
165 __inline__ void flush_dcache_page_impl(struct page *page)
167 #ifdef CONFIG_DEBUG_DCFLUSH
168 atomic_inc(&dcpage_flushes);
171 #ifdef DCACHE_ALIASING_POSSIBLE
172 __flush_dcache_page(page_address(page),
173 ((tlb_type == spitfire) &&
174 page_mapping(page) != NULL));
176 if (page_mapping(page) != NULL &&
177 tlb_type == spitfire)
178 __flush_icache_page(__pa(page_address(page)));
182 #define PG_dcache_dirty PG_arch_1
183 #define PG_dcache_cpu_shift 24
184 #define PG_dcache_cpu_mask (256 - 1)
187 #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
190 #define dcache_dirty_cpu(page) \
191 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
193 static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
195 unsigned long mask = this_cpu;
196 unsigned long non_cpu_bits;
198 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
199 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
201 __asm__ __volatile__("1:\n\t"
203 "and %%g7, %1, %%g1\n\t"
204 "or %%g1, %0, %%g1\n\t"
205 "casx [%2], %%g7, %%g1\n\t"
207 "membar #StoreLoad | #StoreStore\n\t"
208 "bne,pn %%xcc, 1b\n\t"
211 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
215 static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
217 unsigned long mask = (1UL << PG_dcache_dirty);
219 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
222 "srlx %%g7, %4, %%g1\n\t"
223 "and %%g1, %3, %%g1\n\t"
225 "bne,pn %%icc, 2f\n\t"
226 " andn %%g7, %1, %%g1\n\t"
227 "casx [%2], %%g7, %%g1\n\t"
229 "membar #StoreLoad | #StoreStore\n\t"
230 "bne,pn %%xcc, 1b\n\t"
234 : "r" (cpu), "r" (mask), "r" (&page->flags),
235 "i" (PG_dcache_cpu_mask),
236 "i" (PG_dcache_cpu_shift)
240 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
244 unsigned long pg_flags;
247 if (pfn_valid(pfn) &&
248 (page = pfn_to_page(pfn), page_mapping(page)) &&
249 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
250 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
252 int this_cpu = get_cpu();
254 /* This is just to optimize away some function calls
258 flush_dcache_page_impl(page);
260 smp_flush_dcache_page_impl(page, cpu);
262 clear_dcache_dirty_cpu(page, cpu);
268 void flush_dcache_page(struct page *page)
270 struct address_space *mapping;
273 /* Do not bother with the expensive D-cache flush if it
274 * is merely the zero page. The 'bigcore' testcase in GDB
275 * causes this case to run millions of times.
277 if (page == ZERO_PAGE(0))
280 this_cpu = get_cpu();
282 mapping = page_mapping(page);
283 if (mapping && !mapping_mapped(mapping)) {
284 int dirty = test_bit(PG_dcache_dirty, &page->flags);
286 int dirty_cpu = dcache_dirty_cpu(page);
288 if (dirty_cpu == this_cpu)
290 smp_flush_dcache_page_impl(page, dirty_cpu);
292 set_dcache_dirty(page, this_cpu);
294 /* We could delay the flush for the !page_mapping
295 * case too. But that case is for exec env/arg
296 * pages and those are %99 certainly going to get
297 * faulted into the tlb (and thus flushed) anyways.
299 flush_dcache_page_impl(page);
306 void __kprobes flush_icache_range(unsigned long start, unsigned long end)
308 /* Cheetah has coherent I-cache. */
309 if (tlb_type == spitfire) {
312 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
313 __flush_icache_page(__get_phys(kaddr));
317 unsigned long page_to_pfn(struct page *page)
319 return (unsigned long) ((page - mem_map) + pfn_base);
322 struct page *pfn_to_page(unsigned long pfn)
324 return (mem_map + (pfn - pfn_base));
329 printk("Mem-info:\n");
331 printk("Free swap: %6ldkB\n",
332 nr_swap_pages << (PAGE_SHIFT-10));
333 printk("%ld pages of RAM\n", num_physpages);
334 printk("%d free pages\n", nr_free_pages());
335 printk("%d pages in page table cache\n",pgtable_cache_size);
338 void mmu_info(struct seq_file *m)
340 if (tlb_type == cheetah)
341 seq_printf(m, "MMU Type\t: Cheetah\n");
342 else if (tlb_type == cheetah_plus)
343 seq_printf(m, "MMU Type\t: Cheetah+\n");
344 else if (tlb_type == spitfire)
345 seq_printf(m, "MMU Type\t: Spitfire\n");
347 seq_printf(m, "MMU Type\t: ???\n");
349 #ifdef CONFIG_DEBUG_DCFLUSH
350 seq_printf(m, "DCPageFlushes\t: %d\n",
351 atomic_read(&dcpage_flushes));
353 seq_printf(m, "DCPageFlushesXC\t: %d\n",
354 atomic_read(&dcpage_flushes_xcall));
355 #endif /* CONFIG_SMP */
356 #endif /* CONFIG_DEBUG_DCFLUSH */
359 struct linux_prom_translation {
364 static struct linux_prom_translation prom_trans[512] __initdata;
366 extern unsigned long prom_boot_page;
367 extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
368 extern int prom_get_mmu_ihandle(void);
369 extern void register_prom_callbacks(void);
371 /* Exported for SMP bootup purposes. */
372 unsigned long kern_locked_tte_data;
374 /* Exported for kernel TLB miss handling in ktlb.S */
375 unsigned long prom_pmd_phys __read_mostly;
376 unsigned int swapper_pgd_zero __read_mostly;
378 /* Allocate power-of-2 aligned chunks from the end of the
379 * kernel image. Return physical address.
381 static inline unsigned long early_alloc_phys(unsigned long size)
385 BUILD_BUG_ON(size & (size - 1));
387 kern_size = (kern_size + (size - 1)) & ~(size - 1);
388 base = kern_base + kern_size;
394 static inline unsigned long load_phys32(unsigned long pa)
398 __asm__ __volatile__("lduwa [%1] %2, %0"
400 : "r" (pa), "i" (ASI_PHYS_USE_EC));
405 static inline unsigned long load_phys64(unsigned long pa)
409 __asm__ __volatile__("ldxa [%1] %2, %0"
411 : "r" (pa), "i" (ASI_PHYS_USE_EC));
416 static inline void store_phys32(unsigned long pa, unsigned long val)
418 __asm__ __volatile__("stwa %0, [%1] %2"
420 : "r" (val), "r" (pa), "i" (ASI_PHYS_USE_EC));
423 static inline void store_phys64(unsigned long pa, unsigned long val)
425 __asm__ __volatile__("stxa %0, [%1] %2"
427 : "r" (val), "r" (pa), "i" (ASI_PHYS_USE_EC));
430 #define BASE_PAGE_SIZE 8192
433 * Translate PROM's mapping we capture at boot time into physical address.
434 * The second parameter is only set from prom_callback() invocations.
436 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
438 unsigned long pmd_phys = (prom_pmd_phys +
439 ((promva >> 23) & 0x7ff) * sizeof(pmd_t));
440 unsigned long pte_phys;
445 pmd_val(pmd_ent) = load_phys32(pmd_phys);
446 if (pmd_none(pmd_ent)) {
452 pte_phys = (unsigned long)pmd_val(pmd_ent) << 11UL;
453 pte_phys += ((promva >> 13) & 0x3ff) * sizeof(pte_t);
454 pte_val(pte_ent) = load_phys64(pte_phys);
455 if (!pte_present(pte_ent)) {
462 return pte_val(pte_ent);
464 base = pte_val(pte_ent) & _PAGE_PADDR;
465 return (base + (promva & (BASE_PAGE_SIZE - 1)));
468 /* The obp translations are saved based on 8k pagesize, since obp can
469 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
470 * HI_OBP_ADDRESS range are handled in entry.S and do not use the vpte
471 * scheme (also, see rant in inherit_locked_prom_mappings()).
473 static void __init build_obp_range(unsigned long start, unsigned long end, unsigned long data)
477 for (vaddr = start; vaddr < end; vaddr += BASE_PAGE_SIZE) {
478 unsigned long val, pte_phys, pmd_phys;
482 pmd_phys = (prom_pmd_phys +
483 (((vaddr >> 23) & 0x7ff) * sizeof(pmd_t)));
484 pmd_val(pmd_ent) = load_phys32(pmd_phys);
485 if (pmd_none(pmd_ent)) {
486 pte_phys = early_alloc_phys(BASE_PAGE_SIZE);
488 for (i = 0; i < BASE_PAGE_SIZE / sizeof(pte_t); i++)
489 store_phys64(pte_phys+i*sizeof(pte_t),0);
491 pmd_val(pmd_ent) = pte_phys >> 11UL;
492 store_phys32(pmd_phys, pmd_val(pmd_ent));
495 pte_phys = (unsigned long)pmd_val(pmd_ent) << 11UL;
496 pte_phys += (((vaddr >> 13) & 0x3ff) * sizeof(pte_t));
500 /* Clear diag TTE bits. */
501 if (tlb_type == spitfire)
502 val &= ~0x0003fe0000000000UL;
504 store_phys64(pte_phys, val | _PAGE_MODIFIED);
506 data += BASE_PAGE_SIZE;
510 static inline int in_obp_range(unsigned long vaddr)
512 return (vaddr >= LOW_OBP_ADDRESS &&
513 vaddr < HI_OBP_ADDRESS);
516 #define OBP_PMD_SIZE 2048
517 static void __init build_obp_pgtable(int prom_trans_ents)
521 prom_pmd_phys = early_alloc_phys(OBP_PMD_SIZE);
522 for (i = 0; i < OBP_PMD_SIZE; i += 4)
523 store_phys32(prom_pmd_phys + i, 0);
525 for (i = 0; i < prom_trans_ents; i++) {
526 unsigned long start, end;
528 if (!in_obp_range(prom_trans[i].virt))
531 start = prom_trans[i].virt;
532 end = start + prom_trans[i].size;
533 if (end > HI_OBP_ADDRESS)
534 end = HI_OBP_ADDRESS;
536 build_obp_range(start, end, prom_trans[i].data);
540 /* Read OBP translations property into 'prom_trans[]'.
541 * Return the number of entries.
543 static int __init read_obp_translations(void)
547 node = prom_finddevice("/virtual-memory");
548 n = prom_getproplen(node, "translations");
549 if (unlikely(n == 0 || n == -1)) {
550 prom_printf("prom_mappings: Couldn't get size.\n");
553 if (unlikely(n > sizeof(prom_trans))) {
554 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
558 if ((n = prom_getproperty(node, "translations",
559 (char *)&prom_trans[0],
560 sizeof(prom_trans))) == -1) {
561 prom_printf("prom_mappings: Couldn't get property.\n");
564 n = n / sizeof(struct linux_prom_translation);
568 static void __init remap_kernel(void)
570 unsigned long phys_page, tte_vaddr, tte_data;
571 int tlb_ent = sparc64_highest_locked_tlbent();
573 tte_vaddr = (unsigned long) KERNBASE;
574 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
575 tte_data = (phys_page | (_PAGE_VALID | _PAGE_SZ4MB |
576 _PAGE_CP | _PAGE_CV | _PAGE_P |
579 kern_locked_tte_data = tte_data;
581 /* Now lock us into the TLBs via OBP. */
582 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
583 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
585 prom_dtlb_load(tlb_ent - 1,
587 tte_vaddr + 0x400000);
588 prom_itlb_load(tlb_ent - 1,
590 tte_vaddr + 0x400000);
594 static void __init inherit_prom_mappings(void)
598 n = read_obp_translations();
599 build_obp_pgtable(n);
601 /* Now fixup OBP's idea about where we really are mapped. */
602 prom_printf("Remapping the kernel... ");
605 prom_printf("done.\n");
607 register_prom_callbacks();
610 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
611 * upwards as reserved for use by the firmware (I wonder if this
612 * will be the same on Cheetah...). We use this virtual address
613 * range for the VPTE table mappings of the nucleus so we need
614 * to zap them when we enter the PROM. -DaveM
616 static void __flush_nucleus_vptes(void)
618 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
621 /* Only DTLB must be checked for VPTE entries. */
622 if (tlb_type == spitfire) {
623 for (i = 0; i < 63; i++) {
626 /* Spitfire Errata #32 workaround */
627 /* NOTE: Always runs on spitfire, so no cheetah+
628 * page size encodings.
630 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
634 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
636 tag = spitfire_get_dtlb_tag(i);
637 if (((tag & ~(PAGE_MASK)) == 0) &&
638 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
639 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
642 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
643 spitfire_put_dtlb_data(i, 0x0UL);
646 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
647 for (i = 0; i < 512; i++) {
648 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
650 if ((tag & ~PAGE_MASK) == 0 &&
651 (tag & PAGE_MASK) >= prom_reserved_base) {
652 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
655 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
656 cheetah_put_dtlb_data(i, 0x0UL, 2);
659 if (tlb_type != cheetah_plus)
662 tag = cheetah_get_dtlb_tag(i, 3);
664 if ((tag & ~PAGE_MASK) == 0 &&
665 (tag & PAGE_MASK) >= prom_reserved_base) {
666 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
669 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
670 cheetah_put_dtlb_data(i, 0x0UL, 3);
674 /* Implement me :-) */
679 static int prom_ditlb_set;
680 struct prom_tlb_entry {
682 unsigned long tlb_tag;
683 unsigned long tlb_data;
685 struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
687 void prom_world(int enter)
689 unsigned long pstate;
693 set_fs((mm_segment_t) { get_thread_current_ds() });
698 /* Make sure the following runs atomically. */
699 __asm__ __volatile__("flushw\n\t"
700 "rdpr %%pstate, %0\n\t"
701 "wrpr %0, %1, %%pstate"
706 /* Kick out nucleus VPTEs. */
707 __flush_nucleus_vptes();
709 /* Install PROM world. */
710 for (i = 0; i < 16; i++) {
711 if (prom_dtlb[i].tlb_ent != -1) {
712 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
714 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
716 if (tlb_type == spitfire)
717 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
718 prom_dtlb[i].tlb_data);
719 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
720 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
721 prom_dtlb[i].tlb_data);
723 if (prom_itlb[i].tlb_ent != -1) {
724 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
726 : : "r" (prom_itlb[i].tlb_tag),
727 "r" (TLB_TAG_ACCESS),
729 if (tlb_type == spitfire)
730 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
731 prom_itlb[i].tlb_data);
732 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
733 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
734 prom_itlb[i].tlb_data);
738 for (i = 0; i < 16; i++) {
739 if (prom_dtlb[i].tlb_ent != -1) {
740 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
742 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
743 if (tlb_type == spitfire)
744 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
746 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
748 if (prom_itlb[i].tlb_ent != -1) {
749 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
751 : : "r" (TLB_TAG_ACCESS),
753 if (tlb_type == spitfire)
754 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
756 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
760 __asm__ __volatile__("wrpr %0, 0, %%pstate"
764 void inherit_locked_prom_mappings(int save_p)
770 /* Fucking losing PROM has more mappings in the TLB, but
771 * it (conveniently) fails to mention any of these in the
772 * translations property. The only ones that matter are
773 * the locked PROM tlb entries, so we impose the following
774 * irrecovable rule on the PROM, it is allowed 8 locked
775 * entries in the ITLB and 8 in the DTLB.
777 * Supposedly the upper 16GB of the address space is
778 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
779 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
780 * used between the client program and the firmware on sun5
781 * systems to coordinate mmu mappings is also COMPLETELY
782 * UNDOCUMENTED!!!!!! Thanks S(t)un!
785 for (i = 0; i < 16; i++) {
786 prom_itlb[i].tlb_ent = -1;
787 prom_dtlb[i].tlb_ent = -1;
790 if (tlb_type == spitfire) {
791 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
792 for (i = 0; i < high; i++) {
795 /* Spitfire Errata #32 workaround */
796 /* NOTE: Always runs on spitfire, so no cheetah+
797 * page size encodings.
799 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
803 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
805 data = spitfire_get_dtlb_data(i);
806 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
809 /* Spitfire Errata #32 workaround */
810 /* NOTE: Always runs on spitfire, so no
811 * cheetah+ page size encodings.
813 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
817 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
819 tag = spitfire_get_dtlb_tag(i);
821 prom_dtlb[dtlb_seen].tlb_ent = i;
822 prom_dtlb[dtlb_seen].tlb_tag = tag;
823 prom_dtlb[dtlb_seen].tlb_data = data;
825 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
827 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
828 spitfire_put_dtlb_data(i, 0x0UL);
836 for (i = 0; i < high; i++) {
839 /* Spitfire Errata #32 workaround */
840 /* NOTE: Always runs on spitfire, so no
841 * cheetah+ page size encodings.
843 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
847 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
849 data = spitfire_get_itlb_data(i);
850 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
853 /* Spitfire Errata #32 workaround */
854 /* NOTE: Always runs on spitfire, so no
855 * cheetah+ page size encodings.
857 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
861 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
863 tag = spitfire_get_itlb_tag(i);
865 prom_itlb[itlb_seen].tlb_ent = i;
866 prom_itlb[itlb_seen].tlb_tag = tag;
867 prom_itlb[itlb_seen].tlb_data = data;
869 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
871 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
872 spitfire_put_itlb_data(i, 0x0UL);
879 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
880 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
882 for (i = 0; i < high; i++) {
885 data = cheetah_get_ldtlb_data(i);
886 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
889 tag = cheetah_get_ldtlb_tag(i);
891 prom_dtlb[dtlb_seen].tlb_ent = i;
892 prom_dtlb[dtlb_seen].tlb_tag = tag;
893 prom_dtlb[dtlb_seen].tlb_data = data;
895 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
897 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
898 cheetah_put_ldtlb_data(i, 0x0UL);
906 for (i = 0; i < high; i++) {
909 data = cheetah_get_litlb_data(i);
910 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
913 tag = cheetah_get_litlb_tag(i);
915 prom_itlb[itlb_seen].tlb_ent = i;
916 prom_itlb[itlb_seen].tlb_tag = tag;
917 prom_itlb[itlb_seen].tlb_data = data;
919 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
921 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
922 cheetah_put_litlb_data(i, 0x0UL);
930 /* Implement me :-) */
937 /* Give PROM back his world, done during reboots... */
938 void prom_reload_locked(void)
942 for (i = 0; i < 16; i++) {
943 if (prom_dtlb[i].tlb_ent != -1) {
944 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
946 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
948 if (tlb_type == spitfire)
949 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
950 prom_dtlb[i].tlb_data);
951 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
952 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
953 prom_dtlb[i].tlb_data);
956 if (prom_itlb[i].tlb_ent != -1) {
957 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
959 : : "r" (prom_itlb[i].tlb_tag),
960 "r" (TLB_TAG_ACCESS),
962 if (tlb_type == spitfire)
963 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
964 prom_itlb[i].tlb_data);
966 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
967 prom_itlb[i].tlb_data);
972 #ifdef DCACHE_ALIASING_POSSIBLE
973 void __flush_dcache_range(unsigned long start, unsigned long end)
977 if (tlb_type == spitfire) {
980 for (va = start; va < end; va += 32) {
981 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
988 for (va = start; va < end; va += 32)
989 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
993 "i" (ASI_DCACHE_INVALIDATE));
996 #endif /* DCACHE_ALIASING_POSSIBLE */
998 /* If not locked, zap it. */
999 void __flush_tlb_all(void)
1001 unsigned long pstate;
1004 __asm__ __volatile__("flushw\n\t"
1005 "rdpr %%pstate, %0\n\t"
1006 "wrpr %0, %1, %%pstate"
1009 if (tlb_type == spitfire) {
1010 for (i = 0; i < 64; i++) {
1011 /* Spitfire Errata #32 workaround */
1012 /* NOTE: Always runs on spitfire, so no
1013 * cheetah+ page size encodings.
1015 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1019 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1021 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
1022 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1025 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1026 spitfire_put_dtlb_data(i, 0x0UL);
1029 /* Spitfire Errata #32 workaround */
1030 /* NOTE: Always runs on spitfire, so no
1031 * cheetah+ page size encodings.
1033 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1037 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1039 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
1040 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1043 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1044 spitfire_put_itlb_data(i, 0x0UL);
1047 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1048 cheetah_flush_dtlb_all();
1049 cheetah_flush_itlb_all();
1051 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1055 /* Caller does TLB context flushing on local CPU if necessary.
1056 * The caller also ensures that CTX_VALID(mm->context) is false.
1058 * We must be careful about boundary cases so that we never
1059 * let the user have CTX 0 (nucleus) or we ever use a CTX
1060 * version of zero (and thus NO_CONTEXT would not be caught
1061 * by version mis-match tests in mmu_context.h).
1063 void get_new_mmu_context(struct mm_struct *mm)
1065 unsigned long ctx, new_ctx;
1066 unsigned long orig_pgsz_bits;
1069 spin_lock(&ctx_alloc_lock);
1070 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
1071 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
1072 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
1073 if (new_ctx >= (1 << CTX_NR_BITS)) {
1074 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1075 if (new_ctx >= ctx) {
1077 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1080 new_ctx = CTX_FIRST_VERSION;
1082 /* Don't call memset, for 16 entries that's just
1085 mmu_context_bmap[0] = 3;
1086 mmu_context_bmap[1] = 0;
1087 mmu_context_bmap[2] = 0;
1088 mmu_context_bmap[3] = 0;
1089 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1090 mmu_context_bmap[i + 0] = 0;
1091 mmu_context_bmap[i + 1] = 0;
1092 mmu_context_bmap[i + 2] = 0;
1093 mmu_context_bmap[i + 3] = 0;
1098 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1099 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1101 tlb_context_cache = new_ctx;
1102 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
1103 spin_unlock(&ctx_alloc_lock);
1107 struct pgtable_cache_struct pgt_quicklists;
1110 /* OK, we have to color these pages. The page tables are accessed
1111 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1112 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1113 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1114 * fast handlers do not get data from old/garbage dcache lines that
1115 * correspond to an old/stale virtual address (user/kernel) that
1116 * previously mapped the pagetable page while accessing vpte range
1117 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1118 * color is the same, then when the kernel initializes the pagetable
1119 * using the later address range, accesses with the first address
1120 * range will see the newly initialized data rather than the garbage.
1122 #ifdef DCACHE_ALIASING_POSSIBLE
1123 #define DC_ALIAS_SHIFT 1
1125 #define DC_ALIAS_SHIFT 0
1127 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
1130 unsigned long color;
1133 pte_t *ptep = pte_alloc_one_fast(mm, address);
1139 color = VPTE_COLOR(address);
1140 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, DC_ALIAS_SHIFT);
1142 unsigned long *to_free;
1143 unsigned long paddr;
1146 #ifdef DCACHE_ALIASING_POSSIBLE
1147 set_page_count(page, 1);
1148 ClearPageCompound(page);
1150 set_page_count((page + 1), 1);
1151 ClearPageCompound(page + 1);
1153 paddr = (unsigned long) page_address(page);
1154 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1157 pte = (pte_t *) paddr;
1158 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1160 pte = (pte_t *) (paddr + PAGE_SIZE);
1161 to_free = (unsigned long *) paddr;
1164 #ifdef DCACHE_ALIASING_POSSIBLE
1165 /* Now free the other one up, adjust cache size. */
1167 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1168 pte_quicklist[color ^ 0x1] = to_free;
1169 pgtable_cache_size++;
1178 void sparc_ultra_dump_itlb(void)
1182 if (tlb_type == spitfire) {
1183 printk ("Contents of itlb: ");
1184 for (slot = 0; slot < 14; slot++) printk (" ");
1185 printk ("%2x:%016lx,%016lx\n",
1187 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1188 for (slot = 1; slot < 64; slot+=3) {
1189 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1191 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1193 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1195 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1197 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1198 printk ("Contents of itlb0:\n");
1199 for (slot = 0; slot < 16; slot+=2) {
1200 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1202 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1204 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1206 printk ("Contents of itlb2:\n");
1207 for (slot = 0; slot < 128; slot+=2) {
1208 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1210 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1212 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1217 void sparc_ultra_dump_dtlb(void)
1221 if (tlb_type == spitfire) {
1222 printk ("Contents of dtlb: ");
1223 for (slot = 0; slot < 14; slot++) printk (" ");
1224 printk ("%2x:%016lx,%016lx\n", 0,
1225 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1226 for (slot = 1; slot < 64; slot+=3) {
1227 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1229 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1231 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1233 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1235 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1236 printk ("Contents of dtlb0:\n");
1237 for (slot = 0; slot < 16; slot+=2) {
1238 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1240 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1242 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1244 printk ("Contents of dtlb2:\n");
1245 for (slot = 0; slot < 512; slot+=2) {
1246 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1248 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1250 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1252 if (tlb_type == cheetah_plus) {
1253 printk ("Contents of dtlb3:\n");
1254 for (slot = 0; slot < 512; slot+=2) {
1255 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1257 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1259 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1265 extern unsigned long cmdline_memory_size;
1267 unsigned long __init bootmem_init(unsigned long *pages_avail)
1269 unsigned long bootmap_size, start_pfn, end_pfn;
1270 unsigned long end_of_phys_memory = 0UL;
1271 unsigned long bootmap_pfn, bytes_avail, size;
1274 #ifdef CONFIG_DEBUG_BOOTMEM
1275 prom_printf("bootmem_init: Scan pavail, ");
1279 for (i = 0; i < pavail_ents; i++) {
1280 end_of_phys_memory = pavail[i].phys_addr +
1282 bytes_avail += pavail[i].reg_size;
1283 if (cmdline_memory_size) {
1284 if (bytes_avail > cmdline_memory_size) {
1285 unsigned long slack = bytes_avail - cmdline_memory_size;
1287 bytes_avail -= slack;
1288 end_of_phys_memory -= slack;
1290 pavail[i].reg_size -= slack;
1291 if ((long)pavail[i].reg_size <= 0L) {
1292 pavail[i].phys_addr = 0xdeadbeefUL;
1293 pavail[i].reg_size = 0UL;
1296 pavail[i+1].reg_size = 0Ul;
1297 pavail[i+1].phys_addr = 0xdeadbeefUL;
1298 pavail_ents = i + 1;
1305 *pages_avail = bytes_avail >> PAGE_SHIFT;
1307 /* Start with page aligned address of last symbol in kernel
1308 * image. The kernel is hard mapped below PAGE_OFFSET in a
1309 * 4MB locked TLB translation.
1311 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1313 bootmap_pfn = start_pfn;
1315 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1317 #ifdef CONFIG_BLK_DEV_INITRD
1318 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1319 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
1320 unsigned long ramdisk_image = sparc_ramdisk_image ?
1321 sparc_ramdisk_image : sparc_ramdisk_image64;
1322 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
1323 ramdisk_image -= KERNBASE;
1324 initrd_start = ramdisk_image + phys_base;
1325 initrd_end = initrd_start + sparc_ramdisk_size;
1326 if (initrd_end > end_of_phys_memory) {
1327 printk(KERN_CRIT "initrd extends beyond end of memory "
1328 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1329 initrd_end, end_of_phys_memory);
1333 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1334 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1335 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1339 /* Initialize the boot-time allocator. */
1340 max_pfn = max_low_pfn = end_pfn;
1341 min_low_pfn = pfn_base;
1343 #ifdef CONFIG_DEBUG_BOOTMEM
1344 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1345 min_low_pfn, bootmap_pfn, max_low_pfn);
1347 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
1349 /* Now register the available physical memory with the
1352 for (i = 0; i < pavail_ents; i++) {
1353 #ifdef CONFIG_DEBUG_BOOTMEM
1354 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
1355 i, pavail[i].phys_addr, pavail[i].reg_size);
1357 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
1360 #ifdef CONFIG_BLK_DEV_INITRD
1362 size = initrd_end - initrd_start;
1364 /* Resert the initrd image area. */
1365 #ifdef CONFIG_DEBUG_BOOTMEM
1366 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1367 initrd_start, initrd_end);
1369 reserve_bootmem(initrd_start, size);
1370 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1372 initrd_start += PAGE_OFFSET;
1373 initrd_end += PAGE_OFFSET;
1376 /* Reserve the kernel text/data/bss. */
1377 #ifdef CONFIG_DEBUG_BOOTMEM
1378 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1380 reserve_bootmem(kern_base, kern_size);
1381 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1383 /* Reserve the bootmem map. We do not account for it
1384 * in pages_avail because we will release that memory
1385 * in free_all_bootmem.
1387 size = bootmap_size;
1388 #ifdef CONFIG_DEBUG_BOOTMEM
1389 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1390 (bootmap_pfn << PAGE_SHIFT), size);
1392 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1393 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1398 #ifdef CONFIG_DEBUG_PAGEALLOC
1399 static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
1401 unsigned long vstart = PAGE_OFFSET + pstart;
1402 unsigned long vend = PAGE_OFFSET + pend;
1403 unsigned long alloc_bytes = 0UL;
1405 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
1406 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
1411 while (vstart < vend) {
1412 unsigned long this_end, paddr = __pa(vstart);
1413 pgd_t *pgd = pgd_offset_k(vstart);
1418 pud = pud_offset(pgd, vstart);
1419 if (pud_none(*pud)) {
1422 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1423 alloc_bytes += PAGE_SIZE;
1424 pud_populate(&init_mm, pud, new);
1427 pmd = pmd_offset(pud, vstart);
1428 if (!pmd_present(*pmd)) {
1431 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1432 alloc_bytes += PAGE_SIZE;
1433 pmd_populate_kernel(&init_mm, pmd, new);
1436 pte = pte_offset_kernel(pmd, vstart);
1437 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1438 if (this_end > vend)
1441 while (vstart < this_end) {
1442 pte_val(*pte) = (paddr | pgprot_val(prot));
1444 vstart += PAGE_SIZE;
1453 static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1454 static int pall_ents __initdata;
1456 extern unsigned int kvmap_linear_patch[1];
1458 static void __init kernel_physical_mapping_init(void)
1460 unsigned long i, mem_alloced = 0UL;
1462 read_obp_memory("reg", &pall[0], &pall_ents);
1464 for (i = 0; i < pall_ents; i++) {
1465 unsigned long phys_start, phys_end;
1467 phys_start = pall[i].phys_addr;
1468 phys_end = phys_start + pall[i].reg_size;
1469 mem_alloced += kernel_map_range(phys_start, phys_end,
1473 printk("Allocated %ld bytes for kernel page tables.\n",
1476 kvmap_linear_patch[0] = 0x01000000; /* nop */
1477 flushi(&kvmap_linear_patch[0]);
1482 void kernel_map_pages(struct page *page, int numpages, int enable)
1484 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1485 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1487 kernel_map_range(phys_start, phys_end,
1488 (enable ? PAGE_KERNEL : __pgprot(0)));
1490 /* we should perform an IPI and flush all tlbs,
1491 * but that can deadlock->flush only current cpu.
1493 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1494 PAGE_OFFSET + phys_end);
1498 unsigned long __init find_ecache_flush_span(unsigned long size)
1502 for (i = 0; i < pavail_ents; i++) {
1503 if (pavail[i].reg_size >= size)
1504 return pavail[i].phys_addr;
1510 /* paging_init() sets up the page tables */
1512 extern void cheetah_ecache_flush_init(void);
1514 static unsigned long last_valid_pfn;
1515 pgd_t swapper_pg_dir[2048];
1517 void __init paging_init(void)
1519 unsigned long end_pfn, pages_avail, shift;
1520 unsigned long real_end, i;
1522 /* Find available physical memory... */
1523 read_obp_memory("available", &pavail[0], &pavail_ents);
1525 phys_base = 0xffffffffffffffffUL;
1526 for (i = 0; i < pavail_ents; i++)
1527 phys_base = min(phys_base, pavail[i].phys_addr);
1529 pfn_base = phys_base >> PAGE_SHIFT;
1531 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1532 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1534 set_bit(0, mmu_context_bmap);
1536 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1538 real_end = (unsigned long)_end;
1539 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1541 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1542 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1546 /* Set kernel pgd to upper alias so physical page computations
1549 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1551 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
1553 /* Now can init the kernel/bad page tables. */
1554 pud_set(pud_offset(&swapper_pg_dir[0], 0),
1555 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
1557 swapper_pgd_zero = pgd_val(swapper_pg_dir[0]);
1559 /* Inherit non-locked OBP mappings. */
1560 inherit_prom_mappings();
1562 /* Ok, we can use our TLB miss and window trap handlers safely.
1563 * We need to do a quick peek here to see if we are on StarFire
1564 * or not, so setup_tba can setup the IRQ globals correctly (it
1565 * needs to get the hard smp processor id correctly).
1568 extern void setup_tba(int);
1569 setup_tba(this_is_starfire);
1572 inherit_locked_prom_mappings(1);
1576 /* Setup bootmem... */
1578 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1580 #ifdef CONFIG_DEBUG_PAGEALLOC
1581 kernel_physical_mapping_init();
1585 unsigned long zones_size[MAX_NR_ZONES];
1586 unsigned long zholes_size[MAX_NR_ZONES];
1587 unsigned long npages;
1590 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1591 zones_size[znum] = zholes_size[znum] = 0;
1593 npages = end_pfn - pfn_base;
1594 zones_size[ZONE_DMA] = npages;
1595 zholes_size[ZONE_DMA] = npages - pages_avail;
1597 free_area_init_node(0, &contig_page_data, zones_size,
1598 phys_base >> PAGE_SHIFT, zholes_size);
1604 static void __init taint_real_pages(void)
1608 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
1610 /* Find changes discovered in the physmem available rescan and
1611 * reserve the lost portions in the bootmem maps.
1613 for (i = 0; i < pavail_ents; i++) {
1614 unsigned long old_start, old_end;
1616 old_start = pavail[i].phys_addr;
1617 old_end = old_start +
1619 while (old_start < old_end) {
1622 for (n = 0; pavail_rescan_ents; n++) {
1623 unsigned long new_start, new_end;
1625 new_start = pavail_rescan[n].phys_addr;
1626 new_end = new_start +
1627 pavail_rescan[n].reg_size;
1629 if (new_start <= old_start &&
1630 new_end >= (old_start + PAGE_SIZE)) {
1631 set_bit(old_start >> 22,
1632 sparc64_valid_addr_bitmap);
1636 reserve_bootmem(old_start, PAGE_SIZE);
1639 old_start += PAGE_SIZE;
1644 void __init mem_init(void)
1646 unsigned long codepages, datapages, initpages;
1647 unsigned long addr, last;
1650 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1652 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
1653 if (sparc64_valid_addr_bitmap == NULL) {
1654 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1657 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1659 addr = PAGE_OFFSET + kern_base;
1660 last = PAGE_ALIGN(kern_size) + addr;
1661 while (addr < last) {
1662 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1668 max_mapnr = last_valid_pfn - pfn_base;
1669 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1671 #ifdef CONFIG_DEBUG_BOOTMEM
1672 prom_printf("mem_init: Calling free_all_bootmem().\n");
1674 totalram_pages = num_physpages = free_all_bootmem() - 1;
1677 * Set up the zero page, mark it reserved, so that page count
1678 * is not manipulated when freeing the page from user ptes.
1680 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1681 if (mem_map_zero == NULL) {
1682 prom_printf("paging_init: Cannot alloc zero page.\n");
1685 SetPageReserved(mem_map_zero);
1687 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1688 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1689 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1690 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1691 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1692 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1694 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1695 nr_free_pages() << (PAGE_SHIFT-10),
1696 codepages << (PAGE_SHIFT-10),
1697 datapages << (PAGE_SHIFT-10),
1698 initpages << (PAGE_SHIFT-10),
1699 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1701 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1702 cheetah_ecache_flush_init();
1705 void free_initmem(void)
1707 unsigned long addr, initend;
1710 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1712 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1713 initend = (unsigned long)(__init_end) & PAGE_MASK;
1714 for (; addr < initend; addr += PAGE_SIZE) {
1719 ((unsigned long) __va(kern_base)) -
1720 ((unsigned long) KERNBASE));
1721 memset((void *)addr, 0xcc, PAGE_SIZE);
1722 p = virt_to_page(page);
1724 ClearPageReserved(p);
1725 set_page_count(p, 1);
1732 #ifdef CONFIG_BLK_DEV_INITRD
1733 void free_initrd_mem(unsigned long start, unsigned long end)
1736 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1737 for (; start < end; start += PAGE_SIZE) {
1738 struct page *p = virt_to_page(start);
1740 ClearPageReserved(p);
1741 set_page_count(p, 1);