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