cache.c 35 KB

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