blob: f1aa09215d4b30d68a5fdfdff4b9187019524a94 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Self tests for device tree subsystem
4 */
5
6#define pr_fmt(fmt) "### dt-test ### " fmt
7
8#include <linux/memblock.h>
9#include <linux/clk.h>
10#include <linux/err.h>
11#include <linux/errno.h>
12#include <linux/hashtable.h>
13#include <linux/libfdt.h>
14#include <linux/of.h>
15#include <linux/of_fdt.h>
16#include <linux/of_irq.h>
17#include <linux/of_platform.h>
18#include <linux/list.h>
19#include <linux/mutex.h>
20#include <linux/slab.h>
21#include <linux/device.h>
22#include <linux/platform_device.h>
23
24#include <linux/i2c.h>
25#include <linux/i2c-mux.h>
26#include <linux/gpio/driver.h>
27
28#include <linux/bitops.h>
29
30#include "of_private.h"
31
32static struct unittest_results {
33 int passed;
34 int failed;
35} unittest_results;
36
37#define unittest(result, fmt, ...) ({ \
38 bool failed = !(result); \
39 if (failed) { \
40 unittest_results.failed++; \
41 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
42 } else { \
43 unittest_results.passed++; \
44 pr_debug("pass %s():%i\n", __func__, __LINE__); \
45 } \
46 failed; \
47})
48
49#ifdef CONFIG_OF_KOBJ
50#define OF_KREF_READ(NODE) kref_read(&(NODE)->kobj.kref)
51#else
52#define OF_KREF_READ(NODE) 1
53#endif
54
55/*
56 * Expected message may have a message level other than KERN_INFO.
57 * Print the expected message only if the current loglevel will allow
58 * the actual message to print.
59 */
60#define EXPECT_BEGIN(level, fmt, ...) \
61 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
62
63#define EXPECT_END(level, fmt, ...) \
64 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
65
66struct unittest_gpio_dev {
67 struct gpio_chip chip;
68};
69
70static int unittest_gpio_chip_request_count;
71static int unittest_gpio_probe_count;
72static int unittest_gpio_probe_pass_count;
73
74static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
75{
76 unittest_gpio_chip_request_count++;
77
78 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
79 unittest_gpio_chip_request_count);
80 return 0;
81}
82
83static int unittest_gpio_probe(struct platform_device *pdev)
84{
85 struct unittest_gpio_dev *devptr;
86 int ret;
87
88 unittest_gpio_probe_count++;
89
90 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
91 if (!devptr)
92 return -ENOMEM;
93
94 platform_set_drvdata(pdev, devptr);
95
96 devptr->chip.of_node = pdev->dev.of_node;
97 devptr->chip.label = "of-unittest-gpio";
98 devptr->chip.base = -1; /* dynamic allocation */
99 devptr->chip.ngpio = 5;
100 devptr->chip.request = unittest_gpio_chip_request;
101
102 ret = gpiochip_add_data(&devptr->chip, NULL);
103
104 unittest(!ret,
105 "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr->chip.of_node, ret);
106
107 if (!ret)
108 unittest_gpio_probe_pass_count++;
109 return ret;
110}
111
112static int unittest_gpio_remove(struct platform_device *pdev)
113{
114 struct unittest_gpio_dev *gdev = platform_get_drvdata(pdev);
115 struct device *dev = &pdev->dev;
116 struct device_node *np = pdev->dev.of_node;
117
118 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
119
120 if (!gdev)
121 return -EINVAL;
122
123 if (gdev->chip.base != -1)
124 gpiochip_remove(&gdev->chip);
125
126 platform_set_drvdata(pdev, NULL);
127 kfree(gdev);
128
129 return 0;
130}
131
132static const struct of_device_id unittest_gpio_id[] = {
133 { .compatible = "unittest-gpio", },
134 {}
135};
136
137static struct platform_driver unittest_gpio_driver = {
138 .probe = unittest_gpio_probe,
139 .remove = unittest_gpio_remove,
140 .driver = {
141 .name = "unittest-gpio",
142 .of_match_table = of_match_ptr(unittest_gpio_id),
143 },
144};
145
146static void __init of_unittest_find_node_by_name(void)
147{
148 struct device_node *np;
149 const char *options, *name;
150
151 np = of_find_node_by_path("/testcase-data");
152 name = kasprintf(GFP_KERNEL, "%pOF", np);
153 unittest(np && name && !strcmp("/testcase-data", name),
154 "find /testcase-data failed\n");
155 of_node_put(np);
156 kfree(name);
157
158 /* Test if trailing '/' works */
159 np = of_find_node_by_path("/testcase-data/");
160 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
161
162 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
163 name = kasprintf(GFP_KERNEL, "%pOF", np);
164 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
165 "find /testcase-data/phandle-tests/consumer-a failed\n");
166 of_node_put(np);
167 kfree(name);
168
169 np = of_find_node_by_path("testcase-alias");
170 name = kasprintf(GFP_KERNEL, "%pOF", np);
171 unittest(np && name && !strcmp("/testcase-data", name),
172 "find testcase-alias failed\n");
173 of_node_put(np);
174 kfree(name);
175
176 /* Test if trailing '/' works on aliases */
177 np = of_find_node_by_path("testcase-alias/");
178 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
179
180 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
181 name = kasprintf(GFP_KERNEL, "%pOF", np);
182 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
183 "find testcase-alias/phandle-tests/consumer-a failed\n");
184 of_node_put(np);
185 kfree(name);
186
187 np = of_find_node_by_path("/testcase-data/missing-path");
188 unittest(!np, "non-existent path returned node %pOF\n", np);
189 of_node_put(np);
190
191 np = of_find_node_by_path("missing-alias");
192 unittest(!np, "non-existent alias returned node %pOF\n", np);
193 of_node_put(np);
194
195 np = of_find_node_by_path("testcase-alias/missing-path");
196 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
197 of_node_put(np);
198
199 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
200 unittest(np && !strcmp("testoption", options),
201 "option path test failed\n");
202 of_node_put(np);
203
204 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
205 unittest(np && !strcmp("test/option", options),
206 "option path test, subcase #1 failed\n");
207 of_node_put(np);
208
209 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
210 unittest(np && !strcmp("test/option", options),
211 "option path test, subcase #2 failed\n");
212 of_node_put(np);
213
214 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
215 unittest(np, "NULL option path test failed\n");
216 of_node_put(np);
217
218 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
219 &options);
220 unittest(np && !strcmp("testaliasoption", options),
221 "option alias path test failed\n");
222 of_node_put(np);
223
224 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
225 &options);
226 unittest(np && !strcmp("test/alias/option", options),
227 "option alias path test, subcase #1 failed\n");
228 of_node_put(np);
229
230 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
231 unittest(np, "NULL option alias path test failed\n");
232 of_node_put(np);
233
234 options = "testoption";
235 np = of_find_node_opts_by_path("testcase-alias", &options);
236 unittest(np && !options, "option clearing test failed\n");
237 of_node_put(np);
238
239 options = "testoption";
240 np = of_find_node_opts_by_path("/", &options);
241 unittest(np && !options, "option clearing root node test failed\n");
242 of_node_put(np);
243}
244
245static void __init of_unittest_dynamic(void)
246{
247 struct device_node *np;
248 struct property *prop;
249
250 np = of_find_node_by_path("/testcase-data");
251 if (!np) {
252 pr_err("missing testcase data\n");
253 return;
254 }
255
256 /* Array of 4 properties for the purpose of testing */
257 prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
258 if (!prop) {
259 unittest(0, "kzalloc() failed\n");
260 return;
261 }
262
263 /* Add a new property - should pass*/
264 prop->name = "new-property";
265 prop->value = "new-property-data";
266 prop->length = strlen(prop->value) + 1;
267 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
268
269 /* Try to add an existing property - should fail */
270 prop++;
271 prop->name = "new-property";
272 prop->value = "new-property-data-should-fail";
273 prop->length = strlen(prop->value) + 1;
274 unittest(of_add_property(np, prop) != 0,
275 "Adding an existing property should have failed\n");
276
277 /* Try to modify an existing property - should pass */
278 prop->value = "modify-property-data-should-pass";
279 prop->length = strlen(prop->value) + 1;
280 unittest(of_update_property(np, prop) == 0,
281 "Updating an existing property should have passed\n");
282
283 /* Try to modify non-existent property - should pass*/
284 prop++;
285 prop->name = "modify-property";
286 prop->value = "modify-missing-property-data-should-pass";
287 prop->length = strlen(prop->value) + 1;
288 unittest(of_update_property(np, prop) == 0,
289 "Updating a missing property should have passed\n");
290
291 /* Remove property - should pass */
292 unittest(of_remove_property(np, prop) == 0,
293 "Removing a property should have passed\n");
294
295 /* Adding very large property - should pass */
296 prop++;
297 prop->name = "large-property-PAGE_SIZEx8";
298 prop->length = PAGE_SIZE * 8;
299 prop->value = kzalloc(prop->length, GFP_KERNEL);
300 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
301 if (prop->value)
302 unittest(of_add_property(np, prop) == 0,
303 "Adding a large property should have passed\n");
304}
305
306static int __init of_unittest_check_node_linkage(struct device_node *np)
307{
308 struct device_node *child;
309 int count = 0, rc;
310
311 for_each_child_of_node(np, child) {
312 if (child->parent != np) {
313 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
314 child, np);
315 rc = -EINVAL;
316 goto put_child;
317 }
318
319 rc = of_unittest_check_node_linkage(child);
320 if (rc < 0)
321 goto put_child;
322 count += rc;
323 }
324
325 return count + 1;
326put_child:
327 of_node_put(child);
328 return rc;
329}
330
331static void __init of_unittest_check_tree_linkage(void)
332{
333 struct device_node *np;
334 int allnode_count = 0, child_count;
335
336 if (!of_root)
337 return;
338
339 for_each_of_allnodes(np)
340 allnode_count++;
341 child_count = of_unittest_check_node_linkage(of_root);
342
343 unittest(child_count > 0, "Device node data structure is corrupted\n");
344 unittest(child_count == allnode_count,
345 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
346 allnode_count, child_count);
347 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
348}
349
350static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
351 const char *expected)
352{
353 unsigned char *buf;
354 int buf_size;
355 int size, i;
356
357 buf_size = strlen(expected) + 10;
358 buf = kmalloc(buf_size, GFP_KERNEL);
359 if (!buf)
360 return;
361
362 /* Baseline; check conversion with a large size limit */
363 memset(buf, 0xff, buf_size);
364 size = snprintf(buf, buf_size - 2, fmt, np);
365
366 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
367 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
368 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
369 fmt, expected, buf);
370
371 /* Make sure length limits work */
372 size++;
373 for (i = 0; i < 2; i++, size--) {
374 /* Clear the buffer, and make sure it works correctly still */
375 memset(buf, 0xff, buf_size);
376 snprintf(buf, size+1, fmt, np);
377 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
378 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
379 size, fmt, expected, buf);
380 }
381 kfree(buf);
382}
383
384static void __init of_unittest_printf(void)
385{
386 struct device_node *np;
387 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
388 char phandle_str[16] = "";
389
390 np = of_find_node_by_path(full_name);
391 if (!np) {
392 unittest(np, "testcase data missing\n");
393 return;
394 }
395
396 num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
397
398 of_unittest_printf_one(np, "%pOF", full_name);
399 of_unittest_printf_one(np, "%pOFf", full_name);
400 of_unittest_printf_one(np, "%pOFn", "dev");
401 of_unittest_printf_one(np, "%2pOFn", "dev");
402 of_unittest_printf_one(np, "%5pOFn", " dev");
403 of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
404 of_unittest_printf_one(np, "%pOFp", phandle_str);
405 of_unittest_printf_one(np, "%pOFP", "dev@100");
406 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
407 of_unittest_printf_one(np, "%10pOFP", " dev@100");
408 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
409 of_unittest_printf_one(of_root, "%pOFP", "/");
410 of_unittest_printf_one(np, "%pOFF", "----");
411 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
412 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
413 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
414 of_unittest_printf_one(np, "%pOFC",
415 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
416}
417
418struct node_hash {
419 struct hlist_node node;
420 struct device_node *np;
421};
422
423static DEFINE_HASHTABLE(phandle_ht, 8);
424static void __init of_unittest_check_phandles(void)
425{
426 struct device_node *np;
427 struct node_hash *nh;
428 struct hlist_node *tmp;
429 int i, dup_count = 0, phandle_count = 0;
430
431 for_each_of_allnodes(np) {
432 if (!np->phandle)
433 continue;
434
435 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
436 if (nh->np->phandle == np->phandle) {
437 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
438 np->phandle, nh->np, np);
439 dup_count++;
440 break;
441 }
442 }
443
444 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
445 if (!nh)
446 return;
447
448 nh->np = np;
449 hash_add(phandle_ht, &nh->node, np->phandle);
450 phandle_count++;
451 }
452 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
453 dup_count, phandle_count);
454
455 /* Clean up */
456 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
457 hash_del(&nh->node);
458 kfree(nh);
459 }
460}
461
462static void __init of_unittest_parse_phandle_with_args(void)
463{
464 struct device_node *np;
465 struct of_phandle_args args;
466 int i, rc;
467
468 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
469 if (!np) {
470 pr_err("missing testcase data\n");
471 return;
472 }
473
474 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
475 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
476
477 for (i = 0; i < 8; i++) {
478 bool passed = true;
479
480 memset(&args, 0, sizeof(args));
481 rc = of_parse_phandle_with_args(np, "phandle-list",
482 "#phandle-cells", i, &args);
483
484 /* Test the values from tests-phandle.dtsi */
485 switch (i) {
486 case 0:
487 passed &= !rc;
488 passed &= (args.args_count == 1);
489 passed &= (args.args[0] == (i + 1));
490 break;
491 case 1:
492 passed &= !rc;
493 passed &= (args.args_count == 2);
494 passed &= (args.args[0] == (i + 1));
495 passed &= (args.args[1] == 0);
496 break;
497 case 2:
498 passed &= (rc == -ENOENT);
499 break;
500 case 3:
501 passed &= !rc;
502 passed &= (args.args_count == 3);
503 passed &= (args.args[0] == (i + 1));
504 passed &= (args.args[1] == 4);
505 passed &= (args.args[2] == 3);
506 break;
507 case 4:
508 passed &= !rc;
509 passed &= (args.args_count == 2);
510 passed &= (args.args[0] == (i + 1));
511 passed &= (args.args[1] == 100);
512 break;
513 case 5:
514 passed &= !rc;
515 passed &= (args.args_count == 0);
516 break;
517 case 6:
518 passed &= !rc;
519 passed &= (args.args_count == 1);
520 passed &= (args.args[0] == (i + 1));
521 break;
522 case 7:
523 passed &= (rc == -ENOENT);
524 break;
525 default:
526 passed = false;
527 }
528
529 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
530 i, args.np, rc);
531
532 if (rc == 0)
533 of_node_put(args.np);
534 }
535
536 /* Check for missing list property */
537 memset(&args, 0, sizeof(args));
538 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
539 "#phandle-cells", 0, &args);
540 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
541 rc = of_count_phandle_with_args(np, "phandle-list-missing",
542 "#phandle-cells");
543 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
544
545 /* Check for missing cells property */
546 memset(&args, 0, sizeof(args));
547 rc = of_parse_phandle_with_args(np, "phandle-list",
548 "#phandle-cells-missing", 0, &args);
549 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
550 rc = of_count_phandle_with_args(np, "phandle-list",
551 "#phandle-cells-missing");
552 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
553
554 /* Check for bad phandle in list */
555 memset(&args, 0, sizeof(args));
556 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
557 "#phandle-cells", 0, &args);
558 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
559 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
560 "#phandle-cells");
561 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
562
563 /* Check for incorrectly formed argument list */
564 memset(&args, 0, sizeof(args));
565 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
566 "#phandle-cells", 1, &args);
567 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
568 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
569 "#phandle-cells");
570 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
571}
572
573static void __init of_unittest_parse_phandle_with_args_map(void)
574{
575 struct device_node *np, *p[6] = {};
576 struct of_phandle_args args;
577 unsigned int prefs[6];
578 int i, rc;
579
580 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
581 if (!np) {
582 pr_err("missing testcase data\n");
583 return;
584 }
585
586 p[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
587 p[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
588 p[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
589 p[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
590 p[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
591 p[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
592 for (i = 0; i < ARRAY_SIZE(p); ++i) {
593 if (!p[i]) {
594 pr_err("missing testcase data\n");
595 return;
596 }
597 prefs[i] = OF_KREF_READ(p[i]);
598 }
599
600 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
601 unittest(rc == 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc);
602
603 for (i = 0; i < 9; i++) {
604 bool passed = true;
605
606 memset(&args, 0, sizeof(args));
607 rc = of_parse_phandle_with_args_map(np, "phandle-list",
608 "phandle", i, &args);
609
610 /* Test the values from tests-phandle.dtsi */
611 switch (i) {
612 case 0:
613 passed &= !rc;
614 passed &= (args.np == p[1]);
615 passed &= (args.args_count == 1);
616 passed &= (args.args[0] == 1);
617 break;
618 case 1:
619 passed &= !rc;
620 passed &= (args.np == p[3]);
621 passed &= (args.args_count == 3);
622 passed &= (args.args[0] == 2);
623 passed &= (args.args[1] == 5);
624 passed &= (args.args[2] == 3);
625 break;
626 case 2:
627 passed &= (rc == -ENOENT);
628 break;
629 case 3:
630 passed &= !rc;
631 passed &= (args.np == p[0]);
632 passed &= (args.args_count == 0);
633 break;
634 case 4:
635 passed &= !rc;
636 passed &= (args.np == p[1]);
637 passed &= (args.args_count == 1);
638 passed &= (args.args[0] == 3);
639 break;
640 case 5:
641 passed &= !rc;
642 passed &= (args.np == p[0]);
643 passed &= (args.args_count == 0);
644 break;
645 case 6:
646 passed &= !rc;
647 passed &= (args.np == p[2]);
648 passed &= (args.args_count == 2);
649 passed &= (args.args[0] == 15);
650 passed &= (args.args[1] == 0x20);
651 break;
652 case 7:
653 passed &= !rc;
654 passed &= (args.np == p[3]);
655 passed &= (args.args_count == 3);
656 passed &= (args.args[0] == 2);
657 passed &= (args.args[1] == 5);
658 passed &= (args.args[2] == 3);
659 break;
660 case 8:
661 passed &= (rc == -ENOENT);
662 break;
663 default:
664 passed = false;
665 }
666
667 unittest(passed, "index %i - data error on node %s rc=%i\n",
668 i, args.np->full_name, rc);
669
670 if (rc == 0)
671 of_node_put(args.np);
672 }
673
674 /* Check for missing list property */
675 memset(&args, 0, sizeof(args));
676 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
677 "phandle", 0, &args);
678 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
679
680 /* Check for missing cells,map,mask property */
681 memset(&args, 0, sizeof(args));
682 rc = of_parse_phandle_with_args_map(np, "phandle-list",
683 "phandle-missing", 0, &args);
684 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
685
686 /* Check for bad phandle in list */
687 memset(&args, 0, sizeof(args));
688 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
689 "phandle", 0, &args);
690 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
691
692 /* Check for incorrectly formed argument list */
693 memset(&args, 0, sizeof(args));
694 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
695 "phandle", 1, &args);
696 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
697
698 for (i = 0; i < ARRAY_SIZE(p); ++i) {
699 unittest(prefs[i] == OF_KREF_READ(p[i]),
700 "provider%d: expected:%d got:%d\n",
701 i, prefs[i], OF_KREF_READ(p[i]));
702 of_node_put(p[i]);
703 }
704}
705
706static void __init of_unittest_property_string(void)
707{
708 const char *strings[4];
709 struct device_node *np;
710 int rc;
711
712 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
713 if (!np) {
714 pr_err("No testcase data in device tree\n");
715 return;
716 }
717
718 rc = of_property_match_string(np, "phandle-list-names", "first");
719 unittest(rc == 0, "first expected:0 got:%i\n", rc);
720 rc = of_property_match_string(np, "phandle-list-names", "second");
721 unittest(rc == 1, "second expected:1 got:%i\n", rc);
722 rc = of_property_match_string(np, "phandle-list-names", "third");
723 unittest(rc == 2, "third expected:2 got:%i\n", rc);
724 rc = of_property_match_string(np, "phandle-list-names", "fourth");
725 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
726 rc = of_property_match_string(np, "missing-property", "blah");
727 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
728 rc = of_property_match_string(np, "empty-property", "blah");
729 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
730 rc = of_property_match_string(np, "unterminated-string", "blah");
731 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
732
733 /* of_property_count_strings() tests */
734 rc = of_property_count_strings(np, "string-property");
735 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
736 rc = of_property_count_strings(np, "phandle-list-names");
737 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
738 rc = of_property_count_strings(np, "unterminated-string");
739 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
740 rc = of_property_count_strings(np, "unterminated-string-list");
741 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
742
743 /* of_property_read_string_index() tests */
744 rc = of_property_read_string_index(np, "string-property", 0, strings);
745 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
746 strings[0] = NULL;
747 rc = of_property_read_string_index(np, "string-property", 1, strings);
748 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
749 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
750 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
751 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
752 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
753 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
754 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
755 strings[0] = NULL;
756 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
757 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
758 strings[0] = NULL;
759 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
760 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
761 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
762 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
763 strings[0] = NULL;
764 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
765 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
766 strings[1] = NULL;
767
768 /* of_property_read_string_array() tests */
769 rc = of_property_read_string_array(np, "string-property", strings, 4);
770 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
771 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
772 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
773 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
774 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
775 /* -- An incorrectly formed string should cause a failure */
776 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
777 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
778 /* -- parsing the correctly formed strings should still work: */
779 strings[2] = NULL;
780 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
781 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
782 strings[1] = NULL;
783 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
784 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
785}
786
787#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
788 (p1)->value && (p2)->value && \
789 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
790 !strcmp((p1)->name, (p2)->name))
791static void __init of_unittest_property_copy(void)
792{
793#ifdef CONFIG_OF_DYNAMIC
794 struct property p1 = { .name = "p1", .length = 0, .value = "" };
795 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
796 struct property *new;
797
798 new = __of_prop_dup(&p1, GFP_KERNEL);
799 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
800 kfree(new->value);
801 kfree(new->name);
802 kfree(new);
803
804 new = __of_prop_dup(&p2, GFP_KERNEL);
805 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
806 kfree(new->value);
807 kfree(new->name);
808 kfree(new);
809#endif
810}
811
812static void __init of_unittest_changeset(void)
813{
814#ifdef CONFIG_OF_DYNAMIC
815 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
816 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
817 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
818 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
819 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
820 struct property *ppremove;
821 struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
822 struct of_changeset chgset;
823
824 n1 = __of_node_dup(NULL, "n1");
825 unittest(n1, "testcase setup failure\n");
826
827 n2 = __of_node_dup(NULL, "n2");
828 unittest(n2, "testcase setup failure\n");
829
830 n21 = __of_node_dup(NULL, "n21");
831 unittest(n21, "testcase setup failure %p\n", n21);
832
833 nchangeset = of_find_node_by_path("/testcase-data/changeset");
834 nremove = of_get_child_by_name(nchangeset, "node-remove");
835 unittest(nremove, "testcase setup failure\n");
836
837 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
838 unittest(ppadd, "testcase setup failure\n");
839
840 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
841 unittest(ppname_n1, "testcase setup failure\n");
842
843 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
844 unittest(ppname_n2, "testcase setup failure\n");
845
846 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
847 unittest(ppname_n21, "testcase setup failure\n");
848
849 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
850 unittest(ppupdate, "testcase setup failure\n");
851
852 parent = nchangeset;
853 n1->parent = parent;
854 n2->parent = parent;
855 n21->parent = n2;
856
857 ppremove = of_find_property(parent, "prop-remove", NULL);
858 unittest(ppremove, "failed to find removal prop");
859
860 of_changeset_init(&chgset);
861
862 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
863 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
864
865 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
866 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
867
868 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
869 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
870
871 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
872
873 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
874 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
875 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
876
877 unittest(!of_changeset_apply(&chgset), "apply failed\n");
878
879 of_node_put(nchangeset);
880
881 /* Make sure node names are constructed correctly */
882 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
883 "'%pOF' not added\n", n21);
884 of_node_put(np);
885
886 unittest(!of_changeset_revert(&chgset), "revert failed\n");
887
888 of_changeset_destroy(&chgset);
889
890 of_node_put(n1);
891 of_node_put(n2);
892 of_node_put(n21);
893#endif
894}
895
896static void __init of_unittest_parse_interrupts(void)
897{
898 struct device_node *np;
899 struct of_phandle_args args;
900 int i, rc;
901
902 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
903 return;
904
905 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
906 if (!np) {
907 pr_err("missing testcase data\n");
908 return;
909 }
910
911 for (i = 0; i < 4; i++) {
912 bool passed = true;
913
914 memset(&args, 0, sizeof(args));
915 rc = of_irq_parse_one(np, i, &args);
916
917 passed &= !rc;
918 passed &= (args.args_count == 1);
919 passed &= (args.args[0] == (i + 1));
920
921 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
922 i, args.np, rc);
923 }
924 of_node_put(np);
925
926 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
927 if (!np) {
928 pr_err("missing testcase data\n");
929 return;
930 }
931
932 for (i = 0; i < 4; i++) {
933 bool passed = true;
934
935 memset(&args, 0, sizeof(args));
936 rc = of_irq_parse_one(np, i, &args);
937
938 /* Test the values from tests-phandle.dtsi */
939 switch (i) {
940 case 0:
941 passed &= !rc;
942 passed &= (args.args_count == 1);
943 passed &= (args.args[0] == 9);
944 break;
945 case 1:
946 passed &= !rc;
947 passed &= (args.args_count == 3);
948 passed &= (args.args[0] == 10);
949 passed &= (args.args[1] == 11);
950 passed &= (args.args[2] == 12);
951 break;
952 case 2:
953 passed &= !rc;
954 passed &= (args.args_count == 2);
955 passed &= (args.args[0] == 13);
956 passed &= (args.args[1] == 14);
957 break;
958 case 3:
959 passed &= !rc;
960 passed &= (args.args_count == 2);
961 passed &= (args.args[0] == 15);
962 passed &= (args.args[1] == 16);
963 break;
964 default:
965 passed = false;
966 }
967 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
968 i, args.np, rc);
969 }
970 of_node_put(np);
971}
972
973static void __init of_unittest_parse_interrupts_extended(void)
974{
975 struct device_node *np;
976 struct of_phandle_args args;
977 int i, rc;
978
979 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
980 return;
981
982 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
983 if (!np) {
984 pr_err("missing testcase data\n");
985 return;
986 }
987
988 for (i = 0; i < 7; i++) {
989 bool passed = true;
990
991 memset(&args, 0, sizeof(args));
992 rc = of_irq_parse_one(np, i, &args);
993
994 /* Test the values from tests-phandle.dtsi */
995 switch (i) {
996 case 0:
997 passed &= !rc;
998 passed &= (args.args_count == 1);
999 passed &= (args.args[0] == 1);
1000 break;
1001 case 1:
1002 passed &= !rc;
1003 passed &= (args.args_count == 3);
1004 passed &= (args.args[0] == 2);
1005 passed &= (args.args[1] == 3);
1006 passed &= (args.args[2] == 4);
1007 break;
1008 case 2:
1009 passed &= !rc;
1010 passed &= (args.args_count == 2);
1011 passed &= (args.args[0] == 5);
1012 passed &= (args.args[1] == 6);
1013 break;
1014 case 3:
1015 passed &= !rc;
1016 passed &= (args.args_count == 1);
1017 passed &= (args.args[0] == 9);
1018 break;
1019 case 4:
1020 passed &= !rc;
1021 passed &= (args.args_count == 3);
1022 passed &= (args.args[0] == 10);
1023 passed &= (args.args[1] == 11);
1024 passed &= (args.args[2] == 12);
1025 break;
1026 case 5:
1027 passed &= !rc;
1028 passed &= (args.args_count == 2);
1029 passed &= (args.args[0] == 13);
1030 passed &= (args.args[1] == 14);
1031 break;
1032 case 6:
1033 passed &= !rc;
1034 passed &= (args.args_count == 1);
1035 passed &= (args.args[0] == 15);
1036 break;
1037 default:
1038 passed = false;
1039 }
1040
1041 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1042 i, args.np, rc);
1043 }
1044 of_node_put(np);
1045}
1046
1047static const struct of_device_id match_node_table[] = {
1048 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1049 { .data = "B", .type = "type1", }, /* followed by type alone */
1050
1051 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1052 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1053 { .data = "Cc", .name = "name2", .type = "type2", },
1054
1055 { .data = "E", .compatible = "compat3" },
1056 { .data = "G", .compatible = "compat2", },
1057 { .data = "H", .compatible = "compat2", .name = "name5", },
1058 { .data = "I", .compatible = "compat2", .type = "type1", },
1059 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1060 { .data = "K", .compatible = "compat2", .name = "name9", },
1061 {}
1062};
1063
1064static struct {
1065 const char *path;
1066 const char *data;
1067} match_node_tests[] = {
1068 { .path = "/testcase-data/match-node/name0", .data = "A", },
1069 { .path = "/testcase-data/match-node/name1", .data = "B", },
1070 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1071 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1072 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1073 { .path = "/testcase-data/match-node/name3", .data = "E", },
1074 { .path = "/testcase-data/match-node/name4", .data = "G", },
1075 { .path = "/testcase-data/match-node/name5", .data = "H", },
1076 { .path = "/testcase-data/match-node/name6", .data = "G", },
1077 { .path = "/testcase-data/match-node/name7", .data = "I", },
1078 { .path = "/testcase-data/match-node/name8", .data = "J", },
1079 { .path = "/testcase-data/match-node/name9", .data = "K", },
1080};
1081
1082static void __init of_unittest_match_node(void)
1083{
1084 struct device_node *np;
1085 const struct of_device_id *match;
1086 int i;
1087
1088 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1089 np = of_find_node_by_path(match_node_tests[i].path);
1090 if (!np) {
1091 unittest(0, "missing testcase node %s\n",
1092 match_node_tests[i].path);
1093 continue;
1094 }
1095
1096 match = of_match_node(match_node_table, np);
1097 if (!match) {
1098 unittest(0, "%s didn't match anything\n",
1099 match_node_tests[i].path);
1100 continue;
1101 }
1102
1103 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1104 unittest(0, "%s got wrong match. expected %s, got %s\n",
1105 match_node_tests[i].path, match_node_tests[i].data,
1106 (const char *)match->data);
1107 continue;
1108 }
1109 unittest(1, "passed");
1110 }
1111}
1112
1113static struct resource test_bus_res = {
1114 .start = 0xfffffff8,
1115 .end = 0xfffffff9,
1116 .flags = IORESOURCE_MEM,
1117};
1118static const struct platform_device_info test_bus_info = {
1119 .name = "unittest-bus",
1120};
1121static void __init of_unittest_platform_populate(void)
1122{
1123 int irq, rc;
1124 struct device_node *np, *child, *grandchild;
1125 struct platform_device *pdev, *test_bus;
1126 const struct of_device_id match[] = {
1127 { .compatible = "test-device", },
1128 {}
1129 };
1130
1131 np = of_find_node_by_path("/testcase-data");
1132 of_platform_default_populate(np, NULL, NULL);
1133
1134 /* Test that a missing irq domain returns -EPROBE_DEFER */
1135 np = of_find_node_by_path("/testcase-data/testcase-device1");
1136 pdev = of_find_device_by_node(np);
1137 unittest(pdev, "device 1 creation failed\n");
1138
1139 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1140 irq = platform_get_irq(pdev, 0);
1141 unittest(irq == -EPROBE_DEFER,
1142 "device deferred probe failed - %d\n", irq);
1143
1144 /* Test that a parsing failure does not return -EPROBE_DEFER */
1145 np = of_find_node_by_path("/testcase-data/testcase-device2");
1146 pdev = of_find_device_by_node(np);
1147 unittest(pdev, "device 2 creation failed\n");
1148 irq = platform_get_irq(pdev, 0);
1149 unittest(irq < 0 && irq != -EPROBE_DEFER,
1150 "device parsing error failed - %d\n", irq);
1151 }
1152
1153 np = of_find_node_by_path("/testcase-data/platform-tests");
1154 unittest(np, "No testcase data in device tree\n");
1155 if (!np)
1156 return;
1157
1158 test_bus = platform_device_register_full(&test_bus_info);
1159 rc = PTR_ERR_OR_ZERO(test_bus);
1160 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1161 if (rc) {
1162 of_node_put(np);
1163 return;
1164 }
1165 test_bus->dev.of_node = np;
1166
1167 /*
1168 * Add a dummy resource to the test bus node after it is
1169 * registered to catch problems with un-inserted resources. The
1170 * DT code doesn't insert the resources, and it has caused the
1171 * kernel to oops in the past. This makes sure the same bug
1172 * doesn't crop up again.
1173 */
1174 platform_device_add_resources(test_bus, &test_bus_res, 1);
1175
1176 of_platform_populate(np, match, NULL, &test_bus->dev);
1177 for_each_child_of_node(np, child) {
1178 for_each_child_of_node(child, grandchild) {
1179 pdev = of_find_device_by_node(grandchild);
1180 unittest(pdev,
1181 "Could not create device for node '%pOFn'\n",
1182 grandchild);
1183 of_dev_put(pdev);
1184 }
1185 }
1186
1187 of_platform_depopulate(&test_bus->dev);
1188 for_each_child_of_node(np, child) {
1189 for_each_child_of_node(child, grandchild)
1190 unittest(!of_find_device_by_node(grandchild),
1191 "device didn't get destroyed '%pOFn'\n",
1192 grandchild);
1193 }
1194
1195 platform_device_unregister(test_bus);
1196 of_node_put(np);
1197}
1198
1199/**
1200 * update_node_properties - adds the properties
1201 * of np into dup node (present in live tree) and
1202 * updates parent of children of np to dup.
1203 *
1204 * @np: node whose properties are being added to the live tree
1205 * @dup: node present in live tree to be updated
1206 */
1207static void update_node_properties(struct device_node *np,
1208 struct device_node *dup)
1209{
1210 struct property *prop;
1211 struct property *save_next;
1212 struct device_node *child;
1213 int ret;
1214
1215 for_each_child_of_node(np, child)
1216 child->parent = dup;
1217
1218 /*
1219 * "unittest internal error: unable to add testdata property"
1220 *
1221 * If this message reports a property in node '/__symbols__' then
1222 * the respective unittest overlay contains a label that has the
1223 * same name as a label in the live devicetree. The label will
1224 * be in the live devicetree only if the devicetree source was
1225 * compiled with the '-@' option. If you encounter this error,
1226 * please consider renaming __all__ of the labels in the unittest
1227 * overlay dts files with an odd prefix that is unlikely to be
1228 * used in a real devicetree.
1229 */
1230
1231 /*
1232 * open code for_each_property_of_node() because of_add_property()
1233 * sets prop->next to NULL
1234 */
1235 for (prop = np->properties; prop != NULL; prop = save_next) {
1236 save_next = prop->next;
1237 ret = of_add_property(dup, prop);
1238 if (ret) {
1239 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1240 continue;
1241 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1242 np, prop->name);
1243 }
1244 }
1245}
1246
1247/**
1248 * attach_node_and_children - attaches nodes
1249 * and its children to live tree.
1250 * CAUTION: misleading function name - if node @np already exists in
1251 * the live tree then children of @np are *not* attached to the live
1252 * tree. This works for the current test devicetree nodes because such
1253 * nodes do not have child nodes.
1254 *
1255 * @np: Node to attach to live tree
1256 */
1257static void attach_node_and_children(struct device_node *np)
1258{
1259 struct device_node *next, *dup, *child;
1260 unsigned long flags;
1261 const char *full_name;
1262
1263 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1264 if (!full_name)
1265 return;
1266
1267 if (!strcmp(full_name, "/__local_fixups__") ||
1268 !strcmp(full_name, "/__fixups__")) {
1269 kfree(full_name);
1270 return;
1271 }
1272
1273 dup = of_find_node_by_path(full_name);
1274 kfree(full_name);
1275 if (dup) {
1276 update_node_properties(np, dup);
1277 return;
1278 }
1279
1280 child = np->child;
1281 np->child = NULL;
1282
1283 mutex_lock(&of_mutex);
1284 raw_spin_lock_irqsave(&devtree_lock, flags);
1285 np->sibling = np->parent->child;
1286 np->parent->child = np;
1287 of_node_clear_flag(np, OF_DETACHED);
1288 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1289
1290 __of_attach_node_sysfs(np);
1291 mutex_unlock(&of_mutex);
1292
1293 while (child) {
1294 next = child->sibling;
1295 attach_node_and_children(child);
1296 child = next;
1297 }
1298}
1299
1300/**
1301 * unittest_data_add - Reads, copies data from
1302 * linked tree and attaches it to the live tree
1303 */
1304static int __init unittest_data_add(void)
1305{
1306 void *unittest_data;
1307 struct device_node *unittest_data_node, *np;
1308 /*
1309 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1310 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1311 */
1312 extern uint8_t __dtb_testcases_begin[];
1313 extern uint8_t __dtb_testcases_end[];
1314 const int size = __dtb_testcases_end - __dtb_testcases_begin;
1315 int rc;
1316
1317 if (!size) {
1318 pr_warn("%s: No testcase data to attach; not running tests\n",
1319 __func__);
1320 return -ENODATA;
1321 }
1322
1323 /* creating copy */
1324 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
1325 if (!unittest_data)
1326 return -ENOMEM;
1327
1328 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
1329 if (!unittest_data_node) {
1330 pr_warn("%s: No tree to attach; not running tests\n", __func__);
1331 kfree(unittest_data);
1332 return -ENODATA;
1333 }
1334
1335 /*
1336 * This lock normally encloses of_resolve_phandles()
1337 */
1338 of_overlay_mutex_lock();
1339
1340 rc = of_resolve_phandles(unittest_data_node);
1341 if (rc) {
1342 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1343 of_overlay_mutex_unlock();
1344 return -EINVAL;
1345 }
1346
1347 if (!of_root) {
1348 of_root = unittest_data_node;
1349 for_each_of_allnodes(np)
1350 __of_attach_node_sysfs(np);
1351 of_aliases = of_find_node_by_path("/aliases");
1352 of_chosen = of_find_node_by_path("/chosen");
1353 of_overlay_mutex_unlock();
1354 return 0;
1355 }
1356
1357 /* attach the sub-tree to live tree */
1358 np = unittest_data_node->child;
1359 while (np) {
1360 struct device_node *next = np->sibling;
1361
1362 np->parent = of_root;
1363 attach_node_and_children(np);
1364 np = next;
1365 }
1366
1367 of_overlay_mutex_unlock();
1368
1369 return 0;
1370}
1371
1372#ifdef CONFIG_OF_OVERLAY
1373static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
1374
1375static int unittest_probe(struct platform_device *pdev)
1376{
1377 struct device *dev = &pdev->dev;
1378 struct device_node *np = dev->of_node;
1379
1380 if (np == NULL) {
1381 dev_err(dev, "No OF data for device\n");
1382 return -EINVAL;
1383
1384 }
1385
1386 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1387
1388 of_platform_populate(np, NULL, NULL, &pdev->dev);
1389
1390 return 0;
1391}
1392
1393static int unittest_remove(struct platform_device *pdev)
1394{
1395 struct device *dev = &pdev->dev;
1396 struct device_node *np = dev->of_node;
1397
1398 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1399 return 0;
1400}
1401
1402static const struct of_device_id unittest_match[] = {
1403 { .compatible = "unittest", },
1404 {},
1405};
1406
1407static struct platform_driver unittest_driver = {
1408 .probe = unittest_probe,
1409 .remove = unittest_remove,
1410 .driver = {
1411 .name = "unittest",
1412 .of_match_table = of_match_ptr(unittest_match),
1413 },
1414};
1415
1416/* get the platform device instantiated at the path */
1417static struct platform_device *of_path_to_platform_device(const char *path)
1418{
1419 struct device_node *np;
1420 struct platform_device *pdev;
1421
1422 np = of_find_node_by_path(path);
1423 if (np == NULL)
1424 return NULL;
1425
1426 pdev = of_find_device_by_node(np);
1427 of_node_put(np);
1428
1429 return pdev;
1430}
1431
1432/* find out if a platform device exists at that path */
1433static int of_path_platform_device_exists(const char *path)
1434{
1435 struct platform_device *pdev;
1436
1437 pdev = of_path_to_platform_device(path);
1438 platform_device_put(pdev);
1439 return pdev != NULL;
1440}
1441
1442#if IS_BUILTIN(CONFIG_I2C)
1443
1444/* get the i2c client device instantiated at the path */
1445static struct i2c_client *of_path_to_i2c_client(const char *path)
1446{
1447 struct device_node *np;
1448 struct i2c_client *client;
1449
1450 np = of_find_node_by_path(path);
1451 if (np == NULL)
1452 return NULL;
1453
1454 client = of_find_i2c_device_by_node(np);
1455 of_node_put(np);
1456
1457 return client;
1458}
1459
1460/* find out if a i2c client device exists at that path */
1461static int of_path_i2c_client_exists(const char *path)
1462{
1463 struct i2c_client *client;
1464
1465 client = of_path_to_i2c_client(path);
1466 if (client)
1467 put_device(&client->dev);
1468 return client != NULL;
1469}
1470#else
1471static int of_path_i2c_client_exists(const char *path)
1472{
1473 return 0;
1474}
1475#endif
1476
1477enum overlay_type {
1478 PDEV_OVERLAY,
1479 I2C_OVERLAY
1480};
1481
1482static int of_path_device_type_exists(const char *path,
1483 enum overlay_type ovtype)
1484{
1485 switch (ovtype) {
1486 case PDEV_OVERLAY:
1487 return of_path_platform_device_exists(path);
1488 case I2C_OVERLAY:
1489 return of_path_i2c_client_exists(path);
1490 }
1491 return 0;
1492}
1493
1494static const char *unittest_path(int nr, enum overlay_type ovtype)
1495{
1496 const char *base;
1497 static char buf[256];
1498
1499 switch (ovtype) {
1500 case PDEV_OVERLAY:
1501 base = "/testcase-data/overlay-node/test-bus";
1502 break;
1503 case I2C_OVERLAY:
1504 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1505 break;
1506 default:
1507 buf[0] = '\0';
1508 return buf;
1509 }
1510 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1511 buf[sizeof(buf) - 1] = '\0';
1512 return buf;
1513}
1514
1515static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1516{
1517 const char *path;
1518
1519 path = unittest_path(unittest_nr, ovtype);
1520
1521 switch (ovtype) {
1522 case PDEV_OVERLAY:
1523 return of_path_platform_device_exists(path);
1524 case I2C_OVERLAY:
1525 return of_path_i2c_client_exists(path);
1526 }
1527 return 0;
1528}
1529
1530static const char *overlay_name_from_nr(int nr)
1531{
1532 static char buf[256];
1533
1534 snprintf(buf, sizeof(buf) - 1,
1535 "overlay_%d", nr);
1536 buf[sizeof(buf) - 1] = '\0';
1537
1538 return buf;
1539}
1540
1541static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1542
1543/* it is guaranteed that overlay ids are assigned in sequence */
1544#define MAX_UNITTEST_OVERLAYS 256
1545static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1546static int overlay_first_id = -1;
1547
1548static void of_unittest_track_overlay(int id)
1549{
1550 if (overlay_first_id < 0)
1551 overlay_first_id = id;
1552 id -= overlay_first_id;
1553
1554 /* we shouldn't need that many */
1555 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1556 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1557}
1558
1559static void of_unittest_untrack_overlay(int id)
1560{
1561 if (overlay_first_id < 0)
1562 return;
1563 id -= overlay_first_id;
1564 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1565 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1566}
1567
1568static void of_unittest_destroy_tracked_overlays(void)
1569{
1570 int id, ret, defers, ovcs_id;
1571
1572 if (overlay_first_id < 0)
1573 return;
1574
1575 /* try until no defers */
1576 do {
1577 defers = 0;
1578 /* remove in reverse order */
1579 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1580 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1581 continue;
1582
1583 ovcs_id = id + overlay_first_id;
1584 ret = of_overlay_remove(&ovcs_id);
1585 if (ret == -ENODEV) {
1586 pr_warn("%s: no overlay to destroy for #%d\n",
1587 __func__, id + overlay_first_id);
1588 continue;
1589 }
1590 if (ret != 0) {
1591 defers++;
1592 pr_warn("%s: overlay destroy failed for #%d\n",
1593 __func__, id + overlay_first_id);
1594 continue;
1595 }
1596
1597 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1598 }
1599 } while (defers > 0);
1600}
1601
1602static int __init of_unittest_apply_overlay(int overlay_nr, int *overlay_id)
1603{
1604 const char *overlay_name;
1605
1606 overlay_name = overlay_name_from_nr(overlay_nr);
1607
1608 if (!overlay_data_apply(overlay_name, overlay_id)) {
1609 unittest(0, "could not apply overlay \"%s\"\n",
1610 overlay_name);
1611 return -EFAULT;
1612 }
1613 of_unittest_track_overlay(*overlay_id);
1614
1615 return 0;
1616}
1617
1618/* apply an overlay while checking before and after states */
1619static int __init of_unittest_apply_overlay_check(int overlay_nr,
1620 int unittest_nr, int before, int after,
1621 enum overlay_type ovtype)
1622{
1623 int ret, ovcs_id;
1624
1625 /* unittest device must not be in before state */
1626 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1627 unittest(0, "%s with device @\"%s\" %s\n",
1628 overlay_name_from_nr(overlay_nr),
1629 unittest_path(unittest_nr, ovtype),
1630 !before ? "enabled" : "disabled");
1631 return -EINVAL;
1632 }
1633
1634 ovcs_id = 0;
1635 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
1636 if (ret != 0) {
1637 /* of_unittest_apply_overlay already called unittest() */
1638 return ret;
1639 }
1640
1641 /* unittest device must be to set to after state */
1642 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1643 unittest(0, "%s failed to create @\"%s\" %s\n",
1644 overlay_name_from_nr(overlay_nr),
1645 unittest_path(unittest_nr, ovtype),
1646 !after ? "enabled" : "disabled");
1647 return -EINVAL;
1648 }
1649
1650 return 0;
1651}
1652
1653/* apply an overlay and then revert it while checking before, after states */
1654static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
1655 int unittest_nr, int before, int after,
1656 enum overlay_type ovtype)
1657{
1658 int ret, ovcs_id;
1659
1660 /* unittest device must be in before state */
1661 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1662 unittest(0, "%s with device @\"%s\" %s\n",
1663 overlay_name_from_nr(overlay_nr),
1664 unittest_path(unittest_nr, ovtype),
1665 !before ? "enabled" : "disabled");
1666 return -EINVAL;
1667 }
1668
1669 /* apply the overlay */
1670 ovcs_id = 0;
1671 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
1672 if (ret != 0) {
1673 /* of_unittest_apply_overlay already called unittest() */
1674 return ret;
1675 }
1676
1677 /* unittest device must be in after state */
1678 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1679 unittest(0, "%s failed to create @\"%s\" %s\n",
1680 overlay_name_from_nr(overlay_nr),
1681 unittest_path(unittest_nr, ovtype),
1682 !after ? "enabled" : "disabled");
1683 return -EINVAL;
1684 }
1685
1686 ret = of_overlay_remove(&ovcs_id);
1687 if (ret != 0) {
1688 unittest(0, "%s failed to be destroyed @\"%s\"\n",
1689 overlay_name_from_nr(overlay_nr),
1690 unittest_path(unittest_nr, ovtype));
1691 return ret;
1692 }
1693
1694 /* unittest device must be again in before state */
1695 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1696 unittest(0, "%s with device @\"%s\" %s\n",
1697 overlay_name_from_nr(overlay_nr),
1698 unittest_path(unittest_nr, ovtype),
1699 !before ? "enabled" : "disabled");
1700 return -EINVAL;
1701 }
1702
1703 return 0;
1704}
1705
1706/* test activation of device */
1707static void __init of_unittest_overlay_0(void)
1708{
1709 /* device should enable */
1710 if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
1711 return;
1712
1713 unittest(1, "overlay test %d passed\n", 0);
1714}
1715
1716/* test deactivation of device */
1717static void __init of_unittest_overlay_1(void)
1718{
1719 /* device should disable */
1720 if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
1721 return;
1722
1723 unittest(1, "overlay test %d passed\n", 1);
1724}
1725
1726/* test activation of device */
1727static void __init of_unittest_overlay_2(void)
1728{
1729 /* device should enable */
1730 if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
1731 return;
1732
1733 unittest(1, "overlay test %d passed\n", 2);
1734}
1735
1736/* test deactivation of device */
1737static void __init of_unittest_overlay_3(void)
1738{
1739 /* device should disable */
1740 if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
1741 return;
1742
1743 unittest(1, "overlay test %d passed\n", 3);
1744}
1745
1746/* test activation of a full device node */
1747static void __init of_unittest_overlay_4(void)
1748{
1749 /* device should disable */
1750 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
1751 return;
1752
1753 unittest(1, "overlay test %d passed\n", 4);
1754}
1755
1756/* test overlay apply/revert sequence */
1757static void __init of_unittest_overlay_5(void)
1758{
1759 /* device should disable */
1760 if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
1761 return;
1762
1763 unittest(1, "overlay test %d passed\n", 5);
1764}
1765
1766/* test overlay application in sequence */
1767static void __init of_unittest_overlay_6(void)
1768{
1769 int i, ov_id[2], ovcs_id;
1770 int overlay_nr = 6, unittest_nr = 6;
1771 int before = 0, after = 1;
1772 const char *overlay_name;
1773
1774 /* unittest device must be in before state */
1775 for (i = 0; i < 2; i++) {
1776 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1777 != before) {
1778 unittest(0, "%s with device @\"%s\" %s\n",
1779 overlay_name_from_nr(overlay_nr + i),
1780 unittest_path(unittest_nr + i,
1781 PDEV_OVERLAY),
1782 !before ? "enabled" : "disabled");
1783 return;
1784 }
1785 }
1786
1787 /* apply the overlays */
1788 for (i = 0; i < 2; i++) {
1789
1790 overlay_name = overlay_name_from_nr(overlay_nr + i);
1791
1792 if (!overlay_data_apply(overlay_name, &ovcs_id)) {
1793 unittest(0, "could not apply overlay \"%s\"\n",
1794 overlay_name);
1795 return;
1796 }
1797 ov_id[i] = ovcs_id;
1798 of_unittest_track_overlay(ov_id[i]);
1799 }
1800
1801 for (i = 0; i < 2; i++) {
1802 /* unittest device must be in after state */
1803 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1804 != after) {
1805 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1806 overlay_name_from_nr(overlay_nr + i),
1807 unittest_path(unittest_nr + i,
1808 PDEV_OVERLAY),
1809 !after ? "enabled" : "disabled");
1810 return;
1811 }
1812 }
1813
1814 for (i = 1; i >= 0; i--) {
1815 ovcs_id = ov_id[i];
1816 if (of_overlay_remove(&ovcs_id)) {
1817 unittest(0, "%s failed destroy @\"%s\"\n",
1818 overlay_name_from_nr(overlay_nr + i),
1819 unittest_path(unittest_nr + i,
1820 PDEV_OVERLAY));
1821 return;
1822 }
1823 of_unittest_untrack_overlay(ov_id[i]);
1824 }
1825
1826 for (i = 0; i < 2; i++) {
1827 /* unittest device must be again in before state */
1828 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1829 != before) {
1830 unittest(0, "%s with device @\"%s\" %s\n",
1831 overlay_name_from_nr(overlay_nr + i),
1832 unittest_path(unittest_nr + i,
1833 PDEV_OVERLAY),
1834 !before ? "enabled" : "disabled");
1835 return;
1836 }
1837 }
1838
1839 unittest(1, "overlay test %d passed\n", 6);
1840}
1841
1842/* test overlay application in sequence */
1843static void __init of_unittest_overlay_8(void)
1844{
1845 int i, ov_id[2], ovcs_id;
1846 int overlay_nr = 8, unittest_nr = 8;
1847 const char *overlay_name;
1848
1849 /* we don't care about device state in this test */
1850
1851 /* apply the overlays */
1852 for (i = 0; i < 2; i++) {
1853
1854 overlay_name = overlay_name_from_nr(overlay_nr + i);
1855
1856 if (!overlay_data_apply(overlay_name, &ovcs_id)) {
1857 unittest(0, "could not apply overlay \"%s\"\n",
1858 overlay_name);
1859 return;
1860 }
1861 ov_id[i] = ovcs_id;
1862 of_unittest_track_overlay(ov_id[i]);
1863 }
1864
1865 /* now try to remove first overlay (it should fail) */
1866 ovcs_id = ov_id[0];
1867 if (!of_overlay_remove(&ovcs_id)) {
1868 unittest(0, "%s was destroyed @\"%s\"\n",
1869 overlay_name_from_nr(overlay_nr + 0),
1870 unittest_path(unittest_nr,
1871 PDEV_OVERLAY));
1872 return;
1873 }
1874
1875 /* removing them in order should work */
1876 for (i = 1; i >= 0; i--) {
1877 ovcs_id = ov_id[i];
1878 if (of_overlay_remove(&ovcs_id)) {
1879 unittest(0, "%s not destroyed @\"%s\"\n",
1880 overlay_name_from_nr(overlay_nr + i),
1881 unittest_path(unittest_nr,
1882 PDEV_OVERLAY));
1883 return;
1884 }
1885 of_unittest_untrack_overlay(ov_id[i]);
1886 }
1887
1888 unittest(1, "overlay test %d passed\n", 8);
1889}
1890
1891/* test insertion of a bus with parent devices */
1892static void __init of_unittest_overlay_10(void)
1893{
1894 int ret;
1895 char *child_path;
1896
1897 /* device should disable */
1898 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1899 if (unittest(ret == 0,
1900 "overlay test %d failed; overlay application\n", 10))
1901 return;
1902
1903 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1904 unittest_path(10, PDEV_OVERLAY));
1905 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
1906 return;
1907
1908 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1909 kfree(child_path);
1910
1911 unittest(ret, "overlay test %d failed; no child device\n", 10);
1912}
1913
1914/* test insertion of a bus with parent devices (and revert) */
1915static void __init of_unittest_overlay_11(void)
1916{
1917 int ret;
1918
1919 /* device should disable */
1920 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1921 PDEV_OVERLAY);
1922 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
1923}
1924
1925#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1926
1927struct unittest_i2c_bus_data {
1928 struct platform_device *pdev;
1929 struct i2c_adapter adap;
1930};
1931
1932static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
1933 struct i2c_msg *msgs, int num)
1934{
1935 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
1936
1937 (void)std;
1938
1939 return num;
1940}
1941
1942static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
1943{
1944 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1945}
1946
1947static const struct i2c_algorithm unittest_i2c_algo = {
1948 .master_xfer = unittest_i2c_master_xfer,
1949 .functionality = unittest_i2c_functionality,
1950};
1951
1952static int unittest_i2c_bus_probe(struct platform_device *pdev)
1953{
1954 struct device *dev = &pdev->dev;
1955 struct device_node *np = dev->of_node;
1956 struct unittest_i2c_bus_data *std;
1957 struct i2c_adapter *adap;
1958 int ret;
1959
1960 if (np == NULL) {
1961 dev_err(dev, "No OF data for device\n");
1962 return -EINVAL;
1963
1964 }
1965
1966 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1967
1968 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1969 if (!std)
1970 return -ENOMEM;
1971
1972 /* link them together */
1973 std->pdev = pdev;
1974 platform_set_drvdata(pdev, std);
1975
1976 adap = &std->adap;
1977 i2c_set_adapdata(adap, std);
1978 adap->nr = -1;
1979 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1980 adap->class = I2C_CLASS_DEPRECATED;
1981 adap->algo = &unittest_i2c_algo;
1982 adap->dev.parent = dev;
1983 adap->dev.of_node = dev->of_node;
1984 adap->timeout = 5 * HZ;
1985 adap->retries = 3;
1986
1987 ret = i2c_add_numbered_adapter(adap);
1988 if (ret != 0) {
1989 dev_err(dev, "Failed to add I2C adapter\n");
1990 return ret;
1991 }
1992
1993 return 0;
1994}
1995
1996static int unittest_i2c_bus_remove(struct platform_device *pdev)
1997{
1998 struct device *dev = &pdev->dev;
1999 struct device_node *np = dev->of_node;
2000 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2001
2002 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2003 i2c_del_adapter(&std->adap);
2004
2005 return 0;
2006}
2007
2008static const struct of_device_id unittest_i2c_bus_match[] = {
2009 { .compatible = "unittest-i2c-bus", },
2010 {},
2011};
2012
2013static struct platform_driver unittest_i2c_bus_driver = {
2014 .probe = unittest_i2c_bus_probe,
2015 .remove = unittest_i2c_bus_remove,
2016 .driver = {
2017 .name = "unittest-i2c-bus",
2018 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
2019 },
2020};
2021
2022static int unittest_i2c_dev_probe(struct i2c_client *client,
2023 const struct i2c_device_id *id)
2024{
2025 struct device *dev = &client->dev;
2026 struct device_node *np = client->dev.of_node;
2027
2028 if (!np) {
2029 dev_err(dev, "No OF node\n");
2030 return -EINVAL;
2031 }
2032
2033 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2034
2035 return 0;
2036};
2037
2038static int unittest_i2c_dev_remove(struct i2c_client *client)
2039{
2040 struct device *dev = &client->dev;
2041 struct device_node *np = client->dev.of_node;
2042
2043 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2044 return 0;
2045}
2046
2047static const struct i2c_device_id unittest_i2c_dev_id[] = {
2048 { .name = "unittest-i2c-dev" },
2049 { }
2050};
2051
2052static struct i2c_driver unittest_i2c_dev_driver = {
2053 .driver = {
2054 .name = "unittest-i2c-dev",
2055 },
2056 .probe = unittest_i2c_dev_probe,
2057 .remove = unittest_i2c_dev_remove,
2058 .id_table = unittest_i2c_dev_id,
2059};
2060
2061#if IS_BUILTIN(CONFIG_I2C_MUX)
2062
2063static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2064{
2065 return 0;
2066}
2067
2068static int unittest_i2c_mux_probe(struct i2c_client *client,
2069 const struct i2c_device_id *id)
2070{
2071 int i, nchans;
2072 struct device *dev = &client->dev;
2073 struct i2c_adapter *adap = client->adapter;
2074 struct device_node *np = client->dev.of_node, *child;
2075 struct i2c_mux_core *muxc;
2076 u32 reg, max_reg;
2077
2078 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2079
2080 if (!np) {
2081 dev_err(dev, "No OF node\n");
2082 return -EINVAL;
2083 }
2084
2085 max_reg = (u32)-1;
2086 for_each_child_of_node(np, child) {
2087 if (of_property_read_u32(child, "reg", &reg))
2088 continue;
2089 if (max_reg == (u32)-1 || reg > max_reg)
2090 max_reg = reg;
2091 }
2092 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2093 if (nchans == 0) {
2094 dev_err(dev, "No channels\n");
2095 return -EINVAL;
2096 }
2097
2098 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2099 unittest_i2c_mux_select_chan, NULL);
2100 if (!muxc)
2101 return -ENOMEM;
2102 for (i = 0; i < nchans; i++) {
2103 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2104 dev_err(dev, "Failed to register mux #%d\n", i);
2105 i2c_mux_del_adapters(muxc);
2106 return -ENODEV;
2107 }
2108 }
2109
2110 i2c_set_clientdata(client, muxc);
2111
2112 return 0;
2113};
2114
2115static int unittest_i2c_mux_remove(struct i2c_client *client)
2116{
2117 struct device *dev = &client->dev;
2118 struct device_node *np = client->dev.of_node;
2119 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2120
2121 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2122 i2c_mux_del_adapters(muxc);
2123 return 0;
2124}
2125
2126static const struct i2c_device_id unittest_i2c_mux_id[] = {
2127 { .name = "unittest-i2c-mux" },
2128 { }
2129};
2130
2131static struct i2c_driver unittest_i2c_mux_driver = {
2132 .driver = {
2133 .name = "unittest-i2c-mux",
2134 },
2135 .probe = unittest_i2c_mux_probe,
2136 .remove = unittest_i2c_mux_remove,
2137 .id_table = unittest_i2c_mux_id,
2138};
2139
2140#endif
2141
2142static int of_unittest_overlay_i2c_init(void)
2143{
2144 int ret;
2145
2146 ret = i2c_add_driver(&unittest_i2c_dev_driver);
2147 if (unittest(ret == 0,
2148 "could not register unittest i2c device driver\n"))
2149 return ret;
2150
2151 ret = platform_driver_register(&unittest_i2c_bus_driver);
2152 if (unittest(ret == 0,
2153 "could not register unittest i2c bus driver\n"))
2154 return ret;
2155
2156#if IS_BUILTIN(CONFIG_I2C_MUX)
2157 ret = i2c_add_driver(&unittest_i2c_mux_driver);
2158 if (unittest(ret == 0,
2159 "could not register unittest i2c mux driver\n"))
2160 return ret;
2161#endif
2162
2163 return 0;
2164}
2165
2166static void of_unittest_overlay_i2c_cleanup(void)
2167{
2168#if IS_BUILTIN(CONFIG_I2C_MUX)
2169 i2c_del_driver(&unittest_i2c_mux_driver);
2170#endif
2171 platform_driver_unregister(&unittest_i2c_bus_driver);
2172 i2c_del_driver(&unittest_i2c_dev_driver);
2173}
2174
2175static void __init of_unittest_overlay_i2c_12(void)
2176{
2177 /* device should enable */
2178 if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
2179 return;
2180
2181 unittest(1, "overlay test %d passed\n", 12);
2182}
2183
2184/* test deactivation of device */
2185static void __init of_unittest_overlay_i2c_13(void)
2186{
2187 /* device should disable */
2188 if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
2189 return;
2190
2191 unittest(1, "overlay test %d passed\n", 13);
2192}
2193
2194/* just check for i2c mux existence */
2195static void of_unittest_overlay_i2c_14(void)
2196{
2197}
2198
2199static void __init of_unittest_overlay_i2c_15(void)
2200{
2201 /* device should enable */
2202 if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
2203 return;
2204
2205 unittest(1, "overlay test %d passed\n", 15);
2206}
2207
2208#else
2209
2210static inline void of_unittest_overlay_i2c_14(void) { }
2211static inline void of_unittest_overlay_i2c_15(void) { }
2212
2213#endif
2214
2215static void __init of_unittest_overlay_gpio(void)
2216{
2217 int chip_request_count;
2218 int probe_pass_count;
2219 int ret;
2220
2221 /*
2222 * tests: apply overlays before registering driver
2223 * Similar to installing a driver as a module, the
2224 * driver is registered after applying the overlays.
2225 *
2226 * - apply overlay_gpio_01
2227 * - apply overlay_gpio_02a
2228 * - apply overlay_gpio_02b
2229 * - register driver
2230 *
2231 * register driver will result in
2232 * - probe and processing gpio hog for overlay_gpio_01
2233 * - probe for overlay_gpio_02a
2234 * - processing gpio for overlay_gpio_02b
2235 */
2236
2237 probe_pass_count = unittest_gpio_probe_pass_count;
2238 chip_request_count = unittest_gpio_chip_request_count;
2239
2240 /*
2241 * overlay_gpio_01 contains gpio node and child gpio hog node
2242 * overlay_gpio_02a contains gpio node
2243 * overlay_gpio_02b contains child gpio hog node
2244 */
2245
2246 unittest(overlay_data_apply("overlay_gpio_01", NULL),
2247 "Adding overlay 'overlay_gpio_01' failed\n");
2248
2249 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
2250 "Adding overlay 'overlay_gpio_02a' failed\n");
2251
2252 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
2253 "Adding overlay 'overlay_gpio_02b' failed\n");
2254
2255 /*
2256 * messages are the result of the probes, after the
2257 * driver is registered
2258 */
2259
2260 EXPECT_BEGIN(KERN_INFO,
2261 "gpio-<<int>> (line-B-input): hogged as input\n");
2262
2263 EXPECT_BEGIN(KERN_INFO,
2264 "gpio-<<int>> (line-A-input): hogged as input\n");
2265
2266 ret = platform_driver_register(&unittest_gpio_driver);
2267 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
2268 return;
2269
2270 EXPECT_END(KERN_INFO,
2271 "gpio-<<int>> (line-A-input): hogged as input\n");
2272 EXPECT_END(KERN_INFO,
2273 "gpio-<<int>> (line-B-input): hogged as input\n");
2274
2275 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
2276 "unittest_gpio_probe() failed or not called\n");
2277
2278 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
2279 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2280 unittest_gpio_chip_request_count - chip_request_count);
2281
2282 /*
2283 * tests: apply overlays after registering driver
2284 *
2285 * Similar to a driver built-in to the kernel, the
2286 * driver is registered before applying the overlays.
2287 *
2288 * overlay_gpio_03 contains gpio node and child gpio hog node
2289 *
2290 * - apply overlay_gpio_03
2291 *
2292 * apply overlay will result in
2293 * - probe and processing gpio hog.
2294 */
2295
2296 probe_pass_count = unittest_gpio_probe_pass_count;
2297 chip_request_count = unittest_gpio_chip_request_count;
2298
2299 EXPECT_BEGIN(KERN_INFO,
2300 "gpio-<<int>> (line-D-input): hogged as input\n");
2301
2302 /* overlay_gpio_03 contains gpio node and child gpio hog node */
2303
2304 unittest(overlay_data_apply("overlay_gpio_03", NULL),
2305 "Adding overlay 'overlay_gpio_03' failed\n");
2306
2307 EXPECT_END(KERN_INFO,
2308 "gpio-<<int>> (line-D-input): hogged as input\n");
2309
2310 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2311 "unittest_gpio_probe() failed or not called\n");
2312
2313 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2314 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2315 unittest_gpio_chip_request_count - chip_request_count);
2316
2317 /*
2318 * overlay_gpio_04a contains gpio node
2319 *
2320 * - apply overlay_gpio_04a
2321 *
2322 * apply the overlay will result in
2323 * - probe for overlay_gpio_04a
2324 */
2325
2326 probe_pass_count = unittest_gpio_probe_pass_count;
2327 chip_request_count = unittest_gpio_chip_request_count;
2328
2329 /* overlay_gpio_04a contains gpio node */
2330
2331 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
2332 "Adding overlay 'overlay_gpio_04a' failed\n");
2333
2334 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2335 "unittest_gpio_probe() failed or not called\n");
2336
2337 /*
2338 * overlay_gpio_04b contains child gpio hog node
2339 *
2340 * - apply overlay_gpio_04b
2341 *
2342 * apply the overlay will result in
2343 * - processing gpio for overlay_gpio_04b
2344 */
2345
2346 EXPECT_BEGIN(KERN_INFO,
2347 "gpio-<<int>> (line-C-input): hogged as input\n");
2348
2349 /* overlay_gpio_04b contains child gpio hog node */
2350
2351 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
2352 "Adding overlay 'overlay_gpio_04b' failed\n");
2353
2354 EXPECT_END(KERN_INFO,
2355 "gpio-<<int>> (line-C-input): hogged as input\n");
2356
2357 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2358 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2359 unittest_gpio_chip_request_count - chip_request_count);
2360}
2361
2362static void __init of_unittest_overlay(void)
2363{
2364 struct device_node *bus_np = NULL;
2365
2366 if (platform_driver_register(&unittest_driver)) {
2367 unittest(0, "could not register unittest driver\n");
2368 goto out;
2369 }
2370
2371 bus_np = of_find_node_by_path(bus_path);
2372 if (bus_np == NULL) {
2373 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
2374 goto out;
2375 }
2376
2377 if (of_platform_default_populate(bus_np, NULL, NULL)) {
2378 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
2379 goto out;
2380 }
2381
2382 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2383 unittest(0, "could not find unittest0 @ \"%s\"\n",
2384 unittest_path(100, PDEV_OVERLAY));
2385 goto out;
2386 }
2387
2388 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2389 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2390 unittest_path(101, PDEV_OVERLAY));
2391 goto out;
2392 }
2393
2394 unittest(1, "basic infrastructure of overlays passed");
2395
2396 /* tests in sequence */
2397 of_unittest_overlay_0();
2398 of_unittest_overlay_1();
2399 of_unittest_overlay_2();
2400 of_unittest_overlay_3();
2401 of_unittest_overlay_4();
2402 of_unittest_overlay_5();
2403 of_unittest_overlay_6();
2404 of_unittest_overlay_8();
2405
2406 of_unittest_overlay_10();
2407 of_unittest_overlay_11();
2408
2409#if IS_BUILTIN(CONFIG_I2C)
2410 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
2411 goto out;
2412
2413 of_unittest_overlay_i2c_12();
2414 of_unittest_overlay_i2c_13();
2415 of_unittest_overlay_i2c_14();
2416 of_unittest_overlay_i2c_15();
2417
2418 of_unittest_overlay_i2c_cleanup();
2419#endif
2420
2421 of_unittest_overlay_gpio();
2422
2423 of_unittest_destroy_tracked_overlays();
2424
2425out:
2426 of_node_put(bus_np);
2427}
2428
2429#else
2430static inline void __init of_unittest_overlay(void) { }
2431#endif
2432
2433#ifdef CONFIG_OF_OVERLAY
2434
2435/*
2436 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2437 * in scripts/Makefile.lib
2438 */
2439
2440#define OVERLAY_INFO_EXTERN(name) \
2441 extern uint8_t __dtb_##name##_begin[]; \
2442 extern uint8_t __dtb_##name##_end[]
2443
2444#define OVERLAY_INFO(overlay_name, expected) \
2445{ .dtb_begin = __dtb_##overlay_name##_begin, \
2446 .dtb_end = __dtb_##overlay_name##_end, \
2447 .expected_result = expected, \
2448 .name = #overlay_name, \
2449}
2450
2451struct overlay_info {
2452 uint8_t *dtb_begin;
2453 uint8_t *dtb_end;
2454 int expected_result;
2455 int overlay_id;
2456 char *name;
2457};
2458
2459OVERLAY_INFO_EXTERN(overlay_base);
2460OVERLAY_INFO_EXTERN(overlay);
2461OVERLAY_INFO_EXTERN(overlay_0);
2462OVERLAY_INFO_EXTERN(overlay_1);
2463OVERLAY_INFO_EXTERN(overlay_2);
2464OVERLAY_INFO_EXTERN(overlay_3);
2465OVERLAY_INFO_EXTERN(overlay_4);
2466OVERLAY_INFO_EXTERN(overlay_5);
2467OVERLAY_INFO_EXTERN(overlay_6);
2468OVERLAY_INFO_EXTERN(overlay_7);
2469OVERLAY_INFO_EXTERN(overlay_8);
2470OVERLAY_INFO_EXTERN(overlay_9);
2471OVERLAY_INFO_EXTERN(overlay_10);
2472OVERLAY_INFO_EXTERN(overlay_11);
2473OVERLAY_INFO_EXTERN(overlay_12);
2474OVERLAY_INFO_EXTERN(overlay_13);
2475OVERLAY_INFO_EXTERN(overlay_15);
2476OVERLAY_INFO_EXTERN(overlay_gpio_01);
2477OVERLAY_INFO_EXTERN(overlay_gpio_02a);
2478OVERLAY_INFO_EXTERN(overlay_gpio_02b);
2479OVERLAY_INFO_EXTERN(overlay_gpio_03);
2480OVERLAY_INFO_EXTERN(overlay_gpio_04a);
2481OVERLAY_INFO_EXTERN(overlay_gpio_04b);
2482OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
2483OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
2484OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2485OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2486
2487/* entries found by name */
2488static struct overlay_info overlays[] = {
2489 OVERLAY_INFO(overlay_base, -9999),
2490 OVERLAY_INFO(overlay, 0),
2491 OVERLAY_INFO(overlay_0, 0),
2492 OVERLAY_INFO(overlay_1, 0),
2493 OVERLAY_INFO(overlay_2, 0),
2494 OVERLAY_INFO(overlay_3, 0),
2495 OVERLAY_INFO(overlay_4, 0),
2496 OVERLAY_INFO(overlay_5, 0),
2497 OVERLAY_INFO(overlay_6, 0),
2498 OVERLAY_INFO(overlay_7, 0),
2499 OVERLAY_INFO(overlay_8, 0),
2500 OVERLAY_INFO(overlay_9, 0),
2501 OVERLAY_INFO(overlay_10, 0),
2502 OVERLAY_INFO(overlay_11, 0),
2503 OVERLAY_INFO(overlay_12, 0),
2504 OVERLAY_INFO(overlay_13, 0),
2505 OVERLAY_INFO(overlay_15, 0),
2506 OVERLAY_INFO(overlay_gpio_01, 0),
2507 OVERLAY_INFO(overlay_gpio_02a, 0),
2508 OVERLAY_INFO(overlay_gpio_02b, 0),
2509 OVERLAY_INFO(overlay_gpio_03, 0),
2510 OVERLAY_INFO(overlay_gpio_04a, 0),
2511 OVERLAY_INFO(overlay_gpio_04b, 0),
2512 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
2513 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
2514 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2515 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2516 /* end marker */
2517 {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
2518};
2519
2520static struct device_node *overlay_base_root;
2521
2522static void * __init dt_alloc_memory(u64 size, u64 align)
2523{
2524 void *ptr = memblock_alloc(size, align);
2525
2526 if (!ptr)
2527 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2528 __func__, size, align);
2529
2530 return ptr;
2531}
2532
2533/*
2534 * Create base device tree for the overlay unittest.
2535 *
2536 * This is called from very early boot code.
2537 *
2538 * Do as much as possible the same way as done in __unflatten_device_tree
2539 * and other early boot steps for the normal FDT so that the overlay base
2540 * unflattened tree will have the same characteristics as the real tree
2541 * (such as having memory allocated by the early allocator). The goal
2542 * is to test "the real thing" as much as possible, and test "test setup
2543 * code" as little as possible.
2544 *
2545 * Have to stop before resolving phandles, because that uses kmalloc.
2546 */
2547void __init unittest_unflatten_overlay_base(void)
2548{
2549 struct overlay_info *info;
2550 u32 data_size;
2551 void *new_fdt;
2552 u32 size;
2553 int found = 0;
2554 const char *overlay_name = "overlay_base";
2555
2556 for (info = overlays; info && info->name; info++) {
2557 if (!strcmp(overlay_name, info->name)) {
2558 found = 1;
2559 break;
2560 }
2561 }
2562 if (!found) {
2563 pr_err("no overlay data for %s\n", overlay_name);
2564 return;
2565 }
2566
2567 info = &overlays[0];
2568
2569 if (info->expected_result != -9999) {
2570 pr_err("No dtb 'overlay_base' to attach\n");
2571 return;
2572 }
2573
2574 data_size = info->dtb_end - info->dtb_begin;
2575 if (!data_size) {
2576 pr_err("No dtb 'overlay_base' to attach\n");
2577 return;
2578 }
2579
2580 size = fdt_totalsize(info->dtb_begin);
2581 if (size != data_size) {
2582 pr_err("dtb 'overlay_base' header totalsize != actual size");
2583 return;
2584 }
2585
2586 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2587 if (!new_fdt) {
2588 pr_err("alloc for dtb 'overlay_base' failed");
2589 return;
2590 }
2591
2592 memcpy(new_fdt, info->dtb_begin, size);
2593
2594 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
2595 dt_alloc_memory, true);
2596}
2597
2598/*
2599 * The purpose of of_unittest_overlay_data_add is to add an
2600 * overlay in the normal fashion. This is a test of the whole
2601 * picture, instead of testing individual elements.
2602 *
2603 * A secondary purpose is to be able to verify that the contents of
2604 * /proc/device-tree/ contains the updated structure and values from
2605 * the overlay. That must be verified separately in user space.
2606 *
2607 * Return 0 on unexpected error.
2608 */
2609static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
2610{
2611 struct overlay_info *info;
2612 int found = 0;
2613 int ret;
2614 u32 size;
2615
2616 for (info = overlays; info && info->name; info++) {
2617 if (!strcmp(overlay_name, info->name)) {
2618 found = 1;
2619 break;
2620 }
2621 }
2622 if (!found) {
2623 pr_err("no overlay data for %s\n", overlay_name);
2624 return 0;
2625 }
2626
2627 size = info->dtb_end - info->dtb_begin;
2628 if (!size)
2629 pr_err("no overlay data for %s\n", overlay_name);
2630
2631 ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
2632 if (overlay_id)
2633 *overlay_id = info->overlay_id;
2634 if (ret < 0)
2635 goto out;
2636
2637 pr_debug("%s applied\n", overlay_name);
2638
2639out:
2640 if (ret != info->expected_result)
2641 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2642 info->expected_result, ret, overlay_name);
2643
2644 return (ret == info->expected_result);
2645}
2646
2647/*
2648 * The purpose of of_unittest_overlay_high_level is to add an overlay
2649 * in the normal fashion. This is a test of the whole picture,
2650 * instead of individual elements.
2651 *
2652 * The first part of the function is _not_ normal overlay usage; it is
2653 * finishing splicing the base overlay device tree into the live tree.
2654 */
2655static __init void of_unittest_overlay_high_level(void)
2656{
2657 struct device_node *last_sibling;
2658 struct device_node *np;
2659 struct device_node *of_symbols;
2660 struct device_node *overlay_base_symbols;
2661 struct device_node **pprev;
2662 struct property *prop;
2663
2664 if (!overlay_base_root) {
2665 unittest(0, "overlay_base_root not initialized\n");
2666 return;
2667 }
2668
2669 /*
2670 * Could not fixup phandles in unittest_unflatten_overlay_base()
2671 * because kmalloc() was not yet available.
2672 */
2673 of_overlay_mutex_lock();
2674 of_resolve_phandles(overlay_base_root);
2675 of_overlay_mutex_unlock();
2676
2677
2678 /*
2679 * do not allow overlay_base to duplicate any node already in
2680 * tree, this greatly simplifies the code
2681 */
2682
2683 /*
2684 * remove overlay_base_root node "__local_fixups", after
2685 * being used by of_resolve_phandles()
2686 */
2687 pprev = &overlay_base_root->child;
2688 for (np = overlay_base_root->child; np; np = np->sibling) {
2689 if (of_node_name_eq(np, "__local_fixups__")) {
2690 *pprev = np->sibling;
2691 break;
2692 }
2693 pprev = &np->sibling;
2694 }
2695
2696 /* remove overlay_base_root node "__symbols__" if in live tree */
2697 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2698 if (of_symbols) {
2699 /* will have to graft properties from node into live tree */
2700 pprev = &overlay_base_root->child;
2701 for (np = overlay_base_root->child; np; np = np->sibling) {
2702 if (of_node_name_eq(np, "__symbols__")) {
2703 overlay_base_symbols = np;
2704 *pprev = np->sibling;
2705 break;
2706 }
2707 pprev = &np->sibling;
2708 }
2709 }
2710
2711 for_each_child_of_node(overlay_base_root, np) {
2712 struct device_node *base_child;
2713 for_each_child_of_node(of_root, base_child) {
2714 if (!strcmp(np->full_name, base_child->full_name)) {
2715 unittest(0, "illegal node name in overlay_base %pOFn",
2716 np);
2717 return;
2718 }
2719 }
2720 }
2721
2722 /*
2723 * overlay 'overlay_base' is not allowed to have root
2724 * properties, so only need to splice nodes into main device tree.
2725 *
2726 * root node of *overlay_base_root will not be freed, it is lost
2727 * memory.
2728 */
2729
2730 for (np = overlay_base_root->child; np; np = np->sibling)
2731 np->parent = of_root;
2732
2733 mutex_lock(&of_mutex);
2734
2735 for (last_sibling = np = of_root->child; np; np = np->sibling)
2736 last_sibling = np;
2737
2738 if (last_sibling)
2739 last_sibling->sibling = overlay_base_root->child;
2740 else
2741 of_root->child = overlay_base_root->child;
2742
2743 for_each_of_allnodes_from(overlay_base_root, np)
2744 __of_attach_node_sysfs(np);
2745
2746 if (of_symbols) {
2747 struct property *new_prop;
2748 for_each_property_of_node(overlay_base_symbols, prop) {
2749
2750 new_prop = __of_prop_dup(prop, GFP_KERNEL);
2751 if (!new_prop) {
2752 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
2753 prop->name);
2754 goto err_unlock;
2755 }
2756 if (__of_add_property(of_symbols, new_prop)) {
2757 kfree(new_prop->name);
2758 kfree(new_prop->value);
2759 kfree(new_prop);
2760 /* "name" auto-generated by unflatten */
2761 if (!strcmp(prop->name, "name"))
2762 continue;
2763 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2764 prop->name);
2765 goto err_unlock;
2766 }
2767 if (__of_add_property_sysfs(of_symbols, new_prop)) {
2768 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2769 prop->name);
2770 goto err_unlock;
2771 }
2772 }
2773 }
2774
2775 mutex_unlock(&of_mutex);
2776
2777
2778 /* now do the normal overlay usage test */
2779
2780 unittest(overlay_data_apply("overlay", NULL),
2781 "Adding overlay 'overlay' failed\n");
2782
2783 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
2784 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
2785
2786 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
2787 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
2788
2789 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
2790 "Adding overlay 'overlay_bad_phandle' failed\n");
2791
2792 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
2793 "Adding overlay 'overlay_bad_symbol' failed\n");
2794
2795 return;
2796
2797err_unlock:
2798 mutex_unlock(&of_mutex);
2799}
2800
2801#else
2802
2803static inline __init void of_unittest_overlay_high_level(void) {}
2804
2805#endif
2806
2807static int __init of_unittest(void)
2808{
2809 struct device_node *np;
2810 int res;
2811
2812 /* adding data for unittest */
2813
2814 if (IS_ENABLED(CONFIG_UML))
2815 unittest_unflatten_overlay_base();
2816
2817 res = unittest_data_add();
2818 if (res)
2819 return res;
2820 if (!of_aliases)
2821 of_aliases = of_find_node_by_path("/aliases");
2822
2823 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2824 if (!np) {
2825 pr_info("No testcase data in device tree; not running tests\n");
2826 return 0;
2827 }
2828 of_node_put(np);
2829
2830 pr_info("start of unittest - you will see error messages\n");
2831 of_unittest_check_tree_linkage();
2832 of_unittest_check_phandles();
2833 of_unittest_find_node_by_name();
2834 of_unittest_dynamic();
2835 of_unittest_parse_phandle_with_args();
2836 of_unittest_parse_phandle_with_args_map();
2837 of_unittest_printf();
2838 of_unittest_property_string();
2839 of_unittest_property_copy();
2840 of_unittest_changeset();
2841 of_unittest_parse_interrupts();
2842 of_unittest_parse_interrupts_extended();
2843 of_unittest_match_node();
2844 of_unittest_platform_populate();
2845 of_unittest_overlay();
2846
2847 /* Double check linkage after removing testcase data */
2848 of_unittest_check_tree_linkage();
2849
2850 of_unittest_overlay_high_level();
2851
2852 pr_info("end of unittest - %i passed, %i failed\n",
2853 unittest_results.passed, unittest_results.failed);
2854
2855 return 0;
2856}
2857late_initcall(of_unittest);