1 /*
2  * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 //////////////////////////////////////////////////////////
8 // ESP32-C3 PMS memory protection types
9 //
10 
11 #pragma once
12 
13 #include <stdint.h>
14 #include <stdbool.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * @brief Memory types recognized by PMS
22  */
23 typedef enum {
24     MEMPROT_TYPE_NONE                           = 0x00000000,
25     MEMPROT_TYPE_IRAM0_SRAM                     = 0x00000001,
26     MEMPROT_TYPE_DRAM0_SRAM                     = 0x00000002,
27     MEMPROT_TYPE_IRAM0_RTCFAST                  = 0x00000004,
28     MEMPROT_TYPE_ALL                            = 0x7FFFFFFF,
29     MEMPROT_TYPE_INVALID                        = 0x80000000,
30     MEMPROT_TYPE_IRAM0_ANY                      = MEMPROT_TYPE_IRAM0_SRAM | MEMPROT_TYPE_IRAM0_RTCFAST
31 } esp_mprot_mem_t;
32 
33 /**
34  * @brief Splitting address (line) type
35  */
36 typedef enum {
37     MEMPROT_SPLIT_ADDR_NONE                     = 0x00000000,
38     MEMPROT_SPLIT_ADDR_IRAM0_DRAM0              = 0x00000001,
39     MEMPROT_SPLIT_ADDR_IRAM0_LINE_0             = 0x00000002,
40     MEMPROT_SPLIT_ADDR_IRAM0_LINE_1             = 0x00000004,
41     MEMPROT_SPLIT_ADDR_DRAM0_DMA_LINE_0         = 0x00000008,
42     MEMPROT_SPLIT_ADDR_DRAM0_DMA_LINE_1         = 0x00000010,
43     MEMPROT_SPLIT_ADDR_ALL                      = 0x7FFFFFFF,
44     MEMPROT_SPLIT_ADDR_INVALID                  = 0x80000000,
45     MEMPROT_SPLIT_ADDR_MAIN                     = MEMPROT_SPLIT_ADDR_IRAM0_DRAM0
46 } esp_mprot_split_addr_t;
47 
48 /**
49  * @brief PMS area type (memory space between adjacent splitting addresses or above/below the main splt.address)
50  */
51 typedef enum {
52     MEMPROT_PMS_AREA_NONE                       = 0x00000000,
53     MEMPROT_PMS_AREA_IRAM0_0                    = 0x00000001,
54     MEMPROT_PMS_AREA_IRAM0_1                    = 0x00000002,
55     MEMPROT_PMS_AREA_IRAM0_2                    = 0x00000004,
56     MEMPROT_PMS_AREA_IRAM0_3                    = 0x00000008,
57     MEMPROT_PMS_AREA_DRAM0_0                    = 0x00000010,
58     MEMPROT_PMS_AREA_DRAM0_1                    = 0x00000020,
59     MEMPROT_PMS_AREA_DRAM0_2                    = 0x00000040,
60     MEMPROT_PMS_AREA_DRAM0_3                    = 0x00000080,
61     MEMPROT_PMS_AREA_IRAM0_RTCFAST_LO           = 0x00000100,
62     MEMPROT_PMS_AREA_IRAM0_RTCFAST_HI           = 0x00000200,
63     MEMPROT_PMS_AREA_ALL                        = 0x7FFFFFFF,
64     MEMPROT_PMS_AREA_INVALID                    = 0x80000000
65 } esp_mprot_pms_area_t;
66 
67 /**
68 * @brief Memory protection configuration
69 */
70 typedef struct {
71     bool invoke_panic_handler;  /*!< Register PMS violation interrupt for panic-handling */
72     bool lock_feature;          /*!< Lock all PMS settings */
73     void *split_addr;           /*!< Main I/D splitting address */
74     uint32_t mem_type_mask;     /*!< Memory types required to protect. See esp_mprot_mem_t enum */
75 } esp_memp_config_t;
76 
77 #define ESP_MEMPROT_DEFAULT_CONFIG() { \
78     .invoke_panic_handler = true, \
79     .lock_feature = true, \
80     .split_addr = NULL, \
81     .mem_type_mask = MEMPROT_TYPE_ALL \
82 }
83 
84 /**
85  * @brief Converts Memory protection type to string
86  *
87  * @param mem_type Memory protection type
88  */
esp_mprot_mem_type_to_str(const esp_mprot_mem_t mem_type)89 static inline const char *esp_mprot_mem_type_to_str(const esp_mprot_mem_t mem_type)
90 {
91     switch (mem_type) {
92     case MEMPROT_TYPE_NONE:
93         return "NONE";
94     case MEMPROT_TYPE_IRAM0_SRAM:
95         return "IRAM0_SRAM";
96     case MEMPROT_TYPE_DRAM0_SRAM:
97         return "DRAM0_SRAM";
98     case MEMPROT_TYPE_IRAM0_RTCFAST:
99         return "IRAM0_RTCFAST";
100     case MEMPROT_TYPE_IRAM0_ANY:
101         return "IRAM0_ANY";
102     case MEMPROT_TYPE_ALL:
103         return "ALL";
104     default:
105         return "INVALID";
106     }
107 }
108 
109 /**
110  * @brief Converts Splitting address type to string
111  *
112  * @param line_type Split line type
113  */
esp_mprot_split_addr_to_str(const esp_mprot_split_addr_t line_type)114 static inline const char *esp_mprot_split_addr_to_str(const esp_mprot_split_addr_t line_type)
115 {
116     switch (line_type) {
117     case MEMPROT_SPLIT_ADDR_NONE:
118         return "SPLIT_ADDR_NONE";
119     case MEMPROT_SPLIT_ADDR_IRAM0_DRAM0:
120         return "SPLIT_ADDR_IRAM0_DRAM0";
121     case MEMPROT_SPLIT_ADDR_IRAM0_LINE_0:
122         return "SPLIT_ADDR_IRAM0_LINE_0";
123     case MEMPROT_SPLIT_ADDR_IRAM0_LINE_1:
124         return "SPLIT_ADDR_IRAM0_LINE_1";
125     case MEMPROT_SPLIT_ADDR_DRAM0_DMA_LINE_0:
126         return "SPLIT_ADDR_DRAM0_DMA_LINE_0";
127     case MEMPROT_SPLIT_ADDR_DRAM0_DMA_LINE_1:
128         return "SPLIT_ADDR_DRAM0_DMA_LINE_1";
129     case MEMPROT_SPLIT_ADDR_ALL:
130         return "SPLIT_ADDR_ALL";
131     default:
132         return "SPLIT_ADDR_INVALID";
133     }
134 }
135 
136 /**
137  * @brief Converts PMS Area type to string
138  *
139  * @param area_type PMS Area type
140  */
esp_mprot_pms_area_to_str(const esp_mprot_pms_area_t area_type)141 static inline const char *esp_mprot_pms_area_to_str(const esp_mprot_pms_area_t area_type)
142 {
143     switch (area_type) {
144     case MEMPROT_PMS_AREA_NONE:
145         return "PMS_AREA_NONE";
146     case MEMPROT_PMS_AREA_IRAM0_0:
147         return "PMS_AREA_IRAM0_0";
148     case MEMPROT_PMS_AREA_IRAM0_1:
149         return "PMS_AREA_IRAM0_1";
150     case MEMPROT_PMS_AREA_IRAM0_2:
151         return "PMS_AREA_IRAM0_2";
152     case MEMPROT_PMS_AREA_IRAM0_3:
153         return "PMS_AREA_IRAM0_3";
154     case MEMPROT_PMS_AREA_DRAM0_0:
155         return "PMS_AREA_DRAM0_0";
156     case MEMPROT_PMS_AREA_DRAM0_1:
157         return "PMS_AREA_DRAM0_1";
158     case MEMPROT_PMS_AREA_DRAM0_2:
159         return "PMS_AREA_DRAM0_2";
160     case MEMPROT_PMS_AREA_DRAM0_3:
161         return "PMS_AREA_DRAM0_3";
162     case MEMPROT_PMS_AREA_IRAM0_RTCFAST_LO:
163         return "PMS_AREA_IRAM0_RTCFAST_LO";
164     case MEMPROT_PMS_AREA_IRAM0_RTCFAST_HI:
165         return "PMS_AREA_IRAM0_RTCFAST_HI";
166     case MEMPROT_PMS_AREA_ALL:
167         return "PMS_AREA_ALL";
168     default:
169         return "PMS_AREA_INVALID";
170     }
171 }
172 
173 #ifdef __cplusplus
174 }
175 #endif
176