cache.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ROM_CACHE_H_
  7. #define _ROM_CACHE_H_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /** \defgroup cache_apis, cache operation related apis
  12. * @brief cache apis
  13. */
  14. /** @addtogroup cache_apis
  15. * @{
  16. */
  17. #define MIN_CACHE_SIZE 8192
  18. #define MAX_CACHE_SIZE 16384
  19. #define MIN_CACHE_WAYS 4
  20. #define MAX_CACHE_WAYS 4
  21. #define MIN_CACHE_LINE_SIZE 16
  22. //normally should be (MAX_CACHE_SIZE / MIN_CACHE_WAYS / MIN_CACHE_LINE_SIZE), however, the items not all in one tag memory block.
  23. #define MAX_TAG_BLOCK_ITEMS (MAX_CACHE_SIZE / 8 / MIN_CACHE_LINE_SIZE)
  24. #define TAG_SIZE 4
  25. #define MAX_TAG_BLOCK_SIZE (MAX_TAG_BLOCK_ITEMS * TAG_SIZE)
  26. #define ESP_CACHE_TEMP_ADDR DROM0_ADDRESS_LOW
  27. #define CACHE_MAX_OPERATION_SIZE BUS_ADDR_SIZE
  28. typedef enum {
  29. CACHE_DCACHE = 0,
  30. CACHE_ICACHE = 1,
  31. } cache_t;
  32. typedef enum {
  33. CACHE_MEMORY_INVALID = 0,
  34. CACHE_MEMORY_ICACHE_LOW = 1<<0,
  35. CACHE_MEMORY_ICACHE_HIGH = 1<<1,
  36. CACHE_MEMORY_DCACHE_LOW = 1<<2,
  37. CACHE_MEMORY_DCACHE_HIGH = 1<<3,
  38. } cache_layout_t;
  39. #define CACHE_SIZE_8KB CACHE_SIZE_HALF
  40. #define CACHE_SIZE_16KB CACHE_SIZE_FULL
  41. typedef enum {
  42. CACHE_SIZE_HALF = 0, /*!< 8KB for icache and dcache */
  43. CACHE_SIZE_FULL = 1, /*!< 16KB for icache and dcache */
  44. } cache_size_t;
  45. typedef enum {
  46. CACHE_4WAYS_ASSOC = 0, /*!< 4 way associated cache */
  47. } cache_ways_t;
  48. typedef enum {
  49. CACHE_LINE_SIZE_16B = 0, /*!< 16 Byte cache line size */
  50. CACHE_LINE_SIZE_32B = 1, /*!< 32 Byte cache line size */
  51. } cache_line_size_t;
  52. typedef enum {
  53. CACHE_AUTOLOAD_POSITIVE = 0, /*!< cache autoload step is positive */
  54. CACHE_AUTOLOAD_NEGATIVE = 1, /*!< cache autoload step is negative */
  55. } cache_autoload_order_t;
  56. #define CACHE_AUTOLOAD_STEP(i) ((i) - 1)
  57. typedef enum {
  58. CACHE_AUTOLOAD_MISS_TRIGGER = 0, /*!< autoload only triggered by cache miss */
  59. CACHE_AUTOLOAD_HIT_TRIGGER = 1, /*!< autoload only triggered by cache hit */
  60. CACHE_AUTOLOAD_BOTH_TRIGGER = 2, /*!< autoload triggered both by cache miss and hit */
  61. } cache_autoload_trigger_t;
  62. struct cache_mode {
  63. uint32_t cache_size; /*!< cache size in byte */
  64. uint16_t cache_line_size; /*!< cache line size in byte */
  65. uint8_t cache_ways; /*!< cache ways, always 4 */
  66. uint8_t icache; /*!< the cache index, 0 for dcache, 1 for icache */
  67. };
  68. struct tag_item {
  69. uint32_t dirty:1; /*!< the cache line value is dirty or not */
  70. uint32_t tag:14; /*!< the tag is the high part of the cache address, however is only 16MB range, and with out low part */
  71. uint32_t valid:1; /*!< the tag item is valid or not */
  72. uint32_t fifo_cnt:3; /*!< fifo cnt, 0 ~ 3 for 4 ways cache, 0 ~ 7 for 8 ways cache */
  73. uint32_t lock:1; /*!< the cache line is locked or not */
  74. uint32_t attr:3; /*!< the attribute of the external memory physical address */
  75. uint32_t access:1; /*!< software accessable, used by hardware */
  76. uint32_t reserved:8;
  77. };
  78. struct autoload_config {
  79. uint8_t order; /*!< autoload step is positive or negative */
  80. uint8_t trigger; /*!< autoload trigger */
  81. uint8_t ena0; /*!< autoload region0 enable */
  82. uint8_t ena1; /*!< autoload region1 enable */
  83. uint32_t addr0; /*!< autoload region0 start address */
  84. uint32_t size0; /*!< autoload region0 size */
  85. uint32_t addr1; /*!< autoload region1 start address */
  86. uint32_t size1; /*!< autoload region1 size */
  87. };
  88. struct tag_group_info {
  89. struct cache_mode mode; /*!< cache and cache mode */
  90. uint32_t filter_addr; /*!< the address that used to generate the struct */
  91. uint32_t vaddr_offset; /*!< virtual address offset of the cache ways */
  92. uint32_t tag_addr[MAX_CACHE_WAYS]; /*!< tag memory address, only [0~mode.ways-1] is valid to use */
  93. uint32_t cache_memory_offset[MAX_CACHE_WAYS]; /*!< cache memory address, only [0~mode.ways-1] is valid to use */
  94. };
  95. struct lock_config {
  96. uint32_t addr; /*!< manual lock address*/
  97. uint16_t size; /*!< manual lock size*/
  98. uint16_t group; /*!< manual lock group, 0 or 1*/
  99. };
  100. #define ESP_ROM_ERR_INVALID_ARG 1
  101. #define MMU_SET_ADDR_ALIGNED_ERROR 2
  102. #define MMU_SET_PASE_SIZE_ERROR 3
  103. #define MMU_SET_VADDR_OUT_RANGE 4
  104. /**
  105. * @brief Initialise cache mmu, mark all entries as invalid.
  106. * Please do not call this function in your SDK application.
  107. *
  108. * @param None
  109. *
  110. * @return None
  111. */
  112. void Cache_MMU_Init(void);
  113. /**
  114. * @brief Set ICache mmu mapping.
  115. * Please do not call this function in your SDK application.
  116. *
  117. * @param uint32_t ext_ram : DPORT_MMU_ACCESS_FLASH for flash, DPORT_MMU_ACCESS_SPIRAM for spiram, DPORT_MMU_INVALID for invalid.
  118. *
  119. * @param uint32_t vaddr : virtual address in CPU address space.
  120. * Can be Iram0,Iram1,Irom0,Drom0 and AHB buses address.
  121. * Should be aligned by psize.
  122. *
  123. * @param uint32_t paddr : physical address in external memory.
  124. * Should be aligned by psize.
  125. *
  126. * @param uint32_t psize : page size of ICache, in kilobytes. Should be 64 here.
  127. *
  128. * @param uint32_t num : pages to be set.
  129. *
  130. * @param uint32_t fixed : 0 for physical pages grow with virtual pages, other for virtual pages map to same physical page.
  131. *
  132. * @return uint32_t: error status
  133. * 0 : mmu set success
  134. * 2 : vaddr or paddr is not aligned
  135. * 3 : psize error
  136. * 4 : vaddr is out of range
  137. */
  138. int Cache_Ibus_MMU_Set(uint32_t ext_ram, uint32_t vaddr, uint32_t paddr, uint32_t psize, uint32_t num, uint32_t fixed);
  139. /**
  140. * @brief Set DCache mmu mapping.
  141. * Please do not call this function in your SDK application.
  142. *
  143. * @param uint32_t ext_ram : DPORT_MMU_ACCESS_FLASH for flash, DPORT_MMU_ACCESS_SPIRAM for spiram, DPORT_MMU_INVALID for invalid.
  144. *
  145. * @param uint32_t vaddr : virtual address in CPU address space.
  146. * Can be DRam0, DRam1, DRom0, DPort and AHB buses address.
  147. * Should be aligned by psize.
  148. *
  149. * @param uint32_t paddr : physical address in external memory.
  150. * Should be aligned by psize.
  151. *
  152. * @param uint32_t psize : page size of DCache, in kilobytes. Should be 64 here.
  153. *
  154. * @param uint32_t num : pages to be set.
  155. * @param uint32_t fixed : 0 for physical pages grow with virtual pages, other for virtual pages map to same physical page.
  156. *
  157. * @return uint32_t: error status
  158. * 0 : mmu set success
  159. * 2 : vaddr or paddr is not aligned
  160. * 3 : psize error
  161. * 4 : vaddr is out of range
  162. */
  163. int Cache_Dbus_MMU_Set(uint32_t ext_ram, uint32_t vaddr, uint32_t paddr, uint32_t psize, uint32_t num, uint32_t fixed);
  164. /**
  165. * @brief Count the pages in the bus room address which map to Flash.
  166. * Please do not call this function in your SDK application.
  167. *
  168. * @param uint32_t bus : the bus to count with.
  169. *
  170. * @param uint32_t * page0_mapped : value should be initial by user, 0 for not mapped, other for mapped count.
  171. *
  172. * return uint32_t : the number of pages which map to Flash.
  173. */
  174. uint32_t Cache_Count_Flash_Pages(uint32_t bus, uint32_t * page0_mapped);
  175. /**
  176. * @brief Copy Instruction or rodata from Flash to SPIRAM, and remap to SPIRAM.
  177. * Please do not call this function in your SDK application.
  178. *
  179. * @param uint32_t bus : the bus which need to copy to SPIRAM.
  180. *
  181. * @param uint32_t bus_start_addr : the start virtual address for the bus.
  182. *
  183. * @param uint32_t start_page : the start (64KB) page number in SPIRAM.
  184. *
  185. * @param uint32_t * page0_page : the flash page0 in SPIRAM page number, 0xffff for invalid.
  186. *
  187. * return uint32_t : the next start page number for SPIRAM not mapped.
  188. */
  189. uint32_t Cache_Flash_To_SPIRAM_Copy(uint32_t bus, uint32_t bus_start_addr, uint32_t start_page, uint32_t * page0_page);
  190. /**
  191. * @brief allocate memory to used by ICache and DCache.
  192. * Please do not call this function in your SDK application.
  193. *
  194. * @param cache_layout_t sram0_layout : the usage of first 8KB internal memory block, can be CACHE_MEMORY_INVALID, CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW and CACHE_MEMORY_DCACHE_HIGH
  195. *
  196. * @param cache_layout_t sram1_layout : the usage of second 8KB internal memory block
  197. *
  198. * @param cache_layout_t sram2_layout : the usage of third 8KB internal memory block
  199. *
  200. * @param cache_layout_t sram3_layout : the usage of forth 8KB internal memory block
  201. *
  202. * return none
  203. */
  204. void Cache_Allocate_SRAM(cache_layout_t sram0_layout, cache_layout_t sram1_layout, cache_layout_t sram2_layout, cache_layout_t sram3_layout);
  205. /**
  206. * @brief Get cache mode of ICache or DCache.
  207. * Please do not call this function in your SDK application.
  208. *
  209. * @param struct cache_mode * mode : the pointer of cache mode struct, caller should set the icache field
  210. *
  211. * return none
  212. */
  213. void Cache_Get_Mode(struct cache_mode * mode);
  214. /**
  215. * @brief set ICache modes: cache size, associate ways and cache line size.
  216. * Please do not call this function in your SDK application.
  217. *
  218. * @param cache_size_t cache_size : the cache size, can be CACHE_SIZE_HALF and CACHE_SIZE_FULL
  219. *
  220. * @param cache_ways_t ways : the associate ways of cache, can only be CACHE_4WAYS_ASSOC
  221. *
  222. * @param cache_line_size_t cache_line_size : the cache line size, can be CACHE_LINE_SIZE_16B, CACHE_LINE_SIZE_32B
  223. *
  224. * return none
  225. */
  226. void Cache_Set_ICache_Mode(cache_size_t cache_size, cache_ways_t ways, cache_line_size_t cache_line_size);
  227. /**
  228. * @brief set DCache modes: cache size, associate ways and cache line size.
  229. * Please do not call this function in your SDK application.
  230. *
  231. * @param cache_size_t cache_size : the cache size, can be CACHE_SIZE_HALF and CACHE_SIZE_FULL
  232. *
  233. * @param cache_ways_t ways : the associate ways of cache, can only be CACHE_4WAYS_ASSOC
  234. *
  235. * @param cache_line_size_t cache_line_size : the cache line size, can be CACHE_LINE_SIZE_16B, CACHE_LINE_SIZE_32B
  236. *
  237. * return none
  238. */
  239. void Cache_Set_DCache_Mode(cache_size_t cache_size, cache_ways_t ways, cache_line_size_t cache_line_size);
  240. /**
  241. * @brief check if the address is accessed through ICache.
  242. * Please do not call this function in your SDK application.
  243. *
  244. * @param uint32_t addr : the address to check.
  245. *
  246. * @return 1 if the address is accessed through ICache, 0 if not.
  247. */
  248. uint32_t Cache_Address_Through_ICache(uint32_t addr);
  249. /**
  250. * @brief check if the address is accessed through DCache.
  251. * Please do not call this function in your SDK application.
  252. *
  253. * @param uint32_t addr : the address to check.
  254. *
  255. * @return 1 if the address is accessed through DCache, 0 if not.
  256. */
  257. uint32_t Cache_Address_Through_DCache(uint32_t addr);
  258. /**
  259. * @brief Invalidate the cache items for ICache.
  260. * Operation will be done CACHE_LINE_SIZE aligned.
  261. * If the region is not in ICache addr room, nothing will be done.
  262. * Please do not call this function in your SDK application.
  263. *
  264. * @param uint32_t addr: start address to invalidate
  265. *
  266. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(4MB)
  267. *
  268. * @return None
  269. */
  270. void Cache_Invalidate_ICache_Items(uint32_t addr, uint32_t items);
  271. /**
  272. * @brief Invalidate the cache items for DCache.
  273. * Operation will be done CACHE_LINE_SIZE aligned.
  274. * If the region is not in DCache addr room, nothing will be done.
  275. * Please do not call this function in your SDK application.
  276. *
  277. * @param uint32_t addr: start address to invalidate
  278. *
  279. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(4MB)
  280. *
  281. * @return None
  282. */
  283. void Cache_Invalidate_DCache_Items(uint32_t addr, uint32_t items);
  284. /**
  285. * @brief Clean the dirty bit of cache Items of DCache.
  286. * Operation will be done CACHE_LINE_SIZE aligned.
  287. * If the region is not in DCache addr room, nothing will be done.
  288. * Please do not call this function in your SDK application.
  289. *
  290. * @param uint32_t addr: start address to Clean
  291. *
  292. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(4MB)
  293. *
  294. * @return None
  295. */
  296. void Cache_Clean_Items(uint32_t addr, uint32_t items);
  297. /**
  298. * @brief Write back the cache items of DCache.
  299. * Operation will be done CACHE_LINE_SIZE aligned.
  300. * If the region is not in DCache addr room, nothing will be done.
  301. * Please do not call this function in your SDK application.
  302. *
  303. * @param uint32_t addr: start address to write back
  304. *
  305. * @param uint32_t items: cache lines to invalidate, items * cache_line_size should not exceed the bus address size(4MB)
  306. *
  307. * @return None
  308. */
  309. void Cache_WriteBack_Items(uint32_t addr, uint32_t items);
  310. /**
  311. * @brief Invalidate the Cache items in the region from ICache or DCache.
  312. * If the region is not in Cache addr room, nothing will be done.
  313. * Please do not call this function in your SDK application.
  314. *
  315. * @param uint32_t addr : invalidated region start address.
  316. *
  317. * @param uint32_t size : invalidated region size.
  318. *
  319. * @return 0 for success
  320. * 1 for invalid argument
  321. */
  322. int Cache_Invalidate_Addr(uint32_t addr, uint32_t size);
  323. /**
  324. * @brief Clean the dirty bit of Cache items in the region from DCache.
  325. * If the region is not in DCache addr room, nothing will be done.
  326. * Please do not call this function in your SDK application.
  327. *
  328. * @param uint32_t addr : cleaned region start address.
  329. *
  330. * @param uint32_t size : cleaned region size.
  331. *
  332. * @return 0 for success
  333. * 1 for invalid argument
  334. */
  335. int Cache_Clean_Addr(uint32_t addr, uint32_t size);
  336. /**
  337. * @brief Writeback the Cache items(also clean the dirty bit) in the region from DCache.
  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 : writeback region start address.
  342. *
  343. * @param uint32_t size : writeback region size.
  344. *
  345. * @return 0 for success
  346. * 1 for invalid argument
  347. */
  348. int Cache_WriteBack_Addr(uint32_t addr, uint32_t size);
  349. /**
  350. * @brief Invalidate all cache items in ICache.
  351. * Please do not call this function in your SDK application.
  352. *
  353. * @param None
  354. *
  355. * @return None
  356. */
  357. void Cache_Invalidate_ICache_All(void);
  358. /**
  359. * @brief Invalidate all cache items in DCache.
  360. * Please do not call this function in your SDK application.
  361. *
  362. * @param None
  363. *
  364. * @return None
  365. */
  366. void Cache_Invalidate_DCache_All(void);
  367. /**
  368. * @brief Clean the dirty bit of all cache items in DCache.
  369. * Please do not call this function in your SDK application.
  370. *
  371. * @param None
  372. *
  373. * @return None
  374. */
  375. void Cache_Clean_All(void);
  376. /**
  377. * @brief WriteBack all cache items in DCache.
  378. * Please do not call this function in your SDK application.
  379. *
  380. * @param None
  381. *
  382. * @return None
  383. */
  384. void Cache_WriteBack_All(void);
  385. /**
  386. * @brief Mask all buses through ICache and DCache.
  387. * Please do not call this function in your SDK application.
  388. *
  389. * @param None
  390. *
  391. * @return None
  392. */
  393. void Cache_Mask_All(void);
  394. /**
  395. * @brief UnMask DRom0 bus through ICache.
  396. * Please do not call this function in your SDK application.
  397. *
  398. * @param None
  399. *
  400. * @return None
  401. */
  402. void Cache_UnMask_Drom0(void);
  403. /**
  404. * @brief Suspend ICache auto preload operation, then you can resume it after some ICache operations.
  405. * Please do not call this function in your SDK application.
  406. *
  407. * @param None
  408. *
  409. * @return uint32_t : 0 for ICache not auto preload before suspend.
  410. */
  411. uint32_t Cache_Suspend_ICache_Autoload(void);
  412. /**
  413. * @brief Resume ICache auto preload operation after some ICache operations.
  414. * Please do not call this function in your SDK application.
  415. *
  416. * @param uint32_t autoload : 0 for ICache not auto preload before suspend.
  417. *
  418. * @return None.
  419. */
  420. void Cache_Resume_ICache_Autoload(uint32_t autoload);
  421. /**
  422. * @brief Suspend DCache auto preload operation, then you can resume it after some DCache operations.
  423. * Please do not call this function in your SDK application.
  424. *
  425. * @param None
  426. *
  427. * @return uint32_t : 0 for DCache not auto preload before suspend.
  428. */
  429. uint32_t Cache_Suspend_DCache_Autoload(void);
  430. /**
  431. * @brief Resume DCache auto preload operation after some DCache operations.
  432. * Please do not call this function in your SDK application.
  433. *
  434. * @param uint32_t autoload : 0 for DCache not auto preload before suspend.
  435. *
  436. * @return None.
  437. */
  438. void Cache_Resume_DCache_Autoload(uint32_t autoload);
  439. /**
  440. * @brief Start an ICache manual preload, will suspend auto preload of ICache.
  441. * Please do not call this function in your SDK application.
  442. *
  443. * @param uint32_t addr : start address of the preload region.
  444. *
  445. * @param uint32_t size : size of the preload region, should not exceed the size of ICache.
  446. *
  447. * @param uint32_t order : the preload order, 0 for positive, other for negative
  448. *
  449. * @return uint32_t : 0 for ICache not auto preload before manual preload.
  450. */
  451. uint32_t Cache_Start_ICache_Preload(uint32_t addr, uint32_t size, uint32_t order);
  452. /**
  453. * @brief Return if the ICache manual preload done.
  454. * Please do not call this function in your SDK application.
  455. *
  456. * @param None
  457. *
  458. * @return uint32_t : 0 for ICache manual preload not done.
  459. */
  460. uint32_t Cache_ICache_Preload_Done(void);
  461. /**
  462. * @brief End the ICache manual preload to resume auto preload of ICache.
  463. * Please do not call this function in your SDK application.
  464. *
  465. * @param uint32_t autoload : 0 for ICache not auto preload before manual preload.
  466. *
  467. * @return None
  468. */
  469. void Cache_End_ICache_Preload(uint32_t autoload);
  470. /**
  471. * @brief Start an DCache manual preload, will suspend auto preload of DCache.
  472. * Please do not call this function in your SDK application.
  473. *
  474. * @param uint32_t addr : start address of the preload region.
  475. *
  476. * @param uint32_t size : size of the preload region, should not exceed the size of DCache.
  477. *
  478. * @param uint32_t order : the preload order, 0 for positive, other for negative
  479. *
  480. * @return uint32_t : 0 for DCache not auto preload before manual preload.
  481. */
  482. uint32_t Cache_Start_DCache_Preload(uint32_t addr, uint32_t size, uint32_t order);
  483. /**
  484. * @brief Return if the DCache manual preload done.
  485. * Please do not call this function in your SDK application.
  486. *
  487. * @param None
  488. *
  489. * @return uint32_t : 0 for DCache manual preload not done.
  490. */
  491. uint32_t Cache_DCache_Preload_Done(void);
  492. /**
  493. * @brief End the DCache manual preload to resume auto preload of DCache.
  494. * Please do not call this function in your SDK application.
  495. *
  496. * @param uint32_t autoload : 0 for DCache not auto preload before manual preload.
  497. *
  498. * @return None
  499. */
  500. void Cache_End_DCache_Preload(uint32_t autoload);
  501. /**
  502. * @brief Config autoload parameters of ICache.
  503. * Please do not call this function in your SDK application.
  504. *
  505. * @param struct autoload_config * config : autoload parameters.
  506. *
  507. * @return None
  508. */
  509. void Cache_Config_ICache_Autoload(const struct autoload_config * config);
  510. /**
  511. * @brief Enable auto preload for ICache.
  512. * Please do not call this function in your SDK application.
  513. *
  514. * @param None
  515. *
  516. * @return None
  517. */
  518. void Cache_Enable_ICache_Autoload(void);
  519. /**
  520. * @brief Disable auto preload for ICache.
  521. * Please do not call this function in your SDK application.
  522. *
  523. * @param None
  524. *
  525. * @return None
  526. */
  527. void Cache_Disable_ICache_Autoload(void);
  528. /**
  529. * @brief Config autoload parameters of DCache.
  530. * Please do not call this function in your SDK application.
  531. *
  532. * @param struct autoload_config * config : autoload parameters.
  533. *
  534. * @return None
  535. */
  536. void Cache_Config_DCache_Autoload(const struct autoload_config * config);
  537. /**
  538. * @brief Enable auto preload for DCache.
  539. * Please do not call this function in your SDK application.
  540. *
  541. * @param None
  542. *
  543. * @return None
  544. */
  545. void Cache_Enable_DCache_Autoload(void);
  546. /**
  547. * @brief Disable auto preload for DCache.
  548. * Please do not call this function in your SDK application.
  549. *
  550. * @param None
  551. *
  552. * @return None
  553. */
  554. void Cache_Disable_DCache_Autoload(void);
  555. /**
  556. * @brief Config a group of prelock parameters of ICache.
  557. * Please do not call this function in your SDK application.
  558. *
  559. * @param struct lock_config * config : a group of lock parameters.
  560. *
  561. * @return None
  562. */
  563. void Cache_Enable_ICache_PreLock(const struct lock_config *config);
  564. /**
  565. * @brief Disable a group of prelock parameters for ICache.
  566. * However, the locked data will not be released.
  567. * Please do not call this function in your SDK application.
  568. *
  569. * @param uint16_t group : 0 for group0, 1 for group1.
  570. *
  571. * @return None
  572. */
  573. void Cache_Disable_ICache_PreLock(uint16_t group);
  574. /**
  575. * @brief Lock the cache items for ICache.
  576. * Operation will be done CACHE_LINE_SIZE aligned.
  577. * If the region is not in ICache addr room, nothing will be done.
  578. * Please do not call this function in your SDK application.
  579. *
  580. * @param uint32_t addr: start address to lock
  581. *
  582. * @param uint32_t items: cache lines to lock, items * cache_line_size should not exceed the bus address size(4MB)
  583. *
  584. * @return None
  585. */
  586. void Cache_Lock_ICache_Items(uint32_t addr, uint32_t items);
  587. /**
  588. * @brief Unlock the cache items for ICache.
  589. * Operation will be done CACHE_LINE_SIZE aligned.
  590. * If the region is not in ICache addr room, nothing will be done.
  591. * Please do not call this function in your SDK application.
  592. *
  593. * @param uint32_t addr: start address to unlock
  594. *
  595. * @param uint32_t items: cache lines to unlock, items * cache_line_size should not exceed the bus address size(4MB)
  596. *
  597. * @return None
  598. */
  599. void Cache_Unlock_ICache_Items(uint32_t addr, uint32_t items);
  600. /**
  601. * @brief Config a group of prelock parameters of DCache.
  602. * Please do not call this function in your SDK application.
  603. *
  604. * @param struct lock_config * config : a group of lock parameters.
  605. *
  606. * @return None
  607. */
  608. void Cache_Enable_DCache_PreLock(const struct lock_config *config);
  609. /**
  610. * @brief Disable a group of prelock parameters for DCache.
  611. * However, the locked data will not be released.
  612. * Please do not call this function in your SDK application.
  613. *
  614. * @param uint16_t group : 0 for group0, 1 for group1.
  615. *
  616. * @return None
  617. */
  618. void Cache_Disable_DCache_PreLock(uint16_t group);
  619. /**
  620. * @brief Lock the cache items for DCache.
  621. * Operation will be done CACHE_LINE_SIZE aligned.
  622. * If the region is not in DCache addr room, nothing will be done.
  623. * Please do not call this function in your SDK application.
  624. *
  625. * @param uint32_t addr: start address to lock
  626. *
  627. * @param uint32_t items: cache lines to lock, items * cache_line_size should not exceed the bus address size(4MB)
  628. *
  629. * @return None
  630. */
  631. void Cache_Lock_DCache_Items(uint32_t addr, uint32_t items);
  632. /**
  633. * @brief Unlock the cache items for DCache.
  634. * Operation will be done CACHE_LINE_SIZE aligned.
  635. * If the region is not in DCache addr room, nothing will be done.
  636. * Please do not call this function in your SDK application.
  637. *
  638. * @param uint32_t addr: start address to unlock
  639. *
  640. * @param uint32_t items: cache lines to unlock, items * cache_line_size should not exceed the bus address size(4MB)
  641. *
  642. * @return None
  643. */
  644. void Cache_Unlock_DCache_Items(uint32_t addr, uint32_t items);
  645. /**
  646. * @brief Lock the cache items in tag memory for ICache or DCache.
  647. * Please do not call this function in your SDK application.
  648. *
  649. * @param uint32_t addr : start address of lock region.
  650. *
  651. * @param uint32_t size : size of lock region.
  652. *
  653. * @return 0 for success
  654. * 1 for invalid argument
  655. */
  656. int Cache_Lock_Addr(uint32_t addr, uint32_t size);
  657. /**
  658. * @brief Unlock the cache items in tag memory for ICache or DCache.
  659. * Please do not call this function in your SDK application.
  660. *
  661. * @param uint32_t addr : start address of unlock region.
  662. *
  663. * @param uint32_t size : size of unlock region.
  664. *
  665. * @return 0 for success
  666. * 1 for invalid argument
  667. */
  668. int Cache_Unlock_Addr(uint32_t addr, uint32_t size);
  669. /**
  670. * @brief Disable ICache access for the cpu.
  671. * This operation will make all ICache tag memory invalid, CPU can't access ICache, ICache will keep idle.
  672. * Please do not call this function in your SDK application.
  673. *
  674. * @return uint32_t : auto preload enabled before
  675. */
  676. uint32_t Cache_Disable_ICache(void);
  677. /**
  678. * @brief Enable ICache access for the cpu.
  679. * Please do not call this function in your SDK application.
  680. *
  681. * @param uint32_t autoload : ICache will preload then.
  682. *
  683. * @return None
  684. */
  685. void Cache_Enable_ICache(uint32_t autoload);
  686. /**
  687. * @brief Disable DCache access for the cpu.
  688. * This operation will make all DCache tag memory invalid, CPU can't access DCache, DCache will keep idle
  689. * Please do not call this function in your SDK application.
  690. *
  691. * @return uint32_t : auto preload enabled before
  692. */
  693. uint32_t Cache_Disable_DCache(void);
  694. /**
  695. * @brief Enable DCache access for the cpu.
  696. * Please do not call this function in your SDK application.
  697. *
  698. * @param uint32_t autoload : DCache will preload then.
  699. *
  700. * @return None
  701. */
  702. void Cache_Enable_DCache(uint32_t autoload);
  703. /**
  704. * @brief Suspend ICache access for the cpu.
  705. * The ICache tag memory is still there, CPU can't access ICache, ICache will keep idle.
  706. * Please do not change MMU, cache mode or tag memory(tag memory can be changed in some special case).
  707. * Please do not call this function in your SDK application.
  708. *
  709. * @param None
  710. *
  711. * @return uint32_t : auto preload enabled before
  712. */
  713. uint32_t Cache_Suspend_ICache(void);
  714. /**
  715. * @brief Resume ICache access for the cpu.
  716. * Please do not call this function in your SDK application.
  717. *
  718. * @param uint32_t autoload : ICache will preload then.
  719. *
  720. * @return None
  721. */
  722. void Cache_Resume_ICache(uint32_t autoload);
  723. /**
  724. * @brief Suspend DCache access for the cpu.
  725. * The ICache tag memory is still there, CPU can't access DCache, DCache will keep idle.
  726. × Please do not change MMU, cache mode or tag memory(tag memory can be changed in some special case).
  727. * Please do not call this function in your SDK application.
  728. *
  729. * @param None
  730. *
  731. * @return uint32_t : auto preload enabled before
  732. */
  733. uint32_t Cache_Suspend_DCache(void);
  734. /**
  735. * @brief Resume DCache access for the cpu.
  736. * Please do not call this function in your SDK application.
  737. *
  738. * @param uint32_t autoload : DCache will preload then.
  739. *
  740. * @return None
  741. */
  742. void Cache_Resume_DCache(uint32_t autoload);
  743. /**
  744. * @brief Get ICache cache line size
  745. *
  746. * @param None
  747. *
  748. * @return uint32_t: 16, 32 Byte
  749. */
  750. uint32_t Cache_Get_ICache_Line_Size(void);
  751. /**
  752. * @brief Get DCache cache line size
  753. *
  754. * @param None
  755. *
  756. * @return uint32_t: 16, 32 Byte
  757. */
  758. uint32_t Cache_Get_DCache_Line_Size(void);
  759. /**
  760. * @brief Set default mode from boot, 8KB ICache, 16Byte cache line size.
  761. *
  762. * @param None
  763. *
  764. * @return None
  765. */
  766. void Cache_Set_Default_Mode(void);
  767. /**
  768. * @brief Set default mode from boot, 8KB DCache, 16Byte cache line size.
  769. *
  770. * @param None
  771. *
  772. * @return None
  773. */
  774. void Cache_Enable_Defalut_DCache_Mode(void);
  775. /**
  776. * @brief Travel tag memory to run a call back function.
  777. * ICache and DCache are suspend when doing this.
  778. * The callback will get the parameter tag_group_info, which will include a group of tag memory addresses and cache memory addresses.
  779. * Please do not call this function in your SDK application.
  780. *
  781. * @param struct cache_mode * mode : the cache to check and the cache mode.
  782. *
  783. * @param uint32_t filter_addr : only the cache lines which may include the filter_address will be returned to the call back function.
  784. * 0 for do not filter, all cache lines will be returned.
  785. *
  786. * @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.
  787. *
  788. * @return None
  789. */
  790. void Cache_Travel_Tag_Memory(struct cache_mode * mode, uint32_t filter_addr, void (* process)(struct tag_group_info *));
  791. /**
  792. * @brief Get the virtual address from cache mode, cache tag and the virtual address offset of cache ways.
  793. * Please do not call this function in your SDK application.
  794. *
  795. * @param struct cache_mode * mode : the cache to calculate the virtual address and the cache mode.
  796. *
  797. * @param uint32_t tag : the tag part fo a tag item, 12-14 bits.
  798. *
  799. * @param uint32_t addr_offset : the virtual address offset of the cache ways.
  800. *
  801. * @return uint32_t : the virtual address.
  802. */
  803. uint32_t Cache_Get_Virtual_Addr(struct cache_mode *mode, uint32_t tag, uint32_t vaddr_offset);
  804. /**
  805. * @brief Get cache memory block base address.
  806. * Please do not call this function in your SDK application.
  807. *
  808. * @param uint32_t icache : 0 for dcache, other for icache.
  809. *
  810. * @param uint32_t high : 0 for low part block, 1 for high part block.
  811. *
  812. * @return uint32_t : the cache memory block base address, 0 if the block not used.
  813. */
  814. uint32_t Cache_Get_Memory_BaseAddr(uint32_t icache, uint32_t high);
  815. /**
  816. * @brief Get the cache memory address from cache mode, cache memory offset and the virtual address offset of cache ways.
  817. * Please do not call this function in your SDK application.
  818. *
  819. * @param struct cache_mode * mode : the cache to calculate the virtual address and the cache mode.
  820. *
  821. * @param uint32_t cache_memory_offset : the cache memory offset of the whole cache (ICache or DCache) for the cache line.
  822. *
  823. * @param uint32_t addr_offset : the virtual address offset of the cache ways.
  824. *
  825. * @return uint32_t : the virtual address.
  826. */
  827. uint32_t Cache_Get_Memory_Addr(struct cache_mode *mode, uint32_t cache_memory_offset, uint32_t vaddr_offset);
  828. /**
  829. * @brief Get the cache memory value by DRAM address.
  830. * Please do not call this function in your SDK application.
  831. *
  832. * @param uint32_t cache_memory_addr : DRAM address for the cache memory.
  833. *
  834. * @return uint32_t : the word value of the address.
  835. */
  836. uint32_t Cache_Get_Memory_value(uint32_t cache_memory_addr);
  837. /**
  838. * @}
  839. */
  840. #ifdef __cplusplus
  841. }
  842. #endif
  843. #endif /* _ROM_CACHE_H_ */