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/pci.h>
26 #include <linux/kernel.h>
27
28 #include <linux/i2c.h>
29 #include <linux/i2c-mux.h>
30 #include <linux/gpio/driver.h>
31
32 #include <linux/bitops.h>
33
34 #include "of_private.h"
35
36 static struct unittest_results {
37 int passed;
38 int failed;
39 } unittest_results;
40
41 #define unittest(result, fmt, ...) ({ \
42 bool failed = !(result); \
43 if (failed) { \
44 unittest_results.failed++; \
45 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
46 } else { \
47 unittest_results.passed++; \
48 pr_info("pass %s():%i\n", __func__, __LINE__); \
49 } \
50 failed; \
51 })
52
53 #ifdef CONFIG_OF_KOBJ
54 #define OF_KREF_READ(NODE) kref_read(&(NODE)->kobj.kref)
55 #else
56 #define OF_KREF_READ(NODE) 1
57 #endif
58
59 /*
60 * Expected message may have a message level other than KERN_INFO.
61 * Print the expected message only if the current loglevel will allow
62 * the actual message to print.
63 *
64 * Do not use EXPECT_BEGIN(), EXPECT_END(), EXPECT_NOT_BEGIN(), or
65 * EXPECT_NOT_END() to report messages expected to be reported or not
66 * reported by pr_debug().
67 */
68 #define EXPECT_BEGIN(level, fmt, ...) \
69 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
70
71 #define EXPECT_END(level, fmt, ...) \
72 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
73
74 #define EXPECT_NOT_BEGIN(level, fmt, ...) \
75 printk(level pr_fmt("EXPECT_NOT \\ : ") fmt, ##__VA_ARGS__)
76
77 #define EXPECT_NOT_END(level, fmt, ...) \
78 printk(level pr_fmt("EXPECT_NOT / : ") fmt, ##__VA_ARGS__)
79
of_unittest_find_node_by_name(void)80 static void __init of_unittest_find_node_by_name(void)
81 {
82 struct device_node *np;
83 const char *options, *name;
84
85 np = of_find_node_by_path("/testcase-data");
86 name = kasprintf(GFP_KERNEL, "%pOF", np);
87 unittest(np && name && !strcmp("/testcase-data", name),
88 "find /testcase-data failed\n");
89 of_node_put(np);
90 kfree(name);
91
92 /* Test if trailing '/' works */
93 np = of_find_node_by_path("/testcase-data/");
94 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
95
96 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
97 name = kasprintf(GFP_KERNEL, "%pOF", np);
98 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
99 "find /testcase-data/phandle-tests/consumer-a failed\n");
100 of_node_put(np);
101 kfree(name);
102
103 np = of_find_node_by_path("testcase-alias");
104 name = kasprintf(GFP_KERNEL, "%pOF", np);
105 unittest(np && name && !strcmp("/testcase-data", name),
106 "find testcase-alias failed\n");
107 of_node_put(np);
108 kfree(name);
109
110 /* Test if trailing '/' works on aliases */
111 np = of_find_node_by_path("testcase-alias/");
112 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
113
114 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
115 name = kasprintf(GFP_KERNEL, "%pOF", np);
116 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
117 "find testcase-alias/phandle-tests/consumer-a failed\n");
118 of_node_put(np);
119 kfree(name);
120
121 np = of_find_node_by_path("/testcase-data/missing-path");
122 unittest(!np, "non-existent path returned node %pOF\n", np);
123 of_node_put(np);
124
125 np = of_find_node_by_path("missing-alias");
126 unittest(!np, "non-existent alias returned node %pOF\n", np);
127 of_node_put(np);
128
129 np = of_find_node_by_path("testcase-alias/missing-path");
130 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
131 of_node_put(np);
132
133 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
134 unittest(np && !strcmp("testoption", options),
135 "option path test failed\n");
136 of_node_put(np);
137
138 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
139 unittest(np && !strcmp("test/option", options),
140 "option path test, subcase #1 failed\n");
141 of_node_put(np);
142
143 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
144 unittest(np && !strcmp("test/option", options),
145 "option path test, subcase #2 failed\n");
146 of_node_put(np);
147
148 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
149 unittest(np, "NULL option path test failed\n");
150 of_node_put(np);
151
152 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
153 &options);
154 unittest(np && !strcmp("testaliasoption", options),
155 "option alias path test failed\n");
156 of_node_put(np);
157
158 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
159 &options);
160 unittest(np && !strcmp("test/alias/option", options),
161 "option alias path test, subcase #1 failed\n");
162 of_node_put(np);
163
164 np = of_find_node_opts_by_path("testcase-alias/phandle-tests/consumer-a:testaliasoption",
165 &options);
166 name = kasprintf(GFP_KERNEL, "%pOF", np);
167 unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name) &&
168 !strcmp("testaliasoption", options),
169 "option alias path test, subcase #2 failed\n");
170 of_node_put(np);
171 kfree(name);
172
173 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
174 unittest(np, "NULL option alias path test failed\n");
175 of_node_put(np);
176
177 options = "testoption";
178 np = of_find_node_opts_by_path("testcase-alias", &options);
179 unittest(np && !options, "option clearing test failed\n");
180 of_node_put(np);
181
182 options = "testoption";
183 np = of_find_node_opts_by_path("/", &options);
184 unittest(np && !options, "option clearing root node test failed\n");
185 of_node_put(np);
186 }
187
of_unittest_dynamic(void)188 static void __init of_unittest_dynamic(void)
189 {
190 struct device_node *np;
191 struct property *prop;
192
193 np = of_find_node_by_path("/testcase-data");
194 if (!np) {
195 pr_err("missing testcase data\n");
196 return;
197 }
198
199 /* Array of 4 properties for the purpose of testing */
200 prop = kzalloc_objs(*prop, 4, GFP_KERNEL);
201 if (!prop) {
202 unittest(0, "kzalloc() failed\n");
203 return;
204 }
205
206 /* Add a new property - should pass*/
207 prop->name = "new-property";
208 prop->value = "new-property-data";
209 prop->length = strlen(prop->value) + 1;
210 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
211
212 /* Try to add an existing property - should fail */
213 prop++;
214 prop->name = "new-property";
215 prop->value = "new-property-data-should-fail";
216 prop->length = strlen(prop->value) + 1;
217 unittest(of_add_property(np, prop) != 0,
218 "Adding an existing property should have failed\n");
219
220 /* Try to modify an existing property - should pass */
221 prop->value = "modify-property-data-should-pass";
222 prop->length = strlen(prop->value) + 1;
223 unittest(of_update_property(np, prop) == 0,
224 "Updating an existing property should have passed\n");
225
226 /* Try to modify non-existent property - should pass*/
227 prop++;
228 prop->name = "modify-property";
229 prop->value = "modify-missing-property-data-should-pass";
230 prop->length = strlen(prop->value) + 1;
231 unittest(of_update_property(np, prop) == 0,
232 "Updating a missing property should have passed\n");
233
234 /* Remove property - should pass */
235 unittest(of_remove_property(np, prop) == 0,
236 "Removing a property should have passed\n");
237
238 /* Adding very large property - should pass */
239 prop++;
240 prop->name = "large-property-PAGE_SIZEx8";
241 prop->length = PAGE_SIZE * 8;
242 prop->value = kzalloc(prop->length, GFP_KERNEL);
243 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
244 if (prop->value)
245 unittest(of_add_property(np, prop) == 0,
246 "Adding a large property should have passed\n");
247 }
248
of_unittest_check_node_linkage(struct device_node * np)249 static int __init of_unittest_check_node_linkage(struct device_node *np)
250 {
251 int count = 0, rc;
252
253 for_each_child_of_node_scoped(np, child) {
254 if (child->parent != np) {
255 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
256 child, np);
257 return -EINVAL;
258 }
259
260 rc = of_unittest_check_node_linkage(child);
261 if (rc < 0)
262 return rc;
263 count += rc;
264 }
265
266 return count + 1;
267 }
268
of_unittest_check_tree_linkage(void)269 static void __init of_unittest_check_tree_linkage(void)
270 {
271 struct device_node *np;
272 int allnode_count = 0, child_count;
273
274 if (!of_root)
275 return;
276
277 for_each_of_allnodes(np)
278 allnode_count++;
279 child_count = of_unittest_check_node_linkage(of_root);
280
281 unittest(child_count > 0, "Device node data structure is corrupted\n");
282 unittest(child_count == allnode_count,
283 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
284 allnode_count, child_count);
285 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
286 }
287
of_unittest_printf_one(struct device_node * np,const char * fmt,const char * expected)288 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
289 const char *expected)
290 {
291 unsigned char *buf;
292 int buf_size;
293 int size, i;
294
295 buf_size = strlen(expected) + 10;
296 buf = kmalloc(buf_size, GFP_KERNEL);
297 if (!buf)
298 return;
299
300 /* Baseline; check conversion with a large size limit */
301 memset(buf, 0xff, buf_size);
302 size = snprintf(buf, buf_size - 2, fmt, np);
303
304 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
305 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
306 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
307 fmt, expected, buf);
308
309 /* Make sure length limits work */
310 size++;
311 for (i = 0; i < 2; i++, size--) {
312 /* Clear the buffer, and make sure it works correctly still */
313 memset(buf, 0xff, buf_size);
314 snprintf(buf, size+1, fmt, np);
315 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
316 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
317 size, fmt, expected, buf);
318 }
319 kfree(buf);
320 }
321
of_unittest_printf(void)322 static void __init of_unittest_printf(void)
323 {
324 struct device_node *np;
325 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
326 char phandle_str[16] = "";
327
328 np = of_find_node_by_path(full_name);
329 if (!np) {
330 unittest(np, "testcase data missing\n");
331 return;
332 }
333
334 num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
335
336 of_unittest_printf_one(np, "%pOF", full_name);
337 of_unittest_printf_one(np, "%pOFf", full_name);
338 of_unittest_printf_one(np, "%pOFn", "dev");
339 of_unittest_printf_one(np, "%2pOFn", "dev");
340 of_unittest_printf_one(np, "%5pOFn", " dev");
341 of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
342 of_unittest_printf_one(np, "%pOFp", phandle_str);
343 of_unittest_printf_one(np, "%pOFP", "dev@100");
344 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
345 of_unittest_printf_one(np, "%10pOFP", " dev@100");
346 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
347 of_unittest_printf_one(of_root, "%pOFP", "/");
348 of_unittest_printf_one(np, "%pOFF", "----");
349 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
350 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
351 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
352 of_unittest_printf_one(np, "%pOFC",
353 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
354 }
355
356 struct node_hash {
357 struct hlist_node node;
358 struct device_node *np;
359 };
360
361 static DEFINE_HASHTABLE(phandle_ht, 8);
of_unittest_check_phandles(void)362 static void __init of_unittest_check_phandles(void)
363 {
364 struct device_node *np;
365 struct node_hash *nh;
366 struct hlist_node *tmp;
367 int i, dup_count = 0, phandle_count = 0;
368
369 for_each_of_allnodes(np) {
370 if (!np->phandle)
371 continue;
372
373 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
374 if (nh->np->phandle == np->phandle) {
375 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
376 np->phandle, nh->np, np);
377 dup_count++;
378 break;
379 }
380 }
381
382 nh = kzalloc_obj(*nh, GFP_KERNEL);
383 if (!nh)
384 return;
385
386 nh->np = np;
387 hash_add(phandle_ht, &nh->node, np->phandle);
388 phandle_count++;
389 }
390 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
391 dup_count, phandle_count);
392
393 /* Clean up */
394 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
395 hash_del(&nh->node);
396 kfree(nh);
397 }
398 }
399
of_unittest_parse_phandle_with_args(void)400 static void __init of_unittest_parse_phandle_with_args(void)
401 {
402 struct device_node *np;
403 struct of_phandle_args args;
404 int i, rc;
405
406 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
407 if (!np) {
408 pr_err("missing testcase data\n");
409 return;
410 }
411
412 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
413 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
414
415 for (i = 0; i < 8; i++) {
416 bool passed = true;
417
418 memset(&args, 0, sizeof(args));
419 rc = of_parse_phandle_with_args(np, "phandle-list",
420 "#phandle-cells", i, &args);
421
422 /* Test the values from tests-phandle.dtsi */
423 switch (i) {
424 case 0:
425 passed &= !rc;
426 passed &= (args.args_count == 1);
427 passed &= (args.args[0] == (i + 1));
428 break;
429 case 1:
430 passed &= !rc;
431 passed &= (args.args_count == 2);
432 passed &= (args.args[0] == (i + 1));
433 passed &= (args.args[1] == 0);
434 break;
435 case 2:
436 passed &= (rc == -ENOENT);
437 break;
438 case 3:
439 passed &= !rc;
440 passed &= (args.args_count == 3);
441 passed &= (args.args[0] == (i + 1));
442 passed &= (args.args[1] == 4);
443 passed &= (args.args[2] == 3);
444 break;
445 case 4:
446 passed &= !rc;
447 passed &= (args.args_count == 2);
448 passed &= (args.args[0] == (i + 1));
449 passed &= (args.args[1] == 100);
450 break;
451 case 5:
452 passed &= !rc;
453 passed &= (args.args_count == 0);
454 break;
455 case 6:
456 passed &= !rc;
457 passed &= (args.args_count == 1);
458 passed &= (args.args[0] == (i + 1));
459 break;
460 case 7:
461 passed &= (rc == -ENOENT);
462 break;
463 default:
464 passed = false;
465 }
466
467 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
468 i, args.np, rc);
469
470 if (rc == 0)
471 of_node_put(args.np);
472 }
473
474 /* Check for missing list property */
475 memset(&args, 0, sizeof(args));
476 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
477 "#phandle-cells", 0, &args);
478 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
479 rc = of_count_phandle_with_args(np, "phandle-list-missing",
480 "#phandle-cells");
481 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
482
483 /* Check for missing cells property */
484 memset(&args, 0, sizeof(args));
485
486 EXPECT_BEGIN(KERN_INFO,
487 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
488
489 rc = of_parse_phandle_with_args(np, "phandle-list",
490 "#phandle-cells-missing", 0, &args);
491
492 EXPECT_END(KERN_INFO,
493 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
494
495 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
496
497 EXPECT_BEGIN(KERN_INFO,
498 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
499
500 rc = of_count_phandle_with_args(np, "phandle-list",
501 "#phandle-cells-missing");
502
503 EXPECT_END(KERN_INFO,
504 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
505
506 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
507
508 /* Check for bad phandle in list */
509 memset(&args, 0, sizeof(args));
510
511 EXPECT_BEGIN(KERN_INFO,
512 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
513
514 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
515 "#phandle-cells", 0, &args);
516
517 EXPECT_END(KERN_INFO,
518 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
519
520 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
521
522 EXPECT_BEGIN(KERN_INFO,
523 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
524
525 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
526 "#phandle-cells");
527
528 EXPECT_END(KERN_INFO,
529 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
530
531 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
532
533 /* Check for incorrectly formed argument list */
534 memset(&args, 0, sizeof(args));
535
536 EXPECT_BEGIN(KERN_INFO,
537 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
538
539 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
540 "#phandle-cells", 1, &args);
541
542 EXPECT_END(KERN_INFO,
543 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
544
545 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
546
547 EXPECT_BEGIN(KERN_INFO,
548 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
549
550 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
551 "#phandle-cells");
552
553 EXPECT_END(KERN_INFO,
554 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
555
556 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
557 }
558
of_unittest_parse_phandle_with_args_map(void)559 static void __init of_unittest_parse_phandle_with_args_map(void)
560 {
561 struct device_node *np, *p[6] = {};
562 struct of_phandle_args args;
563 unsigned int prefs[6];
564 int i, rc;
565
566 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
567 if (!np) {
568 pr_err("missing testcase data\n");
569 return;
570 }
571
572 p[0] = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
573 p[1] = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
574 p[2] = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
575 p[3] = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
576 p[4] = of_find_node_by_path("/testcase-data/phandle-tests/provider4");
577 p[5] = of_find_node_by_path("/testcase-data/phandle-tests/provider5");
578 for (i = 0; i < ARRAY_SIZE(p); ++i) {
579 if (!p[i]) {
580 pr_err("missing testcase data\n");
581 return;
582 }
583 prefs[i] = OF_KREF_READ(p[i]);
584 }
585
586 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
587 unittest(rc == 8, "of_count_phandle_with_args() returned %i, expected 8\n", rc);
588
589 for (i = 0; i < 9; i++) {
590 bool passed = true;
591
592 memset(&args, 0, sizeof(args));
593 rc = of_parse_phandle_with_args_map(np, "phandle-list",
594 "phandle", i, &args);
595
596 /* Test the values from tests-phandle.dtsi */
597 switch (i) {
598 case 0:
599 passed &= !rc;
600 passed &= (args.np == p[1]);
601 passed &= (args.args_count == 1);
602 passed &= (args.args[0] == 1);
603 break;
604 case 1:
605 passed &= !rc;
606 passed &= (args.np == p[3]);
607 passed &= (args.args_count == 3);
608 passed &= (args.args[0] == 2);
609 passed &= (args.args[1] == 5);
610 passed &= (args.args[2] == 3);
611 break;
612 case 2:
613 passed &= (rc == -ENOENT);
614 break;
615 case 3:
616 passed &= !rc;
617 passed &= (args.np == p[0]);
618 passed &= (args.args_count == 0);
619 break;
620 case 4:
621 passed &= !rc;
622 passed &= (args.np == p[1]);
623 passed &= (args.args_count == 1);
624 passed &= (args.args[0] == 3);
625 break;
626 case 5:
627 passed &= !rc;
628 passed &= (args.np == p[0]);
629 passed &= (args.args_count == 0);
630 break;
631 case 6:
632 passed &= !rc;
633 passed &= (args.np == p[2]);
634 passed &= (args.args_count == 2);
635 passed &= (args.args[0] == 15);
636 passed &= (args.args[1] == 0x20);
637 break;
638 case 7:
639 passed &= !rc;
640 passed &= (args.np == p[3]);
641 passed &= (args.args_count == 3);
642 passed &= (args.args[0] == 2);
643 passed &= (args.args[1] == 5);
644 passed &= (args.args[2] == 3);
645 break;
646 case 8:
647 passed &= (rc == -ENOENT);
648 break;
649 default:
650 passed = false;
651 }
652
653 unittest(passed, "index %i - data error on node %s rc=%i\n",
654 i, args.np->full_name, rc);
655
656 if (rc == 0)
657 of_node_put(args.np);
658 }
659
660 /* Check for missing list property */
661 memset(&args, 0, sizeof(args));
662 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
663 "phandle", 0, &args);
664 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
665
666 /* Check for missing cells,map,mask property */
667 memset(&args, 0, sizeof(args));
668
669 EXPECT_BEGIN(KERN_INFO,
670 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
671
672 rc = of_parse_phandle_with_args_map(np, "phandle-list",
673 "phandle-missing", 0, &args);
674 EXPECT_END(KERN_INFO,
675 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
676
677 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
678
679 /* Check for bad phandle in list */
680 memset(&args, 0, sizeof(args));
681
682 EXPECT_BEGIN(KERN_INFO,
683 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
684
685 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
686 "phandle", 0, &args);
687 EXPECT_END(KERN_INFO,
688 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
689
690 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
691
692 /* Check for incorrectly formed argument list */
693 memset(&args, 0, sizeof(args));
694
695 EXPECT_BEGIN(KERN_INFO,
696 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
697
698 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
699 "phandle", 1, &args);
700 EXPECT_END(KERN_INFO,
701 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
702
703 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
704
705 for (i = 0; i < ARRAY_SIZE(p); ++i) {
706 unittest(prefs[i] == OF_KREF_READ(p[i]),
707 "provider%d: expected:%d got:%d\n",
708 i, prefs[i], OF_KREF_READ(p[i]));
709 of_node_put(p[i]);
710 }
711 }
712
of_unittest_property_string(void)713 static void __init of_unittest_property_string(void)
714 {
715 const char *strings[4];
716 struct device_node *np;
717 int rc;
718
719 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
720 if (!np) {
721 pr_err("No testcase data in device tree\n");
722 return;
723 }
724
725 rc = of_property_match_string(np, "phandle-list-names", "first");
726 unittest(rc == 0, "first expected:0 got:%i\n", rc);
727 rc = of_property_match_string(np, "phandle-list-names", "second");
728 unittest(rc == 1, "second expected:1 got:%i\n", rc);
729 rc = of_property_match_string(np, "phandle-list-names", "third");
730 unittest(rc == 2, "third expected:2 got:%i\n", rc);
731 rc = of_property_match_string(np, "phandle-list-names", "fourth");
732 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
733 rc = of_property_match_string(np, "missing-property", "blah");
734 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
735 rc = of_property_match_string(np, "empty-property", "blah");
736 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
737 rc = of_property_match_string(np, "unterminated-string", "blah");
738 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
739
740 /* of_property_count_strings() tests */
741 rc = of_property_count_strings(np, "string-property");
742 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
743 rc = of_property_count_strings(np, "phandle-list-names");
744 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
745 rc = of_property_count_strings(np, "unterminated-string");
746 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
747 rc = of_property_count_strings(np, "unterminated-string-list");
748 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
749
750 /* of_property_read_string_index() tests */
751 rc = of_property_read_string_index(np, "string-property", 0, strings);
752 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
753 strings[0] = NULL;
754 rc = of_property_read_string_index(np, "string-property", 1, strings);
755 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
756 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
757 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
758 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
759 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
760 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
761 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
762 strings[0] = NULL;
763 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
764 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
765 strings[0] = NULL;
766 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
767 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
768 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
769 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
770 strings[0] = NULL;
771 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
772 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
773 strings[1] = NULL;
774
775 /* of_property_read_string_array() tests */
776 rc = of_property_read_string_array(np, "string-property", strings, 4);
777 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
778 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
779 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
780 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
781 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
782 /* -- An incorrectly formed string should cause a failure */
783 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
784 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
785 /* -- parsing the correctly formed strings should still work: */
786 strings[2] = NULL;
787 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
788 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
789 strings[1] = NULL;
790 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
791 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
792 }
793
794 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
795 (p1)->value && (p2)->value && \
796 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
797 !strcmp((p1)->name, (p2)->name))
of_unittest_property_copy(void)798 static void __init of_unittest_property_copy(void)
799 {
800 #ifdef CONFIG_OF_DYNAMIC
801 struct property p1 = { .name = "p1", .length = 0, .value = "" };
802 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
803 struct property *new;
804
805 new = __of_prop_dup(&p1, GFP_KERNEL);
806 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
807 if (new)
808 __of_prop_free(new);
809
810 new = __of_prop_dup(&p2, GFP_KERNEL);
811 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
812 if (new)
813 __of_prop_free(new);
814 #endif
815 }
816
of_unittest_changeset(void)817 static void __init of_unittest_changeset(void)
818 {
819 #ifdef CONFIG_OF_DYNAMIC
820 int ret;
821 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
822 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
823 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
824 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
825 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
826 struct property *ppremove;
827 struct device_node *n1, *n2, *n21, *n22, *nchangeset, *nremove, *parent, *np;
828 static const char * const str_array[] = { "str1", "str2", "str3" };
829 const u32 u32_array[] = { 1, 2, 3 };
830 struct of_changeset chgset;
831 const char *propstr = NULL;
832
833 n1 = __of_node_dup(NULL, "n1");
834 unittest(n1, "testcase setup failure\n");
835
836 n2 = __of_node_dup(NULL, "n2");
837 unittest(n2, "testcase setup failure\n");
838
839 n21 = __of_node_dup(NULL, "n21");
840 unittest(n21, "testcase setup failure %p\n", n21);
841
842 nchangeset = of_find_node_by_path("/testcase-data/changeset");
843 nremove = of_get_child_by_name(nchangeset, "node-remove");
844 unittest(nremove, "testcase setup failure\n");
845
846 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
847 unittest(ppadd, "testcase setup failure\n");
848
849 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
850 unittest(ppname_n1, "testcase setup failure\n");
851
852 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
853 unittest(ppname_n2, "testcase setup failure\n");
854
855 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
856 unittest(ppname_n21, "testcase setup failure\n");
857
858 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
859 unittest(ppupdate, "testcase setup failure\n");
860
861 parent = nchangeset;
862 n1->parent = parent;
863 n2->parent = parent;
864 n21->parent = n2;
865
866 ppremove = of_find_property(parent, "prop-remove", NULL);
867 unittest(ppremove, "failed to find removal prop");
868
869 of_changeset_init(&chgset);
870
871 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
872 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
873
874 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
875 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
876
877 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
878 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
879
880 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
881
882 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
883 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
884 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
885 n22 = of_changeset_create_node(&chgset, n2, "n22");
886 unittest(n22, "fail create n22\n");
887 unittest(!of_changeset_add_prop_string(&chgset, n22, "prop-str", "abcd"),
888 "fail add prop prop-str");
889 unittest(!of_changeset_add_prop_string_array(&chgset, n22, "prop-str-array",
890 (const char **)str_array,
891 ARRAY_SIZE(str_array)),
892 "fail add prop prop-str-array");
893 unittest(!of_changeset_add_prop_u32_array(&chgset, n22, "prop-u32-array",
894 u32_array, ARRAY_SIZE(u32_array)),
895 "fail add prop prop-u32-array");
896
897 unittest(!of_changeset_apply(&chgset), "apply failed\n");
898
899 of_node_put(nchangeset);
900
901 /* Make sure node names are constructed correctly */
902 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
903 "'%pOF' not added\n", n21);
904 of_node_put(np);
905 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n22")),
906 "'%pOF' not added\n", n22);
907 of_node_put(np);
908
909 unittest(!of_changeset_revert(&chgset), "revert failed\n");
910
911 unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
912 "'%pOF' still present after revert\n", n21);
913
914 unittest(of_property_present(parent, "prop-remove"),
915 "failed to find removed prop after revert\n");
916
917 ret = of_property_read_string(parent, "prop-update", &propstr);
918 unittest(!ret, "failed to find updated prop after revert\n");
919 if (!ret)
920 unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
921
922 of_changeset_destroy(&chgset);
923
924 of_node_put(n1);
925 of_node_put(n2);
926 of_node_put(n21);
927 of_node_put(n22);
928 #endif
929 }
930
changeset_check_string(struct device_node * np,const char * prop_name,const char * expected_str)931 static void __init __maybe_unused changeset_check_string(struct device_node *np,
932 const char *prop_name,
933 const char *expected_str)
934 {
935 const char *str;
936 int ret;
937
938 ret = of_property_read_string(np, prop_name, &str);
939 if (unittest(ret == 0, "failed to read %s\n", prop_name))
940 return;
941
942 unittest(strcmp(str, expected_str) == 0,
943 "%s value mismatch (read '%s', exp '%s')\n",
944 prop_name, str, expected_str);
945 }
946
changeset_check_string_array(struct device_node * np,const char * prop_name,const char * const * expected_array,unsigned int count)947 static void __init __maybe_unused changeset_check_string_array(struct device_node *np,
948 const char *prop_name,
949 const char * const *expected_array,
950 unsigned int count)
951 {
952 const char *str;
953 unsigned int i;
954 int ret;
955 int cnt;
956
957 cnt = of_property_count_strings(np, prop_name);
958 if (unittest(cnt >= 0, "failed to get %s count\n", prop_name))
959 return;
960
961 if (unittest(cnt == count,
962 "%s count mismatch (read %d, exp %u)\n",
963 prop_name, cnt, count))
964 return;
965
966 for (i = 0; i < count; i++) {
967 ret = of_property_read_string_index(np, prop_name, i, &str);
968 if (unittest(ret == 0, "failed to read %s[%d]\n", prop_name, i))
969 continue;
970
971 unittest(strcmp(str, expected_array[i]) == 0,
972 "%s[%d] value mismatch (read '%s', exp '%s')\n",
973 prop_name, i, str, expected_array[i]);
974 }
975 }
976
changeset_check_u32(struct device_node * np,const char * prop_name,u32 expected_u32)977 static void __init __maybe_unused changeset_check_u32(struct device_node *np,
978 const char *prop_name,
979 u32 expected_u32)
980 {
981 u32 val32;
982 int ret;
983
984 ret = of_property_read_u32(np, prop_name, &val32);
985 if (unittest(ret == 0, "failed to read %s\n", prop_name))
986 return;
987
988 unittest(val32 == expected_u32,
989 "%s value mismatch (read '%u', exp '%u')\n",
990 prop_name, val32, expected_u32);
991 }
992
changeset_check_u32_array(struct device_node * np,const char * prop_name,const u32 * expected_array,unsigned int count)993 static void __init __maybe_unused changeset_check_u32_array(struct device_node *np,
994 const char *prop_name,
995 const u32 *expected_array,
996 unsigned int count)
997 {
998 unsigned int i;
999 u32 val32;
1000 int ret;
1001 int cnt;
1002
1003 cnt = of_property_count_u32_elems(np, prop_name);
1004 if (unittest(cnt >= 0, "failed to get %s count\n", prop_name))
1005 return;
1006
1007 if (unittest(cnt == count,
1008 "%s count mismatch (read %d, exp %u)\n",
1009 prop_name, cnt, count))
1010 return;
1011
1012 for (i = 0; i < count; i++) {
1013 ret = of_property_read_u32_index(np, prop_name, i, &val32);
1014 if (unittest(ret == 0, "failed to read %s[%d]\n", prop_name, i))
1015 continue;
1016
1017 unittest(val32 == expected_array[i],
1018 "%s[%d] value mismatch (read '%u', exp '%u')\n",
1019 prop_name, i, val32, expected_array[i]);
1020 }
1021 }
1022
changeset_check_bool(struct device_node * np,const char * prop_name)1023 static void __init __maybe_unused changeset_check_bool(struct device_node *np,
1024 const char *prop_name)
1025 {
1026 unittest(of_property_read_bool(np, prop_name),
1027 "%s value mismatch (read 'false', exp 'true')\n", prop_name);
1028 }
1029
of_unittest_changeset_prop(void)1030 static void __init of_unittest_changeset_prop(void)
1031 {
1032 #ifdef CONFIG_OF_DYNAMIC
1033 static const char * const str_array[] = { "abc", "defg", "hij" };
1034 static const u32 u32_array[] = { 123, 4567, 89, 10, 11 };
1035 struct device_node *nchangeset, *np;
1036 struct of_changeset chgset;
1037 int ret;
1038
1039 nchangeset = of_find_node_by_path("/testcase-data/changeset");
1040 if (!nchangeset) {
1041 pr_err("missing testcase data\n");
1042 return;
1043 }
1044
1045 of_changeset_init(&chgset);
1046
1047 np = of_changeset_create_node(&chgset, nchangeset, "test-prop");
1048 if (unittest(np, "failed to create test-prop node\n"))
1049 goto end_changeset_destroy;
1050
1051 ret = of_changeset_add_prop_string(&chgset, np, "prop-string", "abcde");
1052 unittest(ret == 0, "failed to add prop-string\n");
1053
1054 ret = of_changeset_add_prop_string_array(&chgset, np, "prop-string-array",
1055 str_array, ARRAY_SIZE(str_array));
1056 unittest(ret == 0, "failed to add prop-string-array\n");
1057
1058 ret = of_changeset_add_prop_u32(&chgset, np, "prop-u32", 1234);
1059 unittest(ret == 0, "failed to add prop-u32\n");
1060
1061 ret = of_changeset_add_prop_u32_array(&chgset, np, "prop-u32-array",
1062 u32_array, ARRAY_SIZE(u32_array));
1063 unittest(ret == 0, "failed to add prop-u32-array\n");
1064
1065 ret = of_changeset_add_prop_bool(&chgset, np, "prop-bool");
1066 unittest(ret == 0, "failed to add prop-bool\n");
1067
1068 of_node_put(np);
1069
1070 ret = of_changeset_apply(&chgset);
1071 if (unittest(ret == 0, "failed to apply changeset\n"))
1072 goto end_changeset_destroy;
1073
1074 np = of_find_node_by_path("/testcase-data/changeset/test-prop");
1075 if (unittest(np, "failed to find test-prop node\n"))
1076 goto end_revert_changeset;
1077
1078 changeset_check_string(np, "prop-string", "abcde");
1079 changeset_check_string_array(np, "prop-string-array", str_array, ARRAY_SIZE(str_array));
1080 changeset_check_u32(np, "prop-u32", 1234);
1081 changeset_check_u32_array(np, "prop-u32-array", u32_array, ARRAY_SIZE(u32_array));
1082 changeset_check_bool(np, "prop-bool");
1083
1084 of_node_put(np);
1085
1086 end_revert_changeset:
1087 ret = of_changeset_revert(&chgset);
1088 unittest(ret == 0, "failed to revert changeset\n");
1089
1090 end_changeset_destroy:
1091 of_changeset_destroy(&chgset);
1092 of_node_put(nchangeset);
1093 #endif
1094 }
1095
of_unittest_dma_get_max_cpu_address(void)1096 static void __init of_unittest_dma_get_max_cpu_address(void)
1097 {
1098 struct device_node *np;
1099 phys_addr_t cpu_addr;
1100
1101 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
1102 return;
1103
1104 np = of_find_node_by_path("/testcase-data/address-tests");
1105 if (!np) {
1106 pr_err("missing testcase data\n");
1107 return;
1108 }
1109
1110 cpu_addr = of_dma_get_max_cpu_address(np);
1111 unittest(cpu_addr == 0x4fffffff,
1112 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
1113 &cpu_addr, 0x4fffffff);
1114 }
1115
of_unittest_dma_ranges_one(const char * path,u64 expect_dma_addr,u64 expect_paddr)1116 static void __init of_unittest_dma_ranges_one(const char *path,
1117 u64 expect_dma_addr, u64 expect_paddr)
1118 {
1119 #ifdef CONFIG_HAS_DMA
1120 struct device_node *np;
1121 const struct bus_dma_region *map = NULL;
1122 int rc;
1123
1124 np = of_find_node_by_path(path);
1125 if (!np) {
1126 pr_err("missing testcase data\n");
1127 return;
1128 }
1129
1130 rc = of_dma_get_range(np, &map);
1131
1132 unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
1133
1134 if (!rc) {
1135 phys_addr_t paddr;
1136 dma_addr_t dma_addr;
1137 struct device *dev_bogus;
1138
1139 dev_bogus = kzalloc_obj(struct device, GFP_KERNEL);
1140 if (!dev_bogus) {
1141 unittest(0, "kzalloc() failed\n");
1142 kfree(map);
1143 return;
1144 }
1145
1146 dev_bogus->dma_range_map = map;
1147 paddr = dma_to_phys(dev_bogus, expect_dma_addr);
1148 dma_addr = phys_to_dma(dev_bogus, expect_paddr);
1149
1150 unittest(paddr == expect_paddr,
1151 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
1152 &paddr, expect_paddr, np);
1153 unittest(dma_addr == expect_dma_addr,
1154 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
1155 &dma_addr, expect_dma_addr, np);
1156
1157 kfree(map);
1158 kfree(dev_bogus);
1159 }
1160 of_node_put(np);
1161 #endif
1162 }
1163
of_unittest_parse_dma_ranges(void)1164 static void __init of_unittest_parse_dma_ranges(void)
1165 {
1166 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
1167 0x0, 0x20000000);
1168 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
1169 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
1170 0x100000000, 0x20000000);
1171 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
1172 0x80000000, 0x20000000);
1173 }
1174
of_unittest_pci_dma_ranges(void)1175 static void __init of_unittest_pci_dma_ranges(void)
1176 {
1177 struct device_node *np;
1178 struct of_pci_range range;
1179 struct of_pci_range_parser parser;
1180 int i = 0;
1181
1182 if (!IS_ENABLED(CONFIG_PCI))
1183 return;
1184
1185 np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
1186 if (!np) {
1187 pr_err("missing testcase data\n");
1188 return;
1189 }
1190
1191 if (of_pci_dma_range_parser_init(&parser, np)) {
1192 pr_err("missing dma-ranges property\n");
1193 return;
1194 }
1195
1196 /*
1197 * Get the dma-ranges from the device tree
1198 */
1199 for_each_of_pci_range(&parser, &range) {
1200 if (!i) {
1201 unittest(range.size == 0x10000000,
1202 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1203 np, range.size);
1204 unittest(range.cpu_addr == 0x20000000,
1205 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1206 range.cpu_addr, np);
1207 unittest(range.pci_addr == 0x80000000,
1208 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1209 range.pci_addr, np);
1210 } else {
1211 unittest(range.size == 0x10000000,
1212 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1213 np, range.size);
1214 unittest(range.cpu_addr == 0x40000000,
1215 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1216 range.cpu_addr, np);
1217 unittest(range.pci_addr == 0xc0000000,
1218 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1219 range.pci_addr, np);
1220 }
1221 i++;
1222 }
1223
1224 of_node_put(np);
1225 }
1226
of_unittest_pci_empty_dma_ranges(void)1227 static void __init of_unittest_pci_empty_dma_ranges(void)
1228 {
1229 struct device_node *np;
1230 struct of_pci_range range;
1231 struct of_pci_range_parser parser;
1232
1233 if (!IS_ENABLED(CONFIG_PCI))
1234 return;
1235
1236 np = of_find_node_by_path("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0");
1237 if (!np) {
1238 pr_err("missing testcase data\n");
1239 return;
1240 }
1241
1242 if (of_pci_dma_range_parser_init(&parser, np)) {
1243 pr_err("missing dma-ranges property\n");
1244 return;
1245 }
1246
1247 /*
1248 * Get the dma-ranges from the device tree
1249 */
1250 for_each_of_pci_range(&parser, &range) {
1251 unittest(range.size == 0x10000000,
1252 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1253 np, range.size);
1254 unittest(range.cpu_addr == 0x00000000,
1255 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1256 range.cpu_addr, np);
1257 unittest(range.pci_addr == 0xc0000000,
1258 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1259 range.pci_addr, np);
1260 }
1261
1262 of_node_put(np);
1263 }
1264
of_unittest_bus_ranges(void)1265 static void __init of_unittest_bus_ranges(void)
1266 {
1267 struct device_node *np;
1268 struct of_range range;
1269 struct of_range_parser parser;
1270 struct resource res;
1271 int ret, count, i = 0;
1272
1273 np = of_find_node_by_path("/testcase-data/address-tests");
1274 if (!np) {
1275 pr_err("missing testcase data\n");
1276 return;
1277 }
1278
1279 if (of_range_parser_init(&parser, np)) {
1280 pr_err("missing ranges property\n");
1281 return;
1282 }
1283
1284 ret = of_range_to_resource(np, 1, &res);
1285 unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1286 ret, np);
1287 unittest(resource_type(&res) == IORESOURCE_MEM,
1288 "of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1289 np, &res);
1290 unittest(res.start == 0xd0000000,
1291 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1292 np, &res);
1293 unittest(resource_size(&res) == 0x20000000,
1294 "of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1295 np, &res);
1296
1297 count = of_range_count(&parser);
1298 unittest(count == 2,
1299 "of_range_count wrong size on node %pOF count=%d\n",
1300 np, count);
1301
1302 /*
1303 * Get the "ranges" from the device tree
1304 */
1305 for_each_of_range(&parser, &range) {
1306 unittest(range.flags == IORESOURCE_MEM,
1307 "for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1308 np, range.flags, IORESOURCE_MEM);
1309 if (!i) {
1310 unittest(range.size == 0x50000000,
1311 "for_each_of_range wrong size on node %pOF size=%llx\n",
1312 np, range.size);
1313 unittest(range.cpu_addr == 0x70000000,
1314 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1315 range.cpu_addr, np);
1316 unittest(range.bus_addr == 0x70000000,
1317 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1318 range.pci_addr, np);
1319 } else {
1320 unittest(range.size == 0x20000000,
1321 "for_each_of_range wrong size on node %pOF size=%llx\n",
1322 np, range.size);
1323 unittest(range.cpu_addr == 0xd0000000,
1324 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1325 range.cpu_addr, np);
1326 unittest(range.bus_addr == 0x00000000,
1327 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1328 range.pci_addr, np);
1329 }
1330 i++;
1331 }
1332
1333 of_node_put(np);
1334 }
1335
of_unittest_bus_3cell_ranges(void)1336 static void __init of_unittest_bus_3cell_ranges(void)
1337 {
1338 struct device_node *np;
1339 struct of_range range;
1340 struct of_range_parser parser;
1341 int i = 0;
1342
1343 np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1344 if (!np) {
1345 pr_err("missing testcase data\n");
1346 return;
1347 }
1348
1349 if (of_range_parser_init(&parser, np)) {
1350 pr_err("missing ranges property\n");
1351 return;
1352 }
1353
1354 /*
1355 * Get the "ranges" from the device tree
1356 */
1357 for_each_of_range(&parser, &range) {
1358 if (!i) {
1359 unittest(range.flags == 0xf00baa,
1360 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1361 np, range.flags);
1362 unittest(range.size == 0x100000,
1363 "for_each_of_range wrong size on node %pOF size=%llx\n",
1364 np, range.size);
1365 unittest(range.cpu_addr == 0xa0000000,
1366 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1367 range.cpu_addr, np);
1368 unittest(range.bus_addr == 0x0,
1369 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1370 range.pci_addr, np);
1371 } else {
1372 unittest(range.flags == 0xf00bee,
1373 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1374 np, range.flags);
1375 unittest(range.size == 0x200000,
1376 "for_each_of_range wrong size on node %pOF size=%llx\n",
1377 np, range.size);
1378 unittest(range.cpu_addr == 0xb0000000,
1379 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1380 range.cpu_addr, np);
1381 unittest(range.bus_addr == 0x100000000,
1382 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1383 range.pci_addr, np);
1384 }
1385 i++;
1386 }
1387
1388 of_node_put(np);
1389 }
1390
of_unittest_reg(void)1391 static void __init of_unittest_reg(void)
1392 {
1393 struct device_node *np;
1394 struct resource res;
1395 int ret;
1396 u64 addr, size;
1397
1398 np = of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
1399 if (!np) {
1400 pr_err("missing testcase data\n");
1401 return;
1402 }
1403
1404 ret = of_property_read_reg(np, 0, &addr, &size);
1405 unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
1406 np, ret);
1407 unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
1408 np, addr);
1409
1410 of_node_put(np);
1411
1412 np = of_find_node_by_path("/testcase-data/platform-tests-2/node/test-device@100");
1413 if (!np) {
1414 pr_err("missing testcase data\n");
1415 return;
1416 }
1417
1418 ret = of_address_to_resource(np, 0, &res);
1419 unittest(ret == -EINVAL, "of_address_to_resource(%pOF) expected error on untranslatable address\n",
1420 np);
1421
1422 of_node_put(np);
1423
1424 }
1425
1426 struct of_unittest_expected_res {
1427 int index;
1428 struct resource res;
1429 };
1430
of_unittest_check_addr(const char * node_path,const struct of_unittest_expected_res * tab_exp,unsigned int tab_exp_count)1431 static void __init of_unittest_check_addr(const char *node_path,
1432 const struct of_unittest_expected_res *tab_exp,
1433 unsigned int tab_exp_count)
1434 {
1435 const struct of_unittest_expected_res *expected;
1436 struct device_node *np;
1437 struct resource res;
1438 unsigned int count;
1439 int ret;
1440
1441 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
1442 return;
1443
1444 np = of_find_node_by_path(node_path);
1445 if (!np) {
1446 pr_err("missing testcase data (%s)\n", node_path);
1447 return;
1448 }
1449
1450 expected = tab_exp;
1451 count = tab_exp_count;
1452 while (count--) {
1453 ret = of_address_to_resource(np, expected->index, &res);
1454 unittest(!ret, "of_address_to_resource(%pOF, %d) returned error %d\n",
1455 np, expected->index, ret);
1456 unittest(resource_type(&res) == resource_type(&expected->res) &&
1457 res.start == expected->res.start &&
1458 resource_size(&res) == resource_size(&expected->res),
1459 "of_address_to_resource(%pOF, %d) wrong resource %pR, expected %pR\n",
1460 np, expected->index, &res, &expected->res);
1461 expected++;
1462 }
1463
1464 of_node_put(np);
1465 }
1466
1467 static const struct of_unittest_expected_res of_unittest_reg_2cell_expected_res[] = {
1468 {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1469 {.index = 1, .res = DEFINE_RES_MEM(0xa0a02000, 0x100) },
1470 {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1471 {.index = 3, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1472 };
1473
1474 static const struct of_unittest_expected_res of_unittest_reg_3cell_expected_res[] = {
1475 {.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1476 {.index = 1, .res = DEFINE_RES_MEM(0xa0b02000, 0x100) },
1477 {.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1478 {.index = 3, .res = DEFINE_RES_MEM(0xc0c09000, 0x100) },
1479 {.index = 4, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1480 };
1481
1482 static const struct of_unittest_expected_res of_unittest_reg_pci_expected_res[] = {
1483 {.index = 0, .res = DEFINE_RES_MEM(0xe8001000, 0x1000) },
1484 {.index = 1, .res = DEFINE_RES_MEM(0xea002000, 0x2000) },
1485 };
1486
of_unittest_translate_addr(void)1487 static void __init of_unittest_translate_addr(void)
1488 {
1489 of_unittest_check_addr("/testcase-data/address-tests2/bus-2cell@10000000/device@100000",
1490 of_unittest_reg_2cell_expected_res,
1491 ARRAY_SIZE(of_unittest_reg_2cell_expected_res));
1492
1493 of_unittest_check_addr("/testcase-data/address-tests2/bus-3cell@20000000/local-bus@100000/device@f1001000",
1494 of_unittest_reg_3cell_expected_res,
1495 ARRAY_SIZE(of_unittest_reg_3cell_expected_res));
1496
1497 of_unittest_check_addr("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0/dev@e0000000",
1498 of_unittest_reg_pci_expected_res,
1499 ARRAY_SIZE(of_unittest_reg_pci_expected_res));
1500 }
1501
of_unittest_parse_interrupts(void)1502 static void __init of_unittest_parse_interrupts(void)
1503 {
1504 struct device_node *np;
1505 struct of_phandle_args args;
1506 int i, rc;
1507
1508 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1509 return;
1510
1511 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1512 if (!np) {
1513 pr_err("missing testcase data\n");
1514 return;
1515 }
1516
1517 for (i = 0; i < 4; i++) {
1518 bool passed = true;
1519
1520 memset(&args, 0, sizeof(args));
1521 rc = of_irq_parse_one(np, i, &args);
1522
1523 passed &= !rc;
1524 passed &= (args.args_count == 1);
1525 passed &= (args.args[0] == (i + 1));
1526
1527 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1528 i, args.np, rc);
1529 }
1530 of_node_put(np);
1531
1532 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1533 if (!np) {
1534 pr_err("missing testcase data\n");
1535 return;
1536 }
1537
1538 for (i = 0; i < 4; i++) {
1539 bool passed = true;
1540
1541 memset(&args, 0, sizeof(args));
1542 rc = of_irq_parse_one(np, i, &args);
1543
1544 /* Test the values from tests-phandle.dtsi */
1545 switch (i) {
1546 case 0:
1547 passed &= !rc;
1548 passed &= (args.args_count == 1);
1549 passed &= (args.args[0] == 9);
1550 break;
1551 case 1:
1552 passed &= !rc;
1553 passed &= (args.args_count == 3);
1554 passed &= (args.args[0] == 10);
1555 passed &= (args.args[1] == 11);
1556 passed &= (args.args[2] == 12);
1557 break;
1558 case 2:
1559 passed &= !rc;
1560 passed &= (args.args_count == 2);
1561 passed &= (args.args[0] == 13);
1562 passed &= (args.args[1] == 14);
1563 break;
1564 case 3:
1565 passed &= !rc;
1566 passed &= (args.args_count == 2);
1567 passed &= (args.args[0] == 15);
1568 passed &= (args.args[1] == 16);
1569 break;
1570 default:
1571 passed = false;
1572 }
1573 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1574 i, args.np, rc);
1575 }
1576 of_node_put(np);
1577 }
1578
of_unittest_parse_interrupts_extended(void)1579 static void __init of_unittest_parse_interrupts_extended(void)
1580 {
1581 struct device_node *np;
1582 struct of_phandle_args args;
1583 int i, rc;
1584
1585 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1586 return;
1587
1588 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1589 if (!np) {
1590 pr_err("missing testcase data\n");
1591 return;
1592 }
1593
1594 for (i = 0; i < 7; i++) {
1595 bool passed = true;
1596
1597 memset(&args, 0, sizeof(args));
1598 rc = of_irq_parse_one(np, i, &args);
1599
1600 /* Test the values from tests-phandle.dtsi */
1601 switch (i) {
1602 case 0:
1603 passed &= !rc;
1604 passed &= (args.args_count == 1);
1605 passed &= (args.args[0] == 1);
1606 break;
1607 case 1:
1608 passed &= !rc;
1609 passed &= (args.args_count == 3);
1610 passed &= (args.args[0] == 2);
1611 passed &= (args.args[1] == 3);
1612 passed &= (args.args[2] == 4);
1613 break;
1614 case 2:
1615 passed &= !rc;
1616 passed &= (args.args_count == 2);
1617 passed &= (args.args[0] == 5);
1618 passed &= (args.args[1] == 6);
1619 break;
1620 case 3:
1621 passed &= !rc;
1622 passed &= (args.args_count == 1);
1623 passed &= (args.args[0] == 9);
1624 break;
1625 case 4:
1626 passed &= !rc;
1627 passed &= (args.args_count == 3);
1628 passed &= (args.args[0] == 10);
1629 passed &= (args.args[1] == 11);
1630 passed &= (args.args[2] == 12);
1631 break;
1632 case 5:
1633 passed &= !rc;
1634 passed &= (args.args_count == 2);
1635 passed &= (args.args[0] == 13);
1636 passed &= (args.args[1] == 14);
1637 break;
1638 case 6:
1639 /*
1640 * Tests child node that is missing property
1641 * #address-cells. See the comments in
1642 * drivers/of/unittest-data/tests-interrupts.dtsi
1643 * nodes intmap1 and interrupts-extended0
1644 */
1645 passed &= !rc;
1646 passed &= (args.args_count == 1);
1647 passed &= (args.args[0] == 15);
1648 break;
1649 default:
1650 passed = false;
1651 }
1652
1653 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1654 i, args.np, rc);
1655 }
1656 of_node_put(np);
1657 }
1658
1659 struct of_unittest_expected_imap_item {
1660 u32 child_imap_count;
1661 u32 child_imap[2];
1662 const char *parent_path;
1663 int parent_args_count;
1664 u32 parent_args[3];
1665 };
1666
1667 static const struct of_unittest_expected_imap_item of_unittest_expected_imap_items[] = {
1668 {
1669 .child_imap_count = 2,
1670 .child_imap = {1, 11},
1671 .parent_path = "/testcase-data/interrupts/intc0",
1672 .parent_args_count = 1,
1673 .parent_args = {100},
1674 }, {
1675 .child_imap_count = 2,
1676 .child_imap = {2, 22},
1677 .parent_path = "/testcase-data/interrupts/intc1",
1678 .parent_args_count = 3,
1679 .parent_args = {200, 201, 202},
1680 }, {
1681 .child_imap_count = 2,
1682 .child_imap = {3, 33},
1683 .parent_path = "/testcase-data/interrupts/intc2",
1684 .parent_args_count = 2,
1685 .parent_args = {300, 301},
1686 }, {
1687 .child_imap_count = 2,
1688 .child_imap = {4, 44},
1689 .parent_path = "/testcase-data/interrupts/intc2",
1690 .parent_args_count = 2,
1691 .parent_args = {400, 401},
1692 }
1693 };
1694
of_unittest_parse_interrupt_map(void)1695 static void __init of_unittest_parse_interrupt_map(void)
1696 {
1697 const struct of_unittest_expected_imap_item *expected_item;
1698 struct device_node *imap_np, *expected_parent_np;
1699 struct of_imap_parser imap_parser;
1700 struct of_imap_item imap_item;
1701 int count, ret, i;
1702
1703 if (of_irq_workarounds & (OF_IMAP_NO_PHANDLE | OF_IMAP_OLDWORLD_MAC))
1704 return;
1705
1706 imap_np = of_find_node_by_path("/testcase-data/interrupts/intmap2");
1707 if (!imap_np) {
1708 pr_err("missing testcase data\n");
1709 return;
1710 }
1711
1712 ret = of_imap_parser_init(&imap_parser, imap_np, &imap_item);
1713 if (unittest(!ret, "of_imap_parser_init(%pOF) returned error %d\n",
1714 imap_np, ret))
1715 goto end;
1716
1717 expected_item = of_unittest_expected_imap_items;
1718 count = 0;
1719
1720 for_each_of_imap_item(&imap_parser, &imap_item) {
1721 if (unittest(count < ARRAY_SIZE(of_unittest_expected_imap_items),
1722 "imap item number %d not expected. Max number %zu\n",
1723 count, ARRAY_SIZE(of_unittest_expected_imap_items) - 1)) {
1724 of_node_put(imap_item.parent_args.np);
1725 goto end;
1726 }
1727
1728 expected_parent_np = of_find_node_by_path(expected_item->parent_path);
1729 if (unittest(expected_parent_np,
1730 "missing dependent testcase data (%s)\n",
1731 expected_item->parent_path)) {
1732 of_node_put(imap_item.parent_args.np);
1733 goto end;
1734 }
1735
1736 unittest(imap_item.child_imap_count == expected_item->child_imap_count,
1737 "imap[%d] child_imap_count = %u, expected %u\n",
1738 count, imap_item.child_imap_count,
1739 expected_item->child_imap_count);
1740
1741 for (i = 0; i < expected_item->child_imap_count; i++)
1742 unittest(imap_item.child_imap[i] == expected_item->child_imap[i],
1743 "imap[%d] child_imap[%d] = %u, expected %u\n",
1744 count, i, imap_item.child_imap[i],
1745 expected_item->child_imap[i]);
1746
1747 unittest(imap_item.parent_args.np == expected_parent_np,
1748 "imap[%d] parent np = %pOF, expected %pOF\n",
1749 count, imap_item.parent_args.np, expected_parent_np);
1750
1751 unittest(imap_item.parent_args.args_count == expected_item->parent_args_count,
1752 "imap[%d] parent param_count = %d, expected %d\n",
1753 count, imap_item.parent_args.args_count,
1754 expected_item->parent_args_count);
1755
1756 for (i = 0; i < expected_item->parent_args_count; i++)
1757 unittest(imap_item.parent_args.args[i] == expected_item->parent_args[i],
1758 "imap[%d] parent param[%d] = %u, expected %u\n",
1759 count, i, imap_item.parent_args.args[i],
1760 expected_item->parent_args[i]);
1761
1762 of_node_put(expected_parent_np);
1763 count++;
1764 expected_item++;
1765 }
1766
1767 unittest(count == ARRAY_SIZE(of_unittest_expected_imap_items),
1768 "Missing items. %d parsed, expected %zu\n",
1769 count, ARRAY_SIZE(of_unittest_expected_imap_items));
1770 end:
1771 of_node_put(imap_np);
1772 }
1773
1774 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
of_unittest_irq_refcount(void)1775 static void __init of_unittest_irq_refcount(void)
1776 {
1777 struct of_phandle_args args;
1778 struct device_node *intc0, *int_ext0;
1779 struct device_node *int2, *intc_intmap0;
1780 unsigned int ref_c0, ref_c1, ref_c2;
1781 int rc;
1782 bool passed;
1783
1784 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1785 return;
1786
1787 intc0 = of_find_node_by_path("/testcase-data/interrupts/intc0");
1788 int_ext0 = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1789 intc_intmap0 = of_find_node_by_path("/testcase-data/interrupts/intc-intmap0");
1790 int2 = of_find_node_by_path("/testcase-data/interrupts/interrupts2");
1791 if (!intc0 || !int_ext0 || !intc_intmap0 || !int2) {
1792 pr_err("missing testcase data\n");
1793 goto out;
1794 }
1795
1796 /* Test refcount for API of_irq_parse_one() */
1797 passed = true;
1798 ref_c0 = OF_KREF_READ(intc0);
1799 ref_c1 = ref_c0 + 1;
1800 memset(&args, 0, sizeof(args));
1801 rc = of_irq_parse_one(int_ext0, 0, &args);
1802 ref_c2 = OF_KREF_READ(intc0);
1803 of_node_put(args.np);
1804
1805 passed &= !rc;
1806 passed &= (args.np == intc0);
1807 passed &= (args.args_count == 1);
1808 passed &= (args.args[0] == 1);
1809 passed &= (ref_c1 == ref_c2);
1810 unittest(passed, "IRQ refcount case #1 failed, original(%u) expected(%u) got(%u)\n",
1811 ref_c0, ref_c1, ref_c2);
1812
1813 /* Test refcount for API of_irq_parse_raw() */
1814 passed = true;
1815 ref_c0 = OF_KREF_READ(intc_intmap0);
1816 ref_c1 = ref_c0 + 1;
1817 memset(&args, 0, sizeof(args));
1818 rc = of_irq_parse_one(int2, 0, &args);
1819 ref_c2 = OF_KREF_READ(intc_intmap0);
1820 of_node_put(args.np);
1821
1822 passed &= !rc;
1823 passed &= (args.np == intc_intmap0);
1824 passed &= (args.args_count == 1);
1825 passed &= (args.args[0] == 2);
1826 passed &= (ref_c1 == ref_c2);
1827 unittest(passed, "IRQ refcount case #2 failed, original(%u) expected(%u) got(%u)\n",
1828 ref_c0, ref_c1, ref_c2);
1829
1830 out:
1831 of_node_put(int2);
1832 of_node_put(intc_intmap0);
1833 of_node_put(int_ext0);
1834 of_node_put(intc0);
1835 }
1836 #else
of_unittest_irq_refcount(void)1837 static inline void __init of_unittest_irq_refcount(void) { }
1838 #endif
1839
1840 static const struct of_device_id match_node_table[] = {
1841 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1842 { .data = "B", .type = "type1", }, /* followed by type alone */
1843
1844 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1845 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1846 { .data = "Cc", .name = "name2", .type = "type2", },
1847
1848 { .data = "E", .compatible = "compat3" },
1849 { .data = "G", .compatible = "compat2", },
1850 { .data = "H", .compatible = "compat2", .name = "name5", },
1851 { .data = "I", .compatible = "compat2", .type = "type1", },
1852 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1853 { .data = "K", .compatible = "compat2", .name = "name9", },
1854 {}
1855 };
1856
1857 static struct {
1858 const char *path;
1859 const char *data;
1860 } match_node_tests[] = {
1861 { .path = "/testcase-data/match-node/name0", .data = "A", },
1862 { .path = "/testcase-data/match-node/name1", .data = "B", },
1863 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1864 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1865 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1866 { .path = "/testcase-data/match-node/name3", .data = "E", },
1867 { .path = "/testcase-data/match-node/name4", .data = "G", },
1868 { .path = "/testcase-data/match-node/name5", .data = "H", },
1869 { .path = "/testcase-data/match-node/name6", .data = "G", },
1870 { .path = "/testcase-data/match-node/name7", .data = "I", },
1871 { .path = "/testcase-data/match-node/name8", .data = "J", },
1872 { .path = "/testcase-data/match-node/name9", .data = "K", },
1873 };
1874
of_unittest_match_node(void)1875 static void __init of_unittest_match_node(void)
1876 {
1877 struct device_node *np;
1878 const struct of_device_id *match;
1879 int i;
1880
1881 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1882 np = of_find_node_by_path(match_node_tests[i].path);
1883 if (!np) {
1884 unittest(0, "missing testcase node %s\n",
1885 match_node_tests[i].path);
1886 continue;
1887 }
1888
1889 match = of_match_node(match_node_table, np);
1890 if (!match) {
1891 unittest(0, "%s didn't match anything\n",
1892 match_node_tests[i].path);
1893 continue;
1894 }
1895
1896 if (strcmp(match->data, match_node_tests[i].data) != 0) {
1897 unittest(0, "%s got wrong match. expected %s, got %s\n",
1898 match_node_tests[i].path, match_node_tests[i].data,
1899 (const char *)match->data);
1900 continue;
1901 }
1902 unittest(1, "passed");
1903 }
1904 }
1905
1906 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1907 static const struct platform_device_info test_bus_info = {
1908 .name = "unittest-bus",
1909 };
of_unittest_platform_populate(void)1910 static void __init of_unittest_platform_populate(void)
1911 {
1912 int irq, rc;
1913 struct device_node *np, *child, *grandchild;
1914 struct platform_device *pdev, *test_bus;
1915 const struct of_device_id match[] = {
1916 { .compatible = "test-device", },
1917 {}
1918 };
1919
1920 np = of_find_node_by_path("/testcase-data");
1921 of_platform_default_populate(np, NULL, NULL);
1922
1923 /* Test that a missing irq domain returns -EPROBE_DEFER */
1924 np = of_find_node_by_path("/testcase-data/testcase-device1");
1925 pdev = of_find_device_by_node(np);
1926 unittest(pdev, "device 1 creation failed\n");
1927
1928 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1929 irq = platform_get_irq(pdev, 0);
1930 unittest(irq == -EPROBE_DEFER,
1931 "device deferred probe failed - %d\n", irq);
1932
1933 /* Test that a parsing failure does not return -EPROBE_DEFER */
1934 np = of_find_node_by_path("/testcase-data/testcase-device2");
1935 pdev = of_find_device_by_node(np);
1936 unittest(pdev, "device 2 creation failed\n");
1937
1938 EXPECT_BEGIN(KERN_INFO,
1939 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1940
1941 irq = platform_get_irq(pdev, 0);
1942
1943 EXPECT_END(KERN_INFO,
1944 "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1945
1946 unittest(irq < 0 && irq != -EPROBE_DEFER,
1947 "device parsing error failed - %d\n", irq);
1948 }
1949
1950 np = of_find_node_by_path("/testcase-data/platform-tests");
1951 unittest(np, "No testcase data in device tree\n");
1952 if (!np)
1953 return;
1954
1955 test_bus = platform_device_register_full(&test_bus_info);
1956 rc = PTR_ERR_OR_ZERO(test_bus);
1957 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1958 if (rc) {
1959 of_node_put(np);
1960 return;
1961 }
1962 test_bus->dev.of_node = np;
1963
1964 /*
1965 * Add a dummy resource to the test bus node after it is
1966 * registered to catch problems with un-inserted resources. The
1967 * DT code doesn't insert the resources, and it has caused the
1968 * kernel to oops in the past. This makes sure the same bug
1969 * doesn't crop up again.
1970 */
1971 platform_device_add_resources(test_bus, &test_bus_res, 1);
1972
1973 of_platform_populate(np, match, NULL, &test_bus->dev);
1974 for_each_child_of_node(np, child) {
1975 for_each_child_of_node(child, grandchild) {
1976 if (!of_property_present(grandchild, "compatible"))
1977 continue;
1978 pdev = of_find_device_by_node(grandchild);
1979 unittest(pdev,
1980 "Could not create device for node '%pOFn'\n",
1981 grandchild);
1982 platform_device_put(pdev);
1983 }
1984 }
1985
1986 of_platform_depopulate(&test_bus->dev);
1987 for_each_child_of_node(np, child) {
1988 for_each_child_of_node(child, grandchild)
1989 unittest(!of_find_device_by_node(grandchild),
1990 "device didn't get destroyed '%pOFn'\n",
1991 grandchild);
1992 }
1993
1994 platform_device_unregister(test_bus);
1995 of_node_put(np);
1996 }
1997
1998 /**
1999 * update_node_properties - adds the properties
2000 * of np into dup node (present in live tree) and
2001 * updates parent of children of np to dup.
2002 *
2003 * @np: node whose properties are being added to the live tree
2004 * @dup: node present in live tree to be updated
2005 */
update_node_properties(struct device_node * np,struct device_node * dup)2006 static void update_node_properties(struct device_node *np,
2007 struct device_node *dup)
2008 {
2009 struct property *prop;
2010 struct property *save_next;
2011 struct device_node *child;
2012 int ret;
2013
2014 for_each_child_of_node(np, child)
2015 child->parent = dup;
2016
2017 /*
2018 * "unittest internal error: unable to add testdata property"
2019 *
2020 * If this message reports a property in node '/__symbols__' then
2021 * the respective unittest overlay contains a label that has the
2022 * same name as a label in the live devicetree. The label will
2023 * be in the live devicetree only if the devicetree source was
2024 * compiled with the '-@' option. If you encounter this error,
2025 * please consider renaming __all__ of the labels in the unittest
2026 * overlay dts files with an odd prefix that is unlikely to be
2027 * used in a real devicetree.
2028 */
2029
2030 /*
2031 * open code for_each_property_of_node() because of_add_property()
2032 * sets prop->next to NULL
2033 */
2034 for (prop = np->properties; prop != NULL; prop = save_next) {
2035 save_next = prop->next;
2036 ret = of_add_property(dup, prop);
2037 if (ret) {
2038 if (ret == -EEXIST && !strcmp(prop->name, "name"))
2039 continue;
2040 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
2041 np, prop->name);
2042 }
2043 }
2044 }
2045
2046 /**
2047 * attach_node_and_children - attaches nodes
2048 * and its children to live tree.
2049 * CAUTION: misleading function name - if node @np already exists in
2050 * the live tree then children of @np are *not* attached to the live
2051 * tree. This works for the current test devicetree nodes because such
2052 * nodes do not have child nodes.
2053 *
2054 * @np: Node to attach to live tree
2055 */
attach_node_and_children(struct device_node * np)2056 static void attach_node_and_children(struct device_node *np)
2057 {
2058 struct device_node *next, *dup, *child;
2059 unsigned long flags;
2060 const char *full_name;
2061
2062 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
2063 if (!full_name)
2064 return;
2065
2066 if (!strcmp(full_name, "/__local_fixups__") ||
2067 !strcmp(full_name, "/__fixups__")) {
2068 kfree(full_name);
2069 return;
2070 }
2071
2072 dup = of_find_node_by_path(full_name);
2073 kfree(full_name);
2074 if (dup) {
2075 update_node_properties(np, dup);
2076 return;
2077 }
2078
2079 child = np->child;
2080 np->child = NULL;
2081
2082 mutex_lock(&of_mutex);
2083 raw_spin_lock_irqsave(&devtree_lock, flags);
2084 np->sibling = np->parent->child;
2085 np->parent->child = np;
2086 of_node_clear_flag(np, OF_DETACHED);
2087 raw_spin_unlock_irqrestore(&devtree_lock, flags);
2088
2089 __of_attach_node_sysfs(np);
2090 mutex_unlock(&of_mutex);
2091
2092 while (child) {
2093 next = child->sibling;
2094 attach_node_and_children(child);
2095 child = next;
2096 }
2097 }
2098
2099 /**
2100 * unittest_data_add - Reads, copies data from
2101 * linked tree and attaches it to the live tree
2102 */
unittest_data_add(void)2103 static int __init unittest_data_add(void)
2104 {
2105 void *unittest_data_align;
2106 struct device_node *unittest_data_node = NULL, *np;
2107 /*
2108 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
2109 * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
2110 */
2111 extern uint8_t __dtbo_testcases_begin[];
2112 extern uint8_t __dtbo_testcases_end[];
2113 const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
2114 int rc;
2115 void *ret;
2116
2117 if (!size) {
2118 pr_warn("%s: testcases is empty\n", __func__);
2119 return -ENODATA;
2120 }
2121
2122 /* creating copy */
2123 void *unittest_data __free(kfree) = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
2124 if (!unittest_data)
2125 return -ENOMEM;
2126
2127 unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
2128 memcpy(unittest_data_align, __dtbo_testcases_begin, size);
2129
2130 ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
2131 if (!ret) {
2132 pr_warn("%s: unflatten testcases tree failed\n", __func__);
2133 return -ENODATA;
2134 }
2135 if (!unittest_data_node) {
2136 pr_warn("%s: testcases tree is empty\n", __func__);
2137 return -ENODATA;
2138 }
2139
2140 /*
2141 * This lock normally encloses of_resolve_phandles()
2142 */
2143 of_overlay_mutex_lock();
2144
2145 rc = of_resolve_phandles(unittest_data_node);
2146 if (rc) {
2147 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
2148 rc = -EINVAL;
2149 goto unlock;
2150 }
2151
2152 /* attach the sub-tree to live tree */
2153 if (!of_root) {
2154 pr_warn("%s: no live tree to attach sub-tree\n", __func__);
2155 rc = -ENODEV;
2156 goto unlock;
2157 }
2158
2159 EXPECT_BEGIN(KERN_INFO,
2160 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
2161
2162 np = unittest_data_node->child;
2163 while (np) {
2164 struct device_node *next = np->sibling;
2165
2166 np->parent = of_root;
2167 /* this will clear OF_DETACHED in np and children */
2168 attach_node_and_children(np);
2169 np = next;
2170 }
2171
2172 EXPECT_END(KERN_INFO,
2173 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
2174
2175 retain_and_null_ptr(unittest_data);
2176
2177 unlock:
2178 of_overlay_mutex_unlock();
2179
2180 return rc;
2181 }
2182
2183 #ifdef CONFIG_OF_OVERLAY
2184 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
2185
unittest_probe(struct platform_device * pdev)2186 static int unittest_probe(struct platform_device *pdev)
2187 {
2188 struct device *dev = &pdev->dev;
2189 struct device_node *np = dev->of_node;
2190
2191 if (np == NULL) {
2192 dev_err(dev, "No OF data for device\n");
2193 return -EINVAL;
2194
2195 }
2196
2197 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2198
2199 of_platform_populate(np, NULL, NULL, &pdev->dev);
2200
2201 return 0;
2202 }
2203
unittest_remove(struct platform_device * pdev)2204 static void unittest_remove(struct platform_device *pdev)
2205 {
2206 struct device *dev = &pdev->dev;
2207 struct device_node *np = dev->of_node;
2208
2209 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2210 }
2211
2212 static const struct of_device_id unittest_match[] = {
2213 { .compatible = "unittest", },
2214 {},
2215 };
2216
2217 static struct platform_driver unittest_driver = {
2218 .probe = unittest_probe,
2219 .remove = unittest_remove,
2220 .driver = {
2221 .name = "unittest",
2222 .of_match_table = unittest_match,
2223 },
2224 };
2225
2226 /* get the platform device instantiated at the path */
of_path_to_platform_device(const char * path)2227 static struct platform_device *of_path_to_platform_device(const char *path)
2228 {
2229 struct device_node *np;
2230 struct platform_device *pdev;
2231
2232 np = of_find_node_by_path(path);
2233 if (np == NULL)
2234 return NULL;
2235
2236 pdev = of_find_device_by_node(np);
2237 of_node_put(np);
2238
2239 return pdev;
2240 }
2241
2242 /* find out if a platform device exists at that path */
of_path_platform_device_exists(const char * path)2243 static int of_path_platform_device_exists(const char *path)
2244 {
2245 struct platform_device *pdev;
2246
2247 pdev = of_path_to_platform_device(path);
2248 platform_device_put(pdev);
2249 return pdev != NULL;
2250 }
2251
2252 #ifdef CONFIG_OF_GPIO
2253
2254 struct unittest_gpio_dev {
2255 struct gpio_chip chip;
2256 };
2257
2258 static int unittest_gpio_chip_request_count;
2259 static int unittest_gpio_probe_count;
2260 static int unittest_gpio_probe_pass_count;
2261
unittest_gpio_chip_request(struct gpio_chip * chip,unsigned int offset)2262 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
2263 {
2264 unittest_gpio_chip_request_count++;
2265
2266 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
2267 unittest_gpio_chip_request_count);
2268 return 0;
2269 }
2270
unittest_gpio_probe(struct platform_device * pdev)2271 static int unittest_gpio_probe(struct platform_device *pdev)
2272 {
2273 struct unittest_gpio_dev *devptr;
2274 int ret;
2275
2276 unittest_gpio_probe_count++;
2277
2278 devptr = kzalloc_obj(*devptr, GFP_KERNEL);
2279 if (!devptr)
2280 return -ENOMEM;
2281
2282 platform_set_drvdata(pdev, devptr);
2283
2284 devptr->chip.fwnode = dev_fwnode(&pdev->dev);
2285 devptr->chip.label = "of-unittest-gpio";
2286 devptr->chip.base = -1; /* dynamic allocation */
2287 devptr->chip.ngpio = 5;
2288 devptr->chip.request = unittest_gpio_chip_request;
2289
2290 ret = gpiochip_add_data(&devptr->chip, NULL);
2291
2292 unittest(!ret,
2293 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
2294
2295 if (!ret)
2296 unittest_gpio_probe_pass_count++;
2297 return ret;
2298 }
2299
unittest_gpio_remove(struct platform_device * pdev)2300 static void unittest_gpio_remove(struct platform_device *pdev)
2301 {
2302 struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
2303 struct device *dev = &pdev->dev;
2304
2305 dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
2306
2307 if (devptr->chip.base != -1)
2308 gpiochip_remove(&devptr->chip);
2309
2310 kfree(devptr);
2311 }
2312
2313 static const struct of_device_id unittest_gpio_id[] = {
2314 { .compatible = "unittest-gpio", },
2315 {}
2316 };
2317
2318 static struct platform_driver unittest_gpio_driver = {
2319 .probe = unittest_gpio_probe,
2320 .remove = unittest_gpio_remove,
2321 .driver = {
2322 .name = "unittest-gpio",
2323 .of_match_table = unittest_gpio_id,
2324 },
2325 };
2326
of_unittest_overlay_gpio(void)2327 static void __init of_unittest_overlay_gpio(void)
2328 {
2329 int chip_request_count;
2330 int probe_pass_count;
2331 int ret;
2332
2333 /*
2334 * tests: apply overlays before registering driver
2335 * Similar to installing a driver as a module, the
2336 * driver is registered after applying the overlays.
2337 *
2338 * The overlays are applied by overlay_data_apply()
2339 * instead of of_unittest_apply_overlay() so that they
2340 * will not be tracked. Thus they will not be removed
2341 * by of_unittest_remove_tracked_overlays().
2342 *
2343 * - apply overlay_gpio_01
2344 * - apply overlay_gpio_02a
2345 * - apply overlay_gpio_02b
2346 * - register driver
2347 *
2348 * register driver will result in
2349 * - probe and processing gpio hog for overlay_gpio_01
2350 * - probe for overlay_gpio_02a
2351 * - processing gpio for overlay_gpio_02b
2352 */
2353
2354 probe_pass_count = unittest_gpio_probe_pass_count;
2355 chip_request_count = unittest_gpio_chip_request_count;
2356
2357 /*
2358 * overlay_gpio_01 contains gpio node and child gpio hog node
2359 * overlay_gpio_02a contains gpio node
2360 * overlay_gpio_02b contains child gpio hog node
2361 */
2362
2363 unittest(overlay_data_apply("overlay_gpio_01", NULL),
2364 "Adding overlay 'overlay_gpio_01' failed\n");
2365
2366 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
2367 "Adding overlay 'overlay_gpio_02a' failed\n");
2368
2369 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
2370 "Adding overlay 'overlay_gpio_02b' failed\n");
2371
2372 ret = platform_driver_register(&unittest_gpio_driver);
2373 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
2374 return;
2375
2376 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
2377 "unittest_gpio_probe() failed or not called\n");
2378
2379 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
2380 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2381 unittest_gpio_chip_request_count - chip_request_count);
2382
2383 /*
2384 * tests: apply overlays after registering driver
2385 *
2386 * Similar to a driver built-in to the kernel, the
2387 * driver is registered before applying the overlays.
2388 *
2389 * overlay_gpio_03 contains gpio node and child gpio hog node
2390 *
2391 * - apply overlay_gpio_03
2392 *
2393 * apply overlay will result in
2394 * - probe and processing gpio hog.
2395 */
2396
2397 probe_pass_count = unittest_gpio_probe_pass_count;
2398 chip_request_count = unittest_gpio_chip_request_count;
2399
2400 /* overlay_gpio_03 contains gpio node and child gpio hog node */
2401
2402 unittest(overlay_data_apply("overlay_gpio_03", NULL),
2403 "Adding overlay 'overlay_gpio_03' failed\n");
2404
2405 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2406 "unittest_gpio_probe() failed or not called\n");
2407
2408 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2409 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2410 unittest_gpio_chip_request_count - chip_request_count);
2411
2412 /*
2413 * overlay_gpio_04a contains gpio node
2414 *
2415 * - apply overlay_gpio_04a
2416 *
2417 * apply the overlay will result in
2418 * - probe for overlay_gpio_04a
2419 */
2420
2421 probe_pass_count = unittest_gpio_probe_pass_count;
2422 chip_request_count = unittest_gpio_chip_request_count;
2423
2424 /* overlay_gpio_04a contains gpio node */
2425
2426 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
2427 "Adding overlay 'overlay_gpio_04a' failed\n");
2428
2429 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2430 "unittest_gpio_probe() failed or not called\n");
2431
2432 /*
2433 * overlay_gpio_04b contains child gpio hog node
2434 *
2435 * - apply overlay_gpio_04b
2436 *
2437 * apply the overlay will result in
2438 * - processing gpio for overlay_gpio_04b
2439 */
2440
2441 /* overlay_gpio_04b contains child gpio hog node */
2442
2443 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
2444 "Adding overlay 'overlay_gpio_04b' failed\n");
2445
2446 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2447 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2448 unittest_gpio_chip_request_count - chip_request_count);
2449 }
2450
2451 #else
2452
of_unittest_overlay_gpio(void)2453 static void __init of_unittest_overlay_gpio(void)
2454 {
2455 /* skip tests */
2456 }
2457
2458 #endif
2459
2460 #if IS_BUILTIN(CONFIG_I2C)
2461
2462 /* get the i2c client device instantiated at the path */
of_path_to_i2c_client(const char * path)2463 static struct i2c_client *of_path_to_i2c_client(const char *path)
2464 {
2465 struct device_node *np;
2466 struct i2c_client *client;
2467
2468 np = of_find_node_by_path(path);
2469 if (np == NULL)
2470 return NULL;
2471
2472 client = of_find_i2c_device_by_node(np);
2473 of_node_put(np);
2474
2475 return client;
2476 }
2477
2478 /* find out if a i2c client device exists at that path */
of_path_i2c_client_exists(const char * path)2479 static int of_path_i2c_client_exists(const char *path)
2480 {
2481 struct i2c_client *client;
2482
2483 client = of_path_to_i2c_client(path);
2484 if (client)
2485 put_device(&client->dev);
2486 return client != NULL;
2487 }
2488 #else
of_path_i2c_client_exists(const char * path)2489 static int of_path_i2c_client_exists(const char *path)
2490 {
2491 return 0;
2492 }
2493 #endif
2494
2495 enum overlay_type {
2496 PDEV_OVERLAY,
2497 I2C_OVERLAY
2498 };
2499
of_path_device_type_exists(const char * path,enum overlay_type ovtype)2500 static int of_path_device_type_exists(const char *path,
2501 enum overlay_type ovtype)
2502 {
2503 switch (ovtype) {
2504 case PDEV_OVERLAY:
2505 return of_path_platform_device_exists(path);
2506 case I2C_OVERLAY:
2507 return of_path_i2c_client_exists(path);
2508 }
2509 return 0;
2510 }
2511
unittest_path(int nr,enum overlay_type ovtype)2512 static const char *unittest_path(int nr, enum overlay_type ovtype)
2513 {
2514 const char *base;
2515 static char buf[256];
2516
2517 switch (ovtype) {
2518 case PDEV_OVERLAY:
2519 base = "/testcase-data/overlay-node/test-bus";
2520 break;
2521 case I2C_OVERLAY:
2522 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2523 break;
2524 default:
2525 buf[0] = '\0';
2526 return buf;
2527 }
2528 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2529 buf[sizeof(buf) - 1] = '\0';
2530 return buf;
2531 }
2532
of_unittest_device_exists(int unittest_nr,enum overlay_type ovtype)2533 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2534 {
2535 const char *path;
2536
2537 path = unittest_path(unittest_nr, ovtype);
2538
2539 switch (ovtype) {
2540 case PDEV_OVERLAY:
2541 return of_path_platform_device_exists(path);
2542 case I2C_OVERLAY:
2543 return of_path_i2c_client_exists(path);
2544 }
2545 return 0;
2546 }
2547
overlay_name_from_nr(int nr)2548 static const char *overlay_name_from_nr(int nr)
2549 {
2550 static char buf[256];
2551
2552 snprintf(buf, sizeof(buf) - 1,
2553 "overlay_%d", nr);
2554 buf[sizeof(buf) - 1] = '\0';
2555
2556 return buf;
2557 }
2558
2559 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2560
2561 #define MAX_TRACK_OVCS_IDS 256
2562
2563 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2564 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2565 static int track_ovcs_id_cnt;
2566
of_unittest_track_overlay(int ovcs_id,int overlay_nr)2567 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2568 {
2569 if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2570 return;
2571
2572 track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2573 track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2574 track_ovcs_id_cnt++;
2575 }
2576
of_unittest_untrack_overlay(int ovcs_id)2577 static void of_unittest_untrack_overlay(int ovcs_id)
2578 {
2579 if (WARN_ON(track_ovcs_id_cnt < 1))
2580 return;
2581
2582 track_ovcs_id_cnt--;
2583
2584 /* If out of synch then test is broken. Do not try to recover. */
2585 WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2586 }
2587
of_unittest_remove_tracked_overlays(void)2588 static void of_unittest_remove_tracked_overlays(void)
2589 {
2590 int ret, ovcs_id, overlay_nr, save_ovcs_id;
2591 const char *overlay_name;
2592
2593 while (track_ovcs_id_cnt > 0) {
2594
2595 ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2596 overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2597 save_ovcs_id = ovcs_id;
2598 ret = of_overlay_remove(&ovcs_id);
2599 if (ret == -ENODEV) {
2600 overlay_name = overlay_name_from_nr(overlay_nr);
2601 pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2602 __func__, overlay_name, ret);
2603 }
2604 of_unittest_untrack_overlay(save_ovcs_id);
2605 }
2606
2607 }
2608
of_unittest_apply_overlay(int overlay_nr,int * ovcs_id)2609 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2610 {
2611 /*
2612 * The overlay will be tracked, thus it will be removed
2613 * by of_unittest_remove_tracked_overlays().
2614 */
2615
2616 const char *overlay_name;
2617
2618 overlay_name = overlay_name_from_nr(overlay_nr);
2619
2620 if (!overlay_data_apply(overlay_name, ovcs_id)) {
2621 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2622 return -EFAULT;
2623 }
2624 of_unittest_track_overlay(*ovcs_id, overlay_nr);
2625
2626 return 0;
2627 }
2628
__of_unittest_apply_overlay_check(int overlay_nr,int unittest_nr,int before,int after,enum overlay_type ovtype)2629 static int __init __of_unittest_apply_overlay_check(int overlay_nr,
2630 int unittest_nr, int before, int after,
2631 enum overlay_type ovtype)
2632 {
2633 int ret, ovcs_id;
2634
2635 /* unittest device must be in before state */
2636 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2637 unittest(0, "%s with device @\"%s\" %s\n",
2638 overlay_name_from_nr(overlay_nr),
2639 unittest_path(unittest_nr, ovtype),
2640 !before ? "enabled" : "disabled");
2641 return -EINVAL;
2642 }
2643
2644 /* apply the overlay */
2645 ovcs_id = 0;
2646 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2647 if (ret != 0) {
2648 /* of_unittest_apply_overlay already called unittest() */
2649 return ret;
2650 }
2651
2652 /* unittest device must be in after state */
2653 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2654 unittest(0, "%s with device @\"%s\" %s\n",
2655 overlay_name_from_nr(overlay_nr),
2656 unittest_path(unittest_nr, ovtype),
2657 !after ? "enabled" : "disabled");
2658 return -EINVAL;
2659 }
2660
2661 return ovcs_id;
2662 }
2663
2664 /* 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)2665 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2666 int unittest_nr, int before, int after,
2667 enum overlay_type ovtype)
2668 {
2669 int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
2670 unittest_nr, before, after, ovtype);
2671 if (ovcs_id < 0)
2672 return ovcs_id;
2673
2674 return 0;
2675 }
2676
2677 /* 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)2678 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2679 int unittest_nr, int before, int after,
2680 enum overlay_type ovtype)
2681 {
2682 int ret, ovcs_id, save_ovcs_id;
2683
2684 ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
2685 before, after, ovtype);
2686 if (ovcs_id < 0)
2687 return ovcs_id;
2688
2689 /* remove the overlay */
2690 save_ovcs_id = ovcs_id;
2691 ret = of_overlay_remove(&ovcs_id);
2692 if (ret != 0) {
2693 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2694 overlay_name_from_nr(overlay_nr),
2695 unittest_path(unittest_nr, ovtype));
2696 return ret;
2697 }
2698 of_unittest_untrack_overlay(save_ovcs_id);
2699
2700 /* unittest device must be again in before state */
2701 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2702 unittest(0, "%s with device @\"%s\" %s\n",
2703 overlay_name_from_nr(overlay_nr),
2704 unittest_path(unittest_nr, ovtype),
2705 !before ? "enabled" : "disabled");
2706 return -EINVAL;
2707 }
2708
2709 return 0;
2710 }
2711
2712 /* test activation of device */
of_unittest_overlay_0(void)2713 static void __init of_unittest_overlay_0(void)
2714 {
2715 int ret;
2716
2717 EXPECT_BEGIN(KERN_INFO,
2718 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2719
2720 /* device should enable */
2721 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2722
2723 EXPECT_END(KERN_INFO,
2724 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2725
2726 if (ret)
2727 return;
2728
2729 unittest(1, "overlay test %d passed\n", 0);
2730 }
2731
2732 /* test deactivation of device */
of_unittest_overlay_1(void)2733 static void __init of_unittest_overlay_1(void)
2734 {
2735 int ret;
2736
2737 EXPECT_BEGIN(KERN_INFO,
2738 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2739
2740 /* device should disable */
2741 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2742
2743 EXPECT_END(KERN_INFO,
2744 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2745
2746 if (ret)
2747 return;
2748
2749 unittest(1, "overlay test %d passed\n", 1);
2750
2751 }
2752
2753 /* test activation of device */
of_unittest_overlay_2(void)2754 static void __init of_unittest_overlay_2(void)
2755 {
2756 int ret;
2757
2758 EXPECT_BEGIN(KERN_INFO,
2759 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2760
2761 /* device should enable */
2762 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2763
2764 EXPECT_END(KERN_INFO,
2765 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2766
2767 if (ret)
2768 return;
2769 unittest(1, "overlay test %d passed\n", 2);
2770 }
2771
2772 /* test deactivation of device */
of_unittest_overlay_3(void)2773 static void __init of_unittest_overlay_3(void)
2774 {
2775 int ret;
2776
2777 EXPECT_BEGIN(KERN_INFO,
2778 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2779
2780 /* device should disable */
2781 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2782
2783 EXPECT_END(KERN_INFO,
2784 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2785
2786 if (ret)
2787 return;
2788
2789 unittest(1, "overlay test %d passed\n", 3);
2790 }
2791
2792 /* test activation of a full device node */
of_unittest_overlay_4(void)2793 static void __init of_unittest_overlay_4(void)
2794 {
2795 /* device should disable */
2796 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2797 return;
2798
2799 unittest(1, "overlay test %d passed\n", 4);
2800 }
2801
2802 /* test overlay apply/revert sequence */
of_unittest_overlay_5(void)2803 static void __init of_unittest_overlay_5(void)
2804 {
2805 int ret;
2806
2807 EXPECT_BEGIN(KERN_INFO,
2808 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2809
2810 /* device should disable */
2811 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2812
2813 EXPECT_END(KERN_INFO,
2814 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2815
2816 if (ret)
2817 return;
2818
2819 unittest(1, "overlay test %d passed\n", 5);
2820 }
2821
2822 /* test overlay application in sequence */
of_unittest_overlay_6(void)2823 static void __init of_unittest_overlay_6(void)
2824 {
2825 int i, save_ovcs_id[2], ovcs_id;
2826 int overlay_nr = 6, unittest_nr = 6;
2827 int before = 0, after = 1;
2828 const char *overlay_name;
2829
2830 int ret;
2831
2832 /* unittest device must be in before state */
2833 for (i = 0; i < 2; i++) {
2834 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2835 != before) {
2836 unittest(0, "%s with device @\"%s\" %s\n",
2837 overlay_name_from_nr(overlay_nr + i),
2838 unittest_path(unittest_nr + i,
2839 PDEV_OVERLAY),
2840 !before ? "enabled" : "disabled");
2841 return;
2842 }
2843 }
2844
2845 /* apply the overlays */
2846
2847 EXPECT_BEGIN(KERN_INFO,
2848 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2849
2850 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2851
2852 ret = overlay_data_apply(overlay_name, &ovcs_id);
2853
2854 if (!ret) {
2855 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2856 return;
2857 }
2858 save_ovcs_id[0] = ovcs_id;
2859 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2860
2861 EXPECT_END(KERN_INFO,
2862 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2863
2864 EXPECT_BEGIN(KERN_INFO,
2865 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2866
2867 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2868
2869 ret = overlay_data_apply(overlay_name, &ovcs_id);
2870
2871 if (!ret) {
2872 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2873 return;
2874 }
2875 save_ovcs_id[1] = ovcs_id;
2876 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2877
2878 EXPECT_END(KERN_INFO,
2879 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2880
2881
2882 for (i = 0; i < 2; i++) {
2883 /* unittest device must be in after state */
2884 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2885 != after) {
2886 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2887 overlay_name_from_nr(overlay_nr + i),
2888 unittest_path(unittest_nr + i,
2889 PDEV_OVERLAY),
2890 !after ? "enabled" : "disabled");
2891 return;
2892 }
2893 }
2894
2895 for (i = 1; i >= 0; i--) {
2896 ovcs_id = save_ovcs_id[i];
2897 if (of_overlay_remove(&ovcs_id)) {
2898 unittest(0, "%s failed destroy @\"%s\"\n",
2899 overlay_name_from_nr(overlay_nr + i),
2900 unittest_path(unittest_nr + i,
2901 PDEV_OVERLAY));
2902 return;
2903 }
2904 of_unittest_untrack_overlay(save_ovcs_id[i]);
2905 }
2906
2907 for (i = 0; i < 2; i++) {
2908 /* unittest device must be again in before state */
2909 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2910 != before) {
2911 unittest(0, "%s with device @\"%s\" %s\n",
2912 overlay_name_from_nr(overlay_nr + i),
2913 unittest_path(unittest_nr + i,
2914 PDEV_OVERLAY),
2915 !before ? "enabled" : "disabled");
2916 return;
2917 }
2918 }
2919
2920 unittest(1, "overlay test %d passed\n", 6);
2921
2922 }
2923
2924 /* test overlay application in sequence */
of_unittest_overlay_8(void)2925 static void __init of_unittest_overlay_8(void)
2926 {
2927 int i, save_ovcs_id[2], ovcs_id;
2928 int overlay_nr = 8, unittest_nr = 8;
2929 const char *overlay_name;
2930 int ret;
2931
2932 /* we don't care about device state in this test */
2933
2934 EXPECT_BEGIN(KERN_INFO,
2935 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2936
2937 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2938
2939 ret = overlay_data_apply(overlay_name, &ovcs_id);
2940 if (!ret)
2941 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2942
2943 EXPECT_END(KERN_INFO,
2944 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2945
2946 if (!ret)
2947 return;
2948
2949 save_ovcs_id[0] = ovcs_id;
2950 of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2951
2952 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2953
2954 EXPECT_BEGIN(KERN_INFO,
2955 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2956
2957 /* apply the overlays */
2958 ret = overlay_data_apply(overlay_name, &ovcs_id);
2959
2960 EXPECT_END(KERN_INFO,
2961 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2962
2963 if (!ret) {
2964 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2965 return;
2966 }
2967
2968 save_ovcs_id[1] = ovcs_id;
2969 of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2970
2971 /* now try to remove first overlay (it should fail) */
2972 ovcs_id = save_ovcs_id[0];
2973
2974 EXPECT_BEGIN(KERN_INFO,
2975 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2976
2977 EXPECT_BEGIN(KERN_INFO,
2978 "OF: overlay: overlay #6 is not topmost");
2979
2980 ret = of_overlay_remove(&ovcs_id);
2981
2982 EXPECT_END(KERN_INFO,
2983 "OF: overlay: overlay #6 is not topmost");
2984
2985 EXPECT_END(KERN_INFO,
2986 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2987
2988 if (!ret) {
2989 /*
2990 * Should never get here. If we do, expect a lot of
2991 * subsequent tracking and overlay removal related errors.
2992 */
2993 unittest(0, "%s was destroyed @\"%s\"\n",
2994 overlay_name_from_nr(overlay_nr + 0),
2995 unittest_path(unittest_nr,
2996 PDEV_OVERLAY));
2997 return;
2998 }
2999
3000 /* removing them in order should work */
3001 for (i = 1; i >= 0; i--) {
3002 ovcs_id = save_ovcs_id[i];
3003 if (of_overlay_remove(&ovcs_id)) {
3004 unittest(0, "%s not destroyed @\"%s\"\n",
3005 overlay_name_from_nr(overlay_nr + i),
3006 unittest_path(unittest_nr,
3007 PDEV_OVERLAY));
3008 return;
3009 }
3010 of_unittest_untrack_overlay(save_ovcs_id[i]);
3011 }
3012
3013 unittest(1, "overlay test %d passed\n", 8);
3014 }
3015
3016 /* test insertion of a bus with parent devices */
of_unittest_overlay_10(void)3017 static void __init of_unittest_overlay_10(void)
3018 {
3019 int ret;
3020 char *child_path;
3021
3022 /* device should disable */
3023 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
3024
3025 if (unittest(ret == 0,
3026 "overlay test %d failed; overlay application\n", 10))
3027 return;
3028
3029 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
3030 unittest_path(10, PDEV_OVERLAY));
3031 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
3032 return;
3033
3034 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
3035 kfree(child_path);
3036
3037 unittest(ret, "overlay test %d failed; no child device\n", 10);
3038 }
3039
3040 /* test insertion of a bus with parent devices (and revert) */
of_unittest_overlay_11(void)3041 static void __init of_unittest_overlay_11(void)
3042 {
3043 int ret;
3044
3045 /* device should disable */
3046 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
3047 PDEV_OVERLAY);
3048
3049 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
3050 }
3051
3052 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
3053
3054 struct unittest_i2c_bus_data {
3055 struct platform_device *pdev;
3056 struct i2c_adapter adap;
3057 };
3058
unittest_i2c_master_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)3059 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
3060 struct i2c_msg *msgs, int num)
3061 {
3062 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
3063
3064 (void)std;
3065
3066 return num;
3067 }
3068
unittest_i2c_functionality(struct i2c_adapter * adap)3069 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
3070 {
3071 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3072 }
3073
3074 static const struct i2c_algorithm unittest_i2c_algo = {
3075 .master_xfer = unittest_i2c_master_xfer,
3076 .functionality = unittest_i2c_functionality,
3077 };
3078
unittest_i2c_bus_probe(struct platform_device * pdev)3079 static int unittest_i2c_bus_probe(struct platform_device *pdev)
3080 {
3081 struct device *dev = &pdev->dev;
3082 struct device_node *np = dev->of_node;
3083 struct unittest_i2c_bus_data *std;
3084 struct i2c_adapter *adap;
3085 int ret;
3086
3087 if (np == NULL) {
3088 dev_err(dev, "No OF data for device\n");
3089 return -EINVAL;
3090
3091 }
3092
3093 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3094
3095 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
3096 if (!std)
3097 return -ENOMEM;
3098
3099 /* link them together */
3100 std->pdev = pdev;
3101 platform_set_drvdata(pdev, std);
3102
3103 adap = &std->adap;
3104 i2c_set_adapdata(adap, std);
3105 adap->nr = -1;
3106 strscpy(adap->name, pdev->name, sizeof(adap->name));
3107 adap->class = I2C_CLASS_DEPRECATED;
3108 adap->algo = &unittest_i2c_algo;
3109 adap->dev.parent = dev;
3110 adap->dev.of_node = dev->of_node;
3111 adap->timeout = 5 * HZ;
3112 adap->retries = 3;
3113
3114 ret = i2c_add_numbered_adapter(adap);
3115 if (ret != 0) {
3116 dev_err(dev, "Failed to add I2C adapter\n");
3117 return ret;
3118 }
3119
3120 return 0;
3121 }
3122
unittest_i2c_bus_remove(struct platform_device * pdev)3123 static void unittest_i2c_bus_remove(struct platform_device *pdev)
3124 {
3125 struct device *dev = &pdev->dev;
3126 struct device_node *np = dev->of_node;
3127 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
3128
3129 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3130 i2c_del_adapter(&std->adap);
3131 }
3132
3133 static const struct of_device_id unittest_i2c_bus_match[] = {
3134 { .compatible = "unittest-i2c-bus", },
3135 {},
3136 };
3137
3138 static struct platform_driver unittest_i2c_bus_driver = {
3139 .probe = unittest_i2c_bus_probe,
3140 .remove = unittest_i2c_bus_remove,
3141 .driver = {
3142 .name = "unittest-i2c-bus",
3143 .of_match_table = unittest_i2c_bus_match,
3144 },
3145 };
3146
unittest_i2c_dev_probe(struct i2c_client * client)3147 static int unittest_i2c_dev_probe(struct i2c_client *client)
3148 {
3149 struct device *dev = &client->dev;
3150 struct device_node *np = client->dev.of_node;
3151
3152 if (!np) {
3153 dev_err(dev, "No OF node\n");
3154 return -EINVAL;
3155 }
3156
3157 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3158
3159 return 0;
3160 };
3161
unittest_i2c_dev_remove(struct i2c_client * client)3162 static void unittest_i2c_dev_remove(struct i2c_client *client)
3163 {
3164 struct device *dev = &client->dev;
3165 struct device_node *np = client->dev.of_node;
3166
3167 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3168 }
3169
3170 static const struct i2c_device_id unittest_i2c_dev_id[] = {
3171 { .name = "unittest-i2c-dev" },
3172 { }
3173 };
3174
3175 static struct i2c_driver unittest_i2c_dev_driver = {
3176 .driver = {
3177 .name = "unittest-i2c-dev",
3178 },
3179 .probe = unittest_i2c_dev_probe,
3180 .remove = unittest_i2c_dev_remove,
3181 .id_table = unittest_i2c_dev_id,
3182 };
3183
3184 #if IS_BUILTIN(CONFIG_I2C_MUX)
3185
unittest_i2c_mux_select_chan(struct i2c_mux_core * muxc,u32 chan)3186 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
3187 {
3188 return 0;
3189 }
3190
unittest_i2c_mux_probe(struct i2c_client * client)3191 static int unittest_i2c_mux_probe(struct i2c_client *client)
3192 {
3193 int i, nchans;
3194 struct device *dev = &client->dev;
3195 struct i2c_adapter *adap = client->adapter;
3196 struct device_node *np = client->dev.of_node, *child;
3197 struct i2c_mux_core *muxc;
3198 u32 reg, max_reg;
3199
3200 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3201
3202 if (!np) {
3203 dev_err(dev, "No OF node\n");
3204 return -EINVAL;
3205 }
3206
3207 max_reg = (u32)-1;
3208 for_each_child_of_node(np, child) {
3209 if (of_property_read_u32(child, "reg", ®))
3210 continue;
3211 if (max_reg == (u32)-1 || reg > max_reg)
3212 max_reg = reg;
3213 }
3214 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
3215 if (nchans == 0) {
3216 dev_err(dev, "No channels\n");
3217 return -EINVAL;
3218 }
3219
3220 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
3221 unittest_i2c_mux_select_chan, NULL);
3222 if (!muxc)
3223 return -ENOMEM;
3224 for (i = 0; i < nchans; i++) {
3225 if (i2c_mux_add_adapter(muxc, 0, i)) {
3226 dev_err(dev, "Failed to register mux #%d\n", i);
3227 i2c_mux_del_adapters(muxc);
3228 return -ENODEV;
3229 }
3230 }
3231
3232 i2c_set_clientdata(client, muxc);
3233
3234 return 0;
3235 };
3236
unittest_i2c_mux_remove(struct i2c_client * client)3237 static void unittest_i2c_mux_remove(struct i2c_client *client)
3238 {
3239 struct device *dev = &client->dev;
3240 struct device_node *np = client->dev.of_node;
3241 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
3242
3243 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
3244 i2c_mux_del_adapters(muxc);
3245 }
3246
3247 static const struct i2c_device_id unittest_i2c_mux_id[] = {
3248 { .name = "unittest-i2c-mux" },
3249 { }
3250 };
3251
3252 static struct i2c_driver unittest_i2c_mux_driver = {
3253 .driver = {
3254 .name = "unittest-i2c-mux",
3255 },
3256 .probe = unittest_i2c_mux_probe,
3257 .remove = unittest_i2c_mux_remove,
3258 .id_table = unittest_i2c_mux_id,
3259 };
3260
3261 #endif
3262
of_unittest_overlay_i2c_init(void)3263 static int of_unittest_overlay_i2c_init(void)
3264 {
3265 int ret;
3266
3267 ret = i2c_add_driver(&unittest_i2c_dev_driver);
3268 if (unittest(ret == 0,
3269 "could not register unittest i2c device driver\n"))
3270 return ret;
3271
3272 ret = platform_driver_register(&unittest_i2c_bus_driver);
3273
3274 if (unittest(ret == 0,
3275 "could not register unittest i2c bus driver\n"))
3276 return ret;
3277
3278 #if IS_BUILTIN(CONFIG_I2C_MUX)
3279
3280 EXPECT_BEGIN(KERN_INFO,
3281 "i2c i2c-1: Added multiplexed i2c bus 2");
3282
3283 ret = i2c_add_driver(&unittest_i2c_mux_driver);
3284
3285 EXPECT_END(KERN_INFO,
3286 "i2c i2c-1: Added multiplexed i2c bus 2");
3287
3288 if (unittest(ret == 0,
3289 "could not register unittest i2c mux driver\n"))
3290 return ret;
3291 #endif
3292
3293 return 0;
3294 }
3295
of_unittest_overlay_i2c_cleanup(void)3296 static void of_unittest_overlay_i2c_cleanup(void)
3297 {
3298 #if IS_BUILTIN(CONFIG_I2C_MUX)
3299 i2c_del_driver(&unittest_i2c_mux_driver);
3300 #endif
3301 platform_driver_unregister(&unittest_i2c_bus_driver);
3302 i2c_del_driver(&unittest_i2c_dev_driver);
3303 }
3304
of_unittest_overlay_i2c_12(void)3305 static void __init of_unittest_overlay_i2c_12(void)
3306 {
3307 int ret;
3308
3309 /* device should enable */
3310 EXPECT_BEGIN(KERN_INFO,
3311 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
3312
3313 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
3314
3315 EXPECT_END(KERN_INFO,
3316 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
3317
3318 if (ret)
3319 return;
3320
3321 unittest(1, "overlay test %d passed\n", 12);
3322 }
3323
3324 /* test deactivation of device */
of_unittest_overlay_i2c_13(void)3325 static void __init of_unittest_overlay_i2c_13(void)
3326 {
3327 int ret;
3328
3329 EXPECT_BEGIN(KERN_INFO,
3330 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
3331
3332 /* device should disable */
3333 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
3334
3335 EXPECT_END(KERN_INFO,
3336 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
3337
3338 if (ret)
3339 return;
3340
3341 unittest(1, "overlay test %d passed\n", 13);
3342 }
3343
3344 /* just check for i2c mux existence */
of_unittest_overlay_i2c_14(void)3345 static void of_unittest_overlay_i2c_14(void)
3346 {
3347 }
3348
of_unittest_overlay_i2c_15(void)3349 static void __init of_unittest_overlay_i2c_15(void)
3350 {
3351 int ret;
3352
3353 /* device should enable */
3354 EXPECT_BEGIN(KERN_INFO,
3355 "i2c i2c-1: Added multiplexed i2c bus 3");
3356
3357 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
3358
3359 EXPECT_END(KERN_INFO,
3360 "i2c i2c-1: Added multiplexed i2c bus 3");
3361
3362 if (ret)
3363 return;
3364
3365 unittest(1, "overlay test %d passed\n", 15);
3366 }
3367
3368 #else
3369
of_unittest_overlay_i2c_14(void)3370 static inline void of_unittest_overlay_i2c_14(void) { }
of_unittest_overlay_i2c_15(void)3371 static inline void of_unittest_overlay_i2c_15(void) { }
3372
3373 #endif
3374
of_notify(struct notifier_block * nb,unsigned long action,void * arg)3375 static int of_notify(struct notifier_block *nb, unsigned long action,
3376 void *arg)
3377 {
3378 struct of_overlay_notify_data *nd = arg;
3379 struct device_node *found;
3380 int ret;
3381
3382 /*
3383 * For overlay_16 .. overlay_19, check that returning an error
3384 * works for each of the actions by setting an arbitrary return
3385 * error number that matches the test number. e.g. for unittest16,
3386 * ret = -EBUSY which is -16.
3387 *
3388 * OVERLAY_INFO() for the overlays is declared to expect the same
3389 * error number, so overlay_data_apply() will return no error.
3390 *
3391 * overlay_20 will return NOTIFY_DONE
3392 */
3393
3394 ret = 0;
3395 of_node_get(nd->overlay);
3396
3397 switch (action) {
3398
3399 case OF_OVERLAY_PRE_APPLY:
3400 found = of_find_node_by_name(nd->overlay, "test-unittest16");
3401 if (found) {
3402 of_node_put(found);
3403 ret = -EBUSY;
3404 }
3405 break;
3406
3407 case OF_OVERLAY_POST_APPLY:
3408 found = of_find_node_by_name(nd->overlay, "test-unittest17");
3409 if (found) {
3410 of_node_put(found);
3411 ret = -EEXIST;
3412 }
3413 break;
3414
3415 case OF_OVERLAY_PRE_REMOVE:
3416 found = of_find_node_by_name(nd->overlay, "test-unittest18");
3417 if (found) {
3418 of_node_put(found);
3419 ret = -EXDEV;
3420 }
3421 break;
3422
3423 case OF_OVERLAY_POST_REMOVE:
3424 found = of_find_node_by_name(nd->overlay, "test-unittest19");
3425 if (found) {
3426 of_node_put(found);
3427 ret = -ENODEV;
3428 }
3429 break;
3430
3431 default: /* should not happen */
3432 of_node_put(nd->overlay);
3433 ret = -EINVAL;
3434 break;
3435 }
3436
3437 if (ret)
3438 return notifier_from_errno(ret);
3439
3440 return NOTIFY_DONE;
3441 }
3442
3443 static struct notifier_block of_nb = {
3444 .notifier_call = of_notify,
3445 };
3446
of_unittest_overlay_notify(void)3447 static void __init of_unittest_overlay_notify(void)
3448 {
3449 int ovcs_id;
3450 int ret;
3451
3452 ret = of_overlay_notifier_register(&of_nb);
3453 unittest(!ret,
3454 "of_overlay_notifier_register() failed, ret = %d\n", ret);
3455 if (ret)
3456 return;
3457
3458 /*
3459 * The overlays are applied by overlay_data_apply()
3460 * instead of of_unittest_apply_overlay() so that they
3461 * will not be tracked. Thus they will not be removed
3462 * by of_unittest_remove_tracked_overlays().
3463 *
3464 * Applying overlays 16 - 19 will each trigger an error for a
3465 * different action in of_notify().
3466 *
3467 * Applying overlay 20 will not trigger any error in of_notify().
3468 */
3469
3470 /* --- overlay 16 --- */
3471
3472 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3473
3474 unittest(overlay_data_apply("overlay_16", &ovcs_id),
3475 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
3476
3477 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3478
3479 unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
3480
3481 /* --- overlay 17 --- */
3482
3483 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3484
3485 unittest(overlay_data_apply("overlay_17", &ovcs_id),
3486 "test OF_OVERLAY_POST_APPLY notify injected error\n");
3487
3488 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3489
3490 unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
3491
3492 /* --- overlay 18 --- */
3493
3494 unittest(overlay_data_apply("overlay_18", &ovcs_id),
3495 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3496
3497 unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
3498
3499 if (ovcs_id) {
3500 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3501
3502 ret = of_overlay_remove(&ovcs_id);
3503 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3504 if (ret == -EXDEV) {
3505 /*
3506 * change set ovcs_id should still exist
3507 */
3508 unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3509 } else {
3510 unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3511 }
3512 } else {
3513 unittest(1, "ovcs_id not created for overlay_18\n");
3514 }
3515
3516 unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3517
3518 /* --- overlay 19 --- */
3519
3520 unittest(overlay_data_apply("overlay_19", &ovcs_id),
3521 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3522
3523 unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3524
3525 if (ovcs_id) {
3526 EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3527 ret = of_overlay_remove(&ovcs_id);
3528 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3529 if (ret == -ENODEV)
3530 unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3531 else
3532 unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3533 } else {
3534 unittest(1, "ovcs_id removed for overlay_19\n");
3535 }
3536
3537 unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3538 ovcs_id);
3539
3540 /* --- overlay 20 --- */
3541
3542 unittest(overlay_data_apply("overlay_20", &ovcs_id),
3543 "overlay notify no injected error\n");
3544
3545 if (ovcs_id) {
3546 ret = of_overlay_remove(&ovcs_id);
3547 if (ret)
3548 unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3549 ret);
3550 } else {
3551 unittest(1, "ovcs_id not created for overlay_20\n");
3552 }
3553
3554 unittest(!of_overlay_notifier_unregister(&of_nb),
3555 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3556 }
3557
of_unittest_overlay(void)3558 static void __init of_unittest_overlay(void)
3559 {
3560 struct device_node *bus_np = NULL;
3561 unsigned int i;
3562
3563 if (platform_driver_register(&unittest_driver)) {
3564 unittest(0, "could not register unittest driver\n");
3565 goto out;
3566 }
3567
3568 bus_np = of_find_node_by_path(bus_path);
3569 if (bus_np == NULL) {
3570 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3571 goto out;
3572 }
3573
3574 if (of_platform_default_populate(bus_np, NULL, NULL)) {
3575 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3576 goto out;
3577 }
3578
3579 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3580 unittest(0, "could not find unittest0 @ \"%s\"\n",
3581 unittest_path(100, PDEV_OVERLAY));
3582 goto out;
3583 }
3584
3585 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3586 unittest(0, "unittest1 @ \"%s\" should not exist\n",
3587 unittest_path(101, PDEV_OVERLAY));
3588 goto out;
3589 }
3590
3591 unittest(1, "basic infrastructure of overlays passed");
3592
3593 /* tests in sequence */
3594 of_unittest_overlay_0();
3595 of_unittest_overlay_1();
3596 of_unittest_overlay_2();
3597 of_unittest_overlay_3();
3598 of_unittest_overlay_4();
3599 for (i = 0; i < 3; i++)
3600 of_unittest_overlay_5();
3601 of_unittest_overlay_6();
3602 of_unittest_overlay_8();
3603
3604 of_unittest_overlay_10();
3605 of_unittest_overlay_11();
3606
3607 #if IS_BUILTIN(CONFIG_I2C)
3608 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3609 goto out;
3610
3611 of_unittest_overlay_i2c_12();
3612 of_unittest_overlay_i2c_13();
3613 of_unittest_overlay_i2c_14();
3614 of_unittest_overlay_i2c_15();
3615
3616 of_unittest_overlay_i2c_cleanup();
3617 #endif
3618
3619 of_unittest_overlay_gpio();
3620
3621 of_unittest_remove_tracked_overlays();
3622
3623 of_unittest_overlay_notify();
3624
3625 out:
3626 of_node_put(bus_np);
3627 }
3628
3629 #else
of_unittest_overlay(void)3630 static inline void __init of_unittest_overlay(void) { }
3631 #endif
3632
of_unittest_lifecycle(void)3633 static void __init of_unittest_lifecycle(void)
3634 {
3635 #ifdef CONFIG_OF_DYNAMIC
3636 unsigned int refcount;
3637 int found_refcount_one = 0;
3638 int put_count = 0;
3639 struct device_node *np;
3640 struct device_node *prev_sibling, *next_sibling;
3641 const char *refcount_path = "/testcase-data/refcount-node";
3642 const char *refcount_parent_path = "/testcase-data";
3643
3644 /*
3645 * Node lifecycle tests, non-dynamic node:
3646 *
3647 * - Decrementing refcount to zero via of_node_put() should cause the
3648 * attempt to free the node memory by of_node_release() to fail
3649 * because the node is not a dynamic node.
3650 *
3651 * - Decrementing refcount past zero should result in additional
3652 * errors reported.
3653 */
3654
3655 np = of_find_node_by_path(refcount_path);
3656 unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3657 if (np == NULL)
3658 goto out_skip_tests;
3659
3660 while (!found_refcount_one) {
3661
3662 if (put_count++ > 10) {
3663 unittest(0, "guardrail to avoid infinite loop\n");
3664 goto out_skip_tests;
3665 }
3666
3667 refcount = kref_read(&np->kobj.kref);
3668 if (refcount == 1)
3669 found_refcount_one = 1;
3670 else
3671 of_node_put(np);
3672 }
3673
3674 EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3675
3676 /*
3677 * refcount is now one, decrementing to zero will result in a call to
3678 * of_node_release() to free the node's memory, which should result
3679 * in an error
3680 */
3681 unittest(1, "/testcase-data/refcount-node is one");
3682 of_node_put(np);
3683
3684 EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3685
3686
3687 /*
3688 * expect stack trace for subsequent of_node_put():
3689 * __refcount_sub_and_test() calls:
3690 * refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3691 *
3692 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3693 * the first three lines, and the last line.
3694 */
3695 EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3696 EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3697 EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3698 EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3699
3700 /* refcount is now zero, this should fail */
3701 unittest(1, "/testcase-data/refcount-node is zero");
3702 of_node_put(np);
3703
3704 EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3705 EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3706 EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3707 EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3708
3709 /*
3710 * Q. do we expect to get yet another warning?
3711 * A. no, the WARNING is from WARN_ONCE()
3712 */
3713 EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3714 EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3715 EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3716 EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3717
3718 unittest(1, "/testcase-data/refcount-node is zero, second time");
3719 of_node_put(np);
3720
3721 EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3722 EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3723 EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3724 EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3725
3726 /*
3727 * refcount of zero will trigger stack traces from any further
3728 * attempt to of_node_get() node "refcount-node". One example of
3729 * this is where of_unittest_check_node_linkage() will recursively
3730 * scan the tree, with 'for_each_child_of_node()' doing an
3731 * of_node_get() of the children of a node.
3732 *
3733 * Prevent the stack trace by removing node "refcount-node" from
3734 * its parent's child list.
3735 *
3736 * WARNING: EVIL, EVIL, EVIL:
3737 *
3738 * Directly manipulate the child list of node /testcase-data to
3739 * remove child refcount-node. This is ignoring all proper methods
3740 * of removing a child and will leak a small amount of memory.
3741 */
3742
3743 np = of_find_node_by_path(refcount_parent_path);
3744 unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3745 unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3746 if (np == NULL)
3747 return;
3748
3749 prev_sibling = np->child;
3750 next_sibling = prev_sibling->sibling;
3751 if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3752 np->child = next_sibling;
3753 next_sibling = next_sibling->sibling;
3754 }
3755 while (next_sibling) {
3756 if (!strcmp(next_sibling->full_name, "refcount-node"))
3757 prev_sibling->sibling = next_sibling->sibling;
3758 prev_sibling = next_sibling;
3759 next_sibling = next_sibling->sibling;
3760 }
3761 of_node_put(np);
3762
3763 return;
3764
3765 out_skip_tests:
3766 #endif
3767 unittest(0, "One or more lifecycle tests skipped\n");
3768 }
3769
3770 #ifdef CONFIG_OF_OVERLAY
3771
3772 /*
3773 * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3774 * created by cmd_wrap_S_dtbo in scripts/Makefile.dtbs
3775 */
3776
3777 #define OVERLAY_INFO_EXTERN(overlay_name) \
3778 extern uint8_t __dtbo_##overlay_name##_begin[]; \
3779 extern uint8_t __dtbo_##overlay_name##_end[]
3780
3781 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3782 { .dtbo_begin = __dtbo_##overlay_name##_begin, \
3783 .dtbo_end = __dtbo_##overlay_name##_end, \
3784 .expected_result = expected, \
3785 .expected_result_remove = expected_remove, \
3786 .name = #overlay_name, \
3787 }
3788
3789 struct overlay_info {
3790 uint8_t *dtbo_begin;
3791 uint8_t *dtbo_end;
3792 int expected_result;
3793 int expected_result_remove; /* if apply failed */
3794 int ovcs_id;
3795 char *name;
3796 };
3797
3798 OVERLAY_INFO_EXTERN(overlay_base);
3799 OVERLAY_INFO_EXTERN(overlay);
3800 OVERLAY_INFO_EXTERN(overlay_0);
3801 OVERLAY_INFO_EXTERN(overlay_1);
3802 OVERLAY_INFO_EXTERN(overlay_2);
3803 OVERLAY_INFO_EXTERN(overlay_3);
3804 OVERLAY_INFO_EXTERN(overlay_4);
3805 OVERLAY_INFO_EXTERN(overlay_5);
3806 OVERLAY_INFO_EXTERN(overlay_6);
3807 OVERLAY_INFO_EXTERN(overlay_7);
3808 OVERLAY_INFO_EXTERN(overlay_8);
3809 OVERLAY_INFO_EXTERN(overlay_9);
3810 OVERLAY_INFO_EXTERN(overlay_10);
3811 OVERLAY_INFO_EXTERN(overlay_11);
3812 OVERLAY_INFO_EXTERN(overlay_12);
3813 OVERLAY_INFO_EXTERN(overlay_13);
3814 OVERLAY_INFO_EXTERN(overlay_15);
3815 OVERLAY_INFO_EXTERN(overlay_16);
3816 OVERLAY_INFO_EXTERN(overlay_17);
3817 OVERLAY_INFO_EXTERN(overlay_18);
3818 OVERLAY_INFO_EXTERN(overlay_19);
3819 OVERLAY_INFO_EXTERN(overlay_20);
3820 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3821 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3822 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3823 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3824 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3825 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3826 OVERLAY_INFO_EXTERN(overlay_pci_node);
3827 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3828 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3829 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3830 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3831 OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
3832
3833 /* entries found by name */
3834 static struct overlay_info overlays[] = {
3835 OVERLAY_INFO(overlay_base, -9999, 0),
3836 OVERLAY_INFO(overlay, 0, 0),
3837 OVERLAY_INFO(overlay_0, 0, 0),
3838 OVERLAY_INFO(overlay_1, 0, 0),
3839 OVERLAY_INFO(overlay_2, 0, 0),
3840 OVERLAY_INFO(overlay_3, 0, 0),
3841 OVERLAY_INFO(overlay_4, 0, 0),
3842 OVERLAY_INFO(overlay_5, 0, 0),
3843 OVERLAY_INFO(overlay_6, 0, 0),
3844 OVERLAY_INFO(overlay_7, 0, 0),
3845 OVERLAY_INFO(overlay_8, 0, 0),
3846 OVERLAY_INFO(overlay_9, 0, 0),
3847 OVERLAY_INFO(overlay_10, 0, 0),
3848 OVERLAY_INFO(overlay_11, 0, 0),
3849 OVERLAY_INFO(overlay_12, 0, 0),
3850 OVERLAY_INFO(overlay_13, 0, 0),
3851 OVERLAY_INFO(overlay_15, 0, 0),
3852 OVERLAY_INFO(overlay_16, -EBUSY, 0),
3853 OVERLAY_INFO(overlay_17, -EEXIST, 0),
3854 OVERLAY_INFO(overlay_18, 0, 0),
3855 OVERLAY_INFO(overlay_19, 0, 0),
3856 OVERLAY_INFO(overlay_20, 0, 0),
3857 OVERLAY_INFO(overlay_gpio_01, 0, 0),
3858 OVERLAY_INFO(overlay_gpio_02a, 0, 0),
3859 OVERLAY_INFO(overlay_gpio_02b, 0, 0),
3860 OVERLAY_INFO(overlay_gpio_03, 0, 0),
3861 OVERLAY_INFO(overlay_gpio_04a, 0, 0),
3862 OVERLAY_INFO(overlay_gpio_04b, 0, 0),
3863 OVERLAY_INFO(overlay_pci_node, 0, 0),
3864 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
3865 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
3866 OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
3867 OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
3868 OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
3869 /* end marker */
3870 { }
3871 };
3872
3873 static struct device_node *overlay_base_root;
3874
dt_alloc_memory(u64 size,u64 align)3875 static void * __init dt_alloc_memory(u64 size, u64 align)
3876 {
3877 return memblock_alloc_or_panic(size, align);
3878 }
3879
3880 /*
3881 * Create base device tree for the overlay unittest.
3882 *
3883 * This is called from very early boot code.
3884 *
3885 * Do as much as possible the same way as done in __unflatten_device_tree
3886 * and other early boot steps for the normal FDT so that the overlay base
3887 * unflattened tree will have the same characteristics as the real tree
3888 * (such as having memory allocated by the early allocator). The goal
3889 * is to test "the real thing" as much as possible, and test "test setup
3890 * code" as little as possible.
3891 *
3892 * Have to stop before resolving phandles, because that uses kmalloc.
3893 */
unittest_unflatten_overlay_base(void)3894 void __init unittest_unflatten_overlay_base(void)
3895 {
3896 struct overlay_info *info;
3897 u32 data_size;
3898 void *new_fdt;
3899 u32 size;
3900 int found = 0;
3901 const char *overlay_name = "overlay_base";
3902
3903 for (info = overlays; info && info->name; info++) {
3904 if (!strcmp(overlay_name, info->name)) {
3905 found = 1;
3906 break;
3907 }
3908 }
3909 if (!found) {
3910 pr_err("no overlay data for %s\n", overlay_name);
3911 return;
3912 }
3913
3914 info = &overlays[0];
3915
3916 if (info->expected_result != -9999) {
3917 pr_err("No dtb 'overlay_base' to attach\n");
3918 return;
3919 }
3920
3921 data_size = info->dtbo_end - info->dtbo_begin;
3922 if (!data_size) {
3923 pr_err("No dtb 'overlay_base' to attach\n");
3924 return;
3925 }
3926
3927 size = fdt_totalsize(info->dtbo_begin);
3928 if (size != data_size) {
3929 pr_err("dtb 'overlay_base' header totalsize != actual size");
3930 return;
3931 }
3932
3933 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3934 if (!new_fdt) {
3935 pr_err("alloc for dtb 'overlay_base' failed");
3936 return;
3937 }
3938
3939 memcpy(new_fdt, info->dtbo_begin, size);
3940
3941 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3942 dt_alloc_memory, true);
3943 }
3944
3945 /*
3946 * The purpose of of_unittest_overlay_data_add is to add an
3947 * overlay in the normal fashion. This is a test of the whole
3948 * picture, instead of testing individual elements.
3949 *
3950 * A secondary purpose is to be able to verify that the contents of
3951 * /proc/device-tree/ contains the updated structure and values from
3952 * the overlay. That must be verified separately in user space.
3953 *
3954 * Return 0 on unexpected error.
3955 */
overlay_data_apply(const char * overlay_name,int * ovcs_id)3956 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3957 {
3958 struct overlay_info *info;
3959 int passed = 1;
3960 int found = 0;
3961 int ret, ret2;
3962 u32 size;
3963
3964 for (info = overlays; info && info->name; info++) {
3965 if (!strcmp(overlay_name, info->name)) {
3966 found = 1;
3967 break;
3968 }
3969 }
3970 if (!found) {
3971 pr_err("no overlay data for %s\n", overlay_name);
3972 return 0;
3973 }
3974
3975 size = info->dtbo_end - info->dtbo_begin;
3976 if (!size)
3977 pr_err("no overlay data for %s\n", overlay_name);
3978
3979 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
3980 NULL);
3981 if (ovcs_id)
3982 *ovcs_id = info->ovcs_id;
3983 if (ret < 0)
3984 goto out;
3985
3986 pr_debug("%s applied\n", overlay_name);
3987
3988 out:
3989 if (ret != info->expected_result) {
3990 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3991 info->expected_result, ret, overlay_name);
3992 passed = 0;
3993 }
3994
3995 if (ret < 0) {
3996 /* changeset may be partially applied */
3997 ret2 = of_overlay_remove(&info->ovcs_id);
3998 if (ret2 != info->expected_result_remove) {
3999 pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
4000 info->expected_result_remove, ret2,
4001 overlay_name);
4002 passed = 0;
4003 }
4004 }
4005
4006 return passed;
4007 }
4008
4009 /*
4010 * The purpose of of_unittest_overlay_high_level is to add an overlay
4011 * in the normal fashion. This is a test of the whole picture,
4012 * instead of individual elements.
4013 *
4014 * The first part of the function is _not_ normal overlay usage; it is
4015 * finishing splicing the base overlay device tree into the live tree.
4016 */
of_unittest_overlay_high_level(void)4017 static __init void of_unittest_overlay_high_level(void)
4018 {
4019 struct device_node *last_sibling;
4020 struct device_node *np;
4021 struct device_node *of_symbols;
4022 struct device_node *overlay_base_symbols;
4023 struct device_node **pprev;
4024 struct property *prop;
4025 int ret;
4026
4027 if (!overlay_base_root) {
4028 unittest(0, "overlay_base_root not initialized\n");
4029 return;
4030 }
4031
4032 /*
4033 * Could not fixup phandles in unittest_unflatten_overlay_base()
4034 * because kmalloc() was not yet available.
4035 */
4036 of_overlay_mutex_lock();
4037 of_resolve_phandles(overlay_base_root);
4038 of_overlay_mutex_unlock();
4039
4040
4041 /*
4042 * do not allow overlay_base to duplicate any node already in
4043 * tree, this greatly simplifies the code
4044 */
4045
4046 /*
4047 * remove overlay_base_root node "__local_fixups", after
4048 * being used by of_resolve_phandles()
4049 */
4050 pprev = &overlay_base_root->child;
4051 for (np = overlay_base_root->child; np; np = np->sibling) {
4052 if (of_node_name_eq(np, "__local_fixups__")) {
4053 *pprev = np->sibling;
4054 break;
4055 }
4056 pprev = &np->sibling;
4057 }
4058
4059 /* remove overlay_base_root node "__symbols__" if in live tree */
4060 of_symbols = of_get_child_by_name(of_root, "__symbols__");
4061 if (of_symbols) {
4062 /* will have to graft properties from node into live tree */
4063 pprev = &overlay_base_root->child;
4064 for (np = overlay_base_root->child; np; np = np->sibling) {
4065 if (of_node_name_eq(np, "__symbols__")) {
4066 overlay_base_symbols = np;
4067 *pprev = np->sibling;
4068 break;
4069 }
4070 pprev = &np->sibling;
4071 }
4072 }
4073
4074 for_each_child_of_node(overlay_base_root, np) {
4075 struct device_node *base_child;
4076 for_each_child_of_node(of_root, base_child) {
4077 if (!strcmp(np->full_name, base_child->full_name)) {
4078 unittest(0, "illegal node name in overlay_base %pOFn",
4079 np);
4080 of_node_put(np);
4081 of_node_put(base_child);
4082 return;
4083 }
4084 }
4085 }
4086
4087 /*
4088 * overlay 'overlay_base' is not allowed to have root
4089 * properties, so only need to splice nodes into main device tree.
4090 *
4091 * root node of *overlay_base_root will not be freed, it is lost
4092 * memory.
4093 */
4094
4095 for (np = overlay_base_root->child; np; np = np->sibling)
4096 np->parent = of_root;
4097
4098 mutex_lock(&of_mutex);
4099
4100 for (last_sibling = np = of_root->child; np; np = np->sibling)
4101 last_sibling = np;
4102
4103 if (last_sibling)
4104 last_sibling->sibling = overlay_base_root->child;
4105 else
4106 of_root->child = overlay_base_root->child;
4107
4108 for_each_of_allnodes_from(overlay_base_root, np)
4109 __of_attach_node_sysfs(np);
4110
4111 if (of_symbols) {
4112 struct property *new_prop;
4113 for_each_property_of_node(overlay_base_symbols, prop) {
4114
4115 new_prop = __of_prop_dup(prop, GFP_KERNEL);
4116 if (!new_prop) {
4117 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
4118 prop->name);
4119 goto err_unlock;
4120 }
4121 if (__of_add_property(of_symbols, new_prop)) {
4122 __of_prop_free(new_prop);
4123 /* "name" auto-generated by unflatten */
4124 if (!strcmp(prop->name, "name"))
4125 continue;
4126 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
4127 prop->name);
4128 goto err_unlock;
4129 }
4130 if (__of_add_property_sysfs(of_symbols, new_prop)) {
4131 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
4132 prop->name);
4133 goto err_unlock;
4134 }
4135 }
4136 }
4137
4138 mutex_unlock(&of_mutex);
4139
4140
4141 /* now do the normal overlay usage test */
4142
4143 /* --- overlay --- */
4144
4145 EXPECT_BEGIN(KERN_ERR,
4146 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
4147 EXPECT_BEGIN(KERN_ERR,
4148 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
4149 EXPECT_BEGIN(KERN_ERR,
4150 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
4151 EXPECT_BEGIN(KERN_ERR,
4152 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
4153 EXPECT_BEGIN(KERN_ERR,
4154 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
4155 EXPECT_BEGIN(KERN_ERR,
4156 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
4157 EXPECT_BEGIN(KERN_ERR,
4158 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
4159 EXPECT_BEGIN(KERN_ERR,
4160 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
4161 EXPECT_BEGIN(KERN_ERR,
4162 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
4163 EXPECT_BEGIN(KERN_ERR,
4164 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
4165 EXPECT_BEGIN(KERN_ERR,
4166 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
4167
4168 ret = overlay_data_apply("overlay", NULL);
4169
4170 EXPECT_END(KERN_ERR,
4171 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
4172 EXPECT_END(KERN_ERR,
4173 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
4174 EXPECT_END(KERN_ERR,
4175 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
4176 EXPECT_END(KERN_ERR,
4177 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
4178 EXPECT_END(KERN_ERR,
4179 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
4180 EXPECT_END(KERN_ERR,
4181 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
4182 EXPECT_END(KERN_ERR,
4183 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
4184 EXPECT_END(KERN_ERR,
4185 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
4186 EXPECT_END(KERN_ERR,
4187 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
4188 EXPECT_END(KERN_ERR,
4189 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
4190 EXPECT_END(KERN_ERR,
4191 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
4192
4193 unittest(ret, "Adding overlay 'overlay' failed\n");
4194
4195 /* --- overlay_bad_add_dup_node --- */
4196
4197 EXPECT_BEGIN(KERN_ERR,
4198 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
4199 EXPECT_BEGIN(KERN_ERR,
4200 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
4201 EXPECT_BEGIN(KERN_ERR,
4202 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
4203 EXPECT_BEGIN(KERN_ERR,
4204 "OF: Error reverting changeset (-19)");
4205
4206 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
4207 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
4208
4209 EXPECT_END(KERN_ERR,
4210 "OF: Error reverting changeset (-19)");
4211 EXPECT_END(KERN_ERR,
4212 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
4213 EXPECT_END(KERN_ERR,
4214 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
4215 EXPECT_END(KERN_ERR,
4216 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
4217
4218 /* --- overlay_bad_add_dup_prop --- */
4219
4220 EXPECT_BEGIN(KERN_ERR,
4221 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
4222 EXPECT_BEGIN(KERN_ERR,
4223 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
4224 EXPECT_BEGIN(KERN_ERR,
4225 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
4226 EXPECT_BEGIN(KERN_ERR,
4227 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
4228 EXPECT_BEGIN(KERN_ERR,
4229 "OF: Error reverting changeset (-19)");
4230
4231 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
4232 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
4233
4234 EXPECT_END(KERN_ERR,
4235 "OF: Error reverting changeset (-19)");
4236 EXPECT_END(KERN_ERR,
4237 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
4238 EXPECT_END(KERN_ERR,
4239 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
4240 EXPECT_END(KERN_ERR,
4241 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
4242 EXPECT_END(KERN_ERR,
4243 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
4244
4245 /* --- overlay_bad_phandle --- */
4246
4247 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
4248 "Adding overlay 'overlay_bad_phandle' failed\n");
4249
4250 /* --- overlay_bad_symbol --- */
4251
4252 EXPECT_BEGIN(KERN_ERR,
4253 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
4254 EXPECT_BEGIN(KERN_ERR,
4255 "OF: Error reverting changeset (-19)");
4256
4257 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
4258 "Adding overlay 'overlay_bad_symbol' failed\n");
4259
4260 EXPECT_END(KERN_ERR,
4261 "OF: Error reverting changeset (-19)");
4262 EXPECT_END(KERN_ERR,
4263 "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
4264
4265 /* --- overlay_bad_unresolved --- */
4266
4267 EXPECT_BEGIN(KERN_ERR,
4268 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
4269 EXPECT_BEGIN(KERN_ERR,
4270 "OF: resolver: overlay phandle fixup failed: -22");
4271
4272 unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
4273 "Adding overlay 'overlay_bad_unresolved' failed\n");
4274
4275 EXPECT_END(KERN_ERR,
4276 "OF: resolver: overlay phandle fixup failed: -22");
4277 EXPECT_END(KERN_ERR,
4278 "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
4279
4280 return;
4281
4282 err_unlock:
4283 mutex_unlock(&of_mutex);
4284 }
4285
4286 static int of_unittest_pci_dev_num;
4287 static int of_unittest_pci_child_num;
4288
4289 /*
4290 * PCI device tree node test driver
4291 */
4292 static const struct pci_device_id testdrv_pci_ids[] = {
4293 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
4294 { 0, }
4295 };
4296
testdrv_probe(struct pci_dev * pdev,const struct pci_device_id * id)4297 static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4298 {
4299 struct overlay_info *info;
4300 struct device_node *dn;
4301 int ret, ovcs_id;
4302 u32 size;
4303
4304 dn = pdev->dev.of_node;
4305 if (!dn) {
4306 dev_err(&pdev->dev, "does not find bus endpoint");
4307 return -EINVAL;
4308 }
4309
4310 for (info = overlays; info && info->name; info++) {
4311 if (!strcmp(info->name, "overlay_pci_node"))
4312 break;
4313 }
4314 if (!info || !info->name) {
4315 dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
4316 return -ENODEV;
4317 }
4318
4319 size = info->dtbo_end - info->dtbo_begin;
4320 ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
4321 of_node_put(dn);
4322 if (ret)
4323 return ret;
4324
4325 of_platform_default_populate(dn, NULL, &pdev->dev);
4326 pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
4327
4328 return 0;
4329 }
4330
testdrv_remove(struct pci_dev * pdev)4331 static void testdrv_remove(struct pci_dev *pdev)
4332 {
4333 int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
4334
4335 of_platform_depopulate(&pdev->dev);
4336 of_overlay_remove(&ovcs_id);
4337 }
4338
4339 static struct pci_driver testdrv_driver = {
4340 .name = "pci_dt_testdrv",
4341 .id_table = testdrv_pci_ids,
4342 .probe = testdrv_probe,
4343 .remove = testdrv_remove,
4344 };
4345
unittest_pci_probe(struct platform_device * pdev)4346 static int unittest_pci_probe(struct platform_device *pdev)
4347 {
4348 struct resource *res;
4349 struct device *dev;
4350 u64 exp_addr;
4351
4352 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4353 if (!res)
4354 return -ENODEV;
4355
4356 dev = &pdev->dev;
4357 while (dev && !dev_is_pci(dev))
4358 dev = dev->parent;
4359 if (!dev) {
4360 pr_err("unable to find parent device\n");
4361 return -ENODEV;
4362 }
4363
4364 exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
4365 unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
4366 (u64)res->start, exp_addr);
4367
4368 of_unittest_pci_child_num++;
4369
4370 return 0;
4371 }
4372
4373 static const struct of_device_id unittest_pci_of_match[] = {
4374 { .compatible = "unittest-pci" },
4375 { }
4376 };
4377
4378 static struct platform_driver unittest_pci_driver = {
4379 .probe = unittest_pci_probe,
4380 .driver = {
4381 .name = "unittest-pci",
4382 .of_match_table = unittest_pci_of_match,
4383 },
4384 };
4385
of_unittest_pci_node_verify(struct pci_dev * pdev,bool add)4386 static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
4387 {
4388 struct device_node *pnp, *np = NULL;
4389 struct device *child_dev;
4390 char *path = NULL;
4391 const __be32 *reg;
4392 int rc = 0;
4393
4394 pnp = pdev->dev.of_node;
4395 unittest(pnp, "Failed creating PCI dt node\n");
4396 if (!pnp)
4397 return -ENODEV;
4398
4399 if (add) {
4400 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
4401 np = of_find_node_by_path(path);
4402 unittest(np, "Failed to get unittest-pci node under PCI node\n");
4403 if (!np) {
4404 rc = -ENODEV;
4405 goto failed;
4406 }
4407
4408 reg = of_get_property(np, "reg", NULL);
4409 unittest(reg, "Failed to get reg property\n");
4410 if (!reg)
4411 rc = -ENODEV;
4412 } else {
4413 path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
4414 np = of_find_node_by_path(path);
4415 unittest(!np, "Child device tree node is not removed\n");
4416 child_dev = device_find_any_child(&pdev->dev);
4417 unittest(!child_dev, "Child device is not removed\n");
4418 put_device(child_dev);
4419 }
4420
4421 failed:
4422 kfree(path);
4423 if (np)
4424 of_node_put(np);
4425
4426 return rc;
4427 }
4428
of_unittest_pci_node(void)4429 static void __init of_unittest_pci_node(void)
4430 {
4431 struct pci_dev *pdev = NULL;
4432 int rc;
4433
4434 if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
4435 return;
4436
4437 rc = pci_register_driver(&testdrv_driver);
4438 unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
4439 if (rc)
4440 return;
4441
4442 rc = platform_driver_register(&unittest_pci_driver);
4443 if (unittest(!rc, "Failed to register unittest pci driver\n")) {
4444 pci_unregister_driver(&testdrv_driver);
4445 return;
4446 }
4447
4448 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
4449 of_unittest_pci_node_verify(pdev, true);
4450 of_unittest_pci_dev_num++;
4451 }
4452 if (pdev)
4453 pci_dev_put(pdev);
4454
4455 unittest(of_unittest_pci_dev_num,
4456 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
4457 unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
4458 "Child device number %d is not expected %d", of_unittest_pci_child_num,
4459 of_unittest_pci_dev_num);
4460
4461 platform_driver_unregister(&unittest_pci_driver);
4462 pci_unregister_driver(&testdrv_driver);
4463
4464 while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
4465 of_unittest_pci_node_verify(pdev, false);
4466 if (pdev)
4467 pci_dev_put(pdev);
4468 }
4469 #else
4470
of_unittest_overlay_high_level(void)4471 static inline __init void of_unittest_overlay_high_level(void) {}
of_unittest_pci_node(void)4472 static inline __init void of_unittest_pci_node(void) { }
4473
4474 #endif
4475
of_unittest(void)4476 static int __init of_unittest(void)
4477 {
4478 struct device_node *np;
4479 int res;
4480
4481 pr_info("start of unittest - you will see error messages\n");
4482
4483 /* Taint the kernel so we know we've run tests. */
4484 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
4485
4486 /* adding data for unittest */
4487 res = unittest_data_add();
4488 if (res)
4489 return res;
4490 if (!of_aliases)
4491 of_aliases = of_find_node_by_path("/aliases");
4492
4493 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4494 if (!np) {
4495 pr_info("No testcase data in device tree; not running tests\n");
4496 return 0;
4497 }
4498 of_node_put(np);
4499
4500 of_unittest_check_tree_linkage();
4501 of_unittest_check_phandles();
4502 of_unittest_find_node_by_name();
4503 of_unittest_dynamic();
4504 of_unittest_parse_phandle_with_args();
4505 of_unittest_parse_phandle_with_args_map();
4506 of_unittest_printf();
4507 of_unittest_property_string();
4508 of_unittest_property_copy();
4509 of_unittest_changeset();
4510 of_unittest_changeset_prop();
4511 of_unittest_parse_interrupts();
4512 of_unittest_parse_interrupts_extended();
4513 of_unittest_parse_interrupt_map();
4514 of_unittest_irq_refcount();
4515 of_unittest_dma_get_max_cpu_address();
4516 of_unittest_parse_dma_ranges();
4517 of_unittest_pci_dma_ranges();
4518 of_unittest_pci_empty_dma_ranges();
4519 of_unittest_bus_ranges();
4520 of_unittest_bus_3cell_ranges();
4521 of_unittest_reg();
4522 of_unittest_translate_addr();
4523 of_unittest_match_node();
4524 of_unittest_platform_populate();
4525 of_unittest_overlay();
4526 of_unittest_lifecycle();
4527 of_unittest_pci_node();
4528
4529 /* Double check linkage after removing testcase data */
4530 of_unittest_check_tree_linkage();
4531
4532 of_unittest_overlay_high_level();
4533
4534 pr_info("end of unittest - %i passed, %i failed\n",
4535 unittest_results.passed, unittest_results.failed);
4536
4537 return 0;
4538 }
4539 late_initcall(of_unittest);
4540