]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/of/selftest.c
NVMe: CPU hot plug notification
[karo-tx-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/list.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17
18 static struct selftest_results {
19         int passed;
20         int failed;
21 } selftest_results;
22
23 #define selftest(result, fmt, ...) { \
24         if (!(result)) { \
25                 selftest_results.failed++; \
26                 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
27         } else { \
28                 selftest_results.passed++; \
29                 pr_debug("pass %s():%i\n", __func__, __LINE__); \
30         } \
31 }
32
33 static void __init of_selftest_parse_phandle_with_args(void)
34 {
35         struct device_node *np;
36         struct of_phandle_args args;
37         int i, rc;
38
39         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
40         if (!np) {
41                 pr_err("missing testcase data\n");
42                 return;
43         }
44
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);
47
48         for (i = 0; i < 8; i++) {
49                 bool passed = true;
50                 rc = of_parse_phandle_with_args(np, "phandle-list",
51                                                 "#phandle-cells", i, &args);
52
53                 /* Test the values from tests-phandle.dtsi */
54                 switch (i) {
55                 case 0:
56                         passed &= !rc;
57                         passed &= (args.args_count == 1);
58                         passed &= (args.args[0] == (i + 1));
59                         break;
60                 case 1:
61                         passed &= !rc;
62                         passed &= (args.args_count == 2);
63                         passed &= (args.args[0] == (i + 1));
64                         passed &= (args.args[1] == 0);
65                         break;
66                 case 2:
67                         passed &= (rc == -ENOENT);
68                         break;
69                 case 3:
70                         passed &= !rc;
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);
75                         break;
76                 case 4:
77                         passed &= !rc;
78                         passed &= (args.args_count == 2);
79                         passed &= (args.args[0] == (i + 1));
80                         passed &= (args.args[1] == 100);
81                         break;
82                 case 5:
83                         passed &= !rc;
84                         passed &= (args.args_count == 0);
85                         break;
86                 case 6:
87                         passed &= !rc;
88                         passed &= (args.args_count == 1);
89                         passed &= (args.args[0] == (i + 1));
90                         break;
91                 case 7:
92                         passed &= (rc == -ENOENT);
93                         break;
94                 default:
95                         passed = false;
96                 }
97
98                 selftest(passed, "index %i - data error on node %s rc=%i\n",
99                          i, args.np->full_name, rc);
100         }
101
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",
107                                         "#phandle-cells");
108         selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
109
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);
117
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",
123                                         "#phandle-cells");
124         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
125
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",
131                                         "#phandle-cells");
132         selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
133 }
134
135 static void __init of_selftest_property_match_string(void)
136 {
137         struct device_node *np;
138         int rc;
139
140         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
141         if (!np) {
142                 pr_err("No testcase data in device tree\n");
143                 return;
144         }
145
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);
160 }
161
162 static void __init of_selftest_parse_interrupts(void)
163 {
164         struct device_node *np;
165         struct of_phandle_args args;
166         int i, rc;
167
168         np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
169         if (!np) {
170                 pr_err("missing testcase data\n");
171                 return;
172         }
173
174         for (i = 0; i < 4; i++) {
175                 bool passed = true;
176                 args.args_count = 0;
177                 rc = of_irq_parse_one(np, i, &args);
178
179                 passed &= !rc;
180                 passed &= (args.args_count == 1);
181                 passed &= (args.args[0] == (i + 1));
182
183                 selftest(passed, "index %i - data error on node %s rc=%i\n",
184                          i, args.np->full_name, rc);
185         }
186         of_node_put(np);
187
188         np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
189         if (!np) {
190                 pr_err("missing testcase data\n");
191                 return;
192         }
193
194         for (i = 0; i < 4; i++) {
195                 bool passed = true;
196                 args.args_count = 0;
197                 rc = of_irq_parse_one(np, i, &args);
198
199                 /* Test the values from tests-phandle.dtsi */
200                 switch (i) {
201                 case 0:
202                         passed &= !rc;
203                         passed &= (args.args_count == 1);
204                         passed &= (args.args[0] == 9);
205                         break;
206                 case 1:
207                         passed &= !rc;
208                         passed &= (args.args_count == 3);
209                         passed &= (args.args[0] == 10);
210                         passed &= (args.args[1] == 11);
211                         passed &= (args.args[2] == 12);
212                         break;
213                 case 2:
214                         passed &= !rc;
215                         passed &= (args.args_count == 2);
216                         passed &= (args.args[0] == 13);
217                         passed &= (args.args[1] == 14);
218                         break;
219                 case 3:
220                         passed &= !rc;
221                         passed &= (args.args_count == 2);
222                         passed &= (args.args[0] == 15);
223                         passed &= (args.args[1] == 16);
224                         break;
225                 default:
226                         passed = false;
227                 }
228                 selftest(passed, "index %i - data error on node %s rc=%i\n",
229                          i, args.np->full_name, rc);
230         }
231         of_node_put(np);
232 }
233
234 static void __init of_selftest_parse_interrupts_extended(void)
235 {
236         struct device_node *np;
237         struct of_phandle_args args;
238         int i, rc;
239
240         np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
241         if (!np) {
242                 pr_err("missing testcase data\n");
243                 return;
244         }
245
246         for (i = 0; i < 7; i++) {
247                 bool passed = true;
248                 rc = of_irq_parse_one(np, i, &args);
249
250                 /* Test the values from tests-phandle.dtsi */
251                 switch (i) {
252                 case 0:
253                         passed &= !rc;
254                         passed &= (args.args_count == 1);
255                         passed &= (args.args[0] == 1);
256                         break;
257                 case 1:
258                         passed &= !rc;
259                         passed &= (args.args_count == 3);
260                         passed &= (args.args[0] == 2);
261                         passed &= (args.args[1] == 3);
262                         passed &= (args.args[2] == 4);
263                         break;
264                 case 2:
265                         passed &= !rc;
266                         passed &= (args.args_count == 2);
267                         passed &= (args.args[0] == 5);
268                         passed &= (args.args[1] == 6);
269                         break;
270                 case 3:
271                         passed &= !rc;
272                         passed &= (args.args_count == 1);
273                         passed &= (args.args[0] == 9);
274                         break;
275                 case 4:
276                         passed &= !rc;
277                         passed &= (args.args_count == 3);
278                         passed &= (args.args[0] == 10);
279                         passed &= (args.args[1] == 11);
280                         passed &= (args.args[2] == 12);
281                         break;
282                 case 5:
283                         passed &= !rc;
284                         passed &= (args.args_count == 2);
285                         passed &= (args.args[0] == 13);
286                         passed &= (args.args[1] == 14);
287                         break;
288                 case 6:
289                         passed &= !rc;
290                         passed &= (args.args_count == 1);
291                         passed &= (args.args[0] == 15);
292                         break;
293                 default:
294                         passed = false;
295                 }
296
297                 selftest(passed, "index %i - data error on node %s rc=%i\n",
298                          i, args.np->full_name, rc);
299         }
300         of_node_put(np);
301 }
302
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 */
306
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", },
310
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", },
317         {}
318 };
319
320 static struct {
321         const char *path;
322         const char *data;
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", },
336 };
337
338 static void __init of_selftest_match_node(void)
339 {
340         struct device_node *np;
341         const struct of_device_id *match;
342         int i;
343
344         for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
345                 np = of_find_node_by_path(match_node_tests[i].path);
346                 if (!np) {
347                         selftest(0, "missing testcase node %s\n",
348                                 match_node_tests[i].path);
349                         continue;
350                 }
351
352                 match = of_match_node(match_node_table, np);
353                 if (!match) {
354                         selftest(0, "%s didn't match anything\n",
355                                 match_node_tests[i].path);
356                         continue;
357                 }
358
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);
363                         continue;
364                 }
365                 selftest(1, "passed");
366         }
367 }
368
369 static int __init of_selftest(void)
370 {
371         struct device_node *np;
372
373         np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
374         if (!np) {
375                 pr_info("No testcase data in device tree; not running tests\n");
376                 return 0;
377         }
378         of_node_put(np);
379
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);
388         return 0;
389 }
390 late_initcall(of_selftest);