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