2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/hashtable.h>
11 #include <linux/module.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
23 #include "of_private.h"
25 static struct selftest_results {
30 #define selftest(result, fmt, ...) ({ \
31 bool failed = !(result); \
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__); \
42 static void __init of_selftest_find_node_by_name(void)
44 struct device_node *np;
47 np = of_find_node_by_path("/testcase-data");
48 selftest(np && !strcmp("/testcase-data", np->full_name),
49 "find /testcase-data failed\n");
52 /* Test if trailing '/' works */
53 np = of_find_node_by_path("/testcase-data/");
54 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
56 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
57 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
58 "find /testcase-data/phandle-tests/consumer-a failed\n");
61 np = of_find_node_by_path("testcase-alias");
62 selftest(np && !strcmp("/testcase-data", np->full_name),
63 "find testcase-alias failed\n");
66 /* Test if trailing '/' works on aliases */
67 np = of_find_node_by_path("testcase-alias/");
68 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
70 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
71 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
72 "find testcase-alias/phandle-tests/consumer-a failed\n");
75 np = of_find_node_by_path("/testcase-data/missing-path");
76 selftest(!np, "non-existent path returned node %s\n", np->full_name);
79 np = of_find_node_by_path("missing-alias");
80 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
83 np = of_find_node_by_path("testcase-alias/missing-path");
84 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
87 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
88 selftest(np && !strcmp("testoption", options),
89 "option path test failed\n");
92 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
93 selftest(np, "NULL option path test failed\n");
96 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
98 selftest(np && !strcmp("testaliasoption", options),
99 "option alias path test failed\n");
102 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
103 selftest(np, "NULL option alias path test failed\n");
106 options = "testoption";
107 np = of_find_node_opts_by_path("testcase-alias", &options);
108 selftest(np && !options, "option clearing test failed\n");
111 options = "testoption";
112 np = of_find_node_opts_by_path("/", &options);
113 selftest(np && !options, "option clearing root node test failed\n");
117 static void __init of_selftest_dynamic(void)
119 struct device_node *np;
120 struct property *prop;
122 np = of_find_node_by_path("/testcase-data");
124 pr_err("missing testcase data\n");
128 /* Array of 4 properties for the purpose of testing */
129 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
131 selftest(0, "kzalloc() failed\n");
135 /* Add a new property - should pass*/
136 prop->name = "new-property";
137 prop->value = "new-property-data";
138 prop->length = strlen(prop->value);
139 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
141 /* Try to add an existing property - should fail */
143 prop->name = "new-property";
144 prop->value = "new-property-data-should-fail";
145 prop->length = strlen(prop->value);
146 selftest(of_add_property(np, prop) != 0,
147 "Adding an existing property should have failed\n");
149 /* Try to modify an existing property - should pass */
150 prop->value = "modify-property-data-should-pass";
151 prop->length = strlen(prop->value);
152 selftest(of_update_property(np, prop) == 0,
153 "Updating an existing property should have passed\n");
155 /* Try to modify non-existent property - should pass*/
157 prop->name = "modify-property";
158 prop->value = "modify-missing-property-data-should-pass";
159 prop->length = strlen(prop->value);
160 selftest(of_update_property(np, prop) == 0,
161 "Updating a missing property should have passed\n");
163 /* Remove property - should pass */
164 selftest(of_remove_property(np, prop) == 0,
165 "Removing a property should have passed\n");
167 /* Adding very large property - should pass */
169 prop->name = "large-property-PAGE_SIZEx8";
170 prop->length = PAGE_SIZE * 8;
171 prop->value = kzalloc(prop->length, GFP_KERNEL);
172 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
174 selftest(of_add_property(np, prop) == 0,
175 "Adding a large property should have passed\n");
178 static int __init of_selftest_check_node_linkage(struct device_node *np)
180 struct device_node *child;
183 for_each_child_of_node(np, child) {
184 if (child->parent != np) {
185 pr_err("Child node %s links to wrong parent %s\n",
186 child->name, np->name);
190 rc = of_selftest_check_node_linkage(child);
199 static void __init of_selftest_check_tree_linkage(void)
201 struct device_node *np;
202 int allnode_count = 0, child_count;
207 for_each_of_allnodes(np)
209 child_count = of_selftest_check_node_linkage(of_root);
211 selftest(child_count > 0, "Device node data structure is corrupted\n");
212 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
213 "sibling lists size (%i)\n", allnode_count, child_count);
214 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
218 struct hlist_node node;
219 struct device_node *np;
222 static DEFINE_HASHTABLE(phandle_ht, 8);
223 static void __init of_selftest_check_phandles(void)
225 struct device_node *np;
226 struct node_hash *nh;
227 struct hlist_node *tmp;
228 int i, dup_count = 0, phandle_count = 0;
230 for_each_of_allnodes(np) {
234 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
235 if (nh->np->phandle == np->phandle) {
236 pr_info("Duplicate phandle! %i used by %s and %s\n",
237 np->phandle, nh->np->full_name, np->full_name);
243 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
248 hash_add(phandle_ht, &nh->node, np->phandle);
251 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
252 dup_count, phandle_count);
255 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
261 static void __init of_selftest_parse_phandle_with_args(void)
263 struct device_node *np;
264 struct of_phandle_args args;
267 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
269 pr_err("missing testcase data\n");
273 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
274 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
276 for (i = 0; i < 8; i++) {
278 rc = of_parse_phandle_with_args(np, "phandle-list",
279 "#phandle-cells", i, &args);
281 /* Test the values from tests-phandle.dtsi */
285 passed &= (args.args_count == 1);
286 passed &= (args.args[0] == (i + 1));
290 passed &= (args.args_count == 2);
291 passed &= (args.args[0] == (i + 1));
292 passed &= (args.args[1] == 0);
295 passed &= (rc == -ENOENT);
299 passed &= (args.args_count == 3);
300 passed &= (args.args[0] == (i + 1));
301 passed &= (args.args[1] == 4);
302 passed &= (args.args[2] == 3);
306 passed &= (args.args_count == 2);
307 passed &= (args.args[0] == (i + 1));
308 passed &= (args.args[1] == 100);
312 passed &= (args.args_count == 0);
316 passed &= (args.args_count == 1);
317 passed &= (args.args[0] == (i + 1));
320 passed &= (rc == -ENOENT);
326 selftest(passed, "index %i - data error on node %s rc=%i\n",
327 i, args.np->full_name, rc);
330 /* Check for missing list property */
331 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
332 "#phandle-cells", 0, &args);
333 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
334 rc = of_count_phandle_with_args(np, "phandle-list-missing",
336 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
338 /* Check for missing cells property */
339 rc = of_parse_phandle_with_args(np, "phandle-list",
340 "#phandle-cells-missing", 0, &args);
341 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
342 rc = of_count_phandle_with_args(np, "phandle-list",
343 "#phandle-cells-missing");
344 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
346 /* Check for bad phandle in list */
347 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
348 "#phandle-cells", 0, &args);
349 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
350 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
352 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
354 /* Check for incorrectly formed argument list */
355 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
356 "#phandle-cells", 1, &args);
357 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
358 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
360 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
363 static void __init of_selftest_property_string(void)
365 const char *strings[4];
366 struct device_node *np;
369 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
371 pr_err("No testcase data in device tree\n");
375 rc = of_property_match_string(np, "phandle-list-names", "first");
376 selftest(rc == 0, "first expected:0 got:%i\n", rc);
377 rc = of_property_match_string(np, "phandle-list-names", "second");
378 selftest(rc == 1, "second expected:0 got:%i\n", rc);
379 rc = of_property_match_string(np, "phandle-list-names", "third");
380 selftest(rc == 2, "third expected:0 got:%i\n", rc);
381 rc = of_property_match_string(np, "phandle-list-names", "fourth");
382 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
383 rc = of_property_match_string(np, "missing-property", "blah");
384 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
385 rc = of_property_match_string(np, "empty-property", "blah");
386 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
387 rc = of_property_match_string(np, "unterminated-string", "blah");
388 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
390 /* of_property_count_strings() tests */
391 rc = of_property_count_strings(np, "string-property");
392 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
393 rc = of_property_count_strings(np, "phandle-list-names");
394 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
395 rc = of_property_count_strings(np, "unterminated-string");
396 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
397 rc = of_property_count_strings(np, "unterminated-string-list");
398 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
400 /* of_property_read_string_index() tests */
401 rc = of_property_read_string_index(np, "string-property", 0, strings);
402 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
404 rc = of_property_read_string_index(np, "string-property", 1, strings);
405 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
406 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
407 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
408 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
409 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
410 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
411 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
413 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
414 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
416 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
417 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
418 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
419 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
421 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
422 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
425 /* of_property_read_string_array() tests */
426 rc = of_property_read_string_array(np, "string-property", strings, 4);
427 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
428 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
429 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
430 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
431 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
432 /* -- An incorrectly formed string should cause a failure */
433 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
434 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
435 /* -- parsing the correctly formed strings should still work: */
437 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
438 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
440 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
441 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
444 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
445 (p1)->value && (p2)->value && \
446 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
447 !strcmp((p1)->name, (p2)->name))
448 static void __init of_selftest_property_copy(void)
450 #ifdef CONFIG_OF_DYNAMIC
451 struct property p1 = { .name = "p1", .length = 0, .value = "" };
452 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
453 struct property *new;
455 new = __of_prop_dup(&p1, GFP_KERNEL);
456 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
461 new = __of_prop_dup(&p2, GFP_KERNEL);
462 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
469 static void __init of_selftest_changeset(void)
471 #ifdef CONFIG_OF_DYNAMIC
472 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
473 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
474 struct property *ppremove;
475 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
476 struct of_changeset chgset;
478 of_changeset_init(&chgset);
479 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
480 selftest(n1, "testcase setup failure\n");
481 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
482 selftest(n2, "testcase setup failure\n");
483 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
484 selftest(n21, "testcase setup failure %p\n", n21);
485 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
486 selftest(nremove, "testcase setup failure\n");
487 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
488 selftest(ppadd, "testcase setup failure\n");
489 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
490 selftest(ppupdate, "testcase setup failure\n");
491 parent = nremove->parent;
496 ppremove = of_find_property(parent, "prop-remove", NULL);
497 selftest(ppremove, "failed to find removal prop");
499 of_changeset_init(&chgset);
500 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
501 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
502 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
503 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
504 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
505 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
506 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
507 mutex_lock(&of_mutex);
508 selftest(!of_changeset_apply(&chgset), "apply failed\n");
509 mutex_unlock(&of_mutex);
511 /* Make sure node names are constructed correctly */
512 selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
513 "'%s' not added\n", n21->full_name);
516 mutex_lock(&of_mutex);
517 selftest(!of_changeset_revert(&chgset), "revert failed\n");
518 mutex_unlock(&of_mutex);
520 of_changeset_destroy(&chgset);
524 static void __init of_selftest_parse_interrupts(void)
526 struct device_node *np;
527 struct of_phandle_args args;
530 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
532 pr_err("missing testcase data\n");
536 for (i = 0; i < 4; i++) {
539 rc = of_irq_parse_one(np, i, &args);
542 passed &= (args.args_count == 1);
543 passed &= (args.args[0] == (i + 1));
545 selftest(passed, "index %i - data error on node %s rc=%i\n",
546 i, args.np->full_name, rc);
550 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
552 pr_err("missing testcase data\n");
556 for (i = 0; i < 4; i++) {
559 rc = of_irq_parse_one(np, i, &args);
561 /* Test the values from tests-phandle.dtsi */
565 passed &= (args.args_count == 1);
566 passed &= (args.args[0] == 9);
570 passed &= (args.args_count == 3);
571 passed &= (args.args[0] == 10);
572 passed &= (args.args[1] == 11);
573 passed &= (args.args[2] == 12);
577 passed &= (args.args_count == 2);
578 passed &= (args.args[0] == 13);
579 passed &= (args.args[1] == 14);
583 passed &= (args.args_count == 2);
584 passed &= (args.args[0] == 15);
585 passed &= (args.args[1] == 16);
590 selftest(passed, "index %i - data error on node %s rc=%i\n",
591 i, args.np->full_name, rc);
596 static void __init of_selftest_parse_interrupts_extended(void)
598 struct device_node *np;
599 struct of_phandle_args args;
602 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
604 pr_err("missing testcase data\n");
608 for (i = 0; i < 7; i++) {
610 rc = of_irq_parse_one(np, i, &args);
612 /* Test the values from tests-phandle.dtsi */
616 passed &= (args.args_count == 1);
617 passed &= (args.args[0] == 1);
621 passed &= (args.args_count == 3);
622 passed &= (args.args[0] == 2);
623 passed &= (args.args[1] == 3);
624 passed &= (args.args[2] == 4);
628 passed &= (args.args_count == 2);
629 passed &= (args.args[0] == 5);
630 passed &= (args.args[1] == 6);
634 passed &= (args.args_count == 1);
635 passed &= (args.args[0] == 9);
639 passed &= (args.args_count == 3);
640 passed &= (args.args[0] == 10);
641 passed &= (args.args[1] == 11);
642 passed &= (args.args[2] == 12);
646 passed &= (args.args_count == 2);
647 passed &= (args.args[0] == 13);
648 passed &= (args.args[1] == 14);
652 passed &= (args.args_count == 1);
653 passed &= (args.args[0] == 15);
659 selftest(passed, "index %i - data error on node %s rc=%i\n",
660 i, args.np->full_name, rc);
665 static struct of_device_id match_node_table[] = {
666 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
667 { .data = "B", .type = "type1", }, /* followed by type alone */
669 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
670 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
671 { .data = "Cc", .name = "name2", .type = "type2", },
673 { .data = "E", .compatible = "compat3" },
674 { .data = "G", .compatible = "compat2", },
675 { .data = "H", .compatible = "compat2", .name = "name5", },
676 { .data = "I", .compatible = "compat2", .type = "type1", },
677 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
678 { .data = "K", .compatible = "compat2", .name = "name9", },
685 } match_node_tests[] = {
686 { .path = "/testcase-data/match-node/name0", .data = "A", },
687 { .path = "/testcase-data/match-node/name1", .data = "B", },
688 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
689 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
690 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
691 { .path = "/testcase-data/match-node/name3", .data = "E", },
692 { .path = "/testcase-data/match-node/name4", .data = "G", },
693 { .path = "/testcase-data/match-node/name5", .data = "H", },
694 { .path = "/testcase-data/match-node/name6", .data = "G", },
695 { .path = "/testcase-data/match-node/name7", .data = "I", },
696 { .path = "/testcase-data/match-node/name8", .data = "J", },
697 { .path = "/testcase-data/match-node/name9", .data = "K", },
700 static void __init of_selftest_match_node(void)
702 struct device_node *np;
703 const struct of_device_id *match;
706 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
707 np = of_find_node_by_path(match_node_tests[i].path);
709 selftest(0, "missing testcase node %s\n",
710 match_node_tests[i].path);
714 match = of_match_node(match_node_table, np);
716 selftest(0, "%s didn't match anything\n",
717 match_node_tests[i].path);
721 if (strcmp(match->data, match_node_tests[i].data) != 0) {
722 selftest(0, "%s got wrong match. expected %s, got %s\n",
723 match_node_tests[i].path, match_node_tests[i].data,
724 (const char *)match->data);
727 selftest(1, "passed");
731 struct device test_bus = {
732 .init_name = "unittest-bus",
734 static void __init of_selftest_platform_populate(void)
737 struct device_node *np, *child, *grandchild;
738 struct platform_device *pdev;
739 struct of_device_id match[] = {
740 { .compatible = "test-device", },
744 np = of_find_node_by_path("/testcase-data");
745 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
747 /* Test that a missing irq domain returns -EPROBE_DEFER */
748 np = of_find_node_by_path("/testcase-data/testcase-device1");
749 pdev = of_find_device_by_node(np);
750 selftest(pdev, "device 1 creation failed\n");
752 irq = platform_get_irq(pdev, 0);
753 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
755 /* Test that a parsing failure does not return -EPROBE_DEFER */
756 np = of_find_node_by_path("/testcase-data/testcase-device2");
757 pdev = of_find_device_by_node(np);
758 selftest(pdev, "device 2 creation failed\n");
759 irq = platform_get_irq(pdev, 0);
760 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
762 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
763 "No testcase data in device tree\n"));
766 if (selftest(!(rc = device_register(&test_bus)),
767 "testbus registration failed; rc=%i\n", rc));
770 for_each_child_of_node(np, child) {
771 of_platform_populate(child, match, NULL, &test_bus);
772 for_each_child_of_node(child, grandchild)
773 selftest(of_find_device_by_node(grandchild),
774 "Could not create device for node '%s'\n",
778 of_platform_depopulate(&test_bus);
779 for_each_child_of_node(np, child) {
780 for_each_child_of_node(child, grandchild)
781 selftest(!of_find_device_by_node(grandchild),
782 "device didn't get destroyed '%s'\n",
786 device_unregister(&test_bus);
791 * update_node_properties - adds the properties
792 * of np into dup node (present in live tree) and
793 * updates parent of children of np to dup.
795 * @np: node already present in live tree
796 * @dup: node present in live tree to be updated
798 static void update_node_properties(struct device_node *np,
799 struct device_node *dup)
801 struct property *prop;
802 struct device_node *child;
804 for_each_property_of_node(np, prop)
805 of_add_property(dup, prop);
807 for_each_child_of_node(np, child)
812 * attach_node_and_children - attaches nodes
813 * and its children to live tree
815 * @np: Node to attach to live tree
817 static int attach_node_and_children(struct device_node *np)
819 struct device_node *next, *dup, *child;
822 dup = of_find_node_by_path(np->full_name);
824 update_node_properties(np, dup);
831 mutex_lock(&of_mutex);
832 raw_spin_lock_irqsave(&devtree_lock, flags);
833 np->sibling = np->parent->child;
834 np->parent->child = np;
835 of_node_clear_flag(np, OF_DETACHED);
836 raw_spin_unlock_irqrestore(&devtree_lock, flags);
838 __of_attach_node_sysfs(np);
839 mutex_unlock(&of_mutex);
842 next = child->sibling;
843 attach_node_and_children(child);
851 * selftest_data_add - Reads, copies data from
852 * linked tree and attaches it to the live tree
854 static int __init selftest_data_add(void)
857 struct device_node *selftest_data_node, *np;
858 extern uint8_t __dtb_testcases_begin[];
859 extern uint8_t __dtb_testcases_end[];
860 const int size = __dtb_testcases_end - __dtb_testcases_begin;
864 pr_warn("%s: No testcase data to attach; not running tests\n",
870 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
872 if (!selftest_data) {
873 pr_warn("%s: Failed to allocate memory for selftest_data; "
874 "not running tests\n", __func__);
877 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
878 if (!selftest_data_node) {
879 pr_warn("%s: No tree to attach; not running tests\n", __func__);
882 of_node_set_flag(selftest_data_node, OF_DETACHED);
883 rc = of_resolve_phandles(selftest_data_node);
885 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
890 of_root = selftest_data_node;
891 for_each_of_allnodes(np)
892 __of_attach_node_sysfs(np);
893 of_aliases = of_find_node_by_path("/aliases");
894 of_chosen = of_find_node_by_path("/chosen");
898 /* attach the sub-tree to live tree */
899 np = selftest_data_node->child;
901 struct device_node *next = np->sibling;
902 np->parent = of_root;
903 attach_node_and_children(np);
909 #ifdef CONFIG_OF_OVERLAY
911 static int selftest_probe(struct platform_device *pdev)
913 struct device *dev = &pdev->dev;
914 struct device_node *np = dev->of_node;
917 dev_err(dev, "No OF data for device\n");
922 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
926 static int selftest_remove(struct platform_device *pdev)
928 struct device *dev = &pdev->dev;
929 struct device_node *np = dev->of_node;
931 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
935 static struct of_device_id selftest_match[] = {
936 { .compatible = "selftest", },
940 static struct platform_driver selftest_driver = {
941 .probe = selftest_probe,
942 .remove = selftest_remove,
945 .owner = THIS_MODULE,
946 .of_match_table = of_match_ptr(selftest_match),
950 /* get the platform device instantiated at the path */
951 static struct platform_device *of_path_to_platform_device(const char *path)
953 struct device_node *np;
954 struct platform_device *pdev;
956 np = of_find_node_by_path(path);
960 pdev = of_find_device_by_node(np);
966 /* find out if a platform device exists at that path */
967 static int of_path_platform_device_exists(const char *path)
969 struct platform_device *pdev;
971 pdev = of_path_to_platform_device(path);
972 platform_device_put(pdev);
976 static const char *selftest_path(int nr)
978 static char buf[256];
980 snprintf(buf, sizeof(buf) - 1,
981 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr);
982 buf[sizeof(buf) - 1] = '\0';
987 static const char *overlay_path(int nr)
989 static char buf[256];
991 snprintf(buf, sizeof(buf) - 1,
992 "/testcase-data/overlay%d", nr);
993 buf[sizeof(buf) - 1] = '\0';
998 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1000 static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1003 struct device_node *np = NULL;
1006 np = of_find_node_by_path(overlay_path(overlay_nr));
1008 selftest(0, "could not find overlay node @\"%s\"\n",
1009 overlay_path(overlay_nr));
1014 ret = of_overlay_create(np);
1016 selftest(0, "could not create overlay from \"%s\"\n",
1017 overlay_path(overlay_nr));
1033 /* apply an overlay while checking before and after states */
1034 static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1035 int before, int after)
1039 /* selftest device must not be in before state */
1040 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1042 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1043 overlay_path(overlay_nr),
1044 selftest_path(selftest_nr),
1045 !before ? "enabled" : "disabled");
1049 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1051 /* of_selftest_apply_overlay already called selftest() */
1055 /* selftest device must be to set to after state */
1056 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1058 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1059 overlay_path(overlay_nr),
1060 selftest_path(selftest_nr),
1061 !after ? "enabled" : "disabled");
1068 /* apply an overlay and then revert it while checking before, after states */
1069 static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1070 int selftest_nr, int before, int after)
1074 /* selftest device must be in before state */
1075 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1077 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1078 overlay_path(overlay_nr),
1079 selftest_path(selftest_nr),
1080 !before ? "enabled" : "disabled");
1084 /* apply the overlay */
1085 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1087 /* of_selftest_apply_overlay already called selftest() */
1091 /* selftest device must be in after state */
1092 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1094 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1095 overlay_path(overlay_nr),
1096 selftest_path(selftest_nr),
1097 !after ? "enabled" : "disabled");
1101 ret = of_overlay_destroy(ov_id);
1103 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1104 overlay_path(overlay_nr),
1105 selftest_path(selftest_nr));
1109 /* selftest device must be again in before state */
1110 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1112 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1113 overlay_path(overlay_nr),
1114 selftest_path(selftest_nr),
1115 !before ? "enabled" : "disabled");
1122 /* test activation of device */
1123 static void of_selftest_overlay_0(void)
1127 /* device should enable */
1128 ret = of_selftest_apply_overlay_check(0, 0, 0, 1);
1132 selftest(1, "overlay test %d passed\n", 0);
1135 /* test deactivation of device */
1136 static void of_selftest_overlay_1(void)
1140 /* device should disable */
1141 ret = of_selftest_apply_overlay_check(1, 1, 1, 0);
1145 selftest(1, "overlay test %d passed\n", 1);
1148 /* test activation of device */
1149 static void of_selftest_overlay_2(void)
1153 /* device should enable */
1154 ret = of_selftest_apply_overlay_check(2, 2, 0, 1);
1158 selftest(1, "overlay test %d passed\n", 2);
1161 /* test deactivation of device */
1162 static void of_selftest_overlay_3(void)
1166 /* device should disable */
1167 ret = of_selftest_apply_overlay_check(3, 3, 1, 0);
1171 selftest(1, "overlay test %d passed\n", 3);
1174 /* test activation of a full device node */
1175 static void of_selftest_overlay_4(void)
1179 /* device should disable */
1180 ret = of_selftest_apply_overlay_check(4, 4, 0, 1);
1184 selftest(1, "overlay test %d passed\n", 4);
1187 /* test overlay apply/revert sequence */
1188 static void of_selftest_overlay_5(void)
1192 /* device should disable */
1193 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
1197 selftest(1, "overlay test %d passed\n", 5);
1200 /* test overlay application in sequence */
1201 static void of_selftest_overlay_6(void)
1203 struct device_node *np;
1204 int ret, i, ov_id[2];
1205 int overlay_nr = 6, selftest_nr = 6;
1206 int before = 0, after = 1;
1208 /* selftest device must be in before state */
1209 for (i = 0; i < 2; i++) {
1210 if (of_path_platform_device_exists(
1211 selftest_path(selftest_nr + i))
1213 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1214 overlay_path(overlay_nr + i),
1215 selftest_path(selftest_nr + i),
1216 !before ? "enabled" : "disabled");
1221 /* apply the overlays */
1222 for (i = 0; i < 2; i++) {
1224 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1226 selftest(0, "could not find overlay node @\"%s\"\n",
1227 overlay_path(overlay_nr + i));
1231 ret = of_overlay_create(np);
1233 selftest(0, "could not create overlay from \"%s\"\n",
1234 overlay_path(overlay_nr + i));
1240 for (i = 0; i < 2; i++) {
1241 /* selftest device must be in after state */
1242 if (of_path_platform_device_exists(
1243 selftest_path(selftest_nr + i))
1245 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1246 overlay_path(overlay_nr + i),
1247 selftest_path(selftest_nr + i),
1248 !after ? "enabled" : "disabled");
1253 for (i = 1; i >= 0; i--) {
1254 ret = of_overlay_destroy(ov_id[i]);
1256 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1257 overlay_path(overlay_nr + i),
1258 selftest_path(selftest_nr + i));
1263 for (i = 0; i < 2; i++) {
1264 /* selftest device must be again in before state */
1265 if (of_path_platform_device_exists(
1266 selftest_path(selftest_nr + i))
1268 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1269 overlay_path(overlay_nr + i),
1270 selftest_path(selftest_nr + i),
1271 !before ? "enabled" : "disabled");
1276 selftest(1, "overlay test %d passed\n", 6);
1279 /* test overlay application in sequence */
1280 static void of_selftest_overlay_8(void)
1282 struct device_node *np;
1283 int ret, i, ov_id[2];
1284 int overlay_nr = 8, selftest_nr = 8;
1286 /* we don't care about device state in this test */
1288 /* apply the overlays */
1289 for (i = 0; i < 2; i++) {
1291 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1293 selftest(0, "could not find overlay node @\"%s\"\n",
1294 overlay_path(overlay_nr + i));
1298 ret = of_overlay_create(np);
1300 selftest(0, "could not create overlay from \"%s\"\n",
1301 overlay_path(overlay_nr + i));
1307 /* now try to remove first overlay (it should fail) */
1308 ret = of_overlay_destroy(ov_id[0]);
1310 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1311 overlay_path(overlay_nr + 0),
1312 selftest_path(selftest_nr));
1316 /* removing them in order should work */
1317 for (i = 1; i >= 0; i--) {
1318 ret = of_overlay_destroy(ov_id[i]);
1320 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1321 overlay_path(overlay_nr + i),
1322 selftest_path(selftest_nr));
1327 selftest(1, "overlay test %d passed\n", 8);
1330 static void __init of_selftest_overlay(void)
1332 struct device_node *bus_np = NULL;
1335 ret = platform_driver_register(&selftest_driver);
1337 selftest(0, "could not register selftest driver\n");
1341 bus_np = of_find_node_by_path(bus_path);
1342 if (bus_np == NULL) {
1343 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1347 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1350 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1354 if (!of_path_platform_device_exists(selftest_path(100))) {
1355 selftest(0, "could not find selftest0 @ \"%s\"\n",
1356 selftest_path(100));
1360 if (of_path_platform_device_exists(selftest_path(101))) {
1361 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1362 selftest_path(101));
1366 selftest(1, "basic infrastructure of overlays passed");
1368 /* tests in sequence */
1369 of_selftest_overlay_0();
1370 of_selftest_overlay_1();
1371 of_selftest_overlay_2();
1372 of_selftest_overlay_3();
1373 of_selftest_overlay_4();
1374 of_selftest_overlay_5();
1375 of_selftest_overlay_6();
1376 of_selftest_overlay_8();
1379 of_node_put(bus_np);
1383 static inline void __init of_selftest_overlay(void) { }
1386 static int __init of_selftest(void)
1388 struct device_node *np;
1391 /* adding data for selftest */
1392 res = selftest_data_add();
1396 of_aliases = of_find_node_by_path("/aliases");
1398 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1400 pr_info("No testcase data in device tree; not running tests\n");
1405 pr_info("start of selftest - you will see error messages\n");
1406 of_selftest_check_tree_linkage();
1407 of_selftest_check_phandles();
1408 of_selftest_find_node_by_name();
1409 of_selftest_dynamic();
1410 of_selftest_parse_phandle_with_args();
1411 of_selftest_property_string();
1412 of_selftest_property_copy();
1413 of_selftest_changeset();
1414 of_selftest_parse_interrupts();
1415 of_selftest_parse_interrupts_extended();
1416 of_selftest_match_node();
1417 of_selftest_platform_populate();
1418 of_selftest_overlay();
1420 /* Double check linkage after removing testcase data */
1421 of_selftest_check_tree_linkage();
1423 pr_info("end of selftest - %i passed, %i failed\n",
1424 selftest_results.passed, selftest_results.failed);
1428 late_initcall(of_selftest);