blob: 883e60b04eb5ea080e90da48ba7ade0e9f61e65d [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>
Gaurav Minochaae9304c2014-07-16 23:09:39 -070012#include <linux/of_fdt.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010013#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050014#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070015#include <linux/list.h>
16#include <linux/mutex.h>
17#include <linux/slab.h>
18#include <linux/device.h>
19
Pantelis Antoniou69843392014-07-04 19:58:47 +030020#include "of_private.h"
21
Grant Likelya9f10ca2013-10-11 22:04:23 +010022static struct selftest_results {
23 int passed;
24 int failed;
25} selftest_results;
26
Gaurav Minochaae9304c2014-07-16 23:09:39 -070027#define NO_OF_NODES 2
28static struct device_node *nodes[NO_OF_NODES];
29static int last_node_index;
Gaurav Minochab951f9d2014-07-26 12:48:50 -070030static bool selftest_live_tree;
Gaurav Minochaae9304c2014-07-16 23:09:39 -070031
Grant Likely53a42092011-12-12 09:25:57 -070032#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000033 if (!(result)) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010034 selftest_results.failed++; \
35 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000036 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010037 selftest_results.passed++; \
38 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000039 } \
Grant Likely53a42092011-12-12 09:25:57 -070040}
41
Grant Likelyae91ff72014-03-14 13:53:10 +000042static void __init of_selftest_find_node_by_name(void)
43{
44 struct device_node *np;
45
46 np = of_find_node_by_path("/testcase-data");
47 selftest(np && !strcmp("/testcase-data", np->full_name),
48 "find /testcase-data failed\n");
49 of_node_put(np);
50
51 /* Test if trailing '/' works */
52 np = of_find_node_by_path("/testcase-data/");
53 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
54
55 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
56 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
57 "find /testcase-data/phandle-tests/consumer-a failed\n");
58 of_node_put(np);
59
60 np = of_find_node_by_path("testcase-alias");
61 selftest(np && !strcmp("/testcase-data", np->full_name),
62 "find testcase-alias failed\n");
63 of_node_put(np);
64
65 /* Test if trailing '/' works on aliases */
66 np = of_find_node_by_path("testcase-alias/");
67 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
68
69 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
70 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
71 "find testcase-alias/phandle-tests/consumer-a failed\n");
72 of_node_put(np);
73
74 np = of_find_node_by_path("/testcase-data/missing-path");
75 selftest(!np, "non-existent path returned node %s\n", np->full_name);
76 of_node_put(np);
77
78 np = of_find_node_by_path("missing-alias");
79 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
80 of_node_put(np);
81
82 np = of_find_node_by_path("testcase-alias/missing-path");
83 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
84 of_node_put(np);
85}
86
Grant Likely7e66c5c2013-11-15 17:19:09 +000087static void __init of_selftest_dynamic(void)
88{
89 struct device_node *np;
90 struct property *prop;
91
92 np = of_find_node_by_path("/testcase-data");
93 if (!np) {
94 pr_err("missing testcase data\n");
95 return;
96 }
97
98 /* Array of 4 properties for the purpose of testing */
99 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
100 if (!prop) {
101 selftest(0, "kzalloc() failed\n");
102 return;
103 }
104
105 /* Add a new property - should pass*/
106 prop->name = "new-property";
107 prop->value = "new-property-data";
108 prop->length = strlen(prop->value);
109 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
110
111 /* Try to add an existing property - should fail */
112 prop++;
113 prop->name = "new-property";
114 prop->value = "new-property-data-should-fail";
115 prop->length = strlen(prop->value);
116 selftest(of_add_property(np, prop) != 0,
117 "Adding an existing property should have failed\n");
118
119 /* Try to modify an existing property - should pass */
120 prop->value = "modify-property-data-should-pass";
121 prop->length = strlen(prop->value);
122 selftest(of_update_property(np, prop) == 0,
123 "Updating an existing property should have passed\n");
124
125 /* Try to modify non-existent property - should pass*/
126 prop++;
127 prop->name = "modify-property";
128 prop->value = "modify-missing-property-data-should-pass";
129 prop->length = strlen(prop->value);
130 selftest(of_update_property(np, prop) == 0,
131 "Updating a missing property should have passed\n");
132
133 /* Remove property - should pass */
134 selftest(of_remove_property(np, prop) == 0,
135 "Removing a property should have passed\n");
136
137 /* Adding very large property - should pass */
138 prop++;
139 prop->name = "large-property-PAGE_SIZEx8";
140 prop->length = PAGE_SIZE * 8;
141 prop->value = kzalloc(prop->length, GFP_KERNEL);
142 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
143 if (prop->value)
144 selftest(of_add_property(np, prop) == 0,
145 "Adding a large property should have passed\n");
146}
147
Grant Likely53a42092011-12-12 09:25:57 -0700148static void __init of_selftest_parse_phandle_with_args(void)
149{
150 struct device_node *np;
151 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000152 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700153
Grant Likely53a42092011-12-12 09:25:57 -0700154 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
155 if (!np) {
156 pr_err("missing testcase data\n");
157 return;
158 }
159
Grant Likelybd69f732013-02-10 22:57:21 +0000160 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
161 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
162
Grant Likelyf7f951c2013-02-12 17:41:22 +0000163 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700164 bool passed = true;
165 rc = of_parse_phandle_with_args(np, "phandle-list",
166 "#phandle-cells", i, &args);
167
168 /* Test the values from tests-phandle.dtsi */
169 switch (i) {
170 case 0:
171 passed &= !rc;
172 passed &= (args.args_count == 1);
173 passed &= (args.args[0] == (i + 1));
174 break;
175 case 1:
176 passed &= !rc;
177 passed &= (args.args_count == 2);
178 passed &= (args.args[0] == (i + 1));
179 passed &= (args.args[1] == 0);
180 break;
181 case 2:
182 passed &= (rc == -ENOENT);
183 break;
184 case 3:
185 passed &= !rc;
186 passed &= (args.args_count == 3);
187 passed &= (args.args[0] == (i + 1));
188 passed &= (args.args[1] == 4);
189 passed &= (args.args[2] == 3);
190 break;
191 case 4:
192 passed &= !rc;
193 passed &= (args.args_count == 2);
194 passed &= (args.args[0] == (i + 1));
195 passed &= (args.args[1] == 100);
196 break;
197 case 5:
198 passed &= !rc;
199 passed &= (args.args_count == 0);
200 break;
201 case 6:
202 passed &= !rc;
203 passed &= (args.args_count == 1);
204 passed &= (args.args[0] == (i + 1));
205 break;
206 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000207 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700208 break;
209 default:
210 passed = false;
211 }
212
Grant Likelycabb7d52013-02-12 21:19:37 +0000213 selftest(passed, "index %i - data error on node %s rc=%i\n",
214 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700215 }
216
217 /* Check for missing list property */
218 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
219 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000220 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000221 rc = of_count_phandle_with_args(np, "phandle-list-missing",
222 "#phandle-cells");
223 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700224
225 /* Check for missing cells property */
226 rc = of_parse_phandle_with_args(np, "phandle-list",
227 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000228 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000229 rc = of_count_phandle_with_args(np, "phandle-list",
230 "#phandle-cells-missing");
231 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700232
233 /* Check for bad phandle in list */
234 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
235 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000236 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000237 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
238 "#phandle-cells");
239 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700240
241 /* Check for incorrectly formed argument list */
242 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
243 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000244 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000245 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
246 "#phandle-cells");
247 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700248}
249
Grant Likely7aff0fe2011-12-12 09:25:58 -0700250static void __init of_selftest_property_match_string(void)
251{
252 struct device_node *np;
253 int rc;
254
Grant Likely7aff0fe2011-12-12 09:25:58 -0700255 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
256 if (!np) {
257 pr_err("No testcase data in device tree\n");
258 return;
259 }
260
261 rc = of_property_match_string(np, "phandle-list-names", "first");
262 selftest(rc == 0, "first expected:0 got:%i\n", rc);
263 rc = of_property_match_string(np, "phandle-list-names", "second");
264 selftest(rc == 1, "second expected:0 got:%i\n", rc);
265 rc = of_property_match_string(np, "phandle-list-names", "third");
266 selftest(rc == 2, "third expected:0 got:%i\n", rc);
267 rc = of_property_match_string(np, "phandle-list-names", "fourth");
268 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
269 rc = of_property_match_string(np, "missing-property", "blah");
270 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
271 rc = of_property_match_string(np, "empty-property", "blah");
272 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
273 rc = of_property_match_string(np, "unterminated-string", "blah");
274 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
275}
276
Pantelis Antoniou69843392014-07-04 19:58:47 +0300277#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
278 (p1)->value && (p2)->value && \
279 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
280 !strcmp((p1)->name, (p2)->name))
281static void __init of_selftest_property_copy(void)
282{
283#ifdef CONFIG_OF_DYNAMIC
284 struct property p1 = { .name = "p1", .length = 0, .value = "" };
285 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
286 struct property *new;
287
288 new = __of_prop_dup(&p1, GFP_KERNEL);
289 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
290 kfree(new->value);
291 kfree(new->name);
292 kfree(new);
293
294 new = __of_prop_dup(&p2, GFP_KERNEL);
295 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
296 kfree(new->value);
297 kfree(new->name);
298 kfree(new);
299#endif
300}
301
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300302static void __init of_selftest_changeset(void)
303{
304#ifdef CONFIG_OF_DYNAMIC
305 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
306 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
307 struct property *ppremove;
308 struct device_node *n1, *n2, *n21, *nremove, *parent;
309 struct of_changeset chgset;
310
311 of_changeset_init(&chgset);
312 n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
313 selftest(n1, "testcase setup failure\n");
314 n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
315 selftest(n2, "testcase setup failure\n");
316 n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
317 selftest(n21, "testcase setup failure %p\n", n21);
318 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
319 selftest(nremove, "testcase setup failure\n");
320 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
321 selftest(ppadd, "testcase setup failure\n");
322 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
323 selftest(ppupdate, "testcase setup failure\n");
324 parent = nremove->parent;
325 n1->parent = parent;
326 n2->parent = parent;
327 n21->parent = n2;
328 n2->child = n21;
329 ppremove = of_find_property(parent, "prop-remove", NULL);
330 selftest(ppremove, "failed to find removal prop");
331
332 of_changeset_init(&chgset);
333 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
334 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
335 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
336 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
337 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
338 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
339 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
340 mutex_lock(&of_mutex);
341 selftest(!of_changeset_apply(&chgset), "apply failed\n");
342 mutex_unlock(&of_mutex);
343
344 mutex_lock(&of_mutex);
345 selftest(!of_changeset_revert(&chgset), "revert failed\n");
346 mutex_unlock(&of_mutex);
347
348 of_changeset_destroy(&chgset);
349#endif
350}
351
Grant Likelya9f10ca2013-10-11 22:04:23 +0100352static void __init of_selftest_parse_interrupts(void)
353{
354 struct device_node *np;
355 struct of_phandle_args args;
356 int i, rc;
357
358 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
359 if (!np) {
360 pr_err("missing testcase data\n");
361 return;
362 }
363
364 for (i = 0; i < 4; i++) {
365 bool passed = true;
366 args.args_count = 0;
367 rc = of_irq_parse_one(np, i, &args);
368
369 passed &= !rc;
370 passed &= (args.args_count == 1);
371 passed &= (args.args[0] == (i + 1));
372
373 selftest(passed, "index %i - data error on node %s rc=%i\n",
374 i, args.np->full_name, rc);
375 }
376 of_node_put(np);
377
378 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
379 if (!np) {
380 pr_err("missing testcase data\n");
381 return;
382 }
383
384 for (i = 0; i < 4; i++) {
385 bool passed = true;
386 args.args_count = 0;
387 rc = of_irq_parse_one(np, i, &args);
388
389 /* Test the values from tests-phandle.dtsi */
390 switch (i) {
391 case 0:
392 passed &= !rc;
393 passed &= (args.args_count == 1);
394 passed &= (args.args[0] == 9);
395 break;
396 case 1:
397 passed &= !rc;
398 passed &= (args.args_count == 3);
399 passed &= (args.args[0] == 10);
400 passed &= (args.args[1] == 11);
401 passed &= (args.args[2] == 12);
402 break;
403 case 2:
404 passed &= !rc;
405 passed &= (args.args_count == 2);
406 passed &= (args.args[0] == 13);
407 passed &= (args.args[1] == 14);
408 break;
409 case 3:
410 passed &= !rc;
411 passed &= (args.args_count == 2);
412 passed &= (args.args[0] == 15);
413 passed &= (args.args[1] == 16);
414 break;
415 default:
416 passed = false;
417 }
418 selftest(passed, "index %i - data error on node %s rc=%i\n",
419 i, args.np->full_name, rc);
420 }
421 of_node_put(np);
422}
423
Grant Likely79d97012013-09-19 16:47:37 -0500424static void __init of_selftest_parse_interrupts_extended(void)
425{
426 struct device_node *np;
427 struct of_phandle_args args;
428 int i, rc;
429
430 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
431 if (!np) {
432 pr_err("missing testcase data\n");
433 return;
434 }
435
436 for (i = 0; i < 7; i++) {
437 bool passed = true;
438 rc = of_irq_parse_one(np, i, &args);
439
440 /* Test the values from tests-phandle.dtsi */
441 switch (i) {
442 case 0:
443 passed &= !rc;
444 passed &= (args.args_count == 1);
445 passed &= (args.args[0] == 1);
446 break;
447 case 1:
448 passed &= !rc;
449 passed &= (args.args_count == 3);
450 passed &= (args.args[0] == 2);
451 passed &= (args.args[1] == 3);
452 passed &= (args.args[2] == 4);
453 break;
454 case 2:
455 passed &= !rc;
456 passed &= (args.args_count == 2);
457 passed &= (args.args[0] == 5);
458 passed &= (args.args[1] == 6);
459 break;
460 case 3:
461 passed &= !rc;
462 passed &= (args.args_count == 1);
463 passed &= (args.args[0] == 9);
464 break;
465 case 4:
466 passed &= !rc;
467 passed &= (args.args_count == 3);
468 passed &= (args.args[0] == 10);
469 passed &= (args.args[1] == 11);
470 passed &= (args.args[2] == 12);
471 break;
472 case 5:
473 passed &= !rc;
474 passed &= (args.args_count == 2);
475 passed &= (args.args[0] == 13);
476 passed &= (args.args[1] == 14);
477 break;
478 case 6:
479 passed &= !rc;
480 passed &= (args.args_count == 1);
481 passed &= (args.args[0] == 15);
482 break;
483 default:
484 passed = false;
485 }
486
487 selftest(passed, "index %i - data error on node %s rc=%i\n",
488 i, args.np->full_name, rc);
489 }
490 of_node_put(np);
491}
492
Grant Likely1f42e5d2014-02-18 21:38:55 +0000493static struct of_device_id match_node_table[] = {
494 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
495 { .data = "B", .type = "type1", }, /* followed by type alone */
496
497 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
498 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
499 { .data = "Cc", .name = "name2", .type = "type2", },
500
501 { .data = "E", .compatible = "compat3" },
502 { .data = "G", .compatible = "compat2", },
503 { .data = "H", .compatible = "compat2", .name = "name5", },
504 { .data = "I", .compatible = "compat2", .type = "type1", },
505 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
506 { .data = "K", .compatible = "compat2", .name = "name9", },
507 {}
508};
509
510static struct {
511 const char *path;
512 const char *data;
513} match_node_tests[] = {
514 { .path = "/testcase-data/match-node/name0", .data = "A", },
515 { .path = "/testcase-data/match-node/name1", .data = "B", },
516 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
517 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
518 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
519 { .path = "/testcase-data/match-node/name3", .data = "E", },
520 { .path = "/testcase-data/match-node/name4", .data = "G", },
521 { .path = "/testcase-data/match-node/name5", .data = "H", },
522 { .path = "/testcase-data/match-node/name6", .data = "G", },
523 { .path = "/testcase-data/match-node/name7", .data = "I", },
524 { .path = "/testcase-data/match-node/name8", .data = "J", },
525 { .path = "/testcase-data/match-node/name9", .data = "K", },
526};
527
528static void __init of_selftest_match_node(void)
529{
530 struct device_node *np;
531 const struct of_device_id *match;
532 int i;
533
534 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
535 np = of_find_node_by_path(match_node_tests[i].path);
536 if (!np) {
537 selftest(0, "missing testcase node %s\n",
538 match_node_tests[i].path);
539 continue;
540 }
541
542 match = of_match_node(match_node_table, np);
543 if (!match) {
544 selftest(0, "%s didn't match anything\n",
545 match_node_tests[i].path);
546 continue;
547 }
548
549 if (strcmp(match->data, match_node_tests[i].data) != 0) {
550 selftest(0, "%s got wrong match. expected %s, got %s\n",
551 match_node_tests[i].path, match_node_tests[i].data,
552 (const char *)match->data);
553 continue;
554 }
555 selftest(1, "passed");
556 }
557}
558
Rob Herring82c0f582014-04-23 17:57:40 -0500559static void __init of_selftest_platform_populate(void)
560{
561 int irq;
Rob Herringfb2caa52014-05-13 10:07:54 -0500562 struct device_node *np, *child;
Rob Herring82c0f582014-04-23 17:57:40 -0500563 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500564 struct of_device_id match[] = {
565 { .compatible = "test-device", },
566 {}
567 };
Rob Herring82c0f582014-04-23 17:57:40 -0500568
569 np = of_find_node_by_path("/testcase-data");
570 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
571
572 /* Test that a missing irq domain returns -EPROBE_DEFER */
573 np = of_find_node_by_path("/testcase-data/testcase-device1");
574 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500575 selftest(pdev, "device 1 creation failed\n");
576
Rob Herring82c0f582014-04-23 17:57:40 -0500577 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500578 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500579
580 /* Test that a parsing failure does not return -EPROBE_DEFER */
581 np = of_find_node_by_path("/testcase-data/testcase-device2");
582 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500583 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500584 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500585 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500586
Rob Herringfb2caa52014-05-13 10:07:54 -0500587 np = of_find_node_by_path("/testcase-data/platform-tests");
588 if (!np) {
589 pr_err("No testcase data in device tree\n");
590 return;
591 }
592
593 for_each_child_of_node(np, child) {
594 struct device_node *grandchild;
595 of_platform_populate(child, match, NULL, NULL);
596 for_each_child_of_node(child, grandchild)
597 selftest(of_find_device_by_node(grandchild),
598 "Could not create device for node '%s'\n",
599 grandchild->name);
600 }
Rob Herring82c0f582014-04-23 17:57:40 -0500601}
602
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700603/**
604 * update_node_properties - adds the properties
605 * of np into dup node (present in live tree) and
606 * updates parent of children of np to dup.
607 *
608 * @np: node already present in live tree
609 * @dup: node present in live tree to be updated
610 */
611static void update_node_properties(struct device_node *np,
612 struct device_node *dup)
613{
614 struct property *prop;
615 struct device_node *child;
616
617 for_each_property_of_node(np, prop)
618 of_add_property(dup, prop);
619
620 for_each_child_of_node(np, child)
621 child->parent = dup;
622}
623
624/**
625 * attach_node_and_children - attaches nodes
626 * and its children to live tree
627 *
628 * @np: Node to attach to live tree
629 */
630static int attach_node_and_children(struct device_node *np)
631{
632 struct device_node *next, *root = np, *dup;
633
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700634 /* skip root node */
635 np = np->child;
636 /* storing a copy in temporary node */
637 dup = np;
638
639 while (dup) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100640 if (WARN_ON(last_node_index >= NO_OF_NODES))
641 return -EINVAL;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700642 nodes[last_node_index++] = dup;
643 dup = dup->sibling;
644 }
645 dup = NULL;
646
647 while (np) {
648 next = np->allnext;
649 dup = of_find_node_by_path(np->full_name);
650 if (dup)
651 update_node_properties(np, dup);
652 else {
653 np->child = NULL;
654 if (np->parent == root)
655 np->parent = of_allnodes;
656 of_attach_node(np);
657 }
658 np = next;
659 }
660
661 return 0;
662}
663
664/**
665 * selftest_data_add - Reads, copies data from
666 * linked tree and attaches it to the live tree
667 */
668static int __init selftest_data_add(void)
669{
670 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700671 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700672 extern uint8_t __dtb_testcases_begin[];
673 extern uint8_t __dtb_testcases_end[];
674 const int size = __dtb_testcases_end - __dtb_testcases_begin;
675
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700676 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700677 pr_warn("%s: No testcase data to attach; not running tests\n",
678 __func__);
679 return -ENODATA;
680 }
681
682 /* creating copy */
683 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
684
685 if (!selftest_data) {
686 pr_warn("%s: Failed to allocate memory for selftest_data; "
687 "not running tests\n", __func__);
688 return -ENOMEM;
689 }
690 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700691 if (!selftest_data_node) {
692 pr_warn("%s: No tree to attach; not running tests\n", __func__);
693 return -ENODATA;
694 }
695
696 if (!of_allnodes) {
697 /* enabling flag for removing nodes */
698 selftest_live_tree = true;
699 of_allnodes = selftest_data_node;
700
701 for_each_of_allnodes(np)
702 __of_attach_node_sysfs(np);
703 of_aliases = of_find_node_by_path("/aliases");
704 of_chosen = of_find_node_by_path("/chosen");
705 return 0;
706 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700707
708 /* attach the sub-tree to live tree */
709 return attach_node_and_children(selftest_data_node);
710}
711
712/**
713 * detach_node_and_children - detaches node
714 * and its children from live tree
715 *
716 * @np: Node to detach from live tree
717 */
718static void detach_node_and_children(struct device_node *np)
719{
720 while (np->child)
721 detach_node_and_children(np->child);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700722 of_detach_node(np);
723}
724
725/**
726 * selftest_data_remove - removes the selftest data
727 * nodes from the live tree
728 */
729static void selftest_data_remove(void)
730{
731 struct device_node *np;
732 struct property *prop;
733
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700734 if (selftest_live_tree) {
735 of_node_put(of_aliases);
736 of_node_put(of_chosen);
737 of_aliases = NULL;
738 of_chosen = NULL;
739 for_each_child_of_node(of_allnodes, np)
740 detach_node_and_children(np);
741 __of_detach_node_sysfs(of_allnodes);
742 of_allnodes = NULL;
743 return;
744 }
745
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700746 while (last_node_index >= 0) {
747 if (nodes[last_node_index]) {
748 np = of_find_node_by_path(nodes[last_node_index]->full_name);
749 if (strcmp(np->full_name, "/aliases") != 0) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100750 detach_node_and_children(np);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700751 } else {
752 for_each_property_of_node(np, prop) {
753 if (strcmp(prop->name, "testcase-alias") == 0)
754 of_remove_property(np, prop);
755 }
756 }
757 }
758 last_node_index--;
759 }
760}
761
Grant Likely53a42092011-12-12 09:25:57 -0700762static int __init of_selftest(void)
763{
764 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700765 int res;
766
767 /* adding data for selftest */
768 res = selftest_data_add();
769 if (res)
770 return res;
Grant Likely53a42092011-12-12 09:25:57 -0700771
772 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
773 if (!np) {
774 pr_info("No testcase data in device tree; not running tests\n");
775 return 0;
776 }
777 of_node_put(np);
778
779 pr_info("start of selftest - you will see error messages\n");
Grant Likelyae91ff72014-03-14 13:53:10 +0000780 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +0000781 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -0700782 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700783 of_selftest_property_match_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +0300784 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300785 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100786 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -0500787 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +0000788 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -0500789 of_selftest_platform_populate();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100790 pr_info("end of selftest - %i passed, %i failed\n",
791 selftest_results.passed, selftest_results.failed);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700792
793 /* removing selftest data from live tree */
794 selftest_data_remove();
795
Grant Likely53a42092011-12-12 09:25:57 -0700796 return 0;
797}
798late_initcall(of_selftest);