1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Tests for the core driver model code 4 * 5 * Copyright (c) 2013 Google, Inc 6 */ 7 8 #include <common.h> 9 #include <errno.h> 10 #include <dm.h> 11 #include <fdtdec.h> 12 #include <malloc.h> 13 #include <dm/device-internal.h> 14 #include <dm/root.h> 15 #include <dm/util.h> 16 #include <dm/test.h> 17 #include <dm/uclass-internal.h> 18 #include <test/ut.h> 19 20 DECLARE_GLOBAL_DATA_PTR; 21 22 enum { 23 TEST_INTVAL1 = 0, 24 TEST_INTVAL2 = 3, 25 TEST_INTVAL3 = 6, 26 TEST_INTVAL_MANUAL = 101112, 27 TEST_INTVAL_PRE_RELOC = 7, 28 }; 29 30 static const struct dm_test_pdata test_pdata[] = { 31 { .ping_add = TEST_INTVAL1, }, 32 { .ping_add = TEST_INTVAL2, }, 33 { .ping_add = TEST_INTVAL3, }, 34 }; 35 36 static const struct dm_test_pdata test_pdata_manual = { 37 .ping_add = TEST_INTVAL_MANUAL, 38 }; 39 40 static const struct dm_test_pdata test_pdata_pre_reloc = { 41 .ping_add = TEST_INTVAL_PRE_RELOC, 42 }; 43 44 U_BOOT_DEVICE(dm_test_info1) = { 45 .name = "test_drv", 46 .platdata = &test_pdata[0], 47 }; 48 49 U_BOOT_DEVICE(dm_test_info2) = { 50 .name = "test_drv", 51 .platdata = &test_pdata[1], 52 }; 53 54 U_BOOT_DEVICE(dm_test_info3) = { 55 .name = "test_drv", 56 .platdata = &test_pdata[2], 57 }; 58 59 static struct driver_info driver_info_manual = { 60 .name = "test_manual_drv", 61 .platdata = &test_pdata_manual, 62 }; 63 64 static struct driver_info driver_info_pre_reloc = { 65 .name = "test_pre_reloc_drv", 66 .platdata = &test_pdata_pre_reloc, 67 }; 68 69 static struct driver_info driver_info_act_dma = { 70 .name = "test_act_dma_drv", 71 }; 72 73 void dm_leak_check_start(struct unit_test_state *uts) 74 { 75 uts->start = mallinfo(); 76 if (!uts->start.uordblks) 77 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n"); 78 } 79 80 int dm_leak_check_end(struct unit_test_state *uts) 81 { 82 struct mallinfo end; 83 int id, diff; 84 85 /* Don't delete the root class, since we started with that */ 86 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) { 87 struct uclass *uc; 88 89 uc = uclass_find(id); 90 if (!uc) 91 continue; 92 ut_assertok(uclass_destroy(uc)); 93 } 94 95 end = mallinfo(); 96 diff = end.uordblks - uts->start.uordblks; 97 if (diff > 0) 98 printf("Leak: lost %#xd bytes\n", diff); 99 else if (diff < 0) 100 printf("Leak: gained %#xd bytes\n", -diff); 101 ut_asserteq(uts->start.uordblks, end.uordblks); 102 103 return 0; 104 } 105 106 /* Test that binding with platdata occurs correctly */ 107 static int dm_test_autobind(struct unit_test_state *uts) 108 { 109 struct dm_test_state *dms = uts->priv; 110 struct udevice *dev; 111 112 /* 113 * We should have a single class (UCLASS_ROOT) and a single root 114 * device with no children. 115 */ 116 ut_assert(dms->root); 117 ut_asserteq(1, list_count_items(&gd->uclass_root)); 118 ut_asserteq(0, list_count_items(&gd->dm_root->child_head)); 119 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); 120 121 ut_assertok(dm_scan_platdata(false)); 122 123 /* We should have our test class now at least, plus more children */ 124 ut_assert(1 < list_count_items(&gd->uclass_root)); 125 ut_assert(0 < list_count_items(&gd->dm_root->child_head)); 126 127 /* Our 3 dm_test_infox children should be bound to the test uclass */ 128 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); 129 130 /* No devices should be probed */ 131 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node) 132 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); 133 134 /* Our test driver should have been bound 3 times */ 135 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3); 136 137 return 0; 138 } 139 DM_TEST(dm_test_autobind, 0); 140 141 /* Test that binding with uclass platdata allocation occurs correctly */ 142 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts) 143 { 144 struct dm_test_perdev_uc_pdata *uc_pdata; 145 struct udevice *dev; 146 struct uclass *uc; 147 148 ut_assertok(uclass_get(UCLASS_TEST, &uc)); 149 ut_assert(uc); 150 151 /** 152 * Test if test uclass driver requires allocation for the uclass 153 * platform data and then check the dev->uclass_platdata pointer. 154 */ 155 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size); 156 157 for (uclass_find_first_device(UCLASS_TEST, &dev); 158 dev; 159 uclass_find_next_device(&dev)) { 160 ut_assert(dev); 161 162 uc_pdata = dev_get_uclass_platdata(dev); 163 ut_assert(uc_pdata); 164 } 165 166 return 0; 167 } 168 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA); 169 170 /* Test that binding with uclass platdata setting occurs correctly */ 171 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts) 172 { 173 struct dm_test_perdev_uc_pdata *uc_pdata; 174 struct udevice *dev; 175 176 /** 177 * In the test_postbind() method of test uclass driver, the uclass 178 * platform data should be set to three test int values - test it. 179 */ 180 for (uclass_find_first_device(UCLASS_TEST, &dev); 181 dev; 182 uclass_find_next_device(&dev)) { 183 ut_assert(dev); 184 185 uc_pdata = dev_get_uclass_platdata(dev); 186 ut_assert(uc_pdata); 187 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1); 188 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2); 189 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3); 190 } 191 192 return 0; 193 } 194 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA); 195 196 /* Test that autoprobe finds all the expected devices */ 197 static int dm_test_autoprobe(struct unit_test_state *uts) 198 { 199 struct dm_test_state *dms = uts->priv; 200 int expected_base_add; 201 struct udevice *dev; 202 struct uclass *uc; 203 int i; 204 205 ut_assertok(uclass_get(UCLASS_TEST, &uc)); 206 ut_assert(uc); 207 208 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); 209 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); 210 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); 211 212 /* The root device should not be activated until needed */ 213 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED); 214 215 /* 216 * We should be able to find the three test devices, and they should 217 * all be activated as they are used (lazy activation, required by 218 * U-Boot) 219 */ 220 for (i = 0; i < 3; i++) { 221 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); 222 ut_assert(dev); 223 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED), 224 "Driver %d/%s already activated", i, dev->name); 225 226 /* This should activate it */ 227 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); 228 ut_assert(dev); 229 ut_assert(dev->flags & DM_FLAG_ACTIVATED); 230 231 /* Activating a device should activate the root device */ 232 if (!i) 233 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED); 234 } 235 236 /* 237 * Our 3 dm_test_info children should be passed to pre_probe and 238 * post_probe 239 */ 240 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); 241 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); 242 243 /* Also we can check the per-device data */ 244 expected_base_add = 0; 245 for (i = 0; i < 3; i++) { 246 struct dm_test_uclass_perdev_priv *priv; 247 struct dm_test_pdata *pdata; 248 249 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); 250 ut_assert(dev); 251 252 priv = dev_get_uclass_priv(dev); 253 ut_assert(priv); 254 ut_asserteq(expected_base_add, priv->base_add); 255 256 pdata = dev->platdata; 257 expected_base_add += pdata->ping_add; 258 } 259 260 return 0; 261 } 262 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA); 263 264 /* Check that we see the correct platdata in each device */ 265 static int dm_test_platdata(struct unit_test_state *uts) 266 { 267 const struct dm_test_pdata *pdata; 268 struct udevice *dev; 269 int i; 270 271 for (i = 0; i < 3; i++) { 272 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); 273 ut_assert(dev); 274 pdata = dev->platdata; 275 ut_assert(pdata->ping_add == test_pdata[i].ping_add); 276 } 277 278 return 0; 279 } 280 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA); 281 282 /* Test that we can bind, probe, remove, unbind a driver */ 283 static int dm_test_lifecycle(struct unit_test_state *uts) 284 { 285 struct dm_test_state *dms = uts->priv; 286 int op_count[DM_TEST_OP_COUNT]; 287 struct udevice *dev, *test_dev; 288 int pingret; 289 int ret; 290 291 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count)); 292 293 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, 294 &dev)); 295 ut_assert(dev); 296 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] 297 == op_count[DM_TEST_OP_BIND] + 1); 298 ut_assert(!dev->priv); 299 300 /* Probe the device - it should fail allocating private data */ 301 dms->force_fail_alloc = 1; 302 ret = device_probe(dev); 303 ut_assert(ret == -ENOMEM); 304 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] 305 == op_count[DM_TEST_OP_PROBE] + 1); 306 ut_assert(!dev->priv); 307 308 /* Try again without the alloc failure */ 309 dms->force_fail_alloc = 0; 310 ut_assertok(device_probe(dev)); 311 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] 312 == op_count[DM_TEST_OP_PROBE] + 2); 313 ut_assert(dev->priv); 314 315 /* This should be device 3 in the uclass */ 316 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); 317 ut_assert(dev == test_dev); 318 319 /* Try ping */ 320 ut_assertok(test_ping(dev, 100, &pingret)); 321 ut_assert(pingret == 102); 322 323 /* Now remove device 3 */ 324 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); 325 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); 326 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); 327 328 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); 329 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); 330 ut_assertok(device_unbind(dev)); 331 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); 332 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); 333 334 return 0; 335 } 336 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST); 337 338 /* Test that we can bind/unbind and the lists update correctly */ 339 static int dm_test_ordering(struct unit_test_state *uts) 340 { 341 struct dm_test_state *dms = uts->priv; 342 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev; 343 int pingret; 344 345 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, 346 &dev)); 347 ut_assert(dev); 348 349 /* Bind two new devices (numbers 4 and 5) */ 350 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, 351 &dev_penultimate)); 352 ut_assert(dev_penultimate); 353 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, 354 &dev_last)); 355 ut_assert(dev_last); 356 357 /* Now remove device 3 */ 358 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); 359 ut_assertok(device_unbind(dev)); 360 361 /* The device numbering should have shifted down one */ 362 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); 363 ut_assert(dev_penultimate == test_dev); 364 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev)); 365 ut_assert(dev_last == test_dev); 366 367 /* Add back the original device 3, now in position 5 */ 368 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, 369 &dev)); 370 ut_assert(dev); 371 372 /* Try ping */ 373 ut_assertok(test_ping(dev, 100, &pingret)); 374 ut_assert(pingret == 102); 375 376 /* Remove 3 and 4 */ 377 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL)); 378 ut_assertok(device_unbind(dev_penultimate)); 379 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL)); 380 ut_assertok(device_unbind(dev_last)); 381 382 /* Our device should now be in position 3 */ 383 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); 384 ut_assert(dev == test_dev); 385 386 /* Now remove device 3 */ 387 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); 388 ut_assertok(device_unbind(dev)); 389 390 return 0; 391 } 392 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA); 393 394 /* Check that we can perform operations on a device (do a ping) */ 395 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev, 396 uint32_t base, struct dm_test_priv *priv) 397 { 398 int expected; 399 int pingret; 400 401 /* Getting the child device should allocate platdata / priv */ 402 ut_assertok(testfdt_ping(dev, 10, &pingret)); 403 ut_assert(dev->priv); 404 ut_assert(dev->platdata); 405 406 expected = 10 + base; 407 ut_asserteq(expected, pingret); 408 409 /* Do another ping */ 410 ut_assertok(testfdt_ping(dev, 20, &pingret)); 411 expected = 20 + base; 412 ut_asserteq(expected, pingret); 413 414 /* Now check the ping_total */ 415 priv = dev->priv; 416 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2, 417 priv->ping_total); 418 419 return 0; 420 } 421 422 /* Check that we can perform operations on devices */ 423 static int dm_test_operations(struct unit_test_state *uts) 424 { 425 struct udevice *dev; 426 int i; 427 428 /* 429 * Now check that the ping adds are what we expect. This is using the 430 * ping-add property in each node. 431 */ 432 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) { 433 uint32_t base; 434 435 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); 436 437 /* 438 * Get the 'reg' property, which tells us what the ping add 439 * should be. We don't use the platdata because we want 440 * to test the code that sets that up (testfdt_drv_probe()). 441 */ 442 base = test_pdata[i].ping_add; 443 debug("dev=%d, base=%d\n", i, base); 444 445 ut_assert(!dm_check_operations(uts, dev, base, dev->priv)); 446 } 447 448 return 0; 449 } 450 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA); 451 452 /* Remove all drivers and check that things work */ 453 static int dm_test_remove(struct unit_test_state *uts) 454 { 455 struct udevice *dev; 456 int i; 457 458 for (i = 0; i < 3; i++) { 459 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); 460 ut_assert(dev); 461 ut_assertf(dev->flags & DM_FLAG_ACTIVATED, 462 "Driver %d/%s not activated", i, dev->name); 463 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); 464 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED), 465 "Driver %d/%s should have deactivated", i, 466 dev->name); 467 ut_assert(!dev->priv); 468 } 469 470 return 0; 471 } 472 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST); 473 474 /* Remove and recreate everything, check for memory leaks */ 475 static int dm_test_leak(struct unit_test_state *uts) 476 { 477 int i; 478 479 for (i = 0; i < 2; i++) { 480 struct udevice *dev; 481 int ret; 482 int id; 483 484 dm_leak_check_start(uts); 485 486 ut_assertok(dm_scan_platdata(false)); 487 ut_assertok(dm_scan_fdt(gd->fdt_blob, false)); 488 489 /* Scanning the uclass is enough to probe all the devices */ 490 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) { 491 for (ret = uclass_first_device(UCLASS_TEST, &dev); 492 dev; 493 ret = uclass_next_device(&dev)) 494 ; 495 ut_assertok(ret); 496 } 497 498 ut_assertok(dm_leak_check_end(uts)); 499 } 500 501 return 0; 502 } 503 DM_TEST(dm_test_leak, 0); 504 505 /* Test uclass init/destroy methods */ 506 static int dm_test_uclass(struct unit_test_state *uts) 507 { 508 struct uclass *uc; 509 510 ut_assertok(uclass_get(UCLASS_TEST, &uc)); 511 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); 512 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); 513 ut_assert(uc->priv); 514 515 ut_assertok(uclass_destroy(uc)); 516 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); 517 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); 518 519 return 0; 520 } 521 DM_TEST(dm_test_uclass, 0); 522 523 /** 524 * create_children() - Create children of a parent node 525 * 526 * @dms: Test system state 527 * @parent: Parent device 528 * @count: Number of children to create 529 * @key: Key value to put in first child. Subsequence children 530 * receive an incrementing value 531 * @child: If not NULL, then the child device pointers are written into 532 * this array. 533 * @return 0 if OK, -ve on error 534 */ 535 static int create_children(struct unit_test_state *uts, struct udevice *parent, 536 int count, int key, struct udevice *child[]) 537 { 538 struct udevice *dev; 539 int i; 540 541 for (i = 0; i < count; i++) { 542 struct dm_test_pdata *pdata; 543 544 ut_assertok(device_bind_by_name(parent, false, 545 &driver_info_manual, &dev)); 546 pdata = calloc(1, sizeof(*pdata)); 547 pdata->ping_add = key + i; 548 dev->platdata = pdata; 549 if (child) 550 child[i] = dev; 551 } 552 553 return 0; 554 } 555 556 #define NODE_COUNT 10 557 558 static int dm_test_children(struct unit_test_state *uts) 559 { 560 struct dm_test_state *dms = uts->priv; 561 struct udevice *top[NODE_COUNT]; 562 struct udevice *child[NODE_COUNT]; 563 struct udevice *grandchild[NODE_COUNT]; 564 struct udevice *dev; 565 int total; 566 int ret; 567 int i; 568 569 /* We don't care about the numbering for this test */ 570 dms->skip_post_probe = 1; 571 572 ut_assert(NODE_COUNT > 5); 573 574 /* First create 10 top-level children */ 575 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top)); 576 577 /* Now a few have their own children */ 578 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); 579 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); 580 581 /* And grandchildren */ 582 for (i = 0; i < NODE_COUNT; i++) 583 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, 584 i == 2 ? grandchild : NULL)); 585 586 /* Check total number of devices */ 587 total = NODE_COUNT * (3 + NODE_COUNT); 588 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); 589 590 /* Try probing one of the grandchildren */ 591 ut_assertok(uclass_get_device(UCLASS_TEST, 592 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev)); 593 ut_asserteq_ptr(grandchild[0], dev); 594 595 /* 596 * This should have probed the child and top node also, for a total 597 * of 3 nodes. 598 */ 599 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]); 600 601 /* Probe the other grandchildren */ 602 for (i = 1; i < NODE_COUNT; i++) 603 ut_assertok(device_probe(grandchild[i])); 604 605 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]); 606 607 /* Probe everything */ 608 for (ret = uclass_first_device(UCLASS_TEST, &dev); 609 dev; 610 ret = uclass_next_device(&dev)) 611 ; 612 ut_assertok(ret); 613 614 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]); 615 616 /* Remove a top-level child and check that the children are removed */ 617 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); 618 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]); 619 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0; 620 621 /* Try one with grandchildren */ 622 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); 623 ut_asserteq_ptr(dev, top[5]); 624 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); 625 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), 626 dm_testdrv_op_count[DM_TEST_OP_REMOVE]); 627 628 /* Try the same with unbind */ 629 ut_assertok(device_unbind(top[2])); 630 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); 631 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0; 632 633 /* Try one with grandchildren */ 634 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); 635 ut_asserteq_ptr(dev, top[6]); 636 ut_assertok(device_unbind(top[5])); 637 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), 638 dm_testdrv_op_count[DM_TEST_OP_UNBIND]); 639 640 return 0; 641 } 642 DM_TEST(dm_test_children, 0); 643 644 /* Test that pre-relocation devices work as expected */ 645 static int dm_test_pre_reloc(struct unit_test_state *uts) 646 { 647 struct dm_test_state *dms = uts->priv; 648 struct udevice *dev; 649 650 /* The normal driver should refuse to bind before relocation */ 651 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true, 652 &driver_info_manual, &dev)); 653 654 /* But this one is marked pre-reloc */ 655 ut_assertok(device_bind_by_name(dms->root, true, 656 &driver_info_pre_reloc, &dev)); 657 658 return 0; 659 } 660 DM_TEST(dm_test_pre_reloc, 0); 661 662 /* 663 * Test that removal of devices, either via the "normal" device_remove() 664 * API or via the device driver selective flag works as expected 665 */ 666 static int dm_test_remove_active_dma(struct unit_test_state *uts) 667 { 668 struct dm_test_state *dms = uts->priv; 669 struct udevice *dev; 670 671 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma, 672 &dev)); 673 ut_assert(dev); 674 675 /* Probe the device */ 676 ut_assertok(device_probe(dev)); 677 678 /* Test if device is active right now */ 679 ut_asserteq(true, device_active(dev)); 680 681 /* Remove the device via selective remove flag */ 682 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); 683 684 /* Test if device is inactive right now */ 685 ut_asserteq(false, device_active(dev)); 686 687 /* Probe the device again */ 688 ut_assertok(device_probe(dev)); 689 690 /* Test if device is active right now */ 691 ut_asserteq(true, device_active(dev)); 692 693 /* Remove the device via "normal" remove API */ 694 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); 695 696 /* Test if device is inactive right now */ 697 ut_asserteq(false, device_active(dev)); 698 699 /* 700 * Test if a device without the active DMA flags is not removed upon 701 * the active DMA remove call 702 */ 703 ut_assertok(device_unbind(dev)); 704 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, 705 &dev)); 706 ut_assert(dev); 707 708 /* Probe the device */ 709 ut_assertok(device_probe(dev)); 710 711 /* Test if device is active right now */ 712 ut_asserteq(true, device_active(dev)); 713 714 /* Remove the device via selective remove flag */ 715 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); 716 717 /* Test if device is still active right now */ 718 ut_asserteq(true, device_active(dev)); 719 720 return 0; 721 } 722 DM_TEST(dm_test_remove_active_dma, 0); 723 724 static int dm_test_uclass_before_ready(struct unit_test_state *uts) 725 { 726 struct uclass *uc; 727 728 ut_assertok(uclass_get(UCLASS_TEST, &uc)); 729 730 gd->dm_root = NULL; 731 gd->dm_root_f = NULL; 732 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); 733 734 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); 735 736 return 0; 737 } 738 DM_TEST(dm_test_uclass_before_ready, 0); 739 740 static int dm_test_uclass_devices_find(struct unit_test_state *uts) 741 { 742 struct udevice *dev; 743 int ret; 744 745 for (ret = uclass_find_first_device(UCLASS_TEST, &dev); 746 dev; 747 ret = uclass_find_next_device(&dev)) { 748 ut_assert(!ret); 749 ut_assert(dev); 750 } 751 752 ret = uclass_find_first_device(UCLASS_TEST_DUMMY, &dev); 753 ut_assert(ret == -ENODEV); 754 ut_assert(!dev); 755 756 return 0; 757 } 758 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA); 759 760 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts) 761 { 762 struct udevice *finddev; 763 struct udevice *testdev; 764 int findret, ret; 765 766 /* 767 * For each test device found in fdt like: "a-test", "b-test", etc., 768 * use its name and try to find it by uclass_find_device_by_name(). 769 * Then, on success check if: 770 * - current 'testdev' name is equal to the returned 'finddev' name 771 * - current 'testdev' pointer is equal to the returned 'finddev' 772 * 773 * We assume that, each uclass's device name is unique, so if not, then 774 * this will fail on checking condition: testdev == finddev, since the 775 * uclass_find_device_by_name(), returns the first device by given name. 776 */ 777 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev); 778 testdev; 779 ret = uclass_find_next_device(&testdev)) { 780 ut_assertok(ret); 781 ut_assert(testdev); 782 783 findret = uclass_find_device_by_name(UCLASS_TEST_FDT, 784 testdev->name, 785 &finddev); 786 787 ut_assertok(findret); 788 ut_assert(testdev); 789 ut_asserteq_str(testdev->name, finddev->name); 790 ut_asserteq_ptr(testdev, finddev); 791 } 792 793 return 0; 794 } 795 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT); 796 797 static int dm_test_uclass_devices_get(struct unit_test_state *uts) 798 { 799 struct udevice *dev; 800 int ret; 801 802 for (ret = uclass_first_device(UCLASS_TEST, &dev); 803 dev; 804 ret = uclass_next_device(&dev)) { 805 ut_assert(!ret); 806 ut_assert(dev); 807 ut_assert(device_active(dev)); 808 } 809 810 return 0; 811 } 812 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA); 813 814 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts) 815 { 816 struct udevice *finddev; 817 struct udevice *testdev; 818 int ret, findret; 819 820 /* 821 * For each test device found in fdt like: "a-test", "b-test", etc., 822 * use its name and try to get it by uclass_get_device_by_name(). 823 * On success check if: 824 * - returned finddev' is active 825 * - current 'testdev' name is equal to the returned 'finddev' name 826 * - current 'testdev' pointer is equal to the returned 'finddev' 827 * 828 * We asserts that the 'testdev' is active on each loop entry, so we 829 * could be sure that the 'finddev' is activated too, but for sure 830 * we check it again. 831 * 832 * We assume that, each uclass's device name is unique, so if not, then 833 * this will fail on checking condition: testdev == finddev, since the 834 * uclass_get_device_by_name(), returns the first device by given name. 835 */ 836 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev); 837 testdev; 838 ret = uclass_next_device(&testdev)) { 839 ut_assertok(ret); 840 ut_assert(testdev); 841 ut_assert(device_active(testdev)); 842 843 findret = uclass_get_device_by_name(UCLASS_TEST_FDT, 844 testdev->name, 845 &finddev); 846 847 ut_assertok(findret); 848 ut_assert(finddev); 849 ut_assert(device_active(finddev)); 850 ut_asserteq_str(testdev->name, finddev->name); 851 ut_asserteq_ptr(testdev, finddev); 852 } 853 854 return 0; 855 } 856 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT); 857 858 static int dm_test_device_get_uclass_id(struct unit_test_state *uts) 859 { 860 struct udevice *dev; 861 862 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev)); 863 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev)); 864 865 return 0; 866 } 867 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA); 868 869 static int dm_test_uclass_names(struct unit_test_state *uts) 870 { 871 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST)); 872 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test")); 873 874 return 0; 875 } 876 DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA); 877 878 static int dm_test_inactive_child(struct unit_test_state *uts) 879 { 880 struct dm_test_state *dms = uts->priv; 881 struct udevice *parent, *dev1, *dev2; 882 883 /* Skip the behaviour in test_post_probe() */ 884 dms->skip_post_probe = 1; 885 886 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent)); 887 888 /* 889 * Create a child but do not activate it. Calling the function again 890 * should return the same child. 891 */ 892 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, 893 UCLASS_TEST, &dev1)); 894 ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv), 895 "test_child", 0, ofnode_null(), &dev1)); 896 897 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST, 898 &dev2)); 899 ut_asserteq_ptr(dev1, dev2); 900 901 ut_assertok(device_probe(dev1)); 902 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, 903 UCLASS_TEST, &dev2)); 904 905 return 0; 906 } 907 DM_TEST(dm_test_inactive_child, DM_TESTF_SCAN_PDATA); 908