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/list.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
18 static struct selftest_results {
23 #define selftest(result, fmt, ...) { \
25 selftest_results.failed++; \
26 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
28 selftest_results.passed++; \
29 pr_debug("pass %s():%i\n", __func__, __LINE__); \
33 static void __init of_selftest_parse_phandle_with_args(void)
35 struct device_node *np;
36 struct of_phandle_args args;
39 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
41 pr_err("missing testcase data\n");
45 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
46 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
48 for (i = 0; i < 8; i++) {
50 rc = of_parse_phandle_with_args(np, "phandle-list",
51 "#phandle-cells", i, &args);
53 /* Test the values from tests-phandle.dtsi */
57 passed &= (args.args_count == 1);
58 passed &= (args.args[0] == (i + 1));
62 passed &= (args.args_count == 2);
63 passed &= (args.args[0] == (i + 1));
64 passed &= (args.args[1] == 0);
67 passed &= (rc == -ENOENT);
71 passed &= (args.args_count == 3);
72 passed &= (args.args[0] == (i + 1));
73 passed &= (args.args[1] == 4);
74 passed &= (args.args[2] == 3);
78 passed &= (args.args_count == 2);
79 passed &= (args.args[0] == (i + 1));
80 passed &= (args.args[1] == 100);
84 passed &= (args.args_count == 0);
88 passed &= (args.args_count == 1);
89 passed &= (args.args[0] == (i + 1));
92 passed &= (rc == -ENOENT);
98 selftest(passed, "index %i - data error on node %s rc=%i\n",
99 i, args.np->full_name, rc);
102 /* Check for missing list property */
103 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
104 "#phandle-cells", 0, &args);
105 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
106 rc = of_count_phandle_with_args(np, "phandle-list-missing",
108 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
110 /* Check for missing cells property */
111 rc = of_parse_phandle_with_args(np, "phandle-list",
112 "#phandle-cells-missing", 0, &args);
113 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
114 rc = of_count_phandle_with_args(np, "phandle-list",
115 "#phandle-cells-missing");
116 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
118 /* Check for bad phandle in list */
119 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
120 "#phandle-cells", 0, &args);
121 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
122 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
124 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
126 /* Check for incorrectly formed argument list */
127 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
128 "#phandle-cells", 1, &args);
129 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
130 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
132 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
135 static void __init of_selftest_property_match_string(void)
137 struct device_node *np;
140 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
142 pr_err("No testcase data in device tree\n");
146 rc = of_property_match_string(np, "phandle-list-names", "first");
147 selftest(rc == 0, "first expected:0 got:%i\n", rc);
148 rc = of_property_match_string(np, "phandle-list-names", "second");
149 selftest(rc == 1, "second expected:0 got:%i\n", rc);
150 rc = of_property_match_string(np, "phandle-list-names", "third");
151 selftest(rc == 2, "third expected:0 got:%i\n", rc);
152 rc = of_property_match_string(np, "phandle-list-names", "fourth");
153 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
154 rc = of_property_match_string(np, "missing-property", "blah");
155 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
156 rc = of_property_match_string(np, "empty-property", "blah");
157 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
158 rc = of_property_match_string(np, "unterminated-string", "blah");
159 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
162 static void __init of_selftest_parse_interrupts(void)
164 struct device_node *np;
165 struct of_phandle_args args;
168 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
170 pr_err("missing testcase data\n");
174 for (i = 0; i < 4; i++) {
177 rc = of_irq_parse_one(np, i, &args);
180 passed &= (args.args_count == 1);
181 passed &= (args.args[0] == (i + 1));
183 selftest(passed, "index %i - data error on node %s rc=%i\n",
184 i, args.np->full_name, rc);
188 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
190 pr_err("missing testcase data\n");
194 for (i = 0; i < 4; i++) {
197 rc = of_irq_parse_one(np, i, &args);
199 /* Test the values from tests-phandle.dtsi */
203 passed &= (args.args_count == 1);
204 passed &= (args.args[0] == 9);
208 passed &= (args.args_count == 3);
209 passed &= (args.args[0] == 10);
210 passed &= (args.args[1] == 11);
211 passed &= (args.args[2] == 12);
215 passed &= (args.args_count == 2);
216 passed &= (args.args[0] == 13);
217 passed &= (args.args[1] == 14);
221 passed &= (args.args_count == 2);
222 passed &= (args.args[0] == 15);
223 passed &= (args.args[1] == 16);
228 selftest(passed, "index %i - data error on node %s rc=%i\n",
229 i, args.np->full_name, rc);
234 static void __init of_selftest_parse_interrupts_extended(void)
236 struct device_node *np;
237 struct of_phandle_args args;
240 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
242 pr_err("missing testcase data\n");
246 for (i = 0; i < 7; i++) {
248 rc = of_irq_parse_one(np, i, &args);
250 /* Test the values from tests-phandle.dtsi */
254 passed &= (args.args_count == 1);
255 passed &= (args.args[0] == 1);
259 passed &= (args.args_count == 3);
260 passed &= (args.args[0] == 2);
261 passed &= (args.args[1] == 3);
262 passed &= (args.args[2] == 4);
266 passed &= (args.args_count == 2);
267 passed &= (args.args[0] == 5);
268 passed &= (args.args[1] == 6);
272 passed &= (args.args_count == 1);
273 passed &= (args.args[0] == 9);
277 passed &= (args.args_count == 3);
278 passed &= (args.args[0] == 10);
279 passed &= (args.args[1] == 11);
280 passed &= (args.args[2] == 12);
284 passed &= (args.args_count == 2);
285 passed &= (args.args[0] == 13);
286 passed &= (args.args[1] == 14);
290 passed &= (args.args_count == 1);
291 passed &= (args.args[0] == 15);
297 selftest(passed, "index %i - data error on node %s rc=%i\n",
298 i, args.np->full_name, rc);
303 static struct of_device_id match_node_table[] = {
304 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
305 { .data = "B", .type = "type1", }, /* followed by type alone */
307 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
308 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
309 { .data = "Cc", .name = "name2", .type = "type2", },
311 { .data = "E", .compatible = "compat3" },
312 { .data = "G", .compatible = "compat2", },
313 { .data = "H", .compatible = "compat2", .name = "name5", },
314 { .data = "I", .compatible = "compat2", .type = "type1", },
315 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
316 { .data = "K", .compatible = "compat2", .name = "name9", },
323 } match_node_tests[] = {
324 { .path = "/testcase-data/match-node/name0", .data = "A", },
325 { .path = "/testcase-data/match-node/name1", .data = "B", },
326 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
327 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
328 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
329 { .path = "/testcase-data/match-node/name3", .data = "E", },
330 { .path = "/testcase-data/match-node/name4", .data = "G", },
331 { .path = "/testcase-data/match-node/name5", .data = "H", },
332 { .path = "/testcase-data/match-node/name6", .data = "G", },
333 { .path = "/testcase-data/match-node/name7", .data = "I", },
334 { .path = "/testcase-data/match-node/name8", .data = "J", },
335 { .path = "/testcase-data/match-node/name9", .data = "K", },
338 static void __init of_selftest_match_node(void)
340 struct device_node *np;
341 const struct of_device_id *match;
344 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
345 np = of_find_node_by_path(match_node_tests[i].path);
347 selftest(0, "missing testcase node %s\n",
348 match_node_tests[i].path);
352 match = of_match_node(match_node_table, np);
354 selftest(0, "%s didn't match anything\n",
355 match_node_tests[i].path);
359 if (strcmp(match->data, match_node_tests[i].data) != 0) {
360 selftest(0, "%s got wrong match. expected %s, got %s\n",
361 match_node_tests[i].path, match_node_tests[i].data,
362 (const char *)match->data);
365 selftest(1, "passed");
369 static int __init of_selftest(void)
371 struct device_node *np;
373 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
375 pr_info("No testcase data in device tree; not running tests\n");
380 pr_info("start of selftest - you will see error messages\n");
381 of_selftest_parse_phandle_with_args();
382 of_selftest_property_match_string();
383 of_selftest_parse_interrupts();
384 of_selftest_parse_interrupts_extended();
385 of_selftest_match_node();
386 pr_info("end of selftest - %i passed, %i failed\n",
387 selftest_results.passed, selftest_results.failed);
390 late_initcall(of_selftest);