fsl_cache.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #include "fsl_cache.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. #if (FSL_FEATURE_SOC_L2CACHEC_COUNT > 0)
  35. #define L2CACHE_OPERATION_TIMEOUT 0xFFFFFU
  36. #define L2CACHE_8WAYS_MASK 0xFFU
  37. #define L2CACHE_16WAYS_MASK 0xFFFFU
  38. #define L2CACHE_SMALLWAYS_NUM 8U
  39. #define L2CACHE_1KBCOVERTOB 1024U
  40. #define L2CACHE_SAMLLWAYS_SIZE 16U
  41. #define L2CACHE_LOCKDOWN_REGNUM 8 /*!< Lock down register numbers.*/
  42. /*******************************************************************************
  43. * Prototypes
  44. ******************************************************************************/
  45. /*!
  46. * @brief Set for all ways and waiting for the operation finished.
  47. * This is provided for all the background operations.
  48. *
  49. * @param auxCtlReg The auxiliary control register.
  50. * @param regAddr The register address to be operated.
  51. */
  52. static void L2CACHE_SetAndWaitBackGroundOperate(uint32_t auxCtlReg, uint32_t regAddr);
  53. /*!
  54. * @brief Invalidates the Level 2 cache line by physical address.
  55. * This function invalidates a cache line by physcial address.
  56. *
  57. * @param address The physical addderss of the cache.
  58. * The format of the address shall be :
  59. * bit 31 ~ bit n+1 | bitn ~ bit5 | bit4 ~ bit0
  60. * Tag | index | 0
  61. * Note: the physical address shall be aligned to the line size - 32B (256 bit).
  62. * so keep the last 5 bits (bit 4 ~ bit 0) of the physical address always be zero.
  63. * If the input address is not aligned, it will be changed to 32-byte aligned address.
  64. * The n is varies according to the index width.
  65. * @return The actual 32-byte aligned physical address be operated.
  66. */
  67. static uint32_t L2CACHE_InvalidateLineByAddr(uint32_t address);
  68. /*!
  69. * @brief Cleans the Level 2 cache line based on the physical address.
  70. * This function cleans a cache line based on a physcial address.
  71. *
  72. * @param address The physical addderss of the cache.
  73. * The format of the address shall be :
  74. * bit 31 ~ bit n+1 | bitn ~ bit5 | bit4 ~ bit0
  75. * Tag | index | 0
  76. * Note: the physical address shall be aligned to the line size - 32B (256 bit).
  77. * so keep the last 5 bits (bit 4 ~ bit 0) of the physical address always be zero.
  78. * If the input address is not aligned, it will be changed to 32-byte aligned address.
  79. * The n is varies according to the index width.
  80. * @return The actual 32-byte aligned physical address be operated.
  81. */
  82. static uint32_t L2CACHE_CleanLineByAddr(uint32_t address);
  83. /*!
  84. * @brief Cleans and invalidates the Level 2 cache line based on the physical address.
  85. * This function cleans and invalidates a cache line based on a physcial address.
  86. *
  87. * @param address The physical addderss of the cache.
  88. * The format of the address shall be :
  89. * bit 31 ~ bit n+1 | bitn ~ bit5 | bit4 ~ bit0
  90. * Tag | index | 0
  91. * Note: the physical address shall be aligned to the line size - 32B (256 bit).
  92. * so keep the last 5 bits (bit 4 ~ bit 0) of the physical address always be zero.
  93. * If the input address is not aligned, it will be changed to 32-byte aligned address.
  94. * The n is varies according to the index width.
  95. * @return The actual 32-byte aligned physical address be operated.
  96. */
  97. static uint32_t L2CACHE_CleanInvalidateLineByAddr(uint32_t address);
  98. /*!
  99. * @brief Gets the number of the Level 2 cache and the way size.
  100. * This function cleans and invalidates a cache line based on a physcial address.
  101. *
  102. * @param num_ways The number of the cache way.
  103. * @param size_way The way size.
  104. */
  105. static void L2CACHE_GetWayNumSize(uint32_t *num_ways, uint32_t *size_way);
  106. /*******************************************************************************
  107. * Code
  108. ******************************************************************************/
  109. static void L2CACHE_SetAndWaitBackGroundOperate(uint32_t auxCtlReg, uint32_t regAddr)
  110. {
  111. uint16_t mask = L2CACHE_8WAYS_MASK;
  112. uint32_t timeout = L2CACHE_OPERATION_TIMEOUT;
  113. /* Check the ways used at first. */
  114. if (auxCtlReg & L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_MASK)
  115. {
  116. mask = L2CACHE_16WAYS_MASK;
  117. }
  118. /* Set the opeartion for all ways/entries of the cache. */
  119. *(uint32_t *)regAddr = mask;
  120. /* Waiting for until the operation is complete. */
  121. while ((*(uint32_t *)regAddr & mask) && timeout)
  122. {
  123. __ASM("nop");
  124. timeout--;
  125. }
  126. }
  127. static uint32_t L2CACHE_InvalidateLineByAddr(uint32_t address)
  128. {
  129. /* Align the address first. */
  130. address &= ~(uint32_t)(FSL_FEATURE_L2CACHE_LINESIZE_BYTE - 1);
  131. /* Invalidate the cache line by physical address. */
  132. L2CACHEC->REG7_INV_PA = address;
  133. return address;
  134. }
  135. static uint32_t L2CACHE_CleanLineByAddr(uint32_t address)
  136. {
  137. /* Align the address first. */
  138. address &= ~(uint32_t)(FSL_FEATURE_L2CACHE_LINESIZE_BYTE - 1);
  139. /* Invalidate the cache line by physical address. */
  140. L2CACHEC->REG7_CLEAN_PA = address;
  141. return address;
  142. }
  143. static uint32_t L2CACHE_CleanInvalidateLineByAddr(uint32_t address)
  144. {
  145. /* Align the address first. */
  146. address &= ~(uint32_t)(FSL_FEATURE_L2CACHE_LINESIZE_BYTE - 1);
  147. /* Clean and invalidate the cache line by physical address. */
  148. L2CACHEC->REG7_CLEAN_INV_PA = address;
  149. return address;
  150. }
  151. static void L2CACHE_GetWayNumSize(uint32_t *num_ways, uint32_t *size_way)
  152. {
  153. assert(num_ways);
  154. assert(size_way);
  155. uint32_t number = (L2CACHEC->REG1_AUX_CONTROL & L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_MASK) >>
  156. L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_SHIFT;
  157. uint32_t size = (L2CACHEC->REG1_AUX_CONTROL & L2CACHEC_REG1_AUX_CONTROL_WAYSIZE_MASK) >>
  158. L2CACHEC_REG1_AUX_CONTROL_WAYSIZE_SHIFT;
  159. *num_ways = (number + 1) * L2CACHE_SMALLWAYS_NUM;
  160. if (!size)
  161. {
  162. /* 0 internally mapped to the same size as 1 - 16KB.*/
  163. size += 1;
  164. }
  165. *size_way = (1 << (size - 1)) * L2CACHE_SAMLLWAYS_SIZE * L2CACHE_1KBCOVERTOB;
  166. }
  167. void L2CACHE_Init(l2cache_config_t *config)
  168. {
  169. assert (config);
  170. uint16_t waysNum = 0xFFU; /* Default use the 8-way mask. */
  171. uint8_t count;
  172. uint32_t auxReg = 0;
  173. /*The aux register must be configured when the cachec is disabled
  174. * So disable first if the cache controller is enabled.
  175. */
  176. if (L2CACHEC->REG1_CONTROL & L2CACHEC_REG1_CONTROL_CE_MASK)
  177. {
  178. L2CACHE_Disable();
  179. }
  180. /* Unlock all entries. */
  181. if (L2CACHEC->REG1_AUX_CONTROL & L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_MASK)
  182. {
  183. waysNum = 0xFFFFU;
  184. }
  185. for (count = 0; count < L2CACHE_LOCKDOWN_REGNUM; count ++)
  186. {
  187. L2CACHE_LockdownByWayEnable(count, waysNum, false);
  188. }
  189. /* Set the ways and way-size etc. */
  190. auxReg = L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY(config->wayNum) |
  191. L2CACHEC_REG1_AUX_CONTROL_WAYSIZE(config->waySize) |
  192. L2CACHEC_REG1_AUX_CONTROL_CRP(config->repacePolicy) |
  193. L2CACHEC_REG1_AUX_CONTROL_IPE(config->istrPrefetchEnable) |
  194. L2CACHEC_REG1_AUX_CONTROL_DPE(config->dataPrefetchEnable) |
  195. L2CACHEC_REG1_AUX_CONTROL_NLE(config->nsLockdownEnable) |
  196. L2CACHEC_REG1_AUX_CONTROL_FWA(config->writeAlloc) |
  197. L2CACHEC_REG1_AUX_CONTROL_HPSDRE(config->writeAlloc);
  198. L2CACHEC->REG1_AUX_CONTROL = auxReg;
  199. /* Set the tag/data ram latency. */
  200. if (config->lateConfig)
  201. {
  202. uint32_t data = 0;
  203. /* Tag latency. */
  204. data = L2CACHEC_REG1_TAG_RAM_CONTROL_SL(config->lateConfig->tagSetupLate)|
  205. L2CACHEC_REG1_TAG_RAM_CONTROL_SL(config->lateConfig->tagSetupLate)|
  206. L2CACHEC_REG1_TAG_RAM_CONTROL_RAL(config->lateConfig->tagReadLate)|
  207. L2CACHEC_REG1_TAG_RAM_CONTROL_WAL(config->lateConfig->dataWriteLate);
  208. L2CACHEC->REG1_TAG_RAM_CONTROL = data;
  209. /* Data latency. */
  210. data = L2CACHEC_REG1_DATA_RAM_CONTROL_SL(config->lateConfig->dataSetupLate)|
  211. L2CACHEC_REG1_DATA_RAM_CONTROL_SL(config->lateConfig->dataSetupLate)|
  212. L2CACHEC_REG1_DATA_RAM_CONTROL_RAL(config->lateConfig->dataReadLate)|
  213. L2CACHEC_REG1_DATA_RAM_CONTROL_WAL(config->lateConfig->dataWriteLate);
  214. L2CACHEC->REG1_DATA_RAM_CONTROL = data;
  215. }
  216. }
  217. void L2CACHE_GetDefaultConfig(l2cache_config_t *config)
  218. {
  219. assert(config);
  220. uint32_t number = (L2CACHEC->REG1_AUX_CONTROL & L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_MASK) >>
  221. L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_SHIFT;
  222. uint32_t size = (L2CACHEC->REG1_AUX_CONTROL & L2CACHEC_REG1_AUX_CONTROL_WAYSIZE_MASK) >>
  223. L2CACHEC_REG1_AUX_CONTROL_WAYSIZE_SHIFT;
  224. /* Get the default value */
  225. config->wayNum = (l2cache_way_num_t)number;
  226. config->waySize = (l2cache_way_size)size;
  227. config->repacePolicy = kL2CACHE_Roundrobin;
  228. config->lateConfig = NULL;
  229. config->istrPrefetchEnable = false;
  230. config->dataPrefetchEnable = false;
  231. config->nsLockdownEnable = false;
  232. config->writeAlloc = kL2CACHE_UseAwcache;
  233. }
  234. void L2CACHE_Enable(void)
  235. {
  236. /* Invalidate first. */
  237. L2CACHE_Invalidate();
  238. /* Enable the level 2 cache controller. */
  239. L2CACHEC->REG1_CONTROL = L2CACHEC_REG1_CONTROL_CE_MASK;
  240. }
  241. void L2CACHE_Disable(void)
  242. {
  243. /* First CleanInvalidate all enties in the cache. */
  244. L2CACHE_CleanInvalidate();
  245. /* Disable the level 2 cache controller. */
  246. L2CACHEC->REG1_CONTROL &= ~L2CACHEC_REG1_CONTROL_CE_MASK;
  247. /* DSB - data sync barrier.*/
  248. __DSB();
  249. }
  250. void L2CACHE_Invalidate(void)
  251. {
  252. /* Invalidate all entries in cache. */
  253. L2CACHE_SetAndWaitBackGroundOperate(L2CACHEC->REG1_AUX_CONTROL, (uint32_t)&L2CACHEC->REG7_INV_WAY);
  254. /* Cache sync. */
  255. L2CACHEC->REG7_CACHE_SYNC = 0;
  256. }
  257. void L2CACHE_Clean(void)
  258. {
  259. /* Clean all entries of the cache. */
  260. L2CACHE_SetAndWaitBackGroundOperate(L2CACHEC->REG1_AUX_CONTROL, (uint32_t)&L2CACHEC->REG7_CLEAN_WAY);
  261. /* Cache sync. */
  262. L2CACHEC->REG7_CACHE_SYNC = 0;
  263. }
  264. void L2CACHE_CleanInvalidate(void)
  265. {
  266. /* Clean all entries of the cache. */
  267. L2CACHE_SetAndWaitBackGroundOperate(L2CACHEC->REG1_AUX_CONTROL, (uint32_t)&L2CACHEC->REG7_CLEAN_INV_WAY);
  268. /* Cache sync. */
  269. L2CACHEC->REG7_CACHE_SYNC = 0;
  270. }
  271. void L2CACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  272. {
  273. uint32_t endAddr = address + size_byte;
  274. /* Invalidate addresses in the range. */
  275. while (address < endAddr)
  276. {
  277. address = L2CACHE_InvalidateLineByAddr(address);
  278. /* Update the size. */
  279. address += FSL_FEATURE_L2CACHE_LINESIZE_BYTE;
  280. }
  281. /* Cache sync. */
  282. L2CACHEC->REG7_CACHE_SYNC = 0;
  283. }
  284. void L2CACHE_CleanByRange(uint32_t address, uint32_t size_byte)
  285. {
  286. uint32_t num_ways = 0;
  287. uint32_t size_way = 0;
  288. uint32_t endAddr = address + size_byte;
  289. /* Get the number and size of the cache way. */
  290. L2CACHE_GetWayNumSize(&num_ways, &size_way);
  291. /* Check if the clean size is over the cache size. */
  292. if ((endAddr - address) > num_ways * size_way)
  293. {
  294. L2CACHE_Clean();
  295. return;
  296. }
  297. /* Clean addresses in the range. */
  298. while ((address & ~(uint32_t)(FSL_FEATURE_L2CACHE_LINESIZE_BYTE - 1)) < endAddr)
  299. {
  300. /* Clean the address in the range. */
  301. address = L2CACHE_CleanLineByAddr(address);
  302. address += FSL_FEATURE_L2CACHE_LINESIZE_BYTE;
  303. }
  304. L2CACHEC->REG7_CACHE_SYNC = 0;
  305. }
  306. void L2CACHE_CleanInvalidateByRange(uint32_t address, uint32_t size_byte)
  307. {
  308. uint32_t num_ways = 0;
  309. uint32_t size_way = 0;
  310. uint32_t endAddr = address + size_byte;
  311. /* Get the number and size of the cache way. */
  312. L2CACHE_GetWayNumSize(&num_ways, &size_way);
  313. /* Check if the clean size is over the cache size. */
  314. if ((endAddr - address) > num_ways * size_way)
  315. {
  316. L2CACHE_CleanInvalidate();
  317. return;
  318. }
  319. /* Clean addresses in the range. */
  320. while ((address & ~(uint32_t)(FSL_FEATURE_L2CACHE_LINESIZE_BYTE - 1)) < endAddr)
  321. {
  322. /* Clean the address in the range. */
  323. address = L2CACHE_CleanInvalidateLineByAddr(address);
  324. address += FSL_FEATURE_L2CACHE_LINESIZE_BYTE;
  325. }
  326. L2CACHEC->REG7_CACHE_SYNC = 0;
  327. }
  328. void L2CACHE_LockdownByWayEnable(uint32_t masterId, uint32_t mask, bool enable)
  329. {
  330. uint8_t num_ways = (L2CACHEC->REG1_AUX_CONTROL & L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_MASK) >>
  331. L2CACHEC_REG1_AUX_CONTROL_ASSOCIATIVITY_SHIFT;
  332. num_ways = (num_ways + 1) * L2CACHE_SMALLWAYS_NUM;
  333. assert(mask < (1U << num_ways));
  334. assert(masterId < L2CACHE_LOCKDOWN_REGNUM);
  335. uint32_t dataReg = L2CACHEC->LOCKDOWN[masterId].REG9_D_LOCKDOWN;
  336. uint32_t istrReg = L2CACHEC->LOCKDOWN[masterId].REG9_I_LOCKDOWN;
  337. if (enable)
  338. {
  339. /* Data lockdown. */
  340. L2CACHEC->LOCKDOWN[masterId].REG9_D_LOCKDOWN = dataReg | mask;
  341. /* Instruction lockdown. */
  342. L2CACHEC->LOCKDOWN[masterId].REG9_I_LOCKDOWN = istrReg | mask;
  343. }
  344. else
  345. {
  346. /* Data lockdown. */
  347. L2CACHEC->LOCKDOWN[masterId].REG9_D_LOCKDOWN = dataReg & ~mask;
  348. /* Instruction lockdown. */
  349. L2CACHEC->LOCKDOWN[masterId].REG9_I_LOCKDOWN = istrReg & ~mask;
  350. }
  351. }
  352. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT > 0 */
  353. void L1CACHE_InvalidateICacheByRange(uint32_t address, uint32_t size_byte)
  354. {
  355. #if (__DCACHE_PRESENT == 1U)
  356. uint32_t addr = address & (uint32_t)~(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE - 1);
  357. int32_t size = size_byte + address - addr;
  358. uint32_t linesize = 32U;
  359. __DSB();
  360. while (size > 0)
  361. {
  362. SCB->ICIMVAU = addr;
  363. addr += linesize;
  364. size -= linesize;
  365. }
  366. __DSB();
  367. __ISB();
  368. #endif
  369. }
  370. void ICACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  371. {
  372. #if (FSL_FEATURE_SOC_L2CACHEC_COUNT > 0)
  373. #if defined(FSL_SDK_DISBLE_L2CACHE_PRESENT) && !FSL_SDK_DISBLE_L2CACHE_PRESENT
  374. L2CACHE_InvalidateByRange(address, size_byte);
  375. #endif /* !FSL_SDK_DISBLE_L2CACHE_PRESENT */
  376. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT > 0 */
  377. L1CACHE_InvalidateICacheByRange(address, size_byte);
  378. }
  379. void DCACHE_InvalidateByRange(uint32_t address, uint32_t size_byte)
  380. {
  381. #if (FSL_FEATURE_SOC_L2CACHEC_COUNT > 0)
  382. #if defined(FSL_SDK_DISBLE_L2CACHE_PRESENT) && !FSL_SDK_DISBLE_L2CACHE_PRESENT
  383. L2CACHE_InvalidateByRange(address, size_byte);
  384. #endif /* !FSL_SDK_DISBLE_L2CACHE_PRESENT */
  385. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT > 0 */
  386. L1CACHE_InvalidateDCacheByRange(address, size_byte);
  387. }
  388. void DCACHE_CleanByRange(uint32_t address, uint32_t size_byte)
  389. {
  390. L1CACHE_CleanDCacheByRange(address, size_byte);
  391. #if (FSL_FEATURE_SOC_L2CACHEC_COUNT > 0)
  392. #if defined(FSL_SDK_DISBLE_L2CACHE_PRESENT) && !FSL_SDK_DISBLE_L2CACHE_PRESENT
  393. L2CACHE_CleanByRange(address, size_byte);
  394. #endif /* !FSL_SDK_DISBLE_L2CACHE_PRESENT */
  395. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT > 0 */
  396. }
  397. void DCACHE_CleanInvalidateByRange(uint32_t address, uint32_t size_byte)
  398. {
  399. L1CACHE_CleanInvalidateDCacheByRange(address, size_byte);
  400. #if (FSL_FEATURE_SOC_L2CACHEC_COUNT > 0)
  401. #if defined(FSL_SDK_DISBLE_L2CACHE_PRESENT) && !FSL_SDK_DISBLE_L2CACHE_PRESENT
  402. L2CACHE_CleanInvalidateByRange(address, size_byte);
  403. #endif /* !FSL_SDK_DISBLE_L2CACHE_PRESENT */
  404. #endif /* FSL_FEATURE_SOC_L2CACHEC_COUNT > 0 */
  405. }