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 /* Test that binding with platdata occurs correctly */
71 static int dm_test_autobind(struct dm_test_state
*dms
)
76 * We should have a single class (UCLASS_ROOT) and a single root
77 * device with no children.
80 ut_asserteq(1, list_count_items(&gd
->uclass_root
));
81 ut_asserteq(0, list_count_items(&gd
->dm_root
->child_head
));
82 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_POST_BIND
]);
84 ut_assertok(dm_scan_platdata(false));
86 /* We should have our test class now at least, plus more children */
87 ut_assert(1 < list_count_items(&gd
->uclass_root
));
88 ut_assert(0 < list_count_items(&gd
->dm_root
->child_head
));
90 /* Our 3 dm_test_infox children should be bound to the test uclass */
91 ut_asserteq(3, dm_testdrv_op_count
[DM_TEST_OP_POST_BIND
]);
93 /* No devices should be probed */
94 list_for_each_entry(dev
, &gd
->dm_root
->child_head
, sibling_node
)
95 ut_assert(!(dev
->flags
& DM_FLAG_ACTIVATED
));
97 /* Our test driver should have been bound 3 times */
98 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_BIND
] == 3);
102 DM_TEST(dm_test_autobind
, 0);
104 /* Test that autoprobe finds all the expected devices */
105 static int dm_test_autoprobe(struct dm_test_state
*dms
)
107 int expected_base_add
;
112 ut_assertok(uclass_get(UCLASS_TEST
, &uc
));
115 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_INIT
]);
116 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_POST_PROBE
]);
118 /* The root device should not be activated until needed */
119 ut_assert(dms
->root
->flags
& DM_FLAG_ACTIVATED
);
122 * We should be able to find the three test devices, and they should
123 * all be activated as they are used (lazy activation, required by
126 for (i
= 0; i
< 3; i
++) {
127 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
129 ut_assertf(!(dev
->flags
& DM_FLAG_ACTIVATED
),
130 "Driver %d/%s already activated", i
, dev
->name
);
132 /* This should activate it */
133 ut_assertok(uclass_get_device(UCLASS_TEST
, i
, &dev
));
135 ut_assert(dev
->flags
& DM_FLAG_ACTIVATED
);
137 /* Activating a device should activate the root device */
139 ut_assert(dms
->root
->flags
& DM_FLAG_ACTIVATED
);
142 /* Our 3 dm_test_infox children should be passed to post_probe */
143 ut_asserteq(3, dm_testdrv_op_count
[DM_TEST_OP_POST_PROBE
]);
145 /* Also we can check the per-device data */
146 expected_base_add
= 0;
147 for (i
= 0; i
< 3; i
++) {
148 struct dm_test_uclass_perdev_priv
*priv
;
149 struct dm_test_pdata
*pdata
;
151 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
154 priv
= dev
->uclass_priv
;
156 ut_asserteq(expected_base_add
, priv
->base_add
);
158 pdata
= dev
->platdata
;
159 expected_base_add
+= pdata
->ping_add
;
164 DM_TEST(dm_test_autoprobe
, DM_TESTF_SCAN_PDATA
);
166 /* Check that we see the correct platdata in each device */
167 static int dm_test_platdata(struct dm_test_state
*dms
)
169 const struct dm_test_pdata
*pdata
;
173 for (i
= 0; i
< 3; i
++) {
174 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
176 pdata
= dev
->platdata
;
177 ut_assert(pdata
->ping_add
== test_pdata
[i
].ping_add
);
182 DM_TEST(dm_test_platdata
, DM_TESTF_SCAN_PDATA
);
184 /* Test that we can bind, probe, remove, unbind a driver */
185 static int dm_test_lifecycle(struct dm_test_state
*dms
)
187 int op_count
[DM_TEST_OP_COUNT
];
188 struct udevice
*dev
, *test_dev
;
192 memcpy(op_count
, dm_testdrv_op_count
, sizeof(op_count
));
194 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
197 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_BIND
]
198 == op_count
[DM_TEST_OP_BIND
] + 1);
199 ut_assert(!dev
->priv
);
201 /* Probe the device - it should fail allocating private data */
202 dms
->force_fail_alloc
= 1;
203 ret
= device_probe(dev
);
204 ut_assert(ret
== -ENOMEM
);
205 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_PROBE
]
206 == op_count
[DM_TEST_OP_PROBE
] + 1);
207 ut_assert(!dev
->priv
);
209 /* Try again without the alloc failure */
210 dms
->force_fail_alloc
= 0;
211 ut_assertok(device_probe(dev
));
212 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_PROBE
]
213 == op_count
[DM_TEST_OP_PROBE
] + 2);
214 ut_assert(dev
->priv
);
216 /* This should be device 3 in the uclass */
217 ut_assertok(uclass_find_device(UCLASS_TEST
, 3, &test_dev
));
218 ut_assert(dev
== test_dev
);
221 ut_assertok(test_ping(dev
, 100, &pingret
));
222 ut_assert(pingret
== 102);
224 /* Now remove device 3 */
225 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_PRE_REMOVE
]);
226 ut_assertok(device_remove(dev
));
227 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_PRE_REMOVE
]);
229 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
230 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_PRE_UNBIND
]);
231 ut_assertok(device_unbind(dev
));
232 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
233 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_PRE_UNBIND
]);
237 DM_TEST(dm_test_lifecycle
, DM_TESTF_SCAN_PDATA
| DM_TESTF_PROBE_TEST
);
239 /* Test that we can bind/unbind and the lists update correctly */
240 static int dm_test_ordering(struct dm_test_state
*dms
)
242 struct udevice
*dev
, *dev_penultimate
, *dev_last
, *test_dev
;
245 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
249 /* Bind two new devices (numbers 4 and 5) */
250 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
252 ut_assert(dev_penultimate
);
253 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
257 /* Now remove device 3 */
258 ut_assertok(device_remove(dev
));
259 ut_assertok(device_unbind(dev
));
261 /* The device numbering should have shifted down one */
262 ut_assertok(uclass_find_device(UCLASS_TEST
, 3, &test_dev
));
263 ut_assert(dev_penultimate
== test_dev
);
264 ut_assertok(uclass_find_device(UCLASS_TEST
, 4, &test_dev
));
265 ut_assert(dev_last
== test_dev
);
267 /* Add back the original device 3, now in position 5 */
268 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
273 ut_assertok(test_ping(dev
, 100, &pingret
));
274 ut_assert(pingret
== 102);
277 ut_assertok(device_remove(dev_penultimate
));
278 ut_assertok(device_unbind(dev_penultimate
));
279 ut_assertok(device_remove(dev_last
));
280 ut_assertok(device_unbind(dev_last
));
282 /* Our device should now be in position 3 */
283 ut_assertok(uclass_find_device(UCLASS_TEST
, 3, &test_dev
));
284 ut_assert(dev
== test_dev
);
286 /* Now remove device 3 */
287 ut_assertok(device_remove(dev
));
288 ut_assertok(device_unbind(dev
));
292 DM_TEST(dm_test_ordering
, DM_TESTF_SCAN_PDATA
);
294 /* Check that we can perform operations on a device (do a ping) */
295 int dm_check_operations(struct dm_test_state
*dms
, struct udevice
*dev
,
296 uint32_t base
, struct dm_test_priv
*priv
)
301 /* Getting the child device should allocate platdata / priv */
302 ut_assertok(testfdt_ping(dev
, 10, &pingret
));
303 ut_assert(dev
->priv
);
304 ut_assert(dev
->platdata
);
306 expected
= 10 + base
;
307 ut_asserteq(expected
, pingret
);
309 /* Do another ping */
310 ut_assertok(testfdt_ping(dev
, 20, &pingret
));
311 expected
= 20 + base
;
312 ut_asserteq(expected
, pingret
);
314 /* Now check the ping_total */
316 ut_asserteq(DM_TEST_START_TOTAL
+ 10 + 20 + base
* 2,
322 /* Check that we can perform operations on devices */
323 static int dm_test_operations(struct dm_test_state
*dms
)
329 * Now check that the ping adds are what we expect. This is using the
330 * ping-add property in each node.
332 for (i
= 0; i
< ARRAY_SIZE(test_pdata
); i
++) {
335 ut_assertok(uclass_get_device(UCLASS_TEST
, i
, &dev
));
338 * Get the 'reg' property, which tells us what the ping add
339 * should be. We don't use the platdata because we want
340 * to test the code that sets that up (testfdt_drv_probe()).
342 base
= test_pdata
[i
].ping_add
;
343 debug("dev=%d, base=%d\n", i
, base
);
345 ut_assert(!dm_check_operations(dms
, dev
, base
, dev
->priv
));
350 DM_TEST(dm_test_operations
, DM_TESTF_SCAN_PDATA
);
352 /* Remove all drivers and check that things work */
353 static int dm_test_remove(struct dm_test_state
*dms
)
358 for (i
= 0; i
< 3; i
++) {
359 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
361 ut_assertf(dev
->flags
& DM_FLAG_ACTIVATED
,
362 "Driver %d/%s not activated", i
, dev
->name
);
363 ut_assertok(device_remove(dev
));
364 ut_assertf(!(dev
->flags
& DM_FLAG_ACTIVATED
),
365 "Driver %d/%s should have deactivated", i
,
367 ut_assert(!dev
->priv
);
372 DM_TEST(dm_test_remove
, DM_TESTF_SCAN_PDATA
| DM_TESTF_PROBE_TEST
);
374 /* Remove and recreate everything, check for memory leaks */
375 static int dm_test_leak(struct dm_test_state
*dms
)
379 for (i
= 0; i
< 2; i
++) {
380 struct mallinfo start
, end
;
387 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
389 ut_assertok(dm_scan_platdata(false));
390 ut_assertok(dm_scan_fdt(gd
->fdt_blob
, false));
392 /* Scanning the uclass is enough to probe all the devices */
393 for (id
= UCLASS_ROOT
; id
< UCLASS_COUNT
; id
++) {
394 for (ret
= uclass_first_device(UCLASS_TEST
, &dev
);
396 ret
= uclass_next_device(&dev
))
401 /* Don't delete the root class, since we started with that */
402 for (id
= UCLASS_ROOT
+ 1; id
< UCLASS_COUNT
; id
++) {
405 uc
= uclass_find(id
);
408 ut_assertok(uclass_destroy(uc
));
412 ut_asserteq(start
.uordblks
, end
.uordblks
);
417 DM_TEST(dm_test_leak
, 0);
419 /* Test uclass init/destroy methods */
420 static int dm_test_uclass(struct dm_test_state
*dms
)
424 ut_assertok(uclass_get(UCLASS_TEST
, &uc
));
425 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_INIT
]);
426 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_DESTROY
]);
429 ut_assertok(uclass_destroy(uc
));
430 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_INIT
]);
431 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_DESTROY
]);
435 DM_TEST(dm_test_uclass
, 0);
438 * create_children() - Create children of a parent node
440 * @dms: Test system state
441 * @parent: Parent device
442 * @count: Number of children to create
443 * @key: Key value to put in first child. Subsequence children
444 * receive an incrementing value
445 * @child: If not NULL, then the child device pointers are written into
447 * @return 0 if OK, -ve on error
449 static int create_children(struct dm_test_state
*dms
, struct udevice
*parent
,
450 int count
, int key
, struct udevice
*child
[])
455 for (i
= 0; i
< count
; i
++) {
456 struct dm_test_pdata
*pdata
;
458 ut_assertok(device_bind_by_name(parent
, false,
459 &driver_info_manual
, &dev
));
460 pdata
= calloc(1, sizeof(*pdata
));
461 pdata
->ping_add
= key
+ i
;
462 dev
->platdata
= pdata
;
470 #define NODE_COUNT 10
472 static int dm_test_children(struct dm_test_state
*dms
)
474 struct udevice
*top
[NODE_COUNT
];
475 struct udevice
*child
[NODE_COUNT
];
476 struct udevice
*grandchild
[NODE_COUNT
];
482 /* We don't care about the numbering for this test */
483 dms
->skip_post_probe
= 1;
485 ut_assert(NODE_COUNT
> 5);
487 /* First create 10 top-level children */
488 ut_assertok(create_children(dms
, dms
->root
, NODE_COUNT
, 0, top
));
490 /* Now a few have their own children */
491 ut_assertok(create_children(dms
, top
[2], NODE_COUNT
, 2, NULL
));
492 ut_assertok(create_children(dms
, top
[5], NODE_COUNT
, 5, child
));
494 /* And grandchildren */
495 for (i
= 0; i
< NODE_COUNT
; i
++)
496 ut_assertok(create_children(dms
, child
[i
], NODE_COUNT
, 50 * i
,
497 i
== 2 ? grandchild
: NULL
));
499 /* Check total number of devices */
500 total
= NODE_COUNT
* (3 + NODE_COUNT
);
501 ut_asserteq(total
, dm_testdrv_op_count
[DM_TEST_OP_BIND
]);
503 /* Try probing one of the grandchildren */
504 ut_assertok(uclass_get_device(UCLASS_TEST
,
505 NODE_COUNT
* 3 + 2 * NODE_COUNT
, &dev
));
506 ut_asserteq_ptr(grandchild
[0], dev
);
509 * This should have probed the child and top node also, for a total
512 ut_asserteq(3, dm_testdrv_op_count
[DM_TEST_OP_PROBE
]);
514 /* Probe the other grandchildren */
515 for (i
= 1; i
< NODE_COUNT
; i
++)
516 ut_assertok(device_probe(grandchild
[i
]));
518 ut_asserteq(2 + NODE_COUNT
, dm_testdrv_op_count
[DM_TEST_OP_PROBE
]);
520 /* Probe everything */
521 for (ret
= uclass_first_device(UCLASS_TEST
, &dev
);
523 ret
= uclass_next_device(&dev
))
527 ut_asserteq(total
, dm_testdrv_op_count
[DM_TEST_OP_PROBE
]);
529 /* Remove a top-level child and check that the children are removed */
530 ut_assertok(device_remove(top
[2]));
531 ut_asserteq(NODE_COUNT
+ 1, dm_testdrv_op_count
[DM_TEST_OP_REMOVE
]);
532 dm_testdrv_op_count
[DM_TEST_OP_REMOVE
] = 0;
534 /* Try one with grandchildren */
535 ut_assertok(uclass_get_device(UCLASS_TEST
, 5, &dev
));
536 ut_asserteq_ptr(dev
, top
[5]);
537 ut_assertok(device_remove(dev
));
538 ut_asserteq(1 + NODE_COUNT
* (1 + NODE_COUNT
),
539 dm_testdrv_op_count
[DM_TEST_OP_REMOVE
]);
541 /* Try the same with unbind */
542 ut_assertok(device_unbind(top
[2]));
543 ut_asserteq(NODE_COUNT
+ 1, dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
544 dm_testdrv_op_count
[DM_TEST_OP_UNBIND
] = 0;
546 /* Try one with grandchildren */
547 ut_assertok(uclass_get_device(UCLASS_TEST
, 5, &dev
));
548 ut_asserteq_ptr(dev
, top
[6]);
549 ut_assertok(device_unbind(top
[5]));
550 ut_asserteq(1 + NODE_COUNT
* (1 + NODE_COUNT
),
551 dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
555 DM_TEST(dm_test_children
, 0);
557 /* Test that pre-relocation devices work as expected */
558 static int dm_test_pre_reloc(struct dm_test_state
*dms
)
562 /* The normal driver should refuse to bind before relocation */
563 ut_asserteq(-EPERM
, device_bind_by_name(dms
->root
, true,
564 &driver_info_manual
, &dev
));
566 /* But this one is marked pre-reloc */
567 ut_assertok(device_bind_by_name(dms
->root
, true,
568 &driver_info_pre_reloc
, &dev
));
572 DM_TEST(dm_test_pre_reloc
, 0);
574 static int dm_test_uclass_before_ready(struct dm_test_state
*dms
)
578 ut_assertok(uclass_get(UCLASS_TEST
, &uc
));
580 memset(gd
, '\0', sizeof(*gd
));
581 ut_asserteq_ptr(NULL
, uclass_find(UCLASS_TEST
));
586 DM_TEST(dm_test_uclass_before_ready
, 0);