Skip to content
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

DT_NODE_HAS_PROP not working with certain compatible #85173

Open
iuliana-prodan opened this issue Feb 4, 2025 · 6 comments
Open

DT_NODE_HAS_PROP not working with certain compatible #85173

iuliana-prodan opened this issue Feb 4, 2025 · 6 comments
Assignees
Labels
area: Devicetree bug The issue is a bug, or the PR is fixing a bug

Comments

@iuliana-prodan
Copy link
Collaborator

Describe the bug

DT_NODE_HAS_PROP fails when looking for ranges property and using compatible = "mmio-sram";

To Reproduce

A simple way to reproduce it is using the patch below:

diff --git a/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay b/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay
new file mode 100644
index 00000000000..d04c9b7431a
--- /dev/null
+++ b/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay
@@ -0,0 +1,15 @@
+/ {
+       test1: test@1bef0000 {
+               compatible = "dummy";
+               reg = <0x1bef0000 0x110000>;
+               #address-cells = <1>;
+               ranges = <0x1bef0000 0x8fef0000 0x110000>;
+       };
+
+       test2: test@8bef0000 {
+               compatible = "mmio-sram";
+               reg = <0x8bef0000 0x110000>;
+               #address-cells = <1>;
+               ranges = <0x1bef0000 0x8fef0000 0x110000>;
+       };
+};
diff --git a/samples/hello_world/src/main.c b/samples/hello_world/src/main.c
index c550ab461cb..df967054a53 100644
--- a/samples/hello_world/src/main.c
+++ b/samples/hello_world/src/main.c
@@ -5,6 +5,15 @@
  */
 
 #include <stdio.h>
