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 int __init of_selftest(void)
305 struct device_node *np;
307 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
309 pr_info("No testcase data in device tree; not running tests\n");
314 pr_info("start of selftest - you will see error messages\n");
315 of_selftest_parse_phandle_with_args();
316 of_selftest_property_match_string();
317 of_selftest_parse_interrupts();
318 of_selftest_parse_interrupts_extended();
319 pr_info("end of selftest - %i passed, %i failed\n",
320 selftest_results.passed, selftest_results.failed);
323 late_initcall(of_selftest);