cache.c 28 KB

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