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 of_node_put(args.np);
464 /* Check for missing list property */
465 memset(&args, 0, sizeof(args));
466 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
467 "#phandle-cells", 0, &args);
468 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
469 rc = of_count_phandle_with_args(np, "phandle-list-missing",
471 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
473 /* Check for missing cells property */
474 memset(&args, 0, sizeof(args));
476 EXPECT_BEGIN(KERN_INFO,
477 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
479 rc = of_parse_phandle_with_args(np, "phandle-list",
480 "#phandle-cells-missing", 0, &args);
482 EXPECT_END(KERN_INFO,
483 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
485 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
487 EXPECT_BEGIN(KERN_INFO,
488 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
490 rc = of_count_phandle_with_args(np, "phandle-list",
491 "#phandle-cells-missing");
493 EXPECT_END(KERN_INFO,
494 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
496 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
498 /* Check for bad phandle in list */
499 memset(&args, 0, sizeof(args));
501 EXPECT_BEGIN(KERN_INFO,
502 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
504 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
505 "#phandle-cells", 0, &args);
507 EXPECT_END(KERN_INFO,
508 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
510 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
512 EXPECT_BEGIN(KERN_INFO,
513 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
515 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
518 EXPECT_END(KERN_INFO,
519 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
521 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
523 /* Check for incorrectly formed argument list */
524 memset(&args, 0, sizeof(args));
526 EXPECT_BEGIN(KERN_INFO,
527 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
529 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
530 "#phandle-cells", 1, &args);
532 EXPECT_END(KERN_INFO,
533 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
535 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
537 EXPECT_BEGIN(KERN_INFO,
538 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
540 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
543 EXPECT_END(KERN_INFO,
544 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
546 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
549 static void __init of_unittest_parse_phandle_with_args_map(void)
551 struct device_node *np, *p[6] = {};
552 struct of_phandle_args args;
553 unsigned int prefs[6];
556 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
558 pr_err("missing testcase data\n");
562 p[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
563 p[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
564 p[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
565 p[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
566 p[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
567 p[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
568 for (i = 0; i < ARRAY_SIZE(p); ++i) {
570 pr_err("missing testcase data\n");
573 prefs[i] = kref_read(&p[i]->kobj.kref);
576 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
577 unittest(rc == 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc);
579 for (i = 0; i < 9; i++) {
582 memset(&args, 0, sizeof(args));
583 rc = of_parse_phandle_with_args_map(np, "phandle-list",
584 "phandle", i, &args);
586 /* Test the values from tests-phandle.dtsi */
590 passed &= (args.np == p[1]);
591 passed &= (args.args_count == 1);
592 passed &= (args.args[0] == 1);
596 passed &= (args.np == p[3]);
597 passed &= (args.args_count == 3);
598 passed &= (args.args[0] == 2);
599 passed &= (args.args[1] == 5);
600 passed &= (args.args[2] == 3);
603 passed &= (rc == -ENOENT);
607 passed &= (args.np == p[0]);
608 passed &= (args.args_count == 0);
612 passed &= (args.np == p[1]);
613 passed &= (args.args_count == 1);
614 passed &= (args.args[0] == 3);
618 passed &= (args.np == p[0]);
619 passed &= (args.args_count == 0);
623 passed &= (args.np == p[2]);
624 passed &= (args.args_count == 2);
625 passed &= (args.args[0] == 15);
626 passed &= (args.args[1] == 0x20);
630 passed &= (args.np == p[3]);
631 passed &= (args.args_count == 3);
632 passed &= (args.args[0] == 2);
633 passed &= (args.args[1] == 5);
634 passed &= (args.args[2] == 3);
637 passed &= (rc == -ENOENT);
643 unittest(passed, "index %i - data error on node %s rc=%i\n",
644 i, args.np->full_name, rc);
647 of_node_put(args.np);
650 /* Check for missing list property */
651 memset(&args, 0, sizeof(args));
652 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
653 "phandle", 0, &args);
654 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
656 /* Check for missing cells,map,mask property */
657 memset(&args, 0, sizeof(args));
659 EXPECT_BEGIN(KERN_INFO,
660 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
662 rc = of_parse_phandle_with_args_map(np, "phandle-list",
663 "phandle-missing", 0, &args);
664 EXPECT_END(KERN_INFO,
665 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
667 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
669 /* Check for bad phandle in list */
670 memset(&args, 0, sizeof(args));
672 EXPECT_BEGIN(KERN_INFO,
673 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
675 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
676 "phandle", 0, &args);
677 EXPECT_END(KERN_INFO,
678 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
680 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
682 /* Check for incorrectly formed argument list */
683 memset(&args, 0, sizeof(args));
685 EXPECT_BEGIN(KERN_INFO,
686 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
688 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
689 "phandle", 1, &args);
690 EXPECT_END(KERN_INFO,
691 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
693 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
695 for (i = 0; i < ARRAY_SIZE(p); ++i) {
696 unittest(prefs[i] == kref_read(&p[i]->kobj.kref),
697 "provider%d: expected:%d got:%d\n",
698 i, prefs[i], kref_read(&p[i]->kobj.kref));
703 static void __init of_unittest_property_string(void)
705 const char *strings[4];
706 struct device_node *np;
709 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
711 pr_err("No testcase data in device tree\n");
715 rc = of_property_match_string(np, "phandle-list-names", "first");
716 unittest(rc == 0, "first expected:0 got:%i\n", rc);
717 rc = of_property_match_string(np, "phandle-list-names", "second");
718 unittest(rc == 1, "second expected:1 got:%i\n", rc);
719 rc = of_property_match_string(np, "phandle-list-names", "third");
720 unittest(rc == 2, "third expected:2 got:%i\n", rc);
721 rc = of_property_match_string(np, "phandle-list-names", "fourth");
722 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
723 rc = of_property_match_string(np, "missing-property", "blah");
724 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
725 rc = of_property_match_string(np, "empty-property", "blah");
726 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
727 rc = of_property_match_string(np, "unterminated-string", "blah");
728 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
730 /* of_property_count_strings() tests */
731 rc = of_property_count_strings(np, "string-property");
732 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
733 rc = of_property_count_strings(np, "phandle-list-names");
734 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
735 rc = of_property_count_strings(np, "unterminated-string");
736 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
737 rc = of_property_count_strings(np, "unterminated-string-list");
738 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
740 /* of_property_read_string_index() tests */
741 rc = of_property_read_string_index(np, "string-property", 0, strings);
742 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
744 rc = of_property_read_string_index(np, "string-property", 1, strings);
745 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
746 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
747 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
748 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
749 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
750 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
751 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
753 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
754 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
756 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
757 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
758 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
759 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
761 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
762 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
765 /* of_property_read_string_array() tests */
766 rc = of_property_read_string_array(np, "string-property", strings, 4);
767 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
768 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
769 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
770 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
771 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
772 /* -- An incorrectly formed string should cause a failure */
773 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
774 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
775 /* -- parsing the correctly formed strings should still work: */
777 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
778 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
780 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
781 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
784 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
785 (p1)->value && (p2)->value && \
786 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
787 !strcmp((p1)->name, (p2)->name))
788 static void __init of_unittest_property_copy(void)
790 #ifdef CONFIG_OF_DYNAMIC
791 struct property p1 = { .name = "p1", .length = 0, .value = "" };
792 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
793 struct property *new;
795 new = __of_prop_dup(&p1, GFP_KERNEL);
796 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
801 new = __of_prop_dup(&p2, GFP_KERNEL);
802 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
809 static void __init of_unittest_changeset(void)
811 #ifdef CONFIG_OF_DYNAMIC
813 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
814 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
815 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
816 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
817 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
818 struct property *ppremove;
819 struct device_node *n1, *n2, *n21, *n22, *nchangeset, *nremove, *parent, *np;
820 static const char * const str_array[] = { "str1", "str2", "str3" };
821 const u32 u32_array[] = { 1, 2, 3 };
822 struct of_changeset chgset;
823 const char *propstr = NULL;
825 n1 = __of_node_dup(NULL, "n1");
826 unittest(n1, "testcase setup failure\n");
828 n2 = __of_node_dup(NULL, "n2");
829 unittest(n2, "testcase setup failure\n");
831 n21 = __of_node_dup(NULL, "n21");
832 unittest(n21, "testcase setup failure %p\n", n21);
834 nchangeset = of_find_node_by_path("/testcase-data/changeset");
835 nremove = of_get_child_by_name(nchangeset, "node-remove");
836 unittest(nremove, "testcase setup failure\n");
838 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
839 unittest(ppadd, "testcase setup failure\n");
841 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
842 unittest(ppname_n1, "testcase setup failure\n");
844 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
845 unittest(ppname_n2, "testcase setup failure\n");
847 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
848 unittest(ppname_n21, "testcase setup failure\n");
850 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
851 unittest(ppupdate, "testcase setup failure\n");
858 ppremove = of_find_property(parent, "prop-remove", NULL);
859 unittest(ppremove, "failed to find removal prop");
861 of_changeset_init(&chgset);
863 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
864 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
866 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
867 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
869 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
870 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
872 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
874 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
875 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
876 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
877 n22 = of_changeset_create_node(&chgset, n2, "n22");
878 unittest(n22, "fail create n22\n");
879 unittest(!of_changeset_add_prop_string(&chgset, n22, "prop-str", "abcd"),
880 "fail add prop prop-str");
881 unittest(!of_changeset_add_prop_string_array(&chgset, n22, "prop-str-array",
882 (const char **)str_array,
883 ARRAY_SIZE(str_array)),
884 "fail add prop prop-str-array");
885 unittest(!of_changeset_add_prop_u32_array(&chgset, n22, "prop-u32-array",
886 u32_array, ARRAY_SIZE(u32_array)),
887 "fail add prop prop-u32-array");
889 unittest(!of_changeset_apply(&chgset), "apply failed\n");
891 of_node_put(nchangeset);
893 /* Make sure node names are constructed correctly */
894 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
895 "'%pOF' not added\n", n21);
897 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n22")),
898 "'%pOF' not added\n", n22);
901 unittest(!of_changeset_revert(&chgset), "revert failed\n");
903 unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
904 "'%pOF' still present after revert\n", n21);
906 ppremove = of_find_property(parent, "prop-remove", NULL);
907 unittest(ppremove, "failed to find removed prop after revert\n");
909 ret = of_property_read_string(parent, "prop-update", &propstr);
910 unittest(!ret, "failed to find updated prop after revert\n");
912 unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
914 of_changeset_destroy(&chgset);
923 static void __init of_unittest_dma_get_max_cpu_address(void)
925 struct device_node *np;
926 phys_addr_t cpu_addr;
928 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
931 np = of_find_node_by_path("/testcase-data/address-tests");
933 pr_err("missing testcase data\n");
937 cpu_addr = of_dma_get_max_cpu_address(np);
938 unittest(cpu_addr == 0x4fffffff,
939 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
940 &cpu_addr, 0x4fffffff);
943 static void __init of_unittest_dma_ranges_one(const char *path,
944 u64 expect_dma_addr, u64 expect_paddr)
946 #ifdef CONFIG_HAS_DMA
947 struct device_node *np;
948 const struct bus_dma_region *map = NULL;
951 np = of_find_node_by_path(path);
953 pr_err("missing testcase data\n");
957 rc = of_dma_get_range(np, &map);
959 unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
964 struct device *dev_bogus;
966 dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
968 unittest(0, "kzalloc() failed\n");
973 dev_bogus->dma_range_map = map;
974 paddr = dma_to_phys(dev_bogus, expect_dma_addr);
975 dma_addr = phys_to_dma(dev_bogus, expect_paddr);
977 unittest(paddr == expect_paddr,
978 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
979 &paddr, expect_paddr, np);
980 unittest(dma_addr == expect_dma_addr,
981 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
982 &dma_addr, expect_dma_addr, np);
991 static void __init of_unittest_parse_dma_ranges(void)
993 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
995 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
996 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
997 0x100000000, 0x20000000);
998 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
999 0x80000000, 0x20000000);
1002 static void __init of_unittest_pci_dma_ranges(void)
1004 struct device_node *np;
1005 struct of_pci_range range;
1006 struct of_pci_range_parser parser;
1009 if (!IS_ENABLED(CONFIG_PCI))
1012 np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
1014 pr_err("missing testcase data\n");
1018 if (of_pci_dma_range_parser_init(&parser, np)) {
1019 pr_err("missing dma-ranges property\n");
1024 * Get the dma-ranges from the device tree
1026 for_each_of_pci_range(&parser, &range) {
1028 unittest(range.size == 0x10000000,
1029 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1031 unittest(range.cpu_addr == 0x20000000,
1032 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1033 range.cpu_addr, np);
1034 unittest(range.pci_addr == 0x80000000,
1035 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1036 range.pci_addr, np);
1038 unittest(range.size == 0x10000000,
1039 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1041 unittest(range.cpu_addr == 0x40000000,
1042 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1043 range.cpu_addr, np);
1044 unittest(range.pci_addr == 0xc0000000,
1045 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1046 range.pci_addr, np);
1054 static void __init of_unittest_bus_ranges(void)
1056 struct device_node *np;
1057 struct of_range range;
1058 struct of_range_parser parser;
1059 struct resource res;
1060 int ret, count, i = 0;
1062 np = of_find_node_by_path("/testcase-data/address-tests");
1064 pr_err("missing testcase data\n");
1068 if (of_range_parser_init(&parser, np)) {
1069 pr_err("missing ranges property\n");
1073 ret = of_range_to_resource(np, 1, &res);
1074 unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1076 unittest(resource_type(&res) == IORESOURCE_MEM,
1077 "of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1079 unittest(res.start == 0xd0000000,
1080 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1082 unittest(resource_size(&res) == 0x20000000,
1083 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1086 count = of_range_count(&parser);
1087 unittest(count == 2,
1088 "of_range_count wrong size on node %pOF count=%d\n",
1092 * Get the "ranges" from the device tree
1094 for_each_of_range(&parser, &range) {
1095 unittest(range.flags == IORESOURCE_MEM,
1096 "for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1097 np, range.flags, IORESOURCE_MEM);
1099 unittest(range.size == 0x50000000,
1100 "for_each_of_range wrong size on node %pOF size=%llx\n",
1102 unittest(range.cpu_addr == 0x70000000,
1103 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1104 range.cpu_addr, np);
1105 unittest(range.bus_addr == 0x70000000,
1106 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1107 range.pci_addr, np);
1109 unittest(range.size == 0x20000000,
1110 "for_each_of_range wrong size on node %pOF size=%llx\n",
1112 unittest(range.cpu_addr == 0xd0000000,
1113 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1114 range.cpu_addr, np);
1115 unittest(range.bus_addr == 0x00000000,
1116 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1117 range.pci_addr, np);
1125 static void __init of_unittest_bus_3cell_ranges(void)
1127 struct device_node *np;
1128 struct of_range range;
1129 struct of_range_parser parser;
1132 np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1134 pr_err("missing testcase data\n");
1138 if (of_range_parser_init(&parser, np)) {
1139 pr_err("missing ranges property\n");
1144 * Get the "ranges" from the device tree
1146 for_each_of_range(&parser, &range) {
1148 unittest(range.flags == 0xf00baa,
1149 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1151 unittest(range.size == 0x100000,
1152 "for_each_of_range wrong size on node %pOF size=%llx\n",
1154 unittest(range.cpu_addr == 0xa0000000,
1155 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1156 range.cpu_addr, np);
1157 unittest(range.bus_addr == 0x0,
1158 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1159 range.pci_addr, np);
1161 unittest(range.flags == 0xf00bee,
1162 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1164 unittest(range.size == 0x200000,
1165 "for_each_of_range wrong size on node %pOF size=%llx\n",
1167 unittest(range.cpu_addr == 0xb0000000,
1168 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1169 range.cpu_addr, np);
1170 unittest(range.bus_addr == 0x100000000,
1171 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1172 range.pci_addr, np);
1180 static void __init of_unittest_reg(void)
1182 struct device_node *np;
1186 np = of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
1188 pr_err("missing testcase data\n");
1192 ret = of_property_read_reg(np, 0, &addr, &size);
1193 unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
1195 unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
1201 static void __init of_unittest_parse_interrupts(void)
1203 struct device_node *np;
1204 struct of_phandle_args args;
1207 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1210 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1212 pr_err("missing testcase data\n");
1216 for (i = 0; i < 4; i++) {
1219 memset(&args, 0, sizeof(args));
1220 rc = of_irq_parse_one(np, i, &args);
1223 passed &= (args.args_count == 1);
1224 passed &= (args.args[0] == (i + 1));
1226 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1231 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1233 pr_err("missing testcase data\n");
1237 for (i = 0; i < 4; i++) {
1240 memset(&args, 0, sizeof(args));
1241 rc = of_irq_parse_one(np, i, &args);
1243 /* Test the values from tests-phandle.dtsi */
1247 passed &= (args.args_count == 1);
1248 passed &= (args.args[0] == 9);
1252 passed &= (args.args_count == 3);
1253 passed &= (args.args[0] == 10);
1254 passed &= (args.args[1] == 11);
1255 passed &= (args.args[2] == 12);
1259 passed &= (args.args_count == 2);
1260 passed &= (args.args[0] == 13);
1261 passed &= (args.args[1] == 14);
1265 passed &= (args.args_count == 2);
1266 passed &= (args.args[0] == 15);
1267 passed &= (args.args[1] == 16);
1272 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1278 static void __init of_unittest_parse_interrupts_extended(void)
1280 struct device_node *np;
1281 struct of_phandle_args args;
1284 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1287 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1289 pr_err("missing testcase data\n");
1293 for (i = 0; i < 7; i++) {
1296 memset(&args, 0, sizeof(args));
1297 rc = of_irq_parse_one(np, i, &args);
1299 /* Test the values from tests-phandle.dtsi */
1303 passed &= (args.args_count == 1);
1304 passed &= (args.args[0] == 1);
1308 passed &= (args.args_count == 3);
1309 passed &= (args.args[0] == 2);
1310 passed &= (args.args[1] == 3);
1311 passed &= (args.args[2] == 4);
1315 passed &= (args.args_count == 2);
1316 passed &= (args.args[0] == 5);
1317 passed &= (args.args[1] == 6);
1321 passed &= (args.args_count == 1);
1322 passed &= (args.args[0] == 9);
1326 passed &= (args.args_count == 3);
1327 passed &= (args.args[0] == 10);
1328 passed &= (args.args[1] == 11);
1329 passed &= (args.args[2] == 12);
1333 passed &= (args.args_count == 2);
1334 passed &= (args.args[0] == 13);
1335 passed &= (args.args[1] == 14);
1339 * Tests child node that is missing property
1340 * #address-cells. See the comments in
1341 * drivers/of/unittest-data/tests-interrupts.dtsi
1342 * nodes intmap1 and interrupts-extended0
1345 passed &= (args.args_count == 1);
1346 passed &= (args.args[0] == 15);
1352 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1358 static const struct of_device_id match_node_table[] = {
1359 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1360 { .data = "B", .type = "type1", }, /* followed by type alone */
1362 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1363 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1364 { .data = "Cc", .name = "name2", .type = "type2", },
1366 { .data = "E", .compatible = "compat3" },
1367 { .data = "G", .compatible = "compat2", },
1368 { .data = "H", .compatible = "compat2", .name = "name5", },
1369 { .data = "I", .compatible = "compat2", .type = "type1", },
1370 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1371 { .data = "K", .compatible = "compat2", .name = "name9", },
1378 } match_node_tests[] = {
1379 { .path = "/testcase-data/match-node/name0", .data = "A", },
1380 { .path = "/testcase-data/match-node/name1", .data = "B", },
1381 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1382 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1383 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1384 { .path = "/testcase-data/match-node/name3", .data = "E", },
1385 { .path = "/testcase-data/match-node/name4", .data = "G", },
1386 { .path = "/testcase-data/match-node/name5", .data = "H", },
1387 { .path = "/testcase-data/match-node/name6", .data = "G", },
1388 { .path = "/testcase-data/match-node/name7", .data = "I", },
1389 { .path = "/testcase-data/match-node/name8", .data = "J", },
1390 { .path = "/testcase-data/match-node/name9", .data = "K", },
1393 static void __init of_unittest_match_node(void)
1395 struct device_node *np;
1396 const struct of_device_id *match;
1399 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1400 np = of_find_node_by_path(match_node_tests[i].path);
1402 unittest(0, "missing testcase node %s\n",
1403 match_node_tests[i].path);
1407 match = of_match_node(match_node_table, np);
1409 unittest(0, "%s didn't match anything\n",
1410 match_node_tests[i].path);
1414 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1415 unittest(0, "%s got wrong match. expected %s, got %s\n",
1416 match_node_tests[i].path, match_node_tests[i].data,
1417 (const char *)match->data);
1420 unittest(1, "passed");
1424 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1425 static const struct platform_device_info test_bus_info = {
1426 .name = "unittest-bus",
1428 static void __init of_unittest_platform_populate(void)
1431 struct device_node *np, *child, *grandchild;
1432 struct platform_device *pdev, *test_bus;
1433 const struct of_device_id match[] = {
1434 { .compatible = "test-device", },
1438 np = of_find_node_by_path("/testcase-data");
1439 of_platform_default_populate(np, NULL, NULL);
1441 /* Test that a missing irq domain returns -EPROBE_DEFER */
1442 np = of_find_node_by_path("/testcase-data/testcase-device1");
1443 pdev = of_find_device_by_node(np);
1444 unittest(pdev, "device 1 creation failed\n");
1446 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1447 irq = platform_get_irq(pdev, 0);
1448 unittest(irq == -EPROBE_DEFER,
1449 "device deferred probe failed - %d\n", irq);
1451 /* Test that a parsing failure does not return -EPROBE_DEFER */
1452 np = of_find_node_by_path("/testcase-data/testcase-device2");
1453 pdev = of_find_device_by_node(np);
1454 unittest(pdev, "device 2 creation failed\n");
1456 EXPECT_BEGIN(KERN_INFO,
1457 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1459 irq = platform_get_irq(pdev, 0);
1461 EXPECT_END(KERN_INFO,
1462 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1464 unittest(irq < 0 && irq != -EPROBE_DEFER,
1465 "device parsing error failed - %d\n", irq);
1468 np = of_find_node_by_path("/testcase-data/platform-tests");
1469 unittest(np, "No testcase data in device tree\n");
1473 test_bus = platform_device_register_full(&test_bus_info);
1474 rc = PTR_ERR_OR_ZERO(test_bus);
1475 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1480 test_bus->dev.of_node = np;
1483 * Add a dummy resource to the test bus node after it is
1484 * registered to catch problems with un-inserted resources. The
1485 * DT code doesn't insert the resources, and it has caused the
1486 * kernel to oops in the past. This makes sure the same bug
1487 * doesn't crop up again.
1489 platform_device_add_resources(test_bus, &test_bus_res, 1);
1491 of_platform_populate(np, match, NULL, &test_bus->dev);
1492 for_each_child_of_node(np, child) {
1493 for_each_child_of_node(child, grandchild) {
1494 pdev = of_find_device_by_node(grandchild);
1496 "Could not create device for node '%pOFn'\n",
1498 platform_device_put(pdev);
1502 of_platform_depopulate(&test_bus->dev);
1503 for_each_child_of_node(np, child) {
1504 for_each_child_of_node(child, grandchild)
1505 unittest(!of_find_device_by_node(grandchild),
1506 "device didn't get destroyed '%pOFn'\n",
1510 platform_device_unregister(test_bus);
1515 * update_node_properties - adds the properties
1516 * of np into dup node (present in live tree) and
1517 * updates parent of children of np to dup.
1519 * @np: node whose properties are being added to the live tree
1520 * @dup: node present in live tree to be updated
1522 static void update_node_properties(struct device_node *np,
1523 struct device_node *dup)
1525 struct property *prop;
1526 struct property *save_next;
1527 struct device_node *child;
1530 for_each_child_of_node(np, child)
1531 child->parent = dup;
1534 * "unittest internal error: unable to add testdata property"
1536 * If this message reports a property in node '/__symbols__' then
1537 * the respective unittest overlay contains a label that has the
1538 * same name as a label in the live devicetree. The label will
1539 * be in the live devicetree only if the devicetree source was
1540 * compiled with the '-@' option. If you encounter this error,
1541 * please consider renaming __all__ of the labels in the unittest
1542 * overlay dts files with an odd prefix that is unlikely to be
1543 * used in a real devicetree.
1547 * open code for_each_property_of_node() because of_add_property()
1548 * sets prop->next to NULL
1550 for (prop = np->properties; prop != NULL; prop = save_next) {
1551 save_next = prop->next;
1552 ret = of_add_property(dup, prop);
1554 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1556 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1563 * attach_node_and_children - attaches nodes
1564 * and its children to live tree.
1565 * CAUTION: misleading function name - if node @np already exists in
1566 * the live tree then children of @np are *not* attached to the live
1567 * tree. This works for the current test devicetree nodes because such
1568 * nodes do not have child nodes.
1570 * @np: Node to attach to live tree
1572 static void attach_node_and_children(struct device_node *np)
1574 struct device_node *next, *dup, *child;
1575 unsigned long flags;
1576 const char *full_name;
1578 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1582 if (!strcmp(full_name, "/__local_fixups__") ||
1583 !strcmp(full_name, "/__fixups__")) {
1588 dup = of_find_node_by_path(full_name);
1591 update_node_properties(np, dup);
1598 mutex_lock(&of_mutex);
1599 raw_spin_lock_irqsave(&devtree_lock, flags);
1600 np->sibling = np->parent->child;
1601 np->parent->child = np;
1602 of_node_clear_flag(np, OF_DETACHED);
1603 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1605 __of_attach_node_sysfs(np);
1606 mutex_unlock(&of_mutex);
1609 next = child->sibling;
1610 attach_node_and_children(child);
1616 * unittest_data_add - Reads, copies data from
1617 * linked tree and attaches it to the live tree
1619 static int __init unittest_data_add(void)
1621 void *unittest_data;
1622 void *unittest_data_align;
1623 struct device_node *unittest_data_node = NULL, *np;
1625 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1626 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
1628 extern uint8_t __dtbo_testcases_begin[];
1629 extern uint8_t __dtbo_testcases_end[];
1630 const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
1635 pr_warn("%s: testcases is empty\n", __func__);
1640 unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
1644 unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1645 memcpy(unittest_data_align, __dtbo_testcases_begin, size);
1647 ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
1649 pr_warn("%s: unflatten testcases tree failed\n", __func__);
1650 kfree(unittest_data);
1653 if (!unittest_data_node) {
1654 pr_warn("%s: testcases tree is empty\n", __func__);
1655 kfree(unittest_data);
1660 * This lock normally encloses of_resolve_phandles()
1662 of_overlay_mutex_lock();
1664 rc = of_resolve_phandles(unittest_data_node);
1666 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1667 of_overlay_mutex_unlock();
1672 of_root = unittest_data_node;
1673 for_each_of_allnodes(np)
1674 __of_attach_node_sysfs(np);
1675 of_aliases = of_find_node_by_path("/aliases");
1676 of_chosen = of_find_node_by_path("/chosen");
1677 of_overlay_mutex_unlock();
1681 EXPECT_BEGIN(KERN_INFO,
1682 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1684 /* attach the sub-tree to live tree */
1685 np = unittest_data_node->child;
1687 struct device_node *next = np->sibling;
1689 np->parent = of_root;
1690 /* this will clear OF_DETACHED in np and children */
1691 attach_node_and_children(np);
1695 EXPECT_END(KERN_INFO,
1696 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1698 of_overlay_mutex_unlock();
1703 #ifdef CONFIG_OF_OVERLAY
1704 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
1706 static int unittest_probe(struct platform_device *pdev)
1708 struct device *dev = &pdev->dev;
1709 struct device_node *np = dev->of_node;
1712 dev_err(dev, "No OF data for device\n");
1717 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1719 of_platform_populate(np, NULL, NULL, &pdev->dev);
1724 static void unittest_remove(struct platform_device *pdev)
1726 struct device *dev = &pdev->dev;
1727 struct device_node *np = dev->of_node;
1729 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1732 static const struct of_device_id unittest_match[] = {
1733 { .compatible = "unittest", },
1737 static struct platform_driver unittest_driver = {
1738 .probe = unittest_probe,
1739 .remove_new = unittest_remove,
1742 .of_match_table = unittest_match,
1746 /* get the platform device instantiated at the path */
1747 static struct platform_device *of_path_to_platform_device(const char *path)
1749 struct device_node *np;
1750 struct platform_device *pdev;
1752 np = of_find_node_by_path(path);
1756 pdev = of_find_device_by_node(np);
1762 /* find out if a platform device exists at that path */
1763 static int of_path_platform_device_exists(const char *path)
1765 struct platform_device *pdev;
1767 pdev = of_path_to_platform_device(path);
1768 platform_device_put(pdev);
1769 return pdev != NULL;
1772 #ifdef CONFIG_OF_GPIO
1774 struct unittest_gpio_dev {
1775 struct gpio_chip chip;
1778 static int unittest_gpio_chip_request_count;
1779 static int unittest_gpio_probe_count;
1780 static int unittest_gpio_probe_pass_count;
1782 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1784 unittest_gpio_chip_request_count++;
1786 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1787 unittest_gpio_chip_request_count);
1791 static int unittest_gpio_probe(struct platform_device *pdev)
1793 struct unittest_gpio_dev *devptr;
1796 unittest_gpio_probe_count++;
1798 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1802 platform_set_drvdata(pdev, devptr);
1804 devptr->chip.fwnode = dev_fwnode(&pdev->dev);
1805 devptr->chip.label = "of-unittest-gpio";
1806 devptr->chip.base = -1; /* dynamic allocation */
1807 devptr->chip.ngpio = 5;
1808 devptr->chip.request = unittest_gpio_chip_request;
1810 ret = gpiochip_add_data(&devptr->chip, NULL);
1813 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
1816 unittest_gpio_probe_pass_count++;
1820 static void unittest_gpio_remove(struct platform_device *pdev)
1822 struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
1823 struct device *dev = &pdev->dev;
1825 dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
1827 if (devptr->chip.base != -1)
1828 gpiochip_remove(&devptr->chip);
1833 static const struct of_device_id unittest_gpio_id[] = {
1834 { .compatible = "unittest-gpio", },
1838 static struct platform_driver unittest_gpio_driver = {
1839 .probe = unittest_gpio_probe,
1840 .remove_new = unittest_gpio_remove,
1842 .name = "unittest-gpio",
1843 .of_match_table = unittest_gpio_id,
1847 static void __init of_unittest_overlay_gpio(void)
1849 int chip_request_count;
1850 int probe_pass_count;
1854 * tests: apply overlays before registering driver
1855 * Similar to installing a driver as a module, the
1856 * driver is registered after applying the overlays.
1858 * The overlays are applied by overlay_data_apply()
1859 * instead of of_unittest_apply_overlay() so that they
1860 * will not be tracked. Thus they will not be removed
1861 * by of_unittest_remove_tracked_overlays().
1863 * - apply overlay_gpio_01
1864 * - apply overlay_gpio_02a
1865 * - apply overlay_gpio_02b
1868 * register driver will result in
1869 * - probe and processing gpio hog for overlay_gpio_01
1870 * - probe for overlay_gpio_02a
1871 * - processing gpio for overlay_gpio_02b
1874 probe_pass_count = unittest_gpio_probe_pass_count;
1875 chip_request_count = unittest_gpio_chip_request_count;
1878 * overlay_gpio_01 contains gpio node and child gpio hog node
1879 * overlay_gpio_02a contains gpio node
1880 * overlay_gpio_02b contains child gpio hog node
1883 unittest(overlay_data_apply("overlay_gpio_01", NULL),
1884 "Adding overlay 'overlay_gpio_01' failed\n");
1886 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1887 "Adding overlay 'overlay_gpio_02a' failed\n");
1889 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1890 "Adding overlay 'overlay_gpio_02b' failed\n");
1892 ret = platform_driver_register(&unittest_gpio_driver);
1893 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1896 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1897 "unittest_gpio_probe() failed or not called\n");
1899 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1900 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1901 unittest_gpio_chip_request_count - chip_request_count);
1904 * tests: apply overlays after registering driver
1906 * Similar to a driver built-in to the kernel, the
1907 * driver is registered before applying the overlays.
1909 * overlay_gpio_03 contains gpio node and child gpio hog node
1911 * - apply overlay_gpio_03
1913 * apply overlay will result in
1914 * - probe and processing gpio hog.
1917 probe_pass_count = unittest_gpio_probe_pass_count;
1918 chip_request_count = unittest_gpio_chip_request_count;
1920 /* overlay_gpio_03 contains gpio node and child gpio hog node */
1922 unittest(overlay_data_apply("overlay_gpio_03", NULL),
1923 "Adding overlay 'overlay_gpio_03' failed\n");
1925 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1926 "unittest_gpio_probe() failed or not called\n");
1928 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1929 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1930 unittest_gpio_chip_request_count - chip_request_count);
1933 * overlay_gpio_04a contains gpio node
1935 * - apply overlay_gpio_04a
1937 * apply the overlay will result in
1938 * - probe for overlay_gpio_04a
1941 probe_pass_count = unittest_gpio_probe_pass_count;
1942 chip_request_count = unittest_gpio_chip_request_count;
1944 /* overlay_gpio_04a contains gpio node */
1946 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
1947 "Adding overlay 'overlay_gpio_04a' failed\n");
1949 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1950 "unittest_gpio_probe() failed or not called\n");
1953 * overlay_gpio_04b contains child gpio hog node
1955 * - apply overlay_gpio_04b
1957 * apply the overlay will result in
1958 * - processing gpio for overlay_gpio_04b
1961 /* overlay_gpio_04b contains child gpio hog node */
1963 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
1964 "Adding overlay 'overlay_gpio_04b' failed\n");
1966 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1967 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1968 unittest_gpio_chip_request_count - chip_request_count);
1973 static void __init of_unittest_overlay_gpio(void)
1980 #if IS_BUILTIN(CONFIG_I2C)
1982 /* get the i2c client device instantiated at the path */
1983 static struct i2c_client *of_path_to_i2c_client(const char *path)
1985 struct device_node *np;
1986 struct i2c_client *client;
1988 np = of_find_node_by_path(path);
1992 client = of_find_i2c_device_by_node(np);
1998 /* find out if a i2c client device exists at that path */
1999 static int of_path_i2c_client_exists(const char *path)
2001 struct i2c_client *client;
2003 client = of_path_to_i2c_client(path);
2005 put_device(&client->dev);
2006 return client != NULL;
2009 static int of_path_i2c_client_exists(const char *path)
2020 static int of_path_device_type_exists(const char *path,
2021 enum overlay_type ovtype)
2025 return of_path_platform_device_exists(path);
2027 return of_path_i2c_client_exists(path);
2032 static const char *unittest_path(int nr, enum overlay_type ovtype)
2035 static char buf[256];
2039 base = "/testcase-data/overlay-node/test-bus";
2042 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2048 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2049 buf[sizeof(buf) - 1] = '\0';
2053 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2057 path = unittest_path(unittest_nr, ovtype);
2061 return of_path_platform_device_exists(path);
2063 return of_path_i2c_client_exists(path);
2068 static const char *overlay_name_from_nr(int nr)
2070 static char buf[256];
2072 snprintf(buf, sizeof(buf) - 1,
2074 buf[sizeof(buf) - 1] = '\0';
2079 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2081 #define MAX_TRACK_OVCS_IDS 256
2083 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2084 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2085 static int track_ovcs_id_cnt;
2087 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2089 if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2092 track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2093 track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2094 track_ovcs_id_cnt++;
2097 static void of_unittest_untrack_overlay(int ovcs_id)
2099 if (WARN_ON(track_ovcs_id_cnt < 1))
2102 track_ovcs_id_cnt--;
2104 /* If out of synch then test is broken. Do not try to recover. */
2105 WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2108 static void of_unittest_remove_tracked_overlays(void)
2110 int ret, ovcs_id, overlay_nr, save_ovcs_id;
2111 const char *overlay_name;
2113 while (track_ovcs_id_cnt > 0) {
2115 ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2116 overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2117 save_ovcs_id = ovcs_id;
2118 ret = of_overlay_remove(&ovcs_id);
2119 if (ret == -ENODEV) {
2120 overlay_name = overlay_name_from_nr(overlay_nr);
2121 pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2122 __func__, overlay_name, ret);
2124 of_unittest_untrack_overlay(save_ovcs_id);
2129 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2132 * The overlay will be tracked, thus it will be removed
2133 * by of_unittest_remove_tracked_overlays().
2136 const char *overlay_name;
2138 overlay_name = overlay_name_from_nr(overlay_nr);
2140 if (!overlay_data_apply(overlay_name, ovcs_id)) {
2141 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2144 of_unittest_track_overlay(*ovcs_id, overlay_nr);
2149 static int __init __of_unittest_apply_overlay_check(int overlay_nr,
2150 int unittest_nr, int before, int after,
2151 enum overlay_type ovtype)
2155 /* unittest device must be in before state */
2156 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2157 unittest(0, "%s with device @\"%s\" %s\n",
2158 overlay_name_from_nr(overlay_nr),
2159 unittest_path(unittest_nr, ovtype),
2160 !before ? "enabled" : "disabled");
2164 /* apply the overlay */
2166 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2168 /* of_unittest_apply_overlay already called unittest() */
2172 /* unittest device must be in after state */
2173 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2174 unittest(0, "%s with device @\"%s\" %s\n",
2175 overlay_name_from_nr(overlay_nr),
2176 unittest_path(unittest_nr, ovtype),
2177 !after ? "enabled" : "disabled");
2184 /* apply an overlay while checking before and after states */
2185 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2186 int unittest_nr, int before, int after,
2187 enum overlay_type ovtype)
2189 int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
2190 unittest_nr, before, after, ovtype);
2197 /* apply an overlay and then revert it while checking before, after states */
2198 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2199 int unittest_nr, int before, int after,
2200 enum overlay_type ovtype)
2202 int ret, ovcs_id, save_ovcs_id;
2204 ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
2205 before, after, ovtype);
2209 /* remove the overlay */
2210 save_ovcs_id = ovcs_id;
2211 ret = of_overlay_remove(&ovcs_id);
2213 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2214 overlay_name_from_nr(overlay_nr),
2215 unittest_path(unittest_nr, ovtype));
2218 of_unittest_untrack_overlay(save_ovcs_id);
2220 /* unittest device must be again in before state */
2221 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2222 unittest(0, "%s with device @\"%s\" %s\n",
2223 overlay_name_from_nr(overlay_nr),
2224 unittest_path(unittest_nr, ovtype),
2225 !before ? "enabled" : "disabled");
2232 /* test activation of device */
2233 static void __init of_unittest_overlay_0(void)
2237 EXPECT_BEGIN(KERN_INFO,
2238 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2240 /* device should enable */
2241 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2243 EXPECT_END(KERN_INFO,
2244 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2249 unittest(1, "overlay test %d passed\n", 0);
2252 /* test deactivation of device */
2253 static void __init of_unittest_overlay_1(void)
2257 EXPECT_BEGIN(KERN_INFO,
2258 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2260 /* device should disable */
2261 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2263 EXPECT_END(KERN_INFO,
2264 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2269 unittest(1, "overlay test %d passed\n", 1);
2273 /* test activation of device */
2274 static void __init of_unittest_overlay_2(void)
2278 EXPECT_BEGIN(KERN_INFO,
2279 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2281 /* device should enable */
2282 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2284 EXPECT_END(KERN_INFO,
2285 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2289 unittest(1, "overlay test %d passed\n", 2);
2292 /* test deactivation of device */
2293 static void __init of_unittest_overlay_3(void)
2297 EXPECT_BEGIN(KERN_INFO,
2298 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2300 /* device should disable */
2301 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2303 EXPECT_END(KERN_INFO,
2304 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2309 unittest(1, "overlay test %d passed\n", 3);
2312 /* test activation of a full device node */
2313 static void __init of_unittest_overlay_4(void)
2315 /* device should disable */
2316 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2319 unittest(1, "overlay test %d passed\n", 4);
2322 /* test overlay apply/revert sequence */
2323 static void __init of_unittest_overlay_5(void)
2327 EXPECT_BEGIN(KERN_INFO,
2328 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2330 /* device should disable */
2331 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2333 EXPECT_END(KERN_INFO,
2334 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2339 unittest(1, "overlay test %d passed\n", 5);
2342 /* test overlay application in sequence */
2343 static void __init of_unittest_overlay_6(void)
2345 int i, save_ovcs_id[2], ovcs_id;
2346 int overlay_nr = 6, unittest_nr = 6;
2347 int before = 0, after = 1;
2348 const char *overlay_name;
2352 /* unittest device must be in before state */
2353 for (i = 0; i < 2; i++) {
2354 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2356 unittest(0, "%s with device @\"%s\" %s\n",
2357 overlay_name_from_nr(overlay_nr + i),
2358 unittest_path(unittest_nr + i,
2360 !before ? "enabled" : "disabled");
2365 /* apply the overlays */
2367 EXPECT_BEGIN(KERN_INFO,
2368 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2370 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2372 ret = overlay_data_apply(overlay_name, &ovcs_id);
2375 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2378 save_ovcs_id[0] = ovcs_id;
2379 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2381 EXPECT_END(KERN_INFO,
2382 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2384 EXPECT_BEGIN(KERN_INFO,
2385 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2387 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2389 ret = overlay_data_apply(overlay_name, &ovcs_id);
2392 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2395 save_ovcs_id[1] = ovcs_id;
2396 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2398 EXPECT_END(KERN_INFO,
2399 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2402 for (i = 0; i < 2; i++) {
2403 /* unittest device must be in after state */
2404 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2406 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2407 overlay_name_from_nr(overlay_nr + i),
2408 unittest_path(unittest_nr + i,
2410 !after ? "enabled" : "disabled");
2415 for (i = 1; i >= 0; i--) {
2416 ovcs_id = save_ovcs_id[i];
2417 if (of_overlay_remove(&ovcs_id)) {
2418 unittest(0, "%s failed destroy @\"%s\"\n",
2419 overlay_name_from_nr(overlay_nr + i),
2420 unittest_path(unittest_nr + i,
2424 of_unittest_untrack_overlay(save_ovcs_id[i]);
2427 for (i = 0; i < 2; i++) {
2428 /* unittest device must be again in before state */
2429 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2431 unittest(0, "%s with device @\"%s\" %s\n",
2432 overlay_name_from_nr(overlay_nr + i),
2433 unittest_path(unittest_nr + i,
2435 !before ? "enabled" : "disabled");
2440 unittest(1, "overlay test %d passed\n", 6);
2444 /* test overlay application in sequence */
2445 static void __init of_unittest_overlay_8(void)
2447 int i, save_ovcs_id[2], ovcs_id;
2448 int overlay_nr = 8, unittest_nr = 8;
2449 const char *overlay_name;
2452 /* we don't care about device state in this test */
2454 EXPECT_BEGIN(KERN_INFO,
2455 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2457 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2459 ret = overlay_data_apply(overlay_name, &ovcs_id);
2461 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2463 EXPECT_END(KERN_INFO,
2464 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2469 save_ovcs_id[0] = ovcs_id;
2470 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2472 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2474 EXPECT_BEGIN(KERN_INFO,
2475 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2477 /* apply the overlays */
2478 ret = overlay_data_apply(overlay_name, &ovcs_id);
2480 EXPECT_END(KERN_INFO,
2481 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2484 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2488 save_ovcs_id[1] = ovcs_id;
2489 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2491 /* now try to remove first overlay (it should fail) */
2492 ovcs_id = save_ovcs_id[0];
2494 EXPECT_BEGIN(KERN_INFO,
2495 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2497 EXPECT_BEGIN(KERN_INFO,
2498 "OF: overlay: overlay #6 is not topmost");
2500 ret = of_overlay_remove(&ovcs_id);
2502 EXPECT_END(KERN_INFO,
2503 "OF: overlay: overlay #6 is not topmost");
2505 EXPECT_END(KERN_INFO,
2506 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2510 * Should never get here. If we do, expect a lot of
2511 * subsequent tracking and overlay removal related errors.
2513 unittest(0, "%s was destroyed @\"%s\"\n",
2514 overlay_name_from_nr(overlay_nr + 0),
2515 unittest_path(unittest_nr,
2520 /* removing them in order should work */
2521 for (i = 1; i >= 0; i--) {
2522 ovcs_id = save_ovcs_id[i];
2523 if (of_overlay_remove(&ovcs_id)) {
2524 unittest(0, "%s not destroyed @\"%s\"\n",
2525 overlay_name_from_nr(overlay_nr + i),
2526 unittest_path(unittest_nr,
2530 of_unittest_untrack_overlay(save_ovcs_id[i]);
2533 unittest(1, "overlay test %d passed\n", 8);
2536 /* test insertion of a bus with parent devices */
2537 static void __init of_unittest_overlay_10(void)
2542 /* device should disable */
2543 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2545 if (unittest(ret == 0,
2546 "overlay test %d failed; overlay application\n", 10))
2549 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2550 unittest_path(10, PDEV_OVERLAY));
2551 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2554 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2557 unittest(ret, "overlay test %d failed; no child device\n", 10);
2560 /* test insertion of a bus with parent devices (and revert) */
2561 static void __init of_unittest_overlay_11(void)
2565 /* device should disable */
2566 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2569 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2572 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2574 struct unittest_i2c_bus_data {
2575 struct platform_device *pdev;
2576 struct i2c_adapter adap;
2579 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2580 struct i2c_msg *msgs, int num)
2582 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2589 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2591 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2594 static const struct i2c_algorithm unittest_i2c_algo = {
2595 .master_xfer = unittest_i2c_master_xfer,
2596 .functionality = unittest_i2c_functionality,
2599 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2601 struct device *dev = &pdev->dev;
2602 struct device_node *np = dev->of_node;
2603 struct unittest_i2c_bus_data *std;
2604 struct i2c_adapter *adap;
2608 dev_err(dev, "No OF data for device\n");
2613 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2615 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2619 /* link them together */
2621 platform_set_drvdata(pdev, std);
2624 i2c_set_adapdata(adap, std);
2626 strscpy(adap->name, pdev->name, sizeof(adap->name));
2627 adap->class = I2C_CLASS_DEPRECATED;
2628 adap->algo = &unittest_i2c_algo;
2629 adap->dev.parent = dev;
2630 adap->dev.of_node = dev->of_node;
2631 adap->timeout = 5 * HZ;
2634 ret = i2c_add_numbered_adapter(adap);
2636 dev_err(dev, "Failed to add I2C adapter\n");
2643 static void unittest_i2c_bus_remove(struct platform_device *pdev)
2645 struct device *dev = &pdev->dev;
2646 struct device_node *np = dev->of_node;
2647 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2649 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2650 i2c_del_adapter(&std->adap);
2653 static const struct of_device_id unittest_i2c_bus_match[] = {
2654 { .compatible = "unittest-i2c-bus", },
2658 static struct platform_driver unittest_i2c_bus_driver = {
2659 .probe = unittest_i2c_bus_probe,
2660 .remove_new = unittest_i2c_bus_remove,
2662 .name = "unittest-i2c-bus",
2663 .of_match_table = unittest_i2c_bus_match,
2667 static int unittest_i2c_dev_probe(struct i2c_client *client)
2669 struct device *dev = &client->dev;
2670 struct device_node *np = client->dev.of_node;
2673 dev_err(dev, "No OF node\n");
2677 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2682 static void unittest_i2c_dev_remove(struct i2c_client *client)
2684 struct device *dev = &client->dev;
2685 struct device_node *np = client->dev.of_node;
2687 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2690 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2691 { .name = "unittest-i2c-dev" },
2695 static struct i2c_driver unittest_i2c_dev_driver = {
2697 .name = "unittest-i2c-dev",
2699 .probe = unittest_i2c_dev_probe,
2700 .remove = unittest_i2c_dev_remove,
2701 .id_table = unittest_i2c_dev_id,
2704 #if IS_BUILTIN(CONFIG_I2C_MUX)
2706 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2711 static int unittest_i2c_mux_probe(struct i2c_client *client)
2714 struct device *dev = &client->dev;
2715 struct i2c_adapter *adap = client->adapter;
2716 struct device_node *np = client->dev.of_node, *child;
2717 struct i2c_mux_core *muxc;
2720 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2723 dev_err(dev, "No OF node\n");
2728 for_each_child_of_node(np, child) {
2729 if (of_property_read_u32(child, "reg", ®))
2731 if (max_reg == (u32)-1 || reg > max_reg)
2734 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2736 dev_err(dev, "No channels\n");
2740 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2741 unittest_i2c_mux_select_chan, NULL);
2744 for (i = 0; i < nchans; i++) {
2745 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2746 dev_err(dev, "Failed to register mux #%d\n", i);
2747 i2c_mux_del_adapters(muxc);
2752 i2c_set_clientdata(client, muxc);
2757 static void unittest_i2c_mux_remove(struct i2c_client *client)
2759 struct device *dev = &client->dev;
2760 struct device_node *np = client->dev.of_node;
2761 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2763 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2764 i2c_mux_del_adapters(muxc);
2767 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2768 { .name = "unittest-i2c-mux" },
2772 static struct i2c_driver unittest_i2c_mux_driver = {
2774 .name = "unittest-i2c-mux",
2776 .probe = unittest_i2c_mux_probe,
2777 .remove = unittest_i2c_mux_remove,
2778 .id_table = unittest_i2c_mux_id,
2783 static int of_unittest_overlay_i2c_init(void)
2787 ret = i2c_add_driver(&unittest_i2c_dev_driver);
2788 if (unittest(ret == 0,
2789 "could not register unittest i2c device driver\n"))
2792 ret = platform_driver_register(&unittest_i2c_bus_driver);
2794 if (unittest(ret == 0,
2795 "could not register unittest i2c bus driver\n"))
2798 #if IS_BUILTIN(CONFIG_I2C_MUX)
2800 EXPECT_BEGIN(KERN_INFO,
2801 "i2c i2c-1: Added multiplexed i2c bus 2");
2803 ret = i2c_add_driver(&unittest_i2c_mux_driver);
2805 EXPECT_END(KERN_INFO,
2806 "i2c i2c-1: Added multiplexed i2c bus 2");
2808 if (unittest(ret == 0,
2809 "could not register unittest i2c mux driver\n"))
2816 static void of_unittest_overlay_i2c_cleanup(void)
2818 #if IS_BUILTIN(CONFIG_I2C_MUX)
2819 i2c_del_driver(&unittest_i2c_mux_driver);
2821 platform_driver_unregister(&unittest_i2c_bus_driver);
2822 i2c_del_driver(&unittest_i2c_dev_driver);
2825 static void __init of_unittest_overlay_i2c_12(void)
2829 /* device should enable */
2830 EXPECT_BEGIN(KERN_INFO,
2831 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2833 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2835 EXPECT_END(KERN_INFO,
2836 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2841 unittest(1, "overlay test %d passed\n", 12);
2844 /* test deactivation of device */
2845 static void __init of_unittest_overlay_i2c_13(void)
2849 EXPECT_BEGIN(KERN_INFO,
2850 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2852 /* device should disable */
2853 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2855 EXPECT_END(KERN_INFO,
2856 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2861 unittest(1, "overlay test %d passed\n", 13);
2864 /* just check for i2c mux existence */
2865 static void of_unittest_overlay_i2c_14(void)
2869 static void __init of_unittest_overlay_i2c_15(void)
2873 /* device should enable */
2874 EXPECT_BEGIN(KERN_INFO,
2875 "i2c i2c-1: Added multiplexed i2c bus 3");
2877 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2879 EXPECT_END(KERN_INFO,
2880 "i2c i2c-1: Added multiplexed i2c bus 3");
2885 unittest(1, "overlay test %d passed\n", 15);
2890 static inline void of_unittest_overlay_i2c_14(void) { }
2891 static inline void of_unittest_overlay_i2c_15(void) { }
2895 static int of_notify(struct notifier_block *nb, unsigned long action,
2898 struct of_overlay_notify_data *nd = arg;
2899 struct device_node *found;
2903 * For overlay_16 .. overlay_19, check that returning an error
2904 * works for each of the actions by setting an arbitrary return
2905 * error number that matches the test number. e.g. for unittest16,
2906 * ret = -EBUSY which is -16.
2908 * OVERLAY_INFO() for the overlays is declared to expect the same
2909 * error number, so overlay_data_apply() will return no error.
2911 * overlay_20 will return NOTIFY_DONE
2915 of_node_get(nd->overlay);
2919 case OF_OVERLAY_PRE_APPLY:
2920 found = of_find_node_by_name(nd->overlay, "test-unittest16");
2927 case OF_OVERLAY_POST_APPLY:
2928 found = of_find_node_by_name(nd->overlay, "test-unittest17");
2935 case OF_OVERLAY_PRE_REMOVE:
2936 found = of_find_node_by_name(nd->overlay, "test-unittest18");
2943 case OF_OVERLAY_POST_REMOVE:
2944 found = of_find_node_by_name(nd->overlay, "test-unittest19");
2951 default: /* should not happen */
2952 of_node_put(nd->overlay);
2958 return notifier_from_errno(ret);
2963 static struct notifier_block of_nb = {
2964 .notifier_call = of_notify,
2967 static void __init of_unittest_overlay_notify(void)
2972 ret = of_overlay_notifier_register(&of_nb);
2974 "of_overlay_notifier_register() failed, ret = %d\n", ret);
2979 * The overlays are applied by overlay_data_apply()
2980 * instead of of_unittest_apply_overlay() so that they
2981 * will not be tracked. Thus they will not be removed
2982 * by of_unittest_remove_tracked_overlays().
2984 * Applying overlays 16 - 19 will each trigger an error for a
2985 * different action in of_notify().
2987 * Applying overlay 20 will not trigger any error in of_notify().
2990 /* --- overlay 16 --- */
2992 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
2994 unittest(overlay_data_apply("overlay_16", &ovcs_id),
2995 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
2997 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
2999 unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
3001 /* --- overlay 17 --- */
3003 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3005 unittest(overlay_data_apply("overlay_17", &ovcs_id),
3006 "test OF_OVERLAY_POST_APPLY notify injected error\n");
3008 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3010 unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
3012 /* --- overlay 18 --- */
3014 unittest(overlay_data_apply("overlay_18", &ovcs_id),
3015 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3017 unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
3020 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3022 ret = of_overlay_remove(&ovcs_id);
3023 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3024 if (ret == -EXDEV) {
3026 * change set ovcs_id should still exist
3028 unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3030 unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3033 unittest(1, "ovcs_id not created for overlay_18\n");
3036 unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3038 /* --- overlay 19 --- */
3040 unittest(overlay_data_apply("overlay_19", &ovcs_id),
3041 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3043 unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3046 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3047 ret = of_overlay_remove(&ovcs_id);
3048 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3050 unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3052 unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3054 unittest(1, "ovcs_id removed for overlay_19\n");
3057 unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3060 /* --- overlay 20 --- */
3062 unittest(overlay_data_apply("overlay_20", &ovcs_id),
3063 "overlay notify no injected error\n");
3066 ret = of_overlay_remove(&ovcs_id);
3068 unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3071 unittest(1, "ovcs_id not created for overlay_20\n");
3074 unittest(!of_overlay_notifier_unregister(&of_nb),
3075 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3078 static void __init of_unittest_overlay(void)
3080 struct device_node *bus_np = NULL;
3083 if (platform_driver_register(&unittest_driver)) {
3084 unittest(0, "could not register unittest driver\n");
3088 bus_np = of_find_node_by_path(bus_path);
3089 if (bus_np == NULL) {
3090 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3094 if (of_platform_default_populate(bus_np, NULL, NULL)) {
3095 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3099 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3100 unittest(0, "could not find unittest0 @ \"%s\"\n",
3101 unittest_path(100, PDEV_OVERLAY));
3105 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3106 unittest(0, "unittest1 @ \"%s\" should not exist\n",
3107 unittest_path(101, PDEV_OVERLAY));
3111 unittest(1, "basic infrastructure of overlays passed");
3113 /* tests in sequence */
3114 of_unittest_overlay_0();
3115 of_unittest_overlay_1();
3116 of_unittest_overlay_2();
3117 of_unittest_overlay_3();
3118 of_unittest_overlay_4();
3119 for (i = 0; i < 3; i++)
3120 of_unittest_overlay_5();
3121 of_unittest_overlay_6();
3122 of_unittest_overlay_8();
3124 of_unittest_overlay_10();
3125 of_unittest_overlay_11();
3127 #if IS_BUILTIN(CONFIG_I2C)
3128 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3131 of_unittest_overlay_i2c_12();
3132 of_unittest_overlay_i2c_13();
3133 of_unittest_overlay_i2c_14();
3134 of_unittest_overlay_i2c_15();
3136 of_unittest_overlay_i2c_cleanup();
3139 of_unittest_overlay_gpio();
3141 of_unittest_remove_tracked_overlays();
3143 of_unittest_overlay_notify();
3146 of_node_put(bus_np);
3150 static inline void __init of_unittest_overlay(void) { }
3153 static void __init of_unittest_lifecycle(void)
3155 #ifdef CONFIG_OF_DYNAMIC
3156 unsigned int refcount;
3157 int found_refcount_one = 0;
3159 struct device_node *np;
3160 struct device_node *prev_sibling, *next_sibling;
3161 const char *refcount_path = "/testcase-data/refcount-node";
3162 const char *refcount_parent_path = "/testcase-data";
3165 * Node lifecycle tests, non-dynamic node:
3167 * - Decrementing refcount to zero via of_node_put() should cause the
3168 * attempt to free the node memory by of_node_release() to fail
3169 * because the node is not a dynamic node.
3171 * - Decrementing refcount past zero should result in additional
3175 np = of_find_node_by_path(refcount_path);
3176 unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3178 goto out_skip_tests;
3180 while (!found_refcount_one) {
3182 if (put_count++ > 10) {
3183 unittest(0, "guardrail to avoid infinite loop\n");
3184 goto out_skip_tests;
3187 refcount = kref_read(&np->kobj.kref);
3189 found_refcount_one = 1;
3194 EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3197 * refcount is now one, decrementing to zero will result in a call to
3198 * of_node_release() to free the node's memory, which should result
3201 unittest(1, "/testcase-data/refcount-node is one");
3204 EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3208 * expect stack trace for subsequent of_node_put():
3209 * __refcount_sub_and_test() calls:
3210 * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3212 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3213 * the first three lines, and the last line.
3215 EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3216 EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3217 EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3218 EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3220 /* refcount is now zero, this should fail */
3221 unittest(1, "/testcase-data/refcount-node is zero");
3224 EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3225 EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3226 EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3227 EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3230 * Q. do we expect to get yet another warning?
3231 * A. no, the WARNING is from WARN_ONCE()
3233 EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3234 EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3235 EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3236 EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3238 unittest(1, "/testcase-data/refcount-node is zero, second time");
3241 EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3242 EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3243 EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3244 EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3247 * refcount of zero will trigger stack traces from any further
3248 * attempt to of_node_get() node "refcount-node". One example of
3249 * this is where of_unittest_check_node_linkage() will recursively
3250 * scan the tree, with 'for_each_child_of_node()' doing an
3251 * of_node_get() of the children of a node.
3253 * Prevent the stack trace by removing node "refcount-node" from
3254 * its parent's child list.
3256 * WARNING: EVIL, EVIL, EVIL:
3258 * Directly manipulate the child list of node /testcase-data to
3259 * remove child refcount-node. This is ignoring all proper methods
3260 * of removing a child and will leak a small amount of memory.
3263 np = of_find_node_by_path(refcount_parent_path);
3264 unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3265 unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3269 prev_sibling = np->child;
3270 next_sibling = prev_sibling->sibling;
3271 if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3272 np->child = next_sibling;
3273 next_sibling = next_sibling->sibling;
3275 while (next_sibling) {
3276 if (!strcmp(next_sibling->full_name, "refcount-node"))
3277 prev_sibling->sibling = next_sibling->sibling;
3278 prev_sibling = next_sibling;
3279 next_sibling = next_sibling->sibling;
3287 unittest(0, "One or more lifecycle tests skipped\n");
3290 #ifdef CONFIG_OF_OVERLAY
3293 * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3294 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
3297 #define OVERLAY_INFO_EXTERN(overlay_name) \
3298 extern uint8_t __dtbo_##overlay_name##_begin[]; \
3299 extern uint8_t __dtbo_##overlay_name##_end[]
3301 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3302 { .dtbo_begin = __dtbo_##overlay_name##_begin, \
3303 .dtbo_end = __dtbo_##overlay_name##_end, \
3304 .expected_result = expected, \
3305 .expected_result_remove = expected_remove, \
3306 .name = #overlay_name, \
3309 struct overlay_info {
3310 uint8_t *dtbo_begin;
3312 int expected_result;
3313 int expected_result_remove; /* if apply failed */
3318 OVERLAY_INFO_EXTERN(overlay_base);
3319 OVERLAY_INFO_EXTERN(overlay);
3320 OVERLAY_INFO_EXTERN(overlay_0);
3321 OVERLAY_INFO_EXTERN(overlay_1);
3322 OVERLAY_INFO_EXTERN(overlay_2);
3323 OVERLAY_INFO_EXTERN(overlay_3);
3324 OVERLAY_INFO_EXTERN(overlay_4);
3325 OVERLAY_INFO_EXTERN(overlay_5);
3326 OVERLAY_INFO_EXTERN(overlay_6);
3327 OVERLAY_INFO_EXTERN(overlay_7);
3328 OVERLAY_INFO_EXTERN(overlay_8);
3329 OVERLAY_INFO_EXTERN(overlay_9);
3330 OVERLAY_INFO_EXTERN(overlay_10);
3331 OVERLAY_INFO_EXTERN(overlay_11);
3332 OVERLAY_INFO_EXTERN(overlay_12);
3333 OVERLAY_INFO_EXTERN(overlay_13);
3334 OVERLAY_INFO_EXTERN(overlay_15);
3335 OVERLAY_INFO_EXTERN(overlay_16);
3336 OVERLAY_INFO_EXTERN(overlay_17);
3337 OVERLAY_INFO_EXTERN(overlay_18);
3338 OVERLAY_INFO_EXTERN(overlay_19);
3339 OVERLAY_INFO_EXTERN(overlay_20);
3340 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3341 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3342 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3343 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3344 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3345 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3346 OVERLAY_INFO_EXTERN(overlay_pci_node);
3347 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3348 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3349 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3350 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3351 OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
3353 /* entries found by name */
3354 static struct overlay_info overlays[] = {
3355 OVERLAY_INFO(overlay_base, -9999, 0),
3356 OVERLAY_INFO(overlay, 0, 0),
3357 OVERLAY_INFO(overlay_0, 0, 0),
3358 OVERLAY_INFO(overlay_1, 0, 0),
3359 OVERLAY_INFO(overlay_2, 0, 0),
3360 OVERLAY_INFO(overlay_3, 0, 0),
3361 OVERLAY_INFO(overlay_4, 0, 0),
3362 OVERLAY_INFO(overlay_5, 0, 0),
3363 OVERLAY_INFO(overlay_6, 0, 0),
3364 OVERLAY_INFO(overlay_7, 0, 0),
3365 OVERLAY_INFO(overlay_8, 0, 0),
3366 OVERLAY_INFO(overlay_9, 0, 0),
3367 OVERLAY_INFO(overlay_10, 0, 0),
3368 OVERLAY_INFO(overlay_11, 0, 0),
3369 OVERLAY_INFO(overlay_12, 0, 0),
3370 OVERLAY_INFO(overlay_13, 0, 0),
3371 OVERLAY_INFO(overlay_15, 0, 0),
3372 OVERLAY_INFO(overlay_16, -EBUSY, 0),
3373 OVERLAY_INFO(overlay_17, -EEXIST, 0),
3374 OVERLAY_INFO(overlay_18, 0, 0),
3375 OVERLAY_INFO(overlay_19, 0, 0),
3376 OVERLAY_INFO(overlay_20, 0, 0),
3377 OVERLAY_INFO(overlay_gpio_01, 0, 0),
3378 OVERLAY_INFO(overlay_gpio_02a, 0, 0),
3379 OVERLAY_INFO(overlay_gpio_02b, 0, 0),
3380 OVERLAY_INFO(overlay_gpio_03, 0, 0),
3381 OVERLAY_INFO(overlay_gpio_04a, 0, 0),
3382 OVERLAY_INFO(overlay_gpio_04b, 0, 0),
3383 OVERLAY_INFO(overlay_pci_node, 0, 0),
3384 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
3385 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
3386 OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
3387 OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
3388 OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
3393 static struct device_node *overlay_base_root;
3395 static void * __init dt_alloc_memory(u64 size, u64 align)
3397 void *ptr = memblock_alloc(size, align);
3400 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
3401 __func__, size, align);
3407 * Create base device tree for the overlay unittest.
3409 * This is called from very early boot code.
3411 * Do as much as possible the same way as done in __unflatten_device_tree
3412 * and other early boot steps for the normal FDT so that the overlay base
3413 * unflattened tree will have the same characteristics as the real tree
3414 * (such as having memory allocated by the early allocator). The goal
3415 * is to test "the real thing" as much as possible, and test "test setup
3416 * code" as little as possible.
3418 * Have to stop before resolving phandles, because that uses kmalloc.
3420 void __init unittest_unflatten_overlay_base(void)
3422 struct overlay_info *info;
3427 const char *overlay_name = "overlay_base";
3429 for (info = overlays; info && info->name; info++) {
3430 if (!strcmp(overlay_name, info->name)) {
3436 pr_err("no overlay data for %s\n", overlay_name);
3440 info = &overlays[0];
3442 if (info->expected_result != -9999) {
3443 pr_err("No dtb 'overlay_base' to attach\n");
3447 data_size = info->dtbo_end - info->dtbo_begin;
3449 pr_err("No dtb 'overlay_base' to attach\n");
3453 size = fdt_totalsize(info->dtbo_begin);
3454 if (size != data_size) {
3455 pr_err("dtb 'overlay_base' header totalsize != actual size");
3459 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3461 pr_err("alloc for dtb 'overlay_base' failed");
3465 memcpy(new_fdt, info->dtbo_begin, size);
3467 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3468 dt_alloc_memory, true);
3472 * The purpose of of_unittest_overlay_data_add is to add an
3473 * overlay in the normal fashion. This is a test of the whole
3474 * picture, instead of testing individual elements.
3476 * A secondary purpose is to be able to verify that the contents of
3477 * /proc/device-tree/ contains the updated structure and values from
3478 * the overlay. That must be verified separately in user space.
3480 * Return 0 on unexpected error.
3482 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3484 struct overlay_info *info;
3490 for (info = overlays; info && info->name; info++) {
3491 if (!strcmp(overlay_name, info->name)) {
3497 pr_err("no overlay data for %s\n", overlay_name);
3501 size = info->dtbo_end - info->dtbo_begin;
3503 pr_err("no overlay data for %s\n", overlay_name);
3505 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
3508 *ovcs_id = info->ovcs_id;
3512 pr_debug("%s applied\n", overlay_name);
3515 if (ret != info->expected_result) {
3516 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3517 info->expected_result, ret, overlay_name);
3522 /* changeset may be partially applied */
3523 ret2 = of_overlay_remove(&info->ovcs_id);
3524 if (ret2 != info->expected_result_remove) {
3525 pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
3526 info->expected_result_remove, ret2,
3536 * The purpose of of_unittest_overlay_high_level is to add an overlay
3537 * in the normal fashion. This is a test of the whole picture,
3538 * instead of individual elements.
3540 * The first part of the function is _not_ normal overlay usage; it is
3541 * finishing splicing the base overlay device tree into the live tree.
3543 static __init void of_unittest_overlay_high_level(void)
3545 struct device_node *last_sibling;
3546 struct device_node *np;
3547 struct device_node *of_symbols;
3548 struct device_node *overlay_base_symbols;
3549 struct device_node **pprev;
3550 struct property *prop;
3553 if (!overlay_base_root) {
3554 unittest(0, "overlay_base_root not initialized\n");
3559 * Could not fixup phandles in unittest_unflatten_overlay_base()
3560 * because kmalloc() was not yet available.
3562 of_overlay_mutex_lock();
3563 of_resolve_phandles(overlay_base_root);
3564 of_overlay_mutex_unlock();
3568 * do not allow overlay_base to duplicate any node already in
3569 * tree, this greatly simplifies the code
3573 * remove overlay_base_root node "__local_fixups", after
3574 * being used by of_resolve_phandles()
3576 pprev = &overlay_base_root->child;
3577 for (np = overlay_base_root->child; np; np = np->sibling) {
3578 if (of_node_name_eq(np, "__local_fixups__")) {
3579 *pprev = np->sibling;
3582 pprev = &np->sibling;
3585 /* remove overlay_base_root node "__symbols__" if in live tree */
3586 of_symbols = of_get_child_by_name(of_root, "__symbols__");
3588 /* will have to graft properties from node into live tree */
3589 pprev = &overlay_base_root->child;
3590 for (np = overlay_base_root->child; np; np = np->sibling) {
3591 if (of_node_name_eq(np, "__symbols__")) {
3592 overlay_base_symbols = np;
3593 *pprev = np->sibling;
3596 pprev = &np->sibling;
3600 for_each_child_of_node(overlay_base_root, np) {
3601 struct device_node *base_child;
3602 for_each_child_of_node(of_root, base_child) {
3603 if (!strcmp(np->full_name, base_child->full_name)) {
3604 unittest(0, "illegal node name in overlay_base %pOFn",
3607 of_node_put(base_child);
3614 * overlay 'overlay_base' is not allowed to have root
3615 * properties, so only need to splice nodes into main device tree.
3617 * root node of *overlay_base_root will not be freed, it is lost
3621 for (np = overlay_base_root->child; np; np = np->sibling)
3622 np->parent = of_root;
3624 mutex_lock(&of_mutex);
3626 for (last_sibling = np = of_root->child; np; np = np->sibling)
3630 last_sibling->sibling = overlay_base_root->child;
3632 of_root->child = overlay_base_root->child;
3634 for_each_of_allnodes_from(overlay_base_root, np)
3635 __of_attach_node_sysfs(np);
3638 struct property *new_prop;
3639 for_each_property_of_node(overlay_base_symbols, prop) {
3641 new_prop = __of_prop_dup(prop, GFP_KERNEL);
3643 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3647 if (__of_add_property(of_symbols, new_prop)) {
3648 kfree(new_prop->name);
3649 kfree(new_prop->value);
3651 /* "name" auto-generated by unflatten */
3652 if (!strcmp(prop->name, "name"))
3654 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3658 if (__of_add_property_sysfs(of_symbols, new_prop)) {
3659 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3666 mutex_unlock(&of_mutex);
3669 /* now do the normal overlay usage test */
3671 /* --- overlay --- */
3673 EXPECT_BEGIN(KERN_ERR,
3674 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3675 EXPECT_BEGIN(KERN_ERR,
3676 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3677 EXPECT_BEGIN(KERN_ERR,
3678 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3679 EXPECT_BEGIN(KERN_ERR,
3680 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3681 EXPECT_BEGIN(KERN_ERR,
3682 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3683 EXPECT_BEGIN(KERN_ERR,
3684 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3685 EXPECT_BEGIN(KERN_ERR,
3686 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3687 EXPECT_BEGIN(KERN_ERR,
3688 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3689 EXPECT_BEGIN(KERN_ERR,
3690 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3691 EXPECT_BEGIN(KERN_ERR,
3692 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3693 EXPECT_BEGIN(KERN_ERR,
3694 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3696 ret = overlay_data_apply("overlay", NULL);
3698 EXPECT_END(KERN_ERR,
3699 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3700 EXPECT_END(KERN_ERR,
3701 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3702 EXPECT_END(KERN_ERR,
3703 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3704 EXPECT_END(KERN_ERR,
3705 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3706 EXPECT_END(KERN_ERR,
3707 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3708 EXPECT_END(KERN_ERR,
3709 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3710 EXPECT_END(KERN_ERR,
3711 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3712 EXPECT_END(KERN_ERR,
3713 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3714 EXPECT_END(KERN_ERR,
3715 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3716 EXPECT_END(KERN_ERR,
3717 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3718 EXPECT_END(KERN_ERR,
3719 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3721 unittest(ret, "Adding overlay 'overlay' failed\n");
3723 /* --- overlay_bad_add_dup_node --- */
3725 EXPECT_BEGIN(KERN_ERR,
3726 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3727 EXPECT_BEGIN(KERN_ERR,
3728 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3729 EXPECT_BEGIN(KERN_ERR,
3730 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3731 EXPECT_BEGIN(KERN_ERR,
3732 "OF: Error reverting changeset (-19)");
3734 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3735 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3737 EXPECT_END(KERN_ERR,
3738 "OF: Error reverting changeset (-19)");
3739 EXPECT_END(KERN_ERR,
3740 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3741 EXPECT_END(KERN_ERR,
3742 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3743 EXPECT_END(KERN_ERR,
3744 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3746 /* --- overlay_bad_add_dup_prop --- */
3748 EXPECT_BEGIN(KERN_ERR,
3749 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3750 EXPECT_BEGIN(KERN_ERR,
3751 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3752 EXPECT_BEGIN(KERN_ERR,
3753 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3754 EXPECT_BEGIN(KERN_ERR,
3755 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3756 EXPECT_BEGIN(KERN_ERR,
3757 "OF: Error reverting changeset (-19)");
3759 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3760 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3762 EXPECT_END(KERN_ERR,
3763 "OF: Error reverting changeset (-19)");
3764 EXPECT_END(KERN_ERR,
3765 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3766 EXPECT_END(KERN_ERR,
3767 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3768 EXPECT_END(KERN_ERR,
3769 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3770 EXPECT_END(KERN_ERR,
3771 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3773 /* --- overlay_bad_phandle --- */
3775 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
3776 "Adding overlay 'overlay_bad_phandle' failed\n");
3778 /* --- overlay_bad_symbol --- */
3780 EXPECT_BEGIN(KERN_ERR,
3781 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3782 EXPECT_BEGIN(KERN_ERR,
3783 "OF: Error reverting changeset (-19)");
3785 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
3786 "Adding overlay 'overlay_bad_symbol' failed\n");
3788 EXPECT_END(KERN_ERR,
3789 "OF: Error reverting changeset (-19)");
3790 EXPECT_END(KERN_ERR,
3791 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3793 /* --- overlay_bad_unresolved --- */
3795 EXPECT_BEGIN(KERN_ERR,
3796 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3797 EXPECT_BEGIN(KERN_ERR,
3798 "OF: resolver: overlay phandle fixup failed: -22");
3800 unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
3801 "Adding overlay 'overlay_bad_unresolved' failed\n");
3803 EXPECT_END(KERN_ERR,
3804 "OF: resolver: overlay phandle fixup failed: -22");
3805 EXPECT_END(KERN_ERR,
3806 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3811 mutex_unlock(&of_mutex);
3814 static int of_unittest_pci_dev_num;
3815 static int of_unittest_pci_child_num;
3818 * PCI device tree node test driver
3820 static const struct pci_device_id testdrv_pci_ids[] = {
3821 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
3825 static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3827 struct overlay_info *info;
3828 struct device_node *dn;
3832 dn = pdev->dev.of_node;
3834 dev_err(&pdev->dev, "does not find bus endpoint");
3838 for (info = overlays; info && info->name; info++) {
3839 if (!strcmp(info->name, "overlay_pci_node"))
3842 if (!info || !info->name) {
3843 dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
3847 size = info->dtbo_end - info->dtbo_begin;
3848 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
3853 of_platform_default_populate(dn, NULL, &pdev->dev);
3854 pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
3859 static void testdrv_remove(struct pci_dev *pdev)
3861 int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
3863 of_platform_depopulate(&pdev->dev);
3864 of_overlay_remove(&ovcs_id);
3867 static struct pci_driver testdrv_driver = {
3868 .name = "pci_dt_testdrv",
3869 .id_table = testdrv_pci_ids,
3870 .probe = testdrv_probe,
3871 .remove = testdrv_remove,
3874 static int unittest_pci_probe(struct platform_device *pdev)
3876 struct resource *res;
3880 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3885 while (dev && !dev_is_pci(dev))
3888 pr_err("unable to find parent device\n");
3892 exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
3893 unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
3894 (u64)res->start, exp_addr);
3896 of_unittest_pci_child_num++;
3901 static const struct of_device_id unittest_pci_of_match[] = {
3902 { .compatible = "unittest-pci" },
3906 static struct platform_driver unittest_pci_driver = {
3907 .probe = unittest_pci_probe,
3909 .name = "unittest-pci",
3910 .of_match_table = unittest_pci_of_match,
3914 static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
3916 struct device_node *pnp, *np = NULL;
3917 struct device *child_dev;
3922 pnp = pdev->dev.of_node;
3923 unittest(pnp, "Failed creating PCI dt node\n");
3928 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
3929 np = of_find_node_by_path(path);
3930 unittest(np, "Failed to get unittest-pci node under PCI node\n");
3936 reg = of_get_property(np, "reg", NULL);
3937 unittest(reg, "Failed to get reg property\n");
3941 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
3942 np = of_find_node_by_path(path);
3943 unittest(!np, "Child device tree node is not removed\n");
3944 child_dev = device_find_any_child(&pdev->dev);
3945 unittest(!child_dev, "Child device is not removed\n");
3956 static void __init of_unittest_pci_node(void)
3958 struct pci_dev *pdev = NULL;
3961 if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
3964 rc = pci_register_driver(&testdrv_driver);
3965 unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
3969 rc = platform_driver_register(&unittest_pci_driver);
3970 if (unittest(!rc, "Failed to register unittest pci driver\n")) {
3971 pci_unregister_driver(&testdrv_driver);
3975 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
3976 of_unittest_pci_node_verify(pdev, true);
3977 of_unittest_pci_dev_num++;
3982 unittest(of_unittest_pci_dev_num,
3983 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
3984 unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
3985 "Child device number %d is not expected %d", of_unittest_pci_child_num,
3986 of_unittest_pci_dev_num);
3988 platform_driver_unregister(&unittest_pci_driver);
3989 pci_unregister_driver(&testdrv_driver);
3991 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
3992 of_unittest_pci_node_verify(pdev, false);
3998 static inline __init void of_unittest_overlay_high_level(void) {}
3999 static inline __init void of_unittest_pci_node(void) { }
4003 static int __init of_unittest(void)
4005 struct device_node *np;
4008 pr_info("start of unittest - you will see error messages\n");
4010 /* Taint the kernel so we know we've run tests. */
4011 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
4013 /* adding data for unittest */
4015 if (IS_ENABLED(CONFIG_UML))
4016 unittest_unflatten_overlay_base();
4018 res = unittest_data_add();
4022 of_aliases = of_find_node_by_path("/aliases");
4024 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4026 pr_info("No testcase data in device tree; not running tests\n");
4031 of_unittest_check_tree_linkage();
4032 of_unittest_check_phandles();
4033 of_unittest_find_node_by_name();
4034 of_unittest_dynamic();
4035 of_unittest_parse_phandle_with_args();
4036 of_unittest_parse_phandle_with_args_map();
4037 of_unittest_printf();
4038 of_unittest_property_string();
4039 of_unittest_property_copy();
4040 of_unittest_changeset();
4041 of_unittest_parse_interrupts();
4042 of_unittest_parse_interrupts_extended();
4043 of_unittest_dma_get_max_cpu_address();
4044 of_unittest_parse_dma_ranges();
4045 of_unittest_pci_dma_ranges();
4046 of_unittest_bus_ranges();
4047 of_unittest_bus_3cell_ranges();
4049 of_unittest_match_node();
4050 of_unittest_platform_populate();
4051 of_unittest_overlay();
4052 of_unittest_lifecycle();
4053 of_unittest_pci_node();
4055 /* Double check linkage after removing testcase data */
4056 of_unittest_check_tree_linkage();
4058 of_unittest_overlay_high_level();
4060 pr_info("end of unittest - %i passed, %i failed\n",
4061 unittest_results.passed, unittest_results.failed);
4065 late_initcall(of_unittest);