18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * r2300.c: R2000 and R3000 specific mmu/cache code. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1996 David S. Miller (davem@davemloft.net) 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * with a lot of changes to make this thing work for R3000s 88c2ecf20Sopenharmony_ci * Tx39XX R4k style caches added. HK 98c2ecf20Sopenharmony_ci * Copyright (C) 1998, 1999, 2000 Harald Koerfgen 108c2ecf20Sopenharmony_ci * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov 118c2ecf20Sopenharmony_ci * Copyright (C) 2001, 2004, 2007 Maciej W. Rozycki 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci#include <linux/kernel.h> 148c2ecf20Sopenharmony_ci#include <linux/sched.h> 158c2ecf20Sopenharmony_ci#include <linux/smp.h> 168c2ecf20Sopenharmony_ci#include <linux/mm.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <asm/page.h> 198c2ecf20Sopenharmony_ci#include <asm/mmu_context.h> 208c2ecf20Sopenharmony_ci#include <asm/isadep.h> 218c2ecf20Sopenharmony_ci#include <asm/io.h> 228c2ecf20Sopenharmony_ci#include <asm/bootinfo.h> 238c2ecf20Sopenharmony_ci#include <asm/cpu.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic unsigned long icache_size, dcache_size; /* Size in bytes */ 268c2ecf20Sopenharmony_cistatic unsigned long icache_lsize, dcache_lsize; /* Size in bytes */ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ciunsigned long r3k_cache_size(unsigned long ca_flags) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci unsigned long flags, status, dummy, size; 318c2ecf20Sopenharmony_ci volatile unsigned long *p; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci p = (volatile unsigned long *) KSEG0; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci flags = read_c0_status(); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* isolate cache space */ 388c2ecf20Sopenharmony_ci write_c0_status((ca_flags|flags)&~ST0_IEC); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci *p = 0xa5a55a5a; 418c2ecf20Sopenharmony_ci dummy = *p; 428c2ecf20Sopenharmony_ci status = read_c0_status(); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci if (dummy != 0xa5a55a5a || (status & ST0_CM)) { 458c2ecf20Sopenharmony_ci size = 0; 468c2ecf20Sopenharmony_ci } else { 478c2ecf20Sopenharmony_ci for (size = 128; size <= 0x40000; size <<= 1) 488c2ecf20Sopenharmony_ci *(p + size) = 0; 498c2ecf20Sopenharmony_ci *p = -1; 508c2ecf20Sopenharmony_ci for (size = 128; 518c2ecf20Sopenharmony_ci (size <= 0x40000) && (*(p + size) == 0); 528c2ecf20Sopenharmony_ci size <<= 1) 538c2ecf20Sopenharmony_ci ; 548c2ecf20Sopenharmony_ci if (size > 0x40000) 558c2ecf20Sopenharmony_ci size = 0; 568c2ecf20Sopenharmony_ci } 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci write_c0_status(flags); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci return size * sizeof(*p); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciunsigned long r3k_cache_lsize(unsigned long ca_flags) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci unsigned long flags, status, lsize, i; 668c2ecf20Sopenharmony_ci volatile unsigned long *p; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci p = (volatile unsigned long *) KSEG0; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci flags = read_c0_status(); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* isolate cache space */ 738c2ecf20Sopenharmony_ci write_c0_status((ca_flags|flags)&~ST0_IEC); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci for (i = 0; i < 128; i++) 768c2ecf20Sopenharmony_ci *(p + i) = 0; 778c2ecf20Sopenharmony_ci *(volatile unsigned char *)p = 0; 788c2ecf20Sopenharmony_ci for (lsize = 1; lsize < 128; lsize <<= 1) { 798c2ecf20Sopenharmony_ci *(p + lsize); 808c2ecf20Sopenharmony_ci status = read_c0_status(); 818c2ecf20Sopenharmony_ci if (!(status & ST0_CM)) 828c2ecf20Sopenharmony_ci break; 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci for (i = 0; i < 128; i += lsize) 858c2ecf20Sopenharmony_ci *(volatile unsigned char *)(p + i) = 0; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci write_c0_status(flags); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci return lsize * sizeof(*p); 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic void r3k_probe_cache(void) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci dcache_size = r3k_cache_size(ST0_ISC); 958c2ecf20Sopenharmony_ci if (dcache_size) 968c2ecf20Sopenharmony_ci dcache_lsize = r3k_cache_lsize(ST0_ISC); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci icache_size = r3k_cache_size(ST0_ISC|ST0_SWC); 998c2ecf20Sopenharmony_ci if (icache_size) 1008c2ecf20Sopenharmony_ci icache_lsize = r3k_cache_lsize(ST0_ISC|ST0_SWC); 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic void r3k_flush_icache_range(unsigned long start, unsigned long end) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci unsigned long size, i, flags; 1068c2ecf20Sopenharmony_ci volatile unsigned char *p; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci size = end - start; 1098c2ecf20Sopenharmony_ci if (size > icache_size || KSEGX(start) != KSEG0) { 1108c2ecf20Sopenharmony_ci start = KSEG0; 1118c2ecf20Sopenharmony_ci size = icache_size; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci p = (char *)start; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci flags = read_c0_status(); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* isolate cache space */ 1188c2ecf20Sopenharmony_ci write_c0_status((ST0_ISC|ST0_SWC|flags)&~ST0_IEC); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci for (i = 0; i < size; i += 0x080) { 1218c2ecf20Sopenharmony_ci asm( "sb\t$0, 0x000(%0)\n\t" 1228c2ecf20Sopenharmony_ci "sb\t$0, 0x004(%0)\n\t" 1238c2ecf20Sopenharmony_ci "sb\t$0, 0x008(%0)\n\t" 1248c2ecf20Sopenharmony_ci "sb\t$0, 0x00c(%0)\n\t" 1258c2ecf20Sopenharmony_ci "sb\t$0, 0x010(%0)\n\t" 1268c2ecf20Sopenharmony_ci "sb\t$0, 0x014(%0)\n\t" 1278c2ecf20Sopenharmony_ci "sb\t$0, 0x018(%0)\n\t" 1288c2ecf20Sopenharmony_ci "sb\t$0, 0x01c(%0)\n\t" 1298c2ecf20Sopenharmony_ci "sb\t$0, 0x020(%0)\n\t" 1308c2ecf20Sopenharmony_ci "sb\t$0, 0x024(%0)\n\t" 1318c2ecf20Sopenharmony_ci "sb\t$0, 0x028(%0)\n\t" 1328c2ecf20Sopenharmony_ci "sb\t$0, 0x02c(%0)\n\t" 1338c2ecf20Sopenharmony_ci "sb\t$0, 0x030(%0)\n\t" 1348c2ecf20Sopenharmony_ci "sb\t$0, 0x034(%0)\n\t" 1358c2ecf20Sopenharmony_ci "sb\t$0, 0x038(%0)\n\t" 1368c2ecf20Sopenharmony_ci "sb\t$0, 0x03c(%0)\n\t" 1378c2ecf20Sopenharmony_ci "sb\t$0, 0x040(%0)\n\t" 1388c2ecf20Sopenharmony_ci "sb\t$0, 0x044(%0)\n\t" 1398c2ecf20Sopenharmony_ci "sb\t$0, 0x048(%0)\n\t" 1408c2ecf20Sopenharmony_ci "sb\t$0, 0x04c(%0)\n\t" 1418c2ecf20Sopenharmony_ci "sb\t$0, 0x050(%0)\n\t" 1428c2ecf20Sopenharmony_ci "sb\t$0, 0x054(%0)\n\t" 1438c2ecf20Sopenharmony_ci "sb\t$0, 0x058(%0)\n\t" 1448c2ecf20Sopenharmony_ci "sb\t$0, 0x05c(%0)\n\t" 1458c2ecf20Sopenharmony_ci "sb\t$0, 0x060(%0)\n\t" 1468c2ecf20Sopenharmony_ci "sb\t$0, 0x064(%0)\n\t" 1478c2ecf20Sopenharmony_ci "sb\t$0, 0x068(%0)\n\t" 1488c2ecf20Sopenharmony_ci "sb\t$0, 0x06c(%0)\n\t" 1498c2ecf20Sopenharmony_ci "sb\t$0, 0x070(%0)\n\t" 1508c2ecf20Sopenharmony_ci "sb\t$0, 0x074(%0)\n\t" 1518c2ecf20Sopenharmony_ci "sb\t$0, 0x078(%0)\n\t" 1528c2ecf20Sopenharmony_ci "sb\t$0, 0x07c(%0)\n\t" 1538c2ecf20Sopenharmony_ci : : "r" (p) ); 1548c2ecf20Sopenharmony_ci p += 0x080; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci write_c0_status(flags); 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cistatic void r3k_flush_dcache_range(unsigned long start, unsigned long end) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci unsigned long size, i, flags; 1638c2ecf20Sopenharmony_ci volatile unsigned char *p; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci size = end - start; 1668c2ecf20Sopenharmony_ci if (size > dcache_size || KSEGX(start) != KSEG0) { 1678c2ecf20Sopenharmony_ci start = KSEG0; 1688c2ecf20Sopenharmony_ci size = dcache_size; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci p = (char *)start; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci flags = read_c0_status(); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci /* isolate cache space */ 1758c2ecf20Sopenharmony_ci write_c0_status((ST0_ISC|flags)&~ST0_IEC); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci for (i = 0; i < size; i += 0x080) { 1788c2ecf20Sopenharmony_ci asm( "sb\t$0, 0x000(%0)\n\t" 1798c2ecf20Sopenharmony_ci "sb\t$0, 0x004(%0)\n\t" 1808c2ecf20Sopenharmony_ci "sb\t$0, 0x008(%0)\n\t" 1818c2ecf20Sopenharmony_ci "sb\t$0, 0x00c(%0)\n\t" 1828c2ecf20Sopenharmony_ci "sb\t$0, 0x010(%0)\n\t" 1838c2ecf20Sopenharmony_ci "sb\t$0, 0x014(%0)\n\t" 1848c2ecf20Sopenharmony_ci "sb\t$0, 0x018(%0)\n\t" 1858c2ecf20Sopenharmony_ci "sb\t$0, 0x01c(%0)\n\t" 1868c2ecf20Sopenharmony_ci "sb\t$0, 0x020(%0)\n\t" 1878c2ecf20Sopenharmony_ci "sb\t$0, 0x024(%0)\n\t" 1888c2ecf20Sopenharmony_ci "sb\t$0, 0x028(%0)\n\t" 1898c2ecf20Sopenharmony_ci "sb\t$0, 0x02c(%0)\n\t" 1908c2ecf20Sopenharmony_ci "sb\t$0, 0x030(%0)\n\t" 1918c2ecf20Sopenharmony_ci "sb\t$0, 0x034(%0)\n\t" 1928c2ecf20Sopenharmony_ci "sb\t$0, 0x038(%0)\n\t" 1938c2ecf20Sopenharmony_ci "sb\t$0, 0x03c(%0)\n\t" 1948c2ecf20Sopenharmony_ci "sb\t$0, 0x040(%0)\n\t" 1958c2ecf20Sopenharmony_ci "sb\t$0, 0x044(%0)\n\t" 1968c2ecf20Sopenharmony_ci "sb\t$0, 0x048(%0)\n\t" 1978c2ecf20Sopenharmony_ci "sb\t$0, 0x04c(%0)\n\t" 1988c2ecf20Sopenharmony_ci "sb\t$0, 0x050(%0)\n\t" 1998c2ecf20Sopenharmony_ci "sb\t$0, 0x054(%0)\n\t" 2008c2ecf20Sopenharmony_ci "sb\t$0, 0x058(%0)\n\t" 2018c2ecf20Sopenharmony_ci "sb\t$0, 0x05c(%0)\n\t" 2028c2ecf20Sopenharmony_ci "sb\t$0, 0x060(%0)\n\t" 2038c2ecf20Sopenharmony_ci "sb\t$0, 0x064(%0)\n\t" 2048c2ecf20Sopenharmony_ci "sb\t$0, 0x068(%0)\n\t" 2058c2ecf20Sopenharmony_ci "sb\t$0, 0x06c(%0)\n\t" 2068c2ecf20Sopenharmony_ci "sb\t$0, 0x070(%0)\n\t" 2078c2ecf20Sopenharmony_ci "sb\t$0, 0x074(%0)\n\t" 2088c2ecf20Sopenharmony_ci "sb\t$0, 0x078(%0)\n\t" 2098c2ecf20Sopenharmony_ci "sb\t$0, 0x07c(%0)\n\t" 2108c2ecf20Sopenharmony_ci : : "r" (p) ); 2118c2ecf20Sopenharmony_ci p += 0x080; 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci write_c0_status(flags); 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic inline void r3k_flush_cache_all(void) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic inline void r3k___flush_cache_all(void) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci r3k_flush_dcache_range(KSEG0, KSEG0 + dcache_size); 2248c2ecf20Sopenharmony_ci r3k_flush_icache_range(KSEG0, KSEG0 + icache_size); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic void r3k_flush_cache_mm(struct mm_struct *mm) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cistatic void r3k_flush_cache_range(struct vm_area_struct *vma, 2328c2ecf20Sopenharmony_ci unsigned long start, unsigned long end) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic void r3k_flush_cache_page(struct vm_area_struct *vma, 2378c2ecf20Sopenharmony_ci unsigned long addr, unsigned long pfn) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci unsigned long kaddr = KSEG0ADDR(pfn << PAGE_SHIFT); 2408c2ecf20Sopenharmony_ci int exec = vma->vm_flags & VM_EXEC; 2418c2ecf20Sopenharmony_ci struct mm_struct *mm = vma->vm_mm; 2428c2ecf20Sopenharmony_ci pmd_t *pmdp; 2438c2ecf20Sopenharmony_ci pte_t *ptep; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci pr_debug("cpage[%08llx,%08lx]\n", 2468c2ecf20Sopenharmony_ci cpu_context(smp_processor_id(), mm), addr); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci /* No ASID => no such page in the cache. */ 2498c2ecf20Sopenharmony_ci if (cpu_context(smp_processor_id(), mm) == 0) 2508c2ecf20Sopenharmony_ci return; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci pmdp = pmd_off(mm, addr); 2538c2ecf20Sopenharmony_ci ptep = pte_offset_kernel(pmdp, addr); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci /* Invalid => no such page in the cache. */ 2568c2ecf20Sopenharmony_ci if (!(pte_val(*ptep) & _PAGE_PRESENT)) 2578c2ecf20Sopenharmony_ci return; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci r3k_flush_dcache_range(kaddr, kaddr + PAGE_SIZE); 2608c2ecf20Sopenharmony_ci if (exec) 2618c2ecf20Sopenharmony_ci r3k_flush_icache_range(kaddr, kaddr + PAGE_SIZE); 2628c2ecf20Sopenharmony_ci} 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_cistatic void local_r3k_flush_data_cache_page(void *addr) 2658c2ecf20Sopenharmony_ci{ 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic void r3k_flush_data_cache_page(unsigned long addr) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci} 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic void r3k_flush_kernel_vmap_range(unsigned long vaddr, int size) 2738c2ecf20Sopenharmony_ci{ 2748c2ecf20Sopenharmony_ci BUG(); 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_cistatic void r3k_dma_cache_wback_inv(unsigned long start, unsigned long size) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci /* Catch bad driver code */ 2808c2ecf20Sopenharmony_ci BUG_ON(size == 0); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci iob(); 2838c2ecf20Sopenharmony_ci r3k_flush_dcache_range(start, start + size); 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_civoid r3k_cache_init(void) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci extern void build_clear_page(void); 2898c2ecf20Sopenharmony_ci extern void build_copy_page(void); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci r3k_probe_cache(); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci flush_cache_all = r3k_flush_cache_all; 2948c2ecf20Sopenharmony_ci __flush_cache_all = r3k___flush_cache_all; 2958c2ecf20Sopenharmony_ci flush_cache_mm = r3k_flush_cache_mm; 2968c2ecf20Sopenharmony_ci flush_cache_range = r3k_flush_cache_range; 2978c2ecf20Sopenharmony_ci flush_cache_page = r3k_flush_cache_page; 2988c2ecf20Sopenharmony_ci flush_icache_range = r3k_flush_icache_range; 2998c2ecf20Sopenharmony_ci local_flush_icache_range = r3k_flush_icache_range; 3008c2ecf20Sopenharmony_ci __flush_icache_user_range = r3k_flush_icache_range; 3018c2ecf20Sopenharmony_ci __local_flush_icache_user_range = r3k_flush_icache_range; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci __flush_kernel_vmap_range = r3k_flush_kernel_vmap_range; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci local_flush_data_cache_page = local_r3k_flush_data_cache_page; 3068c2ecf20Sopenharmony_ci flush_data_cache_page = r3k_flush_data_cache_page; 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci _dma_cache_wback_inv = r3k_dma_cache_wback_inv; 3098c2ecf20Sopenharmony_ci _dma_cache_wback = r3k_dma_cache_wback_inv; 3108c2ecf20Sopenharmony_ci _dma_cache_inv = r3k_dma_cache_wback_inv; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci pr_info("Primary instruction cache %ldkB, linesize %ld bytes.\n", 3138c2ecf20Sopenharmony_ci icache_size >> 10, icache_lsize); 3148c2ecf20Sopenharmony_ci pr_info("Primary data cache %ldkB, linesize %ld bytes.\n", 3158c2ecf20Sopenharmony_ci dcache_size >> 10, dcache_lsize); 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci build_clear_page(); 3188c2ecf20Sopenharmony_ci build_copy_page(); 3198c2ecf20Sopenharmony_ci} 320