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