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>
24 #include <asm/system.h>
26 #include <asm/pgalloc.h>
27 #include <asm/pgtable.h>
28 #include <asm/oplib.h>
29 #include <asm/iommu.h>
31 #include <asm/uaccess.h>
32 #include <asm/mmu_context.h>
33 #include <asm/tlbflush.h>
35 #include <asm/starfire.h>
37 #include <asm/spitfire.h>
38 #include <asm/sections.h>
40 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
42 extern void device_scan(void);
44 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
46 unsigned long *sparc64_valid_addr_bitmap;
48 /* Ugly, but necessary... -DaveM */
49 unsigned long phys_base;
50 unsigned long kern_base;
51 unsigned long kern_size;
52 unsigned long pfn_base;
54 /* This is even uglier. We have a problem where the kernel may not be
55 * located at phys_base. However, initial __alloc_bootmem() calls need to
56 * be adjusted to be within the 4-8Megs that the kernel is mapped to, else
57 * those page mappings wont work. Things are ok after inherit_prom_mappings
58 * is called though. Dave says he'll clean this up some other time.
61 static unsigned long bootmap_base;
63 /* get_new_mmu_context() uses "cache + 1". */
64 spinlock_t ctx_alloc_lock = SPIN_LOCK_UNLOCKED;
65 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
66 #define CTX_BMAP_SLOTS (1UL << (CTX_VERSION_SHIFT - 6))
67 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
69 /* References to special section boundaries */
70 extern char _start[], _end[];
72 /* Initial ramdisk setup */
73 extern unsigned int sparc_ramdisk_image;
74 extern unsigned int sparc_ramdisk_size;
76 struct page *mem_map_zero;
80 /* XXX Tune this... */
81 #define PGT_CACHE_LOW 25
82 #define PGT_CACHE_HIGH 50
84 void check_pgt_cache(void)
87 if (pgtable_cache_size > PGT_CACHE_HIGH) {
91 free_pgd_slow(get_pgd_fast());
94 free_pte_slow(pte_alloc_one_fast(NULL, 0));
96 free_pte_slow(pte_alloc_one_fast(NULL, 1 << (PAGE_SHIFT + 10)));
97 } while (pgtable_cache_size > PGT_CACHE_LOW);
100 if (pgd_cache_size > PGT_CACHE_HIGH / 4) {
101 struct page *page, *page2;
102 for (page2 = NULL, page = (struct page *)pgd_quicklist; page;) {
103 if ((unsigned long)page->lru.prev == 3) {
105 page2->lru.next = page->lru.next;
107 pgd_quicklist = (void *) page->lru.next;
111 page = (struct page *)page2->lru.next;
113 page = (struct page *)pgd_quicklist;
114 if (pgd_cache_size <= PGT_CACHE_LOW / 4)
119 page = (struct page *)page->lru.next;
126 #ifdef CONFIG_DEBUG_DCFLUSH
127 atomic_t dcpage_flushes = ATOMIC_INIT(0);
129 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
133 __inline__ void flush_dcache_page_impl(struct page *page)
135 #ifdef CONFIG_DEBUG_DCFLUSH
136 atomic_inc(&dcpage_flushes);
139 #if (L1DCACHE_SIZE > PAGE_SIZE)
140 __flush_dcache_page(page->virtual,
141 ((tlb_type == spitfire) &&
142 page_mapping(page) != NULL));
144 if (page_mapping(page) != NULL &&
145 tlb_type == spitfire)
146 __flush_icache_page(__pa(page->virtual));
150 #define PG_dcache_dirty PG_arch_1
152 #define dcache_dirty_cpu(page) \
153 (((page)->flags >> 24) & (NR_CPUS - 1UL))
155 static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
157 unsigned long mask = this_cpu;
158 unsigned long non_cpu_bits = ~((NR_CPUS - 1UL) << 24UL);
159 mask = (mask << 24) | (1UL << PG_dcache_dirty);
160 __asm__ __volatile__("1:\n\t"
162 "and %%g7, %1, %%g5\n\t"
163 "or %%g5, %0, %%g5\n\t"
164 "casx [%2], %%g7, %%g5\n\t"
166 "bne,pn %%xcc, 1b\n\t"
167 " membar #StoreLoad | #StoreStore"
169 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
173 static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
175 unsigned long mask = (1UL << PG_dcache_dirty);
177 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
180 "srlx %%g7, 24, %%g5\n\t"
181 "and %%g5, %3, %%g5\n\t"
183 "bne,pn %%icc, 2f\n\t"
184 " andn %%g7, %1, %%g5\n\t"
185 "casx [%2], %%g7, %%g5\n\t"
187 "bne,pn %%xcc, 1b\n\t"
188 " membar #StoreLoad | #StoreStore\n"
191 : "r" (cpu), "r" (mask), "r" (&page->flags),
196 extern void __update_mmu_cache(unsigned long mmu_context_hw, unsigned long address, pte_t pte, int code);
198 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
202 unsigned long pg_flags;
205 if (pfn_valid(pfn) &&
206 (page = pfn_to_page(pfn), page_mapping(page)) &&
207 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
208 int cpu = ((pg_flags >> 24) & (NR_CPUS - 1UL));
209 int this_cpu = get_cpu();
211 /* This is just to optimize away some function calls
215 flush_dcache_page_impl(page);
217 smp_flush_dcache_page_impl(page, cpu);
219 clear_dcache_dirty_cpu(page, cpu);
223 if (get_thread_fault_code())
224 __update_mmu_cache(vma->vm_mm->context & TAG_CONTEXT_BITS,
225 address, pte, get_thread_fault_code());
228 void flush_dcache_page(struct page *page)
230 struct address_space *mapping = page_mapping(page);
231 int dirty = test_bit(PG_dcache_dirty, &page->flags);
232 int dirty_cpu = dcache_dirty_cpu(page);
233 int this_cpu = get_cpu();
235 if (mapping && !mapping_mapped(mapping)) {
237 if (dirty_cpu == this_cpu)
239 smp_flush_dcache_page_impl(page, dirty_cpu);
241 set_dcache_dirty(page, this_cpu);
243 /* We could delay the flush for the !page_mapping
244 * case too. But that case is for exec env/arg
245 * pages and those are %99 certainly going to get
246 * faulted into the tlb (and thus flushed) anyways.
248 flush_dcache_page_impl(page);
255 /* When shared+writable mmaps of files go away, we lose all dirty
256 * page state, so we have to deal with D-cache aliasing here.
258 * This code relies on the fact that flush_cache_range() is always
259 * called for an area composed by a single VMA. It also assumes that
260 * the MM's page_table_lock is held.
262 static inline void flush_cache_pte_range(struct mm_struct *mm, pmd_t *pmd, unsigned long address, unsigned long size)
264 unsigned long offset;
269 ptep = pte_offset_map(pmd, address);
270 offset = address & ~PMD_MASK;
271 if (offset + size > PMD_SIZE)
272 size = PMD_SIZE - offset;
274 for (offset = 0; offset < size; ptep++, offset += PAGE_SIZE) {
280 if (pte_present(pte) && pte_dirty(pte)) {
282 unsigned long pgaddr, uaddr;
283 unsigned long pfn = pte_pfn(pte);
287 page = pfn_to_page(pfn);
288 if (PageReserved(page) || !page_mapping(page))
290 pgaddr = (unsigned long) page_address(page);
291 uaddr = address + offset;
292 if ((pgaddr ^ uaddr) & (1 << 13))
293 flush_dcache_page_all(mm, page);
299 static inline void flush_cache_pmd_range(struct mm_struct *mm, pgd_t *dir, unsigned long address, unsigned long size)
306 pmd = pmd_offset(dir, address);
307 end = address + size;
308 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
309 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
311 flush_cache_pte_range(mm, pmd, address, end - address);
312 address = (address + PMD_SIZE) & PMD_MASK;
314 } while (address < end);
317 void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
319 struct mm_struct *mm = vma->vm_mm;
320 pgd_t *dir = pgd_offset(mm, start);
322 if (mm == current->mm)
325 if (vma->vm_file == NULL ||
326 ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE)))
330 flush_cache_pmd_range(mm, dir, start, end - start);
331 start = (start + PGDIR_SIZE) & PGDIR_MASK;
333 } while (start && (start < end));
336 void flush_icache_range(unsigned long start, unsigned long end)
338 /* Cheetah has coherent I-cache. */
339 if (tlb_type == spitfire) {
342 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
343 __flush_icache_page(__get_phys(kaddr));
349 printk("Mem-info:\n");
351 printk("Free swap: %6dkB\n",
352 nr_swap_pages << (PAGE_SHIFT-10));
353 printk("%ld pages of RAM\n", num_physpages);
354 printk("%d free pages\n", nr_free_pages());
355 printk("%d pages in page table cache\n",pgtable_cache_size);
357 printk("%d entries in page dir cache\n",pgd_cache_size);
361 void mmu_info(struct seq_file *m)
363 if (tlb_type == cheetah)
364 seq_printf(m, "MMU Type\t: Cheetah\n");
365 else if (tlb_type == cheetah_plus)
366 seq_printf(m, "MMU Type\t: Cheetah+\n");
367 else if (tlb_type == spitfire)
368 seq_printf(m, "MMU Type\t: Spitfire\n");
370 seq_printf(m, "MMU Type\t: ???\n");
372 #ifdef CONFIG_DEBUG_DCFLUSH
373 seq_printf(m, "DCPageFlushes\t: %d\n",
374 atomic_read(&dcpage_flushes));
376 seq_printf(m, "DCPageFlushesXC\t: %d\n",
377 atomic_read(&dcpage_flushes_xcall));
378 #endif /* CONFIG_SMP */
379 #endif /* CONFIG_DEBUG_DCFLUSH */
382 struct linux_prom_translation {
388 extern unsigned long prom_boot_page;
389 extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
390 extern int prom_get_mmu_ihandle(void);
391 extern void register_prom_callbacks(void);
393 /* Exported for SMP bootup purposes. */
394 unsigned long kern_locked_tte_data;
396 void __init early_pgtable_allocfail(char *type)
398 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
402 #define BASE_PAGE_SIZE 8192
403 static pmd_t *prompmd;
406 * Translate PROM's mapping we capture at boot time into physical address.
407 * The second parameter is only set from prom_callback() invocations.
409 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
411 pmd_t *pmdp = prompmd + ((promva >> 23) & 0x7ff);
415 if (pmd_none(*pmdp)) {
420 ptep = (pte_t *)__pmd_page(*pmdp) + ((promva >> 13) & 0x3ff);
421 if (!pte_present(*ptep)) {
428 return(pte_val(*ptep));
430 base = pte_val(*ptep) & _PAGE_PADDR;
431 return(base + (promva & (BASE_PAGE_SIZE - 1)));
434 static void inherit_prom_mappings(void)
436 struct linux_prom_translation *trans;
437 unsigned long phys_page, tte_vaddr, tte_data;
438 void (*remap_func)(unsigned long, unsigned long, int);
442 extern unsigned int obp_iaddr_patch[2], obp_daddr_patch[2];
444 node = prom_finddevice("/virtual-memory");
445 n = prom_getproplen(node, "translations");
446 if (n == 0 || n == -1) {
447 prom_printf("Couldn't get translation property\n");
450 n += 5 * sizeof(struct linux_prom_translation);
451 for (tsz = 1; tsz < n; tsz <<= 1)
453 trans = __alloc_bootmem(tsz, SMP_CACHE_BYTES, bootmap_base);
455 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
458 memset(trans, 0, tsz);
460 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
461 prom_printf("Couldn't get translation property\n");
464 n = n / sizeof(*trans);
467 * The obp translations are saved based on 8k pagesize, since obp can use
468 * a mixture of pagesizes. Misses to the 0xf0000000 - 0x100000000, ie obp
469 * range, are handled in entry.S and do not use the vpte scheme (see rant
470 * in inherit_locked_prom_mappings()).
472 #define OBP_PMD_SIZE 2048
473 prompmd = __alloc_bootmem(OBP_PMD_SIZE, OBP_PMD_SIZE, bootmap_base);
475 early_pgtable_allocfail("pmd");
476 memset(prompmd, 0, OBP_PMD_SIZE);
477 for (i = 0; i < n; i++) {
480 if (trans[i].virt >= LOW_OBP_ADDRESS && trans[i].virt < HI_OBP_ADDRESS) {
481 for (vaddr = trans[i].virt;
482 ((vaddr < trans[i].virt + trans[i].size) &&
483 (vaddr < HI_OBP_ADDRESS));
484 vaddr += BASE_PAGE_SIZE) {
487 pmdp = prompmd + ((vaddr >> 23) & 0x7ff);
488 if (pmd_none(*pmdp)) {
489 ptep = __alloc_bootmem(BASE_PAGE_SIZE,
493 early_pgtable_allocfail("pte");
494 memset(ptep, 0, BASE_PAGE_SIZE);
497 ptep = (pte_t *)__pmd_page(*pmdp) +
498 ((vaddr >> 13) & 0x3ff);
502 /* Clear diag TTE bits. */
503 if (tlb_type == spitfire)
504 val &= ~0x0003fe0000000000UL;
506 set_pte (ptep, __pte(val | _PAGE_MODIFIED));
507 trans[i].data += BASE_PAGE_SIZE;
511 phys_page = __pa(prompmd);
512 obp_iaddr_patch[0] |= (phys_page >> 10);
513 obp_iaddr_patch[1] |= (phys_page & 0x3ff);
514 flushi((long)&obp_iaddr_patch[0]);
515 obp_daddr_patch[0] |= (phys_page >> 10);
516 obp_daddr_patch[1] |= (phys_page & 0x3ff);
517 flushi((long)&obp_daddr_patch[0]);
519 /* Now fixup OBP's idea about where we really are mapped. */
520 prom_printf("Remapping the kernel... ");
522 /* Spitfire Errata #32 workaround */
523 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
527 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
532 phys_page = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
537 phys_page = cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
541 phys_page &= _PAGE_PADDR;
542 phys_page += ((unsigned long)&prom_boot_page -
543 (unsigned long)KERNBASE);
545 if (tlb_type == spitfire) {
546 /* Lock this into i/d tlb entry 59 */
547 __asm__ __volatile__(
548 "stxa %%g0, [%2] %3\n\t"
549 "stxa %0, [%1] %4\n\t"
552 "stxa %%g0, [%2] %5\n\t"
553 "stxa %0, [%1] %6\n\t"
556 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
557 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
558 "r" (59 << 3), "r" (TLB_TAG_ACCESS),
559 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
560 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
562 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
563 /* Lock this into i/d tlb-0 entry 11 */
564 __asm__ __volatile__(
565 "stxa %%g0, [%2] %3\n\t"
566 "stxa %0, [%1] %4\n\t"
569 "stxa %%g0, [%2] %5\n\t"
570 "stxa %0, [%1] %6\n\t"
573 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
574 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
575 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS),
576 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
577 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
580 /* Implement me :-) */
584 tte_vaddr = (unsigned long) KERNBASE;
586 /* Spitfire Errata #32 workaround */
587 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
591 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
593 if (tlb_type == spitfire)
594 tte_data = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
596 tte_data = cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
598 kern_locked_tte_data = tte_data;
600 remap_func = (void *) ((unsigned long) &prom_remap -
601 (unsigned long) &prom_boot_page);
604 /* Spitfire Errata #32 workaround */
605 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
609 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
611 remap_func((tlb_type == spitfire ?
612 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR) :
613 (cheetah_get_litlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR)),
614 (unsigned long) KERNBASE,
615 prom_get_mmu_ihandle());
618 remap_func(((tte_data + 0x400000) & _PAGE_PADDR),
619 (unsigned long) KERNBASE + 0x400000, prom_get_mmu_ihandle());
621 /* Flush out that temporary mapping. */
622 spitfire_flush_dtlb_nucleus_page(0x0);
623 spitfire_flush_itlb_nucleus_page(0x0);
625 /* Now lock us back into the TLBs via OBP. */
626 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
627 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
629 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
630 tte_vaddr + 0x400000);
631 prom_itlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
632 tte_vaddr + 0x400000);
635 /* Re-read translations property. */
636 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
637 prom_printf("Couldn't get translation property\n");
640 n = n / sizeof(*trans);
642 for (i = 0; i < n; i++) {
643 unsigned long vaddr = trans[i].virt;
644 unsigned long size = trans[i].size;
646 if (vaddr < 0xf0000000UL) {
647 unsigned long avoid_start = (unsigned long) KERNBASE;
648 unsigned long avoid_end = avoid_start + (4 * 1024 * 1024);
651 avoid_end += (4 * 1024 * 1024);
652 if (vaddr < avoid_start) {
653 unsigned long top = vaddr + size;
655 if (top > avoid_start)
657 prom_unmap(top - vaddr, vaddr);
659 if ((vaddr + size) > avoid_end) {
660 unsigned long bottom = vaddr;
662 if (bottom < avoid_end)
664 prom_unmap((vaddr + size) - bottom, bottom);
669 prom_printf("done.\n");
671 register_prom_callbacks();
674 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
675 * upwards as reserved for use by the firmware (I wonder if this
676 * will be the same on Cheetah...). We use this virtual address
677 * range for the VPTE table mappings of the nucleus so we need
678 * to zap them when we enter the PROM. -DaveM
680 static void __flush_nucleus_vptes(void)
682 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
685 /* Only DTLB must be checked for VPTE entries. */
686 if (tlb_type == spitfire) {
687 for (i = 0; i < 63; i++) {
690 /* Spitfire Errata #32 workaround */
691 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
695 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
697 tag = spitfire_get_dtlb_tag(i);
698 if (((tag & ~(PAGE_MASK)) == 0) &&
699 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
700 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
703 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
704 spitfire_put_dtlb_data(i, 0x0UL);
707 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
708 for (i = 0; i < 512; i++) {
709 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
711 if ((tag & ~PAGE_MASK) == 0 &&
712 (tag & PAGE_MASK) >= prom_reserved_base) {
713 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
716 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
717 cheetah_put_dtlb_data(i, 0x0UL, 2);
720 if (tlb_type != cheetah_plus)
723 tag = cheetah_get_dtlb_tag(i, 3);
725 if ((tag & ~PAGE_MASK) == 0 &&
726 (tag & PAGE_MASK) >= prom_reserved_base) {
727 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
730 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
731 cheetah_put_dtlb_data(i, 0x0UL, 3);
735 /* Implement me :-) */
740 static int prom_ditlb_set;
741 struct prom_tlb_entry {
743 unsigned long tlb_tag;
744 unsigned long tlb_data;
746 struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
748 void prom_world(int enter)
750 unsigned long pstate;
754 set_fs((mm_segment_t) { get_thread_current_ds() });
759 /* Make sure the following runs atomically. */
760 __asm__ __volatile__("flushw\n\t"
761 "rdpr %%pstate, %0\n\t"
762 "wrpr %0, %1, %%pstate"
767 /* Kick out nucleus VPTEs. */
768 __flush_nucleus_vptes();
770 /* Install PROM world. */
771 for (i = 0; i < 16; i++) {
772 if (prom_dtlb[i].tlb_ent != -1) {
773 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
775 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
777 if (tlb_type == spitfire)
778 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
779 prom_dtlb[i].tlb_data);
780 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
781 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
782 prom_dtlb[i].tlb_data);
784 if (prom_itlb[i].tlb_ent != -1) {
785 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
787 : : "r" (prom_itlb[i].tlb_tag),
788 "r" (TLB_TAG_ACCESS),
790 if (tlb_type == spitfire)
791 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
792 prom_itlb[i].tlb_data);
793 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
794 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
795 prom_itlb[i].tlb_data);
799 for (i = 0; i < 16; i++) {
800 if (prom_dtlb[i].tlb_ent != -1) {
801 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
803 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
804 if (tlb_type == spitfire)
805 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
807 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
809 if (prom_itlb[i].tlb_ent != -1) {
810 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
812 : : "r" (TLB_TAG_ACCESS),
814 if (tlb_type == spitfire)
815 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
817 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
821 __asm__ __volatile__("wrpr %0, 0, %%pstate"
825 void inherit_locked_prom_mappings(int save_p)
831 /* Fucking losing PROM has more mappings in the TLB, but
832 * it (conveniently) fails to mention any of these in the
833 * translations property. The only ones that matter are
834 * the locked PROM tlb entries, so we impose the following
835 * irrecovable rule on the PROM, it is allowed 8 locked
836 * entries in the ITLB and 8 in the DTLB.
838 * Supposedly the upper 16GB of the address space is
839 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
840 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
841 * used between the client program and the firmware on sun5
842 * systems to coordinate mmu mappings is also COMPLETELY
843 * UNDOCUMENTED!!!!!! Thanks S(t)un!
846 for (i = 0; i < 16; i++) {
847 prom_itlb[i].tlb_ent = -1;
848 prom_dtlb[i].tlb_ent = -1;
851 if (tlb_type == spitfire) {
852 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
853 for (i = 0; i < high; i++) {
856 /* Spitfire Errata #32 workaround */
857 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
861 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
863 data = spitfire_get_dtlb_data(i);
864 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
867 /* Spitfire Errata #32 workaround */
868 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
872 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
874 tag = spitfire_get_dtlb_tag(i);
876 prom_dtlb[dtlb_seen].tlb_ent = i;
877 prom_dtlb[dtlb_seen].tlb_tag = tag;
878 prom_dtlb[dtlb_seen].tlb_data = data;
880 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
882 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
883 spitfire_put_dtlb_data(i, 0x0UL);
891 for (i = 0; i < high; i++) {
894 /* Spitfire Errata #32 workaround */
895 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
899 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
901 data = spitfire_get_itlb_data(i);
902 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
905 /* Spitfire Errata #32 workaround */
906 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
910 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
912 tag = spitfire_get_itlb_tag(i);
914 prom_itlb[itlb_seen].tlb_ent = i;
915 prom_itlb[itlb_seen].tlb_tag = tag;
916 prom_itlb[itlb_seen].tlb_data = data;
918 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
920 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
921 spitfire_put_itlb_data(i, 0x0UL);
928 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
929 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
931 for (i = 0; i < high; i++) {
934 data = cheetah_get_ldtlb_data(i);
935 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
938 tag = cheetah_get_ldtlb_tag(i);
940 prom_dtlb[dtlb_seen].tlb_ent = i;
941 prom_dtlb[dtlb_seen].tlb_tag = tag;
942 prom_dtlb[dtlb_seen].tlb_data = data;
944 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
946 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
947 cheetah_put_ldtlb_data(i, 0x0UL);
955 for (i = 0; i < high; i++) {
958 data = cheetah_get_litlb_data(i);
959 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
962 tag = cheetah_get_litlb_tag(i);
964 prom_itlb[itlb_seen].tlb_ent = i;
965 prom_itlb[itlb_seen].tlb_tag = tag;
966 prom_itlb[itlb_seen].tlb_data = data;
968 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
970 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
971 cheetah_put_litlb_data(i, 0x0UL);
979 /* Implement me :-) */
986 /* Give PROM back his world, done during reboots... */
987 void prom_reload_locked(void)
991 for (i = 0; i < 16; i++) {
992 if (prom_dtlb[i].tlb_ent != -1) {
993 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
995 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
997 if (tlb_type == spitfire)
998 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
999 prom_dtlb[i].tlb_data);
1000 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
1001 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
1002 prom_dtlb[i].tlb_data);
1005 if (prom_itlb[i].tlb_ent != -1) {
1006 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1008 : : "r" (prom_itlb[i].tlb_tag),
1009 "r" (TLB_TAG_ACCESS),
1011 if (tlb_type == spitfire)
1012 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
1013 prom_itlb[i].tlb_data);
1015 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
1016 prom_itlb[i].tlb_data);
1021 void __flush_dcache_range(unsigned long start, unsigned long end)
1025 if (tlb_type == spitfire) {
1028 for (va = start; va < end; va += 32) {
1029 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
1034 start = __pa(start);
1036 for (va = start; va < end; va += 32)
1037 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1041 "i" (ASI_DCACHE_INVALIDATE));
1045 /* If not locked, zap it. */
1046 void __flush_tlb_all(void)
1048 unsigned long pstate;
1051 __asm__ __volatile__("flushw\n\t"
1052 "rdpr %%pstate, %0\n\t"
1053 "wrpr %0, %1, %%pstate"
1056 if (tlb_type == spitfire) {
1057 for (i = 0; i < 64; i++) {
1058 /* Spitfire Errata #32 workaround */
1059 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1063 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1065 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
1066 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1069 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1070 spitfire_put_dtlb_data(i, 0x0UL);
1073 /* Spitfire Errata #32 workaround */
1074 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1078 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1080 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
1081 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1084 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1085 spitfire_put_itlb_data(i, 0x0UL);
1088 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1089 cheetah_flush_dtlb_all();
1090 cheetah_flush_itlb_all();
1092 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1096 /* Caller does TLB context flushing on local CPU if necessary.
1097 * The caller also ensures that CTX_VALID(mm->context) is false.
1099 * We must be careful about boundary cases so that we never
1100 * let the user have CTX 0 (nucleus) or we ever use a CTX
1101 * version of zero (and thus NO_CONTEXT would not be caught
1102 * by version mis-match tests in mmu_context.h).
1104 void get_new_mmu_context(struct mm_struct *mm)
1106 unsigned long ctx, new_ctx;
1108 spin_lock(&ctx_alloc_lock);
1109 ctx = CTX_HWBITS(tlb_context_cache + 1);
1110 new_ctx = find_next_zero_bit(mmu_context_bmap, 1UL << CTX_VERSION_SHIFT, ctx);
1111 if (new_ctx >= (1UL << CTX_VERSION_SHIFT)) {
1112 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1113 if (new_ctx >= ctx) {
1115 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1118 new_ctx = CTX_FIRST_VERSION;
1120 /* Don't call memset, for 16 entries that's just
1123 mmu_context_bmap[0] = 3;
1124 mmu_context_bmap[1] = 0;
1125 mmu_context_bmap[2] = 0;
1126 mmu_context_bmap[3] = 0;
1127 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1128 mmu_context_bmap[i + 0] = 0;
1129 mmu_context_bmap[i + 1] = 0;
1130 mmu_context_bmap[i + 2] = 0;
1131 mmu_context_bmap[i + 3] = 0;
1136 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1137 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1139 tlb_context_cache = new_ctx;
1140 spin_unlock(&ctx_alloc_lock);
1142 mm->context = new_ctx;
1146 struct pgtable_cache_struct pgt_quicklists;
1149 /* OK, we have to color these pages. The page tables are accessed
1150 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1151 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1152 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1153 * fast handlers do not get data from old/garbage dcache lines that
1154 * correspond to an old/stale virtual address (user/kernel) that
1155 * previously mapped the pagetable page while accessing vpte range
1156 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1157 * color is the same, then when the kernel initializes the pagetable
1158 * using the later address range, accesses with the first address
1159 * range will see the newly initialized data rather than the garbage.
1161 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1162 #define DC_ALIAS_SHIFT 1
1164 #define DC_ALIAS_SHIFT 0
1166 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
1169 unsigned long color;
1172 pte_t *ptep = pte_alloc_one_fast(mm, address);
1178 color = VPTE_COLOR(address);
1179 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, DC_ALIAS_SHIFT);
1181 unsigned long *to_free;
1182 unsigned long paddr;
1185 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1186 set_page_count(page, 1);
1187 ClearPageCompound(page);
1189 set_page_count((page + 1), 1);
1190 ClearPageCompound(page + 1);
1192 paddr = (unsigned long) page_address(page);
1193 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1196 pte = (pte_t *) paddr;
1197 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1199 pte = (pte_t *) (paddr + PAGE_SIZE);
1200 to_free = (unsigned long *) paddr;
1203 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1204 /* Now free the other one up, adjust cache size. */
1206 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1207 pte_quicklist[color ^ 0x1] = to_free;
1208 pgtable_cache_size++;
1217 void sparc_ultra_dump_itlb(void)
1221 if (tlb_type == spitfire) {
1222 printk ("Contents of itlb: ");
1223 for (slot = 0; slot < 14; slot++) printk (" ");
1224 printk ("%2x:%016lx,%016lx\n",
1226 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1227 for (slot = 1; slot < 64; slot+=3) {
1228 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1230 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1232 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1234 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1236 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1237 printk ("Contents of itlb0:\n");
1238 for (slot = 0; slot < 16; slot+=2) {
1239 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1241 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1243 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1245 printk ("Contents of itlb2:\n");
1246 for (slot = 0; slot < 128; slot+=2) {
1247 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1249 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1251 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1256 void sparc_ultra_dump_dtlb(void)
1260 if (tlb_type == spitfire) {
1261 printk ("Contents of dtlb: ");
1262 for (slot = 0; slot < 14; slot++) printk (" ");
1263 printk ("%2x:%016lx,%016lx\n", 0,
1264 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1265 for (slot = 1; slot < 64; slot+=3) {
1266 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1268 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1270 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1272 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1274 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1275 printk ("Contents of dtlb0:\n");
1276 for (slot = 0; slot < 16; slot+=2) {
1277 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1279 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1281 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1283 printk ("Contents of dtlb2:\n");
1284 for (slot = 0; slot < 512; slot+=2) {
1285 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1287 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1289 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1291 if (tlb_type == cheetah_plus) {
1292 printk ("Contents of dtlb3:\n");
1293 for (slot = 0; slot < 512; slot+=2) {
1294 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1296 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1298 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1304 extern unsigned long cmdline_memory_size;
1306 unsigned long __init bootmem_init(unsigned long *pages_avail)
1308 unsigned long bootmap_size, start_pfn, end_pfn;
1309 unsigned long end_of_phys_memory = 0UL;
1310 unsigned long bootmap_pfn, bytes_avail, size;
1313 #ifdef CONFIG_DEBUG_BOOTMEM
1314 prom_printf("bootmem_init: Scan sp_banks, ");
1318 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1319 end_of_phys_memory = sp_banks[i].base_addr +
1320 sp_banks[i].num_bytes;
1321 bytes_avail += sp_banks[i].num_bytes;
1322 if (cmdline_memory_size) {
1323 if (bytes_avail > cmdline_memory_size) {
1324 unsigned long slack = bytes_avail - cmdline_memory_size;
1326 bytes_avail -= slack;
1327 end_of_phys_memory -= slack;
1329 sp_banks[i].num_bytes -= slack;
1330 if (sp_banks[i].num_bytes == 0) {
1331 sp_banks[i].base_addr = 0xdeadbeef;
1333 sp_banks[i+1].num_bytes = 0;
1334 sp_banks[i+1].base_addr = 0xdeadbeef;
1341 *pages_avail = bytes_avail >> PAGE_SHIFT;
1343 /* Start with page aligned address of last symbol in kernel
1344 * image. The kernel is hard mapped below PAGE_OFFSET in a
1345 * 4MB locked TLB translation.
1347 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1349 bootmap_pfn = start_pfn;
1351 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1353 #ifdef CONFIG_BLK_DEV_INITRD
1354 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1355 if (sparc_ramdisk_image) {
1356 if (sparc_ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
1357 sparc_ramdisk_image -= KERNBASE;
1358 initrd_start = sparc_ramdisk_image + phys_base;
1359 initrd_end = initrd_start + sparc_ramdisk_size;
1360 if (initrd_end > end_of_phys_memory) {
1361 printk(KERN_CRIT "initrd extends beyond end of memory "
1362 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1363 initrd_end, end_of_phys_memory);
1367 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1368 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1369 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1373 /* Initialize the boot-time allocator. */
1374 max_pfn = max_low_pfn = end_pfn;
1375 min_low_pfn = pfn_base;
1377 #ifdef CONFIG_DEBUG_BOOTMEM
1378 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1379 min_low_pfn, bootmap_pfn, max_low_pfn);
1381 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
1383 bootmap_base = bootmap_pfn << PAGE_SHIFT;
1385 /* Now register the available physical memory with the
1388 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1389 #ifdef CONFIG_DEBUG_BOOTMEM
1390 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1391 i, sp_banks[i].base_addr, sp_banks[i].num_bytes);
1393 free_bootmem(sp_banks[i].base_addr, sp_banks[i].num_bytes);
1396 #ifdef CONFIG_BLK_DEV_INITRD
1398 size = initrd_end - initrd_start;
1400 /* Resert the initrd image area. */
1401 reserve_bootmem(initrd_start, size);
1402 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1404 initrd_start += PAGE_OFFSET;
1405 initrd_end += PAGE_OFFSET;
1408 /* Reserve the kernel text/data/bss. */
1409 #ifdef CONFIG_DEBUG_BOOTMEM
1410 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1412 reserve_bootmem(kern_base, kern_size);
1413 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1415 /* Reserve the bootmem map. We do not account for it
1416 * in pages_avail because we will release that memory
1417 * in free_all_bootmem.
1419 size = bootmap_size;
1420 #ifdef CONFIG_DEBUG_BOOTMEM
1421 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1422 (bootmap_pfn << PAGE_SHIFT), size);
1424 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1425 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1430 /* paging_init() sets up the page tables */
1432 extern void cheetah_ecache_flush_init(void);
1434 static unsigned long last_valid_pfn;
1436 void __init paging_init(void)
1438 extern pmd_t swapper_pmd_dir[1024];
1439 extern unsigned int sparc64_vpte_patchme1[1];
1440 extern unsigned int sparc64_vpte_patchme2[1];
1441 unsigned long alias_base = kern_base + PAGE_OFFSET;
1442 unsigned long second_alias_page = 0;
1443 unsigned long pt, flags, end_pfn, pages_avail;
1444 unsigned long shift = alias_base - ((unsigned long)KERNBASE);
1445 unsigned long real_end;
1447 set_bit(0, mmu_context_bmap);
1449 real_end = (unsigned long)_end;
1450 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1452 #ifdef CONFIG_BLK_DEV_INITRD
1453 if (sparc_ramdisk_image)
1454 real_end = (PAGE_ALIGN(real_end) + PAGE_ALIGN(sparc_ramdisk_size));
1457 /* We assume physical memory starts at some 4mb multiple,
1458 * if this were not true we wouldn't boot up to this point
1461 pt = kern_base | _PAGE_VALID | _PAGE_SZ4MB;
1462 pt |= _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W;
1463 local_irq_save(flags);
1464 if (tlb_type == spitfire) {
1465 __asm__ __volatile__(
1466 " stxa %1, [%0] %3\n"
1467 " stxa %2, [%5] %4\n"
1474 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1475 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (61 << 3)
1477 if (real_end >= KERNBASE + 0x340000) {
1478 second_alias_page = alias_base + 0x400000;
1479 __asm__ __volatile__(
1480 " stxa %1, [%0] %3\n"
1481 " stxa %2, [%5] %4\n"
1488 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1489 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (60 << 3)
1492 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1493 __asm__ __volatile__(
1494 " stxa %1, [%0] %3\n"
1495 " stxa %2, [%5] %4\n"
1502 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1503 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (13<<3))
1505 if (real_end >= KERNBASE + 0x340000) {
1506 second_alias_page = alias_base + 0x400000;
1507 __asm__ __volatile__(
1508 " stxa %1, [%0] %3\n"
1509 " stxa %2, [%5] %4\n"
1516 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1517 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (12<<3))
1521 local_irq_restore(flags);
1523 /* Now set kernel pgd to upper alias so physical page computations
1526 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1528 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1530 /* Now can init the kernel/bad page tables. */
1531 pgd_set(&swapper_pg_dir[0], swapper_pmd_dir + (shift / sizeof(pgd_t)));
1533 sparc64_vpte_patchme1[0] |=
1534 (((unsigned long)pgd_val(init_mm.pgd[0])) >> 10);
1535 sparc64_vpte_patchme2[0] |=
1536 (((unsigned long)pgd_val(init_mm.pgd[0])) & 0x3ff);
1537 flushi((long)&sparc64_vpte_patchme1[0]);
1539 /* Setup bootmem... */
1541 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1543 /* Inherit non-locked OBP mappings. */
1544 inherit_prom_mappings();
1546 /* Ok, we can use our TLB miss and window trap handlers safely.
1547 * We need to do a quick peek here to see if we are on StarFire
1548 * or not, so setup_tba can setup the IRQ globals correctly (it
1549 * needs to get the hard smp processor id correctly).
1552 extern void setup_tba(int);
1553 setup_tba(this_is_starfire);
1556 inherit_locked_prom_mappings(1);
1558 /* We only created DTLB mapping of this stuff. */
1559 spitfire_flush_dtlb_nucleus_page(alias_base);
1560 if (second_alias_page)
1561 spitfire_flush_dtlb_nucleus_page(second_alias_page);
1566 unsigned long zones_size[MAX_NR_ZONES];
1567 unsigned long zholes_size[MAX_NR_ZONES];
1568 unsigned long npages;
1571 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1572 zones_size[znum] = zholes_size[znum] = 0;
1574 npages = end_pfn - pfn_base;
1575 zones_size[ZONE_DMA] = npages;
1576 zholes_size[ZONE_DMA] = npages - pages_avail;
1578 free_area_init_node(0, &contig_page_data, NULL, zones_size,
1579 phys_base >> PAGE_SHIFT, zholes_size);
1580 mem_map = contig_page_data.node_mem_map;
1586 /* Ok, it seems that the prom can allocate some more memory chunks
1587 * as a side effect of some prom calls we perform during the
1588 * boot sequence. My most likely theory is that it is from the
1589 * prom_set_traptable() call, and OBP is allocating a scratchpad
1590 * for saving client program register state etc.
1592 static void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1596 unsigned long tmpaddr, tmpsize;
1597 unsigned long lowest;
1599 for (i = 0; thislist[i].theres_more != 0; i++) {
1600 lowest = thislist[i].start_adr;
1601 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1602 if (thislist[mitr].start_adr < lowest) {
1603 lowest = thislist[mitr].start_adr;
1606 if (lowest == thislist[i].start_adr)
1608 tmpaddr = thislist[swapi].start_adr;
1609 tmpsize = thislist[swapi].num_bytes;
1610 for (mitr = swapi; mitr > i; mitr--) {
1611 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1612 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1614 thislist[i].start_adr = tmpaddr;
1615 thislist[i].num_bytes = tmpsize;
1619 void __init rescan_sp_banks(void)
1621 struct linux_prom64_registers memlist[64];
1622 struct linux_mlist_p1275 avail[64], *mlist;
1623 unsigned long bytes, base_paddr;
1624 int num_regs, node = prom_finddevice("/memory");
1627 num_regs = prom_getproperty(node, "available",
1628 (char *) memlist, sizeof(memlist));
1629 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1630 for (i = 0; i < num_regs; i++) {
1631 avail[i].start_adr = memlist[i].phys_addr;
1632 avail[i].num_bytes = memlist[i].reg_size;
1633 avail[i].theres_more = &avail[i + 1];
1635 avail[i - 1].theres_more = NULL;
1636 sort_memlist(avail);
1640 bytes = mlist->num_bytes;
1641 base_paddr = mlist->start_adr;
1643 sp_banks[0].base_addr = base_paddr;
1644 sp_banks[0].num_bytes = bytes;
1646 while (mlist->theres_more != NULL){
1648 mlist = mlist->theres_more;
1649 bytes = mlist->num_bytes;
1650 if (i >= SPARC_PHYS_BANKS-1) {
1651 printk ("The machine has more banks than "
1652 "this kernel can support\n"
1653 "Increase the SPARC_PHYS_BANKS "
1654 "setting (currently %d)\n",
1656 i = SPARC_PHYS_BANKS-1;
1660 sp_banks[i].base_addr = mlist->start_adr;
1661 sp_banks[i].num_bytes = mlist->num_bytes;
1665 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1666 sp_banks[i].num_bytes = 0;
1668 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1669 sp_banks[i].num_bytes &= PAGE_MASK;
1672 static void __init taint_real_pages(void)
1674 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1677 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1678 saved_sp_banks[i].base_addr =
1679 sp_banks[i].base_addr;
1680 saved_sp_banks[i].num_bytes =
1681 sp_banks[i].num_bytes;
1686 /* Find changes discovered in the sp_bank rescan and
1687 * reserve the lost portions in the bootmem maps.
1689 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1690 unsigned long old_start, old_end;
1692 old_start = saved_sp_banks[i].base_addr;
1693 old_end = old_start +
1694 saved_sp_banks[i].num_bytes;
1695 while (old_start < old_end) {
1698 for (n = 0; sp_banks[n].num_bytes; n++) {
1699 unsigned long new_start, new_end;
1701 new_start = sp_banks[n].base_addr;
1702 new_end = new_start + sp_banks[n].num_bytes;
1704 if (new_start <= old_start &&
1705 new_end >= (old_start + PAGE_SIZE)) {
1706 set_bit (old_start >> 22,
1707 sparc64_valid_addr_bitmap);
1711 reserve_bootmem(old_start, PAGE_SIZE);
1714 old_start += PAGE_SIZE;
1719 void __init mem_init(void)
1721 unsigned long codepages, datapages, initpages;
1722 unsigned long addr, last;
1725 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1727 sparc64_valid_addr_bitmap = (unsigned long *)
1728 __alloc_bootmem(i << 3, SMP_CACHE_BYTES, bootmap_base);
1729 if (sparc64_valid_addr_bitmap == NULL) {
1730 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1733 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1735 addr = PAGE_OFFSET + kern_base;
1736 last = PAGE_ALIGN(kern_size) + addr;
1737 while (addr < last) {
1738 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1744 max_mapnr = last_valid_pfn - pfn_base;
1745 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1747 #ifdef CONFIG_DEBUG_BOOTMEM
1748 prom_printf("mem_init: Calling free_all_bootmem().\n");
1750 totalram_pages = num_physpages = free_all_bootmem() - 1;
1753 * Set up the zero page, mark it reserved, so that page count
1754 * is not manipulated when freeing the page from user ptes.
1756 mem_map_zero = alloc_pages(GFP_KERNEL, 0);
1757 if (mem_map_zero == NULL) {
1758 prom_printf("paging_init: Cannot alloc zero page.\n");
1761 SetPageReserved(mem_map_zero);
1762 clear_page(page_address(mem_map_zero));
1764 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1765 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1766 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1767 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1768 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1769 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1773 /* Put empty_pg_dir on pgd_quicklist */
1774 extern pgd_t empty_pg_dir[1024];
1775 unsigned long addr = (unsigned long)empty_pg_dir;
1776 unsigned long alias_base = kern_base + PAGE_OFFSET -
1779 memset(empty_pg_dir, 0, sizeof(empty_pg_dir));
1781 free_pgd_fast((pgd_t *)addr);
1787 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1788 nr_free_pages() << (PAGE_SHIFT-10),
1789 codepages << (PAGE_SHIFT-10),
1790 datapages << (PAGE_SHIFT-10),
1791 initpages << (PAGE_SHIFT-10),
1792 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1794 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1795 cheetah_ecache_flush_init();
1798 void free_initmem (void)
1800 unsigned long addr, initend;
1803 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1805 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1806 initend = (unsigned long)(__init_end) & PAGE_MASK;
1807 for (; addr < initend; addr += PAGE_SIZE) {
1812 ((unsigned long) __va(kern_base)) -
1813 ((unsigned long) KERNBASE));
1814 p = virt_to_page(page);
1816 ClearPageReserved(p);
1817 set_page_count(p, 1);
1824 #ifdef CONFIG_BLK_DEV_INITRD
1825 void free_initrd_mem(unsigned long start, unsigned long end)
1828 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1829 for (; start < end; start += PAGE_SIZE) {
1830 struct page *p = virt_to_page(start);
1832 ClearPageReserved(p);
1833 set_page_count(p, 1);