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