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 /* compare node names ignoring the unit address */
181 static int dm_test_compare_node_name(struct unit_test_state *uts)
185 node = ofnode_path("/mmio-bus@0");
186 ut_assert(ofnode_valid(node));
187 ut_assert(ofnode_name_eq(node, "mmio-bus"));
192 DM_TEST(dm_test_compare_node_name, UT_TESTF_SCAN_PDATA);
194 /* Test that binding with uclass plat setting occurs correctly */
195 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
197 struct dm_test_perdev_uc_pdata *uc_pdata;
201 * In the test_postbind() method of test uclass driver, the uclass
202 * platform data should be set to three test int values - test it.
204 for (uclass_find_first_device(UCLASS_TEST, &dev);
206 uclass_find_next_device(&dev)) {
207 ut_assertnonnull(dev);
209 uc_pdata = dev_get_uclass_plat(dev);
211 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
212 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
213 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
218 DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
220 /* Test that autoprobe finds all the expected devices */
221 static int dm_test_autoprobe(struct unit_test_state *uts)
223 int expected_base_add;
228 ut_assertok(uclass_get(UCLASS_TEST, &uc));
231 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
232 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
233 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
235 /* The root device should not be activated until needed */
236 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
239 * We should be able to find the three test devices, and they should
240 * all be activated as they are used (lazy activation, required by
243 for (i = 0; i < 3; i++) {
244 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
246 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
247 "Driver %d/%s already activated", i, dev->name);
249 /* This should activate it */
250 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
252 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
254 /* Activating a device should activate the root device */
256 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
260 * Our 3 dm_test_info children should be passed to pre_probe and
263 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
264 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
266 /* Also we can check the per-device data */
267 expected_base_add = 0;
268 for (i = 0; i < 3; i++) {
269 struct dm_test_uclass_perdev_priv *priv;
270 struct dm_test_pdata *pdata;
272 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
275 priv = dev_get_uclass_priv(dev);
277 ut_asserteq(expected_base_add, priv->base_add);
279 pdata = dev_get_plat(dev);
280 expected_base_add += pdata->ping_add;
285 DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
287 /* Check that we see the correct plat in each device */
288 static int dm_test_plat(struct unit_test_state *uts)
290 const struct dm_test_pdata *pdata;
294 for (i = 0; i < 3; i++) {
295 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
297 pdata = dev_get_plat(dev);
298 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
303 DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
305 /* Test that we can bind, probe, remove, unbind a driver */
306 static int dm_test_lifecycle(struct unit_test_state *uts)
308 int op_count[DM_TEST_OP_COUNT];
309 struct udevice *dev, *test_dev;
310 int start_dev_count, start_uc_count;
311 int dev_count, uc_count;
315 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
317 dm_get_stats(&start_dev_count, &start_uc_count);
319 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
322 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
323 == op_count[DM_TEST_OP_BIND] + 1);
324 ut_assert(!dev_get_priv(dev));
326 /* We should have one more device */
327 dm_get_stats(&dev_count, &uc_count);
328 ut_asserteq(start_dev_count + 1, dev_count);
329 ut_asserteq(start_uc_count, uc_count);
331 /* Probe the device - it should fail allocating private data */
332 uts->force_fail_alloc = 1;
333 ret = device_probe(dev);
334 ut_assert(ret == -ENOMEM);
335 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
336 == op_count[DM_TEST_OP_PROBE] + 1);
337 ut_assert(!dev_get_priv(dev));
339 /* Try again without the alloc failure */
340 uts->force_fail_alloc = 0;
341 ut_assertok(device_probe(dev));
342 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
343 == op_count[DM_TEST_OP_PROBE] + 2);
344 ut_assert(dev_get_priv(dev));
346 /* This should be device 3 in the uclass */
347 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
348 ut_assert(dev == test_dev);
351 ut_assertok(test_ping(dev, 100, &pingret));
352 ut_assert(pingret == 102);
354 /* Now remove device 3 */
355 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
356 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
357 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
359 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
360 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
361 ut_assertok(device_unbind(dev));
362 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
363 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
365 /* We should have one less device */
366 dm_get_stats(&dev_count, &uc_count);
367 ut_asserteq(start_dev_count, dev_count);
368 ut_asserteq(start_uc_count, uc_count);
372 DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
374 /* Test that we can bind/unbind and the lists update correctly */
375 static int dm_test_ordering(struct unit_test_state *uts)
377 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
380 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
384 /* Bind two new devices (numbers 4 and 5) */
385 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
387 ut_assert(dev_penultimate);
388 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
392 /* Now remove device 3 */
393 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
394 ut_assertok(device_unbind(dev));
396 /* The device numbering should have shifted down one */
397 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
398 ut_assert(dev_penultimate == test_dev);
399 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
400 ut_assert(dev_last == test_dev);
402 /* Add back the original device 3, now in position 5 */
403 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
408 ut_assertok(test_ping(dev, 100, &pingret));
409 ut_assert(pingret == 102);
412 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
413 ut_assertok(device_unbind(dev_penultimate));
414 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
415 ut_assertok(device_unbind(dev_last));
417 /* Our device should now be in position 3 */
418 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
419 ut_assert(dev == test_dev);
421 /* Now remove device 3 */
422 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
423 ut_assertok(device_unbind(dev));
427 DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
429 /* Check that we can perform operations on a device (do a ping) */
430 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
431 uint32_t base, struct dm_test_priv *priv)
436 /* Getting the child device should allocate plat / priv */
437 ut_assertok(testfdt_ping(dev, 10, &pingret));
438 ut_assert(dev_get_priv(dev));
439 ut_assert(dev_get_plat(dev));
441 expected = 10 + base;
442 ut_asserteq(expected, pingret);
444 /* Do another ping */
445 ut_assertok(testfdt_ping(dev, 20, &pingret));
446 expected = 20 + base;
447 ut_asserteq(expected, pingret);
449 /* Now check the ping_total */
450 priv = dev_get_priv(dev);
451 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
457 /* Check that we can perform operations on devices */
458 static int dm_test_operations(struct unit_test_state *uts)
464 * Now check that the ping adds are what we expect. This is using the
465 * ping-add property in each node.
467 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
470 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
473 * Get the 'reg' property, which tells us what the ping add
474 * should be. We don't use the plat because we want
475 * to test the code that sets that up (testfdt_drv_probe()).
477 base = test_pdata[i].ping_add;
478 debug("dev=%d, base=%d\n", i, base);
480 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
485 DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
487 /* Remove all drivers and check that things work */
488 static int dm_test_remove(struct unit_test_state *uts)
493 for (i = 0; i < 3; i++) {
494 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
496 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
497 "Driver %d/%s not activated", i, dev->name);
498 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
499 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
500 "Driver %d/%s should have deactivated", i,
502 ut_assert(!dev_get_priv(dev));
507 DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
509 /* Remove and recreate everything, check for memory leaks */
510 static int dm_test_leak(struct unit_test_state *uts)
514 for (i = 0; i < 2; i++) {
519 dm_leak_check_start(uts);
521 ut_assertok(dm_scan_plat(false));
522 ut_assertok(dm_scan_fdt(false));
524 /* Scanning the uclass is enough to probe all the devices */
525 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
526 for (ret = uclass_first_device(UCLASS_TEST, &dev);
528 ret = uclass_next_device(&dev))
533 ut_assertok(dm_leak_check_end(uts));
538 DM_TEST(dm_test_leak, 0);
540 /* Test uclass init/destroy methods */
541 static int dm_test_uclass(struct unit_test_state *uts)
543 int dev_count, uc_count;
546 /* We should have just the root device and uclass */
547 dm_get_stats(&dev_count, &uc_count);
548 ut_asserteq(1, dev_count);
549 ut_asserteq(1, uc_count);
551 ut_assertok(uclass_get(UCLASS_TEST, &uc));
552 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
553 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
554 ut_assert(uclass_get_priv(uc));
556 dm_get_stats(&dev_count, &uc_count);
557 ut_asserteq(1, dev_count);
558 ut_asserteq(2, uc_count);
560 ut_assertok(uclass_destroy(uc));
561 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
562 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
564 dm_get_stats(&dev_count, &uc_count);
565 ut_asserteq(1, dev_count);
566 ut_asserteq(1, uc_count);
570 DM_TEST(dm_test_uclass, 0);
573 * create_children() - Create children of a parent node
575 * @dms: Test system state
576 * @parent: Parent device
577 * @count: Number of children to create
578 * @key: Key value to put in first child. Subsequence children
579 * receive an incrementing value
580 * @child: If not NULL, then the child device pointers are written into
582 * Return: 0 if OK, -ve on error
584 static int create_children(struct unit_test_state *uts, struct udevice *parent,
585 int count, int key, struct udevice *child[])
590 for (i = 0; i < count; i++) {
591 struct dm_test_pdata *pdata;
593 ut_assertok(device_bind_by_name(parent, false,
594 &driver_info_manual, &dev));
595 pdata = calloc(1, sizeof(*pdata));
596 pdata->ping_add = key + i;
597 dev_set_plat(dev, pdata);
605 #define NODE_COUNT 10
607 static int dm_test_children(struct unit_test_state *uts)
609 struct udevice *top[NODE_COUNT];
610 struct udevice *child[NODE_COUNT];
611 struct udevice *grandchild[NODE_COUNT];
617 /* We don't care about the numbering for this test */
618 uts->skip_post_probe = 1;
620 ut_assert(NODE_COUNT > 5);
622 /* First create 10 top-level children */
623 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
625 /* Now a few have their own children */
626 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
627 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
629 /* And grandchildren */
630 for (i = 0; i < NODE_COUNT; i++)
631 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
632 i == 2 ? grandchild : NULL));
634 /* Check total number of devices */
635 total = NODE_COUNT * (3 + NODE_COUNT);
636 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
638 /* Try probing one of the grandchildren */
639 ut_assertok(uclass_get_device(UCLASS_TEST,
640 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
641 ut_asserteq_ptr(grandchild[0], dev);
644 * This should have probed the child and top node also, for a total
647 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
649 /* Probe the other grandchildren */
650 for (i = 1; i < NODE_COUNT; i++)
651 ut_assertok(device_probe(grandchild[i]));
653 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
655 /* Probe everything */
656 for (ret = uclass_first_device(UCLASS_TEST, &dev);
658 ret = uclass_next_device(&dev))
662 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
664 /* Remove a top-level child and check that the children are removed */
665 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
666 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
667 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
669 /* Try one with grandchildren */
670 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
671 ut_asserteq_ptr(dev, top[5]);
672 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
673 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
674 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
676 /* Try the same with unbind */
677 ut_assertok(device_unbind(top[2]));
678 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
679 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
681 /* Try one with grandchildren */
682 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
683 ut_asserteq_ptr(dev, top[6]);
684 ut_assertok(device_unbind(top[5]));
685 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
686 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
690 DM_TEST(dm_test_children, 0);
692 static int dm_test_device_reparent(struct unit_test_state *uts)
694 struct udevice *top[NODE_COUNT];
695 struct udevice *child[NODE_COUNT];
696 struct udevice *grandchild[NODE_COUNT];
702 /* We don't care about the numbering for this test */
703 uts->skip_post_probe = 1;
705 ut_assert(NODE_COUNT > 5);
707 /* First create 10 top-level children */
708 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
710 /* Now a few have their own children */
711 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
712 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
714 /* And grandchildren */
715 for (i = 0; i < NODE_COUNT; i++)
716 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
717 i == 2 ? grandchild : NULL));
719 /* Check total number of devices */
720 total = NODE_COUNT * (3 + NODE_COUNT);
721 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
723 /* Probe everything */
724 for (i = 0; i < total; i++)
725 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
727 /* Re-parent top-level children with no grandchildren. */
728 ut_assertok(device_reparent(top[3], top[0]));
729 /* try to get devices */
730 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
732 ret = uclass_find_next_device(&dev)) {
734 ut_assertnonnull(dev);
737 ut_assertok(device_reparent(top[4], top[0]));
738 /* try to get devices */
739 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
741 ret = uclass_find_next_device(&dev)) {
743 ut_assertnonnull(dev);
746 /* Re-parent top-level children with grandchildren. */
747 ut_assertok(device_reparent(top[2], top[0]));
748 /* try to get devices */
749 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
751 ret = uclass_find_next_device(&dev)) {
753 ut_assertnonnull(dev);
756 ut_assertok(device_reparent(top[5], top[2]));
757 /* try to get devices */
758 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
760 ret = uclass_find_next_device(&dev)) {
762 ut_assertnonnull(dev);
765 /* Re-parent grandchildren. */
766 ut_assertok(device_reparent(grandchild[0], top[1]));
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_reparent(grandchild[1], top[1]));
776 /* try to get devices */
777 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
779 ret = uclass_find_next_device(&dev)) {
781 ut_assertnonnull(dev);
784 /* Remove re-pareneted devices. */
785 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
786 /* try to get devices */
787 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
789 ret = uclass_find_next_device(&dev)) {
791 ut_assertnonnull(dev);
794 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
795 /* try to get devices */
796 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
798 ret = uclass_find_next_device(&dev)) {
800 ut_assertnonnull(dev);
803 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
804 /* try to get devices */
805 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
807 ret = uclass_find_next_device(&dev)) {
809 ut_assertnonnull(dev);
812 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
813 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
815 ret = uclass_find_next_device(&dev)) {
817 ut_assertnonnull(dev);
820 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
821 /* try to get devices */
822 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
824 ret = uclass_find_next_device(&dev)) {
826 ut_assertnonnull(dev);
829 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
830 /* try to get devices */
831 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
833 ret = uclass_find_next_device(&dev)) {
835 ut_assertnonnull(dev);
838 /* Try the same with unbind */
839 ut_assertok(device_unbind(top[3]));
840 ut_assertok(device_unbind(top[4]));
841 ut_assertok(device_unbind(top[5]));
842 ut_assertok(device_unbind(top[2]));
844 ut_assertok(device_unbind(grandchild[0]));
845 ut_assertok(device_unbind(grandchild[1]));
849 DM_TEST(dm_test_device_reparent, 0);
851 /* Test that pre-relocation devices work as expected */
852 static int dm_test_pre_reloc(struct unit_test_state *uts)
856 /* The normal driver should refuse to bind before relocation */
857 ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
858 &driver_info_manual, &dev));
860 /* But this one is marked pre-reloc */
861 ut_assertok(device_bind_by_name(uts->root, true,
862 &driver_info_pre_reloc, &dev));
866 DM_TEST(dm_test_pre_reloc, 0);
869 * Test that removal of devices, either via the "normal" device_remove()
870 * API or via the device driver selective flag works as expected
872 static int dm_test_remove_active_dma(struct unit_test_state *uts)
876 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
880 /* Probe the device */
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 selective remove flag */
887 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
889 /* Test if device is inactive right now */
890 ut_asserteq(false, device_active(dev));
892 /* Probe the device again */
893 ut_assertok(device_probe(dev));
895 /* Test if device is active right now */
896 ut_asserteq(true, device_active(dev));
898 /* Remove the device via "normal" remove API */
899 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
901 /* Test if device is inactive right now */
902 ut_asserteq(false, device_active(dev));
905 * Test if a device without the active DMA flags is not removed upon
906 * the active DMA remove call
908 ut_assertok(device_unbind(dev));
909 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
913 /* Probe the device */
914 ut_assertok(device_probe(dev));
916 /* Test if device is active right now */
917 ut_asserteq(true, device_active(dev));
919 /* Remove the device via selective remove flag */
920 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
922 /* Test if device is still active right now */
923 ut_asserteq(true, device_active(dev));
927 DM_TEST(dm_test_remove_active_dma, 0);
929 /* Test removal of 'vital' devices */
930 static int dm_test_remove_vital(struct unit_test_state *uts)
932 struct udevice *normal, *dma, *vital, *dma_vital;
934 /* Skip the behaviour in test_post_probe() */
935 uts->skip_post_probe = 1;
937 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
939 ut_assertnonnull(normal);
941 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
943 ut_assertnonnull(dma);
945 ut_assertok(device_bind_by_name(uts->root, false,
946 &driver_info_vital_clk, &vital));
947 ut_assertnonnull(vital);
949 ut_assertok(device_bind_by_name(uts->root, false,
950 &driver_info_act_dma_vital_clk,
952 ut_assertnonnull(dma_vital);
954 /* Probe the devices */
955 ut_assertok(device_probe(normal));
956 ut_assertok(device_probe(dma));
957 ut_assertok(device_probe(vital));
958 ut_assertok(device_probe(dma_vital));
960 /* Check that devices are active right now */
961 ut_asserteq(true, device_active(normal));
962 ut_asserteq(true, 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 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
970 * Check that this only has an effect on the dma device, since two
971 * devices are vital and the third does not have active DMA
973 ut_asserteq(true, device_active(normal));
974 ut_asserteq(false, device_active(dma));
975 ut_asserteq(true, device_active(vital));
976 ut_asserteq(true, device_active(dma_vital));
978 /* Remove active devices via selective remove flag */
979 ut_assertok(device_probe(dma));
980 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
982 /* This should have affected both active-dma devices */
983 ut_asserteq(true, device_active(normal));
984 ut_asserteq(false, device_active(dma));
985 ut_asserteq(true, device_active(vital));
986 ut_asserteq(false, device_active(dma_vital));
988 /* Remove non-vital devices */
989 ut_assertok(device_probe(dma));
990 ut_assertok(device_probe(dma_vital));
991 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
993 /* This should have affected only non-vital devices */
994 ut_asserteq(false, device_active(normal));
995 ut_asserteq(false, device_active(dma));
996 ut_asserteq(true, device_active(vital));
997 ut_asserteq(true, device_active(dma_vital));
999 /* Remove vital devices via normal remove flag */
1000 ut_assertok(device_probe(normal));
1001 ut_assertok(device_probe(dma));
1002 dm_remove_devices_flags(DM_REMOVE_NORMAL);
1004 /* Check that all devices are inactive right now */
1005 ut_asserteq(false, device_active(normal));
1006 ut_asserteq(false, device_active(dma));
1007 ut_asserteq(false, device_active(vital));
1008 ut_asserteq(false, device_active(dma_vital));
1012 DM_TEST(dm_test_remove_vital, 0);
1014 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
1018 ut_assertok(uclass_get(UCLASS_TEST, &uc));
1021 gd->dm_root_f = NULL;
1022 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
1024 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
1025 ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc));
1029 DM_TEST(dm_test_uclass_before_ready, 0);
1031 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
1033 struct udevice *dev;
1036 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1038 ret = uclass_find_next_device(&dev)) {
1040 ut_assertnonnull(dev);
1043 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1048 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1050 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1052 struct udevice *finddev;
1053 struct udevice *testdev;
1057 * For each test device found in fdt like: "a-test", "b-test", etc.,
1058 * use its name and try to find it by uclass_find_device_by_name().
1059 * Then, on success check if:
1060 * - current 'testdev' name is equal to the returned 'finddev' name
1061 * - current 'testdev' pointer is equal to the returned 'finddev'
1063 * We assume that, each uclass's device name is unique, so if not, then
1064 * this will fail on checking condition: testdev == finddev, since the
1065 * uclass_find_device_by_name(), returns the first device by given name.
1067 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1069 ret = uclass_find_next_device(&testdev)) {
1071 ut_assertnonnull(testdev);
1073 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1077 ut_assertok(findret);
1079 ut_asserteq_str(testdev->name, finddev->name);
1080 ut_asserteq_ptr(testdev, finddev);
1085 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1087 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1089 struct udevice *dev;
1092 for (ret = uclass_first_device(UCLASS_TEST, &dev);
1094 ret = uclass_next_device(&dev)) {
1097 ut_assert(device_active(dev));
1102 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1104 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1106 struct udevice *finddev;
1107 struct udevice *testdev;
1111 * For each test device found in fdt like: "a-test", "b-test", etc.,
1112 * use its name and try to get it by uclass_get_device_by_name().
1113 * On success check if:
1114 * - returned finddev' is active
1115 * - current 'testdev' name is equal to the returned 'finddev' name
1116 * - current 'testdev' pointer is equal to the returned 'finddev'
1118 * We asserts that the 'testdev' is active on each loop entry, so we
1119 * could be sure that the 'finddev' is activated too, but for sure
1120 * we check it again.
1122 * We assume that, each uclass's device name is unique, so if not, then
1123 * this will fail on checking condition: testdev == finddev, since the
1124 * uclass_get_device_by_name(), returns the first device by given name.
1126 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
1128 ret = uclass_next_device(&testdev)) {
1131 ut_assert(device_active(testdev));
1133 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1137 ut_assertok(findret);
1139 ut_assert(device_active(finddev));
1140 ut_asserteq_str(testdev->name, finddev->name);
1141 ut_asserteq_ptr(testdev, finddev);
1146 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1148 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1150 struct udevice *dev;
1152 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1153 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1157 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1159 static int dm_test_uclass_names(struct unit_test_state *uts)
1161 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1162 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1164 ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi"));
1168 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1170 static int dm_test_inactive_child(struct unit_test_state *uts)
1172 struct udevice *parent, *dev1, *dev2;
1174 /* Skip the behaviour in test_post_probe() */
1175 uts->skip_post_probe = 1;
1177 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1180 * Create a child but do not activate it. Calling the function again
1181 * should return the same child.
1183 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1184 UCLASS_TEST, &dev1));
1185 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1186 "test_child", 0, ofnode_null(), &dev1));
1188 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1190 ut_asserteq_ptr(dev1, dev2);
1192 ut_assertok(device_probe(dev1));
1193 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1194 UCLASS_TEST, &dev2));
1198 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1200 /* Make sure all bound devices have a sequence number */
1201 static int dm_test_all_have_seq(struct unit_test_state *uts)
1203 struct udevice *dev;
1206 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1207 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1208 if (dev->seq_ == -1)
1209 printf("Device '%s' has no seq (%d)\n",
1210 dev->name, dev->seq_);
1211 ut_assert(dev->seq_ != -1);
1217 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
1219 #if CONFIG_IS_ENABLED(DM_DMA)
1220 static int dm_test_dma_offset(struct unit_test_state *uts)
1222 struct udevice *dev;
1225 /* Make sure the bus's dma-ranges aren't taken into account here */
1226 node = ofnode_path("/mmio-bus@0");
1227 ut_assert(ofnode_valid(node));
1228 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1229 ut_asserteq_64(0, dev->dma_offset);
1231 /* Device behind a bus with dma-ranges */
1232 node = ofnode_path("/mmio-bus@0/subnode@0");
1233 ut_assert(ofnode_valid(node));
1234 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1235 ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1237 /* This one has no dma-ranges */
1238 node = ofnode_path("/mmio-bus@1");
1239 ut_assert(ofnode_valid(node));
1240 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1241 node = ofnode_path("/mmio-bus@1/subnode@0");
1242 ut_assert(ofnode_valid(node));
1243 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1244 ut_asserteq_64(0, dev->dma_offset);
1248 DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1251 /* Test dm_get_stats() */
1252 static int dm_test_get_stats(struct unit_test_state *uts)
1254 int dev_count, uc_count;
1256 dm_get_stats(&dev_count, &uc_count);
1257 ut_assert(dev_count > 50);
1258 ut_assert(uc_count > 30);
1262 DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT);
1264 /* Test uclass_find_device_by_name() */
1265 static int dm_test_uclass_find_device(struct unit_test_state *uts)
1267 struct udevice *dev;
1269 ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
1270 ut_asserteq(-ENODEV,
1271 uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
1272 ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
1277 DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT);
1279 /* Test getting information about tags attached to devices */
1280 static int dm_test_dev_get_attach(struct unit_test_state *uts)
1282 struct udevice *dev;
1284 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1285 ut_asserteq_str("a-test", dev->name);
1287 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1288 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1289 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1290 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1291 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1292 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1294 ut_asserteq(sizeof(struct dm_test_pdata),
1295 dev_get_attach_size(dev, DM_TAG_PLAT));
1296 ut_asserteq(sizeof(struct dm_test_priv),
1297 dev_get_attach_size(dev, DM_TAG_PRIV));
1298 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1299 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1300 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1301 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1305 DM_TEST(dm_test_dev_get_attach, UT_TESTF_SCAN_FDT);
1307 /* Test getting information about tags attached to bus devices */
1308 static int dm_test_dev_get_attach_bus(struct unit_test_state *uts)
1310 struct udevice *dev, *child;
1312 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev));
1313 ut_asserteq_str("some-bus", dev->name);
1315 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1316 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1317 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1318 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1319 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1320 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1322 ut_asserteq(sizeof(struct dm_test_pdata),
1323 dev_get_attach_size(dev, DM_TAG_PLAT));
1324 ut_asserteq(sizeof(struct dm_test_priv),
1325 dev_get_attach_size(dev, DM_TAG_PRIV));
1326 ut_asserteq(sizeof(struct dm_test_uclass_priv),
1327 dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1328 ut_asserteq(sizeof(struct dm_test_uclass_plat),
1329 dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1330 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1331 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1333 /* Now try the child of the bus */
1334 ut_assertok(device_first_child_err(dev, &child));
1335 ut_asserteq_str("c-test@5", child->name);
1337 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT));
1338 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV));
1339 ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV));
1340 ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT));
1341 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT));
1342 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV));
1344 ut_asserteq(sizeof(struct dm_test_pdata),
1345 dev_get_attach_size(child, DM_TAG_PLAT));
1346 ut_asserteq(sizeof(struct dm_test_priv),
1347 dev_get_attach_size(child, DM_TAG_PRIV));
1348 ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV));
1349 ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT));
1350 ut_asserteq(sizeof(struct dm_test_parent_plat),
1351 dev_get_attach_size(child, DM_TAG_PARENT_PLAT));
1352 ut_asserteq(sizeof(struct dm_test_parent_data),
1353 dev_get_attach_size(child, DM_TAG_PARENT_PRIV));
1357 DM_TEST(dm_test_dev_get_attach_bus, UT_TESTF_SCAN_FDT);
1359 /* Test getting information about tags attached to bus devices */
1360 static int dm_test_dev_get_mem(struct unit_test_state *uts)
1362 struct dm_stats stats;
1368 DM_TEST(dm_test_dev_get_mem, UT_TESTF_SCAN_FDT);