1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * srmmu.c: SRMMU specific routines for memory management. 4 * 5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) 6 * Copyright (C) 1995,2002 Pete Zaitcev (zaitcev@yahoo.com) 7 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be) 8 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) 9 * Copyright (C) 1999,2000 Anton Blanchard (anton@samba.org) 10 */ 11 12 #include <linux/seq_file.h> 13 #include <linux/spinlock.h> 14 #include <linux/memblock.h> 15 #include <linux/pagemap.h> 16 #include <linux/vmalloc.h> 17 #include <linux/kdebug.h> 18 #include <linux/export.h> 19 #include <linux/kernel.h> 20 #include <linux/init.h> 21 #include <linux/log2.h> 22 #include <linux/gfp.h> 23 #include <linux/fs.h> 24 #include <linux/mm.h> 25 26 #include <asm/mmu_context.h> 27 #include <asm/cacheflush.h> 28 #include <asm/tlbflush.h> 29 #include <asm/io-unit.h> 30 #include <asm/pgalloc.h> 31 #include <asm/pgtable.h> 32 #include <asm/bitext.h> 33 #include <asm/vaddrs.h> 34 #include <asm/cache.h> 35 #include <asm/traps.h> 36 #include <asm/oplib.h> 37 #include <asm/mbus.h> 38 #include <asm/page.h> 39 #include <asm/asi.h> 40 #include <asm/smp.h> 41 #include <asm/io.h> 42 43 /* Now the cpu specific definitions. */ 44 #include <asm/turbosparc.h> 45 #include <asm/tsunami.h> 46 #include <asm/viking.h> 47 #include <asm/swift.h> 48 #include <asm/leon.h> 49 #include <asm/mxcc.h> 50 #include <asm/ross.h> 51 52 #include "mm_32.h" 53 54 enum mbus_module srmmu_modtype; 55 static unsigned int hwbug_bitmask; 56 int vac_cache_size; 57 EXPORT_SYMBOL(vac_cache_size); 58 int vac_line_size; 59 60 extern struct resource sparc_iomap; 61 62 extern unsigned long last_valid_pfn; 63 64 static pgd_t *srmmu_swapper_pg_dir; 65 66 const struct sparc32_cachetlb_ops *sparc32_cachetlb_ops; 67 EXPORT_SYMBOL(sparc32_cachetlb_ops); 68 69 #ifdef CONFIG_SMP 70 const struct sparc32_cachetlb_ops *local_ops; 71 72 #define FLUSH_BEGIN(mm) 73 #define FLUSH_END 74 #else 75 #define FLUSH_BEGIN(mm) if ((mm)->context != NO_CONTEXT) { 76 #define FLUSH_END } 77 #endif 78 79 int flush_page_for_dma_global = 1; 80 81 char *srmmu_name; 82 83 ctxd_t *srmmu_ctx_table_phys; 84 static ctxd_t *srmmu_context_table; 85 86 int viking_mxcc_present; 87 static DEFINE_SPINLOCK(srmmu_context_spinlock); 88 89 static int is_hypersparc; 90 91 static int srmmu_cache_pagetables; 92 93 /* these will be initialized in srmmu_nocache_calcsize() */ 94 static unsigned long srmmu_nocache_size; 95 static unsigned long srmmu_nocache_end; 96 97 /* 1 bit <=> 256 bytes of nocache <=> 64 PTEs */ 98 #define SRMMU_NOCACHE_BITMAP_SHIFT (PAGE_SHIFT - 4) 99 100 /* The context table is a nocache user with the biggest alignment needs. */ 101 #define SRMMU_NOCACHE_ALIGN_MAX (sizeof(ctxd_t)*SRMMU_MAX_CONTEXTS) 102 103 void *srmmu_nocache_pool; 104 static struct bit_map srmmu_nocache_map; 105 106 static inline int srmmu_pmd_none(pmd_t pmd) 107 { return !(pmd_val(pmd) & 0xFFFFFFF); } 108 109 /* XXX should we hyper_flush_whole_icache here - Anton */ 110 static inline void srmmu_ctxd_set(ctxd_t *ctxp, pgd_t *pgdp) 111 { 112 pte_t pte; 113 114 pte = __pte((SRMMU_ET_PTD | (__nocache_pa(pgdp) >> 4))); 115 set_pte((pte_t *)ctxp, pte); 116 } 117 118 /* 119 * Locations of MSI Registers. 120 */ 121 #define MSI_MBUS_ARBEN 0xe0001008 /* MBus Arbiter Enable register */ 122 123 /* 124 * Useful bits in the MSI Registers. 125 */ 126 #define MSI_ASYNC_MODE 0x80000000 /* Operate the MSI asynchronously */ 127 128 static void msi_set_sync(void) 129 { 130 __asm__ __volatile__ ("lda [%0] %1, %%g3\n\t" 131 "andn %%g3, %2, %%g3\n\t" 132 "sta %%g3, [%0] %1\n\t" : : 133 "r" (MSI_MBUS_ARBEN), 134 "i" (ASI_M_CTL), "r" (MSI_ASYNC_MODE) : "g3"); 135 } 136 137 void pmd_set(pmd_t *pmdp, pte_t *ptep) 138 { 139 unsigned long ptp = __nocache_pa(ptep) >> 4; 140 set_pte((pte_t *)&pmd_val(*pmdp), __pte(SRMMU_ET_PTD | ptp)); 141 } 142 143 /* 144 * size: bytes to allocate in the nocache area. 145 * align: bytes, number to align at. 146 * Returns the virtual address of the allocated area. 147 */ 148 static void *__srmmu_get_nocache(int size, int align) 149 { 150 int offset, minsz = 1 << SRMMU_NOCACHE_BITMAP_SHIFT; 151 unsigned long addr; 152 153 if (size < minsz) { 154 printk(KERN_ERR "Size 0x%x too small for nocache request\n", 155 size); 156 size = minsz; 157 } 158 if (size & (minsz - 1)) { 159 printk(KERN_ERR "Size 0x%x unaligned in nocache request\n", 160 size); 161 size += minsz - 1; 162 } 163 BUG_ON(align > SRMMU_NOCACHE_ALIGN_MAX); 164 165 offset = bit_map_string_get(&srmmu_nocache_map, 166 size >> SRMMU_NOCACHE_BITMAP_SHIFT, 167 align >> SRMMU_NOCACHE_BITMAP_SHIFT); 168 if (offset == -1) { 169 printk(KERN_ERR "srmmu: out of nocache %d: %d/%d\n", 170 size, (int) srmmu_nocache_size, 171 srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT); 172 return NULL; 173 } 174 175 addr = SRMMU_NOCACHE_VADDR + (offset << SRMMU_NOCACHE_BITMAP_SHIFT); 176 return (void *)addr; 177 } 178 179 void *srmmu_get_nocache(int size, int align) 180 { 181 void *tmp; 182 183 tmp = __srmmu_get_nocache(size, align); 184 185 if (tmp) 186 memset(tmp, 0, size); 187 188 return tmp; 189 } 190 191 void srmmu_free_nocache(void *addr, int size) 192 { 193 unsigned long vaddr; 194 int offset; 195 196 vaddr = (unsigned long)addr; 197 if (vaddr < SRMMU_NOCACHE_VADDR) { 198 printk("Vaddr %lx is smaller than nocache base 0x%lx\n", 199 vaddr, (unsigned long)SRMMU_NOCACHE_VADDR); 200 BUG(); 201 } 202 if (vaddr + size > srmmu_nocache_end) { 203 printk("Vaddr %lx is bigger than nocache end 0x%lx\n", 204 vaddr, srmmu_nocache_end); 205 BUG(); 206 } 207 if (!is_power_of_2(size)) { 208 printk("Size 0x%x is not a power of 2\n", size); 209 BUG(); 210 } 211 if (size < SRMMU_NOCACHE_BITMAP_SHIFT) { 212 printk("Size 0x%x is too small\n", size); 213 BUG(); 214 } 215 if (vaddr & (size - 1)) { 216 printk("Vaddr %lx is not aligned to size 0x%x\n", vaddr, size); 217 BUG(); 218 } 219 220 offset = (vaddr - SRMMU_NOCACHE_VADDR) >> SRMMU_NOCACHE_BITMAP_SHIFT; 221 size = size >> SRMMU_NOCACHE_BITMAP_SHIFT; 222 223 bit_map_clear(&srmmu_nocache_map, offset, size); 224 } 225 226 static void srmmu_early_allocate_ptable_skeleton(unsigned long start, 227 unsigned long end); 228 229 /* Return how much physical memory we have. */ 230 static unsigned long __init probe_memory(void) 231 { 232 unsigned long total = 0; 233 int i; 234 235 for (i = 0; sp_banks[i].num_bytes; i++) 236 total += sp_banks[i].num_bytes; 237 238 return total; 239 } 240 241 /* 242 * Reserve nocache dynamically proportionally to the amount of 243 * system RAM. -- Tomas Szepe <szepe@pinerecords.com>, June 2002 244 */ 245 static void __init srmmu_nocache_calcsize(void) 246 { 247 unsigned long sysmemavail = probe_memory() / 1024; 248 int srmmu_nocache_npages; 249 250 srmmu_nocache_npages = 251 sysmemavail / SRMMU_NOCACHE_ALCRATIO / 1024 * 256; 252 253 /* P3 XXX The 4x overuse: corroborated by /proc/meminfo. */ 254 // if (srmmu_nocache_npages < 256) srmmu_nocache_npages = 256; 255 if (srmmu_nocache_npages < SRMMU_MIN_NOCACHE_PAGES) 256 srmmu_nocache_npages = SRMMU_MIN_NOCACHE_PAGES; 257 258 /* anything above 1280 blows up */ 259 if (srmmu_nocache_npages > SRMMU_MAX_NOCACHE_PAGES) 260 srmmu_nocache_npages = SRMMU_MAX_NOCACHE_PAGES; 261 262 srmmu_nocache_size = srmmu_nocache_npages * PAGE_SIZE; 263 srmmu_nocache_end = SRMMU_NOCACHE_VADDR + srmmu_nocache_size; 264 } 265 266 static void __init srmmu_nocache_init(void) 267 { 268 void *srmmu_nocache_bitmap; 269 unsigned int bitmap_bits; 270 pgd_t *pgd; 271 p4d_t *p4d; 272 pud_t *pud; 273 pmd_t *pmd; 274 pte_t *pte; 275 unsigned long paddr, vaddr; 276 unsigned long pteval; 277 278 bitmap_bits = srmmu_nocache_size >> SRMMU_NOCACHE_BITMAP_SHIFT; 279 280 srmmu_nocache_pool = memblock_alloc_or_panic(srmmu_nocache_size, 281 SRMMU_NOCACHE_ALIGN_MAX); 282 memset(srmmu_nocache_pool, 0, srmmu_nocache_size); 283 284 srmmu_nocache_bitmap = 285 memblock_alloc_or_panic(BITS_TO_LONGS(bitmap_bits) * sizeof(long), 286 SMP_CACHE_BYTES); 287 bit_map_init(&srmmu_nocache_map, srmmu_nocache_bitmap, bitmap_bits); 288 289 srmmu_swapper_pg_dir = __srmmu_get_nocache(SRMMU_PGD_TABLE_SIZE, SRMMU_PGD_TABLE_SIZE); 290 memset(__nocache_fix(srmmu_swapper_pg_dir), 0, SRMMU_PGD_TABLE_SIZE); 291 init_mm.pgd = srmmu_swapper_pg_dir; 292 293 srmmu_early_allocate_ptable_skeleton(SRMMU_NOCACHE_VADDR, srmmu_nocache_end); 294 295 paddr = __pa((unsigned long)srmmu_nocache_pool); 296 vaddr = SRMMU_NOCACHE_VADDR; 297 298 while (vaddr < srmmu_nocache_end) { 299 pgd = pgd_offset_k(vaddr); 300 p4d = p4d_offset(pgd, vaddr); 301 pud = pud_offset(p4d, vaddr); 302 pmd = pmd_offset(__nocache_fix(pud), vaddr); 303 pte = pte_offset_kernel(__nocache_fix(pmd), vaddr); 304 305 pteval = ((paddr >> 4) | SRMMU_ET_PTE | SRMMU_PRIV); 306 307 if (srmmu_cache_pagetables) 308 pteval |= SRMMU_CACHE; 309 310 set_pte(__nocache_fix(pte), __pte(pteval)); 311 312 vaddr += PAGE_SIZE; 313 paddr += PAGE_SIZE; 314 } 315 316 flush_cache_all(); 317 flush_tlb_all(); 318 } 319 320 pgd_t *get_pgd_fast(void) 321 { 322 pgd_t *pgd = NULL; 323 324 pgd = __srmmu_get_nocache(SRMMU_PGD_TABLE_SIZE, SRMMU_PGD_TABLE_SIZE); 325 if (pgd) { 326 pgd_t *init = pgd_offset_k(0); 327 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); 328 memcpy(pgd + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD, 329 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); 330 } 331 332 return pgd; 333 } 334 335 /* 336 * Hardware needs alignment to 256 only, but we align to whole page size 337 * to reduce fragmentation problems due to the buddy principle. 338 * XXX Provide actual fragmentation statistics in /proc. 339 * 340 * Alignments up to the page size are the same for physical and virtual 341 * addresses of the nocache area. 342 */ 343 344 static DEFINE_SPINLOCK(pte_page_lock); 345 346 pgtable_t pte_alloc_one(struct mm_struct *mm) 347 { 348 unsigned long flags; 349 pte_t *ptep; 350 struct page *page; 351 352 if (!(ptep = pte_alloc_one_kernel(mm))) 353 return NULL; 354 page = pfn_to_page(__nocache_pa((unsigned long)ptep) >> PAGE_SHIFT); 355 spin_lock_irqsave(&pte_page_lock, flags); 356 if (page_ref_inc_return(page) == 2 && 357 !pagetable_pte_ctor(mm, page_ptdesc(page))) { 358 page_ref_dec(page); 359 ptep = NULL; 360 } 361 spin_unlock_irqrestore(&pte_page_lock, flags); 362 363 return ptep; 364 } 365 366 static void __pte_free(pgtable_t ptep) 367 { 368 struct page *page; 369 unsigned long flags; 370 371 page = pfn_to_page(__nocache_pa((unsigned long)ptep) >> PAGE_SHIFT); 372 spin_lock_irqsave(&pte_page_lock, flags); 373 if (page_ref_dec_return(page) == 1) 374 pagetable_dtor(page_ptdesc(page)); 375 spin_unlock_irqrestore(&pte_page_lock, flags); 376 377 srmmu_free_nocache(ptep, SRMMU_PTE_TABLE_SIZE); 378 } 379 380 void pte_free(struct mm_struct *mm, pgtable_t ptep) 381 { 382 __pte_free(ptep); 383 } 384 385 void __tlb_remove_table(void *table) 386 { 387 const unsigned long encoded = (unsigned long)table; 388 const unsigned long addr = encoded & ~1UL; 389 const bool is_pmd = encoded & 1; 390 391 if (is_pmd) 392 free_pmd_fast((pmd_t *)addr); 393 else 394 __pte_free((pgtable_t)addr); 395 } 396 397 /* context handling - a dynamically sized pool is used */ 398 #define NO_CONTEXT -1 399 400 struct ctx_list { 401 struct ctx_list *next; 402 struct ctx_list *prev; 403 unsigned int ctx_number; 404 struct mm_struct *ctx_mm; 405 }; 406 407 static struct ctx_list *ctx_list_pool; 408 static struct ctx_list ctx_free; 409 static struct ctx_list ctx_used; 410 411 /* At boot time we determine the number of contexts */ 412 static int num_contexts; 413 414 static inline void remove_from_ctx_list(struct ctx_list *entry) 415 { 416 entry->next->prev = entry->prev; 417 entry->prev->next = entry->next; 418 } 419 420 static inline void add_to_ctx_list(struct ctx_list *head, struct ctx_list *entry) 421 { 422 entry->next = head; 423 (entry->prev = head->prev)->next = entry; 424 head->prev = entry; 425 } 426 #define add_to_free_ctxlist(entry) add_to_ctx_list(&ctx_free, entry) 427 #define add_to_used_ctxlist(entry) add_to_ctx_list(&ctx_used, entry) 428 429 430 static inline void alloc_context(struct mm_struct *old_mm, struct mm_struct *mm) 431 { 432 struct ctx_list *ctxp; 433 434 ctxp = ctx_free.next; 435 if (ctxp != &ctx_free) { 436 remove_from_ctx_list(ctxp); 437 add_to_used_ctxlist(ctxp); 438 mm->context = ctxp->ctx_number; 439 ctxp->ctx_mm = mm; 440 return; 441 } 442 ctxp = ctx_used.next; 443 if (ctxp->ctx_mm == old_mm) 444 ctxp = ctxp->next; 445 if (ctxp == &ctx_used) 446 panic("out of mmu contexts"); 447 flush_cache_mm(ctxp->ctx_mm); 448 flush_tlb_mm(ctxp->ctx_mm); 449 remove_from_ctx_list(ctxp); 450 add_to_used_ctxlist(ctxp); 451 ctxp->ctx_mm->context = NO_CONTEXT; 452 ctxp->ctx_mm = mm; 453 mm->context = ctxp->ctx_number; 454 } 455 456 static inline void free_context(int context) 457 { 458 struct ctx_list *ctx_old; 459 460 ctx_old = ctx_list_pool + context; 461 remove_from_ctx_list(ctx_old); 462 add_to_free_ctxlist(ctx_old); 463 } 464 465 static void __init sparc_context_init(int numctx) 466 { 467 int ctx; 468 unsigned long size; 469 470 size = numctx * sizeof(struct ctx_list); 471 ctx_list_pool = memblock_alloc_or_panic(size, SMP_CACHE_BYTES); 472 473 for (ctx = 0; ctx < numctx; ctx++) { 474 struct ctx_list *clist; 475 476 clist = (ctx_list_pool + ctx); 477 clist->ctx_number = ctx; 478 clist->ctx_mm = NULL; 479 } 480 ctx_free.next = ctx_free.prev = &ctx_free; 481 ctx_used.next = ctx_used.prev = &ctx_used; 482 for (ctx = 0; ctx < numctx; ctx++) 483 add_to_free_ctxlist(ctx_list_pool + ctx); 484 } 485 486 void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm, 487 struct task_struct *tsk) 488 { 489 unsigned long flags; 490 491 if (mm->context == NO_CONTEXT) { 492 spin_lock_irqsave(&srmmu_context_spinlock, flags); 493 alloc_context(old_mm, mm); 494 spin_unlock_irqrestore(&srmmu_context_spinlock, flags); 495 srmmu_ctxd_set(&srmmu_context_table[mm->context], mm->pgd); 496 } 497 498 if (sparc_cpu_model == sparc_leon) 499 leon_switch_mm(); 500 501 if (is_hypersparc) 502 hyper_flush_whole_icache(); 503 504 srmmu_set_context(mm->context); 505 } 506 507 /* Low level IO area allocation on the SRMMU. */ 508 static inline void srmmu_mapioaddr(unsigned long physaddr, 509 unsigned long virt_addr, int bus_type) 510 { 511 pgd_t *pgdp; 512 p4d_t *p4dp; 513 pud_t *pudp; 514 pmd_t *pmdp; 515 pte_t *ptep; 516 unsigned long tmp; 517 518 physaddr &= PAGE_MASK; 519 pgdp = pgd_offset_k(virt_addr); 520 p4dp = p4d_offset(pgdp, virt_addr); 521 pudp = pud_offset(p4dp, virt_addr); 522 pmdp = pmd_offset(pudp, virt_addr); 523 ptep = pte_offset_kernel(pmdp, virt_addr); 524 tmp = (physaddr >> 4) | SRMMU_ET_PTE; 525 526 /* I need to test whether this is consistent over all 527 * sun4m's. The bus_type represents the upper 4 bits of 528 * 36-bit physical address on the I/O space lines... 529 */ 530 tmp |= (bus_type << 28); 531 tmp |= SRMMU_PRIV; 532 __flush_page_to_ram(virt_addr); 533 set_pte(ptep, __pte(tmp)); 534 } 535 536 void srmmu_mapiorange(unsigned int bus, unsigned long xpa, 537 unsigned long xva, unsigned int len) 538 { 539 while (len != 0) { 540 len -= PAGE_SIZE; 541 srmmu_mapioaddr(xpa, xva, bus); 542 xva += PAGE_SIZE; 543 xpa += PAGE_SIZE; 544 } 545 flush_tlb_all(); 546 } 547 548 static inline void srmmu_unmapioaddr(unsigned long virt_addr) 549 { 550 pgd_t *pgdp; 551 p4d_t *p4dp; 552 pud_t *pudp; 553 pmd_t *pmdp; 554 pte_t *ptep; 555 556 557 pgdp = pgd_offset_k(virt_addr); 558 p4dp = p4d_offset(pgdp, virt_addr); 559 pudp = pud_offset(p4dp, virt_addr); 560 pmdp = pmd_offset(pudp, virt_addr); 561 ptep = pte_offset_kernel(pmdp, virt_addr); 562 563 /* No need to flush uncacheable page. */ 564 __pte_clear(ptep); 565 } 566 567 void srmmu_unmapiorange(unsigned long virt_addr, unsigned int len) 568 { 569 while (len != 0) { 570 len -= PAGE_SIZE; 571 srmmu_unmapioaddr(virt_addr); 572 virt_addr += PAGE_SIZE; 573 } 574 flush_tlb_all(); 575 } 576 577 /* tsunami.S */ 578 extern void tsunami_flush_cache_all(void); 579 extern void tsunami_flush_cache_mm(struct mm_struct *mm); 580 extern void tsunami_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); 581 extern void tsunami_flush_cache_page(struct vm_area_struct *vma, unsigned long page); 582 extern void tsunami_flush_page_to_ram(unsigned long page); 583 extern void tsunami_flush_page_for_dma(unsigned long page); 584 extern void tsunami_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr); 585 extern void tsunami_flush_tlb_all(void); 586 extern void tsunami_flush_tlb_mm(struct mm_struct *mm); 587 extern void tsunami_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); 588 extern void tsunami_flush_tlb_page(struct vm_area_struct *vma, unsigned long page); 589 extern void tsunami_setup_blockops(void); 590 591 /* swift.S */ 592 extern void swift_flush_cache_all(void); 593 extern void swift_flush_cache_mm(struct mm_struct *mm); 594 extern void swift_flush_cache_range(struct vm_area_struct *vma, 595 unsigned long start, unsigned long end); 596 extern void swift_flush_cache_page(struct vm_area_struct *vma, unsigned long page); 597 extern void swift_flush_page_to_ram(unsigned long page); 598 extern void swift_flush_page_for_dma(unsigned long page); 599 extern void swift_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr); 600 extern void swift_flush_tlb_all(void); 601 extern void swift_flush_tlb_mm(struct mm_struct *mm); 602 extern void swift_flush_tlb_range(struct vm_area_struct *vma, 603 unsigned long start, unsigned long end); 604 extern void swift_flush_tlb_page(struct vm_area_struct *vma, unsigned long page); 605 606 607 /* 608 * The following are all MBUS based SRMMU modules, and therefore could 609 * be found in a multiprocessor configuration. On the whole, these 610 * chips seems to be much more touchy about DVMA and page tables 611 * with respect to cache coherency. 612 */ 613 614 /* viking.S */ 615 extern void viking_flush_cache_all(void); 616 extern void viking_flush_cache_mm(struct mm_struct *mm); 617 extern void viking_flush_cache_range(struct vm_area_struct *vma, unsigned long start, 618 unsigned long end); 619 extern void viking_flush_cache_page(struct vm_area_struct *vma, unsigned long page); 620 extern void viking_flush_page_to_ram(unsigned long page); 621 extern void viking_flush_page_for_dma(unsigned long page); 622 extern void viking_flush_sig_insns(struct mm_struct *mm, unsigned long addr); 623 extern void viking_flush_page(unsigned long page); 624 extern void viking_mxcc_flush_page(unsigned long page); 625 extern void viking_flush_tlb_all(void); 626 extern void viking_flush_tlb_mm(struct mm_struct *mm); 627 extern void viking_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, 628 unsigned long end); 629 extern void viking_flush_tlb_page(struct vm_area_struct *vma, 630 unsigned long page); 631 extern void sun4dsmp_flush_tlb_all(void); 632 extern void sun4dsmp_flush_tlb_mm(struct mm_struct *mm); 633 extern void sun4dsmp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, 634 unsigned long end); 635 extern void sun4dsmp_flush_tlb_page(struct vm_area_struct *vma, 636 unsigned long page); 637 638 /* hypersparc.S */ 639 extern void hypersparc_flush_cache_all(void); 640 extern void hypersparc_flush_cache_mm(struct mm_struct *mm); 641 extern void hypersparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); 642 extern void hypersparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page); 643 extern void hypersparc_flush_page_to_ram(unsigned long page); 644 extern void hypersparc_flush_page_for_dma(unsigned long page); 645 extern void hypersparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr); 646 extern void hypersparc_flush_tlb_all(void); 647 extern void hypersparc_flush_tlb_mm(struct mm_struct *mm); 648 extern void hypersparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); 649 extern void hypersparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page); 650 extern void hypersparc_setup_blockops(void); 651 652 /* 653 * NOTE: All of this startup code assumes the low 16mb (approx.) of 654 * kernel mappings are done with one single contiguous chunk of 655 * ram. On small ram machines (classics mainly) we only get 656 * around 8mb mapped for us. 657 */ 658 659 static void __init early_pgtable_allocfail(char *type) 660 { 661 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type); 662 prom_halt(); 663 } 664 665 static void __init srmmu_early_allocate_ptable_skeleton(unsigned long start, 666 unsigned long end) 667 { 668 pgd_t *pgdp; 669 p4d_t *p4dp; 670 pud_t *pudp; 671 pmd_t *pmdp; 672 pte_t *ptep; 673 674 while (start < end) { 675 pgdp = pgd_offset_k(start); 676 p4dp = p4d_offset(pgdp, start); 677 pudp = pud_offset(p4dp, start); 678 if (pud_none(*__nocache_fix(pudp))) { 679 pmdp = __srmmu_get_nocache( 680 SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE); 681 if (pmdp == NULL) 682 early_pgtable_allocfail("pmd"); 683 memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE); 684 pud_set(__nocache_fix(pudp), pmdp); 685 } 686 pmdp = pmd_offset(__nocache_fix(pudp), start); 687 if (srmmu_pmd_none(*__nocache_fix(pmdp))) { 688 ptep = __srmmu_get_nocache(PTE_SIZE, PTE_SIZE); 689 if (ptep == NULL) 690 early_pgtable_allocfail("pte"); 691 memset(__nocache_fix(ptep), 0, PTE_SIZE); 692 pmd_set(__nocache_fix(pmdp), ptep); 693 } 694 if (start > (0xffffffffUL - PMD_SIZE)) 695 break; 696 start = (start + PMD_SIZE) & PMD_MASK; 697 } 698 } 699 700 static void __init srmmu_allocate_ptable_skeleton(unsigned long start, 701 unsigned long end) 702 { 703 pgd_t *pgdp; 704 p4d_t *p4dp; 705 pud_t *pudp; 706 pmd_t *pmdp; 707 pte_t *ptep; 708 709 while (start < end) { 710 pgdp = pgd_offset_k(start); 711 p4dp = p4d_offset(pgdp, start); 712 pudp = pud_offset(p4dp, start); 713 if (pud_none(*pudp)) { 714 pmdp = __srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, SRMMU_PMD_TABLE_SIZE); 715 if (pmdp == NULL) 716 early_pgtable_allocfail("pmd"); 717 memset(pmdp, 0, SRMMU_PMD_TABLE_SIZE); 718 pud_set((pud_t *)pgdp, pmdp); 719 } 720 pmdp = pmd_offset(pudp, start); 721 if (srmmu_pmd_none(*pmdp)) { 722 ptep = __srmmu_get_nocache(PTE_SIZE, 723 PTE_SIZE); 724 if (ptep == NULL) 725 early_pgtable_allocfail("pte"); 726 memset(ptep, 0, PTE_SIZE); 727 pmd_set(pmdp, ptep); 728 } 729 if (start > (0xffffffffUL - PMD_SIZE)) 730 break; 731 start = (start + PMD_SIZE) & PMD_MASK; 732 } 733 } 734 735 /* These flush types are not available on all chips... */ 736 static inline unsigned long srmmu_probe(unsigned long vaddr) 737 { 738 unsigned long retval; 739 740 if (sparc_cpu_model != sparc_leon) { 741 742 vaddr &= PAGE_MASK; 743 __asm__ __volatile__("lda [%1] %2, %0\n\t" : 744 "=r" (retval) : 745 "r" (vaddr | 0x400), "i" (ASI_M_FLUSH_PROBE)); 746 } else { 747 retval = leon_swprobe(vaddr, NULL); 748 } 749 return retval; 750 } 751 752 /* 753 * This is much cleaner than poking around physical address space 754 * looking at the prom's page table directly which is what most 755 * other OS's do. Yuck... this is much better. 756 */ 757 static void __init srmmu_inherit_prom_mappings(unsigned long start, 758 unsigned long end) 759 { 760 unsigned long probed; 761 unsigned long addr; 762 pgd_t *pgdp; 763 p4d_t *p4dp; 764 pud_t *pudp; 765 pmd_t *pmdp; 766 pte_t *ptep; 767 int what; /* 0 = normal-pte, 1 = pmd-level pte, 2 = pgd-level pte */ 768 769 while (start <= end) { 770 if (start == 0) 771 break; /* probably wrap around */ 772 if (start == 0xfef00000) 773 start = KADB_DEBUGGER_BEGVM; 774 probed = srmmu_probe(start); 775 if (!probed) { 776 /* continue probing until we find an entry */ 777 start += PAGE_SIZE; 778 continue; 779 } 780 781 /* A red snapper, see what it really is. */ 782 what = 0; 783 addr = start - PAGE_SIZE; 784 785 if (!(start & ~(PMD_MASK))) { 786 if (srmmu_probe(addr + PMD_SIZE) == probed) 787 what = 1; 788 } 789 790 if (!(start & ~(PGDIR_MASK))) { 791 if (srmmu_probe(addr + PGDIR_SIZE) == probed) 792 what = 2; 793 } 794 795 pgdp = pgd_offset_k(start); 796 p4dp = p4d_offset(pgdp, start); 797 pudp = pud_offset(p4dp, start); 798 if (what == 2) { 799 *__nocache_fix(pgdp) = __pgd(probed); 800 start += PGDIR_SIZE; 801 continue; 802 } 803 if (pud_none(*__nocache_fix(pudp))) { 804 pmdp = __srmmu_get_nocache(SRMMU_PMD_TABLE_SIZE, 805 SRMMU_PMD_TABLE_SIZE); 806 if (pmdp == NULL) 807 early_pgtable_allocfail("pmd"); 808 memset(__nocache_fix(pmdp), 0, SRMMU_PMD_TABLE_SIZE); 809 pud_set(__nocache_fix(pudp), pmdp); 810 } 811 pmdp = pmd_offset(__nocache_fix(pudp), start); 812 if (what == 1) { 813 *(pmd_t *)__nocache_fix(pmdp) = __pmd(probed); 814 start += PMD_SIZE; 815 continue; 816 } 817 if (srmmu_pmd_none(*__nocache_fix(pmdp))) { 818 ptep = __srmmu_get_nocache(PTE_SIZE, PTE_SIZE); 819 if (ptep == NULL) 820 early_pgtable_allocfail("pte"); 821 memset(__nocache_fix(ptep), 0, PTE_SIZE); 822 pmd_set(__nocache_fix(pmdp), ptep); 823 } 824 ptep = pte_offset_kernel(__nocache_fix(pmdp), start); 825 *__nocache_fix(ptep) = __pte(probed); 826 start += PAGE_SIZE; 827 } 828 } 829 830 #define KERNEL_PTE(page_shifted) ((page_shifted)|SRMMU_CACHE|SRMMU_PRIV|SRMMU_VALID) 831 832 /* Create a third-level SRMMU 16MB page mapping. */ 833 static void __init do_large_mapping(unsigned long vaddr, unsigned long phys_base) 834 { 835 pgd_t *pgdp = pgd_offset_k(vaddr); 836 unsigned long big_pte; 837 838 big_pte = KERNEL_PTE(phys_base >> 4); 839 *__nocache_fix(pgdp) = __pgd(big_pte); 840 } 841 842 /* Map sp_bank entry SP_ENTRY, starting at virtual address VBASE. */ 843 static unsigned long __init map_spbank(unsigned long vbase, int sp_entry) 844 { 845 unsigned long pstart = (sp_banks[sp_entry].base_addr & PGDIR_MASK); 846 unsigned long vstart = (vbase & PGDIR_MASK); 847 unsigned long vend = PGDIR_ALIGN(vbase + sp_banks[sp_entry].num_bytes); 848 /* Map "low" memory only */ 849 const unsigned long min_vaddr = PAGE_OFFSET; 850 const unsigned long max_vaddr = PAGE_OFFSET + SRMMU_MAXMEM; 851 852 if (vstart < min_vaddr || vstart >= max_vaddr) 853 return vstart; 854 855 if (vend > max_vaddr || vend < min_vaddr) 856 vend = max_vaddr; 857 858 while (vstart < vend) { 859 do_large_mapping(vstart, pstart); 860 vstart += PGDIR_SIZE; pstart += PGDIR_SIZE; 861 } 862 return vstart; 863 } 864 865 static void __init map_kernel(void) 866 { 867 int i; 868 869 if (phys_base > 0) { 870 do_large_mapping(PAGE_OFFSET, phys_base); 871 } 872 873 for (i = 0; sp_banks[i].num_bytes != 0; i++) { 874 map_spbank((unsigned long)__va(sp_banks[i].base_addr), i); 875 } 876 } 877 878 void (*poke_srmmu)(void) = NULL; 879 880 void __init arch_zone_limits_init(unsigned long *max_zone_pfns) 881 { 882 max_zone_pfns[ZONE_DMA] = max_low_pfn; 883 max_zone_pfns[ZONE_NORMAL] = max_low_pfn; 884 max_zone_pfns[ZONE_HIGHMEM] = highend_pfn; 885 } 886 887 void __init srmmu_paging_init(void) 888 { 889 int i; 890 phandle cpunode; 891 char node_str[128]; 892 pgd_t *pgd; 893 p4d_t *p4d; 894 pud_t *pud; 895 pmd_t *pmd; 896 pte_t *pte; 897 unsigned long pages_avail; 898 899 init_mm.context = (unsigned long) NO_CONTEXT; 900 sparc_iomap.start = SUN4M_IOBASE_VADDR; /* 16MB of IOSPACE on all sun4m's. */ 901 902 if (sparc_cpu_model == sun4d) 903 num_contexts = 65536; /* We know it is Viking */ 904 else { 905 /* Find the number of contexts on the srmmu. */ 906 cpunode = prom_getchild(prom_root_node); 907 num_contexts = 0; 908 while (cpunode != 0) { 909 prom_getstring(cpunode, "device_type", node_str, sizeof(node_str)); 910 if (!strcmp(node_str, "cpu")) { 911 num_contexts = prom_getintdefault(cpunode, "mmu-nctx", 0x8); 912 break; 913 } 914 cpunode = prom_getsibling(cpunode); 915 } 916 } 917 918 if (!num_contexts) { 919 prom_printf("Something wrong, can't find cpu node in paging_init.\n"); 920 prom_halt(); 921 } 922 923 pages_avail = 0; 924 last_valid_pfn = bootmem_init(&pages_avail); 925 926 srmmu_nocache_calcsize(); 927 srmmu_nocache_init(); 928 srmmu_inherit_prom_mappings(0xfe400000, (LINUX_OPPROM_ENDVM - PAGE_SIZE)); 929 map_kernel(); 930 931 /* ctx table has to be physically aligned to its size */ 932 srmmu_context_table = __srmmu_get_nocache(num_contexts * sizeof(ctxd_t), num_contexts * sizeof(ctxd_t)); 933 srmmu_ctx_table_phys = (ctxd_t *)__nocache_pa(srmmu_context_table); 934 935 for (i = 0; i < num_contexts; i++) 936 srmmu_ctxd_set(__nocache_fix(&srmmu_context_table[i]), srmmu_swapper_pg_dir); 937 938 flush_cache_all(); 939 srmmu_set_ctable_ptr((unsigned long)srmmu_ctx_table_phys); 940 #ifdef CONFIG_SMP 941 /* Stop from hanging here... */ 942 local_ops->tlb_all(); 943 #else 944 flush_tlb_all(); 945 #endif 946 poke_srmmu(); 947 948 srmmu_allocate_ptable_skeleton(sparc_iomap.start, IOBASE_END); 949 srmmu_allocate_ptable_skeleton(DVMA_VADDR, DVMA_END); 950 951 srmmu_allocate_ptable_skeleton( 952 __fix_to_virt(__end_of_fixed_addresses - 1), FIXADDR_TOP); 953 srmmu_allocate_ptable_skeleton(PKMAP_BASE, PKMAP_END); 954 955 pgd = pgd_offset_k(PKMAP_BASE); 956 p4d = p4d_offset(pgd, PKMAP_BASE); 957 pud = pud_offset(p4d, PKMAP_BASE); 958 pmd = pmd_offset(pud, PKMAP_BASE); 959 pte = pte_offset_kernel(pmd, PKMAP_BASE); 960 pkmap_page_table = pte; 961 962 flush_cache_all(); 963 flush_tlb_all(); 964 965 sparc_context_init(num_contexts); 966 } 967 968 void mmu_info(struct seq_file *m) 969 { 970 seq_printf(m, 971 "MMU type\t: %s\n" 972 "contexts\t: %d\n" 973 "nocache total\t: %ld\n" 974 "nocache used\t: %d\n", 975 srmmu_name, 976 num_contexts, 977 srmmu_nocache_size, 978 srmmu_nocache_map.used << SRMMU_NOCACHE_BITMAP_SHIFT); 979 } 980 981 int init_new_context(struct task_struct *tsk, struct mm_struct *mm) 982 { 983 mm->context = NO_CONTEXT; 984 return 0; 985 } 986 987 void destroy_context(struct mm_struct *mm) 988 { 989 unsigned long flags; 990 991 if (mm->context != NO_CONTEXT) { 992 flush_cache_mm(mm); 993 srmmu_ctxd_set(&srmmu_context_table[mm->context], srmmu_swapper_pg_dir); 994 flush_tlb_mm(mm); 995 spin_lock_irqsave(&srmmu_context_spinlock, flags); 996 free_context(mm->context); 997 spin_unlock_irqrestore(&srmmu_context_spinlock, flags); 998 mm->context = NO_CONTEXT; 999 } 1000 } 1001 1002 /* Init various srmmu chip types. */ 1003 static void __init srmmu_is_bad(void) 1004 { 1005 prom_printf("Could not determine SRMMU chip type.\n"); 1006 prom_halt(); 1007 } 1008 1009 static void __init init_vac_layout(void) 1010 { 1011 phandle nd; 1012 int cache_lines; 1013 char node_str[128]; 1014 #ifdef CONFIG_SMP 1015 int cpu = 0; 1016 unsigned long max_size = 0; 1017 unsigned long min_line_size = 0x10000000; 1018 #endif 1019 1020 nd = prom_getchild(prom_root_node); 1021 while ((nd = prom_getsibling(nd)) != 0) { 1022 prom_getstring(nd, "device_type", node_str, sizeof(node_str)); 1023 if (!strcmp(node_str, "cpu")) { 1024 vac_line_size = prom_getint(nd, "cache-line-size"); 1025 if (vac_line_size == -1) { 1026 prom_printf("can't determine cache-line-size, halting.\n"); 1027 prom_halt(); 1028 } 1029 cache_lines = prom_getint(nd, "cache-nlines"); 1030 if (cache_lines == -1) { 1031 prom_printf("can't determine cache-nlines, halting.\n"); 1032 prom_halt(); 1033 } 1034 1035 vac_cache_size = cache_lines * vac_line_size; 1036 #ifdef CONFIG_SMP 1037 if (vac_cache_size > max_size) 1038 max_size = vac_cache_size; 1039 if (vac_line_size < min_line_size) 1040 min_line_size = vac_line_size; 1041 //FIXME: cpus not contiguous!! 1042 cpu++; 1043 if (cpu >= nr_cpu_ids || !cpu_online(cpu)) 1044 break; 1045 #else 1046 break; 1047 #endif 1048 } 1049 } 1050 if (nd == 0) { 1051 prom_printf("No CPU nodes found, halting.\n"); 1052 prom_halt(); 1053 } 1054 #ifdef CONFIG_SMP 1055 vac_cache_size = max_size; 1056 vac_line_size = min_line_size; 1057 #endif 1058 printk("SRMMU: Using VAC size of %d bytes, line size %d bytes.\n", 1059 (int)vac_cache_size, (int)vac_line_size); 1060 } 1061 1062 static void poke_hypersparc(void) 1063 { 1064 volatile unsigned long clear; 1065 unsigned long mreg = srmmu_get_mmureg(); 1066 1067 hyper_flush_unconditional_combined(); 1068 1069 mreg &= ~(HYPERSPARC_CWENABLE); 1070 mreg |= (HYPERSPARC_CENABLE | HYPERSPARC_WBENABLE); 1071 mreg |= (HYPERSPARC_CMODE); 1072 1073 srmmu_set_mmureg(mreg); 1074 1075 #if 0 /* XXX I think this is bad news... -DaveM */ 1076 hyper_clear_all_tags(); 1077 #endif 1078 1079 put_ross_icr(HYPERSPARC_ICCR_FTD | HYPERSPARC_ICCR_ICE); 1080 hyper_flush_whole_icache(); 1081 clear = srmmu_get_faddr(); 1082 clear = srmmu_get_fstatus(); 1083 } 1084 1085 static const struct sparc32_cachetlb_ops hypersparc_ops = { 1086 .cache_all = hypersparc_flush_cache_all, 1087 .cache_mm = hypersparc_flush_cache_mm, 1088 .cache_page = hypersparc_flush_cache_page, 1089 .cache_range = hypersparc_flush_cache_range, 1090 .tlb_all = hypersparc_flush_tlb_all, 1091 .tlb_mm = hypersparc_flush_tlb_mm, 1092 .tlb_page = hypersparc_flush_tlb_page, 1093 .tlb_range = hypersparc_flush_tlb_range, 1094 .page_to_ram = hypersparc_flush_page_to_ram, 1095 .sig_insns = hypersparc_flush_sig_insns, 1096 .page_for_dma = hypersparc_flush_page_for_dma, 1097 }; 1098 1099 static void __init init_hypersparc(void) 1100 { 1101 srmmu_name = "ROSS HyperSparc"; 1102 srmmu_modtype = HyperSparc; 1103 1104 init_vac_layout(); 1105 1106 is_hypersparc = 1; 1107 sparc32_cachetlb_ops = &hypersparc_ops; 1108 1109 poke_srmmu = poke_hypersparc; 1110 1111 hypersparc_setup_blockops(); 1112 } 1113 1114 static void poke_swift(void) 1115 { 1116 unsigned long mreg; 1117 1118 /* Clear any crap from the cache or else... */ 1119 swift_flush_cache_all(); 1120 1121 /* Enable I & D caches */ 1122 mreg = srmmu_get_mmureg(); 1123 mreg |= (SWIFT_IE | SWIFT_DE); 1124 /* 1125 * The Swift branch folding logic is completely broken. At 1126 * trap time, if things are just right, if can mistakenly 1127 * think that a trap is coming from kernel mode when in fact 1128 * it is coming from user mode (it mis-executes the branch in 1129 * the trap code). So you see things like crashme completely 1130 * hosing your machine which is completely unacceptable. Turn 1131 * this shit off... nice job Fujitsu. 1132 */ 1133 mreg &= ~(SWIFT_BF); 1134 srmmu_set_mmureg(mreg); 1135 } 1136 1137 static const struct sparc32_cachetlb_ops swift_ops = { 1138 .cache_all = swift_flush_cache_all, 1139 .cache_mm = swift_flush_cache_mm, 1140 .cache_page = swift_flush_cache_page, 1141 .cache_range = swift_flush_cache_range, 1142 .tlb_all = swift_flush_tlb_all, 1143 .tlb_mm = swift_flush_tlb_mm, 1144 .tlb_page = swift_flush_tlb_page, 1145 .tlb_range = swift_flush_tlb_range, 1146 .page_to_ram = swift_flush_page_to_ram, 1147 .sig_insns = swift_flush_sig_insns, 1148 .page_for_dma = swift_flush_page_for_dma, 1149 }; 1150 1151 #define SWIFT_MASKID_ADDR 0x10003018 1152 static void __init init_swift(void) 1153 { 1154 unsigned long swift_rev; 1155 1156 __asm__ __volatile__("lda [%1] %2, %0\n\t" 1157 "srl %0, 0x18, %0\n\t" : 1158 "=r" (swift_rev) : 1159 "r" (SWIFT_MASKID_ADDR), "i" (ASI_M_BYPASS)); 1160 srmmu_name = "Fujitsu Swift"; 1161 switch (swift_rev) { 1162 case 0x11: 1163 case 0x20: 1164 case 0x23: 1165 case 0x30: 1166 srmmu_modtype = Swift_lots_o_bugs; 1167 hwbug_bitmask |= (HWBUG_KERN_ACCBROKEN | HWBUG_KERN_CBITBROKEN); 1168 /* 1169 * Gee george, I wonder why Sun is so hush hush about 1170 * this hardware bug... really braindamage stuff going 1171 * on here. However I think we can find a way to avoid 1172 * all of the workaround overhead under Linux. Basically, 1173 * any page fault can cause kernel pages to become user 1174 * accessible (the mmu gets confused and clears some of 1175 * the ACC bits in kernel ptes). Aha, sounds pretty 1176 * horrible eh? But wait, after extensive testing it appears 1177 * that if you use pgd_t level large kernel pte's (like the 1178 * 4MB pages on the Pentium) the bug does not get tripped 1179 * at all. This avoids almost all of the major overhead. 1180 * Welcome to a world where your vendor tells you to, 1181 * "apply this kernel patch" instead of "sorry for the 1182 * broken hardware, send it back and we'll give you 1183 * properly functioning parts" 1184 */ 1185 break; 1186 case 0x25: 1187 case 0x31: 1188 srmmu_modtype = Swift_bad_c; 1189 hwbug_bitmask |= HWBUG_KERN_CBITBROKEN; 1190 /* 1191 * You see Sun allude to this hardware bug but never 1192 * admit things directly, they'll say things like, 1193 * "the Swift chip cache problems" or similar. 1194 */ 1195 break; 1196 default: 1197 srmmu_modtype = Swift_ok; 1198 break; 1199 } 1200 1201 sparc32_cachetlb_ops = &swift_ops; 1202 flush_page_for_dma_global = 0; 1203 1204 /* 1205 * Are you now convinced that the Swift is one of the 1206 * biggest VLSI abortions of all time? Bravo Fujitsu! 1207 * Fujitsu, the !#?!%$'d up processor people. I bet if 1208 * you examined the microcode of the Swift you'd find 1209 * XXX's all over the place. 1210 */ 1211 poke_srmmu = poke_swift; 1212 } 1213 1214 static void turbosparc_flush_cache_all(void) 1215 { 1216 flush_user_windows(); 1217 turbosparc_idflash_clear(); 1218 } 1219 1220 static void turbosparc_flush_cache_mm(struct mm_struct *mm) 1221 { 1222 FLUSH_BEGIN(mm) 1223 flush_user_windows(); 1224 turbosparc_idflash_clear(); 1225 FLUSH_END 1226 } 1227 1228 static void turbosparc_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) 1229 { 1230 FLUSH_BEGIN(vma->vm_mm) 1231 flush_user_windows(); 1232 turbosparc_idflash_clear(); 1233 FLUSH_END 1234 } 1235 1236 static void turbosparc_flush_cache_page(struct vm_area_struct *vma, unsigned long page) 1237 { 1238 FLUSH_BEGIN(vma->vm_mm) 1239 flush_user_windows(); 1240 if (vma->vm_flags & VM_EXEC) 1241 turbosparc_flush_icache(); 1242 turbosparc_flush_dcache(); 1243 FLUSH_END 1244 } 1245 1246 /* TurboSparc is copy-back, if we turn it on, but this does not work. */ 1247 static void turbosparc_flush_page_to_ram(unsigned long page) 1248 { 1249 #ifdef TURBOSPARC_WRITEBACK 1250 volatile unsigned long clear; 1251 1252 if (srmmu_probe(page)) 1253 turbosparc_flush_page_cache(page); 1254 clear = srmmu_get_fstatus(); 1255 #endif 1256 } 1257 1258 static void turbosparc_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr) 1259 { 1260 } 1261 1262 static void turbosparc_flush_page_for_dma(unsigned long page) 1263 { 1264 turbosparc_flush_dcache(); 1265 } 1266 1267 static void turbosparc_flush_tlb_all(void) 1268 { 1269 srmmu_flush_whole_tlb(); 1270 } 1271 1272 static void turbosparc_flush_tlb_mm(struct mm_struct *mm) 1273 { 1274 FLUSH_BEGIN(mm) 1275 srmmu_flush_whole_tlb(); 1276 FLUSH_END 1277 } 1278 1279 static void turbosparc_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) 1280 { 1281 FLUSH_BEGIN(vma->vm_mm) 1282 srmmu_flush_whole_tlb(); 1283 FLUSH_END 1284 } 1285 1286 static void turbosparc_flush_tlb_page(struct vm_area_struct *vma, unsigned long page) 1287 { 1288 FLUSH_BEGIN(vma->vm_mm) 1289 srmmu_flush_whole_tlb(); 1290 FLUSH_END 1291 } 1292 1293 1294 static void poke_turbosparc(void) 1295 { 1296 unsigned long mreg = srmmu_get_mmureg(); 1297 unsigned long ccreg; 1298 1299 /* Clear any crap from the cache or else... */ 1300 turbosparc_flush_cache_all(); 1301 /* Temporarily disable I & D caches */ 1302 mreg &= ~(TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); 1303 mreg &= ~(TURBOSPARC_PCENABLE); /* Don't check parity */ 1304 srmmu_set_mmureg(mreg); 1305 1306 ccreg = turbosparc_get_ccreg(); 1307 1308 #ifdef TURBOSPARC_WRITEBACK 1309 ccreg |= (TURBOSPARC_SNENABLE); /* Do DVMA snooping in Dcache */ 1310 ccreg &= ~(TURBOSPARC_uS2 | TURBOSPARC_WTENABLE); 1311 /* Write-back D-cache, emulate VLSI 1312 * abortion number three, not number one */ 1313 #else 1314 /* For now let's play safe, optimize later */ 1315 ccreg |= (TURBOSPARC_SNENABLE | TURBOSPARC_WTENABLE); 1316 /* Do DVMA snooping in Dcache, Write-thru D-cache */ 1317 ccreg &= ~(TURBOSPARC_uS2); 1318 /* Emulate VLSI abortion number three, not number one */ 1319 #endif 1320 1321 switch (ccreg & 7) { 1322 case 0: /* No SE cache */ 1323 case 7: /* Test mode */ 1324 break; 1325 default: 1326 ccreg |= (TURBOSPARC_SCENABLE); 1327 } 1328 turbosparc_set_ccreg(ccreg); 1329 1330 mreg |= (TURBOSPARC_ICENABLE | TURBOSPARC_DCENABLE); /* I & D caches on */ 1331 mreg |= (TURBOSPARC_ICSNOOP); /* Icache snooping on */ 1332 srmmu_set_mmureg(mreg); 1333 } 1334 1335 static const struct sparc32_cachetlb_ops turbosparc_ops = { 1336 .cache_all = turbosparc_flush_cache_all, 1337 .cache_mm = turbosparc_flush_cache_mm, 1338 .cache_page = turbosparc_flush_cache_page, 1339 .cache_range = turbosparc_flush_cache_range, 1340 .tlb_all = turbosparc_flush_tlb_all, 1341 .tlb_mm = turbosparc_flush_tlb_mm, 1342 .tlb_page = turbosparc_flush_tlb_page, 1343 .tlb_range = turbosparc_flush_tlb_range, 1344 .page_to_ram = turbosparc_flush_page_to_ram, 1345 .sig_insns = turbosparc_flush_sig_insns, 1346 .page_for_dma = turbosparc_flush_page_for_dma, 1347 }; 1348 1349 static void __init init_turbosparc(void) 1350 { 1351 srmmu_name = "Fujitsu TurboSparc"; 1352 srmmu_modtype = TurboSparc; 1353 sparc32_cachetlb_ops = &turbosparc_ops; 1354 poke_srmmu = poke_turbosparc; 1355 } 1356 1357 static void poke_tsunami(void) 1358 { 1359 unsigned long mreg = srmmu_get_mmureg(); 1360 1361 tsunami_flush_icache(); 1362 tsunami_flush_dcache(); 1363 mreg &= ~TSUNAMI_ITD; 1364 mreg |= (TSUNAMI_IENAB | TSUNAMI_DENAB); 1365 srmmu_set_mmureg(mreg); 1366 } 1367 1368 static const struct sparc32_cachetlb_ops tsunami_ops = { 1369 .cache_all = tsunami_flush_cache_all, 1370 .cache_mm = tsunami_flush_cache_mm, 1371 .cache_page = tsunami_flush_cache_page, 1372 .cache_range = tsunami_flush_cache_range, 1373 .tlb_all = tsunami_flush_tlb_all, 1374 .tlb_mm = tsunami_flush_tlb_mm, 1375 .tlb_page = tsunami_flush_tlb_page, 1376 .tlb_range = tsunami_flush_tlb_range, 1377 .page_to_ram = tsunami_flush_page_to_ram, 1378 .sig_insns = tsunami_flush_sig_insns, 1379 .page_for_dma = tsunami_flush_page_for_dma, 1380 }; 1381 1382 static void __init init_tsunami(void) 1383 { 1384 /* 1385 * Tsunami's pretty sane, Sun and TI actually got it 1386 * somewhat right this time. Fujitsu should have 1387 * taken some lessons from them. 1388 */ 1389 1390 srmmu_name = "TI Tsunami"; 1391 srmmu_modtype = Tsunami; 1392 sparc32_cachetlb_ops = &tsunami_ops; 1393 poke_srmmu = poke_tsunami; 1394 1395 tsunami_setup_blockops(); 1396 } 1397 1398 static void poke_viking(void) 1399 { 1400 unsigned long mreg = srmmu_get_mmureg(); 1401 static int smp_catch; 1402 1403 if (viking_mxcc_present) { 1404 unsigned long mxcc_control = mxcc_get_creg(); 1405 1406 mxcc_control |= (MXCC_CTL_ECE | MXCC_CTL_PRE | MXCC_CTL_MCE); 1407 mxcc_control &= ~(MXCC_CTL_RRC); 1408 mxcc_set_creg(mxcc_control); 1409 1410 /* 1411 * We don't need memory parity checks. 1412 * XXX This is a mess, have to dig out later. ecd. 1413 viking_mxcc_turn_off_parity(&mreg, &mxcc_control); 1414 */ 1415 1416 /* We do cache ptables on MXCC. */ 1417 mreg |= VIKING_TCENABLE; 1418 } else { 1419 unsigned long bpreg; 1420 1421 mreg &= ~(VIKING_TCENABLE); 1422 if (smp_catch++) { 1423 /* Must disable mixed-cmd mode here for other cpu's. */ 1424 bpreg = viking_get_bpreg(); 1425 bpreg &= ~(VIKING_ACTION_MIX); 1426 viking_set_bpreg(bpreg); 1427 1428 /* Just in case PROM does something funny. */ 1429 msi_set_sync(); 1430 } 1431 } 1432 1433 mreg |= VIKING_SPENABLE; 1434 mreg |= (VIKING_ICENABLE | VIKING_DCENABLE); 1435 mreg |= VIKING_SBENABLE; 1436 mreg &= ~(VIKING_ACENABLE); 1437 srmmu_set_mmureg(mreg); 1438 } 1439 1440 static struct sparc32_cachetlb_ops viking_ops __ro_after_init = { 1441 .cache_all = viking_flush_cache_all, 1442 .cache_mm = viking_flush_cache_mm, 1443 .cache_page = viking_flush_cache_page, 1444 .cache_range = viking_flush_cache_range, 1445 .tlb_all = viking_flush_tlb_all, 1446 .tlb_mm = viking_flush_tlb_mm, 1447 .tlb_page = viking_flush_tlb_page, 1448 .tlb_range = viking_flush_tlb_range, 1449 .page_to_ram = viking_flush_page_to_ram, 1450 .sig_insns = viking_flush_sig_insns, 1451 .page_for_dma = viking_flush_page_for_dma, 1452 }; 1453 1454 #ifdef CONFIG_SMP 1455 /* On sun4d the cpu broadcasts local TLB flushes, so we can just 1456 * perform the local TLB flush and all the other cpus will see it. 1457 * But, unfortunately, there is a bug in the sun4d XBUS backplane 1458 * that requires that we add some synchronization to these flushes. 1459 * 1460 * The bug is that the fifo which keeps track of all the pending TLB 1461 * broadcasts in the system is an entry or two too small, so if we 1462 * have too many going at once we'll overflow that fifo and lose a TLB 1463 * flush resulting in corruption. 1464 * 1465 * Our workaround is to take a global spinlock around the TLB flushes, 1466 * which guarentees we won't ever have too many pending. It's a big 1467 * hammer, but a semaphore like system to make sure we only have N TLB 1468 * flushes going at once will require SMP locking anyways so there's 1469 * no real value in trying any harder than this. 1470 */ 1471 static struct sparc32_cachetlb_ops viking_sun4d_smp_ops __ro_after_init = { 1472 .cache_all = viking_flush_cache_all, 1473 .cache_mm = viking_flush_cache_mm, 1474 .cache_page = viking_flush_cache_page, 1475 .cache_range = viking_flush_cache_range, 1476 .tlb_all = sun4dsmp_flush_tlb_all, 1477 .tlb_mm = sun4dsmp_flush_tlb_mm, 1478 .tlb_page = sun4dsmp_flush_tlb_page, 1479 .tlb_range = sun4dsmp_flush_tlb_range, 1480 .page_to_ram = viking_flush_page_to_ram, 1481 .sig_insns = viking_flush_sig_insns, 1482 .page_for_dma = viking_flush_page_for_dma, 1483 }; 1484 #endif 1485 1486 static void __init init_viking(void) 1487 { 1488 unsigned long mreg = srmmu_get_mmureg(); 1489 1490 /* Ahhh, the viking. SRMMU VLSI abortion number two... */ 1491 if (mreg & VIKING_MMODE) { 1492 srmmu_name = "TI Viking"; 1493 viking_mxcc_present = 0; 1494 msi_set_sync(); 1495 1496 /* 1497 * We need this to make sure old viking takes no hits 1498 * on its cache for dma snoops to workaround the 1499 * "load from non-cacheable memory" interrupt bug. 1500 * This is only necessary because of the new way in 1501 * which we use the IOMMU. 1502 */ 1503 viking_ops.page_for_dma = viking_flush_page; 1504 #ifdef CONFIG_SMP 1505 viking_sun4d_smp_ops.page_for_dma = viking_flush_page; 1506 #endif 1507 flush_page_for_dma_global = 0; 1508 } else { 1509 srmmu_name = "TI Viking/MXCC"; 1510 viking_mxcc_present = 1; 1511 srmmu_cache_pagetables = 1; 1512 } 1513 1514 sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *) 1515 &viking_ops; 1516 #ifdef CONFIG_SMP 1517 if (sparc_cpu_model == sun4d) 1518 sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *) 1519 &viking_sun4d_smp_ops; 1520 #endif 1521 1522 poke_srmmu = poke_viking; 1523 } 1524 1525 /* Probe for the srmmu chip version. */ 1526 static void __init get_srmmu_type(void) 1527 { 1528 unsigned long mreg, psr; 1529 unsigned long mod_typ, mod_rev, psr_typ, psr_vers; 1530 1531 srmmu_modtype = SRMMU_INVAL_MOD; 1532 hwbug_bitmask = 0; 1533 1534 mreg = srmmu_get_mmureg(); psr = get_psr(); 1535 mod_typ = (mreg & 0xf0000000) >> 28; 1536 mod_rev = (mreg & 0x0f000000) >> 24; 1537 psr_typ = (psr >> 28) & 0xf; 1538 psr_vers = (psr >> 24) & 0xf; 1539 1540 /* First, check for sparc-leon. */ 1541 if (sparc_cpu_model == sparc_leon) { 1542 init_leon(); 1543 return; 1544 } 1545 1546 /* Second, check for HyperSparc or Cypress. */ 1547 if (mod_typ == 1) { 1548 switch (mod_rev) { 1549 case 7: 1550 /* UP or MP Hypersparc */ 1551 init_hypersparc(); 1552 break; 1553 case 0: 1554 case 2: 1555 case 10: 1556 case 11: 1557 case 12: 1558 case 13: 1559 case 14: 1560 case 15: 1561 default: 1562 prom_printf("Sparc-Linux Cypress support does not longer exit.\n"); 1563 prom_halt(); 1564 break; 1565 } 1566 return; 1567 } 1568 1569 /* Now Fujitsu TurboSparc. It might happen that it is 1570 * in Swift emulation mode, so we will check later... 1571 */ 1572 if (psr_typ == 0 && psr_vers == 5) { 1573 init_turbosparc(); 1574 return; 1575 } 1576 1577 /* Next check for Fujitsu Swift. */ 1578 if (psr_typ == 0 && psr_vers == 4) { 1579 phandle cpunode; 1580 char node_str[128]; 1581 1582 /* Look if it is not a TurboSparc emulating Swift... */ 1583 cpunode = prom_getchild(prom_root_node); 1584 while ((cpunode = prom_getsibling(cpunode)) != 0) { 1585 prom_getstring(cpunode, "device_type", node_str, sizeof(node_str)); 1586 if (!strcmp(node_str, "cpu")) { 1587 if (!prom_getintdefault(cpunode, "psr-implementation", 1) && 1588 prom_getintdefault(cpunode, "psr-version", 1) == 5) { 1589 init_turbosparc(); 1590 return; 1591 } 1592 break; 1593 } 1594 } 1595 1596 init_swift(); 1597 return; 1598 } 1599 1600 /* Now the Viking family of srmmu. */ 1601 if (psr_typ == 4 && 1602 ((psr_vers == 0) || 1603 ((psr_vers == 1) && (mod_typ == 0) && (mod_rev == 0)))) { 1604 init_viking(); 1605 return; 1606 } 1607 1608 /* Finally the Tsunami. */ 1609 if (psr_typ == 4 && psr_vers == 1 && (mod_typ || mod_rev)) { 1610 init_tsunami(); 1611 return; 1612 } 1613 1614 /* Oh well */ 1615 srmmu_is_bad(); 1616 } 1617 1618 #ifdef CONFIG_SMP 1619 /* Local cross-calls. */ 1620 static void smp_flush_page_for_dma(unsigned long page) 1621 { 1622 xc1(local_ops->page_for_dma, page); 1623 local_ops->page_for_dma(page); 1624 } 1625 1626 static void smp_flush_cache_all(void) 1627 { 1628 xc0(local_ops->cache_all); 1629 local_ops->cache_all(); 1630 } 1631 1632 static void smp_flush_tlb_all(void) 1633 { 1634 xc0(local_ops->tlb_all); 1635 local_ops->tlb_all(); 1636 } 1637 1638 static bool any_other_mm_cpus(struct mm_struct *mm) 1639 { 1640 return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids; 1641 } 1642 1643 static void smp_flush_cache_mm(struct mm_struct *mm) 1644 { 1645 if (mm->context != NO_CONTEXT) { 1646 if (any_other_mm_cpus(mm)) 1647 xc1(local_ops->cache_mm, (unsigned long)mm); 1648 local_ops->cache_mm(mm); 1649 } 1650 } 1651 1652 static void smp_flush_tlb_mm(struct mm_struct *mm) 1653 { 1654 if (mm->context != NO_CONTEXT) { 1655 if (any_other_mm_cpus(mm)) { 1656 xc1(local_ops->tlb_mm, (unsigned long)mm); 1657 if (atomic_read(&mm->mm_users) == 1 && current->active_mm == mm) 1658 cpumask_copy(mm_cpumask(mm), 1659 cpumask_of(smp_processor_id())); 1660 } 1661 local_ops->tlb_mm(mm); 1662 } 1663 } 1664 1665 static void smp_flush_cache_range(struct vm_area_struct *vma, 1666 unsigned long start, 1667 unsigned long end) 1668 { 1669 struct mm_struct *mm = vma->vm_mm; 1670 1671 if (mm->context != NO_CONTEXT) { 1672 if (any_other_mm_cpus(mm)) 1673 xc3(local_ops->cache_range, (unsigned long)vma, start, 1674 end); 1675 local_ops->cache_range(vma, start, end); 1676 } 1677 } 1678 1679 static void smp_flush_tlb_range(struct vm_area_struct *vma, 1680 unsigned long start, 1681 unsigned long end) 1682 { 1683 struct mm_struct *mm = vma->vm_mm; 1684 1685 if (mm->context != NO_CONTEXT) { 1686 if (any_other_mm_cpus(mm)) 1687 xc3(local_ops->tlb_range, (unsigned long)vma, start, 1688 end); 1689 local_ops->tlb_range(vma, start, end); 1690 } 1691 } 1692 1693 static void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page) 1694 { 1695 struct mm_struct *mm = vma->vm_mm; 1696 1697 if (mm->context != NO_CONTEXT) { 1698 if (any_other_mm_cpus(mm)) 1699 xc2(local_ops->cache_page, (unsigned long)vma, page); 1700 local_ops->cache_page(vma, page); 1701 } 1702 } 1703 1704 static void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page) 1705 { 1706 struct mm_struct *mm = vma->vm_mm; 1707 1708 if (mm->context != NO_CONTEXT) { 1709 if (any_other_mm_cpus(mm)) 1710 xc2(local_ops->tlb_page, (unsigned long)vma, page); 1711 local_ops->tlb_page(vma, page); 1712 } 1713 } 1714 1715 static void smp_flush_page_to_ram(unsigned long page) 1716 { 1717 /* Current theory is that those who call this are the one's 1718 * who have just dirtied their cache with the pages contents 1719 * in kernel space, therefore we only run this on local cpu. 1720 * 1721 * XXX This experiment failed, research further... -DaveM 1722 */ 1723 #if 1 1724 xc1(local_ops->page_to_ram, page); 1725 #endif 1726 local_ops->page_to_ram(page); 1727 } 1728 1729 static void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr) 1730 { 1731 if (any_other_mm_cpus(mm)) 1732 xc2(local_ops->sig_insns, (unsigned long)mm, insn_addr); 1733 local_ops->sig_insns(mm, insn_addr); 1734 } 1735 1736 static struct sparc32_cachetlb_ops smp_cachetlb_ops __ro_after_init = { 1737 .cache_all = smp_flush_cache_all, 1738 .cache_mm = smp_flush_cache_mm, 1739 .cache_page = smp_flush_cache_page, 1740 .cache_range = smp_flush_cache_range, 1741 .tlb_all = smp_flush_tlb_all, 1742 .tlb_mm = smp_flush_tlb_mm, 1743 .tlb_page = smp_flush_tlb_page, 1744 .tlb_range = smp_flush_tlb_range, 1745 .page_to_ram = smp_flush_page_to_ram, 1746 .sig_insns = smp_flush_sig_insns, 1747 .page_for_dma = smp_flush_page_for_dma, 1748 }; 1749 #endif 1750 1751 /* Load up routines and constants for sun4m and sun4d mmu */ 1752 void __init load_mmu(void) 1753 { 1754 /* Functions */ 1755 get_srmmu_type(); 1756 1757 #ifdef CONFIG_SMP 1758 /* El switcheroo... */ 1759 local_ops = sparc32_cachetlb_ops; 1760 1761 if (sparc_cpu_model == sun4d || sparc_cpu_model == sparc_leon) { 1762 smp_cachetlb_ops.tlb_all = local_ops->tlb_all; 1763 smp_cachetlb_ops.tlb_mm = local_ops->tlb_mm; 1764 smp_cachetlb_ops.tlb_range = local_ops->tlb_range; 1765 smp_cachetlb_ops.tlb_page = local_ops->tlb_page; 1766 } 1767 1768 if (poke_srmmu == poke_viking) { 1769 /* Avoid unnecessary cross calls. */ 1770 smp_cachetlb_ops.cache_all = local_ops->cache_all; 1771 smp_cachetlb_ops.cache_mm = local_ops->cache_mm; 1772 smp_cachetlb_ops.cache_range = local_ops->cache_range; 1773 smp_cachetlb_ops.cache_page = local_ops->cache_page; 1774 1775 smp_cachetlb_ops.page_to_ram = local_ops->page_to_ram; 1776 smp_cachetlb_ops.sig_insns = local_ops->sig_insns; 1777 smp_cachetlb_ops.page_for_dma = local_ops->page_for_dma; 1778 } 1779 1780 /* It really is const after this point. */ 1781 sparc32_cachetlb_ops = (const struct sparc32_cachetlb_ops *) 1782 &smp_cachetlb_ops; 1783 #endif 1784 1785 if (sparc_cpu_model != sun4d) 1786 ld_mmu_iommu(); 1787 #ifdef CONFIG_SMP 1788 if (sparc_cpu_model == sun4d) 1789 sun4d_init_smp(); 1790 else if (sparc_cpu_model == sparc_leon) 1791 leon_init_smp(); 1792 else 1793 sun4m_init_smp(); 1794 #endif 1795 } 1796