| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- /*
- * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #pragma once
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include "soc/soc.h"
- #include "soc/soc_caps.h"
- #include "sdkconfig.h"
- #include "esp_attr.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- /** Common functions, to be kept in sync with bootloader_memory_utils.h **/
- /**
- * @brief Check if the IRAM and DRAM are separate or using the same memory space
- *
- * @return true if the DRAM and IRAM are sharing the same memory space, false otherwise
- */
- __attribute__((always_inline))
- inline static bool esp_dram_match_iram(void) {
- return (SOC_DRAM_LOW == SOC_IRAM_LOW && SOC_DRAM_HIGH == SOC_IRAM_HIGH);
- }
- /**
- * @brief Check if the pointer is in iram
- *
- * @param p pointer
- *
- * @return true: is in iram; false: not in iram
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_iram(const void *p) {
- #if CONFIG_IDF_TARGET_ESP32 && CONFIG_FREERTOS_UNICORE
- return ((intptr_t)p >= SOC_CACHE_APP_LOW && (intptr_t)p < SOC_IRAM_HIGH);
- #else
- return ((intptr_t)p >= SOC_IRAM_LOW && (intptr_t)p < SOC_IRAM_HIGH);
- #endif
- }
- /**
- * @brief Check if the pointer is in dram
- *
- * @param p pointer
- *
- * @return true: is in dram; false: not in dram
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_dram(const void *p) {
- return ((intptr_t)p >= SOC_DRAM_LOW && (intptr_t)p < SOC_DRAM_HIGH);
- }
- /**
- * @brief Check if the pointer is in diram_dram
- *
- * @param p pointer
- *
- * @return true: is in diram_dram; false: not in diram_dram
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_diram_dram(const void *p) {
- return ((intptr_t)p >= SOC_DIRAM_DRAM_LOW && (intptr_t)p < SOC_DIRAM_DRAM_HIGH);
- }
- /**
- * @brief Check if the pointer is in diram_iram
- *
- * @param p pointer
- *
- * @return true: is in diram_iram; false: not in diram_iram
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_diram_iram(const void *p) {
- // TODO: IDF-5980 esp32c6 D/I RAM share the same address
- #if SOC_DIRAM_IRAM_LOW == SOC_DIRAM_DRAM_LOW
- return false;
- #else
- return ((intptr_t)p >= SOC_DIRAM_IRAM_LOW && (intptr_t)p < SOC_DIRAM_IRAM_HIGH);
- #endif
- }
- /**
- * @brief Check if the pointer is in rtc_iram_fast
- *
- * @param p pointer
- *
- * @return true: is in rtc_iram_fast; false: not in rtc_iram_fast
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_rtc_iram_fast(const void *p) {
- #if SOC_RTC_FAST_MEM_SUPPORTED
- return ((intptr_t)p >= SOC_RTC_IRAM_LOW && (intptr_t)p < SOC_RTC_IRAM_HIGH);
- #else
- return false;
- #endif
- }
- /**
- * @brief Check if the pointer is in rtc_dram_fast
- *
- * @param p pointer
- *
- * @return true: is in rtc_dram_fast; false: not in rtc_dram_fast
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_rtc_dram_fast(const void *p) {
- #if SOC_RTC_FAST_MEM_SUPPORTED
- return ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
- #else
- return false;
- #endif
- }
- /**
- * @brief Check if the pointer is in rtc_slow
- *
- * @param p pointer
- *
- * @return true: is in rtc_slow; false: not in rtc_slow
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_rtc_slow(const void *p) {
- #if SOC_RTC_SLOW_MEM_SUPPORTED
- return ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
- #else
- return false;
- #endif
- }
- /* Convert a D/IRAM DRAM pointer to equivalent word address in IRAM
- - Address must be word aligned
- - Address must pass esp_ptr_in_diram_dram() test, or result will be invalid pointer
- */
- __attribute__((always_inline))
- inline static void * esp_ptr_diram_dram_to_iram(const void *p) {
- #if SOC_DIRAM_INVERTED
- return (void *) ( SOC_DIRAM_IRAM_LOW + (SOC_DIRAM_DRAM_HIGH - (intptr_t)p) - 4);
- #else
- return (void *) ( SOC_DIRAM_IRAM_LOW + ((intptr_t)p - SOC_DIRAM_DRAM_LOW) );
- #endif
- }
- /* Convert a D/IRAM IRAM pointer to equivalent word address in DRAM
- - Address must be word aligned
- - Address must pass esp_ptr_in_diram_iram() test, or result will be invalid pointer
- */
- __attribute__((always_inline))
- inline static void * esp_ptr_diram_iram_to_dram(const void *p) {
- #if SOC_DIRAM_INVERTED
- return (void *) ( SOC_DIRAM_DRAM_LOW + (SOC_DIRAM_IRAM_HIGH - (intptr_t)p) - 4);
- #else
- return (void *) ( SOC_DIRAM_DRAM_LOW + ((intptr_t)p - SOC_DIRAM_IRAM_LOW) );
- #endif
- }
- #if SOC_MEM_TCM_SUPPORTED
- /**
- * @brief Check if the pointer is in TCM
- *
- * @param p pointer
- *
- * @return true: is in TCM; false: not in TCM
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_tcm(const void *p) {
- return ((intptr_t)p >= SOC_TCM_LOW && (intptr_t)p < SOC_TCM_HIGH);
- }
- #endif //#if SOC_MEM_TCM_SUPPORTED
- /** End of common functions to be kept in sync with bootloader_memory_utils.h **/
- /** Add app-specific functions below **/
- /**
- * @brief Check if the pointer is dma capable
- *
- * @param p pointer
- *
- * @return true: capable; false: not capable
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_dma_capable(const void *p)
- {
- return (intptr_t)p >= SOC_DMA_LOW && (intptr_t)p < SOC_DMA_HIGH;
- }
- /**
- * @brief Check if the pointer is in external ram dma capable region
- *
- * @param p pointer
- *
- * @return true: capable; false: not capable
- */
- bool esp_ptr_dma_ext_capable(const void *p);
- /**
- * @brief Check if the pointer is word aligned
- *
- * @param p pointer
- *
- * @return true: aligned; false: not aligned
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_word_aligned(const void *p)
- {
- return ((intptr_t)p) % 4 == 0;
- }
- /**
- * @brief Check if the pointer is executable
- *
- * @param p pointer
- *
- * @return true: is executable; false: not executable
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_executable(const void *p)
- {
- intptr_t ip = (intptr_t) p;
- return (ip >= SOC_IROM_LOW && ip < SOC_IROM_HIGH)
- || (ip >= SOC_IRAM_LOW && ip < SOC_IRAM_HIGH)
- || (ip >= SOC_IROM_MASK_LOW && ip < SOC_IROM_MASK_HIGH)
- #if defined(SOC_CACHE_APP_LOW) && defined(CONFIG_FREERTOS_UNICORE)
- || (ip >= SOC_CACHE_APP_LOW && ip < SOC_CACHE_APP_HIGH)
- #endif
- #if SOC_RTC_FAST_MEM_SUPPORTED
- || (ip >= SOC_RTC_IRAM_LOW && ip < SOC_RTC_IRAM_HIGH)
- #endif
- ;
- }
- /**
- * @brief Check if the pointer is byte accessible
- *
- * @param p pointer
- *
- * @return true: is byte accessible; false: not byte accessible
- */
- bool esp_ptr_byte_accessible(const void *p);
- /**
- * @brief Check if the pointer is in internal ram
- *
- * @param p pointer
- *
- * @return true: is in internal ram; false: not in internal ram
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_internal(const void *p) {
- bool r;
- r = ((intptr_t)p >= SOC_MEM_INTERNAL_LOW && (intptr_t)p < SOC_MEM_INTERNAL_HIGH);
- #if SOC_RTC_SLOW_MEM_SUPPORTED
- r |= ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
- #endif
- #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
- /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
- * for single core configuration (where it gets added to system heap) following
- * additional check is required */
- r |= ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
- #endif
- #if CONFIG_ESP32S3_DATA_CACHE_16KB
- /* For ESP32-S3, when the DCACHE size is set to 16 kB, the unused 48 kB is
- * added to the heap in 2 blocks of 32 kB (from 0x3FCF0000) and 16 kB
- * (from 0x3C000000 (SOC_DROM_LOW) - 0x3C004000).
- * Though this memory lies in the external memory vaddr, it is no different
- * from the internal RAM in terms of hardware attributes and it is a part of
- * the internal RAM when added to the heap.*/
- r |= ((intptr_t)p >= SOC_DROM_LOW && (intptr_t)p < (SOC_DROM_LOW + 0x4000));
- #endif
- return r;
- }
- /**
- * @brief Check if the pointer is in external ram
- *
- * @param p pointer
- *
- * @return true: is in external ram; false: not in external ram
- */
- bool esp_ptr_external_ram(const void *p);
- /**
- * @brief Check if the pointer is in drom
- *
- * @param p pointer
- *
- * @return true: is in drom; false: not in drom
- */
- __attribute__((always_inline))
- inline static bool esp_ptr_in_drom(const void *p) {
- int32_t drom_start_addr = SOC_DROM_LOW;
- #if CONFIG_ESP32S3_DATA_CACHE_16KB
- /* For ESP32-S3, when the DCACHE size is set to 16 kB, the unused 48 kB is
- * added to the heap in 2 blocks of 32 kB (from 0x3FCF0000) and 16 kB
- * (from 0x3C000000 (SOC_DROM_LOW) - 0x3C004000).
- * The drom_start_addr has to be moved by 0x4000 (16kB) to accomodate
- * this addition. */
- drom_start_addr += 0x4000;
- #endif
- return ((intptr_t)p >= drom_start_addr && (intptr_t)p < SOC_DROM_HIGH);
- }
- /**
- * @brief Check if the stack pointer is in dram
- *
- * @param sp stack pointer
- *
- * @return true: is in dram; false: not in dram
- */
- __attribute__((always_inline))
- inline static bool esp_stack_ptr_in_dram(uint32_t sp)
- {
- //Check if stack ptr is in between SOC_DRAM_LOW and SOC_DRAM_HIGH, and 16 byte aligned.
- return !(sp < SOC_DRAM_LOW + 0x10 || sp > SOC_DRAM_HIGH - 0x10 || ((sp & 0xF) != 0));
- }
- #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
- /**
- * @brief Check if the stack pointer is in external ram
- *
- * @param sp stack pointer
- *
- * @return true: is in external ram; false: not in external ram
- */
- bool esp_stack_ptr_in_extram(uint32_t sp);
- #endif
- /**
- * @brief Check if the stack pointer is sane
- *
- * @param sp stack pointer
- *
- * @return true: is in sane; false: not in sane
- */
- __attribute__((always_inline))
- inline static bool esp_stack_ptr_is_sane(uint32_t sp)
- {
- return esp_stack_ptr_in_dram(sp)
- #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
- || esp_stack_ptr_in_extram(sp)
- #endif
- #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
- || esp_ptr_in_rtc_dram_fast((void*) sp)
- #endif
- ;
- }
- #ifdef __cplusplus
- }
- #endif
|