sleep_retention.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <sys/lock.h>
  9. #include <sys/param.h>
  10. #include "esp_attr.h"
  11. #include "esp_sleep.h"
  12. #include "esp_log.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "esp_heap_caps.h"
  16. #include "soc/soc_caps.h"
  17. #include "hal/rtc_hal.h"
  18. #include "esp_private/sleep_retention.h"
  19. #include "sdkconfig.h"
  20. #ifdef CONFIG_IDF_TARGET_ESP32S3
  21. #include "esp32s3/rom/cache.h"
  22. #endif
  23. static __attribute__((unused)) const char *TAG = "sleep";
  24. /**
  25. * Internal structure which holds all requested light sleep memory retention parameters
  26. */
  27. typedef struct {
  28. rtc_cntl_sleep_retent_t retent;
  29. } sleep_retention_t;
  30. static DRAM_ATTR sleep_retention_t s_retention;
  31. #if SOC_PM_SUPPORT_TAGMEM_PD
  32. #define TAGMEM_PD_MEM_TYPE_CAPS (MALLOC_CAP_DMA | MALLOC_CAP_DEFAULT)
  33. #if CONFIG_PM_POWER_DOWN_TAGMEM_IN_LIGHT_SLEEP
  34. static int cache_tagmem_retention_setup(uint32_t code_seg_vaddr, uint32_t code_seg_size, uint32_t data_seg_vaddr, uint32_t data_seg_size)
  35. {
  36. int sets; /* i/d-cache total set counts */
  37. int index; /* virtual address mapping i/d-cache row offset */
  38. int waysgrp;
  39. int icache_tagmem_blk_gs, dcache_tagmem_blk_gs;
  40. struct cache_mode imode = { .icache = 1 };
  41. struct cache_mode dmode = { .icache = 0 };
  42. /* calculate/prepare i-cache tag memory retention parameters */
  43. Cache_Get_Mode(&imode);
  44. sets = imode.cache_size / imode.cache_ways / imode.cache_line_size;
  45. index = (code_seg_vaddr / imode.cache_line_size) % sets;
  46. waysgrp = imode.cache_ways >> 2;
  47. code_seg_size = ALIGNUP(imode.cache_line_size, code_seg_size);
  48. s_retention.retent.tagmem.icache.start_point = index;
  49. s_retention.retent.tagmem.icache.size = (sets * waysgrp) & 0xff;
  50. s_retention.retent.tagmem.icache.vld_size = s_retention.retent.tagmem.icache.size;
  51. if (code_seg_size < imode.cache_size / imode.cache_ways) {
  52. s_retention.retent.tagmem.icache.vld_size = (code_seg_size / imode.cache_line_size) * waysgrp;
  53. }
  54. s_retention.retent.tagmem.icache.enable = (code_seg_size != 0) ? 1 : 0;
  55. icache_tagmem_blk_gs = s_retention.retent.tagmem.icache.vld_size ? s_retention.retent.tagmem.icache.vld_size : sets * waysgrp;
  56. icache_tagmem_blk_gs = ALIGNUP(4, icache_tagmem_blk_gs);
  57. ESP_LOGD(TAG, "I-cache size:%d KiB, line size:%d B, ways:%d, sets:%d, index:%d, tag block groups:%d", (imode.cache_size>>10),
  58. imode.cache_line_size, imode.cache_ways, sets, index, icache_tagmem_blk_gs);
  59. /* calculate/prepare d-cache tag memory retention parameters */
  60. Cache_Get_Mode(&dmode);
  61. sets = dmode.cache_size / dmode.cache_ways / dmode.cache_line_size;
  62. index = (data_seg_vaddr / dmode.cache_line_size) % sets;
  63. waysgrp = dmode.cache_ways >> 2;
  64. data_seg_size = ALIGNUP(dmode.cache_line_size, data_seg_size);
  65. s_retention.retent.tagmem.dcache.start_point = index;
  66. s_retention.retent.tagmem.dcache.size = (sets * waysgrp) & 0x1ff;
  67. s_retention.retent.tagmem.dcache.vld_size = s_retention.retent.tagmem.dcache.size;
  68. #ifndef CONFIG_ESP32S3_DATA_CACHE_16KB
  69. if (data_seg_size < dmode.cache_size / dmode.cache_ways) {
  70. s_retention.retent.tagmem.dcache.vld_size = (data_seg_size / dmode.cache_line_size) * waysgrp;
  71. }
  72. s_retention.retent.tagmem.dcache.enable = (data_seg_size != 0) ? 1 : 0;
  73. #else
  74. s_retention.retent.tagmem.dcache.enable = 1;
  75. #endif
  76. dcache_tagmem_blk_gs = s_retention.retent.tagmem.dcache.vld_size ? s_retention.retent.tagmem.dcache.vld_size : sets * waysgrp;
  77. dcache_tagmem_blk_gs = ALIGNUP(4, dcache_tagmem_blk_gs);
  78. ESP_LOGD(TAG, "D-cache size:%d KiB, line size:%d B, ways:%d, sets:%d, index:%d, tag block groups:%d", (dmode.cache_size>>10),
  79. dmode.cache_line_size, dmode.cache_ways, sets, index, dcache_tagmem_blk_gs);
  80. /* For I or D cache tagmem retention, backup and restore are performed through
  81. * RTC DMA (its bus width is 128 bits), For I/D Cache tagmem blocks (i-cache
  82. * tagmem blocks = 92 bits, d-cache tagmem blocks = 88 bits), RTC DMA automatically
  83. * aligns its bit width to 96 bits, therefore, 3 times RTC DMA can transfer 4
  84. * i/d-cache tagmem blocks (128 bits * 3 = 96 bits * 4) */
  85. return (((icache_tagmem_blk_gs + dcache_tagmem_blk_gs) << 2) * 3);
  86. }
  87. #endif // CONFIG_PM_POWER_DOWN_TAGMEM_IN_LIGHT_SLEEP
  88. static esp_err_t esp_sleep_tagmem_pd_low_init(bool enable)
  89. {
  90. if (enable) {
  91. #if CONFIG_PM_POWER_DOWN_TAGMEM_IN_LIGHT_SLEEP
  92. if (s_retention.retent.tagmem.link_addr == NULL) {
  93. extern char _stext[], _etext[];
  94. uint32_t code_start = (uint32_t)_stext;
  95. uint32_t code_size = (uint32_t)(_etext - _stext);
  96. #if !CONFIG_ESP32S3_SPIRAM_SUPPORT
  97. extern char _rodata_start[], _rodata_reserved_end[];
  98. uint32_t data_start = (uint32_t)_rodata_start;
  99. uint32_t data_size = (uint32_t)(_rodata_reserved_end - _rodata_start);
  100. #else
  101. uint32_t data_start = SOC_DROM_LOW;
  102. uint32_t data_size = (SOC_EXTRAM_DATA_HIGH-SOC_EXTRAM_DATA_LOW) + (SOC_DROM_HIGH-SOC_DROM_LOW);
  103. #endif
  104. ESP_LOGI(TAG, "Code start at %08x, total %.2f KiB, data start at %08x, total %.2f KiB",
  105. code_start, (float)code_size/1024, data_start, (float)data_size/1024);
  106. int tagmem_sz = cache_tagmem_retention_setup(code_start, code_size, data_start, data_size);
  107. void *buf = heap_caps_aligned_alloc(SOC_RTC_CNTL_TAGMEM_PD_DMA_ADDR_ALIGN,
  108. tagmem_sz + RTC_HAL_DMA_LINK_NODE_SIZE,
  109. TAGMEM_PD_MEM_TYPE_CAPS);
  110. if (buf) {
  111. memset(buf, 0, tagmem_sz + RTC_HAL_DMA_LINK_NODE_SIZE);
  112. s_retention.retent.tagmem.link_addr = rtc_cntl_hal_dma_link_init(buf,
  113. buf + RTC_HAL_DMA_LINK_NODE_SIZE, tagmem_sz, NULL);
  114. } else {
  115. s_retention.retent.tagmem.icache.enable = 0;
  116. s_retention.retent.tagmem.dcache.enable = 0;
  117. s_retention.retent.tagmem.link_addr = NULL;
  118. return ESP_ERR_NO_MEM;
  119. }
  120. }
  121. #else // CONFIG_PM_POWER_DOWN_TAGMEM_IN_LIGHT_SLEEP
  122. s_retention.retent.tagmem.icache.enable = 0;
  123. s_retention.retent.tagmem.dcache.enable = 0;
  124. s_retention.retent.tagmem.link_addr = NULL;
  125. #endif // CONFIG_PM_POWER_DOWN_TAGMEM_IN_LIGHT_SLEEP
  126. } else {
  127. #if SOC_PM_SUPPORT_TAGMEM_PD
  128. if (s_retention.retent.tagmem.link_addr) {
  129. heap_caps_free(s_retention.retent.tagmem.link_addr);
  130. s_retention.retent.tagmem.icache.enable = 0;
  131. s_retention.retent.tagmem.dcache.enable = 0;
  132. s_retention.retent.tagmem.link_addr = NULL;
  133. }
  134. #endif
  135. }
  136. return ESP_OK;
  137. }
  138. #endif // SOC_PM_SUPPORT_TAGMEM_PD
  139. #if SOC_PM_SUPPORT_CPU_PD
  140. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  141. #define CPU_PD_MEM_TYPE_CAPS (MALLOC_CAP_RETENTION | MALLOC_CAP_DEFAULT)
  142. #else
  143. #define CPU_PD_MEM_TYPE_CAPS (MALLOC_CAP_DMA | MALLOC_CAP_DEFAULT)
  144. #endif
  145. esp_err_t esp_sleep_cpu_pd_low_init(bool enable)
  146. {
  147. if (enable) {
  148. if (s_retention.retent.cpu_pd_mem == NULL) {
  149. void *buf = heap_caps_aligned_alloc(SOC_RTC_CNTL_CPU_PD_DMA_ADDR_ALIGN,
  150. SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE,
  151. CPU_PD_MEM_TYPE_CAPS);
  152. if (buf) {
  153. memset(buf, 0, SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE);
  154. s_retention.retent.cpu_pd_mem = rtc_cntl_hal_dma_link_init(buf,
  155. buf + RTC_HAL_DMA_LINK_NODE_SIZE, SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE, NULL);
  156. } else {
  157. return ESP_ERR_NO_MEM;
  158. }
  159. }
  160. } else {
  161. if (s_retention.retent.cpu_pd_mem) {
  162. heap_caps_free(s_retention.retent.cpu_pd_mem);
  163. s_retention.retent.cpu_pd_mem = NULL;
  164. }
  165. }
  166. #if SOC_PM_SUPPORT_TAGMEM_PD
  167. if (esp_sleep_tagmem_pd_low_init(enable) != ESP_OK) {
  168. #ifdef CONFIG_ESP32S3_DATA_CACHE_16KB
  169. esp_sleep_cpu_pd_low_init(false);
  170. return ESP_ERR_NO_MEM;
  171. #endif
  172. }
  173. #endif
  174. return ESP_OK;
  175. }
  176. bool cpu_domain_pd_allowed(void)
  177. {
  178. return (s_retention.retent.cpu_pd_mem != NULL);
  179. }
  180. #endif // SOC_PM_SUPPORT_CPU_PD
  181. #if SOC_PM_SUPPORT_CPU_PD || SOC_PM_SUPPORT_TAGMEM_PD
  182. void sleep_enable_memory_retention(void)
  183. {
  184. #if SOC_PM_SUPPORT_CPU_PD
  185. rtc_cntl_hal_enable_cpu_retention(&s_retention.retent);
  186. #endif
  187. #if SOC_PM_SUPPORT_TAGMEM_PD
  188. rtc_cntl_hal_enable_tagmem_retention(&s_retention.retent);
  189. #endif
  190. }
  191. void IRAM_ATTR sleep_disable_memory_retention(void)
  192. {
  193. #if SOC_PM_SUPPORT_CPU_PD
  194. rtc_cntl_hal_disable_cpu_retention(&s_retention.retent);
  195. #endif
  196. #if SOC_PM_SUPPORT_TAGMEM_PD
  197. rtc_cntl_hal_disable_tagmem_retention(&s_retention.retent);
  198. #endif
  199. }
  200. #endif // SOC_PM_SUPPORT_CPU_PD || SOC_PM_SUPPORT_TAGMEM_PD