blob: d40916ef58857d6e72c776c102675bc8c54ef02e [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
Simon Glassb2c1cac2014-02-26 15:59:21 -07008#include <errno.h>
9#include <dm.h>
10#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070012#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070014#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>
Sughosh Ganu6012c1e2024-07-30 16:41:32 +053019#include <linux/list.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);
Sughosh Ganu6012c1e2024-07-30 16:41:32 +0530127 ut_asserteq(1, list_count_nodes(gd->uclass_root));
128 ut_asserteq(0, list_count_nodes(&gd->dm_root->child_head));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700129 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 */
Sughosh Ganu6012c1e2024-07-30 16:41:32 +0530134 ut_assert(1 < list_count_nodes(gd->uclass_root));
135 ut_assert(0 < list_count_nodes(&gd->dm_root->child_head));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700136
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 Glass1a92f832024-08-22 07:57:48 -0600178DM_TEST(dm_test_autobind_uclass_pdata_alloc, UTF_SCAN_PDATA);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200179
Kishon Vijay Abraham Iae93e8a2021-07-21 21:28:31 +0530180/* compare node names ignoring the unit address */
181static int dm_test_compare_node_name(struct unit_test_state *uts)
182{
183 ofnode node;
184
185 node = ofnode_path("/mmio-bus@0");
186 ut_assert(ofnode_valid(node));
187 ut_assert(ofnode_name_eq(node, "mmio-bus"));
188
Simon Glass6dc1eb32025-01-10 17:00:27 -0700189 ut_assert(!ofnode_name_eq(node, "mmio-bus@0"));
190
Kishon Vijay Abraham Iae93e8a2021-07-21 21:28:31 +0530191 return 0;
192}
Simon Glass1a92f832024-08-22 07:57:48 -0600193DM_TEST(dm_test_compare_node_name, UTF_SCAN_PDATA);
Kishon Vijay Abraham Iae93e8a2021-07-21 21:28:31 +0530194
Simon Glass71fa5b42020-12-03 16:55:18 -0700195/* Test that binding with uclass plat setting occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500196static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200197{
198 struct dm_test_perdev_uc_pdata *uc_pdata;
199 struct udevice *dev;
200
201 /**
202 * In the test_postbind() method of test uclass driver, the uclass
203 * platform data should be set to three test int values - test it.
204 */
205 for (uclass_find_first_device(UCLASS_TEST, &dev);
206 dev;
207 uclass_find_next_device(&dev)) {
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200208 ut_assertnonnull(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200209
Simon Glass71fa5b42020-12-03 16:55:18 -0700210 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200211 ut_assert(uc_pdata);
212 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
213 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
214 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
215 }
216
217 return 0;
218}
Simon Glass1a92f832024-08-22 07:57:48 -0600219DM_TEST(dm_test_autobind_uclass_pdata_valid, UTF_SCAN_PDATA);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200220
Simon Glassb2c1cac2014-02-26 15:59:21 -0700221/* Test that autoprobe finds all the expected devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500222static int dm_test_autoprobe(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700223{
224 int expected_base_add;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200225 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700226 struct uclass *uc;
227 int i;
228
229 ut_assertok(uclass_get(UCLASS_TEST, &uc));
230 ut_assert(uc);
231
232 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700233 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700234 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
235
236 /* The root device should not be activated until needed */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700237 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700238
239 /*
240 * We should be able to find the three test devices, and they should
241 * all be activated as they are used (lazy activation, required by
242 * U-Boot)
243 */
244 for (i = 0; i < 3; i++) {
245 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
246 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700247 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700248 "Driver %d/%s already activated", i, dev->name);
249
250 /* This should activate it */
251 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
252 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700253 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700254
255 /* Activating a device should activate the root device */
256 if (!i)
Simon Glassb98bfbc2021-03-07 17:34:57 -0700257 ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700258 }
259
Simon Glass9c1f3822015-03-05 12:25:22 -0700260 /*
261 * Our 3 dm_test_info children should be passed to pre_probe and
262 * post_probe
263 */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700264 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700265 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700266
267 /* Also we can check the per-device data */
268 expected_base_add = 0;
269 for (i = 0; i < 3; i++) {
270 struct dm_test_uclass_perdev_priv *priv;
271 struct dm_test_pdata *pdata;
272
273 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
274 ut_assert(dev);
275
Simon Glassde0977b2015-03-05 12:25:20 -0700276 priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700277 ut_assert(priv);
278 ut_asserteq(expected_base_add, priv->base_add);
279
Simon Glass95588622020-12-22 19:30:28 -0700280 pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700281 expected_base_add += pdata->ping_add;
282 }
283
284 return 0;
285}
Simon Glass1a92f832024-08-22 07:57:48 -0600286DM_TEST(dm_test_autoprobe, UTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700287
Simon Glass71fa5b42020-12-03 16:55:18 -0700288/* Check that we see the correct plat in each device */
Simon Glassb75b15b2020-12-03 16:55:23 -0700289static int dm_test_plat(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700290{
291 const struct dm_test_pdata *pdata;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200292 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700293 int i;
294
295 for (i = 0; i < 3; i++) {
296 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
297 ut_assert(dev);
Simon Glass95588622020-12-22 19:30:28 -0700298 pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700299 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
300 }
301
302 return 0;
303}
Simon Glass1a92f832024-08-22 07:57:48 -0600304DM_TEST(dm_test_plat, UTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700305
306/* Test that we can bind, probe, remove, unbind a driver */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500307static int dm_test_lifecycle(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700308{
309 int op_count[DM_TEST_OP_COUNT];
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200310 struct udevice *dev, *test_dev;
Simon Glass51608c92021-12-16 20:59:32 -0700311 int start_dev_count, start_uc_count;
312 int dev_count, uc_count;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700313 int pingret;
314 int ret;
315
316 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
317
Simon Glass51608c92021-12-16 20:59:32 -0700318 dm_get_stats(&start_dev_count, &start_uc_count);
319
Simon Glassb98bfbc2021-03-07 17:34:57 -0700320 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700321 &dev));
322 ut_assert(dev);
323 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
324 == op_count[DM_TEST_OP_BIND] + 1);
Simon Glass95588622020-12-22 19:30:28 -0700325 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700326
Simon Glass51608c92021-12-16 20:59:32 -0700327 /* We should have one more device */
328 dm_get_stats(&dev_count, &uc_count);
329 ut_asserteq(start_dev_count + 1, dev_count);
330 ut_asserteq(start_uc_count, uc_count);
331
Simon Glassb2c1cac2014-02-26 15:59:21 -0700332 /* Probe the device - it should fail allocating private data */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700333 uts->force_fail_alloc = 1;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700334 ret = device_probe(dev);
335 ut_assert(ret == -ENOMEM);
336 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
337 == op_count[DM_TEST_OP_PROBE] + 1);
Simon Glass95588622020-12-22 19:30:28 -0700338 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700339
340 /* Try again without the alloc failure */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700341 uts->force_fail_alloc = 0;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700342 ut_assertok(device_probe(dev));
343 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
344 == op_count[DM_TEST_OP_PROBE] + 2);
Simon Glass95588622020-12-22 19:30:28 -0700345 ut_assert(dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700346
347 /* This should be device 3 in the uclass */
348 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
349 ut_assert(dev == test_dev);
350
351 /* Try ping */
352 ut_assertok(test_ping(dev, 100, &pingret));
353 ut_assert(pingret == 102);
354
355 /* Now remove device 3 */
356 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100357 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700358 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
359
360 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
361 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
362 ut_assertok(device_unbind(dev));
363 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
364 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
365
Simon Glass51608c92021-12-16 20:59:32 -0700366 /* We should have one less device */
367 dm_get_stats(&dev_count, &uc_count);
368 ut_asserteq(start_dev_count, dev_count);
369 ut_asserteq(start_uc_count, uc_count);
370
Simon Glassb2c1cac2014-02-26 15:59:21 -0700371 return 0;
372}
Simon Glass1a92f832024-08-22 07:57:48 -0600373DM_TEST(dm_test_lifecycle, UTF_SCAN_PDATA | UTF_PROBE_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700374
375/* Test that we can bind/unbind and the lists update correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500376static int dm_test_ordering(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700377{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200378 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700379 int pingret;
380
Simon Glassb98bfbc2021-03-07 17:34:57 -0700381 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700382 &dev));
383 ut_assert(dev);
384
385 /* Bind two new devices (numbers 4 and 5) */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700386 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700387 &dev_penultimate));
388 ut_assert(dev_penultimate);
Simon Glassb98bfbc2021-03-07 17:34:57 -0700389 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700390 &dev_last));
391 ut_assert(dev_last);
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 /* The device numbering should have shifted down one */
398 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
399 ut_assert(dev_penultimate == test_dev);
400 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
401 ut_assert(dev_last == test_dev);
402
403 /* Add back the original device 3, now in position 5 */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700404 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Simon Glassfef72b72014-07-23 06:55:03 -0600405 &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700406 ut_assert(dev);
407
408 /* Try ping */
409 ut_assertok(test_ping(dev, 100, &pingret));
410 ut_assert(pingret == 102);
411
412 /* Remove 3 and 4 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100413 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700414 ut_assertok(device_unbind(dev_penultimate));
Stefan Roese80b5bc92017-03-20 12:51:48 +0100415 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700416 ut_assertok(device_unbind(dev_last));
417
418 /* Our device should now be in position 3 */
419 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
420 ut_assert(dev == test_dev);
421
422 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100423 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700424 ut_assertok(device_unbind(dev));
425
426 return 0;
427}
Simon Glass1a92f832024-08-22 07:57:48 -0600428DM_TEST(dm_test_ordering, UTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700429
430/* Check that we can perform operations on a device (do a ping) */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500431int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700432 uint32_t base, struct dm_test_priv *priv)
433{
434 int expected;
435 int pingret;
436
Simon Glass71fa5b42020-12-03 16:55:18 -0700437 /* Getting the child device should allocate plat / priv */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700438 ut_assertok(testfdt_ping(dev, 10, &pingret));
Simon Glass95588622020-12-22 19:30:28 -0700439 ut_assert(dev_get_priv(dev));
440 ut_assert(dev_get_plat(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700441
442 expected = 10 + base;
443 ut_asserteq(expected, pingret);
444
445 /* Do another ping */
446 ut_assertok(testfdt_ping(dev, 20, &pingret));
447 expected = 20 + base;
448 ut_asserteq(expected, pingret);
449
450 /* Now check the ping_total */
Simon Glass95588622020-12-22 19:30:28 -0700451 priv = dev_get_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700452 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
453 priv->ping_total);
454
455 return 0;
456}
457
458/* Check that we can perform operations on devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500459static int dm_test_operations(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700460{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200461 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700462 int i;
463
464 /*
465 * Now check that the ping adds are what we expect. This is using the
466 * ping-add property in each node.
467 */
468 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
469 uint32_t base;
470
471 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
472
473 /*
474 * Get the 'reg' property, which tells us what the ping add
Simon Glass71fa5b42020-12-03 16:55:18 -0700475 * should be. We don't use the plat because we want
Simon Glassb2c1cac2014-02-26 15:59:21 -0700476 * to test the code that sets that up (testfdt_drv_probe()).
477 */
478 base = test_pdata[i].ping_add;
479 debug("dev=%d, base=%d\n", i, base);
480
Simon Glass95588622020-12-22 19:30:28 -0700481 ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev)));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700482 }
483
484 return 0;
485}
Simon Glass1a92f832024-08-22 07:57:48 -0600486DM_TEST(dm_test_operations, UTF_SCAN_PDATA);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700487
488/* Remove all drivers and check that things work */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500489static int dm_test_remove(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700490{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200491 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700492 int i;
493
494 for (i = 0; i < 3; i++) {
495 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
496 ut_assert(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700497 ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700498 "Driver %d/%s not activated", i, dev->name);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100499 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glass6211d762020-12-19 10:40:10 -0700500 ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700501 "Driver %d/%s should have deactivated", i,
502 dev->name);
Simon Glass95588622020-12-22 19:30:28 -0700503 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700504 }
505
506 return 0;
507}
Simon Glass1a92f832024-08-22 07:57:48 -0600508DM_TEST(dm_test_remove, UTF_SCAN_PDATA | UTF_PROBE_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700509
510/* Remove and recreate everything, check for memory leaks */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500511static int dm_test_leak(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700512{
513 int i;
514
515 for (i = 0; i < 2; i++) {
Simon Glassb2c1cac2014-02-26 15:59:21 -0700516 int ret;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700517
Joe Hershberger3a77be52015-05-20 14:27:27 -0500518 dm_leak_check_start(uts);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700519
Simon Glassb75b15b2020-12-03 16:55:23 -0700520 ut_assertok(dm_scan_plat(false));
Simon Glass5039cab2020-11-28 17:50:09 -0700521 ut_assertok(dm_scan_fdt(false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700522
Michal Suchanek44322b52022-10-12 21:57:51 +0200523 ret = uclass_probe_all(UCLASS_TEST);
524 ut_assertok(ret);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700525
Joe Hershberger3a77be52015-05-20 14:27:27 -0500526 ut_assertok(dm_leak_check_end(uts));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700527 }
528
529 return 0;
530}
531DM_TEST(dm_test_leak, 0);
532
533/* Test uclass init/destroy methods */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500534static int dm_test_uclass(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700535{
Simon Glass51608c92021-12-16 20:59:32 -0700536 int dev_count, uc_count;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700537 struct uclass *uc;
538
Simon Glass51608c92021-12-16 20:59:32 -0700539 /* We should have just the root device and uclass */
540 dm_get_stats(&dev_count, &uc_count);
541 ut_asserteq(1, dev_count);
542 ut_asserteq(1, uc_count);
543
Simon Glassb2c1cac2014-02-26 15:59:21 -0700544 ut_assertok(uclass_get(UCLASS_TEST, &uc));
545 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
546 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
Simon Glass95588622020-12-22 19:30:28 -0700547 ut_assert(uclass_get_priv(uc));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700548
Simon Glass51608c92021-12-16 20:59:32 -0700549 dm_get_stats(&dev_count, &uc_count);
550 ut_asserteq(1, dev_count);
551 ut_asserteq(2, uc_count);
552
Simon Glassb2c1cac2014-02-26 15:59:21 -0700553 ut_assertok(uclass_destroy(uc));
554 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
555 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
556
Simon Glass51608c92021-12-16 20:59:32 -0700557 dm_get_stats(&dev_count, &uc_count);
558 ut_asserteq(1, dev_count);
559 ut_asserteq(1, uc_count);
560
Simon Glassb2c1cac2014-02-26 15:59:21 -0700561 return 0;
562}
563DM_TEST(dm_test_uclass, 0);
564
565/**
566 * create_children() - Create children of a parent node
567 *
568 * @dms: Test system state
569 * @parent: Parent device
570 * @count: Number of children to create
571 * @key: Key value to put in first child. Subsequence children
572 * receive an incrementing value
573 * @child: If not NULL, then the child device pointers are written into
574 * this array.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100575 * Return: 0 if OK, -ve on error
Simon Glassb2c1cac2014-02-26 15:59:21 -0700576 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500577static int create_children(struct unit_test_state *uts, struct udevice *parent,
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200578 int count, int key, struct udevice *child[])
Simon Glassb2c1cac2014-02-26 15:59:21 -0700579{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200580 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700581 int i;
582
583 for (i = 0; i < count; i++) {
584 struct dm_test_pdata *pdata;
585
Simon Glassfef72b72014-07-23 06:55:03 -0600586 ut_assertok(device_bind_by_name(parent, false,
587 &driver_info_manual, &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700588 pdata = calloc(1, sizeof(*pdata));
589 pdata->ping_add = key + i;
Simon Glass95588622020-12-22 19:30:28 -0700590 dev_set_plat(dev, pdata);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700591 if (child)
592 child[i] = dev;
593 }
594
595 return 0;
596}
597
598#define NODE_COUNT 10
599
Joe Hershberger3a77be52015-05-20 14:27:27 -0500600static int dm_test_children(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700601{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200602 struct udevice *top[NODE_COUNT];
603 struct udevice *child[NODE_COUNT];
604 struct udevice *grandchild[NODE_COUNT];
605 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700606 int total;
607 int ret;
608 int i;
609
610 /* We don't care about the numbering for this test */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700611 uts->skip_post_probe = 1;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700612
613 ut_assert(NODE_COUNT > 5);
614
615 /* First create 10 top-level children */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700616 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700617
618 /* Now a few have their own children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500619 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
620 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700621
622 /* And grandchildren */
623 for (i = 0; i < NODE_COUNT; i++)
Joe Hershberger3a77be52015-05-20 14:27:27 -0500624 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700625 i == 2 ? grandchild : NULL));
626
627 /* Check total number of devices */
628 total = NODE_COUNT * (3 + NODE_COUNT);
629 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
630
631 /* Try probing one of the grandchildren */
632 ut_assertok(uclass_get_device(UCLASS_TEST,
633 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
634 ut_asserteq_ptr(grandchild[0], dev);
635
636 /*
637 * This should have probed the child and top node also, for a total
638 * of 3 nodes.
639 */
640 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
641
642 /* Probe the other grandchildren */
643 for (i = 1; i < NODE_COUNT; i++)
644 ut_assertok(device_probe(grandchild[i]));
645
646 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
647
648 /* Probe everything */
Michal Suchanek44322b52022-10-12 21:57:51 +0200649 ret = uclass_probe_all(UCLASS_TEST);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700650 ut_assertok(ret);
651
652 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
653
654 /* Remove a top-level child and check that the children are removed */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100655 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700656 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
657 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
658
659 /* Try one with grandchildren */
660 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
661 ut_asserteq_ptr(dev, top[5]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100662 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700663 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
664 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
665
666 /* Try the same with unbind */
667 ut_assertok(device_unbind(top[2]));
668 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
669 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
670
671 /* Try one with grandchildren */
672 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
673 ut_asserteq_ptr(dev, top[6]);
674 ut_assertok(device_unbind(top[5]));
675 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
676 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
677
678 return 0;
679}
680DM_TEST(dm_test_children, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600681
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300682static int dm_test_device_reparent(struct unit_test_state *uts)
683{
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300684 struct udevice *top[NODE_COUNT];
685 struct udevice *child[NODE_COUNT];
686 struct udevice *grandchild[NODE_COUNT];
687 struct udevice *dev;
688 int total;
689 int ret;
690 int i;
691
692 /* We don't care about the numbering for this test */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700693 uts->skip_post_probe = 1;
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300694
695 ut_assert(NODE_COUNT > 5);
696
697 /* First create 10 top-level children */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700698 ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top));
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300699
700 /* Now a few have their own children */
701 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
702 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
703
704 /* And grandchildren */
705 for (i = 0; i < NODE_COUNT; i++)
706 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
707 i == 2 ? grandchild : NULL));
708
709 /* Check total number of devices */
710 total = NODE_COUNT * (3 + NODE_COUNT);
711 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
712
713 /* Probe everything */
714 for (i = 0; i < total; i++)
715 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
716
717 /* Re-parent top-level children with no grandchildren. */
718 ut_assertok(device_reparent(top[3], top[0]));
719 /* try to get devices */
720 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
721 dev;
722 ret = uclass_find_next_device(&dev)) {
723 ut_assert(!ret);
724 ut_assertnonnull(dev);
725 }
726
727 ut_assertok(device_reparent(top[4], top[0]));
728 /* try to get devices */
729 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
730 dev;
731 ret = uclass_find_next_device(&dev)) {
732 ut_assert(!ret);
733 ut_assertnonnull(dev);
734 }
735
736 /* Re-parent top-level children with grandchildren. */
737 ut_assertok(device_reparent(top[2], top[0]));
738 /* try to get devices */
739 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
740 dev;
741 ret = uclass_find_next_device(&dev)) {
742 ut_assert(!ret);
743 ut_assertnonnull(dev);
744 }
745
746 ut_assertok(device_reparent(top[5], top[2]));
747 /* try to get devices */
748 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
749 dev;
750 ret = uclass_find_next_device(&dev)) {
751 ut_assert(!ret);
752 ut_assertnonnull(dev);
753 }
754
755 /* Re-parent grandchildren. */
756 ut_assertok(device_reparent(grandchild[0], top[1]));
757 /* try to get devices */
758 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
759 dev;
760 ret = uclass_find_next_device(&dev)) {
761 ut_assert(!ret);
762 ut_assertnonnull(dev);
763 }
764
765 ut_assertok(device_reparent(grandchild[1], top[1]));
766 /* try to get devices */
767 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
768 dev;
769 ret = uclass_find_next_device(&dev)) {
770 ut_assert(!ret);
771 ut_assertnonnull(dev);
772 }
773
774 /* Remove re-pareneted devices. */
775 ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL));
776 /* try to get devices */
777 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
778 dev;
779 ret = uclass_find_next_device(&dev)) {
780 ut_assert(!ret);
781 ut_assertnonnull(dev);
782 }
783
784 ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL));
785 /* try to get devices */
786 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
787 dev;
788 ret = uclass_find_next_device(&dev)) {
789 ut_assert(!ret);
790 ut_assertnonnull(dev);
791 }
792
793 ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL));
794 /* try to get devices */
795 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
796 dev;
797 ret = uclass_find_next_device(&dev)) {
798 ut_assert(!ret);
799 ut_assertnonnull(dev);
800 }
801
802 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
803 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
804 dev;
805 ret = uclass_find_next_device(&dev)) {
806 ut_assert(!ret);
807 ut_assertnonnull(dev);
808 }
809
810 ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL));
811 /* try to get devices */
812 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
813 dev;
814 ret = uclass_find_next_device(&dev)) {
815 ut_assert(!ret);
816 ut_assertnonnull(dev);
817 }
818
819 ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL));
820 /* try to get devices */
821 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
822 dev;
823 ret = uclass_find_next_device(&dev)) {
824 ut_assert(!ret);
825 ut_assertnonnull(dev);
826 }
827
828 /* Try the same with unbind */
829 ut_assertok(device_unbind(top[3]));
830 ut_assertok(device_unbind(top[4]));
831 ut_assertok(device_unbind(top[5]));
832 ut_assertok(device_unbind(top[2]));
833
834 ut_assertok(device_unbind(grandchild[0]));
835 ut_assertok(device_unbind(grandchild[1]));
836
837 return 0;
838}
839DM_TEST(dm_test_device_reparent, 0);
840
Simon Glassfef72b72014-07-23 06:55:03 -0600841/* Test that pre-relocation devices work as expected */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500842static int dm_test_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600843{
844 struct udevice *dev;
845
846 /* The normal driver should refuse to bind before relocation */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700847 ut_asserteq(-EPERM, device_bind_by_name(uts->root, true,
Simon Glassfef72b72014-07-23 06:55:03 -0600848 &driver_info_manual, &dev));
849
850 /* But this one is marked pre-reloc */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700851 ut_assertok(device_bind_by_name(uts->root, true,
Simon Glassfef72b72014-07-23 06:55:03 -0600852 &driver_info_pre_reloc, &dev));
853
854 return 0;
855}
856DM_TEST(dm_test_pre_reloc, 0);
Simon Glassde708672014-07-23 06:55:15 -0600857
Stefan Roeseeaffda72017-03-27 11:02:43 +0200858/*
859 * Test that removal of devices, either via the "normal" device_remove()
860 * API or via the device driver selective flag works as expected
861 */
862static int dm_test_remove_active_dma(struct unit_test_state *uts)
863{
Stefan Roeseeaffda72017-03-27 11:02:43 +0200864 struct udevice *dev;
865
Simon Glassb98bfbc2021-03-07 17:34:57 -0700866 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
Stefan Roeseeaffda72017-03-27 11:02:43 +0200867 &dev));
868 ut_assert(dev);
869
870 /* Probe the device */
871 ut_assertok(device_probe(dev));
872
873 /* Test if device is active right now */
874 ut_asserteq(true, device_active(dev));
875
876 /* Remove the device via selective remove flag */
877 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
878
879 /* Test if device is inactive right now */
880 ut_asserteq(false, device_active(dev));
881
882 /* Probe the device again */
883 ut_assertok(device_probe(dev));
884
885 /* Test if device is active right now */
886 ut_asserteq(true, device_active(dev));
887
888 /* Remove the device via "normal" remove API */
889 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
890
891 /* Test if device is inactive right now */
892 ut_asserteq(false, device_active(dev));
893
894 /*
895 * Test if a device without the active DMA flags is not removed upon
896 * the active DMA remove call
897 */
898 ut_assertok(device_unbind(dev));
Simon Glassb98bfbc2021-03-07 17:34:57 -0700899 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Stefan Roeseeaffda72017-03-27 11:02:43 +0200900 &dev));
901 ut_assert(dev);
902
903 /* Probe the device */
904 ut_assertok(device_probe(dev));
905
906 /* Test if device is active right now */
907 ut_asserteq(true, device_active(dev));
908
909 /* Remove the device via selective remove flag */
910 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
911
912 /* Test if device is still active right now */
913 ut_asserteq(true, device_active(dev));
914
915 return 0;
916}
917DM_TEST(dm_test_remove_active_dma, 0);
918
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700919/* Test removal of 'vital' devices */
920static int dm_test_remove_vital(struct unit_test_state *uts)
921{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700922 struct udevice *normal, *dma, *vital, *dma_vital;
923
924 /* Skip the behaviour in test_post_probe() */
Simon Glassb98bfbc2021-03-07 17:34:57 -0700925 uts->skip_post_probe = 1;
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700926
Simon Glassb98bfbc2021-03-07 17:34:57 -0700927 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700928 &normal));
929 ut_assertnonnull(normal);
930
Simon Glassb98bfbc2021-03-07 17:34:57 -0700931 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700932 &dma));
933 ut_assertnonnull(dma);
934
Simon Glassb98bfbc2021-03-07 17:34:57 -0700935 ut_assertok(device_bind_by_name(uts->root, false,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700936 &driver_info_vital_clk, &vital));
937 ut_assertnonnull(vital);
938
Simon Glassb98bfbc2021-03-07 17:34:57 -0700939 ut_assertok(device_bind_by_name(uts->root, false,
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700940 &driver_info_act_dma_vital_clk,
941 &dma_vital));
942 ut_assertnonnull(dma_vital);
943
944 /* Probe the devices */
945 ut_assertok(device_probe(normal));
946 ut_assertok(device_probe(dma));
947 ut_assertok(device_probe(vital));
948 ut_assertok(device_probe(dma_vital));
949
950 /* Check that devices are active right now */
951 ut_asserteq(true, device_active(normal));
952 ut_asserteq(true, device_active(dma));
953 ut_asserteq(true, device_active(vital));
954 ut_asserteq(true, device_active(dma_vital));
955
956 /* Remove active devices via selective remove flag */
957 dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL);
958
959 /*
960 * Check that this only has an effect on the dma device, since two
961 * devices are vital and the third does not have active DMA
962 */
963 ut_asserteq(true, device_active(normal));
964 ut_asserteq(false, device_active(dma));
965 ut_asserteq(true, device_active(vital));
966 ut_asserteq(true, device_active(dma_vital));
967
968 /* Remove active devices via selective remove flag */
969 ut_assertok(device_probe(dma));
970 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
971
972 /* This should have affected both active-dma devices */
973 ut_asserteq(true, device_active(normal));
974 ut_asserteq(false, device_active(dma));
975 ut_asserteq(true, device_active(vital));
976 ut_asserteq(false, device_active(dma_vital));
977
978 /* Remove non-vital devices */
979 ut_assertok(device_probe(dma));
980 ut_assertok(device_probe(dma_vital));
981 dm_remove_devices_flags(DM_REMOVE_NON_VITAL);
982
983 /* This should have affected only non-vital devices */
984 ut_asserteq(false, device_active(normal));
985 ut_asserteq(false, device_active(dma));
986 ut_asserteq(true, device_active(vital));
987 ut_asserteq(true, device_active(dma_vital));
988
989 /* Remove vital devices via normal remove flag */
990 ut_assertok(device_probe(normal));
991 ut_assertok(device_probe(dma));
992 dm_remove_devices_flags(DM_REMOVE_NORMAL);
993
994 /* Check that all devices are inactive right now */
995 ut_asserteq(false, device_active(normal));
996 ut_asserteq(false, device_active(dma));
997 ut_asserteq(false, device_active(vital));
998 ut_asserteq(false, device_active(dma_vital));
999
1000 return 0;
1001}
1002DM_TEST(dm_test_remove_vital, 0);
1003
Janne Grunau64609732024-11-23 22:44:05 +01001004/* Test removal of 'active' devices */
1005static int dm_test_remove_active(struct unit_test_state *uts)
1006{
1007 struct udevice *normal, *dma, *vital, *dma_vital;
1008
1009 /* Skip the behaviour in test_post_probe() */
1010 uts->skip_post_probe = 1;
1011
1012 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
1013 &normal));
1014 ut_assertnonnull(normal);
1015
1016 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma,
1017 &dma));
1018 ut_assertnonnull(dma);
1019
1020 ut_assertok(device_bind_by_name(uts->root, false,
1021 &driver_info_vital_clk, &vital));
1022 ut_assertnonnull(vital);
1023
1024 ut_assertok(device_bind_by_name(uts->root, false,
1025 &driver_info_act_dma_vital_clk,
1026 &dma_vital));
1027 ut_assertnonnull(dma_vital);
1028
1029 /* Probe the devices */
1030 ut_assertok(device_probe(normal));
1031 ut_assertok(device_probe(dma));
1032 ut_assertok(device_probe(vital));
1033 ut_assertok(device_probe(dma_vital));
1034
1035 /* Check that devices are active right now */
1036 ut_asserteq(true, device_active(normal));
1037 ut_asserteq(true, device_active(dma));
1038 ut_asserteq(true, device_active(vital));
1039 ut_asserteq(true, device_active(dma_vital));
1040
1041 /* Remove active devices in an ordered way */
1042 dm_remove_devices_active();
1043
1044 /* Check that all devices are inactive right now */
1045 ut_asserteq(true, device_active(normal));
1046 ut_asserteq(false, device_active(dma));
1047 ut_asserteq(true, device_active(vital));
1048 ut_asserteq(false, device_active(dma_vital));
1049
1050 return 0;
1051}
1052DM_TEST(dm_test_remove_active, 0);
1053
Joe Hershberger3a77be52015-05-20 14:27:27 -05001054static int dm_test_uclass_before_ready(struct unit_test_state *uts)
Simon Glassde708672014-07-23 06:55:15 -06001055{
1056 struct uclass *uc;
1057
1058 ut_assertok(uclass_get(UCLASS_TEST, &uc));
1059
Simon Glass51a0eac2015-04-19 07:21:02 -06001060 gd->dm_root = NULL;
Simon Glass51a0eac2015-04-19 07:21:02 -06001061 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
1062
Simon Glassde708672014-07-23 06:55:15 -06001063 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
Simon Glass8e9eacf2021-08-01 12:05:23 -06001064 ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc));
Simon Glassde708672014-07-23 06:55:15 -06001065
1066 return 0;
1067}
Simon Glassde708672014-07-23 06:55:15 -06001068DM_TEST(dm_test_uclass_before_ready, 0);
Simon Glass98fd5d12015-01-25 08:27:04 -07001069
Joe Hershberger3a77be52015-05-20 14:27:27 -05001070static int dm_test_uclass_devices_find(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001071{
1072 struct udevice *dev;
1073 int ret;
1074
1075 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
1076 dev;
1077 ret = uclass_find_next_device(&dev)) {
1078 ut_assert(!ret);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001079 ut_assertnonnull(dev);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001080 }
1081
Simon Glass0bb44272019-09-25 08:55:55 -06001082 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001083 ut_assertnull(dev);
Marcel Ziswiler75ec16f2019-02-01 16:01:07 +01001084
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001085 return 0;
1086}
Simon Glass1a92f832024-08-22 07:57:48 -06001087DM_TEST(dm_test_uclass_devices_find, UTF_SCAN_PDATA);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001088
Joe Hershberger3a77be52015-05-20 14:27:27 -05001089static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001090{
1091 struct udevice *finddev;
1092 struct udevice *testdev;
1093 int findret, ret;
1094
1095 /*
1096 * For each test device found in fdt like: "a-test", "b-test", etc.,
1097 * use its name and try to find it by uclass_find_device_by_name().
1098 * Then, on success check if:
1099 * - current 'testdev' name is equal to the returned 'finddev' name
1100 * - current 'testdev' pointer is equal to the returned 'finddev'
1101 *
1102 * We assume that, each uclass's device name is unique, so if not, then
1103 * this will fail on checking condition: testdev == finddev, since the
1104 * uclass_find_device_by_name(), returns the first device by given name.
1105 */
1106 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
1107 testdev;
1108 ret = uclass_find_next_device(&testdev)) {
1109 ut_assertok(ret);
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +02001110 ut_assertnonnull(testdev);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001111
1112 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
1113 testdev->name,
1114 &finddev);
1115
1116 ut_assertok(findret);
1117 ut_assert(testdev);
1118 ut_asserteq_str(testdev->name, finddev->name);
1119 ut_asserteq_ptr(testdev, finddev);
1120 }
1121
1122 return 0;
1123}
Simon Glass1a92f832024-08-22 07:57:48 -06001124DM_TEST(dm_test_uclass_devices_find_by_name, UTF_SCAN_FDT);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001125
Joe Hershberger3a77be52015-05-20 14:27:27 -05001126static int dm_test_uclass_devices_get(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001127{
1128 struct udevice *dev;
1129 int ret;
1130
Michal Suchanek98e1ada2022-10-12 21:58:09 +02001131 for (ret = uclass_first_device_check(UCLASS_TEST, &dev);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001132 dev;
Michal Suchanek98e1ada2022-10-12 21:58:09 +02001133 ret = uclass_next_device_check(&dev)) {
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001134 ut_assert(!ret);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001135 ut_assert(device_active(dev));
1136 }
1137
1138 return 0;
1139}
Simon Glass1a92f832024-08-22 07:57:48 -06001140DM_TEST(dm_test_uclass_devices_get, UTF_SCAN_PDATA);
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +02001141
Joe Hershberger3a77be52015-05-20 14:27:27 -05001142static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001143{
1144 struct udevice *finddev;
1145 struct udevice *testdev;
1146 int ret, findret;
1147
1148 /*
1149 * For each test device found in fdt like: "a-test", "b-test", etc.,
1150 * use its name and try to get it by uclass_get_device_by_name().
1151 * On success check if:
1152 * - returned finddev' is active
1153 * - current 'testdev' name is equal to the returned 'finddev' name
1154 * - current 'testdev' pointer is equal to the returned 'finddev'
1155 *
1156 * We asserts that the 'testdev' is active on each loop entry, so we
1157 * could be sure that the 'finddev' is activated too, but for sure
1158 * we check it again.
1159 *
1160 * We assume that, each uclass's device name is unique, so if not, then
1161 * this will fail on checking condition: testdev == finddev, since the
1162 * uclass_get_device_by_name(), returns the first device by given name.
1163 */
Michal Suchanek98e1ada2022-10-12 21:58:09 +02001164 for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001165 testdev;
Michal Suchanek98e1ada2022-10-12 21:58:09 +02001166 ret = uclass_next_device_check(&testdev)) {
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001167 ut_assertok(ret);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001168 ut_assert(device_active(testdev));
1169
1170 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
1171 testdev->name,
1172 &finddev);
1173
1174 ut_assertok(findret);
1175 ut_assert(finddev);
1176 ut_assert(device_active(finddev));
1177 ut_asserteq_str(testdev->name, finddev->name);
1178 ut_asserteq_ptr(testdev, finddev);
1179 }
1180
1181 return 0;
1182}
Simon Glass1a92f832024-08-22 07:57:48 -06001183DM_TEST(dm_test_uclass_devices_get_by_name, UTF_SCAN_FDT);
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +02001184
Joe Hershberger3a77be52015-05-20 14:27:27 -05001185static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
Simon Glass98fd5d12015-01-25 08:27:04 -07001186{
1187 struct udevice *dev;
1188
1189 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
1190 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1191
1192 return 0;
1193}
Simon Glass1a92f832024-08-22 07:57:48 -06001194DM_TEST(dm_test_device_get_uclass_id, UTF_SCAN_PDATA);
Simon Glass70e35b42017-12-28 13:14:15 -07001195
1196static int dm_test_uclass_names(struct unit_test_state *uts)
1197{
1198 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
1199 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
1200
Simon Glassf1f519f2022-04-24 23:30:59 -06001201 ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi"));
1202
Simon Glass70e35b42017-12-28 13:14:15 -07001203 return 0;
1204}
Simon Glass1a92f832024-08-22 07:57:48 -06001205DM_TEST(dm_test_uclass_names, UTF_SCAN_PDATA);
Simon Glassb775e872018-10-01 12:22:07 -06001206
1207static int dm_test_inactive_child(struct unit_test_state *uts)
1208{
Simon Glassb775e872018-10-01 12:22:07 -06001209 struct udevice *parent, *dev1, *dev2;
1210
1211 /* Skip the behaviour in test_post_probe() */
Simon Glassb98bfbc2021-03-07 17:34:57 -07001212 uts->skip_post_probe = 1;
Simon Glassb775e872018-10-01 12:22:07 -06001213
1214 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
1215
1216 /*
1217 * Create a child but do not activate it. Calling the function again
1218 * should return the same child.
1219 */
1220 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1221 UCLASS_TEST, &dev1));
Simon Glass65130cd2020-12-28 20:34:56 -07001222 ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv),
Simon Glass884870f2020-11-28 17:50:01 -07001223 "test_child", 0, ofnode_null(), &dev1));
Simon Glassb775e872018-10-01 12:22:07 -06001224
1225 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
1226 &dev2));
1227 ut_asserteq_ptr(dev1, dev2);
1228
1229 ut_assertok(device_probe(dev1));
1230 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
1231 UCLASS_TEST, &dev2));
1232
1233 return 0;
1234}
Simon Glass1a92f832024-08-22 07:57:48 -06001235DM_TEST(dm_test_inactive_child, UTF_SCAN_PDATA);
Simon Glass6a109b32020-12-16 21:20:10 -07001236
1237/* Make sure all bound devices have a sequence number */
1238static int dm_test_all_have_seq(struct unit_test_state *uts)
1239{
1240 struct udevice *dev;
1241 struct uclass *uc;
1242
Simon Glass784cd9e2020-12-19 10:40:17 -07001243 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Simon Glass6a109b32020-12-16 21:20:10 -07001244 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -07001245 if (dev->seq_ == -1)
Simon Glass6a109b32020-12-16 21:20:10 -07001246 printf("Device '%s' has no seq (%d)\n",
Simon Glass5e349922020-12-19 10:40:09 -07001247 dev->name, dev->seq_);
1248 ut_assert(dev->seq_ != -1);
Simon Glass6a109b32020-12-16 21:20:10 -07001249 }
1250 }
1251
1252 return 0;
1253}
Simon Glass1a92f832024-08-22 07:57:48 -06001254DM_TEST(dm_test_all_have_seq, UTF_SCAN_PDATA);
Nicolas Saenz Julienne892e9b42021-01-12 13:55:25 +01001255
Simon Glass26974252021-07-05 16:32:42 -06001256#if CONFIG_IS_ENABLED(DM_DMA)
Nicolas Saenz Julienne892e9b42021-01-12 13:55:25 +01001257static int dm_test_dma_offset(struct unit_test_state *uts)
1258{
1259 struct udevice *dev;
1260 ofnode node;
1261
1262 /* Make sure the bus's dma-ranges aren't taken into account here */
1263 node = ofnode_path("/mmio-bus@0");
1264 ut_assert(ofnode_valid(node));
1265 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1266 ut_asserteq_64(0, dev->dma_offset);
1267
1268 /* Device behind a bus with dma-ranges */
1269 node = ofnode_path("/mmio-bus@0/subnode@0");
1270 ut_assert(ofnode_valid(node));
1271 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1272 ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
1273
1274 /* This one has no dma-ranges */
1275 node = ofnode_path("/mmio-bus@1");
1276 ut_assert(ofnode_valid(node));
1277 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
1278 node = ofnode_path("/mmio-bus@1/subnode@0");
1279 ut_assert(ofnode_valid(node));
1280 ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
1281 ut_asserteq_64(0, dev->dma_offset);
1282
1283 return 0;
1284}
Simon Glass1a92f832024-08-22 07:57:48 -06001285DM_TEST(dm_test_dma_offset, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass26974252021-07-05 16:32:42 -06001286#endif
Simon Glass51608c92021-12-16 20:59:32 -07001287
1288/* Test dm_get_stats() */
1289static int dm_test_get_stats(struct unit_test_state *uts)
1290{
1291 int dev_count, uc_count;
1292
1293 dm_get_stats(&dev_count, &uc_count);
1294 ut_assert(dev_count > 50);
1295 ut_assert(uc_count > 30);
1296
1297 return 0;
1298}
Simon Glass1a92f832024-08-22 07:57:48 -06001299DM_TEST(dm_test_get_stats, UTF_SCAN_FDT);
Simon Glass9670f7d2022-04-24 23:31:00 -06001300
1301/* Test uclass_find_device_by_name() */
1302static int dm_test_uclass_find_device(struct unit_test_state *uts)
1303{
1304 struct udevice *dev;
1305
1306 ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
1307 ut_asserteq(-ENODEV,
1308 uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
1309 ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
1310 &dev));
1311
1312 return 0;
1313}
Simon Glass1a92f832024-08-22 07:57:48 -06001314DM_TEST(dm_test_uclass_find_device, UTF_SCAN_FDT);
Simon Glassd1f12cf2022-05-08 04:39:24 -06001315
1316/* Test getting information about tags attached to devices */
1317static int dm_test_dev_get_attach(struct unit_test_state *uts)
1318{
1319 struct udevice *dev;
1320
1321 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1322 ut_asserteq_str("a-test", dev->name);
1323
1324 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1325 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1326 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1327 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1328 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1329 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1330
1331 ut_asserteq(sizeof(struct dm_test_pdata),
1332 dev_get_attach_size(dev, DM_TAG_PLAT));
1333 ut_asserteq(sizeof(struct dm_test_priv),
1334 dev_get_attach_size(dev, DM_TAG_PRIV));
1335 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1336 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1337 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1338 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1339
1340 return 0;
1341}
Simon Glass1a92f832024-08-22 07:57:48 -06001342DM_TEST(dm_test_dev_get_attach, UTF_SCAN_FDT);
Simon Glassd1f12cf2022-05-08 04:39:24 -06001343
1344/* Test getting information about tags attached to bus devices */
1345static int dm_test_dev_get_attach_bus(struct unit_test_state *uts)
1346{
1347 struct udevice *dev, *child;
1348
1349 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev));
1350 ut_asserteq_str("some-bus", dev->name);
1351
1352 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT));
1353 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV));
1354 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV));
1355 ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT));
1356 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT));
1357 ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV));
1358
1359 ut_asserteq(sizeof(struct dm_test_pdata),
1360 dev_get_attach_size(dev, DM_TAG_PLAT));
1361 ut_asserteq(sizeof(struct dm_test_priv),
1362 dev_get_attach_size(dev, DM_TAG_PRIV));
1363 ut_asserteq(sizeof(struct dm_test_uclass_priv),
1364 dev_get_attach_size(dev, DM_TAG_UC_PRIV));
1365 ut_asserteq(sizeof(struct dm_test_uclass_plat),
1366 dev_get_attach_size(dev, DM_TAG_UC_PLAT));
1367 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT));
1368 ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV));
1369
1370 /* Now try the child of the bus */
1371 ut_assertok(device_first_child_err(dev, &child));
1372 ut_asserteq_str("c-test@5", child->name);
1373
1374 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT));
1375 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV));
1376 ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV));
1377 ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT));
1378 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT));
1379 ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV));
1380
1381 ut_asserteq(sizeof(struct dm_test_pdata),
1382 dev_get_attach_size(child, DM_TAG_PLAT));
1383 ut_asserteq(sizeof(struct dm_test_priv),
1384 dev_get_attach_size(child, DM_TAG_PRIV));
1385 ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV));
1386 ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT));
1387 ut_asserteq(sizeof(struct dm_test_parent_plat),
1388 dev_get_attach_size(child, DM_TAG_PARENT_PLAT));
1389 ut_asserteq(sizeof(struct dm_test_parent_data),
1390 dev_get_attach_size(child, DM_TAG_PARENT_PRIV));
1391
1392 return 0;
1393}
Simon Glass1a92f832024-08-22 07:57:48 -06001394DM_TEST(dm_test_dev_get_attach_bus, UTF_SCAN_FDT);
Simon Glassbe1621b2022-05-08 04:39:25 -06001395
1396/* Test getting information about tags attached to bus devices */
1397static int dm_test_dev_get_mem(struct unit_test_state *uts)
1398{
1399 struct dm_stats stats;
1400
1401 dm_get_mem(&stats);
1402
1403 return 0;
1404}
Simon Glass1a92f832024-08-22 07:57:48 -06001405DM_TEST(dm_test_dev_get_mem, UTF_SCAN_FDT);
Simon Glassf66139f2024-10-19 09:21:49 -06001406
1407/* Test uclass_try_first_device() */
1408static int dm_test_try_first_device(struct unit_test_state *uts)
1409{
1410 struct udevice *dev;
1411
1412 /* Check that it doesn't create a device or uclass */
1413 ut_assertnull(uclass_find(UCLASS_TEST));
1414 ut_assertnull(uclass_try_first_device(UCLASS_TEST));
1415 ut_assertnull(uclass_try_first_device(UCLASS_TEST));
1416 ut_assertnull(uclass_find(UCLASS_TEST));
1417
1418 /* Create a test device */
1419 ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
1420 &dev));
1421 dev = uclass_try_first_device(UCLASS_TEST);
1422 ut_assertnonnull(dev);
1423 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
1424
1425 return 0;
1426}
1427DM_TEST(dm_test_try_first_device, 0);