Skip to contentSkip to conversation

Device tree overlay properties order issue

PATCH Lucas Tanure 2 participants 3 messages Mar 5, 2018 – Mar 10, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi,

I'm working with Raspberry Pi 3 and overlays.
I'm seeing one issue with overlays, that I don't see with normal device trees.

Using the attached patch with Rpi 4.9 kernel and loading dts-test-overlay.dts, the order that my machine driver reads properties "cpu-codec" is the opposite of what I is specified on dts.

[    4.558259] dts-test-order soc:sound: dts_test_probe [    4.558275] dts-test-order soc:sound: Dai Name cpu-codec1 [    4.558284] dts-test-order soc:sound: Dai Name cpu-codec0

Just for testing, commenting out drivers/of/fdt.c , line 451, did make the order be the same as dts file.
Looking at the history of the code in that area it appears that it was actually trying to preserve the order of sub-nodes, but for some reason it no longer works for overlays on raspberry pi.

    /*
     * Reverse the child list. Some drivers assumes node order matches .dts
     * node order
     */
    if (!dryrun)
        reverse_nodes(root);

This is a issue with overlays or I'm missing something ?

Thanks
Lucas

---
 arch/arm/boot/dts/overlays/Makefile             |  1 + arch/arm/boot/dts/overlays/dts-test-overlay.dts | 19 ++++++++++++ arch/arm/configs/bcm2709_defconfig              |  1 + sound/soc/cirrus/Kconfig                        |  3 ++ sound/soc/cirrus/Makefile                       |  3 ++ sound/soc/cirrus/dts-test.c                     | 40 +++++++++++++++++++++++++

 6 files changed, 67 insertions(+)

 create mode 100644 arch/arm/boot/dts/overlays/dts-test-overlay.dts create mode 100644 sound/soc/cirrus/dts-test.c

arch/arm/boot/dts/overlays/Makefile +0 -0
index e99392ca95f9..a6b78c3a513c 100644
@@ -17,6 +17,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \

     audioinjector-wm8731-audio.dtbo \
     audremap.dtbo \
     bmp085_i2c-sensor.dtbo \
+    dts-test.dtbo \
     dht11.dtbo \
     dionaudio-loco.dtbo \
     dionaudio-loco-v2.dtbo \

arch/arm/boot/dts/overlays/dts-test-overlay.dts new file +19 -0
new file mode 100644
index 000000000000..e6c91c1fc5a4
@@ -0,0 +1,19 @@
+/dts-v1/;
+/plugin/;
+
+/{
+    compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
+
+    fragment@0 {
+        target = <&sound>;
+        __overlay__ {
+            status = "okay";
+            compatible = "dts,test-order";
+
+            cpu-codec0 {
+            };
+            cpu-codec1 {
+            };
+        };
+    };
+};
arch/arm/configs/bcm2709_defconfig +0 -0
index 281f1381b9b3..d7ff1e89742b 100644
@@ -1348,3 +1348,4 @@ CONFIG_CRYPTO_SHA1_ARM_NEON=m

 CONFIG_CRYPTO_AES_ARM_BS=m
 CONFIG_CRC_ITU_T=y
 CONFIG_LIBCRC32C=y
+CONFIG_SND_SOC_DTS_TEST=m

sound/soc/cirrus/Kconfig +0 -0
index c7cd60f009e9..d981c054e171 100644
@@ -41,3 +41,6 @@ config SND_EP93XX_SOC_EDB93XX

     help
       Say Y or M here if you want to add support for I2S audio on the Cirrus Logic EDB93xx boards.
+
+config SND_SOC_DTS_TEST
+    tristate "Device Tree Order Test"

sound/soc/cirrus/Makefile +0 -0
index 5514146cbdf0..436f1974cd2e 100644
@@ -15,3 +15,6 @@ snd-soc-edb93xx-objs                := edb93xx.o

 obj-$(CONFIG_SND_EP93XX_SOC_SNAPPERCL15)    += snd-soc-snappercl15.o obj-$(CONFIG_SND_EP93XX_SOC_SIMONE)        += snd-soc-simone.o obj-$(CONFIG_SND_EP93XX_SOC_EDB93XX)        += snd-soc-edb93xx.o +
+snd-soc-dts-test-objs                := dts-test.o +obj-$(CONFIG_SND_SOC_DTS_TEST)            += snd-soc-dts-test.o

