cache.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * ARC Cache Management
  3. *
  4. * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
  5. * 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.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/mm.h>
  13. #include <linux/sched.h>
  14. #include <linux/cache.h>
  15. #include <linux/mmu_context.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/pagemap.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/cachectl.h>
  21. #include <asm/setup.h>
  22. static int l2_line_sz;
  23. static int ioc_exists;
  24. int slc_enable = 1, ioc_enable = 1;
  25. unsigned long perip_base = ARC_UNCACHED_ADDR_SPACE; /* legacy value for boot */
  26. unsigned long perip_end = 0xFFFFFFFF; /* legacy value */
  27. void (*_cache_line_loop_ic_fn)(phys_addr_t paddr, unsigned long vaddr,
  28. unsigned long sz, const int cacheop);
  29. void (*__dma_cache_wback_inv)(phys_addr_t start, unsigned long sz);
  30. void (*__dma_cache_inv)(phys_addr_t start, unsigned long sz);
  31. void (*__dma_cache_wback)(phys_addr_t start, unsigned long sz);
  32. char *arc_cache_mumbojumbo(int c, char *buf, int len)
  33. {
  34. int n = 0;
  35. struct cpuinfo_arc_cache *p;
  36. #define PR_CACHE(p, cfg, str) \
  37. if (!(p)->ver) \
  38. n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \
  39. else \
  40. n += scnprintf(buf + n, len - n, \
  41. str"\t\t: %uK, %dway/set, %uB Line, %s%s%s\n", \
  42. (p)->sz_k, (p)->assoc, (p)->line_len, \
  43. (p)->vipt ? "VIPT" : "PIPT", \
  44. (p)->alias ? " aliasing" : "", \
  45. IS_USED_CFG(cfg));
  46. PR_CACHE(&cpuinfo_arc700[c].icache, CONFIG_ARC_HAS_ICACHE, "I-Cache");
  47. PR_CACHE(&cpuinfo_arc700[c].dcache, CONFIG_ARC_HAS_DCACHE, "D-Cache");
  48. p = &cpuinfo_arc700[c].slc;
  49. if (p->ver)
  50. n += scnprintf(buf + n, len - n,
  51. "SLC\t\t: %uK, %uB Line%s\n",
  52. p->sz_k, p->line_len, IS_USED_RUN(slc_enable));
  53. n += scnprintf(buf + n, len - n, "Peripherals\t: %#lx%s%s\n",
  54. perip_base,
  55. IS_AVAIL3(ioc_exists, ioc_enable, ", IO-Coherency "));
  56. return buf;
  57. }
  58. /*
  59. * Read the Cache Build Confuration Registers, Decode them and save into
  60. * the cpuinfo structure for later use.
  61. * No Validation done here, simply read/convert the BCRs
  62. */
  63. static void read_decode_cache_bcr_arcv2(int cpu)
  64. {
  65. struct cpuinfo_arc_cache *p_slc = &cpuinfo_arc700[cpu].slc;
  66. struct bcr_generic sbcr;
  67. struct bcr_slc_cfg {
  68. #ifdef CONFIG_CPU_BIG_ENDIAN
  69. unsigned int pad:24, way:2, lsz:2, sz:4;
  70. #else
  71. unsigned int sz:4, lsz:2, way:2, pad:24;
  72. #endif
  73. } slc_cfg;
  74. struct bcr_clust_cfg {
  75. #ifdef CONFIG_CPU_BIG_ENDIAN
  76. unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8;
  77. #else
  78. unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7;
  79. #endif
  80. } cbcr;
  81. struct bcr_volatile {
  82. #ifdef CONFIG_CPU_BIG_ENDIAN
  83. unsigned int start:4, limit:4, pad:22, order:1, disable:1;
  84. #else
  85. unsigned int disable:1, order:1, pad:22, limit:4, start:4;
  86. #endif
  87. } vol;
  88. READ_BCR(ARC_REG_SLC_BCR, sbcr);
  89. if (sbcr.ver) {
  90. READ_BCR(ARC_REG_SLC_CFG, slc_cfg);
  91. p_slc->ver = sbcr.ver;
  92. p_slc->sz_k = 128 << slc_cfg.sz;
  93. l2_line_sz = p_slc->line_len = (slc_cfg.lsz == 0) ? 128 : 64;
  94. }
  95. READ_BCR(ARC_REG_CLUSTER_BCR, cbcr);
  96. if (cbcr.c)
  97. ioc_exists = 1;
  98. else
  99. ioc_enable = 0;
  100. /* HS 2.0 didn't have AUX_VOL */
  101. if (cpuinfo_arc700[cpu].core.family > 0x51) {
  102. READ_BCR(AUX_VOL, vol);
  103. perip_base = vol.start << 28;
  104. /* HS 3.0 has limit and strict-ordering fields */
  105. if (cpuinfo_arc700[cpu].core.family > 0x52)
  106. perip_end = (vol.limit << 28) - 1;
  107. }
  108. }
  109. void read_decode_cache_bcr(void)
  110. {
  111. struct cpuinfo_arc_cache *p_ic, *p_dc;
  112. unsigned int cpu = smp_processor_id();
  113. struct bcr_cache {
  114. #ifdef CONFIG_CPU_BIG_ENDIAN
  115. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  116. #else
  117. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  118. #endif
  119. } ibcr, dbcr;
  120. p_ic = &cpuinfo_arc700[cpu].icache;
  121. READ_BCR(ARC_REG_IC_BCR, ibcr);
  122. if (!ibcr.ver)
  123. goto dc_chk;
  124. if (ibcr.ver <= 3) {
  125. BUG_ON(ibcr.config != 3);
  126. p_ic->assoc = 2; /* Fixed to 2w set assoc */
  127. } else if (ibcr.ver >= 4) {
  128. p_ic->assoc = 1 << ibcr.config; /* 1,2,4,8 */
  129. }
  130. p_ic->line_len = 8 << ibcr.line_len;
  131. p_ic->sz_k = 1 << (ibcr.sz - 1);
  132. p_ic->ver = ibcr.ver;
  133. p_ic->vipt = 1;
  134. p_ic->alias = p_ic->sz_k/p_ic->assoc/TO_KB(PAGE_SIZE) > 1;
  135. dc_chk:
  136. p_dc = &cpuinfo_arc700[cpu].dcache;
  137. READ_BCR(ARC_REG_DC_BCR, dbcr);
  138. if (!dbcr.ver)
  139. goto slc_chk;
  140. if (dbcr.ver <= 3) {
  141. BUG_ON(dbcr.config != 2);
  142. p_dc->assoc = 4; /* Fixed to 4w set assoc */
  143. p_dc->vipt = 1;
  144. p_dc->alias = p_dc->sz_k/p_dc->assoc/TO_KB(PAGE_SIZE) > 1;
  145. } else if (dbcr.ver >= 4) {
  146. p_dc->assoc = 1 << dbcr.config; /* 1,2,4,8 */
  147. p_dc->vipt = 0;
  148. p_dc->alias = 0; /* PIPT so can't VIPT alias */
  149. }
  150. p_dc->line_len = 16 << dbcr.line_len;
  151. p_dc->sz_k = 1 << (dbcr.sz - 1);
  152. p_dc->ver = dbcr.ver;
  153. slc_chk:
  154. if (is_isa_arcv2())
  155. read_decode_cache_bcr_arcv2(cpu);
  156. }
  157. /*
  158. * Line Operation on {I,D}-Cache
  159. */
  160. #define OP_INV 0x1
  161. #define OP_FLUSH 0x2
  162. #define OP_FLUSH_N_INV 0x3
  163. #define OP_INV_IC 0x4
  164. /*
  165. * I-Cache Aliasing in ARC700 VIPT caches (MMU v1-v3)
  166. *
  167. * ARC VIPT I-cache uses vaddr to index into cache and paddr to match the tag.
  168. * The orig Cache Management Module "CDU" only required paddr to invalidate a
  169. * certain line since it sufficed as index in Non-Aliasing VIPT cache-geometry.
  170. * Infact for distinct V1,V2,P: all of {V1-P},{V2-P},{P-P} would end up fetching
  171. * the exact same line.
  172. *
  173. * However for larger Caches (way-size > page-size) - i.e. in Aliasing config,
  174. * paddr alone could not be used to correctly index the cache.
  175. *
  176. * ------------------
  177. * MMU v1/v2 (Fixed Page Size 8k)
  178. * ------------------
  179. * The solution was to provide CDU with these additonal vaddr bits. These
  180. * would be bits [x:13], x would depend on cache-geometry, 13 comes from
  181. * standard page size of 8k.
  182. * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits
  183. * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the
  184. * orig 5 bits of paddr were anyways ignored by CDU line ops, as they
  185. * represent the offset within cache-line. The adv of using this "clumsy"
  186. * interface for additional info was no new reg was needed in CDU programming
  187. * model.
  188. *
  189. * 17:13 represented the max num of bits passable, actual bits needed were
  190. * fewer, based on the num-of-aliases possible.
  191. * -for 2 alias possibility, only bit 13 needed (32K cache)
  192. * -for 4 alias possibility, bits 14:13 needed (64K cache)
  193. *
  194. * ------------------
  195. * MMU v3
  196. * ------------------
  197. * This ver of MMU supports variable page sizes (1k-16k): although Linux will
  198. * only support 8k (default), 16k and 4k.
  199. * However from hardware perspective, smaller page sizes aggravate aliasing
  200. * meaning more vaddr bits needed to disambiguate the cache-line-op ;
  201. * the existing scheme of piggybacking won't work for certain configurations.
  202. * Two new registers IC_PTAG and DC_PTAG inttoduced.
  203. * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs
  204. */
  205. static inline
  206. void __cache_line_loop_v2(phys_addr_t paddr, unsigned long vaddr,
  207. unsigned long sz, const int op)
  208. {
  209. unsigned int aux_cmd;
  210. int num_lines;
  211. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  212. if (op == OP_INV_IC) {
  213. aux_cmd = ARC_REG_IC_IVIL;
  214. } else {
  215. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  216. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  217. }
  218. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  219. * and have @paddr - aligned to cache line and integral @num_lines.
  220. * This however can be avoided for page sized since:
  221. * -@paddr will be cache-line aligned already (being page aligned)
  222. * -@sz will be integral multiple of line size (being page sized).
  223. */
  224. if (!full_page) {
  225. sz += paddr & ~CACHE_LINE_MASK;
  226. paddr &= CACHE_LINE_MASK;
  227. vaddr &= CACHE_LINE_MASK;
  228. }
  229. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  230. /* MMUv2 and before: paddr contains stuffed vaddrs bits */
  231. paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
  232. while (num_lines-- > 0) {
  233. write_aux_reg(aux_cmd, paddr);
  234. paddr += L1_CACHE_BYTES;
  235. }
  236. }
  237. /*
  238. * For ARC700 MMUv3 I-cache and D-cache flushes
  239. * Also reused for HS38 aliasing I-cache configuration
  240. */
  241. static inline
  242. void __cache_line_loop_v3(phys_addr_t paddr, unsigned long vaddr,
  243. unsigned long sz, const int op)
  244. {
  245. unsigned int aux_cmd, aux_tag;
  246. int num_lines;
  247. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  248. if (op == OP_INV_IC) {
  249. aux_cmd = ARC_REG_IC_IVIL;
  250. aux_tag = ARC_REG_IC_PTAG;
  251. } else {
  252. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  253. aux_tag = ARC_REG_DC_PTAG;
  254. }
  255. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  256. * and have @paddr - aligned to cache line and integral @num_lines.
  257. * This however can be avoided for page sized since:
  258. * -@paddr will be cache-line aligned already (being page aligned)
  259. * -@sz will be integral multiple of line size (being page sized).
  260. */
  261. if (!full_page) {
  262. sz += paddr & ~CACHE_LINE_MASK;
  263. paddr &= CACHE_LINE_MASK;
  264. vaddr &= CACHE_LINE_MASK;
  265. }
  266. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  267. /*
  268. * MMUv3, cache ops require paddr in PTAG reg
  269. * if V-P const for loop, PTAG can be written once outside loop
  270. */
  271. if (full_page)
  272. write_aux_reg(aux_tag, paddr);
  273. /*
  274. * This is technically for MMU v4, using the MMU v3 programming model
  275. * Special work for HS38 aliasing I-cache configuration with PAE40
  276. * - upper 8 bits of paddr need to be written into PTAG_HI
  277. * - (and needs to be written before the lower 32 bits)
  278. * Note that PTAG_HI is hoisted outside the line loop
  279. */
  280. if (is_pae40_enabled() && op == OP_INV_IC)
  281. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  282. while (num_lines-- > 0) {
  283. if (!full_page) {
  284. write_aux_reg(aux_tag, paddr);
  285. paddr += L1_CACHE_BYTES;
  286. }
  287. write_aux_reg(aux_cmd, vaddr);
  288. vaddr += L1_CACHE_BYTES;
  289. }
  290. }
  291. /*
  292. * In HS38x (MMU v4), I-cache is VIPT (can alias), D-cache is PIPT
  293. * Here's how cache ops are implemented
  294. *
  295. * - D-cache: only paddr needed (in DC_IVDL/DC_FLDL)
  296. * - I-cache Non Aliasing: Despite VIPT, only paddr needed (in IC_IVIL)
  297. * - I-cache Aliasing: Both vaddr and paddr needed (in IC_IVIL, IC_PTAG
  298. * respectively, similar to MMU v3 programming model, hence
  299. * __cache_line_loop_v3() is used)
  300. *
  301. * If PAE40 is enabled, independent of aliasing considerations, the higher bits
  302. * needs to be written into PTAG_HI
  303. */
  304. static inline
  305. void __cache_line_loop_v4(phys_addr_t 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. if (cacheop == OP_INV_IC) {
  312. aux_cmd = ARC_REG_IC_IVIL;
  313. } else {
  314. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  315. aux_cmd = cacheop & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  316. }
  317. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  318. * and have @paddr - aligned to cache line and integral @num_lines.
  319. * This however can be avoided for page sized since:
  320. * -@paddr will be cache-line aligned already (being page aligned)
  321. * -@sz will be integral multiple of line size (being page sized).
  322. */
  323. if (!full_page_op) {
  324. sz += paddr & ~CACHE_LINE_MASK;
  325. paddr &= CACHE_LINE_MASK;
  326. }
  327. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  328. /*
  329. * For HS38 PAE40 configuration
  330. * - upper 8 bits of paddr need to be written into PTAG_HI
  331. * - (and needs to be written before the lower 32 bits)
  332. */
  333. if (is_pae40_enabled()) {
  334. if (cacheop == OP_INV_IC)
  335. /*
  336. * Non aliasing I-cache in HS38,
  337. * aliasing I-cache handled in __cache_line_loop_v3()
  338. */
  339. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  340. else
  341. write_aux_reg(ARC_REG_DC_PTAG_HI, (u64)paddr >> 32);
  342. }
  343. while (num_lines-- > 0) {
  344. write_aux_reg(aux_cmd, paddr);
  345. paddr += L1_CACHE_BYTES;
  346. }
  347. }
  348. #if (CONFIG_ARC_MMU_VER < 3)
  349. #define __cache_line_loop __cache_line_loop_v2
  350. #elif (CONFIG_ARC_MMU_VER == 3)
  351. #define __cache_line_loop __cache_line_loop_v3
  352. #elif (CONFIG_ARC_MMU_VER > 3)
  353. #define __cache_line_loop __cache_line_loop_v4
  354. #endif
  355. #ifdef CONFIG_ARC_HAS_DCACHE
  356. /***************************************************************
  357. * Machine specific helpers for Entire D-Cache or Per Line ops
  358. */
  359. static inline void __before_dc_op(const int op)
  360. {
  361. if (op == OP_FLUSH_N_INV) {
  362. /* Dcache provides 2 cmd: FLUSH or INV
  363. * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
  364. * flush-n-inv is achieved by INV cmd but with IM=1
  365. * So toggle INV sub-mode depending on op request and default
  366. */
  367. const unsigned int ctl = ARC_REG_DC_CTRL;
  368. write_aux_reg(ctl, read_aux_reg(ctl) | DC_CTRL_INV_MODE_FLUSH);
  369. }
  370. }
  371. static inline void __after_dc_op(const int op)
  372. {
  373. if (op & OP_FLUSH) {
  374. const unsigned int ctl = ARC_REG_DC_CTRL;
  375. unsigned int reg;
  376. /* flush / flush-n-inv both wait */
  377. while ((reg = read_aux_reg(ctl)) & DC_CTRL_FLUSH_STATUS)
  378. ;
  379. /* Switch back to default Invalidate mode */
  380. if (op == OP_FLUSH_N_INV)
  381. write_aux_reg(ctl, reg & ~DC_CTRL_INV_MODE_FLUSH);
  382. }
  383. }
  384. /*
  385. * Operation on Entire D-Cache
  386. * @op = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
  387. * Note that constant propagation ensures all the checks are gone
  388. * in generated code
  389. */
  390. static inline void __dc_entire_op(const int op)
  391. {
  392. int aux;
  393. __before_dc_op(op);
  394. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  395. aux = ARC_REG_DC_IVDC;
  396. else
  397. aux = ARC_REG_DC_FLSH;
  398. write_aux_reg(aux, 0x1);
  399. __after_dc_op(op);
  400. }
  401. /* For kernel mappings cache operation: index is same as paddr */
  402. #define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op)
  403. /*
  404. * D-Cache Line ops: Per Line INV (discard or wback+discard) or FLUSH (wback)
  405. */
  406. static inline void __dc_line_op(phys_addr_t paddr, unsigned long vaddr,
  407. unsigned long sz, const int op)
  408. {
  409. unsigned long flags;
  410. local_irq_save(flags);
  411. __before_dc_op(op);
  412. __cache_line_loop(paddr, vaddr, sz, op);
  413. __after_dc_op(op);
  414. local_irq_restore(flags);
  415. }
  416. #else
  417. #define __dc_entire_op(op)
  418. #define __dc_line_op(paddr, vaddr, sz, op)
  419. #define __dc_line_op_k(paddr, sz, op)
  420. #endif /* CONFIG_ARC_HAS_DCACHE */
  421. #ifdef CONFIG_ARC_HAS_ICACHE
  422. static inline void __ic_entire_inv(void)
  423. {
  424. write_aux_reg(ARC_REG_IC_IVIC, 1);
  425. read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
  426. }
  427. static inline void
  428. __ic_line_inv_vaddr_local(phys_addr_t paddr, unsigned long vaddr,
  429. unsigned long sz)
  430. {
  431. unsigned long flags;
  432. local_irq_save(flags);
  433. (*_cache_line_loop_ic_fn)(paddr, vaddr, sz, OP_INV_IC);
  434. local_irq_restore(flags);
  435. }
  436. #ifndef CONFIG_SMP
  437. #define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s)
  438. #else
  439. struct ic_inv_args {
  440. phys_addr_t paddr, vaddr;
  441. int sz;
  442. };
  443. static void __ic_line_inv_vaddr_helper(void *info)
  444. {
  445. struct ic_inv_args *ic_inv = info;
  446. __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
  447. }
  448. static void __ic_line_inv_vaddr(phys_addr_t paddr, unsigned long vaddr,
  449. unsigned long sz)
  450. {
  451. struct ic_inv_args ic_inv = {
  452. .paddr = paddr,
  453. .vaddr = vaddr,
  454. .sz = sz
  455. };
  456. on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
  457. }
  458. #endif /* CONFIG_SMP */
  459. #else /* !CONFIG_ARC_HAS_ICACHE */
  460. #define __ic_entire_inv()
  461. #define __ic_line_inv_vaddr(pstart, vstart, sz)
  462. #endif /* CONFIG_ARC_HAS_ICACHE */
  463. noinline void slc_op(phys_addr_t paddr, unsigned long sz, const int op)
  464. {
  465. #ifdef CONFIG_ISA_ARCV2
  466. /*
  467. * SLC is shared between all cores and concurrent aux operations from
  468. * multiple cores need to be serialized using a spinlock
  469. * A concurrent operation can be silently ignored and/or the old/new
  470. * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
  471. * below)
  472. */
  473. static DEFINE_SPINLOCK(lock);
  474. unsigned long flags;
  475. unsigned int ctrl;
  476. spin_lock_irqsave(&lock, flags);
  477. /*
  478. * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
  479. * - b'000 (default) is Flush,
  480. * - b'001 is Invalidate if CTRL.IM == 0
  481. * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
  482. */
  483. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  484. /* Don't rely on default value of IM bit */
  485. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  486. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  487. else
  488. ctrl |= SLC_CTRL_IM;
  489. if (op & OP_INV)
  490. ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
  491. else
  492. ctrl &= ~SLC_CTRL_RGN_OP_INV;
  493. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  494. /*
  495. * Lower bits are ignored, no need to clip
  496. * END needs to be setup before START (latter triggers the operation)
  497. * END can't be same as START, so add (l2_line_sz - 1) to sz
  498. */
  499. write_aux_reg(ARC_REG_SLC_RGN_END, (paddr + sz + l2_line_sz - 1));
  500. write_aux_reg(ARC_REG_SLC_RGN_START, paddr);
  501. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  502. spin_unlock_irqrestore(&lock, flags);
  503. #endif
  504. }
  505. /***********************************************************
  506. * Exported APIs
  507. */
  508. /*
  509. * Handle cache congruency of kernel and userspace mappings of page when kernel
  510. * writes-to/reads-from
  511. *
  512. * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
  513. * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
  514. * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
  515. * -In SMP, if hardware caches are coherent
  516. *
  517. * There's a corollary case, where kernel READs from a userspace mapped page.
  518. * If the U-mapping is not congruent to to K-mapping, former needs flushing.
  519. */
  520. void flush_dcache_page(struct page *page)
  521. {
  522. struct address_space *mapping;
  523. if (!cache_is_vipt_aliasing()) {
  524. clear_bit(PG_dc_clean, &page->flags);
  525. return;
  526. }
  527. /* don't handle anon pages here */
  528. mapping = page_mapping(page);
  529. if (!mapping)
  530. return;
  531. /*
  532. * pagecache page, file not yet mapped to userspace
  533. * Make a note that K-mapping is dirty
  534. */
  535. if (!mapping_mapped(mapping)) {
  536. clear_bit(PG_dc_clean, &page->flags);
  537. } else if (page_mapcount(page)) {
  538. /* kernel reading from page with U-mapping */
  539. phys_addr_t paddr = (unsigned long)page_address(page);
  540. unsigned long vaddr = page->index << PAGE_SHIFT;
  541. if (addr_not_cache_congruent(paddr, vaddr))
  542. __flush_dcache_page(paddr, vaddr);
  543. }
  544. }
  545. EXPORT_SYMBOL(flush_dcache_page);
  546. /*
  547. * DMA ops for systems with L1 cache only
  548. * Make memory coherent with L1 cache by flushing/invalidating L1 lines
  549. */
  550. static void __dma_cache_wback_inv_l1(phys_addr_t start, unsigned long sz)
  551. {
  552. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  553. }
  554. static void __dma_cache_inv_l1(phys_addr_t start, unsigned long sz)
  555. {
  556. __dc_line_op_k(start, sz, OP_INV);
  557. }
  558. static void __dma_cache_wback_l1(phys_addr_t start, unsigned long sz)
  559. {
  560. __dc_line_op_k(start, sz, OP_FLUSH);
  561. }
  562. /*
  563. * DMA ops for systems with both L1 and L2 caches, but without IOC
  564. * Both L1 and L2 lines need to be explicitly flushed/invalidated
  565. */
  566. static void __dma_cache_wback_inv_slc(phys_addr_t start, unsigned long sz)
  567. {
  568. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  569. slc_op(start, sz, OP_FLUSH_N_INV);
  570. }
  571. static void __dma_cache_inv_slc(phys_addr_t start, unsigned long sz)
  572. {
  573. __dc_line_op_k(start, sz, OP_INV);
  574. slc_op(start, sz, OP_INV);
  575. }
  576. static void __dma_cache_wback_slc(phys_addr_t start, unsigned long sz)
  577. {
  578. __dc_line_op_k(start, sz, OP_FLUSH);
  579. slc_op(start, sz, OP_FLUSH);
  580. }
  581. /*
  582. * DMA ops for systems with IOC
  583. * IOC hardware snoops all DMA traffic keeping the caches consistent with
  584. * memory - eliding need for any explicit cache maintenance of DMA buffers
  585. */
  586. static void __dma_cache_wback_inv_ioc(phys_addr_t start, unsigned long sz) {}
  587. static void __dma_cache_inv_ioc(phys_addr_t start, unsigned long sz) {}
  588. static void __dma_cache_wback_ioc(phys_addr_t start, unsigned long sz) {}
  589. /*
  590. * Exported DMA API
  591. */
  592. void dma_cache_wback_inv(phys_addr_t start, unsigned long sz)
  593. {
  594. __dma_cache_wback_inv(start, sz);
  595. }
  596. EXPORT_SYMBOL(dma_cache_wback_inv);
  597. void dma_cache_inv(phys_addr_t start, unsigned long sz)
  598. {
  599. __dma_cache_inv(start, sz);
  600. }
  601. EXPORT_SYMBOL(dma_cache_inv);
  602. void dma_cache_wback(phys_addr_t start, unsigned long sz)
  603. {
  604. __dma_cache_wback(start, sz);
  605. }
  606. EXPORT_SYMBOL(dma_cache_wback);
  607. /*
  608. * This is API for making I/D Caches consistent when modifying
  609. * kernel code (loadable modules, kprobes, kgdb...)
  610. * This is called on insmod, with kernel virtual address for CODE of
  611. * the module. ARC cache maintenance ops require PHY address thus we
  612. * need to convert vmalloc addr to PHY addr
  613. */
  614. void flush_icache_range(unsigned long kstart, unsigned long kend)
  615. {
  616. unsigned int tot_sz;
  617. WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
  618. /* Shortcut for bigger flush ranges.
  619. * Here we don't care if this was kernel virtual or phy addr
  620. */
  621. tot_sz = kend - kstart;
  622. if (tot_sz > PAGE_SIZE) {
  623. flush_cache_all();
  624. return;
  625. }
  626. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  627. if (likely(kstart > PAGE_OFFSET)) {
  628. /*
  629. * The 2nd arg despite being paddr will be used to index icache
  630. * This is OK since no alternate virtual mappings will exist
  631. * given the callers for this case: kprobe/kgdb in built-in
  632. * kernel code only.
  633. */
  634. __sync_icache_dcache(kstart, kstart, kend - kstart);
  635. return;
  636. }
  637. /*
  638. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  639. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  640. * handling of kernel vaddr.
  641. *
  642. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  643. * it still needs to handle a 2 page scenario, where the range
  644. * straddles across 2 virtual pages and hence need for loop
  645. */
  646. while (tot_sz > 0) {
  647. unsigned int off, sz;
  648. unsigned long phy, pfn;
  649. off = kstart % PAGE_SIZE;
  650. pfn = vmalloc_to_pfn((void *)kstart);
  651. phy = (pfn << PAGE_SHIFT) + off;
  652. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  653. __sync_icache_dcache(phy, kstart, sz);
  654. kstart += sz;
  655. tot_sz -= sz;
  656. }
  657. }
  658. EXPORT_SYMBOL(flush_icache_range);
  659. /*
  660. * General purpose helper to make I and D cache lines consistent.
  661. * @paddr is phy addr of region
  662. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  663. * However in one instance, when called by kprobe (for a breakpt in
  664. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  665. * use a paddr to index the cache (despite VIPT). This is fine since since a
  666. * builtin kernel page will not have any virtual mappings.
  667. * kprobe on loadable module will be kernel vaddr.
  668. */
  669. void __sync_icache_dcache(phys_addr_t paddr, unsigned long vaddr, int len)
  670. {
  671. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  672. __ic_line_inv_vaddr(paddr, vaddr, len);
  673. }
  674. /* wrapper to compile time eliminate alignment checks in flush loop */
  675. void __inv_icache_page(phys_addr_t paddr, unsigned long vaddr)
  676. {
  677. __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
  678. }
  679. /*
  680. * wrapper to clearout kernel or userspace mappings of a page
  681. * For kernel mappings @vaddr == @paddr
  682. */
  683. void __flush_dcache_page(phys_addr_t paddr, unsigned long vaddr)
  684. {
  685. __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
  686. }
  687. noinline void flush_cache_all(void)
  688. {
  689. unsigned long flags;
  690. local_irq_save(flags);
  691. __ic_entire_inv();
  692. __dc_entire_op(OP_FLUSH_N_INV);
  693. local_irq_restore(flags);
  694. }
  695. #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
  696. void flush_cache_mm(struct mm_struct *mm)
  697. {
  698. flush_cache_all();
  699. }
  700. void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
  701. unsigned long pfn)
  702. {
  703. unsigned int paddr = pfn << PAGE_SHIFT;
  704. u_vaddr &= PAGE_MASK;
  705. __flush_dcache_page(paddr, u_vaddr);
  706. if (vma->vm_flags & VM_EXEC)
  707. __inv_icache_page(paddr, u_vaddr);
  708. }
  709. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  710. unsigned long end)
  711. {
  712. flush_cache_all();
  713. }
  714. void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  715. unsigned long u_vaddr)
  716. {
  717. /* TBD: do we really need to clear the kernel mapping */
  718. __flush_dcache_page(page_address(page), u_vaddr);
  719. __flush_dcache_page(page_address(page), page_address(page));
  720. }
  721. #endif
  722. void copy_user_highpage(struct page *to, struct page *from,
  723. unsigned long u_vaddr, struct vm_area_struct *vma)
  724. {
  725. void *kfrom = kmap_atomic(from);
  726. void *kto = kmap_atomic(to);
  727. int clean_src_k_mappings = 0;
  728. /*
  729. * If SRC page was already mapped in userspace AND it's U-mapping is
  730. * not congruent with K-mapping, sync former to physical page so that
  731. * K-mapping in memcpy below, sees the right data
  732. *
  733. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  734. * equally valid for SRC page as well
  735. *
  736. * For !VIPT cache, all of this gets compiled out as
  737. * addr_not_cache_congruent() is 0
  738. */
  739. if (page_mapcount(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  740. __flush_dcache_page((unsigned long)kfrom, u_vaddr);
  741. clean_src_k_mappings = 1;
  742. }
  743. copy_page(kto, kfrom);
  744. /*
  745. * Mark DST page K-mapping as dirty for a later finalization by
  746. * update_mmu_cache(). Although the finalization could have been done
  747. * here as well (given that both vaddr/paddr are available).
  748. * But update_mmu_cache() already has code to do that for other
  749. * non copied user pages (e.g. read faults which wire in pagecache page
  750. * directly).
  751. */
  752. clear_bit(PG_dc_clean, &to->flags);
  753. /*
  754. * if SRC was already usermapped and non-congruent to kernel mapping
  755. * sync the kernel mapping back to physical page
  756. */
  757. if (clean_src_k_mappings) {
  758. __flush_dcache_page((unsigned long)kfrom, (unsigned long)kfrom);
  759. set_bit(PG_dc_clean, &from->flags);
  760. } else {
  761. clear_bit(PG_dc_clean, &from->flags);
  762. }
  763. kunmap_atomic(kto);
  764. kunmap_atomic(kfrom);
  765. }
  766. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  767. {
  768. clear_page(to);
  769. clear_bit(PG_dc_clean, &page->flags);
  770. }
  771. /**********************************************************************
  772. * Explicit Cache flush request from user space via syscall
  773. * Needed for JITs which generate code on the fly
  774. */
  775. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  776. {
  777. /* TBD: optimize this */
  778. flush_cache_all();
  779. return 0;
  780. }
  781. void arc_cache_init(void)
  782. {
  783. unsigned int __maybe_unused cpu = smp_processor_id();
  784. char str[256];
  785. printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
  786. /*
  787. * Only master CPU needs to execute rest of function:
  788. * - Assume SMP so all cores will have same cache config so
  789. * any geomtry checks will be same for all
  790. * - IOC setup / dma callbacks only need to be setup once
  791. */
  792. if (cpu)
  793. return;
  794. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  795. struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
  796. if (!ic->ver)
  797. panic("cache support enabled but non-existent cache\n");
  798. if (ic->line_len != L1_CACHE_BYTES)
  799. panic("ICache line [%d] != kernel Config [%d]",
  800. ic->line_len, L1_CACHE_BYTES);
  801. if (ic->ver != CONFIG_ARC_MMU_VER)
  802. panic("Cache ver [%d] doesn't match MMU ver [%d]\n",
  803. ic->ver, CONFIG_ARC_MMU_VER);
  804. /*
  805. * In MMU v4 (HS38x) the aliasing icache config uses IVIL/PTAG
  806. * pair to provide vaddr/paddr respectively, just as in MMU v3
  807. */
  808. if (is_isa_arcv2() && ic->alias)
  809. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  810. else
  811. _cache_line_loop_ic_fn = __cache_line_loop;
  812. }
  813. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  814. struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
  815. if (!dc->ver)
  816. panic("cache support enabled but non-existent cache\n");
  817. if (dc->line_len != L1_CACHE_BYTES)
  818. panic("DCache line [%d] != kernel Config [%d]",
  819. dc->line_len, L1_CACHE_BYTES);
  820. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  821. if (is_isa_arcompact()) {
  822. int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
  823. if (dc->alias && !handled)
  824. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  825. else if (!dc->alias && handled)
  826. panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  827. }
  828. }
  829. if (is_isa_arcv2() && l2_line_sz && !slc_enable) {
  830. /* IM set : flush before invalidate */
  831. write_aux_reg(ARC_REG_SLC_CTRL,
  832. read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_IM);
  833. write_aux_reg(ARC_REG_SLC_INVALIDATE, 1);
  834. /* Important to wait for flush to complete */
  835. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  836. write_aux_reg(ARC_REG_SLC_CTRL,
  837. read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_DISABLE);
  838. }
  839. if (is_isa_arcv2() && ioc_enable) {
  840. /* IO coherency base - 0x8z */
  841. write_aux_reg(ARC_REG_IO_COH_AP0_BASE, 0x80000);
  842. /* IO coherency aperture size - 512Mb: 0x8z-0xAz */
  843. write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, 0x11);
  844. /* Enable partial writes */
  845. write_aux_reg(ARC_REG_IO_COH_PARTIAL, 1);
  846. /* Enable IO coherency */
  847. write_aux_reg(ARC_REG_IO_COH_ENABLE, 1);
  848. __dma_cache_wback_inv = __dma_cache_wback_inv_ioc;
  849. __dma_cache_inv = __dma_cache_inv_ioc;
  850. __dma_cache_wback = __dma_cache_wback_ioc;
  851. } else if (is_isa_arcv2() && l2_line_sz && slc_enable) {
  852. __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
  853. __dma_cache_inv = __dma_cache_inv_slc;
  854. __dma_cache_wback = __dma_cache_wback_slc;
  855. } else {
  856. __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
  857. __dma_cache_inv = __dma_cache_inv_l1;
  858. __dma_cache_wback = __dma_cache_wback_l1;
  859. }
  860. }