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_irq.h>
13 #include <linux/of_platform.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
19 static struct selftest_results
{
24 #define selftest(result, fmt, ...) { \
26 selftest_results.failed++; \
27 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
29 selftest_results.passed++; \
30 pr_debug("pass %s():%i\n", __func__, __LINE__); \
34 static void __init
of_selftest_find_node_by_name(void)
36 struct device_node
*np
;
38 np
= of_find_node_by_path("/testcase-data");
39 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
40 "find /testcase-data failed\n");
43 /* Test if trailing '/' works */
44 np
= of_find_node_by_path("/testcase-data/");
45 selftest(!np
, "trailing '/' on /testcase-data/ should fail\n");
47 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
48 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
49 "find /testcase-data/phandle-tests/consumer-a failed\n");
52 np
= of_find_node_by_path("testcase-alias");
53 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
54 "find testcase-alias failed\n");
57 /* Test if trailing '/' works on aliases */
58 np
= of_find_node_by_path("testcase-alias/");
59 selftest(!np
, "trailing '/' on testcase-alias/ should fail\n");
61 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
62 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
63 "find testcase-alias/phandle-tests/consumer-a failed\n");
66 np
= of_find_node_by_path("/testcase-data/missing-path");
67 selftest(!np
, "non-existent path returned node %s\n", np
->full_name
);
70 np
= of_find_node_by_path("missing-alias");
71 selftest(!np
, "non-existent alias returned node %s\n", np
->full_name
);
74 np
= of_find_node_by_path("testcase-alias/missing-path");
75 selftest(!np
, "non-existent alias with relative path returned node %s\n", np
->full_name
);
79 static void __init
of_selftest_dynamic(void)
81 struct device_node
*np
;
82 struct property
*prop
;
84 np
= of_find_node_by_path("/testcase-data");
86 pr_err("missing testcase data\n");
90 /* Array of 4 properties for the purpose of testing */
91 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
93 selftest(0, "kzalloc() failed\n");
97 /* Add a new property - should pass*/
98 prop
->name
= "new-property";
99 prop
->value
= "new-property-data";
100 prop
->length
= strlen(prop
->value
);
101 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
103 /* Try to add an existing property - should fail */
105 prop
->name
= "new-property";
106 prop
->value
= "new-property-data-should-fail";
107 prop
->length
= strlen(prop
->value
);
108 selftest(of_add_property(np
, prop
) != 0,
109 "Adding an existing property should have failed\n");
111 /* Try to modify an existing property - should pass */
112 prop
->value
= "modify-property-data-should-pass";
113 prop
->length
= strlen(prop
->value
);
114 selftest(of_update_property(np
, prop
) == 0,
115 "Updating an existing property should have passed\n");
117 /* Try to modify non-existent property - should pass*/
119 prop
->name
= "modify-property";
120 prop
->value
= "modify-missing-property-data-should-pass";
121 prop
->length
= strlen(prop
->value
);
122 selftest(of_update_property(np
, prop
) == 0,
123 "Updating a missing property should have passed\n");
125 /* Remove property - should pass */
126 selftest(of_remove_property(np
, prop
) == 0,
127 "Removing a property should have passed\n");
129 /* Adding very large property - should pass */
131 prop
->name
= "large-property-PAGE_SIZEx8";
132 prop
->length
= PAGE_SIZE
* 8;
133 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
134 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
136 selftest(of_add_property(np
, prop
) == 0,
137 "Adding a large property should have passed\n");
140 static void __init
of_selftest_parse_phandle_with_args(void)
142 struct device_node
*np
;
143 struct of_phandle_args args
;
146 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
148 pr_err("missing testcase data\n");
152 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
153 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
155 for (i
= 0; i
< 8; i
++) {
157 rc
= of_parse_phandle_with_args(np
, "phandle-list",
158 "#phandle-cells", i
, &args
);
160 /* Test the values from tests-phandle.dtsi */
164 passed
&= (args
.args_count
== 1);
165 passed
&= (args
.args
[0] == (i
+ 1));
169 passed
&= (args
.args_count
== 2);
170 passed
&= (args
.args
[0] == (i
+ 1));
171 passed
&= (args
.args
[1] == 0);
174 passed
&= (rc
== -ENOENT
);
178 passed
&= (args
.args_count
== 3);
179 passed
&= (args
.args
[0] == (i
+ 1));
180 passed
&= (args
.args
[1] == 4);
181 passed
&= (args
.args
[2] == 3);
185 passed
&= (args
.args_count
== 2);
186 passed
&= (args
.args
[0] == (i
+ 1));
187 passed
&= (args
.args
[1] == 100);
191 passed
&= (args
.args_count
== 0);
195 passed
&= (args
.args_count
== 1);
196 passed
&= (args
.args
[0] == (i
+ 1));
199 passed
&= (rc
== -ENOENT
);
205 selftest(passed
, "index %i - data error on node %s rc=%i\n",
206 i
, args
.np
->full_name
, rc
);
209 /* Check for missing list property */
210 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
211 "#phandle-cells", 0, &args
);
212 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
213 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
215 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
217 /* Check for missing cells property */
218 rc
= of_parse_phandle_with_args(np
, "phandle-list",
219 "#phandle-cells-missing", 0, &args
);
220 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
221 rc
= of_count_phandle_with_args(np
, "phandle-list",
222 "#phandle-cells-missing");
223 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
225 /* Check for bad phandle in list */
226 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
227 "#phandle-cells", 0, &args
);
228 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
229 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
231 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
233 /* Check for incorrectly formed argument list */
234 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
235 "#phandle-cells", 1, &args
);
236 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
237 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
239 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
242 static void __init
of_selftest_property_match_string(void)
244 struct device_node
*np
;
247 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
249 pr_err("No testcase data in device tree\n");
253 rc
= of_property_match_string(np
, "phandle-list-names", "first");
254 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
255 rc
= of_property_match_string(np
, "phandle-list-names", "second");
256 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
257 rc
= of_property_match_string(np
, "phandle-list-names", "third");
258 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
259 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
260 selftest(rc
== -ENODATA
, "unmatched string; rc=%i", rc
);
261 rc
= of_property_match_string(np
, "missing-property", "blah");
262 selftest(rc
== -EINVAL
, "missing property; rc=%i", rc
);
263 rc
= of_property_match_string(np
, "empty-property", "blah");
264 selftest(rc
== -ENODATA
, "empty property; rc=%i", rc
);
265 rc
= of_property_match_string(np
, "unterminated-string", "blah");
266 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i", rc
);
269 static void __init
of_selftest_parse_interrupts(void)
271 struct device_node
*np
;
272 struct of_phandle_args args
;
275 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
277 pr_err("missing testcase data\n");
281 for (i
= 0; i
< 4; i
++) {
284 rc
= of_irq_parse_one(np
, i
, &args
);
287 passed
&= (args
.args_count
== 1);
288 passed
&= (args
.args
[0] == (i
+ 1));
290 selftest(passed
, "index %i - data error on node %s rc=%i\n",
291 i
, args
.np
->full_name
, rc
);
295 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
297 pr_err("missing testcase data\n");
301 for (i
= 0; i
< 4; i
++) {
304 rc
= of_irq_parse_one(np
, i
, &args
);
306 /* Test the values from tests-phandle.dtsi */
310 passed
&= (args
.args_count
== 1);
311 passed
&= (args
.args
[0] == 9);
315 passed
&= (args
.args_count
== 3);
316 passed
&= (args
.args
[0] == 10);
317 passed
&= (args
.args
[1] == 11);
318 passed
&= (args
.args
[2] == 12);
322 passed
&= (args
.args_count
== 2);
323 passed
&= (args
.args
[0] == 13);
324 passed
&= (args
.args
[1] == 14);
328 passed
&= (args
.args_count
== 2);
329 passed
&= (args
.args
[0] == 15);
330 passed
&= (args
.args
[1] == 16);
335 selftest(passed
, "index %i - data error on node %s rc=%i\n",
336 i
, args
.np
->full_name
, rc
);
341 static void __init
of_selftest_parse_interrupts_extended(void)
343 struct device_node
*np
;
344 struct of_phandle_args args
;
347 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
349 pr_err("missing testcase data\n");
353 for (i
= 0; i
< 7; i
++) {
355 rc
= of_irq_parse_one(np
, i
, &args
);
357 /* Test the values from tests-phandle.dtsi */
361 passed
&= (args
.args_count
== 1);
362 passed
&= (args
.args
[0] == 1);
366 passed
&= (args
.args_count
== 3);
367 passed
&= (args
.args
[0] == 2);
368 passed
&= (args
.args
[1] == 3);
369 passed
&= (args
.args
[2] == 4);
373 passed
&= (args
.args_count
== 2);
374 passed
&= (args
.args
[0] == 5);
375 passed
&= (args
.args
[1] == 6);
379 passed
&= (args
.args_count
== 1);
380 passed
&= (args
.args
[0] == 9);
384 passed
&= (args
.args_count
== 3);
385 passed
&= (args
.args
[0] == 10);
386 passed
&= (args
.args
[1] == 11);
387 passed
&= (args
.args
[2] == 12);
391 passed
&= (args
.args_count
== 2);
392 passed
&= (args
.args
[0] == 13);
393 passed
&= (args
.args
[1] == 14);
397 passed
&= (args
.args_count
== 1);
398 passed
&= (args
.args
[0] == 15);
404 selftest(passed
, "index %i - data error on node %s rc=%i\n",
405 i
, args
.np
->full_name
, rc
);
410 static struct of_device_id match_node_table
[] = {
411 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
412 { .data
= "B", .type
= "type1", }, /* followed by type alone */
414 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
415 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
416 { .data
= "Cc", .name
= "name2", .type
= "type2", },
418 { .data
= "E", .compatible
= "compat3" },
419 { .data
= "G", .compatible
= "compat2", },
420 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
421 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
422 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
423 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
430 } match_node_tests
[] = {
431 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
432 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
433 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
434 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
435 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
436 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
437 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
438 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
439 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
440 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
441 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
442 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
445 static void __init
of_selftest_match_node(void)
447 struct device_node
*np
;
448 const struct of_device_id
*match
;
451 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
452 np
= of_find_node_by_path(match_node_tests
[i
].path
);
454 selftest(0, "missing testcase node %s\n",
455 match_node_tests
[i
].path
);
459 match
= of_match_node(match_node_table
, np
);
461 selftest(0, "%s didn't match anything\n",
462 match_node_tests
[i
].path
);
466 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
467 selftest(0, "%s got wrong match. expected %s, got %s\n",
468 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
469 (const char *)match
->data
);
472 selftest(1, "passed");
476 static void __init
of_selftest_platform_populate(void)
479 struct device_node
*np
, *child
;
480 struct platform_device
*pdev
;
481 struct of_device_id match
[] = {
482 { .compatible
= "test-device", },
486 np
= of_find_node_by_path("/testcase-data");
487 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
489 /* Test that a missing irq domain returns -EPROBE_DEFER */
490 np
= of_find_node_by_path("/testcase-data/testcase-device1");
491 pdev
= of_find_device_by_node(np
);
492 selftest(pdev
, "device 1 creation failed\n");
494 irq
= platform_get_irq(pdev
, 0);
495 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
497 /* Test that a parsing failure does not return -EPROBE_DEFER */
498 np
= of_find_node_by_path("/testcase-data/testcase-device2");
499 pdev
= of_find_device_by_node(np
);
500 selftest(pdev
, "device 2 creation failed\n");
501 irq
= platform_get_irq(pdev
, 0);
502 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
504 np
= of_find_node_by_path("/testcase-data/platform-tests");
506 pr_err("No testcase data in device tree\n");
510 for_each_child_of_node(np
, child
) {
511 struct device_node
*grandchild
;
512 of_platform_populate(child
, match
, NULL
, NULL
);
513 for_each_child_of_node(child
, grandchild
)
514 selftest(of_find_device_by_node(grandchild
),
515 "Could not create device for node '%s'\n",
520 static int __init
of_selftest(void)
522 struct device_node
*np
;
524 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
526 pr_info("No testcase data in device tree; not running tests\n");
531 pr_info("start of selftest - you will see error messages\n");
532 of_selftest_find_node_by_name();
533 of_selftest_dynamic();
534 of_selftest_parse_phandle_with_args();
535 of_selftest_property_match_string();
536 of_selftest_parse_interrupts();
537 of_selftest_parse_interrupts_extended();
538 of_selftest_match_node();
539 of_selftest_platform_populate();
540 pr_info("end of selftest - %i passed, %i failed\n",
541 selftest_results
.passed
, selftest_results
.failed
);
544 late_initcall(of_selftest
);