|
85 | 85 | #define _DT_SECTION_SIZE(node_id) UTIL_CAT(_DT_SECTION_PREFIX(node_id), _size) |
86 | 86 | #define _DT_SECTION_LOAD(node_id) UTIL_CAT(_DT_SECTION_PREFIX(node_id), _load_start) |
87 | 87 |
|
| 88 | +#define _DT_ATTR(token) UTIL_CAT(UTIL_CAT(REGION_, token), _ATTR) |
| 89 | + |
88 | 90 | /** |
89 | 91 | * @brief Declare a memory region |
90 | 92 | * |
|
153 | 155 | _DT_SECTION_SIZE(node_id) = _DT_SECTION_END(node_id) - _DT_SECTION_START(node_id); \ |
154 | 156 | _DT_SECTION_LOAD(node_id) = LOADADDR(LINKER_DT_NODE_REGION_NAME_TOKEN(node_id)); |
155 | 157 |
|
| 158 | +/** |
| 159 | + * Call the user-provided MPU_FN() macro passing the expected arguments |
| 160 | + */ |
| 161 | +#define _EXPAND_MPU_FN(node_id, MPU_FN, ...) \ |
| 162 | + MPU_FN(LINKER_DT_NODE_REGION_NAME(node_id), \ |
| 163 | + DT_REG_ADDR(node_id), \ |
| 164 | + DT_REG_SIZE(node_id), \ |
| 165 | + _DT_ATTR(DT_STRING_TOKEN(node_id, zephyr_memory_region_mpu))), |
| 166 | + |
| 167 | +/** |
| 168 | + * Check that the node_id has both properties: |
| 169 | + * - zephyr,memory-region-mpu |
| 170 | + * - zephyr,memory-region |
| 171 | + * |
| 172 | + * and call the EXPAND_MPU_FN() macro |
| 173 | + */ |
| 174 | +#define _CHECK_ATTR_FN(node_id, EXPAND_MPU_FN, ...) \ |
| 175 | + COND_CODE_1(UTIL_AND(DT_NODE_HAS_PROP(node_id, zephyr_memory_region_mpu), \ |
| 176 | + DT_NODE_HAS_PROP(node_id, zephyr_memory_region)), \ |
| 177 | + (EXPAND_MPU_FN(node_id, __VA_ARGS__)), \ |
| 178 | + ()) |
| 179 | + |
| 180 | +/** |
| 181 | + * Call _CHECK_ATTR_FN() for each enabled node passing EXPAND_MPU_FN() as |
| 182 | + * explicit argument and the user-provided MPU_FN() macro in __VA_ARGS__ |
| 183 | + */ |
| 184 | +#define _CHECK_APPLY_FN(compat, EXPAND_MPU_FN, ...) \ |
| 185 | + DT_FOREACH_STATUS_OKAY_VARGS(compat, _CHECK_ATTR_FN, EXPAND_MPU_FN, __VA_ARGS__) |
| 186 | + |
156 | 187 | /** @endcond */ |
157 | 188 |
|
158 | 189 | /** |
|
172 | 203 | */ |
173 | 204 | #define LINKER_DT_SECTIONS() \ |
174 | 205 | DT_FOREACH_STATUS_OKAY(_DT_COMPATIBLE, _SECTION_DECLARE) |
| 206 | + |
| 207 | +/** |
| 208 | + * @brief Generate MPU regions from the device tree nodes with compatible |
| 209 | + * 'zephyr,memory-region' and 'zephyr,memory-region-mpu' attribute. |
| 210 | + * |
| 211 | + * Helper macro to apply an MPU_FN macro to all the memory regions declared |
| 212 | + * using the 'zephyr,memory-region-mpu' property and the 'zephyr,memory-region' |
| 213 | + * compatible. |
| 214 | + * |
| 215 | + * @p MPU_FN must take the form: |
| 216 | + * |
| 217 | + * @code{.c} |
| 218 | + * #define MPU_FN(name, base, size, attr) ... |
| 219 | + * @endcode |
| 220 | + * |
| 221 | + * The 'name', 'base' and 'size' parameters are taken from the DT node. |
| 222 | + * |
| 223 | + * The 'zephyr,memory-region-mpu' enum property is passed as an extended token |
| 224 | + * to the MPU_FN macro using the 'attr' parameter, in the form |
| 225 | + * REGION_{attr}_ATTR. |
| 226 | + * |
| 227 | + * Currently only three enums are supported for the 'zephyr,memory-region-mpu' |
| 228 | + * property: |
| 229 | + * |
| 230 | + * - RAM |
| 231 | + * - RAM_NOCACHE |
| 232 | + * - FLASH |
| 233 | + * |
| 234 | + * This means that usually the arch code would provide some macros or defines |
| 235 | + * with the same name of the extended property, that is: |
| 236 | + * |
| 237 | + * - REGION_RAM_ATTR |
| 238 | + * - REGION_RAM_NOCACHE_ATTR |
| 239 | + * - REGION_FLASH_ATTR |
| 240 | + * |
| 241 | + * Example devicetree fragment: |
| 242 | + * |
| 243 | + * / { |
| 244 | + * soc { |
| 245 | + * sram1: memory@2000000 { |
| 246 | + * zephyr,memory-region = "MY_NAME"; |
| 247 | + * zephyr,memory-region-mpu = "RAM_NOCACHE"; |
| 248 | + * }; |
| 249 | + * }; |
| 250 | + * }; |
| 251 | + * |
| 252 | + * The 'attr' parameter of the MPU_FN function will be the extended |
| 253 | + * 'REGION_RAM_NOCACHE_ATTR' token and the arch code will be usually |
| 254 | + * implementing a macro with the same name. |
| 255 | + * |
| 256 | + * Example: |
| 257 | + * |
| 258 | + * @code{.c} |
| 259 | + * |
| 260 | + * #define REGION_RAM_NOCACHE_ATTR 0xAAAA |
| 261 | + * #define REGION_RAM_ATTR 0xBBBB |
| 262 | + * #define REGION_FLASH_ATTR 0xCCCC |
| 263 | + * |
| 264 | + * #define MPU_FN(p_name, p_base, p_size, p_attr) \ |
| 265 | + * { \ |
| 266 | + * .name = p_name, \ |
| 267 | + * .base = p_base, \ |
| 268 | + * .size = p_size, \ |
| 269 | + * .attr = p_attr, \ |
| 270 | + * } |
| 271 | + * |
| 272 | + * static const struct arm_mpu_region mpu_regions[] = { |
| 273 | + * ... |
| 274 | + * LINKER_DT_REGION_MPU(MPU_FN) |
| 275 | + * ... |
| 276 | + * }; |
| 277 | + * @endcode |
| 278 | + * |
| 279 | + */ |
| 280 | +#define LINKER_DT_REGION_MPU(mpu_fn) _CHECK_APPLY_FN(_DT_COMPATIBLE, _EXPAND_MPU_FN, mpu_fn) |
0 commit comments