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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/root.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 17 | #include <dm/util.h> |
| 18 | #include <dm/test.h> |
| 19 | #include <dm/uclass-internal.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 20 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 21 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
| 25 | enum { |
| 26 | TEST_INTVAL1 = 0, |
| 27 | TEST_INTVAL2 = 3, |
| 28 | TEST_INTVAL3 = 6, |
| 29 | TEST_INTVAL_MANUAL = 101112, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 30 | TEST_INTVAL_PRE_RELOC = 7, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static const struct dm_test_pdata test_pdata[] = { |
| 34 | { .ping_add = TEST_INTVAL1, }, |
| 35 | { .ping_add = TEST_INTVAL2, }, |
| 36 | { .ping_add = TEST_INTVAL3, }, |
| 37 | }; |
| 38 | |
| 39 | static const struct dm_test_pdata test_pdata_manual = { |
| 40 | .ping_add = TEST_INTVAL_MANUAL, |
| 41 | }; |
| 42 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 43 | static const struct dm_test_pdata test_pdata_pre_reloc = { |
| 44 | .ping_add = TEST_INTVAL_PRE_RELOC, |
| 45 | }; |
| 46 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 47 | U_BOOT_DRVINFO(dm_test_info1) = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 48 | .name = "test_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 49 | .plat = &test_pdata[0], |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 52 | U_BOOT_DRVINFO(dm_test_info2) = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 53 | .name = "test_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 54 | .plat = &test_pdata[1], |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 57 | U_BOOT_DRVINFO(dm_test_info3) = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 58 | .name = "test_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 59 | .plat = &test_pdata[2], |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static struct driver_info driver_info_manual = { |
| 63 | .name = "test_manual_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 64 | .plat = &test_pdata_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 67 | static struct driver_info driver_info_pre_reloc = { |
| 68 | .name = "test_pre_reloc_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 69 | .plat = &test_pdata_pre_reloc, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 70 | }; |
| 71 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 72 | static struct driver_info driver_info_act_dma = { |
| 73 | .name = "test_act_dma_drv", |
| 74 | }; |
| 75 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 76 | static struct driver_info driver_info_vital_clk = { |
| 77 | .name = "test_vital_clk_drv", |
| 78 | }; |
| 79 | |
| 80 | static struct driver_info driver_info_act_dma_vital_clk = { |
| 81 | .name = "test_act_dma_vital_clk_drv", |
| 82 | }; |
| 83 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 84 | void dm_leak_check_start(struct unit_test_state *uts) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 85 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 86 | uts->start = mallinfo(); |
| 87 | if (!uts->start.uordblks) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 88 | puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n"); |
| 89 | } |
| 90 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 91 | int dm_leak_check_end(struct unit_test_state *uts) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 92 | { |
| 93 | struct mallinfo end; |
Simon Glass | 94d2218 | 2015-09-12 08:45:20 -0600 | [diff] [blame] | 94 | int id, diff; |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 95 | |
| 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 Glass | 94d2218 | 2015-09-12 08:45:20 -0600 | [diff] [blame] | 107 | 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 Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 112 | ut_asserteq(uts->start.uordblks, end.uordblks); |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 117 | /* Test that binding with plat occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 118 | static int dm_test_autobind(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 119 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 120 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * We should have a single class (UCLASS_ROOT) and a single root |
| 124 | * device with no children. |
| 125 | */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 126 | ut_assert(uts->root); |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 127 | ut_asserteq(1, list_count_items(gd->uclass_root)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 128 | ut_asserteq(0, list_count_items(&gd->dm_root->child_head)); |
| 129 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); |
| 130 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 131 | ut_assertok(dm_scan_plat(false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 132 | |
| 133 | /* We should have our test class now at least, plus more children */ |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 134 | ut_assert(1 < list_count_items(gd->uclass_root)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 135 | ut_assert(0 < list_count_items(&gd->dm_root->child_head)); |
| 136 | |
| 137 | /* Our 3 dm_test_infox children should be bound to the test uclass */ |
| 138 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); |
| 139 | |
| 140 | /* No devices should be probed */ |
| 141 | list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node) |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 142 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 143 | |
| 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 | } |
| 149 | DM_TEST(dm_test_autobind, 0); |
| 150 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 151 | /* Test that binding with uclass plat allocation occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 152 | 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] | 153 | { |
| 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 Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 163 | * platform data and then check the dev->uclass_plat pointer. |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 164 | */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 165 | ut_assert(uc->uc_drv->per_device_plat_auto); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 166 | |
| 167 | for (uclass_find_first_device(UCLASS_TEST, &dev); |
| 168 | dev; |
| 169 | uclass_find_next_device(&dev)) { |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 170 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 171 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 172 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 173 | ut_assert(uc_pdata); |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 178 | DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 179 | |
Kishon Vijay Abraham I | ae93e8a | 2021-07-21 21:28:31 +0530 | [diff] [blame] | 180 | /* compare node names ignoring the unit address */ |
| 181 | static 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 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | DM_TEST(dm_test_compare_node_name, UT_TESTF_SCAN_PDATA); |
| 193 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 194 | /* Test that binding with uclass plat setting occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 195 | 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] | 196 | { |
| 197 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 198 | struct udevice *dev; |
| 199 | |
| 200 | /** |
| 201 | * In the test_postbind() method of test uclass driver, the uclass |
| 202 | * platform data should be set to three test int values - test it. |
| 203 | */ |
| 204 | for (uclass_find_first_device(UCLASS_TEST, &dev); |
| 205 | dev; |
| 206 | uclass_find_next_device(&dev)) { |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 207 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 208 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 209 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 210 | ut_assert(uc_pdata); |
| 211 | ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1); |
| 212 | ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2); |
| 213 | ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3); |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 218 | DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 219 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 220 | /* Test that autoprobe finds all the expected devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 221 | static int dm_test_autoprobe(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 222 | { |
| 223 | int expected_base_add; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 224 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 225 | struct uclass *uc; |
| 226 | int i; |
| 227 | |
| 228 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 229 | ut_assert(uc); |
| 230 | |
| 231 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 232 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 233 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); |
| 234 | |
| 235 | /* The root device should not be activated until needed */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 236 | ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * We should be able to find the three test devices, and they should |
| 240 | * all be activated as they are used (lazy activation, required by |
| 241 | * U-Boot) |
| 242 | */ |
| 243 | for (i = 0; i < 3; i++) { |
| 244 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 245 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 246 | ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 247 | "Driver %d/%s already activated", i, dev->name); |
| 248 | |
| 249 | /* This should activate it */ |
| 250 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 251 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 252 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 253 | |
| 254 | /* Activating a device should activate the root device */ |
| 255 | if (!i) |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 256 | ut_assert(dev_get_flags(uts->root) & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 259 | /* |
| 260 | * Our 3 dm_test_info children should be passed to pre_probe and |
| 261 | * post_probe |
| 262 | */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 263 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 264 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 265 | |
| 266 | /* Also we can check the per-device data */ |
| 267 | expected_base_add = 0; |
| 268 | for (i = 0; i < 3; i++) { |
| 269 | struct dm_test_uclass_perdev_priv *priv; |
| 270 | struct dm_test_pdata *pdata; |
| 271 | |
| 272 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 273 | ut_assert(dev); |
| 274 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 275 | priv = dev_get_uclass_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 276 | ut_assert(priv); |
| 277 | ut_asserteq(expected_base_add, priv->base_add); |
| 278 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 279 | pdata = dev_get_plat(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 280 | expected_base_add += pdata->ping_add; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 285 | DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 286 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 287 | /* Check that we see the correct plat in each device */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 288 | static int dm_test_plat(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 289 | { |
| 290 | const struct dm_test_pdata *pdata; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 291 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 292 | int i; |
| 293 | |
| 294 | for (i = 0; i < 3; i++) { |
| 295 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 296 | ut_assert(dev); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 297 | pdata = dev_get_plat(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 298 | ut_assert(pdata->ping_add == test_pdata[i].ping_add); |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 303 | DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 304 | |
| 305 | /* Test that we can bind, probe, remove, unbind a driver */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 306 | static int dm_test_lifecycle(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 307 | { |
| 308 | int op_count[DM_TEST_OP_COUNT]; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 309 | struct udevice *dev, *test_dev; |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 310 | int start_dev_count, start_uc_count; |
| 311 | int dev_count, uc_count; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 312 | int pingret; |
| 313 | int ret; |
| 314 | |
| 315 | memcpy(op_count, dm_testdrv_op_count, sizeof(op_count)); |
| 316 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 317 | dm_get_stats(&start_dev_count, &start_uc_count); |
| 318 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 319 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 320 | &dev)); |
| 321 | ut_assert(dev); |
| 322 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] |
| 323 | == op_count[DM_TEST_OP_BIND] + 1); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 324 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 325 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 326 | /* We should have one more device */ |
| 327 | dm_get_stats(&dev_count, &uc_count); |
| 328 | ut_asserteq(start_dev_count + 1, dev_count); |
| 329 | ut_asserteq(start_uc_count, uc_count); |
| 330 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 331 | /* Probe the device - it should fail allocating private data */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 332 | uts->force_fail_alloc = 1; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 333 | ret = device_probe(dev); |
| 334 | ut_assert(ret == -ENOMEM); |
| 335 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 336 | == op_count[DM_TEST_OP_PROBE] + 1); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 337 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 338 | |
| 339 | /* Try again without the alloc failure */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 340 | uts->force_fail_alloc = 0; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 341 | ut_assertok(device_probe(dev)); |
| 342 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 343 | == op_count[DM_TEST_OP_PROBE] + 2); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 344 | ut_assert(dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 345 | |
| 346 | /* This should be device 3 in the uclass */ |
| 347 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 348 | ut_assert(dev == test_dev); |
| 349 | |
| 350 | /* Try ping */ |
| 351 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 352 | ut_assert(pingret == 102); |
| 353 | |
| 354 | /* Now remove device 3 */ |
| 355 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 356 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 357 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
| 358 | |
| 359 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 360 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 361 | ut_assertok(device_unbind(dev)); |
| 362 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 363 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 364 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 365 | /* We should have one less device */ |
| 366 | dm_get_stats(&dev_count, &uc_count); |
| 367 | ut_asserteq(start_dev_count, dev_count); |
| 368 | ut_asserteq(start_uc_count, uc_count); |
| 369 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 370 | return 0; |
| 371 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 372 | 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] | 373 | |
| 374 | /* Test that we can bind/unbind and the lists update correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 375 | static int dm_test_ordering(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 376 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 377 | struct udevice *dev, *dev_penultimate, *dev_last, *test_dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 378 | int pingret; |
| 379 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 380 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 381 | &dev)); |
| 382 | ut_assert(dev); |
| 383 | |
| 384 | /* Bind two new devices (numbers 4 and 5) */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 385 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 386 | &dev_penultimate)); |
| 387 | ut_assert(dev_penultimate); |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 388 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 389 | &dev_last)); |
| 390 | ut_assert(dev_last); |
| 391 | |
| 392 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 393 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 394 | ut_assertok(device_unbind(dev)); |
| 395 | |
| 396 | /* The device numbering should have shifted down one */ |
| 397 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 398 | ut_assert(dev_penultimate == test_dev); |
| 399 | ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev)); |
| 400 | ut_assert(dev_last == test_dev); |
| 401 | |
| 402 | /* Add back the original device 3, now in position 5 */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 403 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 404 | &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 405 | ut_assert(dev); |
| 406 | |
| 407 | /* Try ping */ |
| 408 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 409 | ut_assert(pingret == 102); |
| 410 | |
| 411 | /* Remove 3 and 4 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 412 | ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 413 | ut_assertok(device_unbind(dev_penultimate)); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 414 | ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 415 | ut_assertok(device_unbind(dev_last)); |
| 416 | |
| 417 | /* Our device should now be in position 3 */ |
| 418 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 419 | ut_assert(dev == test_dev); |
| 420 | |
| 421 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 422 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 423 | ut_assertok(device_unbind(dev)); |
| 424 | |
| 425 | return 0; |
| 426 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 427 | DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 428 | |
| 429 | /* Check that we can perform operations on a device (do a ping) */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 430 | int dm_check_operations(struct unit_test_state *uts, struct udevice *dev, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 431 | uint32_t base, struct dm_test_priv *priv) |
| 432 | { |
| 433 | int expected; |
| 434 | int pingret; |
| 435 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 436 | /* Getting the child device should allocate plat / priv */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 437 | ut_assertok(testfdt_ping(dev, 10, &pingret)); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 438 | ut_assert(dev_get_priv(dev)); |
| 439 | ut_assert(dev_get_plat(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 440 | |
| 441 | expected = 10 + base; |
| 442 | ut_asserteq(expected, pingret); |
| 443 | |
| 444 | /* Do another ping */ |
| 445 | ut_assertok(testfdt_ping(dev, 20, &pingret)); |
| 446 | expected = 20 + base; |
| 447 | ut_asserteq(expected, pingret); |
| 448 | |
| 449 | /* Now check the ping_total */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 450 | priv = dev_get_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 451 | ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2, |
| 452 | priv->ping_total); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* Check that we can perform operations on devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 458 | static int dm_test_operations(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 459 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 460 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 461 | int i; |
| 462 | |
| 463 | /* |
| 464 | * Now check that the ping adds are what we expect. This is using the |
| 465 | * ping-add property in each node. |
| 466 | */ |
| 467 | for (i = 0; i < ARRAY_SIZE(test_pdata); i++) { |
| 468 | uint32_t base; |
| 469 | |
| 470 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 471 | |
| 472 | /* |
| 473 | * Get the 'reg' property, which tells us what the ping add |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 474 | * should be. We don't use the plat because we want |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 475 | * to test the code that sets that up (testfdt_drv_probe()). |
| 476 | */ |
| 477 | base = test_pdata[i].ping_add; |
| 478 | debug("dev=%d, base=%d\n", i, base); |
| 479 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 480 | ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev))); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 485 | DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 486 | |
| 487 | /* Remove all drivers and check that things work */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 488 | static int dm_test_remove(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 489 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 490 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 491 | int i; |
| 492 | |
| 493 | for (i = 0; i < 3; i++) { |
| 494 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 495 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 496 | ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 497 | "Driver %d/%s not activated", i, dev->name); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 498 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 499 | ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 500 | "Driver %d/%s should have deactivated", i, |
| 501 | dev->name); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 502 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 507 | 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] | 508 | |
| 509 | /* Remove and recreate everything, check for memory leaks */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 510 | static int dm_test_leak(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 511 | { |
| 512 | int i; |
| 513 | |
| 514 | for (i = 0; i < 2; i++) { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 515 | int ret; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 516 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 517 | dm_leak_check_start(uts); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 518 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 519 | ut_assertok(dm_scan_plat(false)); |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 520 | ut_assertok(dm_scan_fdt(false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 521 | |
Michal Suchanek | 44322b5 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 522 | ret = uclass_probe_all(UCLASS_TEST); |
| 523 | ut_assertok(ret); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 524 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 525 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | DM_TEST(dm_test_leak, 0); |
| 531 | |
| 532 | /* Test uclass init/destroy methods */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 533 | static int dm_test_uclass(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 534 | { |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 535 | int dev_count, uc_count; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 536 | struct uclass *uc; |
| 537 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 538 | /* We should have just the root device and uclass */ |
| 539 | dm_get_stats(&dev_count, &uc_count); |
| 540 | ut_asserteq(1, dev_count); |
| 541 | ut_asserteq(1, uc_count); |
| 542 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 543 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 544 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 545 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 546 | ut_assert(uclass_get_priv(uc)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 547 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 548 | dm_get_stats(&dev_count, &uc_count); |
| 549 | ut_asserteq(1, dev_count); |
| 550 | ut_asserteq(2, uc_count); |
| 551 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 552 | ut_assertok(uclass_destroy(uc)); |
| 553 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 554 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
| 555 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 556 | dm_get_stats(&dev_count, &uc_count); |
| 557 | ut_asserteq(1, dev_count); |
| 558 | ut_asserteq(1, uc_count); |
| 559 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | DM_TEST(dm_test_uclass, 0); |
| 563 | |
| 564 | /** |
| 565 | * create_children() - Create children of a parent node |
| 566 | * |
| 567 | * @dms: Test system state |
| 568 | * @parent: Parent device |
| 569 | * @count: Number of children to create |
| 570 | * @key: Key value to put in first child. Subsequence children |
| 571 | * receive an incrementing value |
| 572 | * @child: If not NULL, then the child device pointers are written into |
| 573 | * this array. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 574 | * Return: 0 if OK, -ve on error |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 575 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 576 | static int create_children(struct unit_test_state *uts, struct udevice *parent, |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 577 | int count, int key, struct udevice *child[]) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 578 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 579 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 580 | int i; |
| 581 | |
| 582 | for (i = 0; i < count; i++) { |
| 583 | struct dm_test_pdata *pdata; |
| 584 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 585 | ut_assertok(device_bind_by_name(parent, false, |
| 586 | &driver_info_manual, &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 587 | pdata = calloc(1, sizeof(*pdata)); |
| 588 | pdata->ping_add = key + i; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 589 | dev_set_plat(dev, pdata); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 590 | if (child) |
| 591 | child[i] = dev; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | #define NODE_COUNT 10 |
| 598 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 599 | static int dm_test_children(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 600 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 601 | struct udevice *top[NODE_COUNT]; |
| 602 | struct udevice *child[NODE_COUNT]; |
| 603 | struct udevice *grandchild[NODE_COUNT]; |
| 604 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 605 | int total; |
| 606 | int ret; |
| 607 | int i; |
| 608 | |
| 609 | /* We don't care about the numbering for this test */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 610 | uts->skip_post_probe = 1; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 611 | |
| 612 | ut_assert(NODE_COUNT > 5); |
| 613 | |
| 614 | /* First create 10 top-level children */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 615 | ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 616 | |
| 617 | /* Now a few have their own children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 618 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 619 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 620 | |
| 621 | /* And grandchildren */ |
| 622 | for (i = 0; i < NODE_COUNT; i++) |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 623 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 624 | i == 2 ? grandchild : NULL)); |
| 625 | |
| 626 | /* Check total number of devices */ |
| 627 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 628 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 629 | |
| 630 | /* Try probing one of the grandchildren */ |
| 631 | ut_assertok(uclass_get_device(UCLASS_TEST, |
| 632 | NODE_COUNT * 3 + 2 * NODE_COUNT, &dev)); |
| 633 | ut_asserteq_ptr(grandchild[0], dev); |
| 634 | |
| 635 | /* |
| 636 | * This should have probed the child and top node also, for a total |
| 637 | * of 3 nodes. |
| 638 | */ |
| 639 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 640 | |
| 641 | /* Probe the other grandchildren */ |
| 642 | for (i = 1; i < NODE_COUNT; i++) |
| 643 | ut_assertok(device_probe(grandchild[i])); |
| 644 | |
| 645 | ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 646 | |
| 647 | /* Probe everything */ |
Michal Suchanek | 44322b5 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 648 | ret = uclass_probe_all(UCLASS_TEST); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 649 | ut_assertok(ret); |
| 650 | |
| 651 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 652 | |
| 653 | /* Remove a top-level child and check that the children are removed */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 654 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 655 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 656 | dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0; |
| 657 | |
| 658 | /* Try one with grandchildren */ |
| 659 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 660 | ut_asserteq_ptr(dev, top[5]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 661 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 662 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 663 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 664 | |
| 665 | /* Try the same with unbind */ |
| 666 | ut_assertok(device_unbind(top[2])); |
| 667 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 668 | dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0; |
| 669 | |
| 670 | /* Try one with grandchildren */ |
| 671 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 672 | ut_asserteq_ptr(dev, top[6]); |
| 673 | ut_assertok(device_unbind(top[5])); |
| 674 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 675 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | DM_TEST(dm_test_children, 0); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 680 | |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 681 | static int dm_test_device_reparent(struct unit_test_state *uts) |
| 682 | { |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 683 | struct udevice *top[NODE_COUNT]; |
| 684 | struct udevice *child[NODE_COUNT]; |
| 685 | struct udevice *grandchild[NODE_COUNT]; |
| 686 | struct udevice *dev; |
| 687 | int total; |
| 688 | int ret; |
| 689 | int i; |
| 690 | |
| 691 | /* We don't care about the numbering for this test */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 692 | uts->skip_post_probe = 1; |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 693 | |
| 694 | ut_assert(NODE_COUNT > 5); |
| 695 | |
| 696 | /* First create 10 top-level children */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 697 | ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top)); |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 698 | |
| 699 | /* Now a few have their own children */ |
| 700 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 701 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
| 702 | |
| 703 | /* And grandchildren */ |
| 704 | for (i = 0; i < NODE_COUNT; i++) |
| 705 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
| 706 | i == 2 ? grandchild : NULL)); |
| 707 | |
| 708 | /* Check total number of devices */ |
| 709 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 710 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 711 | |
| 712 | /* Probe everything */ |
| 713 | for (i = 0; i < total; i++) |
| 714 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 715 | |
| 716 | /* Re-parent top-level children with no grandchildren. */ |
| 717 | ut_assertok(device_reparent(top[3], top[0])); |
| 718 | /* try to get devices */ |
| 719 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 720 | dev; |
| 721 | ret = uclass_find_next_device(&dev)) { |
| 722 | ut_assert(!ret); |
| 723 | ut_assertnonnull(dev); |
| 724 | } |
| 725 | |
| 726 | ut_assertok(device_reparent(top[4], top[0])); |
| 727 | /* try to get devices */ |
| 728 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 729 | dev; |
| 730 | ret = uclass_find_next_device(&dev)) { |
| 731 | ut_assert(!ret); |
| 732 | ut_assertnonnull(dev); |
| 733 | } |
| 734 | |
| 735 | /* Re-parent top-level children with grandchildren. */ |
| 736 | ut_assertok(device_reparent(top[2], top[0])); |
| 737 | /* try to get devices */ |
| 738 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 739 | dev; |
| 740 | ret = uclass_find_next_device(&dev)) { |
| 741 | ut_assert(!ret); |
| 742 | ut_assertnonnull(dev); |
| 743 | } |
| 744 | |
| 745 | ut_assertok(device_reparent(top[5], top[2])); |
| 746 | /* try to get devices */ |
| 747 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 748 | dev; |
| 749 | ret = uclass_find_next_device(&dev)) { |
| 750 | ut_assert(!ret); |
| 751 | ut_assertnonnull(dev); |
| 752 | } |
| 753 | |
| 754 | /* Re-parent grandchildren. */ |
| 755 | ut_assertok(device_reparent(grandchild[0], top[1])); |
| 756 | /* try to get devices */ |
| 757 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 758 | dev; |
| 759 | ret = uclass_find_next_device(&dev)) { |
| 760 | ut_assert(!ret); |
| 761 | ut_assertnonnull(dev); |
| 762 | } |
| 763 | |
| 764 | ut_assertok(device_reparent(grandchild[1], top[1])); |
| 765 | /* try to get devices */ |
| 766 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 767 | dev; |
| 768 | ret = uclass_find_next_device(&dev)) { |
| 769 | ut_assert(!ret); |
| 770 | ut_assertnonnull(dev); |
| 771 | } |
| 772 | |
| 773 | /* Remove re-pareneted devices. */ |
| 774 | ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL)); |
| 775 | /* try to get devices */ |
| 776 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 777 | dev; |
| 778 | ret = uclass_find_next_device(&dev)) { |
| 779 | ut_assert(!ret); |
| 780 | ut_assertnonnull(dev); |
| 781 | } |
| 782 | |
| 783 | ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL)); |
| 784 | /* try to get devices */ |
| 785 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 786 | dev; |
| 787 | ret = uclass_find_next_device(&dev)) { |
| 788 | ut_assert(!ret); |
| 789 | ut_assertnonnull(dev); |
| 790 | } |
| 791 | |
| 792 | ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL)); |
| 793 | /* try to get devices */ |
| 794 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 795 | dev; |
| 796 | ret = uclass_find_next_device(&dev)) { |
| 797 | ut_assert(!ret); |
| 798 | ut_assertnonnull(dev); |
| 799 | } |
| 800 | |
| 801 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
| 802 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 803 | dev; |
| 804 | ret = uclass_find_next_device(&dev)) { |
| 805 | ut_assert(!ret); |
| 806 | ut_assertnonnull(dev); |
| 807 | } |
| 808 | |
| 809 | ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL)); |
| 810 | /* try to get devices */ |
| 811 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 812 | dev; |
| 813 | ret = uclass_find_next_device(&dev)) { |
| 814 | ut_assert(!ret); |
| 815 | ut_assertnonnull(dev); |
| 816 | } |
| 817 | |
| 818 | ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL)); |
| 819 | /* try to get devices */ |
| 820 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 821 | dev; |
| 822 | ret = uclass_find_next_device(&dev)) { |
| 823 | ut_assert(!ret); |
| 824 | ut_assertnonnull(dev); |
| 825 | } |
| 826 | |
| 827 | /* Try the same with unbind */ |
| 828 | ut_assertok(device_unbind(top[3])); |
| 829 | ut_assertok(device_unbind(top[4])); |
| 830 | ut_assertok(device_unbind(top[5])); |
| 831 | ut_assertok(device_unbind(top[2])); |
| 832 | |
| 833 | ut_assertok(device_unbind(grandchild[0])); |
| 834 | ut_assertok(device_unbind(grandchild[1])); |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | DM_TEST(dm_test_device_reparent, 0); |
| 839 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 840 | /* Test that pre-relocation devices work as expected */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 841 | static int dm_test_pre_reloc(struct unit_test_state *uts) |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 842 | { |
| 843 | struct udevice *dev; |
| 844 | |
| 845 | /* The normal driver should refuse to bind before relocation */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 846 | ut_asserteq(-EPERM, device_bind_by_name(uts->root, true, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 847 | &driver_info_manual, &dev)); |
| 848 | |
| 849 | /* But this one is marked pre-reloc */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 850 | ut_assertok(device_bind_by_name(uts->root, true, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 851 | &driver_info_pre_reloc, &dev)); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | DM_TEST(dm_test_pre_reloc, 0); |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 856 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 857 | /* |
| 858 | * Test that removal of devices, either via the "normal" device_remove() |
| 859 | * API or via the device driver selective flag works as expected |
| 860 | */ |
| 861 | static int dm_test_remove_active_dma(struct unit_test_state *uts) |
| 862 | { |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 863 | struct udevice *dev; |
| 864 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 865 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma, |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 866 | &dev)); |
| 867 | ut_assert(dev); |
| 868 | |
| 869 | /* Probe the device */ |
| 870 | ut_assertok(device_probe(dev)); |
| 871 | |
| 872 | /* Test if device is active right now */ |
| 873 | ut_asserteq(true, device_active(dev)); |
| 874 | |
| 875 | /* Remove the device via selective remove flag */ |
| 876 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 877 | |
| 878 | /* Test if device is inactive right now */ |
| 879 | ut_asserteq(false, device_active(dev)); |
| 880 | |
| 881 | /* Probe the device again */ |
| 882 | ut_assertok(device_probe(dev)); |
| 883 | |
| 884 | /* Test if device is active right now */ |
| 885 | ut_asserteq(true, device_active(dev)); |
| 886 | |
| 887 | /* Remove the device via "normal" remove API */ |
| 888 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
| 889 | |
| 890 | /* Test if device is inactive right now */ |
| 891 | ut_asserteq(false, device_active(dev)); |
| 892 | |
| 893 | /* |
| 894 | * Test if a device without the active DMA flags is not removed upon |
| 895 | * the active DMA remove call |
| 896 | */ |
| 897 | ut_assertok(device_unbind(dev)); |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 898 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 899 | &dev)); |
| 900 | ut_assert(dev); |
| 901 | |
| 902 | /* Probe the device */ |
| 903 | ut_assertok(device_probe(dev)); |
| 904 | |
| 905 | /* Test if device is active right now */ |
| 906 | ut_asserteq(true, device_active(dev)); |
| 907 | |
| 908 | /* Remove the device via selective remove flag */ |
| 909 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 910 | |
| 911 | /* Test if device is still active right now */ |
| 912 | ut_asserteq(true, device_active(dev)); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | DM_TEST(dm_test_remove_active_dma, 0); |
| 917 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 918 | /* Test removal of 'vital' devices */ |
| 919 | static int dm_test_remove_vital(struct unit_test_state *uts) |
| 920 | { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 921 | struct udevice *normal, *dma, *vital, *dma_vital; |
| 922 | |
| 923 | /* Skip the behaviour in test_post_probe() */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 924 | uts->skip_post_probe = 1; |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 925 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 926 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 927 | &normal)); |
| 928 | ut_assertnonnull(normal); |
| 929 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 930 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_act_dma, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 931 | &dma)); |
| 932 | ut_assertnonnull(dma); |
| 933 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 934 | ut_assertok(device_bind_by_name(uts->root, false, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 935 | &driver_info_vital_clk, &vital)); |
| 936 | ut_assertnonnull(vital); |
| 937 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 938 | ut_assertok(device_bind_by_name(uts->root, false, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 939 | &driver_info_act_dma_vital_clk, |
| 940 | &dma_vital)); |
| 941 | ut_assertnonnull(dma_vital); |
| 942 | |
| 943 | /* Probe the devices */ |
| 944 | ut_assertok(device_probe(normal)); |
| 945 | ut_assertok(device_probe(dma)); |
| 946 | ut_assertok(device_probe(vital)); |
| 947 | ut_assertok(device_probe(dma_vital)); |
| 948 | |
| 949 | /* Check that devices are active right now */ |
| 950 | ut_asserteq(true, device_active(normal)); |
| 951 | ut_asserteq(true, device_active(dma)); |
| 952 | ut_asserteq(true, device_active(vital)); |
| 953 | ut_asserteq(true, device_active(dma_vital)); |
| 954 | |
| 955 | /* Remove active devices via selective remove flag */ |
| 956 | dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL); |
| 957 | |
| 958 | /* |
| 959 | * Check that this only has an effect on the dma device, since two |
| 960 | * devices are vital and the third does not have active DMA |
| 961 | */ |
| 962 | ut_asserteq(true, device_active(normal)); |
| 963 | ut_asserteq(false, device_active(dma)); |
| 964 | ut_asserteq(true, device_active(vital)); |
| 965 | ut_asserteq(true, device_active(dma_vital)); |
| 966 | |
| 967 | /* Remove active devices via selective remove flag */ |
| 968 | ut_assertok(device_probe(dma)); |
| 969 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 970 | |
| 971 | /* This should have affected both active-dma devices */ |
| 972 | ut_asserteq(true, device_active(normal)); |
| 973 | ut_asserteq(false, device_active(dma)); |
| 974 | ut_asserteq(true, device_active(vital)); |
| 975 | ut_asserteq(false, device_active(dma_vital)); |
| 976 | |
| 977 | /* Remove non-vital devices */ |
| 978 | ut_assertok(device_probe(dma)); |
| 979 | ut_assertok(device_probe(dma_vital)); |
| 980 | dm_remove_devices_flags(DM_REMOVE_NON_VITAL); |
| 981 | |
| 982 | /* This should have affected only non-vital devices */ |
| 983 | ut_asserteq(false, device_active(normal)); |
| 984 | ut_asserteq(false, device_active(dma)); |
| 985 | ut_asserteq(true, device_active(vital)); |
| 986 | ut_asserteq(true, device_active(dma_vital)); |
| 987 | |
| 988 | /* Remove vital devices via normal remove flag */ |
| 989 | ut_assertok(device_probe(normal)); |
| 990 | ut_assertok(device_probe(dma)); |
| 991 | dm_remove_devices_flags(DM_REMOVE_NORMAL); |
| 992 | |
| 993 | /* Check that all devices are inactive right now */ |
| 994 | ut_asserteq(false, device_active(normal)); |
| 995 | ut_asserteq(false, device_active(dma)); |
| 996 | ut_asserteq(false, device_active(vital)); |
| 997 | ut_asserteq(false, device_active(dma_vital)); |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | DM_TEST(dm_test_remove_vital, 0); |
| 1002 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1003 | static int dm_test_uclass_before_ready(struct unit_test_state *uts) |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 1004 | { |
| 1005 | struct uclass *uc; |
| 1006 | |
| 1007 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 1008 | |
Simon Glass | 51a0eac | 2015-04-19 07:21:02 -0600 | [diff] [blame] | 1009 | gd->dm_root = NULL; |
| 1010 | gd->dm_root_f = NULL; |
| 1011 | memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); |
| 1012 | |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 1013 | ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); |
Simon Glass | 8e9eacf | 2021-08-01 12:05:23 -0600 | [diff] [blame] | 1014 | ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc)); |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 1015 | |
| 1016 | return 0; |
| 1017 | } |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 1018 | DM_TEST(dm_test_uclass_before_ready, 0); |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 1019 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1020 | static int dm_test_uclass_devices_find(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1021 | { |
| 1022 | struct udevice *dev; |
| 1023 | int ret; |
| 1024 | |
| 1025 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 1026 | dev; |
| 1027 | ret = uclass_find_next_device(&dev)) { |
| 1028 | ut_assert(!ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1029 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
Simon Glass | 0bb4427 | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 1032 | ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1033 | ut_assertnull(dev); |
Marcel Ziswiler | 75ec16f | 2019-02-01 16:01:07 +0100 | [diff] [blame] | 1034 | |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1037 | DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1038 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1039 | 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] | 1040 | { |
| 1041 | struct udevice *finddev; |
| 1042 | struct udevice *testdev; |
| 1043 | int findret, ret; |
| 1044 | |
| 1045 | /* |
| 1046 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 1047 | * use its name and try to find it by uclass_find_device_by_name(). |
| 1048 | * Then, on success check if: |
| 1049 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 1050 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 1051 | * |
| 1052 | * We assume that, each uclass's device name is unique, so if not, then |
| 1053 | * this will fail on checking condition: testdev == finddev, since the |
| 1054 | * uclass_find_device_by_name(), returns the first device by given name. |
| 1055 | */ |
| 1056 | for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev); |
| 1057 | testdev; |
| 1058 | ret = uclass_find_next_device(&testdev)) { |
| 1059 | ut_assertok(ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1060 | ut_assertnonnull(testdev); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1061 | |
| 1062 | findret = uclass_find_device_by_name(UCLASS_TEST_FDT, |
| 1063 | testdev->name, |
| 1064 | &finddev); |
| 1065 | |
| 1066 | ut_assertok(findret); |
| 1067 | ut_assert(testdev); |
| 1068 | ut_asserteq_str(testdev->name, finddev->name); |
| 1069 | ut_asserteq_ptr(testdev, finddev); |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1074 | 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] | 1075 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1076 | static int dm_test_uclass_devices_get(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1077 | { |
| 1078 | struct udevice *dev; |
| 1079 | int ret; |
| 1080 | |
Michal Suchanek | 98e1ada | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 1081 | for (ret = uclass_first_device_check(UCLASS_TEST, &dev); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1082 | dev; |
Michal Suchanek | 98e1ada | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 1083 | ret = uclass_next_device_check(&dev)) { |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1084 | ut_assert(!ret); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1085 | ut_assert(device_active(dev)); |
| 1086 | } |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1090 | DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1091 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1092 | 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] | 1093 | { |
| 1094 | struct udevice *finddev; |
| 1095 | struct udevice *testdev; |
| 1096 | int ret, findret; |
| 1097 | |
| 1098 | /* |
| 1099 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 1100 | * use its name and try to get it by uclass_get_device_by_name(). |
| 1101 | * On success check if: |
| 1102 | * - returned finddev' is active |
| 1103 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 1104 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 1105 | * |
| 1106 | * We asserts that the 'testdev' is active on each loop entry, so we |
| 1107 | * could be sure that the 'finddev' is activated too, but for sure |
| 1108 | * we check it again. |
| 1109 | * |
| 1110 | * We assume that, each uclass's device name is unique, so if not, then |
| 1111 | * this will fail on checking condition: testdev == finddev, since the |
| 1112 | * uclass_get_device_by_name(), returns the first device by given name. |
| 1113 | */ |
Michal Suchanek | 98e1ada | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 1114 | for (ret = uclass_first_device_check(UCLASS_TEST_FDT, &testdev); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1115 | testdev; |
Michal Suchanek | 98e1ada | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 1116 | ret = uclass_next_device_check(&testdev)) { |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1117 | ut_assertok(ret); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1118 | ut_assert(device_active(testdev)); |
| 1119 | |
| 1120 | findret = uclass_get_device_by_name(UCLASS_TEST_FDT, |
| 1121 | testdev->name, |
| 1122 | &finddev); |
| 1123 | |
| 1124 | ut_assertok(findret); |
| 1125 | ut_assert(finddev); |
| 1126 | ut_assert(device_active(finddev)); |
| 1127 | ut_asserteq_str(testdev->name, finddev->name); |
| 1128 | ut_asserteq_ptr(testdev, finddev); |
| 1129 | } |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1133 | 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] | 1134 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1135 | 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] | 1136 | { |
| 1137 | struct udevice *dev; |
| 1138 | |
| 1139 | ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev)); |
| 1140 | ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev)); |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1144 | DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA); |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 1145 | |
| 1146 | static int dm_test_uclass_names(struct unit_test_state *uts) |
| 1147 | { |
| 1148 | ut_asserteq_str("test", uclass_get_name(UCLASS_TEST)); |
| 1149 | ut_asserteq(UCLASS_TEST, uclass_get_by_name("test")); |
| 1150 | |
Simon Glass | f1f519f | 2022-04-24 23:30:59 -0600 | [diff] [blame] | 1151 | ut_asserteq(UCLASS_SPI, uclass_get_by_name("spi")); |
| 1152 | |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1155 | DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1156 | |
| 1157 | static int dm_test_inactive_child(struct unit_test_state *uts) |
| 1158 | { |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1159 | struct udevice *parent, *dev1, *dev2; |
| 1160 | |
| 1161 | /* Skip the behaviour in test_post_probe() */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 1162 | uts->skip_post_probe = 1; |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1163 | |
| 1164 | ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent)); |
| 1165 | |
| 1166 | /* |
| 1167 | * Create a child but do not activate it. Calling the function again |
| 1168 | * should return the same child. |
| 1169 | */ |
| 1170 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1171 | UCLASS_TEST, &dev1)); |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1172 | ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv), |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 1173 | "test_child", 0, ofnode_null(), &dev1)); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1174 | |
| 1175 | ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST, |
| 1176 | &dev2)); |
| 1177 | ut_asserteq_ptr(dev1, dev2); |
| 1178 | |
| 1179 | ut_assertok(device_probe(dev1)); |
| 1180 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1181 | UCLASS_TEST, &dev2)); |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1185 | DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA); |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1186 | |
| 1187 | /* Make sure all bound devices have a sequence number */ |
| 1188 | static int dm_test_all_have_seq(struct unit_test_state *uts) |
| 1189 | { |
| 1190 | struct udevice *dev; |
| 1191 | struct uclass *uc; |
| 1192 | |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 1193 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1194 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1195 | if (dev->seq_ == -1) |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1196 | printf("Device '%s' has no seq (%d)\n", |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1197 | dev->name, dev->seq_); |
| 1198 | ut_assert(dev->seq_ != -1); |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA); |
Nicolas Saenz Julienne | 892e9b4 | 2021-01-12 13:55:25 +0100 | [diff] [blame] | 1205 | |
Simon Glass | 2697425 | 2021-07-05 16:32:42 -0600 | [diff] [blame] | 1206 | #if CONFIG_IS_ENABLED(DM_DMA) |
Nicolas Saenz Julienne | 892e9b4 | 2021-01-12 13:55:25 +0100 | [diff] [blame] | 1207 | static int dm_test_dma_offset(struct unit_test_state *uts) |
| 1208 | { |
| 1209 | struct udevice *dev; |
| 1210 | ofnode node; |
| 1211 | |
| 1212 | /* Make sure the bus's dma-ranges aren't taken into account here */ |
| 1213 | node = ofnode_path("/mmio-bus@0"); |
| 1214 | ut_assert(ofnode_valid(node)); |
| 1215 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev)); |
| 1216 | ut_asserteq_64(0, dev->dma_offset); |
| 1217 | |
| 1218 | /* Device behind a bus with dma-ranges */ |
| 1219 | node = ofnode_path("/mmio-bus@0/subnode@0"); |
| 1220 | ut_assert(ofnode_valid(node)); |
| 1221 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev)); |
| 1222 | ut_asserteq_64(-0x10000000ULL, dev->dma_offset); |
| 1223 | |
| 1224 | /* This one has no dma-ranges */ |
| 1225 | node = ofnode_path("/mmio-bus@1"); |
| 1226 | ut_assert(ofnode_valid(node)); |
| 1227 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev)); |
| 1228 | node = ofnode_path("/mmio-bus@1/subnode@0"); |
| 1229 | ut_assert(ofnode_valid(node)); |
| 1230 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev)); |
| 1231 | ut_asserteq_64(0, dev->dma_offset); |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 2697425 | 2021-07-05 16:32:42 -0600 | [diff] [blame] | 1236 | #endif |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 1237 | |
| 1238 | /* Test dm_get_stats() */ |
| 1239 | static int dm_test_get_stats(struct unit_test_state *uts) |
| 1240 | { |
| 1241 | int dev_count, uc_count; |
| 1242 | |
| 1243 | dm_get_stats(&dev_count, &uc_count); |
| 1244 | ut_assert(dev_count > 50); |
| 1245 | ut_assert(uc_count > 30); |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT); |
Simon Glass | 9670f7d | 2022-04-24 23:31:00 -0600 | [diff] [blame] | 1250 | |
| 1251 | /* Test uclass_find_device_by_name() */ |
| 1252 | static int dm_test_uclass_find_device(struct unit_test_state *uts) |
| 1253 | { |
| 1254 | struct udevice *dev; |
| 1255 | |
| 1256 | ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev)); |
| 1257 | ut_asserteq(-ENODEV, |
| 1258 | uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev)); |
| 1259 | ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5, |
| 1260 | &dev)); |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT); |
Simon Glass | d1f12cf | 2022-05-08 04:39:24 -0600 | [diff] [blame] | 1265 | |
| 1266 | /* Test getting information about tags attached to devices */ |
| 1267 | static int dm_test_dev_get_attach(struct unit_test_state *uts) |
| 1268 | { |
| 1269 | struct udevice *dev; |
| 1270 | |
| 1271 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 1272 | ut_asserteq_str("a-test", dev->name); |
| 1273 | |
| 1274 | ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT)); |
| 1275 | ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV)); |
| 1276 | ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV)); |
| 1277 | ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT)); |
| 1278 | ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT)); |
| 1279 | ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV)); |
| 1280 | |
| 1281 | ut_asserteq(sizeof(struct dm_test_pdata), |
| 1282 | dev_get_attach_size(dev, DM_TAG_PLAT)); |
| 1283 | ut_asserteq(sizeof(struct dm_test_priv), |
| 1284 | dev_get_attach_size(dev, DM_TAG_PRIV)); |
| 1285 | ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PRIV)); |
| 1286 | ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_UC_PLAT)); |
| 1287 | ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT)); |
| 1288 | ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV)); |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | DM_TEST(dm_test_dev_get_attach, UT_TESTF_SCAN_FDT); |
| 1293 | |
| 1294 | /* Test getting information about tags attached to bus devices */ |
| 1295 | static int dm_test_dev_get_attach_bus(struct unit_test_state *uts) |
| 1296 | { |
| 1297 | struct udevice *dev, *child; |
| 1298 | |
| 1299 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &dev)); |
| 1300 | ut_asserteq_str("some-bus", dev->name); |
| 1301 | |
| 1302 | ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PLAT)); |
| 1303 | ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_PRIV)); |
| 1304 | ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PRIV)); |
| 1305 | ut_assertnonnull(dev_get_attach_ptr(dev, DM_TAG_UC_PLAT)); |
| 1306 | ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PLAT)); |
| 1307 | ut_assertnull(dev_get_attach_ptr(dev, DM_TAG_PARENT_PRIV)); |
| 1308 | |
| 1309 | ut_asserteq(sizeof(struct dm_test_pdata), |
| 1310 | dev_get_attach_size(dev, DM_TAG_PLAT)); |
| 1311 | ut_asserteq(sizeof(struct dm_test_priv), |
| 1312 | dev_get_attach_size(dev, DM_TAG_PRIV)); |
| 1313 | ut_asserteq(sizeof(struct dm_test_uclass_priv), |
| 1314 | dev_get_attach_size(dev, DM_TAG_UC_PRIV)); |
| 1315 | ut_asserteq(sizeof(struct dm_test_uclass_plat), |
| 1316 | dev_get_attach_size(dev, DM_TAG_UC_PLAT)); |
| 1317 | ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PLAT)); |
| 1318 | ut_asserteq(0, dev_get_attach_size(dev, DM_TAG_PARENT_PRIV)); |
| 1319 | |
| 1320 | /* Now try the child of the bus */ |
| 1321 | ut_assertok(device_first_child_err(dev, &child)); |
| 1322 | ut_asserteq_str("c-test@5", child->name); |
| 1323 | |
| 1324 | ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PLAT)); |
| 1325 | ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PRIV)); |
| 1326 | ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PRIV)); |
| 1327 | ut_assertnull(dev_get_attach_ptr(child, DM_TAG_UC_PLAT)); |
| 1328 | ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PLAT)); |
| 1329 | ut_assertnonnull(dev_get_attach_ptr(child, DM_TAG_PARENT_PRIV)); |
| 1330 | |
| 1331 | ut_asserteq(sizeof(struct dm_test_pdata), |
| 1332 | dev_get_attach_size(child, DM_TAG_PLAT)); |
| 1333 | ut_asserteq(sizeof(struct dm_test_priv), |
| 1334 | dev_get_attach_size(child, DM_TAG_PRIV)); |
| 1335 | ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PRIV)); |
| 1336 | ut_asserteq(0, dev_get_attach_size(child, DM_TAG_UC_PLAT)); |
| 1337 | ut_asserteq(sizeof(struct dm_test_parent_plat), |
| 1338 | dev_get_attach_size(child, DM_TAG_PARENT_PLAT)); |
| 1339 | ut_asserteq(sizeof(struct dm_test_parent_data), |
| 1340 | dev_get_attach_size(child, DM_TAG_PARENT_PRIV)); |
| 1341 | |
| 1342 | return 0; |
| 1343 | } |
| 1344 | DM_TEST(dm_test_dev_get_attach_bus, UT_TESTF_SCAN_FDT); |
Simon Glass | be1621b | 2022-05-08 04:39:25 -0600 | [diff] [blame] | 1345 | |
| 1346 | /* Test getting information about tags attached to bus devices */ |
| 1347 | static int dm_test_dev_get_mem(struct unit_test_state *uts) |
| 1348 | { |
| 1349 | struct dm_stats stats; |
| 1350 | |
| 1351 | dm_get_mem(&stats); |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | DM_TEST(dm_test_dev_get_mem, UT_TESTF_SCAN_FDT); |