fsl_cache.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_armv7_m7
  12. * @{
  13. */
  14. /*******************************************************************************
  15. * Definitions
  16. ******************************************************************************/
  17. /*! @name Driver version */
  18. /*@{*/
  19. /*! @brief cache driver version 2.0.4. */
  20. #define FSL_CACHE_DRIVER_VERSION (MAKE_VERSION(2, 0, 4))
  21. /*@}*/
  22. #if defined(FSL_FEATURE_SOC_L2CACHEC_COUNT) && FSL_FEATURE_SOC_L2CACHEC_COUNT
  23. #ifndef FSL_SDK_DISBLE_L2CACHE_PRESENT
  24. #define FSL_SDK_DISBLE_L2CACHE_PRESENT 0
  25. #endif
  26. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT */
  27. /*******************************************************************************
  28. * Definitions
  29. ******************************************************************************/
  30. #if defined(FSL_FEATURE_SOC_L2CACHEC_COUNT) && FSL_FEATURE_SOC_L2CACHEC_COUNT
  31. /*! @brief Number of level 2 cache controller ways. */
  32. typedef enum _l2cache_way_num
  33. {
  34. kL2CACHE_8ways = 0, /*!< 8 ways. */
  35. #if defined(FSL_FEATURE_L2CACHE_SUPPORT_16_WAY_ASSOCIATIVITY) && FSL_FEATURE_L2CACHE_SUPPORT_16_WAY_ASSOCIATIVITY
  36. kL2CACHE_16ways /*!< 16 ways. */
  37. #endif /* FSL_FEATURE_L2CACHE_SUPPORT_16_WAY_ASSOCIATIVITY */
  38. } l2cache_way_num_t;
  39. /*! @brief Level 2 cache controller way size. */
  40. typedef enum _l2cache_way_size
  41. {
  42. kL2CACHE_16KBSize = 1, /*!< 16 KB way size. */
  43. kL2CACHE_32KBSize = 2, /*!< 32 KB way size. */
  44. kL2CACHE_64KBSize = 3, /*!< 64 KB way size. */
  45. kL2CACHE_128KBSize = 4, /*!< 128 KB way size. */
  46. kL2CACHE_256KBSize = 5, /*!< 256 KB way size. */
  47. kL2CACHE_512KBSize = 6 /*!< 512 KB way size. */
  48. } l2cache_way_size;
  49. /*! @brief Level 2 cache controller replacement policy. */
  50. typedef enum _l2cache_replacement
  51. {
  52. kL2CACHE_Pseudorandom = 0U, /*!< Peseudo-random replacement policy using an lfsr. */
  53. kL2CACHE_Roundrobin /*!< Round-robin replacemnt policy. */
  54. } l2cache_replacement_t;
  55. /*! @brief Level 2 cache controller force write allocate options. */
  56. typedef enum _l2cache_writealloc
  57. {
  58. kL2CACHE_UseAwcache = 0, /*!< Use AWCAHE attribute for the write allocate. */
  59. kL2CACHE_NoWriteallocate, /*!< Force no write allocate. */
  60. kL2CACHE_forceWriteallocate /*!< Force write allocate when write misses. */
  61. } l2cache_writealloc_t;
  62. /*! @brief Level 2 cache controller tag/data ram latency. */
  63. typedef enum _l2cache_latency
  64. {
  65. kL2CACHE_1CycleLate = 0, /*!< 1 cycle of latency. */
  66. kL2CACHE_2CycleLate, /*!< 2 cycle of latency. */
  67. kL2CACHE_3CycleLate, /*!< 3 cycle of latency. */
  68. kL2CACHE_4CycleLate, /*!< 4 cycle of latency. */
  69. kL2CACHE_5CycleLate, /*!< 5 cycle of latency. */
  70. kL2CACHE_6CycleLate, /*!< 6 cycle of latency. */
  71. kL2CACHE_7CycleLate, /*!< 7 cycle of latency. */
  72. kL2CACHE_8CycleLate /*!< 8 cycle of latency. */
  73. } l2cache_latency_t;
  74. /*! @brief Level 2 cache controller tag/data ram latency configure structure. */
  75. typedef struct _l2cache_latency_config
  76. {
  77. l2cache_latency_t tagWriteLate; /*!< Tag write latency. */
  78. l2cache_latency_t tagReadLate; /*!< Tag Read latency. */
  79. l2cache_latency_t tagSetupLate; /*!< Tag setup latency. */
  80. l2cache_latency_t dataWriteLate; /*!< Data write latency. */
  81. l2cache_latency_t dataReadLate; /*!< Data Read latency. */
  82. l2cache_latency_t dataSetupLate; /*!< Data setup latency. */
  83. } L2cache_latency_config_t;
  84. /*! @brief Level 2 cache controller configure structure. */
  85. typedef struct _l2cache_config
  86. {
  87. /* ------------------------ l2 cachec basic settings ---------------------------- */
  88. l2cache_way_num_t wayNum; /*!< The number of ways. */
  89. l2cache_way_size waySize; /*!< The way size = Cache Ram size / wayNum. */
  90. l2cache_replacement_t repacePolicy; /*!< Replacemnet policy. */
  91. /* ------------------------ tag/data ram latency settings ----------------------- */
  92. L2cache_latency_config_t *lateConfig; /*!< Tag/data latency configure. Set NUll if not required. */
  93. /* ------------------------ Prefetch enable settings ---------------------------- */
  94. bool istrPrefetchEnable; /*!< Instruction prefetch enable. */
  95. bool dataPrefetchEnable; /*!< Data prefetch enable. */
  96. /* ------------------------ Non-secure access settings -------------------------- */
  97. bool nsLockdownEnable; /*!< None-secure lockdown enable. */
  98. /* ------------------------ other settings -------------------------------------- */
  99. l2cache_writealloc_t writeAlloc; /*!< Write allcoate force option. */
  100. } l2cache_config_t;
  101. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT */
  102. /*******************************************************************************
  103. * API
  104. ******************************************************************************/
  105. #if defined(__cplusplus)
  106. extern "C" {
  107. #endif
  108. /*!
  109. * @name Control for cortex-m7 L1 cache
  110. *@{
  111. */
  112. /*!
  113. * @brief Enables cortex-m7 L1 instruction cache.
  114. *
  115. */
  116. static inline void L1CACHE_EnableICache(void)
  117. {
  118. SCB_EnableICache();
  119. }
  120. /*!
  121. * @brief Disables cortex-m7 L1 instruction cache.
  122. *
  123. */
  124. static inline void L1CACHE_DisableICache(void)
  125. {
  126. SCB_DisableICache();
  127. }
  128. /*!
  129. * @brief Invalidate cortex-m7 L1 instruction cache.
  130. *
  131. */
  132. static inline void L1CACHE_InvalidateICache(void)
  133. {
  134. SCB_InvalidateICache();
  135. }
  136. /*!
  137. * @brief Invalidate cortex-m7 L1 instruction cache by range.
  138. *
  139. * @param address The start address of the memory to be invalidated.
  140. * @param size_byte The memory size.
  141. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE) aligned.
  142. * The startAddr here will be forced to align to L1 I-cache line size if
  143. * startAddr is not aligned. For the size_byte, application should make sure the
  144. * alignment or make sure the right operation order if the size_byte is not aligned.
  145. */
  146. void L1CACHE_InvalidateICacheByRange(uint32_t address, uint32_t size_byte);
  147. /*!
  148. * @brief Enables cortex-m7 L1 data cache.
  149. *
  150. */
  151. static inline void L1CACHE_EnableDCache(void)
  152. {
  153. SCB_EnableDCache();
  154. }
  155. /*!
  156. * @brief Disables cortex-m7 L1 data cache.
  157. *
  158. */
  159. static inline void L1CACHE_DisableDCache(void)
  160. {
  161. SCB_DisableDCache();
  162. }
  163. /*!
  164. * @brief Invalidates cortex-m7 L1 data cache.
  165. *
  166. */
  167. static inline void L1CACHE_InvalidateDCache(void)
  168. {
  169. SCB_InvalidateDCache();
  170. }
  171. /*!
  172. * @brief Cleans cortex-m7 L1 data cache.
  173. *
  174. */
  175. static inline void L1CACHE_CleanDCache(void)
  176. {
  177. SCB_CleanDCache();
  178. }
  179. /*!
  180. * @brief Cleans and Invalidates cortex-m7 L1 data cache.
  181. *
  182. */
  183. static inline void L1CACHE_CleanInvalidateDCache(void)
  184. {
  185. SCB_CleanInvalidateDCache();
  186. }
  187. /*!
  188. * @brief Invalidates cortex-m7 L1 data cache by range.
  189. *
  190. * @param address The start address of the memory to be invalidated.
  191. * @param size_byte The memory size.
  192. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  193. * The startAddr here will be forced to align to L1 D-cache line size if
  194. * startAddr is not aligned. For the size_byte, application should make sure the
  195. * alignment or make sure the right operation order if the size_byte is not aligned.
  196. */
  197. static inline void L1CACHE_InvalidateDCacheByRange(uint32_t address, uint32_t size_byte)
  198. {
  199. SCB_InvalidateDCache_by_Addr((uint32_t *)address, (int32_t)size_byte);
  200. }
  201. /*!
  202. * @brief Cleans cortex-m7 L1 data cache by range.
  203. *
  204. * @param address The start address of the memory to be cleaned.
  205. * @param size_byte The memory size.
  206. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  207. * The startAddr here will be forced to align to L1 D-cache line size if
  208. * startAddr is not aligned. For the size_byte, application should make sure the
  209. * alignment or make sure the right operation order if the size_byte is not aligned.
  210. */
  211. static inline void L1CACHE_CleanDCacheByRange(uint32_t address, uint32_t size_byte)
  212. {
  213. SCB_CleanDCache_by_Addr((uint32_t *)address, (int32_t)size_byte);
  214. }
  215. /*!
  216. * @brief Cleans and Invalidates cortex-m7 L1 data cache by range.
  217. *
  218. * @param address The start address of the memory to be clean and invalidated.
  219. * @param size_byte The memory size.
  220. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) aligned.
  221. * The startAddr here will be forced to align to L1 D-cache line size if
  222. * startAddr is not aligned. For the size_byte, application should make sure the
  223. * alignment or make sure the right operation order if the size_byte is not aligned.
  224. */
  225. static inline void L1CACHE_CleanInvalidateDCacheByRange(uint32_t address, uint32_t size_byte)
  226. {
  227. SCB_CleanInvalidateDCache_by_Addr((uint32_t *)address, (int32_t)size_byte);
  228. }
  229. /*@}*/
  230. #if defined(FSL_FEATURE_SOC_L2CACHEC_COUNT) && FSL_FEATURE_SOC_L2CACHEC_COUNT
  231. /*!
  232. * @name Control for L2 pl310 cache
  233. *@{
  234. */
  235. /*!
  236. * @brief Initializes the level 2 cache controller module.
  237. *
  238. * @param config Pointer to configuration structure. See "l2cache_config_t".
  239. */
  240. void L2CACHE_Init(l2cache_config_t *config);
  241. /*!
  242. * @brief Gets an available default settings for the cache controller.
  243. *
  244. * This function initializes the cache controller configuration structure with default settings.
  245. * The default values are:
  246. * @code
  247. * config->waysNum = kL2CACHE_8ways;
  248. * config->waySize = kL2CACHE_32KbSize;
  249. * config->repacePolicy = kL2CACHE_Roundrobin;
  250. * config->lateConfig = NULL;
  251. * config->istrPrefetchEnable = false;
  252. * config->dataPrefetchEnable = false;
  253. * config->nsLockdownEnable = false;
  254. * config->writeAlloc = kL2CACHE_UseAwcache;
  255. * @endcode
  256. * @param config Pointer to the configuration structure.
  257. */
  258. void L2CACHE_GetDefaultConfig(l2cache_config_t *config);
  259. /*!
  260. * @brief Enables the level 2 cache controller.
  261. * This function enables the cache controller. Must be written using a secure access.
  262. * If write with a Non-secure access will cause a DECERR response.
  263. *
  264. */
  265. void L2CACHE_Enable(void);
  266. /*!
  267. * @brief Disables the level 2 cache controller.
  268. * This function disables the cache controller. Must be written using a secure access.
  269. * If write with a Non-secure access will cause a DECERR response.
  270. *
  271. */
  272. void L2CACHE_Disable(void);
  273. /*!
  274. * @brief Invalidates the Level 2 cache.
  275. * This function invalidates all entries in cache.
  276. *
  277. */
  278. void L2CACHE_Invalidate(void);
  279. /*!
  280. * @brief Invalidates the Level 2 cache lines in the range of two physical addresses.
  281. * This function invalidates all cache lines between two physical addresses.
  282. *
  283. * @param address The start address of the memory to be invalidated.
  284. * @param size_byte The memory size.
  285. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L2CACHE_LINESIZE_BYTE) aligned.
  286. * The startAddr here will be forced to align to L2 line size if startAddr
  287. * 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. void L2CACHE_InvalidateByRange(uint32_t address, uint32_t size_byte);
  291. /*!
  292. * @brief Cleans the level 2 cache controller.
  293. * This function cleans all entries in the level 2 cache controller.
  294. *
  295. */
  296. void L2CACHE_Clean(void);
  297. /*!
  298. * @brief Cleans the Level 2 cache lines in the range of two physical addresses.
  299. * This function cleans all cache lines between two physical addresses.
  300. *
  301. * @param address The start address of the memory to be cleaned.
  302. * @param size_byte The memory size.
  303. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L2CACHE_LINESIZE_BYTE) aligned.
  304. * The startAddr here will be forced to align to L2 line size if startAddr
  305. * is not aligned. For the size_byte, application should make sure the
  306. * alignment or make sure the right operation order if the size_byte is not aligned.
  307. */
  308. void L2CACHE_CleanByRange(uint32_t address, uint32_t size_byte);
  309. /*!
  310. * @brief Cleans and invalidates the level 2 cache controller.
  311. * This function cleans and invalidates all entries in the level 2 cache controller.
  312. *
  313. */
  314. void L2CACHE_CleanInvalidate(void);
  315. /*!
  316. * @brief Cleans and invalidates the Level 2 cache lines in the range of two physical addresses.
  317. * This function cleans and invalidates all cache lines between two physical addresses.
  318. *
  319. * @param address The start address of the memory to be cleaned and invalidated.
  320. * @param size_byte The memory size.
  321. * @note The start address and size_byte should be 32-byte(FSL_FEATURE_L2CACHE_LINESIZE_BYTE) aligned.
  322. * The startAddr here will be forced to align to L2 line size if startAddr
  323. * is not aligned. For the size_byte, application should make sure the
  324. * alignment or make sure the right operation order if the size_byte is not aligned.
  325. */
  326. void L2CACHE_CleanInvalidateByRange(uint32_t address, uint32_t size_byte);
  327. /*!
  328. * @brief Enables or disables to lock down the data and instruction by way.
  329. * This function locks down the cached instruction/data by way and prevent the adresses from
  330. * being allocated and prevent dara from being evicted out of the level 2 cache.
  331. * But the normal cache maintenance operations that invalidate, clean or clean
  332. * and validate cache contents affect the locked-down cache lines as normal.
  333. *
  334. * @param masterId The master id, range from 0 ~ 7.
  335. * @param mask The ways to be enabled or disabled to lockdown.
  336. * each bit in value is related to each way of the cache. for example:
  337. * value: bit 0 ------ way 0.
  338. * value: bit 1 ------ way 1.
  339. * --------------------------
  340. * value: bit 15 ------ way 15.
  341. * Note: please make sure the value setting is align with your supported ways.
  342. * @param enable True enable the lockdown, false to disable the lockdown.
  343. */
  344. void L2CACHE_LockdownByWayEnable(uint32_t masterId, uint32_t mask, bool enable);
  345. /*@}*/
  346. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT */
  347. /*!
  348. * @name Unified Cache Control for all caches (cortex-m7 L1 cache + l2 pl310)
  349. * Mainly used for many drivers for easy cache operation.
  350. *@{
  351. */
  352. /*!
  353. * @brief Invalidates all instruction caches by range.
  354. *
  355. * Both cortex-m7 L1 cache line and L2 PL310 cache line length is 32-byte.
  356. *
  357. * @param address The physical address.
  358. * @param size_byte size of the memory to be invalidated.
  359. * @note address and size should be aligned to cache line size
  360. * 32-Byte due to the cache operation unit is one cache line. The startAddr here will be forced
  361. * to align to the cache line size if startAddr is not aligned. For the size_byte, application should
  362. * make sure the alignment or make sure the right operation order if the size_byte is not aligned.
  363. */
  364. void ICACHE_InvalidateByRange(uint32_t address, uint32_t size_byte);
  365. /*!
  366. * @brief Invalidates all data caches by range.
  367. *
  368. * Both cortex-m7 L1 cache line and L2 PL310 cache line length is 32-byte.
  369. *
  370. * @param address The physical address.
  371. * @param size_byte size of the memory to be invalidated.
  372. * @note address and size should be aligned to cache line size
  373. * 32-Byte due to the cache operation unit is one cache line. The startAddr here will be forced
  374. * to align to the cache line size if startAddr is not aligned. For the size_byte, application should
  375. * make sure the alignment or make sure the right operation order if the size_byte is not aligned.
  376. */
  377. void DCACHE_InvalidateByRange(uint32_t address, uint32_t size_byte);
  378. /*!
  379. * @brief Cleans all data caches by range.
  380. *
  381. * Both cortex-m7 L1 cache line and L2 PL310 cache line length is 32-byte.
  382. *
  383. * @param address The physical address.
  384. * @param size_byte size of the memory to be cleaned.
  385. * @note address and size should be aligned to cache line size
  386. * 32-Byte due to the cache operation unit is one cache line. The startAddr here will be forced
  387. * to align to the cache line size if startAddr is not aligned. For the size_byte, application should
  388. * make sure the alignment or make sure the right operation order if the size_byte is not aligned.
  389. */
  390. void DCACHE_CleanByRange(uint32_t address, uint32_t size_byte);
  391. /*!
  392. * @brief Cleans and Invalidates all data caches by range.
  393. *
  394. * Both cortex-m7 L1 cache line and L2 PL310 cache line length is 32-byte.
  395. *
  396. * @param address The physical address.
  397. * @param size_byte size of the memory to be cleaned and invalidated.
  398. * @note address and size should be aligned to cache line size
  399. * 32-Byte due to the cache operation unit is one cache line. The startAddr here will be forced
  400. * to align to the cache line size if startAddr is not aligned. For the size_byte, application should
  401. * make sure the alignment or make sure the right operation order if the size_byte is not aligned.
  402. */
  403. void DCACHE_CleanInvalidateByRange(uint32_t address, uint32_t size_byte);
  404. /*@}*/
  405. #if defined(__cplusplus)
  406. }
  407. #endif
  408. /*! @}*/
  409. #endif /* _FSL_CACHE_H_*/