armv7_cache.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "cortex_a.h"
  31. #include "arm_cp_registers.h"
  32. ////////////////////////////////////////////////////////////////////////////////
  33. // Code
  34. ////////////////////////////////////////////////////////////////////////////////
  35. //! @brief Check if dcache is enabled or disabled
  36. int arm_dcache_state_query()
  37. {
  38. uint32_t sctlr; // System Control Register
  39. // read sctlr
  40. _ARM_MRC(15, 0, sctlr, 1, 0, 0);
  41. if (sctlr & BM_SCTLR_C)
  42. {
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. void arm_dcache_enable()
  48. {
  49. uint32_t sctlr; // System Control Register
  50. // read sctlr
  51. _ARM_MRC(15, 0, sctlr, 1, 0, 0);
  52. if (!(sctlr & BM_SCTLR_C))
  53. {
  54. // set C bit (data caching enable)
  55. sctlr |= BM_SCTLR_C;
  56. // write modified sctlr
  57. _ARM_MCR(15, 0, sctlr, 1, 0, 0);
  58. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  59. _ARM_DSB();
  60. }
  61. }
  62. void arm_dcache_disable()
  63. {
  64. uint32_t sctlr; // System Control Register
  65. // read sctlr
  66. _ARM_MRC(15, 0, sctlr, 1, 0, 0);
  67. // set C bit (data caching enable)
  68. sctlr &= ~BM_SCTLR_C;
  69. // write modified sctlr
  70. _ARM_MCR(15, 0, sctlr, 1, 0, 0);
  71. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  72. _ARM_DSB();
  73. }
  74. void arm_dcache_invalidate()
  75. {
  76. uint32_t csid; // Cache Size ID
  77. uint32_t wayset; // wayset parameter
  78. int num_sets; // number of sets
  79. int num_ways; // number of ways
  80. _ARM_MRC(15, 1, csid, 0, 0, 0); // Read Cache Size ID
  81. // Fill number of sets and number of ways from csid register This walues are decremented by 1
  82. num_ways = (csid >> 0x03) & 0x3FFu; //((csid& csid_ASSOCIATIVITY_MASK) >> csid_ASSOCIATIVITY_SHIFT)
  83. // Invalidation all lines (all Sets in all ways)
  84. while (num_ways >= 0)
  85. {
  86. num_sets = (csid >> 0x0D) & 0x7FFFu; //((csid & csid_NUMSETS_MASK) >> csid_NUMSETS_SHIFT)
  87. while (num_sets >= 0 )
  88. {
  89. wayset = (num_sets << 5u) | (num_ways << 30u); //(num_sets << SETWAY_SET_SHIFT) | (num_sets << 3SETWAY_WAY_SHIFT)
  90. // invalidate line if we know set and way
  91. _ARM_MCR(15, 0, wayset, 7, 6, 2);
  92. num_sets--;
  93. }
  94. num_ways--;
  95. }
  96. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  97. _ARM_DSB();
  98. }
  99. void arm_dcache_invalidate_line(const void * addr)
  100. {
  101. uint32_t csidr = 0, line_size = 0;
  102. uint32_t va;
  103. // get the cache line size
  104. _ARM_MRC(15, 1, csidr, 0, 0, 0);
  105. line_size = 1 << ((csidr & 0x7) + 4);
  106. va = (uint32_t) addr & (~(line_size - 1)); //addr & va_VIRTUAL_ADDRESS_MASK
  107. // Invalidate data cache line by va to PoC (Point of Coherency).
  108. _ARM_MCR(15, 0, va, 7, 6, 1);
  109. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  110. _ARM_DSB();
  111. }
  112. void arm_dcache_invalidate_mlines(const void * addr, size_t length)
  113. {
  114. uint32_t va;
  115. uint32_t csidr = 0, line_size = 0;
  116. // get the cache line size
  117. _ARM_MRC(15, 1, csidr, 0, 0, 0);
  118. line_size = 1 << ((csidr & 0x7) + 4);
  119. // align the address with line
  120. const void * end_addr = (const void *)((uint32_t)addr + length);
  121. do
  122. {
  123. // Clean data cache line to PoC (Point of Coherence) by va.
  124. va = (uint32_t) ((uint32_t)addr & (~(line_size - 1))); //addr & va_VIRTUAL_ADDRESS_MASK
  125. _ARM_MCR(15, 0, va, 7, 6, 1);
  126. // increment addres to next line and decrement lenght
  127. addr = (const void *) ((uint32_t)addr + line_size);
  128. } while (addr < end_addr);
  129. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  130. _ARM_DSB();
  131. }
  132. void arm_dcache_flush()
  133. {
  134. uint32_t csid; // Cache Size ID
  135. uint32_t wayset; // wayset parameter
  136. int num_sets; // number of sets
  137. int num_ways; // number of ways
  138. _ARM_MRC(15, 1, csid, 0, 0, 0); // Read Cache Size ID
  139. // Fill number of sets and number of ways from csid register This walues are decremented by 1
  140. num_ways = (csid >> 0x03) & 0x3FFu; //((csid& csid_ASSOCIATIVITY_MASK) >> csid_ASSOCIATIVITY_SHIFT`)
  141. while (num_ways >= 0)
  142. {
  143. num_sets = (csid >> 0x0D) & 0x7FFFu; //((csid & csid_NUMSETS_MASK) >> csid_NUMSETS_SHIFT )
  144. while (num_sets >= 0 )
  145. {
  146. wayset = (num_sets << 5u) | (num_ways << 30u); //(num_sets << SETWAY_SET_SHIFT) | (num_ways << 3SETWAY_WAY_SHIFT)
  147. // FLUSH (clean) line if we know set and way
  148. _ARM_MCR(15, 0, wayset, 7, 10, 2);
  149. num_sets--;
  150. }
  151. num_ways--;
  152. }
  153. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  154. _ARM_DSB();
  155. }
  156. void arm_dcache_flush_line(const void * addr)
  157. {
  158. uint32_t csidr = 0, line_size = 0;
  159. uint32_t va;
  160. // get the cache line size
  161. _ARM_MRC(15, 1, csidr, 0, 0, 0);
  162. line_size = 1 << ((csidr & 0x7) + 4);
  163. va = (uint32_t) addr & (~(line_size - 1)); //addr & va_VIRTUAL_ADDRESS_MASK
  164. // Clean data cache line to PoC (Point of Coherence) by va.
  165. _ARM_MCR(15, 0, va, 7, 10, 1);
  166. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  167. _ARM_DSB();
  168. }
  169. void arm_dcache_flush_mlines(const void * addr, size_t length)
  170. {
  171. uint32_t va;
  172. uint32_t csidr = 0, line_size = 0;
  173. const void * end_addr = (const void *)((uint32_t)addr + length);
  174. // get the cache line size
  175. _ARM_MRC(15, 1, csidr, 0, 0, 0);
  176. line_size = 1 << ((csidr & 0x7) + 4);
  177. do
  178. {
  179. // Clean data cache line to PoC (Point of Coherence) by va.
  180. va = (uint32_t) ((uint32_t)addr & (~(line_size - 1))); //addr & va_VIRTUAL_ADDRESS_MASK
  181. _ARM_MCR(15, 0, va, 7, 10, 1);
  182. // increment addres to next line and decrement lenght
  183. addr = (const void *) ((uint32_t)addr + line_size);
  184. } while (addr < end_addr);
  185. // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
  186. _ARM_DSB();
  187. }
  188. int arm_icache_state_query()
  189. {
  190. uint32_t sctlr; // System Control Register
  191. // read sctlr
  192. _ARM_MRC(15, 0, sctlr, 1, 0, 0);
  193. if (sctlr & BM_SCTLR_I)
  194. {
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. void arm_icache_enable()
  200. {
  201. uint32_t sctlr ;// System Control Register
  202. // read sctlr
  203. _ARM_MRC(15, 0, sctlr, 1, 0, 0);
  204. // ignore the operation if I is enabled already
  205. if(!(sctlr & BM_SCTLR_I))
  206. {
  207. // set I bit (instruction caching enable)
  208. sctlr |= BM_SCTLR_I;
  209. // write modified sctlr
  210. _ARM_MCR(15, 0, sctlr, 1, 0, 0);
  211. // synchronize context on this processor
  212. _ARM_ISB();
  213. }
  214. }
  215. void arm_icache_disable()
  216. {
  217. uint32_t sctlr ;// System Control Register
  218. // read sctlr
  219. _ARM_MRC(15, 0, sctlr, 1, 0, 0);
  220. // Clear I bit (instruction caching enable)
  221. sctlr &= ~BM_SCTLR_I;
  222. // write modified sctlr
  223. _ARM_MCR(15, 0, sctlr, 1, 0, 0);
  224. // synchronize context on this processor
  225. _ARM_ISB();
  226. }
  227. void arm_icache_invalidate()
  228. {
  229. uint32_t SBZ = 0x0u;
  230. _ARM_MCR(15, 0, SBZ, 7, 5, 0);
  231. // synchronize context on this processor
  232. _ARM_ISB();
  233. }
  234. void arm_icache_invalidate_is()
  235. {
  236. uint32_t SBZ = 0x0u;
  237. _ARM_MCR(15, 0, SBZ, 7, 1, 0);
  238. // synchronize context on this processor
  239. _ARM_ISB();
  240. }
  241. void arm_icache_invalidate_line(const void * addr)
  242. {
  243. uint32_t csidr = 0, line_size = 0;
  244. uint32_t va;
  245. // get the cache line size
  246. _ARM_MRC(15, 1, csidr, 0, 0, 0);
  247. line_size = 1 << ((csidr & 0x7) + 4);
  248. va = (uint32_t) addr & (~(line_size - 1)); //addr & va_VIRTUAL_ADDRESS_MASK
  249. // Invalidate instruction cache by va to PoU (Point of unification).
  250. _ARM_MCR(15, 0, va, 7, 5, 1);
  251. // synchronize context on this processor
  252. _ARM_ISB();
  253. }
  254. void arm_icache_invalidate_mlines(const void * addr, size_t length)
  255. {
  256. uint32_t va;
  257. uint32_t csidr = 0, line_size = 0;
  258. const void * end_addr = (const void *)((uint32_t)addr + length);
  259. // get the cache line size
  260. _ARM_MRC(15, 1, csidr, 0, 0, 0);
  261. line_size = 1 << ((csidr & 0x7) + 4);
  262. do
  263. {
  264. // Clean data cache line to PoC (Point of Coherence) by va.
  265. va = (uint32_t) ((uint32_t)addr & (~(line_size - 1))); //addr & va_VIRTUAL_ADDRESS_MASK
  266. _ARM_MCR(15, 0, va, 7, 5, 1);
  267. // increment addres to next line and decrement lenght
  268. addr = (const void *) ((uint32_t)addr + line_size);
  269. } while (addr < end_addr);
  270. // synchronize context on this processor
  271. _ARM_ISB();
  272. }
  273. ////////////////////////////////////////////////////////////////////////////////
  274. // EOF
  275. ////////////////////////////////////////////////////////////////////////////////