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