esp_memory_utils.h 7.9 KB

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