cache.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. unsigned int 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(page_address(page), u_vaddr);
  725. __flush_dcache_page(page_address(page), page_address(page));
  726. }
  727. #endif
  728. void copy_user_highpage(struct page *to, struct page *from,
  729. unsigned long u_vaddr, struct vm_area_struct *vma)
  730. {
  731. void *kfrom = kmap_atomic(from);
  732. void *kto = kmap_atomic(to);
  733. int clean_src_k_mappings = 0;
  734. /*
  735. * If SRC page was already mapped in userspace AND it's U-mapping is
  736. * not congruent with K-mapping, sync former to physical page so that
  737. * K-mapping in memcpy below, sees the right data
  738. *
  739. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  740. * equally valid for SRC page as well
  741. *
  742. * For !VIPT cache, all of this gets compiled out as
  743. * addr_not_cache_congruent() is 0
  744. */
  745. if (page_mapcount(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  746. __flush_dcache_page((unsigned long)kfrom, u_vaddr);
  747. clean_src_k_mappings = 1;
  748. }
  749. copy_page(kto, kfrom);
  750. /*
  751. * Mark DST page K-mapping as dirty for a later finalization by
  752. * update_mmu_cache(). Although the finalization could have been done
  753. * here as well (given that both vaddr/paddr are available).
  754. * But update_mmu_cache() already has code to do that for other
  755. * non copied user pages (e.g. read faults which wire in pagecache page
  756. * directly).
  757. */
  758. clear_bit(PG_dc_clean, &to->flags);
  759. /*
  760. * if SRC was already usermapped and non-congruent to kernel mapping
  761. * sync the kernel mapping back to physical page
  762. */
  763. if (clean_src_k_mappings) {
  764. __flush_dcache_page((unsigned long)kfrom, (unsigned long)kfrom);
  765. set_bit(PG_dc_clean, &from->flags);
  766. } else {
  767. clear_bit(PG_dc_clean, &from->flags);
  768. }
  769. kunmap_atomic(kto);
  770. kunmap_atomic(kfrom);
  771. }
  772. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  773. {
  774. clear_page(to);
  775. clear_bit(PG_dc_clean, &page->flags);
  776. }
  777. /**********************************************************************
  778. * Explicit Cache flush request from user space via syscall
  779. * Needed for JITs which generate code on the fly
  780. */
  781. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  782. {
  783. /* TBD: optimize this */
  784. flush_cache_all();
  785. return 0;
  786. }
  787. void arc_cache_init(void)
  788. {
  789. unsigned int __maybe_unused cpu = smp_processor_id();
  790. char str[256];
  791. printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
  792. /*
  793. * Only master CPU needs to execute rest of function:
  794. * - Assume SMP so all cores will have same cache config so
  795. * any geomtry checks will be same for all
  796. * - IOC setup / dma callbacks only need to be setup once
  797. */
  798. if (cpu)
  799. return;
  800. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  801. struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
  802. if (!ic->ver)
  803. panic("cache support enabled but non-existent cache\n");
  804. if (ic->line_len != L1_CACHE_BYTES)
  805. panic("ICache line [%d] != kernel Config [%d]",
  806. ic->line_len, L1_CACHE_BYTES);
  807. if (ic->ver != CONFIG_ARC_MMU_VER)
  808. panic("Cache ver [%d] doesn't match MMU ver [%d]\n",
  809. ic->ver, CONFIG_ARC_MMU_VER);
  810. /*
  811. * In MMU v4 (HS38x) the aliasing icache config uses IVIL/PTAG
  812. * pair to provide vaddr/paddr respectively, just as in MMU v3
  813. */
  814. if (is_isa_arcv2() && ic->alias)
  815. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  816. else
  817. _cache_line_loop_ic_fn = __cache_line_loop;
  818. }
  819. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  820. struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
  821. if (!dc->ver)
  822. panic("cache support enabled but non-existent cache\n");
  823. if (dc->line_len != L1_CACHE_BYTES)
  824. panic("DCache line [%d] != kernel Config [%d]",
  825. dc->line_len, L1_CACHE_BYTES);
  826. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  827. if (is_isa_arcompact()) {
  828. int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
  829. int num_colors = dc->sz_k/dc->assoc/TO_KB(PAGE_SIZE);
  830. if (dc->alias) {
  831. if (!handled)
  832. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  833. if (CACHE_COLORS_NUM != num_colors)
  834. panic("CACHE_COLORS_NUM not optimized for config\n");
  835. } else if (!dc->alias && handled) {
  836. panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  837. }
  838. }
  839. }
  840. if (is_isa_arcv2() && l2_line_sz && !slc_enable) {
  841. /* IM set : flush before invalidate */
  842. write_aux_reg(ARC_REG_SLC_CTRL,
  843. read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_IM);
  844. write_aux_reg(ARC_REG_SLC_INVALIDATE, 1);
  845. /* Important to wait for flush to complete */
  846. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  847. write_aux_reg(ARC_REG_SLC_CTRL,
  848. read_aux_reg(ARC_REG_SLC_CTRL) | SLC_CTRL_DISABLE);
  849. }
  850. if (is_isa_arcv2() && ioc_enable) {
  851. /* IO coherency base - 0x8z */
  852. write_aux_reg(ARC_REG_IO_COH_AP0_BASE, 0x80000);
  853. /* IO coherency aperture size - 512Mb: 0x8z-0xAz */
  854. write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, 0x11);
  855. /* Enable partial writes */
  856. write_aux_reg(ARC_REG_IO_COH_PARTIAL, 1);
  857. /* Enable IO coherency */
  858. write_aux_reg(ARC_REG_IO_COH_ENABLE, 1);
  859. __dma_cache_wback_inv = __dma_cache_wback_inv_ioc;
  860. __dma_cache_inv = __dma_cache_inv_ioc;
  861. __dma_cache_wback = __dma_cache_wback_ioc;
  862. } else if (is_isa_arcv2() && l2_line_sz && slc_enable) {
  863. __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
  864. __dma_cache_inv = __dma_cache_inv_slc;
  865. __dma_cache_wback = __dma_cache_wback_slc;
  866. } else {
  867. __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
  868. __dma_cache_inv = __dma_cache_inv_l1;
  869. __dma_cache_wback = __dma_cache_wback_l1;
  870. }
  871. }