xref: /openbmc/linux/drivers/of/unittest.c (revision e6c81cce)
1 /*
2  * Self tests for device tree subsystem
3  */
4 
5 #define pr_fmt(fmt) "### dt-test ### " fmt
6 
7 #include <linux/clk.h>
8 #include <linux/err.h>
9 #include <linux/errno.h>
10 #include <linux/hashtable.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_fdt.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
22 
23 #include <linux/i2c.h>
24 #include <linux/i2c-mux.h>
25 
26 #include "of_private.h"
27 
28 static struct unittest_results {
29 	int passed;
30 	int failed;
31 } unittest_results;
32 
33 #define unittest(result, fmt, ...) ({ \
34 	bool failed = !(result); \
35 	if (failed) { \
36 		unittest_results.failed++; \
37 		pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
38 	} else { \
39 		unittest_results.passed++; \
40 		pr_debug("pass %s():%i\n", __func__, __LINE__); \
41 	} \
42 	failed; \
43 })
44 
45 static void __init of_unittest_find_node_by_name(void)
46 {
47 	struct device_node *np;
48 	const char *options;
49 
50 	np = of_find_node_by_path("/testcase-data");
51 	unittest(np && !strcmp("/testcase-data", np->full_name),
52 		"find /testcase-data failed\n");
53 	of_node_put(np);
54 
55 	/* Test if trailing '/' works */
56 	np = of_find_node_by_path("/testcase-data/");
57 	unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
58 
59 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
60 	unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
61 		"find /testcase-data/phandle-tests/consumer-a failed\n");
62 	of_node_put(np);
63 
64 	np = of_find_node_by_path("testcase-alias");
65 	unittest(np && !strcmp("/testcase-data", np->full_name),
66 		"find testcase-alias failed\n");
67 	of_node_put(np);
68 
69 	/* Test if trailing '/' works on aliases */
70 	np = of_find_node_by_path("testcase-alias/");
71 	unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
72 
73 	np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
74 	unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
75 		"find testcase-alias/phandle-tests/consumer-a failed\n");
76 	of_node_put(np);
77 
78 	np = of_find_node_by_path("/testcase-data/missing-path");
79 	unittest(!np, "non-existent path returned node %s\n", np->full_name);
80 	of_node_put(np);
81 
82 	np = of_find_node_by_path("missing-alias");
83 	unittest(!np, "non-existent alias returned node %s\n", np->full_name);
84 	of_node_put(np);
85 
86 	np = of_find_node_by_path("testcase-alias/missing-path");
87 	unittest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
88 	of_node_put(np);
89 
90 	np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
91 	unittest(np && !strcmp("testoption", options),
92 		 "option path test failed\n");
93 	of_node_put(np);
94 
95 	np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
96 	unittest(np && !strcmp("test/option", options),
97 		 "option path test, subcase #1 failed\n");
98 	of_node_put(np);
99 
100 	np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
101 	unittest(np && !strcmp("test/option", options),
102 		 "option path test, subcase #2 failed\n");
103 	of_node_put(np);
104 
105 	np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
106 	unittest(np, "NULL option path test failed\n");
107 	of_node_put(np);
108 
109 	np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
110 				       &options);
111 	unittest(np && !strcmp("testaliasoption", options),
112 		 "option alias path test failed\n");
113 	of_node_put(np);
114 
115 	np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
116 				       &options);
117 	unittest(np && !strcmp("test/alias/option", options),
118 		 "option alias path test, subcase #1 failed\n");
119 	of_node_put(np);
120 
121 	np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
122 	unittest(np, "NULL option alias path test failed\n");
123 	of_node_put(np);
124 
125 	options = "testoption";
126 	np = of_find_node_opts_by_path("testcase-alias", &options);
127 	unittest(np && !options, "option clearing test failed\n");
128 	of_node_put(np);
129 
130 	options = "testoption";
131 	np = of_find_node_opts_by_path("/", &options);
132 	unittest(np && !options, "option clearing root node test failed\n");
133 	of_node_put(np);
134 }
135 
136 static void __init of_unittest_dynamic(void)
137 {
138 	struct device_node *np;
139 	struct property *prop;
140 
141 	np = of_find_node_by_path("/testcase-data");
142 	if (!np) {
143 		pr_err("missing testcase data\n");
144 		return;
145 	}
146 
147 	/* Array of 4 properties for the purpose of testing */
148 	prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
149 	if (!prop) {
150 		unittest(0, "kzalloc() failed\n");
151 		return;
152 	}
153 
154 	/* Add a new property - should pass*/
155 	prop->name = "new-property";
156 	prop->value = "new-property-data";
157 	prop->length = strlen(prop->value);
158 	unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
159 
160 	/* Try to add an existing property - should fail */
161 	prop++;
162 	prop->name = "new-property";
163 	prop->value = "new-property-data-should-fail";
164 	prop->length = strlen(prop->value);
165 	unittest(of_add_property(np, prop) != 0,
166 		 "Adding an existing property should have failed\n");
167 
168 	/* Try to modify an existing property - should pass */
169 	prop->value = "modify-property-data-should-pass";
170 	prop->length = strlen(prop->value);
171 	unittest(of_update_property(np, prop) == 0,
172 		 "Updating an existing property should have passed\n");
173 
174 	/* Try to modify non-existent property - should pass*/
175 	prop++;
176 	prop->name = "modify-property";
177 	prop->value = "modify-missing-property-data-should-pass";
178 	prop->length = strlen(prop->value);
179 	unittest(of_update_property(np, prop) == 0,
180 		 "Updating a missing property should have passed\n");
181 
182 	/* Remove property - should pass */
183 	unittest(of_remove_property(np, prop) == 0,
184 		 "Removing a property should have passed\n");
185 
186 	/* Adding very large property - should pass */
187 	prop++;
188 	prop->name = "large-property-PAGE_SIZEx8";
189 	prop->length = PAGE_SIZE * 8;
190 	prop->value = kzalloc(prop->length, GFP_KERNEL);
191 	unittest(prop->value != NULL, "Unable to allocate large buffer\n");
192 	if (prop->value)
193 		unittest(of_add_property(np, prop) == 0,
194 			 "Adding a large property should have passed\n");
195 }
196 
197 static int __init of_unittest_check_node_linkage(struct device_node *np)
198 {
199 	struct device_node *child;
200 	int count = 0, rc;
201 
202 	for_each_child_of_node(np, child) {
203 		if (child->parent != np) {
204 			pr_err("Child node %s links to wrong parent %s\n",
205 				 child->name, np->name);
206 			return -EINVAL;
207 		}
208 
209 		rc = of_unittest_check_node_linkage(child);
210 		if (rc < 0)
211 			return rc;
212 		count += rc;
213 	}
214 
215 	return count + 1;
216 }
217 
218 static void __init of_unittest_check_tree_linkage(void)
219 {
220 	struct device_node *np;
221 	int allnode_count = 0, child_count;
222 
223 	if (!of_root)
224 		return;
225 
226 	for_each_of_allnodes(np)
227 		allnode_count++;
228 	child_count = of_unittest_check_node_linkage(of_root);
229 
230 	unittest(child_count > 0, "Device node data structure is corrupted\n");
231 	unittest(child_count == allnode_count,
232 		 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
233 		 allnode_count, child_count);
234 	pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
235 }
236 
237 struct node_hash {
238 	struct hlist_node node;
239 	struct device_node *np;
240 };
241 
242 static DEFINE_HASHTABLE(phandle_ht, 8);
243 static void __init of_unittest_check_phandles(void)
244 {
245 	struct device_node *np;
246 	struct node_hash *nh;
247 	struct hlist_node *tmp;
248 	int i, dup_count = 0, phandle_count = 0;
249 
250 	for_each_of_allnodes(np) {
251 		if (!np->phandle)
252 			continue;
253 
254 		hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
255 			if (nh->np->phandle == np->phandle) {
256 				pr_info("Duplicate phandle! %i used by %s and %s\n",
257 					np->phandle, nh->np->full_name, np->full_name);
258 				dup_count++;
259 				break;
260 			}
261 		}
262 
263 		nh = kzalloc(sizeof(*nh), GFP_KERNEL);
264 		if (WARN_ON(!nh))
265 			return;
266 
267 		nh->np = np;
268 		hash_add(phandle_ht, &nh->node, np->phandle);
269 		phandle_count++;
270 	}
271 	unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
272 		 dup_count, phandle_count);
273 
274 	/* Clean up */
275 	hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
276 		hash_del(&nh->node);
277 		kfree(nh);
278 	}
279 }
280 
281 static void __init of_unittest_parse_phandle_with_args(void)
282 {
283 	struct device_node *np;
284 	struct of_phandle_args args;
285 	int i, rc;
286 
287 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
288 	if (!np) {
289 		pr_err("missing testcase data\n");
290 		return;
291 	}
292 
293 	rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
294 	unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
295 
296 	for (i = 0; i < 8; i++) {
297 		bool passed = true;
298 
299 		rc = of_parse_phandle_with_args(np, "phandle-list",
300 						"#phandle-cells", i, &args);
301 
302 		/* Test the values from tests-phandle.dtsi */
303 		switch (i) {
304 		case 0:
305 			passed &= !rc;
306 			passed &= (args.args_count == 1);
307 			passed &= (args.args[0] == (i + 1));
308 			break;
309 		case 1:
310 			passed &= !rc;
311 			passed &= (args.args_count == 2);
312 			passed &= (args.args[0] == (i + 1));
313 			passed &= (args.args[1] == 0);
314 			break;
315 		case 2:
316 			passed &= (rc == -ENOENT);
317 			break;
318 		case 3:
319 			passed &= !rc;
320 			passed &= (args.args_count == 3);
321 			passed &= (args.args[0] == (i + 1));
322 			passed &= (args.args[1] == 4);
323 			passed &= (args.args[2] == 3);
324 			break;
325 		case 4:
326 			passed &= !rc;
327 			passed &= (args.args_count == 2);
328 			passed &= (args.args[0] == (i + 1));
329 			passed &= (args.args[1] == 100);
330 			break;
331 		case 5:
332 			passed &= !rc;
333 			passed &= (args.args_count == 0);
334 			break;
335 		case 6:
336 			passed &= !rc;
337 			passed &= (args.args_count == 1);
338 			passed &= (args.args[0] == (i + 1));
339 			break;
340 		case 7:
341 			passed &= (rc == -ENOENT);
342 			break;
343 		default:
344 			passed = false;
345 		}
346 
347 		unittest(passed, "index %i - data error on node %s rc=%i\n",
348 			 i, args.np->full_name, rc);
349 	}
350 
351 	/* Check for missing list property */
352 	rc = of_parse_phandle_with_args(np, "phandle-list-missing",
353 					"#phandle-cells", 0, &args);
354 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
355 	rc = of_count_phandle_with_args(np, "phandle-list-missing",
356 					"#phandle-cells");
357 	unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
358 
359 	/* Check for missing cells property */
360 	rc = of_parse_phandle_with_args(np, "phandle-list",
361 					"#phandle-cells-missing", 0, &args);
362 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
363 	rc = of_count_phandle_with_args(np, "phandle-list",
364 					"#phandle-cells-missing");
365 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
366 
367 	/* Check for bad phandle in list */
368 	rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
369 					"#phandle-cells", 0, &args);
370 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
371 	rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
372 					"#phandle-cells");
373 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
374 
375 	/* Check for incorrectly formed argument list */
376 	rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
377 					"#phandle-cells", 1, &args);
378 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
379 	rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
380 					"#phandle-cells");
381 	unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
382 }
383 
384 static void __init of_unittest_property_string(void)
385 {
386 	const char *strings[4];
387 	struct device_node *np;
388 	int rc;
389 
390 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
391 	if (!np) {
392 		pr_err("No testcase data in device tree\n");
393 		return;
394 	}
395 
396 	rc = of_property_match_string(np, "phandle-list-names", "first");
397 	unittest(rc == 0, "first expected:0 got:%i\n", rc);
398 	rc = of_property_match_string(np, "phandle-list-names", "second");
399 	unittest(rc == 1, "second expected:1 got:%i\n", rc);
400 	rc = of_property_match_string(np, "phandle-list-names", "third");
401 	unittest(rc == 2, "third expected:2 got:%i\n", rc);
402 	rc = of_property_match_string(np, "phandle-list-names", "fourth");
403 	unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
404 	rc = of_property_match_string(np, "missing-property", "blah");
405 	unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
406 	rc = of_property_match_string(np, "empty-property", "blah");
407 	unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
408 	rc = of_property_match_string(np, "unterminated-string", "blah");
409 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
410 
411 	/* of_property_count_strings() tests */
412 	rc = of_property_count_strings(np, "string-property");
413 	unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
414 	rc = of_property_count_strings(np, "phandle-list-names");
415 	unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
416 	rc = of_property_count_strings(np, "unterminated-string");
417 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
418 	rc = of_property_count_strings(np, "unterminated-string-list");
419 	unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
420 
421 	/* of_property_read_string_index() tests */
422 	rc = of_property_read_string_index(np, "string-property", 0, strings);
423 	unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
424 	strings[0] = NULL;
425 	rc = of_property_read_string_index(np, "string-property", 1, strings);
426 	unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
427 	rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
428 	unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
429 	rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
430 	unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
431 	rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
432 	unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
433 	strings[0] = NULL;
434 	rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
435 	unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
436 	strings[0] = NULL;
437 	rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
438 	unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
439 	rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
440 	unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
441 	strings[0] = NULL;
442 	rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
443 	unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
444 	strings[1] = NULL;
445 
446 	/* of_property_read_string_array() tests */
447 	rc = of_property_read_string_array(np, "string-property", strings, 4);
448 	unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
449 	rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
450 	unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
451 	rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
452 	unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
453 	/* -- An incorrectly formed string should cause a failure */
454 	rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
455 	unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
456 	/* -- parsing the correctly formed strings should still work: */
457 	strings[2] = NULL;
458 	rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
459 	unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
460 	strings[1] = NULL;
461 	rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
462 	unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
463 }
464 
465 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
466 			(p1)->value && (p2)->value && \
467 			!memcmp((p1)->value, (p2)->value, (p1)->length) && \
468 			!strcmp((p1)->name, (p2)->name))
469 static void __init of_unittest_property_copy(void)
470 {
471 #ifdef CONFIG_OF_DYNAMIC
472 	struct property p1 = { .name = "p1", .length = 0, .value = "" };
473 	struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
474 	struct property *new;
475 
476 	new = __of_prop_dup(&p1, GFP_KERNEL);
477 	unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
478 	kfree(new->value);
479 	kfree(new->name);
480 	kfree(new);
481 
482 	new = __of_prop_dup(&p2, GFP_KERNEL);
483 	unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
484 	kfree(new->value);
485 	kfree(new->name);
486 	kfree(new);
487 #endif
488 }
489 
490 static void __init of_unittest_changeset(void)
491 {
492 #ifdef CONFIG_OF_DYNAMIC
493 	struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
494 	struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
495 	struct property *ppremove;
496 	struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
497 	struct of_changeset chgset;
498 
499 	n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
500 	unittest(n1, "testcase setup failure\n");
501 	n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
502 	unittest(n2, "testcase setup failure\n");
503 	n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
504 	unittest(n21, "testcase setup failure %p\n", n21);
505 	nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
506 	unittest(nremove, "testcase setup failure\n");
507 	ppadd = __of_prop_dup(&padd, GFP_KERNEL);
508 	unittest(ppadd, "testcase setup failure\n");
509 	ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
510 	unittest(ppupdate, "testcase setup failure\n");
511 	parent = nremove->parent;
512 	n1->parent = parent;
513 	n2->parent = parent;
514 	n21->parent = n2;
515 	n2->child = n21;
516 	ppremove = of_find_property(parent, "prop-remove", NULL);
517 	unittest(ppremove, "failed to find removal prop");
518 
519 	of_changeset_init(&chgset);
520 	unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
521 	unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
522 	unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
523 	unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
524 	unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
525 	unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
526 	unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
527 	mutex_lock(&of_mutex);
528 	unittest(!of_changeset_apply(&chgset), "apply failed\n");
529 	mutex_unlock(&of_mutex);
530 
531 	/* Make sure node names are constructed correctly */
532 	unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
533 		 "'%s' not added\n", n21->full_name);
534 	of_node_put(np);
535 
536 	mutex_lock(&of_mutex);
537 	unittest(!of_changeset_revert(&chgset), "revert failed\n");
538 	mutex_unlock(&of_mutex);
539 
540 	of_changeset_destroy(&chgset);
541 #endif
542 }
543 
544 static void __init of_unittest_parse_interrupts(void)
545 {
546 	struct device_node *np;
547 	struct of_phandle_args args;
548 	int i, rc;
549 
550 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
551 	if (!np) {
552 		pr_err("missing testcase data\n");
553 		return;
554 	}
555 
556 	for (i = 0; i < 4; i++) {
557 		bool passed = true;
558 
559 		args.args_count = 0;
560 		rc = of_irq_parse_one(np, i, &args);
561 
562 		passed &= !rc;
563 		passed &= (args.args_count == 1);
564 		passed &= (args.args[0] == (i + 1));
565 
566 		unittest(passed, "index %i - data error on node %s rc=%i\n",
567 			 i, args.np->full_name, rc);
568 	}
569 	of_node_put(np);
570 
571 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
572 	if (!np) {
573 		pr_err("missing testcase data\n");
574 		return;
575 	}
576 
577 	for (i = 0; i < 4; i++) {
578 		bool passed = true;
579 
580 		args.args_count = 0;
581 		rc = of_irq_parse_one(np, i, &args);
582 
583 		/* Test the values from tests-phandle.dtsi */
584 		switch (i) {
585 		case 0:
586 			passed &= !rc;
587 			passed &= (args.args_count == 1);
588 			passed &= (args.args[0] == 9);
589 			break;
590 		case 1:
591 			passed &= !rc;
592 			passed &= (args.args_count == 3);
593 			passed &= (args.args[0] == 10);
594 			passed &= (args.args[1] == 11);
595 			passed &= (args.args[2] == 12);
596 			break;
597 		case 2:
598 			passed &= !rc;
599 			passed &= (args.args_count == 2);
600 			passed &= (args.args[0] == 13);
601 			passed &= (args.args[1] == 14);
602 			break;
603 		case 3:
604 			passed &= !rc;
605 			passed &= (args.args_count == 2);
606 			passed &= (args.args[0] == 15);
607 			passed &= (args.args[1] == 16);
608 			break;
609 		default:
610 			passed = false;
611 		}
612 		unittest(passed, "index %i - data error on node %s rc=%i\n",
613 			 i, args.np->full_name, rc);
614 	}
615 	of_node_put(np);
616 }
617 
618 static void __init of_unittest_parse_interrupts_extended(void)
619 {
620 	struct device_node *np;
621 	struct of_phandle_args args;
622 	int i, rc;
623 
624 	np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
625 	if (!np) {
626 		pr_err("missing testcase data\n");
627 		return;
628 	}
629 
630 	for (i = 0; i < 7; i++) {
631 		bool passed = true;
632 
633 		rc = of_irq_parse_one(np, i, &args);
634 
635 		/* Test the values from tests-phandle.dtsi */
636 		switch (i) {
637 		case 0:
638 			passed &= !rc;
639 			passed &= (args.args_count == 1);
640 			passed &= (args.args[0] == 1);
641 			break;
642 		case 1:
643 			passed &= !rc;
644 			passed &= (args.args_count == 3);
645 			passed &= (args.args[0] == 2);
646 			passed &= (args.args[1] == 3);
647 			passed &= (args.args[2] == 4);
648 			break;
649 		case 2:
650 			passed &= !rc;
651 			passed &= (args.args_count == 2);
652 			passed &= (args.args[0] == 5);
653 			passed &= (args.args[1] == 6);
654 			break;
655 		case 3:
656 			passed &= !rc;
657 			passed &= (args.args_count == 1);
658 			passed &= (args.args[0] == 9);
659 			break;
660 		case 4:
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 5:
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 6:
674 			passed &= !rc;
675 			passed &= (args.args_count == 1);
676 			passed &= (args.args[0] == 15);
677 			break;
678 		default:
679 			passed = false;
680 		}
681 
682 		unittest(passed, "index %i - data error on node %s rc=%i\n",
683 			 i, args.np->full_name, rc);
684 	}
685 	of_node_put(np);
686 }
687 
688 static const struct of_device_id match_node_table[] = {
689 	{ .data = "A", .name = "name0", }, /* Name alone is lowest priority */
690 	{ .data = "B", .type = "type1", }, /* followed by type alone */
691 
692 	{ .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
693 	{ .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
694 	{ .data = "Cc", .name = "name2", .type = "type2", },
695 
696 	{ .data = "E", .compatible = "compat3" },
697 	{ .data = "G", .compatible = "compat2", },
698 	{ .data = "H", .compatible = "compat2", .name = "name5", },
699 	{ .data = "I", .compatible = "compat2", .type = "type1", },
700 	{ .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
701 	{ .data = "K", .compatible = "compat2", .name = "name9", },
702 	{}
703 };
704 
705 static struct {
706 	const char *path;
707 	const char *data;
708 } match_node_tests[] = {
709 	{ .path = "/testcase-data/match-node/name0", .data = "A", },
710 	{ .path = "/testcase-data/match-node/name1", .data = "B", },
711 	{ .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
712 	{ .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
713 	{ .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
714 	{ .path = "/testcase-data/match-node/name3", .data = "E", },
715 	{ .path = "/testcase-data/match-node/name4", .data = "G", },
716 	{ .path = "/testcase-data/match-node/name5", .data = "H", },
717 	{ .path = "/testcase-data/match-node/name6", .data = "G", },
718 	{ .path = "/testcase-data/match-node/name7", .data = "I", },
719 	{ .path = "/testcase-data/match-node/name8", .data = "J", },
720 	{ .path = "/testcase-data/match-node/name9", .data = "K", },
721 };
722 
723 static void __init of_unittest_match_node(void)
724 {
725 	struct device_node *np;
726 	const struct of_device_id *match;
727 	int i;
728 
729 	for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
730 		np = of_find_node_by_path(match_node_tests[i].path);
731 		if (!np) {
732 			unittest(0, "missing testcase node %s\n",
733 				match_node_tests[i].path);
734 			continue;
735 		}
736 
737 		match = of_match_node(match_node_table, np);
738 		if (!match) {
739 			unittest(0, "%s didn't match anything\n",
740 				match_node_tests[i].path);
741 			continue;
742 		}
743 
744 		if (strcmp(match->data, match_node_tests[i].data) != 0) {
745 			unittest(0, "%s got wrong match. expected %s, got %s\n",
746 				match_node_tests[i].path, match_node_tests[i].data,
747 				(const char *)match->data);
748 			continue;
749 		}
750 		unittest(1, "passed");
751 	}
752 }
753 
754 static const struct platform_device_info test_bus_info = {
755 	.name = "unittest-bus",
756 };
757 static void __init of_unittest_platform_populate(void)
758 {
759 	int irq, rc;
760 	struct device_node *np, *child, *grandchild;
761 	struct platform_device *pdev, *test_bus;
762 	const struct of_device_id match[] = {
763 		{ .compatible = "test-device", },
764 		{}
765 	};
766 
767 	np = of_find_node_by_path("/testcase-data");
768 	of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
769 
770 	/* Test that a missing irq domain returns -EPROBE_DEFER */
771 	np = of_find_node_by_path("/testcase-data/testcase-device1");
772 	pdev = of_find_device_by_node(np);
773 	unittest(pdev, "device 1 creation failed\n");
774 
775 	irq = platform_get_irq(pdev, 0);
776 	unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
777 
778 	/* Test that a parsing failure does not return -EPROBE_DEFER */
779 	np = of_find_node_by_path("/testcase-data/testcase-device2");
780 	pdev = of_find_device_by_node(np);
781 	unittest(pdev, "device 2 creation failed\n");
782 	irq = platform_get_irq(pdev, 0);
783 	unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
784 
785 	np = of_find_node_by_path("/testcase-data/platform-tests");
786 	unittest(np, "No testcase data in device tree\n");
787 	if (!np)
788 		return;
789 
790 	test_bus = platform_device_register_full(&test_bus_info);
791 	rc = PTR_ERR_OR_ZERO(test_bus);
792 	unittest(!rc, "testbus registration failed; rc=%i\n", rc);
793 	if (rc)
794 		return;
795 	test_bus->dev.of_node = np;
796 
797 	of_platform_populate(np, match, NULL, &test_bus->dev);
798 	for_each_child_of_node(np, child) {
799 		for_each_child_of_node(child, grandchild)
800 			unittest(of_find_device_by_node(grandchild),
801 				 "Could not create device for node '%s'\n",
802 				 grandchild->name);
803 	}
804 
805 	of_platform_depopulate(&test_bus->dev);
806 	for_each_child_of_node(np, child) {
807 		for_each_child_of_node(child, grandchild)
808 			unittest(!of_find_device_by_node(grandchild),
809 				 "device didn't get destroyed '%s'\n",
810 				 grandchild->name);
811 	}
812 
813 	platform_device_unregister(test_bus);
814 	of_node_put(np);
815 }
816 
817 /**
818  *	update_node_properties - adds the properties
819  *	of np into dup node (present in live tree) and
820  *	updates parent of children of np to dup.
821  *
822  *	@np:	node already present in live tree
823  *	@dup:	node present in live tree to be updated
824  */
825 static void update_node_properties(struct device_node *np,
826 					struct device_node *dup)
827 {
828 	struct property *prop;
829 	struct device_node *child;
830 
831 	for_each_property_of_node(np, prop)
832 		of_add_property(dup, prop);
833 
834 	for_each_child_of_node(np, child)
835 		child->parent = dup;
836 }
837 
838 /**
839  *	attach_node_and_children - attaches nodes
840  *	and its children to live tree
841  *
842  *	@np:	Node to attach to live tree
843  */
844 static int attach_node_and_children(struct device_node *np)
845 {
846 	struct device_node *next, *dup, *child;
847 	unsigned long flags;
848 
849 	dup = of_find_node_by_path(np->full_name);
850 	if (dup) {
851 		update_node_properties(np, dup);
852 		return 0;
853 	}
854 
855 	child = np->child;
856 	np->child = NULL;
857 
858 	mutex_lock(&of_mutex);
859 	raw_spin_lock_irqsave(&devtree_lock, flags);
860 	np->sibling = np->parent->child;
861 	np->parent->child = np;
862 	of_node_clear_flag(np, OF_DETACHED);
863 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
864 
865 	__of_attach_node_sysfs(np);
866 	mutex_unlock(&of_mutex);
867 
868 	while (child) {
869 		next = child->sibling;
870 		attach_node_and_children(child);
871 		child = next;
872 	}
873 
874 	return 0;
875 }
876 
877 /**
878  *	unittest_data_add - Reads, copies data from
879  *	linked tree and attaches it to the live tree
880  */
881 static int __init unittest_data_add(void)
882 {
883 	void *unittest_data;
884 	struct device_node *unittest_data_node, *np;
885 	/*
886 	 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
887 	 * created by cmd_dt_S_dtb in scripts/Makefile.lib
888 	 */
889 	extern uint8_t __dtb_testcases_begin[];
890 	extern uint8_t __dtb_testcases_end[];
891 	const int size = __dtb_testcases_end - __dtb_testcases_begin;
892 	int rc;
893 
894 	if (!size) {
895 		pr_warn("%s: No testcase data to attach; not running tests\n",
896 			__func__);
897 		return -ENODATA;
898 	}
899 
900 	/* creating copy */
901 	unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
902 
903 	if (!unittest_data) {
904 		pr_warn("%s: Failed to allocate memory for unittest_data; "
905 			"not running tests\n", __func__);
906 		return -ENOMEM;
907 	}
908 	of_fdt_unflatten_tree(unittest_data, &unittest_data_node);
909 	if (!unittest_data_node) {
910 		pr_warn("%s: No tree to attach; not running tests\n", __func__);
911 		return -ENODATA;
912 	}
913 	of_node_set_flag(unittest_data_node, OF_DETACHED);
914 	rc = of_resolve_phandles(unittest_data_node);
915 	if (rc) {
916 		pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
917 		return -EINVAL;
918 	}
919 
920 	if (!of_root) {
921 		of_root = unittest_data_node;
922 		for_each_of_allnodes(np)
923 			__of_attach_node_sysfs(np);
924 		of_aliases = of_find_node_by_path("/aliases");
925 		of_chosen = of_find_node_by_path("/chosen");
926 		return 0;
927 	}
928 
929 	/* attach the sub-tree to live tree */
930 	np = unittest_data_node->child;
931 	while (np) {
932 		struct device_node *next = np->sibling;
933 
934 		np->parent = of_root;
935 		attach_node_and_children(np);
936 		np = next;
937 	}
938 	return 0;
939 }
940 
941 #ifdef CONFIG_OF_OVERLAY
942 
943 static int unittest_probe(struct platform_device *pdev)
944 {
945 	struct device *dev = &pdev->dev;
946 	struct device_node *np = dev->of_node;
947 
948 	if (np == NULL) {
949 		dev_err(dev, "No OF data for device\n");
950 		return -EINVAL;
951 
952 	}
953 
954 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
955 
956 	of_platform_populate(np, NULL, NULL, &pdev->dev);
957 
958 	return 0;
959 }
960 
961 static int unittest_remove(struct platform_device *pdev)
962 {
963 	struct device *dev = &pdev->dev;
964 	struct device_node *np = dev->of_node;
965 
966 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
967 	return 0;
968 }
969 
970 static const struct of_device_id unittest_match[] = {
971 	{ .compatible = "unittest", },
972 	{},
973 };
974 
975 static struct platform_driver unittest_driver = {
976 	.probe			= unittest_probe,
977 	.remove			= unittest_remove,
978 	.driver = {
979 		.name		= "unittest",
980 		.owner		= THIS_MODULE,
981 		.of_match_table	= of_match_ptr(unittest_match),
982 	},
983 };
984 
985 /* get the platform device instantiated at the path */
986 static struct platform_device *of_path_to_platform_device(const char *path)
987 {
988 	struct device_node *np;
989 	struct platform_device *pdev;
990 
991 	np = of_find_node_by_path(path);
992 	if (np == NULL)
993 		return NULL;
994 
995 	pdev = of_find_device_by_node(np);
996 	of_node_put(np);
997 
998 	return pdev;
999 }
1000 
1001 /* find out if a platform device exists at that path */
1002 static int of_path_platform_device_exists(const char *path)
1003 {
1004 	struct platform_device *pdev;
1005 
1006 	pdev = of_path_to_platform_device(path);
1007 	platform_device_put(pdev);
1008 	return pdev != NULL;
1009 }
1010 
1011 #if IS_BUILTIN(CONFIG_I2C)
1012 
1013 /* get the i2c client device instantiated at the path */
1014 static struct i2c_client *of_path_to_i2c_client(const char *path)
1015 {
1016 	struct device_node *np;
1017 	struct i2c_client *client;
1018 
1019 	np = of_find_node_by_path(path);
1020 	if (np == NULL)
1021 		return NULL;
1022 
1023 	client = of_find_i2c_device_by_node(np);
1024 	of_node_put(np);
1025 
1026 	return client;
1027 }
1028 
1029 /* find out if a i2c client device exists at that path */
1030 static int of_path_i2c_client_exists(const char *path)
1031 {
1032 	struct i2c_client *client;
1033 
1034 	client = of_path_to_i2c_client(path);
1035 	if (client)
1036 		put_device(&client->dev);
1037 	return client != NULL;
1038 }
1039 #else
1040 static int of_path_i2c_client_exists(const char *path)
1041 {
1042 	return 0;
1043 }
1044 #endif
1045 
1046 enum overlay_type {
1047 	PDEV_OVERLAY,
1048 	I2C_OVERLAY
1049 };
1050 
1051 static int of_path_device_type_exists(const char *path,
1052 		enum overlay_type ovtype)
1053 {
1054 	switch (ovtype) {
1055 	case PDEV_OVERLAY:
1056 		return of_path_platform_device_exists(path);
1057 	case I2C_OVERLAY:
1058 		return of_path_i2c_client_exists(path);
1059 	}
1060 	return 0;
1061 }
1062 
1063 static const char *unittest_path(int nr, enum overlay_type ovtype)
1064 {
1065 	const char *base;
1066 	static char buf[256];
1067 
1068 	switch (ovtype) {
1069 	case PDEV_OVERLAY:
1070 		base = "/testcase-data/overlay-node/test-bus";
1071 		break;
1072 	case I2C_OVERLAY:
1073 		base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1074 		break;
1075 	default:
1076 		buf[0] = '\0';
1077 		return buf;
1078 	}
1079 	snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
1080 	buf[sizeof(buf) - 1] = '\0';
1081 	return buf;
1082 }
1083 
1084 static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
1085 {
1086 	const char *path;
1087 
1088 	path = unittest_path(unittest_nr, ovtype);
1089 
1090 	switch (ovtype) {
1091 	case PDEV_OVERLAY:
1092 		return of_path_platform_device_exists(path);
1093 	case I2C_OVERLAY:
1094 		return of_path_i2c_client_exists(path);
1095 	}
1096 	return 0;
1097 }
1098 
1099 static const char *overlay_path(int nr)
1100 {
1101 	static char buf[256];
1102 
1103 	snprintf(buf, sizeof(buf) - 1,
1104 		"/testcase-data/overlay%d", nr);
1105 	buf[sizeof(buf) - 1] = '\0';
1106 
1107 	return buf;
1108 }
1109 
1110 static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1111 
1112 static int of_unittest_apply_overlay(int unittest_nr, int overlay_nr,
1113 		int *overlay_id)
1114 {
1115 	struct device_node *np = NULL;
1116 	int ret, id = -1;
1117 
1118 	np = of_find_node_by_path(overlay_path(overlay_nr));
1119 	if (np == NULL) {
1120 		unittest(0, "could not find overlay node @\"%s\"\n",
1121 				overlay_path(overlay_nr));
1122 		ret = -EINVAL;
1123 		goto out;
1124 	}
1125 
1126 	ret = of_overlay_create(np);
1127 	if (ret < 0) {
1128 		unittest(0, "could not create overlay from \"%s\"\n",
1129 				overlay_path(overlay_nr));
1130 		goto out;
1131 	}
1132 	id = ret;
1133 
1134 	ret = 0;
1135 
1136 out:
1137 	of_node_put(np);
1138 
1139 	if (overlay_id)
1140 		*overlay_id = id;
1141 
1142 	return ret;
1143 }
1144 
1145 /* apply an overlay while checking before and after states */
1146 static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
1147 		int before, int after, enum overlay_type ovtype)
1148 {
1149 	int ret;
1150 
1151 	/* unittest device must not be in before state */
1152 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1153 		unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1154 				overlay_path(overlay_nr),
1155 				unittest_path(unittest_nr, ovtype),
1156 				!before ? "enabled" : "disabled");
1157 		return -EINVAL;
1158 	}
1159 
1160 	ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, NULL);
1161 	if (ret != 0) {
1162 		/* of_unittest_apply_overlay already called unittest() */
1163 		return ret;
1164 	}
1165 
1166 	/* unittest device must be to set to after state */
1167 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1168 		unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1169 				overlay_path(overlay_nr),
1170 				unittest_path(unittest_nr, ovtype),
1171 				!after ? "enabled" : "disabled");
1172 		return -EINVAL;
1173 	}
1174 
1175 	return 0;
1176 }
1177 
1178 /* apply an overlay and then revert it while checking before, after states */
1179 static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1180 		int unittest_nr, int before, int after,
1181 		enum overlay_type ovtype)
1182 {
1183 	int ret, ov_id;
1184 
1185 	/* unittest device must be in before state */
1186 	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1187 		unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1188 				overlay_path(overlay_nr),
1189 				unittest_path(unittest_nr, ovtype),
1190 				!before ? "enabled" : "disabled");
1191 		return -EINVAL;
1192 	}
1193 
1194 	/* apply the overlay */
1195 	ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ov_id);
1196 	if (ret != 0) {
1197 		/* of_unittest_apply_overlay already called unittest() */
1198 		return ret;
1199 	}
1200 
1201 	/* unittest device must be in after state */
1202 	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1203 		unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1204 				overlay_path(overlay_nr),
1205 				unittest_path(unittest_nr, ovtype),
1206 				!after ? "enabled" : "disabled");
1207 		return -EINVAL;
1208 	}
1209 
1210 	ret = of_overlay_destroy(ov_id);
1211 	if (ret != 0) {
1212 		unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1213 				overlay_path(overlay_nr),
1214 				unittest_path(unittest_nr, ovtype));
1215 		return ret;
1216 	}
1217 
1218 	/* unittest device must be again in before state */
1219 	if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1220 		unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1221 				overlay_path(overlay_nr),
1222 				unittest_path(unittest_nr, ovtype),
1223 				!before ? "enabled" : "disabled");
1224 		return -EINVAL;
1225 	}
1226 
1227 	return 0;
1228 }
1229 
1230 /* test activation of device */
1231 static void of_unittest_overlay_0(void)
1232 {
1233 	int ret;
1234 
1235 	/* device should enable */
1236 	ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
1237 	if (ret != 0)
1238 		return;
1239 
1240 	unittest(1, "overlay test %d passed\n", 0);
1241 }
1242 
1243 /* test deactivation of device */
1244 static void of_unittest_overlay_1(void)
1245 {
1246 	int ret;
1247 
1248 	/* device should disable */
1249 	ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
1250 	if (ret != 0)
1251 		return;
1252 
1253 	unittest(1, "overlay test %d passed\n", 1);
1254 }
1255 
1256 /* test activation of device */
1257 static void of_unittest_overlay_2(void)
1258 {
1259 	int ret;
1260 
1261 	/* device should enable */
1262 	ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
1263 	if (ret != 0)
1264 		return;
1265 
1266 	unittest(1, "overlay test %d passed\n", 2);
1267 }
1268 
1269 /* test deactivation of device */
1270 static void of_unittest_overlay_3(void)
1271 {
1272 	int ret;
1273 
1274 	/* device should disable */
1275 	ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
1276 	if (ret != 0)
1277 		return;
1278 
1279 	unittest(1, "overlay test %d passed\n", 3);
1280 }
1281 
1282 /* test activation of a full device node */
1283 static void of_unittest_overlay_4(void)
1284 {
1285 	int ret;
1286 
1287 	/* device should disable */
1288 	ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
1289 	if (ret != 0)
1290 		return;
1291 
1292 	unittest(1, "overlay test %d passed\n", 4);
1293 }
1294 
1295 /* test overlay apply/revert sequence */
1296 static void of_unittest_overlay_5(void)
1297 {
1298 	int ret;
1299 
1300 	/* device should disable */
1301 	ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
1302 	if (ret != 0)
1303 		return;
1304 
1305 	unittest(1, "overlay test %d passed\n", 5);
1306 }
1307 
1308 /* test overlay application in sequence */
1309 static void of_unittest_overlay_6(void)
1310 {
1311 	struct device_node *np;
1312 	int ret, i, ov_id[2];
1313 	int overlay_nr = 6, unittest_nr = 6;
1314 	int before = 0, after = 1;
1315 
1316 	/* unittest device must be in before state */
1317 	for (i = 0; i < 2; i++) {
1318 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1319 				!= before) {
1320 			unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1321 					overlay_path(overlay_nr + i),
1322 					unittest_path(unittest_nr + i,
1323 						PDEV_OVERLAY),
1324 					!before ? "enabled" : "disabled");
1325 			return;
1326 		}
1327 	}
1328 
1329 	/* apply the overlays */
1330 	for (i = 0; i < 2; i++) {
1331 
1332 		np = of_find_node_by_path(overlay_path(overlay_nr + i));
1333 		if (np == NULL) {
1334 			unittest(0, "could not find overlay node @\"%s\"\n",
1335 					overlay_path(overlay_nr + i));
1336 			return;
1337 		}
1338 
1339 		ret = of_overlay_create(np);
1340 		if (ret < 0)  {
1341 			unittest(0, "could not create overlay from \"%s\"\n",
1342 					overlay_path(overlay_nr + i));
1343 			return;
1344 		}
1345 		ov_id[i] = ret;
1346 	}
1347 
1348 	for (i = 0; i < 2; i++) {
1349 		/* unittest device must be in after state */
1350 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1351 				!= after) {
1352 			unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1353 					overlay_path(overlay_nr + i),
1354 					unittest_path(unittest_nr + i,
1355 						PDEV_OVERLAY),
1356 					!after ? "enabled" : "disabled");
1357 			return;
1358 		}
1359 	}
1360 
1361 	for (i = 1; i >= 0; i--) {
1362 		ret = of_overlay_destroy(ov_id[i]);
1363 		if (ret != 0) {
1364 			unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1365 					overlay_path(overlay_nr + i),
1366 					unittest_path(unittest_nr + i,
1367 						PDEV_OVERLAY));
1368 			return;
1369 		}
1370 	}
1371 
1372 	for (i = 0; i < 2; i++) {
1373 		/* unittest device must be again in before state */
1374 		if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
1375 				!= before) {
1376 			unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1377 					overlay_path(overlay_nr + i),
1378 					unittest_path(unittest_nr + i,
1379 						PDEV_OVERLAY),
1380 					!before ? "enabled" : "disabled");
1381 			return;
1382 		}
1383 	}
1384 
1385 	unittest(1, "overlay test %d passed\n", 6);
1386 }
1387 
1388 /* test overlay application in sequence */
1389 static void of_unittest_overlay_8(void)
1390 {
1391 	struct device_node *np;
1392 	int ret, i, ov_id[2];
1393 	int overlay_nr = 8, unittest_nr = 8;
1394 
1395 	/* we don't care about device state in this test */
1396 
1397 	/* apply the overlays */
1398 	for (i = 0; i < 2; i++) {
1399 
1400 		np = of_find_node_by_path(overlay_path(overlay_nr + i));
1401 		if (np == NULL) {
1402 			unittest(0, "could not find overlay node @\"%s\"\n",
1403 					overlay_path(overlay_nr + i));
1404 			return;
1405 		}
1406 
1407 		ret = of_overlay_create(np);
1408 		if (ret < 0)  {
1409 			unittest(0, "could not create overlay from \"%s\"\n",
1410 					overlay_path(overlay_nr + i));
1411 			return;
1412 		}
1413 		ov_id[i] = ret;
1414 	}
1415 
1416 	/* now try to remove first overlay (it should fail) */
1417 	ret = of_overlay_destroy(ov_id[0]);
1418 	if (ret == 0) {
1419 		unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1420 				overlay_path(overlay_nr + 0),
1421 				unittest_path(unittest_nr,
1422 					PDEV_OVERLAY));
1423 		return;
1424 	}
1425 
1426 	/* removing them in order should work */
1427 	for (i = 1; i >= 0; i--) {
1428 		ret = of_overlay_destroy(ov_id[i]);
1429 		if (ret != 0) {
1430 			unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1431 					overlay_path(overlay_nr + i),
1432 					unittest_path(unittest_nr,
1433 						PDEV_OVERLAY));
1434 			return;
1435 		}
1436 	}
1437 
1438 	unittest(1, "overlay test %d passed\n", 8);
1439 }
1440 
1441 /* test insertion of a bus with parent devices */
1442 static void of_unittest_overlay_10(void)
1443 {
1444 	int ret;
1445 	char *child_path;
1446 
1447 	/* device should disable */
1448 	ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1449 	if (unittest(ret == 0,
1450 			"overlay test %d failed; overlay application\n", 10))
1451 		return;
1452 
1453 	child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1454 			unittest_path(10, PDEV_OVERLAY));
1455 	if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
1456 		return;
1457 
1458 	ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
1459 	kfree(child_path);
1460 	if (unittest(ret, "overlay test %d failed; no child device\n", 10))
1461 		return;
1462 }
1463 
1464 /* test insertion of a bus with parent devices (and revert) */
1465 static void of_unittest_overlay_11(void)
1466 {
1467 	int ret;
1468 
1469 	/* device should disable */
1470 	ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
1471 			PDEV_OVERLAY);
1472 	if (unittest(ret == 0,
1473 			"overlay test %d failed; overlay application\n", 11))
1474 		return;
1475 }
1476 
1477 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
1478 
1479 struct unittest_i2c_bus_data {
1480 	struct platform_device	*pdev;
1481 	struct i2c_adapter	adap;
1482 };
1483 
1484 static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
1485 		struct i2c_msg *msgs, int num)
1486 {
1487 	struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
1488 
1489 	(void)std;
1490 
1491 	return num;
1492 }
1493 
1494 static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
1495 {
1496 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1497 }
1498 
1499 static const struct i2c_algorithm unittest_i2c_algo = {
1500 	.master_xfer	= unittest_i2c_master_xfer,
1501 	.functionality	= unittest_i2c_functionality,
1502 };
1503 
1504 static int unittest_i2c_bus_probe(struct platform_device *pdev)
1505 {
1506 	struct device *dev = &pdev->dev;
1507 	struct device_node *np = dev->of_node;
1508 	struct unittest_i2c_bus_data *std;
1509 	struct i2c_adapter *adap;
1510 	int ret;
1511 
1512 	if (np == NULL) {
1513 		dev_err(dev, "No OF data for device\n");
1514 		return -EINVAL;
1515 
1516 	}
1517 
1518 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1519 
1520 	std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1521 	if (!std) {
1522 		dev_err(dev, "Failed to allocate unittest i2c data\n");
1523 		return -ENOMEM;
1524 	}
1525 
1526 	/* link them together */
1527 	std->pdev = pdev;
1528 	platform_set_drvdata(pdev, std);
1529 
1530 	adap = &std->adap;
1531 	i2c_set_adapdata(adap, std);
1532 	adap->nr = -1;
1533 	strlcpy(adap->name, pdev->name, sizeof(adap->name));
1534 	adap->class = I2C_CLASS_DEPRECATED;
1535 	adap->algo = &unittest_i2c_algo;
1536 	adap->dev.parent = dev;
1537 	adap->dev.of_node = dev->of_node;
1538 	adap->timeout = 5 * HZ;
1539 	adap->retries = 3;
1540 
1541 	ret = i2c_add_numbered_adapter(adap);
1542 	if (ret != 0) {
1543 		dev_err(dev, "Failed to add I2C adapter\n");
1544 		return ret;
1545 	}
1546 
1547 	return 0;
1548 }
1549 
1550 static int unittest_i2c_bus_remove(struct platform_device *pdev)
1551 {
1552 	struct device *dev = &pdev->dev;
1553 	struct device_node *np = dev->of_node;
1554 	struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
1555 
1556 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1557 	i2c_del_adapter(&std->adap);
1558 
1559 	return 0;
1560 }
1561 
1562 static const struct of_device_id unittest_i2c_bus_match[] = {
1563 	{ .compatible = "unittest-i2c-bus", },
1564 	{},
1565 };
1566 
1567 static struct platform_driver unittest_i2c_bus_driver = {
1568 	.probe			= unittest_i2c_bus_probe,
1569 	.remove			= unittest_i2c_bus_remove,
1570 	.driver = {
1571 		.name		= "unittest-i2c-bus",
1572 		.of_match_table	= of_match_ptr(unittest_i2c_bus_match),
1573 	},
1574 };
1575 
1576 static int unittest_i2c_dev_probe(struct i2c_client *client,
1577 		const struct i2c_device_id *id)
1578 {
1579 	struct device *dev = &client->dev;
1580 	struct device_node *np = client->dev.of_node;
1581 
1582 	if (!np) {
1583 		dev_err(dev, "No OF node\n");
1584 		return -EINVAL;
1585 	}
1586 
1587 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1588 
1589 	return 0;
1590 };
1591 
1592 static int unittest_i2c_dev_remove(struct i2c_client *client)
1593 {
1594 	struct device *dev = &client->dev;
1595 	struct device_node *np = client->dev.of_node;
1596 
1597 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1598 	return 0;
1599 }
1600 
1601 static const struct i2c_device_id unittest_i2c_dev_id[] = {
1602 	{ .name = "unittest-i2c-dev" },
1603 	{ }
1604 };
1605 
1606 static struct i2c_driver unittest_i2c_dev_driver = {
1607 	.driver = {
1608 		.name = "unittest-i2c-dev",
1609 		.owner = THIS_MODULE,
1610 	},
1611 	.probe = unittest_i2c_dev_probe,
1612 	.remove = unittest_i2c_dev_remove,
1613 	.id_table = unittest_i2c_dev_id,
1614 };
1615 
1616 #if IS_BUILTIN(CONFIG_I2C_MUX)
1617 
1618 struct unittest_i2c_mux_data {
1619 	int nchans;
1620 	struct i2c_adapter *adap[];
1621 };
1622 
1623 static int unittest_i2c_mux_select_chan(struct i2c_adapter *adap,
1624 			       void *client, u32 chan)
1625 {
1626 	return 0;
1627 }
1628 
1629 static int unittest_i2c_mux_probe(struct i2c_client *client,
1630 		const struct i2c_device_id *id)
1631 {
1632 	int ret, i, nchans, size;
1633 	struct device *dev = &client->dev;
1634 	struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1635 	struct device_node *np = client->dev.of_node, *child;
1636 	struct unittest_i2c_mux_data *stm;
1637 	u32 reg, max_reg;
1638 
1639 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1640 
1641 	if (!np) {
1642 		dev_err(dev, "No OF node\n");
1643 		return -EINVAL;
1644 	}
1645 
1646 	max_reg = (u32)-1;
1647 	for_each_child_of_node(np, child) {
1648 		ret = of_property_read_u32(child, "reg", &reg);
1649 		if (ret)
1650 			continue;
1651 		if (max_reg == (u32)-1 || reg > max_reg)
1652 			max_reg = reg;
1653 	}
1654 	nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1655 	if (nchans == 0) {
1656 		dev_err(dev, "No channels\n");
1657 		return -EINVAL;
1658 	}
1659 
1660 	size = offsetof(struct unittest_i2c_mux_data, adap[nchans]);
1661 	stm = devm_kzalloc(dev, size, GFP_KERNEL);
1662 	if (!stm) {
1663 		dev_err(dev, "Out of memory\n");
1664 		return -ENOMEM;
1665 	}
1666 	stm->nchans = nchans;
1667 	for (i = 0; i < nchans; i++) {
1668 		stm->adap[i] = i2c_add_mux_adapter(adap, dev, client,
1669 				0, i, 0, unittest_i2c_mux_select_chan, NULL);
1670 		if (!stm->adap[i]) {
1671 			dev_err(dev, "Failed to register mux #%d\n", i);
1672 			for (i--; i >= 0; i--)
1673 				i2c_del_mux_adapter(stm->adap[i]);
1674 			return -ENODEV;
1675 		}
1676 	}
1677 
1678 	i2c_set_clientdata(client, stm);
1679 
1680 	return 0;
1681 };
1682 
1683 static int unittest_i2c_mux_remove(struct i2c_client *client)
1684 {
1685 	struct device *dev = &client->dev;
1686 	struct device_node *np = client->dev.of_node;
1687 	struct unittest_i2c_mux_data *stm = i2c_get_clientdata(client);
1688 	int i;
1689 
1690 	dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
1691 	for (i = stm->nchans - 1; i >= 0; i--)
1692 		i2c_del_mux_adapter(stm->adap[i]);
1693 	return 0;
1694 }
1695 
1696 static const struct i2c_device_id unittest_i2c_mux_id[] = {
1697 	{ .name = "unittest-i2c-mux" },
1698 	{ }
1699 };
1700 
1701 static struct i2c_driver unittest_i2c_mux_driver = {
1702 	.driver = {
1703 		.name = "unittest-i2c-mux",
1704 		.owner = THIS_MODULE,
1705 	},
1706 	.probe = unittest_i2c_mux_probe,
1707 	.remove = unittest_i2c_mux_remove,
1708 	.id_table = unittest_i2c_mux_id,
1709 };
1710 
1711 #endif
1712 
1713 static int of_unittest_overlay_i2c_init(void)
1714 {
1715 	int ret;
1716 
1717 	ret = i2c_add_driver(&unittest_i2c_dev_driver);
1718 	if (unittest(ret == 0,
1719 			"could not register unittest i2c device driver\n"))
1720 		return ret;
1721 
1722 	ret = platform_driver_register(&unittest_i2c_bus_driver);
1723 	if (unittest(ret == 0,
1724 			"could not register unittest i2c bus driver\n"))
1725 		return ret;
1726 
1727 #if IS_BUILTIN(CONFIG_I2C_MUX)
1728 	ret = i2c_add_driver(&unittest_i2c_mux_driver);
1729 	if (unittest(ret == 0,
1730 			"could not register unittest i2c mux driver\n"))
1731 		return ret;
1732 #endif
1733 
1734 	return 0;
1735 }
1736 
1737 static void of_unittest_overlay_i2c_cleanup(void)
1738 {
1739 #if IS_BUILTIN(CONFIG_I2C_MUX)
1740 	i2c_del_driver(&unittest_i2c_mux_driver);
1741 #endif
1742 	platform_driver_unregister(&unittest_i2c_bus_driver);
1743 	i2c_del_driver(&unittest_i2c_dev_driver);
1744 }
1745 
1746 static void of_unittest_overlay_i2c_12(void)
1747 {
1748 	int ret;
1749 
1750 	/* device should enable */
1751 	ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
1752 	if (ret != 0)
1753 		return;
1754 
1755 	unittest(1, "overlay test %d passed\n", 12);
1756 }
1757 
1758 /* test deactivation of device */
1759 static void of_unittest_overlay_i2c_13(void)
1760 {
1761 	int ret;
1762 
1763 	/* device should disable */
1764 	ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
1765 	if (ret != 0)
1766 		return;
1767 
1768 	unittest(1, "overlay test %d passed\n", 13);
1769 }
1770 
1771 /* just check for i2c mux existence */
1772 static void of_unittest_overlay_i2c_14(void)
1773 {
1774 }
1775 
1776 static void of_unittest_overlay_i2c_15(void)
1777 {
1778 	int ret;
1779 
1780 	/* device should enable */
1781 	ret = of_unittest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY);
1782 	if (ret != 0)
1783 		return;
1784 
1785 	unittest(1, "overlay test %d passed\n", 15);
1786 }
1787 
1788 #else
1789 
1790 static inline void of_unittest_overlay_i2c_14(void) { }
1791 static inline void of_unittest_overlay_i2c_15(void) { }
1792 
1793 #endif
1794 
1795 static void __init of_unittest_overlay(void)
1796 {
1797 	struct device_node *bus_np = NULL;
1798 	int ret;
1799 
1800 	ret = platform_driver_register(&unittest_driver);
1801 	if (ret != 0) {
1802 		unittest(0, "could not register unittest driver\n");
1803 		goto out;
1804 	}
1805 
1806 	bus_np = of_find_node_by_path(bus_path);
1807 	if (bus_np == NULL) {
1808 		unittest(0, "could not find bus_path \"%s\"\n", bus_path);
1809 		goto out;
1810 	}
1811 
1812 	ret = of_platform_populate(bus_np, of_default_bus_match_table,
1813 			NULL, NULL);
1814 	if (ret != 0) {
1815 		unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
1816 		goto out;
1817 	}
1818 
1819 	if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1820 		unittest(0, "could not find unittest0 @ \"%s\"\n",
1821 				unittest_path(100, PDEV_OVERLAY));
1822 		goto out;
1823 	}
1824 
1825 	if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1826 		unittest(0, "unittest1 @ \"%s\" should not exist\n",
1827 				unittest_path(101, PDEV_OVERLAY));
1828 		goto out;
1829 	}
1830 
1831 	unittest(1, "basic infrastructure of overlays passed");
1832 
1833 	/* tests in sequence */
1834 	of_unittest_overlay_0();
1835 	of_unittest_overlay_1();
1836 	of_unittest_overlay_2();
1837 	of_unittest_overlay_3();
1838 	of_unittest_overlay_4();
1839 	of_unittest_overlay_5();
1840 	of_unittest_overlay_6();
1841 	of_unittest_overlay_8();
1842 
1843 	of_unittest_overlay_10();
1844 	of_unittest_overlay_11();
1845 
1846 #if IS_BUILTIN(CONFIG_I2C)
1847 	if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
1848 		goto out;
1849 
1850 	of_unittest_overlay_i2c_12();
1851 	of_unittest_overlay_i2c_13();
1852 	of_unittest_overlay_i2c_14();
1853 	of_unittest_overlay_i2c_15();
1854 
1855 	of_unittest_overlay_i2c_cleanup();
1856 #endif
1857 
1858 out:
1859 	of_node_put(bus_np);
1860 }
1861 
1862 #else
1863 static inline void __init of_unittest_overlay(void) { }
1864 #endif
1865 
1866 static int __init of_unittest(void)
1867 {
1868 	struct device_node *np;
1869 	int res;
1870 
1871 	/* adding data for unittest */
1872 	res = unittest_data_add();
1873 	if (res)
1874 		return res;
1875 	if (!of_aliases)
1876 		of_aliases = of_find_node_by_path("/aliases");
1877 
1878 	np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1879 	if (!np) {
1880 		pr_info("No testcase data in device tree; not running tests\n");
1881 		return 0;
1882 	}
1883 	of_node_put(np);
1884 
1885 	pr_info("start of unittest - you will see error messages\n");
1886 	of_unittest_check_tree_linkage();
1887 	of_unittest_check_phandles();
1888 	of_unittest_find_node_by_name();
1889 	of_unittest_dynamic();
1890 	of_unittest_parse_phandle_with_args();
1891 	of_unittest_property_string();
1892 	of_unittest_property_copy();
1893 	of_unittest_changeset();
1894 	of_unittest_parse_interrupts();
1895 	of_unittest_parse_interrupts_extended();
1896 	of_unittest_match_node();
1897 	of_unittest_platform_populate();
1898 	of_unittest_overlay();
1899 
1900 	/* Double check linkage after removing testcase data */
1901 	of_unittest_check_tree_linkage();
1902 
1903 	pr_info("end of unittest - %i passed, %i failed\n",
1904 		unittest_results.passed, unittest_results.failed);
1905 
1906 	return 0;
1907 }
1908 late_initcall(of_unittest);
1909