cache.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 = 0;
  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. phys_addr_t end;
  477. spin_lock_irqsave(&lock, flags);
  478. /*
  479. * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
  480. * - b'000 (default) is Flush,
  481. * - b'001 is Invalidate if CTRL.IM == 0
  482. * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
  483. */
  484. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  485. /* Don't rely on default value of IM bit */
  486. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  487. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  488. else
  489. ctrl |= SLC_CTRL_IM;
  490. if (op & OP_INV)
  491. ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
  492. else
  493. ctrl &= ~SLC_CTRL_RGN_OP_INV;
  494. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  495. /*
  496. * Lower bits are ignored, no need to clip
  497. * END needs to be setup before START (latter triggers the operation)
  498. * END can't be same as START, so add (l2_line_sz - 1) to sz
  499. */
  500. end = paddr + sz + l2_line_sz - 1;
  501. if (is_pae40_enabled())
  502. write_aux_reg(ARC_REG_SLC_RGN_END1, upper_32_bits(end));
  503. write_aux_reg(ARC_REG_SLC_RGN_END, lower_32_bits(end));
  504. if (is_pae40_enabled())
  505. write_aux_reg(ARC_REG_SLC_RGN_START1, upper_32_bits(paddr));
  506. write_aux_reg(ARC_REG_SLC_RGN_START, lower_32_bits(paddr));
  507. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  508. spin_unlock_irqrestore(&lock, flags);
  509. #endif
  510. }
  511. /***********************************************************
  512. * Exported APIs
  513. */
  514. /*
  515. * Handle cache congruency of kernel and userspace mappings of page when kernel
  516. * writes-to/reads-from
  517. *
  518. * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
  519. * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
  520. * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
  521. * -In SMP, if hardware caches are coherent
  522. *
  523. * There's a corollary case, where kernel READs from a userspace mapped page.
  524. * If the U-mapping is not congruent to to K-mapping, former needs flushing.
  525. */
  526. void flush_dcache_page(struct page *page)
  527. {
  528. struct address_space *mapping;
  529. if (!cache_is_vipt_aliasing()) {
  530. clear_bit(PG_dc_clean, &page->flags);
  531. return;
  532. }
  533. /* don't handle anon pages here */
  534. mapping = page_mapping(page);
  535. if (!mapping)
  536. return;
  537. /*
  538. * pagecache page, file not yet mapped to userspace
  539. * Make a note that K-mapping is dirty
  540. */
  541. if (!mapping_mapped(mapping)) {
  542. clear_bit(PG_dc_clean, &page->flags);
  543. } else if (page_mapcount(page)) {
  544. /* kernel reading from page with U-mapping */
  545. phys_addr_t paddr = (unsigned long)page_address(page);
  546. unsigned long vaddr = page->index << PAGE_SHIFT;
  547. if (addr_not_cache_congruent(paddr, vaddr))
  548. __flush_dcache_page(paddr, vaddr);
  549. }
  550. }
  551. EXPORT_SYMBOL(flush_dcache_page);
  552. /*
  553. * DMA ops for systems with L1 cache only
  554. * Make memory coherent with L1 cache by flushing/invalidating L1 lines
  555. */
  556. static void __dma_cache_wback_inv_l1(phys_addr_t start, unsigned long sz)
  557. {
  558. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  559. }
  560. static void __dma_cache_inv_l1(phys_addr_t start, unsigned long sz)
  561. {
  562. __dc_line_op_k(start, sz, OP_INV);
  563. }
  564. static void __dma_cache_wback_l1(phys_addr_t start, unsigned long sz)
  565. {
  566. __dc_line_op_k(start, sz, OP_FLUSH);
  567. }
  568. /*
  569. * DMA ops for systems with both L1 and L2 caches, but without IOC
  570. * Both L1 and L2 lines need to be explicitly flushed/invalidated
  571. */
  572. static void __dma_cache_wback_inv_slc(phys_addr_t start, unsigned long sz)
  573. {
  574. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  575. slc_op(start, sz, OP_FLUSH_N_INV);
  576. }
  577. static void __dma_cache_inv_slc(phys_addr_t start, unsigned long sz)
  578. {
  579. __dc_line_op_k(start, sz, OP_INV);
  580. slc_op(start, sz, OP_INV);
  581. }
  582. static void __dma_cache_wback_slc(phys_addr_t start, unsigned long sz)
  583. {
  584. __dc_line_op_k(start, sz, OP_FLUSH);
  585. slc_op(start, sz, OP_FLUSH);
  586. }
  587. /*
  588. * DMA ops for systems with IOC
  589. * IOC hardware snoops all DMA traffic keeping the caches consistent with
  590. * memory - eliding need for any explicit cache maintenance of DMA buffers
  591. */
  592. static void __dma_cache_wback_inv_ioc(phys_addr_t start, unsigned long sz) {}
  593. static void __dma_cache_inv_ioc(phys_addr_t start, unsigned long sz) {}
  594. static void __dma_cache_wback_ioc(phys_addr_t start, unsigned long sz) {}
  595. /*
  596. * Exported DMA API
  597. */
  598. void dma_cache_wback_inv(phys_addr_t start, unsigned long sz)
  599. {
  600. __dma_cache_wback_inv(start, sz);
  601. }
  602. EXPORT_SYMBOL(dma_cache_wback_inv);
  603. void dma_cache_inv(phys_addr_t start, unsigned long sz)
  604. {
  605. __dma_cache_inv(start, sz);
  606. }
  607. EXPORT_SYMBOL(dma_cache_inv);
  608. void dma_cache_wback(phys_addr_t start, unsigned long sz)
  609. {
  610. __dma_cache_wback(start, sz);
  611. }
  612. EXPORT_SYMBOL(dma_cache_wback);
  613. /*
  614. * This is API for making I/D Caches consistent when modifying
  615. * kernel code (loadable modules, kprobes, kgdb...)
  616. * This is called on insmod, with kernel virtual address for CODE of
  617. * the module. ARC cache maintenance ops require PHY address thus we
  618. * need to convert vmalloc addr to PHY addr
  619. */
  620. void flush_icache_range(unsigned long kstart, unsigned long kend)
  621. {
  622. unsigned int tot_sz;
  623. WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
  624. /* Shortcut for bigger flush ranges.
  625. * Here we don't care if this was kernel virtual or phy addr
  626. */
  627. tot_sz = kend - kstart;
  628. if (tot_sz > PAGE_SIZE) {
  629. flush_cache_all();
  630. return;
  631. }
  632. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  633. if (likely(kstart > PAGE_OFFSET)) {
  634. /*
  635. * The 2nd arg despite being paddr will be used to index icache
  636. * This is OK since no alternate virtual mappings will exist
  637. * given the callers for this case: kprobe/kgdb in built-in
  638. * kernel code only.
  639. */
  640. __sync_icache_dcache(kstart, kstart, kend - kstart);
  641. return;
  642. }
  643. /*
  644. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  645. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  646. * handling of kernel vaddr.
  647. *
  648. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  649. * it still needs to handle a 2 page scenario, where the range
  650. * straddles across 2 virtual pages and hence need for loop
  651. */
  652. while (tot_sz > 0) {
  653. unsigned int off, sz;
  654. unsigned long phy, pfn;
  655. off = kstart % PAGE_SIZE;
  656. pfn = vmalloc_to_pfn((void *)kstart);
  657. phy = (pfn << PAGE_SHIFT) + off;
  658. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  659. __sync_icache_dcache(phy, kstart, sz);
  660. kstart += sz;
  661. tot_sz -= sz;
  662. }
  663. }
  664. EXPORT_SYMBOL(flush_icache_range);
  665. /*
  666. * General purpose helper to make I and D cache lines consistent.
  667. * @paddr is phy addr of region
  668. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  669. * However in one instance, when called by kprobe (for a breakpt in
  670. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  671. * use a paddr to index the cache (despite VIPT). This is fine since since a
  672. * builtin kernel page will not have any virtual mappings.
  673. * kprobe on loadable module will be kernel vaddr.
  674. */
  675. void __sync_icache_dcache(phys_addr_t paddr, unsigned long vaddr, int len)
  676. {
  677. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  678. __ic_line_inv_vaddr(paddr, vaddr, len);
  679. }
  680. /* wrapper to compile time eliminate alignment checks in flush loop */
  681. void __inv_icache_page(phys_addr_t paddr, unsigned long vaddr)
  682. {
  683. __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
  684. }
  685. /*
  686. * wrapper to clearout kernel or userspace mappings of a page
  687. * For kernel mappings @vaddr == @paddr
  688. */
  689. void __flush_dcache_page(phys_addr_t paddr, unsigned long vaddr)
  690. {
  691. __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
  692. }
  693. noinline void flush_cache_all(void)
  694. {
  695. unsigned long flags;
  696. local_irq_save(flags);
  697. __ic_entire_inv();
  698. __dc_entire_op(OP_FLUSH_N_INV);
  699. local_irq_restore(flags);
  700. }
  701. #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
  702. void flush_cache_mm(struct mm_struct *mm)
  703. {
  704. flush_cache_all();
  705. }
  706. void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
  707. unsigned long pfn)
  708. {
  709. phys_addr_t paddr = pfn << PAGE_SHIFT;
  710. u_vaddr &= PAGE_MASK;
  711. __flush_dcache_page(paddr, u_vaddr);
  712. if (vma->vm_flags & VM_EXEC)
  713. __inv_icache_page(paddr, u_vaddr);
  714. }
  715. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  716. unsigned long end)
  717. {
  718. flush_cache_all();
  719. }
  720. void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  721. unsigned long u_vaddr)
  722. {
  723. /* TBD: do we really need to clear the kernel mapping */
  724. __flush_dcache_page((phys_addr_t)page_address(page), u_vaddr);
  725. __flush_dcache_page((phys_addr_t)page_address(page),
  726. (phys_addr_t)page_address(page));
  727. }
  728. #endif
  729. void copy_user_highpage(struct page *to, struct page *from,
  730. unsigned long u_vaddr, struct vm_area_struct *vma)
  731. {
  732. void *kfrom = kmap_atomic(from);
  733. void *kto = kmap_atomic(to);
  734. int clean_src_k_mappings = 0;
  735. /*
  736. * If SRC page was already mapped in userspace AND it's U-mapping is
  737. * not congruent with K-mapping, sync former to physical page so that
  738. * K-mapping in memcpy below, sees the right data
  739. *
  740. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  741. * equally valid for SRC page as well
  742. *
  743. * For !VIPT cache, all of this gets compiled out as
  744. * addr_not_cache_congruent() is 0
  745. */
  746. if (page_mapcount(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  747. __flush_dcache_page((unsigned long)kfrom, u_vaddr);
  748. clean_src_k_mappings = 1;
  749. }
  750. copy_page(kto, kfrom);
  751. /*
  752. * Mark DST page K-mapping as dirty for a later finalization by
  753. * update_mmu_cache(). Although the finalization could have been done
  754. * here as well (given that both vaddr/paddr are available).
  755. * But update_mmu_cache() already has code to do that for other
  756. * non copied user pages (e.g. read faults which wire in pagecache page
  757. * directly).
  758. */
  759. clear_bit(PG_dc_clean, &to->flags);
  760. /*
  761. * if SRC was already usermapped and non-congruent to kernel mapping
  762. * sync the kernel mapping back to physical page
  763. */
  764. if (clean_src_k_mappings) {
  765. __flush_dcache_page((unsigned long)kfrom, (unsigned long)kfrom);
  766. set_bit(PG_dc_clean, &from->flags);
  767. } else {
  768. clear_bit(PG_dc_clean, &from->flags);
  769. }
  770. kunmap_atomic(kto);
  771. kunmap_atomic(kfrom);
  772. }
  773. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  774. {
  775. clear_page(to);
  776. clear_bit(PG_dc_clean, &page->flags);
  777. }
  778. /**********************************************************************
  779. * Explicit Cache flush request from user space via syscall
  780. * Needed for JITs which generate code on the fly
  781. */
  782. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  783. {
  784. /* TBD: optimize this */
  785. flush_cache_all();
  786. return 0;
  787. }
  788. void arc_cache_init(void)
  789. {
  790. unsigned int __maybe_unused cpu = smp_processor_id();
  791. char str[256];
  792. printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
  793. /*
  794. * Only master CPU needs to execute rest of function:
  795. * - Assume SMP so all cores will have same cache config so
  796. * any geomtry checks will be same for all
  797. * - IOC setup / dma callbacks only need to be setup once
  798. */
  799. if (cpu)
  800. return;
  801. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  802. struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
  803. if (!ic->ver)
  804. panic("cache support enabled but non-existent cache\n");
  805. if (ic->line_len != L1_CACHE_BYTES)
  806. panic("ICache line [%d] != kernel Config [%d]",
  807. ic->line_len, L1_CACHE_BYTES);
  808. if (ic->ver != CONFIG_ARC_MMU_VER)
  809. panic("Cache ver [%d] doesn't match MMU ver [%d]\n",
  810. ic->ver, CONFIG_ARC_MMU_VER);
  811. /*
  812. * In MMU v4 (HS38x) the aliasing icache config uses IVIL/PTAG
  813. * pair to provide vaddr/paddr respectively, just as in MMU v3
  814. */
  815. if (is_isa_arcv2() && ic->alias)
  816. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  817. else
  818. _cache_line_loop_ic_fn = __cache_line_loop;
  819. }
  820. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  821. struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
  822. if (!dc->ver)
  823. panic("cache support enabled but non-existent cache\n");
  824. if (dc->line_len != L1_CACHE_BYTES)
  825. panic("DCache line [%d] != kernel Config [%d]",
  826. dc->line_len, L1_CACHE_BYTES);
  827. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  828. if (is_isa_arcompact()) {
  829. int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
  830. int num_colors = dc->sz_k/dc->assoc/TO_KB(PAGE_SIZE);
  831. if (dc->alias) {
  832. if (!handled)
  833. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  834. if (CACHE_COLORS_NUM != num_colors)
  835. panic("CACHE_COLORS_NUM not optimized for config\n");
  836. } else if (!dc->alias && handled) {
  837. panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  838. }
  839. }
  840. }
  841. if (is_isa_arcv2() && l2_line_sz && !slc_enable) {
  842. /* IM set : flush before invalidate */
  843. write_aux_reg(ARC_REG_SLC_CTRL,
  844. read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_IM);
  845. write_aux_reg(ARC_REG_SLC_INVALIDATE, 1);
  846. /* Important to wait for flush to complete */
  847. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  848. write_aux_reg(ARC_REG_SLC_CTRL,
  849. read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_DISABLE);
  850. }
  851. if (is_isa_arcv2() && ioc_enable) {
  852. /* IO coherency base - 0x8z */
  853. write_aux_reg(ARC_REG_IO_COH_AP0_BASE, 0x80000);
  854. /* IO coherency aperture size - 512Mb: 0x8z-0xAz */
  855. write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, 0x11);
  856. /* Enable partial writes */
  857. write_aux_reg(ARC_REG_IO_COH_PARTIAL, 1);
  858. /* Enable IO coherency */
  859. write_aux_reg(ARC_REG_IO_COH_ENABLE, 1);
  860. __dma_cache_wback_inv = __dma_cache_wback_inv_ioc;
  861. __dma_cache_inv = __dma_cache_inv_ioc;
  862. __dma_cache_wback = __dma_cache_wback_ioc;
  863. } else if (is_isa_arcv2() && l2_line_sz && slc_enable) {
  864. __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
  865. __dma_cache_inv = __dma_cache_inv_slc;
  866. __dma_cache_wback = __dma_cache_wback_slc;
  867. } else {
  868. __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
  869. __dma_cache_inv = __dma_cache_inv_l1;
  870. __dma_cache_wback = __dma_cache_wback_l1;
  871. }
  872. }