]> Git Repo - u-boot.git/blob - test/dm/core.c
Restore patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet"
[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         gd->dm_root_f = NULL;
1010         memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
1011
1012         ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
1013         ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc));
1014
1015         return 0;
1016 }
1017 DM_TEST(dm_test_uclass_before_ready, 0);
1018
1019 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
1020 {
1021         struct udevice *dev;
1022         int ret;
1023
1024         for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1025              dev;
1026              ret = uclass_find_next_device(&dev)) {
1027                 ut_assert(!ret);
1028                 ut_assertnonnull(dev);
1029         }
1030
1031         ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
1032         ut_assertnull(dev);
1033
1034         return 0;
1035 }
1036 DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
1037
1038 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
1039 {
1040         struct udevice *finddev;
1041         struct udevice *testdev;
1042         int findret, ret;
1043
1044         /*
1045          * For each test device found in fdt like: "a-test", "b-test", etc.,
1046          * use its name and try to find it by uclass_find_device_by_name().
1047          * Then, on success check if:
1048          * - current 'testdev' name is equal to the returned 'finddev' name
1049          * - current 'testdev' pointer is equal to the returned 'finddev'
1050          *
1051          * We assume that, each uclass's device name is unique, so if not, then
1052          * this will fail on checking condition: testdev == finddev, since the
1053          * uclass_find_device_by_name(), returns the first device by given name.
1054         */
1055         for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1056              testdev;
1057              ret = uclass_find_next_device(&testdev)) {
1058                 ut_assertok(ret);
1059                 ut_assertnonnull(testdev);
1060
1061                 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1062                                                      testdev->name,
1063                                                      &finddev);
1064
1065                 ut_assertok(findret);
1066                 ut_assert(testdev);
1067                 ut_asserteq_str(testdev->name, finddev->name);
1068                 ut_asserteq_ptr(testdev, finddev);
1069         }
1070
1071         return 0;
1072 }
1073 DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
1074
1075 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
1076 {
1077         struct udevice *dev;
1078         int ret;
1079
1080         for (ret = uclass_first_device_check(UCLASS_TEST, &dev);
1081              dev;
1082              ret = uclass_next_device_check(&dev)) {
1083                 ut_assert(!ret);
1084                 ut_assert(device_active(dev));
1085         }
1086
1087         return 0;
1088 }
1089 DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
1090
1091 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
1092 {
1093         struct udevice *finddev;
1094         struct udevice *testdev;
1095         int ret, findret;
1096
1097         /*
1098          * For each test device found in fdt like: "a-test", "b-test", etc.,
1099          * use its name and try to get it by uclass_get_device_by_name().
1100          * On success check if:
1101          * - returned finddev' is active
1102          * - current 'testdev' name is equal to the returned 'finddev' name
1103          * - current 'testdev' pointer is equal to the returned 'finddev'
1104          *
1105          * We asserts that the 'testdev' is active on each loop entry, so we
1106          * could be sure that the 'finddev' is activated too, but for sure
1107          * we check it again.
1108          *
1109          * We assume that, each uclass's device name is unique, so if not, then
1110          * this will fail on checking condition: testdev == finddev, since the
1111          * uclass_get_device_by_name(), returns the first device by given name.
1112         */
1113         for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev);
1114              testdev;
1115              ret = uclass_next_device_check(&testdev)) {
1116                 ut_assertok(ret);
1117                 ut_assert(device_active(testdev));
1118
1119                 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1120                                                     testdev->name,
1121                                                     &finddev);
1122
1123                 ut_assertok(findret);
1124                 ut_assert(finddev);
1125                 ut_assert(device_active(finddev));
1126                 ut_asserteq_str(testdev->name, finddev->name);
1127                 ut_asserteq_ptr(testdev, finddev);
1128         }
1129
1130         return 0;
1131 }
1132 DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
1133
1134 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
1135 {
1136         struct udevice *dev;
1137
1138         ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1139         ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1140
1141         return 0;
1142 }
1143 DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
1144
1145 static int dm_test_uclass_names(struct unit_test_state *uts)
1146 {
1147         ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1148         ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1149
1150         ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi"));
1151
1152         return 0;
1153 }
1154 DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
1155
1156 static int dm_test_inactive_child(struct unit_test_state *uts)
1157 {
1158         struct udevice *parent, *dev1, *dev2;
1159
1160         /* Skip the behaviour in test_post_probe() */
1161         uts->skip_post_probe = 1;
1162
1163         ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1164
1165         /*
1166          * Create a child but do not activate it. Calling the function again
1167          * should return the same child.
1168          */
1169         ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1170                                                         UCLASS_TEST, &dev1));
1171         ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
1172                                 "test_child", 0, ofnode_null(), &dev1));
1173
1174         ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1175                                                      &dev2));
1176         ut_asserteq_ptr(dev1, dev2);
1177
1178         ut_assertok(device_probe(dev1));
1179         ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1180                                                         UCLASS_TEST, &dev2));
1181
1182         return 0;
1183 }
1184 DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
1185
1186 /* Make sure all bound devices have a sequence number */
1187 static int dm_test_all_have_seq(struct unit_test_state *uts)
1188 {
1189         struct udevice *dev;
1190         struct uclass *uc;
1191
1192         list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1193                 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
1194                         if (dev->seq_ == -1)
1195                                 printf("Device '%s' has no seq (%d)\n",
1196                                        dev->name, dev->seq_);
1197                         ut_assert(dev->seq_ != -1);
1198                 }
1199         }
1200
1201         return 0;
1202 }
1203 DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
1204
1205 #if CONFIG_IS_ENABLED(DM_DMA)
1206 static int dm_test_dma_offset(struct unit_test_state *uts)
1207 {
1208        struct udevice *dev;
1209        ofnode node;
1210
1211        /* Make sure the bus's dma-ranges aren't taken into account here */
1212        node = ofnode_path("/mmio-bus@0");
1213        ut_assert(ofnode_valid(node));
1214        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1215        ut_asserteq_64(0, dev->dma_offset);
1216
1217        /* Device behind a bus with dma-ranges */
1218        node = ofnode_path("/mmio-bus@0/subnode@0");
1219        ut_assert(ofnode_valid(node));
1220        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1221        ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1222
1223        /* This one has no dma-ranges */
1224        node = ofnode_path("/mmio-bus@1");
1225        ut_assert(ofnode_valid(node));
1226        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1227        node = ofnode_path("/mmio-bus@1/subnode@0");
1228        ut_assert(ofnode_valid(node));
1229        ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1230        ut_asserteq_64(0, dev->dma_offset);
1231
1232        return 0;
1233 }
1234 DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1235 #endif
1236
1237 /* Test dm_get_stats() */
1238 static int dm_test_get_stats(struct unit_test_state *uts)
1239 {
1240         int dev_count, uc_count;
1241
1242         dm_get_stats(&dev_count, &uc_count);
1243         ut_assert(dev_count > 50);
1244         ut_assert(uc_count > 30);
1245
1246         return 0;
1247 }
1248 DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT);
1249
1250 /* Test uclass_find_device_by_name() */
1251 static int dm_test_uclass_find_device(struct unit_test_state *uts)
1252 {
1253         struct udevice *dev;
1254
1255         ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
1256         ut_asserteq(-ENODEV,
1257                     uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
1258         ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
1259                                                   &dev));
1260
1261         return 0;
1262 }
1263 DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT);
1264
1265 /* Test getting information about tags attached to devices */
1266 static int dm_test_dev_get_attach(struct unit_test_state *uts)
1267 {
1268         struct udevice *dev;
1269
1270         ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1271         ut_asserteq_str("a-test", dev->name);
1272
1273         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1274         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1275         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1276         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1277         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1278         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1279
1280         ut_asserteq(sizeof(struct dm_test_pdata),
1281                     dev_get_attach_size(dev, DM_TAG_PLAT));
1282         ut_asserteq(sizeof(struct dm_test_priv),
1283                     dev_get_attach_size(dev, DM_TAG_PRIV));
1284         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1285         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1286         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1287         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1288
1289         return 0;
1290 }
1291 DM_TEST(dm_test_dev_get_attach, UT_TESTF_SCAN_FDT);
1292
1293 /* Test getting information about tags attached to bus devices */
1294 static int dm_test_dev_get_attach_bus(struct unit_test_state *uts)
1295 {
1296         struct udevice *dev, *child;
1297
1298         ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev));
1299         ut_asserteq_str("some-bus", dev->name);
1300
1301         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1302         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1303         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1304         ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1305         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1306         ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1307
1308         ut_asserteq(sizeof(struct dm_test_pdata),
1309                     dev_get_attach_size(dev, DM_TAG_PLAT));
1310         ut_asserteq(sizeof(struct dm_test_priv),
1311                     dev_get_attach_size(dev, DM_TAG_PRIV));
1312         ut_asserteq(sizeof(struct dm_test_uclass_priv),
1313                     dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1314         ut_asserteq(sizeof(struct dm_test_uclass_plat),
1315                     dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1316         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1317         ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1318
1319         /* Now try the child of the bus */
1320         ut_assertok(device_first_child_err(dev, &child));
1321         ut_asserteq_str("c-test@5", child->name);
1322
1323         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT));
1324         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV));
1325         ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV));
1326         ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT));
1327         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT));
1328         ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV));
1329
1330         ut_asserteq(sizeof(struct dm_test_pdata),
1331                     dev_get_attach_size(child, DM_TAG_PLAT));
1332         ut_asserteq(sizeof(struct dm_test_priv),
1333                     dev_get_attach_size(child, DM_TAG_PRIV));
1334         ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV));
1335         ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT));
1336         ut_asserteq(sizeof(struct dm_test_parent_plat),
1337                     dev_get_attach_size(child, DM_TAG_PARENT_PLAT));
1338         ut_asserteq(sizeof(struct dm_test_parent_data),
1339                     dev_get_attach_size(child, DM_TAG_PARENT_PRIV));
1340
1341         return 0;
1342 }
1343 DM_TEST(dm_test_dev_get_attach_bus, UT_TESTF_SCAN_FDT);
1344
1345 /* Test getting information about tags attached to bus devices */
1346 static int dm_test_dev_get_mem(struct unit_test_state *uts)
1347 {
1348         struct dm_stats stats;
1349
1350         dm_get_mem(&stats);
1351
1352         return 0;
1353 }
1354 DM_TEST(dm_test_dev_get_mem, UT_TESTF_SCAN_FDT);
This page took 0.100946 seconds and 4 git commands to generate.