1 /*
2  * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <stdbool.h>
9 
10 #include "sdkconfig.h"
11 #include "soc/soc.h"
12 #include "soc/soc_caps.h"
13 #include "esp_attr.h"
14 #include "esp_memory_utils.h"
15 #if CONFIG_SPIRAM
16 #include "esp_private/esp_psram_extram.h"
17 #endif
18 
19 
esp_ptr_dma_ext_capable(const void * p)20 bool esp_ptr_dma_ext_capable(const void *p)
21 {
22 #if !SOC_PSRAM_DMA_CAPABLE
23     return false;
24 #endif  //!SOC_PSRAM_DMA_CAPABLE
25 #if CONFIG_SPIRAM
26     return esp_psram_check_ptr_addr(p);
27 #else
28     return false;
29 #endif  //CONFIG_SPIRAM
30 }
31 
esp_ptr_byte_accessible(const void * p)32 bool esp_ptr_byte_accessible(const void *p)
33 {
34     intptr_t ip = (intptr_t) p;
35     bool r;
36     r = (ip >= SOC_BYTE_ACCESSIBLE_LOW && ip < SOC_BYTE_ACCESSIBLE_HIGH);
37 #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
38     /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
39      * for single core configuration (where it gets added to system heap) following
40      * additional check is required */
41     r |= (ip >= SOC_RTC_DRAM_LOW && ip < SOC_RTC_DRAM_HIGH);
42 #endif
43 #if CONFIG_SPIRAM
44     r |= esp_psram_check_ptr_addr(p);
45 #endif
46 #if CONFIG_ESP32S3_DATA_CACHE_16KB
47     /* For ESP32-S3, when the DCACHE size is set to 16 kB, the unused 48 kB is
48      * added to the heap in 2 blocks of 32 kB (from 0x3FCF0000) and 16 kB
49      * (from 0x3C000000 (SOC_DROM_LOW) - 0x3C004000).
50      * Though this memory lies in the external memory vaddr, it is no different
51      * from the internal RAM in terms of hardware attributes. It is a part of
52      * the internal RAM when added to the heap and is byte-accessible .*/
53     r |= (ip >= SOC_DROM_LOW && ip < (SOC_DROM_LOW + 0x4000));
54 #endif
55     return r;
56 }
57 
esp_ptr_external_ram(const void * p)58 bool esp_ptr_external_ram(const void *p)
59 {
60 #if !SOC_SPIRAM_SUPPORTED
61     return false;
62 #endif  //!SOC_SPIRAM_SUPPORTED
63 #if CONFIG_SPIRAM
64     return esp_psram_check_ptr_addr(p);
65 #else
66     return false;
67 #endif  //CONFIG_SPIRAM
68 }
69 
70 #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
esp_stack_ptr_in_extram(uint32_t sp)71 bool esp_stack_ptr_in_extram(uint32_t sp)
72 {
73     //Check if stack ptr is on PSRAM, and 16 byte aligned.
74     return (esp_psram_check_ptr_addr((void *)sp) && ((sp & 0xF) == 0));
75 }
76 #endif
77