of/unittest: Rename selftest.c to unittest.c
[deliverable/linux.git] / drivers / of / unittest.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/hashtable.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20
21 #include "of_private.h"
22
23 static struct selftest_results {
24 int passed;
25 int failed;
26 } selftest_results;
27
28 #define NO_OF_NODES 3
29 static struct device_node *nodes[NO_OF_NODES];
30 static int last_node_index;
31 static bool selftest_live_tree;
32
33 #define selftest(result, fmt, ...) ({ \
34 bool failed = !(result); \
35 if (failed) { \
36 selftest_results.failed++; \
37 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
38 } else { \
39 selftest_results.passed++; \
40 pr_debug("pass %s():%i\n", __func__, __LINE__); \
41 } \
42 failed; \
43 })
44
45 static void __init of_selftest_find_node_by_name(void)
46 {
47 struct device_node *np;
48
49 np = of_find_node_by_path("/testcase-data");
50 selftest(np && !strcmp("/testcase-data", np->full_name),
51 "find /testcase-data failed\n");
52 of_node_put(np);
53
54 /* Test if trailing '/' works */
55 np = of_find_node_by_path("/testcase-data/");
56 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
57
58 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
59 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
60 "find /testcase-data/phandle-tests/consumer-a failed\n");
61 of_node_put(np);
62
63 np = of_find_node_by_path("testcase-alias");
64 selftest(np && !strcmp("/testcase-data", np->full_name),
65 "find testcase-alias failed\n");
66 of_node_put(np);
67
68 /* Test if trailing '/' works on aliases */
69 np = of_find_node_by_path("testcase-alias/");
70 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
71
72 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
73 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
74 "find testcase-alias/phandle-tests/consumer-a failed\n");
75 of_node_put(np);
76
77 np = of_find_node_by_path("/testcase-data/missing-path");
78 selftest(!np, "non-existent path returned node %s\n", np->full_name);
79 of_node_put(np);
80
81 np = of_find_node_by_path("missing-alias");
82 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
83 of_node_put(np);
84
85 np = of_find_node_by_path("testcase-alias/missing-path");
86 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
87 of_node_put(np);
88 }
89
90 static void __init of_selftest_dynamic(void)
91 {
92 struct device_node *np;
93 struct property *prop;
94
95 np = of_find_node_by_path("/testcase-data");
96 if (!np) {
97 pr_err("missing testcase data\n");
98 return;
99 }
100
101 /* Array of 4 properties for the purpose of testing */
102 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
103 if (!prop) {
104 selftest(0, "kzalloc() failed\n");
105 return;
106 }
107
108 /* Add a new property - should pass*/
109 prop->name = "new-property";
110 prop->value = "new-property-data";
111 prop->length = strlen(prop->value);
112 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
113
114 /* Try to add an existing property - should fail */
115 prop++;
116 prop->name = "new-property";
117 prop->value = "new-property-data-should-fail";
118 prop->length = strlen(prop->value);
119 selftest(of_add_property(np, prop) != 0,
120 "Adding an existing property should have failed\n");
121
122 /* Try to modify an existing property - should pass */
123 prop->value = "modify-property-data-should-pass";
124 prop->length = strlen(prop->value);
125 selftest(of_update_property(np, prop) == 0,
126 "Updating an existing property should have passed\n");
127
128 /* Try to modify non-existent property - should pass*/
129 prop++;
130 prop->name = "modify-property";
131 prop->value = "modify-missing-property-data-should-pass";
132 prop->length = strlen(prop->value);
133 selftest(of_update_property(np, prop) == 0,
134 "Updating a missing property should have passed\n");
135
136 /* Remove property - should pass */
137 selftest(of_remove_property(np, prop) == 0,
138 "Removing a property should have passed\n");
139
140 /* Adding very large property - should pass */
141 prop++;
142 prop->name = "large-property-PAGE_SIZEx8";
143 prop->length = PAGE_SIZE * 8;
144 prop->value = kzalloc(prop->length, GFP_KERNEL);
145 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
146 if (prop->value)
147 selftest(of_add_property(np, prop) == 0,
148 "Adding a large property should have passed\n");
149 }
150
151 static int __init of_selftest_check_node_linkage(struct device_node *np)
152 {
153 struct device_node *child;
154 int count = 0, rc;
155
156 for_each_child_of_node(np, child) {
157 if (child->parent != np) {
158 pr_err("Child node %s links to wrong parent %s\n",
159 child->name, np->name);
160 return -EINVAL;
161 }
162
163 rc = of_selftest_check_node_linkage(child);
164 if (rc < 0)
165 return rc;
166 count += rc;
167 }
168
169 return count + 1;
170 }
171
172 static void __init of_selftest_check_tree_linkage(void)
173 {
174 struct device_node *np;
175 int allnode_count = 0, child_count;
176
177 if (!of_root)
178 return;
179
180 for_each_of_allnodes(np)
181 allnode_count++;
182 child_count = of_selftest_check_node_linkage(of_root);
183
184 selftest(child_count > 0, "Device node data structure is corrupted\n");
185 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
186 "sibling lists size (%i)\n", allnode_count, child_count);
187 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
188 }
189
190 struct node_hash {
191 struct hlist_node node;
192 struct device_node *np;
193 };
194
195 static DEFINE_HASHTABLE(phandle_ht, 8);
196 static void __init of_selftest_check_phandles(void)
197 {
198 struct device_node *np;
199 struct node_hash *nh;
200 struct hlist_node *tmp;
201 int i, dup_count = 0, phandle_count = 0;
202
203 for_each_of_allnodes(np) {
204 if (!np->phandle)
205 continue;
206
207 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
208 if (nh->np->phandle == np->phandle) {
209 pr_info("Duplicate phandle! %i used by %s and %s\n",
210 np->phandle, nh->np->full_name, np->full_name);
211 dup_count++;
212 break;
213 }
214 }
215
216 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
217 if (WARN_ON(!nh))
218 return;
219
220 nh->np = np;
221 hash_add(phandle_ht, &nh->node, np->phandle);
222 phandle_count++;
223 }
224 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
225 dup_count, phandle_count);
226
227 /* Clean up */
228 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
229 hash_del(&nh->node);
230 kfree(nh);
231 }
232 }
233
234 static void __init of_selftest_parse_phandle_with_args(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/phandle-tests/consumer-a");
241 if (!np) {
242 pr_err("missing testcase data\n");
243 return;
244 }
245
246 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
247 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
248
249 for (i = 0; i < 8; i++) {
250 bool passed = true;
251 rc = of_parse_phandle_with_args(np, "phandle-list",
252 "#phandle-cells", i, &args);
253
254 /* Test the values from tests-phandle.dtsi */
255 switch (i) {
256 case 0:
257 passed &= !rc;
258 passed &= (args.args_count == 1);
259 passed &= (args.args[0] == (i + 1));
260 break;
261 case 1:
262 passed &= !rc;
263 passed &= (args.args_count == 2);
264 passed &= (args.args[0] == (i + 1));
265 passed &= (args.args[1] == 0);
266 break;
267 case 2:
268 passed &= (rc == -ENOENT);
269 break;
270 case 3:
271 passed &= !rc;
272 passed &= (args.args_count == 3);
273 passed &= (args.args[0] == (i + 1));
274 passed &= (args.args[1] == 4);
275 passed &= (args.args[2] == 3);
276 break;
277 case 4:
278 passed &= !rc;
279 passed &= (args.args_count == 2);
280 passed &= (args.args[0] == (i + 1));
281 passed &= (args.args[1] == 100);
282 break;
283 case 5:
284 passed &= !rc;
285 passed &= (args.args_count == 0);
286 break;
287 case 6:
288 passed &= !rc;
289 passed &= (args.args_count == 1);
290 passed &= (args.args[0] == (i + 1));
291 break;
292 case 7:
293 passed &= (rc == -ENOENT);
294 break;
295 default:
296 passed = false;
297 }
298
299 selftest(passed, "index %i - data error on node %s rc=%i\n",
300 i, args.np->full_name, rc);
301 }
302
303 /* Check for missing list property */
304 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
305 "#phandle-cells", 0, &args);
306 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
307 rc = of_count_phandle_with_args(np, "phandle-list-missing",
308 "#phandle-cells");
309 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
310
311 /* Check for missing cells property */
312 rc = of_parse_phandle_with_args(np, "phandle-list",
313 "#phandle-cells-missing", 0, &args);
314 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
315 rc = of_count_phandle_with_args(np, "phandle-list",
316 "#phandle-cells-missing");
317 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
318
319 /* Check for bad phandle in list */
320 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
321 "#phandle-cells", 0, &args);
322 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
323 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
324 "#phandle-cells");
325 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
326
327 /* Check for incorrectly formed argument list */
328 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
329 "#phandle-cells", 1, &args);
330 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
331 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
332 "#phandle-cells");
333 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
334 }
335
336 static void __init of_selftest_property_string(void)
337 {
338 const char *strings[4];
339 struct device_node *np;
340 int rc;
341
342 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
343 if (!np) {
344 pr_err("No testcase data in device tree\n");
345 return;
346 }
347
348 rc = of_property_match_string(np, "phandle-list-names", "first");
349 selftest(rc == 0, "first expected:0 got:%i\n", rc);
350 rc = of_property_match_string(np, "phandle-list-names", "second");
351 selftest(rc == 1, "second expected:0 got:%i\n", rc);
352 rc = of_property_match_string(np, "phandle-list-names", "third");
353 selftest(rc == 2, "third expected:0 got:%i\n", rc);
354 rc = of_property_match_string(np, "phandle-list-names", "fourth");
355 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
356 rc = of_property_match_string(np, "missing-property", "blah");
357 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
358 rc = of_property_match_string(np, "empty-property", "blah");
359 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
360 rc = of_property_match_string(np, "unterminated-string", "blah");
361 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
362
363 /* of_property_count_strings() tests */
364 rc = of_property_count_strings(np, "string-property");
365 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
366 rc = of_property_count_strings(np, "phandle-list-names");
367 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
368 rc = of_property_count_strings(np, "unterminated-string");
369 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
370 rc = of_property_count_strings(np, "unterminated-string-list");
371 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
372
373 /* of_property_read_string_index() tests */
374 rc = of_property_read_string_index(np, "string-property", 0, strings);
375 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
376 strings[0] = NULL;
377 rc = of_property_read_string_index(np, "string-property", 1, strings);
378 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
379 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
380 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
381 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
382 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
383 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
384 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
385 strings[0] = NULL;
386 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
387 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
388 strings[0] = NULL;
389 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
390 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
391 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
392 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
393 strings[0] = NULL;
394 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
395 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
396 strings[1] = NULL;
397
398 /* of_property_read_string_array() tests */
399 rc = of_property_read_string_array(np, "string-property", strings, 4);
400 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
401 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
402 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
403 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
404 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
405 /* -- An incorrectly formed string should cause a failure */
406 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
407 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
408 /* -- parsing the correctly formed strings should still work: */
409 strings[2] = NULL;
410 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
411 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
412 strings[1] = NULL;
413 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
414 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
415 }
416
417 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
418 (p1)->value && (p2)->value && \
419 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
420 !strcmp((p1)->name, (p2)->name))
421 static void __init of_selftest_property_copy(void)
422 {
423 #ifdef CONFIG_OF_DYNAMIC
424 struct property p1 = { .name = "p1", .length = 0, .value = "" };
425 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
426 struct property *new;
427
428 new = __of_prop_dup(&p1, GFP_KERNEL);
429 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
430 kfree(new->value);
431 kfree(new->name);
432 kfree(new);
433
434 new = __of_prop_dup(&p2, GFP_KERNEL);
435 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
436 kfree(new->value);
437 kfree(new->name);
438 kfree(new);
439 #endif
440 }
441
442 static void __init of_selftest_changeset(void)
443 {
444 #ifdef CONFIG_OF_DYNAMIC
445 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
446 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
447 struct property *ppremove;
448 struct device_node *n1, *n2, *n21, *nremove, *parent;
449 struct of_changeset chgset;
450
451 of_changeset_init(&chgset);
452 n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
453 selftest(n1, "testcase setup failure\n");
454 n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
455 selftest(n2, "testcase setup failure\n");
456 n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
457 selftest(n21, "testcase setup failure %p\n", n21);
458 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
459 selftest(nremove, "testcase setup failure\n");
460 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
461 selftest(ppadd, "testcase setup failure\n");
462 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
463 selftest(ppupdate, "testcase setup failure\n");
464 parent = nremove->parent;
465 n1->parent = parent;
466 n2->parent = parent;
467 n21->parent = n2;
468 n2->child = n21;
469 ppremove = of_find_property(parent, "prop-remove", NULL);
470 selftest(ppremove, "failed to find removal prop");
471
472 of_changeset_init(&chgset);
473 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
474 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
475 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
476 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
477 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
478 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
479 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
480 mutex_lock(&of_mutex);
481 selftest(!of_changeset_apply(&chgset), "apply failed\n");
482 mutex_unlock(&of_mutex);
483
484 mutex_lock(&of_mutex);
485 selftest(!of_changeset_revert(&chgset), "revert failed\n");
486 mutex_unlock(&of_mutex);
487
488 of_changeset_destroy(&chgset);
489 #endif
490 }
491
492 static void __init of_selftest_parse_interrupts(void)
493 {
494 struct device_node *np;
495 struct of_phandle_args args;
496 int i, rc;
497
498 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
499 if (!np) {
500 pr_err("missing testcase data\n");
501 return;
502 }
503
504 for (i = 0; i < 4; i++) {
505 bool passed = true;
506 args.args_count = 0;
507 rc = of_irq_parse_one(np, i, &args);
508
509 passed &= !rc;
510 passed &= (args.args_count == 1);
511 passed &= (args.args[0] == (i + 1));
512
513 selftest(passed, "index %i - data error on node %s rc=%i\n",
514 i, args.np->full_name, rc);
515 }
516 of_node_put(np);
517
518 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
519 if (!np) {
520 pr_err("missing testcase data\n");
521 return;
522 }
523
524 for (i = 0; i < 4; i++) {
525 bool passed = true;
526 args.args_count = 0;
527 rc = of_irq_parse_one(np, i, &args);
528
529 /* Test the values from tests-phandle.dtsi */
530 switch (i) {
531 case 0:
532 passed &= !rc;
533 passed &= (args.args_count == 1);
534 passed &= (args.args[0] == 9);
535 break;
536 case 1:
537 passed &= !rc;
538 passed &= (args.args_count == 3);
539 passed &= (args.args[0] == 10);
540 passed &= (args.args[1] == 11);
541 passed &= (args.args[2] == 12);
542 break;
543 case 2:
544 passed &= !rc;
545 passed &= (args.args_count == 2);
546 passed &= (args.args[0] == 13);
547 passed &= (args.args[1] == 14);
548 break;
549 case 3:
550 passed &= !rc;
551 passed &= (args.args_count == 2);
552 passed &= (args.args[0] == 15);
553 passed &= (args.args[1] == 16);
554 break;
555 default:
556 passed = false;
557 }
558 selftest(passed, "index %i - data error on node %s rc=%i\n",
559 i, args.np->full_name, rc);
560 }
561 of_node_put(np);
562 }
563
564 static void __init of_selftest_parse_interrupts_extended(void)
565 {
566 struct device_node *np;
567 struct of_phandle_args args;
568 int i, rc;
569
570 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
571 if (!np) {
572 pr_err("missing testcase data\n");
573 return;
574 }
575
576 for (i = 0; i < 7; i++) {
577 bool passed = true;
578 rc = of_irq_parse_one(np, i, &args);
579
580 /* Test the values from tests-phandle.dtsi */
581 switch (i) {
582 case 0:
583 passed &= !rc;
584 passed &= (args.args_count == 1);
585 passed &= (args.args[0] == 1);
586 break;
587 case 1:
588 passed &= !rc;
589 passed &= (args.args_count == 3);
590 passed &= (args.args[0] == 2);
591 passed &= (args.args[1] == 3);
592 passed &= (args.args[2] == 4);
593 break;
594 case 2:
595 passed &= !rc;
596 passed &= (args.args_count == 2);
597 passed &= (args.args[0] == 5);
598 passed &= (args.args[1] == 6);
599 break;
600 case 3:
601 passed &= !rc;
602 passed &= (args.args_count == 1);
603 passed &= (args.args[0] == 9);
604 break;
605 case 4:
606 passed &= !rc;
607 passed &= (args.args_count == 3);
608 passed &= (args.args[0] == 10);
609 passed &= (args.args[1] == 11);
610 passed &= (args.args[2] == 12);
611 break;
612 case 5:
613 passed &= !rc;
614 passed &= (args.args_count == 2);
615 passed &= (args.args[0] == 13);
616 passed &= (args.args[1] == 14);
617 break;
618 case 6:
619 passed &= !rc;
620 passed &= (args.args_count == 1);
621 passed &= (args.args[0] == 15);
622 break;
623 default:
624 passed = false;
625 }
626
627 selftest(passed, "index %i - data error on node %s rc=%i\n",
628 i, args.np->full_name, rc);
629 }
630 of_node_put(np);
631 }
632
633 static struct of_device_id match_node_table[] = {
634 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
635 { .data = "B", .type = "type1", }, /* followed by type alone */
636
637 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
638 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
639 { .data = "Cc", .name = "name2", .type = "type2", },
640
641 { .data = "E", .compatible = "compat3" },
642 { .data = "G", .compatible = "compat2", },
643 { .data = "H", .compatible = "compat2", .name = "name5", },
644 { .data = "I", .compatible = "compat2", .type = "type1", },
645 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
646 { .data = "K", .compatible = "compat2", .name = "name9", },
647 {}
648 };
649
650 static struct {
651 const char *path;
652 const char *data;
653 } match_node_tests[] = {
654 { .path = "/testcase-data/match-node/name0", .data = "A", },
655 { .path = "/testcase-data/match-node/name1", .data = "B", },
656 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
657 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
658 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
659 { .path = "/testcase-data/match-node/name3", .data = "E", },
660 { .path = "/testcase-data/match-node/name4", .data = "G", },
661 { .path = "/testcase-data/match-node/name5", .data = "H", },
662 { .path = "/testcase-data/match-node/name6", .data = "G", },
663 { .path = "/testcase-data/match-node/name7", .data = "I", },
664 { .path = "/testcase-data/match-node/name8", .data = "J", },
665 { .path = "/testcase-data/match-node/name9", .data = "K", },
666 };
667
668 static void __init of_selftest_match_node(void)
669 {
670 struct device_node *np;
671 const struct of_device_id *match;
672 int i;
673
674 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
675 np = of_find_node_by_path(match_node_tests[i].path);
676 if (!np) {
677 selftest(0, "missing testcase node %s\n",
678 match_node_tests[i].path);
679 continue;
680 }
681
682 match = of_match_node(match_node_table, np);
683 if (!match) {
684 selftest(0, "%s didn't match anything\n",
685 match_node_tests[i].path);
686 continue;
687 }
688
689 if (strcmp(match->data, match_node_tests[i].data) != 0) {
690 selftest(0, "%s got wrong match. expected %s, got %s\n",
691 match_node_tests[i].path, match_node_tests[i].data,
692 (const char *)match->data);
693 continue;
694 }
695 selftest(1, "passed");
696 }
697 }
698
699 struct device test_bus = {
700 .init_name = "unittest-bus",
701 };
702 static void __init of_selftest_platform_populate(void)
703 {
704 int irq, rc;
705 struct device_node *np, *child, *grandchild;
706 struct platform_device *pdev;
707 struct of_device_id match[] = {
708 { .compatible = "test-device", },
709 {}
710 };
711
712 np = of_find_node_by_path("/testcase-data");
713 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
714
715 /* Test that a missing irq domain returns -EPROBE_DEFER */
716 np = of_find_node_by_path("/testcase-data/testcase-device1");
717 pdev = of_find_device_by_node(np);
718 selftest(pdev, "device 1 creation failed\n");
719
720 irq = platform_get_irq(pdev, 0);
721 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
722
723 /* Test that a parsing failure does not return -EPROBE_DEFER */
724 np = of_find_node_by_path("/testcase-data/testcase-device2");
725 pdev = of_find_device_by_node(np);
726 selftest(pdev, "device 2 creation failed\n");
727 irq = platform_get_irq(pdev, 0);
728 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
729
730 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
731 "No testcase data in device tree\n"));
732 return;
733
734 if (selftest(!(rc = device_register(&test_bus)),
735 "testbus registration failed; rc=%i\n", rc));
736 return;
737
738 for_each_child_of_node(np, child) {
739 of_platform_populate(child, match, NULL, &test_bus);
740 for_each_child_of_node(child, grandchild)
741 selftest(of_find_device_by_node(grandchild),
742 "Could not create device for node '%s'\n",
743 grandchild->name);
744 }
745
746 of_platform_depopulate(&test_bus);
747 for_each_child_of_node(np, child) {
748 for_each_child_of_node(child, grandchild)
749 selftest(!of_find_device_by_node(grandchild),
750 "device didn't get destroyed '%s'\n",
751 grandchild->name);
752 }
753
754 device_unregister(&test_bus);
755 of_node_put(np);
756 }
757
758 /**
759 * update_node_properties - adds the properties
760 * of np into dup node (present in live tree) and
761 * updates parent of children of np to dup.
762 *
763 * @np: node already present in live tree
764 * @dup: node present in live tree to be updated
765 */
766 static void update_node_properties(struct device_node *np,
767 struct device_node *dup)
768 {
769 struct property *prop;
770 struct device_node *child;
771
772 for_each_property_of_node(np, prop)
773 of_add_property(dup, prop);
774
775 for_each_child_of_node(np, child)
776 child->parent = dup;
777 }
778
779 /**
780 * attach_node_and_children - attaches nodes
781 * and its children to live tree
782 *
783 * @np: Node to attach to live tree
784 */
785 static int attach_node_and_children(struct device_node *np)
786 {
787 struct device_node *next, *dup, *child;
788
789 dup = of_find_node_by_path(np->full_name);
790 if (dup) {
791 update_node_properties(np, dup);
792 return 0;
793 }
794
795 /* Children of the root need to be remembered for removal */
796 if (np->parent == of_root) {
797 if (WARN_ON(last_node_index >= NO_OF_NODES))
798 return -EINVAL;
799 nodes[last_node_index++] = np;
800 }
801
802 child = np->child;
803 np->child = NULL;
804 np->sibling = NULL;
805 of_attach_node(np);
806 while (child) {
807 next = child->sibling;
808 attach_node_and_children(child);
809 child = next;
810 }
811
812 return 0;
813 }
814
815 /**
816 * selftest_data_add - Reads, copies data from
817 * linked tree and attaches it to the live tree
818 */
819 static int __init selftest_data_add(void)
820 {
821 void *selftest_data;
822 struct device_node *selftest_data_node, *np;
823 extern uint8_t __dtb_testcases_begin[];
824 extern uint8_t __dtb_testcases_end[];
825 const int size = __dtb_testcases_end - __dtb_testcases_begin;
826 int rc;
827
828 if (!size) {
829 pr_warn("%s: No testcase data to attach; not running tests\n",
830 __func__);
831 return -ENODATA;
832 }
833
834 /* creating copy */
835 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
836
837 if (!selftest_data) {
838 pr_warn("%s: Failed to allocate memory for selftest_data; "
839 "not running tests\n", __func__);
840 return -ENOMEM;
841 }
842 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
843 if (!selftest_data_node) {
844 pr_warn("%s: No tree to attach; not running tests\n", __func__);
845 return -ENODATA;
846 }
847 of_node_set_flag(selftest_data_node, OF_DETACHED);
848 rc = of_resolve_phandles(selftest_data_node);
849 if (rc) {
850 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
851 return -EINVAL;
852 }
853
854 if (!of_root) {
855 /* enabling flag for removing nodes */
856 selftest_live_tree = true;
857 of_root = selftest_data_node;
858
859 for_each_of_allnodes(np)
860 __of_attach_node_sysfs(np);
861 of_aliases = of_find_node_by_path("/aliases");
862 of_chosen = of_find_node_by_path("/chosen");
863 return 0;
864 }
865
866 /* attach the sub-tree to live tree */
867 np = selftest_data_node->child;
868 while (np) {
869 struct device_node *next = np->sibling;
870 np->parent = of_root;
871 attach_node_and_children(np);
872 np = next;
873 }
874 return 0;
875 }
876
877 /**
878 * detach_node_and_children - detaches node
879 * and its children from live tree
880 *
881 * @np: Node to detach from live tree
882 */
883 static void detach_node_and_children(struct device_node *np)
884 {
885 while (np->child)
886 detach_node_and_children(np->child);
887 of_detach_node(np);
888 }
889
890 /**
891 * selftest_data_remove - removes the selftest data
892 * nodes from the live tree
893 */
894 static void selftest_data_remove(void)
895 {
896 struct device_node *np;
897 struct property *prop;
898
899 if (selftest_live_tree) {
900 of_node_put(of_aliases);
901 of_node_put(of_chosen);
902 of_aliases = NULL;
903 of_chosen = NULL;
904 for_each_child_of_node(of_root, np)
905 detach_node_and_children(np);
906 __of_detach_node_sysfs(of_root);
907 of_root = NULL;
908 return;
909 }
910
911 while (last_node_index >= 0) {
912 if (nodes[last_node_index]) {
913 np = of_find_node_by_path(nodes[last_node_index]->full_name);
914 if (strcmp(np->full_name, "/aliases") != 0) {
915 detach_node_and_children(np);
916 } else {
917 for_each_property_of_node(np, prop) {
918 if (strcmp(prop->name, "testcase-alias") == 0)
919 of_remove_property(np, prop);
920 }
921 }
922 }
923 last_node_index--;
924 }
925 }
926
927 static int __init of_selftest(void)
928 {
929 struct device_node *np;
930 int res;
931
932 /* adding data for selftest */
933 res = selftest_data_add();
934 if (res)
935 return res;
936
937 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
938 if (!np) {
939 pr_info("No testcase data in device tree; not running tests\n");
940 return 0;
941 }
942 of_node_put(np);
943
944 pr_info("start of selftest - you will see error messages\n");
945 of_selftest_check_tree_linkage();
946 of_selftest_check_phandles();
947 of_selftest_find_node_by_name();
948 of_selftest_dynamic();
949 of_selftest_parse_phandle_with_args();
950 of_selftest_property_string();
951 of_selftest_property_copy();
952 of_selftest_changeset();
953 of_selftest_parse_interrupts();
954 of_selftest_parse_interrupts_extended();
955 of_selftest_match_node();
956 of_selftest_platform_populate();
957
958 /* removing selftest data from live tree */
959 selftest_data_remove();
960
961 /* Double check linkage after removing testcase data */
962 of_selftest_check_tree_linkage();
963
964 pr_info("end of selftest - %i passed, %i failed\n",
965 selftest_results.passed, selftest_results.failed);
966
967 return 0;
968 }
969 late_initcall(of_selftest);
This page took 0.070052 seconds and 6 git commands to generate.