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 extern void device_scan(void);
42 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
44 unsigned long *sparc64_valid_addr_bitmap;
46 /* Ugly, but necessary... -DaveM */
47 unsigned long phys_base;
48 unsigned long kern_base;
49 unsigned long kern_size;
50 unsigned long pfn_base;
52 /* This is even uglier. We have a problem where the kernel may not be
53 * located at phys_base. However, initial __alloc_bootmem() calls need to
54 * be adjusted to be within the 4-8Megs that the kernel is mapped to, else
55 * those page mappings wont work. Things are ok after inherit_prom_mappings
56 * is called though. Dave says he'll clean this up some other time.
59 static unsigned long bootmap_base;
61 /* get_new_mmu_context() uses "cache + 1". */
62 spinlock_t ctx_alloc_lock = SPIN_LOCK_UNLOCKED;
63 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
64 #define CTX_BMAP_SLOTS (1UL << (CTX_VERSION_SHIFT - 6))
65 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
67 /* References to special section boundaries */
68 extern char _start[], _end[];
70 /* Initial ramdisk setup */
71 extern unsigned int sparc_ramdisk_image;
72 extern unsigned int sparc_ramdisk_size;
74 struct page *mem_map_zero;
78 /* XXX Tune this... */
79 #define PGT_CACHE_LOW 25
80 #define PGT_CACHE_HIGH 50
82 void check_pgt_cache(void)
85 if (pgtable_cache_size > PGT_CACHE_HIGH) {
89 free_pgd_slow(get_pgd_fast());
92 free_pte_slow(pte_alloc_one_fast(NULL, 0));
94 free_pte_slow(pte_alloc_one_fast(NULL, 1 << (PAGE_SHIFT + 10)));
95 } while (pgtable_cache_size > PGT_CACHE_LOW);
98 if (pgd_cache_size > PGT_CACHE_HIGH / 4) {
99 struct page *page, *page2;
100 for (page2 = NULL, page = (struct page *)pgd_quicklist; page;) {
101 if ((unsigned long)page->lru.prev == 3) {
103 page2->lru.next = page->lru.next;
105 pgd_quicklist = (void *) page->lru.next;
109 page = (struct page *)page2->lru.next;
111 page = (struct page *)pgd_quicklist;
112 if (pgd_cache_size <= PGT_CACHE_LOW / 4)
117 page = (struct page *)page->lru.next;
124 #ifdef CONFIG_DEBUG_DCFLUSH
125 atomic_t dcpage_flushes = ATOMIC_INIT(0);
127 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
131 __inline__ void flush_dcache_page_impl(struct page *page)
133 #ifdef CONFIG_DEBUG_DCFLUSH
134 atomic_inc(&dcpage_flushes);
137 #if (L1DCACHE_SIZE > PAGE_SIZE)
138 __flush_dcache_page(page_address(page),
139 ((tlb_type == spitfire) &&
140 page_mapping(page) != NULL));
142 if (page_mapping(page) != NULL &&
143 tlb_type == spitfire)
144 __flush_icache_page(__pa(page_address(page)));
148 #define PG_dcache_dirty PG_arch_1
150 #define dcache_dirty_cpu(page) \
151 (((page)->flags >> 24) & (NR_CPUS - 1UL))
153 static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
155 unsigned long mask = this_cpu;
156 unsigned long non_cpu_bits = ~((NR_CPUS - 1UL) << 24UL);
157 mask = (mask << 24) | (1UL << PG_dcache_dirty);
158 __asm__ __volatile__("1:\n\t"
160 "and %%g7, %1, %%g5\n\t"
161 "or %%g5, %0, %%g5\n\t"
162 "casx [%2], %%g7, %%g5\n\t"
164 "bne,pn %%xcc, 1b\n\t"
165 " membar #StoreLoad | #StoreStore"
167 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
171 static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
173 unsigned long mask = (1UL << PG_dcache_dirty);
175 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
178 "srlx %%g7, 24, %%g5\n\t"
179 "and %%g5, %3, %%g5\n\t"
181 "bne,pn %%icc, 2f\n\t"
182 " andn %%g7, %1, %%g5\n\t"
183 "casx [%2], %%g7, %%g5\n\t"
185 "bne,pn %%xcc, 1b\n\t"
186 " membar #StoreLoad | #StoreStore\n"
189 : "r" (cpu), "r" (mask), "r" (&page->flags),
194 extern void __update_mmu_cache(unsigned long mmu_context_hw, unsigned long address, pte_t pte, int code);
196 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
200 unsigned long pg_flags;
203 if (pfn_valid(pfn) &&
204 (page = pfn_to_page(pfn), page_mapping(page)) &&
205 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
206 int cpu = ((pg_flags >> 24) & (NR_CPUS - 1UL));
207 int this_cpu = get_cpu();
209 /* This is just to optimize away some function calls
213 flush_dcache_page_impl(page);
215 smp_flush_dcache_page_impl(page, cpu);
217 clear_dcache_dirty_cpu(page, cpu);
221 if (get_thread_fault_code())
222 __update_mmu_cache(vma->vm_mm->context & TAG_CONTEXT_BITS,
223 address, pte, get_thread_fault_code());
226 void flush_dcache_page(struct page *page)
228 struct address_space *mapping = page_mapping(page);
229 int dirty = test_bit(PG_dcache_dirty, &page->flags);
230 int dirty_cpu = dcache_dirty_cpu(page);
231 int this_cpu = get_cpu();
233 if (mapping && !mapping_mapped(mapping)) {
235 if (dirty_cpu == this_cpu)
237 smp_flush_dcache_page_impl(page, dirty_cpu);
239 set_dcache_dirty(page, this_cpu);
241 /* We could delay the flush for the !page_mapping
242 * case too. But that case is for exec env/arg
243 * pages and those are %99 certainly going to get
244 * faulted into the tlb (and thus flushed) anyways.
246 flush_dcache_page_impl(page);
253 void flush_icache_range(unsigned long start, unsigned long end)
255 /* Cheetah has coherent I-cache. */
256 if (tlb_type == spitfire) {
259 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
260 __flush_icache_page(__get_phys(kaddr));
264 unsigned long page_to_pfn(struct page *page)
266 return (unsigned long) ((page - mem_map) + pfn_base);
269 struct page *pfn_to_page(unsigned long pfn)
271 return (mem_map + (pfn - pfn_base));
276 printk("Mem-info:\n");
278 printk("Free swap: %6ldkB\n",
279 nr_swap_pages << (PAGE_SHIFT-10));
280 printk("%ld pages of RAM\n", num_physpages);
281 printk("%d free pages\n", nr_free_pages());
282 printk("%d pages in page table cache\n",pgtable_cache_size);
284 printk("%d entries in page dir cache\n",pgd_cache_size);
288 void mmu_info(struct seq_file *m)
290 if (tlb_type == cheetah)
291 seq_printf(m, "MMU Type\t: Cheetah\n");
292 else if (tlb_type == cheetah_plus)
293 seq_printf(m, "MMU Type\t: Cheetah+\n");
294 else if (tlb_type == spitfire)
295 seq_printf(m, "MMU Type\t: Spitfire\n");
297 seq_printf(m, "MMU Type\t: ???\n");
299 #ifdef CONFIG_DEBUG_DCFLUSH
300 seq_printf(m, "DCPageFlushes\t: %d\n",
301 atomic_read(&dcpage_flushes));
303 seq_printf(m, "DCPageFlushesXC\t: %d\n",
304 atomic_read(&dcpage_flushes_xcall));
305 #endif /* CONFIG_SMP */
306 #endif /* CONFIG_DEBUG_DCFLUSH */
309 struct linux_prom_translation {
315 extern unsigned long prom_boot_page;
316 extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
317 extern int prom_get_mmu_ihandle(void);
318 extern void register_prom_callbacks(void);
320 /* Exported for SMP bootup purposes. */
321 unsigned long kern_locked_tte_data;
323 void __init early_pgtable_allocfail(char *type)
325 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
329 #define BASE_PAGE_SIZE 8192
330 static pmd_t *prompmd;
333 * Translate PROM's mapping we capture at boot time into physical address.
334 * The second parameter is only set from prom_callback() invocations.
336 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
338 pmd_t *pmdp = prompmd + ((promva >> 23) & 0x7ff);
342 if (pmd_none(*pmdp)) {
347 ptep = (pte_t *)__pmd_page(*pmdp) + ((promva >> 13) & 0x3ff);
348 if (!pte_present(*ptep)) {
355 return(pte_val(*ptep));
357 base = pte_val(*ptep) & _PAGE_PADDR;
358 return(base + (promva & (BASE_PAGE_SIZE - 1)));
361 static void inherit_prom_mappings(void)
363 struct linux_prom_translation *trans;
364 unsigned long phys_page, tte_vaddr, tte_data;
365 void (*remap_func)(unsigned long, unsigned long, int);
369 extern unsigned int obp_iaddr_patch[2], obp_daddr_patch[2];
371 node = prom_finddevice("/virtual-memory");
372 n = prom_getproplen(node, "translations");
373 if (n == 0 || n == -1) {
374 prom_printf("Couldn't get translation property\n");
377 n += 5 * sizeof(struct linux_prom_translation);
378 for (tsz = 1; tsz < n; tsz <<= 1)
380 trans = __alloc_bootmem(tsz, SMP_CACHE_BYTES, bootmap_base);
382 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
385 memset(trans, 0, tsz);
387 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
388 prom_printf("Couldn't get translation property\n");
391 n = n / sizeof(*trans);
394 * The obp translations are saved based on 8k pagesize, since obp can use
395 * a mixture of pagesizes. Misses to the 0xf0000000 - 0x100000000, ie obp
396 * range, are handled in entry.S and do not use the vpte scheme (see rant
397 * in inherit_locked_prom_mappings()).
399 #define OBP_PMD_SIZE 2048
400 prompmd = __alloc_bootmem(OBP_PMD_SIZE, OBP_PMD_SIZE, bootmap_base);
402 early_pgtable_allocfail("pmd");
403 memset(prompmd, 0, OBP_PMD_SIZE);
404 for (i = 0; i < n; i++) {
407 if (trans[i].virt >= LOW_OBP_ADDRESS && trans[i].virt < HI_OBP_ADDRESS) {
408 for (vaddr = trans[i].virt;
409 ((vaddr < trans[i].virt + trans[i].size) &&
410 (vaddr < HI_OBP_ADDRESS));
411 vaddr += BASE_PAGE_SIZE) {
414 pmdp = prompmd + ((vaddr >> 23) & 0x7ff);
415 if (pmd_none(*pmdp)) {
416 ptep = __alloc_bootmem(BASE_PAGE_SIZE,
420 early_pgtable_allocfail("pte");
421 memset(ptep, 0, BASE_PAGE_SIZE);
424 ptep = (pte_t *)__pmd_page(*pmdp) +
425 ((vaddr >> 13) & 0x3ff);
429 /* Clear diag TTE bits. */
430 if (tlb_type == spitfire)
431 val &= ~0x0003fe0000000000UL;
433 set_pte (ptep, __pte(val | _PAGE_MODIFIED));
434 trans[i].data += BASE_PAGE_SIZE;
438 phys_page = __pa(prompmd);
439 obp_iaddr_patch[0] |= (phys_page >> 10);
440 obp_iaddr_patch[1] |= (phys_page & 0x3ff);
441 flushi((long)&obp_iaddr_patch[0]);
442 obp_daddr_patch[0] |= (phys_page >> 10);
443 obp_daddr_patch[1] |= (phys_page & 0x3ff);
444 flushi((long)&obp_daddr_patch[0]);
446 /* Now fixup OBP's idea about where we really are mapped. */
447 prom_printf("Remapping the kernel... ");
449 /* Spitfire Errata #32 workaround */
450 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
454 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
459 phys_page = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
464 phys_page = cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
468 phys_page &= _PAGE_PADDR;
469 phys_page += ((unsigned long)&prom_boot_page -
470 (unsigned long)KERNBASE);
472 if (tlb_type == spitfire) {
473 /* Lock this into i/d tlb entry 59 */
474 __asm__ __volatile__(
475 "stxa %%g0, [%2] %3\n\t"
476 "stxa %0, [%1] %4\n\t"
479 "stxa %%g0, [%2] %5\n\t"
480 "stxa %0, [%1] %6\n\t"
483 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
484 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
485 "r" (59 << 3), "r" (TLB_TAG_ACCESS),
486 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
487 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
489 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
490 /* Lock this into i/d tlb-0 entry 11 */
491 __asm__ __volatile__(
492 "stxa %%g0, [%2] %3\n\t"
493 "stxa %0, [%1] %4\n\t"
496 "stxa %%g0, [%2] %5\n\t"
497 "stxa %0, [%1] %6\n\t"
500 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
501 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
502 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS),
503 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
504 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
507 /* Implement me :-) */
511 tte_vaddr = (unsigned long) KERNBASE;
513 /* Spitfire Errata #32 workaround */
514 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
518 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
520 if (tlb_type == spitfire)
521 tte_data = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
523 tte_data = cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
525 kern_locked_tte_data = tte_data;
527 remap_func = (void *) ((unsigned long) &prom_remap -
528 (unsigned long) &prom_boot_page);
531 /* Spitfire Errata #32 workaround */
532 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
536 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
538 remap_func((tlb_type == spitfire ?
539 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR) :
540 (cheetah_get_litlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR)),
541 (unsigned long) KERNBASE,
542 prom_get_mmu_ihandle());
545 remap_func(((tte_data + 0x400000) & _PAGE_PADDR),
546 (unsigned long) KERNBASE + 0x400000, prom_get_mmu_ihandle());
548 /* Flush out that temporary mapping. */
549 spitfire_flush_dtlb_nucleus_page(0x0);
550 spitfire_flush_itlb_nucleus_page(0x0);
552 /* Now lock us back into the TLBs via OBP. */
553 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
554 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
556 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
557 tte_vaddr + 0x400000);
558 prom_itlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
559 tte_vaddr + 0x400000);
562 /* Re-read translations property. */
563 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
564 prom_printf("Couldn't get translation property\n");
567 n = n / sizeof(*trans);
569 for (i = 0; i < n; i++) {
570 unsigned long vaddr = trans[i].virt;
571 unsigned long size = trans[i].size;
573 if (vaddr < 0xf0000000UL) {
574 unsigned long avoid_start = (unsigned long) KERNBASE;
575 unsigned long avoid_end = avoid_start + (4 * 1024 * 1024);
578 avoid_end += (4 * 1024 * 1024);
579 if (vaddr < avoid_start) {
580 unsigned long top = vaddr + size;
582 if (top > avoid_start)
584 prom_unmap(top - vaddr, vaddr);
586 if ((vaddr + size) > avoid_end) {
587 unsigned long bottom = vaddr;
589 if (bottom < avoid_end)
591 prom_unmap((vaddr + size) - bottom, bottom);
596 prom_printf("done.\n");
598 register_prom_callbacks();
601 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
602 * upwards as reserved for use by the firmware (I wonder if this
603 * will be the same on Cheetah...). We use this virtual address
604 * range for the VPTE table mappings of the nucleus so we need
605 * to zap them when we enter the PROM. -DaveM
607 static void __flush_nucleus_vptes(void)
609 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
612 /* Only DTLB must be checked for VPTE entries. */
613 if (tlb_type == spitfire) {
614 for (i = 0; i < 63; i++) {
617 /* Spitfire Errata #32 workaround */
618 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
622 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
624 tag = spitfire_get_dtlb_tag(i);
625 if (((tag & ~(PAGE_MASK)) == 0) &&
626 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
627 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
630 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
631 spitfire_put_dtlb_data(i, 0x0UL);
634 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
635 for (i = 0; i < 512; i++) {
636 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
638 if ((tag & ~PAGE_MASK) == 0 &&
639 (tag & PAGE_MASK) >= prom_reserved_base) {
640 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
643 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
644 cheetah_put_dtlb_data(i, 0x0UL, 2);
647 if (tlb_type != cheetah_plus)
650 tag = cheetah_get_dtlb_tag(i, 3);
652 if ((tag & ~PAGE_MASK) == 0 &&
653 (tag & PAGE_MASK) >= prom_reserved_base) {
654 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
657 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
658 cheetah_put_dtlb_data(i, 0x0UL, 3);
662 /* Implement me :-) */
667 static int prom_ditlb_set;
668 struct prom_tlb_entry {
670 unsigned long tlb_tag;
671 unsigned long tlb_data;
673 struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
675 void prom_world(int enter)
677 unsigned long pstate;
681 set_fs((mm_segment_t) { get_thread_current_ds() });
686 /* Make sure the following runs atomically. */
687 __asm__ __volatile__("flushw\n\t"
688 "rdpr %%pstate, %0\n\t"
689 "wrpr %0, %1, %%pstate"
694 /* Kick out nucleus VPTEs. */
695 __flush_nucleus_vptes();
697 /* Install PROM world. */
698 for (i = 0; i < 16; i++) {
699 if (prom_dtlb[i].tlb_ent != -1) {
700 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
702 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
704 if (tlb_type == spitfire)
705 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
706 prom_dtlb[i].tlb_data);
707 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
708 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
709 prom_dtlb[i].tlb_data);
711 if (prom_itlb[i].tlb_ent != -1) {
712 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
714 : : "r" (prom_itlb[i].tlb_tag),
715 "r" (TLB_TAG_ACCESS),
717 if (tlb_type == spitfire)
718 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
719 prom_itlb[i].tlb_data);
720 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
721 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
722 prom_itlb[i].tlb_data);
726 for (i = 0; i < 16; i++) {
727 if (prom_dtlb[i].tlb_ent != -1) {
728 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
730 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
731 if (tlb_type == spitfire)
732 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
734 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
736 if (prom_itlb[i].tlb_ent != -1) {
737 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
739 : : "r" (TLB_TAG_ACCESS),
741 if (tlb_type == spitfire)
742 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
744 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
748 __asm__ __volatile__("wrpr %0, 0, %%pstate"
752 void inherit_locked_prom_mappings(int save_p)
758 /* Fucking losing PROM has more mappings in the TLB, but
759 * it (conveniently) fails to mention any of these in the
760 * translations property. The only ones that matter are
761 * the locked PROM tlb entries, so we impose the following
762 * irrecovable rule on the PROM, it is allowed 8 locked
763 * entries in the ITLB and 8 in the DTLB.
765 * Supposedly the upper 16GB of the address space is
766 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
767 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
768 * used between the client program and the firmware on sun5
769 * systems to coordinate mmu mappings is also COMPLETELY
770 * UNDOCUMENTED!!!!!! Thanks S(t)un!
773 for (i = 0; i < 16; i++) {
774 prom_itlb[i].tlb_ent = -1;
775 prom_dtlb[i].tlb_ent = -1;
778 if (tlb_type == spitfire) {
779 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
780 for (i = 0; i < high; i++) {
783 /* Spitfire Errata #32 workaround */
784 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
788 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
790 data = spitfire_get_dtlb_data(i);
791 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
794 /* Spitfire Errata #32 workaround */
795 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
799 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
801 tag = spitfire_get_dtlb_tag(i);
803 prom_dtlb[dtlb_seen].tlb_ent = i;
804 prom_dtlb[dtlb_seen].tlb_tag = tag;
805 prom_dtlb[dtlb_seen].tlb_data = data;
807 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
809 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
810 spitfire_put_dtlb_data(i, 0x0UL);
818 for (i = 0; i < high; i++) {
821 /* Spitfire Errata #32 workaround */
822 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
826 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
828 data = spitfire_get_itlb_data(i);
829 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
832 /* Spitfire Errata #32 workaround */
833 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
837 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
839 tag = spitfire_get_itlb_tag(i);
841 prom_itlb[itlb_seen].tlb_ent = i;
842 prom_itlb[itlb_seen].tlb_tag = tag;
843 prom_itlb[itlb_seen].tlb_data = data;
845 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
847 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
848 spitfire_put_itlb_data(i, 0x0UL);
855 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
856 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
858 for (i = 0; i < high; i++) {
861 data = cheetah_get_ldtlb_data(i);
862 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
865 tag = cheetah_get_ldtlb_tag(i);
867 prom_dtlb[dtlb_seen].tlb_ent = i;
868 prom_dtlb[dtlb_seen].tlb_tag = tag;
869 prom_dtlb[dtlb_seen].tlb_data = data;
871 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
873 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
874 cheetah_put_ldtlb_data(i, 0x0UL);
882 for (i = 0; i < high; i++) {
885 data = cheetah_get_litlb_data(i);
886 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
889 tag = cheetah_get_litlb_tag(i);
891 prom_itlb[itlb_seen].tlb_ent = i;
892 prom_itlb[itlb_seen].tlb_tag = tag;
893 prom_itlb[itlb_seen].tlb_data = data;
895 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
897 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
898 cheetah_put_litlb_data(i, 0x0UL);
906 /* Implement me :-) */
913 /* Give PROM back his world, done during reboots... */
914 void prom_reload_locked(void)
918 for (i = 0; i < 16; i++) {
919 if (prom_dtlb[i].tlb_ent != -1) {
920 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
922 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
924 if (tlb_type == spitfire)
925 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
926 prom_dtlb[i].tlb_data);
927 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
928 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
929 prom_dtlb[i].tlb_data);
932 if (prom_itlb[i].tlb_ent != -1) {
933 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
935 : : "r" (prom_itlb[i].tlb_tag),
936 "r" (TLB_TAG_ACCESS),
938 if (tlb_type == spitfire)
939 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
940 prom_itlb[i].tlb_data);
942 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
943 prom_itlb[i].tlb_data);
948 void __flush_dcache_range(unsigned long start, unsigned long end)
952 if (tlb_type == spitfire) {
955 for (va = start; va < end; va += 32) {
956 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
963 for (va = start; va < end; va += 32)
964 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
968 "i" (ASI_DCACHE_INVALIDATE));
972 /* If not locked, zap it. */
973 void __flush_tlb_all(void)
975 unsigned long pstate;
978 __asm__ __volatile__("flushw\n\t"
979 "rdpr %%pstate, %0\n\t"
980 "wrpr %0, %1, %%pstate"
983 if (tlb_type == spitfire) {
984 for (i = 0; i < 64; i++) {
985 /* Spitfire Errata #32 workaround */
986 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
990 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
992 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
993 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
996 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
997 spitfire_put_dtlb_data(i, 0x0UL);
1000 /* Spitfire Errata #32 workaround */
1001 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1005 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1007 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
1008 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1011 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1012 spitfire_put_itlb_data(i, 0x0UL);
1015 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1016 cheetah_flush_dtlb_all();
1017 cheetah_flush_itlb_all();
1019 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1023 /* Caller does TLB context flushing on local CPU if necessary.
1024 * The caller also ensures that CTX_VALID(mm->context) is false.
1026 * We must be careful about boundary cases so that we never
1027 * let the user have CTX 0 (nucleus) or we ever use a CTX
1028 * version of zero (and thus NO_CONTEXT would not be caught
1029 * by version mis-match tests in mmu_context.h).
1031 void get_new_mmu_context(struct mm_struct *mm)
1033 unsigned long ctx, new_ctx;
1035 spin_lock(&ctx_alloc_lock);
1036 ctx = CTX_HWBITS(tlb_context_cache + 1);
1037 new_ctx = find_next_zero_bit(mmu_context_bmap, 1UL << CTX_VERSION_SHIFT, ctx);
1038 if (new_ctx >= (1UL << CTX_VERSION_SHIFT)) {
1039 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1040 if (new_ctx >= ctx) {
1042 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1045 new_ctx = CTX_FIRST_VERSION;
1047 /* Don't call memset, for 16 entries that's just
1050 mmu_context_bmap[0] = 3;
1051 mmu_context_bmap[1] = 0;
1052 mmu_context_bmap[2] = 0;
1053 mmu_context_bmap[3] = 0;
1054 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1055 mmu_context_bmap[i + 0] = 0;
1056 mmu_context_bmap[i + 1] = 0;
1057 mmu_context_bmap[i + 2] = 0;
1058 mmu_context_bmap[i + 3] = 0;
1063 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1064 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1066 tlb_context_cache = new_ctx;
1067 spin_unlock(&ctx_alloc_lock);
1069 mm->context = new_ctx;
1073 struct pgtable_cache_struct pgt_quicklists;
1076 /* OK, we have to color these pages. The page tables are accessed
1077 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1078 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1079 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1080 * fast handlers do not get data from old/garbage dcache lines that
1081 * correspond to an old/stale virtual address (user/kernel) that
1082 * previously mapped the pagetable page while accessing vpte range
1083 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1084 * color is the same, then when the kernel initializes the pagetable
1085 * using the later address range, accesses with the first address
1086 * range will see the newly initialized data rather than the garbage.
1088 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1089 #define DC_ALIAS_SHIFT 1
1091 #define DC_ALIAS_SHIFT 0
1093 pte_t *__pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
1096 unsigned long color;
1099 pte_t *ptep = pte_alloc_one_fast(mm, address);
1105 color = VPTE_COLOR(address);
1106 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, DC_ALIAS_SHIFT);
1108 unsigned long *to_free;
1109 unsigned long paddr;
1112 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1113 set_page_count(page, 1);
1114 ClearPageCompound(page);
1116 set_page_count((page + 1), 1);
1117 ClearPageCompound(page + 1);
1119 paddr = (unsigned long) page_address(page);
1120 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1123 pte = (pte_t *) paddr;
1124 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1126 pte = (pte_t *) (paddr + PAGE_SIZE);
1127 to_free = (unsigned long *) paddr;
1130 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1131 /* Now free the other one up, adjust cache size. */
1133 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1134 pte_quicklist[color ^ 0x1] = to_free;
1135 pgtable_cache_size++;
1144 void sparc_ultra_dump_itlb(void)
1148 if (tlb_type == spitfire) {
1149 printk ("Contents of itlb: ");
1150 for (slot = 0; slot < 14; slot++) printk (" ");
1151 printk ("%2x:%016lx,%016lx\n",
1153 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1154 for (slot = 1; slot < 64; slot+=3) {
1155 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1157 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1159 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1161 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1163 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1164 printk ("Contents of itlb0:\n");
1165 for (slot = 0; slot < 16; slot+=2) {
1166 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1168 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1170 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1172 printk ("Contents of itlb2:\n");
1173 for (slot = 0; slot < 128; slot+=2) {
1174 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1176 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1178 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1183 void sparc_ultra_dump_dtlb(void)
1187 if (tlb_type == spitfire) {
1188 printk ("Contents of dtlb: ");
1189 for (slot = 0; slot < 14; slot++) printk (" ");
1190 printk ("%2x:%016lx,%016lx\n", 0,
1191 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1192 for (slot = 1; slot < 64; slot+=3) {
1193 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1195 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1197 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1199 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1201 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1202 printk ("Contents of dtlb0:\n");
1203 for (slot = 0; slot < 16; slot+=2) {
1204 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1206 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1208 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1210 printk ("Contents of dtlb2:\n");
1211 for (slot = 0; slot < 512; slot+=2) {
1212 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1214 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1216 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1218 if (tlb_type == cheetah_plus) {
1219 printk ("Contents of dtlb3:\n");
1220 for (slot = 0; slot < 512; slot+=2) {
1221 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1223 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1225 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1231 extern unsigned long cmdline_memory_size;
1233 unsigned long __init bootmem_init(unsigned long *pages_avail)
1235 unsigned long bootmap_size, start_pfn, end_pfn;
1236 unsigned long end_of_phys_memory = 0UL;
1237 unsigned long bootmap_pfn, bytes_avail, size;
1240 #ifdef CONFIG_DEBUG_BOOTMEM
1241 prom_printf("bootmem_init: Scan sp_banks, ");
1245 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1246 end_of_phys_memory = sp_banks[i].base_addr +
1247 sp_banks[i].num_bytes;
1248 bytes_avail += sp_banks[i].num_bytes;
1249 if (cmdline_memory_size) {
1250 if (bytes_avail > cmdline_memory_size) {
1251 unsigned long slack = bytes_avail - cmdline_memory_size;
1253 bytes_avail -= slack;
1254 end_of_phys_memory -= slack;
1256 sp_banks[i].num_bytes -= slack;
1257 if (sp_banks[i].num_bytes == 0) {
1258 sp_banks[i].base_addr = 0xdeadbeef;
1260 sp_banks[i+1].num_bytes = 0;
1261 sp_banks[i+1].base_addr = 0xdeadbeef;
1268 *pages_avail = bytes_avail >> PAGE_SHIFT;
1270 /* Start with page aligned address of last symbol in kernel
1271 * image. The kernel is hard mapped below PAGE_OFFSET in a
1272 * 4MB locked TLB translation.
1274 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1276 bootmap_pfn = start_pfn;
1278 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1280 #ifdef CONFIG_BLK_DEV_INITRD
1281 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1282 if (sparc_ramdisk_image) {
1283 if (sparc_ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
1284 sparc_ramdisk_image -= KERNBASE;
1285 initrd_start = sparc_ramdisk_image + phys_base;
1286 initrd_end = initrd_start + sparc_ramdisk_size;
1287 if (initrd_end > end_of_phys_memory) {
1288 printk(KERN_CRIT "initrd extends beyond end of memory "
1289 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1290 initrd_end, end_of_phys_memory);
1294 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1295 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1296 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1300 /* Initialize the boot-time allocator. */
1301 max_pfn = max_low_pfn = end_pfn;
1302 min_low_pfn = pfn_base;
1304 #ifdef CONFIG_DEBUG_BOOTMEM
1305 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1306 min_low_pfn, bootmap_pfn, max_low_pfn);
1308 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
1310 bootmap_base = bootmap_pfn << PAGE_SHIFT;
1312 /* Now register the available physical memory with the
1315 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1316 #ifdef CONFIG_DEBUG_BOOTMEM
1317 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1318 i, sp_banks[i].base_addr, sp_banks[i].num_bytes);
1320 free_bootmem(sp_banks[i].base_addr, sp_banks[i].num_bytes);
1323 #ifdef CONFIG_BLK_DEV_INITRD
1325 size = initrd_end - initrd_start;
1327 /* Resert the initrd image area. */
1328 reserve_bootmem(initrd_start, size);
1329 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1331 initrd_start += PAGE_OFFSET;
1332 initrd_end += PAGE_OFFSET;
1335 /* Reserve the kernel text/data/bss. */
1336 #ifdef CONFIG_DEBUG_BOOTMEM
1337 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1339 reserve_bootmem(kern_base, kern_size);
1340 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1342 /* Reserve the bootmem map. We do not account for it
1343 * in pages_avail because we will release that memory
1344 * in free_all_bootmem.
1346 size = bootmap_size;
1347 #ifdef CONFIG_DEBUG_BOOTMEM
1348 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1349 (bootmap_pfn << PAGE_SHIFT), size);
1351 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1352 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1357 /* paging_init() sets up the page tables */
1359 extern void cheetah_ecache_flush_init(void);
1361 static unsigned long last_valid_pfn;
1363 void __init paging_init(void)
1365 extern pmd_t swapper_pmd_dir[1024];
1366 extern unsigned int sparc64_vpte_patchme1[1];
1367 extern unsigned int sparc64_vpte_patchme2[1];
1368 unsigned long alias_base = kern_base + PAGE_OFFSET;
1369 unsigned long second_alias_page = 0;
1370 unsigned long pt, flags, end_pfn, pages_avail;
1371 unsigned long shift = alias_base - ((unsigned long)KERNBASE);
1372 unsigned long real_end;
1374 set_bit(0, mmu_context_bmap);
1376 real_end = (unsigned long)_end;
1377 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1379 #ifdef CONFIG_BLK_DEV_INITRD
1380 if (sparc_ramdisk_image)
1381 real_end = (PAGE_ALIGN(real_end) + PAGE_ALIGN(sparc_ramdisk_size));
1384 /* We assume physical memory starts at some 4mb multiple,
1385 * if this were not true we wouldn't boot up to this point
1388 pt = kern_base | _PAGE_VALID | _PAGE_SZ4MB;
1389 pt |= _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W;
1390 local_irq_save(flags);
1391 if (tlb_type == spitfire) {
1392 __asm__ __volatile__(
1393 " stxa %1, [%0] %3\n"
1394 " stxa %2, [%5] %4\n"
1401 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1402 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (61 << 3)
1404 if (real_end >= KERNBASE + 0x340000) {
1405 second_alias_page = alias_base + 0x400000;
1406 __asm__ __volatile__(
1407 " stxa %1, [%0] %3\n"
1408 " stxa %2, [%5] %4\n"
1415 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1416 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (60 << 3)
1419 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1420 __asm__ __volatile__(
1421 " stxa %1, [%0] %3\n"
1422 " stxa %2, [%5] %4\n"
1429 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1430 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (13<<3))
1432 if (real_end >= KERNBASE + 0x340000) {
1433 second_alias_page = alias_base + 0x400000;
1434 __asm__ __volatile__(
1435 " stxa %1, [%0] %3\n"
1436 " stxa %2, [%5] %4\n"
1443 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1444 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (12<<3))
1448 local_irq_restore(flags);
1450 /* Now set kernel pgd to upper alias so physical page computations
1453 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1455 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1457 /* Now can init the kernel/bad page tables. */
1458 pgd_set(&swapper_pg_dir[0], swapper_pmd_dir + (shift / sizeof(pgd_t)));
1460 sparc64_vpte_patchme1[0] |=
1461 (((unsigned long)pgd_val(init_mm.pgd[0])) >> 10);
1462 sparc64_vpte_patchme2[0] |=
1463 (((unsigned long)pgd_val(init_mm.pgd[0])) & 0x3ff);
1464 flushi((long)&sparc64_vpte_patchme1[0]);
1466 /* Setup bootmem... */
1468 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1470 /* Inherit non-locked OBP mappings. */
1471 inherit_prom_mappings();
1473 /* Ok, we can use our TLB miss and window trap handlers safely.
1474 * We need to do a quick peek here to see if we are on StarFire
1475 * or not, so setup_tba can setup the IRQ globals correctly (it
1476 * needs to get the hard smp processor id correctly).
1479 extern void setup_tba(int);
1480 setup_tba(this_is_starfire);
1483 inherit_locked_prom_mappings(1);
1485 /* We only created DTLB mapping of this stuff. */
1486 spitfire_flush_dtlb_nucleus_page(alias_base);
1487 if (second_alias_page)
1488 spitfire_flush_dtlb_nucleus_page(second_alias_page);
1493 unsigned long zones_size[MAX_NR_ZONES];
1494 unsigned long zholes_size[MAX_NR_ZONES];
1495 unsigned long npages;
1498 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1499 zones_size[znum] = zholes_size[znum] = 0;
1501 npages = end_pfn - pfn_base;
1502 zones_size[ZONE_DMA] = npages;
1503 zholes_size[ZONE_DMA] = npages - pages_avail;
1505 free_area_init_node(0, &contig_page_data, NULL, zones_size,
1506 phys_base >> PAGE_SHIFT, zholes_size);
1507 mem_map = contig_page_data.node_mem_map;
1513 /* Ok, it seems that the prom can allocate some more memory chunks
1514 * as a side effect of some prom calls we perform during the
1515 * boot sequence. My most likely theory is that it is from the
1516 * prom_set_traptable() call, and OBP is allocating a scratchpad
1517 * for saving client program register state etc.
1519 static void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1523 unsigned long tmpaddr, tmpsize;
1524 unsigned long lowest;
1526 for (i = 0; thislist[i].theres_more != 0; i++) {
1527 lowest = thislist[i].start_adr;
1528 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1529 if (thislist[mitr].start_adr < lowest) {
1530 lowest = thislist[mitr].start_adr;
1533 if (lowest == thislist[i].start_adr)
1535 tmpaddr = thislist[swapi].start_adr;
1536 tmpsize = thislist[swapi].num_bytes;
1537 for (mitr = swapi; mitr > i; mitr--) {
1538 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1539 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1541 thislist[i].start_adr = tmpaddr;
1542 thislist[i].num_bytes = tmpsize;
1546 void __init rescan_sp_banks(void)
1548 struct linux_prom64_registers memlist[64];
1549 struct linux_mlist_p1275 avail[64], *mlist;
1550 unsigned long bytes, base_paddr;
1551 int num_regs, node = prom_finddevice("/memory");
1554 num_regs = prom_getproperty(node, "available",
1555 (char *) memlist, sizeof(memlist));
1556 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1557 for (i = 0; i < num_regs; i++) {
1558 avail[i].start_adr = memlist[i].phys_addr;
1559 avail[i].num_bytes = memlist[i].reg_size;
1560 avail[i].theres_more = &avail[i + 1];
1562 avail[i - 1].theres_more = NULL;
1563 sort_memlist(avail);
1567 bytes = mlist->num_bytes;
1568 base_paddr = mlist->start_adr;
1570 sp_banks[0].base_addr = base_paddr;
1571 sp_banks[0].num_bytes = bytes;
1573 while (mlist->theres_more != NULL){
1575 mlist = mlist->theres_more;
1576 bytes = mlist->num_bytes;
1577 if (i >= SPARC_PHYS_BANKS-1) {
1578 printk ("The machine has more banks than "
1579 "this kernel can support\n"
1580 "Increase the SPARC_PHYS_BANKS "
1581 "setting (currently %d)\n",
1583 i = SPARC_PHYS_BANKS-1;
1587 sp_banks[i].base_addr = mlist->start_adr;
1588 sp_banks[i].num_bytes = mlist->num_bytes;
1592 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1593 sp_banks[i].num_bytes = 0;
1595 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1596 sp_banks[i].num_bytes &= PAGE_MASK;
1599 static void __init taint_real_pages(void)
1601 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1604 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1605 saved_sp_banks[i].base_addr =
1606 sp_banks[i].base_addr;
1607 saved_sp_banks[i].num_bytes =
1608 sp_banks[i].num_bytes;
1613 /* Find changes discovered in the sp_bank rescan and
1614 * reserve the lost portions in the bootmem maps.
1616 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1617 unsigned long old_start, old_end;
1619 old_start = saved_sp_banks[i].base_addr;
1620 old_end = old_start +
1621 saved_sp_banks[i].num_bytes;
1622 while (old_start < old_end) {
1625 for (n = 0; sp_banks[n].num_bytes; n++) {
1626 unsigned long new_start, new_end;
1628 new_start = sp_banks[n].base_addr;
1629 new_end = new_start + sp_banks[n].num_bytes;
1631 if (new_start <= old_start &&
1632 new_end >= (old_start + PAGE_SIZE)) {
1633 set_bit (old_start >> 22,
1634 sparc64_valid_addr_bitmap);
1638 reserve_bootmem(old_start, PAGE_SIZE);
1641 old_start += PAGE_SIZE;
1646 void __init mem_init(void)
1648 unsigned long codepages, datapages, initpages;
1649 unsigned long addr, last;
1652 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1654 sparc64_valid_addr_bitmap = (unsigned long *)
1655 __alloc_bootmem(i << 3, SMP_CACHE_BYTES, bootmap_base);
1656 if (sparc64_valid_addr_bitmap == NULL) {
1657 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1660 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1662 addr = PAGE_OFFSET + kern_base;
1663 last = PAGE_ALIGN(kern_size) + addr;
1664 while (addr < last) {
1665 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1671 max_mapnr = last_valid_pfn - pfn_base;
1672 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1674 #ifdef CONFIG_DEBUG_BOOTMEM
1675 prom_printf("mem_init: Calling free_all_bootmem().\n");
1677 totalram_pages = num_physpages = free_all_bootmem() - 1;
1680 * Set up the zero page, mark it reserved, so that page count
1681 * is not manipulated when freeing the page from user ptes.
1683 mem_map_zero = alloc_pages(GFP_KERNEL, 0);
1684 if (mem_map_zero == NULL) {
1685 prom_printf("paging_init: Cannot alloc zero page.\n");
1688 SetPageReserved(mem_map_zero);
1689 clear_page(page_address(mem_map_zero));
1691 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1692 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1693 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1694 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1695 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1696 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1700 /* Put empty_pg_dir on pgd_quicklist */
1701 extern pgd_t empty_pg_dir[1024];
1702 unsigned long addr = (unsigned long)empty_pg_dir;
1703 unsigned long alias_base = kern_base + PAGE_OFFSET -
1706 memset(empty_pg_dir, 0, sizeof(empty_pg_dir));
1708 free_pgd_fast((pgd_t *)addr);
1714 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1715 nr_free_pages() << (PAGE_SHIFT-10),
1716 codepages << (PAGE_SHIFT-10),
1717 datapages << (PAGE_SHIFT-10),
1718 initpages << (PAGE_SHIFT-10),
1719 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1721 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1722 cheetah_ecache_flush_init();
1725 void free_initmem (void)
1727 unsigned long addr, initend;
1730 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1732 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1733 initend = (unsigned long)(__init_end) & PAGE_MASK;
1734 for (; addr < initend; addr += PAGE_SIZE) {
1739 ((unsigned long) __va(kern_base)) -
1740 ((unsigned long) KERNBASE));
1741 p = virt_to_page(page);
1743 ClearPageReserved(p);
1744 set_page_count(p, 1);
1751 #ifdef CONFIG_BLK_DEV_INITRD
1752 void free_initrd_mem(unsigned long start, unsigned long end)
1755 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1756 for (; start < end; start += PAGE_SIZE) {
1757 struct page *p = virt_to_page(start);
1759 ClearPageReserved(p);
1760 set_page_count(p, 1);