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