sound/soc/cirrus/dts-test.c new file +40 -0
new file mode 100644
index 000000000000..fe7a5099a005
@@ -0,0 +1,40 @@
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <linux/module.h>
+#include <linux/of.h>
+
+static int dts_test_probe(struct platform_device *pdev)
+{
+    struct device_node *dai;
+    struct device_node *np = pdev->dev.of_node;
+
+    dev_info(&pdev->dev,"dts_test_probe\n");
+    for_each_child_of_node(np, dai) {
+        dev_info(&pdev->dev,"Dai Name %s\n", dai->name);
+    }
+    return 0;
+}
+
+#if IS_ENABLED(CONFIG_OF)
+static const struct of_device_id dts_test_of_match[] = {
+    { .compatible = "dts,test-order", },
+    {},
+};
+MODULE_DEVICE_TABLE(of, dts_test_of_match);
+#endif /* CONFIG_OF */
+
+static struct platform_driver dts_test_driver = {
+    .driver        = {
+        .name    = "dts-test-order",
+        .owner    = THIS_MODULE,
+        .of_match_table = of_match_ptr(dts_test_of_match),
+    },
+
+    .probe        = dts_test_probe,
+};
+
+module_platform_driver(dts_test_driver);
+
+MODULE_DESCRIPTION("Device Order Test");
+MODULE_AUTHOR("Lucas Tanure <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 11 lines · Lucas Tanure

Hi,

I'm working with Raspberry Pi 3 and overlays.
I'm seeing one issue with overlays, that I don't see with normal device trees.

Using the attached patch with Rpi 4.9 kernel and loading dts-test-overlay.dts,
the order that my machine driver reads properties "cpu-codec" is the opposite
of what I is specified on dts.

What about mainline?

Quoted 8 lines
[    4.558259] dts-test-order soc:sound: dts_test_probe
[    4.558275] dts-test-order soc:sound: Dai Name cpu-codec1
[    4.558284] dts-test-order soc:sound: Dai Name cpu-codec0

Just for testing, commenting out drivers/of/fdt.c , line 451, did make the order be
the same as dts file.

Depending on the order within the DT should mostly be solved with deferred probe, but that does require awareness in drivers and subsystems.

Quoted 5 lines

Looking at the history of the code in that area it appears that it was actually trying
to preserve the order of sub-nodes, but for some reason it no longer works for
overlays on raspberry pi.

Yes, only to not change current behavior on existing DTs. However, one should not rely on a certain order.

Quoted 52 lines
    /*
     * Reverse the child list. Some drivers assumes node order matches .dts
     * node order
     */
    if (!dryrun)
        reverse_nodes(root);

This is a issue with overlays or I'm missing something ?

Thanks
Lucas

---
 arch/arm/boot/dts/overlays/Makefile             |  1 +
 arch/arm/boot/dts/overlays/dts-test-overlay.dts | 19 ++++++++++++
 arch/arm/configs/bcm2709_defconfig              |  1 +
 sound/soc/cirrus/Kconfig                        |  3 ++
 sound/soc/cirrus/Makefile                       |  3 ++
 sound/soc/cirrus/dts-test.c                     | 40
+++++++++++++++++++++++++
 6 files changed, 67 insertions(+)
 create mode 100644 arch/arm/boot/dts/overlays/dts-test-overlay.dts
 create mode 100644 sound/soc/cirrus/dts-test.c
arch/arm/boot/dts/overlays/Makefile +1 -0
index e99392ca95f9..a6b78c3a513c 100644
@@ -17,6 +17,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
     audioinjector-wm8731-audio.dtbo \
     audremap.dtbo \
     bmp085_i2c-sensor.dtbo \
+    dts-test.dtbo \
     dht11.dtbo \
     dionaudio-loco.dtbo \
     dionaudio-loco-v2.dtbo \
arch/arm/boot/dts/overlays/dts-test-overlay.dts new file +6 -0
new file mode 100644
index 000000000000..e6c91c1fc5a4
@@ -0,0 +1,19 @@
+/dts-v1/;
+/plugin/;
+
+/{
+    compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
+

arch/arm/boot/dts/overlays/dts-test-overlay.dts line 16

Quoted 10 lines
arch/arm/boot/dts/overlays/dts-test-overlay.dts new file +10 -0
+    fragment@0 {
+        target = <&sound>;
+        __overlay__ {
+            status = "okay";
+            compatible = "dts,test-order";
+
+            cpu-codec0 {
+            };
+            cpu-codec1 {
+            };

Does one of these nodes already exist in the base DT? That's the only think I can think of. That's allowed and I don't think we sort nodes once we apply them.

Rob

#2 lore raw

Hi,

Thanks for the help. I got my answer on the raspberry pi github issue :
https://github.com/raspberrypi/linux/issues/2416

It's a feature of libfdt, the Open Source library for manipulating Flattened Device Trees. The firmware, the Pi-specific dtmerge utility and the (new) standard fdtoverlay utility all use a function called fdt_add_subnode(*) to add each node. The documentation says:

* fdt_add_subnode() creates a new node as a subnode of the node at
* structure block offset parentoffset, with the given name (which
* should include the unit address, if any).

What it doesn't say is where the subnode should be added. A comment in the code makes it clearer:

/* Try to place the new node after the parent's properties */

Since FDT stores the subnodes after the properties, this has the effect of inserting the new subnode before any others. And since the source nodes are enumerated in the order they appear, the end result is a reversal.

As fdtoverlay behaves the same way, I see this as an unexpected but harmless quirk.

(*) Actually the fdt_add_subnode_namelen variant, but they are the same for the purposes of this explanation.

Thanks

Quoted 105 lines · Rob Herring
Quoted 11 lines · Lucas Tanure

Hi,

I'm working with Raspberry Pi 3 and overlays.
I'm seeing one issue with overlays, that I don't see with normal device trees.

Using the attached patch with Rpi 4.9 kernel and loading dts-test-overlay.dts,
the order that my machine driver reads properties "cpu-codec" is the opposite
of what I is specified on dts.

What about mainline?

Quoted 8 lines
[    4.558259] dts-test-order soc:sound: dts_test_probe
[    4.558275] dts-test-order soc:sound: Dai Name cpu-codec1
[    4.558284] dts-test-order soc:sound: Dai Name cpu-codec0

Just for testing, commenting out drivers/of/fdt.c , line 451, did make the order be
the same as dts file.

Depending on the order within the DT should mostly be solved with deferred probe, but that does require awareness in drivers and subsystems.

Quoted 5 lines

Looking at the history of the code in that area it appears that it was actually trying
to preserve the order of sub-nodes, but for some reason it no longer works for
overlays on raspberry pi.

Yes, only to not change current behavior on existing DTs. However, one should not rely on a certain order.

Quoted 52 lines
    /*
     * Reverse the child list. Some drivers assumes node order matches .dts
     * node order
     */
    if (!dryrun)
        reverse_nodes(root);

This is a issue with overlays or I'm missing something ?

Thanks
Lucas

---
 arch/arm/boot/dts/overlays/Makefile             |  1 +
 arch/arm/boot/dts/overlays/dts-test-overlay.dts | 19 ++++++++++++
 arch/arm/configs/bcm2709_defconfig              |  1 +
 sound/soc/cirrus/Kconfig                        |  3 ++
 sound/soc/cirrus/Makefile                       |  3 ++
 sound/soc/cirrus/dts-test.c                     | 40
+++++++++++++++++++++++++
 6 files changed, 67 insertions(+)
 create mode 100644 arch/arm/boot/dts/overlays/dts-test-overlay.dts
 create mode 100644 sound/soc/cirrus/dts-test.c
arch/arm/boot/dts/overlays/Makefile +1 -0
index e99392ca95f9..a6b78c3a513c 100644
@@ -17,6 +17,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
     audioinjector-wm8731-audio.dtbo \
     audremap.dtbo \
     bmp085_i2c-sensor.dtbo \
+    dts-test.dtbo \
     dht11.dtbo \
     dionaudio-loco.dtbo \
     dionaudio-loco-v2.dtbo \
arch/arm/boot/dts/overlays/dts-test-overlay.dts new file +6 -0
new file mode 100644
index 000000000000..e6c91c1fc5a4
@@ -0,0 +1,19 @@
+/dts-v1/;
+/plugin/;
+
+/{
+    compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
+

arch/arm/boot/dts/overlays/dts-test-overlay.dts line 16

Quoted 10 lines
arch/arm/boot/dts/overlays/dts-test-overlay.dts new file +10 -0
+    fragment@0 {
+        target = <&sound>;
+        __overlay__ {
+            status = "okay";
+            compatible = "dts,test-order";
+
+            cpu-codec0 {
+            };
+            cpu-codec1 {
+            };

Does one of these nodes already exist in the base DT? That's the only think I can think of. That's allowed and I don't think we sort nodes once we apply them.

Rob