cache_arc700.c 21 KB

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