2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/hashtable.h>
11 #include <linux/module.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>
21 #include "of_private.h"
23 static struct selftest_results
{
29 static struct device_node
*nodes
[NO_OF_NODES
];
30 static int last_node_index
;
31 static bool selftest_live_tree
;
33 #define selftest(result, fmt, ...) ({ \
34 bool failed = !(result); \
36 selftest_results.failed++; \
37 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
39 selftest_results.passed++; \
40 pr_debug("pass %s():%i\n", __func__, __LINE__); \
45 static void __init
of_selftest_find_node_by_name(void)
47 struct device_node
*np
;
49 np
= of_find_node_by_path("/testcase-data");
50 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
51 "find /testcase-data failed\n");
54 /* Test if trailing '/' works */
55 np
= of_find_node_by_path("/testcase-data/");
56 selftest(!np
, "trailing '/' on /testcase-data/ should fail\n");
58 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
59 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
60 "find /testcase-data/phandle-tests/consumer-a failed\n");
63 np
= of_find_node_by_path("testcase-alias");
64 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
65 "find testcase-alias failed\n");
68 /* Test if trailing '/' works on aliases */
69 np
= of_find_node_by_path("testcase-alias/");
70 selftest(!np
, "trailing '/' on testcase-alias/ should fail\n");
72 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
73 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
74 "find testcase-alias/phandle-tests/consumer-a failed\n");
77 np
= of_find_node_by_path("/testcase-data/missing-path");
78 selftest(!np
, "non-existent path returned node %s\n", np
->full_name
);
81 np
= of_find_node_by_path("missing-alias");
82 selftest(!np
, "non-existent alias returned node %s\n", np
->full_name
);
85 np
= of_find_node_by_path("testcase-alias/missing-path");
86 selftest(!np
, "non-existent alias with relative path returned node %s\n", np
->full_name
);
90 static void __init
of_selftest_dynamic(void)
92 struct device_node
*np
;
93 struct property
*prop
;
95 np
= of_find_node_by_path("/testcase-data");
97 pr_err("missing testcase data\n");
101 /* Array of 4 properties for the purpose of testing */
102 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
104 selftest(0, "kzalloc() failed\n");
108 /* Add a new property - should pass*/
109 prop
->name
= "new-property";
110 prop
->value
= "new-property-data";
111 prop
->length
= strlen(prop
->value
);
112 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
114 /* Try to add an existing property - should fail */
116 prop
->name
= "new-property";
117 prop
->value
= "new-property-data-should-fail";
118 prop
->length
= strlen(prop
->value
);
119 selftest(of_add_property(np
, prop
) != 0,
120 "Adding an existing property should have failed\n");
122 /* Try to modify an existing property - should pass */
123 prop
->value
= "modify-property-data-should-pass";
124 prop
->length
= strlen(prop
->value
);
125 selftest(of_update_property(np
, prop
) == 0,
126 "Updating an existing property should have passed\n");
128 /* Try to modify non-existent property - should pass*/
130 prop
->name
= "modify-property";
131 prop
->value
= "modify-missing-property-data-should-pass";
132 prop
->length
= strlen(prop
->value
);
133 selftest(of_update_property(np
, prop
) == 0,
134 "Updating a missing property should have passed\n");
136 /* Remove property - should pass */
137 selftest(of_remove_property(np
, prop
) == 0,
138 "Removing a property should have passed\n");
140 /* Adding very large property - should pass */
142 prop
->name
= "large-property-PAGE_SIZEx8";
143 prop
->length
= PAGE_SIZE
* 8;
144 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
145 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
147 selftest(of_add_property(np
, prop
) == 0,
148 "Adding a large property should have passed\n");
151 static int __init
of_selftest_check_node_linkage(struct device_node
*np
)
153 struct device_node
*child
;
156 for_each_child_of_node(np
, child
) {
157 if (child
->parent
!= np
) {
158 pr_err("Child node %s links to wrong parent %s\n",
159 child
->name
, np
->name
);
163 rc
= of_selftest_check_node_linkage(child
);
172 static void __init
of_selftest_check_tree_linkage(void)
174 struct device_node
*np
;
175 int allnode_count
= 0, child_count
;
180 for_each_of_allnodes(np
)
182 child_count
= of_selftest_check_node_linkage(of_root
);
184 selftest(child_count
> 0, "Device node data structure is corrupted\n");
185 selftest(child_count
== allnode_count
, "allnodes list size (%i) doesn't match"
186 "sibling lists size (%i)\n", allnode_count
, child_count
);
187 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
191 struct hlist_node node
;
192 struct device_node
*np
;
195 static DEFINE_HASHTABLE(phandle_ht
, 8);
196 static void __init
of_selftest_check_phandles(void)
198 struct device_node
*np
;
199 struct node_hash
*nh
;
200 struct hlist_node
*tmp
;
201 int i
, dup_count
= 0, phandle_count
= 0;
203 for_each_of_allnodes(np
) {
207 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
208 if (nh
->np
->phandle
== np
->phandle
) {
209 pr_info("Duplicate phandle! %i used by %s and %s\n",
210 np
->phandle
, nh
->np
->full_name
, np
->full_name
);
216 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
221 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
224 selftest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
225 dup_count
, phandle_count
);
228 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
234 static void __init
of_selftest_parse_phandle_with_args(void)
236 struct device_node
*np
;
237 struct of_phandle_args args
;
240 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
242 pr_err("missing testcase data\n");
246 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
247 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
249 for (i
= 0; i
< 8; i
++) {
251 rc
= of_parse_phandle_with_args(np
, "phandle-list",
252 "#phandle-cells", i
, &args
);
254 /* Test the values from tests-phandle.dtsi */
258 passed
&= (args
.args_count
== 1);
259 passed
&= (args
.args
[0] == (i
+ 1));
263 passed
&= (args
.args_count
== 2);
264 passed
&= (args
.args
[0] == (i
+ 1));
265 passed
&= (args
.args
[1] == 0);
268 passed
&= (rc
== -ENOENT
);
272 passed
&= (args
.args_count
== 3);
273 passed
&= (args
.args
[0] == (i
+ 1));
274 passed
&= (args
.args
[1] == 4);
275 passed
&= (args
.args
[2] == 3);
279 passed
&= (args
.args_count
== 2);
280 passed
&= (args
.args
[0] == (i
+ 1));
281 passed
&= (args
.args
[1] == 100);
285 passed
&= (args
.args_count
== 0);
289 passed
&= (args
.args_count
== 1);
290 passed
&= (args
.args
[0] == (i
+ 1));
293 passed
&= (rc
== -ENOENT
);
299 selftest(passed
, "index %i - data error on node %s rc=%i\n",
300 i
, args
.np
->full_name
, rc
);
303 /* Check for missing list property */
304 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
305 "#phandle-cells", 0, &args
);
306 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
307 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
309 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
311 /* Check for missing cells property */
312 rc
= of_parse_phandle_with_args(np
, "phandle-list",
313 "#phandle-cells-missing", 0, &args
);
314 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
315 rc
= of_count_phandle_with_args(np
, "phandle-list",
316 "#phandle-cells-missing");
317 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
319 /* Check for bad phandle in list */
320 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
321 "#phandle-cells", 0, &args
);
322 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
323 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
325 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
327 /* Check for incorrectly formed argument list */
328 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
329 "#phandle-cells", 1, &args
);
330 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
331 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
333 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
336 static void __init
of_selftest_property_string(void)
338 const char *strings
[4];
339 struct device_node
*np
;
342 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
344 pr_err("No testcase data in device tree\n");
348 rc
= of_property_match_string(np
, "phandle-list-names", "first");
349 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
350 rc
= of_property_match_string(np
, "phandle-list-names", "second");
351 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
352 rc
= of_property_match_string(np
, "phandle-list-names", "third");
353 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
354 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
355 selftest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
356 rc
= of_property_match_string(np
, "missing-property", "blah");
357 selftest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
358 rc
= of_property_match_string(np
, "empty-property", "blah");
359 selftest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
360 rc
= of_property_match_string(np
, "unterminated-string", "blah");
361 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
363 /* of_property_count_strings() tests */
364 rc
= of_property_count_strings(np
, "string-property");
365 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
366 rc
= of_property_count_strings(np
, "phandle-list-names");
367 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
368 rc
= of_property_count_strings(np
, "unterminated-string");
369 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
370 rc
= of_property_count_strings(np
, "unterminated-string-list");
371 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
373 /* of_property_read_string_index() tests */
374 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
375 selftest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
377 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
378 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
379 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
380 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
381 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
382 selftest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
383 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
384 selftest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
386 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
387 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
389 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
390 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
391 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
392 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
394 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
395 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
398 /* of_property_read_string_array() tests */
399 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
400 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
401 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
402 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
403 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
404 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
405 /* -- An incorrectly formed string should cause a failure */
406 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
407 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
408 /* -- parsing the correctly formed strings should still work: */
410 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
411 selftest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
413 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
414 selftest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
417 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
418 (p1)->value && (p2)->value && \
419 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
420 !strcmp((p1)->name, (p2)->name))
421 static void __init
of_selftest_property_copy(void)
423 #ifdef CONFIG_OF_DYNAMIC
424 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
425 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
426 struct property
*new;
428 new = __of_prop_dup(&p1
, GFP_KERNEL
);
429 selftest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
434 new = __of_prop_dup(&p2
, GFP_KERNEL
);
435 selftest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
442 static void __init
of_selftest_changeset(void)
444 #ifdef CONFIG_OF_DYNAMIC
445 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 0, .value
= "" };
446 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
447 struct property
*ppremove
;
448 struct device_node
*n1
, *n2
, *n21
, *nremove
, *parent
;
449 struct of_changeset chgset
;
451 of_changeset_init(&chgset
);
452 n1
= __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL
);
453 selftest(n1
, "testcase setup failure\n");
454 n2
= __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL
);
455 selftest(n2
, "testcase setup failure\n");
456 n21
= __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL
);
457 selftest(n21
, "testcase setup failure %p\n", n21
);
458 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
459 selftest(nremove
, "testcase setup failure\n");
460 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
461 selftest(ppadd
, "testcase setup failure\n");
462 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
463 selftest(ppupdate
, "testcase setup failure\n");
464 parent
= nremove
->parent
;
469 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
470 selftest(ppremove
, "failed to find removal prop");
472 of_changeset_init(&chgset
);
473 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
474 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
475 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
476 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
477 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
478 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
479 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
480 mutex_lock(&of_mutex
);
481 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
482 mutex_unlock(&of_mutex
);
484 mutex_lock(&of_mutex
);
485 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
486 mutex_unlock(&of_mutex
);
488 of_changeset_destroy(&chgset
);
492 static void __init
of_selftest_parse_interrupts(void)
494 struct device_node
*np
;
495 struct of_phandle_args args
;
498 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
500 pr_err("missing testcase data\n");
504 for (i
= 0; i
< 4; i
++) {
507 rc
= of_irq_parse_one(np
, i
, &args
);
510 passed
&= (args
.args_count
== 1);
511 passed
&= (args
.args
[0] == (i
+ 1));
513 selftest(passed
, "index %i - data error on node %s rc=%i\n",
514 i
, args
.np
->full_name
, rc
);
518 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
520 pr_err("missing testcase data\n");
524 for (i
= 0; i
< 4; i
++) {
527 rc
= of_irq_parse_one(np
, i
, &args
);
529 /* Test the values from tests-phandle.dtsi */
533 passed
&= (args
.args_count
== 1);
534 passed
&= (args
.args
[0] == 9);
538 passed
&= (args
.args_count
== 3);
539 passed
&= (args
.args
[0] == 10);
540 passed
&= (args
.args
[1] == 11);
541 passed
&= (args
.args
[2] == 12);
545 passed
&= (args
.args_count
== 2);
546 passed
&= (args
.args
[0] == 13);
547 passed
&= (args
.args
[1] == 14);
551 passed
&= (args
.args_count
== 2);
552 passed
&= (args
.args
[0] == 15);
553 passed
&= (args
.args
[1] == 16);
558 selftest(passed
, "index %i - data error on node %s rc=%i\n",
559 i
, args
.np
->full_name
, rc
);
564 static void __init
of_selftest_parse_interrupts_extended(void)
566 struct device_node
*np
;
567 struct of_phandle_args args
;
570 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
572 pr_err("missing testcase data\n");
576 for (i
= 0; i
< 7; i
++) {
578 rc
= of_irq_parse_one(np
, i
, &args
);
580 /* Test the values from tests-phandle.dtsi */
584 passed
&= (args
.args_count
== 1);
585 passed
&= (args
.args
[0] == 1);
589 passed
&= (args
.args_count
== 3);
590 passed
&= (args
.args
[0] == 2);
591 passed
&= (args
.args
[1] == 3);
592 passed
&= (args
.args
[2] == 4);
596 passed
&= (args
.args_count
== 2);
597 passed
&= (args
.args
[0] == 5);
598 passed
&= (args
.args
[1] == 6);
602 passed
&= (args
.args_count
== 1);
603 passed
&= (args
.args
[0] == 9);
607 passed
&= (args
.args_count
== 3);
608 passed
&= (args
.args
[0] == 10);
609 passed
&= (args
.args
[1] == 11);
610 passed
&= (args
.args
[2] == 12);
614 passed
&= (args
.args_count
== 2);
615 passed
&= (args
.args
[0] == 13);
616 passed
&= (args
.args
[1] == 14);
620 passed
&= (args
.args_count
== 1);
621 passed
&= (args
.args
[0] == 15);
627 selftest(passed
, "index %i - data error on node %s rc=%i\n",
628 i
, args
.np
->full_name
, rc
);
633 static struct of_device_id match_node_table
[] = {
634 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
635 { .data
= "B", .type
= "type1", }, /* followed by type alone */
637 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
638 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
639 { .data
= "Cc", .name
= "name2", .type
= "type2", },
641 { .data
= "E", .compatible
= "compat3" },
642 { .data
= "G", .compatible
= "compat2", },
643 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
644 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
645 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
646 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
653 } match_node_tests
[] = {
654 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
655 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
656 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
657 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
658 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
659 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
660 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
661 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
662 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
663 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
664 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
665 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
668 static void __init
of_selftest_match_node(void)
670 struct device_node
*np
;
671 const struct of_device_id
*match
;
674 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
675 np
= of_find_node_by_path(match_node_tests
[i
].path
);
677 selftest(0, "missing testcase node %s\n",
678 match_node_tests
[i
].path
);
682 match
= of_match_node(match_node_table
, np
);
684 selftest(0, "%s didn't match anything\n",
685 match_node_tests
[i
].path
);
689 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
690 selftest(0, "%s got wrong match. expected %s, got %s\n",
691 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
692 (const char *)match
->data
);
695 selftest(1, "passed");
699 struct device test_bus
= {
700 .init_name
= "unittest-bus",
702 static void __init
of_selftest_platform_populate(void)
705 struct device_node
*np
, *child
, *grandchild
;
706 struct platform_device
*pdev
;
707 struct of_device_id match
[] = {
708 { .compatible
= "test-device", },
712 np
= of_find_node_by_path("/testcase-data");
713 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
715 /* Test that a missing irq domain returns -EPROBE_DEFER */
716 np
= of_find_node_by_path("/testcase-data/testcase-device1");
717 pdev
= of_find_device_by_node(np
);
718 selftest(pdev
, "device 1 creation failed\n");
720 irq
= platform_get_irq(pdev
, 0);
721 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
723 /* Test that a parsing failure does not return -EPROBE_DEFER */
724 np
= of_find_node_by_path("/testcase-data/testcase-device2");
725 pdev
= of_find_device_by_node(np
);
726 selftest(pdev
, "device 2 creation failed\n");
727 irq
= platform_get_irq(pdev
, 0);
728 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
730 if (selftest(np
= of_find_node_by_path("/testcase-data/platform-tests"),
731 "No testcase data in device tree\n"));
734 if (selftest(!(rc
= device_register(&test_bus
)),
735 "testbus registration failed; rc=%i\n", rc
));
738 for_each_child_of_node(np
, child
) {
739 of_platform_populate(child
, match
, NULL
, &test_bus
);
740 for_each_child_of_node(child
, grandchild
)
741 selftest(of_find_device_by_node(grandchild
),
742 "Could not create device for node '%s'\n",
746 of_platform_depopulate(&test_bus
);
747 for_each_child_of_node(np
, child
) {
748 for_each_child_of_node(child
, grandchild
)
749 selftest(!of_find_device_by_node(grandchild
),
750 "device didn't get destroyed '%s'\n",
754 device_unregister(&test_bus
);
759 * update_node_properties - adds the properties
760 * of np into dup node (present in live tree) and
761 * updates parent of children of np to dup.
763 * @np: node already present in live tree
764 * @dup: node present in live tree to be updated
766 static void update_node_properties(struct device_node
*np
,
767 struct device_node
*dup
)
769 struct property
*prop
;
770 struct device_node
*child
;
772 for_each_property_of_node(np
, prop
)
773 of_add_property(dup
, prop
);
775 for_each_child_of_node(np
, child
)
780 * attach_node_and_children - attaches nodes
781 * and its children to live tree
783 * @np: Node to attach to live tree
785 static int attach_node_and_children(struct device_node
*np
)
787 struct device_node
*next
, *dup
, *child
;
789 dup
= of_find_node_by_path(np
->full_name
);
791 update_node_properties(np
, dup
);
795 /* Children of the root need to be remembered for removal */
796 if (np
->parent
== of_root
) {
797 if (WARN_ON(last_node_index
>= NO_OF_NODES
))
799 nodes
[last_node_index
++] = np
;
807 next
= child
->sibling
;
808 attach_node_and_children(child
);
816 * selftest_data_add - Reads, copies data from
817 * linked tree and attaches it to the live tree
819 static int __init
selftest_data_add(void)
822 struct device_node
*selftest_data_node
, *np
;
823 extern uint8_t __dtb_testcases_begin
[];
824 extern uint8_t __dtb_testcases_end
[];
825 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
829 pr_warn("%s: No testcase data to attach; not running tests\n",
835 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
837 if (!selftest_data
) {
838 pr_warn("%s: Failed to allocate memory for selftest_data; "
839 "not running tests\n", __func__
);
842 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
843 if (!selftest_data_node
) {
844 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
847 of_node_set_flag(selftest_data_node
, OF_DETACHED
);
848 rc
= of_resolve_phandles(selftest_data_node
);
850 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
855 /* enabling flag for removing nodes */
856 selftest_live_tree
= true;
857 of_root
= selftest_data_node
;
859 for_each_of_allnodes(np
)
860 __of_attach_node_sysfs(np
);
861 of_aliases
= of_find_node_by_path("/aliases");
862 of_chosen
= of_find_node_by_path("/chosen");
866 /* attach the sub-tree to live tree */
867 np
= selftest_data_node
->child
;
869 struct device_node
*next
= np
->sibling
;
870 np
->parent
= of_root
;
871 attach_node_and_children(np
);
878 * detach_node_and_children - detaches node
879 * and its children from live tree
881 * @np: Node to detach from live tree
883 static void detach_node_and_children(struct device_node
*np
)
886 detach_node_and_children(np
->child
);
891 * selftest_data_remove - removes the selftest data
892 * nodes from the live tree
894 static void selftest_data_remove(void)
896 struct device_node
*np
;
897 struct property
*prop
;
899 if (selftest_live_tree
) {
900 of_node_put(of_aliases
);
901 of_node_put(of_chosen
);
904 for_each_child_of_node(of_root
, np
)
905 detach_node_and_children(np
);
906 __of_detach_node_sysfs(of_root
);
911 while (last_node_index
>= 0) {
912 if (nodes
[last_node_index
]) {
913 np
= of_find_node_by_path(nodes
[last_node_index
]->full_name
);
914 if (strcmp(np
->full_name
, "/aliases") != 0) {
915 detach_node_and_children(np
);
917 for_each_property_of_node(np
, prop
) {
918 if (strcmp(prop
->name
, "testcase-alias") == 0)
919 of_remove_property(np
, prop
);
927 static int __init
of_selftest(void)
929 struct device_node
*np
;
932 /* adding data for selftest */
933 res
= selftest_data_add();
937 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
939 pr_info("No testcase data in device tree; not running tests\n");
944 pr_info("start of selftest - you will see error messages\n");
945 of_selftest_check_tree_linkage();
946 of_selftest_check_phandles();
947 of_selftest_find_node_by_name();
948 of_selftest_dynamic();
949 of_selftest_parse_phandle_with_args();
950 of_selftest_property_string();
951 of_selftest_property_copy();
952 of_selftest_changeset();
953 of_selftest_parse_interrupts();
954 of_selftest_parse_interrupts_extended();
955 of_selftest_match_node();
956 of_selftest_platform_populate();
958 /* removing selftest data from live tree */
959 selftest_data_remove();
961 /* Double check linkage after removing testcase data */
962 of_selftest_check_tree_linkage();
964 pr_info("end of selftest - %i passed, %i failed\n",
965 selftest_results
.passed
, selftest_results
.failed
);
969 late_initcall(of_selftest
);