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