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