blob: 7c424e3f980de300f590bae41b0f6a57fca9392b [file] [log] [blame]
Vineet Gupta95d69762013-01-18 15:12:19 +05301/*
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +05302 * ARC Cache Management
Vineet Gupta95d69762013-01-18 15:12:19 +05303 *
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +05304 * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
Vineet Gupta95d69762013-01-18 15:12:19 +05305 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
Vineet Gupta95d69762013-01-18 15:12:19 +053010 */
11
12#include <linux/module.h>
13#include <linux/mm.h>
14#include <linux/sched.h>
15#include <linux/cache.h>
16#include <linux/mmu_context.h>
17#include <linux/syscalls.h>
18#include <linux/uaccess.h>
Vineet Gupta4102b532013-05-09 21:54:51 +053019#include <linux/pagemap.h>
Vineet Gupta95d69762013-01-18 15:12:19 +053020#include <asm/cacheflush.h>
21#include <asm/cachectl.h>
22#include <asm/setup.h>
23
Vineet Gupta795f4552015-04-03 12:37:07 +030024static int l2_line_sz;
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030025int ioc_exists;
Vineet Gupta79335a22015-06-04 18:30:23 +053026volatile int slc_enable = 1;
Vineet Gupta795f4552015-04-03 12:37:07 +030027
Vineet Guptabcc4d652015-06-04 14:39:15 +053028void (*_cache_line_loop_ic_fn)(unsigned long paddr, unsigned long vaddr,
29 unsigned long sz, const int cacheop);
30
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030031void (*__dma_cache_wback_inv)(unsigned long start, unsigned long sz);
32void (*__dma_cache_inv)(unsigned long start, unsigned long sz);
33void (*__dma_cache_wback)(unsigned long start, unsigned long sz);
34
Vineet Guptac3441ed2014-02-24 11:42:50 +080035char *arc_cache_mumbojumbo(int c, char *buf, int len)
Vineet Guptaaf617422013-01-18 15:12:24 +053036{
37 int n = 0;
Vineet Guptad1f317d2015-04-06 17:23:57 +053038 struct cpuinfo_arc_cache *p;
Vineet Guptaaf617422013-01-18 15:12:24 +053039
Vineet Gupta79335a22015-06-04 18:30:23 +053040#define IS_USED_RUN(v) ((v) ? "" : "(disabled) ")
Vineet Guptada40ff42014-06-27 15:49:47 +053041#define PR_CACHE(p, cfg, str) \
Vineet Guptaaf617422013-01-18 15:12:24 +053042 if (!(p)->ver) \
43 n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \
44 else \
45 n += scnprintf(buf + n, len - n, \
Vineet Guptada40ff42014-06-27 15:49:47 +053046 str"\t\t: %uK, %dway/set, %uB Line, %s%s%s\n", \
47 (p)->sz_k, (p)->assoc, (p)->line_len, \
48 (p)->vipt ? "VIPT" : "PIPT", \
49 (p)->alias ? " aliasing" : "", \
50 IS_ENABLED(cfg) ? "" : " (not used)");
Vineet Guptaaf617422013-01-18 15:12:24 +053051
Vineet Guptada40ff42014-06-27 15:49:47 +053052 PR_CACHE(&cpuinfo_arc700[c].icache, CONFIG_ARC_HAS_ICACHE, "I-Cache");
53 PR_CACHE(&cpuinfo_arc700[c].dcache, CONFIG_ARC_HAS_DCACHE, "D-Cache");
Vineet Guptaaf617422013-01-18 15:12:24 +053054
Vineet Guptad1f317d2015-04-06 17:23:57 +053055 p = &cpuinfo_arc700[c].slc;
56 if (p->ver)
57 n += scnprintf(buf + n, len - n,
Vineet Gupta79335a22015-06-04 18:30:23 +053058 "SLC\t\t: %uK, %uB Line%s\n",
59 p->sz_k, p->line_len, IS_USED_RUN(slc_enable));
Vineet Guptad1f317d2015-04-06 17:23:57 +053060
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030061 if (ioc_exists)
62 n += scnprintf(buf + n, len - n, "IOC\t\t: exists\n");
63
Vineet Guptaaf617422013-01-18 15:12:24 +053064 return buf;
65}
66
Vineet Gupta95d69762013-01-18 15:12:19 +053067/*
68 * Read the Cache Build Confuration Registers, Decode them and save into
69 * the cpuinfo structure for later use.
70 * No Validation done here, simply read/convert the BCRs
71 */
Paul Gortmakerce759952013-06-24 15:30:15 -040072void read_decode_cache_bcr(void)
Vineet Gupta95d69762013-01-18 15:12:19 +053073{
Vineet Guptad1f317d2015-04-06 17:23:57 +053074 struct cpuinfo_arc_cache *p_ic, *p_dc, *p_slc;
Vineet Gupta95d69762013-01-18 15:12:19 +053075 unsigned int cpu = smp_processor_id();
Vineet Guptada1677b2013-05-14 13:28:17 +053076 struct bcr_cache {
77#ifdef CONFIG_CPU_BIG_ENDIAN
78 unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
79#else
80 unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
81#endif
82 } ibcr, dbcr;
Vineet Gupta95d69762013-01-18 15:12:19 +053083
Vineet Guptad1f317d2015-04-06 17:23:57 +053084 struct bcr_generic sbcr;
85
86 struct bcr_slc_cfg {
87#ifdef CONFIG_CPU_BIG_ENDIAN
88 unsigned int pad:24, way:2, lsz:2, sz:4;
89#else
90 unsigned int sz:4, lsz:2, way:2, pad:24;
91#endif
92 } slc_cfg;
93
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030094 struct bcr_clust_cfg {
95#ifdef CONFIG_CPU_BIG_ENDIAN
96 unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8;
97#else
98 unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7;
99#endif
100 } cbcr;
101
Vineet Gupta95d69762013-01-18 15:12:19 +0530102 p_ic = &cpuinfo_arc700[cpu].icache;
103 READ_BCR(ARC_REG_IC_BCR, ibcr);
104
Vineet Guptada40ff42014-06-27 15:49:47 +0530105 if (!ibcr.ver)
106 goto dc_chk;
107
Vineet Guptad1f317d2015-04-06 17:23:57 +0530108 if (ibcr.ver <= 3) {
109 BUG_ON(ibcr.config != 3);
110 p_ic->assoc = 2; /* Fixed to 2w set assoc */
111 } else if (ibcr.ver >= 4) {
112 p_ic->assoc = 1 << ibcr.config; /* 1,2,4,8 */
113 }
114
Vineet Gupta95d69762013-01-18 15:12:19 +0530115 p_ic->line_len = 8 << ibcr.line_len;
Vineet Guptada40ff42014-06-27 15:49:47 +0530116 p_ic->sz_k = 1 << (ibcr.sz - 1);
Vineet Gupta95d69762013-01-18 15:12:19 +0530117 p_ic->ver = ibcr.ver;
Vineet Guptada40ff42014-06-27 15:49:47 +0530118 p_ic->vipt = 1;
119 p_ic->alias = p_ic->sz_k/p_ic->assoc/TO_KB(PAGE_SIZE) > 1;
Vineet Gupta95d69762013-01-18 15:12:19 +0530120
Vineet Guptada40ff42014-06-27 15:49:47 +0530121dc_chk:
Vineet Gupta95d69762013-01-18 15:12:19 +0530122 p_dc = &cpuinfo_arc700[cpu].dcache;
123 READ_BCR(ARC_REG_DC_BCR, dbcr);
124
Vineet Guptada40ff42014-06-27 15:49:47 +0530125 if (!dbcr.ver)
Vineet Guptad1f317d2015-04-06 17:23:57 +0530126 goto slc_chk;
Vineet Guptada40ff42014-06-27 15:49:47 +0530127
Vineet Guptad1f317d2015-04-06 17:23:57 +0530128 if (dbcr.ver <= 3) {
129 BUG_ON(dbcr.config != 2);
130 p_dc->assoc = 4; /* Fixed to 4w set assoc */
131 p_dc->vipt = 1;
132 p_dc->alias = p_dc->sz_k/p_dc->assoc/TO_KB(PAGE_SIZE) > 1;
133 } else if (dbcr.ver >= 4) {
134 p_dc->assoc = 1 << dbcr.config; /* 1,2,4,8 */
135 p_dc->vipt = 0;
136 p_dc->alias = 0; /* PIPT so can't VIPT alias */
137 }
138
Vineet Gupta95d69762013-01-18 15:12:19 +0530139 p_dc->line_len = 16 << dbcr.line_len;
Vineet Guptada40ff42014-06-27 15:49:47 +0530140 p_dc->sz_k = 1 << (dbcr.sz - 1);
Vineet Gupta95d69762013-01-18 15:12:19 +0530141 p_dc->ver = dbcr.ver;
Vineet Guptad1f317d2015-04-06 17:23:57 +0530142
143slc_chk:
Vineet Gupta795f4552015-04-03 12:37:07 +0300144 if (!is_isa_arcv2())
145 return;
146
Vineet Guptad1f317d2015-04-06 17:23:57 +0530147 p_slc = &cpuinfo_arc700[cpu].slc;
148 READ_BCR(ARC_REG_SLC_BCR, sbcr);
149 if (sbcr.ver) {
150 READ_BCR(ARC_REG_SLC_CFG, slc_cfg);
151 p_slc->ver = sbcr.ver;
152 p_slc->sz_k = 128 << slc_cfg.sz;
Vineet Gupta795f4552015-04-03 12:37:07 +0300153 l2_line_sz = p_slc->line_len = (slc_cfg.lsz == 0) ? 128 : 64;
Vineet Guptad1f317d2015-04-06 17:23:57 +0530154 }
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300155
156 READ_BCR(ARC_REG_CLUSTER_BCR, cbcr);
157 if (cbcr.c)
158 ioc_exists = 1;
Vineet Gupta95d69762013-01-18 15:12:19 +0530159}
160
161/*
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530162 * Line Operation on {I,D}-Cache
Vineet Gupta95d69762013-01-18 15:12:19 +0530163 */
Vineet Gupta95d69762013-01-18 15:12:19 +0530164
165#define OP_INV 0x1
166#define OP_FLUSH 0x2
167#define OP_FLUSH_N_INV 0x3
Vineet Guptabd129762013-09-05 13:43:03 +0530168#define OP_INV_IC 0x4
169
170/*
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530171 * I-Cache Aliasing in ARC700 VIPT caches (MMU v1-v3)
172 *
173 * ARC VIPT I-cache uses vaddr to index into cache and paddr to match the tag.
174 * The orig Cache Management Module "CDU" only required paddr to invalidate a
175 * certain line since it sufficed as index in Non-Aliasing VIPT cache-geometry.
176 * Infact for distinct V1,V2,P: all of {V1-P},{V2-P},{P-P} would end up fetching
177 * the exact same line.
178 *
179 * However for larger Caches (way-size > page-size) - i.e. in Aliasing config,
180 * paddr alone could not be used to correctly index the cache.
181 *
182 * ------------------
183 * MMU v1/v2 (Fixed Page Size 8k)
184 * ------------------
185 * The solution was to provide CDU with these additonal vaddr bits. These
186 * would be bits [x:13], x would depend on cache-geometry, 13 comes from
187 * standard page size of 8k.
188 * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits
189 * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the
190 * orig 5 bits of paddr were anyways ignored by CDU line ops, as they
191 * represent the offset within cache-line. The adv of using this "clumsy"
192 * interface for additional info was no new reg was needed in CDU programming
193 * model.
194 *
195 * 17:13 represented the max num of bits passable, actual bits needed were
196 * fewer, based on the num-of-aliases possible.
197 * -for 2 alias possibility, only bit 13 needed (32K cache)
198 * -for 4 alias possibility, bits 14:13 needed (64K cache)
199 *
200 * ------------------
201 * MMU v3
202 * ------------------
203 * This ver of MMU supports variable page sizes (1k-16k): although Linux will
204 * only support 8k (default), 16k and 4k.
205 * However from hardware perspective, smaller page sizes aggrevate aliasing
206 * meaning more vaddr bits needed to disambiguate the cache-line-op ;
207 * the existing scheme of piggybacking won't work for certain configurations.
208 * Two new registers IC_PTAG and DC_PTAG inttoduced.
209 * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs
Vineet Guptabd129762013-09-05 13:43:03 +0530210 */
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530211
Vineet Gupta11e14892014-08-04 08:32:31 -0700212static inline
213void __cache_line_loop_v2(unsigned long paddr, unsigned long vaddr,
214 unsigned long sz, const int op)
Vineet Guptabd129762013-09-05 13:43:03 +0530215{
Vineet Gupta11e14892014-08-04 08:32:31 -0700216 unsigned int aux_cmd;
Vineet Guptabd129762013-09-05 13:43:03 +0530217 int num_lines;
Vineet Gupta11e14892014-08-04 08:32:31 -0700218 const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
Vineet Guptabd129762013-09-05 13:43:03 +0530219
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530220 if (op == OP_INV_IC) {
Vineet Guptabd129762013-09-05 13:43:03 +0530221 aux_cmd = ARC_REG_IC_IVIL;
Vineet Gupta11e14892014-08-04 08:32:31 -0700222 } else {
Vineet Guptabd129762013-09-05 13:43:03 +0530223 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530224 aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
Vineet Guptabd129762013-09-05 13:43:03 +0530225 }
226
227 /* Ensure we properly floor/ceil the non-line aligned/sized requests
228 * and have @paddr - aligned to cache line and integral @num_lines.
229 * This however can be avoided for page sized since:
230 * -@paddr will be cache-line aligned already (being page aligned)
231 * -@sz will be integral multiple of line size (being page sized).
232 */
Vineet Gupta11e14892014-08-04 08:32:31 -0700233 if (!full_page) {
Vineet Guptabd129762013-09-05 13:43:03 +0530234 sz += paddr & ~CACHE_LINE_MASK;
235 paddr &= CACHE_LINE_MASK;
236 vaddr &= CACHE_LINE_MASK;
237 }
238
239 num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
240
Vineet Guptabd129762013-09-05 13:43:03 +0530241 /* MMUv2 and before: paddr contains stuffed vaddrs bits */
242 paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
Vineet Guptabd129762013-09-05 13:43:03 +0530243
244 while (num_lines-- > 0) {
Vineet Gupta11e14892014-08-04 08:32:31 -0700245 write_aux_reg(aux_cmd, paddr);
246 paddr += L1_CACHE_BYTES;
247 }
248}
249
250static inline
251void __cache_line_loop_v3(unsigned long paddr, unsigned long vaddr,
252 unsigned long sz, const int op)
253{
254 unsigned int aux_cmd, aux_tag;
255 int num_lines;
256 const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
257
258 if (op == OP_INV_IC) {
259 aux_cmd = ARC_REG_IC_IVIL;
260 aux_tag = ARC_REG_IC_PTAG;
261 } else {
262 aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
263 aux_tag = ARC_REG_DC_PTAG;
264 }
265
266 /* Ensure we properly floor/ceil the non-line aligned/sized requests
267 * and have @paddr - aligned to cache line and integral @num_lines.
268 * This however can be avoided for page sized since:
269 * -@paddr will be cache-line aligned already (being page aligned)
270 * -@sz will be integral multiple of line size (being page sized).
271 */
272 if (!full_page) {
273 sz += paddr & ~CACHE_LINE_MASK;
274 paddr &= CACHE_LINE_MASK;
275 vaddr &= CACHE_LINE_MASK;
276 }
277 num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
278
279 /*
280 * MMUv3, cache ops require paddr in PTAG reg
281 * if V-P const for loop, PTAG can be written once outside loop
282 */
283 if (full_page)
284 write_aux_reg(aux_tag, paddr);
285
286 while (num_lines-- > 0) {
287 if (!full_page) {
Vineet Guptad4599ba2013-09-05 14:45:51 +0530288 write_aux_reg(aux_tag, paddr);
289 paddr += L1_CACHE_BYTES;
290 }
Vineet Guptabd129762013-09-05 13:43:03 +0530291
292 write_aux_reg(aux_cmd, vaddr);
293 vaddr += L1_CACHE_BYTES;
Vineet Guptabd129762013-09-05 13:43:03 +0530294 }
295}
Vineet Gupta95d69762013-01-18 15:12:19 +0530296
Vineet Guptad1f317d2015-04-06 17:23:57 +0530297/*
298 * In HS38x (MMU v4), although icache is VIPT, only paddr is needed for cache
299 * maintenance ops (in IVIL reg), as long as icache doesn't alias.
300 *
301 * For Aliasing icache, vaddr is also needed (in IVIL), while paddr is
302 * specified in PTAG (similar to MMU v3)
303 */
304static inline
305void __cache_line_loop_v4(unsigned long paddr, unsigned long vaddr,
306 unsigned long sz, const int cacheop)
307{
308 unsigned int aux_cmd;
309 int num_lines;
310 const int full_page_op = __builtin_constant_p(sz) && sz == PAGE_SIZE;
311
312 if (cacheop == OP_INV_IC) {
313 aux_cmd = ARC_REG_IC_IVIL;
314 } else {
315 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
316 aux_cmd = cacheop & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
317 }
318
319 /* Ensure we properly floor/ceil the non-line aligned/sized requests
320 * and have @paddr - aligned to cache line and integral @num_lines.
321 * This however can be avoided for page sized since:
322 * -@paddr will be cache-line aligned already (being page aligned)
323 * -@sz will be integral multiple of line size (being page sized).
324 */
325 if (!full_page_op) {
326 sz += paddr & ~CACHE_LINE_MASK;
327 paddr &= CACHE_LINE_MASK;
328 }
329
330 num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
331
332 while (num_lines-- > 0) {
333 write_aux_reg(aux_cmd, paddr);
334 paddr += L1_CACHE_BYTES;
335 }
336}
337
Vineet Gupta11e14892014-08-04 08:32:31 -0700338#if (CONFIG_ARC_MMU_VER < 3)
339#define __cache_line_loop __cache_line_loop_v2
340#elif (CONFIG_ARC_MMU_VER == 3)
341#define __cache_line_loop __cache_line_loop_v3
Vineet Guptad1f317d2015-04-06 17:23:57 +0530342#elif (CONFIG_ARC_MMU_VER > 3)
343#define __cache_line_loop __cache_line_loop_v4
Vineet Gupta11e14892014-08-04 08:32:31 -0700344#endif
345
Vineet Gupta95d69762013-01-18 15:12:19 +0530346#ifdef CONFIG_ARC_HAS_DCACHE
347
348/***************************************************************
349 * Machine specific helpers for Entire D-Cache or Per Line ops
350 */
351
Vineet Gupta6c310682015-06-04 08:53:47 +0530352static inline void __before_dc_op(const int op)
Vineet Gupta95d69762013-01-18 15:12:19 +0530353{
Vineet Gupta1b1a22b2014-06-29 19:03:58 +0530354 if (op == OP_FLUSH_N_INV) {
355 /* Dcache provides 2 cmd: FLUSH or INV
356 * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
357 * flush-n-inv is achieved by INV cmd but with IM=1
358 * So toggle INV sub-mode depending on op request and default
359 */
Vineet Gupta6c310682015-06-04 08:53:47 +0530360 const unsigned int ctl = ARC_REG_DC_CTRL;
361 write_aux_reg(ctl, read_aux_reg(ctl) | DC_CTRL_INV_MODE_FLUSH);
Vineet Gupta1b1a22b2014-06-29 19:03:58 +0530362 }
Vineet Gupta1b1a22b2014-06-29 19:03:58 +0530363}
364
Vineet Gupta6c310682015-06-04 08:53:47 +0530365static inline void __after_dc_op(const int op)
Vineet Gupta1b1a22b2014-06-29 19:03:58 +0530366{
Vineet Gupta6c310682015-06-04 08:53:47 +0530367 if (op & OP_FLUSH) {
368 const unsigned int ctl = ARC_REG_DC_CTRL;
369 unsigned int reg;
Vineet Gupta1b1a22b2014-06-29 19:03:58 +0530370
Vineet Gupta6c310682015-06-04 08:53:47 +0530371 /* flush / flush-n-inv both wait */
372 while ((reg = read_aux_reg(ctl)) & DC_CTRL_FLUSH_STATUS)
373 ;
374
375 /* Switch back to default Invalidate mode */
376 if (op == OP_FLUSH_N_INV)
377 write_aux_reg(ctl, reg & ~DC_CTRL_INV_MODE_FLUSH);
378 }
Vineet Gupta95d69762013-01-18 15:12:19 +0530379}
380
381/*
382 * Operation on Entire D-Cache
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530383 * @op = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
Vineet Gupta95d69762013-01-18 15:12:19 +0530384 * Note that constant propagation ensures all the checks are gone
385 * in generated code
386 */
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530387static inline void __dc_entire_op(const int op)
Vineet Gupta95d69762013-01-18 15:12:19 +0530388{
Vineet Gupta95d69762013-01-18 15:12:19 +0530389 int aux;
390
Vineet Gupta6c310682015-06-04 08:53:47 +0530391 __before_dc_op(op);
Vineet Gupta95d69762013-01-18 15:12:19 +0530392
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530393 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
Vineet Gupta95d69762013-01-18 15:12:19 +0530394 aux = ARC_REG_DC_IVDC;
395 else
396 aux = ARC_REG_DC_FLSH;
397
398 write_aux_reg(aux, 0x1);
399
Vineet Gupta6c310682015-06-04 08:53:47 +0530400 __after_dc_op(op);
Vineet Gupta95d69762013-01-18 15:12:19 +0530401}
402
Vineet Gupta4102b532013-05-09 21:54:51 +0530403/* For kernel mappings cache operation: index is same as paddr */
Vineet Gupta6ec18a82013-05-09 15:10:18 +0530404#define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op)
405
Vineet Gupta95d69762013-01-18 15:12:19 +0530406/*
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530407 * D-Cache Line ops: Per Line INV (discard or wback+discard) or FLUSH (wback)
Vineet Gupta95d69762013-01-18 15:12:19 +0530408 */
Vineet Gupta6ec18a82013-05-09 15:10:18 +0530409static inline void __dc_line_op(unsigned long paddr, unsigned long vaddr,
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530410 unsigned long sz, const int op)
Vineet Gupta95d69762013-01-18 15:12:19 +0530411{
Vineet Gupta1b1a22b2014-06-29 19:03:58 +0530412 unsigned long flags;
Vineet Gupta95d69762013-01-18 15:12:19 +0530413
414 local_irq_save(flags);
415
Vineet Gupta6c310682015-06-04 08:53:47 +0530416 __before_dc_op(op);
Vineet Gupta95d69762013-01-18 15:12:19 +0530417
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530418 __cache_line_loop(paddr, vaddr, sz, op);
Vineet Gupta95d69762013-01-18 15:12:19 +0530419
Vineet Gupta6c310682015-06-04 08:53:47 +0530420 __after_dc_op(op);
Vineet Gupta95d69762013-01-18 15:12:19 +0530421
422 local_irq_restore(flags);
423}
424
425#else
426
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530427#define __dc_entire_op(op)
428#define __dc_line_op(paddr, vaddr, sz, op)
429#define __dc_line_op_k(paddr, sz, op)
Vineet Gupta95d69762013-01-18 15:12:19 +0530430
431#endif /* CONFIG_ARC_HAS_DCACHE */
432
Vineet Gupta95d69762013-01-18 15:12:19 +0530433#ifdef CONFIG_ARC_HAS_ICACHE
434
Vineet Guptaaf5abf12014-07-09 14:59:47 +0530435static inline void __ic_entire_inv(void)
436{
437 write_aux_reg(ARC_REG_IC_IVIC, 1);
438 read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
439}
440
441static inline void
442__ic_line_inv_vaddr_local(unsigned long paddr, unsigned long vaddr,
443 unsigned long sz)
Vineet Gupta95d69762013-01-18 15:12:19 +0530444{
445 unsigned long flags;
Vineet Gupta95d69762013-01-18 15:12:19 +0530446
447 local_irq_save(flags);
Vineet Guptabcc4d652015-06-04 14:39:15 +0530448 (*_cache_line_loop_ic_fn)(paddr, vaddr, sz, OP_INV_IC);
Vineet Gupta95d69762013-01-18 15:12:19 +0530449 local_irq_restore(flags);
450}
451
Vineet Guptaaf5abf12014-07-09 14:59:47 +0530452#ifndef CONFIG_SMP
Vineet Gupta336e1992013-06-22 19:22:42 +0530453
Vineet Guptaaf5abf12014-07-09 14:59:47 +0530454#define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s)
455
456#else
457
458struct ic_inv_args {
Vineet Gupta2328af02013-02-17 12:51:42 +0200459 unsigned long paddr, vaddr;
460 int sz;
461};
462
463static void __ic_line_inv_vaddr_helper(void *info)
464{
Noam Camus014018e2014-09-03 14:41:11 +0300465 struct ic_inv_args *ic_inv = info;
Vineet Guptaaf5abf12014-07-09 14:59:47 +0530466
Vineet Gupta2328af02013-02-17 12:51:42 +0200467 __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
468}
469
470static void __ic_line_inv_vaddr(unsigned long paddr, unsigned long vaddr,
471 unsigned long sz)
472{
Vineet Guptaaf5abf12014-07-09 14:59:47 +0530473 struct ic_inv_args ic_inv = {
474 .paddr = paddr,
475 .vaddr = vaddr,
476 .sz = sz
477 };
478
Vineet Gupta2328af02013-02-17 12:51:42 +0200479 on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
480}
Vineet Guptaaf5abf12014-07-09 14:59:47 +0530481
482#endif /* CONFIG_SMP */
483
484#else /* !CONFIG_ARC_HAS_ICACHE */
Vineet Gupta95d69762013-01-18 15:12:19 +0530485
Vineet Gupta336e1992013-06-22 19:22:42 +0530486#define __ic_entire_inv()
Vineet Gupta95d69762013-01-18 15:12:19 +0530487#define __ic_line_inv_vaddr(pstart, vstart, sz)
488
489#endif /* CONFIG_ARC_HAS_ICACHE */
490
Vineet Gupta795f4552015-04-03 12:37:07 +0300491noinline void slc_op(unsigned long paddr, unsigned long sz, const int op)
492{
493#ifdef CONFIG_ISA_ARCV2
Alexey Brodkinb607edd2015-06-29 15:24:37 +0300494 /*
495 * SLC is shared between all cores and concurrent aux operations from
496 * multiple cores need to be serialized using a spinlock
497 * A concurrent operation can be silently ignored and/or the old/new
498 * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
499 * below)
500 */
501 static DEFINE_SPINLOCK(lock);
Vineet Gupta795f4552015-04-03 12:37:07 +0300502 unsigned long flags;
503 unsigned int ctrl;
504
Alexey Brodkinb607edd2015-06-29 15:24:37 +0300505 spin_lock_irqsave(&lock, flags);
Vineet Gupta795f4552015-04-03 12:37:07 +0300506
507 /*
508 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
509 * - b'000 (default) is Flush,
510 * - b'001 is Invalidate if CTRL.IM == 0
511 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
512 */
513 ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
514
515 /* Don't rely on default value of IM bit */
516 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
517 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
518 else
519 ctrl |= SLC_CTRL_IM;
520
521 if (op & OP_INV)
522 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
523 else
524 ctrl &= ~SLC_CTRL_RGN_OP_INV;
525
526 write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
527
528 /*
529 * Lower bits are ignored, no need to clip
530 * END needs to be setup before START (latter triggers the operation)
531 * END can't be same as START, so add (l2_line_sz - 1) to sz
532 */
533 write_aux_reg(ARC_REG_SLC_RGN_END, (paddr + sz + l2_line_sz - 1));
534 write_aux_reg(ARC_REG_SLC_RGN_START, paddr);
535
536 while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
537
Alexey Brodkinb607edd2015-06-29 15:24:37 +0300538 spin_unlock_irqrestore(&lock, flags);
Vineet Gupta795f4552015-04-03 12:37:07 +0300539#endif
540}
541
Vineet Gupta95d69762013-01-18 15:12:19 +0530542/***********************************************************
543 * Exported APIs
544 */
545
Vineet Gupta4102b532013-05-09 21:54:51 +0530546/*
547 * Handle cache congruency of kernel and userspace mappings of page when kernel
548 * writes-to/reads-from
549 *
550 * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
551 * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
552 * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
553 * -In SMP, if hardware caches are coherent
554 *
555 * There's a corollary case, where kernel READs from a userspace mapped page.
556 * If the U-mapping is not congruent to to K-mapping, former needs flushing.
557 */
Vineet Gupta95d69762013-01-18 15:12:19 +0530558void flush_dcache_page(struct page *page)
559{
Vineet Gupta4102b532013-05-09 21:54:51 +0530560 struct address_space *mapping;
561
562 if (!cache_is_vipt_aliasing()) {
Vineet Gupta2ed21da2013-05-13 17:23:58 +0530563 clear_bit(PG_dc_clean, &page->flags);
Vineet Gupta4102b532013-05-09 21:54:51 +0530564 return;
565 }
566
567 /* don't handle anon pages here */
568 mapping = page_mapping(page);
569 if (!mapping)
570 return;
571
572 /*
573 * pagecache page, file not yet mapped to userspace
574 * Make a note that K-mapping is dirty
575 */
576 if (!mapping_mapped(mapping)) {
Vineet Gupta2ed21da2013-05-13 17:23:58 +0530577 clear_bit(PG_dc_clean, &page->flags);
Vineet Gupta4102b532013-05-09 21:54:51 +0530578 } else if (page_mapped(page)) {
579
580 /* kernel reading from page with U-mapping */
Vineet Gupta45309492015-05-18 12:46:37 +0530581 unsigned long paddr = (unsigned long)page_address(page);
Vineet Gupta4102b532013-05-09 21:54:51 +0530582 unsigned long vaddr = page->index << PAGE_CACHE_SHIFT;
583
584 if (addr_not_cache_congruent(paddr, vaddr))
585 __flush_dcache_page(paddr, vaddr);
586 }
Vineet Gupta95d69762013-01-18 15:12:19 +0530587}
588EXPORT_SYMBOL(flush_dcache_page);
589
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300590/*
591 * DMA ops for systems with L1 cache only
592 * Make memory coherent with L1 cache by flushing/invalidating L1 lines
593 */
594static void __dma_cache_wback_inv_l1(unsigned long start, unsigned long sz)
Vineet Gupta95d69762013-01-18 15:12:19 +0530595{
Vineet Gupta6ec18a82013-05-09 15:10:18 +0530596 __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300597}
Vineet Gupta795f4552015-04-03 12:37:07 +0300598
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300599static void __dma_cache_inv_l1(unsigned long start, unsigned long sz)
600{
601 __dc_line_op_k(start, sz, OP_INV);
602}
603
604static void __dma_cache_wback_l1(unsigned long start, unsigned long sz)
605{
606 __dc_line_op_k(start, sz, OP_FLUSH);
607}
608
609/*
610 * DMA ops for systems with both L1 and L2 caches, but without IOC
611 * Both L1 and L2 lines need to be explicity flushed/invalidated
612 */
613static void __dma_cache_wback_inv_slc(unsigned long start, unsigned long sz)
614{
615 __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
616 slc_op(start, sz, OP_FLUSH_N_INV);
617}
618
619static void __dma_cache_inv_slc(unsigned long start, unsigned long sz)
620{
621 __dc_line_op_k(start, sz, OP_INV);
622 slc_op(start, sz, OP_INV);
623}
624
625static void __dma_cache_wback_slc(unsigned long start, unsigned long sz)
626{
627 __dc_line_op_k(start, sz, OP_FLUSH);
628 slc_op(start, sz, OP_FLUSH);
629}
630
631/*
632 * DMA ops for systems with IOC
633 * IOC hardware snoops all DMA traffic keeping the caches consistent with
634 * memory - eliding need for any explicit cache maintenance of DMA buffers
635 */
636static void __dma_cache_wback_inv_ioc(unsigned long start, unsigned long sz) {}
637static void __dma_cache_inv_ioc(unsigned long start, unsigned long sz) {}
638static void __dma_cache_wback_ioc(unsigned long start, unsigned long sz) {}
639
640/*
641 * Exported DMA API
642 */
643void dma_cache_wback_inv(unsigned long start, unsigned long sz)
644{
645 __dma_cache_wback_inv(start, sz);
Vineet Gupta95d69762013-01-18 15:12:19 +0530646}
647EXPORT_SYMBOL(dma_cache_wback_inv);
648
649void dma_cache_inv(unsigned long start, unsigned long sz)
650{
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300651 __dma_cache_inv(start, sz);
Vineet Gupta95d69762013-01-18 15:12:19 +0530652}
653EXPORT_SYMBOL(dma_cache_inv);
654
655void dma_cache_wback(unsigned long start, unsigned long sz)
656{
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300657 __dma_cache_wback(start, sz);
Vineet Gupta95d69762013-01-18 15:12:19 +0530658}
659EXPORT_SYMBOL(dma_cache_wback);
660
661/*
Vineet Gupta7586bf722013-04-12 12:18:25 +0530662 * This is API for making I/D Caches consistent when modifying
663 * kernel code (loadable modules, kprobes, kgdb...)
Vineet Gupta95d69762013-01-18 15:12:19 +0530664 * This is called on insmod, with kernel virtual address for CODE of
665 * the module. ARC cache maintenance ops require PHY address thus we
666 * need to convert vmalloc addr to PHY addr
667 */
668void flush_icache_range(unsigned long kstart, unsigned long kend)
669{
Vineet Guptac59414c2014-09-24 11:36:20 +0530670 unsigned int tot_sz;
Vineet Gupta95d69762013-01-18 15:12:19 +0530671
Vineet Guptac59414c2014-09-24 11:36:20 +0530672 WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
Vineet Gupta95d69762013-01-18 15:12:19 +0530673
674 /* Shortcut for bigger flush ranges.
675 * Here we don't care if this was kernel virtual or phy addr
676 */
677 tot_sz = kend - kstart;
678 if (tot_sz > PAGE_SIZE) {
679 flush_cache_all();
680 return;
681 }
682
683 /* Case: Kernel Phy addr (0x8000_0000 onwards) */
684 if (likely(kstart > PAGE_OFFSET)) {
Vineet Gupta7586bf722013-04-12 12:18:25 +0530685 /*
686 * The 2nd arg despite being paddr will be used to index icache
687 * This is OK since no alternate virtual mappings will exist
688 * given the callers for this case: kprobe/kgdb in built-in
689 * kernel code only.
690 */
Vineet Gupta94bad1a2013-04-12 12:20:23 +0530691 __sync_icache_dcache(kstart, kstart, kend - kstart);
Vineet Gupta95d69762013-01-18 15:12:19 +0530692 return;
693 }
694
695 /*
696 * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
697 * (1) ARC Cache Maintenance ops only take Phy addr, hence special
698 * handling of kernel vaddr.
699 *
700 * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
701 * it still needs to handle a 2 page scenario, where the range
702 * straddles across 2 virtual pages and hence need for loop
703 */
704 while (tot_sz > 0) {
Vineet Guptac59414c2014-09-24 11:36:20 +0530705 unsigned int off, sz;
706 unsigned long phy, pfn;
707
Vineet Gupta95d69762013-01-18 15:12:19 +0530708 off = kstart % PAGE_SIZE;
709 pfn = vmalloc_to_pfn((void *)kstart);
710 phy = (pfn << PAGE_SHIFT) + off;
711 sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
Vineet Gupta94bad1a2013-04-12 12:20:23 +0530712 __sync_icache_dcache(phy, kstart, sz);
Vineet Gupta95d69762013-01-18 15:12:19 +0530713 kstart += sz;
714 tot_sz -= sz;
715 }
716}
Pranith Kumare3560302014-08-29 15:19:09 -0700717EXPORT_SYMBOL(flush_icache_range);
Vineet Gupta95d69762013-01-18 15:12:19 +0530718
719/*
Vineet Gupta94bad1a2013-04-12 12:20:23 +0530720 * General purpose helper to make I and D cache lines consistent.
721 * @paddr is phy addr of region
Vineet Gupta4b06ff32013-07-10 11:40:27 +0530722 * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
723 * However in one instance, when called by kprobe (for a breakpt in
Vineet Gupta94bad1a2013-04-12 12:20:23 +0530724 * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
725 * use a paddr to index the cache (despite VIPT). This is fine since since a
Vineet Gupta4b06ff32013-07-10 11:40:27 +0530726 * builtin kernel page will not have any virtual mappings.
727 * kprobe on loadable module will be kernel vaddr.
Vineet Gupta95d69762013-01-18 15:12:19 +0530728 */
Vineet Gupta94bad1a2013-04-12 12:20:23 +0530729void __sync_icache_dcache(unsigned long paddr, unsigned long vaddr, int len)
Vineet Gupta95d69762013-01-18 15:12:19 +0530730{
Vineet Guptaf5388812013-05-16 12:19:29 +0530731 __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
Vineet Gupta2328af02013-02-17 12:51:42 +0200732 __ic_line_inv_vaddr(paddr, vaddr, len);
Vineet Gupta95d69762013-01-18 15:12:19 +0530733}
734
Vineet Gupta24603fd2013-04-11 18:36:35 +0530735/* wrapper to compile time eliminate alignment checks in flush loop */
736void __inv_icache_page(unsigned long paddr, unsigned long vaddr)
Vineet Gupta95d69762013-01-18 15:12:19 +0530737{
Vineet Gupta24603fd2013-04-11 18:36:35 +0530738 __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
Vineet Gupta95d69762013-01-18 15:12:19 +0530739}
740
Vineet Gupta6ec18a82013-05-09 15:10:18 +0530741/*
742 * wrapper to clearout kernel or userspace mappings of a page
743 * For kernel mappings @vaddr == @paddr
744 */
Vineet Gupta45309492015-05-18 12:46:37 +0530745void __flush_dcache_page(unsigned long paddr, unsigned long vaddr)
Vineet Guptaeacd0e92013-04-16 14:10:48 +0530746{
Vineet Gupta6ec18a82013-05-09 15:10:18 +0530747 __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
Vineet Guptaeacd0e92013-04-16 14:10:48 +0530748}
749
Vineet Gupta95d69762013-01-18 15:12:19 +0530750noinline void flush_cache_all(void)
751{
752 unsigned long flags;
753
754 local_irq_save(flags);
755
Vineet Gupta336e1992013-06-22 19:22:42 +0530756 __ic_entire_inv();
Vineet Gupta95d69762013-01-18 15:12:19 +0530757 __dc_entire_op(OP_FLUSH_N_INV);
758
759 local_irq_restore(flags);
760
761}
762
Vineet Gupta4102b532013-05-09 21:54:51 +0530763#ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
764
765void flush_cache_mm(struct mm_struct *mm)
766{
767 flush_cache_all();
768}
769
770void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
771 unsigned long pfn)
772{
773 unsigned int paddr = pfn << PAGE_SHIFT;
774
Vineet Gupta5971bc72013-05-16 12:23:31 +0530775 u_vaddr &= PAGE_MASK;
776
Vineet Gupta45309492015-05-18 12:46:37 +0530777 __flush_dcache_page(paddr, u_vaddr);
Vineet Gupta5971bc72013-05-16 12:23:31 +0530778
779 if (vma->vm_flags & VM_EXEC)
780 __inv_icache_page(paddr, u_vaddr);
Vineet Gupta4102b532013-05-09 21:54:51 +0530781}
782
783void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
784 unsigned long end)
785{
786 flush_cache_all();
787}
788
Vineet Gupta7bb66f62013-05-25 14:04:25 +0530789void flush_anon_page(struct vm_area_struct *vma, struct page *page,
790 unsigned long u_vaddr)
791{
792 /* TBD: do we really need to clear the kernel mapping */
793 __flush_dcache_page(page_address(page), u_vaddr);
794 __flush_dcache_page(page_address(page), page_address(page));
795
796}
797
798#endif
799
Vineet Gupta4102b532013-05-09 21:54:51 +0530800void copy_user_highpage(struct page *to, struct page *from,
801 unsigned long u_vaddr, struct vm_area_struct *vma)
802{
Vineet Gupta45309492015-05-18 12:46:37 +0530803 unsigned long kfrom = (unsigned long)page_address(from);
804 unsigned long kto = (unsigned long)page_address(to);
Vineet Gupta4102b532013-05-09 21:54:51 +0530805 int clean_src_k_mappings = 0;
806
807 /*
808 * If SRC page was already mapped in userspace AND it's U-mapping is
809 * not congruent with K-mapping, sync former to physical page so that
810 * K-mapping in memcpy below, sees the right data
811 *
812 * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
813 * equally valid for SRC page as well
814 */
815 if (page_mapped(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
816 __flush_dcache_page(kfrom, u_vaddr);
817 clean_src_k_mappings = 1;
818 }
819
Vineet Gupta45309492015-05-18 12:46:37 +0530820 copy_page((void *)kto, (void *)kfrom);
Vineet Gupta4102b532013-05-09 21:54:51 +0530821
822 /*
823 * Mark DST page K-mapping as dirty for a later finalization by
824 * update_mmu_cache(). Although the finalization could have been done
825 * here as well (given that both vaddr/paddr are available).
826 * But update_mmu_cache() already has code to do that for other
827 * non copied user pages (e.g. read faults which wire in pagecache page
828 * directly).
829 */
Vineet Gupta2ed21da2013-05-13 17:23:58 +0530830 clear_bit(PG_dc_clean, &to->flags);
Vineet Gupta4102b532013-05-09 21:54:51 +0530831
832 /*
833 * if SRC was already usermapped and non-congruent to kernel mapping
834 * sync the kernel mapping back to physical page
835 */
836 if (clean_src_k_mappings) {
837 __flush_dcache_page(kfrom, kfrom);
Vineet Gupta2ed21da2013-05-13 17:23:58 +0530838 set_bit(PG_dc_clean, &from->flags);
Vineet Gupta4102b532013-05-09 21:54:51 +0530839 } else {
Vineet Gupta2ed21da2013-05-13 17:23:58 +0530840 clear_bit(PG_dc_clean, &from->flags);
Vineet Gupta4102b532013-05-09 21:54:51 +0530841 }
842}
843
844void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
845{
846 clear_page(to);
Vineet Gupta2ed21da2013-05-13 17:23:58 +0530847 clear_bit(PG_dc_clean, &page->flags);
Vineet Gupta4102b532013-05-09 21:54:51 +0530848}
849
Vineet Gupta4102b532013-05-09 21:54:51 +0530850
Vineet Gupta95d69762013-01-18 15:12:19 +0530851/**********************************************************************
852 * Explicit Cache flush request from user space via syscall
853 * Needed for JITs which generate code on the fly
854 */
855SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
856{
857 /* TBD: optimize this */
858 flush_cache_all();
859 return 0;
860}
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530861
862void arc_cache_init(void)
863{
864 unsigned int __maybe_unused cpu = smp_processor_id();
865 char str[256];
866
867 printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
868
869 if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
870 struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
871
872 if (!ic->ver)
873 panic("cache support enabled but non-existent cache\n");
874
875 if (ic->line_len != L1_CACHE_BYTES)
876 panic("ICache line [%d] != kernel Config [%d]",
877 ic->line_len, L1_CACHE_BYTES);
878
879 if (ic->ver != CONFIG_ARC_MMU_VER)
880 panic("Cache ver [%d] doesn't match MMU ver [%d]\n",
881 ic->ver, CONFIG_ARC_MMU_VER);
Vineet Guptabcc4d652015-06-04 14:39:15 +0530882
883 /*
884 * In MMU v4 (HS38x) the alising icache config uses IVIL/PTAG
885 * pair to provide vaddr/paddr respectively, just as in MMU v3
886 */
887 if (is_isa_arcv2() && ic->alias)
888 _cache_line_loop_ic_fn = __cache_line_loop_v3;
889 else
890 _cache_line_loop_ic_fn = __cache_line_loop;
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530891 }
892
893 if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
894 struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530895
896 if (!dc->ver)
897 panic("cache support enabled but non-existent cache\n");
898
899 if (dc->line_len != L1_CACHE_BYTES)
900 panic("DCache line [%d] != kernel Config [%d]",
901 dc->line_len, L1_CACHE_BYTES);
902
Vineet Guptad1f317d2015-04-06 17:23:57 +0530903 /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
904 if (is_isa_arcompact()) {
905 int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530906
Vineet Guptad1f317d2015-04-06 17:23:57 +0530907 if (dc->alias && !handled)
908 panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
909 else if (!dc->alias && handled)
910 panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
911 }
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530912 }
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300913
Vineet Gupta79335a22015-06-04 18:30:23 +0530914 if (is_isa_arcv2() && l2_line_sz && !slc_enable) {
915
916 /* IM set : flush before invalidate */
917 write_aux_reg(ARC_REG_SLC_CTRL,
918 read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_IM);
919
920 write_aux_reg(ARC_REG_SLC_INVALIDATE, 1);
921
922 /* Important to wait for flush to complete */
923 while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
924 write_aux_reg(ARC_REG_SLC_CTRL,
925 read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_DISABLE);
926 }
927
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300928 if (is_isa_arcv2() && ioc_exists) {
929 /* IO coherency base - 0x8z */
930 write_aux_reg(ARC_REG_IO_COH_AP0_BASE, 0x80000);
931 /* IO coherency aperture size - 512Mb: 0x8z-0xAz */
932 write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, 0x11);
933 /* Enable partial writes */
934 write_aux_reg(ARC_REG_IO_COH_PARTIAL, 1);
935 /* Enable IO coherency */
936 write_aux_reg(ARC_REG_IO_COH_ENABLE, 1);
937
938 __dma_cache_wback_inv = __dma_cache_wback_inv_ioc;
939 __dma_cache_inv = __dma_cache_inv_ioc;
940 __dma_cache_wback = __dma_cache_wback_ioc;
Vineet Gupta79335a22015-06-04 18:30:23 +0530941 } else if (is_isa_arcv2() && l2_line_sz && slc_enable) {
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300942 __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
943 __dma_cache_inv = __dma_cache_inv_slc;
944 __dma_cache_wback = __dma_cache_wback_slc;
945 } else {
946 __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
947 __dma_cache_inv = __dma_cache_inv_l1;
948 __dma_cache_wback = __dma_cache_wback_l1;
949 }
Vineet Gupta8ea2ddf2015-06-04 15:35:53 +0530950}