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