Merge branch 'stable-3.16' of git://git.infradead.org/users/pcmoore/selinux into...
[cascardo/linux.git] / drivers / of / selftest.c
1 /*
2  * Self tests for device tree subsystem
3  */
4
5 #define pr_fmt(fmt) "### dt-test ### " fmt
6
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>
18
19 static struct selftest_results {
20         int passed;
21         int failed;
22 } selftest_results;
23
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         } \
32 }
33
34 static void __init of_selftest_find_node_by_name(void)
35 {
36         struct device_node *np;
37
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);
42
43         /* Test if trailing '/' works */
44         np = of_find_node_by_path("/testcase-data/");
45         selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
46
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);
51
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);
56
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");
60
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);
65
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);
69
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);
73
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);
77 }
78
79 static void __init of_selftest_dynamic(void)
80 {
81         struct device_node *np;
82         struct property *prop;
83
84         np = of_find_node_by_path("/testcase-data");
85         if (!np) {
86                 pr_err("missing testcase data\n");
87                 return;
88         }
89
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;
95         }
96
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");
102
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");
110
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");
116
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");
124
125         /* Remove property - should pass */
126         selftest(of_remove_property(np, prop) == 0,
127                  "Removing a property should have passed\n");
128
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");
138 }
139
140 static void __init of_selftest_parse_phandle_with_args(void)
141 {
142         struct device_node *np;
143         struct of_phandle_args args;
144         int i, rc;
145
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;
150         }
151
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);
154
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);
159
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;
203                 }
204
205                 selftest(passed, "index %i - data error on node %s rc=%i\n",
206                          i, args.np->full_name, rc);
207         }
208
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);
216
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);
224
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);
232
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);
240 }
241
242 static void __init of_selftest_property_match_string(void)
243 {
244         struct device_node *np;
245         int rc;
246
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;
251         }
252
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);
267 }
268
269 static void __init of_selftest_parse_interrupts(void)
270 {
271         struct device_node *np;
272         struct of_phandle_args args;
273         int i, rc;
274
275         np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
276         if (!np) {
277                 pr_err("missing testcase data\n");
278                 return;
279         }
280
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);
285
286                 passed &= !rc;
287                 passed &= (args.args_count == 1);
288                 passed &= (args.args[0] == (i + 1));
289
290                 selftest(passed, "index %i - data error on node %s rc=%i\n",
291                          i, args.np->full_name, rc);
292         }
293         of_node_put(np);
294
295         np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
296         if (!np) {
297                 pr_err("missing testcase data\n");
298                 return;
299         }
300
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);
305
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;
334                 }
335                 selftest(passed, "index %i - data error on node %s rc=%i\n",
336                          i, args.np->full_name, rc);
337         }
338         of_node_put(np);
339 }
340
341 static void __init of_selftest_parse_interrupts_extended(void)
342 {
343         struct device_node *np;
344         struct of_phandle_args args;
345         int i, rc;
346
347         np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
348         if (!np) {
349                 pr_err("missing testcase data\n");
350                 return;
351         }
352
353         for (i = 0; i < 7; i++) {
354                 bool passed = true;
355                 rc = of_irq_parse_one(np, i, &args);
356
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;
402                 }
403
404                 selftest(passed, "index %i - data error on node %s rc=%i\n",
405                          i, args.np->full_name, rc);
406         }
407         of_node_put(np);
408 }
409
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 */
413
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", },
417
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", },
424         {}
425 };
426
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", },
443 };
444
445 static void __init of_selftest_match_node(void)
446 {
447         struct device_node *np;
448         const struct of_device_id *match;
449         int i;
450
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;
457                 }
458
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;
464                 }
465
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;
471                 }
472                 selftest(1, "passed");
473         }
474 }
475
476 static void __init of_selftest_platform_populate(void)
477 {
478         int irq;
479         struct device_node *np, *child;
480         struct platform_device *pdev;
481         struct of_device_id match[] = {
482                 { .compatible = "test-device", },
483                 {}
484         };
485
486         np = of_find_node_by_path("/testcase-data");
487         of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
488
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");
493
494         irq = platform_get_irq(pdev, 0);
495         selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
496
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);
503
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;
508         }
509
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);
517         }
518 }
519
520 static int __init of_selftest(void)
521 {
522         struct device_node *np;
523
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;
528         }
529         of_node_put(np);
530
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;
543 }
544 late_initcall(of_selftest);