cache.h 32 KB

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