cache.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdint.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #ifndef BIT
  20. #define BIT(nr) (1 << (nr))
  21. #endif
  22. /** \defgroup cache_apis, cache operation related apis
  23. * @brief cache apis
  24. */
  25. /** @addtogroup cache_apis
  26. * @{
  27. */
  28. #define MIN_ICACHE_SIZE 16384
  29. #define MAX_ICACHE_SIZE 32768
  30. #define MIN_DCACHE_SIZE 32768
  31. #define MAX_DCACHE_SIZE 65536
  32. #define MIN_ICACHE_WAYS 4
  33. #define MAX_ICACHE_WAYS 8
  34. #define MIN_DCACHE_WAYS 4
  35. #define MAX_DCACHE_WAYS 4
  36. #define MAX_CACHE_WAYS 8
  37. #define MIN_CACHE_LINE_SIZE 16
  38. #define TAG_SIZE 4
  39. #define MIN_ICACHE_BANK_NUM 1
  40. #define MAX_ICACHE_BANK_NUM 2
  41. #define MIN_DCACHE_BANK_NUM 1
  42. #define MAX_DCACHE_BANK_NUM 2
  43. #define CACHE_MEMORY_BANK_NUM 4
  44. #define CACHE_MEMORY_IBANK_SIZE 0x4000
  45. #define CACHE_MEMORY_DBANK_SIZE 0x8000
  46. #define MAX_ITAG_BANK_ITEMS (MAX_ICACHE_SIZE / MAX_ICACHE_BANK_NUM / MIN_CACHE_LINE_SIZE)
  47. #define MAX_ITAG_BLOCK_ITEMS (MAX_ICACHE_SIZE / MAX_ICACHE_BANK_NUM / MAX_ICACHE_WAYS / MIN_CACHE_LINE_SIZE)
  48. #define MAX_ITAG_BANK_SIZE (MAX_ITAG_BANK_ITEMS * TAG_SIZE)
  49. #define MAX_ITAG_BLOCK_SIZE (MAX_ITAG_BLOCK_ITEMS * TAG_SIZE)
  50. #define MAX_DTAG_BANK_ITEMS (MAX_DCACHE_SIZE / MAX_DCACHE_BANK_NUM / MIN_CACHE_LINE_SIZE)
  51. #define MAX_DTAG_BLOCK_ITEMS (MAX_DCACHE_SIZE / MAX_DCACHE_BANK_NUM / MAX_DCACHE_WAYS / MIN_CACHE_LINE_SIZE)
  52. #define MAX_DTAG_BANK_SIZE (MAX_DTAG_BANK_ITEMS * TAG_SIZE)
  53. #define MAX_DTAG_BLOCK_SIZE (MAX_DTAG_BLOCK_ITEMS * TAG_SIZE)
  54. typedef enum {
  55. CACHE_DCACHE = 0,
  56. CACHE_ICACHE0 = 1,
  57. CACHE_ICACHE1 = 2,
  58. } cache_t;
  59. typedef enum {
  60. CACHE_MEMORY_INVALID = 0,
  61. CACHE_MEMORY_IBANK0 = BIT(0),
  62. CACHE_MEMORY_IBANK1 = BIT(1),
  63. CACHE_MEMORY_IBANK2 = BIT(2),
  64. CACHE_MEMORY_IBANK3 = BIT(3),
  65. CACHE_MEMORY_DBANK0 = BIT(0),
  66. CACHE_MEMORY_DBANK1 = BIT(1),
  67. CACHE_MEMORY_DBANK2 = BIT(2),
  68. CACHE_MEMORY_DBANK3 = BIT(3),
  69. } cache_array_t;
  70. #define ICACHE_SIZE_16KB CACHE_SIZE_HALF
  71. #define ICACHE_SIZE_32KB CACHE_SIZE_FULL
  72. #define DCACHE_SIZE_32KB CACHE_SIZE_HALF
  73. #define DCACHE_SIZE_64KB CACHE_SIZE_FULL
  74. typedef enum {
  75. CACHE_SIZE_HALF = 0, /*!< 8KB for icache and dcache */
  76. CACHE_SIZE_FULL = 1, /*!< 16KB for icache and dcache */
  77. } cache_size_t;
  78. typedef enum {
  79. CACHE_4WAYS_ASSOC = 0, /*!< 4 way associated cache */
  80. CACHE_8WAYS_ASSOC = 1, /*!< 8 way associated cache */
  81. } cache_ways_t;
  82. typedef enum {
  83. CACHE_LINE_SIZE_16B = 0, /*!< 16 Byte cache line size */
  84. CACHE_LINE_SIZE_32B = 1, /*!< 32 Byte cache line size */
  85. CACHE_LINE_SIZE_64B = 2, /*!< 64 Byte cache line size */
  86. } cache_line_size_t;
  87. typedef enum {
  88. CACHE_AUTOLOAD_POSITIVE = 0, /*!< cache autoload step is positive */
  89. CACHE_AUTOLOAD_NEGATIVE = 1, /*!< cache autoload step is negative */
  90. } cache_autoload_order_t;
  91. #define CACHE_AUTOLOAD_STEP(i) ((i) - 1)
  92. typedef enum {
  93. CACHE_AUTOLOAD_MISS_TRIGGER = 0, /*!< autoload only triggered by cache miss */
  94. CACHE_AUTOLOAD_HIT_TRIGGER = 1, /*!< autoload only triggered by cache hit */
  95. CACHE_AUTOLOAD_BOTH_TRIGGER = 2, /*!< autoload triggered both by cache miss and hit */
  96. } cache_autoload_trigger_t;
  97. typedef enum {
  98. CACHE_FREEZE_ACK_BUSY = 0, /*!< in this mode, cache ack busy to CPU if a cache miss happens*/
  99. CACHE_FREEZE_ACK_ERROR = 1, /*!< in this mode, cache ack wrong data to CPU and trigger an error if a cache miss happens */
  100. } cache_freeze_mode_t;
  101. struct cache_mode {
  102. uint32_t cache_size; /*!< cache size in byte */
  103. uint16_t cache_line_size; /*!< cache line size in byte */
  104. uint8_t cache_ways; /*!< cache ways, always 4 */
  105. uint8_t icache; /*!< the cache index, 0 for dcache, 1 for icache */
  106. };
  107. struct icache_tag_item {
  108. uint32_t valid: 1; /*!< the tag item is valid or not */
  109. uint32_t lock: 1; /*!< the cache line is locked or not */
  110. uint32_t attr: 4; /*!< the attribute of the external memory physical address */
  111. uint32_t fifo_cnt: 3; /*!< fifo cnt, 0 ~ 3 for 4 ways cache */
  112. uint32_t tag: 14; /*!< the tag is the high part of the cache address, however is only 64MB range, and without low part */
  113. uint32_t reserved: 9;
  114. };
  115. struct dcache_tag_item {
  116. uint32_t dirty: 1; /*!< the cache line value is dirty or not */
  117. uint32_t valid: 1; /*!< the tag item is valid or not */
  118. uint32_t lock: 1; /*!< the cache line is locked or not */
  119. uint32_t occupy: 1; /*!< the cache line is occupied as internal sram */
  120. uint32_t attr: 4; /*!< the attribute of the external memory physical address */
  121. uint32_t fifo_cnt: 2; /*!< fifo cnt, 0 ~ 3 for 4 ways cache */
  122. uint32_t tag: 13; /*!< the tag is the high part of the cache address, however is only 64MB range, and without low part */
  123. uint32_t reserved: 9;
  124. };
  125. struct autoload_config {
  126. uint8_t order; /*!< autoload step is positive or negative */
  127. uint8_t trigger; /*!< autoload trigger */
  128. uint8_t ena0; /*!< autoload region0 enable */
  129. uint8_t ena1; /*!< autoload region1 enable */
  130. uint32_t addr0; /*!< autoload region0 start address */
  131. uint32_t size0; /*!< autoload region0 size */
  132. uint32_t addr1; /*!< autoload region1 start address */
  133. uint32_t size1; /*!< autoload region1 size */
  134. };
  135. struct tag_group_info {
  136. struct cache_mode mode; /*!< cache and cache mode */
  137. uint32_t filter_addr; /*!< the address that used to generate the struct */
  138. uint32_t vaddr_offset; /*!< virtual address offset of the cache ways */
  139. uint32_t tag_addr[MAX_CACHE_WAYS]; /*!< tag memory address, only [0~mode.ways-1] is valid to use */
  140. uint32_t cache_memory_offset[MAX_CACHE_WAYS]; /*!< cache memory address, only [0~mode.ways-1] is valid to use */
  141. };
  142. struct lock_config {
  143. uint32_t addr; /*!< manual lock address*/
  144. uint16_t size; /*!< manual lock size*/
  145. uint16_t group; /*!< manual lock group, 0 or 1*/
  146. };
  147. #define ESP_ROM_ERR_INVALID_ARG 1
  148. #define MMU_SET_ADDR_ALIGNED_ERROR 2
  149. #define MMU_SET_PASE_SIZE_ERROR 3
  150. #define MMU_SET_VADDR_OUT_RANGE 4
  151. #define CACHE_OP_ICACHE_Y 1
  152. #define CACHE_OP_ICACHE_N 0
  153. /**
  154. * @brief Initialise cache mmu, mark all entries as invalid.
  155. * Please do not call this function in your SDK application.
  156. *
  157. * @param None
  158. *
  159. * @return None
  160. */
  161. void Cache_MMU_Init(void);
  162. /**
  163. * @brief Set ICache mmu mapping.
  164. * Please do not call this function in your SDK application.
  165. *
  166. * @param uint32_t ext_ram : DPORT_MMU_ACCESS_FLASH for flash, DPORT_MMU_ACCESS_SPIRAM for spiram, DPORT_MMU_INVALID for invalid.
  167. *
  168. * @param uint32_t vaddr : virtual address in CPU address space.
  169. * Can be Iram0,Iram1,Irom0,Drom0 and AHB buses address.
  170. * Should be aligned by psize.
  171. *
  172. * @param uint32_t paddr : physical address in external memory.
  173. * Should be aligned by psize.
  174. *
  175. * @param uint32_t psize : page size of ICache, in kilobytes. Should be 64 here.
  176. *
  177. * @param uint32_t num : pages to be set.
  178. *
  179. * @param uint32_t fixed : 0 for physical pages grow with virtual pages, other for virtual pages map to same physical page.
  180. *
  181. * @return uint32_t: error status
  182. * 0 : mmu set success
  183. * 2 : vaddr or paddr is not aligned
  184. * 3 : psize error
  185. * 4 : vaddr is out of range
  186. */
  187. int Cache_Ibus_MMU_Set(uint32_t ext_ram, uint32_t vaddr, uint32_t paddr, uint32_t psize, uint32_t num, uint32_t fixed);
  188. /**
  189. * @brief Set DCache mmu mapping.
  190. * Please do not call this function in your SDK application.
  191. *
  192. * @param uint32_t ext_ram : DPORT_MMU_ACCESS_FLASH for flash, DPORT_MMU_ACCESS_SPIRAM for spiram, DPORT_MMU_INVALID for invalid.
  193. *
  194. * @param uint32_t vaddr : virtual address in CPU address space.
  195. * Can be DRam0, DRam1, DRom0, DPort and AHB buses address.
  196. * Should be aligned by psize.
  197. *
  198. * @param uint32_t paddr : physical address in external memory.
  199. * Should be aligned by psize.
  200. *
  201. * @param uint32_t psize : page size of DCache, in kilobytes. Should be 64 here.
  202. *
  203. * @param uint32_t num : pages to be set.
  204. * @param uint32_t fixed : 0 for physical pages grow with virtual pages, other for virtual pages map to same physical page.
  205. *
  206. * @return uint32_t: error status
  207. * 0 : mmu set success
  208. * 2 : vaddr or paddr is not aligned
  209. * 3 : psize error
  210. * 4 : vaddr is out of range
  211. */
  212. int Cache_Dbus_MMU_Set(uint32_t ext_ram, uint32_t vaddr, uint32_t paddr, uint32_t psize, uint32_t num, uint32_t fixed);
  213. /**
  214. * @brief Count the pages in the bus room address which map to Flash.
  215. * Please do not call this function in your SDK application.
  216. *
  217. * @param uint32_t bus : the bus to count with.
  218. *
  219. * @param uint32_t * page0_mapped : value should be initial by user, 0 for not mapped, other for mapped count.
  220. *
  221. * return uint32_t : the number of pages which map to Flash.
  222. */
  223. uint32_t Cache_Count_Flash_Pages(uint32_t bus, uint32_t *page0_mapped);
  224. /**
  225. * @brief Copy Instruction or rodata from Flash to SPIRAM, and remap to SPIRAM.
  226. * Please do not call this function in your SDK application.
  227. *
  228. * @param uint32_t bus : the bus which need to copy to SPIRAM.
  229. *
  230. * @param uint32_t bus_start_addr : the start virtual address for the bus.
  231. *
  232. * @param uint32_t start_page : the start (64KB) page number in SPIRAM.
  233. *
  234. * @param uint32_t * page0_page : the flash page0 in SPIRAM page number, 0xffff for invalid.
  235. *
  236. * return uint32_t : the next start page number for SPIRAM not mapped.
  237. */
  238. uint32_t Cache_Flash_To_SPIRAM_Copy(uint32_t bus, uint32_t bus_start_addr, uint32_t start_page, uint32_t *page0_page);
  239. /**
  240. * @brief allocate memory to used by ICache.
  241. * Please do not call this function in your SDK application.
  242. *
  243. * @param cache_array_t icache_low : the data array bank used by icache low part, can be CACHE_MEMORY_INVALID, CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1
  244. *
  245. * @param cache_array_t icache_high : the data array bank used by icache high part, can be CACHE_MEMORY_INVALID, CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1 only if icache_low and icache_high is not CACHE_MEMORY_INVALID
  246. *
  247. * return none
  248. */
  249. void Cache_Occupy_ICache_MEMORY(cache_array_t icache_low, cache_array_t icache_high);
  250. /**
  251. * @brief allocate memory to used by DCache.
  252. * Please do not call this function in your SDK application.
  253. *
  254. * @param cache_array_t dcache_low : the data array bank used by dcache low part, can be CACHE_MEMORY_INVALID, CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1
  255. *
  256. * @param cache_array_t dcache1_high : the data array bank used by dcache high part, can be CACHE_MEMORY_INVALID, CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1 only if dcache_low0 and dcache_low1 is not CACHE_MEMORY_INVALID
  257. *
  258. * return none
  259. */
  260. void Cache_Occupy_DCache_MEMORY(cache_array_t dcache_low, cache_array_t dcache_high);
  261. /**
  262. * @brief Get cache mode of ICache or DCache.
  263. * Please do not call this function in your SDK application.
  264. *
  265. * @param struct cache_mode * mode : the pointer of cache mode struct, caller should set the icache field
  266. *
  267. * return none
  268. */
  269. void Cache_Get_Mode(struct cache_mode *mode);
  270. /**
  271. * @brief set ICache modes: cache size, associate ways and cache line size.
  272. * Please do not call this function in your SDK application.
  273. *
  274. * @param cache_size_t cache_size : the cache size, can be CACHE_SIZE_HALF and CACHE_SIZE_FULL
  275. *
  276. * @param cache_ways_t ways : the associate ways of cache, can be CACHE_4WAYS_ASSOC and CACHE_8WAYS_ASSOC
  277. *
  278. * @param cache_line_size_t cache_line_size : the cache line size, can be CACHE_LINE_SIZE_16B, CACHE_LINE_SIZE_32B and CACHE_LINE_SIZE_64B
  279. *
  280. * return none
  281. */
  282. void Cache_Set_ICache_Mode(cache_size_t cache_size, cache_ways_t ways, cache_line_size_t cache_line_size);
  283. /**
  284. * @brief set DCache modes: cache size, associate ways and cache line size.
  285. * Please do not call this function in your SDK application.
  286. *
  287. * @param cache_size_t cache_size : the cache size, can be CACHE_SIZE_8KB and CACHE_SIZE_16KB
  288. *
  289. * @param cache_ways_t ways : the associate ways of cache, can be CACHE_4WAYS_ASSOC and CACHE_8WAYS_ASSOC
  290. *
  291. * @param cache_line_size_t cache_line_size : the cache line size, can be CACHE_LINE_SIZE_16B, CACHE_LINE_SIZE_32B and CACHE_LINE_SIZE_64B
  292. *
  293. * return none
  294. */
  295. void Cache_Set_DCache_Mode(cache_size_t cache_size, cache_ways_t ways, cache_line_size_t cache_line_size);
  296. /**
  297. * @brief check if the address is accessed through ICache.
  298. * Please do not call this function in your SDK application.
  299. *
  300. * @param uint32_t addr : the address to check.
  301. *
  302. * @return 1 if the address is accessed through ICache, 0 if not.
  303. */
  304. uint32_t Cache_Address_Through_ICache(uint32_t addr);
  305. /**
  306. * @brief check if the address is accessed through DCache.
  307. * Please do not call this function in your SDK application.
  308. *
  309. * @param uint32_t addr : the address to check.
  310. *
  311. * @return 1 if the address is accessed through DCache, 0 if not.
  312. */
  313. uint32_t Cache_Address_Through_DCache(uint32_t addr);
  314. /**
  315. * @brief Init mmu owner register to make i/d cache use half mmu entries.
  316. *
  317. * @param None
  318. *
  319. * @return None
  320. */
  321. void Cache_Owner_Init(void);
  322. /**
  323. * @brief Invalidate the cache items for ICache.
  324. * Operation will be done CACHE_LINE_SIZE aligned.
  325. * If the region is not in ICache addr room, nothing will be done.
  326. * Please do not call this function in your SDK application.
  327. *
  328. * @param uint32_t addr: start address to invalidate
  329. *
  330. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  331. *
  332. * @return None
  333. */
  334. void Cache_Invalidate_ICache_Items(uint32_t addr, uint32_t items);
  335. /**
  336. * @brief Invalidate the cache items for DCache.
  337. * Operation will be done CACHE_LINE_SIZE aligned.
  338. * If the region is not in DCache addr room, nothing will be done.
  339. * Please do not call this function in your SDK application.
  340. *
  341. * @param uint32_t addr: start address to invalidate
  342. *
  343. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  344. *
  345. * @return None
  346. */
  347. void Cache_Invalidate_DCache_Items(uint32_t addr, uint32_t items);
  348. /**
  349. * @brief Clean the dirty bit of cache Items of DCache.
  350. * Operation will be done CACHE_LINE_SIZE aligned.
  351. * If the region is not in DCache addr room, nothing will be done.
  352. * Please do not call this function in your SDK application.
  353. *
  354. * @param uint32_t addr: start address to Clean
  355. *
  356. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  357. *
  358. * @return None
  359. */
  360. void Cache_Clean_Items(uint32_t addr, uint32_t items);
  361. /**
  362. * @brief Write back the cache items of DCache.
  363. * Operation will be done CACHE_LINE_SIZE aligned.
  364. * If the region is not in DCache addr room, nothing will be done.
  365. * Please do not call this function in your SDK application.
  366. *
  367. * @param uint32_t addr: start address to write back
  368. *
  369. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  370. *
  371. * @return None
  372. */
  373. void Cache_WriteBack_Items(uint32_t addr, uint32_t items);
  374. /**
  375. * @brief Invalidate the Cache items in the region from ICache or DCache.
  376. * If the region is not in Cache addr room, nothing will be done.
  377. * Please do not call this function in your SDK application.
  378. *
  379. * @param uint32_t addr : invalidated region start address.
  380. *
  381. * @param uint32_t size : invalidated region size.
  382. *
  383. * @return 0 for success
  384. * 1 for invalid argument
  385. */
  386. int Cache_Invalidate_Addr(uint32_t addr, uint32_t size);
  387. /**
  388. * @brief Clean the dirty bit of Cache items in the region from DCache.
  389. * If the region is not in DCache addr room, nothing will be done.
  390. * Please do not call this function in your SDK application.
  391. *
  392. * @param uint32_t addr : cleaned region start address.
  393. *
  394. * @param uint32_t size : cleaned region size.
  395. *
  396. * @return 0 for success
  397. * 1 for invalid argument
  398. */
  399. int Cache_Clean_Addr(uint32_t addr, uint32_t size);
  400. /**
  401. * @brief Writeback the Cache items(also clean the dirty bit) in the region from DCache.
  402. * If the region is not in DCache addr room, nothing will be done.
  403. * Please do not call this function in your SDK application.
  404. *
  405. * @param uint32_t addr : writeback region start address.
  406. *
  407. * @param uint32_t size : writeback region size.
  408. *
  409. * @return 0 for success
  410. * 1 for invalid argument
  411. */
  412. int Cache_WriteBack_Addr(uint32_t addr, uint32_t size);
  413. /**
  414. * @brief Invalidate all cache items in ICache.
  415. * Please do not call this function in your SDK application.
  416. *
  417. * @param None
  418. *
  419. * @return None
  420. */
  421. void Cache_Invalidate_ICache_All(void);
  422. /**
  423. * @brief Invalidate all cache items in DCache.
  424. * Please do not call this function in your SDK application.
  425. *
  426. * @param None
  427. *
  428. * @return None
  429. */
  430. void Cache_Invalidate_DCache_All(void);
  431. /**
  432. * @brief Clean the dirty bit of all cache items in DCache.
  433. * Please do not call this function in your SDK application.
  434. *
  435. * @param None
  436. *
  437. * @return None
  438. */
  439. void Cache_Clean_All(void);
  440. /**
  441. * @brief WriteBack all cache items in DCache.
  442. * Please do not call this function in your SDK application.
  443. *
  444. * @param None
  445. *
  446. * @return None
  447. */
  448. void Cache_WriteBack_All(void);
  449. /**
  450. * @brief Mask all buses through ICache and DCache.
  451. * Please do not call this function in your SDK application.
  452. *
  453. * @param None
  454. *
  455. * @return None
  456. */
  457. void Cache_Mask_All(void);
  458. /**
  459. * @brief UnMask DRam0 bus through DCache.
  460. * Please do not call this function in your SDK application.
  461. *
  462. * @param None
  463. *
  464. * @return None
  465. */
  466. void Cache_UnMask_Dram0(void);
  467. /**
  468. * @brief Suspend ICache auto preload operation, then you can resume it after some ICache operations.
  469. * Please do not call this function in your SDK application.
  470. *
  471. * @param None
  472. *
  473. * @return uint32_t : 0 for ICache not auto preload before suspend.
  474. */
  475. uint32_t Cache_Suspend_ICache_Autoload(void);
  476. /**
  477. * @brief Resume ICache auto preload operation after some ICache operations.
  478. * Please do not call this function in your SDK application.
  479. *
  480. * @param uint32_t autoload : 0 for ICache not auto preload before suspend.
  481. *
  482. * @return None.
  483. */
  484. void Cache_Resume_ICache_Autoload(uint32_t autoload);
  485. /**
  486. * @brief Suspend DCache auto preload operation, then you can resume it after some DCache operations.
  487. * Please do not call this function in your SDK application.
  488. *
  489. * @param None
  490. *
  491. * @return uint32_t : 0 for DCache not auto preload before suspend.
  492. */
  493. uint32_t Cache_Suspend_DCache_Autoload(void);
  494. /**
  495. * @brief Resume DCache auto preload operation after some DCache operations.
  496. * Please do not call this function in your SDK application.
  497. *
  498. * @param uint32_t autoload : 0 for DCache not auto preload before suspend.
  499. *
  500. * @return None.
  501. */
  502. void Cache_Resume_DCache_Autoload(uint32_t autoload);
  503. /**
  504. * @brief Start an ICache manual preload, will suspend auto preload of ICache.
  505. * Please do not call this function in your SDK application.
  506. *
  507. * @param uint32_t addr : start address of the preload region.
  508. *
  509. * @param uint32_t size : size of the preload region, should not exceed the size of ICache.
  510. *
  511. * @param uint32_t order : the preload order, 0 for positive, other for negative
  512. *
  513. * @return uint32_t : 0 for ICache not auto preload before manual preload.
  514. */
  515. uint32_t Cache_Start_ICache_Preload(uint32_t addr, uint32_t size, uint32_t order);
  516. /**
  517. * @brief Return if the ICache manual preload done.
  518. * Please do not call this function in your SDK application.
  519. *
  520. * @param None
  521. *
  522. * @return uint32_t : 0 for ICache manual preload not done.
  523. */
  524. uint32_t Cache_ICache_Preload_Done(void);
  525. /**
  526. * @brief End the ICache manual preload to resume auto preload of ICache.
  527. * Please do not call this function in your SDK application.
  528. *
  529. * @param uint32_t autoload : 0 for ICache not auto preload before manual preload.
  530. *
  531. * @return None
  532. */
  533. void Cache_End_ICache_Preload(uint32_t autoload);
  534. /**
  535. * @brief Start an DCache manual preload, will suspend auto preload of DCache.
  536. * Please do not call this function in your SDK application.
  537. *
  538. * @param uint32_t addr : start address of the preload region.
  539. *
  540. * @param uint32_t size : size of the preload region, should not exceed the size of DCache.
  541. *
  542. * @param uint32_t order : the preload order, 0 for positive, other for negative
  543. *
  544. * @return uint32_t : 0 for DCache not auto preload before manual preload.
  545. */
  546. uint32_t Cache_Start_DCache_Preload(uint32_t addr, uint32_t size, uint32_t order);
  547. /**
  548. * @brief Return if the DCache manual preload done.
  549. * Please do not call this function in your SDK application.
  550. *
  551. * @param None
  552. *
  553. * @return uint32_t : 0 for DCache manual preload not done.
  554. */
  555. uint32_t Cache_DCache_Preload_Done(void);
  556. /**
  557. * @brief End the DCache manual preload to resume auto preload of DCache.
  558. * Please do not call this function in your SDK application.
  559. *
  560. * @param uint32_t autoload : 0 for DCache not auto preload before manual preload.
  561. *
  562. * @return None
  563. */
  564. void Cache_End_DCache_Preload(uint32_t autoload);
  565. /**
  566. * @brief Config autoload parameters of ICache.
  567. * Please do not call this function in your SDK application.
  568. *
  569. * @param struct autoload_config * config : autoload parameters.
  570. *
  571. * @return None
  572. */
  573. void Cache_Config_ICache_Autoload(const struct autoload_config *config);
  574. /**
  575. * @brief Enable auto preload for ICache.
  576. * Please do not call this function in your SDK application.
  577. *
  578. * @param None
  579. *
  580. * @return None
  581. */
  582. void Cache_Enable_ICache_Autoload(void);
  583. /**
  584. * @brief Disable auto preload for ICache.
  585. * Please do not call this function in your SDK application.
  586. *
  587. * @param None
  588. *
  589. * @return None
  590. */
  591. void Cache_Disable_ICache_Autoload(void);
  592. /**
  593. * @brief Config autoload parameters of DCache.
  594. * Please do not call this function in your SDK application.
  595. *
  596. * @param struct autoload_config * config : autoload parameters.
  597. *
  598. * @return None
  599. */
  600. void Cache_Config_DCache_Autoload(const struct autoload_config *config);
  601. /**
  602. * @brief Enable auto preload for DCache.
  603. * Please do not call this function in your SDK application.
  604. *
  605. * @param None
  606. *
  607. * @return None
  608. */
  609. void Cache_Enable_DCache_Autoload(void);
  610. /**
  611. * @brief Disable auto preload for DCache.
  612. * Please do not call this function in your SDK application.
  613. *
  614. * @param None
  615. *
  616. * @return None
  617. */
  618. void Cache_Disable_DCache_Autoload(void);
  619. /**
  620. * @brief Config a group of prelock parameters of ICache.
  621. * Please do not call this function in your SDK application.
  622. *
  623. * @param struct lock_config * config : a group of lock parameters.
  624. *
  625. * @return None
  626. */
  627. void Cache_Enable_ICache_PreLock(const struct lock_config *config);
  628. /**
  629. * @brief Disable a group of prelock parameters for ICache.
  630. * However, the locked data will not be released.
  631. * Please do not call this function in your SDK application.
  632. *
  633. * @param uint16_t group : 0 for group0, 1 for group1.
  634. *
  635. * @return None
  636. */
  637. void Cache_Disable_ICache_PreLock(uint16_t group);
  638. /**
  639. * @brief Lock the cache items for ICache.
  640. * Operation will be done CACHE_LINE_SIZE aligned.
  641. * If the region is not in ICache addr room, nothing will be done.
  642. * Please do not call this function in your SDK application.
  643. *
  644. * @param uint32_t addr: start address to lock
  645. *
  646. * @param uint32_t items: cache lines to lock, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  647. *
  648. * @return None
  649. */
  650. void Cache_Lock_ICache_Items(uint32_t addr, uint32_t items);
  651. /**
  652. * @brief Unlock the cache items for ICache.
  653. * Operation will be done CACHE_LINE_SIZE aligned.
  654. * If the region is not in ICache addr room, nothing will be done.
  655. * Please do not call this function in your SDK application.
  656. *
  657. * @param uint32_t addr: start address to unlock
  658. *
  659. * @param uint32_t items: cache lines to unlock, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  660. *
  661. * @return None
  662. */
  663. void Cache_Unlock_ICache_Items(uint32_t addr, uint32_t items);
  664. /**
  665. * @brief Config a group of prelock parameters of DCache.
  666. * Please do not call this function in your SDK application.
  667. *
  668. * @param struct lock_config * config : a group of lock parameters.
  669. *
  670. * @return None
  671. */
  672. void Cache_Enable_DCache_PreLock(const struct lock_config *config);
  673. /**
  674. * @brief Disable a group of prelock parameters for DCache.
  675. * However, the locked data will not be released.
  676. * Please do not call this function in your SDK application.
  677. *
  678. * @param uint16_t group : 0 for group0, 1 for group1.
  679. *
  680. * @return None
  681. */
  682. void Cache_Disable_DCache_PreLock(uint16_t group);
  683. /**
  684. * @brief Lock the cache items for DCache.
  685. * Operation will be done CACHE_LINE_SIZE aligned.
  686. * If the region is not in DCache addr room, nothing will be done.
  687. * Please do not call this function in your SDK application.
  688. *
  689. * @param uint32_t addr: start address to lock
  690. *
  691. * @param uint32_t items: cache lines to lock, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  692. *
  693. * @return None
  694. */
  695. void Cache_Lock_DCache_Items(uint32_t addr, uint32_t items);
  696. /**
  697. * @brief Unlock the cache items for DCache.
  698. * Operation will be done CACHE_LINE_SIZE aligned.
  699. * If the region is not in DCache addr room, nothing will be done.
  700. * Please do not call this function in your SDK application.
  701. *
  702. * @param uint32_t addr: start address to unlock
  703. *
  704. * @param uint32_t items: cache lines to unlock, items * cache_line_size should not exceed the bus address size(16MB/32MB/64MB)
  705. *
  706. * @return None
  707. */
  708. void Cache_Unlock_DCache_Items(uint32_t addr, uint32_t items);
  709. /**
  710. * @brief Lock the cache items in tag memory for ICache or DCache.
  711. * Please do not call this function in your SDK application.
  712. *
  713. * @param uint32_t addr : start address of lock region.
  714. *
  715. * @param uint32_t size : size of lock region.
  716. *
  717. * @return 0 for success
  718. * 1 for invalid argument
  719. */
  720. int Cache_Lock_Addr(uint32_t addr, uint32_t size);
  721. /**
  722. * @brief Unlock the cache items in tag memory for ICache or DCache.
  723. * Please do not call this function in your SDK application.
  724. *
  725. * @param uint32_t addr : start address of unlock region.
  726. *
  727. * @param uint32_t size : size of unlock region.
  728. *
  729. * @return 0 for success
  730. * 1 for invalid argument
  731. */
  732. int Cache_Unlock_Addr(uint32_t addr, uint32_t size);
  733. /**
  734. * @brief Disable ICache access for the cpu.
  735. * This operation will make all ICache tag memory invalid, CPU can't access ICache, ICache will keep idle.
  736. * Please do not call this function in your SDK application.
  737. *
  738. * @return uint32_t : auto preload enabled before
  739. */
  740. uint32_t Cache_Disable_ICache(void);
  741. /**
  742. * @brief Enable ICache access for the cpu.
  743. * Please do not call this function in your SDK application.
  744. *
  745. * @param uint32_t autoload : ICache will preload then.
  746. *
  747. * @return None
  748. */
  749. void Cache_Enable_ICache(uint32_t autoload);
  750. /**
  751. * @brief Disable DCache access for the cpu.
  752. * This operation will make all DCache tag memory invalid, CPU can't access DCache, DCache will keep idle
  753. * Please do not call this function in your SDK application.
  754. *
  755. * @return uint32_t : auto preload enabled before
  756. */
  757. uint32_t Cache_Disable_DCache(void);
  758. /**
  759. * @brief Enable DCache access for the cpu.
  760. * Please do not call this function in your SDK application.
  761. *
  762. * @param uint32_t autoload : DCache will preload then.
  763. *
  764. * @return None
  765. */
  766. void Cache_Enable_DCache(uint32_t autoload);
  767. /**
  768. * @brief Suspend ICache access for the cpu.
  769. * The ICache tag memory is still there, CPU can't access ICache, ICache will keep idle.
  770. * Please do not change MMU, cache mode or tag memory(tag memory can be changed in some special case).
  771. * Please do not call this function in your SDK application.
  772. *
  773. * @param None
  774. *
  775. * @return uint32_t : auto preload enabled before
  776. */
  777. uint32_t Cache_Suspend_ICache(void);
  778. /**
  779. * @brief Resume ICache access for the cpu.
  780. * Please do not call this function in your SDK application.
  781. *
  782. * @param uint32_t autoload : ICache will preload then.
  783. *
  784. * @return None
  785. */
  786. void Cache_Resume_ICache(uint32_t autoload);
  787. /**
  788. * @brief Suspend DCache access for the cpu.
  789. * The ICache tag memory is still there, CPU can't access DCache, DCache will keep idle.
  790. × Please do not change MMU, cache mode or tag memory(tag memory can be changed in some special case).
  791. * Please do not call this function in your SDK application.
  792. *
  793. * @param None
  794. *
  795. * @return uint32_t : auto preload enabled before
  796. */
  797. uint32_t Cache_Suspend_DCache(void);
  798. /**
  799. * @brief Resume DCache access for the cpu.
  800. * Please do not call this function in your SDK application.
  801. *
  802. * @param uint32_t autoload : DCache will preload then.
  803. *
  804. * @return None
  805. */
  806. void Cache_Resume_DCache(uint32_t autoload);
  807. /**
  808. * @brief Get ICache cache line size
  809. *
  810. * @param None
  811. *
  812. * @return uint32_t: 16, 32, 64 Byte
  813. */
  814. uint32_t Cache_Get_ICache_Line_Size(void);
  815. /**
  816. * @brief Get DCache cache line size
  817. *
  818. * @param None
  819. *
  820. * @return uint32_t: 16, 32, 64 Byte
  821. */
  822. uint32_t Cache_Get_DCache_Line_Size(void);
  823. /**
  824. * @brief Set default mode from boot, 8KB ICache, 16Byte cache line size.
  825. *
  826. * @param None
  827. *
  828. * @return None
  829. */
  830. void Cache_Set_Default_Mode(void);
  831. /**
  832. * @brief Set default mode from boot, 8KB ICache, 16Byte cache line size.
  833. *
  834. * @param None
  835. *
  836. * @return None
  837. */
  838. void Cache_Enable_Defalut_ICache_Mode(void);
  839. /**
  840. * @brief Occupy the cache items for DCache.
  841. * Operation will be done CACHE_LINE_SIZE aligned.
  842. * If the region is not in DCache addr room, nothing will be done.
  843. * Please do not call this function in your SDK application.
  844. *
  845. * @param uint32_t addr : start address of occupy region
  846. *
  847. * @param uint32_t items : cache lines to occupy, items * cache_line_size should not exceed the cache_size
  848. *
  849. * @return None
  850. */
  851. void Cache_Occupy_Items(uint32_t addr, uint32_t items);
  852. /**
  853. * @brief Occupy the cache addr for DCache.
  854. * Operation will be done CACHE_LINE_SIZE aligned.
  855. * If the region is not in DCache addr room, nothing will be done.
  856. * Please do not call this function in your SDK application.
  857. *
  858. * @param uint32_t addr : start address of occupy region
  859. *
  860. * @param uint32_t size : size of occupy region, size should not exceed the cache_size
  861. */
  862. int Cache_Occupy_Addr(uint32_t addr, uint32_t size);
  863. /**
  864. * @brief Enable freeze for ICache.
  865. * Any miss request will be rejected, including cpu miss and preload/autoload miss.
  866. * Please do not call this function in your SDK application.
  867. *
  868. * @param cache_freeze_mode_t mode : 0 for assert busy 1 for assert hit
  869. *
  870. * @return None
  871. */
  872. void Cache_Freeze_ICache_Enable(cache_freeze_mode_t mode);
  873. /**
  874. * @brief Disable freeze for ICache.
  875. * Please do not call this function in your SDK application.
  876. *
  877. * @return None
  878. */
  879. void Cache_Freeze_ICache_Disable(void);
  880. /**
  881. * @brief Enable freeze for DCache.
  882. * Any miss request will be rejected, including cpu miss and preload/autoload miss.
  883. * Please do not call this function in your SDK application.
  884. *
  885. * @param cache_freeze_mode_t mode : 0 for assert busy 1 for assert hit
  886. *
  887. * @return None
  888. */
  889. void Cache_Freeze_DCache_Enable(cache_freeze_mode_t mode);
  890. /**
  891. * @brief Disable freeze for DCache.
  892. * Please do not call this function in your SDK application.
  893. *
  894. * @return None
  895. */
  896. void Cache_Freeze_DCache_Disable(void);
  897. /**
  898. * @brief Travel tag memory to run a call back function.
  899. * ICache and DCache are suspend when doing this.
  900. * The callback will get the parameter tag_group_info, which will include a group of tag memory addresses and cache memory addresses.
  901. * Please do not call this function in your SDK application.
  902. *
  903. * @param struct cache_mode * mode : the cache to check and the cache mode.
  904. *
  905. * @param uint32_t filter_addr : only the cache lines which may include the filter_address will be returned to the call back function.
  906. * 0 for do not filter, all cache lines will be returned.
  907. *
  908. * @param void (* process)(struct tag_group_info *) : call back function, which may be called many times, a group(the addresses in the group are in the same position in the cache ways) a time.
  909. *
  910. * @return None
  911. */
  912. void Cache_Travel_Tag_Memory(struct cache_mode *mode, uint32_t filter_addr, void (* process)(struct tag_group_info *));
  913. /**
  914. * @brief Get the virtual address from cache mode, cache tag and the virtual address offset of cache ways.
  915. * Please do not call this function in your SDK application.
  916. *
  917. * @param struct cache_mode * mode : the cache to calculate the virtual address and the cache mode.
  918. *
  919. * @param uint32_t tag : the tag part fo a tag item, 12-14 bits.
  920. *
  921. * @param uint32_t addr_offset : the virtual address offset of the cache ways.
  922. *
  923. * @return uint32_t : the virtual address.
  924. */
  925. uint32_t Cache_Get_Virtual_Addr(struct cache_mode *mode, uint32_t tag, uint32_t vaddr_offset);
  926. /**
  927. * @brief Get cache memory block base address.
  928. * Please do not call this function in your SDK application.
  929. *
  930. * @param uint32_t icache : 0 for dcache, other for icache.
  931. *
  932. * @param uint32_t bank_no : 0 ~ 3 bank.
  933. *
  934. * @return uint32_t : the cache memory block base address, 0 if the block not used.
  935. */
  936. uint32_t Cache_Get_Memory_BaseAddr(uint32_t icache, uint32_t bank_no);
  937. /**
  938. * @brief Get the cache memory address from cache mode, cache memory offset and the virtual address offset of cache ways.
  939. * Please do not call this function in your SDK application.
  940. *
  941. * @param struct cache_mode * mode : the cache to calculate the virtual address and the cache mode.
  942. *
  943. * @param uint32_t cache_memory_offset : the cache memory offset of the whole cache (ICache or DCache) for the cache line.
  944. *
  945. * @param uint32_t addr_offset : the virtual address offset of the cache ways.
  946. *
  947. * @return uint32_t : the virtual address.
  948. */
  949. uint32_t Cache_Get_Memory_Addr(struct cache_mode *mode, uint32_t cache_memory_offset, uint32_t vaddr_offset);
  950. /**
  951. * @brief Get the cache memory value by DRAM address.
  952. * Please do not call this function in your SDK application.
  953. *
  954. * @param uint32_t cache_memory_addr : DRAM address for the cache memory, should be 4 byte aligned for IBus address.
  955. *
  956. * @return uint32_t : the word value of the address.
  957. */
  958. uint32_t Cache_Get_Memory_value(uint32_t cache_memory_addr);
  959. /**
  960. * @}
  961. */
  962. /**
  963. * @brief Get the cache MMU IROM end address.
  964. * Please do not call this function in your SDK application.
  965. *
  966. * @param void
  967. *
  968. * @return uint32_t : the word value of the address.
  969. */
  970. uint32_t Cache_Get_IROM_MMU_End(void);
  971. /**
  972. * @brief Get the cache MMU DROM end address.
  973. * Please do not call this function in your SDK application.
  974. *
  975. * @param void
  976. *
  977. * @return uint32_t : the word value of the address.
  978. */
  979. uint32_t Cache_Get_DROM_MMU_End(void);
  980. /**
  981. * @brief Used by SPI flash mmap
  982. *
  983. */
  984. int flash2spiram_instruction_offset(void);
  985. int flash2spiram_rodata_offset(void);
  986. uint32_t flash_instr_rodata_start_page(uint32_t bus);
  987. uint32_t flash_instr_rodata_end_page(uint32_t bus);
  988. #ifdef __cplusplus
  989. }
  990. #endif