ima: fix ima_alloc_atfm()
[linux/fpc-iii.git] / drivers / of / selftest.c
blob077314eebb95c785b1f1cb8f46790f6004464ee3
1 /*
2 * Self tests for device tree subsystem
3 */
5 #define pr_fmt(fmt) "### dt-test ### " fmt
7 #include <linux/clk.h>
8 #include <linux/err.h>
9 #include <linux/errno.h>
10 #include <linux/module.h>
11 #include <linux/of.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 {
20 int passed;
21 int failed;
22 } selftest_results;
24 #define selftest(result, fmt, ...) { \
25 if (!(result)) { \
26 selftest_results.failed++; \
27 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
28 } else { \
29 selftest_results.passed++; \
30 pr_debug("pass %s():%i\n", __func__, __LINE__); \
31 } \
34 static void __init of_selftest_find_node_by_name(void)
36 struct device_node *np;
38 np = of_find_node_by_path("/testcase-data");
39 selftest(np && !strcmp("/testcase-data", np->full_name),
40 "find /testcase-data failed\n");
41 of_node_put(np);
43 /* Test if trailing '/' works */
44 np = of_find_node_by_path("/testcase-data/");
45 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
47 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
48 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
49 "find /testcase-data/phandle-tests/consumer-a failed\n");
50 of_node_put(np);
52 np = of_find_node_by_path("testcase-alias");
53 selftest(np && !strcmp("/testcase-data", np->full_name),
54 "find testcase-alias failed\n");
55 of_node_put(np);
57 /* Test if trailing '/' works on aliases */
58 np = of_find_node_by_path("testcase-alias/");
59 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
61 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
62 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
63 "find testcase-alias/phandle-tests/consumer-a failed\n");
64 of_node_put(np);
66 np = of_find_node_by_path("/testcase-data/missing-path");
67 selftest(!np, "non-existent path returned node %s\n", np->full_name);
68 of_node_put(np);
70 np = of_find_node_by_path("missing-alias");
71 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
72 of_node_put(np);
74 np = of_find_node_by_path("testcase-alias/missing-path");
75 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
76 of_node_put(np);
79 static void __init of_selftest_dynamic(void)
81 struct device_node *np;
82 struct property *prop;
84 np = of_find_node_by_path("/testcase-data");
85 if (!np) {
86 pr_err("missing testcase data\n");
87 return;
90 /* Array of 4 properties for the purpose of testing */
91 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
92 if (!prop) {
93 selftest(0, "kzalloc() failed\n");
94 return;
97 /* Add a new property - should pass*/
98 prop->name = "new-property";
99 prop->value = "new-property-data";
100 prop->length = strlen(prop->value);
101 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
103 /* Try to add an existing property - should fail */
104 prop++;
105 prop->name = "new-property";
106 prop->value = "new-property-data-should-fail";
107 prop->length = strlen(prop->value);
108 selftest(of_add_property(np, prop) != 0,
109 "Adding an existing property should have failed\n");
111 /* Try to modify an existing property - should pass */
112 prop->value = "modify-property-data-should-pass";
113 prop->length = strlen(prop->value);
114 selftest(of_update_property(np, prop) == 0,
115 "Updating an existing property should have passed\n");
117 /* Try to modify non-existent property - should pass*/
118 prop++;
119 prop->name = "modify-property";
120 prop->value = "modify-missing-property-data-should-pass";
121 prop->length = strlen(prop->value);
122 selftest(of_update_property(np, prop) == 0,
123 "Updating a missing property should have passed\n");
125 /* Remove property - should pass */
126 selftest(of_remove_property(np, prop) == 0,
127 "Removing a property should have passed\n");
129 /* Adding very large property - should pass */
130 prop++;
131 prop->name = "large-property-PAGE_SIZEx8";
132 prop->length = PAGE_SIZE * 8;
133 prop->value = kzalloc(prop->length, GFP_KERNEL);
134 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
135 if (prop->value)
136 selftest(of_add_property(np, prop) == 0,
137 "Adding a large property should have passed\n");
140 static void __init of_selftest_parse_phandle_with_args(void)
142 struct device_node *np;
143 struct of_phandle_args args;
144 int i, rc;
146 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
147 if (!np) {
148 pr_err("missing testcase data\n");
149 return;
152 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
153 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
155 for (i = 0; i < 8; i++) {
156 bool passed = true;
157 rc = of_parse_phandle_with_args(np, "phandle-list",
158 "#phandle-cells", i, &args);
160 /* Test the values from tests-phandle.dtsi */
161 switch (i) {
162 case 0:
163 passed &= !rc;
164 passed &= (args.args_count == 1);
165 passed &= (args.args[0] == (i + 1));
166 break;
167 case 1:
168 passed &= !rc;
169 passed &= (args.args_count == 2);
170 passed &= (args.args[0] == (i + 1));
171 passed &= (args.args[1] == 0);
172 break;
173 case 2:
174 passed &= (rc == -ENOENT);
175 break;
176 case 3:
177 passed &= !rc;
178 passed &= (args.args_count == 3);
179 passed &= (args.args[0] == (i + 1));
180 passed &= (args.args[1] == 4);
181 passed &= (args.args[2] == 3);
182 break;
183 case 4:
184 passed &= !rc;
185 passed &= (args.args_count == 2);
186 passed &= (args.args[0] == (i + 1));
187 passed &= (args.args[1] == 100);
188 break;
189 case 5:
190 passed &= !rc;
191 passed &= (args.args_count == 0);
192 break;
193 case 6:
194 passed &= !rc;
195 passed &= (args.args_count == 1);
196 passed &= (args.args[0] == (i + 1));
197 break;
198 case 7:
199 passed &= (rc == -ENOENT);
200 break;
201 default:
202 passed = false;
205 selftest(passed, "index %i - data error on node %s rc=%i\n",
206 i, args.np->full_name, rc);
209 /* Check for missing list property */
210 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
211 "#phandle-cells", 0, &args);
212 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
213 rc = of_count_phandle_with_args(np, "phandle-list-missing",
214 "#phandle-cells");
215 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
217 /* Check for missing cells property */
218 rc = of_parse_phandle_with_args(np, "phandle-list",
219 "#phandle-cells-missing", 0, &args);
220 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
221 rc = of_count_phandle_with_args(np, "phandle-list",
222 "#phandle-cells-missing");
223 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
225 /* Check for bad phandle in list */
226 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
227 "#phandle-cells", 0, &args);
228 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
229 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
230 "#phandle-cells");
231 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
233 /* Check for incorrectly formed argument list */
234 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
235 "#phandle-cells", 1, &args);
236 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
237 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
238 "#phandle-cells");
239 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
242 static void __init of_selftest_property_match_string(void)
244 struct device_node *np;
245 int rc;
247 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
248 if (!np) {
249 pr_err("No testcase data in device tree\n");
250 return;
253 rc = of_property_match_string(np, "phandle-list-names", "first");
254 selftest(rc == 0, "first expected:0 got:%i\n", rc);
255 rc = of_property_match_string(np, "phandle-list-names", "second");
256 selftest(rc == 1, "second expected:0 got:%i\n", rc);
257 rc = of_property_match_string(np, "phandle-list-names", "third");
258 selftest(rc == 2, "third expected:0 got:%i\n", rc);
259 rc = of_property_match_string(np, "phandle-list-names", "fourth");
260 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
261 rc = of_property_match_string(np, "missing-property", "blah");
262 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
263 rc = of_property_match_string(np, "empty-property", "blah");
264 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
265 rc = of_property_match_string(np, "unterminated-string", "blah");
266 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
269 static void __init of_selftest_parse_interrupts(void)
271 struct device_node *np;
272 struct of_phandle_args args;
273 int i, rc;
275 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
276 if (!np) {
277 pr_err("missing testcase data\n");
278 return;
281 for (i = 0; i < 4; i++) {
282 bool passed = true;
283 args.args_count = 0;
284 rc = of_irq_parse_one(np, i, &args);
286 passed &= !rc;
287 passed &= (args.args_count == 1);
288 passed &= (args.args[0] == (i + 1));
290 selftest(passed, "index %i - data error on node %s rc=%i\n",
291 i, args.np->full_name, rc);
293 of_node_put(np);
295 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
296 if (!np) {
297 pr_err("missing testcase data\n");
298 return;
301 for (i = 0; i < 4; i++) {
302 bool passed = true;
303 args.args_count = 0;
304 rc = of_irq_parse_one(np, i, &args);
306 /* Test the values from tests-phandle.dtsi */
307 switch (i) {
308 case 0:
309 passed &= !rc;
310 passed &= (args.args_count == 1);
311 passed &= (args.args[0] == 9);
312 break;
313 case 1:
314 passed &= !rc;
315 passed &= (args.args_count == 3);
316 passed &= (args.args[0] == 10);
317 passed &= (args.args[1] == 11);
318 passed &= (args.args[2] == 12);
319 break;
320 case 2:
321 passed &= !rc;
322 passed &= (args.args_count == 2);
323 passed &= (args.args[0] == 13);
324 passed &= (args.args[1] == 14);
325 break;
326 case 3:
327 passed &= !rc;
328 passed &= (args.args_count == 2);
329 passed &= (args.args[0] == 15);
330 passed &= (args.args[1] == 16);
331 break;
332 default:
333 passed = false;
335 selftest(passed, "index %i - data error on node %s rc=%i\n",
336 i, args.np->full_name, rc);
338 of_node_put(np);
341 static void __init of_selftest_parse_interrupts_extended(void)
343 struct device_node *np;
344 struct of_phandle_args args;
345 int i, rc;
347 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
348 if (!np) {
349 pr_err("missing testcase data\n");
350 return;
353 for (i = 0; i < 7; i++) {
354 bool passed = true;
355 rc = of_irq_parse_one(np, i, &args);
357 /* Test the values from tests-phandle.dtsi */
358 switch (i) {
359 case 0:
360 passed &= !rc;
361 passed &= (args.args_count == 1);
362 passed &= (args.args[0] == 1);
363 break;
364 case 1:
365 passed &= !rc;
366 passed &= (args.args_count == 3);
367 passed &= (args.args[0] == 2);
368 passed &= (args.args[1] == 3);
369 passed &= (args.args[2] == 4);
370 break;
371 case 2:
372 passed &= !rc;
373 passed &= (args.args_count == 2);
374 passed &= (args.args[0] == 5);
375 passed &= (args.args[1] == 6);
376 break;
377 case 3:
378 passed &= !rc;
379 passed &= (args.args_count == 1);
380 passed &= (args.args[0] == 9);
381 break;
382 case 4:
383 passed &= !rc;
384 passed &= (args.args_count == 3);
385 passed &= (args.args[0] == 10);
386 passed &= (args.args[1] == 11);
387 passed &= (args.args[2] == 12);
388 break;
389 case 5:
390 passed &= !rc;
391 passed &= (args.args_count == 2);
392 passed &= (args.args[0] == 13);
393 passed &= (args.args[1] == 14);
394 break;
395 case 6:
396 passed &= !rc;
397 passed &= (args.args_count == 1);
398 passed &= (args.args[0] == 15);
399 break;
400 default:
401 passed = false;
404 selftest(passed, "index %i - data error on node %s rc=%i\n",
405 i, args.np->full_name, rc);
407 of_node_put(np);
410 static struct of_device_id match_node_table[] = {
411 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
412 { .data = "B", .type = "type1", }, /* followed by type alone */
414 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
415 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
416 { .data = "Cc", .name = "name2", .type = "type2", },
418 { .data = "E", .compatible = "compat3" },
419 { .data = "G", .compatible = "compat2", },
420 { .data = "H", .compatible = "compat2", .name = "name5", },
421 { .data = "I", .compatible = "compat2", .type = "type1", },
422 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
423 { .data = "K", .compatible = "compat2", .name = "name9", },
427 static struct {
428 const char *path;
429 const char *data;
430 } match_node_tests[] = {
431 { .path = "/testcase-data/match-node/name0", .data = "A", },
432 { .path = "/testcase-data/match-node/name1", .data = "B", },
433 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
434 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
435 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
436 { .path = "/testcase-data/match-node/name3", .data = "E", },
437 { .path = "/testcase-data/match-node/name4", .data = "G", },
438 { .path = "/testcase-data/match-node/name5", .data = "H", },
439 { .path = "/testcase-data/match-node/name6", .data = "G", },
440 { .path = "/testcase-data/match-node/name7", .data = "I", },
441 { .path = "/testcase-data/match-node/name8", .data = "J", },
442 { .path = "/testcase-data/match-node/name9", .data = "K", },
445 static void __init of_selftest_match_node(void)
447 struct device_node *np;
448 const struct of_device_id *match;
449 int i;
451 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
452 np = of_find_node_by_path(match_node_tests[i].path);
453 if (!np) {
454 selftest(0, "missing testcase node %s\n",
455 match_node_tests[i].path);
456 continue;
459 match = of_match_node(match_node_table, np);
460 if (!match) {
461 selftest(0, "%s didn't match anything\n",
462 match_node_tests[i].path);
463 continue;
466 if (strcmp(match->data, match_node_tests[i].data) != 0) {
467 selftest(0, "%s got wrong match. expected %s, got %s\n",
468 match_node_tests[i].path, match_node_tests[i].data,
469 (const char *)match->data);
470 continue;
472 selftest(1, "passed");
476 static void __init of_selftest_platform_populate(void)
478 int irq;
479 struct device_node *np, *child;
480 struct platform_device *pdev;
481 struct of_device_id match[] = {
482 { .compatible = "test-device", },
486 np = of_find_node_by_path("/testcase-data");
487 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
489 /* Test that a missing irq domain returns -EPROBE_DEFER */
490 np = of_find_node_by_path("/testcase-data/testcase-device1");
491 pdev = of_find_device_by_node(np);
492 selftest(pdev, "device 1 creation failed\n");
494 irq = platform_get_irq(pdev, 0);
495 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
497 /* Test that a parsing failure does not return -EPROBE_DEFER */
498 np = of_find_node_by_path("/testcase-data/testcase-device2");
499 pdev = of_find_device_by_node(np);
500 selftest(pdev, "device 2 creation failed\n");
501 irq = platform_get_irq(pdev, 0);
502 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
504 np = of_find_node_by_path("/testcase-data/platform-tests");
505 if (!np) {
506 pr_err("No testcase data in device tree\n");
507 return;
510 for_each_child_of_node(np, child) {
511 struct device_node *grandchild;
512 of_platform_populate(child, match, NULL, NULL);
513 for_each_child_of_node(child, grandchild)
514 selftest(of_find_device_by_node(grandchild),
515 "Could not create device for node '%s'\n",
516 grandchild->name);
520 static int __init of_selftest(void)
522 struct device_node *np;
524 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
525 if (!np) {
526 pr_info("No testcase data in device tree; not running tests\n");
527 return 0;
529 of_node_put(np);
531 pr_info("start of selftest - you will see error messages\n");
532 of_selftest_find_node_by_name();
533 of_selftest_dynamic();
534 of_selftest_parse_phandle_with_args();
535 of_selftest_property_match_string();
536 of_selftest_parse_interrupts();
537 of_selftest_parse_interrupts_extended();
538 of_selftest_match_node();
539 of_selftest_platform_populate();
540 pr_info("end of selftest - %i passed, %i failed\n",
541 selftest_results.passed, selftest_results.failed);
542 return 0;
544 late_initcall(of_selftest);