-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Move to and extend zephyr,memory-attr
#60049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Copyright (c) 2023, Carlo Caione <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| include: [base.yaml] | ||
|
|
||
| properties: | ||
| zephyr,memory-attr: | ||
| type: string | ||
| enum: | ||
| - "RAM" | ||
| - "RAM_NOCACHE" | ||
| - "FLASH" | ||
| - "PPB" | ||
| - "IO" | ||
| - "EXTMEM" | ||
| description: | | ||
| Attribute for the memory region. | ||
|
|
||
| reg: | ||
| required: true | ||
carlocaione marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (c) 2020 Linaro Ltd. | ||
erwango marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Test memory and memory attributes | ||
|
|
||
| compatible: "vnd,memory-attr" | ||
|
|
||
| include: [base.yaml, "zephyr,memory-attr.yaml"] | ||
carlocaione marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * Copyright (c) 2023 Carlo Caione <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_MEMORY_ATTR_H_ | ||
| #define ZEPHYR_INCLUDE_MEMORY_ATTR_H_ | ||
|
|
||
| #include <zephyr/devicetree.h> | ||
| #include <zephyr/linker/devicetree_regions.h> | ||
carlocaione marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include <zephyr/sys/util.h> | ||
|
|
||
| /** | ||
| * @file | ||
| * @brief Memory-attr helpers | ||
| */ | ||
|
|
||
| /** | ||
| * @defgroup devicetree-memory-attr Memory attributes | ||
| * @ingroup devicetree | ||
| * @{ | ||
| */ | ||
|
|
||
| /** @cond INTERNAL_HIDDEN */ | ||
|
|
||
| #define _DT_MEM_ATTR zephyr_memory_attr | ||
| #define _DT_ATTR(token) UTIL_CAT(UTIL_CAT(REGION_, token), _ATTR) | ||
|
|
||
| #define _UNPACK(node_id, fn) \ | ||
| fn(COND_CODE_1(DT_NODE_HAS_PROP(node_id, zephyr_memory_region), \ | ||
| (LINKER_DT_NODE_REGION_NAME(node_id)), \ | ||
| (DT_NODE_FULL_NAME(node_id))), \ | ||
| DT_REG_ADDR(node_id), \ | ||
| DT_REG_SIZE(node_id), \ | ||
| _DT_ATTR(DT_STRING_TOKEN(node_id, _DT_MEM_ATTR))), | ||
|
|
||
| #define _APPLY(node_id, fn) \ | ||
| COND_CODE_1(DT_NODE_HAS_PROP(node_id, _DT_MEM_ATTR), \ | ||
| (_UNPACK(node_id, fn)), \ | ||
| ()) | ||
|
|
||
|
|
||
| #define _FILTER(node_id, fn) \ | ||
| COND_CODE_1(DT_NODE_HAS_PROP(node_id, _DT_MEM_ATTR), \ | ||
| (fn(node_id)), \ | ||
| ()) | ||
|
|
||
| /** @endcond */ | ||
|
|
||
| /** | ||
| * @brief Invokes @p fn for every node in the tree with property | ||
| * `zephyr,memory-attr` | ||
| * | ||
| * The macro @p fn must take one parameter, which will be a node identifier | ||
| * with the `zephyr,memory-attr` property. The macro is expanded once for each | ||
| * node in the tree. The order that nodes are visited in is not specified. | ||
| * | ||
| * @param fn macro to invoke | ||
| */ | ||
| #define DT_MEMORY_ATTR_FOREACH_NODE(fn) \ | ||
| DT_FOREACH_STATUS_OKAY_NODE_VARGS(_FILTER, fn) | ||
|
|
||
| /** | ||
| * @brief Invokes @p fn for MPU/MMU regions generation from the device tree | ||
| * nodes with `zephyr,memory-attr` property. | ||
| * | ||
| * Helper macro to invoke a @p fn macro on all the memory regions declared | ||
| * using the `zephyr,memory-attr` property | ||
| * | ||
| * The macro @p fn must take the form: | ||
| * | ||
| * @code{.c} | ||
| * #define MPU_FN(name, base, size, attr) ... | ||
| * @endcode | ||
| * | ||
| * The @p name, @p base and @p size parameters are retrieved from the DT node. | ||
| * When the `zephyr,memory-region` property is present in the node, the @p name | ||
| * parameter is retrived from there, otherwise the full node name is used. | ||
| * | ||
| * The `zephyr,memory-attr` enum property is passed as an extended token | ||
| * to the @p fn macro using the @p attr parameter in the form of a macro | ||
| * REGION_{attr}_ATTR. | ||
| * | ||
| * The following enums are supported for the `zephyr,memory-attr` property (see | ||
| * `zephyr,memory-attr.yaml` for a complete list): | ||
| * | ||
| * - RAM | ||
| * - RAM_NOCACHE | ||
| * - FLASH | ||
| * - PPB | ||
| * - IO | ||
| * - EXTMEM | ||
| * | ||
| * This means that usually the user code would provide some macros or defines | ||
| * with the same name of the extended property, that is: | ||
| * | ||
| * - REGION_RAM_ATTR | ||
| * - REGION_RAM_NOCACHE_ATTR | ||
| * - REGION_FLASH_ATTR | ||
| * - REGION_PPB_ATTR | ||
| * - REGION_IO_ATTR | ||
| * - REGION_EXTMEM_ATTR | ||
| * | ||
| * Example devicetree fragment: | ||
| * | ||
| * @code{.dts} | ||
| * / { | ||
| * soc { | ||
| * res0: memory@20000000 { | ||
| * reg = <0x20000000 0x4000>; | ||
| * zephyr,memory-region = "MY_NAME"; | ||
| * zephyr,memory-attr = "RAM_NOCACHE"; | ||
| * }; | ||
| * | ||
| * res1: memory@30000000 { | ||
| * reg = <0x30000000 0x2000>; | ||
| * zephyr,memory-attr = "RAM"; | ||
| * }; | ||
|
|
||
| * }; | ||
| * }; | ||
| * @endcode | ||
| * | ||
| * Example usage: | ||
| * | ||
| * @code{.c} | ||
| * #define REGION_RAM_NOCACHE_ATTR 0xAAAA | ||
| * #define REGION_RAM_ATTR 0xBBBB | ||
| * #define REGION_FLASH_ATTR 0xCCCC | ||
| * | ||
| * #define MPU_FN(p_name, p_base, p_size, p_attr) \ | ||
| * { \ | ||
| * .name = p_name, \ | ||
| * .base = p_base, \ | ||
| * .size = p_size, \ | ||
| * .attr = p_attr, \ | ||
| * } | ||
| * | ||
| * static const struct arm_mpu_region mpu_regions[] = { | ||
| * DT_MEMORY_ATTR_APPLY(MPU_FN) | ||
| * }; | ||
| * @endcode | ||
| * | ||
| * This expands to: | ||
| * | ||
| * @code{.c} | ||
| * static const struct arm_mpu_region mpu_regions[] = { | ||
| * { "MY_NAME", 0x20000000, 0x4000, 0xAAAA }, | ||
| * { "memory@30000000", 0x30000000, 0x2000, 0xBBBB }, | ||
| * }; | ||
| * @endcode | ||
| * | ||
| * @param fn macro to invoke | ||
| */ | ||
| #define DT_MEMORY_ATTR_APPLY(fn) \ | ||
| DT_FOREACH_STATUS_OKAY_NODE_VARGS(_APPLY, fn) | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_MEMORY_ATTR_H_ */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2667,6 +2667,70 @@ ZTEST(devicetree_api, test_mbox) | |
| DT_NODELABEL(test_mbox_zero_cell)), ""); | ||
| } | ||
|
|
||
| ZTEST(devicetree_api, test_memory_attr) | ||
| { | ||
| #define REGION_RAM_ATTR (0xDEDE) | ||
| #define REGION_RAM_NOCACHE_ATTR (0xCACA) | ||
|
|
||
| #define TEST_FUNC(p_name, p_base, p_size, p_attr) \ | ||
| { .name = (p_name), \ | ||
| .base = (p_base), \ | ||
| .size = (p_size), \ | ||
| .attr = (p_attr), \ | ||
| } | ||
carlocaione marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| struct vnd_memory_binding { | ||
| char *name; | ||
| uintptr_t base; | ||
| size_t size; | ||
| unsigned int attr; | ||
| }; | ||
|
|
||
| struct vnd_memory_binding val_apply[] = { | ||
| DT_MEMORY_ATTR_APPLY(TEST_FUNC) | ||
| }; | ||
|
|
||
| zassert_true(!strcmp(val_apply[0].name, "memory@aabbccdd"), ""); | ||
| zassert_equal(val_apply[0].base, 0xaabbccdd, ""); | ||
| zassert_equal(val_apply[0].size, 0x4000, ""); | ||
| zassert_equal(val_apply[0].attr, 0xDEDE, ""); | ||
|
|
||
| zassert_true(!strcmp(val_apply[1].name, "memory@44332211"), ""); | ||
| zassert_equal(val_apply[1].base, 0x44332211, ""); | ||
| zassert_equal(val_apply[1].size, 0x2000, ""); | ||
| zassert_equal(val_apply[1].attr, 0xCACA, ""); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this 0xCACA be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, maybe for readability but after all this is a test, so it doesn't really matter? |
||
|
|
||
| #undef TEST_FUNC | ||
| #undef REGION_RAM_ATTR | ||
| #undef REGION_RAM_NOCACHE_ATTR | ||
|
|
||
| #define TEST_FUNC(node_id) DT_NODE_FULL_NAME(node_id), | ||
|
|
||
| static const char * const val_func[] = { | ||
| DT_MEMORY_ATTR_FOREACH_NODE(TEST_FUNC) | ||
| }; | ||
|
|
||
| zassert_true(!strcmp(val_func[0], "memory@aabbccdd"), ""); | ||
| zassert_true(!strcmp(val_func[1], "memory@44332211"), ""); | ||
|
|
||
| #undef TEST_FUNC | ||
|
|
||
| #define TEST_FUNC(node_id) \ | ||
| COND_CODE_1(DT_ENUM_HAS_VALUE(node_id, \ | ||
| zephyr_memory_attr, \ | ||
| RAM_NOCACHE), \ | ||
| (DT_REG_ADDR(node_id)), \ | ||
| ()) | ||
|
|
||
| uintptr_t val_filt[] = { | ||
| DT_MEMORY_ATTR_FOREACH_NODE(TEST_FUNC) | ||
| }; | ||
|
|
||
| zassert_equal(val_filt[0], 0x44332211, ""); | ||
|
|
||
| #undef TEST_FUNC | ||
| } | ||
|
|
||
| ZTEST(devicetree_api, test_string_token) | ||
| { | ||
| #undef DT_DRV_COMPAT | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.