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