xref: /OK3568_Linux_fs/kernel/Documentation/devicetree/of_unittest.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun==================================
4*4882a593SmuzhiyunOpen Firmware Device Tree Unittest
5*4882a593Smuzhiyun==================================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunAuthor: Gaurav Minocha <gaurav.minocha.os@gmail.com>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun1. Introduction
10*4882a593Smuzhiyun===============
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunThis document explains how the test data required for executing OF unittest
13*4882a593Smuzhiyunis attached to the live tree dynamically, independent of the machine's
14*4882a593Smuzhiyunarchitecture.
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunIt is recommended to read the following documents before moving ahead.
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun(1) Documentation/devicetree/usage-model.rst
19*4882a593Smuzhiyun(2) http://www.devicetree.org/Device_Tree_Usage
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunOF Selftest has been designed to test the interface (include/linux/of.h)
22*4882a593Smuzhiyunprovided to device driver developers to fetch the device information..etc.
23*4882a593Smuzhiyunfrom the unflattened device tree data structure. This interface is used by
24*4882a593Smuzhiyunmost of the device drivers in various use cases.
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun2. Test-data
28*4882a593Smuzhiyun============
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunThe Device Tree Source file (drivers/of/unittest-data/testcases.dts) contains
31*4882a593Smuzhiyunthe test data required for executing the unit tests automated in
32*4882a593Smuzhiyundrivers/of/unittest.c. Currently, following Device Tree Source Include files
33*4882a593Smuzhiyun(.dtsi) are included in testcases.dts::
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun    drivers/of/unittest-data/tests-interrupts.dtsi
36*4882a593Smuzhiyun    drivers/of/unittest-data/tests-platform.dtsi
37*4882a593Smuzhiyun    drivers/of/unittest-data/tests-phandle.dtsi
38*4882a593Smuzhiyun    drivers/of/unittest-data/tests-match.dtsi
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunWhen the kernel is build with OF_SELFTEST enabled, then the following make
41*4882a593Smuzhiyunrule::
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun    $(obj)/%.dtb: $(src)/%.dts FORCE
44*4882a593Smuzhiyun	    $(call if_changed_dep, dtc)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyunis used to compile the DT source file (testcases.dts) into a binary blob
47*4882a593Smuzhiyun(testcases.dtb), also referred as flattened DT.
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunAfter that, using the following rule the binary blob above is wrapped as an
50*4882a593Smuzhiyunassembly file (testcases.dtb.S)::
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun    $(obj)/%.dtb.S: $(obj)/%.dtb
53*4882a593Smuzhiyun	    $(call cmd, dt_S_dtb)
54*4882a593Smuzhiyun
55*4882a593SmuzhiyunThe assembly file is compiled into an object file (testcases.dtb.o), and is
56*4882a593Smuzhiyunlinked into the kernel image.
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun2.1. Adding the test data
60*4882a593Smuzhiyun-------------------------
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunUn-flattened device tree structure:
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunUn-flattened device tree consists of connected device_node(s) in form of a tree
65*4882a593Smuzhiyunstructure described below::
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun    // following struct members are used to construct the tree
68*4882a593Smuzhiyun    struct device_node {
69*4882a593Smuzhiyun	...
70*4882a593Smuzhiyun	struct  device_node *parent;
71*4882a593Smuzhiyun	struct  device_node *child;
72*4882a593Smuzhiyun	struct  device_node *sibling;
73*4882a593Smuzhiyun	...
74*4882a593Smuzhiyun    };
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunFigure 1, describes a generic structure of machine's un-flattened device tree
77*4882a593Smuzhiyunconsidering only child and sibling pointers. There exists another pointer,
78*4882a593Smuzhiyun``*parent``, that is used to traverse the tree in the reverse direction. So, at
79*4882a593Smuzhiyuna particular level the child node and all the sibling nodes will have a parent
80*4882a593Smuzhiyunpointer pointing to a common node (e.g. child1, sibling2, sibling3, sibling4's
81*4882a593Smuzhiyunparent points to root node)::
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun    root ('/')
84*4882a593Smuzhiyun    |
85*4882a593Smuzhiyun    child1 -> sibling2 -> sibling3 -> sibling4 -> null
86*4882a593Smuzhiyun    |         |           |           |
87*4882a593Smuzhiyun    |         |           |          null
88*4882a593Smuzhiyun    |         |           |
89*4882a593Smuzhiyun    |         |        child31 -> sibling32 -> null
90*4882a593Smuzhiyun    |         |           |          |
91*4882a593Smuzhiyun    |         |          null       null
92*4882a593Smuzhiyun    |         |
93*4882a593Smuzhiyun    |      child21 -> sibling22 -> sibling23 -> null
94*4882a593Smuzhiyun    |         |          |            |
95*4882a593Smuzhiyun    |        null       null         null
96*4882a593Smuzhiyun    |
97*4882a593Smuzhiyun    child11 -> sibling12 -> sibling13 -> sibling14 -> null
98*4882a593Smuzhiyun    |           |           |            |
99*4882a593Smuzhiyun    |           |           |           null
100*4882a593Smuzhiyun    |           |           |
101*4882a593Smuzhiyun    null        null       child131 -> null
102*4882a593Smuzhiyun			    |
103*4882a593Smuzhiyun			    null
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunFigure 1: Generic structure of un-flattened device tree
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun
108*4882a593SmuzhiyunBefore executing OF unittest, it is required to attach the test data to
109*4882a593Smuzhiyunmachine's device tree (if present). So, when selftest_data_add() is called,
110*4882a593Smuzhiyunat first it reads the flattened device tree data linked into the kernel image
111*4882a593Smuzhiyunvia the following kernel symbols::
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun    __dtb_testcases_begin - address marking the start of test data blob
114*4882a593Smuzhiyun    __dtb_testcases_end   - address marking the end of test data blob
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunSecondly, it calls of_fdt_unflatten_tree() to unflatten the flattened
117*4882a593Smuzhiyunblob. And finally, if the machine's device tree (i.e live tree) is present,
118*4882a593Smuzhiyunthen it attaches the unflattened test data tree to the live tree, else it
119*4882a593Smuzhiyunattaches itself as a live device tree.
120*4882a593Smuzhiyun
121*4882a593Smuzhiyunattach_node_and_children() uses of_attach_node() to attach the nodes into the
122*4882a593Smuzhiyunlive tree as explained below. To explain the same, the test data tree described
123*4882a593Smuzhiyunin Figure 2 is attached to the live tree described in Figure 1::
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun    root ('/')
126*4882a593Smuzhiyun	|
127*4882a593Smuzhiyun    testcase-data
128*4882a593Smuzhiyun	|
129*4882a593Smuzhiyun    test-child0 -> test-sibling1 -> test-sibling2 -> test-sibling3 -> null
130*4882a593Smuzhiyun	|               |                |                |
131*4882a593Smuzhiyun    test-child01      null             null             null
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunFigure 2: Example test data tree to be attached to live tree.
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunAccording to the scenario above, the live tree is already present so it isn't
137*4882a593Smuzhiyunrequired to attach the root('/') node. All other nodes are attached by calling
138*4882a593Smuzhiyunof_attach_node() on each node.
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunIn the function of_attach_node(), the new node is attached as the child of the
141*4882a593Smuzhiyungiven parent in live tree. But, if parent already has a child then the new node
142*4882a593Smuzhiyunreplaces the current child and turns it into its sibling. So, when the testcase
143*4882a593Smuzhiyundata node is attached to the live tree above (Figure 1), the final structure is
144*4882a593Smuzhiyunas shown in Figure 3::
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun    root ('/')
147*4882a593Smuzhiyun    |
148*4882a593Smuzhiyun    testcase-data -> child1 -> sibling2 -> sibling3 -> sibling4 -> null
149*4882a593Smuzhiyun    |               |          |           |           |
150*4882a593Smuzhiyun    (...)             |          |           |          null
151*4882a593Smuzhiyun		    |          |         child31 -> sibling32 -> null
152*4882a593Smuzhiyun		    |          |           |           |
153*4882a593Smuzhiyun		    |          |          null        null
154*4882a593Smuzhiyun		    |          |
155*4882a593Smuzhiyun		    |        child21 -> sibling22 -> sibling23 -> null
156*4882a593Smuzhiyun		    |          |           |            |
157*4882a593Smuzhiyun		    |         null        null         null
158*4882a593Smuzhiyun		    |
159*4882a593Smuzhiyun		    child11 -> sibling12 -> sibling13 -> sibling14 -> null
160*4882a593Smuzhiyun		    |          |            |            |
161*4882a593Smuzhiyun		    null       null          |           null
162*4882a593Smuzhiyun					    |
163*4882a593Smuzhiyun					    child131 -> null
164*4882a593Smuzhiyun					    |
165*4882a593Smuzhiyun					    null
166*4882a593Smuzhiyun    -----------------------------------------------------------------------
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun    root ('/')
169*4882a593Smuzhiyun    |
170*4882a593Smuzhiyun    testcase-data -> child1 -> sibling2 -> sibling3 -> sibling4 -> null
171*4882a593Smuzhiyun    |               |          |           |           |
172*4882a593Smuzhiyun    |             (...)      (...)       (...)        null
173*4882a593Smuzhiyun    |
174*4882a593Smuzhiyun    test-sibling3 -> test-sibling2 -> test-sibling1 -> test-child0 -> null
175*4882a593Smuzhiyun    |                |                   |                |
176*4882a593Smuzhiyun    null             null                null         test-child01
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun
179*4882a593SmuzhiyunFigure 3: Live device tree structure after attaching the testcase-data.
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun
182*4882a593SmuzhiyunAstute readers would have noticed that test-child0 node becomes the last
183*4882a593Smuzhiyunsibling compared to the earlier structure (Figure 2). After attaching first
184*4882a593Smuzhiyuntest-child0 the test-sibling1 is attached that pushes the child node
185*4882a593Smuzhiyun(i.e. test-child0) to become a sibling and makes itself a child node,
186*4882a593Smuzhiyunas mentioned above.
187*4882a593Smuzhiyun
188*4882a593SmuzhiyunIf a duplicate node is found (i.e. if a node with same full_name property is
189*4882a593Smuzhiyunalready present in the live tree), then the node isn't attached rather its
190*4882a593Smuzhiyunproperties are updated to the live tree's node by calling the function
191*4882a593Smuzhiyunupdate_node_properties().
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun2.2. Removing the test data
195*4882a593Smuzhiyun---------------------------
196*4882a593Smuzhiyun
197*4882a593SmuzhiyunOnce the test case execution is complete, selftest_data_remove is called in
198*4882a593Smuzhiyunorder to remove the device nodes attached initially (first the leaf nodes are
199*4882a593Smuzhiyundetached and then moving up the parent nodes are removed, and eventually the
200*4882a593Smuzhiyunwhole tree). selftest_data_remove() calls detach_node_and_children() that uses
201*4882a593Smuzhiyunof_detach_node() to detach the nodes from the live device tree.
202*4882a593Smuzhiyun
203*4882a593SmuzhiyunTo detach a node, of_detach_node() either updates the child pointer of given
204*4882a593Smuzhiyunnode's parent to its sibling or attaches the previous sibling to the given
205*4882a593Smuzhiyunnode's sibling, as appropriate. That is it :)
206