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_POST_PROBE
]);
146 /* The root device should not be activated until needed */
147 ut_assert(dms
->root
->flags
& DM_FLAG_ACTIVATED
);
150 * We should be able to find the three test devices, and they should
151 * all be activated as they are used (lazy activation, required by
154 for (i
= 0; i
< 3; i
++) {
155 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
157 ut_assertf(!(dev
->flags
& DM_FLAG_ACTIVATED
),
158 "Driver %d/%s already activated", i
, dev
->name
);
160 /* This should activate it */
161 ut_assertok(uclass_get_device(UCLASS_TEST
, i
, &dev
));
163 ut_assert(dev
->flags
& DM_FLAG_ACTIVATED
);
165 /* Activating a device should activate the root device */
167 ut_assert(dms
->root
->flags
& DM_FLAG_ACTIVATED
);
170 /* Our 3 dm_test_infox children should be passed to post_probe */
171 ut_asserteq(3, dm_testdrv_op_count
[DM_TEST_OP_POST_PROBE
]);
173 /* Also we can check the per-device data */
174 expected_base_add
= 0;
175 for (i
= 0; i
< 3; i
++) {
176 struct dm_test_uclass_perdev_priv
*priv
;
177 struct dm_test_pdata
*pdata
;
179 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
182 priv
= dev
->uclass_priv
;
184 ut_asserteq(expected_base_add
, priv
->base_add
);
186 pdata
= dev
->platdata
;
187 expected_base_add
+= pdata
->ping_add
;
192 DM_TEST(dm_test_autoprobe
, DM_TESTF_SCAN_PDATA
);
194 /* Check that we see the correct platdata in each device */
195 static int dm_test_platdata(struct dm_test_state
*dms
)
197 const struct dm_test_pdata
*pdata
;
201 for (i
= 0; i
< 3; i
++) {
202 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
204 pdata
= dev
->platdata
;
205 ut_assert(pdata
->ping_add
== test_pdata
[i
].ping_add
);
210 DM_TEST(dm_test_platdata
, DM_TESTF_SCAN_PDATA
);
212 /* Test that we can bind, probe, remove, unbind a driver */
213 static int dm_test_lifecycle(struct dm_test_state
*dms
)
215 int op_count
[DM_TEST_OP_COUNT
];
216 struct udevice
*dev
, *test_dev
;
220 memcpy(op_count
, dm_testdrv_op_count
, sizeof(op_count
));
222 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
225 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_BIND
]
226 == op_count
[DM_TEST_OP_BIND
] + 1);
227 ut_assert(!dev
->priv
);
229 /* Probe the device - it should fail allocating private data */
230 dms
->force_fail_alloc
= 1;
231 ret
= device_probe(dev
);
232 ut_assert(ret
== -ENOMEM
);
233 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_PROBE
]
234 == op_count
[DM_TEST_OP_PROBE
] + 1);
235 ut_assert(!dev
->priv
);
237 /* Try again without the alloc failure */
238 dms
->force_fail_alloc
= 0;
239 ut_assertok(device_probe(dev
));
240 ut_assert(dm_testdrv_op_count
[DM_TEST_OP_PROBE
]
241 == op_count
[DM_TEST_OP_PROBE
] + 2);
242 ut_assert(dev
->priv
);
244 /* This should be device 3 in the uclass */
245 ut_assertok(uclass_find_device(UCLASS_TEST
, 3, &test_dev
));
246 ut_assert(dev
== test_dev
);
249 ut_assertok(test_ping(dev
, 100, &pingret
));
250 ut_assert(pingret
== 102);
252 /* Now remove device 3 */
253 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_PRE_REMOVE
]);
254 ut_assertok(device_remove(dev
));
255 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_PRE_REMOVE
]);
257 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
258 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_PRE_UNBIND
]);
259 ut_assertok(device_unbind(dev
));
260 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
261 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_PRE_UNBIND
]);
265 DM_TEST(dm_test_lifecycle
, DM_TESTF_SCAN_PDATA
| DM_TESTF_PROBE_TEST
);
267 /* Test that we can bind/unbind and the lists update correctly */
268 static int dm_test_ordering(struct dm_test_state
*dms
)
270 struct udevice
*dev
, *dev_penultimate
, *dev_last
, *test_dev
;
273 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
277 /* Bind two new devices (numbers 4 and 5) */
278 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
280 ut_assert(dev_penultimate
);
281 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
285 /* Now remove device 3 */
286 ut_assertok(device_remove(dev
));
287 ut_assertok(device_unbind(dev
));
289 /* The device numbering should have shifted down one */
290 ut_assertok(uclass_find_device(UCLASS_TEST
, 3, &test_dev
));
291 ut_assert(dev_penultimate
== test_dev
);
292 ut_assertok(uclass_find_device(UCLASS_TEST
, 4, &test_dev
));
293 ut_assert(dev_last
== test_dev
);
295 /* Add back the original device 3, now in position 5 */
296 ut_assertok(device_bind_by_name(dms
->root
, false, &driver_info_manual
,
301 ut_assertok(test_ping(dev
, 100, &pingret
));
302 ut_assert(pingret
== 102);
305 ut_assertok(device_remove(dev_penultimate
));
306 ut_assertok(device_unbind(dev_penultimate
));
307 ut_assertok(device_remove(dev_last
));
308 ut_assertok(device_unbind(dev_last
));
310 /* Our device should now be in position 3 */
311 ut_assertok(uclass_find_device(UCLASS_TEST
, 3, &test_dev
));
312 ut_assert(dev
== test_dev
);
314 /* Now remove device 3 */
315 ut_assertok(device_remove(dev
));
316 ut_assertok(device_unbind(dev
));
320 DM_TEST(dm_test_ordering
, DM_TESTF_SCAN_PDATA
);
322 /* Check that we can perform operations on a device (do a ping) */
323 int dm_check_operations(struct dm_test_state
*dms
, struct udevice
*dev
,
324 uint32_t base
, struct dm_test_priv
*priv
)
329 /* Getting the child device should allocate platdata / priv */
330 ut_assertok(testfdt_ping(dev
, 10, &pingret
));
331 ut_assert(dev
->priv
);
332 ut_assert(dev
->platdata
);
334 expected
= 10 + base
;
335 ut_asserteq(expected
, pingret
);
337 /* Do another ping */
338 ut_assertok(testfdt_ping(dev
, 20, &pingret
));
339 expected
= 20 + base
;
340 ut_asserteq(expected
, pingret
);
342 /* Now check the ping_total */
344 ut_asserteq(DM_TEST_START_TOTAL
+ 10 + 20 + base
* 2,
350 /* Check that we can perform operations on devices */
351 static int dm_test_operations(struct dm_test_state
*dms
)
357 * Now check that the ping adds are what we expect. This is using the
358 * ping-add property in each node.
360 for (i
= 0; i
< ARRAY_SIZE(test_pdata
); i
++) {
363 ut_assertok(uclass_get_device(UCLASS_TEST
, i
, &dev
));
366 * Get the 'reg' property, which tells us what the ping add
367 * should be. We don't use the platdata because we want
368 * to test the code that sets that up (testfdt_drv_probe()).
370 base
= test_pdata
[i
].ping_add
;
371 debug("dev=%d, base=%d\n", i
, base
);
373 ut_assert(!dm_check_operations(dms
, dev
, base
, dev
->priv
));
378 DM_TEST(dm_test_operations
, DM_TESTF_SCAN_PDATA
);
380 /* Remove all drivers and check that things work */
381 static int dm_test_remove(struct dm_test_state
*dms
)
386 for (i
= 0; i
< 3; i
++) {
387 ut_assertok(uclass_find_device(UCLASS_TEST
, i
, &dev
));
389 ut_assertf(dev
->flags
& DM_FLAG_ACTIVATED
,
390 "Driver %d/%s not activated", i
, dev
->name
);
391 ut_assertok(device_remove(dev
));
392 ut_assertf(!(dev
->flags
& DM_FLAG_ACTIVATED
),
393 "Driver %d/%s should have deactivated", i
,
395 ut_assert(!dev
->priv
);
400 DM_TEST(dm_test_remove
, DM_TESTF_SCAN_PDATA
| DM_TESTF_PROBE_TEST
);
402 /* Remove and recreate everything, check for memory leaks */
403 static int dm_test_leak(struct dm_test_state
*dms
)
407 for (i
= 0; i
< 2; i
++) {
412 dm_leak_check_start(dms
);
414 ut_assertok(dm_scan_platdata(false));
415 ut_assertok(dm_scan_fdt(gd
->fdt_blob
, false));
417 /* Scanning the uclass is enough to probe all the devices */
418 for (id
= UCLASS_ROOT
; id
< UCLASS_COUNT
; id
++) {
419 for (ret
= uclass_first_device(UCLASS_TEST
, &dev
);
421 ret
= uclass_next_device(&dev
))
426 ut_assertok(dm_leak_check_end(dms
));
431 DM_TEST(dm_test_leak
, 0);
433 /* Test uclass init/destroy methods */
434 static int dm_test_uclass(struct dm_test_state
*dms
)
438 ut_assertok(uclass_get(UCLASS_TEST
, &uc
));
439 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_INIT
]);
440 ut_asserteq(0, dm_testdrv_op_count
[DM_TEST_OP_DESTROY
]);
443 ut_assertok(uclass_destroy(uc
));
444 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_INIT
]);
445 ut_asserteq(1, dm_testdrv_op_count
[DM_TEST_OP_DESTROY
]);
449 DM_TEST(dm_test_uclass
, 0);
452 * create_children() - Create children of a parent node
454 * @dms: Test system state
455 * @parent: Parent device
456 * @count: Number of children to create
457 * @key: Key value to put in first child. Subsequence children
458 * receive an incrementing value
459 * @child: If not NULL, then the child device pointers are written into
461 * @return 0 if OK, -ve on error
463 static int create_children(struct dm_test_state
*dms
, struct udevice
*parent
,
464 int count
, int key
, struct udevice
*child
[])
469 for (i
= 0; i
< count
; i
++) {
470 struct dm_test_pdata
*pdata
;
472 ut_assertok(device_bind_by_name(parent
, false,
473 &driver_info_manual
, &dev
));
474 pdata
= calloc(1, sizeof(*pdata
));
475 pdata
->ping_add
= key
+ i
;
476 dev
->platdata
= pdata
;
484 #define NODE_COUNT 10
486 static int dm_test_children(struct dm_test_state
*dms
)
488 struct udevice
*top
[NODE_COUNT
];
489 struct udevice
*child
[NODE_COUNT
];
490 struct udevice
*grandchild
[NODE_COUNT
];
496 /* We don't care about the numbering for this test */
497 dms
->skip_post_probe
= 1;
499 ut_assert(NODE_COUNT
> 5);
501 /* First create 10 top-level children */
502 ut_assertok(create_children(dms
, dms
->root
, NODE_COUNT
, 0, top
));
504 /* Now a few have their own children */
505 ut_assertok(create_children(dms
, top
[2], NODE_COUNT
, 2, NULL
));
506 ut_assertok(create_children(dms
, top
[5], NODE_COUNT
, 5, child
));
508 /* And grandchildren */
509 for (i
= 0; i
< NODE_COUNT
; i
++)
510 ut_assertok(create_children(dms
, child
[i
], NODE_COUNT
, 50 * i
,
511 i
== 2 ? grandchild
: NULL
));
513 /* Check total number of devices */
514 total
= NODE_COUNT
* (3 + NODE_COUNT
);
515 ut_asserteq(total
, dm_testdrv_op_count
[DM_TEST_OP_BIND
]);
517 /* Try probing one of the grandchildren */
518 ut_assertok(uclass_get_device(UCLASS_TEST
,
519 NODE_COUNT
* 3 + 2 * NODE_COUNT
, &dev
));
520 ut_asserteq_ptr(grandchild
[0], dev
);
523 * This should have probed the child and top node also, for a total
526 ut_asserteq(3, dm_testdrv_op_count
[DM_TEST_OP_PROBE
]);
528 /* Probe the other grandchildren */
529 for (i
= 1; i
< NODE_COUNT
; i
++)
530 ut_assertok(device_probe(grandchild
[i
]));
532 ut_asserteq(2 + NODE_COUNT
, dm_testdrv_op_count
[DM_TEST_OP_PROBE
]);
534 /* Probe everything */
535 for (ret
= uclass_first_device(UCLASS_TEST
, &dev
);
537 ret
= uclass_next_device(&dev
))
541 ut_asserteq(total
, dm_testdrv_op_count
[DM_TEST_OP_PROBE
]);
543 /* Remove a top-level child and check that the children are removed */
544 ut_assertok(device_remove(top
[2]));
545 ut_asserteq(NODE_COUNT
+ 1, dm_testdrv_op_count
[DM_TEST_OP_REMOVE
]);
546 dm_testdrv_op_count
[DM_TEST_OP_REMOVE
] = 0;
548 /* Try one with grandchildren */
549 ut_assertok(uclass_get_device(UCLASS_TEST
, 5, &dev
));
550 ut_asserteq_ptr(dev
, top
[5]);
551 ut_assertok(device_remove(dev
));
552 ut_asserteq(1 + NODE_COUNT
* (1 + NODE_COUNT
),
553 dm_testdrv_op_count
[DM_TEST_OP_REMOVE
]);
555 /* Try the same with unbind */
556 ut_assertok(device_unbind(top
[2]));
557 ut_asserteq(NODE_COUNT
+ 1, dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
558 dm_testdrv_op_count
[DM_TEST_OP_UNBIND
] = 0;
560 /* Try one with grandchildren */
561 ut_assertok(uclass_get_device(UCLASS_TEST
, 5, &dev
));
562 ut_asserteq_ptr(dev
, top
[6]);
563 ut_assertok(device_unbind(top
[5]));
564 ut_asserteq(1 + NODE_COUNT
* (1 + NODE_COUNT
),
565 dm_testdrv_op_count
[DM_TEST_OP_UNBIND
]);
569 DM_TEST(dm_test_children
, 0);
571 /* Test that pre-relocation devices work as expected */
572 static int dm_test_pre_reloc(struct dm_test_state
*dms
)
576 /* The normal driver should refuse to bind before relocation */
577 ut_asserteq(-EPERM
, device_bind_by_name(dms
->root
, true,
578 &driver_info_manual
, &dev
));
580 /* But this one is marked pre-reloc */
581 ut_assertok(device_bind_by_name(dms
->root
, true,
582 &driver_info_pre_reloc
, &dev
));
586 DM_TEST(dm_test_pre_reloc
, 0);
588 static int dm_test_uclass_before_ready(struct dm_test_state
*dms
)
592 ut_assertok(uclass_get(UCLASS_TEST
, &uc
));
594 memset(gd
, '\0', sizeof(*gd
));
595 ut_asserteq_ptr(NULL
, uclass_find(UCLASS_TEST
));
600 DM_TEST(dm_test_uclass_before_ready
, 0);