blob: 2210345dd141843320aea1c2b46762aa8519c948 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <dm/device-internal.h>
16#include <dm/root.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070017#include <dm/util.h>
18#include <dm/test.h>
19#include <dm/uclass-internal.h>
Simon Glass75c4d412020-07-19 10:15:37 -060020#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050021#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070022
23DECLARE_GLOBAL_DATA_PTR;
24
25enum {
26 TEST_INTVAL1 = 0,
27 TEST_INTVAL2 = 3,
28 TEST_INTVAL3 = 6,
29 TEST_INTVAL_MANUAL = 101112,
Simon Glassfef72b72014-07-23 06:55:03 -060030 TEST_INTVAL_PRE_RELOC = 7,
Simon Glassb2c1cac2014-02-26 15:59:21 -070031};
32
33static const struct dm_test_pdata test_pdata[] = {
34 { .ping_add = TEST_INTVAL1, },
35 { .ping_add = TEST_INTVAL2, },
36 { .ping_add = TEST_INTVAL3, },
37};
38
39static const struct dm_test_pdata test_pdata_manual = {
40 .ping_add = TEST_INTVAL_MANUAL,
41};
42
Simon Glassfef72b72014-07-23 06:55:03 -060043static const struct dm_test_pdata test_pdata_pre_reloc = {
44 .ping_add = TEST_INTVAL_PRE_RELOC,
45};
46
Simon Glass1d8364a2020-12-28 20:34:54 -070047U_BOOT_DRVINFO(dm_test_info1) = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070048 .name = "test_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070049 .plat = &test_pdata[0],
Simon Glassb2c1cac2014-02-26 15:59:21 -070050};
51
Simon Glass1d8364a2020-12-28 20:34:54 -070052U_BOOT_DRVINFO(dm_test_info2) = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070053 .name = "test_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070054 .plat = &test_pdata[1],
Simon Glassb2c1cac2014-02-26 15:59:21 -070055};
56
Simon Glass1d8364a2020-12-28 20:34:54 -070057U_BOOT_DRVINFO(dm_test_info3) = {
Simon Glassb2c1cac2014-02-26 15:59:21 -070058 .name = "test_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070059 .plat = &test_pdata[2],
Simon Glassb2c1cac2014-02-26 15:59:21 -070060};
61
62static struct driver_info driver_info_manual = {
63 .name = "test_manual_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070064 .plat = &test_pdata_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -070065};
66
Simon Glassfef72b72014-07-23 06:55:03 -060067static struct driver_info driver_info_pre_reloc = {
68 .name = "test_pre_reloc_drv",
Simon Glass71fa5b42020-12-03 16:55:18 -070069 .plat = &test_pdata_pre_reloc,
Simon Glassfef72b72014-07-23 06:55:03 -060070};
71
Stefan Roeseeaffda72017-03-27 11:02:43 +020072static struct driver_info driver_info_act_dma = {
73 .name = "test_act_dma_drv",
74};
75
Marek Vasutabbdbbd2021-01-24 14:32:46 -070076static struct driver_info driver_info_vital_clk = {
77 .name = "test_vital_clk_drv",
78};
79
80static struct driver_info driver_info_act_dma_vital_clk = {
81 .name = "test_act_dma_vital_clk_drv",
82};
83
Joe Hershberger3a77be52015-05-20 14:27:27 -050084void dm_leak_check_start(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060085{
Joe Hershberger3a77be52015-05-20 14:27:27 -050086 uts->start = mallinfo();
87 if (!uts->start.uordblks)
Simon Glass0927a6f2014-10-04 11:29:50 -060088 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
89}
90
Joe Hershberger3a77be52015-05-20 14:27:27 -050091int dm_leak_check_end(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060092{
93 struct mallinfo end;
Simon Glass94d22182015-09-12 08:45:20 -060094 int id, diff;
Simon Glass0927a6f2014-10-04 11:29:50 -060095
96 /* Don't delete the root class, since we started with that */
97 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
98 struct uclass *uc;
99
100 uc = uclass_find(id);
101 if (!uc)
102 continue;
103 ut_assertok(uclass_destroy(uc));
104 }
105
106 end = mallinfo();
Simon Glass94d22182015-09-12 08:45:20 -0600107 diff = end.uordblks - uts->start.uordblks;
108 if (diff > 0)
109 printf("Leak: lost %#xd bytes\n", diff);
110 else if (diff < 0)
111 printf("Leak: gained %#xd bytes\n", -diff);
Joe Hershberger3a77be52015-05-20 14:27:27 -0500112 ut_asserteq(uts->start.uordblks, end.uordblks);
Simon Glass0927a6f2014-10-04 11:29:50 -0600113
114 return 0;
115}
116
Simon Glass71fa5b42020-12-03 16:55:18 -0700117/* Test that binding with plat occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500118static int dm_test_autobind(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700119{
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 */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700126 ut_assert(uts->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{
209 int expected_base_add;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200210 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700211 struct uclass *uc;
212 int i;
213
214 ut_assertok(uclass_get(UCLASS_TEST, &uc));
215 ut_assert(uc);
216
217 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700218 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700219 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
220
221 /* The root device should not be activated until needed */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700222 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700223
224 /*
225 * We should be able to find the three test devices, and they should
226 * all be activated as they are used (lazy activation, required by
227 * U-Boot)
228 */
229 for (i = 0; i < 3; i++) {
230 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
231 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700232 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700233 "Driver %d/%s already activated", i, dev->name);
234
235 /* This should activate it */
236 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
237 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700238 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700239
240 /* Activating a device should activate the root device */
241 if (!i)
Simon Glassb98bfbc2021-03-07 17:34:57 -0700242 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700243 }
244
Simon Glass9c1f3822015-03-05 12:25:22 -0700245 /*
246 * Our 3 dm_test_info children should be passed to pre_probe and
247 * post_probe
248 */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700249 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700250 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700251
252 /* Also we can check the per-device data */
253 expected_base_add = 0;
254 for (i = 0; i < 3; i++) {
255 struct dm_test_uclass_perdev_priv *priv;
256 struct dm_test_pdata *pdata;
257
258 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
259 ut_assert(dev);
260
Simon Glassde0977b2015-03-05 12:25:20 -0700261 priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700262 ut_assert(priv);
263 ut_asserteq(expected_base_add, priv->base_add);
264
Simon Glass95588622020-12-22 19:30:28 -0700265 pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700266 expected_base_add += pdata->ping_add;
267 }
268
269 return 0;
270}
Simon Glass974dccd2020-07-28 19:41:12 -0600271DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700272
Simon Glass71fa5b42020-12-03 16:55:18 -0700273/* Check that we see the correct plat in each device */
Simon Glassb75b15b2020-12-03 16:55:23 -0700274static int dm_test_plat(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700275{
276 const struct dm_test_pdata *pdata;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200277 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700278 int i;
279
280 for (i = 0; i < 3; i++) {
281 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
282 ut_assert(dev);
Simon Glass95588622020-12-22 19:30:28 -0700283 pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700284 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
285 }
286
287 return 0;
288}
Simon Glassb75b15b2020-12-03 16:55:23 -0700289DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700290
291/* Test that we can bind, probe, remove, unbind a driver */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500292static int dm_test_lifecycle(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700293{
294 int op_count[DM_TEST_OP_COUNT];
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200295 struct udevice *dev, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700296 int pingret;
297 int ret;
298
299 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
300
Simon Glassb98bfbc2021-03-07 17:34:57 -0700301 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700302 &dev));
303 ut_assert(dev);
304 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
305 == op_count[DM_TEST_OP_BIND] + 1);
Simon Glass95588622020-12-22 19:30:28 -0700306 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700307
308 /* Probe the device - it should fail allocating private data */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700309 uts->force_fail_alloc = 1;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700310 ret = device_probe(dev);
311 ut_assert(ret == -ENOMEM);
312 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
313 == op_count[DM_TEST_OP_PROBE] + 1);
Simon Glass95588622020-12-22 19:30:28 -0700314 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700315
316 /* Try again without the alloc failure */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700317 uts->force_fail_alloc = 0;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700318 ut_assertok(device_probe(dev));
319 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
320 == op_count[DM_TEST_OP_PROBE] + 2);
Simon Glass95588622020-12-22 19:30:28 -0700321 ut_assert(dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700322
323 /* This should be device 3 in the uclass */
324 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
325 ut_assert(dev == test_dev);
326
327 /* Try ping */
328 ut_assertok(test_ping(dev, 100, &pingret));
329 ut_assert(pingret == 102);
330
331 /* Now remove device 3 */
332 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100333 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700334 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
335
336 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
337 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
338 ut_assertok(device_unbind(dev));
339 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
340 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
341
342 return 0;
343}
Simon Glass974dccd2020-07-28 19:41:12 -0600344DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700345
346/* Test that we can bind/unbind and the lists update correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500347static int dm_test_ordering(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700348{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200349 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700350 int pingret;
351
Simon Glassb98bfbc2021-03-07 17:34:57 -0700352 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700353 &dev));
354 ut_assert(dev);
355
356 /* Bind two new devices (numbers 4 and 5) */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700357 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700358 &dev_penultimate));
359 ut_assert(dev_penultimate);
Simon Glassb98bfbc2021-03-07 17:34:57 -0700360 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700361 &dev_last));
362 ut_assert(dev_last);
363
364 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100365 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700366 ut_assertok(device_unbind(dev));
367
368 /* The device numbering should have shifted down one */
369 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
370 ut_assert(dev_penultimate == test_dev);
371 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
372 ut_assert(dev_last == test_dev);
373
374 /* Add back the original device 3, now in position 5 */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700375 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassfef72b72014-07-23 06:55:03 -0600376 &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700377 ut_assert(dev);
378
379 /* Try ping */
380 ut_assertok(test_ping(dev, 100, &pingret));
381 ut_assert(pingret == 102);
382
383 /* Remove 3 and 4 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100384 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700385 ut_assertok(device_unbind(dev_penultimate));
Stefan Roese80b5bc92017-03-20 12:51:48 +0100386 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700387 ut_assertok(device_unbind(dev_last));
388
389 /* Our device should now be in position 3 */
390 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
391 ut_assert(dev == test_dev);
392
393 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100394 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700395 ut_assertok(device_unbind(dev));
396
397 return 0;
398}
Simon Glass974dccd2020-07-28 19:41:12 -0600399DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700400
401/* Check that we can perform operations on a device (do a ping) */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500402int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700403 uint32_t base, struct dm_test_priv *priv)
404{
405 int expected;
406 int pingret;
407
Simon Glass71fa5b42020-12-03 16:55:18 -0700408 /* Getting the child device should allocate plat / priv */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700409 ut_assertok(testfdt_ping(dev, 10, &pingret));
Simon Glass95588622020-12-22 19:30:28 -0700410 ut_assert(dev_get_priv(dev));
411 ut_assert(dev_get_plat(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700412
413 expected = 10 + base;
414 ut_asserteq(expected, pingret);
415
416 /* Do another ping */
417 ut_assertok(testfdt_ping(dev, 20, &pingret));
418 expected = 20 + base;
419 ut_asserteq(expected, pingret);
420
421 /* Now check the ping_total */
Simon Glass95588622020-12-22 19:30:28 -0700422 priv = dev_get_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700423 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
424 priv->ping_total);
425
426 return 0;
427}
428
429/* Check that we can perform operations on devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500430static int dm_test_operations(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700431{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200432 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700433 int i;
434
435 /*
436 * Now check that the ping adds are what we expect. This is using the
437 * ping-add property in each node.
438 */
439 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
440 uint32_t base;
441
442 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
443
444 /*
445 * Get the 'reg' property, which tells us what the ping add
Simon Glass71fa5b42020-12-03 16:55:18 -0700446 * should be. We don't use the plat because we want
Simon Glassb2c1cac2014-02-26 15:59:21 -0700447 * to test the code that sets that up (testfdt_drv_probe()).
448 */
449 base = test_pdata[i].ping_add;
450 debug("dev=%d, base=%d\n", i, base);
451
Simon Glass95588622020-12-22 19:30:28 -0700452 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700453 }
454
455 return 0;
456}
Simon Glass974dccd2020-07-28 19:41:12 -0600457DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700458
459/* Remove all drivers and check that things work */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500460static int dm_test_remove(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700461{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200462 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700463 int i;
464
465 for (i = 0; i < 3; i++) {
466 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
467 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700468 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700469 "Driver %d/%s not activated", i, dev->name);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100470 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
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 should have deactivated", i,
473 dev->name);
Simon Glass95588622020-12-22 19:30:28 -0700474 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700475 }
476
477 return 0;
478}
Simon Glass974dccd2020-07-28 19:41:12 -0600479DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700480
481/* Remove and recreate everything, check for memory leaks */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500482static int dm_test_leak(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700483{
484 int i;
485
486 for (i = 0; i < 2; i++) {
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200487 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700488 int ret;
489 int id;
490
Joe Hershberger3a77be52015-05-20 14:27:27 -0500491 dm_leak_check_start(uts);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700492
Simon Glassb75b15b2020-12-03 16:55:23 -0700493 ut_assertok(dm_scan_plat(false));
Simon Glass5039cab2020-11-28 17:50:09 -0700494 ut_assertok(dm_scan_fdt(false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700495
496 /* Scanning the uclass is enough to probe all the devices */
497 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
498 for (ret = uclass_first_device(UCLASS_TEST, &dev);
499 dev;
500 ret = uclass_next_device(&dev))
501 ;
502 ut_assertok(ret);
503 }
504
Joe Hershberger3a77be52015-05-20 14:27:27 -0500505 ut_assertok(dm_leak_check_end(uts));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700506 }
507
508 return 0;
509}
510DM_TEST(dm_test_leak, 0);
511
512/* Test uclass init/destroy methods */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500513static int dm_test_uclass(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700514{
515 struct uclass *uc;
516
517 ut_assertok(uclass_get(UCLASS_TEST, &uc));
518 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
519 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
Simon Glass95588622020-12-22 19:30:28 -0700520 ut_assert(uclass_get_priv(uc));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700521
522 ut_assertok(uclass_destroy(uc));
523 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
524 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
525
526 return 0;
527}
528DM_TEST(dm_test_uclass, 0);
529
530/**
531 * create_children() - Create children of a parent node
532 *
533 * @dms: Test system state
534 * @parent: Parent device
535 * @count: Number of children to create
536 * @key: Key value to put in first child. Subsequence children
537 * receive an incrementing value
538 * @child: If not NULL, then the child device pointers are written into
539 * this array.
540 * @return 0 if OK, -ve on error
541 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500542static int create_children(struct unit_test_state *uts, struct udevice *parent,
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200543 int count, int key, struct udevice *child[])
Simon Glassb2c1cac2014-02-26 15:59:21 -0700544{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200545 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700546 int i;
547
548 for (i = 0; i < count; i++) {
549 struct dm_test_pdata *pdata;
550
Simon Glassfef72b72014-07-23 06:55:03 -0600551 ut_assertok(device_bind_by_name(parent, false,
552 &driver_info_manual, &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700553 pdata = calloc(1, sizeof(*pdata));
554 pdata->ping_add = key + i;
Simon Glass95588622020-12-22 19:30:28 -0700555 dev_set_plat(dev, pdata);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700556 if (child)
557 child[i] = dev;
558 }
559
560 return 0;
561}
562
563#define NODE_COUNT 10
564
Joe Hershberger3a77be52015-05-20 14:27:27 -0500565static int dm_test_children(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700566{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200567 struct udevice *top[NODE_COUNT];
568 struct udevice *child[NODE_COUNT];
569 struct udevice *grandchild[NODE_COUNT];
570 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700571 int total;
572 int ret;
573 int i;
574
575 /* We don't care about the numbering for this test */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700576 uts->skip_post_probe = 1;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700577
578 ut_assert(NODE_COUNT > 5);
579
580 /* First create 10 top-level children */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700581 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700582
583 /* Now a few have their own children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500584 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
585 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700586
587 /* And grandchildren */
588 for (i = 0; i < NODE_COUNT; i++)
Joe Hershberger3a77be52015-05-20 14:27:27 -0500589 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700590 i == 2 ? grandchild : NULL));
591
592 /* Check total number of devices */
593 total = NODE_COUNT * (3 + NODE_COUNT);
594 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
595
596 /* Try probing one of the grandchildren */
597 ut_assertok(uclass_get_device(UCLASS_TEST,
598 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
599 ut_asserteq_ptr(grandchild[0], dev);
600
601 /*
602 * This should have probed the child and top node also, for a total
603 * of 3 nodes.
604 */
605 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
606
607 /* Probe the other grandchildren */
608 for (i = 1; i < NODE_COUNT; i++)
609 ut_assertok(device_probe(grandchild[i]));
610
611 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
612
613 /* Probe everything */
614 for (ret = uclass_first_device(UCLASS_TEST, &dev);
615 dev;
616 ret = uclass_next_device(&dev))
617 ;
618 ut_assertok(ret);
619
620 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
621
622 /* Remove a top-level child and check that the children are removed */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100623 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700624 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
625 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
626
627 /* Try one with grandchildren */
628 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
629 ut_asserteq_ptr(dev, top[5]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100630 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700631 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
632 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
633
634 /* Try the same with unbind */
635 ut_assertok(device_unbind(top[2]));
636 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
637 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
638
639 /* Try one with grandchildren */
640 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
641 ut_asserteq_ptr(dev, top[6]);
642 ut_assertok(device_unbind(top[5]));
643 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
644 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
645
646 return 0;
647}
648DM_TEST(dm_test_children, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600649
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300650static int dm_test_device_reparent(struct unit_test_state *uts)
651{
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300652 struct udevice *top[NODE_COUNT];
653 struct udevice *child[NODE_COUNT];
654 struct udevice *grandchild[NODE_COUNT];
655 struct udevice *dev;
656 int total;
657 int ret;
658 int i;
659
660 /* We don't care about the numbering for this test */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700661 uts->skip_post_probe = 1;
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300662
663 ut_assert(NODE_COUNT > 5);
664
665 /* First create 10 top-level children */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700666 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300667
668 /* Now a few have their own children */
669 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
670 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
671
672 /* And grandchildren */
673 for (i = 0; i < NODE_COUNT; i++)
674 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
675 i == 2 ? grandchild : NULL));
676
677 /* Check total number of devices */
678 total = NODE_COUNT * (3 + NODE_COUNT);
679 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
680
681 /* Probe everything */
682 for (i = 0; i < total; i++)
683 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
684
685 /* Re-parent top-level children with no grandchildren. */
686 ut_assertok(device_reparent(top[3], top[0]));
687 /* try to get devices */
688 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
689 dev;
690 ret = uclass_find_next_device(&dev)) {
691 ut_assert(!ret);
692 ut_assertnonnull(dev);
693 }
694
695 ut_assertok(device_reparent(top[4], top[0]));
696 /* try to get devices */
697 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
698 dev;
699 ret = uclass_find_next_device(&dev)) {
700 ut_assert(!ret);
701 ut_assertnonnull(dev);
702 }
703
704 /* Re-parent top-level children with grandchildren. */
705 ut_assertok(device_reparent(top[2], top[0]));
706 /* try to get devices */
707 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
708 dev;
709 ret = uclass_find_next_device(&dev)) {
710 ut_assert(!ret);
711 ut_assertnonnull(dev);
712 }
713
714 ut_assertok(device_reparent(top[5], top[2]));
715 /* try to get devices */
716 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
717 dev;
718 ret = uclass_find_next_device(&dev)) {
719 ut_assert(!ret);
720 ut_assertnonnull(dev);
721 }
722
723 /* Re-parent grandchildren. */
724 ut_assertok(device_reparent(grandchild[0], top[1]));
725 /* try to get devices */
726 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
727 dev;
728 ret = uclass_find_next_device(&dev)) {
729 ut_assert(!ret);
730 ut_assertnonnull(dev);
731 }
732
733 ut_assertok(device_reparent(grandchild[1], top[1]));
734 /* try to get devices */
735 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
736 dev;
737 ret = uclass_find_next_device(&dev)) {
738 ut_assert(!ret);
739 ut_assertnonnull(dev);
740 }
741
742 /* Remove re-pareneted devices. */
743 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
744 /* try to get devices */
745 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
746 dev;
747 ret = uclass_find_next_device(&dev)) {
748 ut_assert(!ret);
749 ut_assertnonnull(dev);
750 }
751
752 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
753 /* try to get devices */
754 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
755 dev;
756 ret = uclass_find_next_device(&dev)) {
757 ut_assert(!ret);
758 ut_assertnonnull(dev);
759 }
760
761 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
762 /* try to get devices */
763 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
764 dev;
765 ret = uclass_find_next_device(&dev)) {
766 ut_assert(!ret);
767 ut_assertnonnull(dev);
768 }
769
770 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
771 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
772 dev;
773 ret = uclass_find_next_device(&dev)) {
774 ut_assert(!ret);
775 ut_assertnonnull(dev);
776 }
777
778 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
779 /* try to get devices */
780 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
781 dev;
782 ret = uclass_find_next_device(&dev)) {
783 ut_assert(!ret);
784 ut_assertnonnull(dev);
785 }
786
787 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
788 /* try to get devices */
789 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
790 dev;
791 ret = uclass_find_next_device(&dev)) {
792 ut_assert(!ret);
793 ut_assertnonnull(dev);
794 }
795
796 /* Try the same with unbind */
797 ut_assertok(device_unbind(top[3]));
798 ut_assertok(device_unbind(top[4]));
799 ut_assertok(device_unbind(top[5]));
800 ut_assertok(device_unbind(top[2]));
801
802 ut_assertok(device_unbind(grandchild[0]));
803 ut_assertok(device_unbind(grandchild[1]));
804
805 return 0;
806}
807DM_TEST(dm_test_device_reparent, 0);
808
Simon Glassfef72b72014-07-23 06:55:03 -0600809/* Test that pre-relocation devices work as expected */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500810static int dm_test_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600811{
812 struct udevice *dev;
813
814 /* The normal driver should refuse to bind before relocation */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700815 ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
Simon Glassfef72b72014-07-23 06:55:03 -0600816 &driver_info_manual, &dev));
817
818 /* But this one is marked pre-reloc */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700819 ut_assertok(device_bind_by_name(uts->root, true,
Simon Glassfef72b72014-07-23 06:55:03 -0600820 &driver_info_pre_reloc, &dev));
821
822 return 0;
823}
824DM_TEST(dm_test_pre_reloc, 0);
Simon Glassde708672014-07-23 06:55:15 -0600825
Stefan Roeseeaffda72017-03-27 11:02:43 +0200826/*
827 * Test that removal of devices, either via the "normal" device_remove()
828 * API or via the device driver selective flag works as expected
829 */
830static int dm_test_remove_active_dma(struct unit_test_state *uts)
831{
Stefan Roeseeaffda72017-03-27 11:02:43 +0200832 struct udevice *dev;
833
Simon Glassb98bfbc2021-03-07 17:34:57 -0700834 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
Stefan Roeseeaffda72017-03-27 11:02:43 +0200835 &dev));
836 ut_assert(dev);
837
838 /* Probe the device */
839 ut_assertok(device_probe(dev));
840
841 /* Test if device is active right now */
842 ut_asserteq(true, device_active(dev));
843
844 /* Remove the device via selective remove flag */
845 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
846
847 /* Test if device is inactive right now */
848 ut_asserteq(false, device_active(dev));
849
850 /* Probe the device again */
851 ut_assertok(device_probe(dev));
852
853 /* Test if device is active right now */
854 ut_asserteq(true, device_active(dev));
855
856 /* Remove the device via "normal" remove API */
857 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
858
859 /* Test if device is inactive right now */
860 ut_asserteq(false, device_active(dev));
861
862 /*
863 * Test if a device without the active DMA flags is not removed upon
864 * the active DMA remove call
865 */
866 ut_assertok(device_unbind(dev));
Simon Glassb98bfbc2021-03-07 17:34:57 -0700867 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Stefan Roeseeaffda72017-03-27 11:02:43 +0200868 &dev));
869 ut_assert(dev);
870
871 /* Probe the device */
872 ut_assertok(device_probe(dev));
873
874 /* Test if device is active right now */
875 ut_asserteq(true, device_active(dev));
876
877 /* Remove the device via selective remove flag */
878 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
879
880 /* Test if device is still active right now */
881 ut_asserteq(true, device_active(dev));
882
883 return 0;
884}
885DM_TEST(dm_test_remove_active_dma, 0);
886
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700887/* Test removal of 'vital' devices */
888static int dm_test_remove_vital(struct unit_test_state *uts)
889{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700890 struct udevice *normal, *dma, *vital, *dma_vital;
891
892 /* Skip the behaviour in test_post_probe() */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700893 uts->skip_post_probe = 1;
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700894
Simon Glassb98bfbc2021-03-07 17:34:57 -0700895 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700896 &normal));
897 ut_assertnonnull(normal);
898
Simon Glassb98bfbc2021-03-07 17:34:57 -0700899 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700900 &dma));
901 ut_assertnonnull(dma);
902
Simon Glassb98bfbc2021-03-07 17:34:57 -0700903 ut_assertok(device_bind_by_name(uts->root, false,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700904 &driver_info_vital_clk, &vital));
905 ut_assertnonnull(vital);
906
Simon Glassb98bfbc2021-03-07 17:34:57 -0700907 ut_assertok(device_bind_by_name(uts->root, false,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700908 &driver_info_act_dma_vital_clk,
909 &dma_vital));
910 ut_assertnonnull(dma_vital);
911
912 /* Probe the devices */
913 ut_assertok(device_probe(normal));
914 ut_assertok(device_probe(dma));
915 ut_assertok(device_probe(vital));
916 ut_assertok(device_probe(dma_vital));
917
918 /* Check that devices are active right now */
919 ut_asserteq(true, device_active(normal));
920 ut_asserteq(true, device_active(dma));
921 ut_asserteq(true, device_active(vital));
922 ut_asserteq(true, device_active(dma_vital));
923
924 /* Remove active devices via selective remove flag */
925 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
926
927 /*
928 * Check that this only has an effect on the dma device, since two
929 * devices are vital and the third does not have active DMA
930 */
931 ut_asserteq(true, device_active(normal));
932 ut_asserteq(false, device_active(dma));
933 ut_asserteq(true, device_active(vital));
934 ut_asserteq(true, device_active(dma_vital));
935
936 /* Remove active devices via selective remove flag */
937 ut_assertok(device_probe(dma));
938 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
939
940 /* This should have affected both active-dma devices */
941 ut_asserteq(true, device_active(normal));
942 ut_asserteq(false, device_active(dma));
943 ut_asserteq(true, device_active(vital));
944 ut_asserteq(false, device_active(dma_vital));
945
946 /* Remove non-vital devices */
947 ut_assertok(device_probe(dma));
948 ut_assertok(device_probe(dma_vital));
949 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
950
951 /* This should have affected only non-vital devices */
952 ut_asserteq(false, device_active(normal));
953 ut_asserteq(false, device_active(dma));
954 ut_asserteq(true, device_active(vital));
955 ut_asserteq(true, device_active(dma_vital));
956
957 /* Remove vital devices via normal remove flag */
958 ut_assertok(device_probe(normal));
959 ut_assertok(device_probe(dma));
960 dm_remove_devices_flags(DM_REMOVE_NORMAL);
961
962 /* Check that all devices are inactive right now */
963 ut_asserteq(false, device_active(normal));
964 ut_asserteq(false, device_active(dma));
965 ut_asserteq(false, device_active(vital));
966 ut_asserteq(false, device_active(dma_vital));
967
968 return 0;
969}
970DM_TEST(dm_test_remove_vital, 0);
971
Joe Hershberger3a77be52015-05-20 14:27:27 -0500972static int dm_test_uclass_before_ready(struct unit_test_state *uts)
Simon Glassde708672014-07-23 06:55:15 -0600973{
974 struct uclass *uc;
975
976 ut_assertok(uclass_get(UCLASS_TEST, &uc));
977
Simon Glass51a0eac2015-04-19 07:21:02 -0600978 gd->dm_root = NULL;
979 gd->dm_root_f = NULL;
980 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
981
Simon Glassde708672014-07-23 06:55:15 -0600982 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
983
984 return 0;
985}
Simon Glassde708672014-07-23 06:55:15 -0600986DM_TEST(dm_test_uclass_before_ready, 0);
Simon Glass98fd5d12015-01-25 08:27:04 -0700987
Joe Hershberger3a77be52015-05-20 14:27:27 -0500988static int dm_test_uclass_devices_find(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200989{
990 struct udevice *dev;
991 int ret;
992
993 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
994 dev;
995 ret = uclass_find_next_device(&dev)) {
996 ut_assert(!ret);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200997 ut_assertnonnull(dev);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200998 }
999
Simon Glass0bb44272019-09-25 08:55:55 -06001000 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001001 ut_assertnull(dev);
Marcel Ziswiler75ec16f2019-02-01 16:01:07 +01001002
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001003 return 0;
1004}
Simon Glass974dccd2020-07-28 19:41:12 -06001005DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001006
Joe Hershberger3a77be52015-05-20 14:27:27 -05001007static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001008{
1009 struct udevice *finddev;
1010 struct udevice *testdev;
1011 int findret, ret;
1012
1013 /*
1014 * For each test device found in fdt like: "a-test", "b-test", etc.,
1015 * use its name and try to find it by uclass_find_device_by_name().
1016 * Then, on success check if:
1017 * - current 'testdev' name is equal to the returned 'finddev' name
1018 * - current 'testdev' pointer is equal to the returned 'finddev'
1019 *
1020 * We assume that, each uclass's device name is unique, so if not, then
1021 * this will fail on checking condition: testdev == finddev, since the
1022 * uclass_find_device_by_name(), returns the first device by given name.
1023 */
1024 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1025 testdev;
1026 ret = uclass_find_next_device(&testdev)) {
1027 ut_assertok(ret);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001028 ut_assertnonnull(testdev);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001029
1030 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1031 testdev->name,
1032 &finddev);
1033
1034 ut_assertok(findret);
1035 ut_assert(testdev);
1036 ut_asserteq_str(testdev->name, finddev->name);
1037 ut_asserteq_ptr(testdev, finddev);
1038 }
1039
1040 return 0;
1041}
Simon Glass974dccd2020-07-28 19:41:12 -06001042DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001043
Joe Hershberger3a77be52015-05-20 14:27:27 -05001044static int dm_test_uclass_devices_get(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001045{
1046 struct udevice *dev;
1047 int ret;
1048
1049 for (ret = uclass_first_device(UCLASS_TEST, &dev);
1050 dev;
1051 ret = uclass_next_device(&dev)) {
1052 ut_assert(!ret);
1053 ut_assert(dev);
1054 ut_assert(device_active(dev));
1055 }
1056
1057 return 0;
1058}
Simon Glass974dccd2020-07-28 19:41:12 -06001059DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001060
Joe Hershberger3a77be52015-05-20 14:27:27 -05001061static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001062{
1063 struct udevice *finddev;
1064 struct udevice *testdev;
1065 int ret, findret;
1066
1067 /*
1068 * For each test device found in fdt like: "a-test", "b-test", etc.,
1069 * use its name and try to get it by uclass_get_device_by_name().
1070 * On success check if:
1071 * - returned finddev' is active
1072 * - current 'testdev' name is equal to the returned 'finddev' name
1073 * - current 'testdev' pointer is equal to the returned 'finddev'
1074 *
1075 * We asserts that the 'testdev' is active on each loop entry, so we
1076 * could be sure that the 'finddev' is activated too, but for sure
1077 * we check it again.
1078 *
1079 * We assume that, each uclass's device name is unique, so if not, then
1080 * this will fail on checking condition: testdev == finddev, since the
1081 * uclass_get_device_by_name(), returns the first device by given name.
1082 */
1083 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
1084 testdev;
1085 ret = uclass_next_device(&testdev)) {
1086 ut_assertok(ret);
1087 ut_assert(testdev);
1088 ut_assert(device_active(testdev));
1089
1090 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1091 testdev->name,
1092 &finddev);
1093
1094 ut_assertok(findret);
1095 ut_assert(finddev);
1096 ut_assert(device_active(finddev));
1097 ut_asserteq_str(testdev->name, finddev->name);
1098 ut_asserteq_ptr(testdev, finddev);
1099 }
1100
1101 return 0;
1102}
Simon Glass974dccd2020-07-28 19:41:12 -06001103DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001104
Joe Hershberger3a77be52015-05-20 14:27:27 -05001105static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
Simon Glass98fd5d12015-01-25 08:27:04 -07001106{
1107 struct udevice *dev;
1108
1109 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1110 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1111
1112 return 0;
1113}
Simon Glass974dccd2020-07-28 19:41:12 -06001114DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA);
Simon Glass70e35b42017-12-28 13:14:15 -07001115
1116static int dm_test_uclass_names(struct unit_test_state *uts)
1117{
1118 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1119 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1120
1121 return 0;
1122}
Simon Glass974dccd2020-07-28 19:41:12 -06001123DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA);
Simon Glassb775e872018-10-01 12:22:07 -06001124
1125static int dm_test_inactive_child(struct unit_test_state *uts)
1126{
Simon Glassb775e872018-10-01 12:22:07 -06001127 struct udevice *parent, *dev1, *dev2;
1128
1129 /* Skip the behaviour in test_post_probe() */
Simon Glassb98bfbc2021-03-07 17:34:57 -07001130 uts->skip_post_probe = 1;
Simon Glassb775e872018-10-01 12:22:07 -06001131
1132 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1133
1134 /*
1135 * Create a child but do not activate it. Calling the function again
1136 * should return the same child.
1137 */
1138 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1139 UCLASS_TEST, &dev1));
Simon Glass65130cd2020-12-28 20:34:56 -07001140 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
Simon Glass884870f2020-11-28 17:50:01 -07001141 "test_child", 0, ofnode_null(), &dev1));
Simon Glassb775e872018-10-01 12:22:07 -06001142
1143 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1144 &dev2));
1145 ut_asserteq_ptr(dev1, dev2);
1146
1147 ut_assertok(device_probe(dev1));
1148 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1149 UCLASS_TEST, &dev2));
1150
1151 return 0;
1152}
Simon Glass974dccd2020-07-28 19:41:12 -06001153DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
Simon Glass6a109b32020-12-16 21:20:10 -07001154
1155/* Make sure all bound devices have a sequence number */
1156static int dm_test_all_have_seq(struct unit_test_state *uts)
1157{
1158 struct udevice *dev;
1159 struct uclass *uc;
1160
Simon Glass784cd9e2020-12-19 10:40:17 -07001161 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Simon Glass6a109b32020-12-16 21:20:10 -07001162 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -07001163 if (dev->seq_ == -1)
Simon Glass6a109b32020-12-16 21:20:10 -07001164 printf("Device '%s' has no seq (%d)\n",
Simon Glass5e349922020-12-19 10:40:09 -07001165 dev->name, dev->seq_);
1166 ut_assert(dev->seq_ != -1);
Simon Glass6a109b32020-12-16 21:20:10 -07001167 }
1168 }
1169
1170 return 0;
1171}
1172DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA);
Nicolas Saenz Julienne892e9b42021-01-12 13:55:25 +01001173
1174static int dm_test_dma_offset(struct unit_test_state *uts)
1175{
1176 struct udevice *dev;
1177 ofnode node;
1178
1179 /* Make sure the bus's dma-ranges aren't taken into account here */
1180 node = ofnode_path("/mmio-bus@0");
1181 ut_assert(ofnode_valid(node));
1182 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1183 ut_asserteq_64(0, dev->dma_offset);
1184
1185 /* Device behind a bus with dma-ranges */
1186 node = ofnode_path("/mmio-bus@0/subnode@0");
1187 ut_assert(ofnode_valid(node));
1188 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1189 ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1190
1191 /* This one has no dma-ranges */
1192 node = ofnode_path("/mmio-bus@1");
1193 ut_assert(ofnode_valid(node));
1194 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1195 node = ofnode_path("/mmio-bus@1/subnode@0");
1196 ut_assert(ofnode_valid(node));
1197 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1198 ut_asserteq_64(0, dev->dma_offset);
1199
1200 return 0;
1201}
1202DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);