cache.c 23 KB

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