cache.c 28 KB

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