blob: c299b30924eeac006f3ffc978325a9c8e044e419 [file] [log] [blame]
Vineet Gupta95d69762013-01-18 15:12:19 +05301/*
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 <asm/cacheflush.h>
72#include <asm/cachectl.h>
73#include <asm/setup.h>
74
75
76#ifdef CONFIG_ARC_HAS_ICACHE
77static void __ic_line_inv_no_alias(unsigned long, int);
78static void __ic_line_inv_2_alias(unsigned long, int);
79static void __ic_line_inv_4_alias(unsigned long, int);
80
81/* Holds the ptr to flush routine, dependign on size due to aliasing issues */
82static void (*___flush_icache_rtn) (unsigned long, int);
83#endif
84
Vineet Guptaaf617422013-01-18 15:12:24 +053085char *arc_cache_mumbojumbo(int cpu_id, char *buf, int len)
86{
87 int n = 0;
88 unsigned int c = smp_processor_id();
89
90#define PR_CACHE(p, enb, str) \
91{ \
92 if (!(p)->ver) \
93 n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \
94 else \
95 n += scnprintf(buf + n, len - n, \
96 str"\t\t: (%uK) VIPT, %dway set-asc, %ub Line %s\n", \
97 TO_KB((p)->sz), (p)->assoc, (p)->line_len, \
98 enb ? "" : "DISABLED (kernel-build)"); \
99}
100
101 PR_CACHE(&cpuinfo_arc700[c].icache, __CONFIG_ARC_HAS_ICACHE, "I-Cache");
102 PR_CACHE(&cpuinfo_arc700[c].dcache, __CONFIG_ARC_HAS_DCACHE, "D-Cache");
103
104 return buf;
105}
106
Vineet Gupta95d69762013-01-18 15:12:19 +0530107/*
108 * Read the Cache Build Confuration Registers, Decode them and save into
109 * the cpuinfo structure for later use.
110 * No Validation done here, simply read/convert the BCRs
111 */
112void __init read_decode_cache_bcr(void)
113{
114 struct bcr_cache ibcr, dbcr;
115 struct cpuinfo_arc_cache *p_ic, *p_dc;
116 unsigned int cpu = smp_processor_id();
117
118 p_ic = &cpuinfo_arc700[cpu].icache;
119 READ_BCR(ARC_REG_IC_BCR, ibcr);
120
121 if (ibcr.config == 0x3)
122 p_ic->assoc = 2;
123 p_ic->line_len = 8 << ibcr.line_len;
124 p_ic->sz = 0x200 << ibcr.sz;
125 p_ic->ver = ibcr.ver;
126
127 p_dc = &cpuinfo_arc700[cpu].dcache;
128 READ_BCR(ARC_REG_DC_BCR, dbcr);
129
130 if (dbcr.config == 0x2)
131 p_dc->assoc = 4;
132 p_dc->line_len = 16 << dbcr.line_len;
133 p_dc->sz = 0x200 << dbcr.sz;
134 p_dc->ver = dbcr.ver;
135}
136
137/*
138 * 1. Validate the Cache Geomtery (compile time config matches hardware)
139 * 2. If I-cache suffers from aliasing, setup work arounds (difft flush rtn)
140 * (aliasing D-cache configurations are not supported YET)
141 * 3. Enable the Caches, setup default flush mode for D-Cache
142 * 3. Calculate the SHMLBA used by user space
143 */
144void __init arc_cache_init(void)
145{
146 unsigned int temp;
147#ifdef CONFIG_ARC_CACHE
148 unsigned int cpu = smp_processor_id();
149#endif
150#ifdef CONFIG_ARC_HAS_ICACHE
151 struct cpuinfo_arc_cache *ic;
152#endif
153#ifdef CONFIG_ARC_HAS_DCACHE
154 struct cpuinfo_arc_cache *dc;
155#endif
156 int way_pg_ratio = way_pg_ratio;
Vineet Guptaaf617422013-01-18 15:12:24 +0530157 char str[256];
158
159 printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
Vineet Gupta95d69762013-01-18 15:12:19 +0530160
161#ifdef CONFIG_ARC_HAS_ICACHE
162 ic = &cpuinfo_arc700[cpu].icache;
163
Vineet Guptaaf617422013-01-18 15:12:24 +0530164 /* 1. Confirm some of I-cache params which Linux assumes */
165 if ((ic->assoc != ARC_ICACHE_WAYS) ||
166 (ic->line_len != ARC_ICACHE_LINE_LEN)) {
167 panic("Cache H/W doesn't match kernel Config");
168 }
169#if (CONFIG_ARC_MMU_VER > 2)
170 if (ic->ver != 3) {
171 if (running_on_hw)
172 panic("Cache ver doesn't match MMU ver\n");
173
174 /* For ISS - suggest the toggles to use */
175 pr_err("Use -prop=icache_version=3,-prop=dcache_version=3\n");
176
177 }
178#endif
179
Vineet Gupta95d69762013-01-18 15:12:19 +0530180 /*
181 * if Cache way size is <= page size then no aliasing exhibited
182 * otherwise ratio determines num of aliases.
183 * e.g. 32K I$, 2 way set assoc, 8k pg size
184 * way-sz = 32k/2 = 16k
185 * way-pg-ratio = 16k/8k = 2, so 2 aliases possible
186 * (meaning 1 line could be in 2 possible locations).
187 */
188 way_pg_ratio = ic->sz / ARC_ICACHE_WAYS / PAGE_SIZE;
189 switch (way_pg_ratio) {
190 case 0:
191 case 1:
192 ___flush_icache_rtn = __ic_line_inv_no_alias;
193 break;
194 case 2:
195 ___flush_icache_rtn = __ic_line_inv_2_alias;
196 break;
197 case 4:
198 ___flush_icache_rtn = __ic_line_inv_4_alias;
199 break;
200 default:
201 panic("Unsupported I-Cache Sz\n");
202 }
203#endif
204
205 /* Enable/disable I-Cache */
206 temp = read_aux_reg(ARC_REG_IC_CTRL);
207
208#ifdef CONFIG_ARC_HAS_ICACHE
209 temp &= ~IC_CTRL_CACHE_DISABLE;
210#else
211 temp |= IC_CTRL_CACHE_DISABLE;
212#endif
213
214 write_aux_reg(ARC_REG_IC_CTRL, temp);
215
216#ifdef CONFIG_ARC_HAS_DCACHE
217 dc = &cpuinfo_arc700[cpu].dcache;
218
Vineet Guptaaf617422013-01-18 15:12:24 +0530219 if ((dc->assoc != ARC_DCACHE_WAYS) ||
220 (dc->line_len != ARC_DCACHE_LINE_LEN)) {
221 panic("Cache H/W doesn't match kernel Config");
222 }
223
Vineet Gupta95d69762013-01-18 15:12:19 +0530224 /* check for D-Cache aliasing */
225 if ((dc->sz / ARC_DCACHE_WAYS) > PAGE_SIZE)
226 panic("D$ aliasing not handled right now\n");
227#endif
228
229 /* Set the default Invalidate Mode to "simpy discard dirty lines"
230 * as this is more frequent then flush before invalidate
231 * Ofcourse we toggle this default behviour when desired
232 */
233 temp = read_aux_reg(ARC_REG_DC_CTRL);
234 temp &= ~DC_CTRL_INV_MODE_FLUSH;
235
236#ifdef CONFIG_ARC_HAS_DCACHE
237 /* Enable D-Cache: Clear Bit 0 */
238 write_aux_reg(ARC_REG_DC_CTRL, temp & ~IC_CTRL_CACHE_DISABLE);
239#else
240 /* Flush D cache */
241 write_aux_reg(ARC_REG_DC_FLSH, 0x1);
242 /* Disable D cache */
243 write_aux_reg(ARC_REG_DC_CTRL, temp | IC_CTRL_CACHE_DISABLE);
244#endif
245
246 return;
247}
248
249#define OP_INV 0x1
250#define OP_FLUSH 0x2
251#define OP_FLUSH_N_INV 0x3
252
253#ifdef CONFIG_ARC_HAS_DCACHE
254
255/***************************************************************
256 * Machine specific helpers for Entire D-Cache or Per Line ops
257 */
258
259static inline void wait_for_flush(void)
260{
261 while (read_aux_reg(ARC_REG_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
262 ;
263}
264
265/*
266 * Operation on Entire D-Cache
267 * @cacheop = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
268 * Note that constant propagation ensures all the checks are gone
269 * in generated code
270 */
271static inline void __dc_entire_op(const int cacheop)
272{
273 unsigned long flags, tmp = tmp;
274 int aux;
275
276 local_irq_save(flags);
277
278 if (cacheop == OP_FLUSH_N_INV) {
279 /* Dcache provides 2 cmd: FLUSH or INV
280 * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
281 * flush-n-inv is achieved by INV cmd but with IM=1
282 * Default INV sub-mode is DISCARD, which needs to be toggled
283 */
284 tmp = read_aux_reg(ARC_REG_DC_CTRL);
285 write_aux_reg(ARC_REG_DC_CTRL, tmp | DC_CTRL_INV_MODE_FLUSH);
286 }
287
288 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
289 aux = ARC_REG_DC_IVDC;
290 else
291 aux = ARC_REG_DC_FLSH;
292
293 write_aux_reg(aux, 0x1);
294
295 if (cacheop & OP_FLUSH) /* flush / flush-n-inv both wait */
296 wait_for_flush();
297
298 /* Switch back the DISCARD ONLY Invalidate mode */
299 if (cacheop == OP_FLUSH_N_INV)
300 write_aux_reg(ARC_REG_DC_CTRL, tmp & ~DC_CTRL_INV_MODE_FLUSH);
301
302 local_irq_restore(flags);
303}
304
305/*
306 * Per Line Operation on D-Cache
307 * Doesn't deal with type-of-op/IRQ-disabling/waiting-for-flush-to-complete
308 * It's sole purpose is to help gcc generate ZOL
309 */
310static inline void __dc_line_loop(unsigned long start, unsigned long sz,
311 int aux_reg)
312{
313 int num_lines, slack;
314
315 /* Ensure we properly floor/ceil the non-line aligned/sized requests
316 * and have @start - aligned to cache line and integral @num_lines.
317 * This however can be avoided for page sized since:
318 * -@start will be cache-line aligned already (being page aligned)
319 * -@sz will be integral multiple of line size (being page sized).
320 */
321 if (!(__builtin_constant_p(sz) && sz == PAGE_SIZE)) {
322 slack = start & ~DCACHE_LINE_MASK;
323 sz += slack;
324 start -= slack;
325 }
326
327 num_lines = DIV_ROUND_UP(sz, ARC_DCACHE_LINE_LEN);
328
329 while (num_lines-- > 0) {
330#if (CONFIG_ARC_MMU_VER > 2)
331 /*
332 * Just as for I$, in MMU v3, D$ ops also require
333 * "tag" bits in DC_PTAG, "index" bits in FLDL,IVDL ops
334 * But we pass phy addr for both. This works since Linux
335 * doesn't support aliasing configs for D$, yet.
336 * Thus paddr is enough to provide both tag and index.
337 */
338 write_aux_reg(ARC_REG_DC_PTAG, start);
339#endif
340 write_aux_reg(aux_reg, start);
341 start += ARC_DCACHE_LINE_LEN;
342 }
343}
344
345/*
346 * D-Cache : Per Line INV (discard or wback+discard) or FLUSH (wback)
347 */
348static inline void __dc_line_op(unsigned long start, unsigned long sz,
349 const int cacheop)
350{
351 unsigned long flags, tmp = tmp;
352 int aux;
353
354 local_irq_save(flags);
355
356 if (cacheop == OP_FLUSH_N_INV) {
357 /*
358 * Dcache provides 2 cmd: FLUSH or INV
359 * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
360 * flush-n-inv is achieved by INV cmd but with IM=1
361 * Default INV sub-mode is DISCARD, which needs to be toggled
362 */
363 tmp = read_aux_reg(ARC_REG_DC_CTRL);
364 write_aux_reg(ARC_REG_DC_CTRL, tmp | DC_CTRL_INV_MODE_FLUSH);
365 }
366
367 if (cacheop & OP_INV) /* Inv / flush-n-inv use same cmd reg */
368 aux = ARC_REG_DC_IVDL;
369 else
370 aux = ARC_REG_DC_FLDL;
371
372 __dc_line_loop(start, sz, aux);
373
374 if (cacheop & OP_FLUSH) /* flush / flush-n-inv both wait */
375 wait_for_flush();
376
377 /* Switch back the DISCARD ONLY Invalidate mode */
378 if (cacheop == OP_FLUSH_N_INV)
379 write_aux_reg(ARC_REG_DC_CTRL, tmp & ~DC_CTRL_INV_MODE_FLUSH);
380
381 local_irq_restore(flags);
382}
383
384#else
385
386#define __dc_entire_op(cacheop)
387#define __dc_line_op(start, sz, cacheop)
388
389#endif /* CONFIG_ARC_HAS_DCACHE */
390
391
392#ifdef CONFIG_ARC_HAS_ICACHE
393
394/*
395 * I-Cache Aliasing in ARC700 VIPT caches
396 *
397 * For fetching code from I$, ARC700 uses vaddr (embedded in program code)
398 * to "index" into SET of cache-line and paddr from MMU to match the TAG
399 * in the WAYS of SET.
400 *
401 * However the CDU iterface (to flush/inv) lines from software, only takes
402 * paddr (to have simpler hardware interface). For simpler cases, using paddr
403 * alone suffices.
404 * e.g. 2-way-set-assoc, 16K I$ (8k MMU pg sz, 32b cache line size):
405 * way_sz = cache_sz / num_ways = 16k/2 = 8k
406 * num_sets = way_sz / line_sz = 8k/32 = 256 => 8 bits
407 * Ignoring the bottom 5 bits corresp to the off within a 32b cacheline,
408 * bits req for calc set-index = bits 12:5 (0 based). Since this range fits
409 * inside the bottom 13 bits of paddr, which are same for vaddr and paddr
410 * (with 8k pg sz), paddr alone can be safely used by CDU to unambigously
411 * locate a cache-line.
412 *
413 * However for a difft sized cache, say 32k I$, above math yields need
414 * for 14 bits of vaddr to locate a cache line, which can't be provided by
415 * paddr, since the bit 13 (0 based) might differ between the two.
416 *
417 * This lack of extra bits needed for correct line addressing, defines the
418 * classical problem of Cache aliasing with VIPT architectures
419 * num_aliases = 1 << extra_bits
420 * e.g. 2-way-set-assoc, 32K I$ with 8k MMU pg sz => 2 aliases
421 * 2-way-set-assoc, 64K I$ with 8k MMU pg sz => 4 aliases
422 * 2-way-set-assoc, 16K I$ with 8k MMU pg sz => NO aliases
423 *
424 * ------------------
425 * MMU v1/v2 (Fixed Page Size 8k)
426 * ------------------
427 * The solution was to provide CDU with these additonal vaddr bits. These
428 * would be bits [x:13], x would depend on cache-geom.
429 * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits
430 * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the
431 * orig 5 bits of paddr were anyways ignored by CDU line ops, as they
432 * represent the offset within cache-line. The adv of using this "clumsy"
433 * interface for additional info was no new reg was needed in CDU.
434 *
435 * 17:13 represented the max num of bits passable, actual bits needed were
436 * fewer, based on the num-of-aliases possible.
437 * -for 2 alias possibility, only bit 13 needed (32K cache)
438 * -for 4 alias possibility, bits 14:13 needed (64K cache)
439 *
440 * Since vaddr was not available for all instances of I$ flush req by core
441 * kernel, the only safe way (non-optimal though) was to kill all possible
442 * lines which could represent an alias (even if they didnt represent one
443 * in execution).
444 * e.g. for 64K I$, 4 aliases possible, so we did
445 * flush start
446 * flush start | 0x01
447 * flush start | 0x2
448 * flush start | 0x3
449 *
450 * The penalty was invoking the operation itself, since tag match is anyways
451 * paddr based, a line which didn't represent an alias would not match the
452 * paddr, hence wont be killed
453 *
454 * Note that aliasing concerns are independent of line-sz for a given cache
455 * geometry (size + set_assoc) because the extra bits required by line-sz are
456 * reduced from the set calc.
457 * e.g. 2-way-set-assoc, 32K I$ with 8k MMU pg sz and using math above
458 * 32b line-sz: 9 bits set-index-calc, 5 bits offset-in-line => 1 extra bit
459 * 64b line-sz: 8 bits set-index-calc, 6 bits offset-in-line => 1 extra bit
460 *
461 * ------------------
462 * MMU v3
463 * ------------------
464 * This ver of MMU supports var page sizes (1k-16k) - Linux will support
465 * 8k (default), 16k and 4k.
466 * However from hardware perspective, smaller page sizes aggrevate aliasing
467 * meaning more vaddr bits needed to disambiguate the cache-line-op ;
468 * the existing scheme of piggybacking won't work for certain configurations.
469 * Two new registers IC_PTAG and DC_PTAG inttoduced.
470 * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs
471 */
472
473/***********************************************************
474 * Machine specific helpers for per line I-Cache invalidate.
475 * 3 routines to accpunt for 1, 2, 4 aliases possible
476 */
477
478static void __ic_line_inv_no_alias(unsigned long start, int num_lines)
479{
480 while (num_lines-- > 0) {
481#if (CONFIG_ARC_MMU_VER > 2)
482 write_aux_reg(ARC_REG_IC_PTAG, start);
483#endif
484 write_aux_reg(ARC_REG_IC_IVIL, start);
485 start += ARC_ICACHE_LINE_LEN;
486 }
487}
488
489static void __ic_line_inv_2_alias(unsigned long start, int num_lines)
490{
491 while (num_lines-- > 0) {
492
493#if (CONFIG_ARC_MMU_VER > 2)
494 /*
495 * MMU v3, CDU prog model (for line ops) now uses a new IC_PTAG
496 * reg to pass the "tag" bits and existing IVIL reg only looks
497 * at bits relevant for "index" (details above)
498 * Programming Notes:
499 * -when writing tag to PTAG reg, bit chopping can be avoided,
500 * CDU ignores non-tag bits.
501 * -Ideally "index" must be computed from vaddr, but it is not
502 * avail in these rtns. So to be safe, we kill the lines in all
503 * possible indexes corresp to num of aliases possible for
504 * given cache config.
505 */
506 write_aux_reg(ARC_REG_IC_PTAG, start);
507 write_aux_reg(ARC_REG_IC_IVIL,
508 start & ~(0x1 << PAGE_SHIFT));
509 write_aux_reg(ARC_REG_IC_IVIL, start | (0x1 << PAGE_SHIFT));
510#else
511 write_aux_reg(ARC_REG_IC_IVIL, start);
512 write_aux_reg(ARC_REG_IC_IVIL, start | 0x01);
513#endif
514 start += ARC_ICACHE_LINE_LEN;
515 }
516}
517
518static void __ic_line_inv_4_alias(unsigned long start, int num_lines)
519{
520 while (num_lines-- > 0) {
521
522#if (CONFIG_ARC_MMU_VER > 2)
523 write_aux_reg(ARC_REG_IC_PTAG, start);
524
525 write_aux_reg(ARC_REG_IC_IVIL,
526 start & ~(0x3 << PAGE_SHIFT));
527 write_aux_reg(ARC_REG_IC_IVIL,
528 start & ~(0x2 << PAGE_SHIFT));
529 write_aux_reg(ARC_REG_IC_IVIL,
530 start & ~(0x1 << PAGE_SHIFT));
531 write_aux_reg(ARC_REG_IC_IVIL, start | (0x3 << PAGE_SHIFT));
532#else
533 write_aux_reg(ARC_REG_IC_IVIL, start);
534 write_aux_reg(ARC_REG_IC_IVIL, start | 0x01);
535 write_aux_reg(ARC_REG_IC_IVIL, start | 0x02);
536 write_aux_reg(ARC_REG_IC_IVIL, start | 0x03);
537#endif
538 start += ARC_ICACHE_LINE_LEN;
539 }
540}
541
542static void __ic_line_inv(unsigned long start, unsigned long sz)
543{
544 unsigned long flags;
545 int num_lines, slack;
546
547 /*
548 * Ensure we properly floor/ceil the non-line aligned/sized requests
549 * and have @start - aligned to cache line, and integral @num_lines
550 * However page sized flushes can be compile time optimised.
551 * -@start will be cache-line aligned already (being page aligned)
552 * -@sz will be integral multiple of line size (being page sized).
553 */
554 if (!(__builtin_constant_p(sz) && sz == PAGE_SIZE)) {
555 slack = start & ~ICACHE_LINE_MASK;
556 sz += slack;
557 start -= slack;
558 }
559
560 num_lines = DIV_ROUND_UP(sz, ARC_ICACHE_LINE_LEN);
561
562 local_irq_save(flags);
563 (*___flush_icache_rtn) (start, num_lines);
564 local_irq_restore(flags);
565}
566
567/* Unlike routines above, having vaddr for flush op (along with paddr),
568 * prevents the need to speculatively kill the lines in multiple sets
569 * based on ratio of way_sz : pg_sz
570 */
571static void __ic_line_inv_vaddr(unsigned long phy_start,
572 unsigned long vaddr, unsigned long sz)
573{
574 unsigned long flags;
575 int num_lines, slack;
576 unsigned int addr;
577
578 slack = phy_start & ~ICACHE_LINE_MASK;
579 sz += slack;
580 phy_start -= slack;
581 num_lines = DIV_ROUND_UP(sz, ARC_ICACHE_LINE_LEN);
582
583#if (CONFIG_ARC_MMU_VER > 2)
584 vaddr &= ~ICACHE_LINE_MASK;
585 addr = phy_start;
586#else
587 /* bits 17:13 of vaddr go as bits 4:0 of paddr */
588 addr = phy_start | ((vaddr >> 13) & 0x1F);
589#endif
590
591 local_irq_save(flags);
592 while (num_lines-- > 0) {
593#if (CONFIG_ARC_MMU_VER > 2)
594 /* tag comes from phy addr */
595 write_aux_reg(ARC_REG_IC_PTAG, addr);
596
597 /* index bits come from vaddr */
598 write_aux_reg(ARC_REG_IC_IVIL, vaddr);
599 vaddr += ARC_ICACHE_LINE_LEN;
600#else
601 /* this paddr contains vaddrs bits as needed */
602 write_aux_reg(ARC_REG_IC_IVIL, addr);
603#endif
604 addr += ARC_ICACHE_LINE_LEN;
605 }
606 local_irq_restore(flags);
607}
608
609#else
610
611#define __ic_line_inv(start, sz)
612#define __ic_line_inv_vaddr(pstart, vstart, sz)
613
614#endif /* CONFIG_ARC_HAS_ICACHE */
615
616
617/***********************************************************
618 * Exported APIs
619 */
620
621/* TBD: use pg_arch_1 to optimize this */
622void flush_dcache_page(struct page *page)
623{
624 __dc_line_op((unsigned long)page_address(page), PAGE_SIZE, OP_FLUSH);
625}
626EXPORT_SYMBOL(flush_dcache_page);
627
628
629void dma_cache_wback_inv(unsigned long start, unsigned long sz)
630{
631 __dc_line_op(start, sz, OP_FLUSH_N_INV);
632}
633EXPORT_SYMBOL(dma_cache_wback_inv);
634
635void dma_cache_inv(unsigned long start, unsigned long sz)
636{
637 __dc_line_op(start, sz, OP_INV);
638}
639EXPORT_SYMBOL(dma_cache_inv);
640
641void dma_cache_wback(unsigned long start, unsigned long sz)
642{
643 __dc_line_op(start, sz, OP_FLUSH);
644}
645EXPORT_SYMBOL(dma_cache_wback);
646
647/*
648 * This is API for making I/D Caches consistent when modifying code
649 * (loadable modules, kprobes, etc)
650 * This is called on insmod, with kernel virtual address for CODE of
651 * the module. ARC cache maintenance ops require PHY address thus we
652 * need to convert vmalloc addr to PHY addr
653 */
654void flush_icache_range(unsigned long kstart, unsigned long kend)
655{
656 unsigned int tot_sz, off, sz;
657 unsigned long phy, pfn;
658 unsigned long flags;
659
660 /* printk("Kernel Cache Cohenercy: %lx to %lx\n",kstart, kend); */
661
662 /* This is not the right API for user virtual address */
663 if (kstart < TASK_SIZE) {
664 BUG_ON("Flush icache range for user virtual addr space");
665 return;
666 }
667
668 /* Shortcut for bigger flush ranges.
669 * Here we don't care if this was kernel virtual or phy addr
670 */
671 tot_sz = kend - kstart;
672 if (tot_sz > PAGE_SIZE) {
673 flush_cache_all();
674 return;
675 }
676
677 /* Case: Kernel Phy addr (0x8000_0000 onwards) */
678 if (likely(kstart > PAGE_OFFSET)) {
679 __ic_line_inv(kstart, kend - kstart);
680 __dc_line_op(kstart, kend - kstart, OP_FLUSH);
681 return;
682 }
683
684 /*
685 * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
686 * (1) ARC Cache Maintenance ops only take Phy addr, hence special
687 * handling of kernel vaddr.
688 *
689 * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
690 * it still needs to handle a 2 page scenario, where the range
691 * straddles across 2 virtual pages and hence need for loop
692 */
693 while (tot_sz > 0) {
694 off = kstart % PAGE_SIZE;
695 pfn = vmalloc_to_pfn((void *)kstart);
696 phy = (pfn << PAGE_SHIFT) + off;
697 sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
698 local_irq_save(flags);
699 __dc_line_op(phy, sz, OP_FLUSH);
700 __ic_line_inv(phy, sz);
701 local_irq_restore(flags);
702 kstart += sz;
703 tot_sz -= sz;
704 }
705}
706
707/*
708 * Optimised ver of flush_icache_range() with spec callers: ptrace/signals
709 * where vaddr is also available. This allows passing both vaddr and paddr
710 * bits to CDU for cache flush, short-circuting the current pessimistic algo
711 * which kills all possible aliases.
712 * An added adv of knowing that vaddr is user-vaddr avoids various checks
713 * and handling for k-vaddr, k-paddr as done in orig ver above
714 */
715void flush_icache_range_vaddr(unsigned long paddr, unsigned long u_vaddr,
716 int len)
717{
718 __ic_line_inv_vaddr(paddr, u_vaddr, len);
719 __dc_line_op(paddr, len, OP_FLUSH);
720}
721
722/*
723 * XXX: This also needs to be optim using pg_arch_1
724 * This is called when a page-cache page is about to be mapped into a
725 * user process' address space. It offers an opportunity for a
726 * port to ensure d-cache/i-cache coherency if necessary.
727 */
728void flush_icache_page(struct vm_area_struct *vma, struct page *page)
729{
730 if (!(vma->vm_flags & VM_EXEC))
731 return;
732
733 __ic_line_inv((unsigned long)page_address(page), PAGE_SIZE);
734}
735
736void flush_icache_all(void)
737{
738 unsigned long flags;
739
740 local_irq_save(flags);
741
742 write_aux_reg(ARC_REG_IC_IVIC, 1);
743
744 /* lr will not complete till the icache inv operation is not over */
745 read_aux_reg(ARC_REG_IC_CTRL);
746 local_irq_restore(flags);
747}
748
749noinline void flush_cache_all(void)
750{
751 unsigned long flags;
752
753 local_irq_save(flags);
754
755 flush_icache_all();
756 __dc_entire_op(OP_FLUSH_N_INV);
757
758 local_irq_restore(flags);
759
760}
761
762/**********************************************************************
763 * Explicit Cache flush request from user space via syscall
764 * Needed for JITs which generate code on the fly
765 */
766SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
767{
768 /* TBD: optimize this */
769 flush_cache_all();
770 return 0;
771}