2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/module.h>
12 #include <linux/of_irq.h>
13 #include <linux/of_platform.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
19 static struct selftest_results
{
24 #define selftest(result, fmt, ...) { \
26 selftest_results.failed++; \
27 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
29 selftest_results.passed++; \
30 pr_debug("pass %s():%i\n", __func__, __LINE__); \
34 static void __init
of_selftest_dynamic(void)
36 struct device_node
*np
;
37 struct property
*prop
;
39 np
= of_find_node_by_path("/testcase-data");
41 pr_err("missing testcase data\n");
45 /* Array of 4 properties for the purpose of testing */
46 prop
= kzalloc(sizeof(*prop
) * 4, GFP_KERNEL
);
48 selftest(0, "kzalloc() failed\n");
52 /* Add a new property - should pass*/
53 prop
->name
= "new-property";
54 prop
->value
= "new-property-data";
55 prop
->length
= strlen(prop
->value
);
56 selftest(of_add_property(np
, prop
) == 0, "Adding a new property failed\n");
58 /* Try to add an existing property - should fail */
60 prop
->name
= "new-property";
61 prop
->value
= "new-property-data-should-fail";
62 prop
->length
= strlen(prop
->value
);
63 selftest(of_add_property(np
, prop
) != 0,
64 "Adding an existing property should have failed\n");
66 /* Try to modify an existing property - should pass */
67 prop
->value
= "modify-property-data-should-pass";
68 prop
->length
= strlen(prop
->value
);
69 selftest(of_update_property(np
, prop
) == 0,
70 "Updating an existing property should have passed\n");
72 /* Try to modify non-existent property - should pass*/
74 prop
->name
= "modify-property";
75 prop
->value
= "modify-missing-property-data-should-pass";
76 prop
->length
= strlen(prop
->value
);
77 selftest(of_update_property(np
, prop
) == 0,
78 "Updating a missing property should have passed\n");
80 /* Remove property - should pass */
81 selftest(of_remove_property(np
, prop
) == 0,
82 "Removing a property should have passed\n");
84 /* Adding very large property - should pass */
86 prop
->name
= "large-property-PAGE_SIZEx8";
87 prop
->length
= PAGE_SIZE
* 8;
88 prop
->value
= kzalloc(prop
->length
, GFP_KERNEL
);
89 selftest(prop
->value
!= NULL
, "Unable to allocate large buffer\n");
91 selftest(of_add_property(np
, prop
) == 0,
92 "Adding a large property should have passed\n");
95 static void __init
of_selftest_parse_phandle_with_args(void)
97 struct device_node
*np
;
98 struct of_phandle_args args
;
101 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
103 pr_err("missing testcase data\n");
107 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
108 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
110 for (i
= 0; i
< 8; i
++) {
112 rc
= of_parse_phandle_with_args(np
, "phandle-list",
113 "#phandle-cells", i
, &args
);
115 /* Test the values from tests-phandle.dtsi */
119 passed
&= (args
.args_count
== 1);
120 passed
&= (args
.args
[0] == (i
+ 1));
124 passed
&= (args
.args_count
== 2);
125 passed
&= (args
.args
[0] == (i
+ 1));
126 passed
&= (args
.args
[1] == 0);
129 passed
&= (rc
== -ENOENT
);
133 passed
&= (args
.args_count
== 3);
134 passed
&= (args
.args
[0] == (i
+ 1));
135 passed
&= (args
.args
[1] == 4);
136 passed
&= (args
.args
[2] == 3);
140 passed
&= (args
.args_count
== 2);
141 passed
&= (args
.args
[0] == (i
+ 1));
142 passed
&= (args
.args
[1] == 100);
146 passed
&= (args
.args_count
== 0);
150 passed
&= (args
.args_count
== 1);
151 passed
&= (args
.args
[0] == (i
+ 1));
154 passed
&= (rc
== -ENOENT
);
160 selftest(passed
, "index %i - data error on node %s rc=%i\n",
161 i
, args
.np
->full_name
, rc
);
164 /* Check for missing list property */
165 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
166 "#phandle-cells", 0, &args
);
167 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
168 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
170 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
172 /* Check for missing cells property */
173 rc
= of_parse_phandle_with_args(np
, "phandle-list",
174 "#phandle-cells-missing", 0, &args
);
175 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
176 rc
= of_count_phandle_with_args(np
, "phandle-list",
177 "#phandle-cells-missing");
178 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
180 /* Check for bad phandle in list */
181 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
182 "#phandle-cells", 0, &args
);
183 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
184 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
186 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
188 /* Check for incorrectly formed argument list */
189 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
190 "#phandle-cells", 1, &args
);
191 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
192 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
194 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
197 static void __init
of_selftest_property_match_string(void)
199 struct device_node
*np
;
202 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
204 pr_err("No testcase data in device tree\n");
208 rc
= of_property_match_string(np
, "phandle-list-names", "first");
209 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
210 rc
= of_property_match_string(np
, "phandle-list-names", "second");
211 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
212 rc
= of_property_match_string(np
, "phandle-list-names", "third");
213 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
214 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
215 selftest(rc
== -ENODATA
, "unmatched string; rc=%i", rc
);
216 rc
= of_property_match_string(np
, "missing-property", "blah");
217 selftest(rc
== -EINVAL
, "missing property; rc=%i", rc
);
218 rc
= of_property_match_string(np
, "empty-property", "blah");
219 selftest(rc
== -ENODATA
, "empty property; rc=%i", rc
);
220 rc
= of_property_match_string(np
, "unterminated-string", "blah");
221 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i", rc
);
224 static void __init
of_selftest_parse_interrupts(void)
226 struct device_node
*np
;
227 struct of_phandle_args args
;
230 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts0");
232 pr_err("missing testcase data\n");
236 for (i
= 0; i
< 4; i
++) {
239 rc
= of_irq_parse_one(np
, i
, &args
);
242 passed
&= (args
.args_count
== 1);
243 passed
&= (args
.args
[0] == (i
+ 1));
245 selftest(passed
, "index %i - data error on node %s rc=%i\n",
246 i
, args
.np
->full_name
, rc
);
250 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts1");
252 pr_err("missing testcase data\n");
256 for (i
= 0; i
< 4; i
++) {
259 rc
= of_irq_parse_one(np
, i
, &args
);
261 /* Test the values from tests-phandle.dtsi */
265 passed
&= (args
.args_count
== 1);
266 passed
&= (args
.args
[0] == 9);
270 passed
&= (args
.args_count
== 3);
271 passed
&= (args
.args
[0] == 10);
272 passed
&= (args
.args
[1] == 11);
273 passed
&= (args
.args
[2] == 12);
277 passed
&= (args
.args_count
== 2);
278 passed
&= (args
.args
[0] == 13);
279 passed
&= (args
.args
[1] == 14);
283 passed
&= (args
.args_count
== 2);
284 passed
&= (args
.args
[0] == 15);
285 passed
&= (args
.args
[1] == 16);
290 selftest(passed
, "index %i - data error on node %s rc=%i\n",
291 i
, args
.np
->full_name
, rc
);
296 static void __init
of_selftest_parse_interrupts_extended(void)
298 struct device_node
*np
;
299 struct of_phandle_args args
;
302 np
= of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
304 pr_err("missing testcase data\n");
308 for (i
= 0; i
< 7; i
++) {
310 rc
= of_irq_parse_one(np
, i
, &args
);
312 /* Test the values from tests-phandle.dtsi */
316 passed
&= (args
.args_count
== 1);
317 passed
&= (args
.args
[0] == 1);
321 passed
&= (args
.args_count
== 3);
322 passed
&= (args
.args
[0] == 2);
323 passed
&= (args
.args
[1] == 3);
324 passed
&= (args
.args
[2] == 4);
328 passed
&= (args
.args_count
== 2);
329 passed
&= (args
.args
[0] == 5);
330 passed
&= (args
.args
[1] == 6);
334 passed
&= (args
.args_count
== 1);
335 passed
&= (args
.args
[0] == 9);
339 passed
&= (args
.args_count
== 3);
340 passed
&= (args
.args
[0] == 10);
341 passed
&= (args
.args
[1] == 11);
342 passed
&= (args
.args
[2] == 12);
346 passed
&= (args
.args_count
== 2);
347 passed
&= (args
.args
[0] == 13);
348 passed
&= (args
.args
[1] == 14);
352 passed
&= (args
.args_count
== 1);
353 passed
&= (args
.args
[0] == 15);
359 selftest(passed
, "index %i - data error on node %s rc=%i\n",
360 i
, args
.np
->full_name
, rc
);
365 static struct of_device_id match_node_table
[] = {
366 { .data
= "A", .name
= "name0", }, /* Name alone is lowest priority */
367 { .data
= "B", .type
= "type1", }, /* followed by type alone */
369 { .data
= "Ca", .name
= "name2", .type
= "type1", }, /* followed by both together */
370 { .data
= "Cb", .name
= "name2", }, /* Only match when type doesn't match */
371 { .data
= "Cc", .name
= "name2", .type
= "type2", },
373 { .data
= "E", .compatible
= "compat3" },
374 { .data
= "G", .compatible
= "compat2", },
375 { .data
= "H", .compatible
= "compat2", .name
= "name5", },
376 { .data
= "I", .compatible
= "compat2", .type
= "type1", },
377 { .data
= "J", .compatible
= "compat2", .type
= "type1", .name
= "name8", },
378 { .data
= "K", .compatible
= "compat2", .name
= "name9", },
385 } match_node_tests
[] = {
386 { .path
= "/testcase-data/match-node/name0", .data
= "A", },
387 { .path
= "/testcase-data/match-node/name1", .data
= "B", },
388 { .path
= "/testcase-data/match-node/a/name2", .data
= "Ca", },
389 { .path
= "/testcase-data/match-node/b/name2", .data
= "Cb", },
390 { .path
= "/testcase-data/match-node/c/name2", .data
= "Cc", },
391 { .path
= "/testcase-data/match-node/name3", .data
= "E", },
392 { .path
= "/testcase-data/match-node/name4", .data
= "G", },
393 { .path
= "/testcase-data/match-node/name5", .data
= "H", },
394 { .path
= "/testcase-data/match-node/name6", .data
= "G", },
395 { .path
= "/testcase-data/match-node/name7", .data
= "I", },
396 { .path
= "/testcase-data/match-node/name8", .data
= "J", },
397 { .path
= "/testcase-data/match-node/name9", .data
= "K", },
400 static void __init
of_selftest_match_node(void)
402 struct device_node
*np
;
403 const struct of_device_id
*match
;
406 for (i
= 0; i
< ARRAY_SIZE(match_node_tests
); i
++) {
407 np
= of_find_node_by_path(match_node_tests
[i
].path
);
409 selftest(0, "missing testcase node %s\n",
410 match_node_tests
[i
].path
);
414 match
= of_match_node(match_node_table
, np
);
416 selftest(0, "%s didn't match anything\n",
417 match_node_tests
[i
].path
);
421 if (strcmp(match
->data
, match_node_tests
[i
].data
) != 0) {
422 selftest(0, "%s got wrong match. expected %s, got %s\n",
423 match_node_tests
[i
].path
, match_node_tests
[i
].data
,
424 (const char *)match
->data
);
427 selftest(1, "passed");
431 static void __init
of_selftest_platform_populate(void)
434 struct device_node
*np
;
435 struct platform_device
*pdev
;
437 np
= of_find_node_by_path("/testcase-data");
438 of_platform_populate(np
, of_default_bus_match_table
, NULL
, NULL
);
440 /* Test that a missing irq domain returns -EPROBE_DEFER */
441 np
= of_find_node_by_path("/testcase-data/testcase-device1");
442 pdev
= of_find_device_by_node(np
);
444 selftest(0, "device 1 creation failed\n");
445 irq
= platform_get_irq(pdev
, 0);
446 if (irq
!= -EPROBE_DEFER
)
447 selftest(0, "device deferred probe failed - %d\n", irq
);
449 /* Test that a parsing failure does not return -EPROBE_DEFER */
450 np
= of_find_node_by_path("/testcase-data/testcase-device2");
451 pdev
= of_find_device_by_node(np
);
453 selftest(0, "device 2 creation failed\n");
454 irq
= platform_get_irq(pdev
, 0);
455 if (irq
>= 0 || irq
== -EPROBE_DEFER
)
456 selftest(0, "device parsing error failed - %d\n", irq
);
458 selftest(1, "passed");
461 static int __init
of_selftest(void)
463 struct device_node
*np
;
465 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
467 pr_info("No testcase data in device tree; not running tests\n");
472 pr_info("start of selftest - you will see error messages\n");
473 of_selftest_dynamic();
474 of_selftest_parse_phandle_with_args();
475 of_selftest_property_match_string();
476 of_selftest_parse_interrupts();
477 of_selftest_parse_interrupts_extended();
478 of_selftest_match_node();
479 of_selftest_platform_populate();
480 pr_info("end of selftest - %i passed, %i failed\n",
481 selftest_results
.passed
, selftest_results
.failed
);
484 late_initcall(of_selftest
);