2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/module.h>
12 #include <linux/of_fdt.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/list.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
20 #include "of_private.h"
22 static struct selftest_results
{
28 static struct device_node
*nodes
[NO_OF_NODES
];
29 static int last_node_index
;
31 #define selftest(result, fmt, ...) { \
33 selftest_results.failed++; \
34 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
36 selftest_results.passed++; \
37 pr_debug("pass %s():%i\n", __func__, __LINE__); \
41 static void __init
of_selftest_find_node_by_name(void)
43 struct device_node
*np
;
45 np
= of_find_node_by_path("/testcase-data");
46 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
47 "find /testcase-data failed\n");
50 /* Test if trailing '/' works */
51 np
= of_find_node_by_path("/testcase-data/");
52 selftest(!np
, "trailing '/' on /testcase-data/ should fail\n");
54 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
55 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
56 "find /testcase-data/phandle-tests/consumer-a failed\n");
59 np
= of_find_node_by_path("testcase-alias");
60 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
61 "find testcase-alias failed\n");
64 /* Test if trailing '/' works on aliases */
65 np
= of_find_node_by_path("testcase-alias/");
66 selftest(!np
, "trailing '/' on testcase-alias/ should fail\n");
68 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
69 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
70 "find testcase-alias/phandle-tests/consumer-a failed\n");
73 np
= of_find_node_by_path("/testcase-data/missing-path");
74 selftest(!np
, "non-existent path returned node %s\n", np
->full_name
);
77 np
= of_find_node_by_path("missing-alias");
78 selftest(!np
, "non-existent alias returned node %s\n", np
->full_name
);
81 np
= of_find_node_by_path("testcase-alias/missing-path");
82 selftest(!np
, "non-existent alias with relative path returned node %s\n", np
->full_name
);
86 static void __init
of_selftest_dynamic(void)
88 struct device_node
*np
;
89 struct property
*prop
;
91 np
= of_find_node_by_path("/testcase-data");
93 pr_err("missing testcase data\n");
97 /* Array of 4 properties for the purpose of testing */
98 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
100 selftest(0, "kzalloc() failed\n");
104 /* Add a new property - should pass*/
105 prop
->name
= "new-property";
106 prop
->value
= "new-property-data";
107 prop
->length
= strlen(prop
->value
);
108 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
110 /* Try to add an existing property - should fail */
112 prop
->name
= "new-property";
113 prop
->value
= "new-property-data-should-fail";
114 prop
->length
= strlen(prop
->value
);
115 selftest(of_add_property(np
, prop
) != 0,
116 "Adding an existing property should have failed\n");
118 /* Try to modify an existing property - should pass */
119 prop
->value
= "modify-property-data-should-pass";
120 prop
->length
= strlen(prop
->value
);
121 selftest(of_update_property(np
, prop
) == 0,
122 "Updating an existing property should have passed\n");
124 /* Try to modify non-existent property - should pass*/
126 prop
->name
= "modify-property";
127 prop
->value
= "modify-missing-property-data-should-pass";
128 prop
->length
= strlen(prop
->value
);
129 selftest(of_update_property(np
, prop
) == 0,
130 "Updating a missing property should have passed\n");
132 /* Remove property - should pass */
133 selftest(of_remove_property(np
, prop
) == 0,
134 "Removing a property should have passed\n");
136 /* Adding very large property - should pass */
138 prop
->name
= "large-property-PAGE_SIZEx8";
139 prop
->length
= PAGE_SIZE
* 8;
140 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
141 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
143 selftest(of_add_property(np
, prop
) == 0,
144 "Adding a large property should have passed\n");
147 static void __init
of_selftest_parse_phandle_with_args(void)
149 struct device_node
*np
;
150 struct of_phandle_args args
;
153 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
155 pr_err("missing testcase data\n");
159 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
160 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
162 for (i
= 0; i
< 8; i
++) {
164 rc
= of_parse_phandle_with_args(np
, "phandle-list",
165 "#phandle-cells", i
, &args
);
167 /* Test the values from tests-phandle.dtsi */
171 passed
&= (args
.args_count
== 1);
172 passed
&= (args
.args
[0] == (i
+ 1));
176 passed
&= (args
.args_count
== 2);
177 passed
&= (args
.args
[0] == (i
+ 1));
178 passed
&= (args
.args
[1] == 0);
181 passed
&= (rc
== -ENOENT
);
185 passed
&= (args
.args_count
== 3);
186 passed
&= (args
.args
[0] == (i
+ 1));
187 passed
&= (args
.args
[1] == 4);
188 passed
&= (args
.args
[2] == 3);
192 passed
&= (args
.args_count
== 2);
193 passed
&= (args
.args
[0] == (i
+ 1));
194 passed
&= (args
.args
[1] == 100);
198 passed
&= (args
.args_count
== 0);
202 passed
&= (args
.args_count
== 1);
203 passed
&= (args
.args
[0] == (i
+ 1));
206 passed
&= (rc
== -ENOENT
);
212 selftest(passed
, "index %i - data error on node %s rc=%i\n",
213 i
, args
.np
->full_name
, rc
);
216 /* Check for missing list property */
217 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
218 "#phandle-cells", 0, &args
);
219 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
220 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
222 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
224 /* Check for missing cells property */
225 rc
= of_parse_phandle_with_args(np
, "phandle-list",
226 "#phandle-cells-missing", 0, &args
);
227 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
228 rc
= of_count_phandle_with_args(np
, "phandle-list",
229 "#phandle-cells-missing");
230 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
232 /* Check for bad phandle in list */
233 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
234 "#phandle-cells", 0, &args
);
235 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
236 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
238 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
240 /* Check for incorrectly formed argument list */
241 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
242 "#phandle-cells", 1, &args
);
243 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
244 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
246 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
249 static void __init
of_selftest_property_match_string(void)
251 struct device_node
*np
;
254 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
256 pr_err("No testcase data in device tree\n");
260 rc
= of_property_match_string(np
, "phandle-list-names", "first");
261 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
262 rc
= of_property_match_string(np
, "phandle-list-names", "second");
263 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
264 rc
= of_property_match_string(np
, "phandle-list-names", "third");
265 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
266 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
267 selftest(rc
== -ENODATA
, "unmatched string; rc=%i", rc
);
268 rc
= of_property_match_string(np
, "missing-property", "blah");
269 selftest(rc
== -EINVAL
, "missing property; rc=%i", rc
);
270 rc
= of_property_match_string(np
, "empty-property", "blah");
271 selftest(rc
== -ENODATA
, "empty property; rc=%i", rc
);
272 rc
= of_property_match_string(np
, "unterminated-string", "blah");
273 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i", rc
);
276 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
277 (p1)->value && (p2)->value && \
278 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
279 !strcmp((p1)->name, (p2)->name))
280 static void __init
of_selftest_property_copy(void)
282 #ifdef CONFIG_OF_DYNAMIC
283 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
284 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
285 struct property
*new;
287 new = __of_prop_dup(&p1
, GFP_KERNEL
);
288 selftest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
293 new = __of_prop_dup(&p2
, GFP_KERNEL
);
294 selftest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
301 static void __init
of_selftest_changeset(void)
303 #ifdef CONFIG_OF_DYNAMIC
304 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 0, .value
= "" };
305 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
306 struct property
*ppremove
;
307 struct device_node
*n1
, *n2
, *n21
, *nremove
, *parent
;
308 struct of_changeset chgset
;
310 of_changeset_init(&chgset
);
311 n1
= __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL
);
312 selftest(n1
, "testcase setup failure\n");
313 n2
= __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL
);
314 selftest(n2
, "testcase setup failure\n");
315 n21
= __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL
);
316 selftest(n21
, "testcase setup failure %p\n", n21
);
317 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
318 selftest(nremove
, "testcase setup failure\n");
319 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
320 selftest(ppadd
, "testcase setup failure\n");
321 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
322 selftest(ppupdate
, "testcase setup failure\n");
323 parent
= nremove
->parent
;
328 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
329 selftest(ppremove
, "failed to find removal prop");
331 of_changeset_init(&chgset
);
332 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
333 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
334 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
335 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
336 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
337 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
338 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
339 mutex_lock(&of_mutex
);
340 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
341 mutex_unlock(&of_mutex
);
343 mutex_lock(&of_mutex
);
344 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
345 mutex_unlock(&of_mutex
);
347 of_changeset_destroy(&chgset
);
351 static void __init
of_selftest_parse_interrupts(void)
353 struct device_node
*np
;
354 struct of_phandle_args args
;
357 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
359 pr_err("missing testcase data\n");
363 for (i
= 0; i
< 4; i
++) {
366 rc
= of_irq_parse_one(np
, i
, &args
);
369 passed
&= (args
.args_count
== 1);
370 passed
&= (args
.args
[0] == (i
+ 1));
372 selftest(passed
, "index %i - data error on node %s rc=%i\n",
373 i
, args
.np
->full_name
, rc
);
377 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
379 pr_err("missing testcase data\n");
383 for (i
= 0; i
< 4; i
++) {
386 rc
= of_irq_parse_one(np
, i
, &args
);
388 /* Test the values from tests-phandle.dtsi */
392 passed
&= (args
.args_count
== 1);
393 passed
&= (args
.args
[0] == 9);
397 passed
&= (args
.args_count
== 3);
398 passed
&= (args
.args
[0] == 10);
399 passed
&= (args
.args
[1] == 11);
400 passed
&= (args
.args
[2] == 12);
404 passed
&= (args
.args_count
== 2);
405 passed
&= (args
.args
[0] == 13);
406 passed
&= (args
.args
[1] == 14);
410 passed
&= (args
.args_count
== 2);
411 passed
&= (args
.args
[0] == 15);
412 passed
&= (args
.args
[1] == 16);
417 selftest(passed
, "index %i - data error on node %s rc=%i\n",
418 i
, args
.np
->full_name
, rc
);
423 static void __init
of_selftest_parse_interrupts_extended(void)
425 struct device_node
*np
;
426 struct of_phandle_args args
;
429 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
431 pr_err("missing testcase data\n");
435 for (i
= 0; i
< 7; i
++) {
437 rc
= of_irq_parse_one(np
, i
, &args
);
439 /* Test the values from tests-phandle.dtsi */
443 passed
&= (args
.args_count
== 1);
444 passed
&= (args
.args
[0] == 1);
448 passed
&= (args
.args_count
== 3);
449 passed
&= (args
.args
[0] == 2);
450 passed
&= (args
.args
[1] == 3);
451 passed
&= (args
.args
[2] == 4);
455 passed
&= (args
.args_count
== 2);
456 passed
&= (args
.args
[0] == 5);
457 passed
&= (args
.args
[1] == 6);
461 passed
&= (args
.args_count
== 1);
462 passed
&= (args
.args
[0] == 9);
466 passed
&= (args
.args_count
== 3);
467 passed
&= (args
.args
[0] == 10);
468 passed
&= (args
.args
[1] == 11);
469 passed
&= (args
.args
[2] == 12);
473 passed
&= (args
.args_count
== 2);
474 passed
&= (args
.args
[0] == 13);
475 passed
&= (args
.args
[1] == 14);
479 passed
&= (args
.args_count
== 1);
480 passed
&= (args
.args
[0] == 15);
486 selftest(passed
, "index %i - data error on node %s rc=%i\n",
487 i
, args
.np
->full_name
, rc
);
492 static struct of_device_id match_node_table
[] = {
493 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
494 { .data
= "B", .type
= "type1", }, /* followed by type alone */
496 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
497 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
498 { .data
= "Cc", .name
= "name2", .type
= "type2", },
500 { .data
= "E", .compatible
= "compat3" },
501 { .data
= "G", .compatible
= "compat2", },
502 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
503 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
504 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
505 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
512 } match_node_tests
[] = {
513 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
514 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
515 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
516 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
517 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
518 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
519 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
520 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
521 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
522 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
523 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
524 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
527 static void __init
of_selftest_match_node(void)
529 struct device_node
*np
;
530 const struct of_device_id
*match
;
533 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
534 np
= of_find_node_by_path(match_node_tests
[i
].path
);
536 selftest(0, "missing testcase node %s\n",
537 match_node_tests
[i
].path
);
541 match
= of_match_node(match_node_table
, np
);
543 selftest(0, "%s didn't match anything\n",
544 match_node_tests
[i
].path
);
548 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
549 selftest(0, "%s got wrong match. expected %s, got %s\n",
550 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
551 (const char *)match
->data
);
554 selftest(1, "passed");
558 static void __init
of_selftest_platform_populate(void)
561 struct device_node
*np
, *child
;
562 struct platform_device
*pdev
;
563 struct of_device_id match
[] = {
564 { .compatible
= "test-device", },
568 np
= of_find_node_by_path("/testcase-data");
569 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
571 /* Test that a missing irq domain returns -EPROBE_DEFER */
572 np
= of_find_node_by_path("/testcase-data/testcase-device1");
573 pdev
= of_find_device_by_node(np
);
574 selftest(pdev
, "device 1 creation failed\n");
576 irq
= platform_get_irq(pdev
, 0);
577 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
579 /* Test that a parsing failure does not return -EPROBE_DEFER */
580 np
= of_find_node_by_path("/testcase-data/testcase-device2");
581 pdev
= of_find_device_by_node(np
);
582 selftest(pdev
, "device 2 creation failed\n");
583 irq
= platform_get_irq(pdev
, 0);
584 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
586 np
= of_find_node_by_path("/testcase-data/platform-tests");
588 pr_err("No testcase data in device tree\n");
592 for_each_child_of_node(np
, child
) {
593 struct device_node
*grandchild
;
594 of_platform_populate(child
, match
, NULL
, NULL
);
595 for_each_child_of_node(child
, grandchild
)
596 selftest(of_find_device_by_node(grandchild
),
597 "Could not create device for node '%s'\n",
603 * update_node_properties - adds the properties
604 * of np into dup node (present in live tree) and
605 * updates parent of children of np to dup.
607 * @np: node already present in live tree
608 * @dup: node present in live tree to be updated
610 static void update_node_properties(struct device_node
*np
,
611 struct device_node
*dup
)
613 struct property
*prop
;
614 struct device_node
*child
;
616 for_each_property_of_node(np
, prop
)
617 of_add_property(dup
, prop
);
619 for_each_child_of_node(np
, child
)
624 * attach_node_and_children - attaches nodes
625 * and its children to live tree
627 * @np: Node to attach to live tree
629 static int attach_node_and_children(struct device_node
*np
)
631 struct device_node
*next
, *root
= np
, *dup
;
634 pr_warn("%s: No tree to attach; not running tests\n",
642 /* storing a copy in temporary node */
646 nodes
[last_node_index
++] = dup
;
653 dup
= of_find_node_by_path(np
->full_name
);
655 update_node_properties(np
, dup
);
658 if (np
->parent
== root
)
659 np
->parent
= of_allnodes
;
669 * selftest_data_add - Reads, copies data from
670 * linked tree and attaches it to the live tree
672 static int __init
selftest_data_add(void)
675 struct device_node
*selftest_data_node
;
676 extern uint8_t __dtb_testcases_begin
[];
677 extern uint8_t __dtb_testcases_end
[];
678 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
680 if (!size
|| !of_allnodes
) {
681 pr_warn("%s: No testcase data to attach; not running tests\n",
687 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
689 if (!selftest_data
) {
690 pr_warn("%s: Failed to allocate memory for selftest_data; "
691 "not running tests\n", __func__
);
694 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
696 /* attach the sub-tree to live tree */
697 return attach_node_and_children(selftest_data_node
);
701 * detach_node_and_children - detaches node
702 * and its children from live tree
704 * @np: Node to detach from live tree
706 static void detach_node_and_children(struct device_node
*np
)
709 detach_node_and_children(np
->child
);
712 detach_node_and_children(np
->sibling
);
718 * selftest_data_remove - removes the selftest data
719 * nodes from the live tree
721 static void selftest_data_remove(void)
723 struct device_node
*np
;
724 struct property
*prop
;
726 while (last_node_index
>= 0) {
727 if (nodes
[last_node_index
]) {
728 np
= of_find_node_by_path(nodes
[last_node_index
]->full_name
);
729 if (strcmp(np
->full_name
, "/aliases") != 0) {
730 detach_node_and_children(np
->child
);
733 for_each_property_of_node(np
, prop
) {
734 if (strcmp(prop
->name
, "testcase-alias") == 0)
735 of_remove_property(np
, prop
);
743 static int __init
of_selftest(void)
745 struct device_node
*np
;
748 /* adding data for selftest */
749 res
= selftest_data_add();
753 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
755 pr_info("No testcase data in device tree; not running tests\n");
760 pr_info("start of selftest - you will see error messages\n");
761 of_selftest_find_node_by_name();
762 of_selftest_dynamic();
763 of_selftest_parse_phandle_with_args();
764 of_selftest_property_match_string();
765 of_selftest_property_copy();
766 of_selftest_changeset();
767 of_selftest_parse_interrupts();
768 of_selftest_parse_interrupts_extended();
769 of_selftest_match_node();
770 of_selftest_platform_populate();
771 pr_info("end of selftest - %i passed, %i failed\n",
772 selftest_results
.passed
, selftest_results
.failed
);
774 /* removing selftest data from live tree */
775 selftest_data_remove();
779 late_initcall(of_selftest
);