Merge tag 'efi-2021-04-rc2' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[platform/kernel/u-boot.git] / test / dm / core.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Tests for the core driver model code
4  *
5  * Copyright (c) 2013 Google, Inc
6  */
7
8 #include <common.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <fdtdec.h>
12 #include <log.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/test.h>
20 #include <test/ut.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 enum {
25         TEST_INTVAL1            = 0,
26         TEST_INTVAL2            = 3,
27         TEST_INTVAL3            = 6,
28         TEST_INTVAL_MANUAL      = 101112,
29         TEST_INTVAL_PRE_RELOC   = 7,
30 };
31
32 static const struct dm_test_pdata test_pdata[] = {
33         { .ping_add             = TEST_INTVAL1, },
34         { .ping_add             = TEST_INTVAL2, },
35         { .ping_add             = TEST_INTVAL3, },
36 };
37
38 static const struct dm_test_pdata test_pdata_manual = {
39         .ping_add               = TEST_INTVAL_MANUAL,
40 };
41
42 static const struct dm_test_pdata test_pdata_pre_reloc = {
43         .ping_add               = TEST_INTVAL_PRE_RELOC,
44 };
45
46 U_BOOT_DRVINFO(dm_test_info1) = {
47         .name = "test_drv",
48         .plat = &test_pdata[0],
49 };
50
51 U_BOOT_DRVINFO(dm_test_info2) = {
52         .name = "test_drv",
53         .plat = &test_pdata[1],
54 };
55
56 U_BOOT_DRVINFO(dm_test_info3) = {
57         .name = "test_drv",
58         .plat = &test_pdata[2],
59 };
60
61 static struct driver_info driver_info_manual = {
62         .name = "test_manual_drv",
63         .plat = &test_pdata_manual,
64 };
65
66 static struct driver_info driver_info_pre_reloc = {
67         .name = "test_pre_reloc_drv",
68         .plat = &test_pdata_pre_reloc,
69 };
70
71 static struct driver_info driver_info_act_dma = {
72         .name = "test_act_dma_drv",
73 };
74
75 static struct driver_info driver_info_vital_clk = {
76         .name = "test_vital_clk_drv",
77 };
78
79 static struct driver_info driver_info_act_dma_vital_clk = {
80         .name = "test_act_dma_vital_clk_drv",
81 };
82
83 void dm_leak_check_start(struct unit_test_state *uts)
84 {
85         uts->start = mallinfo();
86         if (!uts->start.uordblks)
87                 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
88 }
89
90 int dm_leak_check_end(struct unit_test_state *uts)
91 {
92         struct mallinfo end;
93         int id, diff;
94
95         /* Don't delete the root class, since we started with that */
96         for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
97                 struct uclass *uc;
98
99                 uc = uclass_find(id);
100                 if (!uc)
101                         continue;
102                 ut_assertok(uclass_destroy(uc));
103         }
104
105         end = mallinfo();
106         diff = end.uordblks - uts->start.uordblks;
107         if (diff > 0)
108                 printf("Leak: lost %#xd bytes\n", diff);
109         else if (diff < 0)
110                 printf("Leak: gained %#xd bytes\n", -diff);
111         ut_asserteq(uts->start.uordblks, end.uordblks);
112
113         return 0;
114 }
115
116 /* Test that binding with plat occurs correctly */
117 static int dm_test_autobind(struct unit_test_state *uts)
118 {
119         struct dm_test_state *dms = uts->priv;
120         struct udevice *dev;
121
122         /*
123          * We should have a single class (UCLASS_ROOT) and a single root
124          * device with no children.
125          */
126         ut_assert(dms->root);
127         ut_asserteq(1, list_count_items(gd->uclass_root));
128         ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
129         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
130
131         ut_assertok(dm_scan_plat(false));
132
133         /* We should have our test class now at least, plus more children */
134         ut_assert(1 < list_count_items(gd->uclass_root));
135         ut_assert(0 < list_count_items(&gd->dm_root->child_head));
136
137         /* Our 3 dm_test_infox children should be bound to the test uclass */
138         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
139
140         /* No devices should be probed */
141         list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
142                 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
143
144         /* Our test driver should have been bound 3 times */
145         ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
146
147         return 0;
148 }
149 DM_TEST(dm_test_autobind, 0);
150
151 /* Test that binding with uclass plat allocation occurs correctly */
152 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
153 {
154         struct dm_test_perdev_uc_pdata *uc_pdata;
155         struct udevice *dev;
156         struct uclass *uc;
157
158         ut_assertok(uclass_get(UCLASS_TEST, &uc));
159         ut_assert(uc);
160
161         /**
162          * Test if test uclass driver requires allocation for the uclass
163          * platform data and then check the dev->uclass_plat pointer.
164          */
165         ut_assert(uc->uc_drv->per_device_plat_auto);
166
167         for (uclass_find_first_device(UCLASS_TEST, &dev);
168              dev;
169              uclass_find_next_device(&dev)) {
170                 ut_assertnonnull(dev);
171
172                 uc_pdata = dev_get_uclass_plat(dev);
173                 ut_assert(uc_pdata);
174         }
175
176         return 0;
177 }
178 DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA);
179
180 /* Test that binding with uclass plat setting occurs correctly */
181 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
182 {
183         struct dm_test_perdev_uc_pdata *uc_pdata;
184         struct udevice *dev;
185
186         /**
187          * In the test_postbind() method of test uclass driver, the uclass
188          * platform data should be set to three test int values - test it.
189          */
190         for (uclass_find_first_device(UCLASS_TEST, &dev);
191              dev;
192              uclass_find_next_device(&dev)) {
193                 ut_assertnonnull(dev);
194
195                 uc_pdata = dev_get_uclass_plat(dev);
196                 ut_assert(uc_pdata);
197                 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
198                 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
199                 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
200         }
201
202         return 0;
203 }
204 DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
205
206 /* Test that autoprobe finds all the expected devices */
207 static int dm_test_autoprobe(struct unit_test_state *uts)
208 {
209         struct dm_test_state *dms = uts->priv;
210         int expected_base_add;
211         struct udevice *dev;
212         struct uclass *uc;
213         int i;
214
215         ut_assertok(uclass_get(UCLASS_TEST, &uc));
216         ut_assert(uc);
217
218         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
219         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
220         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
221
222         /* The root device should not be activated until needed */
223         ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
224
225         /*
226          * We should be able to find the three test devices, and they should
227          * all be activated as they are used (lazy activation, required by
228          * U-Boot)
229          */
230         for (i = 0; i < 3; i++) {
231                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
232                 ut_assert(dev);
233                 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
234                            "Driver %d/%s already activated", i, dev->name);
235
236                 /* This should activate it */
237                 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
238                 ut_assert(dev);
239                 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
240
241                 /* Activating a device should activate the root device */
242                 if (!i)
243                         ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
244         }
245
246         /*
247          * Our 3 dm_test_info children should be passed to pre_probe and
248          * post_probe
249          */
250         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
251         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
252
253         /* Also we can check the per-device data */
254         expected_base_add = 0;
255         for (i = 0; i < 3; i++) {
256                 struct dm_test_uclass_perdev_priv *priv;
257                 struct dm_test_pdata *pdata;
258
259                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
260                 ut_assert(dev);
261
262                 priv = dev_get_uclass_priv(dev);
263                 ut_assert(priv);
264                 ut_asserteq(expected_base_add, priv->base_add);
265
266                 pdata = dev_get_plat(dev);
267                 expected_base_add += pdata->ping_add;
268         }
269
270         return 0;
271 }
272 DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
273
274 /* Check that we see the correct plat in each device */
275 static int dm_test_plat(struct unit_test_state *uts)
276 {
277         const struct dm_test_pdata *pdata;
278         struct udevice *dev;
279         int i;
280
281         for (i = 0; i < 3; i++) {
282                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
283                 ut_assert(dev);
284                 pdata = dev_get_plat(dev);
285                 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
286         }
287
288         return 0;
289 }
290 DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
291
292 /* Test that we can bind, probe, remove, unbind a driver */
293 static int dm_test_lifecycle(struct unit_test_state *uts)
294 {
295         struct dm_test_state *dms = uts->priv;
296         int op_count[DM_TEST_OP_COUNT];
297         struct udevice *dev, *test_dev;
298         int pingret;
299         int ret;
300
301         memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
302
303         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
304                                         &dev));
305         ut_assert(dev);
306         ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
307                         == op_count[DM_TEST_OP_BIND] + 1);
308         ut_assert(!dev_get_priv(dev));
309
310         /* Probe the device - it should fail allocating private data */
311         dms->force_fail_alloc = 1;
312         ret = device_probe(dev);
313         ut_assert(ret == -ENOMEM);
314         ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
315                         == op_count[DM_TEST_OP_PROBE] + 1);
316         ut_assert(!dev_get_priv(dev));
317
318         /* Try again without the alloc failure */
319         dms->force_fail_alloc = 0;
320         ut_assertok(device_probe(dev));
321         ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
322                         == op_count[DM_TEST_OP_PROBE] + 2);
323         ut_assert(dev_get_priv(dev));
324
325         /* This should be device 3 in the uclass */
326         ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
327         ut_assert(dev == test_dev);
328
329         /* Try ping */
330         ut_assertok(test_ping(dev, 100, &pingret));
331         ut_assert(pingret == 102);
332
333         /* Now remove device 3 */
334         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
335         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
336         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
337
338         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
339         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
340         ut_assertok(device_unbind(dev));
341         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
342         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
343
344         return 0;
345 }
346 DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
347
348 /* Test that we can bind/unbind and the lists update correctly */
349 static int dm_test_ordering(struct unit_test_state *uts)
350 {
351         struct dm_test_state *dms = uts->priv;
352         struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
353         int pingret;
354
355         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
356                                         &dev));
357         ut_assert(dev);
358
359         /* Bind two new devices (numbers 4 and 5) */
360         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
361                                         &dev_penultimate));
362         ut_assert(dev_penultimate);
363         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
364                                         &dev_last));
365         ut_assert(dev_last);
366
367         /* Now remove device 3 */
368         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
369         ut_assertok(device_unbind(dev));
370
371         /* The device numbering should have shifted down one */
372         ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
373         ut_assert(dev_penultimate == test_dev);
374         ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
375         ut_assert(dev_last == test_dev);
376
377         /* Add back the original device 3, now in position 5 */
378         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
379                                         &dev));
380         ut_assert(dev);
381
382         /* Try ping */
383         ut_assertok(test_ping(dev, 100, &pingret));
384         ut_assert(pingret == 102);
385
386         /* Remove 3 and 4 */
387         ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
388         ut_assertok(device_unbind(dev_penultimate));
389         ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
390         ut_assertok(device_unbind(dev_last));
391
392         /* Our device should now be in position 3 */
393         ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
394         ut_assert(dev == test_dev);
395
396         /* Now remove device 3 */
397         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
398         ut_assertok(device_unbind(dev));
399
400         return 0;
401 }
402 DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
403
404 /* Check that we can perform operations on a device (do a ping) */
405 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
406                         uint32_t base, struct dm_test_priv *priv)
407 {
408         int expected;
409         int pingret;
410
411         /* Getting the child device should allocate plat / priv */
412         ut_assertok(testfdt_ping(dev, 10, &pingret));
413         ut_assert(dev_get_priv(dev));
414         ut_assert(dev_get_plat(dev));
415
416         expected = 10 + base;
417         ut_asserteq(expected, pingret);
418
419         /* Do another ping */
420         ut_assertok(testfdt_ping(dev, 20, &pingret));
421         expected = 20 + base;
422         ut_asserteq(expected, pingret);
423
424         /* Now check the ping_total */
425         priv = dev_get_priv(dev);
426         ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
427                     priv->ping_total);
428
429         return 0;
430 }
431
432 /* Check that we can perform operations on devices */
433 static int dm_test_operations(struct unit_test_state *uts)
434 {
435         struct udevice *dev;
436         int i;
437
438         /*
439          * Now check that the ping adds are what we expect. This is using the
440          * ping-add property in each node.
441          */
442         for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
443                 uint32_t base;
444
445                 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
446
447                 /*
448                  * Get the 'reg' property, which tells us what the ping add
449                  * should be. We don't use the plat because we want
450                  * to test the code that sets that up (testfdt_drv_probe()).
451                  */
452                 base = test_pdata[i].ping_add;
453                 debug("dev=%d, base=%d\n", i, base);
454
455                 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
456         }
457
458         return 0;
459 }
460 DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
461
462 /* Remove all drivers and check that things work */
463 static int dm_test_remove(struct unit_test_state *uts)
464 {
465         struct udevice *dev;
466         int i;
467
468         for (i = 0; i < 3; i++) {
469                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
470                 ut_assert(dev);
471                 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
472                            "Driver %d/%s not activated", i, dev->name);
473                 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
474                 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
475                            "Driver %d/%s should have deactivated", i,
476                            dev->name);
477                 ut_assert(!dev_get_priv(dev));
478         }
479
480         return 0;
481 }
482 DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
483
484 /* Remove and recreate everything, check for memory leaks */
485 static int dm_test_leak(struct unit_test_state *uts)
486 {
487         int i;
488
489         for (i = 0; i < 2; i++) {
490                 struct udevice *dev;
491                 int ret;
492                 int id;
493
494                 dm_leak_check_start(uts);
495
496                 ut_assertok(dm_scan_plat(false));
497                 ut_assertok(dm_scan_fdt(false));
498
499                 /* Scanning the uclass is enough to probe all the devices */
500                 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
501                         for (ret = uclass_first_device(UCLASS_TEST, &dev);
502                              dev;
503                              ret = uclass_next_device(&dev))
504                                 ;
505                         ut_assertok(ret);
506                 }
507
508                 ut_assertok(dm_leak_check_end(uts));
509         }
510
511         return 0;
512 }
513 DM_TEST(dm_test_leak, 0);
514
515 /* Test uclass init/destroy methods */
516 static int dm_test_uclass(struct unit_test_state *uts)
517 {
518         struct uclass *uc;
519
520         ut_assertok(uclass_get(UCLASS_TEST, &uc));
521         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
522         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
523         ut_assert(uclass_get_priv(uc));
524
525         ut_assertok(uclass_destroy(uc));
526         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
527         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
528
529         return 0;
530 }
531 DM_TEST(dm_test_uclass, 0);
532
533 /**
534  * create_children() - Create children of a parent node
535  *
536  * @dms:        Test system state
537  * @parent:     Parent device
538  * @count:      Number of children to create
539  * @key:        Key value to put in first child. Subsequence children
540  *              receive an incrementing value
541  * @child:      If not NULL, then the child device pointers are written into
542  *              this array.
543  * @return 0 if OK, -ve on error
544  */
545 static int create_children(struct unit_test_state *uts, struct udevice *parent,
546                            int count, int key, struct udevice *child[])
547 {
548         struct udevice *dev;
549         int i;
550
551         for (i = 0; i < count; i++) {
552                 struct dm_test_pdata *pdata;
553
554                 ut_assertok(device_bind_by_name(parent, false,
555                                                 &driver_info_manual, &dev));
556                 pdata = calloc(1, sizeof(*pdata));
557                 pdata->ping_add = key + i;
558                 dev_set_plat(dev, pdata);
559                 if (child)
560                         child[i] = dev;
561         }
562
563         return 0;
564 }
565
566 #define NODE_COUNT      10
567
568 static int dm_test_children(struct unit_test_state *uts)
569 {
570         struct dm_test_state *dms = uts->priv;
571         struct udevice *top[NODE_COUNT];
572         struct udevice *child[NODE_COUNT];
573         struct udevice *grandchild[NODE_COUNT];
574         struct udevice *dev;
575         int total;
576         int ret;
577         int i;
578
579         /* We don't care about the numbering for this test */
580         dms->skip_post_probe = 1;
581
582         ut_assert(NODE_COUNT > 5);
583
584         /* First create 10 top-level children */
585         ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
586
587         /* Now a few have their own children */
588         ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
589         ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
590
591         /* And grandchildren */
592         for (i = 0; i < NODE_COUNT; i++)
593                 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
594                                             i == 2 ? grandchild : NULL));
595
596         /* Check total number of devices */
597         total = NODE_COUNT * (3 + NODE_COUNT);
598         ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
599
600         /* Try probing one of the grandchildren */
601         ut_assertok(uclass_get_device(UCLASS_TEST,
602                                       NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
603         ut_asserteq_ptr(grandchild[0], dev);
604
605         /*
606          * This should have probed the child and top node also, for a total
607          * of 3 nodes.
608          */
609         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
610
611         /* Probe the other grandchildren */
612         for (i = 1; i < NODE_COUNT; i++)
613                 ut_assertok(device_probe(grandchild[i]));
614
615         ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
616
617         /* Probe everything */
618         for (ret = uclass_first_device(UCLASS_TEST, &dev);
619              dev;
620              ret = uclass_next_device(&dev))
621                 ;
622         ut_assertok(ret);
623
624         ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
625
626         /* Remove a top-level child and check that the children are removed */
627         ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
628         ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
629         dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
630
631         /* Try one with grandchildren */
632         ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
633         ut_asserteq_ptr(dev, top[5]);
634         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
635         ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
636                     dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
637
638         /* Try the same with unbind */
639         ut_assertok(device_unbind(top[2]));
640         ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
641         dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
642
643         /* Try one with grandchildren */
644         ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
645         ut_asserteq_ptr(dev, top[6]);
646         ut_assertok(device_unbind(top[5]));
647         ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
648                     dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
649
650         return 0;
651 }
652 DM_TEST(dm_test_children, 0);
653
654 static int dm_test_device_reparent(struct unit_test_state *uts)
655 {
656         struct dm_test_state *dms = uts->priv;
657         struct udevice *top[NODE_COUNT];
658         struct udevice *child[NODE_COUNT];
659         struct udevice *grandchild[NODE_COUNT];
660         struct udevice *dev;
661         int total;
662         int ret;
663         int i;
664
665         /* We don't care about the numbering for this test */
666         dms->skip_post_probe = 1;
667
668         ut_assert(NODE_COUNT > 5);
669
670         /* First create 10 top-level children */
671         ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
672
673         /* Now a few have their own children */
674         ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
675         ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
676
677         /* And grandchildren */
678         for (i = 0; i < NODE_COUNT; i++)
679                 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
680                                             i == 2 ? grandchild : NULL));
681
682         /* Check total number of devices */
683         total = NODE_COUNT * (3 + NODE_COUNT);
684         ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
685
686         /* Probe everything */
687         for (i = 0; i < total; i++)
688                 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
689
690         /* Re-parent top-level children with no grandchildren. */
691         ut_assertok(device_reparent(top[3], top[0]));
692         /* try to get devices */
693         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
694              dev;
695              ret = uclass_find_next_device(&dev)) {
696                 ut_assert(!ret);
697                 ut_assertnonnull(dev);
698         }
699
700         ut_assertok(device_reparent(top[4], top[0]));
701         /* try to get devices */
702         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
703              dev;
704              ret = uclass_find_next_device(&dev)) {
705                 ut_assert(!ret);
706                 ut_assertnonnull(dev);
707         }
708
709         /* Re-parent top-level children with grandchildren. */
710         ut_assertok(device_reparent(top[2], top[0]));
711         /* try to get devices */
712         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
713              dev;
714              ret = uclass_find_next_device(&dev)) {
715                 ut_assert(!ret);
716                 ut_assertnonnull(dev);
717         }
718
719         ut_assertok(device_reparent(top[5], top[2]));
720         /* try to get devices */
721         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
722              dev;
723              ret = uclass_find_next_device(&dev)) {
724                 ut_assert(!ret);
725                 ut_assertnonnull(dev);
726         }
727
728         /* Re-parent grandchildren. */
729         ut_assertok(device_reparent(grandchild[0], top[1]));
730         /* try to get devices */
731         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
732              dev;
733              ret = uclass_find_next_device(&dev)) {
734                 ut_assert(!ret);
735                 ut_assertnonnull(dev);
736         }
737
738         ut_assertok(device_reparent(grandchild[1], top[1]));
739         /* try to get devices */
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_assertnonnull(dev);
745         }
746
747         /* Remove re-pareneted devices. */
748         ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
749         /* try to get devices */
750         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
751              dev;
752              ret = uclass_find_next_device(&dev)) {
753                 ut_assert(!ret);
754                 ut_assertnonnull(dev);
755         }
756
757         ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
758         /* try to get devices */
759         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
760              dev;
761              ret = uclass_find_next_device(&dev)) {
762                 ut_assert(!ret);
763                 ut_assertnonnull(dev);
764         }
765
766         ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
767         /* try to get devices */
768         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
769              dev;
770              ret = uclass_find_next_device(&dev)) {
771                 ut_assert(!ret);
772                 ut_assertnonnull(dev);
773         }
774
775         ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
776         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
777              dev;
778              ret = uclass_find_next_device(&dev)) {
779                 ut_assert(!ret);
780                 ut_assertnonnull(dev);
781         }
782
783         ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
784         /* try to get devices */
785         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
786              dev;
787              ret = uclass_find_next_device(&dev)) {
788                 ut_assert(!ret);
789                 ut_assertnonnull(dev);
790         }
791
792         ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
793         /* try to get devices */
794         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
795              dev;
796              ret = uclass_find_next_device(&dev)) {
797                 ut_assert(!ret);
798                 ut_assertnonnull(dev);
799         }
800
801         /* Try the same with unbind */
802         ut_assertok(device_unbind(top[3]));
803         ut_assertok(device_unbind(top[4]));
804         ut_assertok(device_unbind(top[5]));
805         ut_assertok(device_unbind(top[2]));
806
807         ut_assertok(device_unbind(grandchild[0]));
808         ut_assertok(device_unbind(grandchild[1]));
809
810         return 0;
811 }
812 DM_TEST(dm_test_device_reparent, 0);
813
814 /* Test that pre-relocation devices work as expected */
815 static int dm_test_pre_reloc(struct unit_test_state *uts)
816 {
817         struct dm_test_state *dms = uts->priv;
818         struct udevice *dev;
819
820         /* The normal driver should refuse to bind before relocation */
821         ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
822                                                 &driver_info_manual, &dev));
823
824         /* But this one is marked pre-reloc */
825         ut_assertok(device_bind_by_name(dms->root, true,
826                                         &driver_info_pre_reloc, &dev));
827
828         return 0;
829 }
830 DM_TEST(dm_test_pre_reloc, 0);
831
832 /*
833  * Test that removal of devices, either via the "normal" device_remove()
834  * API or via the device driver selective flag works as expected
835  */
836 static int dm_test_remove_active_dma(struct unit_test_state *uts)
837 {
838         struct dm_test_state *dms = uts->priv;
839         struct udevice *dev;
840
841         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
842                                         &dev));
843         ut_assert(dev);
844
845         /* Probe the device */
846         ut_assertok(device_probe(dev));
847
848         /* Test if device is active right now */
849         ut_asserteq(true, device_active(dev));
850
851         /* Remove the device via selective remove flag */
852         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
853
854         /* Test if device is inactive right now */
855         ut_asserteq(false, device_active(dev));
856
857         /* Probe the device again */
858         ut_assertok(device_probe(dev));
859
860         /* Test if device is active right now */
861         ut_asserteq(true, device_active(dev));
862
863         /* Remove the device via "normal" remove API */
864         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
865
866         /* Test if device is inactive right now */
867         ut_asserteq(false, device_active(dev));
868
869         /*
870          * Test if a device without the active DMA flags is not removed upon
871          * the active DMA remove call
872          */
873         ut_assertok(device_unbind(dev));
874         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
875                                         &dev));
876         ut_assert(dev);
877
878         /* Probe the device */
879         ut_assertok(device_probe(dev));
880
881         /* Test if device is active right now */
882         ut_asserteq(true, device_active(dev));
883
884         /* Remove the device via selective remove flag */
885         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
886
887         /* Test if device is still active right now */
888         ut_asserteq(true, device_active(dev));
889
890         return 0;
891 }
892 DM_TEST(dm_test_remove_active_dma, 0);
893
894 /* Test removal of 'vital' devices */
895 static int dm_test_remove_vital(struct unit_test_state *uts)
896 {
897         struct dm_test_state *dms = uts->priv;
898         struct udevice *normal, *dma, *vital, *dma_vital;
899
900         /* Skip the behaviour in test_post_probe() */
901         dms->skip_post_probe = 1;
902
903         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
904                                         &normal));
905         ut_assertnonnull(normal);
906
907         ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
908                                         &dma));
909         ut_assertnonnull(dma);
910
911         ut_assertok(device_bind_by_name(dms->root, false,
912                                         &driver_info_vital_clk, &vital));
913         ut_assertnonnull(vital);
914
915         ut_assertok(device_bind_by_name(dms->root, false,
916                                         &driver_info_act_dma_vital_clk,
917                                         &dma_vital));
918         ut_assertnonnull(dma_vital);
919
920         /* Probe the devices */
921         ut_assertok(device_probe(normal));
922         ut_assertok(device_probe(dma));
923         ut_assertok(device_probe(vital));
924         ut_assertok(device_probe(dma_vital));
925
926         /* Check that devices are active right now */
927         ut_asserteq(true, device_active(normal));
928         ut_asserteq(true, device_active(dma));
929         ut_asserteq(true, device_active(vital));
930         ut_asserteq(true, device_active(dma_vital));
931
932         /* Remove active devices via selective remove flag */
933         dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
934
935         /*
936          * Check that this only has an effect on the dma device, since two
937          * devices are vital and the third does not have active DMA
938          */
939         ut_asserteq(true, device_active(normal));
940         ut_asserteq(false, device_active(dma));
941         ut_asserteq(true, device_active(vital));
942         ut_asserteq(true, device_active(dma_vital));
943
944         /* Remove active devices via selective remove flag */
945         ut_assertok(device_probe(dma));
946         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
947
948         /* This should have affected both active-dma devices */
949         ut_asserteq(true, device_active(normal));
950         ut_asserteq(false, device_active(dma));
951         ut_asserteq(true, device_active(vital));
952         ut_asserteq(false, device_active(dma_vital));
953
954         /* Remove non-vital devices */
955         ut_assertok(device_probe(dma));
956         ut_assertok(device_probe(dma_vital));
957         dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
958
959         /* This should have affected only non-vital devices */
960         ut_asserteq(false, device_active(normal));
961         ut_asserteq(false, device_active(dma));
962         ut_asserteq(true, device_active(vital));
963         ut_asserteq(true, device_active(dma_vital));
964
965         /* Remove vital devices via normal remove flag */
966         ut_assertok(device_probe(normal));
967         ut_assertok(device_probe(dma));
968         dm_remove_devices_flags(DM_REMOVE_NORMAL);
969
970         /* Check that all devices are inactive right now */
971         ut_asserteq(false, device_active(normal));
972         ut_asserteq(false, device_active(dma));
973         ut_asserteq(false, device_active(vital));
974         ut_asserteq(false, device_active(dma_vital));
975
976         return 0;
977 }
978 DM_TEST(dm_test_remove_vital, 0);
979
980 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
981 {
982         struct uclass *uc;
983
984         ut_assertok(uclass_get(UCLASS_TEST, &uc));
985
986         gd->dm_root = NULL;
987         gd->dm_root_f = NULL;
988         memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
989
990         ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
991
992         return 0;
993 }
994 DM_TEST(dm_test_uclass_before_ready, 0);
995
996 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
997 {
998         struct udevice *dev;
999         int ret;
1000
1001         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1002              dev;
1003              ret = uclass_find_next_device(&dev)) {
1004                 ut_assert(!ret);
1005                 ut_assertnonnull(dev);
1006         }
1007
1008         ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1009         ut_assertnull(dev);
1010
1011         return 0;
1012 }
1013 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1014
1015 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1016 {
1017         struct udevice *finddev;
1018         struct udevice *testdev;
1019         int findret, ret;
1020
1021         /*
1022          * For each test device found in fdt like: "a-test", "b-test", etc.,
1023          * use its name and try to find it by uclass_find_device_by_name().
1024          * Then, on success check if:
1025          * - current 'testdev' name is equal to the returned 'finddev' name
1026          * - current 'testdev' pointer is equal to the returned 'finddev'
1027          *
1028          * We assume that, each uclass's device name is unique, so if not, then
1029          * this will fail on checking condition: testdev == finddev, since the
1030          * uclass_find_device_by_name(), returns the first device by given name.
1031         */
1032         for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1033              testdev;
1034              ret = uclass_find_next_device(&testdev)) {
1035                 ut_assertok(ret);
1036                 ut_assertnonnull(testdev);
1037
1038                 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1039                                                      testdev->name,
1040                                                      &finddev);
1041
1042                 ut_assertok(findret);
1043                 ut_assert(testdev);
1044                 ut_asserteq_str(testdev->name, finddev->name);
1045                 ut_asserteq_ptr(testdev, finddev);
1046         }
1047
1048         return 0;
1049 }
1050 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1051
1052 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1053 {
1054         struct udevice *dev;
1055         int ret;
1056
1057         for (ret = uclass_first_device(UCLASS_TEST, &dev);
1058              dev;
1059              ret = uclass_next_device(&dev)) {
1060                 ut_assert(!ret);
1061                 ut_assert(dev);
1062                 ut_assert(device_active(dev));
1063         }
1064
1065         return 0;
1066 }
1067 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1068
1069 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1070 {
1071         struct udevice *finddev;
1072         struct udevice *testdev;
1073         int ret, findret;
1074
1075         /*
1076          * For each test device found in fdt like: "a-test", "b-test", etc.,
1077          * use its name and try to get it by uclass_get_device_by_name().
1078          * On success check if:
1079          * - returned finddev' is active
1080          * - current 'testdev' name is equal to the returned 'finddev' name
1081          * - current 'testdev' pointer is equal to the returned 'finddev'
1082          *
1083          * We asserts that the 'testdev' is active on each loop entry, so we
1084          * could be sure that the 'finddev' is activated too, but for sure
1085          * we check it again.
1086          *
1087          * We assume that, each uclass's device name is unique, so if not, then
1088          * this will fail on checking condition: testdev == finddev, since the
1089          * uclass_get_device_by_name(), returns the first device by given name.
1090         */
1091         for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
1092              testdev;
1093              ret = uclass_next_device(&testdev)) {
1094                 ut_assertok(ret);
1095                 ut_assert(testdev);
1096                 ut_assert(device_active(testdev));
1097
1098                 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1099                                                     testdev->name,
1100                                                     &finddev);
1101
1102                 ut_assertok(findret);
1103                 ut_assert(finddev);
1104                 ut_assert(device_active(finddev));
1105                 ut_asserteq_str(testdev->name, finddev->name);
1106                 ut_asserteq_ptr(testdev, finddev);
1107         }
1108
1109         return 0;
1110 }
1111 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1112
1113 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1114 {
1115         struct udevice *dev;
1116
1117         ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1118         ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1119
1120         return 0;
1121 }
1122 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1123
1124 static int dm_test_uclass_names(struct unit_test_state *uts)
1125 {
1126         ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1127         ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1128
1129         return 0;
1130 }
1131 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1132
1133 static int dm_test_inactive_child(struct unit_test_state *uts)
1134 {
1135         struct dm_test_state *dms = uts->priv;
1136         struct udevice *parent, *dev1, *dev2;
1137
1138         /* Skip the behaviour in test_post_probe() */
1139         dms->skip_post_probe = 1;
1140
1141         ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1142
1143         /*
1144          * Create a child but do not activate it. Calling the function again
1145          * should return the same child.
1146          */
1147         ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1148                                                         UCLASS_TEST, &dev1));
1149         ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1150                                 "test_child", 0, ofnode_null(), &dev1));
1151
1152         ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1153                                                      &dev2));
1154         ut_asserteq_ptr(dev1, dev2);
1155
1156         ut_assertok(device_probe(dev1));
1157         ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1158                                                         UCLASS_TEST, &dev2));
1159
1160         return 0;
1161 }
1162 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1163
1164 /* Make sure all bound devices have a sequence number */
1165 static int dm_test_all_have_seq(struct unit_test_state *uts)
1166 {
1167         struct udevice *dev;
1168         struct uclass *uc;
1169
1170         list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1171                 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1172                         if (dev->seq_ == -1)
1173                                 printf("Device '%s' has no seq (%d)\n",
1174                                        dev->name, dev->seq_);
1175                         ut_assert(dev->seq_ != -1);
1176                 }
1177         }
1178
1179         return 0;
1180 }
1181 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);