cache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <config.h>
  7. #include <common.h>
  8. #include <linux/compiler.h>
  9. #include <linux/kernel.h>
  10. #include <linux/log2.h>
  11. #include <asm/arcregs.h>
  12. #include <asm/cache.h>
  13. /*
  14. * [ NOTE 1 ]:
  15. * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
  16. * operation may result in unexpected behavior and data loss even if we flush
  17. * data cache right before invalidation. That may happens if we store any context
  18. * on stack (like we store BLINK register on stack before function call).
  19. * BLINK register is the register where return address is automatically saved
  20. * when we do function call with instructions like 'bl'.
  21. *
  22. * There is the real example:
  23. * We may hang in the next code as we store any BLINK register on stack in
  24. * invalidate_dcache_all() function.
  25. *
  26. * void flush_dcache_all() {
  27. * __dc_entire_op(OP_FLUSH);
  28. * // Other code //
  29. * }
  30. *
  31. * void invalidate_dcache_all() {
  32. * __dc_entire_op(OP_INV);
  33. * // Other code //
  34. * }
  35. *
  36. * void foo(void) {
  37. * flush_dcache_all();
  38. * invalidate_dcache_all();
  39. * }
  40. *
  41. * Now let's see what really happens during that code execution:
  42. *
  43. * foo()
  44. * |->> call flush_dcache_all
  45. * [return address is saved to BLINK register]
  46. * [push BLINK] (save to stack) ![point 1]
  47. * |->> call __dc_entire_op(OP_FLUSH)
  48. * [return address is saved to BLINK register]
  49. * [flush L1 D$]
  50. * return [jump to BLINK]
  51. * <<------
  52. * [other flush_dcache_all code]
  53. * [pop BLINK] (get from stack)
  54. * return [jump to BLINK]
  55. * <<------
  56. * |->> call invalidate_dcache_all
  57. * [return address is saved to BLINK register]
  58. * [push BLINK] (save to stack) ![point 2]
  59. * |->> call __dc_entire_op(OP_FLUSH)
  60. * [return address is saved to BLINK register]
  61. * [invalidate L1 D$] ![point 3]
  62. * // Oops!!!
  63. * // We lose return address from invalidate_dcache_all function:
  64. * // we save it to stack and invalidate L1 D$ after that!
  65. * return [jump to BLINK]
  66. * <<------
  67. * [other invalidate_dcache_all code]
  68. * [pop BLINK] (get from stack)
  69. * // we don't have this data in L1 dcache as we invalidated it in [point 3]
  70. * // so we get it from next memory level (for example DDR memory)
  71. * // but in the memory we have value which we save in [point 1], which
  72. * // is return address from flush_dcache_all function (instead of
  73. * // address from current invalidate_dcache_all function which we
  74. * // saved in [point 2] !)
  75. * return [jump to BLINK]
  76. * <<------
  77. * // As BLINK points to invalidate_dcache_all, we call it again and
  78. * // loop forever.
  79. *
  80. * Fortunately we may fix that by using flush & invalidation of D$ with a single
  81. * one instruction (instead of flush and invalidation instructions pair) and
  82. * enabling force function inline with '__attribute__((always_inline))' gcc
  83. * attribute to avoid any function call (and BLINK store) between cache flush
  84. * and disable.
  85. */
  86. /* Bit values in IC_CTRL */
  87. #define IC_CTRL_CACHE_DISABLE BIT(0)
  88. /* Bit values in DC_CTRL */
  89. #define DC_CTRL_CACHE_DISABLE BIT(0)
  90. #define DC_CTRL_INV_MODE_FLUSH BIT(6)
  91. #define DC_CTRL_FLUSH_STATUS BIT(8)
  92. #define CACHE_VER_NUM_MASK 0xF
  93. #define OP_INV BIT(0)
  94. #define OP_FLUSH BIT(1)
  95. #define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
  96. /* Bit val in SLC_CONTROL */
  97. #define SLC_CTRL_DIS 0x001
  98. #define SLC_CTRL_IM 0x040
  99. #define SLC_CTRL_BUSY 0x100
  100. #define SLC_CTRL_RGN_OP_INV 0x200
  101. /*
  102. * By default that variable will fall into .bss section.
  103. * But .bss section is not relocated and so it will be initilized before
  104. * relocation but will be used after being zeroed.
  105. */
  106. int l1_line_sz __section(".data");
  107. bool dcache_exists __section(".data") = false;
  108. bool icache_exists __section(".data") = false;
  109. #define CACHE_LINE_MASK (~(l1_line_sz - 1))
  110. #ifdef CONFIG_ISA_ARCV2
  111. int slc_line_sz __section(".data");
  112. bool slc_exists __section(".data") = false;
  113. bool ioc_exists __section(".data") = false;
  114. bool pae_exists __section(".data") = false;
  115. /* To force enable IOC set ioc_enable to 'true' */
  116. bool ioc_enable __section(".data") = false;
  117. void read_decode_mmu_bcr(void)
  118. {
  119. /* TODO: should we compare mmu version from BCR and from CONFIG? */
  120. #if (CONFIG_ARC_MMU_VER >= 4)
  121. u32 tmp;
  122. tmp = read_aux_reg(ARC_AUX_MMU_BCR);
  123. struct bcr_mmu_4 {
  124. #ifdef CONFIG_CPU_BIG_ENDIAN
  125. unsigned int ver:8, sasid:1, sz1:4, sz0:4, res:2, pae:1,
  126. n_ways:2, n_entry:2, n_super:2, u_itlb:3, u_dtlb:3;
  127. #else
  128. /* DTLB ITLB JES JE JA */
  129. unsigned int u_dtlb:3, u_itlb:3, n_super:2, n_entry:2, n_ways:2,
  130. pae:1, res:2, sz0:4, sz1:4, sasid:1, ver:8;
  131. #endif /* CONFIG_CPU_BIG_ENDIAN */
  132. } *mmu4;
  133. mmu4 = (struct bcr_mmu_4 *)&tmp;
  134. pae_exists = !!mmu4->pae;
  135. #endif /* (CONFIG_ARC_MMU_VER >= 4) */
  136. }
  137. static void __slc_entire_op(const int op)
  138. {
  139. unsigned int ctrl;
  140. ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
  141. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  142. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  143. else
  144. ctrl |= SLC_CTRL_IM;
  145. write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
  146. if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  147. write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
  148. else
  149. write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
  150. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  151. read_aux_reg(ARC_AUX_SLC_CTRL);
  152. /* Important to wait for flush to complete */
  153. while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
  154. }
  155. static void slc_upper_region_init(void)
  156. {
  157. /*
  158. * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
  159. * as we don't use PAE40.
  160. */
  161. write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
  162. write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
  163. }
  164. static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
  165. {
  166. unsigned int ctrl;
  167. unsigned long end;
  168. /*
  169. * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
  170. * - b'000 (default) is Flush,
  171. * - b'001 is Invalidate if CTRL.IM == 0
  172. * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
  173. */
  174. ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
  175. /* Don't rely on default value of IM bit */
  176. if (!(op & OP_FLUSH)) /* i.e. OP_INV */
  177. ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
  178. else
  179. ctrl |= SLC_CTRL_IM;
  180. if (op & OP_INV)
  181. ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
  182. else
  183. ctrl &= ~SLC_CTRL_RGN_OP_INV;
  184. write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
  185. /*
  186. * Lower bits are ignored, no need to clip
  187. * END needs to be setup before START (latter triggers the operation)
  188. * END can't be same as START, so add (l2_line_sz - 1) to sz
  189. */
  190. end = paddr + sz + slc_line_sz - 1;
  191. /*
  192. * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
  193. * are always == 0 as we don't use PAE40, so we only setup lower ones
  194. * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
  195. */
  196. write_aux_reg(ARC_AUX_SLC_RGN_END, end);
  197. write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
  198. /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
  199. read_aux_reg(ARC_AUX_SLC_CTRL);
  200. while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
  201. }
  202. static void arc_ioc_setup(void)
  203. {
  204. /* IOC Aperture start is equal to DDR start */
  205. unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
  206. /* IOC Aperture size is equal to DDR size */
  207. long ap_size = CONFIG_SYS_SDRAM_SIZE;
  208. flush_n_invalidate_dcache_all();
  209. if (!is_power_of_2(ap_size) || ap_size < 4096)
  210. panic("IOC Aperture size must be power of 2 and bigger 4Kib");
  211. /*
  212. * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
  213. * so setting 0x11 implies 512M, 0x12 implies 1G...
  214. */
  215. write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
  216. order_base_2(ap_size / 1024) - 2);
  217. /* IOC Aperture start must be aligned to the size of the aperture */
  218. if (ap_base % ap_size != 0)
  219. panic("IOC Aperture start must be aligned to the size of the aperture");
  220. write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
  221. write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
  222. write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
  223. }
  224. #endif /* CONFIG_ISA_ARCV2 */
  225. #ifdef CONFIG_ISA_ARCV2
  226. static void read_decode_cache_bcr_arcv2(void)
  227. {
  228. union {
  229. struct {
  230. #ifdef CONFIG_CPU_BIG_ENDIAN
  231. unsigned int pad:24, way:2, lsz:2, sz:4;
  232. #else
  233. unsigned int sz:4, lsz:2, way:2, pad:24;
  234. #endif
  235. } fields;
  236. unsigned int word;
  237. } slc_cfg;
  238. union {
  239. struct {
  240. #ifdef CONFIG_CPU_BIG_ENDIAN
  241. unsigned int pad:24, ver:8;
  242. #else
  243. unsigned int ver:8, pad:24;
  244. #endif
  245. } fields;
  246. unsigned int word;
  247. } sbcr;
  248. sbcr.word = read_aux_reg(ARC_BCR_SLC);
  249. if (sbcr.fields.ver) {
  250. slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
  251. slc_exists = true;
  252. slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
  253. }
  254. union {
  255. struct bcr_clust_cfg {
  256. #ifdef CONFIG_CPU_BIG_ENDIAN
  257. unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8;
  258. #else
  259. unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7;
  260. #endif
  261. } fields;
  262. unsigned int word;
  263. } cbcr;
  264. cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
  265. if (cbcr.fields.c && ioc_enable)
  266. ioc_exists = true;
  267. }
  268. #endif
  269. void read_decode_cache_bcr(void)
  270. {
  271. int dc_line_sz = 0, ic_line_sz = 0;
  272. union {
  273. struct {
  274. #ifdef CONFIG_CPU_BIG_ENDIAN
  275. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  276. #else
  277. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  278. #endif
  279. } fields;
  280. unsigned int word;
  281. } ibcr, dbcr;
  282. ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
  283. if (ibcr.fields.ver) {
  284. icache_exists = true;
  285. l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
  286. if (!ic_line_sz)
  287. panic("Instruction exists but line length is 0\n");
  288. }
  289. dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
  290. if (dbcr.fields.ver) {
  291. dcache_exists = true;
  292. l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
  293. if (!dc_line_sz)
  294. panic("Data cache exists but line length is 0\n");
  295. }
  296. if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
  297. panic("Instruction and data cache line lengths differ\n");
  298. }
  299. void cache_init(void)
  300. {
  301. read_decode_cache_bcr();
  302. #ifdef CONFIG_ISA_ARCV2
  303. read_decode_cache_bcr_arcv2();
  304. if (ioc_exists)
  305. arc_ioc_setup();
  306. read_decode_mmu_bcr();
  307. /*
  308. * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
  309. * only if PAE exists in current HW. So we had to check pae_exist
  310. * before using them.
  311. */
  312. if (slc_exists && pae_exists)
  313. slc_upper_region_init();
  314. #endif /* CONFIG_ISA_ARCV2 */
  315. }
  316. int icache_status(void)
  317. {
  318. if (!icache_exists)
  319. return 0;
  320. if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE)
  321. return 0;
  322. else
  323. return 1;
  324. }
  325. void icache_enable(void)
  326. {
  327. if (icache_exists)
  328. write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
  329. ~IC_CTRL_CACHE_DISABLE);
  330. }
  331. void icache_disable(void)
  332. {
  333. if (icache_exists)
  334. write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
  335. IC_CTRL_CACHE_DISABLE);
  336. }
  337. /* IC supports only invalidation */
  338. static inline void __ic_entire_invalidate(void)
  339. {
  340. if (!icache_status())
  341. return;
  342. /* Any write to IC_IVIC register triggers invalidation of entire I$ */
  343. write_aux_reg(ARC_AUX_IC_IVIC, 1);
  344. /*
  345. * As per ARC HS databook (see chapter 5.3.3.2)
  346. * it is required to add 3 NOPs after each write to IC_IVIC.
  347. */
  348. __builtin_arc_nop();
  349. __builtin_arc_nop();
  350. __builtin_arc_nop();
  351. read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
  352. }
  353. void invalidate_icache_all(void)
  354. {
  355. __ic_entire_invalidate();
  356. #ifdef CONFIG_ISA_ARCV2
  357. if (slc_exists)
  358. __slc_entire_op(OP_INV);
  359. #endif
  360. }
  361. int dcache_status(void)
  362. {
  363. if (!dcache_exists)
  364. return 0;
  365. if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE)
  366. return 0;
  367. else
  368. return 1;
  369. }
  370. void dcache_enable(void)
  371. {
  372. if (!dcache_exists)
  373. return;
  374. write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
  375. ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
  376. }
  377. void dcache_disable(void)
  378. {
  379. if (!dcache_exists)
  380. return;
  381. write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
  382. DC_CTRL_CACHE_DISABLE);
  383. }
  384. #ifndef CONFIG_SYS_DCACHE_OFF
  385. /* Common Helper for Line Operations on D-cache */
  386. static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
  387. const int cacheop)
  388. {
  389. unsigned int aux_cmd;
  390. int num_lines;
  391. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  392. aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
  393. sz += paddr & ~CACHE_LINE_MASK;
  394. paddr &= CACHE_LINE_MASK;
  395. num_lines = DIV_ROUND_UP(sz, l1_line_sz);
  396. while (num_lines-- > 0) {
  397. #if (CONFIG_ARC_MMU_VER == 3)
  398. write_aux_reg(ARC_AUX_DC_PTAG, paddr);
  399. #endif
  400. write_aux_reg(aux_cmd, paddr);
  401. paddr += l1_line_sz;
  402. }
  403. }
  404. static void __before_dc_op(const int op)
  405. {
  406. unsigned int ctrl;
  407. ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
  408. /* IM bit implies flush-n-inv, instead of vanilla inv */
  409. if (op == OP_INV)
  410. ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
  411. else
  412. ctrl |= DC_CTRL_INV_MODE_FLUSH;
  413. write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
  414. }
  415. static void __after_dc_op(const int op)
  416. {
  417. if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
  418. while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
  419. }
  420. static inline void __dc_entire_op(const int cacheop)
  421. {
  422. int aux;
  423. __before_dc_op(cacheop);
  424. if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  425. aux = ARC_AUX_DC_IVDC;
  426. else
  427. aux = ARC_AUX_DC_FLSH;
  428. write_aux_reg(aux, 0x1);
  429. __after_dc_op(cacheop);
  430. }
  431. static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
  432. const int cacheop)
  433. {
  434. __before_dc_op(cacheop);
  435. __dcache_line_loop(paddr, sz, cacheop);
  436. __after_dc_op(cacheop);
  437. }
  438. #else
  439. #define __dc_entire_op(cacheop)
  440. #define __dc_line_op(paddr, sz, cacheop)
  441. #endif /* !CONFIG_SYS_DCACHE_OFF */
  442. void invalidate_dcache_range(unsigned long start, unsigned long end)
  443. {
  444. if (start >= end)
  445. return;
  446. #ifdef CONFIG_ISA_ARCV2
  447. if (!ioc_exists)
  448. #endif
  449. __dc_line_op(start, end - start, OP_INV);
  450. #ifdef CONFIG_ISA_ARCV2
  451. if (slc_exists && !ioc_exists)
  452. __slc_rgn_op(start, end - start, OP_INV);
  453. #endif
  454. }
  455. void flush_dcache_range(unsigned long start, unsigned long end)
  456. {
  457. if (start >= end)
  458. return;
  459. #ifdef CONFIG_ISA_ARCV2
  460. if (!ioc_exists)
  461. #endif
  462. __dc_line_op(start, end - start, OP_FLUSH);
  463. #ifdef CONFIG_ISA_ARCV2
  464. if (slc_exists && !ioc_exists)
  465. __slc_rgn_op(start, end - start, OP_FLUSH);
  466. #endif
  467. }
  468. void flush_cache(unsigned long start, unsigned long size)
  469. {
  470. flush_dcache_range(start, start + size);
  471. }
  472. /*
  473. * As invalidate_dcache_all() is not used in generic U-Boot code and as we
  474. * don't need it in arch/arc code alone (invalidate without flush) we implement
  475. * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
  476. * it's much safer. See [ NOTE 1 ] for more details.
  477. */
  478. void flush_n_invalidate_dcache_all(void)
  479. {
  480. __dc_entire_op(OP_FLUSH_N_INV);
  481. #ifdef CONFIG_ISA_ARCV2
  482. if (slc_exists)
  483. __slc_entire_op(OP_FLUSH_N_INV);
  484. #endif
  485. }
  486. void flush_dcache_all(void)
  487. {
  488. __dc_entire_op(OP_FLUSH);
  489. #ifdef CONFIG_ISA_ARCV2
  490. if (slc_exists)
  491. __slc_entire_op(OP_FLUSH);
  492. #endif
  493. }