1 /*
2 * Tests for the core driver model code
3 *
4 * Copyright (c) 2013 Google, Inc
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9 #include <common.h>
10 #include <errno.h>
11 #include <dm.h>
12 #include <fdtdec.h>
13 #include <malloc.h>
14 #include <dm/device-internal.h>
15 #include <dm/root.h>
16 #include <dm/util.h>
17 #include <dm/test.h>
18 #include <dm/uclass-internal.h>
19 #include <test/ut.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 enum {
24 TEST_INTVAL1 = 0,
25 TEST_INTVAL2 = 3,
26 TEST_INTVAL3 = 6,
27 TEST_INTVAL_MANUAL = 101112,
28 TEST_INTVAL_PRE_RELOC = 7,
29 };
30
31 static const struct dm_test_pdata test_pdata[] = {
32 { .ping_add = TEST_INTVAL1, },
33 { .ping_add = TEST_INTVAL2, },
34 { .ping_add = TEST_INTVAL3, },
35 };
36
37 static const struct dm_test_pdata test_pdata_manual = {
38 .ping_add = TEST_INTVAL_MANUAL,
39 };
40
41 static const struct dm_test_pdata test_pdata_pre_reloc = {
42 .ping_add = TEST_INTVAL_PRE_RELOC,
43 };
44
45 U_BOOT_DEVICE(dm_test_info1) = {
46 .name = "test_drv",
47 .platdata = &test_pdata[0],
48 };
49
50 U_BOOT_DEVICE(dm_test_info2) = {
51 .name = "test_drv",
52 .platdata = &test_pdata[1],
53 };
54
55 U_BOOT_DEVICE(dm_test_info3) = {
56 .name = "test_drv",
57 .platdata = &test_pdata[2],
58 };
59
60 static struct driver_info driver_info_manual = {
61 .name = "test_manual_drv",
62 .platdata = &test_pdata_manual,
63 };
64
65 static struct driver_info driver_info_pre_reloc = {
66 .name = "test_pre_reloc_drv",
67 .platdata = &test_pdata_pre_reloc,
68 };
69
70 static struct driver_info driver_info_act_dma = {
71 .name = "test_act_dma_drv",
72 };
73
dm_leak_check_start(struct unit_test_state * uts)74 void dm_leak_check_start(struct unit_test_state *uts)
75 {
76 uts->start = mallinfo();
77 if (!uts->start.uordblks)
78 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
79 }
80
dm_leak_check_end(struct unit_test_state * uts)81 int dm_leak_check_end(struct unit_test_state *uts)
82 {
83 struct mallinfo end;
84 int id, diff;
85
86 /* Don't delete the root class, since we started with that */
87 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
88 struct uclass *uc;
89
90 uc = uclass_find(id);
91 if (!uc)
92 continue;
93 ut_assertok(uclass_destroy(uc));
94 }
95
96 end = mallinfo();
97 diff = end.uordblks - uts->start.uordblks;
98 if (diff > 0)
99 printf("Leak: lost %#xd bytes\n", diff);
100 else if (diff < 0)
101 printf("Leak: gained %#xd bytes\n", -diff);
102 ut_asserteq(uts->start.uordblks, end.uordblks);
103
104 return 0;
105 }
106
107 /* Test that binding with platdata occurs correctly */
dm_test_autobind(struct unit_test_state * uts)108 static int dm_test_autobind(struct unit_test_state *uts)
109 {
110 struct dm_test_state *dms = uts->priv;
111 struct udevice *dev;
112
113 /*
114 * We should have a single class (UCLASS_ROOT) and a single root
115 * device with no children.
116 */
117 ut_assert(dms->root);
118 ut_asserteq(1, list_count_items(&gd->uclass_root));
119 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
120 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
121
122 ut_assertok(dm_scan_platdata(false));
123
124 /* We should have our test class now at least, plus more children */
125 ut_assert(1 < list_count_items(&gd->uclass_root));
126 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
127
128 /* Our 3 dm_test_infox children should be bound to the test uclass */
129 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
130
131 /* No devices should be probed */
132 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
133 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
134
135 /* Our test driver should have been bound 3 times */
136 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
137
138 return 0;
139 }
140 DM_TEST(dm_test_autobind, 0);
141
142 /* Test that binding with uclass platdata allocation occurs correctly */
dm_test_autobind_uclass_pdata_alloc(struct unit_test_state * uts)143 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
144 {
145 struct dm_test_perdev_uc_pdata *uc_pdata;
146 struct udevice *dev;
147 struct uclass *uc;
148
149 ut_assertok(uclass_get(UCLASS_TEST, &uc));
150 ut_assert(uc);
151
152 /**
153 * Test if test uclass driver requires allocation for the uclass
154 * platform data and then check the dev->uclass_platdata pointer.
155 */
156 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
157
158 for (uclass_find_first_device(UCLASS_TEST, &dev);
159 dev;
160 uclass_find_next_device(&dev)) {
161 ut_assert(dev);
162
163 uc_pdata = dev_get_uclass_platdata(dev);
164 ut_assert(uc_pdata);
165 }
166
167 return 0;
168 }
169 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
170
171 /* Test that binding with uclass platdata setting occurs correctly */
dm_test_autobind_uclass_pdata_valid(struct unit_test_state * uts)172 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
173 {
174 struct dm_test_perdev_uc_pdata *uc_pdata;
175 struct udevice *dev;
176
177 /**
178 * In the test_postbind() method of test uclass driver, the uclass
179 * platform data should be set to three test int values - test it.
180 */
181 for (uclass_find_first_device(UCLASS_TEST, &dev);
182 dev;
183 uclass_find_next_device(&dev)) {
184 ut_assert(dev);
185
186 uc_pdata = dev_get_uclass_platdata(dev);
187 ut_assert(uc_pdata);
188 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
189 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
190 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
191 }
192
193 return 0;
194 }
195 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
196
197 /* Test that autoprobe finds all the expected devices */
dm_test_autoprobe(struct unit_test_state * uts)198 static int dm_test_autoprobe(struct unit_test_state *uts)
199 {
200 struct dm_test_state *dms = uts->priv;
201 int expected_base_add;
202 struct udevice *dev;
203 struct uclass *uc;
204 int i;
205
206 ut_assertok(uclass_get(UCLASS_TEST, &uc));
207 ut_assert(uc);
208
209 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
210 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
211 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
212
213 /* The root device should not be activated until needed */
214 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
215
216 /*
217 * We should be able to find the three test devices, and they should
218 * all be activated as they are used (lazy activation, required by
219 * U-Boot)
220 */
221 for (i = 0; i < 3; i++) {
222 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
223 ut_assert(dev);
224 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
225 "Driver %d/%s already activated", i, dev->name);
226
227 /* This should activate it */
228 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
229 ut_assert(dev);
230 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
231
232 /* Activating a device should activate the root device */
233 if (!i)
234 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
235 }
236
237 /*
238 * Our 3 dm_test_info children should be passed to pre_probe and
239 * post_probe
240 */
241 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
242 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
243
244 /* Also we can check the per-device data */
245 expected_base_add = 0;
246 for (i = 0; i < 3; i++) {
247 struct dm_test_uclass_perdev_priv *priv;
248 struct dm_test_pdata *pdata;
249
250 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
251 ut_assert(dev);
252
253 priv = dev_get_uclass_priv(dev);
254 ut_assert(priv);
255 ut_asserteq(expected_base_add, priv->base_add);
256
257 pdata = dev->platdata;
258 expected_base_add += pdata->ping_add;
259 }
260
261 return 0;
262 }
263 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
264
265 /* Check that we see the correct platdata in each device */
dm_test_platdata(struct unit_test_state * uts)266 static int dm_test_platdata(struct unit_test_state *uts)
267 {
268 const struct dm_test_pdata *pdata;
269 struct udevice *dev;
270 int i;
271
272 for (i = 0; i < 3; i++) {
273 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
274 ut_assert(dev);
275 pdata = dev->platdata;
276 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
277 }
278
279 return 0;
280 }
281 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
282
283 /* Test that we can bind, probe, remove, unbind a driver */
dm_test_lifecycle(struct unit_test_state * uts)284 static int dm_test_lifecycle(struct unit_test_state *uts)
285 {
286 struct dm_test_state *dms = uts->priv;
287 int op_count[DM_TEST_OP_COUNT];
288 struct udevice *dev, *test_dev;
289 int pingret;
290 int ret;
291
292 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
293
294 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
295 &dev));
296 ut_assert(dev);
297 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
298 == op_count[DM_TEST_OP_BIND] + 1);
299 ut_assert(!dev->priv);
300
301 /* Probe the device - it should fail allocating private data */
302 dms->force_fail_alloc = 1;
303 ret = device_probe(dev);
304 ut_assert(ret == -ENOMEM);
305 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
306 == op_count[DM_TEST_OP_PROBE] + 1);
307 ut_assert(!dev->priv);
308
309 /* Try again without the alloc failure */
310 dms->force_fail_alloc = 0;
311 ut_assertok(device_probe(dev));
312 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
313 == op_count[DM_TEST_OP_PROBE] + 2);
314 ut_assert(dev->priv);
315
316 /* This should be device 3 in the uclass */
317 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
318 ut_assert(dev == test_dev);
319
320 /* Try ping */
321 ut_assertok(test_ping(dev, 100, &pingret));
322 ut_assert(pingret == 102);
323
324 /* Now remove device 3 */
325 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
326 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
327 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
328
329 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
330 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
331 ut_assertok(device_unbind(dev));
332 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
333 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
334
335 return 0;
336 }
337 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
338
339 /* Test that we can bind/unbind and the lists update correctly */
dm_test_ordering(struct unit_test_state * uts)340 static int dm_test_ordering(struct unit_test_state *uts)
341 {
342 struct dm_test_state *dms = uts->priv;
343 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
344 int pingret;
345
346 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
347 &dev));
348 ut_assert(dev);
349
350 /* Bind two new devices (numbers 4 and 5) */
351 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
352 &dev_penultimate));
353 ut_assert(dev_penultimate);
354 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
355 &dev_last));
356 ut_assert(dev_last);
357
358 /* Now remove device 3 */
359 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
360 ut_assertok(device_unbind(dev));
361
362 /* The device numbering should have shifted down one */
363 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
364 ut_assert(dev_penultimate == test_dev);
365 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
366 ut_assert(dev_last == test_dev);
367
368 /* Add back the original device 3, now in position 5 */
369 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
370 &dev));
371 ut_assert(dev);
372
373 /* Try ping */
374 ut_assertok(test_ping(dev, 100, &pingret));
375 ut_assert(pingret == 102);
376
377 /* Remove 3 and 4 */
378 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
379 ut_assertok(device_unbind(dev_penultimate));
380 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
381 ut_assertok(device_unbind(dev_last));
382
383 /* Our device should now be in position 3 */
384 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
385 ut_assert(dev == test_dev);
386
387 /* Now remove device 3 */
388 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
389 ut_assertok(device_unbind(dev));
390
391 return 0;
392 }
393 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
394
395 /* Check that we can perform operations on a device (do a ping) */
dm_check_operations(struct unit_test_state * uts,struct udevice * dev,uint32_t base,struct dm_test_priv * priv)396 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
397 uint32_t base, struct dm_test_priv *priv)
398 {
399 int expected;
400 int pingret;
401
402 /* Getting the child device should allocate platdata / priv */
403 ut_assertok(testfdt_ping(dev, 10, &pingret));
404 ut_assert(dev->priv);
405 ut_assert(dev->platdata);
406
407 expected = 10 + base;
408 ut_asserteq(expected, pingret);
409
410 /* Do another ping */
411 ut_assertok(testfdt_ping(dev, 20, &pingret));
412 expected = 20 + base;
413 ut_asserteq(expected, pingret);
414
415 /* Now check the ping_total */
416 priv = dev->priv;
417 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
418 priv->ping_total);
419
420 return 0;
421 }
422
423 /* Check that we can perform operations on devices */
dm_test_operations(struct unit_test_state * uts)424 static int dm_test_operations(struct unit_test_state *uts)
425 {
426 struct udevice *dev;
427 int i;
428
429 /*
430 * Now check that the ping adds are what we expect. This is using the
431 * ping-add property in each node.
432 */
433 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
434 uint32_t base;
435
436 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
437
438 /*
439 * Get the 'reg' property, which tells us what the ping add
440 * should be. We don't use the platdata because we want
441 * to test the code that sets that up (testfdt_drv_probe()).
442 */
443 base = test_pdata[i].ping_add;
444 debug("dev=%d, base=%d\n", i, base);
445
446 ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
447 }
448
449 return 0;
450 }
451 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
452
453 /* Remove all drivers and check that things work */
dm_test_remove(struct unit_test_state * uts)454 static int dm_test_remove(struct unit_test_state *uts)
455 {
456 struct udevice *dev;
457 int i;
458
459 for (i = 0; i < 3; i++) {
460 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
461 ut_assert(dev);
462 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
463 "Driver %d/%s not activated", i, dev->name);
464 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
465 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
466 "Driver %d/%s should have deactivated", i,
467 dev->name);
468 ut_assert(!dev->priv);
469 }
470
471 return 0;
472 }
473 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
474
475 /* Remove and recreate everything, check for memory leaks */
dm_test_leak(struct unit_test_state * uts)476 static int dm_test_leak(struct unit_test_state *uts)
477 {
478 int i;
479
480 for (i = 0; i < 2; i++) {
481 struct udevice *dev;
482 int id;
483
484 dm_leak_check_start(uts);
485
486 ut_assertok(dm_scan_platdata(false));
487 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
488
489 /* Scanning the uclass is enough to probe all the devices */
490 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
491 for (uclass_first_device(UCLASS_TEST, &dev); dev;
492 uclass_next_device(&dev))
493 ;
494 }
495
496 ut_assertok(dm_leak_check_end(uts));
497 }
498
499 return 0;
500 }
501 DM_TEST(dm_test_leak, 0);
502
503 /* Test uclass init/destroy methods */
dm_test_uclass(struct unit_test_state * uts)504 static int dm_test_uclass(struct unit_test_state *uts)
505 {
506 struct uclass *uc;
507
508 ut_assertok(uclass_get(UCLASS_TEST, &uc));
509 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
510 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
511 ut_assert(uc->priv);
512
513 ut_assertok(uclass_destroy(uc));
514 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
515 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
516
517 return 0;
518 }
519 DM_TEST(dm_test_uclass, 0);
520
521 /**
522 * create_children() - Create children of a parent node
523 *
524 * @dms: Test system state
525 * @parent: Parent device
526 * @count: Number of children to create
527 * @key: Key value to put in first child. Subsequence children
528 * receive an incrementing value
529 * @child: If not NULL, then the child device pointers are written into
530 * this array.
531 * @return 0 if OK, -ve on error
532 */
create_children(struct unit_test_state * uts,struct udevice * parent,int count,int key,struct udevice * child[])533 static int create_children(struct unit_test_state *uts, struct udevice *parent,
534 int count, int key, struct udevice *child[])
535 {
536 struct udevice *dev;
537 int i;
538
539 for (i = 0; i < count; i++) {
540 struct dm_test_pdata *pdata;
541
542 ut_assertok(device_bind_by_name(parent, false,
543 &driver_info_manual, &dev));
544 pdata = calloc(1, sizeof(*pdata));
545 pdata->ping_add = key + i;
546 dev->platdata = pdata;
547 if (child)
548 child[i] = dev;
549 }
550
551 return 0;
552 }
553
554 #define NODE_COUNT 10
555
dm_test_children(struct unit_test_state * uts)556 static int dm_test_children(struct unit_test_state *uts)
557 {
558 struct dm_test_state *dms = uts->priv;
559 struct udevice *top[NODE_COUNT];
560 struct udevice *child[NODE_COUNT];
561 struct udevice *grandchild[NODE_COUNT];
562 struct udevice *dev;
563 int total;
564 int i;
565
566 /* We don't care about the numbering for this test */
567 dms->skip_post_probe = 1;
568
569 ut_assert(NODE_COUNT > 5);
570
571 /* First create 10 top-level children */
572 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
573
574 /* Now a few have their own children */
575 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
576 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
577
578 /* And grandchildren */
579 for (i = 0; i < NODE_COUNT; i++)
580 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
581 i == 2 ? grandchild : NULL));
582
583 /* Check total number of devices */
584 total = NODE_COUNT * (3 + NODE_COUNT);
585 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
586
587 /* Try probing one of the grandchildren */
588 ut_assertok(uclass_get_device(UCLASS_TEST,
589 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
590 ut_asserteq_ptr(grandchild[0], dev);
591
592 /*
593 * This should have probed the child and top node also, for a total
594 * of 3 nodes.
595 */
596 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
597
598 /* Probe the other grandchildren */
599 for (i = 1; i < NODE_COUNT; i++)
600 ut_assertok(device_probe(grandchild[i]));
601
602 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
603
604 /* Probe everything */
605 for (uclass_first_device(UCLASS_TEST, &dev); dev;
606 uclass_next_device(&dev))
607 ;
608
609 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
610
611 /* Remove a top-level child and check that the children are removed */
612 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
613 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
614 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
615
616 /* Try one with grandchildren */
617 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
618 ut_asserteq_ptr(dev, top[5]);
619 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
620 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
621 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
622
623 /* Try the same with unbind */
624 ut_assertok(device_unbind(top[2]));
625 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
626 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
627
628 /* Try one with grandchildren */
629 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
630 ut_asserteq_ptr(dev, top[6]);
631 ut_assertok(device_unbind(top[5]));
632 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
633 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
634
635 return 0;
636 }
637 DM_TEST(dm_test_children, 0);
638
639 /* Test that pre-relocation devices work as expected */
dm_test_pre_reloc(struct unit_test_state * uts)640 static int dm_test_pre_reloc(struct unit_test_state *uts)
641 {
642 struct dm_test_state *dms = uts->priv;
643 struct udevice *dev;
644
645 /* The normal driver should refuse to bind before relocation */
646 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
647 &driver_info_manual, &dev));
648
649 /* But this one is marked pre-reloc */
650 ut_assertok(device_bind_by_name(dms->root, true,
651 &driver_info_pre_reloc, &dev));
652
653 return 0;
654 }
655 DM_TEST(dm_test_pre_reloc, 0);
656
657 /*
658 * Test that removal of devices, either via the "normal" device_remove()
659 * API or via the device driver selective flag works as expected
660 */
dm_test_remove_active_dma(struct unit_test_state * uts)661 static int dm_test_remove_active_dma(struct unit_test_state *uts)
662 {
663 struct dm_test_state *dms = uts->priv;
664 struct udevice *dev;
665
666 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
667 &dev));
668 ut_assert(dev);
669
670 /* Probe the device */
671 ut_assertok(device_probe(dev));
672
673 /* Test if device is active right now */
674 ut_asserteq(true, device_active(dev));
675
676 /* Remove the device via selective remove flag */
677 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
678
679 /* Test if device is inactive right now */
680 ut_asserteq(false, device_active(dev));
681
682 /* Probe the device again */
683 ut_assertok(device_probe(dev));
684
685 /* Test if device is active right now */
686 ut_asserteq(true, device_active(dev));
687
688 /* Remove the device via "normal" remove API */
689 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
690
691 /* Test if device is inactive right now */
692 ut_asserteq(false, device_active(dev));
693
694 /*
695 * Test if a device without the active DMA flags is not removed upon
696 * the active DMA remove call
697 */
698 ut_assertok(device_unbind(dev));
699 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
700 &dev));
701 ut_assert(dev);
702
703 /* Probe the device */
704 ut_assertok(device_probe(dev));
705
706 /* Test if device is active right now */
707 ut_asserteq(true, device_active(dev));
708
709 /* Remove the device via selective remove flag */
710 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
711
712 /* Test if device is still active right now */
713 ut_asserteq(true, device_active(dev));
714
715 return 0;
716 }
717 DM_TEST(dm_test_remove_active_dma, 0);
718
dm_test_uclass_before_ready(struct unit_test_state * uts)719 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
720 {
721 struct uclass *uc;
722
723 ut_assertok(uclass_get(UCLASS_TEST, &uc));
724
725 gd->dm_root = NULL;
726 gd->dm_root_f = NULL;
727 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
728
729 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
730
731 return 0;
732 }
733 DM_TEST(dm_test_uclass_before_ready, 0);
734
dm_test_uclass_devices_find(struct unit_test_state * uts)735 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
736 {
737 struct udevice *dev;
738 int ret;
739
740 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
741 dev;
742 ret = uclass_find_next_device(&dev)) {
743 ut_assert(!ret);
744 ut_assert(dev);
745 }
746
747 return 0;
748 }
749 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
750
dm_test_uclass_devices_find_by_name(struct unit_test_state * uts)751 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
752 {
753 struct udevice *finddev;
754 struct udevice *testdev;
755 int findret, ret;
756
757 /*
758 * For each test device found in fdt like: "a-test", "b-test", etc.,
759 * use its name and try to find it by uclass_find_device_by_name().
760 * Then, on success check if:
761 * - current 'testdev' name is equal to the returned 'finddev' name
762 * - current 'testdev' pointer is equal to the returned 'finddev'
763 *
764 * We assume that, each uclass's device name is unique, so if not, then
765 * this will fail on checking condition: testdev == finddev, since the
766 * uclass_find_device_by_name(), returns the first device by given name.
767 */
768 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
769 testdev;
770 ret = uclass_find_next_device(&testdev)) {
771 ut_assertok(ret);
772 ut_assert(testdev);
773
774 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
775 testdev->name,
776 &finddev);
777
778 ut_assertok(findret);
779 ut_assert(testdev);
780 ut_asserteq_str(testdev->name, finddev->name);
781 ut_asserteq_ptr(testdev, finddev);
782 }
783
784 return 0;
785 }
786 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
787
dm_test_uclass_devices_get(struct unit_test_state * uts)788 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
789 {
790 struct udevice *dev;
791 int ret;
792
793 for (ret = uclass_first_device_check(UCLASS_TEST, &dev);
794 dev;
795 ret = uclass_next_device_check(&dev)) {
796 ut_assert(!ret);
797 ut_assert(device_active(dev));
798 }
799
800 return 0;
801 }
802 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
803
dm_test_uclass_devices_get_by_name(struct unit_test_state * uts)804 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
805 {
806 struct udevice *finddev;
807 struct udevice *testdev;
808 int ret, findret;
809
810 /*
811 * For each test device found in fdt like: "a-test", "b-test", etc.,
812 * use its name and try to get it by uclass_get_device_by_name().
813 * On success check if:
814 * - returned finddev' is active
815 * - current 'testdev' name is equal to the returned 'finddev' name
816 * - current 'testdev' pointer is equal to the returned 'finddev'
817 *
818 * We asserts that the 'testdev' is active on each loop entry, so we
819 * could be sure that the 'finddev' is activated too, but for sure
820 * we check it again.
821 *
822 * We assume that, each uclass's device name is unique, so if not, then
823 * this will fail on checking condition: testdev == finddev, since the
824 * uclass_get_device_by_name(), returns the first device by given name.
825 */
826 for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev);
827 testdev;
828 ret = uclass_next_device_check(&testdev)) {
829 ut_assertok(ret);
830 ut_assert(device_active(testdev));
831
832 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
833 testdev->name,
834 &finddev);
835
836 ut_assertok(findret);
837 ut_assert(finddev);
838 ut_assert(device_active(finddev));
839 ut_asserteq_str(testdev->name, finddev->name);
840 ut_asserteq_ptr(testdev, finddev);
841 }
842
843 return 0;
844 }
845 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
846
dm_test_device_get_uclass_id(struct unit_test_state * uts)847 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
848 {
849 struct udevice *dev;
850
851 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
852 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
853
854 return 0;
855 }
856 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
857
dm_test_uclass_names(struct unit_test_state * uts)858 static int dm_test_uclass_names(struct unit_test_state *uts)
859 {
860 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
861 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
862
863 return 0;
864 }
865 DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA);
866