fsl_cache.h 17 KB

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