xref: /linux/drivers/of/unittest.c (revision 3d5089c4263d3594dc055e0f9c5cb990505cdd64)
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_bus_ranges(void)
1012 {
1013 	struct device_node *np;
1014 	struct of_range range;
1015 	struct of_range_parser parser;
1016 	struct resource res;
1017 	int ret, i = 0;
1018 
1019 	np = of_find_node_by_path("/testcase-data/address-tests");
1020 	if (!np) {
1021 		pr_err("missing testcase data\n");
1022 		return;
1023 	}
1024 
1025 	if (of_range_parser_init(&parser, np)) {
1026 		pr_err("missing ranges property\n");
1027 		return;
1028 	}
1029 
1030 	ret = of_range_to_resource(np, 1, &res);
1031 	unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n",
1032 		ret, np);
1033 	unittest(resource_type(&res) == IORESOURCE_MEM,
1034 		"of_range_to_resource wrong resource type on node %pOF res=%pR\n",
1035 		np, &res);
1036 	unittest(res.start == 0xd0000000,
1037 		"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1038 		np, &res);
1039 	unittest(resource_size(&res) == 0x20000000,
1040 		"of_range_to_resource wrong resource start address on node %pOF res=%pR\n",
1041 		np, &res);
1042 
1043 	/*
1044 	 * Get the "ranges" from the device tree
1045 	 */
1046 	for_each_of_range(&parser, &range) {
1047 		unittest(range.flags == IORESOURCE_MEM,
1048 			"for_each_of_range wrong flags on node %pOF flags=%x (expected %x)\n",
1049 			np, range.flags, IORESOURCE_MEM);
1050 		if (!i) {
1051 			unittest(range.size == 0x50000000,
1052 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1053 				 np, range.size);
1054 			unittest(range.cpu_addr == 0x70000000,
1055 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1056 				 range.cpu_addr, np);
1057 			unittest(range.bus_addr == 0x70000000,
1058 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1059 				 range.pci_addr, np);
1060 		} else {
1061 			unittest(range.size == 0x20000000,
1062 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1063 				 np, range.size);
1064 			unittest(range.cpu_addr == 0xd0000000,
1065 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1066 				 range.cpu_addr, np);
1067 			unittest(range.bus_addr == 0x00000000,
1068 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1069 				 range.pci_addr, np);
1070 		}
1071 		i++;
1072 	}
1073 
1074 	of_node_put(np);
1075 }
1076 
1077 static void __init of_unittest_bus_3cell_ranges(void)
1078 {
1079 	struct device_node *np;
1080 	struct of_range range;
1081 	struct of_range_parser parser;
1082 	int i = 0;
1083 
1084 	np = of_find_node_by_path("/testcase-data/address-tests/bus@a0000000");
1085 	if (!np) {
1086 		pr_err("missing testcase data\n");
1087 		return;
1088 	}
1089 
1090 	if (of_range_parser_init(&parser, np)) {
1091 		pr_err("missing ranges property\n");
1092 		return;
1093 	}
1094 
1095 	/*
1096 	 * Get the "ranges" from the device tree
1097 	 */
1098 	for_each_of_range(&parser, &range) {
1099 		if (!i) {
1100 			unittest(range.flags == 0xf00baa,
1101 				 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1102 				 np, range.flags);
1103 			unittest(range.size == 0x100000,
1104 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1105 				 np, range.size);
1106 			unittest(range.cpu_addr == 0xa0000000,
1107 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1108 				 range.cpu_addr, np);
1109 			unittest(range.bus_addr == 0x0,
1110 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1111 				 range.pci_addr, np);
1112 		} else {
1113 			unittest(range.flags == 0xf00bee,
1114 				 "for_each_of_range wrong flags on node %pOF flags=%x\n",
1115 				 np, range.flags);
1116 			unittest(range.size == 0x200000,
1117 				 "for_each_of_range wrong size on node %pOF size=%llx\n",
1118 				 np, range.size);
1119 			unittest(range.cpu_addr == 0xb0000000,
1120 				 "for_each_of_range wrong CPU addr (%llx) on node %pOF",
1121 				 range.cpu_addr, np);
1122 			unittest(range.bus_addr == 0x100000000,
1123 				 "for_each_of_range wrong bus addr (%llx) on node %pOF",
1124 				 range.pci_addr, np);
1125 		}
1126 		i++;
1127 	}
1128 
1129 	of_node_put(np);
1130 }
1131 
1132 static void __init of_unittest_parse_interrupts(void)
1133 {
1134 	struct device_node *np;
1135 	struct of_phandle_args args;
1136 	int i, rc;
1137 
1138 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1139 		return;
1140 
1141 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1142 	if (!np) {
1143 		pr_err("missing testcase data\n");
1144 		return;
1145 	}
1146 
1147 	for (i = 0; i < 4; i++) {
1148 		bool passed = true;
1149 
1150 		memset(&args, 0, sizeof(args));
1151 		rc = of_irq_parse_one(np, i, &args);
1152 
1153 		passed &= !rc;
1154 		passed &= (args.args_count == 1);
1155 		passed &= (args.args[0] == (i + 1));
1156 
1157 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1158 			 i, args.np, rc);
1159 	}
1160 	of_node_put(np);
1161 
1162 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1163 	if (!np) {
1164 		pr_err("missing testcase data\n");
1165 		return;
1166 	}
1167 
1168 	for (i = 0; i < 4; i++) {
1169 		bool passed = true;
1170 
1171 		memset(&args, 0, sizeof(args));
1172 		rc = of_irq_parse_one(np, i, &args);
1173 
1174 		/* Test the values from tests-phandle.dtsi */
1175 		switch (i) {
1176 		case 0:
1177 			passed &= !rc;
1178 			passed &= (args.args_count == 1);
1179 			passed &= (args.args[0] == 9);
1180 			break;
1181 		case 1:
1182 			passed &= !rc;
1183 			passed &= (args.args_count == 3);
1184 			passed &= (args.args[0] == 10);
1185 			passed &= (args.args[1] == 11);
1186 			passed &= (args.args[2] == 12);
1187 			break;
1188 		case 2:
1189 			passed &= !rc;
1190 			passed &= (args.args_count == 2);
1191 			passed &= (args.args[0] == 13);
1192 			passed &= (args.args[1] == 14);
1193 			break;
1194 		case 3:
1195 			passed &= !rc;
1196 			passed &= (args.args_count == 2);
1197 			passed &= (args.args[0] == 15);
1198 			passed &= (args.args[1] == 16);
1199 			break;
1200 		default:
1201 			passed = false;
1202 		}
1203 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1204 			 i, args.np, rc);
1205 	}
1206 	of_node_put(np);
1207 }
1208 
1209 static void __init of_unittest_parse_interrupts_extended(void)
1210 {
1211 	struct device_node *np;
1212 	struct of_phandle_args args;
1213 	int i, rc;
1214 
1215 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1216 		return;
1217 
1218 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1219 	if (!np) {
1220 		pr_err("missing testcase data\n");
1221 		return;
1222 	}
1223 
1224 	for (i = 0; i < 7; i++) {
1225 		bool passed = true;
1226 
1227 		memset(&args, 0, sizeof(args));
1228 		rc = of_irq_parse_one(np, i, &args);
1229 
1230 		/* Test the values from tests-phandle.dtsi */
1231 		switch (i) {
1232 		case 0:
1233 			passed &= !rc;
1234 			passed &= (args.args_count == 1);
1235 			passed &= (args.args[0] == 1);
1236 			break;
1237 		case 1:
1238 			passed &= !rc;
1239 			passed &= (args.args_count == 3);
1240 			passed &= (args.args[0] == 2);
1241 			passed &= (args.args[1] == 3);
1242 			passed &= (args.args[2] == 4);
1243 			break;
1244 		case 2:
1245 			passed &= !rc;
1246 			passed &= (args.args_count == 2);
1247 			passed &= (args.args[0] == 5);
1248 			passed &= (args.args[1] == 6);
1249 			break;
1250 		case 3:
1251 			passed &= !rc;
1252 			passed &= (args.args_count == 1);
1253 			passed &= (args.args[0] == 9);
1254 			break;
1255 		case 4:
1256 			passed &= !rc;
1257 			passed &= (args.args_count == 3);
1258 			passed &= (args.args[0] == 10);
1259 			passed &= (args.args[1] == 11);
1260 			passed &= (args.args[2] == 12);
1261 			break;
1262 		case 5:
1263 			passed &= !rc;
1264 			passed &= (args.args_count == 2);
1265 			passed &= (args.args[0] == 13);
1266 			passed &= (args.args[1] == 14);
1267 			break;
1268 		case 6:
1269 			/*
1270 			 * Tests child node that is missing property
1271 			 * #address-cells.  See the comments in
1272 			 * drivers/of/unittest-data/tests-interrupts.dtsi
1273 			 * nodes intmap1 and interrupts-extended0
1274 			 */
1275 			passed &= !rc;
1276 			passed &= (args.args_count == 1);
1277 			passed &= (args.args[0] == 15);
1278 			break;
1279 		default:
1280 			passed = false;
1281 		}
1282 
1283 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1284 			 i, args.np, rc);
1285 	}
1286 	of_node_put(np);
1287 }
1288 
1289 static const struct of_device_id match_node_table[] = {
1290 	{ .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1291 	{ .data = "B", .type = "type1", }, /* followed by type alone */
1292 
1293 	{ .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1294 	{ .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1295 	{ .data = "Cc", .name = "name2", .type = "type2", },
1296 
1297 	{ .data = "E", .compatible = "compat3" },
1298 	{ .data = "G", .compatible = "compat2", },
1299 	{ .data = "H", .compatible = "compat2", .name = "name5", },
1300 	{ .data = "I", .compatible = "compat2", .type = "type1", },
1301 	{ .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1302 	{ .data = "K", .compatible = "compat2", .name = "name9", },
1303 	{}
1304 };
1305 
1306 static struct {
1307 	const char *path;
1308 	const char *data;
1309 } match_node_tests[] = {
1310 	{ .path = "/testcase-data/match-node/name0", .data = "A", },
1311 	{ .path = "/testcase-data/match-node/name1", .data = "B", },
1312 	{ .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1313 	{ .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1314 	{ .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1315 	{ .path = "/testcase-data/match-node/name3", .data = "E", },
1316 	{ .path = "/testcase-data/match-node/name4", .data = "G", },
1317 	{ .path = "/testcase-data/match-node/name5", .data = "H", },
1318 	{ .path = "/testcase-data/match-node/name6", .data = "G", },
1319 	{ .path = "/testcase-data/match-node/name7", .data = "I", },
1320 	{ .path = "/testcase-data/match-node/name8", .data = "J", },
1321 	{ .path = "/testcase-data/match-node/name9", .data = "K", },
1322 };
1323 
1324 static void __init of_unittest_match_node(void)
1325 {
1326 	struct device_node *np;
1327 	const struct of_device_id *match;
1328 	int i;
1329 
1330 	for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1331 		np = of_find_node_by_path(match_node_tests[i].path);
1332 		if (!np) {
1333 			unittest(0, "missing testcase node %s\n",
1334 				match_node_tests[i].path);
1335 			continue;
1336 		}
1337 
1338 		match = of_match_node(match_node_table, np);
1339 		if (!match) {
1340 			unittest(0, "%s didn't match anything\n",
1341 				match_node_tests[i].path);
1342 			continue;
1343 		}
1344 
1345 		if (strcmp(match->data, match_node_tests[i].data) != 0) {
1346 			unittest(0, "%s got wrong match. expected %s, got %s\n",
1347 				match_node_tests[i].path, match_node_tests[i].data,
1348 				(const char *)match->data);
1349 			continue;
1350 		}
1351 		unittest(1, "passed");
1352 	}
1353 }
1354 
1355 static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
1356 static const struct platform_device_info test_bus_info = {
1357 	.name = "unittest-bus",
1358 };
1359 static void __init of_unittest_platform_populate(void)
1360 {
1361 	int irq, rc;
1362 	struct device_node *np, *child, *grandchild;
1363 	struct platform_device *pdev, *test_bus;
1364 	const struct of_device_id match[] = {
1365 		{ .compatible = "test-device", },
1366 		{}
1367 	};
1368 
1369 	np = of_find_node_by_path("/testcase-data");
1370 	of_platform_default_populate(np, NULL, NULL);
1371 
1372 	/* Test that a missing irq domain returns -EPROBE_DEFER */
1373 	np = of_find_node_by_path("/testcase-data/testcase-device1");
1374 	pdev = of_find_device_by_node(np);
1375 	unittest(pdev, "device 1 creation failed\n");
1376 
1377 	if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1378 		irq = platform_get_irq(pdev, 0);
1379 		unittest(irq == -EPROBE_DEFER,
1380 			 "device deferred probe failed - %d\n", irq);
1381 
1382 		/* Test that a parsing failure does not return -EPROBE_DEFER */
1383 		np = of_find_node_by_path("/testcase-data/testcase-device2");
1384 		pdev = of_find_device_by_node(np);
1385 		unittest(pdev, "device 2 creation failed\n");
1386 
1387 		EXPECT_BEGIN(KERN_INFO,
1388 			     "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1389 
1390 		irq = platform_get_irq(pdev, 0);
1391 
1392 		EXPECT_END(KERN_INFO,
1393 			   "platform testcase-data:testcase-device2: error -ENXIO: IRQ index 0 not found");
1394 
1395 		unittest(irq < 0 && irq != -EPROBE_DEFER,
1396 			 "device parsing error failed - %d\n", irq);
1397 	}
1398 
1399 	np = of_find_node_by_path("/testcase-data/platform-tests");
1400 	unittest(np, "No testcase data in device tree\n");
1401 	if (!np)
1402 		return;
1403 
1404 	test_bus = platform_device_register_full(&test_bus_info);
1405 	rc = PTR_ERR_OR_ZERO(test_bus);
1406 	unittest(!rc, "testbus registration failed; rc=%i\n", rc);
1407 	if (rc) {
1408 		of_node_put(np);
1409 		return;
1410 	}
1411 	test_bus->dev.of_node = np;
1412 
1413 	/*
1414 	 * Add a dummy resource to the test bus node after it is
1415 	 * registered to catch problems with un-inserted resources. The
1416 	 * DT code doesn't insert the resources, and it has caused the
1417 	 * kernel to oops in the past. This makes sure the same bug
1418 	 * doesn't crop up again.
1419 	 */
1420 	platform_device_add_resources(test_bus, &test_bus_res, 1);
1421 
1422 	of_platform_populate(np, match, NULL, &test_bus->dev);
1423 	for_each_child_of_node(np, child) {
1424 		for_each_child_of_node(child, grandchild) {
1425 			pdev = of_find_device_by_node(grandchild);
1426 			unittest(pdev,
1427 				 "Could not create device for node '%pOFn'\n",
1428 				 grandchild);
1429 			platform_device_put(pdev);
1430 		}
1431 	}
1432 
1433 	of_platform_depopulate(&test_bus->dev);
1434 	for_each_child_of_node(np, child) {
1435 		for_each_child_of_node(child, grandchild)
1436 			unittest(!of_find_device_by_node(grandchild),
1437 				 "device didn't get destroyed '%pOFn'\n",
1438 				 grandchild);
1439 	}
1440 
1441 	platform_device_unregister(test_bus);
1442 	of_node_put(np);
1443 }
1444 
1445 /**
1446  *	update_node_properties - adds the properties
1447  *	of np into dup node (present in live tree) and
1448  *	updates parent of children of np to dup.
1449  *
1450  *	@np:	node whose properties are being added to the live tree
1451  *	@dup:	node present in live tree to be updated
1452  */
1453 static void update_node_properties(struct device_node *np,
1454 					struct device_node *dup)
1455 {
1456 	struct property *prop;
1457 	struct property *save_next;
1458 	struct device_node *child;
1459 	int ret;
1460 
1461 	for_each_child_of_node(np, child)
1462 		child->parent = dup;
1463 
1464 	/*
1465 	 * "unittest internal error: unable to add testdata property"
1466 	 *
1467 	 *    If this message reports a property in node '/__symbols__' then
1468 	 *    the respective unittest overlay contains a label that has the
1469 	 *    same name as a label in the live devicetree.  The label will
1470 	 *    be in the live devicetree only if the devicetree source was
1471 	 *    compiled with the '-@' option.  If you encounter this error,
1472 	 *    please consider renaming __all__ of the labels in the unittest
1473 	 *    overlay dts files with an odd prefix that is unlikely to be
1474 	 *    used in a real devicetree.
1475 	 */
1476 
1477 	/*
1478 	 * open code for_each_property_of_node() because of_add_property()
1479 	 * sets prop->next to NULL
1480 	 */
1481 	for (prop = np->properties; prop != NULL; prop = save_next) {
1482 		save_next = prop->next;
1483 		ret = of_add_property(dup, prop);
1484 		if (ret) {
1485 			if (ret == -EEXIST && !strcmp(prop->name, "name"))
1486 				continue;
1487 			pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1488 			       np, prop->name);
1489 		}
1490 	}
1491 }
1492 
1493 /**
1494  *	attach_node_and_children - attaches nodes
1495  *	and its children to live tree.
1496  *	CAUTION: misleading function name - if node @np already exists in
1497  *	the live tree then children of @np are *not* attached to the live
1498  *	tree.  This works for the current test devicetree nodes because such
1499  *	nodes do not have child nodes.
1500  *
1501  *	@np:	Node to attach to live tree
1502  */
1503 static void attach_node_and_children(struct device_node *np)
1504 {
1505 	struct device_node *next, *dup, *child;
1506 	unsigned long flags;
1507 	const char *full_name;
1508 
1509 	full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1510 
1511 	if (!strcmp(full_name, "/__local_fixups__") ||
1512 	    !strcmp(full_name, "/__fixups__")) {
1513 		kfree(full_name);
1514 		return;
1515 	}
1516 
1517 	dup = of_find_node_by_path(full_name);
1518 	kfree(full_name);
1519 	if (dup) {
1520 		update_node_properties(np, dup);
1521 		return;
1522 	}
1523 
1524 	child = np->child;
1525 	np->child = NULL;
1526 
1527 	mutex_lock(&of_mutex);
1528 	raw_spin_lock_irqsave(&devtree_lock, flags);
1529 	np->sibling = np->parent->child;
1530 	np->parent->child = np;
1531 	of_node_clear_flag(np, OF_DETACHED);
1532 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1533 
1534 	__of_attach_node_sysfs(np);
1535 	mutex_unlock(&of_mutex);
1536 
1537 	while (child) {
1538 		next = child->sibling;
1539 		attach_node_and_children(child);
1540 		child = next;
1541 	}
1542 }
1543 
1544 /**
1545  *	unittest_data_add - Reads, copies data from
1546  *	linked tree and attaches it to the live tree
1547  */
1548 static int __init unittest_data_add(void)
1549 {
1550 	void *unittest_data;
1551 	void *unittest_data_align;
1552 	struct device_node *unittest_data_node = NULL, *np;
1553 	/*
1554 	 * __dtbo_testcases_begin[] and __dtbo_testcases_end[] are magically
1555 	 * created by cmd_dt_S_dtbo in scripts/Makefile.lib
1556 	 */
1557 	extern uint8_t __dtbo_testcases_begin[];
1558 	extern uint8_t __dtbo_testcases_end[];
1559 	const int size = __dtbo_testcases_end - __dtbo_testcases_begin;
1560 	int rc;
1561 	void *ret;
1562 
1563 	if (!size) {
1564 		pr_warn("%s: testcases is empty\n", __func__);
1565 		return -ENODATA;
1566 	}
1567 
1568 	/* creating copy */
1569 	unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
1570 	if (!unittest_data)
1571 		return -ENOMEM;
1572 
1573 	unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1574 	memcpy(unittest_data_align, __dtbo_testcases_begin, size);
1575 
1576 	ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
1577 	if (!ret) {
1578 		pr_warn("%s: unflatten testcases tree failed\n", __func__);
1579 		kfree(unittest_data);
1580 		return -ENODATA;
1581 	}
1582 	if (!unittest_data_node) {
1583 		pr_warn("%s: testcases tree is empty\n", __func__);
1584 		kfree(unittest_data);
1585 		return -ENODATA;
1586 	}
1587 
1588 	/*
1589 	 * This lock normally encloses of_resolve_phandles()
1590 	 */
1591 	of_overlay_mutex_lock();
1592 
1593 	rc = of_resolve_phandles(unittest_data_node);
1594 	if (rc) {
1595 		pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1596 		of_overlay_mutex_unlock();
1597 		return -EINVAL;
1598 	}
1599 
1600 	if (!of_root) {
1601 		of_root = unittest_data_node;
1602 		for_each_of_allnodes(np)
1603 			__of_attach_node_sysfs(np);
1604 		of_aliases = of_find_node_by_path("/aliases");
1605 		of_chosen = of_find_node_by_path("/chosen");
1606 		of_overlay_mutex_unlock();
1607 		return 0;
1608 	}
1609 
1610 	EXPECT_BEGIN(KERN_INFO,
1611 		     "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1612 
1613 	/* attach the sub-tree to live tree */
1614 	np = unittest_data_node->child;
1615 	while (np) {
1616 		struct device_node *next = np->sibling;
1617 
1618 		np->parent = of_root;
1619 		/* this will clear OF_DETACHED in np and children */
1620 		attach_node_and_children(np);
1621 		np = next;
1622 	}
1623 
1624 	EXPECT_END(KERN_INFO,
1625 		   "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1626 
1627 	of_overlay_mutex_unlock();
1628 
1629 	return 0;
1630 }
1631 
1632 #ifdef CONFIG_OF_OVERLAY
1633 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id);
1634 
1635 static int unittest_probe(struct platform_device *pdev)
1636 {
1637 	struct device *dev = &pdev->dev;
1638 	struct device_node *np = dev->of_node;
1639 
1640 	if (np == NULL) {
1641 		dev_err(dev, "No OF data for device\n");
1642 		return -EINVAL;
1643 
1644 	}
1645 
1646 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1647 
1648 	of_platform_populate(np, NULL, NULL, &pdev->dev);
1649 
1650 	return 0;
1651 }
1652 
1653 static int unittest_remove(struct platform_device *pdev)
1654 {
1655 	struct device *dev = &pdev->dev;
1656 	struct device_node *np = dev->of_node;
1657 
1658 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1659 	return 0;
1660 }
1661 
1662 static const struct of_device_id unittest_match[] = {
1663 	{ .compatible = "unittest", },
1664 	{},
1665 };
1666 
1667 static struct platform_driver unittest_driver = {
1668 	.probe			= unittest_probe,
1669 	.remove			= unittest_remove,
1670 	.driver = {
1671 		.name		= "unittest",
1672 		.of_match_table	= of_match_ptr(unittest_match),
1673 	},
1674 };
1675 
1676 /* get the platform device instantiated at the path */
1677 static struct platform_device *of_path_to_platform_device(const char *path)
1678 {
1679 	struct device_node *np;
1680 	struct platform_device *pdev;
1681 
1682 	np = of_find_node_by_path(path);
1683 	if (np == NULL)
1684 		return NULL;
1685 
1686 	pdev = of_find_device_by_node(np);
1687 	of_node_put(np);
1688 
1689 	return pdev;
1690 }
1691 
1692 /* find out if a platform device exists at that path */
1693 static int of_path_platform_device_exists(const char *path)
1694 {
1695 	struct platform_device *pdev;
1696 
1697 	pdev = of_path_to_platform_device(path);
1698 	platform_device_put(pdev);
1699 	return pdev != NULL;
1700 }
1701 
1702 #ifdef CONFIG_OF_GPIO
1703 
1704 struct unittest_gpio_dev {
1705 	struct gpio_chip chip;
1706 };
1707 
1708 static int unittest_gpio_chip_request_count;
1709 static int unittest_gpio_probe_count;
1710 static int unittest_gpio_probe_pass_count;
1711 
1712 static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1713 {
1714 	unittest_gpio_chip_request_count++;
1715 
1716 	pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1717 		 unittest_gpio_chip_request_count);
1718 	return 0;
1719 }
1720 
1721 static int unittest_gpio_probe(struct platform_device *pdev)
1722 {
1723 	struct unittest_gpio_dev *devptr;
1724 	int ret;
1725 
1726 	unittest_gpio_probe_count++;
1727 
1728 	devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1729 	if (!devptr)
1730 		return -ENOMEM;
1731 
1732 	platform_set_drvdata(pdev, devptr);
1733 
1734 	devptr->chip.fwnode = dev_fwnode(&pdev->dev);
1735 	devptr->chip.label = "of-unittest-gpio";
1736 	devptr->chip.base = -1; /* dynamic allocation */
1737 	devptr->chip.ngpio = 5;
1738 	devptr->chip.request = unittest_gpio_chip_request;
1739 
1740 	ret = gpiochip_add_data(&devptr->chip, NULL);
1741 
1742 	unittest(!ret,
1743 		 "gpiochip_add_data() for node @%pfw failed, ret = %d\n", devptr->chip.fwnode, ret);
1744 
1745 	if (!ret)
1746 		unittest_gpio_probe_pass_count++;
1747 	return ret;
1748 }
1749 
1750 static int unittest_gpio_remove(struct platform_device *pdev)
1751 {
1752 	struct unittest_gpio_dev *devptr = platform_get_drvdata(pdev);
1753 	struct device *dev = &pdev->dev;
1754 
1755 	dev_dbg(dev, "%s for node @%pfw\n", __func__, devptr->chip.fwnode);
1756 
1757 	if (!devptr)
1758 		return -EINVAL;
1759 
1760 	if (devptr->chip.base != -1)
1761 		gpiochip_remove(&devptr->chip);
1762 
1763 	platform_set_drvdata(pdev, NULL);
1764 	kfree(devptr);
1765 
1766 	return 0;
1767 }
1768 
1769 static const struct of_device_id unittest_gpio_id[] = {
1770 	{ .compatible = "unittest-gpio", },
1771 	{}
1772 };
1773 
1774 static struct platform_driver unittest_gpio_driver = {
1775 	.probe	= unittest_gpio_probe,
1776 	.remove	= unittest_gpio_remove,
1777 	.driver	= {
1778 		.name		= "unittest-gpio",
1779 		.of_match_table	= of_match_ptr(unittest_gpio_id),
1780 	},
1781 };
1782 
1783 static void __init of_unittest_overlay_gpio(void)
1784 {
1785 	int chip_request_count;
1786 	int probe_pass_count;
1787 	int ret;
1788 
1789 	/*
1790 	 * tests: apply overlays before registering driver
1791 	 * Similar to installing a driver as a module, the
1792 	 * driver is registered after applying the overlays.
1793 	 *
1794 	 * The overlays are applied by overlay_data_apply()
1795 	 * instead of of_unittest_apply_overlay() so that they
1796 	 * will not be tracked.  Thus they will not be removed
1797 	 * by of_unittest_remove_tracked_overlays().
1798 	 *
1799 	 * - apply overlay_gpio_01
1800 	 * - apply overlay_gpio_02a
1801 	 * - apply overlay_gpio_02b
1802 	 * - register driver
1803 	 *
1804 	 * register driver will result in
1805 	 *   - probe and processing gpio hog for overlay_gpio_01
1806 	 *   - probe for overlay_gpio_02a
1807 	 *   - processing gpio for overlay_gpio_02b
1808 	 */
1809 
1810 	probe_pass_count = unittest_gpio_probe_pass_count;
1811 	chip_request_count = unittest_gpio_chip_request_count;
1812 
1813 	/*
1814 	 * overlay_gpio_01 contains gpio node and child gpio hog node
1815 	 * overlay_gpio_02a contains gpio node
1816 	 * overlay_gpio_02b contains child gpio hog node
1817 	 */
1818 
1819 	unittest(overlay_data_apply("overlay_gpio_01", NULL),
1820 		 "Adding overlay 'overlay_gpio_01' failed\n");
1821 
1822 	unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1823 		 "Adding overlay 'overlay_gpio_02a' failed\n");
1824 
1825 	unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1826 		 "Adding overlay 'overlay_gpio_02b' failed\n");
1827 
1828 	/*
1829 	 * messages are the result of the probes, after the
1830 	 * driver is registered
1831 	 */
1832 
1833 	EXPECT_BEGIN(KERN_INFO,
1834 		     "gpio-<<int>> (line-B-input): hogged as input\n");
1835 
1836 	EXPECT_BEGIN(KERN_INFO,
1837 		     "gpio-<<int>> (line-A-input): hogged as input\n");
1838 
1839 	ret = platform_driver_register(&unittest_gpio_driver);
1840 	if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1841 		return;
1842 
1843 	EXPECT_END(KERN_INFO,
1844 		   "gpio-<<int>> (line-A-input): hogged as input\n");
1845 	EXPECT_END(KERN_INFO,
1846 		   "gpio-<<int>> (line-B-input): hogged as input\n");
1847 
1848 	unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1849 		 "unittest_gpio_probe() failed or not called\n");
1850 
1851 	unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1852 		 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1853 		 unittest_gpio_chip_request_count - chip_request_count);
1854 
1855 	/*
1856 	 * tests: apply overlays after registering driver
1857 	 *
1858 	 * Similar to a driver built-in to the kernel, the
1859 	 * driver is registered before applying the overlays.
1860 	 *
1861 	 * overlay_gpio_03 contains gpio node and child gpio hog node
1862 	 *
1863 	 * - apply overlay_gpio_03
1864 	 *
1865 	 * apply overlay will result in
1866 	 *   - probe and processing gpio hog.
1867 	 */
1868 
1869 	probe_pass_count = unittest_gpio_probe_pass_count;
1870 	chip_request_count = unittest_gpio_chip_request_count;
1871 
1872 	EXPECT_BEGIN(KERN_INFO,
1873 		     "gpio-<<int>> (line-D-input): hogged as input\n");
1874 
1875 	/* overlay_gpio_03 contains gpio node and child gpio hog node */
1876 
1877 	unittest(overlay_data_apply("overlay_gpio_03", NULL),
1878 		 "Adding overlay 'overlay_gpio_03' failed\n");
1879 
1880 	EXPECT_END(KERN_INFO,
1881 		   "gpio-<<int>> (line-D-input): hogged as input\n");
1882 
1883 	unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1884 		 "unittest_gpio_probe() failed or not called\n");
1885 
1886 	unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1887 		 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1888 		 unittest_gpio_chip_request_count - chip_request_count);
1889 
1890 	/*
1891 	 * overlay_gpio_04a contains gpio node
1892 	 *
1893 	 * - apply overlay_gpio_04a
1894 	 *
1895 	 * apply the overlay will result in
1896 	 *   - probe for overlay_gpio_04a
1897 	 */
1898 
1899 	probe_pass_count = unittest_gpio_probe_pass_count;
1900 	chip_request_count = unittest_gpio_chip_request_count;
1901 
1902 	/* overlay_gpio_04a contains gpio node */
1903 
1904 	unittest(overlay_data_apply("overlay_gpio_04a", NULL),
1905 		 "Adding overlay 'overlay_gpio_04a' failed\n");
1906 
1907 	unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1908 		 "unittest_gpio_probe() failed or not called\n");
1909 
1910 	/*
1911 	 * overlay_gpio_04b contains child gpio hog node
1912 	 *
1913 	 * - apply overlay_gpio_04b
1914 	 *
1915 	 * apply the overlay will result in
1916 	 *   - processing gpio for overlay_gpio_04b
1917 	 */
1918 
1919 	EXPECT_BEGIN(KERN_INFO,
1920 		     "gpio-<<int>> (line-C-input): hogged as input\n");
1921 
1922 	/* overlay_gpio_04b contains child gpio hog node */
1923 
1924 	unittest(overlay_data_apply("overlay_gpio_04b", NULL),
1925 		 "Adding overlay 'overlay_gpio_04b' failed\n");
1926 
1927 	EXPECT_END(KERN_INFO,
1928 		   "gpio-<<int>> (line-C-input): hogged as input\n");
1929 
1930 	unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1931 		 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1932 		 unittest_gpio_chip_request_count - chip_request_count);
1933 }
1934 
1935 #else
1936 
1937 static void __init of_unittest_overlay_gpio(void)
1938 {
1939 	/* skip tests */
1940 }
1941 
1942 #endif
1943 
1944 #if IS_BUILTIN(CONFIG_I2C)
1945 
1946 /* get the i2c client device instantiated at the path */
1947 static struct i2c_client *of_path_to_i2c_client(const char *path)
1948 {
1949 	struct device_node *np;
1950 	struct i2c_client *client;
1951 
1952 	np = of_find_node_by_path(path);
1953 	if (np == NULL)
1954 		return NULL;
1955 
1956 	client = of_find_i2c_device_by_node(np);
1957 	of_node_put(np);
1958 
1959 	return client;
1960 }
1961 
1962 /* find out if a i2c client device exists at that path */
1963 static int of_path_i2c_client_exists(const char *path)
1964 {
1965 	struct i2c_client *client;
1966 
1967 	client = of_path_to_i2c_client(path);
1968 	if (client)
1969 		put_device(&client->dev);
1970 	return client != NULL;
1971 }
1972 #else
1973 static int of_path_i2c_client_exists(const char *path)
1974 {
1975 	return 0;
1976 }
1977 #endif
1978 
1979 enum overlay_type {
1980 	PDEV_OVERLAY,
1981 	I2C_OVERLAY
1982 };
1983 
1984 static int of_path_device_type_exists(const char *path,
1985 		enum overlay_type ovtype)
1986 {
1987 	switch (ovtype) {
1988 	case PDEV_OVERLAY:
1989 		return of_path_platform_device_exists(path);
1990 	case I2C_OVERLAY:
1991 		return of_path_i2c_client_exists(path);
1992 	}
1993 	return 0;
1994 }
1995 
1996 static const char *unittest_path(int nr, enum overlay_type ovtype)
1997 {
1998 	const char *base;
1999 	static char buf[256];
2000 
2001 	switch (ovtype) {
2002 	case PDEV_OVERLAY:
2003 		base = "/testcase-data/overlay-node/test-bus";
2004 		break;
2005 	case I2C_OVERLAY:
2006 		base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
2007 		break;
2008 	default:
2009 		buf[0] = '\0';
2010 		return buf;
2011 	}
2012 	snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
2013 	buf[sizeof(buf) - 1] = '\0';
2014 	return buf;
2015 }
2016 
2017 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
2018 {
2019 	const char *path;
2020 
2021 	path = unittest_path(unittest_nr, ovtype);
2022 
2023 	switch (ovtype) {
2024 	case PDEV_OVERLAY:
2025 		return of_path_platform_device_exists(path);
2026 	case I2C_OVERLAY:
2027 		return of_path_i2c_client_exists(path);
2028 	}
2029 	return 0;
2030 }
2031 
2032 static const char *overlay_name_from_nr(int nr)
2033 {
2034 	static char buf[256];
2035 
2036 	snprintf(buf, sizeof(buf) - 1,
2037 		"overlay_%d", nr);
2038 	buf[sizeof(buf) - 1] = '\0';
2039 
2040 	return buf;
2041 }
2042 
2043 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
2044 
2045 #define MAX_TRACK_OVCS_IDS 256
2046 
2047 static int track_ovcs_id[MAX_TRACK_OVCS_IDS];
2048 static int track_ovcs_id_overlay_nr[MAX_TRACK_OVCS_IDS];
2049 static int track_ovcs_id_cnt;
2050 
2051 static void of_unittest_track_overlay(int ovcs_id, int overlay_nr)
2052 {
2053 	if (WARN_ON(track_ovcs_id_cnt >= MAX_TRACK_OVCS_IDS))
2054 		return;
2055 
2056 	track_ovcs_id[track_ovcs_id_cnt] = ovcs_id;
2057 	track_ovcs_id_overlay_nr[track_ovcs_id_cnt] = overlay_nr;
2058 	track_ovcs_id_cnt++;
2059 }
2060 
2061 static void of_unittest_untrack_overlay(int ovcs_id)
2062 {
2063 	if (WARN_ON(track_ovcs_id_cnt < 1))
2064 		return;
2065 
2066 	track_ovcs_id_cnt--;
2067 
2068 	/* If out of synch then test is broken.  Do not try to recover. */
2069 	WARN_ON(track_ovcs_id[track_ovcs_id_cnt] != ovcs_id);
2070 }
2071 
2072 static void of_unittest_remove_tracked_overlays(void)
2073 {
2074 	int ret, ovcs_id, overlay_nr, save_ovcs_id;
2075 	const char *overlay_name;
2076 
2077 	while (track_ovcs_id_cnt > 0) {
2078 
2079 		ovcs_id = track_ovcs_id[track_ovcs_id_cnt - 1];
2080 		overlay_nr = track_ovcs_id_overlay_nr[track_ovcs_id_cnt - 1];
2081 		save_ovcs_id = ovcs_id;
2082 		ret = of_overlay_remove(&ovcs_id);
2083 		if (ret == -ENODEV) {
2084 			overlay_name = overlay_name_from_nr(overlay_nr);
2085 			pr_warn("%s: of_overlay_remove() for overlay \"%s\" failed, ret = %d\n",
2086 				__func__, overlay_name, ret);
2087 		}
2088 		of_unittest_untrack_overlay(save_ovcs_id);
2089 	}
2090 
2091 }
2092 
2093 static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
2094 {
2095 	/*
2096 	 * The overlay will be tracked, thus it will be removed
2097 	 * by of_unittest_remove_tracked_overlays().
2098 	 */
2099 
2100 	const char *overlay_name;
2101 
2102 	overlay_name = overlay_name_from_nr(overlay_nr);
2103 
2104 	if (!overlay_data_apply(overlay_name, ovcs_id)) {
2105 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2106 		return -EFAULT;
2107 	}
2108 	of_unittest_track_overlay(*ovcs_id, overlay_nr);
2109 
2110 	return 0;
2111 }
2112 
2113 /* apply an overlay while checking before and after states */
2114 static int __init of_unittest_apply_overlay_check(int overlay_nr,
2115 		int unittest_nr, int before, int after,
2116 		enum overlay_type ovtype)
2117 {
2118 	int ret, ovcs_id;
2119 
2120 	/* unittest device must not be in before state */
2121 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2122 		unittest(0, "%s with device @\"%s\" %s\n",
2123 				overlay_name_from_nr(overlay_nr),
2124 				unittest_path(unittest_nr, ovtype),
2125 				!before ? "enabled" : "disabled");
2126 		return -EINVAL;
2127 	}
2128 
2129 	ovcs_id = 0;
2130 	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2131 	if (ret != 0) {
2132 		/* of_unittest_apply_overlay already called unittest() */
2133 		return ret;
2134 	}
2135 
2136 	/* unittest device must be to set to after state */
2137 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2138 		unittest(0, "%s failed to create @\"%s\" %s\n",
2139 				overlay_name_from_nr(overlay_nr),
2140 				unittest_path(unittest_nr, ovtype),
2141 				!after ? "enabled" : "disabled");
2142 		return -EINVAL;
2143 	}
2144 
2145 	return 0;
2146 }
2147 
2148 /* apply an overlay and then revert it while checking before, after states */
2149 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
2150 		int unittest_nr, int before, int after,
2151 		enum overlay_type ovtype)
2152 {
2153 	int ret, ovcs_id, save_ovcs_id;
2154 
2155 	/* unittest device must be in before state */
2156 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
2157 		unittest(0, "%s with device @\"%s\" %s\n",
2158 				overlay_name_from_nr(overlay_nr),
2159 				unittest_path(unittest_nr, ovtype),
2160 				!before ? "enabled" : "disabled");
2161 		return -EINVAL;
2162 	}
2163 
2164 	/* apply the overlay */
2165 	ovcs_id = 0;
2166 	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
2167 	if (ret != 0) {
2168 		/* of_unittest_apply_overlay already called unittest() */
2169 		return ret;
2170 	}
2171 
2172 	/* unittest device must be in after state */
2173 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
2174 		unittest(0, "%s failed to create @\"%s\" %s\n",
2175 				overlay_name_from_nr(overlay_nr),
2176 				unittest_path(unittest_nr, ovtype),
2177 				!after ? "enabled" : "disabled");
2178 		return -EINVAL;
2179 	}
2180 
2181 	save_ovcs_id = ovcs_id;
2182 	ret = of_overlay_remove(&ovcs_id);
2183 	if (ret != 0) {
2184 		unittest(0, "%s failed to be destroyed @\"%s\"\n",
2185 				overlay_name_from_nr(overlay_nr),
2186 				unittest_path(unittest_nr, ovtype));
2187 		return ret;
2188 	}
2189 	of_unittest_untrack_overlay(save_ovcs_id);
2190 
2191 	/* unittest device must be again in before state */
2192 	if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
2193 		unittest(0, "%s with device @\"%s\" %s\n",
2194 				overlay_name_from_nr(overlay_nr),
2195 				unittest_path(unittest_nr, ovtype),
2196 				!before ? "enabled" : "disabled");
2197 		return -EINVAL;
2198 	}
2199 
2200 	return 0;
2201 }
2202 
2203 /* test activation of device */
2204 static void __init of_unittest_overlay_0(void)
2205 {
2206 	int ret;
2207 
2208 	EXPECT_BEGIN(KERN_INFO,
2209 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2210 
2211 	/* device should enable */
2212 	ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2213 
2214 	EXPECT_END(KERN_INFO,
2215 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2216 
2217 	if (ret)
2218 		return;
2219 
2220 	unittest(1, "overlay test %d passed\n", 0);
2221 }
2222 
2223 /* test deactivation of device */
2224 static void __init of_unittest_overlay_1(void)
2225 {
2226 	int ret;
2227 
2228 	EXPECT_BEGIN(KERN_INFO,
2229 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2230 
2231 	/* device should disable */
2232 	ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2233 
2234 	EXPECT_END(KERN_INFO,
2235 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2236 
2237 	if (ret)
2238 		return;
2239 
2240 	unittest(1, "overlay test %d passed\n", 1);
2241 
2242 }
2243 
2244 /* test activation of device */
2245 static void __init of_unittest_overlay_2(void)
2246 {
2247 	int ret;
2248 
2249 	EXPECT_BEGIN(KERN_INFO,
2250 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2251 
2252 	/* device should enable */
2253 	ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2254 
2255 	EXPECT_END(KERN_INFO,
2256 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2257 
2258 	if (ret)
2259 		return;
2260 	unittest(1, "overlay test %d passed\n", 2);
2261 }
2262 
2263 /* test deactivation of device */
2264 static void __init of_unittest_overlay_3(void)
2265 {
2266 	int ret;
2267 
2268 	EXPECT_BEGIN(KERN_INFO,
2269 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2270 
2271 	/* device should disable */
2272 	ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2273 
2274 	EXPECT_END(KERN_INFO,
2275 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2276 
2277 	if (ret)
2278 		return;
2279 
2280 	unittest(1, "overlay test %d passed\n", 3);
2281 }
2282 
2283 /* test activation of a full device node */
2284 static void __init of_unittest_overlay_4(void)
2285 {
2286 	/* device should disable */
2287 	if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
2288 		return;
2289 
2290 	unittest(1, "overlay test %d passed\n", 4);
2291 }
2292 
2293 /* test overlay apply/revert sequence */
2294 static void __init of_unittest_overlay_5(void)
2295 {
2296 	int ret;
2297 
2298 	EXPECT_BEGIN(KERN_INFO,
2299 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2300 
2301 	/* device should disable */
2302 	ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2303 
2304 	EXPECT_END(KERN_INFO,
2305 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2306 
2307 	if (ret)
2308 		return;
2309 
2310 	unittest(1, "overlay test %d passed\n", 5);
2311 }
2312 
2313 /* test overlay application in sequence */
2314 static void __init of_unittest_overlay_6(void)
2315 {
2316 	int i, save_ovcs_id[2], ovcs_id;
2317 	int overlay_nr = 6, unittest_nr = 6;
2318 	int before = 0, after = 1;
2319 	const char *overlay_name;
2320 
2321 	int ret;
2322 
2323 	/* unittest device must be in before state */
2324 	for (i = 0; i < 2; i++) {
2325 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2326 				!= before) {
2327 			unittest(0, "%s with device @\"%s\" %s\n",
2328 					overlay_name_from_nr(overlay_nr + i),
2329 					unittest_path(unittest_nr + i,
2330 						PDEV_OVERLAY),
2331 					!before ? "enabled" : "disabled");
2332 			return;
2333 		}
2334 	}
2335 
2336 	/* apply the overlays */
2337 
2338 	EXPECT_BEGIN(KERN_INFO,
2339 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2340 
2341 	overlay_name = overlay_name_from_nr(overlay_nr + 0);
2342 
2343 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2344 
2345 	if (!ret) {
2346 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2347 			return;
2348 	}
2349 	save_ovcs_id[0] = ovcs_id;
2350 	of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2351 
2352 	EXPECT_END(KERN_INFO,
2353 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2354 
2355 	EXPECT_BEGIN(KERN_INFO,
2356 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2357 
2358 	overlay_name = overlay_name_from_nr(overlay_nr + 1);
2359 
2360 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2361 
2362 	if (!ret) {
2363 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2364 			return;
2365 	}
2366 	save_ovcs_id[1] = ovcs_id;
2367 	of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2368 
2369 	EXPECT_END(KERN_INFO,
2370 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2371 
2372 
2373 	for (i = 0; i < 2; i++) {
2374 		/* unittest device must be in after state */
2375 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2376 				!= after) {
2377 			unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
2378 					overlay_name_from_nr(overlay_nr + i),
2379 					unittest_path(unittest_nr + i,
2380 						PDEV_OVERLAY),
2381 					!after ? "enabled" : "disabled");
2382 			return;
2383 		}
2384 	}
2385 
2386 	for (i = 1; i >= 0; i--) {
2387 		ovcs_id = save_ovcs_id[i];
2388 		if (of_overlay_remove(&ovcs_id)) {
2389 			unittest(0, "%s failed destroy @\"%s\"\n",
2390 					overlay_name_from_nr(overlay_nr + i),
2391 					unittest_path(unittest_nr + i,
2392 						PDEV_OVERLAY));
2393 			return;
2394 		}
2395 		of_unittest_untrack_overlay(save_ovcs_id[i]);
2396 	}
2397 
2398 	for (i = 0; i < 2; i++) {
2399 		/* unittest device must be again in before state */
2400 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
2401 				!= before) {
2402 			unittest(0, "%s with device @\"%s\" %s\n",
2403 					overlay_name_from_nr(overlay_nr + i),
2404 					unittest_path(unittest_nr + i,
2405 						PDEV_OVERLAY),
2406 					!before ? "enabled" : "disabled");
2407 			return;
2408 		}
2409 	}
2410 
2411 	unittest(1, "overlay test %d passed\n", 6);
2412 
2413 }
2414 
2415 /* test overlay application in sequence */
2416 static void __init of_unittest_overlay_8(void)
2417 {
2418 	int i, save_ovcs_id[2], ovcs_id;
2419 	int overlay_nr = 8, unittest_nr = 8;
2420 	const char *overlay_name;
2421 	int ret;
2422 
2423 	/* we don't care about device state in this test */
2424 
2425 	EXPECT_BEGIN(KERN_INFO,
2426 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2427 
2428 	overlay_name = overlay_name_from_nr(overlay_nr + 0);
2429 
2430 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2431 	if (!ret)
2432 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2433 
2434 	EXPECT_END(KERN_INFO,
2435 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2436 
2437 	if (!ret)
2438 		return;
2439 
2440 	save_ovcs_id[0] = ovcs_id;
2441 	of_unittest_track_overlay(ovcs_id, overlay_nr + 0);
2442 
2443 	overlay_name = overlay_name_from_nr(overlay_nr + 1);
2444 
2445 	EXPECT_BEGIN(KERN_INFO,
2446 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2447 
2448 	/* apply the overlays */
2449 	ret = overlay_data_apply(overlay_name, &ovcs_id);
2450 
2451 	EXPECT_END(KERN_INFO,
2452 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2453 
2454 	if (!ret) {
2455 		unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2456 		return;
2457 	}
2458 
2459 	save_ovcs_id[1] = ovcs_id;
2460 	of_unittest_track_overlay(ovcs_id, overlay_nr + 1);
2461 
2462 	/* now try to remove first overlay (it should fail) */
2463 	ovcs_id = save_ovcs_id[0];
2464 
2465 	EXPECT_BEGIN(KERN_INFO,
2466 		     "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2467 
2468 	EXPECT_BEGIN(KERN_INFO,
2469 		     "OF: overlay: overlay #6 is not topmost");
2470 
2471 	ret = of_overlay_remove(&ovcs_id);
2472 
2473 	EXPECT_END(KERN_INFO,
2474 		   "OF: overlay: overlay #6 is not topmost");
2475 
2476 	EXPECT_END(KERN_INFO,
2477 		   "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2478 
2479 	if (!ret) {
2480 		/*
2481 		 * Should never get here.  If we do, expect a lot of
2482 		 * subsequent tracking and overlay removal related errors.
2483 		 */
2484 		unittest(0, "%s was destroyed @\"%s\"\n",
2485 				overlay_name_from_nr(overlay_nr + 0),
2486 				unittest_path(unittest_nr,
2487 					PDEV_OVERLAY));
2488 		return;
2489 	}
2490 
2491 	/* removing them in order should work */
2492 	for (i = 1; i >= 0; i--) {
2493 		ovcs_id = save_ovcs_id[i];
2494 		if (of_overlay_remove(&ovcs_id)) {
2495 			unittest(0, "%s not destroyed @\"%s\"\n",
2496 					overlay_name_from_nr(overlay_nr + i),
2497 					unittest_path(unittest_nr,
2498 						PDEV_OVERLAY));
2499 			return;
2500 		}
2501 		of_unittest_untrack_overlay(save_ovcs_id[i]);
2502 	}
2503 
2504 	unittest(1, "overlay test %d passed\n", 8);
2505 }
2506 
2507 /* test insertion of a bus with parent devices */
2508 static void __init of_unittest_overlay_10(void)
2509 {
2510 	int ret;
2511 	char *child_path;
2512 
2513 	/* device should disable */
2514 	ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
2515 
2516 	if (unittest(ret == 0,
2517 			"overlay test %d failed; overlay application\n", 10))
2518 		return;
2519 
2520 	child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2521 			unittest_path(10, PDEV_OVERLAY));
2522 	if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
2523 		return;
2524 
2525 	ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
2526 	kfree(child_path);
2527 
2528 	unittest(ret, "overlay test %d failed; no child device\n", 10);
2529 }
2530 
2531 /* test insertion of a bus with parent devices (and revert) */
2532 static void __init of_unittest_overlay_11(void)
2533 {
2534 	int ret;
2535 
2536 	/* device should disable */
2537 	ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
2538 			PDEV_OVERLAY);
2539 
2540 	unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
2541 }
2542 
2543 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
2544 
2545 struct unittest_i2c_bus_data {
2546 	struct platform_device	*pdev;
2547 	struct i2c_adapter	adap;
2548 };
2549 
2550 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
2551 		struct i2c_msg *msgs, int num)
2552 {
2553 	struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
2554 
2555 	(void)std;
2556 
2557 	return num;
2558 }
2559 
2560 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
2561 {
2562 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2563 }
2564 
2565 static const struct i2c_algorithm unittest_i2c_algo = {
2566 	.master_xfer	= unittest_i2c_master_xfer,
2567 	.functionality	= unittest_i2c_functionality,
2568 };
2569 
2570 static int unittest_i2c_bus_probe(struct platform_device *pdev)
2571 {
2572 	struct device *dev = &pdev->dev;
2573 	struct device_node *np = dev->of_node;
2574 	struct unittest_i2c_bus_data *std;
2575 	struct i2c_adapter *adap;
2576 	int ret;
2577 
2578 	if (np == NULL) {
2579 		dev_err(dev, "No OF data for device\n");
2580 		return -EINVAL;
2581 
2582 	}
2583 
2584 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2585 
2586 	std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
2587 	if (!std)
2588 		return -ENOMEM;
2589 
2590 	/* link them together */
2591 	std->pdev = pdev;
2592 	platform_set_drvdata(pdev, std);
2593 
2594 	adap = &std->adap;
2595 	i2c_set_adapdata(adap, std);
2596 	adap->nr = -1;
2597 	strscpy(adap->name, pdev->name, sizeof(adap->name));
2598 	adap->class = I2C_CLASS_DEPRECATED;
2599 	adap->algo = &unittest_i2c_algo;
2600 	adap->dev.parent = dev;
2601 	adap->dev.of_node = dev->of_node;
2602 	adap->timeout = 5 * HZ;
2603 	adap->retries = 3;
2604 
2605 	ret = i2c_add_numbered_adapter(adap);
2606 	if (ret != 0) {
2607 		dev_err(dev, "Failed to add I2C adapter\n");
2608 		return ret;
2609 	}
2610 
2611 	return 0;
2612 }
2613 
2614 static int unittest_i2c_bus_remove(struct platform_device *pdev)
2615 {
2616 	struct device *dev = &pdev->dev;
2617 	struct device_node *np = dev->of_node;
2618 	struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
2619 
2620 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2621 	i2c_del_adapter(&std->adap);
2622 
2623 	return 0;
2624 }
2625 
2626 static const struct of_device_id unittest_i2c_bus_match[] = {
2627 	{ .compatible = "unittest-i2c-bus", },
2628 	{},
2629 };
2630 
2631 static struct platform_driver unittest_i2c_bus_driver = {
2632 	.probe			= unittest_i2c_bus_probe,
2633 	.remove			= unittest_i2c_bus_remove,
2634 	.driver = {
2635 		.name		= "unittest-i2c-bus",
2636 		.of_match_table	= of_match_ptr(unittest_i2c_bus_match),
2637 	},
2638 };
2639 
2640 static int unittest_i2c_dev_probe(struct i2c_client *client)
2641 {
2642 	struct device *dev = &client->dev;
2643 	struct device_node *np = client->dev.of_node;
2644 
2645 	if (!np) {
2646 		dev_err(dev, "No OF node\n");
2647 		return -EINVAL;
2648 	}
2649 
2650 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2651 
2652 	return 0;
2653 };
2654 
2655 static void unittest_i2c_dev_remove(struct i2c_client *client)
2656 {
2657 	struct device *dev = &client->dev;
2658 	struct device_node *np = client->dev.of_node;
2659 
2660 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2661 }
2662 
2663 static const struct i2c_device_id unittest_i2c_dev_id[] = {
2664 	{ .name = "unittest-i2c-dev" },
2665 	{ }
2666 };
2667 
2668 static struct i2c_driver unittest_i2c_dev_driver = {
2669 	.driver = {
2670 		.name = "unittest-i2c-dev",
2671 	},
2672 	.probe_new = unittest_i2c_dev_probe,
2673 	.remove = unittest_i2c_dev_remove,
2674 	.id_table = unittest_i2c_dev_id,
2675 };
2676 
2677 #if IS_BUILTIN(CONFIG_I2C_MUX)
2678 
2679 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
2680 {
2681 	return 0;
2682 }
2683 
2684 static int unittest_i2c_mux_probe(struct i2c_client *client)
2685 {
2686 	int i, nchans;
2687 	struct device *dev = &client->dev;
2688 	struct i2c_adapter *adap = client->adapter;
2689 	struct device_node *np = client->dev.of_node, *child;
2690 	struct i2c_mux_core *muxc;
2691 	u32 reg, max_reg;
2692 
2693 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2694 
2695 	if (!np) {
2696 		dev_err(dev, "No OF node\n");
2697 		return -EINVAL;
2698 	}
2699 
2700 	max_reg = (u32)-1;
2701 	for_each_child_of_node(np, child) {
2702 		if (of_property_read_u32(child, "reg", &reg))
2703 			continue;
2704 		if (max_reg == (u32)-1 || reg > max_reg)
2705 			max_reg = reg;
2706 	}
2707 	nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2708 	if (nchans == 0) {
2709 		dev_err(dev, "No channels\n");
2710 		return -EINVAL;
2711 	}
2712 
2713 	muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2714 			     unittest_i2c_mux_select_chan, NULL);
2715 	if (!muxc)
2716 		return -ENOMEM;
2717 	for (i = 0; i < nchans; i++) {
2718 		if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
2719 			dev_err(dev, "Failed to register mux #%d\n", i);
2720 			i2c_mux_del_adapters(muxc);
2721 			return -ENODEV;
2722 		}
2723 	}
2724 
2725 	i2c_set_clientdata(client, muxc);
2726 
2727 	return 0;
2728 };
2729 
2730 static void unittest_i2c_mux_remove(struct i2c_client *client)
2731 {
2732 	struct device *dev = &client->dev;
2733 	struct device_node *np = client->dev.of_node;
2734 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
2735 
2736 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
2737 	i2c_mux_del_adapters(muxc);
2738 }
2739 
2740 static const struct i2c_device_id unittest_i2c_mux_id[] = {
2741 	{ .name = "unittest-i2c-mux" },
2742 	{ }
2743 };
2744 
2745 static struct i2c_driver unittest_i2c_mux_driver = {
2746 	.driver = {
2747 		.name = "unittest-i2c-mux",
2748 	},
2749 	.probe_new = unittest_i2c_mux_probe,
2750 	.remove = unittest_i2c_mux_remove,
2751 	.id_table = unittest_i2c_mux_id,
2752 };
2753 
2754 #endif
2755 
2756 static int of_unittest_overlay_i2c_init(void)
2757 {
2758 	int ret;
2759 
2760 	ret = i2c_add_driver(&unittest_i2c_dev_driver);
2761 	if (unittest(ret == 0,
2762 			"could not register unittest i2c device driver\n"))
2763 		return ret;
2764 
2765 	ret = platform_driver_register(&unittest_i2c_bus_driver);
2766 
2767 	if (unittest(ret == 0,
2768 			"could not register unittest i2c bus driver\n"))
2769 		return ret;
2770 
2771 #if IS_BUILTIN(CONFIG_I2C_MUX)
2772 
2773 	EXPECT_BEGIN(KERN_INFO,
2774 		     "i2c i2c-1: Added multiplexed i2c bus 2");
2775 
2776 	ret = i2c_add_driver(&unittest_i2c_mux_driver);
2777 
2778 	EXPECT_END(KERN_INFO,
2779 		   "i2c i2c-1: Added multiplexed i2c bus 2");
2780 
2781 	if (unittest(ret == 0,
2782 			"could not register unittest i2c mux driver\n"))
2783 		return ret;
2784 #endif
2785 
2786 	return 0;
2787 }
2788 
2789 static void of_unittest_overlay_i2c_cleanup(void)
2790 {
2791 #if IS_BUILTIN(CONFIG_I2C_MUX)
2792 	i2c_del_driver(&unittest_i2c_mux_driver);
2793 #endif
2794 	platform_driver_unregister(&unittest_i2c_bus_driver);
2795 	i2c_del_driver(&unittest_i2c_dev_driver);
2796 }
2797 
2798 static void __init of_unittest_overlay_i2c_12(void)
2799 {
2800 	int ret;
2801 
2802 	/* device should enable */
2803 	EXPECT_BEGIN(KERN_INFO,
2804 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2805 
2806 	ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2807 
2808 	EXPECT_END(KERN_INFO,
2809 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2810 
2811 	if (ret)
2812 		return;
2813 
2814 	unittest(1, "overlay test %d passed\n", 12);
2815 }
2816 
2817 /* test deactivation of device */
2818 static void __init of_unittest_overlay_i2c_13(void)
2819 {
2820 	int ret;
2821 
2822 	EXPECT_BEGIN(KERN_INFO,
2823 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2824 
2825 	/* device should disable */
2826 	ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2827 
2828 	EXPECT_END(KERN_INFO,
2829 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2830 
2831 	if (ret)
2832 		return;
2833 
2834 	unittest(1, "overlay test %d passed\n", 13);
2835 }
2836 
2837 /* just check for i2c mux existence */
2838 static void of_unittest_overlay_i2c_14(void)
2839 {
2840 }
2841 
2842 static void __init of_unittest_overlay_i2c_15(void)
2843 {
2844 	int ret;
2845 
2846 	/* device should enable */
2847 	EXPECT_BEGIN(KERN_INFO,
2848 		     "i2c i2c-1: Added multiplexed i2c bus 3");
2849 
2850 	ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2851 
2852 	EXPECT_END(KERN_INFO,
2853 		   "i2c i2c-1: Added multiplexed i2c bus 3");
2854 
2855 	if (ret)
2856 		return;
2857 
2858 	unittest(1, "overlay test %d passed\n", 15);
2859 }
2860 
2861 #else
2862 
2863 static inline void of_unittest_overlay_i2c_14(void) { }
2864 static inline void of_unittest_overlay_i2c_15(void) { }
2865 
2866 #endif
2867 
2868 static int of_notify(struct notifier_block *nb, unsigned long action,
2869 		     void *arg)
2870 {
2871 	struct of_overlay_notify_data *nd = arg;
2872 	struct device_node *found;
2873 	int ret;
2874 
2875 	/*
2876 	 * For overlay_16 .. overlay_19, check that returning an error
2877 	 * works for each of the actions by setting an arbitrary return
2878 	 * error number that matches the test number.  e.g. for unittest16,
2879 	 * ret = -EBUSY which is -16.
2880 	 *
2881 	 * OVERLAY_INFO() for the overlays is declared to expect the same
2882 	 * error number, so overlay_data_apply() will return no error.
2883 	 *
2884 	 * overlay_20 will return NOTIFY_DONE
2885 	 */
2886 
2887 	ret = 0;
2888 	of_node_get(nd->overlay);
2889 
2890 	switch (action) {
2891 
2892 	case OF_OVERLAY_PRE_APPLY:
2893 		found = of_find_node_by_name(nd->overlay, "test-unittest16");
2894 		if (found) {
2895 			of_node_put(found);
2896 			ret = -EBUSY;
2897 		}
2898 		break;
2899 
2900 	case OF_OVERLAY_POST_APPLY:
2901 		found = of_find_node_by_name(nd->overlay, "test-unittest17");
2902 		if (found) {
2903 			of_node_put(found);
2904 			ret = -EEXIST;
2905 		}
2906 		break;
2907 
2908 	case OF_OVERLAY_PRE_REMOVE:
2909 		found = of_find_node_by_name(nd->overlay, "test-unittest18");
2910 		if (found) {
2911 			of_node_put(found);
2912 			ret = -EXDEV;
2913 		}
2914 		break;
2915 
2916 	case OF_OVERLAY_POST_REMOVE:
2917 		found = of_find_node_by_name(nd->overlay, "test-unittest19");
2918 		if (found) {
2919 			of_node_put(found);
2920 			ret = -ENODEV;
2921 		}
2922 		break;
2923 
2924 	default:			/* should not happen */
2925 		of_node_put(nd->overlay);
2926 		ret = -EINVAL;
2927 		break;
2928 	}
2929 
2930 	if (ret)
2931 		return notifier_from_errno(ret);
2932 
2933 	return NOTIFY_DONE;
2934 }
2935 
2936 static struct notifier_block of_nb = {
2937 	.notifier_call = of_notify,
2938 };
2939 
2940 static void __init of_unittest_overlay_notify(void)
2941 {
2942 	int ovcs_id;
2943 	int ret;
2944 
2945 	ret = of_overlay_notifier_register(&of_nb);
2946 	unittest(!ret,
2947 		 "of_overlay_notifier_register() failed, ret = %d\n", ret);
2948 	if (ret)
2949 		return;
2950 
2951 	/*
2952 	 * The overlays are applied by overlay_data_apply()
2953 	 * instead of of_unittest_apply_overlay() so that they
2954 	 * will not be tracked.  Thus they will not be removed
2955 	 * by of_unittest_remove_tracked_overlays().
2956 	 *
2957 	 * Applying overlays 16 - 19 will each trigger an error for a
2958 	 * different action in of_notify().
2959 	 *
2960 	 * Applying overlay 20 will not trigger any error in of_notify().
2961 	 */
2962 
2963 	/* ---  overlay 16  --- */
2964 
2965 	EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
2966 
2967 	unittest(overlay_data_apply("overlay_16", &ovcs_id),
2968 		 "test OF_OVERLAY_PRE_APPLY notify injected error\n");
2969 
2970 	EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus");
2971 
2972 	unittest(ovcs_id, "ovcs_id not created for overlay_16\n");
2973 
2974 	/* ---  overlay 17  --- */
2975 
2976 	EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
2977 
2978 	unittest(overlay_data_apply("overlay_17", &ovcs_id),
2979 		 "test OF_OVERLAY_POST_APPLY notify injected error\n");
2980 
2981 	EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus");
2982 
2983 	unittest(ovcs_id, "ovcs_id not created for overlay_17\n");
2984 
2985 	if (ovcs_id) {
2986 		ret = of_overlay_remove(&ovcs_id);
2987 		unittest(!ret,
2988 			"overlay_17 of_overlay_remove(), ret = %d\n", ret);
2989 	}
2990 
2991 	/* ---  overlay 18  --- */
2992 
2993 	unittest(overlay_data_apply("overlay_18", &ovcs_id),
2994 		 "OF_OVERLAY_PRE_REMOVE notify injected error\n");
2995 
2996 	unittest(ovcs_id, "ovcs_id not created for overlay_18\n");
2997 
2998 	if (ovcs_id) {
2999 		EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3000 
3001 		ret = of_overlay_remove(&ovcs_id);
3002 		EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus");
3003 		if (ret == -EXDEV) {
3004 			/*
3005 			 * change set ovcs_id should still exist
3006 			 */
3007 			unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE\n");
3008 		} else {
3009 			unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERLAY_PRE_REMOVE not returned\n");
3010 		}
3011 	} else {
3012 		unittest(1, "ovcs_id not created for overlay_18\n");
3013 	}
3014 
3015 	unittest(ovcs_id, "ovcs_id removed for overlay_18\n");
3016 
3017 	/* ---  overlay 19  --- */
3018 
3019 	unittest(overlay_data_apply("overlay_19", &ovcs_id),
3020 		 "OF_OVERLAY_POST_REMOVE notify injected error\n");
3021 
3022 	unittest(ovcs_id, "ovcs_id not created for overlay_19\n");
3023 
3024 	if (ovcs_id) {
3025 		EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3026 		ret = of_overlay_remove(&ovcs_id);
3027 		EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus");
3028 		if (ret == -ENODEV)
3029 			unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE\n");
3030 		else
3031 			unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERLAY_POST_REMOVE not returned\n");
3032 	} else {
3033 		unittest(1, "ovcs_id removed for overlay_19\n");
3034 	}
3035 
3036 	unittest(!ovcs_id, "changeset ovcs_id = %d not removed for overlay_19\n",
3037 		 ovcs_id);
3038 
3039 	/* ---  overlay 20  --- */
3040 
3041 	unittest(overlay_data_apply("overlay_20", &ovcs_id),
3042 		 "overlay notify no injected error\n");
3043 
3044 	if (ovcs_id) {
3045 		ret = of_overlay_remove(&ovcs_id);
3046 		if (ret)
3047 			unittest(1, "overlay_20 failed to be destroyed, ret = %d\n",
3048 				 ret);
3049 	} else {
3050 		unittest(1, "ovcs_id not created for overlay_20\n");
3051 	}
3052 
3053 	unittest(!of_overlay_notifier_unregister(&of_nb),
3054 		 "of_overlay_notifier_unregister() failed, ret = %d\n", ret);
3055 }
3056 
3057 static void __init of_unittest_overlay(void)
3058 {
3059 	struct device_node *bus_np = NULL;
3060 
3061 	if (platform_driver_register(&unittest_driver)) {
3062 		unittest(0, "could not register unittest driver\n");
3063 		goto out;
3064 	}
3065 
3066 	bus_np = of_find_node_by_path(bus_path);
3067 	if (bus_np == NULL) {
3068 		unittest(0, "could not find bus_path \"%s\"\n", bus_path);
3069 		goto out;
3070 	}
3071 
3072 	if (of_platform_default_populate(bus_np, NULL, NULL)) {
3073 		unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
3074 		goto out;
3075 	}
3076 
3077 	if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
3078 		unittest(0, "could not find unittest0 @ \"%s\"\n",
3079 				unittest_path(100, PDEV_OVERLAY));
3080 		goto out;
3081 	}
3082 
3083 	if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
3084 		unittest(0, "unittest1 @ \"%s\" should not exist\n",
3085 				unittest_path(101, PDEV_OVERLAY));
3086 		goto out;
3087 	}
3088 
3089 	unittest(1, "basic infrastructure of overlays passed");
3090 
3091 	/* tests in sequence */
3092 	of_unittest_overlay_0();
3093 	of_unittest_overlay_1();
3094 	of_unittest_overlay_2();
3095 	of_unittest_overlay_3();
3096 	of_unittest_overlay_4();
3097 	of_unittest_overlay_5();
3098 	of_unittest_overlay_6();
3099 	of_unittest_overlay_8();
3100 
3101 	of_unittest_overlay_10();
3102 	of_unittest_overlay_11();
3103 
3104 #if IS_BUILTIN(CONFIG_I2C)
3105 	if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
3106 		goto out;
3107 
3108 	of_unittest_overlay_i2c_12();
3109 	of_unittest_overlay_i2c_13();
3110 	of_unittest_overlay_i2c_14();
3111 	of_unittest_overlay_i2c_15();
3112 
3113 	of_unittest_overlay_i2c_cleanup();
3114 #endif
3115 
3116 	of_unittest_overlay_gpio();
3117 
3118 	of_unittest_remove_tracked_overlays();
3119 
3120 	of_unittest_overlay_notify();
3121 
3122 out:
3123 	of_node_put(bus_np);
3124 }
3125 
3126 #else
3127 static inline void __init of_unittest_overlay(void) { }
3128 #endif
3129 
3130 static void __init of_unittest_lifecycle(void)
3131 {
3132 #ifdef CONFIG_OF_DYNAMIC
3133 	unsigned int refcount;
3134 	int found_refcount_one = 0;
3135 	int put_count = 0;
3136 	struct device_node *np;
3137 	struct device_node *prev_sibling, *next_sibling;
3138 	const char *refcount_path = "/testcase-data/refcount-node";
3139 	const char *refcount_parent_path = "/testcase-data";
3140 
3141 	/*
3142 	 * Node lifecycle tests, non-dynamic node:
3143 	 *
3144 	 * - Decrementing refcount to zero via of_node_put() should cause the
3145 	 *   attempt to free the node memory by of_node_release() to fail
3146 	 *   because the node is not a dynamic node.
3147 	 *
3148 	 * - Decrementing refcount past zero should result in additional
3149 	 *   errors reported.
3150 	 */
3151 
3152 	np = of_find_node_by_path(refcount_path);
3153 	unittest(np, "find refcount_path \"%s\"\n", refcount_path);
3154 	if (np == NULL)
3155 		goto out_skip_tests;
3156 
3157 	while (!found_refcount_one) {
3158 
3159 		if (put_count++ > 10) {
3160 			unittest(0, "guardrail to avoid infinite loop\n");
3161 			goto out_skip_tests;
3162 		}
3163 
3164 		refcount = kref_read(&np->kobj.kref);
3165 		if (refcount == 1)
3166 			found_refcount_one = 1;
3167 		else
3168 			of_node_put(np);
3169 	}
3170 
3171 	EXPECT_BEGIN(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3172 
3173 	/*
3174 	 * refcount is now one, decrementing to zero will result in a call to
3175 	 * of_node_release() to free the node's memory, which should result
3176 	 * in an error
3177 	 */
3178 	unittest(1, "/testcase-data/refcount-node is one");
3179 	of_node_put(np);
3180 
3181 	EXPECT_END(KERN_INFO, "OF: ERROR: of_node_release() detected bad of_node_put() on /testcase-data/refcount-node");
3182 
3183 
3184 	/*
3185 	 * expect stack trace for subsequent of_node_put():
3186 	 *   __refcount_sub_and_test() calls:
3187 	 *   refcount_warn_saturate(r, REFCOUNT_SUB_UAF)
3188 	 *
3189 	 * Not capturing entire WARN_ONCE() trace with EXPECT_*(), just
3190 	 * the first three lines, and the last line.
3191 	 */
3192 	EXPECT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3193 	EXPECT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3194 	EXPECT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3195 	EXPECT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3196 
3197 	/* refcount is now zero, this should fail */
3198 	unittest(1, "/testcase-data/refcount-node is zero");
3199 	of_node_put(np);
3200 
3201 	EXPECT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3202 	EXPECT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3203 	EXPECT_END(KERN_INFO, "WARNING: <<all>>");
3204 	EXPECT_END(KERN_INFO, "------------[ cut here ]------------");
3205 
3206 	/*
3207 	 * Q. do we expect to get yet another warning?
3208 	 * A. no, the WARNING is from WARN_ONCE()
3209 	 */
3210 	EXPECT_NOT_BEGIN(KERN_INFO, "------------[ cut here ]------------");
3211 	EXPECT_NOT_BEGIN(KERN_INFO, "WARNING: <<all>>");
3212 	EXPECT_NOT_BEGIN(KERN_INFO, "refcount_t: underflow; use-after-free.");
3213 	EXPECT_NOT_BEGIN(KERN_INFO, "---[ end trace <<int>> ]---");
3214 
3215 	unittest(1, "/testcase-data/refcount-node is zero, second time");
3216 	of_node_put(np);
3217 
3218 	EXPECT_NOT_END(KERN_INFO, "---[ end trace <<int>> ]---");
3219 	EXPECT_NOT_END(KERN_INFO, "refcount_t: underflow; use-after-free.");
3220 	EXPECT_NOT_END(KERN_INFO, "WARNING: <<all>>");
3221 	EXPECT_NOT_END(KERN_INFO, "------------[ cut here ]------------");
3222 
3223 	/*
3224 	 * refcount of zero will trigger stack traces from any further
3225 	 * attempt to of_node_get() node "refcount-node". One example of
3226 	 * this is where of_unittest_check_node_linkage() will recursively
3227 	 * scan the tree, with 'for_each_child_of_node()' doing an
3228 	 * of_node_get() of the children of a node.
3229 	 *
3230 	 * Prevent the stack trace by removing node "refcount-node" from
3231 	 * its parent's child list.
3232 	 *
3233 	 * WARNING:  EVIL, EVIL, EVIL:
3234 	 *
3235 	 *   Directly manipulate the child list of node /testcase-data to
3236 	 *   remove child refcount-node.  This is ignoring all proper methods
3237 	 *   of removing a child and will leak a small amount of memory.
3238 	 */
3239 
3240 	np = of_find_node_by_path(refcount_parent_path);
3241 	unittest(np, "find refcount_parent_path \"%s\"\n", refcount_parent_path);
3242 	unittest(np, "ERROR: devicetree live tree left in a 'bad state' if test fail\n");
3243 	if (np == NULL)
3244 		return;
3245 
3246 	prev_sibling = np->child;
3247 	next_sibling = prev_sibling->sibling;
3248 	if (!strcmp(prev_sibling->full_name, "refcount-node")) {
3249 		np->child = next_sibling;
3250 		next_sibling = next_sibling->sibling;
3251 	}
3252 	while (next_sibling) {
3253 		if (!strcmp(next_sibling->full_name, "refcount-node"))
3254 			prev_sibling->sibling = next_sibling->sibling;
3255 		prev_sibling = next_sibling;
3256 		next_sibling = next_sibling->sibling;
3257 	}
3258 	of_node_put(np);
3259 
3260 	return;
3261 
3262 out_skip_tests:
3263 #endif
3264 	unittest(0, "One or more lifecycle tests skipped\n");
3265 }
3266 
3267 #ifdef CONFIG_OF_OVERLAY
3268 
3269 /*
3270  * __dtbo_##overlay_name##_begin[] and __dtbo_##overlay_name##_end[] are
3271  * created by cmd_dt_S_dtbo in scripts/Makefile.lib
3272  */
3273 
3274 #define OVERLAY_INFO_EXTERN(overlay_name) \
3275 	extern uint8_t __dtbo_##overlay_name##_begin[]; \
3276 	extern uint8_t __dtbo_##overlay_name##_end[]
3277 
3278 #define OVERLAY_INFO(overlay_name, expected) \
3279 {	.dtbo_begin       = __dtbo_##overlay_name##_begin, \
3280 	.dtbo_end         = __dtbo_##overlay_name##_end, \
3281 	.expected_result = expected, \
3282 	.name            = #overlay_name, \
3283 }
3284 
3285 struct overlay_info {
3286 	uint8_t		*dtbo_begin;
3287 	uint8_t		*dtbo_end;
3288 	int		expected_result;
3289 	int		ovcs_id;
3290 	char		*name;
3291 };
3292 
3293 OVERLAY_INFO_EXTERN(overlay_base);
3294 OVERLAY_INFO_EXTERN(overlay);
3295 OVERLAY_INFO_EXTERN(overlay_0);
3296 OVERLAY_INFO_EXTERN(overlay_1);
3297 OVERLAY_INFO_EXTERN(overlay_2);
3298 OVERLAY_INFO_EXTERN(overlay_3);
3299 OVERLAY_INFO_EXTERN(overlay_4);
3300 OVERLAY_INFO_EXTERN(overlay_5);
3301 OVERLAY_INFO_EXTERN(overlay_6);
3302 OVERLAY_INFO_EXTERN(overlay_7);
3303 OVERLAY_INFO_EXTERN(overlay_8);
3304 OVERLAY_INFO_EXTERN(overlay_9);
3305 OVERLAY_INFO_EXTERN(overlay_10);
3306 OVERLAY_INFO_EXTERN(overlay_11);
3307 OVERLAY_INFO_EXTERN(overlay_12);
3308 OVERLAY_INFO_EXTERN(overlay_13);
3309 OVERLAY_INFO_EXTERN(overlay_15);
3310 OVERLAY_INFO_EXTERN(overlay_16);
3311 OVERLAY_INFO_EXTERN(overlay_17);
3312 OVERLAY_INFO_EXTERN(overlay_18);
3313 OVERLAY_INFO_EXTERN(overlay_19);
3314 OVERLAY_INFO_EXTERN(overlay_20);
3315 OVERLAY_INFO_EXTERN(overlay_gpio_01);
3316 OVERLAY_INFO_EXTERN(overlay_gpio_02a);
3317 OVERLAY_INFO_EXTERN(overlay_gpio_02b);
3318 OVERLAY_INFO_EXTERN(overlay_gpio_03);
3319 OVERLAY_INFO_EXTERN(overlay_gpio_04a);
3320 OVERLAY_INFO_EXTERN(overlay_gpio_04b);
3321 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
3322 OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
3323 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
3324 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
3325 
3326 /* entries found by name */
3327 static struct overlay_info overlays[] = {
3328 	OVERLAY_INFO(overlay_base, -9999),
3329 	OVERLAY_INFO(overlay, 0),
3330 	OVERLAY_INFO(overlay_0, 0),
3331 	OVERLAY_INFO(overlay_1, 0),
3332 	OVERLAY_INFO(overlay_2, 0),
3333 	OVERLAY_INFO(overlay_3, 0),
3334 	OVERLAY_INFO(overlay_4, 0),
3335 	OVERLAY_INFO(overlay_5, 0),
3336 	OVERLAY_INFO(overlay_6, 0),
3337 	OVERLAY_INFO(overlay_7, 0),
3338 	OVERLAY_INFO(overlay_8, 0),
3339 	OVERLAY_INFO(overlay_9, 0),
3340 	OVERLAY_INFO(overlay_10, 0),
3341 	OVERLAY_INFO(overlay_11, 0),
3342 	OVERLAY_INFO(overlay_12, 0),
3343 	OVERLAY_INFO(overlay_13, 0),
3344 	OVERLAY_INFO(overlay_15, 0),
3345 	OVERLAY_INFO(overlay_16, -EBUSY),
3346 	OVERLAY_INFO(overlay_17, -EEXIST),
3347 	OVERLAY_INFO(overlay_18, 0),
3348 	OVERLAY_INFO(overlay_19, 0),
3349 	OVERLAY_INFO(overlay_20, 0),
3350 	OVERLAY_INFO(overlay_gpio_01, 0),
3351 	OVERLAY_INFO(overlay_gpio_02a, 0),
3352 	OVERLAY_INFO(overlay_gpio_02b, 0),
3353 	OVERLAY_INFO(overlay_gpio_03, 0),
3354 	OVERLAY_INFO(overlay_gpio_04a, 0),
3355 	OVERLAY_INFO(overlay_gpio_04b, 0),
3356 	OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
3357 	OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
3358 	OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
3359 	OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
3360 	/* end marker */
3361 	{.dtbo_begin = NULL, .dtbo_end = NULL, .expected_result = 0, .name = NULL}
3362 };
3363 
3364 static struct device_node *overlay_base_root;
3365 
3366 static void * __init dt_alloc_memory(u64 size, u64 align)
3367 {
3368 	void *ptr = memblock_alloc(size, align);
3369 
3370 	if (!ptr)
3371 		panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
3372 		      __func__, size, align);
3373 
3374 	return ptr;
3375 }
3376 
3377 /*
3378  * Create base device tree for the overlay unittest.
3379  *
3380  * This is called from very early boot code.
3381  *
3382  * Do as much as possible the same way as done in __unflatten_device_tree
3383  * and other early boot steps for the normal FDT so that the overlay base
3384  * unflattened tree will have the same characteristics as the real tree
3385  * (such as having memory allocated by the early allocator).  The goal
3386  * is to test "the real thing" as much as possible, and test "test setup
3387  * code" as little as possible.
3388  *
3389  * Have to stop before resolving phandles, because that uses kmalloc.
3390  */
3391 void __init unittest_unflatten_overlay_base(void)
3392 {
3393 	struct overlay_info *info;
3394 	u32 data_size;
3395 	void *new_fdt;
3396 	u32 size;
3397 	int found = 0;
3398 	const char *overlay_name = "overlay_base";
3399 
3400 	for (info = overlays; info && info->name; info++) {
3401 		if (!strcmp(overlay_name, info->name)) {
3402 			found = 1;
3403 			break;
3404 		}
3405 	}
3406 	if (!found) {
3407 		pr_err("no overlay data for %s\n", overlay_name);
3408 		return;
3409 	}
3410 
3411 	info = &overlays[0];
3412 
3413 	if (info->expected_result != -9999) {
3414 		pr_err("No dtb 'overlay_base' to attach\n");
3415 		return;
3416 	}
3417 
3418 	data_size = info->dtbo_end - info->dtbo_begin;
3419 	if (!data_size) {
3420 		pr_err("No dtb 'overlay_base' to attach\n");
3421 		return;
3422 	}
3423 
3424 	size = fdt_totalsize(info->dtbo_begin);
3425 	if (size != data_size) {
3426 		pr_err("dtb 'overlay_base' header totalsize != actual size");
3427 		return;
3428 	}
3429 
3430 	new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
3431 	if (!new_fdt) {
3432 		pr_err("alloc for dtb 'overlay_base' failed");
3433 		return;
3434 	}
3435 
3436 	memcpy(new_fdt, info->dtbo_begin, size);
3437 
3438 	__unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
3439 				dt_alloc_memory, true);
3440 }
3441 
3442 /*
3443  * The purpose of of_unittest_overlay_data_add is to add an
3444  * overlay in the normal fashion.  This is a test of the whole
3445  * picture, instead of testing individual elements.
3446  *
3447  * A secondary purpose is to be able to verify that the contents of
3448  * /proc/device-tree/ contains the updated structure and values from
3449  * the overlay.  That must be verified separately in user space.
3450  *
3451  * Return 0 on unexpected error.
3452  */
3453 static int __init overlay_data_apply(const char *overlay_name, int *ovcs_id)
3454 {
3455 	struct overlay_info *info;
3456 	int found = 0;
3457 	int ret;
3458 	u32 size;
3459 
3460 	for (info = overlays; info && info->name; info++) {
3461 		if (!strcmp(overlay_name, info->name)) {
3462 			found = 1;
3463 			break;
3464 		}
3465 	}
3466 	if (!found) {
3467 		pr_err("no overlay data for %s\n", overlay_name);
3468 		return 0;
3469 	}
3470 
3471 	size = info->dtbo_end - info->dtbo_begin;
3472 	if (!size)
3473 		pr_err("no overlay data for %s\n", overlay_name);
3474 
3475 	ret = of_overlay_fdt_apply(info->dtbo_begin, size, &info->ovcs_id);
3476 	if (ovcs_id)
3477 		*ovcs_id = info->ovcs_id;
3478 	if (ret < 0)
3479 		goto out;
3480 
3481 	pr_debug("%s applied\n", overlay_name);
3482 
3483 out:
3484 	if (ret != info->expected_result)
3485 		pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3486 		       info->expected_result, ret, overlay_name);
3487 
3488 	return (ret == info->expected_result);
3489 }
3490 
3491 /*
3492  * The purpose of of_unittest_overlay_high_level is to add an overlay
3493  * in the normal fashion.  This is a test of the whole picture,
3494  * instead of individual elements.
3495  *
3496  * The first part of the function is _not_ normal overlay usage; it is
3497  * finishing splicing the base overlay device tree into the live tree.
3498  */
3499 static __init void of_unittest_overlay_high_level(void)
3500 {
3501 	struct device_node *last_sibling;
3502 	struct device_node *np;
3503 	struct device_node *of_symbols;
3504 	struct device_node *overlay_base_symbols;
3505 	struct device_node **pprev;
3506 	struct property *prop;
3507 	int ret;
3508 
3509 	if (!overlay_base_root) {
3510 		unittest(0, "overlay_base_root not initialized\n");
3511 		return;
3512 	}
3513 
3514 	/*
3515 	 * Could not fixup phandles in unittest_unflatten_overlay_base()
3516 	 * because kmalloc() was not yet available.
3517 	 */
3518 	of_overlay_mutex_lock();
3519 	of_resolve_phandles(overlay_base_root);
3520 	of_overlay_mutex_unlock();
3521 
3522 
3523 	/*
3524 	 * do not allow overlay_base to duplicate any node already in
3525 	 * tree, this greatly simplifies the code
3526 	 */
3527 
3528 	/*
3529 	 * remove overlay_base_root node "__local_fixups", after
3530 	 * being used by of_resolve_phandles()
3531 	 */
3532 	pprev = &overlay_base_root->child;
3533 	for (np = overlay_base_root->child; np; np = np->sibling) {
3534 		if (of_node_name_eq(np, "__local_fixups__")) {
3535 			*pprev = np->sibling;
3536 			break;
3537 		}
3538 		pprev = &np->sibling;
3539 	}
3540 
3541 	/* remove overlay_base_root node "__symbols__" if in live tree */
3542 	of_symbols = of_get_child_by_name(of_root, "__symbols__");
3543 	if (of_symbols) {
3544 		/* will have to graft properties from node into live tree */
3545 		pprev = &overlay_base_root->child;
3546 		for (np = overlay_base_root->child; np; np = np->sibling) {
3547 			if (of_node_name_eq(np, "__symbols__")) {
3548 				overlay_base_symbols = np;
3549 				*pprev = np->sibling;
3550 				break;
3551 			}
3552 			pprev = &np->sibling;
3553 		}
3554 	}
3555 
3556 	for_each_child_of_node(overlay_base_root, np) {
3557 		struct device_node *base_child;
3558 		for_each_child_of_node(of_root, base_child) {
3559 			if (!strcmp(np->full_name, base_child->full_name)) {
3560 				unittest(0, "illegal node name in overlay_base %pOFn",
3561 					 np);
3562 				of_node_put(np);
3563 				of_node_put(base_child);
3564 				return;
3565 			}
3566 		}
3567 	}
3568 
3569 	/*
3570 	 * overlay 'overlay_base' is not allowed to have root
3571 	 * properties, so only need to splice nodes into main device tree.
3572 	 *
3573 	 * root node of *overlay_base_root will not be freed, it is lost
3574 	 * memory.
3575 	 */
3576 
3577 	for (np = overlay_base_root->child; np; np = np->sibling)
3578 		np->parent = of_root;
3579 
3580 	mutex_lock(&of_mutex);
3581 
3582 	for (last_sibling = np = of_root->child; np; np = np->sibling)
3583 		last_sibling = np;
3584 
3585 	if (last_sibling)
3586 		last_sibling->sibling = overlay_base_root->child;
3587 	else
3588 		of_root->child = overlay_base_root->child;
3589 
3590 	for_each_of_allnodes_from(overlay_base_root, np)
3591 		__of_attach_node_sysfs(np);
3592 
3593 	if (of_symbols) {
3594 		struct property *new_prop;
3595 		for_each_property_of_node(overlay_base_symbols, prop) {
3596 
3597 			new_prop = __of_prop_dup(prop, GFP_KERNEL);
3598 			if (!new_prop) {
3599 				unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
3600 					 prop->name);
3601 				goto err_unlock;
3602 			}
3603 			if (__of_add_property(of_symbols, new_prop)) {
3604 				kfree(new_prop->name);
3605 				kfree(new_prop->value);
3606 				kfree(new_prop);
3607 				/* "name" auto-generated by unflatten */
3608 				if (!strcmp(prop->name, "name"))
3609 					continue;
3610 				unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3611 					 prop->name);
3612 				goto err_unlock;
3613 			}
3614 			if (__of_add_property_sysfs(of_symbols, new_prop)) {
3615 				unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
3616 					 prop->name);
3617 				goto err_unlock;
3618 			}
3619 		}
3620 	}
3621 
3622 	mutex_unlock(&of_mutex);
3623 
3624 
3625 	/* now do the normal overlay usage test */
3626 
3627 	EXPECT_BEGIN(KERN_ERR,
3628 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3629 	EXPECT_BEGIN(KERN_ERR,
3630 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3631 	EXPECT_BEGIN(KERN_ERR,
3632 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3633 	EXPECT_BEGIN(KERN_ERR,
3634 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3635 	EXPECT_BEGIN(KERN_ERR,
3636 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3637 	EXPECT_BEGIN(KERN_ERR,
3638 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3639 	EXPECT_BEGIN(KERN_ERR,
3640 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3641 	EXPECT_BEGIN(KERN_ERR,
3642 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3643 	EXPECT_BEGIN(KERN_ERR,
3644 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3645 	EXPECT_BEGIN(KERN_ERR,
3646 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3647 	EXPECT_BEGIN(KERN_ERR,
3648 		     "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3649 
3650 	ret = overlay_data_apply("overlay", NULL);
3651 
3652 	EXPECT_END(KERN_ERR,
3653 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3654 	EXPECT_END(KERN_ERR,
3655 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3656 	EXPECT_END(KERN_ERR,
3657 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3658 	EXPECT_END(KERN_ERR,
3659 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3660 	EXPECT_END(KERN_ERR,
3661 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3662 	EXPECT_END(KERN_ERR,
3663 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3664 	EXPECT_END(KERN_ERR,
3665 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3666 	EXPECT_END(KERN_ERR,
3667 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3668 	EXPECT_END(KERN_ERR,
3669 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3670 	EXPECT_END(KERN_ERR,
3671 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3672 	EXPECT_END(KERN_ERR,
3673 		   "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3674 
3675 	unittest(ret, "Adding overlay 'overlay' failed\n");
3676 
3677 	EXPECT_BEGIN(KERN_ERR,
3678 		     "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3679 	EXPECT_BEGIN(KERN_ERR,
3680 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3681 
3682 	unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3683 		 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3684 
3685 	EXPECT_END(KERN_ERR,
3686 		   "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3687 	EXPECT_END(KERN_ERR,
3688 		   "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3689 
3690 	EXPECT_BEGIN(KERN_ERR,
3691 		     "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3692 	EXPECT_BEGIN(KERN_ERR,
3693 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3694 	EXPECT_BEGIN(KERN_ERR,
3695 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3696 
3697 	unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3698 		 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3699 
3700 	EXPECT_END(KERN_ERR,
3701 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
3702 	EXPECT_END(KERN_ERR,
3703 		     "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
3704 	EXPECT_END(KERN_ERR,
3705 		     "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
3706 
3707 	unittest(overlay_data_apply("overlay_bad_phandle", NULL),
3708 		 "Adding overlay 'overlay_bad_phandle' failed\n");
3709 
3710 	unittest(overlay_data_apply("overlay_bad_symbol", NULL),
3711 		 "Adding overlay 'overlay_bad_symbol' failed\n");
3712 
3713 	return;
3714 
3715 err_unlock:
3716 	mutex_unlock(&of_mutex);
3717 }
3718 
3719 #else
3720 
3721 static inline __init void of_unittest_overlay_high_level(void) {}
3722 
3723 #endif
3724 
3725 static int __init of_unittest(void)
3726 {
3727 	struct device_node *np;
3728 	int res;
3729 
3730 	pr_info("start of unittest - you will see error messages\n");
3731 
3732 	/* Taint the kernel so we know we've run tests. */
3733 	add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
3734 
3735 	/* adding data for unittest */
3736 
3737 	if (IS_ENABLED(CONFIG_UML))
3738 		unittest_unflatten_overlay_base();
3739 
3740 	res = unittest_data_add();
3741 	if (res)
3742 		return res;
3743 	if (!of_aliases)
3744 		of_aliases = of_find_node_by_path("/aliases");
3745 
3746 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
3747 	if (!np) {
3748 		pr_info("No testcase data in device tree; not running tests\n");
3749 		return 0;
3750 	}
3751 	of_node_put(np);
3752 
3753 	of_unittest_check_tree_linkage();
3754 	of_unittest_check_phandles();
3755 	of_unittest_find_node_by_name();
3756 	of_unittest_dynamic();
3757 	of_unittest_parse_phandle_with_args();
3758 	of_unittest_parse_phandle_with_args_map();
3759 	of_unittest_printf();
3760 	of_unittest_property_string();
3761 	of_unittest_property_copy();
3762 	of_unittest_changeset();
3763 	of_unittest_parse_interrupts();
3764 	of_unittest_parse_interrupts_extended();
3765 	of_unittest_dma_get_max_cpu_address();
3766 	of_unittest_parse_dma_ranges();
3767 	of_unittest_pci_dma_ranges();
3768 	of_unittest_bus_ranges();
3769 	of_unittest_bus_3cell_ranges();
3770 	of_unittest_match_node();
3771 	of_unittest_platform_populate();
3772 	of_unittest_overlay();
3773 	of_unittest_lifecycle();
3774 
3775 	/* Double check linkage after removing testcase data */
3776 	of_unittest_check_tree_linkage();
3777 
3778 	of_unittest_overlay_high_level();
3779 
3780 	pr_info("end of unittest - %i passed, %i failed\n",
3781 		unittest_results.passed, unittest_results.failed);
3782 
3783 	return 0;
3784 }
3785 late_initcall(of_unittest);
3786