esp_memory_utils.h 8.0 KB

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