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>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-mux.h>
30 #include <linux/gpio/driver.h>
32 #include <linux/bitops.h>
34 #include "of_private.h"
36 static struct unittest_results {
41 #define unittest(result, fmt, ...) ({ \
42 bool failed = !(result); \
44 unittest_results.failed++; \
45 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
47 unittest_results.passed++; \
48 pr_info("pass %s():%i\n", __func__, __LINE__); \
54 * Expected message may have a message level other than KERN_INFO.
55 * Print the expected message only if the current loglevel will allow
56 * the actual message to print.
58 * Do not use EXPECT_BEGIN(), EXPECT_END(), EXPECT_NOT_BEGIN(), or
59 * EXPECT_NOT_END() to report messages expected to be reported or not
60 * reported by pr_debug().
62 #define EXPECT_BEGIN(level, fmt, ...) \
63 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
65 #define EXPECT_END(level, fmt, ...) \
66 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
68 #define EXPECT_NOT_BEGIN(level, fmt, ...) \
69 printk(level pr_fmt("EXPECT_NOT \\ : ") fmt, ##__VA_ARGS__)
71 #define EXPECT_NOT_END(level, fmt, ...) \
72 printk(level pr_fmt("EXPECT_NOT / : ") fmt, ##__VA_ARGS__)
74 static void __init of_unittest_find_node_by_name(void)
76 struct device_node *np;
77 const char *options, *name;
79 np = of_find_node_by_path("/testcase-data");
80 name = kasprintf(GFP_KERNEL, "%pOF", np);
81 unittest(np && name && !strcmp("/testcase-data", name),
82 "find /testcase-data failed\n");
86 /* Test if trailing '/' works */
87 np = of_find_node_by_path("/testcase-data/");
88 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
90 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
91 name = kasprintf(GFP_KERNEL, "%pOF", np);
92 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
93 "find /testcase-data/phandle-tests/consumer-a failed\n");
97 np = of_find_node_by_path("testcase-alias");
98 name = kasprintf(GFP_KERNEL, "%pOF", np);
99 unittest(np && name && !strcmp("/testcase-data", name),
100 "find testcase-alias failed\n");
104 /* Test if trailing '/' works on aliases */
105 np = of_find_node_by_path("testcase-alias/");
106 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
108 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
109 name = kasprintf(GFP_KERNEL, "%pOF", np);
110 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
111 "find testcase-alias/phandle-tests/consumer-a failed\n");
115 np = of_find_node_by_path("/testcase-data/missing-path");
116 unittest(!np, "non-existent path returned node %pOF\n", np);
119 np = of_find_node_by_path("missing-alias");
120 unittest(!np, "non-existent alias returned node %pOF\n", np);
123 np = of_find_node_by_path("testcase-alias/missing-path");
124 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
127 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
128 unittest(np && !strcmp("testoption", options),
129 "option path test failed\n");
132 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
133 unittest(np && !strcmp("test/option", options),
134 "option path test, subcase #1 failed\n");
137 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
138 unittest(np && !strcmp("test/option", options),
139 "option path test, subcase #2 failed\n");
142 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
143 unittest(np, "NULL option path test failed\n");
146 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
148 unittest(np && !strcmp("testaliasoption", options),
149 "option alias path test failed\n");
152 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
154 unittest(np && !strcmp("test/alias/option", options),
155 "option alias path test, subcase #1 failed\n");
158 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
159 unittest(np, "NULL option alias path test failed\n");
162 options = "testoption";
163 np = of_find_node_opts_by_path("testcase-alias", &options);
164 unittest(np && !options, "option clearing test failed\n");
167 options = "testoption";
168 np = of_find_node_opts_by_path("/", &options);
169 unittest(np && !options, "option clearing root node test failed\n");
173 static void __init of_unittest_dynamic(void)
175 struct device_node *np;
176 struct property *prop;
178 np = of_find_node_by_path("/testcase-data");
180 pr_err("missing testcase data\n");
184 /* Array of 4 properties for the purpose of testing */
185 prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
187 unittest(0, "kzalloc() failed\n");
191 /* Add a new property - should pass*/
192 prop->name = "new-property";
193 prop->value = "new-property-data";
194 prop->length = strlen(prop->value) + 1;
195 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
197 /* Try to add an existing property - should fail */
199 prop->name = "new-property";
200 prop->value = "new-property-data-should-fail";
201 prop->length = strlen(prop->value) + 1;
202 unittest(of_add_property(np, prop) != 0,
203 "Adding an existing property should have failed\n");
205 /* Try to modify an existing property - should pass */
206 prop->value = "modify-property-data-should-pass";
207 prop->length = strlen(prop->value) + 1;
208 unittest(of_update_property(np, prop) == 0,
209 "Updating an existing property should have passed\n");
211 /* Try to modify non-existent property - should pass*/
213 prop->name = "modify-property";
214 prop->value = "modify-missing-property-data-should-pass";
215 prop->length = strlen(prop->value) + 1;
216 unittest(of_update_property(np, prop) == 0,
217 "Updating a missing property should have passed\n");
219 /* Remove property - should pass */
220 unittest(of_remove_property(np, prop) == 0,
221 "Removing a property should have passed\n");
223 /* Adding very large property - should pass */
225 prop->name = "large-property-PAGE_SIZEx8";
226 prop->length = PAGE_SIZE * 8;
227 prop->value = kzalloc(prop->length, GFP_KERNEL);
228 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
230 unittest(of_add_property(np, prop) == 0,
231 "Adding a large property should have passed\n");
234 static int __init of_unittest_check_node_linkage(struct device_node *np)
236 struct device_node *child;
239 for_each_child_of_node(np, child) {
240 if (child->parent != np) {
241 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
247 rc = of_unittest_check_node_linkage(child);
259 static void __init of_unittest_check_tree_linkage(void)
261 struct device_node *np;
262 int allnode_count = 0, child_count;
267 for_each_of_allnodes(np)
269 child_count = of_unittest_check_node_linkage(of_root);
271 unittest(child_count > 0, "Device node data structure is corrupted\n");
272 unittest(child_count == allnode_count,
273 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
274 allnode_count, child_count);
275 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
278 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
279 const char *expected)
285 buf_size = strlen(expected) + 10;
286 buf = kmalloc(buf_size, GFP_KERNEL);
290 /* Baseline; check conversion with a large size limit */
291 memset(buf, 0xff, buf_size);
292 size = snprintf(buf, buf_size - 2, fmt, np);
294 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
295 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
296 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
299 /* Make sure length limits work */
301 for (i = 0; i < 2; i++, size--) {
302 /* Clear the buffer, and make sure it works correctly still */
303 memset(buf, 0xff, buf_size);
304 snprintf(buf, size+1, fmt, np);
305 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
306 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
307 size, fmt, expected, buf);
312 static void __init of_unittest_printf(void)
314 struct device_node *np;
315 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
316 char phandle_str[16] = "";
318 np = of_find_node_by_path(full_name);
320 unittest(np, "testcase data missing\n");
324 num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
326 of_unittest_printf_one(np, "%pOF", full_name);
327 of_unittest_printf_one(np, "%pOFf", full_name);
328 of_unittest_printf_one(np, "%pOFn", "dev");
329 of_unittest_printf_one(np, "%2pOFn", "dev");
330 of_unittest_printf_one(np, "%5pOFn", " dev");
331 of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
332 of_unittest_printf_one(np, "%pOFp", phandle_str);
333 of_unittest_printf_one(np, "%pOFP", "dev@100");
334 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
335 of_unittest_printf_one(np, "%10pOFP", " dev@100");
336 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
337 of_unittest_printf_one(of_root, "%pOFP", "/");
338 of_unittest_printf_one(np, "%pOFF", "----");
339 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
340 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
341 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
342 of_unittest_printf_one(np, "%pOFC",
343 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
347 struct hlist_node node;
348 struct device_node *np;
351 static DEFINE_HASHTABLE(phandle_ht, 8);
352 static void __init of_unittest_check_phandles(void)
354 struct device_node *np;
355 struct node_hash *nh;
356 struct hlist_node *tmp;
357 int i, dup_count = 0, phandle_count = 0;
359 for_each_of_allnodes(np) {
363 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
364 if (nh->np->phandle == np->phandle) {
365 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
366 np->phandle, nh->np, np);
372 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
377 hash_add(phandle_ht, &nh->node, np->phandle);
380 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
381 dup_count, phandle_count);
384 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
390 static void __init of_unittest_parse_phandle_with_args(void)
392 struct device_node *np;
393 struct of_phandle_args args;
396 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
398 pr_err("missing testcase data\n");
402 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
403 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
405 for (i = 0; i < 8; i++) {
408 memset(&args, 0, sizeof(args));
409 rc = of_parse_phandle_with_args(np, "phandle-list",
410 "#phandle-cells", i, &args);
412 /* Test the values from tests-phandle.dtsi */
416 passed &= (args.args_count == 1);
417 passed &= (args.args[0] == (i + 1));
421 passed &= (args.args_count == 2);
422 passed &= (args.args[0] == (i + 1));
423 passed &= (args.args[1] == 0);
426 passed &= (rc == -ENOENT);
430 passed &= (args.args_count == 3);
431 passed &= (args.args[0] == (i + 1));
432 passed &= (args.args[1] == 4);
433 passed &= (args.args[2] == 3);
437 passed &= (args.args_count == 2);
438 passed &= (args.args[0] == (i + 1));
439 passed &= (args.args[1] == 100);
443 passed &= (args.args_count == 0);
447 passed &= (args.args_count == 1);
448 passed &= (args.args[0] == (i + 1));
451 passed &= (rc == -ENOENT);
457 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
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, *p0, *p1, *p2, *p3;
549 struct of_phandle_args args;
552 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
554 pr_err("missing testcase data\n");
558 p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
560 pr_err("missing testcase data\n");
564 p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
566 pr_err("missing testcase data\n");
570 p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
572 pr_err("missing testcase data\n");
576 p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
578 pr_err("missing testcase data\n");
582 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
583 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
585 for (i = 0; i < 8; i++) {
588 memset(&args, 0, sizeof(args));
589 rc = of_parse_phandle_with_args_map(np, "phandle-list",
590 "phandle", i, &args);
592 /* Test the values from tests-phandle.dtsi */
596 passed &= (args.np == p1);
597 passed &= (args.args_count == 1);
598 passed &= (args.args[0] == 1);
602 passed &= (args.np == p3);
603 passed &= (args.args_count == 3);
604 passed &= (args.args[0] == 2);
605 passed &= (args.args[1] == 5);
606 passed &= (args.args[2] == 3);
609 passed &= (rc == -ENOENT);
613 passed &= (args.np == p0);
614 passed &= (args.args_count == 0);
618 passed &= (args.np == p1);
619 passed &= (args.args_count == 1);
620 passed &= (args.args[0] == 3);
624 passed &= (args.np == p0);
625 passed &= (args.args_count == 0);
629 passed &= (args.np == p2);
630 passed &= (args.args_count == 2);
631 passed &= (args.args[0] == 15);
632 passed &= (args.args[1] == 0x20);
635 passed &= (rc == -ENOENT);
641 unittest(passed, "index %i - data error on node %s rc=%i\n",
642 i, args.np->full_name, rc);
645 /* Check for missing list property */
646 memset(&args, 0, sizeof(args));
647 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
648 "phandle", 0, &args);
649 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
651 /* Check for missing cells,map,mask property */
652 memset(&args, 0, sizeof(args));
654 EXPECT_BEGIN(KERN_INFO,
655 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
657 rc = of_parse_phandle_with_args_map(np, "phandle-list",
658 "phandle-missing", 0, &args);
659 EXPECT_END(KERN_INFO,
660 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
662 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
664 /* Check for bad phandle in list */
665 memset(&args, 0, sizeof(args));
667 EXPECT_BEGIN(KERN_INFO,
668 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
670 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
671 "phandle", 0, &args);
672 EXPECT_END(KERN_INFO,
673 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
675 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
677 /* Check for incorrectly formed argument list */
678 memset(&args, 0, sizeof(args));
680 EXPECT_BEGIN(KERN_INFO,
681 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
683 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
684 "phandle", 1, &args);
685 EXPECT_END(KERN_INFO,
686 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
688 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
691 static void __init of_unittest_property_string(void)
693 const char *strings[4];
694 struct device_node *np;
697 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
699 pr_err("No testcase data in device tree\n");
703 rc = of_property_match_string(np, "phandle-list-names", "first");
704 unittest(rc == 0, "first expected:0 got:%i\n", rc);
705 rc = of_property_match_string(np, "phandle-list-names", "second");
706 unittest(rc == 1, "second expected:1 got:%i\n", rc);
707 rc = of_property_match_string(np, "phandle-list-names", "third");
708 unittest(rc == 2, "third expected:2 got:%i\n", rc);
709 rc = of_property_match_string(np, "phandle-list-names", "fourth");
710 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
711 rc = of_property_match_string(np, "missing-property", "blah");
712 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
713 rc = of_property_match_string(np, "empty-property", "blah");
714 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
715 rc = of_property_match_string(np, "unterminated-string", "blah");
716 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
718 /* of_property_count_strings() tests */
719 rc = of_property_count_strings(np, "string-property");
720 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
721 rc = of_property_count_strings(np, "phandle-list-names");
722 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
723 rc = of_property_count_strings(np, "unterminated-string");
724 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
725 rc = of_property_count_strings(np, "unterminated-string-list");
726 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
728 /* of_property_read_string_index() tests */
729 rc = of_property_read_string_index(np, "string-property", 0, strings);
730 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
732 rc = of_property_read_string_index(np, "string-property", 1, strings);
733 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
734 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
735 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
736 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
737 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
738 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
739 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
741 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
742 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
744 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
745 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
746 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
747 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
749 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
750 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
753 /* of_property_read_string_array() tests */
754 rc = of_property_read_string_array(np, "string-property", strings, 4);
755 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
756 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
757 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
758 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
759 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
760 /* -- An incorrectly formed string should cause a failure */
761 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
762 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
763 /* -- parsing the correctly formed strings should still work: */
765 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
766 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
768 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
769 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
772 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
773 (p1)->value && (p2)->value && \
774 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
775 !strcmp((p1)->name, (p2)->name))
776 static void __init of_unittest_property_copy(void)
778 #ifdef CONFIG_OF_DYNAMIC
779 struct property p1 = { .name = "p1", .length = 0, .value = "" };
780 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
781 struct property *new;
783 new = __of_prop_dup(&p1, GFP_KERNEL);
784 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
789 new = __of_prop_dup(&p2, GFP_KERNEL);
790 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
797 static void __init of_unittest_changeset(void)
799 #ifdef CONFIG_OF_DYNAMIC
801 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
802 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
803 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
804 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
805 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
806 struct property *ppremove;
807 struct device_node *n1, *n2, *n21, *n22, *nchangeset, *nremove, *parent, *np;
808 static const char * const str_array[] = { "str1", "str2", "str3" };
809 const u32 u32_array[] = { 1, 2, 3 };
810 struct of_changeset chgset;
811 const char *propstr = NULL;
813 n1 = __of_node_dup(NULL, "n1");
814 unittest(n1, "testcase setup failure\n");
816 n2 = __of_node_dup(NULL, "n2");
817 unittest(n2, "testcase setup failure\n");
819 n21 = __of_node_dup(NULL, "n21");
820 unittest(n21, "testcase setup failure %p\n", n21);
822 nchangeset = of_find_node_by_path("/testcase-data/changeset");
823 nremove = of_get_child_by_name(nchangeset, "node-remove");
824 unittest(nremove, "testcase setup failure\n");
826 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
827 unittest(ppadd, "testcase setup failure\n");
829 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
830 unittest(ppname_n1, "testcase setup failure\n");
832 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
833 unittest(ppname_n2, "testcase setup failure\n");
835 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
836 unittest(ppname_n21, "testcase setup failure\n");
838 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
839 unittest(ppupdate, "testcase setup failure\n");
846 ppremove = of_find_property(parent, "prop-remove", NULL);
847 unittest(ppremove, "failed to find removal prop");
849 of_changeset_init(&chgset);
851 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
852 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
854 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
855 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
857 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
858 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
860 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
862 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
863 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
864 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
865 n22 = of_changeset_create_node(&chgset, n2, "n22");
866 unittest(n22, "fail create n22\n");
867 unittest(!of_changeset_add_prop_string(&chgset, n22, "prop-str", "abcd"),
868 "fail add prop prop-str");
869 unittest(!of_changeset_add_prop_string_array(&chgset, n22, "prop-str-array",
870 (const char **)str_array,
871 ARRAY_SIZE(str_array)),
872 "fail add prop prop-str-array");
873 unittest(!of_changeset_add_prop_u32_array(&chgset, n22, "prop-u32-array",
874 u32_array, ARRAY_SIZE(u32_array)),
875 "fail add prop prop-u32-array");
877 unittest(!of_changeset_apply(&chgset), "apply failed\n");
879 of_node_put(nchangeset);
881 /* Make sure node names are constructed correctly */
882 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
883 "'%pOF' not added\n", n21);
885 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n22")),
886 "'%pOF' not added\n", n22);
889 unittest(!of_changeset_revert(&chgset), "revert failed\n");
891 unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
892 "'%pOF' still present after revert\n", n21);
894 ppremove = of_find_property(parent, "prop-remove", NULL);
895 unittest(ppremove, "failed to find removed prop after revert\n");
897 ret = of_property_read_string(parent, "prop-update", &propstr);
898 unittest(!ret, "failed to find updated prop after revert\n");
900 unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
902 of_changeset_destroy(&chgset);
911 static void __init of_unittest_dma_get_max_cpu_address(void)
913 struct device_node *np;
914 phys_addr_t cpu_addr;
916 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
919 np = of_find_node_by_path("/testcase-data/address-tests");
921 pr_err("missing testcase data\n");
925 cpu_addr = of_dma_get_max_cpu_address(np);
926 unittest(cpu_addr == 0x4fffffff,
927 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
928 &cpu_addr, 0x4fffffff);
931 static void __init of_unittest_dma_ranges_one(const char *path,
932 u64 expect_dma_addr, u64 expect_paddr)
934 #ifdef CONFIG_HAS_DMA
935 struct device_node *np;
936 const struct bus_dma_region *map = NULL;
939 np = of_find_node_by_path(path);
941 pr_err("missing testcase data\n");
945 rc = of_dma_get_range(np, &map);
947 unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
952 struct device *dev_bogus;
954 dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
956 unittest(0, "kzalloc() failed\n");
961 dev_bogus->dma_range_map = map;
962 paddr = dma_to_phys(dev_bogus, expect_dma_addr);
963 dma_addr = phys_to_dma(dev_bogus, expect_paddr);
965 unittest(paddr == expect_paddr,
966 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
967 &paddr, expect_paddr, np);
968 unittest(dma_addr == expect_dma_addr,
969 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
970 &dma_addr, expect_dma_addr, np);
979 static void __init of_unittest_parse_dma_ranges(void)
981 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
983 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
984 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
985 0x100000000, 0x20000000);
986 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
987 0x80000000, 0x20000000);
990 static void __init of_unittest_pci_dma_ranges(void)
992 struct device_node *np;
993 struct of_pci_range range;
994 struct of_pci_range_parser parser;
997 if (!IS_ENABLED(CONFIG_PCI))
1000 np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
1002 pr_err("missing testcase data\n");
1006 if (of_pci_dma_range_parser_init(&parser, np)) {
1007 pr_err("missing dma-ranges property\n");
1012 * Get the dma-ranges from the device tree
1014 for_each_of_pci_range(&parser, &range) {
1016 unittest(range.size == 0x10000000,
1017 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1019 unittest(range.cpu_addr == 0x20000000,
1020 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1021 range.cpu_addr, np);
1022 unittest(range.pci_addr == 0x80000000,
1023 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1024 range.pci_addr, np);
1026 unittest(range.size == 0x10000000,
1027 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1029 unittest(range.cpu_addr == 0x40000000,
1030 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1031 range.cpu_addr, np);
1032 unittest(range.pci_addr == 0xc0000000,
1033 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1034 range.pci_addr, np);
1042 static void __init of_unittest_bus_ranges(void)
1044 struct device_node *np;
1045 struct of_range range;
1046 struct of_range_parser parser;
1047 struct resource res;
1048 int ret, count, i = 0;
1050 np = of_find_node_by_path("/testcase-data/address-tests");
1052 pr_err("missing testcase data\n");
1056 if (of_range_parser_init(&parser, np)) {
1057 pr_err("missing ranges property\n");
1061 ret = of_range_to_resource(np, 1, &res);
1062 unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1064 unittest(resource_type(&res) == IORESOURCE_MEM,
1065 "of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1067 unittest(res.start == 0xd0000000,
1068 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1070 unittest(resource_size(&res) == 0x20000000,
1071 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1074 count = of_range_count(&parser);
1075 unittest(count == 2,
1076 "of_range_count wrong size on node %pOF count=%d\n",
1080 * Get the "ranges" from the device tree
1082 for_each_of_range(&parser, &range) {
1083 unittest(range.flags == IORESOURCE_MEM,
1084 "for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1085 np, range.flags, IORESOURCE_MEM);
1087 unittest(range.size == 0x50000000,
1088 "for_each_of_range wrong size on node %pOF size=%llx\n",
1090 unittest(range.cpu_addr == 0x70000000,
1091 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1092 range.cpu_addr, np);
1093 unittest(range.bus_addr == 0x70000000,
1094 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1095 range.pci_addr, np);
1097 unittest(range.size == 0x20000000,
1098 "for_each_of_range wrong size on node %pOF size=%llx\n",
1100 unittest(range.cpu_addr == 0xd0000000,
1101 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1102 range.cpu_addr, np);
1103 unittest(range.bus_addr == 0x00000000,
1104 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1105 range.pci_addr, np);
1113 static void __init of_unittest_bus_3cell_ranges(void)
1115 struct device_node *np;
1116 struct of_range range;
1117 struct of_range_parser parser;
1120 np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1122 pr_err("missing testcase data\n");
1126 if (of_range_parser_init(&parser, np)) {
1127 pr_err("missing ranges property\n");
1132 * Get the "ranges" from the device tree
1134 for_each_of_range(&parser, &range) {
1136 unittest(range.flags == 0xf00baa,
1137 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1139 unittest(range.size == 0x100000,
1140 "for_each_of_range wrong size on node %pOF size=%llx\n",
1142 unittest(range.cpu_addr == 0xa0000000,
1143 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1144 range.cpu_addr, np);
1145 unittest(range.bus_addr == 0x0,
1146 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1147 range.pci_addr, np);
1149 unittest(range.flags == 0xf00bee,
1150 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1152 unittest(range.size == 0x200000,
1153 "for_each_of_range wrong size on node %pOF size=%llx\n",
1155 unittest(range.cpu_addr == 0xb0000000,
1156 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1157 range.cpu_addr, np);
1158 unittest(range.bus_addr == 0x100000000,
1159 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1160 range.pci_addr, np);
1168 static void __init of_unittest_reg(void)
1170 struct device_node *np;
1174 np = of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
1176 pr_err("missing testcase data\n");
1180 ret = of_property_read_reg(np, 0, &addr, &size);
1181 unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
1183 unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
1189 static void __init of_unittest_parse_interrupts(void)
1191 struct device_node *np;
1192 struct of_phandle_args args;
1195 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1198 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1200 pr_err("missing testcase data\n");
1204 for (i = 0; i < 4; i++) {
1207 memset(&args, 0, sizeof(args));
1208 rc = of_irq_parse_one(np, i, &args);
1211 passed &= (args.args_count == 1);
1212 passed &= (args.args[0] == (i + 1));
1214 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1219 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1221 pr_err("missing testcase data\n");
1225 for (i = 0; i < 4; i++) {
1228 memset(&args, 0, sizeof(args));
1229 rc = of_irq_parse_one(np, i, &args);
1231 /* Test the values from tests-phandle.dtsi */
1235 passed &= (args.args_count == 1);
1236 passed &= (args.args[0] == 9);
1240 passed &= (args.args_count == 3);
1241 passed &= (args.args[0] == 10);
1242 passed &= (args.args[1] == 11);
1243 passed &= (args.args[2] == 12);
1247 passed &= (args.args_count == 2);
1248 passed &= (args.args[0] == 13);
1249 passed &= (args.args[1] == 14);
1253 passed &= (args.args_count == 2);
1254 passed &= (args.args[0] == 15);
1255 passed &= (args.args[1] == 16);
1260 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1266 static void __init of_unittest_parse_interrupts_extended(void)
1268 struct device_node *np;
1269 struct of_phandle_args args;
1272 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1275 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1277 pr_err("missing testcase data\n");
1281 for (i = 0; i < 7; i++) {
1284 memset(&args, 0, sizeof(args));
1285 rc = of_irq_parse_one(np, i, &args);
1287 /* Test the values from tests-phandle.dtsi */
1291 passed &= (args.args_count == 1);
1292 passed &= (args.args[0] == 1);
1296 passed &= (args.args_count == 3);
1297 passed &= (args.args[0] == 2);
1298 passed &= (args.args[1] == 3);
1299 passed &= (args.args[2] == 4);
1303 passed &= (args.args_count == 2);
1304 passed &= (args.args[0] == 5);
1305 passed &= (args.args[1] == 6);
1309 passed &= (args.args_count == 1);
1310 passed &= (args.args[0] == 9);
1314 passed &= (args.args_count == 3);
1315 passed &= (args.args[0] == 10);
1316 passed &= (args.args[1] == 11);
1317 passed &= (args.args[2] == 12);
1321 passed &= (args.args_count == 2);
1322 passed &= (args.args[0] == 13);
1323 passed &= (args.args[1] == 14);
1327 * Tests child node that is missing property
1328 * #address-cells. See the comments in
1329 * drivers/of/unittest-data/tests-interrupts.dtsi
1330 * nodes intmap1 and interrupts-extended0
1333 passed &= (args.args_count == 1);
1334 passed &= (args.args[0] == 15);
1340 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1346 static const struct of_device_id match_node_table[] = {
1347 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1348 { .data = "B", .type = "type1", }, /* followed by type alone */
1350 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1351 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1352 { .data = "Cc", .name = "name2", .type = "type2", },
1354 { .data = "E", .compatible = "compat3" },
1355 { .data = "G", .compatible = "compat2", },
1356 { .data = "H", .compatible = "compat2", .name = "name5", },
1357 { .data = "I", .compatible = "compat2", .type = "type1", },
1358 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1359 { .data = "K", .compatible = "compat2", .name = "name9", },
1366 } match_node_tests[] = {
1367 { .path = "/testcase-data/match-node/name0", .data = "A", },
1368 { .path = "/testcase-data/match-node/name1", .data = "B", },
1369 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1370 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1371 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1372 { .path = "/testcase-data/match-node/name3", .data = "E", },
1373 { .path = "/testcase-data/match-node/name4", .data = "G", },
1374 { .path = "/testcase-data/match-node/name5", .data = "H", },
1375 { .path = "/testcase-data/match-node/name6", .data = "G", },
1376 { .path = "/testcase-data/match-node/name7", .data = "I", },
1377 { .path = "/testcase-data/match-node/name8", .data = "J", },
1378 { .path = "/testcase-data/match-node/name9", .data = "K", },
1381 static void __init of_unittest_match_node(void)
1383 struct device_node *np;
1384 const struct of_device_id *match;
1387 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1388 np = of_find_node_by_path(match_node_tests[i].path);
1390 unittest(0, "missing testcase node %s\n",
1391 match_node_tests[i].path);
1395 match = of_match_node(match_node_table, np);
1397 unittest(0, "%s didn't match anything\n",
1398 match_node_tests[i].path);
1402 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1403 unittest(0, "%s got wrong match. expected %s, got %s\n",
1404 match_node_tests[i].path, match_node_tests[i].data,
1405 (const char *)match->data);
1408 unittest(1, "passed");
1412 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1413 static const struct platform_device_info test_bus_info = {
1414 .name = "unittest-bus",
1416 static void __init of_unittest_platform_populate(void)
1419 struct device_node *np, *child, *grandchild;
1420 struct platform_device *pdev, *test_bus;
1421 const struct of_device_id match[] = {
1422 { .compatible = "test-device", },
1426 np = of_find_node_by_path("/testcase-data");
1427 of_platform_default_populate(np, NULL, NULL);
1429 /* Test that a missing irq domain returns -EPROBE_DEFER */
1430 np = of_find_node_by_path("/testcase-data/testcase-device1");
1431 pdev = of_find_device_by_node(np);
1432 unittest(pdev, "device 1 creation failed\n");
1434 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1435 irq = platform_get_irq(pdev, 0);
1436 unittest(irq == -EPROBE_DEFER,
1437 "device deferred probe failed - %d\n", irq);
1439 /* Test that a parsing failure does not return -EPROBE_DEFER */
1440 np = of_find_node_by_path("/testcase-data/testcase-device2");
1441 pdev = of_find_device_by_node(np);
1442 unittest(pdev, "device 2 creation failed\n");
1444 EXPECT_BEGIN(KERN_INFO,
1445 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1447 irq = platform_get_irq(pdev, 0);
1449 EXPECT_END(KERN_INFO,
1450 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1452 unittest(irq < 0 && irq != -EPROBE_DEFER,
1453 "device parsing error failed - %d\n", irq);
1456 np = of_find_node_by_path("/testcase-data/platform-tests");
1457 unittest(np, "No testcase data in device tree\n");
1461 test_bus = platform_device_register_full(&test_bus_info);
1462 rc = PTR_ERR_OR_ZERO(test_bus);
1463 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1468 test_bus->dev.of_node = np;
1471 * Add a dummy resource to the test bus node after it is
1472 * registered to catch problems with un-inserted resources. The
1473 * DT code doesn't insert the resources, and it has caused the
1474 * kernel to oops in the past. This makes sure the same bug
1475 * doesn't crop up again.
1477 platform_device_add_resources(test_bus, &test_bus_res, 1);
1479 of_platform_populate(np, match, NULL, &test_bus->dev);
1480 for_each_child_of_node(np, child) {
1481 for_each_child_of_node(child, grandchild) {
1482 pdev = of_find_device_by_node(grandchild);
1484 "Could not create device for node '%pOFn'\n",
1486 platform_device_put(pdev);
1490 of_platform_depopulate(&test_bus->dev);
1491 for_each_child_of_node(np, child) {
1492 for_each_child_of_node(child, grandchild)
1493 unittest(!of_find_device_by_node(grandchild),
1494 "device didn't get destroyed '%pOFn'\n",
1498 platform_device_unregister(test_bus);
1503 * update_node_properties - adds the properties
1504 * of np into dup node (present in live tree) and
1505 * updates parent of children of np to dup.
1507 * @np: node whose properties are being added to the live tree
1508 * @dup: node present in live tree to be updated
1510 static void update_node_properties(struct device_node *np,
1511 struct device_node *dup)
1513 struct property *prop;
1514 struct property *save_next;
1515 struct device_node *child;
1518 for_each_child_of_node(np, child)
1519 child->parent = dup;
1522 * "unittest internal error: unable to add testdata property"
1524 * If this message reports a property in node '/__symbols__' then
1525 * the respective unittest overlay contains a label that has the
1526 * same name as a label in the live devicetree. The label will
1527 * be in the live devicetree only if the devicetree source was
1528 * compiled with the '-@' option. If you encounter this error,
1529 * please consider renaming __all__ of the labels in the unittest
1530 * overlay dts files with an odd prefix that is unlikely to be
1531 * used in a real devicetree.
1535 * open code for_each_property_of_node() because of_add_property()
1536 * sets prop->next to NULL
1538 for (prop = np->properties; prop != NULL; prop = save_next) {
1539 save_next = prop->next;
1540 ret = of_add_property(dup, prop);
1542 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1544 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1551 * attach_node_and_children - attaches nodes
1552 * and its children to live tree.
1553 * CAUTION: misleading function name - if node @np already exists in
1554 * the live tree then children of @np are *not* attached to the live
1555 * tree. This works for the current test devicetree nodes because such
1556 * nodes do not have child nodes.
1558 * @np: Node to attach to live tree
1560 static void attach_node_and_children(struct device_node *np)
1562 struct device_node *next, *dup, *child;
1563 unsigned long flags;
1564 const char *full_name;
1566 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1570 if (!strcmp(full_name, "/__local_fixups__") ||
1571 !strcmp(full_name, "/__fixups__")) {
1576 dup = of_find_node_by_path(full_name);
1579 update_node_properties(np, dup);
1586 mutex_lock(&of_mutex);
1587 raw_spin_lock_irqsave(&devtree_lock, flags);
1588 np->sibling = np->parent->child;
1589 np->parent->child = np;
1590 of_node_clear_flag(np, OF_DETACHED);
1591 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1593 __of_attach_node_sysfs(np);
1594 mutex_unlock(&of_mutex);
1597 next = child->sibling;
1598 attach_node_and_children(child);
1604 * unittest_data_add - Reads, copies data from
1605 * linked tree and attaches it to the live tree
1607 static int __init unittest_data_add(void)
1609 void *unittest_data;
1610 void *unittest_data_align;
1611 struct device_node *unittest_data_node = NULL, *np;
1613 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1614 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
1616 extern uint8_t __dtbo_testcases_begin[];
1617 extern uint8_t __dtbo_testcases_end[];
1618 const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
1623 pr_warn("%s: testcases is empty\n", __func__);
1628 unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
1632 unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1633 memcpy(unittest_data_align, __dtbo_testcases_begin, size);
1635 ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
1637 pr_warn("%s: unflatten testcases tree failed\n", __func__);
1638 kfree(unittest_data);
1641 if (!unittest_data_node) {
1642 pr_warn("%s: testcases tree is empty\n", __func__);
1643 kfree(unittest_data);
1648 * This lock normally encloses of_resolve_phandles()
1650 of_overlay_mutex_lock();
1652 rc = of_resolve_phandles(unittest_data_node);
1654 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1655 of_overlay_mutex_unlock();
1660 of_root = unittest_data_node;
1661 for_each_of_allnodes(np)
1662 __of_attach_node_sysfs(np);
1663 of_aliases = of_find_node_by_path("/aliases");
1664 of_chosen = of_find_node_by_path("/chosen");
1665 of_overlay_mutex_unlock();
1669 EXPECT_BEGIN(KERN_INFO,
1670 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1672 /* attach the sub-tree to live tree */
1673 np = unittest_data_node->child;
1675 struct device_node *next = np->sibling;
1677 np->parent = of_root;
1678 /* this will clear OF_DETACHED in np and children */
1679 attach_node_and_children(np);
1683 EXPECT_END(KERN_INFO,
1684 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1686 of_overlay_mutex_unlock();
1691 #ifdef CONFIG_OF_OVERLAY
1692 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
1694 static int unittest_probe(struct platform_device *pdev)
1696 struct device *dev = &pdev->dev;
1697 struct device_node *np = dev->of_node;
1700 dev_err(dev, "No OF data for device\n");
1705 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1707 of_platform_populate(np, NULL, NULL, &pdev->dev);
1712 static void unittest_remove(struct platform_device *pdev)
1714 struct device *dev = &pdev->dev;
1715 struct device_node *np = dev->of_node;
1717 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1720 static const struct of_device_id unittest_match[] = {
1721 { .compatible = "unittest", },
1725 static struct platform_driver unittest_driver = {
1726 .probe = unittest_probe,
1727 .remove_new = unittest_remove,
1730 .of_match_table = unittest_match,
1734 /* get the platform device instantiated at the path */
1735 static struct platform_device *of_path_to_platform_device(const char *path)
1737 struct device_node *np;
1738 struct platform_device *pdev;
1740 np = of_find_node_by_path(path);
1744 pdev = of_find_device_by_node(np);
1750 /* find out if a platform device exists at that path */
1751 static int of_path_platform_device_exists(const char *path)
1753 struct platform_device *pdev;
1755 pdev = of_path_to_platform_device(path);
1756 platform_device_put(pdev);
1757 return pdev != NULL;
1760 #ifdef CONFIG_OF_GPIO
1762 struct unittest_gpio_dev {
1763 struct gpio_chip chip;
1766 static int unittest_gpio_chip_request_count;
1767 static int unittest_gpio_probe_count;
1768 static int unittest_gpio_probe_pass_count;
1770 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1772 unittest_gpio_chip_request_count++;
1774 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1775 unittest_gpio_chip_request_count);
1779 static int unittest_gpio_probe(struct platform_device *pdev)
1781 struct unittest_gpio_dev *devptr;
1784 unittest_gpio_probe_count++;
1786 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1790 platform_set_drvdata(pdev, devptr);
1792 devptr->chip.fwnode = dev_fwnode(&pdev->dev);
1793 devptr->chip.label = "of-unittest-gpio";
1794 devptr->chip.base = -1; /* dynamic allocation */
1795 devptr->chip.ngpio = 5;
1796 devptr->chip.request = unittest_gpio_chip_request;
1798 ret = gpiochip_add_data(&devptr->chip, NULL);
1801 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
1804 unittest_gpio_probe_pass_count++;
1808 static void unittest_gpio_remove(struct platform_device *pdev)
1810 struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
1811 struct device *dev = &pdev->dev;
1813 dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
1815 if (devptr->chip.base != -1)
1816 gpiochip_remove(&devptr->chip);
1821 static const struct of_device_id unittest_gpio_id[] = {
1822 { .compatible = "unittest-gpio", },
1826 static struct platform_driver unittest_gpio_driver = {
1827 .probe = unittest_gpio_probe,
1828 .remove_new = unittest_gpio_remove,
1830 .name = "unittest-gpio",
1831 .of_match_table = unittest_gpio_id,
1835 static void __init of_unittest_overlay_gpio(void)
1837 int chip_request_count;
1838 int probe_pass_count;
1842 * tests: apply overlays before registering driver
1843 * Similar to installing a driver as a module, the
1844 * driver is registered after applying the overlays.
1846 * The overlays are applied by overlay_data_apply()
1847 * instead of of_unittest_apply_overlay() so that they
1848 * will not be tracked. Thus they will not be removed
1849 * by of_unittest_remove_tracked_overlays().
1851 * - apply overlay_gpio_01
1852 * - apply overlay_gpio_02a
1853 * - apply overlay_gpio_02b
1856 * register driver will result in
1857 * - probe and processing gpio hog for overlay_gpio_01
1858 * - probe for overlay_gpio_02a
1859 * - processing gpio for overlay_gpio_02b
1862 probe_pass_count = unittest_gpio_probe_pass_count;
1863 chip_request_count = unittest_gpio_chip_request_count;
1866 * overlay_gpio_01 contains gpio node and child gpio hog node
1867 * overlay_gpio_02a contains gpio node
1868 * overlay_gpio_02b contains child gpio hog node
1871 unittest(overlay_data_apply("overlay_gpio_01", NULL),
1872 "Adding overlay 'overlay_gpio_01' failed\n");
1874 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1875 "Adding overlay 'overlay_gpio_02a' failed\n");
1877 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1878 "Adding overlay 'overlay_gpio_02b' failed\n");
1880 ret = platform_driver_register(&unittest_gpio_driver);
1881 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1884 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1885 "unittest_gpio_probe() failed or not called\n");
1887 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1888 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1889 unittest_gpio_chip_request_count - chip_request_count);
1892 * tests: apply overlays after registering driver
1894 * Similar to a driver built-in to the kernel, the
1895 * driver is registered before applying the overlays.
1897 * overlay_gpio_03 contains gpio node and child gpio hog node
1899 * - apply overlay_gpio_03
1901 * apply overlay will result in
1902 * - probe and processing gpio hog.
1905 probe_pass_count = unittest_gpio_probe_pass_count;
1906 chip_request_count = unittest_gpio_chip_request_count;
1908 /* overlay_gpio_03 contains gpio node and child gpio hog node */
1910 unittest(overlay_data_apply("overlay_gpio_03", NULL),
1911 "Adding overlay 'overlay_gpio_03' failed\n");
1913 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1914 "unittest_gpio_probe() failed or not called\n");
1916 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1917 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1918 unittest_gpio_chip_request_count - chip_request_count);
1921 * overlay_gpio_04a contains gpio node
1923 * - apply overlay_gpio_04a
1925 * apply the overlay will result in
1926 * - probe for overlay_gpio_04a
1929 probe_pass_count = unittest_gpio_probe_pass_count;
1930 chip_request_count = unittest_gpio_chip_request_count;
1932 /* overlay_gpio_04a contains gpio node */
1934 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
1935 "Adding overlay 'overlay_gpio_04a' failed\n");
1937 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1938 "unittest_gpio_probe() failed or not called\n");
1941 * overlay_gpio_04b contains child gpio hog node
1943 * - apply overlay_gpio_04b
1945 * apply the overlay will result in
1946 * - processing gpio for overlay_gpio_04b
1949 /* overlay_gpio_04b contains child gpio hog node */
1951 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
1952 "Adding overlay 'overlay_gpio_04b' failed\n");
1954 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1955 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1956 unittest_gpio_chip_request_count - chip_request_count);
1961 static void __init of_unittest_overlay_gpio(void)
1968 #if IS_BUILTIN(CONFIG_I2C)
1970 /* get the i2c client device instantiated at the path */
1971 static struct i2c_client *of_path_to_i2c_client(const char *path)
1973 struct device_node *np;
1974 struct i2c_client *client;
1976 np = of_find_node_by_path(path);
1980 client = of_find_i2c_device_by_node(np);
1986 /* find out if a i2c client device exists at that path */
1987 static int of_path_i2c_client_exists(const char *path)
1989 struct i2c_client *client;
1991 client = of_path_to_i2c_client(path);
1993 put_device(&client->dev);
1994 return client != NULL;
1997 static int of_path_i2c_client_exists(const char *path)
2008 static int of_path_device_type_exists(const char *path,
2009 enum overlay_type ovtype)
2013 return of_path_platform_device_exists(path);
2015 return of_path_i2c_client_exists(path);
2020 static const char *unittest_path(int nr, enum overlay_type ovtype)
2023 static char buf[256];
2027 base = "/testcase-data/overlay-node/test-bus";
2030 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2036 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2037 buf[sizeof(buf) - 1] = '\0';
2041 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2045 path = unittest_path(unittest_nr, ovtype);
2049 return of_path_platform_device_exists(path);
2051 return of_path_i2c_client_exists(path);
2056 static const char *overlay_name_from_nr(int nr)
2058 static char buf[256];
2060 snprintf(buf, sizeof(buf) - 1,
2062 buf[sizeof(buf) - 1] = '\0';
2067 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2069 #define MAX_TRACK_OVCS_IDS 256
2071 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2072 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2073 static int track_ovcs_id_cnt;
2075 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2077 if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2080 track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2081 track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2082 track_ovcs_id_cnt++;
2085 static void of_unittest_untrack_overlay(int ovcs_id)
2087 if (WARN_ON(track_ovcs_id_cnt < 1))
2090 track_ovcs_id_cnt--;
2092 /* If out of synch then test is broken. Do not try to recover. */
2093 WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2096 static void of_unittest_remove_tracked_overlays(void)
2098 int ret, ovcs_id, overlay_nr, save_ovcs_id;
2099 const char *overlay_name;
2101 while (track_ovcs_id_cnt > 0) {
2103 ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2104 overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2105 save_ovcs_id = ovcs_id;
2106 ret = of_overlay_remove(&ovcs_id);
2107 if (ret == -ENODEV) {
2108 overlay_name = overlay_name_from_nr(overlay_nr);
2109 pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2110 __func__, overlay_name, ret);
2112 of_unittest_untrack_overlay(save_ovcs_id);
2117 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2120 * The overlay will be tracked, thus it will be removed
2121 * by of_unittest_remove_tracked_overlays().
2124 const char *overlay_name;
2126 overlay_name = overlay_name_from_nr(overlay_nr);
2128 if (!overlay_data_apply(overlay_name, ovcs_id)) {
2129 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2132 of_unittest_track_overlay(*ovcs_id, overlay_nr);
2137 static int __init __of_unittest_apply_overlay_check(int overlay_nr,
2138 int unittest_nr, int before, int after,
2139 enum overlay_type ovtype)
2143 /* unittest device must be in before state */
2144 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2145 unittest(0, "%s with device @\"%s\" %s\n",
2146 overlay_name_from_nr(overlay_nr),
2147 unittest_path(unittest_nr, ovtype),
2148 !before ? "enabled" : "disabled");
2152 /* apply the overlay */
2154 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2156 /* of_unittest_apply_overlay already called unittest() */
2160 /* unittest device must be in after state */
2161 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2162 unittest(0, "%s with device @\"%s\" %s\n",
2163 overlay_name_from_nr(overlay_nr),
2164 unittest_path(unittest_nr, ovtype),
2165 !after ? "enabled" : "disabled");
2172 /* apply an overlay while checking before and after states */
2173 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2174 int unittest_nr, int before, int after,
2175 enum overlay_type ovtype)
2177 int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
2178 unittest_nr, before, after, ovtype);
2185 /* apply an overlay and then revert it while checking before, after states */
2186 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2187 int unittest_nr, int before, int after,
2188 enum overlay_type ovtype)
2190 int ret, ovcs_id, save_ovcs_id;
2192 ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
2193 before, after, ovtype);
2197 /* remove the overlay */
2198 save_ovcs_id = ovcs_id;
2199 ret = of_overlay_remove(&ovcs_id);
2201 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2202 overlay_name_from_nr(overlay_nr),
2203 unittest_path(unittest_nr, ovtype));
2206 of_unittest_untrack_overlay(save_ovcs_id);
2208 /* unittest device must be again in before state */
2209 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2210 unittest(0, "%s with device @\"%s\" %s\n",
2211 overlay_name_from_nr(overlay_nr),
2212 unittest_path(unittest_nr, ovtype),
2213 !before ? "enabled" : "disabled");
2220 /* test activation of device */
2221 static void __init of_unittest_overlay_0(void)
2225 EXPECT_BEGIN(KERN_INFO,
2226 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2228 /* device should enable */
2229 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2231 EXPECT_END(KERN_INFO,
2232 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2237 unittest(1, "overlay test %d passed\n", 0);
2240 /* test deactivation of device */
2241 static void __init of_unittest_overlay_1(void)
2245 EXPECT_BEGIN(KERN_INFO,
2246 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2248 /* device should disable */
2249 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2251 EXPECT_END(KERN_INFO,
2252 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2257 unittest(1, "overlay test %d passed\n", 1);
2261 /* test activation of device */
2262 static void __init of_unittest_overlay_2(void)
2266 EXPECT_BEGIN(KERN_INFO,
2267 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2269 /* device should enable */
2270 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2272 EXPECT_END(KERN_INFO,
2273 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2277 unittest(1, "overlay test %d passed\n", 2);
2280 /* test deactivation of device */
2281 static void __init of_unittest_overlay_3(void)
2285 EXPECT_BEGIN(KERN_INFO,
2286 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2288 /* device should disable */
2289 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2291 EXPECT_END(KERN_INFO,
2292 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2297 unittest(1, "overlay test %d passed\n", 3);
2300 /* test activation of a full device node */
2301 static void __init of_unittest_overlay_4(void)
2303 /* device should disable */
2304 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2307 unittest(1, "overlay test %d passed\n", 4);
2310 /* test overlay apply/revert sequence */
2311 static void __init of_unittest_overlay_5(void)
2315 EXPECT_BEGIN(KERN_INFO,
2316 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2318 /* device should disable */
2319 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2321 EXPECT_END(KERN_INFO,
2322 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2327 unittest(1, "overlay test %d passed\n", 5);
2330 /* test overlay application in sequence */
2331 static void __init of_unittest_overlay_6(void)
2333 int i, save_ovcs_id[2], ovcs_id;
2334 int overlay_nr = 6, unittest_nr = 6;
2335 int before = 0, after = 1;
2336 const char *overlay_name;
2340 /* unittest device must be in before state */
2341 for (i = 0; i < 2; i++) {
2342 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2344 unittest(0, "%s with device @\"%s\" %s\n",
2345 overlay_name_from_nr(overlay_nr + i),
2346 unittest_path(unittest_nr + i,
2348 !before ? "enabled" : "disabled");
2353 /* apply the overlays */
2355 EXPECT_BEGIN(KERN_INFO,
2356 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2358 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2360 ret = overlay_data_apply(overlay_name, &ovcs_id);
2363 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2366 save_ovcs_id[0] = ovcs_id;
2367 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2369 EXPECT_END(KERN_INFO,
2370 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2372 EXPECT_BEGIN(KERN_INFO,
2373 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2375 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2377 ret = overlay_data_apply(overlay_name, &ovcs_id);
2380 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2383 save_ovcs_id[1] = ovcs_id;
2384 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2386 EXPECT_END(KERN_INFO,
2387 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2390 for (i = 0; i < 2; i++) {
2391 /* unittest device must be in after state */
2392 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2394 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2395 overlay_name_from_nr(overlay_nr + i),
2396 unittest_path(unittest_nr + i,
2398 !after ? "enabled" : "disabled");
2403 for (i = 1; i >= 0; i--) {
2404 ovcs_id = save_ovcs_id[i];
2405 if (of_overlay_remove(&ovcs_id)) {
2406 unittest(0, "%s failed destroy @\"%s\"\n",
2407 overlay_name_from_nr(overlay_nr + i),
2408 unittest_path(unittest_nr + i,
2412 of_unittest_untrack_overlay(save_ovcs_id[i]);
2415 for (i = 0; i < 2; i++) {
2416 /* unittest device must be again in before state */
2417 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2419 unittest(0, "%s with device @\"%s\" %s\n",
2420 overlay_name_from_nr(overlay_nr + i),
2421 unittest_path(unittest_nr + i,
2423 !before ? "enabled" : "disabled");
2428 unittest(1, "overlay test %d passed\n", 6);
2432 /* test overlay application in sequence */
2433 static void __init of_unittest_overlay_8(void)
2435 int i, save_ovcs_id[2], ovcs_id;
2436 int overlay_nr = 8, unittest_nr = 8;
2437 const char *overlay_name;
2440 /* we don't care about device state in this test */
2442 EXPECT_BEGIN(KERN_INFO,
2443 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2445 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2447 ret = overlay_data_apply(overlay_name, &ovcs_id);
2449 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2451 EXPECT_END(KERN_INFO,
2452 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2457 save_ovcs_id[0] = ovcs_id;
2458 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2460 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2462 EXPECT_BEGIN(KERN_INFO,
2463 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2465 /* apply the overlays */
2466 ret = overlay_data_apply(overlay_name, &ovcs_id);
2468 EXPECT_END(KERN_INFO,
2469 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2472 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2476 save_ovcs_id[1] = ovcs_id;
2477 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2479 /* now try to remove first overlay (it should fail) */
2480 ovcs_id = save_ovcs_id[0];
2482 EXPECT_BEGIN(KERN_INFO,
2483 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2485 EXPECT_BEGIN(KERN_INFO,
2486 "OF: overlay: overlay #6 is not topmost");
2488 ret = of_overlay_remove(&ovcs_id);
2490 EXPECT_END(KERN_INFO,
2491 "OF: overlay: overlay #6 is not topmost");
2493 EXPECT_END(KERN_INFO,
2494 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2498 * Should never get here. If we do, expect a lot of
2499 * subsequent tracking and overlay removal related errors.
2501 unittest(0, "%s was destroyed @\"%s\"\n",
2502 overlay_name_from_nr(overlay_nr + 0),
2503 unittest_path(unittest_nr,
2508 /* removing them in order should work */
2509 for (i = 1; i >= 0; i--) {
2510 ovcs_id = save_ovcs_id[i];
2511 if (of_overlay_remove(&ovcs_id)) {
2512 unittest(0, "%s not destroyed @\"%s\"\n",
2513 overlay_name_from_nr(overlay_nr + i),
2514 unittest_path(unittest_nr,
2518 of_unittest_untrack_overlay(save_ovcs_id[i]);
2521 unittest(1, "overlay test %d passed\n", 8);
2524 /* test insertion of a bus with parent devices */
2525 static void __init of_unittest_overlay_10(void)
2530 /* device should disable */
2531 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2533 if (unittest(ret == 0,
2534 "overlay test %d failed; overlay application\n", 10))
2537 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2538 unittest_path(10, PDEV_OVERLAY));
2539 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2542 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2545 unittest(ret, "overlay test %d failed; no child device\n", 10);
2548 /* test insertion of a bus with parent devices (and revert) */
2549 static void __init of_unittest_overlay_11(void)
2553 /* device should disable */
2554 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2557 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2560 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2562 struct unittest_i2c_bus_data {
2563 struct platform_device *pdev;
2564 struct i2c_adapter adap;
2567 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2568 struct i2c_msg *msgs, int num)
2570 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2577 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2579 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2582 static const struct i2c_algorithm unittest_i2c_algo = {
2583 .master_xfer = unittest_i2c_master_xfer,
2584 .functionality = unittest_i2c_functionality,
2587 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2589 struct device *dev = &pdev->dev;
2590 struct device_node *np = dev->of_node;
2591 struct unittest_i2c_bus_data *std;
2592 struct i2c_adapter *adap;
2596 dev_err(dev, "No OF data for device\n");
2601 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2603 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2607 /* link them together */
2609 platform_set_drvdata(pdev, std);
2612 i2c_set_adapdata(adap, std);
2614 strscpy(adap->name, pdev->name, sizeof(adap->name));
2615 adap->class = I2C_CLASS_DEPRECATED;
2616 adap->algo = &unittest_i2c_algo;
2617 adap->dev.parent = dev;
2618 adap->dev.of_node = dev->of_node;
2619 adap->timeout = 5 * HZ;
2622 ret = i2c_add_numbered_adapter(adap);
2624 dev_err(dev, "Failed to add I2C adapter\n");
2631 static void unittest_i2c_bus_remove(struct platform_device *pdev)
2633 struct device *dev = &pdev->dev;
2634 struct device_node *np = dev->of_node;
2635 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2637 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2638 i2c_del_adapter(&std->adap);
2641 static const struct of_device_id unittest_i2c_bus_match[] = {
2642 { .compatible = "unittest-i2c-bus", },
2646 static struct platform_driver unittest_i2c_bus_driver = {
2647 .probe = unittest_i2c_bus_probe,
2648 .remove_new = unittest_i2c_bus_remove,
2650 .name = "unittest-i2c-bus",
2651 .of_match_table = unittest_i2c_bus_match,
2655 static int unittest_i2c_dev_probe(struct i2c_client *client)
2657 struct device *dev = &client->dev;
2658 struct device_node *np = client->dev.of_node;
2661 dev_err(dev, "No OF node\n");
2665 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2670 static void unittest_i2c_dev_remove(struct i2c_client *client)
2672 struct device *dev = &client->dev;
2673 struct device_node *np = client->dev.of_node;
2675 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2678 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2679 { .name = "unittest-i2c-dev" },
2683 static struct i2c_driver unittest_i2c_dev_driver = {
2685 .name = "unittest-i2c-dev",
2687 .probe = unittest_i2c_dev_probe,
2688 .remove = unittest_i2c_dev_remove,
2689 .id_table = unittest_i2c_dev_id,
2692 #if IS_BUILTIN(CONFIG_I2C_MUX)
2694 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2699 static int unittest_i2c_mux_probe(struct i2c_client *client)
2702 struct device *dev = &client->dev;
2703 struct i2c_adapter *adap = client->adapter;
2704 struct device_node *np = client->dev.of_node, *child;
2705 struct i2c_mux_core *muxc;
2708 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2711 dev_err(dev, "No OF node\n");
2716 for_each_child_of_node(np, child) {
2717 if (of_property_read_u32(child, "reg", ®))
2719 if (max_reg == (u32)-1 || reg > max_reg)
2722 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2724 dev_err(dev, "No channels\n");
2728 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2729 unittest_i2c_mux_select_chan, NULL);
2732 for (i = 0; i < nchans; i++) {
2733 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2734 dev_err(dev, "Failed to register mux #%d\n", i);
2735 i2c_mux_del_adapters(muxc);
2740 i2c_set_clientdata(client, muxc);
2745 static void unittest_i2c_mux_remove(struct i2c_client *client)
2747 struct device *dev = &client->dev;
2748 struct device_node *np = client->dev.of_node;
2749 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2751 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2752 i2c_mux_del_adapters(muxc);
2755 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2756 { .name = "unittest-i2c-mux" },
2760 static struct i2c_driver unittest_i2c_mux_driver = {
2762 .name = "unittest-i2c-mux",
2764 .probe = unittest_i2c_mux_probe,
2765 .remove = unittest_i2c_mux_remove,
2766 .id_table = unittest_i2c_mux_id,
2771 static int of_unittest_overlay_i2c_init(void)
2775 ret = i2c_add_driver(&unittest_i2c_dev_driver);
2776 if (unittest(ret == 0,
2777 "could not register unittest i2c device driver\n"))
2780 ret = platform_driver_register(&unittest_i2c_bus_driver);
2782 if (unittest(ret == 0,
2783 "could not register unittest i2c bus driver\n"))
2786 #if IS_BUILTIN(CONFIG_I2C_MUX)
2788 EXPECT_BEGIN(KERN_INFO,
2789 "i2c i2c-1: Added multiplexed i2c bus 2");
2791 ret = i2c_add_driver(&unittest_i2c_mux_driver);
2793 EXPECT_END(KERN_INFO,
2794 "i2c i2c-1: Added multiplexed i2c bus 2");
2796 if (unittest(ret == 0,
2797 "could not register unittest i2c mux driver\n"))
2804 static void of_unittest_overlay_i2c_cleanup(void)
2806 #if IS_BUILTIN(CONFIG_I2C_MUX)
2807 i2c_del_driver(&unittest_i2c_mux_driver);
2809 platform_driver_unregister(&unittest_i2c_bus_driver);
2810 i2c_del_driver(&unittest_i2c_dev_driver);
2813 static void __init of_unittest_overlay_i2c_12(void)
2817 /* device should enable */
2818 EXPECT_BEGIN(KERN_INFO,
2819 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2821 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2823 EXPECT_END(KERN_INFO,
2824 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2829 unittest(1, "overlay test %d passed\n", 12);
2832 /* test deactivation of device */
2833 static void __init of_unittest_overlay_i2c_13(void)
2837 EXPECT_BEGIN(KERN_INFO,
2838 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2840 /* device should disable */
2841 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2843 EXPECT_END(KERN_INFO,
2844 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2849 unittest(1, "overlay test %d passed\n", 13);
2852 /* just check for i2c mux existence */
2853 static void of_unittest_overlay_i2c_14(void)
2857 static void __init of_unittest_overlay_i2c_15(void)
2861 /* device should enable */
2862 EXPECT_BEGIN(KERN_INFO,
2863 "i2c i2c-1: Added multiplexed i2c bus 3");
2865 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2867 EXPECT_END(KERN_INFO,
2868 "i2c i2c-1: Added multiplexed i2c bus 3");
2873 unittest(1, "overlay test %d passed\n", 15);
2878 static inline void of_unittest_overlay_i2c_14(void) { }
2879 static inline void of_unittest_overlay_i2c_15(void) { }
2883 static int of_notify(struct notifier_block *nb, unsigned long action,
2886 struct of_overlay_notify_data *nd = arg;
2887 struct device_node *found;
2891 * For overlay_16 .. overlay_19, check that returning an error
2892 * works for each of the actions by setting an arbitrary return
2893 * error number that matches the test number. e.g. for unittest16,
2894 * ret = -EBUSY which is -16.
2896 * OVERLAY_INFO() for the overlays is declared to expect the same
2897 * error number, so overlay_data_apply() will return no error.
2899 * overlay_20 will return NOTIFY_DONE
2903 of_node_get(nd->overlay);
2907 case OF_OVERLAY_PRE_APPLY:
2908 found = of_find_node_by_name(nd->overlay, "test-unittest16");
2915 case OF_OVERLAY_POST_APPLY:
2916 found = of_find_node_by_name(nd->overlay, "test-unittest17");
2923 case OF_OVERLAY_PRE_REMOVE:
2924 found = of_find_node_by_name(nd->overlay, "test-unittest18");
2931 case OF_OVERLAY_POST_REMOVE:
2932 found = of_find_node_by_name(nd->overlay, "test-unittest19");
2939 default: /* should not happen */
2940 of_node_put(nd->overlay);
2946 return notifier_from_errno(ret);
2951 static struct notifier_block of_nb = {
2952 .notifier_call = of_notify,
2955 static void __init of_unittest_overlay_notify(void)
2960 ret = of_overlay_notifier_register(&of_nb);
2962 "of_overlay_notifier_register() failed, ret = %d\n", ret);
2967 * The overlays are applied by overlay_data_apply()
2968 * instead of of_unittest_apply_overlay() so that they
2969 * will not be tracked. Thus they will not be removed
2970 * by of_unittest_remove_tracked_overlays().
2972 * Applying overlays 16 - 19 will each trigger an error for a
2973 * different action in of_notify().
2975 * Applying overlay 20 will not trigger any error in of_notify().
2978 /* --- overlay 16 --- */
2980 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
2982 unittest(overlay_data_apply("overlay_16", &ovcs_id),
2983 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
2985 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
2987 unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
2989 /* --- overlay 17 --- */
2991 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
2993 unittest(overlay_data_apply("overlay_17", &ovcs_id),
2994 "test OF_OVERLAY_POST_APPLY notify injected error\n");
2996 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
2998 unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
3000 /* --- overlay 18 --- */
3002 unittest(overlay_data_apply("overlay_18", &ovcs_id),
3003 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3005 unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
3008 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3010 ret = of_overlay_remove(&ovcs_id);
3011 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3012 if (ret == -EXDEV) {
3014 * change set ovcs_id should still exist
3016 unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3018 unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3021 unittest(1, "ovcs_id not created for overlay_18\n");
3024 unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3026 /* --- overlay 19 --- */
3028 unittest(overlay_data_apply("overlay_19", &ovcs_id),
3029 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3031 unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3034 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3035 ret = of_overlay_remove(&ovcs_id);
3036 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3038 unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3040 unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3042 unittest(1, "ovcs_id removed for overlay_19\n");
3045 unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3048 /* --- overlay 20 --- */
3050 unittest(overlay_data_apply("overlay_20", &ovcs_id),
3051 "overlay notify no injected error\n");
3054 ret = of_overlay_remove(&ovcs_id);
3056 unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3059 unittest(1, "ovcs_id not created for overlay_20\n");
3062 unittest(!of_overlay_notifier_unregister(&of_nb),
3063 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3066 static void __init of_unittest_overlay(void)
3068 struct device_node *bus_np = NULL;
3071 if (platform_driver_register(&unittest_driver)) {
3072 unittest(0, "could not register unittest driver\n");
3076 bus_np = of_find_node_by_path(bus_path);
3077 if (bus_np == NULL) {
3078 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3082 if (of_platform_default_populate(bus_np, NULL, NULL)) {
3083 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3087 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3088 unittest(0, "could not find unittest0 @ \"%s\"\n",
3089 unittest_path(100, PDEV_OVERLAY));
3093 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3094 unittest(0, "unittest1 @ \"%s\" should not exist\n",
3095 unittest_path(101, PDEV_OVERLAY));
3099 unittest(1, "basic infrastructure of overlays passed");
3101 /* tests in sequence */
3102 of_unittest_overlay_0();
3103 of_unittest_overlay_1();
3104 of_unittest_overlay_2();
3105 of_unittest_overlay_3();
3106 of_unittest_overlay_4();
3107 for (i = 0; i < 3; i++)
3108 of_unittest_overlay_5();
3109 of_unittest_overlay_6();
3110 of_unittest_overlay_8();
3112 of_unittest_overlay_10();
3113 of_unittest_overlay_11();
3115 #if IS_BUILTIN(CONFIG_I2C)
3116 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3119 of_unittest_overlay_i2c_12();
3120 of_unittest_overlay_i2c_13();
3121 of_unittest_overlay_i2c_14();
3122 of_unittest_overlay_i2c_15();
3124 of_unittest_overlay_i2c_cleanup();
3127 of_unittest_overlay_gpio();
3129 of_unittest_remove_tracked_overlays();
3131 of_unittest_overlay_notify();
3134 of_node_put(bus_np);
3138 static inline void __init of_unittest_overlay(void) { }
3141 static void __init of_unittest_lifecycle(void)
3143 #ifdef CONFIG_OF_DYNAMIC
3144 unsigned int refcount;
3145 int found_refcount_one = 0;
3147 struct device_node *np;
3148 struct device_node *prev_sibling, *next_sibling;
3149 const char *refcount_path = "/testcase-data/refcount-node";
3150 const char *refcount_parent_path = "/testcase-data";
3153 * Node lifecycle tests, non-dynamic node:
3155 * - Decrementing refcount to zero via of_node_put() should cause the
3156 * attempt to free the node memory by of_node_release() to fail
3157 * because the node is not a dynamic node.
3159 * - Decrementing refcount past zero should result in additional
3163 np = of_find_node_by_path(refcount_path);
3164 unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3166 goto out_skip_tests;
3168 while (!found_refcount_one) {
3170 if (put_count++ > 10) {
3171 unittest(0, "guardrail to avoid infinite loop\n");
3172 goto out_skip_tests;
3175 refcount = kref_read(&np->kobj.kref);
3177 found_refcount_one = 1;
3182 EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3185 * refcount is now one, decrementing to zero will result in a call to
3186 * of_node_release() to free the node's memory, which should result
3189 unittest(1, "/testcase-data/refcount-node is one");
3192 EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3196 * expect stack trace for subsequent of_node_put():
3197 * __refcount_sub_and_test() calls:
3198 * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3200 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3201 * the first three lines, and the last line.
3203 EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3204 EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3205 EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3206 EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3208 /* refcount is now zero, this should fail */
3209 unittest(1, "/testcase-data/refcount-node is zero");
3212 EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3213 EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3214 EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3215 EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3218 * Q. do we expect to get yet another warning?
3219 * A. no, the WARNING is from WARN_ONCE()
3221 EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3222 EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3223 EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3224 EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3226 unittest(1, "/testcase-data/refcount-node is zero, second time");
3229 EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3230 EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3231 EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3232 EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3235 * refcount of zero will trigger stack traces from any further
3236 * attempt to of_node_get() node "refcount-node". One example of
3237 * this is where of_unittest_check_node_linkage() will recursively
3238 * scan the tree, with 'for_each_child_of_node()' doing an
3239 * of_node_get() of the children of a node.
3241 * Prevent the stack trace by removing node "refcount-node" from
3242 * its parent's child list.
3244 * WARNING: EVIL, EVIL, EVIL:
3246 * Directly manipulate the child list of node /testcase-data to
3247 * remove child refcount-node. This is ignoring all proper methods
3248 * of removing a child and will leak a small amount of memory.
3251 np = of_find_node_by_path(refcount_parent_path);
3252 unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3253 unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3257 prev_sibling = np->child;
3258 next_sibling = prev_sibling->sibling;
3259 if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3260 np->child = next_sibling;
3261 next_sibling = next_sibling->sibling;
3263 while (next_sibling) {
3264 if (!strcmp(next_sibling->full_name, "refcount-node"))
3265 prev_sibling->sibling = next_sibling->sibling;
3266 prev_sibling = next_sibling;
3267 next_sibling = next_sibling->sibling;
3275 unittest(0, "One or more lifecycle tests skipped\n");
3278 #ifdef CONFIG_OF_OVERLAY
3281 * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3282 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
3285 #define OVERLAY_INFO_EXTERN(overlay_name) \
3286 extern uint8_t __dtbo_##overlay_name##_begin[]; \
3287 extern uint8_t __dtbo_##overlay_name##_end[]
3289 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3290 { .dtbo_begin = __dtbo_##overlay_name##_begin, \
3291 .dtbo_end = __dtbo_##overlay_name##_end, \
3292 .expected_result = expected, \
3293 .expected_result_remove = expected_remove, \
3294 .name = #overlay_name, \
3297 struct overlay_info {
3298 uint8_t *dtbo_begin;
3300 int expected_result;
3301 int expected_result_remove; /* if apply failed */
3306 OVERLAY_INFO_EXTERN(overlay_base);
3307 OVERLAY_INFO_EXTERN(overlay);
3308 OVERLAY_INFO_EXTERN(overlay_0);
3309 OVERLAY_INFO_EXTERN(overlay_1);
3310 OVERLAY_INFO_EXTERN(overlay_2);
3311 OVERLAY_INFO_EXTERN(overlay_3);
3312 OVERLAY_INFO_EXTERN(overlay_4);
3313 OVERLAY_INFO_EXTERN(overlay_5);
3314 OVERLAY_INFO_EXTERN(overlay_6);
3315 OVERLAY_INFO_EXTERN(overlay_7);
3316 OVERLAY_INFO_EXTERN(overlay_8);
3317 OVERLAY_INFO_EXTERN(overlay_9);
3318 OVERLAY_INFO_EXTERN(overlay_10);
3319 OVERLAY_INFO_EXTERN(overlay_11);
3320 OVERLAY_INFO_EXTERN(overlay_12);
3321 OVERLAY_INFO_EXTERN(overlay_13);
3322 OVERLAY_INFO_EXTERN(overlay_15);
3323 OVERLAY_INFO_EXTERN(overlay_16);
3324 OVERLAY_INFO_EXTERN(overlay_17);
3325 OVERLAY_INFO_EXTERN(overlay_18);
3326 OVERLAY_INFO_EXTERN(overlay_19);
3327 OVERLAY_INFO_EXTERN(overlay_20);
3328 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3329 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3330 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3331 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3332 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3333 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3334 OVERLAY_INFO_EXTERN(overlay_pci_node);
3335 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3336 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3337 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3338 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3339 OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
3341 /* entries found by name */
3342 static struct overlay_info overlays[] = {
3343 OVERLAY_INFO(overlay_base, -9999, 0),
3344 OVERLAY_INFO(overlay, 0, 0),
3345 OVERLAY_INFO(overlay_0, 0, 0),
3346 OVERLAY_INFO(overlay_1, 0, 0),
3347 OVERLAY_INFO(overlay_2, 0, 0),
3348 OVERLAY_INFO(overlay_3, 0, 0),
3349 OVERLAY_INFO(overlay_4, 0, 0),
3350 OVERLAY_INFO(overlay_5, 0, 0),
3351 OVERLAY_INFO(overlay_6, 0, 0),
3352 OVERLAY_INFO(overlay_7, 0, 0),
3353 OVERLAY_INFO(overlay_8, 0, 0),
3354 OVERLAY_INFO(overlay_9, 0, 0),
3355 OVERLAY_INFO(overlay_10, 0, 0),
3356 OVERLAY_INFO(overlay_11, 0, 0),
3357 OVERLAY_INFO(overlay_12, 0, 0),
3358 OVERLAY_INFO(overlay_13, 0, 0),
3359 OVERLAY_INFO(overlay_15, 0, 0),
3360 OVERLAY_INFO(overlay_16, -EBUSY, 0),
3361 OVERLAY_INFO(overlay_17, -EEXIST, 0),
3362 OVERLAY_INFO(overlay_18, 0, 0),
3363 OVERLAY_INFO(overlay_19, 0, 0),
3364 OVERLAY_INFO(overlay_20, 0, 0),
3365 OVERLAY_INFO(overlay_gpio_01, 0, 0),
3366 OVERLAY_INFO(overlay_gpio_02a, 0, 0),
3367 OVERLAY_INFO(overlay_gpio_02b, 0, 0),
3368 OVERLAY_INFO(overlay_gpio_03, 0, 0),
3369 OVERLAY_INFO(overlay_gpio_04a, 0, 0),
3370 OVERLAY_INFO(overlay_gpio_04b, 0, 0),
3371 OVERLAY_INFO(overlay_pci_node, 0, 0),
3372 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
3373 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
3374 OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
3375 OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
3376 OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
3381 static struct device_node *overlay_base_root;
3383 static void * __init dt_alloc_memory(u64 size, u64 align)
3385 void *ptr = memblock_alloc(size, align);
3388 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
3389 __func__, size, align);
3395 * Create base device tree for the overlay unittest.
3397 * This is called from very early boot code.
3399 * Do as much as possible the same way as done in __unflatten_device_tree
3400 * and other early boot steps for the normal FDT so that the overlay base
3401 * unflattened tree will have the same characteristics as the real tree
3402 * (such as having memory allocated by the early allocator). The goal
3403 * is to test "the real thing" as much as possible, and test "test setup
3404 * code" as little as possible.
3406 * Have to stop before resolving phandles, because that uses kmalloc.
3408 void __init unittest_unflatten_overlay_base(void)
3410 struct overlay_info *info;
3415 const char *overlay_name = "overlay_base";
3417 for (info = overlays; info && info->name; info++) {
3418 if (!strcmp(overlay_name, info->name)) {
3424 pr_err("no overlay data for %s\n", overlay_name);
3428 info = &overlays[0];
3430 if (info->expected_result != -9999) {
3431 pr_err("No dtb 'overlay_base' to attach\n");
3435 data_size = info->dtbo_end - info->dtbo_begin;
3437 pr_err("No dtb 'overlay_base' to attach\n");
3441 size = fdt_totalsize(info->dtbo_begin);
3442 if (size != data_size) {
3443 pr_err("dtb 'overlay_base' header totalsize != actual size");
3447 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3449 pr_err("alloc for dtb 'overlay_base' failed");
3453 memcpy(new_fdt, info->dtbo_begin, size);
3455 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3456 dt_alloc_memory, true);
3460 * The purpose of of_unittest_overlay_data_add is to add an
3461 * overlay in the normal fashion. This is a test of the whole
3462 * picture, instead of testing individual elements.
3464 * A secondary purpose is to be able to verify that the contents of
3465 * /proc/device-tree/ contains the updated structure and values from
3466 * the overlay. That must be verified separately in user space.
3468 * Return 0 on unexpected error.
3470 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3472 struct overlay_info *info;
3478 for (info = overlays; info && info->name; info++) {
3479 if (!strcmp(overlay_name, info->name)) {
3485 pr_err("no overlay data for %s\n", overlay_name);
3489 size = info->dtbo_end - info->dtbo_begin;
3491 pr_err("no overlay data for %s\n", overlay_name);
3493 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
3496 *ovcs_id = info->ovcs_id;
3500 pr_debug("%s applied\n", overlay_name);
3503 if (ret != info->expected_result) {
3504 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3505 info->expected_result, ret, overlay_name);
3510 /* changeset may be partially applied */
3511 ret2 = of_overlay_remove(&info->ovcs_id);
3512 if (ret2 != info->expected_result_remove) {
3513 pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
3514 info->expected_result_remove, ret2,
3524 * The purpose of of_unittest_overlay_high_level is to add an overlay
3525 * in the normal fashion. This is a test of the whole picture,
3526 * instead of individual elements.
3528 * The first part of the function is _not_ normal overlay usage; it is
3529 * finishing splicing the base overlay device tree into the live tree.
3531 static __init void of_unittest_overlay_high_level(void)
3533 struct device_node *last_sibling;
3534 struct device_node *np;
3535 struct device_node *of_symbols;
3536 struct device_node *overlay_base_symbols;
3537 struct device_node **pprev;
3538 struct property *prop;
3541 if (!overlay_base_root) {
3542 unittest(0, "overlay_base_root not initialized\n");
3547 * Could not fixup phandles in unittest_unflatten_overlay_base()
3548 * because kmalloc() was not yet available.
3550 of_overlay_mutex_lock();
3551 of_resolve_phandles(overlay_base_root);
3552 of_overlay_mutex_unlock();
3556 * do not allow overlay_base to duplicate any node already in
3557 * tree, this greatly simplifies the code
3561 * remove overlay_base_root node "__local_fixups", after
3562 * being used by of_resolve_phandles()
3564 pprev = &overlay_base_root->child;
3565 for (np = overlay_base_root->child; np; np = np->sibling) {
3566 if (of_node_name_eq(np, "__local_fixups__")) {
3567 *pprev = np->sibling;
3570 pprev = &np->sibling;
3573 /* remove overlay_base_root node "__symbols__" if in live tree */
3574 of_symbols = of_get_child_by_name(of_root, "__symbols__");
3576 /* will have to graft properties from node into live tree */
3577 pprev = &overlay_base_root->child;
3578 for (np = overlay_base_root->child; np; np = np->sibling) {
3579 if (of_node_name_eq(np, "__symbols__")) {
3580 overlay_base_symbols = np;
3581 *pprev = np->sibling;
3584 pprev = &np->sibling;
3588 for_each_child_of_node(overlay_base_root, np) {
3589 struct device_node *base_child;
3590 for_each_child_of_node(of_root, base_child) {
3591 if (!strcmp(np->full_name, base_child->full_name)) {
3592 unittest(0, "illegal node name in overlay_base %pOFn",
3595 of_node_put(base_child);
3602 * overlay 'overlay_base' is not allowed to have root
3603 * properties, so only need to splice nodes into main device tree.
3605 * root node of *overlay_base_root will not be freed, it is lost
3609 for (np = overlay_base_root->child; np; np = np->sibling)
3610 np->parent = of_root;
3612 mutex_lock(&of_mutex);
3614 for (last_sibling = np = of_root->child; np; np = np->sibling)
3618 last_sibling->sibling = overlay_base_root->child;
3620 of_root->child = overlay_base_root->child;
3622 for_each_of_allnodes_from(overlay_base_root, np)
3623 __of_attach_node_sysfs(np);
3626 struct property *new_prop;
3627 for_each_property_of_node(overlay_base_symbols, prop) {
3629 new_prop = __of_prop_dup(prop, GFP_KERNEL);
3631 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3635 if (__of_add_property(of_symbols, new_prop)) {
3636 kfree(new_prop->name);
3637 kfree(new_prop->value);
3639 /* "name" auto-generated by unflatten */
3640 if (!strcmp(prop->name, "name"))
3642 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3646 if (__of_add_property_sysfs(of_symbols, new_prop)) {
3647 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3654 mutex_unlock(&of_mutex);
3657 /* now do the normal overlay usage test */
3659 /* --- overlay --- */
3661 EXPECT_BEGIN(KERN_ERR,
3662 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3663 EXPECT_BEGIN(KERN_ERR,
3664 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3665 EXPECT_BEGIN(KERN_ERR,
3666 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3667 EXPECT_BEGIN(KERN_ERR,
3668 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3669 EXPECT_BEGIN(KERN_ERR,
3670 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3671 EXPECT_BEGIN(KERN_ERR,
3672 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3673 EXPECT_BEGIN(KERN_ERR,
3674 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3675 EXPECT_BEGIN(KERN_ERR,
3676 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3677 EXPECT_BEGIN(KERN_ERR,
3678 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3679 EXPECT_BEGIN(KERN_ERR,
3680 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3681 EXPECT_BEGIN(KERN_ERR,
3682 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3684 ret = overlay_data_apply("overlay", NULL);
3686 EXPECT_END(KERN_ERR,
3687 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3688 EXPECT_END(KERN_ERR,
3689 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3690 EXPECT_END(KERN_ERR,
3691 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3692 EXPECT_END(KERN_ERR,
3693 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3694 EXPECT_END(KERN_ERR,
3695 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3696 EXPECT_END(KERN_ERR,
3697 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3698 EXPECT_END(KERN_ERR,
3699 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3700 EXPECT_END(KERN_ERR,
3701 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3702 EXPECT_END(KERN_ERR,
3703 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3704 EXPECT_END(KERN_ERR,
3705 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3706 EXPECT_END(KERN_ERR,
3707 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3709 unittest(ret, "Adding overlay 'overlay' failed\n");
3711 /* --- overlay_bad_add_dup_node --- */
3713 EXPECT_BEGIN(KERN_ERR,
3714 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3715 EXPECT_BEGIN(KERN_ERR,
3716 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3717 EXPECT_BEGIN(KERN_ERR,
3718 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3719 EXPECT_BEGIN(KERN_ERR,
3720 "OF: Error reverting changeset (-19)");
3722 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3723 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3725 EXPECT_END(KERN_ERR,
3726 "OF: Error reverting changeset (-19)");
3727 EXPECT_END(KERN_ERR,
3728 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3729 EXPECT_END(KERN_ERR,
3730 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3731 EXPECT_END(KERN_ERR,
3732 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3734 /* --- overlay_bad_add_dup_prop --- */
3736 EXPECT_BEGIN(KERN_ERR,
3737 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3738 EXPECT_BEGIN(KERN_ERR,
3739 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3740 EXPECT_BEGIN(KERN_ERR,
3741 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3742 EXPECT_BEGIN(KERN_ERR,
3743 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3744 EXPECT_BEGIN(KERN_ERR,
3745 "OF: Error reverting changeset (-19)");
3747 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3748 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3750 EXPECT_END(KERN_ERR,
3751 "OF: Error reverting changeset (-19)");
3752 EXPECT_END(KERN_ERR,
3753 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3754 EXPECT_END(KERN_ERR,
3755 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3756 EXPECT_END(KERN_ERR,
3757 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3758 EXPECT_END(KERN_ERR,
3759 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3761 /* --- overlay_bad_phandle --- */
3763 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
3764 "Adding overlay 'overlay_bad_phandle' failed\n");
3766 /* --- overlay_bad_symbol --- */
3768 EXPECT_BEGIN(KERN_ERR,
3769 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3770 EXPECT_BEGIN(KERN_ERR,
3771 "OF: Error reverting changeset (-19)");
3773 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
3774 "Adding overlay 'overlay_bad_symbol' failed\n");
3776 EXPECT_END(KERN_ERR,
3777 "OF: Error reverting changeset (-19)");
3778 EXPECT_END(KERN_ERR,
3779 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3781 /* --- overlay_bad_unresolved --- */
3783 EXPECT_BEGIN(KERN_ERR,
3784 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3785 EXPECT_BEGIN(KERN_ERR,
3786 "OF: resolver: overlay phandle fixup failed: -22");
3788 unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
3789 "Adding overlay 'overlay_bad_unresolved' failed\n");
3791 EXPECT_END(KERN_ERR,
3792 "OF: resolver: overlay phandle fixup failed: -22");
3793 EXPECT_END(KERN_ERR,
3794 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3799 mutex_unlock(&of_mutex);
3802 static int of_unittest_pci_dev_num;
3803 static int of_unittest_pci_child_num;
3806 * PCI device tree node test driver
3808 static const struct pci_device_id testdrv_pci_ids[] = {
3809 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
3813 static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3815 struct overlay_info *info;
3816 struct device_node *dn;
3820 dn = pdev->dev.of_node;
3822 dev_err(&pdev->dev, "does not find bus endpoint");
3826 for (info = overlays; info && info->name; info++) {
3827 if (!strcmp(info->name, "overlay_pci_node"))
3830 if (!info || !info->name) {
3831 dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
3835 size = info->dtbo_end - info->dtbo_begin;
3836 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
3841 of_platform_default_populate(dn, NULL, &pdev->dev);
3842 pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
3847 static void testdrv_remove(struct pci_dev *pdev)
3849 int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
3851 of_platform_depopulate(&pdev->dev);
3852 of_overlay_remove(&ovcs_id);
3855 static struct pci_driver testdrv_driver = {
3856 .name = "pci_dt_testdrv",
3857 .id_table = testdrv_pci_ids,
3858 .probe = testdrv_probe,
3859 .remove = testdrv_remove,
3862 static int unittest_pci_probe(struct platform_device *pdev)
3864 struct resource *res;
3868 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3873 while (dev && !dev_is_pci(dev))
3876 pr_err("unable to find parent device\n");
3880 exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
3881 unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
3882 (u64)res->start, exp_addr);
3884 of_unittest_pci_child_num++;
3889 static const struct of_device_id unittest_pci_of_match[] = {
3890 { .compatible = "unittest-pci" },
3894 static struct platform_driver unittest_pci_driver = {
3895 .probe = unittest_pci_probe,
3897 .name = "unittest-pci",
3898 .of_match_table = unittest_pci_of_match,
3902 static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
3904 struct device_node *pnp, *np = NULL;
3905 struct device *child_dev;
3910 pnp = pdev->dev.of_node;
3911 unittest(pnp, "Failed creating PCI dt node\n");
3916 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
3917 np = of_find_node_by_path(path);
3918 unittest(np, "Failed to get unittest-pci node under PCI node\n");
3924 reg = of_get_property(np, "reg", NULL);
3925 unittest(reg, "Failed to get reg property\n");
3929 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
3930 np = of_find_node_by_path(path);
3931 unittest(!np, "Child device tree node is not removed\n");
3932 child_dev = device_find_any_child(&pdev->dev);
3933 unittest(!child_dev, "Child device is not removed\n");
3944 static void __init of_unittest_pci_node(void)
3946 struct pci_dev *pdev = NULL;
3949 if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
3952 rc = pci_register_driver(&testdrv_driver);
3953 unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
3957 rc = platform_driver_register(&unittest_pci_driver);
3958 if (unittest(!rc, "Failed to register unittest pci driver\n")) {
3959 pci_unregister_driver(&testdrv_driver);
3963 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
3964 of_unittest_pci_node_verify(pdev, true);
3965 of_unittest_pci_dev_num++;
3970 unittest(of_unittest_pci_dev_num,
3971 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
3972 unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
3973 "Child device number %d is not expected %d", of_unittest_pci_child_num,
3974 of_unittest_pci_dev_num);
3976 platform_driver_unregister(&unittest_pci_driver);
3977 pci_unregister_driver(&testdrv_driver);
3979 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
3980 of_unittest_pci_node_verify(pdev, false);
3986 static inline __init void of_unittest_overlay_high_level(void) {}
3987 static inline __init void of_unittest_pci_node(void) { }
3991 static int __init of_unittest(void)
3993 struct device_node *np;
3996 pr_info("start of unittest - you will see error messages\n");
3998 /* Taint the kernel so we know we've run tests. */
3999 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
4001 /* adding data for unittest */
4003 if (IS_ENABLED(CONFIG_UML))
4004 unittest_unflatten_overlay_base();
4006 res = unittest_data_add();
4010 of_aliases = of_find_node_by_path("/aliases");
4012 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4014 pr_info("No testcase data in device tree; not running tests\n");
4019 of_unittest_check_tree_linkage();
4020 of_unittest_check_phandles();
4021 of_unittest_find_node_by_name();
4022 of_unittest_dynamic();
4023 of_unittest_parse_phandle_with_args();
4024 of_unittest_parse_phandle_with_args_map();
4025 of_unittest_printf();
4026 of_unittest_property_string();
4027 of_unittest_property_copy();
4028 of_unittest_changeset();
4029 of_unittest_parse_interrupts();
4030 of_unittest_parse_interrupts_extended();
4031 of_unittest_dma_get_max_cpu_address();
4032 of_unittest_parse_dma_ranges();
4033 of_unittest_pci_dma_ranges();
4034 of_unittest_bus_ranges();
4035 of_unittest_bus_3cell_ranges();
4037 of_unittest_match_node();
4038 of_unittest_platform_populate();
4039 of_unittest_overlay();
4040 of_unittest_lifecycle();
4041 of_unittest_pci_node();
4043 /* Double check linkage after removing testcase data */
4044 of_unittest_check_tree_linkage();
4046 of_unittest_overlay_high_level();
4048 pr_info("end of unittest - %i passed, %i failed\n",
4049 unittest_results.passed, unittest_results.failed);
4053 late_initcall(of_unittest);