cache.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /*
  2. * ARC Cache Management
  3. *
  4. * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
  5. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/mm.h>
  13. #include <linux/sched.h>
  14. #include <linux/cache.h>
  15. #include <linux/mmu_context.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/pagemap.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/cachectl.h>
  21. #include <asm/setup.h>
  22. static int l2_line_sz;
  23. static int ioc_exists;
  24. int slc_enable = 1, ioc_enable = 1;
  25. unsigned long perip_base = ARC_UNCACHED_ADDR_SPACE; /* legacy value for boot */
  26. unsigned long perip_end = 0xFFFFFFFF; /* legacy value */
  27. void (*_cache_line_loop_ic_fn)(phys_addr_t paddr, unsigned long vaddr,
  28. unsigned long sz, const int cacheop);
  29. void (*__dma_cache_wback_inv)(phys_addr_t start, unsigned long sz);
  30. void (*__dma_cache_inv)(phys_addr_t start, unsigned long sz);
  31. void (*__dma_cache_wback)(phys_addr_t start, unsigned long sz);
  32. char *arc_cache_mumbojumbo(int c, char *buf, int len)
  33. {
  34. int n = 0;
  35. struct cpuinfo_arc_cache *p;
  36. #define PR_CACHE(p, cfg, str) \
  37. if (!(p)->line_len) \
  38. n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \
  39. else \
  40. n += scnprintf(buf + n, len - n, \
  41. str"\t\t: %uK, %dway/set, %uB Line, %s%s%s\n", \
  42. (p)->sz_k, (p)->assoc, (p)->line_len, \
  43. (p)->vipt ? "VIPT" : "PIPT", \
  44. (p)->alias ? " aliasing" : "", \
  45. IS_USED_CFG(cfg));
  46. PR_CACHE(&cpuinfo_arc700[c].icache, CONFIG_ARC_HAS_ICACHE, "I-Cache");
  47. PR_CACHE(&cpuinfo_arc700[c].dcache, CONFIG_ARC_HAS_DCACHE, "D-Cache");
  48. p = &cpuinfo_arc700[c].slc;
  49. if (p->line_len)
  50. n += scnprintf(buf + n, len - n,
  51. "SLC\t\t: %uK, %uB Line%s\n",
  52. p->sz_k, p->line_len, IS_USED_RUN(slc_enable));
  53. n += scnprintf(buf + n, len - n, "Peripherals\t: %#lx%s%s\n",
  54. perip_base,
  55. IS_AVAIL3(ioc_exists, ioc_enable, ", IO-Coherency "));
  56. return buf;
  57. }
  58. /*
  59. * Read the Cache Build Confuration Registers, Decode them and save into
  60. * the cpuinfo structure for later use.
  61. * No Validation done here, simply read/convert the BCRs
  62. */
  63. static void read_decode_cache_bcr_arcv2(int cpu)
  64. {
  65. struct cpuinfo_arc_cache *p_slc = &cpuinfo_arc700[cpu].slc;
  66. struct bcr_generic sbcr;
  67. struct bcr_slc_cfg {
  68. #ifdef CONFIG_CPU_BIG_ENDIAN
  69. unsigned int pad:24, way:2, lsz:2, sz:4;
  70. #else
  71. unsigned int sz:4, lsz:2, way:2, pad:24;
  72. #endif
  73. } slc_cfg;
  74. struct bcr_clust_cfg {
  75. #ifdef CONFIG_CPU_BIG_ENDIAN
  76. unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8;
  77. #else
  78. unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7;
  79. #endif
  80. } cbcr;
  81. struct bcr_volatile {
  82. #ifdef CONFIG_CPU_BIG_ENDIAN
  83. unsigned int start:4, limit:4, pad:22, order:1, disable:1;
  84. #else
  85. unsigned int disable:1, order:1, pad:22, limit:4, start:4;
  86. #endif
  87. } vol;
  88. READ_BCR(ARC_REG_SLC_BCR, sbcr);
  89. if (sbcr.ver) {
  90. READ_BCR(ARC_REG_SLC_CFG, slc_cfg);
  91. p_slc->sz_k = 128 << slc_cfg.sz;
  92. l2_line_sz = p_slc->line_len = (slc_cfg.lsz == 0) ? 128 : 64;
  93. }
  94. READ_BCR(ARC_REG_CLUSTER_BCR, cbcr);
  95. if (cbcr.c)
  96. ioc_exists = 1;
  97. else
  98. ioc_enable = 0;
  99. /* HS 2.0 didn't have AUX_VOL */
  100. if (cpuinfo_arc700[cpu].core.family > 0x51) {
  101. READ_BCR(AUX_VOL, vol);
  102. perip_base = vol.start << 28;
  103. /* HS 3.0 has limit and strict-ordering fields */
  104. if (cpuinfo_arc700[cpu].core.family > 0x52)
  105. perip_end = (vol.limit << 28) - 1;
  106. }
  107. }
  108. void read_decode_cache_bcr(void)
  109. {
  110. struct cpuinfo_arc_cache *p_ic, *p_dc;
  111. unsigned int cpu = smp_processor_id();
  112. struct bcr_cache {
  113. #ifdef CONFIG_CPU_BIG_ENDIAN
  114. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  115. #else
  116. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  117. #endif
  118. } ibcr, dbcr;
  119. p_ic = &cpuinfo_arc700[cpu].icache;
  120. READ_BCR(ARC_REG_IC_BCR, ibcr);
  121. if (!ibcr.ver)
  122. goto dc_chk;
  123. if (ibcr.ver <= 3) {
  124. BUG_ON(ibcr.config != 3);
  125. p_ic->assoc = 2; /* Fixed to 2w set assoc */
  126. } else if (ibcr.ver >= 4) {
  127. p_ic->assoc = 1 << ibcr.config; /* 1,2,4,8 */
  128. }
  129. p_ic->line_len = 8 << ibcr.line_len;
  130. p_ic->sz_k = 1 << (ibcr.sz - 1);
  131. p_ic->vipt = 1;
  132. p_ic->alias = p_ic->sz_k/p_ic->assoc/TO_KB(PAGE_SIZE) > 1;
  133. dc_chk:
  134. p_dc = &cpuinfo_arc700[cpu].dcache;
  135. READ_BCR(ARC_REG_DC_BCR, dbcr);
  136. if (!dbcr.ver)
  137. goto slc_chk;
  138. if (dbcr.ver <= 3) {
  139. BUG_ON(dbcr.config != 2);
  140. p_dc->assoc = 4; /* Fixed to 4w set assoc */
  141. p_dc->vipt = 1;
  142. p_dc->alias = p_dc->sz_k/p_dc->assoc/TO_KB(PAGE_SIZE) > 1;
  143. } else if (dbcr.ver >= 4) {
  144. p_dc->assoc = 1 << dbcr.config; /* 1,2,4,8 */
  145. p_dc->vipt = 0;
  146. p_dc->alias = 0; /* PIPT so can't VIPT alias */
  147. }
  148. p_dc->line_len = 16 << dbcr.line_len;
  149. p_dc->sz_k = 1 << (dbcr.sz - 1);
  150. slc_chk:
  151. if (is_isa_arcv2())
  152. read_decode_cache_bcr_arcv2(cpu);
  153. }
  154. /*
  155. * Line Operation on {I,D}-Cache
  156. */
  157. #define OP_INV 0x1
  158. #define OP_FLUSH 0x2
  159. #define OP_FLUSH_N_INV 0x3
  160. #define OP_INV_IC 0x4
  161. /*
  162. * I-Cache Aliasing in ARC700 VIPT caches (MMU v1-v3)
  163. *
  164. * ARC VIPT I-cache uses vaddr to index into cache and paddr to match the tag.
  165. * The orig Cache Management Module "CDU" only required paddr to invalidate a
  166. * certain line since it sufficed as index in Non-Aliasing VIPT cache-geometry.
  167. * Infact for distinct V1,V2,P: all of {V1-P},{V2-P},{P-P} would end up fetching
  168. * the exact same line.
  169. *
  170. * However for larger Caches (way-size > page-size) - i.e. in Aliasing config,
  171. * paddr alone could not be used to correctly index the cache.
  172. *
  173. * ------------------
  174. * MMU v1/v2 (Fixed Page Size 8k)
  175. * ------------------
  176. * The solution was to provide CDU with these additonal vaddr bits. These
  177. * would be bits [x:13], x would depend on cache-geometry, 13 comes from
  178. * standard page size of 8k.
  179. * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits
  180. * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the
  181. * orig 5 bits of paddr were anyways ignored by CDU line ops, as they
  182. * represent the offset within cache-line. The adv of using this "clumsy"
  183. * interface for additional info was no new reg was needed in CDU programming
  184. * model.
  185. *
  186. * 17:13 represented the max num of bits passable, actual bits needed were
  187. * fewer, based on the num-of-aliases possible.
  188. * -for 2 alias possibility, only bit 13 needed (32K cache)
  189. * -for 4 alias possibility, bits 14:13 needed (64K cache)
  190. *
  191. * ------------------
  192. * MMU v3
  193. * ------------------
  194. * This ver of MMU supports variable page sizes (1k-16k): although Linux will
  195. * only support 8k (default), 16k and 4k.
  196. * However from hardware perspective, smaller page sizes aggravate aliasing
  197. * meaning more vaddr bits needed to disambiguate the cache-line-op ;
  198. * the existing scheme of piggybacking won't work for certain configurations.
  199. * Two new registers IC_PTAG and DC_PTAG inttoduced.
  200. * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs
  201. */
  202. static inline
  203. void __cache_line_loop_v2(phys_addr_t paddr, unsigned long vaddr,
  204. unsigned long sz, const int op)
  205. {
  206. unsigned int aux_cmd;
  207. int num_lines;
  208. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  209. if (op == OP_INV_IC) {
  210. aux_cmd = ARC_REG_IC_IVIL;
  211. } else {
  212. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  213. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  214. }
  215. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  216. * and have @paddr - aligned to cache line and integral @num_lines.
  217. * This however can be avoided for page sized since:
  218. * -@paddr will be cache-line aligned already (being page aligned)
  219. * -@sz will be integral multiple of line size (being page sized).
  220. */
  221. if (!full_page) {
  222. sz += paddr & ~CACHE_LINE_MASK;
  223. paddr &= CACHE_LINE_MASK;
  224. vaddr &= CACHE_LINE_MASK;
  225. }
  226. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  227. /* MMUv2 and before: paddr contains stuffed vaddrs bits */
  228. paddr |= (vaddr >> PAGE_SHIFT) & 0x1F;
  229. while (num_lines-- > 0) {
  230. write_aux_reg(aux_cmd, paddr);
  231. paddr += L1_CACHE_BYTES;
  232. }
  233. }
  234. /*
  235. * For ARC700 MMUv3 I-cache and D-cache flushes
  236. * - ARC700 programming model requires paddr and vaddr be passed in seperate
  237. * AUX registers (*_IV*L and *_PTAG respectively) irrespective of whether the
  238. * caches actually alias or not.
  239. * - For HS38, only the aliasing I-cache configuration uses the PTAG reg
  240. * (non aliasing I-cache version doesn't; while D-cache can't possibly alias)
  241. */
  242. static inline
  243. void __cache_line_loop_v3(phys_addr_t paddr, unsigned long vaddr,
  244. unsigned long sz, const int op)
  245. {
  246. unsigned int aux_cmd, aux_tag;
  247. int num_lines;
  248. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  249. if (op == OP_INV_IC) {
  250. aux_cmd = ARC_REG_IC_IVIL;
  251. aux_tag = ARC_REG_IC_PTAG;
  252. } else {
  253. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  254. aux_tag = ARC_REG_DC_PTAG;
  255. }
  256. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  257. * and have @paddr - aligned to cache line and integral @num_lines.
  258. * This however can be avoided for page sized since:
  259. * -@paddr will be cache-line aligned already (being page aligned)
  260. * -@sz will be integral multiple of line size (being page sized).
  261. */
  262. if (!full_page) {
  263. sz += paddr & ~CACHE_LINE_MASK;
  264. paddr &= CACHE_LINE_MASK;
  265. vaddr &= CACHE_LINE_MASK;
  266. }
  267. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  268. /*
  269. * MMUv3, cache ops require paddr in PTAG reg
  270. * if V-P const for loop, PTAG can be written once outside loop
  271. */
  272. if (full_page)
  273. write_aux_reg(aux_tag, paddr);
  274. /*
  275. * This is technically for MMU v4, using the MMU v3 programming model
  276. * Special work for HS38 aliasing I-cache configuration with PAE40
  277. * - upper 8 bits of paddr need to be written into PTAG_HI
  278. * - (and needs to be written before the lower 32 bits)
  279. * Note that PTAG_HI is hoisted outside the line loop
  280. */
  281. if (is_pae40_enabled() && op == OP_INV_IC)
  282. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  283. while (num_lines-- > 0) {
  284. if (!full_page) {
  285. write_aux_reg(aux_tag, paddr);
  286. paddr += L1_CACHE_BYTES;
  287. }
  288. write_aux_reg(aux_cmd, vaddr);
  289. vaddr += L1_CACHE_BYTES;
  290. }
  291. }
  292. /*
  293. * In HS38x (MMU v4), I-cache is VIPT (can alias), D-cache is PIPT
  294. * Here's how cache ops are implemented
  295. *
  296. * - D-cache: only paddr needed (in DC_IVDL/DC_FLDL)
  297. * - I-cache Non Aliasing: Despite VIPT, only paddr needed (in IC_IVIL)
  298. * - I-cache Aliasing: Both vaddr and paddr needed (in IC_IVIL, IC_PTAG
  299. * respectively, similar to MMU v3 programming model, hence
  300. * __cache_line_loop_v3() is used)
  301. *
  302. * If PAE40 is enabled, independent of aliasing considerations, the higher bits
  303. * needs to be written into PTAG_HI
  304. */
  305. static inline
  306. void __cache_line_loop_v4(phys_addr_t paddr, unsigned long vaddr,
  307. unsigned long sz, const int cacheop)
  308. {
  309. unsigned int aux_cmd;
  310. int num_lines;
  311. const int full_page_op = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  312. if (cacheop == OP_INV_IC) {
  313. aux_cmd = ARC_REG_IC_IVIL;
  314. } else {
  315. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  316. aux_cmd = cacheop & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  317. }
  318. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  319. * and have @paddr - aligned to cache line and integral @num_lines.
  320. * This however can be avoided for page sized since:
  321. * -@paddr will be cache-line aligned already (being page aligned)
  322. * -@sz will be integral multiple of line size (being page sized).
  323. */
  324. if (!full_page_op) {
  325. sz += paddr & ~CACHE_LINE_MASK;
  326. paddr &= CACHE_LINE_MASK;
  327. }
  328. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  329. /*
  330. * For HS38 PAE40 configuration
  331. * - upper 8 bits of paddr need to be written into PTAG_HI
  332. * - (and needs to be written before the lower 32 bits)
  333. */
  334. if (is_pae40_enabled()) {
  335. if (cacheop == OP_INV_IC)
  336. /*
  337. * Non aliasing I-cache in HS38,
  338. * aliasing I-cache handled in __cache_line_loop_v3()
  339. */
  340. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  341. else
  342. write_aux_reg(ARC_REG_DC_PTAG_HI, (u64)paddr >> 32);
  343. }
  344. while (num_lines-- > 0) {
  345. write_aux_reg(aux_cmd, paddr);
  346. paddr += L1_CACHE_BYTES;
  347. }
  348. }
  349. #if (CONFIG_ARC_MMU_VER < 3)
  350. #define __cache_line_loop __cache_line_loop_v2
  351. #elif (CONFIG_ARC_MMU_VER == 3)
  352. #define __cache_line_loop __cache_line_loop_v3
  353. #elif (CONFIG_ARC_MMU_VER > 3)
  354. #define __cache_line_loop __cache_line_loop_v4
  355. #endif
  356. #ifdef CONFIG_ARC_HAS_DCACHE
  357. /***************************************************************
  358. * Machine specific helpers for Entire D-Cache or Per Line ops
  359. */
  360. static inline void __before_dc_op(const int op)
  361. {
  362. if (op == OP_FLUSH_N_INV) {
  363. /* Dcache provides 2 cmd: FLUSH or INV
  364. * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE
  365. * flush-n-inv is achieved by INV cmd but with IM=1
  366. * So toggle INV sub-mode depending on op request and default
  367. */
  368. const unsigned int ctl = ARC_REG_DC_CTRL;
  369. write_aux_reg(ctl, read_aux_reg(ctl) | DC_CTRL_INV_MODE_FLUSH);
  370. }
  371. }
  372. static inline void __after_dc_op(const int op)
  373. {
  374. if (op & OP_FLUSH) {
  375. const unsigned int ctl = ARC_REG_DC_CTRL;
  376. unsigned int reg;
  377. /* flush / flush-n-inv both wait */
  378. while ((reg = read_aux_reg(ctl)) & DC_CTRL_FLUSH_STATUS)
  379. ;
  380. /* Switch back to default Invalidate mode */
  381. if (op == OP_FLUSH_N_INV)
  382. write_aux_reg(ctl, reg & ~DC_CTRL_INV_MODE_FLUSH);
  383. }
  384. }
  385. /*
  386. * Operation on Entire D-Cache
  387. * @op = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
  388. * Note that constant propagation ensures all the checks are gone
  389. * in generated code
  390. */
  391. static inline void __dc_entire_op(const int op)
  392. {
  393. int aux;
  394. __before_dc_op(op);
  395. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  396. aux = ARC_REG_DC_IVDC;
  397. else
  398. aux = ARC_REG_DC_FLSH;
  399. write_aux_reg(aux, 0x1);
  400. __after_dc_op(op);
  401. }
  402. static inline void __dc_disable(void)
  403. {
  404. const int r = ARC_REG_DC_CTRL;
  405. __dc_entire_op(OP_FLUSH_N_INV);
  406. write_aux_reg(r, read_aux_reg(r) | DC_CTRL_DIS);
  407. }
  408. static void __dc_enable(void)
  409. {
  410. const int r = ARC_REG_DC_CTRL;
  411. write_aux_reg(r, read_aux_reg(r) & ~DC_CTRL_DIS);
  412. }
  413. /* For kernel mappings cache operation: index is same as paddr */
  414. #define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op)
  415. /*
  416. * D-Cache Line ops: Per Line INV (discard or wback+discard) or FLUSH (wback)
  417. */
  418. static inline void __dc_line_op(phys_addr_t paddr, unsigned long vaddr,
  419. unsigned long sz, const int op)
  420. {
  421. unsigned long flags;
  422. local_irq_save(flags);
  423. __before_dc_op(op);
  424. __cache_line_loop(paddr, vaddr, sz, op);
  425. __after_dc_op(op);
  426. local_irq_restore(flags);
  427. }
  428. #else
  429. #define __dc_entire_op(op)
  430. #define __dc_disable()
  431. #define __dc_enable()
  432. #define __dc_line_op(paddr, vaddr, sz, op)
  433. #define __dc_line_op_k(paddr, sz, op)
  434. #endif /* CONFIG_ARC_HAS_DCACHE */
  435. #ifdef CONFIG_ARC_HAS_ICACHE
  436. static inline void __ic_entire_inv(void)
  437. {
  438. write_aux_reg(ARC_REG_IC_IVIC, 1);
  439. read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
  440. }
  441. static inline void
  442. __ic_line_inv_vaddr_local(phys_addr_t paddr, unsigned long vaddr,
  443. unsigned long sz)
  444. {
  445. unsigned long flags;
  446. local_irq_save(flags);
  447. (*_cache_line_loop_ic_fn)(paddr, vaddr, sz, OP_INV_IC);
  448. local_irq_restore(flags);
  449. }
  450. #ifndef CONFIG_SMP
  451. #define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s)
  452. #else
  453. struct ic_inv_args {
  454. phys_addr_t paddr, vaddr;
  455. int sz;
  456. };
  457. static void __ic_line_inv_vaddr_helper(void *info)
  458. {
  459. struct ic_inv_args *ic_inv = info;
  460. __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
  461. }
  462. static void __ic_line_inv_vaddr(phys_addr_t paddr, unsigned long vaddr,
  463. unsigned long sz)
  464. {
  465. struct ic_inv_args ic_inv = {
  466. .paddr = paddr,
  467. .vaddr = vaddr,
  468. .sz = sz
  469. };
  470. on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
  471. }
  472. #endif /* CONFIG_SMP */
  473. #else /* !CONFIG_ARC_HAS_ICACHE */
  474. #define __ic_entire_inv()
  475. #define __ic_line_inv_vaddr(pstart, vstart, sz)
  476. #endif /* CONFIG_ARC_HAS_ICACHE */
  477. noinline void slc_op(phys_addr_t paddr, unsigned long sz, const int op)
  478. {
  479. #ifdef CONFIG_ISA_ARCV2
  480. /*
  481. * SLC is shared between all cores and concurrent aux operations from
  482. * multiple cores need to be serialized using a spinlock
  483. * A concurrent operation can be silently ignored and/or the old/new
  484. * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
  485. * below)
  486. */
  487. static DEFINE_SPINLOCK(lock);
  488. unsigned long flags;
  489. unsigned int ctrl;
  490. spin_lock_irqsave(&lock, flags);
  491. /*
  492. * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
  493. * - b'000 (default) is Flush,
  494. * - b'001 is Invalidate if CTRL.IM == 0
  495. * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
  496. */
  497. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  498. /* Don't rely on default value of IM bit */
  499. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  500. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  501. else
  502. ctrl |= SLC_CTRL_IM;
  503. if (op & OP_INV)
  504. ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
  505. else
  506. ctrl &= ~SLC_CTRL_RGN_OP_INV;
  507. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  508. /*
  509. * Lower bits are ignored, no need to clip
  510. * END needs to be setup before START (latter triggers the operation)
  511. * END can't be same as START, so add (l2_line_sz - 1) to sz
  512. */
  513. write_aux_reg(ARC_REG_SLC_RGN_END, (paddr + sz + l2_line_sz - 1));
  514. write_aux_reg(ARC_REG_SLC_RGN_START, paddr);
  515. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  516. spin_unlock_irqrestore(&lock, flags);
  517. #endif
  518. }
  519. noinline static void slc_entire_op(const int op)
  520. {
  521. unsigned int ctrl, r = ARC_REG_SLC_CTRL;
  522. ctrl = read_aux_reg(r);
  523. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  524. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  525. else
  526. ctrl |= SLC_CTRL_IM;
  527. write_aux_reg(r, ctrl);
  528. write_aux_reg(ARC_REG_SLC_INVALIDATE, 1);
  529. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  530. read_aux_reg(r);
  531. /* Important to wait for flush to complete */
  532. while (read_aux_reg(r) & SLC_CTRL_BUSY);
  533. }
  534. static inline void arc_slc_disable(void)
  535. {
  536. const int r = ARC_REG_SLC_CTRL;
  537. slc_entire_op(OP_FLUSH_N_INV);
  538. write_aux_reg(r, read_aux_reg(r) | SLC_CTRL_DIS);
  539. }
  540. static inline void arc_slc_enable(void)
  541. {
  542. const int r = ARC_REG_SLC_CTRL;
  543. write_aux_reg(r, read_aux_reg(r) & ~SLC_CTRL_DIS);
  544. }
  545. /***********************************************************
  546. * Exported APIs
  547. */
  548. /*
  549. * Handle cache congruency of kernel and userspace mappings of page when kernel
  550. * writes-to/reads-from
  551. *
  552. * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
  553. * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
  554. * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
  555. * -In SMP, if hardware caches are coherent
  556. *
  557. * There's a corollary case, where kernel READs from a userspace mapped page.
  558. * If the U-mapping is not congruent to to K-mapping, former needs flushing.
  559. */
  560. void flush_dcache_page(struct page *page)
  561. {
  562. struct address_space *mapping;
  563. if (!cache_is_vipt_aliasing()) {
  564. clear_bit(PG_dc_clean, &page->flags);
  565. return;
  566. }
  567. /* don't handle anon pages here */
  568. mapping = page_mapping(page);
  569. if (!mapping)
  570. return;
  571. /*
  572. * pagecache page, file not yet mapped to userspace
  573. * Make a note that K-mapping is dirty
  574. */
  575. if (!mapping_mapped(mapping)) {
  576. clear_bit(PG_dc_clean, &page->flags);
  577. } else if (page_mapcount(page)) {
  578. /* kernel reading from page with U-mapping */
  579. phys_addr_t paddr = (unsigned long)page_address(page);
  580. unsigned long vaddr = page->index << PAGE_SHIFT;
  581. if (addr_not_cache_congruent(paddr, vaddr))
  582. __flush_dcache_page(paddr, vaddr);
  583. }
  584. }
  585. EXPORT_SYMBOL(flush_dcache_page);
  586. /*
  587. * DMA ops for systems with L1 cache only
  588. * Make memory coherent with L1 cache by flushing/invalidating L1 lines
  589. */
  590. static void __dma_cache_wback_inv_l1(phys_addr_t start, unsigned long sz)
  591. {
  592. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  593. }
  594. static void __dma_cache_inv_l1(phys_addr_t start, unsigned long sz)
  595. {
  596. __dc_line_op_k(start, sz, OP_INV);
  597. }
  598. static void __dma_cache_wback_l1(phys_addr_t start, unsigned long sz)
  599. {
  600. __dc_line_op_k(start, sz, OP_FLUSH);
  601. }
  602. /*
  603. * DMA ops for systems with both L1 and L2 caches, but without IOC
  604. * Both L1 and L2 lines need to be explicitly flushed/invalidated
  605. */
  606. static void __dma_cache_wback_inv_slc(phys_addr_t start, unsigned long sz)
  607. {
  608. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  609. slc_op(start, sz, OP_FLUSH_N_INV);
  610. }
  611. static void __dma_cache_inv_slc(phys_addr_t start, unsigned long sz)
  612. {
  613. __dc_line_op_k(start, sz, OP_INV);
  614. slc_op(start, sz, OP_INV);
  615. }
  616. static void __dma_cache_wback_slc(phys_addr_t start, unsigned long sz)
  617. {
  618. __dc_line_op_k(start, sz, OP_FLUSH);
  619. slc_op(start, sz, OP_FLUSH);
  620. }
  621. /*
  622. * DMA ops for systems with IOC
  623. * IOC hardware snoops all DMA traffic keeping the caches consistent with
  624. * memory - eliding need for any explicit cache maintenance of DMA buffers
  625. */
  626. static void __dma_cache_wback_inv_ioc(phys_addr_t start, unsigned long sz) {}
  627. static void __dma_cache_inv_ioc(phys_addr_t start, unsigned long sz) {}
  628. static void __dma_cache_wback_ioc(phys_addr_t start, unsigned long sz) {}
  629. /*
  630. * Exported DMA API
  631. */
  632. void dma_cache_wback_inv(phys_addr_t start, unsigned long sz)
  633. {
  634. __dma_cache_wback_inv(start, sz);
  635. }
  636. EXPORT_SYMBOL(dma_cache_wback_inv);
  637. void dma_cache_inv(phys_addr_t start, unsigned long sz)
  638. {
  639. __dma_cache_inv(start, sz);
  640. }
  641. EXPORT_SYMBOL(dma_cache_inv);
  642. void dma_cache_wback(phys_addr_t start, unsigned long sz)
  643. {
  644. __dma_cache_wback(start, sz);
  645. }
  646. EXPORT_SYMBOL(dma_cache_wback);
  647. /*
  648. * This is API for making I/D Caches consistent when modifying
  649. * kernel code (loadable modules, kprobes, kgdb...)
  650. * This is called on insmod, with kernel virtual address for CODE of
  651. * the module. ARC cache maintenance ops require PHY address thus we
  652. * need to convert vmalloc addr to PHY addr
  653. */
  654. void flush_icache_range(unsigned long kstart, unsigned long kend)
  655. {
  656. unsigned int tot_sz;
  657. WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
  658. /* Shortcut for bigger flush ranges.
  659. * Here we don't care if this was kernel virtual or phy addr
  660. */
  661. tot_sz = kend - kstart;
  662. if (tot_sz > PAGE_SIZE) {
  663. flush_cache_all();
  664. return;
  665. }
  666. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  667. if (likely(kstart > PAGE_OFFSET)) {
  668. /*
  669. * The 2nd arg despite being paddr will be used to index icache
  670. * This is OK since no alternate virtual mappings will exist
  671. * given the callers for this case: kprobe/kgdb in built-in
  672. * kernel code only.
  673. */
  674. __sync_icache_dcache(kstart, kstart, kend - kstart);
  675. return;
  676. }
  677. /*
  678. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  679. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  680. * handling of kernel vaddr.
  681. *
  682. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  683. * it still needs to handle a 2 page scenario, where the range
  684. * straddles across 2 virtual pages and hence need for loop
  685. */
  686. while (tot_sz > 0) {
  687. unsigned int off, sz;
  688. unsigned long phy, pfn;
  689. off = kstart % PAGE_SIZE;
  690. pfn = vmalloc_to_pfn((void *)kstart);
  691. phy = (pfn << PAGE_SHIFT) + off;
  692. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  693. __sync_icache_dcache(phy, kstart, sz);
  694. kstart += sz;
  695. tot_sz -= sz;
  696. }
  697. }
  698. EXPORT_SYMBOL(flush_icache_range);
  699. /*
  700. * General purpose helper to make I and D cache lines consistent.
  701. * @paddr is phy addr of region
  702. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  703. * However in one instance, when called by kprobe (for a breakpt in
  704. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  705. * use a paddr to index the cache (despite VIPT). This is fine since since a
  706. * builtin kernel page will not have any virtual mappings.
  707. * kprobe on loadable module will be kernel vaddr.
  708. */
  709. void __sync_icache_dcache(phys_addr_t paddr, unsigned long vaddr, int len)
  710. {
  711. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  712. __ic_line_inv_vaddr(paddr, vaddr, len);
  713. }
  714. /* wrapper to compile time eliminate alignment checks in flush loop */
  715. void __inv_icache_page(phys_addr_t paddr, unsigned long vaddr)
  716. {
  717. __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
  718. }
  719. /*
  720. * wrapper to clearout kernel or userspace mappings of a page
  721. * For kernel mappings @vaddr == @paddr
  722. */
  723. void __flush_dcache_page(phys_addr_t paddr, unsigned long vaddr)
  724. {
  725. __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
  726. }
  727. noinline void flush_cache_all(void)
  728. {
  729. unsigned long flags;
  730. local_irq_save(flags);
  731. __ic_entire_inv();
  732. __dc_entire_op(OP_FLUSH_N_INV);
  733. local_irq_restore(flags);
  734. }
  735. #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
  736. void flush_cache_mm(struct mm_struct *mm)
  737. {
  738. flush_cache_all();
  739. }
  740. void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
  741. unsigned long pfn)
  742. {
  743. unsigned int paddr = pfn << PAGE_SHIFT;
  744. u_vaddr &= PAGE_MASK;
  745. __flush_dcache_page(paddr, u_vaddr);
  746. if (vma->vm_flags & VM_EXEC)
  747. __inv_icache_page(paddr, u_vaddr);
  748. }
  749. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  750. unsigned long end)
  751. {
  752. flush_cache_all();
  753. }
  754. void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  755. unsigned long u_vaddr)
  756. {
  757. /* TBD: do we really need to clear the kernel mapping */
  758. __flush_dcache_page(page_address(page), u_vaddr);
  759. __flush_dcache_page(page_address(page), page_address(page));
  760. }
  761. #endif
  762. void copy_user_highpage(struct page *to, struct page *from,
  763. unsigned long u_vaddr, struct vm_area_struct *vma)
  764. {
  765. void *kfrom = kmap_atomic(from);
  766. void *kto = kmap_atomic(to);
  767. int clean_src_k_mappings = 0;
  768. /*
  769. * If SRC page was already mapped in userspace AND it's U-mapping is
  770. * not congruent with K-mapping, sync former to physical page so that
  771. * K-mapping in memcpy below, sees the right data
  772. *
  773. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  774. * equally valid for SRC page as well
  775. *
  776. * For !VIPT cache, all of this gets compiled out as
  777. * addr_not_cache_congruent() is 0
  778. */
  779. if (page_mapcount(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  780. __flush_dcache_page((unsigned long)kfrom, u_vaddr);
  781. clean_src_k_mappings = 1;
  782. }
  783. copy_page(kto, kfrom);
  784. /*
  785. * Mark DST page K-mapping as dirty for a later finalization by
  786. * update_mmu_cache(). Although the finalization could have been done
  787. * here as well (given that both vaddr/paddr are available).
  788. * But update_mmu_cache() already has code to do that for other
  789. * non copied user pages (e.g. read faults which wire in pagecache page
  790. * directly).
  791. */
  792. clear_bit(PG_dc_clean, &to->flags);
  793. /*
  794. * if SRC was already usermapped and non-congruent to kernel mapping
  795. * sync the kernel mapping back to physical page
  796. */
  797. if (clean_src_k_mappings) {
  798. __flush_dcache_page((unsigned long)kfrom, (unsigned long)kfrom);
  799. set_bit(PG_dc_clean, &from->flags);
  800. } else {
  801. clear_bit(PG_dc_clean, &from->flags);
  802. }
  803. kunmap_atomic(kto);
  804. kunmap_atomic(kfrom);
  805. }
  806. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  807. {
  808. clear_page(to);
  809. clear_bit(PG_dc_clean, &page->flags);
  810. }
  811. /**********************************************************************
  812. * Explicit Cache flush request from user space via syscall
  813. * Needed for JITs which generate code on the fly
  814. */
  815. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  816. {
  817. /* TBD: optimize this */
  818. flush_cache_all();
  819. return 0;
  820. }
  821. /*
  822. * IO-Coherency (IOC) setup rules:
  823. *
  824. * 1. Needs to be at system level, so only once by Master core
  825. * Non-Masters need not be accessing caches at that time
  826. * - They are either HALT_ON_RESET and kick started much later or
  827. * - if run on reset, need to ensure that arc_platform_smp_wait_to_boot()
  828. * doesn't perturb caches or coherency unit
  829. *
  830. * 2. caches (L1 and SLC) need to be purged (flush+inv) before setting up IOC,
  831. * otherwise any straggler data might behave strangely post IOC enabling
  832. *
  833. * 3. All Caches need to be disabled when setting up IOC to elide any in-flight
  834. * Coherency transactions
  835. */
  836. noinline void __init arc_ioc_setup(void)
  837. {
  838. unsigned int ap_sz;
  839. /* Flush + invalidate + disable L1 dcache */
  840. __dc_disable();
  841. /* Flush + invalidate SLC */
  842. if (read_aux_reg(ARC_REG_SLC_BCR))
  843. slc_entire_op(OP_FLUSH_N_INV);
  844. /* IOC Aperture start: TDB: handle non default CONFIG_LINUX_LINK_BASE */
  845. write_aux_reg(ARC_REG_IO_COH_AP0_BASE, 0x80000);
  846. /*
  847. * IOC Aperture size:
  848. * decoded as 2 ^ (SIZE + 2) KB: so setting 0x11 implies 512M
  849. * TBD: fix for PGU + 1GB of low mem
  850. * TBD: fix for PAE
  851. */
  852. ap_sz = order_base_2(arc_get_mem_sz()/1024) - 2;
  853. write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, ap_sz);
  854. write_aux_reg(ARC_REG_IO_COH_PARTIAL, 1);
  855. write_aux_reg(ARC_REG_IO_COH_ENABLE, 1);
  856. /* Re-enable L1 dcache */
  857. __dc_enable();
  858. }
  859. void __init arc_cache_init_master(void)
  860. {
  861. unsigned int __maybe_unused cpu = smp_processor_id();
  862. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  863. struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
  864. if (!ic->line_len)
  865. panic("cache support enabled but non-existent cache\n");
  866. if (ic->line_len != L1_CACHE_BYTES)
  867. panic("ICache line [%d] != kernel Config [%d]",
  868. ic->line_len, L1_CACHE_BYTES);
  869. /*
  870. * In MMU v4 (HS38x) the aliasing icache config uses IVIL/PTAG
  871. * pair to provide vaddr/paddr respectively, just as in MMU v3
  872. */
  873. if (is_isa_arcv2() && ic->alias)
  874. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  875. else
  876. _cache_line_loop_ic_fn = __cache_line_loop;
  877. }
  878. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  879. struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
  880. if (!dc->line_len)
  881. panic("cache support enabled but non-existent cache\n");
  882. if (dc->line_len != L1_CACHE_BYTES)
  883. panic("DCache line [%d] != kernel Config [%d]",
  884. dc->line_len, L1_CACHE_BYTES);
  885. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  886. if (is_isa_arcompact()) {
  887. int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
  888. int num_colors = dc->sz_k/dc->assoc/TO_KB(PAGE_SIZE);
  889. if (dc->alias) {
  890. if (!handled)
  891. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  892. if (CACHE_COLORS_NUM != num_colors)
  893. panic("CACHE_COLORS_NUM not optimized for config\n");
  894. } else if (!dc->alias && handled) {
  895. panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  896. }
  897. }
  898. }
  899. /* Note that SLC disable not formally supported till HS 3.0 */
  900. if (is_isa_arcv2() && l2_line_sz && !slc_enable)
  901. arc_slc_disable();
  902. if (is_isa_arcv2() && ioc_enable)
  903. arc_ioc_setup();
  904. if (is_isa_arcv2() && ioc_enable) {
  905. __dma_cache_wback_inv = __dma_cache_wback_inv_ioc;
  906. __dma_cache_inv = __dma_cache_inv_ioc;
  907. __dma_cache_wback = __dma_cache_wback_ioc;
  908. } else if (is_isa_arcv2() && l2_line_sz && slc_enable) {
  909. __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
  910. __dma_cache_inv = __dma_cache_inv_slc;
  911. __dma_cache_wback = __dma_cache_wback_slc;
  912. } else {
  913. __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
  914. __dma_cache_inv = __dma_cache_inv_l1;
  915. __dma_cache_wback = __dma_cache_wback_l1;
  916. }
  917. }
  918. void __ref arc_cache_init(void)
  919. {
  920. unsigned int __maybe_unused cpu = smp_processor_id();
  921. char str[256];
  922. printk(arc_cache_mumbojumbo(0, str, sizeof(str)));
  923. /*
  924. * Only master CPU needs to execute rest of function:
  925. * - Assume SMP so all cores will have same cache config so
  926. * any geomtry checks will be same for all
  927. * - IOC setup / dma callbacks only need to be setup once
  928. */
  929. if (!cpu)
  930. arc_cache_init_master();
  931. }