1 // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include "sdkconfig.h"
20 
21 #define SOC_MEMORY_TYPE_NO_PRIOS 3
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Type descriptor holds a description for a particular type of memory on a particular SoC.
28  */
29 typedef struct {
30     const char *name;  ///< Name of this memory type
31     uint32_t caps[SOC_MEMORY_TYPE_NO_PRIOS]; ///< Capabilities for this memory type (as a prioritised set)
32     bool aliased_iram;   ///< If true, this is data memory that is is also mapped in IRAM
33     bool startup_stack; ///< If true, memory of this type is used for ROM stack during startup
34 } soc_memory_type_desc_t;
35 
36 /* Constant table of tag descriptors for all this SoC's tags */
37 extern const soc_memory_type_desc_t soc_memory_types[];
38 extern const size_t soc_memory_type_count;
39 
40 /* Region descriptor holds a description for a particular region of memory on a particular SoC.
41  */
42 typedef struct {
43     intptr_t start;  ///< Start address of the region
44     size_t size;            ///< Size of the region in bytes
45     size_t type;             ///< Type of the region (index into soc_memory_types array)
46     intptr_t iram_address; ///< If non-zero, is equivalent address in IRAM
47 } soc_memory_region_t;
48 
49 extern const soc_memory_region_t soc_memory_regions[];
50 extern const size_t soc_memory_region_count;
51 
52 /* Region descriptor holds a description for a particular region of
53    memory reserved on this SoC for a particular use (ie not available
54    for stack/heap usage.) */
55 typedef struct {
56     intptr_t start;
57     intptr_t end;
58 } soc_reserved_region_t;
59 
60 /* Use this macro to reserved a fixed region of RAM (hardcoded addresses)
61  * for a particular purpose.
62  *
63  * Usually used to mark out memory addresses needed for hardware or ROM code
64  * purposes.
65  *
66  * Don't call this macro from user code which can use normal C static allocation
67  * instead.
68  *
69  * @param START Start address to be reserved.
70  * @param END One after the address of the last byte to be reserved. (ie length of
71  * the reserved region is (END - START) in bytes.
72  * @param NAME Name for the reserved region. Must be a valid variable name,
73  * unique to this source file.
74  */
75 #define SOC_RESERVE_MEMORY_REGION(START, END, NAME)     \
76     __attribute__((section(".reserved_memory_address"))) __attribute__((used)) \
77     static soc_reserved_region_t reserved_region_##NAME = { START, END };
78 
79 /* Return available memory regions for this SoC. Each available memory
80  * region is a contiguous piece of memory which is not being used by
81  * static data, used by ROM code, or reserved by a component using
82  * the SOC_RESERVE_MEMORY_REGION() macro.
83  *
84  * This result is soc_memory_regions[] minus all regions reserved
85  * via the SOC_RESERVE_MEMORY_REGION() macro (which may also split
86  * some regions up.)
87  *
88  * At startup, all available memory returned by this function is
89  * registered as heap space.
90  *
91  * @note OS-level startup function only, not recommended to call from
92  * app code.
93  *
94  * @param regions Pointer to an array for reading available regions into.
95  * Size of the array should be at least the result of
96  * soc_get_available_memory_region_max_count(). Entries in the array
97  * will be ordered by memory address.
98  *
99  * @return Number of entries copied to 'regions'. Will be no greater than
100  * the result of soc_get_available_memory_region_max_count().
101  */
102 size_t soc_get_available_memory_regions(soc_memory_region_t *regions);
103 
104 /* Return the maximum number of available memory regions which could be
105  * returned by soc_get_available_memory_regions(). Used to size the
106  * array passed to that function.
107  */
108 size_t soc_get_available_memory_region_max_count(void);
109 
110 
111 #ifdef __cplusplus
112 }
113 #endif
114