xref: /linux/drivers/of/unittest.c (revision b1b7ce97fa1e084b2ea41b906b74371e82d61d3a)
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 		return -ENODATA;
1301 	}
1302 
1303 	/*
1304 	 * This lock normally encloses of_resolve_phandles()
1305 	 */
1306 	of_overlay_mutex_lock();
1307 
1308 	rc = of_resolve_phandles(unittest_data_node);
1309 	if (rc) {
1310 		pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1311 		of_overlay_mutex_unlock();
1312 		return -EINVAL;
1313 	}
1314 
1315 	if (!of_root) {
1316 		of_root = unittest_data_node;
1317 		for_each_of_allnodes(np)
1318 			__of_attach_node_sysfs(np);
1319 		of_aliases = of_find_node_by_path("/aliases");
1320 		of_chosen = of_find_node_by_path("/chosen");
1321 		of_overlay_mutex_unlock();
1322 		return 0;
1323 	}
1324 
1325 	/* attach the sub-tree to live tree */
1326 	np = unittest_data_node->child;
1327 	while (np) {
1328 		struct device_node *next = np->sibling;
1329 
1330 		np->parent = of_root;
1331 		attach_node_and_children(np);
1332 		np = next;
1333 	}
1334 
1335 	of_overlay_mutex_unlock();
1336 
1337 	return 0;
1338 }
1339 
1340 #ifdef CONFIG_OF_OVERLAY
1341 static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
1342 
1343 static int unittest_probe(struct platform_device *pdev)
1344 {
1345 	struct device *dev = &pdev->dev;
1346 	struct device_node *np = dev->of_node;
1347 
1348 	if (np == NULL) {
1349 		dev_err(dev, "No OF data for device\n");
1350 		return -EINVAL;
1351 
1352 	}
1353 
1354 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1355 
1356 	of_platform_populate(np, NULL, NULL, &pdev->dev);
1357 
1358 	return 0;
1359 }
1360 
1361 static int unittest_remove(struct platform_device *pdev)
1362 {
1363 	struct device *dev = &pdev->dev;
1364 	struct device_node *np = dev->of_node;
1365 
1366 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1367 	return 0;
1368 }
1369 
1370 static const struct of_device_id unittest_match[] = {
1371 	{ .compatible = "unittest", },
1372 	{},
1373 };
1374 
1375 static struct platform_driver unittest_driver = {
1376 	.probe			= unittest_probe,
1377 	.remove			= unittest_remove,
1378 	.driver = {
1379 		.name		= "unittest",
1380 		.of_match_table	= of_match_ptr(unittest_match),
1381 	},
1382 };
1383 
1384 /* get the platform device instantiated at the path */
1385 static struct platform_device *of_path_to_platform_device(const char *path)
1386 {
1387 	struct device_node *np;
1388 	struct platform_device *pdev;
1389 
1390 	np = of_find_node_by_path(path);
1391 	if (np == NULL)
1392 		return NULL;
1393 
1394 	pdev = of_find_device_by_node(np);
1395 	of_node_put(np);
1396 
1397 	return pdev;
1398 }
1399 
1400 /* find out if a platform device exists at that path */
1401 static int of_path_platform_device_exists(const char *path)
1402 {
1403 	struct platform_device *pdev;
1404 
1405 	pdev = of_path_to_platform_device(path);
1406 	platform_device_put(pdev);
1407 	return pdev != NULL;
1408 }
1409 
1410 #if IS_BUILTIN(CONFIG_I2C)
1411 
1412 /* get the i2c client device instantiated at the path */
1413 static struct i2c_client *of_path_to_i2c_client(const char *path)
1414 {
1415 	struct device_node *np;
1416 	struct i2c_client *client;
1417 
1418 	np = of_find_node_by_path(path);
1419 	if (np == NULL)
1420 		return NULL;
1421 
1422 	client = of_find_i2c_device_by_node(np);
1423 	of_node_put(np);
1424 
1425 	return client;
1426 }
1427 
1428 /* find out if a i2c client device exists at that path */
1429 static int of_path_i2c_client_exists(const char *path)
1430 {
1431 	struct i2c_client *client;
1432 
1433 	client = of_path_to_i2c_client(path);
1434 	if (client)
1435 		put_device(&client->dev);
1436 	return client != NULL;
1437 }
1438 #else
1439 static int of_path_i2c_client_exists(const char *path)
1440 {
1441 	return 0;
1442 }
1443 #endif
1444 
1445 enum overlay_type {
1446 	PDEV_OVERLAY,
1447 	I2C_OVERLAY
1448 };
1449 
1450 static int of_path_device_type_exists(const char *path,
1451 		enum overlay_type ovtype)
1452 {
1453 	switch (ovtype) {
1454 	case PDEV_OVERLAY:
1455 		return of_path_platform_device_exists(path);
1456 	case I2C_OVERLAY:
1457 		return of_path_i2c_client_exists(path);
1458 	}
1459 	return 0;
1460 }
1461 
1462 static const char *unittest_path(int nr, enum overlay_type ovtype)
1463 {
1464 	const char *base;
1465 	static char buf[256];
1466 
1467 	switch (ovtype) {
1468 	case PDEV_OVERLAY:
1469 		base = "/testcase-data/overlay-node/test-bus";
1470 		break;
1471 	case I2C_OVERLAY:
1472 		base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1473 		break;
1474 	default:
1475 		buf[0] = '\0';
1476 		return buf;
1477 	}
1478 	snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1479 	buf[sizeof(buf) - 1] = '\0';
1480 	return buf;
1481 }
1482 
1483 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1484 {
1485 	const char *path;
1486 
1487 	path = unittest_path(unittest_nr, ovtype);
1488 
1489 	switch (ovtype) {
1490 	case PDEV_OVERLAY:
1491 		return of_path_platform_device_exists(path);
1492 	case I2C_OVERLAY:
1493 		return of_path_i2c_client_exists(path);
1494 	}
1495 	return 0;
1496 }
1497 
1498 static const char *overlay_name_from_nr(int nr)
1499 {
1500 	static char buf[256];
1501 
1502 	snprintf(buf, sizeof(buf) - 1,
1503 		"overlay_%d", nr);
1504 	buf[sizeof(buf) - 1] = '\0';
1505 
1506 	return buf;
1507 }
1508 
1509 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1510 
1511 /* it is guaranteed that overlay ids are assigned in sequence */
1512 #define MAX_UNITTEST_OVERLAYS	256
1513 static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1514 static int overlay_first_id = -1;
1515 
1516 static void of_unittest_track_overlay(int id)
1517 {
1518 	if (overlay_first_id < 0)
1519 		overlay_first_id = id;
1520 	id -= overlay_first_id;
1521 
1522 	/* we shouldn't need that many */
1523 	BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1524 	overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1525 }
1526 
1527 static void of_unittest_untrack_overlay(int id)
1528 {
1529 	if (overlay_first_id < 0)
1530 		return;
1531 	id -= overlay_first_id;
1532 	BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1533 	overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1534 }
1535 
1536 static void of_unittest_destroy_tracked_overlays(void)
1537 {
1538 	int id, ret, defers, ovcs_id;
1539 
1540 	if (overlay_first_id < 0)
1541 		return;
1542 
1543 	/* try until no defers */
1544 	do {
1545 		defers = 0;
1546 		/* remove in reverse order */
1547 		for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1548 			if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1549 				continue;
1550 
1551 			ovcs_id = id + overlay_first_id;
1552 			ret = of_overlay_remove(&ovcs_id);
1553 			if (ret == -ENODEV) {
1554 				pr_warn("%s: no overlay to destroy for #%d\n",
1555 					__func__, id + overlay_first_id);
1556 				continue;
1557 			}
1558 			if (ret != 0) {
1559 				defers++;
1560 				pr_warn("%s: overlay destroy failed for #%d\n",
1561 					__func__, id + overlay_first_id);
1562 				continue;
1563 			}
1564 
1565 			overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1566 		}
1567 	} while (defers > 0);
1568 }
1569 
1570 static int __init of_unittest_apply_overlay(int overlay_nr, int *overlay_id)
1571 {
1572 	const char *overlay_name;
1573 
1574 	overlay_name = overlay_name_from_nr(overlay_nr);
1575 
1576 	if (!overlay_data_apply(overlay_name, overlay_id)) {
1577 		unittest(0, "could not apply overlay \"%s\"\n",
1578 				overlay_name);
1579 		return -EFAULT;
1580 	}
1581 	of_unittest_track_overlay(*overlay_id);
1582 
1583 	return 0;
1584 }
1585 
1586 /* apply an overlay while checking before and after states */
1587 static int __init of_unittest_apply_overlay_check(int overlay_nr,
1588 		int unittest_nr, int before, int after,
1589 		enum overlay_type ovtype)
1590 {
1591 	int ret, ovcs_id;
1592 
1593 	/* unittest device must not be in before state */
1594 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1595 		unittest(0, "%s with device @\"%s\" %s\n",
1596 				overlay_name_from_nr(overlay_nr),
1597 				unittest_path(unittest_nr, ovtype),
1598 				!before ? "enabled" : "disabled");
1599 		return -EINVAL;
1600 	}
1601 
1602 	ovcs_id = 0;
1603 	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
1604 	if (ret != 0) {
1605 		/* of_unittest_apply_overlay already called unittest() */
1606 		return ret;
1607 	}
1608 
1609 	/* unittest device must be to set to after state */
1610 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1611 		unittest(0, "%s failed to create @\"%s\" %s\n",
1612 				overlay_name_from_nr(overlay_nr),
1613 				unittest_path(unittest_nr, ovtype),
1614 				!after ? "enabled" : "disabled");
1615 		return -EINVAL;
1616 	}
1617 
1618 	return 0;
1619 }
1620 
1621 /* apply an overlay and then revert it while checking before, after states */
1622 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
1623 		int unittest_nr, int before, int after,
1624 		enum overlay_type ovtype)
1625 {
1626 	int ret, ovcs_id;
1627 
1628 	/* unittest device must be in before state */
1629 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1630 		unittest(0, "%s with device @\"%s\" %s\n",
1631 				overlay_name_from_nr(overlay_nr),
1632 				unittest_path(unittest_nr, ovtype),
1633 				!before ? "enabled" : "disabled");
1634 		return -EINVAL;
1635 	}
1636 
1637 	/* apply the overlay */
1638 	ovcs_id = 0;
1639 	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
1640 	if (ret != 0) {
1641 		/* of_unittest_apply_overlay already called unittest() */
1642 		return ret;
1643 	}
1644 
1645 	/* unittest device must be in after state */
1646 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1647 		unittest(0, "%s failed to create @\"%s\" %s\n",
1648 				overlay_name_from_nr(overlay_nr),
1649 				unittest_path(unittest_nr, ovtype),
1650 				!after ? "enabled" : "disabled");
1651 		return -EINVAL;
1652 	}
1653 
1654 	ret = of_overlay_remove(&ovcs_id);
1655 	if (ret != 0) {
1656 		unittest(0, "%s failed to be destroyed @\"%s\"\n",
1657 				overlay_name_from_nr(overlay_nr),
1658 				unittest_path(unittest_nr, ovtype));
1659 		return ret;
1660 	}
1661 
1662 	/* unittest device must be again in before state */
1663 	if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1664 		unittest(0, "%s with device @\"%s\" %s\n",
1665 				overlay_name_from_nr(overlay_nr),
1666 				unittest_path(unittest_nr, ovtype),
1667 				!before ? "enabled" : "disabled");
1668 		return -EINVAL;
1669 	}
1670 
1671 	return 0;
1672 }
1673 
1674 /* test activation of device */
1675 static void __init of_unittest_overlay_0(void)
1676 {
1677 	/* device should enable */
1678 	if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
1679 		return;
1680 
1681 	unittest(1, "overlay test %d passed\n", 0);
1682 }
1683 
1684 /* test deactivation of device */
1685 static void __init of_unittest_overlay_1(void)
1686 {
1687 	/* device should disable */
1688 	if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
1689 		return;
1690 
1691 	unittest(1, "overlay test %d passed\n", 1);
1692 }
1693 
1694 /* test activation of device */
1695 static void __init of_unittest_overlay_2(void)
1696 {
1697 	/* device should enable */
1698 	if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
1699 		return;
1700 
1701 	unittest(1, "overlay test %d passed\n", 2);
1702 }
1703 
1704 /* test deactivation of device */
1705 static void __init of_unittest_overlay_3(void)
1706 {
1707 	/* device should disable */
1708 	if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
1709 		return;
1710 
1711 	unittest(1, "overlay test %d passed\n", 3);
1712 }
1713 
1714 /* test activation of a full device node */
1715 static void __init of_unittest_overlay_4(void)
1716 {
1717 	/* device should disable */
1718 	if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
1719 		return;
1720 
1721 	unittest(1, "overlay test %d passed\n", 4);
1722 }
1723 
1724 /* test overlay apply/revert sequence */
1725 static void __init of_unittest_overlay_5(void)
1726 {
1727 	/* device should disable */
1728 	if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
1729 		return;
1730 
1731 	unittest(1, "overlay test %d passed\n", 5);
1732 }
1733 
1734 /* test overlay application in sequence */
1735 static void __init of_unittest_overlay_6(void)
1736 {
1737 	int i, ov_id[2], ovcs_id;
1738 	int overlay_nr = 6, unittest_nr = 6;
1739 	int before = 0, after = 1;
1740 	const char *overlay_name;
1741 
1742 	/* unittest device must be in before state */
1743 	for (i = 0; i < 2; i++) {
1744 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1745 				!= before) {
1746 			unittest(0, "%s with device @\"%s\" %s\n",
1747 					overlay_name_from_nr(overlay_nr + i),
1748 					unittest_path(unittest_nr + i,
1749 						PDEV_OVERLAY),
1750 					!before ? "enabled" : "disabled");
1751 			return;
1752 		}
1753 	}
1754 
1755 	/* apply the overlays */
1756 	for (i = 0; i < 2; i++) {
1757 
1758 		overlay_name = overlay_name_from_nr(overlay_nr + i);
1759 
1760 		if (!overlay_data_apply(overlay_name, &ovcs_id)) {
1761 			unittest(0, "could not apply overlay \"%s\"\n",
1762 					overlay_name);
1763 			return;
1764 		}
1765 		ov_id[i] = ovcs_id;
1766 		of_unittest_track_overlay(ov_id[i]);
1767 	}
1768 
1769 	for (i = 0; i < 2; i++) {
1770 		/* unittest device must be in after state */
1771 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1772 				!= after) {
1773 			unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1774 					overlay_name_from_nr(overlay_nr + i),
1775 					unittest_path(unittest_nr + i,
1776 						PDEV_OVERLAY),
1777 					!after ? "enabled" : "disabled");
1778 			return;
1779 		}
1780 	}
1781 
1782 	for (i = 1; i >= 0; i--) {
1783 		ovcs_id = ov_id[i];
1784 		if (of_overlay_remove(&ovcs_id)) {
1785 			unittest(0, "%s failed destroy @\"%s\"\n",
1786 					overlay_name_from_nr(overlay_nr + i),
1787 					unittest_path(unittest_nr + i,
1788 						PDEV_OVERLAY));
1789 			return;
1790 		}
1791 		of_unittest_untrack_overlay(ov_id[i]);
1792 	}
1793 
1794 	for (i = 0; i < 2; i++) {
1795 		/* unittest device must be again in before state */
1796 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1797 				!= before) {
1798 			unittest(0, "%s with device @\"%s\" %s\n",
1799 					overlay_name_from_nr(overlay_nr + i),
1800 					unittest_path(unittest_nr + i,
1801 						PDEV_OVERLAY),
1802 					!before ? "enabled" : "disabled");
1803 			return;
1804 		}
1805 	}
1806 
1807 	unittest(1, "overlay test %d passed\n", 6);
1808 }
1809 
1810 /* test overlay application in sequence */
1811 static void __init of_unittest_overlay_8(void)
1812 {
1813 	int i, ov_id[2], ovcs_id;
1814 	int overlay_nr = 8, unittest_nr = 8;
1815 	const char *overlay_name;
1816 
1817 	/* we don't care about device state in this test */
1818 
1819 	/* apply the overlays */
1820 	for (i = 0; i < 2; i++) {
1821 
1822 		overlay_name = overlay_name_from_nr(overlay_nr + i);
1823 
1824 		if (!overlay_data_apply(overlay_name, &ovcs_id)) {
1825 			unittest(0, "could not apply overlay \"%s\"\n",
1826 					overlay_name);
1827 			return;
1828 		}
1829 		ov_id[i] = ovcs_id;
1830 		of_unittest_track_overlay(ov_id[i]);
1831 	}
1832 
1833 	/* now try to remove first overlay (it should fail) */
1834 	ovcs_id = ov_id[0];
1835 	if (!of_overlay_remove(&ovcs_id)) {
1836 		unittest(0, "%s was destroyed @\"%s\"\n",
1837 				overlay_name_from_nr(overlay_nr + 0),
1838 				unittest_path(unittest_nr,
1839 					PDEV_OVERLAY));
1840 		return;
1841 	}
1842 
1843 	/* removing them in order should work */
1844 	for (i = 1; i >= 0; i--) {
1845 		ovcs_id = ov_id[i];
1846 		if (of_overlay_remove(&ovcs_id)) {
1847 			unittest(0, "%s not destroyed @\"%s\"\n",
1848 					overlay_name_from_nr(overlay_nr + i),
1849 					unittest_path(unittest_nr,
1850 						PDEV_OVERLAY));
1851 			return;
1852 		}
1853 		of_unittest_untrack_overlay(ov_id[i]);
1854 	}
1855 
1856 	unittest(1, "overlay test %d passed\n", 8);
1857 }
1858 
1859 /* test insertion of a bus with parent devices */
1860 static void __init of_unittest_overlay_10(void)
1861 {
1862 	int ret;
1863 	char *child_path;
1864 
1865 	/* device should disable */
1866 	ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1867 	if (unittest(ret == 0,
1868 			"overlay test %d failed; overlay application\n", 10))
1869 		return;
1870 
1871 	child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1872 			unittest_path(10, PDEV_OVERLAY));
1873 	if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
1874 		return;
1875 
1876 	ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1877 	kfree(child_path);
1878 
1879 	unittest(ret, "overlay test %d failed; no child device\n", 10);
1880 }
1881 
1882 /* test insertion of a bus with parent devices (and revert) */
1883 static void __init of_unittest_overlay_11(void)
1884 {
1885 	int ret;
1886 
1887 	/* device should disable */
1888 	ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1889 			PDEV_OVERLAY);
1890 	unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
1891 }
1892 
1893 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1894 
1895 struct unittest_i2c_bus_data {
1896 	struct platform_device	*pdev;
1897 	struct i2c_adapter	adap;
1898 };
1899 
1900 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
1901 		struct i2c_msg *msgs, int num)
1902 {
1903 	struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
1904 
1905 	(void)std;
1906 
1907 	return num;
1908 }
1909 
1910 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
1911 {
1912 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1913 }
1914 
1915 static const struct i2c_algorithm unittest_i2c_algo = {
1916 	.master_xfer	= unittest_i2c_master_xfer,
1917 	.functionality	= unittest_i2c_functionality,
1918 };
1919 
1920 static int unittest_i2c_bus_probe(struct platform_device *pdev)
1921 {
1922 	struct device *dev = &pdev->dev;
1923 	struct device_node *np = dev->of_node;
1924 	struct unittest_i2c_bus_data *std;
1925 	struct i2c_adapter *adap;
1926 	int ret;
1927 
1928 	if (np == NULL) {
1929 		dev_err(dev, "No OF data for device\n");
1930 		return -EINVAL;
1931 
1932 	}
1933 
1934 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1935 
1936 	std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1937 	if (!std)
1938 		return -ENOMEM;
1939 
1940 	/* link them together */
1941 	std->pdev = pdev;
1942 	platform_set_drvdata(pdev, std);
1943 
1944 	adap = &std->adap;
1945 	i2c_set_adapdata(adap, std);
1946 	adap->nr = -1;
1947 	strlcpy(adap->name, pdev->name, sizeof(adap->name));
1948 	adap->class = I2C_CLASS_DEPRECATED;
1949 	adap->algo = &unittest_i2c_algo;
1950 	adap->dev.parent = dev;
1951 	adap->dev.of_node = dev->of_node;
1952 	adap->timeout = 5 * HZ;
1953 	adap->retries = 3;
1954 
1955 	ret = i2c_add_numbered_adapter(adap);
1956 	if (ret != 0) {
1957 		dev_err(dev, "Failed to add I2C adapter\n");
1958 		return ret;
1959 	}
1960 
1961 	return 0;
1962 }
1963 
1964 static int unittest_i2c_bus_remove(struct platform_device *pdev)
1965 {
1966 	struct device *dev = &pdev->dev;
1967 	struct device_node *np = dev->of_node;
1968 	struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
1969 
1970 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1971 	i2c_del_adapter(&std->adap);
1972 
1973 	return 0;
1974 }
1975 
1976 static const struct of_device_id unittest_i2c_bus_match[] = {
1977 	{ .compatible = "unittest-i2c-bus", },
1978 	{},
1979 };
1980 
1981 static struct platform_driver unittest_i2c_bus_driver = {
1982 	.probe			= unittest_i2c_bus_probe,
1983 	.remove			= unittest_i2c_bus_remove,
1984 	.driver = {
1985 		.name		= "unittest-i2c-bus",
1986 		.of_match_table	= of_match_ptr(unittest_i2c_bus_match),
1987 	},
1988 };
1989 
1990 static int unittest_i2c_dev_probe(struct i2c_client *client,
1991 		const struct i2c_device_id *id)
1992 {
1993 	struct device *dev = &client->dev;
1994 	struct device_node *np = client->dev.of_node;
1995 
1996 	if (!np) {
1997 		dev_err(dev, "No OF node\n");
1998 		return -EINVAL;
1999 	}
2000 
2001 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2002 
2003 	return 0;
2004 };
2005 
2006 static int unittest_i2c_dev_remove(struct i2c_client *client)
2007 {
2008 	struct device *dev = &client->dev;
2009 	struct device_node *np = client->dev.of_node;
2010 
2011 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2012 	return 0;
2013 }
2014 
2015 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2016 	{ .name = "unittest-i2c-dev" },
2017 	{ }
2018 };
2019 
2020 static struct i2c_driver unittest_i2c_dev_driver = {
2021 	.driver = {
2022 		.name = "unittest-i2c-dev",
2023 	},
2024 	.probe = unittest_i2c_dev_probe,
2025 	.remove = unittest_i2c_dev_remove,
2026 	.id_table = unittest_i2c_dev_id,
2027 };
2028 
2029 #if IS_BUILTIN(CONFIG_I2C_MUX)
2030 
2031 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2032 {
2033 	return 0;
2034 }
2035 
2036 static int unittest_i2c_mux_probe(struct i2c_client *client,
2037 		const struct i2c_device_id *id)
2038 {
2039 	int i, nchans;
2040 	struct device *dev = &client->dev;
2041 	struct i2c_adapter *adap = client->adapter;
2042 	struct device_node *np = client->dev.of_node, *child;
2043 	struct i2c_mux_core *muxc;
2044 	u32 reg, max_reg;
2045 
2046 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2047 
2048 	if (!np) {
2049 		dev_err(dev, "No OF node\n");
2050 		return -EINVAL;
2051 	}
2052 
2053 	max_reg = (u32)-1;
2054 	for_each_child_of_node(np, child) {
2055 		if (of_property_read_u32(child, "reg", &reg))
2056 			continue;
2057 		if (max_reg == (u32)-1 || reg > max_reg)
2058 			max_reg = reg;
2059 	}
2060 	nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2061 	if (nchans == 0) {
2062 		dev_err(dev, "No channels\n");
2063 		return -EINVAL;
2064 	}
2065 
2066 	muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2067 			     unittest_i2c_mux_select_chan, NULL);
2068 	if (!muxc)
2069 		return -ENOMEM;
2070 	for (i = 0; i < nchans; i++) {
2071 		if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2072 			dev_err(dev, "Failed to register mux #%d\n", i);
2073 			i2c_mux_del_adapters(muxc);
2074 			return -ENODEV;
2075 		}
2076 	}
2077 
2078 	i2c_set_clientdata(client, muxc);
2079 
2080 	return 0;
2081 };
2082 
2083 static int unittest_i2c_mux_remove(struct i2c_client *client)
2084 {
2085 	struct device *dev = &client->dev;
2086 	struct device_node *np = client->dev.of_node;
2087 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2088 
2089 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2090 	i2c_mux_del_adapters(muxc);
2091 	return 0;
2092 }
2093 
2094 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2095 	{ .name = "unittest-i2c-mux" },
2096 	{ }
2097 };
2098 
2099 static struct i2c_driver unittest_i2c_mux_driver = {
2100 	.driver = {
2101 		.name = "unittest-i2c-mux",
2102 	},
2103 	.probe = unittest_i2c_mux_probe,
2104 	.remove = unittest_i2c_mux_remove,
2105 	.id_table = unittest_i2c_mux_id,
2106 };
2107 
2108 #endif
2109 
2110 static int of_unittest_overlay_i2c_init(void)
2111 {
2112 	int ret;
2113 
2114 	ret = i2c_add_driver(&unittest_i2c_dev_driver);
2115 	if (unittest(ret == 0,
2116 			"could not register unittest i2c device driver\n"))
2117 		return ret;
2118 
2119 	ret = platform_driver_register(&unittest_i2c_bus_driver);
2120 	if (unittest(ret == 0,
2121 			"could not register unittest i2c bus driver\n"))
2122 		return ret;
2123 
2124 #if IS_BUILTIN(CONFIG_I2C_MUX)
2125 	ret = i2c_add_driver(&unittest_i2c_mux_driver);
2126 	if (unittest(ret == 0,
2127 			"could not register unittest i2c mux driver\n"))
2128 		return ret;
2129 #endif
2130 
2131 	return 0;
2132 }
2133 
2134 static void of_unittest_overlay_i2c_cleanup(void)
2135 {
2136 #if IS_BUILTIN(CONFIG_I2C_MUX)
2137 	i2c_del_driver(&unittest_i2c_mux_driver);
2138 #endif
2139 	platform_driver_unregister(&unittest_i2c_bus_driver);
2140 	i2c_del_driver(&unittest_i2c_dev_driver);
2141 }
2142 
2143 static void __init of_unittest_overlay_i2c_12(void)
2144 {
2145 	/* device should enable */
2146 	if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
2147 		return;
2148 
2149 	unittest(1, "overlay test %d passed\n", 12);
2150 }
2151 
2152 /* test deactivation of device */
2153 static void __init of_unittest_overlay_i2c_13(void)
2154 {
2155 	/* device should disable */
2156 	if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
2157 		return;
2158 
2159 	unittest(1, "overlay test %d passed\n", 13);
2160 }
2161 
2162 /* just check for i2c mux existence */
2163 static void of_unittest_overlay_i2c_14(void)
2164 {
2165 }
2166 
2167 static void __init of_unittest_overlay_i2c_15(void)
2168 {
2169 	/* device should enable */
2170 	if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
2171 		return;
2172 
2173 	unittest(1, "overlay test %d passed\n", 15);
2174 }
2175 
2176 #else
2177 
2178 static inline void of_unittest_overlay_i2c_14(void) { }
2179 static inline void of_unittest_overlay_i2c_15(void) { }
2180 
2181 #endif
2182 
2183 static void __init of_unittest_overlay(void)
2184 {
2185 	struct device_node *bus_np = NULL;
2186 
2187 	if (platform_driver_register(&unittest_driver)) {
2188 		unittest(0, "could not register unittest driver\n");
2189 		goto out;
2190 	}
2191 
2192 	bus_np = of_find_node_by_path(bus_path);
2193 	if (bus_np == NULL) {
2194 		unittest(0, "could not find bus_path \"%s\"\n", bus_path);
2195 		goto out;
2196 	}
2197 
2198 	if (of_platform_default_populate(bus_np, NULL, NULL)) {
2199 		unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
2200 		goto out;
2201 	}
2202 
2203 	if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2204 		unittest(0, "could not find unittest0 @ \"%s\"\n",
2205 				unittest_path(100, PDEV_OVERLAY));
2206 		goto out;
2207 	}
2208 
2209 	if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2210 		unittest(0, "unittest1 @ \"%s\" should not exist\n",
2211 				unittest_path(101, PDEV_OVERLAY));
2212 		goto out;
2213 	}
2214 
2215 	unittest(1, "basic infrastructure of overlays passed");
2216 
2217 	/* tests in sequence */
2218 	of_unittest_overlay_0();
2219 	of_unittest_overlay_1();
2220 	of_unittest_overlay_2();
2221 	of_unittest_overlay_3();
2222 	of_unittest_overlay_4();
2223 	of_unittest_overlay_5();
2224 	of_unittest_overlay_6();
2225 	of_unittest_overlay_8();
2226 
2227 	of_unittest_overlay_10();
2228 	of_unittest_overlay_11();
2229 
2230 #if IS_BUILTIN(CONFIG_I2C)
2231 	if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
2232 		goto out;
2233 
2234 	of_unittest_overlay_i2c_12();
2235 	of_unittest_overlay_i2c_13();
2236 	of_unittest_overlay_i2c_14();
2237 	of_unittest_overlay_i2c_15();
2238 
2239 	of_unittest_overlay_i2c_cleanup();
2240 #endif
2241 
2242 	of_unittest_destroy_tracked_overlays();
2243 
2244 out:
2245 	of_node_put(bus_np);
2246 }
2247 
2248 #else
2249 static inline void __init of_unittest_overlay(void) { }
2250 #endif
2251 
2252 #ifdef CONFIG_OF_OVERLAY
2253 
2254 /*
2255  * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2256  * in scripts/Makefile.lib
2257  */
2258 
2259 #define OVERLAY_INFO_EXTERN(name) \
2260 	extern uint8_t __dtb_##name##_begin[]; \
2261 	extern uint8_t __dtb_##name##_end[]
2262 
2263 #define OVERLAY_INFO(overlay_name, expected)             \
2264 {	.dtb_begin       = __dtb_##overlay_name##_begin, \
2265 	.dtb_end         = __dtb_##overlay_name##_end,   \
2266 	.expected_result = expected,                     \
2267 	.name            = #overlay_name,                \
2268 }
2269 
2270 struct overlay_info {
2271 	uint8_t		*dtb_begin;
2272 	uint8_t		*dtb_end;
2273 	int		expected_result;
2274 	int		overlay_id;
2275 	char		*name;
2276 };
2277 
2278 OVERLAY_INFO_EXTERN(overlay_base);
2279 OVERLAY_INFO_EXTERN(overlay);
2280 OVERLAY_INFO_EXTERN(overlay_0);
2281 OVERLAY_INFO_EXTERN(overlay_1);
2282 OVERLAY_INFO_EXTERN(overlay_2);
2283 OVERLAY_INFO_EXTERN(overlay_3);
2284 OVERLAY_INFO_EXTERN(overlay_4);
2285 OVERLAY_INFO_EXTERN(overlay_5);
2286 OVERLAY_INFO_EXTERN(overlay_6);
2287 OVERLAY_INFO_EXTERN(overlay_7);
2288 OVERLAY_INFO_EXTERN(overlay_8);
2289 OVERLAY_INFO_EXTERN(overlay_9);
2290 OVERLAY_INFO_EXTERN(overlay_10);
2291 OVERLAY_INFO_EXTERN(overlay_11);
2292 OVERLAY_INFO_EXTERN(overlay_12);
2293 OVERLAY_INFO_EXTERN(overlay_13);
2294 OVERLAY_INFO_EXTERN(overlay_15);
2295 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
2296 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
2297 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2298 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2299 
2300 /* entries found by name */
2301 static struct overlay_info overlays[] = {
2302 	OVERLAY_INFO(overlay_base, -9999),
2303 	OVERLAY_INFO(overlay, 0),
2304 	OVERLAY_INFO(overlay_0, 0),
2305 	OVERLAY_INFO(overlay_1, 0),
2306 	OVERLAY_INFO(overlay_2, 0),
2307 	OVERLAY_INFO(overlay_3, 0),
2308 	OVERLAY_INFO(overlay_4, 0),
2309 	OVERLAY_INFO(overlay_5, 0),
2310 	OVERLAY_INFO(overlay_6, 0),
2311 	OVERLAY_INFO(overlay_7, 0),
2312 	OVERLAY_INFO(overlay_8, 0),
2313 	OVERLAY_INFO(overlay_9, 0),
2314 	OVERLAY_INFO(overlay_10, 0),
2315 	OVERLAY_INFO(overlay_11, 0),
2316 	OVERLAY_INFO(overlay_12, 0),
2317 	OVERLAY_INFO(overlay_13, 0),
2318 	OVERLAY_INFO(overlay_15, 0),
2319 	OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
2320 	OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
2321 	OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2322 	OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2323 	/* end marker */
2324 	{.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
2325 };
2326 
2327 static struct device_node *overlay_base_root;
2328 
2329 static void * __init dt_alloc_memory(u64 size, u64 align)
2330 {
2331 	void *ptr = memblock_alloc(size, align);
2332 
2333 	if (!ptr)
2334 		panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2335 		      __func__, size, align);
2336 
2337 	return ptr;
2338 }
2339 
2340 /*
2341  * Create base device tree for the overlay unittest.
2342  *
2343  * This is called from very early boot code.
2344  *
2345  * Do as much as possible the same way as done in __unflatten_device_tree
2346  * and other early boot steps for the normal FDT so that the overlay base
2347  * unflattened tree will have the same characteristics as the real tree
2348  * (such as having memory allocated by the early allocator).  The goal
2349  * is to test "the real thing" as much as possible, and test "test setup
2350  * code" as little as possible.
2351  *
2352  * Have to stop before resolving phandles, because that uses kmalloc.
2353  */
2354 void __init unittest_unflatten_overlay_base(void)
2355 {
2356 	struct overlay_info *info;
2357 	u32 data_size;
2358 	void *new_fdt;
2359 	u32 size;
2360 	int found = 0;
2361 	const char *overlay_name = "overlay_base";
2362 
2363 	for (info = overlays; info && info->name; info++) {
2364 		if (!strcmp(overlay_name, info->name)) {
2365 			found = 1;
2366 			break;
2367 		}
2368 	}
2369 	if (!found) {
2370 		pr_err("no overlay data for %s\n", overlay_name);
2371 		return;
2372 	}
2373 
2374 	info = &overlays[0];
2375 
2376 	if (info->expected_result != -9999) {
2377 		pr_err("No dtb 'overlay_base' to attach\n");
2378 		return;
2379 	}
2380 
2381 	data_size = info->dtb_end - info->dtb_begin;
2382 	if (!data_size) {
2383 		pr_err("No dtb 'overlay_base' to attach\n");
2384 		return;
2385 	}
2386 
2387 	size = fdt_totalsize(info->dtb_begin);
2388 	if (size != data_size) {
2389 		pr_err("dtb 'overlay_base' header totalsize != actual size");
2390 		return;
2391 	}
2392 
2393 	new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2394 	if (!new_fdt) {
2395 		pr_err("alloc for dtb 'overlay_base' failed");
2396 		return;
2397 	}
2398 
2399 	memcpy(new_fdt, info->dtb_begin, size);
2400 
2401 	__unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
2402 				dt_alloc_memory, true);
2403 }
2404 
2405 /*
2406  * The purpose of of_unittest_overlay_data_add is to add an
2407  * overlay in the normal fashion.  This is a test of the whole
2408  * picture, instead of testing individual elements.
2409  *
2410  * A secondary purpose is to be able to verify that the contents of
2411  * /proc/device-tree/ contains the updated structure and values from
2412  * the overlay.  That must be verified separately in user space.
2413  *
2414  * Return 0 on unexpected error.
2415  */
2416 static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
2417 {
2418 	struct overlay_info *info;
2419 	int found = 0;
2420 	int ret;
2421 	u32 size;
2422 
2423 	for (info = overlays; info && info->name; info++) {
2424 		if (!strcmp(overlay_name, info->name)) {
2425 			found = 1;
2426 			break;
2427 		}
2428 	}
2429 	if (!found) {
2430 		pr_err("no overlay data for %s\n", overlay_name);
2431 		return 0;
2432 	}
2433 
2434 	size = info->dtb_end - info->dtb_begin;
2435 	if (!size)
2436 		pr_err("no overlay data for %s\n", overlay_name);
2437 
2438 	ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
2439 	if (overlay_id)
2440 		*overlay_id = info->overlay_id;
2441 	if (ret < 0)
2442 		goto out;
2443 
2444 	pr_debug("%s applied\n", overlay_name);
2445 
2446 out:
2447 	if (ret != info->expected_result)
2448 		pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2449 		       info->expected_result, ret, overlay_name);
2450 
2451 	return (ret == info->expected_result);
2452 }
2453 
2454 /*
2455  * The purpose of of_unittest_overlay_high_level is to add an overlay
2456  * in the normal fashion.  This is a test of the whole picture,
2457  * instead of individual elements.
2458  *
2459  * The first part of the function is _not_ normal overlay usage; it is
2460  * finishing splicing the base overlay device tree into the live tree.
2461  */
2462 static __init void of_unittest_overlay_high_level(void)
2463 {
2464 	struct device_node *last_sibling;
2465 	struct device_node *np;
2466 	struct device_node *of_symbols;
2467 	struct device_node *overlay_base_symbols;
2468 	struct device_node **pprev;
2469 	struct property *prop;
2470 
2471 	if (!overlay_base_root) {
2472 		unittest(0, "overlay_base_root not initialized\n");
2473 		return;
2474 	}
2475 
2476 	/*
2477 	 * Could not fixup phandles in unittest_unflatten_overlay_base()
2478 	 * because kmalloc() was not yet available.
2479 	 */
2480 	of_overlay_mutex_lock();
2481 	of_resolve_phandles(overlay_base_root);
2482 	of_overlay_mutex_unlock();
2483 
2484 
2485 	/*
2486 	 * do not allow overlay_base to duplicate any node already in
2487 	 * tree, this greatly simplifies the code
2488 	 */
2489 
2490 	/*
2491 	 * remove overlay_base_root node "__local_fixups", after
2492 	 * being used by of_resolve_phandles()
2493 	 */
2494 	pprev = &overlay_base_root->child;
2495 	for (np = overlay_base_root->child; np; np = np->sibling) {
2496 		if (of_node_name_eq(np, "__local_fixups__")) {
2497 			*pprev = np->sibling;
2498 			break;
2499 		}
2500 		pprev = &np->sibling;
2501 	}
2502 
2503 	/* remove overlay_base_root node "__symbols__" if in live tree */
2504 	of_symbols = of_get_child_by_name(of_root, "__symbols__");
2505 	if (of_symbols) {
2506 		/* will have to graft properties from node into live tree */
2507 		pprev = &overlay_base_root->child;
2508 		for (np = overlay_base_root->child; np; np = np->sibling) {
2509 			if (of_node_name_eq(np, "__symbols__")) {
2510 				overlay_base_symbols = np;
2511 				*pprev = np->sibling;
2512 				break;
2513 			}
2514 			pprev = &np->sibling;
2515 		}
2516 	}
2517 
2518 	for_each_child_of_node(overlay_base_root, np) {
2519 		struct device_node *base_child;
2520 		for_each_child_of_node(of_root, base_child) {
2521 			if (!strcmp(np->full_name, base_child->full_name)) {
2522 				unittest(0, "illegal node name in overlay_base %pOFn",
2523 					 np);
2524 				return;
2525 			}
2526 		}
2527 	}
2528 
2529 	/*
2530 	 * overlay 'overlay_base' is not allowed to have root
2531 	 * properties, so only need to splice nodes into main device tree.
2532 	 *
2533 	 * root node of *overlay_base_root will not be freed, it is lost
2534 	 * memory.
2535 	 */
2536 
2537 	for (np = overlay_base_root->child; np; np = np->sibling)
2538 		np->parent = of_root;
2539 
2540 	mutex_lock(&of_mutex);
2541 
2542 	for (last_sibling = np = of_root->child; np; np = np->sibling)
2543 		last_sibling = np;
2544 
2545 	if (last_sibling)
2546 		last_sibling->sibling = overlay_base_root->child;
2547 	else
2548 		of_root->child = overlay_base_root->child;
2549 
2550 	for_each_of_allnodes_from(overlay_base_root, np)
2551 		__of_attach_node_sysfs(np);
2552 
2553 	if (of_symbols) {
2554 		struct property *new_prop;
2555 		for_each_property_of_node(overlay_base_symbols, prop) {
2556 
2557 			new_prop = __of_prop_dup(prop, GFP_KERNEL);
2558 			if (!new_prop) {
2559 				unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
2560 					 prop->name);
2561 				goto err_unlock;
2562 			}
2563 			if (__of_add_property(of_symbols, new_prop)) {
2564 				/* "name" auto-generated by unflatten */
2565 				if (!strcmp(new_prop->name, "name"))
2566 					continue;
2567 				unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2568 					 prop->name);
2569 				goto err_unlock;
2570 			}
2571 			if (__of_add_property_sysfs(of_symbols, new_prop)) {
2572 				unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2573 					 prop->name);
2574 				goto err_unlock;
2575 			}
2576 		}
2577 	}
2578 
2579 	mutex_unlock(&of_mutex);
2580 
2581 
2582 	/* now do the normal overlay usage test */
2583 
2584 	unittest(overlay_data_apply("overlay", NULL),
2585 		 "Adding overlay 'overlay' failed\n");
2586 
2587 	unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
2588 		 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
2589 
2590 	unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
2591 		 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
2592 
2593 	unittest(overlay_data_apply("overlay_bad_phandle", NULL),
2594 		 "Adding overlay 'overlay_bad_phandle' failed\n");
2595 
2596 	unittest(overlay_data_apply("overlay_bad_symbol", NULL),
2597 		 "Adding overlay 'overlay_bad_symbol' failed\n");
2598 
2599 	return;
2600 
2601 err_unlock:
2602 	mutex_unlock(&of_mutex);
2603 }
2604 
2605 #else
2606 
2607 static inline __init void of_unittest_overlay_high_level(void) {}
2608 
2609 #endif
2610 
2611 static int __init of_unittest(void)
2612 {
2613 	struct device_node *np;
2614 	int res;
2615 
2616 	/* adding data for unittest */
2617 
2618 	if (IS_ENABLED(CONFIG_UML))
2619 		unittest_unflatten_overlay_base();
2620 
2621 	res = unittest_data_add();
2622 	if (res)
2623 		return res;
2624 	if (!of_aliases)
2625 		of_aliases = of_find_node_by_path("/aliases");
2626 
2627 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2628 	if (!np) {
2629 		pr_info("No testcase data in device tree; not running tests\n");
2630 		return 0;
2631 	}
2632 	of_node_put(np);
2633 
2634 	pr_info("start of unittest - you will see error messages\n");
2635 	of_unittest_check_tree_linkage();
2636 	of_unittest_check_phandles();
2637 	of_unittest_find_node_by_name();
2638 	of_unittest_dynamic();
2639 	of_unittest_parse_phandle_with_args();
2640 	of_unittest_parse_phandle_with_args_map();
2641 	of_unittest_printf();
2642 	of_unittest_property_string();
2643 	of_unittest_property_copy();
2644 	of_unittest_changeset();
2645 	of_unittest_parse_interrupts();
2646 	of_unittest_parse_interrupts_extended();
2647 	of_unittest_parse_dma_ranges();
2648 	of_unittest_pci_dma_ranges();
2649 	of_unittest_match_node();
2650 	of_unittest_platform_populate();
2651 	of_unittest_overlay();
2652 
2653 	/* Double check linkage after removing testcase data */
2654 	of_unittest_check_tree_linkage();
2655 
2656 	of_unittest_overlay_high_level();
2657 
2658 	pr_info("end of unittest - %i passed, %i failed\n",
2659 		unittest_results.passed, unittest_results.failed);
2660 
2661 	return 0;
2662 }
2663 late_initcall(of_unittest);
2664