cache_arc700.c 23 KB

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