1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the core driver model code
5 * Copyright (c) 2013 Google, Inc
13 #include <asm/global_data.h>
14 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 #include <test/test.h>
22 DECLARE_GLOBAL_DATA_PTR;
28 TEST_INTVAL_MANUAL = 101112,
29 TEST_INTVAL_PRE_RELOC = 7,
32 static const struct dm_test_pdata test_pdata[] = {
33 { .ping_add = TEST_INTVAL1, },
34 { .ping_add = TEST_INTVAL2, },
35 { .ping_add = TEST_INTVAL3, },
38 static const struct dm_test_pdata test_pdata_manual = {
39 .ping_add = TEST_INTVAL_MANUAL,
42 static const struct dm_test_pdata test_pdata_pre_reloc = {
43 .ping_add = TEST_INTVAL_PRE_RELOC,
46 U_BOOT_DRVINFO(dm_test_info1) = {
48 .plat = &test_pdata[0],
51 U_BOOT_DRVINFO(dm_test_info2) = {
53 .plat = &test_pdata[1],
56 U_BOOT_DRVINFO(dm_test_info3) = {
58 .plat = &test_pdata[2],
61 static struct driver_info driver_info_manual = {
62 .name = "test_manual_drv",
63 .plat = &test_pdata_manual,
66 static struct driver_info driver_info_pre_reloc = {
67 .name = "test_pre_reloc_drv",
68 .plat = &test_pdata_pre_reloc,
71 static struct driver_info driver_info_act_dma = {
72 .name = "test_act_dma_drv",
75 static struct driver_info driver_info_vital_clk = {
76 .name = "test_vital_clk_drv",
79 static struct driver_info driver_info_act_dma_vital_clk = {
80 .name = "test_act_dma_vital_clk_drv",
83 void dm_leak_check_start(struct unit_test_state *uts)
85 uts->start = mallinfo();
86 if (!uts->start.uordblks)
87 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
90 int dm_leak_check_end(struct unit_test_state *uts)
95 /* Don't delete the root class, since we started with that */
96 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
102 ut_assertok(uclass_destroy(uc));
106 diff = end.uordblks - uts->start.uordblks;
108 printf("Leak: lost %#xd bytes\n", diff);
110 printf("Leak: gained %#xd bytes\n", -diff);
111 ut_asserteq(uts->start.uordblks, end.uordblks);
116 /* Test that binding with plat occurs correctly */
117 static int dm_test_autobind(struct unit_test_state *uts)
122 * We should have a single class (UCLASS_ROOT) and a single root
123 * device with no children.
125 ut_assert(uts->root);
126 ut_asserteq(1, list_count_items(gd->uclass_root));
127 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
128 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
130 ut_assertok(dm_scan_plat(false));
132 /* We should have our test class now at least, plus more children */
133 ut_assert(1 < list_count_items(gd->uclass_root));
134 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
136 /* Our 3 dm_test_infox children should be bound to the test uclass */
137 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
139 /* No devices should be probed */
140 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
141 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
143 /* Our test driver should have been bound 3 times */
144 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
148 DM_TEST(dm_test_autobind, 0);
150 /* Test that binding with uclass plat allocation occurs correctly */
151 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
153 struct dm_test_perdev_uc_pdata *uc_pdata;
157 ut_assertok(uclass_get(UCLASS_TEST, &uc));
161 * Test if test uclass driver requires allocation for the uclass
162 * platform data and then check the dev->uclass_plat pointer.
164 ut_assert(uc->uc_drv->per_device_plat_auto);
166 for (uclass_find_first_device(UCLASS_TEST, &dev);
168 uclass_find_next_device(&dev)) {
169 ut_assertnonnull(dev);
171 uc_pdata = dev_get_uclass_plat(dev);
177 DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA);
179 /* compare node names ignoring the unit address */
180 static int dm_test_compare_node_name(struct unit_test_state *uts)
184 node = ofnode_path("/mmio-bus@0");
185 ut_assert(ofnode_valid(node));
186 ut_assert(ofnode_name_eq(node, "mmio-bus"));
191 DM_TEST(dm_test_compare_node_name, UT_TESTF_SCAN_PDATA);
193 /* Test that binding with uclass plat setting occurs correctly */
194 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
196 struct dm_test_perdev_uc_pdata *uc_pdata;
200 * In the test_postbind() method of test uclass driver, the uclass
201 * platform data should be set to three test int values - test it.
203 for (uclass_find_first_device(UCLASS_TEST, &dev);
205 uclass_find_next_device(&dev)) {
206 ut_assertnonnull(dev);
208 uc_pdata = dev_get_uclass_plat(dev);
210 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
211 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
212 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
217 DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
219 /* Test that autoprobe finds all the expected devices */
220 static int dm_test_autoprobe(struct unit_test_state *uts)
222 int expected_base_add;
227 ut_assertok(uclass_get(UCLASS_TEST, &uc));
230 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
231 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
232 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
234 /* The root device should not be activated until needed */
235 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
238 * We should be able to find the three test devices, and they should
239 * all be activated as they are used (lazy activation, required by
242 for (i = 0; i < 3; i++) {
243 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
245 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
246 "Driver %d/%s already activated", i, dev->name);
248 /* This should activate it */
249 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
251 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
253 /* Activating a device should activate the root device */
255 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
259 * Our 3 dm_test_info children should be passed to pre_probe and
262 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
263 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
265 /* Also we can check the per-device data */
266 expected_base_add = 0;
267 for (i = 0; i < 3; i++) {
268 struct dm_test_uclass_perdev_priv *priv;
269 struct dm_test_pdata *pdata;
271 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
274 priv = dev_get_uclass_priv(dev);
276 ut_asserteq(expected_base_add, priv->base_add);
278 pdata = dev_get_plat(dev);
279 expected_base_add += pdata->ping_add;
284 DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
286 /* Check that we see the correct plat in each device */
287 static int dm_test_plat(struct unit_test_state *uts)
289 const struct dm_test_pdata *pdata;
293 for (i = 0; i < 3; i++) {
294 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
296 pdata = dev_get_plat(dev);
297 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
302 DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
304 /* Test that we can bind, probe, remove, unbind a driver */
305 static int dm_test_lifecycle(struct unit_test_state *uts)
307 int op_count[DM_TEST_OP_COUNT];
308 struct udevice *dev, *test_dev;
309 int start_dev_count, start_uc_count;
310 int dev_count, uc_count;
314 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
316 dm_get_stats(&start_dev_count, &start_uc_count);
318 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
321 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
322 == op_count[DM_TEST_OP_BIND] + 1);
323 ut_assert(!dev_get_priv(dev));
325 /* We should have one more device */
326 dm_get_stats(&dev_count, &uc_count);
327 ut_asserteq(start_dev_count + 1, dev_count);
328 ut_asserteq(start_uc_count, uc_count);
330 /* Probe the device - it should fail allocating private data */
331 uts->force_fail_alloc = 1;
332 ret = device_probe(dev);
333 ut_assert(ret == -ENOMEM);
334 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
335 == op_count[DM_TEST_OP_PROBE] + 1);
336 ut_assert(!dev_get_priv(dev));
338 /* Try again without the alloc failure */
339 uts->force_fail_alloc = 0;
340 ut_assertok(device_probe(dev));
341 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
342 == op_count[DM_TEST_OP_PROBE] + 2);
343 ut_assert(dev_get_priv(dev));
345 /* This should be device 3 in the uclass */
346 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
347 ut_assert(dev == test_dev);
350 ut_assertok(test_ping(dev, 100, &pingret));
351 ut_assert(pingret == 102);
353 /* Now remove device 3 */
354 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
355 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
356 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
358 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
359 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
360 ut_assertok(device_unbind(dev));
361 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
362 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
364 /* We should have one less device */
365 dm_get_stats(&dev_count, &uc_count);
366 ut_asserteq(start_dev_count, dev_count);
367 ut_asserteq(start_uc_count, uc_count);
371 DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
373 /* Test that we can bind/unbind and the lists update correctly */
374 static int dm_test_ordering(struct unit_test_state *uts)
376 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
379 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
383 /* Bind two new devices (numbers 4 and 5) */
384 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
386 ut_assert(dev_penultimate);
387 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
391 /* Now remove device 3 */
392 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
393 ut_assertok(device_unbind(dev));
395 /* The device numbering should have shifted down one */
396 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
397 ut_assert(dev_penultimate == test_dev);
398 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
399 ut_assert(dev_last == test_dev);
401 /* Add back the original device 3, now in position 5 */
402 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
407 ut_assertok(test_ping(dev, 100, &pingret));
408 ut_assert(pingret == 102);
411 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
412 ut_assertok(device_unbind(dev_penultimate));
413 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
414 ut_assertok(device_unbind(dev_last));
416 /* Our device should now be in position 3 */
417 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
418 ut_assert(dev == test_dev);
420 /* Now remove device 3 */
421 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
422 ut_assertok(device_unbind(dev));
426 DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
428 /* Check that we can perform operations on a device (do a ping) */
429 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
430 uint32_t base, struct dm_test_priv *priv)
435 /* Getting the child device should allocate plat / priv */
436 ut_assertok(testfdt_ping(dev, 10, &pingret));
437 ut_assert(dev_get_priv(dev));
438 ut_assert(dev_get_plat(dev));
440 expected = 10 + base;
441 ut_asserteq(expected, pingret);
443 /* Do another ping */
444 ut_assertok(testfdt_ping(dev, 20, &pingret));
445 expected = 20 + base;
446 ut_asserteq(expected, pingret);
448 /* Now check the ping_total */
449 priv = dev_get_priv(dev);
450 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
456 /* Check that we can perform operations on devices */
457 static int dm_test_operations(struct unit_test_state *uts)
463 * Now check that the ping adds are what we expect. This is using the
464 * ping-add property in each node.
466 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
469 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
472 * Get the 'reg' property, which tells us what the ping add
473 * should be. We don't use the plat because we want
474 * to test the code that sets that up (testfdt_drv_probe()).
476 base = test_pdata[i].ping_add;
477 debug("dev=%d, base=%d\n", i, base);
479 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
484 DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
486 /* Remove all drivers and check that things work */
487 static int dm_test_remove(struct unit_test_state *uts)
492 for (i = 0; i < 3; i++) {
493 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
495 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
496 "Driver %d/%s not activated", i, dev->name);
497 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
498 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
499 "Driver %d/%s should have deactivated", i,
501 ut_assert(!dev_get_priv(dev));
506 DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
508 /* Remove and recreate everything, check for memory leaks */
509 static int dm_test_leak(struct unit_test_state *uts)
513 for (i = 0; i < 2; i++) {
516 dm_leak_check_start(uts);
518 ut_assertok(dm_scan_plat(false));
519 ut_assertok(dm_scan_fdt(false));
521 ret = uclass_probe_all(UCLASS_TEST);
524 ut_assertok(dm_leak_check_end(uts));
529 DM_TEST(dm_test_leak, 0);
531 /* Test uclass init/destroy methods */
532 static int dm_test_uclass(struct unit_test_state *uts)
534 int dev_count, uc_count;
537 /* We should have just the root device and uclass */
538 dm_get_stats(&dev_count, &uc_count);
539 ut_asserteq(1, dev_count);
540 ut_asserteq(1, uc_count);
542 ut_assertok(uclass_get(UCLASS_TEST, &uc));
543 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
544 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
545 ut_assert(uclass_get_priv(uc));
547 dm_get_stats(&dev_count, &uc_count);
548 ut_asserteq(1, dev_count);
549 ut_asserteq(2, uc_count);
551 ut_assertok(uclass_destroy(uc));
552 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
553 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
555 dm_get_stats(&dev_count, &uc_count);
556 ut_asserteq(1, dev_count);
557 ut_asserteq(1, uc_count);
561 DM_TEST(dm_test_uclass, 0);
564 * create_children() - Create children of a parent node
566 * @dms: Test system state
567 * @parent: Parent device
568 * @count: Number of children to create
569 * @key: Key value to put in first child. Subsequence children
570 * receive an incrementing value
571 * @child: If not NULL, then the child device pointers are written into
573 * Return: 0 if OK, -ve on error
575 static int create_children(struct unit_test_state *uts, struct udevice *parent,
576 int count, int key, struct udevice *child[])
581 for (i = 0; i < count; i++) {
582 struct dm_test_pdata *pdata;
584 ut_assertok(device_bind_by_name(parent, false,
585 &driver_info_manual, &dev));
586 pdata = calloc(1, sizeof(*pdata));
587 pdata->ping_add = key + i;
588 dev_set_plat(dev, pdata);
596 #define NODE_COUNT 10
598 static int dm_test_children(struct unit_test_state *uts)
600 struct udevice *top[NODE_COUNT];
601 struct udevice *child[NODE_COUNT];
602 struct udevice *grandchild[NODE_COUNT];
608 /* We don't care about the numbering for this test */
609 uts->skip_post_probe = 1;
611 ut_assert(NODE_COUNT > 5);
613 /* First create 10 top-level children */
614 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
616 /* Now a few have their own children */
617 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
618 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
620 /* And grandchildren */
621 for (i = 0; i < NODE_COUNT; i++)
622 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
623 i == 2 ? grandchild : NULL));
625 /* Check total number of devices */
626 total = NODE_COUNT * (3 + NODE_COUNT);
627 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
629 /* Try probing one of the grandchildren */
630 ut_assertok(uclass_get_device(UCLASS_TEST,
631 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
632 ut_asserteq_ptr(grandchild[0], dev);
635 * This should have probed the child and top node also, for a total
638 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
640 /* Probe the other grandchildren */
641 for (i = 1; i < NODE_COUNT; i++)
642 ut_assertok(device_probe(grandchild[i]));
644 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
646 /* Probe everything */
647 ret = uclass_probe_all(UCLASS_TEST);
650 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
652 /* Remove a top-level child and check that the children are removed */
653 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
654 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
655 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
657 /* Try one with grandchildren */
658 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
659 ut_asserteq_ptr(dev, top[5]);
660 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
661 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
662 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
664 /* Try the same with unbind */
665 ut_assertok(device_unbind(top[2]));
666 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
667 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
669 /* Try one with grandchildren */
670 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
671 ut_asserteq_ptr(dev, top[6]);
672 ut_assertok(device_unbind(top[5]));
673 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
674 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
678 DM_TEST(dm_test_children, 0);
680 static int dm_test_device_reparent(struct unit_test_state *uts)
682 struct udevice *top[NODE_COUNT];
683 struct udevice *child[NODE_COUNT];
684 struct udevice *grandchild[NODE_COUNT];
690 /* We don't care about the numbering for this test */
691 uts->skip_post_probe = 1;
693 ut_assert(NODE_COUNT > 5);
695 /* First create 10 top-level children */
696 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
698 /* Now a few have their own children */
699 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
700 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
702 /* And grandchildren */
703 for (i = 0; i < NODE_COUNT; i++)
704 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
705 i == 2 ? grandchild : NULL));
707 /* Check total number of devices */
708 total = NODE_COUNT * (3 + NODE_COUNT);
709 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
711 /* Probe everything */
712 for (i = 0; i < total; i++)
713 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
715 /* Re-parent top-level children with no grandchildren. */
716 ut_assertok(device_reparent(top[3], top[0]));
717 /* try to get devices */
718 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
720 ret = uclass_find_next_device(&dev)) {
722 ut_assertnonnull(dev);
725 ut_assertok(device_reparent(top[4], top[0]));
726 /* try to get devices */
727 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
729 ret = uclass_find_next_device(&dev)) {
731 ut_assertnonnull(dev);
734 /* Re-parent top-level children with grandchildren. */
735 ut_assertok(device_reparent(top[2], top[0]));
736 /* try to get devices */
737 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
739 ret = uclass_find_next_device(&dev)) {
741 ut_assertnonnull(dev);
744 ut_assertok(device_reparent(top[5], top[2]));
745 /* try to get devices */
746 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
748 ret = uclass_find_next_device(&dev)) {
750 ut_assertnonnull(dev);
753 /* Re-parent grandchildren. */
754 ut_assertok(device_reparent(grandchild[0], top[1]));
755 /* try to get devices */
756 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
758 ret = uclass_find_next_device(&dev)) {
760 ut_assertnonnull(dev);
763 ut_assertok(device_reparent(grandchild[1], top[1]));
764 /* try to get devices */
765 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
767 ret = uclass_find_next_device(&dev)) {
769 ut_assertnonnull(dev);
772 /* Remove re-pareneted devices. */
773 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
774 /* try to get devices */
775 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
777 ret = uclass_find_next_device(&dev)) {
779 ut_assertnonnull(dev);
782 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
783 /* try to get devices */
784 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
786 ret = uclass_find_next_device(&dev)) {
788 ut_assertnonnull(dev);
791 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
792 /* try to get devices */
793 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
795 ret = uclass_find_next_device(&dev)) {
797 ut_assertnonnull(dev);
800 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
801 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
803 ret = uclass_find_next_device(&dev)) {
805 ut_assertnonnull(dev);
808 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
809 /* try to get devices */
810 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
812 ret = uclass_find_next_device(&dev)) {
814 ut_assertnonnull(dev);
817 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
818 /* try to get devices */
819 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
821 ret = uclass_find_next_device(&dev)) {
823 ut_assertnonnull(dev);
826 /* Try the same with unbind */
827 ut_assertok(device_unbind(top[3]));
828 ut_assertok(device_unbind(top[4]));
829 ut_assertok(device_unbind(top[5]));
830 ut_assertok(device_unbind(top[2]));
832 ut_assertok(device_unbind(grandchild[0]));
833 ut_assertok(device_unbind(grandchild[1]));
837 DM_TEST(dm_test_device_reparent, 0);
839 /* Test that pre-relocation devices work as expected */
840 static int dm_test_pre_reloc(struct unit_test_state *uts)
844 /* The normal driver should refuse to bind before relocation */
845 ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
846 &driver_info_manual, &dev));
848 /* But this one is marked pre-reloc */
849 ut_assertok(device_bind_by_name(uts->root, true,
850 &driver_info_pre_reloc, &dev));
854 DM_TEST(dm_test_pre_reloc, 0);
857 * Test that removal of devices, either via the "normal" device_remove()
858 * API or via the device driver selective flag works as expected
860 static int dm_test_remove_active_dma(struct unit_test_state *uts)
864 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
868 /* Probe the device */
869 ut_assertok(device_probe(dev));
871 /* Test if device is active right now */
872 ut_asserteq(true, device_active(dev));
874 /* Remove the device via selective remove flag */
875 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
877 /* Test if device is inactive right now */
878 ut_asserteq(false, device_active(dev));
880 /* Probe the device again */
881 ut_assertok(device_probe(dev));
883 /* Test if device is active right now */
884 ut_asserteq(true, device_active(dev));
886 /* Remove the device via "normal" remove API */
887 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
889 /* Test if device is inactive right now */
890 ut_asserteq(false, device_active(dev));
893 * Test if a device without the active DMA flags is not removed upon
894 * the active DMA remove call
896 ut_assertok(device_unbind(dev));
897 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
901 /* Probe the device */
902 ut_assertok(device_probe(dev));
904 /* Test if device is active right now */
905 ut_asserteq(true, device_active(dev));
907 /* Remove the device via selective remove flag */
908 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
910 /* Test if device is still active right now */
911 ut_asserteq(true, device_active(dev));
915 DM_TEST(dm_test_remove_active_dma, 0);
917 /* Test removal of 'vital' devices */
918 static int dm_test_remove_vital(struct unit_test_state *uts)
920 struct udevice *normal, *dma, *vital, *dma_vital;
922 /* Skip the behaviour in test_post_probe() */
923 uts->skip_post_probe = 1;
925 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
927 ut_assertnonnull(normal);
929 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
931 ut_assertnonnull(dma);
933 ut_assertok(device_bind_by_name(uts->root, false,
934 &driver_info_vital_clk, &vital));
935 ut_assertnonnull(vital);
937 ut_assertok(device_bind_by_name(uts->root, false,
938 &driver_info_act_dma_vital_clk,
940 ut_assertnonnull(dma_vital);
942 /* Probe the devices */
943 ut_assertok(device_probe(normal));
944 ut_assertok(device_probe(dma));
945 ut_assertok(device_probe(vital));
946 ut_assertok(device_probe(dma_vital));
948 /* Check that devices are active right now */
949 ut_asserteq(true, device_active(normal));
950 ut_asserteq(true, device_active(dma));
951 ut_asserteq(true, device_active(vital));
952 ut_asserteq(true, device_active(dma_vital));
954 /* Remove active devices via selective remove flag */
955 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
958 * Check that this only has an effect on the dma device, since two
959 * devices are vital and the third does not have active DMA
961 ut_asserteq(true, device_active(normal));
962 ut_asserteq(false, device_active(dma));
963 ut_asserteq(true, device_active(vital));
964 ut_asserteq(true, device_active(dma_vital));
966 /* Remove active devices via selective remove flag */
967 ut_assertok(device_probe(dma));
968 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
970 /* This should have affected both active-dma devices */
971 ut_asserteq(true, device_active(normal));
972 ut_asserteq(false, device_active(dma));
973 ut_asserteq(true, device_active(vital));
974 ut_asserteq(false, device_active(dma_vital));
976 /* Remove non-vital devices */
977 ut_assertok(device_probe(dma));
978 ut_assertok(device_probe(dma_vital));
979 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
981 /* This should have affected only non-vital devices */
982 ut_asserteq(false, device_active(normal));
983 ut_asserteq(false, device_active(dma));
984 ut_asserteq(true, device_active(vital));
985 ut_asserteq(true, device_active(dma_vital));
987 /* Remove vital devices via normal remove flag */
988 ut_assertok(device_probe(normal));
989 ut_assertok(device_probe(dma));
990 dm_remove_devices_flags(DM_REMOVE_NORMAL);
992 /* Check that all devices are inactive right now */
993 ut_asserteq(false, device_active(normal));
994 ut_asserteq(false, device_active(dma));
995 ut_asserteq(false, device_active(vital));
996 ut_asserteq(false, device_active(dma_vital));
1000 DM_TEST(dm_test_remove_vital, 0);
1002 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
1006 ut_assertok(uclass_get(UCLASS_TEST, &uc));
1009 gd->dm_root_f = NULL;
1010 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
1012 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
1013 ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc));
1017 DM_TEST(dm_test_uclass_before_ready, 0);
1019 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
1021 struct udevice *dev;
1024 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1026 ret = uclass_find_next_device(&dev)) {
1028 ut_assertnonnull(dev);
1031 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1036 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1038 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1040 struct udevice *finddev;
1041 struct udevice *testdev;
1045 * For each test device found in fdt like: "a-test", "b-test", etc.,
1046 * use its name and try to find it by uclass_find_device_by_name().
1047 * Then, on success check if:
1048 * - current 'testdev' name is equal to the returned 'finddev' name
1049 * - current 'testdev' pointer is equal to the returned 'finddev'
1051 * We assume that, each uclass's device name is unique, so if not, then
1052 * this will fail on checking condition: testdev == finddev, since the
1053 * uclass_find_device_by_name(), returns the first device by given name.
1055 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1057 ret = uclass_find_next_device(&testdev)) {
1059 ut_assertnonnull(testdev);
1061 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1065 ut_assertok(findret);
1067 ut_asserteq_str(testdev->name, finddev->name);
1068 ut_asserteq_ptr(testdev, finddev);
1073 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1075 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1077 struct udevice *dev;
1080 for (ret = uclass_first_device_check(UCLASS_TEST, &dev);
1082 ret = uclass_next_device_check(&dev)) {
1084 ut_assert(device_active(dev));
1089 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1091 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1093 struct udevice *finddev;
1094 struct udevice *testdev;
1098 * For each test device found in fdt like: "a-test", "b-test", etc.,
1099 * use its name and try to get it by uclass_get_device_by_name().
1100 * On success check if:
1101 * - returned finddev' is active
1102 * - current 'testdev' name is equal to the returned 'finddev' name
1103 * - current 'testdev' pointer is equal to the returned 'finddev'
1105 * We asserts that the 'testdev' is active on each loop entry, so we
1106 * could be sure that the 'finddev' is activated too, but for sure
1107 * we check it again.
1109 * We assume that, each uclass's device name is unique, so if not, then
1110 * this will fail on checking condition: testdev == finddev, since the
1111 * uclass_get_device_by_name(), returns the first device by given name.
1113 for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev);
1115 ret = uclass_next_device_check(&testdev)) {
1117 ut_assert(device_active(testdev));
1119 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1123 ut_assertok(findret);
1125 ut_assert(device_active(finddev));
1126 ut_asserteq_str(testdev->name, finddev->name);
1127 ut_asserteq_ptr(testdev, finddev);
1132 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1134 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1136 struct udevice *dev;
1138 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1139 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1143 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1145 static int dm_test_uclass_names(struct unit_test_state *uts)
1147 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1148 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1150 ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi"));
1154 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1156 static int dm_test_inactive_child(struct unit_test_state *uts)
1158 struct udevice *parent, *dev1, *dev2;
1160 /* Skip the behaviour in test_post_probe() */
1161 uts->skip_post_probe = 1;
1163 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1166 * Create a child but do not activate it. Calling the function again
1167 * should return the same child.
1169 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1170 UCLASS_TEST, &dev1));
1171 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1172 "test_child", 0, ofnode_null(), &dev1));
1174 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1176 ut_asserteq_ptr(dev1, dev2);
1178 ut_assertok(device_probe(dev1));
1179 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1180 UCLASS_TEST, &dev2));
1184 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1186 /* Make sure all bound devices have a sequence number */
1187 static int dm_test_all_have_seq(struct unit_test_state *uts)
1189 struct udevice *dev;
1192 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1193 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1194 if (dev->seq_ == -1)
1195 printf("Device '%s' has no seq (%d)\n",
1196 dev->name, dev->seq_);
1197 ut_assert(dev->seq_ != -1);
1203 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
1205 #if CONFIG_IS_ENABLED(DM_DMA)
1206 static int dm_test_dma_offset(struct unit_test_state *uts)
1208 struct udevice *dev;
1211 /* Make sure the bus's dma-ranges aren't taken into account here */
1212 node = ofnode_path("/mmio-bus@0");
1213 ut_assert(ofnode_valid(node));
1214 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1215 ut_asserteq_64(0, dev->dma_offset);
1217 /* Device behind a bus with dma-ranges */
1218 node = ofnode_path("/mmio-bus@0/subnode@0");
1219 ut_assert(ofnode_valid(node));
1220 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1221 ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1223 /* This one has no dma-ranges */
1224 node = ofnode_path("/mmio-bus@1");
1225 ut_assert(ofnode_valid(node));
1226 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1227 node = ofnode_path("/mmio-bus@1/subnode@0");
1228 ut_assert(ofnode_valid(node));
1229 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1230 ut_asserteq_64(0, dev->dma_offset);
1234 DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1237 /* Test dm_get_stats() */
1238 static int dm_test_get_stats(struct unit_test_state *uts)
1240 int dev_count, uc_count;
1242 dm_get_stats(&dev_count, &uc_count);
1243 ut_assert(dev_count > 50);
1244 ut_assert(uc_count > 30);
1248 DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT);
1250 /* Test uclass_find_device_by_name() */
1251 static int dm_test_uclass_find_device(struct unit_test_state *uts)
1253 struct udevice *dev;
1255 ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
1256 ut_asserteq(-ENODEV,
1257 uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
1258 ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
1263 DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT);
1265 /* Test getting information about tags attached to devices */
1266 static int dm_test_dev_get_attach(struct unit_test_state *uts)
1268 struct udevice *dev;
1270 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1271 ut_asserteq_str("a-test", dev->name);
1273 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1274 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1275 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1276 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1277 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1278 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1280 ut_asserteq(sizeof(struct dm_test_pdata),
1281 dev_get_attach_size(dev, DM_TAG_PLAT));
1282 ut_asserteq(sizeof(struct dm_test_priv),
1283 dev_get_attach_size(dev, DM_TAG_PRIV));
1284 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1285 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1286 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1287 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1291 DM_TEST(dm_test_dev_get_attach, UT_TESTF_SCAN_FDT);
1293 /* Test getting information about tags attached to bus devices */
1294 static int dm_test_dev_get_attach_bus(struct unit_test_state *uts)
1296 struct udevice *dev, *child;
1298 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev));
1299 ut_asserteq_str("some-bus", dev->name);
1301 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1302 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1303 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1304 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1305 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1306 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1308 ut_asserteq(sizeof(struct dm_test_pdata),
1309 dev_get_attach_size(dev, DM_TAG_PLAT));
1310 ut_asserteq(sizeof(struct dm_test_priv),
1311 dev_get_attach_size(dev, DM_TAG_PRIV));
1312 ut_asserteq(sizeof(struct dm_test_uclass_priv),
1313 dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1314 ut_asserteq(sizeof(struct dm_test_uclass_plat),
1315 dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1316 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1317 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1319 /* Now try the child of the bus */
1320 ut_assertok(device_first_child_err(dev, &child));
1321 ut_asserteq_str("c-test@5", child->name);
1323 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT));
1324 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV));
1325 ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV));
1326 ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT));
1327 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT));
1328 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV));
1330 ut_asserteq(sizeof(struct dm_test_pdata),
1331 dev_get_attach_size(child, DM_TAG_PLAT));
1332 ut_asserteq(sizeof(struct dm_test_priv),
1333 dev_get_attach_size(child, DM_TAG_PRIV));
1334 ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV));
1335 ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT));
1336 ut_asserteq(sizeof(struct dm_test_parent_plat),
1337 dev_get_attach_size(child, DM_TAG_PARENT_PLAT));
1338 ut_asserteq(sizeof(struct dm_test_parent_data),
1339 dev_get_attach_size(child, DM_TAG_PARENT_PRIV));
1343 DM_TEST(dm_test_dev_get_attach_bus, UT_TESTF_SCAN_FDT);
1345 /* Test getting information about tags attached to bus devices */
1346 static int dm_test_dev_get_mem(struct unit_test_state *uts)
1348 struct dm_stats stats;
1354 DM_TEST(dm_test_dev_get_mem, UT_TESTF_SCAN_FDT);