cache.c 9.3 KB

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