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_dynamic(void)
35 struct device_node *np;
36 struct property *prop;
38 np = of_find_node_by_path("/testcase-data");
40 pr_err("missing testcase data\n");
44 /* Array of 4 properties for the purpose of testing */
45 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
47 selftest(0, "kzalloc() failed\n");
51 /* Add a new property - should pass*/
52 prop->name = "new-property";
53 prop->value = "new-property-data";
54 prop->length = strlen(prop->value);
55 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
57 /* Try to add an existing property - should fail */
59 prop->name = "new-property";
60 prop->value = "new-property-data-should-fail";
61 prop->length = strlen(prop->value);
62 selftest(of_add_property(np, prop) != 0,
63 "Adding an existing property should have failed\n");
65 /* Try to modify an existing property - should pass */
66 prop->value = "modify-property-data-should-pass";
67 prop->length = strlen(prop->value);
68 selftest(of_update_property(np, prop) == 0,
69 "Updating an existing property should have passed\n");
71 /* Try to modify non-existent property - should pass*/
73 prop->name = "modify-property";
74 prop->value = "modify-missing-property-data-should-pass";
75 prop->length = strlen(prop->value);
76 selftest(of_update_property(np, prop) == 0,
77 "Updating a missing property should have passed\n");
79 /* Remove property - should pass */
80 selftest(of_remove_property(np, prop) == 0,
81 "Removing a property should have passed\n");
83 /* Adding very large property - should pass */
85 prop->name = "large-property-PAGE_SIZEx8";
86 prop->length = PAGE_SIZE * 8;
87 prop->value = kzalloc(prop->length, GFP_KERNEL);
88 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
90 selftest(of_add_property(np, prop) == 0,
91 "Adding a large property should have passed\n");
94 static void __init of_selftest_parse_phandle_with_args(void)
96 struct device_node *np;
97 struct of_phandle_args args;
100 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
102 pr_err("missing testcase data\n");
106 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
107 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
109 for (i = 0; i < 8; i++) {
111 rc = of_parse_phandle_with_args(np, "phandle-list",
112 "#phandle-cells", i, &args);
114 /* Test the values from tests-phandle.dtsi */
118 passed &= (args.args_count == 1);
119 passed &= (args.args[0] == (i + 1));
123 passed &= (args.args_count == 2);
124 passed &= (args.args[0] == (i + 1));
125 passed &= (args.args[1] == 0);
128 passed &= (rc == -ENOENT);
132 passed &= (args.args_count == 3);
133 passed &= (args.args[0] == (i + 1));
134 passed &= (args.args[1] == 4);
135 passed &= (args.args[2] == 3);
139 passed &= (args.args_count == 2);
140 passed &= (args.args[0] == (i + 1));
141 passed &= (args.args[1] == 100);
145 passed &= (args.args_count == 0);
149 passed &= (args.args_count == 1);
150 passed &= (args.args[0] == (i + 1));
153 passed &= (rc == -ENOENT);
159 selftest(passed, "index %i - data error on node %s rc=%i\n",
160 i, args.np->full_name, rc);
163 /* Check for missing list property */
164 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
165 "#phandle-cells", 0, &args);
166 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
167 rc = of_count_phandle_with_args(np, "phandle-list-missing",
169 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
171 /* Check for missing cells property */
172 rc = of_parse_phandle_with_args(np, "phandle-list",
173 "#phandle-cells-missing", 0, &args);
174 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
175 rc = of_count_phandle_with_args(np, "phandle-list",
176 "#phandle-cells-missing");
177 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
179 /* Check for bad phandle in list */
180 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
181 "#phandle-cells", 0, &args);
182 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
183 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
185 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
187 /* Check for incorrectly formed argument list */
188 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
189 "#phandle-cells", 1, &args);
190 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
191 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
193 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
196 static void __init of_selftest_property_match_string(void)
198 struct device_node *np;
201 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
203 pr_err("No testcase data in device tree\n");
207 rc = of_property_match_string(np, "phandle-list-names", "first");
208 selftest(rc == 0, "first expected:0 got:%i\n", rc);
209 rc = of_property_match_string(np, "phandle-list-names", "second");
210 selftest(rc == 1, "second expected:0 got:%i\n", rc);
211 rc = of_property_match_string(np, "phandle-list-names", "third");
212 selftest(rc == 2, "third expected:0 got:%i\n", rc);
213 rc = of_property_match_string(np, "phandle-list-names", "fourth");
214 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
215 rc = of_property_match_string(np, "missing-property", "blah");
216 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
217 rc = of_property_match_string(np, "empty-property", "blah");
218 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
219 rc = of_property_match_string(np, "unterminated-string", "blah");
220 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
223 static void __init of_selftest_parse_interrupts(void)
225 struct device_node *np;
226 struct of_phandle_args args;
229 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
231 pr_err("missing testcase data\n");
235 for (i = 0; i < 4; i++) {
238 rc = of_irq_parse_one(np, i, &args);
241 passed &= (args.args_count == 1);
242 passed &= (args.args[0] == (i + 1));
244 selftest(passed, "index %i - data error on node %s rc=%i\n",
245 i, args.np->full_name, rc);
249 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
251 pr_err("missing testcase data\n");
255 for (i = 0; i < 4; i++) {
258 rc = of_irq_parse_one(np, i, &args);
260 /* Test the values from tests-phandle.dtsi */
264 passed &= (args.args_count == 1);
265 passed &= (args.args[0] == 9);
269 passed &= (args.args_count == 3);
270 passed &= (args.args[0] == 10);
271 passed &= (args.args[1] == 11);
272 passed &= (args.args[2] == 12);
276 passed &= (args.args_count == 2);
277 passed &= (args.args[0] == 13);
278 passed &= (args.args[1] == 14);
282 passed &= (args.args_count == 2);
283 passed &= (args.args[0] == 15);
284 passed &= (args.args[1] == 16);
289 selftest(passed, "index %i - data error on node %s rc=%i\n",
290 i, args.np->full_name, rc);
295 static void __init of_selftest_parse_interrupts_extended(void)
297 struct device_node *np;
298 struct of_phandle_args args;
301 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
303 pr_err("missing testcase data\n");
307 for (i = 0; i < 7; i++) {
309 rc = of_irq_parse_one(np, i, &args);
311 /* Test the values from tests-phandle.dtsi */
315 passed &= (args.args_count == 1);
316 passed &= (args.args[0] == 1);
320 passed &= (args.args_count == 3);
321 passed &= (args.args[0] == 2);
322 passed &= (args.args[1] == 3);
323 passed &= (args.args[2] == 4);
327 passed &= (args.args_count == 2);
328 passed &= (args.args[0] == 5);
329 passed &= (args.args[1] == 6);
333 passed &= (args.args_count == 1);
334 passed &= (args.args[0] == 9);
338 passed &= (args.args_count == 3);
339 passed &= (args.args[0] == 10);
340 passed &= (args.args[1] == 11);
341 passed &= (args.args[2] == 12);
345 passed &= (args.args_count == 2);
346 passed &= (args.args[0] == 13);
347 passed &= (args.args[1] == 14);
351 passed &= (args.args_count == 1);
352 passed &= (args.args[0] == 15);
358 selftest(passed, "index %i - data error on node %s rc=%i\n",
359 i, args.np->full_name, rc);
364 static struct of_device_id match_node_table[] = {
365 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
366 { .data = "B", .type = "type1", }, /* followed by type alone */
368 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
369 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
370 { .data = "Cc", .name = "name2", .type = "type2", },
372 { .data = "E", .compatible = "compat3" },
373 { .data = "G", .compatible = "compat2", },
374 { .data = "H", .compatible = "compat2", .name = "name5", },
375 { .data = "I", .compatible = "compat2", .type = "type1", },
376 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
377 { .data = "K", .compatible = "compat2", .name = "name9", },
384 } match_node_tests[] = {
385 { .path = "/testcase-data/match-node/name0", .data = "A", },
386 { .path = "/testcase-data/match-node/name1", .data = "B", },
387 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
388 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
389 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
390 { .path = "/testcase-data/match-node/name3", .data = "E", },
391 { .path = "/testcase-data/match-node/name4", .data = "G", },
392 { .path = "/testcase-data/match-node/name5", .data = "H", },
393 { .path = "/testcase-data/match-node/name6", .data = "G", },
394 { .path = "/testcase-data/match-node/name7", .data = "I", },
395 { .path = "/testcase-data/match-node/name8", .data = "J", },
396 { .path = "/testcase-data/match-node/name9", .data = "K", },
399 static void __init of_selftest_match_node(void)
401 struct device_node *np;
402 const struct of_device_id *match;
405 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
406 np = of_find_node_by_path(match_node_tests[i].path);
408 selftest(0, "missing testcase node %s\n",
409 match_node_tests[i].path);
413 match = of_match_node(match_node_table, np);
415 selftest(0, "%s didn't match anything\n",
416 match_node_tests[i].path);
420 if (strcmp(match->data, match_node_tests[i].data) != 0) {
421 selftest(0, "%s got wrong match. expected %s, got %s\n",
422 match_node_tests[i].path, match_node_tests[i].data,
423 (const char *)match->data);
426 selftest(1, "passed");
430 static int __init of_selftest(void)
432 struct device_node *np;
434 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
436 pr_info("No testcase data in device tree; not running tests\n");
441 pr_info("start of selftest - you will see error messages\n");
442 of_selftest_dynamic();
443 of_selftest_parse_phandle_with_args();
444 of_selftest_property_match_string();
445 of_selftest_parse_interrupts();
446 of_selftest_parse_interrupts_extended();
447 of_selftest_match_node();
448 pr_info("end of selftest - %i passed, %i failed\n",
449 selftest_results.passed, selftest_results.failed);
452 late_initcall(of_selftest);