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