esp_memory_utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "bootloader_memory_utils.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * @brief Check if the pointer is dma capable
  20. *
  21. * @param p pointer
  22. *
  23. * @return true: capable; false: not capable
  24. */
  25. __attribute__((always_inline))
  26. inline static bool esp_ptr_dma_capable(const void *p)
  27. {
  28. return (intptr_t)p >= SOC_DMA_LOW && (intptr_t)p < SOC_DMA_HIGH;
  29. }
  30. /**
  31. * @brief Check if the pointer is in external ram dma capable region
  32. *
  33. * @param p pointer
  34. *
  35. * @return true: capable; false: not capable
  36. */
  37. bool esp_ptr_dma_ext_capable(const void *p);
  38. /**
  39. * @brief Check if the pointer is word aligned
  40. *
  41. * @param p pointer
  42. *
  43. * @return true: aligned; false: not aligned
  44. */
  45. __attribute__((always_inline))
  46. inline static bool esp_ptr_word_aligned(const void *p)
  47. {
  48. return ((intptr_t)p) % 4 == 0;
  49. }
  50. /**
  51. * @brief Check if the pointer is executable
  52. *
  53. * @param p pointer
  54. *
  55. * @return true: is executable; false: not executable
  56. */
  57. __attribute__((always_inline))
  58. inline static bool esp_ptr_executable(const void *p)
  59. {
  60. intptr_t ip = (intptr_t) p;
  61. return (ip >= SOC_IROM_LOW && ip < SOC_IROM_HIGH)
  62. || (ip >= SOC_IRAM_LOW && ip < SOC_IRAM_HIGH)
  63. || (ip >= SOC_IROM_MASK_LOW && ip < SOC_IROM_MASK_HIGH)
  64. #if defined(SOC_CACHE_APP_LOW) && defined(CONFIG_FREERTOS_UNICORE)
  65. || (ip >= SOC_CACHE_APP_LOW && ip < SOC_CACHE_APP_HIGH)
  66. #endif
  67. #if SOC_RTC_FAST_MEM_SUPPORTED
  68. || (ip >= SOC_RTC_IRAM_LOW && ip < SOC_RTC_IRAM_HIGH)
  69. #endif
  70. ;
  71. }
  72. /**
  73. * @brief Check if the pointer is byte accessible
  74. *
  75. * @param p pointer
  76. *
  77. * @return true: is byte accessible; false: not byte accessible
  78. */
  79. bool esp_ptr_byte_accessible(const void *p);
  80. /**
  81. * @brief Check if the pointer is in internal ram
  82. *
  83. * @param p pointer
  84. *
  85. * @return true: is in internal ram; false: not in internal ram
  86. */
  87. __attribute__((always_inline))
  88. inline static bool esp_ptr_internal(const void *p) {
  89. bool r;
  90. r = ((intptr_t)p >= SOC_MEM_INTERNAL_LOW && (intptr_t)p < SOC_MEM_INTERNAL_HIGH);
  91. #if SOC_RTC_SLOW_MEM_SUPPORTED
  92. r |= ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
  93. #endif
  94. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  95. /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
  96. * for single core configuration (where it gets added to system heap) following
  97. * additional check is required */
  98. r |= ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
  99. #endif
  100. return r;
  101. }
  102. /**
  103. * @brief Check if the pointer is in external ram
  104. *
  105. * @param p pointer
  106. *
  107. * @return true: is in external ram; false: not in external ram
  108. */
  109. bool esp_ptr_external_ram(const void *p);
  110. /**
  111. * @brief Check if the pointer is in drom
  112. *
  113. * @param p pointer
  114. *
  115. * @return true: is in drom; false: not in drom
  116. */
  117. __attribute__((always_inline))
  118. inline static bool esp_ptr_in_drom(const void *p) {
  119. return ((intptr_t)p >= SOC_DROM_LOW && (intptr_t)p < SOC_DROM_HIGH);
  120. }
  121. /**
  122. * @brief Check if the stack pointer is in dram
  123. *
  124. * @param sp stack pointer
  125. *
  126. * @return true: is in dram; false: not in dram
  127. */
  128. __attribute__((always_inline))
  129. inline static bool esp_stack_ptr_in_dram(uint32_t sp)
  130. {
  131. //Check if stack ptr is in between SOC_DRAM_LOW and SOC_DRAM_HIGH, and 16 byte aligned.
  132. return !(sp < SOC_DRAM_LOW + 0x10 || sp > SOC_DRAM_HIGH - 0x10 || ((sp & 0xF) != 0));
  133. }
  134. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  135. /**
  136. * @brief Check if the stack pointer is in external ram
  137. *
  138. * @param sp stack pointer
  139. *
  140. * @return true: is in external ram; false: not in external ram
  141. */
  142. bool esp_stack_ptr_in_extram(uint32_t sp);
  143. #endif
  144. /**
  145. * @brief Check if the stack pointer is sane
  146. *
  147. * @param sp stack pointer
  148. *
  149. * @return true: is in sane; false: not in sane
  150. */
  151. __attribute__((always_inline))
  152. inline static bool esp_stack_ptr_is_sane(uint32_t sp)
  153. {
  154. return esp_stack_ptr_in_dram(sp)
  155. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  156. || esp_stack_ptr_in_extram(sp)
  157. #endif
  158. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  159. || esp_ptr_in_rtc_dram_fast((void*) sp)
  160. #endif
  161. ;
  162. }
  163. #ifdef __cplusplus
  164. }
  165. #endif