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