WIP FPC-III support
[linux/fpc-iii.git] / arch / nds32 / mm / proc.c
blob848c845f5f333f71446407d2699643f901290acb
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2005-2017 Andes Technology Corporation
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/mm.h>
7 #include <asm/nds32.h>
8 #include <asm/tlbflush.h>
9 #include <asm/cacheflush.h>
10 #include <asm/l2_cache.h>
11 #include <nds32_intrinsic.h>
13 #include <asm/cache_info.h>
14 extern struct cache_info L1_cache_info[2];
16 int va_kernel_present(unsigned long addr)
18 pmd_t *pmd;
19 pte_t *ptep, pte;
21 pmd = pmd_off_k(addr);
22 if (!pmd_none(*pmd)) {
23 ptep = pte_offset_map(pmd, addr);
24 pte = *ptep;
25 if (pte_present(pte))
26 return pte;
28 return 0;
31 pte_t va_present(struct mm_struct * mm, unsigned long addr)
33 pgd_t *pgd;
34 p4d_t *p4d;
35 pud_t *pud;
36 pmd_t *pmd;
37 pte_t *ptep, pte;
39 pgd = pgd_offset(mm, addr);
40 if (!pgd_none(*pgd)) {
41 p4d = p4d_offset(pgd, addr);
42 if (!p4d_none(*p4d)) {
43 pud = pud_offset(p4d, addr);
44 if (!pud_none(*pud)) {
45 pmd = pmd_offset(pud, addr);
46 if (!pmd_none(*pmd)) {
47 ptep = pte_offset_map(pmd, addr);
48 pte = *ptep;
49 if (pte_present(pte))
50 return pte;
55 return 0;
59 int va_readable(struct pt_regs *regs, unsigned long addr)
61 struct mm_struct *mm = current->mm;
62 pte_t pte;
63 int ret = 0;
65 if (user_mode(regs)) {
66 /* user mode */
67 pte = va_present(mm, addr);
68 if (!pte && pte_read(pte))
69 ret = 1;
70 } else {
71 /* superuser mode is always readable, so we can only
72 * check it is present or not*/
73 return (! !va_kernel_present(addr));
75 return ret;
78 int va_writable(struct pt_regs *regs, unsigned long addr)
80 struct mm_struct *mm = current->mm;
81 pte_t pte;
82 int ret = 0;
84 if (user_mode(regs)) {
85 /* user mode */
86 pte = va_present(mm, addr);
87 if (!pte && pte_write(pte))
88 ret = 1;
89 } else {
90 /* superuser mode */
91 pte = va_kernel_present(addr);
92 if (!pte && pte_kernel_write(pte))
93 ret = 1;
95 return ret;
99 * All
101 void cpu_icache_inval_all(void)
103 unsigned long end, line_size;
105 line_size = L1_cache_info[ICACHE].line_size;
106 end =
107 line_size * L1_cache_info[ICACHE].ways * L1_cache_info[ICACHE].sets;
109 do {
110 end -= line_size;
111 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
112 end -= line_size;
113 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
114 end -= line_size;
115 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
116 end -= line_size;
117 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
118 } while (end > 0);
119 __nds32__isb();
122 void cpu_dcache_inval_all(void)
124 __nds32__cctl_l1d_invalall();
127 #ifdef CONFIG_CACHE_L2
128 void dcache_wb_all_level(void)
130 unsigned long flags, cmd;
131 local_irq_save(flags);
132 __nds32__cctl_l1d_wball_alvl();
133 /* Section 1: Ensure the section 2 & 3 program code execution after */
134 __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
136 /* Section 2: Confirm the writeback all level is done in CPU and L2C */
137 cmd = CCTL_CMD_L2_SYNC;
138 L2_CMD_RDY();
139 L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
140 L2_CMD_RDY();
142 /* Section 3: Writeback whole L2 cache */
143 cmd = CCTL_ALL_CMD | CCTL_CMD_L2_IX_WB;
144 L2_CMD_RDY();
145 L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
146 L2_CMD_RDY();
147 __nds32__msync_all();
148 local_irq_restore(flags);
150 EXPORT_SYMBOL(dcache_wb_all_level);
151 #endif
153 void cpu_dcache_wb_all(void)
155 __nds32__cctl_l1d_wball_one_lvl();
156 __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
159 void cpu_dcache_wbinval_all(void)
161 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
162 unsigned long flags;
163 local_irq_save(flags);
164 #endif
165 cpu_dcache_wb_all();
166 cpu_dcache_inval_all();
167 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
168 local_irq_restore(flags);
169 #endif
173 * Page
175 void cpu_icache_inval_page(unsigned long start)
177 unsigned long line_size, end;
179 line_size = L1_cache_info[ICACHE].line_size;
180 end = start + PAGE_SIZE;
182 do {
183 end -= line_size;
184 __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
185 end -= line_size;
186 __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
187 end -= line_size;
188 __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
189 end -= line_size;
190 __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
191 } while (end != start);
192 __nds32__isb();
195 void cpu_dcache_inval_page(unsigned long start)
197 unsigned long line_size, end;
199 line_size = L1_cache_info[DCACHE].line_size;
200 end = start + PAGE_SIZE;
202 do {
203 end -= line_size;
204 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
205 end -= line_size;
206 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
207 end -= line_size;
208 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
209 end -= line_size;
210 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
211 } while (end != start);
214 void cpu_dcache_wb_page(unsigned long start)
216 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
217 unsigned long line_size, end;
219 line_size = L1_cache_info[DCACHE].line_size;
220 end = start + PAGE_SIZE;
222 do {
223 end -= line_size;
224 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
225 end -= line_size;
226 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
227 end -= line_size;
228 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
229 end -= line_size;
230 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
231 } while (end != start);
232 __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
233 #endif
236 void cpu_dcache_wbinval_page(unsigned long start)
238 unsigned long line_size, end;
240 line_size = L1_cache_info[DCACHE].line_size;
241 end = start + PAGE_SIZE;
243 do {
244 end -= line_size;
245 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
246 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
247 #endif
248 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
249 end -= line_size;
250 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
251 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
252 #endif
253 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
254 end -= line_size;
255 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
256 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
257 #endif
258 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
259 end -= line_size;
260 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
261 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
262 #endif
263 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
264 } while (end != start);
265 __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
268 void cpu_cache_wbinval_page(unsigned long page, int flushi)
270 cpu_dcache_wbinval_page(page);
271 if (flushi)
272 cpu_icache_inval_page(page);
276 * Range
278 void cpu_icache_inval_range(unsigned long start, unsigned long end)
280 unsigned long line_size;
282 line_size = L1_cache_info[ICACHE].line_size;
284 while (end > start) {
285 __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (start));
286 start += line_size;
288 __nds32__isb();
291 void cpu_dcache_inval_range(unsigned long start, unsigned long end)
293 unsigned long line_size;
295 line_size = L1_cache_info[DCACHE].line_size;
297 while (end > start) {
298 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (start));
299 start += line_size;
303 void cpu_dcache_wb_range(unsigned long start, unsigned long end)
305 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
306 unsigned long line_size;
308 line_size = L1_cache_info[DCACHE].line_size;
310 while (end > start) {
311 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (start));
312 start += line_size;
314 __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
315 #endif
318 void cpu_dcache_wbinval_range(unsigned long start, unsigned long end)
320 unsigned long line_size;
322 line_size = L1_cache_info[DCACHE].line_size;
324 while (end > start) {
325 #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
326 __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (start));
327 #endif
328 __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (start));
329 start += line_size;
331 __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
334 void cpu_cache_wbinval_range(unsigned long start, unsigned long end, int flushi)
336 unsigned long line_size, align_start, align_end;
338 line_size = L1_cache_info[DCACHE].line_size;
339 align_start = start & ~(line_size - 1);
340 align_end = (end + line_size - 1) & ~(line_size - 1);
341 cpu_dcache_wbinval_range(align_start, align_end);
343 if (flushi) {
344 line_size = L1_cache_info[ICACHE].line_size;
345 align_start = start & ~(line_size - 1);
346 align_end = (end + line_size - 1) & ~(line_size - 1);
347 cpu_icache_inval_range(align_start, align_end);
351 void cpu_cache_wbinval_range_check(struct vm_area_struct *vma,
352 unsigned long start, unsigned long end,
353 bool flushi, bool wbd)
355 unsigned long line_size, t_start, t_end;
357 if (!flushi && !wbd)
358 return;
359 line_size = L1_cache_info[DCACHE].line_size;
360 start = start & ~(line_size - 1);
361 end = (end + line_size - 1) & ~(line_size - 1);
363 if ((end - start) > (8 * PAGE_SIZE)) {
364 if (wbd)
365 cpu_dcache_wbinval_all();
366 if (flushi)
367 cpu_icache_inval_all();
368 return;
371 t_start = (start + PAGE_SIZE) & PAGE_MASK;
372 t_end = ((end - 1) & PAGE_MASK);
374 if ((start & PAGE_MASK) == t_end) {
375 if (va_present(vma->vm_mm, start)) {
376 if (wbd)
377 cpu_dcache_wbinval_range(start, end);
378 if (flushi)
379 cpu_icache_inval_range(start, end);
381 return;
384 if (va_present(vma->vm_mm, start)) {
385 if (wbd)
386 cpu_dcache_wbinval_range(start, t_start);
387 if (flushi)
388 cpu_icache_inval_range(start, t_start);
391 if (va_present(vma->vm_mm, end - 1)) {
392 if (wbd)
393 cpu_dcache_wbinval_range(t_end, end);
394 if (flushi)
395 cpu_icache_inval_range(t_end, end);
398 while (t_start < t_end) {
399 if (va_present(vma->vm_mm, t_start)) {
400 if (wbd)
401 cpu_dcache_wbinval_page(t_start);
402 if (flushi)
403 cpu_icache_inval_page(t_start);
405 t_start += PAGE_SIZE;
409 #ifdef CONFIG_CACHE_L2
410 static inline void cpu_l2cache_op(unsigned long start, unsigned long end, unsigned long op)
412 if (atl2c_base) {
413 unsigned long p_start = __pa(start);
414 unsigned long p_end = __pa(end);
415 unsigned long cmd;
416 unsigned long line_size;
417 /* TODO Can Use PAGE Mode to optimize if range large than PAGE_SIZE */
418 line_size = L2_CACHE_LINE_SIZE();
419 p_start = p_start & (~(line_size - 1));
420 p_end = (p_end + line_size - 1) & (~(line_size - 1));
421 cmd =
422 (p_start & ~(line_size - 1)) | op |
423 CCTL_SINGLE_CMD;
424 do {
425 L2_CMD_RDY();
426 L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
427 cmd += line_size;
428 p_start += line_size;
429 } while (p_end > p_start);
430 cmd = CCTL_CMD_L2_SYNC;
431 L2_CMD_RDY();
432 L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
433 L2_CMD_RDY();
436 #else
437 #define cpu_l2cache_op(start,end,op) do { } while (0)
438 #endif
440 * DMA
442 void cpu_dma_wb_range(unsigned long start, unsigned long end)
444 unsigned long line_size;
445 unsigned long flags;
446 line_size = L1_cache_info[DCACHE].line_size;
447 start = start & (~(line_size - 1));
448 end = (end + line_size - 1) & (~(line_size - 1));
449 if (unlikely(start == end))
450 return;
452 local_irq_save(flags);
453 cpu_dcache_wb_range(start, end);
454 cpu_l2cache_op(start, end, CCTL_CMD_L2_PA_WB);
455 __nds32__msync_all();
456 local_irq_restore(flags);
459 void cpu_dma_inval_range(unsigned long start, unsigned long end)
461 unsigned long line_size;
462 unsigned long old_start = start;
463 unsigned long old_end = end;
464 unsigned long flags;
465 line_size = L1_cache_info[DCACHE].line_size;
466 start = start & (~(line_size - 1));
467 end = (end + line_size - 1) & (~(line_size - 1));
468 if (unlikely(start == end))
469 return;
470 local_irq_save(flags);
471 if (start != old_start) {
472 cpu_dcache_wbinval_range(start, start + line_size);
473 cpu_l2cache_op(start, start + line_size, CCTL_CMD_L2_PA_WBINVAL);
475 if (end != old_end) {
476 cpu_dcache_wbinval_range(end - line_size, end);
477 cpu_l2cache_op(end - line_size, end, CCTL_CMD_L2_PA_WBINVAL);
479 cpu_dcache_inval_range(start, end);
480 cpu_l2cache_op(start, end, CCTL_CMD_L2_PA_INVAL);
481 __nds32__msync_all();
482 local_irq_restore(flags);
486 void cpu_dma_wbinval_range(unsigned long start, unsigned long end)
488 unsigned long line_size;
489 unsigned long flags;
490 line_size = L1_cache_info[DCACHE].line_size;
491 start = start & (~(line_size - 1));
492 end = (end + line_size - 1) & (~(line_size - 1));
493 if (unlikely(start == end))
494 return;
496 local_irq_save(flags);
497 cpu_dcache_wbinval_range(start, end);
498 cpu_l2cache_op(start, end, CCTL_CMD_L2_PA_WBINVAL);
499 __nds32__msync_all();
500 local_irq_restore(flags);
503 void cpu_proc_init(void)
507 void cpu_proc_fin(void)
511 void cpu_do_idle(void)
513 __nds32__standby_no_wake_grant();
516 void cpu_reset(unsigned long reset)
518 u32 tmp;
519 GIE_DISABLE();
520 tmp = __nds32__mfsr(NDS32_SR_CACHE_CTL);
521 tmp &= ~(CACHE_CTL_mskIC_EN | CACHE_CTL_mskDC_EN);
522 __nds32__mtsr_isb(tmp, NDS32_SR_CACHE_CTL);
523 cpu_dcache_wbinval_all();
524 cpu_icache_inval_all();
526 __asm__ __volatile__("jr.toff %0\n\t"::"r"(reset));
529 void cpu_switch_mm(struct mm_struct *mm)
531 unsigned long cid;
532 cid = __nds32__mfsr(NDS32_SR_TLB_MISC);
533 cid = (cid & ~TLB_MISC_mskCID) | mm->context.id;
534 __nds32__mtsr_dsb(cid, NDS32_SR_TLB_MISC);
535 __nds32__mtsr_isb(__pa(mm->pgd), NDS32_SR_L1_PPTB);