1 // SPDX-License-Identifier: GPL-2.0
3 * Self tests for device tree subsystem
6 #define pr_fmt(fmt) "### dt-test ### " fmt
8 #include <linux/memblock.h>
10 #include <linux/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/hashtable.h>
14 #include <linux/libfdt.h>
16 #include <linux/of_address.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-mux.h>
30 #include <linux/gpio/driver.h>
32 #include <linux/bitops.h>
34 #include "of_private.h"
36 static struct unittest_results
{
41 #define unittest(result, fmt, ...) ({ \
42 bool failed = !(result); \
44 unittest_results.failed++; \
45 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
47 unittest_results.passed++; \
48 pr_info("pass %s():%i\n", __func__, __LINE__); \
54 #define OF_KREF_READ(NODE) kref_read(&(NODE)->kobj.kref)
56 #define OF_KREF_READ(NODE) 1
60 * Expected message may have a message level other than KERN_INFO.
61 * Print the expected message only if the current loglevel will allow
62 * the actual message to print.
64 * Do not use EXPECT_BEGIN(), EXPECT_END(), EXPECT_NOT_BEGIN(), or
65 * EXPECT_NOT_END() to report messages expected to be reported or not
66 * reported by pr_debug().
68 #define EXPECT_BEGIN(level, fmt, ...) \
69 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
71 #define EXPECT_END(level, fmt, ...) \
72 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
74 #define EXPECT_NOT_BEGIN(level, fmt, ...) \
75 printk(level pr_fmt("EXPECT_NOT \\ : ") fmt, ##__VA_ARGS__)
77 #define EXPECT_NOT_END(level, fmt, ...) \
78 printk(level pr_fmt("EXPECT_NOT / : ") fmt, ##__VA_ARGS__)
80 static void __init
of_unittest_find_node_by_name(void)
82 struct device_node
*np
;
83 const char *options
, *name
;
85 np
= of_find_node_by_path("/testcase-data");
86 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
87 unittest(np
&& name
&& !strcmp("/testcase-data", name
),
88 "find /testcase-data failed\n");
92 /* Test if trailing '/' works */
93 np
= of_find_node_by_path("/testcase-data/");
94 unittest(!np
, "trailing '/' on /testcase-data/ should fail\n");
96 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
97 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
98 unittest(np
&& name
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
),
99 "find /testcase-data/phandle-tests/consumer-a failed\n");
103 np
= of_find_node_by_path("testcase-alias");
104 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
105 unittest(np
&& name
&& !strcmp("/testcase-data", name
),
106 "find testcase-alias failed\n");
110 /* Test if trailing '/' works on aliases */
111 np
= of_find_node_by_path("testcase-alias/");
112 unittest(!np
, "trailing '/' on testcase-alias/ should fail\n");
114 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
115 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
116 unittest(np
&& name
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
),
117 "find testcase-alias/phandle-tests/consumer-a failed\n");
121 np
= of_find_node_by_path("/testcase-data/missing-path");
122 unittest(!np
, "non-existent path returned node %pOF\n", np
);
125 np
= of_find_node_by_path("missing-alias");
126 unittest(!np
, "non-existent alias returned node %pOF\n", np
);
129 np
= of_find_node_by_path("testcase-alias/missing-path");
130 unittest(!np
, "non-existent alias with relative path returned node %pOF\n", np
);
133 np
= of_find_node_opts_by_path("/testcase-data:testoption", &options
);
134 unittest(np
&& !strcmp("testoption", options
),
135 "option path test failed\n");
138 np
= of_find_node_opts_by_path("/testcase-data:test/option", &options
);
139 unittest(np
&& !strcmp("test/option", options
),
140 "option path test, subcase #1 failed\n");
143 np
= of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options
);
144 unittest(np
&& !strcmp("test/option", options
),
145 "option path test, subcase #2 failed\n");
148 np
= of_find_node_opts_by_path("/testcase-data:testoption", NULL
);
149 unittest(np
, "NULL option path test failed\n");
152 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption",
154 unittest(np
&& !strcmp("testaliasoption", options
),
155 "option alias path test failed\n");
158 np
= of_find_node_opts_by_path("testcase-alias:test/alias/option",
160 unittest(np
&& !strcmp("test/alias/option", options
),
161 "option alias path test, subcase #1 failed\n");
164 np
= of_find_node_opts_by_path("testcase-alias/phandle-tests/consumer-a:testaliasoption",
166 name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
167 unittest(np
&& name
&& !strcmp("/testcase-data/phandle-tests/consumer-a", name
) &&
168 !strcmp("testaliasoption", options
),
169 "option alias path test, subcase #2 failed\n");
173 np
= of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL
);
174 unittest(np
, "NULL option alias path test failed\n");
177 options
= "testoption";
178 np
= of_find_node_opts_by_path("testcase-alias", &options
);
179 unittest(np
&& !options
, "option clearing test failed\n");
182 options
= "testoption";
183 np
= of_find_node_opts_by_path("/", &options
);
184 unittest(np
&& !options
, "option clearing root node test failed\n");
188 static void __init
of_unittest_dynamic(void)
190 struct device_node
*np
;
191 struct property
*prop
;
193 np
= of_find_node_by_path("/testcase-data");
195 pr_err("missing testcase data\n");
199 /* Array of 4 properties for the purpose of testing */
200 prop
= kcalloc(4, sizeof(*prop
), GFP_KERNEL
);
202 unittest(0, "kzalloc() failed\n");
206 /* Add a new property - should pass*/
207 prop
->name
= "new-property";
208 prop
->value
= "new-property-data";
209 prop
->length
= strlen(prop
->value
) + 1;
210 unittest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
212 /* Try to add an existing property - should fail */
214 prop
->name
= "new-property";
215 prop
->value
= "new-property-data-should-fail";
216 prop
->length
= strlen(prop
->value
) + 1;
217 unittest(of_add_property(np
, prop
) != 0,
218 "Adding an existing property should have failed\n");
220 /* Try to modify an existing property - should pass */
221 prop
->value
= "modify-property-data-should-pass";
222 prop
->length
= strlen(prop
->value
) + 1;
223 unittest(of_update_property(np
, prop
) == 0,
224 "Updating an existing property should have passed\n");
226 /* Try to modify non-existent property - should pass*/
228 prop
->name
= "modify-property";
229 prop
->value
= "modify-missing-property-data-should-pass";
230 prop
->length
= strlen(prop
->value
) + 1;
231 unittest(of_update_property(np
, prop
) == 0,
232 "Updating a missing property should have passed\n");
234 /* Remove property - should pass */
235 unittest(of_remove_property(np
, prop
) == 0,
236 "Removing a property should have passed\n");
238 /* Adding very large property - should pass */
240 prop
->name
= "large-property-PAGE_SIZEx8";
241 prop
->length
= PAGE_SIZE
* 8;
242 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
243 unittest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
245 unittest(of_add_property(np
, prop
) == 0,
246 "Adding a large property should have passed\n");
249 static int __init
of_unittest_check_node_linkage(struct device_node
*np
)
253 for_each_child_of_node_scoped(np
, child
) {
254 if (child
->parent
!= np
) {
255 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
260 rc
= of_unittest_check_node_linkage(child
);
269 static void __init
of_unittest_check_tree_linkage(void)
271 struct device_node
*np
;
272 int allnode_count
= 0, child_count
;
277 for_each_of_allnodes(np
)
279 child_count
= of_unittest_check_node_linkage(of_root
);
281 unittest(child_count
> 0, "Device node data structure is corrupted\n");
282 unittest(child_count
== allnode_count
,
283 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
284 allnode_count
, child_count
);
285 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
288 static void __init
of_unittest_printf_one(struct device_node
*np
, const char *fmt
,
289 const char *expected
)
295 buf_size
= strlen(expected
) + 10;
296 buf
= kmalloc(buf_size
, GFP_KERNEL
);
300 /* Baseline; check conversion with a large size limit */
301 memset(buf
, 0xff, buf_size
);
302 size
= snprintf(buf
, buf_size
- 2, fmt
, np
);
304 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
305 unittest((strcmp(buf
, expected
) == 0) && (buf
[size
+1] == 0xff),
306 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
309 /* Make sure length limits work */
311 for (i
= 0; i
< 2; i
++, size
--) {
312 /* Clear the buffer, and make sure it works correctly still */
313 memset(buf
, 0xff, buf_size
);
314 snprintf(buf
, size
+1, fmt
, np
);
315 unittest(strncmp(buf
, expected
, size
) == 0 && (buf
[size
+1] == 0xff),
316 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
317 size
, fmt
, expected
, buf
);
322 static void __init
of_unittest_printf(void)
324 struct device_node
*np
;
325 const char *full_name
= "/testcase-data/platform-tests/test-device@1/dev@100";
326 char phandle_str
[16] = "";
328 np
= of_find_node_by_path(full_name
);
330 unittest(np
, "testcase data missing\n");
334 num_to_str(phandle_str
, sizeof(phandle_str
), np
->phandle
, 0);
336 of_unittest_printf_one(np
, "%pOF", full_name
);
337 of_unittest_printf_one(np
, "%pOFf", full_name
);
338 of_unittest_printf_one(np
, "%pOFn", "dev");
339 of_unittest_printf_one(np
, "%2pOFn", "dev");
340 of_unittest_printf_one(np
, "%5pOFn", " dev");
341 of_unittest_printf_one(np
, "%pOFnc", "dev:test-sub-device");
342 of_unittest_printf_one(np
, "%pOFp", phandle_str
);
343 of_unittest_printf_one(np
, "%pOFP", "dev@100");
344 of_unittest_printf_one(np
, "ABC %pOFP ABC", "ABC dev@100 ABC");
345 of_unittest_printf_one(np
, "%10pOFP", " dev@100");
346 of_unittest_printf_one(np
, "%-10pOFP", "dev@100 ");
347 of_unittest_printf_one(of_root
, "%pOFP", "/");
348 of_unittest_printf_one(np
, "%pOFF", "----");
349 of_unittest_printf_one(np
, "%pOFPF", "dev@100:----");
350 of_unittest_printf_one(np
, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
351 of_unittest_printf_one(np
, "%pOFc", "test-sub-device");
352 of_unittest_printf_one(np
, "%pOFC",
353 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
357 struct hlist_node node
;
358 struct device_node
*np
;
361 static DEFINE_HASHTABLE(phandle_ht
, 8);
362 static void __init
of_unittest_check_phandles(void)
364 struct device_node
*np
;
365 struct node_hash
*nh
;
366 struct hlist_node
*tmp
;
367 int i
, dup_count
= 0, phandle_count
= 0;
369 for_each_of_allnodes(np
) {
373 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
374 if (nh
->np
->phandle
== np
->phandle
) {
375 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
376 np
->phandle
, nh
->np
, np
);
382 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
387 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
390 unittest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
391 dup_count
, phandle_count
);
394 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
400 static void __init
of_unittest_parse_phandle_with_args(void)
402 struct device_node
*np
;
403 struct of_phandle_args args
;
406 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
408 pr_err("missing testcase data\n");
412 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
413 unittest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
415 for (i
= 0; i
< 8; i
++) {
418 memset(&args
, 0, sizeof(args
));
419 rc
= of_parse_phandle_with_args(np
, "phandle-list",
420 "#phandle-cells", i
, &args
);
422 /* Test the values from tests-phandle.dtsi */
426 passed
&= (args
.args_count
== 1);
427 passed
&= (args
.args
[0] == (i
+ 1));
431 passed
&= (args
.args_count
== 2);
432 passed
&= (args
.args
[0] == (i
+ 1));
433 passed
&= (args
.args
[1] == 0);
436 passed
&= (rc
== -ENOENT
);
440 passed
&= (args
.args_count
== 3);
441 passed
&= (args
.args
[0] == (i
+ 1));
442 passed
&= (args
.args
[1] == 4);
443 passed
&= (args
.args
[2] == 3);
447 passed
&= (args
.args_count
== 2);
448 passed
&= (args
.args
[0] == (i
+ 1));
449 passed
&= (args
.args
[1] == 100);
453 passed
&= (args
.args_count
== 0);
457 passed
&= (args
.args_count
== 1);
458 passed
&= (args
.args
[0] == (i
+ 1));
461 passed
&= (rc
== -ENOENT
);
467 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
471 of_node_put(args
.np
);
474 /* Check for missing list property */
475 memset(&args
, 0, sizeof(args
));
476 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
477 "#phandle-cells", 0, &args
);
478 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
479 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
481 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
483 /* Check for missing cells property */
484 memset(&args
, 0, sizeof(args
));
486 EXPECT_BEGIN(KERN_INFO
,
487 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
489 rc
= of_parse_phandle_with_args(np
, "phandle-list",
490 "#phandle-cells-missing", 0, &args
);
492 EXPECT_END(KERN_INFO
,
493 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
495 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
497 EXPECT_BEGIN(KERN_INFO
,
498 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
500 rc
= of_count_phandle_with_args(np
, "phandle-list",
501 "#phandle-cells-missing");
503 EXPECT_END(KERN_INFO
,
504 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
506 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
508 /* Check for bad phandle in list */
509 memset(&args
, 0, sizeof(args
));
511 EXPECT_BEGIN(KERN_INFO
,
512 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
514 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
515 "#phandle-cells", 0, &args
);
517 EXPECT_END(KERN_INFO
,
518 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
520 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
522 EXPECT_BEGIN(KERN_INFO
,
523 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
525 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
528 EXPECT_END(KERN_INFO
,
529 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
531 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
533 /* Check for incorrectly formed argument list */
534 memset(&args
, 0, sizeof(args
));
536 EXPECT_BEGIN(KERN_INFO
,
537 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
539 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
540 "#phandle-cells", 1, &args
);
542 EXPECT_END(KERN_INFO
,
543 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
545 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
547 EXPECT_BEGIN(KERN_INFO
,
548 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
550 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
553 EXPECT_END(KERN_INFO
,
554 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
556 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
559 static void __init
of_unittest_parse_phandle_with_args_map(void)
561 struct device_node
*np
, *p
[6] = {};
562 struct of_phandle_args args
;
563 unsigned int prefs
[6];
566 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
568 pr_err("missing testcase data\n");
572 p
[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
573 p
[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
574 p
[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
575 p
[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
576 p
[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
577 p
[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
578 for (i
= 0; i
< ARRAY_SIZE(p
); ++i
) {
580 pr_err("missing testcase data\n");
583 prefs
[i
] = OF_KREF_READ(p
[i
]);
586 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
587 unittest(rc
== 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc
);
589 for (i
= 0; i
< 9; i
++) {
592 memset(&args
, 0, sizeof(args
));
593 rc
= of_parse_phandle_with_args_map(np
, "phandle-list",
594 "phandle", i
, &args
);
596 /* Test the values from tests-phandle.dtsi */
600 passed
&= (args
.np
== p
[1]);
601 passed
&= (args
.args_count
== 1);
602 passed
&= (args
.args
[0] == 1);
606 passed
&= (args
.np
== p
[3]);
607 passed
&= (args
.args_count
== 3);
608 passed
&= (args
.args
[0] == 2);
609 passed
&= (args
.args
[1] == 5);
610 passed
&= (args
.args
[2] == 3);
613 passed
&= (rc
== -ENOENT
);
617 passed
&= (args
.np
== p
[0]);
618 passed
&= (args
.args_count
== 0);
622 passed
&= (args
.np
== p
[1]);
623 passed
&= (args
.args_count
== 1);
624 passed
&= (args
.args
[0] == 3);
628 passed
&= (args
.np
== p
[0]);
629 passed
&= (args
.args_count
== 0);
633 passed
&= (args
.np
== p
[2]);
634 passed
&= (args
.args_count
== 2);
635 passed
&= (args
.args
[0] == 15);
636 passed
&= (args
.args
[1] == 0x20);
640 passed
&= (args
.np
== p
[3]);
641 passed
&= (args
.args_count
== 3);
642 passed
&= (args
.args
[0] == 2);
643 passed
&= (args
.args
[1] == 5);
644 passed
&= (args
.args
[2] == 3);
647 passed
&= (rc
== -ENOENT
);
653 unittest(passed
, "index %i - data error on node %s rc=%i\n",
654 i
, args
.np
->full_name
, rc
);
657 of_node_put(args
.np
);
660 /* Check for missing list property */
661 memset(&args
, 0, sizeof(args
));
662 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-missing",
663 "phandle", 0, &args
);
664 unittest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
666 /* Check for missing cells,map,mask property */
667 memset(&args
, 0, sizeof(args
));
669 EXPECT_BEGIN(KERN_INFO
,
670 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
672 rc
= of_parse_phandle_with_args_map(np
, "phandle-list",
673 "phandle-missing", 0, &args
);
674 EXPECT_END(KERN_INFO
,
675 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
677 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
679 /* Check for bad phandle in list */
680 memset(&args
, 0, sizeof(args
));
682 EXPECT_BEGIN(KERN_INFO
,
683 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
685 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-bad-phandle",
686 "phandle", 0, &args
);
687 EXPECT_END(KERN_INFO
,
688 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
690 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
692 /* Check for incorrectly formed argument list */
693 memset(&args
, 0, sizeof(args
));
695 EXPECT_BEGIN(KERN_INFO
,
696 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
698 rc
= of_parse_phandle_with_args_map(np
, "phandle-list-bad-args",
699 "phandle", 1, &args
);
700 EXPECT_END(KERN_INFO
,
701 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
703 unittest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
705 for (i
= 0; i
< ARRAY_SIZE(p
); ++i
) {
706 unittest(prefs
[i
] == OF_KREF_READ(p
[i
]),
707 "provider%d: expected:%d got:%d\n",
708 i
, prefs
[i
], OF_KREF_READ(p
[i
]));
713 static void __init
of_unittest_property_string(void)
715 const char *strings
[4];
716 struct device_node
*np
;
719 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
721 pr_err("No testcase data in device tree\n");
725 rc
= of_property_match_string(np
, "phandle-list-names", "first");
726 unittest(rc
== 0, "first expected:0 got:%i\n", rc
);
727 rc
= of_property_match_string(np
, "phandle-list-names", "second");
728 unittest(rc
== 1, "second expected:1 got:%i\n", rc
);
729 rc
= of_property_match_string(np
, "phandle-list-names", "third");
730 unittest(rc
== 2, "third expected:2 got:%i\n", rc
);
731 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
732 unittest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
733 rc
= of_property_match_string(np
, "missing-property", "blah");
734 unittest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
735 rc
= of_property_match_string(np
, "empty-property", "blah");
736 unittest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
737 rc
= of_property_match_string(np
, "unterminated-string", "blah");
738 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
740 /* of_property_count_strings() tests */
741 rc
= of_property_count_strings(np
, "string-property");
742 unittest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
743 rc
= of_property_count_strings(np
, "phandle-list-names");
744 unittest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
745 rc
= of_property_count_strings(np
, "unterminated-string");
746 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
747 rc
= of_property_count_strings(np
, "unterminated-string-list");
748 unittest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
750 /* of_property_read_string_index() tests */
751 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
752 unittest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
754 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
755 unittest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
756 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
757 unittest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
758 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
759 unittest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
760 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
761 unittest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
763 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
764 unittest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
766 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
767 unittest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
768 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
769 unittest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
771 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
772 unittest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
775 /* of_property_read_string_array() tests */
776 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
777 unittest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
778 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
779 unittest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
780 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
781 unittest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
782 /* -- An incorrectly formed string should cause a failure */
783 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
784 unittest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
785 /* -- parsing the correctly formed strings should still work: */
787 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
788 unittest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
790 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
791 unittest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
794 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
795 (p1)->value && (p2)->value && \
796 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
797 !strcmp((p1)->name, (p2)->name))
798 static void __init
of_unittest_property_copy(void)
800 #ifdef CONFIG_OF_DYNAMIC
801 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
802 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
803 struct property
*new;
805 new = __of_prop_dup(&p1
, GFP_KERNEL
);
806 unittest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
809 new = __of_prop_dup(&p2
, GFP_KERNEL
);
810 unittest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
815 static void __init
of_unittest_changeset(void)
817 #ifdef CONFIG_OF_DYNAMIC
819 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 1, .value
= "" };
820 struct property
*ppname_n1
, pname_n1
= { .name
= "name", .length
= 3, .value
= "n1" };
821 struct property
*ppname_n2
, pname_n2
= { .name
= "name", .length
= 3, .value
= "n2" };
822 struct property
*ppname_n21
, pname_n21
= { .name
= "name", .length
= 3, .value
= "n21" };
823 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
824 struct property
*ppremove
;
825 struct device_node
*n1
, *n2
, *n21
, *n22
, *nchangeset
, *nremove
, *parent
, *np
;
826 static const char * const str_array
[] = { "str1", "str2", "str3" };
827 const u32 u32_array
[] = { 1, 2, 3 };
828 struct of_changeset chgset
;
829 const char *propstr
= NULL
;
831 n1
= __of_node_dup(NULL
, "n1");
832 unittest(n1
, "testcase setup failure\n");
834 n2
= __of_node_dup(NULL
, "n2");
835 unittest(n2
, "testcase setup failure\n");
837 n21
= __of_node_dup(NULL
, "n21");
838 unittest(n21
, "testcase setup failure %p\n", n21
);
840 nchangeset
= of_find_node_by_path("/testcase-data/changeset");
841 nremove
= of_get_child_by_name(nchangeset
, "node-remove");
842 unittest(nremove
, "testcase setup failure\n");
844 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
845 unittest(ppadd
, "testcase setup failure\n");
847 ppname_n1
= __of_prop_dup(&pname_n1
, GFP_KERNEL
);
848 unittest(ppname_n1
, "testcase setup failure\n");
850 ppname_n2
= __of_prop_dup(&pname_n2
, GFP_KERNEL
);
851 unittest(ppname_n2
, "testcase setup failure\n");
853 ppname_n21
= __of_prop_dup(&pname_n21
, GFP_KERNEL
);
854 unittest(ppname_n21
, "testcase setup failure\n");
856 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
857 unittest(ppupdate
, "testcase setup failure\n");
864 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
865 unittest(ppremove
, "failed to find removal prop");
867 of_changeset_init(&chgset
);
869 unittest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
870 unittest(!of_changeset_add_property(&chgset
, n1
, ppname_n1
), "fail add prop name\n");
872 unittest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
873 unittest(!of_changeset_add_property(&chgset
, n2
, ppname_n2
), "fail add prop name\n");
875 unittest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
876 unittest(!of_changeset_add_property(&chgset
, n21
, ppname_n21
), "fail add prop name\n");
878 unittest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
880 unittest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop prop-add\n");
881 unittest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
882 unittest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
883 n22
= of_changeset_create_node(&chgset
, n2
, "n22");
884 unittest(n22
, "fail create n22\n");
885 unittest(!of_changeset_add_prop_string(&chgset
, n22
, "prop-str", "abcd"),
886 "fail add prop prop-str");
887 unittest(!of_changeset_add_prop_string_array(&chgset
, n22
, "prop-str-array",
888 (const char **)str_array
,
889 ARRAY_SIZE(str_array
)),
890 "fail add prop prop-str-array");
891 unittest(!of_changeset_add_prop_u32_array(&chgset
, n22
, "prop-u32-array",
892 u32_array
, ARRAY_SIZE(u32_array
)),
893 "fail add prop prop-u32-array");
895 unittest(!of_changeset_apply(&chgset
), "apply failed\n");
897 of_node_put(nchangeset
);
899 /* Make sure node names are constructed correctly */
900 unittest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
901 "'%pOF' not added\n", n21
);
903 unittest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n22")),
904 "'%pOF' not added\n", n22
);
907 unittest(!of_changeset_revert(&chgset
), "revert failed\n");
909 unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
910 "'%pOF' still present after revert\n", n21
);
912 unittest(of_property_present(parent
, "prop-remove"),
913 "failed to find removed prop after revert\n");
915 ret
= of_property_read_string(parent
, "prop-update", &propstr
);
916 unittest(!ret
, "failed to find updated prop after revert\n");
918 unittest(strcmp(propstr
, "hello") == 0, "original value not in updated property after revert");
920 of_changeset_destroy(&chgset
);
929 static void __init __maybe_unused
changeset_check_string(struct device_node
*np
,
930 const char *prop_name
,
931 const char *expected_str
)
936 ret
= of_property_read_string(np
, prop_name
, &str
);
937 if (unittest(ret
== 0, "failed to read %s\n", prop_name
))
940 unittest(strcmp(str
, expected_str
) == 0,
941 "%s value mismatch (read '%s', exp '%s')\n",
942 prop_name
, str
, expected_str
);
945 static void __init __maybe_unused
changeset_check_string_array(struct device_node
*np
,
946 const char *prop_name
,
947 const char * const *expected_array
,
955 cnt
= of_property_count_strings(np
, prop_name
);
956 if (unittest(cnt
>= 0, "failed to get %s count\n", prop_name
))
959 if (unittest(cnt
== count
,
960 "%s count mismatch (read %d, exp %u)\n",
961 prop_name
, cnt
, count
))
964 for (i
= 0; i
< count
; i
++) {
965 ret
= of_property_read_string_index(np
, prop_name
, i
, &str
);
966 if (unittest(ret
== 0, "failed to read %s[%d]\n", prop_name
, i
))
969 unittest(strcmp(str
, expected_array
[i
]) == 0,
970 "%s[%d] value mismatch (read '%s', exp '%s')\n",
971 prop_name
, i
, str
, expected_array
[i
]);
975 static void __init __maybe_unused
changeset_check_u32(struct device_node
*np
,
976 const char *prop_name
,
982 ret
= of_property_read_u32(np
, prop_name
, &val32
);
983 if (unittest(ret
== 0, "failed to read %s\n", prop_name
))
986 unittest(val32
== expected_u32
,
987 "%s value mismatch (read '%u', exp '%u')\n",
988 prop_name
, val32
, expected_u32
);
991 static void __init __maybe_unused
changeset_check_u32_array(struct device_node
*np
,
992 const char *prop_name
,
993 const u32
*expected_array
,
1001 cnt
= of_property_count_u32_elems(np
, prop_name
);
1002 if (unittest(cnt
>= 0, "failed to get %s count\n", prop_name
))
1005 if (unittest(cnt
== count
,
1006 "%s count mismatch (read %d, exp %u)\n",
1007 prop_name
, cnt
, count
))
1010 for (i
= 0; i
< count
; i
++) {
1011 ret
= of_property_read_u32_index(np
, prop_name
, i
, &val32
);
1012 if (unittest(ret
== 0, "failed to read %s[%d]\n", prop_name
, i
))
1015 unittest(val32
== expected_array
[i
],
1016 "%s[%d] value mismatch (read '%u', exp '%u')\n",
1017 prop_name
, i
, val32
, expected_array
[i
]);
1021 static void __init __maybe_unused
changeset_check_bool(struct device_node
*np
,
1022 const char *prop_name
)
1024 unittest(of_property_read_bool(np
, prop_name
),
1025 "%s value mismatch (read 'false', exp 'true')\n", prop_name
);
1028 static void __init
of_unittest_changeset_prop(void)
1030 #ifdef CONFIG_OF_DYNAMIC
1031 static const char * const str_array
[] = { "abc", "defg", "hij" };
1032 static const u32 u32_array
[] = { 123, 4567, 89, 10, 11 };
1033 struct device_node
*nchangeset
, *np
;
1034 struct of_changeset chgset
;
1037 nchangeset
= of_find_node_by_path("/testcase-data/changeset");
1039 pr_err("missing testcase data\n");
1043 of_changeset_init(&chgset
);
1045 np
= of_changeset_create_node(&chgset
, nchangeset
, "test-prop");
1046 if (unittest(np
, "failed to create test-prop node\n"))
1047 goto end_changeset_destroy
;
1049 ret
= of_changeset_add_prop_string(&chgset
, np
, "prop-string", "abcde");
1050 unittest(ret
== 0, "failed to add prop-string\n");
1052 ret
= of_changeset_add_prop_string_array(&chgset
, np
, "prop-string-array",
1053 str_array
, ARRAY_SIZE(str_array
));
1054 unittest(ret
== 0, "failed to add prop-string-array\n");
1056 ret
= of_changeset_add_prop_u32(&chgset
, np
, "prop-u32", 1234);
1057 unittest(ret
== 0, "failed to add prop-u32\n");
1059 ret
= of_changeset_add_prop_u32_array(&chgset
, np
, "prop-u32-array",
1060 u32_array
, ARRAY_SIZE(u32_array
));
1061 unittest(ret
== 0, "failed to add prop-u32-array\n");
1063 ret
= of_changeset_add_prop_bool(&chgset
, np
, "prop-bool");
1064 unittest(ret
== 0, "failed to add prop-bool\n");
1068 ret
= of_changeset_apply(&chgset
);
1069 if (unittest(ret
== 0, "failed to apply changeset\n"))
1070 goto end_changeset_destroy
;
1072 np
= of_find_node_by_path("/testcase-data/changeset/test-prop");
1073 if (unittest(np
, "failed to find test-prop node\n"))
1074 goto end_revert_changeset
;
1076 changeset_check_string(np
, "prop-string", "abcde");
1077 changeset_check_string_array(np
, "prop-string-array", str_array
, ARRAY_SIZE(str_array
));
1078 changeset_check_u32(np
, "prop-u32", 1234);
1079 changeset_check_u32_array(np
, "prop-u32-array", u32_array
, ARRAY_SIZE(u32_array
));
1080 changeset_check_bool(np
, "prop-bool");
1084 end_revert_changeset
:
1085 ret
= of_changeset_revert(&chgset
);
1086 unittest(ret
== 0, "failed to revert changeset\n");
1088 end_changeset_destroy
:
1089 of_changeset_destroy(&chgset
);
1090 of_node_put(nchangeset
);
1094 static void __init
of_unittest_dma_get_max_cpu_address(void)
1096 struct device_node
*np
;
1097 phys_addr_t cpu_addr
;
1099 if (!IS_ENABLED(CONFIG_OF_ADDRESS
))
1102 np
= of_find_node_by_path("/testcase-data/address-tests");
1104 pr_err("missing testcase data\n");
1108 cpu_addr
= of_dma_get_max_cpu_address(np
);
1109 unittest(cpu_addr
== 0x4fffffff,
1110 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
1111 &cpu_addr
, 0x4fffffff);
1114 static void __init
of_unittest_dma_ranges_one(const char *path
,
1115 u64 expect_dma_addr
, u64 expect_paddr
)
1117 #ifdef CONFIG_HAS_DMA
1118 struct device_node
*np
;
1119 const struct bus_dma_region
*map
= NULL
;
1122 np
= of_find_node_by_path(path
);
1124 pr_err("missing testcase data\n");
1128 rc
= of_dma_get_range(np
, &map
);
1130 unittest(!rc
, "of_dma_get_range failed on node %pOF rc=%i\n", np
, rc
);
1134 dma_addr_t dma_addr
;
1135 struct device
*dev_bogus
;
1137 dev_bogus
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
1139 unittest(0, "kzalloc() failed\n");
1144 dev_bogus
->dma_range_map
= map
;
1145 paddr
= dma_to_phys(dev_bogus
, expect_dma_addr
);
1146 dma_addr
= phys_to_dma(dev_bogus
, expect_paddr
);
1148 unittest(paddr
== expect_paddr
,
1149 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
1150 &paddr
, expect_paddr
, np
);
1151 unittest(dma_addr
== expect_dma_addr
,
1152 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
1153 &dma_addr
, expect_dma_addr
, np
);
1162 static void __init
of_unittest_parse_dma_ranges(void)
1164 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
1166 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT
))
1167 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
1168 0x100000000, 0x20000000);
1169 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
1170 0x80000000, 0x20000000);
1173 static void __init
of_unittest_pci_dma_ranges(void)
1175 struct device_node
*np
;
1176 struct of_pci_range range
;
1177 struct of_pci_range_parser parser
;
1180 if (!IS_ENABLED(CONFIG_PCI
))
1183 np
= of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
1185 pr_err("missing testcase data\n");
1189 if (of_pci_dma_range_parser_init(&parser
, np
)) {
1190 pr_err("missing dma-ranges property\n");
1195 * Get the dma-ranges from the device tree
1197 for_each_of_pci_range(&parser
, &range
) {
1199 unittest(range
.size
== 0x10000000,
1200 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1202 unittest(range
.cpu_addr
== 0x20000000,
1203 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1204 range
.cpu_addr
, np
);
1205 unittest(range
.pci_addr
== 0x80000000,
1206 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1207 range
.pci_addr
, np
);
1209 unittest(range
.size
== 0x10000000,
1210 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1212 unittest(range
.cpu_addr
== 0x40000000,
1213 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1214 range
.cpu_addr
, np
);
1215 unittest(range
.pci_addr
== 0xc0000000,
1216 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1217 range
.pci_addr
, np
);
1225 static void __init
of_unittest_pci_empty_dma_ranges(void)
1227 struct device_node
*np
;
1228 struct of_pci_range range
;
1229 struct of_pci_range_parser parser
;
1231 if (!IS_ENABLED(CONFIG_PCI
))
1234 np
= of_find_node_by_path("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0");
1236 pr_err("missing testcase data\n");
1240 if (of_pci_dma_range_parser_init(&parser
, np
)) {
1241 pr_err("missing dma-ranges property\n");
1246 * Get the dma-ranges from the device tree
1248 for_each_of_pci_range(&parser
, &range
) {
1249 unittest(range
.size
== 0x10000000,
1250 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1252 unittest(range
.cpu_addr
== 0x00000000,
1253 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1254 range
.cpu_addr
, np
);
1255 unittest(range
.pci_addr
== 0xc0000000,
1256 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1257 range
.pci_addr
, np
);
1263 static void __init
of_unittest_bus_ranges(void)
1265 struct device_node
*np
;
1266 struct of_range range
;
1267 struct of_range_parser parser
;
1268 struct resource res
;
1269 int ret
, count
, i
= 0;
1271 np
= of_find_node_by_path("/testcase-data/address-tests");
1273 pr_err("missing testcase data\n");
1277 if (of_range_parser_init(&parser
, np
)) {
1278 pr_err("missing ranges property\n");
1282 ret
= of_range_to_resource(np
, 1, &res
);
1283 unittest(!ret
, "of_range_to_resource returned error (%d) node %pOF\n",
1285 unittest(resource_type(&res
) == IORESOURCE_MEM
,
1286 "of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1288 unittest(res
.start
== 0xd0000000,
1289 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1291 unittest(resource_size(&res
) == 0x20000000,
1292 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1295 count
= of_range_count(&parser
);
1296 unittest(count
== 2,
1297 "of_range_count wrong size on node %pOF count=%d\n",
1301 * Get the "ranges" from the device tree
1303 for_each_of_range(&parser
, &range
) {
1304 unittest(range
.flags
== IORESOURCE_MEM
,
1305 "for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1306 np
, range
.flags
, IORESOURCE_MEM
);
1308 unittest(range
.size
== 0x50000000,
1309 "for_each_of_range wrong size on node %pOF size=%llx\n",
1311 unittest(range
.cpu_addr
== 0x70000000,
1312 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1313 range
.cpu_addr
, np
);
1314 unittest(range
.bus_addr
== 0x70000000,
1315 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1316 range
.pci_addr
, np
);
1318 unittest(range
.size
== 0x20000000,
1319 "for_each_of_range wrong size on node %pOF size=%llx\n",
1321 unittest(range
.cpu_addr
== 0xd0000000,
1322 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1323 range
.cpu_addr
, np
);
1324 unittest(range
.bus_addr
== 0x00000000,
1325 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1326 range
.pci_addr
, np
);
1334 static void __init
of_unittest_bus_3cell_ranges(void)
1336 struct device_node
*np
;
1337 struct of_range range
;
1338 struct of_range_parser parser
;
1341 np
= of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1343 pr_err("missing testcase data\n");
1347 if (of_range_parser_init(&parser
, np
)) {
1348 pr_err("missing ranges property\n");
1353 * Get the "ranges" from the device tree
1355 for_each_of_range(&parser
, &range
) {
1357 unittest(range
.flags
== 0xf00baa,
1358 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1360 unittest(range
.size
== 0x100000,
1361 "for_each_of_range wrong size on node %pOF size=%llx\n",
1363 unittest(range
.cpu_addr
== 0xa0000000,
1364 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1365 range
.cpu_addr
, np
);
1366 unittest(range
.bus_addr
== 0x0,
1367 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1368 range
.pci_addr
, np
);
1370 unittest(range
.flags
== 0xf00bee,
1371 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1373 unittest(range
.size
== 0x200000,
1374 "for_each_of_range wrong size on node %pOF size=%llx\n",
1376 unittest(range
.cpu_addr
== 0xb0000000,
1377 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1378 range
.cpu_addr
, np
);
1379 unittest(range
.bus_addr
== 0x100000000,
1380 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1381 range
.pci_addr
, np
);
1389 static void __init
of_unittest_reg(void)
1391 struct device_node
*np
;
1392 struct resource res
;
1396 np
= of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
1398 pr_err("missing testcase data\n");
1402 ret
= of_property_read_reg(np
, 0, &addr
, &size
);
1403 unittest(!ret
, "of_property_read_reg(%pOF) returned error %d\n",
1405 unittest(addr
== 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
1410 np
= of_find_node_by_path("/testcase-data/platform-tests-2/node/test-device@100");
1412 pr_err("missing testcase data\n");
1416 ret
= of_address_to_resource(np
, 0, &res
);
1417 unittest(ret
== -EINVAL
, "of_address_to_resource(%pOF) expected error on untranslatable address\n",
1424 struct of_unittest_expected_res
{
1426 struct resource res
;
1429 static void __init
of_unittest_check_addr(const char *node_path
,
1430 const struct of_unittest_expected_res
*tab_exp
,
1431 unsigned int tab_exp_count
)
1433 const struct of_unittest_expected_res
*expected
;
1434 struct device_node
*np
;
1435 struct resource res
;
1439 if (!IS_ENABLED(CONFIG_OF_ADDRESS
))
1442 np
= of_find_node_by_path(node_path
);
1444 pr_err("missing testcase data (%s)\n", node_path
);
1449 count
= tab_exp_count
;
1451 ret
= of_address_to_resource(np
, expected
->index
, &res
);
1452 unittest(!ret
, "of_address_to_resource(%pOF, %d) returned error %d\n",
1453 np
, expected
->index
, ret
);
1454 unittest(resource_type(&res
) == resource_type(&expected
->res
) &&
1455 res
.start
== expected
->res
.start
&&
1456 resource_size(&res
) == resource_size(&expected
->res
),
1457 "of_address_to_resource(%pOF, %d) wrong resource %pR, expected %pR\n",
1458 np
, expected
->index
, &res
, &expected
->res
);
1465 static const struct of_unittest_expected_res of_unittest_reg_2cell_expected_res
[] = {
1466 {.index
= 0, .res
= DEFINE_RES_MEM(0xa0a01000, 0x100) },
1467 {.index
= 1, .res
= DEFINE_RES_MEM(0xa0a02000, 0x100) },
1468 {.index
= 2, .res
= DEFINE_RES_MEM(0xc0c01000, 0x100) },
1469 {.index
= 3, .res
= DEFINE_RES_MEM(0xd0d01000, 0x100) },
1472 static const struct of_unittest_expected_res of_unittest_reg_3cell_expected_res
[] = {
1473 {.index
= 0, .res
= DEFINE_RES_MEM(0xa0a01000, 0x100) },
1474 {.index
= 1, .res
= DEFINE_RES_MEM(0xa0b02000, 0x100) },
1475 {.index
= 2, .res
= DEFINE_RES_MEM(0xc0c01000, 0x100) },
1476 {.index
= 3, .res
= DEFINE_RES_MEM(0xc0c09000, 0x100) },
1477 {.index
= 4, .res
= DEFINE_RES_MEM(0xd0d01000, 0x100) },
1480 static const struct of_unittest_expected_res of_unittest_reg_pci_expected_res
[] = {
1481 {.index
= 0, .res
= DEFINE_RES_MEM(0xe8001000, 0x1000) },
1482 {.index
= 1, .res
= DEFINE_RES_MEM(0xea002000, 0x2000) },
1485 static void __init
of_unittest_translate_addr(void)
1487 of_unittest_check_addr("/testcase-data/address-tests2/bus-2cell@10000000/device@100000",
1488 of_unittest_reg_2cell_expected_res
,
1489 ARRAY_SIZE(of_unittest_reg_2cell_expected_res
));
1491 of_unittest_check_addr("/testcase-data/address-tests2/bus-3cell@20000000/local-bus@100000/device@f1001000",
1492 of_unittest_reg_3cell_expected_res
,
1493 ARRAY_SIZE(of_unittest_reg_3cell_expected_res
));
1495 of_unittest_check_addr("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0/dev@e0000000",
1496 of_unittest_reg_pci_expected_res
,
1497 ARRAY_SIZE(of_unittest_reg_pci_expected_res
));
1500 static void __init
of_unittest_parse_interrupts(void)
1502 struct device_node
*np
;
1503 struct of_phandle_args args
;
1506 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
1509 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1511 pr_err("missing testcase data\n");
1515 for (i
= 0; i
< 4; i
++) {
1518 memset(&args
, 0, sizeof(args
));
1519 rc
= of_irq_parse_one(np
, i
, &args
);
1522 passed
&= (args
.args_count
== 1);
1523 passed
&= (args
.args
[0] == (i
+ 1));
1525 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1530 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1532 pr_err("missing testcase data\n");
1536 for (i
= 0; i
< 4; i
++) {
1539 memset(&args
, 0, sizeof(args
));
1540 rc
= of_irq_parse_one(np
, i
, &args
);
1542 /* Test the values from tests-phandle.dtsi */
1546 passed
&= (args
.args_count
== 1);
1547 passed
&= (args
.args
[0] == 9);
1551 passed
&= (args
.args_count
== 3);
1552 passed
&= (args
.args
[0] == 10);
1553 passed
&= (args
.args
[1] == 11);
1554 passed
&= (args
.args
[2] == 12);
1558 passed
&= (args
.args_count
== 2);
1559 passed
&= (args
.args
[0] == 13);
1560 passed
&= (args
.args
[1] == 14);
1564 passed
&= (args
.args_count
== 2);
1565 passed
&= (args
.args
[0] == 15);
1566 passed
&= (args
.args
[1] == 16);
1571 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1577 static void __init
of_unittest_parse_interrupts_extended(void)
1579 struct device_node
*np
;
1580 struct of_phandle_args args
;
1583 if (of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)
1586 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1588 pr_err("missing testcase data\n");
1592 for (i
= 0; i
< 7; i
++) {
1595 memset(&args
, 0, sizeof(args
));
1596 rc
= of_irq_parse_one(np
, i
, &args
);
1598 /* Test the values from tests-phandle.dtsi */
1602 passed
&= (args
.args_count
== 1);
1603 passed
&= (args
.args
[0] == 1);
1607 passed
&= (args
.args_count
== 3);
1608 passed
&= (args
.args
[0] == 2);
1609 passed
&= (args
.args
[1] == 3);
1610 passed
&= (args
.args
[2] == 4);
1614 passed
&= (args
.args_count
== 2);
1615 passed
&= (args
.args
[0] == 5);
1616 passed
&= (args
.args
[1] == 6);
1620 passed
&= (args
.args_count
== 1);
1621 passed
&= (args
.args
[0] == 9);
1625 passed
&= (args
.args_count
== 3);
1626 passed
&= (args
.args
[0] == 10);
1627 passed
&= (args
.args
[1] == 11);
1628 passed
&= (args
.args
[2] == 12);
1632 passed
&= (args
.args_count
== 2);
1633 passed
&= (args
.args
[0] == 13);
1634 passed
&= (args
.args
[1] == 14);
1638 * Tests child node that is missing property
1639 * #address-cells. See the comments in
1640 * drivers/of/unittest-data/tests-interrupts.dtsi
1641 * nodes intmap1 and interrupts-extended0
1644 passed
&= (args
.args_count
== 1);
1645 passed
&= (args
.args
[0] == 15);
1651 unittest(passed
, "index %i - data error on node %pOF rc=%i\n",
1657 static const struct of_device_id match_node_table
[] = {
1658 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
1659 { .data
= "B", .type
= "type1", }, /* followed by type alone */
1661 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
1662 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
1663 { .data
= "Cc", .name
= "name2", .type
= "type2", },
1665 { .data
= "E", .compatible
= "compat3" },
1666 { .data
= "G", .compatible
= "compat2", },
1667 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
1668 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
1669 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
1670 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
1677 } match_node_tests
[] = {
1678 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
1679 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
1680 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
1681 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
1682 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
1683 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
1684 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
1685 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
1686 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
1687 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
1688 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
1689 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
1692 static void __init
of_unittest_match_node(void)
1694 struct device_node
*np
;
1695 const struct of_device_id
*match
;
1698 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
1699 np
= of_find_node_by_path(match_node_tests
[i
].path
);
1701 unittest(0, "missing testcase node %s\n",
1702 match_node_tests
[i
].path
);
1706 match
= of_match_node(match_node_table
, np
);
1708 unittest(0, "%s didn't match anything\n",
1709 match_node_tests
[i
].path
);
1713 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
1714 unittest(0, "%s got wrong match. expected %s, got %s\n",
1715 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
1716 (const char *)match
->data
);
1719 unittest(1, "passed");
1723 static struct resource test_bus_res
= DEFINE_RES_MEM(0xfffffff8, 2);
1724 static const struct platform_device_info test_bus_info
= {
1725 .name
= "unittest-bus",
1727 static void __init
of_unittest_platform_populate(void)
1730 struct device_node
*np
, *child
, *grandchild
;
1731 struct platform_device
*pdev
, *test_bus
;
1732 const struct of_device_id match
[] = {
1733 { .compatible
= "test-device", },
1737 np
= of_find_node_by_path("/testcase-data");
1738 of_platform_default_populate(np
, NULL
, NULL
);
1740 /* Test that a missing irq domain returns -EPROBE_DEFER */
1741 np
= of_find_node_by_path("/testcase-data/testcase-device1");
1742 pdev
= of_find_device_by_node(np
);
1743 unittest(pdev
, "device 1 creation failed\n");
1745 if (!(of_irq_workarounds
& OF_IMAP_OLDWORLD_MAC
)) {
1746 irq
= platform_get_irq(pdev
, 0);
1747 unittest(irq
== -EPROBE_DEFER
,
1748 "device deferred probe failed - %d\n", irq
);
1750 /* Test that a parsing failure does not return -EPROBE_DEFER */
1751 np
= of_find_node_by_path("/testcase-data/testcase-device2");
1752 pdev
= of_find_device_by_node(np
);
1753 unittest(pdev
, "device 2 creation failed\n");
1755 EXPECT_BEGIN(KERN_INFO
,
1756 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1758 irq
= platform_get_irq(pdev
, 0);
1760 EXPECT_END(KERN_INFO
,
1761 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1763 unittest(irq
< 0 && irq
!= -EPROBE_DEFER
,
1764 "device parsing error failed - %d\n", irq
);
1767 np
= of_find_node_by_path("/testcase-data/platform-tests");
1768 unittest(np
, "No testcase data in device tree\n");
1772 test_bus
= platform_device_register_full(&test_bus_info
);
1773 rc
= PTR_ERR_OR_ZERO(test_bus
);
1774 unittest(!rc
, "testbus registration failed; rc=%i\n", rc
);
1779 test_bus
->dev
.of_node
= np
;
1782 * Add a dummy resource to the test bus node after it is
1783 * registered to catch problems with un-inserted resources. The
1784 * DT code doesn't insert the resources, and it has caused the
1785 * kernel to oops in the past. This makes sure the same bug
1786 * doesn't crop up again.
1788 platform_device_add_resources(test_bus
, &test_bus_res
, 1);
1790 of_platform_populate(np
, match
, NULL
, &test_bus
->dev
);
1791 for_each_child_of_node(np
, child
) {
1792 for_each_child_of_node(child
, grandchild
) {
1793 pdev
= of_find_device_by_node(grandchild
);
1795 "Could not create device for node '%pOFn'\n",
1797 platform_device_put(pdev
);
1801 of_platform_depopulate(&test_bus
->dev
);
1802 for_each_child_of_node(np
, child
) {
1803 for_each_child_of_node(child
, grandchild
)
1804 unittest(!of_find_device_by_node(grandchild
),
1805 "device didn't get destroyed '%pOFn'\n",
1809 platform_device_unregister(test_bus
);
1814 * update_node_properties - adds the properties
1815 * of np into dup node (present in live tree) and
1816 * updates parent of children of np to dup.
1818 * @np: node whose properties are being added to the live tree
1819 * @dup: node present in live tree to be updated
1821 static void update_node_properties(struct device_node
*np
,
1822 struct device_node
*dup
)
1824 struct property
*prop
;
1825 struct property
*save_next
;
1826 struct device_node
*child
;
1829 for_each_child_of_node(np
, child
)
1830 child
->parent
= dup
;
1833 * "unittest internal error: unable to add testdata property"
1835 * If this message reports a property in node '/__symbols__' then
1836 * the respective unittest overlay contains a label that has the
1837 * same name as a label in the live devicetree. The label will
1838 * be in the live devicetree only if the devicetree source was
1839 * compiled with the '-@' option. If you encounter this error,
1840 * please consider renaming __all__ of the labels in the unittest
1841 * overlay dts files with an odd prefix that is unlikely to be
1842 * used in a real devicetree.
1846 * open code for_each_property_of_node() because of_add_property()
1847 * sets prop->next to NULL
1849 for (prop
= np
->properties
; prop
!= NULL
; prop
= save_next
) {
1850 save_next
= prop
->next
;
1851 ret
= of_add_property(dup
, prop
);
1853 if (ret
== -EEXIST
&& !strcmp(prop
->name
, "name"))
1855 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1862 * attach_node_and_children - attaches nodes
1863 * and its children to live tree.
1864 * CAUTION: misleading function name - if node @np already exists in
1865 * the live tree then children of @np are *not* attached to the live
1866 * tree. This works for the current test devicetree nodes because such
1867 * nodes do not have child nodes.
1869 * @np: Node to attach to live tree
1871 static void attach_node_and_children(struct device_node
*np
)
1873 struct device_node
*next
, *dup
, *child
;
1874 unsigned long flags
;
1875 const char *full_name
;
1877 full_name
= kasprintf(GFP_KERNEL
, "%pOF", np
);
1881 if (!strcmp(full_name
, "/__local_fixups__") ||
1882 !strcmp(full_name
, "/__fixups__")) {
1887 dup
= of_find_node_by_path(full_name
);
1890 update_node_properties(np
, dup
);
1897 mutex_lock(&of_mutex
);
1898 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1899 np
->sibling
= np
->parent
->child
;
1900 np
->parent
->child
= np
;
1901 of_node_clear_flag(np
, OF_DETACHED
);
1902 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1904 __of_attach_node_sysfs(np
);
1905 mutex_unlock(&of_mutex
);
1908 next
= child
->sibling
;
1909 attach_node_and_children(child
);
1915 * unittest_data_add - Reads, copies data from
1916 * linked tree and attaches it to the live tree
1918 static int __init
unittest_data_add(void)
1920 void *unittest_data
;
1921 void *unittest_data_align
;
1922 struct device_node
*unittest_data_node
= NULL
, *np
;
1924 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1925 * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
1927 extern uint8_t __dtbo_testcases_begin
[];
1928 extern uint8_t __dtbo_testcases_end
[];
1929 const int size
= __dtbo_testcases_end
- __dtbo_testcases_begin
;
1934 pr_warn("%s: testcases is empty\n", __func__
);
1939 unittest_data
= kmalloc(size
+ FDT_ALIGN_SIZE
, GFP_KERNEL
);
1943 unittest_data_align
= PTR_ALIGN(unittest_data
, FDT_ALIGN_SIZE
);
1944 memcpy(unittest_data_align
, __dtbo_testcases_begin
, size
);
1946 ret
= of_fdt_unflatten_tree(unittest_data_align
, NULL
, &unittest_data_node
);
1948 pr_warn("%s: unflatten testcases tree failed\n", __func__
);
1949 kfree(unittest_data
);
1952 if (!unittest_data_node
) {
1953 pr_warn("%s: testcases tree is empty\n", __func__
);
1954 kfree(unittest_data
);
1959 * This lock normally encloses of_resolve_phandles()
1961 of_overlay_mutex_lock();
1963 rc
= of_resolve_phandles(unittest_data_node
);
1965 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
1966 of_overlay_mutex_unlock();
1970 /* attach the sub-tree to live tree */
1972 pr_warn("%s: no live tree to attach sub-tree\n", __func__
);
1973 kfree(unittest_data
);
1977 EXPECT_BEGIN(KERN_INFO
,
1978 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1980 np
= unittest_data_node
->child
;
1982 struct device_node
*next
= np
->sibling
;
1984 np
->parent
= of_root
;
1985 /* this will clear OF_DETACHED in np and children */
1986 attach_node_and_children(np
);
1990 EXPECT_END(KERN_INFO
,
1991 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1993 of_overlay_mutex_unlock();
1998 #ifdef CONFIG_OF_OVERLAY
1999 static int __init
overlay_data_apply(const char *overlay_name
, int *ovcs_id
);
2001 static int unittest_probe(struct platform_device
*pdev
)
2003 struct device
*dev
= &pdev
->dev
;
2004 struct device_node
*np
= dev
->of_node
;
2007 dev_err(dev
, "No OF data for device\n");
2012 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2014 of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
2019 static void unittest_remove(struct platform_device
*pdev
)
2021 struct device
*dev
= &pdev
->dev
;
2022 struct device_node
*np
= dev
->of_node
;
2024 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2027 static const struct of_device_id unittest_match
[] = {
2028 { .compatible
= "unittest", },
2032 static struct platform_driver unittest_driver
= {
2033 .probe
= unittest_probe
,
2034 .remove
= unittest_remove
,
2037 .of_match_table
= unittest_match
,
2041 /* get the platform device instantiated at the path */
2042 static struct platform_device
*of_path_to_platform_device(const char *path
)
2044 struct device_node
*np
;
2045 struct platform_device
*pdev
;
2047 np
= of_find_node_by_path(path
);
2051 pdev
= of_find_device_by_node(np
);
2057 /* find out if a platform device exists at that path */
2058 static int of_path_platform_device_exists(const char *path
)
2060 struct platform_device
*pdev
;
2062 pdev
= of_path_to_platform_device(path
);
2063 platform_device_put(pdev
);
2064 return pdev
!= NULL
;
2067 #ifdef CONFIG_OF_GPIO
2069 struct unittest_gpio_dev
{
2070 struct gpio_chip chip
;
2073 static int unittest_gpio_chip_request_count
;
2074 static int unittest_gpio_probe_count
;
2075 static int unittest_gpio_probe_pass_count
;
2077 static int unittest_gpio_chip_request(struct gpio_chip
*chip
, unsigned int offset
)
2079 unittest_gpio_chip_request_count
++;
2081 pr_debug("%s(): %s %d %d\n", __func__
, chip
->label
, offset
,
2082 unittest_gpio_chip_request_count
);
2086 static int unittest_gpio_probe(struct platform_device
*pdev
)
2088 struct unittest_gpio_dev
*devptr
;
2091 unittest_gpio_probe_count
++;
2093 devptr
= kzalloc(sizeof(*devptr
), GFP_KERNEL
);
2097 platform_set_drvdata(pdev
, devptr
);
2099 devptr
->chip
.fwnode
= dev_fwnode(&pdev
->dev
);
2100 devptr
->chip
.label
= "of-unittest-gpio";
2101 devptr
->chip
.base
= -1; /* dynamic allocation */
2102 devptr
->chip
.ngpio
= 5;
2103 devptr
->chip
.request
= unittest_gpio_chip_request
;
2105 ret
= gpiochip_add_data(&devptr
->chip
, NULL
);
2108 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr
->chip
.fwnode
, ret
);
2111 unittest_gpio_probe_pass_count
++;
2115 static void unittest_gpio_remove(struct platform_device
*pdev
)
2117 struct unittest_gpio_dev
*devptr
= platform_get_drvdata(pdev
);
2118 struct device
*dev
= &pdev
->dev
;
2120 dev_dbg(dev
, "%s for node @%pfw\n", __func__
, devptr
->chip
.fwnode
);
2122 if (devptr
->chip
.base
!= -1)
2123 gpiochip_remove(&devptr
->chip
);
2128 static const struct of_device_id unittest_gpio_id
[] = {
2129 { .compatible
= "unittest-gpio", },
2133 static struct platform_driver unittest_gpio_driver
= {
2134 .probe
= unittest_gpio_probe
,
2135 .remove
= unittest_gpio_remove
,
2137 .name
= "unittest-gpio",
2138 .of_match_table
= unittest_gpio_id
,
2142 static void __init
of_unittest_overlay_gpio(void)
2144 int chip_request_count
;
2145 int probe_pass_count
;
2149 * tests: apply overlays before registering driver
2150 * Similar to installing a driver as a module, the
2151 * driver is registered after applying the overlays.
2153 * The overlays are applied by overlay_data_apply()
2154 * instead of of_unittest_apply_overlay() so that they
2155 * will not be tracked. Thus they will not be removed
2156 * by of_unittest_remove_tracked_overlays().
2158 * - apply overlay_gpio_01
2159 * - apply overlay_gpio_02a
2160 * - apply overlay_gpio_02b
2163 * register driver will result in
2164 * - probe and processing gpio hog for overlay_gpio_01
2165 * - probe for overlay_gpio_02a
2166 * - processing gpio for overlay_gpio_02b
2169 probe_pass_count
= unittest_gpio_probe_pass_count
;
2170 chip_request_count
= unittest_gpio_chip_request_count
;
2173 * overlay_gpio_01 contains gpio node and child gpio hog node
2174 * overlay_gpio_02a contains gpio node
2175 * overlay_gpio_02b contains child gpio hog node
2178 unittest(overlay_data_apply("overlay_gpio_01", NULL
),
2179 "Adding overlay 'overlay_gpio_01' failed\n");
2181 unittest(overlay_data_apply("overlay_gpio_02a", NULL
),
2182 "Adding overlay 'overlay_gpio_02a' failed\n");
2184 unittest(overlay_data_apply("overlay_gpio_02b", NULL
),
2185 "Adding overlay 'overlay_gpio_02b' failed\n");
2187 ret
= platform_driver_register(&unittest_gpio_driver
);
2188 if (unittest(ret
== 0, "could not register unittest gpio driver\n"))
2191 unittest(probe_pass_count
+ 2 == unittest_gpio_probe_pass_count
,
2192 "unittest_gpio_probe() failed or not called\n");
2194 unittest(chip_request_count
+ 2 == unittest_gpio_chip_request_count
,
2195 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2196 unittest_gpio_chip_request_count
- chip_request_count
);
2199 * tests: apply overlays after registering driver
2201 * Similar to a driver built-in to the kernel, the
2202 * driver is registered before applying the overlays.
2204 * overlay_gpio_03 contains gpio node and child gpio hog node
2206 * - apply overlay_gpio_03
2208 * apply overlay will result in
2209 * - probe and processing gpio hog.
2212 probe_pass_count
= unittest_gpio_probe_pass_count
;
2213 chip_request_count
= unittest_gpio_chip_request_count
;
2215 /* overlay_gpio_03 contains gpio node and child gpio hog node */
2217 unittest(overlay_data_apply("overlay_gpio_03", NULL
),
2218 "Adding overlay 'overlay_gpio_03' failed\n");
2220 unittest(probe_pass_count
+ 1 == unittest_gpio_probe_pass_count
,
2221 "unittest_gpio_probe() failed or not called\n");
2223 unittest(chip_request_count
+ 1 == unittest_gpio_chip_request_count
,
2224 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2225 unittest_gpio_chip_request_count
- chip_request_count
);
2228 * overlay_gpio_04a contains gpio node
2230 * - apply overlay_gpio_04a
2232 * apply the overlay will result in
2233 * - probe for overlay_gpio_04a
2236 probe_pass_count
= unittest_gpio_probe_pass_count
;
2237 chip_request_count
= unittest_gpio_chip_request_count
;
2239 /* overlay_gpio_04a contains gpio node */
2241 unittest(overlay_data_apply("overlay_gpio_04a", NULL
),
2242 "Adding overlay 'overlay_gpio_04a' failed\n");
2244 unittest(probe_pass_count
+ 1 == unittest_gpio_probe_pass_count
,
2245 "unittest_gpio_probe() failed or not called\n");
2248 * overlay_gpio_04b contains child gpio hog node
2250 * - apply overlay_gpio_04b
2252 * apply the overlay will result in
2253 * - processing gpio for overlay_gpio_04b
2256 /* overlay_gpio_04b contains child gpio hog node */
2258 unittest(overlay_data_apply("overlay_gpio_04b", NULL
),
2259 "Adding overlay 'overlay_gpio_04b' failed\n");
2261 unittest(chip_request_count
+ 1 == unittest_gpio_chip_request_count
,
2262 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2263 unittest_gpio_chip_request_count
- chip_request_count
);
2268 static void __init
of_unittest_overlay_gpio(void)
2275 #if IS_BUILTIN(CONFIG_I2C)
2277 /* get the i2c client device instantiated at the path */
2278 static struct i2c_client
*of_path_to_i2c_client(const char *path
)
2280 struct device_node
*np
;
2281 struct i2c_client
*client
;
2283 np
= of_find_node_by_path(path
);
2287 client
= of_find_i2c_device_by_node(np
);
2293 /* find out if a i2c client device exists at that path */
2294 static int of_path_i2c_client_exists(const char *path
)
2296 struct i2c_client
*client
;
2298 client
= of_path_to_i2c_client(path
);
2300 put_device(&client
->dev
);
2301 return client
!= NULL
;
2304 static int of_path_i2c_client_exists(const char *path
)
2315 static int of_path_device_type_exists(const char *path
,
2316 enum overlay_type ovtype
)
2320 return of_path_platform_device_exists(path
);
2322 return of_path_i2c_client_exists(path
);
2327 static const char *unittest_path(int nr
, enum overlay_type ovtype
)
2330 static char buf
[256];
2334 base
= "/testcase-data/overlay-node/test-bus";
2337 base
= "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2343 snprintf(buf
, sizeof(buf
) - 1, "%s/test-unittest%d", base
, nr
);
2344 buf
[sizeof(buf
) - 1] = '\0';
2348 static int of_unittest_device_exists(int unittest_nr
, enum overlay_type ovtype
)
2352 path
= unittest_path(unittest_nr
, ovtype
);
2356 return of_path_platform_device_exists(path
);
2358 return of_path_i2c_client_exists(path
);
2363 static const char *overlay_name_from_nr(int nr
)
2365 static char buf
[256];
2367 snprintf(buf
, sizeof(buf
) - 1,
2369 buf
[sizeof(buf
) - 1] = '\0';
2374 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
2376 #define MAX_TRACK_OVCS_IDS 256
2378 static int track_ovcs_id
[MAX_TRACK_OVCS_IDS
];
2379 static int track_ovcs_id_overlay_nr
[MAX_TRACK_OVCS_IDS
];
2380 static int track_ovcs_id_cnt
;
2382 static void of_unittest_track_overlay(int ovcs_id
, int overlay_nr
)
2384 if (WARN_ON(track_ovcs_id_cnt
>= MAX_TRACK_OVCS_IDS
))
2387 track_ovcs_id
[track_ovcs_id_cnt
] = ovcs_id
;
2388 track_ovcs_id_overlay_nr
[track_ovcs_id_cnt
] = overlay_nr
;
2389 track_ovcs_id_cnt
++;
2392 static void of_unittest_untrack_overlay(int ovcs_id
)
2394 if (WARN_ON(track_ovcs_id_cnt
< 1))
2397 track_ovcs_id_cnt
--;
2399 /* If out of synch then test is broken. Do not try to recover. */
2400 WARN_ON(track_ovcs_id
[track_ovcs_id_cnt
] != ovcs_id
);
2403 static void of_unittest_remove_tracked_overlays(void)
2405 int ret
, ovcs_id
, overlay_nr
, save_ovcs_id
;
2406 const char *overlay_name
;
2408 while (track_ovcs_id_cnt
> 0) {
2410 ovcs_id
= track_ovcs_id
[track_ovcs_id_cnt
- 1];
2411 overlay_nr
= track_ovcs_id_overlay_nr
[track_ovcs_id_cnt
- 1];
2412 save_ovcs_id
= ovcs_id
;
2413 ret
= of_overlay_remove(&ovcs_id
);
2414 if (ret
== -ENODEV
) {
2415 overlay_name
= overlay_name_from_nr(overlay_nr
);
2416 pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2417 __func__
, overlay_name
, ret
);
2419 of_unittest_untrack_overlay(save_ovcs_id
);
2424 static int __init
of_unittest_apply_overlay(int overlay_nr
, int *ovcs_id
)
2427 * The overlay will be tracked, thus it will be removed
2428 * by of_unittest_remove_tracked_overlays().
2431 const char *overlay_name
;
2433 overlay_name
= overlay_name_from_nr(overlay_nr
);
2435 if (!overlay_data_apply(overlay_name
, ovcs_id
)) {
2436 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2439 of_unittest_track_overlay(*ovcs_id
, overlay_nr
);
2444 static int __init
__of_unittest_apply_overlay_check(int overlay_nr
,
2445 int unittest_nr
, int before
, int after
,
2446 enum overlay_type ovtype
)
2450 /* unittest device must be in before state */
2451 if (of_unittest_device_exists(unittest_nr
, ovtype
) != before
) {
2452 unittest(0, "%s with device @\"%s\" %s\n",
2453 overlay_name_from_nr(overlay_nr
),
2454 unittest_path(unittest_nr
, ovtype
),
2455 !before
? "enabled" : "disabled");
2459 /* apply the overlay */
2461 ret
= of_unittest_apply_overlay(overlay_nr
, &ovcs_id
);
2463 /* of_unittest_apply_overlay already called unittest() */
2467 /* unittest device must be in after state */
2468 if (of_unittest_device_exists(unittest_nr
, ovtype
) != after
) {
2469 unittest(0, "%s with device @\"%s\" %s\n",
2470 overlay_name_from_nr(overlay_nr
),
2471 unittest_path(unittest_nr
, ovtype
),
2472 !after
? "enabled" : "disabled");
2479 /* apply an overlay while checking before and after states */
2480 static int __init
of_unittest_apply_overlay_check(int overlay_nr
,
2481 int unittest_nr
, int before
, int after
,
2482 enum overlay_type ovtype
)
2484 int ovcs_id
= __of_unittest_apply_overlay_check(overlay_nr
,
2485 unittest_nr
, before
, after
, ovtype
);
2492 /* apply an overlay and then revert it while checking before, after states */
2493 static int __init
of_unittest_apply_revert_overlay_check(int overlay_nr
,
2494 int unittest_nr
, int before
, int after
,
2495 enum overlay_type ovtype
)
2497 int ret
, ovcs_id
, save_ovcs_id
;
2499 ovcs_id
= __of_unittest_apply_overlay_check(overlay_nr
, unittest_nr
,
2500 before
, after
, ovtype
);
2504 /* remove the overlay */
2505 save_ovcs_id
= ovcs_id
;
2506 ret
= of_overlay_remove(&ovcs_id
);
2508 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2509 overlay_name_from_nr(overlay_nr
),
2510 unittest_path(unittest_nr
, ovtype
));
2513 of_unittest_untrack_overlay(save_ovcs_id
);
2515 /* unittest device must be again in before state */
2516 if (of_unittest_device_exists(unittest_nr
, ovtype
) != before
) {
2517 unittest(0, "%s with device @\"%s\" %s\n",
2518 overlay_name_from_nr(overlay_nr
),
2519 unittest_path(unittest_nr
, ovtype
),
2520 !before
? "enabled" : "disabled");
2527 /* test activation of device */
2528 static void __init
of_unittest_overlay_0(void)
2532 EXPECT_BEGIN(KERN_INFO
,
2533 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2535 /* device should enable */
2536 ret
= of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY
);
2538 EXPECT_END(KERN_INFO
,
2539 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2544 unittest(1, "overlay test %d passed\n", 0);
2547 /* test deactivation of device */
2548 static void __init
of_unittest_overlay_1(void)
2552 EXPECT_BEGIN(KERN_INFO
,
2553 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2555 /* device should disable */
2556 ret
= of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY
);
2558 EXPECT_END(KERN_INFO
,
2559 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2564 unittest(1, "overlay test %d passed\n", 1);
2568 /* test activation of device */
2569 static void __init
of_unittest_overlay_2(void)
2573 EXPECT_BEGIN(KERN_INFO
,
2574 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2576 /* device should enable */
2577 ret
= of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY
);
2579 EXPECT_END(KERN_INFO
,
2580 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2584 unittest(1, "overlay test %d passed\n", 2);
2587 /* test deactivation of device */
2588 static void __init
of_unittest_overlay_3(void)
2592 EXPECT_BEGIN(KERN_INFO
,
2593 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2595 /* device should disable */
2596 ret
= of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY
);
2598 EXPECT_END(KERN_INFO
,
2599 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2604 unittest(1, "overlay test %d passed\n", 3);
2607 /* test activation of a full device node */
2608 static void __init
of_unittest_overlay_4(void)
2610 /* device should disable */
2611 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY
))
2614 unittest(1, "overlay test %d passed\n", 4);
2617 /* test overlay apply/revert sequence */
2618 static void __init
of_unittest_overlay_5(void)
2622 EXPECT_BEGIN(KERN_INFO
,
2623 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2625 /* device should disable */
2626 ret
= of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY
);
2628 EXPECT_END(KERN_INFO
,
2629 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2634 unittest(1, "overlay test %d passed\n", 5);
2637 /* test overlay application in sequence */
2638 static void __init
of_unittest_overlay_6(void)
2640 int i
, save_ovcs_id
[2], ovcs_id
;
2641 int overlay_nr
= 6, unittest_nr
= 6;
2642 int before
= 0, after
= 1;
2643 const char *overlay_name
;
2647 /* unittest device must be in before state */
2648 for (i
= 0; i
< 2; i
++) {
2649 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
2651 unittest(0, "%s with device @\"%s\" %s\n",
2652 overlay_name_from_nr(overlay_nr
+ i
),
2653 unittest_path(unittest_nr
+ i
,
2655 !before
? "enabled" : "disabled");
2660 /* apply the overlays */
2662 EXPECT_BEGIN(KERN_INFO
,
2663 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2665 overlay_name
= overlay_name_from_nr(overlay_nr
+ 0);
2667 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2670 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2673 save_ovcs_id
[0] = ovcs_id
;
2674 of_unittest_track_overlay(ovcs_id
, overlay_nr
+ 0);
2676 EXPECT_END(KERN_INFO
,
2677 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2679 EXPECT_BEGIN(KERN_INFO
,
2680 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2682 overlay_name
= overlay_name_from_nr(overlay_nr
+ 1);
2684 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2687 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2690 save_ovcs_id
[1] = ovcs_id
;
2691 of_unittest_track_overlay(ovcs_id
, overlay_nr
+ 1);
2693 EXPECT_END(KERN_INFO
,
2694 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2697 for (i
= 0; i
< 2; i
++) {
2698 /* unittest device must be in after state */
2699 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
2701 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2702 overlay_name_from_nr(overlay_nr
+ i
),
2703 unittest_path(unittest_nr
+ i
,
2705 !after
? "enabled" : "disabled");
2710 for (i
= 1; i
>= 0; i
--) {
2711 ovcs_id
= save_ovcs_id
[i
];
2712 if (of_overlay_remove(&ovcs_id
)) {
2713 unittest(0, "%s failed destroy @\"%s\"\n",
2714 overlay_name_from_nr(overlay_nr
+ i
),
2715 unittest_path(unittest_nr
+ i
,
2719 of_unittest_untrack_overlay(save_ovcs_id
[i
]);
2722 for (i
= 0; i
< 2; i
++) {
2723 /* unittest device must be again in before state */
2724 if (of_unittest_device_exists(unittest_nr
+ i
, PDEV_OVERLAY
)
2726 unittest(0, "%s with device @\"%s\" %s\n",
2727 overlay_name_from_nr(overlay_nr
+ i
),
2728 unittest_path(unittest_nr
+ i
,
2730 !before
? "enabled" : "disabled");
2735 unittest(1, "overlay test %d passed\n", 6);
2739 /* test overlay application in sequence */
2740 static void __init
of_unittest_overlay_8(void)
2742 int i
, save_ovcs_id
[2], ovcs_id
;
2743 int overlay_nr
= 8, unittest_nr
= 8;
2744 const char *overlay_name
;
2747 /* we don't care about device state in this test */
2749 EXPECT_BEGIN(KERN_INFO
,
2750 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2752 overlay_name
= overlay_name_from_nr(overlay_nr
+ 0);
2754 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2756 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2758 EXPECT_END(KERN_INFO
,
2759 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2764 save_ovcs_id
[0] = ovcs_id
;
2765 of_unittest_track_overlay(ovcs_id
, overlay_nr
+ 0);
2767 overlay_name
= overlay_name_from_nr(overlay_nr
+ 1);
2769 EXPECT_BEGIN(KERN_INFO
,
2770 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2772 /* apply the overlays */
2773 ret
= overlay_data_apply(overlay_name
, &ovcs_id
);
2775 EXPECT_END(KERN_INFO
,
2776 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2779 unittest(0, "could not apply overlay \"%s\"\n", overlay_name
);
2783 save_ovcs_id
[1] = ovcs_id
;
2784 of_unittest_track_overlay(ovcs_id
, overlay_nr
+ 1);
2786 /* now try to remove first overlay (it should fail) */
2787 ovcs_id
= save_ovcs_id
[0];
2789 EXPECT_BEGIN(KERN_INFO
,
2790 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2792 EXPECT_BEGIN(KERN_INFO
,
2793 "OF: overlay: overlay #6 is not topmost");
2795 ret
= of_overlay_remove(&ovcs_id
);
2797 EXPECT_END(KERN_INFO
,
2798 "OF: overlay: overlay #6 is not topmost");
2800 EXPECT_END(KERN_INFO
,
2801 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2805 * Should never get here. If we do, expect a lot of
2806 * subsequent tracking and overlay removal related errors.
2808 unittest(0, "%s was destroyed @\"%s\"\n",
2809 overlay_name_from_nr(overlay_nr
+ 0),
2810 unittest_path(unittest_nr
,
2815 /* removing them in order should work */
2816 for (i
= 1; i
>= 0; i
--) {
2817 ovcs_id
= save_ovcs_id
[i
];
2818 if (of_overlay_remove(&ovcs_id
)) {
2819 unittest(0, "%s not destroyed @\"%s\"\n",
2820 overlay_name_from_nr(overlay_nr
+ i
),
2821 unittest_path(unittest_nr
,
2825 of_unittest_untrack_overlay(save_ovcs_id
[i
]);
2828 unittest(1, "overlay test %d passed\n", 8);
2831 /* test insertion of a bus with parent devices */
2832 static void __init
of_unittest_overlay_10(void)
2837 /* device should disable */
2838 ret
= of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY
);
2840 if (unittest(ret
== 0,
2841 "overlay test %d failed; overlay application\n", 10))
2844 child_path
= kasprintf(GFP_KERNEL
, "%s/test-unittest101",
2845 unittest_path(10, PDEV_OVERLAY
));
2846 if (unittest(child_path
, "overlay test %d failed; kasprintf\n", 10))
2849 ret
= of_path_device_type_exists(child_path
, PDEV_OVERLAY
);
2852 unittest(ret
, "overlay test %d failed; no child device\n", 10);
2855 /* test insertion of a bus with parent devices (and revert) */
2856 static void __init
of_unittest_overlay_11(void)
2860 /* device should disable */
2861 ret
= of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2864 unittest(ret
== 0, "overlay test %d failed; overlay apply\n", 11);
2867 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2869 struct unittest_i2c_bus_data
{
2870 struct platform_device
*pdev
;
2871 struct i2c_adapter adap
;
2874 static int unittest_i2c_master_xfer(struct i2c_adapter
*adap
,
2875 struct i2c_msg
*msgs
, int num
)
2877 struct unittest_i2c_bus_data
*std
= i2c_get_adapdata(adap
);
2884 static u32
unittest_i2c_functionality(struct i2c_adapter
*adap
)
2886 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
2889 static const struct i2c_algorithm unittest_i2c_algo
= {
2890 .master_xfer
= unittest_i2c_master_xfer
,
2891 .functionality
= unittest_i2c_functionality
,
2894 static int unittest_i2c_bus_probe(struct platform_device
*pdev
)
2896 struct device
*dev
= &pdev
->dev
;
2897 struct device_node
*np
= dev
->of_node
;
2898 struct unittest_i2c_bus_data
*std
;
2899 struct i2c_adapter
*adap
;
2903 dev_err(dev
, "No OF data for device\n");
2908 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2910 std
= devm_kzalloc(dev
, sizeof(*std
), GFP_KERNEL
);
2914 /* link them together */
2916 platform_set_drvdata(pdev
, std
);
2919 i2c_set_adapdata(adap
, std
);
2921 strscpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
2922 adap
->class = I2C_CLASS_DEPRECATED
;
2923 adap
->algo
= &unittest_i2c_algo
;
2924 adap
->dev
.parent
= dev
;
2925 adap
->dev
.of_node
= dev
->of_node
;
2926 adap
->timeout
= 5 * HZ
;
2929 ret
= i2c_add_numbered_adapter(adap
);
2931 dev_err(dev
, "Failed to add I2C adapter\n");
2938 static void unittest_i2c_bus_remove(struct platform_device
*pdev
)
2940 struct device
*dev
= &pdev
->dev
;
2941 struct device_node
*np
= dev
->of_node
;
2942 struct unittest_i2c_bus_data
*std
= platform_get_drvdata(pdev
);
2944 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2945 i2c_del_adapter(&std
->adap
);
2948 static const struct of_device_id unittest_i2c_bus_match
[] = {
2949 { .compatible
= "unittest-i2c-bus", },
2953 static struct platform_driver unittest_i2c_bus_driver
= {
2954 .probe
= unittest_i2c_bus_probe
,
2955 .remove
= unittest_i2c_bus_remove
,
2957 .name
= "unittest-i2c-bus",
2958 .of_match_table
= unittest_i2c_bus_match
,
2962 static int unittest_i2c_dev_probe(struct i2c_client
*client
)
2964 struct device
*dev
= &client
->dev
;
2965 struct device_node
*np
= client
->dev
.of_node
;
2968 dev_err(dev
, "No OF node\n");
2972 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2977 static void unittest_i2c_dev_remove(struct i2c_client
*client
)
2979 struct device
*dev
= &client
->dev
;
2980 struct device_node
*np
= client
->dev
.of_node
;
2982 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
2985 static const struct i2c_device_id unittest_i2c_dev_id
[] = {
2986 { .name
= "unittest-i2c-dev" },
2990 static struct i2c_driver unittest_i2c_dev_driver
= {
2992 .name
= "unittest-i2c-dev",
2994 .probe
= unittest_i2c_dev_probe
,
2995 .remove
= unittest_i2c_dev_remove
,
2996 .id_table
= unittest_i2c_dev_id
,
2999 #if IS_BUILTIN(CONFIG_I2C_MUX)
3001 static int unittest_i2c_mux_select_chan(struct i2c_mux_core
*muxc
, u32 chan
)
3006 static int unittest_i2c_mux_probe(struct i2c_client
*client
)
3009 struct device
*dev
= &client
->dev
;
3010 struct i2c_adapter
*adap
= client
->adapter
;
3011 struct device_node
*np
= client
->dev
.of_node
, *child
;
3012 struct i2c_mux_core
*muxc
;
3015 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
3018 dev_err(dev
, "No OF node\n");
3023 for_each_child_of_node(np
, child
) {
3024 if (of_property_read_u32(child
, "reg", ®
))
3026 if (max_reg
== (u32
)-1 || reg
> max_reg
)
3029 nchans
= max_reg
== (u32
)-1 ? 0 : max_reg
+ 1;
3031 dev_err(dev
, "No channels\n");
3035 muxc
= i2c_mux_alloc(adap
, dev
, nchans
, 0, 0,
3036 unittest_i2c_mux_select_chan
, NULL
);
3039 for (i
= 0; i
< nchans
; i
++) {
3040 if (i2c_mux_add_adapter(muxc
, 0, i
)) {
3041 dev_err(dev
, "Failed to register mux #%d\n", i
);
3042 i2c_mux_del_adapters(muxc
);
3047 i2c_set_clientdata(client
, muxc
);
3052 static void unittest_i2c_mux_remove(struct i2c_client
*client
)
3054 struct device
*dev
= &client
->dev
;
3055 struct device_node
*np
= client
->dev
.of_node
;
3056 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
3058 dev_dbg(dev
, "%s for node @%pOF\n", __func__
, np
);
3059 i2c_mux_del_adapters(muxc
);
3062 static const struct i2c_device_id unittest_i2c_mux_id
[] = {
3063 { .name
= "unittest-i2c-mux" },
3067 static struct i2c_driver unittest_i2c_mux_driver
= {
3069 .name
= "unittest-i2c-mux",
3071 .probe
= unittest_i2c_mux_probe
,
3072 .remove
= unittest_i2c_mux_remove
,
3073 .id_table
= unittest_i2c_mux_id
,
3078 static int of_unittest_overlay_i2c_init(void)
3082 ret
= i2c_add_driver(&unittest_i2c_dev_driver
);
3083 if (unittest(ret
== 0,
3084 "could not register unittest i2c device driver\n"))
3087 ret
= platform_driver_register(&unittest_i2c_bus_driver
);
3089 if (unittest(ret
== 0,
3090 "could not register unittest i2c bus driver\n"))
3093 #if IS_BUILTIN(CONFIG_I2C_MUX)
3095 EXPECT_BEGIN(KERN_INFO
,
3096 "i2c i2c-1: Added multiplexed i2c bus 2");
3098 ret
= i2c_add_driver(&unittest_i2c_mux_driver
);
3100 EXPECT_END(KERN_INFO
,
3101 "i2c i2c-1: Added multiplexed i2c bus 2");
3103 if (unittest(ret
== 0,
3104 "could not register unittest i2c mux driver\n"))
3111 static void of_unittest_overlay_i2c_cleanup(void)
3113 #if IS_BUILTIN(CONFIG_I2C_MUX)
3114 i2c_del_driver(&unittest_i2c_mux_driver
);
3116 platform_driver_unregister(&unittest_i2c_bus_driver
);
3117 i2c_del_driver(&unittest_i2c_dev_driver
);
3120 static void __init
of_unittest_overlay_i2c_12(void)
3124 /* device should enable */
3125 EXPECT_BEGIN(KERN_INFO
,
3126 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
3128 ret
= of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY
);
3130 EXPECT_END(KERN_INFO
,
3131 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
3136 unittest(1, "overlay test %d passed\n", 12);
3139 /* test deactivation of device */
3140 static void __init
of_unittest_overlay_i2c_13(void)
3144 EXPECT_BEGIN(KERN_INFO
,
3145 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
3147 /* device should disable */
3148 ret
= of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY
);
3150 EXPECT_END(KERN_INFO
,
3151 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
3156 unittest(1, "overlay test %d passed\n", 13);
3159 /* just check for i2c mux existence */
3160 static void of_unittest_overlay_i2c_14(void)
3164 static void __init
of_unittest_overlay_i2c_15(void)
3168 /* device should enable */
3169 EXPECT_BEGIN(KERN_INFO
,
3170 "i2c i2c-1: Added multiplexed i2c bus 3");
3172 ret
= of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY
);
3174 EXPECT_END(KERN_INFO
,
3175 "i2c i2c-1: Added multiplexed i2c bus 3");
3180 unittest(1, "overlay test %d passed\n", 15);
3185 static inline void of_unittest_overlay_i2c_14(void) { }
3186 static inline void of_unittest_overlay_i2c_15(void) { }
3190 static int of_notify(struct notifier_block
*nb
, unsigned long action
,
3193 struct of_overlay_notify_data
*nd
= arg
;
3194 struct device_node
*found
;
3198 * For overlay_16 .. overlay_19, check that returning an error
3199 * works for each of the actions by setting an arbitrary return
3200 * error number that matches the test number. e.g. for unittest16,
3201 * ret = -EBUSY which is -16.
3203 * OVERLAY_INFO() for the overlays is declared to expect the same
3204 * error number, so overlay_data_apply() will return no error.
3206 * overlay_20 will return NOTIFY_DONE
3210 of_node_get(nd
->overlay
);
3214 case OF_OVERLAY_PRE_APPLY
:
3215 found
= of_find_node_by_name(nd
->overlay
, "test-unittest16");
3222 case OF_OVERLAY_POST_APPLY
:
3223 found
= of_find_node_by_name(nd
->overlay
, "test-unittest17");
3230 case OF_OVERLAY_PRE_REMOVE
:
3231 found
= of_find_node_by_name(nd
->overlay
, "test-unittest18");
3238 case OF_OVERLAY_POST_REMOVE
:
3239 found
= of_find_node_by_name(nd
->overlay
, "test-unittest19");
3246 default: /* should not happen */
3247 of_node_put(nd
->overlay
);
3253 return notifier_from_errno(ret
);
3258 static struct notifier_block of_nb
= {
3259 .notifier_call
= of_notify
,
3262 static void __init
of_unittest_overlay_notify(void)
3267 ret
= of_overlay_notifier_register(&of_nb
);
3269 "of_overlay_notifier_register() failed, ret = %d\n", ret
);
3274 * The overlays are applied by overlay_data_apply()
3275 * instead of of_unittest_apply_overlay() so that they
3276 * will not be tracked. Thus they will not be removed
3277 * by of_unittest_remove_tracked_overlays().
3279 * Applying overlays 16 - 19 will each trigger an error for a
3280 * different action in of_notify().
3282 * Applying overlay 20 will not trigger any error in of_notify().
3285 /* --- overlay 16 --- */
3287 EXPECT_BEGIN(KERN_INFO
, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3289 unittest(overlay_data_apply("overlay_16", &ovcs_id
),
3290 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
3292 EXPECT_END(KERN_INFO
, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3294 unittest(ovcs_id
, "ovcs_id not created for overlay_16\n");
3296 /* --- overlay 17 --- */
3298 EXPECT_BEGIN(KERN_INFO
, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3300 unittest(overlay_data_apply("overlay_17", &ovcs_id
),
3301 "test OF_OVERLAY_POST_APPLY notify injected error\n");
3303 EXPECT_END(KERN_INFO
, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3305 unittest(ovcs_id
, "ovcs_id not created for overlay_17\n");
3307 /* --- overlay 18 --- */
3309 unittest(overlay_data_apply("overlay_18", &ovcs_id
),
3310 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3312 unittest(ovcs_id
, "ovcs_id not created for overlay_18\n");
3315 EXPECT_BEGIN(KERN_INFO
, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3317 ret
= of_overlay_remove(&ovcs_id
);
3318 EXPECT_END(KERN_INFO
, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3319 if (ret
== -EXDEV
) {
3321 * change set ovcs_id should still exist
3323 unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3325 unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3328 unittest(1, "ovcs_id not created for overlay_18\n");
3331 unittest(ovcs_id
, "ovcs_id removed for overlay_18\n");
3333 /* --- overlay 19 --- */
3335 unittest(overlay_data_apply("overlay_19", &ovcs_id
),
3336 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3338 unittest(ovcs_id
, "ovcs_id not created for overlay_19\n");
3341 EXPECT_BEGIN(KERN_INFO
, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3342 ret
= of_overlay_remove(&ovcs_id
);
3343 EXPECT_END(KERN_INFO
, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3345 unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3347 unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3349 unittest(1, "ovcs_id removed for overlay_19\n");
3352 unittest(!ovcs_id
, "changeset ovcs_id = %d not removed for overlay_19\n",
3355 /* --- overlay 20 --- */
3357 unittest(overlay_data_apply("overlay_20", &ovcs_id
),
3358 "overlay notify no injected error\n");
3361 ret
= of_overlay_remove(&ovcs_id
);
3363 unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3366 unittest(1, "ovcs_id not created for overlay_20\n");
3369 unittest(!of_overlay_notifier_unregister(&of_nb
),
3370 "of_overlay_notifier_unregister() failed, ret = %d\n", ret
);
3373 static void __init
of_unittest_overlay(void)
3375 struct device_node
*bus_np
= NULL
;
3378 if (platform_driver_register(&unittest_driver
)) {
3379 unittest(0, "could not register unittest driver\n");
3383 bus_np
= of_find_node_by_path(bus_path
);
3384 if (bus_np
== NULL
) {
3385 unittest(0, "could not find bus_path \"%s\"\n", bus_path
);
3389 if (of_platform_default_populate(bus_np
, NULL
, NULL
)) {
3390 unittest(0, "could not populate bus @ \"%s\"\n", bus_path
);
3394 if (!of_unittest_device_exists(100, PDEV_OVERLAY
)) {
3395 unittest(0, "could not find unittest0 @ \"%s\"\n",
3396 unittest_path(100, PDEV_OVERLAY
));
3400 if (of_unittest_device_exists(101, PDEV_OVERLAY
)) {
3401 unittest(0, "unittest1 @ \"%s\" should not exist\n",
3402 unittest_path(101, PDEV_OVERLAY
));
3406 unittest(1, "basic infrastructure of overlays passed");
3408 /* tests in sequence */
3409 of_unittest_overlay_0();
3410 of_unittest_overlay_1();
3411 of_unittest_overlay_2();
3412 of_unittest_overlay_3();
3413 of_unittest_overlay_4();
3414 for (i
= 0; i
< 3; i
++)
3415 of_unittest_overlay_5();
3416 of_unittest_overlay_6();
3417 of_unittest_overlay_8();
3419 of_unittest_overlay_10();
3420 of_unittest_overlay_11();
3422 #if IS_BUILTIN(CONFIG_I2C)
3423 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3426 of_unittest_overlay_i2c_12();
3427 of_unittest_overlay_i2c_13();
3428 of_unittest_overlay_i2c_14();
3429 of_unittest_overlay_i2c_15();
3431 of_unittest_overlay_i2c_cleanup();
3434 of_unittest_overlay_gpio();
3436 of_unittest_remove_tracked_overlays();
3438 of_unittest_overlay_notify();
3441 of_node_put(bus_np
);
3445 static inline void __init
of_unittest_overlay(void) { }
3448 static void __init
of_unittest_lifecycle(void)
3450 #ifdef CONFIG_OF_DYNAMIC
3451 unsigned int refcount
;
3452 int found_refcount_one
= 0;
3454 struct device_node
*np
;
3455 struct device_node
*prev_sibling
, *next_sibling
;
3456 const char *refcount_path
= "/testcase-data/refcount-node";
3457 const char *refcount_parent_path
= "/testcase-data";
3460 * Node lifecycle tests, non-dynamic node:
3462 * - Decrementing refcount to zero via of_node_put() should cause the
3463 * attempt to free the node memory by of_node_release() to fail
3464 * because the node is not a dynamic node.
3466 * - Decrementing refcount past zero should result in additional
3470 np
= of_find_node_by_path(refcount_path
);
3471 unittest(np
, "find refcount_path \"%s\"\n", refcount_path
);
3473 goto out_skip_tests
;
3475 while (!found_refcount_one
) {
3477 if (put_count
++ > 10) {
3478 unittest(0, "guardrail to avoid infinite loop\n");
3479 goto out_skip_tests
;
3482 refcount
= kref_read(&np
->kobj
.kref
);
3484 found_refcount_one
= 1;
3489 EXPECT_BEGIN(KERN_INFO
, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3492 * refcount is now one, decrementing to zero will result in a call to
3493 * of_node_release() to free the node's memory, which should result
3496 unittest(1, "/testcase-data/refcount-node is one");
3499 EXPECT_END(KERN_INFO
, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3503 * expect stack trace for subsequent of_node_put():
3504 * __refcount_sub_and_test() calls:
3505 * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3507 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3508 * the first three lines, and the last line.
3510 EXPECT_BEGIN(KERN_INFO
, "------------[ cut here ]------------");
3511 EXPECT_BEGIN(KERN_INFO
, "WARNING: <<all>>");
3512 EXPECT_BEGIN(KERN_INFO
, "refcount_t: underflow; use-after-free.");
3513 EXPECT_BEGIN(KERN_INFO
, "---[ end trace <<int>> ]---");
3515 /* refcount is now zero, this should fail */
3516 unittest(1, "/testcase-data/refcount-node is zero");
3519 EXPECT_END(KERN_INFO
, "---[ end trace <<int>> ]---");
3520 EXPECT_END(KERN_INFO
, "refcount_t: underflow; use-after-free.");
3521 EXPECT_END(KERN_INFO
, "WARNING: <<all>>");
3522 EXPECT_END(KERN_INFO
, "------------[ cut here ]------------");
3525 * Q. do we expect to get yet another warning?
3526 * A. no, the WARNING is from WARN_ONCE()
3528 EXPECT_NOT_BEGIN(KERN_INFO
, "------------[ cut here ]------------");
3529 EXPECT_NOT_BEGIN(KERN_INFO
, "WARNING: <<all>>");
3530 EXPECT_NOT_BEGIN(KERN_INFO
, "refcount_t: underflow; use-after-free.");
3531 EXPECT_NOT_BEGIN(KERN_INFO
, "---[ end trace <<int>> ]---");
3533 unittest(1, "/testcase-data/refcount-node is zero, second time");
3536 EXPECT_NOT_END(KERN_INFO
, "---[ end trace <<int>> ]---");
3537 EXPECT_NOT_END(KERN_INFO
, "refcount_t: underflow; use-after-free.");
3538 EXPECT_NOT_END(KERN_INFO
, "WARNING: <<all>>");
3539 EXPECT_NOT_END(KERN_INFO
, "------------[ cut here ]------------");
3542 * refcount of zero will trigger stack traces from any further
3543 * attempt to of_node_get() node "refcount-node". One example of
3544 * this is where of_unittest_check_node_linkage() will recursively
3545 * scan the tree, with 'for_each_child_of_node()' doing an
3546 * of_node_get() of the children of a node.
3548 * Prevent the stack trace by removing node "refcount-node" from
3549 * its parent's child list.
3551 * WARNING: EVIL, EVIL, EVIL:
3553 * Directly manipulate the child list of node /testcase-data to
3554 * remove child refcount-node. This is ignoring all proper methods
3555 * of removing a child and will leak a small amount of memory.
3558 np
= of_find_node_by_path(refcount_parent_path
);
3559 unittest(np
, "find refcount_parent_path \"%s\"\n", refcount_parent_path
);
3560 unittest(np
, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3564 prev_sibling
= np
->child
;
3565 next_sibling
= prev_sibling
->sibling
;
3566 if (!strcmp(prev_sibling
->full_name
, "refcount-node")) {
3567 np
->child
= next_sibling
;
3568 next_sibling
= next_sibling
->sibling
;
3570 while (next_sibling
) {
3571 if (!strcmp(next_sibling
->full_name
, "refcount-node"))
3572 prev_sibling
->sibling
= next_sibling
->sibling
;
3573 prev_sibling
= next_sibling
;
3574 next_sibling
= next_sibling
->sibling
;
3582 unittest(0, "One or more lifecycle tests skipped\n");
3585 #ifdef CONFIG_OF_OVERLAY
3588 * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3589 * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
3592 #define OVERLAY_INFO_EXTERN(overlay_name) \
3593 extern uint8_t __dtbo_##overlay_name##_begin[]; \
3594 extern uint8_t __dtbo_##overlay_name##_end[]
3596 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3597 { .dtbo_begin = __dtbo_##overlay_name##_begin, \
3598 .dtbo_end = __dtbo_##overlay_name##_end, \
3599 .expected_result = expected, \
3600 .expected_result_remove = expected_remove, \
3601 .name = #overlay_name, \
3604 struct overlay_info
{
3605 uint8_t *dtbo_begin
;
3607 int expected_result
;
3608 int expected_result_remove
; /* if apply failed */
3613 OVERLAY_INFO_EXTERN(overlay_base
);
3614 OVERLAY_INFO_EXTERN(overlay
);
3615 OVERLAY_INFO_EXTERN(overlay_0
);
3616 OVERLAY_INFO_EXTERN(overlay_1
);
3617 OVERLAY_INFO_EXTERN(overlay_2
);
3618 OVERLAY_INFO_EXTERN(overlay_3
);
3619 OVERLAY_INFO_EXTERN(overlay_4
);
3620 OVERLAY_INFO_EXTERN(overlay_5
);
3621 OVERLAY_INFO_EXTERN(overlay_6
);
3622 OVERLAY_INFO_EXTERN(overlay_7
);
3623 OVERLAY_INFO_EXTERN(overlay_8
);
3624 OVERLAY_INFO_EXTERN(overlay_9
);
3625 OVERLAY_INFO_EXTERN(overlay_10
);
3626 OVERLAY_INFO_EXTERN(overlay_11
);
3627 OVERLAY_INFO_EXTERN(overlay_12
);
3628 OVERLAY_INFO_EXTERN(overlay_13
);
3629 OVERLAY_INFO_EXTERN(overlay_15
);
3630 OVERLAY_INFO_EXTERN(overlay_16
);
3631 OVERLAY_INFO_EXTERN(overlay_17
);
3632 OVERLAY_INFO_EXTERN(overlay_18
);
3633 OVERLAY_INFO_EXTERN(overlay_19
);
3634 OVERLAY_INFO_EXTERN(overlay_20
);
3635 OVERLAY_INFO_EXTERN(overlay_gpio_01
);
3636 OVERLAY_INFO_EXTERN(overlay_gpio_02a
);
3637 OVERLAY_INFO_EXTERN(overlay_gpio_02b
);
3638 OVERLAY_INFO_EXTERN(overlay_gpio_03
);
3639 OVERLAY_INFO_EXTERN(overlay_gpio_04a
);
3640 OVERLAY_INFO_EXTERN(overlay_gpio_04b
);
3641 OVERLAY_INFO_EXTERN(overlay_pci_node
);
3642 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node
);
3643 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop
);
3644 OVERLAY_INFO_EXTERN(overlay_bad_phandle
);
3645 OVERLAY_INFO_EXTERN(overlay_bad_symbol
);
3646 OVERLAY_INFO_EXTERN(overlay_bad_unresolved
);
3648 /* entries found by name */
3649 static struct overlay_info overlays
[] = {
3650 OVERLAY_INFO(overlay_base
, -9999, 0),
3651 OVERLAY_INFO(overlay
, 0, 0),
3652 OVERLAY_INFO(overlay_0
, 0, 0),
3653 OVERLAY_INFO(overlay_1
, 0, 0),
3654 OVERLAY_INFO(overlay_2
, 0, 0),
3655 OVERLAY_INFO(overlay_3
, 0, 0),
3656 OVERLAY_INFO(overlay_4
, 0, 0),
3657 OVERLAY_INFO(overlay_5
, 0, 0),
3658 OVERLAY_INFO(overlay_6
, 0, 0),
3659 OVERLAY_INFO(overlay_7
, 0, 0),
3660 OVERLAY_INFO(overlay_8
, 0, 0),
3661 OVERLAY_INFO(overlay_9
, 0, 0),
3662 OVERLAY_INFO(overlay_10
, 0, 0),
3663 OVERLAY_INFO(overlay_11
, 0, 0),
3664 OVERLAY_INFO(overlay_12
, 0, 0),
3665 OVERLAY_INFO(overlay_13
, 0, 0),
3666 OVERLAY_INFO(overlay_15
, 0, 0),
3667 OVERLAY_INFO(overlay_16
, -EBUSY
, 0),
3668 OVERLAY_INFO(overlay_17
, -EEXIST
, 0),
3669 OVERLAY_INFO(overlay_18
, 0, 0),
3670 OVERLAY_INFO(overlay_19
, 0, 0),
3671 OVERLAY_INFO(overlay_20
, 0, 0),
3672 OVERLAY_INFO(overlay_gpio_01
, 0, 0),
3673 OVERLAY_INFO(overlay_gpio_02a
, 0, 0),
3674 OVERLAY_INFO(overlay_gpio_02b
, 0, 0),
3675 OVERLAY_INFO(overlay_gpio_03
, 0, 0),
3676 OVERLAY_INFO(overlay_gpio_04a
, 0, 0),
3677 OVERLAY_INFO(overlay_gpio_04b
, 0, 0),
3678 OVERLAY_INFO(overlay_pci_node
, 0, 0),
3679 OVERLAY_INFO(overlay_bad_add_dup_node
, -EINVAL
, -ENODEV
),
3680 OVERLAY_INFO(overlay_bad_add_dup_prop
, -EINVAL
, -ENODEV
),
3681 OVERLAY_INFO(overlay_bad_phandle
, -EINVAL
, 0),
3682 OVERLAY_INFO(overlay_bad_symbol
, -EINVAL
, -ENODEV
),
3683 OVERLAY_INFO(overlay_bad_unresolved
, -EINVAL
, 0),
3688 static struct device_node
*overlay_base_root
;
3690 static void * __init
dt_alloc_memory(u64 size
, u64 align
)
3692 return memblock_alloc_or_panic(size
, align
);
3696 * Create base device tree for the overlay unittest.
3698 * This is called from very early boot code.
3700 * Do as much as possible the same way as done in __unflatten_device_tree
3701 * and other early boot steps for the normal FDT so that the overlay base
3702 * unflattened tree will have the same characteristics as the real tree
3703 * (such as having memory allocated by the early allocator). The goal
3704 * is to test "the real thing" as much as possible, and test "test setup
3705 * code" as little as possible.
3707 * Have to stop before resolving phandles, because that uses kmalloc.
3709 void __init
unittest_unflatten_overlay_base(void)
3711 struct overlay_info
*info
;
3716 const char *overlay_name
= "overlay_base";
3718 for (info
= overlays
; info
&& info
->name
; info
++) {
3719 if (!strcmp(overlay_name
, info
->name
)) {
3725 pr_err("no overlay data for %s\n", overlay_name
);
3729 info
= &overlays
[0];
3731 if (info
->expected_result
!= -9999) {
3732 pr_err("No dtb 'overlay_base' to attach\n");
3736 data_size
= info
->dtbo_end
- info
->dtbo_begin
;
3738 pr_err("No dtb 'overlay_base' to attach\n");
3742 size
= fdt_totalsize(info
->dtbo_begin
);
3743 if (size
!= data_size
) {
3744 pr_err("dtb 'overlay_base' header totalsize != actual size");
3748 new_fdt
= dt_alloc_memory(size
, roundup_pow_of_two(FDT_V17_SIZE
));
3750 pr_err("alloc for dtb 'overlay_base' failed");
3754 memcpy(new_fdt
, info
->dtbo_begin
, size
);
3756 __unflatten_device_tree(new_fdt
, NULL
, &overlay_base_root
,
3757 dt_alloc_memory
, true);
3761 * The purpose of of_unittest_overlay_data_add is to add an
3762 * overlay in the normal fashion. This is a test of the whole
3763 * picture, instead of testing individual elements.
3765 * A secondary purpose is to be able to verify that the contents of
3766 * /proc/device-tree/ contains the updated structure and values from
3767 * the overlay. That must be verified separately in user space.
3769 * Return 0 on unexpected error.
3771 static int __init
overlay_data_apply(const char *overlay_name
, int *ovcs_id
)
3773 struct overlay_info
*info
;
3779 for (info
= overlays
; info
&& info
->name
; info
++) {
3780 if (!strcmp(overlay_name
, info
->name
)) {
3786 pr_err("no overlay data for %s\n", overlay_name
);
3790 size
= info
->dtbo_end
- info
->dtbo_begin
;
3792 pr_err("no overlay data for %s\n", overlay_name
);
3794 ret
= of_overlay_fdt_apply(info
->dtbo_begin
, size
, &info
->ovcs_id
,
3797 *ovcs_id
= info
->ovcs_id
;
3801 pr_debug("%s applied\n", overlay_name
);
3804 if (ret
!= info
->expected_result
) {
3805 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3806 info
->expected_result
, ret
, overlay_name
);
3811 /* changeset may be partially applied */
3812 ret2
= of_overlay_remove(&info
->ovcs_id
);
3813 if (ret2
!= info
->expected_result_remove
) {
3814 pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
3815 info
->expected_result_remove
, ret2
,
3825 * The purpose of of_unittest_overlay_high_level is to add an overlay
3826 * in the normal fashion. This is a test of the whole picture,
3827 * instead of individual elements.
3829 * The first part of the function is _not_ normal overlay usage; it is
3830 * finishing splicing the base overlay device tree into the live tree.
3832 static __init
void of_unittest_overlay_high_level(void)
3834 struct device_node
*last_sibling
;
3835 struct device_node
*np
;
3836 struct device_node
*of_symbols
;
3837 struct device_node
*overlay_base_symbols
;
3838 struct device_node
**pprev
;
3839 struct property
*prop
;
3842 if (!overlay_base_root
) {
3843 unittest(0, "overlay_base_root not initialized\n");
3848 * Could not fixup phandles in unittest_unflatten_overlay_base()
3849 * because kmalloc() was not yet available.
3851 of_overlay_mutex_lock();
3852 of_resolve_phandles(overlay_base_root
);
3853 of_overlay_mutex_unlock();
3857 * do not allow overlay_base to duplicate any node already in
3858 * tree, this greatly simplifies the code
3862 * remove overlay_base_root node "__local_fixups", after
3863 * being used by of_resolve_phandles()
3865 pprev
= &overlay_base_root
->child
;
3866 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
) {
3867 if (of_node_name_eq(np
, "__local_fixups__")) {
3868 *pprev
= np
->sibling
;
3871 pprev
= &np
->sibling
;
3874 /* remove overlay_base_root node "__symbols__" if in live tree */
3875 of_symbols
= of_get_child_by_name(of_root
, "__symbols__");
3877 /* will have to graft properties from node into live tree */
3878 pprev
= &overlay_base_root
->child
;
3879 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
) {
3880 if (of_node_name_eq(np
, "__symbols__")) {
3881 overlay_base_symbols
= np
;
3882 *pprev
= np
->sibling
;
3885 pprev
= &np
->sibling
;
3889 for_each_child_of_node(overlay_base_root
, np
) {
3890 struct device_node
*base_child
;
3891 for_each_child_of_node(of_root
, base_child
) {
3892 if (!strcmp(np
->full_name
, base_child
->full_name
)) {
3893 unittest(0, "illegal node name in overlay_base %pOFn",
3896 of_node_put(base_child
);
3903 * overlay 'overlay_base' is not allowed to have root
3904 * properties, so only need to splice nodes into main device tree.
3906 * root node of *overlay_base_root will not be freed, it is lost
3910 for (np
= overlay_base_root
->child
; np
; np
= np
->sibling
)
3911 np
->parent
= of_root
;
3913 mutex_lock(&of_mutex
);
3915 for (last_sibling
= np
= of_root
->child
; np
; np
= np
->sibling
)
3919 last_sibling
->sibling
= overlay_base_root
->child
;
3921 of_root
->child
= overlay_base_root
->child
;
3923 for_each_of_allnodes_from(overlay_base_root
, np
)
3924 __of_attach_node_sysfs(np
);
3927 struct property
*new_prop
;
3928 for_each_property_of_node(overlay_base_symbols
, prop
) {
3930 new_prop
= __of_prop_dup(prop
, GFP_KERNEL
);
3932 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3936 if (__of_add_property(of_symbols
, new_prop
)) {
3937 __of_prop_free(new_prop
);
3938 /* "name" auto-generated by unflatten */
3939 if (!strcmp(prop
->name
, "name"))
3941 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3945 if (__of_add_property_sysfs(of_symbols
, new_prop
)) {
3946 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3953 mutex_unlock(&of_mutex
);
3956 /* now do the normal overlay usage test */
3958 /* --- overlay --- */
3960 EXPECT_BEGIN(KERN_ERR
,
3961 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3962 EXPECT_BEGIN(KERN_ERR
,
3963 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3964 EXPECT_BEGIN(KERN_ERR
,
3965 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3966 EXPECT_BEGIN(KERN_ERR
,
3967 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3968 EXPECT_BEGIN(KERN_ERR
,
3969 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3970 EXPECT_BEGIN(KERN_ERR
,
3971 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3972 EXPECT_BEGIN(KERN_ERR
,
3973 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3974 EXPECT_BEGIN(KERN_ERR
,
3975 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3976 EXPECT_BEGIN(KERN_ERR
,
3977 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3978 EXPECT_BEGIN(KERN_ERR
,
3979 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3980 EXPECT_BEGIN(KERN_ERR
,
3981 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3983 ret
= overlay_data_apply("overlay", NULL
);
3985 EXPECT_END(KERN_ERR
,
3986 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3987 EXPECT_END(KERN_ERR
,
3988 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3989 EXPECT_END(KERN_ERR
,
3990 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3991 EXPECT_END(KERN_ERR
,
3992 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3993 EXPECT_END(KERN_ERR
,
3994 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3995 EXPECT_END(KERN_ERR
,
3996 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3997 EXPECT_END(KERN_ERR
,
3998 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3999 EXPECT_END(KERN_ERR
,
4000 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
4001 EXPECT_END(KERN_ERR
,
4002 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
4003 EXPECT_END(KERN_ERR
,
4004 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
4005 EXPECT_END(KERN_ERR
,
4006 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
4008 unittest(ret
, "Adding overlay 'overlay' failed\n");
4010 /* --- overlay_bad_add_dup_node --- */
4012 EXPECT_BEGIN(KERN_ERR
,
4013 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
4014 EXPECT_BEGIN(KERN_ERR
,
4015 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
4016 EXPECT_BEGIN(KERN_ERR
,
4017 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
4018 EXPECT_BEGIN(KERN_ERR
,
4019 "OF: Error reverting changeset (-19)");
4021 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL
),
4022 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
4024 EXPECT_END(KERN_ERR
,
4025 "OF: Error reverting changeset (-19)");
4026 EXPECT_END(KERN_ERR
,
4027 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
4028 EXPECT_END(KERN_ERR
,
4029 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
4030 EXPECT_END(KERN_ERR
,
4031 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
4033 /* --- overlay_bad_add_dup_prop --- */
4035 EXPECT_BEGIN(KERN_ERR
,
4036 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
4037 EXPECT_BEGIN(KERN_ERR
,
4038 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
4039 EXPECT_BEGIN(KERN_ERR
,
4040 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
4041 EXPECT_BEGIN(KERN_ERR
,
4042 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
4043 EXPECT_BEGIN(KERN_ERR
,
4044 "OF: Error reverting changeset (-19)");
4046 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL
),
4047 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
4049 EXPECT_END(KERN_ERR
,
4050 "OF: Error reverting changeset (-19)");
4051 EXPECT_END(KERN_ERR
,
4052 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
4053 EXPECT_END(KERN_ERR
,
4054 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
4055 EXPECT_END(KERN_ERR
,
4056 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
4057 EXPECT_END(KERN_ERR
,
4058 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
4060 /* --- overlay_bad_phandle --- */
4062 unittest(overlay_data_apply("overlay_bad_phandle", NULL
),
4063 "Adding overlay 'overlay_bad_phandle' failed\n");
4065 /* --- overlay_bad_symbol --- */
4067 EXPECT_BEGIN(KERN_ERR
,
4068 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
4069 EXPECT_BEGIN(KERN_ERR
,
4070 "OF: Error reverting changeset (-19)");
4072 unittest(overlay_data_apply("overlay_bad_symbol", NULL
),
4073 "Adding overlay 'overlay_bad_symbol' failed\n");
4075 EXPECT_END(KERN_ERR
,
4076 "OF: Error reverting changeset (-19)");
4077 EXPECT_END(KERN_ERR
,
4078 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
4080 /* --- overlay_bad_unresolved --- */
4082 EXPECT_BEGIN(KERN_ERR
,
4083 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
4084 EXPECT_BEGIN(KERN_ERR
,
4085 "OF: resolver: overlay phandle fixup failed: -22");
4087 unittest(overlay_data_apply("overlay_bad_unresolved", NULL
),
4088 "Adding overlay 'overlay_bad_unresolved' failed\n");
4090 EXPECT_END(KERN_ERR
,
4091 "OF: resolver: overlay phandle fixup failed: -22");
4092 EXPECT_END(KERN_ERR
,
4093 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
4098 mutex_unlock(&of_mutex
);
4101 static int of_unittest_pci_dev_num
;
4102 static int of_unittest_pci_child_num
;
4105 * PCI device tree node test driver
4107 static const struct pci_device_id testdrv_pci_ids
[] = {
4108 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT
, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
4112 static int testdrv_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
4114 struct overlay_info
*info
;
4115 struct device_node
*dn
;
4119 dn
= pdev
->dev
.of_node
;
4121 dev_err(&pdev
->dev
, "does not find bus endpoint");
4125 for (info
= overlays
; info
&& info
->name
; info
++) {
4126 if (!strcmp(info
->name
, "overlay_pci_node"))
4129 if (!info
|| !info
->name
) {
4130 dev_err(&pdev
->dev
, "no overlay data for overlay_pci_node");
4134 size
= info
->dtbo_end
- info
->dtbo_begin
;
4135 ret
= of_overlay_fdt_apply(info
->dtbo_begin
, size
, &ovcs_id
, dn
);
4140 of_platform_default_populate(dn
, NULL
, &pdev
->dev
);
4141 pci_set_drvdata(pdev
, (void *)(uintptr_t)ovcs_id
);
4146 static void testdrv_remove(struct pci_dev
*pdev
)
4148 int ovcs_id
= (int)(uintptr_t)pci_get_drvdata(pdev
);
4150 of_platform_depopulate(&pdev
->dev
);
4151 of_overlay_remove(&ovcs_id
);
4154 static struct pci_driver testdrv_driver
= {
4155 .name
= "pci_dt_testdrv",
4156 .id_table
= testdrv_pci_ids
,
4157 .probe
= testdrv_probe
,
4158 .remove
= testdrv_remove
,
4161 static int unittest_pci_probe(struct platform_device
*pdev
)
4163 struct resource
*res
;
4167 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
4172 while (dev
&& !dev_is_pci(dev
))
4175 pr_err("unable to find parent device\n");
4179 exp_addr
= pci_resource_start(to_pci_dev(dev
), 0) + 0x100;
4180 unittest(res
->start
== exp_addr
, "Incorrect translated address %llx, expected %llx\n",
4181 (u64
)res
->start
, exp_addr
);
4183 of_unittest_pci_child_num
++;
4188 static const struct of_device_id unittest_pci_of_match
[] = {
4189 { .compatible
= "unittest-pci" },
4193 static struct platform_driver unittest_pci_driver
= {
4194 .probe
= unittest_pci_probe
,
4196 .name
= "unittest-pci",
4197 .of_match_table
= unittest_pci_of_match
,
4201 static int of_unittest_pci_node_verify(struct pci_dev
*pdev
, bool add
)
4203 struct device_node
*pnp
, *np
= NULL
;
4204 struct device
*child_dev
;
4209 pnp
= pdev
->dev
.of_node
;
4210 unittest(pnp
, "Failed creating PCI dt node\n");
4215 path
= kasprintf(GFP_KERNEL
, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp
);
4216 np
= of_find_node_by_path(path
);
4217 unittest(np
, "Failed to get unittest-pci node under PCI node\n");
4223 reg
= of_get_property(np
, "reg", NULL
);
4224 unittest(reg
, "Failed to get reg property\n");
4228 path
= kasprintf(GFP_KERNEL
, "%pOF/pci-ep-bus@0", pnp
);
4229 np
= of_find_node_by_path(path
);
4230 unittest(!np
, "Child device tree node is not removed\n");
4231 child_dev
= device_find_any_child(&pdev
->dev
);
4232 unittest(!child_dev
, "Child device is not removed\n");
4243 static void __init
of_unittest_pci_node(void)
4245 struct pci_dev
*pdev
= NULL
;
4248 if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES
))
4251 rc
= pci_register_driver(&testdrv_driver
);
4252 unittest(!rc
, "Failed to register pci test driver; rc = %d\n", rc
);
4256 rc
= platform_driver_register(&unittest_pci_driver
);
4257 if (unittest(!rc
, "Failed to register unittest pci driver\n")) {
4258 pci_unregister_driver(&testdrv_driver
);
4262 while ((pdev
= pci_get_device(PCI_VENDOR_ID_REDHAT
, 0x5, pdev
)) != NULL
) {
4263 of_unittest_pci_node_verify(pdev
, true);
4264 of_unittest_pci_dev_num
++;
4269 unittest(of_unittest_pci_dev_num
,
4270 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
4271 unittest(of_unittest_pci_dev_num
== of_unittest_pci_child_num
,
4272 "Child device number %d is not expected %d", of_unittest_pci_child_num
,
4273 of_unittest_pci_dev_num
);
4275 platform_driver_unregister(&unittest_pci_driver
);
4276 pci_unregister_driver(&testdrv_driver
);
4278 while ((pdev
= pci_get_device(PCI_VENDOR_ID_REDHAT
, 0x5, pdev
)) != NULL
)
4279 of_unittest_pci_node_verify(pdev
, false);
4285 static inline __init
void of_unittest_overlay_high_level(void) {}
4286 static inline __init
void of_unittest_pci_node(void) { }
4290 static int __init
of_unittest(void)
4292 struct device_node
*np
;
4295 pr_info("start of unittest - you will see error messages\n");
4297 /* Taint the kernel so we know we've run tests. */
4298 add_taint(TAINT_TEST
, LOCKDEP_STILL_OK
);
4300 /* adding data for unittest */
4301 res
= unittest_data_add();
4305 of_aliases
= of_find_node_by_path("/aliases");
4307 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4309 pr_info("No testcase data in device tree; not running tests\n");
4314 of_unittest_check_tree_linkage();
4315 of_unittest_check_phandles();
4316 of_unittest_find_node_by_name();
4317 of_unittest_dynamic();
4318 of_unittest_parse_phandle_with_args();
4319 of_unittest_parse_phandle_with_args_map();
4320 of_unittest_printf();
4321 of_unittest_property_string();
4322 of_unittest_property_copy();
4323 of_unittest_changeset();
4324 of_unittest_changeset_prop();
4325 of_unittest_parse_interrupts();
4326 of_unittest_parse_interrupts_extended();
4327 of_unittest_dma_get_max_cpu_address();
4328 of_unittest_parse_dma_ranges();
4329 of_unittest_pci_dma_ranges();
4330 of_unittest_pci_empty_dma_ranges();
4331 of_unittest_bus_ranges();
4332 of_unittest_bus_3cell_ranges();
4334 of_unittest_translate_addr();
4335 of_unittest_match_node();
4336 of_unittest_platform_populate();
4337 of_unittest_overlay();
4338 of_unittest_lifecycle();
4339 of_unittest_pci_node();
4341 /* Double check linkage after removing testcase data */
4342 of_unittest_check_tree_linkage();
4344 of_unittest_overlay_high_level();
4346 pr_info("end of unittest - %i passed, %i failed\n",
4347 unittest_results
.passed
, unittest_results
.failed
);
4351 late_initcall(of_unittest
);