xref: /linux/drivers/of/unittest.c (revision 3652117f854819a148ff0fbe4492587d3520b5e5)
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/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/hashtable.h>
14 #include <linux/libfdt.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 
28 #include <linux/i2c.h>
29 #include <linux/i2c-mux.h>
30 #include <linux/gpio/driver.h>
31 
32 #include <linux/bitops.h>
33 
34 #include "of_private.h"
35 
36 static struct unittest_results {
37 	int passed;
38 	int failed;
39 } unittest_results;
40 
41 #define unittest(result, fmt, ...) ({ \
42 	bool failed = !(result); \
43 	if (failed) { \
44 		unittest_results.failed++; \
45 		pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
46 	} else { \
47 		unittest_results.passed++; \
48 		pr_info("pass %s():%i\n", __func__, __LINE__); \
49 	} \
50 	failed; \
51 })
52 
53 /*
54  * Expected message may have a message level other than KERN_INFO.
55  * Print the expected message only if the current loglevel will allow
56  * the actual message to print.
57  *
58  * Do not use EXPECT_BEGIN(), EXPECT_END(), EXPECT_NOT_BEGIN(), or
59  * EXPECT_NOT_END() to report messages expected to be reported or not
60  * reported by pr_debug().
61  */
62 #define EXPECT_BEGIN(level, fmt, ...) \
63 	printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
64 
65 #define EXPECT_END(level, fmt, ...) \
66 	printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
67 
68 #define EXPECT_NOT_BEGIN(level, fmt, ...) \
69 	printk(level pr_fmt("EXPECT_NOT \\ : ") fmt, ##__VA_ARGS__)
70 
71 #define EXPECT_NOT_END(level, fmt, ...) \
72 	printk(level pr_fmt("EXPECT_NOT / : ") fmt, ##__VA_ARGS__)
73 
74 static void __init of_unittest_find_node_by_name(void)
75 {
76 	struct device_node *np;
77 	const char *options, *name;
78 
79 	np = of_find_node_by_path("/testcase-data");
80 	name = kasprintf(GFP_KERNEL, "%pOF", np);
81 	unittest(np && name && !strcmp("/testcase-data", name),
82 		"find /testcase-data failed\n");
83 	of_node_put(np);
84 	kfree(name);
85 
86 	/* Test if trailing '/' works */
87 	np = of_find_node_by_path("/testcase-data/");
88 	unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
89 
90 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
91 	name = kasprintf(GFP_KERNEL, "%pOF", np);
92 	unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
93 		"find /testcase-data/phandle-tests/consumer-a failed\n");
94 	of_node_put(np);
95 	kfree(name);
96 
97 	np = of_find_node_by_path("testcase-alias");
98 	name = kasprintf(GFP_KERNEL, "%pOF", np);
99 	unittest(np && name && !strcmp("/testcase-data", name),
100 		"find testcase-alias failed\n");
101 	of_node_put(np);
102 	kfree(name);
103 
104 	/* Test if trailing '/' works on aliases */
105 	np = of_find_node_by_path("testcase-alias/");
106 	unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
107 
108 	np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
109 	name = kasprintf(GFP_KERNEL, "%pOF", np);
110 	unittest(np && name && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
111 		"find testcase-alias/phandle-tests/consumer-a failed\n");
112 	of_node_put(np);
113 	kfree(name);
114 
115 	np = of_find_node_by_path("/testcase-data/missing-path");
116 	unittest(!np, "non-existent path returned node %pOF\n", np);
117 	of_node_put(np);
118 
119 	np = of_find_node_by_path("missing-alias");
120 	unittest(!np, "non-existent alias returned node %pOF\n", np);
121 	of_node_put(np);
122 
123 	np = of_find_node_by_path("testcase-alias/missing-path");
124 	unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
125 	of_node_put(np);
126 
127 	np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
128 	unittest(np && !strcmp("testoption", options),
129 		 "option path test failed\n");
130 	of_node_put(np);
131 
132 	np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
133 	unittest(np && !strcmp("test/option", options),
134 		 "option path test, subcase #1 failed\n");
135 	of_node_put(np);
136 
137 	np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
138 	unittest(np && !strcmp("test/option", options),
139 		 "option path test, subcase #2 failed\n");
140 	of_node_put(np);
141 
142 	np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
143 	unittest(np, "NULL option path test failed\n");
144 	of_node_put(np);
145 
146 	np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
147 				       &options);
148 	unittest(np && !strcmp("testaliasoption", options),
149 		 "option alias path test failed\n");
150 	of_node_put(np);
151 
152 	np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
153 				       &options);
154 	unittest(np && !strcmp("test/alias/option", options),
155 		 "option alias path test, subcase #1 failed\n");
156 	of_node_put(np);
157 
158 	np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
159 	unittest(np, "NULL option alias path test failed\n");
160 	of_node_put(np);
161 
162 	options = "testoption";
163 	np = of_find_node_opts_by_path("testcase-alias", &options);
164 	unittest(np && !options, "option clearing test failed\n");
165 	of_node_put(np);
166 
167 	options = "testoption";
168 	np = of_find_node_opts_by_path("/", &options);
169 	unittest(np && !options, "option clearing root node test failed\n");
170 	of_node_put(np);
171 }
172 
173 static void __init of_unittest_dynamic(void)
174 {
175 	struct device_node *np;
176 	struct property *prop;
177 
178 	np = of_find_node_by_path("/testcase-data");
179 	if (!np) {
180 		pr_err("missing testcase data\n");
181 		return;
182 	}
183 
184 	/* Array of 4 properties for the purpose of testing */
185 	prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
186 	if (!prop) {
187 		unittest(0, "kzalloc() failed\n");
188 		return;
189 	}
190 
191 	/* Add a new property - should pass*/
192 	prop->name = "new-property";
193 	prop->value = "new-property-data";
194 	prop->length = strlen(prop->value) + 1;
195 	unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
196 
197 	/* Try to add an existing property - should fail */
198 	prop++;
199 	prop->name = "new-property";
200 	prop->value = "new-property-data-should-fail";
201 	prop->length = strlen(prop->value) + 1;
202 	unittest(of_add_property(np, prop) != 0,
203 		 "Adding an existing property should have failed\n");
204 
205 	/* Try to modify an existing property - should pass */
206 	prop->value = "modify-property-data-should-pass";
207 	prop->length = strlen(prop->value) + 1;
208 	unittest(of_update_property(np, prop) == 0,
209 		 "Updating an existing property should have passed\n");
210 
211 	/* Try to modify non-existent property - should pass*/
212 	prop++;
213 	prop->name = "modify-property";
214 	prop->value = "modify-missing-property-data-should-pass";
215 	prop->length = strlen(prop->value) + 1;
216 	unittest(of_update_property(np, prop) == 0,
217 		 "Updating a missing property should have passed\n");
218 
219 	/* Remove property - should pass */
220 	unittest(of_remove_property(np, prop) == 0,
221 		 "Removing a property should have passed\n");
222 
223 	/* Adding very large property - should pass */
224 	prop++;
225 	prop->name = "large-property-PAGE_SIZEx8";
226 	prop->length = PAGE_SIZE * 8;
227 	prop->value = kzalloc(prop->length, GFP_KERNEL);
228 	unittest(prop->value != NULL, "Unable to allocate large buffer\n");
229 	if (prop->value)
230 		unittest(of_add_property(np, prop) == 0,
231 			 "Adding a large property should have passed\n");
232 }
233 
234 static int __init of_unittest_check_node_linkage(struct device_node *np)
235 {
236 	struct device_node *child;
237 	int count = 0, rc;
238 
239 	for_each_child_of_node(np, child) {
240 		if (child->parent != np) {
241 			pr_err("Child node %pOFn links to wrong parent %pOFn\n",
242 				 child, np);
243 			rc = -EINVAL;
244 			goto put_child;
245 		}
246 
247 		rc = of_unittest_check_node_linkage(child);
248 		if (rc < 0)
249 			goto put_child;
250 		count += rc;
251 	}
252 
253 	return count + 1;
254 put_child:
255 	of_node_put(child);
256 	return rc;
257 }
258 
259 static void __init of_unittest_check_tree_linkage(void)
260 {
261 	struct device_node *np;
262 	int allnode_count = 0, child_count;
263 
264 	if (!of_root)
265 		return;
266 
267 	for_each_of_allnodes(np)
268 		allnode_count++;
269 	child_count = of_unittest_check_node_linkage(of_root);
270 
271 	unittest(child_count > 0, "Device node data structure is corrupted\n");
272 	unittest(child_count == allnode_count,
273 		 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
274 		 allnode_count, child_count);
275 	pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
276 }
277 
278 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
279 					  const char *expected)
280 {
281 	unsigned char *buf;
282 	int buf_size;
283 	int size, i;
284 
285 	buf_size = strlen(expected) + 10;
286 	buf = kmalloc(buf_size, GFP_KERNEL);
287 	if (!buf)
288 		return;
289 
290 	/* Baseline; check conversion with a large size limit */
291 	memset(buf, 0xff, buf_size);
292 	size = snprintf(buf, buf_size - 2, fmt, np);
293 
294 	/* use strcmp() instead of strncmp() here to be absolutely sure strings match */
295 	unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
296 		"sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
297 		fmt, expected, buf);
298 
299 	/* Make sure length limits work */
300 	size++;
301 	for (i = 0; i < 2; i++, size--) {
302 		/* Clear the buffer, and make sure it works correctly still */
303 		memset(buf, 0xff, buf_size);
304 		snprintf(buf, size+1, fmt, np);
305 		unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
306 			"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
307 			size, fmt, expected, buf);
308 	}
309 	kfree(buf);
310 }
311 
312 static void __init of_unittest_printf(void)
313 {
314 	struct device_node *np;
315 	const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
316 	char phandle_str[16] = "";
317 
318 	np = of_find_node_by_path(full_name);
319 	if (!np) {
320 		unittest(np, "testcase data missing\n");
321 		return;
322 	}
323 
324 	num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
325 
326 	of_unittest_printf_one(np, "%pOF",  full_name);
327 	of_unittest_printf_one(np, "%pOFf", full_name);
328 	of_unittest_printf_one(np, "%pOFn", "dev");
329 	of_unittest_printf_one(np, "%2pOFn", "dev");
330 	of_unittest_printf_one(np, "%5pOFn", "  dev");
331 	of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
332 	of_unittest_printf_one(np, "%pOFp", phandle_str);
333 	of_unittest_printf_one(np, "%pOFP", "dev@100");
334 	of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
335 	of_unittest_printf_one(np, "%10pOFP", "   dev@100");
336 	of_unittest_printf_one(np, "%-10pOFP", "dev@100   ");
337 	of_unittest_printf_one(of_root, "%pOFP", "/");
338 	of_unittest_printf_one(np, "%pOFF", "----");
339 	of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
340 	of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
341 	of_unittest_printf_one(np, "%pOFc", "test-sub-device");
342 	of_unittest_printf_one(np, "%pOFC",
343 			"\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
344 }
345 
346 struct node_hash {
347 	struct hlist_node node;
348 	struct device_node *np;
349 };
350 
351 static DEFINE_HASHTABLE(phandle_ht, 8);
352 static void __init of_unittest_check_phandles(void)
353 {
354 	struct device_node *np;
355 	struct node_hash *nh;
356 	struct hlist_node *tmp;
357 	int i, dup_count = 0, phandle_count = 0;
358 
359 	for_each_of_allnodes(np) {
360 		if (!np->phandle)
361 			continue;
362 
363 		hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
364 			if (nh->np->phandle == np->phandle) {
365 				pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
366 					np->phandle, nh->np, np);
367 				dup_count++;
368 				break;
369 			}
370 		}
371 
372 		nh = kzalloc(sizeof(*nh), GFP_KERNEL);
373 		if (!nh)
374 			return;
375 
376 		nh->np = np;
377 		hash_add(phandle_ht, &nh->node, np->phandle);
378 		phandle_count++;
379 	}
380 	unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
381 		 dup_count, phandle_count);
382 
383 	/* Clean up */
384 	hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
385 		hash_del(&nh->node);
386 		kfree(nh);
387 	}
388 }
389 
390 static void __init of_unittest_parse_phandle_with_args(void)
391 {
392 	struct device_node *np;
393 	struct of_phandle_args args;
394 	int i, rc;
395 
396 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
397 	if (!np) {
398 		pr_err("missing testcase data\n");
399 		return;
400 	}
401 
402 	rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
403 	unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
404 
405 	for (i = 0; i < 8; i++) {
406 		bool passed = true;
407 
408 		memset(&args, 0, sizeof(args));
409 		rc = of_parse_phandle_with_args(np, "phandle-list",
410 						"#phandle-cells", i, &args);
411 
412 		/* Test the values from tests-phandle.dtsi */
413 		switch (i) {
414 		case 0:
415 			passed &= !rc;
416 			passed &= (args.args_count == 1);
417 			passed &= (args.args[0] == (i + 1));
418 			break;
419 		case 1:
420 			passed &= !rc;
421 			passed &= (args.args_count == 2);
422 			passed &= (args.args[0] == (i + 1));
423 			passed &= (args.args[1] == 0);
424 			break;
425 		case 2:
426 			passed &= (rc == -ENOENT);
427 			break;
428 		case 3:
429 			passed &= !rc;
430 			passed &= (args.args_count == 3);
431 			passed &= (args.args[0] == (i + 1));
432 			passed &= (args.args[1] == 4);
433 			passed &= (args.args[2] == 3);
434 			break;
435 		case 4:
436 			passed &= !rc;
437 			passed &= (args.args_count == 2);
438 			passed &= (args.args[0] == (i + 1));
439 			passed &= (args.args[1] == 100);
440 			break;
441 		case 5:
442 			passed &= !rc;
443 			passed &= (args.args_count == 0);
444 			break;
445 		case 6:
446 			passed &= !rc;
447 			passed &= (args.args_count == 1);
448 			passed &= (args.args[0] == (i + 1));
449 			break;
450 		case 7:
451 			passed &= (rc == -ENOENT);
452 			break;
453 		default:
454 			passed = false;
455 		}
456 
457 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
458 			 i, args.np, rc);
459 	}
460 
461 	/* Check for missing list property */
462 	memset(&args, 0, sizeof(args));
463 	rc = of_parse_phandle_with_args(np, "phandle-list-missing",
464 					"#phandle-cells", 0, &args);
465 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
466 	rc = of_count_phandle_with_args(np, "phandle-list-missing",
467 					"#phandle-cells");
468 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
469 
470 	/* Check for missing cells property */
471 	memset(&args, 0, sizeof(args));
472 
473 	EXPECT_BEGIN(KERN_INFO,
474 		     "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
475 
476 	rc = of_parse_phandle_with_args(np, "phandle-list",
477 					"#phandle-cells-missing", 0, &args);
478 
479 	EXPECT_END(KERN_INFO,
480 		   "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
481 
482 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
483 
484 	EXPECT_BEGIN(KERN_INFO,
485 		     "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
486 
487 	rc = of_count_phandle_with_args(np, "phandle-list",
488 					"#phandle-cells-missing");
489 
490 	EXPECT_END(KERN_INFO,
491 		   "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
492 
493 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
494 
495 	/* Check for bad phandle in list */
496 	memset(&args, 0, sizeof(args));
497 
498 	EXPECT_BEGIN(KERN_INFO,
499 		     "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
500 
501 	rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
502 					"#phandle-cells", 0, &args);
503 
504 	EXPECT_END(KERN_INFO,
505 		   "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
506 
507 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
508 
509 	EXPECT_BEGIN(KERN_INFO,
510 		     "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
511 
512 	rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
513 					"#phandle-cells");
514 
515 	EXPECT_END(KERN_INFO,
516 		   "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
517 
518 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
519 
520 	/* Check for incorrectly formed argument list */
521 	memset(&args, 0, sizeof(args));
522 
523 	EXPECT_BEGIN(KERN_INFO,
524 		     "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
525 
526 	rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
527 					"#phandle-cells", 1, &args);
528 
529 	EXPECT_END(KERN_INFO,
530 		   "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
531 
532 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
533 
534 	EXPECT_BEGIN(KERN_INFO,
535 		     "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
536 
537 	rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
538 					"#phandle-cells");
539 
540 	EXPECT_END(KERN_INFO,
541 		   "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found 1");
542 
543 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
544 }
545 
546 static void __init of_unittest_parse_phandle_with_args_map(void)
547 {
548 	struct device_node *np, *p0, *p1, *p2, *p3;
549 	struct of_phandle_args args;
550 	int i, rc;
551 
552 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
553 	if (!np) {
554 		pr_err("missing testcase data\n");
555 		return;
556 	}
557 
558 	p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
559 	if (!p0) {
560 		pr_err("missing testcase data\n");
561 		return;
562 	}
563 
564 	p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
565 	if (!p1) {
566 		pr_err("missing testcase data\n");
567 		return;
568 	}
569 
570 	p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
571 	if (!p2) {
572 		pr_err("missing testcase data\n");
573 		return;
574 	}
575 
576 	p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
577 	if (!p3) {
578 		pr_err("missing testcase data\n");
579 		return;
580 	}
581 
582 	rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
583 	unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
584 
585 	for (i = 0; i < 8; i++) {
586 		bool passed = true;
587 
588 		memset(&args, 0, sizeof(args));
589 		rc = of_parse_phandle_with_args_map(np, "phandle-list",
590 						    "phandle", i, &args);
591 
592 		/* Test the values from tests-phandle.dtsi */
593 		switch (i) {
594 		case 0:
595 			passed &= !rc;
596 			passed &= (args.np == p1);
597 			passed &= (args.args_count == 1);
598 			passed &= (args.args[0] == 1);
599 			break;
600 		case 1:
601 			passed &= !rc;
602 			passed &= (args.np == p3);
603 			passed &= (args.args_count == 3);
604 			passed &= (args.args[0] == 2);
605 			passed &= (args.args[1] == 5);
606 			passed &= (args.args[2] == 3);
607 			break;
608 		case 2:
609 			passed &= (rc == -ENOENT);
610 			break;
611 		case 3:
612 			passed &= !rc;
613 			passed &= (args.np == p0);
614 			passed &= (args.args_count == 0);
615 			break;
616 		case 4:
617 			passed &= !rc;
618 			passed &= (args.np == p1);
619 			passed &= (args.args_count == 1);
620 			passed &= (args.args[0] == 3);
621 			break;
622 		case 5:
623 			passed &= !rc;
624 			passed &= (args.np == p0);
625 			passed &= (args.args_count == 0);
626 			break;
627 		case 6:
628 			passed &= !rc;
629 			passed &= (args.np == p2);
630 			passed &= (args.args_count == 2);
631 			passed &= (args.args[0] == 15);
632 			passed &= (args.args[1] == 0x20);
633 			break;
634 		case 7:
635 			passed &= (rc == -ENOENT);
636 			break;
637 		default:
638 			passed = false;
639 		}
640 
641 		unittest(passed, "index %i - data error on node %s rc=%i\n",
642 			 i, args.np->full_name, rc);
643 	}
644 
645 	/* Check for missing list property */
646 	memset(&args, 0, sizeof(args));
647 	rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
648 					    "phandle", 0, &args);
649 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
650 
651 	/* Check for missing cells,map,mask property */
652 	memset(&args, 0, sizeof(args));
653 
654 	EXPECT_BEGIN(KERN_INFO,
655 		     "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
656 
657 	rc = of_parse_phandle_with_args_map(np, "phandle-list",
658 					    "phandle-missing", 0, &args);
659 	EXPECT_END(KERN_INFO,
660 		   "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
661 
662 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
663 
664 	/* Check for bad phandle in list */
665 	memset(&args, 0, sizeof(args));
666 
667 	EXPECT_BEGIN(KERN_INFO,
668 		     "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
669 
670 	rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
671 					    "phandle", 0, &args);
672 	EXPECT_END(KERN_INFO,
673 		   "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle 12345678");
674 
675 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
676 
677 	/* Check for incorrectly formed argument list */
678 	memset(&args, 0, sizeof(args));
679 
680 	EXPECT_BEGIN(KERN_INFO,
681 		     "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
682 
683 	rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
684 					    "phandle", 1, &args);
685 	EXPECT_END(KERN_INFO,
686 		   "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found 1");
687 
688 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
689 }
690 
691 static void __init of_unittest_property_string(void)
692 {
693 	const char *strings[4];
694 	struct device_node *np;
695 	int rc;
696 
697 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
698 	if (!np) {
699 		pr_err("No testcase data in device tree\n");
700 		return;
701 	}
702 
703 	rc = of_property_match_string(np, "phandle-list-names", "first");
704 	unittest(rc == 0, "first expected:0 got:%i\n", rc);
705 	rc = of_property_match_string(np, "phandle-list-names", "second");
706 	unittest(rc == 1, "second expected:1 got:%i\n", rc);
707 	rc = of_property_match_string(np, "phandle-list-names", "third");
708 	unittest(rc == 2, "third expected:2 got:%i\n", rc);
709 	rc = of_property_match_string(np, "phandle-list-names", "fourth");
710 	unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
711 	rc = of_property_match_string(np, "missing-property", "blah");
712 	unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
713 	rc = of_property_match_string(np, "empty-property", "blah");
714 	unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
715 	rc = of_property_match_string(np, "unterminated-string", "blah");
716 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
717 
718 	/* of_property_count_strings() tests */
719 	rc = of_property_count_strings(np, "string-property");
720 	unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
721 	rc = of_property_count_strings(np, "phandle-list-names");
722 	unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
723 	rc = of_property_count_strings(np, "unterminated-string");
724 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
725 	rc = of_property_count_strings(np, "unterminated-string-list");
726 	unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
727 
728 	/* of_property_read_string_index() tests */
729 	rc = of_property_read_string_index(np, "string-property", 0, strings);
730 	unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
731 	strings[0] = NULL;
732 	rc = of_property_read_string_index(np, "string-property", 1, strings);
733 	unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
734 	rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
735 	unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
736 	rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
737 	unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
738 	rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
739 	unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
740 	strings[0] = NULL;
741 	rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
742 	unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
743 	strings[0] = NULL;
744 	rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
745 	unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
746 	rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
747 	unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
748 	strings[0] = NULL;
749 	rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
750 	unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
751 	strings[1] = NULL;
752 
753 	/* of_property_read_string_array() tests */
754 	rc = of_property_read_string_array(np, "string-property", strings, 4);
755 	unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
756 	rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
757 	unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
758 	rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
759 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
760 	/* -- An incorrectly formed string should cause a failure */
761 	rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
762 	unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
763 	/* -- parsing the correctly formed strings should still work: */
764 	strings[2] = NULL;
765 	rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
766 	unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
767 	strings[1] = NULL;
768 	rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
769 	unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
770 }
771 
772 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
773 			(p1)->value && (p2)->value && \
774 			!memcmp((p1)->value, (p2)->value, (p1)->length) && \
775 			!strcmp((p1)->name, (p2)->name))
776 static void __init of_unittest_property_copy(void)
777 {
778 #ifdef CONFIG_OF_DYNAMIC
779 	struct property p1 = { .name = "p1", .length = 0, .value = "" };
780 	struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
781 	struct property *new;
782 
783 	new = __of_prop_dup(&p1, GFP_KERNEL);
784 	unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
785 	kfree(new->value);
786 	kfree(new->name);
787 	kfree(new);
788 
789 	new = __of_prop_dup(&p2, GFP_KERNEL);
790 	unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
791 	kfree(new->value);
792 	kfree(new->name);
793 	kfree(new);
794 #endif
795 }
796 
797 static void __init of_unittest_changeset(void)
798 {
799 #ifdef CONFIG_OF_DYNAMIC
800 	int ret;
801 	struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
802 	struct property *ppname_n1,  pname_n1  = { .name = "name", .length = 3, .value = "n1"  };
803 	struct property *ppname_n2,  pname_n2  = { .name = "name", .length = 3, .value = "n2"  };
804 	struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
805 	struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
806 	struct property *ppremove;
807 	struct device_node *n1, *n2, *n21, *n22, *nchangeset, *nremove, *parent, *np;
808 	static const char * const str_array[] = { "str1", "str2", "str3" };
809 	const u32 u32_array[] = { 1, 2, 3 };
810 	struct of_changeset chgset;
811 	const char *propstr = NULL;
812 
813 	n1 = __of_node_dup(NULL, "n1");
814 	unittest(n1, "testcase setup failure\n");
815 
816 	n2 = __of_node_dup(NULL, "n2");
817 	unittest(n2, "testcase setup failure\n");
818 
819 	n21 = __of_node_dup(NULL, "n21");
820 	unittest(n21, "testcase setup failure %p\n", n21);
821 
822 	nchangeset = of_find_node_by_path("/testcase-data/changeset");
823 	nremove = of_get_child_by_name(nchangeset, "node-remove");
824 	unittest(nremove, "testcase setup failure\n");
825 
826 	ppadd = __of_prop_dup(&padd, GFP_KERNEL);
827 	unittest(ppadd, "testcase setup failure\n");
828 
829 	ppname_n1  = __of_prop_dup(&pname_n1, GFP_KERNEL);
830 	unittest(ppname_n1, "testcase setup failure\n");
831 
832 	ppname_n2  = __of_prop_dup(&pname_n2, GFP_KERNEL);
833 	unittest(ppname_n2, "testcase setup failure\n");
834 
835 	ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
836 	unittest(ppname_n21, "testcase setup failure\n");
837 
838 	ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
839 	unittest(ppupdate, "testcase setup failure\n");
840 
841 	parent = nchangeset;
842 	n1->parent = parent;
843 	n2->parent = parent;
844 	n21->parent = n2;
845 
846 	ppremove = of_find_property(parent, "prop-remove", NULL);
847 	unittest(ppremove, "failed to find removal prop");
848 
849 	of_changeset_init(&chgset);
850 
851 	unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
852 	unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
853 
854 	unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
855 	unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
856 
857 	unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
858 	unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
859 
860 	unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
861 
862 	unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
863 	unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
864 	unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
865 	n22 = of_changeset_create_node(&chgset, n2, "n22");
866 	unittest(n22, "fail create n22\n");
867 	unittest(!of_changeset_add_prop_string(&chgset, n22, "prop-str", "abcd"),
868 		 "fail add prop prop-str");
869 	unittest(!of_changeset_add_prop_string_array(&chgset, n22, "prop-str-array",
870 						     (const char **)str_array,
871 						     ARRAY_SIZE(str_array)),
872 		 "fail add prop prop-str-array");
873 	unittest(!of_changeset_add_prop_u32_array(&chgset, n22, "prop-u32-array",
874 						  u32_array, ARRAY_SIZE(u32_array)),
875 		 "fail add prop prop-u32-array");
876 
877 	unittest(!of_changeset_apply(&chgset), "apply failed\n");
878 
879 	of_node_put(nchangeset);
880 
881 	/* Make sure node names are constructed correctly */
882 	unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
883 		 "'%pOF' not added\n", n21);
884 	of_node_put(np);
885 	unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n22")),
886 		 "'%pOF' not added\n", n22);
887 	of_node_put(np);
888 
889 	unittest(!of_changeset_revert(&chgset), "revert failed\n");
890 
891 	unittest(!of_find_node_by_path("/testcase-data/changeset/n2/n21"),
892 		 "'%pOF' still present after revert\n", n21);
893 
894 	ppremove = of_find_property(parent, "prop-remove", NULL);
895 	unittest(ppremove, "failed to find removed prop after revert\n");
896 
897 	ret = of_property_read_string(parent, "prop-update", &propstr);
898 	unittest(!ret, "failed to find updated prop after revert\n");
899 	if (!ret)
900 		unittest(strcmp(propstr, "hello") == 0, "original value not in updated property after revert");
901 
902 	of_changeset_destroy(&chgset);
903 
904 	of_node_put(n1);
905 	of_node_put(n2);
906 	of_node_put(n21);
907 	of_node_put(n22);
908 #endif
909 }
910 
911 static void __init of_unittest_dma_get_max_cpu_address(void)
912 {
913 	struct device_node *np;
914 	phys_addr_t cpu_addr;
915 
916 	if (!IS_ENABLED(CONFIG_OF_ADDRESS))
917 		return;
918 
919 	np = of_find_node_by_path("/testcase-data/address-tests");
920 	if (!np) {
921 		pr_err("missing testcase data\n");
922 		return;
923 	}
924 
925 	cpu_addr = of_dma_get_max_cpu_address(np);
926 	unittest(cpu_addr == 0x4fffffff,
927 		 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
928 		 &cpu_addr, 0x4fffffff);
929 }
930 
931 static void __init of_unittest_dma_ranges_one(const char *path,
932 		u64 expect_dma_addr, u64 expect_paddr)
933 {
934 #ifdef CONFIG_HAS_DMA
935 	struct device_node *np;
936 	const struct bus_dma_region *map = NULL;
937 	int rc;
938 
939 	np = of_find_node_by_path(path);
940 	if (!np) {
941 		pr_err("missing testcase data\n");
942 		return;
943 	}
944 
945 	rc = of_dma_get_range(np, &map);
946 
947 	unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
948 
949 	if (!rc) {
950 		phys_addr_t	paddr;
951 		dma_addr_t	dma_addr;
952 		struct device	*dev_bogus;
953 
954 		dev_bogus = kzalloc(sizeof(struct device), GFP_KERNEL);
955 		if (!dev_bogus) {
956 			unittest(0, "kzalloc() failed\n");
957 			kfree(map);
958 			return;
959 		}
960 
961 		dev_bogus->dma_range_map = map;
962 		paddr = dma_to_phys(dev_bogus, expect_dma_addr);
963 		dma_addr = phys_to_dma(dev_bogus, expect_paddr);
964 
965 		unittest(paddr == expect_paddr,
966 			 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
967 			 &paddr, expect_paddr, np);
968 		unittest(dma_addr == expect_dma_addr,
969 			 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
970 			 &dma_addr, expect_dma_addr, np);
971 
972 		kfree(map);
973 		kfree(dev_bogus);
974 	}
975 	of_node_put(np);
976 #endif
977 }
978 
979 static void __init of_unittest_parse_dma_ranges(void)
980 {
981 	of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
982 		0x0, 0x20000000);
983 	if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
984 		of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
985 			0x100000000, 0x20000000);
986 	of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
987 		0x80000000, 0x20000000);
988 }
989 
990 static void __init of_unittest_pci_dma_ranges(void)
991 {
992 	struct device_node *np;
993 	struct of_pci_range range;
994 	struct of_pci_range_parser parser;
995 	int i = 0;
996 
997 	if (!IS_ENABLED(CONFIG_PCI))
998 		return;
999 
1000 	np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
1001 	if (!np) {
1002 		pr_err("missing testcase data\n");
1003 		return;
1004 	}
1005 
1006 	if (of_pci_dma_range_parser_init(&parser, np)) {
1007 		pr_err("missing dma-ranges property\n");
1008 		return;
1009 	}
1010 
1011 	/*
1012 	 * Get the dma-ranges from the device tree
1013 	 */
1014 	for_each_of_pci_range(&parser, &range) {
1015 		if (!i) {
1016 			unittest(range.size == 0x10000000,
1017 				 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1018 				 np, range.size);
1019 			unittest(range.cpu_addr == 0x20000000,
1020 				 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1021 				 range.cpu_addr, np);
1022 			unittest(range.pci_addr == 0x80000000,
1023 				 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1024 				 range.pci_addr, np);
1025 		} else {
1026 			unittest(range.size == 0x10000000,
1027 				 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
1028 				 np, range.size);
1029 			unittest(range.cpu_addr == 0x40000000,
1030 				 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
1031 				 range.cpu_addr, np);
1032 			unittest(range.pci_addr == 0xc0000000,
1033 				 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
1034 				 range.pci_addr, np);
1035 		}
1036 		i++;
1037 	}
1038 
1039 	of_node_put(np);
1040 }
1041 
1042 static void __init of_unittest_bus_ranges(void)
1043 {
1044 	struct device_node *np;
1045 	struct of_range range;
1046 	struct of_range_parser parser;
1047 	struct resource res;
1048 	int ret, count, i = 0;
1049 
1050 	np = of_find_node_by_path("/testcase-data/address-tests");
1051 	if (!np) {
1052 		pr_err("missing testcase data\n");
1053 		return;
1054 	}
1055 
1056 	if (of_range_parser_init(&parser, np)) {
1057 		pr_err("missing ranges property\n");
1058 		return;
1059 	}
1060 
1061 	ret = of_range_to_resource(np, 1, &res);
1062 	unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1063 		ret, np);
1064 	unittest(resource_type(&res) == IORESOURCE_MEM,
1065 		"of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1066 		np, &res);
1067 	unittest(res.start == 0xd0000000,
1068 		"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1069 		np, &res);
1070 	unittest(resource_size(&res) == 0x20000000,
1071 		"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1072 		np, &res);
1073 
1074 	count = of_range_count(&parser);
1075 	unittest(count == 2,
1076 		"of_range_count wrong size on node %pOF count=%d\n",
1077 		np, count);
1078 
1079 	/*
1080 	 * Get the "ranges" from the device tree
1081 	 */
1082 	for_each_of_range(&parser, &range) {
1083 		unittest(range.flags == IORESOURCE_MEM,
1084 			"for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1085 			np, range.flags, IORESOURCE_MEM);
1086 		if (!i) {
1087 			unittest(range.size == 0x50000000,
1088 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1089 				 np, range.size);
1090 			unittest(range.cpu_addr == 0x70000000,
1091 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1092 				 range.cpu_addr, np);
1093 			unittest(range.bus_addr == 0x70000000,
1094 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1095 				 range.pci_addr, np);
1096 		} else {
1097 			unittest(range.size == 0x20000000,
1098 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1099 				 np, range.size);
1100 			unittest(range.cpu_addr == 0xd0000000,
1101 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1102 				 range.cpu_addr, np);
1103 			unittest(range.bus_addr == 0x00000000,
1104 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1105 				 range.pci_addr, np);
1106 		}
1107 		i++;
1108 	}
1109 
1110 	of_node_put(np);
1111 }
1112 
1113 static void __init of_unittest_bus_3cell_ranges(void)
1114 {
1115 	struct device_node *np;
1116 	struct of_range range;
1117 	struct of_range_parser parser;
1118 	int i = 0;
1119 
1120 	np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1121 	if (!np) {
1122 		pr_err("missing testcase data\n");
1123 		return;
1124 	}
1125 
1126 	if (of_range_parser_init(&parser, np)) {
1127 		pr_err("missing ranges property\n");
1128 		return;
1129 	}
1130 
1131 	/*
1132 	 * Get the "ranges" from the device tree
1133 	 */
1134 	for_each_of_range(&parser, &range) {
1135 		if (!i) {
1136 			unittest(range.flags == 0xf00baa,
1137 				 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1138 				 np, range.flags);
1139 			unittest(range.size == 0x100000,
1140 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1141 				 np, range.size);
1142 			unittest(range.cpu_addr == 0xa0000000,
1143 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1144 				 range.cpu_addr, np);
1145 			unittest(range.bus_addr == 0x0,
1146 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1147 				 range.pci_addr, np);
1148 		} else {
1149 			unittest(range.flags == 0xf00bee,
1150 				 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1151 				 np, range.flags);
1152 			unittest(range.size == 0x200000,
1153 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1154 				 np, range.size);
1155 			unittest(range.cpu_addr == 0xb0000000,
1156 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1157 				 range.cpu_addr, np);
1158 			unittest(range.bus_addr == 0x100000000,
1159 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1160 				 range.pci_addr, np);
1161 		}
1162 		i++;
1163 	}
1164 
1165 	of_node_put(np);
1166 }
1167 
1168 static void __init of_unittest_reg(void)
1169 {
1170 	struct device_node *np;
1171 	int ret;
1172 	u64 addr, size;
1173 
1174 	np = of_find_node_by_path("/testcase-data/address-tests/bus@80000000/device@1000");
1175 	if (!np) {
1176 		pr_err("missing testcase data\n");
1177 		return;
1178 	}
1179 
1180 	ret = of_property_read_reg(np, 0, &addr, &size);
1181 	unittest(!ret, "of_property_read_reg(%pOF) returned error %d\n",
1182 		np, ret);
1183 	unittest(addr == 0x1000, "of_property_read_reg(%pOF) untranslated address (%llx) incorrect\n",
1184 		np, addr);
1185 
1186 	of_node_put(np);
1187 }
1188 
1189 struct of_unittest_expected_res {
1190 	int index;
1191 	struct resource res;
1192 };
1193 
1194 static void __init of_unittest_check_addr(const char *node_path,
1195 					  const struct of_unittest_expected_res *tab_exp,
1196 					  unsigned int tab_exp_count)
1197 {
1198 	const struct of_unittest_expected_res *expected;
1199 	struct device_node *np;
1200 	struct resource res;
1201 	unsigned int count;
1202 	int ret;
1203 
1204 	if (!IS_ENABLED(CONFIG_OF_ADDRESS))
1205 		return;
1206 
1207 	np = of_find_node_by_path(node_path);
1208 	if (!np) {
1209 		pr_err("missing testcase data (%s)\n", node_path);
1210 		return;
1211 	}
1212 
1213 	expected = tab_exp;
1214 	count = tab_exp_count;
1215 	while (count--) {
1216 		ret = of_address_to_resource(np, expected->index, &res);
1217 		unittest(!ret, "of_address_to_resource(%pOF, %d) returned error %d\n",
1218 			 np, expected->index, ret);
1219 		unittest(resource_type(&res) == resource_type(&expected->res) &&
1220 			 res.start == expected->res.start &&
1221 			 resource_size(&res) == resource_size(&expected->res),
1222 			"of_address_to_resource(%pOF, %d) wrong resource %pR, expected %pR\n",
1223 			np, expected->index, &res, &expected->res);
1224 		expected++;
1225 	}
1226 
1227 	of_node_put(np);
1228 }
1229 
1230 static const struct of_unittest_expected_res of_unittest_reg_2cell_expected_res[] = {
1231 	{.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1232 	{.index = 1, .res = DEFINE_RES_MEM(0xa0a02000, 0x100) },
1233 	{.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1234 	{.index = 3, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1235 };
1236 
1237 static const struct of_unittest_expected_res of_unittest_reg_3cell_expected_res[] = {
1238 	{.index = 0, .res = DEFINE_RES_MEM(0xa0a01000, 0x100) },
1239 	{.index = 1, .res = DEFINE_RES_MEM(0xa0b02000, 0x100) },
1240 	{.index = 2, .res = DEFINE_RES_MEM(0xc0c01000, 0x100) },
1241 	{.index = 3, .res = DEFINE_RES_MEM(0xc0c09000, 0x100) },
1242 	{.index = 4, .res = DEFINE_RES_MEM(0xd0d01000, 0x100) },
1243 };
1244 
1245 static const struct of_unittest_expected_res of_unittest_reg_pci_expected_res[] = {
1246 	{.index = 0, .res = DEFINE_RES_MEM(0xe8001000, 0x1000) },
1247 	{.index = 1, .res = DEFINE_RES_MEM(0xea002000, 0x2000) },
1248 };
1249 
1250 static void __init of_unittest_translate_addr(void)
1251 {
1252 	of_unittest_check_addr("/testcase-data/address-tests2/bus-2cell@10000000/device@100000",
1253 			       of_unittest_reg_2cell_expected_res,
1254 			       ARRAY_SIZE(of_unittest_reg_2cell_expected_res));
1255 
1256 	of_unittest_check_addr("/testcase-data/address-tests2/bus-3cell@20000000/local-bus@100000/device@f1001000",
1257 			       of_unittest_reg_3cell_expected_res,
1258 			       ARRAY_SIZE(of_unittest_reg_3cell_expected_res));
1259 
1260 	of_unittest_check_addr("/testcase-data/address-tests2/pcie@d1070000/pci@0,0/dev@0,0/local-bus@0/dev@e0000000",
1261 			       of_unittest_reg_pci_expected_res,
1262 			       ARRAY_SIZE(of_unittest_reg_pci_expected_res));
1263 }
1264 
1265 static void __init of_unittest_parse_interrupts(void)
1266 {
1267 	struct device_node *np;
1268 	struct of_phandle_args args;
1269 	int i, rc;
1270 
1271 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1272 		return;
1273 
1274 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1275 	if (!np) {
1276 		pr_err("missing testcase data\n");
1277 		return;
1278 	}
1279 
1280 	for (i = 0; i < 4; i++) {
1281 		bool passed = true;
1282 
1283 		memset(&args, 0, sizeof(args));
1284 		rc = of_irq_parse_one(np, i, &args);
1285 
1286 		passed &= !rc;
1287 		passed &= (args.args_count == 1);
1288 		passed &= (args.args[0] == (i + 1));
1289 
1290 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1291 			 i, args.np, rc);
1292 	}
1293 	of_node_put(np);
1294 
1295 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1296 	if (!np) {
1297 		pr_err("missing testcase data\n");
1298 		return;
1299 	}
1300 
1301 	for (i = 0; i < 4; i++) {
1302 		bool passed = true;
1303 
1304 		memset(&args, 0, sizeof(args));
1305 		rc = of_irq_parse_one(np, i, &args);
1306 
1307 		/* Test the values from tests-phandle.dtsi */
1308 		switch (i) {
1309 		case 0:
1310 			passed &= !rc;
1311 			passed &= (args.args_count == 1);
1312 			passed &= (args.args[0] == 9);
1313 			break;
1314 		case 1:
1315 			passed &= !rc;
1316 			passed &= (args.args_count == 3);
1317 			passed &= (args.args[0] == 10);
1318 			passed &= (args.args[1] == 11);
1319 			passed &= (args.args[2] == 12);
1320 			break;
1321 		case 2:
1322 			passed &= !rc;
1323 			passed &= (args.args_count == 2);
1324 			passed &= (args.args[0] == 13);
1325 			passed &= (args.args[1] == 14);
1326 			break;
1327 		case 3:
1328 			passed &= !rc;
1329 			passed &= (args.args_count == 2);
1330 			passed &= (args.args[0] == 15);
1331 			passed &= (args.args[1] == 16);
1332 			break;
1333 		default:
1334 			passed = false;
1335 		}
1336 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1337 			 i, args.np, rc);
1338 	}
1339 	of_node_put(np);
1340 }
1341 
1342 static void __init of_unittest_parse_interrupts_extended(void)
1343 {
1344 	struct device_node *np;
1345 	struct of_phandle_args args;
1346 	int i, rc;
1347 
1348 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1349 		return;
1350 
1351 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1352 	if (!np) {
1353 		pr_err("missing testcase data\n");
1354 		return;
1355 	}
1356 
1357 	for (i = 0; i < 7; i++) {
1358 		bool passed = true;
1359 
1360 		memset(&args, 0, sizeof(args));
1361 		rc = of_irq_parse_one(np, i, &args);
1362 
1363 		/* Test the values from tests-phandle.dtsi */
1364 		switch (i) {
1365 		case 0:
1366 			passed &= !rc;
1367 			passed &= (args.args_count == 1);
1368 			passed &= (args.args[0] == 1);
1369 			break;
1370 		case 1:
1371 			passed &= !rc;
1372 			passed &= (args.args_count == 3);
1373 			passed &= (args.args[0] == 2);
1374 			passed &= (args.args[1] == 3);
1375 			passed &= (args.args[2] == 4);
1376 			break;
1377 		case 2:
1378 			passed &= !rc;
1379 			passed &= (args.args_count == 2);
1380 			passed &= (args.args[0] == 5);
1381 			passed &= (args.args[1] == 6);
1382 			break;
1383 		case 3:
1384 			passed &= !rc;
1385 			passed &= (args.args_count == 1);
1386 			passed &= (args.args[0] == 9);
1387 			break;
1388 		case 4:
1389 			passed &= !rc;
1390 			passed &= (args.args_count == 3);
1391 			passed &= (args.args[0] == 10);
1392 			passed &= (args.args[1] == 11);
1393 			passed &= (args.args[2] == 12);
1394 			break;
1395 		case 5:
1396 			passed &= !rc;
1397 			passed &= (args.args_count == 2);
1398 			passed &= (args.args[0] == 13);
1399 			passed &= (args.args[1] == 14);
1400 			break;
1401 		case 6:
1402 			/*
1403 			 * Tests child node that is missing property
1404 			 * #address-cells.  See the comments in
1405 			 * drivers/of/unittest-data/tests-interrupts.dtsi
1406 			 * nodes intmap1 and interrupts-extended0
1407 			 */
1408 			passed &= !rc;
1409 			passed &= (args.args_count == 1);
1410 			passed &= (args.args[0] == 15);
1411 			break;
1412 		default:
1413 			passed = false;
1414 		}
1415 
1416 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1417 			 i, args.np, rc);
1418 	}
1419 	of_node_put(np);
1420 }
1421 
1422 static const struct of_device_id match_node_table[] = {
1423 	{ .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1424 	{ .data = "B", .type = "type1", }, /* followed by type alone */
1425 
1426 	{ .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1427 	{ .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1428 	{ .data = "Cc", .name = "name2", .type = "type2", },
1429 
1430 	{ .data = "E", .compatible = "compat3" },
1431 	{ .data = "G", .compatible = "compat2", },
1432 	{ .data = "H", .compatible = "compat2", .name = "name5", },
1433 	{ .data = "I", .compatible = "compat2", .type = "type1", },
1434 	{ .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1435 	{ .data = "K", .compatible = "compat2", .name = "name9", },
1436 	{}
1437 };
1438 
1439 static struct {
1440 	const char *path;
1441 	const char *data;
1442 } match_node_tests[] = {
1443 	{ .path = "/testcase-data/match-node/name0", .data = "A", },
1444 	{ .path = "/testcase-data/match-node/name1", .data = "B", },
1445 	{ .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1446 	{ .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1447 	{ .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1448 	{ .path = "/testcase-data/match-node/name3", .data = "E", },
1449 	{ .path = "/testcase-data/match-node/name4", .data = "G", },
1450 	{ .path = "/testcase-data/match-node/name5", .data = "H", },
1451 	{ .path = "/testcase-data/match-node/name6", .data = "G", },
1452 	{ .path = "/testcase-data/match-node/name7", .data = "I", },
1453 	{ .path = "/testcase-data/match-node/name8", .data = "J", },
1454 	{ .path = "/testcase-data/match-node/name9", .data = "K", },
1455 };
1456 
1457 static void __init of_unittest_match_node(void)
1458 {
1459 	struct device_node *np;
1460 	const struct of_device_id *match;
1461 	int i;
1462 
1463 	for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1464 		np = of_find_node_by_path(match_node_tests[i].path);
1465 		if (!np) {
1466 			unittest(0, "missing testcase node %s\n",
1467 				match_node_tests[i].path);
1468 			continue;
1469 		}
1470 
1471 		match = of_match_node(match_node_table, np);
1472 		if (!match) {
1473 			unittest(0, "%s didn't match anything\n",
1474 				match_node_tests[i].path);
1475 			continue;
1476 		}
1477 
1478 		if (strcmp(match->data, match_node_tests[i].data) != 0) {
1479 			unittest(0, "%s got wrong match. expected %s, got %s\n",
1480 				match_node_tests[i].path, match_node_tests[i].data,
1481 				(const char *)match->data);
1482 			continue;
1483 		}
1484 		unittest(1, "passed");
1485 	}
1486 }
1487 
1488 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1489 static const struct platform_device_info test_bus_info = {
1490 	.name = "unittest-bus",
1491 };
1492 static void __init of_unittest_platform_populate(void)
1493 {
1494 	int irq, rc;
1495 	struct device_node *np, *child, *grandchild;
1496 	struct platform_device *pdev, *test_bus;
1497 	const struct of_device_id match[] = {
1498 		{ .compatible = "test-device", },
1499 		{}
1500 	};
1501 
1502 	np = of_find_node_by_path("/testcase-data");
1503 	of_platform_default_populate(np, NULL, NULL);
1504 
1505 	/* Test that a missing irq domain returns -EPROBE_DEFER */
1506 	np = of_find_node_by_path("/testcase-data/testcase-device1");
1507 	pdev = of_find_device_by_node(np);
1508 	unittest(pdev, "device 1 creation failed\n");
1509 
1510 	if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1511 		irq = platform_get_irq(pdev, 0);
1512 		unittest(irq == -EPROBE_DEFER,
1513 			 "device deferred probe failed - %d\n", irq);
1514 
1515 		/* Test that a parsing failure does not return -EPROBE_DEFER */
1516 		np = of_find_node_by_path("/testcase-data/testcase-device2");
1517 		pdev = of_find_device_by_node(np);
1518 		unittest(pdev, "device 2 creation failed\n");
1519 
1520 		EXPECT_BEGIN(KERN_INFO,
1521 			     "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1522 
1523 		irq = platform_get_irq(pdev, 0);
1524 
1525 		EXPECT_END(KERN_INFO,
1526 			   "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1527 
1528 		unittest(irq < 0 && irq != -EPROBE_DEFER,
1529 			 "device parsing error failed - %d\n", irq);
1530 	}
1531 
1532 	np = of_find_node_by_path("/testcase-data/platform-tests");
1533 	unittest(np, "No testcase data in device tree\n");
1534 	if (!np)
1535 		return;
1536 
1537 	test_bus = platform_device_register_full(&test_bus_info);
1538 	rc = PTR_ERR_OR_ZERO(test_bus);
1539 	unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1540 	if (rc) {
1541 		of_node_put(np);
1542 		return;
1543 	}
1544 	test_bus->dev.of_node = np;
1545 
1546 	/*
1547 	 * Add a dummy resource to the test bus node after it is
1548 	 * registered to catch problems with un-inserted resources. The
1549 	 * DT code doesn't insert the resources, and it has caused the
1550 	 * kernel to oops in the past. This makes sure the same bug
1551 	 * doesn't crop up again.
1552 	 */
1553 	platform_device_add_resources(test_bus, &test_bus_res, 1);
1554 
1555 	of_platform_populate(np, match, NULL, &test_bus->dev);
1556 	for_each_child_of_node(np, child) {
1557 		for_each_child_of_node(child, grandchild) {
1558 			pdev = of_find_device_by_node(grandchild);
1559 			unittest(pdev,
1560 				 "Could not create device for node '%pOFn'\n",
1561 				 grandchild);
1562 			platform_device_put(pdev);
1563 		}
1564 	}
1565 
1566 	of_platform_depopulate(&test_bus->dev);
1567 	for_each_child_of_node(np, child) {
1568 		for_each_child_of_node(child, grandchild)
1569 			unittest(!of_find_device_by_node(grandchild),
1570 				 "device didn't get destroyed '%pOFn'\n",
1571 				 grandchild);
1572 	}
1573 
1574 	platform_device_unregister(test_bus);
1575 	of_node_put(np);
1576 }
1577 
1578 /**
1579  *	update_node_properties - adds the properties
1580  *	of np into dup node (present in live tree) and
1581  *	updates parent of children of np to dup.
1582  *
1583  *	@np:	node whose properties are being added to the live tree
1584  *	@dup:	node present in live tree to be updated
1585  */
1586 static void update_node_properties(struct device_node *np,
1587 					struct device_node *dup)
1588 {
1589 	struct property *prop;
1590 	struct property *save_next;
1591 	struct device_node *child;
1592 	int ret;
1593 
1594 	for_each_child_of_node(np, child)
1595 		child->parent = dup;
1596 
1597 	/*
1598 	 * "unittest internal error: unable to add testdata property"
1599 	 *
1600 	 *    If this message reports a property in node '/__symbols__' then
1601 	 *    the respective unittest overlay contains a label that has the
1602 	 *    same name as a label in the live devicetree.  The label will
1603 	 *    be in the live devicetree only if the devicetree source was
1604 	 *    compiled with the '-@' option.  If you encounter this error,
1605 	 *    please consider renaming __all__ of the labels in the unittest
1606 	 *    overlay dts files with an odd prefix that is unlikely to be
1607 	 *    used in a real devicetree.
1608 	 */
1609 
1610 	/*
1611 	 * open code for_each_property_of_node() because of_add_property()
1612 	 * sets prop->next to NULL
1613 	 */
1614 	for (prop = np->properties; prop != NULL; prop = save_next) {
1615 		save_next = prop->next;
1616 		ret = of_add_property(dup, prop);
1617 		if (ret) {
1618 			if (ret == -EEXIST && !strcmp(prop->name, "name"))
1619 				continue;
1620 			pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1621 			       np, prop->name);
1622 		}
1623 	}
1624 }
1625 
1626 /**
1627  *	attach_node_and_children - attaches nodes
1628  *	and its children to live tree.
1629  *	CAUTION: misleading function name - if node @np already exists in
1630  *	the live tree then children of @np are *not* attached to the live
1631  *	tree.  This works for the current test devicetree nodes because such
1632  *	nodes do not have child nodes.
1633  *
1634  *	@np:	Node to attach to live tree
1635  */
1636 static void attach_node_and_children(struct device_node *np)
1637 {
1638 	struct device_node *next, *dup, *child;
1639 	unsigned long flags;
1640 	const char *full_name;
1641 
1642 	full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1643 	if (!full_name)
1644 		return;
1645 
1646 	if (!strcmp(full_name, "/__local_fixups__") ||
1647 	    !strcmp(full_name, "/__fixups__")) {
1648 		kfree(full_name);
1649 		return;
1650 	}
1651 
1652 	dup = of_find_node_by_path(full_name);
1653 	kfree(full_name);
1654 	if (dup) {
1655 		update_node_properties(np, dup);
1656 		return;
1657 	}
1658 
1659 	child = np->child;
1660 	np->child = NULL;
1661 
1662 	mutex_lock(&of_mutex);
1663 	raw_spin_lock_irqsave(&devtree_lock, flags);
1664 	np->sibling = np->parent->child;
1665 	np->parent->child = np;
1666 	of_node_clear_flag(np, OF_DETACHED);
1667 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1668 
1669 	__of_attach_node_sysfs(np);
1670 	mutex_unlock(&of_mutex);
1671 
1672 	while (child) {
1673 		next = child->sibling;
1674 		attach_node_and_children(child);
1675 		child = next;
1676 	}
1677 }
1678 
1679 /**
1680  *	unittest_data_add - Reads, copies data from
1681  *	linked tree and attaches it to the live tree
1682  */
1683 static int __init unittest_data_add(void)
1684 {
1685 	void *unittest_data;
1686 	void *unittest_data_align;
1687 	struct device_node *unittest_data_node = NULL, *np;
1688 	/*
1689 	 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1690 	 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
1691 	 */
1692 	extern uint8_t __dtbo_testcases_begin[];
1693 	extern uint8_t __dtbo_testcases_end[];
1694 	const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
1695 	int rc;
1696 	void *ret;
1697 
1698 	if (!size) {
1699 		pr_warn("%s: testcases is empty\n", __func__);
1700 		return -ENODATA;
1701 	}
1702 
1703 	/* creating copy */
1704 	unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
1705 	if (!unittest_data)
1706 		return -ENOMEM;
1707 
1708 	unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1709 	memcpy(unittest_data_align, __dtbo_testcases_begin, size);
1710 
1711 	ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
1712 	if (!ret) {
1713 		pr_warn("%s: unflatten testcases tree failed\n", __func__);
1714 		kfree(unittest_data);
1715 		return -ENODATA;
1716 	}
1717 	if (!unittest_data_node) {
1718 		pr_warn("%s: testcases tree is empty\n", __func__);
1719 		kfree(unittest_data);
1720 		return -ENODATA;
1721 	}
1722 
1723 	/*
1724 	 * This lock normally encloses of_resolve_phandles()
1725 	 */
1726 	of_overlay_mutex_lock();
1727 
1728 	rc = of_resolve_phandles(unittest_data_node);
1729 	if (rc) {
1730 		pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1731 		of_overlay_mutex_unlock();
1732 		return -EINVAL;
1733 	}
1734 
1735 	if (!of_root) {
1736 		of_root = unittest_data_node;
1737 		for_each_of_allnodes(np)
1738 			__of_attach_node_sysfs(np);
1739 		of_aliases = of_find_node_by_path("/aliases");
1740 		of_chosen = of_find_node_by_path("/chosen");
1741 		of_overlay_mutex_unlock();
1742 		return 0;
1743 	}
1744 
1745 	EXPECT_BEGIN(KERN_INFO,
1746 		     "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1747 
1748 	/* attach the sub-tree to live tree */
1749 	np = unittest_data_node->child;
1750 	while (np) {
1751 		struct device_node *next = np->sibling;
1752 
1753 		np->parent = of_root;
1754 		/* this will clear OF_DETACHED in np and children */
1755 		attach_node_and_children(np);
1756 		np = next;
1757 	}
1758 
1759 	EXPECT_END(KERN_INFO,
1760 		   "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1761 
1762 	of_overlay_mutex_unlock();
1763 
1764 	return 0;
1765 }
1766 
1767 #ifdef CONFIG_OF_OVERLAY
1768 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
1769 
1770 static int unittest_probe(struct platform_device *pdev)
1771 {
1772 	struct device *dev = &pdev->dev;
1773 	struct device_node *np = dev->of_node;
1774 
1775 	if (np == NULL) {
1776 		dev_err(dev, "No OF data for device\n");
1777 		return -EINVAL;
1778 
1779 	}
1780 
1781 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1782 
1783 	of_platform_populate(np, NULL, NULL, &pdev->dev);
1784 
1785 	return 0;
1786 }
1787 
1788 static void unittest_remove(struct platform_device *pdev)
1789 {
1790 	struct device *dev = &pdev->dev;
1791 	struct device_node *np = dev->of_node;
1792 
1793 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1794 }
1795 
1796 static const struct of_device_id unittest_match[] = {
1797 	{ .compatible = "unittest", },
1798 	{},
1799 };
1800 
1801 static struct platform_driver unittest_driver = {
1802 	.probe			= unittest_probe,
1803 	.remove_new		= unittest_remove,
1804 	.driver = {
1805 		.name		= "unittest",
1806 		.of_match_table	= unittest_match,
1807 	},
1808 };
1809 
1810 /* get the platform device instantiated at the path */
1811 static struct platform_device *of_path_to_platform_device(const char *path)
1812 {
1813 	struct device_node *np;
1814 	struct platform_device *pdev;
1815 
1816 	np = of_find_node_by_path(path);
1817 	if (np == NULL)
1818 		return NULL;
1819 
1820 	pdev = of_find_device_by_node(np);
1821 	of_node_put(np);
1822 
1823 	return pdev;
1824 }
1825 
1826 /* find out if a platform device exists at that path */
1827 static int of_path_platform_device_exists(const char *path)
1828 {
1829 	struct platform_device *pdev;
1830 
1831 	pdev = of_path_to_platform_device(path);
1832 	platform_device_put(pdev);
1833 	return pdev != NULL;
1834 }
1835 
1836 #ifdef CONFIG_OF_GPIO
1837 
1838 struct unittest_gpio_dev {
1839 	struct gpio_chip chip;
1840 };
1841 
1842 static int unittest_gpio_chip_request_count;
1843 static int unittest_gpio_probe_count;
1844 static int unittest_gpio_probe_pass_count;
1845 
1846 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1847 {
1848 	unittest_gpio_chip_request_count++;
1849 
1850 	pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1851 		 unittest_gpio_chip_request_count);
1852 	return 0;
1853 }
1854 
1855 static int unittest_gpio_probe(struct platform_device *pdev)
1856 {
1857 	struct unittest_gpio_dev *devptr;
1858 	int ret;
1859 
1860 	unittest_gpio_probe_count++;
1861 
1862 	devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1863 	if (!devptr)
1864 		return -ENOMEM;
1865 
1866 	platform_set_drvdata(pdev, devptr);
1867 
1868 	devptr->chip.fwnode = dev_fwnode(&pdev->dev);
1869 	devptr->chip.label = "of-unittest-gpio";
1870 	devptr->chip.base = -1; /* dynamic allocation */
1871 	devptr->chip.ngpio = 5;
1872 	devptr->chip.request = unittest_gpio_chip_request;
1873 
1874 	ret = gpiochip_add_data(&devptr->chip, NULL);
1875 
1876 	unittest(!ret,
1877 		 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
1878 
1879 	if (!ret)
1880 		unittest_gpio_probe_pass_count++;
1881 	return ret;
1882 }
1883 
1884 static void unittest_gpio_remove(struct platform_device *pdev)
1885 {
1886 	struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
1887 	struct device *dev = &pdev->dev;
1888 
1889 	dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
1890 
1891 	if (devptr->chip.base != -1)
1892 		gpiochip_remove(&devptr->chip);
1893 
1894 	kfree(devptr);
1895 }
1896 
1897 static const struct of_device_id unittest_gpio_id[] = {
1898 	{ .compatible = "unittest-gpio", },
1899 	{}
1900 };
1901 
1902 static struct platform_driver unittest_gpio_driver = {
1903 	.probe	= unittest_gpio_probe,
1904 	.remove_new = unittest_gpio_remove,
1905 	.driver	= {
1906 		.name		= "unittest-gpio",
1907 		.of_match_table	= unittest_gpio_id,
1908 	},
1909 };
1910 
1911 static void __init of_unittest_overlay_gpio(void)
1912 {
1913 	int chip_request_count;
1914 	int probe_pass_count;
1915 	int ret;
1916 
1917 	/*
1918 	 * tests: apply overlays before registering driver
1919 	 * Similar to installing a driver as a module, the
1920 	 * driver is registered after applying the overlays.
1921 	 *
1922 	 * The overlays are applied by overlay_data_apply()
1923 	 * instead of of_unittest_apply_overlay() so that they
1924 	 * will not be tracked.  Thus they will not be removed
1925 	 * by of_unittest_remove_tracked_overlays().
1926 	 *
1927 	 * - apply overlay_gpio_01
1928 	 * - apply overlay_gpio_02a
1929 	 * - apply overlay_gpio_02b
1930 	 * - register driver
1931 	 *
1932 	 * register driver will result in
1933 	 *   - probe and processing gpio hog for overlay_gpio_01
1934 	 *   - probe for overlay_gpio_02a
1935 	 *   - processing gpio for overlay_gpio_02b
1936 	 */
1937 
1938 	probe_pass_count = unittest_gpio_probe_pass_count;
1939 	chip_request_count = unittest_gpio_chip_request_count;
1940 
1941 	/*
1942 	 * overlay_gpio_01 contains gpio node and child gpio hog node
1943 	 * overlay_gpio_02a contains gpio node
1944 	 * overlay_gpio_02b contains child gpio hog node
1945 	 */
1946 
1947 	unittest(overlay_data_apply("overlay_gpio_01", NULL),
1948 		 "Adding overlay 'overlay_gpio_01' failed\n");
1949 
1950 	unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1951 		 "Adding overlay 'overlay_gpio_02a' failed\n");
1952 
1953 	unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1954 		 "Adding overlay 'overlay_gpio_02b' failed\n");
1955 
1956 	ret = platform_driver_register(&unittest_gpio_driver);
1957 	if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1958 		return;
1959 
1960 	unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1961 		 "unittest_gpio_probe() failed or not called\n");
1962 
1963 	unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1964 		 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1965 		 unittest_gpio_chip_request_count - chip_request_count);
1966 
1967 	/*
1968 	 * tests: apply overlays after registering driver
1969 	 *
1970 	 * Similar to a driver built-in to the kernel, the
1971 	 * driver is registered before applying the overlays.
1972 	 *
1973 	 * overlay_gpio_03 contains gpio node and child gpio hog node
1974 	 *
1975 	 * - apply overlay_gpio_03
1976 	 *
1977 	 * apply overlay will result in
1978 	 *   - probe and processing gpio hog.
1979 	 */
1980 
1981 	probe_pass_count = unittest_gpio_probe_pass_count;
1982 	chip_request_count = unittest_gpio_chip_request_count;
1983 
1984 	/* overlay_gpio_03 contains gpio node and child gpio hog node */
1985 
1986 	unittest(overlay_data_apply("overlay_gpio_03", NULL),
1987 		 "Adding overlay 'overlay_gpio_03' failed\n");
1988 
1989 	unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1990 		 "unittest_gpio_probe() failed or not called\n");
1991 
1992 	unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1993 		 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1994 		 unittest_gpio_chip_request_count - chip_request_count);
1995 
1996 	/*
1997 	 * overlay_gpio_04a contains gpio node
1998 	 *
1999 	 * - apply overlay_gpio_04a
2000 	 *
2001 	 * apply the overlay will result in
2002 	 *   - probe for overlay_gpio_04a
2003 	 */
2004 
2005 	probe_pass_count = unittest_gpio_probe_pass_count;
2006 	chip_request_count = unittest_gpio_chip_request_count;
2007 
2008 	/* overlay_gpio_04a contains gpio node */
2009 
2010 	unittest(overlay_data_apply("overlay_gpio_04a", NULL),
2011 		 "Adding overlay 'overlay_gpio_04a' failed\n");
2012 
2013 	unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2014 		 "unittest_gpio_probe() failed or not called\n");
2015 
2016 	/*
2017 	 * overlay_gpio_04b contains child gpio hog node
2018 	 *
2019 	 * - apply overlay_gpio_04b
2020 	 *
2021 	 * apply the overlay will result in
2022 	 *   - processing gpio for overlay_gpio_04b
2023 	 */
2024 
2025 	/* overlay_gpio_04b contains child gpio hog node */
2026 
2027 	unittest(overlay_data_apply("overlay_gpio_04b", NULL),
2028 		 "Adding overlay 'overlay_gpio_04b' failed\n");
2029 
2030 	unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2031 		 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2032 		 unittest_gpio_chip_request_count - chip_request_count);
2033 }
2034 
2035 #else
2036 
2037 static void __init of_unittest_overlay_gpio(void)
2038 {
2039 	/* skip tests */
2040 }
2041 
2042 #endif
2043 
2044 #if IS_BUILTIN(CONFIG_I2C)
2045 
2046 /* get the i2c client device instantiated at the path */
2047 static struct i2c_client *of_path_to_i2c_client(const char *path)
2048 {
2049 	struct device_node *np;
2050 	struct i2c_client *client;
2051 
2052 	np = of_find_node_by_path(path);
2053 	if (np == NULL)
2054 		return NULL;
2055 
2056 	client = of_find_i2c_device_by_node(np);
2057 	of_node_put(np);
2058 
2059 	return client;
2060 }
2061 
2062 /* find out if a i2c client device exists at that path */
2063 static int of_path_i2c_client_exists(const char *path)
2064 {
2065 	struct i2c_client *client;
2066 
2067 	client = of_path_to_i2c_client(path);
2068 	if (client)
2069 		put_device(&client->dev);
2070 	return client != NULL;
2071 }
2072 #else
2073 static int of_path_i2c_client_exists(const char *path)
2074 {
2075 	return 0;
2076 }
2077 #endif
2078 
2079 enum overlay_type {
2080 	PDEV_OVERLAY,
2081 	I2C_OVERLAY
2082 };
2083 
2084 static int of_path_device_type_exists(const char *path,
2085 		enum overlay_type ovtype)
2086 {
2087 	switch (ovtype) {
2088 	case PDEV_OVERLAY:
2089 		return of_path_platform_device_exists(path);
2090 	case I2C_OVERLAY:
2091 		return of_path_i2c_client_exists(path);
2092 	}
2093 	return 0;
2094 }
2095 
2096 static const char *unittest_path(int nr, enum overlay_type ovtype)
2097 {
2098 	const char *base;
2099 	static char buf[256];
2100 
2101 	switch (ovtype) {
2102 	case PDEV_OVERLAY:
2103 		base = "/testcase-data/overlay-node/test-bus";
2104 		break;
2105 	case I2C_OVERLAY:
2106 		base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2107 		break;
2108 	default:
2109 		buf[0] = '\0';
2110 		return buf;
2111 	}
2112 	snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2113 	buf[sizeof(buf) - 1] = '\0';
2114 	return buf;
2115 }
2116 
2117 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2118 {
2119 	const char *path;
2120 
2121 	path = unittest_path(unittest_nr, ovtype);
2122 
2123 	switch (ovtype) {
2124 	case PDEV_OVERLAY:
2125 		return of_path_platform_device_exists(path);
2126 	case I2C_OVERLAY:
2127 		return of_path_i2c_client_exists(path);
2128 	}
2129 	return 0;
2130 }
2131 
2132 static const char *overlay_name_from_nr(int nr)
2133 {
2134 	static char buf[256];
2135 
2136 	snprintf(buf, sizeof(buf) - 1,
2137 		"overlay_%d", nr);
2138 	buf[sizeof(buf) - 1] = '\0';
2139 
2140 	return buf;
2141 }
2142 
2143 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2144 
2145 #define MAX_TRACK_OVCS_IDS 256
2146 
2147 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2148 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2149 static int track_ovcs_id_cnt;
2150 
2151 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2152 {
2153 	if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2154 		return;
2155 
2156 	track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2157 	track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2158 	track_ovcs_id_cnt++;
2159 }
2160 
2161 static void of_unittest_untrack_overlay(int ovcs_id)
2162 {
2163 	if (WARN_ON(track_ovcs_id_cnt < 1))
2164 		return;
2165 
2166 	track_ovcs_id_cnt--;
2167 
2168 	/* If out of synch then test is broken.  Do not try to recover. */
2169 	WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2170 }
2171 
2172 static void of_unittest_remove_tracked_overlays(void)
2173 {
2174 	int ret, ovcs_id, overlay_nr, save_ovcs_id;
2175 	const char *overlay_name;
2176 
2177 	while (track_ovcs_id_cnt > 0) {
2178 
2179 		ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2180 		overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2181 		save_ovcs_id = ovcs_id;
2182 		ret = of_overlay_remove(&ovcs_id);
2183 		if (ret == -ENODEV) {
2184 			overlay_name = overlay_name_from_nr(overlay_nr);
2185 			pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2186 				__func__, overlay_name, ret);
2187 		}
2188 		of_unittest_untrack_overlay(save_ovcs_id);
2189 	}
2190 
2191 }
2192 
2193 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2194 {
2195 	/*
2196 	 * The overlay will be tracked, thus it will be removed
2197 	 * by of_unittest_remove_tracked_overlays().
2198 	 */
2199 
2200 	const char *overlay_name;
2201 
2202 	overlay_name = overlay_name_from_nr(overlay_nr);
2203 
2204 	if (!overlay_data_apply(overlay_name, ovcs_id)) {
2205 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2206 		return -EFAULT;
2207 	}
2208 	of_unittest_track_overlay(*ovcs_id, overlay_nr);
2209 
2210 	return 0;
2211 }
2212 
2213 static int __init __of_unittest_apply_overlay_check(int overlay_nr,
2214 		int unittest_nr, int before, int after,
2215 		enum overlay_type ovtype)
2216 {
2217 	int ret, ovcs_id;
2218 
2219 	/* unittest device must be in before state */
2220 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2221 		unittest(0, "%s with device @\"%s\" %s\n",
2222 				overlay_name_from_nr(overlay_nr),
2223 				unittest_path(unittest_nr, ovtype),
2224 				!before ? "enabled" : "disabled");
2225 		return -EINVAL;
2226 	}
2227 
2228 	/* apply the overlay */
2229 	ovcs_id = 0;
2230 	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2231 	if (ret != 0) {
2232 		/* of_unittest_apply_overlay already called unittest() */
2233 		return ret;
2234 	}
2235 
2236 	/* unittest device must be in after state */
2237 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2238 		unittest(0, "%s with device @\"%s\" %s\n",
2239 				overlay_name_from_nr(overlay_nr),
2240 				unittest_path(unittest_nr, ovtype),
2241 				!after ? "enabled" : "disabled");
2242 		return -EINVAL;
2243 	}
2244 
2245 	return ovcs_id;
2246 }
2247 
2248 /* apply an overlay while checking before and after states */
2249 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2250 		int unittest_nr, int before, int after,
2251 		enum overlay_type ovtype)
2252 {
2253 	int ovcs_id = __of_unittest_apply_overlay_check(overlay_nr,
2254 				unittest_nr, before, after, ovtype);
2255 	if (ovcs_id < 0)
2256 		return ovcs_id;
2257 
2258 	return 0;
2259 }
2260 
2261 /* apply an overlay and then revert it while checking before, after states */
2262 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2263 		int unittest_nr, int before, int after,
2264 		enum overlay_type ovtype)
2265 {
2266 	int ret, ovcs_id, save_ovcs_id;
2267 
2268 	ovcs_id = __of_unittest_apply_overlay_check(overlay_nr, unittest_nr,
2269 						    before, after, ovtype);
2270 	if (ovcs_id < 0)
2271 		return ovcs_id;
2272 
2273 	/* remove the overlay */
2274 	save_ovcs_id = ovcs_id;
2275 	ret = of_overlay_remove(&ovcs_id);
2276 	if (ret != 0) {
2277 		unittest(0, "%s failed to be destroyed @\"%s\"\n",
2278 				overlay_name_from_nr(overlay_nr),
2279 				unittest_path(unittest_nr, ovtype));
2280 		return ret;
2281 	}
2282 	of_unittest_untrack_overlay(save_ovcs_id);
2283 
2284 	/* unittest device must be again in before state */
2285 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2286 		unittest(0, "%s with device @\"%s\" %s\n",
2287 				overlay_name_from_nr(overlay_nr),
2288 				unittest_path(unittest_nr, ovtype),
2289 				!before ? "enabled" : "disabled");
2290 		return -EINVAL;
2291 	}
2292 
2293 	return 0;
2294 }
2295 
2296 /* test activation of device */
2297 static void __init of_unittest_overlay_0(void)
2298 {
2299 	int ret;
2300 
2301 	EXPECT_BEGIN(KERN_INFO,
2302 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2303 
2304 	/* device should enable */
2305 	ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2306 
2307 	EXPECT_END(KERN_INFO,
2308 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2309 
2310 	if (ret)
2311 		return;
2312 
2313 	unittest(1, "overlay test %d passed\n", 0);
2314 }
2315 
2316 /* test deactivation of device */
2317 static void __init of_unittest_overlay_1(void)
2318 {
2319 	int ret;
2320 
2321 	EXPECT_BEGIN(KERN_INFO,
2322 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2323 
2324 	/* device should disable */
2325 	ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2326 
2327 	EXPECT_END(KERN_INFO,
2328 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2329 
2330 	if (ret)
2331 		return;
2332 
2333 	unittest(1, "overlay test %d passed\n", 1);
2334 
2335 }
2336 
2337 /* test activation of device */
2338 static void __init of_unittest_overlay_2(void)
2339 {
2340 	int ret;
2341 
2342 	EXPECT_BEGIN(KERN_INFO,
2343 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2344 
2345 	/* device should enable */
2346 	ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2347 
2348 	EXPECT_END(KERN_INFO,
2349 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2350 
2351 	if (ret)
2352 		return;
2353 	unittest(1, "overlay test %d passed\n", 2);
2354 }
2355 
2356 /* test deactivation of device */
2357 static void __init of_unittest_overlay_3(void)
2358 {
2359 	int ret;
2360 
2361 	EXPECT_BEGIN(KERN_INFO,
2362 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2363 
2364 	/* device should disable */
2365 	ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2366 
2367 	EXPECT_END(KERN_INFO,
2368 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2369 
2370 	if (ret)
2371 		return;
2372 
2373 	unittest(1, "overlay test %d passed\n", 3);
2374 }
2375 
2376 /* test activation of a full device node */
2377 static void __init of_unittest_overlay_4(void)
2378 {
2379 	/* device should disable */
2380 	if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2381 		return;
2382 
2383 	unittest(1, "overlay test %d passed\n", 4);
2384 }
2385 
2386 /* test overlay apply/revert sequence */
2387 static void __init of_unittest_overlay_5(void)
2388 {
2389 	int ret;
2390 
2391 	EXPECT_BEGIN(KERN_INFO,
2392 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2393 
2394 	/* device should disable */
2395 	ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2396 
2397 	EXPECT_END(KERN_INFO,
2398 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2399 
2400 	if (ret)
2401 		return;
2402 
2403 	unittest(1, "overlay test %d passed\n", 5);
2404 }
2405 
2406 /* test overlay application in sequence */
2407 static void __init of_unittest_overlay_6(void)
2408 {
2409 	int i, save_ovcs_id[2], ovcs_id;
2410 	int overlay_nr = 6, unittest_nr = 6;
2411 	int before = 0, after = 1;
2412 	const char *overlay_name;
2413 
2414 	int ret;
2415 
2416 	/* unittest device must be in before state */
2417 	for (i = 0; i < 2; i++) {
2418 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2419 				!= before) {
2420 			unittest(0, "%s with device @\"%s\" %s\n",
2421 					overlay_name_from_nr(overlay_nr + i),
2422 					unittest_path(unittest_nr + i,
2423 						PDEV_OVERLAY),
2424 					!before ? "enabled" : "disabled");
2425 			return;
2426 		}
2427 	}
2428 
2429 	/* apply the overlays */
2430 
2431 	EXPECT_BEGIN(KERN_INFO,
2432 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2433 
2434 	overlay_name = overlay_name_from_nr(overlay_nr + 0);
2435 
2436 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2437 
2438 	if (!ret) {
2439 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2440 			return;
2441 	}
2442 	save_ovcs_id[0] = ovcs_id;
2443 	of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2444 
2445 	EXPECT_END(KERN_INFO,
2446 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2447 
2448 	EXPECT_BEGIN(KERN_INFO,
2449 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2450 
2451 	overlay_name = overlay_name_from_nr(overlay_nr + 1);
2452 
2453 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2454 
2455 	if (!ret) {
2456 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2457 			return;
2458 	}
2459 	save_ovcs_id[1] = ovcs_id;
2460 	of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2461 
2462 	EXPECT_END(KERN_INFO,
2463 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2464 
2465 
2466 	for (i = 0; i < 2; i++) {
2467 		/* unittest device must be in after state */
2468 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2469 				!= after) {
2470 			unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2471 					overlay_name_from_nr(overlay_nr + i),
2472 					unittest_path(unittest_nr + i,
2473 						PDEV_OVERLAY),
2474 					!after ? "enabled" : "disabled");
2475 			return;
2476 		}
2477 	}
2478 
2479 	for (i = 1; i >= 0; i--) {
2480 		ovcs_id = save_ovcs_id[i];
2481 		if (of_overlay_remove(&ovcs_id)) {
2482 			unittest(0, "%s failed destroy @\"%s\"\n",
2483 					overlay_name_from_nr(overlay_nr + i),
2484 					unittest_path(unittest_nr + i,
2485 						PDEV_OVERLAY));
2486 			return;
2487 		}
2488 		of_unittest_untrack_overlay(save_ovcs_id[i]);
2489 	}
2490 
2491 	for (i = 0; i < 2; i++) {
2492 		/* unittest device must be again in before state */
2493 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2494 				!= before) {
2495 			unittest(0, "%s with device @\"%s\" %s\n",
2496 					overlay_name_from_nr(overlay_nr + i),
2497 					unittest_path(unittest_nr + i,
2498 						PDEV_OVERLAY),
2499 					!before ? "enabled" : "disabled");
2500 			return;
2501 		}
2502 	}
2503 
2504 	unittest(1, "overlay test %d passed\n", 6);
2505 
2506 }
2507 
2508 /* test overlay application in sequence */
2509 static void __init of_unittest_overlay_8(void)
2510 {
2511 	int i, save_ovcs_id[2], ovcs_id;
2512 	int overlay_nr = 8, unittest_nr = 8;
2513 	const char *overlay_name;
2514 	int ret;
2515 
2516 	/* we don't care about device state in this test */
2517 
2518 	EXPECT_BEGIN(KERN_INFO,
2519 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2520 
2521 	overlay_name = overlay_name_from_nr(overlay_nr + 0);
2522 
2523 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2524 	if (!ret)
2525 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2526 
2527 	EXPECT_END(KERN_INFO,
2528 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2529 
2530 	if (!ret)
2531 		return;
2532 
2533 	save_ovcs_id[0] = ovcs_id;
2534 	of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2535 
2536 	overlay_name = overlay_name_from_nr(overlay_nr + 1);
2537 
2538 	EXPECT_BEGIN(KERN_INFO,
2539 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2540 
2541 	/* apply the overlays */
2542 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2543 
2544 	EXPECT_END(KERN_INFO,
2545 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2546 
2547 	if (!ret) {
2548 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2549 		return;
2550 	}
2551 
2552 	save_ovcs_id[1] = ovcs_id;
2553 	of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2554 
2555 	/* now try to remove first overlay (it should fail) */
2556 	ovcs_id = save_ovcs_id[0];
2557 
2558 	EXPECT_BEGIN(KERN_INFO,
2559 		     "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2560 
2561 	EXPECT_BEGIN(KERN_INFO,
2562 		     "OF: overlay: overlay #6 is not topmost");
2563 
2564 	ret = of_overlay_remove(&ovcs_id);
2565 
2566 	EXPECT_END(KERN_INFO,
2567 		   "OF: overlay: overlay #6 is not topmost");
2568 
2569 	EXPECT_END(KERN_INFO,
2570 		   "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2571 
2572 	if (!ret) {
2573 		/*
2574 		 * Should never get here.  If we do, expect a lot of
2575 		 * subsequent tracking and overlay removal related errors.
2576 		 */
2577 		unittest(0, "%s was destroyed @\"%s\"\n",
2578 				overlay_name_from_nr(overlay_nr + 0),
2579 				unittest_path(unittest_nr,
2580 					PDEV_OVERLAY));
2581 		return;
2582 	}
2583 
2584 	/* removing them in order should work */
2585 	for (i = 1; i >= 0; i--) {
2586 		ovcs_id = save_ovcs_id[i];
2587 		if (of_overlay_remove(&ovcs_id)) {
2588 			unittest(0, "%s not destroyed @\"%s\"\n",
2589 					overlay_name_from_nr(overlay_nr + i),
2590 					unittest_path(unittest_nr,
2591 						PDEV_OVERLAY));
2592 			return;
2593 		}
2594 		of_unittest_untrack_overlay(save_ovcs_id[i]);
2595 	}
2596 
2597 	unittest(1, "overlay test %d passed\n", 8);
2598 }
2599 
2600 /* test insertion of a bus with parent devices */
2601 static void __init of_unittest_overlay_10(void)
2602 {
2603 	int ret;
2604 	char *child_path;
2605 
2606 	/* device should disable */
2607 	ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2608 
2609 	if (unittest(ret == 0,
2610 			"overlay test %d failed; overlay application\n", 10))
2611 		return;
2612 
2613 	child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2614 			unittest_path(10, PDEV_OVERLAY));
2615 	if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2616 		return;
2617 
2618 	ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2619 	kfree(child_path);
2620 
2621 	unittest(ret, "overlay test %d failed; no child device\n", 10);
2622 }
2623 
2624 /* test insertion of a bus with parent devices (and revert) */
2625 static void __init of_unittest_overlay_11(void)
2626 {
2627 	int ret;
2628 
2629 	/* device should disable */
2630 	ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2631 			PDEV_OVERLAY);
2632 
2633 	unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2634 }
2635 
2636 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2637 
2638 struct unittest_i2c_bus_data {
2639 	struct platform_device	*pdev;
2640 	struct i2c_adapter	adap;
2641 };
2642 
2643 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2644 		struct i2c_msg *msgs, int num)
2645 {
2646 	struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2647 
2648 	(void)std;
2649 
2650 	return num;
2651 }
2652 
2653 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2654 {
2655 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2656 }
2657 
2658 static const struct i2c_algorithm unittest_i2c_algo = {
2659 	.master_xfer	= unittest_i2c_master_xfer,
2660 	.functionality	= unittest_i2c_functionality,
2661 };
2662 
2663 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2664 {
2665 	struct device *dev = &pdev->dev;
2666 	struct device_node *np = dev->of_node;
2667 	struct unittest_i2c_bus_data *std;
2668 	struct i2c_adapter *adap;
2669 	int ret;
2670 
2671 	if (np == NULL) {
2672 		dev_err(dev, "No OF data for device\n");
2673 		return -EINVAL;
2674 
2675 	}
2676 
2677 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2678 
2679 	std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2680 	if (!std)
2681 		return -ENOMEM;
2682 
2683 	/* link them together */
2684 	std->pdev = pdev;
2685 	platform_set_drvdata(pdev, std);
2686 
2687 	adap = &std->adap;
2688 	i2c_set_adapdata(adap, std);
2689 	adap->nr = -1;
2690 	strscpy(adap->name, pdev->name, sizeof(adap->name));
2691 	adap->class = I2C_CLASS_DEPRECATED;
2692 	adap->algo = &unittest_i2c_algo;
2693 	adap->dev.parent = dev;
2694 	adap->dev.of_node = dev->of_node;
2695 	adap->timeout = 5 * HZ;
2696 	adap->retries = 3;
2697 
2698 	ret = i2c_add_numbered_adapter(adap);
2699 	if (ret != 0) {
2700 		dev_err(dev, "Failed to add I2C adapter\n");
2701 		return ret;
2702 	}
2703 
2704 	return 0;
2705 }
2706 
2707 static void unittest_i2c_bus_remove(struct platform_device *pdev)
2708 {
2709 	struct device *dev = &pdev->dev;
2710 	struct device_node *np = dev->of_node;
2711 	struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2712 
2713 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2714 	i2c_del_adapter(&std->adap);
2715 }
2716 
2717 static const struct of_device_id unittest_i2c_bus_match[] = {
2718 	{ .compatible = "unittest-i2c-bus", },
2719 	{},
2720 };
2721 
2722 static struct platform_driver unittest_i2c_bus_driver = {
2723 	.probe			= unittest_i2c_bus_probe,
2724 	.remove_new		= unittest_i2c_bus_remove,
2725 	.driver = {
2726 		.name		= "unittest-i2c-bus",
2727 		.of_match_table	= unittest_i2c_bus_match,
2728 	},
2729 };
2730 
2731 static int unittest_i2c_dev_probe(struct i2c_client *client)
2732 {
2733 	struct device *dev = &client->dev;
2734 	struct device_node *np = client->dev.of_node;
2735 
2736 	if (!np) {
2737 		dev_err(dev, "No OF node\n");
2738 		return -EINVAL;
2739 	}
2740 
2741 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2742 
2743 	return 0;
2744 };
2745 
2746 static void unittest_i2c_dev_remove(struct i2c_client *client)
2747 {
2748 	struct device *dev = &client->dev;
2749 	struct device_node *np = client->dev.of_node;
2750 
2751 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2752 }
2753 
2754 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2755 	{ .name = "unittest-i2c-dev" },
2756 	{ }
2757 };
2758 
2759 static struct i2c_driver unittest_i2c_dev_driver = {
2760 	.driver = {
2761 		.name = "unittest-i2c-dev",
2762 	},
2763 	.probe = unittest_i2c_dev_probe,
2764 	.remove = unittest_i2c_dev_remove,
2765 	.id_table = unittest_i2c_dev_id,
2766 };
2767 
2768 #if IS_BUILTIN(CONFIG_I2C_MUX)
2769 
2770 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2771 {
2772 	return 0;
2773 }
2774 
2775 static int unittest_i2c_mux_probe(struct i2c_client *client)
2776 {
2777 	int i, nchans;
2778 	struct device *dev = &client->dev;
2779 	struct i2c_adapter *adap = client->adapter;
2780 	struct device_node *np = client->dev.of_node, *child;
2781 	struct i2c_mux_core *muxc;
2782 	u32 reg, max_reg;
2783 
2784 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2785 
2786 	if (!np) {
2787 		dev_err(dev, "No OF node\n");
2788 		return -EINVAL;
2789 	}
2790 
2791 	max_reg = (u32)-1;
2792 	for_each_child_of_node(np, child) {
2793 		if (of_property_read_u32(child, "reg", &reg))
2794 			continue;
2795 		if (max_reg == (u32)-1 || reg > max_reg)
2796 			max_reg = reg;
2797 	}
2798 	nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2799 	if (nchans == 0) {
2800 		dev_err(dev, "No channels\n");
2801 		return -EINVAL;
2802 	}
2803 
2804 	muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2805 			     unittest_i2c_mux_select_chan, NULL);
2806 	if (!muxc)
2807 		return -ENOMEM;
2808 	for (i = 0; i < nchans; i++) {
2809 		if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2810 			dev_err(dev, "Failed to register mux #%d\n", i);
2811 			i2c_mux_del_adapters(muxc);
2812 			return -ENODEV;
2813 		}
2814 	}
2815 
2816 	i2c_set_clientdata(client, muxc);
2817 
2818 	return 0;
2819 };
2820 
2821 static void unittest_i2c_mux_remove(struct i2c_client *client)
2822 {
2823 	struct device *dev = &client->dev;
2824 	struct device_node *np = client->dev.of_node;
2825 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2826 
2827 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2828 	i2c_mux_del_adapters(muxc);
2829 }
2830 
2831 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2832 	{ .name = "unittest-i2c-mux" },
2833 	{ }
2834 };
2835 
2836 static struct i2c_driver unittest_i2c_mux_driver = {
2837 	.driver = {
2838 		.name = "unittest-i2c-mux",
2839 	},
2840 	.probe = unittest_i2c_mux_probe,
2841 	.remove = unittest_i2c_mux_remove,
2842 	.id_table = unittest_i2c_mux_id,
2843 };
2844 
2845 #endif
2846 
2847 static int of_unittest_overlay_i2c_init(void)
2848 {
2849 	int ret;
2850 
2851 	ret = i2c_add_driver(&unittest_i2c_dev_driver);
2852 	if (unittest(ret == 0,
2853 			"could not register unittest i2c device driver\n"))
2854 		return ret;
2855 
2856 	ret = platform_driver_register(&unittest_i2c_bus_driver);
2857 
2858 	if (unittest(ret == 0,
2859 			"could not register unittest i2c bus driver\n"))
2860 		return ret;
2861 
2862 #if IS_BUILTIN(CONFIG_I2C_MUX)
2863 
2864 	EXPECT_BEGIN(KERN_INFO,
2865 		     "i2c i2c-1: Added multiplexed i2c bus 2");
2866 
2867 	ret = i2c_add_driver(&unittest_i2c_mux_driver);
2868 
2869 	EXPECT_END(KERN_INFO,
2870 		   "i2c i2c-1: Added multiplexed i2c bus 2");
2871 
2872 	if (unittest(ret == 0,
2873 			"could not register unittest i2c mux driver\n"))
2874 		return ret;
2875 #endif
2876 
2877 	return 0;
2878 }
2879 
2880 static void of_unittest_overlay_i2c_cleanup(void)
2881 {
2882 #if IS_BUILTIN(CONFIG_I2C_MUX)
2883 	i2c_del_driver(&unittest_i2c_mux_driver);
2884 #endif
2885 	platform_driver_unregister(&unittest_i2c_bus_driver);
2886 	i2c_del_driver(&unittest_i2c_dev_driver);
2887 }
2888 
2889 static void __init of_unittest_overlay_i2c_12(void)
2890 {
2891 	int ret;
2892 
2893 	/* device should enable */
2894 	EXPECT_BEGIN(KERN_INFO,
2895 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2896 
2897 	ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2898 
2899 	EXPECT_END(KERN_INFO,
2900 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2901 
2902 	if (ret)
2903 		return;
2904 
2905 	unittest(1, "overlay test %d passed\n", 12);
2906 }
2907 
2908 /* test deactivation of device */
2909 static void __init of_unittest_overlay_i2c_13(void)
2910 {
2911 	int ret;
2912 
2913 	EXPECT_BEGIN(KERN_INFO,
2914 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2915 
2916 	/* device should disable */
2917 	ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2918 
2919 	EXPECT_END(KERN_INFO,
2920 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2921 
2922 	if (ret)
2923 		return;
2924 
2925 	unittest(1, "overlay test %d passed\n", 13);
2926 }
2927 
2928 /* just check for i2c mux existence */
2929 static void of_unittest_overlay_i2c_14(void)
2930 {
2931 }
2932 
2933 static void __init of_unittest_overlay_i2c_15(void)
2934 {
2935 	int ret;
2936 
2937 	/* device should enable */
2938 	EXPECT_BEGIN(KERN_INFO,
2939 		     "i2c i2c-1: Added multiplexed i2c bus 3");
2940 
2941 	ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2942 
2943 	EXPECT_END(KERN_INFO,
2944 		   "i2c i2c-1: Added multiplexed i2c bus 3");
2945 
2946 	if (ret)
2947 		return;
2948 
2949 	unittest(1, "overlay test %d passed\n", 15);
2950 }
2951 
2952 #else
2953 
2954 static inline void of_unittest_overlay_i2c_14(void) { }
2955 static inline void of_unittest_overlay_i2c_15(void) { }
2956 
2957 #endif
2958 
2959 static int of_notify(struct notifier_block *nb, unsigned long action,
2960 		     void *arg)
2961 {
2962 	struct of_overlay_notify_data *nd = arg;
2963 	struct device_node *found;
2964 	int ret;
2965 
2966 	/*
2967 	 * For overlay_16 .. overlay_19, check that returning an error
2968 	 * works for each of the actions by setting an arbitrary return
2969 	 * error number that matches the test number.  e.g. for unittest16,
2970 	 * ret = -EBUSY which is -16.
2971 	 *
2972 	 * OVERLAY_INFO() for the overlays is declared to expect the same
2973 	 * error number, so overlay_data_apply() will return no error.
2974 	 *
2975 	 * overlay_20 will return NOTIFY_DONE
2976 	 */
2977 
2978 	ret = 0;
2979 	of_node_get(nd->overlay);
2980 
2981 	switch (action) {
2982 
2983 	case OF_OVERLAY_PRE_APPLY:
2984 		found = of_find_node_by_name(nd->overlay, "test-unittest16");
2985 		if (found) {
2986 			of_node_put(found);
2987 			ret = -EBUSY;
2988 		}
2989 		break;
2990 
2991 	case OF_OVERLAY_POST_APPLY:
2992 		found = of_find_node_by_name(nd->overlay, "test-unittest17");
2993 		if (found) {
2994 			of_node_put(found);
2995 			ret = -EEXIST;
2996 		}
2997 		break;
2998 
2999 	case OF_OVERLAY_PRE_REMOVE:
3000 		found = of_find_node_by_name(nd->overlay, "test-unittest18");
3001 		if (found) {
3002 			of_node_put(found);
3003 			ret = -EXDEV;
3004 		}
3005 		break;
3006 
3007 	case OF_OVERLAY_POST_REMOVE:
3008 		found = of_find_node_by_name(nd->overlay, "test-unittest19");
3009 		if (found) {
3010 			of_node_put(found);
3011 			ret = -ENODEV;
3012 		}
3013 		break;
3014 
3015 	default:			/* should not happen */
3016 		of_node_put(nd->overlay);
3017 		ret = -EINVAL;
3018 		break;
3019 	}
3020 
3021 	if (ret)
3022 		return notifier_from_errno(ret);
3023 
3024 	return NOTIFY_DONE;
3025 }
3026 
3027 static struct notifier_block of_nb = {
3028 	.notifier_call = of_notify,
3029 };
3030 
3031 static void __init of_unittest_overlay_notify(void)
3032 {
3033 	int ovcs_id;
3034 	int ret;
3035 
3036 	ret = of_overlay_notifier_register(&of_nb);
3037 	unittest(!ret,
3038 		 "of_overlay_notifier_register() failed, ret = %d\n", ret);
3039 	if (ret)
3040 		return;
3041 
3042 	/*
3043 	 * The overlays are applied by overlay_data_apply()
3044 	 * instead of of_unittest_apply_overlay() so that they
3045 	 * will not be tracked.  Thus they will not be removed
3046 	 * by of_unittest_remove_tracked_overlays().
3047 	 *
3048 	 * Applying overlays 16 - 19 will each trigger an error for a
3049 	 * different action in of_notify().
3050 	 *
3051 	 * Applying overlay 20 will not trigger any error in of_notify().
3052 	 */
3053 
3054 	/* ---  overlay 16  --- */
3055 
3056 	EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3057 
3058 	unittest(overlay_data_apply("overlay_16", &ovcs_id),
3059 		 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
3060 
3061 	EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
3062 
3063 	unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
3064 
3065 	/* ---  overlay 17  --- */
3066 
3067 	EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3068 
3069 	unittest(overlay_data_apply("overlay_17", &ovcs_id),
3070 		 "test OF_OVERLAY_POST_APPLY notify injected error\n");
3071 
3072 	EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
3073 
3074 	unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
3075 
3076 	/* ---  overlay 18  --- */
3077 
3078 	unittest(overlay_data_apply("overlay_18", &ovcs_id),
3079 		 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
3080 
3081 	unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
3082 
3083 	if (ovcs_id) {
3084 		EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3085 
3086 		ret = of_overlay_remove(&ovcs_id);
3087 		EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3088 		if (ret == -EXDEV) {
3089 			/*
3090 			 * change set ovcs_id should still exist
3091 			 */
3092 			unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3093 		} else {
3094 			unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3095 		}
3096 	} else {
3097 		unittest(1, "ovcs_id not created for overlay_18\n");
3098 	}
3099 
3100 	unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3101 
3102 	/* ---  overlay 19  --- */
3103 
3104 	unittest(overlay_data_apply("overlay_19", &ovcs_id),
3105 		 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3106 
3107 	unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3108 
3109 	if (ovcs_id) {
3110 		EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3111 		ret = of_overlay_remove(&ovcs_id);
3112 		EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3113 		if (ret == -ENODEV)
3114 			unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3115 		else
3116 			unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3117 	} else {
3118 		unittest(1, "ovcs_id removed for overlay_19\n");
3119 	}
3120 
3121 	unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3122 		 ovcs_id);
3123 
3124 	/* ---  overlay 20  --- */
3125 
3126 	unittest(overlay_data_apply("overlay_20", &ovcs_id),
3127 		 "overlay notify no injected error\n");
3128 
3129 	if (ovcs_id) {
3130 		ret = of_overlay_remove(&ovcs_id);
3131 		if (ret)
3132 			unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3133 				 ret);
3134 	} else {
3135 		unittest(1, "ovcs_id not created for overlay_20\n");
3136 	}
3137 
3138 	unittest(!of_overlay_notifier_unregister(&of_nb),
3139 		 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3140 }
3141 
3142 static void __init of_unittest_overlay(void)
3143 {
3144 	struct device_node *bus_np = NULL;
3145 	unsigned int i;
3146 
3147 	if (platform_driver_register(&unittest_driver)) {
3148 		unittest(0, "could not register unittest driver\n");
3149 		goto out;
3150 	}
3151 
3152 	bus_np = of_find_node_by_path(bus_path);
3153 	if (bus_np == NULL) {
3154 		unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3155 		goto out;
3156 	}
3157 
3158 	if (of_platform_default_populate(bus_np, NULL, NULL)) {
3159 		unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3160 		goto out;
3161 	}
3162 
3163 	if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3164 		unittest(0, "could not find unittest0 @ \"%s\"\n",
3165 				unittest_path(100, PDEV_OVERLAY));
3166 		goto out;
3167 	}
3168 
3169 	if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3170 		unittest(0, "unittest1 @ \"%s\" should not exist\n",
3171 				unittest_path(101, PDEV_OVERLAY));
3172 		goto out;
3173 	}
3174 
3175 	unittest(1, "basic infrastructure of overlays passed");
3176 
3177 	/* tests in sequence */
3178 	of_unittest_overlay_0();
3179 	of_unittest_overlay_1();
3180 	of_unittest_overlay_2();
3181 	of_unittest_overlay_3();
3182 	of_unittest_overlay_4();
3183 	for (i = 0; i < 3; i++)
3184 		of_unittest_overlay_5();
3185 	of_unittest_overlay_6();
3186 	of_unittest_overlay_8();
3187 
3188 	of_unittest_overlay_10();
3189 	of_unittest_overlay_11();
3190 
3191 #if IS_BUILTIN(CONFIG_I2C)
3192 	if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3193 		goto out;
3194 
3195 	of_unittest_overlay_i2c_12();
3196 	of_unittest_overlay_i2c_13();
3197 	of_unittest_overlay_i2c_14();
3198 	of_unittest_overlay_i2c_15();
3199 
3200 	of_unittest_overlay_i2c_cleanup();
3201 #endif
3202 
3203 	of_unittest_overlay_gpio();
3204 
3205 	of_unittest_remove_tracked_overlays();
3206 
3207 	of_unittest_overlay_notify();
3208 
3209 out:
3210 	of_node_put(bus_np);
3211 }
3212 
3213 #else
3214 static inline void __init of_unittest_overlay(void) { }
3215 #endif
3216 
3217 static void __init of_unittest_lifecycle(void)
3218 {
3219 #ifdef CONFIG_OF_DYNAMIC
3220 	unsigned int refcount;
3221 	int found_refcount_one = 0;
3222 	int put_count = 0;
3223 	struct device_node *np;
3224 	struct device_node *prev_sibling, *next_sibling;
3225 	const char *refcount_path = "/testcase-data/refcount-node";
3226 	const char *refcount_parent_path = "/testcase-data";
3227 
3228 	/*
3229 	 * Node lifecycle tests, non-dynamic node:
3230 	 *
3231 	 * - Decrementing refcount to zero via of_node_put() should cause the
3232 	 *   attempt to free the node memory by of_node_release() to fail
3233 	 *   because the node is not a dynamic node.
3234 	 *
3235 	 * - Decrementing refcount past zero should result in additional
3236 	 *   errors reported.
3237 	 */
3238 
3239 	np = of_find_node_by_path(refcount_path);
3240 	unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3241 	if (np == NULL)
3242 		goto out_skip_tests;
3243 
3244 	while (!found_refcount_one) {
3245 
3246 		if (put_count++ > 10) {
3247 			unittest(0, "guardrail to avoid infinite loop\n");
3248 			goto out_skip_tests;
3249 		}
3250 
3251 		refcount = kref_read(&np->kobj.kref);
3252 		if (refcount == 1)
3253 			found_refcount_one = 1;
3254 		else
3255 			of_node_put(np);
3256 	}
3257 
3258 	EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3259 
3260 	/*
3261 	 * refcount is now one, decrementing to zero will result in a call to
3262 	 * of_node_release() to free the node's memory, which should result
3263 	 * in an error
3264 	 */
3265 	unittest(1, "/testcase-data/refcount-node is one");
3266 	of_node_put(np);
3267 
3268 	EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3269 
3270 
3271 	/*
3272 	 * expect stack trace for subsequent of_node_put():
3273 	 *   __refcount_sub_and_test() calls:
3274 	 *   refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3275 	 *
3276 	 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3277 	 * the first three lines, and the last line.
3278 	 */
3279 	EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3280 	EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3281 	EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3282 	EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3283 
3284 	/* refcount is now zero, this should fail */
3285 	unittest(1, "/testcase-data/refcount-node is zero");
3286 	of_node_put(np);
3287 
3288 	EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3289 	EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3290 	EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3291 	EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3292 
3293 	/*
3294 	 * Q. do we expect to get yet another warning?
3295 	 * A. no, the WARNING is from WARN_ONCE()
3296 	 */
3297 	EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3298 	EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3299 	EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3300 	EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3301 
3302 	unittest(1, "/testcase-data/refcount-node is zero, second time");
3303 	of_node_put(np);
3304 
3305 	EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3306 	EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3307 	EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3308 	EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3309 
3310 	/*
3311 	 * refcount of zero will trigger stack traces from any further
3312 	 * attempt to of_node_get() node "refcount-node". One example of
3313 	 * this is where of_unittest_check_node_linkage() will recursively
3314 	 * scan the tree, with 'for_each_child_of_node()' doing an
3315 	 * of_node_get() of the children of a node.
3316 	 *
3317 	 * Prevent the stack trace by removing node "refcount-node" from
3318 	 * its parent's child list.
3319 	 *
3320 	 * WARNING:  EVIL, EVIL, EVIL:
3321 	 *
3322 	 *   Directly manipulate the child list of node /testcase-data to
3323 	 *   remove child refcount-node.  This is ignoring all proper methods
3324 	 *   of removing a child and will leak a small amount of memory.
3325 	 */
3326 
3327 	np = of_find_node_by_path(refcount_parent_path);
3328 	unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3329 	unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3330 	if (np == NULL)
3331 		return;
3332 
3333 	prev_sibling = np->child;
3334 	next_sibling = prev_sibling->sibling;
3335 	if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3336 		np->child = next_sibling;
3337 		next_sibling = next_sibling->sibling;
3338 	}
3339 	while (next_sibling) {
3340 		if (!strcmp(next_sibling->full_name, "refcount-node"))
3341 			prev_sibling->sibling = next_sibling->sibling;
3342 		prev_sibling = next_sibling;
3343 		next_sibling = next_sibling->sibling;
3344 	}
3345 	of_node_put(np);
3346 
3347 	return;
3348 
3349 out_skip_tests:
3350 #endif
3351 	unittest(0, "One or more lifecycle tests skipped\n");
3352 }
3353 
3354 #ifdef CONFIG_OF_OVERLAY
3355 
3356 /*
3357  * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3358  * created by cmd_dt_S_dtbo in scripts/Makefile.lib
3359  */
3360 
3361 #define OVERLAY_INFO_EXTERN(overlay_name) \
3362 	extern uint8_t __dtbo_##overlay_name##_begin[]; \
3363 	extern uint8_t __dtbo_##overlay_name##_end[]
3364 
3365 #define OVERLAY_INFO(overlay_name, expected, expected_remove) \
3366 {	.dtbo_begin		= __dtbo_##overlay_name##_begin, \
3367 	.dtbo_end		= __dtbo_##overlay_name##_end, \
3368 	.expected_result	= expected, \
3369 	.expected_result_remove	= expected_remove, \
3370 	.name			= #overlay_name, \
3371 }
3372 
3373 struct overlay_info {
3374 	uint8_t		*dtbo_begin;
3375 	uint8_t		*dtbo_end;
3376 	int		expected_result;
3377 	int		expected_result_remove;	/* if apply failed */
3378 	int		ovcs_id;
3379 	char		*name;
3380 };
3381 
3382 OVERLAY_INFO_EXTERN(overlay_base);
3383 OVERLAY_INFO_EXTERN(overlay);
3384 OVERLAY_INFO_EXTERN(overlay_0);
3385 OVERLAY_INFO_EXTERN(overlay_1);
3386 OVERLAY_INFO_EXTERN(overlay_2);
3387 OVERLAY_INFO_EXTERN(overlay_3);
3388 OVERLAY_INFO_EXTERN(overlay_4);
3389 OVERLAY_INFO_EXTERN(overlay_5);
3390 OVERLAY_INFO_EXTERN(overlay_6);
3391 OVERLAY_INFO_EXTERN(overlay_7);
3392 OVERLAY_INFO_EXTERN(overlay_8);
3393 OVERLAY_INFO_EXTERN(overlay_9);
3394 OVERLAY_INFO_EXTERN(overlay_10);
3395 OVERLAY_INFO_EXTERN(overlay_11);
3396 OVERLAY_INFO_EXTERN(overlay_12);
3397 OVERLAY_INFO_EXTERN(overlay_13);
3398 OVERLAY_INFO_EXTERN(overlay_15);
3399 OVERLAY_INFO_EXTERN(overlay_16);
3400 OVERLAY_INFO_EXTERN(overlay_17);
3401 OVERLAY_INFO_EXTERN(overlay_18);
3402 OVERLAY_INFO_EXTERN(overlay_19);
3403 OVERLAY_INFO_EXTERN(overlay_20);
3404 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3405 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3406 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3407 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3408 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3409 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3410 OVERLAY_INFO_EXTERN(overlay_pci_node);
3411 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3412 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3413 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3414 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3415 OVERLAY_INFO_EXTERN(overlay_bad_unresolved);
3416 
3417 /* entries found by name */
3418 static struct overlay_info overlays[] = {
3419 	OVERLAY_INFO(overlay_base, -9999, 0),
3420 	OVERLAY_INFO(overlay, 0, 0),
3421 	OVERLAY_INFO(overlay_0, 0, 0),
3422 	OVERLAY_INFO(overlay_1, 0, 0),
3423 	OVERLAY_INFO(overlay_2, 0, 0),
3424 	OVERLAY_INFO(overlay_3, 0, 0),
3425 	OVERLAY_INFO(overlay_4, 0, 0),
3426 	OVERLAY_INFO(overlay_5, 0, 0),
3427 	OVERLAY_INFO(overlay_6, 0, 0),
3428 	OVERLAY_INFO(overlay_7, 0, 0),
3429 	OVERLAY_INFO(overlay_8, 0, 0),
3430 	OVERLAY_INFO(overlay_9, 0, 0),
3431 	OVERLAY_INFO(overlay_10, 0, 0),
3432 	OVERLAY_INFO(overlay_11, 0, 0),
3433 	OVERLAY_INFO(overlay_12, 0, 0),
3434 	OVERLAY_INFO(overlay_13, 0, 0),
3435 	OVERLAY_INFO(overlay_15, 0, 0),
3436 	OVERLAY_INFO(overlay_16, -EBUSY, 0),
3437 	OVERLAY_INFO(overlay_17, -EEXIST, 0),
3438 	OVERLAY_INFO(overlay_18, 0, 0),
3439 	OVERLAY_INFO(overlay_19, 0, 0),
3440 	OVERLAY_INFO(overlay_20, 0, 0),
3441 	OVERLAY_INFO(overlay_gpio_01, 0, 0),
3442 	OVERLAY_INFO(overlay_gpio_02a, 0, 0),
3443 	OVERLAY_INFO(overlay_gpio_02b, 0, 0),
3444 	OVERLAY_INFO(overlay_gpio_03, 0, 0),
3445 	OVERLAY_INFO(overlay_gpio_04a, 0, 0),
3446 	OVERLAY_INFO(overlay_gpio_04b, 0, 0),
3447 	OVERLAY_INFO(overlay_pci_node, 0, 0),
3448 	OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL, -ENODEV),
3449 	OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL, -ENODEV),
3450 	OVERLAY_INFO(overlay_bad_phandle, -EINVAL, 0),
3451 	OVERLAY_INFO(overlay_bad_symbol, -EINVAL, -ENODEV),
3452 	OVERLAY_INFO(overlay_bad_unresolved, -EINVAL, 0),
3453 	/* end marker */
3454 	{ }
3455 };
3456 
3457 static struct device_node *overlay_base_root;
3458 
3459 static void * __init dt_alloc_memory(u64 size, u64 align)
3460 {
3461 	void *ptr = memblock_alloc(size, align);
3462 
3463 	if (!ptr)
3464 		panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
3465 		      __func__, size, align);
3466 
3467 	return ptr;
3468 }
3469 
3470 /*
3471  * Create base device tree for the overlay unittest.
3472  *
3473  * This is called from very early boot code.
3474  *
3475  * Do as much as possible the same way as done in __unflatten_device_tree
3476  * and other early boot steps for the normal FDT so that the overlay base
3477  * unflattened tree will have the same characteristics as the real tree
3478  * (such as having memory allocated by the early allocator).  The goal
3479  * is to test "the real thing" as much as possible, and test "test setup
3480  * code" as little as possible.
3481  *
3482  * Have to stop before resolving phandles, because that uses kmalloc.
3483  */
3484 void __init unittest_unflatten_overlay_base(void)
3485 {
3486 	struct overlay_info *info;
3487 	u32 data_size;
3488 	void *new_fdt;
3489 	u32 size;
3490 	int found = 0;
3491 	const char *overlay_name = "overlay_base";
3492 
3493 	for (info = overlays; info && info->name; info++) {
3494 		if (!strcmp(overlay_name, info->name)) {
3495 			found = 1;
3496 			break;
3497 		}
3498 	}
3499 	if (!found) {
3500 		pr_err("no overlay data for %s\n", overlay_name);
3501 		return;
3502 	}
3503 
3504 	info = &overlays[0];
3505 
3506 	if (info->expected_result != -9999) {
3507 		pr_err("No dtb 'overlay_base' to attach\n");
3508 		return;
3509 	}
3510 
3511 	data_size = info->dtbo_end - info->dtbo_begin;
3512 	if (!data_size) {
3513 		pr_err("No dtb 'overlay_base' to attach\n");
3514 		return;
3515 	}
3516 
3517 	size = fdt_totalsize(info->dtbo_begin);
3518 	if (size != data_size) {
3519 		pr_err("dtb 'overlay_base' header totalsize != actual size");
3520 		return;
3521 	}
3522 
3523 	new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3524 	if (!new_fdt) {
3525 		pr_err("alloc for dtb 'overlay_base' failed");
3526 		return;
3527 	}
3528 
3529 	memcpy(new_fdt, info->dtbo_begin, size);
3530 
3531 	__unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3532 				dt_alloc_memory, true);
3533 }
3534 
3535 /*
3536  * The purpose of of_unittest_overlay_data_add is to add an
3537  * overlay in the normal fashion.  This is a test of the whole
3538  * picture, instead of testing individual elements.
3539  *
3540  * A secondary purpose is to be able to verify that the contents of
3541  * /proc/device-tree/ contains the updated structure and values from
3542  * the overlay.  That must be verified separately in user space.
3543  *
3544  * Return 0 on unexpected error.
3545  */
3546 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3547 {
3548 	struct overlay_info *info;
3549 	int passed = 1;
3550 	int found = 0;
3551 	int ret, ret2;
3552 	u32 size;
3553 
3554 	for (info = overlays; info && info->name; info++) {
3555 		if (!strcmp(overlay_name, info->name)) {
3556 			found = 1;
3557 			break;
3558 		}
3559 	}
3560 	if (!found) {
3561 		pr_err("no overlay data for %s\n", overlay_name);
3562 		return 0;
3563 	}
3564 
3565 	size = info->dtbo_end - info->dtbo_begin;
3566 	if (!size)
3567 		pr_err("no overlay data for %s\n", overlay_name);
3568 
3569 	ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id,
3570 				   NULL);
3571 	if (ovcs_id)
3572 		*ovcs_id = info->ovcs_id;
3573 	if (ret < 0)
3574 		goto out;
3575 
3576 	pr_debug("%s applied\n", overlay_name);
3577 
3578 out:
3579 	if (ret != info->expected_result) {
3580 		pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3581 		       info->expected_result, ret, overlay_name);
3582 		passed = 0;
3583 	}
3584 
3585 	if (ret < 0) {
3586 		/* changeset may be partially applied */
3587 		ret2 = of_overlay_remove(&info->ovcs_id);
3588 		if (ret2 != info->expected_result_remove) {
3589 			pr_err("of_overlay_remove() expected %d, ret=%d, %s\n",
3590 			       info->expected_result_remove, ret2,
3591 			       overlay_name);
3592 			passed = 0;
3593 		}
3594 	}
3595 
3596 	return passed;
3597 }
3598 
3599 /*
3600  * The purpose of of_unittest_overlay_high_level is to add an overlay
3601  * in the normal fashion.  This is a test of the whole picture,
3602  * instead of individual elements.
3603  *
3604  * The first part of the function is _not_ normal overlay usage; it is
3605  * finishing splicing the base overlay device tree into the live tree.
3606  */
3607 static __init void of_unittest_overlay_high_level(void)
3608 {
3609 	struct device_node *last_sibling;
3610 	struct device_node *np;
3611 	struct device_node *of_symbols;
3612 	struct device_node *overlay_base_symbols;
3613 	struct device_node **pprev;
3614 	struct property *prop;
3615 	int ret;
3616 
3617 	if (!overlay_base_root) {
3618 		unittest(0, "overlay_base_root not initialized\n");
3619 		return;
3620 	}
3621 
3622 	/*
3623 	 * Could not fixup phandles in unittest_unflatten_overlay_base()
3624 	 * because kmalloc() was not yet available.
3625 	 */
3626 	of_overlay_mutex_lock();
3627 	of_resolve_phandles(overlay_base_root);
3628 	of_overlay_mutex_unlock();
3629 
3630 
3631 	/*
3632 	 * do not allow overlay_base to duplicate any node already in
3633 	 * tree, this greatly simplifies the code
3634 	 */
3635 
3636 	/*
3637 	 * remove overlay_base_root node "__local_fixups", after
3638 	 * being used by of_resolve_phandles()
3639 	 */
3640 	pprev = &overlay_base_root->child;
3641 	for (np = overlay_base_root->child; np; np = np->sibling) {
3642 		if (of_node_name_eq(np, "__local_fixups__")) {
3643 			*pprev = np->sibling;
3644 			break;
3645 		}
3646 		pprev = &np->sibling;
3647 	}
3648 
3649 	/* remove overlay_base_root node "__symbols__" if in live tree */
3650 	of_symbols = of_get_child_by_name(of_root, "__symbols__");
3651 	if (of_symbols) {
3652 		/* will have to graft properties from node into live tree */
3653 		pprev = &overlay_base_root->child;
3654 		for (np = overlay_base_root->child; np; np = np->sibling) {
3655 			if (of_node_name_eq(np, "__symbols__")) {
3656 				overlay_base_symbols = np;
3657 				*pprev = np->sibling;
3658 				break;
3659 			}
3660 			pprev = &np->sibling;
3661 		}
3662 	}
3663 
3664 	for_each_child_of_node(overlay_base_root, np) {
3665 		struct device_node *base_child;
3666 		for_each_child_of_node(of_root, base_child) {
3667 			if (!strcmp(np->full_name, base_child->full_name)) {
3668 				unittest(0, "illegal node name in overlay_base %pOFn",
3669 					 np);
3670 				of_node_put(np);
3671 				of_node_put(base_child);
3672 				return;
3673 			}
3674 		}
3675 	}
3676 
3677 	/*
3678 	 * overlay 'overlay_base' is not allowed to have root
3679 	 * properties, so only need to splice nodes into main device tree.
3680 	 *
3681 	 * root node of *overlay_base_root will not be freed, it is lost
3682 	 * memory.
3683 	 */
3684 
3685 	for (np = overlay_base_root->child; np; np = np->sibling)
3686 		np->parent = of_root;
3687 
3688 	mutex_lock(&of_mutex);
3689 
3690 	for (last_sibling = np = of_root->child; np; np = np->sibling)
3691 		last_sibling = np;
3692 
3693 	if (last_sibling)
3694 		last_sibling->sibling = overlay_base_root->child;
3695 	else
3696 		of_root->child = overlay_base_root->child;
3697 
3698 	for_each_of_allnodes_from(overlay_base_root, np)
3699 		__of_attach_node_sysfs(np);
3700 
3701 	if (of_symbols) {
3702 		struct property *new_prop;
3703 		for_each_property_of_node(overlay_base_symbols, prop) {
3704 
3705 			new_prop = __of_prop_dup(prop, GFP_KERNEL);
3706 			if (!new_prop) {
3707 				unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3708 					 prop->name);
3709 				goto err_unlock;
3710 			}
3711 			if (__of_add_property(of_symbols, new_prop)) {
3712 				kfree(new_prop->name);
3713 				kfree(new_prop->value);
3714 				kfree(new_prop);
3715 				/* "name" auto-generated by unflatten */
3716 				if (!strcmp(prop->name, "name"))
3717 					continue;
3718 				unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3719 					 prop->name);
3720 				goto err_unlock;
3721 			}
3722 			if (__of_add_property_sysfs(of_symbols, new_prop)) {
3723 				unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3724 					 prop->name);
3725 				goto err_unlock;
3726 			}
3727 		}
3728 	}
3729 
3730 	mutex_unlock(&of_mutex);
3731 
3732 
3733 	/* now do the normal overlay usage test */
3734 
3735 	/* ---  overlay  --- */
3736 
3737 	EXPECT_BEGIN(KERN_ERR,
3738 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3739 	EXPECT_BEGIN(KERN_ERR,
3740 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3741 	EXPECT_BEGIN(KERN_ERR,
3742 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3743 	EXPECT_BEGIN(KERN_ERR,
3744 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3745 	EXPECT_BEGIN(KERN_ERR,
3746 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3747 	EXPECT_BEGIN(KERN_ERR,
3748 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3749 	EXPECT_BEGIN(KERN_ERR,
3750 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3751 	EXPECT_BEGIN(KERN_ERR,
3752 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3753 	EXPECT_BEGIN(KERN_ERR,
3754 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3755 	EXPECT_BEGIN(KERN_ERR,
3756 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3757 	EXPECT_BEGIN(KERN_ERR,
3758 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3759 
3760 	ret = overlay_data_apply("overlay", NULL);
3761 
3762 	EXPECT_END(KERN_ERR,
3763 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3764 	EXPECT_END(KERN_ERR,
3765 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3766 	EXPECT_END(KERN_ERR,
3767 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3768 	EXPECT_END(KERN_ERR,
3769 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3770 	EXPECT_END(KERN_ERR,
3771 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3772 	EXPECT_END(KERN_ERR,
3773 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3774 	EXPECT_END(KERN_ERR,
3775 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3776 	EXPECT_END(KERN_ERR,
3777 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3778 	EXPECT_END(KERN_ERR,
3779 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3780 	EXPECT_END(KERN_ERR,
3781 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3782 	EXPECT_END(KERN_ERR,
3783 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3784 
3785 	unittest(ret, "Adding overlay 'overlay' failed\n");
3786 
3787 	/* ---  overlay_bad_add_dup_node  --- */
3788 
3789 	EXPECT_BEGIN(KERN_ERR,
3790 		     "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3791 	EXPECT_BEGIN(KERN_ERR,
3792 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3793 	EXPECT_BEGIN(KERN_ERR,
3794 		     "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3795 	EXPECT_BEGIN(KERN_ERR,
3796 		     "OF: Error reverting changeset (-19)");
3797 
3798 	unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3799 		 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3800 
3801 	EXPECT_END(KERN_ERR,
3802 		   "OF: Error reverting changeset (-19)");
3803 	EXPECT_END(KERN_ERR,
3804 		   "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/controller:name");
3805 	EXPECT_END(KERN_ERR,
3806 		   "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3807 	EXPECT_END(KERN_ERR,
3808 		   "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3809 
3810 	/* ---  overlay_bad_add_dup_prop  --- */
3811 
3812 	EXPECT_BEGIN(KERN_ERR,
3813 		     "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3814 	EXPECT_BEGIN(KERN_ERR,
3815 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3816 	EXPECT_BEGIN(KERN_ERR,
3817 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3818 	EXPECT_BEGIN(KERN_ERR,
3819 		     "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3820 	EXPECT_BEGIN(KERN_ERR,
3821 		     "OF: Error reverting changeset (-19)");
3822 
3823 	unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3824 		 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3825 
3826 	EXPECT_END(KERN_ERR,
3827 		   "OF: Error reverting changeset (-19)");
3828 	EXPECT_END(KERN_ERR,
3829 		   "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/motor-1/electric:name");
3830 	EXPECT_END(KERN_ERR,
3831 		   "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3832 	EXPECT_END(KERN_ERR,
3833 		   "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3834 	EXPECT_END(KERN_ERR,
3835 		   "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3836 
3837 	/* ---  overlay_bad_phandle  --- */
3838 
3839 	unittest(overlay_data_apply("overlay_bad_phandle", NULL),
3840 		 "Adding overlay 'overlay_bad_phandle' failed\n");
3841 
3842 	/* ---  overlay_bad_symbol  --- */
3843 
3844 	EXPECT_BEGIN(KERN_ERR,
3845 		     "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3846 	EXPECT_BEGIN(KERN_ERR,
3847 		     "OF: Error reverting changeset (-19)");
3848 
3849 	unittest(overlay_data_apply("overlay_bad_symbol", NULL),
3850 		 "Adding overlay 'overlay_bad_symbol' failed\n");
3851 
3852 	EXPECT_END(KERN_ERR,
3853 		   "OF: Error reverting changeset (-19)");
3854 	EXPECT_END(KERN_ERR,
3855 		   "OF: changeset: apply failed: REMOVE_PROPERTY /testcase-data-2/substation@100/hvac-medium-2:name");
3856 
3857 	/* ---  overlay_bad_unresolved  --- */
3858 
3859 	EXPECT_BEGIN(KERN_ERR,
3860 		     "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3861 	EXPECT_BEGIN(KERN_ERR,
3862 		     "OF: resolver: overlay phandle fixup failed: -22");
3863 
3864 	unittest(overlay_data_apply("overlay_bad_unresolved", NULL),
3865 		 "Adding overlay 'overlay_bad_unresolved' failed\n");
3866 
3867 	EXPECT_END(KERN_ERR,
3868 		   "OF: resolver: overlay phandle fixup failed: -22");
3869 	EXPECT_END(KERN_ERR,
3870 		   "OF: resolver: node label 'this_label_does_not_exist' not found in live devicetree symbols table");
3871 
3872 	return;
3873 
3874 err_unlock:
3875 	mutex_unlock(&of_mutex);
3876 }
3877 
3878 static int of_unittest_pci_dev_num;
3879 static int of_unittest_pci_child_num;
3880 
3881 /*
3882  * PCI device tree node test driver
3883  */
3884 static const struct pci_device_id testdrv_pci_ids[] = {
3885 	{ PCI_DEVICE(PCI_VENDOR_ID_REDHAT, 0x5), }, /* PCI_VENDOR_ID_REDHAT */
3886 	{ 0, }
3887 };
3888 
3889 static int testdrv_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3890 {
3891 	struct overlay_info *info;
3892 	struct device_node *dn;
3893 	int ret, ovcs_id;
3894 	u32 size;
3895 
3896 	dn = pdev->dev.of_node;
3897 	if (!dn) {
3898 		dev_err(&pdev->dev, "does not find bus endpoint");
3899 		return -EINVAL;
3900 	}
3901 
3902 	for (info = overlays; info && info->name; info++) {
3903 		if (!strcmp(info->name, "overlay_pci_node"))
3904 			break;
3905 	}
3906 	if (!info || !info->name) {
3907 		dev_err(&pdev->dev, "no overlay data for overlay_pci_node");
3908 		return -ENODEV;
3909 	}
3910 
3911 	size = info->dtbo_end - info->dtbo_begin;
3912 	ret = of_overlay_fdt_apply(info->dtbo_begin, size, &ovcs_id, dn);
3913 	of_node_put(dn);
3914 	if (ret)
3915 		return ret;
3916 
3917 	of_platform_default_populate(dn, NULL, &pdev->dev);
3918 	pci_set_drvdata(pdev, (void *)(uintptr_t)ovcs_id);
3919 
3920 	return 0;
3921 }
3922 
3923 static void testdrv_remove(struct pci_dev *pdev)
3924 {
3925 	int ovcs_id = (int)(uintptr_t)pci_get_drvdata(pdev);
3926 
3927 	of_platform_depopulate(&pdev->dev);
3928 	of_overlay_remove(&ovcs_id);
3929 }
3930 
3931 static struct pci_driver testdrv_driver = {
3932 	.name = "pci_dt_testdrv",
3933 	.id_table = testdrv_pci_ids,
3934 	.probe = testdrv_probe,
3935 	.remove = testdrv_remove,
3936 };
3937 
3938 static int unittest_pci_probe(struct platform_device *pdev)
3939 {
3940 	struct resource *res;
3941 	struct device *dev;
3942 	u64 exp_addr;
3943 
3944 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3945 	if (!res)
3946 		return -ENODEV;
3947 
3948 	dev = &pdev->dev;
3949 	while (dev && !dev_is_pci(dev))
3950 		dev = dev->parent;
3951 	if (!dev) {
3952 		pr_err("unable to find parent device\n");
3953 		return -ENODEV;
3954 	}
3955 
3956 	exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
3957 	unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
3958 		 (u64)res->start, exp_addr);
3959 
3960 	of_unittest_pci_child_num++;
3961 
3962 	return 0;
3963 }
3964 
3965 static const struct of_device_id unittest_pci_of_match[] = {
3966 	{ .compatible = "unittest-pci" },
3967 	{ }
3968 };
3969 
3970 static struct platform_driver unittest_pci_driver = {
3971 	.probe = unittest_pci_probe,
3972 	.driver = {
3973 		.name = "unittest-pci",
3974 		.of_match_table = unittest_pci_of_match,
3975 	},
3976 };
3977 
3978 static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
3979 {
3980 	struct device_node *pnp, *np = NULL;
3981 	struct device *child_dev;
3982 	char *path = NULL;
3983 	const __be32 *reg;
3984 	int rc = 0;
3985 
3986 	pnp = pdev->dev.of_node;
3987 	unittest(pnp, "Failed creating PCI dt node\n");
3988 	if (!pnp)
3989 		return -ENODEV;
3990 
3991 	if (add) {
3992 		path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
3993 		np = of_find_node_by_path(path);
3994 		unittest(np, "Failed to get unittest-pci node under PCI node\n");
3995 		if (!np) {
3996 			rc = -ENODEV;
3997 			goto failed;
3998 		}
3999 
4000 		reg = of_get_property(np, "reg", NULL);
4001 		unittest(reg, "Failed to get reg property\n");
4002 		if (!reg)
4003 			rc = -ENODEV;
4004 	} else {
4005 		path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
4006 		np = of_find_node_by_path(path);
4007 		unittest(!np, "Child device tree node is not removed\n");
4008 		child_dev = device_find_any_child(&pdev->dev);
4009 		unittest(!child_dev, "Child device is not removed\n");
4010 	}
4011 
4012 failed:
4013 	kfree(path);
4014 	if (np)
4015 		of_node_put(np);
4016 
4017 	return rc;
4018 }
4019 
4020 static void __init of_unittest_pci_node(void)
4021 {
4022 	struct pci_dev *pdev = NULL;
4023 	int rc;
4024 
4025 	if (!IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES))
4026 		return;
4027 
4028 	rc = pci_register_driver(&testdrv_driver);
4029 	unittest(!rc, "Failed to register pci test driver; rc = %d\n", rc);
4030 	if (rc)
4031 		return;
4032 
4033 	rc = platform_driver_register(&unittest_pci_driver);
4034 	if (unittest(!rc, "Failed to register unittest pci driver\n")) {
4035 		pci_unregister_driver(&testdrv_driver);
4036 		return;
4037 	}
4038 
4039 	while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL) {
4040 		of_unittest_pci_node_verify(pdev, true);
4041 		of_unittest_pci_dev_num++;
4042 	}
4043 	if (pdev)
4044 		pci_dev_put(pdev);
4045 
4046 	unittest(of_unittest_pci_dev_num,
4047 		 "No test PCI device been found. Please run QEMU with '-device pci-testdev'\n");
4048 	unittest(of_unittest_pci_dev_num == of_unittest_pci_child_num,
4049 		 "Child device number %d is not expected %d", of_unittest_pci_child_num,
4050 		 of_unittest_pci_dev_num);
4051 
4052 	platform_driver_unregister(&unittest_pci_driver);
4053 	pci_unregister_driver(&testdrv_driver);
4054 
4055 	while ((pdev = pci_get_device(PCI_VENDOR_ID_REDHAT, 0x5, pdev)) != NULL)
4056 		of_unittest_pci_node_verify(pdev, false);
4057 	if (pdev)
4058 		pci_dev_put(pdev);
4059 }
4060 #else
4061 
4062 static inline __init void of_unittest_overlay_high_level(void) {}
4063 static inline __init void of_unittest_pci_node(void) { }
4064 
4065 #endif
4066 
4067 static int __init of_unittest(void)
4068 {
4069 	struct device_node *np;
4070 	int res;
4071 
4072 	pr_info("start of unittest - you will see error messages\n");
4073 
4074 	/* Taint the kernel so we know we've run tests. */
4075 	add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
4076 
4077 	/* adding data for unittest */
4078 
4079 	if (IS_ENABLED(CONFIG_UML))
4080 		unittest_unflatten_overlay_base();
4081 
4082 	res = unittest_data_add();
4083 	if (res)
4084 		return res;
4085 	if (!of_aliases)
4086 		of_aliases = of_find_node_by_path("/aliases");
4087 
4088 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
4089 	if (!np) {
4090 		pr_info("No testcase data in device tree; not running tests\n");
4091 		return 0;
4092 	}
4093 	of_node_put(np);
4094 
4095 	of_unittest_check_tree_linkage();
4096 	of_unittest_check_phandles();
4097 	of_unittest_find_node_by_name();
4098 	of_unittest_dynamic();
4099 	of_unittest_parse_phandle_with_args();
4100 	of_unittest_parse_phandle_with_args_map();
4101 	of_unittest_printf();
4102 	of_unittest_property_string();
4103 	of_unittest_property_copy();
4104 	of_unittest_changeset();
4105 	of_unittest_parse_interrupts();
4106 	of_unittest_parse_interrupts_extended();
4107 	of_unittest_dma_get_max_cpu_address();
4108 	of_unittest_parse_dma_ranges();
4109 	of_unittest_pci_dma_ranges();
4110 	of_unittest_bus_ranges();
4111 	of_unittest_bus_3cell_ranges();
4112 	of_unittest_reg();
4113 	of_unittest_translate_addr();
4114 	of_unittest_match_node();
4115 	of_unittest_platform_populate();
4116 	of_unittest_overlay();
4117 	of_unittest_lifecycle();
4118 	of_unittest_pci_node();
4119 
4120 	/* Double check linkage after removing testcase data */
4121 	of_unittest_check_tree_linkage();
4122 
4123 	of_unittest_overlay_high_level();
4124 
4125 	pr_info("end of unittest - %i passed, %i failed\n",
4126 		unittest_results.passed, unittest_results.failed);
4127 
4128 	return 0;
4129 }
4130 late_initcall(of_unittest);
4131