Linux 6.13-rc6
[linux.git] / drivers / of / unittest.c
blob438fd70fa9950048b066086ecd49fb788d97da38
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Self tests for device tree subsystem
4 */
6 #define pr_fmt(fmt) "### dt-test ### " fmt
8 #include <linux/memblock.h>
9 #include <linux/clk.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>
15 #include <linux/of.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 {
37 int passed;
38 int failed;
39 } unittest_results;
41 #define unittest(result, fmt, ...) ({ \
42 bool failed = !(result); \
43 if (failed) { \
44 unittest_results.failed++; \
45 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
46 } else { \
47 unittest_results.passed++; \
48 pr_info("pass %s():%i\n", __func__, __LINE__); \
49 } \
50 failed; \
53 #ifdef CONFIG_OF_KOBJ
54 #define OF_KREF_READ(NODE) kref_read(&(NODE)->kobj.kref)
55 #else
56 #define OF_KREF_READ(NODE) 1
57 #endif
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");
89 of_node_put(np);
90 kfree(name);
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");
100 of_node_put(np);
101 kfree(name);
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");
107 of_node_put(np);
108 kfree(name);
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");
118 of_node_put(np);
119 kfree(name);
121 np = of_find_node_by_path("/testcase-data/missing-path");
122 unittest(!np, "non-existent path returned node %pOF\n", np);
123 of_node_put(np);
125 np = of_find_node_by_path("missing-alias");
126 unittest(!np, "non-existent alias returned node %pOF\n", np);
127 of_node_put(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);
131 of_node_put(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");
136 of_node_put(np);
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");
141 of_node_put(np);
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");
146 of_node_put(np);
148 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
149 unittest(np, "NULL option path test failed\n");
150 of_node_put(np);
152 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
153 &options);
154 unittest(np && !strcmp("testaliasoption", options),
155 "option alias path test failed\n");
156 of_node_put(np);
158 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
159 &options);
160 unittest(np && !strcmp("test/alias/option", options),
161 "option alias path test, subcase #1 failed\n");
162 of_node_put(np);
164 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
165 unittest(np, "NULL option alias path test failed\n");
166 of_node_put(np);
168 options = "testoption";
169 np = of_find_node_opts_by_path("testcase-alias", &options);
170 unittest(np && !options, "option clearing test failed\n");
171 of_node_put(np);
173 options = "testoption";
174 np = of_find_node_opts_by_path("/", &options);
175 unittest(np && !options, "option clearing root node test failed\n");
176 of_node_put(np);
179 static void __init of_unittest_dynamic(void)
181 struct device_node *np;
182 struct property *prop;
184 np = of_find_node_by_path("/testcase-data");
185 if (!np) {
186 pr_err("missing testcase data\n");
187 return;
190 /* Array of 4 properties for the purpose of testing */
191 prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
192 if (!prop) {
193 unittest(0, "kzalloc() failed\n");
194 return;
197 /* Add a new property - should pass*/
198 prop->name = "new-property";
199 prop->value = "new-property-data";
200 prop->length = strlen(prop->value) + 1;
201 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
203 /* Try to add an existing property - should fail */
204 prop++;
205 prop->name = "new-property";
206 prop->value = "new-property-data-should-fail";
207 prop->length = strlen(prop->value) + 1;
208 unittest(of_add_property(np, prop) != 0,
209 "Adding an existing property should have failed\n");
211 /* Try to modify an existing property - should pass */
212 prop->value = "modify-property-data-should-pass";
213 prop->length = strlen(prop->value) + 1;
214 unittest(of_update_property(np, prop) == 0,
215 "Updating an existing property should have passed\n");
217 /* Try to modify non-existent property - should pass*/
218 prop++;
219 prop->name = "modify-property";
220 prop->value = "modify-missing-property-data-should-pass";
221 prop->length = strlen(prop->value) + 1;
222 unittest(of_update_property(np, prop) == 0,
223 "Updating a missing property should have passed\n");
225 /* Remove property - should pass */
226 unittest(of_remove_property(np, prop) == 0,
227 "Removing a property should have passed\n");
229 /* Adding very large property - should pass */
230 prop++;
231 prop->name = "large-property-PAGE_SIZEx8";
232 prop->length = PAGE_SIZE * 8;
233 prop->value = kzalloc(prop->length, GFP_KERNEL);
234 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
235 if (prop->value)
236 unittest(of_add_property(np, prop) == 0,
237 "Adding a large property should have passed\n");
240 static int __init of_unittest_check_node_linkage(struct device_node *np)
242 int count = 0, rc;
244 for_each_child_of_node_scoped(np, child) {
245 if (child->parent != np) {
246 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
247 child, np);
248 return -EINVAL;
251 rc = of_unittest_check_node_linkage(child);
252 if (rc < 0)
253 return rc;
254 count += rc;
257 return count + 1;
260 static void __init of_unittest_check_tree_linkage(void)
262 struct device_node *np;
263 int allnode_count = 0, child_count;
265 if (!of_root)
266 return;
268 for_each_of_allnodes(np)
269 allnode_count++;
270 child_count = of_unittest_check_node_linkage(of_root);
272 unittest(child_count > 0, "Device node data structure is corrupted\n");
273 unittest(child_count == allnode_count,
274 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
275 allnode_count, child_count);
276 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
279 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
280 const char *expected)
282 unsigned char *buf;
283 int buf_size;
284 int size, i;
286 buf_size = strlen(expected) + 10;
287 buf = kmalloc(buf_size, GFP_KERNEL);
288 if (!buf)
289 return;
291 /* Baseline; check conversion with a large size limit */
292 memset(buf, 0xff, buf_size);
293 size = snprintf(buf, buf_size - 2, fmt, np);
295 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
296 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
297 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
298 fmt, expected, buf);
300 /* Make sure length limits work */
301 size++;
302 for (i = 0; i < 2; i++, size--) {
303 /* Clear the buffer, and make sure it works correctly still */
304 memset(buf, 0xff, buf_size);
305 snprintf(buf, size+1, fmt, np);
306 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
307 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
308 size, fmt, expected, buf);
310 kfree(buf);
313 static void __init of_unittest_printf(void)
315 struct device_node *np;
316 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
317 char phandle_str[16] = "";
319 np = of_find_node_by_path(full_name);
320 if (!np) {
321 unittest(np, "testcase data missing\n");
322 return;
325 num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
327 of_unittest_printf_one(np, "%pOF", full_name);
328 of_unittest_printf_one(np, "%pOFf", full_name);
329 of_unittest_printf_one(np, "%pOFn", "dev");
330 of_unittest_printf_one(np, "%2pOFn", "dev");
331 of_unittest_printf_one(np, "%5pOFn", " dev");
332 of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
333 of_unittest_printf_one(np, "%pOFp", phandle_str);
334 of_unittest_printf_one(np, "%pOFP", "dev@100");
335 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
336 of_unittest_printf_one(np, "%10pOFP", " dev@100");
337 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
338 of_unittest_printf_one(of_root, "%pOFP", "/");
339 of_unittest_printf_one(np, "%pOFF", "----");
340 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
341 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
342 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
343 of_unittest_printf_one(np, "%pOFC",
344 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
347 struct node_hash {
348 struct hlist_node node;
349 struct device_node *np;
352 static DEFINE_HASHTABLE(phandle_ht, 8);
353 static void __init of_unittest_check_phandles(void)
355 struct device_node *np;
356 struct node_hash *nh;
357 struct hlist_node *tmp;
358 int i, dup_count = 0, phandle_count = 0;
360 for_each_of_allnodes(np) {
361 if (!np->phandle)
362 continue;
364 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
365 if (nh->np->phandle == np->phandle) {
366 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
367 np->phandle, nh->np, np);
368 dup_count++;
369 break;
373 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
374 if (!nh)
375 return;
377 nh->np = np;
378 hash_add(phandle_ht, &nh->node, np->phandle);
379 phandle_count++;
381 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
382 dup_count, phandle_count);
384 /* Clean up */
385 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
386 hash_del(&nh->node);
387 kfree(nh);
391 static void __init of_unittest_parse_phandle_with_args(void)
393 struct device_node *np;
394 struct of_phandle_args args;
395 int i, rc;
397 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
398 if (!np) {
399 pr_err("missing testcase data\n");
400 return;
403 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
404 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
406 for (i = 0; i < 8; i++) {
407 bool passed = true;
409 memset(&args, 0, sizeof(args));
410 rc = of_parse_phandle_with_args(np, "phandle-list",
411 "#phandle-cells", i, &args);
413 /* Test the values from tests-phandle.dtsi */
414 switch (i) {
415 case 0:
416 passed &= !rc;
417 passed &= (args.args_count == 1);
418 passed &= (args.args[0] == (i + 1));
419 break;
420 case 1:
421 passed &= !rc;
422 passed &= (args.args_count == 2);
423 passed &= (args.args[0] == (i + 1));
424 passed &= (args.args[1] == 0);
425 break;
426 case 2:
427 passed &= (rc == -ENOENT);
428 break;
429 case 3:
430 passed &= !rc;
431 passed &= (args.args_count == 3);
432 passed &= (args.args[0] == (i + 1));
433 passed &= (args.args[1] == 4);
434 passed &= (args.args[2] == 3);
435 break;
436 case 4:
437 passed &= !rc;
438 passed &= (args.args_count == 2);
439 passed &= (args.args[0] == (i + 1));
440 passed &= (args.args[1] == 100);
441 break;
442 case 5:
443 passed &= !rc;
444 passed &= (args.args_count == 0);
445 break;
446 case 6:
447 passed &= !rc;
448 passed &= (args.args_count == 1);
449 passed &= (args.args[0] == (i + 1));
450 break;
451 case 7:
452 passed &= (rc == -ENOENT);
453 break;
454 default:
455 passed = false;
458 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
459 i, args.np, rc);
461 if (rc == 0)
462 of_node_put(args.np);
465 /* Check for missing list property */
466 memset(&args, 0, sizeof(args));
467 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
468 "#phandle-cells", 0, &args);
469 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
470 rc = of_count_phandle_with_args(np, "phandle-list-missing",
471 "#phandle-cells");
472 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
474 /* Check for missing cells property */
475 memset(&args, 0, sizeof(args));
477 EXPECT_BEGIN(KERN_INFO,
478 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
480 rc = of_parse_phandle_with_args(np, "phandle-list",
481 "#phandle-cells-missing", 0, &args);
483 EXPECT_END(KERN_INFO,
484 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
486 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
488 EXPECT_BEGIN(KERN_INFO,
489 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
491 rc = of_count_phandle_with_args(np, "phandle-list",
492 "#phandle-cells-missing");
494 EXPECT_END(KERN_INFO,
495 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
497 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
499 /* Check for bad phandle in list */
500 memset(&args, 0, sizeof(args));
502 EXPECT_BEGIN(KERN_INFO,
503 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
505 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
506 "#phandle-cells", 0, &args);
508 EXPECT_END(KERN_INFO,
509 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
511 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
513 EXPECT_BEGIN(KERN_INFO,
514 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
516 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
517 "#phandle-cells");
519 EXPECT_END(KERN_INFO,
520 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
522 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
524 /* Check for incorrectly formed argument list */
525 memset(&args, 0, sizeof(args));
527 EXPECT_BEGIN(KERN_INFO,
528 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
530 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
531 "#phandle-cells", 1, &args);
533 EXPECT_END(KERN_INFO,
534 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
536 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
538 EXPECT_BEGIN(KERN_INFO,
539 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
541 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
542 "#phandle-cells");
544 EXPECT_END(KERN_INFO,
545 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
547 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
550 static void __init of_unittest_parse_phandle_with_args_map(void)
552 struct device_node *np, *p[6] = {};
553 struct of_phandle_args args;
554 unsigned int prefs[6];
555 int i, rc;
557 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
558 if (!np) {
559 pr_err("missing testcase data\n");
560 return;
563 p[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
564 p[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
565 p[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
566 p[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
567 p[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
568 p[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
569 for (i = 0; i < ARRAY_SIZE(p); ++i) {
570 if (!p[i]) {
571 pr_err("missing testcase data\n");
572 return;
574 prefs[i] = OF_KREF_READ(p[i]);
577 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
578 unittest(rc == 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc);
580 for (i = 0; i < 9; i++) {
581 bool passed = true;
583 memset(&args, 0, sizeof(args));
584 rc = of_parse_phandle_with_args_map(np, "phandle-list",
585 "phandle", i, &args);
587 /* Test the values from tests-phandle.dtsi */
588 switch (i) {
589 case 0:
590 passed &= !rc;
591 passed &= (args.np == p[1]);
592 passed &= (args.args_count == 1);
593 passed &= (args.args[0] == 1);
594 break;
595 case 1:
596 passed &= !rc;
597 passed &= (args.np == p[3]);
598 passed &= (args.args_count == 3);
599 passed &= (args.args[0] == 2);
600 passed &= (args.args[1] == 5);
601 passed &= (args.args[2] == 3);
602 break;
603 case 2:
604 passed &= (rc == -ENOENT);
605 break;
606 case 3:
607 passed &= !rc;
608 passed &= (args.np == p[0]);
609 passed &= (args.args_count == 0);
610 break;
611 case 4:
612 passed &= !rc;
613 passed &= (args.np == p[1]);
614 passed &= (args.args_count == 1);
615 passed &= (args.args[0] == 3);
616 break;
617 case 5:
618 passed &= !rc;
619 passed &= (args.np == p[0]);
620 passed &= (args.args_count == 0);
621 break;
622 case 6:
623 passed &= !rc;
624 passed &= (args.np == p[2]);
625 passed &= (args.args_count == 2);
626 passed &= (args.args[0] == 15);
627 passed &= (args.args[1] == 0x20);
628 break;
629 case 7:
630 passed &= !rc;
631 passed &= (args.np == p[3]);
632 passed &= (args.args_count == 3);
633 passed &= (args.args[0] == 2);
634 passed &= (args.args[1] == 5);
635 passed &= (args.args[2] == 3);
636 break;
637 case 8:
638 passed &= (rc == -ENOENT);
639 break;
640 default:
641 passed = false;
644 unittest(passed, "index %i - data error on node %s rc=%i\n",
645 i, args.np->full_name, rc);
647 if (rc == 0)
648 of_node_put(args.np);
651 /* Check for missing list property */
652 memset(&args, 0, sizeof(args));
653 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
654 "phandle", 0, &args);
655 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
657 /* Check for missing cells,map,mask property */
658 memset(&args, 0, sizeof(args));
660 EXPECT_BEGIN(KERN_INFO,
661 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
663 rc = of_parse_phandle_with_args_map(np, "phandle-list",
664 "phandle-missing", 0, &args);
665 EXPECT_END(KERN_INFO,
666 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
668 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
670 /* Check for bad phandle in list */
671 memset(&args, 0, sizeof(args));
673 EXPECT_BEGIN(KERN_INFO,
674 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
676 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
677 "phandle", 0, &args);
678 EXPECT_END(KERN_INFO,
679 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
681 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
683 /* Check for incorrectly formed argument list */
684 memset(&args, 0, sizeof(args));
686 EXPECT_BEGIN(KERN_INFO,
687 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
689 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
690 "phandle", 1, &args);
691 EXPECT_END(KERN_INFO,
692 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
694 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
696 for (i = 0; i < ARRAY_SIZE(p); ++i) {
697 unittest(prefs[i] == OF_KREF_READ(p[i]),
698 "provider%d: expected:%d got:%d\n",
699 i, prefs[i], OF_KREF_READ(p[i]));
700 of_node_put(p[i]);
704 static void __init of_unittest_property_string(void)
706 const char *strings[4];
707 struct device_node *np;
708 int rc;
710 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
711 if (!np) {
712 pr_err("No testcase data in device tree\n");
713 return;
716 rc = of_property_match_string(np, "phandle-list-names", "first");
717 unittest(rc == 0, "first expected:0 got:%i\n", rc);
718 rc = of_property_match_string(np, "phandle-list-names", "second");
719 unittest(rc == 1, "second expected:1 got:%i\n", rc);
720 rc = of_property_match_string(np, "phandle-list-names", "third");
721 unittest(rc == 2, "third expected:2 got:%i\n", rc);
722 rc = of_property_match_string(np, "phandle-list-names", "fourth");
723 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
724 rc = of_property_match_string(np, "missing-property", "blah");
725 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
726 rc = of_property_match_string(np, "empty-property", "blah");
727 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
728 rc = of_property_match_string(np, "unterminated-string", "blah");
729 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
731 /* of_property_count_strings() tests */
732 rc = of_property_count_strings(np, "string-property");
733 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
734 rc = of_property_count_strings(np, "phandle-list-names");
735 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
736 rc = of_property_count_strings(np, "unterminated-string");
737 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
738 rc = of_property_count_strings(np, "unterminated-string-list");
739 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
741 /* of_property_read_string_index() tests */
742 rc = of_property_read_string_index(np, "string-property", 0, strings);
743 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
744 strings[0] = NULL;
745 rc = of_property_read_string_index(np, "string-property", 1, strings);
746 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
747 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
748 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
749 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
750 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
751 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
752 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
753 strings[0] = NULL;
754 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
755 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
756 strings[0] = NULL;
757 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
758 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
759 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
760 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
761 strings[0] = NULL;
762 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
763 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
764 strings[1] = NULL;
766 /* of_property_read_string_array() tests */
767 rc = of_property_read_string_array(np, "string-property", strings, 4);
768 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
769 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
770 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
771 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
772 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
773 /* -- An incorrectly formed string should cause a failure */
774 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
775 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
776 /* -- parsing the correctly formed strings should still work: */
777 strings[2] = NULL;
778 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
779 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
780 strings[1] = NULL;
781 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
782 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
785 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
786 (p1)->value && (p2)->value && \
787 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
788 !strcmp((p1)->name, (p2)->name))
789 static void __init of_unittest_property_copy(void)
791 #ifdef CONFIG_OF_DYNAMIC
792 struct property p1 = { .name = "p1", .length = 0, .value = "" };
793 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
794 struct property *new;
796 new = __of_prop_dup(&p1, GFP_KERNEL);
797 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
798 __of_prop_free(new);
800 new = __of_prop_dup(&p2, GFP_KERNEL);
801 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
802 __of_prop_free(new);
803 #endif
806 static void __init of_unittest_changeset(void)
808 #ifdef CONFIG_OF_DYNAMIC
809 int ret;
810 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
811 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
812 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
813 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
814 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
815 struct property *ppremove;
816 struct device_node *n1, *n2, *n21, *n22, *nchangeset, *nremove, *parent, *np;
817 static const char * const str_array[] = { "str1", "str2", "str3" };
818 const u32 u32_array[] = { 1, 2, 3 };
819 struct of_changeset chgset;
820 const char *propstr = NULL;
822 n1 = __of_node_dup(NULL, "n1");
823 unittest(n1, "testcase setup failure\n");
825 n2 = __of_node_dup(NULL, "n2");
826 unittest(n2, "testcase setup failure\n");
828 n21 = __of_node_dup(NULL, "n21");
829 unittest(n21, "testcase setup failure %p\n", n21);
831 nchangeset = of_find_node_by_path("/testcase-data/changeset");
832 nremove = of_get_child_by_name(nchangeset, "node-remove");
833 unittest(nremove, "testcase setup failure\n");
835 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
836 unittest(ppadd, "testcase setup failure\n");
838 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
839 unittest(ppname_n1, "testcase setup failure\n");
841 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
842 unittest(ppname_n2, "testcase setup failure\n");
844 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
845 unittest(ppname_n21, "testcase setup failure\n");
847 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
848 unittest(ppupdate, "testcase setup failure\n");
850 parent = nchangeset;
851 n1->parent = parent;
852 n2->parent = parent;
853 n21->parent = n2;
855 ppremove = of_find_property(parent, "prop-remove", NULL);
856 unittest(ppremove, "failed to find removal prop");
858 of_changeset_init(&chgset);
860 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
861 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
863 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
864 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
866 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
867 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
869 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
871 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
872 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
873 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
874 n22 = of_changeset_create_node(&chgset, n2, "n22");
875 unittest(n22, "fail create n22\n");
876 unittest(!of_changeset_add_prop_string(&chgset, n22, "prop-str", "abcd"),
877 "fail add prop prop-str");
878 unittest(!of_changeset_add_prop_string_array(&chgset, n22, "prop-str-array",
879 (const char **)str_array,
880 ARRAY_SIZE(str_array)),
881 "fail add prop prop-str-array");
882 unittest(!of_changeset_add_prop_u32_array(&chgset, n22, "prop-u32-array",
883 u32_array, ARRAY_SIZE(u32_array)),
884 "fail add prop prop-u32-array");
886 unittest(!of_changeset_apply(&chgset), "apply failed\n");
888 of_node_put(nchangeset);
890 /* Make sure node names are constructed correctly */
891 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
892 "'%pOF' not added\n", n21);
893 of_node_put(np);
894 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n22")),
895 "'%pOF' not added\n", n22);
896 of_node_put(np);
898 unittest(!of_changeset_revert(&chgset), "revert failed\n");
900 unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
901 "'%pOF' still present after revert\n", n21);
903 unittest(of_property_present(parent, "prop-remove"),
904 "failed to find removed prop after revert\n");
906 ret = of_property_read_string(parent, "prop-update", &propstr);
907 unittest(!ret, "failed to find updated prop after revert\n");
908 if (!ret)
909 unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
911 of_changeset_destroy(&chgset);
913 of_node_put(n1);
914 of_node_put(n2);
915 of_node_put(n21);
916 of_node_put(n22);
917 #endif
920 static void __init __maybe_unused changeset_check_string(struct device_node *np,
921 const char *prop_name,
922 const char *expected_str)
924 const char *str;
925 int ret;
927 ret = of_property_read_string(np, prop_name, &str);
928 if (unittest(ret == 0, "failed to read %s\n", prop_name))
929 return;
931 unittest(strcmp(str, expected_str) == 0,
932 "%s value mismatch (read '%s', exp '%s')\n",
933 prop_name, str, expected_str);
936 static void __init __maybe_unused changeset_check_string_array(struct device_node *np,
937 const char *prop_name,
938 const char * const *expected_array,
939 unsigned int count)
941 const char *str;
942 unsigned int i;
943 int ret;
944 int cnt;
946 cnt = of_property_count_strings(np, prop_name);
947 if (unittest(cnt >= 0, "failed to get %s count\n", prop_name))
948 return;
950 if (unittest(cnt == count,
951 "%s count mismatch (read %d, exp %u)\n",
952 prop_name, cnt, count))
953 return;
955 for (i = 0; i < count; i++) {
956 ret = of_property_read_string_index(np, prop_name, i, &str);
957 if (unittest(ret == 0, "failed to read %s[%d]\n", prop_name, i))
958 continue;
960 unittest(strcmp(str, expected_array[i]) == 0,
961 "%s[%d] value mismatch (read '%s', exp '%s')\n",
962 prop_name, i, str, expected_array[i]);
966 static void __init __maybe_unused changeset_check_u32(struct device_node *np,
967 const char *prop_name,
968 u32 expected_u32)
970 u32 val32;
971 int ret;
973 ret = of_property_read_u32(np, prop_name, &val32);
974 if (unittest(ret == 0, "failed to read %s\n", prop_name))
975 return;
977 unittest(val32 == expected_u32,
978 "%s value mismatch (read '%u', exp '%u')\n",
979 prop_name, val32, expected_u32);
982 static void __init __maybe_unused changeset_check_u32_array(struct device_node *np,
983 const char *prop_name,
984 const u32 *expected_array,
985 unsigned int count)
987 unsigned int i;
988 u32 val32;
989 int ret;
990 int cnt;
992 cnt = of_property_count_u32_elems(np, prop_name);
993 if (unittest(cnt >= 0, "failed to get %s count\n", prop_name))
994 return;
996 if (unittest(cnt == count,
997 "%s count mismatch (read %d, exp %u)\n",
998 prop_name, cnt, count))
999 return;
1001 for (i = 0; i < count; i++) {
1002 ret = of_property_read_u32_index(np, prop_name, i, &val32);
1003 if (unittest(ret == 0, "failed to read %s[%d]\n", prop_name, i))
1004 continue;
1006 unittest(val32 == expected_array[i],
1007 "%s[%d] value mismatch (read '%u', exp '%u')\n",
1008 prop_name, i, val32, expected_array[i]);
1012 static void __init __maybe_unused changeset_check_bool(struct device_node *np,
1013 const char *prop_name)
1015 unittest(of_property_read_bool(np, prop_name),
1016 "%s value mismatch (read 'false', exp 'true')\n", prop_name);
1019 static void __init of_unittest_changeset_prop(void)
1021 #ifdef CONFIG_OF_DYNAMIC
1022 static const char * const str_array[] = { "abc", "defg", "hij" };
1023 static const u32 u32_array[] = { 123, 4567, 89, 10, 11 };
1024 struct device_node *nchangeset, *np;
1025 struct of_changeset chgset;
1026 int ret;
1028 nchangeset = of_find_node_by_path("/testcase-data/changeset");
1029 if (!nchangeset) {
1030 pr_err("missing testcase data\n");
1031 return;
1034 of_changeset_init(&chgset);
1036 np = of_changeset_create_node(&chgset, nchangeset, "test-prop");
1037 if (unittest(np, "failed to create test-prop node\n"))
1038 goto end_changeset_destroy;
1040 ret = of_changeset_add_prop_string(&chgset, np, "prop-string", "abcde");
1041 unittest(ret == 0, "failed to add prop-string\n");
1043 ret = of_changeset_add_prop_string_array(&chgset, np, "prop-string-array",
1044 str_array, ARRAY_SIZE(str_array));
1045 unittest(ret == 0, "failed to add prop-string-array\n");
1047 ret = of_changeset_add_prop_u32(&chgset, np, "prop-u32", 1234);
1048 unittest(ret == 0, "failed to add prop-u32\n");
1050 ret = of_changeset_add_prop_u32_array(&chgset, np, "prop-u32-array",
1051 u32_array, ARRAY_SIZE(u32_array));
1052 unittest(ret == 0, "failed to add prop-u32-array\n");
1054 ret = of_changeset_add_prop_bool(&chgset, np, "prop-bool");
1055 unittest(ret == 0, "failed to add prop-bool\n");
1057 of_node_put(np);
1059 ret = of_changeset_apply(&chgset);
1060 if (unittest(ret == 0, "failed to apply changeset\n"))
1061 goto end_changeset_destroy;
1063 np = of_find_node_by_path("/testcase-data/changeset/test-prop");
1064 if (unittest(np, "failed to find test-prop node\n"))
1065 goto end_revert_changeset;
1067 changeset_check_string(np, "prop-string", "abcde");
1068 changeset_check_string_array(np, "prop-string-array", str_array, ARRAY_SIZE(str_array));
1069 changeset_check_u32(np, "prop-u32", 1234);
1070 changeset_check_u32_array(np, "prop-u32-array", u32_array, ARRAY_SIZE(u32_array));
1071 changeset_check_bool(np, "prop-bool");
1073 of_node_put(np);
1075 end_revert_changeset:
1076 ret = of_changeset_revert(&chgset);
1077 unittest(ret == 0, "failed to revert changeset\n");
1079 end_changeset_destroy:
1080 of_changeset_destroy(&chgset);
1081 of_node_put(nchangeset);
1082 #endif
1085 static void __init of_unittest_dma_get_max_cpu_address(void)
1087 struct device_node *np;
1088 phys_addr_t cpu_addr;
1090 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
1091 return;
1093 np = of_find_node_by_path("/testcase-data/address-tests");
1094 if (!np) {
1095 pr_err("missing testcase data\n");
1096 return;
1099 cpu_addr = of_dma_get_max_cpu_address(np);
1100 unittest(cpu_addr == 0x4fffffff,
1101 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
1102 &cpu_addr, 0x4fffffff);
1105 static void __init of_unittest_dma_ranges_one(const char *path,
1106 u64 expect_dma_addr, u64 expect_paddr)
1108 #ifdef CONFIG_HAS_DMA
1109 struct device_node *np;
1110 const struct bus_dma_region *map = NULL;
1111 int rc;
1113 np = of_find_node_by_path(path);
1114 if (!np) {
1115 pr_err("missing testcase data\n");
1116 return;
1119 rc = of_dma_get_range(np, &map);
1121 unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
1123 if (!rc) {
1124 phys_addr_t paddr;
1125 dma_addr_t dma_addr;
1126 struct device *dev_bogus;
1128 dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
1129 if (!dev_bogus) {
1130 unittest(0, "kzalloc() failed\n");
1131 kfree(map);
1132 return;
1135 dev_bogus->dma_range_map = map;
1136 paddr = dma_to_phys(dev_bogus, expect_dma_addr);
1137 dma_addr = phys_to_dma(dev_bogus, expect_paddr);
1139 unittest(paddr == expect_paddr,
1140 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
1141 &paddr, expect_paddr, np);
1142 unittest(dma_addr == expect_dma_addr,
1143 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
1144 &dma_addr, expect_dma_addr, np);
1146 kfree(map);
1147 kfree(dev_bogus);
1149 of_node_put(np);
1150 #endif
1153 static void __init of_unittest_parse_dma_ranges(void)
1155 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
1156 0x0, 0x20000000);
1157 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
1158 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
1159 0x100000000, 0x20000000);
1160 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
1161 0x80000000, 0x20000000);
1164 static void __init of_unittest_pci_dma_ranges(void)
1166 struct device_node *np;
1167 struct of_pci_range range;
1168 struct of_pci_range_parser parser;
1169 int i = 0;
1171 if (!IS_ENABLED(CONFIG_PCI))
1172 return;
1174 np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
1175 if (!np) {
1176 pr_err("missing testcase data\n");
1177 return;
1180 if (of_pci_dma_range_parser_init(&parser, np)) {
1181 pr_err("missing dma-ranges property\n");
1182 return;
1186 * Get the dma-ranges from the device tree
1188 for_each_of_pci_range(&parser, &range) {
1189 if (!i) {
1190 unittest(range.size == 0x10000000,
1191 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1192 np, range.size);
1193 unittest(range.cpu_addr == 0x20000000,
1194 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1195 range.cpu_addr, np);
1196 unittest(range.pci_addr == 0x80000000,
1197 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1198 range.pci_addr, np);
1199 } else {
1200 unittest(range.size == 0x10000000,
1201 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1202 np, range.size);
1203 unittest(range.cpu_addr == 0x40000000,
1204 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1205 range.cpu_addr, np);
1206 unittest(range.pci_addr == 0xc0000000,
1207 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1208 range.pci_addr, np);
1210 i++;
1213 of_node_put(np);
1216 static void __init of_unittest_pci_empty_dma_ranges(void)
1218 struct device_node *np;
1219 struct of_pci_range range;
1220 struct of_pci_range_parser parser;
1222 if (!IS_ENABLED(CONFIG_PCI))
1223 return;
1225 np = of_find_node_by_path("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0");
1226 if (!np) {
1227 pr_err("missing testcase data\n");
1228 return;
1231 if (of_pci_dma_range_parser_init(&parser, np)) {
1232 pr_err("missing dma-ranges property\n");
1233 return;
1237 * Get the dma-ranges from the device tree
1239 for_each_of_pci_range(&parser, &range) {
1240 unittest(range.size == 0x10000000,
1241 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1242 np, range.size);
1243 unittest(range.cpu_addr == 0x00000000,
1244 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1245 range.cpu_addr, np);
1246 unittest(range.pci_addr == 0xc0000000,
1247 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1248 range.pci_addr, np);
1251 of_node_put(np);
1254 static void __init of_unittest_bus_ranges(void)
1256 struct device_node *np;
1257 struct of_range range;
1258 struct of_range_parser parser;
1259 struct resource res;
1260 int ret, count, i = 0;
1262 np = of_find_node_by_path("/testcase-data/address-tests");
1263 if (!np) {
1264 pr_err("missing testcase data\n");
1265 return;
1268 if (of_range_parser_init(&parser, np)) {
1269 pr_err("missing ranges property\n");
1270 return;
1273 ret = of_range_to_resource(np, 1, &res);
1274 unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1275 ret, np);
1276 unittest(resource_type(&res) == IORESOURCE_MEM,
1277 "of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1278 np, &res);
1279 unittest(res.start == 0xd0000000,
1280 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1281 np, &res);
1282 unittest(resource_size(&res) == 0x20000000,
1283 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1284 np, &res);
1286 count = of_range_count(&parser);
1287 unittest(count == 2,
1288 "of_range_count wrong size on node %pOF count=%d\n",
1289 np, count);
1292 * Get the "ranges" from the device tree
1294 for_each_of_range(&parser, &range) {
1295 unittest(range.flags == IORESOURCE_MEM,
1296 "for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1297 np, range.flags, IORESOURCE_MEM);
1298 if (!i) {
1299 unittest(range.size == 0x50000000,
1300 "for_each_of_range wrong size on node %pOF size=%llx\n",
1301 np, range.size);
1302 unittest(range.cpu_addr == 0x70000000,
1303 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1304 range.cpu_addr, np);
1305 unittest(range.bus_addr == 0x70000000,
1306 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1307 range.pci_addr, np);
1308 } else {
1309 unittest(range.size == 0x20000000,
1310 "for_each_of_range wrong size on node %pOF size=%llx\n",
1311 np, range.size);
1312 unittest(range.cpu_addr == 0xd0000000,
1313 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1314 range.cpu_addr, np);
1315 unittest(range.bus_addr == 0x00000000,
1316 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1317 range.pci_addr, np);
1319 i++;
1322 of_node_put(np);
1325 static void __init of_unittest_bus_3cell_ranges(void)
1327 struct device_node *np;
1328 struct of_range range;
1329 struct of_range_parser parser;
1330 int i = 0;
1332 np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1333 if (!np) {
1334 pr_err("missing testcase data\n");
1335 return;
1338 if (of_range_parser_init(&parser, np)) {
1339 pr_err("missing ranges property\n");
1340 return;
1344 * Get the "ranges" from the device tree
1346 for_each_of_range(&parser, &range) {
1347 if (!i) {
1348 unittest(range.flags == 0xf00baa,
1349 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1350 np, range.flags);
1351 unittest(range.size == 0x100000,
1352 "for_each_of_range wrong size on node %pOF size=%llx\n",
1353 np, range.size);
1354 unittest(range.cpu_addr == 0xa0000000,
1355 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1356 range.cpu_addr, np);
1357 unittest(range.bus_addr == 0x0,
1358 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1359 range.pci_addr, np);
1360 } else {
1361 unittest(range.flags == 0xf00bee,
1362 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1363 np, range.flags);
1364 unittest(range.size == 0x200000,
1365 "for_each_of_range wrong size on node %pOF size=%llx\n",
1366 np, range.size);
1367 unittest(range.cpu_addr == 0xb0000000,
1368 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1369 range.cpu_addr, np);
1370 unittest(range.bus_addr == 0x100000000,
1371 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1372 range.pci_addr, np);
1374 i++;
1377 of_node_put(np);
1380 static void __init of_unittest_reg(void)
1382 struct device_node *np;
1383 int ret;
1384 u64 addr, size;
1386 np = of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
1387 if (!np) {
1388 pr_err("missing testcase data\n");
1389 return;
1392 ret = of_property_read_reg(np, 0, &addr, &size);
1393 unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
1394 np, ret);
1395 unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
1396 np, addr);
1398 of_node_put(np);
1401 struct of_unittest_expected_res {
1402 int index;
1403 struct resource res;
1406 static void __init of_unittest_check_addr(const char *node_path,
1407 const struct of_unittest_expected_res *tab_exp,
1408 unsigned int tab_exp_count)
1410 const struct of_unittest_expected_res *expected;
1411 struct device_node *np;
1412 struct resource res;
1413 unsigned int count;
1414 int ret;
1416 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
1417 return;
1419 np = of_find_node_by_path(node_path);
1420 if (!np) {
1421 pr_err("missing testcase data (%s)\n", node_path);
1422 return;
1425 expected = tab_exp;
1426 count = tab_exp_count;
1427 while (count--) {
1428 ret = of_address_to_resource(np, expected->index, &res);
1429 unittest(!ret, "of_address_to_resource(%pOF, %d) returned error %d\n",
1430 np, expected->index, ret);
1431 unittest(resource_type(&res) == resource_type(&expected->res) &&
1432 res.start == expected->res.start &&
1433 resource_size(&res) == resource_size(&expected->res),
1434 "of_address_to_resource(%pOF, %d) wrong resource %pR, expected %pR\n",
1435 np, expected->index, &res, &expected->res);
1436 expected++;
1439 of_node_put(np);
1442 static const struct of_unittest_expected_res of_unittest_reg_2cell_expected_res[] = {
1443 {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1444 {.index = 1, .res = DEFINE_RES_MEM(0xa0a02000, 0x100) },
1445 {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1446 {.index = 3, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1449 static const struct of_unittest_expected_res of_unittest_reg_3cell_expected_res[] = {
1450 {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1451 {.index = 1, .res = DEFINE_RES_MEM(0xa0b02000, 0x100) },
1452 {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1453 {.index = 3, .res = DEFINE_RES_MEM(0xc0c09000, 0x100) },
1454 {.index = 4, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1457 static const struct of_unittest_expected_res of_unittest_reg_pci_expected_res[] = {
1458 {.index = 0, .res = DEFINE_RES_MEM(0xe8001000, 0x1000) },
1459 {.index = 1, .res = DEFINE_RES_MEM(0xea002000, 0x2000) },
1462 static void __init of_unittest_translate_addr(void)
1464 of_unittest_check_addr("/testcase-data/address-tests2/bus-2cell@10000000/device@100000",
1465 of_unittest_reg_2cell_expected_res,
1466 ARRAY_SIZE(of_unittest_reg_2cell_expected_res));
1468 of_unittest_check_addr("/testcase-data/address-tests2/bus-3cell@20000000/local-bus@100000/device@f1001000",
1469 of_unittest_reg_3cell_expected_res,
1470 ARRAY_SIZE(of_unittest_reg_3cell_expected_res));
1472 of_unittest_check_addr("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0/dev@e0000000",
1473 of_unittest_reg_pci_expected_res,
1474 ARRAY_SIZE(of_unittest_reg_pci_expected_res));
1477 static void __init of_unittest_parse_interrupts(void)
1479 struct device_node *np;
1480 struct of_phandle_args args;
1481 int i, rc;
1483 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1484 return;
1486 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1487 if (!np) {
1488 pr_err("missing testcase data\n");
1489 return;
1492 for (i = 0; i < 4; i++) {
1493 bool passed = true;
1495 memset(&args, 0, sizeof(args));
1496 rc = of_irq_parse_one(np, i, &args);
1498 passed &= !rc;
1499 passed &= (args.args_count == 1);
1500 passed &= (args.args[0] == (i + 1));
1502 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1503 i, args.np, rc);
1505 of_node_put(np);
1507 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1508 if (!np) {
1509 pr_err("missing testcase data\n");
1510 return;
1513 for (i = 0; i < 4; i++) {
1514 bool passed = true;
1516 memset(&args, 0, sizeof(args));
1517 rc = of_irq_parse_one(np, i, &args);
1519 /* Test the values from tests-phandle.dtsi */
1520 switch (i) {
1521 case 0:
1522 passed &= !rc;
1523 passed &= (args.args_count == 1);
1524 passed &= (args.args[0] == 9);
1525 break;
1526 case 1:
1527 passed &= !rc;
1528 passed &= (args.args_count == 3);
1529 passed &= (args.args[0] == 10);
1530 passed &= (args.args[1] == 11);
1531 passed &= (args.args[2] == 12);
1532 break;
1533 case 2:
1534 passed &= !rc;
1535 passed &= (args.args_count == 2);
1536 passed &= (args.args[0] == 13);
1537 passed &= (args.args[1] == 14);
1538 break;
1539 case 3:
1540 passed &= !rc;
1541 passed &= (args.args_count == 2);
1542 passed &= (args.args[0] == 15);
1543 passed &= (args.args[1] == 16);
1544 break;
1545 default:
1546 passed = false;
1548 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1549 i, args.np, rc);
1551 of_node_put(np);
1554 static void __init of_unittest_parse_interrupts_extended(void)
1556 struct device_node *np;
1557 struct of_phandle_args args;
1558 int i, rc;
1560 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1561 return;
1563 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1564 if (!np) {
1565 pr_err("missing testcase data\n");
1566 return;
1569 for (i = 0; i < 7; i++) {
1570 bool passed = true;
1572 memset(&args, 0, sizeof(args));
1573 rc = of_irq_parse_one(np, i, &args);
1575 /* Test the values from tests-phandle.dtsi */
1576 switch (i) {
1577 case 0:
1578 passed &= !rc;
1579 passed &= (args.args_count == 1);
1580 passed &= (args.args[0] == 1);
1581 break;
1582 case 1:
1583 passed &= !rc;
1584 passed &= (args.args_count == 3);
1585 passed &= (args.args[0] == 2);
1586 passed &= (args.args[1] == 3);
1587 passed &= (args.args[2] == 4);
1588 break;
1589 case 2:
1590 passed &= !rc;
1591 passed &= (args.args_count == 2);
1592 passed &= (args.args[0] == 5);
1593 passed &= (args.args[1] == 6);
1594 break;
1595 case 3:
1596 passed &= !rc;
1597 passed &= (args.args_count == 1);
1598 passed &= (args.args[0] == 9);
1599 break;
1600 case 4:
1601 passed &= !rc;
1602 passed &= (args.args_count == 3);
1603 passed &= (args.args[0] == 10);
1604 passed &= (args.args[1] == 11);
1605 passed &= (args.args[2] == 12);
1606 break;
1607 case 5:
1608 passed &= !rc;
1609 passed &= (args.args_count == 2);
1610 passed &= (args.args[0] == 13);
1611 passed &= (args.args[1] == 14);
1612 break;
1613 case 6:
1615 * Tests child node that is missing property
1616 * #address-cells. See the comments in
1617 * drivers/of/unittest-data/tests-interrupts.dtsi
1618 * nodes intmap1 and interrupts-extended0
1620 passed &= !rc;
1621 passed &= (args.args_count == 1);
1622 passed &= (args.args[0] == 15);
1623 break;
1624 default:
1625 passed = false;
1628 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1629 i, args.np, rc);
1631 of_node_put(np);
1634 static const struct of_device_id match_node_table[] = {
1635 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1636 { .data = "B", .type = "type1", }, /* followed by type alone */
1638 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1639 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1640 { .data = "Cc", .name = "name2", .type = "type2", },
1642 { .data = "E", .compatible = "compat3" },
1643 { .data = "G", .compatible = "compat2", },
1644 { .data = "H", .compatible = "compat2", .name = "name5", },
1645 { .data = "I", .compatible = "compat2", .type = "type1", },
1646 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1647 { .data = "K", .compatible = "compat2", .name = "name9", },
1651 static struct {
1652 const char *path;
1653 const char *data;
1654 } match_node_tests[] = {
1655 { .path = "/testcase-data/match-node/name0", .data = "A", },
1656 { .path = "/testcase-data/match-node/name1", .data = "B", },
1657 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1658 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1659 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1660 { .path = "/testcase-data/match-node/name3", .data = "E", },
1661 { .path = "/testcase-data/match-node/name4", .data = "G", },
1662 { .path = "/testcase-data/match-node/name5", .data = "H", },
1663 { .path = "/testcase-data/match-node/name6", .data = "G", },
1664 { .path = "/testcase-data/match-node/name7", .data = "I", },
1665 { .path = "/testcase-data/match-node/name8", .data = "J", },
1666 { .path = "/testcase-data/match-node/name9", .data = "K", },
1669 static void __init of_unittest_match_node(void)
1671 struct device_node *np;
1672 const struct of_device_id *match;
1673 int i;
1675 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1676 np = of_find_node_by_path(match_node_tests[i].path);
1677 if (!np) {
1678 unittest(0, "missing testcase node %s\n",
1679 match_node_tests[i].path);
1680 continue;
1683 match = of_match_node(match_node_table, np);
1684 if (!match) {
1685 unittest(0, "%s didn't match anything\n",
1686 match_node_tests[i].path);
1687 continue;
1690 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1691 unittest(0, "%s got wrong match. expected %s, got %s\n",
1692 match_node_tests[i].path, match_node_tests[i].data,
1693 (const char *)match->data);
1694 continue;
1696 unittest(1, "passed");
1700 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1701 static const struct platform_device_info test_bus_info = {
1702 .name = "unittest-bus",
1704 static void __init of_unittest_platform_populate(void)
1706 int irq, rc;
1707 struct device_node *np, *child, *grandchild;
1708 struct platform_device *pdev, *test_bus;
1709 const struct of_device_id match[] = {
1710 { .compatible = "test-device", },
1714 np = of_find_node_by_path("/testcase-data");
1715 of_platform_default_populate(np, NULL, NULL);
1717 /* Test that a missing irq domain returns -EPROBE_DEFER */
1718 np = of_find_node_by_path("/testcase-data/testcase-device1");
1719 pdev = of_find_device_by_node(np);
1720 unittest(pdev, "device 1 creation failed\n");
1722 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1723 irq = platform_get_irq(pdev, 0);
1724 unittest(irq == -EPROBE_DEFER,
1725 "device deferred probe failed - %d\n", irq);
1727 /* Test that a parsing failure does not return -EPROBE_DEFER */
1728 np = of_find_node_by_path("/testcase-data/testcase-device2");
1729 pdev = of_find_device_by_node(np);
1730 unittest(pdev, "device 2 creation failed\n");
1732 EXPECT_BEGIN(KERN_INFO,
1733 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1735 irq = platform_get_irq(pdev, 0);
1737 EXPECT_END(KERN_INFO,
1738 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1740 unittest(irq < 0 && irq != -EPROBE_DEFER,
1741 "device parsing error failed - %d\n", irq);
1744 np = of_find_node_by_path("/testcase-data/platform-tests");
1745 unittest(np, "No testcase data in device tree\n");
1746 if (!np)
1747 return;
1749 test_bus = platform_device_register_full(&test_bus_info);
1750 rc = PTR_ERR_OR_ZERO(test_bus);
1751 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1752 if (rc) {
1753 of_node_put(np);
1754 return;
1756 test_bus->dev.of_node = np;
1759 * Add a dummy resource to the test bus node after it is
1760 * registered to catch problems with un-inserted resources. The
1761 * DT code doesn't insert the resources, and it has caused the
1762 * kernel to oops in the past. This makes sure the same bug
1763 * doesn't crop up again.
1765 platform_device_add_resources(test_bus, &test_bus_res, 1);
1767 of_platform_populate(np, match, NULL, &test_bus->dev);
1768 for_each_child_of_node(np, child) {
1769 for_each_child_of_node(child, grandchild) {
1770 pdev = of_find_device_by_node(grandchild);
1771 unittest(pdev,
1772 "Could not create device for node '%pOFn'\n",
1773 grandchild);
1774 platform_device_put(pdev);
1778 of_platform_depopulate(&test_bus->dev);
1779 for_each_child_of_node(np, child) {
1780 for_each_child_of_node(child, grandchild)
1781 unittest(!of_find_device_by_node(grandchild),
1782 "device didn't get destroyed '%pOFn'\n",
1783 grandchild);
1786 platform_device_unregister(test_bus);
1787 of_node_put(np);
1791 * update_node_properties - adds the properties
1792 * of np into dup node (present in live tree) and
1793 * updates parent of children of np to dup.
1795 * @np: node whose properties are being added to the live tree
1796 * @dup: node present in live tree to be updated
1798 static void update_node_properties(struct device_node *np,
1799 struct device_node *dup)
1801 struct property *prop;
1802 struct property *save_next;
1803 struct device_node *child;
1804 int ret;
1806 for_each_child_of_node(np, child)
1807 child->parent = dup;
1810 * "unittest internal error: unable to add testdata property"
1812 * If this message reports a property in node '/__symbols__' then
1813 * the respective unittest overlay contains a label that has the
1814 * same name as a label in the live devicetree. The label will
1815 * be in the live devicetree only if the devicetree source was
1816 * compiled with the '-@' option. If you encounter this error,
1817 * please consider renaming __all__ of the labels in the unittest
1818 * overlay dts files with an odd prefix that is unlikely to be
1819 * used in a real devicetree.
1823 * open code for_each_property_of_node() because of_add_property()
1824 * sets prop->next to NULL
1826 for (prop = np->properties; prop != NULL; prop = save_next) {
1827 save_next = prop->next;
1828 ret = of_add_property(dup, prop);
1829 if (ret) {
1830 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1831 continue;
1832 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1833 np, prop->name);
1839 * attach_node_and_children - attaches nodes
1840 * and its children to live tree.
1841 * CAUTION: misleading function name - if node @np already exists in
1842 * the live tree then children of @np are *not* attached to the live
1843 * tree. This works for the current test devicetree nodes because such
1844 * nodes do not have child nodes.
1846 * @np: Node to attach to live tree
1848 static void attach_node_and_children(struct device_node *np)
1850 struct device_node *next, *dup, *child;
1851 unsigned long flags;
1852 const char *full_name;
1854 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1855 if (!full_name)
1856 return;
1858 if (!strcmp(full_name, "/__local_fixups__") ||
1859 !strcmp(full_name, "/__fixups__")) {
1860 kfree(full_name);
1861 return;
1864 dup = of_find_node_by_path(full_name);
1865 kfree(full_name);
1866 if (dup) {
1867 update_node_properties(np, dup);
1868 return;
1871 child = np->child;
1872 np->child = NULL;
1874 mutex_lock(&of_mutex);
1875 raw_spin_lock_irqsave(&devtree_lock, flags);
1876 np->sibling = np->parent->child;
1877 np->parent->child = np;
1878 of_node_clear_flag(np, OF_DETACHED);
1879 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1881 __of_attach_node_sysfs(np);
1882 mutex_unlock(&of_mutex);
1884 while (child) {
1885 next = child->sibling;
1886 attach_node_and_children(child);
1887 child = next;
1892 * unittest_data_add - Reads, copies data from
1893 * linked tree and attaches it to the live tree
1895 static int __init unittest_data_add(void)
1897 void *unittest_data;
1898 void *unittest_data_align;
1899 struct device_node *unittest_data_node = NULL, *np;
1901 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1902 * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
1904 extern uint8_t __dtbo_testcases_begin[];
1905 extern uint8_t __dtbo_testcases_end[];
1906 const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
1907 int rc;
1908 void *ret;
1910 if (!size) {
1911 pr_warn("%s: testcases is empty\n", __func__);
1912 return -ENODATA;
1915 /* creating copy */
1916 unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
1917 if (!unittest_data)
1918 return -ENOMEM;
1920 unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1921 memcpy(unittest_data_align, __dtbo_testcases_begin, size);
1923 ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
1924 if (!ret) {
1925 pr_warn("%s: unflatten testcases tree failed\n", __func__);
1926 kfree(unittest_data);
1927 return -ENODATA;
1929 if (!unittest_data_node) {
1930 pr_warn("%s: testcases tree is empty\n", __func__);
1931 kfree(unittest_data);
1932 return -ENODATA;
1936 * This lock normally encloses of_resolve_phandles()
1938 of_overlay_mutex_lock();
1940 rc = of_resolve_phandles(unittest_data_node);
1941 if (rc) {
1942 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1943 of_overlay_mutex_unlock();
1944 return -EINVAL;
1947 /* attach the sub-tree to live tree */
1948 if (!of_root) {
1949 pr_warn("%s: no live tree to attach sub-tree\n", __func__);
1950 kfree(unittest_data);
1951 return -ENODEV;
1954 EXPECT_BEGIN(KERN_INFO,
1955 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1957 np = unittest_data_node->child;
1958 while (np) {
1959 struct device_node *next = np->sibling;
1961 np->parent = of_root;
1962 /* this will clear OF_DETACHED in np and children */
1963 attach_node_and_children(np);
1964 np = next;
1967 EXPECT_END(KERN_INFO,
1968 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1970 of_overlay_mutex_unlock();
1972 return 0;
1975 #ifdef CONFIG_OF_OVERLAY
1976 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
1978 static int unittest_probe(struct platform_device *pdev)
1980 struct device *dev = &pdev->dev;
1981 struct device_node *np = dev->of_node;
1983 if (np == NULL) {
1984 dev_err(dev, "No OF data for device\n");
1985 return -EINVAL;
1989 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1991 of_platform_populate(np, NULL, NULL, &pdev->dev);
1993 return 0;
1996 static void unittest_remove(struct platform_device *pdev)
1998 struct device *dev = &pdev->dev;
1999 struct device_node *np = dev->of_node;
2001 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2004 static const struct of_device_id unittest_match[] = {
2005 { .compatible = "unittest", },
2009 static struct platform_driver unittest_driver = {
2010 .probe = unittest_probe,
2011 .remove = unittest_remove,
2012 .driver = {
2013 .name = "unittest",
2014 .of_match_table = unittest_match,
2018 /* get the platform device instantiated at the path */
2019 static struct platform_device *of_path_to_platform_device(const char *path)
2021 struct device_node *np;
2022 struct platform_device *pdev;
2024 np = of_find_node_by_path(path);
2025 if (np == NULL)
2026 return NULL;
2028 pdev = of_find_device_by_node(np);
2029 of_node_put(np);
2031 return pdev;
2034 /* find out if a platform device exists at that path */
2035 static int of_path_platform_device_exists(const char *path)
2037 struct platform_device *pdev;
2039 pdev = of_path_to_platform_device(path);
2040 platform_device_put(pdev);
2041 return pdev != NULL;
2044 #ifdef CONFIG_OF_GPIO
2046 struct unittest_gpio_dev {
2047 struct gpio_chip chip;
2050 static int unittest_gpio_chip_request_count;
2051 static int unittest_gpio_probe_count;
2052 static int unittest_gpio_probe_pass_count;
2054 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
2056 unittest_gpio_chip_request_count++;
2058 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
2059 unittest_gpio_chip_request_count);
2060 return 0;
2063 static int unittest_gpio_probe(struct platform_device *pdev)
2065 struct unittest_gpio_dev *devptr;
2066 int ret;
2068 unittest_gpio_probe_count++;
2070 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
2071 if (!devptr)
2072 return -ENOMEM;
2074 platform_set_drvdata(pdev, devptr);
2076 devptr->chip.fwnode = dev_fwnode(&pdev->dev);
2077 devptr->chip.label = "of-unittest-gpio";
2078 devptr->chip.base = -1; /* dynamic allocation */
2079 devptr->chip.ngpio = 5;
2080 devptr->chip.request = unittest_gpio_chip_request;
2082 ret = gpiochip_add_data(&devptr->chip, NULL);
2084 unittest(!ret,
2085 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
2087 if (!ret)
2088 unittest_gpio_probe_pass_count++;
2089 return ret;
2092 static void unittest_gpio_remove(struct platform_device *pdev)
2094 struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
2095 struct device *dev = &pdev->dev;
2097 dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
2099 if (devptr->chip.base != -1)
2100 gpiochip_remove(&devptr->chip);
2102 kfree(devptr);
2105 static const struct of_device_id unittest_gpio_id[] = {
2106 { .compatible = "unittest-gpio", },
2110 static struct platform_driver unittest_gpio_driver = {
2111 .probe = unittest_gpio_probe,
2112 .remove = unittest_gpio_remove,
2113 .driver = {
2114 .name = "unittest-gpio",
2115 .of_match_table = unittest_gpio_id,
2119 static void __init of_unittest_overlay_gpio(void)
2121 int chip_request_count;
2122 int probe_pass_count;
2123 int ret;
2126 * tests: apply overlays before registering driver
2127 * Similar to installing a driver as a module, the
2128 * driver is registered after applying the overlays.
2130 * The overlays are applied by overlay_data_apply()
2131 * instead of of_unittest_apply_overlay() so that they
2132 * will not be tracked. Thus they will not be removed
2133 * by of_unittest_remove_tracked_overlays().
2135 * - apply overlay_gpio_01
2136 * - apply overlay_gpio_02a
2137 * - apply overlay_gpio_02b
2138 * - register driver
2140 * register driver will result in
2141 * - probe and processing gpio hog for overlay_gpio_01
2142 * - probe for overlay_gpio_02a
2143 * - processing gpio for overlay_gpio_02b
2146 probe_pass_count = unittest_gpio_probe_pass_count;
2147 chip_request_count = unittest_gpio_chip_request_count;
2150 * overlay_gpio_01 contains gpio node and child gpio hog node
2151 * overlay_gpio_02a contains gpio node
2152 * overlay_gpio_02b contains child gpio hog node
2155 unittest(overlay_data_apply("overlay_gpio_01", NULL),
2156 "Adding overlay 'overlay_gpio_01' failed\n");
2158 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
2159 "Adding overlay 'overlay_gpio_02a' failed\n");
2161 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
2162 "Adding overlay 'overlay_gpio_02b' failed\n");
2164 ret = platform_driver_register(&unittest_gpio_driver);
2165 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
2166 return;
2168 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
2169 "unittest_gpio_probe() failed or not called\n");
2171 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
2172 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2173 unittest_gpio_chip_request_count - chip_request_count);
2176 * tests: apply overlays after registering driver
2178 * Similar to a driver built-in to the kernel, the
2179 * driver is registered before applying the overlays.
2181 * overlay_gpio_03 contains gpio node and child gpio hog node
2183 * - apply overlay_gpio_03
2185 * apply overlay will result in
2186 * - probe and processing gpio hog.
2189 probe_pass_count = unittest_gpio_probe_pass_count;
2190 chip_request_count = unittest_gpio_chip_request_count;
2192 /* overlay_gpio_03 contains gpio node and child gpio hog node */
2194 unittest(overlay_data_apply("overlay_gpio_03", NULL),
2195 "Adding overlay 'overlay_gpio_03' failed\n");
2197 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2198 "unittest_gpio_probe() failed or not called\n");
2200 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2201 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2202 unittest_gpio_chip_request_count - chip_request_count);
2205 * overlay_gpio_04a contains gpio node
2207 * - apply overlay_gpio_04a
2209 * apply the overlay will result in
2210 * - probe for overlay_gpio_04a
2213 probe_pass_count = unittest_gpio_probe_pass_count;
2214 chip_request_count = unittest_gpio_chip_request_count;
2216 /* overlay_gpio_04a contains gpio node */
2218 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
2219 "Adding overlay 'overlay_gpio_04a' failed\n");
2221 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2222 "unittest_gpio_probe() failed or not called\n");
2225 * overlay_gpio_04b contains child gpio hog node
2227 * - apply overlay_gpio_04b
2229 * apply the overlay will result in
2230 * - processing gpio for overlay_gpio_04b
2233 /* overlay_gpio_04b contains child gpio hog node */
2235 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
2236 "Adding overlay 'overlay_gpio_04b' failed\n");
2238 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2239 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2240 unittest_gpio_chip_request_count - chip_request_count);
2243 #else
2245 static void __init of_unittest_overlay_gpio(void)
2247 /* skip tests */
2250 #endif
2252 #if IS_BUILTIN(CONFIG_I2C)
2254 /* get the i2c client device instantiated at the path */
2255 static struct i2c_client *of_path_to_i2c_client(const char *path)
2257 struct device_node *np;
2258 struct i2c_client *client;
2260 np = of_find_node_by_path(path);
2261 if (np == NULL)
2262 return NULL;
2264 client = of_find_i2c_device_by_node(np);
2265 of_node_put(np);
2267 return client;
2270 /* find out if a i2c client device exists at that path */
2271 static int of_path_i2c_client_exists(const char *path)
2273 struct i2c_client *client;
2275 client = of_path_to_i2c_client(path);
2276 if (client)
2277 put_device(&client->dev);
2278 return client != NULL;
2280 #else
2281 static int of_path_i2c_client_exists(const char *path)
2283 return 0;
2285 #endif
2287 enum overlay_type {
2288 PDEV_OVERLAY,
2289 I2C_OVERLAY
2292 static int of_path_device_type_exists(const char *path,
2293 enum overlay_type ovtype)
2295 switch (ovtype) {
2296 case PDEV_OVERLAY:
2297 return of_path_platform_device_exists(path);
2298 case I2C_OVERLAY:
2299 return of_path_i2c_client_exists(path);
2301 return 0;
2304 static const char *unittest_path(int nr, enum overlay_type ovtype)
2306 const char *base;
2307 static char buf[256];
2309 switch (ovtype) {
2310 case PDEV_OVERLAY:
2311 base = "/testcase-data/overlay-node/test-bus";
2312 break;
2313 case I2C_OVERLAY:
2314 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2315 break;
2316 default:
2317 buf[0] = '\0';
2318 return buf;
2320 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2321 buf[sizeof(buf) - 1] = '\0';
2322 return buf;
2325 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2327 const char *path;
2329 path = unittest_path(unittest_nr, ovtype);
2331 switch (ovtype) {
2332 case PDEV_OVERLAY:
2333 return of_path_platform_device_exists(path);
2334 case I2C_OVERLAY:
2335 return of_path_i2c_client_exists(path);
2337 return 0;
2340 static const char *overlay_name_from_nr(int nr)
2342 static char buf[256];
2344 snprintf(buf, sizeof(buf) - 1,
2345 "overlay_%d", nr);
2346 buf[sizeof(buf) - 1] = '\0';
2348 return buf;
2351 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2353 #define MAX_TRACK_OVCS_IDS 256
2355 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2356 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2357 static int track_ovcs_id_cnt;
2359 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2361 if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2362 return;
2364 track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2365 track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2366 track_ovcs_id_cnt++;
2369 static void of_unittest_untrack_overlay(int ovcs_id)
2371 if (WARN_ON(track_ovcs_id_cnt < 1))
2372 return;
2374 track_ovcs_id_cnt--;
2376 /* If out of synch then test is broken. Do not try to recover. */
2377 WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2380 static void of_unittest_remove_tracked_overlays(void)
2382 int ret, ovcs_id, overlay_nr, save_ovcs_id;
2383 const char *overlay_name;
2385 while (track_ovcs_id_cnt > 0) {
2387 ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2388 overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2389 save_ovcs_id = ovcs_id;
2390 ret = of_overlay_remove(&ovcs_id);
2391 if (ret == -ENODEV) {
2392 overlay_name = overlay_name_from_nr(overlay_nr);
2393 pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2394 __func__, overlay_name, ret);
2396 of_unittest_untrack_overlay(save_ovcs_id);
2401 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2404 * The overlay will be tracked, thus it will be removed
2405 * by of_unittest_remove_tracked_overlays().
2408 const char *overlay_name;
2410 overlay_name = overlay_name_from_nr(overlay_nr);
2412 if (!overlay_data_apply(overlay_name, ovcs_id)) {
2413 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2414 return -EFAULT;
2416 of_unittest_track_overlay(*ovcs_id, overlay_nr);
2418 return 0;
2421 static int __init __of_unittest_apply_overlay_check(int overlay_nr,
2422 int unittest_nr, int before, int after,
2423 enum overlay_type ovtype)
2425 int ret, ovcs_id;
2427 /* unittest device must be in before state */
2428 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2429 unittest(0, "%s with device @\"%s\" %s\n",
2430 overlay_name_from_nr(overlay_nr),
2431 unittest_path(unittest_nr, ovtype),
2432 !before ? "enabled" : "disabled");
2433 return -EINVAL;
2436 /* apply the overlay */
2437 ovcs_id = 0;
2438 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2439 if (ret != 0) {
2440 /* of_unittest_apply_overlay already called unittest() */
2441 return ret;
2444 /* unittest device must be in after state */
2445 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2446 unittest(0, "%s with device @\"%s\" %s\n",
2447 overlay_name_from_nr(overlay_nr),
2448 unittest_path(unittest_nr, ovtype),
2449 !after ? "enabled" : "disabled");
2450 return -EINVAL;
2453 return ovcs_id;
2456 /* apply an overlay while checking before and after states */
2457 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2458 int unittest_nr, int before, int after,
2459 enum overlay_type ovtype)
2461 int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
2462 unittest_nr, before, after, ovtype);
2463 if (ovcs_id < 0)
2464 return ovcs_id;
2466 return 0;
2469 /* apply an overlay and then revert it while checking before, after states */
2470 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2471 int unittest_nr, int before, int after,
2472 enum overlay_type ovtype)
2474 int ret, ovcs_id, save_ovcs_id;
2476 ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
2477 before, after, ovtype);
2478 if (ovcs_id < 0)
2479 return ovcs_id;
2481 /* remove the overlay */
2482 save_ovcs_id = ovcs_id;
2483 ret = of_overlay_remove(&ovcs_id);
2484 if (ret != 0) {
2485 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2486 overlay_name_from_nr(overlay_nr),
2487 unittest_path(unittest_nr, ovtype));
2488 return ret;
2490 of_unittest_untrack_overlay(save_ovcs_id);
2492 /* unittest device must be again in before state */
2493 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2494 unittest(0, "%s with device @\"%s\" %s\n",
2495 overlay_name_from_nr(overlay_nr),
2496 unittest_path(unittest_nr, ovtype),
2497 !before ? "enabled" : "disabled");
2498 return -EINVAL;
2501 return 0;
2504 /* test activation of device */
2505 static void __init of_unittest_overlay_0(void)
2507 int ret;
2509 EXPECT_BEGIN(KERN_INFO,
2510 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2512 /* device should enable */
2513 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2515 EXPECT_END(KERN_INFO,
2516 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2518 if (ret)
2519 return;
2521 unittest(1, "overlay test %d passed\n", 0);
2524 /* test deactivation of device */
2525 static void __init of_unittest_overlay_1(void)
2527 int ret;
2529 EXPECT_BEGIN(KERN_INFO,
2530 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2532 /* device should disable */
2533 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2535 EXPECT_END(KERN_INFO,
2536 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2538 if (ret)
2539 return;
2541 unittest(1, "overlay test %d passed\n", 1);
2545 /* test activation of device */
2546 static void __init of_unittest_overlay_2(void)
2548 int ret;
2550 EXPECT_BEGIN(KERN_INFO,
2551 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2553 /* device should enable */
2554 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2556 EXPECT_END(KERN_INFO,
2557 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2559 if (ret)
2560 return;
2561 unittest(1, "overlay test %d passed\n", 2);
2564 /* test deactivation of device */
2565 static void __init of_unittest_overlay_3(void)
2567 int ret;
2569 EXPECT_BEGIN(KERN_INFO,
2570 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2572 /* device should disable */
2573 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2575 EXPECT_END(KERN_INFO,
2576 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2578 if (ret)
2579 return;
2581 unittest(1, "overlay test %d passed\n", 3);
2584 /* test activation of a full device node */
2585 static void __init of_unittest_overlay_4(void)
2587 /* device should disable */
2588 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2589 return;
2591 unittest(1, "overlay test %d passed\n", 4);
2594 /* test overlay apply/revert sequence */
2595 static void __init of_unittest_overlay_5(void)
2597 int ret;
2599 EXPECT_BEGIN(KERN_INFO,
2600 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2602 /* device should disable */
2603 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2605 EXPECT_END(KERN_INFO,
2606 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2608 if (ret)
2609 return;
2611 unittest(1, "overlay test %d passed\n", 5);
2614 /* test overlay application in sequence */
2615 static void __init of_unittest_overlay_6(void)
2617 int i, save_ovcs_id[2], ovcs_id;
2618 int overlay_nr = 6, unittest_nr = 6;
2619 int before = 0, after = 1;
2620 const char *overlay_name;
2622 int ret;
2624 /* unittest device must be in before state */
2625 for (i = 0; i < 2; i++) {
2626 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2627 != before) {
2628 unittest(0, "%s with device @\"%s\" %s\n",
2629 overlay_name_from_nr(overlay_nr + i),
2630 unittest_path(unittest_nr + i,
2631 PDEV_OVERLAY),
2632 !before ? "enabled" : "disabled");
2633 return;
2637 /* apply the overlays */
2639 EXPECT_BEGIN(KERN_INFO,
2640 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2642 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2644 ret = overlay_data_apply(overlay_name, &ovcs_id);
2646 if (!ret) {
2647 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2648 return;
2650 save_ovcs_id[0] = ovcs_id;
2651 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2653 EXPECT_END(KERN_INFO,
2654 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2656 EXPECT_BEGIN(KERN_INFO,
2657 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2659 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2661 ret = overlay_data_apply(overlay_name, &ovcs_id);
2663 if (!ret) {
2664 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2665 return;
2667 save_ovcs_id[1] = ovcs_id;
2668 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2670 EXPECT_END(KERN_INFO,
2671 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2674 for (i = 0; i < 2; i++) {
2675 /* unittest device must be in after state */
2676 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2677 != after) {
2678 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2679 overlay_name_from_nr(overlay_nr + i),
2680 unittest_path(unittest_nr + i,
2681 PDEV_OVERLAY),
2682 !after ? "enabled" : "disabled");
2683 return;
2687 for (i = 1; i >= 0; i--) {
2688 ovcs_id = save_ovcs_id[i];
2689 if (of_overlay_remove(&ovcs_id)) {
2690 unittest(0, "%s failed destroy @\"%s\"\n",
2691 overlay_name_from_nr(overlay_nr + i),
2692 unittest_path(unittest_nr + i,
2693 PDEV_OVERLAY));
2694 return;
2696 of_unittest_untrack_overlay(save_ovcs_id[i]);
2699 for (i = 0; i < 2; i++) {
2700 /* unittest device must be again in before state */
2701 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2702 != before) {
2703 unittest(0, "%s with device @\"%s\" %s\n",
2704 overlay_name_from_nr(overlay_nr + i),
2705 unittest_path(unittest_nr + i,
2706 PDEV_OVERLAY),
2707 !before ? "enabled" : "disabled");
2708 return;
2712 unittest(1, "overlay test %d passed\n", 6);
2716 /* test overlay application in sequence */
2717 static void __init of_unittest_overlay_8(void)
2719 int i, save_ovcs_id[2], ovcs_id;
2720 int overlay_nr = 8, unittest_nr = 8;
2721 const char *overlay_name;
2722 int ret;
2724 /* we don't care about device state in this test */
2726 EXPECT_BEGIN(KERN_INFO,
2727 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2729 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2731 ret = overlay_data_apply(overlay_name, &ovcs_id);
2732 if (!ret)
2733 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2735 EXPECT_END(KERN_INFO,
2736 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2738 if (!ret)
2739 return;
2741 save_ovcs_id[0] = ovcs_id;
2742 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2744 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2746 EXPECT_BEGIN(KERN_INFO,
2747 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2749 /* apply the overlays */
2750 ret = overlay_data_apply(overlay_name, &ovcs_id);
2752 EXPECT_END(KERN_INFO,
2753 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2755 if (!ret) {
2756 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2757 return;
2760 save_ovcs_id[1] = ovcs_id;
2761 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2763 /* now try to remove first overlay (it should fail) */
2764 ovcs_id = save_ovcs_id[0];
2766 EXPECT_BEGIN(KERN_INFO,
2767 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2769 EXPECT_BEGIN(KERN_INFO,
2770 "OF: overlay: overlay #6 is not topmost");
2772 ret = of_overlay_remove(&ovcs_id);
2774 EXPECT_END(KERN_INFO,
2775 "OF: overlay: overlay #6 is not topmost");
2777 EXPECT_END(KERN_INFO,
2778 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2780 if (!ret) {
2782 * Should never get here. If we do, expect a lot of
2783 * subsequent tracking and overlay removal related errors.
2785 unittest(0, "%s was destroyed @\"%s\"\n",
2786 overlay_name_from_nr(overlay_nr + 0),
2787 unittest_path(unittest_nr,
2788 PDEV_OVERLAY));
2789 return;
2792 /* removing them in order should work */
2793 for (i = 1; i >= 0; i--) {
2794 ovcs_id = save_ovcs_id[i];
2795 if (of_overlay_remove(&ovcs_id)) {
2796 unittest(0, "%s not destroyed @\"%s\"\n",
2797 overlay_name_from_nr(overlay_nr + i),
2798 unittest_path(unittest_nr,
2799 PDEV_OVERLAY));
2800 return;
2802 of_unittest_untrack_overlay(save_ovcs_id[i]);
2805 unittest(1, "overlay test %d passed\n", 8);
2808 /* test insertion of a bus with parent devices */
2809 static void __init of_unittest_overlay_10(void)
2811 int ret;
2812 char *child_path;
2814 /* device should disable */
2815 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2817 if (unittest(ret == 0,
2818 "overlay test %d failed; overlay application\n", 10))
2819 return;
2821 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2822 unittest_path(10, PDEV_OVERLAY));
2823 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2824 return;
2826 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2827 kfree(child_path);
2829 unittest(ret, "overlay test %d failed; no child device\n", 10);
2832 /* test insertion of a bus with parent devices (and revert) */
2833 static void __init of_unittest_overlay_11(void)
2835 int ret;
2837 /* device should disable */
2838 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2839 PDEV_OVERLAY);
2841 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2844 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2846 struct unittest_i2c_bus_data {
2847 struct platform_device *pdev;
2848 struct i2c_adapter adap;
2851 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2852 struct i2c_msg *msgs, int num)
2854 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2856 (void)std;
2858 return num;
2861 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2863 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2866 static const struct i2c_algorithm unittest_i2c_algo = {
2867 .master_xfer = unittest_i2c_master_xfer,
2868 .functionality = unittest_i2c_functionality,
2871 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2873 struct device *dev = &pdev->dev;
2874 struct device_node *np = dev->of_node;
2875 struct unittest_i2c_bus_data *std;
2876 struct i2c_adapter *adap;
2877 int ret;
2879 if (np == NULL) {
2880 dev_err(dev, "No OF data for device\n");
2881 return -EINVAL;
2885 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2887 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2888 if (!std)
2889 return -ENOMEM;
2891 /* link them together */
2892 std->pdev = pdev;
2893 platform_set_drvdata(pdev, std);
2895 adap = &std->adap;
2896 i2c_set_adapdata(adap, std);
2897 adap->nr = -1;
2898 strscpy(adap->name, pdev->name, sizeof(adap->name));
2899 adap->class = I2C_CLASS_DEPRECATED;
2900 adap->algo = &unittest_i2c_algo;
2901 adap->dev.parent = dev;
2902 adap->dev.of_node = dev->of_node;
2903 adap->timeout = 5 * HZ;
2904 adap->retries = 3;
2906 ret = i2c_add_numbered_adapter(adap);
2907 if (ret != 0) {
2908 dev_err(dev, "Failed to add I2C adapter\n");
2909 return ret;
2912 return 0;
2915 static void unittest_i2c_bus_remove(struct platform_device *pdev)
2917 struct device *dev = &pdev->dev;
2918 struct device_node *np = dev->of_node;
2919 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2921 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2922 i2c_del_adapter(&std->adap);
2925 static const struct of_device_id unittest_i2c_bus_match[] = {
2926 { .compatible = "unittest-i2c-bus", },
2930 static struct platform_driver unittest_i2c_bus_driver = {
2931 .probe = unittest_i2c_bus_probe,
2932 .remove = unittest_i2c_bus_remove,
2933 .driver = {
2934 .name = "unittest-i2c-bus",
2935 .of_match_table = unittest_i2c_bus_match,
2939 static int unittest_i2c_dev_probe(struct i2c_client *client)
2941 struct device *dev = &client->dev;
2942 struct device_node *np = client->dev.of_node;
2944 if (!np) {
2945 dev_err(dev, "No OF node\n");
2946 return -EINVAL;
2949 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2951 return 0;
2954 static void unittest_i2c_dev_remove(struct i2c_client *client)
2956 struct device *dev = &client->dev;
2957 struct device_node *np = client->dev.of_node;
2959 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2962 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2963 { .name = "unittest-i2c-dev" },
2967 static struct i2c_driver unittest_i2c_dev_driver = {
2968 .driver = {
2969 .name = "unittest-i2c-dev",
2971 .probe = unittest_i2c_dev_probe,
2972 .remove = unittest_i2c_dev_remove,
2973 .id_table = unittest_i2c_dev_id,
2976 #if IS_BUILTIN(CONFIG_I2C_MUX)
2978 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2980 return 0;
2983 static int unittest_i2c_mux_probe(struct i2c_client *client)
2985 int i, nchans;
2986 struct device *dev = &client->dev;
2987 struct i2c_adapter *adap = client->adapter;
2988 struct device_node *np = client->dev.of_node, *child;
2989 struct i2c_mux_core *muxc;
2990 u32 reg, max_reg;
2992 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2994 if (!np) {
2995 dev_err(dev, "No OF node\n");
2996 return -EINVAL;
2999 max_reg = (u32)-1;
3000 for_each_child_of_node(np, child) {
3001 if (of_property_read_u32(child, "reg", &reg))
3002 continue;
3003 if (max_reg == (u32)-1 || reg > max_reg)
3004 max_reg = reg;
3006 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
3007 if (nchans == 0) {
3008 dev_err(dev, "No channels\n");
3009 return -EINVAL;
3012 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
3013 unittest_i2c_mux_select_chan, NULL);
3014 if (!muxc)
3015 return -ENOMEM;
3016 for (i = 0; i < nchans; i++) {
3017 if (i2c_mux_add_adapter(muxc, 0, i)) {
3018 dev_err(dev, "Failed to register mux #%d\n", i);
3019 i2c_mux_del_adapters(muxc);
3020 return -ENODEV;
3024 i2c_set_clientdata(client, muxc);
3026 return 0;
3029 static void unittest_i2c_mux_remove(struct i2c_client *client)
3031 struct device *dev = &client->dev;
3032 struct device_node *np = client->dev.of_node;
3033 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
3035 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3036 i2c_mux_del_adapters(muxc);
3039 static const struct i2c_device_id unittest_i2c_mux_id[] = {
3040 { .name = "unittest-i2c-mux" },
3044 static struct i2c_driver unittest_i2c_mux_driver = {
3045 .driver = {
3046 .name = "unittest-i2c-mux",
3048 .probe = unittest_i2c_mux_probe,
3049 .remove = unittest_i2c_mux_remove,
3050 .id_table = unittest_i2c_mux_id,
3053 #endif
3055 static int of_unittest_overlay_i2c_init(void)
3057 int ret;
3059 ret = i2c_add_driver(&unittest_i2c_dev_driver);
3060 if (unittest(ret == 0,
3061 "could not register unittest i2c device driver\n"))
3062 return ret;
3064 ret = platform_driver_register(&unittest_i2c_bus_driver);
3066 if (unittest(ret == 0,
3067 "could not register unittest i2c bus driver\n"))
3068 return ret;
3070 #if IS_BUILTIN(CONFIG_I2C_MUX)
3072 EXPECT_BEGIN(KERN_INFO,
3073 "i2c i2c-1: Added multiplexed i2c bus 2");
3075 ret = i2c_add_driver(&unittest_i2c_mux_driver);
3077 EXPECT_END(KERN_INFO,
3078 "i2c i2c-1: Added multiplexed i2c bus 2");
3080 if (unittest(ret == 0,
3081 "could not register unittest i2c mux driver\n"))
3082 return ret;
3083 #endif
3085 return 0;
3088 static void of_unittest_overlay_i2c_cleanup(void)
3090 #if IS_BUILTIN(CONFIG_I2C_MUX)
3091 i2c_del_driver(&unittest_i2c_mux_driver);
3092 #endif
3093 platform_driver_unregister(&unittest_i2c_bus_driver);
3094 i2c_del_driver(&unittest_i2c_dev_driver);
3097 static void __init of_unittest_overlay_i2c_12(void)
3099 int ret;
3101 /* device should enable */
3102 EXPECT_BEGIN(KERN_INFO,
3103 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
3105 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
3107 EXPECT_END(KERN_INFO,
3108 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
3110 if (ret)
3111 return;
3113 unittest(1, "overlay test %d passed\n", 12);
3116 /* test deactivation of device */
3117 static void __init of_unittest_overlay_i2c_13(void)
3119 int ret;
3121 EXPECT_BEGIN(KERN_INFO,
3122 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
3124 /* device should disable */
3125 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
3127 EXPECT_END(KERN_INFO,
3128 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
3130 if (ret)
3131 return;
3133 unittest(1, "overlay test %d passed\n", 13);
3136 /* just check for i2c mux existence */
3137 static void of_unittest_overlay_i2c_14(void)
3141 static void __init of_unittest_overlay_i2c_15(void)
3143 int ret;
3145 /* device should enable */
3146 EXPECT_BEGIN(KERN_INFO,
3147 "i2c i2c-1: Added multiplexed i2c bus 3");
3149 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
3151 EXPECT_END(KERN_INFO,
3152 "i2c i2c-1: Added multiplexed i2c bus 3");
3154 if (ret)
3155 return;
3157 unittest(1, "overlay test %d passed\n", 15);
3160 #else
3162 static inline void of_unittest_overlay_i2c_14(void) { }
3163 static inline void of_unittest_overlay_i2c_15(void) { }
3165 #endif
3167 static int of_notify(struct notifier_block *nb, unsigned long action,
3168 void *arg)
3170 struct of_overlay_notify_data *nd = arg;
3171 struct device_node *found;
3172 int ret;
3175 * For overlay_16 .. overlay_19, check that returning an error
3176 * works for each of the actions by setting an arbitrary return
3177 * error number that matches the test number. e.g. for unittest16,
3178 * ret = -EBUSY which is -16.
3180 * OVERLAY_INFO() for the overlays is declared to expect the same
3181 * error number, so overlay_data_apply() will return no error.
3183 * overlay_20 will return NOTIFY_DONE
3186 ret = 0;
3187 of_node_get(nd->overlay);
3189 switch (action) {
3191 case OF_OVERLAY_PRE_APPLY:
3192 found = of_find_node_by_name(nd->overlay, "test-unittest16");
3193 if (found) {
3194 of_node_put(found);
3195 ret = -EBUSY;
3197 break;
3199 case OF_OVERLAY_POST_APPLY:
3200 found = of_find_node_by_name(nd->overlay, "test-unittest17");
3201 if (found) {
3202 of_node_put(found);
3203 ret = -EEXIST;
3205 break;
3207 case OF_OVERLAY_PRE_REMOVE:
3208 found = of_find_node_by_name(nd->overlay, "test-unittest18");
3209 if (found) {
3210 of_node_put(found);
3211 ret = -EXDEV;
3213 break;
3215 case OF_OVERLAY_POST_REMOVE:
3216 found = of_find_node_by_name(nd->overlay, "test-unittest19");
3217 if (found) {
3218 of_node_put(found);
3219 ret = -ENODEV;
3221 break;
3223 default: /* should not happen */
3224 of_node_put(nd->overlay);
3225 ret = -EINVAL;
3226 break;
3229 if (ret)
3230 return notifier_from_errno(ret);
3232 return NOTIFY_DONE;
3235 static struct notifier_block of_nb = {
3236 .notifier_call = of_notify,
3239 static void __init of_unittest_overlay_notify(void)
3241 int ovcs_id;
3242 int ret;
3244 ret = of_overlay_notifier_register(&of_nb);
3245 unittest(!ret,
3246 "of_overlay_notifier_register() failed, ret = %d\n", ret);
3247 if (ret)
3248 return;
3251 * The overlays are applied by overlay_data_apply()
3252 * instead of of_unittest_apply_overlay() so that they
3253 * will not be tracked. Thus they will not be removed
3254 * by of_unittest_remove_tracked_overlays().
3256 * Applying overlays 16 - 19 will each trigger an error for a
3257 * different action in of_notify().
3259 * Applying overlay 20 will not trigger any error in of_notify().
3262 /* --- overlay 16 --- */
3264 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3266 unittest(overlay_data_apply("overlay_16", &ovcs_id),
3267 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
3269 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3271 unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
3273 /* --- overlay 17 --- */
3275 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3277 unittest(overlay_data_apply("overlay_17", &ovcs_id),
3278 "test OF_OVERLAY_POST_APPLY notify injected error\n");
3280 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3282 unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
3284 /* --- overlay 18 --- */
3286 unittest(overlay_data_apply("overlay_18", &ovcs_id),
3287 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3289 unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
3291 if (ovcs_id) {
3292 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3294 ret = of_overlay_remove(&ovcs_id);
3295 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3296 if (ret == -EXDEV) {
3298 * change set ovcs_id should still exist
3300 unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3301 } else {
3302 unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3304 } else {
3305 unittest(1, "ovcs_id not created for overlay_18\n");
3308 unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3310 /* --- overlay 19 --- */
3312 unittest(overlay_data_apply("overlay_19", &ovcs_id),
3313 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3315 unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3317 if (ovcs_id) {
3318 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3319 ret = of_overlay_remove(&ovcs_id);
3320 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3321 if (ret == -ENODEV)
3322 unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3323 else
3324 unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3325 } else {
3326 unittest(1, "ovcs_id removed for overlay_19\n");
3329 unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3330 ovcs_id);
3332 /* --- overlay 20 --- */
3334 unittest(overlay_data_apply("overlay_20", &ovcs_id),
3335 "overlay notify no injected error\n");
3337 if (ovcs_id) {
3338 ret = of_overlay_remove(&ovcs_id);
3339 if (ret)
3340 unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3341 ret);
3342 } else {
3343 unittest(1, "ovcs_id not created for overlay_20\n");
3346 unittest(!of_overlay_notifier_unregister(&of_nb),
3347 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3350 static void __init of_unittest_overlay(void)
3352 struct device_node *bus_np = NULL;
3353 unsigned int i;
3355 if (platform_driver_register(&unittest_driver)) {
3356 unittest(0, "could not register unittest driver\n");
3357 goto out;
3360 bus_np = of_find_node_by_path(bus_path);
3361 if (bus_np == NULL) {
3362 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3363 goto out;
3366 if (of_platform_default_populate(bus_np, NULL, NULL)) {
3367 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3368 goto out;
3371 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3372 unittest(0, "could not find unittest0 @ \"%s\"\n",
3373 unittest_path(100, PDEV_OVERLAY));
3374 goto out;
3377 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3378 unittest(0, "unittest1 @ \"%s\" should not exist\n",
3379 unittest_path(101, PDEV_OVERLAY));
3380 goto out;
3383 unittest(1, "basic infrastructure of overlays passed");
3385 /* tests in sequence */
3386 of_unittest_overlay_0();
3387 of_unittest_overlay_1();
3388 of_unittest_overlay_2();
3389 of_unittest_overlay_3();
3390 of_unittest_overlay_4();
3391 for (i = 0; i < 3; i++)
3392 of_unittest_overlay_5();
3393 of_unittest_overlay_6();
3394 of_unittest_overlay_8();
3396 of_unittest_overlay_10();
3397 of_unittest_overlay_11();
3399 #if IS_BUILTIN(CONFIG_I2C)
3400 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3401 goto out;
3403 of_unittest_overlay_i2c_12();
3404 of_unittest_overlay_i2c_13();
3405 of_unittest_overlay_i2c_14();
3406 of_unittest_overlay_i2c_15();
3408 of_unittest_overlay_i2c_cleanup();
3409 #endif
3411 of_unittest_overlay_gpio();
3413 of_unittest_remove_tracked_overlays();
3415 of_unittest_overlay_notify();
3417 out:
3418 of_node_put(bus_np);
3421 #else
3422 static inline void __init of_unittest_overlay(void) { }
3423 #endif
3425 static void __init of_unittest_lifecycle(void)
3427 #ifdef CONFIG_OF_DYNAMIC
3428 unsigned int refcount;
3429 int found_refcount_one = 0;
3430 int put_count = 0;
3431 struct device_node *np;
3432 struct device_node *prev_sibling, *next_sibling;
3433 const char *refcount_path = "/testcase-data/refcount-node";
3434 const char *refcount_parent_path = "/testcase-data";
3437 * Node lifecycle tests, non-dynamic node:
3439 * - Decrementing refcount to zero via of_node_put() should cause the
3440 * attempt to free the node memory by of_node_release() to fail
3441 * because the node is not a dynamic node.
3443 * - Decrementing refcount past zero should result in additional
3444 * errors reported.
3447 np = of_find_node_by_path(refcount_path);
3448 unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3449 if (np == NULL)
3450 goto out_skip_tests;
3452 while (!found_refcount_one) {
3454 if (put_count++ > 10) {
3455 unittest(0, "guardrail to avoid infinite loop\n");
3456 goto out_skip_tests;
3459 refcount = kref_read(&np->kobj.kref);
3460 if (refcount == 1)
3461 found_refcount_one = 1;
3462 else
3463 of_node_put(np);
3466 EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3469 * refcount is now one, decrementing to zero will result in a call to
3470 * of_node_release() to free the node's memory, which should result
3471 * in an error
3473 unittest(1, "/testcase-data/refcount-node is one");
3474 of_node_put(np);
3476 EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3480 * expect stack trace for subsequent of_node_put():
3481 * __refcount_sub_and_test() calls:
3482 * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3484 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3485 * the first three lines, and the last line.
3487 EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3488 EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3489 EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3490 EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3492 /* refcount is now zero, this should fail */
3493 unittest(1, "/testcase-data/refcount-node is zero");
3494 of_node_put(np);
3496 EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3497 EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3498 EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3499 EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3502 * Q. do we expect to get yet another warning?
3503 * A. no, the WARNING is from WARN_ONCE()
3505 EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3506 EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3507 EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3508 EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3510 unittest(1, "/testcase-data/refcount-node is zero, second time");
3511 of_node_put(np);
3513 EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3514 EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3515 EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3516 EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3519 * refcount of zero will trigger stack traces from any further
3520 * attempt to of_node_get() node "refcount-node". One example of
3521 * this is where of_unittest_check_node_linkage() will recursively
3522 * scan the tree, with 'for_each_child_of_node()' doing an
3523 * of_node_get() of the children of a node.
3525 * Prevent the stack trace by removing node "refcount-node" from
3526 * its parent's child list.
3528 * WARNING: EVIL, EVIL, EVIL:
3530 * Directly manipulate the child list of node /testcase-data to
3531 * remove child refcount-node. This is ignoring all proper methods
3532 * of removing a child and will leak a small amount of memory.
3535 np = of_find_node_by_path(refcount_parent_path);
3536 unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3537 unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3538 if (np == NULL)
3539 return;
3541 prev_sibling = np->child;
3542 next_sibling = prev_sibling->sibling;
3543 if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3544 np->child = next_sibling;
3545 next_sibling = next_sibling->sibling;
3547 while (next_sibling) {
3548 if (!strcmp(next_sibling->full_name, "refcount-node"))
3549 prev_sibling->sibling = next_sibling->sibling;
3550 prev_sibling = next_sibling;
3551 next_sibling = next_sibling->sibling;
3553 of_node_put(np);
3555 return;
3557 out_skip_tests:
3558 #endif
3559 unittest(0, "One or more lifecycle tests skipped\n");
3562 #ifdef CONFIG_OF_OVERLAY
3565 * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3566 * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
3569 #define OVERLAY_INFO_EXTERN(overlay_name) \
3570 extern uint8_t __dtbo_##overlay_name##_begin[]; \
3571 extern uint8_t __dtbo_##overlay_name##_end[]
3573 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3574 { .dtbo_begin = __dtbo_##overlay_name##_begin, \
3575 .dtbo_end = __dtbo_##overlay_name##_end, \
3576 .expected_result = expected, \
3577 .expected_result_remove = expected_remove, \
3578 .name = #overlay_name, \
3581 struct overlay_info {
3582 uint8_t *dtbo_begin;
3583 uint8_t *dtbo_end;
3584 int expected_result;
3585 int expected_result_remove; /* if apply failed */
3586 int ovcs_id;
3587 char *name;
3590 OVERLAY_INFO_EXTERN(overlay_base);
3591 OVERLAY_INFO_EXTERN(overlay);
3592 OVERLAY_INFO_EXTERN(overlay_0);
3593 OVERLAY_INFO_EXTERN(overlay_1);
3594 OVERLAY_INFO_EXTERN(overlay_2);
3595 OVERLAY_INFO_EXTERN(overlay_3);
3596 OVERLAY_INFO_EXTERN(overlay_4);
3597 OVERLAY_INFO_EXTERN(overlay_5);
3598 OVERLAY_INFO_EXTERN(overlay_6);
3599 OVERLAY_INFO_EXTERN(overlay_7);
3600 OVERLAY_INFO_EXTERN(overlay_8);
3601 OVERLAY_INFO_EXTERN(overlay_9);
3602 OVERLAY_INFO_EXTERN(overlay_10);
3603 OVERLAY_INFO_EXTERN(overlay_11);
3604 OVERLAY_INFO_EXTERN(overlay_12);
3605 OVERLAY_INFO_EXTERN(overlay_13);
3606 OVERLAY_INFO_EXTERN(overlay_15);
3607 OVERLAY_INFO_EXTERN(overlay_16);
3608 OVERLAY_INFO_EXTERN(overlay_17);
3609 OVERLAY_INFO_EXTERN(overlay_18);
3610 OVERLAY_INFO_EXTERN(overlay_19);
3611 OVERLAY_INFO_EXTERN(overlay_20);
3612 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3613 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3614 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3615 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3616 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3617 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3618 OVERLAY_INFO_EXTERN(overlay_pci_node);
3619 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3620 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3621 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3622 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3623 OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
3625 /* entries found by name */
3626 static struct overlay_info overlays[] = {
3627 OVERLAY_INFO(overlay_base, -9999, 0),
3628 OVERLAY_INFO(overlay, 0, 0),
3629 OVERLAY_INFO(overlay_0, 0, 0),
3630 OVERLAY_INFO(overlay_1, 0, 0),
3631 OVERLAY_INFO(overlay_2, 0, 0),
3632 OVERLAY_INFO(overlay_3, 0, 0),
3633 OVERLAY_INFO(overlay_4, 0, 0),
3634 OVERLAY_INFO(overlay_5, 0, 0),
3635 OVERLAY_INFO(overlay_6, 0, 0),
3636 OVERLAY_INFO(overlay_7, 0, 0),
3637 OVERLAY_INFO(overlay_8, 0, 0),
3638 OVERLAY_INFO(overlay_9, 0, 0),
3639 OVERLAY_INFO(overlay_10, 0, 0),
3640 OVERLAY_INFO(overlay_11, 0, 0),
3641 OVERLAY_INFO(overlay_12, 0, 0),
3642 OVERLAY_INFO(overlay_13, 0, 0),
3643 OVERLAY_INFO(overlay_15, 0, 0),
3644 OVERLAY_INFO(overlay_16, -EBUSY, 0),
3645 OVERLAY_INFO(overlay_17, -EEXIST, 0),
3646 OVERLAY_INFO(overlay_18, 0, 0),
3647 OVERLAY_INFO(overlay_19, 0, 0),
3648 OVERLAY_INFO(overlay_20, 0, 0),
3649 OVERLAY_INFO(overlay_gpio_01, 0, 0),
3650 OVERLAY_INFO(overlay_gpio_02a, 0, 0),
3651 OVERLAY_INFO(overlay_gpio_02b, 0, 0),
3652 OVERLAY_INFO(overlay_gpio_03, 0, 0),
3653 OVERLAY_INFO(overlay_gpio_04a, 0, 0),
3654 OVERLAY_INFO(overlay_gpio_04b, 0, 0),
3655 OVERLAY_INFO(overlay_pci_node, 0, 0),
3656 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
3657 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
3658 OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
3659 OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
3660 OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
3661 /* end marker */
3665 static struct device_node *overlay_base_root;
3667 static void * __init dt_alloc_memory(u64 size, u64 align)
3669 void *ptr = memblock_alloc(size, align);
3671 if (!ptr)
3672 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
3673 __func__, size, align);
3675 return ptr;
3679 * Create base device tree for the overlay unittest.
3681 * This is called from very early boot code.
3683 * Do as much as possible the same way as done in __unflatten_device_tree
3684 * and other early boot steps for the normal FDT so that the overlay base
3685 * unflattened tree will have the same characteristics as the real tree
3686 * (such as having memory allocated by the early allocator). The goal
3687 * is to test "the real thing" as much as possible, and test "test setup
3688 * code" as little as possible.
3690 * Have to stop before resolving phandles, because that uses kmalloc.
3692 void __init unittest_unflatten_overlay_base(void)
3694 struct overlay_info *info;
3695 u32 data_size;
3696 void *new_fdt;
3697 u32 size;
3698 int found = 0;
3699 const char *overlay_name = "overlay_base";
3701 for (info = overlays; info && info->name; info++) {
3702 if (!strcmp(overlay_name, info->name)) {
3703 found = 1;
3704 break;
3707 if (!found) {
3708 pr_err("no overlay data for %s\n", overlay_name);
3709 return;
3712 info = &overlays[0];
3714 if (info->expected_result != -9999) {
3715 pr_err("No dtb 'overlay_base' to attach\n");
3716 return;
3719 data_size = info->dtbo_end - info->dtbo_begin;
3720 if (!data_size) {
3721 pr_err("No dtb 'overlay_base' to attach\n");
3722 return;
3725 size = fdt_totalsize(info->dtbo_begin);
3726 if (size != data_size) {
3727 pr_err("dtb 'overlay_base' header totalsize != actual size");
3728 return;
3731 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3732 if (!new_fdt) {
3733 pr_err("alloc for dtb 'overlay_base' failed");
3734 return;
3737 memcpy(new_fdt, info->dtbo_begin, size);
3739 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3740 dt_alloc_memory, true);
3744 * The purpose of of_unittest_overlay_data_add is to add an
3745 * overlay in the normal fashion. This is a test of the whole
3746 * picture, instead of testing individual elements.
3748 * A secondary purpose is to be able to verify that the contents of
3749 * /proc/device-tree/ contains the updated structure and values from
3750 * the overlay. That must be verified separately in user space.
3752 * Return 0 on unexpected error.
3754 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3756 struct overlay_info *info;
3757 int passed = 1;
3758 int found = 0;
3759 int ret, ret2;
3760 u32 size;
3762 for (info = overlays; info && info->name; info++) {
3763 if (!strcmp(overlay_name, info->name)) {
3764 found = 1;
3765 break;
3768 if (!found) {
3769 pr_err("no overlay data for %s\n", overlay_name);
3770 return 0;
3773 size = info->dtbo_end - info->dtbo_begin;
3774 if (!size)
3775 pr_err("no overlay data for %s\n", overlay_name);
3777 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
3778 NULL);
3779 if (ovcs_id)
3780 *ovcs_id = info->ovcs_id;
3781 if (ret < 0)
3782 goto out;
3784 pr_debug("%s applied\n", overlay_name);
3786 out:
3787 if (ret != info->expected_result) {
3788 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3789 info->expected_result, ret, overlay_name);
3790 passed = 0;
3793 if (ret < 0) {
3794 /* changeset may be partially applied */
3795 ret2 = of_overlay_remove(&info->ovcs_id);
3796 if (ret2 != info->expected_result_remove) {
3797 pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
3798 info->expected_result_remove, ret2,
3799 overlay_name);
3800 passed = 0;
3804 return passed;
3808 * The purpose of of_unittest_overlay_high_level is to add an overlay
3809 * in the normal fashion. This is a test of the whole picture,
3810 * instead of individual elements.
3812 * The first part of the function is _not_ normal overlay usage; it is
3813 * finishing splicing the base overlay device tree into the live tree.
3815 static __init void of_unittest_overlay_high_level(void)
3817 struct device_node *last_sibling;
3818 struct device_node *np;
3819 struct device_node *of_symbols;
3820 struct device_node *overlay_base_symbols;
3821 struct device_node **pprev;
3822 struct property *prop;
3823 int ret;
3825 if (!overlay_base_root) {
3826 unittest(0, "overlay_base_root not initialized\n");
3827 return;
3831 * Could not fixup phandles in unittest_unflatten_overlay_base()
3832 * because kmalloc() was not yet available.
3834 of_overlay_mutex_lock();
3835 of_resolve_phandles(overlay_base_root);
3836 of_overlay_mutex_unlock();
3840 * do not allow overlay_base to duplicate any node already in
3841 * tree, this greatly simplifies the code
3845 * remove overlay_base_root node "__local_fixups", after
3846 * being used by of_resolve_phandles()
3848 pprev = &overlay_base_root->child;
3849 for (np = overlay_base_root->child; np; np = np->sibling) {
3850 if (of_node_name_eq(np, "__local_fixups__")) {
3851 *pprev = np->sibling;
3852 break;
3854 pprev = &np->sibling;
3857 /* remove overlay_base_root node "__symbols__" if in live tree */
3858 of_symbols = of_get_child_by_name(of_root, "__symbols__");
3859 if (of_symbols) {
3860 /* will have to graft properties from node into live tree */
3861 pprev = &overlay_base_root->child;
3862 for (np = overlay_base_root->child; np; np = np->sibling) {
3863 if (of_node_name_eq(np, "__symbols__")) {
3864 overlay_base_symbols = np;
3865 *pprev = np->sibling;
3866 break;
3868 pprev = &np->sibling;
3872 for_each_child_of_node(overlay_base_root, np) {
3873 struct device_node *base_child;
3874 for_each_child_of_node(of_root, base_child) {
3875 if (!strcmp(np->full_name, base_child->full_name)) {
3876 unittest(0, "illegal node name in overlay_base %pOFn",
3877 np);
3878 of_node_put(np);
3879 of_node_put(base_child);
3880 return;
3886 * overlay 'overlay_base' is not allowed to have root
3887 * properties, so only need to splice nodes into main device tree.
3889 * root node of *overlay_base_root will not be freed, it is lost
3890 * memory.
3893 for (np = overlay_base_root->child; np; np = np->sibling)
3894 np->parent = of_root;
3896 mutex_lock(&of_mutex);
3898 for (last_sibling = np = of_root->child; np; np = np->sibling)
3899 last_sibling = np;
3901 if (last_sibling)
3902 last_sibling->sibling = overlay_base_root->child;
3903 else
3904 of_root->child = overlay_base_root->child;
3906 for_each_of_allnodes_from(overlay_base_root, np)
3907 __of_attach_node_sysfs(np);
3909 if (of_symbols) {
3910 struct property *new_prop;
3911 for_each_property_of_node(overlay_base_symbols, prop) {
3913 new_prop = __of_prop_dup(prop, GFP_KERNEL);
3914 if (!new_prop) {
3915 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3916 prop->name);
3917 goto err_unlock;
3919 if (__of_add_property(of_symbols, new_prop)) {
3920 __of_prop_free(new_prop);
3921 /* "name" auto-generated by unflatten */
3922 if (!strcmp(prop->name, "name"))
3923 continue;
3924 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3925 prop->name);
3926 goto err_unlock;
3928 if (__of_add_property_sysfs(of_symbols, new_prop)) {
3929 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3930 prop->name);
3931 goto err_unlock;
3936 mutex_unlock(&of_mutex);
3939 /* now do the normal overlay usage test */
3941 /* --- overlay --- */
3943 EXPECT_BEGIN(KERN_ERR,
3944 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3945 EXPECT_BEGIN(KERN_ERR,
3946 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3947 EXPECT_BEGIN(KERN_ERR,
3948 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3949 EXPECT_BEGIN(KERN_ERR,
3950 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3951 EXPECT_BEGIN(KERN_ERR,
3952 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3953 EXPECT_BEGIN(KERN_ERR,
3954 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3955 EXPECT_BEGIN(KERN_ERR,
3956 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3957 EXPECT_BEGIN(KERN_ERR,
3958 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3959 EXPECT_BEGIN(KERN_ERR,
3960 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3961 EXPECT_BEGIN(KERN_ERR,
3962 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3963 EXPECT_BEGIN(KERN_ERR,
3964 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3966 ret = overlay_data_apply("overlay", NULL);
3968 EXPECT_END(KERN_ERR,
3969 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3970 EXPECT_END(KERN_ERR,
3971 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3972 EXPECT_END(KERN_ERR,
3973 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3974 EXPECT_END(KERN_ERR,
3975 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3976 EXPECT_END(KERN_ERR,
3977 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3978 EXPECT_END(KERN_ERR,
3979 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3980 EXPECT_END(KERN_ERR,
3981 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3982 EXPECT_END(KERN_ERR,
3983 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3984 EXPECT_END(KERN_ERR,
3985 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3986 EXPECT_END(KERN_ERR,
3987 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3988 EXPECT_END(KERN_ERR,
3989 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3991 unittest(ret, "Adding overlay 'overlay' failed\n");
3993 /* --- overlay_bad_add_dup_node --- */
3995 EXPECT_BEGIN(KERN_ERR,
3996 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3997 EXPECT_BEGIN(KERN_ERR,
3998 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3999 EXPECT_BEGIN(KERN_ERR,
4000 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
4001 EXPECT_BEGIN(KERN_ERR,
4002 "OF: Error reverting changeset (-19)");
4004 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
4005 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
4007 EXPECT_END(KERN_ERR,
4008 "OF: Error reverting changeset (-19)");
4009 EXPECT_END(KERN_ERR,
4010 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
4011 EXPECT_END(KERN_ERR,
4012 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
4013 EXPECT_END(KERN_ERR,
4014 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
4016 /* --- overlay_bad_add_dup_prop --- */
4018 EXPECT_BEGIN(KERN_ERR,
4019 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
4020 EXPECT_BEGIN(KERN_ERR,
4021 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
4022 EXPECT_BEGIN(KERN_ERR,
4023 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
4024 EXPECT_BEGIN(KERN_ERR,
4025 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
4026 EXPECT_BEGIN(KERN_ERR,
4027 "OF: Error reverting changeset (-19)");
4029 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
4030 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
4032 EXPECT_END(KERN_ERR,
4033 "OF: Error reverting changeset (-19)");
4034 EXPECT_END(KERN_ERR,
4035 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
4036 EXPECT_END(KERN_ERR,
4037 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
4038 EXPECT_END(KERN_ERR,
4039 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
4040 EXPECT_END(KERN_ERR,
4041 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
4043 /* --- overlay_bad_phandle --- */
4045 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
4046 "Adding overlay 'overlay_bad_phandle' failed\n");
4048 /* --- overlay_bad_symbol --- */
4050 EXPECT_BEGIN(KERN_ERR,
4051 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
4052 EXPECT_BEGIN(KERN_ERR,
4053 "OF: Error reverting changeset (-19)");
4055 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
4056 "Adding overlay 'overlay_bad_symbol' failed\n");
4058 EXPECT_END(KERN_ERR,
4059 "OF: Error reverting changeset (-19)");
4060 EXPECT_END(KERN_ERR,
4061 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
4063 /* --- overlay_bad_unresolved --- */
4065 EXPECT_BEGIN(KERN_ERR,
4066 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
4067 EXPECT_BEGIN(KERN_ERR,
4068 "OF: resolver: overlay phandle fixup failed: -22");
4070 unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
4071 "Adding overlay 'overlay_bad_unresolved' failed\n");
4073 EXPECT_END(KERN_ERR,
4074 "OF: resolver: overlay phandle fixup failed: -22");
4075 EXPECT_END(KERN_ERR,
4076 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
4078 return;
4080 err_unlock:
4081 mutex_unlock(&of_mutex);
4084 static int of_unittest_pci_dev_num;
4085 static int of_unittest_pci_child_num;
4088 * PCI device tree node test driver
4090 static const struct pci_device_id testdrv_pci_ids[] = {
4091 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
4092 { 0, }
4095 static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4097 struct overlay_info *info;
4098 struct device_node *dn;
4099 int ret, ovcs_id;
4100 u32 size;
4102 dn = pdev->dev.of_node;
4103 if (!dn) {
4104 dev_err(&pdev->dev, "does not find bus endpoint");
4105 return -EINVAL;
4108 for (info = overlays; info && info->name; info++) {
4109 if (!strcmp(info->name, "overlay_pci_node"))
4110 break;
4112 if (!info || !info->name) {
4113 dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
4114 return -ENODEV;
4117 size = info->dtbo_end - info->dtbo_begin;
4118 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
4119 of_node_put(dn);
4120 if (ret)
4121 return ret;
4123 of_platform_default_populate(dn, NULL, &pdev->dev);
4124 pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
4126 return 0;
4129 static void testdrv_remove(struct pci_dev *pdev)
4131 int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
4133 of_platform_depopulate(&pdev->dev);
4134 of_overlay_remove(&ovcs_id);
4137 static struct pci_driver testdrv_driver = {
4138 .name = "pci_dt_testdrv",
4139 .id_table = testdrv_pci_ids,
4140 .probe = testdrv_probe,
4141 .remove = testdrv_remove,
4144 static int unittest_pci_probe(struct platform_device *pdev)
4146 struct resource *res;
4147 struct device *dev;
4148 u64 exp_addr;
4150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4151 if (!res)
4152 return -ENODEV;
4154 dev = &pdev->dev;
4155 while (dev && !dev_is_pci(dev))
4156 dev = dev->parent;
4157 if (!dev) {
4158 pr_err("unable to find parent device\n");
4159 return -ENODEV;
4162 exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
4163 unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
4164 (u64)res->start, exp_addr);
4166 of_unittest_pci_child_num++;
4168 return 0;
4171 static const struct of_device_id unittest_pci_of_match[] = {
4172 { .compatible = "unittest-pci" },
4176 static struct platform_driver unittest_pci_driver = {
4177 .probe = unittest_pci_probe,
4178 .driver = {
4179 .name = "unittest-pci",
4180 .of_match_table = unittest_pci_of_match,
4184 static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
4186 struct device_node *pnp, *np = NULL;
4187 struct device *child_dev;
4188 char *path = NULL;
4189 const __be32 *reg;
4190 int rc = 0;
4192 pnp = pdev->dev.of_node;
4193 unittest(pnp, "Failed creating PCI dt node\n");
4194 if (!pnp)
4195 return -ENODEV;
4197 if (add) {
4198 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
4199 np = of_find_node_by_path(path);
4200 unittest(np, "Failed to get unittest-pci node under PCI node\n");
4201 if (!np) {
4202 rc = -ENODEV;
4203 goto failed;
4206 reg = of_get_property(np, "reg", NULL);
4207 unittest(reg, "Failed to get reg property\n");
4208 if (!reg)
4209 rc = -ENODEV;
4210 } else {
4211 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
4212 np = of_find_node_by_path(path);
4213 unittest(!np, "Child device tree node is not removed\n");
4214 child_dev = device_find_any_child(&pdev->dev);
4215 unittest(!child_dev, "Child device is not removed\n");
4218 failed:
4219 kfree(path);
4220 if (np)
4221 of_node_put(np);
4223 return rc;
4226 static void __init of_unittest_pci_node(void)
4228 struct pci_dev *pdev = NULL;
4229 int rc;
4231 if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
4232 return;
4234 rc = pci_register_driver(&testdrv_driver);
4235 unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
4236 if (rc)
4237 return;
4239 rc = platform_driver_register(&unittest_pci_driver);
4240 if (unittest(!rc, "Failed to register unittest pci driver\n")) {
4241 pci_unregister_driver(&testdrv_driver);
4242 return;
4245 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
4246 of_unittest_pci_node_verify(pdev, true);
4247 of_unittest_pci_dev_num++;
4249 if (pdev)
4250 pci_dev_put(pdev);
4252 unittest(of_unittest_pci_dev_num,
4253 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
4254 unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
4255 "Child device number %d is not expected %d", of_unittest_pci_child_num,
4256 of_unittest_pci_dev_num);
4258 platform_driver_unregister(&unittest_pci_driver);
4259 pci_unregister_driver(&testdrv_driver);
4261 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
4262 of_unittest_pci_node_verify(pdev, false);
4263 if (pdev)
4264 pci_dev_put(pdev);
4266 #else
4268 static inline __init void of_unittest_overlay_high_level(void) {}
4269 static inline __init void of_unittest_pci_node(void) { }
4271 #endif
4273 static int __init of_unittest(void)
4275 struct device_node *np;
4276 int res;
4278 pr_info("start of unittest - you will see error messages\n");
4280 /* Taint the kernel so we know we've run tests. */
4281 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
4283 /* adding data for unittest */
4284 res = unittest_data_add();
4285 if (res)
4286 return res;
4287 if (!of_aliases)
4288 of_aliases = of_find_node_by_path("/aliases");
4290 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4291 if (!np) {
4292 pr_info("No testcase data in device tree; not running tests\n");
4293 return 0;
4295 of_node_put(np);
4297 of_unittest_check_tree_linkage();
4298 of_unittest_check_phandles();
4299 of_unittest_find_node_by_name();
4300 of_unittest_dynamic();
4301 of_unittest_parse_phandle_with_args();
4302 of_unittest_parse_phandle_with_args_map();
4303 of_unittest_printf();
4304 of_unittest_property_string();
4305 of_unittest_property_copy();
4306 of_unittest_changeset();
4307 of_unittest_changeset_prop();
4308 of_unittest_parse_interrupts();
4309 of_unittest_parse_interrupts_extended();
4310 of_unittest_dma_get_max_cpu_address();
4311 of_unittest_parse_dma_ranges();
4312 of_unittest_pci_dma_ranges();
4313 of_unittest_pci_empty_dma_ranges();
4314 of_unittest_bus_ranges();
4315 of_unittest_bus_3cell_ranges();
4316 of_unittest_reg();
4317 of_unittest_translate_addr();
4318 of_unittest_match_node();
4319 of_unittest_platform_populate();
4320 of_unittest_overlay();
4321 of_unittest_lifecycle();
4322 of_unittest_pci_node();
4324 /* Double check linkage after removing testcase data */
4325 of_unittest_check_tree_linkage();
4327 of_unittest_overlay_high_level();
4329 pr_info("end of unittest - %i passed, %i failed\n",
4330 unittest_results.passed, unittest_results.failed);
4332 return 0;
4334 late_initcall(of_unittest);