esp_psram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*----------------------------------------------------------------------------------------------------
  7. * Abstraction layer for PSRAM. PSRAM device related registers and MMU/Cache related code shouls be
  8. * abstracted to lower layers.
  9. *
  10. * When we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
  11. *----------------------------------------------------------------------------------------------------*/
  12. #include <sys/param.h>
  13. #include "sdkconfig.h"
  14. #include "esp_attr.h"
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/xtensa_api.h"
  19. #include "esp_heap_caps_init.h"
  20. #include "hal/mmu_hal.h"
  21. #include "hal/cache_ll.h"
  22. #include "esp_private/esp_psram_io.h"
  23. #include "esp_private/esp_psram_extram.h"
  24. #include "esp_private/mmu.h"
  25. #include "esp_psram_impl.h"
  26. #include "esp_psram.h"
  27. #if CONFIG_IDF_TARGET_ESP32
  28. #include "esp32/himem.h"
  29. #include "esp32/rom/cache.h"
  30. #endif
  31. #if CONFIG_IDF_TARGET_ESP32
  32. #if CONFIG_FREERTOS_UNICORE
  33. #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
  34. #else
  35. #define PSRAM_MODE PSRAM_VADDR_MODE_LOWHIGH
  36. #endif
  37. #else
  38. #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
  39. #endif
  40. #if CONFIG_SPIRAM
  41. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  42. extern uint8_t _ext_ram_bss_start;
  43. extern uint8_t _ext_ram_bss_end;
  44. #endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  45. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  46. extern uint8_t _ext_ram_noinit_start;
  47. extern uint8_t _ext_ram_noinit_end;
  48. #endif //#if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  49. //These variables are in bytes
  50. static intptr_t s_allocable_vaddr_start;
  51. static intptr_t s_allocable_vaddr_end;
  52. static intptr_t s_mapped_vaddr_start;
  53. static intptr_t s_mapped_vaddr_end;
  54. static bool s_spiram_inited;
  55. static const char* TAG = "esp_psram";
  56. #if CONFIG_IDF_TARGET_ESP32
  57. //If no function in esp_himem.c is used, this function will be linked into the
  58. //binary instead of the one in esp_himem.c, automatically making sure no memory
  59. //is reserved if no himem function is used.
  60. size_t __attribute__((weak)) esp_himem_reserved_area_size(void) {
  61. return 0;
  62. }
  63. static void IRAM_ATTR s_mapping(int v_start, int size)
  64. {
  65. //Enable external RAM in MMU
  66. cache_sram_mmu_set(0, 0, v_start, 0, 32, (size / 1024 / 32));
  67. //Flush and enable icache for APP CPU
  68. #if !CONFIG_FREERTOS_UNICORE
  69. DPORT_CLEAR_PERI_REG_MASK(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DRAM1);
  70. cache_sram_mmu_set(1, 0, v_start, 0, 32, (size / 1024 / 32));
  71. #endif
  72. }
  73. #endif //CONFIG_IDF_TARGET_ESP32
  74. esp_err_t esp_psram_init(void)
  75. {
  76. if (s_spiram_inited) {
  77. return ESP_ERR_INVALID_STATE;
  78. }
  79. esp_err_t ret;
  80. ret = esp_psram_impl_enable(PSRAM_MODE);
  81. if (ret != ESP_OK) {
  82. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  83. ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
  84. #endif
  85. return ret;
  86. }
  87. s_spiram_inited = true;
  88. uint32_t psram_physical_size = 0;
  89. ret = esp_psram_impl_get_physical_size(&psram_physical_size);
  90. assert(ret == ESP_OK);
  91. ESP_EARLY_LOGI(TAG, "Found %dMBit SPI RAM device", psram_physical_size / (1024 * 1024));
  92. ESP_EARLY_LOGI(TAG, "Speed: %dMHz", CONFIG_SPIRAM_SPEED);
  93. #if CONFIG_IDF_TARGET_ESP32
  94. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
  95. (PSRAM_MODE==PSRAM_VADDR_MODE_EVENODD)?"even/odd (2-core)": \
  96. (PSRAM_MODE==PSRAM_VADDR_MODE_LOWHIGH)?"low/high (2-core)": \
  97. (PSRAM_MODE==PSRAM_VADDR_MODE_NORMAL)?"normal (1-core)":"ERROR");
  98. #endif
  99. uint32_t psram_available_size = 0;
  100. ret = esp_psram_impl_get_available_size(&psram_available_size);
  101. assert(ret == ESP_OK);
  102. __attribute__((unused)) uint32_t total_available_size = psram_available_size;
  103. /**
  104. * `start_page` is the psram physical address in MMU page size.
  105. * MMU page size on ESP32S2 is 64KB
  106. * e.g.: psram physical address 16 is in page 0
  107. *
  108. * Here we plan to copy FLASH instructions to psram physical address 0, which is the No.0 page.
  109. */
  110. __attribute__((unused)) uint32_t start_page = 0;
  111. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
  112. uint32_t used_page = 0;
  113. #endif
  114. //------------------------------------Copy Flash .text to PSRAM-------------------------------------//
  115. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  116. ret = mmu_config_psram_text_segment(start_page, total_available_size, &used_page);
  117. if (ret != ESP_OK) {
  118. ESP_EARLY_LOGE(TAG, "No enough psram memory for instructon!");
  119. abort();
  120. }
  121. start_page += used_page;
  122. psram_available_size -= MMU_PAGE_TO_BYTES(used_page);
  123. ESP_EARLY_LOGV(TAG, "after copy .text, used page is %d, start_page is %d, psram_available_size is %d B", used_page, start_page, psram_available_size);
  124. #endif //#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  125. //------------------------------------Copy Flash .rodata to PSRAM-------------------------------------//
  126. #if CONFIG_SPIRAM_RODATA
  127. ret = mmu_config_psram_rodata_segment(start_page, total_available_size, &used_page);
  128. if (ret != ESP_OK) {
  129. ESP_EARLY_LOGE(TAG, "No enough psram memory for rodata!");
  130. abort();
  131. }
  132. start_page += used_page;
  133. psram_available_size -= MMU_PAGE_TO_BYTES(used_page);
  134. ESP_EARLY_LOGV(TAG, "after copy .rodata, used page is %d, start_page is %d, psram_available_size is %d B", used_page, start_page, psram_available_size);
  135. #endif //#if CONFIG_SPIRAM_RODATA
  136. //----------------------------------Map the PSRAM physical range to MMU-----------------------------//
  137. intptr_t vaddr_start = mmu_get_psram_vaddr_start();
  138. if (vaddr_start + psram_available_size > mmu_get_psram_vaddr_end()) {
  139. ESP_EARLY_LOGV(TAG, "Virtual address not enough for PSRAM!");
  140. psram_available_size = mmu_get_psram_vaddr_end() - vaddr_start;
  141. }
  142. #if CONFIG_IDF_TARGET_ESP32
  143. s_mapping(vaddr_start, psram_available_size);
  144. #else
  145. uint32_t actual_mapped_len = 0;
  146. mmu_hal_map_region(0, MMU_TARGET_PSRAM0, vaddr_start, MMU_PAGE_TO_BYTES(start_page), psram_available_size, &actual_mapped_len);
  147. ESP_EARLY_LOGV(TAG, "actual_mapped_len is 0x%x bytes", actual_mapped_len);
  148. cache_bus_mask_t bus_mask = cache_ll_l1_get_bus(0, vaddr_start, actual_mapped_len);
  149. cache_ll_l1_enable_bus(0, bus_mask);
  150. #if !CONFIG_FREERTOS_UNICORE
  151. bus_mask = cache_ll_l1_get_bus(1, vaddr_start, actual_mapped_len);
  152. cache_ll_l1_enable_bus(1, bus_mask);
  153. #endif
  154. #endif //#if CONFIG_IDF_TARGET_ESP32
  155. /*------------------------------------------------------------------------------
  156. * After mapping, we DON'T care about the PSRAM PHYSICAL ADDRESSS ANYMORE!
  157. *----------------------------------------------------------------------------*/
  158. s_mapped_vaddr_start = vaddr_start;
  159. s_mapped_vaddr_end = vaddr_start + psram_available_size;
  160. s_allocable_vaddr_start = vaddr_start;
  161. s_allocable_vaddr_end = vaddr_start + psram_available_size;
  162. //------------------------------------Configure .bss in PSRAM-------------------------------------//
  163. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  164. //should never be negative number
  165. uint32_t ext_bss_size = ((intptr_t)&_ext_ram_bss_end - (intptr_t)&_ext_ram_bss_start);
  166. ESP_EARLY_LOGV(TAG, "ext_bss_size is %d", ext_bss_size);
  167. s_allocable_vaddr_start += ext_bss_size;
  168. #endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  169. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  170. uint32_t ext_noinit_size = ((intptr_t)&_ext_ram_noinit_end - (intptr_t)&_ext_ram_noinit_start);
  171. ESP_EARLY_LOGV(TAG, "ext_noinit_size is %d", ext_noinit_size);
  172. s_allocable_vaddr_start += ext_noinit_size;
  173. #endif
  174. #if CONFIG_IDF_TARGET_ESP32
  175. s_allocable_vaddr_end -= esp_himem_reserved_area_size() - 1;
  176. #endif
  177. ESP_EARLY_LOGV(TAG, "s_allocable_vaddr_start is 0x%x, s_allocable_vaddr_end is 0x%x", s_allocable_vaddr_start, s_allocable_vaddr_end);
  178. return ESP_OK;
  179. }
  180. /**
  181. * Add the PSRAM available region to heap allocator. Heap allocator knows the capabilities of this type of memory,
  182. * so there's no need to explicitly specify them.
  183. */
  184. esp_err_t esp_psram_extram_add_to_heap_allocator(void)
  185. {
  186. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (s_allocable_vaddr_end - s_allocable_vaddr_start) / 1024);
  187. return heap_caps_add_region(s_allocable_vaddr_start, s_allocable_vaddr_end);
  188. }
  189. esp_err_t IRAM_ATTR esp_psram_extram_get_mapped_range(intptr_t *out_vstart, intptr_t *out_vend)
  190. {
  191. if (!out_vstart || !out_vend) {
  192. return ESP_ERR_INVALID_ARG;
  193. }
  194. if (!s_spiram_inited) {
  195. return ESP_ERR_INVALID_STATE;
  196. }
  197. *out_vstart = s_mapped_vaddr_start;
  198. *out_vend = s_mapped_vaddr_end;
  199. return ESP_OK;
  200. }
  201. esp_err_t esp_psram_extram_get_alloced_range(intptr_t *out_vstart, intptr_t *out_vend)
  202. {
  203. if (!out_vstart || !out_vend) {
  204. return ESP_ERR_INVALID_ARG;
  205. }
  206. if (!s_spiram_inited) {
  207. return ESP_ERR_INVALID_STATE;
  208. }
  209. *out_vstart = s_allocable_vaddr_start;
  210. *out_vend = s_allocable_vaddr_end;
  211. return ESP_OK;
  212. }
  213. #if CONFIG_IDF_TARGET_ESP32
  214. esp_err_t esp_psram_extram_reserve_dma_pool(size_t size) {
  215. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
  216. /* Pool may be allocated in multiple non-contiguous chunks, depending on available RAM */
  217. while (size > 0) {
  218. size_t next_size = heap_caps_get_largest_free_block(MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  219. next_size = MIN(next_size, size);
  220. ESP_EARLY_LOGD(TAG, "Allocating block of size %d bytes", next_size);
  221. uint8_t *dma_heap = heap_caps_malloc(next_size, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  222. if (!dma_heap || next_size == 0) {
  223. return ESP_ERR_NO_MEM;
  224. }
  225. uint32_t caps[] = { 0, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL, MALLOC_CAP_8BIT|MALLOC_CAP_32BIT };
  226. esp_err_t e = heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap+next_size-1);
  227. if (e != ESP_OK) {
  228. return e;
  229. }
  230. size -= next_size;
  231. }
  232. return ESP_OK;
  233. }
  234. #else
  235. esp_err_t esp_psram_extram_reserve_dma_pool(size_t size)
  236. {
  237. if (size == 0) {
  238. return ESP_OK; //no-op
  239. }
  240. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
  241. uint8_t *dma_heap = heap_caps_malloc(size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  242. if (!dma_heap) {
  243. return ESP_ERR_NO_MEM;
  244. }
  245. uint32_t caps[] = {MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT};
  246. return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap + size);
  247. }
  248. #endif
  249. bool IRAM_ATTR esp_psram_is_initialized(void)
  250. {
  251. return s_spiram_inited;
  252. }
  253. size_t esp_psram_get_size(void)
  254. {
  255. uint32_t available_size = 0;
  256. esp_err_t ret = esp_psram_impl_get_available_size(&available_size);
  257. if (ret != ESP_OK) {
  258. //This means PSRAM isn't initialised, to keep back-compatibility, set size to 0.
  259. available_size = 0;
  260. }
  261. return (size_t)available_size;
  262. }
  263. uint8_t esp_psram_io_get_cs_io(void)
  264. {
  265. return esp_psram_impl_get_cs_io();
  266. }
  267. /*
  268. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  269. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  270. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  271. */
  272. bool esp_psram_extram_test(void)
  273. {
  274. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  275. const void *keepout_addr_low = (const void*)&_ext_ram_noinit_start;
  276. const void *keepout_addr_high = (const void*)&_ext_ram_noinit_end;
  277. #else
  278. const void *keepout_addr_low = 0;
  279. const void *keepout_addr_high = 0;
  280. #endif
  281. volatile int *spiram = (volatile int *)s_mapped_vaddr_start;
  282. size_t p;
  283. size_t s = s_mapped_vaddr_end - s_mapped_vaddr_start;
  284. int errct=0;
  285. int initial_err=-1;
  286. for (p=0; p<(s/sizeof(int)); p+=8) {
  287. const void *addr = (const void *)&spiram[p];
  288. if ((keepout_addr_low <= addr) && (addr < keepout_addr_high)) {
  289. continue;
  290. }
  291. spiram[p]=p^0xAAAAAAAA;
  292. }
  293. for (p=0; p<(s/sizeof(int)); p+=8) {
  294. const void *addr = (const void *)&spiram[p];
  295. if ((keepout_addr_low <= addr) && (addr < keepout_addr_high)) {
  296. continue;
  297. }
  298. if (spiram[p]!=(p^0xAAAAAAAA)) {
  299. errct++;
  300. if (errct==1) initial_err=p*4;
  301. }
  302. }
  303. if (errct) {
  304. ESP_EARLY_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, s/32, initial_err + s_mapped_vaddr_start);
  305. return false;
  306. } else {
  307. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  308. return true;
  309. }
  310. }
  311. #endif //#if CONFIG_SPIRAM