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_address(page),
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_address(page)));
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));
347 unsigned long page_to_pfn(struct page *page)
349 return (unsigned long) ((page - mem_map) + pfn_base);
352 struct page *pfn_to_page(unsigned long pfn)
354 return (mem_map + (pfn - pfn_base));
359 printk("Mem-info:\n");
361 printk("Free swap: %6ldkB\n",
362 nr_swap_pages << (PAGE_SHIFT-10));
363 printk("%ld pages of RAM\n", num_physpages);
364 printk("%d free pages\n", nr_free_pages());
365 printk("%d pages in page table cache\n",pgtable_cache_size);
367 printk("%d entries in page dir cache\n",pgd_cache_size);
371 void mmu_info(struct seq_file *m)
373 if (tlb_type == cheetah)
374 seq_printf(m, "MMU Type\t: Cheetah\n");
375 else if (tlb_type == cheetah_plus)
376 seq_printf(m, "MMU Type\t: Cheetah+\n");
377 else if (tlb_type == spitfire)
378 seq_printf(m, "MMU Type\t: Spitfire\n");
380 seq_printf(m, "MMU Type\t: ???\n");
382 #ifdef CONFIG_DEBUG_DCFLUSH
383 seq_printf(m, "DCPageFlushes\t: %d\n",
384 atomic_read(&dcpage_flushes));
386 seq_printf(m, "DCPageFlushesXC\t: %d\n",
387 atomic_read(&dcpage_flushes_xcall));
388 #endif /* CONFIG_SMP */
389 #endif /* CONFIG_DEBUG_DCFLUSH */
392 struct linux_prom_translation {
398 extern unsigned long prom_boot_page;
399 extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
400 extern int prom_get_mmu_ihandle(void);
401 extern void register_prom_callbacks(void);
403 /* Exported for SMP bootup purposes. */
404 unsigned long kern_locked_tte_data;
406 void __init early_pgtable_allocfail(char *type)
408 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
412 #define BASE_PAGE_SIZE 8192
413 static pmd_t *prompmd;
416 * Translate PROM's mapping we capture at boot time into physical address.
417 * The second parameter is only set from prom_callback() invocations.
419 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
421 pmd_t *pmdp = prompmd + ((promva >> 23) & 0x7ff);
425 if (pmd_none(*pmdp)) {
430 ptep = (pte_t *)__pmd_page(*pmdp) + ((promva >> 13) & 0x3ff);
431 if (!pte_present(*ptep)) {
438 return(pte_val(*ptep));
440 base = pte_val(*ptep) & _PAGE_PADDR;
441 return(base + (promva & (BASE_PAGE_SIZE - 1)));
444 static void inherit_prom_mappings(void)
446 struct linux_prom_translation *trans;
447 unsigned long phys_page, tte_vaddr, tte_data;
448 void (*remap_func)(unsigned long, unsigned long, int);
452 extern unsigned int obp_iaddr_patch[2], obp_daddr_patch[2];
454 node = prom_finddevice("/virtual-memory");
455 n = prom_getproplen(node, "translations");
456 if (n == 0 || n == -1) {
457 prom_printf("Couldn't get translation property\n");
460 n += 5 * sizeof(struct linux_prom_translation);
461 for (tsz = 1; tsz < n; tsz <<= 1)
463 trans = __alloc_bootmem(tsz, SMP_CACHE_BYTES, bootmap_base);
465 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
468 memset(trans, 0, tsz);
470 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
471 prom_printf("Couldn't get translation property\n");
474 n = n / sizeof(*trans);
477 * The obp translations are saved based on 8k pagesize, since obp can use
478 * a mixture of pagesizes. Misses to the 0xf0000000 - 0x100000000, ie obp
479 * range, are handled in entry.S and do not use the vpte scheme (see rant
480 * in inherit_locked_prom_mappings()).
482 #define OBP_PMD_SIZE 2048
483 prompmd = __alloc_bootmem(OBP_PMD_SIZE, OBP_PMD_SIZE, bootmap_base);
485 early_pgtable_allocfail("pmd");
486 memset(prompmd, 0, OBP_PMD_SIZE);
487 for (i = 0; i < n; i++) {
490 if (trans[i].virt >= LOW_OBP_ADDRESS && trans[i].virt < HI_OBP_ADDRESS) {
491 for (vaddr = trans[i].virt;
492 ((vaddr < trans[i].virt + trans[i].size) &&
493 (vaddr < HI_OBP_ADDRESS));
494 vaddr += BASE_PAGE_SIZE) {
497 pmdp = prompmd + ((vaddr >> 23) & 0x7ff);
498 if (pmd_none(*pmdp)) {
499 ptep = __alloc_bootmem(BASE_PAGE_SIZE,
503 early_pgtable_allocfail("pte");
504 memset(ptep, 0, BASE_PAGE_SIZE);
507 ptep = (pte_t *)__pmd_page(*pmdp) +
508 ((vaddr >> 13) & 0x3ff);
512 /* Clear diag TTE bits. */
513 if (tlb_type == spitfire)
514 val &= ~0x0003fe0000000000UL;
516 set_pte (ptep, __pte(val | _PAGE_MODIFIED));
517 trans[i].data += BASE_PAGE_SIZE;
521 phys_page = __pa(prompmd);
522 obp_iaddr_patch[0] |= (phys_page >> 10);
523 obp_iaddr_patch[1] |= (phys_page & 0x3ff);
524 flushi((long)&obp_iaddr_patch[0]);
525 obp_daddr_patch[0] |= (phys_page >> 10);
526 obp_daddr_patch[1] |= (phys_page & 0x3ff);
527 flushi((long)&obp_daddr_patch[0]);
529 /* Now fixup OBP's idea about where we really are mapped. */
530 prom_printf("Remapping the kernel... ");
532 /* Spitfire Errata #32 workaround */
533 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
537 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
542 phys_page = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
547 phys_page = cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
551 phys_page &= _PAGE_PADDR;
552 phys_page += ((unsigned long)&prom_boot_page -
553 (unsigned long)KERNBASE);
555 if (tlb_type == spitfire) {
556 /* Lock this into i/d tlb entry 59 */
557 __asm__ __volatile__(
558 "stxa %%g0, [%2] %3\n\t"
559 "stxa %0, [%1] %4\n\t"
562 "stxa %%g0, [%2] %5\n\t"
563 "stxa %0, [%1] %6\n\t"
566 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
567 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
568 "r" (59 << 3), "r" (TLB_TAG_ACCESS),
569 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
570 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
572 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
573 /* Lock this into i/d tlb-0 entry 11 */
574 __asm__ __volatile__(
575 "stxa %%g0, [%2] %3\n\t"
576 "stxa %0, [%1] %4\n\t"
579 "stxa %%g0, [%2] %5\n\t"
580 "stxa %0, [%1] %6\n\t"
583 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
584 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
585 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS),
586 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
587 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
590 /* Implement me :-) */
594 tte_vaddr = (unsigned long) KERNBASE;
596 /* Spitfire Errata #32 workaround */
597 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
601 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
603 if (tlb_type == spitfire)
604 tte_data = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
606 tte_data = cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
608 kern_locked_tte_data = tte_data;
610 remap_func = (void *) ((unsigned long) &prom_remap -
611 (unsigned long) &prom_boot_page);
614 /* Spitfire Errata #32 workaround */
615 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
619 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
621 remap_func((tlb_type == spitfire ?
622 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR) :
623 (cheetah_get_litlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR)),
624 (unsigned long) KERNBASE,
625 prom_get_mmu_ihandle());
628 remap_func(((tte_data + 0x400000) & _PAGE_PADDR),
629 (unsigned long) KERNBASE + 0x400000, prom_get_mmu_ihandle());
631 /* Flush out that temporary mapping. */
632 spitfire_flush_dtlb_nucleus_page(0x0);
633 spitfire_flush_itlb_nucleus_page(0x0);
635 /* Now lock us back into the TLBs via OBP. */
636 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
637 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
639 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
640 tte_vaddr + 0x400000);
641 prom_itlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
642 tte_vaddr + 0x400000);
645 /* Re-read translations property. */
646 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
647 prom_printf("Couldn't get translation property\n");
650 n = n / sizeof(*trans);
652 for (i = 0; i < n; i++) {
653 unsigned long vaddr = trans[i].virt;
654 unsigned long size = trans[i].size;
656 if (vaddr < 0xf0000000UL) {
657 unsigned long avoid_start = (unsigned long) KERNBASE;
658 unsigned long avoid_end = avoid_start + (4 * 1024 * 1024);
661 avoid_end += (4 * 1024 * 1024);
662 if (vaddr < avoid_start) {
663 unsigned long top = vaddr + size;
665 if (top > avoid_start)
667 prom_unmap(top - vaddr, vaddr);
669 if ((vaddr + size) > avoid_end) {
670 unsigned long bottom = vaddr;
672 if (bottom < avoid_end)
674 prom_unmap((vaddr + size) - bottom, bottom);
679 prom_printf("done.\n");
681 register_prom_callbacks();
684 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
685 * upwards as reserved for use by the firmware (I wonder if this
686 * will be the same on Cheetah...). We use this virtual address
687 * range for the VPTE table mappings of the nucleus so we need
688 * to zap them when we enter the PROM. -DaveM
690 static void __flush_nucleus_vptes(void)
692 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
695 /* Only DTLB must be checked for VPTE entries. */
696 if (tlb_type == spitfire) {
697 for (i = 0; i < 63; i++) {
700 /* Spitfire Errata #32 workaround */
701 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
705 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
707 tag = spitfire_get_dtlb_tag(i);
708 if (((tag & ~(PAGE_MASK)) == 0) &&
709 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
710 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
713 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
714 spitfire_put_dtlb_data(i, 0x0UL);
717 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
718 for (i = 0; i < 512; i++) {
719 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
721 if ((tag & ~PAGE_MASK) == 0 &&
722 (tag & PAGE_MASK) >= prom_reserved_base) {
723 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
726 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
727 cheetah_put_dtlb_data(i, 0x0UL, 2);
730 if (tlb_type != cheetah_plus)
733 tag = cheetah_get_dtlb_tag(i, 3);
735 if ((tag & ~PAGE_MASK) == 0 &&
736 (tag & PAGE_MASK) >= prom_reserved_base) {
737 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
740 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
741 cheetah_put_dtlb_data(i, 0x0UL, 3);
745 /* Implement me :-) */
750 static int prom_ditlb_set;
751 struct prom_tlb_entry {
753 unsigned long tlb_tag;
754 unsigned long tlb_data;
756 struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
758 void prom_world(int enter)
760 unsigned long pstate;
764 set_fs((mm_segment_t) { get_thread_current_ds() });
769 /* Make sure the following runs atomically. */
770 __asm__ __volatile__("flushw\n\t"
771 "rdpr %%pstate, %0\n\t"
772 "wrpr %0, %1, %%pstate"
777 /* Kick out nucleus VPTEs. */
778 __flush_nucleus_vptes();
780 /* Install PROM world. */
781 for (i = 0; i < 16; i++) {
782 if (prom_dtlb[i].tlb_ent != -1) {
783 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
785 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
787 if (tlb_type == spitfire)
788 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
789 prom_dtlb[i].tlb_data);
790 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
791 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
792 prom_dtlb[i].tlb_data);
794 if (prom_itlb[i].tlb_ent != -1) {
795 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
797 : : "r" (prom_itlb[i].tlb_tag),
798 "r" (TLB_TAG_ACCESS),
800 if (tlb_type == spitfire)
801 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
802 prom_itlb[i].tlb_data);
803 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
804 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
805 prom_itlb[i].tlb_data);
809 for (i = 0; i < 16; i++) {
810 if (prom_dtlb[i].tlb_ent != -1) {
811 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
813 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
814 if (tlb_type == spitfire)
815 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
817 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
819 if (prom_itlb[i].tlb_ent != -1) {
820 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
822 : : "r" (TLB_TAG_ACCESS),
824 if (tlb_type == spitfire)
825 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
827 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
831 __asm__ __volatile__("wrpr %0, 0, %%pstate"
835 void inherit_locked_prom_mappings(int save_p)
841 /* Fucking losing PROM has more mappings in the TLB, but
842 * it (conveniently) fails to mention any of these in the
843 * translations property. The only ones that matter are
844 * the locked PROM tlb entries, so we impose the following
845 * irrecovable rule on the PROM, it is allowed 8 locked
846 * entries in the ITLB and 8 in the DTLB.
848 * Supposedly the upper 16GB of the address space is
849 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
850 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
851 * used between the client program and the firmware on sun5
852 * systems to coordinate mmu mappings is also COMPLETELY
853 * UNDOCUMENTED!!!!!! Thanks S(t)un!
856 for (i = 0; i < 16; i++) {
857 prom_itlb[i].tlb_ent = -1;
858 prom_dtlb[i].tlb_ent = -1;
861 if (tlb_type == spitfire) {
862 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
863 for (i = 0; i < high; i++) {
866 /* Spitfire Errata #32 workaround */
867 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
871 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
873 data = spitfire_get_dtlb_data(i);
874 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
877 /* Spitfire Errata #32 workaround */
878 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
882 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
884 tag = spitfire_get_dtlb_tag(i);
886 prom_dtlb[dtlb_seen].tlb_ent = i;
887 prom_dtlb[dtlb_seen].tlb_tag = tag;
888 prom_dtlb[dtlb_seen].tlb_data = data;
890 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
892 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
893 spitfire_put_dtlb_data(i, 0x0UL);
901 for (i = 0; i < high; i++) {
904 /* Spitfire Errata #32 workaround */
905 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
909 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
911 data = spitfire_get_itlb_data(i);
912 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
915 /* Spitfire Errata #32 workaround */
916 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
920 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
922 tag = spitfire_get_itlb_tag(i);
924 prom_itlb[itlb_seen].tlb_ent = i;
925 prom_itlb[itlb_seen].tlb_tag = tag;
926 prom_itlb[itlb_seen].tlb_data = data;
928 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
930 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
931 spitfire_put_itlb_data(i, 0x0UL);
938 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
939 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
941 for (i = 0; i < high; i++) {
944 data = cheetah_get_ldtlb_data(i);
945 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
948 tag = cheetah_get_ldtlb_tag(i);
950 prom_dtlb[dtlb_seen].tlb_ent = i;
951 prom_dtlb[dtlb_seen].tlb_tag = tag;
952 prom_dtlb[dtlb_seen].tlb_data = data;
954 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
956 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
957 cheetah_put_ldtlb_data(i, 0x0UL);
965 for (i = 0; i < high; i++) {
968 data = cheetah_get_litlb_data(i);
969 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
972 tag = cheetah_get_litlb_tag(i);
974 prom_itlb[itlb_seen].tlb_ent = i;
975 prom_itlb[itlb_seen].tlb_tag = tag;
976 prom_itlb[itlb_seen].tlb_data = data;
978 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
980 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
981 cheetah_put_litlb_data(i, 0x0UL);
989 /* Implement me :-) */
996 /* Give PROM back his world, done during reboots... */
997 void prom_reload_locked(void)
1001 for (i = 0; i < 16; i++) {
1002 if (prom_dtlb[i].tlb_ent != -1) {
1003 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1005 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
1007 if (tlb_type == spitfire)
1008 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
1009 prom_dtlb[i].tlb_data);
1010 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
1011 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
1012 prom_dtlb[i].tlb_data);
1015 if (prom_itlb[i].tlb_ent != -1) {
1016 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1018 : : "r" (prom_itlb[i].tlb_tag),
1019 "r" (TLB_TAG_ACCESS),
1021 if (tlb_type == spitfire)
1022 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
1023 prom_itlb[i].tlb_data);
1025 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
1026 prom_itlb[i].tlb_data);
1031 void __flush_dcache_range(unsigned long start, unsigned long end)
1035 if (tlb_type == spitfire) {
1038 for (va = start; va < end; va += 32) {
1039 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
1044 start = __pa(start);
1046 for (va = start; va < end; va += 32)
1047 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1051 "i" (ASI_DCACHE_INVALIDATE));
1055 /* If not locked, zap it. */
1056 void __flush_tlb_all(void)
1058 unsigned long pstate;
1061 __asm__ __volatile__("flushw\n\t"
1062 "rdpr %%pstate, %0\n\t"
1063 "wrpr %0, %1, %%pstate"
1066 if (tlb_type == spitfire) {
1067 for (i = 0; i < 64; i++) {
1068 /* Spitfire Errata #32 workaround */
1069 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1073 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1075 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
1076 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1079 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1080 spitfire_put_dtlb_data(i, 0x0UL);
1083 /* Spitfire Errata #32 workaround */
1084 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1088 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1090 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
1091 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1094 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1095 spitfire_put_itlb_data(i, 0x0UL);
1098 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1099 cheetah_flush_dtlb_all();
1100 cheetah_flush_itlb_all();
1102 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1106 /* Caller does TLB context flushing on local CPU if necessary.
1107 * The caller also ensures that CTX_VALID(mm->context) is false.
1109 * We must be careful about boundary cases so that we never
1110 * let the user have CTX 0 (nucleus) or we ever use a CTX
1111 * version of zero (and thus NO_CONTEXT would not be caught
1112 * by version mis-match tests in mmu_context.h).
1114 void get_new_mmu_context(struct mm_struct *mm)
1116 unsigned long ctx, new_ctx;
1118 spin_lock(&ctx_alloc_lock);
1119 ctx = CTX_HWBITS(tlb_context_cache + 1);
1120 new_ctx = find_next_zero_bit(mmu_context_bmap, 1UL << CTX_VERSION_SHIFT, ctx);
1121 if (new_ctx >= (1UL << CTX_VERSION_SHIFT)) {
1122 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1123 if (new_ctx >= ctx) {
1125 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1128 new_ctx = CTX_FIRST_VERSION;
1130 /* Don't call memset, for 16 entries that's just
1133 mmu_context_bmap[0] = 3;
1134 mmu_context_bmap[1] = 0;
1135 mmu_context_bmap[2] = 0;
1136 mmu_context_bmap[3] = 0;
1137 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1138 mmu_context_bmap[i + 0] = 0;
1139 mmu_context_bmap[i + 1] = 0;
1140 mmu_context_bmap[i + 2] = 0;
1141 mmu_context_bmap[i + 3] = 0;
1146 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1147 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1149 tlb_context_cache = new_ctx;
1150 spin_unlock(&ctx_alloc_lock);
1152 mm->context = new_ctx;
1156 struct pgtable_cache_struct pgt_quicklists;
1159 /* OK, we have to color these pages. The page tables are accessed
1160 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1161 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1162 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1163 * fast handlers do not get data from old/garbage dcache lines that
1164 * correspond to an old/stale virtual address (user/kernel) that
1165 * previously mapped the pagetable page while accessing vpte range
1166 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1167 * color is the same, then when the kernel initializes the pagetable
1168 * using the later address range, accesses with the first address
1169 * range will see the newly initialized data rather than the garbage.
1171 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1172 #define DC_ALIAS_SHIFT 1
1174 #define DC_ALIAS_SHIFT 0
1176 pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
1179 unsigned long color;
1182 pte_t *ptep = pte_alloc_one_fast(mm, address);
1188 color = VPTE_COLOR(address);
1189 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, DC_ALIAS_SHIFT);
1191 unsigned long *to_free;
1192 unsigned long paddr;
1195 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1196 set_page_count(page, 1);
1197 ClearPageCompound(page);
1199 set_page_count((page + 1), 1);
1200 ClearPageCompound(page + 1);
1202 paddr = (unsigned long) page_address(page);
1203 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1206 pte = (pte_t *) paddr;
1207 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1209 pte = (pte_t *) (paddr + PAGE_SIZE);
1210 to_free = (unsigned long *) paddr;
1213 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1214 /* Now free the other one up, adjust cache size. */
1216 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1217 pte_quicklist[color ^ 0x1] = to_free;
1218 pgtable_cache_size++;
1227 void sparc_ultra_dump_itlb(void)
1231 if (tlb_type == spitfire) {
1232 printk ("Contents of itlb: ");
1233 for (slot = 0; slot < 14; slot++) printk (" ");
1234 printk ("%2x:%016lx,%016lx\n",
1236 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1237 for (slot = 1; slot < 64; slot+=3) {
1238 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1240 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1242 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1244 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1246 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1247 printk ("Contents of itlb0:\n");
1248 for (slot = 0; slot < 16; slot+=2) {
1249 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1251 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1253 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1255 printk ("Contents of itlb2:\n");
1256 for (slot = 0; slot < 128; slot+=2) {
1257 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1259 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1261 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1266 void sparc_ultra_dump_dtlb(void)
1270 if (tlb_type == spitfire) {
1271 printk ("Contents of dtlb: ");
1272 for (slot = 0; slot < 14; slot++) printk (" ");
1273 printk ("%2x:%016lx,%016lx\n", 0,
1274 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1275 for (slot = 1; slot < 64; slot+=3) {
1276 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1278 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1280 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1282 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1284 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1285 printk ("Contents of dtlb0:\n");
1286 for (slot = 0; slot < 16; slot+=2) {
1287 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1289 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1291 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1293 printk ("Contents of dtlb2:\n");
1294 for (slot = 0; slot < 512; slot+=2) {
1295 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1297 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1299 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1301 if (tlb_type == cheetah_plus) {
1302 printk ("Contents of dtlb3:\n");
1303 for (slot = 0; slot < 512; slot+=2) {
1304 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1306 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1308 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1314 extern unsigned long cmdline_memory_size;
1316 unsigned long __init bootmem_init(unsigned long *pages_avail)
1318 unsigned long bootmap_size, start_pfn, end_pfn;
1319 unsigned long end_of_phys_memory = 0UL;
1320 unsigned long bootmap_pfn, bytes_avail, size;
1323 #ifdef CONFIG_DEBUG_BOOTMEM
1324 prom_printf("bootmem_init: Scan sp_banks, ");
1328 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1329 end_of_phys_memory = sp_banks[i].base_addr +
1330 sp_banks[i].num_bytes;
1331 bytes_avail += sp_banks[i].num_bytes;
1332 if (cmdline_memory_size) {
1333 if (bytes_avail > cmdline_memory_size) {
1334 unsigned long slack = bytes_avail - cmdline_memory_size;
1336 bytes_avail -= slack;
1337 end_of_phys_memory -= slack;
1339 sp_banks[i].num_bytes -= slack;
1340 if (sp_banks[i].num_bytes == 0) {
1341 sp_banks[i].base_addr = 0xdeadbeef;
1343 sp_banks[i+1].num_bytes = 0;
1344 sp_banks[i+1].base_addr = 0xdeadbeef;
1351 *pages_avail = bytes_avail >> PAGE_SHIFT;
1353 /* Start with page aligned address of last symbol in kernel
1354 * image. The kernel is hard mapped below PAGE_OFFSET in a
1355 * 4MB locked TLB translation.
1357 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1359 bootmap_pfn = start_pfn;
1361 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1363 #ifdef CONFIG_BLK_DEV_INITRD
1364 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1365 if (sparc_ramdisk_image) {
1366 if (sparc_ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
1367 sparc_ramdisk_image -= KERNBASE;
1368 initrd_start = sparc_ramdisk_image + phys_base;
1369 initrd_end = initrd_start + sparc_ramdisk_size;
1370 if (initrd_end > end_of_phys_memory) {
1371 printk(KERN_CRIT "initrd extends beyond end of memory "
1372 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1373 initrd_end, end_of_phys_memory);
1377 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1378 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1379 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1383 /* Initialize the boot-time allocator. */
1384 max_pfn = max_low_pfn = end_pfn;
1385 min_low_pfn = pfn_base;
1387 #ifdef CONFIG_DEBUG_BOOTMEM
1388 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1389 min_low_pfn, bootmap_pfn, max_low_pfn);
1391 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
1393 bootmap_base = bootmap_pfn << PAGE_SHIFT;
1395 /* Now register the available physical memory with the
1398 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1399 #ifdef CONFIG_DEBUG_BOOTMEM
1400 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1401 i, sp_banks[i].base_addr, sp_banks[i].num_bytes);
1403 free_bootmem(sp_banks[i].base_addr, sp_banks[i].num_bytes);
1406 #ifdef CONFIG_BLK_DEV_INITRD
1408 size = initrd_end - initrd_start;
1410 /* Resert the initrd image area. */
1411 reserve_bootmem(initrd_start, size);
1412 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1414 initrd_start += PAGE_OFFSET;
1415 initrd_end += PAGE_OFFSET;
1418 /* Reserve the kernel text/data/bss. */
1419 #ifdef CONFIG_DEBUG_BOOTMEM
1420 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1422 reserve_bootmem(kern_base, kern_size);
1423 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1425 /* Reserve the bootmem map. We do not account for it
1426 * in pages_avail because we will release that memory
1427 * in free_all_bootmem.
1429 size = bootmap_size;
1430 #ifdef CONFIG_DEBUG_BOOTMEM
1431 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1432 (bootmap_pfn << PAGE_SHIFT), size);
1434 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1435 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1440 /* paging_init() sets up the page tables */
1442 extern void cheetah_ecache_flush_init(void);
1444 static unsigned long last_valid_pfn;
1446 void __init paging_init(void)
1448 extern pmd_t swapper_pmd_dir[1024];
1449 extern unsigned int sparc64_vpte_patchme1[1];
1450 extern unsigned int sparc64_vpte_patchme2[1];
1451 unsigned long alias_base = kern_base + PAGE_OFFSET;
1452 unsigned long second_alias_page = 0;
1453 unsigned long pt, flags, end_pfn, pages_avail;
1454 unsigned long shift = alias_base - ((unsigned long)KERNBASE);
1455 unsigned long real_end;
1457 set_bit(0, mmu_context_bmap);
1459 real_end = (unsigned long)_end;
1460 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1462 #ifdef CONFIG_BLK_DEV_INITRD
1463 if (sparc_ramdisk_image)
1464 real_end = (PAGE_ALIGN(real_end) + PAGE_ALIGN(sparc_ramdisk_size));
1467 /* We assume physical memory starts at some 4mb multiple,
1468 * if this were not true we wouldn't boot up to this point
1471 pt = kern_base | _PAGE_VALID | _PAGE_SZ4MB;
1472 pt |= _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W;
1473 local_irq_save(flags);
1474 if (tlb_type == spitfire) {
1475 __asm__ __volatile__(
1476 " stxa %1, [%0] %3\n"
1477 " stxa %2, [%5] %4\n"
1484 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1485 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (61 << 3)
1487 if (real_end >= KERNBASE + 0x340000) {
1488 second_alias_page = alias_base + 0x400000;
1489 __asm__ __volatile__(
1490 " stxa %1, [%0] %3\n"
1491 " stxa %2, [%5] %4\n"
1498 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1499 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (60 << 3)
1502 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1503 __asm__ __volatile__(
1504 " stxa %1, [%0] %3\n"
1505 " stxa %2, [%5] %4\n"
1512 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1513 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (13<<3))
1515 if (real_end >= KERNBASE + 0x340000) {
1516 second_alias_page = alias_base + 0x400000;
1517 __asm__ __volatile__(
1518 " stxa %1, [%0] %3\n"
1519 " stxa %2, [%5] %4\n"
1526 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1527 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (12<<3))
1531 local_irq_restore(flags);
1533 /* Now set kernel pgd to upper alias so physical page computations
1536 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1538 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1540 /* Now can init the kernel/bad page tables. */
1541 pgd_set(&swapper_pg_dir[0], swapper_pmd_dir + (shift / sizeof(pgd_t)));
1543 sparc64_vpte_patchme1[0] |=
1544 (((unsigned long)pgd_val(init_mm.pgd[0])) >> 10);
1545 sparc64_vpte_patchme2[0] |=
1546 (((unsigned long)pgd_val(init_mm.pgd[0])) & 0x3ff);
1547 flushi((long)&sparc64_vpte_patchme1[0]);
1549 /* Setup bootmem... */
1551 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1553 /* Inherit non-locked OBP mappings. */
1554 inherit_prom_mappings();
1556 /* Ok, we can use our TLB miss and window trap handlers safely.
1557 * We need to do a quick peek here to see if we are on StarFire
1558 * or not, so setup_tba can setup the IRQ globals correctly (it
1559 * needs to get the hard smp processor id correctly).
1562 extern void setup_tba(int);
1563 setup_tba(this_is_starfire);
1566 inherit_locked_prom_mappings(1);
1568 /* We only created DTLB mapping of this stuff. */
1569 spitfire_flush_dtlb_nucleus_page(alias_base);
1570 if (second_alias_page)
1571 spitfire_flush_dtlb_nucleus_page(second_alias_page);
1576 unsigned long zones_size[MAX_NR_ZONES];
1577 unsigned long zholes_size[MAX_NR_ZONES];
1578 unsigned long npages;
1581 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1582 zones_size[znum] = zholes_size[znum] = 0;
1584 npages = end_pfn - pfn_base;
1585 zones_size[ZONE_DMA] = npages;
1586 zholes_size[ZONE_DMA] = npages - pages_avail;
1588 free_area_init_node(0, &contig_page_data, NULL, zones_size,
1589 phys_base >> PAGE_SHIFT, zholes_size);
1590 mem_map = contig_page_data.node_mem_map;
1596 /* Ok, it seems that the prom can allocate some more memory chunks
1597 * as a side effect of some prom calls we perform during the
1598 * boot sequence. My most likely theory is that it is from the
1599 * prom_set_traptable() call, and OBP is allocating a scratchpad
1600 * for saving client program register state etc.
1602 static void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1606 unsigned long tmpaddr, tmpsize;
1607 unsigned long lowest;
1609 for (i = 0; thislist[i].theres_more != 0; i++) {
1610 lowest = thislist[i].start_adr;
1611 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1612 if (thislist[mitr].start_adr < lowest) {
1613 lowest = thislist[mitr].start_adr;
1616 if (lowest == thislist[i].start_adr)
1618 tmpaddr = thislist[swapi].start_adr;
1619 tmpsize = thislist[swapi].num_bytes;
1620 for (mitr = swapi; mitr > i; mitr--) {
1621 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1622 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1624 thislist[i].start_adr = tmpaddr;
1625 thislist[i].num_bytes = tmpsize;
1629 void __init rescan_sp_banks(void)
1631 struct linux_prom64_registers memlist[64];
1632 struct linux_mlist_p1275 avail[64], *mlist;
1633 unsigned long bytes, base_paddr;
1634 int num_regs, node = prom_finddevice("/memory");
1637 num_regs = prom_getproperty(node, "available",
1638 (char *) memlist, sizeof(memlist));
1639 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1640 for (i = 0; i < num_regs; i++) {
1641 avail[i].start_adr = memlist[i].phys_addr;
1642 avail[i].num_bytes = memlist[i].reg_size;
1643 avail[i].theres_more = &avail[i + 1];
1645 avail[i - 1].theres_more = NULL;
1646 sort_memlist(avail);
1650 bytes = mlist->num_bytes;
1651 base_paddr = mlist->start_adr;
1653 sp_banks[0].base_addr = base_paddr;
1654 sp_banks[0].num_bytes = bytes;
1656 while (mlist->theres_more != NULL){
1658 mlist = mlist->theres_more;
1659 bytes = mlist->num_bytes;
1660 if (i >= SPARC_PHYS_BANKS-1) {
1661 printk ("The machine has more banks than "
1662 "this kernel can support\n"
1663 "Increase the SPARC_PHYS_BANKS "
1664 "setting (currently %d)\n",
1666 i = SPARC_PHYS_BANKS-1;
1670 sp_banks[i].base_addr = mlist->start_adr;
1671 sp_banks[i].num_bytes = mlist->num_bytes;
1675 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1676 sp_banks[i].num_bytes = 0;
1678 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1679 sp_banks[i].num_bytes &= PAGE_MASK;
1682 static void __init taint_real_pages(void)
1684 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1687 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1688 saved_sp_banks[i].base_addr =
1689 sp_banks[i].base_addr;
1690 saved_sp_banks[i].num_bytes =
1691 sp_banks[i].num_bytes;
1696 /* Find changes discovered in the sp_bank rescan and
1697 * reserve the lost portions in the bootmem maps.
1699 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1700 unsigned long old_start, old_end;
1702 old_start = saved_sp_banks[i].base_addr;
1703 old_end = old_start +
1704 saved_sp_banks[i].num_bytes;
1705 while (old_start < old_end) {
1708 for (n = 0; sp_banks[n].num_bytes; n++) {
1709 unsigned long new_start, new_end;
1711 new_start = sp_banks[n].base_addr;
1712 new_end = new_start + sp_banks[n].num_bytes;
1714 if (new_start <= old_start &&
1715 new_end >= (old_start + PAGE_SIZE)) {
1716 set_bit (old_start >> 22,
1717 sparc64_valid_addr_bitmap);
1721 reserve_bootmem(old_start, PAGE_SIZE);
1724 old_start += PAGE_SIZE;
1729 void __init mem_init(void)
1731 unsigned long codepages, datapages, initpages;
1732 unsigned long addr, last;
1735 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1737 sparc64_valid_addr_bitmap = (unsigned long *)
1738 __alloc_bootmem(i << 3, SMP_CACHE_BYTES, bootmap_base);
1739 if (sparc64_valid_addr_bitmap == NULL) {
1740 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1743 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1745 addr = PAGE_OFFSET + kern_base;
1746 last = PAGE_ALIGN(kern_size) + addr;
1747 while (addr < last) {
1748 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1754 max_mapnr = last_valid_pfn - pfn_base;
1755 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1757 #ifdef CONFIG_DEBUG_BOOTMEM
1758 prom_printf("mem_init: Calling free_all_bootmem().\n");
1760 totalram_pages = num_physpages = free_all_bootmem() - 1;
1763 * Set up the zero page, mark it reserved, so that page count
1764 * is not manipulated when freeing the page from user ptes.
1766 mem_map_zero = alloc_pages(GFP_KERNEL, 0);
1767 if (mem_map_zero == NULL) {
1768 prom_printf("paging_init: Cannot alloc zero page.\n");
1771 SetPageReserved(mem_map_zero);
1772 clear_page(page_address(mem_map_zero));
1774 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1775 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1776 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1777 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1778 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1779 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1783 /* Put empty_pg_dir on pgd_quicklist */
1784 extern pgd_t empty_pg_dir[1024];
1785 unsigned long addr = (unsigned long)empty_pg_dir;
1786 unsigned long alias_base = kern_base + PAGE_OFFSET -
1789 memset(empty_pg_dir, 0, sizeof(empty_pg_dir));
1791 free_pgd_fast((pgd_t *)addr);
1797 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1798 nr_free_pages() << (PAGE_SHIFT-10),
1799 codepages << (PAGE_SHIFT-10),
1800 datapages << (PAGE_SHIFT-10),
1801 initpages << (PAGE_SHIFT-10),
1802 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1804 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1805 cheetah_ecache_flush_init();
1808 void free_initmem (void)
1810 unsigned long addr, initend;
1813 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1815 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1816 initend = (unsigned long)(__init_end) & PAGE_MASK;
1817 for (; addr < initend; addr += PAGE_SIZE) {
1822 ((unsigned long) __va(kern_base)) -
1823 ((unsigned long) KERNBASE));
1824 p = virt_to_page(page);
1826 ClearPageReserved(p);
1827 set_page_count(p, 1);
1834 #ifdef CONFIG_BLK_DEV_INITRD
1835 void free_initrd_mem(unsigned long start, unsigned long end)
1838 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1839 for (; start < end; start += PAGE_SIZE) {
1840 struct page *p = virt_to_page(start);
1842 ClearPageReserved(p);
1843 set_page_count(p, 1);