1 /* $Id: init.c,v 1.207 2001/11/30 06:55:39 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/slab.h>
16 #include <linux/blk.h>
17 #include <linux/swap.h>
18 #include <linux/swapctl.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>
34 #include <asm/starfire.h>
36 #include <asm/spitfire.h>
37 #include <asm/sections.h>
39 mmu_gather_t mmu_gathers[NR_CPUS];
41 extern void device_scan(void);
43 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
45 unsigned long *sparc64_valid_addr_bitmap;
47 /* Ugly, but necessary... -DaveM */
48 unsigned long phys_base, kern_base, kern_size;
50 /* This is even uglier. We have a problem where the kernel may not be
51 * located at phys_base. However, initial __alloc_bootmem() calls need to
52 * be adjusted to be within the 4-8Megs that the kernel is mapped to, else
53 * those page mappings wont work. Things are ok after inherit_prom_mappings
54 * is called though. Dave says he'll clean this up some other time.
57 static unsigned long bootmap_base;
59 /* get_new_mmu_context() uses "cache + 1". */
60 spinlock_t ctx_alloc_lock = SPIN_LOCK_UNLOCKED;
61 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
62 #define CTX_BMAP_SLOTS (1UL << (CTX_VERSION_SHIFT - 6))
63 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
65 /* Initial ramdisk setup */
66 extern unsigned long sparc_ramdisk_image64;
67 extern unsigned int sparc_ramdisk_image;
68 extern unsigned int sparc_ramdisk_size;
70 struct page *mem_map_zero;
74 int do_check_pgt_cache(int low, int high)
78 if (pgtable_cache_size > high) {
82 free_pgd_slow(get_pgd_fast()), freed++;
85 free_pte_slow(pte_alloc_one_fast(NULL, 0)), freed++;
87 free_pte_slow(pte_alloc_one_fast(NULL, 1 << (PAGE_SHIFT + 10))), freed++;
88 } while (pgtable_cache_size > low);
91 if (pgd_cache_size > high / 4) {
92 struct page *page, *page2;
93 for (page2 = NULL, page = (struct page *)pgd_quicklist; page;) {
94 if ((unsigned long)page->pprev_hash == 3) {
96 page2->next_hash = page->next_hash;
98 pgd_quicklist = (unsigned long *)page->next_hash;
99 page->next_hash = NULL;
100 page->pprev_hash = NULL;
105 page = page2->next_hash;
107 page = (struct page *)pgd_quicklist;
108 if (pgd_cache_size <= low / 4)
113 page = page->next_hash;
120 extern void __update_mmu_cache(struct vm_area_struct *, unsigned long, pte_t);
122 #ifdef CONFIG_DEBUG_DCFLUSH
123 atomic_t dcpage_flushes = ATOMIC_INIT(0);
125 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
129 __inline__ void flush_dcache_page_impl(struct page *page)
131 #ifdef CONFIG_DEBUG_DCFLUSH
132 atomic_inc(&dcpage_flushes);
135 #if (L1DCACHE_SIZE > PAGE_SIZE)
136 __flush_dcache_page(page->virtual,
137 ((tlb_type == spitfire) &&
138 page->mapping != NULL));
140 if (page->mapping != NULL &&
141 tlb_type == spitfire)
142 __flush_icache_page(__pa(page->virtual));
146 #define PG_dcache_dirty PG_arch_1
148 #define dcache_dirty_cpu(page) \
149 (((page)->flags >> 24) & (NR_CPUS - 1UL))
151 static __inline__ void set_dcache_dirty(struct page *page)
153 unsigned long mask = smp_processor_id();
154 unsigned long non_cpu_bits = (1UL << 24UL) - 1UL;
155 mask = (mask << 24) | (1UL << PG_dcache_dirty);
156 __asm__ __volatile__("1:\n\t"
158 "and %%g7, %1, %%g5\n\t"
159 "or %%g5, %0, %%g5\n\t"
160 "casx [%2], %%g7, %%g5\n\t"
162 "bne,pn %%xcc, 1b\n\t"
163 " membar #StoreLoad | #StoreStore"
165 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
169 static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
171 unsigned long mask = (1UL << PG_dcache_dirty);
173 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
176 "srlx %%g7, 24, %%g5\n\t"
178 "bne,pn %%icc, 2f\n\t"
179 " andn %%g7, %1, %%g5\n\t"
180 "casx [%2], %%g7, %%g5\n\t"
182 "bne,pn %%xcc, 1b\n\t"
183 " membar #StoreLoad | #StoreStore\n"
186 : "r" (cpu), "r" (mask), "r" (&page->flags)
190 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
192 struct page *page = pte_page(pte);
193 unsigned long pg_flags;
195 if (VALID_PAGE(page) &&
197 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
198 int cpu = (pg_flags >> 24);
200 /* This is just to optimize away some function calls
203 if (cpu == smp_processor_id())
204 flush_dcache_page_impl(page);
206 smp_flush_dcache_page_impl(page, cpu);
208 clear_dcache_dirty_cpu(page, cpu);
210 __update_mmu_cache(vma, address, pte);
213 void flush_dcache_page(struct page *page)
215 int dirty = test_bit(PG_dcache_dirty, &page->flags);
216 int dirty_cpu = dcache_dirty_cpu(page);
219 page->mapping->i_mmap == NULL &&
220 page->mapping->i_mmap_shared == NULL) {
222 if (dirty_cpu == smp_processor_id())
224 smp_flush_dcache_page_impl(page, dirty_cpu);
226 set_dcache_dirty(page);
228 /* We could delay the flush for the !page->mapping
229 * case too. But that case is for exec env/arg
230 * pages and those are %99 certainly going to get
231 * faulted into the tlb (and thus flushed) anyways.
233 flush_dcache_page_impl(page);
237 void flush_icache_range(unsigned long start, unsigned long end)
239 /* Cheetah has coherent I-cache. */
240 if (tlb_type == spitfire) {
243 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
244 __flush_icache_page(__get_phys(kaddr));
250 printk("Mem-info:\n");
252 printk("Free swap: %6dkB\n",
253 nr_swap_pages << (PAGE_SHIFT-10));
254 printk("%ld pages of RAM\n", num_physpages);
255 printk("%d free pages\n", nr_free_pages());
256 printk("%d pages in page table cache\n",pgtable_cache_size);
258 printk("%d entries in page dir cache\n",pgd_cache_size);
263 void mmu_info(struct seq_file *m)
265 if (tlb_type == cheetah)
266 seq_printf(m, "MMU Type\t: Cheetah\n");
267 else if (tlb_type == cheetah_plus)
268 seq_printf(m, "MMU Type\t: Cheetah+\n");
269 else if (tlb_type == spitfire)
270 seq_printf(m, "MMU Type\t: Spitfire\n");
272 seq_printf(m, "MMU Type\t: ???\n");
274 #ifdef CONFIG_DEBUG_DCFLUSH
275 seq_printf(m, "DCPageFlushes\t: %d\n",
276 atomic_read(&dcpage_flushes));
278 seq_printf(m, "DCPageFlushesXC\t: %d\n",
279 atomic_read(&dcpage_flushes_xcall));
280 #endif /* CONFIG_SMP */
281 #endif /* CONFIG_DEBUG_DCFLUSH */
284 struct linux_prom_translation {
290 extern unsigned long prom_boot_page;
291 extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
292 extern int prom_get_mmu_ihandle(void);
293 extern void register_prom_callbacks(void);
295 /* Exported for SMP bootup purposes. */
296 unsigned long kern_locked_tte_data;
298 void __init early_pgtable_allocfail(char *type)
300 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
304 #define BASE_PAGE_SIZE 8192
305 static pmd_t *prompmd;
308 * Translate PROM's mapping we capture at boot time into physical address.
309 * The second parameter is only set from prom_callback() invocations.
311 unsigned long prom_virt_to_phys(unsigned long promva, int *error)
313 pmd_t *pmdp = prompmd + ((promva >> 23) & 0x7ff);
317 if (pmd_none(*pmdp)) {
322 ptep = (pte_t *)pmd_page(*pmdp) + ((promva >> 13) & 0x3ff);
323 if (!pte_present(*ptep)) {
330 return(pte_val(*ptep));
332 base = pte_val(*ptep) & _PAGE_PADDR;
333 return(base + (promva & (BASE_PAGE_SIZE - 1)));
336 static void inherit_prom_mappings(void)
338 struct linux_prom_translation *trans;
339 unsigned long phys_page, tte_vaddr, tte_data;
340 void (*remap_func)(unsigned long, unsigned long, int);
344 extern unsigned int obp_iaddr_patch[2], obp_daddr_patch[2];
346 node = prom_finddevice("/virtual-memory");
347 n = prom_getproplen(node, "translations");
348 if (n == 0 || n == -1) {
349 prom_printf("Couldn't get translation property\n");
352 n += 5 * sizeof(struct linux_prom_translation);
353 for (tsz = 1; tsz < n; tsz <<= 1)
355 trans = __alloc_bootmem(tsz, SMP_CACHE_BYTES, bootmap_base);
357 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
360 memset(trans, 0, tsz);
362 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
363 prom_printf("Couldn't get translation property\n");
366 n = n / sizeof(*trans);
369 * The obp translations are saved based on 8k pagesize, since obp can use
370 * a mixture of pagesizes. Misses to the 0xf0000000 - 0x100000000, ie obp
371 * range, are handled in entry.S and do not use the vpte scheme (see rant
372 * in inherit_locked_prom_mappings()).
374 #define OBP_PMD_SIZE 2048
375 prompmd = __alloc_bootmem(OBP_PMD_SIZE, OBP_PMD_SIZE, bootmap_base);
377 early_pgtable_allocfail("pmd");
378 memset(prompmd, 0, OBP_PMD_SIZE);
379 for (i = 0; i < n; i++) {
382 if (trans[i].virt >= LOW_OBP_ADDRESS && trans[i].virt < HI_OBP_ADDRESS) {
383 for (vaddr = trans[i].virt;
384 ((vaddr < trans[i].virt + trans[i].size) &&
385 (vaddr < HI_OBP_ADDRESS));
386 vaddr += BASE_PAGE_SIZE) {
389 pmdp = prompmd + ((vaddr >> 23) & 0x7ff);
390 if (pmd_none(*pmdp)) {
391 ptep = __alloc_bootmem(BASE_PAGE_SIZE,
395 early_pgtable_allocfail("pte");
396 memset(ptep, 0, BASE_PAGE_SIZE);
399 ptep = (pte_t *)pmd_page(*pmdp) +
400 ((vaddr >> 13) & 0x3ff);
404 /* Clear diag TTE bits. */
405 if (tlb_type == spitfire)
406 val &= ~0x0003fe0000000000UL;
408 set_pte (ptep, __pte(val | _PAGE_MODIFIED));
409 trans[i].data += BASE_PAGE_SIZE;
413 phys_page = __pa(prompmd);
414 obp_iaddr_patch[0] |= (phys_page >> 10);
415 obp_iaddr_patch[1] |= (phys_page & 0x3ff);
416 flushi((long)&obp_iaddr_patch[0]);
417 obp_daddr_patch[0] |= (phys_page >> 10);
418 obp_daddr_patch[1] |= (phys_page & 0x3ff);
419 flushi((long)&obp_daddr_patch[0]);
421 /* Now fixup OBP's idea about where we really are mapped. */
422 prom_printf("Remapping the kernel... ");
424 /* Spitfire Errata #32 workaround */
425 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
429 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
434 phys_page = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
439 phys_page = cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
443 phys_page &= _PAGE_PADDR;
444 phys_page += ((unsigned long)&prom_boot_page -
445 (unsigned long)KERNBASE);
447 if (tlb_type == spitfire) {
448 /* Lock this into i/d tlb entry 59 */
449 __asm__ __volatile__(
450 "stxa %%g0, [%2] %3\n\t"
451 "stxa %0, [%1] %4\n\t"
454 "stxa %%g0, [%2] %5\n\t"
455 "stxa %0, [%1] %6\n\t"
458 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
459 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
460 "r" (59 << 3), "r" (TLB_TAG_ACCESS),
461 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
462 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
464 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
465 /* Lock this into i/d tlb-0 entry 11 */
466 __asm__ __volatile__(
467 "stxa %%g0, [%2] %3\n\t"
468 "stxa %0, [%1] %4\n\t"
471 "stxa %%g0, [%2] %5\n\t"
472 "stxa %0, [%1] %6\n\t"
475 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
476 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
477 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS),
478 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
479 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
482 /* Implement me :-) */
486 tte_vaddr = (unsigned long) KERNBASE;
488 /* Spitfire Errata #32 workaround */
489 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
493 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
495 if (tlb_type == spitfire)
496 tte_data = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
498 tte_data = cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
500 kern_locked_tte_data = tte_data;
502 remap_func = (void *) ((unsigned long) &prom_remap -
503 (unsigned long) &prom_boot_page);
506 /* Spitfire Errata #32 workaround */
507 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
511 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
513 remap_func((tlb_type == spitfire ?
514 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR) :
515 (cheetah_get_litlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR)),
516 (unsigned long) KERNBASE,
517 prom_get_mmu_ihandle());
520 remap_func(((tte_data + 0x400000) & _PAGE_PADDR),
521 (unsigned long) KERNBASE + 0x400000, prom_get_mmu_ihandle());
523 /* Flush out that temporary mapping. */
524 spitfire_flush_dtlb_nucleus_page(0x0);
525 spitfire_flush_itlb_nucleus_page(0x0);
527 /* Now lock us back into the TLBs via OBP. */
528 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
529 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
531 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
532 tte_vaddr + 0x400000);
533 prom_itlb_load(sparc64_highest_locked_tlbent()-1, tte_data + 0x400000,
534 tte_vaddr + 0x400000);
537 /* Re-read translations property. */
538 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
539 prom_printf("Couldn't get translation property\n");
542 n = n / sizeof(*trans);
544 for (i = 0; i < n; i++) {
545 unsigned long vaddr = trans[i].virt;
546 unsigned long size = trans[i].size;
548 if (vaddr < 0xf0000000UL) {
549 unsigned long avoid_start = (unsigned long) KERNBASE;
550 unsigned long avoid_end = avoid_start + (4 * 1024 * 1024);
553 avoid_end += (4 * 1024 * 1024);
554 if (vaddr < avoid_start) {
555 unsigned long top = vaddr + size;
557 if (top > avoid_start)
559 prom_unmap(top - vaddr, vaddr);
561 if ((vaddr + size) > avoid_end) {
562 unsigned long bottom = vaddr;
564 if (bottom < avoid_end)
566 prom_unmap((vaddr + size) - bottom, bottom);
571 prom_printf("done.\n");
573 register_prom_callbacks();
576 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
577 * upwards as reserved for use by the firmware (I wonder if this
578 * will be the same on Cheetah...). We use this virtual address
579 * range for the VPTE table mappings of the nucleus so we need
580 * to zap them when we enter the PROM. -DaveM
582 static void __flush_nucleus_vptes(void)
584 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
587 /* Only DTLB must be checked for VPTE entries. */
588 if (tlb_type == spitfire) {
589 for (i = 0; i < 63; i++) {
592 /* Spitfire Errata #32 workaround */
593 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
597 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
599 tag = spitfire_get_dtlb_tag(i);
600 if (((tag & ~(PAGE_MASK)) == 0) &&
601 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
602 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
605 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
606 spitfire_put_dtlb_data(i, 0x0UL);
609 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
610 for (i = 0; i < 512; i++) {
611 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
613 if ((tag & ~PAGE_MASK) == 0 &&
614 (tag & PAGE_MASK) >= prom_reserved_base) {
615 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
618 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
619 cheetah_put_dtlb_data(i, 0x0UL, 2);
622 if (tlb_type != cheetah_plus)
625 tag = cheetah_get_dtlb_tag(i, 3);
627 if ((tag & ~PAGE_MASK) == 0 &&
628 (tag & PAGE_MASK) >= prom_reserved_base) {
629 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
632 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
633 cheetah_put_dtlb_data(i, 0x0UL, 3);
637 /* Implement me :-) */
642 static int prom_ditlb_set;
643 struct prom_tlb_entry {
645 unsigned long tlb_tag;
646 unsigned long tlb_data;
648 struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
650 void prom_world(int enter)
652 unsigned long pstate;
656 set_fs(current->thread.current_ds);
661 /* Make sure the following runs atomically. */
662 __asm__ __volatile__("flushw\n\t"
663 "rdpr %%pstate, %0\n\t"
664 "wrpr %0, %1, %%pstate"
669 /* Kick out nucleus VPTEs. */
670 __flush_nucleus_vptes();
672 /* Install PROM world. */
673 for (i = 0; i < 16; i++) {
674 if (prom_dtlb[i].tlb_ent != -1) {
675 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
677 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
679 if (tlb_type == spitfire)
680 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
681 prom_dtlb[i].tlb_data);
682 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
683 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
684 prom_dtlb[i].tlb_data);
686 if (prom_itlb[i].tlb_ent != -1) {
687 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
689 : : "r" (prom_itlb[i].tlb_tag),
690 "r" (TLB_TAG_ACCESS),
692 if (tlb_type == spitfire)
693 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
694 prom_itlb[i].tlb_data);
695 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
696 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
697 prom_itlb[i].tlb_data);
701 for (i = 0; i < 16; i++) {
702 if (prom_dtlb[i].tlb_ent != -1) {
703 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
705 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
706 if (tlb_type == spitfire)
707 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
709 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
711 if (prom_itlb[i].tlb_ent != -1) {
712 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
714 : : "r" (TLB_TAG_ACCESS),
716 if (tlb_type == spitfire)
717 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
719 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
723 __asm__ __volatile__("wrpr %0, 0, %%pstate"
727 void inherit_locked_prom_mappings(int save_p)
733 /* Fucking losing PROM has more mappings in the TLB, but
734 * it (conveniently) fails to mention any of these in the
735 * translations property. The only ones that matter are
736 * the locked PROM tlb entries, so we impose the following
737 * irrecovable rule on the PROM, it is allowed 8 locked
738 * entries in the ITLB and 8 in the DTLB.
740 * Supposedly the upper 16GB of the address space is
741 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
742 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
743 * used between the client program and the firmware on sun5
744 * systems to coordinate mmu mappings is also COMPLETELY
745 * UNDOCUMENTED!!!!!! Thanks S(t)un!
748 for (i = 0; i < 16; i++) {
749 prom_itlb[i].tlb_ent = -1;
750 prom_dtlb[i].tlb_ent = -1;
753 if (tlb_type == spitfire) {
754 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
755 for (i = 0; i < high; i++) {
758 /* Spitfire Errata #32 workaround */
759 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
763 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
765 data = spitfire_get_dtlb_data(i);
766 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
769 /* Spitfire Errata #32 workaround */
770 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
774 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
776 tag = spitfire_get_dtlb_tag(i);
778 prom_dtlb[dtlb_seen].tlb_ent = i;
779 prom_dtlb[dtlb_seen].tlb_tag = tag;
780 prom_dtlb[dtlb_seen].tlb_data = data;
782 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
784 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
785 spitfire_put_dtlb_data(i, 0x0UL);
793 for (i = 0; i < high; i++) {
796 /* Spitfire Errata #32 workaround */
797 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
801 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
803 data = spitfire_get_itlb_data(i);
804 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
807 /* Spitfire Errata #32 workaround */
808 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
812 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
814 tag = spitfire_get_itlb_tag(i);
816 prom_itlb[itlb_seen].tlb_ent = i;
817 prom_itlb[itlb_seen].tlb_tag = tag;
818 prom_itlb[itlb_seen].tlb_data = data;
820 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
822 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
823 spitfire_put_itlb_data(i, 0x0UL);
830 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
831 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
833 for (i = 0; i < high; i++) {
836 data = cheetah_get_ldtlb_data(i);
837 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
840 tag = cheetah_get_ldtlb_tag(i);
842 prom_dtlb[dtlb_seen].tlb_ent = i;
843 prom_dtlb[dtlb_seen].tlb_tag = tag;
844 prom_dtlb[dtlb_seen].tlb_data = data;
846 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
848 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
849 cheetah_put_ldtlb_data(i, 0x0UL);
857 for (i = 0; i < high; i++) {
860 data = cheetah_get_litlb_data(i);
861 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
864 tag = cheetah_get_litlb_tag(i);
866 prom_itlb[itlb_seen].tlb_ent = i;
867 prom_itlb[itlb_seen].tlb_tag = tag;
868 prom_itlb[itlb_seen].tlb_data = data;
870 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
872 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
873 cheetah_put_litlb_data(i, 0x0UL);
881 /* Implement me :-) */
888 /* Give PROM back his world, done during reboots... */
889 void prom_reload_locked(void)
893 for (i = 0; i < 16; i++) {
894 if (prom_dtlb[i].tlb_ent != -1) {
895 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
897 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
899 if (tlb_type == spitfire)
900 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
901 prom_dtlb[i].tlb_data);
902 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
903 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
904 prom_dtlb[i].tlb_data);
907 if (prom_itlb[i].tlb_ent != -1) {
908 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
910 : : "r" (prom_itlb[i].tlb_tag),
911 "r" (TLB_TAG_ACCESS),
913 if (tlb_type == spitfire)
914 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
915 prom_itlb[i].tlb_data);
917 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
918 prom_itlb[i].tlb_data);
923 void __flush_dcache_range(unsigned long start, unsigned long end)
927 if (tlb_type == spitfire) {
930 for (va = start; va < end; va += 32) {
931 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
938 for (va = start; va < end; va += 32)
939 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
943 "i" (ASI_DCACHE_INVALIDATE));
947 void __flush_cache_all(void)
949 /* Cheetah should be fine here too. */
950 if (tlb_type == spitfire) {
954 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
955 spitfire_put_icache_tag(va, 0x0);
956 __asm__ __volatile__("flush %g6");
960 /* If not locked, zap it. */
961 void __flush_tlb_all(void)
963 unsigned long pstate;
966 __asm__ __volatile__("flushw\n\t"
967 "rdpr %%pstate, %0\n\t"
968 "wrpr %0, %1, %%pstate"
971 if (tlb_type == spitfire) {
972 for (i = 0; i < 64; i++) {
973 /* Spitfire Errata #32 workaround */
974 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
978 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
980 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
981 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
984 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
985 spitfire_put_dtlb_data(i, 0x0UL);
988 /* Spitfire Errata #32 workaround */
989 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
993 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
995 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
996 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
999 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1000 spitfire_put_itlb_data(i, 0x0UL);
1003 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1004 cheetah_flush_dtlb_all();
1005 cheetah_flush_itlb_all();
1007 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1011 /* Caller does TLB context flushing on local CPU if necessary.
1012 * The caller also ensures that CTX_VALID(mm->context) is false.
1014 * We must be careful about boundary cases so that we never
1015 * let the user have CTX 0 (nucleus) or we ever use a CTX
1016 * version of zero (and thus NO_CONTEXT would not be caught
1017 * by version mis-match tests in mmu_context.h).
1019 void get_new_mmu_context(struct mm_struct *mm)
1021 unsigned long ctx, new_ctx;
1023 spin_lock(&ctx_alloc_lock);
1024 ctx = CTX_HWBITS(tlb_context_cache + 1);
1025 new_ctx = find_next_zero_bit(mmu_context_bmap, 1UL << CTX_VERSION_SHIFT, ctx);
1026 if (new_ctx >= (1UL << CTX_VERSION_SHIFT)) {
1027 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1028 if (new_ctx >= ctx) {
1030 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1033 new_ctx = CTX_FIRST_VERSION;
1035 /* Don't call memset, for 16 entries that's just
1038 mmu_context_bmap[0] = 3;
1039 mmu_context_bmap[1] = 0;
1040 mmu_context_bmap[2] = 0;
1041 mmu_context_bmap[3] = 0;
1042 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1043 mmu_context_bmap[i + 0] = 0;
1044 mmu_context_bmap[i + 1] = 0;
1045 mmu_context_bmap[i + 2] = 0;
1046 mmu_context_bmap[i + 3] = 0;
1051 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1052 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1054 tlb_context_cache = new_ctx;
1055 spin_unlock(&ctx_alloc_lock);
1057 mm->context = new_ctx;
1061 struct pgtable_cache_struct pgt_quicklists;
1064 /* OK, we have to color these pages. The page tables are accessed
1065 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1066 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1067 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1068 * fast handlers do not get data from old/garbage dcache lines that
1069 * correspond to an old/stale virtual address (user/kernel) that
1070 * previously mapped the pagetable page while accessing vpte range
1071 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1072 * color is the same, then when the kernel initializes the pagetable
1073 * using the later address range, accesses with the first address
1074 * range will see the newly initialized data rather than the garbage.
1076 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1077 #define DC_ALIAS_SHIFT 1
1079 #define DC_ALIAS_SHIFT 0
1081 pte_t *pte_alloc_one(struct mm_struct *mm, unsigned long address)
1083 struct page *page = alloc_pages(GFP_KERNEL, DC_ALIAS_SHIFT);
1084 unsigned long color = VPTE_COLOR(address);
1087 unsigned long *to_free;
1088 unsigned long paddr;
1091 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1092 set_page_count((page + 1), 1);
1094 paddr = (unsigned long) page_address(page);
1095 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1098 pte = (pte_t *) paddr;
1099 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1101 pte = (pte_t *) (paddr + PAGE_SIZE);
1102 to_free = (unsigned long *) paddr;
1105 #if (L1DCACHE_SIZE > PAGE_SIZE) /* is there D$ aliasing problem */
1106 /* Now free the other one up, adjust cache size. */
1107 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1108 pte_quicklist[color ^ 0x1] = to_free;
1109 pgtable_cache_size++;
1117 void sparc_ultra_dump_itlb(void)
1121 if (tlb_type == spitfire) {
1122 printk ("Contents of itlb: ");
1123 for (slot = 0; slot < 14; slot++) printk (" ");
1124 printk ("%2x:%016lx,%016lx\n",
1126 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1127 for (slot = 1; slot < 64; slot+=3) {
1128 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1130 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1132 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1134 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1136 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1137 printk ("Contents of itlb0:\n");
1138 for (slot = 0; slot < 16; slot+=2) {
1139 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1141 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1143 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1145 printk ("Contents of itlb2:\n");
1146 for (slot = 0; slot < 128; slot+=2) {
1147 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1149 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1151 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1156 void sparc_ultra_dump_dtlb(void)
1160 if (tlb_type == spitfire) {
1161 printk ("Contents of dtlb: ");
1162 for (slot = 0; slot < 14; slot++) printk (" ");
1163 printk ("%2x:%016lx,%016lx\n", 0,
1164 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1165 for (slot = 1; slot < 64; slot+=3) {
1166 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1168 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1170 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1172 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1174 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1175 printk ("Contents of dtlb0:\n");
1176 for (slot = 0; slot < 16; slot+=2) {
1177 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1179 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1181 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1183 printk ("Contents of dtlb2:\n");
1184 for (slot = 0; slot < 512; slot+=2) {
1185 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1187 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1189 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1191 if (tlb_type == cheetah_plus) {
1192 printk ("Contents of dtlb3:\n");
1193 for (slot = 0; slot < 512; slot+=2) {
1194 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1196 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1198 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1204 extern unsigned long cmdline_memory_size;
1206 unsigned long __init bootmem_init(unsigned long *pages_avail)
1208 unsigned long bootmap_size, start_pfn, end_pfn;
1209 unsigned long end_of_phys_memory = 0UL;
1210 unsigned long bootmap_pfn, bytes_avail, size;
1213 #ifdef CONFIG_DEBUG_BOOTMEM
1214 prom_printf("bootmem_init: Scan sp_banks, ");
1218 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1219 end_of_phys_memory = sp_banks[i].base_addr +
1220 sp_banks[i].num_bytes;
1221 bytes_avail += sp_banks[i].num_bytes;
1222 if (cmdline_memory_size) {
1223 if (bytes_avail > cmdline_memory_size) {
1224 unsigned long slack = bytes_avail - cmdline_memory_size;
1226 bytes_avail -= slack;
1227 end_of_phys_memory -= slack;
1229 sp_banks[i].num_bytes -= slack;
1230 if (sp_banks[i].num_bytes == 0) {
1231 sp_banks[i].base_addr = 0xdeadbeef;
1233 sp_banks[i+1].num_bytes = 0;
1234 sp_banks[i+1].base_addr = 0xdeadbeef;
1241 *pages_avail = bytes_avail >> PAGE_SHIFT;
1243 /* Start with page aligned address of last symbol in kernel
1244 * image. The kernel is hard mapped below PAGE_OFFSET in a
1245 * 4MB locked TLB translation.
1247 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1249 bootmap_pfn = start_pfn;
1251 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1253 #ifdef CONFIG_BLK_DEV_INITRD
1254 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1255 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
1256 unsigned long ramdisk_image = sparc_ramdisk_image ?
1257 sparc_ramdisk_image : sparc_ramdisk_image64;
1258 if (ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
1259 ramdisk_image -= KERNBASE;
1260 initrd_start = ramdisk_image + phys_base;
1261 initrd_end = initrd_start + sparc_ramdisk_size;
1262 if (initrd_end > end_of_phys_memory) {
1263 printk(KERN_CRIT "initrd extends beyond end of memory "
1264 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1265 initrd_end, end_of_phys_memory);
1269 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1270 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1271 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1275 /* Initialize the boot-time allocator. */
1276 max_pfn = max_low_pfn = end_pfn;
1277 min_low_pfn = phys_base >> PAGE_SHIFT;
1279 #ifdef CONFIG_DEBUG_BOOTMEM
1280 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1281 min_low_pfn, bootmap_pfn, max_low_pfn);
1283 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, min_low_pfn, end_pfn);
1285 bootmap_base = bootmap_pfn << PAGE_SHIFT;
1287 /* Now register the available physical memory with the
1290 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1291 #ifdef CONFIG_DEBUG_BOOTMEM
1292 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1293 i, sp_banks[i].base_addr, sp_banks[i].num_bytes);
1295 free_bootmem(sp_banks[i].base_addr, sp_banks[i].num_bytes);
1298 #ifdef CONFIG_BLK_DEV_INITRD
1300 size = initrd_end - initrd_start;
1301 #ifdef CONFIG_DEBUG_BOOTMEM
1302 prom_printf("reserve_bootmem(initrd): base[%lx] size[%lx]\n",
1303 initrd_start, size);
1305 /* Resert the initrd image area. */
1306 #ifdef CONFIG_DEBUG_BOOTMEM
1307 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1308 initrd_start, initrd_end);
1310 reserve_bootmem(initrd_start, size);
1311 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1313 initrd_start += PAGE_OFFSET;
1314 initrd_end += PAGE_OFFSET;
1317 /* Reserve the kernel text/data/bss. */
1318 #ifdef CONFIG_DEBUG_BOOTMEM
1319 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1321 reserve_bootmem(kern_base, kern_size);
1322 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1324 /* Reserve the bootmem map. We do not account for it
1325 * in pages_avail because we will release that memory
1326 * in free_all_bootmem.
1328 size = bootmap_size;
1329 #ifdef CONFIG_DEBUG_BOOTMEM
1330 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1331 (bootmap_pfn << PAGE_SHIFT), size);
1333 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1334 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1339 /* paging_init() sets up the page tables */
1341 extern void sun_serial_setup(void);
1342 extern void cheetah_ecache_flush_init(void);
1344 static unsigned long last_valid_pfn;
1346 void __init paging_init(void)
1348 extern pmd_t swapper_pmd_dir[1024];
1349 extern unsigned int sparc64_vpte_patchme1[1];
1350 extern unsigned int sparc64_vpte_patchme2[1];
1351 unsigned long alias_base = kern_base + PAGE_OFFSET;
1352 unsigned long second_alias_page = 0;
1353 unsigned long pt, flags, end_pfn, pages_avail;
1354 unsigned long shift = alias_base - ((unsigned long)KERNBASE);
1355 unsigned long real_end;
1357 set_bit(0, mmu_context_bmap);
1359 real_end = (unsigned long)&_end;
1360 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1362 #ifdef CONFIG_BLK_DEV_INITRD
1363 if (sparc_ramdisk_image || sparc_ramdisk_image64)
1364 real_end = (PAGE_ALIGN(real_end) + PAGE_ALIGN(sparc_ramdisk_size));
1367 /* We assume physical memory starts at some 4mb multiple,
1368 * if this were not true we wouldn't boot up to this point
1371 pt = kern_base | _PAGE_VALID | _PAGE_SZ4MB;
1372 pt |= _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W;
1373 __save_and_cli(flags);
1374 if (tlb_type == spitfire) {
1375 __asm__ __volatile__(
1376 " stxa %1, [%0] %3\n"
1377 " stxa %2, [%5] %4\n"
1384 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1385 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (61 << 3)
1387 if (real_end >= KERNBASE + 0x340000) {
1388 second_alias_page = alias_base + 0x400000;
1389 __asm__ __volatile__(
1390 " stxa %1, [%0] %3\n"
1391 " stxa %2, [%5] %4\n"
1398 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1399 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (60 << 3)
1402 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1403 __asm__ __volatile__(
1404 " stxa %1, [%0] %3\n"
1405 " stxa %2, [%5] %4\n"
1412 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1413 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (13<<3))
1415 if (real_end >= KERNBASE + 0x340000) {
1416 second_alias_page = alias_base + 0x400000;
1417 __asm__ __volatile__(
1418 " stxa %1, [%0] %3\n"
1419 " stxa %2, [%5] %4\n"
1426 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1427 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (12<<3))
1431 __restore_flags(flags);
1433 /* Now set kernel pgd to upper alias so physical page computations
1436 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1438 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1440 /* Now can init the kernel/bad page tables. */
1441 pgd_set(&swapper_pg_dir[0], swapper_pmd_dir + (shift / sizeof(pgd_t)));
1443 sparc64_vpte_patchme1[0] |=
1444 (((unsigned long)pgd_val(init_mm.pgd[0])) >> 10);
1445 sparc64_vpte_patchme2[0] |=
1446 (((unsigned long)pgd_val(init_mm.pgd[0])) & 0x3ff);
1447 flushi((long)&sparc64_vpte_patchme1[0]);
1449 /* Setup bootmem... */
1451 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1453 /* Inherit non-locked OBP mappings. */
1454 inherit_prom_mappings();
1456 /* Ok, we can use our TLB miss and window trap handlers safely.
1457 * We need to do a quick peek here to see if we are on StarFire
1458 * or not, so setup_tba can setup the IRQ globals correctly (it
1459 * needs to get the hard smp processor id correctly).
1462 extern void setup_tba(int);
1463 setup_tba(this_is_starfire);
1466 inherit_locked_prom_mappings(1);
1468 #ifdef CONFIG_SUN_SERIAL
1469 /* This does not logically belong here, but we need to call it at
1470 * the moment we are able to use the bootmem allocator. This _has_
1471 * to be done after the prom_mappings above so since
1472 * __alloc_bootmem() doesn't work correctly until then.
1477 /* We only created DTLB mapping of this stuff. */
1478 spitfire_flush_dtlb_nucleus_page(alias_base);
1479 if (second_alias_page)
1480 spitfire_flush_dtlb_nucleus_page(second_alias_page);
1485 unsigned long zones_size[MAX_NR_ZONES];
1486 unsigned long zholes_size[MAX_NR_ZONES];
1487 unsigned long npages;
1490 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1491 zones_size[znum] = zholes_size[znum] = 0;
1493 npages = end_pfn - (phys_base >> PAGE_SHIFT);
1494 zones_size[ZONE_DMA] = npages;
1495 zholes_size[ZONE_DMA] = npages - pages_avail;
1497 free_area_init_node(0, NULL, NULL, zones_size,
1498 phys_base, zholes_size);
1504 /* Ok, it seems that the prom can allocate some more memory chunks
1505 * as a side effect of some prom calls we perform during the
1506 * boot sequence. My most likely theory is that it is from the
1507 * prom_set_traptable() call, and OBP is allocating a scratchpad
1508 * for saving client program register state etc.
1510 void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1514 unsigned long tmpaddr, tmpsize;
1515 unsigned long lowest;
1517 for (i = 0; thislist[i].theres_more != 0; i++) {
1518 lowest = thislist[i].start_adr;
1519 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1520 if (thislist[mitr].start_adr < lowest) {
1521 lowest = thislist[mitr].start_adr;
1524 if (lowest == thislist[i].start_adr)
1526 tmpaddr = thislist[swapi].start_adr;
1527 tmpsize = thislist[swapi].num_bytes;
1528 for (mitr = swapi; mitr > i; mitr--) {
1529 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1530 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1532 thislist[i].start_adr = tmpaddr;
1533 thislist[i].num_bytes = tmpsize;
1537 void __init rescan_sp_banks(void)
1539 struct linux_prom64_registers memlist[64];
1540 struct linux_mlist_p1275 avail[64], *mlist;
1541 unsigned long bytes, base_paddr;
1542 int num_regs, node = prom_finddevice("/memory");
1545 num_regs = prom_getproperty(node, "available",
1546 (char *) memlist, sizeof(memlist));
1547 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1548 for (i = 0; i < num_regs; i++) {
1549 avail[i].start_adr = memlist[i].phys_addr;
1550 avail[i].num_bytes = memlist[i].reg_size;
1551 avail[i].theres_more = &avail[i + 1];
1553 avail[i - 1].theres_more = NULL;
1554 sort_memlist(avail);
1558 bytes = mlist->num_bytes;
1559 base_paddr = mlist->start_adr;
1561 sp_banks[0].base_addr = base_paddr;
1562 sp_banks[0].num_bytes = bytes;
1564 while (mlist->theres_more != NULL){
1566 mlist = mlist->theres_more;
1567 bytes = mlist->num_bytes;
1568 if (i >= SPARC_PHYS_BANKS-1) {
1569 printk ("The machine has more banks than "
1570 "this kernel can support\n"
1571 "Increase the SPARC_PHYS_BANKS "
1572 "setting (currently %d)\n",
1574 i = SPARC_PHYS_BANKS-1;
1578 sp_banks[i].base_addr = mlist->start_adr;
1579 sp_banks[i].num_bytes = mlist->num_bytes;
1583 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1584 sp_banks[i].num_bytes = 0;
1586 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1587 sp_banks[i].num_bytes &= PAGE_MASK;
1590 static void __init taint_real_pages(void)
1592 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1595 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1596 saved_sp_banks[i].base_addr =
1597 sp_banks[i].base_addr;
1598 saved_sp_banks[i].num_bytes =
1599 sp_banks[i].num_bytes;
1604 /* Find changes discovered in the sp_bank rescan and
1605 * reserve the lost portions in the bootmem maps.
1607 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1608 unsigned long old_start, old_end;
1610 old_start = saved_sp_banks[i].base_addr;
1611 old_end = old_start +
1612 saved_sp_banks[i].num_bytes;
1613 while (old_start < old_end) {
1616 for (n = 0; sp_banks[n].num_bytes; n++) {
1617 unsigned long new_start, new_end;
1619 new_start = sp_banks[n].base_addr;
1620 new_end = new_start + sp_banks[n].num_bytes;
1622 if (new_start <= old_start &&
1623 new_end >= (old_start + PAGE_SIZE)) {
1624 set_bit (old_start >> 22,
1625 sparc64_valid_addr_bitmap);
1629 reserve_bootmem(old_start, PAGE_SIZE);
1632 old_start += PAGE_SIZE;
1637 void __init mem_init(void)
1639 unsigned long codepages, datapages, initpages;
1640 unsigned long addr, last;
1643 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1645 sparc64_valid_addr_bitmap = (unsigned long *)
1646 __alloc_bootmem(i << 3, SMP_CACHE_BYTES, bootmap_base);
1647 if (sparc64_valid_addr_bitmap == NULL) {
1648 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1651 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1653 addr = PAGE_OFFSET + kern_base;
1654 last = PAGE_ALIGN(kern_size) + addr;
1655 while (addr < last) {
1656 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1662 max_mapnr = last_valid_pfn - (phys_base >> PAGE_SHIFT);
1663 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1665 #ifdef CONFIG_DEBUG_BOOTMEM
1666 prom_printf("mem_init: Calling free_all_bootmem().\n");
1668 num_physpages = free_all_bootmem() - 1;
1671 * Set up the zero page, mark it reserved, so that page count
1672 * is not manipulated when freeing the page from user ptes.
1674 mem_map_zero = _alloc_pages(GFP_KERNEL, 0);
1675 if (mem_map_zero == NULL) {
1676 prom_printf("paging_init: Cannot alloc zero page.\n");
1679 SetPageReserved(mem_map_zero);
1680 clear_page(page_address(mem_map_zero));
1682 codepages = (((unsigned long) &etext) - ((unsigned long)&_start));
1683 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1684 datapages = (((unsigned long) &edata) - ((unsigned long)&etext));
1685 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1686 initpages = (((unsigned long) &__init_end) - ((unsigned long) &__init_begin));
1687 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1691 /* Put empty_pg_dir on pgd_quicklist */
1692 extern pgd_t empty_pg_dir[1024];
1693 unsigned long addr = (unsigned long)empty_pg_dir;
1694 unsigned long alias_base = kern_base + PAGE_OFFSET -
1697 memset(empty_pg_dir, 0, sizeof(empty_pg_dir));
1699 free_pgd_fast((pgd_t *)addr);
1704 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1705 nr_free_pages() << (PAGE_SHIFT-10),
1706 codepages << (PAGE_SHIFT-10),
1707 datapages << (PAGE_SHIFT-10),
1708 initpages << (PAGE_SHIFT-10),
1709 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1711 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1712 cheetah_ecache_flush_init();
1715 void free_initmem (void)
1717 unsigned long addr, initend;
1720 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1722 addr = PAGE_ALIGN((unsigned long)(&__init_begin));
1723 initend = (unsigned long)(&__init_end) & PAGE_MASK;
1724 for (; addr < initend; addr += PAGE_SIZE) {
1729 ((unsigned long) __va(kern_base)) -
1730 ((unsigned long) KERNBASE));
1731 p = virt_to_page(page);
1733 ClearPageReserved(p);
1734 set_page_count(p, 1);
1740 #ifdef CONFIG_BLK_DEV_INITRD
1741 void free_initrd_mem(unsigned long start, unsigned long end)
1744 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1745 for (; start < end; start += PAGE_SIZE) {
1746 struct page *p = virt_to_page(start);
1748 ClearPageReserved(p);
1749 set_page_count(p, 1);
1756 void si_meminfo(struct sysinfo *val)
1758 val->totalram = num_physpages;
1760 val->freeram = nr_free_pages();
1761 val->bufferram = atomic_read(&buffermem_pages);
1763 /* These are always zero on Sparc64. */
1767 val->mem_unit = PAGE_SIZE;