blob: 78001270a5980bc7342a5241c8930297cf92f883 [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
Grant Likely2118f4b2014-10-07 11:30:31 +0100201static DEFINE_HASHTABLE(phandle_ht, 8);
Grant Likely841ec212014-10-02 13:09:15 +0100202static void __init of_selftest_check_phandles(void)
203{
204 struct device_node *np;
205 struct node_hash *nh;
206 struct hlist_node *tmp;
207 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100208
Grant Likely841ec212014-10-02 13:09:15 +0100209 for_each_of_allnodes(np) {
210 if (!np->phandle)
211 continue;
212
Grant Likely2118f4b2014-10-07 11:30:31 +0100213 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100214 if (nh->np->phandle == np->phandle) {
215 pr_info("Duplicate phandle! %i used by %s and %s\n",
216 np->phandle, nh->np->full_name, np->full_name);
217 dup_count++;
218 break;
219 }
220 }
221
222 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
223 if (WARN_ON(!nh))
224 return;
225
226 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100227 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100228 phandle_count++;
229 }
230 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
231 dup_count, phandle_count);
232
233 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100234 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100235 hash_del(&nh->node);
236 kfree(nh);
237 }
238}
239
Grant Likely53a42092011-12-12 09:25:57 -0700240static void __init of_selftest_parse_phandle_with_args(void)
241{
242 struct device_node *np;
243 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000244 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700245
Grant Likely53a42092011-12-12 09:25:57 -0700246 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
247 if (!np) {
248 pr_err("missing testcase data\n");
249 return;
250 }
251
Grant Likelybd69f732013-02-10 22:57:21 +0000252 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
253 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
254
Grant Likelyf7f951c2013-02-12 17:41:22 +0000255 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700256 bool passed = true;
257 rc = of_parse_phandle_with_args(np, "phandle-list",
258 "#phandle-cells", i, &args);
259
260 /* Test the values from tests-phandle.dtsi */
261 switch (i) {
262 case 0:
263 passed &= !rc;
264 passed &= (args.args_count == 1);
265 passed &= (args.args[0] == (i + 1));
266 break;
267 case 1:
268 passed &= !rc;
269 passed &= (args.args_count == 2);
270 passed &= (args.args[0] == (i + 1));
271 passed &= (args.args[1] == 0);
272 break;
273 case 2:
274 passed &= (rc == -ENOENT);
275 break;
276 case 3:
277 passed &= !rc;
278 passed &= (args.args_count == 3);
279 passed &= (args.args[0] == (i + 1));
280 passed &= (args.args[1] == 4);
281 passed &= (args.args[2] == 3);
282 break;
283 case 4:
284 passed &= !rc;
285 passed &= (args.args_count == 2);
286 passed &= (args.args[0] == (i + 1));
287 passed &= (args.args[1] == 100);
288 break;
289 case 5:
290 passed &= !rc;
291 passed &= (args.args_count == 0);
292 break;
293 case 6:
294 passed &= !rc;
295 passed &= (args.args_count == 1);
296 passed &= (args.args[0] == (i + 1));
297 break;
298 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000299 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700300 break;
301 default:
302 passed = false;
303 }
304
Grant Likelycabb7d52013-02-12 21:19:37 +0000305 selftest(passed, "index %i - data error on node %s rc=%i\n",
306 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700307 }
308
309 /* Check for missing list property */
310 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
311 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000312 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000313 rc = of_count_phandle_with_args(np, "phandle-list-missing",
314 "#phandle-cells");
315 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700316
317 /* Check for missing cells property */
318 rc = of_parse_phandle_with_args(np, "phandle-list",
319 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000320 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000321 rc = of_count_phandle_with_args(np, "phandle-list",
322 "#phandle-cells-missing");
323 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700324
325 /* Check for bad phandle in list */
326 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
327 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000328 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000329 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
330 "#phandle-cells");
331 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700332
333 /* Check for incorrectly formed argument list */
334 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
335 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000336 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000337 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
338 "#phandle-cells");
339 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700340}
341
Grant Likely7aff0fe2011-12-12 09:25:58 -0700342static void __init of_selftest_property_match_string(void)
343{
344 struct device_node *np;
345 int rc;
346
Grant Likely7aff0fe2011-12-12 09:25:58 -0700347 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
348 if (!np) {
349 pr_err("No testcase data in device tree\n");
350 return;
351 }
352
353 rc = of_property_match_string(np, "phandle-list-names", "first");
354 selftest(rc == 0, "first expected:0 got:%i\n", rc);
355 rc = of_property_match_string(np, "phandle-list-names", "second");
356 selftest(rc == 1, "second expected:0 got:%i\n", rc);
357 rc = of_property_match_string(np, "phandle-list-names", "third");
358 selftest(rc == 2, "third expected:0 got:%i\n", rc);
359 rc = of_property_match_string(np, "phandle-list-names", "fourth");
360 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
361 rc = of_property_match_string(np, "missing-property", "blah");
362 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
363 rc = of_property_match_string(np, "empty-property", "blah");
364 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
365 rc = of_property_match_string(np, "unterminated-string", "blah");
366 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
367}
368
Pantelis Antoniou69843392014-07-04 19:58:47 +0300369#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
370 (p1)->value && (p2)->value && \
371 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
372 !strcmp((p1)->name, (p2)->name))
373static void __init of_selftest_property_copy(void)
374{
375#ifdef CONFIG_OF_DYNAMIC
376 struct property p1 = { .name = "p1", .length = 0, .value = "" };
377 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
378 struct property *new;
379
380 new = __of_prop_dup(&p1, GFP_KERNEL);
381 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
382 kfree(new->value);
383 kfree(new->name);
384 kfree(new);
385
386 new = __of_prop_dup(&p2, GFP_KERNEL);
387 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
388 kfree(new->value);
389 kfree(new->name);
390 kfree(new);
391#endif
392}
393
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300394static void __init of_selftest_changeset(void)
395{
396#ifdef CONFIG_OF_DYNAMIC
397 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
398 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
399 struct property *ppremove;
400 struct device_node *n1, *n2, *n21, *nremove, *parent;
401 struct of_changeset chgset;
402
403 of_changeset_init(&chgset);
404 n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
405 selftest(n1, "testcase setup failure\n");
406 n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
407 selftest(n2, "testcase setup failure\n");
408 n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
409 selftest(n21, "testcase setup failure %p\n", n21);
410 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
411 selftest(nremove, "testcase setup failure\n");
412 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
413 selftest(ppadd, "testcase setup failure\n");
414 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
415 selftest(ppupdate, "testcase setup failure\n");
416 parent = nremove->parent;
417 n1->parent = parent;
418 n2->parent = parent;
419 n21->parent = n2;
420 n2->child = n21;
421 ppremove = of_find_property(parent, "prop-remove", NULL);
422 selftest(ppremove, "failed to find removal prop");
423
424 of_changeset_init(&chgset);
425 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
426 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
427 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
428 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
429 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
430 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
431 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
432 mutex_lock(&of_mutex);
433 selftest(!of_changeset_apply(&chgset), "apply failed\n");
434 mutex_unlock(&of_mutex);
435
436 mutex_lock(&of_mutex);
437 selftest(!of_changeset_revert(&chgset), "revert failed\n");
438 mutex_unlock(&of_mutex);
439
440 of_changeset_destroy(&chgset);
441#endif
442}
443
Grant Likelya9f10ca2013-10-11 22:04:23 +0100444static void __init of_selftest_parse_interrupts(void)
445{
446 struct device_node *np;
447 struct of_phandle_args args;
448 int i, rc;
449
450 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
451 if (!np) {
452 pr_err("missing testcase data\n");
453 return;
454 }
455
456 for (i = 0; i < 4; i++) {
457 bool passed = true;
458 args.args_count = 0;
459 rc = of_irq_parse_one(np, i, &args);
460
461 passed &= !rc;
462 passed &= (args.args_count == 1);
463 passed &= (args.args[0] == (i + 1));
464
465 selftest(passed, "index %i - data error on node %s rc=%i\n",
466 i, args.np->full_name, rc);
467 }
468 of_node_put(np);
469
470 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
471 if (!np) {
472 pr_err("missing testcase data\n");
473 return;
474 }
475
476 for (i = 0; i < 4; i++) {
477 bool passed = true;
478 args.args_count = 0;
479 rc = of_irq_parse_one(np, i, &args);
480
481 /* Test the values from tests-phandle.dtsi */
482 switch (i) {
483 case 0:
484 passed &= !rc;
485 passed &= (args.args_count == 1);
486 passed &= (args.args[0] == 9);
487 break;
488 case 1:
489 passed &= !rc;
490 passed &= (args.args_count == 3);
491 passed &= (args.args[0] == 10);
492 passed &= (args.args[1] == 11);
493 passed &= (args.args[2] == 12);
494 break;
495 case 2:
496 passed &= !rc;
497 passed &= (args.args_count == 2);
498 passed &= (args.args[0] == 13);
499 passed &= (args.args[1] == 14);
500 break;
501 case 3:
502 passed &= !rc;
503 passed &= (args.args_count == 2);
504 passed &= (args.args[0] == 15);
505 passed &= (args.args[1] == 16);
506 break;
507 default:
508 passed = false;
509 }
510 selftest(passed, "index %i - data error on node %s rc=%i\n",
511 i, args.np->full_name, rc);
512 }
513 of_node_put(np);
514}
515
Grant Likely79d97012013-09-19 16:47:37 -0500516static void __init of_selftest_parse_interrupts_extended(void)
517{
518 struct device_node *np;
519 struct of_phandle_args args;
520 int i, rc;
521
522 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
523 if (!np) {
524 pr_err("missing testcase data\n");
525 return;
526 }
527
528 for (i = 0; i < 7; i++) {
529 bool passed = true;
530 rc = of_irq_parse_one(np, i, &args);
531
532 /* Test the values from tests-phandle.dtsi */
533 switch (i) {
534 case 0:
535 passed &= !rc;
536 passed &= (args.args_count == 1);
537 passed &= (args.args[0] == 1);
538 break;
539 case 1:
540 passed &= !rc;
541 passed &= (args.args_count == 3);
542 passed &= (args.args[0] == 2);
543 passed &= (args.args[1] == 3);
544 passed &= (args.args[2] == 4);
545 break;
546 case 2:
547 passed &= !rc;
548 passed &= (args.args_count == 2);
549 passed &= (args.args[0] == 5);
550 passed &= (args.args[1] == 6);
551 break;
552 case 3:
553 passed &= !rc;
554 passed &= (args.args_count == 1);
555 passed &= (args.args[0] == 9);
556 break;
557 case 4:
558 passed &= !rc;
559 passed &= (args.args_count == 3);
560 passed &= (args.args[0] == 10);
561 passed &= (args.args[1] == 11);
562 passed &= (args.args[2] == 12);
563 break;
564 case 5:
565 passed &= !rc;
566 passed &= (args.args_count == 2);
567 passed &= (args.args[0] == 13);
568 passed &= (args.args[1] == 14);
569 break;
570 case 6:
571 passed &= !rc;
572 passed &= (args.args_count == 1);
573 passed &= (args.args[0] == 15);
574 break;
575 default:
576 passed = false;
577 }
578
579 selftest(passed, "index %i - data error on node %s rc=%i\n",
580 i, args.np->full_name, rc);
581 }
582 of_node_put(np);
583}
584
Grant Likely1f42e5d2014-02-18 21:38:55 +0000585static struct of_device_id match_node_table[] = {
586 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
587 { .data = "B", .type = "type1", }, /* followed by type alone */
588
589 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
590 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
591 { .data = "Cc", .name = "name2", .type = "type2", },
592
593 { .data = "E", .compatible = "compat3" },
594 { .data = "G", .compatible = "compat2", },
595 { .data = "H", .compatible = "compat2", .name = "name5", },
596 { .data = "I", .compatible = "compat2", .type = "type1", },
597 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
598 { .data = "K", .compatible = "compat2", .name = "name9", },
599 {}
600};
601
602static struct {
603 const char *path;
604 const char *data;
605} match_node_tests[] = {
606 { .path = "/testcase-data/match-node/name0", .data = "A", },
607 { .path = "/testcase-data/match-node/name1", .data = "B", },
608 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
609 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
610 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
611 { .path = "/testcase-data/match-node/name3", .data = "E", },
612 { .path = "/testcase-data/match-node/name4", .data = "G", },
613 { .path = "/testcase-data/match-node/name5", .data = "H", },
614 { .path = "/testcase-data/match-node/name6", .data = "G", },
615 { .path = "/testcase-data/match-node/name7", .data = "I", },
616 { .path = "/testcase-data/match-node/name8", .data = "J", },
617 { .path = "/testcase-data/match-node/name9", .data = "K", },
618};
619
620static void __init of_selftest_match_node(void)
621{
622 struct device_node *np;
623 const struct of_device_id *match;
624 int i;
625
626 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
627 np = of_find_node_by_path(match_node_tests[i].path);
628 if (!np) {
629 selftest(0, "missing testcase node %s\n",
630 match_node_tests[i].path);
631 continue;
632 }
633
634 match = of_match_node(match_node_table, np);
635 if (!match) {
636 selftest(0, "%s didn't match anything\n",
637 match_node_tests[i].path);
638 continue;
639 }
640
641 if (strcmp(match->data, match_node_tests[i].data) != 0) {
642 selftest(0, "%s got wrong match. expected %s, got %s\n",
643 match_node_tests[i].path, match_node_tests[i].data,
644 (const char *)match->data);
645 continue;
646 }
647 selftest(1, "passed");
648 }
649}
650
Rob Herring82c0f582014-04-23 17:57:40 -0500651static void __init of_selftest_platform_populate(void)
652{
653 int irq;
Rob Herringfb2caa52014-05-13 10:07:54 -0500654 struct device_node *np, *child;
Rob Herring82c0f582014-04-23 17:57:40 -0500655 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500656 struct of_device_id match[] = {
657 { .compatible = "test-device", },
658 {}
659 };
Rob Herring82c0f582014-04-23 17:57:40 -0500660
661 np = of_find_node_by_path("/testcase-data");
662 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
663
664 /* Test that a missing irq domain returns -EPROBE_DEFER */
665 np = of_find_node_by_path("/testcase-data/testcase-device1");
666 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500667 selftest(pdev, "device 1 creation failed\n");
668
Rob Herring82c0f582014-04-23 17:57:40 -0500669 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500670 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500671
672 /* Test that a parsing failure does not return -EPROBE_DEFER */
673 np = of_find_node_by_path("/testcase-data/testcase-device2");
674 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500675 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500676 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500677 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500678
Rob Herringfb2caa52014-05-13 10:07:54 -0500679 np = of_find_node_by_path("/testcase-data/platform-tests");
680 if (!np) {
681 pr_err("No testcase data in device tree\n");
682 return;
683 }
684
685 for_each_child_of_node(np, child) {
686 struct device_node *grandchild;
687 of_platform_populate(child, match, NULL, NULL);
688 for_each_child_of_node(child, grandchild)
689 selftest(of_find_device_by_node(grandchild),
690 "Could not create device for node '%s'\n",
691 grandchild->name);
692 }
Rob Herring82c0f582014-04-23 17:57:40 -0500693}
694
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700695/**
696 * update_node_properties - adds the properties
697 * of np into dup node (present in live tree) and
698 * updates parent of children of np to dup.
699 *
700 * @np: node already present in live tree
701 * @dup: node present in live tree to be updated
702 */
703static void update_node_properties(struct device_node *np,
704 struct device_node *dup)
705{
706 struct property *prop;
707 struct device_node *child;
708
709 for_each_property_of_node(np, prop)
710 of_add_property(dup, prop);
711
712 for_each_child_of_node(np, child)
713 child->parent = dup;
714}
715
716/**
717 * attach_node_and_children - attaches nodes
718 * and its children to live tree
719 *
720 * @np: Node to attach to live tree
721 */
722static int attach_node_and_children(struct device_node *np)
723{
724 struct device_node *next, *root = np, *dup;
725
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700726 /* skip root node */
727 np = np->child;
728 /* storing a copy in temporary node */
729 dup = np;
730
731 while (dup) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100732 if (WARN_ON(last_node_index >= NO_OF_NODES))
733 return -EINVAL;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700734 nodes[last_node_index++] = dup;
735 dup = dup->sibling;
736 }
737 dup = NULL;
738
739 while (np) {
740 next = np->allnext;
741 dup = of_find_node_by_path(np->full_name);
742 if (dup)
743 update_node_properties(np, dup);
744 else {
745 np->child = NULL;
746 if (np->parent == root)
747 np->parent = of_allnodes;
748 of_attach_node(np);
749 }
750 np = next;
751 }
752
753 return 0;
754}
755
756/**
757 * selftest_data_add - Reads, copies data from
758 * linked tree and attaches it to the live tree
759 */
760static int __init selftest_data_add(void)
761{
762 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700763 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700764 extern uint8_t __dtb_testcases_begin[];
765 extern uint8_t __dtb_testcases_end[];
766 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100767 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700768
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700769 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700770 pr_warn("%s: No testcase data to attach; not running tests\n",
771 __func__);
772 return -ENODATA;
773 }
774
775 /* creating copy */
776 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
777
778 if (!selftest_data) {
779 pr_warn("%s: Failed to allocate memory for selftest_data; "
780 "not running tests\n", __func__);
781 return -ENOMEM;
782 }
783 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700784 if (!selftest_data_node) {
785 pr_warn("%s: No tree to attach; not running tests\n", __func__);
786 return -ENODATA;
787 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100788 of_node_set_flag(selftest_data_node, OF_DETACHED);
789 rc = of_resolve_phandles(selftest_data_node);
790 if (rc) {
791 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
792 return -EINVAL;
793 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700794
795 if (!of_allnodes) {
796 /* enabling flag for removing nodes */
797 selftest_live_tree = true;
798 of_allnodes = selftest_data_node;
799
800 for_each_of_allnodes(np)
801 __of_attach_node_sysfs(np);
802 of_aliases = of_find_node_by_path("/aliases");
803 of_chosen = of_find_node_by_path("/chosen");
804 return 0;
805 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700806
807 /* attach the sub-tree to live tree */
808 return attach_node_and_children(selftest_data_node);
809}
810
811/**
812 * detach_node_and_children - detaches node
813 * and its children from live tree
814 *
815 * @np: Node to detach from live tree
816 */
817static void detach_node_and_children(struct device_node *np)
818{
819 while (np->child)
820 detach_node_and_children(np->child);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700821 of_detach_node(np);
822}
823
824/**
825 * selftest_data_remove - removes the selftest data
826 * nodes from the live tree
827 */
828static void selftest_data_remove(void)
829{
830 struct device_node *np;
831 struct property *prop;
832
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700833 if (selftest_live_tree) {
834 of_node_put(of_aliases);
835 of_node_put(of_chosen);
836 of_aliases = NULL;
837 of_chosen = NULL;
838 for_each_child_of_node(of_allnodes, np)
839 detach_node_and_children(np);
840 __of_detach_node_sysfs(of_allnodes);
841 of_allnodes = NULL;
842 return;
843 }
844
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700845 while (last_node_index >= 0) {
846 if (nodes[last_node_index]) {
847 np = of_find_node_by_path(nodes[last_node_index]->full_name);
848 if (strcmp(np->full_name, "/aliases") != 0) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100849 detach_node_and_children(np);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700850 } else {
851 for_each_property_of_node(np, prop) {
852 if (strcmp(prop->name, "testcase-alias") == 0)
853 of_remove_property(np, prop);
854 }
855 }
856 }
857 last_node_index--;
858 }
859}
860
Grant Likely53a42092011-12-12 09:25:57 -0700861static int __init of_selftest(void)
862{
863 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700864 int res;
865
866 /* adding data for selftest */
867 res = selftest_data_add();
868 if (res)
869 return res;
Grant Likely53a42092011-12-12 09:25:57 -0700870
871 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
872 if (!np) {
873 pr_info("No testcase data in device tree; not running tests\n");
874 return 0;
875 }
876 of_node_put(np);
877
878 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +0100879 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +0100880 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +0000881 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +0000882 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -0700883 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700884 of_selftest_property_match_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +0300885 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300886 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100887 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -0500888 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +0000889 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -0500890 of_selftest_platform_populate();
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700891
892 /* removing selftest data from live tree */
893 selftest_data_remove();
894
Grant Likelyf2051d62014-10-01 17:40:22 +0100895 /* Double check linkage after removing testcase data */
896 of_selftest_check_tree_linkage();
897
898 pr_info("end of selftest - %i passed, %i failed\n",
899 selftest_results.passed, selftest_results.failed);
900
Grant Likely53a42092011-12-12 09:25:57 -0700901 return 0;
902}
903late_initcall(of_selftest);