cache.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. /* Bit values in IC_CTRL */
  14. #define IC_CTRL_CACHE_DISABLE (1 << 0)
  15. /* Bit values in DC_CTRL */
  16. #define DC_CTRL_CACHE_DISABLE (1 << 0)
  17. #define DC_CTRL_INV_MODE_FLUSH (1 << 6)
  18. #define DC_CTRL_FLUSH_STATUS (1 << 8)
  19. #define CACHE_VER_NUM_MASK 0xF
  20. #define SLC_CTRL_SB (1 << 2)
  21. #define OP_INV 0x1
  22. #define OP_FLUSH 0x2
  23. #define OP_INV_IC 0x3
  24. /*
  25. * By default that variable will fall into .bss section.
  26. * But .bss section is not relocated and so it will be initilized before
  27. * relocation but will be used after being zeroed.
  28. */
  29. int l1_line_sz __section(".data");
  30. int dcache_exists __section(".data");
  31. int icache_exists __section(".data");
  32. #define CACHE_LINE_MASK (~(l1_line_sz - 1))
  33. #ifdef CONFIG_ISA_ARCV2
  34. int slc_line_sz __section(".data");
  35. int slc_exists __section(".data");
  36. int ioc_exists __section(".data");
  37. static unsigned int __before_slc_op(const int op)
  38. {
  39. unsigned int reg = reg;
  40. if (op == OP_INV) {
  41. /*
  42. * IM is set by default and implies Flush-n-inv
  43. * Clear it here for vanilla inv
  44. */
  45. reg = read_aux_reg(ARC_AUX_SLC_CTRL);
  46. write_aux_reg(ARC_AUX_SLC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
  47. }
  48. return reg;
  49. }
  50. static void __after_slc_op(const int op, unsigned int reg)
  51. {
  52. if (op & OP_FLUSH) { /* flush / flush-n-inv both wait */
  53. /*
  54. * Make sure "busy" bit reports correct status,
  55. * see STAR 9001165532
  56. */
  57. read_aux_reg(ARC_AUX_SLC_CTRL);
  58. while (read_aux_reg(ARC_AUX_SLC_CTRL) &
  59. DC_CTRL_FLUSH_STATUS)
  60. ;
  61. }
  62. /* Switch back to default Invalidate mode */
  63. if (op == OP_INV)
  64. write_aux_reg(ARC_AUX_SLC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
  65. }
  66. static inline void __slc_line_loop(unsigned long paddr, unsigned long sz,
  67. const int op)
  68. {
  69. unsigned int aux_cmd;
  70. int num_lines;
  71. #define SLC_LINE_MASK (~(slc_line_sz - 1))
  72. aux_cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL;
  73. sz += paddr & ~SLC_LINE_MASK;
  74. paddr &= SLC_LINE_MASK;
  75. num_lines = DIV_ROUND_UP(sz, slc_line_sz);
  76. while (num_lines-- > 0) {
  77. write_aux_reg(aux_cmd, paddr);
  78. paddr += slc_line_sz;
  79. }
  80. }
  81. static inline void __slc_entire_op(const int cacheop)
  82. {
  83. int aux;
  84. unsigned int ctrl_reg = __before_slc_op(cacheop);
  85. if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  86. aux = ARC_AUX_SLC_INVALIDATE;
  87. else
  88. aux = ARC_AUX_SLC_FLUSH;
  89. write_aux_reg(aux, 0x1);
  90. __after_slc_op(cacheop, ctrl_reg);
  91. }
  92. static inline void __slc_line_op(unsigned long paddr, unsigned long sz,
  93. const int cacheop)
  94. {
  95. unsigned int ctrl_reg = __before_slc_op(cacheop);
  96. __slc_line_loop(paddr, sz, cacheop);
  97. __after_slc_op(cacheop, ctrl_reg);
  98. }
  99. #else
  100. #define __slc_entire_op(cacheop)
  101. #define __slc_line_op(paddr, sz, cacheop)
  102. #endif
  103. #ifdef CONFIG_ISA_ARCV2
  104. static void read_decode_cache_bcr_arcv2(void)
  105. {
  106. union {
  107. struct {
  108. #ifdef CONFIG_CPU_BIG_ENDIAN
  109. unsigned int pad:24, way:2, lsz:2, sz:4;
  110. #else
  111. unsigned int sz:4, lsz:2, way:2, pad:24;
  112. #endif
  113. } fields;
  114. unsigned int word;
  115. } slc_cfg;
  116. union {
  117. struct {
  118. #ifdef CONFIG_CPU_BIG_ENDIAN
  119. unsigned int pad:24, ver:8;
  120. #else
  121. unsigned int ver:8, pad:24;
  122. #endif
  123. } fields;
  124. unsigned int word;
  125. } sbcr;
  126. sbcr.word = read_aux_reg(ARC_BCR_SLC);
  127. if (sbcr.fields.ver) {
  128. slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
  129. slc_exists = 1;
  130. slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
  131. }
  132. union {
  133. struct bcr_clust_cfg {
  134. #ifdef CONFIG_CPU_BIG_ENDIAN
  135. unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8;
  136. #else
  137. unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7;
  138. #endif
  139. } fields;
  140. unsigned int word;
  141. } cbcr;
  142. cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
  143. if (cbcr.fields.c)
  144. ioc_exists = 1;
  145. }
  146. #endif
  147. void read_decode_cache_bcr(void)
  148. {
  149. int dc_line_sz = 0, ic_line_sz = 0;
  150. union {
  151. struct {
  152. #ifdef CONFIG_CPU_BIG_ENDIAN
  153. unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
  154. #else
  155. unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
  156. #endif
  157. } fields;
  158. unsigned int word;
  159. } ibcr, dbcr;
  160. ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
  161. if (ibcr.fields.ver) {
  162. icache_exists = 1;
  163. l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
  164. if (!ic_line_sz)
  165. panic("Instruction exists but line length is 0\n");
  166. }
  167. dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
  168. if (dbcr.fields.ver){
  169. dcache_exists = 1;
  170. l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
  171. if (!dc_line_sz)
  172. panic("Data cache exists but line length is 0\n");
  173. }
  174. if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
  175. panic("Instruction and data cache line lengths differ\n");
  176. }
  177. void cache_init(void)
  178. {
  179. read_decode_cache_bcr();
  180. #ifdef CONFIG_ISA_ARCV2
  181. read_decode_cache_bcr_arcv2();
  182. if (ioc_exists) {
  183. /* IOC Aperture start is equal to DDR start */
  184. unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
  185. /* IOC Aperture size is equal to DDR size */
  186. long ap_size = CONFIG_SYS_SDRAM_SIZE;
  187. flush_dcache_all();
  188. invalidate_dcache_all();
  189. if (!is_power_of_2(ap_size) || ap_size < 4096)
  190. panic("IOC Aperture size must be power of 2 and bigger 4Kib");
  191. /*
  192. * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
  193. * so setting 0x11 implies 512M, 0x12 implies 1G...
  194. */
  195. write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
  196. order_base_2(ap_size/1024) - 2);
  197. /* IOC Aperture start must be aligned to the size of the aperture */
  198. if (ap_base % ap_size != 0)
  199. panic("IOC Aperture start must be aligned to the size of the aperture");
  200. write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
  201. write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
  202. write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
  203. }
  204. #endif
  205. }
  206. int icache_status(void)
  207. {
  208. if (!icache_exists)
  209. return 0;
  210. if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE)
  211. return 0;
  212. else
  213. return 1;
  214. }
  215. void icache_enable(void)
  216. {
  217. if (icache_exists)
  218. write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
  219. ~IC_CTRL_CACHE_DISABLE);
  220. }
  221. void icache_disable(void)
  222. {
  223. if (icache_exists)
  224. write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
  225. IC_CTRL_CACHE_DISABLE);
  226. }
  227. #ifndef CONFIG_SYS_DCACHE_OFF
  228. void invalidate_icache_all(void)
  229. {
  230. /* Any write to IC_IVIC register triggers invalidation of entire I$ */
  231. if (icache_status()) {
  232. write_aux_reg(ARC_AUX_IC_IVIC, 1);
  233. read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
  234. }
  235. }
  236. #else
  237. void invalidate_icache_all(void)
  238. {
  239. }
  240. #endif
  241. int dcache_status(void)
  242. {
  243. if (!dcache_exists)
  244. return 0;
  245. if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE)
  246. return 0;
  247. else
  248. return 1;
  249. }
  250. void dcache_enable(void)
  251. {
  252. if (!dcache_exists)
  253. return;
  254. write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
  255. ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
  256. }
  257. void dcache_disable(void)
  258. {
  259. if (!dcache_exists)
  260. return;
  261. write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
  262. DC_CTRL_CACHE_DISABLE);
  263. }
  264. #ifndef CONFIG_SYS_DCACHE_OFF
  265. /*
  266. * Common Helper for Line Operations on {I,D}-Cache
  267. */
  268. static inline void __cache_line_loop(unsigned long paddr, unsigned long sz,
  269. const int cacheop)
  270. {
  271. unsigned int aux_cmd;
  272. #if (CONFIG_ARC_MMU_VER == 3)
  273. unsigned int aux_tag;
  274. #endif
  275. int num_lines;
  276. if (cacheop == OP_INV_IC) {
  277. aux_cmd = ARC_AUX_IC_IVIL;
  278. #if (CONFIG_ARC_MMU_VER == 3)
  279. aux_tag = ARC_AUX_IC_PTAG;
  280. #endif
  281. } else {
  282. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  283. aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
  284. #if (CONFIG_ARC_MMU_VER == 3)
  285. aux_tag = ARC_AUX_DC_PTAG;
  286. #endif
  287. }
  288. sz += paddr & ~CACHE_LINE_MASK;
  289. paddr &= CACHE_LINE_MASK;
  290. num_lines = DIV_ROUND_UP(sz, l1_line_sz);
  291. while (num_lines-- > 0) {
  292. #if (CONFIG_ARC_MMU_VER == 3)
  293. write_aux_reg(aux_tag, paddr);
  294. #endif
  295. write_aux_reg(aux_cmd, paddr);
  296. paddr += l1_line_sz;
  297. }
  298. }
  299. static unsigned int __before_dc_op(const int op)
  300. {
  301. unsigned int reg;
  302. if (op == OP_INV) {
  303. /*
  304. * IM is set by default and implies Flush-n-inv
  305. * Clear it here for vanilla inv
  306. */
  307. reg = read_aux_reg(ARC_AUX_DC_CTRL);
  308. write_aux_reg(ARC_AUX_DC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
  309. }
  310. return reg;
  311. }
  312. static void __after_dc_op(const int op, unsigned int reg)
  313. {
  314. if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
  315. while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
  316. ;
  317. /* Switch back to default Invalidate mode */
  318. if (op == OP_INV)
  319. write_aux_reg(ARC_AUX_DC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
  320. }
  321. static inline void __dc_entire_op(const int cacheop)
  322. {
  323. int aux;
  324. unsigned int ctrl_reg = __before_dc_op(cacheop);
  325. if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  326. aux = ARC_AUX_DC_IVDC;
  327. else
  328. aux = ARC_AUX_DC_FLSH;
  329. write_aux_reg(aux, 0x1);
  330. __after_dc_op(cacheop, ctrl_reg);
  331. }
  332. static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
  333. const int cacheop)
  334. {
  335. unsigned int ctrl_reg = __before_dc_op(cacheop);
  336. __cache_line_loop(paddr, sz, cacheop);
  337. __after_dc_op(cacheop, ctrl_reg);
  338. }
  339. #else
  340. #define __dc_entire_op(cacheop)
  341. #define __dc_line_op(paddr, sz, cacheop)
  342. #endif /* !CONFIG_SYS_DCACHE_OFF */
  343. void invalidate_dcache_range(unsigned long start, unsigned long end)
  344. {
  345. #ifdef CONFIG_ISA_ARCV2
  346. if (!ioc_exists)
  347. #endif
  348. __dc_line_op(start, end - start, OP_INV);
  349. #ifdef CONFIG_ISA_ARCV2
  350. if (slc_exists && !ioc_exists)
  351. __slc_line_op(start, end - start, OP_INV);
  352. #endif
  353. }
  354. void flush_dcache_range(unsigned long start, unsigned long end)
  355. {
  356. #ifdef CONFIG_ISA_ARCV2
  357. if (!ioc_exists)
  358. #endif
  359. __dc_line_op(start, end - start, OP_FLUSH);
  360. #ifdef CONFIG_ISA_ARCV2
  361. if (slc_exists && !ioc_exists)
  362. __slc_line_op(start, end - start, OP_FLUSH);
  363. #endif
  364. }
  365. void flush_cache(unsigned long start, unsigned long size)
  366. {
  367. flush_dcache_range(start, start + size);
  368. }
  369. void invalidate_dcache_all(void)
  370. {
  371. __dc_entire_op(OP_INV);
  372. #ifdef CONFIG_ISA_ARCV2
  373. if (slc_exists)
  374. __slc_entire_op(OP_INV);
  375. #endif
  376. }
  377. void flush_dcache_all(void)
  378. {
  379. __dc_entire_op(OP_FLUSH);
  380. #ifdef CONFIG_ISA_ARCV2
  381. if (slc_exists)
  382. __slc_entire_op(OP_FLUSH);
  383. #endif
  384. }