0

I'm trying to find a way to accesses, from node_1, a variable in node_0 (see code below) in this device-tree:

/ {
    amba_pl: amba_pl@0 {
      node_0: node@a8000000 {
         node_id = <0x0>;
         node0State = <0x0>;
      };
   };

      node_1: node@a9000000 {
         node1State = <node_0's node0State>;
      };
   };
};

The primary goal is to be able to share a state between kernel modules. I'm aware that I can EXPORT_SYMBOL(variable) in the writing node and then extern *variable in the reading node, but wanted to see if I could accomplish this in the device-tree itself. node_0 would always be the only node to set the nodeState, and node_1 would only read. Is this possible?

plasmaphase
  • 7
  • 1
  • 1
  • Suddenly I know that it’s easy in ACPI, but in DT probably you can use C preprocessing. I am not sure if it is possible at run time (again ACPI can do it all). – 0andriy Mar 01 '21 at 22:30

1 Answers1

0

You can store a phandle referring to the node containing node0state:

/ {
    amba_pl: amba_pl@0 {
      node_0: node@a8000000 {
         node_id = <0x0>;
         node0State = <0x0>;
      };
   };

      node_1: node@a9000000 {
         stateNode = <&node_0>;
      };
   };
};

In the driver code, if struct device_node *mynode; refers to the node_1 node, the node0state property of the other node referred to by the stateNode phandle property can be accessed as follows:

    int rc = 0;
    struct device_node *np;
    u32 node0state;

    np = of_parse_phandle(mynode, "stateNode", 0);
    if (!np) {
        // error node not found
        rc = -ENXIO;
        goto error1;
    }
    
    // Do whatever is needed to read "node0state".  Here is an example.
    rc = of_property_read_u32(np, "node0state", &node0state);
    if (rc) {
        // error
        of_node_put(np);  // decrement reference to the node
        goto error2;
    }

    // Finished with the node.
    of_node_put(np);  // decrement reference to the node
0andriy
  • 4,183
  • 1
  • 24
  • 37
Ian Abbott
  • 15,083
  • 19
  • 33
  • This is exactly what I was looking for. So is stateNode modifiable by node_1? Also, is node0State modifiable by node_0? I've been using of_property_read_u32() to get the field value, but it looks like there isn't a of_property_write_u32() in of.h – plasmaphase Mar 02 '21 at 18:28
  • 1
    @plasmaphase, You can’t write to OF — it’s only for getting data. But you may modify it with overlays. – 0andriy Mar 02 '21 at 23:12