]> Git Repo - linux.git/blob - drivers/of/unittest.c
of/unittest: Remove obsolete code
[linux.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/module.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 #include <linux/of_platform.h>
22
23 #include "of_private.h"
24
25 static struct selftest_results {
26         int passed;
27         int failed;
28 } selftest_results;
29
30 #define selftest(result, fmt, ...) ({ \
31         bool failed = !(result); \
32         if (failed) { \
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         failed; \
40 })
41
42 static void __init of_selftest_find_node_by_name(void)
43 {
44         struct device_node *np;
45         const char *options;
46
47         np = of_find_node_by_path("/testcase-data");
48         selftest(np && !strcmp("/testcase-data", np->full_name),
49                 "find /testcase-data failed\n");
50         of_node_put(np);
51
52         /* Test if trailing '/' works */
53         np = of_find_node_by_path("/testcase-data/");
54         selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
55
56         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
57         selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
58                 "find /testcase-data/phandle-tests/consumer-a failed\n");
59         of_node_put(np);
60
61         np = of_find_node_by_path("testcase-alias");
62         selftest(np && !strcmp("/testcase-data", np->full_name),
63                 "find testcase-alias failed\n");
64         of_node_put(np);
65
66         /* Test if trailing '/' works on aliases */
67         np = of_find_node_by_path("testcase-alias/");
68         selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
69
70         np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
71         selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
72                 "find testcase-alias/phandle-tests/consumer-a failed\n");
73         of_node_put(np);
74
75         np = of_find_node_by_path("/testcase-data/missing-path");
76         selftest(!np, "non-existent path returned node %s\n", np->full_name);
77         of_node_put(np);
78
79         np = of_find_node_by_path("missing-alias");
80         selftest(!np, "non-existent alias returned node %s\n", np->full_name);
81         of_node_put(np);
82
83         np = of_find_node_by_path("testcase-alias/missing-path");
84         selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
85         of_node_put(np);
86
87         np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
88         selftest(np && !strcmp("testoption", options),
89                  "option path test failed\n");
90         of_node_put(np);
91
92         np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
93         selftest(np, "NULL option path test failed\n");
94         of_node_put(np);
95
96         np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
97                                        &options);
98         selftest(np && !strcmp("testaliasoption", options),
99                  "option alias path test failed\n");
100         of_node_put(np);
101
102         np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
103         selftest(np, "NULL option alias path test failed\n");
104         of_node_put(np);
105
106         options = "testoption";
107         np = of_find_node_opts_by_path("testcase-alias", &options);
108         selftest(np && !options, "option clearing test failed\n");
109         of_node_put(np);
110
111         options = "testoption";
112         np = of_find_node_opts_by_path("/", &options);
113         selftest(np && !options, "option clearing root node test failed\n");
114         of_node_put(np);
115 }
116
117 static void __init of_selftest_dynamic(void)
118 {
119         struct device_node *np;
120         struct property *prop;
121
122         np = of_find_node_by_path("/testcase-data");
123         if (!np) {
124                 pr_err("missing testcase data\n");
125                 return;
126         }
127
128         /* Array of 4 properties for the purpose of testing */
129         prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
130         if (!prop) {
131                 selftest(0, "kzalloc() failed\n");
132                 return;
133         }
134
135         /* Add a new property - should pass*/
136         prop->name = "new-property";
137         prop->value = "new-property-data";
138         prop->length = strlen(prop->value);
139         selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
140
141         /* Try to add an existing property - should fail */
142         prop++;
143         prop->name = "new-property";
144         prop->value = "new-property-data-should-fail";
145         prop->length = strlen(prop->value);
146         selftest(of_add_property(np, prop) != 0,
147                  "Adding an existing property should have failed\n");
148
149         /* Try to modify an existing property - should pass */
150         prop->value = "modify-property-data-should-pass";
151         prop->length = strlen(prop->value);
152         selftest(of_update_property(np, prop) == 0,
153                  "Updating an existing property should have passed\n");
154
155         /* Try to modify non-existent property - should pass*/
156         prop++;
157         prop->name = "modify-property";
158         prop->value = "modify-missing-property-data-should-pass";
159         prop->length = strlen(prop->value);
160         selftest(of_update_property(np, prop) == 0,
161                  "Updating a missing property should have passed\n");
162
163         /* Remove property - should pass */
164         selftest(of_remove_property(np, prop) == 0,
165                  "Removing a property should have passed\n");
166
167         /* Adding very large property - should pass */
168         prop++;
169         prop->name = "large-property-PAGE_SIZEx8";
170         prop->length = PAGE_SIZE * 8;
171         prop->value = kzalloc(prop->length, GFP_KERNEL);
172         selftest(prop->value != NULL, "Unable to allocate large buffer\n");
173         if (prop->value)
174                 selftest(of_add_property(np, prop) == 0,
175                          "Adding a large property should have passed\n");
176 }
177
178 static int __init of_selftest_check_node_linkage(struct device_node *np)
179 {
180         struct device_node *child;
181         int count = 0, rc;
182
183         for_each_child_of_node(np, child) {
184                 if (child->parent != np) {
185                         pr_err("Child node %s links to wrong parent %s\n",
186                                  child->name, np->name);
187                         return -EINVAL;
188                 }
189
190                 rc = of_selftest_check_node_linkage(child);
191                 if (rc < 0)
192                         return rc;
193                 count += rc;
194         }
195
196         return count + 1;
197 }
198
199 static void __init of_selftest_check_tree_linkage(void)
200 {
201         struct device_node *np;
202         int allnode_count = 0, child_count;
203
204         if (!of_root)
205                 return;
206
207         for_each_of_allnodes(np)
208                 allnode_count++;
209         child_count = of_selftest_check_node_linkage(of_root);
210
211         selftest(child_count > 0, "Device node data structure is corrupted\n");
212         selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
213                  "sibling lists size (%i)\n", allnode_count, child_count);
214         pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
215 }
216
217 struct node_hash {
218         struct hlist_node node;
219         struct device_node *np;
220 };
221
222 static DEFINE_HASHTABLE(phandle_ht, 8);
223 static void __init of_selftest_check_phandles(void)
224 {
225         struct device_node *np;
226         struct node_hash *nh;
227         struct hlist_node *tmp;
228         int i, dup_count = 0, phandle_count = 0;
229
230         for_each_of_allnodes(np) {
231                 if (!np->phandle)
232                         continue;
233
234                 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
235                         if (nh->np->phandle == np->phandle) {
236                                 pr_info("Duplicate phandle! %i used by %s and %s\n",
237                                         np->phandle, nh->np->full_name, np->full_name);
238                                 dup_count++;
239                                 break;
240                         }
241                 }
242
243                 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
244                 if (WARN_ON(!nh))
245                         return;
246
247                 nh->np = np;
248                 hash_add(phandle_ht, &nh->node, np->phandle);
249                 phandle_count++;
250         }
251         selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
252                  dup_count, phandle_count);
253
254         /* Clean up */
255         hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
256                 hash_del(&nh->node);
257                 kfree(nh);
258         }
259 }
260
261 static void __init of_selftest_parse_phandle_with_args(void)
262 {
263         struct device_node *np;
264         struct of_phandle_args args;
265         int i, rc;
266
267         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
268         if (!np) {
269                 pr_err("missing testcase data\n");
270                 return;
271         }
272
273         rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
274         selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
275
276         for (i = 0; i < 8; i++) {
277                 bool passed = true;
278                 rc = of_parse_phandle_with_args(np, "phandle-list",
279                                                 "#phandle-cells", i, &args);
280
281                 /* Test the values from tests-phandle.dtsi */
282                 switch (i) {
283                 case 0:
284                         passed &= !rc;
285                         passed &= (args.args_count == 1);
286                         passed &= (args.args[0] == (i + 1));
287                         break;
288                 case 1:
289                         passed &= !rc;
290                         passed &= (args.args_count == 2);
291                         passed &= (args.args[0] == (i + 1));
292                         passed &= (args.args[1] == 0);
293                         break;
294                 case 2:
295                         passed &= (rc == -ENOENT);
296                         break;
297                 case 3:
298                         passed &= !rc;
299                         passed &= (args.args_count == 3);
300                         passed &= (args.args[0] == (i + 1));
301                         passed &= (args.args[1] == 4);
302                         passed &= (args.args[2] == 3);
303                         break;
304                 case 4:
305                         passed &= !rc;
306                         passed &= (args.args_count == 2);
307                         passed &= (args.args[0] == (i + 1));
308                         passed &= (args.args[1] == 100);
309                         break;
310                 case 5:
311                         passed &= !rc;
312                         passed &= (args.args_count == 0);
313                         break;
314                 case 6:
315                         passed &= !rc;
316                         passed &= (args.args_count == 1);
317                         passed &= (args.args[0] == (i + 1));
318                         break;
319                 case 7:
320                         passed &= (rc == -ENOENT);
321                         break;
322                 default:
323                         passed = false;
324                 }
325
326                 selftest(passed, "index %i - data error on node %s rc=%i\n",
327                          i, args.np->full_name, rc);
328         }
329
330         /* Check for missing list property */
331         rc = of_parse_phandle_with_args(np, "phandle-list-missing",
332                                         "#phandle-cells", 0, &args);
333         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
334         rc = of_count_phandle_with_args(np, "phandle-list-missing",
335                                         "#phandle-cells");
336         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
337
338         /* Check for missing cells property */
339         rc = of_parse_phandle_with_args(np, "phandle-list",
340                                         "#phandle-cells-missing", 0, &args);
341         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
342         rc = of_count_phandle_with_args(np, "phandle-list",
343                                         "#phandle-cells-missing");
344         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
345
346         /* Check for bad phandle in list */
347         rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
348                                         "#phandle-cells", 0, &args);
349         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
350         rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
351                                         "#phandle-cells");
352         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
353
354         /* Check for incorrectly formed argument list */
355         rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
356                                         "#phandle-cells", 1, &args);
357         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
358         rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
359                                         "#phandle-cells");
360         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
361 }
362
363 static void __init of_selftest_property_string(void)
364 {
365         const char *strings[4];
366         struct device_node *np;
367         int rc;
368
369         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
370         if (!np) {
371                 pr_err("No testcase data in device tree\n");
372                 return;
373         }
374
375         rc = of_property_match_string(np, "phandle-list-names", "first");
376         selftest(rc == 0, "first expected:0 got:%i\n", rc);
377         rc = of_property_match_string(np, "phandle-list-names", "second");
378         selftest(rc == 1, "second expected:0 got:%i\n", rc);
379         rc = of_property_match_string(np, "phandle-list-names", "third");
380         selftest(rc == 2, "third expected:0 got:%i\n", rc);
381         rc = of_property_match_string(np, "phandle-list-names", "fourth");
382         selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
383         rc = of_property_match_string(np, "missing-property", "blah");
384         selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
385         rc = of_property_match_string(np, "empty-property", "blah");
386         selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
387         rc = of_property_match_string(np, "unterminated-string", "blah");
388         selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
389
390         /* of_property_count_strings() tests */
391         rc = of_property_count_strings(np, "string-property");
392         selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
393         rc = of_property_count_strings(np, "phandle-list-names");
394         selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
395         rc = of_property_count_strings(np, "unterminated-string");
396         selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
397         rc = of_property_count_strings(np, "unterminated-string-list");
398         selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
399
400         /* of_property_read_string_index() tests */
401         rc = of_property_read_string_index(np, "string-property", 0, strings);
402         selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
403         strings[0] = NULL;
404         rc = of_property_read_string_index(np, "string-property", 1, strings);
405         selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
406         rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
407         selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
408         rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
409         selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
410         rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
411         selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
412         strings[0] = NULL;
413         rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
414         selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
415         strings[0] = NULL;
416         rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
417         selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
418         rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
419         selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
420         strings[0] = NULL;
421         rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
422         selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
423         strings[1] = NULL;
424
425         /* of_property_read_string_array() tests */
426         rc = of_property_read_string_array(np, "string-property", strings, 4);
427         selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
428         rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
429         selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
430         rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
431         selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
432         /* -- An incorrectly formed string should cause a failure */
433         rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
434         selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
435         /* -- parsing the correctly formed strings should still work: */
436         strings[2] = NULL;
437         rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
438         selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
439         strings[1] = NULL;
440         rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
441         selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
442 }
443
444 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
445                         (p1)->value && (p2)->value && \
446                         !memcmp((p1)->value, (p2)->value, (p1)->length) && \
447                         !strcmp((p1)->name, (p2)->name))
448 static void __init of_selftest_property_copy(void)
449 {
450 #ifdef CONFIG_OF_DYNAMIC
451         struct property p1 = { .name = "p1", .length = 0, .value = "" };
452         struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
453         struct property *new;
454
455         new = __of_prop_dup(&p1, GFP_KERNEL);
456         selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
457         kfree(new->value);
458         kfree(new->name);
459         kfree(new);
460
461         new = __of_prop_dup(&p2, GFP_KERNEL);
462         selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
463         kfree(new->value);
464         kfree(new->name);
465         kfree(new);
466 #endif
467 }
468
469 static void __init of_selftest_changeset(void)
470 {
471 #ifdef CONFIG_OF_DYNAMIC
472         struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
473         struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
474         struct property *ppremove;
475         struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
476         struct of_changeset chgset;
477
478         of_changeset_init(&chgset);
479         n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
480         selftest(n1, "testcase setup failure\n");
481         n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
482         selftest(n2, "testcase setup failure\n");
483         n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
484         selftest(n21, "testcase setup failure %p\n", n21);
485         nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
486         selftest(nremove, "testcase setup failure\n");
487         ppadd = __of_prop_dup(&padd, GFP_KERNEL);
488         selftest(ppadd, "testcase setup failure\n");
489         ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
490         selftest(ppupdate, "testcase setup failure\n");
491         parent = nremove->parent;
492         n1->parent = parent;
493         n2->parent = parent;
494         n21->parent = n2;
495         n2->child = n21;
496         ppremove = of_find_property(parent, "prop-remove", NULL);
497         selftest(ppremove, "failed to find removal prop");
498
499         of_changeset_init(&chgset);
500         selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
501         selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
502         selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
503         selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
504         selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
505         selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
506         selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
507         mutex_lock(&of_mutex);
508         selftest(!of_changeset_apply(&chgset), "apply failed\n");
509         mutex_unlock(&of_mutex);
510
511         /* Make sure node names are constructed correctly */
512         selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
513                  "'%s' not added\n", n21->full_name);
514         of_node_put(np);
515
516         mutex_lock(&of_mutex);
517         selftest(!of_changeset_revert(&chgset), "revert failed\n");
518         mutex_unlock(&of_mutex);
519
520         of_changeset_destroy(&chgset);
521 #endif
522 }
523
524 static void __init of_selftest_parse_interrupts(void)
525 {
526         struct device_node *np;
527         struct of_phandle_args args;
528         int i, rc;
529
530         np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
531         if (!np) {
532                 pr_err("missing testcase data\n");
533                 return;
534         }
535
536         for (i = 0; i < 4; i++) {
537                 bool passed = true;
538                 args.args_count = 0;
539                 rc = of_irq_parse_one(np, i, &args);
540
541                 passed &= !rc;
542                 passed &= (args.args_count == 1);
543                 passed &= (args.args[0] == (i + 1));
544
545                 selftest(passed, "index %i - data error on node %s rc=%i\n",
546                          i, args.np->full_name, rc);
547         }
548         of_node_put(np);
549
550         np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
551         if (!np) {
552                 pr_err("missing testcase data\n");
553                 return;
554         }
555
556         for (i = 0; i < 4; i++) {
557                 bool passed = true;
558                 args.args_count = 0;
559                 rc = of_irq_parse_one(np, i, &args);
560
561                 /* Test the values from tests-phandle.dtsi */
562                 switch (i) {
563                 case 0:
564                         passed &= !rc;
565                         passed &= (args.args_count == 1);
566                         passed &= (args.args[0] == 9);
567                         break;
568                 case 1:
569                         passed &= !rc;
570                         passed &= (args.args_count == 3);
571                         passed &= (args.args[0] == 10);
572                         passed &= (args.args[1] == 11);
573                         passed &= (args.args[2] == 12);
574                         break;
575                 case 2:
576                         passed &= !rc;
577                         passed &= (args.args_count == 2);
578                         passed &= (args.args[0] == 13);
579                         passed &= (args.args[1] == 14);
580                         break;
581                 case 3:
582                         passed &= !rc;
583                         passed &= (args.args_count == 2);
584                         passed &= (args.args[0] == 15);
585                         passed &= (args.args[1] == 16);
586                         break;
587                 default:
588                         passed = false;
589                 }
590                 selftest(passed, "index %i - data error on node %s rc=%i\n",
591                          i, args.np->full_name, rc);
592         }
593         of_node_put(np);
594 }
595
596 static void __init of_selftest_parse_interrupts_extended(void)
597 {
598         struct device_node *np;
599         struct of_phandle_args args;
600         int i, rc;
601
602         np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
603         if (!np) {
604                 pr_err("missing testcase data\n");
605                 return;
606         }
607
608         for (i = 0; i < 7; i++) {
609                 bool passed = true;
610                 rc = of_irq_parse_one(np, i, &args);
611
612                 /* Test the values from tests-phandle.dtsi */
613                 switch (i) {
614                 case 0:
615                         passed &= !rc;
616                         passed &= (args.args_count == 1);
617                         passed &= (args.args[0] == 1);
618                         break;
619                 case 1:
620                         passed &= !rc;
621                         passed &= (args.args_count == 3);
622                         passed &= (args.args[0] == 2);
623                         passed &= (args.args[1] == 3);
624                         passed &= (args.args[2] == 4);
625                         break;
626                 case 2:
627                         passed &= !rc;
628                         passed &= (args.args_count == 2);
629                         passed &= (args.args[0] == 5);
630                         passed &= (args.args[1] == 6);
631                         break;
632                 case 3:
633                         passed &= !rc;
634                         passed &= (args.args_count == 1);
635                         passed &= (args.args[0] == 9);
636                         break;
637                 case 4:
638                         passed &= !rc;
639                         passed &= (args.args_count == 3);
640                         passed &= (args.args[0] == 10);
641                         passed &= (args.args[1] == 11);
642                         passed &= (args.args[2] == 12);
643                         break;
644                 case 5:
645                         passed &= !rc;
646                         passed &= (args.args_count == 2);
647                         passed &= (args.args[0] == 13);
648                         passed &= (args.args[1] == 14);
649                         break;
650                 case 6:
651                         passed &= !rc;
652                         passed &= (args.args_count == 1);
653                         passed &= (args.args[0] == 15);
654                         break;
655                 default:
656                         passed = false;
657                 }
658
659                 selftest(passed, "index %i - data error on node %s rc=%i\n",
660                          i, args.np->full_name, rc);
661         }
662         of_node_put(np);
663 }
664
665 static struct of_device_id match_node_table[] = {
666         { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
667         { .data = "B", .type = "type1", }, /* followed by type alone */
668
669         { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
670         { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
671         { .data = "Cc", .name = "name2", .type = "type2", },
672
673         { .data = "E", .compatible = "compat3" },
674         { .data = "G", .compatible = "compat2", },
675         { .data = "H", .compatible = "compat2", .name = "name5", },
676         { .data = "I", .compatible = "compat2", .type = "type1", },
677         { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
678         { .data = "K", .compatible = "compat2", .name = "name9", },
679         {}
680 };
681
682 static struct {
683         const char *path;
684         const char *data;
685 } match_node_tests[] = {
686         { .path = "/testcase-data/match-node/name0", .data = "A", },
687         { .path = "/testcase-data/match-node/name1", .data = "B", },
688         { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
689         { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
690         { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
691         { .path = "/testcase-data/match-node/name3", .data = "E", },
692         { .path = "/testcase-data/match-node/name4", .data = "G", },
693         { .path = "/testcase-data/match-node/name5", .data = "H", },
694         { .path = "/testcase-data/match-node/name6", .data = "G", },
695         { .path = "/testcase-data/match-node/name7", .data = "I", },
696         { .path = "/testcase-data/match-node/name8", .data = "J", },
697         { .path = "/testcase-data/match-node/name9", .data = "K", },
698 };
699
700 static void __init of_selftest_match_node(void)
701 {
702         struct device_node *np;
703         const struct of_device_id *match;
704         int i;
705
706         for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
707                 np = of_find_node_by_path(match_node_tests[i].path);
708                 if (!np) {
709                         selftest(0, "missing testcase node %s\n",
710                                 match_node_tests[i].path);
711                         continue;
712                 }
713
714                 match = of_match_node(match_node_table, np);
715                 if (!match) {
716                         selftest(0, "%s didn't match anything\n",
717                                 match_node_tests[i].path);
718                         continue;
719                 }
720
721                 if (strcmp(match->data, match_node_tests[i].data) != 0) {
722                         selftest(0, "%s got wrong match. expected %s, got %s\n",
723                                 match_node_tests[i].path, match_node_tests[i].data,
724                                 (const char *)match->data);
725                         continue;
726                 }
727                 selftest(1, "passed");
728         }
729 }
730
731 struct device test_bus = {
732         .init_name = "unittest-bus",
733 };
734 static void __init of_selftest_platform_populate(void)
735 {
736         int irq, rc;
737         struct device_node *np, *child, *grandchild;
738         struct platform_device *pdev;
739         struct of_device_id match[] = {
740                 { .compatible = "test-device", },
741                 {}
742         };
743
744         np = of_find_node_by_path("/testcase-data");
745         of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
746
747         /* Test that a missing irq domain returns -EPROBE_DEFER */
748         np = of_find_node_by_path("/testcase-data/testcase-device1");
749         pdev = of_find_device_by_node(np);
750         selftest(pdev, "device 1 creation failed\n");
751
752         irq = platform_get_irq(pdev, 0);
753         selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
754
755         /* Test that a parsing failure does not return -EPROBE_DEFER */
756         np = of_find_node_by_path("/testcase-data/testcase-device2");
757         pdev = of_find_device_by_node(np);
758         selftest(pdev, "device 2 creation failed\n");
759         irq = platform_get_irq(pdev, 0);
760         selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
761
762         if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
763                      "No testcase data in device tree\n"));
764                 return;
765
766         if (selftest(!(rc = device_register(&test_bus)),
767                      "testbus registration failed; rc=%i\n", rc));
768                 return;
769
770         for_each_child_of_node(np, child) {
771                 of_platform_populate(child, match, NULL, &test_bus);
772                 for_each_child_of_node(child, grandchild)
773                         selftest(of_find_device_by_node(grandchild),
774                                  "Could not create device for node '%s'\n",
775                                  grandchild->name);
776         }
777
778         of_platform_depopulate(&test_bus);
779         for_each_child_of_node(np, child) {
780                 for_each_child_of_node(child, grandchild)
781                         selftest(!of_find_device_by_node(grandchild),
782                                  "device didn't get destroyed '%s'\n",
783                                  grandchild->name);
784         }
785
786         device_unregister(&test_bus);
787         of_node_put(np);
788 }
789
790 /**
791  *      update_node_properties - adds the properties
792  *      of np into dup node (present in live tree) and
793  *      updates parent of children of np to dup.
794  *
795  *      @np:    node already present in live tree
796  *      @dup:   node present in live tree to be updated
797  */
798 static void update_node_properties(struct device_node *np,
799                                         struct device_node *dup)
800 {
801         struct property *prop;
802         struct device_node *child;
803
804         for_each_property_of_node(np, prop)
805                 of_add_property(dup, prop);
806
807         for_each_child_of_node(np, child)
808                 child->parent = dup;
809 }
810
811 /**
812  *      attach_node_and_children - attaches nodes
813  *      and its children to live tree
814  *
815  *      @np:    Node to attach to live tree
816  */
817 static int attach_node_and_children(struct device_node *np)
818 {
819         struct device_node *next, *dup, *child;
820         unsigned long flags;
821
822         dup = of_find_node_by_path(np->full_name);
823         if (dup) {
824                 update_node_properties(np, dup);
825                 return 0;
826         }
827
828         child = np->child;
829         np->child = NULL;
830
831         mutex_lock(&of_mutex);
832         raw_spin_lock_irqsave(&devtree_lock, flags);
833         np->sibling = np->parent->child;
834         np->parent->child = np;
835         of_node_clear_flag(np, OF_DETACHED);
836         raw_spin_unlock_irqrestore(&devtree_lock, flags);
837
838         __of_attach_node_sysfs(np);
839         mutex_unlock(&of_mutex);
840
841         while (child) {
842                 next = child->sibling;
843                 attach_node_and_children(child);
844                 child = next;
845         }
846
847         return 0;
848 }
849
850 /**
851  *      selftest_data_add - Reads, copies data from
852  *      linked tree and attaches it to the live tree
853  */
854 static int __init selftest_data_add(void)
855 {
856         void *selftest_data;
857         struct device_node *selftest_data_node, *np;
858         extern uint8_t __dtb_testcases_begin[];
859         extern uint8_t __dtb_testcases_end[];
860         const int size = __dtb_testcases_end - __dtb_testcases_begin;
861         int rc;
862
863         if (!size) {
864                 pr_warn("%s: No testcase data to attach; not running tests\n",
865                         __func__);
866                 return -ENODATA;
867         }
868
869         /* creating copy */
870         selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
871
872         if (!selftest_data) {
873                 pr_warn("%s: Failed to allocate memory for selftest_data; "
874                         "not running tests\n", __func__);
875                 return -ENOMEM;
876         }
877         of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
878         if (!selftest_data_node) {
879                 pr_warn("%s: No tree to attach; not running tests\n", __func__);
880                 return -ENODATA;
881         }
882         of_node_set_flag(selftest_data_node, OF_DETACHED);
883         rc = of_resolve_phandles(selftest_data_node);
884         if (rc) {
885                 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
886                 return -EINVAL;
887         }
888
889         if (!of_root) {
890                 of_root = selftest_data_node;
891                 for_each_of_allnodes(np)
892                         __of_attach_node_sysfs(np);
893                 of_aliases = of_find_node_by_path("/aliases");
894                 of_chosen = of_find_node_by_path("/chosen");
895                 return 0;
896         }
897
898         /* attach the sub-tree to live tree */
899         np = selftest_data_node->child;
900         while (np) {
901                 struct device_node *next = np->sibling;
902                 np->parent = of_root;
903                 attach_node_and_children(np);
904                 np = next;
905         }
906         return 0;
907 }
908
909 #ifdef CONFIG_OF_OVERLAY
910
911 static int selftest_probe(struct platform_device *pdev)
912 {
913         struct device *dev = &pdev->dev;
914         struct device_node *np = dev->of_node;
915
916         if (np == NULL) {
917                 dev_err(dev, "No OF data for device\n");
918                 return -EINVAL;
919
920         }
921
922         dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
923         return 0;
924 }
925
926 static int selftest_remove(struct platform_device *pdev)
927 {
928         struct device *dev = &pdev->dev;
929         struct device_node *np = dev->of_node;
930
931         dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
932         return 0;
933 }
934
935 static struct of_device_id selftest_match[] = {
936         { .compatible = "selftest", },
937         {},
938 };
939
940 static struct platform_driver selftest_driver = {
941         .probe                  = selftest_probe,
942         .remove                 = selftest_remove,
943         .driver = {
944                 .name           = "selftest",
945                 .owner          = THIS_MODULE,
946                 .of_match_table = of_match_ptr(selftest_match),
947         },
948 };
949
950 /* get the platform device instantiated at the path */
951 static struct platform_device *of_path_to_platform_device(const char *path)
952 {
953         struct device_node *np;
954         struct platform_device *pdev;
955
956         np = of_find_node_by_path(path);
957         if (np == NULL)
958                 return NULL;
959
960         pdev = of_find_device_by_node(np);
961         of_node_put(np);
962
963         return pdev;
964 }
965
966 /* find out if a platform device exists at that path */
967 static int of_path_platform_device_exists(const char *path)
968 {
969         struct platform_device *pdev;
970
971         pdev = of_path_to_platform_device(path);
972         platform_device_put(pdev);
973         return pdev != NULL;
974 }
975
976 static const char *selftest_path(int nr)
977 {
978         static char buf[256];
979
980         snprintf(buf, sizeof(buf) - 1,
981                 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr);
982         buf[sizeof(buf) - 1] = '\0';
983
984         return buf;
985 }
986
987 static const char *overlay_path(int nr)
988 {
989         static char buf[256];
990
991         snprintf(buf, sizeof(buf) - 1,
992                 "/testcase-data/overlay%d", nr);
993         buf[sizeof(buf) - 1] = '\0';
994
995         return buf;
996 }
997
998 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
999
1000 static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1001                 int *overlay_id)
1002 {
1003         struct device_node *np = NULL;
1004         int ret, id = -1;
1005
1006         np = of_find_node_by_path(overlay_path(overlay_nr));
1007         if (np == NULL) {
1008                 selftest(0, "could not find overlay node @\"%s\"\n",
1009                                 overlay_path(overlay_nr));
1010                 ret = -EINVAL;
1011                 goto out;
1012         }
1013
1014         ret = of_overlay_create(np);
1015         if (ret < 0) {
1016                 selftest(0, "could not create overlay from \"%s\"\n",
1017                                 overlay_path(overlay_nr));
1018                 goto out;
1019         }
1020         id = ret;
1021
1022         ret = 0;
1023
1024 out:
1025         of_node_put(np);
1026
1027         if (overlay_id)
1028                 *overlay_id = id;
1029
1030         return ret;
1031 }
1032
1033 /* apply an overlay while checking before and after states */
1034 static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1035                 int before, int after)
1036 {
1037         int ret;
1038
1039         /* selftest device must not be in before state */
1040         if (of_path_platform_device_exists(selftest_path(selftest_nr))
1041                         != before) {
1042                 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1043                                 overlay_path(overlay_nr),
1044                                 selftest_path(selftest_nr),
1045                                 !before ? "enabled" : "disabled");
1046                 return -EINVAL;
1047         }
1048
1049         ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1050         if (ret != 0) {
1051                 /* of_selftest_apply_overlay already called selftest() */
1052                 return ret;
1053         }
1054
1055         /* selftest device must be to set to after state */
1056         if (of_path_platform_device_exists(selftest_path(selftest_nr))
1057                         != after) {
1058                 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1059                                 overlay_path(overlay_nr),
1060                                 selftest_path(selftest_nr),
1061                                 !after ? "enabled" : "disabled");
1062                 return -EINVAL;
1063         }
1064
1065         return 0;
1066 }
1067
1068 /* apply an overlay and then revert it while checking before, after states */
1069 static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1070                 int selftest_nr, int before, int after)
1071 {
1072         int ret, ov_id;
1073
1074         /* selftest device must be in before state */
1075         if (of_path_platform_device_exists(selftest_path(selftest_nr))
1076                         != before) {
1077                 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1078                                 overlay_path(overlay_nr),
1079                                 selftest_path(selftest_nr),
1080                                 !before ? "enabled" : "disabled");
1081                 return -EINVAL;
1082         }
1083
1084         /* apply the overlay */
1085         ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1086         if (ret != 0) {
1087                 /* of_selftest_apply_overlay already called selftest() */
1088                 return ret;
1089         }
1090
1091         /* selftest device must be in after state */
1092         if (of_path_platform_device_exists(selftest_path(selftest_nr))
1093                         != after) {
1094                 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1095                                 overlay_path(overlay_nr),
1096                                 selftest_path(selftest_nr),
1097                                 !after ? "enabled" : "disabled");
1098                 return -EINVAL;
1099         }
1100
1101         ret = of_overlay_destroy(ov_id);
1102         if (ret != 0) {
1103                 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1104                                 overlay_path(overlay_nr),
1105                                 selftest_path(selftest_nr));
1106                 return ret;
1107         }
1108
1109         /* selftest device must be again in before state */
1110         if (of_path_platform_device_exists(selftest_path(selftest_nr))
1111                         != before) {
1112                 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1113                                 overlay_path(overlay_nr),
1114                                 selftest_path(selftest_nr),
1115                                 !before ? "enabled" : "disabled");
1116                 return -EINVAL;
1117         }
1118
1119         return 0;
1120 }
1121
1122 /* test activation of device */
1123 static void of_selftest_overlay_0(void)
1124 {
1125         int ret;
1126
1127         /* device should enable */
1128         ret = of_selftest_apply_overlay_check(0, 0, 0, 1);
1129         if (ret != 0)
1130                 return;
1131
1132         selftest(1, "overlay test %d passed\n", 0);
1133 }
1134
1135 /* test deactivation of device */
1136 static void of_selftest_overlay_1(void)
1137 {
1138         int ret;
1139
1140         /* device should disable */
1141         ret = of_selftest_apply_overlay_check(1, 1, 1, 0);
1142         if (ret != 0)
1143                 return;
1144
1145         selftest(1, "overlay test %d passed\n", 1);
1146 }
1147
1148 /* test activation of device */
1149 static void of_selftest_overlay_2(void)
1150 {
1151         int ret;
1152
1153         /* device should enable */
1154         ret = of_selftest_apply_overlay_check(2, 2, 0, 1);
1155         if (ret != 0)
1156                 return;
1157
1158         selftest(1, "overlay test %d passed\n", 2);
1159 }
1160
1161 /* test deactivation of device */
1162 static void of_selftest_overlay_3(void)
1163 {
1164         int ret;
1165
1166         /* device should disable */
1167         ret = of_selftest_apply_overlay_check(3, 3, 1, 0);
1168         if (ret != 0)
1169                 return;
1170
1171         selftest(1, "overlay test %d passed\n", 3);
1172 }
1173
1174 /* test activation of a full device node */
1175 static void of_selftest_overlay_4(void)
1176 {
1177         int ret;
1178
1179         /* device should disable */
1180         ret = of_selftest_apply_overlay_check(4, 4, 0, 1);
1181         if (ret != 0)
1182                 return;
1183
1184         selftest(1, "overlay test %d passed\n", 4);
1185 }
1186
1187 /* test overlay apply/revert sequence */
1188 static void of_selftest_overlay_5(void)
1189 {
1190         int ret;
1191
1192         /* device should disable */
1193         ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
1194         if (ret != 0)
1195                 return;
1196
1197         selftest(1, "overlay test %d passed\n", 5);
1198 }
1199
1200 /* test overlay application in sequence */
1201 static void of_selftest_overlay_6(void)
1202 {
1203         struct device_node *np;
1204         int ret, i, ov_id[2];
1205         int overlay_nr = 6, selftest_nr = 6;
1206         int before = 0, after = 1;
1207
1208         /* selftest device must be in before state */
1209         for (i = 0; i < 2; i++) {
1210                 if (of_path_platform_device_exists(
1211                                         selftest_path(selftest_nr + i))
1212                                 != before) {
1213                         selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1214                                         overlay_path(overlay_nr + i),
1215                                         selftest_path(selftest_nr + i),
1216                                         !before ? "enabled" : "disabled");
1217                         return;
1218                 }
1219         }
1220
1221         /* apply the overlays */
1222         for (i = 0; i < 2; i++) {
1223
1224                 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1225                 if (np == NULL) {
1226                         selftest(0, "could not find overlay node @\"%s\"\n",
1227                                         overlay_path(overlay_nr + i));
1228                         return;
1229                 }
1230
1231                 ret = of_overlay_create(np);
1232                 if (ret < 0)  {
1233                         selftest(0, "could not create overlay from \"%s\"\n",
1234                                         overlay_path(overlay_nr + i));
1235                         return;
1236                 }
1237                 ov_id[i] = ret;
1238         }
1239
1240         for (i = 0; i < 2; i++) {
1241                 /* selftest device must be in after state */
1242                 if (of_path_platform_device_exists(
1243                                         selftest_path(selftest_nr + i))
1244                                 != after) {
1245                         selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1246                                         overlay_path(overlay_nr + i),
1247                                         selftest_path(selftest_nr + i),
1248                                         !after ? "enabled" : "disabled");
1249                         return;
1250                 }
1251         }
1252
1253         for (i = 1; i >= 0; i--) {
1254                 ret = of_overlay_destroy(ov_id[i]);
1255                 if (ret != 0) {
1256                         selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1257                                         overlay_path(overlay_nr + i),
1258                                         selftest_path(selftest_nr + i));
1259                         return;
1260                 }
1261         }
1262
1263         for (i = 0; i < 2; i++) {
1264                 /* selftest device must be again in before state */
1265                 if (of_path_platform_device_exists(
1266                                         selftest_path(selftest_nr + i))
1267                                 != before) {
1268                         selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1269                                         overlay_path(overlay_nr + i),
1270                                         selftest_path(selftest_nr + i),
1271                                         !before ? "enabled" : "disabled");
1272                         return;
1273                 }
1274         }
1275
1276         selftest(1, "overlay test %d passed\n", 6);
1277 }
1278
1279 /* test overlay application in sequence */
1280 static void of_selftest_overlay_8(void)
1281 {
1282         struct device_node *np;
1283         int ret, i, ov_id[2];
1284         int overlay_nr = 8, selftest_nr = 8;
1285
1286         /* we don't care about device state in this test */
1287
1288         /* apply the overlays */
1289         for (i = 0; i < 2; i++) {
1290
1291                 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1292                 if (np == NULL) {
1293                         selftest(0, "could not find overlay node @\"%s\"\n",
1294                                         overlay_path(overlay_nr + i));
1295                         return;
1296                 }
1297
1298                 ret = of_overlay_create(np);
1299                 if (ret < 0)  {
1300                         selftest(0, "could not create overlay from \"%s\"\n",
1301                                         overlay_path(overlay_nr + i));
1302                         return;
1303                 }
1304                 ov_id[i] = ret;
1305         }
1306
1307         /* now try to remove first overlay (it should fail) */
1308         ret = of_overlay_destroy(ov_id[0]);
1309         if (ret == 0) {
1310                 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1311                                 overlay_path(overlay_nr + 0),
1312                                 selftest_path(selftest_nr));
1313                 return;
1314         }
1315
1316         /* removing them in order should work */
1317         for (i = 1; i >= 0; i--) {
1318                 ret = of_overlay_destroy(ov_id[i]);
1319                 if (ret != 0) {
1320                         selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1321                                         overlay_path(overlay_nr + i),
1322                                         selftest_path(selftest_nr));
1323                         return;
1324                 }
1325         }
1326
1327         selftest(1, "overlay test %d passed\n", 8);
1328 }
1329
1330 static void __init of_selftest_overlay(void)
1331 {
1332         struct device_node *bus_np = NULL;
1333         int ret;
1334
1335         ret = platform_driver_register(&selftest_driver);
1336         if (ret != 0) {
1337                 selftest(0, "could not register selftest driver\n");
1338                 goto out;
1339         }
1340
1341         bus_np = of_find_node_by_path(bus_path);
1342         if (bus_np == NULL) {
1343                 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1344                 goto out;
1345         }
1346
1347         ret = of_platform_populate(bus_np, of_default_bus_match_table,
1348                         NULL, NULL);
1349         if (ret != 0) {
1350                 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1351                 goto out;
1352         }
1353
1354         if (!of_path_platform_device_exists(selftest_path(100))) {
1355                 selftest(0, "could not find selftest0 @ \"%s\"\n",
1356                                 selftest_path(100));
1357                 goto out;
1358         }
1359
1360         if (of_path_platform_device_exists(selftest_path(101))) {
1361                 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1362                                 selftest_path(101));
1363                 goto out;
1364         }
1365
1366         selftest(1, "basic infrastructure of overlays passed");
1367
1368         /* tests in sequence */
1369         of_selftest_overlay_0();
1370         of_selftest_overlay_1();
1371         of_selftest_overlay_2();
1372         of_selftest_overlay_3();
1373         of_selftest_overlay_4();
1374         of_selftest_overlay_5();
1375         of_selftest_overlay_6();
1376         of_selftest_overlay_8();
1377
1378 out:
1379         of_node_put(bus_np);
1380 }
1381
1382 #else
1383 static inline void __init of_selftest_overlay(void) { }
1384 #endif
1385
1386 static int __init of_selftest(void)
1387 {
1388         struct device_node *np;
1389         int res;
1390
1391         /* adding data for selftest */
1392         res = selftest_data_add();
1393         if (res)
1394                 return res;
1395         if (!of_aliases)
1396                 of_aliases = of_find_node_by_path("/aliases");
1397
1398         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1399         if (!np) {
1400                 pr_info("No testcase data in device tree; not running tests\n");
1401                 return 0;
1402         }
1403         of_node_put(np);
1404
1405         pr_info("start of selftest - you will see error messages\n");
1406         of_selftest_check_tree_linkage();
1407         of_selftest_check_phandles();
1408         of_selftest_find_node_by_name();
1409         of_selftest_dynamic();
1410         of_selftest_parse_phandle_with_args();
1411         of_selftest_property_string();
1412         of_selftest_property_copy();
1413         of_selftest_changeset();
1414         of_selftest_parse_interrupts();
1415         of_selftest_parse_interrupts_extended();
1416         of_selftest_match_node();
1417         of_selftest_platform_populate();
1418         of_selftest_overlay();
1419
1420         /* Double check linkage after removing testcase data */
1421         of_selftest_check_tree_linkage();
1422
1423         pr_info("end of selftest - %i passed, %i failed\n",
1424                 selftest_results.passed, selftest_results.failed);
1425
1426         return 0;
1427 }
1428 late_initcall(of_selftest);
This page took 0.121159 seconds and 4 git commands to generate.