cache.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARC Cache Management
  4. *
  5. * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
  6. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/sched.h>
  11. #include <linux/cache.h>
  12. #include <linux/mmu_context.h>
  13. #include <linux/syscalls.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/pagemap.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/cachectl.h>
  18. #include <asm/setup.h>
  19. #ifdef CONFIG_ISA_ARCV2
  20. #define USE_RGN_FLSH 1
  21. #endif
  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 op, const int full_page);
  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 (per-device) "));
  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. /*
  98. * As for today we don't support both IOC and ZONE_HIGHMEM enabled
  99. * simultaneously. This happens because as of today IOC aperture covers
  100. * only ZONE_NORMAL (low mem) and any dma transactions outside this
  101. * region won't be HW coherent.
  102. * If we want to use both IOC and ZONE_HIGHMEM we can use
  103. * bounce_buffer to handle dma transactions to HIGHMEM.
  104. * Also it is possible to modify dma_direct cache ops or increase IOC
  105. * aperture size if we are planning to use HIGHMEM without PAE.
  106. */
  107. if (IS_ENABLED(CONFIG_HIGHMEM) || is_pae40_enabled())
  108. ioc_enable = 0;
  109. } else {
  110. ioc_enable = 0;
  111. }
  112. /* HS 2.0 didn't have AUX_VOL */
  113. if (cpuinfo_arc700[cpu].core.family > 0x51) {
  114. READ_BCR(AUX_VOL, vol);
  115. perip_base = vol.start << 28;
  116. /* HS 3.0 has limit and strict-ordering fields */
  117. if (cpuinfo_arc700[cpu].core.family > 0x52)
  118. perip_end = (vol.limit << 28) - 1;
  119. }
  120. }
  121. void read_decode_cache_bcr(void)
  122. {
  123. struct cpuinfo_arc_cache *p_ic, *p_dc;
  124. unsigned int cpu = smp_processor_id();
  125. struct bcr_cache {
  126. #ifdef CONFIG_CPU_BIG_ENDIAN
  127. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  128. #else
  129. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  130. #endif
  131. } ibcr, dbcr;
  132. p_ic = &cpuinfo_arc700[cpu].icache;
  133. READ_BCR(ARC_REG_IC_BCR, ibcr);
  134. if (!ibcr.ver)
  135. goto dc_chk;
  136. if (ibcr.ver <= 3) {
  137. BUG_ON(ibcr.config != 3);
  138. p_ic->assoc = 2; /* Fixed to 2w set assoc */
  139. } else if (ibcr.ver >= 4) {
  140. p_ic->assoc = 1 << ibcr.config; /* 1,2,4,8 */
  141. }
  142. p_ic->line_len = 8 << ibcr.line_len;
  143. p_ic->sz_k = 1 << (ibcr.sz - 1);
  144. p_ic->vipt = 1;
  145. p_ic->alias = p_ic->sz_k/p_ic->assoc/TO_KB(PAGE_SIZE) > 1;
  146. dc_chk:
  147. p_dc = &cpuinfo_arc700[cpu].dcache;
  148. READ_BCR(ARC_REG_DC_BCR, dbcr);
  149. if (!dbcr.ver)
  150. goto slc_chk;
  151. if (dbcr.ver <= 3) {
  152. BUG_ON(dbcr.config != 2);
  153. p_dc->assoc = 4; /* Fixed to 4w set assoc */
  154. p_dc->vipt = 1;
  155. p_dc->alias = p_dc->sz_k/p_dc->assoc/TO_KB(PAGE_SIZE) > 1;
  156. } else if (dbcr.ver >= 4) {
  157. p_dc->assoc = 1 << dbcr.config; /* 1,2,4,8 */
  158. p_dc->vipt = 0;
  159. p_dc->alias = 0; /* PIPT so can't VIPT alias */
  160. }
  161. p_dc->line_len = 16 << dbcr.line_len;
  162. p_dc->sz_k = 1 << (dbcr.sz - 1);
  163. slc_chk:
  164. if (is_isa_arcv2())
  165. read_decode_cache_bcr_arcv2(cpu);
  166. }
  167. /*
  168. * Line Operation on {I,D}-Cache
  169. */
  170. #define OP_INV 0x1
  171. #define OP_FLUSH 0x2
  172. #define OP_FLUSH_N_INV 0x3
  173. #define OP_INV_IC 0x4
  174. /*
  175. * Cache Flush programming model
  176. *
  177. * ARC700 MMUv3 I$ and D$ are both VIPT and can potentially alias.
  178. * Programming model requires both paddr and vaddr irrespecive of aliasing
  179. * considerations:
  180. * - vaddr in {I,D}C_IV?L
  181. * - paddr in {I,D}C_PTAG
  182. *
  183. * In HS38x (MMUv4), D$ is PIPT, I$ is VIPT and can still alias.
  184. * Programming model is different for aliasing vs. non-aliasing I$
  185. * - D$ / Non-aliasing I$: only paddr in {I,D}C_IV?L
  186. * - Aliasing I$: same as ARC700 above (so MMUv3 routine used for MMUv4 I$)
  187. *
  188. * - If PAE40 is enabled, independent of aliasing considerations, the higher
  189. * bits needs to be written into PTAG_HI
  190. */
  191. static inline
  192. void __cache_line_loop_v3(phys_addr_t paddr, unsigned long vaddr,
  193. unsigned long sz, const int op, const int full_page)
  194. {
  195. unsigned int aux_cmd, aux_tag;
  196. int num_lines;
  197. if (op == OP_INV_IC) {
  198. aux_cmd = ARC_REG_IC_IVIL;
  199. aux_tag = ARC_REG_IC_PTAG;
  200. } else {
  201. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  202. aux_tag = ARC_REG_DC_PTAG;
  203. }
  204. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  205. * and have @paddr - aligned to cache line and integral @num_lines.
  206. * This however can be avoided for page sized since:
  207. * -@paddr will be cache-line aligned already (being page aligned)
  208. * -@sz will be integral multiple of line size (being page sized).
  209. */
  210. if (!full_page) {
  211. sz += paddr & ~CACHE_LINE_MASK;
  212. paddr &= CACHE_LINE_MASK;
  213. vaddr &= CACHE_LINE_MASK;
  214. }
  215. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  216. /*
  217. * MMUv3, cache ops require paddr in PTAG reg
  218. * if V-P const for loop, PTAG can be written once outside loop
  219. */
  220. if (full_page)
  221. write_aux_reg(aux_tag, paddr);
  222. /*
  223. * This is technically for MMU v4, using the MMU v3 programming model
  224. * Special work for HS38 aliasing I-cache configuration with PAE40
  225. * - upper 8 bits of paddr need to be written into PTAG_HI
  226. * - (and needs to be written before the lower 32 bits)
  227. * Note that PTAG_HI is hoisted outside the line loop
  228. */
  229. if (is_pae40_enabled() && op == OP_INV_IC)
  230. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  231. while (num_lines-- > 0) {
  232. if (!full_page) {
  233. write_aux_reg(aux_tag, paddr);
  234. paddr += L1_CACHE_BYTES;
  235. }
  236. write_aux_reg(aux_cmd, vaddr);
  237. vaddr += L1_CACHE_BYTES;
  238. }
  239. }
  240. #ifndef USE_RGN_FLSH
  241. /*
  242. */
  243. static inline
  244. void __cache_line_loop_v4(phys_addr_t paddr, unsigned long vaddr,
  245. unsigned long sz, const int op, const int full_page)
  246. {
  247. unsigned int aux_cmd;
  248. int num_lines;
  249. if (op == OP_INV_IC) {
  250. aux_cmd = ARC_REG_IC_IVIL;
  251. } else {
  252. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  253. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  254. }
  255. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  256. * and have @paddr - aligned to cache line and integral @num_lines.
  257. * This however can be avoided for page sized since:
  258. * -@paddr will be cache-line aligned already (being page aligned)
  259. * -@sz will be integral multiple of line size (being page sized).
  260. */
  261. if (!full_page) {
  262. sz += paddr & ~CACHE_LINE_MASK;
  263. paddr &= CACHE_LINE_MASK;
  264. }
  265. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  266. /*
  267. * For HS38 PAE40 configuration
  268. * - upper 8 bits of paddr need to be written into PTAG_HI
  269. * - (and needs to be written before the lower 32 bits)
  270. */
  271. if (is_pae40_enabled()) {
  272. if (op == OP_INV_IC)
  273. /*
  274. * Non aliasing I-cache in HS38,
  275. * aliasing I-cache handled in __cache_line_loop_v3()
  276. */
  277. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  278. else
  279. write_aux_reg(ARC_REG_DC_PTAG_HI, (u64)paddr >> 32);
  280. }
  281. while (num_lines-- > 0) {
  282. write_aux_reg(aux_cmd, paddr);
  283. paddr += L1_CACHE_BYTES;
  284. }
  285. }
  286. #else
  287. /*
  288. * optimized flush operation which takes a region as opposed to iterating per line
  289. */
  290. static inline
  291. void __cache_line_loop_v4(phys_addr_t paddr, unsigned long vaddr,
  292. unsigned long sz, const int op, const int full_page)
  293. {
  294. unsigned int s, e;
  295. /* Only for Non aliasing I-cache in HS38 */
  296. if (op == OP_INV_IC) {
  297. s = ARC_REG_IC_IVIR;
  298. e = ARC_REG_IC_ENDR;
  299. } else {
  300. s = ARC_REG_DC_STARTR;
  301. e = ARC_REG_DC_ENDR;
  302. }
  303. if (!full_page) {
  304. /* for any leading gap between @paddr and start of cache line */
  305. sz += paddr & ~CACHE_LINE_MASK;
  306. paddr &= CACHE_LINE_MASK;
  307. /*
  308. * account for any trailing gap to end of cache line
  309. * this is equivalent to DIV_ROUND_UP() in line ops above
  310. */
  311. sz += L1_CACHE_BYTES - 1;
  312. }
  313. if (is_pae40_enabled()) {
  314. /* TBD: check if crossing 4TB boundary */
  315. if (op == OP_INV_IC)
  316. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  317. else
  318. write_aux_reg(ARC_REG_DC_PTAG_HI, (u64)paddr >> 32);
  319. }
  320. /* ENDR needs to be set ahead of START */
  321. write_aux_reg(e, paddr + sz); /* ENDR is exclusive */
  322. write_aux_reg(s, paddr);
  323. /* caller waits on DC_CTRL.FS */
  324. }
  325. #endif
  326. #ifdef CONFIG_ARC_MMU_V3
  327. #define __cache_line_loop __cache_line_loop_v3
  328. #else
  329. #define __cache_line_loop __cache_line_loop_v4
  330. #endif
  331. #ifdef CONFIG_ARC_HAS_DCACHE
  332. /***************************************************************
  333. * Machine specific helpers for Entire D-Cache or Per Line ops
  334. */
  335. #ifndef USE_RGN_FLSH
  336. /*
  337. * this version avoids extra read/write of DC_CTRL for flush or invalid ops
  338. * in the non region flush regime (such as for ARCompact)
  339. */
  340. static inline void __before_dc_op(const int op)
  341. {
  342. if (op == OP_FLUSH_N_INV) {
  343. /* Dcache provides 2 cmd: FLUSH or INV
  344. * INV in turn has sub-modes: DISCARD or FLUSH-BEFORE
  345. * flush-n-inv is achieved by INV cmd but with IM=1
  346. * So toggle INV sub-mode depending on op request and default
  347. */
  348. const unsigned int ctl = ARC_REG_DC_CTRL;
  349. write_aux_reg(ctl, read_aux_reg(ctl) | DC_CTRL_INV_MODE_FLUSH);
  350. }
  351. }
  352. #else
  353. static inline void __before_dc_op(const int op)
  354. {
  355. const unsigned int ctl = ARC_REG_DC_CTRL;
  356. unsigned int val = read_aux_reg(ctl);
  357. if (op == OP_FLUSH_N_INV) {
  358. val |= DC_CTRL_INV_MODE_FLUSH;
  359. }
  360. if (op != OP_INV_IC) {
  361. /*
  362. * Flush / Invalidate is provided by DC_CTRL.RNG_OP 0 or 1
  363. * combined Flush-n-invalidate uses DC_CTRL.IM = 1 set above
  364. */
  365. val &= ~DC_CTRL_RGN_OP_MSK;
  366. if (op & OP_INV)
  367. val |= DC_CTRL_RGN_OP_INV;
  368. }
  369. write_aux_reg(ctl, val);
  370. }
  371. #endif
  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. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  422. unsigned long flags;
  423. local_irq_save(flags);
  424. __before_dc_op(op);
  425. __cache_line_loop(paddr, vaddr, sz, op, full_page);
  426. __after_dc_op(op);
  427. local_irq_restore(flags);
  428. }
  429. #else
  430. #define __dc_entire_op(op)
  431. #define __dc_disable()
  432. #define __dc_enable()
  433. #define __dc_line_op(paddr, vaddr, sz, op)
  434. #define __dc_line_op_k(paddr, sz, op)
  435. #endif /* CONFIG_ARC_HAS_DCACHE */
  436. #ifdef CONFIG_ARC_HAS_ICACHE
  437. static inline void __ic_entire_inv(void)
  438. {
  439. write_aux_reg(ARC_REG_IC_IVIC, 1);
  440. read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
  441. }
  442. static inline void
  443. __ic_line_inv_vaddr_local(phys_addr_t paddr, unsigned long vaddr,
  444. unsigned long sz)
  445. {
  446. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  447. unsigned long flags;
  448. local_irq_save(flags);
  449. (*_cache_line_loop_ic_fn)(paddr, vaddr, sz, OP_INV_IC, full_page);
  450. local_irq_restore(flags);
  451. }
  452. #ifndef CONFIG_SMP
  453. #define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s)
  454. #else
  455. struct ic_inv_args {
  456. phys_addr_t paddr, vaddr;
  457. int sz;
  458. };
  459. static void __ic_line_inv_vaddr_helper(void *info)
  460. {
  461. struct ic_inv_args *ic_inv = info;
  462. __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
  463. }
  464. static void __ic_line_inv_vaddr(phys_addr_t paddr, unsigned long vaddr,
  465. unsigned long sz)
  466. {
  467. struct ic_inv_args ic_inv = {
  468. .paddr = paddr,
  469. .vaddr = vaddr,
  470. .sz = sz
  471. };
  472. on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
  473. }
  474. #endif /* CONFIG_SMP */
  475. #else /* !CONFIG_ARC_HAS_ICACHE */
  476. #define __ic_entire_inv()
  477. #define __ic_line_inv_vaddr(pstart, vstart, sz)
  478. #endif /* CONFIG_ARC_HAS_ICACHE */
  479. noinline void slc_op_rgn(phys_addr_t paddr, unsigned long sz, const int op)
  480. {
  481. #ifdef CONFIG_ISA_ARCV2
  482. /*
  483. * SLC is shared between all cores and concurrent aux operations from
  484. * multiple cores need to be serialized using a spinlock
  485. * A concurrent operation can be silently ignored and/or the old/new
  486. * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
  487. * below)
  488. */
  489. static DEFINE_SPINLOCK(lock);
  490. unsigned long flags;
  491. unsigned int ctrl;
  492. phys_addr_t end;
  493. spin_lock_irqsave(&lock, flags);
  494. /*
  495. * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
  496. * - b'000 (default) is Flush,
  497. * - b'001 is Invalidate if CTRL.IM == 0
  498. * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
  499. */
  500. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  501. /* Don't rely on default value of IM bit */
  502. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  503. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  504. else
  505. ctrl |= SLC_CTRL_IM;
  506. if (op & OP_INV)
  507. ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
  508. else
  509. ctrl &= ~SLC_CTRL_RGN_OP_INV;
  510. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  511. /*
  512. * Lower bits are ignored, no need to clip
  513. * END needs to be setup before START (latter triggers the operation)
  514. * END can't be same as START, so add (l2_line_sz - 1) to sz
  515. */
  516. end = paddr + sz + l2_line_sz - 1;
  517. if (is_pae40_enabled())
  518. write_aux_reg(ARC_REG_SLC_RGN_END1, upper_32_bits(end));
  519. write_aux_reg(ARC_REG_SLC_RGN_END, lower_32_bits(end));
  520. if (is_pae40_enabled())
  521. write_aux_reg(ARC_REG_SLC_RGN_START1, upper_32_bits(paddr));
  522. write_aux_reg(ARC_REG_SLC_RGN_START, lower_32_bits(paddr));
  523. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  524. read_aux_reg(ARC_REG_SLC_CTRL);
  525. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  526. spin_unlock_irqrestore(&lock, flags);
  527. #endif
  528. }
  529. noinline void slc_op_line(phys_addr_t paddr, unsigned long sz, const int op)
  530. {
  531. #ifdef CONFIG_ISA_ARCV2
  532. /*
  533. * SLC is shared between all cores and concurrent aux operations from
  534. * multiple cores need to be serialized using a spinlock
  535. * A concurrent operation can be silently ignored and/or the old/new
  536. * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
  537. * below)
  538. */
  539. static DEFINE_SPINLOCK(lock);
  540. const unsigned long SLC_LINE_MASK = ~(l2_line_sz - 1);
  541. unsigned int ctrl, cmd;
  542. unsigned long flags;
  543. int num_lines;
  544. spin_lock_irqsave(&lock, flags);
  545. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  546. /* Don't rely on default value of IM bit */
  547. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  548. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  549. else
  550. ctrl |= SLC_CTRL_IM;
  551. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  552. cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL;
  553. sz += paddr & ~SLC_LINE_MASK;
  554. paddr &= SLC_LINE_MASK;
  555. num_lines = DIV_ROUND_UP(sz, l2_line_sz);
  556. while (num_lines-- > 0) {
  557. write_aux_reg(cmd, paddr);
  558. paddr += l2_line_sz;
  559. }
  560. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  561. read_aux_reg(ARC_REG_SLC_CTRL);
  562. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  563. spin_unlock_irqrestore(&lock, flags);
  564. #endif
  565. }
  566. #define slc_op(paddr, sz, op) slc_op_rgn(paddr, sz, op)
  567. noinline static void slc_entire_op(const int op)
  568. {
  569. unsigned int ctrl, r = ARC_REG_SLC_CTRL;
  570. ctrl = read_aux_reg(r);
  571. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  572. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  573. else
  574. ctrl |= SLC_CTRL_IM;
  575. write_aux_reg(r, ctrl);
  576. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  577. write_aux_reg(ARC_REG_SLC_INVALIDATE, 0x1);
  578. else
  579. write_aux_reg(ARC_REG_SLC_FLUSH, 0x1);
  580. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  581. read_aux_reg(r);
  582. /* Important to wait for flush to complete */
  583. while (read_aux_reg(r) & SLC_CTRL_BUSY);
  584. }
  585. static inline void arc_slc_disable(void)
  586. {
  587. const int r = ARC_REG_SLC_CTRL;
  588. slc_entire_op(OP_FLUSH_N_INV);
  589. write_aux_reg(r, read_aux_reg(r) | SLC_CTRL_DIS);
  590. }
  591. static inline void arc_slc_enable(void)
  592. {
  593. const int r = ARC_REG_SLC_CTRL;
  594. write_aux_reg(r, read_aux_reg(r) & ~SLC_CTRL_DIS);
  595. }
  596. /***********************************************************
  597. * Exported APIs
  598. */
  599. /*
  600. * Handle cache congruency of kernel and userspace mappings of page when kernel
  601. * writes-to/reads-from
  602. *
  603. * The idea is to defer flushing of kernel mapping after a WRITE, possible if:
  604. * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent
  605. * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache)
  606. * -In SMP, if hardware caches are coherent
  607. *
  608. * There's a corollary case, where kernel READs from a userspace mapped page.
  609. * If the U-mapping is not congruent to K-mapping, former needs flushing.
  610. */
  611. void flush_dcache_page(struct page *page)
  612. {
  613. struct address_space *mapping;
  614. if (!cache_is_vipt_aliasing()) {
  615. clear_bit(PG_dc_clean, &page->flags);
  616. return;
  617. }
  618. /* don't handle anon pages here */
  619. mapping = page_mapping_file(page);
  620. if (!mapping)
  621. return;
  622. /*
  623. * pagecache page, file not yet mapped to userspace
  624. * Make a note that K-mapping is dirty
  625. */
  626. if (!mapping_mapped(mapping)) {
  627. clear_bit(PG_dc_clean, &page->flags);
  628. } else if (page_mapcount(page)) {
  629. /* kernel reading from page with U-mapping */
  630. phys_addr_t paddr = (unsigned long)page_address(page);
  631. unsigned long vaddr = page->index << PAGE_SHIFT;
  632. if (addr_not_cache_congruent(paddr, vaddr))
  633. __flush_dcache_page(paddr, vaddr);
  634. }
  635. }
  636. EXPORT_SYMBOL(flush_dcache_page);
  637. /*
  638. * DMA ops for systems with L1 cache only
  639. * Make memory coherent with L1 cache by flushing/invalidating L1 lines
  640. */
  641. static void __dma_cache_wback_inv_l1(phys_addr_t start, unsigned long sz)
  642. {
  643. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  644. }
  645. static void __dma_cache_inv_l1(phys_addr_t start, unsigned long sz)
  646. {
  647. __dc_line_op_k(start, sz, OP_INV);
  648. }
  649. static void __dma_cache_wback_l1(phys_addr_t start, unsigned long sz)
  650. {
  651. __dc_line_op_k(start, sz, OP_FLUSH);
  652. }
  653. /*
  654. * DMA ops for systems with both L1 and L2 caches, but without IOC
  655. * Both L1 and L2 lines need to be explicitly flushed/invalidated
  656. */
  657. static void __dma_cache_wback_inv_slc(phys_addr_t start, unsigned long sz)
  658. {
  659. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  660. slc_op(start, sz, OP_FLUSH_N_INV);
  661. }
  662. static void __dma_cache_inv_slc(phys_addr_t start, unsigned long sz)
  663. {
  664. __dc_line_op_k(start, sz, OP_INV);
  665. slc_op(start, sz, OP_INV);
  666. }
  667. static void __dma_cache_wback_slc(phys_addr_t start, unsigned long sz)
  668. {
  669. __dc_line_op_k(start, sz, OP_FLUSH);
  670. slc_op(start, sz, OP_FLUSH);
  671. }
  672. /*
  673. * Exported DMA API
  674. */
  675. void dma_cache_wback_inv(phys_addr_t start, unsigned long sz)
  676. {
  677. __dma_cache_wback_inv(start, sz);
  678. }
  679. EXPORT_SYMBOL(dma_cache_wback_inv);
  680. void dma_cache_inv(phys_addr_t start, unsigned long sz)
  681. {
  682. __dma_cache_inv(start, sz);
  683. }
  684. EXPORT_SYMBOL(dma_cache_inv);
  685. void dma_cache_wback(phys_addr_t start, unsigned long sz)
  686. {
  687. __dma_cache_wback(start, sz);
  688. }
  689. EXPORT_SYMBOL(dma_cache_wback);
  690. /*
  691. * This is API for making I/D Caches consistent when modifying
  692. * kernel code (loadable modules, kprobes, kgdb...)
  693. * This is called on insmod, with kernel virtual address for CODE of
  694. * the module. ARC cache maintenance ops require PHY address thus we
  695. * need to convert vmalloc addr to PHY addr
  696. */
  697. void flush_icache_range(unsigned long kstart, unsigned long kend)
  698. {
  699. unsigned int tot_sz;
  700. WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
  701. /* Shortcut for bigger flush ranges.
  702. * Here we don't care if this was kernel virtual or phy addr
  703. */
  704. tot_sz = kend - kstart;
  705. if (tot_sz > PAGE_SIZE) {
  706. flush_cache_all();
  707. return;
  708. }
  709. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  710. if (likely(kstart > PAGE_OFFSET)) {
  711. /*
  712. * The 2nd arg despite being paddr will be used to index icache
  713. * This is OK since no alternate virtual mappings will exist
  714. * given the callers for this case: kprobe/kgdb in built-in
  715. * kernel code only.
  716. */
  717. __sync_icache_dcache(kstart, kstart, kend - kstart);
  718. return;
  719. }
  720. /*
  721. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  722. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  723. * handling of kernel vaddr.
  724. *
  725. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  726. * it still needs to handle a 2 page scenario, where the range
  727. * straddles across 2 virtual pages and hence need for loop
  728. */
  729. while (tot_sz > 0) {
  730. unsigned int off, sz;
  731. unsigned long phy, pfn;
  732. off = kstart % PAGE_SIZE;
  733. pfn = vmalloc_to_pfn((void *)kstart);
  734. phy = (pfn << PAGE_SHIFT) + off;
  735. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  736. __sync_icache_dcache(phy, kstart, sz);
  737. kstart += sz;
  738. tot_sz -= sz;
  739. }
  740. }
  741. EXPORT_SYMBOL(flush_icache_range);
  742. /*
  743. * General purpose helper to make I and D cache lines consistent.
  744. * @paddr is phy addr of region
  745. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  746. * However in one instance, when called by kprobe (for a breakpt in
  747. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  748. * use a paddr to index the cache (despite VIPT). This is fine since a
  749. * builtin kernel page will not have any virtual mappings.
  750. * kprobe on loadable module will be kernel vaddr.
  751. */
  752. void __sync_icache_dcache(phys_addr_t paddr, unsigned long vaddr, int len)
  753. {
  754. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  755. __ic_line_inv_vaddr(paddr, vaddr, len);
  756. }
  757. /* wrapper to compile time eliminate alignment checks in flush loop */
  758. void __inv_icache_page(phys_addr_t paddr, unsigned long vaddr)
  759. {
  760. __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE);
  761. }
  762. /*
  763. * wrapper to clearout kernel or userspace mappings of a page
  764. * For kernel mappings @vaddr == @paddr
  765. */
  766. void __flush_dcache_page(phys_addr_t paddr, unsigned long vaddr)
  767. {
  768. __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV);
  769. }
  770. noinline void flush_cache_all(void)
  771. {
  772. unsigned long flags;
  773. local_irq_save(flags);
  774. __ic_entire_inv();
  775. __dc_entire_op(OP_FLUSH_N_INV);
  776. local_irq_restore(flags);
  777. }
  778. #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING
  779. void flush_cache_mm(struct mm_struct *mm)
  780. {
  781. flush_cache_all();
  782. }
  783. void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr,
  784. unsigned long pfn)
  785. {
  786. phys_addr_t paddr = pfn << PAGE_SHIFT;
  787. u_vaddr &= PAGE_MASK;
  788. __flush_dcache_page(paddr, u_vaddr);
  789. if (vma->vm_flags & VM_EXEC)
  790. __inv_icache_page(paddr, u_vaddr);
  791. }
  792. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  793. unsigned long end)
  794. {
  795. flush_cache_all();
  796. }
  797. void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  798. unsigned long u_vaddr)
  799. {
  800. /* TBD: do we really need to clear the kernel mapping */
  801. __flush_dcache_page((phys_addr_t)page_address(page), u_vaddr);
  802. __flush_dcache_page((phys_addr_t)page_address(page),
  803. (phys_addr_t)page_address(page));
  804. }
  805. #endif
  806. void copy_user_highpage(struct page *to, struct page *from,
  807. unsigned long u_vaddr, struct vm_area_struct *vma)
  808. {
  809. void *kfrom = kmap_atomic(from);
  810. void *kto = kmap_atomic(to);
  811. int clean_src_k_mappings = 0;
  812. /*
  813. * If SRC page was already mapped in userspace AND it's U-mapping is
  814. * not congruent with K-mapping, sync former to physical page so that
  815. * K-mapping in memcpy below, sees the right data
  816. *
  817. * Note that while @u_vaddr refers to DST page's userspace vaddr, it is
  818. * equally valid for SRC page as well
  819. *
  820. * For !VIPT cache, all of this gets compiled out as
  821. * addr_not_cache_congruent() is 0
  822. */
  823. if (page_mapcount(from) && addr_not_cache_congruent(kfrom, u_vaddr)) {
  824. __flush_dcache_page((unsigned long)kfrom, u_vaddr);
  825. clean_src_k_mappings = 1;
  826. }
  827. copy_page(kto, kfrom);
  828. /*
  829. * Mark DST page K-mapping as dirty for a later finalization by
  830. * update_mmu_cache(). Although the finalization could have been done
  831. * here as well (given that both vaddr/paddr are available).
  832. * But update_mmu_cache() already has code to do that for other
  833. * non copied user pages (e.g. read faults which wire in pagecache page
  834. * directly).
  835. */
  836. clear_bit(PG_dc_clean, &to->flags);
  837. /*
  838. * if SRC was already usermapped and non-congruent to kernel mapping
  839. * sync the kernel mapping back to physical page
  840. */
  841. if (clean_src_k_mappings) {
  842. __flush_dcache_page((unsigned long)kfrom, (unsigned long)kfrom);
  843. set_bit(PG_dc_clean, &from->flags);
  844. } else {
  845. clear_bit(PG_dc_clean, &from->flags);
  846. }
  847. kunmap_atomic(kto);
  848. kunmap_atomic(kfrom);
  849. }
  850. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  851. {
  852. clear_page(to);
  853. clear_bit(PG_dc_clean, &page->flags);
  854. }
  855. EXPORT_SYMBOL(clear_user_page);
  856. /**********************************************************************
  857. * Explicit Cache flush request from user space via syscall
  858. * Needed for JITs which generate code on the fly
  859. */
  860. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  861. {
  862. /* TBD: optimize this */
  863. flush_cache_all();
  864. return 0;
  865. }
  866. /*
  867. * IO-Coherency (IOC) setup rules:
  868. *
  869. * 1. Needs to be at system level, so only once by Master core
  870. * Non-Masters need not be accessing caches at that time
  871. * - They are either HALT_ON_RESET and kick started much later or
  872. * - if run on reset, need to ensure that arc_platform_smp_wait_to_boot()
  873. * doesn't perturb caches or coherency unit
  874. *
  875. * 2. caches (L1 and SLC) need to be purged (flush+inv) before setting up IOC,
  876. * otherwise any straggler data might behave strangely post IOC enabling
  877. *
  878. * 3. All Caches need to be disabled when setting up IOC to elide any in-flight
  879. * Coherency transactions
  880. */
  881. noinline void __init arc_ioc_setup(void)
  882. {
  883. unsigned int ioc_base, mem_sz;
  884. /*
  885. * If IOC was already enabled (due to bootloader) it technically needs to
  886. * be reconfigured with aperture base,size corresponding to Linux memory map
  887. * which will certainly be different than uboot's. But disabling and
  888. * reenabling IOC when DMA might be potentially active is tricky business.
  889. * To avoid random memory issues later, just panic here and ask user to
  890. * upgrade bootloader to one which doesn't enable IOC
  891. */
  892. if (read_aux_reg(ARC_REG_IO_COH_ENABLE) & ARC_IO_COH_ENABLE_BIT)
  893. panic("IOC already enabled, please upgrade bootloader!\n");
  894. if (!ioc_enable)
  895. return;
  896. /* Flush + invalidate + disable L1 dcache */
  897. __dc_disable();
  898. /* Flush + invalidate SLC */
  899. if (read_aux_reg(ARC_REG_SLC_BCR))
  900. slc_entire_op(OP_FLUSH_N_INV);
  901. /*
  902. * currently IOC Aperture covers entire DDR
  903. * TBD: fix for PGU + 1GB of low mem
  904. * TBD: fix for PAE
  905. */
  906. mem_sz = arc_get_mem_sz();
  907. if (!is_power_of_2(mem_sz) || mem_sz < 4096)
  908. panic("IOC Aperture size must be power of 2 larger than 4KB");
  909. /*
  910. * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
  911. * so setting 0x11 implies 512MB, 0x12 implies 1GB...
  912. */
  913. write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, order_base_2(mem_sz >> 10) - 2);
  914. /* for now assume kernel base is start of IOC aperture */
  915. ioc_base = CONFIG_LINUX_RAM_BASE;
  916. if (ioc_base % mem_sz != 0)
  917. panic("IOC Aperture start must be aligned to the size of the aperture");
  918. write_aux_reg(ARC_REG_IO_COH_AP0_BASE, ioc_base >> 12);
  919. write_aux_reg(ARC_REG_IO_COH_PARTIAL, ARC_IO_COH_PARTIAL_BIT);
  920. write_aux_reg(ARC_REG_IO_COH_ENABLE, ARC_IO_COH_ENABLE_BIT);
  921. /* Re-enable L1 dcache */
  922. __dc_enable();
  923. }
  924. /*
  925. * Cache related boot time checks/setups only needed on master CPU:
  926. * - Geometry checks (kernel build and hardware agree: e.g. L1_CACHE_BYTES)
  927. * Assume SMP only, so all cores will have same cache config. A check on
  928. * one core suffices for all
  929. * - IOC setup / dma callbacks only need to be done once
  930. */
  931. void __init arc_cache_init_master(void)
  932. {
  933. unsigned int __maybe_unused cpu = smp_processor_id();
  934. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  935. struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache;
  936. if (!ic->line_len)
  937. panic("cache support enabled but non-existent cache\n");
  938. if (ic->line_len != L1_CACHE_BYTES)
  939. panic("ICache line [%d] != kernel Config [%d]",
  940. ic->line_len, L1_CACHE_BYTES);
  941. /*
  942. * In MMU v4 (HS38x) the aliasing icache config uses IVIL/PTAG
  943. * pair to provide vaddr/paddr respectively, just as in MMU v3
  944. */
  945. if (is_isa_arcv2() && ic->alias)
  946. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  947. else
  948. _cache_line_loop_ic_fn = __cache_line_loop;
  949. }
  950. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  951. struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache;
  952. if (!dc->line_len)
  953. panic("cache support enabled but non-existent cache\n");
  954. if (dc->line_len != L1_CACHE_BYTES)
  955. panic("DCache line [%d] != kernel Config [%d]",
  956. dc->line_len, L1_CACHE_BYTES);
  957. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  958. if (is_isa_arcompact()) {
  959. int handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING);
  960. int num_colors = dc->sz_k/dc->assoc/TO_KB(PAGE_SIZE);
  961. if (dc->alias) {
  962. if (!handled)
  963. panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  964. if (CACHE_COLORS_NUM != num_colors)
  965. panic("CACHE_COLORS_NUM not optimized for config\n");
  966. } else if (!dc->alias && handled) {
  967. panic("Disable CONFIG_ARC_CACHE_VIPT_ALIASING\n");
  968. }
  969. }
  970. }
  971. /*
  972. * Check that SMP_CACHE_BYTES (and hence ARCH_DMA_MINALIGN) is larger
  973. * or equal to any cache line length.
  974. */
  975. BUILD_BUG_ON_MSG(L1_CACHE_BYTES > SMP_CACHE_BYTES,
  976. "SMP_CACHE_BYTES must be >= any cache line length");
  977. if (is_isa_arcv2() && (l2_line_sz > SMP_CACHE_BYTES))
  978. panic("L2 Cache line [%d] > kernel Config [%d]\n",
  979. l2_line_sz, SMP_CACHE_BYTES);
  980. /* Note that SLC disable not formally supported till HS 3.0 */
  981. if (is_isa_arcv2() && l2_line_sz && !slc_enable)
  982. arc_slc_disable();
  983. if (is_isa_arcv2() && ioc_exists)
  984. arc_ioc_setup();
  985. if (is_isa_arcv2() && l2_line_sz && slc_enable) {
  986. __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
  987. __dma_cache_inv = __dma_cache_inv_slc;
  988. __dma_cache_wback = __dma_cache_wback_slc;
  989. } else {
  990. __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
  991. __dma_cache_inv = __dma_cache_inv_l1;
  992. __dma_cache_wback = __dma_cache_wback_l1;
  993. }
  994. /*
  995. * In case of IOC (say IOC+SLC case), pointers above could still be set
  996. * but end up not being relevant as the first function in chain is not
  997. * called at all for devices using coherent DMA.
  998. * arch_sync_dma_for_cpu() -> dma_cache_*() -> __dma_cache_*()
  999. */
  1000. }
  1001. void __ref arc_cache_init(void)
  1002. {
  1003. unsigned int __maybe_unused cpu = smp_processor_id();
  1004. char str[256];
  1005. pr_info("%s", arc_cache_mumbojumbo(0, str, sizeof(str)));
  1006. if (!cpu)
  1007. arc_cache_init_master();
  1008. /*
  1009. * In PAE regime, TLB and cache maintenance ops take wider addresses
  1010. * And even if PAE is not enabled in kernel, the upper 32-bits still need
  1011. * to be zeroed to keep the ops sane.
  1012. * As an optimization for more common !PAE enabled case, zero them out
  1013. * once at init, rather than checking/setting to 0 for every runtime op
  1014. */
  1015. if (is_isa_arcv2() && pae40_exist_but_not_enab()) {
  1016. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE))
  1017. write_aux_reg(ARC_REG_IC_PTAG_HI, 0);
  1018. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE))
  1019. write_aux_reg(ARC_REG_DC_PTAG_HI, 0);
  1020. if (l2_line_sz) {
  1021. write_aux_reg(ARC_REG_SLC_RGN_END1, 0);
  1022. write_aux_reg(ARC_REG_SLC_RGN_START1, 0);
  1023. }
  1024. }
  1025. }