1 /*
2  * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include "esp_err.h"
12 #include "esp_memprot_err.h"
13 #include "hal/memprot_types.h"
14 #include "esp_memprot_types.h"
15 #include "sdkconfig.h"
16 #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
17 #include "soc_memprot_types.h"
18 #endif
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25 * @brief Convert Memprot low level errors to esp_err_t
26 */
27 esp_err_t esp_mprot_ll_err_to_esp_err(const memprot_hal_err_t err);
28 
29 /**
30  * @brief Convert Memprot low level PMS World IDs to esp_mprot_pms_world_t
31  */
32 esp_mprot_pms_world_t esp_mprot_ll_world_to_hl_world(const memprot_hal_world_t world);
33 
34 /**
35  * @brief Converts operation type to string, no combination of operations allowed
36  *
37  * @param oper_type PMS operation type
38  */
39 const char *esp_mprot_oper_type_to_str(const uint32_t oper_type);
40 
41 /**
42  * @brief Converts PMS World type to string
43  *
44  * @param area_type PMS World type
45  */
46 const char *esp_mprot_pms_world_to_str(const esp_mprot_pms_world_t world_type);
47 
48 /**
49  * @brief Sets splitting address for given line type in the target Memory type
50  *
51  * @param mem_type memory type
52  * @param line_type split address type
53  * @param line_addr target address from a memory range relevant to given line_addr
54  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
55  *
56  * @return ESP_OK on success
57  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
58  *         ESP_ERR_MEMPROT_SPLIT_ADDR_INVALID on invalid line_type
59  *         ESP_ERR_MEMPROT_SPLIT_ADDR_OUT_OF_RANGE on splitting line out of given memory-type range
60  *         ESP_ERR_MEMPROT_SPLIT_ADDR_UNALIGNED on splitting line not aligned to PMS-required boundaries
61  */
62 esp_err_t esp_mprot_set_split_addr(const esp_mprot_mem_t mem_type, const esp_mprot_split_addr_t line_type, const void *line_addr, const int core);
63 
64 /**
65  * @brief Gets PMS splitting address for given split_addr type
66  *
67  * The value is read from the PMS configuration registers
68  *
69  * @param mem_type memory type
70  * @param line_type Split line type (see esp_mprot_split_addr_t enum)
71  * @param[out] line_addr Split line address from the configuration register
72  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
73  *
74  * @return ESP_OK on success
75  *         ESP_ERR_INVALID_ARG on line_addr is pointer
76  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
77  *         ESP_ERR_MEMPROT_SPLIT_ADDR_INVALID on invalid line_type
78  */
79 esp_err_t esp_mprot_get_split_addr(const esp_mprot_mem_t mem_type, const esp_mprot_split_addr_t line_type, void **line_addr, const int core);
80 
81 /**
82  * @brief Returns default main I/D splitting address for given Memory type
83  *
84  * @param mem_type memory type
85  * @param[out] def_split_addr Main I/D splitting address of required mem_type
86  *
87  * @return ESP_OK on success
88  *         ESP_ERR_INVALID_ARG on invalid def_split_addr pointer
89  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
90  */
91 esp_err_t esp_mprot_get_default_main_split_addr(const esp_mprot_mem_t mem_type, void **def_split_addr);
92 
93 /**
94  * @brief Sets a lock for the main IRAM/DRAM splitting addresses
95  * Locks can be unlocked only by digital system reset
96  *
97  * @param mem_type memory type
98  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
99  *
100  * @return ESP_OK on success
101  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
102  */
103 esp_err_t esp_mprot_set_split_addr_lock(const esp_mprot_mem_t mem_type, const int core);
104 
105 /**
106  * @brief Gets a lock status for the splitting address configuration of given Memory type
107  *
108  * @param mem_type memory type
109  * @param[out] locked mem_type related lock status
110  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
111  *
112  * @return ESP_OK on success
113  *         ESP_ERR_INVALID_ARGUMENT on invalid locked pointer
114  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
115  */
116 esp_err_t esp_mprot_get_split_addr_lock(const esp_mprot_mem_t mem_type, bool *locked, const int core);
117 
118 /**
119  * @brief Sets a lock for PMS Area settings of required Memory type
120  * Locks can be unlocked only by digital system reset
121  *
122  * @param mem_type memory type
123  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
124  *
125  * @return ESP_OK on success
126  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
127  */
128 esp_err_t esp_mprot_set_pms_lock(const esp_mprot_mem_t mem_type, const int core);
129 
130 /**
131  * @brief Gets a lock status for PMS Area settings of required Memory type
132  *
133  * @param mem_type memory type
134  * @param[out] locked mem_type related lock status
135  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
136  *
137  * @return ESP_OK on success
138  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
139  *         ESP_ERR_INVALID_ARGUMENT on invalid locked pointer
140  */
141 esp_err_t esp_mprot_get_pms_lock(const esp_mprot_mem_t mem_type, bool *locked, const int core);
142 
143 /**
144  * @brief Sets permissions for given PMS Area
145  *
146  * @param area_type PMS area type
147  * @param flags combination of MEMPROT_OP_* defines
148  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
149  *
150  * @return ESP_OK on success
151  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
152  */
153 esp_err_t esp_mprot_set_pms_area(const esp_mprot_pms_area_t area_type, const uint32_t flags, const int core);
154 
155 /**
156  * @brief Gets current permissions for given PMS Area
157  *
158  * @param area_type PMS area type
159  * @param[out] flags combination of MEMPROT_OP_* defines
160  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
161  *
162  * @return ESP_OK on success
163  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
164  *         ESP_ERR_INVALID_ARG on invalid flags pointer
165  */
166 esp_err_t esp_mprot_get_pms_area(const esp_mprot_pms_area_t area_type, uint32_t *flags, const int core);
167 
168 /**
169  * @brief Sets a lock for PMS interrupt monitor settings of required Memory type
170  *
171  * Locks can be unlocked only by digital system reset
172  *
173  * @param mem_type memory type (see esp_mprot_mem_t enum)
174  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
175  *
176  * @return ESP_OK on success
177  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
178  */
179 esp_err_t esp_mprot_set_monitor_lock(const esp_mprot_mem_t mem_type, const int core);
180 
181 /**
182  * @brief Gets a lock status for PMS interrupt monitor settings of required Memory type
183  *
184  * @param mem_type memory type
185  * @param[out] locked mem_type related lock status
186  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
187  *
188  * @return ESP_OK on success
189  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
190  *         ESP_ERR_INVALID_ARG on invalid locked pointer
191  */
192 esp_err_t esp_mprot_get_monitor_lock(const esp_mprot_mem_t mem_type, bool *locked, const int core);
193 
194 /**
195  * @brief Enable PMS violation interrupt monitoring of required Memory type
196  *
197  * @param mem_type memory type
198  * @param enable enable/disable violation interrupt monitoring
199  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
200  *
201  * @return ESP_OK on success
202  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
203  */
204 esp_err_t esp_mprot_set_monitor_en(const esp_mprot_mem_t mem_type, const bool enable, const int core);
205 
206 /**
207  * @brief Gets PMS violation-monitoring-enabled flag for required Memory type
208  *
209  * @param mem_type memory type
210  * @param[out] enabled violation interrupt monitoring enable flag
211  * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
212  *
213  * @return ESP_OK on success
214  *         ESP_ERR_INVALID_ARG on invalid enabled pointer
215  *         ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
216  */
217 esp_err_t esp_mprot_get_monitor_en(const esp_mprot_mem_t mem_type, bool* enabled, const int core);
218 
219 #ifdef __cplusplus
220 }
221 #endif
222