Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Tests for the core driver model code |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <dm.h> |
| 11 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/root.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 16 | #include <dm/util.h> |
| 17 | #include <dm/test.h> |
| 18 | #include <dm/uclass-internal.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 19 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | enum { |
| 25 | TEST_INTVAL1 = 0, |
| 26 | TEST_INTVAL2 = 3, |
| 27 | TEST_INTVAL3 = 6, |
| 28 | TEST_INTVAL_MANUAL = 101112, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 29 | TEST_INTVAL_PRE_RELOC = 7, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static const struct dm_test_pdata test_pdata[] = { |
| 33 | { .ping_add = TEST_INTVAL1, }, |
| 34 | { .ping_add = TEST_INTVAL2, }, |
| 35 | { .ping_add = TEST_INTVAL3, }, |
| 36 | }; |
| 37 | |
| 38 | static const struct dm_test_pdata test_pdata_manual = { |
| 39 | .ping_add = TEST_INTVAL_MANUAL, |
| 40 | }; |
| 41 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 42 | static const struct dm_test_pdata test_pdata_pre_reloc = { |
| 43 | .ping_add = TEST_INTVAL_PRE_RELOC, |
| 44 | }; |
| 45 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 46 | U_BOOT_DEVICE(dm_test_info1) = { |
| 47 | .name = "test_drv", |
| 48 | .platdata = &test_pdata[0], |
| 49 | }; |
| 50 | |
| 51 | U_BOOT_DEVICE(dm_test_info2) = { |
| 52 | .name = "test_drv", |
| 53 | .platdata = &test_pdata[1], |
| 54 | }; |
| 55 | |
| 56 | U_BOOT_DEVICE(dm_test_info3) = { |
| 57 | .name = "test_drv", |
| 58 | .platdata = &test_pdata[2], |
| 59 | }; |
| 60 | |
| 61 | static struct driver_info driver_info_manual = { |
| 62 | .name = "test_manual_drv", |
| 63 | .platdata = &test_pdata_manual, |
| 64 | }; |
| 65 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 66 | static struct driver_info driver_info_pre_reloc = { |
| 67 | .name = "test_pre_reloc_drv", |
Tom Rini | e75c02d | 2016-03-20 10:10:28 -0400 | [diff] [blame] | 68 | .platdata = &test_pdata_pre_reloc, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 69 | }; |
| 70 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 71 | static struct driver_info driver_info_act_dma = { |
| 72 | .name = "test_act_dma_drv", |
| 73 | }; |
| 74 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 75 | void dm_leak_check_start(struct unit_test_state *uts) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 76 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 77 | uts->start = mallinfo(); |
| 78 | if (!uts->start.uordblks) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 79 | puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n"); |
| 80 | } |
| 81 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 82 | int dm_leak_check_end(struct unit_test_state *uts) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 83 | { |
| 84 | struct mallinfo end; |
Simon Glass | 94d2218 | 2015-09-12 08:45:20 -0600 | [diff] [blame] | 85 | int id, diff; |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 86 | |
| 87 | /* Don't delete the root class, since we started with that */ |
| 88 | for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) { |
| 89 | struct uclass *uc; |
| 90 | |
| 91 | uc = uclass_find(id); |
| 92 | if (!uc) |
| 93 | continue; |
| 94 | ut_assertok(uclass_destroy(uc)); |
| 95 | } |
| 96 | |
| 97 | end = mallinfo(); |
Simon Glass | 94d2218 | 2015-09-12 08:45:20 -0600 | [diff] [blame] | 98 | diff = end.uordblks - uts->start.uordblks; |
| 99 | if (diff > 0) |
| 100 | printf("Leak: lost %#xd bytes\n", diff); |
| 101 | else if (diff < 0) |
| 102 | printf("Leak: gained %#xd bytes\n", -diff); |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 103 | ut_asserteq(uts->start.uordblks, end.uordblks); |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 108 | /* Test that binding with platdata occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 109 | static int dm_test_autobind(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 110 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 111 | struct dm_test_state *dms = uts->priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 112 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * We should have a single class (UCLASS_ROOT) and a single root |
| 116 | * device with no children. |
| 117 | */ |
| 118 | ut_assert(dms->root); |
| 119 | ut_asserteq(1, list_count_items(&gd->uclass_root)); |
| 120 | ut_asserteq(0, list_count_items(&gd->dm_root->child_head)); |
| 121 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); |
| 122 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 123 | ut_assertok(dm_scan_platdata(false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 124 | |
| 125 | /* We should have our test class now at least, plus more children */ |
| 126 | ut_assert(1 < list_count_items(&gd->uclass_root)); |
| 127 | ut_assert(0 < list_count_items(&gd->dm_root->child_head)); |
| 128 | |
| 129 | /* Our 3 dm_test_infox children should be bound to the test uclass */ |
| 130 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); |
| 131 | |
| 132 | /* No devices should be probed */ |
| 133 | list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node) |
| 134 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 135 | |
| 136 | /* Our test driver should have been bound 3 times */ |
| 137 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | DM_TEST(dm_test_autobind, 0); |
| 142 | |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 143 | /* Test that binding with uclass platdata allocation occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 144 | static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts) |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 145 | { |
| 146 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 147 | struct udevice *dev; |
| 148 | struct uclass *uc; |
| 149 | |
| 150 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 151 | ut_assert(uc); |
| 152 | |
| 153 | /** |
| 154 | * Test if test uclass driver requires allocation for the uclass |
| 155 | * platform data and then check the dev->uclass_platdata pointer. |
| 156 | */ |
| 157 | ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size); |
| 158 | |
| 159 | for (uclass_find_first_device(UCLASS_TEST, &dev); |
| 160 | dev; |
| 161 | uclass_find_next_device(&dev)) { |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 162 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 163 | |
| 164 | uc_pdata = dev_get_uclass_platdata(dev); |
| 165 | ut_assert(uc_pdata); |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 170 | DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 171 | |
| 172 | /* Test that binding with uclass platdata setting occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 173 | static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts) |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 174 | { |
| 175 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 176 | struct udevice *dev; |
| 177 | |
| 178 | /** |
| 179 | * In the test_postbind() method of test uclass driver, the uclass |
| 180 | * platform data should be set to three test int values - test it. |
| 181 | */ |
| 182 | for (uclass_find_first_device(UCLASS_TEST, &dev); |
| 183 | dev; |
| 184 | uclass_find_next_device(&dev)) { |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 185 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 186 | |
| 187 | uc_pdata = dev_get_uclass_platdata(dev); |
| 188 | ut_assert(uc_pdata); |
| 189 | ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1); |
| 190 | ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2); |
| 191 | ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 196 | DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 197 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 198 | /* Test that autoprobe finds all the expected devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 199 | static int dm_test_autoprobe(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 200 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 201 | struct dm_test_state *dms = uts->priv; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 202 | int expected_base_add; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 203 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 204 | struct uclass *uc; |
| 205 | int i; |
| 206 | |
| 207 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 208 | ut_assert(uc); |
| 209 | |
| 210 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 211 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 212 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); |
| 213 | |
| 214 | /* The root device should not be activated until needed */ |
Simon Glass | 6d3b3e2 | 2014-07-23 06:55:00 -0600 | [diff] [blame] | 215 | ut_assert(dms->root->flags & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * We should be able to find the three test devices, and they should |
| 219 | * all be activated as they are used (lazy activation, required by |
| 220 | * U-Boot) |
| 221 | */ |
| 222 | for (i = 0; i < 3; i++) { |
| 223 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 224 | ut_assert(dev); |
| 225 | ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED), |
| 226 | "Driver %d/%s already activated", i, dev->name); |
| 227 | |
| 228 | /* This should activate it */ |
| 229 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 230 | ut_assert(dev); |
| 231 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 232 | |
| 233 | /* Activating a device should activate the root device */ |
| 234 | if (!i) |
| 235 | ut_assert(dms->root->flags & DM_FLAG_ACTIVATED); |
| 236 | } |
| 237 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Our 3 dm_test_info children should be passed to pre_probe and |
| 240 | * post_probe |
| 241 | */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 242 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 243 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 244 | |
| 245 | /* Also we can check the per-device data */ |
| 246 | expected_base_add = 0; |
| 247 | for (i = 0; i < 3; i++) { |
| 248 | struct dm_test_uclass_perdev_priv *priv; |
| 249 | struct dm_test_pdata *pdata; |
| 250 | |
| 251 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 252 | ut_assert(dev); |
| 253 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 254 | priv = dev_get_uclass_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 255 | ut_assert(priv); |
| 256 | ut_asserteq(expected_base_add, priv->base_add); |
| 257 | |
| 258 | pdata = dev->platdata; |
| 259 | expected_base_add += pdata->ping_add; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 264 | DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 265 | |
| 266 | /* Check that we see the correct platdata in each device */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 267 | static int dm_test_platdata(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 268 | { |
| 269 | const struct dm_test_pdata *pdata; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 270 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 271 | int i; |
| 272 | |
| 273 | for (i = 0; i < 3; i++) { |
| 274 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 275 | ut_assert(dev); |
| 276 | pdata = dev->platdata; |
| 277 | ut_assert(pdata->ping_add == test_pdata[i].ping_add); |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 282 | DM_TEST(dm_test_platdata, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 283 | |
| 284 | /* Test that we can bind, probe, remove, unbind a driver */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 285 | static int dm_test_lifecycle(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 286 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 287 | struct dm_test_state *dms = uts->priv; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 288 | int op_count[DM_TEST_OP_COUNT]; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 289 | struct udevice *dev, *test_dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 290 | int pingret; |
| 291 | int ret; |
| 292 | |
| 293 | memcpy(op_count, dm_testdrv_op_count, sizeof(op_count)); |
| 294 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 295 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 296 | &dev)); |
| 297 | ut_assert(dev); |
| 298 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] |
| 299 | == op_count[DM_TEST_OP_BIND] + 1); |
| 300 | ut_assert(!dev->priv); |
| 301 | |
| 302 | /* Probe the device - it should fail allocating private data */ |
| 303 | dms->force_fail_alloc = 1; |
| 304 | ret = device_probe(dev); |
| 305 | ut_assert(ret == -ENOMEM); |
| 306 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 307 | == op_count[DM_TEST_OP_PROBE] + 1); |
| 308 | ut_assert(!dev->priv); |
| 309 | |
| 310 | /* Try again without the alloc failure */ |
| 311 | dms->force_fail_alloc = 0; |
| 312 | ut_assertok(device_probe(dev)); |
| 313 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 314 | == op_count[DM_TEST_OP_PROBE] + 2); |
| 315 | ut_assert(dev->priv); |
| 316 | |
| 317 | /* This should be device 3 in the uclass */ |
| 318 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 319 | ut_assert(dev == test_dev); |
| 320 | |
| 321 | /* Try ping */ |
| 322 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 323 | ut_assert(pingret == 102); |
| 324 | |
| 325 | /* Now remove device 3 */ |
| 326 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 327 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 328 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
| 329 | |
| 330 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 331 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 332 | ut_assertok(device_unbind(dev)); |
| 333 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 334 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 335 | |
| 336 | return 0; |
| 337 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 338 | DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 339 | |
| 340 | /* Test that we can bind/unbind and the lists update correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 341 | static int dm_test_ordering(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 342 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 343 | struct dm_test_state *dms = uts->priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 344 | struct udevice *dev, *dev_penultimate, *dev_last, *test_dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 345 | int pingret; |
| 346 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 347 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 348 | &dev)); |
| 349 | ut_assert(dev); |
| 350 | |
| 351 | /* Bind two new devices (numbers 4 and 5) */ |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 352 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 353 | &dev_penultimate)); |
| 354 | ut_assert(dev_penultimate); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 355 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 356 | &dev_last)); |
| 357 | ut_assert(dev_last); |
| 358 | |
| 359 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 360 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 361 | ut_assertok(device_unbind(dev)); |
| 362 | |
| 363 | /* The device numbering should have shifted down one */ |
| 364 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 365 | ut_assert(dev_penultimate == test_dev); |
| 366 | ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev)); |
| 367 | ut_assert(dev_last == test_dev); |
| 368 | |
| 369 | /* Add back the original device 3, now in position 5 */ |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 370 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
| 371 | &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 372 | ut_assert(dev); |
| 373 | |
| 374 | /* Try ping */ |
| 375 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 376 | ut_assert(pingret == 102); |
| 377 | |
| 378 | /* Remove 3 and 4 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 379 | ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 380 | ut_assertok(device_unbind(dev_penultimate)); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 381 | ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 382 | ut_assertok(device_unbind(dev_last)); |
| 383 | |
| 384 | /* Our device should now be in position 3 */ |
| 385 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 386 | ut_assert(dev == test_dev); |
| 387 | |
| 388 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 389 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 390 | ut_assertok(device_unbind(dev)); |
| 391 | |
| 392 | return 0; |
| 393 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 394 | DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 395 | |
| 396 | /* Check that we can perform operations on a device (do a ping) */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 397 | int dm_check_operations(struct unit_test_state *uts, struct udevice *dev, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 398 | uint32_t base, struct dm_test_priv *priv) |
| 399 | { |
| 400 | int expected; |
| 401 | int pingret; |
| 402 | |
| 403 | /* Getting the child device should allocate platdata / priv */ |
| 404 | ut_assertok(testfdt_ping(dev, 10, &pingret)); |
| 405 | ut_assert(dev->priv); |
| 406 | ut_assert(dev->platdata); |
| 407 | |
| 408 | expected = 10 + base; |
| 409 | ut_asserteq(expected, pingret); |
| 410 | |
| 411 | /* Do another ping */ |
| 412 | ut_assertok(testfdt_ping(dev, 20, &pingret)); |
| 413 | expected = 20 + base; |
| 414 | ut_asserteq(expected, pingret); |
| 415 | |
| 416 | /* Now check the ping_total */ |
| 417 | priv = dev->priv; |
| 418 | ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2, |
| 419 | priv->ping_total); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /* Check that we can perform operations on devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 425 | static int dm_test_operations(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 426 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 427 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 428 | int i; |
| 429 | |
| 430 | /* |
| 431 | * Now check that the ping adds are what we expect. This is using the |
| 432 | * ping-add property in each node. |
| 433 | */ |
| 434 | for (i = 0; i < ARRAY_SIZE(test_pdata); i++) { |
| 435 | uint32_t base; |
| 436 | |
| 437 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 438 | |
| 439 | /* |
| 440 | * Get the 'reg' property, which tells us what the ping add |
| 441 | * should be. We don't use the platdata because we want |
| 442 | * to test the code that sets that up (testfdt_drv_probe()). |
| 443 | */ |
| 444 | base = test_pdata[i].ping_add; |
| 445 | debug("dev=%d, base=%d\n", i, base); |
| 446 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 447 | ut_assert(!dm_check_operations(uts, dev, base, dev->priv)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 452 | DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 453 | |
| 454 | /* Remove all drivers and check that things work */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 455 | static int dm_test_remove(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 456 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 457 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 458 | int i; |
| 459 | |
| 460 | for (i = 0; i < 3; i++) { |
| 461 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 462 | ut_assert(dev); |
| 463 | ut_assertf(dev->flags & DM_FLAG_ACTIVATED, |
| 464 | "Driver %d/%s not activated", i, dev->name); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 465 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 466 | ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED), |
| 467 | "Driver %d/%s should have deactivated", i, |
| 468 | dev->name); |
| 469 | ut_assert(!dev->priv); |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 474 | DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 475 | |
| 476 | /* Remove and recreate everything, check for memory leaks */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 477 | static int dm_test_leak(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 478 | { |
| 479 | int i; |
| 480 | |
| 481 | for (i = 0; i < 2; i++) { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 482 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 483 | int ret; |
| 484 | int id; |
| 485 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 486 | dm_leak_check_start(uts); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 487 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 488 | ut_assertok(dm_scan_platdata(false)); |
| 489 | ut_assertok(dm_scan_fdt(gd->fdt_blob, false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 490 | |
| 491 | /* Scanning the uclass is enough to probe all the devices */ |
| 492 | for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) { |
| 493 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 494 | dev; |
| 495 | ret = uclass_next_device(&dev)) |
| 496 | ; |
| 497 | ut_assertok(ret); |
| 498 | } |
| 499 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 500 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | DM_TEST(dm_test_leak, 0); |
| 506 | |
| 507 | /* Test uclass init/destroy methods */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 508 | static int dm_test_uclass(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 509 | { |
| 510 | struct uclass *uc; |
| 511 | |
| 512 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 513 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 514 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
| 515 | ut_assert(uc->priv); |
| 516 | |
| 517 | ut_assertok(uclass_destroy(uc)); |
| 518 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 519 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | DM_TEST(dm_test_uclass, 0); |
| 524 | |
| 525 | /** |
| 526 | * create_children() - Create children of a parent node |
| 527 | * |
| 528 | * @dms: Test system state |
| 529 | * @parent: Parent device |
| 530 | * @count: Number of children to create |
| 531 | * @key: Key value to put in first child. Subsequence children |
| 532 | * receive an incrementing value |
| 533 | * @child: If not NULL, then the child device pointers are written into |
| 534 | * this array. |
| 535 | * @return 0 if OK, -ve on error |
| 536 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 537 | static int create_children(struct unit_test_state *uts, struct udevice *parent, |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 538 | int count, int key, struct udevice *child[]) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 539 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 540 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 541 | int i; |
| 542 | |
| 543 | for (i = 0; i < count; i++) { |
| 544 | struct dm_test_pdata *pdata; |
| 545 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 546 | ut_assertok(device_bind_by_name(parent, false, |
| 547 | &driver_info_manual, &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 548 | pdata = calloc(1, sizeof(*pdata)); |
| 549 | pdata->ping_add = key + i; |
| 550 | dev->platdata = pdata; |
| 551 | if (child) |
| 552 | child[i] = dev; |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | #define NODE_COUNT 10 |
| 559 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 560 | static int dm_test_children(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 561 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 562 | struct dm_test_state *dms = uts->priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 563 | struct udevice *top[NODE_COUNT]; |
| 564 | struct udevice *child[NODE_COUNT]; |
| 565 | struct udevice *grandchild[NODE_COUNT]; |
| 566 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 567 | int total; |
| 568 | int ret; |
| 569 | int i; |
| 570 | |
| 571 | /* We don't care about the numbering for this test */ |
| 572 | dms->skip_post_probe = 1; |
| 573 | |
| 574 | ut_assert(NODE_COUNT > 5); |
| 575 | |
| 576 | /* First create 10 top-level children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 577 | ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 578 | |
| 579 | /* Now a few have their own children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 580 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 581 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 582 | |
| 583 | /* And grandchildren */ |
| 584 | for (i = 0; i < NODE_COUNT; i++) |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 585 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 586 | i == 2 ? grandchild : NULL)); |
| 587 | |
| 588 | /* Check total number of devices */ |
| 589 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 590 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 591 | |
| 592 | /* Try probing one of the grandchildren */ |
| 593 | ut_assertok(uclass_get_device(UCLASS_TEST, |
| 594 | NODE_COUNT * 3 + 2 * NODE_COUNT, &dev)); |
| 595 | ut_asserteq_ptr(grandchild[0], dev); |
| 596 | |
| 597 | /* |
| 598 | * This should have probed the child and top node also, for a total |
| 599 | * of 3 nodes. |
| 600 | */ |
| 601 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 602 | |
| 603 | /* Probe the other grandchildren */ |
| 604 | for (i = 1; i < NODE_COUNT; i++) |
| 605 | ut_assertok(device_probe(grandchild[i])); |
| 606 | |
| 607 | ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 608 | |
| 609 | /* Probe everything */ |
| 610 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 611 | dev; |
| 612 | ret = uclass_next_device(&dev)) |
| 613 | ; |
| 614 | ut_assertok(ret); |
| 615 | |
| 616 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 617 | |
| 618 | /* Remove a top-level child and check that the children are removed */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 619 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 620 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 621 | dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0; |
| 622 | |
| 623 | /* Try one with grandchildren */ |
| 624 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 625 | ut_asserteq_ptr(dev, top[5]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 626 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 627 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 628 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 629 | |
| 630 | /* Try the same with unbind */ |
| 631 | ut_assertok(device_unbind(top[2])); |
| 632 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 633 | dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0; |
| 634 | |
| 635 | /* Try one with grandchildren */ |
| 636 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 637 | ut_asserteq_ptr(dev, top[6]); |
| 638 | ut_assertok(device_unbind(top[5])); |
| 639 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 640 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | DM_TEST(dm_test_children, 0); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 645 | |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 646 | static int dm_test_device_reparent(struct unit_test_state *uts) |
| 647 | { |
| 648 | struct dm_test_state *dms = uts->priv; |
| 649 | struct udevice *top[NODE_COUNT]; |
| 650 | struct udevice *child[NODE_COUNT]; |
| 651 | struct udevice *grandchild[NODE_COUNT]; |
| 652 | struct udevice *dev; |
| 653 | int total; |
| 654 | int ret; |
| 655 | int i; |
| 656 | |
| 657 | /* We don't care about the numbering for this test */ |
| 658 | dms->skip_post_probe = 1; |
| 659 | |
| 660 | ut_assert(NODE_COUNT > 5); |
| 661 | |
| 662 | /* First create 10 top-level children */ |
| 663 | ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top)); |
| 664 | |
| 665 | /* Now a few have their own children */ |
| 666 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 667 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
| 668 | |
| 669 | /* And grandchildren */ |
| 670 | for (i = 0; i < NODE_COUNT; i++) |
| 671 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
| 672 | i == 2 ? grandchild : NULL)); |
| 673 | |
| 674 | /* Check total number of devices */ |
| 675 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 676 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 677 | |
| 678 | /* Probe everything */ |
| 679 | for (i = 0; i < total; i++) |
| 680 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 681 | |
| 682 | /* Re-parent top-level children with no grandchildren. */ |
| 683 | ut_assertok(device_reparent(top[3], top[0])); |
| 684 | /* try to get devices */ |
| 685 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 686 | dev; |
| 687 | ret = uclass_find_next_device(&dev)) { |
| 688 | ut_assert(!ret); |
| 689 | ut_assertnonnull(dev); |
| 690 | } |
| 691 | |
| 692 | ut_assertok(device_reparent(top[4], top[0])); |
| 693 | /* try to get devices */ |
| 694 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 695 | dev; |
| 696 | ret = uclass_find_next_device(&dev)) { |
| 697 | ut_assert(!ret); |
| 698 | ut_assertnonnull(dev); |
| 699 | } |
| 700 | |
| 701 | /* Re-parent top-level children with grandchildren. */ |
| 702 | ut_assertok(device_reparent(top[2], top[0])); |
| 703 | /* try to get devices */ |
| 704 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 705 | dev; |
| 706 | ret = uclass_find_next_device(&dev)) { |
| 707 | ut_assert(!ret); |
| 708 | ut_assertnonnull(dev); |
| 709 | } |
| 710 | |
| 711 | ut_assertok(device_reparent(top[5], top[2])); |
| 712 | /* try to get devices */ |
| 713 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 714 | dev; |
| 715 | ret = uclass_find_next_device(&dev)) { |
| 716 | ut_assert(!ret); |
| 717 | ut_assertnonnull(dev); |
| 718 | } |
| 719 | |
| 720 | /* Re-parent grandchildren. */ |
| 721 | ut_assertok(device_reparent(grandchild[0], top[1])); |
| 722 | /* try to get devices */ |
| 723 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 724 | dev; |
| 725 | ret = uclass_find_next_device(&dev)) { |
| 726 | ut_assert(!ret); |
| 727 | ut_assertnonnull(dev); |
| 728 | } |
| 729 | |
| 730 | ut_assertok(device_reparent(grandchild[1], top[1])); |
| 731 | /* try to get devices */ |
| 732 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 733 | dev; |
| 734 | ret = uclass_find_next_device(&dev)) { |
| 735 | ut_assert(!ret); |
| 736 | ut_assertnonnull(dev); |
| 737 | } |
| 738 | |
| 739 | /* Remove re-pareneted devices. */ |
| 740 | ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL)); |
| 741 | /* try to get devices */ |
| 742 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 743 | dev; |
| 744 | ret = uclass_find_next_device(&dev)) { |
| 745 | ut_assert(!ret); |
| 746 | ut_assertnonnull(dev); |
| 747 | } |
| 748 | |
| 749 | ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL)); |
| 750 | /* try to get devices */ |
| 751 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 752 | dev; |
| 753 | ret = uclass_find_next_device(&dev)) { |
| 754 | ut_assert(!ret); |
| 755 | ut_assertnonnull(dev); |
| 756 | } |
| 757 | |
| 758 | ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL)); |
| 759 | /* try to get devices */ |
| 760 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 761 | dev; |
| 762 | ret = uclass_find_next_device(&dev)) { |
| 763 | ut_assert(!ret); |
| 764 | ut_assertnonnull(dev); |
| 765 | } |
| 766 | |
| 767 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
| 768 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 769 | dev; |
| 770 | ret = uclass_find_next_device(&dev)) { |
| 771 | ut_assert(!ret); |
| 772 | ut_assertnonnull(dev); |
| 773 | } |
| 774 | |
| 775 | ut_assertok(device_remove(grandchild[0], 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(grandchild[1], 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 | /* Try the same with unbind */ |
| 794 | ut_assertok(device_unbind(top[3])); |
| 795 | ut_assertok(device_unbind(top[4])); |
| 796 | ut_assertok(device_unbind(top[5])); |
| 797 | ut_assertok(device_unbind(top[2])); |
| 798 | |
| 799 | ut_assertok(device_unbind(grandchild[0])); |
| 800 | ut_assertok(device_unbind(grandchild[1])); |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | DM_TEST(dm_test_device_reparent, 0); |
| 805 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 806 | /* Test that pre-relocation devices work as expected */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 807 | static int dm_test_pre_reloc(struct unit_test_state *uts) |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 808 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 809 | struct dm_test_state *dms = uts->priv; |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 810 | struct udevice *dev; |
| 811 | |
| 812 | /* The normal driver should refuse to bind before relocation */ |
| 813 | ut_asserteq(-EPERM, device_bind_by_name(dms->root, true, |
| 814 | &driver_info_manual, &dev)); |
| 815 | |
| 816 | /* But this one is marked pre-reloc */ |
| 817 | ut_assertok(device_bind_by_name(dms->root, true, |
| 818 | &driver_info_pre_reloc, &dev)); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | DM_TEST(dm_test_pre_reloc, 0); |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 823 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 824 | /* |
| 825 | * Test that removal of devices, either via the "normal" device_remove() |
| 826 | * API or via the device driver selective flag works as expected |
| 827 | */ |
| 828 | static int dm_test_remove_active_dma(struct unit_test_state *uts) |
| 829 | { |
| 830 | struct dm_test_state *dms = uts->priv; |
| 831 | struct udevice *dev; |
| 832 | |
| 833 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma, |
| 834 | &dev)); |
| 835 | ut_assert(dev); |
| 836 | |
| 837 | /* Probe the device */ |
| 838 | ut_assertok(device_probe(dev)); |
| 839 | |
| 840 | /* Test if device is active right now */ |
| 841 | ut_asserteq(true, device_active(dev)); |
| 842 | |
| 843 | /* Remove the device via selective remove flag */ |
| 844 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 845 | |
| 846 | /* Test if device is inactive right now */ |
| 847 | ut_asserteq(false, device_active(dev)); |
| 848 | |
| 849 | /* Probe the device again */ |
| 850 | ut_assertok(device_probe(dev)); |
| 851 | |
| 852 | /* Test if device is active right now */ |
| 853 | ut_asserteq(true, device_active(dev)); |
| 854 | |
| 855 | /* Remove the device via "normal" remove API */ |
| 856 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
| 857 | |
| 858 | /* Test if device is inactive right now */ |
| 859 | ut_asserteq(false, device_active(dev)); |
| 860 | |
| 861 | /* |
| 862 | * Test if a device without the active DMA flags is not removed upon |
| 863 | * the active DMA remove call |
| 864 | */ |
| 865 | ut_assertok(device_unbind(dev)); |
| 866 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
| 867 | &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 still active right now */ |
| 880 | ut_asserteq(true, device_active(dev)); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | DM_TEST(dm_test_remove_active_dma, 0); |
| 885 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 886 | static int dm_test_uclass_before_ready(struct unit_test_state *uts) |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 887 | { |
| 888 | struct uclass *uc; |
| 889 | |
| 890 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 891 | |
Simon Glass | 51a0eac | 2015-04-19 07:21:02 -0600 | [diff] [blame] | 892 | gd->dm_root = NULL; |
| 893 | gd->dm_root_f = NULL; |
| 894 | memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); |
| 895 | |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 896 | ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); |
| 897 | |
| 898 | return 0; |
| 899 | } |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 900 | DM_TEST(dm_test_uclass_before_ready, 0); |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 901 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 902 | static int dm_test_uclass_devices_find(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 903 | { |
| 904 | struct udevice *dev; |
| 905 | int ret; |
| 906 | |
| 907 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 908 | dev; |
| 909 | ret = uclass_find_next_device(&dev)) { |
| 910 | ut_assert(!ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 911 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 912 | } |
| 913 | |
Simon Glass | 0bb4427 | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 914 | ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 915 | ut_assertnull(dev); |
Marcel Ziswiler | 75ec16f | 2019-02-01 16:01:07 +0100 | [diff] [blame] | 916 | |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 917 | return 0; |
| 918 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 919 | DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 920 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 921 | static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts) |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 922 | { |
| 923 | struct udevice *finddev; |
| 924 | struct udevice *testdev; |
| 925 | int findret, ret; |
| 926 | |
| 927 | /* |
| 928 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 929 | * use its name and try to find it by uclass_find_device_by_name(). |
| 930 | * Then, on success check if: |
| 931 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 932 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 933 | * |
| 934 | * We assume that, each uclass's device name is unique, so if not, then |
| 935 | * this will fail on checking condition: testdev == finddev, since the |
| 936 | * uclass_find_device_by_name(), returns the first device by given name. |
| 937 | */ |
| 938 | for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev); |
| 939 | testdev; |
| 940 | ret = uclass_find_next_device(&testdev)) { |
| 941 | ut_assertok(ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 942 | ut_assertnonnull(testdev); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 943 | |
| 944 | findret = uclass_find_device_by_name(UCLASS_TEST_FDT, |
| 945 | testdev->name, |
| 946 | &finddev); |
| 947 | |
| 948 | ut_assertok(findret); |
| 949 | ut_assert(testdev); |
| 950 | ut_asserteq_str(testdev->name, finddev->name); |
| 951 | ut_asserteq_ptr(testdev, finddev); |
| 952 | } |
| 953 | |
| 954 | return 0; |
| 955 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 956 | DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 957 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 958 | static int dm_test_uclass_devices_get(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 959 | { |
| 960 | struct udevice *dev; |
| 961 | int ret; |
| 962 | |
| 963 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 964 | dev; |
| 965 | ret = uclass_next_device(&dev)) { |
| 966 | ut_assert(!ret); |
| 967 | ut_assert(dev); |
| 968 | ut_assert(device_active(dev)); |
| 969 | } |
| 970 | |
| 971 | return 0; |
| 972 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 973 | DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 974 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 975 | static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts) |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 976 | { |
| 977 | struct udevice *finddev; |
| 978 | struct udevice *testdev; |
| 979 | int ret, findret; |
| 980 | |
| 981 | /* |
| 982 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 983 | * use its name and try to get it by uclass_get_device_by_name(). |
| 984 | * On success check if: |
| 985 | * - returned finddev' is active |
| 986 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 987 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 988 | * |
| 989 | * We asserts that the 'testdev' is active on each loop entry, so we |
| 990 | * could be sure that the 'finddev' is activated too, but for sure |
| 991 | * we check it again. |
| 992 | * |
| 993 | * We assume that, each uclass's device name is unique, so if not, then |
| 994 | * this will fail on checking condition: testdev == finddev, since the |
| 995 | * uclass_get_device_by_name(), returns the first device by given name. |
| 996 | */ |
| 997 | for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev); |
| 998 | testdev; |
| 999 | ret = uclass_next_device(&testdev)) { |
| 1000 | ut_assertok(ret); |
| 1001 | ut_assert(testdev); |
| 1002 | ut_assert(device_active(testdev)); |
| 1003 | |
| 1004 | findret = uclass_get_device_by_name(UCLASS_TEST_FDT, |
| 1005 | testdev->name, |
| 1006 | &finddev); |
| 1007 | |
| 1008 | ut_assertok(findret); |
| 1009 | ut_assert(finddev); |
| 1010 | ut_assert(device_active(finddev)); |
| 1011 | ut_asserteq_str(testdev->name, finddev->name); |
| 1012 | ut_asserteq_ptr(testdev, finddev); |
| 1013 | } |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1017 | DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1018 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1019 | static int dm_test_device_get_uclass_id(struct unit_test_state *uts) |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 1020 | { |
| 1021 | struct udevice *dev; |
| 1022 | |
| 1023 | ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev)); |
| 1024 | ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev)); |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1028 | DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA); |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 1029 | |
| 1030 | static int dm_test_uclass_names(struct unit_test_state *uts) |
| 1031 | { |
| 1032 | ut_asserteq_str("test", uclass_get_name(UCLASS_TEST)); |
| 1033 | ut_asserteq(UCLASS_TEST, uclass_get_by_name("test")); |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1037 | DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1038 | |
| 1039 | static int dm_test_inactive_child(struct unit_test_state *uts) |
| 1040 | { |
| 1041 | struct dm_test_state *dms = uts->priv; |
| 1042 | struct udevice *parent, *dev1, *dev2; |
| 1043 | |
| 1044 | /* Skip the behaviour in test_post_probe() */ |
| 1045 | dms->skip_post_probe = 1; |
| 1046 | |
| 1047 | ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent)); |
| 1048 | |
| 1049 | /* |
| 1050 | * Create a child but do not activate it. Calling the function again |
| 1051 | * should return the same child. |
| 1052 | */ |
| 1053 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1054 | UCLASS_TEST, &dev1)); |
| 1055 | ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv), |
| 1056 | "test_child", 0, ofnode_null(), &dev1)); |
| 1057 | |
| 1058 | ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST, |
| 1059 | &dev2)); |
| 1060 | ut_asserteq_ptr(dev1, dev2); |
| 1061 | |
| 1062 | ut_assertok(device_probe(dev1)); |
| 1063 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1064 | UCLASS_TEST, &dev2)); |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1068 | DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA); |