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