cache_arc700.c 22 KB

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