cache.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. static struct cpuinfo_arc_cache {
  28. unsigned int sz_k, line_len, colors;
  29. } ic_info, dc_info, slc_info;
  30. void (*_cache_line_loop_ic_fn)(phys_addr_t paddr, unsigned long vaddr,
  31. unsigned long sz, const int op, const int full_page);
  32. void (*__dma_cache_wback_inv)(phys_addr_t start, unsigned long sz);
  33. void (*__dma_cache_inv)(phys_addr_t start, unsigned long sz);
  34. void (*__dma_cache_wback)(phys_addr_t start, unsigned long sz);
  35. static int read_decode_cache_bcr_arcv2(int c, char *buf, int len)
  36. {
  37. struct cpuinfo_arc_cache *p_slc = &slc_info;
  38. struct bcr_identity ident;
  39. struct bcr_generic sbcr;
  40. struct bcr_clust_cfg cbcr;
  41. struct bcr_volatile vol;
  42. int n = 0;
  43. READ_BCR(ARC_REG_SLC_BCR, sbcr);
  44. if (sbcr.ver) {
  45. struct bcr_slc_cfg slc_cfg;
  46. READ_BCR(ARC_REG_SLC_CFG, slc_cfg);
  47. p_slc->sz_k = 128 << slc_cfg.sz;
  48. l2_line_sz = p_slc->line_len = (slc_cfg.lsz == 0) ? 128 : 64;
  49. n += scnprintf(buf + n, len - n,
  50. "SLC\t\t: %uK, %uB Line%s\n",
  51. p_slc->sz_k, p_slc->line_len, IS_USED_RUN(slc_enable));
  52. }
  53. READ_BCR(ARC_REG_CLUSTER_BCR, cbcr);
  54. if (cbcr.c) {
  55. ioc_exists = 1;
  56. /*
  57. * As for today we don't support both IOC and ZONE_HIGHMEM enabled
  58. * simultaneously. This happens because as of today IOC aperture covers
  59. * only ZONE_NORMAL (low mem) and any dma transactions outside this
  60. * region won't be HW coherent.
  61. * If we want to use both IOC and ZONE_HIGHMEM we can use
  62. * bounce_buffer to handle dma transactions to HIGHMEM.
  63. * Also it is possible to modify dma_direct cache ops or increase IOC
  64. * aperture size if we are planning to use HIGHMEM without PAE.
  65. */
  66. if (IS_ENABLED(CONFIG_HIGHMEM) || is_pae40_enabled())
  67. ioc_enable = 0;
  68. } else {
  69. ioc_enable = 0;
  70. }
  71. READ_BCR(AUX_IDENTITY, ident);
  72. /* HS 2.0 didn't have AUX_VOL */
  73. if (ident.family > 0x51) {
  74. READ_BCR(AUX_VOL, vol);
  75. perip_base = vol.start << 28;
  76. /* HS 3.0 has limit and strict-ordering fields */
  77. if (ident.family > 0x52)
  78. perip_end = (vol.limit << 28) - 1;
  79. }
  80. n += scnprintf(buf + n, len - n, "Peripherals\t: %#lx%s%s\n",
  81. perip_base,
  82. IS_AVAIL3(ioc_exists, ioc_enable, ", IO-Coherency (per-device) "));
  83. return n;
  84. }
  85. int arc_cache_mumbojumbo(int c, char *buf, int len)
  86. {
  87. struct cpuinfo_arc_cache *p_ic = &ic_info, *p_dc = &dc_info;
  88. struct bcr_cache ibcr, dbcr;
  89. int vipt, assoc;
  90. int n = 0;
  91. READ_BCR(ARC_REG_IC_BCR, ibcr);
  92. if (!ibcr.ver)
  93. goto dc_chk;
  94. if (is_isa_arcompact() && (ibcr.ver <= 3)) {
  95. BUG_ON(ibcr.config != 3);
  96. assoc = 2; /* Fixed to 2w set assoc */
  97. } else if (is_isa_arcv2() && (ibcr.ver >= 4)) {
  98. assoc = 1 << ibcr.config; /* 1,2,4,8 */
  99. }
  100. p_ic->line_len = 8 << ibcr.line_len;
  101. p_ic->sz_k = 1 << (ibcr.sz - 1);
  102. p_ic->colors = p_ic->sz_k/assoc/TO_KB(PAGE_SIZE);
  103. n += scnprintf(buf + n, len - n,
  104. "I-Cache\t\t: %uK, %dway/set, %uB Line, VIPT%s%s\n",
  105. p_ic->sz_k, assoc, p_ic->line_len,
  106. p_ic->colors > 1 ? " aliasing" : "",
  107. IS_USED_CFG(CONFIG_ARC_HAS_ICACHE));
  108. dc_chk:
  109. READ_BCR(ARC_REG_DC_BCR, dbcr);
  110. if (!dbcr.ver)
  111. goto slc_chk;
  112. if (is_isa_arcompact() && (dbcr.ver <= 3)) {
  113. BUG_ON(dbcr.config != 2);
  114. vipt = 1;
  115. assoc = 4; /* Fixed to 4w set assoc */
  116. p_dc->colors = p_dc->sz_k/assoc/TO_KB(PAGE_SIZE);
  117. } else if (is_isa_arcv2() && (dbcr.ver >= 4)) {
  118. vipt = 0;
  119. assoc = 1 << dbcr.config; /* 1,2,4,8 */
  120. p_dc->colors = 1; /* PIPT so can't VIPT alias */
  121. }
  122. p_dc->line_len = 16 << dbcr.line_len;
  123. p_dc->sz_k = 1 << (dbcr.sz - 1);
  124. n += scnprintf(buf + n, len - n,
  125. "D-Cache\t\t: %uK, %dway/set, %uB Line, %s%s\n",
  126. p_dc->sz_k, assoc, p_dc->line_len,
  127. vipt ? "VIPT" : "PIPT",
  128. IS_USED_CFG(CONFIG_ARC_HAS_DCACHE));
  129. slc_chk:
  130. if (is_isa_arcv2())
  131. n += read_decode_cache_bcr_arcv2(c, buf + n, len - n);
  132. return n;
  133. }
  134. /*
  135. * Line Operation on {I,D}-Cache
  136. */
  137. #define OP_INV 0x1
  138. #define OP_FLUSH 0x2
  139. #define OP_FLUSH_N_INV 0x3
  140. #define OP_INV_IC 0x4
  141. /*
  142. * Cache Flush programming model
  143. *
  144. * ARC700 MMUv3 I$ and D$ are both VIPT and can potentially alias.
  145. * Programming model requires both paddr and vaddr irrespecive of aliasing
  146. * considerations:
  147. * - vaddr in {I,D}C_IV?L
  148. * - paddr in {I,D}C_PTAG
  149. *
  150. * In HS38x (MMUv4), D$ is PIPT, I$ is VIPT and can still alias.
  151. * Programming model is different for aliasing vs. non-aliasing I$
  152. * - D$ / Non-aliasing I$: only paddr in {I,D}C_IV?L
  153. * - Aliasing I$: same as ARC700 above (so MMUv3 routine used for MMUv4 I$)
  154. *
  155. * - If PAE40 is enabled, independent of aliasing considerations, the higher
  156. * bits needs to be written into PTAG_HI
  157. */
  158. static inline
  159. void __cache_line_loop_v3(phys_addr_t paddr, unsigned long vaddr,
  160. unsigned long sz, const int op, const int full_page)
  161. {
  162. unsigned int aux_cmd, aux_tag;
  163. int num_lines;
  164. if (op == OP_INV_IC) {
  165. aux_cmd = ARC_REG_IC_IVIL;
  166. aux_tag = ARC_REG_IC_PTAG;
  167. } else {
  168. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  169. aux_tag = ARC_REG_DC_PTAG;
  170. }
  171. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  172. * and have @paddr - aligned to cache line and integral @num_lines.
  173. * This however can be avoided for page sized since:
  174. * -@paddr will be cache-line aligned already (being page aligned)
  175. * -@sz will be integral multiple of line size (being page sized).
  176. */
  177. if (!full_page) {
  178. sz += paddr & ~CACHE_LINE_MASK;
  179. paddr &= CACHE_LINE_MASK;
  180. vaddr &= CACHE_LINE_MASK;
  181. }
  182. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  183. /*
  184. * MMUv3, cache ops require paddr in PTAG reg
  185. * if V-P const for loop, PTAG can be written once outside loop
  186. */
  187. if (full_page)
  188. write_aux_reg(aux_tag, paddr);
  189. /*
  190. * This is technically for MMU v4, using the MMU v3 programming model
  191. * Special work for HS38 aliasing I-cache configuration with PAE40
  192. * - upper 8 bits of paddr need to be written into PTAG_HI
  193. * - (and needs to be written before the lower 32 bits)
  194. * Note that PTAG_HI is hoisted outside the line loop
  195. */
  196. if (is_pae40_enabled() && op == OP_INV_IC)
  197. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  198. while (num_lines-- > 0) {
  199. if (!full_page) {
  200. write_aux_reg(aux_tag, paddr);
  201. paddr += L1_CACHE_BYTES;
  202. }
  203. write_aux_reg(aux_cmd, vaddr);
  204. vaddr += L1_CACHE_BYTES;
  205. }
  206. }
  207. #ifndef USE_RGN_FLSH
  208. /*
  209. */
  210. static inline
  211. void __cache_line_loop_v4(phys_addr_t paddr, unsigned long vaddr,
  212. unsigned long sz, const int op, const int full_page)
  213. {
  214. unsigned int aux_cmd;
  215. int num_lines;
  216. if (op == OP_INV_IC) {
  217. aux_cmd = ARC_REG_IC_IVIL;
  218. } else {
  219. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  220. aux_cmd = op & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL;
  221. }
  222. /* Ensure we properly floor/ceil the non-line aligned/sized requests
  223. * and have @paddr - aligned to cache line and integral @num_lines.
  224. * This however can be avoided for page sized since:
  225. * -@paddr will be cache-line aligned already (being page aligned)
  226. * -@sz will be integral multiple of line size (being page sized).
  227. */
  228. if (!full_page) {
  229. sz += paddr & ~CACHE_LINE_MASK;
  230. paddr &= CACHE_LINE_MASK;
  231. }
  232. num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES);
  233. /*
  234. * For HS38 PAE40 configuration
  235. * - upper 8 bits of paddr need to be written into PTAG_HI
  236. * - (and needs to be written before the lower 32 bits)
  237. */
  238. if (is_pae40_enabled()) {
  239. if (op == OP_INV_IC)
  240. /*
  241. * Non aliasing I-cache in HS38,
  242. * aliasing I-cache handled in __cache_line_loop_v3()
  243. */
  244. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  245. else
  246. write_aux_reg(ARC_REG_DC_PTAG_HI, (u64)paddr >> 32);
  247. }
  248. while (num_lines-- > 0) {
  249. write_aux_reg(aux_cmd, paddr);
  250. paddr += L1_CACHE_BYTES;
  251. }
  252. }
  253. #else
  254. /*
  255. * optimized flush operation which takes a region as opposed to iterating per line
  256. */
  257. static inline
  258. void __cache_line_loop_v4(phys_addr_t paddr, unsigned long vaddr,
  259. unsigned long sz, const int op, const int full_page)
  260. {
  261. unsigned int s, e;
  262. /* Only for Non aliasing I-cache in HS38 */
  263. if (op == OP_INV_IC) {
  264. s = ARC_REG_IC_IVIR;
  265. e = ARC_REG_IC_ENDR;
  266. } else {
  267. s = ARC_REG_DC_STARTR;
  268. e = ARC_REG_DC_ENDR;
  269. }
  270. if (!full_page) {
  271. /* for any leading gap between @paddr and start of cache line */
  272. sz += paddr & ~CACHE_LINE_MASK;
  273. paddr &= CACHE_LINE_MASK;
  274. /*
  275. * account for any trailing gap to end of cache line
  276. * this is equivalent to DIV_ROUND_UP() in line ops above
  277. */
  278. sz += L1_CACHE_BYTES - 1;
  279. }
  280. if (is_pae40_enabled()) {
  281. /* TBD: check if crossing 4TB boundary */
  282. if (op == OP_INV_IC)
  283. write_aux_reg(ARC_REG_IC_PTAG_HI, (u64)paddr >> 32);
  284. else
  285. write_aux_reg(ARC_REG_DC_PTAG_HI, (u64)paddr >> 32);
  286. }
  287. /* ENDR needs to be set ahead of START */
  288. write_aux_reg(e, paddr + sz); /* ENDR is exclusive */
  289. write_aux_reg(s, paddr);
  290. /* caller waits on DC_CTRL.FS */
  291. }
  292. #endif
  293. #ifdef CONFIG_ARC_MMU_V3
  294. #define __cache_line_loop __cache_line_loop_v3
  295. #else
  296. #define __cache_line_loop __cache_line_loop_v4
  297. #endif
  298. #ifdef CONFIG_ARC_HAS_DCACHE
  299. /***************************************************************
  300. * Machine specific helpers for Entire D-Cache or Per Line ops
  301. */
  302. #ifndef USE_RGN_FLSH
  303. /*
  304. * this version avoids extra read/write of DC_CTRL for flush or invalid ops
  305. * in the non region flush regime (such as for ARCompact)
  306. */
  307. static inline void __before_dc_op(const int op)
  308. {
  309. if (op == OP_FLUSH_N_INV) {
  310. /* Dcache provides 2 cmd: FLUSH or INV
  311. * INV in turn has sub-modes: DISCARD or FLUSH-BEFORE
  312. * flush-n-inv is achieved by INV cmd but with IM=1
  313. * So toggle INV sub-mode depending on op request and default
  314. */
  315. const unsigned int ctl = ARC_REG_DC_CTRL;
  316. write_aux_reg(ctl, read_aux_reg(ctl) | DC_CTRL_INV_MODE_FLUSH);
  317. }
  318. }
  319. #else
  320. static inline void __before_dc_op(const int op)
  321. {
  322. const unsigned int ctl = ARC_REG_DC_CTRL;
  323. unsigned int val = read_aux_reg(ctl);
  324. if (op == OP_FLUSH_N_INV) {
  325. val |= DC_CTRL_INV_MODE_FLUSH;
  326. }
  327. if (op != OP_INV_IC) {
  328. /*
  329. * Flush / Invalidate is provided by DC_CTRL.RNG_OP 0 or 1
  330. * combined Flush-n-invalidate uses DC_CTRL.IM = 1 set above
  331. */
  332. val &= ~DC_CTRL_RGN_OP_MSK;
  333. if (op & OP_INV)
  334. val |= DC_CTRL_RGN_OP_INV;
  335. }
  336. write_aux_reg(ctl, val);
  337. }
  338. #endif
  339. static inline void __after_dc_op(const int op)
  340. {
  341. if (op & OP_FLUSH) {
  342. const unsigned int ctl = ARC_REG_DC_CTRL;
  343. unsigned int reg;
  344. /* flush / flush-n-inv both wait */
  345. while ((reg = read_aux_reg(ctl)) & DC_CTRL_FLUSH_STATUS)
  346. ;
  347. /* Switch back to default Invalidate mode */
  348. if (op == OP_FLUSH_N_INV)
  349. write_aux_reg(ctl, reg & ~DC_CTRL_INV_MODE_FLUSH);
  350. }
  351. }
  352. /*
  353. * Operation on Entire D-Cache
  354. * @op = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV}
  355. * Note that constant propagation ensures all the checks are gone
  356. * in generated code
  357. */
  358. static inline void __dc_entire_op(const int op)
  359. {
  360. int aux;
  361. __before_dc_op(op);
  362. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  363. aux = ARC_REG_DC_IVDC;
  364. else
  365. aux = ARC_REG_DC_FLSH;
  366. write_aux_reg(aux, 0x1);
  367. __after_dc_op(op);
  368. }
  369. static inline void __dc_disable(void)
  370. {
  371. const int r = ARC_REG_DC_CTRL;
  372. __dc_entire_op(OP_FLUSH_N_INV);
  373. write_aux_reg(r, read_aux_reg(r) | DC_CTRL_DIS);
  374. }
  375. static void __dc_enable(void)
  376. {
  377. const int r = ARC_REG_DC_CTRL;
  378. write_aux_reg(r, read_aux_reg(r) & ~DC_CTRL_DIS);
  379. }
  380. /* For kernel mappings cache operation: index is same as paddr */
  381. #define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op)
  382. /*
  383. * D-Cache Line ops: Per Line INV (discard or wback+discard) or FLUSH (wback)
  384. */
  385. static inline void __dc_line_op(phys_addr_t paddr, unsigned long vaddr,
  386. unsigned long sz, const int op)
  387. {
  388. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  389. unsigned long flags;
  390. local_irq_save(flags);
  391. __before_dc_op(op);
  392. __cache_line_loop(paddr, vaddr, sz, op, full_page);
  393. __after_dc_op(op);
  394. local_irq_restore(flags);
  395. }
  396. #else
  397. #define __dc_entire_op(op)
  398. #define __dc_disable()
  399. #define __dc_enable()
  400. #define __dc_line_op(paddr, vaddr, sz, op)
  401. #define __dc_line_op_k(paddr, sz, op)
  402. #endif /* CONFIG_ARC_HAS_DCACHE */
  403. #ifdef CONFIG_ARC_HAS_ICACHE
  404. static inline void __ic_entire_inv(void)
  405. {
  406. write_aux_reg(ARC_REG_IC_IVIC, 1);
  407. read_aux_reg(ARC_REG_IC_CTRL); /* blocks */
  408. }
  409. static inline void
  410. __ic_line_inv_vaddr_local(phys_addr_t paddr, unsigned long vaddr,
  411. unsigned long sz)
  412. {
  413. const int full_page = __builtin_constant_p(sz) && sz == PAGE_SIZE;
  414. unsigned long flags;
  415. local_irq_save(flags);
  416. (*_cache_line_loop_ic_fn)(paddr, vaddr, sz, OP_INV_IC, full_page);
  417. local_irq_restore(flags);
  418. }
  419. #ifndef CONFIG_SMP
  420. #define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s)
  421. #else
  422. struct ic_inv_args {
  423. phys_addr_t paddr, vaddr;
  424. int sz;
  425. };
  426. static void __ic_line_inv_vaddr_helper(void *info)
  427. {
  428. struct ic_inv_args *ic_inv = info;
  429. __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz);
  430. }
  431. static void __ic_line_inv_vaddr(phys_addr_t paddr, unsigned long vaddr,
  432. unsigned long sz)
  433. {
  434. struct ic_inv_args ic_inv = {
  435. .paddr = paddr,
  436. .vaddr = vaddr,
  437. .sz = sz
  438. };
  439. on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1);
  440. }
  441. #endif /* CONFIG_SMP */
  442. #else /* !CONFIG_ARC_HAS_ICACHE */
  443. #define __ic_entire_inv()
  444. #define __ic_line_inv_vaddr(pstart, vstart, sz)
  445. #endif /* CONFIG_ARC_HAS_ICACHE */
  446. static noinline void slc_op_rgn(phys_addr_t paddr, unsigned long sz, const int op)
  447. {
  448. #ifdef CONFIG_ISA_ARCV2
  449. /*
  450. * SLC is shared between all cores and concurrent aux operations from
  451. * multiple cores need to be serialized using a spinlock
  452. * A concurrent operation can be silently ignored and/or the old/new
  453. * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
  454. * below)
  455. */
  456. static DEFINE_SPINLOCK(lock);
  457. unsigned long flags;
  458. unsigned int ctrl;
  459. phys_addr_t end;
  460. spin_lock_irqsave(&lock, flags);
  461. /*
  462. * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
  463. * - b'000 (default) is Flush,
  464. * - b'001 is Invalidate if CTRL.IM == 0
  465. * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
  466. */
  467. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  468. /* Don't rely on default value of IM bit */
  469. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  470. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  471. else
  472. ctrl |= SLC_CTRL_IM;
  473. if (op & OP_INV)
  474. ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
  475. else
  476. ctrl &= ~SLC_CTRL_RGN_OP_INV;
  477. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  478. /*
  479. * Lower bits are ignored, no need to clip
  480. * END needs to be setup before START (latter triggers the operation)
  481. * END can't be same as START, so add (l2_line_sz - 1) to sz
  482. */
  483. end = paddr + sz + l2_line_sz - 1;
  484. if (is_pae40_enabled())
  485. write_aux_reg(ARC_REG_SLC_RGN_END1, upper_32_bits(end));
  486. write_aux_reg(ARC_REG_SLC_RGN_END, lower_32_bits(end));
  487. if (is_pae40_enabled())
  488. write_aux_reg(ARC_REG_SLC_RGN_START1, upper_32_bits(paddr));
  489. write_aux_reg(ARC_REG_SLC_RGN_START, lower_32_bits(paddr));
  490. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  491. read_aux_reg(ARC_REG_SLC_CTRL);
  492. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  493. spin_unlock_irqrestore(&lock, flags);
  494. #endif
  495. }
  496. static __maybe_unused noinline void slc_op_line(phys_addr_t paddr, unsigned long sz, const int op)
  497. {
  498. #ifdef CONFIG_ISA_ARCV2
  499. /*
  500. * SLC is shared between all cores and concurrent aux operations from
  501. * multiple cores need to be serialized using a spinlock
  502. * A concurrent operation can be silently ignored and/or the old/new
  503. * operation can remain incomplete forever (lockup in SLC_CTRL_BUSY loop
  504. * below)
  505. */
  506. static DEFINE_SPINLOCK(lock);
  507. const unsigned long SLC_LINE_MASK = ~(l2_line_sz - 1);
  508. unsigned int ctrl, cmd;
  509. unsigned long flags;
  510. int num_lines;
  511. spin_lock_irqsave(&lock, flags);
  512. ctrl = read_aux_reg(ARC_REG_SLC_CTRL);
  513. /* Don't rely on default value of IM bit */
  514. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  515. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  516. else
  517. ctrl |= SLC_CTRL_IM;
  518. write_aux_reg(ARC_REG_SLC_CTRL, ctrl);
  519. cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL;
  520. sz += paddr & ~SLC_LINE_MASK;
  521. paddr &= SLC_LINE_MASK;
  522. num_lines = DIV_ROUND_UP(sz, l2_line_sz);
  523. while (num_lines-- > 0) {
  524. write_aux_reg(cmd, paddr);
  525. paddr += l2_line_sz;
  526. }
  527. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  528. read_aux_reg(ARC_REG_SLC_CTRL);
  529. while (read_aux_reg(ARC_REG_SLC_CTRL) & SLC_CTRL_BUSY);
  530. spin_unlock_irqrestore(&lock, flags);
  531. #endif
  532. }
  533. #define slc_op(paddr, sz, op) slc_op_rgn(paddr, sz, op)
  534. noinline static void slc_entire_op(const int op)
  535. {
  536. unsigned int ctrl, r = ARC_REG_SLC_CTRL;
  537. ctrl = read_aux_reg(r);
  538. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  539. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  540. else
  541. ctrl |= SLC_CTRL_IM;
  542. write_aux_reg(r, ctrl);
  543. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  544. write_aux_reg(ARC_REG_SLC_INVALIDATE, 0x1);
  545. else
  546. write_aux_reg(ARC_REG_SLC_FLUSH, 0x1);
  547. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  548. read_aux_reg(r);
  549. /* Important to wait for flush to complete */
  550. while (read_aux_reg(r) & SLC_CTRL_BUSY);
  551. }
  552. static inline void arc_slc_disable(void)
  553. {
  554. const int r = ARC_REG_SLC_CTRL;
  555. slc_entire_op(OP_FLUSH_N_INV);
  556. write_aux_reg(r, read_aux_reg(r) | SLC_CTRL_DIS);
  557. }
  558. static inline void arc_slc_enable(void)
  559. {
  560. const int r = ARC_REG_SLC_CTRL;
  561. write_aux_reg(r, read_aux_reg(r) & ~SLC_CTRL_DIS);
  562. }
  563. /***********************************************************
  564. * Exported APIs
  565. */
  566. void flush_dcache_folio(struct folio *folio)
  567. {
  568. clear_bit(PG_dc_clean, &folio->flags);
  569. return;
  570. }
  571. EXPORT_SYMBOL(flush_dcache_folio);
  572. void flush_dcache_page(struct page *page)
  573. {
  574. return flush_dcache_folio(page_folio(page));
  575. }
  576. EXPORT_SYMBOL(flush_dcache_page);
  577. /*
  578. * DMA ops for systems with L1 cache only
  579. * Make memory coherent with L1 cache by flushing/invalidating L1 lines
  580. */
  581. static void __dma_cache_wback_inv_l1(phys_addr_t start, unsigned long sz)
  582. {
  583. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  584. }
  585. static void __dma_cache_inv_l1(phys_addr_t start, unsigned long sz)
  586. {
  587. __dc_line_op_k(start, sz, OP_INV);
  588. }
  589. static void __dma_cache_wback_l1(phys_addr_t start, unsigned long sz)
  590. {
  591. __dc_line_op_k(start, sz, OP_FLUSH);
  592. }
  593. /*
  594. * DMA ops for systems with both L1 and L2 caches, but without IOC
  595. * Both L1 and L2 lines need to be explicitly flushed/invalidated
  596. */
  597. static void __dma_cache_wback_inv_slc(phys_addr_t start, unsigned long sz)
  598. {
  599. __dc_line_op_k(start, sz, OP_FLUSH_N_INV);
  600. slc_op(start, sz, OP_FLUSH_N_INV);
  601. }
  602. static void __dma_cache_inv_slc(phys_addr_t start, unsigned long sz)
  603. {
  604. __dc_line_op_k(start, sz, OP_INV);
  605. slc_op(start, sz, OP_INV);
  606. }
  607. static void __dma_cache_wback_slc(phys_addr_t start, unsigned long sz)
  608. {
  609. __dc_line_op_k(start, sz, OP_FLUSH);
  610. slc_op(start, sz, OP_FLUSH);
  611. }
  612. /*
  613. * Exported DMA API
  614. */
  615. void dma_cache_wback_inv(phys_addr_t start, unsigned long sz)
  616. {
  617. __dma_cache_wback_inv(start, sz);
  618. }
  619. EXPORT_SYMBOL(dma_cache_wback_inv);
  620. void dma_cache_inv(phys_addr_t start, unsigned long sz)
  621. {
  622. __dma_cache_inv(start, sz);
  623. }
  624. EXPORT_SYMBOL(dma_cache_inv);
  625. void dma_cache_wback(phys_addr_t start, unsigned long sz)
  626. {
  627. __dma_cache_wback(start, sz);
  628. }
  629. EXPORT_SYMBOL(dma_cache_wback);
  630. /*
  631. * This is API for making I/D Caches consistent when modifying
  632. * kernel code (loadable modules, kprobes, kgdb...)
  633. * This is called on insmod, with kernel virtual address for CODE of
  634. * the module. ARC cache maintenance ops require PHY address thus we
  635. * need to convert vmalloc addr to PHY addr
  636. */
  637. void flush_icache_range(unsigned long kstart, unsigned long kend)
  638. {
  639. unsigned int tot_sz;
  640. WARN(kstart < TASK_SIZE, "%s() can't handle user vaddr", __func__);
  641. /* Shortcut for bigger flush ranges.
  642. * Here we don't care if this was kernel virtual or phy addr
  643. */
  644. tot_sz = kend - kstart;
  645. if (tot_sz > PAGE_SIZE) {
  646. flush_cache_all();
  647. return;
  648. }
  649. /* Case: Kernel Phy addr (0x8000_0000 onwards) */
  650. if (likely(kstart > PAGE_OFFSET)) {
  651. /*
  652. * The 2nd arg despite being paddr will be used to index icache
  653. * This is OK since no alternate virtual mappings will exist
  654. * given the callers for this case: kprobe/kgdb in built-in
  655. * kernel code only.
  656. */
  657. __sync_icache_dcache(kstart, kstart, kend - kstart);
  658. return;
  659. }
  660. /*
  661. * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff)
  662. * (1) ARC Cache Maintenance ops only take Phy addr, hence special
  663. * handling of kernel vaddr.
  664. *
  665. * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already),
  666. * it still needs to handle a 2 page scenario, where the range
  667. * straddles across 2 virtual pages and hence need for loop
  668. */
  669. while (tot_sz > 0) {
  670. unsigned int off, sz;
  671. unsigned long phy, pfn;
  672. off = kstart % PAGE_SIZE;
  673. pfn = vmalloc_to_pfn((void *)kstart);
  674. phy = (pfn << PAGE_SHIFT) + off;
  675. sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off);
  676. __sync_icache_dcache(phy, kstart, sz);
  677. kstart += sz;
  678. tot_sz -= sz;
  679. }
  680. }
  681. EXPORT_SYMBOL(flush_icache_range);
  682. /*
  683. * General purpose helper to make I and D cache lines consistent.
  684. * @paddr is phy addr of region
  685. * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc)
  686. * However in one instance, when called by kprobe (for a breakpt in
  687. * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will
  688. * use a paddr to index the cache (despite VIPT). This is fine since a
  689. * builtin kernel page will not have any virtual mappings.
  690. * kprobe on loadable module will be kernel vaddr.
  691. */
  692. void __sync_icache_dcache(phys_addr_t paddr, unsigned long vaddr, int len)
  693. {
  694. __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV);
  695. __ic_line_inv_vaddr(paddr, vaddr, len);
  696. }
  697. /* wrapper to compile time eliminate alignment checks in flush loop */
  698. void __inv_icache_pages(phys_addr_t paddr, unsigned long vaddr, unsigned nr)
  699. {
  700. __ic_line_inv_vaddr(paddr, vaddr, nr * PAGE_SIZE);
  701. }
  702. /*
  703. * wrapper to clearout kernel or userspace mappings of a page
  704. * For kernel mappings @vaddr == @paddr
  705. */
  706. void __flush_dcache_pages(phys_addr_t paddr, unsigned long vaddr, unsigned nr)
  707. {
  708. __dc_line_op(paddr, vaddr & PAGE_MASK, nr * PAGE_SIZE, OP_FLUSH_N_INV);
  709. }
  710. noinline void flush_cache_all(void)
  711. {
  712. unsigned long flags;
  713. local_irq_save(flags);
  714. __ic_entire_inv();
  715. __dc_entire_op(OP_FLUSH_N_INV);
  716. local_irq_restore(flags);
  717. }
  718. void copy_user_highpage(struct page *to, struct page *from,
  719. unsigned long u_vaddr, struct vm_area_struct *vma)
  720. {
  721. struct folio *src = page_folio(from);
  722. struct folio *dst = page_folio(to);
  723. void *kfrom = kmap_atomic(from);
  724. void *kto = kmap_atomic(to);
  725. copy_page(kto, kfrom);
  726. clear_bit(PG_dc_clean, &dst->flags);
  727. clear_bit(PG_dc_clean, &src->flags);
  728. kunmap_atomic(kto);
  729. kunmap_atomic(kfrom);
  730. }
  731. void clear_user_page(void *to, unsigned long u_vaddr, struct page *page)
  732. {
  733. struct folio *folio = page_folio(page);
  734. clear_page(to);
  735. clear_bit(PG_dc_clean, &folio->flags);
  736. }
  737. EXPORT_SYMBOL(clear_user_page);
  738. /**********************************************************************
  739. * Explicit Cache flush request from user space via syscall
  740. * Needed for JITs which generate code on the fly
  741. */
  742. SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags)
  743. {
  744. /* TBD: optimize this */
  745. flush_cache_all();
  746. return 0;
  747. }
  748. /*
  749. * IO-Coherency (IOC) setup rules:
  750. *
  751. * 1. Needs to be at system level, so only once by Master core
  752. * Non-Masters need not be accessing caches at that time
  753. * - They are either HALT_ON_RESET and kick started much later or
  754. * - if run on reset, need to ensure that arc_platform_smp_wait_to_boot()
  755. * doesn't perturb caches or coherency unit
  756. *
  757. * 2. caches (L1 and SLC) need to be purged (flush+inv) before setting up IOC,
  758. * otherwise any straggler data might behave strangely post IOC enabling
  759. *
  760. * 3. All Caches need to be disabled when setting up IOC to elide any in-flight
  761. * Coherency transactions
  762. */
  763. static noinline void __init arc_ioc_setup(void)
  764. {
  765. unsigned int ioc_base, mem_sz;
  766. /*
  767. * If IOC was already enabled (due to bootloader) it technically needs to
  768. * be reconfigured with aperture base,size corresponding to Linux memory map
  769. * which will certainly be different than uboot's. But disabling and
  770. * reenabling IOC when DMA might be potentially active is tricky business.
  771. * To avoid random memory issues later, just panic here and ask user to
  772. * upgrade bootloader to one which doesn't enable IOC
  773. */
  774. if (read_aux_reg(ARC_REG_IO_COH_ENABLE) & ARC_IO_COH_ENABLE_BIT)
  775. panic("IOC already enabled, please upgrade bootloader!\n");
  776. if (!ioc_enable)
  777. return;
  778. /* Flush + invalidate + disable L1 dcache */
  779. __dc_disable();
  780. /* Flush + invalidate SLC */
  781. if (read_aux_reg(ARC_REG_SLC_BCR))
  782. slc_entire_op(OP_FLUSH_N_INV);
  783. /*
  784. * currently IOC Aperture covers entire DDR
  785. * TBD: fix for PGU + 1GB of low mem
  786. * TBD: fix for PAE
  787. */
  788. mem_sz = arc_get_mem_sz();
  789. if (!is_power_of_2(mem_sz) || mem_sz < 4096)
  790. panic("IOC Aperture size must be power of 2 larger than 4KB");
  791. /*
  792. * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
  793. * so setting 0x11 implies 512MB, 0x12 implies 1GB...
  794. */
  795. write_aux_reg(ARC_REG_IO_COH_AP0_SIZE, order_base_2(mem_sz >> 10) - 2);
  796. /* for now assume kernel base is start of IOC aperture */
  797. ioc_base = CONFIG_LINUX_RAM_BASE;
  798. if (ioc_base % mem_sz != 0)
  799. panic("IOC Aperture start must be aligned to the size of the aperture");
  800. write_aux_reg(ARC_REG_IO_COH_AP0_BASE, ioc_base >> 12);
  801. write_aux_reg(ARC_REG_IO_COH_PARTIAL, ARC_IO_COH_PARTIAL_BIT);
  802. write_aux_reg(ARC_REG_IO_COH_ENABLE, ARC_IO_COH_ENABLE_BIT);
  803. /* Re-enable L1 dcache */
  804. __dc_enable();
  805. }
  806. /*
  807. * Cache related boot time checks/setups only needed on master CPU:
  808. * - Geometry checks (kernel build and hardware agree: e.g. L1_CACHE_BYTES)
  809. * Assume SMP only, so all cores will have same cache config. A check on
  810. * one core suffices for all
  811. * - IOC setup / dma callbacks only need to be done once
  812. */
  813. static noinline void __init arc_cache_init_master(void)
  814. {
  815. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) {
  816. struct cpuinfo_arc_cache *ic = &ic_info;
  817. if (!ic->line_len)
  818. panic("cache support enabled but non-existent cache\n");
  819. if (ic->line_len != L1_CACHE_BYTES)
  820. panic("ICache line [%d] != kernel Config [%d]",
  821. ic->line_len, L1_CACHE_BYTES);
  822. /*
  823. * In MMU v4 (HS38x) the aliasing icache config uses IVIL/PTAG
  824. * pair to provide vaddr/paddr respectively, just as in MMU v3
  825. */
  826. if (is_isa_arcv2() && ic->colors > 1)
  827. _cache_line_loop_ic_fn = __cache_line_loop_v3;
  828. else
  829. _cache_line_loop_ic_fn = __cache_line_loop;
  830. }
  831. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) {
  832. struct cpuinfo_arc_cache *dc = &dc_info;
  833. if (!dc->line_len)
  834. panic("cache support enabled but non-existent cache\n");
  835. if (dc->line_len != L1_CACHE_BYTES)
  836. panic("DCache line [%d] != kernel Config [%d]",
  837. dc->line_len, L1_CACHE_BYTES);
  838. /* check for D-Cache aliasing on ARCompact: ARCv2 has PIPT */
  839. if (is_isa_arcompact() && dc->colors > 1) {
  840. panic("Aliasing VIPT cache not supported\n");
  841. }
  842. }
  843. /*
  844. * Check that SMP_CACHE_BYTES (and hence ARCH_DMA_MINALIGN) is larger
  845. * or equal to any cache line length.
  846. */
  847. BUILD_BUG_ON_MSG(L1_CACHE_BYTES > SMP_CACHE_BYTES,
  848. "SMP_CACHE_BYTES must be >= any cache line length");
  849. if (is_isa_arcv2() && (l2_line_sz > SMP_CACHE_BYTES))
  850. panic("L2 Cache line [%d] > kernel Config [%d]\n",
  851. l2_line_sz, SMP_CACHE_BYTES);
  852. /* Note that SLC disable not formally supported till HS 3.0 */
  853. if (is_isa_arcv2() && l2_line_sz && !slc_enable)
  854. arc_slc_disable();
  855. if (is_isa_arcv2() && ioc_exists)
  856. arc_ioc_setup();
  857. if (is_isa_arcv2() && l2_line_sz && slc_enable) {
  858. __dma_cache_wback_inv = __dma_cache_wback_inv_slc;
  859. __dma_cache_inv = __dma_cache_inv_slc;
  860. __dma_cache_wback = __dma_cache_wback_slc;
  861. } else {
  862. __dma_cache_wback_inv = __dma_cache_wback_inv_l1;
  863. __dma_cache_inv = __dma_cache_inv_l1;
  864. __dma_cache_wback = __dma_cache_wback_l1;
  865. }
  866. /*
  867. * In case of IOC (say IOC+SLC case), pointers above could still be set
  868. * but end up not being relevant as the first function in chain is not
  869. * called at all for devices using coherent DMA.
  870. * arch_sync_dma_for_cpu() -> dma_cache_*() -> __dma_cache_*()
  871. */
  872. }
  873. void __ref arc_cache_init(void)
  874. {
  875. unsigned int __maybe_unused cpu = smp_processor_id();
  876. if (!cpu)
  877. arc_cache_init_master();
  878. /*
  879. * In PAE regime, TLB and cache maintenance ops take wider addresses
  880. * And even if PAE is not enabled in kernel, the upper 32-bits still need
  881. * to be zeroed to keep the ops sane.
  882. * As an optimization for more common !PAE enabled case, zero them out
  883. * once at init, rather than checking/setting to 0 for every runtime op
  884. */
  885. if (is_isa_arcv2() && pae40_exist_but_not_enab()) {
  886. if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE))
  887. write_aux_reg(ARC_REG_IC_PTAG_HI, 0);
  888. if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE))
  889. write_aux_reg(ARC_REG_DC_PTAG_HI, 0);
  890. if (l2_line_sz) {
  891. write_aux_reg(ARC_REG_SLC_RGN_END1, 0);
  892. write_aux_reg(ARC_REG_SLC_RGN_START1, 0);
  893. }
  894. }
  895. }