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