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