fsl_cache.h 19 KB

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