]> Pileus Git - ~andy/linux/blob - arch/arc/mm/cache_arc700.c
28007d25066e93a7d4c69b4f3b15da8168f7510d
[~andy/linux] / arch / arc / mm / cache_arc700.c
1 /*
2  * ARC700 VIPT Cache Management
3  *
4  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  vineetg: May 2011: for Non-aliasing VIPT D-cache following can be NOPs
11  *   -flush_cache_dup_mm (fork)
12  *   -likewise for flush_cache_mm (exit/execve)
13  *   -likewise for flush_cache_range,flush_cache_page (munmap, exit, COW-break)
14  *
15  * vineetg: Apr 2011
16  *  -Now that MMU can support larger pg sz (16K), the determiniation of
17  *   aliasing shd not be based on assumption of 8k pg
18  *
19  * vineetg: Mar 2011
20  *  -optimised version of flush_icache_range( ) for making I/D coherent
21  *   when vaddr is available (agnostic of num of aliases)
22  *
23  * vineetg: Mar 2011
24  *  -Added documentation about I-cache aliasing on ARC700 and the way it
25  *   was handled up until MMU V2.
26  *  -Spotted a three year old bug when killing the 4 aliases, which needs
27  *   bottom 2 bits, so we need to do paddr | {0x00, 0x01, 0x02, 0x03}
28  *                        instead of paddr | {0x00, 0x01, 0x10, 0x11}
29  *   (Rajesh you owe me one now)
30  *
31  * vineetg: Dec 2010
32  *  -Off-by-one error when computing num_of_lines to flush
33  *   This broke signal handling with bionic which uses synthetic sigret stub
34  *
35  * vineetg: Mar 2010
36  *  -GCC can't generate ZOL for core cache flush loops.
37  *   Conv them into iterations based as opposed to while (start < end) types
38  *
39  * Vineetg: July 2009
40  *  -In I-cache flush routine we used to chk for aliasing for every line INV.
41  *   Instead now we setup routines per cache geometry and invoke them
42  *   via function pointers.
43  *
44  * Vineetg: Jan 2009
45  *  -Cache Line flush routines used to flush an extra line beyond end addr
46  *   because check was while (end >= start) instead of (end > start)
47  *     =Some call sites had to work around by doing -1, -4 etc to end param
48  *     =Some callers didnt care. This was spec bad in case of INV routines
49  *      which would discard valid data (cause of the horrible ext2 bug
50  *      in ARC IDE driver)
51  *
52  * vineetg: June 11th 2008: Fixed flush_icache_range( )
53  *  -Since ARC700 caches are not coherent (I$ doesnt snoop D$) both need
54  *   to be flushed, which it was not doing.
55  *  -load_module( ) passes vmalloc addr (Kernel Virtual Addr) to the API,
56  *   however ARC cache maintenance OPs require PHY addr. Thus need to do
57  *   vmalloc_to_phy.
58  *  -Also added optimisation there, that for range > PAGE SIZE we flush the
59  *   entire cache in one shot rather than line by line. For e.g. a module
60  *   with Code sz 600k, old code flushed 600k worth of cache (line-by-line),
61  *   while cache is only 16 or 32k.
62  */
63
64 #include <linux/module.h>
65 #include <linux/mm.h>
66 #include <linux/sched.h>
67 #include <linux/cache.h>
68 #include <linux/mmu_context.h>
69 #include <linux/syscalls.h>
70 #include <linux/uaccess.h>
71 #include <linux/pagemap.h>
72 #include <asm/cacheflush.h>
73 #include <asm/cachectl.h>
74 #include <asm/setup.h>
75
76 char *arc_cache_mumbojumbo(int cpu_id, char *buf, int len)
77 {
78         int n = 0;
79         unsigned int c = smp_processor_id();
80
81 #define PR_CACHE(p, enb, str)                                           \
82 {                                                                       \
83         if (!(p)->ver)                                                  \
84                 n += scnprintf(buf + n, len - n, str"\t\t: N/A\n");     \
85         else                                                            \
86                 n += scnprintf(buf + n, len - n,                        \
87                         str"\t\t: (%uK) VIPT, %dway set-asc, %ub Line %s\n", \
88                         TO_KB((p)->sz), (p)->assoc, (p)->line_len,      \
89                         enb ?  "" : "DISABLED (kernel-build)");         \
90 }
91
92         PR_CACHE(&cpuinfo_arc700[c].icache, IS_ENABLED(CONFIG_ARC_HAS_ICACHE),
93                         "I-Cache");
94         PR_CACHE(&cpuinfo_arc700[c].dcache, IS_ENABLED(CONFIG_ARC_HAS_DCACHE),
95                         "D-Cache");
96
97         return buf;
98 }
99
100 /*
101  * Read the Cache Build Confuration Registers, Decode them and save into
102  * the cpuinfo structure for later use.
103  * No Validation done here, simply read/convert the BCRs
104  */
105 void __cpuinit read_decode_cache_bcr(void)
106 {
107         struct bcr_cache ibcr, dbcr;
108         struct cpuinfo_arc_cache *p_ic, *p_dc;
109         unsigned int cpu = smp_processor_id();
110
111         p_ic = &cpuinfo_arc700[cpu].icache;
112         READ_BCR(ARC_REG_IC_BCR, ibcr);
113
114         if (ibcr.config == 0x3)
115                 p_ic->assoc = 2;
116         p_ic->line_len = 8 << ibcr.line_len;
117         p_ic->sz = 0x200 << ibcr.sz;
118         p_ic->ver = ibcr.ver;
119
120         p_dc = &cpuinfo_arc700[cpu].dcache;
121         READ_BCR(ARC_REG_DC_BCR, dbcr);
122
123         if (dbcr.config == 0x2)
124                 p_dc->assoc = 4;
125         p_dc->line_len = 16 << dbcr.line_len;
126         p_dc->sz = 0x200 << dbcr.sz;
127         p_dc->ver = dbcr.ver;
128 }
129
130 /*
131  * 1. Validate the Cache Geomtery (compile time config matches hardware)
132  * 2. If I-cache suffers from aliasing, setup work arounds (difft flush rtn)
133  *    (aliasing D-cache configurations are not supported YET)
134  * 3. Enable the Caches, setup default flush mode for D-Cache
135  * 3. Calculate the SHMLBA used by user space
136  */
137 void __cpuinit arc_cache_init(void)
138 {
139         unsigned int temp;
140         unsigned int cpu = smp_processor_id();
141         struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
142         struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
143         int way_pg_ratio = way_pg_ratio;
144         int dcache_does_alias;
145         char str[256];
146
147         printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
148
149         if (!ic->ver)
150                 goto chk_dc;
151
152 #ifdef CONFIG_ARC_HAS_ICACHE
153         /* 1. Confirm some of I-cache params which Linux assumes */
154         if ((ic->assoc != ARC_ICACHE_WAYS) ||
155             (ic->line_len != ARC_ICACHE_LINE_LEN)) {
156                 panic("Cache H/W doesn't match kernel Config");
157         }
158 #if (CONFIG_ARC_MMU_VER > 2)
159         if (ic->ver != 3) {
160                 if (running_on_hw)
161                         panic("Cache ver doesn't match MMU ver\n");
162
163                 /* For ISS - suggest the toggles to use */
164                 pr_err("Use -prop=icache_version=3,-prop=dcache_version=3\n");
165
166         }
167 #endif
168 #endif
169
170         /* Enable/disable I-Cache */
171         temp = read_aux_reg(ARC_REG_IC_CTRL);
172
173 #ifdef CONFIG_ARC_HAS_ICACHE
174         temp &= ~IC_CTRL_CACHE_DISABLE;
175 #else
176         temp |= IC_CTRL_CACHE_DISABLE;
177 #endif
178
179         write_aux_reg(ARC_REG_IC_CTRL, temp);
180
181 chk_dc:
182         if (!dc->ver)
183                 return;
184
185 #ifdef CONFIG_ARC_HAS_DCACHE
186         if ((dc->assoc != ARC_DCACHE_WAYS) ||
187             (dc->line_len != ARC_DCACHE_LINE_LEN)) {
188                 panic("Cache H/W doesn't match kernel Config");
189         }
190
191         dcache_does_alias = (dc->sz / ARC_DCACHE_WAYS) > PAGE_SIZE;
192
193         /* check for D-Cache aliasing */
194         if (dcache_does_alias && !cache_is_vipt_aliasing())
195                 panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
196         else if (!dcache_does_alias && cache_is_vipt_aliasing())
197                 panic("Don't need CONFIG_ARC_CACHE_VIPT_ALIASING\n");
198 #endif
199
200         /* Set the default Invalidate Mode to "simpy discard dirty lines"
201          *  as this is more frequent then flush before invalidate
202          * Ofcourse we toggle this default behviour when desired
203          */
204         temp = read_aux_reg(ARC_REG_DC_CTRL);
205         temp &= ~DC_CTRL_INV_MODE_FLUSH;
206
207 #ifdef CONFIG_ARC_HAS_DCACHE
208         /* Enable D-Cache: Clear Bit 0 */
209         write_aux_reg(ARC_REG_DC_CTRL, temp & ~IC_CTRL_CACHE_DISABLE);
210 #else
211         /* Flush D cache */
212         write_aux_reg(ARC_REG_DC_FLSH, 0x1);
213         /* Disable D cache */
214         write_aux_reg(ARC_REG_DC_CTRL, temp | IC_CTRL_CACHE_DISABLE);
215 #endif
216
217         return;
218 }
219
220 #define OP_INV          0x1
221 #define OP_FLUSH        0x2
222 #define OP_FLUSH_N_INV  0x3
223
224 #ifdef CONFIG_ARC_HAS_DCACHE
225
226 /***************************************************************
227  * Machine specific helpers for Entire D-Cache or Per Line ops
228  */
229
230 static inline void wait_for_flush(void)
231 {
232         while (read_aux_reg(ARC_REG_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
233                 ;
234 }
235
236 /*
237  * Operation on Entire D-Cache
238  * @cacheop = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
239  * Note that constant propagation ensures all the checks are gone
240  * in generated code
241  */
242 static inline void __dc_entire_op(const int cacheop)
243 {
244         unsigned long flags, tmp = tmp;
245         int aux;
246
247         local_irq_save(flags);
248
249         if (cacheop == OP_FLUSH_N_INV) {
250                 /* Dcache provides 2 cmd: FLUSH or INV
251                  * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
252                  * flush-n-inv is achieved by INV cmd but with IM=1
253                  * Default INV sub-mode is DISCARD, which needs to be toggled
254                  */
255                 tmp = read_aux_reg(ARC_REG_DC_CTRL);
256                 write_aux_reg(ARC_REG_DC_CTRL, tmp | DC_CTRL_INV_MODE_FLUSH);
257         }
258
259         if (cacheop & OP_INV)   /* Inv or flush-n-inv use same cmd reg */
260                 aux = ARC_REG_DC_IVDC;
261         else
262                 aux = ARC_REG_DC_FLSH;
263
264         write_aux_reg(aux, 0x1);
265
266         if (cacheop & OP_FLUSH) /* flush / flush-n-inv both wait */
267                 wait_for_flush();
268
269         /* Switch back the DISCARD ONLY Invalidate mode */
270         if (cacheop == OP_FLUSH_N_INV)
271                 write_aux_reg(ARC_REG_DC_CTRL, tmp & ~DC_CTRL_INV_MODE_FLUSH);
272
273         local_irq_restore(flags);
274 }
275
276 /*
277  * Per Line Operation on D-Cache
278  * Doesn't deal with type-of-op/IRQ-disabling/waiting-for-flush-to-complete
279  * It's sole purpose is to help gcc generate ZOL
280  * (aliasing VIPT dcache flushing needs both vaddr and paddr)
281  */
282 static inline void __dc_line_loop(unsigned long paddr, unsigned long vaddr,
283                                   unsigned long sz, const int aux_reg)
284 {
285         int num_lines;
286
287         /* Ensure we properly floor/ceil the non-line aligned/sized requests
288          * and have @paddr - aligned to cache line and integral @num_lines.
289          * This however can be avoided for page sized since:
290          *  -@paddr will be cache-line aligned already (being page aligned)
291          *  -@sz will be integral multiple of line size (being page sized).
292          */
293         if (!(__builtin_constant_p(sz) && sz == PAGE_SIZE)) {
294                 sz += paddr & ~DCACHE_LINE_MASK;
295                 paddr &= DCACHE_LINE_MASK;
296                 vaddr &= DCACHE_LINE_MASK;
297         }
298
299         num_lines = DIV_ROUND_UP(sz, ARC_DCACHE_LINE_LEN);
300
301 #if (CONFIG_ARC_MMU_VER <= 2)
302         paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
303 #endif
304
305         while (num_lines-- > 0) {
306 #if (CONFIG_ARC_MMU_VER > 2)
307                 /*
308                  * Just as for I$, in MMU v3, D$ ops also require
309                  * "tag" bits in DC_PTAG, "index" bits in FLDL,IVDL ops
310                  */
311                 write_aux_reg(ARC_REG_DC_PTAG, paddr);
312
313                 write_aux_reg(aux_reg, vaddr);
314                 vaddr += ARC_DCACHE_LINE_LEN;
315 #else
316                 /* paddr contains stuffed vaddrs bits */
317                 write_aux_reg(aux_reg, paddr);
318 #endif
319                 paddr += ARC_DCACHE_LINE_LEN;
320         }
321 }
322
323 /* For kernel mappings cache operation: index is same as paddr */
324 #define __dc_line_op_k(p, sz, op)       __dc_line_op(p, p, sz, op)
325
326 /*
327  * D-Cache : Per Line INV (discard or wback+discard) or FLUSH (wback)
328  */
329 static inline void __dc_line_op(unsigned long paddr, unsigned long vaddr,
330                                 unsigned long sz, const int cacheop)
331 {
332         unsigned long flags, tmp = tmp;
333         int aux;
334
335         local_irq_save(flags);
336
337         if (cacheop == OP_FLUSH_N_INV) {
338                 /*
339                  * Dcache provides 2 cmd: FLUSH or INV
340                  * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
341                  * flush-n-inv is achieved by INV cmd but with IM=1
342                  * Default INV sub-mode is DISCARD, which needs to be toggled
343                  */
344                 tmp = read_aux_reg(ARC_REG_DC_CTRL);
345                 write_aux_reg(ARC_REG_DC_CTRL, tmp | DC_CTRL_INV_MODE_FLUSH);
346         }
347
348         if (cacheop & OP_INV)   /* Inv / flush-n-inv use same cmd reg */
349                 aux = ARC_REG_DC_IVDL;
350         else
351                 aux = ARC_REG_DC_FLDL;
352
353         __dc_line_loop(paddr, vaddr, sz, aux);
354
355         if (cacheop & OP_FLUSH) /* flush / flush-n-inv both wait */
356                 wait_for_flush();
357
358         /* Switch back the DISCARD ONLY Invalidate mode */
359         if (cacheop == OP_FLUSH_N_INV)
360                 write_aux_reg(ARC_REG_DC_CTRL, tmp & ~DC_CTRL_INV_MODE_FLUSH);
361
362         local_irq_restore(flags);
363 }
364
365 #else
366
367 #define __dc_entire_op(cacheop)
368 #define __dc_line_op(paddr, vaddr, sz, cacheop)
369 #define __dc_line_op_k(paddr, sz, cacheop)
370
371 #endif /* CONFIG_ARC_HAS_DCACHE */
372
373
374 #ifdef CONFIG_ARC_HAS_ICACHE
375
376 /*
377  *              I-Cache Aliasing in ARC700 VIPT caches
378  *
379  * ARC VIPT I-cache uses vaddr to index into cache and paddr to match the tag.
380  * The orig Cache Management Module "CDU" only required paddr to invalidate a
381  * certain line since it sufficed as index in Non-Aliasing VIPT cache-geometry.
382  * Infact for distinct V1,V2,P: all of {V1-P},{V2-P},{P-P} would end up fetching
383  * the exact same line.
384  *
385  * However for larger Caches (way-size > page-size) - i.e. in Aliasing config,
386  * paddr alone could not be used to correctly index the cache.
387  *
388  * ------------------
389  * MMU v1/v2 (Fixed Page Size 8k)
390  * ------------------
391  * The solution was to provide CDU with these additonal vaddr bits. These
392  * would be bits [x:13], x would depend on cache-geometry, 13 comes from
393  * standard page size of 8k.
394  * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits
395  * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the
396  * orig 5 bits of paddr were anyways ignored by CDU line ops, as they
397  * represent the offset within cache-line. The adv of using this "clumsy"
398  * interface for additional info was no new reg was needed in CDU programming
399  * model.
400  *
401  * 17:13 represented the max num of bits passable, actual bits needed were
402  * fewer, based on the num-of-aliases possible.
403  * -for 2 alias possibility, only bit 13 needed (32K cache)
404  * -for 4 alias possibility, bits 14:13 needed (64K cache)
405  *
406  * ------------------
407  * MMU v3
408  * ------------------
409  * This ver of MMU supports variable page sizes (1k-16k): although Linux will
410  * only support 8k (default), 16k and 4k.
411  * However from hardware perspective, smaller page sizes aggrevate aliasing
412  * meaning more vaddr bits needed to disambiguate the cache-line-op ;
413  * the existing scheme of piggybacking won't work for certain configurations.
414  * Two new registers IC_PTAG and DC_PTAG inttoduced.
415  * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs
416  */
417
418 /***********************************************************
419  * Machine specific helper for per line I-Cache invalidate.
420  */
421 static void __ic_line_inv_vaddr(unsigned long paddr, unsigned long vaddr,
422                                 unsigned long sz)
423 {
424         unsigned long flags;
425         int num_lines;
426
427         /*
428          * Ensure we properly floor/ceil the non-line aligned/sized requests:
429          * However page sized flushes can be compile time optimised.
430          *  -@paddr will be cache-line aligned already (being page aligned)
431          *  -@sz will be integral multiple of line size (being page sized).
432          */
433         if (!(__builtin_constant_p(sz) && sz == PAGE_SIZE)) {
434                 sz += paddr & ~ICACHE_LINE_MASK;
435                 paddr &= ICACHE_LINE_MASK;
436                 vaddr &= ICACHE_LINE_MASK;
437         }
438
439         num_lines = DIV_ROUND_UP(sz, ARC_ICACHE_LINE_LEN);
440
441 #if (CONFIG_ARC_MMU_VER <= 2)
442         /* bits 17:13 of vaddr go as bits 4:0 of paddr */
443         paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
444 #endif
445
446         local_irq_save(flags);
447         while (num_lines-- > 0) {
448 #if (CONFIG_ARC_MMU_VER > 2)
449                 /* tag comes from phy addr */
450                 write_aux_reg(ARC_REG_IC_PTAG, paddr);
451
452                 /* index bits come from vaddr */
453                 write_aux_reg(ARC_REG_IC_IVIL, vaddr);
454                 vaddr += ARC_ICACHE_LINE_LEN;
455 #else
456                 /* paddr contains stuffed vaddrs bits */
457                 write_aux_reg(ARC_REG_IC_IVIL, paddr);
458 #endif
459                 paddr += ARC_ICACHE_LINE_LEN;
460         }
461         local_irq_restore(flags);
462 }
463
464 #else
465
466 #define __ic_line_inv_vaddr(pstart, vstart, sz)
467
468 #endif /* CONFIG_ARC_HAS_ICACHE */
469
470
471 /***********************************************************
472  * Exported APIs
473  */
474
475 /*
476  * Handle cache congruency of kernel and userspace mappings of page when kernel
477  * writes-to/reads-from
478  *
479  * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
480  *  -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
481  *  -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
482  *  -In SMP, if hardware caches are coherent
483  *
484  * There's a corollary case, where kernel READs from a userspace mapped page.
485  * If the U-mapping is not congruent to to K-mapping, former needs flushing.
486  */
487 void flush_dcache_page(struct page *page)
488 {
489         struct address_space *mapping;
490
491         if (!cache_is_vipt_aliasing()) {
492                 set_bit(PG_arch_1, &page->flags);
493                 return;
494         }
495
496         /* don't handle anon pages here */
497         mapping = page_mapping(page);
498         if (!mapping)
499                 return;
500
501         /*
502          * pagecache page, file not yet mapped to userspace
503          * Make a note that K-mapping is dirty
504          */
505         if (!mapping_mapped(mapping)) {
506                 set_bit(PG_arch_1, &page->flags);
507         } else if (page_mapped(page)) {
508
509                 /* kernel reading from page with U-mapping */
510                 void *paddr = page_address(page);
511                 unsigned long vaddr = page->index << PAGE_CACHE_SHIFT;
512
513                 if (addr_not_cache_congruent(paddr, vaddr))
514                         __flush_dcache_page(paddr, vaddr);
515         }
516 }
517 EXPORT_SYMBOL(flush_dcache_page);
518
519
520 void dma_cache_wback_inv(unsigned long start, unsigned long sz)
521 {
522         __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
523 }
524 EXPORT_SYMBOL(dma_cache_wback_inv);
525
526 void dma_cache_inv(unsigned long start, unsigned long sz)
527 {
528         __dc_line_op_k(start, sz, OP_INV);
529 }
530 EXPORT_SYMBOL(dma_cache_inv);
531
532 void dma_cache_wback(unsigned long start, unsigned long sz)
533 {
534         __dc_line_op_k(start, sz, OP_FLUSH);
535 }
536 EXPORT_SYMBOL(dma_cache_wback);
537
538 /*
539  * This is API for making I/D Caches consistent when modifying
540  * kernel code (loadable modules, kprobes, kgdb...)
541  * This is called on insmod, with kernel virtual address for CODE of
542  * the module. ARC cache maintenance ops require PHY address thus we
543  * need to convert vmalloc addr to PHY addr
544  */
545 void flush_icache_range(unsigned long kstart, unsigned long kend)
546 {
547         unsigned int tot_sz, off, sz;
548         unsigned long phy, pfn;
549
550         /* printk("Kernel Cache Cohenercy: %lx to %lx\n",kstart, kend); */
551
552         /* This is not the right API for user virtual address */
553         if (kstart < TASK_SIZE) {
554                 BUG_ON("Flush icache range for user virtual addr space");
555                 return;
556         }
557
558         /* Shortcut for bigger flush ranges.
559          * Here we don't care if this was kernel virtual or phy addr
560          */
561         tot_sz = kend - kstart;
562         if (tot_sz > PAGE_SIZE) {
563                 flush_cache_all();
564                 return;
565         }
566
567         /* Case: Kernel Phy addr (0x8000_0000 onwards) */
568         if (likely(kstart > PAGE_OFFSET)) {
569                 /*
570                  * The 2nd arg despite being paddr will be used to index icache
571                  * This is OK since no alternate virtual mappings will exist
572                  * given the callers for this case: kprobe/kgdb in built-in
573                  * kernel code only.
574                  */
575                 __sync_icache_dcache(kstart, kstart, kend - kstart);
576                 return;
577         }
578
579         /*
580          * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
581          * (1) ARC Cache Maintenance ops only take Phy addr, hence special
582          *     handling of kernel vaddr.
583          *
584          * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
585          *     it still needs to handle  a 2 page scenario, where the range
586          *     straddles across 2 virtual pages and hence need for loop
587          */
588         while (tot_sz > 0) {
589                 off = kstart % PAGE_SIZE;
590                 pfn = vmalloc_to_pfn((void *)kstart);
591                 phy = (pfn << PAGE_SHIFT) + off;
592                 sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
593                 __sync_icache_dcache(phy, kstart, sz);
594                 kstart += sz;
595                 tot_sz -= sz;
596         }
597 }
598
599 /*
600  * General purpose helper to make I and D cache lines consistent.
601  * @paddr is phy addr of region
602  * @vaddr is typically user or kernel vaddr (vmalloc)
603  *    Howver in one instance, flush_icache_range() by kprobe (for a breakpt in
604  *    builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
605  *    use a paddr to index the cache (despite VIPT). This is fine since since a
606  *    built-in kernel page will not have any virtual mappings (not even kernel)
607  *    kprobe on loadable module is different as it will have kvaddr.
608  */
609 void __sync_icache_dcache(unsigned long paddr, unsigned long vaddr, int len)
610 {
611         unsigned long flags;
612
613         local_irq_save(flags);
614         __ic_line_inv_vaddr(paddr, vaddr, len);
615         __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
616         local_irq_restore(flags);
617 }
618
619 /* wrapper to compile time eliminate alignment checks in flush loop */
620 void __inv_icache_page(unsigned long paddr, unsigned long vaddr)
621 {
622         __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
623 }
624
625 /*
626  * wrapper to clearout kernel or userspace mappings of a page
627  * For kernel mappings @vaddr == @paddr
628  */
629 void ___flush_dcache_page(unsigned long paddr, unsigned long vaddr)
630 {
631         __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
632 }
633
634 void flush_icache_all(void)
635 {
636         unsigned long flags;
637
638         local_irq_save(flags);
639
640         write_aux_reg(ARC_REG_IC_IVIC, 1);
641
642         /* lr will not complete till the icache inv operation is not over */
643         read_aux_reg(ARC_REG_IC_CTRL);
644         local_irq_restore(flags);
645 }
646
647 noinline void flush_cache_all(void)
648 {
649         unsigned long flags;
650
651         local_irq_save(flags);
652
653         flush_icache_all();
654         __dc_entire_op(OP_FLUSH_N_INV);
655
656         local_irq_restore(flags);
657
658 }
659
660 #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
661
662 void flush_cache_mm(struct mm_struct *mm)
663 {
664         flush_cache_all();
665 }
666
667 void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
668                       unsigned long pfn)
669 {
670         unsigned int paddr = pfn << PAGE_SHIFT;
671
672         __sync_icache_dcache(paddr, u_vaddr, PAGE_SIZE);
673 }
674
675 void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
676                        unsigned long end)
677 {
678         flush_cache_all();
679 }
680
681 void flush_anon_page(struct vm_area_struct *vma, struct page *page,
682                      unsigned long u_vaddr)
683 {
684         /* TBD: do we really need to clear the kernel mapping */
685         __flush_dcache_page(page_address(page), u_vaddr);
686         __flush_dcache_page(page_address(page), page_address(page));
687
688 }
689
690 #endif
691
692 void copy_user_highpage(struct page *to, struct page *from,
693         unsigned long u_vaddr, struct vm_area_struct *vma)
694 {
695         void *kfrom = page_address(from);
696         void *kto = page_address(to);
697         int clean_src_k_mappings = 0;
698
699         /*
700          * If SRC page was already mapped in userspace AND it's U-mapping is
701          * not congruent with K-mapping, sync former to physical page so that
702          * K-mapping in memcpy below, sees the right data
703          *
704          * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
705          * equally valid for SRC page as well
706          */
707         if (page_mapped(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
708                 __flush_dcache_page(kfrom, u_vaddr);
709                 clean_src_k_mappings = 1;
710         }
711
712         copy_page(kto, kfrom);
713
714         /*
715          * Mark DST page K-mapping as dirty for a later finalization by
716          * update_mmu_cache(). Although the finalization could have been done
717          * here as well (given that both vaddr/paddr are available).
718          * But update_mmu_cache() already has code to do that for other
719          * non copied user pages (e.g. read faults which wire in pagecache page
720          * directly).
721          */
722         set_bit(PG_arch_1, &to->flags);
723
724         /*
725          * if SRC was already usermapped and non-congruent to kernel mapping
726          * sync the kernel mapping back to physical page
727          */
728         if (clean_src_k_mappings) {
729                 __flush_dcache_page(kfrom, kfrom);
730         } else {
731                 set_bit(PG_arch_1, &from->flags);
732         }
733 }
734
735 void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
736 {
737         clear_page(to);
738         set_bit(PG_arch_1, &page->flags);
739 }
740
741
742 /**********************************************************************
743  * Explicit Cache flush request from user space via syscall
744  * Needed for JITs which generate code on the fly
745  */
746 SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
747 {
748         /* TBD: optimize this */
749         flush_cache_all();
750         return 0;
751 }