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