cache.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <linux/compiler.h>
  8. #include <linux/kernel.h>
  9. #include <asm/arcregs.h>
  10. #include <asm/cache.h>
  11. #define CACHE_LINE_MASK (~(CONFIG_SYS_CACHELINE_SIZE - 1))
  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. #ifdef CONFIG_ISA_ARCV2
  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 slc_line_sz __section(".data");
  30. int slc_exists __section(".data");
  31. static unsigned int __before_slc_op(const int op)
  32. {
  33. unsigned int reg = reg;
  34. if (op == OP_INV) {
  35. /*
  36. * IM is set by default and implies Flush-n-inv
  37. * Clear it here for vanilla inv
  38. */
  39. reg = read_aux_reg(ARC_AUX_SLC_CTRL);
  40. write_aux_reg(ARC_AUX_SLC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
  41. }
  42. return reg;
  43. }
  44. static void __after_slc_op(const int op, unsigned int reg)
  45. {
  46. if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
  47. while (read_aux_reg(ARC_AUX_SLC_CTRL) &
  48. DC_CTRL_FLUSH_STATUS)
  49. ;
  50. /* Switch back to default Invalidate mode */
  51. if (op == OP_INV)
  52. write_aux_reg(ARC_AUX_SLC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
  53. }
  54. static inline void __slc_line_loop(unsigned long paddr, unsigned long sz,
  55. const int op)
  56. {
  57. unsigned int aux_cmd;
  58. int num_lines;
  59. #define SLC_LINE_MASK (~(slc_line_sz - 1))
  60. aux_cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL;
  61. sz += paddr & ~SLC_LINE_MASK;
  62. paddr &= SLC_LINE_MASK;
  63. num_lines = DIV_ROUND_UP(sz, slc_line_sz);
  64. while (num_lines-- > 0) {
  65. write_aux_reg(aux_cmd, paddr);
  66. paddr += slc_line_sz;
  67. }
  68. }
  69. static inline void __slc_entire_op(const int cacheop)
  70. {
  71. int aux;
  72. unsigned int ctrl_reg = __before_slc_op(cacheop);
  73. if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  74. aux = ARC_AUX_SLC_INVALIDATE;
  75. else
  76. aux = ARC_AUX_SLC_FLUSH;
  77. write_aux_reg(aux, 0x1);
  78. __after_slc_op(cacheop, ctrl_reg);
  79. }
  80. static inline void __slc_line_op(unsigned long paddr, unsigned long sz,
  81. const int cacheop)
  82. {
  83. unsigned int ctrl_reg = __before_slc_op(cacheop);
  84. __slc_line_loop(paddr, sz, cacheop);
  85. __after_slc_op(cacheop, ctrl_reg);
  86. }
  87. #else
  88. #define __slc_entire_op(cacheop)
  89. #define __slc_line_op(paddr, sz, cacheop)
  90. #endif
  91. static inline int icache_exists(void)
  92. {
  93. /* Check if Instruction Cache is available */
  94. if (read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK)
  95. return 1;
  96. else
  97. return 0;
  98. }
  99. static inline int dcache_exists(void)
  100. {
  101. /* Check if Data Cache is available */
  102. if (read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK)
  103. return 1;
  104. else
  105. return 0;
  106. }
  107. void cache_init(void)
  108. {
  109. #ifdef CONFIG_ISA_ARCV2
  110. /* Check if System-Level Cache (SLC) is available */
  111. if (read_aux_reg(ARC_BCR_SLC) & CACHE_VER_NUM_MASK) {
  112. #define LSIZE_OFFSET 4
  113. #define LSIZE_MASK 3
  114. if (read_aux_reg(ARC_AUX_SLC_CONFIG) &
  115. (LSIZE_MASK << LSIZE_OFFSET))
  116. slc_line_sz = 64;
  117. else
  118. slc_line_sz = 128;
  119. slc_exists = 1;
  120. } else {
  121. slc_exists = 0;
  122. }
  123. #endif
  124. }
  125. int icache_status(void)
  126. {
  127. if (!icache_exists())
  128. return 0;
  129. if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE)
  130. return 0;
  131. else
  132. return 1;
  133. }
  134. void icache_enable(void)
  135. {
  136. if (icache_exists())
  137. write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
  138. ~IC_CTRL_CACHE_DISABLE);
  139. }
  140. void icache_disable(void)
  141. {
  142. if (icache_exists())
  143. write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
  144. IC_CTRL_CACHE_DISABLE);
  145. }
  146. #ifndef CONFIG_SYS_DCACHE_OFF
  147. void invalidate_icache_all(void)
  148. {
  149. /* Any write to IC_IVIC register triggers invalidation of entire I$ */
  150. if (icache_status()) {
  151. write_aux_reg(ARC_AUX_IC_IVIC, 1);
  152. read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
  153. }
  154. }
  155. #else
  156. void invalidate_icache_all(void)
  157. {
  158. }
  159. #endif
  160. int dcache_status(void)
  161. {
  162. if (!dcache_exists())
  163. return 0;
  164. if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE)
  165. return 0;
  166. else
  167. return 1;
  168. }
  169. void dcache_enable(void)
  170. {
  171. if (!dcache_exists())
  172. return;
  173. write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
  174. ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
  175. }
  176. void dcache_disable(void)
  177. {
  178. if (!dcache_exists())
  179. return;
  180. write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
  181. DC_CTRL_CACHE_DISABLE);
  182. }
  183. #ifndef CONFIG_SYS_DCACHE_OFF
  184. /*
  185. * Common Helper for Line Operations on {I,D}-Cache
  186. */
  187. static inline void __cache_line_loop(unsigned long paddr, unsigned long sz,
  188. const int cacheop)
  189. {
  190. unsigned int aux_cmd;
  191. #if (CONFIG_ARC_MMU_VER == 3)
  192. unsigned int aux_tag;
  193. #endif
  194. int num_lines;
  195. if (cacheop == OP_INV_IC) {
  196. aux_cmd = ARC_AUX_IC_IVIL;
  197. #if (CONFIG_ARC_MMU_VER == 3)
  198. aux_tag = ARC_AUX_IC_PTAG;
  199. #endif
  200. } else {
  201. /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
  202. aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
  203. #if (CONFIG_ARC_MMU_VER == 3)
  204. aux_tag = ARC_AUX_DC_PTAG;
  205. #endif
  206. }
  207. sz += paddr & ~CACHE_LINE_MASK;
  208. paddr &= CACHE_LINE_MASK;
  209. num_lines = DIV_ROUND_UP(sz, CONFIG_SYS_CACHELINE_SIZE);
  210. while (num_lines-- > 0) {
  211. #if (CONFIG_ARC_MMU_VER == 3)
  212. write_aux_reg(aux_tag, paddr);
  213. #endif
  214. write_aux_reg(aux_cmd, paddr);
  215. paddr += CONFIG_SYS_CACHELINE_SIZE;
  216. }
  217. }
  218. static unsigned int __before_dc_op(const int op)
  219. {
  220. unsigned int reg;
  221. if (op == OP_INV) {
  222. /*
  223. * IM is set by default and implies Flush-n-inv
  224. * Clear it here for vanilla inv
  225. */
  226. reg = read_aux_reg(ARC_AUX_DC_CTRL);
  227. write_aux_reg(ARC_AUX_DC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
  228. }
  229. return reg;
  230. }
  231. static void __after_dc_op(const int op, unsigned int reg)
  232. {
  233. if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
  234. while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
  235. ;
  236. /* Switch back to default Invalidate mode */
  237. if (op == OP_INV)
  238. write_aux_reg(ARC_AUX_DC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
  239. }
  240. static inline void __dc_entire_op(const int cacheop)
  241. {
  242. int aux;
  243. unsigned int ctrl_reg = __before_dc_op(cacheop);
  244. if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
  245. aux = ARC_AUX_DC_IVDC;
  246. else
  247. aux = ARC_AUX_DC_FLSH;
  248. write_aux_reg(aux, 0x1);
  249. __after_dc_op(cacheop, ctrl_reg);
  250. }
  251. static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
  252. const int cacheop)
  253. {
  254. unsigned int ctrl_reg = __before_dc_op(cacheop);
  255. __cache_line_loop(paddr, sz, cacheop);
  256. __after_dc_op(cacheop, ctrl_reg);
  257. }
  258. #else
  259. #define __dc_entire_op(cacheop)
  260. #define __dc_line_op(paddr, sz, cacheop)
  261. #endif /* !CONFIG_SYS_DCACHE_OFF */
  262. void invalidate_dcache_range(unsigned long start, unsigned long end)
  263. {
  264. __dc_line_op(start, end - start, OP_INV);
  265. #ifdef CONFIG_ISA_ARCV2
  266. if (slc_exists)
  267. __slc_line_op(start, end - start, OP_INV);
  268. #endif
  269. }
  270. void flush_dcache_range(unsigned long start, unsigned long end)
  271. {
  272. __dc_line_op(start, end - start, OP_FLUSH);
  273. #ifdef CONFIG_ISA_ARCV2
  274. if (slc_exists)
  275. __slc_line_op(start, end - start, OP_FLUSH);
  276. #endif
  277. }
  278. void flush_cache(unsigned long start, unsigned long size)
  279. {
  280. flush_dcache_range(start, start + size);
  281. }
  282. void invalidate_dcache_all(void)
  283. {
  284. __dc_entire_op(OP_INV);
  285. #ifdef CONFIG_ISA_ARCV2
  286. if (slc_exists)
  287. __slc_entire_op(OP_INV);
  288. #endif
  289. }
  290. void flush_dcache_all(void)
  291. {
  292. __dc_entire_op(OP_FLUSH);
  293. #ifdef CONFIG_ISA_ARCV2
  294. if (slc_exists)
  295. __slc_entire_op(OP_FLUSH);
  296. #endif
  297. }