1 // SPDX-License-Identifier: GPL-2.0
3 * Self tests for device tree subsystem
6 #define pr_fmt(fmt) "### dt-test ### " fmt
8 #include <linux/memblock.h>
10 #include <linux/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/hashtable.h>
14 #include <linux/libfdt.h>
16 #include <linux/of_address.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-mux.h>
28 #include <linux/gpio/driver.h>
30 #include <linux/bitops.h>
32 #include "of_private.h"
34 static struct unittest_results
{
39 #define unittest(result, fmt, ...) ({ \
40 bool failed = !(result); \
42 unittest_results.failed++; \
43 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
45 unittest_results.passed++; \
46 pr_debug("pass %s():%i\n", __func__, __LINE__); \
52 * Expected message may have a message level other than KERN_INFO.
53 * Print the expected message only if the current loglevel will allow
54 * the actual message to print.
56 * Do not use EXPECT_BEGIN() or EXPECT_END() for messages generated by
59 #define EXPECT_BEGIN(level, fmt, ...) \
60 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
62 #define EXPECT_END(level, fmt, ...) \
63 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
65 static void __init
of_unittest_find_node_by_name(void)
67 struct device_node
*np
;
68 const char *options
, *name
;
70 np
= of_find_node_by_path("/testcase-data");
71 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
72 unittest(np
&& !strcmp("/testcase-data", name
),
73 "find /testcase-data failed\n");
77 /* Test if trailing '/' works */
78 np
= of_find_node_by_path("/testcase-data/");
79 unittest(!np
, "trailing '/' on /testcase-data/ should fail\n");
81 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
82 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
83 unittest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
),
84 "find /testcase-data/phandle-tests/consumer-a failed\n");
88 np
= of_find_node_by_path("testcase-alias");
89 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
90 unittest(np
&& !strcmp("/testcase-data", name
),
91 "find testcase-alias failed\n");
95 /* Test if trailing '/' works on aliases */
96 np
= of_find_node_by_path("testcase-alias/");
97 unittest(!np
, "trailing '/' on testcase-alias/ should fail\n");
99 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
100 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
101 unittest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
),
102 "find testcase-alias/phandle-tests/consumer-a failed\n");
106 np
= of_find_node_by_path("/testcase-data/missing-path");
107 unittest(!np
, "non-existent path returned node %pOF\n", np
);
110 np
= of_find_node_by_path("missing-alias");
111 unittest(!np
, "non-existent alias returned node %pOF\n", np
);
114 np
= of_find_node_by_path("testcase-alias/missing-path");
115 unittest(!np
, "non-existent alias with relative path returned node %pOF\n", np
);
118 np
= of_find_node_opts_by_path("/testcase-data:testoption", &options
);
119 unittest(np
&& !strcmp("testoption", options
),
120 "option path test failed\n");
123 np
= of_find_node_opts_by_path("/testcase-data:test/option", &options
);
124 unittest(np
&& !strcmp("test/option", options
),
125 "option path test, subcase #1 failed\n");
128 np
= of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options
);
129 unittest(np
&& !strcmp("test/option", options
),
130 "option path test, subcase #2 failed\n");
133 np
= of_find_node_opts_by_path("/testcase-data:testoption", NULL
);
134 unittest(np
, "NULL option path test failed\n");
137 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption",
139 unittest(np
&& !strcmp("testaliasoption", options
),
140 "option alias path test failed\n");
143 np
= of_find_node_opts_by_path("testcase-alias:test/alias/option",
145 unittest(np
&& !strcmp("test/alias/option", options
),
146 "option alias path test, subcase #1 failed\n");
149 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL
);
150 unittest(np
, "NULL option alias path test failed\n");
153 options
= "testoption";
154 np
= of_find_node_opts_by_path("testcase-alias", &options
);
155 unittest(np
&& !options
, "option clearing test failed\n");
158 options
= "testoption";
159 np
= of_find_node_opts_by_path("/", &options
);
160 unittest(np
&& !options
, "option clearing root node test failed\n");
164 static void __init
of_unittest_dynamic(void)
166 struct device_node
*np
;
167 struct property
*prop
;
169 np
= of_find_node_by_path("/testcase-data");
171 pr_err("missing testcase data\n");
175 /* Array of 4 properties for the purpose of testing */
176 prop
= kcalloc(4, sizeof(*prop
), GFP_KERNEL
);
178 unittest(0, "kzalloc() failed\n");
182 /* Add a new property - should pass*/
183 prop
->name
= "new-property";
184 prop
->value
= "new-property-data";
185 prop
->length
= strlen(prop
->value
) + 1;
186 unittest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
188 /* Try to add an existing property - should fail */
190 prop
->name
= "new-property";
191 prop
->value
= "new-property-data-should-fail";
192 prop
->length
= strlen(prop
->value
) + 1;
193 unittest(of_add_property(np
, prop
) != 0,
194 "Adding an existing property should have failed\n");
196 /* Try to modify an existing property - should pass */
197 prop
->value
= "modify-property-data-should-pass";
198 prop
->length
= strlen(prop
->value
) + 1;
199 unittest(of_update_property(np
, prop
) == 0,
200 "Updating an existing property should have passed\n");
202 /* Try to modify non-existent property - should pass*/
204 prop
->name
= "modify-property";
205 prop
->value
= "modify-missing-property-data-should-pass";
206 prop
->length
= strlen(prop
->value
) + 1;
207 unittest(of_update_property(np
, prop
) == 0,
208 "Updating a missing property should have passed\n");
210 /* Remove property - should pass */
211 unittest(of_remove_property(np
, prop
) == 0,
212 "Removing a property should have passed\n");
214 /* Adding very large property - should pass */
216 prop
->name
= "large-property-PAGE_SIZEx8";
217 prop
->length
= PAGE_SIZE
* 8;
218 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
219 unittest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
221 unittest(of_add_property(np
, prop
) == 0,
222 "Adding a large property should have passed\n");
225 static int __init
of_unittest_check_node_linkage(struct device_node
*np
)
227 struct device_node
*child
;
230 for_each_child_of_node(np
, child
) {
231 if (child
->parent
!= np
) {
232 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
238 rc
= of_unittest_check_node_linkage(child
);
250 static void __init
of_unittest_check_tree_linkage(void)
252 struct device_node
*np
;
253 int allnode_count
= 0, child_count
;
258 for_each_of_allnodes(np
)
260 child_count
= of_unittest_check_node_linkage(of_root
);
262 unittest(child_count
> 0, "Device node data structure is corrupted\n");
263 unittest(child_count
== allnode_count
,
264 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
265 allnode_count
, child_count
);
266 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
269 static void __init
of_unittest_printf_one(struct device_node
*np
, const char *fmt
,
270 const char *expected
)
276 buf_size
= strlen(expected
) + 10;
277 buf
= kmalloc(buf_size
, GFP_KERNEL
);
281 /* Baseline; check conversion with a large size limit */
282 memset(buf
, 0xff, buf_size
);
283 size
= snprintf(buf
, buf_size
- 2, fmt
, np
);
285 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
286 unittest((strcmp(buf
, expected
) == 0) && (buf
[size
+1] == 0xff),
287 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
290 /* Make sure length limits work */
292 for (i
= 0; i
< 2; i
++, size
--) {
293 /* Clear the buffer, and make sure it works correctly still */
294 memset(buf
, 0xff, buf_size
);
295 snprintf(buf
, size
+1, fmt
, np
);
296 unittest(strncmp(buf
, expected
, size
) == 0 && (buf
[size
+1] == 0xff),
297 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
298 size
, fmt
, expected
, buf
);
303 static void __init
of_unittest_printf(void)
305 struct device_node
*np
;
306 const char *full_name
= "/testcase-data/platform-tests/test-device@1/dev@100";
307 char phandle_str
[16] = "";
309 np
= of_find_node_by_path(full_name
);
311 unittest(np
, "testcase data missing\n");
315 num_to_str(phandle_str
, sizeof(phandle_str
), np
->phandle
, 0);
317 of_unittest_printf_one(np
, "%pOF", full_name
);
318 of_unittest_printf_one(np
, "%pOFf", full_name
);
319 of_unittest_printf_one(np
, "%pOFn", "dev");
320 of_unittest_printf_one(np
, "%2pOFn", "dev");
321 of_unittest_printf_one(np
, "%5pOFn", " dev");
322 of_unittest_printf_one(np
, "%pOFnc", "dev:test-sub-device");
323 of_unittest_printf_one(np
, "%pOFp", phandle_str
);
324 of_unittest_printf_one(np
, "%pOFP", "dev@100");
325 of_unittest_printf_one(np
, "ABC %pOFP ABC", "ABC dev@100 ABC");
326 of_unittest_printf_one(np
, "%10pOFP", " dev@100");
327 of_unittest_printf_one(np
, "%-10pOFP", "dev@100 ");
328 of_unittest_printf_one(of_root
, "%pOFP", "/");
329 of_unittest_printf_one(np
, "%pOFF", "----");
330 of_unittest_printf_one(np
, "%pOFPF", "dev@100:----");
331 of_unittest_printf_one(np
, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
332 of_unittest_printf_one(np
, "%pOFc", "test-sub-device");
333 of_unittest_printf_one(np
, "%pOFC",
334 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
338 struct hlist_node node
;
339 struct device_node
*np
;
342 static DEFINE_HASHTABLE(phandle_ht
, 8);
343 static void __init
of_unittest_check_phandles(void)
345 struct device_node
*np
;
346 struct node_hash
*nh
;
347 struct hlist_node
*tmp
;
348 int i
, dup_count
= 0, phandle_count
= 0;
350 for_each_of_allnodes(np
) {
354 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
355 if (nh
->np
->phandle
== np
->phandle
) {
356 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
357 np
->phandle
, nh
->np
, np
);
363 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
368 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
371 unittest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
372 dup_count
, phandle_count
);
375 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
381 static void __init
of_unittest_parse_phandle_with_args(void)
383 struct device_node
*np
;
384 struct of_phandle_args args
;
387 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
389 pr_err("missing testcase data\n");
393 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
394 unittest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
396 for (i
= 0; i
< 8; i
++) {
399 memset(&args
, 0, sizeof(args
));
400 rc
= of_parse_phandle_with_args(np
, "phandle-list",
401 "#phandle-cells", i
, &args
);
403 /* Test the values from tests-phandle.dtsi */
407 passed
&= (args
.args_count
== 1);
408 passed
&= (args
.args
[0] == (i
+ 1));
412 passed
&= (args
.args_count
== 2);
413 passed
&= (args
.args
[0] == (i
+ 1));
414 passed
&= (args
.args
[1] == 0);
417 passed
&= (rc
== -ENOENT
);
421 passed
&= (args
.args_count
== 3);
422 passed
&= (args
.args
[0] == (i
+ 1));
423 passed
&= (args
.args
[1] == 4);
424 passed
&= (args
.args
[2] == 3);
428 passed
&= (args
.args_count
== 2);
429 passed
&= (args
.args
[0] == (i
+ 1));
430 passed
&= (args
.args
[1] == 100);
434 passed
&= (args
.args_count
== 0);
438 passed
&= (args
.args_count
== 1);
439 passed
&= (args
.args
[0] == (i
+ 1));
442 passed
&= (rc
== -ENOENT
);
448 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
452 /* Check for missing list property */
453 memset(&args
, 0, sizeof(args
));
454 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
455 "#phandle-cells", 0, &args
);
456 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
457 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
459 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
461 /* Check for missing cells property */
462 memset(&args
, 0, sizeof(args
));
464 EXPECT_BEGIN(KERN_INFO
,
465 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
467 rc
= of_parse_phandle_with_args(np
, "phandle-list",
468 "#phandle-cells-missing", 0, &args
);
470 EXPECT_END(KERN_INFO
,
471 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
473 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
475 EXPECT_BEGIN(KERN_INFO
,
476 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
478 rc
= of_count_phandle_with_args(np
, "phandle-list",
479 "#phandle-cells-missing");
481 EXPECT_END(KERN_INFO
,
482 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
484 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
486 /* Check for bad phandle in list */
487 memset(&args
, 0, sizeof(args
));
489 EXPECT_BEGIN(KERN_INFO
,
490 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
492 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
493 "#phandle-cells", 0, &args
);
495 EXPECT_END(KERN_INFO
,
496 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
498 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
500 EXPECT_BEGIN(KERN_INFO
,
501 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
503 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
506 EXPECT_END(KERN_INFO
,
507 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
509 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
511 /* Check for incorrectly formed argument list */
512 memset(&args
, 0, sizeof(args
));
514 EXPECT_BEGIN(KERN_INFO
,
515 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
517 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
518 "#phandle-cells", 1, &args
);
520 EXPECT_END(KERN_INFO
,
521 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
523 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
525 EXPECT_BEGIN(KERN_INFO
,
526 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
528 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
531 EXPECT_END(KERN_INFO
,
532 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
534 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
537 static void __init
of_unittest_parse_phandle_with_args_map(void)
539 struct device_node
*np
, *p0
, *p1
, *p2
, *p3
;
540 struct of_phandle_args args
;
543 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
545 pr_err("missing testcase data\n");
549 p0
= of_find_node_by_path("/testcase-data/phandle-tests/provider0");
551 pr_err("missing testcase data\n");
555 p1
= of_find_node_by_path("/testcase-data/phandle-tests/provider1");
557 pr_err("missing testcase data\n");
561 p2
= of_find_node_by_path("/testcase-data/phandle-tests/provider2");
563 pr_err("missing testcase data\n");
567 p3
= of_find_node_by_path("/testcase-data/phandle-tests/provider3");
569 pr_err("missing testcase data\n");
573 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
574 unittest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
576 for (i
= 0; i
< 8; i
++) {
579 memset(&args
, 0, sizeof(args
));
580 rc
= of_parse_phandle_with_args_map(np
, "phandle-list",
581 "phandle", i
, &args
);
583 /* Test the values from tests-phandle.dtsi */
587 passed
&= (args
.np
== p1
);
588 passed
&= (args
.args_count
== 1);
589 passed
&= (args
.args
[0] == 1);
593 passed
&= (args
.np
== p3
);
594 passed
&= (args
.args_count
== 3);
595 passed
&= (args
.args
[0] == 2);
596 passed
&= (args
.args
[1] == 5);
597 passed
&= (args
.args
[2] == 3);
600 passed
&= (rc
== -ENOENT
);
604 passed
&= (args
.np
== p0
);
605 passed
&= (args
.args_count
== 0);
609 passed
&= (args
.np
== p1
);
610 passed
&= (args
.args_count
== 1);
611 passed
&= (args
.args
[0] == 3);
615 passed
&= (args
.np
== p0
);
616 passed
&= (args
.args_count
== 0);
620 passed
&= (args
.np
== p2
);
621 passed
&= (args
.args_count
== 2);
622 passed
&= (args
.args
[0] == 15);
623 passed
&= (args
.args
[1] == 0x20);
626 passed
&= (rc
== -ENOENT
);
632 unittest(passed
, "index %i - data error on node %s rc=%i\n",
633 i
, args
.np
->full_name
, rc
);
636 /* Check for missing list property */
637 memset(&args
, 0, sizeof(args
));
638 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-missing",
639 "phandle", 0, &args
);
640 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
642 /* Check for missing cells,map,mask property */
643 memset(&args
, 0, sizeof(args
));
645 EXPECT_BEGIN(KERN_INFO
,
646 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
648 rc
= of_parse_phandle_with_args_map(np
, "phandle-list",
649 "phandle-missing", 0, &args
);
650 EXPECT_END(KERN_INFO
,
651 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
653 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
655 /* Check for bad phandle in list */
656 memset(&args
, 0, sizeof(args
));
658 EXPECT_BEGIN(KERN_INFO
,
659 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
661 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-bad-phandle",
662 "phandle", 0, &args
);
663 EXPECT_END(KERN_INFO
,
664 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
666 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
668 /* Check for incorrectly formed argument list */
669 memset(&args
, 0, sizeof(args
));
671 EXPECT_BEGIN(KERN_INFO
,
672 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
674 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-bad-args",
675 "phandle", 1, &args
);
676 EXPECT_END(KERN_INFO
,
677 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
679 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
682 static void __init
of_unittest_property_string(void)
684 const char *strings
[4];
685 struct device_node
*np
;
688 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
690 pr_err("No testcase data in device tree\n");
694 rc
= of_property_match_string(np
, "phandle-list-names", "first");
695 unittest(rc
== 0, "first expected:0 got:%i\n", rc
);
696 rc
= of_property_match_string(np
, "phandle-list-names", "second");
697 unittest(rc
== 1, "second expected:1 got:%i\n", rc
);
698 rc
= of_property_match_string(np
, "phandle-list-names", "third");
699 unittest(rc
== 2, "third expected:2 got:%i\n", rc
);
700 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
701 unittest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
702 rc
= of_property_match_string(np
, "missing-property", "blah");
703 unittest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
704 rc
= of_property_match_string(np
, "empty-property", "blah");
705 unittest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
706 rc
= of_property_match_string(np
, "unterminated-string", "blah");
707 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
709 /* of_property_count_strings() tests */
710 rc
= of_property_count_strings(np
, "string-property");
711 unittest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
712 rc
= of_property_count_strings(np
, "phandle-list-names");
713 unittest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
714 rc
= of_property_count_strings(np
, "unterminated-string");
715 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
716 rc
= of_property_count_strings(np
, "unterminated-string-list");
717 unittest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
719 /* of_property_read_string_index() tests */
720 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
721 unittest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
723 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
724 unittest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
725 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
726 unittest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
727 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
728 unittest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
729 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
730 unittest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
732 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
733 unittest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
735 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
736 unittest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
737 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
738 unittest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
740 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
741 unittest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
744 /* of_property_read_string_array() tests */
745 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
746 unittest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
747 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
748 unittest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
749 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
750 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
751 /* -- An incorrectly formed string should cause a failure */
752 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
753 unittest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
754 /* -- parsing the correctly formed strings should still work: */
756 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
757 unittest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
759 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
760 unittest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
763 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
764 (p1)->value && (p2)->value && \
765 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
766 !strcmp((p1)->name, (p2)->name))
767 static void __init
of_unittest_property_copy(void)
769 #ifdef CONFIG_OF_DYNAMIC
770 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
771 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
772 struct property
*new;
774 new = __of_prop_dup(&p1
, GFP_KERNEL
);
775 unittest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
780 new = __of_prop_dup(&p2
, GFP_KERNEL
);
781 unittest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
788 static void __init
of_unittest_changeset(void)
790 #ifdef CONFIG_OF_DYNAMIC
791 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 1, .value
= "" };
792 struct property
*ppname_n1
, pname_n1
= { .name
= "name", .length
= 3, .value
= "n1" };
793 struct property
*ppname_n2
, pname_n2
= { .name
= "name", .length
= 3, .value
= "n2" };
794 struct property
*ppname_n21
, pname_n21
= { .name
= "name", .length
= 3, .value
= "n21" };
795 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
796 struct property
*ppremove
;
797 struct device_node
*n1
, *n2
, *n21
, *nchangeset
, *nremove
, *parent
, *np
;
798 struct of_changeset chgset
;
800 n1
= __of_node_dup(NULL
, "n1");
801 unittest(n1
, "testcase setup failure\n");
803 n2
= __of_node_dup(NULL
, "n2");
804 unittest(n2
, "testcase setup failure\n");
806 n21
= __of_node_dup(NULL
, "n21");
807 unittest(n21
, "testcase setup failure %p\n", n21
);
809 nchangeset
= of_find_node_by_path("/testcase-data/changeset");
810 nremove
= of_get_child_by_name(nchangeset
, "node-remove");
811 unittest(nremove
, "testcase setup failure\n");
813 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
814 unittest(ppadd
, "testcase setup failure\n");
816 ppname_n1
= __of_prop_dup(&pname_n1
, GFP_KERNEL
);
817 unittest(ppname_n1
, "testcase setup failure\n");
819 ppname_n2
= __of_prop_dup(&pname_n2
, GFP_KERNEL
);
820 unittest(ppname_n2
, "testcase setup failure\n");
822 ppname_n21
= __of_prop_dup(&pname_n21
, GFP_KERNEL
);
823 unittest(ppname_n21
, "testcase setup failure\n");
825 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
826 unittest(ppupdate
, "testcase setup failure\n");
833 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
834 unittest(ppremove
, "failed to find removal prop");
836 of_changeset_init(&chgset
);
838 unittest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
839 unittest(!of_changeset_add_property(&chgset
, n1
, ppname_n1
), "fail add prop name\n");
841 unittest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
842 unittest(!of_changeset_add_property(&chgset
, n2
, ppname_n2
), "fail add prop name\n");
844 unittest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
845 unittest(!of_changeset_add_property(&chgset
, n21
, ppname_n21
), "fail add prop name\n");
847 unittest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
849 unittest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop prop-add\n");
850 unittest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
851 unittest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
853 unittest(!of_changeset_apply(&chgset
), "apply failed\n");
855 of_node_put(nchangeset
);
857 /* Make sure node names are constructed correctly */
858 unittest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
859 "'%pOF' not added\n", n21
);
862 unittest(!of_changeset_revert(&chgset
), "revert failed\n");
864 of_changeset_destroy(&chgset
);
872 static void __init
of_unittest_dma_get_max_cpu_address(void)
874 struct device_node
*np
;
875 phys_addr_t cpu_addr
;
877 if (!IS_ENABLED(CONFIG_OF_ADDRESS
))
880 np
= of_find_node_by_path("/testcase-data/address-tests");
882 pr_err("missing testcase data\n");
886 cpu_addr
= of_dma_get_max_cpu_address(np
);
887 unittest(cpu_addr
== 0x4fffffff,
888 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
889 &cpu_addr
, 0x4fffffff);
892 static void __init
of_unittest_dma_ranges_one(const char *path
,
893 u64 expect_dma_addr
, u64 expect_paddr
)
895 #ifdef CONFIG_HAS_DMA
896 struct device_node
*np
;
897 const struct bus_dma_region
*map
= NULL
;
900 np
= of_find_node_by_path(path
);
902 pr_err("missing testcase data\n");
906 rc
= of_dma_get_range(np
, &map
);
908 unittest(!rc
, "of_dma_get_range failed on node %pOF rc=%i\n", np
, rc
);
913 struct device dev_bogus
;
915 dev_bogus
.dma_range_map
= map
;
916 paddr
= dma_to_phys(&dev_bogus
, expect_dma_addr
);
917 dma_addr
= phys_to_dma(&dev_bogus
, expect_paddr
);
919 unittest(paddr
== expect_paddr
,
920 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
921 &paddr
, expect_paddr
, np
);
922 unittest(dma_addr
== expect_dma_addr
,
923 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
924 &dma_addr
, expect_dma_addr
, np
);
932 static void __init
of_unittest_parse_dma_ranges(void)
934 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
936 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
937 0x100000000, 0x20000000);
938 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
939 0x80000000, 0x20000000);
942 static void __init
of_unittest_pci_dma_ranges(void)
944 struct device_node
*np
;
945 struct of_pci_range range
;
946 struct of_pci_range_parser parser
;
949 if (!IS_ENABLED(CONFIG_PCI
))
952 np
= of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
954 pr_err("missing testcase data\n");
958 if (of_pci_dma_range_parser_init(&parser
, np
)) {
959 pr_err("missing dma-ranges property\n");
964 * Get the dma-ranges from the device tree
966 for_each_of_pci_range(&parser
, &range
) {
968 unittest(range
.size
== 0x10000000,
969 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
971 unittest(range
.cpu_addr
== 0x20000000,
972 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
974 unittest(range
.pci_addr
== 0x80000000,
975 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
978 unittest(range
.size
== 0x10000000,
979 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
981 unittest(range
.cpu_addr
== 0x40000000,
982 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
984 unittest(range
.pci_addr
== 0xc0000000,
985 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
994 static void __init
of_unittest_parse_interrupts(void)
996 struct device_node
*np
;
997 struct of_phandle_args args
;
1000 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
1003 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1005 pr_err("missing testcase data\n");
1009 for (i
= 0; i
< 4; i
++) {
1012 memset(&args
, 0, sizeof(args
));
1013 rc
= of_irq_parse_one(np
, i
, &args
);
1016 passed
&= (args
.args_count
== 1);
1017 passed
&= (args
.args
[0] == (i
+ 1));
1019 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1024 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1026 pr_err("missing testcase data\n");
1030 for (i
= 0; i
< 4; i
++) {
1033 memset(&args
, 0, sizeof(args
));
1034 rc
= of_irq_parse_one(np
, i
, &args
);
1036 /* Test the values from tests-phandle.dtsi */
1040 passed
&= (args
.args_count
== 1);
1041 passed
&= (args
.args
[0] == 9);
1045 passed
&= (args
.args_count
== 3);
1046 passed
&= (args
.args
[0] == 10);
1047 passed
&= (args
.args
[1] == 11);
1048 passed
&= (args
.args
[2] == 12);
1052 passed
&= (args
.args_count
== 2);
1053 passed
&= (args
.args
[0] == 13);
1054 passed
&= (args
.args
[1] == 14);
1058 passed
&= (args
.args_count
== 2);
1059 passed
&= (args
.args
[0] == 15);
1060 passed
&= (args
.args
[1] == 16);
1065 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1071 static void __init
of_unittest_parse_interrupts_extended(void)
1073 struct device_node
*np
;
1074 struct of_phandle_args args
;
1077 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
1080 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1082 pr_err("missing testcase data\n");
1086 for (i
= 0; i
< 7; i
++) {
1089 memset(&args
, 0, sizeof(args
));
1090 rc
= of_irq_parse_one(np
, i
, &args
);
1092 /* Test the values from tests-phandle.dtsi */
1096 passed
&= (args
.args_count
== 1);
1097 passed
&= (args
.args
[0] == 1);
1101 passed
&= (args
.args_count
== 3);
1102 passed
&= (args
.args
[0] == 2);
1103 passed
&= (args
.args
[1] == 3);
1104 passed
&= (args
.args
[2] == 4);
1108 passed
&= (args
.args_count
== 2);
1109 passed
&= (args
.args
[0] == 5);
1110 passed
&= (args
.args
[1] == 6);
1114 passed
&= (args
.args_count
== 1);
1115 passed
&= (args
.args
[0] == 9);
1119 passed
&= (args
.args_count
== 3);
1120 passed
&= (args
.args
[0] == 10);
1121 passed
&= (args
.args
[1] == 11);
1122 passed
&= (args
.args
[2] == 12);
1126 passed
&= (args
.args_count
== 2);
1127 passed
&= (args
.args
[0] == 13);
1128 passed
&= (args
.args
[1] == 14);
1132 passed
&= (args
.args_count
== 1);
1133 passed
&= (args
.args
[0] == 15);
1139 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1145 static const struct of_device_id match_node_table
[] = {
1146 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
1147 { .data
= "B", .type
= "type1", }, /* followed by type alone */
1149 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
1150 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
1151 { .data
= "Cc", .name
= "name2", .type
= "type2", },
1153 { .data
= "E", .compatible
= "compat3" },
1154 { .data
= "G", .compatible
= "compat2", },
1155 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
1156 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
1157 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
1158 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
1165 } match_node_tests
[] = {
1166 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
1167 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
1168 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
1169 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
1170 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
1171 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
1172 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
1173 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
1174 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
1175 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
1176 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
1177 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
1180 static void __init
of_unittest_match_node(void)
1182 struct device_node
*np
;
1183 const struct of_device_id
*match
;
1186 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
1187 np
= of_find_node_by_path(match_node_tests
[i
].path
);
1189 unittest(0, "missing testcase node %s\n",
1190 match_node_tests
[i
].path
);
1194 match
= of_match_node(match_node_table
, np
);
1196 unittest(0, "%s didn't match anything\n",
1197 match_node_tests
[i
].path
);
1201 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
1202 unittest(0, "%s got wrong match. expected %s, got %s\n",
1203 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
1204 (const char *)match
->data
);
1207 unittest(1, "passed");
1211 static struct resource test_bus_res
= {
1212 .start
= 0xfffffff8,
1214 .flags
= IORESOURCE_MEM
,
1216 static const struct platform_device_info test_bus_info
= {
1217 .name
= "unittest-bus",
1219 static void __init
of_unittest_platform_populate(void)
1222 struct device_node
*np
, *child
, *grandchild
;
1223 struct platform_device
*pdev
, *test_bus
;
1224 const struct of_device_id match
[] = {
1225 { .compatible
= "test-device", },
1229 np
= of_find_node_by_path("/testcase-data");
1230 of_platform_default_populate(np
, NULL
, NULL
);
1232 /* Test that a missing irq domain returns -EPROBE_DEFER */
1233 np
= of_find_node_by_path("/testcase-data/testcase-device1");
1234 pdev
= of_find_device_by_node(np
);
1235 unittest(pdev
, "device 1 creation failed\n");
1237 if (!(of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)) {
1238 irq
= platform_get_irq(pdev
, 0);
1239 unittest(irq
== -EPROBE_DEFER
,
1240 "device deferred probe failed - %d\n", irq
);
1242 /* Test that a parsing failure does not return -EPROBE_DEFER */
1243 np
= of_find_node_by_path("/testcase-data/testcase-device2");
1244 pdev
= of_find_device_by_node(np
);
1245 unittest(pdev
, "device 2 creation failed\n");
1247 EXPECT_BEGIN(KERN_INFO
,
1248 "platform testcase-data:testcase-device2: IRQ index 0 not found");
1250 irq
= platform_get_irq(pdev
, 0);
1252 EXPECT_END(KERN_INFO
,
1253 "platform testcase-data:testcase-device2: IRQ index 0 not found");
1255 unittest(irq
< 0 && irq
!= -EPROBE_DEFER
,
1256 "device parsing error failed - %d\n", irq
);
1259 np
= of_find_node_by_path("/testcase-data/platform-tests");
1260 unittest(np
, "No testcase data in device tree\n");
1264 test_bus
= platform_device_register_full(&test_bus_info
);
1265 rc
= PTR_ERR_OR_ZERO(test_bus
);
1266 unittest(!rc
, "testbus registration failed; rc=%i\n", rc
);
1271 test_bus
->dev
.of_node
= np
;
1274 * Add a dummy resource to the test bus node after it is
1275 * registered to catch problems with un-inserted resources. The
1276 * DT code doesn't insert the resources, and it has caused the
1277 * kernel to oops in the past. This makes sure the same bug
1278 * doesn't crop up again.
1280 platform_device_add_resources(test_bus
, &test_bus_res
, 1);
1282 of_platform_populate(np
, match
, NULL
, &test_bus
->dev
);
1283 for_each_child_of_node(np
, child
) {
1284 for_each_child_of_node(child
, grandchild
) {
1285 pdev
= of_find_device_by_node(grandchild
);
1287 "Could not create device for node '%pOFn'\n",
1293 of_platform_depopulate(&test_bus
->dev
);
1294 for_each_child_of_node(np
, child
) {
1295 for_each_child_of_node(child
, grandchild
)
1296 unittest(!of_find_device_by_node(grandchild
),
1297 "device didn't get destroyed '%pOFn'\n",
1301 platform_device_unregister(test_bus
);
1306 * update_node_properties - adds the properties
1307 * of np into dup node (present in live tree) and
1308 * updates parent of children of np to dup.
1310 * @np: node whose properties are being added to the live tree
1311 * @dup: node present in live tree to be updated
1313 static void update_node_properties(struct device_node
*np
,
1314 struct device_node
*dup
)
1316 struct property
*prop
;
1317 struct property
*save_next
;
1318 struct device_node
*child
;
1321 for_each_child_of_node(np
, child
)
1322 child
->parent
= dup
;
1325 * "unittest internal error: unable to add testdata property"
1327 * If this message reports a property in node '/__symbols__' then
1328 * the respective unittest overlay contains a label that has the
1329 * same name as a label in the live devicetree. The label will
1330 * be in the live devicetree only if the devicetree source was
1331 * compiled with the '-@' option. If you encounter this error,
1332 * please consider renaming __all__ of the labels in the unittest
1333 * overlay dts files with an odd prefix that is unlikely to be
1334 * used in a real devicetree.
1338 * open code for_each_property_of_node() because of_add_property()
1339 * sets prop->next to NULL
1341 for (prop
= np
->properties
; prop
!= NULL
; prop
= save_next
) {
1342 save_next
= prop
->next
;
1343 ret
= of_add_property(dup
, prop
);
1345 if (ret
== -EEXIST
&& !strcmp(prop
->name
, "name"))
1347 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1354 * attach_node_and_children - attaches nodes
1355 * and its children to live tree.
1356 * CAUTION: misleading function name - if node @np already exists in
1357 * the live tree then children of @np are *not* attached to the live
1358 * tree. This works for the current test devicetree nodes because such
1359 * nodes do not have child nodes.
1361 * @np: Node to attach to live tree
1363 static void attach_node_and_children(struct device_node
*np
)
1365 struct device_node
*next
, *dup
, *child
;
1366 unsigned long flags
;
1367 const char *full_name
;
1369 full_name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
1371 if (!strcmp(full_name
, "/__local_fixups__") ||
1372 !strcmp(full_name
, "/__fixups__")) {
1377 dup
= of_find_node_by_path(full_name
);
1380 update_node_properties(np
, dup
);
1387 mutex_lock(&of_mutex
);
1388 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1389 np
->sibling
= np
->parent
->child
;
1390 np
->parent
->child
= np
;
1391 of_node_clear_flag(np
, OF_DETACHED
);
1392 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1394 __of_attach_node_sysfs(np
);
1395 mutex_unlock(&of_mutex
);
1398 next
= child
->sibling
;
1399 attach_node_and_children(child
);
1405 * unittest_data_add - Reads, copies data from
1406 * linked tree and attaches it to the live tree
1408 static int __init
unittest_data_add(void)
1410 void *unittest_data
;
1411 struct device_node
*unittest_data_node
, *np
;
1413 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1414 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1416 extern uint8_t __dtb_testcases_begin
[];
1417 extern uint8_t __dtb_testcases_end
[];
1418 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
1422 pr_warn("%s: No testcase data to attach; not running tests\n",
1428 unittest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
1432 of_fdt_unflatten_tree(unittest_data
, NULL
, &unittest_data_node
);
1433 if (!unittest_data_node
) {
1434 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
1435 kfree(unittest_data
);
1440 * This lock normally encloses of_resolve_phandles()
1442 of_overlay_mutex_lock();
1444 rc
= of_resolve_phandles(unittest_data_node
);
1446 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
1447 of_overlay_mutex_unlock();
1452 of_root
= unittest_data_node
;
1453 for_each_of_allnodes(np
)
1454 __of_attach_node_sysfs(np
);
1455 of_aliases
= of_find_node_by_path("/aliases");
1456 of_chosen
= of_find_node_by_path("/chosen");
1457 of_overlay_mutex_unlock();
1461 EXPECT_BEGIN(KERN_INFO
,
1462 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1464 /* attach the sub-tree to live tree */
1465 np
= unittest_data_node
->child
;
1467 struct device_node
*next
= np
->sibling
;
1469 np
->parent
= of_root
;
1470 attach_node_and_children(np
);
1474 EXPECT_END(KERN_INFO
,
1475 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1477 of_overlay_mutex_unlock();
1482 #ifdef CONFIG_OF_OVERLAY
1483 static int __init
overlay_data_apply(const char *overlay_name
, int *overlay_id
);
1485 static int unittest_probe(struct platform_device
*pdev
)
1487 struct device
*dev
= &pdev
->dev
;
1488 struct device_node
*np
= dev
->of_node
;
1491 dev_err(dev
, "No OF data for device\n");
1496 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1498 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
1503 static int unittest_remove(struct platform_device
*pdev
)
1505 struct device
*dev
= &pdev
->dev
;
1506 struct device_node
*np
= dev
->of_node
;
1508 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1512 static const struct of_device_id unittest_match
[] = {
1513 { .compatible
= "unittest", },
1517 static struct platform_driver unittest_driver
= {
1518 .probe
= unittest_probe
,
1519 .remove
= unittest_remove
,
1522 .of_match_table
= of_match_ptr(unittest_match
),
1526 /* get the platform device instantiated at the path */
1527 static struct platform_device
*of_path_to_platform_device(const char *path
)
1529 struct device_node
*np
;
1530 struct platform_device
*pdev
;
1532 np
= of_find_node_by_path(path
);
1536 pdev
= of_find_device_by_node(np
);
1542 /* find out if a platform device exists at that path */
1543 static int of_path_platform_device_exists(const char *path
)
1545 struct platform_device
*pdev
;
1547 pdev
= of_path_to_platform_device(path
);
1548 platform_device_put(pdev
);
1549 return pdev
!= NULL
;
1552 #ifdef CONFIG_OF_GPIO
1554 struct unittest_gpio_dev
{
1555 struct gpio_chip chip
;
1558 static int unittest_gpio_chip_request_count
;
1559 static int unittest_gpio_probe_count
;
1560 static int unittest_gpio_probe_pass_count
;
1562 static int unittest_gpio_chip_request(struct gpio_chip
*chip
, unsigned int offset
)
1564 unittest_gpio_chip_request_count
++;
1566 pr_debug("%s(): %s %d %d\n", __func__
, chip
->label
, offset
,
1567 unittest_gpio_chip_request_count
);
1571 static int unittest_gpio_probe(struct platform_device
*pdev
)
1573 struct unittest_gpio_dev
*devptr
;
1576 unittest_gpio_probe_count
++;
1578 devptr
= kzalloc(sizeof(*devptr
), GFP_KERNEL
);
1582 platform_set_drvdata(pdev
, devptr
);
1584 devptr
->chip
.of_node
= pdev
->dev
.of_node
;
1585 devptr
->chip
.label
= "of-unittest-gpio";
1586 devptr
->chip
.base
= -1; /* dynamic allocation */
1587 devptr
->chip
.ngpio
= 5;
1588 devptr
->chip
.request
= unittest_gpio_chip_request
;
1590 ret
= gpiochip_add_data(&devptr
->chip
, NULL
);
1593 "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr
->chip
.of_node
, ret
);
1596 unittest_gpio_probe_pass_count
++;
1600 static int unittest_gpio_remove(struct platform_device
*pdev
)
1602 struct unittest_gpio_dev
*gdev
= platform_get_drvdata(pdev
);
1603 struct device
*dev
= &pdev
->dev
;
1604 struct device_node
*np
= pdev
->dev
.of_node
;
1606 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
1611 if (gdev
->chip
.base
!= -1)
1612 gpiochip_remove(&gdev
->chip
);
1614 platform_set_drvdata(pdev
, NULL
);
1620 static const struct of_device_id unittest_gpio_id
[] = {
1621 { .compatible
= "unittest-gpio", },
1625 static struct platform_driver unittest_gpio_driver
= {
1626 .probe
= unittest_gpio_probe
,
1627 .remove
= unittest_gpio_remove
,
1629 .name
= "unittest-gpio",
1630 .of_match_table
= of_match_ptr(unittest_gpio_id
),
1634 static void __init
of_unittest_overlay_gpio(void)
1636 int chip_request_count
;
1637 int probe_pass_count
;
1641 * tests: apply overlays before registering driver
1642 * Similar to installing a driver as a module, the
1643 * driver is registered after applying the overlays.
1645 * The overlays are applied by overlay_data_apply()
1646 * instead of of_unittest_apply_overlay() so that they
1647 * will not be tracked. Thus they will not be removed
1648 * by of_unittest_destroy_tracked_overlays().
1650 * - apply overlay_gpio_01
1651 * - apply overlay_gpio_02a
1652 * - apply overlay_gpio_02b
1655 * register driver will result in
1656 * - probe and processing gpio hog for overlay_gpio_01
1657 * - probe for overlay_gpio_02a
1658 * - processing gpio for overlay_gpio_02b
1661 probe_pass_count
= unittest_gpio_probe_pass_count
;
1662 chip_request_count
= unittest_gpio_chip_request_count
;
1665 * overlay_gpio_01 contains gpio node and child gpio hog node
1666 * overlay_gpio_02a contains gpio node
1667 * overlay_gpio_02b contains child gpio hog node
1670 unittest(overlay_data_apply("overlay_gpio_01", NULL
),
1671 "Adding overlay 'overlay_gpio_01' failed\n");
1673 unittest(overlay_data_apply("overlay_gpio_02a", NULL
),
1674 "Adding overlay 'overlay_gpio_02a' failed\n");
1676 unittest(overlay_data_apply("overlay_gpio_02b", NULL
),
1677 "Adding overlay 'overlay_gpio_02b' failed\n");
1680 * messages are the result of the probes, after the
1681 * driver is registered
1684 EXPECT_BEGIN(KERN_INFO
,
1685 "GPIO line <<int>> (line-B-input) hogged as input\n");
1687 EXPECT_BEGIN(KERN_INFO
,
1688 "GPIO line <<int>> (line-A-input) hogged as input\n");
1690 ret
= platform_driver_register(&unittest_gpio_driver
);
1691 if (unittest(ret
== 0, "could not register unittest gpio driver\n"))
1694 EXPECT_END(KERN_INFO
,
1695 "GPIO line <<int>> (line-A-input) hogged as input\n");
1696 EXPECT_END(KERN_INFO
,
1697 "GPIO line <<int>> (line-B-input) hogged as input\n");
1699 unittest(probe_pass_count
+ 2 == unittest_gpio_probe_pass_count
,
1700 "unittest_gpio_probe() failed or not called\n");
1702 unittest(chip_request_count
+ 2 == unittest_gpio_chip_request_count
,
1703 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1704 unittest_gpio_chip_request_count
- chip_request_count
);
1707 * tests: apply overlays after registering driver
1709 * Similar to a driver built-in to the kernel, the
1710 * driver is registered before applying the overlays.
1712 * overlay_gpio_03 contains gpio node and child gpio hog node
1714 * - apply overlay_gpio_03
1716 * apply overlay will result in
1717 * - probe and processing gpio hog.
1720 probe_pass_count
= unittest_gpio_probe_pass_count
;
1721 chip_request_count
= unittest_gpio_chip_request_count
;
1723 EXPECT_BEGIN(KERN_INFO
,
1724 "GPIO line <<int>> (line-D-input) hogged as input\n");
1726 /* overlay_gpio_03 contains gpio node and child gpio hog node */
1728 unittest(overlay_data_apply("overlay_gpio_03", NULL
),
1729 "Adding overlay 'overlay_gpio_03' failed\n");
1731 EXPECT_END(KERN_INFO
,
1732 "GPIO line <<int>> (line-D-input) hogged as input\n");
1734 unittest(probe_pass_count
+ 1 == unittest_gpio_probe_pass_count
,
1735 "unittest_gpio_probe() failed or not called\n");
1737 unittest(chip_request_count
+ 1 == unittest_gpio_chip_request_count
,
1738 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1739 unittest_gpio_chip_request_count
- chip_request_count
);
1742 * overlay_gpio_04a contains gpio node
1744 * - apply overlay_gpio_04a
1746 * apply the overlay will result in
1747 * - probe for overlay_gpio_04a
1750 probe_pass_count
= unittest_gpio_probe_pass_count
;
1751 chip_request_count
= unittest_gpio_chip_request_count
;
1753 /* overlay_gpio_04a contains gpio node */
1755 unittest(overlay_data_apply("overlay_gpio_04a", NULL
),
1756 "Adding overlay 'overlay_gpio_04a' failed\n");
1758 unittest(probe_pass_count
+ 1 == unittest_gpio_probe_pass_count
,
1759 "unittest_gpio_probe() failed or not called\n");
1762 * overlay_gpio_04b contains child gpio hog node
1764 * - apply overlay_gpio_04b
1766 * apply the overlay will result in
1767 * - processing gpio for overlay_gpio_04b
1770 EXPECT_BEGIN(KERN_INFO
,
1771 "GPIO line <<int>> (line-C-input) hogged as input\n");
1773 /* overlay_gpio_04b contains child gpio hog node */
1775 unittest(overlay_data_apply("overlay_gpio_04b", NULL
),
1776 "Adding overlay 'overlay_gpio_04b' failed\n");
1778 EXPECT_END(KERN_INFO
,
1779 "GPIO line <<int>> (line-C-input) hogged as input\n");
1781 unittest(chip_request_count
+ 1 == unittest_gpio_chip_request_count
,
1782 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1783 unittest_gpio_chip_request_count
- chip_request_count
);
1788 static void __init
of_unittest_overlay_gpio(void)
1795 #if IS_BUILTIN(CONFIG_I2C)
1797 /* get the i2c client device instantiated at the path */
1798 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
1800 struct device_node
*np
;
1801 struct i2c_client
*client
;
1803 np
= of_find_node_by_path(path
);
1807 client
= of_find_i2c_device_by_node(np
);
1813 /* find out if a i2c client device exists at that path */
1814 static int of_path_i2c_client_exists(const char *path
)
1816 struct i2c_client
*client
;
1818 client
= of_path_to_i2c_client(path
);
1820 put_device(&client
->dev
);
1821 return client
!= NULL
;
1824 static int of_path_i2c_client_exists(const char *path
)
1835 static int of_path_device_type_exists(const char *path
,
1836 enum overlay_type ovtype
)
1840 return of_path_platform_device_exists(path
);
1842 return of_path_i2c_client_exists(path
);
1847 static const char *unittest_path(int nr
, enum overlay_type ovtype
)
1850 static char buf
[256];
1854 base
= "/testcase-data/overlay-node/test-bus";
1857 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1863 snprintf(buf
, sizeof(buf
) - 1, "%s/test-unittest%d", base
, nr
);
1864 buf
[sizeof(buf
) - 1] = '\0';
1868 static int of_unittest_device_exists(int unittest_nr
, enum overlay_type ovtype
)
1872 path
= unittest_path(unittest_nr
, ovtype
);
1876 return of_path_platform_device_exists(path
);
1878 return of_path_i2c_client_exists(path
);
1883 static const char *overlay_name_from_nr(int nr
)
1885 static char buf
[256];
1887 snprintf(buf
, sizeof(buf
) - 1,
1889 buf
[sizeof(buf
) - 1] = '\0';
1894 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1896 /* FIXME: it is NOT guaranteed that overlay ids are assigned in sequence */
1898 #define MAX_UNITTEST_OVERLAYS 256
1899 static unsigned long overlay_id_bits
[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS
)];
1900 static int overlay_first_id
= -1;
1902 static long of_unittest_overlay_tracked(int id
)
1904 if (WARN_ON(id
>= MAX_UNITTEST_OVERLAYS
))
1906 return overlay_id_bits
[BIT_WORD(id
)] & BIT_MASK(id
);
1909 static void of_unittest_track_overlay(int id
)
1911 if (overlay_first_id
< 0)
1912 overlay_first_id
= id
;
1913 id
-= overlay_first_id
;
1915 if (WARN_ON(id
>= MAX_UNITTEST_OVERLAYS
))
1917 overlay_id_bits
[BIT_WORD(id
)] |= BIT_MASK(id
);
1920 static void of_unittest_untrack_overlay(int id
)
1922 if (overlay_first_id
< 0)
1924 id
-= overlay_first_id
;
1925 if (WARN_ON(id
>= MAX_UNITTEST_OVERLAYS
))
1927 overlay_id_bits
[BIT_WORD(id
)] &= ~BIT_MASK(id
);
1930 static void of_unittest_destroy_tracked_overlays(void)
1932 int id
, ret
, defers
, ovcs_id
;
1934 if (overlay_first_id
< 0)
1937 /* try until no defers */
1940 /* remove in reverse order */
1941 for (id
= MAX_UNITTEST_OVERLAYS
- 1; id
>= 0; id
--) {
1942 if (!of_unittest_overlay_tracked(id
))
1945 ovcs_id
= id
+ overlay_first_id
;
1946 ret
= of_overlay_remove(&ovcs_id
);
1947 if (ret
== -ENODEV
) {
1948 pr_warn("%s: no overlay to destroy for #%d\n",
1949 __func__
, id
+ overlay_first_id
);
1954 pr_warn("%s: overlay destroy failed for #%d\n",
1955 __func__
, id
+ overlay_first_id
);
1959 of_unittest_untrack_overlay(id
);
1961 } while (defers
> 0);
1964 static int __init
of_unittest_apply_overlay(int overlay_nr
, int *overlay_id
)
1966 const char *overlay_name
;
1968 overlay_name
= overlay_name_from_nr(overlay_nr
);
1970 if (!overlay_data_apply(overlay_name
, overlay_id
)) {
1971 unittest(0, "could not apply overlay \"%s\"\n",
1975 of_unittest_track_overlay(*overlay_id
);
1980 /* apply an overlay while checking before and after states */
1981 static int __init
of_unittest_apply_overlay_check(int overlay_nr
,
1982 int unittest_nr
, int before
, int after
,
1983 enum overlay_type ovtype
)
1987 /* unittest device must not be in before state */
1988 if (of_unittest_device_exists(unittest_nr
, ovtype
) != before
) {
1989 unittest(0, "%s with device @\"%s\" %s\n",
1990 overlay_name_from_nr(overlay_nr
),
1991 unittest_path(unittest_nr
, ovtype
),
1992 !before
? "enabled" : "disabled");
1997 ret
= of_unittest_apply_overlay(overlay_nr
, &ovcs_id
);
1999 /* of_unittest_apply_overlay already called unittest() */
2003 /* unittest device must be to set to after state */
2004 if (of_unittest_device_exists(unittest_nr
, ovtype
) != after
) {
2005 unittest(0, "%s failed to create @\"%s\" %s\n",
2006 overlay_name_from_nr(overlay_nr
),
2007 unittest_path(unittest_nr
, ovtype
),
2008 !after
? "enabled" : "disabled");
2015 /* apply an overlay and then revert it while checking before, after states */
2016 static int __init
of_unittest_apply_revert_overlay_check(int overlay_nr
,
2017 int unittest_nr
, int before
, int after
,
2018 enum overlay_type ovtype
)
2020 int ret
, ovcs_id
, save_id
;
2022 /* unittest device must be in before state */
2023 if (of_unittest_device_exists(unittest_nr
, ovtype
) != before
) {
2024 unittest(0, "%s with device @\"%s\" %s\n",
2025 overlay_name_from_nr(overlay_nr
),
2026 unittest_path(unittest_nr
, ovtype
),
2027 !before
? "enabled" : "disabled");
2031 /* apply the overlay */
2033 ret
= of_unittest_apply_overlay(overlay_nr
, &ovcs_id
);
2035 /* of_unittest_apply_overlay already called unittest() */
2039 /* unittest device must be in after state */
2040 if (of_unittest_device_exists(unittest_nr
, ovtype
) != after
) {
2041 unittest(0, "%s failed to create @\"%s\" %s\n",
2042 overlay_name_from_nr(overlay_nr
),
2043 unittest_path(unittest_nr
, ovtype
),
2044 !after
? "enabled" : "disabled");
2049 ret
= of_overlay_remove(&ovcs_id
);
2051 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2052 overlay_name_from_nr(overlay_nr
),
2053 unittest_path(unittest_nr
, ovtype
));
2056 of_unittest_untrack_overlay(save_id
);
2058 /* unittest device must be again in before state */
2059 if (of_unittest_device_exists(unittest_nr
, PDEV_OVERLAY
) != before
) {
2060 unittest(0, "%s with device @\"%s\" %s\n",
2061 overlay_name_from_nr(overlay_nr
),
2062 unittest_path(unittest_nr
, ovtype
),
2063 !before
? "enabled" : "disabled");
2070 /* test activation of device */
2071 static void __init
of_unittest_overlay_0(void)
2075 EXPECT_BEGIN(KERN_INFO
,
2076 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2078 /* device should enable */
2079 ret
= of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
);
2081 EXPECT_END(KERN_INFO
,
2082 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2087 unittest(1, "overlay test %d passed\n", 0);
2090 /* test deactivation of device */
2091 static void __init
of_unittest_overlay_1(void)
2095 EXPECT_BEGIN(KERN_INFO
,
2096 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2098 /* device should disable */
2099 ret
= of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
);
2101 EXPECT_END(KERN_INFO
,
2102 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2107 unittest(1, "overlay test %d passed\n", 1);
2111 /* test activation of device */
2112 static void __init
of_unittest_overlay_2(void)
2116 EXPECT_BEGIN(KERN_INFO
,
2117 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2119 /* device should enable */
2120 ret
= of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
);
2122 EXPECT_END(KERN_INFO
,
2123 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2127 unittest(1, "overlay test %d passed\n", 2);
2130 /* test deactivation of device */
2131 static void __init
of_unittest_overlay_3(void)
2135 EXPECT_BEGIN(KERN_INFO
,
2136 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2138 /* device should disable */
2139 ret
= of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
);
2141 EXPECT_END(KERN_INFO
,
2142 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2147 unittest(1, "overlay test %d passed\n", 3);
2150 /* test activation of a full device node */
2151 static void __init
of_unittest_overlay_4(void)
2153 /* device should disable */
2154 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
))
2157 unittest(1, "overlay test %d passed\n", 4);
2160 /* test overlay apply/revert sequence */
2161 static void __init
of_unittest_overlay_5(void)
2165 EXPECT_BEGIN(KERN_INFO
,
2166 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2168 /* device should disable */
2169 ret
= of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
);
2171 EXPECT_END(KERN_INFO
,
2172 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2177 unittest(1, "overlay test %d passed\n", 5);
2180 /* test overlay application in sequence */
2181 static void __init
of_unittest_overlay_6(void)
2183 int i
, ov_id
[2], ovcs_id
;
2184 int overlay_nr
= 6, unittest_nr
= 6;
2185 int before
= 0, after
= 1;
2186 const char *overlay_name
;
2190 /* unittest device must be in before state */
2191 for (i
= 0; i
< 2; i
++) {
2192 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
2194 unittest(0, "%s with device @\"%s\" %s\n",
2195 overlay_name_from_nr(overlay_nr
+ i
),
2196 unittest_path(unittest_nr
+ i
,
2198 !before
? "enabled" : "disabled");
2203 /* apply the overlays */
2205 EXPECT_BEGIN(KERN_INFO
,
2206 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2208 overlay_name
= overlay_name_from_nr(overlay_nr
+ 0);
2210 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2213 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2217 of_unittest_track_overlay(ov_id
[0]);
2219 EXPECT_END(KERN_INFO
,
2220 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2222 EXPECT_BEGIN(KERN_INFO
,
2223 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2225 overlay_name
= overlay_name_from_nr(overlay_nr
+ 1);
2227 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2230 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2234 of_unittest_track_overlay(ov_id
[1]);
2236 EXPECT_END(KERN_INFO
,
2237 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2240 for (i
= 0; i
< 2; i
++) {
2241 /* unittest device must be in after state */
2242 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
2244 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2245 overlay_name_from_nr(overlay_nr
+ i
),
2246 unittest_path(unittest_nr
+ i
,
2248 !after
? "enabled" : "disabled");
2253 for (i
= 1; i
>= 0; i
--) {
2255 if (of_overlay_remove(&ovcs_id
)) {
2256 unittest(0, "%s failed destroy @\"%s\"\n",
2257 overlay_name_from_nr(overlay_nr
+ i
),
2258 unittest_path(unittest_nr
+ i
,
2262 of_unittest_untrack_overlay(ov_id
[i
]);
2265 for (i
= 0; i
< 2; i
++) {
2266 /* unittest device must be again in before state */
2267 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
2269 unittest(0, "%s with device @\"%s\" %s\n",
2270 overlay_name_from_nr(overlay_nr
+ i
),
2271 unittest_path(unittest_nr
+ i
,
2273 !before
? "enabled" : "disabled");
2278 unittest(1, "overlay test %d passed\n", 6);
2282 /* test overlay application in sequence */
2283 static void __init
of_unittest_overlay_8(void)
2285 int i
, ov_id
[2], ovcs_id
;
2286 int overlay_nr
= 8, unittest_nr
= 8;
2287 const char *overlay_name
;
2290 /* we don't care about device state in this test */
2292 EXPECT_BEGIN(KERN_INFO
,
2293 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2295 overlay_name
= overlay_name_from_nr(overlay_nr
+ 0);
2297 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2299 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2301 EXPECT_END(KERN_INFO
,
2302 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2308 of_unittest_track_overlay(ov_id
[0]);
2310 overlay_name
= overlay_name_from_nr(overlay_nr
+ 1);
2312 EXPECT_BEGIN(KERN_INFO
,
2313 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2315 /* apply the overlays */
2316 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2318 EXPECT_END(KERN_INFO
,
2319 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2322 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2327 of_unittest_track_overlay(ov_id
[1]);
2329 /* now try to remove first overlay (it should fail) */
2332 EXPECT_BEGIN(KERN_INFO
,
2333 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2335 EXPECT_BEGIN(KERN_INFO
,
2336 "OF: overlay: overlay #6 is not topmost");
2338 ret
= of_overlay_remove(&ovcs_id
);
2340 EXPECT_END(KERN_INFO
,
2341 "OF: overlay: overlay #6 is not topmost");
2343 EXPECT_END(KERN_INFO
,
2344 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2347 unittest(0, "%s was destroyed @\"%s\"\n",
2348 overlay_name_from_nr(overlay_nr
+ 0),
2349 unittest_path(unittest_nr
,
2354 /* removing them in order should work */
2355 for (i
= 1; i
>= 0; i
--) {
2357 if (of_overlay_remove(&ovcs_id
)) {
2358 unittest(0, "%s not destroyed @\"%s\"\n",
2359 overlay_name_from_nr(overlay_nr
+ i
),
2360 unittest_path(unittest_nr
,
2364 of_unittest_untrack_overlay(ov_id
[i
]);
2367 unittest(1, "overlay test %d passed\n", 8);
2370 /* test insertion of a bus with parent devices */
2371 static void __init
of_unittest_overlay_10(void)
2376 /* device should disable */
2377 ret
= of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
2379 if (unittest(ret
== 0,
2380 "overlay test %d failed; overlay application\n", 10))
2383 child_path
= kasprintf(GFP_KERNEL
, "%s/test-unittest101",
2384 unittest_path(10, PDEV_OVERLAY
));
2385 if (unittest(child_path
, "overlay test %d failed; kasprintf\n", 10))
2388 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
2391 unittest(ret
, "overlay test %d failed; no child device\n", 10);
2394 /* test insertion of a bus with parent devices (and revert) */
2395 static void __init
of_unittest_overlay_11(void)
2399 /* device should disable */
2400 ret
= of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2403 unittest(ret
== 0, "overlay test %d failed; overlay apply\n", 11);
2406 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2408 struct unittest_i2c_bus_data
{
2409 struct platform_device
*pdev
;
2410 struct i2c_adapter adap
;
2413 static int unittest_i2c_master_xfer(struct i2c_adapter
*adap
,
2414 struct i2c_msg
*msgs
, int num
)
2416 struct unittest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
2423 static u32
unittest_i2c_functionality(struct i2c_adapter
*adap
)
2425 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
2428 static const struct i2c_algorithm unittest_i2c_algo
= {
2429 .master_xfer
= unittest_i2c_master_xfer
,
2430 .functionality
= unittest_i2c_functionality
,
2433 static int unittest_i2c_bus_probe(struct platform_device
*pdev
)
2435 struct device
*dev
= &pdev
->dev
;
2436 struct device_node
*np
= dev
->of_node
;
2437 struct unittest_i2c_bus_data
*std
;
2438 struct i2c_adapter
*adap
;
2442 dev_err(dev
, "No OF data for device\n");
2447 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2449 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
2453 /* link them together */
2455 platform_set_drvdata(pdev
, std
);
2458 i2c_set_adapdata(adap
, std
);
2460 strlcpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
2461 adap
->class = I2C_CLASS_DEPRECATED
;
2462 adap
->algo
= &unittest_i2c_algo
;
2463 adap
->dev
.parent
= dev
;
2464 adap
->dev
.of_node
= dev
->of_node
;
2465 adap
->timeout
= 5 * HZ
;
2468 ret
= i2c_add_numbered_adapter(adap
);
2470 dev_err(dev
, "Failed to add I2C adapter\n");
2477 static int unittest_i2c_bus_remove(struct platform_device
*pdev
)
2479 struct device
*dev
= &pdev
->dev
;
2480 struct device_node
*np
= dev
->of_node
;
2481 struct unittest_i2c_bus_data
*std
= platform_get_drvdata(pdev
);
2483 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2484 i2c_del_adapter(&std
->adap
);
2489 static const struct of_device_id unittest_i2c_bus_match
[] = {
2490 { .compatible
= "unittest-i2c-bus", },
2494 static struct platform_driver unittest_i2c_bus_driver
= {
2495 .probe
= unittest_i2c_bus_probe
,
2496 .remove
= unittest_i2c_bus_remove
,
2498 .name
= "unittest-i2c-bus",
2499 .of_match_table
= of_match_ptr(unittest_i2c_bus_match
),
2503 static int unittest_i2c_dev_probe(struct i2c_client
*client
,
2504 const struct i2c_device_id
*id
)
2506 struct device
*dev
= &client
->dev
;
2507 struct device_node
*np
= client
->dev
.of_node
;
2510 dev_err(dev
, "No OF node\n");
2514 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2519 static int unittest_i2c_dev_remove(struct i2c_client
*client
)
2521 struct device
*dev
= &client
->dev
;
2522 struct device_node
*np
= client
->dev
.of_node
;
2524 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2528 static const struct i2c_device_id unittest_i2c_dev_id
[] = {
2529 { .name
= "unittest-i2c-dev" },
2533 static struct i2c_driver unittest_i2c_dev_driver
= {
2535 .name
= "unittest-i2c-dev",
2537 .probe
= unittest_i2c_dev_probe
,
2538 .remove
= unittest_i2c_dev_remove
,
2539 .id_table
= unittest_i2c_dev_id
,
2542 #if IS_BUILTIN(CONFIG_I2C_MUX)
2544 static int unittest_i2c_mux_select_chan(struct i2c_mux_core
*muxc
, u32 chan
)
2549 static int unittest_i2c_mux_probe(struct i2c_client
*client
,
2550 const struct i2c_device_id
*id
)
2553 struct device
*dev
= &client
->dev
;
2554 struct i2c_adapter
*adap
= client
->adapter
;
2555 struct device_node
*np
= client
->dev
.of_node
, *child
;
2556 struct i2c_mux_core
*muxc
;
2559 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2562 dev_err(dev
, "No OF node\n");
2567 for_each_child_of_node(np
, child
) {
2568 if (of_property_read_u32(child
, "reg", ®
))
2570 if (max_reg
== (u32
)-1 || reg
> max_reg
)
2573 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
2575 dev_err(dev
, "No channels\n");
2579 muxc
= i2c_mux_alloc(adap
, dev
, nchans
, 0, 0,
2580 unittest_i2c_mux_select_chan
, NULL
);
2583 for (i
= 0; i
< nchans
; i
++) {
2584 if (i2c_mux_add_adapter(muxc
, 0, i
, 0)) {
2585 dev_err(dev
, "Failed to register mux #%d\n", i
);
2586 i2c_mux_del_adapters(muxc
);
2591 i2c_set_clientdata(client
, muxc
);
2596 static int unittest_i2c_mux_remove(struct i2c_client
*client
)
2598 struct device
*dev
= &client
->dev
;
2599 struct device_node
*np
= client
->dev
.of_node
;
2600 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
2602 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2603 i2c_mux_del_adapters(muxc
);
2607 static const struct i2c_device_id unittest_i2c_mux_id
[] = {
2608 { .name
= "unittest-i2c-mux" },
2612 static struct i2c_driver unittest_i2c_mux_driver
= {
2614 .name
= "unittest-i2c-mux",
2616 .probe
= unittest_i2c_mux_probe
,
2617 .remove
= unittest_i2c_mux_remove
,
2618 .id_table
= unittest_i2c_mux_id
,
2623 static int of_unittest_overlay_i2c_init(void)
2627 ret
= i2c_add_driver(&unittest_i2c_dev_driver
);
2628 if (unittest(ret
== 0,
2629 "could not register unittest i2c device driver\n"))
2632 ret
= platform_driver_register(&unittest_i2c_bus_driver
);
2634 if (unittest(ret
== 0,
2635 "could not register unittest i2c bus driver\n"))
2638 #if IS_BUILTIN(CONFIG_I2C_MUX)
2640 EXPECT_BEGIN(KERN_INFO
,
2641 "i2c i2c-1: Added multiplexed i2c bus 2");
2643 ret
= i2c_add_driver(&unittest_i2c_mux_driver
);
2645 EXPECT_END(KERN_INFO
,
2646 "i2c i2c-1: Added multiplexed i2c bus 2");
2648 if (unittest(ret
== 0,
2649 "could not register unittest i2c mux driver\n"))
2656 static void of_unittest_overlay_i2c_cleanup(void)
2658 #if IS_BUILTIN(CONFIG_I2C_MUX)
2659 i2c_del_driver(&unittest_i2c_mux_driver
);
2661 platform_driver_unregister(&unittest_i2c_bus_driver
);
2662 i2c_del_driver(&unittest_i2c_dev_driver
);
2665 static void __init
of_unittest_overlay_i2c_12(void)
2669 /* device should enable */
2670 EXPECT_BEGIN(KERN_INFO
,
2671 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2673 ret
= of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
);
2675 EXPECT_END(KERN_INFO
,
2676 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2681 unittest(1, "overlay test %d passed\n", 12);
2684 /* test deactivation of device */
2685 static void __init
of_unittest_overlay_i2c_13(void)
2689 EXPECT_BEGIN(KERN_INFO
,
2690 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2692 /* device should disable */
2693 ret
= of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
);
2695 EXPECT_END(KERN_INFO
,
2696 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2701 unittest(1, "overlay test %d passed\n", 13);
2704 /* just check for i2c mux existence */
2705 static void of_unittest_overlay_i2c_14(void)
2709 static void __init
of_unittest_overlay_i2c_15(void)
2713 /* device should enable */
2714 EXPECT_BEGIN(KERN_INFO
,
2715 "i2c i2c-1: Added multiplexed i2c bus 3");
2717 ret
= of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY
);
2719 EXPECT_END(KERN_INFO
,
2720 "i2c i2c-1: Added multiplexed i2c bus 3");
2725 unittest(1, "overlay test %d passed\n", 15);
2730 static inline void of_unittest_overlay_i2c_14(void) { }
2731 static inline void of_unittest_overlay_i2c_15(void) { }
2735 static void __init
of_unittest_overlay(void)
2737 struct device_node
*bus_np
= NULL
;
2739 if (platform_driver_register(&unittest_driver
)) {
2740 unittest(0, "could not register unittest driver\n");
2744 bus_np
= of_find_node_by_path(bus_path
);
2745 if (bus_np
== NULL
) {
2746 unittest(0, "could not find bus_path \"%s\"\n", bus_path
);
2750 if (of_platform_default_populate(bus_np
, NULL
, NULL
)) {
2751 unittest(0, "could not populate bus @ \"%s\"\n", bus_path
);
2755 if (!of_unittest_device_exists(100, PDEV_OVERLAY
)) {
2756 unittest(0, "could not find unittest0 @ \"%s\"\n",
2757 unittest_path(100, PDEV_OVERLAY
));
2761 if (of_unittest_device_exists(101, PDEV_OVERLAY
)) {
2762 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2763 unittest_path(101, PDEV_OVERLAY
));
2767 unittest(1, "basic infrastructure of overlays passed");
2769 /* tests in sequence */
2770 of_unittest_overlay_0();
2771 of_unittest_overlay_1();
2772 of_unittest_overlay_2();
2773 of_unittest_overlay_3();
2774 of_unittest_overlay_4();
2775 of_unittest_overlay_5();
2776 of_unittest_overlay_6();
2777 of_unittest_overlay_8();
2779 of_unittest_overlay_10();
2780 of_unittest_overlay_11();
2782 #if IS_BUILTIN(CONFIG_I2C)
2783 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
2786 of_unittest_overlay_i2c_12();
2787 of_unittest_overlay_i2c_13();
2788 of_unittest_overlay_i2c_14();
2789 of_unittest_overlay_i2c_15();
2791 of_unittest_overlay_i2c_cleanup();
2794 of_unittest_overlay_gpio();
2796 of_unittest_destroy_tracked_overlays();
2799 of_node_put(bus_np
);
2803 static inline void __init
of_unittest_overlay(void) { }
2806 #ifdef CONFIG_OF_OVERLAY
2809 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2810 * in scripts/Makefile.lib
2813 #define OVERLAY_INFO_EXTERN(name) \
2814 extern uint8_t __dtb_##name##_begin[]; \
2815 extern uint8_t __dtb_##name##_end[]
2817 #define OVERLAY_INFO(overlay_name, expected) \
2818 { .dtb_begin = __dtb_##overlay_name##_begin, \
2819 .dtb_end = __dtb_##overlay_name##_end, \
2820 .expected_result = expected, \
2821 .name = #overlay_name, \
2824 struct overlay_info
{
2827 int expected_result
;
2832 OVERLAY_INFO_EXTERN(overlay_base
);
2833 OVERLAY_INFO_EXTERN(overlay
);
2834 OVERLAY_INFO_EXTERN(overlay_0
);
2835 OVERLAY_INFO_EXTERN(overlay_1
);
2836 OVERLAY_INFO_EXTERN(overlay_2
);
2837 OVERLAY_INFO_EXTERN(overlay_3
);
2838 OVERLAY_INFO_EXTERN(overlay_4
);
2839 OVERLAY_INFO_EXTERN(overlay_5
);
2840 OVERLAY_INFO_EXTERN(overlay_6
);
2841 OVERLAY_INFO_EXTERN(overlay_7
);
2842 OVERLAY_INFO_EXTERN(overlay_8
);
2843 OVERLAY_INFO_EXTERN(overlay_9
);
2844 OVERLAY_INFO_EXTERN(overlay_10
);
2845 OVERLAY_INFO_EXTERN(overlay_11
);
2846 OVERLAY_INFO_EXTERN(overlay_12
);
2847 OVERLAY_INFO_EXTERN(overlay_13
);
2848 OVERLAY_INFO_EXTERN(overlay_15
);
2849 OVERLAY_INFO_EXTERN(overlay_gpio_01
);
2850 OVERLAY_INFO_EXTERN(overlay_gpio_02a
);
2851 OVERLAY_INFO_EXTERN(overlay_gpio_02b
);
2852 OVERLAY_INFO_EXTERN(overlay_gpio_03
);
2853 OVERLAY_INFO_EXTERN(overlay_gpio_04a
);
2854 OVERLAY_INFO_EXTERN(overlay_gpio_04b
);
2855 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node
);
2856 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop
);
2857 OVERLAY_INFO_EXTERN(overlay_bad_phandle
);
2858 OVERLAY_INFO_EXTERN(overlay_bad_symbol
);
2860 /* entries found by name */
2861 static struct overlay_info overlays
[] = {
2862 OVERLAY_INFO(overlay_base
, -9999),
2863 OVERLAY_INFO(overlay
, 0),
2864 OVERLAY_INFO(overlay_0
, 0),
2865 OVERLAY_INFO(overlay_1
, 0),
2866 OVERLAY_INFO(overlay_2
, 0),
2867 OVERLAY_INFO(overlay_3
, 0),
2868 OVERLAY_INFO(overlay_4
, 0),
2869 OVERLAY_INFO(overlay_5
, 0),
2870 OVERLAY_INFO(overlay_6
, 0),
2871 OVERLAY_INFO(overlay_7
, 0),
2872 OVERLAY_INFO(overlay_8
, 0),
2873 OVERLAY_INFO(overlay_9
, 0),
2874 OVERLAY_INFO(overlay_10
, 0),
2875 OVERLAY_INFO(overlay_11
, 0),
2876 OVERLAY_INFO(overlay_12
, 0),
2877 OVERLAY_INFO(overlay_13
, 0),
2878 OVERLAY_INFO(overlay_15
, 0),
2879 OVERLAY_INFO(overlay_gpio_01
, 0),
2880 OVERLAY_INFO(overlay_gpio_02a
, 0),
2881 OVERLAY_INFO(overlay_gpio_02b
, 0),
2882 OVERLAY_INFO(overlay_gpio_03
, 0),
2883 OVERLAY_INFO(overlay_gpio_04a
, 0),
2884 OVERLAY_INFO(overlay_gpio_04b
, 0),
2885 OVERLAY_INFO(overlay_bad_add_dup_node
, -EINVAL
),
2886 OVERLAY_INFO(overlay_bad_add_dup_prop
, -EINVAL
),
2887 OVERLAY_INFO(overlay_bad_phandle
, -EINVAL
),
2888 OVERLAY_INFO(overlay_bad_symbol
, -EINVAL
),
2890 {.dtb_begin
= NULL
, .dtb_end
= NULL
, .expected_result
= 0, .name
= NULL
}
2893 static struct device_node
*overlay_base_root
;
2895 static void * __init
dt_alloc_memory(u64 size
, u64 align
)
2897 void *ptr
= memblock_alloc(size
, align
);
2900 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2901 __func__
, size
, align
);
2907 * Create base device tree for the overlay unittest.
2909 * This is called from very early boot code.
2911 * Do as much as possible the same way as done in __unflatten_device_tree
2912 * and other early boot steps for the normal FDT so that the overlay base
2913 * unflattened tree will have the same characteristics as the real tree
2914 * (such as having memory allocated by the early allocator). The goal
2915 * is to test "the real thing" as much as possible, and test "test setup
2916 * code" as little as possible.
2918 * Have to stop before resolving phandles, because that uses kmalloc.
2920 void __init
unittest_unflatten_overlay_base(void)
2922 struct overlay_info
*info
;
2927 const char *overlay_name
= "overlay_base";
2929 for (info
= overlays
; info
&& info
->name
; info
++) {
2930 if (!strcmp(overlay_name
, info
->name
)) {
2936 pr_err("no overlay data for %s\n", overlay_name
);
2940 info
= &overlays
[0];
2942 if (info
->expected_result
!= -9999) {
2943 pr_err("No dtb 'overlay_base' to attach\n");
2947 data_size
= info
->dtb_end
- info
->dtb_begin
;
2949 pr_err("No dtb 'overlay_base' to attach\n");
2953 size
= fdt_totalsize(info
->dtb_begin
);
2954 if (size
!= data_size
) {
2955 pr_err("dtb 'overlay_base' header totalsize != actual size");
2959 new_fdt
= dt_alloc_memory(size
, roundup_pow_of_two(FDT_V17_SIZE
));
2961 pr_err("alloc for dtb 'overlay_base' failed");
2965 memcpy(new_fdt
, info
->dtb_begin
, size
);
2967 __unflatten_device_tree(new_fdt
, NULL
, &overlay_base_root
,
2968 dt_alloc_memory
, true);
2972 * The purpose of of_unittest_overlay_data_add is to add an
2973 * overlay in the normal fashion. This is a test of the whole
2974 * picture, instead of testing individual elements.
2976 * A secondary purpose is to be able to verify that the contents of
2977 * /proc/device-tree/ contains the updated structure and values from
2978 * the overlay. That must be verified separately in user space.
2980 * Return 0 on unexpected error.
2982 static int __init
overlay_data_apply(const char *overlay_name
, int *overlay_id
)
2984 struct overlay_info
*info
;
2989 for (info
= overlays
; info
&& info
->name
; info
++) {
2990 if (!strcmp(overlay_name
, info
->name
)) {
2996 pr_err("no overlay data for %s\n", overlay_name
);
3000 size
= info
->dtb_end
- info
->dtb_begin
;
3002 pr_err("no overlay data for %s\n", overlay_name
);
3004 ret
= of_overlay_fdt_apply(info
->dtb_begin
, size
, &info
->overlay_id
);
3006 *overlay_id
= info
->overlay_id
;
3010 pr_debug("%s applied\n", overlay_name
);
3013 if (ret
!= info
->expected_result
)
3014 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3015 info
->expected_result
, ret
, overlay_name
);
3017 return (ret
== info
->expected_result
);
3021 * The purpose of of_unittest_overlay_high_level is to add an overlay
3022 * in the normal fashion. This is a test of the whole picture,
3023 * instead of individual elements.
3025 * The first part of the function is _not_ normal overlay usage; it is
3026 * finishing splicing the base overlay device tree into the live tree.
3028 static __init
void of_unittest_overlay_high_level(void)
3030 struct device_node
*last_sibling
;
3031 struct device_node
*np
;
3032 struct device_node
*of_symbols
;
3033 struct device_node
*overlay_base_symbols
;
3034 struct device_node
**pprev
;
3035 struct property
*prop
;
3038 if (!overlay_base_root
) {
3039 unittest(0, "overlay_base_root not initialized\n");
3044 * Could not fixup phandles in unittest_unflatten_overlay_base()
3045 * because kmalloc() was not yet available.
3047 of_overlay_mutex_lock();
3048 of_resolve_phandles(overlay_base_root
);
3049 of_overlay_mutex_unlock();
3053 * do not allow overlay_base to duplicate any node already in
3054 * tree, this greatly simplifies the code
3058 * remove overlay_base_root node "__local_fixups", after
3059 * being used by of_resolve_phandles()
3061 pprev
= &overlay_base_root
->child
;
3062 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
) {
3063 if (of_node_name_eq(np
, "__local_fixups__")) {
3064 *pprev
= np
->sibling
;
3067 pprev
= &np
->sibling
;
3070 /* remove overlay_base_root node "__symbols__" if in live tree */
3071 of_symbols
= of_get_child_by_name(of_root
, "__symbols__");
3073 /* will have to graft properties from node into live tree */
3074 pprev
= &overlay_base_root
->child
;
3075 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
) {
3076 if (of_node_name_eq(np
, "__symbols__")) {
3077 overlay_base_symbols
= np
;
3078 *pprev
= np
->sibling
;
3081 pprev
= &np
->sibling
;
3085 for_each_child_of_node(overlay_base_root
, np
) {
3086 struct device_node
*base_child
;
3087 for_each_child_of_node(of_root
, base_child
) {
3088 if (!strcmp(np
->full_name
, base_child
->full_name
)) {
3089 unittest(0, "illegal node name in overlay_base %pOFn",
3097 * overlay 'overlay_base' is not allowed to have root
3098 * properties, so only need to splice nodes into main device tree.
3100 * root node of *overlay_base_root will not be freed, it is lost
3104 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
)
3105 np
->parent
= of_root
;
3107 mutex_lock(&of_mutex
);
3109 for (last_sibling
= np
= of_root
->child
; np
; np
= np
->sibling
)
3113 last_sibling
->sibling
= overlay_base_root
->child
;
3115 of_root
->child
= overlay_base_root
->child
;
3117 for_each_of_allnodes_from(overlay_base_root
, np
)
3118 __of_attach_node_sysfs(np
);
3121 struct property
*new_prop
;
3122 for_each_property_of_node(overlay_base_symbols
, prop
) {
3124 new_prop
= __of_prop_dup(prop
, GFP_KERNEL
);
3126 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3130 if (__of_add_property(of_symbols
, new_prop
)) {
3131 kfree(new_prop
->name
);
3132 kfree(new_prop
->value
);
3134 /* "name" auto-generated by unflatten */
3135 if (!strcmp(prop
->name
, "name"))
3137 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3141 if (__of_add_property_sysfs(of_symbols
, new_prop
)) {
3142 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3149 mutex_unlock(&of_mutex
);
3152 /* now do the normal overlay usage test */
3154 EXPECT_BEGIN(KERN_ERR
,
3155 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3156 EXPECT_BEGIN(KERN_ERR
,
3157 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3158 EXPECT_BEGIN(KERN_ERR
,
3159 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3160 EXPECT_BEGIN(KERN_ERR
,
3161 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3162 EXPECT_BEGIN(KERN_ERR
,
3163 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3164 EXPECT_BEGIN(KERN_ERR
,
3165 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3166 EXPECT_BEGIN(KERN_ERR
,
3167 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3168 EXPECT_BEGIN(KERN_ERR
,
3169 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3170 EXPECT_BEGIN(KERN_ERR
,
3171 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3172 EXPECT_BEGIN(KERN_ERR
,
3173 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3174 EXPECT_BEGIN(KERN_ERR
,
3175 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3177 ret
= overlay_data_apply("overlay", NULL
);
3179 EXPECT_END(KERN_ERR
,
3180 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3181 EXPECT_END(KERN_ERR
,
3182 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3183 EXPECT_END(KERN_ERR
,
3184 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3185 EXPECT_END(KERN_ERR
,
3186 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3187 EXPECT_END(KERN_ERR
,
3188 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3189 EXPECT_END(KERN_ERR
,
3190 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3191 EXPECT_END(KERN_ERR
,
3192 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3193 EXPECT_END(KERN_ERR
,
3194 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3195 EXPECT_END(KERN_ERR
,
3196 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3197 EXPECT_END(KERN_ERR
,
3198 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3199 EXPECT_END(KERN_ERR
,
3200 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3202 unittest(ret
, "Adding overlay 'overlay' failed\n");
3204 EXPECT_BEGIN(KERN_ERR
,
3205 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3206 EXPECT_BEGIN(KERN_ERR
,
3207 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3209 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL
),
3210 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3212 EXPECT_END(KERN_ERR
,
3213 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3214 EXPECT_END(KERN_ERR
,
3215 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3217 EXPECT_BEGIN(KERN_ERR
,
3218 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3219 EXPECT_BEGIN(KERN_ERR
,
3220 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3221 EXPECT_BEGIN(KERN_ERR
,
3222 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3224 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL
),
3225 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3227 EXPECT_END(KERN_ERR
,
3228 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3229 EXPECT_END(KERN_ERR
,
3230 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3231 EXPECT_END(KERN_ERR
,
3232 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3234 unittest(overlay_data_apply("overlay_bad_phandle", NULL
),
3235 "Adding overlay 'overlay_bad_phandle' failed\n");
3237 unittest(overlay_data_apply("overlay_bad_symbol", NULL
),
3238 "Adding overlay 'overlay_bad_symbol' failed\n");
3243 mutex_unlock(&of_mutex
);
3248 static inline __init
void of_unittest_overlay_high_level(void) {}
3252 static int __init
of_unittest(void)
3254 struct device_node
*np
;
3257 pr_info("start of unittest - you will see error messages\n");
3259 /* adding data for unittest */
3261 if (IS_ENABLED(CONFIG_UML
))
3262 unittest_unflatten_overlay_base();
3264 res
= unittest_data_add();
3268 of_aliases
= of_find_node_by_path("/aliases");
3270 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
3272 pr_info("No testcase data in device tree; not running tests\n");
3277 of_unittest_check_tree_linkage();
3278 of_unittest_check_phandles();
3279 of_unittest_find_node_by_name();
3280 of_unittest_dynamic();
3281 of_unittest_parse_phandle_with_args();
3282 of_unittest_parse_phandle_with_args_map();
3283 of_unittest_printf();
3284 of_unittest_property_string();
3285 of_unittest_property_copy();
3286 of_unittest_changeset();
3287 of_unittest_parse_interrupts();
3288 of_unittest_parse_interrupts_extended();
3289 of_unittest_dma_get_max_cpu_address();
3290 of_unittest_parse_dma_ranges();
3291 of_unittest_pci_dma_ranges();
3292 of_unittest_match_node();
3293 of_unittest_platform_populate();
3294 of_unittest_overlay();
3296 /* Double check linkage after removing testcase data */
3297 of_unittest_check_tree_linkage();
3299 of_unittest_overlay_high_level();
3301 pr_info("end of unittest - %i passed, %i failed\n",
3302 unittest_results
.passed
, unittest_results
.failed
);
3306 late_initcall(of_unittest
);