1 /** 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 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 * Description: Provides MPU driver api \n 16 * 17 * History: \n 18 * 2023-01-16, Create file. \n 19 */ 20 21 #ifndef MPU_H 22 #define MPU_H 23 24 #include <stdint.h> 25 #include <stdbool.h> 26 #include "errcode.h" 27 28 #ifdef __cplusplus 29 #if __cplusplus 30 extern "C" { 31 #endif /* __cplusplus */ 32 #endif /* __cplusplus */ 33 34 /** 35 * @defgroup drivers_driver_mpu MPU 36 * @ingroup drivers_driver 37 * @{ 38 */ 39 40 /** 41 * @if Eng 42 * @brief MPU configuration. 43 * @else 44 * @brief MPU配置信息定义。 45 * @endif 46 */ 47 typedef struct mpu_config { 48 /** 49 * @if Eng 50 * @brief MPU region addr. Needs to be divisible by mpu_region_len. 51 * @else 52 * @brief MPU域地址,需要被mpu_region_len整除。 53 * @endif 54 */ 55 uint32_t mpu_region_addr; 56 /** 57 * @if Eng MPU region length. 58 -b00000~b00011: reserved. 59 -b00100: 32B 60 -b00101: 64B 61 -b00110: 128B 62 -b00111: 256B 63 -b01000: 512B 64 -b01001: 1KB 65 -b01010: 2KB 66 -b01011: 4KB 67 -b01100: 8KB 68 -b01101: 16KB 69 -b01110: 32KB 70 -b01111: 64B 71 -b10000: 128KB 72 -b10001: 256KB 73 -b10010: 512KB 74 -b10011: 1MB 75 -b10100: 2MB 76 -b10101: 4MB 77 -b10110: 8MB 78 -b10111: 16MB 79 -b11000: 32MB 80 -b11001: 64MB 81 -b11010: 128MB 82 -b11011: 256MB 83 -b11100: 512MB 84 -b11101: 1GM 85 -b11110: 2GB 86 -b11111: 4GB 87 * @else MPU域长度。 88 -b00000~b00011: reserved. 89 -b00100: 32B 90 -b00101: 64B 91 -b00110: 128B 92 -b00111: 256B 93 -b01000: 512B 94 -b01001: 1KB 95 -b01010: 2KB 96 -b01011: 4KB 97 -b01100: 8KB 98 -b01101: 16KB 99 -b01110: 32KB 100 -b01111: 64B 101 -b10000: 128KB 102 -b10001: 256KB 103 -b10010: 512KB 104 -b10011: 1MB 105 -b10100: 2MB 106 -b10101: 4MB 107 -b10110: 8MB 108 -b10111: 16MB 109 -b11000: 32MB 110 -b11001: 64MB 111 -b11010: 128MB 112 -b11011: 256MB 113 -b11100: 512MB 114 -b11101: 1GM 115 -b11110: 2GB 116 -b11111: 4GB 117 * @endif 118 */ 119 uint32_t mpu_region_len; 120 /** 121 * @if Eng 122 * @brief MPU access permit. 123 * @else 124 * @brief MPU数据访问的权限。 125 * @endif 126 */ 127 uint8_t mpu_rasr_ap; 128 /** 129 * @if Eng 130 * @brief MPU enable buffur bit. 131 * @else 132 * @brief MPU缓冲使能位。 133 * @endif 134 */ 135 bool mpu_rasr_buffer_en; 136 /** 137 * @if Eng 138 * @brief MPU enable indicates access bit. 139 * @else 140 * @brief MPU指令访问使能位。 141 * @endif 142 */ 143 bool mpu_rasr_exec_en; 144 /** 145 * @if Eng 146 * @brief MPU type extension bit. 147 * @else 148 * @brief MPU类型展开域。 149 * @endif 150 */ 151 uint8_t mpu_rasr_tex; 152 /** 153 * @if Eng 154 * @brief MPU enable cache bit. 155 * @else 156 * @brief MPU缓存使能位。 157 * @endif 158 */ 159 bool mpu_rasr_cache_en; 160 /** 161 * @if Eng 162 * @brief MPU enable share bit. 163 * @else 164 * @brief MPU共用使能位。 165 * @endif 166 */ 167 bool mpu_rasr_share_en; 168 /** 169 * @if Eng 170 * @brief MPU disable subregion. 171 * @else 172 * @brief MPU子域禁止。 173 * @endif 174 */ 175 uint8_t mpu_rasr_srd; 176 } mpu_config_t; 177 178 /** 179 * @if Eng 180 * @brief Initialize MPU. 181 * @retval ERRCODE_SUCC Success. 182 * @retval Other Failure. For details, see @ref errcode_t. 183 * @else 184 * @brief 初始化MPU。 185 * @retval ERRCODE_SUCC 成功。 186 * @retval Other 失败,参考 @ref errcode_t 。 187 * @endif 188 */ 189 errcode_t uapi_mpu_init(void); 190 191 /** 192 * @if Eng 193 * @brief Deinitialize MPU. 194 * @else 195 * @brief 去初始化MPU。 196 * @endif 197 */ 198 void uapi_mpu_deinit(void); 199 200 /** 201 * @if Eng 202 * @brief Config MPU. 203 * @param [in] mpu_cfg MPU config table. 204 * @param [in] mpu_cfg_size lenth of MPU config table. 205 * @retval ERRCODE_SUCC Success. 206 * @retval Other Failure. For details, see @ref errcode_t. 207 * @else 208 * @brief 配置MPU。 209 * @param [in] mpu_cfg MPU配置项。 210 * @param [in] mpu_cfg_size MPU配置项长度。 211 * @retval ERRCODE_SUCC 成功。 212 * @retval Other 失败,参考 @ref errcode_t 。 213 * @endif 214 */ 215 errcode_t uapi_mpu_config(const mpu_config_t *mpu_cfg, uint8_t mpu_cfg_size); 216 217 /** 218 * @if Eng 219 * @brief Enable MPU. 220 * @param [in] open_privdef Enables the default memory map as a background region for privileged access. 221 * @param [in] enable_hfnmi Enable mpu hfnmi. 222 * Controls whether handlers executing with priority less than 0 access memory 223 * with the MPU enabled or with the MPU disabled.This applies to HardFaults, NMIs, 224 * and exception handlers when FAULTMASK is set to 1. 225 * -true: Disables the MPU, use these handlers. 226 * -false: Use the MPU for memory accesses by these handlers. 227 * @else 228 * @brief 使能MPU。 229 * @param [in] open_privdef 启用默认内存映射作为特权访问的背景域。 230 * @param [in] enable_hfnmi 使能mpu hfnmi。 231 * 控制以小于0的优先级执行的处理程序是在MPU启用还是MPU禁用的情况下访问内存。 232 * 当故障掩码设置为1时,这适用于HardFaults、NMI和异常处理程序。 233 * -true:禁用MPU,使用这些处理程序。 234 * -false:使用MPU进行这些处理程序的内存访问。 235 * @endif 236 */ 237 void uapi_mpu_enable(bool open_privdef, bool enable_hfnmi); 238 239 /** 240 * @if Eng 241 * @brief Disable MPU. 242 * @else 243 * @brief 去使能MPU。 244 * @endif 245 */ 246 void uapi_mpu_disable(void); 247 248 /** 249 * @if Eng 250 * @brief Enable MPU region. 251 * @param [in] mpu_region_num Enable which one region. 252 * @param [in] mpu_cfg Region config. mpu_region_len size need be 2^n, minimum is 32. And region_addr needs 253 * to be divisible by region_len.The region_addr is low than 5 is required to be 0. 254 * @retval ERRCODE_SUCC Success. 255 * @retval Other Failure. For details, see @ref errcode_t. 256 * @else 257 * @brief 使能PMU域功能。 258 * @param [in] mpu_region_num 使能哪一个域。 259 * @param [in] mpu_cfg 域配置。 260 * mpu_region_len大小需要为2^n,最小为32。 261 * 并且region_addr需要被region_len整除,region_addr低于5则视为0。 262 * @retval ERRCODE_SUCC 成功。 263 * @retval Other 失败,参考 @ref errcode_t 。 264 * @endif 265 */ 266 errcode_t uapi_mpu_region_enable(uint8_t mpu_region_num, const mpu_config_t *mpu_cfg); 267 268 /** 269 * @if Eng 270 * @brief Disable MPU region. 271 * @param [in] mpu_region_num Enable which one region. 272 * @retval ERRCODE_SUCC Success. 273 * @retval Other Failure. For details, see @ref errcode_t. 274 * @else 275 * @brief 去使能PMU域功能。 276 * @param [in] mpu_region_num 使能哪一个域。 277 * @retval ERRCODE_SUCC 成功。 278 * @retval Other 失败,参考 @ref errcode_t 。 279 * @endif 280 */ 281 errcode_t uapi_mpu_region_disable(uint8_t mpu_region_num); 282 283 /** 284 * @} 285 */ 286 287 #ifdef __cplusplus 288 #if __cplusplus 289 } 290 #endif /* __cplusplus */ 291 #endif /* __cplusplus */ 292 293 #endif 294