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