blob: ae4450070503f1067579f02576ce5ec14b2a7192 [file] [log] [blame]
Grant Likely53a42092011-12-12 09:25:57 -07001/*
2 * Self tests for device tree subsystem
3 */
4
Grant Likelycabb7d52013-02-12 21:19:37 +00005#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07006
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>
Grant Likelya9f10ca2013-10-11 22:04:23 +010012#include <linux/of_irq.h>
Grant Likely53a42092011-12-12 09:25:57 -070013#include <linux/list.h>
14#include <linux/mutex.h>
15#include <linux/slab.h>
16#include <linux/device.h>
17
Grant Likelya9f10ca2013-10-11 22:04:23 +010018static struct selftest_results {
19 int passed;
20 int failed;
21} selftest_results;
22
Grant Likely53a42092011-12-12 09:25:57 -070023#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000024 if (!(result)) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010025 selftest_results.failed++; \
26 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000027 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010028 selftest_results.passed++; \
29 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000030 } \
Grant Likely53a42092011-12-12 09:25:57 -070031}
32
Grant Likely7e66c5c2013-11-15 17:19:09 +000033static void __init of_selftest_dynamic(void)
34{
35 struct device_node *np;
36 struct property *prop;
37
38 np = of_find_node_by_path("/testcase-data");
39 if (!np) {
40 pr_err("missing testcase data\n");
41 return;
42 }
43
44 /* Array of 4 properties for the purpose of testing */
45 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
46 if (!prop) {
47 selftest(0, "kzalloc() failed\n");
48 return;
49 }
50
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");
56
57 /* Try to add an existing property - should fail */
58 prop++;
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");
64
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");
70
71 /* Try to modify non-existent property - should pass*/
72 prop++;
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");
78
79 /* Remove property - should pass */
80 selftest(of_remove_property(np, prop) == 0,
81 "Removing a property should have passed\n");
82
83 /* Adding very large property - should pass */
84 prop++;
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");
89 if (prop->value)
90 selftest(of_add_property(np, prop) == 0,
91 "Adding a large property should have passed\n");
92}
93
Grant Likely53a42092011-12-12 09:25:57 -070094static void __init of_selftest_parse_phandle_with_args(void)
95{
96 struct device_node *np;
97 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +000098 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -070099
Grant Likely53a42092011-12-12 09:25:57 -0700100 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
101 if (!np) {
102 pr_err("missing testcase data\n");
103 return;
104 }
105
Grant Likelybd69f732013-02-10 22:57:21 +0000106 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);
108
Grant Likelyf7f951c2013-02-12 17:41:22 +0000109 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700110 bool passed = true;
111 rc = of_parse_phandle_with_args(np, "phandle-list",
112 "#phandle-cells", i, &args);
113
114 /* Test the values from tests-phandle.dtsi */
115 switch (i) {
116 case 0:
117 passed &= !rc;
118 passed &= (args.args_count == 1);
119 passed &= (args.args[0] == (i + 1));
120 break;
121 case 1:
122 passed &= !rc;
123 passed &= (args.args_count == 2);
124 passed &= (args.args[0] == (i + 1));
125 passed &= (args.args[1] == 0);
126 break;
127 case 2:
128 passed &= (rc == -ENOENT);
129 break;
130 case 3:
131 passed &= !rc;
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);
136 break;
137 case 4:
138 passed &= !rc;
139 passed &= (args.args_count == 2);
140 passed &= (args.args[0] == (i + 1));
141 passed &= (args.args[1] == 100);
142 break;
143 case 5:
144 passed &= !rc;
145 passed &= (args.args_count == 0);
146 break;
147 case 6:
148 passed &= !rc;
149 passed &= (args.args_count == 1);
150 passed &= (args.args[0] == (i + 1));
151 break;
152 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000153 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700154 break;
155 default:
156 passed = false;
157 }
158
Grant Likelycabb7d52013-02-12 21:19:37 +0000159 selftest(passed, "index %i - data error on node %s rc=%i\n",
160 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700161 }
162
163 /* Check for missing list property */
164 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
165 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000166 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000167 rc = of_count_phandle_with_args(np, "phandle-list-missing",
168 "#phandle-cells");
169 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700170
171 /* Check for missing cells property */
172 rc = of_parse_phandle_with_args(np, "phandle-list",
173 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000174 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000175 rc = of_count_phandle_with_args(np, "phandle-list",
176 "#phandle-cells-missing");
177 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700178
179 /* Check for bad phandle in list */
180 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
181 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000182 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000183 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
184 "#phandle-cells");
185 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700186
187 /* Check for incorrectly formed argument list */
188 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
189 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000190 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000191 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
192 "#phandle-cells");
193 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700194}
195
Grant Likely7aff0fe2011-12-12 09:25:58 -0700196static void __init of_selftest_property_match_string(void)
197{
198 struct device_node *np;
199 int rc;
200
Grant Likely7aff0fe2011-12-12 09:25:58 -0700201 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
202 if (!np) {
203 pr_err("No testcase data in device tree\n");
204 return;
205 }
206
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);
221}
222
Grant Likelya9f10ca2013-10-11 22:04:23 +0100223static void __init of_selftest_parse_interrupts(void)
224{
225 struct device_node *np;
226 struct of_phandle_args args;
227 int i, rc;
228
229 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
230 if (!np) {
231 pr_err("missing testcase data\n");
232 return;
233 }
234
235 for (i = 0; i < 4; i++) {
236 bool passed = true;
237 args.args_count = 0;
238 rc = of_irq_parse_one(np, i, &args);
239
240 passed &= !rc;
241 passed &= (args.args_count == 1);
242 passed &= (args.args[0] == (i + 1));
243
244 selftest(passed, "index %i - data error on node %s rc=%i\n",
245 i, args.np->full_name, rc);
246 }
247 of_node_put(np);
248
249 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
250 if (!np) {
251 pr_err("missing testcase data\n");
252 return;
253 }
254
255 for (i = 0; i < 4; i++) {
256 bool passed = true;
257 args.args_count = 0;
258 rc = of_irq_parse_one(np, i, &args);
259
260 /* Test the values from tests-phandle.dtsi */
261 switch (i) {
262 case 0:
263 passed &= !rc;
264 passed &= (args.args_count == 1);
265 passed &= (args.args[0] == 9);
266 break;
267 case 1:
268 passed &= !rc;
269 passed &= (args.args_count == 3);
270 passed &= (args.args[0] == 10);
271 passed &= (args.args[1] == 11);
272 passed &= (args.args[2] == 12);
273 break;
274 case 2:
275 passed &= !rc;
276 passed &= (args.args_count == 2);
277 passed &= (args.args[0] == 13);
278 passed &= (args.args[1] == 14);
279 break;
280 case 3:
281 passed &= !rc;
282 passed &= (args.args_count == 2);
283 passed &= (args.args[0] == 15);
284 passed &= (args.args[1] == 16);
285 break;
286 default:
287 passed = false;
288 }
289 selftest(passed, "index %i - data error on node %s rc=%i\n",
290 i, args.np->full_name, rc);
291 }
292 of_node_put(np);
293}
294
Grant Likely79d97012013-09-19 16:47:37 -0500295static void __init of_selftest_parse_interrupts_extended(void)
296{
297 struct device_node *np;
298 struct of_phandle_args args;
299 int i, rc;
300
301 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
302 if (!np) {
303 pr_err("missing testcase data\n");
304 return;
305 }
306
307 for (i = 0; i < 7; i++) {
308 bool passed = true;
309 rc = of_irq_parse_one(np, i, &args);
310
311 /* Test the values from tests-phandle.dtsi */
312 switch (i) {
313 case 0:
314 passed &= !rc;
315 passed &= (args.args_count == 1);
316 passed &= (args.args[0] == 1);
317 break;
318 case 1:
319 passed &= !rc;
320 passed &= (args.args_count == 3);
321 passed &= (args.args[0] == 2);
322 passed &= (args.args[1] == 3);
323 passed &= (args.args[2] == 4);
324 break;
325 case 2:
326 passed &= !rc;
327 passed &= (args.args_count == 2);
328 passed &= (args.args[0] == 5);
329 passed &= (args.args[1] == 6);
330 break;
331 case 3:
332 passed &= !rc;
333 passed &= (args.args_count == 1);
334 passed &= (args.args[0] == 9);
335 break;
336 case 4:
337 passed &= !rc;
338 passed &= (args.args_count == 3);
339 passed &= (args.args[0] == 10);
340 passed &= (args.args[1] == 11);
341 passed &= (args.args[2] == 12);
342 break;
343 case 5:
344 passed &= !rc;
345 passed &= (args.args_count == 2);
346 passed &= (args.args[0] == 13);
347 passed &= (args.args[1] == 14);
348 break;
349 case 6:
350 passed &= !rc;
351 passed &= (args.args_count == 1);
352 passed &= (args.args[0] == 15);
353 break;
354 default:
355 passed = false;
356 }
357
358 selftest(passed, "index %i - data error on node %s rc=%i\n",
359 i, args.np->full_name, rc);
360 }
361 of_node_put(np);
362}
363
Grant Likely1f42e5d2014-02-18 21:38:55 +0000364static 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 */
367
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", },
371
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", },
378 {}
379};
380
381static struct {
382 const char *path;
383 const char *data;
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", },
397};
398
399static void __init of_selftest_match_node(void)
400{
401 struct device_node *np;
402 const struct of_device_id *match;
403 int i;
404
405 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
406 np = of_find_node_by_path(match_node_tests[i].path);
407 if (!np) {
408 selftest(0, "missing testcase node %s\n",
409 match_node_tests[i].path);
410 continue;
411 }
412
413 match = of_match_node(match_node_table, np);
414 if (!match) {
415 selftest(0, "%s didn't match anything\n",
416 match_node_tests[i].path);
417 continue;
418 }
419
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);
424 continue;
425 }
426 selftest(1, "passed");
427 }
428}
429
Grant Likely53a42092011-12-12 09:25:57 -0700430static int __init of_selftest(void)
431{
432 struct device_node *np;
433
434 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
435 if (!np) {
436 pr_info("No testcase data in device tree; not running tests\n");
437 return 0;
438 }
439 of_node_put(np);
440
441 pr_info("start of selftest - you will see error messages\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000442 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -0700443 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700444 of_selftest_property_match_string();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100445 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -0500446 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +0000447 of_selftest_match_node();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100448 pr_info("end of selftest - %i passed, %i failed\n",
449 selftest_results.passed, selftest_results.failed);
Grant Likely53a42092011-12-12 09:25:57 -0700450 return 0;
451}
452late_initcall(of_selftest);