]> Git Repo - J-u-boot.git/blob - test/dm/core.c
global_data.h: drop write-only field dm_root_f
[J-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 <errno.h>
9 #include <dm.h>
10 #include <fdtdec.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <asm/global_data.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 udevice *dev;
120
121         /*
122          * We should have a single class (UCLASS_ROOT) and a single root
123          * device with no children.
124          */
125         ut_assert(uts->root);
126         ut_asserteq(1, list_count_items(gd->uclass_root));
127         ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
128         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
129
130         ut_assertok(dm_scan_plat(false));
131
132         /* We should have our test class now at least, plus more children */
133         ut_assert(1 < list_count_items(gd->uclass_root));
134         ut_assert(0 < list_count_items(&gd->dm_root->child_head));
135
136         /* Our 3 dm_test_infox children should be bound to the test uclass */
137         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
138
139         /* No devices should be probed */
140         list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
141                 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
142
143         /* Our test driver should have been bound 3 times */
144         ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
145
146         return 0;
147 }
148 DM_TEST(dm_test_autobind, 0);
149
150 /* Test that binding with uclass plat allocation occurs correctly */
151 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
152 {
153         struct dm_test_perdev_uc_pdata *uc_pdata;
154         struct udevice *dev;
155         struct uclass *uc;
156
157         ut_assertok(uclass_get(UCLASS_TEST, &uc));
158         ut_assert(uc);
159
160         /**
161          * Test if test uclass driver requires allocation for the uclass
162          * platform data and then check the dev->uclass_plat pointer.
163          */
164         ut_assert(uc->uc_drv->per_device_plat_auto);
165
166         for (uclass_find_first_device(UCLASS_TEST, &dev);
167              dev;
168              uclass_find_next_device(&dev)) {
169                 ut_assertnonnull(dev);
170
171                 uc_pdata = dev_get_uclass_plat(dev);
172                 ut_assert(uc_pdata);
173         }
174
175         return 0;
176 }
177 DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA);
178
179 /* compare node names ignoring the unit address */
180 static int dm_test_compare_node_name(struct unit_test_state *uts)
181 {
182         ofnode node;
183
184         node = ofnode_path("/mmio-bus@0");
185         ut_assert(ofnode_valid(node));
186         ut_assert(ofnode_name_eq(node, "mmio-bus"));
187
188         return 0;
189 }
190
191 DM_TEST(dm_test_compare_node_name, UT_TESTF_SCAN_PDATA);
192
193 /* Test that binding with uclass plat setting occurs correctly */
194 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
195 {
196         struct dm_test_perdev_uc_pdata *uc_pdata;
197         struct udevice *dev;
198
199         /**
200          * In the test_postbind() method of test uclass driver, the uclass
201          * platform data should be set to three test int values - test it.
202          */
203         for (uclass_find_first_device(UCLASS_TEST, &dev);
204              dev;
205              uclass_find_next_device(&dev)) {
206                 ut_assertnonnull(dev);
207
208                 uc_pdata = dev_get_uclass_plat(dev);
209                 ut_assert(uc_pdata);
210                 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
211                 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
212                 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
213         }
214
215         return 0;
216 }
217 DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
218
219 /* Test that autoprobe finds all the expected devices */
220 static int dm_test_autoprobe(struct unit_test_state *uts)
221 {
222         int expected_base_add;
223         struct udevice *dev;
224         struct uclass *uc;
225         int i;
226
227         ut_assertok(uclass_get(UCLASS_TEST, &uc));
228         ut_assert(uc);
229
230         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
231         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
232         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
233
234         /* The root device should not be activated until needed */
235         ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
236
237         /*
238          * We should be able to find the three test devices, and they should
239          * all be activated as they are used (lazy activation, required by
240          * U-Boot)
241          */
242         for (i = 0; i < 3; i++) {
243                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
244                 ut_assert(dev);
245                 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
246                            "Driver %d/%s already activated", i, dev->name);
247
248                 /* This should activate it */
249                 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
250                 ut_assert(dev);
251                 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
252
253                 /* Activating a device should activate the root device */
254                 if (!i)
255                         ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
256         }
257
258         /*
259          * Our 3 dm_test_info children should be passed to pre_probe and
260          * post_probe
261          */
262         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
263         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
264
265         /* Also we can check the per-device data */
266         expected_base_add = 0;
267         for (i = 0; i < 3; i++) {
268                 struct dm_test_uclass_perdev_priv *priv;
269                 struct dm_test_pdata *pdata;
270
271                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
272                 ut_assert(dev);
273
274                 priv = dev_get_uclass_priv(dev);
275                 ut_assert(priv);
276                 ut_asserteq(expected_base_add, priv->base_add);
277
278                 pdata = dev_get_plat(dev);
279                 expected_base_add += pdata->ping_add;
280         }
281
282         return 0;
283 }
284 DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
285
286 /* Check that we see the correct plat in each device */
287 static int dm_test_plat(struct unit_test_state *uts)
288 {
289         const struct dm_test_pdata *pdata;
290         struct udevice *dev;
291         int i;
292
293         for (i = 0; i < 3; i++) {
294                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
295                 ut_assert(dev);
296                 pdata = dev_get_plat(dev);
297                 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
298         }
299
300         return 0;
301 }
302 DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
303
304 /* Test that we can bind, probe, remove, unbind a driver */
305 static int dm_test_lifecycle(struct unit_test_state *uts)
306 {
307         int op_count[DM_TEST_OP_COUNT];
308         struct udevice *dev, *test_dev;
309         int start_dev_count, start_uc_count;
310         int dev_count, uc_count;
311         int pingret;
312         int ret;
313
314         memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
315
316         dm_get_stats(&start_dev_count, &start_uc_count);
317
318         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
319                                         &dev));
320         ut_assert(dev);
321         ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
322                         == op_count[DM_TEST_OP_BIND] + 1);
323         ut_assert(!dev_get_priv(dev));
324
325         /* We should have one more device */
326         dm_get_stats(&dev_count, &uc_count);
327         ut_asserteq(start_dev_count + 1, dev_count);
328         ut_asserteq(start_uc_count, uc_count);
329
330         /* Probe the device - it should fail allocating private data */
331         uts->force_fail_alloc = 1;
332         ret = device_probe(dev);
333         ut_assert(ret == -ENOMEM);
334         ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
335                         == op_count[DM_TEST_OP_PROBE] + 1);
336         ut_assert(!dev_get_priv(dev));
337
338         /* Try again without the alloc failure */
339         uts->force_fail_alloc = 0;
340         ut_assertok(device_probe(dev));
341         ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
342                         == op_count[DM_TEST_OP_PROBE] + 2);
343         ut_assert(dev_get_priv(dev));
344
345         /* This should be device 3 in the uclass */
346         ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
347         ut_assert(dev == test_dev);
348
349         /* Try ping */
350         ut_assertok(test_ping(dev, 100, &pingret));
351         ut_assert(pingret == 102);
352
353         /* Now remove device 3 */
354         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
355         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
356         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
357
358         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
359         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
360         ut_assertok(device_unbind(dev));
361         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
362         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
363
364         /* We should have one less device */
365         dm_get_stats(&dev_count, &uc_count);
366         ut_asserteq(start_dev_count, dev_count);
367         ut_asserteq(start_uc_count, uc_count);
368
369         return 0;
370 }
371 DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
372
373 /* Test that we can bind/unbind and the lists update correctly */
374 static int dm_test_ordering(struct unit_test_state *uts)
375 {
376         struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
377         int pingret;
378
379         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
380                                         &dev));
381         ut_assert(dev);
382
383         /* Bind two new devices (numbers 4 and 5) */
384         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
385                                         &dev_penultimate));
386         ut_assert(dev_penultimate);
387         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
388                                         &dev_last));
389         ut_assert(dev_last);
390
391         /* Now remove device 3 */
392         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
393         ut_assertok(device_unbind(dev));
394
395         /* The device numbering should have shifted down one */
396         ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
397         ut_assert(dev_penultimate == test_dev);
398         ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
399         ut_assert(dev_last == test_dev);
400
401         /* Add back the original device 3, now in position 5 */
402         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
403                                         &dev));
404         ut_assert(dev);
405
406         /* Try ping */
407         ut_assertok(test_ping(dev, 100, &pingret));
408         ut_assert(pingret == 102);
409
410         /* Remove 3 and 4 */
411         ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
412         ut_assertok(device_unbind(dev_penultimate));
413         ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
414         ut_assertok(device_unbind(dev_last));
415
416         /* Our device should now be in position 3 */
417         ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
418         ut_assert(dev == test_dev);
419
420         /* Now remove device 3 */
421         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
422         ut_assertok(device_unbind(dev));
423
424         return 0;
425 }
426 DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
427
428 /* Check that we can perform operations on a device (do a ping) */
429 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
430                         uint32_t base, struct dm_test_priv *priv)
431 {
432         int expected;
433         int pingret;
434
435         /* Getting the child device should allocate plat / priv */
436         ut_assertok(testfdt_ping(dev, 10, &pingret));
437         ut_assert(dev_get_priv(dev));
438         ut_assert(dev_get_plat(dev));
439
440         expected = 10 + base;
441         ut_asserteq(expected, pingret);
442
443         /* Do another ping */
444         ut_assertok(testfdt_ping(dev, 20, &pingret));
445         expected = 20 + base;
446         ut_asserteq(expected, pingret);
447
448         /* Now check the ping_total */
449         priv = dev_get_priv(dev);
450         ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
451                     priv->ping_total);
452
453         return 0;
454 }
455
456 /* Check that we can perform operations on devices */
457 static int dm_test_operations(struct unit_test_state *uts)
458 {
459         struct udevice *dev;
460         int i;
461
462         /*
463          * Now check that the ping adds are what we expect. This is using the
464          * ping-add property in each node.
465          */
466         for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
467                 uint32_t base;
468
469                 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
470
471                 /*
472                  * Get the 'reg' property, which tells us what the ping add
473                  * should be. We don't use the plat because we want
474                  * to test the code that sets that up (testfdt_drv_probe()).
475                  */
476                 base = test_pdata[i].ping_add;
477                 debug("dev=%d, base=%d\n", i, base);
478
479                 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
480         }
481
482         return 0;
483 }
484 DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
485
486 /* Remove all drivers and check that things work */
487 static int dm_test_remove(struct unit_test_state *uts)
488 {
489         struct udevice *dev;
490         int i;
491
492         for (i = 0; i < 3; i++) {
493                 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
494                 ut_assert(dev);
495                 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
496                            "Driver %d/%s not activated", i, dev->name);
497                 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
498                 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
499                            "Driver %d/%s should have deactivated", i,
500                            dev->name);
501                 ut_assert(!dev_get_priv(dev));
502         }
503
504         return 0;
505 }
506 DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
507
508 /* Remove and recreate everything, check for memory leaks */
509 static int dm_test_leak(struct unit_test_state *uts)
510 {
511         int i;
512
513         for (i = 0; i < 2; i++) {
514                 int ret;
515
516                 dm_leak_check_start(uts);
517
518                 ut_assertok(dm_scan_plat(false));
519                 ut_assertok(dm_scan_fdt(false));
520
521                 ret = uclass_probe_all(UCLASS_TEST);
522                 ut_assertok(ret);
523
524                 ut_assertok(dm_leak_check_end(uts));
525         }
526
527         return 0;
528 }
529 DM_TEST(dm_test_leak, 0);
530
531 /* Test uclass init/destroy methods */
532 static int dm_test_uclass(struct unit_test_state *uts)
533 {
534         int dev_count, uc_count;
535         struct uclass *uc;
536
537         /* We should have just the root device and uclass */
538         dm_get_stats(&dev_count, &uc_count);
539         ut_asserteq(1, dev_count);
540         ut_asserteq(1, uc_count);
541
542         ut_assertok(uclass_get(UCLASS_TEST, &uc));
543         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
544         ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
545         ut_assert(uclass_get_priv(uc));
546
547         dm_get_stats(&dev_count, &uc_count);
548         ut_asserteq(1, dev_count);
549         ut_asserteq(2, uc_count);
550
551         ut_assertok(uclass_destroy(uc));
552         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
553         ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
554
555         dm_get_stats(&dev_count, &uc_count);
556         ut_asserteq(1, dev_count);
557         ut_asserteq(1, uc_count);
558
559         return 0;
560 }
561 DM_TEST(dm_test_uclass, 0);
562
563 /**
564  * create_children() - Create children of a parent node
565  *
566  * @dms:        Test system state
567  * @parent:     Parent device
568  * @count:      Number of children to create
569  * @key:        Key value to put in first child. Subsequence children
570  *              receive an incrementing value
571  * @child:      If not NULL, then the child device pointers are written into
572  *              this array.
573  * Return: 0 if OK, -ve on error
574  */
575 static int create_children(struct unit_test_state *uts, struct udevice *parent,
576                            int count, int key, struct udevice *child[])
577 {
578         struct udevice *dev;
579         int i;
580
581         for (i = 0; i < count; i++) {
582                 struct dm_test_pdata *pdata;
583
584                 ut_assertok(device_bind_by_name(parent, false,
585                                                 &driver_info_manual, &dev));
586                 pdata = calloc(1, sizeof(*pdata));
587                 pdata->ping_add = key + i;
588                 dev_set_plat(dev, pdata);
589                 if (child)
590                         child[i] = dev;
591         }
592
593         return 0;
594 }
595
596 #define NODE_COUNT      10
597
598 static int dm_test_children(struct unit_test_state *uts)
599 {
600         struct udevice *top[NODE_COUNT];
601         struct udevice *child[NODE_COUNT];
602         struct udevice *grandchild[NODE_COUNT];
603         struct udevice *dev;
604         int total;
605         int ret;
606         int i;
607
608         /* We don't care about the numbering for this test */
609         uts->skip_post_probe = 1;
610
611         ut_assert(NODE_COUNT > 5);
612
613         /* First create 10 top-level children */
614         ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
615
616         /* Now a few have their own children */
617         ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
618         ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
619
620         /* And grandchildren */
621         for (i = 0; i < NODE_COUNT; i++)
622                 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
623                                             i == 2 ? grandchild : NULL));
624
625         /* Check total number of devices */
626         total = NODE_COUNT * (3 + NODE_COUNT);
627         ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
628
629         /* Try probing one of the grandchildren */
630         ut_assertok(uclass_get_device(UCLASS_TEST,
631                                       NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
632         ut_asserteq_ptr(grandchild[0], dev);
633
634         /*
635          * This should have probed the child and top node also, for a total
636          * of 3 nodes.
637          */
638         ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
639
640         /* Probe the other grandchildren */
641         for (i = 1; i < NODE_COUNT; i++)
642                 ut_assertok(device_probe(grandchild[i]));
643
644         ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
645
646         /* Probe everything */
647         ret = uclass_probe_all(UCLASS_TEST);
648         ut_assertok(ret);
649
650         ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
651
652         /* Remove a top-level child and check that the children are removed */
653         ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
654         ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
655         dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
656
657         /* Try one with grandchildren */
658         ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
659         ut_asserteq_ptr(dev, top[5]);
660         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
661         ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
662                     dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
663
664         /* Try the same with unbind */
665         ut_assertok(device_unbind(top[2]));
666         ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
667         dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
668
669         /* Try one with grandchildren */
670         ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
671         ut_asserteq_ptr(dev, top[6]);
672         ut_assertok(device_unbind(top[5]));
673         ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
674                     dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
675
676         return 0;
677 }
678 DM_TEST(dm_test_children, 0);
679
680 static int dm_test_device_reparent(struct unit_test_state *uts)
681 {
682         struct udevice *top[NODE_COUNT];
683         struct udevice *child[NODE_COUNT];
684         struct udevice *grandchild[NODE_COUNT];
685         struct udevice *dev;
686         int total;
687         int ret;
688         int i;
689
690         /* We don't care about the numbering for this test */
691         uts->skip_post_probe = 1;
692
693         ut_assert(NODE_COUNT > 5);
694
695         /* First create 10 top-level children */
696         ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
697
698         /* Now a few have their own children */
699         ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
700         ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
701
702         /* And grandchildren */
703         for (i = 0; i < NODE_COUNT; i++)
704                 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
705                                             i == 2 ? grandchild : NULL));
706
707         /* Check total number of devices */
708         total = NODE_COUNT * (3 + NODE_COUNT);
709         ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
710
711         /* Probe everything */
712         for (i = 0; i < total; i++)
713                 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
714
715         /* Re-parent top-level children with no grandchildren. */
716         ut_assertok(device_reparent(top[3], top[0]));
717         /* try to get devices */
718         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
719              dev;
720              ret = uclass_find_next_device(&dev)) {
721                 ut_assert(!ret);
722                 ut_assertnonnull(dev);
723         }
724
725         ut_assertok(device_reparent(top[4], top[0]));
726         /* try to get devices */
727         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
728              dev;
729              ret = uclass_find_next_device(&dev)) {
730                 ut_assert(!ret);
731                 ut_assertnonnull(dev);
732         }
733
734         /* Re-parent top-level children with grandchildren. */
735         ut_assertok(device_reparent(top[2], top[0]));
736         /* try to get devices */
737         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
738              dev;
739              ret = uclass_find_next_device(&dev)) {
740                 ut_assert(!ret);
741                 ut_assertnonnull(dev);
742         }
743
744         ut_assertok(device_reparent(top[5], top[2]));
745         /* try to get devices */
746         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
747              dev;
748              ret = uclass_find_next_device(&dev)) {
749                 ut_assert(!ret);
750                 ut_assertnonnull(dev);
751         }
752
753         /* Re-parent grandchildren. */
754         ut_assertok(device_reparent(grandchild[0], top[1]));
755         /* try to get devices */
756         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
757              dev;
758              ret = uclass_find_next_device(&dev)) {
759                 ut_assert(!ret);
760                 ut_assertnonnull(dev);
761         }
762
763         ut_assertok(device_reparent(grandchild[1], top[1]));
764         /* try to get devices */
765         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
766              dev;
767              ret = uclass_find_next_device(&dev)) {
768                 ut_assert(!ret);
769                 ut_assertnonnull(dev);
770         }
771
772         /* Remove re-pareneted devices. */
773         ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
774         /* try to get devices */
775         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
776              dev;
777              ret = uclass_find_next_device(&dev)) {
778                 ut_assert(!ret);
779                 ut_assertnonnull(dev);
780         }
781
782         ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
783         /* try to get devices */
784         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
785              dev;
786              ret = uclass_find_next_device(&dev)) {
787                 ut_assert(!ret);
788                 ut_assertnonnull(dev);
789         }
790
791         ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
792         /* try to get devices */
793         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
794              dev;
795              ret = uclass_find_next_device(&dev)) {
796                 ut_assert(!ret);
797                 ut_assertnonnull(dev);
798         }
799
800         ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
801         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
802              dev;
803              ret = uclass_find_next_device(&dev)) {
804                 ut_assert(!ret);
805                 ut_assertnonnull(dev);
806         }
807
808         ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
809         /* try to get devices */
810         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
811              dev;
812              ret = uclass_find_next_device(&dev)) {
813                 ut_assert(!ret);
814                 ut_assertnonnull(dev);
815         }
816
817         ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
818         /* try to get devices */
819         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
820              dev;
821              ret = uclass_find_next_device(&dev)) {
822                 ut_assert(!ret);
823                 ut_assertnonnull(dev);
824         }
825
826         /* Try the same with unbind */
827         ut_assertok(device_unbind(top[3]));
828         ut_assertok(device_unbind(top[4]));
829         ut_assertok(device_unbind(top[5]));
830         ut_assertok(device_unbind(top[2]));
831
832         ut_assertok(device_unbind(grandchild[0]));
833         ut_assertok(device_unbind(grandchild[1]));
834
835         return 0;
836 }
837 DM_TEST(dm_test_device_reparent, 0);
838
839 /* Test that pre-relocation devices work as expected */
840 static int dm_test_pre_reloc(struct unit_test_state *uts)
841 {
842         struct udevice *dev;
843
844         /* The normal driver should refuse to bind before relocation */
845         ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
846                                                 &driver_info_manual, &dev));
847
848         /* But this one is marked pre-reloc */
849         ut_assertok(device_bind_by_name(uts->root, true,
850                                         &driver_info_pre_reloc, &dev));
851
852         return 0;
853 }
854 DM_TEST(dm_test_pre_reloc, 0);
855
856 /*
857  * Test that removal of devices, either via the "normal" device_remove()
858  * API or via the device driver selective flag works as expected
859  */
860 static int dm_test_remove_active_dma(struct unit_test_state *uts)
861 {
862         struct udevice *dev;
863
864         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
865                                         &dev));
866         ut_assert(dev);
867
868         /* Probe the device */
869         ut_assertok(device_probe(dev));
870
871         /* Test if device is active right now */
872         ut_asserteq(true, device_active(dev));
873
874         /* Remove the device via selective remove flag */
875         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
876
877         /* Test if device is inactive right now */
878         ut_asserteq(false, device_active(dev));
879
880         /* Probe the device again */
881         ut_assertok(device_probe(dev));
882
883         /* Test if device is active right now */
884         ut_asserteq(true, device_active(dev));
885
886         /* Remove the device via "normal" remove API */
887         ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
888
889         /* Test if device is inactive right now */
890         ut_asserteq(false, device_active(dev));
891
892         /*
893          * Test if a device without the active DMA flags is not removed upon
894          * the active DMA remove call
895          */
896         ut_assertok(device_unbind(dev));
897         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
898                                         &dev));
899         ut_assert(dev);
900
901         /* Probe the device */
902         ut_assertok(device_probe(dev));
903
904         /* Test if device is active right now */
905         ut_asserteq(true, device_active(dev));
906
907         /* Remove the device via selective remove flag */
908         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
909
910         /* Test if device is still active right now */
911         ut_asserteq(true, device_active(dev));
912
913         return 0;
914 }
915 DM_TEST(dm_test_remove_active_dma, 0);
916
917 /* Test removal of 'vital' devices */
918 static int dm_test_remove_vital(struct unit_test_state *uts)
919 {
920         struct udevice *normal, *dma, *vital, *dma_vital;
921
922         /* Skip the behaviour in test_post_probe() */
923         uts->skip_post_probe = 1;
924
925         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
926                                         &normal));
927         ut_assertnonnull(normal);
928
929         ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
930                                         &dma));
931         ut_assertnonnull(dma);
932
933         ut_assertok(device_bind_by_name(uts->root, false,
934                                         &driver_info_vital_clk, &vital));
935         ut_assertnonnull(vital);
936
937         ut_assertok(device_bind_by_name(uts->root, false,
938                                         &driver_info_act_dma_vital_clk,
939                                         &dma_vital));
940         ut_assertnonnull(dma_vital);
941
942         /* Probe the devices */
943         ut_assertok(device_probe(normal));
944         ut_assertok(device_probe(dma));
945         ut_assertok(device_probe(vital));
946         ut_assertok(device_probe(dma_vital));
947
948         /* Check that devices are active right now */
949         ut_asserteq(true, device_active(normal));
950         ut_asserteq(true, device_active(dma));
951         ut_asserteq(true, device_active(vital));
952         ut_asserteq(true, device_active(dma_vital));
953
954         /* Remove active devices via selective remove flag */
955         dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
956
957         /*
958          * Check that this only has an effect on the dma device, since two
959          * devices are vital and the third does not have active DMA
960          */
961         ut_asserteq(true, device_active(normal));
962         ut_asserteq(false, device_active(dma));
963         ut_asserteq(true, device_active(vital));
964         ut_asserteq(true, device_active(dma_vital));
965
966         /* Remove active devices via selective remove flag */
967         ut_assertok(device_probe(dma));
968         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
969
970         /* This should have affected both active-dma devices */
971         ut_asserteq(true, device_active(normal));
972         ut_asserteq(false, device_active(dma));
973         ut_asserteq(true, device_active(vital));
974         ut_asserteq(false, device_active(dma_vital));
975
976         /* Remove non-vital devices */
977         ut_assertok(device_probe(dma));
978         ut_assertok(device_probe(dma_vital));
979         dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
980
981         /* This should have affected only non-vital devices */
982         ut_asserteq(false, device_active(normal));
983         ut_asserteq(false, device_active(dma));
984         ut_asserteq(true, device_active(vital));
985         ut_asserteq(true, device_active(dma_vital));
986
987         /* Remove vital devices via normal remove flag */
988         ut_assertok(device_probe(normal));
989         ut_assertok(device_probe(dma));
990         dm_remove_devices_flags(DM_REMOVE_NORMAL);
991
992         /* Check that all devices are inactive right now */
993         ut_asserteq(false, device_active(normal));
994         ut_asserteq(false, device_active(dma));
995         ut_asserteq(false, device_active(vital));
996         ut_asserteq(false, device_active(dma_vital));
997
998         return 0;
999 }
1000 DM_TEST(dm_test_remove_vital, 0);
1001
1002 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
1003 {
1004         struct uclass *uc;
1005
1006         ut_assertok(uclass_get(UCLASS_TEST, &uc));
1007
1008         gd->dm_root = NULL;
1009         memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
1010
1011         ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
1012         ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc));
1013
1014         return 0;
1015 }
1016 DM_TEST(dm_test_uclass_before_ready, 0);
1017
1018 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
1019 {
1020         struct udevice *dev;
1021         int ret;
1022
1023         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1024              dev;
1025              ret = uclass_find_next_device(&dev)) {
1026                 ut_assert(!ret);
1027                 ut_assertnonnull(dev);
1028         }
1029
1030         ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1031         ut_assertnull(dev);
1032
1033         return 0;
1034 }
1035 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1036
1037 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1038 {
1039         struct udevice *finddev;
1040         struct udevice *testdev;
1041         int findret, ret;
1042
1043         /*
1044          * For each test device found in fdt like: "a-test", "b-test", etc.,
1045          * use its name and try to find it by uclass_find_device_by_name().
1046          * Then, on success check if:
1047          * - current 'testdev' name is equal to the returned 'finddev' name
1048          * - current 'testdev' pointer is equal to the returned 'finddev'
1049          *
1050          * We assume that, each uclass's device name is unique, so if not, then
1051          * this will fail on checking condition: testdev == finddev, since the
1052          * uclass_find_device_by_name(), returns the first device by given name.
1053         */
1054         for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1055              testdev;
1056              ret = uclass_find_next_device(&testdev)) {
1057                 ut_assertok(ret);
1058                 ut_assertnonnull(testdev);
1059
1060                 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1061                                                      testdev->name,
1062                                                      &finddev);
1063
1064                 ut_assertok(findret);
1065                 ut_assert(testdev);
1066                 ut_asserteq_str(testdev->name, finddev->name);
1067                 ut_asserteq_ptr(testdev, finddev);
1068         }
1069
1070         return 0;
1071 }
1072 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1073
1074 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1075 {
1076         struct udevice *dev;
1077         int ret;
1078
1079         for (ret = uclass_first_device_check(UCLASS_TEST, &dev);
1080              dev;
1081              ret = uclass_next_device_check(&dev)) {
1082                 ut_assert(!ret);
1083                 ut_assert(device_active(dev));
1084         }
1085
1086         return 0;
1087 }
1088 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1089
1090 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1091 {
1092         struct udevice *finddev;
1093         struct udevice *testdev;
1094         int ret, findret;
1095
1096         /*
1097          * For each test device found in fdt like: "a-test", "b-test", etc.,
1098          * use its name and try to get it by uclass_get_device_by_name().
1099          * On success check if:
1100          * - returned finddev' is active
1101          * - current 'testdev' name is equal to the returned 'finddev' name
1102          * - current 'testdev' pointer is equal to the returned 'finddev'
1103          *
1104          * We asserts that the 'testdev' is active on each loop entry, so we
1105          * could be sure that the 'finddev' is activated too, but for sure
1106          * we check it again.
1107          *
1108          * We assume that, each uclass's device name is unique, so if not, then
1109          * this will fail on checking condition: testdev == finddev, since the
1110          * uclass_get_device_by_name(), returns the first device by given name.
1111         */
1112         for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev);
1113              testdev;
1114              ret = uclass_next_device_check(&testdev)) {
1115                 ut_assertok(ret);
1116                 ut_assert(device_active(testdev));
1117
1118                 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1119                                                     testdev->name,
1120                                                     &finddev);
1121
1122                 ut_assertok(findret);
1123                 ut_assert(finddev);
1124                 ut_assert(device_active(finddev));
1125                 ut_asserteq_str(testdev->name, finddev->name);
1126                 ut_asserteq_ptr(testdev, finddev);
1127         }
1128
1129         return 0;
1130 }
1131 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1132
1133 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1134 {
1135         struct udevice *dev;
1136
1137         ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1138         ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1139
1140         return 0;
1141 }
1142 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1143
1144 static int dm_test_uclass_names(struct unit_test_state *uts)
1145 {
1146         ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1147         ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1148
1149         ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi"));
1150
1151         return 0;
1152 }
1153 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1154
1155 static int dm_test_inactive_child(struct unit_test_state *uts)
1156 {
1157         struct udevice *parent, *dev1, *dev2;
1158
1159         /* Skip the behaviour in test_post_probe() */
1160         uts->skip_post_probe = 1;
1161
1162         ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1163
1164         /*
1165          * Create a child but do not activate it. Calling the function again
1166          * should return the same child.
1167          */
1168         ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1169                                                         UCLASS_TEST, &dev1));
1170         ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1171                                 "test_child", 0, ofnode_null(), &dev1));
1172
1173         ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1174                                                      &dev2));
1175         ut_asserteq_ptr(dev1, dev2);
1176
1177         ut_assertok(device_probe(dev1));
1178         ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1179                                                         UCLASS_TEST, &dev2));
1180
1181         return 0;
1182 }
1183 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1184
1185 /* Make sure all bound devices have a sequence number */
1186 static int dm_test_all_have_seq(struct unit_test_state *uts)
1187 {
1188         struct udevice *dev;
1189         struct uclass *uc;
1190
1191         list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1192                 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1193                         if (dev->seq_ == -1)
1194                                 printf("Device '%s' has no seq (%d)\n",
1195                                        dev->name, dev->seq_);
1196                         ut_assert(dev->seq_ != -1);
1197                 }
1198         }
1199
1200         return 0;
1201 }
1202 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
1203
1204 #if CONFIG_IS_ENABLED(DM_DMA)
1205 static int dm_test_dma_offset(struct unit_test_state *uts)
1206 {
1207        struct udevice *dev;
1208        ofnode node;
1209
1210        /* Make sure the bus's dma-ranges aren't taken into account here */
1211        node = ofnode_path("/mmio-bus@0");
1212        ut_assert(ofnode_valid(node));
1213        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1214        ut_asserteq_64(0, dev->dma_offset);
1215
1216        /* Device behind a bus with dma-ranges */
1217        node = ofnode_path("/mmio-bus@0/subnode@0");
1218        ut_assert(ofnode_valid(node));
1219        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1220        ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1221
1222        /* This one has no dma-ranges */
1223        node = ofnode_path("/mmio-bus@1");
1224        ut_assert(ofnode_valid(node));
1225        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1226        node = ofnode_path("/mmio-bus@1/subnode@0");
1227        ut_assert(ofnode_valid(node));
1228        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1229        ut_asserteq_64(0, dev->dma_offset);
1230
1231        return 0;
1232 }
1233 DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1234 #endif
1235
1236 /* Test dm_get_stats() */
1237 static int dm_test_get_stats(struct unit_test_state *uts)
1238 {
1239         int dev_count, uc_count;
1240
1241         dm_get_stats(&dev_count, &uc_count);
1242         ut_assert(dev_count > 50);
1243         ut_assert(uc_count > 30);
1244
1245         return 0;
1246 }
1247 DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT);
1248
1249 /* Test uclass_find_device_by_name() */
1250 static int dm_test_uclass_find_device(struct unit_test_state *uts)
1251 {
1252         struct udevice *dev;
1253
1254         ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
1255         ut_asserteq(-ENODEV,
1256                     uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
1257         ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
1258                                                   &dev));
1259
1260         return 0;
1261 }
1262 DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT);
1263
1264 /* Test getting information about tags attached to devices */
1265 static int dm_test_dev_get_attach(struct unit_test_state *uts)
1266 {
1267         struct udevice *dev;
1268
1269         ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1270         ut_asserteq_str("a-test", dev->name);
1271
1272         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1273         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1274         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1275         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1276         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1277         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1278
1279         ut_asserteq(sizeof(struct dm_test_pdata),
1280                     dev_get_attach_size(dev, DM_TAG_PLAT));
1281         ut_asserteq(sizeof(struct dm_test_priv),
1282                     dev_get_attach_size(dev, DM_TAG_PRIV));
1283         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1284         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1285         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1286         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1287
1288         return 0;
1289 }
1290 DM_TEST(dm_test_dev_get_attach, UT_TESTF_SCAN_FDT);
1291
1292 /* Test getting information about tags attached to bus devices */
1293 static int dm_test_dev_get_attach_bus(struct unit_test_state *uts)
1294 {
1295         struct udevice *dev, *child;
1296
1297         ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev));
1298         ut_asserteq_str("some-bus", dev->name);
1299
1300         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1301         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1302         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1303         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1304         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1305         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1306
1307         ut_asserteq(sizeof(struct dm_test_pdata),
1308                     dev_get_attach_size(dev, DM_TAG_PLAT));
1309         ut_asserteq(sizeof(struct dm_test_priv),
1310                     dev_get_attach_size(dev, DM_TAG_PRIV));
1311         ut_asserteq(sizeof(struct dm_test_uclass_priv),
1312                     dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1313         ut_asserteq(sizeof(struct dm_test_uclass_plat),
1314                     dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1315         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1316         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1317
1318         /* Now try the child of the bus */
1319         ut_assertok(device_first_child_err(dev, &child));
1320         ut_asserteq_str("c-test@5", child->name);
1321
1322         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT));
1323         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV));
1324         ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV));
1325         ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT));
1326         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT));
1327         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV));
1328
1329         ut_asserteq(sizeof(struct dm_test_pdata),
1330                     dev_get_attach_size(child, DM_TAG_PLAT));
1331         ut_asserteq(sizeof(struct dm_test_priv),
1332                     dev_get_attach_size(child, DM_TAG_PRIV));
1333         ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV));
1334         ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT));
1335         ut_asserteq(sizeof(struct dm_test_parent_plat),
1336                     dev_get_attach_size(child, DM_TAG_PARENT_PLAT));
1337         ut_asserteq(sizeof(struct dm_test_parent_data),
1338                     dev_get_attach_size(child, DM_TAG_PARENT_PRIV));
1339
1340         return 0;
1341 }
1342 DM_TEST(dm_test_dev_get_attach_bus, UT_TESTF_SCAN_FDT);
1343
1344 /* Test getting information about tags attached to bus devices */
1345 static int dm_test_dev_get_mem(struct unit_test_state *uts)
1346 {
1347         struct dm_stats stats;
1348
1349         dm_get_mem(&stats);
1350
1351         return 0;
1352 }
1353 DM_TEST(dm_test_dev_get_mem, UT_TESTF_SCAN_FDT);
This page took 0.103747 seconds and 4 git commands to generate.