Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[cascardo/linux.git] / drivers / of / selftest.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/module.h>
11 #include <linux/of.h>
12 #include <linux/of_fdt.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/list.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
19
20 #include "of_private.h"
21
22 static struct selftest_results {
23         int passed;
24         int failed;
25 } selftest_results;
26
27 #define NO_OF_NODES 2
28 static struct device_node *nodes[NO_OF_NODES];
29 static int last_node_index;
30
31 #define selftest(result, fmt, ...) { \
32         if (!(result)) { \
33                 selftest_results.failed++; \
34                 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
35         } else { \
36                 selftest_results.passed++; \
37                 pr_debug("pass %s():%i\n", __func__, __LINE__); \
38         } \
39 }
40
41 static void __init of_selftest_find_node_by_name(void)
42 {
43         struct device_node *np;
44
45         np = of_find_node_by_path("/testcase-data");
46         selftest(np && !strcmp("/testcase-data", np->full_name),
47                 "find /testcase-data failed\n");
48         of_node_put(np);
49
50         /* Test if trailing '/' works */
51         np = of_find_node_by_path("/testcase-data/");
52         selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
53
54         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
55         selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
56                 "find /testcase-data/phandle-tests/consumer-a failed\n");
57         of_node_put(np);
58
59         np = of_find_node_by_path("testcase-alias");
60         selftest(np && !strcmp("/testcase-data", np->full_name),
61                 "find testcase-alias failed\n");
62         of_node_put(np);
63
64         /* Test if trailing '/' works on aliases */
65         np = of_find_node_by_path("testcase-alias/");
66         selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
67
68         np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
69         selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
70                 "find testcase-alias/phandle-tests/consumer-a failed\n");
71         of_node_put(np);
72
73         np = of_find_node_by_path("/testcase-data/missing-path");
74         selftest(!np, "non-existent path returned node %s\n", np->full_name);
75         of_node_put(np);
76
77         np = of_find_node_by_path("missing-alias");
78         selftest(!np, "non-existent alias returned node %s\n", np->full_name);
79         of_node_put(np);
80
81         np = of_find_node_by_path("testcase-alias/missing-path");
82         selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
83         of_node_put(np);
84 }
85
86 static void __init of_selftest_dynamic(void)
87 {
88         struct device_node *np;
89         struct property *prop;
90
91         np = of_find_node_by_path("/testcase-data");
92         if (!np) {
93                 pr_err("missing testcase data\n");
94                 return;
95         }
96
97         /* Array of 4 properties for the purpose of testing */
98         prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
99         if (!prop) {
100                 selftest(0, "kzalloc() failed\n");
101                 return;
102         }
103
104         /* Add a new property - should pass*/
105         prop->name = "new-property";
106         prop->value = "new-property-data";
107         prop->length = strlen(prop->value);
108         selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
109
110         /* Try to add an existing property - should fail */
111         prop++;
112         prop->name = "new-property";
113         prop->value = "new-property-data-should-fail";
114         prop->length = strlen(prop->value);
115         selftest(of_add_property(np, prop) != 0,
116                  "Adding an existing property should have failed\n");
117
118         /* Try to modify an existing property - should pass */
119         prop->value = "modify-property-data-should-pass";
120         prop->length = strlen(prop->value);
121         selftest(of_update_property(np, prop) == 0,
122                  "Updating an existing property should have passed\n");
123
124         /* Try to modify non-existent property - should pass*/
125         prop++;
126         prop->name = "modify-property";
127         prop->value = "modify-missing-property-data-should-pass";
128         prop->length = strlen(prop->value);
129         selftest(of_update_property(np, prop) == 0,
130                  "Updating a missing property should have passed\n");
131
132         /* Remove property - should pass */
133         selftest(of_remove_property(np, prop) == 0,
134                  "Removing a property should have passed\n");
135
136         /* Adding very large property - should pass */
137         prop++;
138         prop->name = "large-property-PAGE_SIZEx8";
139         prop->length = PAGE_SIZE * 8;
140         prop->value = kzalloc(prop->length, GFP_KERNEL);
141         selftest(prop->value != NULL, "Unable to allocate large buffer\n");
142         if (prop->value)
143                 selftest(of_add_property(np, prop) == 0,
144                          "Adding a large property should have passed\n");
145 }
146
147 static void __init of_selftest_parse_phandle_with_args(void)
148 {
149         struct device_node *np;
150         struct of_phandle_args args;
151         int i, rc;
152
153         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
154         if (!np) {
155                 pr_err("missing testcase data\n");
156                 return;
157         }
158
159         rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
160         selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
161
162         for (i = 0; i < 8; i++) {
163                 bool passed = true;
164                 rc = of_parse_phandle_with_args(np, "phandle-list",
165                                                 "#phandle-cells", i, &args);
166
167                 /* Test the values from tests-phandle.dtsi */
168                 switch (i) {
169                 case 0:
170                         passed &= !rc;
171                         passed &= (args.args_count == 1);
172                         passed &= (args.args[0] == (i + 1));
173                         break;
174                 case 1:
175                         passed &= !rc;
176                         passed &= (args.args_count == 2);
177                         passed &= (args.args[0] == (i + 1));
178                         passed &= (args.args[1] == 0);
179                         break;
180                 case 2:
181                         passed &= (rc == -ENOENT);
182                         break;
183                 case 3:
184                         passed &= !rc;
185                         passed &= (args.args_count == 3);
186                         passed &= (args.args[0] == (i + 1));
187                         passed &= (args.args[1] == 4);
188                         passed &= (args.args[2] == 3);
189                         break;
190                 case 4:
191                         passed &= !rc;
192                         passed &= (args.args_count == 2);
193                         passed &= (args.args[0] == (i + 1));
194                         passed &= (args.args[1] == 100);
195                         break;
196                 case 5:
197                         passed &= !rc;
198                         passed &= (args.args_count == 0);
199                         break;
200                 case 6:
201                         passed &= !rc;
202                         passed &= (args.args_count == 1);
203                         passed &= (args.args[0] == (i + 1));
204                         break;
205                 case 7:
206                         passed &= (rc == -ENOENT);
207                         break;
208                 default:
209                         passed = false;
210                 }
211
212                 selftest(passed, "index %i - data error on node %s rc=%i\n",
213                          i, args.np->full_name, rc);
214         }
215
216         /* Check for missing list property */
217         rc = of_parse_phandle_with_args(np, "phandle-list-missing",
218                                         "#phandle-cells", 0, &args);
219         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
220         rc = of_count_phandle_with_args(np, "phandle-list-missing",
221                                         "#phandle-cells");
222         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
223
224         /* Check for missing cells property */
225         rc = of_parse_phandle_with_args(np, "phandle-list",
226                                         "#phandle-cells-missing", 0, &args);
227         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
228         rc = of_count_phandle_with_args(np, "phandle-list",
229                                         "#phandle-cells-missing");
230         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
231
232         /* Check for bad phandle in list */
233         rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
234                                         "#phandle-cells", 0, &args);
235         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
236         rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
237                                         "#phandle-cells");
238         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
239
240         /* Check for incorrectly formed argument list */
241         rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
242                                         "#phandle-cells", 1, &args);
243         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
244         rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
245                                         "#phandle-cells");
246         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
247 }
248
249 static void __init of_selftest_property_match_string(void)
250 {
251         struct device_node *np;
252         int rc;
253
254         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
255         if (!np) {
256                 pr_err("No testcase data in device tree\n");
257                 return;
258         }
259
260         rc = of_property_match_string(np, "phandle-list-names", "first");
261         selftest(rc == 0, "first expected:0 got:%i\n", rc);
262         rc = of_property_match_string(np, "phandle-list-names", "second");
263         selftest(rc == 1, "second expected:0 got:%i\n", rc);
264         rc = of_property_match_string(np, "phandle-list-names", "third");
265         selftest(rc == 2, "third expected:0 got:%i\n", rc);
266         rc = of_property_match_string(np, "phandle-list-names", "fourth");
267         selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
268         rc = of_property_match_string(np, "missing-property", "blah");
269         selftest(rc == -EINVAL, "missing property; rc=%i", rc);
270         rc = of_property_match_string(np, "empty-property", "blah");
271         selftest(rc == -ENODATA, "empty property; rc=%i", rc);
272         rc = of_property_match_string(np, "unterminated-string", "blah");
273         selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
274 }
275
276 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
277                         (p1)->value && (p2)->value && \
278                         !memcmp((p1)->value, (p2)->value, (p1)->length) && \
279                         !strcmp((p1)->name, (p2)->name))
280 static void __init of_selftest_property_copy(void)
281 {
282 #ifdef CONFIG_OF_DYNAMIC
283         struct property p1 = { .name = "p1", .length = 0, .value = "" };
284         struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
285         struct property *new;
286
287         new = __of_prop_dup(&p1, GFP_KERNEL);
288         selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
289         kfree(new->value);
290         kfree(new->name);
291         kfree(new);
292
293         new = __of_prop_dup(&p2, GFP_KERNEL);
294         selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
295         kfree(new->value);
296         kfree(new->name);
297         kfree(new);
298 #endif
299 }
300
301 static void __init of_selftest_changeset(void)
302 {
303 #ifdef CONFIG_OF_DYNAMIC
304         struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
305         struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
306         struct property *ppremove;
307         struct device_node *n1, *n2, *n21, *nremove, *parent;
308         struct of_changeset chgset;
309
310         of_changeset_init(&chgset);
311         n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
312         selftest(n1, "testcase setup failure\n");
313         n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
314         selftest(n2, "testcase setup failure\n");
315         n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
316         selftest(n21, "testcase setup failure %p\n", n21);
317         nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
318         selftest(nremove, "testcase setup failure\n");
319         ppadd = __of_prop_dup(&padd, GFP_KERNEL);
320         selftest(ppadd, "testcase setup failure\n");
321         ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
322         selftest(ppupdate, "testcase setup failure\n");
323         parent = nremove->parent;
324         n1->parent = parent;
325         n2->parent = parent;
326         n21->parent = n2;
327         n2->child = n21;
328         ppremove = of_find_property(parent, "prop-remove", NULL);
329         selftest(ppremove, "failed to find removal prop");
330
331         of_changeset_init(&chgset);
332         selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
333         selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
334         selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
335         selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
336         selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
337         selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
338         selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
339         mutex_lock(&of_mutex);
340         selftest(!of_changeset_apply(&chgset), "apply failed\n");
341         mutex_unlock(&of_mutex);
342
343         mutex_lock(&of_mutex);
344         selftest(!of_changeset_revert(&chgset), "revert failed\n");
345         mutex_unlock(&of_mutex);
346
347         of_changeset_destroy(&chgset);
348 #endif
349 }
350
351 static void __init of_selftest_parse_interrupts(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/interrupts/interrupts0");
358         if (!np) {
359                 pr_err("missing testcase data\n");
360                 return;
361         }
362
363         for (i = 0; i < 4; i++) {
364                 bool passed = true;
365                 args.args_count = 0;
366                 rc = of_irq_parse_one(np, i, &args);
367
368                 passed &= !rc;
369                 passed &= (args.args_count == 1);
370                 passed &= (args.args[0] == (i + 1));
371
372                 selftest(passed, "index %i - data error on node %s rc=%i\n",
373                          i, args.np->full_name, rc);
374         }
375         of_node_put(np);
376
377         np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
378         if (!np) {
379                 pr_err("missing testcase data\n");
380                 return;
381         }
382
383         for (i = 0; i < 4; i++) {
384                 bool passed = true;
385                 args.args_count = 0;
386                 rc = of_irq_parse_one(np, i, &args);
387
388                 /* Test the values from tests-phandle.dtsi */
389                 switch (i) {
390                 case 0:
391                         passed &= !rc;
392                         passed &= (args.args_count == 1);
393                         passed &= (args.args[0] == 9);
394                         break;
395                 case 1:
396                         passed &= !rc;
397                         passed &= (args.args_count == 3);
398                         passed &= (args.args[0] == 10);
399                         passed &= (args.args[1] == 11);
400                         passed &= (args.args[2] == 12);
401                         break;
402                 case 2:
403                         passed &= !rc;
404                         passed &= (args.args_count == 2);
405                         passed &= (args.args[0] == 13);
406                         passed &= (args.args[1] == 14);
407                         break;
408                 case 3:
409                         passed &= !rc;
410                         passed &= (args.args_count == 2);
411                         passed &= (args.args[0] == 15);
412                         passed &= (args.args[1] == 16);
413                         break;
414                 default:
415                         passed = false;
416                 }
417                 selftest(passed, "index %i - data error on node %s rc=%i\n",
418                          i, args.np->full_name, rc);
419         }
420         of_node_put(np);
421 }
422
423 static void __init of_selftest_parse_interrupts_extended(void)
424 {
425         struct device_node *np;
426         struct of_phandle_args args;
427         int i, rc;
428
429         np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
430         if (!np) {
431                 pr_err("missing testcase data\n");
432                 return;
433         }
434
435         for (i = 0; i < 7; i++) {
436                 bool passed = true;
437                 rc = of_irq_parse_one(np, i, &args);
438
439                 /* Test the values from tests-phandle.dtsi */
440                 switch (i) {
441                 case 0:
442                         passed &= !rc;
443                         passed &= (args.args_count == 1);
444                         passed &= (args.args[0] == 1);
445                         break;
446                 case 1:
447                         passed &= !rc;
448                         passed &= (args.args_count == 3);
449                         passed &= (args.args[0] == 2);
450                         passed &= (args.args[1] == 3);
451                         passed &= (args.args[2] == 4);
452                         break;
453                 case 2:
454                         passed &= !rc;
455                         passed &= (args.args_count == 2);
456                         passed &= (args.args[0] == 5);
457                         passed &= (args.args[1] == 6);
458                         break;
459                 case 3:
460                         passed &= !rc;
461                         passed &= (args.args_count == 1);
462                         passed &= (args.args[0] == 9);
463                         break;
464                 case 4:
465                         passed &= !rc;
466                         passed &= (args.args_count == 3);
467                         passed &= (args.args[0] == 10);
468                         passed &= (args.args[1] == 11);
469                         passed &= (args.args[2] == 12);
470                         break;
471                 case 5:
472                         passed &= !rc;
473                         passed &= (args.args_count == 2);
474                         passed &= (args.args[0] == 13);
475                         passed &= (args.args[1] == 14);
476                         break;
477                 case 6:
478                         passed &= !rc;
479                         passed &= (args.args_count == 1);
480                         passed &= (args.args[0] == 15);
481                         break;
482                 default:
483                         passed = false;
484                 }
485
486                 selftest(passed, "index %i - data error on node %s rc=%i\n",
487                          i, args.np->full_name, rc);
488         }
489         of_node_put(np);
490 }
491
492 static struct of_device_id match_node_table[] = {
493         { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
494         { .data = "B", .type = "type1", }, /* followed by type alone */
495
496         { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
497         { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
498         { .data = "Cc", .name = "name2", .type = "type2", },
499
500         { .data = "E", .compatible = "compat3" },
501         { .data = "G", .compatible = "compat2", },
502         { .data = "H", .compatible = "compat2", .name = "name5", },
503         { .data = "I", .compatible = "compat2", .type = "type1", },
504         { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
505         { .data = "K", .compatible = "compat2", .name = "name9", },
506         {}
507 };
508
509 static struct {
510         const char *path;
511         const char *data;
512 } match_node_tests[] = {
513         { .path = "/testcase-data/match-node/name0", .data = "A", },
514         { .path = "/testcase-data/match-node/name1", .data = "B", },
515         { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
516         { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
517         { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
518         { .path = "/testcase-data/match-node/name3", .data = "E", },
519         { .path = "/testcase-data/match-node/name4", .data = "G", },
520         { .path = "/testcase-data/match-node/name5", .data = "H", },
521         { .path = "/testcase-data/match-node/name6", .data = "G", },
522         { .path = "/testcase-data/match-node/name7", .data = "I", },
523         { .path = "/testcase-data/match-node/name8", .data = "J", },
524         { .path = "/testcase-data/match-node/name9", .data = "K", },
525 };
526
527 static void __init of_selftest_match_node(void)
528 {
529         struct device_node *np;
530         const struct of_device_id *match;
531         int i;
532
533         for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
534                 np = of_find_node_by_path(match_node_tests[i].path);
535                 if (!np) {
536                         selftest(0, "missing testcase node %s\n",
537                                 match_node_tests[i].path);
538                         continue;
539                 }
540
541                 match = of_match_node(match_node_table, np);
542                 if (!match) {
543                         selftest(0, "%s didn't match anything\n",
544                                 match_node_tests[i].path);
545                         continue;
546                 }
547
548                 if (strcmp(match->data, match_node_tests[i].data) != 0) {
549                         selftest(0, "%s got wrong match. expected %s, got %s\n",
550                                 match_node_tests[i].path, match_node_tests[i].data,
551                                 (const char *)match->data);
552                         continue;
553                 }
554                 selftest(1, "passed");
555         }
556 }
557
558 static void __init of_selftest_platform_populate(void)
559 {
560         int irq;
561         struct device_node *np, *child;
562         struct platform_device *pdev;
563         struct of_device_id match[] = {
564                 { .compatible = "test-device", },
565                 {}
566         };
567
568         np = of_find_node_by_path("/testcase-data");
569         of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
570
571         /* Test that a missing irq domain returns -EPROBE_DEFER */
572         np = of_find_node_by_path("/testcase-data/testcase-device1");
573         pdev = of_find_device_by_node(np);
574         selftest(pdev, "device 1 creation failed\n");
575
576         irq = platform_get_irq(pdev, 0);
577         selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
578
579         /* Test that a parsing failure does not return -EPROBE_DEFER */
580         np = of_find_node_by_path("/testcase-data/testcase-device2");
581         pdev = of_find_device_by_node(np);
582         selftest(pdev, "device 2 creation failed\n");
583         irq = platform_get_irq(pdev, 0);
584         selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
585
586         np = of_find_node_by_path("/testcase-data/platform-tests");
587         if (!np) {
588                 pr_err("No testcase data in device tree\n");
589                 return;
590         }
591
592         for_each_child_of_node(np, child) {
593                 struct device_node *grandchild;
594                 of_platform_populate(child, match, NULL, NULL);
595                 for_each_child_of_node(child, grandchild)
596                         selftest(of_find_device_by_node(grandchild),
597                                  "Could not create device for node '%s'\n",
598                                  grandchild->name);
599         }
600 }
601
602 /**
603  *      update_node_properties - adds the properties
604  *      of np into dup node (present in live tree) and
605  *      updates parent of children of np to dup.
606  *
607  *      @np:    node already present in live tree
608  *      @dup:   node present in live tree to be updated
609  */
610 static void update_node_properties(struct device_node *np,
611                                         struct device_node *dup)
612 {
613         struct property *prop;
614         struct device_node *child;
615
616         for_each_property_of_node(np, prop)
617                 of_add_property(dup, prop);
618
619         for_each_child_of_node(np, child)
620                 child->parent = dup;
621 }
622
623 /**
624  *      attach_node_and_children - attaches nodes
625  *      and its children to live tree
626  *
627  *      @np:    Node to attach to live tree
628  */
629 static int attach_node_and_children(struct device_node *np)
630 {
631         struct device_node *next, *root = np, *dup;
632
633         if (!np) {
634                 pr_warn("%s: No tree to attach; not running tests\n",
635                         __func__);
636                 return -ENODATA;
637         }
638
639
640         /* skip root node */
641         np = np->child;
642         /* storing a copy in temporary node */
643         dup = np;
644
645         while (dup) {
646                 nodes[last_node_index++] = dup;
647                 dup = dup->sibling;
648         }
649         dup = NULL;
650
651         while (np) {
652                 next = np->allnext;
653                 dup = of_find_node_by_path(np->full_name);
654                 if (dup)
655                         update_node_properties(np, dup);
656                 else {
657                         np->child = NULL;
658                         if (np->parent == root)
659                                 np->parent = of_allnodes;
660                         of_attach_node(np);
661                 }
662                 np = next;
663         }
664
665         return 0;
666 }
667
668 /**
669  *      selftest_data_add - Reads, copies data from
670  *      linked tree and attaches it to the live tree
671  */
672 static int __init selftest_data_add(void)
673 {
674         void *selftest_data;
675         struct device_node *selftest_data_node;
676         extern uint8_t __dtb_testcases_begin[];
677         extern uint8_t __dtb_testcases_end[];
678         const int size = __dtb_testcases_end - __dtb_testcases_begin;
679
680         if (!size || !of_allnodes) {
681                 pr_warn("%s: No testcase data to attach; not running tests\n",
682                         __func__);
683                 return -ENODATA;
684         }
685
686         /* creating copy */
687         selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
688
689         if (!selftest_data) {
690                 pr_warn("%s: Failed to allocate memory for selftest_data; "
691                         "not running tests\n", __func__);
692                 return -ENOMEM;
693         }
694         of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
695
696         /* attach the sub-tree to live tree */
697         return attach_node_and_children(selftest_data_node);
698 }
699
700 /**
701  *      detach_node_and_children - detaches node
702  *      and its children from live tree
703  *
704  *      @np:    Node to detach from live tree
705  */
706 static void detach_node_and_children(struct device_node *np)
707 {
708         while (np->child)
709                 detach_node_and_children(np->child);
710
711         while (np->sibling)
712                 detach_node_and_children(np->sibling);
713
714         of_detach_node(np);
715 }
716
717 /**
718  *      selftest_data_remove - removes the selftest data
719  *      nodes from the live tree
720  */
721 static void selftest_data_remove(void)
722 {
723         struct device_node *np;
724         struct property *prop;
725
726         while (last_node_index >= 0) {
727                 if (nodes[last_node_index]) {
728                         np = of_find_node_by_path(nodes[last_node_index]->full_name);
729                         if (strcmp(np->full_name, "/aliases") != 0) {
730                                 detach_node_and_children(np->child);
731                                 of_detach_node(np);
732                         } else {
733                                 for_each_property_of_node(np, prop) {
734                                         if (strcmp(prop->name, "testcase-alias") == 0)
735                                                 of_remove_property(np, prop);
736                                 }
737                         }
738                 }
739                 last_node_index--;
740         }
741 }
742
743 static int __init of_selftest(void)
744 {
745         struct device_node *np;
746         int res;
747
748         /* adding data for selftest */
749         res = selftest_data_add();
750         if (res)
751                 return res;
752
753         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
754         if (!np) {
755                 pr_info("No testcase data in device tree; not running tests\n");
756                 return 0;
757         }
758         of_node_put(np);
759
760         pr_info("start of selftest - you will see error messages\n");
761         of_selftest_find_node_by_name();
762         of_selftest_dynamic();
763         of_selftest_parse_phandle_with_args();
764         of_selftest_property_match_string();
765         of_selftest_property_copy();
766         of_selftest_changeset();
767         of_selftest_parse_interrupts();
768         of_selftest_parse_interrupts_extended();
769         of_selftest_match_node();
770         of_selftest_platform_populate();
771         pr_info("end of selftest - %i passed, %i failed\n",
772                 selftest_results.passed, selftest_results.failed);
773
774         /* removing selftest data from live tree */
775         selftest_data_remove();
776
777         return 0;
778 }
779 late_initcall(of_selftest);