cache_arc700.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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, enb, str) \
  78. { \
  79. if (!(p)->ver) \
  80. n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \
  81. else \
  82. n += scnprintf(buf + n, len - n, \
  83. str"\t\t: (%uK) VIPT, %dway set-asc, %ub Line %s\n", \
  84. TO_KB((p)->sz), (p)->assoc, (p)->line_len, \
  85. enb ? "" : "DISABLED (kernel-build)"); \
  86. }
  87. PR_CACHE(&cpuinfo_arc700[c].icache, IS_ENABLED(CONFIG_ARC_HAS_ICACHE),
  88. "I-Cache");
  89. PR_CACHE(&cpuinfo_arc700[c].dcache, IS_ENABLED(CONFIG_ARC_HAS_DCACHE),
  90. "D-Cache");
  91. return buf;
  92. }
  93. /*
  94. * Read the Cache Build Confuration Registers, Decode them and save into
  95. * the cpuinfo structure for later use.
  96. * No Validation done here, simply read/convert the BCRs
  97. */
  98. void read_decode_cache_bcr(void)
  99. {
  100. struct cpuinfo_arc_cache *p_ic, *p_dc;
  101. unsigned int cpu = smp_processor_id();
  102. struct bcr_cache {
  103. #ifdef CONFIG_CPU_BIG_ENDIAN
  104. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  105. #else
  106. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  107. #endif
  108. } ibcr, dbcr;
  109. p_ic = &cpuinfo_arc700[cpu].icache;
  110. READ_BCR(ARC_REG_IC_BCR, ibcr);
  111. 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 = 0x200 << ibcr.sz;
  115. p_ic->ver = ibcr.ver;
  116. p_dc = &cpuinfo_arc700[cpu].dcache;
  117. READ_BCR(ARC_REG_DC_BCR, dbcr);
  118. BUG_ON(dbcr.config != 2);
  119. p_dc->assoc = 4; /* Fixed to 4w set assoc */
  120. p_dc->line_len = 16 << dbcr.line_len;
  121. p_dc->sz = 0x200 << dbcr.sz;
  122. p_dc->ver = dbcr.ver;
  123. }
  124. /*
  125. * 1. Validate the Cache Geomtery (compile time config matches hardware)
  126. * 2. If I-cache suffers from aliasing, setup work arounds (difft flush rtn)
  127. * (aliasing D-cache configurations are not supported YET)
  128. * 3. Enable the Caches, setup default flush mode for D-Cache
  129. * 3. Calculate the SHMLBA used by user space
  130. */
  131. void arc_cache_init(void)
  132. {
  133. unsigned int __maybe_unused cpu = smp_processor_id();
  134. struct cpuinfo_arc_cache __maybe_unused *ic, __maybe_unused *dc;
  135. char str[256];
  136. printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
  137. #ifdef CONFIG_ARC_HAS_ICACHE
  138. ic = &cpuinfo_arc700[cpu].icache;
  139. if (ic->ver) {
  140. if (ic->line_len != L1_CACHE_BYTES)
  141. panic("ICache line [%d] != kernel Config [%d]",
  142. ic->line_len, L1_CACHE_BYTES);
  143. if (ic->ver != CONFIG_ARC_MMU_VER)
  144. panic("Cache ver [%d] doesn't match MMU ver [%d]\n",
  145. ic->ver, CONFIG_ARC_MMU_VER);
  146. }
  147. #endif
  148. #ifdef CONFIG_ARC_HAS_DCACHE
  149. dc = &cpuinfo_arc700[cpu].dcache;
  150. if (dc->ver) {
  151. unsigned int dcache_does_alias;
  152. if (dc->line_len != L1_CACHE_BYTES)
  153. panic("DCache line [%d] != kernel Config [%d]",
  154. dc->line_len, L1_CACHE_BYTES);
  155. /* check for D-Cache aliasing */
  156. dcache_does_alias = (dc->sz / dc->assoc) > PAGE_SIZE;
  157. if (dcache_does_alias && !cache_is_vipt_aliasing())
  158. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  159. else if (!dcache_does_alias && cache_is_vipt_aliasing())
  160. panic("Don't need CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  161. }
  162. #endif
  163. }
  164. #define OP_INV 0x1
  165. #define OP_FLUSH 0x2
  166. #define OP_FLUSH_N_INV 0x3
  167. #define OP_INV_IC 0x4
  168. /*
  169. * Common Helper for Line Operations on {I,D}-Cache
  170. */
  171. static inline void __cache_line_loop(unsigned long paddr, unsigned long vaddr,
  172. unsigned long sz, const int cacheop)
  173. {
  174. unsigned int aux_cmd, aux_tag;
  175. int num_lines;
  176. const int full_page_op = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  177. if (cacheop == OP_INV_IC) {
  178. aux_cmd = ARC_REG_IC_IVIL;
  179. #if (CONFIG_ARC_MMU_VER > 2)
  180. aux_tag = ARC_REG_IC_PTAG;
  181. #endif
  182. }
  183. else {
  184. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  185. aux_cmd = cacheop & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  186. #if (CONFIG_ARC_MMU_VER > 2)
  187. aux_tag = ARC_REG_DC_PTAG;
  188. #endif
  189. }
  190. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  191. * and have @paddr - aligned to cache line and integral @num_lines.
  192. * This however can be avoided for page sized since:
  193. * -@paddr will be cache-line aligned already (being page aligned)
  194. * -@sz will be integral multiple of line size (being page sized).
  195. */
  196. if (!full_page_op) {
  197. sz += paddr & ~CACHE_LINE_MASK;
  198. paddr &= CACHE_LINE_MASK;
  199. vaddr &= CACHE_LINE_MASK;
  200. }
  201. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  202. #if (CONFIG_ARC_MMU_VER <= 2)
  203. /* MMUv2 and before: paddr contains stuffed vaddrs bits */
  204. paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
  205. #else
  206. /* if V-P const for loop, PTAG can be written once outside loop */
  207. if (full_page_op)
  208. write_aux_reg(aux_tag, paddr);
  209. #endif
  210. while (num_lines-- > 0) {
  211. #if (CONFIG_ARC_MMU_VER > 2)
  212. /* MMUv3, cache ops require paddr seperately */
  213. if (!full_page_op) {
  214. write_aux_reg(aux_tag, paddr);
  215. paddr += L1_CACHE_BYTES;
  216. }
  217. write_aux_reg(aux_cmd, vaddr);
  218. vaddr += L1_CACHE_BYTES;
  219. #else
  220. write_aux_reg(aux_cmd, paddr);
  221. paddr += L1_CACHE_BYTES;
  222. #endif
  223. }
  224. }
  225. #ifdef CONFIG_ARC_HAS_DCACHE
  226. /***************************************************************
  227. * Machine specific helpers for Entire D-Cache or Per Line ops
  228. */
  229. static inline void wait_for_flush(void)
  230. {
  231. while (read_aux_reg(ARC_REG_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
  232. ;
  233. }
  234. /*
  235. * Operation on Entire D-Cache
  236. * @cacheop = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
  237. * Note that constant propagation ensures all the checks are gone
  238. * in generated code
  239. */
  240. static inline void __dc_entire_op(const int cacheop)
  241. {
  242. unsigned int tmp = tmp;
  243. int aux;
  244. if (cacheop == OP_FLUSH_N_INV) {
  245. /* Dcache provides 2 cmd: FLUSH or INV
  246. * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
  247. * flush-n-inv is achieved by INV cmd but with IM=1
  248. * Default INV sub-mode is DISCARD, which needs to be toggled
  249. */
  250. tmp = read_aux_reg(ARC_REG_DC_CTRL);
  251. write_aux_reg(ARC_REG_DC_CTRL, tmp | DC_CTRL_INV_MODE_FLUSH);
  252. }
  253. if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  254. aux = ARC_REG_DC_IVDC;
  255. else
  256. aux = ARC_REG_DC_FLSH;
  257. write_aux_reg(aux, 0x1);
  258. if (cacheop & OP_FLUSH) /* flush / flush-n-inv both wait */
  259. wait_for_flush();
  260. /* Switch back the DISCARD ONLY Invalidate mode */
  261. if (cacheop == OP_FLUSH_N_INV)
  262. write_aux_reg(ARC_REG_DC_CTRL, tmp & ~DC_CTRL_INV_MODE_FLUSH);
  263. }
  264. /* For kernel mappings cache operation: index is same as paddr */
  265. #define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op)
  266. /*
  267. * D-Cache : Per Line INV (discard or wback+discard) or FLUSH (wback)
  268. */
  269. static inline void __dc_line_op(unsigned long paddr, unsigned long vaddr,
  270. unsigned long sz, const int cacheop)
  271. {
  272. unsigned long flags, tmp = tmp;
  273. local_irq_save(flags);
  274. if (cacheop == OP_FLUSH_N_INV) {
  275. /*
  276. * Dcache provides 2 cmd: FLUSH or INV
  277. * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
  278. * flush-n-inv is achieved by INV cmd but with IM=1
  279. * Default INV sub-mode is DISCARD, which needs to be toggled
  280. */
  281. tmp = read_aux_reg(ARC_REG_DC_CTRL);
  282. write_aux_reg(ARC_REG_DC_CTRL, tmp | DC_CTRL_INV_MODE_FLUSH);
  283. }
  284. __cache_line_loop(paddr, vaddr, sz, cacheop);
  285. if (cacheop & OP_FLUSH) /* flush / flush-n-inv both wait */
  286. wait_for_flush();
  287. /* Switch back the DISCARD ONLY Invalidate mode */
  288. if (cacheop == OP_FLUSH_N_INV)
  289. write_aux_reg(ARC_REG_DC_CTRL, tmp & ~DC_CTRL_INV_MODE_FLUSH);
  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 void __ic_line_inv_vaddr_local(unsigned long paddr, unsigned long vaddr,
  343. unsigned long sz)
  344. {
  345. unsigned long flags;
  346. local_irq_save(flags);
  347. __cache_line_loop(paddr, vaddr, sz, OP_INV_IC);
  348. local_irq_restore(flags);
  349. }
  350. static inline void __ic_entire_inv(void)
  351. {
  352. write_aux_reg(ARC_REG_IC_IVIC, 1);
  353. read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
  354. }
  355. struct ic_line_inv_vaddr_ipi {
  356. unsigned long paddr, vaddr;
  357. int sz;
  358. };
  359. static void __ic_line_inv_vaddr_helper(void *info)
  360. {
  361. struct ic_line_inv_vaddr_ipi *ic_inv = (struct ic_line_inv_vaddr_ipi*) info;
  362. __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
  363. }
  364. static void __ic_line_inv_vaddr(unsigned long paddr, unsigned long vaddr,
  365. unsigned long sz)
  366. {
  367. struct ic_line_inv_vaddr_ipi ic_inv = { paddr, vaddr , sz};
  368. on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
  369. }
  370. #else
  371. #define __ic_entire_inv()
  372. #define __ic_line_inv_vaddr(pstart, vstart, sz)
  373. #endif /* CONFIG_ARC_HAS_ICACHE */
  374. /***********************************************************
  375. * Exported APIs
  376. */
  377. /*
  378. * Handle cache congruency of kernel and userspace mappings of page when kernel
  379. * writes-to/reads-from
  380. *
  381. * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
  382. * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
  383. * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
  384. * -In SMP, if hardware caches are coherent
  385. *
  386. * There's a corollary case, where kernel READs from a userspace mapped page.
  387. * If the U-mapping is not congruent to to K-mapping, former needs flushing.
  388. */
  389. void flush_dcache_page(struct page *page)
  390. {
  391. struct address_space *mapping;
  392. if (!cache_is_vipt_aliasing()) {
  393. clear_bit(PG_dc_clean, &page->flags);
  394. return;
  395. }
  396. /* don't handle anon pages here */
  397. mapping = page_mapping(page);
  398. if (!mapping)
  399. return;
  400. /*
  401. * pagecache page, file not yet mapped to userspace
  402. * Make a note that K-mapping is dirty
  403. */
  404. if (!mapping_mapped(mapping)) {
  405. clear_bit(PG_dc_clean, &page->flags);
  406. } else if (page_mapped(page)) {
  407. /* kernel reading from page with U-mapping */
  408. void *paddr = page_address(page);
  409. unsigned long vaddr = page->index << PAGE_CACHE_SHIFT;
  410. if (addr_not_cache_congruent(paddr, vaddr))
  411. __flush_dcache_page(paddr, vaddr);
  412. }
  413. }
  414. EXPORT_SYMBOL(flush_dcache_page);
  415. void dma_cache_wback_inv(unsigned long start, unsigned long sz)
  416. {
  417. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  418. }
  419. EXPORT_SYMBOL(dma_cache_wback_inv);
  420. void dma_cache_inv(unsigned long start, unsigned long sz)
  421. {
  422. __dc_line_op_k(start, sz, OP_INV);
  423. }
  424. EXPORT_SYMBOL(dma_cache_inv);
  425. void dma_cache_wback(unsigned long start, unsigned long sz)
  426. {
  427. __dc_line_op_k(start, sz, OP_FLUSH);
  428. }
  429. EXPORT_SYMBOL(dma_cache_wback);
  430. /*
  431. * This is API for making I/D Caches consistent when modifying
  432. * kernel code (loadable modules, kprobes, kgdb...)
  433. * This is called on insmod, with kernel virtual address for CODE of
  434. * the module. ARC cache maintenance ops require PHY address thus we
  435. * need to convert vmalloc addr to PHY addr
  436. */
  437. void flush_icache_range(unsigned long kstart, unsigned long kend)
  438. {
  439. unsigned int tot_sz, off, sz;
  440. unsigned long phy, pfn;
  441. /* printk("Kernel Cache Cohenercy: %lx to %lx\n",kstart, kend); */
  442. /* This is not the right API for user virtual address */
  443. if (kstart < TASK_SIZE) {
  444. BUG_ON("Flush icache range for user virtual addr space");
  445. return;
  446. }
  447. /* Shortcut for bigger flush ranges.
  448. * Here we don't care if this was kernel virtual or phy addr
  449. */
  450. tot_sz = kend - kstart;
  451. if (tot_sz > PAGE_SIZE) {
  452. flush_cache_all();
  453. return;
  454. }
  455. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  456. if (likely(kstart > PAGE_OFFSET)) {
  457. /*
  458. * The 2nd arg despite being paddr will be used to index icache
  459. * This is OK since no alternate virtual mappings will exist
  460. * given the callers for this case: kprobe/kgdb in built-in
  461. * kernel code only.
  462. */
  463. __sync_icache_dcache(kstart, kstart, kend - kstart);
  464. return;
  465. }
  466. /*
  467. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  468. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  469. * handling of kernel vaddr.
  470. *
  471. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  472. * it still needs to handle a 2 page scenario, where the range
  473. * straddles across 2 virtual pages and hence need for loop
  474. */
  475. while (tot_sz > 0) {
  476. off = kstart % PAGE_SIZE;
  477. pfn = vmalloc_to_pfn((void *)kstart);
  478. phy = (pfn << PAGE_SHIFT) + off;
  479. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  480. __sync_icache_dcache(phy, kstart, sz);
  481. kstart += sz;
  482. tot_sz -= sz;
  483. }
  484. }
  485. /*
  486. * General purpose helper to make I and D cache lines consistent.
  487. * @paddr is phy addr of region
  488. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  489. * However in one instance, when called by kprobe (for a breakpt in
  490. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  491. * use a paddr to index the cache (despite VIPT). This is fine since since a
  492. * builtin kernel page will not have any virtual mappings.
  493. * kprobe on loadable module will be kernel vaddr.
  494. */
  495. void __sync_icache_dcache(unsigned long paddr, unsigned long vaddr, int len)
  496. {
  497. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  498. __ic_line_inv_vaddr(paddr, vaddr, len);
  499. }
  500. /* wrapper to compile time eliminate alignment checks in flush loop */
  501. void __inv_icache_page(unsigned long paddr, unsigned long vaddr)
  502. {
  503. __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
  504. }
  505. /*
  506. * wrapper to clearout kernel or userspace mappings of a page
  507. * For kernel mappings @vaddr == @paddr
  508. */
  509. void ___flush_dcache_page(unsigned long paddr, unsigned long vaddr)
  510. {
  511. __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
  512. }
  513. noinline void flush_cache_all(void)
  514. {
  515. unsigned long flags;
  516. local_irq_save(flags);
  517. __ic_entire_inv();
  518. __dc_entire_op(OP_FLUSH_N_INV);
  519. local_irq_restore(flags);
  520. }
  521. #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
  522. void flush_cache_mm(struct mm_struct *mm)
  523. {
  524. flush_cache_all();
  525. }
  526. void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
  527. unsigned long pfn)
  528. {
  529. unsigned int paddr = pfn << PAGE_SHIFT;
  530. u_vaddr &= PAGE_MASK;
  531. ___flush_dcache_page(paddr, u_vaddr);
  532. if (vma->vm_flags & VM_EXEC)
  533. __inv_icache_page(paddr, u_vaddr);
  534. }
  535. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  536. unsigned long end)
  537. {
  538. flush_cache_all();
  539. }
  540. void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  541. unsigned long u_vaddr)
  542. {
  543. /* TBD: do we really need to clear the kernel mapping */
  544. __flush_dcache_page(page_address(page), u_vaddr);
  545. __flush_dcache_page(page_address(page), page_address(page));
  546. }
  547. #endif
  548. void copy_user_highpage(struct page *to, struct page *from,
  549. unsigned long u_vaddr, struct vm_area_struct *vma)
  550. {
  551. void *kfrom = page_address(from);
  552. void *kto = page_address(to);
  553. int clean_src_k_mappings = 0;
  554. /*
  555. * If SRC page was already mapped in userspace AND it's U-mapping is
  556. * not congruent with K-mapping, sync former to physical page so that
  557. * K-mapping in memcpy below, sees the right data
  558. *
  559. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  560. * equally valid for SRC page as well
  561. */
  562. if (page_mapped(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  563. __flush_dcache_page(kfrom, u_vaddr);
  564. clean_src_k_mappings = 1;
  565. }
  566. copy_page(kto, kfrom);
  567. /*
  568. * Mark DST page K-mapping as dirty for a later finalization by
  569. * update_mmu_cache(). Although the finalization could have been done
  570. * here as well (given that both vaddr/paddr are available).
  571. * But update_mmu_cache() already has code to do that for other
  572. * non copied user pages (e.g. read faults which wire in pagecache page
  573. * directly).
  574. */
  575. clear_bit(PG_dc_clean, &to->flags);
  576. /*
  577. * if SRC was already usermapped and non-congruent to kernel mapping
  578. * sync the kernel mapping back to physical page
  579. */
  580. if (clean_src_k_mappings) {
  581. __flush_dcache_page(kfrom, kfrom);
  582. set_bit(PG_dc_clean, &from->flags);
  583. } else {
  584. clear_bit(PG_dc_clean, &from->flags);
  585. }
  586. }
  587. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  588. {
  589. clear_page(to);
  590. clear_bit(PG_dc_clean, &page->flags);
  591. }
  592. /**********************************************************************
  593. * Explicit Cache flush request from user space via syscall
  594. * Needed for JITs which generate code on the fly
  595. */
  596. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  597. {
  598. /* TBD: optimize this */
  599. flush_cache_all();
  600. return 0;
  601. }