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/libfdt.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>
22 #include <linux/i2c.h>
23 #include <linux/i2c-mux.h>
25 #include <linux/bitops.h>
27 #include "of_private.h"
29 static struct unittest_results {
34 #define unittest(result, fmt, ...) ({ \
35 bool failed = !(result); \
37 unittest_results.failed++; \
38 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
40 unittest_results.passed++; \
41 pr_debug("pass %s():%i\n", __func__, __LINE__); \
46 static void __init of_unittest_find_node_by_name(void)
48 struct device_node *np;
49 const char *options, *name;
51 np = of_find_node_by_path("/testcase-data");
52 name = kasprintf(GFP_KERNEL, "%pOF", np);
53 unittest(np && !strcmp("/testcase-data", name),
54 "find /testcase-data failed\n");
58 /* Test if trailing '/' works */
59 np = of_find_node_by_path("/testcase-data/");
60 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
62 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
63 name = kasprintf(GFP_KERNEL, "%pOF", np);
64 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
65 "find /testcase-data/phandle-tests/consumer-a failed\n");
69 np = of_find_node_by_path("testcase-alias");
70 name = kasprintf(GFP_KERNEL, "%pOF", np);
71 unittest(np && !strcmp("/testcase-data", name),
72 "find testcase-alias failed\n");
76 /* Test if trailing '/' works on aliases */
77 np = of_find_node_by_path("testcase-alias/");
78 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
80 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
81 name = kasprintf(GFP_KERNEL, "%pOF", np);
82 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
83 "find testcase-alias/phandle-tests/consumer-a failed\n");
87 np = of_find_node_by_path("/testcase-data/missing-path");
88 unittest(!np, "non-existent path returned node %pOF\n", np);
91 np = of_find_node_by_path("missing-alias");
92 unittest(!np, "non-existent alias returned node %pOF\n", np);
95 np = of_find_node_by_path("testcase-alias/missing-path");
96 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
99 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
100 unittest(np && !strcmp("testoption", options),
101 "option path test failed\n");
104 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
105 unittest(np && !strcmp("test/option", options),
106 "option path test, subcase #1 failed\n");
109 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
110 unittest(np && !strcmp("test/option", options),
111 "option path test, subcase #2 failed\n");
114 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
115 unittest(np, "NULL option path test failed\n");
118 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
120 unittest(np && !strcmp("testaliasoption", options),
121 "option alias path test failed\n");
124 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
126 unittest(np && !strcmp("test/alias/option", options),
127 "option alias path test, subcase #1 failed\n");
130 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
131 unittest(np, "NULL option alias path test failed\n");
134 options = "testoption";
135 np = of_find_node_opts_by_path("testcase-alias", &options);
136 unittest(np && !options, "option clearing test failed\n");
139 options = "testoption";
140 np = of_find_node_opts_by_path("/", &options);
141 unittest(np && !options, "option clearing root node test failed\n");
145 static void __init of_unittest_dynamic(void)
147 struct device_node *np;
148 struct property *prop;
150 np = of_find_node_by_path("/testcase-data");
152 pr_err("missing testcase data\n");
156 /* Array of 4 properties for the purpose of testing */
157 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
159 unittest(0, "kzalloc() failed\n");
163 /* Add a new property - should pass*/
164 prop->name = "new-property";
165 prop->value = "new-property-data";
166 prop->length = strlen(prop->value);
167 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
169 /* Try to add an existing property - should fail */
171 prop->name = "new-property";
172 prop->value = "new-property-data-should-fail";
173 prop->length = strlen(prop->value);
174 unittest(of_add_property(np, prop) != 0,
175 "Adding an existing property should have failed\n");
177 /* Try to modify an existing property - should pass */
178 prop->value = "modify-property-data-should-pass";
179 prop->length = strlen(prop->value);
180 unittest(of_update_property(np, prop) == 0,
181 "Updating an existing property should have passed\n");
183 /* Try to modify non-existent property - should pass*/
185 prop->name = "modify-property";
186 prop->value = "modify-missing-property-data-should-pass";
187 prop->length = strlen(prop->value);
188 unittest(of_update_property(np, prop) == 0,
189 "Updating a missing property should have passed\n");
191 /* Remove property - should pass */
192 unittest(of_remove_property(np, prop) == 0,
193 "Removing a property should have passed\n");
195 /* Adding very large property - should pass */
197 prop->name = "large-property-PAGE_SIZEx8";
198 prop->length = PAGE_SIZE * 8;
199 prop->value = kzalloc(prop->length, GFP_KERNEL);
200 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
202 unittest(of_add_property(np, prop) == 0,
203 "Adding a large property should have passed\n");
206 static int __init of_unittest_check_node_linkage(struct device_node *np)
208 struct device_node *child;
211 for_each_child_of_node(np, child) {
212 if (child->parent != np) {
213 pr_err("Child node %s links to wrong parent %s\n",
214 child->name, np->name);
219 rc = of_unittest_check_node_linkage(child);
231 static void __init of_unittest_check_tree_linkage(void)
233 struct device_node *np;
234 int allnode_count = 0, child_count;
239 for_each_of_allnodes(np)
241 child_count = of_unittest_check_node_linkage(of_root);
243 unittest(child_count > 0, "Device node data structure is corrupted\n");
244 unittest(child_count == allnode_count,
245 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
246 allnode_count, child_count);
247 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
250 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
251 const char *expected)
253 unsigned char buf[strlen(expected)+10];
256 /* Baseline; check conversion with a large size limit */
257 memset(buf, 0xff, sizeof(buf));
258 size = snprintf(buf, sizeof(buf) - 2, fmt, np);
260 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
261 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
262 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
265 /* Make sure length limits work */
267 for (i = 0; i < 2; i++, size--) {
268 /* Clear the buffer, and make sure it works correctly still */
269 memset(buf, 0xff, sizeof(buf));
270 snprintf(buf, size+1, fmt, np);
271 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
272 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
273 size, fmt, expected, buf);
277 static void __init of_unittest_printf(void)
279 struct device_node *np;
280 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
281 char phandle_str[16] = "";
283 np = of_find_node_by_path(full_name);
285 unittest(np, "testcase data missing\n");
289 num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
291 of_unittest_printf_one(np, "%pOF", full_name);
292 of_unittest_printf_one(np, "%pOFf", full_name);
293 of_unittest_printf_one(np, "%pOFp", phandle_str);
294 of_unittest_printf_one(np, "%pOFP", "dev@100");
295 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
296 of_unittest_printf_one(np, "%10pOFP", " dev@100");
297 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
298 of_unittest_printf_one(of_root, "%pOFP", "/");
299 of_unittest_printf_one(np, "%pOFF", "----");
300 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
301 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
302 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
303 of_unittest_printf_one(np, "%pOFC",
304 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
308 struct hlist_node node;
309 struct device_node *np;
312 static DEFINE_HASHTABLE(phandle_ht, 8);
313 static void __init of_unittest_check_phandles(void)
315 struct device_node *np;
316 struct node_hash *nh;
317 struct hlist_node *tmp;
318 int i, dup_count = 0, phandle_count = 0;
320 for_each_of_allnodes(np) {
324 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
325 if (nh->np->phandle == np->phandle) {
326 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
327 np->phandle, nh->np, np);
333 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
338 hash_add(phandle_ht, &nh->node, np->phandle);
341 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
342 dup_count, phandle_count);
345 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
351 static void __init of_unittest_parse_phandle_with_args(void)
353 struct device_node *np;
354 struct of_phandle_args args;
357 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
359 pr_err("missing testcase data\n");
363 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
364 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
366 for (i = 0; i < 8; i++) {
369 rc = of_parse_phandle_with_args(np, "phandle-list",
370 "#phandle-cells", i, &args);
372 /* Test the values from tests-phandle.dtsi */
376 passed &= (args.args_count == 1);
377 passed &= (args.args[0] == (i + 1));
381 passed &= (args.args_count == 2);
382 passed &= (args.args[0] == (i + 1));
383 passed &= (args.args[1] == 0);
386 passed &= (rc == -ENOENT);
390 passed &= (args.args_count == 3);
391 passed &= (args.args[0] == (i + 1));
392 passed &= (args.args[1] == 4);
393 passed &= (args.args[2] == 3);
397 passed &= (args.args_count == 2);
398 passed &= (args.args[0] == (i + 1));
399 passed &= (args.args[1] == 100);
403 passed &= (args.args_count == 0);
407 passed &= (args.args_count == 1);
408 passed &= (args.args[0] == (i + 1));
411 passed &= (rc == -ENOENT);
417 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
421 /* Check for missing list property */
422 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
423 "#phandle-cells", 0, &args);
424 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
425 rc = of_count_phandle_with_args(np, "phandle-list-missing",
427 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
429 /* Check for missing cells property */
430 rc = of_parse_phandle_with_args(np, "phandle-list",
431 "#phandle-cells-missing", 0, &args);
432 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
433 rc = of_count_phandle_with_args(np, "phandle-list",
434 "#phandle-cells-missing");
435 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
437 /* Check for bad phandle in list */
438 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
439 "#phandle-cells", 0, &args);
440 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
441 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
443 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
445 /* Check for incorrectly formed argument list */
446 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
447 "#phandle-cells", 1, &args);
448 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
449 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
451 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
454 static void __init of_unittest_property_string(void)
456 const char *strings[4];
457 struct device_node *np;
460 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
462 pr_err("No testcase data in device tree\n");
466 rc = of_property_match_string(np, "phandle-list-names", "first");
467 unittest(rc == 0, "first expected:0 got:%i\n", rc);
468 rc = of_property_match_string(np, "phandle-list-names", "second");
469 unittest(rc == 1, "second expected:1 got:%i\n", rc);
470 rc = of_property_match_string(np, "phandle-list-names", "third");
471 unittest(rc == 2, "third expected:2 got:%i\n", rc);
472 rc = of_property_match_string(np, "phandle-list-names", "fourth");
473 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
474 rc = of_property_match_string(np, "missing-property", "blah");
475 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
476 rc = of_property_match_string(np, "empty-property", "blah");
477 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
478 rc = of_property_match_string(np, "unterminated-string", "blah");
479 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
481 /* of_property_count_strings() tests */
482 rc = of_property_count_strings(np, "string-property");
483 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
484 rc = of_property_count_strings(np, "phandle-list-names");
485 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
486 rc = of_property_count_strings(np, "unterminated-string");
487 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
488 rc = of_property_count_strings(np, "unterminated-string-list");
489 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
491 /* of_property_read_string_index() tests */
492 rc = of_property_read_string_index(np, "string-property", 0, strings);
493 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
495 rc = of_property_read_string_index(np, "string-property", 1, strings);
496 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
497 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
498 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
499 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
500 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
501 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
502 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
504 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
505 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
507 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
508 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
509 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
510 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
512 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
513 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
516 /* of_property_read_string_array() tests */
517 rc = of_property_read_string_array(np, "string-property", strings, 4);
518 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
519 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
520 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
521 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
522 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
523 /* -- An incorrectly formed string should cause a failure */
524 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
525 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
526 /* -- parsing the correctly formed strings should still work: */
528 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
529 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
531 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
532 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
535 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
536 (p1)->value && (p2)->value && \
537 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
538 !strcmp((p1)->name, (p2)->name))
539 static void __init of_unittest_property_copy(void)
541 #ifdef CONFIG_OF_DYNAMIC
542 struct property p1 = { .name = "p1", .length = 0, .value = "" };
543 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
544 struct property *new;
546 new = __of_prop_dup(&p1, GFP_KERNEL);
547 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
552 new = __of_prop_dup(&p2, GFP_KERNEL);
553 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
560 static void __init of_unittest_changeset(void)
562 #ifdef CONFIG_OF_DYNAMIC
563 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
564 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
565 struct property *ppremove;
566 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
567 struct of_changeset chgset;
569 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
570 unittest(n1, "testcase setup failure\n");
571 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
572 unittest(n2, "testcase setup failure\n");
573 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
574 unittest(n21, "testcase setup failure %p\n", n21);
575 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
576 unittest(nremove, "testcase setup failure\n");
577 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
578 unittest(ppadd, "testcase setup failure\n");
579 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
580 unittest(ppupdate, "testcase setup failure\n");
581 parent = nremove->parent;
586 ppremove = of_find_property(parent, "prop-remove", NULL);
587 unittest(ppremove, "failed to find removal prop");
589 of_changeset_init(&chgset);
590 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
591 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
592 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
593 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
594 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
595 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
596 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
597 unittest(!of_changeset_apply(&chgset), "apply failed\n");
599 /* Make sure node names are constructed correctly */
600 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
601 "'%pOF' not added\n", n21);
604 unittest(!of_changeset_revert(&chgset), "revert failed\n");
606 of_changeset_destroy(&chgset);
610 static void __init of_unittest_parse_interrupts(void)
612 struct device_node *np;
613 struct of_phandle_args args;
616 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
618 pr_err("missing testcase data\n");
622 for (i = 0; i < 4; i++) {
626 rc = of_irq_parse_one(np, i, &args);
629 passed &= (args.args_count == 1);
630 passed &= (args.args[0] == (i + 1));
632 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
637 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
639 pr_err("missing testcase data\n");
643 for (i = 0; i < 4; i++) {
647 rc = of_irq_parse_one(np, i, &args);
649 /* Test the values from tests-phandle.dtsi */
653 passed &= (args.args_count == 1);
654 passed &= (args.args[0] == 9);
658 passed &= (args.args_count == 3);
659 passed &= (args.args[0] == 10);
660 passed &= (args.args[1] == 11);
661 passed &= (args.args[2] == 12);
665 passed &= (args.args_count == 2);
666 passed &= (args.args[0] == 13);
667 passed &= (args.args[1] == 14);
671 passed &= (args.args_count == 2);
672 passed &= (args.args[0] == 15);
673 passed &= (args.args[1] == 16);
678 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
684 static void __init of_unittest_parse_interrupts_extended(void)
686 struct device_node *np;
687 struct of_phandle_args args;
690 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
692 pr_err("missing testcase data\n");
696 for (i = 0; i < 7; i++) {
699 rc = of_irq_parse_one(np, i, &args);
701 /* Test the values from tests-phandle.dtsi */
705 passed &= (args.args_count == 1);
706 passed &= (args.args[0] == 1);
710 passed &= (args.args_count == 3);
711 passed &= (args.args[0] == 2);
712 passed &= (args.args[1] == 3);
713 passed &= (args.args[2] == 4);
717 passed &= (args.args_count == 2);
718 passed &= (args.args[0] == 5);
719 passed &= (args.args[1] == 6);
723 passed &= (args.args_count == 1);
724 passed &= (args.args[0] == 9);
728 passed &= (args.args_count == 3);
729 passed &= (args.args[0] == 10);
730 passed &= (args.args[1] == 11);
731 passed &= (args.args[2] == 12);
735 passed &= (args.args_count == 2);
736 passed &= (args.args[0] == 13);
737 passed &= (args.args[1] == 14);
741 passed &= (args.args_count == 1);
742 passed &= (args.args[0] == 15);
748 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
754 static const struct of_device_id match_node_table[] = {
755 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
756 { .data = "B", .type = "type1", }, /* followed by type alone */
758 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
759 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
760 { .data = "Cc", .name = "name2", .type = "type2", },
762 { .data = "E", .compatible = "compat3" },
763 { .data = "G", .compatible = "compat2", },
764 { .data = "H", .compatible = "compat2", .name = "name5", },
765 { .data = "I", .compatible = "compat2", .type = "type1", },
766 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
767 { .data = "K", .compatible = "compat2", .name = "name9", },
774 } match_node_tests[] = {
775 { .path = "/testcase-data/match-node/name0", .data = "A", },
776 { .path = "/testcase-data/match-node/name1", .data = "B", },
777 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
778 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
779 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
780 { .path = "/testcase-data/match-node/name3", .data = "E", },
781 { .path = "/testcase-data/match-node/name4", .data = "G", },
782 { .path = "/testcase-data/match-node/name5", .data = "H", },
783 { .path = "/testcase-data/match-node/name6", .data = "G", },
784 { .path = "/testcase-data/match-node/name7", .data = "I", },
785 { .path = "/testcase-data/match-node/name8", .data = "J", },
786 { .path = "/testcase-data/match-node/name9", .data = "K", },
789 static void __init of_unittest_match_node(void)
791 struct device_node *np;
792 const struct of_device_id *match;
795 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
796 np = of_find_node_by_path(match_node_tests[i].path);
798 unittest(0, "missing testcase node %s\n",
799 match_node_tests[i].path);
803 match = of_match_node(match_node_table, np);
805 unittest(0, "%s didn't match anything\n",
806 match_node_tests[i].path);
810 if (strcmp(match->data, match_node_tests[i].data) != 0) {
811 unittest(0, "%s got wrong match. expected %s, got %s\n",
812 match_node_tests[i].path, match_node_tests[i].data,
813 (const char *)match->data);
816 unittest(1, "passed");
820 static struct resource test_bus_res = {
823 .flags = IORESOURCE_MEM,
825 static const struct platform_device_info test_bus_info = {
826 .name = "unittest-bus",
828 static void __init of_unittest_platform_populate(void)
831 struct device_node *np, *child, *grandchild;
832 struct platform_device *pdev, *test_bus;
833 const struct of_device_id match[] = {
834 { .compatible = "test-device", },
838 np = of_find_node_by_path("/testcase-data");
839 of_platform_default_populate(np, NULL, NULL);
841 /* Test that a missing irq domain returns -EPROBE_DEFER */
842 np = of_find_node_by_path("/testcase-data/testcase-device1");
843 pdev = of_find_device_by_node(np);
844 unittest(pdev, "device 1 creation failed\n");
846 irq = platform_get_irq(pdev, 0);
847 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
849 /* Test that a parsing failure does not return -EPROBE_DEFER */
850 np = of_find_node_by_path("/testcase-data/testcase-device2");
851 pdev = of_find_device_by_node(np);
852 unittest(pdev, "device 2 creation failed\n");
853 irq = platform_get_irq(pdev, 0);
854 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
856 np = of_find_node_by_path("/testcase-data/platform-tests");
857 unittest(np, "No testcase data in device tree\n");
861 test_bus = platform_device_register_full(&test_bus_info);
862 rc = PTR_ERR_OR_ZERO(test_bus);
863 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
866 test_bus->dev.of_node = np;
869 * Add a dummy resource to the test bus node after it is
870 * registered to catch problems with un-inserted resources. The
871 * DT code doesn't insert the resources, and it has caused the
872 * kernel to oops in the past. This makes sure the same bug
873 * doesn't crop up again.
875 platform_device_add_resources(test_bus, &test_bus_res, 1);
877 of_platform_populate(np, match, NULL, &test_bus->dev);
878 for_each_child_of_node(np, child) {
879 for_each_child_of_node(child, grandchild)
880 unittest(of_find_device_by_node(grandchild),
881 "Could not create device for node '%s'\n",
885 of_platform_depopulate(&test_bus->dev);
886 for_each_child_of_node(np, child) {
887 for_each_child_of_node(child, grandchild)
888 unittest(!of_find_device_by_node(grandchild),
889 "device didn't get destroyed '%s'\n",
893 platform_device_unregister(test_bus);
898 * update_node_properties - adds the properties
899 * of np into dup node (present in live tree) and
900 * updates parent of children of np to dup.
902 * @np: node already present in live tree
903 * @dup: node present in live tree to be updated
905 static void update_node_properties(struct device_node *np,
906 struct device_node *dup)
908 struct property *prop;
909 struct device_node *child;
911 for_each_property_of_node(np, prop)
912 of_add_property(dup, prop);
914 for_each_child_of_node(np, child)
919 * attach_node_and_children - attaches nodes
920 * and its children to live tree
922 * @np: Node to attach to live tree
924 static int attach_node_and_children(struct device_node *np)
926 struct device_node *next, *dup, *child;
928 const char *full_name;
930 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
931 dup = of_find_node_by_path(full_name);
934 update_node_properties(np, dup);
941 mutex_lock(&of_mutex);
942 raw_spin_lock_irqsave(&devtree_lock, flags);
943 np->sibling = np->parent->child;
944 np->parent->child = np;
945 of_node_clear_flag(np, OF_DETACHED);
946 raw_spin_unlock_irqrestore(&devtree_lock, flags);
948 __of_attach_node_sysfs(np);
949 mutex_unlock(&of_mutex);
952 next = child->sibling;
953 attach_node_and_children(child);
961 * unittest_data_add - Reads, copies data from
962 * linked tree and attaches it to the live tree
964 static int __init unittest_data_add(void)
967 struct device_node *unittest_data_node, *np;
969 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
970 * created by cmd_dt_S_dtb in scripts/Makefile.lib
972 extern uint8_t __dtb_testcases_begin[];
973 extern uint8_t __dtb_testcases_end[];
974 const int size = __dtb_testcases_end - __dtb_testcases_begin;
978 pr_warn("%s: No testcase data to attach; not running tests\n",
984 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
986 if (!unittest_data) {
987 pr_warn("%s: Failed to allocate memory for unittest_data; "
988 "not running tests\n", __func__);
991 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
992 if (!unittest_data_node) {
993 pr_warn("%s: No tree to attach; not running tests\n", __func__);
996 of_node_set_flag(unittest_data_node, OF_DETACHED);
997 rc = of_resolve_phandles(unittest_data_node);
999 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1004 of_root = unittest_data_node;
1005 for_each_of_allnodes(np)
1006 __of_attach_node_sysfs(np);
1007 of_aliases = of_find_node_by_path("/aliases");
1008 of_chosen = of_find_node_by_path("/chosen");
1012 /* attach the sub-tree to live tree */
1013 np = unittest_data_node->child;
1015 struct device_node *next = np->sibling;
1017 np->parent = of_root;
1018 attach_node_and_children(np);
1024 #ifdef CONFIG_OF_OVERLAY
1026 static int unittest_probe(struct platform_device *pdev)
1028 struct device *dev = &pdev->dev;
1029 struct device_node *np = dev->of_node;
1032 dev_err(dev, "No OF data for device\n");
1037 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1039 of_platform_populate(np, NULL, NULL, &pdev->dev);
1044 static int unittest_remove(struct platform_device *pdev)
1046 struct device *dev = &pdev->dev;
1047 struct device_node *np = dev->of_node;
1049 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1053 static const struct of_device_id unittest_match[] = {
1054 { .compatible = "unittest", },
1058 static struct platform_driver unittest_driver = {
1059 .probe = unittest_probe,
1060 .remove = unittest_remove,
1063 .of_match_table = of_match_ptr(unittest_match),
1067 /* get the platform device instantiated at the path */
1068 static struct platform_device *of_path_to_platform_device(const char *path)
1070 struct device_node *np;
1071 struct platform_device *pdev;
1073 np = of_find_node_by_path(path);
1077 pdev = of_find_device_by_node(np);
1083 /* find out if a platform device exists at that path */
1084 static int of_path_platform_device_exists(const char *path)
1086 struct platform_device *pdev;
1088 pdev = of_path_to_platform_device(path);
1089 platform_device_put(pdev);
1090 return pdev != NULL;
1093 #if IS_BUILTIN(CONFIG_I2C)
1095 /* get the i2c client device instantiated at the path */
1096 static struct i2c_client *of_path_to_i2c_client(const char *path)
1098 struct device_node *np;
1099 struct i2c_client *client;
1101 np = of_find_node_by_path(path);
1105 client = of_find_i2c_device_by_node(np);
1111 /* find out if a i2c client device exists at that path */
1112 static int of_path_i2c_client_exists(const char *path)
1114 struct i2c_client *client;
1116 client = of_path_to_i2c_client(path);
1118 put_device(&client->dev);
1119 return client != NULL;
1122 static int of_path_i2c_client_exists(const char *path)
1133 static int of_path_device_type_exists(const char *path,
1134 enum overlay_type ovtype)
1138 return of_path_platform_device_exists(path);
1140 return of_path_i2c_client_exists(path);
1145 static const char *unittest_path(int nr, enum overlay_type ovtype)
1148 static char buf[256];
1152 base = "/testcase-data/overlay-node/test-bus";
1155 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1161 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1162 buf[sizeof(buf) - 1] = '\0';
1166 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1170 path = unittest_path(unittest_nr, ovtype);
1174 return of_path_platform_device_exists(path);
1176 return of_path_i2c_client_exists(path);
1181 static const char *overlay_path(int nr)
1183 static char buf[256];
1185 snprintf(buf, sizeof(buf) - 1,
1186 "/testcase-data/overlay%d", nr);
1187 buf[sizeof(buf) - 1] = '\0';
1192 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1194 /* it is guaranteed that overlay ids are assigned in sequence */
1195 #define MAX_UNITTEST_OVERLAYS 256
1196 static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1197 static int overlay_first_id = -1;
1199 static void of_unittest_track_overlay(int id)
1201 if (overlay_first_id < 0)
1202 overlay_first_id = id;
1203 id -= overlay_first_id;
1205 /* we shouldn't need that many */
1206 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1207 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1210 static void of_unittest_untrack_overlay(int id)
1212 if (overlay_first_id < 0)
1214 id -= overlay_first_id;
1215 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1216 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1219 static void of_unittest_destroy_tracked_overlays(void)
1221 int id, ret, defers;
1223 if (overlay_first_id < 0)
1226 /* try until no defers */
1229 /* remove in reverse order */
1230 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1231 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1234 ret = of_overlay_destroy(id + overlay_first_id);
1235 if (ret == -ENODEV) {
1236 pr_warn("%s: no overlay to destroy for #%d\n",
1237 __func__, id + overlay_first_id);
1242 pr_warn("%s: overlay destroy failed for #%d\n",
1243 __func__, id + overlay_first_id);
1247 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1249 } while (defers > 0);
1252 static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
1255 struct device_node *np = NULL;
1258 np = of_find_node_by_path(overlay_path(overlay_nr));
1260 unittest(0, "could not find overlay node @\"%s\"\n",
1261 overlay_path(overlay_nr));
1266 ret = of_overlay_create(np);
1268 unittest(0, "could not create overlay from \"%s\"\n",
1269 overlay_path(overlay_nr));
1273 of_unittest_track_overlay(id);
1286 /* apply an overlay while checking before and after states */
1287 static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
1288 int before, int after, enum overlay_type ovtype)
1292 /* unittest device must not be in before state */
1293 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1294 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1295 overlay_path(overlay_nr),
1296 unittest_path(unittest_nr, ovtype),
1297 !before ? "enabled" : "disabled");
1301 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, NULL);
1303 /* of_unittest_apply_overlay already called unittest() */
1307 /* unittest device must be to set to after state */
1308 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1309 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1310 overlay_path(overlay_nr),
1311 unittest_path(unittest_nr, ovtype),
1312 !after ? "enabled" : "disabled");
1319 /* apply an overlay and then revert it while checking before, after states */
1320 static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1321 int unittest_nr, int before, int after,
1322 enum overlay_type ovtype)
1326 /* unittest device must be in before state */
1327 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1328 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1329 overlay_path(overlay_nr),
1330 unittest_path(unittest_nr, ovtype),
1331 !before ? "enabled" : "disabled");
1335 /* apply the overlay */
1336 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ov_id);
1338 /* of_unittest_apply_overlay already called unittest() */
1342 /* unittest device must be in after state */
1343 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1344 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1345 overlay_path(overlay_nr),
1346 unittest_path(unittest_nr, ovtype),
1347 !after ? "enabled" : "disabled");
1351 ret = of_overlay_destroy(ov_id);
1353 unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1354 overlay_path(overlay_nr),
1355 unittest_path(unittest_nr, ovtype));
1359 /* unittest device must be again in before state */
1360 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1361 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1362 overlay_path(overlay_nr),
1363 unittest_path(unittest_nr, ovtype),
1364 !before ? "enabled" : "disabled");
1371 /* test activation of device */
1372 static void of_unittest_overlay_0(void)
1376 /* device should enable */
1377 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1381 unittest(1, "overlay test %d passed\n", 0);
1384 /* test deactivation of device */
1385 static void of_unittest_overlay_1(void)
1389 /* device should disable */
1390 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1394 unittest(1, "overlay test %d passed\n", 1);
1397 /* test activation of device */
1398 static void of_unittest_overlay_2(void)
1402 /* device should enable */
1403 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1407 unittest(1, "overlay test %d passed\n", 2);
1410 /* test deactivation of device */
1411 static void of_unittest_overlay_3(void)
1415 /* device should disable */
1416 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1420 unittest(1, "overlay test %d passed\n", 3);
1423 /* test activation of a full device node */
1424 static void of_unittest_overlay_4(void)
1428 /* device should disable */
1429 ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1433 unittest(1, "overlay test %d passed\n", 4);
1436 /* test overlay apply/revert sequence */
1437 static void of_unittest_overlay_5(void)
1441 /* device should disable */
1442 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1446 unittest(1, "overlay test %d passed\n", 5);
1449 /* test overlay application in sequence */
1450 static void of_unittest_overlay_6(void)
1452 struct device_node *np;
1453 int ret, i, ov_id[2];
1454 int overlay_nr = 6, unittest_nr = 6;
1455 int before = 0, after = 1;
1457 /* unittest device must be in before state */
1458 for (i = 0; i < 2; i++) {
1459 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1461 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1462 overlay_path(overlay_nr + i),
1463 unittest_path(unittest_nr + i,
1465 !before ? "enabled" : "disabled");
1470 /* apply the overlays */
1471 for (i = 0; i < 2; i++) {
1473 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1475 unittest(0, "could not find overlay node @\"%s\"\n",
1476 overlay_path(overlay_nr + i));
1480 ret = of_overlay_create(np);
1482 unittest(0, "could not create overlay from \"%s\"\n",
1483 overlay_path(overlay_nr + i));
1487 of_unittest_track_overlay(ov_id[i]);
1490 for (i = 0; i < 2; i++) {
1491 /* unittest device must be in after state */
1492 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1494 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1495 overlay_path(overlay_nr + i),
1496 unittest_path(unittest_nr + i,
1498 !after ? "enabled" : "disabled");
1503 for (i = 1; i >= 0; i--) {
1504 ret = of_overlay_destroy(ov_id[i]);
1506 unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1507 overlay_path(overlay_nr + i),
1508 unittest_path(unittest_nr + i,
1512 of_unittest_untrack_overlay(ov_id[i]);
1515 for (i = 0; i < 2; i++) {
1516 /* unittest device must be again in before state */
1517 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1519 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1520 overlay_path(overlay_nr + i),
1521 unittest_path(unittest_nr + i,
1523 !before ? "enabled" : "disabled");
1528 unittest(1, "overlay test %d passed\n", 6);
1531 /* test overlay application in sequence */
1532 static void of_unittest_overlay_8(void)
1534 struct device_node *np;
1535 int ret, i, ov_id[2];
1536 int overlay_nr = 8, unittest_nr = 8;
1538 /* we don't care about device state in this test */
1540 /* apply the overlays */
1541 for (i = 0; i < 2; i++) {
1543 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1545 unittest(0, "could not find overlay node @\"%s\"\n",
1546 overlay_path(overlay_nr + i));
1550 ret = of_overlay_create(np);
1552 unittest(0, "could not create overlay from \"%s\"\n",
1553 overlay_path(overlay_nr + i));
1557 of_unittest_track_overlay(ov_id[i]);
1560 /* now try to remove first overlay (it should fail) */
1561 ret = of_overlay_destroy(ov_id[0]);
1563 unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1564 overlay_path(overlay_nr + 0),
1565 unittest_path(unittest_nr,
1570 /* removing them in order should work */
1571 for (i = 1; i >= 0; i--) {
1572 ret = of_overlay_destroy(ov_id[i]);
1574 unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1575 overlay_path(overlay_nr + i),
1576 unittest_path(unittest_nr,
1580 of_unittest_untrack_overlay(ov_id[i]);
1583 unittest(1, "overlay test %d passed\n", 8);
1586 /* test insertion of a bus with parent devices */
1587 static void of_unittest_overlay_10(void)
1592 /* device should disable */
1593 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1594 if (unittest(ret == 0,
1595 "overlay test %d failed; overlay application\n", 10))
1598 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1599 unittest_path(10, PDEV_OVERLAY));
1600 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
1603 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1605 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
1609 /* test insertion of a bus with parent devices (and revert) */
1610 static void of_unittest_overlay_11(void)
1614 /* device should disable */
1615 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1617 if (unittest(ret == 0,
1618 "overlay test %d failed; overlay application\n", 11))
1622 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1624 struct unittest_i2c_bus_data {
1625 struct platform_device *pdev;
1626 struct i2c_adapter adap;
1629 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
1630 struct i2c_msg *msgs, int num)
1632 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
1639 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
1641 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1644 static const struct i2c_algorithm unittest_i2c_algo = {
1645 .master_xfer = unittest_i2c_master_xfer,
1646 .functionality = unittest_i2c_functionality,
1649 static int unittest_i2c_bus_probe(struct platform_device *pdev)
1651 struct device *dev = &pdev->dev;
1652 struct device_node *np = dev->of_node;
1653 struct unittest_i2c_bus_data *std;
1654 struct i2c_adapter *adap;
1658 dev_err(dev, "No OF data for device\n");
1663 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1665 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1667 dev_err(dev, "Failed to allocate unittest i2c data\n");
1671 /* link them together */
1673 platform_set_drvdata(pdev, std);
1676 i2c_set_adapdata(adap, std);
1678 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1679 adap->class = I2C_CLASS_DEPRECATED;
1680 adap->algo = &unittest_i2c_algo;
1681 adap->dev.parent = dev;
1682 adap->dev.of_node = dev->of_node;
1683 adap->timeout = 5 * HZ;
1686 ret = i2c_add_numbered_adapter(adap);
1688 dev_err(dev, "Failed to add I2C adapter\n");
1695 static int unittest_i2c_bus_remove(struct platform_device *pdev)
1697 struct device *dev = &pdev->dev;
1698 struct device_node *np = dev->of_node;
1699 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
1701 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1702 i2c_del_adapter(&std->adap);
1707 static const struct of_device_id unittest_i2c_bus_match[] = {
1708 { .compatible = "unittest-i2c-bus", },
1712 static struct platform_driver unittest_i2c_bus_driver = {
1713 .probe = unittest_i2c_bus_probe,
1714 .remove = unittest_i2c_bus_remove,
1716 .name = "unittest-i2c-bus",
1717 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
1721 static int unittest_i2c_dev_probe(struct i2c_client *client,
1722 const struct i2c_device_id *id)
1724 struct device *dev = &client->dev;
1725 struct device_node *np = client->dev.of_node;
1728 dev_err(dev, "No OF node\n");
1732 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1737 static int unittest_i2c_dev_remove(struct i2c_client *client)
1739 struct device *dev = &client->dev;
1740 struct device_node *np = client->dev.of_node;
1742 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1746 static const struct i2c_device_id unittest_i2c_dev_id[] = {
1747 { .name = "unittest-i2c-dev" },
1751 static struct i2c_driver unittest_i2c_dev_driver = {
1753 .name = "unittest-i2c-dev",
1755 .probe = unittest_i2c_dev_probe,
1756 .remove = unittest_i2c_dev_remove,
1757 .id_table = unittest_i2c_dev_id,
1760 #if IS_BUILTIN(CONFIG_I2C_MUX)
1762 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
1767 static int unittest_i2c_mux_probe(struct i2c_client *client,
1768 const struct i2c_device_id *id)
1771 struct device *dev = &client->dev;
1772 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1773 struct device_node *np = client->dev.of_node, *child;
1774 struct i2c_mux_core *muxc;
1777 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1780 dev_err(dev, "No OF node\n");
1785 for_each_child_of_node(np, child) {
1786 ret = of_property_read_u32(child, "reg", ®);
1789 if (max_reg == (u32)-1 || reg > max_reg)
1792 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1794 dev_err(dev, "No channels\n");
1798 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1799 unittest_i2c_mux_select_chan, NULL);
1802 for (i = 0; i < nchans; i++) {
1803 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1805 dev_err(dev, "Failed to register mux #%d\n", i);
1806 i2c_mux_del_adapters(muxc);
1811 i2c_set_clientdata(client, muxc);
1816 static int unittest_i2c_mux_remove(struct i2c_client *client)
1818 struct device *dev = &client->dev;
1819 struct device_node *np = client->dev.of_node;
1820 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
1822 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1823 i2c_mux_del_adapters(muxc);
1827 static const struct i2c_device_id unittest_i2c_mux_id[] = {
1828 { .name = "unittest-i2c-mux" },
1832 static struct i2c_driver unittest_i2c_mux_driver = {
1834 .name = "unittest-i2c-mux",
1836 .probe = unittest_i2c_mux_probe,
1837 .remove = unittest_i2c_mux_remove,
1838 .id_table = unittest_i2c_mux_id,
1843 static int of_unittest_overlay_i2c_init(void)
1847 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1848 if (unittest(ret == 0,
1849 "could not register unittest i2c device driver\n"))
1852 ret = platform_driver_register(&unittest_i2c_bus_driver);
1853 if (unittest(ret == 0,
1854 "could not register unittest i2c bus driver\n"))
1857 #if IS_BUILTIN(CONFIG_I2C_MUX)
1858 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1859 if (unittest(ret == 0,
1860 "could not register unittest i2c mux driver\n"))
1867 static void of_unittest_overlay_i2c_cleanup(void)
1869 #if IS_BUILTIN(CONFIG_I2C_MUX)
1870 i2c_del_driver(&unittest_i2c_mux_driver);
1872 platform_driver_unregister(&unittest_i2c_bus_driver);
1873 i2c_del_driver(&unittest_i2c_dev_driver);
1876 static void of_unittest_overlay_i2c_12(void)
1880 /* device should enable */
1881 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1885 unittest(1, "overlay test %d passed\n", 12);
1888 /* test deactivation of device */
1889 static void of_unittest_overlay_i2c_13(void)
1893 /* device should disable */
1894 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1898 unittest(1, "overlay test %d passed\n", 13);
1901 /* just check for i2c mux existence */
1902 static void of_unittest_overlay_i2c_14(void)
1906 static void of_unittest_overlay_i2c_15(void)
1910 /* device should enable */
1911 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
1915 unittest(1, "overlay test %d passed\n", 15);
1920 static inline void of_unittest_overlay_i2c_14(void) { }
1921 static inline void of_unittest_overlay_i2c_15(void) { }
1925 static void __init of_unittest_overlay(void)
1927 struct device_node *bus_np = NULL;
1930 ret = platform_driver_register(&unittest_driver);
1932 unittest(0, "could not register unittest driver\n");
1936 bus_np = of_find_node_by_path(bus_path);
1937 if (bus_np == NULL) {
1938 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
1942 ret = of_platform_default_populate(bus_np, NULL, NULL);
1944 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
1948 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1949 unittest(0, "could not find unittest0 @ \"%s\"\n",
1950 unittest_path(100, PDEV_OVERLAY));
1954 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1955 unittest(0, "unittest1 @ \"%s\" should not exist\n",
1956 unittest_path(101, PDEV_OVERLAY));
1960 unittest(1, "basic infrastructure of overlays passed");
1962 /* tests in sequence */
1963 of_unittest_overlay_0();
1964 of_unittest_overlay_1();
1965 of_unittest_overlay_2();
1966 of_unittest_overlay_3();
1967 of_unittest_overlay_4();
1968 of_unittest_overlay_5();
1969 of_unittest_overlay_6();
1970 of_unittest_overlay_8();
1972 of_unittest_overlay_10();
1973 of_unittest_overlay_11();
1975 #if IS_BUILTIN(CONFIG_I2C)
1976 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
1979 of_unittest_overlay_i2c_12();
1980 of_unittest_overlay_i2c_13();
1981 of_unittest_overlay_i2c_14();
1982 of_unittest_overlay_i2c_15();
1984 of_unittest_overlay_i2c_cleanup();
1987 of_unittest_destroy_tracked_overlays();
1990 of_node_put(bus_np);
1994 static inline void __init of_unittest_overlay(void) { }
1997 #ifdef CONFIG_OF_OVERLAY
2000 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2001 * in scripts/Makefile.lib
2004 #define OVERLAY_INFO_EXTERN(name) \
2005 extern uint8_t __dtb_##name##_begin[]; \
2006 extern uint8_t __dtb_##name##_end[]
2008 #define OVERLAY_INFO(name, expected) \
2009 { .dtb_begin = __dtb_##name##_begin, \
2010 .dtb_end = __dtb_##name##_end, \
2011 .expected_result = expected, \
2014 struct overlay_info {
2018 struct device_node *np_overlay;
2019 int expected_result;
2023 OVERLAY_INFO_EXTERN(overlay_base);
2024 OVERLAY_INFO_EXTERN(overlay);
2025 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2026 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2028 /* order of entries is hard-coded into users of overlays[] */
2029 static struct overlay_info overlays[] = {
2030 OVERLAY_INFO(overlay_base, -9999),
2031 OVERLAY_INFO(overlay, 0),
2032 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2033 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2037 static struct device_node *overlay_base_root;
2040 * Create base device tree for the overlay unittest.
2042 * This is called from very early boot code.
2044 * Do as much as possible the same way as done in __unflatten_device_tree
2045 * and other early boot steps for the normal FDT so that the overlay base
2046 * unflattened tree will have the same characteristics as the real tree
2047 * (such as having memory allocated by the early allocator). The goal
2048 * is to test "the real thing" as much as possible, and test "test setup
2049 * code" as little as possible.
2051 * Have to stop before resolving phandles, because that uses kmalloc.
2053 void __init unittest_unflatten_overlay_base(void)
2055 struct overlay_info *info;
2059 info = &overlays[0];
2061 if (info->expected_result != -9999) {
2062 pr_err("No dtb 'overlay_base' to attach\n");
2066 data_size = info->dtb_end - info->dtb_begin;
2068 pr_err("No dtb 'overlay_base' to attach\n");
2072 size = fdt_totalsize(info->dtb_begin);
2073 if (size != data_size) {
2074 pr_err("dtb 'overlay_base' header totalsize != actual size");
2078 info->data = early_init_dt_alloc_memory_arch(size,
2079 roundup_pow_of_two(FDT_V17_SIZE));
2081 pr_err("alloc for dtb 'overlay_base' failed");
2085 memcpy(info->data, info->dtb_begin, size);
2087 __unflatten_device_tree(info->data, NULL, &info->np_overlay,
2088 early_init_dt_alloc_memory_arch, true);
2089 overlay_base_root = info->np_overlay;
2093 * The purpose of of_unittest_overlay_data_add is to add an
2094 * overlay in the normal fashion. This is a test of the whole
2095 * picture, instead of testing individual elements.
2097 * A secondary purpose is to be able to verify that the contents of
2098 * /proc/device-tree/ contains the updated structure and values from
2099 * the overlay. That must be verified separately in user space.
2101 * Return 0 on unexpected error.
2103 static int __init overlay_data_add(int onum)
2105 struct overlay_info *info;
2109 u32 size_from_header;
2111 for (k = 0, info = overlays; info; info++, k++) {
2118 size = info->dtb_end - info->dtb_begin;
2120 pr_err("no overlay to attach, %d\n", onum);
2124 size_from_header = fdt_totalsize(info->dtb_begin);
2125 if (size_from_header != size) {
2126 pr_err("overlay header totalsize != actual size, %d", onum);
2131 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
2132 * will create pointers to the passed in FDT in the EDT.
2134 info->data = kmemdup(info->dtb_begin, size, GFP_KERNEL);
2136 pr_err("unable to allocate memory for data, %d\n", onum);
2140 of_fdt_unflatten_tree(info->data, NULL, &info->np_overlay);
2141 if (!info->np_overlay) {
2142 pr_err("unable to unflatten overlay, %d\n", onum);
2146 of_node_set_flag(info->np_overlay, OF_DETACHED);
2148 ret = of_resolve_phandles(info->np_overlay);
2150 pr_err("resolve ot phandles (ret=%d), %d\n", ret, onum);
2151 goto out_free_np_overlay;
2154 ret = of_overlay_create(info->np_overlay);
2156 pr_err("of_overlay_create() (ret=%d), %d\n", ret, onum);
2157 goto out_free_np_overlay;
2159 info->overlay_id = ret;
2163 pr_debug("__dtb_overlay_begin applied, overlay id %d\n", ret);
2167 out_free_np_overlay:
2169 * info->np_overlay is the unflattened device tree
2170 * It has not been spliced into the live tree.
2173 /* todo: function to free unflattened device tree */
2179 return (ret == info->expected_result);
2183 * The purpose of of_unittest_overlay_high_level is to add an overlay
2184 * in the normal fashion. This is a test of the whole picture,
2185 * instead of individual elements.
2187 * The first part of the function is _not_ normal overlay usage; it is
2188 * finishing splicing the base overlay device tree into the live tree.
2190 static __init void of_unittest_overlay_high_level(void)
2192 struct device_node *last_sibling;
2193 struct device_node *np;
2194 struct device_node *of_symbols;
2195 struct device_node *overlay_base_symbols;
2196 struct device_node **pprev;
2197 struct property *prop;
2200 if (!overlay_base_root) {
2201 unittest(0, "overlay_base_root not initialized\n");
2206 * Could not fixup phandles in unittest_unflatten_overlay_base()
2207 * because kmalloc() was not yet available.
2209 of_resolve_phandles(overlay_base_root);
2212 * do not allow overlay_base to duplicate any node already in
2213 * tree, this greatly simplifies the code
2217 * remove overlay_base_root node "__local_fixups", after
2218 * being used by of_resolve_phandles()
2220 pprev = &overlay_base_root->child;
2221 for (np = overlay_base_root->child; np; np = np->sibling) {
2222 if (!of_node_cmp(np->name, "__local_fixups__")) {
2223 *pprev = np->sibling;
2226 pprev = &np->sibling;
2229 /* remove overlay_base_root node "__symbols__" if in live tree */
2230 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2232 /* will have to graft properties from node into live tree */
2233 pprev = &overlay_base_root->child;
2234 for (np = overlay_base_root->child; np; np = np->sibling) {
2235 if (!of_node_cmp(np->name, "__symbols__")) {
2236 overlay_base_symbols = np;
2237 *pprev = np->sibling;
2240 pprev = &np->sibling;
2244 for (np = overlay_base_root->child; np; np = np->sibling) {
2245 if (of_get_child_by_name(of_root, np->name)) {
2246 unittest(0, "illegal node name in overlay_base %s",
2253 * overlay 'overlay_base' is not allowed to have root
2254 * properties, so only need to splice nodes into main device tree.
2256 * root node of *overlay_base_root will not be freed, it is lost
2260 for (np = overlay_base_root->child; np; np = np->sibling)
2261 np->parent = of_root;
2263 mutex_lock(&of_mutex);
2265 for (last_sibling = np = of_root->child; np; np = np->sibling)
2269 last_sibling->sibling = overlay_base_root->child;
2271 of_root->child = overlay_base_root->child;
2273 for_each_of_allnodes_from(overlay_base_root, np)
2274 __of_attach_node_sysfs(np);
2277 for_each_property_of_node(overlay_base_symbols, prop) {
2278 ret = __of_add_property(of_symbols, prop);
2281 "duplicate property '%s' in overlay_base node __symbols__",
2285 ret = __of_add_property_sysfs(of_symbols, prop);
2288 "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2295 mutex_unlock(&of_mutex);
2298 /* now do the normal overlay usage test */
2300 unittest(overlay_data_add(1),
2301 "Adding overlay 'overlay' failed\n");
2303 unittest(overlay_data_add(2),
2304 "Adding overlay 'overlay_bad_phandle' failed\n");
2306 unittest(overlay_data_add(3),
2307 "Adding overlay 'overlay_bad_symbol' failed\n");
2312 mutex_unlock(&of_mutex);
2317 static inline __init void of_unittest_overlay_high_level(void) {}
2321 static int __init of_unittest(void)
2323 struct device_node *np;
2326 /* adding data for unittest */
2327 res = unittest_data_add();
2331 of_aliases = of_find_node_by_path("/aliases");
2333 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2335 pr_info("No testcase data in device tree; not running tests\n");
2340 pr_info("start of unittest - you will see error messages\n");
2341 of_unittest_check_tree_linkage();
2342 of_unittest_check_phandles();
2343 of_unittest_find_node_by_name();
2344 of_unittest_dynamic();
2345 of_unittest_parse_phandle_with_args();
2346 of_unittest_printf();
2347 of_unittest_property_string();
2348 of_unittest_property_copy();
2349 of_unittest_changeset();
2350 of_unittest_parse_interrupts();
2351 of_unittest_parse_interrupts_extended();
2352 of_unittest_match_node();
2353 of_unittest_platform_populate();
2354 of_unittest_overlay();
2356 /* Double check linkage after removing testcase data */
2357 of_unittest_check_tree_linkage();
2359 of_unittest_overlay_high_level();
2361 pr_info("end of unittest - %i passed, %i failed\n",
2362 unittest_results.passed, unittest_results.failed);
2366 late_initcall(of_unittest);