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