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 struct of_unittest_expected_res {
1191 struct resource res;
1194 static void __init of_unittest_check_addr(const char *node_path,
1195 const struct of_unittest_expected_res *tab_exp,
1196 unsigned int tab_exp_count)
1198 const struct of_unittest_expected_res *expected;
1199 struct device_node *np;
1200 struct resource res;
1204 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
1207 np = of_find_node_by_path(node_path);
1209 pr_err("missing testcase data (%s)\n", node_path);
1214 count = tab_exp_count;
1216 ret = of_address_to_resource(np, expected->index, &res);
1217 unittest(!ret, "of_address_to_resource(%pOF, %d) returned error %d\n",
1218 np, expected->index, ret);
1219 unittest(resource_type(&res) == resource_type(&expected->res) &&
1220 res.start == expected->res.start &&
1221 resource_size(&res) == resource_size(&expected->res),
1222 "of_address_to_resource(%pOF, %d) wrong resource %pR, expected %pR\n",
1223 np, expected->index, &res, &expected->res);
1230 static const struct of_unittest_expected_res of_unittest_reg_2cell_expected_res[] = {
1231 {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1232 {.index = 1, .res = DEFINE_RES_MEM(0xa0a02000, 0x100) },
1233 {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1234 {.index = 3, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1237 static const struct of_unittest_expected_res of_unittest_reg_3cell_expected_res[] = {
1238 {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1239 {.index = 1, .res = DEFINE_RES_MEM(0xa0b02000, 0x100) },
1240 {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1241 {.index = 3, .res = DEFINE_RES_MEM(0xc0c09000, 0x100) },
1242 {.index = 4, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1245 static const struct of_unittest_expected_res of_unittest_reg_pci_expected_res[] = {
1246 {.index = 0, .res = DEFINE_RES_MEM(0xe8001000, 0x1000) },
1247 {.index = 1, .res = DEFINE_RES_MEM(0xea002000, 0x2000) },
1250 static void __init of_unittest_translate_addr(void)
1252 of_unittest_check_addr("/testcase-data/address-tests2/bus-2cell@10000000/device@100000",
1253 of_unittest_reg_2cell_expected_res,
1254 ARRAY_SIZE(of_unittest_reg_2cell_expected_res));
1256 of_unittest_check_addr("/testcase-data/address-tests2/bus-3cell@20000000/local-bus@100000/device@f1001000",
1257 of_unittest_reg_3cell_expected_res,
1258 ARRAY_SIZE(of_unittest_reg_3cell_expected_res));
1260 of_unittest_check_addr("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0/dev@e0000000",
1261 of_unittest_reg_pci_expected_res,
1262 ARRAY_SIZE(of_unittest_reg_pci_expected_res));
1265 static void __init of_unittest_parse_interrupts(void)
1267 struct device_node *np;
1268 struct of_phandle_args args;
1271 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1274 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1276 pr_err("missing testcase data\n");
1280 for (i = 0; i < 4; i++) {
1283 memset(&args, 0, sizeof(args));
1284 rc = of_irq_parse_one(np, i, &args);
1287 passed &= (args.args_count == 1);
1288 passed &= (args.args[0] == (i + 1));
1290 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1295 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1297 pr_err("missing testcase data\n");
1301 for (i = 0; i < 4; i++) {
1304 memset(&args, 0, sizeof(args));
1305 rc = of_irq_parse_one(np, i, &args);
1307 /* Test the values from tests-phandle.dtsi */
1311 passed &= (args.args_count == 1);
1312 passed &= (args.args[0] == 9);
1316 passed &= (args.args_count == 3);
1317 passed &= (args.args[0] == 10);
1318 passed &= (args.args[1] == 11);
1319 passed &= (args.args[2] == 12);
1323 passed &= (args.args_count == 2);
1324 passed &= (args.args[0] == 13);
1325 passed &= (args.args[1] == 14);
1329 passed &= (args.args_count == 2);
1330 passed &= (args.args[0] == 15);
1331 passed &= (args.args[1] == 16);
1336 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1342 static void __init of_unittest_parse_interrupts_extended(void)
1344 struct device_node *np;
1345 struct of_phandle_args args;
1348 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1351 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1353 pr_err("missing testcase data\n");
1357 for (i = 0; i < 7; i++) {
1360 memset(&args, 0, sizeof(args));
1361 rc = of_irq_parse_one(np, i, &args);
1363 /* Test the values from tests-phandle.dtsi */
1367 passed &= (args.args_count == 1);
1368 passed &= (args.args[0] == 1);
1372 passed &= (args.args_count == 3);
1373 passed &= (args.args[0] == 2);
1374 passed &= (args.args[1] == 3);
1375 passed &= (args.args[2] == 4);
1379 passed &= (args.args_count == 2);
1380 passed &= (args.args[0] == 5);
1381 passed &= (args.args[1] == 6);
1385 passed &= (args.args_count == 1);
1386 passed &= (args.args[0] == 9);
1390 passed &= (args.args_count == 3);
1391 passed &= (args.args[0] == 10);
1392 passed &= (args.args[1] == 11);
1393 passed &= (args.args[2] == 12);
1397 passed &= (args.args_count == 2);
1398 passed &= (args.args[0] == 13);
1399 passed &= (args.args[1] == 14);
1403 * Tests child node that is missing property
1404 * #address-cells. See the comments in
1405 * drivers/of/unittest-data/tests-interrupts.dtsi
1406 * nodes intmap1 and interrupts-extended0
1409 passed &= (args.args_count == 1);
1410 passed &= (args.args[0] == 15);
1416 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1422 static const struct of_device_id match_node_table[] = {
1423 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1424 { .data = "B", .type = "type1", }, /* followed by type alone */
1426 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1427 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1428 { .data = "Cc", .name = "name2", .type = "type2", },
1430 { .data = "E", .compatible = "compat3" },
1431 { .data = "G", .compatible = "compat2", },
1432 { .data = "H", .compatible = "compat2", .name = "name5", },
1433 { .data = "I", .compatible = "compat2", .type = "type1", },
1434 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1435 { .data = "K", .compatible = "compat2", .name = "name9", },
1442 } match_node_tests[] = {
1443 { .path = "/testcase-data/match-node/name0", .data = "A", },
1444 { .path = "/testcase-data/match-node/name1", .data = "B", },
1445 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1446 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1447 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1448 { .path = "/testcase-data/match-node/name3", .data = "E", },
1449 { .path = "/testcase-data/match-node/name4", .data = "G", },
1450 { .path = "/testcase-data/match-node/name5", .data = "H", },
1451 { .path = "/testcase-data/match-node/name6", .data = "G", },
1452 { .path = "/testcase-data/match-node/name7", .data = "I", },
1453 { .path = "/testcase-data/match-node/name8", .data = "J", },
1454 { .path = "/testcase-data/match-node/name9", .data = "K", },
1457 static void __init of_unittest_match_node(void)
1459 struct device_node *np;
1460 const struct of_device_id *match;
1463 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1464 np = of_find_node_by_path(match_node_tests[i].path);
1466 unittest(0, "missing testcase node %s\n",
1467 match_node_tests[i].path);
1471 match = of_match_node(match_node_table, np);
1473 unittest(0, "%s didn't match anything\n",
1474 match_node_tests[i].path);
1478 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1479 unittest(0, "%s got wrong match. expected %s, got %s\n",
1480 match_node_tests[i].path, match_node_tests[i].data,
1481 (const char *)match->data);
1484 unittest(1, "passed");
1488 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1489 static const struct platform_device_info test_bus_info = {
1490 .name = "unittest-bus",
1492 static void __init of_unittest_platform_populate(void)
1495 struct device_node *np, *child, *grandchild;
1496 struct platform_device *pdev, *test_bus;
1497 const struct of_device_id match[] = {
1498 { .compatible = "test-device", },
1502 np = of_find_node_by_path("/testcase-data");
1503 of_platform_default_populate(np, NULL, NULL);
1505 /* Test that a missing irq domain returns -EPROBE_DEFER */
1506 np = of_find_node_by_path("/testcase-data/testcase-device1");
1507 pdev = of_find_device_by_node(np);
1508 unittest(pdev, "device 1 creation failed\n");
1510 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1511 irq = platform_get_irq(pdev, 0);
1512 unittest(irq == -EPROBE_DEFER,
1513 "device deferred probe failed - %d\n", irq);
1515 /* Test that a parsing failure does not return -EPROBE_DEFER */
1516 np = of_find_node_by_path("/testcase-data/testcase-device2");
1517 pdev = of_find_device_by_node(np);
1518 unittest(pdev, "device 2 creation failed\n");
1520 EXPECT_BEGIN(KERN_INFO,
1521 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1523 irq = platform_get_irq(pdev, 0);
1525 EXPECT_END(KERN_INFO,
1526 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1528 unittest(irq < 0 && irq != -EPROBE_DEFER,
1529 "device parsing error failed - %d\n", irq);
1532 np = of_find_node_by_path("/testcase-data/platform-tests");
1533 unittest(np, "No testcase data in device tree\n");
1537 test_bus = platform_device_register_full(&test_bus_info);
1538 rc = PTR_ERR_OR_ZERO(test_bus);
1539 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1544 test_bus->dev.of_node = np;
1547 * Add a dummy resource to the test bus node after it is
1548 * registered to catch problems with un-inserted resources. The
1549 * DT code doesn't insert the resources, and it has caused the
1550 * kernel to oops in the past. This makes sure the same bug
1551 * doesn't crop up again.
1553 platform_device_add_resources(test_bus, &test_bus_res, 1);
1555 of_platform_populate(np, match, NULL, &test_bus->dev);
1556 for_each_child_of_node(np, child) {
1557 for_each_child_of_node(child, grandchild) {
1558 pdev = of_find_device_by_node(grandchild);
1560 "Could not create device for node '%pOFn'\n",
1562 platform_device_put(pdev);
1566 of_platform_depopulate(&test_bus->dev);
1567 for_each_child_of_node(np, child) {
1568 for_each_child_of_node(child, grandchild)
1569 unittest(!of_find_device_by_node(grandchild),
1570 "device didn't get destroyed '%pOFn'\n",
1574 platform_device_unregister(test_bus);
1579 * update_node_properties - adds the properties
1580 * of np into dup node (present in live tree) and
1581 * updates parent of children of np to dup.
1583 * @np: node whose properties are being added to the live tree
1584 * @dup: node present in live tree to be updated
1586 static void update_node_properties(struct device_node *np,
1587 struct device_node *dup)
1589 struct property *prop;
1590 struct property *save_next;
1591 struct device_node *child;
1594 for_each_child_of_node(np, child)
1595 child->parent = dup;
1598 * "unittest internal error: unable to add testdata property"
1600 * If this message reports a property in node '/__symbols__' then
1601 * the respective unittest overlay contains a label that has the
1602 * same name as a label in the live devicetree. The label will
1603 * be in the live devicetree only if the devicetree source was
1604 * compiled with the '-@' option. If you encounter this error,
1605 * please consider renaming __all__ of the labels in the unittest
1606 * overlay dts files with an odd prefix that is unlikely to be
1607 * used in a real devicetree.
1611 * open code for_each_property_of_node() because of_add_property()
1612 * sets prop->next to NULL
1614 for (prop = np->properties; prop != NULL; prop = save_next) {
1615 save_next = prop->next;
1616 ret = of_add_property(dup, prop);
1618 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1620 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1627 * attach_node_and_children - attaches nodes
1628 * and its children to live tree.
1629 * CAUTION: misleading function name - if node @np already exists in
1630 * the live tree then children of @np are *not* attached to the live
1631 * tree. This works for the current test devicetree nodes because such
1632 * nodes do not have child nodes.
1634 * @np: Node to attach to live tree
1636 static void attach_node_and_children(struct device_node *np)
1638 struct device_node *next, *dup, *child;
1639 unsigned long flags;
1640 const char *full_name;
1642 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1646 if (!strcmp(full_name, "/__local_fixups__") ||
1647 !strcmp(full_name, "/__fixups__")) {
1652 dup = of_find_node_by_path(full_name);
1655 update_node_properties(np, dup);
1662 mutex_lock(&of_mutex);
1663 raw_spin_lock_irqsave(&devtree_lock, flags);
1664 np->sibling = np->parent->child;
1665 np->parent->child = np;
1666 of_node_clear_flag(np, OF_DETACHED);
1667 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1669 __of_attach_node_sysfs(np);
1670 mutex_unlock(&of_mutex);
1673 next = child->sibling;
1674 attach_node_and_children(child);
1680 * unittest_data_add - Reads, copies data from
1681 * linked tree and attaches it to the live tree
1683 static int __init unittest_data_add(void)
1685 void *unittest_data;
1686 void *unittest_data_align;
1687 struct device_node *unittest_data_node = NULL, *np;
1689 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1690 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
1692 extern uint8_t __dtbo_testcases_begin[];
1693 extern uint8_t __dtbo_testcases_end[];
1694 const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
1699 pr_warn("%s: testcases is empty\n", __func__);
1704 unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
1708 unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1709 memcpy(unittest_data_align, __dtbo_testcases_begin, size);
1711 ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
1713 pr_warn("%s: unflatten testcases tree failed\n", __func__);
1714 kfree(unittest_data);
1717 if (!unittest_data_node) {
1718 pr_warn("%s: testcases tree is empty\n", __func__);
1719 kfree(unittest_data);
1724 * This lock normally encloses of_resolve_phandles()
1726 of_overlay_mutex_lock();
1728 rc = of_resolve_phandles(unittest_data_node);
1730 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1731 of_overlay_mutex_unlock();
1736 of_root = unittest_data_node;
1737 for_each_of_allnodes(np)
1738 __of_attach_node_sysfs(np);
1739 of_aliases = of_find_node_by_path("/aliases");
1740 of_chosen = of_find_node_by_path("/chosen");
1741 of_overlay_mutex_unlock();
1745 EXPECT_BEGIN(KERN_INFO,
1746 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1748 /* attach the sub-tree to live tree */
1749 np = unittest_data_node->child;
1751 struct device_node *next = np->sibling;
1753 np->parent = of_root;
1754 /* this will clear OF_DETACHED in np and children */
1755 attach_node_and_children(np);
1759 EXPECT_END(KERN_INFO,
1760 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1762 of_overlay_mutex_unlock();
1767 #ifdef CONFIG_OF_OVERLAY
1768 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
1770 static int unittest_probe(struct platform_device *pdev)
1772 struct device *dev = &pdev->dev;
1773 struct device_node *np = dev->of_node;
1776 dev_err(dev, "No OF data for device\n");
1781 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1783 of_platform_populate(np, NULL, NULL, &pdev->dev);
1788 static void unittest_remove(struct platform_device *pdev)
1790 struct device *dev = &pdev->dev;
1791 struct device_node *np = dev->of_node;
1793 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1796 static const struct of_device_id unittest_match[] = {
1797 { .compatible = "unittest", },
1801 static struct platform_driver unittest_driver = {
1802 .probe = unittest_probe,
1803 .remove_new = unittest_remove,
1806 .of_match_table = unittest_match,
1810 /* get the platform device instantiated at the path */
1811 static struct platform_device *of_path_to_platform_device(const char *path)
1813 struct device_node *np;
1814 struct platform_device *pdev;
1816 np = of_find_node_by_path(path);
1820 pdev = of_find_device_by_node(np);
1826 /* find out if a platform device exists at that path */
1827 static int of_path_platform_device_exists(const char *path)
1829 struct platform_device *pdev;
1831 pdev = of_path_to_platform_device(path);
1832 platform_device_put(pdev);
1833 return pdev != NULL;
1836 #ifdef CONFIG_OF_GPIO
1838 struct unittest_gpio_dev {
1839 struct gpio_chip chip;
1842 static int unittest_gpio_chip_request_count;
1843 static int unittest_gpio_probe_count;
1844 static int unittest_gpio_probe_pass_count;
1846 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1848 unittest_gpio_chip_request_count++;
1850 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1851 unittest_gpio_chip_request_count);
1855 static int unittest_gpio_probe(struct platform_device *pdev)
1857 struct unittest_gpio_dev *devptr;
1860 unittest_gpio_probe_count++;
1862 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1866 platform_set_drvdata(pdev, devptr);
1868 devptr->chip.fwnode = dev_fwnode(&pdev->dev);
1869 devptr->chip.label = "of-unittest-gpio";
1870 devptr->chip.base = -1; /* dynamic allocation */
1871 devptr->chip.ngpio = 5;
1872 devptr->chip.request = unittest_gpio_chip_request;
1874 ret = gpiochip_add_data(&devptr->chip, NULL);
1877 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
1880 unittest_gpio_probe_pass_count++;
1884 static void unittest_gpio_remove(struct platform_device *pdev)
1886 struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
1887 struct device *dev = &pdev->dev;
1889 dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
1891 if (devptr->chip.base != -1)
1892 gpiochip_remove(&devptr->chip);
1897 static const struct of_device_id unittest_gpio_id[] = {
1898 { .compatible = "unittest-gpio", },
1902 static struct platform_driver unittest_gpio_driver = {
1903 .probe = unittest_gpio_probe,
1904 .remove_new = unittest_gpio_remove,
1906 .name = "unittest-gpio",
1907 .of_match_table = unittest_gpio_id,
1911 static void __init of_unittest_overlay_gpio(void)
1913 int chip_request_count;
1914 int probe_pass_count;
1918 * tests: apply overlays before registering driver
1919 * Similar to installing a driver as a module, the
1920 * driver is registered after applying the overlays.
1922 * The overlays are applied by overlay_data_apply()
1923 * instead of of_unittest_apply_overlay() so that they
1924 * will not be tracked. Thus they will not be removed
1925 * by of_unittest_remove_tracked_overlays().
1927 * - apply overlay_gpio_01
1928 * - apply overlay_gpio_02a
1929 * - apply overlay_gpio_02b
1932 * register driver will result in
1933 * - probe and processing gpio hog for overlay_gpio_01
1934 * - probe for overlay_gpio_02a
1935 * - processing gpio for overlay_gpio_02b
1938 probe_pass_count = unittest_gpio_probe_pass_count;
1939 chip_request_count = unittest_gpio_chip_request_count;
1942 * overlay_gpio_01 contains gpio node and child gpio hog node
1943 * overlay_gpio_02a contains gpio node
1944 * overlay_gpio_02b contains child gpio hog node
1947 unittest(overlay_data_apply("overlay_gpio_01", NULL),
1948 "Adding overlay 'overlay_gpio_01' failed\n");
1950 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1951 "Adding overlay 'overlay_gpio_02a' failed\n");
1953 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1954 "Adding overlay 'overlay_gpio_02b' failed\n");
1956 ret = platform_driver_register(&unittest_gpio_driver);
1957 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1960 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1961 "unittest_gpio_probe() failed or not called\n");
1963 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1964 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1965 unittest_gpio_chip_request_count - chip_request_count);
1968 * tests: apply overlays after registering driver
1970 * Similar to a driver built-in to the kernel, the
1971 * driver is registered before applying the overlays.
1973 * overlay_gpio_03 contains gpio node and child gpio hog node
1975 * - apply overlay_gpio_03
1977 * apply overlay will result in
1978 * - probe and processing gpio hog.
1981 probe_pass_count = unittest_gpio_probe_pass_count;
1982 chip_request_count = unittest_gpio_chip_request_count;
1984 /* overlay_gpio_03 contains gpio node and child gpio hog node */
1986 unittest(overlay_data_apply("overlay_gpio_03", NULL),
1987 "Adding overlay 'overlay_gpio_03' failed\n");
1989 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1990 "unittest_gpio_probe() failed or not called\n");
1992 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1993 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1994 unittest_gpio_chip_request_count - chip_request_count);
1997 * overlay_gpio_04a contains gpio node
1999 * - apply overlay_gpio_04a
2001 * apply the overlay will result in
2002 * - probe for overlay_gpio_04a
2005 probe_pass_count = unittest_gpio_probe_pass_count;
2006 chip_request_count = unittest_gpio_chip_request_count;
2008 /* overlay_gpio_04a contains gpio node */
2010 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
2011 "Adding overlay 'overlay_gpio_04a' failed\n");
2013 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2014 "unittest_gpio_probe() failed or not called\n");
2017 * overlay_gpio_04b contains child gpio hog node
2019 * - apply overlay_gpio_04b
2021 * apply the overlay will result in
2022 * - processing gpio for overlay_gpio_04b
2025 /* overlay_gpio_04b contains child gpio hog node */
2027 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
2028 "Adding overlay 'overlay_gpio_04b' failed\n");
2030 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2031 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2032 unittest_gpio_chip_request_count - chip_request_count);
2037 static void __init of_unittest_overlay_gpio(void)
2044 #if IS_BUILTIN(CONFIG_I2C)
2046 /* get the i2c client device instantiated at the path */
2047 static struct i2c_client *of_path_to_i2c_client(const char *path)
2049 struct device_node *np;
2050 struct i2c_client *client;
2052 np = of_find_node_by_path(path);
2056 client = of_find_i2c_device_by_node(np);
2062 /* find out if a i2c client device exists at that path */
2063 static int of_path_i2c_client_exists(const char *path)
2065 struct i2c_client *client;
2067 client = of_path_to_i2c_client(path);
2069 put_device(&client->dev);
2070 return client != NULL;
2073 static int of_path_i2c_client_exists(const char *path)
2084 static int of_path_device_type_exists(const char *path,
2085 enum overlay_type ovtype)
2089 return of_path_platform_device_exists(path);
2091 return of_path_i2c_client_exists(path);
2096 static const char *unittest_path(int nr, enum overlay_type ovtype)
2099 static char buf[256];
2103 base = "/testcase-data/overlay-node/test-bus";
2106 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2112 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2113 buf[sizeof(buf) - 1] = '\0';
2117 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2121 path = unittest_path(unittest_nr, ovtype);
2125 return of_path_platform_device_exists(path);
2127 return of_path_i2c_client_exists(path);
2132 static const char *overlay_name_from_nr(int nr)
2134 static char buf[256];
2136 snprintf(buf, sizeof(buf) - 1,
2138 buf[sizeof(buf) - 1] = '\0';
2143 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2145 #define MAX_TRACK_OVCS_IDS 256
2147 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2148 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2149 static int track_ovcs_id_cnt;
2151 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2153 if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2156 track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2157 track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2158 track_ovcs_id_cnt++;
2161 static void of_unittest_untrack_overlay(int ovcs_id)
2163 if (WARN_ON(track_ovcs_id_cnt < 1))
2166 track_ovcs_id_cnt--;
2168 /* If out of synch then test is broken. Do not try to recover. */
2169 WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2172 static void of_unittest_remove_tracked_overlays(void)
2174 int ret, ovcs_id, overlay_nr, save_ovcs_id;
2175 const char *overlay_name;
2177 while (track_ovcs_id_cnt > 0) {
2179 ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2180 overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2181 save_ovcs_id = ovcs_id;
2182 ret = of_overlay_remove(&ovcs_id);
2183 if (ret == -ENODEV) {
2184 overlay_name = overlay_name_from_nr(overlay_nr);
2185 pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2186 __func__, overlay_name, ret);
2188 of_unittest_untrack_overlay(save_ovcs_id);
2193 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2196 * The overlay will be tracked, thus it will be removed
2197 * by of_unittest_remove_tracked_overlays().
2200 const char *overlay_name;
2202 overlay_name = overlay_name_from_nr(overlay_nr);
2204 if (!overlay_data_apply(overlay_name, ovcs_id)) {
2205 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2208 of_unittest_track_overlay(*ovcs_id, overlay_nr);
2213 static int __init __of_unittest_apply_overlay_check(int overlay_nr,
2214 int unittest_nr, int before, int after,
2215 enum overlay_type ovtype)
2219 /* unittest device must be in before state */
2220 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2221 unittest(0, "%s with device @\"%s\" %s\n",
2222 overlay_name_from_nr(overlay_nr),
2223 unittest_path(unittest_nr, ovtype),
2224 !before ? "enabled" : "disabled");
2228 /* apply the overlay */
2230 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2232 /* of_unittest_apply_overlay already called unittest() */
2236 /* unittest device must be in after state */
2237 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2238 unittest(0, "%s with device @\"%s\" %s\n",
2239 overlay_name_from_nr(overlay_nr),
2240 unittest_path(unittest_nr, ovtype),
2241 !after ? "enabled" : "disabled");
2248 /* apply an overlay while checking before and after states */
2249 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2250 int unittest_nr, int before, int after,
2251 enum overlay_type ovtype)
2253 int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
2254 unittest_nr, before, after, ovtype);
2261 /* apply an overlay and then revert it while checking before, after states */
2262 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2263 int unittest_nr, int before, int after,
2264 enum overlay_type ovtype)
2266 int ret, ovcs_id, save_ovcs_id;
2268 ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
2269 before, after, ovtype);
2273 /* remove the overlay */
2274 save_ovcs_id = ovcs_id;
2275 ret = of_overlay_remove(&ovcs_id);
2277 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2278 overlay_name_from_nr(overlay_nr),
2279 unittest_path(unittest_nr, ovtype));
2282 of_unittest_untrack_overlay(save_ovcs_id);
2284 /* unittest device must be again in before state */
2285 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2286 unittest(0, "%s with device @\"%s\" %s\n",
2287 overlay_name_from_nr(overlay_nr),
2288 unittest_path(unittest_nr, ovtype),
2289 !before ? "enabled" : "disabled");
2296 /* test activation of device */
2297 static void __init of_unittest_overlay_0(void)
2301 EXPECT_BEGIN(KERN_INFO,
2302 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2304 /* device should enable */
2305 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2307 EXPECT_END(KERN_INFO,
2308 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2313 unittest(1, "overlay test %d passed\n", 0);
2316 /* test deactivation of device */
2317 static void __init of_unittest_overlay_1(void)
2321 EXPECT_BEGIN(KERN_INFO,
2322 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2324 /* device should disable */
2325 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2327 EXPECT_END(KERN_INFO,
2328 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2333 unittest(1, "overlay test %d passed\n", 1);
2337 /* test activation of device */
2338 static void __init of_unittest_overlay_2(void)
2342 EXPECT_BEGIN(KERN_INFO,
2343 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2345 /* device should enable */
2346 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2348 EXPECT_END(KERN_INFO,
2349 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2353 unittest(1, "overlay test %d passed\n", 2);
2356 /* test deactivation of device */
2357 static void __init of_unittest_overlay_3(void)
2361 EXPECT_BEGIN(KERN_INFO,
2362 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2364 /* device should disable */
2365 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2367 EXPECT_END(KERN_INFO,
2368 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2373 unittest(1, "overlay test %d passed\n", 3);
2376 /* test activation of a full device node */
2377 static void __init of_unittest_overlay_4(void)
2379 /* device should disable */
2380 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2383 unittest(1, "overlay test %d passed\n", 4);
2386 /* test overlay apply/revert sequence */
2387 static void __init of_unittest_overlay_5(void)
2391 EXPECT_BEGIN(KERN_INFO,
2392 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2394 /* device should disable */
2395 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2397 EXPECT_END(KERN_INFO,
2398 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2403 unittest(1, "overlay test %d passed\n", 5);
2406 /* test overlay application in sequence */
2407 static void __init of_unittest_overlay_6(void)
2409 int i, save_ovcs_id[2], ovcs_id;
2410 int overlay_nr = 6, unittest_nr = 6;
2411 int before = 0, after = 1;
2412 const char *overlay_name;
2416 /* unittest device must be in before state */
2417 for (i = 0; i < 2; i++) {
2418 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2420 unittest(0, "%s with device @\"%s\" %s\n",
2421 overlay_name_from_nr(overlay_nr + i),
2422 unittest_path(unittest_nr + i,
2424 !before ? "enabled" : "disabled");
2429 /* apply the overlays */
2431 EXPECT_BEGIN(KERN_INFO,
2432 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2434 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2436 ret = overlay_data_apply(overlay_name, &ovcs_id);
2439 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2442 save_ovcs_id[0] = ovcs_id;
2443 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2445 EXPECT_END(KERN_INFO,
2446 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2448 EXPECT_BEGIN(KERN_INFO,
2449 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2451 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2453 ret = overlay_data_apply(overlay_name, &ovcs_id);
2456 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2459 save_ovcs_id[1] = ovcs_id;
2460 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2462 EXPECT_END(KERN_INFO,
2463 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2466 for (i = 0; i < 2; i++) {
2467 /* unittest device must be in after state */
2468 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2470 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2471 overlay_name_from_nr(overlay_nr + i),
2472 unittest_path(unittest_nr + i,
2474 !after ? "enabled" : "disabled");
2479 for (i = 1; i >= 0; i--) {
2480 ovcs_id = save_ovcs_id[i];
2481 if (of_overlay_remove(&ovcs_id)) {
2482 unittest(0, "%s failed destroy @\"%s\"\n",
2483 overlay_name_from_nr(overlay_nr + i),
2484 unittest_path(unittest_nr + i,
2488 of_unittest_untrack_overlay(save_ovcs_id[i]);
2491 for (i = 0; i < 2; i++) {
2492 /* unittest device must be again in before state */
2493 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2495 unittest(0, "%s with device @\"%s\" %s\n",
2496 overlay_name_from_nr(overlay_nr + i),
2497 unittest_path(unittest_nr + i,
2499 !before ? "enabled" : "disabled");
2504 unittest(1, "overlay test %d passed\n", 6);
2508 /* test overlay application in sequence */
2509 static void __init of_unittest_overlay_8(void)
2511 int i, save_ovcs_id[2], ovcs_id;
2512 int overlay_nr = 8, unittest_nr = 8;
2513 const char *overlay_name;
2516 /* we don't care about device state in this test */
2518 EXPECT_BEGIN(KERN_INFO,
2519 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2521 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2523 ret = overlay_data_apply(overlay_name, &ovcs_id);
2525 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2527 EXPECT_END(KERN_INFO,
2528 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2533 save_ovcs_id[0] = ovcs_id;
2534 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2536 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2538 EXPECT_BEGIN(KERN_INFO,
2539 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2541 /* apply the overlays */
2542 ret = overlay_data_apply(overlay_name, &ovcs_id);
2544 EXPECT_END(KERN_INFO,
2545 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2548 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2552 save_ovcs_id[1] = ovcs_id;
2553 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2555 /* now try to remove first overlay (it should fail) */
2556 ovcs_id = save_ovcs_id[0];
2558 EXPECT_BEGIN(KERN_INFO,
2559 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2561 EXPECT_BEGIN(KERN_INFO,
2562 "OF: overlay: overlay #6 is not topmost");
2564 ret = of_overlay_remove(&ovcs_id);
2566 EXPECT_END(KERN_INFO,
2567 "OF: overlay: overlay #6 is not topmost");
2569 EXPECT_END(KERN_INFO,
2570 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2574 * Should never get here. If we do, expect a lot of
2575 * subsequent tracking and overlay removal related errors.
2577 unittest(0, "%s was destroyed @\"%s\"\n",
2578 overlay_name_from_nr(overlay_nr + 0),
2579 unittest_path(unittest_nr,
2584 /* removing them in order should work */
2585 for (i = 1; i >= 0; i--) {
2586 ovcs_id = save_ovcs_id[i];
2587 if (of_overlay_remove(&ovcs_id)) {
2588 unittest(0, "%s not destroyed @\"%s\"\n",
2589 overlay_name_from_nr(overlay_nr + i),
2590 unittest_path(unittest_nr,
2594 of_unittest_untrack_overlay(save_ovcs_id[i]);
2597 unittest(1, "overlay test %d passed\n", 8);
2600 /* test insertion of a bus with parent devices */
2601 static void __init of_unittest_overlay_10(void)
2606 /* device should disable */
2607 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2609 if (unittest(ret == 0,
2610 "overlay test %d failed; overlay application\n", 10))
2613 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2614 unittest_path(10, PDEV_OVERLAY));
2615 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2618 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2621 unittest(ret, "overlay test %d failed; no child device\n", 10);
2624 /* test insertion of a bus with parent devices (and revert) */
2625 static void __init of_unittest_overlay_11(void)
2629 /* device should disable */
2630 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2633 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2636 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2638 struct unittest_i2c_bus_data {
2639 struct platform_device *pdev;
2640 struct i2c_adapter adap;
2643 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2644 struct i2c_msg *msgs, int num)
2646 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2653 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2655 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2658 static const struct i2c_algorithm unittest_i2c_algo = {
2659 .master_xfer = unittest_i2c_master_xfer,
2660 .functionality = unittest_i2c_functionality,
2663 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2665 struct device *dev = &pdev->dev;
2666 struct device_node *np = dev->of_node;
2667 struct unittest_i2c_bus_data *std;
2668 struct i2c_adapter *adap;
2672 dev_err(dev, "No OF data for device\n");
2677 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2679 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2683 /* link them together */
2685 platform_set_drvdata(pdev, std);
2688 i2c_set_adapdata(adap, std);
2690 strscpy(adap->name, pdev->name, sizeof(adap->name));
2691 adap->class = I2C_CLASS_DEPRECATED;
2692 adap->algo = &unittest_i2c_algo;
2693 adap->dev.parent = dev;
2694 adap->dev.of_node = dev->of_node;
2695 adap->timeout = 5 * HZ;
2698 ret = i2c_add_numbered_adapter(adap);
2700 dev_err(dev, "Failed to add I2C adapter\n");
2707 static void unittest_i2c_bus_remove(struct platform_device *pdev)
2709 struct device *dev = &pdev->dev;
2710 struct device_node *np = dev->of_node;
2711 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2713 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2714 i2c_del_adapter(&std->adap);
2717 static const struct of_device_id unittest_i2c_bus_match[] = {
2718 { .compatible = "unittest-i2c-bus", },
2722 static struct platform_driver unittest_i2c_bus_driver = {
2723 .probe = unittest_i2c_bus_probe,
2724 .remove_new = unittest_i2c_bus_remove,
2726 .name = "unittest-i2c-bus",
2727 .of_match_table = unittest_i2c_bus_match,
2731 static int unittest_i2c_dev_probe(struct i2c_client *client)
2733 struct device *dev = &client->dev;
2734 struct device_node *np = client->dev.of_node;
2737 dev_err(dev, "No OF node\n");
2741 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2746 static void unittest_i2c_dev_remove(struct i2c_client *client)
2748 struct device *dev = &client->dev;
2749 struct device_node *np = client->dev.of_node;
2751 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2754 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2755 { .name = "unittest-i2c-dev" },
2759 static struct i2c_driver unittest_i2c_dev_driver = {
2761 .name = "unittest-i2c-dev",
2763 .probe = unittest_i2c_dev_probe,
2764 .remove = unittest_i2c_dev_remove,
2765 .id_table = unittest_i2c_dev_id,
2768 #if IS_BUILTIN(CONFIG_I2C_MUX)
2770 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2775 static int unittest_i2c_mux_probe(struct i2c_client *client)
2778 struct device *dev = &client->dev;
2779 struct i2c_adapter *adap = client->adapter;
2780 struct device_node *np = client->dev.of_node, *child;
2781 struct i2c_mux_core *muxc;
2784 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2787 dev_err(dev, "No OF node\n");
2792 for_each_child_of_node(np, child) {
2793 if (of_property_read_u32(child, "reg", ®))
2795 if (max_reg == (u32)-1 || reg > max_reg)
2798 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2800 dev_err(dev, "No channels\n");
2804 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2805 unittest_i2c_mux_select_chan, NULL);
2808 for (i = 0; i < nchans; i++) {
2809 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2810 dev_err(dev, "Failed to register mux #%d\n", i);
2811 i2c_mux_del_adapters(muxc);
2816 i2c_set_clientdata(client, muxc);
2821 static void unittest_i2c_mux_remove(struct i2c_client *client)
2823 struct device *dev = &client->dev;
2824 struct device_node *np = client->dev.of_node;
2825 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2827 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2828 i2c_mux_del_adapters(muxc);
2831 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2832 { .name = "unittest-i2c-mux" },
2836 static struct i2c_driver unittest_i2c_mux_driver = {
2838 .name = "unittest-i2c-mux",
2840 .probe = unittest_i2c_mux_probe,
2841 .remove = unittest_i2c_mux_remove,
2842 .id_table = unittest_i2c_mux_id,
2847 static int of_unittest_overlay_i2c_init(void)
2851 ret = i2c_add_driver(&unittest_i2c_dev_driver);
2852 if (unittest(ret == 0,
2853 "could not register unittest i2c device driver\n"))
2856 ret = platform_driver_register(&unittest_i2c_bus_driver);
2858 if (unittest(ret == 0,
2859 "could not register unittest i2c bus driver\n"))
2862 #if IS_BUILTIN(CONFIG_I2C_MUX)
2864 EXPECT_BEGIN(KERN_INFO,
2865 "i2c i2c-1: Added multiplexed i2c bus 2");
2867 ret = i2c_add_driver(&unittest_i2c_mux_driver);
2869 EXPECT_END(KERN_INFO,
2870 "i2c i2c-1: Added multiplexed i2c bus 2");
2872 if (unittest(ret == 0,
2873 "could not register unittest i2c mux driver\n"))
2880 static void of_unittest_overlay_i2c_cleanup(void)
2882 #if IS_BUILTIN(CONFIG_I2C_MUX)
2883 i2c_del_driver(&unittest_i2c_mux_driver);
2885 platform_driver_unregister(&unittest_i2c_bus_driver);
2886 i2c_del_driver(&unittest_i2c_dev_driver);
2889 static void __init of_unittest_overlay_i2c_12(void)
2893 /* device should enable */
2894 EXPECT_BEGIN(KERN_INFO,
2895 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2897 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2899 EXPECT_END(KERN_INFO,
2900 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2905 unittest(1, "overlay test %d passed\n", 12);
2908 /* test deactivation of device */
2909 static void __init of_unittest_overlay_i2c_13(void)
2913 EXPECT_BEGIN(KERN_INFO,
2914 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2916 /* device should disable */
2917 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2919 EXPECT_END(KERN_INFO,
2920 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2925 unittest(1, "overlay test %d passed\n", 13);
2928 /* just check for i2c mux existence */
2929 static void of_unittest_overlay_i2c_14(void)
2933 static void __init of_unittest_overlay_i2c_15(void)
2937 /* device should enable */
2938 EXPECT_BEGIN(KERN_INFO,
2939 "i2c i2c-1: Added multiplexed i2c bus 3");
2941 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2943 EXPECT_END(KERN_INFO,
2944 "i2c i2c-1: Added multiplexed i2c bus 3");
2949 unittest(1, "overlay test %d passed\n", 15);
2954 static inline void of_unittest_overlay_i2c_14(void) { }
2955 static inline void of_unittest_overlay_i2c_15(void) { }
2959 static int of_notify(struct notifier_block *nb, unsigned long action,
2962 struct of_overlay_notify_data *nd = arg;
2963 struct device_node *found;
2967 * For overlay_16 .. overlay_19, check that returning an error
2968 * works for each of the actions by setting an arbitrary return
2969 * error number that matches the test number. e.g. for unittest16,
2970 * ret = -EBUSY which is -16.
2972 * OVERLAY_INFO() for the overlays is declared to expect the same
2973 * error number, so overlay_data_apply() will return no error.
2975 * overlay_20 will return NOTIFY_DONE
2979 of_node_get(nd->overlay);
2983 case OF_OVERLAY_PRE_APPLY:
2984 found = of_find_node_by_name(nd->overlay, "test-unittest16");
2991 case OF_OVERLAY_POST_APPLY:
2992 found = of_find_node_by_name(nd->overlay, "test-unittest17");
2999 case OF_OVERLAY_PRE_REMOVE:
3000 found = of_find_node_by_name(nd->overlay, "test-unittest18");
3007 case OF_OVERLAY_POST_REMOVE:
3008 found = of_find_node_by_name(nd->overlay, "test-unittest19");
3015 default: /* should not happen */
3016 of_node_put(nd->overlay);
3022 return notifier_from_errno(ret);
3027 static struct notifier_block of_nb = {
3028 .notifier_call = of_notify,
3031 static void __init of_unittest_overlay_notify(void)
3036 ret = of_overlay_notifier_register(&of_nb);
3038 "of_overlay_notifier_register() failed, ret = %d\n", ret);
3043 * The overlays are applied by overlay_data_apply()
3044 * instead of of_unittest_apply_overlay() so that they
3045 * will not be tracked. Thus they will not be removed
3046 * by of_unittest_remove_tracked_overlays().
3048 * Applying overlays 16 - 19 will each trigger an error for a
3049 * different action in of_notify().
3051 * Applying overlay 20 will not trigger any error in of_notify().
3054 /* --- overlay 16 --- */
3056 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3058 unittest(overlay_data_apply("overlay_16", &ovcs_id),
3059 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
3061 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3063 unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
3065 /* --- overlay 17 --- */
3067 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3069 unittest(overlay_data_apply("overlay_17", &ovcs_id),
3070 "test OF_OVERLAY_POST_APPLY notify injected error\n");
3072 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3074 unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
3076 /* --- overlay 18 --- */
3078 unittest(overlay_data_apply("overlay_18", &ovcs_id),
3079 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3081 unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
3084 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3086 ret = of_overlay_remove(&ovcs_id);
3087 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3088 if (ret == -EXDEV) {
3090 * change set ovcs_id should still exist
3092 unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3094 unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3097 unittest(1, "ovcs_id not created for overlay_18\n");
3100 unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3102 /* --- overlay 19 --- */
3104 unittest(overlay_data_apply("overlay_19", &ovcs_id),
3105 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3107 unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3110 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3111 ret = of_overlay_remove(&ovcs_id);
3112 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3114 unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3116 unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3118 unittest(1, "ovcs_id removed for overlay_19\n");
3121 unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3124 /* --- overlay 20 --- */
3126 unittest(overlay_data_apply("overlay_20", &ovcs_id),
3127 "overlay notify no injected error\n");
3130 ret = of_overlay_remove(&ovcs_id);
3132 unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3135 unittest(1, "ovcs_id not created for overlay_20\n");
3138 unittest(!of_overlay_notifier_unregister(&of_nb),
3139 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3142 static void __init of_unittest_overlay(void)
3144 struct device_node *bus_np = NULL;
3147 if (platform_driver_register(&unittest_driver)) {
3148 unittest(0, "could not register unittest driver\n");
3152 bus_np = of_find_node_by_path(bus_path);
3153 if (bus_np == NULL) {
3154 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3158 if (of_platform_default_populate(bus_np, NULL, NULL)) {
3159 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3163 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3164 unittest(0, "could not find unittest0 @ \"%s\"\n",
3165 unittest_path(100, PDEV_OVERLAY));
3169 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3170 unittest(0, "unittest1 @ \"%s\" should not exist\n",
3171 unittest_path(101, PDEV_OVERLAY));
3175 unittest(1, "basic infrastructure of overlays passed");
3177 /* tests in sequence */
3178 of_unittest_overlay_0();
3179 of_unittest_overlay_1();
3180 of_unittest_overlay_2();
3181 of_unittest_overlay_3();
3182 of_unittest_overlay_4();
3183 for (i = 0; i < 3; i++)
3184 of_unittest_overlay_5();
3185 of_unittest_overlay_6();
3186 of_unittest_overlay_8();
3188 of_unittest_overlay_10();
3189 of_unittest_overlay_11();
3191 #if IS_BUILTIN(CONFIG_I2C)
3192 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3195 of_unittest_overlay_i2c_12();
3196 of_unittest_overlay_i2c_13();
3197 of_unittest_overlay_i2c_14();
3198 of_unittest_overlay_i2c_15();
3200 of_unittest_overlay_i2c_cleanup();
3203 of_unittest_overlay_gpio();
3205 of_unittest_remove_tracked_overlays();
3207 of_unittest_overlay_notify();
3210 of_node_put(bus_np);
3214 static inline void __init of_unittest_overlay(void) { }
3217 static void __init of_unittest_lifecycle(void)
3219 #ifdef CONFIG_OF_DYNAMIC
3220 unsigned int refcount;
3221 int found_refcount_one = 0;
3223 struct device_node *np;
3224 struct device_node *prev_sibling, *next_sibling;
3225 const char *refcount_path = "/testcase-data/refcount-node";
3226 const char *refcount_parent_path = "/testcase-data";
3229 * Node lifecycle tests, non-dynamic node:
3231 * - Decrementing refcount to zero via of_node_put() should cause the
3232 * attempt to free the node memory by of_node_release() to fail
3233 * because the node is not a dynamic node.
3235 * - Decrementing refcount past zero should result in additional
3239 np = of_find_node_by_path(refcount_path);
3240 unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3242 goto out_skip_tests;
3244 while (!found_refcount_one) {
3246 if (put_count++ > 10) {
3247 unittest(0, "guardrail to avoid infinite loop\n");
3248 goto out_skip_tests;
3251 refcount = kref_read(&np->kobj.kref);
3253 found_refcount_one = 1;
3258 EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3261 * refcount is now one, decrementing to zero will result in a call to
3262 * of_node_release() to free the node's memory, which should result
3265 unittest(1, "/testcase-data/refcount-node is one");
3268 EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3272 * expect stack trace for subsequent of_node_put():
3273 * __refcount_sub_and_test() calls:
3274 * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3276 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3277 * the first three lines, and the last line.
3279 EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3280 EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3281 EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3282 EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3284 /* refcount is now zero, this should fail */
3285 unittest(1, "/testcase-data/refcount-node is zero");
3288 EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3289 EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3290 EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3291 EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3294 * Q. do we expect to get yet another warning?
3295 * A. no, the WARNING is from WARN_ONCE()
3297 EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3298 EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3299 EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3300 EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3302 unittest(1, "/testcase-data/refcount-node is zero, second time");
3305 EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3306 EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3307 EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3308 EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3311 * refcount of zero will trigger stack traces from any further
3312 * attempt to of_node_get() node "refcount-node". One example of
3313 * this is where of_unittest_check_node_linkage() will recursively
3314 * scan the tree, with 'for_each_child_of_node()' doing an
3315 * of_node_get() of the children of a node.
3317 * Prevent the stack trace by removing node "refcount-node" from
3318 * its parent's child list.
3320 * WARNING: EVIL, EVIL, EVIL:
3322 * Directly manipulate the child list of node /testcase-data to
3323 * remove child refcount-node. This is ignoring all proper methods
3324 * of removing a child and will leak a small amount of memory.
3327 np = of_find_node_by_path(refcount_parent_path);
3328 unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3329 unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3333 prev_sibling = np->child;
3334 next_sibling = prev_sibling->sibling;
3335 if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3336 np->child = next_sibling;
3337 next_sibling = next_sibling->sibling;
3339 while (next_sibling) {
3340 if (!strcmp(next_sibling->full_name, "refcount-node"))
3341 prev_sibling->sibling = next_sibling->sibling;
3342 prev_sibling = next_sibling;
3343 next_sibling = next_sibling->sibling;
3351 unittest(0, "One or more lifecycle tests skipped\n");
3354 #ifdef CONFIG_OF_OVERLAY
3357 * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3358 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
3361 #define OVERLAY_INFO_EXTERN(overlay_name) \
3362 extern uint8_t __dtbo_##overlay_name##_begin[]; \
3363 extern uint8_t __dtbo_##overlay_name##_end[]
3365 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3366 { .dtbo_begin = __dtbo_##overlay_name##_begin, \
3367 .dtbo_end = __dtbo_##overlay_name##_end, \
3368 .expected_result = expected, \
3369 .expected_result_remove = expected_remove, \
3370 .name = #overlay_name, \
3373 struct overlay_info {
3374 uint8_t *dtbo_begin;
3376 int expected_result;
3377 int expected_result_remove; /* if apply failed */
3382 OVERLAY_INFO_EXTERN(overlay_base);
3383 OVERLAY_INFO_EXTERN(overlay);
3384 OVERLAY_INFO_EXTERN(overlay_0);
3385 OVERLAY_INFO_EXTERN(overlay_1);
3386 OVERLAY_INFO_EXTERN(overlay_2);
3387 OVERLAY_INFO_EXTERN(overlay_3);
3388 OVERLAY_INFO_EXTERN(overlay_4);
3389 OVERLAY_INFO_EXTERN(overlay_5);
3390 OVERLAY_INFO_EXTERN(overlay_6);
3391 OVERLAY_INFO_EXTERN(overlay_7);
3392 OVERLAY_INFO_EXTERN(overlay_8);
3393 OVERLAY_INFO_EXTERN(overlay_9);
3394 OVERLAY_INFO_EXTERN(overlay_10);
3395 OVERLAY_INFO_EXTERN(overlay_11);
3396 OVERLAY_INFO_EXTERN(overlay_12);
3397 OVERLAY_INFO_EXTERN(overlay_13);
3398 OVERLAY_INFO_EXTERN(overlay_15);
3399 OVERLAY_INFO_EXTERN(overlay_16);
3400 OVERLAY_INFO_EXTERN(overlay_17);
3401 OVERLAY_INFO_EXTERN(overlay_18);
3402 OVERLAY_INFO_EXTERN(overlay_19);
3403 OVERLAY_INFO_EXTERN(overlay_20);
3404 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3405 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3406 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3407 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3408 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3409 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3410 OVERLAY_INFO_EXTERN(overlay_pci_node);
3411 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3412 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3413 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3414 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3415 OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
3417 /* entries found by name */
3418 static struct overlay_info overlays[] = {
3419 OVERLAY_INFO(overlay_base, -9999, 0),
3420 OVERLAY_INFO(overlay, 0, 0),
3421 OVERLAY_INFO(overlay_0, 0, 0),
3422 OVERLAY_INFO(overlay_1, 0, 0),
3423 OVERLAY_INFO(overlay_2, 0, 0),
3424 OVERLAY_INFO(overlay_3, 0, 0),
3425 OVERLAY_INFO(overlay_4, 0, 0),
3426 OVERLAY_INFO(overlay_5, 0, 0),
3427 OVERLAY_INFO(overlay_6, 0, 0),
3428 OVERLAY_INFO(overlay_7, 0, 0),
3429 OVERLAY_INFO(overlay_8, 0, 0),
3430 OVERLAY_INFO(overlay_9, 0, 0),
3431 OVERLAY_INFO(overlay_10, 0, 0),
3432 OVERLAY_INFO(overlay_11, 0, 0),
3433 OVERLAY_INFO(overlay_12, 0, 0),
3434 OVERLAY_INFO(overlay_13, 0, 0),
3435 OVERLAY_INFO(overlay_15, 0, 0),
3436 OVERLAY_INFO(overlay_16, -EBUSY, 0),
3437 OVERLAY_INFO(overlay_17, -EEXIST, 0),
3438 OVERLAY_INFO(overlay_18, 0, 0),
3439 OVERLAY_INFO(overlay_19, 0, 0),
3440 OVERLAY_INFO(overlay_20, 0, 0),
3441 OVERLAY_INFO(overlay_gpio_01, 0, 0),
3442 OVERLAY_INFO(overlay_gpio_02a, 0, 0),
3443 OVERLAY_INFO(overlay_gpio_02b, 0, 0),
3444 OVERLAY_INFO(overlay_gpio_03, 0, 0),
3445 OVERLAY_INFO(overlay_gpio_04a, 0, 0),
3446 OVERLAY_INFO(overlay_gpio_04b, 0, 0),
3447 OVERLAY_INFO(overlay_pci_node, 0, 0),
3448 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
3449 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
3450 OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
3451 OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
3452 OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
3457 static struct device_node *overlay_base_root;
3459 static void * __init dt_alloc_memory(u64 size, u64 align)
3461 void *ptr = memblock_alloc(size, align);
3464 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
3465 __func__, size, align);
3471 * Create base device tree for the overlay unittest.
3473 * This is called from very early boot code.
3475 * Do as much as possible the same way as done in __unflatten_device_tree
3476 * and other early boot steps for the normal FDT so that the overlay base
3477 * unflattened tree will have the same characteristics as the real tree
3478 * (such as having memory allocated by the early allocator). The goal
3479 * is to test "the real thing" as much as possible, and test "test setup
3480 * code" as little as possible.
3482 * Have to stop before resolving phandles, because that uses kmalloc.
3484 void __init unittest_unflatten_overlay_base(void)
3486 struct overlay_info *info;
3491 const char *overlay_name = "overlay_base";
3493 for (info = overlays; info && info->name; info++) {
3494 if (!strcmp(overlay_name, info->name)) {
3500 pr_err("no overlay data for %s\n", overlay_name);
3504 info = &overlays[0];
3506 if (info->expected_result != -9999) {
3507 pr_err("No dtb 'overlay_base' to attach\n");
3511 data_size = info->dtbo_end - info->dtbo_begin;
3513 pr_err("No dtb 'overlay_base' to attach\n");
3517 size = fdt_totalsize(info->dtbo_begin);
3518 if (size != data_size) {
3519 pr_err("dtb 'overlay_base' header totalsize != actual size");
3523 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3525 pr_err("alloc for dtb 'overlay_base' failed");
3529 memcpy(new_fdt, info->dtbo_begin, size);
3531 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3532 dt_alloc_memory, true);
3536 * The purpose of of_unittest_overlay_data_add is to add an
3537 * overlay in the normal fashion. This is a test of the whole
3538 * picture, instead of testing individual elements.
3540 * A secondary purpose is to be able to verify that the contents of
3541 * /proc/device-tree/ contains the updated structure and values from
3542 * the overlay. That must be verified separately in user space.
3544 * Return 0 on unexpected error.
3546 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3548 struct overlay_info *info;
3554 for (info = overlays; info && info->name; info++) {
3555 if (!strcmp(overlay_name, info->name)) {
3561 pr_err("no overlay data for %s\n", overlay_name);
3565 size = info->dtbo_end - info->dtbo_begin;
3567 pr_err("no overlay data for %s\n", overlay_name);
3569 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
3572 *ovcs_id = info->ovcs_id;
3576 pr_debug("%s applied\n", overlay_name);
3579 if (ret != info->expected_result) {
3580 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3581 info->expected_result, ret, overlay_name);
3586 /* changeset may be partially applied */
3587 ret2 = of_overlay_remove(&info->ovcs_id);
3588 if (ret2 != info->expected_result_remove) {
3589 pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
3590 info->expected_result_remove, ret2,
3600 * The purpose of of_unittest_overlay_high_level is to add an overlay
3601 * in the normal fashion. This is a test of the whole picture,
3602 * instead of individual elements.
3604 * The first part of the function is _not_ normal overlay usage; it is
3605 * finishing splicing the base overlay device tree into the live tree.
3607 static __init void of_unittest_overlay_high_level(void)
3609 struct device_node *last_sibling;
3610 struct device_node *np;
3611 struct device_node *of_symbols;
3612 struct device_node *overlay_base_symbols;
3613 struct device_node **pprev;
3614 struct property *prop;
3617 if (!overlay_base_root) {
3618 unittest(0, "overlay_base_root not initialized\n");
3623 * Could not fixup phandles in unittest_unflatten_overlay_base()
3624 * because kmalloc() was not yet available.
3626 of_overlay_mutex_lock();
3627 of_resolve_phandles(overlay_base_root);
3628 of_overlay_mutex_unlock();
3632 * do not allow overlay_base to duplicate any node already in
3633 * tree, this greatly simplifies the code
3637 * remove overlay_base_root node "__local_fixups", after
3638 * being used by of_resolve_phandles()
3640 pprev = &overlay_base_root->child;
3641 for (np = overlay_base_root->child; np; np = np->sibling) {
3642 if (of_node_name_eq(np, "__local_fixups__")) {
3643 *pprev = np->sibling;
3646 pprev = &np->sibling;
3649 /* remove overlay_base_root node "__symbols__" if in live tree */
3650 of_symbols = of_get_child_by_name(of_root, "__symbols__");
3652 /* will have to graft properties from node into live tree */
3653 pprev = &overlay_base_root->child;
3654 for (np = overlay_base_root->child; np; np = np->sibling) {
3655 if (of_node_name_eq(np, "__symbols__")) {
3656 overlay_base_symbols = np;
3657 *pprev = np->sibling;
3660 pprev = &np->sibling;
3664 for_each_child_of_node(overlay_base_root, np) {
3665 struct device_node *base_child;
3666 for_each_child_of_node(of_root, base_child) {
3667 if (!strcmp(np->full_name, base_child->full_name)) {
3668 unittest(0, "illegal node name in overlay_base %pOFn",
3671 of_node_put(base_child);
3678 * overlay 'overlay_base' is not allowed to have root
3679 * properties, so only need to splice nodes into main device tree.
3681 * root node of *overlay_base_root will not be freed, it is lost
3685 for (np = overlay_base_root->child; np; np = np->sibling)
3686 np->parent = of_root;
3688 mutex_lock(&of_mutex);
3690 for (last_sibling = np = of_root->child; np; np = np->sibling)
3694 last_sibling->sibling = overlay_base_root->child;
3696 of_root->child = overlay_base_root->child;
3698 for_each_of_allnodes_from(overlay_base_root, np)
3699 __of_attach_node_sysfs(np);
3702 struct property *new_prop;
3703 for_each_property_of_node(overlay_base_symbols, prop) {
3705 new_prop = __of_prop_dup(prop, GFP_KERNEL);
3707 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3711 if (__of_add_property(of_symbols, new_prop)) {
3712 kfree(new_prop->name);
3713 kfree(new_prop->value);
3715 /* "name" auto-generated by unflatten */
3716 if (!strcmp(prop->name, "name"))
3718 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3722 if (__of_add_property_sysfs(of_symbols, new_prop)) {
3723 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3730 mutex_unlock(&of_mutex);
3733 /* now do the normal overlay usage test */
3735 /* --- overlay --- */
3737 EXPECT_BEGIN(KERN_ERR,
3738 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3739 EXPECT_BEGIN(KERN_ERR,
3740 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3741 EXPECT_BEGIN(KERN_ERR,
3742 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3743 EXPECT_BEGIN(KERN_ERR,
3744 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3745 EXPECT_BEGIN(KERN_ERR,
3746 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3747 EXPECT_BEGIN(KERN_ERR,
3748 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3749 EXPECT_BEGIN(KERN_ERR,
3750 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3751 EXPECT_BEGIN(KERN_ERR,
3752 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3753 EXPECT_BEGIN(KERN_ERR,
3754 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3755 EXPECT_BEGIN(KERN_ERR,
3756 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3757 EXPECT_BEGIN(KERN_ERR,
3758 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3760 ret = overlay_data_apply("overlay", NULL);
3762 EXPECT_END(KERN_ERR,
3763 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3764 EXPECT_END(KERN_ERR,
3765 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3766 EXPECT_END(KERN_ERR,
3767 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3768 EXPECT_END(KERN_ERR,
3769 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3770 EXPECT_END(KERN_ERR,
3771 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3772 EXPECT_END(KERN_ERR,
3773 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3774 EXPECT_END(KERN_ERR,
3775 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3776 EXPECT_END(KERN_ERR,
3777 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3778 EXPECT_END(KERN_ERR,
3779 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3780 EXPECT_END(KERN_ERR,
3781 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3782 EXPECT_END(KERN_ERR,
3783 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3785 unittest(ret, "Adding overlay 'overlay' failed\n");
3787 /* --- overlay_bad_add_dup_node --- */
3789 EXPECT_BEGIN(KERN_ERR,
3790 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3791 EXPECT_BEGIN(KERN_ERR,
3792 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3793 EXPECT_BEGIN(KERN_ERR,
3794 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3795 EXPECT_BEGIN(KERN_ERR,
3796 "OF: Error reverting changeset (-19)");
3798 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3799 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3801 EXPECT_END(KERN_ERR,
3802 "OF: Error reverting changeset (-19)");
3803 EXPECT_END(KERN_ERR,
3804 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3805 EXPECT_END(KERN_ERR,
3806 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3807 EXPECT_END(KERN_ERR,
3808 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3810 /* --- overlay_bad_add_dup_prop --- */
3812 EXPECT_BEGIN(KERN_ERR,
3813 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3814 EXPECT_BEGIN(KERN_ERR,
3815 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3816 EXPECT_BEGIN(KERN_ERR,
3817 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3818 EXPECT_BEGIN(KERN_ERR,
3819 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3820 EXPECT_BEGIN(KERN_ERR,
3821 "OF: Error reverting changeset (-19)");
3823 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3824 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3826 EXPECT_END(KERN_ERR,
3827 "OF: Error reverting changeset (-19)");
3828 EXPECT_END(KERN_ERR,
3829 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3830 EXPECT_END(KERN_ERR,
3831 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3832 EXPECT_END(KERN_ERR,
3833 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3834 EXPECT_END(KERN_ERR,
3835 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3837 /* --- overlay_bad_phandle --- */
3839 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
3840 "Adding overlay 'overlay_bad_phandle' failed\n");
3842 /* --- overlay_bad_symbol --- */
3844 EXPECT_BEGIN(KERN_ERR,
3845 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3846 EXPECT_BEGIN(KERN_ERR,
3847 "OF: Error reverting changeset (-19)");
3849 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
3850 "Adding overlay 'overlay_bad_symbol' failed\n");
3852 EXPECT_END(KERN_ERR,
3853 "OF: Error reverting changeset (-19)");
3854 EXPECT_END(KERN_ERR,
3855 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3857 /* --- overlay_bad_unresolved --- */
3859 EXPECT_BEGIN(KERN_ERR,
3860 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3861 EXPECT_BEGIN(KERN_ERR,
3862 "OF: resolver: overlay phandle fixup failed: -22");
3864 unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
3865 "Adding overlay 'overlay_bad_unresolved' failed\n");
3867 EXPECT_END(KERN_ERR,
3868 "OF: resolver: overlay phandle fixup failed: -22");
3869 EXPECT_END(KERN_ERR,
3870 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3875 mutex_unlock(&of_mutex);
3878 static int of_unittest_pci_dev_num;
3879 static int of_unittest_pci_child_num;
3882 * PCI device tree node test driver
3884 static const struct pci_device_id testdrv_pci_ids[] = {
3885 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
3889 static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3891 struct overlay_info *info;
3892 struct device_node *dn;
3896 dn = pdev->dev.of_node;
3898 dev_err(&pdev->dev, "does not find bus endpoint");
3902 for (info = overlays; info && info->name; info++) {
3903 if (!strcmp(info->name, "overlay_pci_node"))
3906 if (!info || !info->name) {
3907 dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
3911 size = info->dtbo_end - info->dtbo_begin;
3912 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
3917 of_platform_default_populate(dn, NULL, &pdev->dev);
3918 pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
3923 static void testdrv_remove(struct pci_dev *pdev)
3925 int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
3927 of_platform_depopulate(&pdev->dev);
3928 of_overlay_remove(&ovcs_id);
3931 static struct pci_driver testdrv_driver = {
3932 .name = "pci_dt_testdrv",
3933 .id_table = testdrv_pci_ids,
3934 .probe = testdrv_probe,
3935 .remove = testdrv_remove,
3938 static int unittest_pci_probe(struct platform_device *pdev)
3940 struct resource *res;
3944 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3949 while (dev && !dev_is_pci(dev))
3952 pr_err("unable to find parent device\n");
3956 exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
3957 unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
3958 (u64)res->start, exp_addr);
3960 of_unittest_pci_child_num++;
3965 static const struct of_device_id unittest_pci_of_match[] = {
3966 { .compatible = "unittest-pci" },
3970 static struct platform_driver unittest_pci_driver = {
3971 .probe = unittest_pci_probe,
3973 .name = "unittest-pci",
3974 .of_match_table = unittest_pci_of_match,
3978 static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
3980 struct device_node *pnp, *np = NULL;
3981 struct device *child_dev;
3986 pnp = pdev->dev.of_node;
3987 unittest(pnp, "Failed creating PCI dt node\n");
3992 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
3993 np = of_find_node_by_path(path);
3994 unittest(np, "Failed to get unittest-pci node under PCI node\n");
4000 reg = of_get_property(np, "reg", NULL);
4001 unittest(reg, "Failed to get reg property\n");
4005 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
4006 np = of_find_node_by_path(path);
4007 unittest(!np, "Child device tree node is not removed\n");
4008 child_dev = device_find_any_child(&pdev->dev);
4009 unittest(!child_dev, "Child device is not removed\n");
4020 static void __init of_unittest_pci_node(void)
4022 struct pci_dev *pdev = NULL;
4025 if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
4028 rc = pci_register_driver(&testdrv_driver);
4029 unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
4033 rc = platform_driver_register(&unittest_pci_driver);
4034 if (unittest(!rc, "Failed to register unittest pci driver\n")) {
4035 pci_unregister_driver(&testdrv_driver);
4039 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
4040 of_unittest_pci_node_verify(pdev, true);
4041 of_unittest_pci_dev_num++;
4046 unittest(of_unittest_pci_dev_num,
4047 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
4048 unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
4049 "Child device number %d is not expected %d", of_unittest_pci_child_num,
4050 of_unittest_pci_dev_num);
4052 platform_driver_unregister(&unittest_pci_driver);
4053 pci_unregister_driver(&testdrv_driver);
4055 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
4056 of_unittest_pci_node_verify(pdev, false);
4062 static inline __init void of_unittest_overlay_high_level(void) {}
4063 static inline __init void of_unittest_pci_node(void) { }
4067 static int __init of_unittest(void)
4069 struct device_node *np;
4072 pr_info("start of unittest - you will see error messages\n");
4074 /* Taint the kernel so we know we've run tests. */
4075 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
4077 /* adding data for unittest */
4079 if (IS_ENABLED(CONFIG_UML))
4080 unittest_unflatten_overlay_base();
4082 res = unittest_data_add();
4086 of_aliases = of_find_node_by_path("/aliases");
4088 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4090 pr_info("No testcase data in device tree; not running tests\n");
4095 of_unittest_check_tree_linkage();
4096 of_unittest_check_phandles();
4097 of_unittest_find_node_by_name();
4098 of_unittest_dynamic();
4099 of_unittest_parse_phandle_with_args();
4100 of_unittest_parse_phandle_with_args_map();
4101 of_unittest_printf();
4102 of_unittest_property_string();
4103 of_unittest_property_copy();
4104 of_unittest_changeset();
4105 of_unittest_parse_interrupts();
4106 of_unittest_parse_interrupts_extended();
4107 of_unittest_dma_get_max_cpu_address();
4108 of_unittest_parse_dma_ranges();
4109 of_unittest_pci_dma_ranges();
4110 of_unittest_bus_ranges();
4111 of_unittest_bus_3cell_ranges();
4113 of_unittest_translate_addr();
4114 of_unittest_match_node();
4115 of_unittest_platform_populate();
4116 of_unittest_overlay();
4117 of_unittest_lifecycle();
4118 of_unittest_pci_node();
4120 /* Double check linkage after removing testcase data */
4121 of_unittest_check_tree_linkage();
4123 of_unittest_overlay_high_level();
4125 pr_info("end of unittest - %i passed, %i failed\n",
4126 unittest_results.passed, unittest_results.failed);
4130 late_initcall(of_unittest);