fsl_cache.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright 2016-2021 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef _FSL_CACHE_H_
  8. #define _FSL_CACHE_H_
  9. #include "fsl_common.h"
  10. /*!
  11. * @addtogroup cache_lmem
  12. * @{
  13. */
  14. /*******************************************************************************
  15. * Definitions
  16. ******************************************************************************/
  17. /*! @name Driver version */
  18. /*@{*/
  19. /*! @brief cache driver version. */
  20. #define FSL_CACHE_DRIVER_VERSION (MAKE_VERSION(2, 0, 6))
  21. /*@}*/
  22. /*! @brief code bus cache line size is equal to system bus line size, so the unified I/D cache line size equals too. */
  23. #define L1CODEBUSCACHE_LINESIZE_BYTE \
  24. FSL_FEATURE_L1ICACHE_LINESIZE_BYTE /*!< The code bus CACHE line size is 16B = 128b. */
  25. #define L1SYSTEMBUSCACHE_LINESIZE_BYTE \
  26. L1CODEBUSCACHE_LINESIZE_BYTE /*!< The system bus CACHE line size is 16B = 128b. */
  27. /*******************************************************************************
  28. * API
  29. ******************************************************************************/
  30. #if defined(__cplusplus)
  31. extern "C" {
  32. #endif
  33. #if (FSL_FEATURE_SOC_LMEM_COUNT == 1)
  34. /*!
  35. * @name cache control for L1 cache (local memory controller for code/system bus cache)
  36. *@{
  37. */
  38. /*!
  39. * @brief Enables the processor code bus cache.
  40. *
  41. */
  42. void L1CACHE_EnableCodeCache(void);
  43. /*!
  44. * @brief Disables the processor code bus cache.
  45. *
  46. */
  47. void L1CACHE_DisableCodeCache(void);
  48. /*!
  49. * @brief Invalidates the processor code bus cache.
  50. *
  51. */
  52. void L1CACHE_InvalidateCodeCache(void);
  53. /*!
  54. * @brief Invalidates processor code bus cache by range.
  55. *
  56. * @param address The physical address of cache.
  57. * @param size_byte size of the memory to be invalidated.
  58. * @note Address and size should be aligned to "L1CODCACHE_LINESIZE_BYTE".
  59. * The startAddr here will be forced to align to L1CODEBUSCACHE_LINESIZE_BYTE if
  60. * startAddr is not aligned. For the size_byte, application should make sure the
  61. * alignment or make sure the right operation order if the size_byte is not aligned.
  62. */
  63. void L1CACHE_InvalidateCodeCacheByRange(uint32_t address, uint32_t size_byte);
  64. /*!
  65. * @brief Cleans the processor code bus cache.
  66. *
  67. */
  68. void L1CACHE_CleanCodeCache(void);
  69. /*!
  70. * @brief Cleans processor code bus cache by range.
  71. *
  72. * @param address The physical address of cache.
  73. * @param size_byte size of the memory to be cleaned.
  74. * @note Address and size should be aligned to "L1CODEBUSCACHE_LINESIZE_BYTE".
  75. * The startAddr here will be forced to align to L1CODEBUSCACHE_LINESIZE_BYTE if
  76. * startAddr is not aligned. For the size_byte, application should make sure the
  77. * alignment or make sure the right operation order if the size_byte is not aligned.
  78. */
  79. void L1CACHE_CleanCodeCacheByRange(uint32_t address, uint32_t size_byte);
  80. /*!
  81. * @brief Cleans and invalidates the processor code bus cache.
  82. *
  83. */
  84. void L1CACHE_CleanInvalidateCodeCache(void);
  85. /*!
  86. * @brief Cleans and invalidate processor code bus cache by range.
  87. *
  88. * @param address The physical address of cache.
  89. * @param size_byte size of the memory to be Cleaned and Invalidated.
  90. * @note Address and size should be aligned to "L1CODEBUSCACHE_LINESIZE_BYTE".
  91. * The startAddr here will be forced to align to L1CODEBUSCACHE_LINESIZE_BYTE if
  92. * startAddr is not aligned. For the size_byte, application should make sure the
  93. * alignment or make sure the right operation order if the size_byte is not aligned.
  94. */
  95. void L1CACHE_CleanInvalidateCodeCacheByRange(uint32_t address, uint32_t size_byte);
  96. /*!
  97. * @brief Enables/disables the processor code bus write buffer.
  98. *
  99. * @param enable The enable or disable flag.
  100. * true - enable the code bus write buffer.
  101. * false - disable the code bus write buffer.
  102. */
  103. static inline void L1CACHE_EnableCodeCacheWriteBuffer(bool enable)
  104. {
  105. if (enable)
  106. {
  107. LMEM->PCCCR |= LMEM_PCCCR_ENWRBUF_MASK;
  108. }
  109. else
  110. {
  111. LMEM->PCCCR &= ~LMEM_PCCCR_ENWRBUF_MASK;
  112. }
  113. }
  114. #if defined(FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE) && FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE
  115. /*!
  116. * @brief Enables the processor system bus cache.
  117. *
  118. */
  119. void L1CACHE_EnableSystemCache(void);
  120. /*!
  121. * @brief Disables the processor system bus cache.
  122. *
  123. */
  124. void L1CACHE_DisableSystemCache(void);
  125. /*!
  126. * @brief Invalidates the processor system bus cache.
  127. *
  128. */
  129. void L1CACHE_InvalidateSystemCache(void);
  130. /*!
  131. * @brief Invalidates processor system bus cache by range.
  132. *
  133. * @param address The physical address of cache.
  134. * @param size_byte size of the memory to be invalidated.
  135. * @note Address and size should be aligned to "L1SYSTEMBUSCACHE_LINESIZE_BYTE".
  136. * The startAddr here will be forced to align to L1SYSTEMBUSCACHE_LINESIZE_BYTE if
  137. * startAddr is not aligned. For the size_byte, application should make sure the
  138. * alignment or make sure the right operation order if the size_byte is not aligned.
  139. */
  140. void L1CACHE_InvalidateSystemCacheByRange(uint32_t address, uint32_t size_byte);
  141. /*!
  142. * @brief Cleans the processor system bus cache.
  143. *
  144. */
  145. void L1CACHE_CleanSystemCache(void);
  146. /*!
  147. * @brief Cleans processor system bus cache by range.
  148. *
  149. * @param address The physical address of cache.
  150. * @param size_byte size of the memory to be cleaned.
  151. * @note Address and size should be aligned to "L1SYSTEMBUSCACHE_LINESIZE_BYTE".
  152. * The startAddr here will be forced to align to L1SYSTEMBUSCACHE_LINESIZE_BYTE if
  153. * startAddr is not aligned. For the size_byte, application should make sure the
  154. * alignment or make sure the right operation order if the size_byte is not aligned.
  155. */
  156. void L1CACHE_CleanSystemCacheByRange(uint32_t address, uint32_t size_byte);
  157. /*!
  158. * @brief Cleans and invalidates the processor system bus cache.
  159. *
  160. */
  161. void L1CACHE_CleanInvalidateSystemCache(void);
  162. /*!
  163. * @brief Cleans and Invalidates processor system bus cache by range.
  164. *
  165. * @param address The physical address of cache.
  166. * @param size_byte size of the memory to be Clean and Invalidated.
  167. * @note Address and size should be aligned to "L1SYSTEMBUSCACHE_LINESIZE_BYTE".
  168. * The startAddr here will be forced to align to L1SYSTEMBUSCACHE_LINESIZE_BYTE if
  169. * startAddr is not aligned. For the size_byte, application should make sure the
  170. * alignment or make sure the right operation order if the size_byte is not aligned.
  171. */
  172. void L1CACHE_CleanInvalidateSystemCacheByRange(uint32_t address, uint32_t size_byte);
  173. /*!
  174. * @brief Enables/disables the processor system bus write buffer.
  175. *
  176. * @param enable The enable or disable flag.
  177. * true - enable the code bus write buffer.
  178. * false - disable the code bus write buffer.
  179. */
  180. static inline void L1CACHE_EnableSystemCacheWriteBuffer(bool enable)
  181. {
  182. if (enable)
  183. {
  184. LMEM->PSCCR |= LMEM_PSCCR_ENWRBUF_MASK;
  185. }
  186. else
  187. {
  188. LMEM->PSCCR &= ~LMEM_PSCCR_ENWRBUF_MASK;
  189. }
  190. }
  191. /*@}*/
  192. #endif /* FSL_FEATURE_LMEM_HAS_SYSTEMBUS_CACHE */
  193. /*!
  194. * @name cache control for unified L1 cache driver
  195. *@{
  196. */
  197. /*!
  198. * @brief Invalidates cortex-m4 L1 instrument cache by range.
  199. *
  200. * @param address The start address of the memory to be invalidated.
  201. * @param size_byte The memory size.
  202. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE) aligned.
  203. */
  204. void L1CACHE_InvalidateICacheByRange(uint32_t address, uint32_t size_byte);
  205. /*!
  206. * @brief Invalidates cortex-m4 L1 data cache by range.
  207. *
  208. * @param address The start address of the memory to be invalidated.
  209. * @param size_byte The memory size.
  210. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  211. */
  212. static inline void L1CACHE_InvalidateDCacheByRange(uint32_t address, uint32_t size_byte)
  213. {
  214. L1CACHE_InvalidateICacheByRange(address, size_byte);
  215. }
  216. /*!
  217. * @brief Cleans cortex-m4 L1 data cache by range.
  218. *
  219. * @param address The start address of the memory to be cleaned.
  220. * @param size_byte The memory size.
  221. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  222. */
  223. void L1CACHE_CleanDCacheByRange(uint32_t address, uint32_t size_byte);
  224. /*!
  225. * @brief Cleans and Invalidates cortex-m4 L1 data cache by range.
  226. *
  227. * @param address The start address of the memory to be clean and invalidated.
  228. * @param size_byte The memory size.
  229. * @note The start address and size_byte should be 16-Byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  230. */
  231. void L1CACHE_CleanInvalidateDCacheByRange(uint32_t address, uint32_t size_byte);
  232. /*@}*/
  233. #endif /* FSL_FEATURE_SOC_LMEM_COUNT == 1 */
  234. /*!
  235. * @name Unified Cache Control for all caches
  236. *@{
  237. */
  238. /*!
  239. * @brief Invalidates instruction cache by range.
  240. *
  241. * @param address The physical address.
  242. * @param size_byte size of the memory to be invalidated.
  243. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  244. * FSL_FEATURE_L1ICACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  245. * size if startAddr is not aligned. For the size_byte, application should make sure the
  246. * alignment or make sure the right operation order if the size_byte is not aligned.
  247. */
  248. static inline void ICACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  249. {
  250. L1CACHE_InvalidateICacheByRange(address, size_byte);
  251. }
  252. /*!
  253. * @brief Invalidates data cache by range.
  254. *
  255. * @param address The physical address.
  256. * @param size_byte size of the memory to be invalidated.
  257. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  258. * FSL_FEATURE_L1DCACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  259. * size if startAddr is not aligned. For the size_byte, application should make sure the
  260. * alignment or make sure the right operation order if the size_byte is not aligned.
  261. */
  262. static inline void DCACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  263. {
  264. L1CACHE_InvalidateDCacheByRange(address, size_byte);
  265. }
  266. /*!
  267. * @brief Clean data cache by range.
  268. *
  269. * @param address The physical address.
  270. * @param size_byte size of the memory to be cleaned.
  271. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  272. * FSL_FEATURE_L1DCACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  273. * size if startAddr is not aligned. For the size_byte, application should make sure the
  274. * alignment or make sure the right operation order if the size_byte is not aligned.
  275. */
  276. static inline void DCACHE_CleanByRange(uint32_t address, uint32_t size_byte)
  277. {
  278. L1CACHE_CleanDCacheByRange(address, size_byte);
  279. }
  280. /*!
  281. * @brief Cleans and Invalidates data cache by range.
  282. *
  283. * @param address The physical address.
  284. * @param size_byte size of the memory to be Cleaned and Invalidated.
  285. * @note Address and size should be aligned to 16-Byte due to the cache operation unit
  286. * FSL_FEATURE_L1DCACHE_LINESIZE_BYTE. The startAddr here will be forced to align to the cache line
  287. * size if startAddr is not aligned. For the size_byte, application should make sure the
  288. * alignment or make sure the right operation order if the size_byte is not aligned.
  289. */
  290. static inline void DCACHE_CleanInvalidateByRange(uint32_t address, uint32_t size_byte)
  291. {
  292. L1CACHE_CleanInvalidateDCacheByRange(address, size_byte);
  293. }
  294. /*@}*/
  295. #if defined(__cplusplus)
  296. }
  297. #endif
  298. /*! @}*/
  299. #endif /* _FSL_CACHE_H_*/