cache_arc700.c 22 KB

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