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 autoprobe finds all the expected devices */
133 static int dm_test_autoprobe(struct dm_test_state *dms)
135 int expected_base_add;
140 ut_assertok(uclass_get(UCLASS_TEST, &uc));
143 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
144 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
145 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
147 /* The root device should not be activated until needed */
148 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
151 * We should be able to find the three test devices, and they should
152 * all be activated as they are used (lazy activation, required by
155 for (i = 0; i < 3; i++) {
156 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
158 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
159 "Driver %d/%s already activated", i, dev->name);
161 /* This should activate it */
162 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
164 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
166 /* Activating a device should activate the root device */
168 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
172 * Our 3 dm_test_info children should be passed to pre_probe and
175 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
176 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
178 /* Also we can check the per-device data */
179 expected_base_add = 0;
180 for (i = 0; i < 3; i++) {
181 struct dm_test_uclass_perdev_priv *priv;
182 struct dm_test_pdata *pdata;
184 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
187 priv = dev_get_uclass_priv(dev);
189 ut_asserteq(expected_base_add, priv->base_add);
191 pdata = dev->platdata;
192 expected_base_add += pdata->ping_add;
197 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
199 /* Check that we see the correct platdata in each device */
200 static int dm_test_platdata(struct dm_test_state *dms)
202 const struct dm_test_pdata *pdata;
206 for (i = 0; i < 3; i++) {
207 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
209 pdata = dev->platdata;
210 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
215 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
217 /* Test that we can bind, probe, remove, unbind a driver */
218 static int dm_test_lifecycle(struct dm_test_state *dms)
220 int op_count[DM_TEST_OP_COUNT];
221 struct udevice *dev, *test_dev;
225 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
227 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
230 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
231 == op_count[DM_TEST_OP_BIND] + 1);
232 ut_assert(!dev->priv);
234 /* Probe the device - it should fail allocating private data */
235 dms->force_fail_alloc = 1;
236 ret = device_probe(dev);
237 ut_assert(ret == -ENOMEM);
238 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
239 == op_count[DM_TEST_OP_PROBE] + 1);
240 ut_assert(!dev->priv);
242 /* Try again without the alloc failure */
243 dms->force_fail_alloc = 0;
244 ut_assertok(device_probe(dev));
245 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
246 == op_count[DM_TEST_OP_PROBE] + 2);
247 ut_assert(dev->priv);
249 /* This should be device 3 in the uclass */
250 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
251 ut_assert(dev == test_dev);
254 ut_assertok(test_ping(dev, 100, &pingret));
255 ut_assert(pingret == 102);
257 /* Now remove device 3 */
258 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
259 ut_assertok(device_remove(dev));
260 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
262 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
263 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
264 ut_assertok(device_unbind(dev));
265 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
266 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
270 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
272 /* Test that we can bind/unbind and the lists update correctly */
273 static int dm_test_ordering(struct dm_test_state *dms)
275 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
278 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
282 /* Bind two new devices (numbers 4 and 5) */
283 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
285 ut_assert(dev_penultimate);
286 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
290 /* Now remove device 3 */
291 ut_assertok(device_remove(dev));
292 ut_assertok(device_unbind(dev));
294 /* The device numbering should have shifted down one */
295 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
296 ut_assert(dev_penultimate == test_dev);
297 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
298 ut_assert(dev_last == test_dev);
300 /* Add back the original device 3, now in position 5 */
301 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
306 ut_assertok(test_ping(dev, 100, &pingret));
307 ut_assert(pingret == 102);
310 ut_assertok(device_remove(dev_penultimate));
311 ut_assertok(device_unbind(dev_penultimate));
312 ut_assertok(device_remove(dev_last));
313 ut_assertok(device_unbind(dev_last));
315 /* Our device should now be in position 3 */
316 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
317 ut_assert(dev == test_dev);
319 /* Now remove device 3 */
320 ut_assertok(device_remove(dev));
321 ut_assertok(device_unbind(dev));
325 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
327 /* Check that we can perform operations on a device (do a ping) */
328 int dm_check_operations(struct dm_test_state *dms, struct udevice *dev,
329 uint32_t base, struct dm_test_priv *priv)
334 /* Getting the child device should allocate platdata / priv */
335 ut_assertok(testfdt_ping(dev, 10, &pingret));
336 ut_assert(dev->priv);
337 ut_assert(dev->platdata);
339 expected = 10 + base;
340 ut_asserteq(expected, pingret);
342 /* Do another ping */
343 ut_assertok(testfdt_ping(dev, 20, &pingret));
344 expected = 20 + base;
345 ut_asserteq(expected, pingret);
347 /* Now check the ping_total */
349 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
355 /* Check that we can perform operations on devices */
356 static int dm_test_operations(struct dm_test_state *dms)
362 * Now check that the ping adds are what we expect. This is using the
363 * ping-add property in each node.
365 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
368 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
371 * Get the 'reg' property, which tells us what the ping add
372 * should be. We don't use the platdata because we want
373 * to test the code that sets that up (testfdt_drv_probe()).
375 base = test_pdata[i].ping_add;
376 debug("dev=%d, base=%d\n", i, base);
378 ut_assert(!dm_check_operations(dms, dev, base, dev->priv));
383 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
385 /* Remove all drivers and check that things work */
386 static int dm_test_remove(struct dm_test_state *dms)
391 for (i = 0; i < 3; i++) {
392 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
394 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
395 "Driver %d/%s not activated", i, dev->name);
396 ut_assertok(device_remove(dev));
397 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
398 "Driver %d/%s should have deactivated", i,
400 ut_assert(!dev->priv);
405 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
407 /* Remove and recreate everything, check for memory leaks */
408 static int dm_test_leak(struct dm_test_state *dms)
412 for (i = 0; i < 2; i++) {
417 dm_leak_check_start(dms);
419 ut_assertok(dm_scan_platdata(false));
420 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
422 /* Scanning the uclass is enough to probe all the devices */
423 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
424 for (ret = uclass_first_device(UCLASS_TEST, &dev);
426 ret = uclass_next_device(&dev))
431 ut_assertok(dm_leak_check_end(dms));
436 DM_TEST(dm_test_leak, 0);
438 /* Test uclass init/destroy methods */
439 static int dm_test_uclass(struct dm_test_state *dms)
443 ut_assertok(uclass_get(UCLASS_TEST, &uc));
444 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
445 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
448 ut_assertok(uclass_destroy(uc));
449 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
450 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
454 DM_TEST(dm_test_uclass, 0);
457 * create_children() - Create children of a parent node
459 * @dms: Test system state
460 * @parent: Parent device
461 * @count: Number of children to create
462 * @key: Key value to put in first child. Subsequence children
463 * receive an incrementing value
464 * @child: If not NULL, then the child device pointers are written into
466 * @return 0 if OK, -ve on error
468 static int create_children(struct dm_test_state *dms, struct udevice *parent,
469 int count, int key, struct udevice *child[])
474 for (i = 0; i < count; i++) {
475 struct dm_test_pdata *pdata;
477 ut_assertok(device_bind_by_name(parent, false,
478 &driver_info_manual, &dev));
479 pdata = calloc(1, sizeof(*pdata));
480 pdata->ping_add = key + i;
481 dev->platdata = pdata;
489 #define NODE_COUNT 10
491 static int dm_test_children(struct dm_test_state *dms)
493 struct udevice *top[NODE_COUNT];
494 struct udevice *child[NODE_COUNT];
495 struct udevice *grandchild[NODE_COUNT];
501 /* We don't care about the numbering for this test */
502 dms->skip_post_probe = 1;
504 ut_assert(NODE_COUNT > 5);
506 /* First create 10 top-level children */
507 ut_assertok(create_children(dms, dms->root, NODE_COUNT, 0, top));
509 /* Now a few have their own children */
510 ut_assertok(create_children(dms, top[2], NODE_COUNT, 2, NULL));
511 ut_assertok(create_children(dms, top[5], NODE_COUNT, 5, child));
513 /* And grandchildren */
514 for (i = 0; i < NODE_COUNT; i++)
515 ut_assertok(create_children(dms, child[i], NODE_COUNT, 50 * i,
516 i == 2 ? grandchild : NULL));
518 /* Check total number of devices */
519 total = NODE_COUNT * (3 + NODE_COUNT);
520 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
522 /* Try probing one of the grandchildren */
523 ut_assertok(uclass_get_device(UCLASS_TEST,
524 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
525 ut_asserteq_ptr(grandchild[0], dev);
528 * This should have probed the child and top node also, for a total
531 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
533 /* Probe the other grandchildren */
534 for (i = 1; i < NODE_COUNT; i++)
535 ut_assertok(device_probe(grandchild[i]));
537 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
539 /* Probe everything */
540 for (ret = uclass_first_device(UCLASS_TEST, &dev);
542 ret = uclass_next_device(&dev))
546 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
548 /* Remove a top-level child and check that the children are removed */
549 ut_assertok(device_remove(top[2]));
550 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
551 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
553 /* Try one with grandchildren */
554 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
555 ut_asserteq_ptr(dev, top[5]);
556 ut_assertok(device_remove(dev));
557 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
558 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
560 /* Try the same with unbind */
561 ut_assertok(device_unbind(top[2]));
562 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
563 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
565 /* Try one with grandchildren */
566 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
567 ut_asserteq_ptr(dev, top[6]);
568 ut_assertok(device_unbind(top[5]));
569 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
570 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
574 DM_TEST(dm_test_children, 0);
576 /* Test that pre-relocation devices work as expected */
577 static int dm_test_pre_reloc(struct dm_test_state *dms)
581 /* The normal driver should refuse to bind before relocation */
582 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
583 &driver_info_manual, &dev));
585 /* But this one is marked pre-reloc */
586 ut_assertok(device_bind_by_name(dms->root, true,
587 &driver_info_pre_reloc, &dev));
591 DM_TEST(dm_test_pre_reloc, 0);
593 static int dm_test_uclass_before_ready(struct dm_test_state *dms)
597 ut_assertok(uclass_get(UCLASS_TEST, &uc));
599 memset(gd, '\0', sizeof(*gd));
600 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
605 DM_TEST(dm_test_uclass_before_ready, 0);
607 static int dm_test_device_get_uclass_id(struct dm_test_state *dms)
611 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
612 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
616 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);