1 /* 2 * Self tests for device tree subsystem 3 */ 4 5 #define pr_fmt(fmt) "### dt-test ### " fmt 6 7 #include <linux/clk.h> 8 #include <linux/err.h> 9 #include <linux/errno.h> 10 #include <linux/hashtable.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/of_fdt.h> 14 #include <linux/of_irq.h> 15 #include <linux/of_platform.h> 16 #include <linux/list.h> 17 #include <linux/mutex.h> 18 #include <linux/slab.h> 19 #include <linux/device.h> 20 #include <linux/platform_device.h> 21 #include <linux/of_platform.h> 22 23 #include <linux/i2c.h> 24 #include <linux/i2c-mux.h> 25 26 #include "of_private.h" 27 28 static struct selftest_results { 29 int passed; 30 int failed; 31 } selftest_results; 32 33 #define selftest(result, fmt, ...) ({ \ 34 bool failed = !(result); \ 35 if (failed) { \ 36 selftest_results.failed++; \ 37 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ 38 } else { \ 39 selftest_results.passed++; \ 40 pr_debug("pass %s():%i\n", __func__, __LINE__); \ 41 } \ 42 failed; \ 43 }) 44 45 static void __init of_selftest_find_node_by_name(void) 46 { 47 struct device_node *np; 48 const char *options; 49 50 np = of_find_node_by_path("/testcase-data"); 51 selftest(np && !strcmp("/testcase-data", np->full_name), 52 "find /testcase-data failed\n"); 53 of_node_put(np); 54 55 /* Test if trailing '/' works */ 56 np = of_find_node_by_path("/testcase-data/"); 57 selftest(!np, "trailing '/' on /testcase-data/ should fail\n"); 58 59 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); 60 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), 61 "find /testcase-data/phandle-tests/consumer-a failed\n"); 62 of_node_put(np); 63 64 np = of_find_node_by_path("testcase-alias"); 65 selftest(np && !strcmp("/testcase-data", np->full_name), 66 "find testcase-alias failed\n"); 67 of_node_put(np); 68 69 /* Test if trailing '/' works on aliases */ 70 np = of_find_node_by_path("testcase-alias/"); 71 selftest(!np, "trailing '/' on testcase-alias/ should fail\n"); 72 73 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); 74 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), 75 "find testcase-alias/phandle-tests/consumer-a failed\n"); 76 of_node_put(np); 77 78 np = of_find_node_by_path("/testcase-data/missing-path"); 79 selftest(!np, "non-existent path returned node %s\n", np->full_name); 80 of_node_put(np); 81 82 np = of_find_node_by_path("missing-alias"); 83 selftest(!np, "non-existent alias returned node %s\n", np->full_name); 84 of_node_put(np); 85 86 np = of_find_node_by_path("testcase-alias/missing-path"); 87 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name); 88 of_node_put(np); 89 90 np = of_find_node_opts_by_path("/testcase-data:testoption", &options); 91 selftest(np && !strcmp("testoption", options), 92 "option path test failed\n"); 93 of_node_put(np); 94 95 np = of_find_node_opts_by_path("/testcase-data:test/option", &options); 96 selftest(np && !strcmp("test/option", options), 97 "option path test, subcase #1 failed\n"); 98 of_node_put(np); 99 100 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options); 101 selftest(np && !strcmp("test/option", options), 102 "option path test, subcase #2 failed\n"); 103 of_node_put(np); 104 105 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL); 106 selftest(np, "NULL option path test failed\n"); 107 of_node_put(np); 108 109 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", 110 &options); 111 selftest(np && !strcmp("testaliasoption", options), 112 "option alias path test failed\n"); 113 of_node_put(np); 114 115 np = of_find_node_opts_by_path("testcase-alias:test/alias/option", 116 &options); 117 selftest(np && !strcmp("test/alias/option", options), 118 "option alias path test, subcase #1 failed\n"); 119 of_node_put(np); 120 121 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL); 122 selftest(np, "NULL option alias path test failed\n"); 123 of_node_put(np); 124 125 options = "testoption"; 126 np = of_find_node_opts_by_path("testcase-alias", &options); 127 selftest(np && !options, "option clearing test failed\n"); 128 of_node_put(np); 129 130 options = "testoption"; 131 np = of_find_node_opts_by_path("/", &options); 132 selftest(np && !options, "option clearing root node test failed\n"); 133 of_node_put(np); 134 } 135 136 static void __init of_selftest_dynamic(void) 137 { 138 struct device_node *np; 139 struct property *prop; 140 141 np = of_find_node_by_path("/testcase-data"); 142 if (!np) { 143 pr_err("missing testcase data\n"); 144 return; 145 } 146 147 /* Array of 4 properties for the purpose of testing */ 148 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL); 149 if (!prop) { 150 selftest(0, "kzalloc() failed\n"); 151 return; 152 } 153 154 /* Add a new property - should pass*/ 155 prop->name = "new-property"; 156 prop->value = "new-property-data"; 157 prop->length = strlen(prop->value); 158 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n"); 159 160 /* Try to add an existing property - should fail */ 161 prop++; 162 prop->name = "new-property"; 163 prop->value = "new-property-data-should-fail"; 164 prop->length = strlen(prop->value); 165 selftest(of_add_property(np, prop) != 0, 166 "Adding an existing property should have failed\n"); 167 168 /* Try to modify an existing property - should pass */ 169 prop->value = "modify-property-data-should-pass"; 170 prop->length = strlen(prop->value); 171 selftest(of_update_property(np, prop) == 0, 172 "Updating an existing property should have passed\n"); 173 174 /* Try to modify non-existent property - should pass*/ 175 prop++; 176 prop->name = "modify-property"; 177 prop->value = "modify-missing-property-data-should-pass"; 178 prop->length = strlen(prop->value); 179 selftest(of_update_property(np, prop) == 0, 180 "Updating a missing property should have passed\n"); 181 182 /* Remove property - should pass */ 183 selftest(of_remove_property(np, prop) == 0, 184 "Removing a property should have passed\n"); 185 186 /* Adding very large property - should pass */ 187 prop++; 188 prop->name = "large-property-PAGE_SIZEx8"; 189 prop->length = PAGE_SIZE * 8; 190 prop->value = kzalloc(prop->length, GFP_KERNEL); 191 selftest(prop->value != NULL, "Unable to allocate large buffer\n"); 192 if (prop->value) 193 selftest(of_add_property(np, prop) == 0, 194 "Adding a large property should have passed\n"); 195 } 196 197 static int __init of_selftest_check_node_linkage(struct device_node *np) 198 { 199 struct device_node *child; 200 int count = 0, rc; 201 202 for_each_child_of_node(np, child) { 203 if (child->parent != np) { 204 pr_err("Child node %s links to wrong parent %s\n", 205 child->name, np->name); 206 return -EINVAL; 207 } 208 209 rc = of_selftest_check_node_linkage(child); 210 if (rc < 0) 211 return rc; 212 count += rc; 213 } 214 215 return count + 1; 216 } 217 218 static void __init of_selftest_check_tree_linkage(void) 219 { 220 struct device_node *np; 221 int allnode_count = 0, child_count; 222 223 if (!of_root) 224 return; 225 226 for_each_of_allnodes(np) 227 allnode_count++; 228 child_count = of_selftest_check_node_linkage(of_root); 229 230 selftest(child_count > 0, "Device node data structure is corrupted\n"); 231 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match" 232 "sibling lists size (%i)\n", allnode_count, child_count); 233 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count); 234 } 235 236 struct node_hash { 237 struct hlist_node node; 238 struct device_node *np; 239 }; 240 241 static DEFINE_HASHTABLE(phandle_ht, 8); 242 static void __init of_selftest_check_phandles(void) 243 { 244 struct device_node *np; 245 struct node_hash *nh; 246 struct hlist_node *tmp; 247 int i, dup_count = 0, phandle_count = 0; 248 249 for_each_of_allnodes(np) { 250 if (!np->phandle) 251 continue; 252 253 hash_for_each_possible(phandle_ht, nh, node, np->phandle) { 254 if (nh->np->phandle == np->phandle) { 255 pr_info("Duplicate phandle! %i used by %s and %s\n", 256 np->phandle, nh->np->full_name, np->full_name); 257 dup_count++; 258 break; 259 } 260 } 261 262 nh = kzalloc(sizeof(*nh), GFP_KERNEL); 263 if (WARN_ON(!nh)) 264 return; 265 266 nh->np = np; 267 hash_add(phandle_ht, &nh->node, np->phandle); 268 phandle_count++; 269 } 270 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n", 271 dup_count, phandle_count); 272 273 /* Clean up */ 274 hash_for_each_safe(phandle_ht, i, tmp, nh, node) { 275 hash_del(&nh->node); 276 kfree(nh); 277 } 278 } 279 280 static void __init of_selftest_parse_phandle_with_args(void) 281 { 282 struct device_node *np; 283 struct of_phandle_args args; 284 int i, rc; 285 286 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); 287 if (!np) { 288 pr_err("missing testcase data\n"); 289 return; 290 } 291 292 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); 293 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); 294 295 for (i = 0; i < 8; i++) { 296 bool passed = true; 297 rc = of_parse_phandle_with_args(np, "phandle-list", 298 "#phandle-cells", i, &args); 299 300 /* Test the values from tests-phandle.dtsi */ 301 switch (i) { 302 case 0: 303 passed &= !rc; 304 passed &= (args.args_count == 1); 305 passed &= (args.args[0] == (i + 1)); 306 break; 307 case 1: 308 passed &= !rc; 309 passed &= (args.args_count == 2); 310 passed &= (args.args[0] == (i + 1)); 311 passed &= (args.args[1] == 0); 312 break; 313 case 2: 314 passed &= (rc == -ENOENT); 315 break; 316 case 3: 317 passed &= !rc; 318 passed &= (args.args_count == 3); 319 passed &= (args.args[0] == (i + 1)); 320 passed &= (args.args[1] == 4); 321 passed &= (args.args[2] == 3); 322 break; 323 case 4: 324 passed &= !rc; 325 passed &= (args.args_count == 2); 326 passed &= (args.args[0] == (i + 1)); 327 passed &= (args.args[1] == 100); 328 break; 329 case 5: 330 passed &= !rc; 331 passed &= (args.args_count == 0); 332 break; 333 case 6: 334 passed &= !rc; 335 passed &= (args.args_count == 1); 336 passed &= (args.args[0] == (i + 1)); 337 break; 338 case 7: 339 passed &= (rc == -ENOENT); 340 break; 341 default: 342 passed = false; 343 } 344 345 selftest(passed, "index %i - data error on node %s rc=%i\n", 346 i, args.np->full_name, rc); 347 } 348 349 /* Check for missing list property */ 350 rc = of_parse_phandle_with_args(np, "phandle-list-missing", 351 "#phandle-cells", 0, &args); 352 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); 353 rc = of_count_phandle_with_args(np, "phandle-list-missing", 354 "#phandle-cells"); 355 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); 356 357 /* Check for missing cells property */ 358 rc = of_parse_phandle_with_args(np, "phandle-list", 359 "#phandle-cells-missing", 0, &args); 360 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); 361 rc = of_count_phandle_with_args(np, "phandle-list", 362 "#phandle-cells-missing"); 363 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); 364 365 /* Check for bad phandle in list */ 366 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", 367 "#phandle-cells", 0, &args); 368 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); 369 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", 370 "#phandle-cells"); 371 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); 372 373 /* Check for incorrectly formed argument list */ 374 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", 375 "#phandle-cells", 1, &args); 376 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); 377 rc = of_count_phandle_with_args(np, "phandle-list-bad-args", 378 "#phandle-cells"); 379 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); 380 } 381 382 static void __init of_selftest_property_string(void) 383 { 384 const char *strings[4]; 385 struct device_node *np; 386 int rc; 387 388 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); 389 if (!np) { 390 pr_err("No testcase data in device tree\n"); 391 return; 392 } 393 394 rc = of_property_match_string(np, "phandle-list-names", "first"); 395 selftest(rc == 0, "first expected:0 got:%i\n", rc); 396 rc = of_property_match_string(np, "phandle-list-names", "second"); 397 selftest(rc == 1, "second expected:1 got:%i\n", rc); 398 rc = of_property_match_string(np, "phandle-list-names", "third"); 399 selftest(rc == 2, "third expected:2 got:%i\n", rc); 400 rc = of_property_match_string(np, "phandle-list-names", "fourth"); 401 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); 402 rc = of_property_match_string(np, "missing-property", "blah"); 403 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc); 404 rc = of_property_match_string(np, "empty-property", "blah"); 405 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc); 406 rc = of_property_match_string(np, "unterminated-string", "blah"); 407 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); 408 409 /* of_property_count_strings() tests */ 410 rc = of_property_count_strings(np, "string-property"); 411 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); 412 rc = of_property_count_strings(np, "phandle-list-names"); 413 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); 414 rc = of_property_count_strings(np, "unterminated-string"); 415 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); 416 rc = of_property_count_strings(np, "unterminated-string-list"); 417 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); 418 419 /* of_property_read_string_index() tests */ 420 rc = of_property_read_string_index(np, "string-property", 0, strings); 421 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc); 422 strings[0] = NULL; 423 rc = of_property_read_string_index(np, "string-property", 1, strings); 424 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); 425 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings); 426 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); 427 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings); 428 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc); 429 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings); 430 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc); 431 strings[0] = NULL; 432 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings); 433 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); 434 strings[0] = NULL; 435 rc = of_property_read_string_index(np, "unterminated-string", 0, strings); 436 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); 437 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings); 438 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); 439 strings[0] = NULL; 440 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */ 441 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); 442 strings[1] = NULL; 443 444 /* of_property_read_string_array() tests */ 445 rc = of_property_read_string_array(np, "string-property", strings, 4); 446 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); 447 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4); 448 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); 449 rc = of_property_read_string_array(np, "unterminated-string", strings, 4); 450 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); 451 /* -- An incorrectly formed string should cause a failure */ 452 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4); 453 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); 454 /* -- parsing the correctly formed strings should still work: */ 455 strings[2] = NULL; 456 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2); 457 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc); 458 strings[1] = NULL; 459 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1); 460 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]); 461 } 462 463 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ 464 (p1)->value && (p2)->value && \ 465 !memcmp((p1)->value, (p2)->value, (p1)->length) && \ 466 !strcmp((p1)->name, (p2)->name)) 467 static void __init of_selftest_property_copy(void) 468 { 469 #ifdef CONFIG_OF_DYNAMIC 470 struct property p1 = { .name = "p1", .length = 0, .value = "" }; 471 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; 472 struct property *new; 473 474 new = __of_prop_dup(&p1, GFP_KERNEL); 475 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); 476 kfree(new->value); 477 kfree(new->name); 478 kfree(new); 479 480 new = __of_prop_dup(&p2, GFP_KERNEL); 481 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); 482 kfree(new->value); 483 kfree(new->name); 484 kfree(new); 485 #endif 486 } 487 488 static void __init of_selftest_changeset(void) 489 { 490 #ifdef CONFIG_OF_DYNAMIC 491 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; 492 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; 493 struct property *ppremove; 494 struct device_node *n1, *n2, *n21, *nremove, *parent, *np; 495 struct of_changeset chgset; 496 497 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); 498 selftest(n1, "testcase setup failure\n"); 499 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); 500 selftest(n2, "testcase setup failure\n"); 501 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); 502 selftest(n21, "testcase setup failure %p\n", n21); 503 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); 504 selftest(nremove, "testcase setup failure\n"); 505 ppadd = __of_prop_dup(&padd, GFP_KERNEL); 506 selftest(ppadd, "testcase setup failure\n"); 507 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); 508 selftest(ppupdate, "testcase setup failure\n"); 509 parent = nremove->parent; 510 n1->parent = parent; 511 n2->parent = parent; 512 n21->parent = n2; 513 n2->child = n21; 514 ppremove = of_find_property(parent, "prop-remove", NULL); 515 selftest(ppremove, "failed to find removal prop"); 516 517 of_changeset_init(&chgset); 518 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); 519 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); 520 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); 521 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); 522 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); 523 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); 524 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); 525 mutex_lock(&of_mutex); 526 selftest(!of_changeset_apply(&chgset), "apply failed\n"); 527 mutex_unlock(&of_mutex); 528 529 /* Make sure node names are constructed correctly */ 530 selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")), 531 "'%s' not added\n", n21->full_name); 532 of_node_put(np); 533 534 mutex_lock(&of_mutex); 535 selftest(!of_changeset_revert(&chgset), "revert failed\n"); 536 mutex_unlock(&of_mutex); 537 538 of_changeset_destroy(&chgset); 539 #endif 540 } 541 542 static void __init of_selftest_parse_interrupts(void) 543 { 544 struct device_node *np; 545 struct of_phandle_args args; 546 int i, rc; 547 548 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); 549 if (!np) { 550 pr_err("missing testcase data\n"); 551 return; 552 } 553 554 for (i = 0; i < 4; i++) { 555 bool passed = true; 556 args.args_count = 0; 557 rc = of_irq_parse_one(np, i, &args); 558 559 passed &= !rc; 560 passed &= (args.args_count == 1); 561 passed &= (args.args[0] == (i + 1)); 562 563 selftest(passed, "index %i - data error on node %s rc=%i\n", 564 i, args.np->full_name, rc); 565 } 566 of_node_put(np); 567 568 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); 569 if (!np) { 570 pr_err("missing testcase data\n"); 571 return; 572 } 573 574 for (i = 0; i < 4; i++) { 575 bool passed = true; 576 args.args_count = 0; 577 rc = of_irq_parse_one(np, i, &args); 578 579 /* Test the values from tests-phandle.dtsi */ 580 switch (i) { 581 case 0: 582 passed &= !rc; 583 passed &= (args.args_count == 1); 584 passed &= (args.args[0] == 9); 585 break; 586 case 1: 587 passed &= !rc; 588 passed &= (args.args_count == 3); 589 passed &= (args.args[0] == 10); 590 passed &= (args.args[1] == 11); 591 passed &= (args.args[2] == 12); 592 break; 593 case 2: 594 passed &= !rc; 595 passed &= (args.args_count == 2); 596 passed &= (args.args[0] == 13); 597 passed &= (args.args[1] == 14); 598 break; 599 case 3: 600 passed &= !rc; 601 passed &= (args.args_count == 2); 602 passed &= (args.args[0] == 15); 603 passed &= (args.args[1] == 16); 604 break; 605 default: 606 passed = false; 607 } 608 selftest(passed, "index %i - data error on node %s rc=%i\n", 609 i, args.np->full_name, rc); 610 } 611 of_node_put(np); 612 } 613 614 static void __init of_selftest_parse_interrupts_extended(void) 615 { 616 struct device_node *np; 617 struct of_phandle_args args; 618 int i, rc; 619 620 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); 621 if (!np) { 622 pr_err("missing testcase data\n"); 623 return; 624 } 625 626 for (i = 0; i < 7; i++) { 627 bool passed = true; 628 rc = of_irq_parse_one(np, i, &args); 629 630 /* Test the values from tests-phandle.dtsi */ 631 switch (i) { 632 case 0: 633 passed &= !rc; 634 passed &= (args.args_count == 1); 635 passed &= (args.args[0] == 1); 636 break; 637 case 1: 638 passed &= !rc; 639 passed &= (args.args_count == 3); 640 passed &= (args.args[0] == 2); 641 passed &= (args.args[1] == 3); 642 passed &= (args.args[2] == 4); 643 break; 644 case 2: 645 passed &= !rc; 646 passed &= (args.args_count == 2); 647 passed &= (args.args[0] == 5); 648 passed &= (args.args[1] == 6); 649 break; 650 case 3: 651 passed &= !rc; 652 passed &= (args.args_count == 1); 653 passed &= (args.args[0] == 9); 654 break; 655 case 4: 656 passed &= !rc; 657 passed &= (args.args_count == 3); 658 passed &= (args.args[0] == 10); 659 passed &= (args.args[1] == 11); 660 passed &= (args.args[2] == 12); 661 break; 662 case 5: 663 passed &= !rc; 664 passed &= (args.args_count == 2); 665 passed &= (args.args[0] == 13); 666 passed &= (args.args[1] == 14); 667 break; 668 case 6: 669 passed &= !rc; 670 passed &= (args.args_count == 1); 671 passed &= (args.args[0] == 15); 672 break; 673 default: 674 passed = false; 675 } 676 677 selftest(passed, "index %i - data error on node %s rc=%i\n", 678 i, args.np->full_name, rc); 679 } 680 of_node_put(np); 681 } 682 683 static struct of_device_id match_node_table[] = { 684 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */ 685 { .data = "B", .type = "type1", }, /* followed by type alone */ 686 687 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */ 688 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */ 689 { .data = "Cc", .name = "name2", .type = "type2", }, 690 691 { .data = "E", .compatible = "compat3" }, 692 { .data = "G", .compatible = "compat2", }, 693 { .data = "H", .compatible = "compat2", .name = "name5", }, 694 { .data = "I", .compatible = "compat2", .type = "type1", }, 695 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", }, 696 { .data = "K", .compatible = "compat2", .name = "name9", }, 697 {} 698 }; 699 700 static struct { 701 const char *path; 702 const char *data; 703 } match_node_tests[] = { 704 { .path = "/testcase-data/match-node/name0", .data = "A", }, 705 { .path = "/testcase-data/match-node/name1", .data = "B", }, 706 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", }, 707 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", }, 708 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", }, 709 { .path = "/testcase-data/match-node/name3", .data = "E", }, 710 { .path = "/testcase-data/match-node/name4", .data = "G", }, 711 { .path = "/testcase-data/match-node/name5", .data = "H", }, 712 { .path = "/testcase-data/match-node/name6", .data = "G", }, 713 { .path = "/testcase-data/match-node/name7", .data = "I", }, 714 { .path = "/testcase-data/match-node/name8", .data = "J", }, 715 { .path = "/testcase-data/match-node/name9", .data = "K", }, 716 }; 717 718 static void __init of_selftest_match_node(void) 719 { 720 struct device_node *np; 721 const struct of_device_id *match; 722 int i; 723 724 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) { 725 np = of_find_node_by_path(match_node_tests[i].path); 726 if (!np) { 727 selftest(0, "missing testcase node %s\n", 728 match_node_tests[i].path); 729 continue; 730 } 731 732 match = of_match_node(match_node_table, np); 733 if (!match) { 734 selftest(0, "%s didn't match anything\n", 735 match_node_tests[i].path); 736 continue; 737 } 738 739 if (strcmp(match->data, match_node_tests[i].data) != 0) { 740 selftest(0, "%s got wrong match. expected %s, got %s\n", 741 match_node_tests[i].path, match_node_tests[i].data, 742 (const char *)match->data); 743 continue; 744 } 745 selftest(1, "passed"); 746 } 747 } 748 749 struct device test_bus = { 750 .init_name = "unittest-bus", 751 }; 752 static void __init of_selftest_platform_populate(void) 753 { 754 int irq, rc; 755 struct device_node *np, *child, *grandchild; 756 struct platform_device *pdev; 757 struct of_device_id match[] = { 758 { .compatible = "test-device", }, 759 {} 760 }; 761 762 np = of_find_node_by_path("/testcase-data"); 763 of_platform_populate(np, of_default_bus_match_table, NULL, NULL); 764 765 /* Test that a missing irq domain returns -EPROBE_DEFER */ 766 np = of_find_node_by_path("/testcase-data/testcase-device1"); 767 pdev = of_find_device_by_node(np); 768 selftest(pdev, "device 1 creation failed\n"); 769 770 irq = platform_get_irq(pdev, 0); 771 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); 772 773 /* Test that a parsing failure does not return -EPROBE_DEFER */ 774 np = of_find_node_by_path("/testcase-data/testcase-device2"); 775 pdev = of_find_device_by_node(np); 776 selftest(pdev, "device 2 creation failed\n"); 777 irq = platform_get_irq(pdev, 0); 778 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq); 779 780 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"), 781 "No testcase data in device tree\n")); 782 return; 783 784 if (selftest(!(rc = device_register(&test_bus)), 785 "testbus registration failed; rc=%i\n", rc)); 786 return; 787 788 for_each_child_of_node(np, child) { 789 of_platform_populate(child, match, NULL, &test_bus); 790 for_each_child_of_node(child, grandchild) 791 selftest(of_find_device_by_node(grandchild), 792 "Could not create device for node '%s'\n", 793 grandchild->name); 794 } 795 796 of_platform_depopulate(&test_bus); 797 for_each_child_of_node(np, child) { 798 for_each_child_of_node(child, grandchild) 799 selftest(!of_find_device_by_node(grandchild), 800 "device didn't get destroyed '%s'\n", 801 grandchild->name); 802 } 803 804 device_unregister(&test_bus); 805 of_node_put(np); 806 } 807 808 /** 809 * update_node_properties - adds the properties 810 * of np into dup node (present in live tree) and 811 * updates parent of children of np to dup. 812 * 813 * @np: node already present in live tree 814 * @dup: node present in live tree to be updated 815 */ 816 static void update_node_properties(struct device_node *np, 817 struct device_node *dup) 818 { 819 struct property *prop; 820 struct device_node *child; 821 822 for_each_property_of_node(np, prop) 823 of_add_property(dup, prop); 824 825 for_each_child_of_node(np, child) 826 child->parent = dup; 827 } 828 829 /** 830 * attach_node_and_children - attaches nodes 831 * and its children to live tree 832 * 833 * @np: Node to attach to live tree 834 */ 835 static int attach_node_and_children(struct device_node *np) 836 { 837 struct device_node *next, *dup, *child; 838 unsigned long flags; 839 840 dup = of_find_node_by_path(np->full_name); 841 if (dup) { 842 update_node_properties(np, dup); 843 return 0; 844 } 845 846 child = np->child; 847 np->child = NULL; 848 849 mutex_lock(&of_mutex); 850 raw_spin_lock_irqsave(&devtree_lock, flags); 851 np->sibling = np->parent->child; 852 np->parent->child = np; 853 of_node_clear_flag(np, OF_DETACHED); 854 raw_spin_unlock_irqrestore(&devtree_lock, flags); 855 856 __of_attach_node_sysfs(np); 857 mutex_unlock(&of_mutex); 858 859 while (child) { 860 next = child->sibling; 861 attach_node_and_children(child); 862 child = next; 863 } 864 865 return 0; 866 } 867 868 /** 869 * selftest_data_add - Reads, copies data from 870 * linked tree and attaches it to the live tree 871 */ 872 static int __init selftest_data_add(void) 873 { 874 void *selftest_data; 875 struct device_node *selftest_data_node, *np; 876 extern uint8_t __dtb_testcases_begin[]; 877 extern uint8_t __dtb_testcases_end[]; 878 const int size = __dtb_testcases_end - __dtb_testcases_begin; 879 int rc; 880 881 if (!size) { 882 pr_warn("%s: No testcase data to attach; not running tests\n", 883 __func__); 884 return -ENODATA; 885 } 886 887 /* creating copy */ 888 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL); 889 890 if (!selftest_data) { 891 pr_warn("%s: Failed to allocate memory for selftest_data; " 892 "not running tests\n", __func__); 893 return -ENOMEM; 894 } 895 of_fdt_unflatten_tree(selftest_data, &selftest_data_node); 896 if (!selftest_data_node) { 897 pr_warn("%s: No tree to attach; not running tests\n", __func__); 898 return -ENODATA; 899 } 900 of_node_set_flag(selftest_data_node, OF_DETACHED); 901 rc = of_resolve_phandles(selftest_data_node); 902 if (rc) { 903 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc); 904 return -EINVAL; 905 } 906 907 if (!of_root) { 908 of_root = selftest_data_node; 909 for_each_of_allnodes(np) 910 __of_attach_node_sysfs(np); 911 of_aliases = of_find_node_by_path("/aliases"); 912 of_chosen = of_find_node_by_path("/chosen"); 913 return 0; 914 } 915 916 /* attach the sub-tree to live tree */ 917 np = selftest_data_node->child; 918 while (np) { 919 struct device_node *next = np->sibling; 920 np->parent = of_root; 921 attach_node_and_children(np); 922 np = next; 923 } 924 return 0; 925 } 926 927 #ifdef CONFIG_OF_OVERLAY 928 929 static int selftest_probe(struct platform_device *pdev) 930 { 931 struct device *dev = &pdev->dev; 932 struct device_node *np = dev->of_node; 933 934 if (np == NULL) { 935 dev_err(dev, "No OF data for device\n"); 936 return -EINVAL; 937 938 } 939 940 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 941 942 of_platform_populate(np, NULL, NULL, &pdev->dev); 943 944 return 0; 945 } 946 947 static int selftest_remove(struct platform_device *pdev) 948 { 949 struct device *dev = &pdev->dev; 950 struct device_node *np = dev->of_node; 951 952 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 953 return 0; 954 } 955 956 static struct of_device_id selftest_match[] = { 957 { .compatible = "selftest", }, 958 {}, 959 }; 960 961 static struct platform_driver selftest_driver = { 962 .probe = selftest_probe, 963 .remove = selftest_remove, 964 .driver = { 965 .name = "selftest", 966 .owner = THIS_MODULE, 967 .of_match_table = of_match_ptr(selftest_match), 968 }, 969 }; 970 971 /* get the platform device instantiated at the path */ 972 static struct platform_device *of_path_to_platform_device(const char *path) 973 { 974 struct device_node *np; 975 struct platform_device *pdev; 976 977 np = of_find_node_by_path(path); 978 if (np == NULL) 979 return NULL; 980 981 pdev = of_find_device_by_node(np); 982 of_node_put(np); 983 984 return pdev; 985 } 986 987 /* find out if a platform device exists at that path */ 988 static int of_path_platform_device_exists(const char *path) 989 { 990 struct platform_device *pdev; 991 992 pdev = of_path_to_platform_device(path); 993 platform_device_put(pdev); 994 return pdev != NULL; 995 } 996 997 #if IS_BUILTIN(CONFIG_I2C) 998 999 /* get the i2c client device instantiated at the path */ 1000 static struct i2c_client *of_path_to_i2c_client(const char *path) 1001 { 1002 struct device_node *np; 1003 struct i2c_client *client; 1004 1005 np = of_find_node_by_path(path); 1006 if (np == NULL) 1007 return NULL; 1008 1009 client = of_find_i2c_device_by_node(np); 1010 of_node_put(np); 1011 1012 return client; 1013 } 1014 1015 /* find out if a i2c client device exists at that path */ 1016 static int of_path_i2c_client_exists(const char *path) 1017 { 1018 struct i2c_client *client; 1019 1020 client = of_path_to_i2c_client(path); 1021 if (client) 1022 put_device(&client->dev); 1023 return client != NULL; 1024 } 1025 #else 1026 static int of_path_i2c_client_exists(const char *path) 1027 { 1028 return 0; 1029 } 1030 #endif 1031 1032 enum overlay_type { 1033 PDEV_OVERLAY, 1034 I2C_OVERLAY 1035 }; 1036 1037 static int of_path_device_type_exists(const char *path, 1038 enum overlay_type ovtype) 1039 { 1040 switch (ovtype) { 1041 case PDEV_OVERLAY: 1042 return of_path_platform_device_exists(path); 1043 case I2C_OVERLAY: 1044 return of_path_i2c_client_exists(path); 1045 } 1046 return 0; 1047 } 1048 1049 static const char *selftest_path(int nr, enum overlay_type ovtype) 1050 { 1051 const char *base; 1052 static char buf[256]; 1053 1054 switch (ovtype) { 1055 case PDEV_OVERLAY: 1056 base = "/testcase-data/overlay-node/test-bus"; 1057 break; 1058 case I2C_OVERLAY: 1059 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus"; 1060 break; 1061 default: 1062 buf[0] = '\0'; 1063 return buf; 1064 } 1065 snprintf(buf, sizeof(buf) - 1, "%s/test-selftest%d", base, nr); 1066 buf[sizeof(buf) - 1] = '\0'; 1067 return buf; 1068 } 1069 1070 static int of_selftest_device_exists(int selftest_nr, enum overlay_type ovtype) 1071 { 1072 const char *path; 1073 1074 path = selftest_path(selftest_nr, ovtype); 1075 1076 switch (ovtype) { 1077 case PDEV_OVERLAY: 1078 return of_path_platform_device_exists(path); 1079 case I2C_OVERLAY: 1080 return of_path_i2c_client_exists(path); 1081 } 1082 return 0; 1083 } 1084 1085 static const char *overlay_path(int nr) 1086 { 1087 static char buf[256]; 1088 1089 snprintf(buf, sizeof(buf) - 1, 1090 "/testcase-data/overlay%d", nr); 1091 buf[sizeof(buf) - 1] = '\0'; 1092 1093 return buf; 1094 } 1095 1096 static const char *bus_path = "/testcase-data/overlay-node/test-bus"; 1097 1098 static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr, 1099 int *overlay_id) 1100 { 1101 struct device_node *np = NULL; 1102 int ret, id = -1; 1103 1104 np = of_find_node_by_path(overlay_path(overlay_nr)); 1105 if (np == NULL) { 1106 selftest(0, "could not find overlay node @\"%s\"\n", 1107 overlay_path(overlay_nr)); 1108 ret = -EINVAL; 1109 goto out; 1110 } 1111 1112 ret = of_overlay_create(np); 1113 if (ret < 0) { 1114 selftest(0, "could not create overlay from \"%s\"\n", 1115 overlay_path(overlay_nr)); 1116 goto out; 1117 } 1118 id = ret; 1119 1120 ret = 0; 1121 1122 out: 1123 of_node_put(np); 1124 1125 if (overlay_id) 1126 *overlay_id = id; 1127 1128 return ret; 1129 } 1130 1131 /* apply an overlay while checking before and after states */ 1132 static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr, 1133 int before, int after, enum overlay_type ovtype) 1134 { 1135 int ret; 1136 1137 /* selftest device must not be in before state */ 1138 if (of_selftest_device_exists(selftest_nr, ovtype) != before) { 1139 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1140 overlay_path(overlay_nr), 1141 selftest_path(selftest_nr, ovtype), 1142 !before ? "enabled" : "disabled"); 1143 return -EINVAL; 1144 } 1145 1146 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL); 1147 if (ret != 0) { 1148 /* of_selftest_apply_overlay already called selftest() */ 1149 return ret; 1150 } 1151 1152 /* selftest device must be to set to after state */ 1153 if (of_selftest_device_exists(selftest_nr, ovtype) != after) { 1154 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", 1155 overlay_path(overlay_nr), 1156 selftest_path(selftest_nr, ovtype), 1157 !after ? "enabled" : "disabled"); 1158 return -EINVAL; 1159 } 1160 1161 return 0; 1162 } 1163 1164 /* apply an overlay and then revert it while checking before, after states */ 1165 static int of_selftest_apply_revert_overlay_check(int overlay_nr, 1166 int selftest_nr, int before, int after, 1167 enum overlay_type ovtype) 1168 { 1169 int ret, ov_id; 1170 1171 /* selftest device must be in before state */ 1172 if (of_selftest_device_exists(selftest_nr, ovtype) != before) { 1173 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1174 overlay_path(overlay_nr), 1175 selftest_path(selftest_nr, ovtype), 1176 !before ? "enabled" : "disabled"); 1177 return -EINVAL; 1178 } 1179 1180 /* apply the overlay */ 1181 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id); 1182 if (ret != 0) { 1183 /* of_selftest_apply_overlay already called selftest() */ 1184 return ret; 1185 } 1186 1187 /* selftest device must be in after state */ 1188 if (of_selftest_device_exists(selftest_nr, ovtype) != after) { 1189 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", 1190 overlay_path(overlay_nr), 1191 selftest_path(selftest_nr, ovtype), 1192 !after ? "enabled" : "disabled"); 1193 return -EINVAL; 1194 } 1195 1196 ret = of_overlay_destroy(ov_id); 1197 if (ret != 0) { 1198 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n", 1199 overlay_path(overlay_nr), 1200 selftest_path(selftest_nr, ovtype)); 1201 return ret; 1202 } 1203 1204 /* selftest device must be again in before state */ 1205 if (of_selftest_device_exists(selftest_nr, PDEV_OVERLAY) != before) { 1206 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1207 overlay_path(overlay_nr), 1208 selftest_path(selftest_nr, ovtype), 1209 !before ? "enabled" : "disabled"); 1210 return -EINVAL; 1211 } 1212 1213 return 0; 1214 } 1215 1216 /* test activation of device */ 1217 static void of_selftest_overlay_0(void) 1218 { 1219 int ret; 1220 1221 /* device should enable */ 1222 ret = of_selftest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY); 1223 if (ret != 0) 1224 return; 1225 1226 selftest(1, "overlay test %d passed\n", 0); 1227 } 1228 1229 /* test deactivation of device */ 1230 static void of_selftest_overlay_1(void) 1231 { 1232 int ret; 1233 1234 /* device should disable */ 1235 ret = of_selftest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY); 1236 if (ret != 0) 1237 return; 1238 1239 selftest(1, "overlay test %d passed\n", 1); 1240 } 1241 1242 /* test activation of device */ 1243 static void of_selftest_overlay_2(void) 1244 { 1245 int ret; 1246 1247 /* device should enable */ 1248 ret = of_selftest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY); 1249 if (ret != 0) 1250 return; 1251 1252 selftest(1, "overlay test %d passed\n", 2); 1253 } 1254 1255 /* test deactivation of device */ 1256 static void of_selftest_overlay_3(void) 1257 { 1258 int ret; 1259 1260 /* device should disable */ 1261 ret = of_selftest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY); 1262 if (ret != 0) 1263 return; 1264 1265 selftest(1, "overlay test %d passed\n", 3); 1266 } 1267 1268 /* test activation of a full device node */ 1269 static void of_selftest_overlay_4(void) 1270 { 1271 int ret; 1272 1273 /* device should disable */ 1274 ret = of_selftest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY); 1275 if (ret != 0) 1276 return; 1277 1278 selftest(1, "overlay test %d passed\n", 4); 1279 } 1280 1281 /* test overlay apply/revert sequence */ 1282 static void of_selftest_overlay_5(void) 1283 { 1284 int ret; 1285 1286 /* device should disable */ 1287 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY); 1288 if (ret != 0) 1289 return; 1290 1291 selftest(1, "overlay test %d passed\n", 5); 1292 } 1293 1294 /* test overlay application in sequence */ 1295 static void of_selftest_overlay_6(void) 1296 { 1297 struct device_node *np; 1298 int ret, i, ov_id[2]; 1299 int overlay_nr = 6, selftest_nr = 6; 1300 int before = 0, after = 1; 1301 1302 /* selftest device must be in before state */ 1303 for (i = 0; i < 2; i++) { 1304 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) 1305 != before) { 1306 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1307 overlay_path(overlay_nr + i), 1308 selftest_path(selftest_nr + i, 1309 PDEV_OVERLAY), 1310 !before ? "enabled" : "disabled"); 1311 return; 1312 } 1313 } 1314 1315 /* apply the overlays */ 1316 for (i = 0; i < 2; i++) { 1317 1318 np = of_find_node_by_path(overlay_path(overlay_nr + i)); 1319 if (np == NULL) { 1320 selftest(0, "could not find overlay node @\"%s\"\n", 1321 overlay_path(overlay_nr + i)); 1322 return; 1323 } 1324 1325 ret = of_overlay_create(np); 1326 if (ret < 0) { 1327 selftest(0, "could not create overlay from \"%s\"\n", 1328 overlay_path(overlay_nr + i)); 1329 return; 1330 } 1331 ov_id[i] = ret; 1332 } 1333 1334 for (i = 0; i < 2; i++) { 1335 /* selftest device must be in after state */ 1336 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) 1337 != after) { 1338 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n", 1339 overlay_path(overlay_nr + i), 1340 selftest_path(selftest_nr + i, 1341 PDEV_OVERLAY), 1342 !after ? "enabled" : "disabled"); 1343 return; 1344 } 1345 } 1346 1347 for (i = 1; i >= 0; i--) { 1348 ret = of_overlay_destroy(ov_id[i]); 1349 if (ret != 0) { 1350 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n", 1351 overlay_path(overlay_nr + i), 1352 selftest_path(selftest_nr + i, 1353 PDEV_OVERLAY)); 1354 return; 1355 } 1356 } 1357 1358 for (i = 0; i < 2; i++) { 1359 /* selftest device must be again in before state */ 1360 if (of_selftest_device_exists(selftest_nr + i, PDEV_OVERLAY) 1361 != before) { 1362 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", 1363 overlay_path(overlay_nr + i), 1364 selftest_path(selftest_nr + i, 1365 PDEV_OVERLAY), 1366 !before ? "enabled" : "disabled"); 1367 return; 1368 } 1369 } 1370 1371 selftest(1, "overlay test %d passed\n", 6); 1372 } 1373 1374 /* test overlay application in sequence */ 1375 static void of_selftest_overlay_8(void) 1376 { 1377 struct device_node *np; 1378 int ret, i, ov_id[2]; 1379 int overlay_nr = 8, selftest_nr = 8; 1380 1381 /* we don't care about device state in this test */ 1382 1383 /* apply the overlays */ 1384 for (i = 0; i < 2; i++) { 1385 1386 np = of_find_node_by_path(overlay_path(overlay_nr + i)); 1387 if (np == NULL) { 1388 selftest(0, "could not find overlay node @\"%s\"\n", 1389 overlay_path(overlay_nr + i)); 1390 return; 1391 } 1392 1393 ret = of_overlay_create(np); 1394 if (ret < 0) { 1395 selftest(0, "could not create overlay from \"%s\"\n", 1396 overlay_path(overlay_nr + i)); 1397 return; 1398 } 1399 ov_id[i] = ret; 1400 } 1401 1402 /* now try to remove first overlay (it should fail) */ 1403 ret = of_overlay_destroy(ov_id[0]); 1404 if (ret == 0) { 1405 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n", 1406 overlay_path(overlay_nr + 0), 1407 selftest_path(selftest_nr, 1408 PDEV_OVERLAY)); 1409 return; 1410 } 1411 1412 /* removing them in order should work */ 1413 for (i = 1; i >= 0; i--) { 1414 ret = of_overlay_destroy(ov_id[i]); 1415 if (ret != 0) { 1416 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n", 1417 overlay_path(overlay_nr + i), 1418 selftest_path(selftest_nr, 1419 PDEV_OVERLAY)); 1420 return; 1421 } 1422 } 1423 1424 selftest(1, "overlay test %d passed\n", 8); 1425 } 1426 1427 /* test insertion of a bus with parent devices */ 1428 static void of_selftest_overlay_10(void) 1429 { 1430 int ret; 1431 char *child_path; 1432 1433 /* device should disable */ 1434 ret = of_selftest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY); 1435 if (selftest(ret == 0, 1436 "overlay test %d failed; overlay application\n", 10)) 1437 return; 1438 1439 child_path = kasprintf(GFP_KERNEL, "%s/test-selftest101", 1440 selftest_path(10, PDEV_OVERLAY)); 1441 if (selftest(child_path, "overlay test %d failed; kasprintf\n", 10)) 1442 return; 1443 1444 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY); 1445 kfree(child_path); 1446 if (selftest(ret, "overlay test %d failed; no child device\n", 10)) 1447 return; 1448 } 1449 1450 /* test insertion of a bus with parent devices (and revert) */ 1451 static void of_selftest_overlay_11(void) 1452 { 1453 int ret; 1454 1455 /* device should disable */ 1456 ret = of_selftest_apply_revert_overlay_check(11, 11, 0, 1, 1457 PDEV_OVERLAY); 1458 if (selftest(ret == 0, 1459 "overlay test %d failed; overlay application\n", 11)) 1460 return; 1461 } 1462 1463 #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY) 1464 1465 struct selftest_i2c_bus_data { 1466 struct platform_device *pdev; 1467 struct i2c_adapter adap; 1468 }; 1469 1470 static int selftest_i2c_master_xfer(struct i2c_adapter *adap, 1471 struct i2c_msg *msgs, int num) 1472 { 1473 struct selftest_i2c_bus_data *std = i2c_get_adapdata(adap); 1474 1475 (void)std; 1476 1477 return num; 1478 } 1479 1480 static u32 selftest_i2c_functionality(struct i2c_adapter *adap) 1481 { 1482 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 1483 } 1484 1485 static const struct i2c_algorithm selftest_i2c_algo = { 1486 .master_xfer = selftest_i2c_master_xfer, 1487 .functionality = selftest_i2c_functionality, 1488 }; 1489 1490 static int selftest_i2c_bus_probe(struct platform_device *pdev) 1491 { 1492 struct device *dev = &pdev->dev; 1493 struct device_node *np = dev->of_node; 1494 struct selftest_i2c_bus_data *std; 1495 struct i2c_adapter *adap; 1496 int ret; 1497 1498 if (np == NULL) { 1499 dev_err(dev, "No OF data for device\n"); 1500 return -EINVAL; 1501 1502 } 1503 1504 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 1505 1506 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL); 1507 if (!std) { 1508 dev_err(dev, "Failed to allocate selftest i2c data\n"); 1509 return -ENOMEM; 1510 } 1511 1512 /* link them together */ 1513 std->pdev = pdev; 1514 platform_set_drvdata(pdev, std); 1515 1516 adap = &std->adap; 1517 i2c_set_adapdata(adap, std); 1518 adap->nr = -1; 1519 strlcpy(adap->name, pdev->name, sizeof(adap->name)); 1520 adap->class = I2C_CLASS_DEPRECATED; 1521 adap->algo = &selftest_i2c_algo; 1522 adap->dev.parent = dev; 1523 adap->dev.of_node = dev->of_node; 1524 adap->timeout = 5 * HZ; 1525 adap->retries = 3; 1526 1527 ret = i2c_add_numbered_adapter(adap); 1528 if (ret != 0) { 1529 dev_err(dev, "Failed to add I2C adapter\n"); 1530 return ret; 1531 } 1532 1533 return 0; 1534 } 1535 1536 static int selftest_i2c_bus_remove(struct platform_device *pdev) 1537 { 1538 struct device *dev = &pdev->dev; 1539 struct device_node *np = dev->of_node; 1540 struct selftest_i2c_bus_data *std = platform_get_drvdata(pdev); 1541 1542 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 1543 i2c_del_adapter(&std->adap); 1544 1545 return 0; 1546 } 1547 1548 static struct of_device_id selftest_i2c_bus_match[] = { 1549 { .compatible = "selftest-i2c-bus", }, 1550 {}, 1551 }; 1552 1553 static struct platform_driver selftest_i2c_bus_driver = { 1554 .probe = selftest_i2c_bus_probe, 1555 .remove = selftest_i2c_bus_remove, 1556 .driver = { 1557 .name = "selftest-i2c-bus", 1558 .of_match_table = of_match_ptr(selftest_i2c_bus_match), 1559 }, 1560 }; 1561 1562 static int selftest_i2c_dev_probe(struct i2c_client *client, 1563 const struct i2c_device_id *id) 1564 { 1565 struct device *dev = &client->dev; 1566 struct device_node *np = client->dev.of_node; 1567 1568 if (!np) { 1569 dev_err(dev, "No OF node\n"); 1570 return -EINVAL; 1571 } 1572 1573 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 1574 1575 return 0; 1576 }; 1577 1578 static int selftest_i2c_dev_remove(struct i2c_client *client) 1579 { 1580 struct device *dev = &client->dev; 1581 struct device_node *np = client->dev.of_node; 1582 1583 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 1584 return 0; 1585 } 1586 1587 static const struct i2c_device_id selftest_i2c_dev_id[] = { 1588 { .name = "selftest-i2c-dev" }, 1589 { } 1590 }; 1591 1592 static struct i2c_driver selftest_i2c_dev_driver = { 1593 .driver = { 1594 .name = "selftest-i2c-dev", 1595 .owner = THIS_MODULE, 1596 }, 1597 .probe = selftest_i2c_dev_probe, 1598 .remove = selftest_i2c_dev_remove, 1599 .id_table = selftest_i2c_dev_id, 1600 }; 1601 1602 #if IS_BUILTIN(CONFIG_I2C_MUX) 1603 1604 struct selftest_i2c_mux_data { 1605 int nchans; 1606 struct i2c_adapter *adap[]; 1607 }; 1608 1609 static int selftest_i2c_mux_select_chan(struct i2c_adapter *adap, 1610 void *client, u32 chan) 1611 { 1612 return 0; 1613 } 1614 1615 static int selftest_i2c_mux_probe(struct i2c_client *client, 1616 const struct i2c_device_id *id) 1617 { 1618 int ret, i, nchans, size; 1619 struct device *dev = &client->dev; 1620 struct i2c_adapter *adap = to_i2c_adapter(dev->parent); 1621 struct device_node *np = client->dev.of_node, *child; 1622 struct selftest_i2c_mux_data *stm; 1623 u32 reg, max_reg; 1624 1625 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 1626 1627 if (!np) { 1628 dev_err(dev, "No OF node\n"); 1629 return -EINVAL; 1630 } 1631 1632 max_reg = (u32)-1; 1633 for_each_child_of_node(np, child) { 1634 ret = of_property_read_u32(child, "reg", ®); 1635 if (ret) 1636 continue; 1637 if (max_reg == (u32)-1 || reg > max_reg) 1638 max_reg = reg; 1639 } 1640 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1; 1641 if (nchans == 0) { 1642 dev_err(dev, "No channels\n"); 1643 return -EINVAL; 1644 } 1645 1646 size = offsetof(struct selftest_i2c_mux_data, adap[nchans]); 1647 stm = devm_kzalloc(dev, size, GFP_KERNEL); 1648 if (!stm) { 1649 dev_err(dev, "Out of memory\n"); 1650 return -ENOMEM; 1651 } 1652 stm->nchans = nchans; 1653 for (i = 0; i < nchans; i++) { 1654 stm->adap[i] = i2c_add_mux_adapter(adap, dev, client, 1655 0, i, 0, selftest_i2c_mux_select_chan, NULL); 1656 if (!stm->adap[i]) { 1657 dev_err(dev, "Failed to register mux #%d\n", i); 1658 for (i--; i >= 0; i--) 1659 i2c_del_mux_adapter(stm->adap[i]); 1660 return -ENODEV; 1661 } 1662 } 1663 1664 i2c_set_clientdata(client, stm); 1665 1666 return 0; 1667 }; 1668 1669 static int selftest_i2c_mux_remove(struct i2c_client *client) 1670 { 1671 struct device *dev = &client->dev; 1672 struct device_node *np = client->dev.of_node; 1673 struct selftest_i2c_mux_data *stm = i2c_get_clientdata(client); 1674 int i; 1675 1676 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); 1677 for (i = stm->nchans - 1; i >= 0; i--) 1678 i2c_del_mux_adapter(stm->adap[i]); 1679 return 0; 1680 } 1681 1682 static const struct i2c_device_id selftest_i2c_mux_id[] = { 1683 { .name = "selftest-i2c-mux" }, 1684 { } 1685 }; 1686 1687 static struct i2c_driver selftest_i2c_mux_driver = { 1688 .driver = { 1689 .name = "selftest-i2c-mux", 1690 .owner = THIS_MODULE, 1691 }, 1692 .probe = selftest_i2c_mux_probe, 1693 .remove = selftest_i2c_mux_remove, 1694 .id_table = selftest_i2c_mux_id, 1695 }; 1696 1697 #endif 1698 1699 static int of_selftest_overlay_i2c_init(void) 1700 { 1701 int ret; 1702 1703 ret = i2c_add_driver(&selftest_i2c_dev_driver); 1704 if (selftest(ret == 0, 1705 "could not register selftest i2c device driver\n")) 1706 return ret; 1707 1708 ret = platform_driver_register(&selftest_i2c_bus_driver); 1709 if (selftest(ret == 0, 1710 "could not register selftest i2c bus driver\n")) 1711 return ret; 1712 1713 #if IS_BUILTIN(CONFIG_I2C_MUX) 1714 ret = i2c_add_driver(&selftest_i2c_mux_driver); 1715 if (selftest(ret == 0, 1716 "could not register selftest i2c mux driver\n")) 1717 return ret; 1718 #endif 1719 1720 return 0; 1721 } 1722 1723 static void of_selftest_overlay_i2c_cleanup(void) 1724 { 1725 #if IS_BUILTIN(CONFIG_I2C_MUX) 1726 i2c_del_driver(&selftest_i2c_mux_driver); 1727 #endif 1728 platform_driver_unregister(&selftest_i2c_bus_driver); 1729 i2c_del_driver(&selftest_i2c_dev_driver); 1730 } 1731 1732 static void of_selftest_overlay_i2c_12(void) 1733 { 1734 int ret; 1735 1736 /* device should enable */ 1737 ret = of_selftest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY); 1738 if (ret != 0) 1739 return; 1740 1741 selftest(1, "overlay test %d passed\n", 12); 1742 } 1743 1744 /* test deactivation of device */ 1745 static void of_selftest_overlay_i2c_13(void) 1746 { 1747 int ret; 1748 1749 /* device should disable */ 1750 ret = of_selftest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY); 1751 if (ret != 0) 1752 return; 1753 1754 selftest(1, "overlay test %d passed\n", 13); 1755 } 1756 1757 /* just check for i2c mux existence */ 1758 static void of_selftest_overlay_i2c_14(void) 1759 { 1760 } 1761 1762 static void of_selftest_overlay_i2c_15(void) 1763 { 1764 int ret; 1765 1766 /* device should enable */ 1767 ret = of_selftest_apply_overlay_check(16, 15, 0, 1, I2C_OVERLAY); 1768 if (ret != 0) 1769 return; 1770 1771 selftest(1, "overlay test %d passed\n", 15); 1772 } 1773 1774 #else 1775 1776 static inline void of_selftest_overlay_i2c_14(void) { } 1777 static inline void of_selftest_overlay_i2c_15(void) { } 1778 1779 #endif 1780 1781 static void __init of_selftest_overlay(void) 1782 { 1783 struct device_node *bus_np = NULL; 1784 int ret; 1785 1786 ret = platform_driver_register(&selftest_driver); 1787 if (ret != 0) { 1788 selftest(0, "could not register selftest driver\n"); 1789 goto out; 1790 } 1791 1792 bus_np = of_find_node_by_path(bus_path); 1793 if (bus_np == NULL) { 1794 selftest(0, "could not find bus_path \"%s\"\n", bus_path); 1795 goto out; 1796 } 1797 1798 ret = of_platform_populate(bus_np, of_default_bus_match_table, 1799 NULL, NULL); 1800 if (ret != 0) { 1801 selftest(0, "could not populate bus @ \"%s\"\n", bus_path); 1802 goto out; 1803 } 1804 1805 if (!of_selftest_device_exists(100, PDEV_OVERLAY)) { 1806 selftest(0, "could not find selftest0 @ \"%s\"\n", 1807 selftest_path(100, PDEV_OVERLAY)); 1808 goto out; 1809 } 1810 1811 if (of_selftest_device_exists(101, PDEV_OVERLAY)) { 1812 selftest(0, "selftest1 @ \"%s\" should not exist\n", 1813 selftest_path(101, PDEV_OVERLAY)); 1814 goto out; 1815 } 1816 1817 selftest(1, "basic infrastructure of overlays passed"); 1818 1819 /* tests in sequence */ 1820 of_selftest_overlay_0(); 1821 of_selftest_overlay_1(); 1822 of_selftest_overlay_2(); 1823 of_selftest_overlay_3(); 1824 of_selftest_overlay_4(); 1825 of_selftest_overlay_5(); 1826 of_selftest_overlay_6(); 1827 of_selftest_overlay_8(); 1828 1829 of_selftest_overlay_10(); 1830 of_selftest_overlay_11(); 1831 1832 #if IS_BUILTIN(CONFIG_I2C) 1833 if (selftest(of_selftest_overlay_i2c_init() == 0, "i2c init failed\n")) 1834 goto out; 1835 1836 of_selftest_overlay_i2c_12(); 1837 of_selftest_overlay_i2c_13(); 1838 of_selftest_overlay_i2c_14(); 1839 of_selftest_overlay_i2c_15(); 1840 1841 of_selftest_overlay_i2c_cleanup(); 1842 #endif 1843 1844 out: 1845 of_node_put(bus_np); 1846 } 1847 1848 #else 1849 static inline void __init of_selftest_overlay(void) { } 1850 #endif 1851 1852 static int __init of_selftest(void) 1853 { 1854 struct device_node *np; 1855 int res; 1856 1857 /* adding data for selftest */ 1858 res = selftest_data_add(); 1859 if (res) 1860 return res; 1861 if (!of_aliases) 1862 of_aliases = of_find_node_by_path("/aliases"); 1863 1864 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); 1865 if (!np) { 1866 pr_info("No testcase data in device tree; not running tests\n"); 1867 return 0; 1868 } 1869 of_node_put(np); 1870 1871 pr_info("start of selftest - you will see error messages\n"); 1872 of_selftest_check_tree_linkage(); 1873 of_selftest_check_phandles(); 1874 of_selftest_find_node_by_name(); 1875 of_selftest_dynamic(); 1876 of_selftest_parse_phandle_with_args(); 1877 of_selftest_property_string(); 1878 of_selftest_property_copy(); 1879 of_selftest_changeset(); 1880 of_selftest_parse_interrupts(); 1881 of_selftest_parse_interrupts_extended(); 1882 of_selftest_match_node(); 1883 of_selftest_platform_populate(); 1884 of_selftest_overlay(); 1885 1886 /* Double check linkage after removing testcase data */ 1887 of_selftest_check_tree_linkage(); 1888 1889 pr_info("end of selftest - %i passed, %i failed\n", 1890 selftest_results.passed, selftest_results.failed); 1891 1892 return 0; 1893 } 1894 late_initcall(of_selftest); 1895