spiram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. Abstraction layer for spi-ram. For now, it's no more than a stub for the spiram_psram functions, but if
  3. we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
  4. */
  5. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <sys/param.h>
  21. #include "sdkconfig.h"
  22. #include "esp_attr.h"
  23. #include "esp_err.h"
  24. #include "esp32s3/spiram.h"
  25. #include "spiram_psram.h"
  26. #include "esp_log.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/xtensa_api.h"
  29. #include "soc/soc.h"
  30. #include "esp_heap_caps_init.h"
  31. #include "soc/soc_memory_layout.h"
  32. #include "soc/dport_reg.h"
  33. #include "esp32s3/rom/cache.h"
  34. #include "soc/cache_memory.h"
  35. #include "soc/extmem_reg.h"
  36. #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
  37. #if CONFIG_SPIRAM
  38. static const char *TAG = "spiram";
  39. #if CONFIG_SPIRAM_SPEED_40M
  40. #define PSRAM_SPEED PSRAM_CACHE_S40M
  41. #elif CONFIG_SPIRAM_SPEED_80M
  42. #define PSRAM_SPEED PSRAM_CACHE_S80M
  43. #else
  44. #define PSRAM_SPEED PSRAM_CACHE_S20M
  45. #endif
  46. static bool spiram_inited = false;
  47. /*
  48. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  49. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  50. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  51. */
  52. bool esp_spiram_test(void)
  53. {
  54. size_t spiram_size = esp_spiram_get_size();
  55. volatile int *spiram = (volatile int *)(SOC_EXTRAM_DATA_HIGH - spiram_size);
  56. size_t p;
  57. size_t s = spiram_size;
  58. int errct = 0;
  59. int initial_err = -1;
  60. if (SOC_EXTRAM_DATA_SIZE < spiram_size) {
  61. ESP_EARLY_LOGW(TAG, "Only test spiram from %08x to %08x\n", SOC_EXTRAM_DATA_LOW, SOC_EXTRAM_DATA_HIGH);
  62. spiram = (volatile int *)SOC_EXTRAM_DATA_LOW;
  63. s = SOC_EXTRAM_DATA_HIGH - SOC_EXTRAM_DATA_LOW;
  64. }
  65. for (p = 0; p < (s / sizeof(int)); p += 8) {
  66. spiram[p] = p ^ 0xAAAAAAAA;
  67. }
  68. for (p = 0; p < (s / sizeof(int)); p += 8) {
  69. if (spiram[p] != (p ^ 0xAAAAAAAA)) {
  70. errct++;
  71. if (errct == 1) {
  72. initial_err = p * 4;
  73. }
  74. if (errct < 4) {
  75. ESP_EARLY_LOGE(TAG, "SPI SRAM error@%08x:%08x/%08x \n", &spiram[p], spiram[p], p ^ 0xAAAAAAAA);
  76. }
  77. }
  78. }
  79. if (errct) {
  80. ESP_EARLY_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, s / 32, initial_err + SOC_EXTRAM_DATA_LOW);
  81. return false;
  82. } else {
  83. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  84. return true;
  85. }
  86. }
  87. void IRAM_ATTR esp_spiram_init_cache(void)
  88. {
  89. size_t spiram_size = esp_spiram_get_size();
  90. Cache_Suspend_DCache();
  91. if ((SOC_EXTRAM_DATA_HIGH - SOC_EXTRAM_DATA_LOW) >= spiram_size) {
  92. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SOC_EXTRAM_DATA_HIGH - spiram_size, 0, 64, spiram_size >> 16, 0);
  93. } else {
  94. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SOC_EXTRAM_DATA_HIGH - spiram_size, 0, 64, (SOC_EXTRAM_DATA_HIGH - SOC_EXTRAM_DATA_LOW) >> 16, 0);
  95. }
  96. REG_CLR_BIT(EXTMEM_DCACHE_CTRL1_REG, EXTMEM_DCACHE_SHUT_CORE0_BUS);
  97. #if !CONFIG_FREERTOS_UNICORE
  98. REG_CLR_BIT(EXTMEM_DCACHE_CTRL1_REG, EXTMEM_DCACHE_SHUT_CORE1_BUS);
  99. #endif
  100. Cache_Resume_DCache(0);
  101. }
  102. static uint32_t pages_for_flash = 0;
  103. static uint32_t instruction_in_spiram = 0;
  104. static uint32_t rodata_in_spiram = 0;
  105. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  106. static int instr_flash2spiram_offs = 0;
  107. static uint32_t instr_start_page = 0;
  108. static uint32_t instr_end_page = 0;
  109. #endif
  110. #if CONFIG_SPIRAM_RODATA
  111. static int rodata_flash2spiram_offs = 0;
  112. static uint32_t rodata_start_page = 0;
  113. static uint32_t rodata_end_page = 0;
  114. #endif
  115. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
  116. static uint32_t page0_mapped = 0;
  117. static uint32_t page0_page = INVALID_PHY_PAGE;
  118. #endif
  119. uint32_t esp_spiram_instruction_access_enabled(void)
  120. {
  121. return instruction_in_spiram;
  122. }
  123. uint32_t esp_spiram_rodata_access_enabled(void)
  124. {
  125. return rodata_in_spiram;
  126. }
  127. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  128. esp_err_t esp_spiram_enable_instruction_access(void)
  129. {
  130. size_t spiram_size = esp_spiram_get_size();
  131. uint32_t pages_in_flash = 0;
  132. pages_in_flash += Cache_Count_Flash_Pages(CACHE_IBUS, &page0_mapped);
  133. if ((pages_in_flash + pages_for_flash) > (spiram_size >> 16)) {
  134. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the instructions, has %d pages, need %d pages.", (spiram_size >> 16), (pages_in_flash + pages_for_flash));
  135. return ESP_FAIL;
  136. }
  137. ESP_EARLY_LOGI(TAG, "Instructions copied and mapped to SPIRAM");
  138. uint32_t mmu_value = *(volatile uint32_t *)(DR_REG_MMU_TABLE + CACHE_IROM_MMU_START);
  139. instr_flash2spiram_offs = mmu_value - pages_for_flash;
  140. ESP_EARLY_LOGV(TAG, "Instructions from flash page%d copy to SPIRAM page%d, Offset: %d", mmu_value, pages_for_flash, instr_flash2spiram_offs);
  141. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(CACHE_IBUS, IRAM0_CACHE_ADDRESS_LOW, pages_for_flash, &page0_page);
  142. instruction_in_spiram = 1;
  143. return ESP_OK;
  144. }
  145. #endif
  146. #if CONFIG_SPIRAM_RODATA
  147. esp_err_t esp_spiram_enable_rodata_access(void)
  148. {
  149. size_t spiram_size = esp_spiram_get_size();
  150. uint32_t pages_in_flash = 0;
  151. pages_in_flash += Cache_Count_Flash_Pages(CACHE_DBUS, &page0_mapped);
  152. if ((pages_in_flash + pages_for_flash) > (spiram_size >> 16)) {
  153. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the read only data.");
  154. return ESP_FAIL;
  155. }
  156. ESP_EARLY_LOGI(TAG, "Read only data copied and mapped to SPIRAM");
  157. uint32_t mmu_value = *(volatile uint32_t *)(DR_REG_MMU_TABLE + CACHE_DROM_MMU_START);
  158. rodata_flash2spiram_offs = mmu_value - pages_for_flash;
  159. ESP_EARLY_LOGV(TAG, "Rodata from flash page%d copy to SPIRAM page%d, Offset: %d", mmu_value, pages_for_flash, rodata_flash2spiram_offs);
  160. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(CACHE_DBUS, DRAM0_CACHE_ADDRESS_LOW, pages_for_flash, &page0_page);
  161. rodata_in_spiram = 1;
  162. return ESP_OK;
  163. }
  164. #endif
  165. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  166. void instruction_flash_page_info_init(void)
  167. {
  168. uint32_t instr_page_cnt = ((uint32_t)&_instruction_reserved_end - SOC_IROM_LOW + MMU_PAGE_SIZE - 1) / MMU_PAGE_SIZE;
  169. instr_start_page = *(volatile uint32_t *)(DR_REG_MMU_TABLE + CACHE_IROM_MMU_START);
  170. instr_start_page &= MMU_ADDRESS_MASK;
  171. instr_end_page = instr_start_page + instr_page_cnt - 1;
  172. }
  173. uint32_t IRAM_ATTR instruction_flash_start_page_get(void)
  174. {
  175. return instr_start_page;
  176. }
  177. uint32_t IRAM_ATTR instruction_flash_end_page_get(void)
  178. {
  179. return instr_end_page;
  180. }
  181. int IRAM_ATTR instruction_flash2spiram_offset(void)
  182. {
  183. return instr_flash2spiram_offs;
  184. }
  185. #endif
  186. #if CONFIG_SPIRAM_RODATA
  187. void rodata_flash_page_info_init(void)
  188. {
  189. uint32_t rodata_page_cnt = ((uint32_t)&_rodata_reserved_end - ((uint32_t)&_rodata_reserved_start & ~ (MMU_PAGE_SIZE - 1)) + MMU_PAGE_SIZE - 1) / MMU_PAGE_SIZE;
  190. rodata_start_page = *(volatile uint32_t *)(DR_REG_MMU_TABLE + CACHE_DROM_MMU_START);
  191. rodata_start_page &= MMU_ADDRESS_MASK;
  192. rodata_end_page = rodata_start_page + rodata_page_cnt - 1;
  193. }
  194. uint32_t IRAM_ATTR rodata_flash_start_page_get(void)
  195. {
  196. return rodata_start_page;
  197. }
  198. uint32_t IRAM_ATTR rodata_flash_end_page_get(void)
  199. {
  200. return rodata_end_page;
  201. }
  202. int IRAM_ATTR rodata_flash2spiram_offset(void)
  203. {
  204. return rodata_flash2spiram_offs;
  205. }
  206. #endif
  207. esp_err_t esp_spiram_init(void)
  208. {
  209. esp_err_t r;
  210. r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
  211. if (r != ESP_OK) {
  212. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  213. ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
  214. #endif
  215. return r;
  216. }
  217. spiram_inited = true;
  218. #if (CONFIG_SPIRAM_SIZE != -1)
  219. if (esp_spiram_get_size() != CONFIG_SPIRAM_SIZE) {
  220. ESP_EARLY_LOGE(TAG, "Expected %dKiB chip but found %dKiB chip. Bailing out..", CONFIG_SPIRAM_SIZE / 1024, esp_spiram_get_size() / 1024);
  221. return ESP_ERR_INVALID_SIZE;
  222. }
  223. #endif
  224. ESP_EARLY_LOGI(TAG, "Found %dMBit SPI RAM device",
  225. (esp_spiram_get_size() * 8) / (1024 * 1024));
  226. ESP_EARLY_LOGI(TAG, "SPI RAM mode: %s", PSRAM_SPEED == PSRAM_CACHE_S40M ? "sram 40m" : \
  227. PSRAM_SPEED == PSRAM_CACHE_S80M ? "sram 80m" : "sram 20m");
  228. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
  229. (PSRAM_MODE == PSRAM_VADDR_MODE_EVENODD) ? "even/odd (2-core)" : \
  230. (PSRAM_MODE == PSRAM_VADDR_MODE_LOWHIGH) ? "low/high (2-core)" : \
  231. (PSRAM_MODE == PSRAM_VADDR_MODE_NORMAL) ? "normal (1-core)" : "ERROR");
  232. return ESP_OK;
  233. }
  234. esp_err_t esp_spiram_add_to_heapalloc(void)
  235. {
  236. size_t spiram_size = esp_spiram_get_size();
  237. uint32_t size_for_flash = (pages_for_flash << 16);
  238. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (spiram_size - (pages_for_flash << 16)) / 1024);
  239. //Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's
  240. //no need to explicitly specify them.
  241. return heap_caps_add_region((intptr_t)SOC_EXTRAM_DATA_HIGH - spiram_size + size_for_flash, (intptr_t)SOC_EXTRAM_DATA_HIGH - 1);
  242. }
  243. static uint8_t *dma_heap;
  244. esp_err_t esp_spiram_reserve_dma_pool(size_t size)
  245. {
  246. if (size == 0) {
  247. return ESP_OK; //no-op
  248. }
  249. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size / 1024);
  250. dma_heap = heap_caps_malloc(size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  251. if (!dma_heap) {
  252. return ESP_ERR_NO_MEM;
  253. }
  254. uint32_t caps[] = {MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT};
  255. return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap + size - 1);
  256. }
  257. size_t esp_spiram_get_size(void)
  258. {
  259. if (!spiram_inited) {
  260. ESP_EARLY_LOGE(TAG, "SPI RAM not initialized");
  261. abort();
  262. }
  263. psram_size_t size = psram_get_size();
  264. if (size == PSRAM_SIZE_16MBITS) {
  265. return 2 * 1024 * 1024;
  266. }
  267. if (size == PSRAM_SIZE_32MBITS) {
  268. return 4 * 1024 * 1024;
  269. }
  270. if (size == PSRAM_SIZE_64MBITS) {
  271. return 8 * 1024 * 1024;
  272. }
  273. return CONFIG_SPIRAM_SIZE;
  274. }
  275. /*
  276. Before flushing the cache, if psram is enabled as a memory-mapped thing, we need to write back the data in the cache to the psram first,
  277. otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
  278. */
  279. void IRAM_ATTR esp_spiram_writeback_cache(void)
  280. {
  281. extern void Cache_WriteBack_All(void);
  282. Cache_WriteBack_All();
  283. }
  284. #endif