esp_memory_utils.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include "soc/soc.h"
  11. #include "soc/soc_caps.h"
  12. #include "sdkconfig.h"
  13. #include "esp_attr.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /** Common functions, to be kept in sync with bootloader_memory_utils.h **/
  18. /**
  19. * @brief Check if the IRAM and DRAM are separate or using the same memory space
  20. *
  21. * @return true if the DRAM and IRAM are sharing the same memory space, false otherwise
  22. */
  23. __attribute__((always_inline))
  24. inline static bool esp_dram_match_iram(void) {
  25. return (SOC_DRAM_LOW == SOC_IRAM_LOW && SOC_DRAM_HIGH == SOC_IRAM_HIGH);
  26. }
  27. /**
  28. * @brief Check if the pointer is in iram
  29. *
  30. * @param p pointer
  31. *
  32. * @return true: is in iram; false: not in iram
  33. */
  34. __attribute__((always_inline))
  35. inline static bool esp_ptr_in_iram(const void *p) {
  36. #if CONFIG_IDF_TARGET_ESP32 && CONFIG_FREERTOS_UNICORE
  37. return ((intptr_t)p >= SOC_CACHE_APP_LOW && (intptr_t)p < SOC_IRAM_HIGH);
  38. #else
  39. return ((intptr_t)p >= SOC_IRAM_LOW && (intptr_t)p < SOC_IRAM_HIGH);
  40. #endif
  41. }
  42. /**
  43. * @brief Check if the pointer is in dram
  44. *
  45. * @param p pointer
  46. *
  47. * @return true: is in dram; false: not in dram
  48. */
  49. __attribute__((always_inline))
  50. inline static bool esp_ptr_in_dram(const void *p) {
  51. return ((intptr_t)p >= SOC_DRAM_LOW && (intptr_t)p < SOC_DRAM_HIGH);
  52. }
  53. /**
  54. * @brief Check if the pointer is in diram_dram
  55. *
  56. * @param p pointer
  57. *
  58. * @return true: is in diram_dram; false: not in diram_dram
  59. */
  60. __attribute__((always_inline))
  61. inline static bool esp_ptr_in_diram_dram(const void *p) {
  62. return ((intptr_t)p >= SOC_DIRAM_DRAM_LOW && (intptr_t)p < SOC_DIRAM_DRAM_HIGH);
  63. }
  64. /**
  65. * @brief Check if the pointer is in diram_iram
  66. *
  67. * @param p pointer
  68. *
  69. * @return true: is in diram_iram; false: not in diram_iram
  70. */
  71. __attribute__((always_inline))
  72. inline static bool esp_ptr_in_diram_iram(const void *p) {
  73. // TODO: IDF-5980 esp32c6 D/I RAM share the same address
  74. #if SOC_DIRAM_IRAM_LOW == SOC_DIRAM_DRAM_LOW
  75. return false;
  76. #else
  77. return ((intptr_t)p >= SOC_DIRAM_IRAM_LOW && (intptr_t)p < SOC_DIRAM_IRAM_HIGH);
  78. #endif
  79. }
  80. /**
  81. * @brief Check if the pointer is in rtc_iram_fast
  82. *
  83. * @param p pointer
  84. *
  85. * @return true: is in rtc_iram_fast; false: not in rtc_iram_fast
  86. */
  87. __attribute__((always_inline))
  88. inline static bool esp_ptr_in_rtc_iram_fast(const void *p) {
  89. #if SOC_RTC_FAST_MEM_SUPPORTED
  90. return ((intptr_t)p >= SOC_RTC_IRAM_LOW && (intptr_t)p < SOC_RTC_IRAM_HIGH);
  91. #else
  92. return false;
  93. #endif
  94. }
  95. /**
  96. * @brief Check if the pointer is in rtc_dram_fast
  97. *
  98. * @param p pointer
  99. *
  100. * @return true: is in rtc_dram_fast; false: not in rtc_dram_fast
  101. */
  102. __attribute__((always_inline))
  103. inline static bool esp_ptr_in_rtc_dram_fast(const void *p) {
  104. #if SOC_RTC_FAST_MEM_SUPPORTED
  105. return ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
  106. #else
  107. return false;
  108. #endif
  109. }
  110. /**
  111. * @brief Check if the pointer is in rtc_slow
  112. *
  113. * @param p pointer
  114. *
  115. * @return true: is in rtc_slow; false: not in rtc_slow
  116. */
  117. __attribute__((always_inline))
  118. inline static bool esp_ptr_in_rtc_slow(const void *p) {
  119. #if SOC_RTC_SLOW_MEM_SUPPORTED
  120. return ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
  121. #else
  122. return false;
  123. #endif
  124. }
  125. /* Convert a D/IRAM DRAM pointer to equivalent word address in IRAM
  126. - Address must be word aligned
  127. - Address must pass esp_ptr_in_diram_dram() test, or result will be invalid pointer
  128. */
  129. __attribute__((always_inline))
  130. inline static void * esp_ptr_diram_dram_to_iram(const void *p) {
  131. #if SOC_DIRAM_INVERTED
  132. return (void *) ( SOC_DIRAM_IRAM_LOW + (SOC_DIRAM_DRAM_HIGH - (intptr_t)p) - 4);
  133. #else
  134. return (void *) ( SOC_DIRAM_IRAM_LOW + ((intptr_t)p - SOC_DIRAM_DRAM_LOW) );
  135. #endif
  136. }
  137. /* Convert a D/IRAM IRAM pointer to equivalent word address in DRAM
  138. - Address must be word aligned
  139. - Address must pass esp_ptr_in_diram_iram() test, or result will be invalid pointer
  140. */
  141. __attribute__((always_inline))
  142. inline static void * esp_ptr_diram_iram_to_dram(const void *p) {
  143. #if SOC_DIRAM_INVERTED
  144. return (void *) ( SOC_DIRAM_DRAM_LOW + (SOC_DIRAM_IRAM_HIGH - (intptr_t)p) - 4);
  145. #else
  146. return (void *) ( SOC_DIRAM_DRAM_LOW + ((intptr_t)p - SOC_DIRAM_IRAM_LOW) );
  147. #endif
  148. }
  149. /** End of common functions to be kept in sync with bootloader_memory_utils.h **/
  150. /** Add app-specific functions below **/
  151. /**
  152. * @brief Check if the pointer is dma capable
  153. *
  154. * @param p pointer
  155. *
  156. * @return true: capable; false: not capable
  157. */
  158. __attribute__((always_inline))
  159. inline static bool esp_ptr_dma_capable(const void *p)
  160. {
  161. return (intptr_t)p >= SOC_DMA_LOW && (intptr_t)p < SOC_DMA_HIGH;
  162. }
  163. /**
  164. * @brief Check if the pointer is in external ram dma capable region
  165. *
  166. * @param p pointer
  167. *
  168. * @return true: capable; false: not capable
  169. */
  170. bool esp_ptr_dma_ext_capable(const void *p);
  171. /**
  172. * @brief Check if the pointer is word aligned
  173. *
  174. * @param p pointer
  175. *
  176. * @return true: aligned; false: not aligned
  177. */
  178. __attribute__((always_inline))
  179. inline static bool esp_ptr_word_aligned(const void *p)
  180. {
  181. return ((intptr_t)p) % 4 == 0;
  182. }
  183. /**
  184. * @brief Check if the pointer is executable
  185. *
  186. * @param p pointer
  187. *
  188. * @return true: is executable; false: not executable
  189. */
  190. __attribute__((always_inline))
  191. inline static bool esp_ptr_executable(const void *p)
  192. {
  193. intptr_t ip = (intptr_t) p;
  194. return (ip >= SOC_IROM_LOW && ip < SOC_IROM_HIGH)
  195. || (ip >= SOC_IRAM_LOW && ip < SOC_IRAM_HIGH)
  196. || (ip >= SOC_IROM_MASK_LOW && ip < SOC_IROM_MASK_HIGH)
  197. #if defined(SOC_CACHE_APP_LOW) && defined(CONFIG_FREERTOS_UNICORE)
  198. || (ip >= SOC_CACHE_APP_LOW && ip < SOC_CACHE_APP_HIGH)
  199. #endif
  200. #if SOC_RTC_FAST_MEM_SUPPORTED
  201. || (ip >= SOC_RTC_IRAM_LOW && ip < SOC_RTC_IRAM_HIGH)
  202. #endif
  203. ;
  204. }
  205. /**
  206. * @brief Check if the pointer is byte accessible
  207. *
  208. * @param p pointer
  209. *
  210. * @return true: is byte accessible; false: not byte accessible
  211. */
  212. bool esp_ptr_byte_accessible(const void *p);
  213. /**
  214. * @brief Check if the pointer is in internal ram
  215. *
  216. * @param p pointer
  217. *
  218. * @return true: is in internal ram; false: not in internal ram
  219. */
  220. __attribute__((always_inline))
  221. inline static bool esp_ptr_internal(const void *p) {
  222. bool r;
  223. r = ((intptr_t)p >= SOC_MEM_INTERNAL_LOW && (intptr_t)p < SOC_MEM_INTERNAL_HIGH);
  224. #if SOC_RTC_SLOW_MEM_SUPPORTED
  225. r |= ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
  226. #endif
  227. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  228. /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
  229. * for single core configuration (where it gets added to system heap) following
  230. * additional check is required */
  231. r |= ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
  232. #endif
  233. #if CONFIG_ESP32S3_DATA_CACHE_16KB
  234. /* For ESP32-S3, when the DCACHE size is set to 16 kB, the unused 48 kB is
  235. * added to the heap in 2 blocks of 32 kB (from 0x3FCF0000) and 16 kB
  236. * (from 0x3C000000 (SOC_DROM_LOW) - 0x3C004000).
  237. * Though this memory lies in the external memory vaddr, it is no different
  238. * from the internal RAM in terms of hardware attributes and it is a part of
  239. * the internal RAM when added to the heap.*/
  240. r |= ((intptr_t)p >= SOC_DROM_LOW && (intptr_t)p < (SOC_DROM_LOW + 0x4000));
  241. #endif
  242. return r;
  243. }
  244. /**
  245. * @brief Check if the pointer is in external ram
  246. *
  247. * @param p pointer
  248. *
  249. * @return true: is in external ram; false: not in external ram
  250. */
  251. bool esp_ptr_external_ram(const void *p);
  252. /**
  253. * @brief Check if the pointer is in drom
  254. *
  255. * @param p pointer
  256. *
  257. * @return true: is in drom; false: not in drom
  258. */
  259. __attribute__((always_inline))
  260. inline static bool esp_ptr_in_drom(const void *p) {
  261. uint32_t drom_start_addr = SOC_DROM_LOW;
  262. #if CONFIG_ESP32S3_DATA_CACHE_16KB
  263. /* For ESP32-S3, when the DCACHE size is set to 16 kB, the unused 48 kB is
  264. * added to the heap in 2 blocks of 32 kB (from 0x3FCF0000) and 16 kB
  265. * (from 0x3C000000 (SOC_DROM_LOW) - 0x3C004000).
  266. * The drom_start_addr has to be moved by 0x4000 (16kB) to accomodate
  267. * this addition. */
  268. drom_start_addr += 0x4000;
  269. #endif
  270. return ((intptr_t)p >= drom_start_addr && (intptr_t)p < SOC_DROM_HIGH);
  271. }
  272. /**
  273. * @brief Check if the stack pointer is in dram
  274. *
  275. * @param sp stack pointer
  276. *
  277. * @return true: is in dram; false: not in dram
  278. */
  279. __attribute__((always_inline))
  280. inline static bool esp_stack_ptr_in_dram(uint32_t sp)
  281. {
  282. //Check if stack ptr is in between SOC_DRAM_LOW and SOC_DRAM_HIGH, and 16 byte aligned.
  283. return !(sp < SOC_DRAM_LOW + 0x10 || sp > SOC_DRAM_HIGH - 0x10 || ((sp & 0xF) != 0));
  284. }
  285. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  286. /**
  287. * @brief Check if the stack pointer is in external ram
  288. *
  289. * @param sp stack pointer
  290. *
  291. * @return true: is in external ram; false: not in external ram
  292. */
  293. bool esp_stack_ptr_in_extram(uint32_t sp);
  294. #endif
  295. /**
  296. * @brief Check if the stack pointer is sane
  297. *
  298. * @param sp stack pointer
  299. *
  300. * @return true: is in sane; false: not in sane
  301. */
  302. __attribute__((always_inline))
  303. inline static bool esp_stack_ptr_is_sane(uint32_t sp)
  304. {
  305. return esp_stack_ptr_in_dram(sp)
  306. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  307. || esp_stack_ptr_in_extram(sp)
  308. #endif
  309. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  310. || esp_ptr_in_rtc_dram_fast((void*) sp)
  311. #endif
  312. ;
  313. }
  314. #ifdef __cplusplus
  315. }
  316. #endif