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