1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/sparc/mm/leon_m.c
9 * do srmmu probe in software
13 #include <linux/kernel.h>
17 #include <asm/tlbflush.h>
21 int leon_flush_during_switch = 1;
22 static int srmmu_swprobe_trace;
24 static inline unsigned long leon_get_ctable_ptr(void)
28 __asm__ __volatile__("lda [%1] %2, %0\n\t" :
30 "r" (SRMMU_CTXTBL_PTR),
31 "i" (ASI_LEON_MMUREGS));
32 return (retval & SRMMU_CTX_PMASK) << 4;
36 unsigned long leon_swprobe(unsigned long vaddr, unsigned long *paddr)
40 unsigned int pgd, pmd, ped;
42 unsigned int lvl, pte;
44 unsigned int paddr_calc;
46 if (srmmu_swprobe_trace)
47 printk(KERN_INFO "swprobe: trace on\n");
49 ctxtbl = leon_get_ctable_ptr();
51 if (srmmu_swprobe_trace)
52 printk(KERN_INFO "swprobe: leon_get_ctable_ptr returned 0=>0\n");
55 if (!_pfn_valid(PFN(ctxtbl))) {
56 if (srmmu_swprobe_trace)
58 "swprobe: !_pfn_valid(%x)=>0\n",
63 ctx = srmmu_get_context();
64 if (srmmu_swprobe_trace)
65 printk(KERN_INFO "swprobe: --- ctx (%x) ---\n", ctx);
67 pgd = LEON_BYPASS_LOAD_PA(ctxtbl + (ctx * 4));
69 if (((pgd & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
70 if (srmmu_swprobe_trace)
71 printk(KERN_INFO "swprobe: pgd is entry level 3\n");
76 if (((pgd & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
77 if (srmmu_swprobe_trace)
78 printk(KERN_INFO "swprobe: pgd is invalid => 0\n");
82 if (srmmu_swprobe_trace)
83 printk(KERN_INFO "swprobe: --- pgd (%x) ---\n", pgd);
85 ptr = (pgd & SRMMU_PTD_PMASK) << 4;
86 ptr += ((((vaddr) >> LEON_PGD_SH) & LEON_PGD_M) * 4);
87 if (!_pfn_valid(PFN(ptr)))
90 pmd = LEON_BYPASS_LOAD_PA(ptr);
91 if (((pmd & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
92 if (srmmu_swprobe_trace)
93 printk(KERN_INFO "swprobe: pmd is entry level 2\n");
98 if (((pmd & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
99 if (srmmu_swprobe_trace)
100 printk(KERN_INFO "swprobe: pmd is invalid => 0\n");
104 if (srmmu_swprobe_trace)
105 printk(KERN_INFO "swprobe: --- pmd (%x) ---\n", pmd);
107 ptr = (pmd & SRMMU_PTD_PMASK) << 4;
108 ptr += (((vaddr >> LEON_PMD_SH) & LEON_PMD_M) * 4);
109 if (!_pfn_valid(PFN(ptr))) {
110 if (srmmu_swprobe_trace)
111 printk(KERN_INFO "swprobe: !_pfn_valid(%x)=>0\n",
116 ped = LEON_BYPASS_LOAD_PA(ptr);
118 if (((ped & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
119 if (srmmu_swprobe_trace)
120 printk(KERN_INFO "swprobe: ped is entry level 1\n");
125 if (((ped & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
126 if (srmmu_swprobe_trace)
127 printk(KERN_INFO "swprobe: ped is invalid => 0\n");
131 if (srmmu_swprobe_trace)
132 printk(KERN_INFO "swprobe: --- ped (%x) ---\n", ped);
134 ptr = (ped & SRMMU_PTD_PMASK) << 4;
135 ptr += (((vaddr >> LEON_PTE_SH) & LEON_PTE_M) * 4);
136 if (!_pfn_valid(PFN(ptr)))
139 ptr = LEON_BYPASS_LOAD_PA(ptr);
140 if (((ptr & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
141 if (srmmu_swprobe_trace)
142 printk(KERN_INFO "swprobe: ptr is entry level 0\n");
147 if (srmmu_swprobe_trace)
148 printk(KERN_INFO "swprobe: ptr is invalid => 0\n");
155 (vaddr & ~(-1 << LEON_PTE_SH)) | ((pte & ~0xff) << 4);
159 (vaddr & ~(-1 << LEON_PMD_SH)) | ((pte & ~0xff) << 4);
163 (vaddr & ~(-1 << LEON_PGD_SH)) | ((pte & ~0xff) << 4);
170 if (srmmu_swprobe_trace)
171 printk(KERN_INFO "swprobe: padde %x\n", paddr_calc);
177 void leon_flush_icache_all(void)
179 __asm__ __volatile__(" flush "); /*iflush*/
182 void leon_flush_dcache_all(void)
184 __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t" : :
185 "i"(ASI_LEON_DFLUSH) : "memory");
188 void leon_flush_pcache_all(struct vm_area_struct *vma, unsigned long page)
190 if (vma->vm_flags & VM_EXEC)
191 leon_flush_icache_all();
192 leon_flush_dcache_all();
195 void leon_flush_cache_all(void)
197 __asm__ __volatile__(" flush "); /*iflush*/
198 __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t" : :
199 "i"(ASI_LEON_DFLUSH) : "memory");
202 void leon_flush_tlb_all(void)
204 leon_flush_cache_all();
205 __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : : "r"(0x400),
206 "i"(ASI_LEON_MMUFLUSH) : "memory");
209 /* get all cache regs */
210 void leon3_getCacheRegs(struct leon3_cacheregs *regs)
212 unsigned long ccr, iccr, dccr;
216 /* Get Cache regs from "Cache ASI" address 0x0, 0x8 and 0xC */
217 __asm__ __volatile__("lda [%%g0] %3, %0\n\t"
219 "lda [%%g1] %3, %1\n\t"
221 "lda [%%g1] %3, %2\n\t"
222 : "=r"(ccr), "=r"(iccr), "=r"(dccr)
224 : "i"(ASI_LEON_CACHEREGS) /* input */
225 : "g1" /* clobber list */
232 /* Due to virtual cache we need to check cache configuration if
233 * it is possible to skip flushing in some cases.
235 * Leon2 and Leon3 differ in their way of telling cache information
238 int __init leon_flush_needed(void)
240 int flush_needed = -1;
241 unsigned int ssize, sets;
243 { "direct mapped", "2-way associative", "3-way associative",
247 struct leon3_cacheregs cregs;
248 leon3_getCacheRegs(&cregs);
249 sets = (cregs.dccr & LEON3_XCCR_SETS_MASK) >> 24;
250 /* (ssize=>realsize) 0=>1k, 1=>2k, 2=>4k, 3=>8k ... */
251 ssize = 1 << ((cregs.dccr & LEON3_XCCR_SSIZE_MASK) >> 20);
253 printk(KERN_INFO "CACHE: %s cache, set size %dk\n",
254 sets > 3 ? "unknown" : setStr[sets], ssize);
255 if ((ssize <= (PAGE_SIZE / 1024)) && (sets == 0)) {
256 /* Set Size <= Page size ==>
257 flush on every context switch not needed. */
259 printk(KERN_INFO "CACHE: not flushing on every context switch\n");
264 void leon_switch_mm(void)
266 flush_tlb_mm((void *)0);
267 if (leon_flush_during_switch)
268 leon_flush_cache_all();
271 static void leon_flush_cache_mm(struct mm_struct *mm)
273 leon_flush_cache_all();
276 static void leon_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
278 leon_flush_pcache_all(vma, page);
281 static void leon_flush_cache_range(struct vm_area_struct *vma,
285 leon_flush_cache_all();
288 static void leon_flush_tlb_mm(struct mm_struct *mm)
290 leon_flush_tlb_all();
293 static void leon_flush_tlb_page(struct vm_area_struct *vma,
296 leon_flush_tlb_all();
299 static void leon_flush_tlb_range(struct vm_area_struct *vma,
303 leon_flush_tlb_all();
306 static void leon_flush_page_to_ram(unsigned long page)
308 leon_flush_cache_all();
311 static void leon_flush_sig_insns(struct mm_struct *mm, unsigned long page)
313 leon_flush_cache_all();
316 static void leon_flush_page_for_dma(unsigned long page)
318 leon_flush_dcache_all();
321 void __init poke_leonsparc(void)
325 static const struct sparc32_cachetlb_ops leon_ops = {
326 .cache_all = leon_flush_cache_all,
327 .cache_mm = leon_flush_cache_mm,
328 .cache_page = leon_flush_cache_page,
329 .cache_range = leon_flush_cache_range,
330 .tlb_all = leon_flush_tlb_all,
331 .tlb_mm = leon_flush_tlb_mm,
332 .tlb_page = leon_flush_tlb_page,
333 .tlb_range = leon_flush_tlb_range,
334 .page_to_ram = leon_flush_page_to_ram,
335 .sig_insns = leon_flush_sig_insns,
336 .page_for_dma = leon_flush_page_for_dma,
339 void __init init_leon(void)
342 sparc32_cachetlb_ops = &leon_ops;
343 poke_srmmu = poke_leonsparc;
345 leon_flush_during_switch = leon_flush_needed();