cache.c 26 KB

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