blob: bfd6565d9527220b8288dc2767e8a47c11a72aa4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
3 * Tests for the core driver model code
4 *
5 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07006 */
7
8#include <common.h>
9#include <errno.h>
10#include <dm.h>
11#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070013#include <malloc.h>
14#include <dm/device-internal.h>
15#include <dm/root.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070016#include <dm/util.h>
17#include <dm/test.h>
18#include <dm/uclass-internal.h>
Simon Glass75c4d412020-07-19 10:15:37 -060019#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050020#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070021
22DECLARE_GLOBAL_DATA_PTR;
23
24enum {
25 TEST_INTVAL1 = 0,
26 TEST_INTVAL2 = 3,
27 TEST_INTVAL3 = 6,
28 TEST_INTVAL_MANUAL = 101112,
Simon Glassfef72b72014-07-23 06:55:03 -060029 TEST_INTVAL_PRE_RELOC = 7,
Simon Glassb2c1cac2014-02-26 15:59:21 -070030};
31
32static const struct dm_test_pdata test_pdata[] = {
33 { .ping_add = TEST_INTVAL1, },
34 { .ping_add = TEST_INTVAL2, },
35 { .ping_add = TEST_INTVAL3, },
36};
37
38static const struct dm_test_pdata test_pdata_manual = {
39 .ping_add = TEST_INTVAL_MANUAL,
40};
41
Simon Glassfef72b72014-07-23 06:55:03 -060042static const struct dm_test_pdata test_pdata_pre_reloc = {
43 .ping_add = TEST_INTVAL_PRE_RELOC,
44};
45
Simon Glass1d8364a2020-12-28 20:34:54 -070046U_BOOT_DRVINFO(dm_test_info1) = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070047 .name = "test_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070048 .plat = &test_pdata[0],
Simon Glassb2c1cac2014-02-26 15:59:21 -070049};
50
Simon Glass1d8364a2020-12-28 20:34:54 -070051U_BOOT_DRVINFO(dm_test_info2) = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070052 .name = "test_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070053 .plat = &test_pdata[1],
Simon Glassb2c1cac2014-02-26 15:59:21 -070054};
55
Simon Glass1d8364a2020-12-28 20:34:54 -070056U_BOOT_DRVINFO(dm_test_info3) = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070057 .name = "test_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070058 .plat = &test_pdata[2],
Simon Glassb2c1cac2014-02-26 15:59:21 -070059};
60
61static struct driver_info driver_info_manual = {
62 .name = "test_manual_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070063 .plat = &test_pdata_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -070064};
65
Simon Glassfef72b72014-07-23 06:55:03 -060066static struct driver_info driver_info_pre_reloc = {
67 .name = "test_pre_reloc_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070068 .plat = &test_pdata_pre_reloc,
Simon Glassfef72b72014-07-23 06:55:03 -060069};
70
Stefan Roeseeaffda72017-03-27 11:02:43 +020071static struct driver_info driver_info_act_dma = {
72 .name = "test_act_dma_drv",
73};
74
Marek Vasutabbdbbd2021-01-24 14:32:46 -070075static struct driver_info driver_info_vital_clk = {
76 .name = "test_vital_clk_drv",
77};
78
79static struct driver_info driver_info_act_dma_vital_clk = {
80 .name = "test_act_dma_vital_clk_drv",
81};
82
Joe Hershberger3a77be52015-05-20 14:27:27 -050083void dm_leak_check_start(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060084{
Joe Hershberger3a77be52015-05-20 14:27:27 -050085 uts->start = mallinfo();
86 if (!uts->start.uordblks)
Simon Glass0927a6f2014-10-04 11:29:50 -060087 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
88}
89
Joe Hershberger3a77be52015-05-20 14:27:27 -050090int dm_leak_check_end(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060091{
92 struct mallinfo end;
Simon Glass94d22182015-09-12 08:45:20 -060093 int id, diff;
Simon Glass0927a6f2014-10-04 11:29:50 -060094
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();
Simon Glass94d22182015-09-12 08:45:20 -0600106 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);
Joe Hershberger3a77be52015-05-20 14:27:27 -0500111 ut_asserteq(uts->start.uordblks, end.uordblks);
Simon Glass0927a6f2014-10-04 11:29:50 -0600112
113 return 0;
114}
115
Simon Glass71fa5b42020-12-03 16:55:18 -0700116/* Test that binding with plat occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500117static int dm_test_autobind(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700118{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500119 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200120 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700121
122 /*
123 * We should have a single class (UCLASS_ROOT) and a single root
124 * device with no children.
125 */
126 ut_assert(dms->root);
Simon Glass784cd9e2020-12-19 10:40:17 -0700127 ut_asserteq(1, list_count_items(gd->uclass_root));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700128 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
129 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
130
Simon Glassb75b15b2020-12-03 16:55:23 -0700131 ut_assertok(dm_scan_plat(false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700132
133 /* We should have our test class now at least, plus more children */
Simon Glass784cd9e2020-12-19 10:40:17 -0700134 ut_assert(1 < list_count_items(gd->uclass_root));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700135 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
136
137 /* Our 3 dm_test_infox children should be bound to the test uclass */
138 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
139
140 /* No devices should be probed */
141 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
Simon Glass6211d762020-12-19 10:40:10 -0700142 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700143
144 /* Our test driver should have been bound 3 times */
145 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
146
147 return 0;
148}
149DM_TEST(dm_test_autobind, 0);
150
Simon Glass71fa5b42020-12-03 16:55:18 -0700151/* Test that binding with uclass plat allocation occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500152static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200153{
154 struct dm_test_perdev_uc_pdata *uc_pdata;
155 struct udevice *dev;
156 struct uclass *uc;
157
158 ut_assertok(uclass_get(UCLASS_TEST, &uc));
159 ut_assert(uc);
160
161 /**
162 * Test if test uclass driver requires allocation for the uclass
Simon Glass71fa5b42020-12-03 16:55:18 -0700163 * platform data and then check the dev->uclass_plat pointer.
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200164 */
Simon Glass71fa5b42020-12-03 16:55:18 -0700165 ut_assert(uc->uc_drv->per_device_plat_auto);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200166
167 for (uclass_find_first_device(UCLASS_TEST, &dev);
168 dev;
169 uclass_find_next_device(&dev)) {
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200170 ut_assertnonnull(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200171
Simon Glass71fa5b42020-12-03 16:55:18 -0700172 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200173 ut_assert(uc_pdata);
174 }
175
176 return 0;
177}
Simon Glass974dccd2020-07-28 19:41:12 -0600178DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200179
Simon Glass71fa5b42020-12-03 16:55:18 -0700180/* Test that binding with uclass plat setting occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500181static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200182{
183 struct dm_test_perdev_uc_pdata *uc_pdata;
184 struct udevice *dev;
185
186 /**
187 * In the test_postbind() method of test uclass driver, the uclass
188 * platform data should be set to three test int values - test it.
189 */
190 for (uclass_find_first_device(UCLASS_TEST, &dev);
191 dev;
192 uclass_find_next_device(&dev)) {
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200193 ut_assertnonnull(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200194
Simon Glass71fa5b42020-12-03 16:55:18 -0700195 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200196 ut_assert(uc_pdata);
197 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
198 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
199 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
200 }
201
202 return 0;
203}
Simon Glass974dccd2020-07-28 19:41:12 -0600204DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200205
Simon Glassb2c1cac2014-02-26 15:59:21 -0700206/* Test that autoprobe finds all the expected devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500207static int dm_test_autoprobe(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700208{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500209 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700210 int expected_base_add;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200211 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700212 struct uclass *uc;
213 int i;
214
215 ut_assertok(uclass_get(UCLASS_TEST, &uc));
216 ut_assert(uc);
217
218 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700219 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700220 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
221
222 /* The root device should not be activated until needed */
Simon Glass6211d762020-12-19 10:40:10 -0700223 ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700224
225 /*
226 * We should be able to find the three test devices, and they should
227 * all be activated as they are used (lazy activation, required by
228 * U-Boot)
229 */
230 for (i = 0; i < 3; i++) {
231 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
232 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700233 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700234 "Driver %d/%s already activated", i, dev->name);
235
236 /* This should activate it */
237 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
238 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700239 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700240
241 /* Activating a device should activate the root device */
242 if (!i)
Simon Glass6211d762020-12-19 10:40:10 -0700243 ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700244 }
245
Simon Glass9c1f3822015-03-05 12:25:22 -0700246 /*
247 * Our 3 dm_test_info children should be passed to pre_probe and
248 * post_probe
249 */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700250 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700251 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700252
253 /* Also we can check the per-device data */
254 expected_base_add = 0;
255 for (i = 0; i < 3; i++) {
256 struct dm_test_uclass_perdev_priv *priv;
257 struct dm_test_pdata *pdata;
258
259 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
260 ut_assert(dev);
261
Simon Glassde0977b2015-03-05 12:25:20 -0700262 priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700263 ut_assert(priv);
264 ut_asserteq(expected_base_add, priv->base_add);
265
Simon Glass95588622020-12-22 19:30:28 -0700266 pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700267 expected_base_add += pdata->ping_add;
268 }
269
270 return 0;
271}
Simon Glass974dccd2020-07-28 19:41:12 -0600272DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700273
Simon Glass71fa5b42020-12-03 16:55:18 -0700274/* Check that we see the correct plat in each device */
Simon Glassb75b15b2020-12-03 16:55:23 -0700275static int dm_test_plat(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700276{
277 const struct dm_test_pdata *pdata;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200278 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700279 int i;
280
281 for (i = 0; i < 3; i++) {
282 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
283 ut_assert(dev);
Simon Glass95588622020-12-22 19:30:28 -0700284 pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700285 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
286 }
287
288 return 0;
289}
Simon Glassb75b15b2020-12-03 16:55:23 -0700290DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700291
292/* Test that we can bind, probe, remove, unbind a driver */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500293static int dm_test_lifecycle(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700294{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500295 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700296 int op_count[DM_TEST_OP_COUNT];
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200297 struct udevice *dev, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700298 int pingret;
299 int ret;
300
301 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
302
Simon Glassfef72b72014-07-23 06:55:03 -0600303 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700304 &dev));
305 ut_assert(dev);
306 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
307 == op_count[DM_TEST_OP_BIND] + 1);
Simon Glass95588622020-12-22 19:30:28 -0700308 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700309
310 /* Probe the device - it should fail allocating private data */
311 dms->force_fail_alloc = 1;
312 ret = device_probe(dev);
313 ut_assert(ret == -ENOMEM);
314 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
315 == op_count[DM_TEST_OP_PROBE] + 1);
Simon Glass95588622020-12-22 19:30:28 -0700316 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700317
318 /* Try again without the alloc failure */
319 dms->force_fail_alloc = 0;
320 ut_assertok(device_probe(dev));
321 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
322 == op_count[DM_TEST_OP_PROBE] + 2);
Simon Glass95588622020-12-22 19:30:28 -0700323 ut_assert(dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700324
325 /* This should be device 3 in the uclass */
326 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
327 ut_assert(dev == test_dev);
328
329 /* Try ping */
330 ut_assertok(test_ping(dev, 100, &pingret));
331 ut_assert(pingret == 102);
332
333 /* Now remove device 3 */
334 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100335 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700336 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
337
338 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
339 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
340 ut_assertok(device_unbind(dev));
341 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
342 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
343
344 return 0;
345}
Simon Glass974dccd2020-07-28 19:41:12 -0600346DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700347
348/* Test that we can bind/unbind and the lists update correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500349static int dm_test_ordering(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700350{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500351 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200352 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700353 int pingret;
354
Simon Glassfef72b72014-07-23 06:55:03 -0600355 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700356 &dev));
357 ut_assert(dev);
358
359 /* Bind two new devices (numbers 4 and 5) */
Simon Glassfef72b72014-07-23 06:55:03 -0600360 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700361 &dev_penultimate));
362 ut_assert(dev_penultimate);
Simon Glassfef72b72014-07-23 06:55:03 -0600363 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700364 &dev_last));
365 ut_assert(dev_last);
366
367 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100368 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700369 ut_assertok(device_unbind(dev));
370
371 /* The device numbering should have shifted down one */
372 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
373 ut_assert(dev_penultimate == test_dev);
374 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
375 ut_assert(dev_last == test_dev);
376
377 /* Add back the original device 3, now in position 5 */
Simon Glassfef72b72014-07-23 06:55:03 -0600378 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
379 &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700380 ut_assert(dev);
381
382 /* Try ping */
383 ut_assertok(test_ping(dev, 100, &pingret));
384 ut_assert(pingret == 102);
385
386 /* Remove 3 and 4 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100387 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700388 ut_assertok(device_unbind(dev_penultimate));
Stefan Roese80b5bc92017-03-20 12:51:48 +0100389 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700390 ut_assertok(device_unbind(dev_last));
391
392 /* Our device should now be in position 3 */
393 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
394 ut_assert(dev == test_dev);
395
396 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100397 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700398 ut_assertok(device_unbind(dev));
399
400 return 0;
401}
Simon Glass974dccd2020-07-28 19:41:12 -0600402DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700403
404/* Check that we can perform operations on a device (do a ping) */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500405int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700406 uint32_t base, struct dm_test_priv *priv)
407{
408 int expected;
409 int pingret;
410
Simon Glass71fa5b42020-12-03 16:55:18 -0700411 /* Getting the child device should allocate plat / priv */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700412 ut_assertok(testfdt_ping(dev, 10, &pingret));
Simon Glass95588622020-12-22 19:30:28 -0700413 ut_assert(dev_get_priv(dev));
414 ut_assert(dev_get_plat(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700415
416 expected = 10 + base;
417 ut_asserteq(expected, pingret);
418
419 /* Do another ping */
420 ut_assertok(testfdt_ping(dev, 20, &pingret));
421 expected = 20 + base;
422 ut_asserteq(expected, pingret);
423
424 /* Now check the ping_total */
Simon Glass95588622020-12-22 19:30:28 -0700425 priv = dev_get_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700426 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
427 priv->ping_total);
428
429 return 0;
430}
431
432/* Check that we can perform operations on devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500433static int dm_test_operations(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700434{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200435 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700436 int i;
437
438 /*
439 * Now check that the ping adds are what we expect. This is using the
440 * ping-add property in each node.
441 */
442 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
443 uint32_t base;
444
445 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
446
447 /*
448 * Get the 'reg' property, which tells us what the ping add
Simon Glass71fa5b42020-12-03 16:55:18 -0700449 * should be. We don't use the plat because we want
Simon Glassb2c1cac2014-02-26 15:59:21 -0700450 * to test the code that sets that up (testfdt_drv_probe()).
451 */
452 base = test_pdata[i].ping_add;
453 debug("dev=%d, base=%d\n", i, base);
454
Simon Glass95588622020-12-22 19:30:28 -0700455 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700456 }
457
458 return 0;
459}
Simon Glass974dccd2020-07-28 19:41:12 -0600460DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700461
462/* Remove all drivers and check that things work */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500463static int dm_test_remove(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700464{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200465 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700466 int i;
467
468 for (i = 0; i < 3; i++) {
469 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
470 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700471 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700472 "Driver %d/%s not activated", i, dev->name);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100473 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glass6211d762020-12-19 10:40:10 -0700474 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700475 "Driver %d/%s should have deactivated", i,
476 dev->name);
Simon Glass95588622020-12-22 19:30:28 -0700477 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700478 }
479
480 return 0;
481}
Simon Glass974dccd2020-07-28 19:41:12 -0600482DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700483
484/* Remove and recreate everything, check for memory leaks */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500485static int dm_test_leak(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700486{
487 int i;
488
489 for (i = 0; i < 2; i++) {
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200490 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700491 int ret;
492 int id;
493
Joe Hershberger3a77be52015-05-20 14:27:27 -0500494 dm_leak_check_start(uts);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700495
Simon Glassb75b15b2020-12-03 16:55:23 -0700496 ut_assertok(dm_scan_plat(false));
Simon Glass5039cab2020-11-28 17:50:09 -0700497 ut_assertok(dm_scan_fdt(false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700498
499 /* Scanning the uclass is enough to probe all the devices */
500 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
501 for (ret = uclass_first_device(UCLASS_TEST, &dev);
502 dev;
503 ret = uclass_next_device(&dev))
504 ;
505 ut_assertok(ret);
506 }
507
Joe Hershberger3a77be52015-05-20 14:27:27 -0500508 ut_assertok(dm_leak_check_end(uts));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700509 }
510
511 return 0;
512}
513DM_TEST(dm_test_leak, 0);
514
515/* Test uclass init/destroy methods */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500516static int dm_test_uclass(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700517{
518 struct uclass *uc;
519
520 ut_assertok(uclass_get(UCLASS_TEST, &uc));
521 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
522 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
Simon Glass95588622020-12-22 19:30:28 -0700523 ut_assert(uclass_get_priv(uc));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700524
525 ut_assertok(uclass_destroy(uc));
526 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
527 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
528
529 return 0;
530}
531DM_TEST(dm_test_uclass, 0);
532
533/**
534 * create_children() - Create children of a parent node
535 *
536 * @dms: Test system state
537 * @parent: Parent device
538 * @count: Number of children to create
539 * @key: Key value to put in first child. Subsequence children
540 * receive an incrementing value
541 * @child: If not NULL, then the child device pointers are written into
542 * this array.
543 * @return 0 if OK, -ve on error
544 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500545static int create_children(struct unit_test_state *uts, struct udevice *parent,
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200546 int count, int key, struct udevice *child[])
Simon Glassb2c1cac2014-02-26 15:59:21 -0700547{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200548 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700549 int i;
550
551 for (i = 0; i < count; i++) {
552 struct dm_test_pdata *pdata;
553
Simon Glassfef72b72014-07-23 06:55:03 -0600554 ut_assertok(device_bind_by_name(parent, false,
555 &driver_info_manual, &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700556 pdata = calloc(1, sizeof(*pdata));
557 pdata->ping_add = key + i;
Simon Glass95588622020-12-22 19:30:28 -0700558 dev_set_plat(dev, pdata);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700559 if (child)
560 child[i] = dev;
561 }
562
563 return 0;
564}
565
566#define NODE_COUNT 10
567
Joe Hershberger3a77be52015-05-20 14:27:27 -0500568static int dm_test_children(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700569{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500570 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200571 struct udevice *top[NODE_COUNT];
572 struct udevice *child[NODE_COUNT];
573 struct udevice *grandchild[NODE_COUNT];
574 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700575 int total;
576 int ret;
577 int i;
578
579 /* We don't care about the numbering for this test */
580 dms->skip_post_probe = 1;
581
582 ut_assert(NODE_COUNT > 5);
583
584 /* First create 10 top-level children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500585 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700586
587 /* Now a few have their own children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500588 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
589 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700590
591 /* And grandchildren */
592 for (i = 0; i < NODE_COUNT; i++)
Joe Hershberger3a77be52015-05-20 14:27:27 -0500593 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700594 i == 2 ? grandchild : NULL));
595
596 /* Check total number of devices */
597 total = NODE_COUNT * (3 + NODE_COUNT);
598 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
599
600 /* Try probing one of the grandchildren */
601 ut_assertok(uclass_get_device(UCLASS_TEST,
602 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
603 ut_asserteq_ptr(grandchild[0], dev);
604
605 /*
606 * This should have probed the child and top node also, for a total
607 * of 3 nodes.
608 */
609 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
610
611 /* Probe the other grandchildren */
612 for (i = 1; i < NODE_COUNT; i++)
613 ut_assertok(device_probe(grandchild[i]));
614
615 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
616
617 /* Probe everything */
618 for (ret = uclass_first_device(UCLASS_TEST, &dev);
619 dev;
620 ret = uclass_next_device(&dev))
621 ;
622 ut_assertok(ret);
623
624 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
625
626 /* Remove a top-level child and check that the children are removed */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100627 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700628 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
629 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
630
631 /* Try one with grandchildren */
632 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
633 ut_asserteq_ptr(dev, top[5]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100634 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700635 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
636 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
637
638 /* Try the same with unbind */
639 ut_assertok(device_unbind(top[2]));
640 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
641 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
642
643 /* Try one with grandchildren */
644 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
645 ut_asserteq_ptr(dev, top[6]);
646 ut_assertok(device_unbind(top[5]));
647 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
648 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
649
650 return 0;
651}
652DM_TEST(dm_test_children, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600653
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300654static int dm_test_device_reparent(struct unit_test_state *uts)
655{
656 struct dm_test_state *dms = uts->priv;
657 struct udevice *top[NODE_COUNT];
658 struct udevice *child[NODE_COUNT];
659 struct udevice *grandchild[NODE_COUNT];
660 struct udevice *dev;
661 int total;
662 int ret;
663 int i;
664
665 /* We don't care about the numbering for this test */
666 dms->skip_post_probe = 1;
667
668 ut_assert(NODE_COUNT > 5);
669
670 /* First create 10 top-level children */
671 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
672
673 /* Now a few have their own children */
674 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
675 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
676
677 /* And grandchildren */
678 for (i = 0; i < NODE_COUNT; i++)
679 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
680 i == 2 ? grandchild : NULL));
681
682 /* Check total number of devices */
683 total = NODE_COUNT * (3 + NODE_COUNT);
684 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
685
686 /* Probe everything */
687 for (i = 0; i < total; i++)
688 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
689
690 /* Re-parent top-level children with no grandchildren. */
691 ut_assertok(device_reparent(top[3], top[0]));
692 /* try to get devices */
693 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
694 dev;
695 ret = uclass_find_next_device(&dev)) {
696 ut_assert(!ret);
697 ut_assertnonnull(dev);
698 }
699
700 ut_assertok(device_reparent(top[4], top[0]));
701 /* try to get devices */
702 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
703 dev;
704 ret = uclass_find_next_device(&dev)) {
705 ut_assert(!ret);
706 ut_assertnonnull(dev);
707 }
708
709 /* Re-parent top-level children with grandchildren. */
710 ut_assertok(device_reparent(top[2], top[0]));
711 /* try to get devices */
712 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
713 dev;
714 ret = uclass_find_next_device(&dev)) {
715 ut_assert(!ret);
716 ut_assertnonnull(dev);
717 }
718
719 ut_assertok(device_reparent(top[5], top[2]));
720 /* try to get devices */
721 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
722 dev;
723 ret = uclass_find_next_device(&dev)) {
724 ut_assert(!ret);
725 ut_assertnonnull(dev);
726 }
727
728 /* Re-parent grandchildren. */
729 ut_assertok(device_reparent(grandchild[0], top[1]));
730 /* try to get devices */
731 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
732 dev;
733 ret = uclass_find_next_device(&dev)) {
734 ut_assert(!ret);
735 ut_assertnonnull(dev);
736 }
737
738 ut_assertok(device_reparent(grandchild[1], top[1]));
739 /* try to get devices */
740 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
741 dev;
742 ret = uclass_find_next_device(&dev)) {
743 ut_assert(!ret);
744 ut_assertnonnull(dev);
745 }
746
747 /* Remove re-pareneted devices. */
748 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
749 /* try to get devices */
750 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
751 dev;
752 ret = uclass_find_next_device(&dev)) {
753 ut_assert(!ret);
754 ut_assertnonnull(dev);
755 }
756
757 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
758 /* try to get devices */
759 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
760 dev;
761 ret = uclass_find_next_device(&dev)) {
762 ut_assert(!ret);
763 ut_assertnonnull(dev);
764 }
765
766 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
767 /* try to get devices */
768 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
769 dev;
770 ret = uclass_find_next_device(&dev)) {
771 ut_assert(!ret);
772 ut_assertnonnull(dev);
773 }
774
775 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
776 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
777 dev;
778 ret = uclass_find_next_device(&dev)) {
779 ut_assert(!ret);
780 ut_assertnonnull(dev);
781 }
782
783 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
784 /* try to get devices */
785 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
786 dev;
787 ret = uclass_find_next_device(&dev)) {
788 ut_assert(!ret);
789 ut_assertnonnull(dev);
790 }
791
792 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
793 /* try to get devices */
794 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
795 dev;
796 ret = uclass_find_next_device(&dev)) {
797 ut_assert(!ret);
798 ut_assertnonnull(dev);
799 }
800
801 /* Try the same with unbind */
802 ut_assertok(device_unbind(top[3]));
803 ut_assertok(device_unbind(top[4]));
804 ut_assertok(device_unbind(top[5]));
805 ut_assertok(device_unbind(top[2]));
806
807 ut_assertok(device_unbind(grandchild[0]));
808 ut_assertok(device_unbind(grandchild[1]));
809
810 return 0;
811}
812DM_TEST(dm_test_device_reparent, 0);
813
Simon Glassfef72b72014-07-23 06:55:03 -0600814/* Test that pre-relocation devices work as expected */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500815static int dm_test_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600816{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500817 struct dm_test_state *dms = uts->priv;
Simon Glassfef72b72014-07-23 06:55:03 -0600818 struct udevice *dev;
819
820 /* The normal driver should refuse to bind before relocation */
821 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
822 &driver_info_manual, &dev));
823
824 /* But this one is marked pre-reloc */
825 ut_assertok(device_bind_by_name(dms->root, true,
826 &driver_info_pre_reloc, &dev));
827
828 return 0;
829}
830DM_TEST(dm_test_pre_reloc, 0);
Simon Glassde708672014-07-23 06:55:15 -0600831
Stefan Roeseeaffda72017-03-27 11:02:43 +0200832/*
833 * Test that removal of devices, either via the "normal" device_remove()
834 * API or via the device driver selective flag works as expected
835 */
836static int dm_test_remove_active_dma(struct unit_test_state *uts)
837{
838 struct dm_test_state *dms = uts->priv;
839 struct udevice *dev;
840
841 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
842 &dev));
843 ut_assert(dev);
844
845 /* Probe the device */
846 ut_assertok(device_probe(dev));
847
848 /* Test if device is active right now */
849 ut_asserteq(true, device_active(dev));
850
851 /* Remove the device via selective remove flag */
852 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
853
854 /* Test if device is inactive right now */
855 ut_asserteq(false, device_active(dev));
856
857 /* Probe the device again */
858 ut_assertok(device_probe(dev));
859
860 /* Test if device is active right now */
861 ut_asserteq(true, device_active(dev));
862
863 /* Remove the device via "normal" remove API */
864 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
865
866 /* Test if device is inactive right now */
867 ut_asserteq(false, device_active(dev));
868
869 /*
870 * Test if a device without the active DMA flags is not removed upon
871 * the active DMA remove call
872 */
873 ut_assertok(device_unbind(dev));
874 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
875 &dev));
876 ut_assert(dev);
877
878 /* Probe the device */
879 ut_assertok(device_probe(dev));
880
881 /* Test if device is active right now */
882 ut_asserteq(true, device_active(dev));
883
884 /* Remove the device via selective remove flag */
885 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
886
887 /* Test if device is still active right now */
888 ut_asserteq(true, device_active(dev));
889
890 return 0;
891}
892DM_TEST(dm_test_remove_active_dma, 0);
893
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700894/* Test removal of 'vital' devices */
895static int dm_test_remove_vital(struct unit_test_state *uts)
896{
897 struct dm_test_state *dms = uts->priv;
898 struct udevice *normal, *dma, *vital, *dma_vital;
899
900 /* Skip the behaviour in test_post_probe() */
901 dms->skip_post_probe = 1;
902
903 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
904 &normal));
905 ut_assertnonnull(normal);
906
907 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
908 &dma));
909 ut_assertnonnull(dma);
910
911 ut_assertok(device_bind_by_name(dms->root, false,
912 &driver_info_vital_clk, &vital));
913 ut_assertnonnull(vital);
914
915 ut_assertok(device_bind_by_name(dms->root, false,
916 &driver_info_act_dma_vital_clk,
917 &dma_vital));
918 ut_assertnonnull(dma_vital);
919
920 /* Probe the devices */
921 ut_assertok(device_probe(normal));
922 ut_assertok(device_probe(dma));
923 ut_assertok(device_probe(vital));
924 ut_assertok(device_probe(dma_vital));
925
926 /* Check that devices are active right now */
927 ut_asserteq(true, device_active(normal));
928 ut_asserteq(true, device_active(dma));
929 ut_asserteq(true, device_active(vital));
930 ut_asserteq(true, device_active(dma_vital));
931
932 /* Remove active devices via selective remove flag */
933 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
934
935 /*
936 * Check that this only has an effect on the dma device, since two
937 * devices are vital and the third does not have active DMA
938 */
939 ut_asserteq(true, device_active(normal));
940 ut_asserteq(false, device_active(dma));
941 ut_asserteq(true, device_active(vital));
942 ut_asserteq(true, device_active(dma_vital));
943
944 /* Remove active devices via selective remove flag */
945 ut_assertok(device_probe(dma));
946 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
947
948 /* This should have affected both active-dma devices */
949 ut_asserteq(true, device_active(normal));
950 ut_asserteq(false, device_active(dma));
951 ut_asserteq(true, device_active(vital));
952 ut_asserteq(false, device_active(dma_vital));
953
954 /* Remove non-vital devices */
955 ut_assertok(device_probe(dma));
956 ut_assertok(device_probe(dma_vital));
957 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
958
959 /* This should have affected only non-vital devices */
960 ut_asserteq(false, device_active(normal));
961 ut_asserteq(false, device_active(dma));
962 ut_asserteq(true, device_active(vital));
963 ut_asserteq(true, device_active(dma_vital));
964
965 /* Remove vital devices via normal remove flag */
966 ut_assertok(device_probe(normal));
967 ut_assertok(device_probe(dma));
968 dm_remove_devices_flags(DM_REMOVE_NORMAL);
969
970 /* Check that all devices are inactive right now */
971 ut_asserteq(false, device_active(normal));
972 ut_asserteq(false, device_active(dma));
973 ut_asserteq(false, device_active(vital));
974 ut_asserteq(false, device_active(dma_vital));
975
976 return 0;
977}
978DM_TEST(dm_test_remove_vital, 0);
979
Joe Hershberger3a77be52015-05-20 14:27:27 -0500980static int dm_test_uclass_before_ready(struct unit_test_state *uts)
Simon Glassde708672014-07-23 06:55:15 -0600981{
982 struct uclass *uc;
983
984 ut_assertok(uclass_get(UCLASS_TEST, &uc));
985
Simon Glass51a0eac2015-04-19 07:21:02 -0600986 gd->dm_root = NULL;
987 gd->dm_root_f = NULL;
988 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
989
Simon Glassde708672014-07-23 06:55:15 -0600990 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
991
992 return 0;
993}
Simon Glassde708672014-07-23 06:55:15 -0600994DM_TEST(dm_test_uclass_before_ready, 0);
Simon Glass98fd5d12015-01-25 08:27:04 -0700995
Joe Hershberger3a77be52015-05-20 14:27:27 -0500996static int dm_test_uclass_devices_find(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200997{
998 struct udevice *dev;
999 int ret;
1000
1001 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1002 dev;
1003 ret = uclass_find_next_device(&dev)) {
1004 ut_assert(!ret);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001005 ut_assertnonnull(dev);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001006 }
1007
Simon Glass0bb44272019-09-25 08:55:55 -06001008 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001009 ut_assertnull(dev);
Marcel Ziswiler75ec16f2019-02-01 16:01:07 +01001010
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001011 return 0;
1012}
Simon Glass974dccd2020-07-28 19:41:12 -06001013DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001014
Joe Hershberger3a77be52015-05-20 14:27:27 -05001015static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001016{
1017 struct udevice *finddev;
1018 struct udevice *testdev;
1019 int findret, ret;
1020
1021 /*
1022 * For each test device found in fdt like: "a-test", "b-test", etc.,
1023 * use its name and try to find it by uclass_find_device_by_name().
1024 * Then, on success check if:
1025 * - current 'testdev' name is equal to the returned 'finddev' name
1026 * - current 'testdev' pointer is equal to the returned 'finddev'
1027 *
1028 * We assume that, each uclass's device name is unique, so if not, then
1029 * this will fail on checking condition: testdev == finddev, since the
1030 * uclass_find_device_by_name(), returns the first device by given name.
1031 */
1032 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1033 testdev;
1034 ret = uclass_find_next_device(&testdev)) {
1035 ut_assertok(ret);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001036 ut_assertnonnull(testdev);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001037
1038 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1039 testdev->name,
1040 &finddev);
1041
1042 ut_assertok(findret);
1043 ut_assert(testdev);
1044 ut_asserteq_str(testdev->name, finddev->name);
1045 ut_asserteq_ptr(testdev, finddev);
1046 }
1047
1048 return 0;
1049}
Simon Glass974dccd2020-07-28 19:41:12 -06001050DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001051
Joe Hershberger3a77be52015-05-20 14:27:27 -05001052static int dm_test_uclass_devices_get(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001053{
1054 struct udevice *dev;
1055 int ret;
1056
1057 for (ret = uclass_first_device(UCLASS_TEST, &dev);
1058 dev;
1059 ret = uclass_next_device(&dev)) {
1060 ut_assert(!ret);
1061 ut_assert(dev);
1062 ut_assert(device_active(dev));
1063 }
1064
1065 return 0;
1066}
Simon Glass974dccd2020-07-28 19:41:12 -06001067DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001068
Joe Hershberger3a77be52015-05-20 14:27:27 -05001069static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001070{
1071 struct udevice *finddev;
1072 struct udevice *testdev;
1073 int ret, findret;
1074
1075 /*
1076 * For each test device found in fdt like: "a-test", "b-test", etc.,
1077 * use its name and try to get it by uclass_get_device_by_name().
1078 * On success check if:
1079 * - returned finddev' is active
1080 * - current 'testdev' name is equal to the returned 'finddev' name
1081 * - current 'testdev' pointer is equal to the returned 'finddev'
1082 *
1083 * We asserts that the 'testdev' is active on each loop entry, so we
1084 * could be sure that the 'finddev' is activated too, but for sure
1085 * we check it again.
1086 *
1087 * We assume that, each uclass's device name is unique, so if not, then
1088 * this will fail on checking condition: testdev == finddev, since the
1089 * uclass_get_device_by_name(), returns the first device by given name.
1090 */
1091 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
1092 testdev;
1093 ret = uclass_next_device(&testdev)) {
1094 ut_assertok(ret);
1095 ut_assert(testdev);
1096 ut_assert(device_active(testdev));
1097
1098 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1099 testdev->name,
1100 &finddev);
1101
1102 ut_assertok(findret);
1103 ut_assert(finddev);
1104 ut_assert(device_active(finddev));
1105 ut_asserteq_str(testdev->name, finddev->name);
1106 ut_asserteq_ptr(testdev, finddev);
1107 }
1108
1109 return 0;
1110}
Simon Glass974dccd2020-07-28 19:41:12 -06001111DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001112
Joe Hershberger3a77be52015-05-20 14:27:27 -05001113static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
Simon Glass98fd5d12015-01-25 08:27:04 -07001114{
1115 struct udevice *dev;
1116
1117 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1118 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1119
1120 return 0;
1121}
Simon Glass974dccd2020-07-28 19:41:12 -06001122DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
Simon Glass70e35b42017-12-28 13:14:15 -07001123
1124static int dm_test_uclass_names(struct unit_test_state *uts)
1125{
1126 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1127 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1128
1129 return 0;
1130}
Simon Glass974dccd2020-07-28 19:41:12 -06001131DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
Simon Glassb775e872018-10-01 12:22:07 -06001132
1133static int dm_test_inactive_child(struct unit_test_state *uts)
1134{
1135 struct dm_test_state *dms = uts->priv;
1136 struct udevice *parent, *dev1, *dev2;
1137
1138 /* Skip the behaviour in test_post_probe() */
1139 dms->skip_post_probe = 1;
1140
1141 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1142
1143 /*
1144 * Create a child but do not activate it. Calling the function again
1145 * should return the same child.
1146 */
1147 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1148 UCLASS_TEST, &dev1));
Simon Glass65130cd2020-12-28 20:34:56 -07001149 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
Simon Glass884870f2020-11-28 17:50:01 -07001150 "test_child", 0, ofnode_null(), &dev1));
Simon Glassb775e872018-10-01 12:22:07 -06001151
1152 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1153 &dev2));
1154 ut_asserteq_ptr(dev1, dev2);
1155
1156 ut_assertok(device_probe(dev1));
1157 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1158 UCLASS_TEST, &dev2));
1159
1160 return 0;
1161}
Simon Glass974dccd2020-07-28 19:41:12 -06001162DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
Simon Glass6a109b32020-12-16 21:20:10 -07001163
1164/* Make sure all bound devices have a sequence number */
1165static int dm_test_all_have_seq(struct unit_test_state *uts)
1166{
1167 struct udevice *dev;
1168 struct uclass *uc;
1169
Simon Glass784cd9e2020-12-19 10:40:17 -07001170 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Simon Glass6a109b32020-12-16 21:20:10 -07001171 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -07001172 if (dev->seq_ == -1)
Simon Glass6a109b32020-12-16 21:20:10 -07001173 printf("Device '%s' has no seq (%d)\n",
Simon Glass5e349922020-12-19 10:40:09 -07001174 dev->name, dev->seq_);
1175 ut_assert(dev->seq_ != -1);
Simon Glass6a109b32020-12-16 21:20:10 -07001176 }
1177 }
1178
1179 return 0;
1180}
1181DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);