2 * Tests for the core driver model code
4 * Copyright (c) 2013 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
21 DECLARE_GLOBAL_DATA_PTR;
27 TEST_INTVAL_MANUAL = 101112,
28 TEST_INTVAL_PRE_RELOC = 7,
31 static const struct dm_test_pdata test_pdata[] = {
32 { .ping_add = TEST_INTVAL1, },
33 { .ping_add = TEST_INTVAL2, },
34 { .ping_add = TEST_INTVAL3, },
37 static const struct dm_test_pdata test_pdata_manual = {
38 .ping_add = TEST_INTVAL_MANUAL,
41 static const struct dm_test_pdata test_pdata_pre_reloc = {
42 .ping_add = TEST_INTVAL_PRE_RELOC,
45 U_BOOT_DEVICE(dm_test_info1) = {
47 .platdata = &test_pdata[0],
50 U_BOOT_DEVICE(dm_test_info2) = {
52 .platdata = &test_pdata[1],
55 U_BOOT_DEVICE(dm_test_info3) = {
57 .platdata = &test_pdata[2],
60 static struct driver_info driver_info_manual = {
61 .name = "test_manual_drv",
62 .platdata = &test_pdata_manual,
65 static struct driver_info driver_info_pre_reloc = {
66 .name = "test_pre_reloc_drv",
67 .platdata = &test_pdata_manual,
70 void dm_leak_check_start(struct dm_test_state *dms)
72 dms->start = mallinfo();
73 if (!dms->start.uordblks)
74 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
77 int dm_leak_check_end(struct dm_test_state *dms)
82 /* Don't delete the root class, since we started with that */
83 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
89 ut_assertok(uclass_destroy(uc));
93 ut_asserteq(dms->start.uordblks, end.uordblks);
98 /* Test that binding with platdata occurs correctly */
99 static int dm_test_autobind(struct dm_test_state *dms)
104 * We should have a single class (UCLASS_ROOT) and a single root
105 * device with no children.
107 ut_assert(dms->root);
108 ut_asserteq(1, list_count_items(&gd->uclass_root));
109 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
110 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
112 ut_assertok(dm_scan_platdata(false));
114 /* We should have our test class now at least, plus more children */
115 ut_assert(1 < list_count_items(&gd->uclass_root));
116 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
118 /* Our 3 dm_test_infox children should be bound to the test uclass */
119 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
121 /* No devices should be probed */
122 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
123 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
125 /* Our test driver should have been bound 3 times */
126 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
130 DM_TEST(dm_test_autobind, 0);
132 /* Test that binding with uclass platdata allocation occurs correctly */
133 static int dm_test_autobind_uclass_pdata_alloc(struct dm_test_state *dms)
135 struct dm_test_perdev_uc_pdata *uc_pdata;
139 ut_assertok(uclass_get(UCLASS_TEST, &uc));
143 * Test if test uclass driver requires allocation for the uclass
144 * platform data and then check the dev->uclass_platdata pointer.
146 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
148 for (uclass_find_first_device(UCLASS_TEST, &dev);
150 uclass_find_next_device(&dev)) {
153 uc_pdata = dev_get_uclass_platdata(dev);
159 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
161 /* Test that binding with uclass platdata setting occurs correctly */
162 static int dm_test_autobind_uclass_pdata_valid(struct dm_test_state *dms)
164 struct dm_test_perdev_uc_pdata *uc_pdata;
168 * In the test_postbind() method of test uclass driver, the uclass
169 * platform data should be set to three test int values - test it.
171 for (uclass_find_first_device(UCLASS_TEST, &dev);
173 uclass_find_next_device(&dev)) {
176 uc_pdata = dev_get_uclass_platdata(dev);
178 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
179 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
180 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
185 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
187 /* Test that autoprobe finds all the expected devices */
188 static int dm_test_autoprobe(struct dm_test_state *dms)
190 int expected_base_add;
195 ut_assertok(uclass_get(UCLASS_TEST, &uc));
198 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
199 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
200 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
202 /* The root device should not be activated until needed */
203 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
206 * We should be able to find the three test devices, and they should
207 * all be activated as they are used (lazy activation, required by
210 for (i = 0; i < 3; i++) {
211 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
213 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
214 "Driver %d/%s already activated", i, dev->name);
216 /* This should activate it */
217 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
219 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
221 /* Activating a device should activate the root device */
223 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
227 * Our 3 dm_test_info children should be passed to pre_probe and
230 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
231 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
233 /* Also we can check the per-device data */
234 expected_base_add = 0;
235 for (i = 0; i < 3; i++) {
236 struct dm_test_uclass_perdev_priv *priv;
237 struct dm_test_pdata *pdata;
239 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
242 priv = dev_get_uclass_priv(dev);
244 ut_asserteq(expected_base_add, priv->base_add);
246 pdata = dev->platdata;
247 expected_base_add += pdata->ping_add;
252 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
254 /* Check that we see the correct platdata in each device */
255 static int dm_test_platdata(struct dm_test_state *dms)
257 const struct dm_test_pdata *pdata;
261 for (i = 0; i < 3; i++) {
262 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
264 pdata = dev->platdata;
265 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
270 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
272 /* Test that we can bind, probe, remove, unbind a driver */
273 static int dm_test_lifecycle(struct dm_test_state *dms)
275 int op_count[DM_TEST_OP_COUNT];
276 struct udevice *dev, *test_dev;
280 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
282 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
285 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
286 == op_count[DM_TEST_OP_BIND] + 1);
287 ut_assert(!dev->priv);
289 /* Probe the device - it should fail allocating private data */
290 dms->force_fail_alloc = 1;
291 ret = device_probe(dev);
292 ut_assert(ret == -ENOMEM);
293 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
294 == op_count[DM_TEST_OP_PROBE] + 1);
295 ut_assert(!dev->priv);
297 /* Try again without the alloc failure */
298 dms->force_fail_alloc = 0;
299 ut_assertok(device_probe(dev));
300 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
301 == op_count[DM_TEST_OP_PROBE] + 2);
302 ut_assert(dev->priv);
304 /* This should be device 3 in the uclass */
305 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
306 ut_assert(dev == test_dev);
309 ut_assertok(test_ping(dev, 100, &pingret));
310 ut_assert(pingret == 102);
312 /* Now remove device 3 */
313 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
314 ut_assertok(device_remove(dev));
315 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
317 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
318 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
319 ut_assertok(device_unbind(dev));
320 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
321 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
325 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
327 /* Test that we can bind/unbind and the lists update correctly */
328 static int dm_test_ordering(struct dm_test_state *dms)
330 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
333 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
337 /* Bind two new devices (numbers 4 and 5) */
338 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
340 ut_assert(dev_penultimate);
341 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
345 /* Now remove device 3 */
346 ut_assertok(device_remove(dev));
347 ut_assertok(device_unbind(dev));
349 /* The device numbering should have shifted down one */
350 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
351 ut_assert(dev_penultimate == test_dev);
352 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
353 ut_assert(dev_last == test_dev);
355 /* Add back the original device 3, now in position 5 */
356 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
361 ut_assertok(test_ping(dev, 100, &pingret));
362 ut_assert(pingret == 102);
365 ut_assertok(device_remove(dev_penultimate));
366 ut_assertok(device_unbind(dev_penultimate));
367 ut_assertok(device_remove(dev_last));
368 ut_assertok(device_unbind(dev_last));
370 /* Our device should now be in position 3 */
371 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
372 ut_assert(dev == test_dev);
374 /* Now remove device 3 */
375 ut_assertok(device_remove(dev));
376 ut_assertok(device_unbind(dev));
380 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
382 /* Check that we can perform operations on a device (do a ping) */
383 int dm_check_operations(struct dm_test_state *dms, struct udevice *dev,
384 uint32_t base, struct dm_test_priv *priv)
389 /* Getting the child device should allocate platdata / priv */
390 ut_assertok(testfdt_ping(dev, 10, &pingret));
391 ut_assert(dev->priv);
392 ut_assert(dev->platdata);
394 expected = 10 + base;
395 ut_asserteq(expected, pingret);
397 /* Do another ping */
398 ut_assertok(testfdt_ping(dev, 20, &pingret));
399 expected = 20 + base;
400 ut_asserteq(expected, pingret);
402 /* Now check the ping_total */
404 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
410 /* Check that we can perform operations on devices */
411 static int dm_test_operations(struct dm_test_state *dms)
417 * Now check that the ping adds are what we expect. This is using the
418 * ping-add property in each node.
420 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
423 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
426 * Get the 'reg' property, which tells us what the ping add
427 * should be. We don't use the platdata because we want
428 * to test the code that sets that up (testfdt_drv_probe()).
430 base = test_pdata[i].ping_add;
431 debug("dev=%d, base=%d\n", i, base);
433 ut_assert(!dm_check_operations(dms, dev, base, dev->priv));
438 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
440 /* Remove all drivers and check that things work */
441 static int dm_test_remove(struct dm_test_state *dms)
446 for (i = 0; i < 3; i++) {
447 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
449 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
450 "Driver %d/%s not activated", i, dev->name);
451 ut_assertok(device_remove(dev));
452 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
453 "Driver %d/%s should have deactivated", i,
455 ut_assert(!dev->priv);
460 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
462 /* Remove and recreate everything, check for memory leaks */
463 static int dm_test_leak(struct dm_test_state *dms)
467 for (i = 0; i < 2; i++) {
472 dm_leak_check_start(dms);
474 ut_assertok(dm_scan_platdata(false));
475 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
477 /* Scanning the uclass is enough to probe all the devices */
478 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
479 for (ret = uclass_first_device(UCLASS_TEST, &dev);
481 ret = uclass_next_device(&dev))
486 ut_assertok(dm_leak_check_end(dms));
491 DM_TEST(dm_test_leak, 0);
493 /* Test uclass init/destroy methods */
494 static int dm_test_uclass(struct dm_test_state *dms)
498 ut_assertok(uclass_get(UCLASS_TEST, &uc));
499 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
500 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
503 ut_assertok(uclass_destroy(uc));
504 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
505 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
509 DM_TEST(dm_test_uclass, 0);
512 * create_children() - Create children of a parent node
514 * @dms: Test system state
515 * @parent: Parent device
516 * @count: Number of children to create
517 * @key: Key value to put in first child. Subsequence children
518 * receive an incrementing value
519 * @child: If not NULL, then the child device pointers are written into
521 * @return 0 if OK, -ve on error
523 static int create_children(struct dm_test_state *dms, struct udevice *parent,
524 int count, int key, struct udevice *child[])
529 for (i = 0; i < count; i++) {
530 struct dm_test_pdata *pdata;
532 ut_assertok(device_bind_by_name(parent, false,
533 &driver_info_manual, &dev));
534 pdata = calloc(1, sizeof(*pdata));
535 pdata->ping_add = key + i;
536 dev->platdata = pdata;
544 #define NODE_COUNT 10
546 static int dm_test_children(struct dm_test_state *dms)
548 struct udevice *top[NODE_COUNT];
549 struct udevice *child[NODE_COUNT];
550 struct udevice *grandchild[NODE_COUNT];
556 /* We don't care about the numbering for this test */
557 dms->skip_post_probe = 1;
559 ut_assert(NODE_COUNT > 5);
561 /* First create 10 top-level children */
562 ut_assertok(create_children(dms, dms->root, NODE_COUNT, 0, top));
564 /* Now a few have their own children */
565 ut_assertok(create_children(dms, top[2], NODE_COUNT, 2, NULL));
566 ut_assertok(create_children(dms, top[5], NODE_COUNT, 5, child));
568 /* And grandchildren */
569 for (i = 0; i < NODE_COUNT; i++)
570 ut_assertok(create_children(dms, child[i], NODE_COUNT, 50 * i,
571 i == 2 ? grandchild : NULL));
573 /* Check total number of devices */
574 total = NODE_COUNT * (3 + NODE_COUNT);
575 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
577 /* Try probing one of the grandchildren */
578 ut_assertok(uclass_get_device(UCLASS_TEST,
579 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
580 ut_asserteq_ptr(grandchild[0], dev);
583 * This should have probed the child and top node also, for a total
586 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
588 /* Probe the other grandchildren */
589 for (i = 1; i < NODE_COUNT; i++)
590 ut_assertok(device_probe(grandchild[i]));
592 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
594 /* Probe everything */
595 for (ret = uclass_first_device(UCLASS_TEST, &dev);
597 ret = uclass_next_device(&dev))
601 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
603 /* Remove a top-level child and check that the children are removed */
604 ut_assertok(device_remove(top[2]));
605 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
606 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
608 /* Try one with grandchildren */
609 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
610 ut_asserteq_ptr(dev, top[5]);
611 ut_assertok(device_remove(dev));
612 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
613 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
615 /* Try the same with unbind */
616 ut_assertok(device_unbind(top[2]));
617 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
618 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
620 /* Try one with grandchildren */
621 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
622 ut_asserteq_ptr(dev, top[6]);
623 ut_assertok(device_unbind(top[5]));
624 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
625 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
629 DM_TEST(dm_test_children, 0);
631 /* Test that pre-relocation devices work as expected */
632 static int dm_test_pre_reloc(struct dm_test_state *dms)
636 /* The normal driver should refuse to bind before relocation */
637 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
638 &driver_info_manual, &dev));
640 /* But this one is marked pre-reloc */
641 ut_assertok(device_bind_by_name(dms->root, true,
642 &driver_info_pre_reloc, &dev));
646 DM_TEST(dm_test_pre_reloc, 0);
648 static int dm_test_uclass_before_ready(struct dm_test_state *dms)
652 ut_assertok(uclass_get(UCLASS_TEST, &uc));
655 gd->dm_root_f = NULL;
656 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
658 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
662 DM_TEST(dm_test_uclass_before_ready, 0);
664 static int dm_test_uclass_devices_find(struct dm_test_state *dms)
669 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
671 ret = uclass_find_next_device(&dev)) {
678 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
680 static int dm_test_uclass_devices_find_by_name(struct dm_test_state *dms)
682 struct udevice *finddev;
683 struct udevice *testdev;
687 * For each test device found in fdt like: "a-test", "b-test", etc.,
688 * use its name and try to find it by uclass_find_device_by_name().
689 * Then, on success check if:
690 * - current 'testdev' name is equal to the returned 'finddev' name
691 * - current 'testdev' pointer is equal to the returned 'finddev'
693 * We assume that, each uclass's device name is unique, so if not, then
694 * this will fail on checking condition: testdev == finddev, since the
695 * uclass_find_device_by_name(), returns the first device by given name.
697 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
699 ret = uclass_find_next_device(&testdev)) {
703 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
707 ut_assertok(findret);
709 ut_asserteq_str(testdev->name, finddev->name);
710 ut_asserteq_ptr(testdev, finddev);
715 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
717 static int dm_test_uclass_devices_get(struct dm_test_state *dms)
722 for (ret = uclass_first_device(UCLASS_TEST, &dev);
724 ret = uclass_next_device(&dev)) {
727 ut_assert(device_active(dev));
732 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
734 static int dm_test_uclass_devices_get_by_name(struct dm_test_state *dms)
736 struct udevice *finddev;
737 struct udevice *testdev;
741 * For each test device found in fdt like: "a-test", "b-test", etc.,
742 * use its name and try to get it by uclass_get_device_by_name().
743 * On success check if:
744 * - returned finddev' is active
745 * - current 'testdev' name is equal to the returned 'finddev' name
746 * - current 'testdev' pointer is equal to the returned 'finddev'
748 * We asserts that the 'testdev' is active on each loop entry, so we
749 * could be sure that the 'finddev' is activated too, but for sure
752 * We assume that, each uclass's device name is unique, so if not, then
753 * this will fail on checking condition: testdev == finddev, since the
754 * uclass_get_device_by_name(), returns the first device by given name.
756 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
758 ret = uclass_next_device(&testdev)) {
761 ut_assert(device_active(testdev));
763 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
767 ut_assertok(findret);
769 ut_assert(device_active(finddev));
770 ut_asserteq_str(testdev->name, finddev->name);
771 ut_asserteq_ptr(testdev, finddev);
776 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
778 static int dm_test_device_get_uclass_id(struct dm_test_state *dms)
782 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
783 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
787 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);