fsl_cache.h 18 KB

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