1 /* $Id: init.c,v 1.164 2001/03/03 10:34:45 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>
21 #include <asm/system.h>
23 #include <asm/pgalloc.h>
24 #include <asm/pgtable.h>
25 #include <asm/oplib.h>
26 #include <asm/iommu.h>
28 #include <asm/uaccess.h>
29 #include <asm/mmu_context.h>
30 #include <asm/vaddrs.h>
32 #include <asm/starfire.h>
34 extern void device_scan(void);
36 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
38 unsigned long *sparc64_valid_addr_bitmap;
40 /* Ugly, but necessary... -DaveM */
41 unsigned long phys_base;
43 enum ultra_tlb_layout tlb_type = spitfire;
45 /* get_new_mmu_context() uses "cache + 1". */
46 spinlock_t ctx_alloc_lock = SPIN_LOCK_UNLOCKED;
47 unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
48 #define CTX_BMAP_SLOTS (1UL << (CTX_VERSION_SHIFT - 6))
49 unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
51 /* References to section boundaries */
52 extern char __init_begin, __init_end, _start, _end, etext, edata;
54 /* Initial ramdisk setup */
55 extern unsigned int sparc_ramdisk_image;
56 extern unsigned int sparc_ramdisk_size;
58 int do_check_pgt_cache(int low, int high)
62 if (pgtable_cache_size > high) {
66 free_pgd_slow(get_pgd_fast()), freed++;
69 free_pte_slow(get_pte_fast(0)), freed++;
71 free_pte_slow(get_pte_fast(1)), freed++;
72 } while (pgtable_cache_size > low);
75 if (pgd_cache_size > high / 4) {
76 struct page *page, *page2;
77 for (page2 = NULL, page = (struct page *)pgd_quicklist; page;) {
78 if ((unsigned long)page->pprev_hash == 3) {
80 page2->next_hash = page->next_hash;
82 (struct page *)pgd_quicklist = page->next_hash;
83 page->next_hash = NULL;
84 page->pprev_hash = NULL;
89 page = page2->next_hash;
91 page = (struct page *)pgd_quicklist;
92 if (pgd_cache_size <= low / 4)
97 page = page->next_hash;
104 extern void __update_mmu_cache(struct vm_area_struct *, unsigned long, pte_t);
106 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
108 struct page *page = pte_page(pte);
110 if (VALID_PAGE(page) && page->mapping &&
111 test_bit(PG_dcache_dirty, &page->flags)) {
112 __flush_dcache_page(page->virtual, 1);
113 clear_bit(PG_dcache_dirty, &page->flags);
115 __update_mmu_cache(vma, address, pte);
118 /* In arch/sparc64/mm/ultra.S */
119 extern void __flush_icache_page(unsigned long);
121 void flush_icache_range(unsigned long start, unsigned long end)
125 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
126 __flush_icache_page(__get_phys(kaddr));
130 * BAD_PAGE is the page that is used for page faults when linux
131 * is out-of-memory. Older versions of linux just did a
132 * do_exit(), but using this instead means there is less risk
133 * for a process dying in kernel mode, possibly leaving an inode
136 * BAD_PAGETABLE is the accompanying page-table: it is initialized
137 * to point to BAD_PAGE entries.
139 * ZERO_PAGE is a special page that is used for zero-initialized
142 pte_t __bad_page(void)
144 memset((void *) &empty_bad_page, 0, PAGE_SIZE);
145 return pte_mkdirty(mk_pte_phys((((unsigned long) &empty_bad_page)
146 - ((unsigned long)&empty_zero_page)
153 printk("Mem-info:\n");
155 printk("Free swap: %6dkB\n",
156 nr_swap_pages << (PAGE_SHIFT-10));
157 printk("%ld pages of RAM\n", num_physpages);
158 printk("%d free pages\n", nr_free_pages());
159 printk("%d pages in page table cache\n",pgtable_cache_size);
161 printk("%d entries in page dir cache\n",pgd_cache_size);
166 int mmu_info(char *buf)
168 if (tlb_type == cheetah)
169 return sprintf(buf, "MMU Type\t: Cheetah\n");
170 else if (tlb_type == spitfire)
171 return sprintf(buf, "MMU Type\t: Spitfire\n");
173 return sprintf(buf, "MMU Type\t: ???\n");
176 struct linux_prom_translation {
182 extern unsigned long prom_boot_page;
183 extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
184 extern int prom_get_mmu_ihandle(void);
185 extern void register_prom_callbacks(void);
187 /* Exported for SMP bootup purposes. */
188 unsigned long kern_locked_tte_data;
190 void __init early_pgtable_allocfail(char *type)
192 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
196 static void inherit_prom_mappings(void)
198 struct linux_prom_translation *trans;
199 unsigned long phys_page, tte_vaddr, tte_data;
200 void (*remap_func)(unsigned long, unsigned long, int);
206 node = prom_finddevice("/virtual-memory");
207 n = prom_getproplen(node, "translations");
208 if (n == 0 || n == -1) {
209 prom_printf("Couldn't get translation property\n");
212 n += 5 * sizeof(struct linux_prom_translation);
213 for (tsz = 1; tsz < n; tsz <<= 1)
215 trans = __alloc_bootmem(tsz, SMP_CACHE_BYTES, 0UL);
217 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
220 memset(trans, 0, tsz);
222 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
223 prom_printf("Couldn't get translation property\n");
226 n = n / sizeof(*trans);
228 for (i = 0; i < n; i++) {
231 if (trans[i].virt >= 0xf0000000 && trans[i].virt < 0x100000000) {
232 for (vaddr = trans[i].virt;
233 vaddr < trans[i].virt + trans[i].size;
234 vaddr += PAGE_SIZE) {
237 pgdp = pgd_offset(&init_mm, vaddr);
238 if (pgd_none(*pgdp)) {
239 pmdp = __alloc_bootmem(PMD_TABLE_SIZE,
243 early_pgtable_allocfail("pmd");
244 memset(pmdp, 0, PMD_TABLE_SIZE);
247 pmdp = pmd_offset(pgdp, vaddr);
248 if (pmd_none(*pmdp)) {
249 ptep = __alloc_bootmem(PTE_TABLE_SIZE,
253 early_pgtable_allocfail("pte");
254 memset(ptep, 0, PTE_TABLE_SIZE);
257 ptep = pte_offset(pmdp, vaddr);
261 /* Clear diag TTE bits. */
262 if (tlb_type == spitfire)
263 val &= ~0x0003fe0000000000UL;
265 set_pte (ptep, __pte(val | _PAGE_MODIFIED));
266 trans[i].data += PAGE_SIZE;
271 /* Now fixup OBP's idea about where we really are mapped. */
272 prom_printf("Remapping the kernel... ");
274 /* Spitfire Errata #32 workaround */
275 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
279 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
284 phys_page = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
288 phys_page = cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
292 phys_page &= _PAGE_PADDR;
293 phys_page += ((unsigned long)&prom_boot_page -
294 (unsigned long)&empty_zero_page);
296 if (tlb_type == spitfire) {
297 /* Lock this into i/d tlb entry 59 */
298 __asm__ __volatile__(
299 "stxa %%g0, [%2] %3\n\t"
300 "stxa %0, [%1] %4\n\t"
303 "stxa %%g0, [%2] %5\n\t"
304 "stxa %0, [%1] %6\n\t"
307 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
308 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
309 "r" (59 << 3), "r" (TLB_TAG_ACCESS),
310 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
311 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
313 } else if (tlb_type == cheetah) {
314 /* Lock this into i/d tlb-0 entry 11 */
315 __asm__ __volatile__(
316 "stxa %%g0, [%2] %3\n\t"
317 "stxa %0, [%1] %4\n\t"
320 "stxa %%g0, [%2] %5\n\t"
321 "stxa %0, [%1] %6\n\t"
324 : : "r" (phys_page | _PAGE_VALID | _PAGE_SZ8K | _PAGE_CP |
325 _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W),
326 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS),
327 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
328 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
331 /* Implement me :-) */
335 tte_vaddr = (unsigned long) &empty_zero_page;
337 /* Spitfire Errata #32 workaround */
338 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
342 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
344 if (tlb_type == spitfire)
345 tte_data = spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
347 tte_data = cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
349 kern_locked_tte_data = tte_data;
351 remap_func = (void *) ((unsigned long) &prom_remap -
352 (unsigned long) &prom_boot_page);
355 /* Spitfire Errata #32 workaround */
356 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
360 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
362 remap_func((tlb_type == spitfire ?
363 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR) :
364 (cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR)),
365 (unsigned long) &empty_zero_page,
366 prom_get_mmu_ihandle());
368 /* Flush out that temporary mapping. */
369 spitfire_flush_dtlb_nucleus_page(0x0);
370 spitfire_flush_itlb_nucleus_page(0x0);
372 /* Now lock us back into the TLBs via OBP. */
373 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
374 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data, tte_vaddr);
376 /* Re-read translations property. */
377 if ((n = prom_getproperty(node, "translations", (char *)trans, tsz)) == -1) {
378 prom_printf("Couldn't get translation property\n");
381 n = n / sizeof(*trans);
383 for (i = 0; i < n; i++) {
384 unsigned long vaddr = trans[i].virt;
385 unsigned long size = trans[i].size;
387 if (vaddr < 0xf0000000UL) {
388 unsigned long avoid_start = (unsigned long) &empty_zero_page;
389 unsigned long avoid_end = avoid_start + (4 * 1024 * 1024);
391 if (vaddr < avoid_start) {
392 unsigned long top = vaddr + size;
394 if (top > avoid_start)
396 prom_unmap(top - vaddr, vaddr);
398 if ((vaddr + size) > avoid_end) {
399 unsigned long bottom = vaddr;
401 if (bottom < avoid_end)
403 prom_unmap((vaddr + size) - bottom, bottom);
408 prom_printf("done.\n");
410 register_prom_callbacks();
413 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
414 * upwards as reserved for use by the firmware (I wonder if this
415 * will be the same on Cheetah...). We use this virtual address
416 * range for the VPTE table mappings of the nucleus so we need
417 * to zap them when we enter the PROM. -DaveM
419 static void __flush_nucleus_vptes(void)
421 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
424 /* Only DTLB must be checked for VPTE entries. */
425 if (tlb_type == spitfire) {
426 for (i = 0; i < 63; i++) {
429 /* Spitfire Errata #32 workaround */
430 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
434 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
436 tag = spitfire_get_dtlb_tag(i);
437 if (((tag & ~(PAGE_MASK)) == 0) &&
438 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
439 __asm__ __volatile__("stxa %%g0, [%0] %1"
441 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
443 spitfire_put_dtlb_data(i, 0x0UL);
447 } else if (tlb_type == cheetah) {
448 for (i = 0; i < 511; i++) {
449 unsigned long tag = cheetah_get_dtlb_tag(i);
451 if ((tag & ~PAGE_MASK) == 0 &&
452 (tag & PAGE_MASK) >= prom_reserved_base) {
453 __asm__ __volatile__("stxa %%g0, [%0] %1"
455 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
457 cheetah_put_dtlb_data(i, 0x0UL);
462 /* Implement me :-) */
467 static int prom_ditlb_set = 0;
468 struct prom_tlb_entry {
470 unsigned long tlb_tag;
471 unsigned long tlb_data;
473 struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
475 void prom_world(int enter)
477 unsigned long pstate;
481 set_fs(current->thread.current_ds);
486 /* Make sure the following runs atomically. */
487 __asm__ __volatile__("flushw\n\t"
488 "rdpr %%pstate, %0\n\t"
489 "wrpr %0, %1, %%pstate"
494 /* Kick out nucleus VPTEs. */
495 __flush_nucleus_vptes();
497 /* Install PROM world. */
498 for (i = 0; i < 16; i++) {
499 if (prom_dtlb[i].tlb_ent != -1) {
500 __asm__ __volatile__("stxa %0, [%1] %2"
501 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
504 if (tlb_type == spitfire)
505 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
506 prom_dtlb[i].tlb_data);
507 else if (tlb_type == cheetah)
508 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
509 prom_dtlb[i].tlb_data);
512 if (prom_itlb[i].tlb_ent != -1) {
513 __asm__ __volatile__("stxa %0, [%1] %2"
514 : : "r" (prom_itlb[i].tlb_tag),
515 "r" (TLB_TAG_ACCESS),
518 if (tlb_type == spitfire)
519 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
520 prom_itlb[i].tlb_data);
521 else if (tlb_type == cheetah)
522 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
523 prom_itlb[i].tlb_data);
528 for (i = 0; i < 16; i++) {
529 if (prom_dtlb[i].tlb_ent != -1) {
530 __asm__ __volatile__("stxa %%g0, [%0] %1"
531 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
533 if (tlb_type == spitfire)
534 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
536 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
539 if (prom_itlb[i].tlb_ent != -1) {
540 __asm__ __volatile__("stxa %%g0, [%0] %1"
541 : : "r" (TLB_TAG_ACCESS),
544 if (tlb_type == spitfire)
545 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
547 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
552 __asm__ __volatile__("wrpr %0, 0, %%pstate"
556 void inherit_locked_prom_mappings(int save_p)
562 /* Fucking losing PROM has more mappings in the TLB, but
563 * it (conveniently) fails to mention any of these in the
564 * translations property. The only ones that matter are
565 * the locked PROM tlb entries, so we impose the following
566 * irrecovable rule on the PROM, it is allowed 8 locked
567 * entries in the ITLB and 8 in the DTLB.
569 * Supposedly the upper 16GB of the address space is
570 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
571 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
572 * used between the client program and the firmware on sun5
573 * systems to coordinate mmu mappings is also COMPLETELY
574 * UNDOCUMENTED!!!!!! Thanks S(t)un!
577 for (i = 0; i < 16; i++) {
578 prom_itlb[i].tlb_ent = -1;
579 prom_dtlb[i].tlb_ent = -1;
582 if (tlb_type == spitfire) {
583 for (i = 0; i < SPITFIRE_HIGHEST_LOCKED_TLBENT; i++) {
586 /* Spitfire Errata #32 workaround */
587 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
591 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
593 data = spitfire_get_dtlb_data(i);
594 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
597 /* Spitfire Errata #32 workaround */
598 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
602 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
604 tag = spitfire_get_dtlb_tag(i);
606 prom_dtlb[dtlb_seen].tlb_ent = i;
607 prom_dtlb[dtlb_seen].tlb_tag = tag;
608 prom_dtlb[dtlb_seen].tlb_data = data;
610 __asm__ __volatile__("stxa %%g0, [%0] %1"
611 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
613 spitfire_put_dtlb_data(i, 0x0UL);
622 for (i = 0; i < SPITFIRE_HIGHEST_LOCKED_TLBENT; i++) {
625 /* Spitfire Errata #32 workaround */
626 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
630 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
632 data = spitfire_get_itlb_data(i);
633 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
636 /* Spitfire Errata #32 workaround */
637 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
641 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
643 tag = spitfire_get_itlb_tag(i);
645 prom_itlb[itlb_seen].tlb_ent = i;
646 prom_itlb[itlb_seen].tlb_tag = tag;
647 prom_itlb[itlb_seen].tlb_data = data;
649 __asm__ __volatile__("stxa %%g0, [%0] %1"
650 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
652 spitfire_put_itlb_data(i, 0x0UL);
660 } else if (tlb_type == cheetah) {
661 for (i = 0; i < CHEETAH_HIGHEST_LOCKED_TLBENT; i++) {
664 data = cheetah_get_ldtlb_data(i);
665 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
668 tag = cheetah_get_ldtlb_tag(i);
670 prom_dtlb[dtlb_seen].tlb_ent = i;
671 prom_dtlb[dtlb_seen].tlb_tag = tag;
672 prom_dtlb[dtlb_seen].tlb_data = data;
674 __asm__ __volatile__("stxa %%g0, [%0] %1"
675 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
677 cheetah_put_ldtlb_data(i, 0x0UL);
686 for (i = 0; i < CHEETAH_HIGHEST_LOCKED_TLBENT; i++) {
689 data = cheetah_get_litlb_data(i);
690 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
693 tag = cheetah_get_litlb_tag(i);
695 prom_itlb[itlb_seen].tlb_ent = i;
696 prom_itlb[itlb_seen].tlb_tag = tag;
697 prom_itlb[itlb_seen].tlb_data = data;
699 __asm__ __volatile__("stxa %%g0, [%0] %1"
700 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
702 cheetah_put_litlb_data(i, 0x0UL);
711 /* Implement me :-) */
718 /* Give PROM back his world, done during reboots... */
719 void prom_reload_locked(void)
723 for (i = 0; i < 16; i++) {
724 if (prom_dtlb[i].tlb_ent != -1) {
725 __asm__ __volatile__("stxa %0, [%1] %2"
726 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
729 if (tlb_type == spitfire)
730 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
731 prom_dtlb[i].tlb_data);
732 else if (tlb_type == cheetah)
733 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
734 prom_dtlb[i].tlb_data);
738 if (prom_itlb[i].tlb_ent != -1) {
739 __asm__ __volatile__("stxa %0, [%1] %2"
740 : : "r" (prom_itlb[i].tlb_tag),
741 "r" (TLB_TAG_ACCESS),
744 if (tlb_type == spitfire)
745 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
746 prom_itlb[i].tlb_data);
748 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
749 prom_itlb[i].tlb_data);
755 void __flush_dcache_range(unsigned long start, unsigned long end)
760 for (va = start; va < end; va += 32) {
761 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
767 void __flush_cache_all(void)
772 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
773 spitfire_put_icache_tag(va, 0x0);
776 /* If not locked, zap it. */
777 void __flush_tlb_all(void)
779 unsigned long pstate;
782 __asm__ __volatile__("flushw\n\t"
783 "rdpr %%pstate, %0\n\t"
784 "wrpr %0, %1, %%pstate"
787 if (tlb_type == spitfire) {
788 for (i = 0; i < 64; i++) {
789 /* Spitfire Errata #32 workaround */
790 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
794 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
796 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
797 __asm__ __volatile__("stxa %%g0, [%0] %1"
799 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
801 spitfire_put_dtlb_data(i, 0x0UL);
805 /* Spitfire Errata #32 workaround */
806 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
810 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
812 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
813 __asm__ __volatile__("stxa %%g0, [%0] %1"
815 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
817 spitfire_put_itlb_data(i, 0x0UL);
821 } else if (tlb_type == cheetah) {
822 cheetah_flush_dtlb_all();
823 cheetah_flush_itlb_all();
825 __asm__ __volatile__("wrpr %0, 0, %%pstate"
829 /* Caller does TLB context flushing on local CPU if necessary.
831 * We must be careful about boundary cases so that we never
832 * let the user have CTX 0 (nucleus) or we ever use a CTX
833 * version of zero (and thus NO_CONTEXT would not be caught
834 * by version mis-match tests in mmu_context.h).
836 void get_new_mmu_context(struct mm_struct *mm)
838 unsigned long ctx, new_ctx;
840 spin_lock(&ctx_alloc_lock);
841 ctx = CTX_HWBITS(tlb_context_cache + 1);
844 if (CTX_VALID(mm->context)) {
845 unsigned long nr = CTX_HWBITS(mm->context);
846 mmu_context_bmap[nr>>6] &= ~(1UL << (nr & 63));
848 new_ctx = find_next_zero_bit(mmu_context_bmap, 1UL << CTX_VERSION_SHIFT, ctx);
849 if (new_ctx >= (1UL << CTX_VERSION_SHIFT)) {
850 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
851 if (new_ctx >= ctx) {
853 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
856 new_ctx = CTX_FIRST_VERSION;
858 /* Don't call memset, for 16 entries that's just
861 mmu_context_bmap[0] = 3;
862 mmu_context_bmap[1] = 0;
863 mmu_context_bmap[2] = 0;
864 mmu_context_bmap[3] = 0;
865 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
866 mmu_context_bmap[i + 0] = 0;
867 mmu_context_bmap[i + 1] = 0;
868 mmu_context_bmap[i + 2] = 0;
869 mmu_context_bmap[i + 3] = 0;
874 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
875 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
877 tlb_context_cache = new_ctx;
878 spin_unlock(&ctx_alloc_lock);
880 mm->context = new_ctx;
884 struct pgtable_cache_struct pgt_quicklists;
887 /* For PMDs we don't care about the color, writes are
888 * only done via Dcache which is write-thru, so non-Dcache
889 * reads will always see correct data.
891 pmd_t *get_pmd_slow(pgd_t *pgd, unsigned long offset)
895 pmd = (pmd_t *) __get_free_page(GFP_KERNEL);
897 memset(pmd, 0, PAGE_SIZE);
904 /* OK, we have to color these pages because during DTLB
905 * protection faults we set the dirty bit via a non-Dcache
906 * enabled mapping in the VPTE area. The kernel can end
907 * up missing the dirty bit resulting in processes crashing
908 * _iff_ the VPTE mapping of the ptes have a virtual address
909 * bit 13 which is different from bit 13 of the physical address.
912 * 1) DTLB protection fault, write dirty bit into pte via VPTE
914 * 2) Swapper checks pte, does not see dirty bit, frees page.
915 * 3) Process faults back in the page, the old pre-dirtied copy
916 * is provided and here is the corruption.
918 pte_t *get_pte_slow(pmd_t *pmd, unsigned long offset, unsigned long color)
920 struct page *page = alloc_pages(GFP_KERNEL, 1);
923 unsigned long *to_free;
927 set_page_count((page + 1), 1);
928 paddr = (unsigned long) page_address(page);
929 memset((char *)paddr, 0, (PAGE_SIZE << 1));
932 pte = (pte_t *) paddr;
933 to_free = (unsigned long *) (paddr + PAGE_SIZE);
935 pte = (pte_t *) (paddr + PAGE_SIZE);
936 to_free = (unsigned long *) paddr;
939 /* Now free the other one up, adjust cache size. */
940 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
941 pte_quicklist[color ^ 0x1] = to_free;
942 pgtable_cache_size++;
950 void sparc_ultra_dump_itlb(void)
954 if (tlb_type == spitfire) {
955 printk ("Contents of itlb: ");
956 for (slot = 0; slot < 14; slot++) printk (" ");
957 printk ("%2x:%016lx,%016lx\n",
959 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
960 for (slot = 1; slot < 64; slot+=3) {
961 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
963 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
965 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
967 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
969 } else if (tlb_type == cheetah) {
970 printk ("Contents of itlb0:\n");
971 for (slot = 0; slot < 16; slot+=2) {
972 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
974 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
976 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
978 printk ("Contents of itlb2:\n");
979 for (slot = 0; slot < 128; slot+=2) {
980 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
982 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
984 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
989 void sparc_ultra_dump_dtlb(void)
993 if (tlb_type == spitfire) {
994 printk ("Contents of dtlb: ");
995 for (slot = 0; slot < 14; slot++) printk (" ");
996 printk ("%2x:%016lx,%016lx\n", 0,
997 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
998 for (slot = 1; slot < 64; slot+=3) {
999 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1001 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1003 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1005 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1007 } else if (tlb_type == cheetah) {
1008 printk ("Contents of dtlb0:\n");
1009 for (slot = 0; slot < 16; slot+=2) {
1010 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1012 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1014 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1016 printk ("Contents of dtlb2:\n");
1017 for (slot = 0; slot < 512; slot+=2) {
1018 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1020 cheetah_get_dtlb_tag(slot), cheetah_get_dtlb_data(slot),
1022 cheetah_get_dtlb_tag(slot+1), cheetah_get_dtlb_data(slot+1));
1027 extern unsigned long cmdline_memory_size;
1029 unsigned long __init bootmem_init(unsigned long *pages_avail)
1031 unsigned long bootmap_size, start_pfn, end_pfn;
1032 unsigned long end_of_phys_memory = 0UL;
1033 unsigned long bootmap_pfn, bytes_avail, size;
1037 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1038 end_of_phys_memory = sp_banks[i].base_addr +
1039 sp_banks[i].num_bytes;
1040 bytes_avail += sp_banks[i].num_bytes;
1041 if (cmdline_memory_size) {
1042 if (bytes_avail > cmdline_memory_size) {
1043 unsigned long slack = bytes_avail - cmdline_memory_size;
1045 bytes_avail -= slack;
1046 end_of_phys_memory -= slack;
1048 sp_banks[i].num_bytes -= slack;
1049 if (sp_banks[i].num_bytes == 0) {
1050 sp_banks[i].base_addr = 0xdeadbeef;
1052 sp_banks[i+1].num_bytes = 0;
1053 sp_banks[i+1].base_addr = 0xdeadbeef;
1060 *pages_avail = bytes_avail >> PAGE_SHIFT;
1062 /* Start with page aligned address of last symbol in kernel
1063 * image. The kernel is hard mapped below PAGE_OFFSET in a
1064 * 4MB locked TLB translation.
1066 start_pfn = PAGE_ALIGN((unsigned long) &_end) -
1067 ((unsigned long) &empty_zero_page);
1069 /* Adjust up to the physical address where the kernel begins. */
1070 start_pfn += phys_base;
1072 /* Now shift down to get the real physical page frame number. */
1073 start_pfn >>= PAGE_SHIFT;
1075 bootmap_pfn = start_pfn;
1077 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1079 #ifdef CONFIG_BLK_DEV_INITRD
1080 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1081 if (sparc_ramdisk_image) {
1082 if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
1083 sparc_ramdisk_image -= KERNBASE;
1084 initrd_start = sparc_ramdisk_image + phys_base;
1085 initrd_end = initrd_start + sparc_ramdisk_size;
1086 if (initrd_end > end_of_phys_memory) {
1087 printk(KERN_CRIT "initrd extends beyond end of memory "
1088 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1089 initrd_end, end_of_phys_memory);
1093 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1094 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1095 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1099 /* Initialize the boot-time allocator. */
1100 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, phys_base>>PAGE_SHIFT, end_pfn);
1102 /* Now register the available physical memory with the
1105 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1106 free_bootmem(sp_banks[i].base_addr,
1107 sp_banks[i].num_bytes);
1109 #ifdef CONFIG_BLK_DEV_INITRD
1111 size = initrd_end - initrd_start;
1113 /* Resert the initrd image area. */
1114 reserve_bootmem(initrd_start, size);
1115 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1117 initrd_start += PAGE_OFFSET;
1118 initrd_end += PAGE_OFFSET;
1121 /* Reserve the kernel text/data/bss. */
1122 size = (start_pfn << PAGE_SHIFT) - phys_base;
1123 reserve_bootmem(phys_base, size);
1124 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1126 /* Reserve the bootmem map. We do not account for it
1127 * in pages_avail because we will release that memory
1128 * in free_all_bootmem.
1130 size = bootmap_size;
1131 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1132 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1137 /* paging_init() sets up the page tables */
1139 extern void sun_serial_setup(void);
1141 static unsigned long last_valid_pfn;
1143 void __init paging_init(void)
1145 extern pmd_t swapper_pmd_dir[1024];
1146 extern unsigned int sparc64_vpte_patchme1[1];
1147 extern unsigned int sparc64_vpte_patchme2[1];
1148 unsigned long alias_base = phys_base + PAGE_OFFSET;
1149 unsigned long second_alias_page = 0;
1150 unsigned long pt, flags, end_pfn, pages_avail;
1151 unsigned long shift = alias_base - ((unsigned long)&empty_zero_page);
1153 set_bit(0, mmu_context_bmap);
1154 /* We assume physical memory starts at some 4mb multiple,
1155 * if this were not true we wouldn't boot up to this point
1158 pt = phys_base | _PAGE_VALID | _PAGE_SZ4MB;
1159 pt |= _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W;
1160 __save_and_cli(flags);
1161 if (tlb_type == spitfire) {
1162 __asm__ __volatile__("
1171 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1172 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (61 << 3)
1174 if (((unsigned long)&_end) >= KERNBASE + 0x340000) {
1175 second_alias_page = alias_base + 0x400000;
1176 __asm__ __volatile__("
1185 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1186 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" (60 << 3)
1189 } else if (tlb_type == cheetah) {
1190 __asm__ __volatile__("
1199 : "r" (TLB_TAG_ACCESS), "r" (alias_base), "r" (pt),
1200 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (13<<3))
1202 if (((unsigned long)&_end) >= KERNBASE + 0x340000) {
1203 second_alias_page = alias_base + 0x400000;
1204 __asm__ __volatile__("
1213 : "r" (TLB_TAG_ACCESS), "r" (second_alias_page), "r" (pt + 0x400000),
1214 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS), "r" ((0<<16) | (12<<3))
1218 __restore_flags(flags);
1220 /* Now set kernel pgd to upper alias so physical page computations
1223 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1225 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1227 /* Now can init the kernel/bad page tables. */
1228 pgd_set(&swapper_pg_dir[0], swapper_pmd_dir + (shift / sizeof(pgd_t)));
1230 sparc64_vpte_patchme1[0] |= (pgd_val(init_mm.pgd[0]) >> 10);
1231 sparc64_vpte_patchme2[0] |= (pgd_val(init_mm.pgd[0]) & 0x3ff);
1232 flushi((long)&sparc64_vpte_patchme1[0]);
1234 /* Setup bootmem... */
1236 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1238 #ifdef CONFIG_SUN_SERIAL
1239 /* This does not logically belong here, but we need to
1240 * call it at the moment we are able to use the bootmem
1246 /* Inherit non-locked OBP mappings. */
1247 inherit_prom_mappings();
1249 /* Ok, we can use our TLB miss and window trap handlers safely.
1250 * We need to do a quick peek here to see if we are on StarFire
1251 * or not, so setup_tba can setup the IRQ globals correctly (it
1252 * needs to get the hard smp processor id correctly).
1255 extern void setup_tba(int);
1256 setup_tba(this_is_starfire);
1259 inherit_locked_prom_mappings(1);
1261 /* We only created DTLB mapping of this stuff. */
1262 spitfire_flush_dtlb_nucleus_page(alias_base);
1263 if (second_alias_page)
1264 spitfire_flush_dtlb_nucleus_page(second_alias_page);
1269 unsigned long zones_size[MAX_NR_ZONES];
1270 unsigned long zholes_size[MAX_NR_ZONES];
1271 unsigned long npages;
1274 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1275 zones_size[znum] = zholes_size[znum] = 0;
1277 npages = end_pfn - (phys_base >> PAGE_SHIFT);
1278 zones_size[ZONE_DMA] = npages;
1279 zholes_size[ZONE_DMA] = npages - pages_avail;
1281 free_area_init_node(0, NULL, NULL, zones_size,
1282 phys_base, zholes_size);
1288 /* Ok, it seems that the prom can allocate some more memory chunks
1289 * as a side effect of some prom calls we perform during the
1290 * boot sequence. My most likely theory is that it is from the
1291 * prom_set_traptable() call, and OBP is allocating a scratchpad
1292 * for saving client program register state etc.
1294 void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1298 unsigned long tmpaddr, tmpsize;
1299 unsigned long lowest;
1301 for (i = 0; thislist[i].theres_more != 0; i++) {
1302 lowest = thislist[i].start_adr;
1303 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1304 if (thislist[mitr].start_adr < lowest) {
1305 lowest = thislist[mitr].start_adr;
1308 if (lowest == thislist[i].start_adr)
1310 tmpaddr = thislist[swapi].start_adr;
1311 tmpsize = thislist[swapi].num_bytes;
1312 for (mitr = swapi; mitr > i; mitr--) {
1313 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1314 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1316 thislist[i].start_adr = tmpaddr;
1317 thislist[i].num_bytes = tmpsize;
1321 void __init rescan_sp_banks(void)
1323 struct linux_prom64_registers memlist[64];
1324 struct linux_mlist_p1275 avail[64], *mlist;
1325 unsigned long bytes, base_paddr;
1326 int num_regs, node = prom_finddevice("/memory");
1329 num_regs = prom_getproperty(node, "available",
1330 (char *) memlist, sizeof(memlist));
1331 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1332 for (i = 0; i < num_regs; i++) {
1333 avail[i].start_adr = memlist[i].phys_addr;
1334 avail[i].num_bytes = memlist[i].reg_size;
1335 avail[i].theres_more = &avail[i + 1];
1337 avail[i - 1].theres_more = NULL;
1338 sort_memlist(avail);
1342 bytes = mlist->num_bytes;
1343 base_paddr = mlist->start_adr;
1345 sp_banks[0].base_addr = base_paddr;
1346 sp_banks[0].num_bytes = bytes;
1348 while (mlist->theres_more != NULL){
1350 mlist = mlist->theres_more;
1351 bytes = mlist->num_bytes;
1352 if (i >= SPARC_PHYS_BANKS-1) {
1353 printk ("The machine has more banks than "
1354 "this kernel can support\n"
1355 "Increase the SPARC_PHYS_BANKS "
1356 "setting (currently %d)\n",
1358 i = SPARC_PHYS_BANKS-1;
1362 sp_banks[i].base_addr = mlist->start_adr;
1363 sp_banks[i].num_bytes = mlist->num_bytes;
1367 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1368 sp_banks[i].num_bytes = 0;
1370 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1371 sp_banks[i].num_bytes &= PAGE_MASK;
1374 static void __init taint_real_pages(void)
1376 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1379 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1380 saved_sp_banks[i].base_addr =
1381 sp_banks[i].base_addr;
1382 saved_sp_banks[i].num_bytes =
1383 sp_banks[i].num_bytes;
1388 /* Find changes discovered in the sp_bank rescan and
1389 * reserve the lost portions in the bootmem maps.
1391 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1392 unsigned long old_start, old_end;
1394 old_start = saved_sp_banks[i].base_addr;
1395 old_end = old_start +
1396 saved_sp_banks[i].num_bytes;
1397 while (old_start < old_end) {
1400 for (n = 0; sp_banks[n].num_bytes; n++) {
1401 unsigned long new_start, new_end;
1403 new_start = sp_banks[n].base_addr;
1404 new_end = new_start + sp_banks[n].num_bytes;
1406 if (new_start <= old_start &&
1407 new_end >= (old_start + PAGE_SIZE)) {
1408 set_bit (old_start >> 22,
1409 sparc64_valid_addr_bitmap);
1413 reserve_bootmem(old_start, PAGE_SIZE);
1416 old_start += PAGE_SIZE;
1421 void __init mem_init(void)
1423 unsigned long codepages, datapages, initpages;
1424 unsigned long addr, last;
1427 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1429 sparc64_valid_addr_bitmap = (unsigned long *)
1430 __alloc_bootmem(i << 3, SMP_CACHE_BYTES, 0UL);
1431 if (sparc64_valid_addr_bitmap == NULL) {
1432 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1435 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1437 addr = PAGE_OFFSET + phys_base;
1438 last = PAGE_ALIGN((unsigned long)&_end) -
1439 ((unsigned long) &empty_zero_page);
1440 last += PAGE_OFFSET + phys_base;
1441 while (addr < last) {
1442 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1448 max_mapnr = last_valid_pfn - (phys_base >> PAGE_SHIFT);
1449 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1451 num_physpages = free_all_bootmem();
1452 codepages = (((unsigned long) &etext) - ((unsigned long)&_start));
1453 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1454 datapages = (((unsigned long) &edata) - ((unsigned long)&etext));
1455 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1456 initpages = (((unsigned long) &__init_end) - ((unsigned long) &__init_begin));
1457 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1461 /* Put empty_pg_dir on pgd_quicklist */
1462 extern pgd_t empty_pg_dir[1024];
1463 unsigned long addr = (unsigned long)empty_pg_dir;
1464 unsigned long alias_base = phys_base + PAGE_OFFSET -
1465 (long)(&empty_zero_page);
1467 memset(empty_pg_dir, 0, sizeof(empty_pg_dir));
1469 free_pgd_fast((pgd_t *)addr);
1474 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1475 nr_free_pages() << (PAGE_SHIFT-10),
1476 codepages << (PAGE_SHIFT-10),
1477 datapages << (PAGE_SHIFT-10),
1478 initpages << (PAGE_SHIFT-10),
1479 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1482 void free_initmem (void)
1486 addr = (unsigned long)(&__init_begin);
1487 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
1492 ((unsigned long) __va(phys_base)) -
1493 ((unsigned long) &empty_zero_page));
1494 p = virt_to_page(page);
1496 ClearPageReserved(p);
1497 set_page_count(p, 1);
1503 #ifdef CONFIG_BLK_DEV_INITRD
1504 void free_initrd_mem(unsigned long start, unsigned long end)
1507 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1508 for (; start < end; start += PAGE_SIZE) {
1509 struct page *p = virt_to_page(start);
1511 ClearPageReserved(p);
1512 set_page_count(p, 1);
1519 void si_meminfo(struct sysinfo *val)
1521 val->totalram = num_physpages;
1523 val->freeram = nr_free_pages();
1524 val->bufferram = atomic_read(&buffermem_pages);
1526 /* These are always zero on Sparc64. */
1530 val->mem_unit = PAGE_SIZE;