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