xref: /openbmc/linux/drivers/of/unittest.c (revision 160b8e75)
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/bootmem.h>
9 #include <linux/clk.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/hashtable.h>
13 #include <linux/libfdt.h>
14 #include <linux/of.h>
15 #include <linux/of_fdt.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_platform.h>
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 
24 #include <linux/i2c.h>
25 #include <linux/i2c-mux.h>
26 
27 #include <linux/bitops.h>
28 
29 #include "of_private.h"
30 
31 static struct unittest_results {
32 	int passed;
33 	int failed;
34 } unittest_results;
35 
36 #define unittest(result, fmt, ...) ({ \
37 	bool failed = !(result); \
38 	if (failed) { \
39 		unittest_results.failed++; \
40 		pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
41 	} else { \
42 		unittest_results.passed++; \
43 		pr_debug("pass %s():%i\n", __func__, __LINE__); \
44 	} \
45 	failed; \
46 })
47 
48 static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
49 
50 static void __init of_unittest_find_node_by_name(void)
51 {
52 	struct device_node *np;
53 	const char *options, *name;
54 
55 	np = of_find_node_by_path("/testcase-data");
56 	name = kasprintf(GFP_KERNEL, "%pOF", np);
57 	unittest(np && !strcmp("/testcase-data", name),
58 		"find /testcase-data failed\n");
59 	of_node_put(np);
60 	kfree(name);
61 
62 	/* Test if trailing '/' works */
63 	np = of_find_node_by_path("/testcase-data/");
64 	unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
65 
66 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
67 	name = kasprintf(GFP_KERNEL, "%pOF", np);
68 	unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
69 		"find /testcase-data/phandle-tests/consumer-a failed\n");
70 	of_node_put(np);
71 	kfree(name);
72 
73 	np = of_find_node_by_path("testcase-alias");
74 	name = kasprintf(GFP_KERNEL, "%pOF", np);
75 	unittest(np && !strcmp("/testcase-data", name),
76 		"find testcase-alias failed\n");
77 	of_node_put(np);
78 	kfree(name);
79 
80 	/* Test if trailing '/' works on aliases */
81 	np = of_find_node_by_path("testcase-alias/");
82 	unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
83 
84 	np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
85 	name = kasprintf(GFP_KERNEL, "%pOF", np);
86 	unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
87 		"find testcase-alias/phandle-tests/consumer-a failed\n");
88 	of_node_put(np);
89 	kfree(name);
90 
91 	np = of_find_node_by_path("/testcase-data/missing-path");
92 	unittest(!np, "non-existent path returned node %pOF\n", np);
93 	of_node_put(np);
94 
95 	np = of_find_node_by_path("missing-alias");
96 	unittest(!np, "non-existent alias returned node %pOF\n", np);
97 	of_node_put(np);
98 
99 	np = of_find_node_by_path("testcase-alias/missing-path");
100 	unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
101 	of_node_put(np);
102 
103 	np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
104 	unittest(np && !strcmp("testoption", options),
105 		 "option path test failed\n");
106 	of_node_put(np);
107 
108 	np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
109 	unittest(np && !strcmp("test/option", options),
110 		 "option path test, subcase #1 failed\n");
111 	of_node_put(np);
112 
113 	np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
114 	unittest(np && !strcmp("test/option", options),
115 		 "option path test, subcase #2 failed\n");
116 	of_node_put(np);
117 
118 	np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
119 	unittest(np, "NULL option path test failed\n");
120 	of_node_put(np);
121 
122 	np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
123 				       &options);
124 	unittest(np && !strcmp("testaliasoption", options),
125 		 "option alias path test failed\n");
126 	of_node_put(np);
127 
128 	np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
129 				       &options);
130 	unittest(np && !strcmp("test/alias/option", options),
131 		 "option alias path test, subcase #1 failed\n");
132 	of_node_put(np);
133 
134 	np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
135 	unittest(np, "NULL option alias path test failed\n");
136 	of_node_put(np);
137 
138 	options = "testoption";
139 	np = of_find_node_opts_by_path("testcase-alias", &options);
140 	unittest(np && !options, "option clearing test failed\n");
141 	of_node_put(np);
142 
143 	options = "testoption";
144 	np = of_find_node_opts_by_path("/", &options);
145 	unittest(np && !options, "option clearing root node test failed\n");
146 	of_node_put(np);
147 }
148 
149 static void __init of_unittest_dynamic(void)
150 {
151 	struct device_node *np;
152 	struct property *prop;
153 
154 	np = of_find_node_by_path("/testcase-data");
155 	if (!np) {
156 		pr_err("missing testcase data\n");
157 		return;
158 	}
159 
160 	/* Array of 4 properties for the purpose of testing */
161 	prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
162 	if (!prop) {
163 		unittest(0, "kzalloc() failed\n");
164 		return;
165 	}
166 
167 	/* Add a new property - should pass*/
168 	prop->name = "new-property";
169 	prop->value = "new-property-data";
170 	prop->length = strlen(prop->value);
171 	unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
172 
173 	/* Try to add an existing property - should fail */
174 	prop++;
175 	prop->name = "new-property";
176 	prop->value = "new-property-data-should-fail";
177 	prop->length = strlen(prop->value);
178 	unittest(of_add_property(np, prop) != 0,
179 		 "Adding an existing property should have failed\n");
180 
181 	/* Try to modify an existing property - should pass */
182 	prop->value = "modify-property-data-should-pass";
183 	prop->length = strlen(prop->value);
184 	unittest(of_update_property(np, prop) == 0,
185 		 "Updating an existing property should have passed\n");
186 
187 	/* Try to modify non-existent property - should pass*/
188 	prop++;
189 	prop->name = "modify-property";
190 	prop->value = "modify-missing-property-data-should-pass";
191 	prop->length = strlen(prop->value);
192 	unittest(of_update_property(np, prop) == 0,
193 		 "Updating a missing property should have passed\n");
194 
195 	/* Remove property - should pass */
196 	unittest(of_remove_property(np, prop) == 0,
197 		 "Removing a property should have passed\n");
198 
199 	/* Adding very large property - should pass */
200 	prop++;
201 	prop->name = "large-property-PAGE_SIZEx8";
202 	prop->length = PAGE_SIZE * 8;
203 	prop->value = kzalloc(prop->length, GFP_KERNEL);
204 	unittest(prop->value != NULL, "Unable to allocate large buffer\n");
205 	if (prop->value)
206 		unittest(of_add_property(np, prop) == 0,
207 			 "Adding a large property should have passed\n");
208 }
209 
210 static int __init of_unittest_check_node_linkage(struct device_node *np)
211 {
212 	struct device_node *child;
213 	int count = 0, rc;
214 
215 	for_each_child_of_node(np, child) {
216 		if (child->parent != np) {
217 			pr_err("Child node %s links to wrong parent %s\n",
218 				 child->name, np->name);
219 			rc = -EINVAL;
220 			goto put_child;
221 		}
222 
223 		rc = of_unittest_check_node_linkage(child);
224 		if (rc < 0)
225 			goto put_child;
226 		count += rc;
227 	}
228 
229 	return count + 1;
230 put_child:
231 	of_node_put(child);
232 	return rc;
233 }
234 
235 static void __init of_unittest_check_tree_linkage(void)
236 {
237 	struct device_node *np;
238 	int allnode_count = 0, child_count;
239 
240 	if (!of_root)
241 		return;
242 
243 	for_each_of_allnodes(np)
244 		allnode_count++;
245 	child_count = of_unittest_check_node_linkage(of_root);
246 
247 	unittest(child_count > 0, "Device node data structure is corrupted\n");
248 	unittest(child_count == allnode_count,
249 		 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
250 		 allnode_count, child_count);
251 	pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
252 }
253 
254 static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
255 					  const char *expected)
256 {
257 	unsigned char buf[strlen(expected)+10];
258 	int size, i;
259 
260 	/* Baseline; check conversion with a large size limit */
261 	memset(buf, 0xff, sizeof(buf));
262 	size = snprintf(buf, sizeof(buf) - 2, fmt, np);
263 
264 	/* use strcmp() instead of strncmp() here to be absolutely sure strings match */
265 	unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
266 		"sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
267 		fmt, expected, buf);
268 
269 	/* Make sure length limits work */
270 	size++;
271 	for (i = 0; i < 2; i++, size--) {
272 		/* Clear the buffer, and make sure it works correctly still */
273 		memset(buf, 0xff, sizeof(buf));
274 		snprintf(buf, size+1, fmt, np);
275 		unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
276 			"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
277 			size, fmt, expected, buf);
278 	}
279 }
280 
281 static void __init of_unittest_printf(void)
282 {
283 	struct device_node *np;
284 	const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
285 	char phandle_str[16] = "";
286 
287 	np = of_find_node_by_path(full_name);
288 	if (!np) {
289 		unittest(np, "testcase data missing\n");
290 		return;
291 	}
292 
293 	num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
294 
295 	of_unittest_printf_one(np, "%pOF",  full_name);
296 	of_unittest_printf_one(np, "%pOFf", full_name);
297 	of_unittest_printf_one(np, "%pOFp", phandle_str);
298 	of_unittest_printf_one(np, "%pOFP", "dev@100");
299 	of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
300 	of_unittest_printf_one(np, "%10pOFP", "   dev@100");
301 	of_unittest_printf_one(np, "%-10pOFP", "dev@100   ");
302 	of_unittest_printf_one(of_root, "%pOFP", "/");
303 	of_unittest_printf_one(np, "%pOFF", "----");
304 	of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
305 	of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
306 	of_unittest_printf_one(np, "%pOFc", "test-sub-device");
307 	of_unittest_printf_one(np, "%pOFC",
308 			"\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
309 }
310 
311 struct node_hash {
312 	struct hlist_node node;
313 	struct device_node *np;
314 };
315 
316 static DEFINE_HASHTABLE(phandle_ht, 8);
317 static void __init of_unittest_check_phandles(void)
318 {
319 	struct device_node *np;
320 	struct node_hash *nh;
321 	struct hlist_node *tmp;
322 	int i, dup_count = 0, phandle_count = 0;
323 
324 	for_each_of_allnodes(np) {
325 		if (!np->phandle)
326 			continue;
327 
328 		hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
329 			if (nh->np->phandle == np->phandle) {
330 				pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
331 					np->phandle, nh->np, np);
332 				dup_count++;
333 				break;
334 			}
335 		}
336 
337 		nh = kzalloc(sizeof(*nh), GFP_KERNEL);
338 		if (WARN_ON(!nh))
339 			return;
340 
341 		nh->np = np;
342 		hash_add(phandle_ht, &nh->node, np->phandle);
343 		phandle_count++;
344 	}
345 	unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
346 		 dup_count, phandle_count);
347 
348 	/* Clean up */
349 	hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
350 		hash_del(&nh->node);
351 		kfree(nh);
352 	}
353 }
354 
355 static void __init of_unittest_parse_phandle_with_args(void)
356 {
357 	struct device_node *np;
358 	struct of_phandle_args args;
359 	int i, rc;
360 
361 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
362 	if (!np) {
363 		pr_err("missing testcase data\n");
364 		return;
365 	}
366 
367 	rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
368 	unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
369 
370 	for (i = 0; i < 8; i++) {
371 		bool passed = true;
372 
373 		rc = of_parse_phandle_with_args(np, "phandle-list",
374 						"#phandle-cells", i, &args);
375 
376 		/* Test the values from tests-phandle.dtsi */
377 		switch (i) {
378 		case 0:
379 			passed &= !rc;
380 			passed &= (args.args_count == 1);
381 			passed &= (args.args[0] == (i + 1));
382 			break;
383 		case 1:
384 			passed &= !rc;
385 			passed &= (args.args_count == 2);
386 			passed &= (args.args[0] == (i + 1));
387 			passed &= (args.args[1] == 0);
388 			break;
389 		case 2:
390 			passed &= (rc == -ENOENT);
391 			break;
392 		case 3:
393 			passed &= !rc;
394 			passed &= (args.args_count == 3);
395 			passed &= (args.args[0] == (i + 1));
396 			passed &= (args.args[1] == 4);
397 			passed &= (args.args[2] == 3);
398 			break;
399 		case 4:
400 			passed &= !rc;
401 			passed &= (args.args_count == 2);
402 			passed &= (args.args[0] == (i + 1));
403 			passed &= (args.args[1] == 100);
404 			break;
405 		case 5:
406 			passed &= !rc;
407 			passed &= (args.args_count == 0);
408 			break;
409 		case 6:
410 			passed &= !rc;
411 			passed &= (args.args_count == 1);
412 			passed &= (args.args[0] == (i + 1));
413 			break;
414 		case 7:
415 			passed &= (rc == -ENOENT);
416 			break;
417 		default:
418 			passed = false;
419 		}
420 
421 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
422 			 i, args.np, rc);
423 	}
424 
425 	/* Check for missing list property */
426 	rc = of_parse_phandle_with_args(np, "phandle-list-missing",
427 					"#phandle-cells", 0, &args);
428 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
429 	rc = of_count_phandle_with_args(np, "phandle-list-missing",
430 					"#phandle-cells");
431 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
432 
433 	/* Check for missing cells property */
434 	rc = of_parse_phandle_with_args(np, "phandle-list",
435 					"#phandle-cells-missing", 0, &args);
436 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
437 	rc = of_count_phandle_with_args(np, "phandle-list",
438 					"#phandle-cells-missing");
439 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
440 
441 	/* Check for bad phandle in list */
442 	rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
443 					"#phandle-cells", 0, &args);
444 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
445 	rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
446 					"#phandle-cells");
447 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
448 
449 	/* Check for incorrectly formed argument list */
450 	rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
451 					"#phandle-cells", 1, &args);
452 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
453 	rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
454 					"#phandle-cells");
455 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
456 }
457 
458 static void __init of_unittest_property_string(void)
459 {
460 	const char *strings[4];
461 	struct device_node *np;
462 	int rc;
463 
464 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
465 	if (!np) {
466 		pr_err("No testcase data in device tree\n");
467 		return;
468 	}
469 
470 	rc = of_property_match_string(np, "phandle-list-names", "first");
471 	unittest(rc == 0, "first expected:0 got:%i\n", rc);
472 	rc = of_property_match_string(np, "phandle-list-names", "second");
473 	unittest(rc == 1, "second expected:1 got:%i\n", rc);
474 	rc = of_property_match_string(np, "phandle-list-names", "third");
475 	unittest(rc == 2, "third expected:2 got:%i\n", rc);
476 	rc = of_property_match_string(np, "phandle-list-names", "fourth");
477 	unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
478 	rc = of_property_match_string(np, "missing-property", "blah");
479 	unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
480 	rc = of_property_match_string(np, "empty-property", "blah");
481 	unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
482 	rc = of_property_match_string(np, "unterminated-string", "blah");
483 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
484 
485 	/* of_property_count_strings() tests */
486 	rc = of_property_count_strings(np, "string-property");
487 	unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
488 	rc = of_property_count_strings(np, "phandle-list-names");
489 	unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
490 	rc = of_property_count_strings(np, "unterminated-string");
491 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
492 	rc = of_property_count_strings(np, "unterminated-string-list");
493 	unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
494 
495 	/* of_property_read_string_index() tests */
496 	rc = of_property_read_string_index(np, "string-property", 0, strings);
497 	unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
498 	strings[0] = NULL;
499 	rc = of_property_read_string_index(np, "string-property", 1, strings);
500 	unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
501 	rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
502 	unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
503 	rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
504 	unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
505 	rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
506 	unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
507 	strings[0] = NULL;
508 	rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
509 	unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
510 	strings[0] = NULL;
511 	rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
512 	unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
513 	rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
514 	unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
515 	strings[0] = NULL;
516 	rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
517 	unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
518 	strings[1] = NULL;
519 
520 	/* of_property_read_string_array() tests */
521 	rc = of_property_read_string_array(np, "string-property", strings, 4);
522 	unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
523 	rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
524 	unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
525 	rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
526 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
527 	/* -- An incorrectly formed string should cause a failure */
528 	rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
529 	unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
530 	/* -- parsing the correctly formed strings should still work: */
531 	strings[2] = NULL;
532 	rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
533 	unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
534 	strings[1] = NULL;
535 	rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
536 	unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
537 }
538 
539 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
540 			(p1)->value && (p2)->value && \
541 			!memcmp((p1)->value, (p2)->value, (p1)->length) && \
542 			!strcmp((p1)->name, (p2)->name))
543 static void __init of_unittest_property_copy(void)
544 {
545 #ifdef CONFIG_OF_DYNAMIC
546 	struct property p1 = { .name = "p1", .length = 0, .value = "" };
547 	struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
548 	struct property *new;
549 
550 	new = __of_prop_dup(&p1, GFP_KERNEL);
551 	unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
552 	kfree(new->value);
553 	kfree(new->name);
554 	kfree(new);
555 
556 	new = __of_prop_dup(&p2, GFP_KERNEL);
557 	unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
558 	kfree(new->value);
559 	kfree(new->name);
560 	kfree(new);
561 #endif
562 }
563 
564 static void __init of_unittest_changeset(void)
565 {
566 #ifdef CONFIG_OF_DYNAMIC
567 	struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
568 	struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
569 	struct property *ppremove;
570 	struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
571 	struct of_changeset chgset;
572 
573 	n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
574 	unittest(n1, "testcase setup failure\n");
575 	n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
576 	unittest(n2, "testcase setup failure\n");
577 	n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
578 	unittest(n21, "testcase setup failure %p\n", n21);
579 	nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
580 	unittest(nremove, "testcase setup failure\n");
581 	ppadd = __of_prop_dup(&padd, GFP_KERNEL);
582 	unittest(ppadd, "testcase setup failure\n");
583 	ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
584 	unittest(ppupdate, "testcase setup failure\n");
585 	parent = nremove->parent;
586 	n1->parent = parent;
587 	n2->parent = parent;
588 	n21->parent = n2;
589 	n2->child = n21;
590 	ppremove = of_find_property(parent, "prop-remove", NULL);
591 	unittest(ppremove, "failed to find removal prop");
592 
593 	of_changeset_init(&chgset);
594 	unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
595 	unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
596 	unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
597 	unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
598 	unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
599 	unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
600 	unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
601 	unittest(!of_changeset_apply(&chgset), "apply failed\n");
602 
603 	/* Make sure node names are constructed correctly */
604 	unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
605 		 "'%pOF' not added\n", n21);
606 	of_node_put(np);
607 
608 	unittest(!of_changeset_revert(&chgset), "revert failed\n");
609 
610 	of_changeset_destroy(&chgset);
611 #endif
612 }
613 
614 static void __init of_unittest_parse_interrupts(void)
615 {
616 	struct device_node *np;
617 	struct of_phandle_args args;
618 	int i, rc;
619 
620 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
621 	if (!np) {
622 		pr_err("missing testcase data\n");
623 		return;
624 	}
625 
626 	for (i = 0; i < 4; i++) {
627 		bool passed = true;
628 
629 		args.args_count = 0;
630 		rc = of_irq_parse_one(np, i, &args);
631 
632 		passed &= !rc;
633 		passed &= (args.args_count == 1);
634 		passed &= (args.args[0] == (i + 1));
635 
636 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
637 			 i, args.np, rc);
638 	}
639 	of_node_put(np);
640 
641 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
642 	if (!np) {
643 		pr_err("missing testcase data\n");
644 		return;
645 	}
646 
647 	for (i = 0; i < 4; i++) {
648 		bool passed = true;
649 
650 		args.args_count = 0;
651 		rc = of_irq_parse_one(np, i, &args);
652 
653 		/* Test the values from tests-phandle.dtsi */
654 		switch (i) {
655 		case 0:
656 			passed &= !rc;
657 			passed &= (args.args_count == 1);
658 			passed &= (args.args[0] == 9);
659 			break;
660 		case 1:
661 			passed &= !rc;
662 			passed &= (args.args_count == 3);
663 			passed &= (args.args[0] == 10);
664 			passed &= (args.args[1] == 11);
665 			passed &= (args.args[2] == 12);
666 			break;
667 		case 2:
668 			passed &= !rc;
669 			passed &= (args.args_count == 2);
670 			passed &= (args.args[0] == 13);
671 			passed &= (args.args[1] == 14);
672 			break;
673 		case 3:
674 			passed &= !rc;
675 			passed &= (args.args_count == 2);
676 			passed &= (args.args[0] == 15);
677 			passed &= (args.args[1] == 16);
678 			break;
679 		default:
680 			passed = false;
681 		}
682 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
683 			 i, args.np, rc);
684 	}
685 	of_node_put(np);
686 }
687 
688 static void __init of_unittest_parse_interrupts_extended(void)
689 {
690 	struct device_node *np;
691 	struct of_phandle_args args;
692 	int i, rc;
693 
694 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
695 	if (!np) {
696 		pr_err("missing testcase data\n");
697 		return;
698 	}
699 
700 	for (i = 0; i < 7; i++) {
701 		bool passed = true;
702 
703 		rc = of_irq_parse_one(np, i, &args);
704 
705 		/* Test the values from tests-phandle.dtsi */
706 		switch (i) {
707 		case 0:
708 			passed &= !rc;
709 			passed &= (args.args_count == 1);
710 			passed &= (args.args[0] == 1);
711 			break;
712 		case 1:
713 			passed &= !rc;
714 			passed &= (args.args_count == 3);
715 			passed &= (args.args[0] == 2);
716 			passed &= (args.args[1] == 3);
717 			passed &= (args.args[2] == 4);
718 			break;
719 		case 2:
720 			passed &= !rc;
721 			passed &= (args.args_count == 2);
722 			passed &= (args.args[0] == 5);
723 			passed &= (args.args[1] == 6);
724 			break;
725 		case 3:
726 			passed &= !rc;
727 			passed &= (args.args_count == 1);
728 			passed &= (args.args[0] == 9);
729 			break;
730 		case 4:
731 			passed &= !rc;
732 			passed &= (args.args_count == 3);
733 			passed &= (args.args[0] == 10);
734 			passed &= (args.args[1] == 11);
735 			passed &= (args.args[2] == 12);
736 			break;
737 		case 5:
738 			passed &= !rc;
739 			passed &= (args.args_count == 2);
740 			passed &= (args.args[0] == 13);
741 			passed &= (args.args[1] == 14);
742 			break;
743 		case 6:
744 			passed &= !rc;
745 			passed &= (args.args_count == 1);
746 			passed &= (args.args[0] == 15);
747 			break;
748 		default:
749 			passed = false;
750 		}
751 
752 		unittest(passed, "index %i - data error on node %pOF rc=%i\n",
753 			 i, args.np, rc);
754 	}
755 	of_node_put(np);
756 }
757 
758 static const struct of_device_id match_node_table[] = {
759 	{ .data = "A", .name = "name0", }, /* Name alone is lowest priority */
760 	{ .data = "B", .type = "type1", }, /* followed by type alone */
761 
762 	{ .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
763 	{ .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
764 	{ .data = "Cc", .name = "name2", .type = "type2", },
765 
766 	{ .data = "E", .compatible = "compat3" },
767 	{ .data = "G", .compatible = "compat2", },
768 	{ .data = "H", .compatible = "compat2", .name = "name5", },
769 	{ .data = "I", .compatible = "compat2", .type = "type1", },
770 	{ .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
771 	{ .data = "K", .compatible = "compat2", .name = "name9", },
772 	{}
773 };
774 
775 static struct {
776 	const char *path;
777 	const char *data;
778 } match_node_tests[] = {
779 	{ .path = "/testcase-data/match-node/name0", .data = "A", },
780 	{ .path = "/testcase-data/match-node/name1", .data = "B", },
781 	{ .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
782 	{ .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
783 	{ .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
784 	{ .path = "/testcase-data/match-node/name3", .data = "E", },
785 	{ .path = "/testcase-data/match-node/name4", .data = "G", },
786 	{ .path = "/testcase-data/match-node/name5", .data = "H", },
787 	{ .path = "/testcase-data/match-node/name6", .data = "G", },
788 	{ .path = "/testcase-data/match-node/name7", .data = "I", },
789 	{ .path = "/testcase-data/match-node/name8", .data = "J", },
790 	{ .path = "/testcase-data/match-node/name9", .data = "K", },
791 };
792 
793 static void __init of_unittest_match_node(void)
794 {
795 	struct device_node *np;
796 	const struct of_device_id *match;
797 	int i;
798 
799 	for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
800 		np = of_find_node_by_path(match_node_tests[i].path);
801 		if (!np) {
802 			unittest(0, "missing testcase node %s\n",
803 				match_node_tests[i].path);
804 			continue;
805 		}
806 
807 		match = of_match_node(match_node_table, np);
808 		if (!match) {
809 			unittest(0, "%s didn't match anything\n",
810 				match_node_tests[i].path);
811 			continue;
812 		}
813 
814 		if (strcmp(match->data, match_node_tests[i].data) != 0) {
815 			unittest(0, "%s got wrong match. expected %s, got %s\n",
816 				match_node_tests[i].path, match_node_tests[i].data,
817 				(const char *)match->data);
818 			continue;
819 		}
820 		unittest(1, "passed");
821 	}
822 }
823 
824 static struct resource test_bus_res = {
825 	.start = 0xfffffff8,
826 	.end = 0xfffffff9,
827 	.flags = IORESOURCE_MEM,
828 };
829 static const struct platform_device_info test_bus_info = {
830 	.name = "unittest-bus",
831 };
832 static void __init of_unittest_platform_populate(void)
833 {
834 	int irq, rc;
835 	struct device_node *np, *child, *grandchild;
836 	struct platform_device *pdev, *test_bus;
837 	const struct of_device_id match[] = {
838 		{ .compatible = "test-device", },
839 		{}
840 	};
841 
842 	np = of_find_node_by_path("/testcase-data");
843 	of_platform_default_populate(np, NULL, NULL);
844 
845 	/* Test that a missing irq domain returns -EPROBE_DEFER */
846 	np = of_find_node_by_path("/testcase-data/testcase-device1");
847 	pdev = of_find_device_by_node(np);
848 	unittest(pdev, "device 1 creation failed\n");
849 
850 	irq = platform_get_irq(pdev, 0);
851 	unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
852 
853 	/* Test that a parsing failure does not return -EPROBE_DEFER */
854 	np = of_find_node_by_path("/testcase-data/testcase-device2");
855 	pdev = of_find_device_by_node(np);
856 	unittest(pdev, "device 2 creation failed\n");
857 	irq = platform_get_irq(pdev, 0);
858 	unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
859 
860 	np = of_find_node_by_path("/testcase-data/platform-tests");
861 	unittest(np, "No testcase data in device tree\n");
862 	if (!np)
863 		return;
864 
865 	test_bus = platform_device_register_full(&test_bus_info);
866 	rc = PTR_ERR_OR_ZERO(test_bus);
867 	unittest(!rc, "testbus registration failed; rc=%i\n", rc);
868 	if (rc)
869 		return;
870 	test_bus->dev.of_node = np;
871 
872 	/*
873 	 * Add a dummy resource to the test bus node after it is
874 	 * registered to catch problems with un-inserted resources. The
875 	 * DT code doesn't insert the resources, and it has caused the
876 	 * kernel to oops in the past. This makes sure the same bug
877 	 * doesn't crop up again.
878 	 */
879 	platform_device_add_resources(test_bus, &test_bus_res, 1);
880 
881 	of_platform_populate(np, match, NULL, &test_bus->dev);
882 	for_each_child_of_node(np, child) {
883 		for_each_child_of_node(child, grandchild)
884 			unittest(of_find_device_by_node(grandchild),
885 				 "Could not create device for node '%s'\n",
886 				 grandchild->name);
887 	}
888 
889 	of_platform_depopulate(&test_bus->dev);
890 	for_each_child_of_node(np, child) {
891 		for_each_child_of_node(child, grandchild)
892 			unittest(!of_find_device_by_node(grandchild),
893 				 "device didn't get destroyed '%s'\n",
894 				 grandchild->name);
895 	}
896 
897 	platform_device_unregister(test_bus);
898 	of_node_put(np);
899 }
900 
901 /**
902  *	update_node_properties - adds the properties
903  *	of np into dup node (present in live tree) and
904  *	updates parent of children of np to dup.
905  *
906  *	@np:	node already present in live tree
907  *	@dup:	node present in live tree to be updated
908  */
909 static void update_node_properties(struct device_node *np,
910 					struct device_node *dup)
911 {
912 	struct property *prop;
913 	struct device_node *child;
914 
915 	for_each_property_of_node(np, prop)
916 		of_add_property(dup, prop);
917 
918 	for_each_child_of_node(np, child)
919 		child->parent = dup;
920 }
921 
922 /**
923  *	attach_node_and_children - attaches nodes
924  *	and its children to live tree
925  *
926  *	@np:	Node to attach to live tree
927  */
928 static int attach_node_and_children(struct device_node *np)
929 {
930 	struct device_node *next, *dup, *child;
931 	unsigned long flags;
932 	const char *full_name;
933 
934 	full_name = kasprintf(GFP_KERNEL, "%pOF", np);
935 	dup = of_find_node_by_path(full_name);
936 	kfree(full_name);
937 	if (dup) {
938 		update_node_properties(np, dup);
939 		return 0;
940 	}
941 
942 	child = np->child;
943 	np->child = NULL;
944 
945 	mutex_lock(&of_mutex);
946 	raw_spin_lock_irqsave(&devtree_lock, flags);
947 	np->sibling = np->parent->child;
948 	np->parent->child = np;
949 	of_node_clear_flag(np, OF_DETACHED);
950 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
951 
952 	__of_attach_node_sysfs(np);
953 	mutex_unlock(&of_mutex);
954 
955 	while (child) {
956 		next = child->sibling;
957 		attach_node_and_children(child);
958 		child = next;
959 	}
960 
961 	return 0;
962 }
963 
964 /**
965  *	unittest_data_add - Reads, copies data from
966  *	linked tree and attaches it to the live tree
967  */
968 static int __init unittest_data_add(void)
969 {
970 	void *unittest_data;
971 	struct device_node *unittest_data_node, *np;
972 	/*
973 	 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
974 	 * created by cmd_dt_S_dtb in scripts/Makefile.lib
975 	 */
976 	extern uint8_t __dtb_testcases_begin[];
977 	extern uint8_t __dtb_testcases_end[];
978 	const int size = __dtb_testcases_end - __dtb_testcases_begin;
979 	int rc;
980 
981 	if (!size) {
982 		pr_warn("%s: No testcase data to attach; not running tests\n",
983 			__func__);
984 		return -ENODATA;
985 	}
986 
987 	/* creating copy */
988 	unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
989 
990 	if (!unittest_data) {
991 		pr_warn("%s: Failed to allocate memory for unittest_data; "
992 			"not running tests\n", __func__);
993 		return -ENOMEM;
994 	}
995 	of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
996 	if (!unittest_data_node) {
997 		pr_warn("%s: No tree to attach; not running tests\n", __func__);
998 		return -ENODATA;
999 	}
1000 
1001 	/*
1002 	 * This lock normally encloses of_resolve_phandles()
1003 	 */
1004 	of_overlay_mutex_lock();
1005 
1006 	rc = of_resolve_phandles(unittest_data_node);
1007 	if (rc) {
1008 		pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1009 		of_overlay_mutex_unlock();
1010 		return -EINVAL;
1011 	}
1012 
1013 	if (!of_root) {
1014 		of_root = unittest_data_node;
1015 		for_each_of_allnodes(np)
1016 			__of_attach_node_sysfs(np);
1017 		of_aliases = of_find_node_by_path("/aliases");
1018 		of_chosen = of_find_node_by_path("/chosen");
1019 		of_overlay_mutex_unlock();
1020 		return 0;
1021 	}
1022 
1023 	/* attach the sub-tree to live tree */
1024 	np = unittest_data_node->child;
1025 	while (np) {
1026 		struct device_node *next = np->sibling;
1027 
1028 		np->parent = of_root;
1029 		attach_node_and_children(np);
1030 		np = next;
1031 	}
1032 
1033 	of_overlay_mutex_unlock();
1034 
1035 	return 0;
1036 }
1037 
1038 #ifdef CONFIG_OF_OVERLAY
1039 
1040 static int unittest_probe(struct platform_device *pdev)
1041 {
1042 	struct device *dev = &pdev->dev;
1043 	struct device_node *np = dev->of_node;
1044 
1045 	if (np == NULL) {
1046 		dev_err(dev, "No OF data for device\n");
1047 		return -EINVAL;
1048 
1049 	}
1050 
1051 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1052 
1053 	of_platform_populate(np, NULL, NULL, &pdev->dev);
1054 
1055 	return 0;
1056 }
1057 
1058 static int unittest_remove(struct platform_device *pdev)
1059 {
1060 	struct device *dev = &pdev->dev;
1061 	struct device_node *np = dev->of_node;
1062 
1063 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1064 	return 0;
1065 }
1066 
1067 static const struct of_device_id unittest_match[] = {
1068 	{ .compatible = "unittest", },
1069 	{},
1070 };
1071 
1072 static struct platform_driver unittest_driver = {
1073 	.probe			= unittest_probe,
1074 	.remove			= unittest_remove,
1075 	.driver = {
1076 		.name		= "unittest",
1077 		.of_match_table	= of_match_ptr(unittest_match),
1078 	},
1079 };
1080 
1081 /* get the platform device instantiated at the path */
1082 static struct platform_device *of_path_to_platform_device(const char *path)
1083 {
1084 	struct device_node *np;
1085 	struct platform_device *pdev;
1086 
1087 	np = of_find_node_by_path(path);
1088 	if (np == NULL)
1089 		return NULL;
1090 
1091 	pdev = of_find_device_by_node(np);
1092 	of_node_put(np);
1093 
1094 	return pdev;
1095 }
1096 
1097 /* find out if a platform device exists at that path */
1098 static int of_path_platform_device_exists(const char *path)
1099 {
1100 	struct platform_device *pdev;
1101 
1102 	pdev = of_path_to_platform_device(path);
1103 	platform_device_put(pdev);
1104 	return pdev != NULL;
1105 }
1106 
1107 #if IS_BUILTIN(CONFIG_I2C)
1108 
1109 /* get the i2c client device instantiated at the path */
1110 static struct i2c_client *of_path_to_i2c_client(const char *path)
1111 {
1112 	struct device_node *np;
1113 	struct i2c_client *client;
1114 
1115 	np = of_find_node_by_path(path);
1116 	if (np == NULL)
1117 		return NULL;
1118 
1119 	client = of_find_i2c_device_by_node(np);
1120 	of_node_put(np);
1121 
1122 	return client;
1123 }
1124 
1125 /* find out if a i2c client device exists at that path */
1126 static int of_path_i2c_client_exists(const char *path)
1127 {
1128 	struct i2c_client *client;
1129 
1130 	client = of_path_to_i2c_client(path);
1131 	if (client)
1132 		put_device(&client->dev);
1133 	return client != NULL;
1134 }
1135 #else
1136 static int of_path_i2c_client_exists(const char *path)
1137 {
1138 	return 0;
1139 }
1140 #endif
1141 
1142 enum overlay_type {
1143 	PDEV_OVERLAY,
1144 	I2C_OVERLAY
1145 };
1146 
1147 static int of_path_device_type_exists(const char *path,
1148 		enum overlay_type ovtype)
1149 {
1150 	switch (ovtype) {
1151 	case PDEV_OVERLAY:
1152 		return of_path_platform_device_exists(path);
1153 	case I2C_OVERLAY:
1154 		return of_path_i2c_client_exists(path);
1155 	}
1156 	return 0;
1157 }
1158 
1159 static const char *unittest_path(int nr, enum overlay_type ovtype)
1160 {
1161 	const char *base;
1162 	static char buf[256];
1163 
1164 	switch (ovtype) {
1165 	case PDEV_OVERLAY:
1166 		base = "/testcase-data/overlay-node/test-bus";
1167 		break;
1168 	case I2C_OVERLAY:
1169 		base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1170 		break;
1171 	default:
1172 		buf[0] = '\0';
1173 		return buf;
1174 	}
1175 	snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1176 	buf[sizeof(buf) - 1] = '\0';
1177 	return buf;
1178 }
1179 
1180 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1181 {
1182 	const char *path;
1183 
1184 	path = unittest_path(unittest_nr, ovtype);
1185 
1186 	switch (ovtype) {
1187 	case PDEV_OVERLAY:
1188 		return of_path_platform_device_exists(path);
1189 	case I2C_OVERLAY:
1190 		return of_path_i2c_client_exists(path);
1191 	}
1192 	return 0;
1193 }
1194 
1195 static const char *overlay_name_from_nr(int nr)
1196 {
1197 	static char buf[256];
1198 
1199 	snprintf(buf, sizeof(buf) - 1,
1200 		"overlay_%d", nr);
1201 	buf[sizeof(buf) - 1] = '\0';
1202 
1203 	return buf;
1204 }
1205 
1206 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1207 
1208 /* it is guaranteed that overlay ids are assigned in sequence */
1209 #define MAX_UNITTEST_OVERLAYS	256
1210 static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1211 static int overlay_first_id = -1;
1212 
1213 static void of_unittest_track_overlay(int id)
1214 {
1215 	if (overlay_first_id < 0)
1216 		overlay_first_id = id;
1217 	id -= overlay_first_id;
1218 
1219 	/* we shouldn't need that many */
1220 	BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1221 	overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1222 }
1223 
1224 static void of_unittest_untrack_overlay(int id)
1225 {
1226 	if (overlay_first_id < 0)
1227 		return;
1228 	id -= overlay_first_id;
1229 	BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1230 	overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1231 }
1232 
1233 static void of_unittest_destroy_tracked_overlays(void)
1234 {
1235 	int id, ret, defers, ovcs_id;
1236 
1237 	if (overlay_first_id < 0)
1238 		return;
1239 
1240 	/* try until no defers */
1241 	do {
1242 		defers = 0;
1243 		/* remove in reverse order */
1244 		for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1245 			if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1246 				continue;
1247 
1248 			ovcs_id = id + overlay_first_id;
1249 			ret = of_overlay_remove(&ovcs_id);
1250 			if (ret == -ENODEV) {
1251 				pr_warn("%s: no overlay to destroy for #%d\n",
1252 					__func__, id + overlay_first_id);
1253 				continue;
1254 			}
1255 			if (ret != 0) {
1256 				defers++;
1257 				pr_warn("%s: overlay destroy failed for #%d\n",
1258 					__func__, id + overlay_first_id);
1259 				continue;
1260 			}
1261 
1262 			overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1263 		}
1264 	} while (defers > 0);
1265 }
1266 
1267 static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
1268 		int *overlay_id)
1269 {
1270 	struct device_node *np = NULL;
1271 	const char *overlay_name;
1272 	int ret;
1273 
1274 	overlay_name = overlay_name_from_nr(overlay_nr);
1275 
1276 	ret = overlay_data_apply(overlay_name, overlay_id);
1277 	if (!ret) {
1278 		unittest(0, "could not apply overlay \"%s\"\n",
1279 				overlay_name);
1280 		goto out;
1281 	}
1282 	of_unittest_track_overlay(*overlay_id);
1283 
1284 	ret = 0;
1285 
1286 out:
1287 	of_node_put(np);
1288 
1289 	return ret;
1290 }
1291 
1292 /* apply an overlay while checking before and after states */
1293 static int __init of_unittest_apply_overlay_check(int overlay_nr,
1294 		int unittest_nr, int before, int after,
1295 		enum overlay_type ovtype)
1296 {
1297 	int ret, ovcs_id;
1298 
1299 	/* unittest device must not be in before state */
1300 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1301 		unittest(0, "%s with device @\"%s\" %s\n",
1302 				overlay_name_from_nr(overlay_nr),
1303 				unittest_path(unittest_nr, ovtype),
1304 				!before ? "enabled" : "disabled");
1305 		return -EINVAL;
1306 	}
1307 
1308 	ovcs_id = 0;
1309 	ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
1310 	if (ret != 0) {
1311 		/* of_unittest_apply_overlay already called unittest() */
1312 		return ret;
1313 	}
1314 
1315 	/* unittest device must be to set to after state */
1316 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1317 		unittest(0, "%s failed to create @\"%s\" %s\n",
1318 				overlay_name_from_nr(overlay_nr),
1319 				unittest_path(unittest_nr, ovtype),
1320 				!after ? "enabled" : "disabled");
1321 		return -EINVAL;
1322 	}
1323 
1324 	return 0;
1325 }
1326 
1327 /* apply an overlay and then revert it while checking before, after states */
1328 static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
1329 		int unittest_nr, int before, int after,
1330 		enum overlay_type ovtype)
1331 {
1332 	int ret, ovcs_id;
1333 
1334 	/* unittest device must be in before state */
1335 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1336 		unittest(0, "%s with device @\"%s\" %s\n",
1337 				overlay_name_from_nr(overlay_nr),
1338 				unittest_path(unittest_nr, ovtype),
1339 				!before ? "enabled" : "disabled");
1340 		return -EINVAL;
1341 	}
1342 
1343 	/* apply the overlay */
1344 	ovcs_id = 0;
1345 	ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
1346 	if (ret != 0) {
1347 		/* of_unittest_apply_overlay already called unittest() */
1348 		return ret;
1349 	}
1350 
1351 	/* unittest device must be in after state */
1352 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1353 		unittest(0, "%s failed to create @\"%s\" %s\n",
1354 				overlay_name_from_nr(overlay_nr),
1355 				unittest_path(unittest_nr, ovtype),
1356 				!after ? "enabled" : "disabled");
1357 		return -EINVAL;
1358 	}
1359 
1360 	ret = of_overlay_remove(&ovcs_id);
1361 	if (ret != 0) {
1362 		unittest(0, "%s failed to be destroyed @\"%s\"\n",
1363 				overlay_name_from_nr(overlay_nr),
1364 				unittest_path(unittest_nr, ovtype));
1365 		return ret;
1366 	}
1367 
1368 	/* unittest device must be again in before state */
1369 	if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1370 		unittest(0, "%s with device @\"%s\" %s\n",
1371 				overlay_name_from_nr(overlay_nr),
1372 				unittest_path(unittest_nr, ovtype),
1373 				!before ? "enabled" : "disabled");
1374 		return -EINVAL;
1375 	}
1376 
1377 	return 0;
1378 }
1379 
1380 /* test activation of device */
1381 static void __init of_unittest_overlay_0(void)
1382 {
1383 	int ret;
1384 
1385 	/* device should enable */
1386 	ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1387 	if (ret != 0)
1388 		return;
1389 
1390 	unittest(1, "overlay test %d passed\n", 0);
1391 }
1392 
1393 /* test deactivation of device */
1394 static void __init of_unittest_overlay_1(void)
1395 {
1396 	int ret;
1397 
1398 	/* device should disable */
1399 	ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1400 	if (ret != 0)
1401 		return;
1402 
1403 	unittest(1, "overlay test %d passed\n", 1);
1404 }
1405 
1406 /* test activation of device */
1407 static void __init of_unittest_overlay_2(void)
1408 {
1409 	int ret;
1410 
1411 	/* device should enable */
1412 	ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1413 	if (ret != 0)
1414 		return;
1415 
1416 	unittest(1, "overlay test %d passed\n", 2);
1417 }
1418 
1419 /* test deactivation of device */
1420 static void __init of_unittest_overlay_3(void)
1421 {
1422 	int ret;
1423 
1424 	/* device should disable */
1425 	ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1426 	if (ret != 0)
1427 		return;
1428 
1429 	unittest(1, "overlay test %d passed\n", 3);
1430 }
1431 
1432 /* test activation of a full device node */
1433 static void __init of_unittest_overlay_4(void)
1434 {
1435 	int ret;
1436 
1437 	/* device should disable */
1438 	ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1439 	if (ret != 0)
1440 		return;
1441 
1442 	unittest(1, "overlay test %d passed\n", 4);
1443 }
1444 
1445 /* test overlay apply/revert sequence */
1446 static void __init of_unittest_overlay_5(void)
1447 {
1448 	int ret;
1449 
1450 	/* device should disable */
1451 	ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1452 	if (ret != 0)
1453 		return;
1454 
1455 	unittest(1, "overlay test %d passed\n", 5);
1456 }
1457 
1458 /* test overlay application in sequence */
1459 static void __init of_unittest_overlay_6(void)
1460 {
1461 	int ret, i, ov_id[2], ovcs_id;
1462 	int overlay_nr = 6, unittest_nr = 6;
1463 	int before = 0, after = 1;
1464 	const char *overlay_name;
1465 
1466 	/* unittest device must be in before state */
1467 	for (i = 0; i < 2; i++) {
1468 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1469 				!= before) {
1470 			unittest(0, "%s with device @\"%s\" %s\n",
1471 					overlay_name_from_nr(overlay_nr + i),
1472 					unittest_path(unittest_nr + i,
1473 						PDEV_OVERLAY),
1474 					!before ? "enabled" : "disabled");
1475 			return;
1476 		}
1477 	}
1478 
1479 	/* apply the overlays */
1480 	for (i = 0; i < 2; i++) {
1481 
1482 		overlay_name = overlay_name_from_nr(overlay_nr + i);
1483 
1484 		ret = overlay_data_apply(overlay_name, &ovcs_id);
1485 		if (!ret)  {
1486 			unittest(0, "could not apply overlay \"%s\"\n",
1487 					overlay_name);
1488 			return;
1489 		}
1490 		ov_id[i] = ovcs_id;
1491 		of_unittest_track_overlay(ov_id[i]);
1492 	}
1493 
1494 	for (i = 0; i < 2; i++) {
1495 		/* unittest device must be in after state */
1496 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1497 				!= after) {
1498 			unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1499 					overlay_name_from_nr(overlay_nr + i),
1500 					unittest_path(unittest_nr + i,
1501 						PDEV_OVERLAY),
1502 					!after ? "enabled" : "disabled");
1503 			return;
1504 		}
1505 	}
1506 
1507 	for (i = 1; i >= 0; i--) {
1508 		ovcs_id = ov_id[i];
1509 		ret = of_overlay_remove(&ovcs_id);
1510 		if (ret != 0) {
1511 			unittest(0, "%s failed destroy @\"%s\"\n",
1512 					overlay_name_from_nr(overlay_nr + i),
1513 					unittest_path(unittest_nr + i,
1514 						PDEV_OVERLAY));
1515 			return;
1516 		}
1517 		of_unittest_untrack_overlay(ov_id[i]);
1518 	}
1519 
1520 	for (i = 0; i < 2; i++) {
1521 		/* unittest device must be again in before state */
1522 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1523 				!= before) {
1524 			unittest(0, "%s with device @\"%s\" %s\n",
1525 					overlay_name_from_nr(overlay_nr + i),
1526 					unittest_path(unittest_nr + i,
1527 						PDEV_OVERLAY),
1528 					!before ? "enabled" : "disabled");
1529 			return;
1530 		}
1531 	}
1532 
1533 	unittest(1, "overlay test %d passed\n", 6);
1534 }
1535 
1536 /* test overlay application in sequence */
1537 static void __init of_unittest_overlay_8(void)
1538 {
1539 	int ret, i, ov_id[2], ovcs_id;
1540 	int overlay_nr = 8, unittest_nr = 8;
1541 	const char *overlay_name;
1542 
1543 	/* we don't care about device state in this test */
1544 
1545 	/* apply the overlays */
1546 	for (i = 0; i < 2; i++) {
1547 
1548 		overlay_name = overlay_name_from_nr(overlay_nr + i);
1549 
1550 		ret = overlay_data_apply(overlay_name, &ovcs_id);
1551 		if (ret < 0)  {
1552 			unittest(0, "could not apply overlay \"%s\"\n",
1553 					overlay_name);
1554 			return;
1555 		}
1556 		ov_id[i] = ovcs_id;
1557 		of_unittest_track_overlay(ov_id[i]);
1558 	}
1559 
1560 	/* now try to remove first overlay (it should fail) */
1561 	ovcs_id = ov_id[0];
1562 	ret = of_overlay_remove(&ovcs_id);
1563 	if (ret == 0) {
1564 		unittest(0, "%s was destroyed @\"%s\"\n",
1565 				overlay_name_from_nr(overlay_nr + 0),
1566 				unittest_path(unittest_nr,
1567 					PDEV_OVERLAY));
1568 		return;
1569 	}
1570 
1571 	/* removing them in order should work */
1572 	for (i = 1; i >= 0; i--) {
1573 		ovcs_id = ov_id[i];
1574 		ret = of_overlay_remove(&ovcs_id);
1575 		if (ret != 0) {
1576 			unittest(0, "%s not destroyed @\"%s\"\n",
1577 					overlay_name_from_nr(overlay_nr + i),
1578 					unittest_path(unittest_nr,
1579 						PDEV_OVERLAY));
1580 			return;
1581 		}
1582 		of_unittest_untrack_overlay(ov_id[i]);
1583 	}
1584 
1585 	unittest(1, "overlay test %d passed\n", 8);
1586 }
1587 
1588 /* test insertion of a bus with parent devices */
1589 static void __init of_unittest_overlay_10(void)
1590 {
1591 	int ret;
1592 	char *child_path;
1593 
1594 	/* device should disable */
1595 	ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1596 	if (unittest(ret == 0,
1597 			"overlay test %d failed; overlay application\n", 10))
1598 		return;
1599 
1600 	child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1601 			unittest_path(10, PDEV_OVERLAY));
1602 	if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
1603 		return;
1604 
1605 	ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1606 	kfree(child_path);
1607 	if (unittest(ret, "overlay test %d failed; no child device\n", 10))
1608 		return;
1609 }
1610 
1611 /* test insertion of a bus with parent devices (and revert) */
1612 static void __init of_unittest_overlay_11(void)
1613 {
1614 	int ret;
1615 
1616 	/* device should disable */
1617 	ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1618 			PDEV_OVERLAY);
1619 	if (unittest(ret == 0,
1620 			"overlay test %d failed; overlay application\n", 11))
1621 		return;
1622 }
1623 
1624 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1625 
1626 struct unittest_i2c_bus_data {
1627 	struct platform_device	*pdev;
1628 	struct i2c_adapter	adap;
1629 };
1630 
1631 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
1632 		struct i2c_msg *msgs, int num)
1633 {
1634 	struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
1635 
1636 	(void)std;
1637 
1638 	return num;
1639 }
1640 
1641 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
1642 {
1643 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1644 }
1645 
1646 static const struct i2c_algorithm unittest_i2c_algo = {
1647 	.master_xfer	= unittest_i2c_master_xfer,
1648 	.functionality	= unittest_i2c_functionality,
1649 };
1650 
1651 static int unittest_i2c_bus_probe(struct platform_device *pdev)
1652 {
1653 	struct device *dev = &pdev->dev;
1654 	struct device_node *np = dev->of_node;
1655 	struct unittest_i2c_bus_data *std;
1656 	struct i2c_adapter *adap;
1657 	int ret;
1658 
1659 	if (np == NULL) {
1660 		dev_err(dev, "No OF data for device\n");
1661 		return -EINVAL;
1662 
1663 	}
1664 
1665 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1666 
1667 	std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1668 	if (!std) {
1669 		dev_err(dev, "Failed to allocate unittest i2c data\n");
1670 		return -ENOMEM;
1671 	}
1672 
1673 	/* link them together */
1674 	std->pdev = pdev;
1675 	platform_set_drvdata(pdev, std);
1676 
1677 	adap = &std->adap;
1678 	i2c_set_adapdata(adap, std);
1679 	adap->nr = -1;
1680 	strlcpy(adap->name, pdev->name, sizeof(adap->name));
1681 	adap->class = I2C_CLASS_DEPRECATED;
1682 	adap->algo = &unittest_i2c_algo;
1683 	adap->dev.parent = dev;
1684 	adap->dev.of_node = dev->of_node;
1685 	adap->timeout = 5 * HZ;
1686 	adap->retries = 3;
1687 
1688 	ret = i2c_add_numbered_adapter(adap);
1689 	if (ret != 0) {
1690 		dev_err(dev, "Failed to add I2C adapter\n");
1691 		return ret;
1692 	}
1693 
1694 	return 0;
1695 }
1696 
1697 static int unittest_i2c_bus_remove(struct platform_device *pdev)
1698 {
1699 	struct device *dev = &pdev->dev;
1700 	struct device_node *np = dev->of_node;
1701 	struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
1702 
1703 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1704 	i2c_del_adapter(&std->adap);
1705 
1706 	return 0;
1707 }
1708 
1709 static const struct of_device_id unittest_i2c_bus_match[] = {
1710 	{ .compatible = "unittest-i2c-bus", },
1711 	{},
1712 };
1713 
1714 static struct platform_driver unittest_i2c_bus_driver = {
1715 	.probe			= unittest_i2c_bus_probe,
1716 	.remove			= unittest_i2c_bus_remove,
1717 	.driver = {
1718 		.name		= "unittest-i2c-bus",
1719 		.of_match_table	= of_match_ptr(unittest_i2c_bus_match),
1720 	},
1721 };
1722 
1723 static int unittest_i2c_dev_probe(struct i2c_client *client,
1724 		const struct i2c_device_id *id)
1725 {
1726 	struct device *dev = &client->dev;
1727 	struct device_node *np = client->dev.of_node;
1728 
1729 	if (!np) {
1730 		dev_err(dev, "No OF node\n");
1731 		return -EINVAL;
1732 	}
1733 
1734 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1735 
1736 	return 0;
1737 };
1738 
1739 static int unittest_i2c_dev_remove(struct i2c_client *client)
1740 {
1741 	struct device *dev = &client->dev;
1742 	struct device_node *np = client->dev.of_node;
1743 
1744 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1745 	return 0;
1746 }
1747 
1748 static const struct i2c_device_id unittest_i2c_dev_id[] = {
1749 	{ .name = "unittest-i2c-dev" },
1750 	{ }
1751 };
1752 
1753 static struct i2c_driver unittest_i2c_dev_driver = {
1754 	.driver = {
1755 		.name = "unittest-i2c-dev",
1756 	},
1757 	.probe = unittest_i2c_dev_probe,
1758 	.remove = unittest_i2c_dev_remove,
1759 	.id_table = unittest_i2c_dev_id,
1760 };
1761 
1762 #if IS_BUILTIN(CONFIG_I2C_MUX)
1763 
1764 static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
1765 {
1766 	return 0;
1767 }
1768 
1769 static int unittest_i2c_mux_probe(struct i2c_client *client,
1770 		const struct i2c_device_id *id)
1771 {
1772 	int ret, i, nchans;
1773 	struct device *dev = &client->dev;
1774 	struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1775 	struct device_node *np = client->dev.of_node, *child;
1776 	struct i2c_mux_core *muxc;
1777 	u32 reg, max_reg;
1778 
1779 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1780 
1781 	if (!np) {
1782 		dev_err(dev, "No OF node\n");
1783 		return -EINVAL;
1784 	}
1785 
1786 	max_reg = (u32)-1;
1787 	for_each_child_of_node(np, child) {
1788 		ret = of_property_read_u32(child, "reg", &reg);
1789 		if (ret)
1790 			continue;
1791 		if (max_reg == (u32)-1 || reg > max_reg)
1792 			max_reg = reg;
1793 	}
1794 	nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1795 	if (nchans == 0) {
1796 		dev_err(dev, "No channels\n");
1797 		return -EINVAL;
1798 	}
1799 
1800 	muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1801 			     unittest_i2c_mux_select_chan, NULL);
1802 	if (!muxc)
1803 		return -ENOMEM;
1804 	for (i = 0; i < nchans; i++) {
1805 		ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1806 		if (ret) {
1807 			dev_err(dev, "Failed to register mux #%d\n", i);
1808 			i2c_mux_del_adapters(muxc);
1809 			return -ENODEV;
1810 		}
1811 	}
1812 
1813 	i2c_set_clientdata(client, muxc);
1814 
1815 	return 0;
1816 };
1817 
1818 static int unittest_i2c_mux_remove(struct i2c_client *client)
1819 {
1820 	struct device *dev = &client->dev;
1821 	struct device_node *np = client->dev.of_node;
1822 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
1823 
1824 	dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1825 	i2c_mux_del_adapters(muxc);
1826 	return 0;
1827 }
1828 
1829 static const struct i2c_device_id unittest_i2c_mux_id[] = {
1830 	{ .name = "unittest-i2c-mux" },
1831 	{ }
1832 };
1833 
1834 static struct i2c_driver unittest_i2c_mux_driver = {
1835 	.driver = {
1836 		.name = "unittest-i2c-mux",
1837 	},
1838 	.probe = unittest_i2c_mux_probe,
1839 	.remove = unittest_i2c_mux_remove,
1840 	.id_table = unittest_i2c_mux_id,
1841 };
1842 
1843 #endif
1844 
1845 static int of_unittest_overlay_i2c_init(void)
1846 {
1847 	int ret;
1848 
1849 	ret = i2c_add_driver(&unittest_i2c_dev_driver);
1850 	if (unittest(ret == 0,
1851 			"could not register unittest i2c device driver\n"))
1852 		return ret;
1853 
1854 	ret = platform_driver_register(&unittest_i2c_bus_driver);
1855 	if (unittest(ret == 0,
1856 			"could not register unittest i2c bus driver\n"))
1857 		return ret;
1858 
1859 #if IS_BUILTIN(CONFIG_I2C_MUX)
1860 	ret = i2c_add_driver(&unittest_i2c_mux_driver);
1861 	if (unittest(ret == 0,
1862 			"could not register unittest i2c mux driver\n"))
1863 		return ret;
1864 #endif
1865 
1866 	return 0;
1867 }
1868 
1869 static void of_unittest_overlay_i2c_cleanup(void)
1870 {
1871 #if IS_BUILTIN(CONFIG_I2C_MUX)
1872 	i2c_del_driver(&unittest_i2c_mux_driver);
1873 #endif
1874 	platform_driver_unregister(&unittest_i2c_bus_driver);
1875 	i2c_del_driver(&unittest_i2c_dev_driver);
1876 }
1877 
1878 static void __init of_unittest_overlay_i2c_12(void)
1879 {
1880 	int ret;
1881 
1882 	/* device should enable */
1883 	ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1884 	if (ret != 0)
1885 		return;
1886 
1887 	unittest(1, "overlay test %d passed\n", 12);
1888 }
1889 
1890 /* test deactivation of device */
1891 static void __init of_unittest_overlay_i2c_13(void)
1892 {
1893 	int ret;
1894 
1895 	/* device should disable */
1896 	ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1897 	if (ret != 0)
1898 		return;
1899 
1900 	unittest(1, "overlay test %d passed\n", 13);
1901 }
1902 
1903 /* just check for i2c mux existence */
1904 static void of_unittest_overlay_i2c_14(void)
1905 {
1906 }
1907 
1908 static void __init of_unittest_overlay_i2c_15(void)
1909 {
1910 	int ret;
1911 
1912 	/* device should enable */
1913 	ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
1914 	if (ret != 0)
1915 		return;
1916 
1917 	unittest(1, "overlay test %d passed\n", 15);
1918 }
1919 
1920 #else
1921 
1922 static inline void of_unittest_overlay_i2c_14(void) { }
1923 static inline void of_unittest_overlay_i2c_15(void) { }
1924 
1925 #endif
1926 
1927 static void __init of_unittest_overlay(void)
1928 {
1929 	struct device_node *bus_np = NULL;
1930 	int ret;
1931 
1932 	ret = platform_driver_register(&unittest_driver);
1933 	if (ret != 0) {
1934 		unittest(0, "could not register unittest driver\n");
1935 		goto out;
1936 	}
1937 
1938 	bus_np = of_find_node_by_path(bus_path);
1939 	if (bus_np == NULL) {
1940 		unittest(0, "could not find bus_path \"%s\"\n", bus_path);
1941 		goto out;
1942 	}
1943 
1944 	ret = of_platform_default_populate(bus_np, NULL, NULL);
1945 	if (ret != 0) {
1946 		unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
1947 		goto out;
1948 	}
1949 
1950 	if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1951 		unittest(0, "could not find unittest0 @ \"%s\"\n",
1952 				unittest_path(100, PDEV_OVERLAY));
1953 		goto out;
1954 	}
1955 
1956 	if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1957 		unittest(0, "unittest1 @ \"%s\" should not exist\n",
1958 				unittest_path(101, PDEV_OVERLAY));
1959 		goto out;
1960 	}
1961 
1962 	unittest(1, "basic infrastructure of overlays passed");
1963 
1964 	/* tests in sequence */
1965 	of_unittest_overlay_0();
1966 	of_unittest_overlay_1();
1967 	of_unittest_overlay_2();
1968 	of_unittest_overlay_3();
1969 	of_unittest_overlay_4();
1970 	of_unittest_overlay_5();
1971 	of_unittest_overlay_6();
1972 	of_unittest_overlay_8();
1973 
1974 	of_unittest_overlay_10();
1975 	of_unittest_overlay_11();
1976 
1977 #if IS_BUILTIN(CONFIG_I2C)
1978 	if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
1979 		goto out;
1980 
1981 	of_unittest_overlay_i2c_12();
1982 	of_unittest_overlay_i2c_13();
1983 	of_unittest_overlay_i2c_14();
1984 	of_unittest_overlay_i2c_15();
1985 
1986 	of_unittest_overlay_i2c_cleanup();
1987 #endif
1988 
1989 	of_unittest_destroy_tracked_overlays();
1990 
1991 out:
1992 	of_node_put(bus_np);
1993 }
1994 
1995 #else
1996 static inline void __init of_unittest_overlay(void) { }
1997 #endif
1998 
1999 #ifdef CONFIG_OF_OVERLAY
2000 
2001 /*
2002  * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2003  * in scripts/Makefile.lib
2004  */
2005 
2006 #define OVERLAY_INFO_EXTERN(name) \
2007 	extern uint8_t __dtb_##name##_begin[]; \
2008 	extern uint8_t __dtb_##name##_end[]
2009 
2010 #define OVERLAY_INFO(overlay_name, expected)             \
2011 {	.dtb_begin       = __dtb_##overlay_name##_begin, \
2012 	.dtb_end         = __dtb_##overlay_name##_end,   \
2013 	.expected_result = expected,                     \
2014 	.name            = #overlay_name,                \
2015 }
2016 
2017 struct overlay_info {
2018 	uint8_t		*dtb_begin;
2019 	uint8_t		*dtb_end;
2020 	int		expected_result;
2021 	int		overlay_id;
2022 	char		*name;
2023 };
2024 
2025 OVERLAY_INFO_EXTERN(overlay_base);
2026 OVERLAY_INFO_EXTERN(overlay);
2027 OVERLAY_INFO_EXTERN(overlay_0);
2028 OVERLAY_INFO_EXTERN(overlay_1);
2029 OVERLAY_INFO_EXTERN(overlay_2);
2030 OVERLAY_INFO_EXTERN(overlay_3);
2031 OVERLAY_INFO_EXTERN(overlay_4);
2032 OVERLAY_INFO_EXTERN(overlay_5);
2033 OVERLAY_INFO_EXTERN(overlay_6);
2034 OVERLAY_INFO_EXTERN(overlay_7);
2035 OVERLAY_INFO_EXTERN(overlay_8);
2036 OVERLAY_INFO_EXTERN(overlay_9);
2037 OVERLAY_INFO_EXTERN(overlay_10);
2038 OVERLAY_INFO_EXTERN(overlay_11);
2039 OVERLAY_INFO_EXTERN(overlay_12);
2040 OVERLAY_INFO_EXTERN(overlay_13);
2041 OVERLAY_INFO_EXTERN(overlay_15);
2042 OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2043 OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2044 
2045 /* order of entries is hard-coded into users of overlays[] */
2046 static struct overlay_info overlays[] = {
2047 	OVERLAY_INFO(overlay_base, -9999),
2048 	OVERLAY_INFO(overlay, 0),
2049 	OVERLAY_INFO(overlay_0, 0),
2050 	OVERLAY_INFO(overlay_1, 0),
2051 	OVERLAY_INFO(overlay_2, 0),
2052 	OVERLAY_INFO(overlay_3, 0),
2053 	OVERLAY_INFO(overlay_4, 0),
2054 	OVERLAY_INFO(overlay_5, 0),
2055 	OVERLAY_INFO(overlay_6, 0),
2056 	OVERLAY_INFO(overlay_7, 0),
2057 	OVERLAY_INFO(overlay_8, 0),
2058 	OVERLAY_INFO(overlay_9, 0),
2059 	OVERLAY_INFO(overlay_10, 0),
2060 	OVERLAY_INFO(overlay_11, 0),
2061 	OVERLAY_INFO(overlay_12, 0),
2062 	OVERLAY_INFO(overlay_13, 0),
2063 	OVERLAY_INFO(overlay_15, 0),
2064 	OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2065 	OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2066 	{}
2067 };
2068 
2069 static struct device_node *overlay_base_root;
2070 
2071 static void * __init dt_alloc_memory(u64 size, u64 align)
2072 {
2073 	return memblock_virt_alloc(size, align);
2074 }
2075 
2076 /*
2077  * Create base device tree for the overlay unittest.
2078  *
2079  * This is called from very early boot code.
2080  *
2081  * Do as much as possible the same way as done in __unflatten_device_tree
2082  * and other early boot steps for the normal FDT so that the overlay base
2083  * unflattened tree will have the same characteristics as the real tree
2084  * (such as having memory allocated by the early allocator).  The goal
2085  * is to test "the real thing" as much as possible, and test "test setup
2086  * code" as little as possible.
2087  *
2088  * Have to stop before resolving phandles, because that uses kmalloc.
2089  */
2090 void __init unittest_unflatten_overlay_base(void)
2091 {
2092 	struct overlay_info *info;
2093 	u32 data_size;
2094 	void *new_fdt;
2095 	u32 size;
2096 
2097 	info = &overlays[0];
2098 
2099 	if (info->expected_result != -9999) {
2100 		pr_err("No dtb 'overlay_base' to attach\n");
2101 		return;
2102 	}
2103 
2104 	data_size = info->dtb_end - info->dtb_begin;
2105 	if (!data_size) {
2106 		pr_err("No dtb 'overlay_base' to attach\n");
2107 		return;
2108 	}
2109 
2110 	size = fdt_totalsize(info->dtb_begin);
2111 	if (size != data_size) {
2112 		pr_err("dtb 'overlay_base' header totalsize != actual size");
2113 		return;
2114 	}
2115 
2116 	new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2117 	if (!new_fdt) {
2118 		pr_err("alloc for dtb 'overlay_base' failed");
2119 		return;
2120 	}
2121 
2122 	memcpy(new_fdt, info->dtb_begin, size);
2123 
2124 	__unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
2125 				dt_alloc_memory, true);
2126 }
2127 
2128 /*
2129  * The purpose of of_unittest_overlay_data_add is to add an
2130  * overlay in the normal fashion.  This is a test of the whole
2131  * picture, instead of testing individual elements.
2132  *
2133  * A secondary purpose is to be able to verify that the contents of
2134  * /proc/device-tree/ contains the updated structure and values from
2135  * the overlay.  That must be verified separately in user space.
2136  *
2137  * Return 0 on unexpected error.
2138  */
2139 static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
2140 {
2141 	struct overlay_info *info;
2142 	int found = 0;
2143 	int k;
2144 	int ret;
2145 	u32 size;
2146 
2147 	for (k = 0, info = overlays; info && info->name; info++, k++) {
2148 		if (!strcmp(overlay_name, info->name)) {
2149 			found = 1;
2150 			break;
2151 		}
2152 	}
2153 	if (!found) {
2154 		pr_err("no overlay data for %s\n", overlay_name);
2155 		return 0;
2156 	}
2157 
2158 	size = info->dtb_end - info->dtb_begin;
2159 	if (!size) {
2160 		pr_err("no overlay data for %s\n", overlay_name);
2161 		ret = 0;
2162 	}
2163 
2164 	ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
2165 	if (overlay_id)
2166 		*overlay_id = info->overlay_id;
2167 	if (ret < 0)
2168 		goto out;
2169 
2170 	pr_debug("%s applied\n", overlay_name);
2171 
2172 out:
2173 	if (ret != info->expected_result)
2174 		pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2175 		       info->expected_result, ret, overlay_name);
2176 
2177 	return (ret == info->expected_result);
2178 }
2179 
2180 /*
2181  * The purpose of of_unittest_overlay_high_level is to add an overlay
2182  * in the normal fashion.  This is a test of the whole picture,
2183  * instead of individual elements.
2184  *
2185  * The first part of the function is _not_ normal overlay usage; it is
2186  * finishing splicing the base overlay device tree into the live tree.
2187  */
2188 static __init void of_unittest_overlay_high_level(void)
2189 {
2190 	struct device_node *last_sibling;
2191 	struct device_node *np;
2192 	struct device_node *of_symbols;
2193 	struct device_node *overlay_base_symbols;
2194 	struct device_node **pprev;
2195 	struct property *prop;
2196 	int ret;
2197 
2198 	if (!overlay_base_root) {
2199 		unittest(0, "overlay_base_root not initialized\n");
2200 		return;
2201 	}
2202 
2203 	/*
2204 	 * Could not fixup phandles in unittest_unflatten_overlay_base()
2205 	 * because kmalloc() was not yet available.
2206 	 */
2207 	of_overlay_mutex_lock();
2208 	of_resolve_phandles(overlay_base_root);
2209 	of_overlay_mutex_unlock();
2210 
2211 
2212 	/*
2213 	 * do not allow overlay_base to duplicate any node already in
2214 	 * tree, this greatly simplifies the code
2215 	 */
2216 
2217 	/*
2218 	 * remove overlay_base_root node "__local_fixups", after
2219 	 * being used by of_resolve_phandles()
2220 	 */
2221 	pprev = &overlay_base_root->child;
2222 	for (np = overlay_base_root->child; np; np = np->sibling) {
2223 		if (!of_node_cmp(np->name, "__local_fixups__")) {
2224 			*pprev = np->sibling;
2225 			break;
2226 		}
2227 		pprev = &np->sibling;
2228 	}
2229 
2230 	/* remove overlay_base_root node "__symbols__" if in live tree */
2231 	of_symbols = of_get_child_by_name(of_root, "__symbols__");
2232 	if (of_symbols) {
2233 		/* will have to graft properties from node into live tree */
2234 		pprev = &overlay_base_root->child;
2235 		for (np = overlay_base_root->child; np; np = np->sibling) {
2236 			if (!of_node_cmp(np->name, "__symbols__")) {
2237 				overlay_base_symbols = np;
2238 				*pprev = np->sibling;
2239 				break;
2240 			}
2241 			pprev = &np->sibling;
2242 		}
2243 	}
2244 
2245 	for (np = overlay_base_root->child; np; np = np->sibling) {
2246 		if (of_get_child_by_name(of_root, np->name)) {
2247 			unittest(0, "illegal node name in overlay_base %s",
2248 				np->name);
2249 			return;
2250 		}
2251 	}
2252 
2253 	/*
2254 	 * overlay 'overlay_base' is not allowed to have root
2255 	 * properties, so only need to splice nodes into main device tree.
2256 	 *
2257 	 * root node of *overlay_base_root will not be freed, it is lost
2258 	 * memory.
2259 	 */
2260 
2261 	for (np = overlay_base_root->child; np; np = np->sibling)
2262 		np->parent = of_root;
2263 
2264 	mutex_lock(&of_mutex);
2265 
2266 	for (last_sibling = np = of_root->child; np; np = np->sibling)
2267 		last_sibling = np;
2268 
2269 	if (last_sibling)
2270 		last_sibling->sibling = overlay_base_root->child;
2271 	else
2272 		of_root->child = overlay_base_root->child;
2273 
2274 	for_each_of_allnodes_from(overlay_base_root, np)
2275 		__of_attach_node_sysfs(np);
2276 
2277 	if (of_symbols) {
2278 		struct property *new_prop;
2279 		for_each_property_of_node(overlay_base_symbols, prop) {
2280 
2281 			new_prop = __of_prop_dup(prop, GFP_KERNEL);
2282 			if (!new_prop) {
2283 				unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
2284 					 prop->name);
2285 				goto err_unlock;
2286 			}
2287 			ret = __of_add_property(of_symbols, new_prop);
2288 			if (ret) {
2289 				if (!strcmp(new_prop->name, "name")) {
2290 					/* auto-generated by unflatten */
2291 					ret = 0;
2292 					continue;
2293 				}
2294 				unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2295 					 prop->name);
2296 				goto err_unlock;
2297 			}
2298 			ret = __of_add_property_sysfs(of_symbols, new_prop);
2299 			if (ret) {
2300 				unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2301 					 prop->name);
2302 				goto err_unlock;
2303 			}
2304 		}
2305 	}
2306 
2307 	mutex_unlock(&of_mutex);
2308 
2309 
2310 	/* now do the normal overlay usage test */
2311 
2312 	unittest(overlay_data_apply("overlay", NULL),
2313 		 "Adding overlay 'overlay' failed\n");
2314 
2315 	unittest(overlay_data_apply("overlay_bad_phandle", NULL),
2316 		 "Adding overlay 'overlay_bad_phandle' failed\n");
2317 
2318 	unittest(overlay_data_apply("overlay_bad_symbol", NULL),
2319 		 "Adding overlay 'overlay_bad_symbol' failed\n");
2320 
2321 	return;
2322 
2323 err_unlock:
2324 	mutex_unlock(&of_mutex);
2325 }
2326 
2327 #else
2328 
2329 static inline __init void of_unittest_overlay_high_level(void) {}
2330 
2331 #endif
2332 
2333 static int __init of_unittest(void)
2334 {
2335 	struct device_node *np;
2336 	int res;
2337 
2338 	/* adding data for unittest */
2339 	res = unittest_data_add();
2340 	if (res)
2341 		return res;
2342 	if (!of_aliases)
2343 		of_aliases = of_find_node_by_path("/aliases");
2344 
2345 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2346 	if (!np) {
2347 		pr_info("No testcase data in device tree; not running tests\n");
2348 		return 0;
2349 	}
2350 	of_node_put(np);
2351 
2352 	pr_info("start of unittest - you will see error messages\n");
2353 	of_unittest_check_tree_linkage();
2354 	of_unittest_check_phandles();
2355 	of_unittest_find_node_by_name();
2356 	of_unittest_dynamic();
2357 	of_unittest_parse_phandle_with_args();
2358 	of_unittest_printf();
2359 	of_unittest_property_string();
2360 	of_unittest_property_copy();
2361 	of_unittest_changeset();
2362 	of_unittest_parse_interrupts();
2363 	of_unittest_parse_interrupts_extended();
2364 	of_unittest_match_node();
2365 	of_unittest_platform_populate();
2366 	of_unittest_overlay();
2367 
2368 	/* Double check linkage after removing testcase data */
2369 	of_unittest_check_tree_linkage();
2370 
2371 	of_unittest_overlay_high_level();
2372 
2373 	pr_info("end of unittest - %i passed, %i failed\n",
2374 		unittest_results.passed, unittest_results.failed);
2375 
2376 	return 0;
2377 }
2378 late_initcall(of_unittest);
2379