blob: 4fed34bff5cf736cdf2dd6d998222dee1b1b52d2 [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>
Grant Likely841ec212014-10-02 13:09:15 +010010#include <linux/hashtable.h>
Grant Likely53a42092011-12-12 09:25:57 -070011#include <linux/module.h>
12#include <linux/of.h>
Gaurav Minochaae9304c2014-07-16 23:09:39 -070013#include <linux/of_fdt.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010014#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050015#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070016#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/device.h>
20
Pantelis Antoniou69843392014-07-04 19:58:47 +030021#include "of_private.h"
22
Grant Likelya9f10ca2013-10-11 22:04:23 +010023static struct selftest_results {
24 int passed;
25 int failed;
26} selftest_results;
27
Grant Likely2eb46da2014-10-02 14:36:46 +010028#define NO_OF_NODES 3
Gaurav Minochaae9304c2014-07-16 23:09:39 -070029static struct device_node *nodes[NO_OF_NODES];
30static int last_node_index;
Gaurav Minochab951f9d2014-07-26 12:48:50 -070031static bool selftest_live_tree;
Gaurav Minochaae9304c2014-07-16 23:09:39 -070032
Grant Likely53a42092011-12-12 09:25:57 -070033#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000034 if (!(result)) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010035 selftest_results.failed++; \
36 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000037 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010038 selftest_results.passed++; \
39 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000040 } \
Grant Likely53a42092011-12-12 09:25:57 -070041}
42
Grant Likelyae91ff72014-03-14 13:53:10 +000043static void __init of_selftest_find_node_by_name(void)
44{
45 struct device_node *np;
46
47 np = of_find_node_by_path("/testcase-data");
48 selftest(np && !strcmp("/testcase-data", np->full_name),
49 "find /testcase-data failed\n");
50 of_node_put(np);
51
52 /* Test if trailing '/' works */
53 np = of_find_node_by_path("/testcase-data/");
54 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
55
56 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
57 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
58 "find /testcase-data/phandle-tests/consumer-a failed\n");
59 of_node_put(np);
60
61 np = of_find_node_by_path("testcase-alias");
62 selftest(np && !strcmp("/testcase-data", np->full_name),
63 "find testcase-alias failed\n");
64 of_node_put(np);
65
66 /* Test if trailing '/' works on aliases */
67 np = of_find_node_by_path("testcase-alias/");
68 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
69
70 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
71 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
72 "find testcase-alias/phandle-tests/consumer-a failed\n");
73 of_node_put(np);
74
75 np = of_find_node_by_path("/testcase-data/missing-path");
76 selftest(!np, "non-existent path returned node %s\n", np->full_name);
77 of_node_put(np);
78
79 np = of_find_node_by_path("missing-alias");
80 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
81 of_node_put(np);
82
83 np = of_find_node_by_path("testcase-alias/missing-path");
84 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
85 of_node_put(np);
86}
87
Grant Likely7e66c5c2013-11-15 17:19:09 +000088static void __init of_selftest_dynamic(void)
89{
90 struct device_node *np;
91 struct property *prop;
92
93 np = of_find_node_by_path("/testcase-data");
94 if (!np) {
95 pr_err("missing testcase data\n");
96 return;
97 }
98
99 /* Array of 4 properties for the purpose of testing */
100 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
101 if (!prop) {
102 selftest(0, "kzalloc() failed\n");
103 return;
104 }
105
106 /* Add a new property - should pass*/
107 prop->name = "new-property";
108 prop->value = "new-property-data";
109 prop->length = strlen(prop->value);
110 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
111
112 /* Try to add an existing property - should fail */
113 prop++;
114 prop->name = "new-property";
115 prop->value = "new-property-data-should-fail";
116 prop->length = strlen(prop->value);
117 selftest(of_add_property(np, prop) != 0,
118 "Adding an existing property should have failed\n");
119
120 /* Try to modify an existing property - should pass */
121 prop->value = "modify-property-data-should-pass";
122 prop->length = strlen(prop->value);
123 selftest(of_update_property(np, prop) == 0,
124 "Updating an existing property should have passed\n");
125
126 /* Try to modify non-existent property - should pass*/
127 prop++;
128 prop->name = "modify-property";
129 prop->value = "modify-missing-property-data-should-pass";
130 prop->length = strlen(prop->value);
131 selftest(of_update_property(np, prop) == 0,
132 "Updating a missing property should have passed\n");
133
134 /* Remove property - should pass */
135 selftest(of_remove_property(np, prop) == 0,
136 "Removing a property should have passed\n");
137
138 /* Adding very large property - should pass */
139 prop++;
140 prop->name = "large-property-PAGE_SIZEx8";
141 prop->length = PAGE_SIZE * 8;
142 prop->value = kzalloc(prop->length, GFP_KERNEL);
143 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
144 if (prop->value)
145 selftest(of_add_property(np, prop) == 0,
146 "Adding a large property should have passed\n");
147}
148
Grant Likelyf2051d62014-10-01 17:40:22 +0100149static int __init of_selftest_check_node_linkage(struct device_node *np)
150{
151 struct device_node *child, *allnext_index = np;
152 int count = 0, rc;
153
154 for_each_child_of_node(np, child) {
155 if (child->parent != np) {
156 pr_err("Child node %s links to wrong parent %s\n",
157 child->name, np->name);
158 return -EINVAL;
159 }
160
161 while (allnext_index && allnext_index != child)
162 allnext_index = allnext_index->allnext;
163 if (allnext_index != child) {
164 pr_err("Node %s is ordered differently in sibling and allnode lists\n",
165 child->name);
166 return -EINVAL;
167 }
168
169 rc = of_selftest_check_node_linkage(child);
170 if (rc < 0)
171 return rc;
172 count += rc;
173 }
174
175 return count + 1;
176}
177
178static void __init of_selftest_check_tree_linkage(void)
179{
180 struct device_node *np;
181 int allnode_count = 0, child_count;
182
183 if (!of_allnodes)
184 return;
185
186 for_each_of_allnodes(np)
187 allnode_count++;
188 child_count = of_selftest_check_node_linkage(of_allnodes);
189
190 selftest(child_count > 0, "Device node data structure is corrupted\n");
191 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
192 "sibling lists size (%i)\n", allnode_count, child_count);
193 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
194}
195
Grant Likely841ec212014-10-02 13:09:15 +0100196struct node_hash {
197 struct hlist_node node;
198 struct device_node *np;
199};
200
201static void __init of_selftest_check_phandles(void)
202{
203 struct device_node *np;
204 struct node_hash *nh;
205 struct hlist_node *tmp;
206 int i, dup_count = 0, phandle_count = 0;
207 DECLARE_HASHTABLE(ht, 8);
208
209 hash_init(ht);
210 for_each_of_allnodes(np) {
211 if (!np->phandle)
212 continue;
213
214 hash_for_each_possible(ht, nh, node, np->phandle) {
215 if (nh->np->phandle == np->phandle) {
216 pr_info("Duplicate phandle! %i used by %s and %s\n",
217 np->phandle, nh->np->full_name, np->full_name);
218 dup_count++;
219 break;
220 }
221 }
222
223 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
224 if (WARN_ON(!nh))
225 return;
226
227 nh->np = np;
228 hash_add(ht, &nh->node, np->phandle);
229 phandle_count++;
230 }
231 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
232 dup_count, phandle_count);
233
234 /* Clean up */
235 hash_for_each_safe(ht, i, tmp, nh, node) {
236 hash_del(&nh->node);
237 kfree(nh);
238 }
239}
240
Grant Likely53a42092011-12-12 09:25:57 -0700241static void __init of_selftest_parse_phandle_with_args(void)
242{
243 struct device_node *np;
244 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000245 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700246
Grant Likely53a42092011-12-12 09:25:57 -0700247 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
248 if (!np) {
249 pr_err("missing testcase data\n");
250 return;
251 }
252
Grant Likelybd69f732013-02-10 22:57:21 +0000253 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
254 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
255
Grant Likelyf7f951c2013-02-12 17:41:22 +0000256 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700257 bool passed = true;
258 rc = of_parse_phandle_with_args(np, "phandle-list",
259 "#phandle-cells", i, &args);
260
261 /* Test the values from tests-phandle.dtsi */
262 switch (i) {
263 case 0:
264 passed &= !rc;
265 passed &= (args.args_count == 1);
266 passed &= (args.args[0] == (i + 1));
267 break;
268 case 1:
269 passed &= !rc;
270 passed &= (args.args_count == 2);
271 passed &= (args.args[0] == (i + 1));
272 passed &= (args.args[1] == 0);
273 break;
274 case 2:
275 passed &= (rc == -ENOENT);
276 break;
277 case 3:
278 passed &= !rc;
279 passed &= (args.args_count == 3);
280 passed &= (args.args[0] == (i + 1));
281 passed &= (args.args[1] == 4);
282 passed &= (args.args[2] == 3);
283 break;
284 case 4:
285 passed &= !rc;
286 passed &= (args.args_count == 2);
287 passed &= (args.args[0] == (i + 1));
288 passed &= (args.args[1] == 100);
289 break;
290 case 5:
291 passed &= !rc;
292 passed &= (args.args_count == 0);
293 break;
294 case 6:
295 passed &= !rc;
296 passed &= (args.args_count == 1);
297 passed &= (args.args[0] == (i + 1));
298 break;
299 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000300 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700301 break;
302 default:
303 passed = false;
304 }
305
Grant Likelycabb7d52013-02-12 21:19:37 +0000306 selftest(passed, "index %i - data error on node %s rc=%i\n",
307 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700308 }
309
310 /* Check for missing list property */
311 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
312 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000313 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000314 rc = of_count_phandle_with_args(np, "phandle-list-missing",
315 "#phandle-cells");
316 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700317
318 /* Check for missing cells property */
319 rc = of_parse_phandle_with_args(np, "phandle-list",
320 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000321 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000322 rc = of_count_phandle_with_args(np, "phandle-list",
323 "#phandle-cells-missing");
324 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700325
326 /* Check for bad phandle in list */
327 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
328 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000329 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000330 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
331 "#phandle-cells");
332 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700333
334 /* Check for incorrectly formed argument list */
335 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
336 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000337 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000338 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
339 "#phandle-cells");
340 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700341}
342
Grant Likely7aff0fe2011-12-12 09:25:58 -0700343static void __init of_selftest_property_match_string(void)
344{
345 struct device_node *np;
346 int rc;
347
Grant Likely7aff0fe2011-12-12 09:25:58 -0700348 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
349 if (!np) {
350 pr_err("No testcase data in device tree\n");
351 return;
352 }
353
354 rc = of_property_match_string(np, "phandle-list-names", "first");
355 selftest(rc == 0, "first expected:0 got:%i\n", rc);
356 rc = of_property_match_string(np, "phandle-list-names", "second");
357 selftest(rc == 1, "second expected:0 got:%i\n", rc);
358 rc = of_property_match_string(np, "phandle-list-names", "third");
359 selftest(rc == 2, "third expected:0 got:%i\n", rc);
360 rc = of_property_match_string(np, "phandle-list-names", "fourth");
361 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
362 rc = of_property_match_string(np, "missing-property", "blah");
363 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
364 rc = of_property_match_string(np, "empty-property", "blah");
365 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
366 rc = of_property_match_string(np, "unterminated-string", "blah");
367 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
368}
369
Pantelis Antoniou69843392014-07-04 19:58:47 +0300370#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
371 (p1)->value && (p2)->value && \
372 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
373 !strcmp((p1)->name, (p2)->name))
374static void __init of_selftest_property_copy(void)
375{
376#ifdef CONFIG_OF_DYNAMIC
377 struct property p1 = { .name = "p1", .length = 0, .value = "" };
378 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
379 struct property *new;
380
381 new = __of_prop_dup(&p1, GFP_KERNEL);
382 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
383 kfree(new->value);
384 kfree(new->name);
385 kfree(new);
386
387 new = __of_prop_dup(&p2, GFP_KERNEL);
388 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
389 kfree(new->value);
390 kfree(new->name);
391 kfree(new);
392#endif
393}
394
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300395static void __init of_selftest_changeset(void)
396{
397#ifdef CONFIG_OF_DYNAMIC
398 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
399 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
400 struct property *ppremove;
401 struct device_node *n1, *n2, *n21, *nremove, *parent;
402 struct of_changeset chgset;
403
404 of_changeset_init(&chgset);
405 n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
406 selftest(n1, "testcase setup failure\n");
407 n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
408 selftest(n2, "testcase setup failure\n");
409 n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
410 selftest(n21, "testcase setup failure %p\n", n21);
411 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
412 selftest(nremove, "testcase setup failure\n");
413 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
414 selftest(ppadd, "testcase setup failure\n");
415 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
416 selftest(ppupdate, "testcase setup failure\n");
417 parent = nremove->parent;
418 n1->parent = parent;
419 n2->parent = parent;
420 n21->parent = n2;
421 n2->child = n21;
422 ppremove = of_find_property(parent, "prop-remove", NULL);
423 selftest(ppremove, "failed to find removal prop");
424
425 of_changeset_init(&chgset);
426 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
427 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
428 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
429 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
430 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
431 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
432 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
433 mutex_lock(&of_mutex);
434 selftest(!of_changeset_apply(&chgset), "apply failed\n");
435 mutex_unlock(&of_mutex);
436
437 mutex_lock(&of_mutex);
438 selftest(!of_changeset_revert(&chgset), "revert failed\n");
439 mutex_unlock(&of_mutex);
440
441 of_changeset_destroy(&chgset);
442#endif
443}
444
Grant Likelya9f10ca2013-10-11 22:04:23 +0100445static void __init of_selftest_parse_interrupts(void)
446{
447 struct device_node *np;
448 struct of_phandle_args args;
449 int i, rc;
450
451 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
452 if (!np) {
453 pr_err("missing testcase data\n");
454 return;
455 }
456
457 for (i = 0; i < 4; i++) {
458 bool passed = true;
459 args.args_count = 0;
460 rc = of_irq_parse_one(np, i, &args);
461
462 passed &= !rc;
463 passed &= (args.args_count == 1);
464 passed &= (args.args[0] == (i + 1));
465
466 selftest(passed, "index %i - data error on node %s rc=%i\n",
467 i, args.np->full_name, rc);
468 }
469 of_node_put(np);
470
471 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
472 if (!np) {
473 pr_err("missing testcase data\n");
474 return;
475 }
476
477 for (i = 0; i < 4; i++) {
478 bool passed = true;
479 args.args_count = 0;
480 rc = of_irq_parse_one(np, i, &args);
481
482 /* Test the values from tests-phandle.dtsi */
483 switch (i) {
484 case 0:
485 passed &= !rc;
486 passed &= (args.args_count == 1);
487 passed &= (args.args[0] == 9);
488 break;
489 case 1:
490 passed &= !rc;
491 passed &= (args.args_count == 3);
492 passed &= (args.args[0] == 10);
493 passed &= (args.args[1] == 11);
494 passed &= (args.args[2] == 12);
495 break;
496 case 2:
497 passed &= !rc;
498 passed &= (args.args_count == 2);
499 passed &= (args.args[0] == 13);
500 passed &= (args.args[1] == 14);
501 break;
502 case 3:
503 passed &= !rc;
504 passed &= (args.args_count == 2);
505 passed &= (args.args[0] == 15);
506 passed &= (args.args[1] == 16);
507 break;
508 default:
509 passed = false;
510 }
511 selftest(passed, "index %i - data error on node %s rc=%i\n",
512 i, args.np->full_name, rc);
513 }
514 of_node_put(np);
515}
516
Grant Likely79d97012013-09-19 16:47:37 -0500517static void __init of_selftest_parse_interrupts_extended(void)
518{
519 struct device_node *np;
520 struct of_phandle_args args;
521 int i, rc;
522
523 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
524 if (!np) {
525 pr_err("missing testcase data\n");
526 return;
527 }
528
529 for (i = 0; i < 7; i++) {
530 bool passed = true;
531 rc = of_irq_parse_one(np, i, &args);
532
533 /* Test the values from tests-phandle.dtsi */
534 switch (i) {
535 case 0:
536 passed &= !rc;
537 passed &= (args.args_count == 1);
538 passed &= (args.args[0] == 1);
539 break;
540 case 1:
541 passed &= !rc;
542 passed &= (args.args_count == 3);
543 passed &= (args.args[0] == 2);
544 passed &= (args.args[1] == 3);
545 passed &= (args.args[2] == 4);
546 break;
547 case 2:
548 passed &= !rc;
549 passed &= (args.args_count == 2);
550 passed &= (args.args[0] == 5);
551 passed &= (args.args[1] == 6);
552 break;
553 case 3:
554 passed &= !rc;
555 passed &= (args.args_count == 1);
556 passed &= (args.args[0] == 9);
557 break;
558 case 4:
559 passed &= !rc;
560 passed &= (args.args_count == 3);
561 passed &= (args.args[0] == 10);
562 passed &= (args.args[1] == 11);
563 passed &= (args.args[2] == 12);
564 break;
565 case 5:
566 passed &= !rc;
567 passed &= (args.args_count == 2);
568 passed &= (args.args[0] == 13);
569 passed &= (args.args[1] == 14);
570 break;
571 case 6:
572 passed &= !rc;
573 passed &= (args.args_count == 1);
574 passed &= (args.args[0] == 15);
575 break;
576 default:
577 passed = false;
578 }
579
580 selftest(passed, "index %i - data error on node %s rc=%i\n",
581 i, args.np->full_name, rc);
582 }
583 of_node_put(np);
584}
585
Grant Likely1f42e5d2014-02-18 21:38:55 +0000586static struct of_device_id match_node_table[] = {
587 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
588 { .data = "B", .type = "type1", }, /* followed by type alone */
589
590 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
591 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
592 { .data = "Cc", .name = "name2", .type = "type2", },
593
594 { .data = "E", .compatible = "compat3" },
595 { .data = "G", .compatible = "compat2", },
596 { .data = "H", .compatible = "compat2", .name = "name5", },
597 { .data = "I", .compatible = "compat2", .type = "type1", },
598 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
599 { .data = "K", .compatible = "compat2", .name = "name9", },
600 {}
601};
602
603static struct {
604 const char *path;
605 const char *data;
606} match_node_tests[] = {
607 { .path = "/testcase-data/match-node/name0", .data = "A", },
608 { .path = "/testcase-data/match-node/name1", .data = "B", },
609 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
610 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
611 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
612 { .path = "/testcase-data/match-node/name3", .data = "E", },
613 { .path = "/testcase-data/match-node/name4", .data = "G", },
614 { .path = "/testcase-data/match-node/name5", .data = "H", },
615 { .path = "/testcase-data/match-node/name6", .data = "G", },
616 { .path = "/testcase-data/match-node/name7", .data = "I", },
617 { .path = "/testcase-data/match-node/name8", .data = "J", },
618 { .path = "/testcase-data/match-node/name9", .data = "K", },
619};
620
621static void __init of_selftest_match_node(void)
622{
623 struct device_node *np;
624 const struct of_device_id *match;
625 int i;
626
627 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
628 np = of_find_node_by_path(match_node_tests[i].path);
629 if (!np) {
630 selftest(0, "missing testcase node %s\n",
631 match_node_tests[i].path);
632 continue;
633 }
634
635 match = of_match_node(match_node_table, np);
636 if (!match) {
637 selftest(0, "%s didn't match anything\n",
638 match_node_tests[i].path);
639 continue;
640 }
641
642 if (strcmp(match->data, match_node_tests[i].data) != 0) {
643 selftest(0, "%s got wrong match. expected %s, got %s\n",
644 match_node_tests[i].path, match_node_tests[i].data,
645 (const char *)match->data);
646 continue;
647 }
648 selftest(1, "passed");
649 }
650}
651
Rob Herring82c0f582014-04-23 17:57:40 -0500652static void __init of_selftest_platform_populate(void)
653{
654 int irq;
Rob Herringfb2caa52014-05-13 10:07:54 -0500655 struct device_node *np, *child;
Rob Herring82c0f582014-04-23 17:57:40 -0500656 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500657 struct of_device_id match[] = {
658 { .compatible = "test-device", },
659 {}
660 };
Rob Herring82c0f582014-04-23 17:57:40 -0500661
662 np = of_find_node_by_path("/testcase-data");
663 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
664
665 /* Test that a missing irq domain returns -EPROBE_DEFER */
666 np = of_find_node_by_path("/testcase-data/testcase-device1");
667 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500668 selftest(pdev, "device 1 creation failed\n");
669
Rob Herring82c0f582014-04-23 17:57:40 -0500670 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500671 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500672
673 /* Test that a parsing failure does not return -EPROBE_DEFER */
674 np = of_find_node_by_path("/testcase-data/testcase-device2");
675 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500676 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500677 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500678 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500679
Rob Herringfb2caa52014-05-13 10:07:54 -0500680 np = of_find_node_by_path("/testcase-data/platform-tests");
681 if (!np) {
682 pr_err("No testcase data in device tree\n");
683 return;
684 }
685
686 for_each_child_of_node(np, child) {
687 struct device_node *grandchild;
688 of_platform_populate(child, match, NULL, NULL);
689 for_each_child_of_node(child, grandchild)
690 selftest(of_find_device_by_node(grandchild),
691 "Could not create device for node '%s'\n",
692 grandchild->name);
693 }
Rob Herring82c0f582014-04-23 17:57:40 -0500694}
695
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700696/**
697 * update_node_properties - adds the properties
698 * of np into dup node (present in live tree) and
699 * updates parent of children of np to dup.
700 *
701 * @np: node already present in live tree
702 * @dup: node present in live tree to be updated
703 */
704static void update_node_properties(struct device_node *np,
705 struct device_node *dup)
706{
707 struct property *prop;
708 struct device_node *child;
709
710 for_each_property_of_node(np, prop)
711 of_add_property(dup, prop);
712
713 for_each_child_of_node(np, child)
714 child->parent = dup;
715}
716
717/**
718 * attach_node_and_children - attaches nodes
719 * and its children to live tree
720 *
721 * @np: Node to attach to live tree
722 */
723static int attach_node_and_children(struct device_node *np)
724{
725 struct device_node *next, *root = np, *dup;
726
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700727 /* skip root node */
728 np = np->child;
729 /* storing a copy in temporary node */
730 dup = np;
731
732 while (dup) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100733 if (WARN_ON(last_node_index >= NO_OF_NODES))
734 return -EINVAL;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700735 nodes[last_node_index++] = dup;
736 dup = dup->sibling;
737 }
738 dup = NULL;
739
740 while (np) {
741 next = np->allnext;
742 dup = of_find_node_by_path(np->full_name);
743 if (dup)
744 update_node_properties(np, dup);
745 else {
746 np->child = NULL;
747 if (np->parent == root)
748 np->parent = of_allnodes;
749 of_attach_node(np);
750 }
751 np = next;
752 }
753
754 return 0;
755}
756
757/**
758 * selftest_data_add - Reads, copies data from
759 * linked tree and attaches it to the live tree
760 */
761static int __init selftest_data_add(void)
762{
763 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700764 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700765 extern uint8_t __dtb_testcases_begin[];
766 extern uint8_t __dtb_testcases_end[];
767 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100768 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700769
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700770 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700771 pr_warn("%s: No testcase data to attach; not running tests\n",
772 __func__);
773 return -ENODATA;
774 }
775
776 /* creating copy */
777 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
778
779 if (!selftest_data) {
780 pr_warn("%s: Failed to allocate memory for selftest_data; "
781 "not running tests\n", __func__);
782 return -ENOMEM;
783 }
784 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700785 if (!selftest_data_node) {
786 pr_warn("%s: No tree to attach; not running tests\n", __func__);
787 return -ENODATA;
788 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100789 of_node_set_flag(selftest_data_node, OF_DETACHED);
790 rc = of_resolve_phandles(selftest_data_node);
791 if (rc) {
792 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
793 return -EINVAL;
794 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700795
796 if (!of_allnodes) {
797 /* enabling flag for removing nodes */
798 selftest_live_tree = true;
799 of_allnodes = selftest_data_node;
800
801 for_each_of_allnodes(np)
802 __of_attach_node_sysfs(np);
803 of_aliases = of_find_node_by_path("/aliases");
804 of_chosen = of_find_node_by_path("/chosen");
805 return 0;
806 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700807
808 /* attach the sub-tree to live tree */
809 return attach_node_and_children(selftest_data_node);
810}
811
812/**
813 * detach_node_and_children - detaches node
814 * and its children from live tree
815 *
816 * @np: Node to detach from live tree
817 */
818static void detach_node_and_children(struct device_node *np)
819{
820 while (np->child)
821 detach_node_and_children(np->child);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700822 of_detach_node(np);
823}
824
825/**
826 * selftest_data_remove - removes the selftest data
827 * nodes from the live tree
828 */
829static void selftest_data_remove(void)
830{
831 struct device_node *np;
832 struct property *prop;
833
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700834 if (selftest_live_tree) {
835 of_node_put(of_aliases);
836 of_node_put(of_chosen);
837 of_aliases = NULL;
838 of_chosen = NULL;
839 for_each_child_of_node(of_allnodes, np)
840 detach_node_and_children(np);
841 __of_detach_node_sysfs(of_allnodes);
842 of_allnodes = NULL;
843 return;
844 }
845
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700846 while (last_node_index >= 0) {
847 if (nodes[last_node_index]) {
848 np = of_find_node_by_path(nodes[last_node_index]->full_name);
849 if (strcmp(np->full_name, "/aliases") != 0) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100850 detach_node_and_children(np);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700851 } else {
852 for_each_property_of_node(np, prop) {
853 if (strcmp(prop->name, "testcase-alias") == 0)
854 of_remove_property(np, prop);
855 }
856 }
857 }
858 last_node_index--;
859 }
860}
861
Grant Likely53a42092011-12-12 09:25:57 -0700862static int __init of_selftest(void)
863{
864 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700865 int res;
866
867 /* adding data for selftest */
868 res = selftest_data_add();
869 if (res)
870 return res;
Grant Likely53a42092011-12-12 09:25:57 -0700871
872 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
873 if (!np) {
874 pr_info("No testcase data in device tree; not running tests\n");
875 return 0;
876 }
877 of_node_put(np);
878
879 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +0100880 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +0100881 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +0000882 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +0000883 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -0700884 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700885 of_selftest_property_match_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +0300886 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300887 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100888 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -0500889 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +0000890 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -0500891 of_selftest_platform_populate();
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700892
893 /* removing selftest data from live tree */
894 selftest_data_remove();
895
Grant Likelyf2051d62014-10-01 17:40:22 +0100896 /* Double check linkage after removing testcase data */
897 of_selftest_check_tree_linkage();
898
899 pr_info("end of selftest - %i passed, %i failed\n",
900 selftest_results.passed, selftest_results.failed);
901
Grant Likely53a42092011-12-12 09:25:57 -0700902 return 0;
903}
904late_initcall(of_selftest);