1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the core driver model code
5 * Copyright (c) 2013 Google, Inc
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)
119 struct dm_test_state *dms = uts->priv;
123 * We should have a single class (UCLASS_ROOT) and a single root
124 * device with no children.
126 ut_assert(dms->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 struct dm_test_state *dms = uts->priv;
210 int expected_base_add;
215 ut_assertok(uclass_get(UCLASS_TEST, &uc));
218 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
219 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
220 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
222 /* The root device should not be activated until needed */
223 ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
226 * We should be able to find the three test devices, and they should
227 * all be activated as they are used (lazy activation, required by
230 for (i = 0; i < 3; i++) {
231 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
233 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
234 "Driver %d/%s already activated", i, dev->name);
236 /* This should activate it */
237 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
239 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
241 /* Activating a device should activate the root device */
243 ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
247 * Our 3 dm_test_info children should be passed to pre_probe and
250 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
251 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
253 /* Also we can check the per-device data */
254 expected_base_add = 0;
255 for (i = 0; i < 3; i++) {
256 struct dm_test_uclass_perdev_priv *priv;
257 struct dm_test_pdata *pdata;
259 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
262 priv = dev_get_uclass_priv(dev);
264 ut_asserteq(expected_base_add, priv->base_add);
266 pdata = dev_get_plat(dev);
267 expected_base_add += pdata->ping_add;
272 DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
274 /* Check that we see the correct plat in each device */
275 static int dm_test_plat(struct unit_test_state *uts)
277 const struct dm_test_pdata *pdata;
281 for (i = 0; i < 3; i++) {
282 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
284 pdata = dev_get_plat(dev);
285 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
290 DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
292 /* Test that we can bind, probe, remove, unbind a driver */
293 static int dm_test_lifecycle(struct unit_test_state *uts)
295 struct dm_test_state *dms = uts->priv;
296 int op_count[DM_TEST_OP_COUNT];
297 struct udevice *dev, *test_dev;
301 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
303 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
306 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
307 == op_count[DM_TEST_OP_BIND] + 1);
308 ut_assert(!dev_get_priv(dev));
310 /* Probe the device - it should fail allocating private data */
311 dms->force_fail_alloc = 1;
312 ret = device_probe(dev);
313 ut_assert(ret == -ENOMEM);
314 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
315 == op_count[DM_TEST_OP_PROBE] + 1);
316 ut_assert(!dev_get_priv(dev));
318 /* Try again without the alloc failure */
319 dms->force_fail_alloc = 0;
320 ut_assertok(device_probe(dev));
321 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
322 == op_count[DM_TEST_OP_PROBE] + 2);
323 ut_assert(dev_get_priv(dev));
325 /* This should be device 3 in the uclass */
326 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
327 ut_assert(dev == test_dev);
330 ut_assertok(test_ping(dev, 100, &pingret));
331 ut_assert(pingret == 102);
333 /* Now remove device 3 */
334 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
335 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
336 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
338 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
339 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
340 ut_assertok(device_unbind(dev));
341 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
342 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
346 DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
348 /* Test that we can bind/unbind and the lists update correctly */
349 static int dm_test_ordering(struct unit_test_state *uts)
351 struct dm_test_state *dms = uts->priv;
352 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
355 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
359 /* Bind two new devices (numbers 4 and 5) */
360 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
362 ut_assert(dev_penultimate);
363 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
367 /* Now remove device 3 */
368 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
369 ut_assertok(device_unbind(dev));
371 /* The device numbering should have shifted down one */
372 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
373 ut_assert(dev_penultimate == test_dev);
374 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
375 ut_assert(dev_last == test_dev);
377 /* Add back the original device 3, now in position 5 */
378 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
383 ut_assertok(test_ping(dev, 100, &pingret));
384 ut_assert(pingret == 102);
387 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
388 ut_assertok(device_unbind(dev_penultimate));
389 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
390 ut_assertok(device_unbind(dev_last));
392 /* Our device should now be in position 3 */
393 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
394 ut_assert(dev == test_dev);
396 /* Now remove device 3 */
397 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
398 ut_assertok(device_unbind(dev));
402 DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
404 /* Check that we can perform operations on a device (do a ping) */
405 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
406 uint32_t base, struct dm_test_priv *priv)
411 /* Getting the child device should allocate plat / priv */
412 ut_assertok(testfdt_ping(dev, 10, &pingret));
413 ut_assert(dev_get_priv(dev));
414 ut_assert(dev_get_plat(dev));
416 expected = 10 + base;
417 ut_asserteq(expected, pingret);
419 /* Do another ping */
420 ut_assertok(testfdt_ping(dev, 20, &pingret));
421 expected = 20 + base;
422 ut_asserteq(expected, pingret);
424 /* Now check the ping_total */
425 priv = dev_get_priv(dev);
426 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
432 /* Check that we can perform operations on devices */
433 static int dm_test_operations(struct unit_test_state *uts)
439 * Now check that the ping adds are what we expect. This is using the
440 * ping-add property in each node.
442 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
445 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
448 * Get the 'reg' property, which tells us what the ping add
449 * should be. We don't use the plat because we want
450 * to test the code that sets that up (testfdt_drv_probe()).
452 base = test_pdata[i].ping_add;
453 debug("dev=%d, base=%d\n", i, base);
455 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
460 DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
462 /* Remove all drivers and check that things work */
463 static int dm_test_remove(struct unit_test_state *uts)
468 for (i = 0; i < 3; i++) {
469 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
471 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
472 "Driver %d/%s not activated", i, dev->name);
473 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
474 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
475 "Driver %d/%s should have deactivated", i,
477 ut_assert(!dev_get_priv(dev));
482 DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
484 /* Remove and recreate everything, check for memory leaks */
485 static int dm_test_leak(struct unit_test_state *uts)
489 for (i = 0; i < 2; i++) {
494 dm_leak_check_start(uts);
496 ut_assertok(dm_scan_plat(false));
497 ut_assertok(dm_scan_fdt(false));
499 /* Scanning the uclass is enough to probe all the devices */
500 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
501 for (ret = uclass_first_device(UCLASS_TEST, &dev);
503 ret = uclass_next_device(&dev))
508 ut_assertok(dm_leak_check_end(uts));
513 DM_TEST(dm_test_leak, 0);
515 /* Test uclass init/destroy methods */
516 static int dm_test_uclass(struct unit_test_state *uts)
520 ut_assertok(uclass_get(UCLASS_TEST, &uc));
521 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
522 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
523 ut_assert(uclass_get_priv(uc));
525 ut_assertok(uclass_destroy(uc));
526 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
527 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
531 DM_TEST(dm_test_uclass, 0);
534 * create_children() - Create children of a parent node
536 * @dms: Test system state
537 * @parent: Parent device
538 * @count: Number of children to create
539 * @key: Key value to put in first child. Subsequence children
540 * receive an incrementing value
541 * @child: If not NULL, then the child device pointers are written into
543 * @return 0 if OK, -ve on error
545 static int create_children(struct unit_test_state *uts, struct udevice *parent,
546 int count, int key, struct udevice *child[])
551 for (i = 0; i < count; i++) {
552 struct dm_test_pdata *pdata;
554 ut_assertok(device_bind_by_name(parent, false,
555 &driver_info_manual, &dev));
556 pdata = calloc(1, sizeof(*pdata));
557 pdata->ping_add = key + i;
558 dev_set_plat(dev, pdata);
566 #define NODE_COUNT 10
568 static int dm_test_children(struct unit_test_state *uts)
570 struct dm_test_state *dms = uts->priv;
571 struct udevice *top[NODE_COUNT];
572 struct udevice *child[NODE_COUNT];
573 struct udevice *grandchild[NODE_COUNT];
579 /* We don't care about the numbering for this test */
580 dms->skip_post_probe = 1;
582 ut_assert(NODE_COUNT > 5);
584 /* First create 10 top-level children */
585 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
587 /* Now a few have their own children */
588 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
589 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
591 /* And grandchildren */
592 for (i = 0; i < NODE_COUNT; i++)
593 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
594 i == 2 ? grandchild : NULL));
596 /* Check total number of devices */
597 total = NODE_COUNT * (3 + NODE_COUNT);
598 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
600 /* Try probing one of the grandchildren */
601 ut_assertok(uclass_get_device(UCLASS_TEST,
602 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
603 ut_asserteq_ptr(grandchild[0], dev);
606 * This should have probed the child and top node also, for a total
609 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
611 /* Probe the other grandchildren */
612 for (i = 1; i < NODE_COUNT; i++)
613 ut_assertok(device_probe(grandchild[i]));
615 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
617 /* Probe everything */
618 for (ret = uclass_first_device(UCLASS_TEST, &dev);
620 ret = uclass_next_device(&dev))
624 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
626 /* Remove a top-level child and check that the children are removed */
627 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
628 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
629 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
631 /* Try one with grandchildren */
632 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
633 ut_asserteq_ptr(dev, top[5]);
634 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
635 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
636 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
638 /* Try the same with unbind */
639 ut_assertok(device_unbind(top[2]));
640 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
641 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
643 /* Try one with grandchildren */
644 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
645 ut_asserteq_ptr(dev, top[6]);
646 ut_assertok(device_unbind(top[5]));
647 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
648 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
652 DM_TEST(dm_test_children, 0);
654 static int dm_test_device_reparent(struct unit_test_state *uts)
656 struct dm_test_state *dms = uts->priv;
657 struct udevice *top[NODE_COUNT];
658 struct udevice *child[NODE_COUNT];
659 struct udevice *grandchild[NODE_COUNT];
665 /* We don't care about the numbering for this test */
666 dms->skip_post_probe = 1;
668 ut_assert(NODE_COUNT > 5);
670 /* First create 10 top-level children */
671 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
673 /* Now a few have their own children */
674 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
675 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
677 /* And grandchildren */
678 for (i = 0; i < NODE_COUNT; i++)
679 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
680 i == 2 ? grandchild : NULL));
682 /* Check total number of devices */
683 total = NODE_COUNT * (3 + NODE_COUNT);
684 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
686 /* Probe everything */
687 for (i = 0; i < total; i++)
688 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
690 /* Re-parent top-level children with no grandchildren. */
691 ut_assertok(device_reparent(top[3], top[0]));
692 /* try to get devices */
693 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
695 ret = uclass_find_next_device(&dev)) {
697 ut_assertnonnull(dev);
700 ut_assertok(device_reparent(top[4], top[0]));
701 /* try to get devices */
702 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
704 ret = uclass_find_next_device(&dev)) {
706 ut_assertnonnull(dev);
709 /* Re-parent top-level children with grandchildren. */
710 ut_assertok(device_reparent(top[2], top[0]));
711 /* try to get devices */
712 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
714 ret = uclass_find_next_device(&dev)) {
716 ut_assertnonnull(dev);
719 ut_assertok(device_reparent(top[5], top[2]));
720 /* try to get devices */
721 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
723 ret = uclass_find_next_device(&dev)) {
725 ut_assertnonnull(dev);
728 /* Re-parent grandchildren. */
729 ut_assertok(device_reparent(grandchild[0], top[1]));
730 /* try to get devices */
731 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
733 ret = uclass_find_next_device(&dev)) {
735 ut_assertnonnull(dev);
738 ut_assertok(device_reparent(grandchild[1], top[1]));
739 /* try to get devices */
740 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
742 ret = uclass_find_next_device(&dev)) {
744 ut_assertnonnull(dev);
747 /* Remove re-pareneted devices. */
748 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
749 /* try to get devices */
750 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
752 ret = uclass_find_next_device(&dev)) {
754 ut_assertnonnull(dev);
757 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
758 /* try to get devices */
759 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
761 ret = uclass_find_next_device(&dev)) {
763 ut_assertnonnull(dev);
766 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
767 /* try to get devices */
768 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
770 ret = uclass_find_next_device(&dev)) {
772 ut_assertnonnull(dev);
775 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
776 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
778 ret = uclass_find_next_device(&dev)) {
780 ut_assertnonnull(dev);
783 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
784 /* try to get devices */
785 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
787 ret = uclass_find_next_device(&dev)) {
789 ut_assertnonnull(dev);
792 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
793 /* try to get devices */
794 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
796 ret = uclass_find_next_device(&dev)) {
798 ut_assertnonnull(dev);
801 /* Try the same with unbind */
802 ut_assertok(device_unbind(top[3]));
803 ut_assertok(device_unbind(top[4]));
804 ut_assertok(device_unbind(top[5]));
805 ut_assertok(device_unbind(top[2]));
807 ut_assertok(device_unbind(grandchild[0]));
808 ut_assertok(device_unbind(grandchild[1]));
812 DM_TEST(dm_test_device_reparent, 0);
814 /* Test that pre-relocation devices work as expected */
815 static int dm_test_pre_reloc(struct unit_test_state *uts)
817 struct dm_test_state *dms = uts->priv;
820 /* The normal driver should refuse to bind before relocation */
821 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
822 &driver_info_manual, &dev));
824 /* But this one is marked pre-reloc */
825 ut_assertok(device_bind_by_name(dms->root, true,
826 &driver_info_pre_reloc, &dev));
830 DM_TEST(dm_test_pre_reloc, 0);
833 * Test that removal of devices, either via the "normal" device_remove()
834 * API or via the device driver selective flag works as expected
836 static int dm_test_remove_active_dma(struct unit_test_state *uts)
838 struct dm_test_state *dms = uts->priv;
841 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
845 /* Probe the device */
846 ut_assertok(device_probe(dev));
848 /* Test if device is active right now */
849 ut_asserteq(true, device_active(dev));
851 /* Remove the device via selective remove flag */
852 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
854 /* Test if device is inactive right now */
855 ut_asserteq(false, device_active(dev));
857 /* Probe the device again */
858 ut_assertok(device_probe(dev));
860 /* Test if device is active right now */
861 ut_asserteq(true, device_active(dev));
863 /* Remove the device via "normal" remove API */
864 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
866 /* Test if device is inactive right now */
867 ut_asserteq(false, device_active(dev));
870 * Test if a device without the active DMA flags is not removed upon
871 * the active DMA remove call
873 ut_assertok(device_unbind(dev));
874 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
878 /* Probe the device */
879 ut_assertok(device_probe(dev));
881 /* Test if device is active right now */
882 ut_asserteq(true, device_active(dev));
884 /* Remove the device via selective remove flag */
885 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
887 /* Test if device is still active right now */
888 ut_asserteq(true, device_active(dev));
892 DM_TEST(dm_test_remove_active_dma, 0);
894 /* Test removal of 'vital' devices */
895 static int dm_test_remove_vital(struct unit_test_state *uts)
897 struct dm_test_state *dms = uts->priv;
898 struct udevice *normal, *dma, *vital, *dma_vital;
900 /* Skip the behaviour in test_post_probe() */
901 dms->skip_post_probe = 1;
903 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
905 ut_assertnonnull(normal);
907 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
909 ut_assertnonnull(dma);
911 ut_assertok(device_bind_by_name(dms->root, false,
912 &driver_info_vital_clk, &vital));
913 ut_assertnonnull(vital);
915 ut_assertok(device_bind_by_name(dms->root, false,
916 &driver_info_act_dma_vital_clk,
918 ut_assertnonnull(dma_vital);
920 /* Probe the devices */
921 ut_assertok(device_probe(normal));
922 ut_assertok(device_probe(dma));
923 ut_assertok(device_probe(vital));
924 ut_assertok(device_probe(dma_vital));
926 /* Check that devices are active right now */
927 ut_asserteq(true, device_active(normal));
928 ut_asserteq(true, device_active(dma));
929 ut_asserteq(true, device_active(vital));
930 ut_asserteq(true, device_active(dma_vital));
932 /* Remove active devices via selective remove flag */
933 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
936 * Check that this only has an effect on the dma device, since two
937 * devices are vital and the third does not have active DMA
939 ut_asserteq(true, device_active(normal));
940 ut_asserteq(false, device_active(dma));
941 ut_asserteq(true, device_active(vital));
942 ut_asserteq(true, device_active(dma_vital));
944 /* Remove active devices via selective remove flag */
945 ut_assertok(device_probe(dma));
946 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
948 /* This should have affected both active-dma devices */
949 ut_asserteq(true, device_active(normal));
950 ut_asserteq(false, device_active(dma));
951 ut_asserteq(true, device_active(vital));
952 ut_asserteq(false, device_active(dma_vital));
954 /* Remove non-vital devices */
955 ut_assertok(device_probe(dma));
956 ut_assertok(device_probe(dma_vital));
957 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
959 /* This should have affected only non-vital devices */
960 ut_asserteq(false, device_active(normal));
961 ut_asserteq(false, device_active(dma));
962 ut_asserteq(true, device_active(vital));
963 ut_asserteq(true, device_active(dma_vital));
965 /* Remove vital devices via normal remove flag */
966 ut_assertok(device_probe(normal));
967 ut_assertok(device_probe(dma));
968 dm_remove_devices_flags(DM_REMOVE_NORMAL);
970 /* Check that all devices are inactive right now */
971 ut_asserteq(false, device_active(normal));
972 ut_asserteq(false, device_active(dma));
973 ut_asserteq(false, device_active(vital));
974 ut_asserteq(false, device_active(dma_vital));
978 DM_TEST(dm_test_remove_vital, 0);
980 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
984 ut_assertok(uclass_get(UCLASS_TEST, &uc));
987 gd->dm_root_f = NULL;
988 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
990 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
994 DM_TEST(dm_test_uclass_before_ready, 0);
996 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
1001 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1003 ret = uclass_find_next_device(&dev)) {
1005 ut_assertnonnull(dev);
1008 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1013 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1015 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1017 struct udevice *finddev;
1018 struct udevice *testdev;
1022 * For each test device found in fdt like: "a-test", "b-test", etc.,
1023 * use its name and try to find it by uclass_find_device_by_name().
1024 * Then, on success check if:
1025 * - current 'testdev' name is equal to the returned 'finddev' name
1026 * - current 'testdev' pointer is equal to the returned 'finddev'
1028 * We assume that, each uclass's device name is unique, so if not, then
1029 * this will fail on checking condition: testdev == finddev, since the
1030 * uclass_find_device_by_name(), returns the first device by given name.
1032 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1034 ret = uclass_find_next_device(&testdev)) {
1036 ut_assertnonnull(testdev);
1038 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1042 ut_assertok(findret);
1044 ut_asserteq_str(testdev->name, finddev->name);
1045 ut_asserteq_ptr(testdev, finddev);
1050 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1052 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1054 struct udevice *dev;
1057 for (ret = uclass_first_device(UCLASS_TEST, &dev);
1059 ret = uclass_next_device(&dev)) {
1062 ut_assert(device_active(dev));
1067 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1069 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1071 struct udevice *finddev;
1072 struct udevice *testdev;
1076 * For each test device found in fdt like: "a-test", "b-test", etc.,
1077 * use its name and try to get it by uclass_get_device_by_name().
1078 * On success check if:
1079 * - returned finddev' is active
1080 * - current 'testdev' name is equal to the returned 'finddev' name
1081 * - current 'testdev' pointer is equal to the returned 'finddev'
1083 * We asserts that the 'testdev' is active on each loop entry, so we
1084 * could be sure that the 'finddev' is activated too, but for sure
1085 * we check it again.
1087 * We assume that, each uclass's device name is unique, so if not, then
1088 * this will fail on checking condition: testdev == finddev, since the
1089 * uclass_get_device_by_name(), returns the first device by given name.
1091 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
1093 ret = uclass_next_device(&testdev)) {
1096 ut_assert(device_active(testdev));
1098 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1102 ut_assertok(findret);
1104 ut_assert(device_active(finddev));
1105 ut_asserteq_str(testdev->name, finddev->name);
1106 ut_asserteq_ptr(testdev, finddev);
1111 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1113 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1115 struct udevice *dev;
1117 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1118 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1122 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1124 static int dm_test_uclass_names(struct unit_test_state *uts)
1126 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1127 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1131 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1133 static int dm_test_inactive_child(struct unit_test_state *uts)
1135 struct dm_test_state *dms = uts->priv;
1136 struct udevice *parent, *dev1, *dev2;
1138 /* Skip the behaviour in test_post_probe() */
1139 dms->skip_post_probe = 1;
1141 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1144 * Create a child but do not activate it. Calling the function again
1145 * should return the same child.
1147 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1148 UCLASS_TEST, &dev1));
1149 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1150 "test_child", 0, ofnode_null(), &dev1));
1152 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1154 ut_asserteq_ptr(dev1, dev2);
1156 ut_assertok(device_probe(dev1));
1157 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1158 UCLASS_TEST, &dev2));
1162 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1164 /* Make sure all bound devices have a sequence number */
1165 static int dm_test_all_have_seq(struct unit_test_state *uts)
1167 struct udevice *dev;
1170 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1171 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1172 if (dev->seq_ == -1)
1173 printf("Device '%s' has no seq (%d)\n",
1174 dev->name, dev->seq_);
1175 ut_assert(dev->seq_ != -1);
1181 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);