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