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