1 // SPDX-License-Identifier: GPL-2.0
3 * Self tests for device tree subsystem
6 #define pr_fmt(fmt) "### dt-test ### " fmt
8 #include <linux/memblock.h>
10 #include <linux/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/hashtable.h>
14 #include <linux/libfdt.h>
16 #include <linux/of_address.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-mux.h>
28 #include <linux/gpio/driver.h>
30 #include <linux/bitops.h>
32 #include "of_private.h"
34 static struct unittest_results {
39 #define unittest(result, fmt, ...) ({ \
40 bool failed = !(result); \
42 unittest_results.failed++; \
43 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
45 unittest_results.passed++; \
46 pr_debug("pass %s():%i\n", __func__, __LINE__); \
52 #define OF_KREF_READ(NODE) kref_read(&(NODE)->kobj.kref)
54 #define OF_KREF_READ(NODE) 1
58 * Expected message may have a message level other than KERN_INFO.
59 * Print the expected message only if the current loglevel will allow
60 * the actual message to print.
62 * Do not use EXPECT_BEGIN() or EXPECT_END() for messages generated by
65 #define EXPECT_BEGIN(level, fmt, ...) \
66 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
68 #define EXPECT_END(level, fmt, ...) \
69 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
71 static void __init of_unittest_find_node_by_name(void)
73 struct device_node *np;
74 const char *options, *name;
76 np = of_find_node_by_path("/testcase-data");
77 name = kasprintf(GFP_KERNEL, "%pOF", np);
78 unittest(np && name && !strcmp("/testcase-data", name),
79 "find /testcase-data failed\n");
83 /* Test if trailing '/' works */
84 np = of_find_node_by_path("/testcase-data/");
85 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
87 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
88 name = kasprintf(GFP_KERNEL, "%pOF", np);
89 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
90 "find /testcase-data/phandle-tests/consumer-a failed\n");
94 np = of_find_node_by_path("testcase-alias");
95 name = kasprintf(GFP_KERNEL, "%pOF", np);
96 unittest(np && name && !strcmp("/testcase-data", name),
97 "find testcase-alias failed\n");
101 /* Test if trailing '/' works on aliases */
102 np = of_find_node_by_path("testcase-alias/");
103 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
105 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
106 name = kasprintf(GFP_KERNEL, "%pOF", np);
107 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
108 "find testcase-alias/phandle-tests/consumer-a failed\n");
112 np = of_find_node_by_path("/testcase-data/missing-path");
113 unittest(!np, "non-existent path returned node %pOF\n", np);
116 np = of_find_node_by_path("missing-alias");
117 unittest(!np, "non-existent alias returned node %pOF\n", np);
120 np = of_find_node_by_path("testcase-alias/missing-path");
121 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
124 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
125 unittest(np && !strcmp("testoption", options),
126 "option path test failed\n");
129 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
130 unittest(np && !strcmp("test/option", options),
131 "option path test, subcase #1 failed\n");
134 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
135 unittest(np && !strcmp("test/option", options),
136 "option path test, subcase #2 failed\n");
139 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
140 unittest(np, "NULL option path test failed\n");
143 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
145 unittest(np && !strcmp("testaliasoption", options),
146 "option alias path test failed\n");
149 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
151 unittest(np && !strcmp("test/alias/option", options),
152 "option alias path test, subcase #1 failed\n");
155 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
156 unittest(np, "NULL option alias path test failed\n");
159 options = "testoption";
160 np = of_find_node_opts_by_path("testcase-alias", &options);
161 unittest(np && !options, "option clearing test failed\n");
164 options = "testoption";
165 np = of_find_node_opts_by_path("/", &options);
166 unittest(np && !options, "option clearing root node test failed\n");
170 static void __init of_unittest_dynamic(void)
172 struct device_node *np;
173 struct property *prop;
175 np = of_find_node_by_path("/testcase-data");
177 pr_err("missing testcase data\n");
181 /* Array of 4 properties for the purpose of testing */
182 prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
184 unittest(0, "kzalloc() failed\n");
188 /* Add a new property - should pass*/
189 prop->name = "new-property";
190 prop->value = "new-property-data";
191 prop->length = strlen(prop->value) + 1;
192 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
194 /* Try to add an existing property - should fail */
196 prop->name = "new-property";
197 prop->value = "new-property-data-should-fail";
198 prop->length = strlen(prop->value) + 1;
199 unittest(of_add_property(np, prop) != 0,
200 "Adding an existing property should have failed\n");
202 /* Try to modify an existing property - should pass */
203 prop->value = "modify-property-data-should-pass";
204 prop->length = strlen(prop->value) + 1;
205 unittest(of_update_property(np, prop) == 0,
206 "Updating an existing property should have passed\n");
208 /* Try to modify non-existent property - should pass*/
210 prop->name = "modify-property";
211 prop->value = "modify-missing-property-data-should-pass";
212 prop->length = strlen(prop->value) + 1;
213 unittest(of_update_property(np, prop) == 0,
214 "Updating a missing property should have passed\n");
216 /* Remove property - should pass */
217 unittest(of_remove_property(np, prop) == 0,
218 "Removing a property should have passed\n");
220 /* Adding very large property - should pass */
222 prop->name = "large-property-PAGE_SIZEx8";
223 prop->length = PAGE_SIZE * 8;
224 prop->value = kzalloc(prop->length, GFP_KERNEL);
225 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
227 unittest(of_add_property(np, prop) == 0,
228 "Adding a large property should have passed\n");
231 static int __init of_unittest_check_node_linkage(struct device_node *np)
233 struct device_node *child;
236 for_each_child_of_node(np, child) {
237 if (child->parent != np) {
238 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
244 rc = of_unittest_check_node_linkage(child);
256 static void __init of_unittest_check_tree_linkage(void)
258 struct device_node *np;
259 int allnode_count = 0, child_count;
264 for_each_of_allnodes(np)
266 child_count = of_unittest_check_node_linkage(of_root);
268 unittest(child_count > 0, "Device node data structure is corrupted\n");
269 unittest(child_count == allnode_count,
270 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
271 allnode_count, child_count);
272 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
275 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
276 const char *expected)
282 buf_size = strlen(expected) + 10;
283 buf = kmalloc(buf_size, GFP_KERNEL);
287 /* Baseline; check conversion with a large size limit */
288 memset(buf, 0xff, buf_size);
289 size = snprintf(buf, buf_size - 2, fmt, np);
291 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
292 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
293 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
296 /* Make sure length limits work */
298 for (i = 0; i < 2; i++, size--) {
299 /* Clear the buffer, and make sure it works correctly still */
300 memset(buf, 0xff, buf_size);
301 snprintf(buf, size+1, fmt, np);
302 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
303 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
304 size, fmt, expected, buf);
309 static void __init of_unittest_printf(void)
311 struct device_node *np;
312 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
313 char phandle_str[16] = "";
315 np = of_find_node_by_path(full_name);
317 unittest(np, "testcase data missing\n");
321 num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
323 of_unittest_printf_one(np, "%pOF", full_name);
324 of_unittest_printf_one(np, "%pOFf", full_name);
325 of_unittest_printf_one(np, "%pOFn", "dev");
326 of_unittest_printf_one(np, "%2pOFn", "dev");
327 of_unittest_printf_one(np, "%5pOFn", " dev");
328 of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
329 of_unittest_printf_one(np, "%pOFp", phandle_str);
330 of_unittest_printf_one(np, "%pOFP", "dev@100");
331 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
332 of_unittest_printf_one(np, "%10pOFP", " dev@100");
333 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
334 of_unittest_printf_one(of_root, "%pOFP", "/");
335 of_unittest_printf_one(np, "%pOFF", "----");
336 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
337 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
338 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
339 of_unittest_printf_one(np, "%pOFC",
340 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
344 struct hlist_node node;
345 struct device_node *np;
348 static DEFINE_HASHTABLE(phandle_ht, 8);
349 static void __init of_unittest_check_phandles(void)
351 struct device_node *np;
352 struct node_hash *nh;
353 struct hlist_node *tmp;
354 int i, dup_count = 0, phandle_count = 0;
356 for_each_of_allnodes(np) {
360 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
361 if (nh->np->phandle == np->phandle) {
362 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
363 np->phandle, nh->np, np);
369 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
374 hash_add(phandle_ht, &nh->node, np->phandle);
377 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
378 dup_count, phandle_count);
381 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
387 static void __init of_unittest_parse_phandle_with_args(void)
389 struct device_node *np;
390 struct of_phandle_args args;
393 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
395 pr_err("missing testcase data\n");
399 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
400 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
402 for (i = 0; i < 8; i++) {
405 memset(&args, 0, sizeof(args));
406 rc = of_parse_phandle_with_args(np, "phandle-list",
407 "#phandle-cells", i, &args);
409 /* Test the values from tests-phandle.dtsi */
413 passed &= (args.args_count == 1);
414 passed &= (args.args[0] == (i + 1));
418 passed &= (args.args_count == 2);
419 passed &= (args.args[0] == (i + 1));
420 passed &= (args.args[1] == 0);
423 passed &= (rc == -ENOENT);
427 passed &= (args.args_count == 3);
428 passed &= (args.args[0] == (i + 1));
429 passed &= (args.args[1] == 4);
430 passed &= (args.args[2] == 3);
434 passed &= (args.args_count == 2);
435 passed &= (args.args[0] == (i + 1));
436 passed &= (args.args[1] == 100);
440 passed &= (args.args_count == 0);
444 passed &= (args.args_count == 1);
445 passed &= (args.args[0] == (i + 1));
448 passed &= (rc == -ENOENT);
454 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
458 of_node_put(args.np);
461 /* Check for missing list property */
462 memset(&args, 0, sizeof(args));
463 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
464 "#phandle-cells", 0, &args);
465 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
466 rc = of_count_phandle_with_args(np, "phandle-list-missing",
468 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
470 /* Check for missing cells property */
471 memset(&args, 0, sizeof(args));
473 EXPECT_BEGIN(KERN_INFO,
474 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
476 rc = of_parse_phandle_with_args(np, "phandle-list",
477 "#phandle-cells-missing", 0, &args);
479 EXPECT_END(KERN_INFO,
480 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
482 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
484 EXPECT_BEGIN(KERN_INFO,
485 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
487 rc = of_count_phandle_with_args(np, "phandle-list",
488 "#phandle-cells-missing");
490 EXPECT_END(KERN_INFO,
491 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
493 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
495 /* Check for bad phandle in list */
496 memset(&args, 0, sizeof(args));
498 EXPECT_BEGIN(KERN_INFO,
499 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
501 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
502 "#phandle-cells", 0, &args);
504 EXPECT_END(KERN_INFO,
505 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
507 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
509 EXPECT_BEGIN(KERN_INFO,
510 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
512 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
515 EXPECT_END(KERN_INFO,
516 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
518 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
520 /* Check for incorrectly formed argument list */
521 memset(&args, 0, sizeof(args));
523 EXPECT_BEGIN(KERN_INFO,
524 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
526 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
527 "#phandle-cells", 1, &args);
529 EXPECT_END(KERN_INFO,
530 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
532 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
534 EXPECT_BEGIN(KERN_INFO,
535 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
537 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
540 EXPECT_END(KERN_INFO,
541 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
543 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
546 static void __init of_unittest_parse_phandle_with_args_map(void)
548 struct device_node *np, *p[6] = {};
549 struct of_phandle_args args;
550 unsigned int prefs[6];
553 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
555 pr_err("missing testcase data\n");
559 p[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
560 p[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
561 p[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
562 p[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
563 p[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
564 p[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
565 for (i = 0; i < ARRAY_SIZE(p); ++i) {
567 pr_err("missing testcase data\n");
570 prefs[i] = OF_KREF_READ(p[i]);
573 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
574 unittest(rc == 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc);
576 for (i = 0; i < 9; i++) {
579 memset(&args, 0, sizeof(args));
580 rc = of_parse_phandle_with_args_map(np, "phandle-list",
581 "phandle", i, &args);
583 /* Test the values from tests-phandle.dtsi */
587 passed &= (args.np == p[1]);
588 passed &= (args.args_count == 1);
589 passed &= (args.args[0] == 1);
593 passed &= (args.np == p[3]);
594 passed &= (args.args_count == 3);
595 passed &= (args.args[0] == 2);
596 passed &= (args.args[1] == 5);
597 passed &= (args.args[2] == 3);
600 passed &= (rc == -ENOENT);
604 passed &= (args.np == p[0]);
605 passed &= (args.args_count == 0);
609 passed &= (args.np == p[1]);
610 passed &= (args.args_count == 1);
611 passed &= (args.args[0] == 3);
615 passed &= (args.np == p[0]);
616 passed &= (args.args_count == 0);
620 passed &= (args.np == p[2]);
621 passed &= (args.args_count == 2);
622 passed &= (args.args[0] == 15);
623 passed &= (args.args[1] == 0x20);
627 passed &= (args.np == p[3]);
628 passed &= (args.args_count == 3);
629 passed &= (args.args[0] == 2);
630 passed &= (args.args[1] == 5);
631 passed &= (args.args[2] == 3);
634 passed &= (rc == -ENOENT);
640 unittest(passed, "index %i - data error on node %s rc=%i\n",
641 i, args.np->full_name, rc);
644 of_node_put(args.np);
647 /* Check for missing list property */
648 memset(&args, 0, sizeof(args));
649 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
650 "phandle", 0, &args);
651 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
653 /* Check for missing cells,map,mask property */
654 memset(&args, 0, sizeof(args));
656 EXPECT_BEGIN(KERN_INFO,
657 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
659 rc = of_parse_phandle_with_args_map(np, "phandle-list",
660 "phandle-missing", 0, &args);
661 EXPECT_END(KERN_INFO,
662 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
664 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
666 /* Check for bad phandle in list */
667 memset(&args, 0, sizeof(args));
669 EXPECT_BEGIN(KERN_INFO,
670 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
672 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
673 "phandle", 0, &args);
674 EXPECT_END(KERN_INFO,
675 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
677 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
679 /* Check for incorrectly formed argument list */
680 memset(&args, 0, sizeof(args));
682 EXPECT_BEGIN(KERN_INFO,
683 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
685 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
686 "phandle", 1, &args);
687 EXPECT_END(KERN_INFO,
688 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
690 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
692 for (i = 0; i < ARRAY_SIZE(p); ++i) {
693 unittest(prefs[i] == OF_KREF_READ(p[i]),
694 "provider%d: expected:%d got:%d\n",
695 i, prefs[i], OF_KREF_READ(p[i]));
700 static void __init of_unittest_property_string(void)
702 const char *strings[4];
703 struct device_node *np;
706 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
708 pr_err("No testcase data in device tree\n");
712 rc = of_property_match_string(np, "phandle-list-names", "first");
713 unittest(rc == 0, "first expected:0 got:%i\n", rc);
714 rc = of_property_match_string(np, "phandle-list-names", "second");
715 unittest(rc == 1, "second expected:1 got:%i\n", rc);
716 rc = of_property_match_string(np, "phandle-list-names", "third");
717 unittest(rc == 2, "third expected:2 got:%i\n", rc);
718 rc = of_property_match_string(np, "phandle-list-names", "fourth");
719 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
720 rc = of_property_match_string(np, "missing-property", "blah");
721 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
722 rc = of_property_match_string(np, "empty-property", "blah");
723 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
724 rc = of_property_match_string(np, "unterminated-string", "blah");
725 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
727 /* of_property_count_strings() tests */
728 rc = of_property_count_strings(np, "string-property");
729 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
730 rc = of_property_count_strings(np, "phandle-list-names");
731 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
732 rc = of_property_count_strings(np, "unterminated-string");
733 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
734 rc = of_property_count_strings(np, "unterminated-string-list");
735 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
737 /* of_property_read_string_index() tests */
738 rc = of_property_read_string_index(np, "string-property", 0, strings);
739 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
741 rc = of_property_read_string_index(np, "string-property", 1, strings);
742 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
743 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
744 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
745 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
746 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
747 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
748 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
750 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
751 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
753 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
754 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
755 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
756 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
758 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
759 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
762 /* of_property_read_string_array() tests */
763 rc = of_property_read_string_array(np, "string-property", strings, 4);
764 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
765 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
766 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
767 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
768 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
769 /* -- An incorrectly formed string should cause a failure */
770 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
771 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
772 /* -- parsing the correctly formed strings should still work: */
774 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
775 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
777 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
778 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
781 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
782 (p1)->value && (p2)->value && \
783 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
784 !strcmp((p1)->name, (p2)->name))
785 static void __init of_unittest_property_copy(void)
787 #ifdef CONFIG_OF_DYNAMIC
788 struct property p1 = { .name = "p1", .length = 0, .value = "" };
789 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
790 struct property *new;
792 new = __of_prop_dup(&p1, GFP_KERNEL);
793 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
798 new = __of_prop_dup(&p2, GFP_KERNEL);
799 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
806 static void __init of_unittest_changeset(void)
808 #ifdef CONFIG_OF_DYNAMIC
809 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
810 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
811 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
812 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
813 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
814 struct property *ppremove;
815 struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
816 struct of_changeset chgset;
818 n1 = __of_node_dup(NULL, "n1");
819 unittest(n1, "testcase setup failure\n");
821 n2 = __of_node_dup(NULL, "n2");
822 unittest(n2, "testcase setup failure\n");
824 n21 = __of_node_dup(NULL, "n21");
825 unittest(n21, "testcase setup failure %p\n", n21);
827 nchangeset = of_find_node_by_path("/testcase-data/changeset");
828 nremove = of_get_child_by_name(nchangeset, "node-remove");
829 unittest(nremove, "testcase setup failure\n");
831 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
832 unittest(ppadd, "testcase setup failure\n");
834 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
835 unittest(ppname_n1, "testcase setup failure\n");
837 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
838 unittest(ppname_n2, "testcase setup failure\n");
840 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
841 unittest(ppname_n21, "testcase setup failure\n");
843 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
844 unittest(ppupdate, "testcase setup failure\n");
851 ppremove = of_find_property(parent, "prop-remove", NULL);
852 unittest(ppremove, "failed to find removal prop");
854 of_changeset_init(&chgset);
856 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
857 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
859 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
860 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
862 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
863 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
865 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
867 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
868 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
869 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
871 unittest(!of_changeset_apply(&chgset), "apply failed\n");
873 of_node_put(nchangeset);
875 /* Make sure node names are constructed correctly */
876 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
877 "'%pOF' not added\n", n21);
880 unittest(!of_changeset_revert(&chgset), "revert failed\n");
882 of_changeset_destroy(&chgset);
890 static void __init of_unittest_dma_get_max_cpu_address(void)
892 struct device_node *np;
893 phys_addr_t cpu_addr;
895 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
898 np = of_find_node_by_path("/testcase-data/address-tests");
900 pr_err("missing testcase data\n");
904 cpu_addr = of_dma_get_max_cpu_address(np);
905 unittest(cpu_addr == 0x4fffffff,
906 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
907 &cpu_addr, 0x4fffffff);
910 static void __init of_unittest_dma_ranges_one(const char *path,
911 u64 expect_dma_addr, u64 expect_paddr)
913 #ifdef CONFIG_HAS_DMA
914 struct device_node *np;
915 const struct bus_dma_region *map = NULL;
918 np = of_find_node_by_path(path);
920 pr_err("missing testcase data\n");
924 rc = of_dma_get_range(np, &map);
926 unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
931 struct device *dev_bogus;
933 dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
935 unittest(0, "kzalloc() failed\n");
940 dev_bogus->dma_range_map = map;
941 paddr = dma_to_phys(dev_bogus, expect_dma_addr);
942 dma_addr = phys_to_dma(dev_bogus, expect_paddr);
944 unittest(paddr == expect_paddr,
945 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
946 &paddr, expect_paddr, np);
947 unittest(dma_addr == expect_dma_addr,
948 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
949 &dma_addr, expect_dma_addr, np);
958 static void __init of_unittest_parse_dma_ranges(void)
960 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
962 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
963 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
964 0x100000000, 0x20000000);
965 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
966 0x80000000, 0x20000000);
969 static void __init of_unittest_pci_dma_ranges(void)
971 struct device_node *np;
972 struct of_pci_range range;
973 struct of_pci_range_parser parser;
976 if (!IS_ENABLED(CONFIG_PCI))
979 np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
981 pr_err("missing testcase data\n");
985 if (of_pci_dma_range_parser_init(&parser, np)) {
986 pr_err("missing dma-ranges property\n");
991 * Get the dma-ranges from the device tree
993 for_each_of_pci_range(&parser, &range) {
995 unittest(range.size == 0x10000000,
996 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
998 unittest(range.cpu_addr == 0x20000000,
999 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1000 range.cpu_addr, np);
1001 unittest(range.pci_addr == 0x80000000,
1002 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1003 range.pci_addr, np);
1005 unittest(range.size == 0x10000000,
1006 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1008 unittest(range.cpu_addr == 0x40000000,
1009 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1010 range.cpu_addr, np);
1011 unittest(range.pci_addr == 0xc0000000,
1012 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1013 range.pci_addr, np);
1021 static void __init of_unittest_parse_interrupts(void)
1023 struct device_node *np;
1024 struct of_phandle_args args;
1027 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1030 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1032 pr_err("missing testcase data\n");
1036 for (i = 0; i < 4; i++) {
1039 memset(&args, 0, sizeof(args));
1040 rc = of_irq_parse_one(np, i, &args);
1043 passed &= (args.args_count == 1);
1044 passed &= (args.args[0] == (i + 1));
1046 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1051 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1053 pr_err("missing testcase data\n");
1057 for (i = 0; i < 4; i++) {
1060 memset(&args, 0, sizeof(args));
1061 rc = of_irq_parse_one(np, i, &args);
1063 /* Test the values from tests-phandle.dtsi */
1067 passed &= (args.args_count == 1);
1068 passed &= (args.args[0] == 9);
1072 passed &= (args.args_count == 3);
1073 passed &= (args.args[0] == 10);
1074 passed &= (args.args[1] == 11);
1075 passed &= (args.args[2] == 12);
1079 passed &= (args.args_count == 2);
1080 passed &= (args.args[0] == 13);
1081 passed &= (args.args[1] == 14);
1085 passed &= (args.args_count == 2);
1086 passed &= (args.args[0] == 15);
1087 passed &= (args.args[1] == 16);
1092 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1098 static void __init of_unittest_parse_interrupts_extended(void)
1100 struct device_node *np;
1101 struct of_phandle_args args;
1104 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1107 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1109 pr_err("missing testcase data\n");
1113 for (i = 0; i < 7; i++) {
1116 memset(&args, 0, sizeof(args));
1117 rc = of_irq_parse_one(np, i, &args);
1119 /* Test the values from tests-phandle.dtsi */
1123 passed &= (args.args_count == 1);
1124 passed &= (args.args[0] == 1);
1128 passed &= (args.args_count == 3);
1129 passed &= (args.args[0] == 2);
1130 passed &= (args.args[1] == 3);
1131 passed &= (args.args[2] == 4);
1135 passed &= (args.args_count == 2);
1136 passed &= (args.args[0] == 5);
1137 passed &= (args.args[1] == 6);
1141 passed &= (args.args_count == 1);
1142 passed &= (args.args[0] == 9);
1146 passed &= (args.args_count == 3);
1147 passed &= (args.args[0] == 10);
1148 passed &= (args.args[1] == 11);
1149 passed &= (args.args[2] == 12);
1153 passed &= (args.args_count == 2);
1154 passed &= (args.args[0] == 13);
1155 passed &= (args.args[1] == 14);
1159 passed &= (args.args_count == 1);
1160 passed &= (args.args[0] == 15);
1166 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1172 static const struct of_device_id match_node_table[] = {
1173 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1174 { .data = "B", .type = "type1", }, /* followed by type alone */
1176 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1177 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1178 { .data = "Cc", .name = "name2", .type = "type2", },
1180 { .data = "E", .compatible = "compat3" },
1181 { .data = "G", .compatible = "compat2", },
1182 { .data = "H", .compatible = "compat2", .name = "name5", },
1183 { .data = "I", .compatible = "compat2", .type = "type1", },
1184 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1185 { .data = "K", .compatible = "compat2", .name = "name9", },
1192 } match_node_tests[] = {
1193 { .path = "/testcase-data/match-node/name0", .data = "A", },
1194 { .path = "/testcase-data/match-node/name1", .data = "B", },
1195 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1196 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1197 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1198 { .path = "/testcase-data/match-node/name3", .data = "E", },
1199 { .path = "/testcase-data/match-node/name4", .data = "G", },
1200 { .path = "/testcase-data/match-node/name5", .data = "H", },
1201 { .path = "/testcase-data/match-node/name6", .data = "G", },
1202 { .path = "/testcase-data/match-node/name7", .data = "I", },
1203 { .path = "/testcase-data/match-node/name8", .data = "J", },
1204 { .path = "/testcase-data/match-node/name9", .data = "K", },
1207 static void __init of_unittest_match_node(void)
1209 struct device_node *np;
1210 const struct of_device_id *match;
1213 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1214 np = of_find_node_by_path(match_node_tests[i].path);
1216 unittest(0, "missing testcase node %s\n",
1217 match_node_tests[i].path);
1221 match = of_match_node(match_node_table, np);
1223 unittest(0, "%s didn't match anything\n",
1224 match_node_tests[i].path);
1228 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1229 unittest(0, "%s got wrong match. expected %s, got %s\n",
1230 match_node_tests[i].path, match_node_tests[i].data,
1231 (const char *)match->data);
1234 unittest(1, "passed");
1238 static struct resource test_bus_res = {
1239 .start = 0xfffffff8,
1241 .flags = IORESOURCE_MEM,
1243 static const struct platform_device_info test_bus_info = {
1244 .name = "unittest-bus",
1246 static void __init of_unittest_platform_populate(void)
1249 struct device_node *np, *child, *grandchild;
1250 struct platform_device *pdev, *test_bus;
1251 const struct of_device_id match[] = {
1252 { .compatible = "test-device", },
1256 np = of_find_node_by_path("/testcase-data");
1257 of_platform_default_populate(np, NULL, NULL);
1259 /* Test that a missing irq domain returns -EPROBE_DEFER */
1260 np = of_find_node_by_path("/testcase-data/testcase-device1");
1261 pdev = of_find_device_by_node(np);
1262 unittest(pdev, "device 1 creation failed\n");
1264 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1265 irq = platform_get_irq(pdev, 0);
1266 unittest(irq == -EPROBE_DEFER,
1267 "device deferred probe failed - %d\n", irq);
1269 /* Test that a parsing failure does not return -EPROBE_DEFER */
1270 np = of_find_node_by_path("/testcase-data/testcase-device2");
1271 pdev = of_find_device_by_node(np);
1272 unittest(pdev, "device 2 creation failed\n");
1274 EXPECT_BEGIN(KERN_INFO,
1275 "platform testcase-data:testcase-device2: IRQ index 0 not found");
1277 irq = platform_get_irq(pdev, 0);
1279 EXPECT_END(KERN_INFO,
1280 "platform testcase-data:testcase-device2: IRQ index 0 not found");
1282 unittest(irq < 0 && irq != -EPROBE_DEFER,
1283 "device parsing error failed - %d\n", irq);
1286 np = of_find_node_by_path("/testcase-data/platform-tests");
1287 unittest(np, "No testcase data in device tree\n");
1291 test_bus = platform_device_register_full(&test_bus_info);
1292 rc = PTR_ERR_OR_ZERO(test_bus);
1293 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1298 test_bus->dev.of_node = np;
1301 * Add a dummy resource to the test bus node after it is
1302 * registered to catch problems with un-inserted resources. The
1303 * DT code doesn't insert the resources, and it has caused the
1304 * kernel to oops in the past. This makes sure the same bug
1305 * doesn't crop up again.
1307 platform_device_add_resources(test_bus, &test_bus_res, 1);
1309 of_platform_populate(np, match, NULL, &test_bus->dev);
1310 for_each_child_of_node(np, child) {
1311 for_each_child_of_node(child, grandchild) {
1312 pdev = of_find_device_by_node(grandchild);
1314 "Could not create device for node '%pOFn'\n",
1320 of_platform_depopulate(&test_bus->dev);
1321 for_each_child_of_node(np, child) {
1322 for_each_child_of_node(child, grandchild)
1323 unittest(!of_find_device_by_node(grandchild),
1324 "device didn't get destroyed '%pOFn'\n",
1328 platform_device_unregister(test_bus);
1333 * update_node_properties - adds the properties
1334 * of np into dup node (present in live tree) and
1335 * updates parent of children of np to dup.
1337 * @np: node whose properties are being added to the live tree
1338 * @dup: node present in live tree to be updated
1340 static void update_node_properties(struct device_node *np,
1341 struct device_node *dup)
1343 struct property *prop;
1344 struct property *save_next;
1345 struct device_node *child;
1348 for_each_child_of_node(np, child)
1349 child->parent = dup;
1352 * "unittest internal error: unable to add testdata property"
1354 * If this message reports a property in node '/__symbols__' then
1355 * the respective unittest overlay contains a label that has the
1356 * same name as a label in the live devicetree. The label will
1357 * be in the live devicetree only if the devicetree source was
1358 * compiled with the '-@' option. If you encounter this error,
1359 * please consider renaming __all__ of the labels in the unittest
1360 * overlay dts files with an odd prefix that is unlikely to be
1361 * used in a real devicetree.
1365 * open code for_each_property_of_node() because of_add_property()
1366 * sets prop->next to NULL
1368 for (prop = np->properties; prop != NULL; prop = save_next) {
1369 save_next = prop->next;
1370 ret = of_add_property(dup, prop);
1372 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1374 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1381 * attach_node_and_children - attaches nodes
1382 * and its children to live tree.
1383 * CAUTION: misleading function name - if node @np already exists in
1384 * the live tree then children of @np are *not* attached to the live
1385 * tree. This works for the current test devicetree nodes because such
1386 * nodes do not have child nodes.
1388 * @np: Node to attach to live tree
1390 static void attach_node_and_children(struct device_node *np)
1392 struct device_node *next, *dup, *child;
1393 unsigned long flags;
1394 const char *full_name;
1396 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1400 if (!strcmp(full_name, "/__local_fixups__") ||
1401 !strcmp(full_name, "/__fixups__")) {
1406 dup = of_find_node_by_path(full_name);
1409 update_node_properties(np, dup);
1416 mutex_lock(&of_mutex);
1417 raw_spin_lock_irqsave(&devtree_lock, flags);
1418 np->sibling = np->parent->child;
1419 np->parent->child = np;
1420 of_node_clear_flag(np, OF_DETACHED);
1421 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1423 __of_attach_node_sysfs(np);
1424 mutex_unlock(&of_mutex);
1427 next = child->sibling;
1428 attach_node_and_children(child);
1434 * unittest_data_add - Reads, copies data from
1435 * linked tree and attaches it to the live tree
1437 static int __init unittest_data_add(void)
1439 void *unittest_data;
1440 struct device_node *unittest_data_node, *np;
1442 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1443 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1445 extern uint8_t __dtb_testcases_begin[];
1446 extern uint8_t __dtb_testcases_end[];
1447 const int size = __dtb_testcases_end - __dtb_testcases_begin;
1451 pr_warn("%s: No testcase data to attach; not running tests\n",
1457 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
1461 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
1462 if (!unittest_data_node) {
1463 pr_warn("%s: No tree to attach; not running tests\n", __func__);
1464 kfree(unittest_data);
1469 * This lock normally encloses of_resolve_phandles()
1471 of_overlay_mutex_lock();
1473 rc = of_resolve_phandles(unittest_data_node);
1475 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1476 of_overlay_mutex_unlock();
1481 of_root = unittest_data_node;
1482 for_each_of_allnodes(np)
1483 __of_attach_node_sysfs(np);
1484 of_aliases = of_find_node_by_path("/aliases");
1485 of_chosen = of_find_node_by_path("/chosen");
1486 of_overlay_mutex_unlock();
1490 EXPECT_BEGIN(KERN_INFO,
1491 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1493 /* attach the sub-tree to live tree */
1494 np = unittest_data_node->child;
1496 struct device_node *next = np->sibling;
1498 np->parent = of_root;
1499 attach_node_and_children(np);
1503 EXPECT_END(KERN_INFO,
1504 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1506 of_overlay_mutex_unlock();
1511 #ifdef CONFIG_OF_OVERLAY
1512 static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
1514 static int unittest_probe(struct platform_device *pdev)
1516 struct device *dev = &pdev->dev;
1517 struct device_node *np = dev->of_node;
1520 dev_err(dev, "No OF data for device\n");
1525 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1527 of_platform_populate(np, NULL, NULL, &pdev->dev);
1532 static int unittest_remove(struct platform_device *pdev)
1534 struct device *dev = &pdev->dev;
1535 struct device_node *np = dev->of_node;
1537 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1541 static const struct of_device_id unittest_match[] = {
1542 { .compatible = "unittest", },
1546 static struct platform_driver unittest_driver = {
1547 .probe = unittest_probe,
1548 .remove = unittest_remove,
1551 .of_match_table = of_match_ptr(unittest_match),
1555 /* get the platform device instantiated at the path */
1556 static struct platform_device *of_path_to_platform_device(const char *path)
1558 struct device_node *np;
1559 struct platform_device *pdev;
1561 np = of_find_node_by_path(path);
1565 pdev = of_find_device_by_node(np);
1571 /* find out if a platform device exists at that path */
1572 static int of_path_platform_device_exists(const char *path)
1574 struct platform_device *pdev;
1576 pdev = of_path_to_platform_device(path);
1577 platform_device_put(pdev);
1578 return pdev != NULL;
1581 #ifdef CONFIG_OF_GPIO
1583 struct unittest_gpio_dev {
1584 struct gpio_chip chip;
1587 static int unittest_gpio_chip_request_count;
1588 static int unittest_gpio_probe_count;
1589 static int unittest_gpio_probe_pass_count;
1591 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1593 unittest_gpio_chip_request_count++;
1595 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1596 unittest_gpio_chip_request_count);
1600 static int unittest_gpio_probe(struct platform_device *pdev)
1602 struct unittest_gpio_dev *devptr;
1605 unittest_gpio_probe_count++;
1607 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1611 platform_set_drvdata(pdev, devptr);
1613 devptr->chip.of_node = pdev->dev.of_node;
1614 devptr->chip.label = "of-unittest-gpio";
1615 devptr->chip.base = -1; /* dynamic allocation */
1616 devptr->chip.ngpio = 5;
1617 devptr->chip.request = unittest_gpio_chip_request;
1619 ret = gpiochip_add_data(&devptr->chip, NULL);
1622 "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr->chip.of_node, ret);
1625 unittest_gpio_probe_pass_count++;
1629 static int unittest_gpio_remove(struct platform_device *pdev)
1631 struct unittest_gpio_dev *gdev = platform_get_drvdata(pdev);
1632 struct device *dev = &pdev->dev;
1633 struct device_node *np = pdev->dev.of_node;
1635 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1640 if (gdev->chip.base != -1)
1641 gpiochip_remove(&gdev->chip);
1643 platform_set_drvdata(pdev, NULL);
1649 static const struct of_device_id unittest_gpio_id[] = {
1650 { .compatible = "unittest-gpio", },
1654 static struct platform_driver unittest_gpio_driver = {
1655 .probe = unittest_gpio_probe,
1656 .remove = unittest_gpio_remove,
1658 .name = "unittest-gpio",
1659 .of_match_table = of_match_ptr(unittest_gpio_id),
1663 static void __init of_unittest_overlay_gpio(void)
1665 int chip_request_count;
1666 int probe_pass_count;
1670 * tests: apply overlays before registering driver
1671 * Similar to installing a driver as a module, the
1672 * driver is registered after applying the overlays.
1674 * The overlays are applied by overlay_data_apply()
1675 * instead of of_unittest_apply_overlay() so that they
1676 * will not be tracked. Thus they will not be removed
1677 * by of_unittest_destroy_tracked_overlays().
1679 * - apply overlay_gpio_01
1680 * - apply overlay_gpio_02a
1681 * - apply overlay_gpio_02b
1684 * register driver will result in
1685 * - probe and processing gpio hog for overlay_gpio_01
1686 * - probe for overlay_gpio_02a
1687 * - processing gpio for overlay_gpio_02b
1690 probe_pass_count = unittest_gpio_probe_pass_count;
1691 chip_request_count = unittest_gpio_chip_request_count;
1694 * overlay_gpio_01 contains gpio node and child gpio hog node
1695 * overlay_gpio_02a contains gpio node
1696 * overlay_gpio_02b contains child gpio hog node
1699 unittest(overlay_data_apply("overlay_gpio_01", NULL),
1700 "Adding overlay 'overlay_gpio_01' failed\n");
1702 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1703 "Adding overlay 'overlay_gpio_02a' failed\n");
1705 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1706 "Adding overlay 'overlay_gpio_02b' failed\n");
1709 * messages are the result of the probes, after the
1710 * driver is registered
1713 EXPECT_BEGIN(KERN_INFO,
1714 "gpio-<<int>> (line-B-input): hogged as input\n");
1716 EXPECT_BEGIN(KERN_INFO,
1717 "gpio-<<int>> (line-A-input): hogged as input\n");
1719 ret = platform_driver_register(&unittest_gpio_driver);
1720 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1723 EXPECT_END(KERN_INFO,
1724 "gpio-<<int>> (line-A-input): hogged as input\n");
1725 EXPECT_END(KERN_INFO,
1726 "gpio-<<int>> (line-B-input): hogged as input\n");
1728 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1729 "unittest_gpio_probe() failed or not called\n");
1731 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1732 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1733 unittest_gpio_chip_request_count - chip_request_count);
1736 * tests: apply overlays after registering driver
1738 * Similar to a driver built-in to the kernel, the
1739 * driver is registered before applying the overlays.
1741 * overlay_gpio_03 contains gpio node and child gpio hog node
1743 * - apply overlay_gpio_03
1745 * apply overlay will result in
1746 * - probe and processing gpio hog.
1749 probe_pass_count = unittest_gpio_probe_pass_count;
1750 chip_request_count = unittest_gpio_chip_request_count;
1752 EXPECT_BEGIN(KERN_INFO,
1753 "gpio-<<int>> (line-D-input): hogged as input\n");
1755 /* overlay_gpio_03 contains gpio node and child gpio hog node */
1757 unittest(overlay_data_apply("overlay_gpio_03", NULL),
1758 "Adding overlay 'overlay_gpio_03' failed\n");
1760 EXPECT_END(KERN_INFO,
1761 "gpio-<<int>> (line-D-input): hogged as input\n");
1763 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1764 "unittest_gpio_probe() failed or not called\n");
1766 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1767 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1768 unittest_gpio_chip_request_count - chip_request_count);
1771 * overlay_gpio_04a contains gpio node
1773 * - apply overlay_gpio_04a
1775 * apply the overlay will result in
1776 * - probe for overlay_gpio_04a
1779 probe_pass_count = unittest_gpio_probe_pass_count;
1780 chip_request_count = unittest_gpio_chip_request_count;
1782 /* overlay_gpio_04a contains gpio node */
1784 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
1785 "Adding overlay 'overlay_gpio_04a' failed\n");
1787 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1788 "unittest_gpio_probe() failed or not called\n");
1791 * overlay_gpio_04b contains child gpio hog node
1793 * - apply overlay_gpio_04b
1795 * apply the overlay will result in
1796 * - processing gpio for overlay_gpio_04b
1799 EXPECT_BEGIN(KERN_INFO,
1800 "gpio-<<int>> (line-C-input): hogged as input\n");
1802 /* overlay_gpio_04b contains child gpio hog node */
1804 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
1805 "Adding overlay 'overlay_gpio_04b' failed\n");
1807 EXPECT_END(KERN_INFO,
1808 "gpio-<<int>> (line-C-input): hogged as input\n");
1810 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1811 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1812 unittest_gpio_chip_request_count - chip_request_count);
1817 static void __init of_unittest_overlay_gpio(void)
1824 #if IS_BUILTIN(CONFIG_I2C)
1826 /* get the i2c client device instantiated at the path */
1827 static struct i2c_client *of_path_to_i2c_client(const char *path)
1829 struct device_node *np;
1830 struct i2c_client *client;
1832 np = of_find_node_by_path(path);
1836 client = of_find_i2c_device_by_node(np);
1842 /* find out if a i2c client device exists at that path */
1843 static int of_path_i2c_client_exists(const char *path)
1845 struct i2c_client *client;
1847 client = of_path_to_i2c_client(path);
1849 put_device(&client->dev);
1850 return client != NULL;
1853 static int of_path_i2c_client_exists(const char *path)
1864 static int of_path_device_type_exists(const char *path,
1865 enum overlay_type ovtype)
1869 return of_path_platform_device_exists(path);
1871 return of_path_i2c_client_exists(path);
1876 static const char *unittest_path(int nr, enum overlay_type ovtype)
1879 static char buf[256];
1883 base = "/testcase-data/overlay-node/test-bus";
1886 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1892 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1893 buf[sizeof(buf) - 1] = '\0';
1897 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1901 path = unittest_path(unittest_nr, ovtype);
1905 return of_path_platform_device_exists(path);
1907 return of_path_i2c_client_exists(path);
1912 static const char *overlay_name_from_nr(int nr)
1914 static char buf[256];
1916 snprintf(buf, sizeof(buf) - 1,
1918 buf[sizeof(buf) - 1] = '\0';
1923 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1925 /* FIXME: it is NOT guaranteed that overlay ids are assigned in sequence */
1927 #define MAX_UNITTEST_OVERLAYS 256
1928 static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1929 static int overlay_first_id = -1;
1931 static long of_unittest_overlay_tracked(int id)
1933 if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1935 return overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id);
1938 static void of_unittest_track_overlay(int id)
1940 if (overlay_first_id < 0)
1941 overlay_first_id = id;
1942 id -= overlay_first_id;
1944 if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1946 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1949 static void of_unittest_untrack_overlay(int id)
1951 if (overlay_first_id < 0)
1953 id -= overlay_first_id;
1954 if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1956 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1959 static void of_unittest_destroy_tracked_overlays(void)
1961 int id, ret, defers, ovcs_id;
1963 if (overlay_first_id < 0)
1966 /* try until no defers */
1969 /* remove in reverse order */
1970 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1971 if (!of_unittest_overlay_tracked(id))
1974 ovcs_id = id + overlay_first_id;
1975 ret = of_overlay_remove(&ovcs_id);
1976 if (ret == -ENODEV) {
1977 pr_warn("%s: no overlay to destroy for #%d\n",
1978 __func__, id + overlay_first_id);
1983 pr_warn("%s: overlay destroy failed for #%d\n",
1984 __func__, id + overlay_first_id);
1988 of_unittest_untrack_overlay(id);
1990 } while (defers > 0);
1993 static int __init of_unittest_apply_overlay(int overlay_nr, int *overlay_id)
1995 const char *overlay_name;
1997 overlay_name = overlay_name_from_nr(overlay_nr);
1999 if (!overlay_data_apply(overlay_name, overlay_id)) {
2000 unittest(0, "could not apply overlay \"%s\"\n",
2004 of_unittest_track_overlay(*overlay_id);
2009 /* apply an overlay while checking before and after states */
2010 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2011 int unittest_nr, int before, int after,
2012 enum overlay_type ovtype)
2016 /* unittest device must not be in before state */
2017 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2018 unittest(0, "%s with device @\"%s\" %s\n",
2019 overlay_name_from_nr(overlay_nr),
2020 unittest_path(unittest_nr, ovtype),
2021 !before ? "enabled" : "disabled");
2026 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2028 /* of_unittest_apply_overlay already called unittest() */
2032 /* unittest device must be to set to after state */
2033 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2034 unittest(0, "%s failed to create @\"%s\" %s\n",
2035 overlay_name_from_nr(overlay_nr),
2036 unittest_path(unittest_nr, ovtype),
2037 !after ? "enabled" : "disabled");
2044 /* apply an overlay and then revert it while checking before, after states */
2045 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2046 int unittest_nr, int before, int after,
2047 enum overlay_type ovtype)
2049 int ret, ovcs_id, save_id;
2051 /* unittest device must be in before state */
2052 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2053 unittest(0, "%s with device @\"%s\" %s\n",
2054 overlay_name_from_nr(overlay_nr),
2055 unittest_path(unittest_nr, ovtype),
2056 !before ? "enabled" : "disabled");
2060 /* apply the overlay */
2062 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2064 /* of_unittest_apply_overlay already called unittest() */
2068 /* unittest device must be in after state */
2069 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2070 unittest(0, "%s failed to create @\"%s\" %s\n",
2071 overlay_name_from_nr(overlay_nr),
2072 unittest_path(unittest_nr, ovtype),
2073 !after ? "enabled" : "disabled");
2078 ret = of_overlay_remove(&ovcs_id);
2080 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2081 overlay_name_from_nr(overlay_nr),
2082 unittest_path(unittest_nr, ovtype));
2085 of_unittest_untrack_overlay(save_id);
2087 /* unittest device must be again in before state */
2088 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2089 unittest(0, "%s with device @\"%s\" %s\n",
2090 overlay_name_from_nr(overlay_nr),
2091 unittest_path(unittest_nr, ovtype),
2092 !before ? "enabled" : "disabled");
2099 /* test activation of device */
2100 static void __init of_unittest_overlay_0(void)
2104 EXPECT_BEGIN(KERN_INFO,
2105 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2107 /* device should enable */
2108 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2110 EXPECT_END(KERN_INFO,
2111 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2116 unittest(1, "overlay test %d passed\n", 0);
2119 /* test deactivation of device */
2120 static void __init of_unittest_overlay_1(void)
2124 EXPECT_BEGIN(KERN_INFO,
2125 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2127 /* device should disable */
2128 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2130 EXPECT_END(KERN_INFO,
2131 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2136 unittest(1, "overlay test %d passed\n", 1);
2140 /* test activation of device */
2141 static void __init of_unittest_overlay_2(void)
2145 EXPECT_BEGIN(KERN_INFO,
2146 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2148 /* device should enable */
2149 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2151 EXPECT_END(KERN_INFO,
2152 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2156 unittest(1, "overlay test %d passed\n", 2);
2159 /* test deactivation of device */
2160 static void __init of_unittest_overlay_3(void)
2164 EXPECT_BEGIN(KERN_INFO,
2165 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2167 /* device should disable */
2168 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2170 EXPECT_END(KERN_INFO,
2171 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2176 unittest(1, "overlay test %d passed\n", 3);
2179 /* test activation of a full device node */
2180 static void __init of_unittest_overlay_4(void)
2182 /* device should disable */
2183 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2186 unittest(1, "overlay test %d passed\n", 4);
2189 /* test overlay apply/revert sequence */
2190 static void __init of_unittest_overlay_5(void)
2194 EXPECT_BEGIN(KERN_INFO,
2195 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2197 /* device should disable */
2198 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2200 EXPECT_END(KERN_INFO,
2201 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2206 unittest(1, "overlay test %d passed\n", 5);
2209 /* test overlay application in sequence */
2210 static void __init of_unittest_overlay_6(void)
2212 int i, ov_id[2], ovcs_id;
2213 int overlay_nr = 6, unittest_nr = 6;
2214 int before = 0, after = 1;
2215 const char *overlay_name;
2219 /* unittest device must be in before state */
2220 for (i = 0; i < 2; i++) {
2221 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2223 unittest(0, "%s with device @\"%s\" %s\n",
2224 overlay_name_from_nr(overlay_nr + i),
2225 unittest_path(unittest_nr + i,
2227 !before ? "enabled" : "disabled");
2232 /* apply the overlays */
2234 EXPECT_BEGIN(KERN_INFO,
2235 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2237 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2239 ret = overlay_data_apply(overlay_name, &ovcs_id);
2242 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2246 of_unittest_track_overlay(ov_id[0]);
2248 EXPECT_END(KERN_INFO,
2249 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2251 EXPECT_BEGIN(KERN_INFO,
2252 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2254 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2256 ret = overlay_data_apply(overlay_name, &ovcs_id);
2259 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2263 of_unittest_track_overlay(ov_id[1]);
2265 EXPECT_END(KERN_INFO,
2266 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2269 for (i = 0; i < 2; i++) {
2270 /* unittest device must be in after state */
2271 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2273 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2274 overlay_name_from_nr(overlay_nr + i),
2275 unittest_path(unittest_nr + i,
2277 !after ? "enabled" : "disabled");
2282 for (i = 1; i >= 0; i--) {
2284 if (of_overlay_remove(&ovcs_id)) {
2285 unittest(0, "%s failed destroy @\"%s\"\n",
2286 overlay_name_from_nr(overlay_nr + i),
2287 unittest_path(unittest_nr + i,
2291 of_unittest_untrack_overlay(ov_id[i]);
2294 for (i = 0; i < 2; i++) {
2295 /* unittest device must be again in before state */
2296 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2298 unittest(0, "%s with device @\"%s\" %s\n",
2299 overlay_name_from_nr(overlay_nr + i),
2300 unittest_path(unittest_nr + i,
2302 !before ? "enabled" : "disabled");
2307 unittest(1, "overlay test %d passed\n", 6);
2311 /* test overlay application in sequence */
2312 static void __init of_unittest_overlay_8(void)
2314 int i, ov_id[2], ovcs_id;
2315 int overlay_nr = 8, unittest_nr = 8;
2316 const char *overlay_name;
2319 /* we don't care about device state in this test */
2321 EXPECT_BEGIN(KERN_INFO,
2322 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2324 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2326 ret = overlay_data_apply(overlay_name, &ovcs_id);
2328 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2330 EXPECT_END(KERN_INFO,
2331 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2337 of_unittest_track_overlay(ov_id[0]);
2339 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2341 EXPECT_BEGIN(KERN_INFO,
2342 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2344 /* apply the overlays */
2345 ret = overlay_data_apply(overlay_name, &ovcs_id);
2347 EXPECT_END(KERN_INFO,
2348 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2351 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2356 of_unittest_track_overlay(ov_id[1]);
2358 /* now try to remove first overlay (it should fail) */
2361 EXPECT_BEGIN(KERN_INFO,
2362 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2364 EXPECT_BEGIN(KERN_INFO,
2365 "OF: overlay: overlay #6 is not topmost");
2367 ret = of_overlay_remove(&ovcs_id);
2369 EXPECT_END(KERN_INFO,
2370 "OF: overlay: overlay #6 is not topmost");
2372 EXPECT_END(KERN_INFO,
2373 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2376 unittest(0, "%s was destroyed @\"%s\"\n",
2377 overlay_name_from_nr(overlay_nr + 0),
2378 unittest_path(unittest_nr,
2383 /* removing them in order should work */
2384 for (i = 1; i >= 0; i--) {
2386 if (of_overlay_remove(&ovcs_id)) {
2387 unittest(0, "%s not destroyed @\"%s\"\n",
2388 overlay_name_from_nr(overlay_nr + i),
2389 unittest_path(unittest_nr,
2393 of_unittest_untrack_overlay(ov_id[i]);
2396 unittest(1, "overlay test %d passed\n", 8);
2399 /* test insertion of a bus with parent devices */
2400 static void __init of_unittest_overlay_10(void)
2405 /* device should disable */
2406 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2408 if (unittest(ret == 0,
2409 "overlay test %d failed; overlay application\n", 10))
2412 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2413 unittest_path(10, PDEV_OVERLAY));
2414 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2417 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2420 unittest(ret, "overlay test %d failed; no child device\n", 10);
2423 /* test insertion of a bus with parent devices (and revert) */
2424 static void __init of_unittest_overlay_11(void)
2428 /* device should disable */
2429 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2432 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2435 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2437 struct unittest_i2c_bus_data {
2438 struct platform_device *pdev;
2439 struct i2c_adapter adap;
2442 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2443 struct i2c_msg *msgs, int num)
2445 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2452 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2454 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2457 static const struct i2c_algorithm unittest_i2c_algo = {
2458 .master_xfer = unittest_i2c_master_xfer,
2459 .functionality = unittest_i2c_functionality,
2462 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2464 struct device *dev = &pdev->dev;
2465 struct device_node *np = dev->of_node;
2466 struct unittest_i2c_bus_data *std;
2467 struct i2c_adapter *adap;
2471 dev_err(dev, "No OF data for device\n");
2476 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2478 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2482 /* link them together */
2484 platform_set_drvdata(pdev, std);
2487 i2c_set_adapdata(adap, std);
2489 strlcpy(adap->name, pdev->name, sizeof(adap->name));
2490 adap->class = I2C_CLASS_DEPRECATED;
2491 adap->algo = &unittest_i2c_algo;
2492 adap->dev.parent = dev;
2493 adap->dev.of_node = dev->of_node;
2494 adap->timeout = 5 * HZ;
2497 ret = i2c_add_numbered_adapter(adap);
2499 dev_err(dev, "Failed to add I2C adapter\n");
2506 static int unittest_i2c_bus_remove(struct platform_device *pdev)
2508 struct device *dev = &pdev->dev;
2509 struct device_node *np = dev->of_node;
2510 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2512 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2513 i2c_del_adapter(&std->adap);
2518 static const struct of_device_id unittest_i2c_bus_match[] = {
2519 { .compatible = "unittest-i2c-bus", },
2523 static struct platform_driver unittest_i2c_bus_driver = {
2524 .probe = unittest_i2c_bus_probe,
2525 .remove = unittest_i2c_bus_remove,
2527 .name = "unittest-i2c-bus",
2528 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
2532 static int unittest_i2c_dev_probe(struct i2c_client *client,
2533 const struct i2c_device_id *id)
2535 struct device *dev = &client->dev;
2536 struct device_node *np = client->dev.of_node;
2539 dev_err(dev, "No OF node\n");
2543 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2548 static int unittest_i2c_dev_remove(struct i2c_client *client)
2550 struct device *dev = &client->dev;
2551 struct device_node *np = client->dev.of_node;
2553 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2557 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2558 { .name = "unittest-i2c-dev" },
2562 static struct i2c_driver unittest_i2c_dev_driver = {
2564 .name = "unittest-i2c-dev",
2566 .probe = unittest_i2c_dev_probe,
2567 .remove = unittest_i2c_dev_remove,
2568 .id_table = unittest_i2c_dev_id,
2571 #if IS_BUILTIN(CONFIG_I2C_MUX)
2573 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2578 static int unittest_i2c_mux_probe(struct i2c_client *client,
2579 const struct i2c_device_id *id)
2582 struct device *dev = &client->dev;
2583 struct i2c_adapter *adap = client->adapter;
2584 struct device_node *np = client->dev.of_node, *child;
2585 struct i2c_mux_core *muxc;
2588 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2591 dev_err(dev, "No OF node\n");
2596 for_each_child_of_node(np, child) {
2597 if (of_property_read_u32(child, "reg", ®))
2599 if (max_reg == (u32)-1 || reg > max_reg)
2602 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2604 dev_err(dev, "No channels\n");
2608 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2609 unittest_i2c_mux_select_chan, NULL);
2612 for (i = 0; i < nchans; i++) {
2613 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2614 dev_err(dev, "Failed to register mux #%d\n", i);
2615 i2c_mux_del_adapters(muxc);
2620 i2c_set_clientdata(client, muxc);
2625 static int unittest_i2c_mux_remove(struct i2c_client *client)
2627 struct device *dev = &client->dev;
2628 struct device_node *np = client->dev.of_node;
2629 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2631 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2632 i2c_mux_del_adapters(muxc);
2636 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2637 { .name = "unittest-i2c-mux" },
2641 static struct i2c_driver unittest_i2c_mux_driver = {
2643 .name = "unittest-i2c-mux",
2645 .probe = unittest_i2c_mux_probe,
2646 .remove = unittest_i2c_mux_remove,
2647 .id_table = unittest_i2c_mux_id,
2652 static int of_unittest_overlay_i2c_init(void)
2656 ret = i2c_add_driver(&unittest_i2c_dev_driver);
2657 if (unittest(ret == 0,
2658 "could not register unittest i2c device driver\n"))
2661 ret = platform_driver_register(&unittest_i2c_bus_driver);
2663 if (unittest(ret == 0,
2664 "could not register unittest i2c bus driver\n"))
2667 #if IS_BUILTIN(CONFIG_I2C_MUX)
2669 EXPECT_BEGIN(KERN_INFO,
2670 "i2c i2c-1: Added multiplexed i2c bus 2");
2672 ret = i2c_add_driver(&unittest_i2c_mux_driver);
2674 EXPECT_END(KERN_INFO,
2675 "i2c i2c-1: Added multiplexed i2c bus 2");
2677 if (unittest(ret == 0,
2678 "could not register unittest i2c mux driver\n"))
2685 static void of_unittest_overlay_i2c_cleanup(void)
2687 #if IS_BUILTIN(CONFIG_I2C_MUX)
2688 i2c_del_driver(&unittest_i2c_mux_driver);
2690 platform_driver_unregister(&unittest_i2c_bus_driver);
2691 i2c_del_driver(&unittest_i2c_dev_driver);
2694 static void __init of_unittest_overlay_i2c_12(void)
2698 /* device should enable */
2699 EXPECT_BEGIN(KERN_INFO,
2700 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2702 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2704 EXPECT_END(KERN_INFO,
2705 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2710 unittest(1, "overlay test %d passed\n", 12);
2713 /* test deactivation of device */
2714 static void __init of_unittest_overlay_i2c_13(void)
2718 EXPECT_BEGIN(KERN_INFO,
2719 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2721 /* device should disable */
2722 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2724 EXPECT_END(KERN_INFO,
2725 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2730 unittest(1, "overlay test %d passed\n", 13);
2733 /* just check for i2c mux existence */
2734 static void of_unittest_overlay_i2c_14(void)
2738 static void __init of_unittest_overlay_i2c_15(void)
2742 /* device should enable */
2743 EXPECT_BEGIN(KERN_INFO,
2744 "i2c i2c-1: Added multiplexed i2c bus 3");
2746 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2748 EXPECT_END(KERN_INFO,
2749 "i2c i2c-1: Added multiplexed i2c bus 3");
2754 unittest(1, "overlay test %d passed\n", 15);
2759 static inline void of_unittest_overlay_i2c_14(void) { }
2760 static inline void of_unittest_overlay_i2c_15(void) { }
2764 static void __init of_unittest_overlay(void)
2766 struct device_node *bus_np = NULL;
2768 if (platform_driver_register(&unittest_driver)) {
2769 unittest(0, "could not register unittest driver\n");
2773 bus_np = of_find_node_by_path(bus_path);
2774 if (bus_np == NULL) {
2775 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
2779 if (of_platform_default_populate(bus_np, NULL, NULL)) {
2780 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
2784 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2785 unittest(0, "could not find unittest0 @ \"%s\"\n",
2786 unittest_path(100, PDEV_OVERLAY));
2790 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2791 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2792 unittest_path(101, PDEV_OVERLAY));
2796 unittest(1, "basic infrastructure of overlays passed");
2798 /* tests in sequence */
2799 of_unittest_overlay_0();
2800 of_unittest_overlay_1();
2801 of_unittest_overlay_2();
2802 of_unittest_overlay_3();
2803 of_unittest_overlay_4();
2804 of_unittest_overlay_5();
2805 of_unittest_overlay_6();
2806 of_unittest_overlay_8();
2808 of_unittest_overlay_10();
2809 of_unittest_overlay_11();
2811 #if IS_BUILTIN(CONFIG_I2C)
2812 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
2815 of_unittest_overlay_i2c_12();
2816 of_unittest_overlay_i2c_13();
2817 of_unittest_overlay_i2c_14();
2818 of_unittest_overlay_i2c_15();
2820 of_unittest_overlay_i2c_cleanup();
2823 of_unittest_overlay_gpio();
2825 of_unittest_destroy_tracked_overlays();
2828 of_node_put(bus_np);
2832 static inline void __init of_unittest_overlay(void) { }
2835 #ifdef CONFIG_OF_OVERLAY
2838 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2839 * in scripts/Makefile.lib
2842 #define OVERLAY_INFO_EXTERN(name) \
2843 extern uint8_t __dtb_##name##_begin[]; \
2844 extern uint8_t __dtb_##name##_end[]
2846 #define OVERLAY_INFO(overlay_name, expected) \
2847 { .dtb_begin = __dtb_##overlay_name##_begin, \
2848 .dtb_end = __dtb_##overlay_name##_end, \
2849 .expected_result = expected, \
2850 .name = #overlay_name, \
2853 struct overlay_info {
2856 int expected_result;
2861 OVERLAY_INFO_EXTERN(overlay_base);
2862 OVERLAY_INFO_EXTERN(overlay);
2863 OVERLAY_INFO_EXTERN(overlay_0);
2864 OVERLAY_INFO_EXTERN(overlay_1);
2865 OVERLAY_INFO_EXTERN(overlay_2);
2866 OVERLAY_INFO_EXTERN(overlay_3);
2867 OVERLAY_INFO_EXTERN(overlay_4);
2868 OVERLAY_INFO_EXTERN(overlay_5);
2869 OVERLAY_INFO_EXTERN(overlay_6);
2870 OVERLAY_INFO_EXTERN(overlay_7);
2871 OVERLAY_INFO_EXTERN(overlay_8);
2872 OVERLAY_INFO_EXTERN(overlay_9);
2873 OVERLAY_INFO_EXTERN(overlay_10);
2874 OVERLAY_INFO_EXTERN(overlay_11);
2875 OVERLAY_INFO_EXTERN(overlay_12);
2876 OVERLAY_INFO_EXTERN(overlay_13);
2877 OVERLAY_INFO_EXTERN(overlay_15);
2878 OVERLAY_INFO_EXTERN(overlay_gpio_01);
2879 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
2880 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
2881 OVERLAY_INFO_EXTERN(overlay_gpio_03);
2882 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
2883 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
2884 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
2885 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
2886 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2887 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2889 /* entries found by name */
2890 static struct overlay_info overlays[] = {
2891 OVERLAY_INFO(overlay_base, -9999),
2892 OVERLAY_INFO(overlay, 0),
2893 OVERLAY_INFO(overlay_0, 0),
2894 OVERLAY_INFO(overlay_1, 0),
2895 OVERLAY_INFO(overlay_2, 0),
2896 OVERLAY_INFO(overlay_3, 0),
2897 OVERLAY_INFO(overlay_4, 0),
2898 OVERLAY_INFO(overlay_5, 0),
2899 OVERLAY_INFO(overlay_6, 0),
2900 OVERLAY_INFO(overlay_7, 0),
2901 OVERLAY_INFO(overlay_8, 0),
2902 OVERLAY_INFO(overlay_9, 0),
2903 OVERLAY_INFO(overlay_10, 0),
2904 OVERLAY_INFO(overlay_11, 0),
2905 OVERLAY_INFO(overlay_12, 0),
2906 OVERLAY_INFO(overlay_13, 0),
2907 OVERLAY_INFO(overlay_15, 0),
2908 OVERLAY_INFO(overlay_gpio_01, 0),
2909 OVERLAY_INFO(overlay_gpio_02a, 0),
2910 OVERLAY_INFO(overlay_gpio_02b, 0),
2911 OVERLAY_INFO(overlay_gpio_03, 0),
2912 OVERLAY_INFO(overlay_gpio_04a, 0),
2913 OVERLAY_INFO(overlay_gpio_04b, 0),
2914 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
2915 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
2916 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2917 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2919 {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
2922 static struct device_node *overlay_base_root;
2924 static void * __init dt_alloc_memory(u64 size, u64 align)
2926 void *ptr = memblock_alloc(size, align);
2929 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2930 __func__, size, align);
2936 * Create base device tree for the overlay unittest.
2938 * This is called from very early boot code.
2940 * Do as much as possible the same way as done in __unflatten_device_tree
2941 * and other early boot steps for the normal FDT so that the overlay base
2942 * unflattened tree will have the same characteristics as the real tree
2943 * (such as having memory allocated by the early allocator). The goal
2944 * is to test "the real thing" as much as possible, and test "test setup
2945 * code" as little as possible.
2947 * Have to stop before resolving phandles, because that uses kmalloc.
2949 void __init unittest_unflatten_overlay_base(void)
2951 struct overlay_info *info;
2956 const char *overlay_name = "overlay_base";
2958 for (info = overlays; info && info->name; info++) {
2959 if (!strcmp(overlay_name, info->name)) {
2965 pr_err("no overlay data for %s\n", overlay_name);
2969 info = &overlays[0];
2971 if (info->expected_result != -9999) {
2972 pr_err("No dtb 'overlay_base' to attach\n");
2976 data_size = info->dtb_end - info->dtb_begin;
2978 pr_err("No dtb 'overlay_base' to attach\n");
2982 size = fdt_totalsize(info->dtb_begin);
2983 if (size != data_size) {
2984 pr_err("dtb 'overlay_base' header totalsize != actual size");
2988 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2990 pr_err("alloc for dtb 'overlay_base' failed");
2994 memcpy(new_fdt, info->dtb_begin, size);
2996 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
2997 dt_alloc_memory, true);
3001 * The purpose of of_unittest_overlay_data_add is to add an
3002 * overlay in the normal fashion. This is a test of the whole
3003 * picture, instead of testing individual elements.
3005 * A secondary purpose is to be able to verify that the contents of
3006 * /proc/device-tree/ contains the updated structure and values from
3007 * the overlay. That must be verified separately in user space.
3009 * Return 0 on unexpected error.
3011 static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
3013 struct overlay_info *info;
3018 for (info = overlays; info && info->name; info++) {
3019 if (!strcmp(overlay_name, info->name)) {
3025 pr_err("no overlay data for %s\n", overlay_name);
3029 size = info->dtb_end - info->dtb_begin;
3031 pr_err("no overlay data for %s\n", overlay_name);
3033 ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
3035 *overlay_id = info->overlay_id;
3039 pr_debug("%s applied\n", overlay_name);
3042 if (ret != info->expected_result)
3043 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3044 info->expected_result, ret, overlay_name);
3046 return (ret == info->expected_result);
3050 * The purpose of of_unittest_overlay_high_level is to add an overlay
3051 * in the normal fashion. This is a test of the whole picture,
3052 * instead of individual elements.
3054 * The first part of the function is _not_ normal overlay usage; it is
3055 * finishing splicing the base overlay device tree into the live tree.
3057 static __init void of_unittest_overlay_high_level(void)
3059 struct device_node *last_sibling;
3060 struct device_node *np;
3061 struct device_node *of_symbols;
3062 struct device_node *overlay_base_symbols;
3063 struct device_node **pprev;
3064 struct property *prop;
3067 if (!overlay_base_root) {
3068 unittest(0, "overlay_base_root not initialized\n");
3073 * Could not fixup phandles in unittest_unflatten_overlay_base()
3074 * because kmalloc() was not yet available.
3076 of_overlay_mutex_lock();
3077 of_resolve_phandles(overlay_base_root);
3078 of_overlay_mutex_unlock();
3082 * do not allow overlay_base to duplicate any node already in
3083 * tree, this greatly simplifies the code
3087 * remove overlay_base_root node "__local_fixups", after
3088 * being used by of_resolve_phandles()
3090 pprev = &overlay_base_root->child;
3091 for (np = overlay_base_root->child; np; np = np->sibling) {
3092 if (of_node_name_eq(np, "__local_fixups__")) {
3093 *pprev = np->sibling;
3096 pprev = &np->sibling;
3099 /* remove overlay_base_root node "__symbols__" if in live tree */
3100 of_symbols = of_get_child_by_name(of_root, "__symbols__");
3102 /* will have to graft properties from node into live tree */
3103 pprev = &overlay_base_root->child;
3104 for (np = overlay_base_root->child; np; np = np->sibling) {
3105 if (of_node_name_eq(np, "__symbols__")) {
3106 overlay_base_symbols = np;
3107 *pprev = np->sibling;
3110 pprev = &np->sibling;
3114 for_each_child_of_node(overlay_base_root, np) {
3115 struct device_node *base_child;
3116 for_each_child_of_node(of_root, base_child) {
3117 if (!strcmp(np->full_name, base_child->full_name)) {
3118 unittest(0, "illegal node name in overlay_base %pOFn",
3126 * overlay 'overlay_base' is not allowed to have root
3127 * properties, so only need to splice nodes into main device tree.
3129 * root node of *overlay_base_root will not be freed, it is lost
3133 for (np = overlay_base_root->child; np; np = np->sibling)
3134 np->parent = of_root;
3136 mutex_lock(&of_mutex);
3138 for (last_sibling = np = of_root->child; np; np = np->sibling)
3142 last_sibling->sibling = overlay_base_root->child;
3144 of_root->child = overlay_base_root->child;
3146 for_each_of_allnodes_from(overlay_base_root, np)
3147 __of_attach_node_sysfs(np);
3150 struct property *new_prop;
3151 for_each_property_of_node(overlay_base_symbols, prop) {
3153 new_prop = __of_prop_dup(prop, GFP_KERNEL);
3155 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3159 if (__of_add_property(of_symbols, new_prop)) {
3160 kfree(new_prop->name);
3161 kfree(new_prop->value);
3163 /* "name" auto-generated by unflatten */
3164 if (!strcmp(prop->name, "name"))
3166 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3170 if (__of_add_property_sysfs(of_symbols, new_prop)) {
3171 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3178 mutex_unlock(&of_mutex);
3181 /* now do the normal overlay usage test */
3183 EXPECT_BEGIN(KERN_ERR,
3184 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3185 EXPECT_BEGIN(KERN_ERR,
3186 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3187 EXPECT_BEGIN(KERN_ERR,
3188 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3189 EXPECT_BEGIN(KERN_ERR,
3190 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3191 EXPECT_BEGIN(KERN_ERR,
3192 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3193 EXPECT_BEGIN(KERN_ERR,
3194 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3195 EXPECT_BEGIN(KERN_ERR,
3196 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3197 EXPECT_BEGIN(KERN_ERR,
3198 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3199 EXPECT_BEGIN(KERN_ERR,
3200 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3201 EXPECT_BEGIN(KERN_ERR,
3202 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3203 EXPECT_BEGIN(KERN_ERR,
3204 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3206 ret = overlay_data_apply("overlay", NULL);
3208 EXPECT_END(KERN_ERR,
3209 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3210 EXPECT_END(KERN_ERR,
3211 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3212 EXPECT_END(KERN_ERR,
3213 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3214 EXPECT_END(KERN_ERR,
3215 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3216 EXPECT_END(KERN_ERR,
3217 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3218 EXPECT_END(KERN_ERR,
3219 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3220 EXPECT_END(KERN_ERR,
3221 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3222 EXPECT_END(KERN_ERR,
3223 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3224 EXPECT_END(KERN_ERR,
3225 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3226 EXPECT_END(KERN_ERR,
3227 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3228 EXPECT_END(KERN_ERR,
3229 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3231 unittest(ret, "Adding overlay 'overlay' failed\n");
3233 EXPECT_BEGIN(KERN_ERR,
3234 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3235 EXPECT_BEGIN(KERN_ERR,
3236 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3238 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3239 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3241 EXPECT_END(KERN_ERR,
3242 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3243 EXPECT_END(KERN_ERR,
3244 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3246 EXPECT_BEGIN(KERN_ERR,
3247 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3248 EXPECT_BEGIN(KERN_ERR,
3249 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3250 EXPECT_BEGIN(KERN_ERR,
3251 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3253 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3254 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3256 EXPECT_END(KERN_ERR,
3257 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3258 EXPECT_END(KERN_ERR,
3259 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3260 EXPECT_END(KERN_ERR,
3261 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3263 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
3264 "Adding overlay 'overlay_bad_phandle' failed\n");
3266 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
3267 "Adding overlay 'overlay_bad_symbol' failed\n");
3272 mutex_unlock(&of_mutex);
3277 static inline __init void of_unittest_overlay_high_level(void) {}
3281 static int __init of_unittest(void)
3283 struct device_node *np;
3286 pr_info("start of unittest - you will see error messages\n");
3288 /* adding data for unittest */
3290 if (IS_ENABLED(CONFIG_UML))
3291 unittest_unflatten_overlay_base();
3293 res = unittest_data_add();
3297 of_aliases = of_find_node_by_path("/aliases");
3299 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
3301 pr_info("No testcase data in device tree; not running tests\n");
3306 of_unittest_check_tree_linkage();
3307 of_unittest_check_phandles();
3308 of_unittest_find_node_by_name();
3309 of_unittest_dynamic();
3310 of_unittest_parse_phandle_with_args();
3311 of_unittest_parse_phandle_with_args_map();
3312 of_unittest_printf();
3313 of_unittest_property_string();
3314 of_unittest_property_copy();
3315 of_unittest_changeset();
3316 of_unittest_parse_interrupts();
3317 of_unittest_parse_interrupts_extended();
3318 of_unittest_dma_get_max_cpu_address();
3319 of_unittest_parse_dma_ranges();
3320 of_unittest_pci_dma_ranges();
3321 of_unittest_match_node();
3322 of_unittest_platform_populate();
3323 of_unittest_overlay();
3325 /* Double check linkage after removing testcase data */
3326 of_unittest_check_tree_linkage();
3328 of_unittest_overlay_high_level();
3330 pr_info("end of unittest - %i passed, %i failed\n",
3331 unittest_results.passed, unittest_results.failed);
3335 late_initcall(of_unittest);