2 * srmmu.c: SRMMU specific routines for memory management.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995,2002 Pete Zaitcev (zaitcev@yahoo.com)
6 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
7 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 * Copyright (C) 1999,2000 Anton Blanchard (anton@samba.org)
11 #include <linux/kernel.h>
13 #include <linux/vmalloc.h>
14 #include <linux/pagemap.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/bootmem.h>
19 #include <linux/seq_file.h>
20 #include <linux/kdebug.h>
21 #include <linux/log2.h>
22 #include <linux/gfp.h>
24 #include <asm/bitext.h>
26 #include <asm/pgalloc.h>
27 #include <asm/pgtable.h>
29 #include <asm/vaddrs.h>
30 #include <asm/traps.h>
33 #include <asm/cache.h>
34 #include <asm/oplib.h>
37 #include <asm/mmu_context.h>
38 #include <asm/io-unit.h>
39 #include <asm/cacheflush.h>
40 #include <asm/tlbflush.h>
42 /* Now the cpu specific definitions. */
43 #include <asm/viking.h>
46 #include <asm/tsunami.h>
47 #include <asm/swift.h>
48 #include <asm/turbosparc.h>
53 enum mbus_module srmmu_modtype;
54 static unsigned int hwbug_bitmask;
58 struct ctx_list *ctx_list_pool;
59 struct ctx_list ctx_free;
60 struct ctx_list ctx_used;
62 extern struct resource sparc_iomap;
64 extern unsigned long last_valid_pfn;
66 static pgd_t *srmmu_swapper_pg_dir;
68 const struct sparc32_cachetlb_ops *sparc32_cachetlb_ops;
71 const struct sparc32_cachetlb_ops *local_ops;
73 #define FLUSH_BEGIN(mm)
76 #define FLUSH_BEGIN(mm) if ((mm)->context != NO_CONTEXT) {
80 int flush_page_for_dma_global = 1;
84 ctxd_t *srmmu_ctx_table_phys;
85 static ctxd_t *srmmu_context_table;
87 int viking_mxcc_present;
88 static DEFINE_SPINLOCK(srmmu_context_spinlock);
90 static int is_hypersparc;
92 static int srmmu_cache_pagetables;
94 /* these will be initialized in srmmu_nocache_calcsize() */
95 static unsigned long srmmu_nocache_size;
96 static unsigned long srmmu_nocache_end;
98 /* 1 bit <=> 256 bytes of nocache <=> 64 PTEs */
99 #define SRMMU_NOCACHE_BITMAP_SHIFT (PAGE_SHIFT - 4)
101 /* The context table is a nocache user with the biggest alignment needs. */
102 #define SRMMU_NOCACHE_ALIGN_MAX (sizeof(ctxd_t)*SRMMU_MAX_CONTEXTS)
104 void *srmmu_nocache_pool;
105 void *srmmu_nocache_bitmap;
106 static struct bit_map srmmu_nocache_map;
108 static inline int srmmu_pte_none(pte_t pte)
109 { return !(pte_val(pte) & 0xFFFFFFF); }
111 static inline int srmmu_pmd_none(pmd_t pmd)
112 { return !(pmd_val(pmd) & 0xFFFFFFF); }
114 static inline pte_t srmmu_pte_wrprotect(pte_t pte)
115 { return __pte(pte_val(pte) & ~SRMMU_WRITE);}
117 static inline pte_t srmmu_pte_mkclean(pte_t pte)
118 { return __pte(pte_val(pte) & ~SRMMU_DIRTY);}
120 static inline pte_t srmmu_pte_mkold(pte_t pte)
121 { return __pte(pte_val(pte) & ~SRMMU_REF);}
123 /* XXX should we hyper_flush_whole_icache here - Anton */
124 static inline void srmmu_ctxd_set(ctxd_t *ctxp, pgd_t *pgdp)
125 { set_pte((pte_t *)ctxp, (SRMMU_ET_PTD | (__nocache_pa((unsigned long) pgdp) >> 4))); }
127 void pmd_set(pmd_t *pmdp, pte_t *ptep)
129 unsigned long ptp; /* Physical address, shifted right by 4 */
132 ptp = __nocache_pa((unsigned long) ptep) >> 4;
133 for (i = 0; i < PTRS_PER_PTE/SRMMU_REAL_PTRS_PER_PTE; i++) {
134 set_pte((pte_t *)&pmdp->pmdv[i], SRMMU_ET_PTD | ptp);
135 ptp += (SRMMU_REAL_PTRS_PER_PTE*sizeof(pte_t) >> 4);
139 void pmd_populate(struct mm_struct *mm, pmd_t *pmdp, struct page *ptep)
141 unsigned long ptp; /* Physical address, shifted right by 4 */
144 ptp = page_to_pfn(ptep) << (PAGE_SHIFT-4); /* watch for overflow */
145 for (i = 0; i < PTRS_PER_PTE/SRMMU_REAL_PTRS_PER_PTE; i++) {
146 set_pte((pte_t *)&pmdp->pmdv[i], SRMMU_ET_PTD | ptp);
147 ptp += (SRMMU_REAL_PTRS_PER_PTE*sizeof(pte_t) >> 4);
151 static inline pte_t srmmu_pte_modify(pte_t pte, pgprot_t newprot)
152 { return __pte((pte_val(pte) & SRMMU_CHG_MASK) | pgprot_val(newprot)); }
154 /* to find an entry in a top-level page table... */
155 static inline pgd_t *srmmu_pgd_offset(struct mm_struct * mm, unsigned long address)
156 { return mm->pgd + (address >> SRMMU_PGDIR_SHIFT); }
158 /* Find an entry in the third-level page table.. */
159 pte_t *pte_offset_kernel(pmd_t * dir, unsigned long address)
163 pte = __nocache_va((dir->pmdv[0] & SRMMU_PTD_PMASK) << 4);
164 return (pte_t *) pte +
165 ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
169 * size: bytes to allocate in the nocache area.
170 * align: bytes, number to align at.
171 * Returns the virtual address of the allocated area.
173 static unsigned long __srmmu_get_nocache(int size, int align)
177 if (size < SRMMU_NOCACHE_BITMAP_SHIFT) {
178 printk("Size 0x%x too small for nocache request\n", size);
179 size = SRMMU_NOCACHE_BITMAP_SHIFT;
181 if (size & (SRMMU_NOCACHE_BITMAP_SHIFT-1)) {
182 printk("Size 0x%x unaligned int nocache request\n", size);
183 size += SRMMU_NOCACHE_BITMAP_SHIFT-1;
185 BUG_ON(align > SRMMU_NOCACHE_ALIGN_MAX);
187 offset = bit_map_string_get(&srmmu_nocache_map,
188 size >> SRMMU_NOCACHE_BITMAP_SHIFT,
189 align >> SRMMU_NOCACHE_BITMAP_SHIFT);
191 printk("srmmu: out of nocache %d: %d/%d\n",
192 size, (int) srmmu_nocache_size,
193 srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT);
197 return (SRMMU_NOCACHE_VADDR + (offset << SRMMU_NOCACHE_BITMAP_SHIFT));
200 unsigned long srmmu_get_nocache(int size, int align)
204 tmp = __srmmu_get_nocache(size, align);
207 memset((void *)tmp, 0, size);
212 void srmmu_free_nocache(unsigned long vaddr, int size)
216 if (vaddr < SRMMU_NOCACHE_VADDR) {
217 printk("Vaddr %lx is smaller than nocache base 0x%lx\n",
218 vaddr, (unsigned long)SRMMU_NOCACHE_VADDR);
221 if (vaddr+size > srmmu_nocache_end) {
222 printk("Vaddr %lx is bigger than nocache end 0x%lx\n",
223 vaddr, srmmu_nocache_end);
226 if (!is_power_of_2(size)) {
227 printk("Size 0x%x is not a power of 2\n", size);
230 if (size < SRMMU_NOCACHE_BITMAP_SHIFT) {
231 printk("Size 0x%x is too small\n", size);
234 if (vaddr & (size-1)) {
235 printk("Vaddr %lx is not aligned to size 0x%x\n", vaddr, size);
239 offset = (vaddr - SRMMU_NOCACHE_VADDR) >> SRMMU_NOCACHE_BITMAP_SHIFT;
240 size = size >> SRMMU_NOCACHE_BITMAP_SHIFT;
242 bit_map_clear(&srmmu_nocache_map, offset, size);
245 static void srmmu_early_allocate_ptable_skeleton(unsigned long start,
248 extern unsigned long probe_memory(void); /* in fault.c */
251 * Reserve nocache dynamically proportionally to the amount of
252 * system RAM. -- Tomas Szepe <szepe@pinerecords.com>, June 2002
254 static void srmmu_nocache_calcsize(void)
256 unsigned long sysmemavail = probe_memory() / 1024;
257 int srmmu_nocache_npages;
259 srmmu_nocache_npages =
260 sysmemavail / SRMMU_NOCACHE_ALCRATIO / 1024 * 256;
262 /* P3 XXX The 4x overuse: corroborated by /proc/meminfo. */
263 // if (srmmu_nocache_npages < 256) srmmu_nocache_npages = 256;
264 if (srmmu_nocache_npages < SRMMU_MIN_NOCACHE_PAGES)
265 srmmu_nocache_npages = SRMMU_MIN_NOCACHE_PAGES;
267 /* anything above 1280 blows up */
268 if (srmmu_nocache_npages > SRMMU_MAX_NOCACHE_PAGES)
269 srmmu_nocache_npages = SRMMU_MAX_NOCACHE_PAGES;
271 srmmu_nocache_size = srmmu_nocache_npages * PAGE_SIZE;
272 srmmu_nocache_end = SRMMU_NOCACHE_VADDR + srmmu_nocache_size;
275 static void __init srmmu_nocache_init(void)
277 unsigned int bitmap_bits;
281 unsigned long paddr, vaddr;
282 unsigned long pteval;
284 bitmap_bits = srmmu_nocache_size >> SRMMU_NOCACHE_BITMAP_SHIFT;
286 srmmu_nocache_pool = __alloc_bootmem(srmmu_nocache_size,
287 SRMMU_NOCACHE_ALIGN_MAX, 0UL);
288 memset(srmmu_nocache_pool, 0, srmmu_nocache_size);
290 srmmu_nocache_bitmap = __alloc_bootmem(bitmap_bits >> 3, SMP_CACHE_BYTES, 0UL);
291 bit_map_init(&srmmu_nocache_map, srmmu_nocache_bitmap, bitmap_bits);
293 srmmu_swapper_pg_dir = (pgd_t *)__srmmu_get_nocache(SRMMU_PGD_TABLE_SIZE, SRMMU_PGD_TABLE_SIZE);
294 memset(__nocache_fix(srmmu_swapper_pg_dir), 0, SRMMU_PGD_TABLE_SIZE);
295 init_mm.pgd = srmmu_swapper_pg_dir;
297 srmmu_early_allocate_ptable_skeleton(SRMMU_NOCACHE_VADDR, srmmu_nocache_end);
299 paddr = __pa((unsigned long)srmmu_nocache_pool);
300 vaddr = SRMMU_NOCACHE_VADDR;
302 while (vaddr < srmmu_nocache_end) {
303 pgd = pgd_offset_k(vaddr);
304 pmd = pmd_offset(__nocache_fix(pgd), vaddr);
305 pte = pte_offset_kernel(__nocache_fix(pmd), vaddr);
307 pteval = ((paddr >> 4) | SRMMU_ET_PTE | SRMMU_PRIV);
309 if (srmmu_cache_pagetables)
310 pteval |= SRMMU_CACHE;
312 set_pte(__nocache_fix(pte), __pte(pteval));
322 pgd_t *get_pgd_fast(void)
326 pgd = (pgd_t *)__srmmu_get_nocache(SRMMU_PGD_TABLE_SIZE, SRMMU_PGD_TABLE_SIZE);
328 pgd_t *init = pgd_offset_k(0);
329 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
330 memcpy(pgd + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
331 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
338 * Hardware needs alignment to 256 only, but we align to whole page size
339 * to reduce fragmentation problems due to the buddy principle.
340 * XXX Provide actual fragmentation statistics in /proc.
342 * Alignments up to the page size are the same for physical and virtual
343 * addresses of the nocache area.
345 pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
350 if ((pte = (unsigned long)pte_alloc_one_kernel(mm, address)) == 0)
352 page = pfn_to_page( __nocache_pa(pte) >> PAGE_SHIFT );
353 pgtable_page_ctor(page);
357 void pte_free(struct mm_struct *mm, pgtable_t pte)
361 pgtable_page_dtor(pte);
362 p = (unsigned long)page_address(pte); /* Cached address (for test) */
365 p = page_to_pfn(pte) << PAGE_SHIFT; /* Physical address */
366 p = (unsigned long) __nocache_va(p); /* Nocached virtual */
367 srmmu_free_nocache(p, PTE_SIZE);
372 static inline void alloc_context(struct mm_struct *old_mm, struct mm_struct *mm)
374 struct ctx_list *ctxp;
376 ctxp = ctx_free.next;
377 if(ctxp != &ctx_free) {
378 remove_from_ctx_list(ctxp);
379 add_to_used_ctxlist(ctxp);
380 mm->context = ctxp->ctx_number;
384 ctxp = ctx_used.next;
385 if(ctxp->ctx_mm == old_mm)
387 if(ctxp == &ctx_used)
388 panic("out of mmu contexts");
389 flush_cache_mm(ctxp->ctx_mm);
390 flush_tlb_mm(ctxp->ctx_mm);
391 remove_from_ctx_list(ctxp);
392 add_to_used_ctxlist(ctxp);
393 ctxp->ctx_mm->context = NO_CONTEXT;
395 mm->context = ctxp->ctx_number;
398 static inline void free_context(int context)
400 struct ctx_list *ctx_old;
402 ctx_old = ctx_list_pool + context;
403 remove_from_ctx_list(ctx_old);
404 add_to_free_ctxlist(ctx_old);
408 void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm,
409 struct task_struct *tsk)
411 if(mm->context == NO_CONTEXT) {
412 spin_lock(&srmmu_context_spinlock);
413 alloc_context(old_mm, mm);
414 spin_unlock(&srmmu_context_spinlock);
415 srmmu_ctxd_set(&srmmu_context_table[mm->context], mm->pgd);
418 if (sparc_cpu_model == sparc_leon)
422 hyper_flush_whole_icache();
424 srmmu_set_context(mm->context);
427 /* Low level IO area allocation on the SRMMU. */
428 static inline void srmmu_mapioaddr(unsigned long physaddr,
429 unsigned long virt_addr, int bus_type)
436 physaddr &= PAGE_MASK;
437 pgdp = pgd_offset_k(virt_addr);
438 pmdp = pmd_offset(pgdp, virt_addr);
439 ptep = pte_offset_kernel(pmdp, virt_addr);
440 tmp = (physaddr >> 4) | SRMMU_ET_PTE;
443 * I need to test whether this is consistent over all
444 * sun4m's. The bus_type represents the upper 4 bits of
445 * 36-bit physical address on the I/O space lines...
447 tmp |= (bus_type << 28);
449 __flush_page_to_ram(virt_addr);
450 set_pte(ptep, __pte(tmp));
453 void srmmu_mapiorange(unsigned int bus, unsigned long xpa,
454 unsigned long xva, unsigned int len)
458 srmmu_mapioaddr(xpa, xva, bus);
465 static inline void srmmu_unmapioaddr(unsigned long virt_addr)
471 pgdp = pgd_offset_k(virt_addr);
472 pmdp = pmd_offset(pgdp, virt_addr);
473 ptep = pte_offset_kernel(pmdp, virt_addr);
475 /* No need to flush uncacheable page. */
479 void srmmu_unmapiorange(unsigned long virt_addr, unsigned int len)
483 srmmu_unmapioaddr(virt_addr);
484 virt_addr += PAGE_SIZE;
490 * On the SRMMU we do not have the problems with limited tlb entries
491 * for mapping kernel pages, so we just take things from the free page
492 * pool. As a side effect we are putting a little too much pressure
493 * on the gfp() subsystem. This setup also makes the logic of the
494 * iommu mapping code a lot easier as we can transparently handle
495 * mappings on the kernel stack without any special code.
497 struct thread_info *alloc_thread_info_node(struct task_struct *tsk, int node)
499 struct thread_info *ret;
501 ret = (struct thread_info *)__get_free_pages(GFP_KERNEL,
503 #ifdef CONFIG_DEBUG_STACK_USAGE
505 memset(ret, 0, PAGE_SIZE << THREAD_INFO_ORDER);
506 #endif /* DEBUG_STACK_USAGE */
511 void free_thread_info(struct thread_info *ti)
513 free_pages((unsigned long)ti, THREAD_INFO_ORDER);
517 extern void tsunami_flush_cache_all(void);
518 extern void tsunami_flush_cache_mm(struct mm_struct *mm);
519 extern void tsunami_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
520 extern void tsunami_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
521 extern void tsunami_flush_page_to_ram(unsigned long page);
522 extern void tsunami_flush_page_for_dma(unsigned long page);
523 extern void tsunami_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
524 extern void tsunami_flush_tlb_all(void);
525 extern void tsunami_flush_tlb_mm(struct mm_struct *mm);
526 extern void tsunami_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
527 extern void tsunami_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
528 extern void tsunami_setup_blockops(void);
531 extern void swift_flush_cache_all(void);
532 extern void swift_flush_cache_mm(struct mm_struct *mm);
533 extern void swift_flush_cache_range(struct vm_area_struct *vma,
534 unsigned long start, unsigned long end);
535 extern void swift_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
536 extern void swift_flush_page_to_ram(unsigned long page);
537 extern void swift_flush_page_for_dma(unsigned long page);
538 extern void swift_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
539 extern void swift_flush_tlb_all(void);
540 extern void swift_flush_tlb_mm(struct mm_struct *mm);
541 extern void swift_flush_tlb_range(struct vm_area_struct *vma,
542 unsigned long start, unsigned long end);
543 extern void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
545 #if 0 /* P3: deadwood to debug precise flushes on Swift. */
546 void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
551 if ((ctx1 = vma->vm_mm->context) != -1) {
552 cctx = srmmu_get_context();
553 /* Is context # ever different from current context? P3 */
555 printk("flush ctx %02x curr %02x\n", ctx1, cctx);
556 srmmu_set_context(ctx1);
557 swift_flush_page(page);
558 __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
559 "r" (page), "i" (ASI_M_FLUSH_PROBE));
560 srmmu_set_context(cctx);
562 /* Rm. prot. bits from virt. c. */
563 /* swift_flush_cache_all(); */
564 /* swift_flush_cache_page(vma, page); */
565 swift_flush_page(page);
567 __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
568 "r" (page), "i" (ASI_M_FLUSH_PROBE));
569 /* same as above: srmmu_flush_tlb_page() */
576 * The following are all MBUS based SRMMU modules, and therefore could
577 * be found in a multiprocessor configuration. On the whole, these
578 * chips seems to be much more touchy about DVMA and page tables
579 * with respect to cache coherency.
583 extern void viking_flush_cache_all(void);
584 extern void viking_flush_cache_mm(struct mm_struct *mm);
585 extern void viking_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
587 extern void viking_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
588 extern void viking_flush_page_to_ram(unsigned long page);
589 extern void viking_flush_page_for_dma(unsigned long page);
590 extern void viking_flush_sig_insns(struct mm_struct *mm, unsigned long addr);
591 extern void viking_flush_page(unsigned long page);
592 extern void viking_mxcc_flush_page(unsigned long page);
593 extern void viking_flush_tlb_all(void);
594 extern void viking_flush_tlb_mm(struct mm_struct *mm);
595 extern void viking_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
597 extern void viking_flush_tlb_page(struct vm_area_struct *vma,
599 extern void sun4dsmp_flush_tlb_all(void);
600 extern void sun4dsmp_flush_tlb_mm(struct mm_struct *mm);
601 extern void sun4dsmp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
603 extern void sun4dsmp_flush_tlb_page(struct vm_area_struct *vma,
607 extern void hypersparc_flush_cache_all(void);
608 extern void hypersparc_flush_cache_mm(struct mm_struct *mm);
609 extern void hypersparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
610 extern void hypersparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page);
611 extern void hypersparc_flush_page_to_ram(unsigned long page);
612 extern void hypersparc_flush_page_for_dma(unsigned long page);
613 extern void hypersparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr);
614 extern void hypersparc_flush_tlb_all(void);
615 extern void hypersparc_flush_tlb_mm(struct mm_struct *mm);
616 extern void hypersparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
617 extern void hypersparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page);
618 extern void hypersparc_setup_blockops(void);
621 * NOTE: All of this startup code assumes the low 16mb (approx.) of
622 * kernel mappings are done with one single contiguous chunk of
623 * ram. On small ram machines (classics mainly) we only get
624 * around 8mb mapped for us.
627 static void __init early_pgtable_allocfail(char *type)
629 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type);
633 static void __init srmmu_early_allocate_ptable_skeleton(unsigned long start,
641 pgdp = pgd_offset_k(start);
642 if (pgd_none(*(pgd_t *)__nocache_fix(pgdp))) {
643 pmdp = (pmd_t *) __srmmu_get_nocache(
644 SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
646 early_pgtable_allocfail("pmd");
647 memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE);
648 pgd_set(__nocache_fix(pgdp), pmdp);
650 pmdp = pmd_offset(__nocache_fix(pgdp), start);
651 if(srmmu_pmd_none(*(pmd_t *)__nocache_fix(pmdp))) {
652 ptep = (pte_t *)__srmmu_get_nocache(PTE_SIZE, PTE_SIZE);
654 early_pgtable_allocfail("pte");
655 memset(__nocache_fix(ptep), 0, PTE_SIZE);
656 pmd_set(__nocache_fix(pmdp), ptep);
658 if (start > (0xffffffffUL - PMD_SIZE))
660 start = (start + PMD_SIZE) & PMD_MASK;
664 static void __init srmmu_allocate_ptable_skeleton(unsigned long start,
672 pgdp = pgd_offset_k(start);
673 if (pgd_none(*pgdp)) {
674 pmdp = (pmd_t *)__srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
676 early_pgtable_allocfail("pmd");
677 memset(pmdp, 0, SRMMU_PMD_TABLE_SIZE);
680 pmdp = pmd_offset(pgdp, start);
681 if(srmmu_pmd_none(*pmdp)) {
682 ptep = (pte_t *) __srmmu_get_nocache(PTE_SIZE,
685 early_pgtable_allocfail("pte");
686 memset(ptep, 0, PTE_SIZE);
689 if (start > (0xffffffffUL - PMD_SIZE))
691 start = (start + PMD_SIZE) & PMD_MASK;
696 * This is much cleaner than poking around physical address space
697 * looking at the prom's page table directly which is what most
698 * other OS's do. Yuck... this is much better.
700 static void __init srmmu_inherit_prom_mappings(unsigned long start,
706 int what = 0; /* 0 = normal-pte, 1 = pmd-level pte, 2 = pgd-level pte */
707 unsigned long prompte;
709 while(start <= end) {
711 break; /* probably wrap around */
712 if(start == 0xfef00000)
713 start = KADB_DEBUGGER_BEGVM;
714 if(!(prompte = srmmu_hwprobe(start))) {
719 /* A red snapper, see what it really is. */
722 if(!(start & ~(SRMMU_REAL_PMD_MASK))) {
723 if(srmmu_hwprobe((start-PAGE_SIZE) + SRMMU_REAL_PMD_SIZE) == prompte)
727 if(!(start & ~(SRMMU_PGDIR_MASK))) {
728 if(srmmu_hwprobe((start-PAGE_SIZE) + SRMMU_PGDIR_SIZE) ==
733 pgdp = pgd_offset_k(start);
735 *(pgd_t *)__nocache_fix(pgdp) = __pgd(prompte);
736 start += SRMMU_PGDIR_SIZE;
739 if (pgd_none(*(pgd_t *)__nocache_fix(pgdp))) {
740 pmdp = (pmd_t *)__srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE);
742 early_pgtable_allocfail("pmd");
743 memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE);
744 pgd_set(__nocache_fix(pgdp), pmdp);
746 pmdp = pmd_offset(__nocache_fix(pgdp), start);
747 if(srmmu_pmd_none(*(pmd_t *)__nocache_fix(pmdp))) {
748 ptep = (pte_t *) __srmmu_get_nocache(PTE_SIZE,
751 early_pgtable_allocfail("pte");
752 memset(__nocache_fix(ptep), 0, PTE_SIZE);
753 pmd_set(__nocache_fix(pmdp), ptep);
757 * We bend the rule where all 16 PTPs in a pmd_t point
758 * inside the same PTE page, and we leak a perfectly
759 * good hardware PTE piece. Alternatives seem worse.
761 unsigned int x; /* Index of HW PMD in soft cluster */
762 x = (start >> PMD_SHIFT) & 15;
763 *(unsigned long *)__nocache_fix(&pmdp->pmdv[x]) = prompte;
764 start += SRMMU_REAL_PMD_SIZE;
767 ptep = pte_offset_kernel(__nocache_fix(pmdp), start);
768 *(pte_t *)__nocache_fix(ptep) = __pte(prompte);
773 #define KERNEL_PTE(page_shifted) ((page_shifted)|SRMMU_CACHE|SRMMU_PRIV|SRMMU_VALID)
775 /* Create a third-level SRMMU 16MB page mapping. */
776 static void __init do_large_mapping(unsigned long vaddr, unsigned long phys_base)
778 pgd_t *pgdp = pgd_offset_k(vaddr);
779 unsigned long big_pte;
781 big_pte = KERNEL_PTE(phys_base >> 4);
782 *(pgd_t *)__nocache_fix(pgdp) = __pgd(big_pte);
785 /* Map sp_bank entry SP_ENTRY, starting at virtual address VBASE. */
786 static unsigned long __init map_spbank(unsigned long vbase, int sp_entry)
788 unsigned long pstart = (sp_banks[sp_entry].base_addr & SRMMU_PGDIR_MASK);
789 unsigned long vstart = (vbase & SRMMU_PGDIR_MASK);
790 unsigned long vend = SRMMU_PGDIR_ALIGN(vbase + sp_banks[sp_entry].num_bytes);
791 /* Map "low" memory only */
792 const unsigned long min_vaddr = PAGE_OFFSET;
793 const unsigned long max_vaddr = PAGE_OFFSET + SRMMU_MAXMEM;
795 if (vstart < min_vaddr || vstart >= max_vaddr)
798 if (vend > max_vaddr || vend < min_vaddr)
801 while(vstart < vend) {
802 do_large_mapping(vstart, pstart);
803 vstart += SRMMU_PGDIR_SIZE; pstart += SRMMU_PGDIR_SIZE;
808 static inline void memprobe_error(char *msg)
811 prom_printf("Halting now...\n");
815 static inline void map_kernel(void)
820 do_large_mapping(PAGE_OFFSET, phys_base);
823 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
824 map_spbank((unsigned long)__va(sp_banks[i].base_addr), i);
828 /* Paging initialization on the Sparc Reference MMU. */
829 extern void sparc_context_init(int);
831 void (*poke_srmmu)(void) __cpuinitdata = NULL;
833 extern unsigned long bootmem_init(unsigned long *pages_avail);
835 void __init srmmu_paging_init(void)
843 unsigned long pages_avail;
845 sparc_iomap.start = SUN4M_IOBASE_VADDR; /* 16MB of IOSPACE on all sun4m's. */
847 if (sparc_cpu_model == sun4d)
848 num_contexts = 65536; /* We know it is Viking */
850 /* Find the number of contexts on the srmmu. */
851 cpunode = prom_getchild(prom_root_node);
853 while(cpunode != 0) {
854 prom_getstring(cpunode, "device_type", node_str, sizeof(node_str));
855 if(!strcmp(node_str, "cpu")) {
856 num_contexts = prom_getintdefault(cpunode, "mmu-nctx", 0x8);
859 cpunode = prom_getsibling(cpunode);
864 prom_printf("Something wrong, can't find cpu node in paging_init.\n");
869 last_valid_pfn = bootmem_init(&pages_avail);
871 srmmu_nocache_calcsize();
872 srmmu_nocache_init();
873 srmmu_inherit_prom_mappings(0xfe400000,(LINUX_OPPROM_ENDVM-PAGE_SIZE));
876 /* ctx table has to be physically aligned to its size */
877 srmmu_context_table = (ctxd_t *)__srmmu_get_nocache(num_contexts*sizeof(ctxd_t), num_contexts*sizeof(ctxd_t));
878 srmmu_ctx_table_phys = (ctxd_t *)__nocache_pa((unsigned long)srmmu_context_table);
880 for(i = 0; i < num_contexts; i++)
881 srmmu_ctxd_set((ctxd_t *)__nocache_fix(&srmmu_context_table[i]), srmmu_swapper_pg_dir);
884 srmmu_set_ctable_ptr((unsigned long)srmmu_ctx_table_phys);
886 /* Stop from hanging here... */
887 local_ops->tlb_all();
893 srmmu_allocate_ptable_skeleton(sparc_iomap.start, IOBASE_END);
894 srmmu_allocate_ptable_skeleton(DVMA_VADDR, DVMA_END);
896 srmmu_allocate_ptable_skeleton(
897 __fix_to_virt(__end_of_fixed_addresses - 1), FIXADDR_TOP);
898 srmmu_allocate_ptable_skeleton(PKMAP_BASE, PKMAP_END);
900 pgd = pgd_offset_k(PKMAP_BASE);
901 pmd = pmd_offset(pgd, PKMAP_BASE);
902 pte = pte_offset_kernel(pmd, PKMAP_BASE);
903 pkmap_page_table = pte;
908 sparc_context_init(num_contexts);
913 unsigned long zones_size[MAX_NR_ZONES];
914 unsigned long zholes_size[MAX_NR_ZONES];
915 unsigned long npages;
918 for (znum = 0; znum < MAX_NR_ZONES; znum++)
919 zones_size[znum] = zholes_size[znum] = 0;
921 npages = max_low_pfn - pfn_base;
923 zones_size[ZONE_DMA] = npages;
924 zholes_size[ZONE_DMA] = npages - pages_avail;
926 npages = highend_pfn - max_low_pfn;
927 zones_size[ZONE_HIGHMEM] = npages;
928 zholes_size[ZONE_HIGHMEM] = npages - calc_highpages();
930 free_area_init_node(0, zones_size, pfn_base, zholes_size);
934 void mmu_info(struct seq_file *m)
939 "nocache total\t: %ld\n"
940 "nocache used\t: %d\n",
944 srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT);
947 void destroy_context(struct mm_struct *mm)
950 if(mm->context != NO_CONTEXT) {
952 srmmu_ctxd_set(&srmmu_context_table[mm->context], srmmu_swapper_pg_dir);
954 spin_lock(&srmmu_context_spinlock);
955 free_context(mm->context);
956 spin_unlock(&srmmu_context_spinlock);
957 mm->context = NO_CONTEXT;
961 /* Init various srmmu chip types. */
962 static void __init srmmu_is_bad(void)
964 prom_printf("Could not determine SRMMU chip type.\n");
968 static void __init init_vac_layout(void)
975 unsigned long max_size = 0;
976 unsigned long min_line_size = 0x10000000;
979 nd = prom_getchild(prom_root_node);
980 while((nd = prom_getsibling(nd)) != 0) {
981 prom_getstring(nd, "device_type", node_str, sizeof(node_str));
982 if(!strcmp(node_str, "cpu")) {
983 vac_line_size = prom_getint(nd, "cache-line-size");
984 if (vac_line_size == -1) {
985 prom_printf("can't determine cache-line-size, "
989 cache_lines = prom_getint(nd, "cache-nlines");
990 if (cache_lines == -1) {
991 prom_printf("can't determine cache-nlines, halting.\n");
995 vac_cache_size = cache_lines * vac_line_size;
997 if(vac_cache_size > max_size)
998 max_size = vac_cache_size;
999 if(vac_line_size < min_line_size)
1000 min_line_size = vac_line_size;
1001 //FIXME: cpus not contiguous!!
1003 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
1011 prom_printf("No CPU nodes found, halting.\n");
1015 vac_cache_size = max_size;
1016 vac_line_size = min_line_size;
1018 printk("SRMMU: Using VAC size of %d bytes, line size %d bytes.\n",
1019 (int)vac_cache_size, (int)vac_line_size);
1022 static void __cpuinit poke_hypersparc(void)
1024 volatile unsigned long clear;
1025 unsigned long mreg = srmmu_get_mmureg();
1027 hyper_flush_unconditional_combined();
1029 mreg &= ~(HYPERSPARC_CWENABLE);
1030 mreg |= (HYPERSPARC_CENABLE | HYPERSPARC_WBENABLE);
1031 mreg |= (HYPERSPARC_CMODE);
1033 srmmu_set_mmureg(mreg);
1035 #if 0 /* XXX I think this is bad news... -DaveM */
1036 hyper_clear_all_tags();
1039 put_ross_icr(HYPERSPARC_ICCR_FTD | HYPERSPARC_ICCR_ICE);
1040 hyper_flush_whole_icache();
1041 clear = srmmu_get_faddr();
1042 clear = srmmu_get_fstatus();
1045 static const struct sparc32_cachetlb_ops hypersparc_ops = {
1046 .cache_all = hypersparc_flush_cache_all,
1047 .cache_mm = hypersparc_flush_cache_mm,
1048 .cache_page = hypersparc_flush_cache_page,
1049 .cache_range = hypersparc_flush_cache_range,
1050 .tlb_all = hypersparc_flush_tlb_all,
1051 .tlb_mm = hypersparc_flush_tlb_mm,
1052 .tlb_page = hypersparc_flush_tlb_page,
1053 .tlb_range = hypersparc_flush_tlb_range,
1054 .page_to_ram = hypersparc_flush_page_to_ram,
1055 .sig_insns = hypersparc_flush_sig_insns,
1056 .page_for_dma = hypersparc_flush_page_for_dma,
1059 static void __init init_hypersparc(void)
1061 srmmu_name = "ROSS HyperSparc";
1062 srmmu_modtype = HyperSparc;
1067 sparc32_cachetlb_ops = &hypersparc_ops;
1069 poke_srmmu = poke_hypersparc;
1071 hypersparc_setup_blockops();
1074 static void __cpuinit poke_swift(void)
1078 /* Clear any crap from the cache or else... */
1079 swift_flush_cache_all();
1081 /* Enable I & D caches */
1082 mreg = srmmu_get_mmureg();
1083 mreg |= (SWIFT_IE | SWIFT_DE);
1085 * The Swift branch folding logic is completely broken. At
1086 * trap time, if things are just right, if can mistakenly
1087 * think that a trap is coming from kernel mode when in fact
1088 * it is coming from user mode (it mis-executes the branch in
1089 * the trap code). So you see things like crashme completely
1090 * hosing your machine which is completely unacceptable. Turn
1091 * this shit off... nice job Fujitsu.
1093 mreg &= ~(SWIFT_BF);
1094 srmmu_set_mmureg(mreg);
1097 static const struct sparc32_cachetlb_ops swift_ops = {
1098 .cache_all = swift_flush_cache_all,
1099 .cache_mm = swift_flush_cache_mm,
1100 .cache_page = swift_flush_cache_page,
1101 .cache_range = swift_flush_cache_range,
1102 .tlb_all = swift_flush_tlb_all,
1103 .tlb_mm = swift_flush_tlb_mm,
1104 .tlb_page = swift_flush_tlb_page,
1105 .tlb_range = swift_flush_tlb_range,
1106 .page_to_ram = swift_flush_page_to_ram,
1107 .sig_insns = swift_flush_sig_insns,
1108 .page_for_dma = swift_flush_page_for_dma,
1111 #define SWIFT_MASKID_ADDR 0x10003018
1112 static void __init init_swift(void)
1114 unsigned long swift_rev;
1116 __asm__ __volatile__("lda [%1] %2, %0\n\t"
1117 "srl %0, 0x18, %0\n\t" :
1119 "r" (SWIFT_MASKID_ADDR), "i" (ASI_M_BYPASS));
1120 srmmu_name = "Fujitsu Swift";
1126 srmmu_modtype = Swift_lots_o_bugs;
1127 hwbug_bitmask |= (HWBUG_KERN_ACCBROKEN | HWBUG_KERN_CBITBROKEN);
1129 * Gee george, I wonder why Sun is so hush hush about
1130 * this hardware bug... really braindamage stuff going
1131 * on here. However I think we can find a way to avoid
1132 * all of the workaround overhead under Linux. Basically,
1133 * any page fault can cause kernel pages to become user
1134 * accessible (the mmu gets confused and clears some of
1135 * the ACC bits in kernel ptes). Aha, sounds pretty
1136 * horrible eh? But wait, after extensive testing it appears
1137 * that if you use pgd_t level large kernel pte's (like the
1138 * 4MB pages on the Pentium) the bug does not get tripped
1139 * at all. This avoids almost all of the major overhead.
1140 * Welcome to a world where your vendor tells you to,
1141 * "apply this kernel patch" instead of "sorry for the
1142 * broken hardware, send it back and we'll give you
1143 * properly functioning parts"
1148 srmmu_modtype = Swift_bad_c;
1149 hwbug_bitmask |= HWBUG_KERN_CBITBROKEN;
1151 * You see Sun allude to this hardware bug but never
1152 * admit things directly, they'll say things like,
1153 * "the Swift chip cache problems" or similar.
1157 srmmu_modtype = Swift_ok;
1161 sparc32_cachetlb_ops = &swift_ops;
1162 flush_page_for_dma_global = 0;
1165 * Are you now convinced that the Swift is one of the
1166 * biggest VLSI abortions of all time? Bravo Fujitsu!
1167 * Fujitsu, the !#?!%$'d up processor people. I bet if
1168 * you examined the microcode of the Swift you'd find
1169 * XXX's all over the place.
1171 poke_srmmu = poke_swift;
1174 static void turbosparc_flush_cache_all(void)
1176 flush_user_windows();
1177 turbosparc_idflash_clear();
1180 static void turbosparc_flush_cache_mm(struct mm_struct *mm)
1183 flush_user_windows();
1184 turbosparc_idflash_clear();
1188 static void turbosparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1190 FLUSH_BEGIN(vma->vm_mm)
1191 flush_user_windows();
1192 turbosparc_idflash_clear();
1196 static void turbosparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
1198 FLUSH_BEGIN(vma->vm_mm)
1199 flush_user_windows();
1200 if (vma->vm_flags & VM_EXEC)
1201 turbosparc_flush_icache();
1202 turbosparc_flush_dcache();
1206 /* TurboSparc is copy-back, if we turn it on, but this does not work. */
1207 static void turbosparc_flush_page_to_ram(unsigned long page)
1209 #ifdef TURBOSPARC_WRITEBACK
1210 volatile unsigned long clear;
1212 if (srmmu_hwprobe(page))
1213 turbosparc_flush_page_cache(page);
1214 clear = srmmu_get_fstatus();
1218 static void turbosparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
1222 static void turbosparc_flush_page_for_dma(unsigned long page)
1224 turbosparc_flush_dcache();
1227 static void turbosparc_flush_tlb_all(void)
1229 srmmu_flush_whole_tlb();
1232 static void turbosparc_flush_tlb_mm(struct mm_struct *mm)
1235 srmmu_flush_whole_tlb();
1239 static void turbosparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1241 FLUSH_BEGIN(vma->vm_mm)
1242 srmmu_flush_whole_tlb();
1246 static void turbosparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
1248 FLUSH_BEGIN(vma->vm_mm)
1249 srmmu_flush_whole_tlb();
1254 static void __cpuinit poke_turbosparc(void)
1256 unsigned long mreg = srmmu_get_mmureg();
1257 unsigned long ccreg;
1259 /* Clear any crap from the cache or else... */
1260 turbosparc_flush_cache_all();
1261 mreg &= ~(TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); /* Temporarily disable I & D caches */
1262 mreg &= ~(TURBOSPARC_PCENABLE); /* Don't check parity */
1263 srmmu_set_mmureg(mreg);
1265 ccreg = turbosparc_get_ccreg();
1267 #ifdef TURBOSPARC_WRITEBACK
1268 ccreg |= (TURBOSPARC_SNENABLE); /* Do DVMA snooping in Dcache */
1269 ccreg &= ~(TURBOSPARC_uS2 | TURBOSPARC_WTENABLE);
1270 /* Write-back D-cache, emulate VLSI
1271 * abortion number three, not number one */
1273 /* For now let's play safe, optimize later */
1274 ccreg |= (TURBOSPARC_SNENABLE | TURBOSPARC_WTENABLE);
1275 /* Do DVMA snooping in Dcache, Write-thru D-cache */
1276 ccreg &= ~(TURBOSPARC_uS2);
1277 /* Emulate VLSI abortion number three, not number one */
1280 switch (ccreg & 7) {
1281 case 0: /* No SE cache */
1282 case 7: /* Test mode */
1285 ccreg |= (TURBOSPARC_SCENABLE);
1287 turbosparc_set_ccreg (ccreg);
1289 mreg |= (TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); /* I & D caches on */
1290 mreg |= (TURBOSPARC_ICSNOOP); /* Icache snooping on */
1291 srmmu_set_mmureg(mreg);
1294 static const struct sparc32_cachetlb_ops turbosparc_ops = {
1295 .cache_all = turbosparc_flush_cache_all,
1296 .cache_mm = turbosparc_flush_cache_mm,
1297 .cache_page = turbosparc_flush_cache_page,
1298 .cache_range = turbosparc_flush_cache_range,
1299 .tlb_all = turbosparc_flush_tlb_all,
1300 .tlb_mm = turbosparc_flush_tlb_mm,
1301 .tlb_page = turbosparc_flush_tlb_page,
1302 .tlb_range = turbosparc_flush_tlb_range,
1303 .page_to_ram = turbosparc_flush_page_to_ram,
1304 .sig_insns = turbosparc_flush_sig_insns,
1305 .page_for_dma = turbosparc_flush_page_for_dma,
1308 static void __init init_turbosparc(void)
1310 srmmu_name = "Fujitsu TurboSparc";
1311 srmmu_modtype = TurboSparc;
1312 sparc32_cachetlb_ops = &turbosparc_ops;
1313 poke_srmmu = poke_turbosparc;
1316 static void __cpuinit poke_tsunami(void)
1318 unsigned long mreg = srmmu_get_mmureg();
1320 tsunami_flush_icache();
1321 tsunami_flush_dcache();
1322 mreg &= ~TSUNAMI_ITD;
1323 mreg |= (TSUNAMI_IENAB | TSUNAMI_DENAB);
1324 srmmu_set_mmureg(mreg);
1327 static const struct sparc32_cachetlb_ops tsunami_ops = {
1328 .cache_all = tsunami_flush_cache_all,
1329 .cache_mm = tsunami_flush_cache_mm,
1330 .cache_page = tsunami_flush_cache_page,
1331 .cache_range = tsunami_flush_cache_range,
1332 .tlb_all = tsunami_flush_tlb_all,
1333 .tlb_mm = tsunami_flush_tlb_mm,
1334 .tlb_page = tsunami_flush_tlb_page,
1335 .tlb_range = tsunami_flush_tlb_range,
1336 .page_to_ram = tsunami_flush_page_to_ram,
1337 .sig_insns = tsunami_flush_sig_insns,
1338 .page_for_dma = tsunami_flush_page_for_dma,
1341 static void __init init_tsunami(void)
1344 * Tsunami's pretty sane, Sun and TI actually got it
1345 * somewhat right this time. Fujitsu should have
1346 * taken some lessons from them.
1349 srmmu_name = "TI Tsunami";
1350 srmmu_modtype = Tsunami;
1351 sparc32_cachetlb_ops = &tsunami_ops;
1352 poke_srmmu = poke_tsunami;
1354 tsunami_setup_blockops();
1357 static void __cpuinit poke_viking(void)
1359 unsigned long mreg = srmmu_get_mmureg();
1360 static int smp_catch;
1362 if (viking_mxcc_present) {
1363 unsigned long mxcc_control = mxcc_get_creg();
1365 mxcc_control |= (MXCC_CTL_ECE | MXCC_CTL_PRE | MXCC_CTL_MCE);
1366 mxcc_control &= ~(MXCC_CTL_RRC);
1367 mxcc_set_creg(mxcc_control);
1370 * We don't need memory parity checks.
1371 * XXX This is a mess, have to dig out later. ecd.
1372 viking_mxcc_turn_off_parity(&mreg, &mxcc_control);
1375 /* We do cache ptables on MXCC. */
1376 mreg |= VIKING_TCENABLE;
1378 unsigned long bpreg;
1380 mreg &= ~(VIKING_TCENABLE);
1382 /* Must disable mixed-cmd mode here for other cpu's. */
1383 bpreg = viking_get_bpreg();
1384 bpreg &= ~(VIKING_ACTION_MIX);
1385 viking_set_bpreg(bpreg);
1387 /* Just in case PROM does something funny. */
1392 mreg |= VIKING_SPENABLE;
1393 mreg |= (VIKING_ICENABLE | VIKING_DCENABLE);
1394 mreg |= VIKING_SBENABLE;
1395 mreg &= ~(VIKING_ACENABLE);
1396 srmmu_set_mmureg(mreg);
1399 static struct sparc32_cachetlb_ops viking_ops = {
1400 .cache_all = viking_flush_cache_all,
1401 .cache_mm = viking_flush_cache_mm,
1402 .cache_page = viking_flush_cache_page,
1403 .cache_range = viking_flush_cache_range,
1404 .tlb_all = viking_flush_tlb_all,
1405 .tlb_mm = viking_flush_tlb_mm,
1406 .tlb_page = viking_flush_tlb_page,
1407 .tlb_range = viking_flush_tlb_range,
1408 .page_to_ram = viking_flush_page_to_ram,
1409 .sig_insns = viking_flush_sig_insns,
1410 .page_for_dma = viking_flush_page_for_dma,
1414 /* On sun4d the cpu broadcasts local TLB flushes, so we can just
1415 * perform the local TLB flush and all the other cpus will see it.
1416 * But, unfortunately, there is a bug in the sun4d XBUS backplane
1417 * that requires that we add some synchronization to these flushes.
1419 * The bug is that the fifo which keeps track of all the pending TLB
1420 * broadcasts in the system is an entry or two too small, so if we
1421 * have too many going at once we'll overflow that fifo and lose a TLB
1422 * flush resulting in corruption.
1424 * Our workaround is to take a global spinlock around the TLB flushes,
1425 * which guarentees we won't ever have too many pending. It's a big
1426 * hammer, but a semaphore like system to make sure we only have N TLB
1427 * flushes going at once will require SMP locking anyways so there's
1428 * no real value in trying any harder than this.
1430 static struct sparc32_cachetlb_ops viking_sun4d_smp_ops = {
1431 .cache_all = viking_flush_cache_all,
1432 .cache_mm = viking_flush_cache_mm,
1433 .cache_page = viking_flush_cache_page,
1434 .cache_range = viking_flush_cache_range,
1435 .tlb_all = sun4dsmp_flush_tlb_all,
1436 .tlb_mm = sun4dsmp_flush_tlb_mm,
1437 .tlb_page = sun4dsmp_flush_tlb_page,
1438 .tlb_range = sun4dsmp_flush_tlb_range,
1439 .page_to_ram = viking_flush_page_to_ram,
1440 .sig_insns = viking_flush_sig_insns,
1441 .page_for_dma = viking_flush_page_for_dma,
1445 static void __init init_viking(void)
1447 unsigned long mreg = srmmu_get_mmureg();
1449 /* Ahhh, the viking. SRMMU VLSI abortion number two... */
1450 if(mreg & VIKING_MMODE) {
1451 srmmu_name = "TI Viking";
1452 viking_mxcc_present = 0;
1456 * We need this to make sure old viking takes no hits
1457 * on it's cache for dma snoops to workaround the
1458 * "load from non-cacheable memory" interrupt bug.
1459 * This is only necessary because of the new way in
1460 * which we use the IOMMU.
1462 viking_ops.page_for_dma = viking_flush_page;
1464 viking_sun4d_smp_ops.page_for_dma = viking_flush_page;
1466 flush_page_for_dma_global = 0;
1468 srmmu_name = "TI Viking/MXCC";
1469 viking_mxcc_present = 1;
1470 srmmu_cache_pagetables = 1;
1473 sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *)
1476 if (sparc_cpu_model == sun4d)
1477 sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *)
1478 &viking_sun4d_smp_ops;
1481 poke_srmmu = poke_viking;
1484 /* Probe for the srmmu chip version. */
1485 static void __init get_srmmu_type(void)
1487 unsigned long mreg, psr;
1488 unsigned long mod_typ, mod_rev, psr_typ, psr_vers;
1490 srmmu_modtype = SRMMU_INVAL_MOD;
1493 mreg = srmmu_get_mmureg(); psr = get_psr();
1494 mod_typ = (mreg & 0xf0000000) >> 28;
1495 mod_rev = (mreg & 0x0f000000) >> 24;
1496 psr_typ = (psr >> 28) & 0xf;
1497 psr_vers = (psr >> 24) & 0xf;
1499 /* First, check for sparc-leon. */
1500 if (sparc_cpu_model == sparc_leon) {
1505 /* Second, check for HyperSparc or Cypress. */
1509 /* UP or MP Hypersparc */
1521 prom_printf("Sparc-Linux Cypress support does not longer exit.\n");
1529 * Now Fujitsu TurboSparc. It might happen that it is
1530 * in Swift emulation mode, so we will check later...
1532 if (psr_typ == 0 && psr_vers == 5) {
1537 /* Next check for Fujitsu Swift. */
1538 if(psr_typ == 0 && psr_vers == 4) {
1542 /* Look if it is not a TurboSparc emulating Swift... */
1543 cpunode = prom_getchild(prom_root_node);
1544 while((cpunode = prom_getsibling(cpunode)) != 0) {
1545 prom_getstring(cpunode, "device_type", node_str, sizeof(node_str));
1546 if(!strcmp(node_str, "cpu")) {
1547 if (!prom_getintdefault(cpunode, "psr-implementation", 1) &&
1548 prom_getintdefault(cpunode, "psr-version", 1) == 5) {
1560 /* Now the Viking family of srmmu. */
1563 ((psr_vers == 1) && (mod_typ == 0) && (mod_rev == 0)))) {
1568 /* Finally the Tsunami. */
1569 if(psr_typ == 4 && psr_vers == 1 && (mod_typ || mod_rev)) {
1579 /* Local cross-calls. */
1580 static void smp_flush_page_for_dma(unsigned long page)
1582 xc1((smpfunc_t) local_ops->page_for_dma, page);
1583 local_ops->page_for_dma(page);
1586 static void smp_flush_cache_all(void)
1588 xc0((smpfunc_t) local_ops->cache_all);
1589 local_ops->cache_all();
1592 static void smp_flush_tlb_all(void)
1594 xc0((smpfunc_t) local_ops->tlb_all);
1595 local_ops->tlb_all();
1598 static void smp_flush_cache_mm(struct mm_struct *mm)
1600 if (mm->context != NO_CONTEXT) {
1602 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1603 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1604 if (!cpumask_empty(&cpu_mask))
1605 xc1((smpfunc_t) local_ops->cache_mm, (unsigned long) mm);
1606 local_ops->cache_mm(mm);
1610 static void smp_flush_tlb_mm(struct mm_struct *mm)
1612 if (mm->context != NO_CONTEXT) {
1614 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1615 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1616 if (!cpumask_empty(&cpu_mask)) {
1617 xc1((smpfunc_t) local_ops->tlb_mm, (unsigned long) mm);
1618 if (atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
1619 cpumask_copy(mm_cpumask(mm),
1620 cpumask_of(smp_processor_id()));
1622 local_ops->tlb_mm(mm);
1626 static void smp_flush_cache_range(struct vm_area_struct *vma,
1627 unsigned long start,
1630 struct mm_struct *mm = vma->vm_mm;
1632 if (mm->context != NO_CONTEXT) {
1634 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1635 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1636 if (!cpumask_empty(&cpu_mask))
1637 xc3((smpfunc_t) local_ops->cache_range,
1638 (unsigned long) vma, start, end);
1639 local_ops->cache_range(vma, start, end);
1643 static void smp_flush_tlb_range(struct vm_area_struct *vma,
1644 unsigned long start,
1647 struct mm_struct *mm = vma->vm_mm;
1649 if (mm->context != NO_CONTEXT) {
1651 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1652 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1653 if (!cpumask_empty(&cpu_mask))
1654 xc3((smpfunc_t) local_ops->tlb_range,
1655 (unsigned long) vma, start, end);
1656 local_ops->tlb_range(vma, start, end);
1660 static void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
1662 struct mm_struct *mm = vma->vm_mm;
1664 if (mm->context != NO_CONTEXT) {
1666 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1667 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1668 if (!cpumask_empty(&cpu_mask))
1669 xc2((smpfunc_t) local_ops->cache_page,
1670 (unsigned long) vma, page);
1671 local_ops->cache_page(vma, page);
1675 static void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
1677 struct mm_struct *mm = vma->vm_mm;
1679 if (mm->context != NO_CONTEXT) {
1681 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1682 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1683 if (!cpumask_empty(&cpu_mask))
1684 xc2((smpfunc_t) local_ops->tlb_page,
1685 (unsigned long) vma, page);
1686 local_ops->tlb_page(vma, page);
1690 static void smp_flush_page_to_ram(unsigned long page)
1692 /* Current theory is that those who call this are the one's
1693 * who have just dirtied their cache with the pages contents
1694 * in kernel space, therefore we only run this on local cpu.
1696 * XXX This experiment failed, research further... -DaveM
1699 xc1((smpfunc_t) local_ops->page_to_ram, page);
1701 local_ops->page_to_ram(page);
1704 static void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
1707 cpumask_copy(&cpu_mask, mm_cpumask(mm));
1708 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
1709 if (!cpumask_empty(&cpu_mask))
1710 xc2((smpfunc_t) local_ops->sig_insns,
1711 (unsigned long) mm, insn_addr);
1712 local_ops->sig_insns(mm, insn_addr);
1715 static struct sparc32_cachetlb_ops smp_cachetlb_ops = {
1716 .cache_all = smp_flush_cache_all,
1717 .cache_mm = smp_flush_cache_mm,
1718 .cache_page = smp_flush_cache_page,
1719 .cache_range = smp_flush_cache_range,
1720 .tlb_all = smp_flush_tlb_all,
1721 .tlb_mm = smp_flush_tlb_mm,
1722 .tlb_page = smp_flush_tlb_page,
1723 .tlb_range = smp_flush_tlb_range,
1724 .page_to_ram = smp_flush_page_to_ram,
1725 .sig_insns = smp_flush_sig_insns,
1726 .page_for_dma = smp_flush_page_for_dma,
1730 /* Load up routines and constants for sun4m and sun4d mmu */
1731 void __init load_mmu(void)
1733 extern void ld_mmu_iommu(void);
1734 extern void ld_mmu_iounit(void);
1740 /* El switcheroo... */
1741 local_ops = sparc32_cachetlb_ops;
1743 if (sparc_cpu_model == sun4d || sparc_cpu_model == sparc_leon) {
1744 smp_cachetlb_ops.tlb_all = local_ops->tlb_all;
1745 smp_cachetlb_ops.tlb_mm = local_ops->tlb_mm;
1746 smp_cachetlb_ops.tlb_range = local_ops->tlb_range;
1747 smp_cachetlb_ops.tlb_page = local_ops->tlb_page;
1750 if (poke_srmmu == poke_viking) {
1751 /* Avoid unnecessary cross calls. */
1752 smp_cachetlb_ops.cache_all = local_ops->cache_all;
1753 smp_cachetlb_ops.cache_mm = local_ops->cache_mm;
1754 smp_cachetlb_ops.cache_range = local_ops->cache_range;
1755 smp_cachetlb_ops.cache_page = local_ops->cache_page;
1757 smp_cachetlb_ops.page_to_ram = local_ops->page_to_ram;
1758 smp_cachetlb_ops.sig_insns = local_ops->sig_insns;
1759 smp_cachetlb_ops.page_for_dma = local_ops->page_for_dma;
1762 /* It really is const after this point. */
1763 sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *)
1767 if (sparc_cpu_model == sun4d)
1772 if (sparc_cpu_model == sun4d)
1774 else if (sparc_cpu_model == sparc_leon)