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/err.h>
11 #include <linux/errno.h>
12 #include <linux/hashtable.h>
13 #include <linux/libfdt.h>
15 #include <linux/of_address.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_platform.h>
19 #include <linux/list.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-mux.h>
28 #include <linux/bitops.h>
30 #include "of_private.h"
32 static struct unittest_results
{
37 #define unittest(result, fmt, ...) ({ \
38 bool failed = !(result); \
40 unittest_results.failed++; \
41 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
43 unittest_results.passed++; \
44 pr_debug("pass %s():%i\n", __func__, __LINE__); \
49 static void __init
of_unittest_find_node_by_name(void)
51 struct device_node
*np
;
52 const char *options
, *name
;
54 np
= of_find_node_by_path("/testcase-data");
55 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
56 unittest(np
&& !strcmp("/testcase-data", name
),
57 "find /testcase-data failed\n");
61 /* Test if trailing '/' works */
62 np
= of_find_node_by_path("/testcase-data/");
63 unittest(!np
, "trailing '/' on /testcase-data/ should fail\n");
65 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
66 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
67 unittest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
),
68 "find /testcase-data/phandle-tests/consumer-a failed\n");
72 np
= of_find_node_by_path("testcase-alias");
73 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
74 unittest(np
&& !strcmp("/testcase-data", name
),
75 "find testcase-alias failed\n");
79 /* Test if trailing '/' works on aliases */
80 np
= of_find_node_by_path("testcase-alias/");
81 unittest(!np
, "trailing '/' on testcase-alias/ should fail\n");
83 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
84 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
85 unittest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
),
86 "find testcase-alias/phandle-tests/consumer-a failed\n");
90 np
= of_find_node_by_path("/testcase-data/missing-path");
91 unittest(!np
, "non-existent path returned node %pOF\n", np
);
94 np
= of_find_node_by_path("missing-alias");
95 unittest(!np
, "non-existent alias returned node %pOF\n", np
);
98 np
= of_find_node_by_path("testcase-alias/missing-path");
99 unittest(!np
, "non-existent alias with relative path returned node %pOF\n", np
);
102 np
= of_find_node_opts_by_path("/testcase-data:testoption", &options
);
103 unittest(np
&& !strcmp("testoption", options
),
104 "option path test failed\n");
107 np
= of_find_node_opts_by_path("/testcase-data:test/option", &options
);
108 unittest(np
&& !strcmp("test/option", options
),
109 "option path test, subcase #1 failed\n");
112 np
= of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options
);
113 unittest(np
&& !strcmp("test/option", options
),
114 "option path test, subcase #2 failed\n");
117 np
= of_find_node_opts_by_path("/testcase-data:testoption", NULL
);
118 unittest(np
, "NULL option path test failed\n");
121 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption",
123 unittest(np
&& !strcmp("testaliasoption", options
),
124 "option alias path test failed\n");
127 np
= of_find_node_opts_by_path("testcase-alias:test/alias/option",
129 unittest(np
&& !strcmp("test/alias/option", options
),
130 "option alias path test, subcase #1 failed\n");
133 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL
);
134 unittest(np
, "NULL option alias path test failed\n");
137 options
= "testoption";
138 np
= of_find_node_opts_by_path("testcase-alias", &options
);
139 unittest(np
&& !options
, "option clearing test failed\n");
142 options
= "testoption";
143 np
= of_find_node_opts_by_path("/", &options
);
144 unittest(np
&& !options
, "option clearing root node test failed\n");
148 static void __init
of_unittest_dynamic(void)
150 struct device_node
*np
;
151 struct property
*prop
;
153 np
= of_find_node_by_path("/testcase-data");
155 pr_err("missing testcase data\n");
159 /* Array of 4 properties for the purpose of testing */
160 prop
= kcalloc(4, sizeof(*prop
), GFP_KERNEL
);
162 unittest(0, "kzalloc() failed\n");
166 /* Add a new property - should pass*/
167 prop
->name
= "new-property";
168 prop
->value
= "new-property-data";
169 prop
->length
= strlen(prop
->value
) + 1;
170 unittest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
172 /* Try to add an existing property - should fail */
174 prop
->name
= "new-property";
175 prop
->value
= "new-property-data-should-fail";
176 prop
->length
= strlen(prop
->value
) + 1;
177 unittest(of_add_property(np
, prop
) != 0,
178 "Adding an existing property should have failed\n");
180 /* Try to modify an existing property - should pass */
181 prop
->value
= "modify-property-data-should-pass";
182 prop
->length
= strlen(prop
->value
) + 1;
183 unittest(of_update_property(np
, prop
) == 0,
184 "Updating an existing property should have passed\n");
186 /* Try to modify non-existent property - should pass*/
188 prop
->name
= "modify-property";
189 prop
->value
= "modify-missing-property-data-should-pass";
190 prop
->length
= strlen(prop
->value
) + 1;
191 unittest(of_update_property(np
, prop
) == 0,
192 "Updating a missing property should have passed\n");
194 /* Remove property - should pass */
195 unittest(of_remove_property(np
, prop
) == 0,
196 "Removing a property should have passed\n");
198 /* Adding very large property - should pass */
200 prop
->name
= "large-property-PAGE_SIZEx8";
201 prop
->length
= PAGE_SIZE
* 8;
202 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
203 unittest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
205 unittest(of_add_property(np
, prop
) == 0,
206 "Adding a large property should have passed\n");
209 static int __init
of_unittest_check_node_linkage(struct device_node
*np
)
211 struct device_node
*child
;
214 for_each_child_of_node(np
, child
) {
215 if (child
->parent
!= np
) {
216 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
222 rc
= of_unittest_check_node_linkage(child
);
234 static void __init
of_unittest_check_tree_linkage(void)
236 struct device_node
*np
;
237 int allnode_count
= 0, child_count
;
242 for_each_of_allnodes(np
)
244 child_count
= of_unittest_check_node_linkage(of_root
);
246 unittest(child_count
> 0, "Device node data structure is corrupted\n");
247 unittest(child_count
== allnode_count
,
248 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
249 allnode_count
, child_count
);
250 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
253 static void __init
of_unittest_printf_one(struct device_node
*np
, const char *fmt
,
254 const char *expected
)
260 buf_size
= strlen(expected
) + 10;
261 buf
= kmalloc(buf_size
, GFP_KERNEL
);
265 /* Baseline; check conversion with a large size limit */
266 memset(buf
, 0xff, buf_size
);
267 size
= snprintf(buf
, buf_size
- 2, fmt
, np
);
269 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
270 unittest((strcmp(buf
, expected
) == 0) && (buf
[size
+1] == 0xff),
271 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
274 /* Make sure length limits work */
276 for (i
= 0; i
< 2; i
++, size
--) {
277 /* Clear the buffer, and make sure it works correctly still */
278 memset(buf
, 0xff, buf_size
);
279 snprintf(buf
, size
+1, fmt
, np
);
280 unittest(strncmp(buf
, expected
, size
) == 0 && (buf
[size
+1] == 0xff),
281 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
282 size
, fmt
, expected
, buf
);
287 static void __init
of_unittest_printf(void)
289 struct device_node
*np
;
290 const char *full_name
= "/testcase-data/platform-tests/test-device@1/dev@100";
291 char phandle_str
[16] = "";
293 np
= of_find_node_by_path(full_name
);
295 unittest(np
, "testcase data missing\n");
299 num_to_str(phandle_str
, sizeof(phandle_str
), np
->phandle
, 0);
301 of_unittest_printf_one(np
, "%pOF", full_name
);
302 of_unittest_printf_one(np
, "%pOFf", full_name
);
303 of_unittest_printf_one(np
, "%pOFn", "dev");
304 of_unittest_printf_one(np
, "%2pOFn", "dev");
305 of_unittest_printf_one(np
, "%5pOFn", " dev");
306 of_unittest_printf_one(np
, "%pOFnc", "dev:test-sub-device");
307 of_unittest_printf_one(np
, "%pOFp", phandle_str
);
308 of_unittest_printf_one(np
, "%pOFP", "dev@100");
309 of_unittest_printf_one(np
, "ABC %pOFP ABC", "ABC dev@100 ABC");
310 of_unittest_printf_one(np
, "%10pOFP", " dev@100");
311 of_unittest_printf_one(np
, "%-10pOFP", "dev@100 ");
312 of_unittest_printf_one(of_root
, "%pOFP", "/");
313 of_unittest_printf_one(np
, "%pOFF", "----");
314 of_unittest_printf_one(np
, "%pOFPF", "dev@100:----");
315 of_unittest_printf_one(np
, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
316 of_unittest_printf_one(np
, "%pOFc", "test-sub-device");
317 of_unittest_printf_one(np
, "%pOFC",
318 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
322 struct hlist_node node
;
323 struct device_node
*np
;
326 static DEFINE_HASHTABLE(phandle_ht
, 8);
327 static void __init
of_unittest_check_phandles(void)
329 struct device_node
*np
;
330 struct node_hash
*nh
;
331 struct hlist_node
*tmp
;
332 int i
, dup_count
= 0, phandle_count
= 0;
334 for_each_of_allnodes(np
) {
338 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
339 if (nh
->np
->phandle
== np
->phandle
) {
340 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
341 np
->phandle
, nh
->np
, np
);
347 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
352 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
355 unittest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
356 dup_count
, phandle_count
);
359 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
365 static void __init
of_unittest_parse_phandle_with_args(void)
367 struct device_node
*np
;
368 struct of_phandle_args args
;
371 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
373 pr_err("missing testcase data\n");
377 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
378 unittest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
380 for (i
= 0; i
< 8; i
++) {
383 memset(&args
, 0, sizeof(args
));
384 rc
= of_parse_phandle_with_args(np
, "phandle-list",
385 "#phandle-cells", i
, &args
);
387 /* Test the values from tests-phandle.dtsi */
391 passed
&= (args
.args_count
== 1);
392 passed
&= (args
.args
[0] == (i
+ 1));
396 passed
&= (args
.args_count
== 2);
397 passed
&= (args
.args
[0] == (i
+ 1));
398 passed
&= (args
.args
[1] == 0);
401 passed
&= (rc
== -ENOENT
);
405 passed
&= (args
.args_count
== 3);
406 passed
&= (args
.args
[0] == (i
+ 1));
407 passed
&= (args
.args
[1] == 4);
408 passed
&= (args
.args
[2] == 3);
412 passed
&= (args
.args_count
== 2);
413 passed
&= (args
.args
[0] == (i
+ 1));
414 passed
&= (args
.args
[1] == 100);
418 passed
&= (args
.args_count
== 0);
422 passed
&= (args
.args_count
== 1);
423 passed
&= (args
.args
[0] == (i
+ 1));
426 passed
&= (rc
== -ENOENT
);
432 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
436 /* Check for missing list property */
437 memset(&args
, 0, sizeof(args
));
438 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
439 "#phandle-cells", 0, &args
);
440 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
441 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
443 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
445 /* Check for missing cells property */
446 memset(&args
, 0, sizeof(args
));
447 rc
= of_parse_phandle_with_args(np
, "phandle-list",
448 "#phandle-cells-missing", 0, &args
);
449 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
450 rc
= of_count_phandle_with_args(np
, "phandle-list",
451 "#phandle-cells-missing");
452 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
454 /* Check for bad phandle in list */
455 memset(&args
, 0, sizeof(args
));
456 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
457 "#phandle-cells", 0, &args
);
458 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
459 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
461 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
463 /* Check for incorrectly formed argument list */
464 memset(&args
, 0, sizeof(args
));
465 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
466 "#phandle-cells", 1, &args
);
467 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
468 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
470 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
473 static void __init
of_unittest_parse_phandle_with_args_map(void)
475 struct device_node
*np
, *p0
, *p1
, *p2
, *p3
;
476 struct of_phandle_args args
;
479 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
481 pr_err("missing testcase data\n");
485 p0
= of_find_node_by_path("/testcase-data/phandle-tests/provider0");
487 pr_err("missing testcase data\n");
491 p1
= of_find_node_by_path("/testcase-data/phandle-tests/provider1");
493 pr_err("missing testcase data\n");
497 p2
= of_find_node_by_path("/testcase-data/phandle-tests/provider2");
499 pr_err("missing testcase data\n");
503 p3
= of_find_node_by_path("/testcase-data/phandle-tests/provider3");
505 pr_err("missing testcase data\n");
509 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
510 unittest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
512 for (i
= 0; i
< 8; i
++) {
515 memset(&args
, 0, sizeof(args
));
516 rc
= of_parse_phandle_with_args_map(np
, "phandle-list",
517 "phandle", i
, &args
);
519 /* Test the values from tests-phandle.dtsi */
523 passed
&= (args
.np
== p1
);
524 passed
&= (args
.args_count
== 1);
525 passed
&= (args
.args
[0] == 1);
529 passed
&= (args
.np
== p3
);
530 passed
&= (args
.args_count
== 3);
531 passed
&= (args
.args
[0] == 2);
532 passed
&= (args
.args
[1] == 5);
533 passed
&= (args
.args
[2] == 3);
536 passed
&= (rc
== -ENOENT
);
540 passed
&= (args
.np
== p0
);
541 passed
&= (args
.args_count
== 0);
545 passed
&= (args
.np
== p1
);
546 passed
&= (args
.args_count
== 1);
547 passed
&= (args
.args
[0] == 3);
551 passed
&= (args
.np
== p0
);
552 passed
&= (args
.args_count
== 0);
556 passed
&= (args
.np
== p2
);
557 passed
&= (args
.args_count
== 2);
558 passed
&= (args
.args
[0] == 15);
559 passed
&= (args
.args
[1] == 0x20);
562 passed
&= (rc
== -ENOENT
);
568 unittest(passed
, "index %i - data error on node %s rc=%i\n",
569 i
, args
.np
->full_name
, rc
);
572 /* Check for missing list property */
573 memset(&args
, 0, sizeof(args
));
574 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-missing",
575 "phandle", 0, &args
);
576 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
578 /* Check for missing cells,map,mask property */
579 memset(&args
, 0, sizeof(args
));
580 rc
= of_parse_phandle_with_args_map(np
, "phandle-list",
581 "phandle-missing", 0, &args
);
582 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
584 /* Check for bad phandle in list */
585 memset(&args
, 0, sizeof(args
));
586 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-bad-phandle",
587 "phandle", 0, &args
);
588 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
590 /* Check for incorrectly formed argument list */
591 memset(&args
, 0, sizeof(args
));
592 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-bad-args",
593 "phandle", 1, &args
);
594 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
597 static void __init
of_unittest_property_string(void)
599 const char *strings
[4];
600 struct device_node
*np
;
603 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
605 pr_err("No testcase data in device tree\n");
609 rc
= of_property_match_string(np
, "phandle-list-names", "first");
610 unittest(rc
== 0, "first expected:0 got:%i\n", rc
);
611 rc
= of_property_match_string(np
, "phandle-list-names", "second");
612 unittest(rc
== 1, "second expected:1 got:%i\n", rc
);
613 rc
= of_property_match_string(np
, "phandle-list-names", "third");
614 unittest(rc
== 2, "third expected:2 got:%i\n", rc
);
615 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
616 unittest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
617 rc
= of_property_match_string(np
, "missing-property", "blah");
618 unittest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
619 rc
= of_property_match_string(np
, "empty-property", "blah");
620 unittest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
621 rc
= of_property_match_string(np
, "unterminated-string", "blah");
622 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
624 /* of_property_count_strings() tests */
625 rc
= of_property_count_strings(np
, "string-property");
626 unittest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
627 rc
= of_property_count_strings(np
, "phandle-list-names");
628 unittest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
629 rc
= of_property_count_strings(np
, "unterminated-string");
630 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
631 rc
= of_property_count_strings(np
, "unterminated-string-list");
632 unittest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
634 /* of_property_read_string_index() tests */
635 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
636 unittest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
638 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
639 unittest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
640 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
641 unittest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
642 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
643 unittest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
644 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
645 unittest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
647 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
648 unittest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
650 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
651 unittest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
652 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
653 unittest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
655 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
656 unittest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
659 /* of_property_read_string_array() tests */
660 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
661 unittest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
662 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
663 unittest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
664 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
665 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
666 /* -- An incorrectly formed string should cause a failure */
667 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
668 unittest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
669 /* -- parsing the correctly formed strings should still work: */
671 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
672 unittest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
674 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
675 unittest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
678 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
679 (p1)->value && (p2)->value && \
680 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
681 !strcmp((p1)->name, (p2)->name))
682 static void __init
of_unittest_property_copy(void)
684 #ifdef CONFIG_OF_DYNAMIC
685 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
686 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
687 struct property
*new;
689 new = __of_prop_dup(&p1
, GFP_KERNEL
);
690 unittest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
695 new = __of_prop_dup(&p2
, GFP_KERNEL
);
696 unittest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
703 static void __init
of_unittest_changeset(void)
705 #ifdef CONFIG_OF_DYNAMIC
706 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 1, .value
= "" };
707 struct property
*ppname_n1
, pname_n1
= { .name
= "name", .length
= 3, .value
= "n1" };
708 struct property
*ppname_n2
, pname_n2
= { .name
= "name", .length
= 3, .value
= "n2" };
709 struct property
*ppname_n21
, pname_n21
= { .name
= "name", .length
= 3, .value
= "n21" };
710 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
711 struct property
*ppremove
;
712 struct device_node
*n1
, *n2
, *n21
, *nchangeset
, *nremove
, *parent
, *np
;
713 struct of_changeset chgset
;
715 n1
= __of_node_dup(NULL
, "n1");
716 unittest(n1
, "testcase setup failure\n");
718 n2
= __of_node_dup(NULL
, "n2");
719 unittest(n2
, "testcase setup failure\n");
721 n21
= __of_node_dup(NULL
, "n21");
722 unittest(n21
, "testcase setup failure %p\n", n21
);
724 nchangeset
= of_find_node_by_path("/testcase-data/changeset");
725 nremove
= of_get_child_by_name(nchangeset
, "node-remove");
726 unittest(nremove
, "testcase setup failure\n");
728 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
729 unittest(ppadd
, "testcase setup failure\n");
731 ppname_n1
= __of_prop_dup(&pname_n1
, GFP_KERNEL
);
732 unittest(ppname_n1
, "testcase setup failure\n");
734 ppname_n2
= __of_prop_dup(&pname_n2
, GFP_KERNEL
);
735 unittest(ppname_n2
, "testcase setup failure\n");
737 ppname_n21
= __of_prop_dup(&pname_n21
, GFP_KERNEL
);
738 unittest(ppname_n21
, "testcase setup failure\n");
740 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
741 unittest(ppupdate
, "testcase setup failure\n");
748 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
749 unittest(ppremove
, "failed to find removal prop");
751 of_changeset_init(&chgset
);
753 unittest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
754 unittest(!of_changeset_add_property(&chgset
, n1
, ppname_n1
), "fail add prop name\n");
756 unittest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
757 unittest(!of_changeset_add_property(&chgset
, n2
, ppname_n2
), "fail add prop name\n");
759 unittest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
760 unittest(!of_changeset_add_property(&chgset
, n21
, ppname_n21
), "fail add prop name\n");
762 unittest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
764 unittest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop prop-add\n");
765 unittest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
766 unittest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
768 unittest(!of_changeset_apply(&chgset
), "apply failed\n");
770 of_node_put(nchangeset
);
772 /* Make sure node names are constructed correctly */
773 unittest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
774 "'%pOF' not added\n", n21
);
777 unittest(!of_changeset_revert(&chgset
), "revert failed\n");
779 of_changeset_destroy(&chgset
);
783 static void __init
of_unittest_dma_ranges_one(const char *path
,
784 u64 expect_dma_addr
, u64 expect_paddr
, u64 expect_size
)
786 struct device_node
*np
;
787 u64 dma_addr
, paddr
, size
;
790 np
= of_find_node_by_path(path
);
792 pr_err("missing testcase data\n");
796 rc
= of_dma_get_range(np
, &dma_addr
, &paddr
, &size
);
798 unittest(!rc
, "of_dma_get_range failed on node %pOF rc=%i\n", np
, rc
);
800 unittest(size
== expect_size
,
801 "of_dma_get_range wrong size on node %pOF size=%llx\n", np
, size
);
802 unittest(paddr
== expect_paddr
,
803 "of_dma_get_range wrong phys addr (%llx) on node %pOF", paddr
, np
);
804 unittest(dma_addr
== expect_dma_addr
,
805 "of_dma_get_range wrong DMA addr (%llx) on node %pOF", dma_addr
, np
);
810 static void __init
of_unittest_parse_dma_ranges(void)
812 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
813 0x0, 0x20000000, 0x40000000);
814 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
815 0x10000000, 0x20000000, 0x40000000);
816 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
817 0x80000000, 0x20000000, 0x10000000);
820 static void __init
of_unittest_pci_dma_ranges(void)
822 struct device_node
*np
;
823 struct of_pci_range range
;
824 struct of_pci_range_parser parser
;
827 if (!IS_ENABLED(CONFIG_PCI
))
830 np
= of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
832 pr_err("missing testcase data\n");
836 if (of_pci_dma_range_parser_init(&parser
, np
)) {
837 pr_err("missing dma-ranges property\n");
842 * Get the dma-ranges from the device tree
844 for_each_of_pci_range(&parser
, &range
) {
846 unittest(range
.size
== 0x10000000,
847 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
849 unittest(range
.cpu_addr
== 0x20000000,
850 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
852 unittest(range
.pci_addr
== 0x80000000,
853 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
856 unittest(range
.size
== 0x10000000,
857 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
859 unittest(range
.cpu_addr
== 0x40000000,
860 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
862 unittest(range
.pci_addr
== 0xc0000000,
863 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
872 static void __init
of_unittest_parse_interrupts(void)
874 struct device_node
*np
;
875 struct of_phandle_args args
;
878 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
881 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
883 pr_err("missing testcase data\n");
887 for (i
= 0; i
< 4; i
++) {
890 memset(&args
, 0, sizeof(args
));
891 rc
= of_irq_parse_one(np
, i
, &args
);
894 passed
&= (args
.args_count
== 1);
895 passed
&= (args
.args
[0] == (i
+ 1));
897 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
902 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
904 pr_err("missing testcase data\n");
908 for (i
= 0; i
< 4; i
++) {
911 memset(&args
, 0, sizeof(args
));
912 rc
= of_irq_parse_one(np
, i
, &args
);
914 /* Test the values from tests-phandle.dtsi */
918 passed
&= (args
.args_count
== 1);
919 passed
&= (args
.args
[0] == 9);
923 passed
&= (args
.args_count
== 3);
924 passed
&= (args
.args
[0] == 10);
925 passed
&= (args
.args
[1] == 11);
926 passed
&= (args
.args
[2] == 12);
930 passed
&= (args
.args_count
== 2);
931 passed
&= (args
.args
[0] == 13);
932 passed
&= (args
.args
[1] == 14);
936 passed
&= (args
.args_count
== 2);
937 passed
&= (args
.args
[0] == 15);
938 passed
&= (args
.args
[1] == 16);
943 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
949 static void __init
of_unittest_parse_interrupts_extended(void)
951 struct device_node
*np
;
952 struct of_phandle_args args
;
955 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
958 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
960 pr_err("missing testcase data\n");
964 for (i
= 0; i
< 7; i
++) {
967 memset(&args
, 0, sizeof(args
));
968 rc
= of_irq_parse_one(np
, i
, &args
);
970 /* Test the values from tests-phandle.dtsi */
974 passed
&= (args
.args_count
== 1);
975 passed
&= (args
.args
[0] == 1);
979 passed
&= (args
.args_count
== 3);
980 passed
&= (args
.args
[0] == 2);
981 passed
&= (args
.args
[1] == 3);
982 passed
&= (args
.args
[2] == 4);
986 passed
&= (args
.args_count
== 2);
987 passed
&= (args
.args
[0] == 5);
988 passed
&= (args
.args
[1] == 6);
992 passed
&= (args
.args_count
== 1);
993 passed
&= (args
.args
[0] == 9);
997 passed
&= (args
.args_count
== 3);
998 passed
&= (args
.args
[0] == 10);
999 passed
&= (args
.args
[1] == 11);
1000 passed
&= (args
.args
[2] == 12);
1004 passed
&= (args
.args_count
== 2);
1005 passed
&= (args
.args
[0] == 13);
1006 passed
&= (args
.args
[1] == 14);
1010 passed
&= (args
.args_count
== 1);
1011 passed
&= (args
.args
[0] == 15);
1017 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1023 static const struct of_device_id match_node_table
[] = {
1024 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
1025 { .data
= "B", .type
= "type1", }, /* followed by type alone */
1027 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
1028 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
1029 { .data
= "Cc", .name
= "name2", .type
= "type2", },
1031 { .data
= "E", .compatible
= "compat3" },
1032 { .data
= "G", .compatible
= "compat2", },
1033 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
1034 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
1035 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
1036 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
1043 } match_node_tests
[] = {
1044 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
1045 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
1046 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
1047 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
1048 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
1049 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
1050 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
1051 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
1052 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
1053 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
1054 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
1055 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
1058 static void __init
of_unittest_match_node(void)
1060 struct device_node
*np
;
1061 const struct of_device_id
*match
;
1064 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
1065 np
= of_find_node_by_path(match_node_tests
[i
].path
);
1067 unittest(0, "missing testcase node %s\n",
1068 match_node_tests
[i
].path
);
1072 match
= of_match_node(match_node_table
, np
);
1074 unittest(0, "%s didn't match anything\n",
1075 match_node_tests
[i
].path
);
1079 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
1080 unittest(0, "%s got wrong match. expected %s, got %s\n",
1081 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
1082 (const char *)match
->data
);
1085 unittest(1, "passed");
1089 static struct resource test_bus_res
= {
1090 .start
= 0xfffffff8,
1092 .flags
= IORESOURCE_MEM
,
1094 static const struct platform_device_info test_bus_info
= {
1095 .name
= "unittest-bus",
1097 static void __init
of_unittest_platform_populate(void)
1100 struct device_node
*np
, *child
, *grandchild
;
1101 struct platform_device
*pdev
, *test_bus
;
1102 const struct of_device_id match
[] = {
1103 { .compatible
= "test-device", },
1107 np
= of_find_node_by_path("/testcase-data");
1108 of_platform_default_populate(np
, NULL
, NULL
);
1110 /* Test that a missing irq domain returns -EPROBE_DEFER */
1111 np
= of_find_node_by_path("/testcase-data/testcase-device1");
1112 pdev
= of_find_device_by_node(np
);
1113 unittest(pdev
, "device 1 creation failed\n");
1115 if (!(of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)) {
1116 irq
= platform_get_irq(pdev
, 0);
1117 unittest(irq
== -EPROBE_DEFER
,
1118 "device deferred probe failed - %d\n", irq
);
1120 /* Test that a parsing failure does not return -EPROBE_DEFER */
1121 np
= of_find_node_by_path("/testcase-data/testcase-device2");
1122 pdev
= of_find_device_by_node(np
);
1123 unittest(pdev
, "device 2 creation failed\n");
1124 irq
= platform_get_irq(pdev
, 0);
1125 unittest(irq
< 0 && irq
!= -EPROBE_DEFER
,
1126 "device parsing error failed - %d\n", irq
);
1129 np
= of_find_node_by_path("/testcase-data/platform-tests");
1130 unittest(np
, "No testcase data in device tree\n");
1134 test_bus
= platform_device_register_full(&test_bus_info
);
1135 rc
= PTR_ERR_OR_ZERO(test_bus
);
1136 unittest(!rc
, "testbus registration failed; rc=%i\n", rc
);
1141 test_bus
->dev
.of_node
= np
;
1144 * Add a dummy resource to the test bus node after it is
1145 * registered to catch problems with un-inserted resources. The
1146 * DT code doesn't insert the resources, and it has caused the
1147 * kernel to oops in the past. This makes sure the same bug
1148 * doesn't crop up again.
1150 platform_device_add_resources(test_bus
, &test_bus_res
, 1);
1152 of_platform_populate(np
, match
, NULL
, &test_bus
->dev
);
1153 for_each_child_of_node(np
, child
) {
1154 for_each_child_of_node(child
, grandchild
)
1155 unittest(of_find_device_by_node(grandchild
),
1156 "Could not create device for node '%pOFn'\n",
1160 of_platform_depopulate(&test_bus
->dev
);
1161 for_each_child_of_node(np
, child
) {
1162 for_each_child_of_node(child
, grandchild
)
1163 unittest(!of_find_device_by_node(grandchild
),
1164 "device didn't get destroyed '%pOFn'\n",
1168 platform_device_unregister(test_bus
);
1173 * update_node_properties - adds the properties
1174 * of np into dup node (present in live tree) and
1175 * updates parent of children of np to dup.
1177 * @np: node whose properties are being added to the live tree
1178 * @dup: node present in live tree to be updated
1180 static void update_node_properties(struct device_node
*np
,
1181 struct device_node
*dup
)
1183 struct property
*prop
;
1184 struct property
*save_next
;
1185 struct device_node
*child
;
1188 for_each_child_of_node(np
, child
)
1189 child
->parent
= dup
;
1192 * "unittest internal error: unable to add testdata property"
1194 * If this message reports a property in node '/__symbols__' then
1195 * the respective unittest overlay contains a label that has the
1196 * same name as a label in the live devicetree. The label will
1197 * be in the live devicetree only if the devicetree source was
1198 * compiled with the '-@' option. If you encounter this error,
1199 * please consider renaming __all__ of the labels in the unittest
1200 * overlay dts files with an odd prefix that is unlikely to be
1201 * used in a real devicetree.
1205 * open code for_each_property_of_node() because of_add_property()
1206 * sets prop->next to NULL
1208 for (prop
= np
->properties
; prop
!= NULL
; prop
= save_next
) {
1209 save_next
= prop
->next
;
1210 ret
= of_add_property(dup
, prop
);
1212 if (ret
== -EEXIST
&& !strcmp(prop
->name
, "name"))
1214 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1221 * attach_node_and_children - attaches nodes
1222 * and its children to live tree.
1223 * CAUTION: misleading function name - if node @np already exists in
1224 * the live tree then children of @np are *not* attached to the live
1225 * tree. This works for the current test devicetree nodes because such
1226 * nodes do not have child nodes.
1228 * @np: Node to attach to live tree
1230 static void attach_node_and_children(struct device_node
*np
)
1232 struct device_node
*next
, *dup
, *child
;
1233 unsigned long flags
;
1234 const char *full_name
;
1236 full_name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
1238 if (!strcmp(full_name
, "/__local_fixups__") ||
1239 !strcmp(full_name
, "/__fixups__")) {
1244 dup
= of_find_node_by_path(full_name
);
1247 update_node_properties(np
, dup
);
1254 mutex_lock(&of_mutex
);
1255 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1256 np
->sibling
= np
->parent
->child
;
1257 np
->parent
->child
= np
;
1258 of_node_clear_flag(np
, OF_DETACHED
);
1259 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1261 __of_attach_node_sysfs(np
);
1262 mutex_unlock(&of_mutex
);
1265 next
= child
->sibling
;
1266 attach_node_and_children(child
);
1272 * unittest_data_add - Reads, copies data from
1273 * linked tree and attaches it to the live tree
1275 static int __init
unittest_data_add(void)
1277 void *unittest_data
;
1278 struct device_node
*unittest_data_node
, *np
;
1280 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1281 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1283 extern uint8_t __dtb_testcases_begin
[];
1284 extern uint8_t __dtb_testcases_end
[];
1285 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
1289 pr_warn("%s: No testcase data to attach; not running tests\n",
1295 unittest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
1299 of_fdt_unflatten_tree(unittest_data
, NULL
, &unittest_data_node
);
1300 if (!unittest_data_node
) {
1301 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
1302 kfree(unittest_data
);
1307 * This lock normally encloses of_resolve_phandles()
1309 of_overlay_mutex_lock();
1311 rc
= of_resolve_phandles(unittest_data_node
);
1313 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
1314 of_overlay_mutex_unlock();
1319 of_root
= unittest_data_node
;
1320 for_each_of_allnodes(np
)
1321 __of_attach_node_sysfs(np
);
1322 of_aliases
= of_find_node_by_path("/aliases");
1323 of_chosen
= of_find_node_by_path("/chosen");
1324 of_overlay_mutex_unlock();
1328 /* attach the sub-tree to live tree */
1329 np
= unittest_data_node
->child
;
1331 struct device_node
*next
= np
->sibling
;
1333 np
->parent
= of_root
;
1334 attach_node_and_children(np
);
1338 of_overlay_mutex_unlock();
1343 #ifdef CONFIG_OF_OVERLAY
1344 static int __init
overlay_data_apply(const char *overlay_name
, int *overlay_id
);
1346 static int unittest_probe(struct platform_device
*pdev
)
1348 struct device
*dev
= &pdev
->dev
;
1349 struct device_node
*np
= dev
->of_node
;
1352 dev_err(dev
, "No OF data for device\n");
1357 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1359 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
1364 static int unittest_remove(struct platform_device
*pdev
)
1366 struct device
*dev
= &pdev
->dev
;
1367 struct device_node
*np
= dev
->of_node
;
1369 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1373 static const struct of_device_id unittest_match
[] = {
1374 { .compatible
= "unittest", },
1378 static struct platform_driver unittest_driver
= {
1379 .probe
= unittest_probe
,
1380 .remove
= unittest_remove
,
1383 .of_match_table
= of_match_ptr(unittest_match
),
1387 /* get the platform device instantiated at the path */
1388 static struct platform_device
*of_path_to_platform_device(const char *path
)
1390 struct device_node
*np
;
1391 struct platform_device
*pdev
;
1393 np
= of_find_node_by_path(path
);
1397 pdev
= of_find_device_by_node(np
);
1403 /* find out if a platform device exists at that path */
1404 static int of_path_platform_device_exists(const char *path
)
1406 struct platform_device
*pdev
;
1408 pdev
= of_path_to_platform_device(path
);
1409 platform_device_put(pdev
);
1410 return pdev
!= NULL
;
1413 #if IS_BUILTIN(CONFIG_I2C)
1415 /* get the i2c client device instantiated at the path */
1416 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
1418 struct device_node
*np
;
1419 struct i2c_client
*client
;
1421 np
= of_find_node_by_path(path
);
1425 client
= of_find_i2c_device_by_node(np
);
1431 /* find out if a i2c client device exists at that path */
1432 static int of_path_i2c_client_exists(const char *path
)
1434 struct i2c_client
*client
;
1436 client
= of_path_to_i2c_client(path
);
1438 put_device(&client
->dev
);
1439 return client
!= NULL
;
1442 static int of_path_i2c_client_exists(const char *path
)
1453 static int of_path_device_type_exists(const char *path
,
1454 enum overlay_type ovtype
)
1458 return of_path_platform_device_exists(path
);
1460 return of_path_i2c_client_exists(path
);
1465 static const char *unittest_path(int nr
, enum overlay_type ovtype
)
1468 static char buf
[256];
1472 base
= "/testcase-data/overlay-node/test-bus";
1475 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1481 snprintf(buf
, sizeof(buf
) - 1, "%s/test-unittest%d", base
, nr
);
1482 buf
[sizeof(buf
) - 1] = '\0';
1486 static int of_unittest_device_exists(int unittest_nr
, enum overlay_type ovtype
)
1490 path
= unittest_path(unittest_nr
, ovtype
);
1494 return of_path_platform_device_exists(path
);
1496 return of_path_i2c_client_exists(path
);
1501 static const char *overlay_name_from_nr(int nr
)
1503 static char buf
[256];
1505 snprintf(buf
, sizeof(buf
) - 1,
1507 buf
[sizeof(buf
) - 1] = '\0';
1512 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1514 /* it is guaranteed that overlay ids are assigned in sequence */
1515 #define MAX_UNITTEST_OVERLAYS 256
1516 static unsigned long overlay_id_bits
[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS
)];
1517 static int overlay_first_id
= -1;
1519 static void of_unittest_track_overlay(int id
)
1521 if (overlay_first_id
< 0)
1522 overlay_first_id
= id
;
1523 id
-= overlay_first_id
;
1525 /* we shouldn't need that many */
1526 BUG_ON(id
>= MAX_UNITTEST_OVERLAYS
);
1527 overlay_id_bits
[BIT_WORD(id
)] |= BIT_MASK(id
);
1530 static void of_unittest_untrack_overlay(int id
)
1532 if (overlay_first_id
< 0)
1534 id
-= overlay_first_id
;
1535 BUG_ON(id
>= MAX_UNITTEST_OVERLAYS
);
1536 overlay_id_bits
[BIT_WORD(id
)] &= ~BIT_MASK(id
);
1539 static void of_unittest_destroy_tracked_overlays(void)
1541 int id
, ret
, defers
, ovcs_id
;
1543 if (overlay_first_id
< 0)
1546 /* try until no defers */
1549 /* remove in reverse order */
1550 for (id
= MAX_UNITTEST_OVERLAYS
- 1; id
>= 0; id
--) {
1551 if (!(overlay_id_bits
[BIT_WORD(id
)] & BIT_MASK(id
)))
1554 ovcs_id
= id
+ overlay_first_id
;
1555 ret
= of_overlay_remove(&ovcs_id
);
1556 if (ret
== -ENODEV
) {
1557 pr_warn("%s: no overlay to destroy for #%d\n",
1558 __func__
, id
+ overlay_first_id
);
1563 pr_warn("%s: overlay destroy failed for #%d\n",
1564 __func__
, id
+ overlay_first_id
);
1568 overlay_id_bits
[BIT_WORD(id
)] &= ~BIT_MASK(id
);
1570 } while (defers
> 0);
1573 static int __init
of_unittest_apply_overlay(int overlay_nr
, int *overlay_id
)
1575 const char *overlay_name
;
1577 overlay_name
= overlay_name_from_nr(overlay_nr
);
1579 if (!overlay_data_apply(overlay_name
, overlay_id
)) {
1580 unittest(0, "could not apply overlay \"%s\"\n",
1584 of_unittest_track_overlay(*overlay_id
);
1589 /* apply an overlay while checking before and after states */
1590 static int __init
of_unittest_apply_overlay_check(int overlay_nr
,
1591 int unittest_nr
, int before
, int after
,
1592 enum overlay_type ovtype
)
1596 /* unittest device must not be in before state */
1597 if (of_unittest_device_exists(unittest_nr
, ovtype
) != before
) {
1598 unittest(0, "%s with device @\"%s\" %s\n",
1599 overlay_name_from_nr(overlay_nr
),
1600 unittest_path(unittest_nr
, ovtype
),
1601 !before
? "enabled" : "disabled");
1606 ret
= of_unittest_apply_overlay(overlay_nr
, &ovcs_id
);
1608 /* of_unittest_apply_overlay already called unittest() */
1612 /* unittest device must be to set to after state */
1613 if (of_unittest_device_exists(unittest_nr
, ovtype
) != after
) {
1614 unittest(0, "%s failed to create @\"%s\" %s\n",
1615 overlay_name_from_nr(overlay_nr
),
1616 unittest_path(unittest_nr
, ovtype
),
1617 !after
? "enabled" : "disabled");
1624 /* apply an overlay and then revert it while checking before, after states */
1625 static int __init
of_unittest_apply_revert_overlay_check(int overlay_nr
,
1626 int unittest_nr
, int before
, int after
,
1627 enum overlay_type ovtype
)
1631 /* unittest device must be in before state */
1632 if (of_unittest_device_exists(unittest_nr
, ovtype
) != before
) {
1633 unittest(0, "%s with device @\"%s\" %s\n",
1634 overlay_name_from_nr(overlay_nr
),
1635 unittest_path(unittest_nr
, ovtype
),
1636 !before
? "enabled" : "disabled");
1640 /* apply the overlay */
1642 ret
= of_unittest_apply_overlay(overlay_nr
, &ovcs_id
);
1644 /* of_unittest_apply_overlay already called unittest() */
1648 /* unittest device must be in after state */
1649 if (of_unittest_device_exists(unittest_nr
, ovtype
) != after
) {
1650 unittest(0, "%s failed to create @\"%s\" %s\n",
1651 overlay_name_from_nr(overlay_nr
),
1652 unittest_path(unittest_nr
, ovtype
),
1653 !after
? "enabled" : "disabled");
1657 ret
= of_overlay_remove(&ovcs_id
);
1659 unittest(0, "%s failed to be destroyed @\"%s\"\n",
1660 overlay_name_from_nr(overlay_nr
),
1661 unittest_path(unittest_nr
, ovtype
));
1665 /* unittest device must be again in before state */
1666 if (of_unittest_device_exists(unittest_nr
, PDEV_OVERLAY
) != before
) {
1667 unittest(0, "%s with device @\"%s\" %s\n",
1668 overlay_name_from_nr(overlay_nr
),
1669 unittest_path(unittest_nr
, ovtype
),
1670 !before
? "enabled" : "disabled");
1677 /* test activation of device */
1678 static void __init
of_unittest_overlay_0(void)
1680 /* device should enable */
1681 if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
))
1684 unittest(1, "overlay test %d passed\n", 0);
1687 /* test deactivation of device */
1688 static void __init
of_unittest_overlay_1(void)
1690 /* device should disable */
1691 if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
))
1694 unittest(1, "overlay test %d passed\n", 1);
1697 /* test activation of device */
1698 static void __init
of_unittest_overlay_2(void)
1700 /* device should enable */
1701 if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
))
1704 unittest(1, "overlay test %d passed\n", 2);
1707 /* test deactivation of device */
1708 static void __init
of_unittest_overlay_3(void)
1710 /* device should disable */
1711 if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
))
1714 unittest(1, "overlay test %d passed\n", 3);
1717 /* test activation of a full device node */
1718 static void __init
of_unittest_overlay_4(void)
1720 /* device should disable */
1721 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
))
1724 unittest(1, "overlay test %d passed\n", 4);
1727 /* test overlay apply/revert sequence */
1728 static void __init
of_unittest_overlay_5(void)
1730 /* device should disable */
1731 if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
))
1734 unittest(1, "overlay test %d passed\n", 5);
1737 /* test overlay application in sequence */
1738 static void __init
of_unittest_overlay_6(void)
1740 int i
, ov_id
[2], ovcs_id
;
1741 int overlay_nr
= 6, unittest_nr
= 6;
1742 int before
= 0, after
= 1;
1743 const char *overlay_name
;
1745 /* unittest device must be in before state */
1746 for (i
= 0; i
< 2; i
++) {
1747 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
1749 unittest(0, "%s with device @\"%s\" %s\n",
1750 overlay_name_from_nr(overlay_nr
+ i
),
1751 unittest_path(unittest_nr
+ i
,
1753 !before
? "enabled" : "disabled");
1758 /* apply the overlays */
1759 for (i
= 0; i
< 2; i
++) {
1761 overlay_name
= overlay_name_from_nr(overlay_nr
+ i
);
1763 if (!overlay_data_apply(overlay_name
, &ovcs_id
)) {
1764 unittest(0, "could not apply overlay \"%s\"\n",
1769 of_unittest_track_overlay(ov_id
[i
]);
1772 for (i
= 0; i
< 2; i
++) {
1773 /* unittest device must be in after state */
1774 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
1776 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1777 overlay_name_from_nr(overlay_nr
+ i
),
1778 unittest_path(unittest_nr
+ i
,
1780 !after
? "enabled" : "disabled");
1785 for (i
= 1; i
>= 0; i
--) {
1787 if (of_overlay_remove(&ovcs_id
)) {
1788 unittest(0, "%s failed destroy @\"%s\"\n",
1789 overlay_name_from_nr(overlay_nr
+ i
),
1790 unittest_path(unittest_nr
+ i
,
1794 of_unittest_untrack_overlay(ov_id
[i
]);
1797 for (i
= 0; i
< 2; i
++) {
1798 /* unittest device must be again in before state */
1799 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
1801 unittest(0, "%s with device @\"%s\" %s\n",
1802 overlay_name_from_nr(overlay_nr
+ i
),
1803 unittest_path(unittest_nr
+ i
,
1805 !before
? "enabled" : "disabled");
1810 unittest(1, "overlay test %d passed\n", 6);
1813 /* test overlay application in sequence */
1814 static void __init
of_unittest_overlay_8(void)
1816 int i
, ov_id
[2], ovcs_id
;
1817 int overlay_nr
= 8, unittest_nr
= 8;
1818 const char *overlay_name
;
1820 /* we don't care about device state in this test */
1822 /* apply the overlays */
1823 for (i
= 0; i
< 2; i
++) {
1825 overlay_name
= overlay_name_from_nr(overlay_nr
+ i
);
1827 if (!overlay_data_apply(overlay_name
, &ovcs_id
)) {
1828 unittest(0, "could not apply overlay \"%s\"\n",
1833 of_unittest_track_overlay(ov_id
[i
]);
1836 /* now try to remove first overlay (it should fail) */
1838 if (!of_overlay_remove(&ovcs_id
)) {
1839 unittest(0, "%s was destroyed @\"%s\"\n",
1840 overlay_name_from_nr(overlay_nr
+ 0),
1841 unittest_path(unittest_nr
,
1846 /* removing them in order should work */
1847 for (i
= 1; i
>= 0; i
--) {
1849 if (of_overlay_remove(&ovcs_id
)) {
1850 unittest(0, "%s not destroyed @\"%s\"\n",
1851 overlay_name_from_nr(overlay_nr
+ i
),
1852 unittest_path(unittest_nr
,
1856 of_unittest_untrack_overlay(ov_id
[i
]);
1859 unittest(1, "overlay test %d passed\n", 8);
1862 /* test insertion of a bus with parent devices */
1863 static void __init
of_unittest_overlay_10(void)
1868 /* device should disable */
1869 ret
= of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
1870 if (unittest(ret
== 0,
1871 "overlay test %d failed; overlay application\n", 10))
1874 child_path
= kasprintf(GFP_KERNEL
, "%s/test-unittest101",
1875 unittest_path(10, PDEV_OVERLAY
));
1876 if (unittest(child_path
, "overlay test %d failed; kasprintf\n", 10))
1879 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
1882 unittest(ret
, "overlay test %d failed; no child device\n", 10);
1885 /* test insertion of a bus with parent devices (and revert) */
1886 static void __init
of_unittest_overlay_11(void)
1890 /* device should disable */
1891 ret
= of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1893 unittest(ret
== 0, "overlay test %d failed; overlay apply\n", 11);
1896 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1898 struct unittest_i2c_bus_data
{
1899 struct platform_device
*pdev
;
1900 struct i2c_adapter adap
;
1903 static int unittest_i2c_master_xfer(struct i2c_adapter
*adap
,
1904 struct i2c_msg
*msgs
, int num
)
1906 struct unittest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
1913 static u32
unittest_i2c_functionality(struct i2c_adapter
*adap
)
1915 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
1918 static const struct i2c_algorithm unittest_i2c_algo
= {
1919 .master_xfer
= unittest_i2c_master_xfer
,
1920 .functionality
= unittest_i2c_functionality
,
1923 static int unittest_i2c_bus_probe(struct platform_device
*pdev
)
1925 struct device
*dev
= &pdev
->dev
;
1926 struct device_node
*np
= dev
->of_node
;
1927 struct unittest_i2c_bus_data
*std
;
1928 struct i2c_adapter
*adap
;
1932 dev_err(dev
, "No OF data for device\n");
1937 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1939 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
1943 /* link them together */
1945 platform_set_drvdata(pdev
, std
);
1948 i2c_set_adapdata(adap
, std
);
1950 strlcpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
1951 adap
->class = I2C_CLASS_DEPRECATED
;
1952 adap
->algo
= &unittest_i2c_algo
;
1953 adap
->dev
.parent
= dev
;
1954 adap
->dev
.of_node
= dev
->of_node
;
1955 adap
->timeout
= 5 * HZ
;
1958 ret
= i2c_add_numbered_adapter(adap
);
1960 dev_err(dev
, "Failed to add I2C adapter\n");
1967 static int unittest_i2c_bus_remove(struct platform_device
*pdev
)
1969 struct device
*dev
= &pdev
->dev
;
1970 struct device_node
*np
= dev
->of_node
;
1971 struct unittest_i2c_bus_data
*std
= platform_get_drvdata(pdev
);
1973 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1974 i2c_del_adapter(&std
->adap
);
1979 static const struct of_device_id unittest_i2c_bus_match
[] = {
1980 { .compatible
= "unittest-i2c-bus", },
1984 static struct platform_driver unittest_i2c_bus_driver
= {
1985 .probe
= unittest_i2c_bus_probe
,
1986 .remove
= unittest_i2c_bus_remove
,
1988 .name
= "unittest-i2c-bus",
1989 .of_match_table
= of_match_ptr(unittest_i2c_bus_match
),
1993 static int unittest_i2c_dev_probe(struct i2c_client
*client
,
1994 const struct i2c_device_id
*id
)
1996 struct device
*dev
= &client
->dev
;
1997 struct device_node
*np
= client
->dev
.of_node
;
2000 dev_err(dev
, "No OF node\n");
2004 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2009 static int unittest_i2c_dev_remove(struct i2c_client
*client
)
2011 struct device
*dev
= &client
->dev
;
2012 struct device_node
*np
= client
->dev
.of_node
;
2014 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2018 static const struct i2c_device_id unittest_i2c_dev_id
[] = {
2019 { .name
= "unittest-i2c-dev" },
2023 static struct i2c_driver unittest_i2c_dev_driver
= {
2025 .name
= "unittest-i2c-dev",
2027 .probe
= unittest_i2c_dev_probe
,
2028 .remove
= unittest_i2c_dev_remove
,
2029 .id_table
= unittest_i2c_dev_id
,
2032 #if IS_BUILTIN(CONFIG_I2C_MUX)
2034 static int unittest_i2c_mux_select_chan(struct i2c_mux_core
*muxc
, u32 chan
)
2039 static int unittest_i2c_mux_probe(struct i2c_client
*client
,
2040 const struct i2c_device_id
*id
)
2043 struct device
*dev
= &client
->dev
;
2044 struct i2c_adapter
*adap
= client
->adapter
;
2045 struct device_node
*np
= client
->dev
.of_node
, *child
;
2046 struct i2c_mux_core
*muxc
;
2049 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2052 dev_err(dev
, "No OF node\n");
2057 for_each_child_of_node(np
, child
) {
2058 if (of_property_read_u32(child
, "reg", ®
))
2060 if (max_reg
== (u32
)-1 || reg
> max_reg
)
2063 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
2065 dev_err(dev
, "No channels\n");
2069 muxc
= i2c_mux_alloc(adap
, dev
, nchans
, 0, 0,
2070 unittest_i2c_mux_select_chan
, NULL
);
2073 for (i
= 0; i
< nchans
; i
++) {
2074 if (i2c_mux_add_adapter(muxc
, 0, i
, 0)) {
2075 dev_err(dev
, "Failed to register mux #%d\n", i
);
2076 i2c_mux_del_adapters(muxc
);
2081 i2c_set_clientdata(client
, muxc
);
2086 static int unittest_i2c_mux_remove(struct i2c_client
*client
)
2088 struct device
*dev
= &client
->dev
;
2089 struct device_node
*np
= client
->dev
.of_node
;
2090 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
2092 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2093 i2c_mux_del_adapters(muxc
);
2097 static const struct i2c_device_id unittest_i2c_mux_id
[] = {
2098 { .name
= "unittest-i2c-mux" },
2102 static struct i2c_driver unittest_i2c_mux_driver
= {
2104 .name
= "unittest-i2c-mux",
2106 .probe
= unittest_i2c_mux_probe
,
2107 .remove
= unittest_i2c_mux_remove
,
2108 .id_table
= unittest_i2c_mux_id
,
2113 static int of_unittest_overlay_i2c_init(void)
2117 ret
= i2c_add_driver(&unittest_i2c_dev_driver
);
2118 if (unittest(ret
== 0,
2119 "could not register unittest i2c device driver\n"))
2122 ret
= platform_driver_register(&unittest_i2c_bus_driver
);
2123 if (unittest(ret
== 0,
2124 "could not register unittest i2c bus driver\n"))
2127 #if IS_BUILTIN(CONFIG_I2C_MUX)
2128 ret
= i2c_add_driver(&unittest_i2c_mux_driver
);
2129 if (unittest(ret
== 0,
2130 "could not register unittest i2c mux driver\n"))
2137 static void of_unittest_overlay_i2c_cleanup(void)
2139 #if IS_BUILTIN(CONFIG_I2C_MUX)
2140 i2c_del_driver(&unittest_i2c_mux_driver
);
2142 platform_driver_unregister(&unittest_i2c_bus_driver
);
2143 i2c_del_driver(&unittest_i2c_dev_driver
);
2146 static void __init
of_unittest_overlay_i2c_12(void)
2148 /* device should enable */
2149 if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
))
2152 unittest(1, "overlay test %d passed\n", 12);
2155 /* test deactivation of device */
2156 static void __init
of_unittest_overlay_i2c_13(void)
2158 /* device should disable */
2159 if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
))
2162 unittest(1, "overlay test %d passed\n", 13);
2165 /* just check for i2c mux existence */
2166 static void of_unittest_overlay_i2c_14(void)
2170 static void __init
of_unittest_overlay_i2c_15(void)
2172 /* device should enable */
2173 if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY
))
2176 unittest(1, "overlay test %d passed\n", 15);
2181 static inline void of_unittest_overlay_i2c_14(void) { }
2182 static inline void of_unittest_overlay_i2c_15(void) { }
2186 static void __init
of_unittest_overlay(void)
2188 struct device_node
*bus_np
= NULL
;
2190 if (platform_driver_register(&unittest_driver
)) {
2191 unittest(0, "could not register unittest driver\n");
2195 bus_np
= of_find_node_by_path(bus_path
);
2196 if (bus_np
== NULL
) {
2197 unittest(0, "could not find bus_path \"%s\"\n", bus_path
);
2201 if (of_platform_default_populate(bus_np
, NULL
, NULL
)) {
2202 unittest(0, "could not populate bus @ \"%s\"\n", bus_path
);
2206 if (!of_unittest_device_exists(100, PDEV_OVERLAY
)) {
2207 unittest(0, "could not find unittest0 @ \"%s\"\n",
2208 unittest_path(100, PDEV_OVERLAY
));
2212 if (of_unittest_device_exists(101, PDEV_OVERLAY
)) {
2213 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2214 unittest_path(101, PDEV_OVERLAY
));
2218 unittest(1, "basic infrastructure of overlays passed");
2220 /* tests in sequence */
2221 of_unittest_overlay_0();
2222 of_unittest_overlay_1();
2223 of_unittest_overlay_2();
2224 of_unittest_overlay_3();
2225 of_unittest_overlay_4();
2226 of_unittest_overlay_5();
2227 of_unittest_overlay_6();
2228 of_unittest_overlay_8();
2230 of_unittest_overlay_10();
2231 of_unittest_overlay_11();
2233 #if IS_BUILTIN(CONFIG_I2C)
2234 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
2237 of_unittest_overlay_i2c_12();
2238 of_unittest_overlay_i2c_13();
2239 of_unittest_overlay_i2c_14();
2240 of_unittest_overlay_i2c_15();
2242 of_unittest_overlay_i2c_cleanup();
2245 of_unittest_destroy_tracked_overlays();
2248 of_node_put(bus_np
);
2252 static inline void __init
of_unittest_overlay(void) { }
2255 #ifdef CONFIG_OF_OVERLAY
2258 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2259 * in scripts/Makefile.lib
2262 #define OVERLAY_INFO_EXTERN(name) \
2263 extern uint8_t __dtb_##name##_begin[]; \
2264 extern uint8_t __dtb_##name##_end[]
2266 #define OVERLAY_INFO(overlay_name, expected) \
2267 { .dtb_begin = __dtb_##overlay_name##_begin, \
2268 .dtb_end = __dtb_##overlay_name##_end, \
2269 .expected_result = expected, \
2270 .name = #overlay_name, \
2273 struct overlay_info
{
2276 int expected_result
;
2281 OVERLAY_INFO_EXTERN(overlay_base
);
2282 OVERLAY_INFO_EXTERN(overlay
);
2283 OVERLAY_INFO_EXTERN(overlay_0
);
2284 OVERLAY_INFO_EXTERN(overlay_1
);
2285 OVERLAY_INFO_EXTERN(overlay_2
);
2286 OVERLAY_INFO_EXTERN(overlay_3
);
2287 OVERLAY_INFO_EXTERN(overlay_4
);
2288 OVERLAY_INFO_EXTERN(overlay_5
);
2289 OVERLAY_INFO_EXTERN(overlay_6
);
2290 OVERLAY_INFO_EXTERN(overlay_7
);
2291 OVERLAY_INFO_EXTERN(overlay_8
);
2292 OVERLAY_INFO_EXTERN(overlay_9
);
2293 OVERLAY_INFO_EXTERN(overlay_10
);
2294 OVERLAY_INFO_EXTERN(overlay_11
);
2295 OVERLAY_INFO_EXTERN(overlay_12
);
2296 OVERLAY_INFO_EXTERN(overlay_13
);
2297 OVERLAY_INFO_EXTERN(overlay_15
);
2298 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node
);
2299 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop
);
2300 OVERLAY_INFO_EXTERN(overlay_bad_phandle
);
2301 OVERLAY_INFO_EXTERN(overlay_bad_symbol
);
2303 /* entries found by name */
2304 static struct overlay_info overlays
[] = {
2305 OVERLAY_INFO(overlay_base
, -9999),
2306 OVERLAY_INFO(overlay
, 0),
2307 OVERLAY_INFO(overlay_0
, 0),
2308 OVERLAY_INFO(overlay_1
, 0),
2309 OVERLAY_INFO(overlay_2
, 0),
2310 OVERLAY_INFO(overlay_3
, 0),
2311 OVERLAY_INFO(overlay_4
, 0),
2312 OVERLAY_INFO(overlay_5
, 0),
2313 OVERLAY_INFO(overlay_6
, 0),
2314 OVERLAY_INFO(overlay_7
, 0),
2315 OVERLAY_INFO(overlay_8
, 0),
2316 OVERLAY_INFO(overlay_9
, 0),
2317 OVERLAY_INFO(overlay_10
, 0),
2318 OVERLAY_INFO(overlay_11
, 0),
2319 OVERLAY_INFO(overlay_12
, 0),
2320 OVERLAY_INFO(overlay_13
, 0),
2321 OVERLAY_INFO(overlay_15
, 0),
2322 OVERLAY_INFO(overlay_bad_add_dup_node
, -EINVAL
),
2323 OVERLAY_INFO(overlay_bad_add_dup_prop
, -EINVAL
),
2324 OVERLAY_INFO(overlay_bad_phandle
, -EINVAL
),
2325 OVERLAY_INFO(overlay_bad_symbol
, -EINVAL
),
2327 {.dtb_begin
= NULL
, .dtb_end
= NULL
, .expected_result
= 0, .name
= NULL
}
2330 static struct device_node
*overlay_base_root
;
2332 static void * __init
dt_alloc_memory(u64 size
, u64 align
)
2334 void *ptr
= memblock_alloc(size
, align
);
2337 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2338 __func__
, size
, align
);
2344 * Create base device tree for the overlay unittest.
2346 * This is called from very early boot code.
2348 * Do as much as possible the same way as done in __unflatten_device_tree
2349 * and other early boot steps for the normal FDT so that the overlay base
2350 * unflattened tree will have the same characteristics as the real tree
2351 * (such as having memory allocated by the early allocator). The goal
2352 * is to test "the real thing" as much as possible, and test "test setup
2353 * code" as little as possible.
2355 * Have to stop before resolving phandles, because that uses kmalloc.
2357 void __init
unittest_unflatten_overlay_base(void)
2359 struct overlay_info
*info
;
2364 const char *overlay_name
= "overlay_base";
2366 for (info
= overlays
; info
&& info
->name
; info
++) {
2367 if (!strcmp(overlay_name
, info
->name
)) {
2373 pr_err("no overlay data for %s\n", overlay_name
);
2377 info
= &overlays
[0];
2379 if (info
->expected_result
!= -9999) {
2380 pr_err("No dtb 'overlay_base' to attach\n");
2384 data_size
= info
->dtb_end
- info
->dtb_begin
;
2386 pr_err("No dtb 'overlay_base' to attach\n");
2390 size
= fdt_totalsize(info
->dtb_begin
);
2391 if (size
!= data_size
) {
2392 pr_err("dtb 'overlay_base' header totalsize != actual size");
2396 new_fdt
= dt_alloc_memory(size
, roundup_pow_of_two(FDT_V17_SIZE
));
2398 pr_err("alloc for dtb 'overlay_base' failed");
2402 memcpy(new_fdt
, info
->dtb_begin
, size
);
2404 __unflatten_device_tree(new_fdt
, NULL
, &overlay_base_root
,
2405 dt_alloc_memory
, true);
2409 * The purpose of of_unittest_overlay_data_add is to add an
2410 * overlay in the normal fashion. This is a test of the whole
2411 * picture, instead of testing individual elements.
2413 * A secondary purpose is to be able to verify that the contents of
2414 * /proc/device-tree/ contains the updated structure and values from
2415 * the overlay. That must be verified separately in user space.
2417 * Return 0 on unexpected error.
2419 static int __init
overlay_data_apply(const char *overlay_name
, int *overlay_id
)
2421 struct overlay_info
*info
;
2426 for (info
= overlays
; info
&& info
->name
; info
++) {
2427 if (!strcmp(overlay_name
, info
->name
)) {
2433 pr_err("no overlay data for %s\n", overlay_name
);
2437 size
= info
->dtb_end
- info
->dtb_begin
;
2439 pr_err("no overlay data for %s\n", overlay_name
);
2441 ret
= of_overlay_fdt_apply(info
->dtb_begin
, size
, &info
->overlay_id
);
2443 *overlay_id
= info
->overlay_id
;
2447 pr_debug("%s applied\n", overlay_name
);
2450 if (ret
!= info
->expected_result
)
2451 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2452 info
->expected_result
, ret
, overlay_name
);
2454 return (ret
== info
->expected_result
);
2458 * The purpose of of_unittest_overlay_high_level is to add an overlay
2459 * in the normal fashion. This is a test of the whole picture,
2460 * instead of individual elements.
2462 * The first part of the function is _not_ normal overlay usage; it is
2463 * finishing splicing the base overlay device tree into the live tree.
2465 static __init
void of_unittest_overlay_high_level(void)
2467 struct device_node
*last_sibling
;
2468 struct device_node
*np
;
2469 struct device_node
*of_symbols
;
2470 struct device_node
*overlay_base_symbols
;
2471 struct device_node
**pprev
;
2472 struct property
*prop
;
2474 if (!overlay_base_root
) {
2475 unittest(0, "overlay_base_root not initialized\n");
2480 * Could not fixup phandles in unittest_unflatten_overlay_base()
2481 * because kmalloc() was not yet available.
2483 of_overlay_mutex_lock();
2484 of_resolve_phandles(overlay_base_root
);
2485 of_overlay_mutex_unlock();
2489 * do not allow overlay_base to duplicate any node already in
2490 * tree, this greatly simplifies the code
2494 * remove overlay_base_root node "__local_fixups", after
2495 * being used by of_resolve_phandles()
2497 pprev
= &overlay_base_root
->child
;
2498 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
) {
2499 if (of_node_name_eq(np
, "__local_fixups__")) {
2500 *pprev
= np
->sibling
;
2503 pprev
= &np
->sibling
;
2506 /* remove overlay_base_root node "__symbols__" if in live tree */
2507 of_symbols
= of_get_child_by_name(of_root
, "__symbols__");
2509 /* will have to graft properties from node into live tree */
2510 pprev
= &overlay_base_root
->child
;
2511 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
) {
2512 if (of_node_name_eq(np
, "__symbols__")) {
2513 overlay_base_symbols
= np
;
2514 *pprev
= np
->sibling
;
2517 pprev
= &np
->sibling
;
2521 for_each_child_of_node(overlay_base_root
, np
) {
2522 struct device_node
*base_child
;
2523 for_each_child_of_node(of_root
, base_child
) {
2524 if (!strcmp(np
->full_name
, base_child
->full_name
)) {
2525 unittest(0, "illegal node name in overlay_base %pOFn",
2533 * overlay 'overlay_base' is not allowed to have root
2534 * properties, so only need to splice nodes into main device tree.
2536 * root node of *overlay_base_root will not be freed, it is lost
2540 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
)
2541 np
->parent
= of_root
;
2543 mutex_lock(&of_mutex
);
2545 for (last_sibling
= np
= of_root
->child
; np
; np
= np
->sibling
)
2549 last_sibling
->sibling
= overlay_base_root
->child
;
2551 of_root
->child
= overlay_base_root
->child
;
2553 for_each_of_allnodes_from(overlay_base_root
, np
)
2554 __of_attach_node_sysfs(np
);
2557 struct property
*new_prop
;
2558 for_each_property_of_node(overlay_base_symbols
, prop
) {
2560 new_prop
= __of_prop_dup(prop
, GFP_KERNEL
);
2562 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
2566 if (__of_add_property(of_symbols
, new_prop
)) {
2567 /* "name" auto-generated by unflatten */
2568 if (!strcmp(new_prop
->name
, "name"))
2570 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2574 if (__of_add_property_sysfs(of_symbols
, new_prop
)) {
2575 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2582 mutex_unlock(&of_mutex
);
2585 /* now do the normal overlay usage test */
2587 unittest(overlay_data_apply("overlay", NULL
),
2588 "Adding overlay 'overlay' failed\n");
2590 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL
),
2591 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
2593 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL
),
2594 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
2596 unittest(overlay_data_apply("overlay_bad_phandle", NULL
),
2597 "Adding overlay 'overlay_bad_phandle' failed\n");
2599 unittest(overlay_data_apply("overlay_bad_symbol", NULL
),
2600 "Adding overlay 'overlay_bad_symbol' failed\n");
2605 mutex_unlock(&of_mutex
);
2610 static inline __init
void of_unittest_overlay_high_level(void) {}
2614 static int __init
of_unittest(void)
2616 struct device_node
*np
;
2619 /* adding data for unittest */
2621 if (IS_ENABLED(CONFIG_UML
))
2622 unittest_unflatten_overlay_base();
2624 res
= unittest_data_add();
2628 of_aliases
= of_find_node_by_path("/aliases");
2630 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2632 pr_info("No testcase data in device tree; not running tests\n");
2637 pr_info("start of unittest - you will see error messages\n");
2638 of_unittest_check_tree_linkage();
2639 of_unittest_check_phandles();
2640 of_unittest_find_node_by_name();
2641 of_unittest_dynamic();
2642 of_unittest_parse_phandle_with_args();
2643 of_unittest_parse_phandle_with_args_map();
2644 of_unittest_printf();
2645 of_unittest_property_string();
2646 of_unittest_property_copy();
2647 of_unittest_changeset();
2648 of_unittest_parse_interrupts();
2649 of_unittest_parse_interrupts_extended();
2650 of_unittest_parse_dma_ranges();
2651 of_unittest_pci_dma_ranges();
2652 of_unittest_match_node();
2653 of_unittest_platform_populate();
2654 of_unittest_overlay();
2656 /* Double check linkage after removing testcase data */
2657 of_unittest_check_tree_linkage();
2659 of_unittest_overlay_high_level();
2661 pr_info("end of unittest - %i passed, %i failed\n",
2662 unittest_results
.passed
, unittest_results
.failed
);
2666 late_initcall(of_unittest
);