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