cache.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. void (*_cache_line_loop_ic_fn)(unsigned long paddr, unsigned long vaddr,
  23. unsigned long sz, const int cacheop);
  24. char *arc_cache_mumbojumbo(int c, char *buf, int len)
  25. {
  26. int n = 0;
  27. struct cpuinfo_arc_cache *p;
  28. #define PR_CACHE(p, cfg, str) \
  29. if (!(p)->ver) \
  30. n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \
  31. else \
  32. n += scnprintf(buf + n, len - n, \
  33. str"\t\t: %uK, %dway/set, %uB Line, %s%s%s\n", \
  34. (p)->sz_k, (p)->assoc, (p)->line_len, \
  35. (p)->vipt ? "VIPT" : "PIPT", \
  36. (p)->alias ? " aliasing" : "", \
  37. IS_ENABLED(cfg) ? "" : " (not used)");
  38. PR_CACHE(&cpuinfo_arc700[c].icache, CONFIG_ARC_HAS_ICACHE, "I-Cache");
  39. PR_CACHE(&cpuinfo_arc700[c].dcache, CONFIG_ARC_HAS_DCACHE, "D-Cache");
  40. p = &cpuinfo_arc700[c].slc;
  41. if (p->ver)
  42. n += scnprintf(buf + n, len - n,
  43. "SLC\t\t: %uK, %uB Line\n", p->sz_k, p->line_len);
  44. return buf;
  45. }
  46. /*
  47. * Read the Cache Build Confuration Registers, Decode them and save into
  48. * the cpuinfo structure for later use.
  49. * No Validation done here, simply read/convert the BCRs
  50. */
  51. void read_decode_cache_bcr(void)
  52. {
  53. struct cpuinfo_arc_cache *p_ic, *p_dc, *p_slc;
  54. unsigned int cpu = smp_processor_id();
  55. struct bcr_cache {
  56. #ifdef CONFIG_CPU_BIG_ENDIAN
  57. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  58. #else
  59. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  60. #endif
  61. } ibcr, dbcr;
  62. struct bcr_generic sbcr;
  63. struct bcr_slc_cfg {
  64. #ifdef CONFIG_CPU_BIG_ENDIAN
  65. unsigned int pad:24, way:2, lsz:2, sz:4;
  66. #else
  67. unsigned int sz:4, lsz:2, way:2, pad:24;
  68. #endif
  69. } slc_cfg;
  70. p_ic = &cpuinfo_arc700[cpu].icache;
  71. READ_BCR(ARC_REG_IC_BCR, ibcr);
  72. if (!ibcr.ver)
  73. goto dc_chk;
  74. if (ibcr.ver <= 3) {
  75. BUG_ON(ibcr.config != 3);
  76. p_ic->assoc = 2; /* Fixed to 2w set assoc */
  77. } else if (ibcr.ver >= 4) {
  78. p_ic->assoc = 1 << ibcr.config; /* 1,2,4,8 */
  79. }
  80. p_ic->line_len = 8 << ibcr.line_len;
  81. p_ic->sz_k = 1 << (ibcr.sz - 1);
  82. p_ic->ver = ibcr.ver;
  83. p_ic->vipt = 1;
  84. p_ic->alias = p_ic->sz_k/p_ic->assoc/TO_KB(PAGE_SIZE) > 1;
  85. dc_chk:
  86. p_dc = &cpuinfo_arc700[cpu].dcache;
  87. READ_BCR(ARC_REG_DC_BCR, dbcr);
  88. if (!dbcr.ver)
  89. goto slc_chk;
  90. if (dbcr.ver <= 3) {
  91. BUG_ON(dbcr.config != 2);
  92. p_dc->assoc = 4; /* Fixed to 4w set assoc */
  93. p_dc->vipt = 1;
  94. p_dc->alias = p_dc->sz_k/p_dc->assoc/TO_KB(PAGE_SIZE) > 1;
  95. } else if (dbcr.ver >= 4) {
  96. p_dc->assoc = 1 << dbcr.config; /* 1,2,4,8 */
  97. p_dc->vipt = 0;
  98. p_dc->alias = 0; /* PIPT so can't VIPT alias */
  99. }
  100. p_dc->line_len = 16 << dbcr.line_len;
  101. p_dc->sz_k = 1 << (dbcr.sz - 1);
  102. p_dc->ver = dbcr.ver;
  103. slc_chk:
  104. p_slc = &cpuinfo_arc700[cpu].slc;
  105. READ_BCR(ARC_REG_SLC_BCR, sbcr);
  106. if (sbcr.ver) {
  107. READ_BCR(ARC_REG_SLC_CFG, slc_cfg);
  108. p_slc->ver = sbcr.ver;
  109. p_slc->sz_k = 128 << slc_cfg.sz;
  110. p_slc->line_len = (slc_cfg.lsz == 0) ? 128 : 64;
  111. }
  112. }
  113. /*
  114. * Line Operation on {I,D}-Cache
  115. */
  116. #define OP_INV 0x1
  117. #define OP_FLUSH 0x2
  118. #define OP_FLUSH_N_INV 0x3
  119. #define OP_INV_IC 0x4
  120. /*
  121. * I-Cache Aliasing in ARC700 VIPT caches (MMU v1-v3)
  122. *
  123. * ARC VIPT I-cache uses vaddr to index into cache and paddr to match the tag.
  124. * The orig Cache Management Module "CDU" only required paddr to invalidate a
  125. * certain line since it sufficed as index in Non-Aliasing VIPT cache-geometry.
  126. * Infact for distinct V1,V2,P: all of {V1-P},{V2-P},{P-P} would end up fetching
  127. * the exact same line.
  128. *
  129. * However for larger Caches (way-size > page-size) - i.e. in Aliasing config,
  130. * paddr alone could not be used to correctly index the cache.
  131. *
  132. * ------------------
  133. * MMU v1/v2 (Fixed Page Size 8k)
  134. * ------------------
  135. * The solution was to provide CDU with these additonal vaddr bits. These
  136. * would be bits [x:13], x would depend on cache-geometry, 13 comes from
  137. * standard page size of 8k.
  138. * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits
  139. * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the
  140. * orig 5 bits of paddr were anyways ignored by CDU line ops, as they
  141. * represent the offset within cache-line. The adv of using this "clumsy"
  142. * interface for additional info was no new reg was needed in CDU programming
  143. * model.
  144. *
  145. * 17:13 represented the max num of bits passable, actual bits needed were
  146. * fewer, based on the num-of-aliases possible.
  147. * -for 2 alias possibility, only bit 13 needed (32K cache)
  148. * -for 4 alias possibility, bits 14:13 needed (64K cache)
  149. *
  150. * ------------------
  151. * MMU v3
  152. * ------------------
  153. * This ver of MMU supports variable page sizes (1k-16k): although Linux will
  154. * only support 8k (default), 16k and 4k.
  155. * However from hardware perspective, smaller page sizes aggrevate aliasing
  156. * meaning more vaddr bits needed to disambiguate the cache-line-op ;
  157. * the existing scheme of piggybacking won't work for certain configurations.
  158. * Two new registers IC_PTAG and DC_PTAG inttoduced.
  159. * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs
  160. */
  161. static inline
  162. void __cache_line_loop_v2(unsigned long paddr, unsigned long vaddr,
  163. unsigned long sz, const int op)
  164. {
  165. unsigned int aux_cmd;
  166. int num_lines;
  167. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  168. if (op == OP_INV_IC) {
  169. aux_cmd = ARC_REG_IC_IVIL;
  170. } else {
  171. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  172. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  173. }
  174. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  175. * and have @paddr - aligned to cache line and integral @num_lines.
  176. * This however can be avoided for page sized since:
  177. * -@paddr will be cache-line aligned already (being page aligned)
  178. * -@sz will be integral multiple of line size (being page sized).
  179. */
  180. if (!full_page) {
  181. sz += paddr & ~CACHE_LINE_MASK;
  182. paddr &= CACHE_LINE_MASK;
  183. vaddr &= CACHE_LINE_MASK;
  184. }
  185. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  186. /* MMUv2 and before: paddr contains stuffed vaddrs bits */
  187. paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
  188. while (num_lines-- > 0) {
  189. write_aux_reg(aux_cmd, paddr);
  190. paddr += L1_CACHE_BYTES;
  191. }
  192. }
  193. static inline
  194. void __cache_line_loop_v3(unsigned long paddr, unsigned long vaddr,
  195. unsigned long sz, const int op)
  196. {
  197. unsigned int aux_cmd, aux_tag;
  198. int num_lines;
  199. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  200. if (op == OP_INV_IC) {
  201. aux_cmd = ARC_REG_IC_IVIL;
  202. aux_tag = ARC_REG_IC_PTAG;
  203. } else {
  204. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  205. aux_tag = ARC_REG_DC_PTAG;
  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. /*
  220. * MMUv3, cache ops require paddr in PTAG reg
  221. * if V-P const for loop, PTAG can be written once outside loop
  222. */
  223. if (full_page)
  224. write_aux_reg(aux_tag, paddr);
  225. while (num_lines-- > 0) {
  226. if (!full_page) {
  227. write_aux_reg(aux_tag, paddr);
  228. paddr += L1_CACHE_BYTES;
  229. }
  230. write_aux_reg(aux_cmd, vaddr);
  231. vaddr += L1_CACHE_BYTES;
  232. }
  233. }
  234. /*
  235. * In HS38x (MMU v4), although icache is VIPT, only paddr is needed for cache
  236. * maintenance ops (in IVIL reg), as long as icache doesn't alias.
  237. *
  238. * For Aliasing icache, vaddr is also needed (in IVIL), while paddr is
  239. * specified in PTAG (similar to MMU v3)
  240. */
  241. static inline
  242. void __cache_line_loop_v4(unsigned long paddr, unsigned long vaddr,
  243. unsigned long sz, const int cacheop)
  244. {
  245. unsigned int aux_cmd;
  246. int num_lines;
  247. const int full_page_op = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  248. if (cacheop == OP_INV_IC) {
  249. aux_cmd = ARC_REG_IC_IVIL;
  250. } else {
  251. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  252. aux_cmd = cacheop & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  253. }
  254. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  255. * and have @paddr - aligned to cache line and integral @num_lines.
  256. * This however can be avoided for page sized since:
  257. * -@paddr will be cache-line aligned already (being page aligned)
  258. * -@sz will be integral multiple of line size (being page sized).
  259. */
  260. if (!full_page_op) {
  261. sz += paddr & ~CACHE_LINE_MASK;
  262. paddr &= CACHE_LINE_MASK;
  263. }
  264. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  265. while (num_lines-- > 0) {
  266. write_aux_reg(aux_cmd, paddr);
  267. paddr += L1_CACHE_BYTES;
  268. }
  269. }
  270. #if (CONFIG_ARC_MMU_VER < 3)
  271. #define __cache_line_loop __cache_line_loop_v2
  272. #elif (CONFIG_ARC_MMU_VER == 3)
  273. #define __cache_line_loop __cache_line_loop_v3
  274. #elif (CONFIG_ARC_MMU_VER > 3)
  275. #define __cache_line_loop __cache_line_loop_v4
  276. #endif
  277. #ifdef CONFIG_ARC_HAS_DCACHE
  278. /***************************************************************
  279. * Machine specific helpers for Entire D-Cache or Per Line ops
  280. */
  281. static inline void __before_dc_op(const int op)
  282. {
  283. if (op == OP_FLUSH_N_INV) {
  284. /* Dcache provides 2 cmd: FLUSH or INV
  285. * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
  286. * flush-n-inv is achieved by INV cmd but with IM=1
  287. * So toggle INV sub-mode depending on op request and default
  288. */
  289. const unsigned int ctl = ARC_REG_DC_CTRL;
  290. write_aux_reg(ctl, read_aux_reg(ctl) | DC_CTRL_INV_MODE_FLUSH);
  291. }
  292. }
  293. static inline void __after_dc_op(const int op)
  294. {
  295. if (op & OP_FLUSH) {
  296. const unsigned int ctl = ARC_REG_DC_CTRL;
  297. unsigned int reg;
  298. /* flush / flush-n-inv both wait */
  299. while ((reg = read_aux_reg(ctl)) & DC_CTRL_FLUSH_STATUS)
  300. ;
  301. /* Switch back to default Invalidate mode */
  302. if (op == OP_FLUSH_N_INV)
  303. write_aux_reg(ctl, reg & ~DC_CTRL_INV_MODE_FLUSH);
  304. }
  305. }
  306. /*
  307. * Operation on Entire D-Cache
  308. * @op = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
  309. * Note that constant propagation ensures all the checks are gone
  310. * in generated code
  311. */
  312. static inline void __dc_entire_op(const int op)
  313. {
  314. int aux;
  315. __before_dc_op(op);
  316. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  317. aux = ARC_REG_DC_IVDC;
  318. else
  319. aux = ARC_REG_DC_FLSH;
  320. write_aux_reg(aux, 0x1);
  321. __after_dc_op(op);
  322. }
  323. /* For kernel mappings cache operation: index is same as paddr */
  324. #define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op)
  325. /*
  326. * D-Cache Line ops: Per Line INV (discard or wback+discard) or FLUSH (wback)
  327. */
  328. static inline void __dc_line_op(unsigned long paddr, unsigned long vaddr,
  329. unsigned long sz, const int op)
  330. {
  331. unsigned long flags;
  332. local_irq_save(flags);
  333. __before_dc_op(op);
  334. __cache_line_loop(paddr, vaddr, sz, op);
  335. __after_dc_op(op);
  336. local_irq_restore(flags);
  337. }
  338. #else
  339. #define __dc_entire_op(op)
  340. #define __dc_line_op(paddr, vaddr, sz, op)
  341. #define __dc_line_op_k(paddr, sz, op)
  342. #endif /* CONFIG_ARC_HAS_DCACHE */
  343. #ifdef CONFIG_ARC_HAS_ICACHE
  344. static inline void __ic_entire_inv(void)
  345. {
  346. write_aux_reg(ARC_REG_IC_IVIC, 1);
  347. read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
  348. }
  349. static inline void
  350. __ic_line_inv_vaddr_local(unsigned long paddr, unsigned long vaddr,
  351. unsigned long sz)
  352. {
  353. unsigned long flags;
  354. local_irq_save(flags);
  355. (*_cache_line_loop_ic_fn)(paddr, vaddr, sz, OP_INV_IC);
  356. local_irq_restore(flags);
  357. }
  358. #ifndef CONFIG_SMP
  359. #define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s)
  360. #else
  361. struct ic_inv_args {
  362. unsigned long paddr, vaddr;
  363. int sz;
  364. };
  365. static void __ic_line_inv_vaddr_helper(void *info)
  366. {
  367. struct ic_inv_args *ic_inv = info;
  368. __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
  369. }
  370. static void __ic_line_inv_vaddr(unsigned long paddr, unsigned long vaddr,
  371. unsigned long sz)
  372. {
  373. struct ic_inv_args ic_inv = {
  374. .paddr = paddr,
  375. .vaddr = vaddr,
  376. .sz = sz
  377. };
  378. on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
  379. }
  380. #endif /* CONFIG_SMP */
  381. #else /* !CONFIG_ARC_HAS_ICACHE */
  382. #define __ic_entire_inv()
  383. #define __ic_line_inv_vaddr(pstart, vstart, sz)
  384. #endif /* CONFIG_ARC_HAS_ICACHE */
  385. /***********************************************************
  386. * Exported APIs
  387. */
  388. /*
  389. * Handle cache congruency of kernel and userspace mappings of page when kernel
  390. * writes-to/reads-from
  391. *
  392. * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
  393. * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
  394. * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
  395. * -In SMP, if hardware caches are coherent
  396. *
  397. * There's a corollary case, where kernel READs from a userspace mapped page.
  398. * If the U-mapping is not congruent to to K-mapping, former needs flushing.
  399. */
  400. void flush_dcache_page(struct page *page)
  401. {
  402. struct address_space *mapping;
  403. if (!cache_is_vipt_aliasing()) {
  404. clear_bit(PG_dc_clean, &page->flags);
  405. return;
  406. }
  407. /* don't handle anon pages here */
  408. mapping = page_mapping(page);
  409. if (!mapping)
  410. return;
  411. /*
  412. * pagecache page, file not yet mapped to userspace
  413. * Make a note that K-mapping is dirty
  414. */
  415. if (!mapping_mapped(mapping)) {
  416. clear_bit(PG_dc_clean, &page->flags);
  417. } else if (page_mapped(page)) {
  418. /* kernel reading from page with U-mapping */
  419. unsigned long paddr = (unsigned long)page_address(page);
  420. unsigned long vaddr = page->index << PAGE_CACHE_SHIFT;
  421. if (addr_not_cache_congruent(paddr, vaddr))
  422. __flush_dcache_page(paddr, vaddr);
  423. }
  424. }
  425. EXPORT_SYMBOL(flush_dcache_page);
  426. void dma_cache_wback_inv(unsigned long start, unsigned long sz)
  427. {
  428. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  429. }
  430. EXPORT_SYMBOL(dma_cache_wback_inv);
  431. void dma_cache_inv(unsigned long start, unsigned long sz)
  432. {
  433. __dc_line_op_k(start, sz, OP_INV);
  434. }
  435. EXPORT_SYMBOL(dma_cache_inv);
  436. void dma_cache_wback(unsigned long start, unsigned long sz)
  437. {
  438. __dc_line_op_k(start, sz, OP_FLUSH);
  439. }
  440. EXPORT_SYMBOL(dma_cache_wback);
  441. /*
  442. * This is API for making I/D Caches consistent when modifying
  443. * kernel code (loadable modules, kprobes, kgdb...)
  444. * This is called on insmod, with kernel virtual address for CODE of
  445. * the module. ARC cache maintenance ops require PHY address thus we
  446. * need to convert vmalloc addr to PHY addr
  447. */
  448. void flush_icache_range(unsigned long kstart, unsigned long kend)
  449. {
  450. unsigned int tot_sz;
  451. WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
  452. /* Shortcut for bigger flush ranges.
  453. * Here we don't care if this was kernel virtual or phy addr
  454. */
  455. tot_sz = kend - kstart;
  456. if (tot_sz > PAGE_SIZE) {
  457. flush_cache_all();
  458. return;
  459. }
  460. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  461. if (likely(kstart > PAGE_OFFSET)) {
  462. /*
  463. * The 2nd arg despite being paddr will be used to index icache
  464. * This is OK since no alternate virtual mappings will exist
  465. * given the callers for this case: kprobe/kgdb in built-in
  466. * kernel code only.
  467. */
  468. __sync_icache_dcache(kstart, kstart, kend - kstart);
  469. return;
  470. }
  471. /*
  472. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  473. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  474. * handling of kernel vaddr.
  475. *
  476. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  477. * it still needs to handle a 2 page scenario, where the range
  478. * straddles across 2 virtual pages and hence need for loop
  479. */
  480. while (tot_sz > 0) {
  481. unsigned int off, sz;
  482. unsigned long phy, pfn;
  483. off = kstart % PAGE_SIZE;
  484. pfn = vmalloc_to_pfn((void *)kstart);
  485. phy = (pfn << PAGE_SHIFT) + off;
  486. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  487. __sync_icache_dcache(phy, kstart, sz);
  488. kstart += sz;
  489. tot_sz -= sz;
  490. }
  491. }
  492. EXPORT_SYMBOL(flush_icache_range);
  493. /*
  494. * General purpose helper to make I and D cache lines consistent.
  495. * @paddr is phy addr of region
  496. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  497. * However in one instance, when called by kprobe (for a breakpt in
  498. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  499. * use a paddr to index the cache (despite VIPT). This is fine since since a
  500. * builtin kernel page will not have any virtual mappings.
  501. * kprobe on loadable module will be kernel vaddr.
  502. */
  503. void __sync_icache_dcache(unsigned long paddr, unsigned long vaddr, int len)
  504. {
  505. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  506. __ic_line_inv_vaddr(paddr, vaddr, len);
  507. }
  508. /* wrapper to compile time eliminate alignment checks in flush loop */
  509. void __inv_icache_page(unsigned long paddr, unsigned long vaddr)
  510. {
  511. __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
  512. }
  513. /*
  514. * wrapper to clearout kernel or userspace mappings of a page
  515. * For kernel mappings @vaddr == @paddr
  516. */
  517. void __flush_dcache_page(unsigned long paddr, unsigned long vaddr)
  518. {
  519. __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
  520. }
  521. noinline void flush_cache_all(void)
  522. {
  523. unsigned long flags;
  524. local_irq_save(flags);
  525. __ic_entire_inv();
  526. __dc_entire_op(OP_FLUSH_N_INV);
  527. local_irq_restore(flags);
  528. }
  529. #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
  530. void flush_cache_mm(struct mm_struct *mm)
  531. {
  532. flush_cache_all();
  533. }
  534. void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
  535. unsigned long pfn)
  536. {
  537. unsigned int paddr = pfn << PAGE_SHIFT;
  538. u_vaddr &= PAGE_MASK;
  539. __flush_dcache_page(paddr, u_vaddr);
  540. if (vma->vm_flags & VM_EXEC)
  541. __inv_icache_page(paddr, u_vaddr);
  542. }
  543. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  544. unsigned long end)
  545. {
  546. flush_cache_all();
  547. }
  548. void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  549. unsigned long u_vaddr)
  550. {
  551. /* TBD: do we really need to clear the kernel mapping */
  552. __flush_dcache_page(page_address(page), u_vaddr);
  553. __flush_dcache_page(page_address(page), page_address(page));
  554. }
  555. #endif
  556. void copy_user_highpage(struct page *to, struct page *from,
  557. unsigned long u_vaddr, struct vm_area_struct *vma)
  558. {
  559. unsigned long kfrom = (unsigned long)page_address(from);
  560. unsigned long kto = (unsigned long)page_address(to);
  561. int clean_src_k_mappings = 0;
  562. /*
  563. * If SRC page was already mapped in userspace AND it's U-mapping is
  564. * not congruent with K-mapping, sync former to physical page so that
  565. * K-mapping in memcpy below, sees the right data
  566. *
  567. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  568. * equally valid for SRC page as well
  569. */
  570. if (page_mapped(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  571. __flush_dcache_page(kfrom, u_vaddr);
  572. clean_src_k_mappings = 1;
  573. }
  574. copy_page((void *)kto, (void *)kfrom);
  575. /*
  576. * Mark DST page K-mapping as dirty for a later finalization by
  577. * update_mmu_cache(). Although the finalization could have been done
  578. * here as well (given that both vaddr/paddr are available).
  579. * But update_mmu_cache() already has code to do that for other
  580. * non copied user pages (e.g. read faults which wire in pagecache page
  581. * directly).
  582. */
  583. clear_bit(PG_dc_clean, &to->flags);
  584. /*
  585. * if SRC was already usermapped and non-congruent to kernel mapping
  586. * sync the kernel mapping back to physical page
  587. */
  588. if (clean_src_k_mappings) {
  589. __flush_dcache_page(kfrom, kfrom);
  590. set_bit(PG_dc_clean, &from->flags);
  591. } else {
  592. clear_bit(PG_dc_clean, &from->flags);
  593. }
  594. }
  595. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  596. {
  597. clear_page(to);
  598. clear_bit(PG_dc_clean, &page->flags);
  599. }
  600. /**********************************************************************
  601. * Explicit Cache flush request from user space via syscall
  602. * Needed for JITs which generate code on the fly
  603. */
  604. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  605. {
  606. /* TBD: optimize this */
  607. flush_cache_all();
  608. return 0;
  609. }
  610. void arc_cache_init(void)
  611. {
  612. unsigned int __maybe_unused cpu = smp_processor_id();
  613. char str[256];
  614. printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
  615. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  616. struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
  617. if (!ic->ver)
  618. panic("cache support enabled but non-existent cache\n");
  619. if (ic->line_len != L1_CACHE_BYTES)
  620. panic("ICache line [%d] != kernel Config [%d]",
  621. ic->line_len, L1_CACHE_BYTES);
  622. if (ic->ver != CONFIG_ARC_MMU_VER)
  623. panic("Cache ver [%d] doesn't match MMU ver [%d]\n",
  624. ic->ver, CONFIG_ARC_MMU_VER);
  625. /*
  626. * In MMU v4 (HS38x) the alising icache config uses IVIL/PTAG
  627. * pair to provide vaddr/paddr respectively, just as in MMU v3
  628. */
  629. if (is_isa_arcv2() && ic->alias)
  630. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  631. else
  632. _cache_line_loop_ic_fn = __cache_line_loop;
  633. }
  634. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  635. struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
  636. if (!dc->ver)
  637. panic("cache support enabled but non-existent cache\n");
  638. if (dc->line_len != L1_CACHE_BYTES)
  639. panic("DCache line [%d] != kernel Config [%d]",
  640. dc->line_len, L1_CACHE_BYTES);
  641. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  642. if (is_isa_arcompact()) {
  643. int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
  644. if (dc->alias && !handled)
  645. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  646. else if (!dc->alias && handled)
  647. panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  648. }
  649. }
  650. }