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>
20 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
23 #include "of_private.h"
25 static struct selftest_results
{
31 static struct device_node
*nodes
[NO_OF_NODES
];
32 static int last_node_index
;
33 static bool selftest_live_tree
;
35 #define selftest(result, fmt, ...) ({ \
36 bool failed = !(result); \
38 selftest_results.failed++; \
39 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
41 selftest_results.passed++; \
42 pr_debug("pass %s():%i\n", __func__, __LINE__); \
47 static void __init
of_selftest_find_node_by_name(void)
49 struct device_node
*np
;
51 np
= of_find_node_by_path("/testcase-data");
52 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
53 "find /testcase-data failed\n");
56 /* Test if trailing '/' works */
57 np
= of_find_node_by_path("/testcase-data/");
58 selftest(!np
, "trailing '/' on /testcase-data/ should fail\n");
60 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
61 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
62 "find /testcase-data/phandle-tests/consumer-a failed\n");
65 np
= of_find_node_by_path("testcase-alias");
66 selftest(np
&& !strcmp("/testcase-data", np
->full_name
),
67 "find testcase-alias failed\n");
70 /* Test if trailing '/' works on aliases */
71 np
= of_find_node_by_path("testcase-alias/");
72 selftest(!np
, "trailing '/' on testcase-alias/ should fail\n");
74 np
= of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
75 selftest(np
&& !strcmp("/testcase-data/phandle-tests/consumer-a", np
->full_name
),
76 "find testcase-alias/phandle-tests/consumer-a failed\n");
79 np
= of_find_node_by_path("/testcase-data/missing-path");
80 selftest(!np
, "non-existent path returned node %s\n", np
->full_name
);
83 np
= of_find_node_by_path("missing-alias");
84 selftest(!np
, "non-existent alias returned node %s\n", np
->full_name
);
87 np
= of_find_node_by_path("testcase-alias/missing-path");
88 selftest(!np
, "non-existent alias with relative path returned node %s\n", np
->full_name
);
92 static void __init
of_selftest_dynamic(void)
94 struct device_node
*np
;
95 struct property
*prop
;
97 np
= of_find_node_by_path("/testcase-data");
99 pr_err("missing testcase data\n");
103 /* Array of 4 properties for the purpose of testing */
104 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
106 selftest(0, "kzalloc() failed\n");
110 /* Add a new property - should pass*/
111 prop
->name
= "new-property";
112 prop
->value
= "new-property-data";
113 prop
->length
= strlen(prop
->value
);
114 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
116 /* Try to add an existing property - should fail */
118 prop
->name
= "new-property";
119 prop
->value
= "new-property-data-should-fail";
120 prop
->length
= strlen(prop
->value
);
121 selftest(of_add_property(np
, prop
) != 0,
122 "Adding an existing property should have failed\n");
124 /* Try to modify an existing property - should pass */
125 prop
->value
= "modify-property-data-should-pass";
126 prop
->length
= strlen(prop
->value
);
127 selftest(of_update_property(np
, prop
) == 0,
128 "Updating an existing property should have passed\n");
130 /* Try to modify non-existent property - should pass*/
132 prop
->name
= "modify-property";
133 prop
->value
= "modify-missing-property-data-should-pass";
134 prop
->length
= strlen(prop
->value
);
135 selftest(of_update_property(np
, prop
) == 0,
136 "Updating a missing property should have passed\n");
138 /* Remove property - should pass */
139 selftest(of_remove_property(np
, prop
) == 0,
140 "Removing a property should have passed\n");
142 /* Adding very large property - should pass */
144 prop
->name
= "large-property-PAGE_SIZEx8";
145 prop
->length
= PAGE_SIZE
* 8;
146 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
147 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
149 selftest(of_add_property(np
, prop
) == 0,
150 "Adding a large property should have passed\n");
153 static int __init
of_selftest_check_node_linkage(struct device_node
*np
)
155 struct device_node
*child
;
158 for_each_child_of_node(np
, child
) {
159 if (child
->parent
!= np
) {
160 pr_err("Child node %s links to wrong parent %s\n",
161 child
->name
, np
->name
);
165 rc
= of_selftest_check_node_linkage(child
);
174 static void __init
of_selftest_check_tree_linkage(void)
176 struct device_node
*np
;
177 int allnode_count
= 0, child_count
;
182 for_each_of_allnodes(np
)
184 child_count
= of_selftest_check_node_linkage(of_root
);
186 selftest(child_count
> 0, "Device node data structure is corrupted\n");
187 selftest(child_count
== allnode_count
, "allnodes list size (%i) doesn't match"
188 "sibling lists size (%i)\n", allnode_count
, child_count
);
189 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count
, child_count
);
193 struct hlist_node node
;
194 struct device_node
*np
;
197 static DEFINE_HASHTABLE(phandle_ht
, 8);
198 static void __init
of_selftest_check_phandles(void)
200 struct device_node
*np
;
201 struct node_hash
*nh
;
202 struct hlist_node
*tmp
;
203 int i
, dup_count
= 0, phandle_count
= 0;
205 for_each_of_allnodes(np
) {
209 hash_for_each_possible(phandle_ht
, nh
, node
, np
->phandle
) {
210 if (nh
->np
->phandle
== np
->phandle
) {
211 pr_info("Duplicate phandle! %i used by %s and %s\n",
212 np
->phandle
, nh
->np
->full_name
, np
->full_name
);
218 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
223 hash_add(phandle_ht
, &nh
->node
, np
->phandle
);
226 selftest(dup_count
== 0, "Found %i duplicates in %i phandles\n",
227 dup_count
, phandle_count
);
230 hash_for_each_safe(phandle_ht
, i
, tmp
, nh
, node
) {
236 static void __init
of_selftest_parse_phandle_with_args(void)
238 struct device_node
*np
;
239 struct of_phandle_args args
;
242 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
244 pr_err("missing testcase data\n");
248 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
249 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
251 for (i
= 0; i
< 8; i
++) {
253 rc
= of_parse_phandle_with_args(np
, "phandle-list",
254 "#phandle-cells", i
, &args
);
256 /* Test the values from tests-phandle.dtsi */
260 passed
&= (args
.args_count
== 1);
261 passed
&= (args
.args
[0] == (i
+ 1));
265 passed
&= (args
.args_count
== 2);
266 passed
&= (args
.args
[0] == (i
+ 1));
267 passed
&= (args
.args
[1] == 0);
270 passed
&= (rc
== -ENOENT
);
274 passed
&= (args
.args_count
== 3);
275 passed
&= (args
.args
[0] == (i
+ 1));
276 passed
&= (args
.args
[1] == 4);
277 passed
&= (args
.args
[2] == 3);
281 passed
&= (args
.args_count
== 2);
282 passed
&= (args
.args
[0] == (i
+ 1));
283 passed
&= (args
.args
[1] == 100);
287 passed
&= (args
.args_count
== 0);
291 passed
&= (args
.args_count
== 1);
292 passed
&= (args
.args
[0] == (i
+ 1));
295 passed
&= (rc
== -ENOENT
);
301 selftest(passed
, "index %i - data error on node %s rc=%i\n",
302 i
, args
.np
->full_name
, rc
);
305 /* Check for missing list property */
306 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
307 "#phandle-cells", 0, &args
);
308 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
309 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
311 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
313 /* Check for missing cells property */
314 rc
= of_parse_phandle_with_args(np
, "phandle-list",
315 "#phandle-cells-missing", 0, &args
);
316 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
317 rc
= of_count_phandle_with_args(np
, "phandle-list",
318 "#phandle-cells-missing");
319 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
321 /* Check for bad phandle in list */
322 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
323 "#phandle-cells", 0, &args
);
324 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
325 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
327 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
329 /* Check for incorrectly formed argument list */
330 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
331 "#phandle-cells", 1, &args
);
332 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
333 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
335 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
338 static void __init
of_selftest_property_string(void)
340 const char *strings
[4];
341 struct device_node
*np
;
344 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
346 pr_err("No testcase data in device tree\n");
350 rc
= of_property_match_string(np
, "phandle-list-names", "first");
351 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
352 rc
= of_property_match_string(np
, "phandle-list-names", "second");
353 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
354 rc
= of_property_match_string(np
, "phandle-list-names", "third");
355 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
356 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
357 selftest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
358 rc
= of_property_match_string(np
, "missing-property", "blah");
359 selftest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
360 rc
= of_property_match_string(np
, "empty-property", "blah");
361 selftest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
362 rc
= of_property_match_string(np
, "unterminated-string", "blah");
363 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
365 /* of_property_count_strings() tests */
366 rc
= of_property_count_strings(np
, "string-property");
367 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
368 rc
= of_property_count_strings(np
, "phandle-list-names");
369 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
370 rc
= of_property_count_strings(np
, "unterminated-string");
371 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
372 rc
= of_property_count_strings(np
, "unterminated-string-list");
373 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
375 /* of_property_read_string_index() tests */
376 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
377 selftest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
379 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
380 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
381 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
382 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
383 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
384 selftest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
385 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
386 selftest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
388 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
389 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
391 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
392 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
393 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
394 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
396 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
397 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
400 /* of_property_read_string_array() tests */
401 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
402 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
403 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
404 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
405 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
406 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
407 /* -- An incorrectly formed string should cause a failure */
408 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
409 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
410 /* -- parsing the correctly formed strings should still work: */
412 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
413 selftest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
415 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
416 selftest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
419 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
420 (p1)->value && (p2)->value && \
421 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
422 !strcmp((p1)->name, (p2)->name))
423 static void __init
of_selftest_property_copy(void)
425 #ifdef CONFIG_OF_DYNAMIC
426 struct property p1
= { .name
= "p1", .length
= 0, .value
= "" };
427 struct property p2
= { .name
= "p2", .length
= 5, .value
= "abcd" };
428 struct property
*new;
430 new = __of_prop_dup(&p1
, GFP_KERNEL
);
431 selftest(new && propcmp(&p1
, new), "empty property didn't copy correctly\n");
436 new = __of_prop_dup(&p2
, GFP_KERNEL
);
437 selftest(new && propcmp(&p2
, new), "non-empty property didn't copy correctly\n");
444 static void __init
of_selftest_changeset(void)
446 #ifdef CONFIG_OF_DYNAMIC
447 struct property
*ppadd
, padd
= { .name
= "prop-add", .length
= 0, .value
= "" };
448 struct property
*ppupdate
, pupdate
= { .name
= "prop-update", .length
= 5, .value
= "abcd" };
449 struct property
*ppremove
;
450 struct device_node
*n1
, *n2
, *n21
, *nremove
, *parent
, *np
;
451 struct of_changeset chgset
;
453 of_changeset_init(&chgset
);
454 n1
= __of_node_dup(NULL
, "/testcase-data/changeset/n1");
455 selftest(n1
, "testcase setup failure\n");
456 n2
= __of_node_dup(NULL
, "/testcase-data/changeset/n2");
457 selftest(n2
, "testcase setup failure\n");
458 n21
= __of_node_dup(NULL
, "%s/%s", "/testcase-data/changeset/n2", "n21");
459 selftest(n21
, "testcase setup failure %p\n", n21
);
460 nremove
= of_find_node_by_path("/testcase-data/changeset/node-remove");
461 selftest(nremove
, "testcase setup failure\n");
462 ppadd
= __of_prop_dup(&padd
, GFP_KERNEL
);
463 selftest(ppadd
, "testcase setup failure\n");
464 ppupdate
= __of_prop_dup(&pupdate
, GFP_KERNEL
);
465 selftest(ppupdate
, "testcase setup failure\n");
466 parent
= nremove
->parent
;
471 ppremove
= of_find_property(parent
, "prop-remove", NULL
);
472 selftest(ppremove
, "failed to find removal prop");
474 of_changeset_init(&chgset
);
475 selftest(!of_changeset_attach_node(&chgset
, n1
), "fail attach n1\n");
476 selftest(!of_changeset_attach_node(&chgset
, n2
), "fail attach n2\n");
477 selftest(!of_changeset_detach_node(&chgset
, nremove
), "fail remove node\n");
478 selftest(!of_changeset_attach_node(&chgset
, n21
), "fail attach n21\n");
479 selftest(!of_changeset_add_property(&chgset
, parent
, ppadd
), "fail add prop\n");
480 selftest(!of_changeset_update_property(&chgset
, parent
, ppupdate
), "fail update prop\n");
481 selftest(!of_changeset_remove_property(&chgset
, parent
, ppremove
), "fail remove prop\n");
482 mutex_lock(&of_mutex
);
483 selftest(!of_changeset_apply(&chgset
), "apply failed\n");
484 mutex_unlock(&of_mutex
);
486 /* Make sure node names are constructed correctly */
487 selftest((np
= of_find_node_by_path("/testcase-data/changeset/n2/n21")),
488 "'%s' not added\n", n21
->full_name
);
492 mutex_lock(&of_mutex
);
493 selftest(!of_changeset_revert(&chgset
), "revert failed\n");
494 mutex_unlock(&of_mutex
);
496 of_changeset_destroy(&chgset
);
500 static void __init
of_selftest_parse_interrupts(void)
502 struct device_node
*np
;
503 struct of_phandle_args args
;
506 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
508 pr_err("missing testcase data\n");
512 for (i
= 0; i
< 4; i
++) {
515 rc
= of_irq_parse_one(np
, i
, &args
);
518 passed
&= (args
.args_count
== 1);
519 passed
&= (args
.args
[0] == (i
+ 1));
521 selftest(passed
, "index %i - data error on node %s rc=%i\n",
522 i
, args
.np
->full_name
, rc
);
526 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
528 pr_err("missing testcase data\n");
532 for (i
= 0; i
< 4; i
++) {
535 rc
= of_irq_parse_one(np
, i
, &args
);
537 /* Test the values from tests-phandle.dtsi */
541 passed
&= (args
.args_count
== 1);
542 passed
&= (args
.args
[0] == 9);
546 passed
&= (args
.args_count
== 3);
547 passed
&= (args
.args
[0] == 10);
548 passed
&= (args
.args
[1] == 11);
549 passed
&= (args
.args
[2] == 12);
553 passed
&= (args
.args_count
== 2);
554 passed
&= (args
.args
[0] == 13);
555 passed
&= (args
.args
[1] == 14);
559 passed
&= (args
.args_count
== 2);
560 passed
&= (args
.args
[0] == 15);
561 passed
&= (args
.args
[1] == 16);
566 selftest(passed
, "index %i - data error on node %s rc=%i\n",
567 i
, args
.np
->full_name
, rc
);
572 static void __init
of_selftest_parse_interrupts_extended(void)
574 struct device_node
*np
;
575 struct of_phandle_args args
;
578 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
580 pr_err("missing testcase data\n");
584 for (i
= 0; i
< 7; i
++) {
586 rc
= of_irq_parse_one(np
, i
, &args
);
588 /* Test the values from tests-phandle.dtsi */
592 passed
&= (args
.args_count
== 1);
593 passed
&= (args
.args
[0] == 1);
597 passed
&= (args
.args_count
== 3);
598 passed
&= (args
.args
[0] == 2);
599 passed
&= (args
.args
[1] == 3);
600 passed
&= (args
.args
[2] == 4);
604 passed
&= (args
.args_count
== 2);
605 passed
&= (args
.args
[0] == 5);
606 passed
&= (args
.args
[1] == 6);
610 passed
&= (args
.args_count
== 1);
611 passed
&= (args
.args
[0] == 9);
615 passed
&= (args
.args_count
== 3);
616 passed
&= (args
.args
[0] == 10);
617 passed
&= (args
.args
[1] == 11);
618 passed
&= (args
.args
[2] == 12);
622 passed
&= (args
.args_count
== 2);
623 passed
&= (args
.args
[0] == 13);
624 passed
&= (args
.args
[1] == 14);
628 passed
&= (args
.args_count
== 1);
629 passed
&= (args
.args
[0] == 15);
635 selftest(passed
, "index %i - data error on node %s rc=%i\n",
636 i
, args
.np
->full_name
, rc
);
641 static struct of_device_id match_node_table
[] = {
642 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
643 { .data
= "B", .type
= "type1", }, /* followed by type alone */
645 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
646 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
647 { .data
= "Cc", .name
= "name2", .type
= "type2", },
649 { .data
= "E", .compatible
= "compat3" },
650 { .data
= "G", .compatible
= "compat2", },
651 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
652 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
653 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
654 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
661 } match_node_tests
[] = {
662 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
663 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
664 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
665 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
666 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
667 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
668 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
669 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
670 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
671 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
672 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
673 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
676 static void __init
of_selftest_match_node(void)
678 struct device_node
*np
;
679 const struct of_device_id
*match
;
682 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
683 np
= of_find_node_by_path(match_node_tests
[i
].path
);
685 selftest(0, "missing testcase node %s\n",
686 match_node_tests
[i
].path
);
690 match
= of_match_node(match_node_table
, np
);
692 selftest(0, "%s didn't match anything\n",
693 match_node_tests
[i
].path
);
697 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
698 selftest(0, "%s got wrong match. expected %s, got %s\n",
699 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
700 (const char *)match
->data
);
703 selftest(1, "passed");
707 struct device test_bus
= {
708 .init_name
= "unittest-bus",
710 static void __init
of_selftest_platform_populate(void)
713 struct device_node
*np
, *child
, *grandchild
;
714 struct platform_device
*pdev
;
715 struct of_device_id match
[] = {
716 { .compatible
= "test-device", },
720 np
= of_find_node_by_path("/testcase-data");
721 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
723 /* Test that a missing irq domain returns -EPROBE_DEFER */
724 np
= of_find_node_by_path("/testcase-data/testcase-device1");
725 pdev
= of_find_device_by_node(np
);
726 selftest(pdev
, "device 1 creation failed\n");
728 irq
= platform_get_irq(pdev
, 0);
729 selftest(irq
== -EPROBE_DEFER
, "device deferred probe failed - %d\n", irq
);
731 /* Test that a parsing failure does not return -EPROBE_DEFER */
732 np
= of_find_node_by_path("/testcase-data/testcase-device2");
733 pdev
= of_find_device_by_node(np
);
734 selftest(pdev
, "device 2 creation failed\n");
735 irq
= platform_get_irq(pdev
, 0);
736 selftest(irq
< 0 && irq
!= -EPROBE_DEFER
, "device parsing error failed - %d\n", irq
);
738 if (selftest(np
= of_find_node_by_path("/testcase-data/platform-tests"),
739 "No testcase data in device tree\n"));
742 if (selftest(!(rc
= device_register(&test_bus
)),
743 "testbus registration failed; rc=%i\n", rc
));
746 for_each_child_of_node(np
, child
) {
747 of_platform_populate(child
, match
, NULL
, &test_bus
);
748 for_each_child_of_node(child
, grandchild
)
749 selftest(of_find_device_by_node(grandchild
),
750 "Could not create device for node '%s'\n",
754 of_platform_depopulate(&test_bus
);
755 for_each_child_of_node(np
, child
) {
756 for_each_child_of_node(child
, grandchild
)
757 selftest(!of_find_device_by_node(grandchild
),
758 "device didn't get destroyed '%s'\n",
762 device_unregister(&test_bus
);
767 * update_node_properties - adds the properties
768 * of np into dup node (present in live tree) and
769 * updates parent of children of np to dup.
771 * @np: node already present in live tree
772 * @dup: node present in live tree to be updated
774 static void update_node_properties(struct device_node
*np
,
775 struct device_node
*dup
)
777 struct property
*prop
;
778 struct device_node
*child
;
780 for_each_property_of_node(np
, prop
)
781 of_add_property(dup
, prop
);
783 for_each_child_of_node(np
, child
)
788 * attach_node_and_children - attaches nodes
789 * and its children to live tree
791 * @np: Node to attach to live tree
793 static int attach_node_and_children(struct device_node
*np
)
795 struct device_node
*next
, *dup
, *child
;
797 dup
= of_find_node_by_path(np
->full_name
);
799 update_node_properties(np
, dup
);
803 /* Children of the root need to be remembered for removal */
804 if (np
->parent
== of_root
) {
805 if (WARN_ON(last_node_index
>= NO_OF_NODES
))
807 nodes
[last_node_index
++] = np
;
815 next
= child
->sibling
;
816 attach_node_and_children(child
);
824 * selftest_data_add - Reads, copies data from
825 * linked tree and attaches it to the live tree
827 static int __init
selftest_data_add(void)
830 struct device_node
*selftest_data_node
, *np
;
831 extern uint8_t __dtb_testcases_begin
[];
832 extern uint8_t __dtb_testcases_end
[];
833 const int size
= __dtb_testcases_end
- __dtb_testcases_begin
;
837 pr_warn("%s: No testcase data to attach; not running tests\n",
843 selftest_data
= kmemdup(__dtb_testcases_begin
, size
, GFP_KERNEL
);
845 if (!selftest_data
) {
846 pr_warn("%s: Failed to allocate memory for selftest_data; "
847 "not running tests\n", __func__
);
850 of_fdt_unflatten_tree(selftest_data
, &selftest_data_node
);
851 if (!selftest_data_node
) {
852 pr_warn("%s: No tree to attach; not running tests\n", __func__
);
855 of_node_set_flag(selftest_data_node
, OF_DETACHED
);
856 rc
= of_resolve_phandles(selftest_data_node
);
858 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__
, rc
);
863 /* enabling flag for removing nodes */
864 selftest_live_tree
= true;
865 of_root
= selftest_data_node
;
867 for_each_of_allnodes(np
)
868 __of_attach_node_sysfs(np
);
869 of_aliases
= of_find_node_by_path("/aliases");
870 of_chosen
= of_find_node_by_path("/chosen");
874 /* attach the sub-tree to live tree */
875 np
= selftest_data_node
->child
;
877 struct device_node
*next
= np
->sibling
;
878 np
->parent
= of_root
;
879 attach_node_and_children(np
);
886 * detach_node_and_children - detaches node
887 * and its children from live tree
889 * @np: Node to detach from live tree
891 static void detach_node_and_children(struct device_node
*np
)
894 detach_node_and_children(np
->child
);
899 * selftest_data_remove - removes the selftest data
900 * nodes from the live tree
902 static void selftest_data_remove(void)
904 struct device_node
*np
;
905 struct property
*prop
;
907 if (selftest_live_tree
) {
908 of_node_put(of_aliases
);
909 of_node_put(of_chosen
);
912 for_each_child_of_node(of_root
, np
)
913 detach_node_and_children(np
);
914 __of_detach_node_sysfs(of_root
);
919 while (last_node_index
-- > 0) {
920 if (nodes
[last_node_index
]) {
921 np
= of_find_node_by_path(nodes
[last_node_index
]->full_name
);
922 if (np
== nodes
[last_node_index
]) {
923 if (of_aliases
== np
) {
924 of_node_put(of_aliases
);
927 detach_node_and_children(np
);
929 for_each_property_of_node(np
, prop
) {
930 if (strcmp(prop
->name
, "testcase-alias") == 0)
931 of_remove_property(np
, prop
);
938 #ifdef CONFIG_OF_OVERLAY
940 static int selftest_probe(struct platform_device
*pdev
)
942 struct device
*dev
= &pdev
->dev
;
943 struct device_node
*np
= dev
->of_node
;
946 dev_err(dev
, "No OF data for device\n");
951 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
955 static int selftest_remove(struct platform_device
*pdev
)
957 struct device
*dev
= &pdev
->dev
;
958 struct device_node
*np
= dev
->of_node
;
960 dev_dbg(dev
, "%s for node @%s\n", __func__
, np
->full_name
);
964 static struct of_device_id selftest_match
[] = {
965 { .compatible
= "selftest", },
969 static struct platform_driver selftest_driver
= {
970 .probe
= selftest_probe
,
971 .remove
= selftest_remove
,
974 .owner
= THIS_MODULE
,
975 .of_match_table
= of_match_ptr(selftest_match
),
979 /* get the platform device instantiated at the path */
980 static struct platform_device
*of_path_to_platform_device(const char *path
)
982 struct device_node
*np
;
983 struct platform_device
*pdev
;
985 np
= of_find_node_by_path(path
);
989 pdev
= of_find_device_by_node(np
);
995 /* find out if a platform device exists at that path */
996 static int of_path_platform_device_exists(const char *path
)
998 struct platform_device
*pdev
;
1000 pdev
= of_path_to_platform_device(path
);
1001 platform_device_put(pdev
);
1002 return pdev
!= NULL
;
1005 static const char *selftest_path(int nr
)
1007 static char buf
[256];
1009 snprintf(buf
, sizeof(buf
) - 1,
1010 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr
);
1011 buf
[sizeof(buf
) - 1] = '\0';
1016 static const char *overlay_path(int nr
)
1018 static char buf
[256];
1020 snprintf(buf
, sizeof(buf
) - 1,
1021 "/testcase-data/overlay%d", nr
);
1022 buf
[sizeof(buf
) - 1] = '\0';
1027 static const char *bus_path
= "/testcase-data/overlay-node/test-bus";
1029 static int of_selftest_apply_overlay(int selftest_nr
, int overlay_nr
,
1032 struct device_node
*np
= NULL
;
1035 np
= of_find_node_by_path(overlay_path(overlay_nr
));
1037 selftest(0, "could not find overlay node @\"%s\"\n",
1038 overlay_path(overlay_nr
));
1043 ret
= of_overlay_create(np
);
1045 selftest(0, "could not create overlay from \"%s\"\n",
1046 overlay_path(overlay_nr
));
1062 /* apply an overlay while checking before and after states */
1063 static int of_selftest_apply_overlay_check(int overlay_nr
, int selftest_nr
,
1064 int before
, int after
)
1068 /* selftest device must not be in before state */
1069 if (of_path_platform_device_exists(selftest_path(selftest_nr
))
1071 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1072 overlay_path(overlay_nr
),
1073 selftest_path(selftest_nr
),
1074 !before
? "enabled" : "disabled");
1078 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, NULL
);
1080 /* of_selftest_apply_overlay already called selftest() */
1084 /* selftest device must be to set to after state */
1085 if (of_path_platform_device_exists(selftest_path(selftest_nr
))
1087 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1088 overlay_path(overlay_nr
),
1089 selftest_path(selftest_nr
),
1090 !after
? "enabled" : "disabled");
1097 /* apply an overlay and then revert it while checking before, after states */
1098 static int of_selftest_apply_revert_overlay_check(int overlay_nr
,
1099 int selftest_nr
, int before
, int after
)
1103 /* selftest device must be in before state */
1104 if (of_path_platform_device_exists(selftest_path(selftest_nr
))
1106 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1107 overlay_path(overlay_nr
),
1108 selftest_path(selftest_nr
),
1109 !before
? "enabled" : "disabled");
1113 /* apply the overlay */
1114 ret
= of_selftest_apply_overlay(overlay_nr
, selftest_nr
, &ov_id
);
1116 /* of_selftest_apply_overlay already called selftest() */
1120 /* selftest device must be in after state */
1121 if (of_path_platform_device_exists(selftest_path(selftest_nr
))
1123 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1124 overlay_path(overlay_nr
),
1125 selftest_path(selftest_nr
),
1126 !after
? "enabled" : "disabled");
1130 ret
= of_overlay_destroy(ov_id
);
1132 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1133 overlay_path(overlay_nr
),
1134 selftest_path(selftest_nr
));
1138 /* selftest device must be again in before state */
1139 if (of_path_platform_device_exists(selftest_path(selftest_nr
))
1141 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1142 overlay_path(overlay_nr
),
1143 selftest_path(selftest_nr
),
1144 !before
? "enabled" : "disabled");
1151 /* test activation of device */
1152 static void of_selftest_overlay_0(void)
1156 /* device should enable */
1157 ret
= of_selftest_apply_overlay_check(0, 0, 0, 1);
1161 selftest(1, "overlay test %d passed\n", 0);
1164 /* test deactivation of device */
1165 static void of_selftest_overlay_1(void)
1169 /* device should disable */
1170 ret
= of_selftest_apply_overlay_check(1, 1, 1, 0);
1174 selftest(1, "overlay test %d passed\n", 1);
1177 /* test activation of device */
1178 static void of_selftest_overlay_2(void)
1182 /* device should enable */
1183 ret
= of_selftest_apply_overlay_check(2, 2, 0, 1);
1187 selftest(1, "overlay test %d passed\n", 2);
1190 /* test deactivation of device */
1191 static void of_selftest_overlay_3(void)
1195 /* device should disable */
1196 ret
= of_selftest_apply_overlay_check(3, 3, 1, 0);
1200 selftest(1, "overlay test %d passed\n", 3);
1203 /* test activation of a full device node */
1204 static void of_selftest_overlay_4(void)
1208 /* device should disable */
1209 ret
= of_selftest_apply_overlay_check(4, 4, 0, 1);
1213 selftest(1, "overlay test %d passed\n", 4);
1216 /* test overlay apply/revert sequence */
1217 static void of_selftest_overlay_5(void)
1221 /* device should disable */
1222 ret
= of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
1226 selftest(1, "overlay test %d passed\n", 5);
1229 /* test overlay application in sequence */
1230 static void of_selftest_overlay_6(void)
1232 struct device_node
*np
;
1233 int ret
, i
, ov_id
[2];
1234 int overlay_nr
= 6, selftest_nr
= 6;
1235 int before
= 0, after
= 1;
1237 /* selftest device must be in before state */
1238 for (i
= 0; i
< 2; i
++) {
1239 if (of_path_platform_device_exists(
1240 selftest_path(selftest_nr
+ i
))
1242 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1243 overlay_path(overlay_nr
+ i
),
1244 selftest_path(selftest_nr
+ i
),
1245 !before
? "enabled" : "disabled");
1250 /* apply the overlays */
1251 for (i
= 0; i
< 2; i
++) {
1253 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1255 selftest(0, "could not find overlay node @\"%s\"\n",
1256 overlay_path(overlay_nr
+ i
));
1260 ret
= of_overlay_create(np
);
1262 selftest(0, "could not create overlay from \"%s\"\n",
1263 overlay_path(overlay_nr
+ i
));
1269 for (i
= 0; i
< 2; i
++) {
1270 /* selftest device must be in after state */
1271 if (of_path_platform_device_exists(
1272 selftest_path(selftest_nr
+ i
))
1274 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1275 overlay_path(overlay_nr
+ i
),
1276 selftest_path(selftest_nr
+ i
),
1277 !after
? "enabled" : "disabled");
1282 for (i
= 1; i
>= 0; i
--) {
1283 ret
= of_overlay_destroy(ov_id
[i
]);
1285 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1286 overlay_path(overlay_nr
+ i
),
1287 selftest_path(selftest_nr
+ i
));
1292 for (i
= 0; i
< 2; i
++) {
1293 /* selftest device must be again in before state */
1294 if (of_path_platform_device_exists(
1295 selftest_path(selftest_nr
+ i
))
1297 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1298 overlay_path(overlay_nr
+ i
),
1299 selftest_path(selftest_nr
+ i
),
1300 !before
? "enabled" : "disabled");
1305 selftest(1, "overlay test %d passed\n", 6);
1308 /* test overlay application in sequence */
1309 static void of_selftest_overlay_8(void)
1311 struct device_node
*np
;
1312 int ret
, i
, ov_id
[2];
1313 int overlay_nr
= 8, selftest_nr
= 8;
1315 /* we don't care about device state in this test */
1317 /* apply the overlays */
1318 for (i
= 0; i
< 2; i
++) {
1320 np
= of_find_node_by_path(overlay_path(overlay_nr
+ i
));
1322 selftest(0, "could not find overlay node @\"%s\"\n",
1323 overlay_path(overlay_nr
+ i
));
1327 ret
= of_overlay_create(np
);
1329 selftest(0, "could not create overlay from \"%s\"\n",
1330 overlay_path(overlay_nr
+ i
));
1336 /* now try to remove first overlay (it should fail) */
1337 ret
= of_overlay_destroy(ov_id
[0]);
1339 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1340 overlay_path(overlay_nr
+ 0),
1341 selftest_path(selftest_nr
));
1345 /* removing them in order should work */
1346 for (i
= 1; i
>= 0; i
--) {
1347 ret
= of_overlay_destroy(ov_id
[i
]);
1349 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1350 overlay_path(overlay_nr
+ i
),
1351 selftest_path(selftest_nr
));
1356 selftest(1, "overlay test %d passed\n", 8);
1359 static void __init
of_selftest_overlay(void)
1361 struct device_node
*bus_np
= NULL
;
1364 ret
= platform_driver_register(&selftest_driver
);
1366 selftest(0, "could not register selftest driver\n");
1370 bus_np
= of_find_node_by_path(bus_path
);
1371 if (bus_np
== NULL
) {
1372 selftest(0, "could not find bus_path \"%s\"\n", bus_path
);
1376 ret
= of_platform_populate(bus_np
, of_default_bus_match_table
,
1379 selftest(0, "could not populate bus @ \"%s\"\n", bus_path
);
1383 if (!of_path_platform_device_exists(selftest_path(100))) {
1384 selftest(0, "could not find selftest0 @ \"%s\"\n",
1385 selftest_path(100));
1389 if (of_path_platform_device_exists(selftest_path(101))) {
1390 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1391 selftest_path(101));
1395 selftest(1, "basic infrastructure of overlays passed");
1397 /* tests in sequence */
1398 of_selftest_overlay_0();
1399 of_selftest_overlay_1();
1400 of_selftest_overlay_2();
1401 of_selftest_overlay_3();
1402 of_selftest_overlay_4();
1403 of_selftest_overlay_5();
1404 of_selftest_overlay_6();
1405 of_selftest_overlay_8();
1408 of_node_put(bus_np
);
1412 static inline void __init
of_selftest_overlay(void) { }
1415 static int __init
of_selftest(void)
1417 struct device_node
*np
;
1420 /* adding data for selftest */
1421 res
= selftest_data_add();
1425 of_aliases
= of_find_node_by_path("/aliases");
1427 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1429 pr_info("No testcase data in device tree; not running tests\n");
1434 pr_info("start of selftest - you will see error messages\n");
1435 of_selftest_check_tree_linkage();
1436 of_selftest_check_phandles();
1437 of_selftest_find_node_by_name();
1438 of_selftest_dynamic();
1439 of_selftest_parse_phandle_with_args();
1440 of_selftest_property_string();
1441 of_selftest_property_copy();
1442 of_selftest_changeset();
1443 of_selftest_parse_interrupts();
1444 of_selftest_parse_interrupts_extended();
1445 of_selftest_match_node();
1446 of_selftest_platform_populate();
1447 of_selftest_overlay();
1449 /* removing selftest data from live tree */
1450 selftest_data_remove();
1452 /* Double check linkage after removing testcase data */
1453 of_selftest_check_tree_linkage();
1455 pr_info("end of selftest - %i passed, %i failed\n",
1456 selftest_results
.passed
, selftest_results
.failed
);
1460 late_initcall(of_selftest
);