|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Carlo Caione <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef ZEPHYR_INCLUDE_MEMORY_ATTR_H_ |
| 8 | +#define ZEPHYR_INCLUDE_MEMORY_ATTR_H_ |
| 9 | + |
| 10 | +#include <zephyr/devicetree.h> |
| 11 | +#include <zephyr/linker/devicetree_regions.h> |
| 12 | +#include <zephyr/sys/util.h> |
| 13 | + |
| 14 | +/** |
| 15 | + * @file |
| 16 | + * @brief Memory-attr helpers |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @defgroup devicetree-memory-attr Memory attributes |
| 21 | + * @ingroup devicetree |
| 22 | + * @{ |
| 23 | + */ |
| 24 | + |
| 25 | +/** @cond INTERNAL_HIDDEN */ |
| 26 | + |
| 27 | +#define _DT_MEM_ATTR zephyr_memory_attr |
| 28 | +#define _DT_ATTR(token) UTIL_CAT(UTIL_CAT(REGION_, token), _ATTR) |
| 29 | + |
| 30 | +#define _UNPACK(node_id, fn) \ |
| 31 | + fn(COND_CODE_1(DT_NODE_HAS_PROP(node_id, zephyr_memory_region), \ |
| 32 | + (LINKER_DT_NODE_REGION_NAME(node_id)), \ |
| 33 | + (DT_NODE_FULL_NAME(node_id))), \ |
| 34 | + DT_REG_ADDR(node_id), \ |
| 35 | + DT_REG_SIZE(node_id), \ |
| 36 | + _DT_ATTR(DT_STRING_TOKEN(node_id, _DT_MEM_ATTR))), |
| 37 | + |
| 38 | +#define _APPLY(node_id, fn) \ |
| 39 | + COND_CODE_1(DT_NODE_HAS_PROP(node_id, _DT_MEM_ATTR), \ |
| 40 | + (_UNPACK(node_id, fn)), \ |
| 41 | + ()) |
| 42 | + |
| 43 | + |
| 44 | +#define _FILTER(node_id, fn) \ |
| 45 | + COND_CODE_1(DT_NODE_HAS_PROP(node_id, _DT_MEM_ATTR), \ |
| 46 | + (fn(node_id)), \ |
| 47 | + ()) |
| 48 | + |
| 49 | +/** @endcond */ |
| 50 | + |
| 51 | +/** |
| 52 | + * @brief Invokes @p fn for every node in the tree with property |
| 53 | + * `zephyr,memory-attr` |
| 54 | + * |
| 55 | + * The macro @p fn must take one parameter, which will be a node identifier |
| 56 | + * with the `zephyr,memory-attr` property. The macro is expanded once for each |
| 57 | + * node in the tree. The order that nodes are visited in is not specified. |
| 58 | + * |
| 59 | + * @param fn macro to invoke |
| 60 | + */ |
| 61 | +#define DT_MEMORY_ATTR_FOREACH_NODE(fn) \ |
| 62 | + DT_FOREACH_STATUS_OKAY_NODE_VARGS(_FILTER, fn) |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief Invokes @p fn for MPU/MMU regions generation from the device tree |
| 66 | + * nodes with `zephyr,memory-attr` property. |
| 67 | + * |
| 68 | + * Helper macro to invoke a @p fn macro on all the memory regions declared |
| 69 | + * using the `zephyr,memory-attr` property |
| 70 | + * |
| 71 | + * The macro @p fn must take the form: |
| 72 | + * |
| 73 | + * @code{.c} |
| 74 | + * #define MPU_FN(name, base, size, attr) ... |
| 75 | + * @endcode |
| 76 | + * |
| 77 | + * The @p name, @p base and @p size parameters are retrieved from the DT node. |
| 78 | + * When the `zephyr,memory-region` property is present in the node, the @p name |
| 79 | + * parameter is retrived from there, otherwise the full node name is used. |
| 80 | + * |
| 81 | + * The `zephyr,memory-attr` enum property is passed as an extended token |
| 82 | + * to the @p fn macro using the @p attr parameter in the form of a macro |
| 83 | + * REGION_{attr}_ATTR. |
| 84 | + * |
| 85 | + * The following enums are supported for the `zephyr,memory-attr` property (see |
| 86 | + * `zephyr,memory-attr.yaml` for a complete list): |
| 87 | + * |
| 88 | + * - RAM |
| 89 | + * - RAM_NOCACHE |
| 90 | + * - FLASH |
| 91 | + * - PPB |
| 92 | + * - IO |
| 93 | + * - EXTMEM |
| 94 | + * |
| 95 | + * This means that usually the user code would provide some macros or defines |
| 96 | + * with the same name of the extended property, that is: |
| 97 | + * |
| 98 | + * - REGION_RAM_ATTR |
| 99 | + * - REGION_RAM_NOCACHE_ATTR |
| 100 | + * - REGION_FLASH_ATTR |
| 101 | + * - REGION_PPB_ATTR |
| 102 | + * - REGION_IO_ATTR |
| 103 | + * - REGION_EXTMEM_ATTR |
| 104 | + * |
| 105 | + * Example devicetree fragment: |
| 106 | + * |
| 107 | + * @code{.dts} |
| 108 | + * / { |
| 109 | + * soc { |
| 110 | + * res0: memory@20000000 { |
| 111 | + * reg = <0x20000000 0x4000>; |
| 112 | + * zephyr,memory-region = "MY_NAME"; |
| 113 | + * zephyr,memory-attr = "RAM_NOCACHE"; |
| 114 | + * }; |
| 115 | + * |
| 116 | + * res1: memory@30000000 { |
| 117 | + * reg = <0x30000000 0x2000>; |
| 118 | + * zephyr,memory-attr = "RAM"; |
| 119 | + * }; |
| 120 | +
|
| 121 | + * }; |
| 122 | + * }; |
| 123 | + * @endcode |
| 124 | + * |
| 125 | + * Example usage: |
| 126 | + * |
| 127 | + * @code{.c} |
| 128 | + * #define REGION_RAM_NOCACHE_ATTR 0xAAAA |
| 129 | + * #define REGION_RAM_ATTR 0xBBBB |
| 130 | + * #define REGION_FLASH_ATTR 0xCCCC |
| 131 | + * |
| 132 | + * #define MPU_FN(p_name, p_base, p_size, p_attr) \ |
| 133 | + * { \ |
| 134 | + * .name = p_name, \ |
| 135 | + * .base = p_base, \ |
| 136 | + * .size = p_size, \ |
| 137 | + * .attr = p_attr, \ |
| 138 | + * } |
| 139 | + * |
| 140 | + * static const struct arm_mpu_region mpu_regions[] = { |
| 141 | + * DT_MEMORY_ATTR_APPLY(MPU_FN) |
| 142 | + * }; |
| 143 | + * @endcode |
| 144 | + * |
| 145 | + * This expands to: |
| 146 | + * |
| 147 | + * @code{.c} |
| 148 | + * static const struct arm_mpu_region mpu_regions[] = { |
| 149 | + * { "MY_NAME", 0x20000000, 0x4000, 0xAAAA }, |
| 150 | + * { "memory@30000000", 0x30000000, 0x2000, 0xBBBB }, |
| 151 | + * }; |
| 152 | + * @endcode |
| 153 | + * |
| 154 | + * @param fn macro to invoke |
| 155 | + */ |
| 156 | +#define DT_MEMORY_ATTR_APPLY(fn) \ |
| 157 | + DT_FOREACH_STATUS_OKAY_NODE_VARGS(_APPLY, fn) |
| 158 | + |
| 159 | +/** |
| 160 | + * @} |
| 161 | + */ |
| 162 | + |
| 163 | +#endif /* ZEPHYR_INCLUDE_MEMORY_ATTR_H_ */ |
0 commit comments