1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the core driver model code
5 * Copyright (c) 2013 Google, Inc
14 #include <asm/global_data.h>
15 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
20 #include <test/test.h>
23 DECLARE_GLOBAL_DATA_PTR;
29 TEST_INTVAL_MANUAL = 101112,
30 TEST_INTVAL_PRE_RELOC = 7,
33 static const struct dm_test_pdata test_pdata[] = {
34 { .ping_add = TEST_INTVAL1, },
35 { .ping_add = TEST_INTVAL2, },
36 { .ping_add = TEST_INTVAL3, },
39 static const struct dm_test_pdata test_pdata_manual = {
40 .ping_add = TEST_INTVAL_MANUAL,
43 static const struct dm_test_pdata test_pdata_pre_reloc = {
44 .ping_add = TEST_INTVAL_PRE_RELOC,
47 U_BOOT_DRVINFO(dm_test_info1) = {
49 .plat = &test_pdata[0],
52 U_BOOT_DRVINFO(dm_test_info2) = {
54 .plat = &test_pdata[1],
57 U_BOOT_DRVINFO(dm_test_info3) = {
59 .plat = &test_pdata[2],
62 static struct driver_info driver_info_manual = {
63 .name = "test_manual_drv",
64 .plat = &test_pdata_manual,
67 static struct driver_info driver_info_pre_reloc = {
68 .name = "test_pre_reloc_drv",
69 .plat = &test_pdata_pre_reloc,
72 static struct driver_info driver_info_act_dma = {
73 .name = "test_act_dma_drv",
76 static struct driver_info driver_info_vital_clk = {
77 .name = "test_vital_clk_drv",
80 static struct driver_info driver_info_act_dma_vital_clk = {
81 .name = "test_act_dma_vital_clk_drv",
84 void dm_leak_check_start(struct unit_test_state *uts)
86 uts->start = mallinfo();
87 if (!uts->start.uordblks)
88 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
91 int dm_leak_check_end(struct unit_test_state *uts)
96 /* Don't delete the root class, since we started with that */
97 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
100 uc = uclass_find(id);
103 ut_assertok(uclass_destroy(uc));
107 diff = end.uordblks - uts->start.uordblks;
109 printf("Leak: lost %#xd bytes\n", diff);
111 printf("Leak: gained %#xd bytes\n", -diff);
112 ut_asserteq(uts->start.uordblks, end.uordblks);
117 /* Test that binding with plat occurs correctly */
118 static int dm_test_autobind(struct unit_test_state *uts)
123 * We should have a single class (UCLASS_ROOT) and a single root
124 * device with no children.
126 ut_assert(uts->root);
127 ut_asserteq(1, list_count_items(gd->uclass_root));
128 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
129 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
131 ut_assertok(dm_scan_plat(false));
133 /* We should have our test class now at least, plus more children */
134 ut_assert(1 < list_count_items(gd->uclass_root));
135 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
137 /* Our 3 dm_test_infox children should be bound to the test uclass */
138 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
140 /* No devices should be probed */
141 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
142 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
144 /* Our test driver should have been bound 3 times */
145 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
149 DM_TEST(dm_test_autobind, 0);
151 /* Test that binding with uclass plat allocation occurs correctly */
152 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
154 struct dm_test_perdev_uc_pdata *uc_pdata;
158 ut_assertok(uclass_get(UCLASS_TEST, &uc));
162 * Test if test uclass driver requires allocation for the uclass
163 * platform data and then check the dev->uclass_plat pointer.
165 ut_assert(uc->uc_drv->per_device_plat_auto);
167 for (uclass_find_first_device(UCLASS_TEST, &dev);
169 uclass_find_next_device(&dev)) {
170 ut_assertnonnull(dev);
172 uc_pdata = dev_get_uclass_plat(dev);
178 DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA);
180 /* Test that binding with uclass plat setting occurs correctly */
181 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
183 struct dm_test_perdev_uc_pdata *uc_pdata;
187 * In the test_postbind() method of test uclass driver, the uclass
188 * platform data should be set to three test int values - test it.
190 for (uclass_find_first_device(UCLASS_TEST, &dev);
192 uclass_find_next_device(&dev)) {
193 ut_assertnonnull(dev);
195 uc_pdata = dev_get_uclass_plat(dev);
197 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
198 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
199 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
204 DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
206 /* Test that autoprobe finds all the expected devices */
207 static int dm_test_autoprobe(struct unit_test_state *uts)
209 int expected_base_add;
214 ut_assertok(uclass_get(UCLASS_TEST, &uc));
217 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
218 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
219 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
221 /* The root device should not be activated until needed */
222 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
225 * We should be able to find the three test devices, and they should
226 * all be activated as they are used (lazy activation, required by
229 for (i = 0; i < 3; i++) {
230 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
232 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
233 "Driver %d/%s already activated", i, dev->name);
235 /* This should activate it */
236 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
238 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
240 /* Activating a device should activate the root device */
242 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
246 * Our 3 dm_test_info children should be passed to pre_probe and
249 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
250 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
252 /* Also we can check the per-device data */
253 expected_base_add = 0;
254 for (i = 0; i < 3; i++) {
255 struct dm_test_uclass_perdev_priv *priv;
256 struct dm_test_pdata *pdata;
258 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
261 priv = dev_get_uclass_priv(dev);
263 ut_asserteq(expected_base_add, priv->base_add);
265 pdata = dev_get_plat(dev);
266 expected_base_add += pdata->ping_add;
271 DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
273 /* Check that we see the correct plat in each device */
274 static int dm_test_plat(struct unit_test_state *uts)
276 const struct dm_test_pdata *pdata;
280 for (i = 0; i < 3; i++) {
281 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
283 pdata = dev_get_plat(dev);
284 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
289 DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
291 /* Test that we can bind, probe, remove, unbind a driver */
292 static int dm_test_lifecycle(struct unit_test_state *uts)
294 int op_count[DM_TEST_OP_COUNT];
295 struct udevice *dev, *test_dev;
299 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
301 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
304 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
305 == op_count[DM_TEST_OP_BIND] + 1);
306 ut_assert(!dev_get_priv(dev));
308 /* Probe the device - it should fail allocating private data */
309 uts->force_fail_alloc = 1;
310 ret = device_probe(dev);
311 ut_assert(ret == -ENOMEM);
312 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
313 == op_count[DM_TEST_OP_PROBE] + 1);
314 ut_assert(!dev_get_priv(dev));
316 /* Try again without the alloc failure */
317 uts->force_fail_alloc = 0;
318 ut_assertok(device_probe(dev));
319 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
320 == op_count[DM_TEST_OP_PROBE] + 2);
321 ut_assert(dev_get_priv(dev));
323 /* This should be device 3 in the uclass */
324 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
325 ut_assert(dev == test_dev);
328 ut_assertok(test_ping(dev, 100, &pingret));
329 ut_assert(pingret == 102);
331 /* Now remove device 3 */
332 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
333 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
334 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
336 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
337 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
338 ut_assertok(device_unbind(dev));
339 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
340 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
344 DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
346 /* Test that we can bind/unbind and the lists update correctly */
347 static int dm_test_ordering(struct unit_test_state *uts)
349 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
352 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
356 /* Bind two new devices (numbers 4 and 5) */
357 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
359 ut_assert(dev_penultimate);
360 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
364 /* Now remove device 3 */
365 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
366 ut_assertok(device_unbind(dev));
368 /* The device numbering should have shifted down one */
369 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
370 ut_assert(dev_penultimate == test_dev);
371 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
372 ut_assert(dev_last == test_dev);
374 /* Add back the original device 3, now in position 5 */
375 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
380 ut_assertok(test_ping(dev, 100, &pingret));
381 ut_assert(pingret == 102);
384 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
385 ut_assertok(device_unbind(dev_penultimate));
386 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
387 ut_assertok(device_unbind(dev_last));
389 /* Our device should now be in position 3 */
390 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
391 ut_assert(dev == test_dev);
393 /* Now remove device 3 */
394 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
395 ut_assertok(device_unbind(dev));
399 DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
401 /* Check that we can perform operations on a device (do a ping) */
402 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
403 uint32_t base, struct dm_test_priv *priv)
408 /* Getting the child device should allocate plat / priv */
409 ut_assertok(testfdt_ping(dev, 10, &pingret));
410 ut_assert(dev_get_priv(dev));
411 ut_assert(dev_get_plat(dev));
413 expected = 10 + base;
414 ut_asserteq(expected, pingret);
416 /* Do another ping */
417 ut_assertok(testfdt_ping(dev, 20, &pingret));
418 expected = 20 + base;
419 ut_asserteq(expected, pingret);
421 /* Now check the ping_total */
422 priv = dev_get_priv(dev);
423 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
429 /* Check that we can perform operations on devices */
430 static int dm_test_operations(struct unit_test_state *uts)
436 * Now check that the ping adds are what we expect. This is using the
437 * ping-add property in each node.
439 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
442 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
445 * Get the 'reg' property, which tells us what the ping add
446 * should be. We don't use the plat because we want
447 * to test the code that sets that up (testfdt_drv_probe()).
449 base = test_pdata[i].ping_add;
450 debug("dev=%d, base=%d\n", i, base);
452 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
457 DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
459 /* Remove all drivers and check that things work */
460 static int dm_test_remove(struct unit_test_state *uts)
465 for (i = 0; i < 3; i++) {
466 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
468 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
469 "Driver %d/%s not activated", i, dev->name);
470 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
471 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
472 "Driver %d/%s should have deactivated", i,
474 ut_assert(!dev_get_priv(dev));
479 DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
481 /* Remove and recreate everything, check for memory leaks */
482 static int dm_test_leak(struct unit_test_state *uts)
486 for (i = 0; i < 2; i++) {
491 dm_leak_check_start(uts);
493 ut_assertok(dm_scan_plat(false));
494 ut_assertok(dm_scan_fdt(false));
496 /* Scanning the uclass is enough to probe all the devices */
497 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
498 for (ret = uclass_first_device(UCLASS_TEST, &dev);
500 ret = uclass_next_device(&dev))
505 ut_assertok(dm_leak_check_end(uts));
510 DM_TEST(dm_test_leak, 0);
512 /* Test uclass init/destroy methods */
513 static int dm_test_uclass(struct unit_test_state *uts)
517 ut_assertok(uclass_get(UCLASS_TEST, &uc));
518 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
519 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
520 ut_assert(uclass_get_priv(uc));
522 ut_assertok(uclass_destroy(uc));
523 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
524 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
528 DM_TEST(dm_test_uclass, 0);
531 * create_children() - Create children of a parent node
533 * @dms: Test system state
534 * @parent: Parent device
535 * @count: Number of children to create
536 * @key: Key value to put in first child. Subsequence children
537 * receive an incrementing value
538 * @child: If not NULL, then the child device pointers are written into
540 * @return 0 if OK, -ve on error
542 static int create_children(struct unit_test_state *uts, struct udevice *parent,
543 int count, int key, struct udevice *child[])
548 for (i = 0; i < count; i++) {
549 struct dm_test_pdata *pdata;
551 ut_assertok(device_bind_by_name(parent, false,
552 &driver_info_manual, &dev));
553 pdata = calloc(1, sizeof(*pdata));
554 pdata->ping_add = key + i;
555 dev_set_plat(dev, pdata);
563 #define NODE_COUNT 10
565 static int dm_test_children(struct unit_test_state *uts)
567 struct udevice *top[NODE_COUNT];
568 struct udevice *child[NODE_COUNT];
569 struct udevice *grandchild[NODE_COUNT];
575 /* We don't care about the numbering for this test */
576 uts->skip_post_probe = 1;
578 ut_assert(NODE_COUNT > 5);
580 /* First create 10 top-level children */
581 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
583 /* Now a few have their own children */
584 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
585 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
587 /* And grandchildren */
588 for (i = 0; i < NODE_COUNT; i++)
589 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
590 i == 2 ? grandchild : NULL));
592 /* Check total number of devices */
593 total = NODE_COUNT * (3 + NODE_COUNT);
594 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
596 /* Try probing one of the grandchildren */
597 ut_assertok(uclass_get_device(UCLASS_TEST,
598 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
599 ut_asserteq_ptr(grandchild[0], dev);
602 * This should have probed the child and top node also, for a total
605 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
607 /* Probe the other grandchildren */
608 for (i = 1; i < NODE_COUNT; i++)
609 ut_assertok(device_probe(grandchild[i]));
611 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
613 /* Probe everything */
614 for (ret = uclass_first_device(UCLASS_TEST, &dev);
616 ret = uclass_next_device(&dev))
620 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
622 /* Remove a top-level child and check that the children are removed */
623 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
624 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
625 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
627 /* Try one with grandchildren */
628 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
629 ut_asserteq_ptr(dev, top[5]);
630 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
631 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
632 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
634 /* Try the same with unbind */
635 ut_assertok(device_unbind(top[2]));
636 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
637 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
639 /* Try one with grandchildren */
640 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
641 ut_asserteq_ptr(dev, top[6]);
642 ut_assertok(device_unbind(top[5]));
643 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
644 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
648 DM_TEST(dm_test_children, 0);
650 static int dm_test_device_reparent(struct unit_test_state *uts)
652 struct udevice *top[NODE_COUNT];
653 struct udevice *child[NODE_COUNT];
654 struct udevice *grandchild[NODE_COUNT];
660 /* We don't care about the numbering for this test */
661 uts->skip_post_probe = 1;
663 ut_assert(NODE_COUNT > 5);
665 /* First create 10 top-level children */
666 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
668 /* Now a few have their own children */
669 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
670 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
672 /* And grandchildren */
673 for (i = 0; i < NODE_COUNT; i++)
674 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
675 i == 2 ? grandchild : NULL));
677 /* Check total number of devices */
678 total = NODE_COUNT * (3 + NODE_COUNT);
679 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
681 /* Probe everything */
682 for (i = 0; i < total; i++)
683 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
685 /* Re-parent top-level children with no grandchildren. */
686 ut_assertok(device_reparent(top[3], top[0]));
687 /* try to get devices */
688 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
690 ret = uclass_find_next_device(&dev)) {
692 ut_assertnonnull(dev);
695 ut_assertok(device_reparent(top[4], top[0]));
696 /* try to get devices */
697 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
699 ret = uclass_find_next_device(&dev)) {
701 ut_assertnonnull(dev);
704 /* Re-parent top-level children with grandchildren. */
705 ut_assertok(device_reparent(top[2], top[0]));
706 /* try to get devices */
707 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
709 ret = uclass_find_next_device(&dev)) {
711 ut_assertnonnull(dev);
714 ut_assertok(device_reparent(top[5], top[2]));
715 /* try to get devices */
716 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
718 ret = uclass_find_next_device(&dev)) {
720 ut_assertnonnull(dev);
723 /* Re-parent grandchildren. */
724 ut_assertok(device_reparent(grandchild[0], top[1]));
725 /* try to get devices */
726 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
728 ret = uclass_find_next_device(&dev)) {
730 ut_assertnonnull(dev);
733 ut_assertok(device_reparent(grandchild[1], top[1]));
734 /* try to get devices */
735 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
737 ret = uclass_find_next_device(&dev)) {
739 ut_assertnonnull(dev);
742 /* Remove re-pareneted devices. */
743 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
744 /* try to get devices */
745 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
747 ret = uclass_find_next_device(&dev)) {
749 ut_assertnonnull(dev);
752 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
753 /* try to get devices */
754 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
756 ret = uclass_find_next_device(&dev)) {
758 ut_assertnonnull(dev);
761 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
762 /* try to get devices */
763 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
765 ret = uclass_find_next_device(&dev)) {
767 ut_assertnonnull(dev);
770 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
771 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
773 ret = uclass_find_next_device(&dev)) {
775 ut_assertnonnull(dev);
778 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
779 /* try to get devices */
780 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
782 ret = uclass_find_next_device(&dev)) {
784 ut_assertnonnull(dev);
787 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
788 /* try to get devices */
789 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
791 ret = uclass_find_next_device(&dev)) {
793 ut_assertnonnull(dev);
796 /* Try the same with unbind */
797 ut_assertok(device_unbind(top[3]));
798 ut_assertok(device_unbind(top[4]));
799 ut_assertok(device_unbind(top[5]));
800 ut_assertok(device_unbind(top[2]));
802 ut_assertok(device_unbind(grandchild[0]));
803 ut_assertok(device_unbind(grandchild[1]));
807 DM_TEST(dm_test_device_reparent, 0);
809 /* Test that pre-relocation devices work as expected */
810 static int dm_test_pre_reloc(struct unit_test_state *uts)
814 /* The normal driver should refuse to bind before relocation */
815 ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
816 &driver_info_manual, &dev));
818 /* But this one is marked pre-reloc */
819 ut_assertok(device_bind_by_name(uts->root, true,
820 &driver_info_pre_reloc, &dev));
824 DM_TEST(dm_test_pre_reloc, 0);
827 * Test that removal of devices, either via the "normal" device_remove()
828 * API or via the device driver selective flag works as expected
830 static int dm_test_remove_active_dma(struct unit_test_state *uts)
834 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
838 /* Probe the device */
839 ut_assertok(device_probe(dev));
841 /* Test if device is active right now */
842 ut_asserteq(true, device_active(dev));
844 /* Remove the device via selective remove flag */
845 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
847 /* Test if device is inactive right now */
848 ut_asserteq(false, device_active(dev));
850 /* Probe the device again */
851 ut_assertok(device_probe(dev));
853 /* Test if device is active right now */
854 ut_asserteq(true, device_active(dev));
856 /* Remove the device via "normal" remove API */
857 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
859 /* Test if device is inactive right now */
860 ut_asserteq(false, device_active(dev));
863 * Test if a device without the active DMA flags is not removed upon
864 * the active DMA remove call
866 ut_assertok(device_unbind(dev));
867 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
871 /* Probe the device */
872 ut_assertok(device_probe(dev));
874 /* Test if device is active right now */
875 ut_asserteq(true, device_active(dev));
877 /* Remove the device via selective remove flag */
878 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
880 /* Test if device is still active right now */
881 ut_asserteq(true, device_active(dev));
885 DM_TEST(dm_test_remove_active_dma, 0);
887 /* Test removal of 'vital' devices */
888 static int dm_test_remove_vital(struct unit_test_state *uts)
890 struct udevice *normal, *dma, *vital, *dma_vital;
892 /* Skip the behaviour in test_post_probe() */
893 uts->skip_post_probe = 1;
895 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
897 ut_assertnonnull(normal);
899 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
901 ut_assertnonnull(dma);
903 ut_assertok(device_bind_by_name(uts->root, false,
904 &driver_info_vital_clk, &vital));
905 ut_assertnonnull(vital);
907 ut_assertok(device_bind_by_name(uts->root, false,
908 &driver_info_act_dma_vital_clk,
910 ut_assertnonnull(dma_vital);
912 /* Probe the devices */
913 ut_assertok(device_probe(normal));
914 ut_assertok(device_probe(dma));
915 ut_assertok(device_probe(vital));
916 ut_assertok(device_probe(dma_vital));
918 /* Check that devices are active right now */
919 ut_asserteq(true, device_active(normal));
920 ut_asserteq(true, device_active(dma));
921 ut_asserteq(true, device_active(vital));
922 ut_asserteq(true, device_active(dma_vital));
924 /* Remove active devices via selective remove flag */
925 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
928 * Check that this only has an effect on the dma device, since two
929 * devices are vital and the third does not have active DMA
931 ut_asserteq(true, device_active(normal));
932 ut_asserteq(false, device_active(dma));
933 ut_asserteq(true, device_active(vital));
934 ut_asserteq(true, device_active(dma_vital));
936 /* Remove active devices via selective remove flag */
937 ut_assertok(device_probe(dma));
938 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
940 /* This should have affected both active-dma devices */
941 ut_asserteq(true, device_active(normal));
942 ut_asserteq(false, device_active(dma));
943 ut_asserteq(true, device_active(vital));
944 ut_asserteq(false, device_active(dma_vital));
946 /* Remove non-vital devices */
947 ut_assertok(device_probe(dma));
948 ut_assertok(device_probe(dma_vital));
949 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
951 /* This should have affected only non-vital devices */
952 ut_asserteq(false, device_active(normal));
953 ut_asserteq(false, device_active(dma));
954 ut_asserteq(true, device_active(vital));
955 ut_asserteq(true, device_active(dma_vital));
957 /* Remove vital devices via normal remove flag */
958 ut_assertok(device_probe(normal));
959 ut_assertok(device_probe(dma));
960 dm_remove_devices_flags(DM_REMOVE_NORMAL);
962 /* Check that all devices are inactive right now */
963 ut_asserteq(false, device_active(normal));
964 ut_asserteq(false, device_active(dma));
965 ut_asserteq(false, device_active(vital));
966 ut_asserteq(false, device_active(dma_vital));
970 DM_TEST(dm_test_remove_vital, 0);
972 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
976 ut_assertok(uclass_get(UCLASS_TEST, &uc));
979 gd->dm_root_f = NULL;
980 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
982 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
986 DM_TEST(dm_test_uclass_before_ready, 0);
988 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
993 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
995 ret = uclass_find_next_device(&dev)) {
997 ut_assertnonnull(dev);
1000 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1005 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1007 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1009 struct udevice *finddev;
1010 struct udevice *testdev;
1014 * For each test device found in fdt like: "a-test", "b-test", etc.,
1015 * use its name and try to find it by uclass_find_device_by_name().
1016 * Then, on success check if:
1017 * - current 'testdev' name is equal to the returned 'finddev' name
1018 * - current 'testdev' pointer is equal to the returned 'finddev'
1020 * We assume that, each uclass's device name is unique, so if not, then
1021 * this will fail on checking condition: testdev == finddev, since the
1022 * uclass_find_device_by_name(), returns the first device by given name.
1024 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1026 ret = uclass_find_next_device(&testdev)) {
1028 ut_assertnonnull(testdev);
1030 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1034 ut_assertok(findret);
1036 ut_asserteq_str(testdev->name, finddev->name);
1037 ut_asserteq_ptr(testdev, finddev);
1042 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1044 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1046 struct udevice *dev;
1049 for (ret = uclass_first_device(UCLASS_TEST, &dev);
1051 ret = uclass_next_device(&dev)) {
1054 ut_assert(device_active(dev));
1059 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1061 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1063 struct udevice *finddev;
1064 struct udevice *testdev;
1068 * For each test device found in fdt like: "a-test", "b-test", etc.,
1069 * use its name and try to get it by uclass_get_device_by_name().
1070 * On success check if:
1071 * - returned finddev' is active
1072 * - current 'testdev' name is equal to the returned 'finddev' name
1073 * - current 'testdev' pointer is equal to the returned 'finddev'
1075 * We asserts that the 'testdev' is active on each loop entry, so we
1076 * could be sure that the 'finddev' is activated too, but for sure
1077 * we check it again.
1079 * We assume that, each uclass's device name is unique, so if not, then
1080 * this will fail on checking condition: testdev == finddev, since the
1081 * uclass_get_device_by_name(), returns the first device by given name.
1083 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
1085 ret = uclass_next_device(&testdev)) {
1088 ut_assert(device_active(testdev));
1090 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1094 ut_assertok(findret);
1096 ut_assert(device_active(finddev));
1097 ut_asserteq_str(testdev->name, finddev->name);
1098 ut_asserteq_ptr(testdev, finddev);
1103 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1105 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1107 struct udevice *dev;
1109 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1110 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1114 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1116 static int dm_test_uclass_names(struct unit_test_state *uts)
1118 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1119 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1123 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1125 static int dm_test_inactive_child(struct unit_test_state *uts)
1127 struct udevice *parent, *dev1, *dev2;
1129 /* Skip the behaviour in test_post_probe() */
1130 uts->skip_post_probe = 1;
1132 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1135 * Create a child but do not activate it. Calling the function again
1136 * should return the same child.
1138 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1139 UCLASS_TEST, &dev1));
1140 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1141 "test_child", 0, ofnode_null(), &dev1));
1143 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1145 ut_asserteq_ptr(dev1, dev2);
1147 ut_assertok(device_probe(dev1));
1148 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1149 UCLASS_TEST, &dev2));
1153 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1155 /* Make sure all bound devices have a sequence number */
1156 static int dm_test_all_have_seq(struct unit_test_state *uts)
1158 struct udevice *dev;
1161 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1162 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1163 if (dev->seq_ == -1)
1164 printf("Device '%s' has no seq (%d)\n",
1165 dev->name, dev->seq_);
1166 ut_assert(dev->seq_ != -1);
1172 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
1174 static int dm_test_dma_offset(struct unit_test_state *uts)
1176 struct udevice *dev;
1179 /* Make sure the bus's dma-ranges aren't taken into account here */
1180 node = ofnode_path("/mmio-bus@0");
1181 ut_assert(ofnode_valid(node));
1182 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1183 ut_asserteq_64(0, dev->dma_offset);
1185 /* Device behind a bus with dma-ranges */
1186 node = ofnode_path("/mmio-bus@0/subnode@0");
1187 ut_assert(ofnode_valid(node));
1188 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1189 ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1191 /* This one has no dma-ranges */
1192 node = ofnode_path("/mmio-bus@1");
1193 ut_assert(ofnode_valid(node));
1194 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1195 node = ofnode_path("/mmio-bus@1/subnode@0");
1196 ut_assert(ofnode_valid(node));
1197 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1198 ut_asserteq_64(0, dev->dma_offset);
1202 DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);