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