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