]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/of/unittest.c
of/unittest: Fix the wrong expected value in of_selftest_property_string
[mirror_ubuntu-artful-kernel.git] / drivers / of / unittest.c
1 /*
2 * Self tests for device tree subsystem
3 */
4
5 #define pr_fmt(fmt) "### dt-test ### " fmt
6
7 #include <linux/clk.h>
8 #include <linux/err.h>
9 #include <linux/errno.h>
10 #include <linux/hashtable.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
22
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
25
26 #include "of_private.h"
27
28 static struct selftest_results {
29 int passed;
30 int failed;
31 } selftest_results;
32
33 #define selftest(result, fmt, ...) ({ \
34 bool failed = !(result); \
35 if (failed) { \
36 selftest_results.failed++; \
37 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
38 } else { \
39 selftest_results.passed++; \
40 pr_debug("pass %s():%i\n", __func__, __LINE__); \
41 } \
42 failed; \
43 })
44
45 static void __init of_selftest_find_node_by_name(void)
46 {
47 struct device_node *np;
48 const char *options;
49
50 np = of_find_node_by_path("/testcase-data");
51 selftest(np && !strcmp("/testcase-data", np->full_name),
52 "find /testcase-data failed\n");
53 of_node_put(np);
54
55 /* Test if trailing '/' works */
56 np = of_find_node_by_path("/testcase-data/");
57 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
58
59 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
60 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
61 "find /testcase-data/phandle-tests/consumer-a failed\n");
62 of_node_put(np);
63
64 np = of_find_node_by_path("testcase-alias");
65 selftest(np && !strcmp("/testcase-data", np->full_name),
66 "find testcase-alias failed\n");
67 of_node_put(np);
68
69 /* Test if trailing '/' works on aliases */
70 np = of_find_node_by_path("testcase-alias/");
71 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
72
73 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
74 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
75 "find testcase-alias/phandle-tests/consumer-a failed\n");
76 of_node_put(np);
77
78 np = of_find_node_by_path("/testcase-data/missing-path");
79 selftest(!np, "non-existent path returned node %s\n", np->full_name);
80 of_node_put(np);
81
82 np = of_find_node_by_path("missing-alias");
83 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
84 of_node_put(np);
85
86 np = of_find_node_by_path("testcase-alias/missing-path");
87 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
88 of_node_put(np);
89
90 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
91 selftest(np && !strcmp("testoption", options),
92 "option path test failed\n");
93 of_node_put(np);
94
95 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
96 selftest(np, "NULL option path test failed\n");
97 of_node_put(np);
98
99 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
100 &options);
101 selftest(np && !strcmp("testaliasoption", options),
102 "option alias path test failed\n");
103 of_node_put(np);
104
105 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
106 selftest(np, "NULL option alias path test failed\n");
107 of_node_put(np);
108
109 options = "testoption";
110 np = of_find_node_opts_by_path("testcase-alias", &options);
111 selftest(np && !options, "option clearing test failed\n");
112 of_node_put(np);
113
114 options = "testoption";
115 np = of_find_node_opts_by_path("/", &options);
116 selftest(np && !options, "option clearing root node test failed\n");
117 of_node_put(np);
118 }
119
120 static void __init of_selftest_dynamic(void)
121 {
122 struct device_node *np;
123 struct property *prop;
124
125 np = of_find_node_by_path("/testcase-data");
126 if (!np) {
127 pr_err("missing testcase data\n");
128 return;
129 }
130
131 /* Array of 4 properties for the purpose of testing */
132 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
133 if (!prop) {
134 selftest(0, "kzalloc() failed\n");
135 return;
136 }
137
138 /* Add a new property - should pass*/
139 prop->name = "new-property";
140 prop->value = "new-property-data";
141 prop->length = strlen(prop->value);
142 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
143
144 /* Try to add an existing property - should fail */
145 prop++;
146 prop->name = "new-property";
147 prop->value = "new-property-data-should-fail";
148 prop->length = strlen(prop->value);
149 selftest(of_add_property(np, prop) != 0,
150 "Adding an existing property should have failed\n");
151
152 /* Try to modify an existing property - should pass */
153 prop->value = "modify-property-data-should-pass";
154 prop->length = strlen(prop->value);
155 selftest(of_update_property(np, prop) == 0,
156 "Updating an existing property should have passed\n");
157
158 /* Try to modify non-existent property - should pass*/
159 prop++;
160 prop->name = "modify-property";
161 prop->value = "modify-missing-property-data-should-pass";
162 prop->length = strlen(prop->value);
163 selftest(of_update_property(np, prop) == 0,
164 "Updating a missing property should have passed\n");
165
166 /* Remove property - should pass */
167 selftest(of_remove_property(np, prop) == 0,
168 "Removing a property should have passed\n");
169
170 /* Adding very large property - should pass */
171 prop++;
172 prop->name = "large-property-PAGE_SIZEx8";
173 prop->length = PAGE_SIZE * 8;
174 prop->value = kzalloc(prop->length, GFP_KERNEL);
175 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
176 if (prop->value)
177 selftest(of_add_property(np, prop) == 0,
178 "Adding a large property should have passed\n");
179 }
180
181 static int __init of_selftest_check_node_linkage(struct device_node *np)
182 {
183 struct device_node *child;
184 int count = 0, rc;
185
186 for_each_child_of_node(np, child) {
187 if (child->parent != np) {
188 pr_err("Child node %s links to wrong parent %s\n",
189 child->name, np->name);
190 return -EINVAL;
191 }
192
193 rc = of_selftest_check_node_linkage(child);
194 if (rc < 0)
195 return rc;
196 count += rc;
197 }
198
199 return count + 1;
200 }
201
202 static void __init of_selftest_check_tree_linkage(void)
203 {
204 struct device_node *np;
205 int allnode_count = 0, child_count;
206
207 if (!of_root)
208 return;
209
210 for_each_of_allnodes(np)
211 allnode_count++;
212 child_count = of_selftest_check_node_linkage(of_root);
213
214 selftest(child_count > 0, "Device node data structure is corrupted\n");
215 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
216 "sibling lists size (%i)\n", allnode_count, child_count);
217 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
218 }
219
220 struct node_hash {
221 struct hlist_node node;
222 struct device_node *np;
223 };
224
225 static DEFINE_HASHTABLE(phandle_ht, 8);
226 static void __init of_selftest_check_phandles(void)
227 {
228 struct device_node *np;
229 struct node_hash *nh;
230 struct hlist_node *tmp;
231 int i, dup_count = 0, phandle_count = 0;
232
233 for_each_of_allnodes(np) {
234 if (!np->phandle)
235 continue;
236
237 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
238 if (nh->np->phandle == np->phandle) {
239 pr_info("Duplicate phandle! %i used by %s and %s\n",
240 np->phandle, nh->np->full_name, np->full_name);
241 dup_count++;
242 break;
243 }
244 }
245
246 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
247 if (WARN_ON(!nh))
248 return;
249
250 nh->np = np;
251 hash_add(phandle_ht, &nh->node, np->phandle);
252 phandle_count++;
253 }
254 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
255 dup_count, phandle_count);
256
257 /* Clean up */
258 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
259 hash_del(&nh->node);
260 kfree(nh);
261 }
262 }
263
264 static void __init of_selftest_parse_phandle_with_args(void)
265 {
266 struct device_node *np;
267 struct of_phandle_args args;
268 int i, rc;
269
270 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
271 if (!np) {
272 pr_err("missing testcase data\n");
273 return;
274 }
275
276 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
277 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
278
279 for (i = 0; i < 8; i++) {
280 bool passed = true;
281 rc = of_parse_phandle_with_args(np, "phandle-list",
282 "#phandle-cells", i, &args);
283
284 /* Test the values from tests-phandle.dtsi */
285 switch (i) {
286 case 0:
287 passed &= !rc;
288 passed &= (args.args_count == 1);
289 passed &= (args.args[0] == (i + 1));
290 break;
291 case 1:
292 passed &= !rc;
293 passed &= (args.args_count == 2);
294 passed &= (args.args[0] == (i + 1));
295 passed &= (args.args[1] == 0);
296 break;
297 case 2:
298 passed &= (rc == -ENOENT);
299 break;
300 case 3:
301 passed &= !rc;
302 passed &= (args.args_count == 3);
303 passed &= (args.args[0] == (i + 1));
304 passed &= (args.args[1] == 4);
305 passed &= (args.args[2] == 3);
306 break;
307 case 4:
308 passed &= !rc;
309 passed &= (args.args_count == 2);
310 passed &= (args.args[0] == (i + 1));
311 passed &= (args.args[1] == 100);
312 break;
313 case 5:
314 passed &= !rc;
315 passed &= (args.args_count == 0);
316 break;
317 case 6:
318 passed &= !rc;
319 passed &= (args.args_count == 1);
320 passed &= (args.args[0] == (i + 1));
321 break;
322 case 7:
323 passed &= (rc == -ENOENT);
324 break;
325 default:
326 passed = false;
327 }
328
329 selftest(passed, "index %i - data error on node %s rc=%i\n",
330 i, args.np->full_name, rc);
331 }
332
333 /* Check for missing list property */
334 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
335 "#phandle-cells", 0, &args);
336 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
337 rc = of_count_phandle_with_args(np, "phandle-list-missing",
338 "#phandle-cells");
339 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
340
341 /* Check for missing cells property */
342 rc = of_parse_phandle_with_args(np, "phandle-list",
343 "#phandle-cells-missing", 0, &args);
344 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
345 rc = of_count_phandle_with_args(np, "phandle-list",
346 "#phandle-cells-missing");
347 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
348
349 /* Check for bad phandle in list */
350 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
351 "#phandle-cells", 0, &args);
352 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
353 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
354 "#phandle-cells");
355 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
356
357 /* Check for incorrectly formed argument list */
358 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
359 "#phandle-cells", 1, &args);
360 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
361 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
362 "#phandle-cells");
363 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
364 }
365
366 static void __init of_selftest_property_string(void)
367 {
368 const char *strings[4];
369 struct device_node *np;
370 int rc;
371
372 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
373 if (!np) {
374 pr_err("No testcase data in device tree\n");
375 return;
376 }
377
378 rc = of_property_match_string(np, "phandle-list-names", "first");
379 selftest(rc == 0, "first expected:0 got:%i\n", rc);
380 rc = of_property_match_string(np, "phandle-list-names", "second");
381 selftest(rc == 1, "second expected:1 got:%i\n", rc);
382 rc = of_property_match_string(np, "phandle-list-names", "third");
383 selftest(rc == 2, "third expected:2 got:%i\n", rc);
384 rc = of_property_match_string(np, "phandle-list-names", "fourth");
385 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
386 rc = of_property_match_string(np, "missing-property", "blah");
387 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
388 rc = of_property_match_string(np, "empty-property", "blah");
389 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
390 rc = of_property_match_string(np, "unterminated-string", "blah");
391 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
392
393 /* of_property_count_strings() tests */
394 rc = of_property_count_strings(np, "string-property");
395 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
396 rc = of_property_count_strings(np, "phandle-list-names");
397 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
398 rc = of_property_count_strings(np, "unterminated-string");
399 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
400 rc = of_property_count_strings(np, "unterminated-string-list");
401 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
402
403 /* of_property_read_string_index() tests */
404 rc = of_property_read_string_index(np, "string-property", 0, strings);
405 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
406 strings[0] = NULL;
407 rc = of_property_read_string_index(np, "string-property", 1, strings);
408 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
409 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
410 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
411 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
412 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
413 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
414 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
415 strings[0] = NULL;
416 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
417 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
418 strings[0] = NULL;
419 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
420 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
421 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
422 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
423 strings[0] = NULL;
424 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
425 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
426 strings[1] = NULL;
427
428 /* of_property_read_string_array() tests */
429 rc = of_property_read_string_array(np, "string-property", strings, 4);
430 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
431 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
432 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
433 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
434 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
435 /* -- An incorrectly formed string should cause a failure */
436 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
437 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
438 /* -- parsing the correctly formed strings should still work: */
439 strings[2] = NULL;
440 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
441 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
442 strings[1] = NULL;
443 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
444 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
445 }
446
447 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
448 (p1)->value && (p2)->value && \
449 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
450 !strcmp((p1)->name, (p2)->name))
451 static void __init of_selftest_property_copy(void)
452 {
453 #ifdef CONFIG_OF_DYNAMIC
454 struct property p1 = { .name = "p1", .length = 0, .value = "" };
455 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
456 struct property *new;
457
458 new = __of_prop_dup(&p1, GFP_KERNEL);
459 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
460 kfree(new->value);
461 kfree(new->name);
462 kfree(new);
463
464 new = __of_prop_dup(&p2, GFP_KERNEL);
465 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
466 kfree(new->value);
467 kfree(new->name);
468 kfree(new);
469 #endif
470 }
471
472 static void __init of_selftest_changeset(void)
473 {
474 #ifdef CONFIG_OF_DYNAMIC
475 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
476 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
477 struct property *ppremove;
478 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
479 struct of_changeset chgset;
480
481 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
482 selftest(n1, "testcase setup failure\n");
483 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
484 selftest(n2, "testcase setup failure\n");
485 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
486 selftest(n21, "testcase setup failure %p\n", n21);
487 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
488 selftest(nremove, "testcase setup failure\n");
489 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
490 selftest(ppadd, "testcase setup failure\n");
491 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
492 selftest(ppupdate, "testcase setup failure\n");
493 parent = nremove->parent;
494 n1->parent = parent;
495 n2->parent = parent;
496 n21->parent = n2;
497 n2->child = n21;
498 ppremove = of_find_property(parent, "prop-remove", NULL);
499 selftest(ppremove, "failed to find removal prop");
500
501 of_changeset_init(&chgset);
502 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
503 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
504 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
505 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
506 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
507 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
508 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
509 mutex_lock(&of_mutex);
510 selftest(!of_changeset_apply(&chgset), "apply failed\n");
511 mutex_unlock(&of_mutex);
512
513 /* Make sure node names are constructed correctly */
514 selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
515 "'%s' not added\n", n21->full_name);
516 of_node_put(np);
517
518 mutex_lock(&of_mutex);
519 selftest(!of_changeset_revert(&chgset), "revert failed\n");
520 mutex_unlock(&of_mutex);
521
522 of_changeset_destroy(&chgset);
523 #endif
524 }
525
526 static void __init of_selftest_parse_interrupts(void)
527 {
528 struct device_node *np;
529 struct of_phandle_args args;
530 int i, rc;
531
532 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
533 if (!np) {
534 pr_err("missing testcase data\n");
535 return;
536 }
537
538 for (i = 0; i < 4; i++) {
539 bool passed = true;
540 args.args_count = 0;
541 rc = of_irq_parse_one(np, i, &args);
542
543 passed &= !rc;
544 passed &= (args.args_count == 1);
545 passed &= (args.args[0] == (i + 1));
546
547 selftest(passed, "index %i - data error on node %s rc=%i\n",
548 i, args.np->full_name, rc);
549 }
550 of_node_put(np);
551
552 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
553 if (!np) {
554 pr_err("missing testcase data\n");
555 return;
556 }
557
558 for (i = 0; i < 4; i++) {
559 bool passed = true;
560 args.args_count = 0;
561 rc = of_irq_parse_one(np, i, &args);
562
563 /* Test the values from tests-phandle.dtsi */
564 switch (i) {
565 case 0:
566 passed &= !rc;
567 passed &= (args.args_count == 1);
568 passed &= (args.args[0] == 9);
569 break;
570 case 1:
571 passed &= !rc;
572 passed &= (args.args_count == 3);
573 passed &= (args.args[0] == 10);
574 passed &= (args.args[1] == 11);
575 passed &= (args.args[2] == 12);
576 break;
577 case 2:
578 passed &= !rc;
579 passed &= (args.args_count == 2);
580 passed &= (args.args[0] == 13);
581 passed &= (args.args[1] == 14);
582 break;
583 case 3:
584 passed &= !rc;
585 passed &= (args.args_count == 2);
586 passed &= (args.args[0] == 15);
587 passed &= (args.args[1] == 16);
588 break;
589 default:
590 passed = false;
591 }
592 selftest(passed, "index %i - data error on node %s rc=%i\n",
593 i, args.np->full_name, rc);
594 }
595 of_node_put(np);
596 }
597
598 static void __init of_selftest_parse_interrupts_extended(void)
599 {
600 struct device_node *np;
601 struct of_phandle_args args;
602 int i, rc;
603
604 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
605 if (!np) {
606 pr_err("missing testcase data\n");
607 return;
608 }
609
610 for (i = 0; i < 7; i++) {
611 bool passed = true;
612 rc = of_irq_parse_one(np, i, &args);
613
614 /* Test the values from tests-phandle.dtsi */
615 switch (i) {
616 case 0:
617 passed &= !rc;
618 passed &= (args.args_count == 1);
619 passed &= (args.args[0] == 1);
620 break;
621 case 1:
622 passed &= !rc;
623 passed &= (args.args_count == 3);
624 passed &= (args.args[0] == 2);
625 passed &= (args.args[1] == 3);
626 passed &= (args.args[2] == 4);
627 break;
628 case 2:
629 passed &= !rc;
630 passed &= (args.args_count == 2);
631 passed &= (args.args[0] == 5);
632 passed &= (args.args[1] == 6);
633 break;
634 case 3:
635 passed &= !rc;
636 passed &= (args.args_count == 1);
637 passed &= (args.args[0] == 9);
638 break;
639 case 4:
640 passed &= !rc;
641 passed &= (args.args_count == 3);
642 passed &= (args.args[0] == 10);
643 passed &= (args.args[1] == 11);
644 passed &= (args.args[2] == 12);
645 break;
646 case 5:
647 passed &= !rc;
648 passed &= (args.args_count == 2);
649 passed &= (args.args[0] == 13);
650 passed &= (args.args[1] == 14);
651 break;
652 case 6:
653 passed &= !rc;
654 passed &= (args.args_count == 1);
655 passed &= (args.args[0] == 15);
656 break;
657 default:
658 passed = false;
659 }
660
661 selftest(passed, "index %i - data error on node %s rc=%i\n",
662 i, args.np->full_name, rc);
663 }
664 of_node_put(np);
665 }
666
667 static struct of_device_id match_node_table[] = {
668 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
669 { .data = "B", .type = "type1", }, /* followed by type alone */
670
671 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
672 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
673 { .data = "Cc", .name = "name2", .type = "type2", },
674
675 { .data = "E", .compatible = "compat3" },
676 { .data = "G", .compatible = "compat2", },
677 { .data = "H", .compatible = "compat2", .name = "name5", },
678 { .data = "I", .compatible = "compat2", .type = "type1", },
679 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
680 { .data = "K", .compatible = "compat2", .name = "name9", },
681 {}
682 };
683
684 static struct {
685 const char *path;
686 const char *data;
687 } match_node_tests[] = {
688 { .path = "/testcase-data/match-node/name0", .data = "A", },
689 { .path = "/testcase-data/match-node/name1", .data = "B", },
690 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
691 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
692 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
693 { .path = "/testcase-data/match-node/name3", .data = "E", },
694 { .path = "/testcase-data/match-node/name4", .data = "G", },
695 { .path = "/testcase-data/match-node/name5", .data = "H", },
696 { .path = "/testcase-data/match-node/name6", .data = "G", },
697 { .path = "/testcase-data/match-node/name7", .data = "I", },
698 { .path = "/testcase-data/match-node/name8", .data = "J", },
699 { .path = "/testcase-data/match-node/name9", .data = "K", },
700 };
701
702 static void __init of_selftest_match_node(void)
703 {
704 struct device_node *np;
705 const struct of_device_id *match;
706 int i;
707
708 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
709 np = of_find_node_by_path(match_node_tests[i].path);
710 if (!np) {
711 selftest(0, "missing testcase node %s\n",
712 match_node_tests[i].path);
713 continue;
714 }
715
716 match = of_match_node(match_node_table, np);
717 if (!match) {
718 selftest(0, "%s didn't match anything\n",
719 match_node_tests[i].path);
720 continue;
721 }
722
723 if (strcmp(match->data, match_node_tests[i].data) != 0) {
724 selftest(0, "%s got wrong match. expected %s, got %s\n",
725 match_node_tests[i].path, match_node_tests[i].data,
726 (const char *)match->data);
727 continue;
728 }
729 selftest(1, "passed");
730 }
731 }
732
733 struct device test_bus = {
734 .init_name = "unittest-bus",
735 };
736 static void __init of_selftest_platform_populate(void)
737 {
738 int irq, rc;
739 struct device_node *np, *child, *grandchild;
740 struct platform_device *pdev;
741 struct of_device_id match[] = {
742 { .compatible = "test-device", },
743 {}
744 };
745
746 np = of_find_node_by_path("/testcase-data");
747 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
748
749 /* Test that a missing irq domain returns -EPROBE_DEFER */
750 np = of_find_node_by_path("/testcase-data/testcase-device1");
751 pdev = of_find_device_by_node(np);
752 selftest(pdev, "device 1 creation failed\n");
753
754 irq = platform_get_irq(pdev, 0);
755 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
756
757 /* Test that a parsing failure does not return -EPROBE_DEFER */
758 np = of_find_node_by_path("/testcase-data/testcase-device2");
759 pdev = of_find_device_by_node(np);
760 selftest(pdev, "device 2 creation failed\n");
761 irq = platform_get_irq(pdev, 0);
762 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
763
764 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
765 "No testcase data in device tree\n"));
766 return;
767
768 if (selftest(!(rc = device_register(&test_bus)),
769 "testbus registration failed; rc=%i\n", rc));
770 return;
771
772 for_each_child_of_node(np, child) {
773 of_platform_populate(child, match, NULL, &test_bus);
774 for_each_child_of_node(child, grandchild)
775 selftest(of_find_device_by_node(grandchild),
776 "Could not create device for node '%s'\n",
777 grandchild->name);
778 }
779
780 of_platform_depopulate(&test_bus);
781 for_each_child_of_node(np, child) {
782 for_each_child_of_node(child, grandchild)
783 selftest(!of_find_device_by_node(grandchild),
784 "device didn't get destroyed '%s'\n",
785 grandchild->name);
786 }
787
788 device_unregister(&test_bus);
789 of_node_put(np);
790 }
791
792 /**
793 * update_node_properties - adds the properties
794 * of np into dup node (present in live tree) and
795 * updates parent of children of np to dup.
796 *
797 * @np: node already present in live tree
798 * @dup: node present in live tree to be updated
799 */
800 static void update_node_properties(struct device_node *np,
801 struct device_node *dup)
802 {
803 struct property *prop;
804 struct device_node *child;
805
806 for_each_property_of_node(np, prop)
807 of_add_property(dup, prop);
808
809 for_each_child_of_node(np, child)
810 child->parent = dup;
811 }
812
813 /**
814 * attach_node_and_children - attaches nodes
815 * and its children to live tree
816 *
817 * @np: Node to attach to live tree
818 */
819 static int attach_node_and_children(struct device_node *np)
820 {
821 struct device_node *next, *dup, *child;
822 unsigned long flags;
823
824 dup = of_find_node_by_path(np->full_name);
825 if (dup) {
826 update_node_properties(np, dup);
827 return 0;
828 }
829
830 child = np->child;
831 np->child = NULL;
832
833 mutex_lock(&of_mutex);
834 raw_spin_lock_irqsave(&devtree_lock, flags);
835 np->sibling = np->parent->child;
836 np->parent->child = np;
837 of_node_clear_flag(np, OF_DETACHED);
838 raw_spin_unlock_irqrestore(&devtree_lock, flags);
839
840 __of_attach_node_sysfs(np);
841 mutex_unlock(&of_mutex);
842
843 while (child) {
844 next = child->sibling;
845 attach_node_and_children(child);
846 child = next;
847 }
848
849 return 0;
850 }
851
852 /**
853 * selftest_data_add - Reads, copies data from
854 * linked tree and attaches it to the live tree
855 */
856 static int __init selftest_data_add(void)
857 {
858 void *selftest_data;
859 struct device_node *selftest_data_node, *np;
860 extern uint8_t __dtb_testcases_begin[];
861 extern uint8_t __dtb_testcases_end[];
862 const int size = __dtb_testcases_end - __dtb_testcases_begin;
863 int rc;
864
865 if (!size) {
866 pr_warn("%s: No testcase data to attach; not running tests\n",
867 __func__);
868 return -ENODATA;
869 }
870
871 /* creating copy */
872 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
873
874 if (!selftest_data) {
875 pr_warn("%s: Failed to allocate memory for selftest_data; "
876 "not running tests\n", __func__);
877 return -ENOMEM;
878 }
879 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
880 if (!selftest_data_node) {
881 pr_warn("%s: No tree to attach; not running tests\n", __func__);
882 return -ENODATA;
883 }
884 of_node_set_flag(selftest_data_node, OF_DETACHED);
885 rc = of_resolve_phandles(selftest_data_node);
886 if (rc) {
887 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
888 return -EINVAL;
889 }
890
891 if (!of_root) {
892 of_root = selftest_data_node;
893 for_each_of_allnodes(np)
894 __of_attach_node_sysfs(np);
895 of_aliases = of_find_node_by_path("/aliases");
896 of_chosen = of_find_node_by_path("/chosen");
897 return 0;
898 }
899
900 /* attach the sub-tree to live tree */
901 np = selftest_data_node->child;
902 while (np) {
903 struct device_node *next = np->sibling;
904 np->parent = of_root;
905 attach_node_and_children(np);
906 np = next;
907 }
908 return 0;
909 }
910
911 #ifdef CONFIG_OF_OVERLAY
912
913 static int selftest_probe(struct platform_device *pdev)
914 {
915 struct device *dev = &pdev->dev;
916 struct device_node *np = dev->of_node;
917
918 if (np == NULL) {
919 dev_err(dev, "No OF data for device\n");
920 return -EINVAL;
921
922 }
923
924 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
925
926 of_platform_populate(np, NULL, NULL, &pdev->dev);
927
928 return 0;
929 }
930
931 static int selftest_remove(struct platform_device *pdev)
932 {
933 struct device *dev = &pdev->dev;
934 struct device_node *np = dev->of_node;
935
936 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
937 return 0;
938 }
939
940 static struct of_device_id selftest_match[] = {
941 { .compatible = "selftest", },
942 {},
943 };
944
945 static struct platform_driver selftest_driver = {
946 .probe = selftest_probe,
947 .remove = selftest_remove,
948 .driver = {
949 .name = "selftest",
950 .owner = THIS_MODULE,
951 .of_match_table = of_match_ptr(selftest_match),
952 },
953 };
954
955 /* get the platform device instantiated at the path */
956 static struct platform_device *of_path_to_platform_device(const char *path)
957 {
958 struct device_node *np;
959 struct platform_device *pdev;
960
961 np = of_find_node_by_path(path);
962 if (np == NULL)
963 return NULL;
964
965 pdev = of_find_device_by_node(np);
966 of_node_put(np);
967
968 return pdev;
969 }
970
971 /* find out if a platform device exists at that path */
972 static int of_path_platform_device_exists(const char *path)
973 {
974 struct platform_device *pdev;
975
976 pdev = of_path_to_platform_device(path);
977 platform_device_put(pdev);
978 return pdev != NULL;
979 }
980
981 #if IS_BUILTIN(CONFIG_I2C)
982
983 /* get the i2c client device instantiated at the path */
984 static struct i2c_client *of_path_to_i2c_client(const char *path)
985 {
986 struct device_node *np;
987 struct i2c_client *client;
988
989 np = of_find_node_by_path(path);
990 if (np == NULL)
991 return NULL;
992
993 client = of_find_i2c_device_by_node(np);
994 of_node_put(np);
995
996 return client;
997 }
998
999 /* find out if a i2c client device exists at that path */
1000 static int of_path_i2c_client_exists(const char *path)
1001 {
1002 struct i2c_client *client;
1003
1004 client = of_path_to_i2c_client(path);
1005 if (client)
1006 put_device(&client->dev);
1007 return client != NULL;
1008 }
1009 #else
1010 static int of_path_i2c_client_exists(const char *path)
1011 {
1012 return 0;
1013 }
1014 #endif
1015
1016 enum overlay_type {
1017 PDEV_OVERLAY,
1018 I2C_OVERLAY
1019 };
1020
1021 static int of_path_device_type_exists(const char *path,
1022 enum overlay_type ovtype)
1023 {
1024 switch (ovtype) {
1025 case PDEV_OVERLAY:
1026 return of_path_platform_device_exists(path);
1027 case I2C_OVERLAY:
1028 return of_path_i2c_client_exists(path);
1029 }
1030 return 0;
1031 }
1032
1033 static const char *selftest_path(int nr, enum overlay_type ovtype)
1034 {
1035 const char *base;
1036 static char buf[256];
1037
1038 switch (ovtype) {
1039 case PDEV_OVERLAY:
1040 base = "/testcase-data/overlay-node/test-bus";
1041 break;
1042 case I2C_OVERLAY:
1043 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1044 break;
1045 default:
1046 buf[0] = '\0';
1047 return buf;
1048 }
1049 snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr);
1050 buf[sizeof(buf) - 1] = '\0';
1051 return buf;
1052 }
1053
1054 static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype)
1055 {
1056 const char *path;
1057
1058 path = selftest_path(selftest_nr, ovtype);
1059
1060 switch (ovtype) {
1061 case PDEV_OVERLAY:
1062 return of_path_platform_device_exists(path);
1063 case I2C_OVERLAY:
1064 return of_path_i2c_client_exists(path);
1065 }
1066 return 0;
1067 }
1068
1069 static const char *overlay_path(int nr)
1070 {
1071 static char buf[256];
1072
1073 snprintf(buf, sizeof(buf) - 1,
1074 "/testcase-data/overlay%d", nr);
1075 buf[sizeof(buf) - 1] = '\0';
1076
1077 return buf;
1078 }
1079
1080 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1081
1082 static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1083 int *overlay_id)
1084 {
1085 struct device_node *np = NULL;
1086 int ret, id = -1;
1087
1088 np = of_find_node_by_path(overlay_path(overlay_nr));
1089 if (np == NULL) {
1090 selftest(0, "could not find overlay node @\"%s\"\n",
1091 overlay_path(overlay_nr));
1092 ret = -EINVAL;
1093 goto out;
1094 }
1095
1096 ret = of_overlay_create(np);
1097 if (ret < 0) {
1098 selftest(0, "could not create overlay from \"%s\"\n",
1099 overlay_path(overlay_nr));
1100 goto out;
1101 }
1102 id = ret;
1103
1104 ret = 0;
1105
1106 out:
1107 of_node_put(np);
1108
1109 if (overlay_id)
1110 *overlay_id = id;
1111
1112 return ret;
1113 }
1114
1115 /* apply an overlay while checking before and after states */
1116 static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1117 int before, int after, enum overlay_type ovtype)
1118 {
1119 int ret;
1120
1121 /* selftest device must not be in before state */
1122 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
1123 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1124 overlay_path(overlay_nr),
1125 selftest_path(selftest_nr, ovtype),
1126 !before ? "enabled" : "disabled");
1127 return -EINVAL;
1128 }
1129
1130 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1131 if (ret != 0) {
1132 /* of_selftest_apply_overlay already called selftest() */
1133 return ret;
1134 }
1135
1136 /* selftest device must be to set to after state */
1137 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
1138 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1139 overlay_path(overlay_nr),
1140 selftest_path(selftest_nr, ovtype),
1141 !after ? "enabled" : "disabled");
1142 return -EINVAL;
1143 }
1144
1145 return 0;
1146 }
1147
1148 /* apply an overlay and then revert it while checking before, after states */
1149 static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1150 int selftest_nr, int before, int after,
1151 enum overlay_type ovtype)
1152 {
1153 int ret, ov_id;
1154
1155 /* selftest device must be in before state */
1156 if (of_selftest_device_exists(selftest_nr, ovtype) != before) {
1157 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1158 overlay_path(overlay_nr),
1159 selftest_path(selftest_nr, ovtype),
1160 !before ? "enabled" : "disabled");
1161 return -EINVAL;
1162 }
1163
1164 /* apply the overlay */
1165 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1166 if (ret != 0) {
1167 /* of_selftest_apply_overlay already called selftest() */
1168 return ret;
1169 }
1170
1171 /* selftest device must be in after state */
1172 if (of_selftest_device_exists(selftest_nr, ovtype) != after) {
1173 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1174 overlay_path(overlay_nr),
1175 selftest_path(selftest_nr, ovtype),
1176 !after ? "enabled" : "disabled");
1177 return -EINVAL;
1178 }
1179
1180 ret = of_overlay_destroy(ov_id);
1181 if (ret != 0) {
1182 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1183 overlay_path(overlay_nr),
1184 selftest_path(selftest_nr, ovtype));
1185 return ret;
1186 }
1187
1188 /* selftest device must be again in before state */
1189 if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) {
1190 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1191 overlay_path(overlay_nr),
1192 selftest_path(selftest_nr, ovtype),
1193 !before ? "enabled" : "disabled");
1194 return -EINVAL;
1195 }
1196
1197 return 0;
1198 }
1199
1200 /* test activation of device */
1201 static void of_selftest_overlay_0(void)
1202 {
1203 int ret;
1204
1205 /* device should enable */
1206 ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1207 if (ret != 0)
1208 return;
1209
1210 selftest(1, "overlay test %d passed\n", 0);
1211 }
1212
1213 /* test deactivation of device */
1214 static void of_selftest_overlay_1(void)
1215 {
1216 int ret;
1217
1218 /* device should disable */
1219 ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1220 if (ret != 0)
1221 return;
1222
1223 selftest(1, "overlay test %d passed\n", 1);
1224 }
1225
1226 /* test activation of device */
1227 static void of_selftest_overlay_2(void)
1228 {
1229 int ret;
1230
1231 /* device should enable */
1232 ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1233 if (ret != 0)
1234 return;
1235
1236 selftest(1, "overlay test %d passed\n", 2);
1237 }
1238
1239 /* test deactivation of device */
1240 static void of_selftest_overlay_3(void)
1241 {
1242 int ret;
1243
1244 /* device should disable */
1245 ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1246 if (ret != 0)
1247 return;
1248
1249 selftest(1, "overlay test %d passed\n", 3);
1250 }
1251
1252 /* test activation of a full device node */
1253 static void of_selftest_overlay_4(void)
1254 {
1255 int ret;
1256
1257 /* device should disable */
1258 ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1259 if (ret != 0)
1260 return;
1261
1262 selftest(1, "overlay test %d passed\n", 4);
1263 }
1264
1265 /* test overlay apply/revert sequence */
1266 static void of_selftest_overlay_5(void)
1267 {
1268 int ret;
1269
1270 /* device should disable */
1271 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1272 if (ret != 0)
1273 return;
1274
1275 selftest(1, "overlay test %d passed\n", 5);
1276 }
1277
1278 /* test overlay application in sequence */
1279 static void of_selftest_overlay_6(void)
1280 {
1281 struct device_node *np;
1282 int ret, i, ov_id[2];
1283 int overlay_nr = 6, selftest_nr = 6;
1284 int before = 0, after = 1;
1285
1286 /* selftest device must be in before state */
1287 for (i = 0; i < 2; i++) {
1288 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1289 != before) {
1290 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1291 overlay_path(overlay_nr + i),
1292 selftest_path(selftest_nr + i,
1293 PDEV_OVERLAY),
1294 !before ? "enabled" : "disabled");
1295 return;
1296 }
1297 }
1298
1299 /* apply the overlays */
1300 for (i = 0; i < 2; i++) {
1301
1302 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1303 if (np == NULL) {
1304 selftest(0, "could not find overlay node @\"%s\"\n",
1305 overlay_path(overlay_nr + i));
1306 return;
1307 }
1308
1309 ret = of_overlay_create(np);
1310 if (ret < 0) {
1311 selftest(0, "could not create overlay from \"%s\"\n",
1312 overlay_path(overlay_nr + i));
1313 return;
1314 }
1315 ov_id[i] = ret;
1316 }
1317
1318 for (i = 0; i < 2; i++) {
1319 /* selftest device must be in after state */
1320 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1321 != after) {
1322 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1323 overlay_path(overlay_nr + i),
1324 selftest_path(selftest_nr + i,
1325 PDEV_OVERLAY),
1326 !after ? "enabled" : "disabled");
1327 return;
1328 }
1329 }
1330
1331 for (i = 1; i >= 0; i--) {
1332 ret = of_overlay_destroy(ov_id[i]);
1333 if (ret != 0) {
1334 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1335 overlay_path(overlay_nr + i),
1336 selftest_path(selftest_nr + i,
1337 PDEV_OVERLAY));
1338 return;
1339 }
1340 }
1341
1342 for (i = 0; i < 2; i++) {
1343 /* selftest device must be again in before state */
1344 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY)
1345 != before) {
1346 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1347 overlay_path(overlay_nr + i),
1348 selftest_path(selftest_nr + i,
1349 PDEV_OVERLAY),
1350 !before ? "enabled" : "disabled");
1351 return;
1352 }
1353 }
1354
1355 selftest(1, "overlay test %d passed\n", 6);
1356 }
1357
1358 /* test overlay application in sequence */
1359 static void of_selftest_overlay_8(void)
1360 {
1361 struct device_node *np;
1362 int ret, i, ov_id[2];
1363 int overlay_nr = 8, selftest_nr = 8;
1364
1365 /* we don't care about device state in this test */
1366
1367 /* apply the overlays */
1368 for (i = 0; i < 2; i++) {
1369
1370 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1371 if (np == NULL) {
1372 selftest(0, "could not find overlay node @\"%s\"\n",
1373 overlay_path(overlay_nr + i));
1374 return;
1375 }
1376
1377 ret = of_overlay_create(np);
1378 if (ret < 0) {
1379 selftest(0, "could not create overlay from \"%s\"\n",
1380 overlay_path(overlay_nr + i));
1381 return;
1382 }
1383 ov_id[i] = ret;
1384 }
1385
1386 /* now try to remove first overlay (it should fail) */
1387 ret = of_overlay_destroy(ov_id[0]);
1388 if (ret == 0) {
1389 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1390 overlay_path(overlay_nr + 0),
1391 selftest_path(selftest_nr,
1392 PDEV_OVERLAY));
1393 return;
1394 }
1395
1396 /* removing them in order should work */
1397 for (i = 1; i >= 0; i--) {
1398 ret = of_overlay_destroy(ov_id[i]);
1399 if (ret != 0) {
1400 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1401 overlay_path(overlay_nr + i),
1402 selftest_path(selftest_nr,
1403 PDEV_OVERLAY));
1404 return;
1405 }
1406 }
1407
1408 selftest(1, "overlay test %d passed\n", 8);
1409 }
1410
1411 /* test insertion of a bus with parent devices */
1412 static void of_selftest_overlay_10(void)
1413 {
1414 int ret;
1415 char *child_path;
1416
1417 /* device should disable */
1418 ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1419 if (selftest(ret == 0,
1420 "overlay test %d failed; overlay application\n", 10))
1421 return;
1422
1423 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101",
1424 selftest_path(10, PDEV_OVERLAY));
1425 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10))
1426 return;
1427
1428 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1429 kfree(child_path);
1430 if (selftest(ret, "overlay test %d failed; no child device\n", 10))
1431 return;
1432 }
1433
1434 /* test insertion of a bus with parent devices (and revert) */
1435 static void of_selftest_overlay_11(void)
1436 {
1437 int ret;
1438
1439 /* device should disable */
1440 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1,
1441 PDEV_OVERLAY);
1442 if (selftest(ret == 0,
1443 "overlay test %d failed; overlay application\n", 11))
1444 return;
1445 }
1446
1447 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1448
1449 struct selftest_i2c_bus_data {
1450 struct platform_device *pdev;
1451 struct i2c_adapter adap;
1452 };
1453
1454 static int selftest_i2c_master_xfer(struct i2c_adapter *adap,
1455 struct i2c_msg *msgs, int num)
1456 {
1457 struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap);
1458
1459 (void)std;
1460
1461 return num;
1462 }
1463
1464 static u32 selftest_i2c_functionality(struct i2c_adapter *adap)
1465 {
1466 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1467 }
1468
1469 static const struct i2c_algorithm selftest_i2c_algo = {
1470 .master_xfer = selftest_i2c_master_xfer,
1471 .functionality = selftest_i2c_functionality,
1472 };
1473
1474 static int selftest_i2c_bus_probe(struct platform_device *pdev)
1475 {
1476 struct device *dev = &pdev->dev;
1477 struct device_node *np = dev->of_node;
1478 struct selftest_i2c_bus_data *std;
1479 struct i2c_adapter *adap;
1480 int ret;
1481
1482 if (np == NULL) {
1483 dev_err(dev, "No OF data for device\n");
1484 return -EINVAL;
1485
1486 }
1487
1488 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1489
1490 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1491 if (!std) {
1492 dev_err(dev, "Failed to allocate selftest i2c data\n");
1493 return -ENOMEM;
1494 }
1495
1496 /* link them together */
1497 std->pdev = pdev;
1498 platform_set_drvdata(pdev, std);
1499
1500 adap = &std->adap;
1501 i2c_set_adapdata(adap, std);
1502 adap->nr = -1;
1503 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1504 adap->class = I2C_CLASS_DEPRECATED;
1505 adap->algo = &selftest_i2c_algo;
1506 adap->dev.parent = dev;
1507 adap->dev.of_node = dev->of_node;
1508 adap->timeout = 5 * HZ;
1509 adap->retries = 3;
1510
1511 ret = i2c_add_numbered_adapter(adap);
1512 if (ret != 0) {
1513 dev_err(dev, "Failed to add I2C adapter\n");
1514 return ret;
1515 }
1516
1517 return 0;
1518 }
1519
1520 static int selftest_i2c_bus_remove(struct platform_device *pdev)
1521 {
1522 struct device *dev = &pdev->dev;
1523 struct device_node *np = dev->of_node;
1524 struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev);
1525
1526 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1527 i2c_del_adapter(&std->adap);
1528
1529 return 0;
1530 }
1531
1532 static struct of_device_id selftest_i2c_bus_match[] = {
1533 { .compatible = "selftest-i2c-bus", },
1534 {},
1535 };
1536
1537 static struct platform_driver selftest_i2c_bus_driver = {
1538 .probe = selftest_i2c_bus_probe,
1539 .remove = selftest_i2c_bus_remove,
1540 .driver = {
1541 .name = "selftest-i2c-bus",
1542 .of_match_table = of_match_ptr(selftest_i2c_bus_match),
1543 },
1544 };
1545
1546 static int selftest_i2c_dev_probe(struct i2c_client *client,
1547 const struct i2c_device_id *id)
1548 {
1549 struct device *dev = &client->dev;
1550 struct device_node *np = client->dev.of_node;
1551
1552 if (!np) {
1553 dev_err(dev, "No OF node\n");
1554 return -EINVAL;
1555 }
1556
1557 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1558
1559 return 0;
1560 };
1561
1562 static int selftest_i2c_dev_remove(struct i2c_client *client)
1563 {
1564 struct device *dev = &client->dev;
1565 struct device_node *np = client->dev.of_node;
1566
1567 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1568 return 0;
1569 }
1570
1571 static const struct i2c_device_id selftest_i2c_dev_id[] = {
1572 { .name = "selftest-i2c-dev" },
1573 { }
1574 };
1575
1576 static struct i2c_driver selftest_i2c_dev_driver = {
1577 .driver = {
1578 .name = "selftest-i2c-dev",
1579 .owner = THIS_MODULE,
1580 },
1581 .probe = selftest_i2c_dev_probe,
1582 .remove = selftest_i2c_dev_remove,
1583 .id_table = selftest_i2c_dev_id,
1584 };
1585
1586 #if IS_BUILTIN(CONFIG_I2C_MUX)
1587
1588 struct selftest_i2c_mux_data {
1589 int nchans;
1590 struct i2c_adapter *adap[];
1591 };
1592
1593 static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap,
1594 void *client, u32 chan)
1595 {
1596 return 0;
1597 }
1598
1599 static int selftest_i2c_mux_probe(struct i2c_client *client,
1600 const struct i2c_device_id *id)
1601 {
1602 int ret, i, nchans, size;
1603 struct device *dev = &client->dev;
1604 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1605 struct device_node *np = client->dev.of_node, *child;
1606 struct selftest_i2c_mux_data *stm;
1607 u32 reg, max_reg;
1608
1609 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1610
1611 if (!np) {
1612 dev_err(dev, "No OF node\n");
1613 return -EINVAL;
1614 }
1615
1616 max_reg = (u32)-1;
1617 for_each_child_of_node(np, child) {
1618 ret = of_property_read_u32(child, "reg", &reg);
1619 if (ret)
1620 continue;
1621 if (max_reg == (u32)-1 || reg > max_reg)
1622 max_reg = reg;
1623 }
1624 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1625 if (nchans == 0) {
1626 dev_err(dev, "No channels\n");
1627 return -EINVAL;
1628 }
1629
1630 size = offsetof(struct selftest_i2c_mux_data, adap[nchans]);
1631 stm = devm_kzalloc(dev, size, GFP_KERNEL);
1632 if (!stm) {
1633 dev_err(dev, "Out of memory\n");
1634 return -ENOMEM;
1635 }
1636 stm->nchans = nchans;
1637 for (i = 0; i < nchans; i++) {
1638 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
1639 0, i, 0, selftest_i2c_mux_select_chan, NULL);
1640 if (!stm->adap[i]) {
1641 dev_err(dev, "Failed to register mux #%d\n", i);
1642 for (i--; i >= 0; i--)
1643 i2c_del_mux_adapter(stm->adap[i]);
1644 return -ENODEV;
1645 }
1646 }
1647
1648 i2c_set_clientdata(client, stm);
1649
1650 return 0;
1651 };
1652
1653 static int selftest_i2c_mux_remove(struct i2c_client *client)
1654 {
1655 struct device *dev = &client->dev;
1656 struct device_node *np = client->dev.of_node;
1657 struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client);
1658 int i;
1659
1660 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1661 for (i = stm->nchans - 1; i >= 0; i--)
1662 i2c_del_mux_adapter(stm->adap[i]);
1663 return 0;
1664 }
1665
1666 static const struct i2c_device_id selftest_i2c_mux_id[] = {
1667 { .name = "selftest-i2c-mux" },
1668 { }
1669 };
1670
1671 static struct i2c_driver selftest_i2c_mux_driver = {
1672 .driver = {
1673 .name = "selftest-i2c-mux",
1674 .owner = THIS_MODULE,
1675 },
1676 .probe = selftest_i2c_mux_probe,
1677 .remove = selftest_i2c_mux_remove,
1678 .id_table = selftest_i2c_mux_id,
1679 };
1680
1681 #endif
1682
1683 static int of_selftest_overlay_i2c_init(void)
1684 {
1685 int ret;
1686
1687 ret = i2c_add_driver(&selftest_i2c_dev_driver);
1688 if (selftest(ret == 0,
1689 "could not register selftest i2c device driver\n"))
1690 return ret;
1691
1692 ret = platform_driver_register(&selftest_i2c_bus_driver);
1693 if (selftest(ret == 0,
1694 "could not register selftest i2c bus driver\n"))
1695 return ret;
1696
1697 #if IS_BUILTIN(CONFIG_I2C_MUX)
1698 ret = i2c_add_driver(&selftest_i2c_mux_driver);
1699 if (selftest(ret == 0,
1700 "could not register selftest i2c mux driver\n"))
1701 return ret;
1702 #endif
1703
1704 return 0;
1705 }
1706
1707 static void of_selftest_overlay_i2c_cleanup(void)
1708 {
1709 #if IS_BUILTIN(CONFIG_I2C_MUX)
1710 i2c_del_driver(&selftest_i2c_mux_driver);
1711 #endif
1712 platform_driver_unregister(&selftest_i2c_bus_driver);
1713 i2c_del_driver(&selftest_i2c_dev_driver);
1714 }
1715
1716 static void of_selftest_overlay_i2c_12(void)
1717 {
1718 int ret;
1719
1720 /* device should enable */
1721 ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1722 if (ret != 0)
1723 return;
1724
1725 selftest(1, "overlay test %d passed\n", 12);
1726 }
1727
1728 /* test deactivation of device */
1729 static void of_selftest_overlay_i2c_13(void)
1730 {
1731 int ret;
1732
1733 /* device should disable */
1734 ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1735 if (ret != 0)
1736 return;
1737
1738 selftest(1, "overlay test %d passed\n", 13);
1739 }
1740
1741 /* just check for i2c mux existence */
1742 static void of_selftest_overlay_i2c_14(void)
1743 {
1744 }
1745
1746 static void of_selftest_overlay_i2c_15(void)
1747 {
1748 int ret;
1749
1750 /* device should enable */
1751 ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
1752 if (ret != 0)
1753 return;
1754
1755 selftest(1, "overlay test %d passed\n", 15);
1756 }
1757
1758 #else
1759
1760 static inline void of_selftest_overlay_i2c_14(void) { }
1761 static inline void of_selftest_overlay_i2c_15(void) { }
1762
1763 #endif
1764
1765 static void __init of_selftest_overlay(void)
1766 {
1767 struct device_node *bus_np = NULL;
1768 int ret;
1769
1770 ret = platform_driver_register(&selftest_driver);
1771 if (ret != 0) {
1772 selftest(0, "could not register selftest driver\n");
1773 goto out;
1774 }
1775
1776 bus_np = of_find_node_by_path(bus_path);
1777 if (bus_np == NULL) {
1778 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1779 goto out;
1780 }
1781
1782 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1783 NULL, NULL);
1784 if (ret != 0) {
1785 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1786 goto out;
1787 }
1788
1789 if (!of_selftest_device_exists(100, PDEV_OVERLAY)) {
1790 selftest(0, "could not find selftest0 @ \"%s\"\n",
1791 selftest_path(100, PDEV_OVERLAY));
1792 goto out;
1793 }
1794
1795 if (of_selftest_device_exists(101, PDEV_OVERLAY)) {
1796 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1797 selftest_path(101, PDEV_OVERLAY));
1798 goto out;
1799 }
1800
1801 selftest(1, "basic infrastructure of overlays passed");
1802
1803 /* tests in sequence */
1804 of_selftest_overlay_0();
1805 of_selftest_overlay_1();
1806 of_selftest_overlay_2();
1807 of_selftest_overlay_3();
1808 of_selftest_overlay_4();
1809 of_selftest_overlay_5();
1810 of_selftest_overlay_6();
1811 of_selftest_overlay_8();
1812
1813 of_selftest_overlay_10();
1814 of_selftest_overlay_11();
1815
1816 #if IS_BUILTIN(CONFIG_I2C)
1817 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n"))
1818 goto out;
1819
1820 of_selftest_overlay_i2c_12();
1821 of_selftest_overlay_i2c_13();
1822 of_selftest_overlay_i2c_14();
1823 of_selftest_overlay_i2c_15();
1824
1825 of_selftest_overlay_i2c_cleanup();
1826 #endif
1827
1828 out:
1829 of_node_put(bus_np);
1830 }
1831
1832 #else
1833 static inline void __init of_selftest_overlay(void) { }
1834 #endif
1835
1836 static int __init of_selftest(void)
1837 {
1838 struct device_node *np;
1839 int res;
1840
1841 /* adding data for selftest */
1842 res = selftest_data_add();
1843 if (res)
1844 return res;
1845 if (!of_aliases)
1846 of_aliases = of_find_node_by_path("/aliases");
1847
1848 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1849 if (!np) {
1850 pr_info("No testcase data in device tree; not running tests\n");
1851 return 0;
1852 }
1853 of_node_put(np);
1854
1855 pr_info("start of selftest - you will see error messages\n");
1856 of_selftest_check_tree_linkage();
1857 of_selftest_check_phandles();
1858 of_selftest_find_node_by_name();
1859 of_selftest_dynamic();
1860 of_selftest_parse_phandle_with_args();
1861 of_selftest_property_string();
1862 of_selftest_property_copy();
1863 of_selftest_changeset();
1864 of_selftest_parse_interrupts();
1865 of_selftest_parse_interrupts_extended();
1866 of_selftest_match_node();
1867 of_selftest_platform_populate();
1868 of_selftest_overlay();
1869
1870 /* Double check linkage after removing testcase data */
1871 of_selftest_check_tree_linkage();
1872
1873 pr_info("end of selftest - %i passed, %i failed\n",
1874 selftest_results.passed, selftest_results.failed);
1875
1876 return 0;
1877 }
1878 late_initcall(of_selftest);