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