]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/of/unittest.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / of / unittest.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Self tests for device tree subsystem
4 */
5
6 #define pr_fmt(fmt) "### dt-test ### " fmt
7
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/errno.h>
11 #include <linux/hashtable.h>
12 #include <linux/libfdt.h>
13 #include <linux/of.h>
14 #include <linux/of_fdt.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_platform.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/platform_device.h>
22
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
25
26 #include <linux/bitops.h>
27
28 #include "of_private.h"
29
30 static struct unittest_results {
31 int passed;
32 int failed;
33 } unittest_results;
34
35 #define unittest(result, fmt, ...) ({ \
36 bool failed = !(result); \
37 if (failed) { \
38 unittest_results.failed++; \
39 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
40 } else { \
41 unittest_results.passed++; \
42 pr_debug("pass %s():%i\n", __func__, __LINE__); \
43 } \
44 failed; \
45 })
46
47 static void __init of_unittest_find_node_by_name(void)
48 {
49 struct device_node *np;
50 const char *options, *name;
51
52 np = of_find_node_by_path("/testcase-data");
53 name = kasprintf(GFP_KERNEL, "%pOF", np);
54 unittest(np && !strcmp("/testcase-data", name),
55 "find /testcase-data failed\n");
56 of_node_put(np);
57 kfree(name);
58
59 /* Test if trailing '/' works */
60 np = of_find_node_by_path("/testcase-data/");
61 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
62
63 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
64 name = kasprintf(GFP_KERNEL, "%pOF", np);
65 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
66 "find /testcase-data/phandle-tests/consumer-a failed\n");
67 of_node_put(np);
68 kfree(name);
69
70 np = of_find_node_by_path("testcase-alias");
71 name = kasprintf(GFP_KERNEL, "%pOF", np);
72 unittest(np && !strcmp("/testcase-data", name),
73 "find testcase-alias failed\n");
74 of_node_put(np);
75 kfree(name);
76
77 /* Test if trailing '/' works on aliases */
78 np = of_find_node_by_path("testcase-alias/");
79 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
80
81 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
82 name = kasprintf(GFP_KERNEL, "%pOF", np);
83 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
84 "find testcase-alias/phandle-tests/consumer-a failed\n");
85 of_node_put(np);
86 kfree(name);
87
88 np = of_find_node_by_path("/testcase-data/missing-path");
89 unittest(!np, "non-existent path returned node %pOF\n", np);
90 of_node_put(np);
91
92 np = of_find_node_by_path("missing-alias");
93 unittest(!np, "non-existent alias returned node %pOF\n", np);
94 of_node_put(np);
95
96 np = of_find_node_by_path("testcase-alias/missing-path");
97 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
98 of_node_put(np);
99
100 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
101 unittest(np && !strcmp("testoption", options),
102 "option path test failed\n");
103 of_node_put(np);
104
105 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
106 unittest(np && !strcmp("test/option", options),
107 "option path test, subcase #1 failed\n");
108 of_node_put(np);
109
110 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
111 unittest(np && !strcmp("test/option", options),
112 "option path test, subcase #2 failed\n");
113 of_node_put(np);
114
115 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
116 unittest(np, "NULL option path test failed\n");
117 of_node_put(np);
118
119 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
120 &options);
121 unittest(np && !strcmp("testaliasoption", options),
122 "option alias path test failed\n");
123 of_node_put(np);
124
125 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
126 &options);
127 unittest(np && !strcmp("test/alias/option", options),
128 "option alias path test, subcase #1 failed\n");
129 of_node_put(np);
130
131 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
132 unittest(np, "NULL option alias path test failed\n");
133 of_node_put(np);
134
135 options = "testoption";
136 np = of_find_node_opts_by_path("testcase-alias", &options);
137 unittest(np && !options, "option clearing test failed\n");
138 of_node_put(np);
139
140 options = "testoption";
141 np = of_find_node_opts_by_path("/", &options);
142 unittest(np && !options, "option clearing root node test failed\n");
143 of_node_put(np);
144 }
145
146 static void __init of_unittest_dynamic(void)
147 {
148 struct device_node *np;
149 struct property *prop;
150
151 np = of_find_node_by_path("/testcase-data");
152 if (!np) {
153 pr_err("missing testcase data\n");
154 return;
155 }
156
157 /* Array of 4 properties for the purpose of testing */
158 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
159 if (!prop) {
160 unittest(0, "kzalloc() failed\n");
161 return;
162 }
163
164 /* Add a new property - should pass*/
165 prop->name = "new-property";
166 prop->value = "new-property-data";
167 prop->length = strlen(prop->value) + 1;
168 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
169
170 /* Try to add an existing property - should fail */
171 prop++;
172 prop->name = "new-property";
173 prop->value = "new-property-data-should-fail";
174 prop->length = strlen(prop->value) + 1;
175 unittest(of_add_property(np, prop) != 0,
176 "Adding an existing property should have failed\n");
177
178 /* Try to modify an existing property - should pass */
179 prop->value = "modify-property-data-should-pass";
180 prop->length = strlen(prop->value) + 1;
181 unittest(of_update_property(np, prop) == 0,
182 "Updating an existing property should have passed\n");
183
184 /* Try to modify non-existent property - should pass*/
185 prop++;
186 prop->name = "modify-property";
187 prop->value = "modify-missing-property-data-should-pass";
188 prop->length = strlen(prop->value) + 1;
189 unittest(of_update_property(np, prop) == 0,
190 "Updating a missing property should have passed\n");
191
192 /* Remove property - should pass */
193 unittest(of_remove_property(np, prop) == 0,
194 "Removing a property should have passed\n");
195
196 /* Adding very large property - should pass */
197 prop++;
198 prop->name = "large-property-PAGE_SIZEx8";
199 prop->length = PAGE_SIZE * 8;
200 prop->value = kzalloc(prop->length, GFP_KERNEL);
201 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
202 if (prop->value)
203 unittest(of_add_property(np, prop) == 0,
204 "Adding a large property should have passed\n");
205 }
206
207 static int __init of_unittest_check_node_linkage(struct device_node *np)
208 {
209 struct device_node *child;
210 int count = 0, rc;
211
212 for_each_child_of_node(np, child) {
213 if (child->parent != np) {
214 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
215 child, np);
216 rc = -EINVAL;
217 goto put_child;
218 }
219
220 rc = of_unittest_check_node_linkage(child);
221 if (rc < 0)
222 goto put_child;
223 count += rc;
224 }
225
226 return count + 1;
227 put_child:
228 of_node_put(child);
229 return rc;
230 }
231
232 static void __init of_unittest_check_tree_linkage(void)
233 {
234 struct device_node *np;
235 int allnode_count = 0, child_count;
236
237 if (!of_root)
238 return;
239
240 for_each_of_allnodes(np)
241 allnode_count++;
242 child_count = of_unittest_check_node_linkage(of_root);
243
244 unittest(child_count > 0, "Device node data structure is corrupted\n");
245 unittest(child_count == allnode_count,
246 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
247 allnode_count, child_count);
248 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
249 }
250
251 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
252 const char *expected)
253 {
254 unsigned char buf[strlen(expected)+10];
255 int size, i;
256
257 /* Baseline; check conversion with a large size limit */
258 memset(buf, 0xff, sizeof(buf));
259 size = snprintf(buf, sizeof(buf) - 2, fmt, np);
260
261 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
262 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
263 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
264 fmt, expected, buf);
265
266 /* Make sure length limits work */
267 size++;
268 for (i = 0; i < 2; i++, size--) {
269 /* Clear the buffer, and make sure it works correctly still */
270 memset(buf, 0xff, sizeof(buf));
271 snprintf(buf, size+1, fmt, np);
272 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
273 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
274 size, fmt, expected, buf);
275 }
276 }
277
278 static void __init of_unittest_printf(void)
279 {
280 struct device_node *np;
281 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
282 char phandle_str[16] = "";
283
284 np = of_find_node_by_path(full_name);
285 if (!np) {
286 unittest(np, "testcase data missing\n");
287 return;
288 }
289
290 num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
291
292 of_unittest_printf_one(np, "%pOF", full_name);
293 of_unittest_printf_one(np, "%pOFf", full_name);
294 of_unittest_printf_one(np, "%pOFp", phandle_str);
295 of_unittest_printf_one(np, "%pOFP", "dev@100");
296 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
297 of_unittest_printf_one(np, "%10pOFP", " dev@100");
298 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
299 of_unittest_printf_one(of_root, "%pOFP", "/");
300 of_unittest_printf_one(np, "%pOFF", "----");
301 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
302 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
303 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
304 of_unittest_printf_one(np, "%pOFC",
305 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
306 }
307
308 struct node_hash {
309 struct hlist_node node;
310 struct device_node *np;
311 };
312
313 static DEFINE_HASHTABLE(phandle_ht, 8);
314 static void __init of_unittest_check_phandles(void)
315 {
316 struct device_node *np;
317 struct node_hash *nh;
318 struct hlist_node *tmp;
319 int i, dup_count = 0, phandle_count = 0;
320
321 for_each_of_allnodes(np) {
322 if (!np->phandle)
323 continue;
324
325 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
326 if (nh->np->phandle == np->phandle) {
327 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
328 np->phandle, nh->np, np);
329 dup_count++;
330 break;
331 }
332 }
333
334 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
335 if (WARN_ON(!nh))
336 return;
337
338 nh->np = np;
339 hash_add(phandle_ht, &nh->node, np->phandle);
340 phandle_count++;
341 }
342 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
343 dup_count, phandle_count);
344
345 /* Clean up */
346 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
347 hash_del(&nh->node);
348 kfree(nh);
349 }
350 }
351
352 static void __init of_unittest_parse_phandle_with_args(void)
353 {
354 struct device_node *np;
355 struct of_phandle_args args;
356 int i, rc;
357
358 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
359 if (!np) {
360 pr_err("missing testcase data\n");
361 return;
362 }
363
364 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
365 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
366
367 for (i = 0; i < 8; i++) {
368 bool passed = true;
369
370 memset(&args, 0, sizeof(args));
371 rc = of_parse_phandle_with_args(np, "phandle-list",
372 "#phandle-cells", i, &args);
373
374 /* Test the values from tests-phandle.dtsi */
375 switch (i) {
376 case 0:
377 passed &= !rc;
378 passed &= (args.args_count == 1);
379 passed &= (args.args[0] == (i + 1));
380 break;
381 case 1:
382 passed &= !rc;
383 passed &= (args.args_count == 2);
384 passed &= (args.args[0] == (i + 1));
385 passed &= (args.args[1] == 0);
386 break;
387 case 2:
388 passed &= (rc == -ENOENT);
389 break;
390 case 3:
391 passed &= !rc;
392 passed &= (args.args_count == 3);
393 passed &= (args.args[0] == (i + 1));
394 passed &= (args.args[1] == 4);
395 passed &= (args.args[2] == 3);
396 break;
397 case 4:
398 passed &= !rc;
399 passed &= (args.args_count == 2);
400 passed &= (args.args[0] == (i + 1));
401 passed &= (args.args[1] == 100);
402 break;
403 case 5:
404 passed &= !rc;
405 passed &= (args.args_count == 0);
406 break;
407 case 6:
408 passed &= !rc;
409 passed &= (args.args_count == 1);
410 passed &= (args.args[0] == (i + 1));
411 break;
412 case 7:
413 passed &= (rc == -ENOENT);
414 break;
415 default:
416 passed = false;
417 }
418
419 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
420 i, args.np, rc);
421 }
422
423 /* Check for missing list property */
424 memset(&args, 0, sizeof(args));
425 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
426 "#phandle-cells", 0, &args);
427 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
428 rc = of_count_phandle_with_args(np, "phandle-list-missing",
429 "#phandle-cells");
430 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
431
432 /* Check for missing cells property */
433 memset(&args, 0, sizeof(args));
434 rc = of_parse_phandle_with_args(np, "phandle-list",
435 "#phandle-cells-missing", 0, &args);
436 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
437 rc = of_count_phandle_with_args(np, "phandle-list",
438 "#phandle-cells-missing");
439 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
440
441 /* Check for bad phandle in list */
442 memset(&args, 0, sizeof(args));
443 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
444 "#phandle-cells", 0, &args);
445 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
446 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
447 "#phandle-cells");
448 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
449
450 /* Check for incorrectly formed argument list */
451 memset(&args, 0, sizeof(args));
452 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
453 "#phandle-cells", 1, &args);
454 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
455 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
456 "#phandle-cells");
457 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
458 }
459
460 static void __init of_unittest_property_string(void)
461 {
462 const char *strings[4];
463 struct device_node *np;
464 int rc;
465
466 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
467 if (!np) {
468 pr_err("No testcase data in device tree\n");
469 return;
470 }
471
472 rc = of_property_match_string(np, "phandle-list-names", "first");
473 unittest(rc == 0, "first expected:0 got:%i\n", rc);
474 rc = of_property_match_string(np, "phandle-list-names", "second");
475 unittest(rc == 1, "second expected:1 got:%i\n", rc);
476 rc = of_property_match_string(np, "phandle-list-names", "third");
477 unittest(rc == 2, "third expected:2 got:%i\n", rc);
478 rc = of_property_match_string(np, "phandle-list-names", "fourth");
479 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
480 rc = of_property_match_string(np, "missing-property", "blah");
481 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
482 rc = of_property_match_string(np, "empty-property", "blah");
483 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
484 rc = of_property_match_string(np, "unterminated-string", "blah");
485 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
486
487 /* of_property_count_strings() tests */
488 rc = of_property_count_strings(np, "string-property");
489 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
490 rc = of_property_count_strings(np, "phandle-list-names");
491 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
492 rc = of_property_count_strings(np, "unterminated-string");
493 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
494 rc = of_property_count_strings(np, "unterminated-string-list");
495 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
496
497 /* of_property_read_string_index() tests */
498 rc = of_property_read_string_index(np, "string-property", 0, strings);
499 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
500 strings[0] = NULL;
501 rc = of_property_read_string_index(np, "string-property", 1, strings);
502 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
503 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
504 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
505 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
506 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
507 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
508 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
509 strings[0] = NULL;
510 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
511 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
512 strings[0] = NULL;
513 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
514 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
515 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
516 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
517 strings[0] = NULL;
518 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
519 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
520 strings[1] = NULL;
521
522 /* of_property_read_string_array() tests */
523 rc = of_property_read_string_array(np, "string-property", strings, 4);
524 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
525 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
526 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
527 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
528 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
529 /* -- An incorrectly formed string should cause a failure */
530 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
531 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
532 /* -- parsing the correctly formed strings should still work: */
533 strings[2] = NULL;
534 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
535 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
536 strings[1] = NULL;
537 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
538 unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
539 }
540
541 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
542 (p1)->value && (p2)->value && \
543 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
544 !strcmp((p1)->name, (p2)->name))
545 static void __init of_unittest_property_copy(void)
546 {
547 #ifdef CONFIG_OF_DYNAMIC
548 struct property p1 = { .name = "p1", .length = 0, .value = "" };
549 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
550 struct property *new;
551
552 new = __of_prop_dup(&p1, GFP_KERNEL);
553 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
554 kfree(new->value);
555 kfree(new->name);
556 kfree(new);
557
558 new = __of_prop_dup(&p2, GFP_KERNEL);
559 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
560 kfree(new->value);
561 kfree(new->name);
562 kfree(new);
563 #endif
564 }
565
566 static void __init of_unittest_changeset(void)
567 {
568 #ifdef CONFIG_OF_DYNAMIC
569 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
570 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
571 struct property *ppremove;
572 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
573 struct of_changeset chgset;
574
575 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
576 unittest(n1, "testcase setup failure\n");
577 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
578 unittest(n2, "testcase setup failure\n");
579 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
580 unittest(n21, "testcase setup failure %p\n", n21);
581 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
582 unittest(nremove, "testcase setup failure\n");
583 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
584 unittest(ppadd, "testcase setup failure\n");
585 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
586 unittest(ppupdate, "testcase setup failure\n");
587 parent = nremove->parent;
588 n1->parent = parent;
589 n2->parent = parent;
590 n21->parent = n2;
591 n2->child = n21;
592 ppremove = of_find_property(parent, "prop-remove", NULL);
593 unittest(ppremove, "failed to find removal prop");
594
595 of_changeset_init(&chgset);
596 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
597 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
598 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
599 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
600 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
601 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
602 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
603 unittest(!of_changeset_apply(&chgset), "apply failed\n");
604
605 /* Make sure node names are constructed correctly */
606 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
607 "'%pOF' not added\n", n21);
608 of_node_put(np);
609
610 unittest(!of_changeset_revert(&chgset), "revert failed\n");
611
612 of_changeset_destroy(&chgset);
613 #endif
614 }
615
616 static void __init of_unittest_parse_interrupts(void)
617 {
618 struct device_node *np;
619 struct of_phandle_args args;
620 int i, rc;
621
622 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
623 return;
624
625 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
626 if (!np) {
627 pr_err("missing testcase data\n");
628 return;
629 }
630
631 for (i = 0; i < 4; i++) {
632 bool passed = true;
633
634 memset(&args, 0, sizeof(args));
635 rc = of_irq_parse_one(np, i, &args);
636
637 passed &= !rc;
638 passed &= (args.args_count == 1);
639 passed &= (args.args[0] == (i + 1));
640
641 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
642 i, args.np, rc);
643 }
644 of_node_put(np);
645
646 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
647 if (!np) {
648 pr_err("missing testcase data\n");
649 return;
650 }
651
652 for (i = 0; i < 4; i++) {
653 bool passed = true;
654
655 memset(&args, 0, sizeof(args));
656 rc = of_irq_parse_one(np, i, &args);
657
658 /* Test the values from tests-phandle.dtsi */
659 switch (i) {
660 case 0:
661 passed &= !rc;
662 passed &= (args.args_count == 1);
663 passed &= (args.args[0] == 9);
664 break;
665 case 1:
666 passed &= !rc;
667 passed &= (args.args_count == 3);
668 passed &= (args.args[0] == 10);
669 passed &= (args.args[1] == 11);
670 passed &= (args.args[2] == 12);
671 break;
672 case 2:
673 passed &= !rc;
674 passed &= (args.args_count == 2);
675 passed &= (args.args[0] == 13);
676 passed &= (args.args[1] == 14);
677 break;
678 case 3:
679 passed &= !rc;
680 passed &= (args.args_count == 2);
681 passed &= (args.args[0] == 15);
682 passed &= (args.args[1] == 16);
683 break;
684 default:
685 passed = false;
686 }
687 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
688 i, args.np, rc);
689 }
690 of_node_put(np);
691 }
692
693 static void __init of_unittest_parse_interrupts_extended(void)
694 {
695 struct device_node *np;
696 struct of_phandle_args args;
697 int i, rc;
698
699 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
700 return;
701
702 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
703 if (!np) {
704 pr_err("missing testcase data\n");
705 return;
706 }
707
708 for (i = 0; i < 7; i++) {
709 bool passed = true;
710
711 memset(&args, 0, sizeof(args));
712 rc = of_irq_parse_one(np, i, &args);
713
714 /* Test the values from tests-phandle.dtsi */
715 switch (i) {
716 case 0:
717 passed &= !rc;
718 passed &= (args.args_count == 1);
719 passed &= (args.args[0] == 1);
720 break;
721 case 1:
722 passed &= !rc;
723 passed &= (args.args_count == 3);
724 passed &= (args.args[0] == 2);
725 passed &= (args.args[1] == 3);
726 passed &= (args.args[2] == 4);
727 break;
728 case 2:
729 passed &= !rc;
730 passed &= (args.args_count == 2);
731 passed &= (args.args[0] == 5);
732 passed &= (args.args[1] == 6);
733 break;
734 case 3:
735 passed &= !rc;
736 passed &= (args.args_count == 1);
737 passed &= (args.args[0] == 9);
738 break;
739 case 4:
740 passed &= !rc;
741 passed &= (args.args_count == 3);
742 passed &= (args.args[0] == 10);
743 passed &= (args.args[1] == 11);
744 passed &= (args.args[2] == 12);
745 break;
746 case 5:
747 passed &= !rc;
748 passed &= (args.args_count == 2);
749 passed &= (args.args[0] == 13);
750 passed &= (args.args[1] == 14);
751 break;
752 case 6:
753 passed &= !rc;
754 passed &= (args.args_count == 1);
755 passed &= (args.args[0] == 15);
756 break;
757 default:
758 passed = false;
759 }
760
761 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
762 i, args.np, rc);
763 }
764 of_node_put(np);
765 }
766
767 static const struct of_device_id match_node_table[] = {
768 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
769 { .data = "B", .type = "type1", }, /* followed by type alone */
770
771 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
772 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
773 { .data = "Cc", .name = "name2", .type = "type2", },
774
775 { .data = "E", .compatible = "compat3" },
776 { .data = "G", .compatible = "compat2", },
777 { .data = "H", .compatible = "compat2", .name = "name5", },
778 { .data = "I", .compatible = "compat2", .type = "type1", },
779 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
780 { .data = "K", .compatible = "compat2", .name = "name9", },
781 {}
782 };
783
784 static struct {
785 const char *path;
786 const char *data;
787 } match_node_tests[] = {
788 { .path = "/testcase-data/match-node/name0", .data = "A", },
789 { .path = "/testcase-data/match-node/name1", .data = "B", },
790 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
791 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
792 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
793 { .path = "/testcase-data/match-node/name3", .data = "E", },
794 { .path = "/testcase-data/match-node/name4", .data = "G", },
795 { .path = "/testcase-data/match-node/name5", .data = "H", },
796 { .path = "/testcase-data/match-node/name6", .data = "G", },
797 { .path = "/testcase-data/match-node/name7", .data = "I", },
798 { .path = "/testcase-data/match-node/name8", .data = "J", },
799 { .path = "/testcase-data/match-node/name9", .data = "K", },
800 };
801
802 static void __init of_unittest_match_node(void)
803 {
804 struct device_node *np;
805 const struct of_device_id *match;
806 int i;
807
808 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
809 np = of_find_node_by_path(match_node_tests[i].path);
810 if (!np) {
811 unittest(0, "missing testcase node %s\n",
812 match_node_tests[i].path);
813 continue;
814 }
815
816 match = of_match_node(match_node_table, np);
817 if (!match) {
818 unittest(0, "%s didn't match anything\n",
819 match_node_tests[i].path);
820 continue;
821 }
822
823 if (strcmp(match->data, match_node_tests[i].data) != 0) {
824 unittest(0, "%s got wrong match. expected %s, got %s\n",
825 match_node_tests[i].path, match_node_tests[i].data,
826 (const char *)match->data);
827 continue;
828 }
829 unittest(1, "passed");
830 }
831 }
832
833 static struct resource test_bus_res = {
834 .start = 0xfffffff8,
835 .end = 0xfffffff9,
836 .flags = IORESOURCE_MEM,
837 };
838 static const struct platform_device_info test_bus_info = {
839 .name = "unittest-bus",
840 };
841 static void __init of_unittest_platform_populate(void)
842 {
843 int irq, rc;
844 struct device_node *np, *child, *grandchild;
845 struct platform_device *pdev, *test_bus;
846 const struct of_device_id match[] = {
847 { .compatible = "test-device", },
848 {}
849 };
850
851 np = of_find_node_by_path("/testcase-data");
852 of_platform_default_populate(np, NULL, NULL);
853
854 /* Test that a missing irq domain returns -EPROBE_DEFER */
855 np = of_find_node_by_path("/testcase-data/testcase-device1");
856 pdev = of_find_device_by_node(np);
857 unittest(pdev, "device 1 creation failed\n");
858
859 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
860 irq = platform_get_irq(pdev, 0);
861 unittest(irq == -EPROBE_DEFER,
862 "device deferred probe failed - %d\n", irq);
863
864 /* Test that a parsing failure does not return -EPROBE_DEFER */
865 np = of_find_node_by_path("/testcase-data/testcase-device2");
866 pdev = of_find_device_by_node(np);
867 unittest(pdev, "device 2 creation failed\n");
868 irq = platform_get_irq(pdev, 0);
869 unittest(irq < 0 && irq != -EPROBE_DEFER,
870 "device parsing error failed - %d\n", irq);
871 }
872
873 np = of_find_node_by_path("/testcase-data/platform-tests");
874 unittest(np, "No testcase data in device tree\n");
875 if (!np)
876 return;
877
878 test_bus = platform_device_register_full(&test_bus_info);
879 rc = PTR_ERR_OR_ZERO(test_bus);
880 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
881 if (rc)
882 return;
883 test_bus->dev.of_node = np;
884
885 /*
886 * Add a dummy resource to the test bus node after it is
887 * registered to catch problems with un-inserted resources. The
888 * DT code doesn't insert the resources, and it has caused the
889 * kernel to oops in the past. This makes sure the same bug
890 * doesn't crop up again.
891 */
892 platform_device_add_resources(test_bus, &test_bus_res, 1);
893
894 of_platform_populate(np, match, NULL, &test_bus->dev);
895 for_each_child_of_node(np, child) {
896 for_each_child_of_node(child, grandchild)
897 unittest(of_find_device_by_node(grandchild),
898 "Could not create device for node '%pOFn'\n",
899 grandchild);
900 }
901
902 of_platform_depopulate(&test_bus->dev);
903 for_each_child_of_node(np, child) {
904 for_each_child_of_node(child, grandchild)
905 unittest(!of_find_device_by_node(grandchild),
906 "device didn't get destroyed '%pOFn'\n",
907 grandchild);
908 }
909
910 platform_device_unregister(test_bus);
911 of_node_put(np);
912 }
913
914 /**
915 * update_node_properties - adds the properties
916 * of np into dup node (present in live tree) and
917 * updates parent of children of np to dup.
918 *
919 * @np: node whose properties are being added to the live tree
920 * @dup: node present in live tree to be updated
921 */
922 static void update_node_properties(struct device_node *np,
923 struct device_node *dup)
924 {
925 struct property *prop;
926 struct property *save_next;
927 struct device_node *child;
928 int ret;
929
930 for_each_child_of_node(np, child)
931 child->parent = dup;
932
933 /*
934 * "unittest internal error: unable to add testdata property"
935 *
936 * If this message reports a property in node '/__symbols__' then
937 * the respective unittest overlay contains a label that has the
938 * same name as a label in the live devicetree. The label will
939 * be in the live devicetree only if the devicetree source was
940 * compiled with the '-@' option. If you encounter this error,
941 * please consider renaming __all__ of the labels in the unittest
942 * overlay dts files with an odd prefix that is unlikely to be
943 * used in a real devicetree.
944 */
945
946 /*
947 * open code for_each_property_of_node() because of_add_property()
948 * sets prop->next to NULL
949 */
950 for (prop = np->properties; prop != NULL; prop = save_next) {
951 save_next = prop->next;
952 ret = of_add_property(dup, prop);
953 if (ret)
954 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
955 np, prop->name);
956 }
957 }
958
959 /**
960 * attach_node_and_children - attaches nodes
961 * and its children to live tree
962 *
963 * @np: Node to attach to live tree
964 */
965 static void attach_node_and_children(struct device_node *np)
966 {
967 struct device_node *next, *dup, *child;
968 unsigned long flags;
969 const char *full_name;
970
971 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
972
973 if (!strcmp(full_name, "/__local_fixups__") ||
974 !strcmp(full_name, "/__fixups__")) {
975 kfree(full_name);
976 return;
977 }
978
979 dup = of_find_node_by_path(full_name);
980 kfree(full_name);
981 if (dup) {
982 update_node_properties(np, dup);
983 return;
984 }
985
986 child = np->child;
987 np->child = NULL;
988
989 mutex_lock(&of_mutex);
990 raw_spin_lock_irqsave(&devtree_lock, flags);
991 np->sibling = np->parent->child;
992 np->parent->child = np;
993 of_node_clear_flag(np, OF_DETACHED);
994 raw_spin_unlock_irqrestore(&devtree_lock, flags);
995
996 __of_attach_node_sysfs(np);
997 mutex_unlock(&of_mutex);
998
999 while (child) {
1000 next = child->sibling;
1001 attach_node_and_children(child);
1002 child = next;
1003 }
1004 }
1005
1006 /**
1007 * unittest_data_add - Reads, copies data from
1008 * linked tree and attaches it to the live tree
1009 */
1010 static int __init unittest_data_add(void)
1011 {
1012 void *unittest_data;
1013 struct device_node *unittest_data_node, *np;
1014 /*
1015 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1016 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1017 */
1018 extern uint8_t __dtb_testcases_begin[];
1019 extern uint8_t __dtb_testcases_end[];
1020 const int size = __dtb_testcases_end - __dtb_testcases_begin;
1021 int rc;
1022
1023 if (!size) {
1024 pr_warn("%s: No testcase data to attach; not running tests\n",
1025 __func__);
1026 return -ENODATA;
1027 }
1028
1029 /* creating copy */
1030 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
1031
1032 if (!unittest_data) {
1033 pr_warn("%s: Failed to allocate memory for unittest_data; "
1034 "not running tests\n", __func__);
1035 return -ENOMEM;
1036 }
1037 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
1038 if (!unittest_data_node) {
1039 pr_warn("%s: No tree to attach; not running tests\n", __func__);
1040 kfree(unittest_data);
1041 return -ENODATA;
1042 }
1043
1044 /*
1045 * This lock normally encloses of_overlay_apply() as well as
1046 * of_resolve_phandles().
1047 */
1048 of_overlay_mutex_lock();
1049
1050 rc = of_resolve_phandles(unittest_data_node);
1051 if (rc) {
1052 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1053 of_overlay_mutex_unlock();
1054 return -EINVAL;
1055 }
1056
1057 if (!of_root) {
1058 of_root = unittest_data_node;
1059 for_each_of_allnodes(np)
1060 __of_attach_node_sysfs(np);
1061 of_aliases = of_find_node_by_path("/aliases");
1062 of_chosen = of_find_node_by_path("/chosen");
1063 of_overlay_mutex_unlock();
1064 return 0;
1065 }
1066
1067 /* attach the sub-tree to live tree */
1068 np = unittest_data_node->child;
1069 while (np) {
1070 struct device_node *next = np->sibling;
1071
1072 np->parent = of_root;
1073 attach_node_and_children(np);
1074 np = next;
1075 }
1076
1077 of_overlay_mutex_unlock();
1078
1079 return 0;
1080 }
1081
1082 #ifdef CONFIG_OF_OVERLAY
1083
1084 static int unittest_probe(struct platform_device *pdev)
1085 {
1086 struct device *dev = &pdev->dev;
1087 struct device_node *np = dev->of_node;
1088
1089 if (np == NULL) {
1090 dev_err(dev, "No OF data for device\n");
1091 return -EINVAL;
1092
1093 }
1094
1095 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1096
1097 of_platform_populate(np, NULL, NULL, &pdev->dev);
1098
1099 return 0;
1100 }
1101
1102 static int unittest_remove(struct platform_device *pdev)
1103 {
1104 struct device *dev = &pdev->dev;
1105 struct device_node *np = dev->of_node;
1106
1107 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1108 return 0;
1109 }
1110
1111 static const struct of_device_id unittest_match[] = {
1112 { .compatible = "unittest", },
1113 {},
1114 };
1115
1116 static struct platform_driver unittest_driver = {
1117 .probe = unittest_probe,
1118 .remove = unittest_remove,
1119 .driver = {
1120 .name = "unittest",
1121 .of_match_table = of_match_ptr(unittest_match),
1122 },
1123 };
1124
1125 /* get the platform device instantiated at the path */
1126 static struct platform_device *of_path_to_platform_device(const char *path)
1127 {
1128 struct device_node *np;
1129 struct platform_device *pdev;
1130
1131 np = of_find_node_by_path(path);
1132 if (np == NULL)
1133 return NULL;
1134
1135 pdev = of_find_device_by_node(np);
1136 of_node_put(np);
1137
1138 return pdev;
1139 }
1140
1141 /* find out if a platform device exists at that path */
1142 static int of_path_platform_device_exists(const char *path)
1143 {
1144 struct platform_device *pdev;
1145
1146 pdev = of_path_to_platform_device(path);
1147 platform_device_put(pdev);
1148 return pdev != NULL;
1149 }
1150
1151 #if IS_BUILTIN(CONFIG_I2C)
1152
1153 /* get the i2c client device instantiated at the path */
1154 static struct i2c_client *of_path_to_i2c_client(const char *path)
1155 {
1156 struct device_node *np;
1157 struct i2c_client *client;
1158
1159 np = of_find_node_by_path(path);
1160 if (np == NULL)
1161 return NULL;
1162
1163 client = of_find_i2c_device_by_node(np);
1164 of_node_put(np);
1165
1166 return client;
1167 }
1168
1169 /* find out if a i2c client device exists at that path */
1170 static int of_path_i2c_client_exists(const char *path)
1171 {
1172 struct i2c_client *client;
1173
1174 client = of_path_to_i2c_client(path);
1175 if (client)
1176 put_device(&client->dev);
1177 return client != NULL;
1178 }
1179 #else
1180 static int of_path_i2c_client_exists(const char *path)
1181 {
1182 return 0;
1183 }
1184 #endif
1185
1186 enum overlay_type {
1187 PDEV_OVERLAY,
1188 I2C_OVERLAY
1189 };
1190
1191 static int of_path_device_type_exists(const char *path,
1192 enum overlay_type ovtype)
1193 {
1194 switch (ovtype) {
1195 case PDEV_OVERLAY:
1196 return of_path_platform_device_exists(path);
1197 case I2C_OVERLAY:
1198 return of_path_i2c_client_exists(path);
1199 }
1200 return 0;
1201 }
1202
1203 static const char *unittest_path(int nr, enum overlay_type ovtype)
1204 {
1205 const char *base;
1206 static char buf[256];
1207
1208 switch (ovtype) {
1209 case PDEV_OVERLAY:
1210 base = "/testcase-data/overlay-node/test-bus";
1211 break;
1212 case I2C_OVERLAY:
1213 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1214 break;
1215 default:
1216 buf[0] = '\0';
1217 return buf;
1218 }
1219 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1220 buf[sizeof(buf) - 1] = '\0';
1221 return buf;
1222 }
1223
1224 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1225 {
1226 const char *path;
1227
1228 path = unittest_path(unittest_nr, ovtype);
1229
1230 switch (ovtype) {
1231 case PDEV_OVERLAY:
1232 return of_path_platform_device_exists(path);
1233 case I2C_OVERLAY:
1234 return of_path_i2c_client_exists(path);
1235 }
1236 return 0;
1237 }
1238
1239 static const char *overlay_path(int nr)
1240 {
1241 static char buf[256];
1242
1243 snprintf(buf, sizeof(buf) - 1,
1244 "/testcase-data/overlay%d", nr);
1245 buf[sizeof(buf) - 1] = '\0';
1246
1247 return buf;
1248 }
1249
1250 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1251
1252 /* it is guaranteed that overlay ids are assigned in sequence */
1253 #define MAX_UNITTEST_OVERLAYS 256
1254 static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1255 static int overlay_first_id = -1;
1256
1257 static void of_unittest_track_overlay(int id)
1258 {
1259 if (overlay_first_id < 0)
1260 overlay_first_id = id;
1261 id -= overlay_first_id;
1262
1263 /* we shouldn't need that many */
1264 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1265 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1266 }
1267
1268 static void of_unittest_untrack_overlay(int id)
1269 {
1270 if (overlay_first_id < 0)
1271 return;
1272 id -= overlay_first_id;
1273 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1274 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1275 }
1276
1277 static void of_unittest_destroy_tracked_overlays(void)
1278 {
1279 int id, ret, defers, ovcs_id;
1280
1281 if (overlay_first_id < 0)
1282 return;
1283
1284 /* try until no defers */
1285 do {
1286 defers = 0;
1287 /* remove in reverse order */
1288 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1289 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1290 continue;
1291
1292 ovcs_id = id + overlay_first_id;
1293 ret = of_overlay_remove(&ovcs_id);
1294 if (ret == -ENODEV) {
1295 pr_warn("%s: no overlay to destroy for #%d\n",
1296 __func__, id + overlay_first_id);
1297 continue;
1298 }
1299 if (ret != 0) {
1300 defers++;
1301 pr_warn("%s: overlay destroy failed for #%d\n",
1302 __func__, id + overlay_first_id);
1303 continue;
1304 }
1305
1306 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1307 }
1308 } while (defers > 0);
1309 }
1310
1311 static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
1312 int *overlay_id)
1313 {
1314 struct device_node *np = NULL;
1315 int ret;
1316
1317 np = of_find_node_by_path(overlay_path(overlay_nr));
1318 if (np == NULL) {
1319 unittest(0, "could not find overlay node @\"%s\"\n",
1320 overlay_path(overlay_nr));
1321 ret = -EINVAL;
1322 goto out;
1323 }
1324
1325 *overlay_id = 0;
1326 ret = of_overlay_apply(np, overlay_id);
1327 if (ret < 0) {
1328 unittest(0, "could not create overlay from \"%s\"\n",
1329 overlay_path(overlay_nr));
1330 goto out;
1331 }
1332 of_unittest_track_overlay(*overlay_id);
1333
1334 ret = 0;
1335
1336 out:
1337 of_node_put(np);
1338
1339 return ret;
1340 }
1341
1342 /* apply an overlay while checking before and after states */
1343 static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
1344 int before, int after, enum overlay_type ovtype)
1345 {
1346 int ret, ovcs_id;
1347
1348 /* unittest device must not be in before state */
1349 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1350 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1351 overlay_path(overlay_nr),
1352 unittest_path(unittest_nr, ovtype),
1353 !before ? "enabled" : "disabled");
1354 return -EINVAL;
1355 }
1356
1357 ovcs_id = 0;
1358 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
1359 if (ret != 0) {
1360 /* of_unittest_apply_overlay already called unittest() */
1361 return ret;
1362 }
1363
1364 /* unittest device must be to set to after state */
1365 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1366 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1367 overlay_path(overlay_nr),
1368 unittest_path(unittest_nr, ovtype),
1369 !after ? "enabled" : "disabled");
1370 return -EINVAL;
1371 }
1372
1373 return 0;
1374 }
1375
1376 /* apply an overlay and then revert it while checking before, after states */
1377 static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1378 int unittest_nr, int before, int after,
1379 enum overlay_type ovtype)
1380 {
1381 int ret, ovcs_id;
1382
1383 /* unittest device must be in before state */
1384 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1385 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1386 overlay_path(overlay_nr),
1387 unittest_path(unittest_nr, ovtype),
1388 !before ? "enabled" : "disabled");
1389 return -EINVAL;
1390 }
1391
1392 /* apply the overlay */
1393 ovcs_id = 0;
1394 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
1395 if (ret != 0) {
1396 /* of_unittest_apply_overlay already called unittest() */
1397 return ret;
1398 }
1399
1400 /* unittest device must be in after state */
1401 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1402 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1403 overlay_path(overlay_nr),
1404 unittest_path(unittest_nr, ovtype),
1405 !after ? "enabled" : "disabled");
1406 return -EINVAL;
1407 }
1408
1409 ret = of_overlay_remove(&ovcs_id);
1410 if (ret != 0) {
1411 unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1412 overlay_path(overlay_nr),
1413 unittest_path(unittest_nr, ovtype));
1414 return ret;
1415 }
1416
1417 /* unittest device must be again in before state */
1418 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1419 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1420 overlay_path(overlay_nr),
1421 unittest_path(unittest_nr, ovtype),
1422 !before ? "enabled" : "disabled");
1423 return -EINVAL;
1424 }
1425
1426 return 0;
1427 }
1428
1429 /* test activation of device */
1430 static void of_unittest_overlay_0(void)
1431 {
1432 int ret;
1433
1434 /* device should enable */
1435 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1436 if (ret != 0)
1437 return;
1438
1439 unittest(1, "overlay test %d passed\n", 0);
1440 }
1441
1442 /* test deactivation of device */
1443 static void of_unittest_overlay_1(void)
1444 {
1445 int ret;
1446
1447 /* device should disable */
1448 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1449 if (ret != 0)
1450 return;
1451
1452 unittest(1, "overlay test %d passed\n", 1);
1453 }
1454
1455 /* test activation of device */
1456 static void of_unittest_overlay_2(void)
1457 {
1458 int ret;
1459
1460 /* device should enable */
1461 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1462 if (ret != 0)
1463 return;
1464
1465 unittest(1, "overlay test %d passed\n", 2);
1466 }
1467
1468 /* test deactivation of device */
1469 static void of_unittest_overlay_3(void)
1470 {
1471 int ret;
1472
1473 /* device should disable */
1474 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1475 if (ret != 0)
1476 return;
1477
1478 unittest(1, "overlay test %d passed\n", 3);
1479 }
1480
1481 /* test activation of a full device node */
1482 static void of_unittest_overlay_4(void)
1483 {
1484 int ret;
1485
1486 /* device should disable */
1487 ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1488 if (ret != 0)
1489 return;
1490
1491 unittest(1, "overlay test %d passed\n", 4);
1492 }
1493
1494 /* test overlay apply/revert sequence */
1495 static void of_unittest_overlay_5(void)
1496 {
1497 int ret;
1498
1499 /* device should disable */
1500 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1501 if (ret != 0)
1502 return;
1503
1504 unittest(1, "overlay test %d passed\n", 5);
1505 }
1506
1507 /* test overlay application in sequence */
1508 static void of_unittest_overlay_6(void)
1509 {
1510 struct device_node *np;
1511 int ret, i, ov_id[2], ovcs_id;
1512 int overlay_nr = 6, unittest_nr = 6;
1513 int before = 0, after = 1;
1514
1515 /* unittest device must be in before state */
1516 for (i = 0; i < 2; i++) {
1517 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1518 != before) {
1519 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1520 overlay_path(overlay_nr + i),
1521 unittest_path(unittest_nr + i,
1522 PDEV_OVERLAY),
1523 !before ? "enabled" : "disabled");
1524 return;
1525 }
1526 }
1527
1528 /* apply the overlays */
1529 for (i = 0; i < 2; i++) {
1530
1531 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1532 if (np == NULL) {
1533 unittest(0, "could not find overlay node @\"%s\"\n",
1534 overlay_path(overlay_nr + i));
1535 return;
1536 }
1537
1538 ovcs_id = 0;
1539 ret = of_overlay_apply(np, &ovcs_id);
1540 if (ret < 0) {
1541 unittest(0, "could not create overlay from \"%s\"\n",
1542 overlay_path(overlay_nr + i));
1543 return;
1544 }
1545 ov_id[i] = ovcs_id;
1546 of_unittest_track_overlay(ov_id[i]);
1547 }
1548
1549 for (i = 0; i < 2; i++) {
1550 /* unittest device must be in after state */
1551 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1552 != after) {
1553 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1554 overlay_path(overlay_nr + i),
1555 unittest_path(unittest_nr + i,
1556 PDEV_OVERLAY),
1557 !after ? "enabled" : "disabled");
1558 return;
1559 }
1560 }
1561
1562 for (i = 1; i >= 0; i--) {
1563 ovcs_id = ov_id[i];
1564 ret = of_overlay_remove(&ovcs_id);
1565 if (ret != 0) {
1566 unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1567 overlay_path(overlay_nr + i),
1568 unittest_path(unittest_nr + i,
1569 PDEV_OVERLAY));
1570 return;
1571 }
1572 of_unittest_untrack_overlay(ov_id[i]);
1573 }
1574
1575 for (i = 0; i < 2; i++) {
1576 /* unittest device must be again in before state */
1577 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1578 != before) {
1579 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1580 overlay_path(overlay_nr + i),
1581 unittest_path(unittest_nr + i,
1582 PDEV_OVERLAY),
1583 !before ? "enabled" : "disabled");
1584 return;
1585 }
1586 }
1587
1588 unittest(1, "overlay test %d passed\n", 6);
1589 }
1590
1591 /* test overlay application in sequence */
1592 static void of_unittest_overlay_8(void)
1593 {
1594 struct device_node *np;
1595 int ret, i, ov_id[2], ovcs_id;
1596 int overlay_nr = 8, unittest_nr = 8;
1597
1598 /* we don't care about device state in this test */
1599
1600 /* apply the overlays */
1601 for (i = 0; i < 2; i++) {
1602
1603 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1604 if (np == NULL) {
1605 unittest(0, "could not find overlay node @\"%s\"\n",
1606 overlay_path(overlay_nr + i));
1607 return;
1608 }
1609
1610 ovcs_id = 0;
1611 ret = of_overlay_apply(np, &ovcs_id);
1612 if (ret < 0) {
1613 unittest(0, "could not create overlay from \"%s\"\n",
1614 overlay_path(overlay_nr + i));
1615 return;
1616 }
1617 ov_id[i] = ovcs_id;
1618 of_unittest_track_overlay(ov_id[i]);
1619 }
1620
1621 /* now try to remove first overlay (it should fail) */
1622 ovcs_id = ov_id[0];
1623 ret = of_overlay_remove(&ovcs_id);
1624 if (ret == 0) {
1625 unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1626 overlay_path(overlay_nr + 0),
1627 unittest_path(unittest_nr,
1628 PDEV_OVERLAY));
1629 return;
1630 }
1631
1632 /* removing them in order should work */
1633 for (i = 1; i >= 0; i--) {
1634 ovcs_id = ov_id[i];
1635 ret = of_overlay_remove(&ovcs_id);
1636 if (ret != 0) {
1637 unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1638 overlay_path(overlay_nr + i),
1639 unittest_path(unittest_nr,
1640 PDEV_OVERLAY));
1641 return;
1642 }
1643 of_unittest_untrack_overlay(ov_id[i]);
1644 }
1645
1646 unittest(1, "overlay test %d passed\n", 8);
1647 }
1648
1649 /* test insertion of a bus with parent devices */
1650 static void of_unittest_overlay_10(void)
1651 {
1652 int ret;
1653 char *child_path;
1654
1655 /* device should disable */
1656 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1657 if (unittest(ret == 0,
1658 "overlay test %d failed; overlay application\n", 10))
1659 return;
1660
1661 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1662 unittest_path(10, PDEV_OVERLAY));
1663 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
1664 return;
1665
1666 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1667 kfree(child_path);
1668 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
1669 return;
1670 }
1671
1672 /* test insertion of a bus with parent devices (and revert) */
1673 static void of_unittest_overlay_11(void)
1674 {
1675 int ret;
1676
1677 /* device should disable */
1678 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1679 PDEV_OVERLAY);
1680 if (unittest(ret == 0,
1681 "overlay test %d failed; overlay application\n", 11))
1682 return;
1683 }
1684
1685 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1686
1687 struct unittest_i2c_bus_data {
1688 struct platform_device *pdev;
1689 struct i2c_adapter adap;
1690 };
1691
1692 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
1693 struct i2c_msg *msgs, int num)
1694 {
1695 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
1696
1697 (void)std;
1698
1699 return num;
1700 }
1701
1702 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
1703 {
1704 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1705 }
1706
1707 static const struct i2c_algorithm unittest_i2c_algo = {
1708 .master_xfer = unittest_i2c_master_xfer,
1709 .functionality = unittest_i2c_functionality,
1710 };
1711
1712 static int unittest_i2c_bus_probe(struct platform_device *pdev)
1713 {
1714 struct device *dev = &pdev->dev;
1715 struct device_node *np = dev->of_node;
1716 struct unittest_i2c_bus_data *std;
1717 struct i2c_adapter *adap;
1718 int ret;
1719
1720 if (np == NULL) {
1721 dev_err(dev, "No OF data for device\n");
1722 return -EINVAL;
1723
1724 }
1725
1726 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1727
1728 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1729 if (!std) {
1730 dev_err(dev, "Failed to allocate unittest i2c data\n");
1731 return -ENOMEM;
1732 }
1733
1734 /* link them together */
1735 std->pdev = pdev;
1736 platform_set_drvdata(pdev, std);
1737
1738 adap = &std->adap;
1739 i2c_set_adapdata(adap, std);
1740 adap->nr = -1;
1741 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1742 adap->class = I2C_CLASS_DEPRECATED;
1743 adap->algo = &unittest_i2c_algo;
1744 adap->dev.parent = dev;
1745 adap->dev.of_node = dev->of_node;
1746 adap->timeout = 5 * HZ;
1747 adap->retries = 3;
1748
1749 ret = i2c_add_numbered_adapter(adap);
1750 if (ret != 0) {
1751 dev_err(dev, "Failed to add I2C adapter\n");
1752 return ret;
1753 }
1754
1755 return 0;
1756 }
1757
1758 static int unittest_i2c_bus_remove(struct platform_device *pdev)
1759 {
1760 struct device *dev = &pdev->dev;
1761 struct device_node *np = dev->of_node;
1762 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
1763
1764 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1765 i2c_del_adapter(&std->adap);
1766
1767 return 0;
1768 }
1769
1770 static const struct of_device_id unittest_i2c_bus_match[] = {
1771 { .compatible = "unittest-i2c-bus", },
1772 {},
1773 };
1774
1775 static struct platform_driver unittest_i2c_bus_driver = {
1776 .probe = unittest_i2c_bus_probe,
1777 .remove = unittest_i2c_bus_remove,
1778 .driver = {
1779 .name = "unittest-i2c-bus",
1780 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
1781 },
1782 };
1783
1784 static int unittest_i2c_dev_probe(struct i2c_client *client,
1785 const struct i2c_device_id *id)
1786 {
1787 struct device *dev = &client->dev;
1788 struct device_node *np = client->dev.of_node;
1789
1790 if (!np) {
1791 dev_err(dev, "No OF node\n");
1792 return -EINVAL;
1793 }
1794
1795 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1796
1797 return 0;
1798 };
1799
1800 static int unittest_i2c_dev_remove(struct i2c_client *client)
1801 {
1802 struct device *dev = &client->dev;
1803 struct device_node *np = client->dev.of_node;
1804
1805 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1806 return 0;
1807 }
1808
1809 static const struct i2c_device_id unittest_i2c_dev_id[] = {
1810 { .name = "unittest-i2c-dev" },
1811 { }
1812 };
1813
1814 static struct i2c_driver unittest_i2c_dev_driver = {
1815 .driver = {
1816 .name = "unittest-i2c-dev",
1817 },
1818 .probe = unittest_i2c_dev_probe,
1819 .remove = unittest_i2c_dev_remove,
1820 .id_table = unittest_i2c_dev_id,
1821 };
1822
1823 #if IS_BUILTIN(CONFIG_I2C_MUX)
1824
1825 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
1826 {
1827 return 0;
1828 }
1829
1830 static int unittest_i2c_mux_probe(struct i2c_client *client,
1831 const struct i2c_device_id *id)
1832 {
1833 int ret, i, nchans;
1834 struct device *dev = &client->dev;
1835 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1836 struct device_node *np = client->dev.of_node, *child;
1837 struct i2c_mux_core *muxc;
1838 u32 reg, max_reg;
1839
1840 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1841
1842 if (!np) {
1843 dev_err(dev, "No OF node\n");
1844 return -EINVAL;
1845 }
1846
1847 max_reg = (u32)-1;
1848 for_each_child_of_node(np, child) {
1849 ret = of_property_read_u32(child, "reg", &reg);
1850 if (ret)
1851 continue;
1852 if (max_reg == (u32)-1 || reg > max_reg)
1853 max_reg = reg;
1854 }
1855 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1856 if (nchans == 0) {
1857 dev_err(dev, "No channels\n");
1858 return -EINVAL;
1859 }
1860
1861 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1862 unittest_i2c_mux_select_chan, NULL);
1863 if (!muxc)
1864 return -ENOMEM;
1865 for (i = 0; i < nchans; i++) {
1866 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1867 if (ret) {
1868 dev_err(dev, "Failed to register mux #%d\n", i);
1869 i2c_mux_del_adapters(muxc);
1870 return -ENODEV;
1871 }
1872 }
1873
1874 i2c_set_clientdata(client, muxc);
1875
1876 return 0;
1877 };
1878
1879 static int unittest_i2c_mux_remove(struct i2c_client *client)
1880 {
1881 struct device *dev = &client->dev;
1882 struct device_node *np = client->dev.of_node;
1883 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
1884
1885 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1886 i2c_mux_del_adapters(muxc);
1887 return 0;
1888 }
1889
1890 static const struct i2c_device_id unittest_i2c_mux_id[] = {
1891 { .name = "unittest-i2c-mux" },
1892 { }
1893 };
1894
1895 static struct i2c_driver unittest_i2c_mux_driver = {
1896 .driver = {
1897 .name = "unittest-i2c-mux",
1898 },
1899 .probe = unittest_i2c_mux_probe,
1900 .remove = unittest_i2c_mux_remove,
1901 .id_table = unittest_i2c_mux_id,
1902 };
1903
1904 #endif
1905
1906 static int of_unittest_overlay_i2c_init(void)
1907 {
1908 int ret;
1909
1910 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1911 if (unittest(ret == 0,
1912 "could not register unittest i2c device driver\n"))
1913 return ret;
1914
1915 ret = platform_driver_register(&unittest_i2c_bus_driver);
1916 if (unittest(ret == 0,
1917 "could not register unittest i2c bus driver\n"))
1918 return ret;
1919
1920 #if IS_BUILTIN(CONFIG_I2C_MUX)
1921 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1922 if (unittest(ret == 0,
1923 "could not register unittest i2c mux driver\n"))
1924 return ret;
1925 #endif
1926
1927 return 0;
1928 }
1929
1930 static void of_unittest_overlay_i2c_cleanup(void)
1931 {
1932 #if IS_BUILTIN(CONFIG_I2C_MUX)
1933 i2c_del_driver(&unittest_i2c_mux_driver);
1934 #endif
1935 platform_driver_unregister(&unittest_i2c_bus_driver);
1936 i2c_del_driver(&unittest_i2c_dev_driver);
1937 }
1938
1939 static void of_unittest_overlay_i2c_12(void)
1940 {
1941 int ret;
1942
1943 /* device should enable */
1944 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1945 if (ret != 0)
1946 return;
1947
1948 unittest(1, "overlay test %d passed\n", 12);
1949 }
1950
1951 /* test deactivation of device */
1952 static void of_unittest_overlay_i2c_13(void)
1953 {
1954 int ret;
1955
1956 /* device should disable */
1957 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1958 if (ret != 0)
1959 return;
1960
1961 unittest(1, "overlay test %d passed\n", 13);
1962 }
1963
1964 /* just check for i2c mux existence */
1965 static void of_unittest_overlay_i2c_14(void)
1966 {
1967 }
1968
1969 static void of_unittest_overlay_i2c_15(void)
1970 {
1971 int ret;
1972
1973 /* device should enable */
1974 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
1975 if (ret != 0)
1976 return;
1977
1978 unittest(1, "overlay test %d passed\n", 15);
1979 }
1980
1981 #else
1982
1983 static inline void of_unittest_overlay_i2c_14(void) { }
1984 static inline void of_unittest_overlay_i2c_15(void) { }
1985
1986 #endif
1987
1988 static void __init of_unittest_overlay(void)
1989 {
1990 struct device_node *bus_np = NULL;
1991 int ret;
1992
1993 ret = platform_driver_register(&unittest_driver);
1994 if (ret != 0) {
1995 unittest(0, "could not register unittest driver\n");
1996 goto out;
1997 }
1998
1999 bus_np = of_find_node_by_path(bus_path);
2000 if (bus_np == NULL) {
2001 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
2002 goto out;
2003 }
2004
2005 ret = of_platform_default_populate(bus_np, NULL, NULL);
2006 if (ret != 0) {
2007 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
2008 goto out;
2009 }
2010
2011 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2012 unittest(0, "could not find unittest0 @ \"%s\"\n",
2013 unittest_path(100, PDEV_OVERLAY));
2014 goto out;
2015 }
2016
2017 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2018 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2019 unittest_path(101, PDEV_OVERLAY));
2020 goto out;
2021 }
2022
2023 unittest(1, "basic infrastructure of overlays passed");
2024
2025 /* tests in sequence */
2026 of_unittest_overlay_0();
2027 of_unittest_overlay_1();
2028 of_unittest_overlay_2();
2029 of_unittest_overlay_3();
2030 of_unittest_overlay_4();
2031 of_unittest_overlay_5();
2032 of_unittest_overlay_6();
2033 of_unittest_overlay_8();
2034
2035 of_unittest_overlay_10();
2036 of_unittest_overlay_11();
2037
2038 #if IS_BUILTIN(CONFIG_I2C)
2039 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
2040 goto out;
2041
2042 of_unittest_overlay_i2c_12();
2043 of_unittest_overlay_i2c_13();
2044 of_unittest_overlay_i2c_14();
2045 of_unittest_overlay_i2c_15();
2046
2047 of_unittest_overlay_i2c_cleanup();
2048 #endif
2049
2050 of_unittest_destroy_tracked_overlays();
2051
2052 out:
2053 of_node_put(bus_np);
2054 }
2055
2056 #else
2057 static inline void __init of_unittest_overlay(void) { }
2058 #endif
2059
2060 #ifdef CONFIG_OF_OVERLAY
2061
2062 /*
2063 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2064 * in scripts/Makefile.lib
2065 */
2066
2067 #define OVERLAY_INFO_EXTERN(name) \
2068 extern uint8_t __dtb_##name##_begin[]; \
2069 extern uint8_t __dtb_##name##_end[]
2070
2071 #define OVERLAY_INFO(name, expected) \
2072 { .dtb_begin = __dtb_##name##_begin, \
2073 .dtb_end = __dtb_##name##_end, \
2074 .expected_result = expected, \
2075 }
2076
2077 struct overlay_info {
2078 uint8_t *dtb_begin;
2079 uint8_t *dtb_end;
2080 void *data;
2081 struct device_node *np_overlay;
2082 int expected_result;
2083 int overlay_id;
2084 };
2085
2086 OVERLAY_INFO_EXTERN(overlay_base);
2087 OVERLAY_INFO_EXTERN(overlay);
2088 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2089 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2090
2091 /* order of entries is hard-coded into users of overlays[] */
2092 static struct overlay_info overlays[] = {
2093 OVERLAY_INFO(overlay_base, -9999),
2094 OVERLAY_INFO(overlay, 0),
2095 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2096 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2097 {}
2098 };
2099
2100 static struct device_node *overlay_base_root;
2101
2102 /*
2103 * Create base device tree for the overlay unittest.
2104 *
2105 * This is called from very early boot code.
2106 *
2107 * Do as much as possible the same way as done in __unflatten_device_tree
2108 * and other early boot steps for the normal FDT so that the overlay base
2109 * unflattened tree will have the same characteristics as the real tree
2110 * (such as having memory allocated by the early allocator). The goal
2111 * is to test "the real thing" as much as possible, and test "test setup
2112 * code" as little as possible.
2113 *
2114 * Have to stop before resolving phandles, because that uses kmalloc.
2115 */
2116 void __init unittest_unflatten_overlay_base(void)
2117 {
2118 struct overlay_info *info;
2119 u32 data_size;
2120 u32 size;
2121
2122 info = &overlays[0];
2123
2124 if (info->expected_result != -9999) {
2125 pr_err("No dtb 'overlay_base' to attach\n");
2126 return;
2127 }
2128
2129 data_size = info->dtb_end - info->dtb_begin;
2130 if (!data_size) {
2131 pr_err("No dtb 'overlay_base' to attach\n");
2132 return;
2133 }
2134
2135 size = fdt_totalsize(info->dtb_begin);
2136 if (size != data_size) {
2137 pr_err("dtb 'overlay_base' header totalsize != actual size");
2138 return;
2139 }
2140
2141 info->data = early_init_dt_alloc_memory_arch(size,
2142 roundup_pow_of_two(FDT_V17_SIZE));
2143 if (!info->data) {
2144 pr_err("alloc for dtb 'overlay_base' failed");
2145 return;
2146 }
2147
2148 memcpy(info->data, info->dtb_begin, size);
2149
2150 __unflatten_device_tree(info->data, NULL, &info->np_overlay,
2151 early_init_dt_alloc_memory_arch, true);
2152 overlay_base_root = info->np_overlay;
2153 }
2154
2155 /*
2156 * The purpose of of_unittest_overlay_data_add is to add an
2157 * overlay in the normal fashion. This is a test of the whole
2158 * picture, instead of testing individual elements.
2159 *
2160 * A secondary purpose is to be able to verify that the contents of
2161 * /proc/device-tree/ contains the updated structure and values from
2162 * the overlay. That must be verified separately in user space.
2163 *
2164 * Return 0 on unexpected error.
2165 */
2166 static int __init overlay_data_add(int onum)
2167 {
2168 struct overlay_info *info;
2169 int k;
2170 int ret;
2171 u32 size;
2172 u32 size_from_header;
2173
2174 for (k = 0, info = overlays; info; info++, k++) {
2175 if (k == onum)
2176 break;
2177 }
2178 if (onum > k)
2179 return 0;
2180
2181 size = info->dtb_end - info->dtb_begin;
2182 if (!size) {
2183 pr_err("no overlay to attach, %d\n", onum);
2184 ret = 0;
2185 }
2186
2187 size_from_header = fdt_totalsize(info->dtb_begin);
2188 if (size_from_header != size) {
2189 pr_err("overlay header totalsize != actual size, %d", onum);
2190 return 0;
2191 }
2192
2193 /*
2194 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
2195 * will create pointers to the passed in FDT in the EDT.
2196 */
2197 info->data = kmemdup(info->dtb_begin, size, GFP_KERNEL);
2198 if (!info->data) {
2199 pr_err("unable to allocate memory for data, %d\n", onum);
2200 return 0;
2201 }
2202
2203 of_fdt_unflatten_tree(info->data, NULL, &info->np_overlay);
2204 if (!info->np_overlay) {
2205 pr_err("unable to unflatten overlay, %d\n", onum);
2206 ret = 0;
2207 goto out_free_data;
2208 }
2209
2210 info->overlay_id = 0;
2211 ret = of_overlay_apply(info->np_overlay, &info->overlay_id);
2212 if (ret < 0) {
2213 pr_err("of_overlay_apply() (ret=%d), %d\n", ret, onum);
2214 goto out_free_np_overlay;
2215 }
2216
2217 pr_debug("__dtb_overlay_begin applied, overlay id %d\n", ret);
2218
2219 goto out;
2220
2221 out_free_np_overlay:
2222 /*
2223 * info->np_overlay is the unflattened device tree
2224 * It has not been spliced into the live tree.
2225 */
2226
2227 /* todo: function to free unflattened device tree */
2228
2229 out_free_data:
2230 kfree(info->data);
2231
2232 out:
2233 return (ret == info->expected_result);
2234 }
2235
2236 /*
2237 * The purpose of of_unittest_overlay_high_level is to add an overlay
2238 * in the normal fashion. This is a test of the whole picture,
2239 * instead of individual elements.
2240 *
2241 * The first part of the function is _not_ normal overlay usage; it is
2242 * finishing splicing the base overlay device tree into the live tree.
2243 */
2244 static __init void of_unittest_overlay_high_level(void)
2245 {
2246 struct device_node *last_sibling;
2247 struct device_node *np;
2248 struct device_node *of_symbols;
2249 struct device_node *overlay_base_symbols;
2250 struct device_node **pprev;
2251 struct property *prop;
2252 int ret;
2253
2254 if (!overlay_base_root) {
2255 unittest(0, "overlay_base_root not initialized\n");
2256 return;
2257 }
2258
2259 /*
2260 * Could not fixup phandles in unittest_unflatten_overlay_base()
2261 * because kmalloc() was not yet available.
2262 */
2263 of_overlay_mutex_lock();
2264 of_resolve_phandles(overlay_base_root);
2265 of_overlay_mutex_unlock();
2266
2267
2268 /*
2269 * do not allow overlay_base to duplicate any node already in
2270 * tree, this greatly simplifies the code
2271 */
2272
2273 /*
2274 * remove overlay_base_root node "__local_fixups", after
2275 * being used by of_resolve_phandles()
2276 */
2277 pprev = &overlay_base_root->child;
2278 for (np = overlay_base_root->child; np; np = np->sibling) {
2279 if (!of_node_cmp(np->name, "__local_fixups__")) {
2280 *pprev = np->sibling;
2281 break;
2282 }
2283 pprev = &np->sibling;
2284 }
2285
2286 /* remove overlay_base_root node "__symbols__" if in live tree */
2287 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2288 if (of_symbols) {
2289 /* will have to graft properties from node into live tree */
2290 pprev = &overlay_base_root->child;
2291 for (np = overlay_base_root->child; np; np = np->sibling) {
2292 if (!of_node_cmp(np->name, "__symbols__")) {
2293 overlay_base_symbols = np;
2294 *pprev = np->sibling;
2295 break;
2296 }
2297 pprev = &np->sibling;
2298 }
2299 }
2300
2301 for (np = overlay_base_root->child; np; np = np->sibling) {
2302 if (of_get_child_by_name(of_root, np->name)) {
2303 unittest(0, "illegal node name in overlay_base %s",
2304 np->name);
2305 return;
2306 }
2307 }
2308
2309 /*
2310 * overlay 'overlay_base' is not allowed to have root
2311 * properties, so only need to splice nodes into main device tree.
2312 *
2313 * root node of *overlay_base_root will not be freed, it is lost
2314 * memory.
2315 */
2316
2317 for (np = overlay_base_root->child; np; np = np->sibling)
2318 np->parent = of_root;
2319
2320 mutex_lock(&of_mutex);
2321
2322 for (last_sibling = np = of_root->child; np; np = np->sibling)
2323 last_sibling = np;
2324
2325 if (last_sibling)
2326 last_sibling->sibling = overlay_base_root->child;
2327 else
2328 of_root->child = overlay_base_root->child;
2329
2330 for_each_of_allnodes_from(overlay_base_root, np)
2331 __of_attach_node_sysfs(np);
2332
2333 if (of_symbols) {
2334 for_each_property_of_node(overlay_base_symbols, prop) {
2335 ret = __of_add_property(of_symbols, prop);
2336 if (ret) {
2337 unittest(0,
2338 "duplicate property '%s' in overlay_base node __symbols__",
2339 prop->name);
2340 goto err_unlock;
2341 }
2342 ret = __of_add_property_sysfs(of_symbols, prop);
2343 if (ret) {
2344 unittest(0,
2345 "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2346 prop->name);
2347 goto err_unlock;
2348 }
2349 }
2350 }
2351
2352 mutex_unlock(&of_mutex);
2353
2354
2355 /* now do the normal overlay usage test */
2356
2357 unittest(overlay_data_add(1),
2358 "Adding overlay 'overlay' failed\n");
2359
2360 unittest(overlay_data_add(2),
2361 "Adding overlay 'overlay_bad_phandle' failed\n");
2362
2363 unittest(overlay_data_add(3),
2364 "Adding overlay 'overlay_bad_symbol' failed\n");
2365
2366 return;
2367
2368 err_unlock:
2369 mutex_unlock(&of_mutex);
2370 }
2371
2372 #else
2373
2374 static inline __init void of_unittest_overlay_high_level(void) {}
2375
2376 #endif
2377
2378 static int __init of_unittest(void)
2379 {
2380 struct device_node *np;
2381 int res;
2382
2383 /* adding data for unittest */
2384 res = unittest_data_add();
2385 if (res)
2386 return res;
2387 if (!of_aliases)
2388 of_aliases = of_find_node_by_path("/aliases");
2389
2390 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2391 if (!np) {
2392 pr_info("No testcase data in device tree; not running tests\n");
2393 return 0;
2394 }
2395 of_node_put(np);
2396
2397 pr_info("start of unittest - you will see error messages\n");
2398 of_unittest_check_tree_linkage();
2399 of_unittest_check_phandles();
2400 of_unittest_find_node_by_name();
2401 of_unittest_dynamic();
2402 of_unittest_parse_phandle_with_args();
2403 of_unittest_printf();
2404 of_unittest_property_string();
2405 of_unittest_property_copy();
2406 of_unittest_changeset();
2407 of_unittest_parse_interrupts();
2408 of_unittest_parse_interrupts_extended();
2409 of_unittest_match_node();
2410 of_unittest_platform_populate();
2411 of_unittest_overlay();
2412
2413 /* Double check linkage after removing testcase data */
2414 of_unittest_check_tree_linkage();
2415
2416 of_unittest_overlay_high_level();
2417
2418 pr_info("end of unittest - %i passed, %i failed\n",
2419 unittest_results.passed, unittest_results.failed);
2420
2421 return 0;
2422 }
2423 late_initcall(of_unittest);