cache_v7.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * (C) Copyright 2010
  3. * Texas Instruments, <www.ti.com>
  4. * Aneesh V <aneesh@ti.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <linux/types.h>
  9. #include <common.h>
  10. #include <asm/armv7.h>
  11. #include <asm/utils.h>
  12. #define ARMV7_DCACHE_INVAL_ALL 1
  13. #define ARMV7_DCACHE_CLEAN_INVAL_ALL 2
  14. #define ARMV7_DCACHE_INVAL_RANGE 3
  15. #define ARMV7_DCACHE_CLEAN_INVAL_RANGE 4
  16. #ifndef CONFIG_SYS_DCACHE_OFF
  17. static int check_cache_range(unsigned long start, unsigned long stop)
  18. {
  19. int ok = 1;
  20. if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
  21. ok = 0;
  22. if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
  23. ok = 0;
  24. if (!ok)
  25. debug("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
  26. start, stop);
  27. return ok;
  28. }
  29. /*
  30. * Write the level and type you want to Cache Size Selection Register(CSSELR)
  31. * to get size details from Current Cache Size ID Register(CCSIDR)
  32. */
  33. static void set_csselr(u32 level, u32 type)
  34. {
  35. u32 csselr = level << 1 | type;
  36. /* Write to Cache Size Selection Register(CSSELR) */
  37. asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (csselr));
  38. }
  39. static u32 get_ccsidr(void)
  40. {
  41. u32 ccsidr;
  42. /* Read current CP15 Cache Size ID Register */
  43. asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
  44. return ccsidr;
  45. }
  46. static u32 get_clidr(void)
  47. {
  48. u32 clidr;
  49. /* Read current CP15 Cache Level ID Register */
  50. asm volatile ("mrc p15,1,%0,c0,c0,1" : "=r" (clidr));
  51. return clidr;
  52. }
  53. static void v7_inval_dcache_level_setway(u32 level, u32 num_sets,
  54. u32 num_ways, u32 way_shift,
  55. u32 log2_line_len)
  56. {
  57. int way, set;
  58. u32 setway;
  59. /*
  60. * For optimal assembly code:
  61. * a. count down
  62. * b. have bigger loop inside
  63. */
  64. for (way = num_ways - 1; way >= 0 ; way--) {
  65. for (set = num_sets - 1; set >= 0; set--) {
  66. setway = (level << 1) | (set << log2_line_len) |
  67. (way << way_shift);
  68. /* Invalidate data/unified cache line by set/way */
  69. asm volatile (" mcr p15, 0, %0, c7, c6, 2"
  70. : : "r" (setway));
  71. }
  72. }
  73. /* DSB to make sure the operation is complete */
  74. DSB;
  75. }
  76. static void v7_clean_inval_dcache_level_setway(u32 level, u32 num_sets,
  77. u32 num_ways, u32 way_shift,
  78. u32 log2_line_len)
  79. {
  80. int way, set;
  81. u32 setway;
  82. /*
  83. * For optimal assembly code:
  84. * a. count down
  85. * b. have bigger loop inside
  86. */
  87. for (way = num_ways - 1; way >= 0 ; way--) {
  88. for (set = num_sets - 1; set >= 0; set--) {
  89. setway = (level << 1) | (set << log2_line_len) |
  90. (way << way_shift);
  91. /*
  92. * Clean & Invalidate data/unified
  93. * cache line by set/way
  94. */
  95. asm volatile (" mcr p15, 0, %0, c7, c14, 2"
  96. : : "r" (setway));
  97. }
  98. }
  99. /* DSB to make sure the operation is complete */
  100. DSB;
  101. }
  102. static void v7_maint_dcache_level_setway(u32 level, u32 operation)
  103. {
  104. u32 ccsidr;
  105. u32 num_sets, num_ways, log2_line_len, log2_num_ways;
  106. u32 way_shift;
  107. set_csselr(level, ARMV7_CSSELR_IND_DATA_UNIFIED);
  108. ccsidr = get_ccsidr();
  109. log2_line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
  110. CCSIDR_LINE_SIZE_OFFSET) + 2;
  111. /* Converting from words to bytes */
  112. log2_line_len += 2;
  113. num_ways = ((ccsidr & CCSIDR_ASSOCIATIVITY_MASK) >>
  114. CCSIDR_ASSOCIATIVITY_OFFSET) + 1;
  115. num_sets = ((ccsidr & CCSIDR_NUM_SETS_MASK) >>
  116. CCSIDR_NUM_SETS_OFFSET) + 1;
  117. /*
  118. * According to ARMv7 ARM number of sets and number of ways need
  119. * not be a power of 2
  120. */
  121. log2_num_ways = log_2_n_round_up(num_ways);
  122. way_shift = (32 - log2_num_ways);
  123. if (operation == ARMV7_DCACHE_INVAL_ALL) {
  124. v7_inval_dcache_level_setway(level, num_sets, num_ways,
  125. way_shift, log2_line_len);
  126. } else if (operation == ARMV7_DCACHE_CLEAN_INVAL_ALL) {
  127. v7_clean_inval_dcache_level_setway(level, num_sets, num_ways,
  128. way_shift, log2_line_len);
  129. }
  130. }
  131. static void v7_maint_dcache_all(u32 operation)
  132. {
  133. u32 level, cache_type, level_start_bit = 0;
  134. u32 clidr = get_clidr();
  135. for (level = 0; level < 7; level++) {
  136. cache_type = (clidr >> level_start_bit) & 0x7;
  137. if ((cache_type == ARMV7_CLIDR_CTYPE_DATA_ONLY) ||
  138. (cache_type == ARMV7_CLIDR_CTYPE_INSTRUCTION_DATA) ||
  139. (cache_type == ARMV7_CLIDR_CTYPE_UNIFIED))
  140. v7_maint_dcache_level_setway(level, operation);
  141. level_start_bit += 3;
  142. }
  143. }
  144. static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
  145. {
  146. u32 mva;
  147. /* Align start to cache line boundary */
  148. start &= ~(line_len - 1);
  149. for (mva = start; mva < stop; mva = mva + line_len) {
  150. /* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
  151. asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
  152. }
  153. }
  154. static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
  155. {
  156. u32 mva;
  157. /*
  158. * If start address is not aligned to cache-line do not
  159. * invalidate the first cache-line
  160. */
  161. if (start & (line_len - 1)) {
  162. printf("ERROR: %s - start address is not aligned - 0x%08x\n",
  163. __func__, start);
  164. /* move to next cache line */
  165. start = (start + line_len - 1) & ~(line_len - 1);
  166. }
  167. /*
  168. * If stop address is not aligned to cache-line do not
  169. * invalidate the last cache-line
  170. */
  171. if (stop & (line_len - 1)) {
  172. printf("ERROR: %s - stop address is not aligned - 0x%08x\n",
  173. __func__, stop);
  174. /* align to the beginning of this cache line */
  175. stop &= ~(line_len - 1);
  176. }
  177. for (mva = start; mva < stop; mva = mva + line_len) {
  178. /* DCIMVAC - Invalidate data cache by MVA to PoC */
  179. asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
  180. }
  181. }
  182. static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
  183. {
  184. u32 line_len, ccsidr;
  185. ccsidr = get_ccsidr();
  186. line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
  187. CCSIDR_LINE_SIZE_OFFSET) + 2;
  188. /* Converting from words to bytes */
  189. line_len += 2;
  190. /* converting from log2(linelen) to linelen */
  191. line_len = 1 << line_len;
  192. switch (range_op) {
  193. case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
  194. v7_dcache_clean_inval_range(start, stop, line_len);
  195. break;
  196. case ARMV7_DCACHE_INVAL_RANGE:
  197. v7_dcache_inval_range(start, stop, line_len);
  198. break;
  199. }
  200. /* DSB to make sure the operation is complete */
  201. DSB;
  202. }
  203. /* Invalidate TLB */
  204. static void v7_inval_tlb(void)
  205. {
  206. /* Invalidate entire unified TLB */
  207. asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
  208. /* Invalidate entire data TLB */
  209. asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
  210. /* Invalidate entire instruction TLB */
  211. asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
  212. /* Full system DSB - make sure that the invalidation is complete */
  213. DSB;
  214. /* Full system ISB - make sure the instruction stream sees it */
  215. ISB;
  216. }
  217. void invalidate_dcache_all(void)
  218. {
  219. v7_maint_dcache_all(ARMV7_DCACHE_INVAL_ALL);
  220. v7_outer_cache_inval_all();
  221. }
  222. /*
  223. * Performs a clean & invalidation of the entire data cache
  224. * at all levels
  225. */
  226. void flush_dcache_all(void)
  227. {
  228. v7_maint_dcache_all(ARMV7_DCACHE_CLEAN_INVAL_ALL);
  229. v7_outer_cache_flush_all();
  230. }
  231. /*
  232. * Invalidates range in all levels of D-cache/unified cache used:
  233. * Affects the range [start, stop - 1]
  234. */
  235. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  236. {
  237. check_cache_range(start, stop);
  238. v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
  239. v7_outer_cache_inval_range(start, stop);
  240. }
  241. /*
  242. * Flush range(clean & invalidate) from all levels of D-cache/unified
  243. * cache used:
  244. * Affects the range [start, stop - 1]
  245. */
  246. void flush_dcache_range(unsigned long start, unsigned long stop)
  247. {
  248. check_cache_range(start, stop);
  249. v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
  250. v7_outer_cache_flush_range(start, stop);
  251. }
  252. void arm_init_before_mmu(void)
  253. {
  254. v7_outer_cache_enable();
  255. invalidate_dcache_all();
  256. v7_inval_tlb();
  257. }
  258. void mmu_page_table_flush(unsigned long start, unsigned long stop)
  259. {
  260. flush_dcache_range(start, stop);
  261. v7_inval_tlb();
  262. }
  263. #else /* #ifndef CONFIG_SYS_DCACHE_OFF */
  264. void invalidate_dcache_all(void)
  265. {
  266. }
  267. void flush_dcache_all(void)
  268. {
  269. }
  270. void arm_init_before_mmu(void)
  271. {
  272. }
  273. void mmu_page_table_flush(unsigned long start, unsigned long stop)
  274. {
  275. }
  276. void arm_init_domains(void)
  277. {
  278. }
  279. #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
  280. #ifndef CONFIG_SYS_ICACHE_OFF
  281. /* Invalidate entire I-cache and branch predictor array */
  282. void invalidate_icache_all(void)
  283. {
  284. /*
  285. * Invalidate all instruction caches to PoU.
  286. * Also flushes branch target cache.
  287. */
  288. asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
  289. /* Invalidate entire branch predictor array */
  290. asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
  291. /* Full system DSB - make sure that the invalidation is complete */
  292. DSB;
  293. /* ISB - make sure the instruction stream sees it */
  294. ISB;
  295. }
  296. #else
  297. void invalidate_icache_all(void)
  298. {
  299. }
  300. #endif
  301. /* Stub implementations for outer cache operations */
  302. __weak void v7_outer_cache_enable(void) {}
  303. __weak void v7_outer_cache_disable(void) {}
  304. __weak void v7_outer_cache_flush_all(void) {}
  305. __weak void v7_outer_cache_inval_all(void) {}
  306. __weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
  307. __weak void v7_outer_cache_inval_range(u32 start, u32 end) {}