+#include <zephyr/devicetree.h>
+
+#if DT_NODE_HAS_PROP(DT_NODELABEL(test1), ranges)
+#error "<ranges> property found in <test1> node"
+#endif
+
+#if DT_NODE_HAS_PROP(DT_NODELABEL(test2), ranges)
+#error "<ranges> property found in <test2> node"
+#endif
 
 int main(void)
 {

When compiling hello_world sample for 8ulp, using command: west build -p -b imx8ulp_evk//adsp -s samples/hello_world/, I'm getting only:

/zephyrproject/zephyr/samples/hello_world/src/main.c:11:2: error: #error "<ranges> property found in <test1> node"
   11 | #error "<ranges> property found in <test1> node"
      |  ^~~~~
[55/150] Building C object zephyr/arch/arch/xtensa/core/CMakeFiles/arch__xtensa__core.dir/vector_handlers.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build //zephyrproject/zephyr/build

IF I comment the compatible = "mmio-sram"; from test2 node I'm getting both errors (the ranges is found in both nodes):

/zephyrproject/zephyr/samples/hello_world/src/main.c:11:2: error: #error "<ranges> property found in <test1> node"
   11 | #error "<ranges> property found in <test1> node"
      |  ^~~~~
/zephyrproject/zephyr/samples/hello_world/src/main.c:15:2: error: #error "<ranges> property found in <test2> node"
   15 | #error "<ranges> property found in <test2> node"
      |  ^~~~~
[27/150] Building C object zephyr/CMakeFiles/zephyr.dir/lib/utils/rb.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /zephyrproject/zephyr/build

Expected behavior
If the property is in the node DT_NODE_HAS_PROP should return true or document this if it's the expected behavior.

Environment (please complete the following information):

  • OS: Linux
  • Toolchain:zephyr-sdk-0.17.0
  • Zephyr main 8cff70a

Additional context
This was found while working on: #84168

@iuliana-prodan iuliana-prodan added the bug The issue is a bug, or the PR is fixing a bug label Feb 4, 2025
@JarmouniA
Copy link
Collaborator

JarmouniA commented Feb 5, 2025

@iuliana-prodan maybe that's because "mmio-sram" compatible doesn't have a "ranges" property!?
https://github.com/zephyrproject-rtos/zephyr/blob/a36c91b4cc49b9b54030121004a1565d6fc69475/dts/bindings/sram/mmio-sram.yaml

@iuliana-prodan
Copy link
Collaborator Author

iuliana-prodan commented Feb 5, 2025

@iuliana-prodan maybe that's because "mmio-sram" compatible doesn't have a "ranges" property!? https://github.com/zephyrproject-rtos/zephyr/blob/a36c91b4cc49b9b54030121004a1565d6fc69475/dts/bindings/sram/mmio-sram.yaml

I've looked for ranges property in Zephyr's device trees and I found it in some Nordic dtsi (+ others), so I've used their compatible).
Here's my overlay update:

diff --git a/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay b/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay
index d04c9b7431a..f0ceda938a7 100644
--- a/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay
+++ b/samples/hello_world/imx8ulp_evk_mimx8ud7_adsp.overlay
@@ -7,7 +7,7 @@
        };
 
        test2: test@8bef0000 {
-                compatible = "mmio-sram";
+               compatible = "nordic,owned-memory.yaml";
                reg = <0x8bef0000 0x110000>;
                #address-cells = <1>;
                ranges = <0x1bef0000 0x8fef0000 0x110000>;

and with this, when compiling the code from bug description it finds the ranges from test2 node.

/zephyrproject/zephyr/samples/hello_world/src/main.c:11:2: error: #error "<ranges> property found in <test1> node"
   11 | #error "<ranges> property found in <test1> node"
      |  ^~~~~
/zephyrproject/zephyr/samples/hello_world/src/main.c:15:2: error: #error "<ranges> property found in <test2> node"
   15 | #error "<ranges> property found in <test2> node"
      |  ^~~~~
[38/150] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/logging/backends/log_backend_uart.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /zephyrproject/zephyr/build

I've looked for nordic,owned-memory.yaml, see here the binding and it includes base.yaml and zephyr,memory-common.yaml and none has ranges property.
In case of mmio-sram binding it includes only base.yml.

Am I missing something?

@JarmouniA
Copy link
Collaborator

JarmouniA commented Feb 5, 2025

@iuliana-prodan turns out ranges is a special property that doesn't need to be declared in the binding, and has special macros, I think you can use this instead

#define DT_RANGES_HAS_IDX(node_id, idx) \

@mbolivar
Copy link
Contributor

mbolivar commented Feb 5, 2025

Ranges is indeed special and I am guessing this is about #84170?

There seems to be some confusion about ranges. Please refer to my latest comment #84170 (comment) for the context for what you need to understand about this property and how it relates to addressing via reg. Once we are all on the same page about that we can continue discussion. (If there are any clarifying questions about how the spec works, please ask after taking a look at the spec.)

THanks!

@iuliana-prodan
Copy link
Collaborator Author

Ranges is indeed special and I am guessing this is about #84170?

There seems to be some confusion about ranges. Please refer to my latest comment #84170 (comment) for the context for what you need to understand about this property and how it relates to addressing via reg. Once we are all on the same page about that we can continue discussion. (If there are any clarifying questions about how the spec works, please ask after taking a look at the spec.)

THanks!

@mbolivar I've read the specs and it doesn't say anywhere we shouldn't use ranges with compatible = "mmio-sram";
I've found an example in Linux kernel - https://www.kernel.org/doc/Documentation/devicetree/bindings/sram/sram.txt.
This doesn't apply to Zephyr?
Maybe this should be documented.

@57300
Copy link
Contributor

57300 commented Feb 9, 2025

I thought I had a solution for this in #58858. It turns out to be incomplete, but it's the reason why DT_NODE_HAS_PROP(x, ranges) works when node x has an unknown compatible or no compatible. In all other cases, the node's binding must define the ranges property explicitly for the macro to work. This problem isn't specific to mmio-sram.

The simplest fix would be to add ranges to base.yaml with type: compound. Note that ranges is part of a set of properties which edtlib allows to be omitted from a binding, but I believe that this behavior should be deprecated in favor of explicit definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

No branches or pull requests

5 participants