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 | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 310 | int pingret; |
| 311 | int ret; |
| 312 | |
| 313 | memcpy(op_count, dm_testdrv_op_count, sizeof(op_count)); |
| 314 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 315 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 316 | &dev)); |
| 317 | ut_assert(dev); |
| 318 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] |
| 319 | == op_count[DM_TEST_OP_BIND] + 1); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 320 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 321 | |
| 322 | /* Probe the device - it should fail allocating private data */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 323 | uts->force_fail_alloc = 1; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 324 | ret = device_probe(dev); |
| 325 | ut_assert(ret == -ENOMEM); |
| 326 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 327 | == op_count[DM_TEST_OP_PROBE] + 1); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 328 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 329 | |
| 330 | /* Try again without the alloc failure */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 331 | uts->force_fail_alloc = 0; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 332 | ut_assertok(device_probe(dev)); |
| 333 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 334 | == op_count[DM_TEST_OP_PROBE] + 2); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 335 | ut_assert(dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 336 | |
| 337 | /* This should be device 3 in the uclass */ |
| 338 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 339 | ut_assert(dev == test_dev); |
| 340 | |
| 341 | /* Try ping */ |
| 342 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 343 | ut_assert(pingret == 102); |
| 344 | |
| 345 | /* Now remove device 3 */ |
| 346 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 347 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 348 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
| 349 | |
| 350 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 351 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 352 | ut_assertok(device_unbind(dev)); |
| 353 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 354 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 355 | |
| 356 | return 0; |
| 357 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 358 | 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] | 359 | |
| 360 | /* Test that we can bind/unbind and the lists update correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 361 | static int dm_test_ordering(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 362 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 363 | struct udevice *dev, *dev_penultimate, *dev_last, *test_dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 364 | int pingret; |
| 365 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 366 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 367 | &dev)); |
| 368 | ut_assert(dev); |
| 369 | |
| 370 | /* Bind two new devices (numbers 4 and 5) */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 371 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 372 | &dev_penultimate)); |
| 373 | ut_assert(dev_penultimate); |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 374 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 375 | &dev_last)); |
| 376 | ut_assert(dev_last); |
| 377 | |
| 378 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 379 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 380 | ut_assertok(device_unbind(dev)); |
| 381 | |
| 382 | /* The device numbering should have shifted down one */ |
| 383 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 384 | ut_assert(dev_penultimate == test_dev); |
| 385 | ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev)); |
| 386 | ut_assert(dev_last == test_dev); |
| 387 | |
| 388 | /* Add back the original device 3, now in position 5 */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 389 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 390 | &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 391 | ut_assert(dev); |
| 392 | |
| 393 | /* Try ping */ |
| 394 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 395 | ut_assert(pingret == 102); |
| 396 | |
| 397 | /* Remove 3 and 4 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 398 | ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 399 | ut_assertok(device_unbind(dev_penultimate)); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 400 | ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 401 | ut_assertok(device_unbind(dev_last)); |
| 402 | |
| 403 | /* Our device should now be in position 3 */ |
| 404 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 405 | ut_assert(dev == test_dev); |
| 406 | |
| 407 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 408 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 409 | ut_assertok(device_unbind(dev)); |
| 410 | |
| 411 | return 0; |
| 412 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 413 | DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 414 | |
| 415 | /* Check that we can perform operations on a device (do a ping) */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 416 | int dm_check_operations(struct unit_test_state *uts, struct udevice *dev, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 417 | uint32_t base, struct dm_test_priv *priv) |
| 418 | { |
| 419 | int expected; |
| 420 | int pingret; |
| 421 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 422 | /* Getting the child device should allocate plat / priv */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 423 | ut_assertok(testfdt_ping(dev, 10, &pingret)); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 424 | ut_assert(dev_get_priv(dev)); |
| 425 | ut_assert(dev_get_plat(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 426 | |
| 427 | expected = 10 + base; |
| 428 | ut_asserteq(expected, pingret); |
| 429 | |
| 430 | /* Do another ping */ |
| 431 | ut_assertok(testfdt_ping(dev, 20, &pingret)); |
| 432 | expected = 20 + base; |
| 433 | ut_asserteq(expected, pingret); |
| 434 | |
| 435 | /* Now check the ping_total */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 436 | priv = dev_get_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 437 | ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2, |
| 438 | priv->ping_total); |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | /* Check that we can perform operations on devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 444 | static int dm_test_operations(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 445 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 446 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 447 | int i; |
| 448 | |
| 449 | /* |
| 450 | * Now check that the ping adds are what we expect. This is using the |
| 451 | * ping-add property in each node. |
| 452 | */ |
| 453 | for (i = 0; i < ARRAY_SIZE(test_pdata); i++) { |
| 454 | uint32_t base; |
| 455 | |
| 456 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 457 | |
| 458 | /* |
| 459 | * Get the 'reg' property, which tells us what the ping add |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 460 | * should be. We don't use the plat because we want |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 461 | * to test the code that sets that up (testfdt_drv_probe()). |
| 462 | */ |
| 463 | base = test_pdata[i].ping_add; |
| 464 | debug("dev=%d, base=%d\n", i, base); |
| 465 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 466 | ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev))); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 471 | DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 472 | |
| 473 | /* Remove all drivers and check that things work */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 474 | static int dm_test_remove(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 475 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 476 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 477 | int i; |
| 478 | |
| 479 | for (i = 0; i < 3; i++) { |
| 480 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 481 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 482 | ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 483 | "Driver %d/%s not activated", i, dev->name); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 484 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 485 | ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 486 | "Driver %d/%s should have deactivated", i, |
| 487 | dev->name); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 488 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 493 | 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] | 494 | |
| 495 | /* Remove and recreate everything, check for memory leaks */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 496 | static int dm_test_leak(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 497 | { |
| 498 | int i; |
| 499 | |
| 500 | for (i = 0; i < 2; i++) { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 501 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 502 | int ret; |
| 503 | int id; |
| 504 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 505 | dm_leak_check_start(uts); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 506 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 507 | ut_assertok(dm_scan_plat(false)); |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 508 | ut_assertok(dm_scan_fdt(false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 509 | |
| 510 | /* Scanning the uclass is enough to probe all the devices */ |
| 511 | for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) { |
| 512 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 513 | dev; |
| 514 | ret = uclass_next_device(&dev)) |
| 515 | ; |
| 516 | ut_assertok(ret); |
| 517 | } |
| 518 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 519 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | DM_TEST(dm_test_leak, 0); |
| 525 | |
| 526 | /* Test uclass init/destroy methods */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 527 | static int dm_test_uclass(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 528 | { |
| 529 | struct uclass *uc; |
| 530 | |
| 531 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 532 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 533 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 534 | ut_assert(uclass_get_priv(uc)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 535 | |
| 536 | ut_assertok(uclass_destroy(uc)); |
| 537 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 538 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | DM_TEST(dm_test_uclass, 0); |
| 543 | |
| 544 | /** |
| 545 | * create_children() - Create children of a parent node |
| 546 | * |
| 547 | * @dms: Test system state |
| 548 | * @parent: Parent device |
| 549 | * @count: Number of children to create |
| 550 | * @key: Key value to put in first child. Subsequence children |
| 551 | * receive an incrementing value |
| 552 | * @child: If not NULL, then the child device pointers are written into |
| 553 | * this array. |
| 554 | * @return 0 if OK, -ve on error |
| 555 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 556 | static int create_children(struct unit_test_state *uts, struct udevice *parent, |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 557 | int count, int key, struct udevice *child[]) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 558 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 559 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 560 | int i; |
| 561 | |
| 562 | for (i = 0; i < count; i++) { |
| 563 | struct dm_test_pdata *pdata; |
| 564 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 565 | ut_assertok(device_bind_by_name(parent, false, |
| 566 | &driver_info_manual, &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 567 | pdata = calloc(1, sizeof(*pdata)); |
| 568 | pdata->ping_add = key + i; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 569 | dev_set_plat(dev, pdata); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 570 | if (child) |
| 571 | child[i] = dev; |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | #define NODE_COUNT 10 |
| 578 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 579 | static int dm_test_children(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 580 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 581 | struct udevice *top[NODE_COUNT]; |
| 582 | struct udevice *child[NODE_COUNT]; |
| 583 | struct udevice *grandchild[NODE_COUNT]; |
| 584 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 585 | int total; |
| 586 | int ret; |
| 587 | int i; |
| 588 | |
| 589 | /* We don't care about the numbering for this test */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 590 | uts->skip_post_probe = 1; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 591 | |
| 592 | ut_assert(NODE_COUNT > 5); |
| 593 | |
| 594 | /* First create 10 top-level children */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 595 | ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 596 | |
| 597 | /* Now a few have their own children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 598 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 599 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 600 | |
| 601 | /* And grandchildren */ |
| 602 | for (i = 0; i < NODE_COUNT; i++) |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 603 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 604 | i == 2 ? grandchild : NULL)); |
| 605 | |
| 606 | /* Check total number of devices */ |
| 607 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 608 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 609 | |
| 610 | /* Try probing one of the grandchildren */ |
| 611 | ut_assertok(uclass_get_device(UCLASS_TEST, |
| 612 | NODE_COUNT * 3 + 2 * NODE_COUNT, &dev)); |
| 613 | ut_asserteq_ptr(grandchild[0], dev); |
| 614 | |
| 615 | /* |
| 616 | * This should have probed the child and top node also, for a total |
| 617 | * of 3 nodes. |
| 618 | */ |
| 619 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 620 | |
| 621 | /* Probe the other grandchildren */ |
| 622 | for (i = 1; i < NODE_COUNT; i++) |
| 623 | ut_assertok(device_probe(grandchild[i])); |
| 624 | |
| 625 | ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 626 | |
| 627 | /* Probe everything */ |
| 628 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 629 | dev; |
| 630 | ret = uclass_next_device(&dev)) |
| 631 | ; |
| 632 | ut_assertok(ret); |
| 633 | |
| 634 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 635 | |
| 636 | /* Remove a top-level child and check that the children are removed */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 637 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 638 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 639 | dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0; |
| 640 | |
| 641 | /* Try one with grandchildren */ |
| 642 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 643 | ut_asserteq_ptr(dev, top[5]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 644 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 645 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 646 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 647 | |
| 648 | /* Try the same with unbind */ |
| 649 | ut_assertok(device_unbind(top[2])); |
| 650 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 651 | dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0; |
| 652 | |
| 653 | /* Try one with grandchildren */ |
| 654 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 655 | ut_asserteq_ptr(dev, top[6]); |
| 656 | ut_assertok(device_unbind(top[5])); |
| 657 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 658 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | DM_TEST(dm_test_children, 0); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 663 | |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 664 | static int dm_test_device_reparent(struct unit_test_state *uts) |
| 665 | { |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 666 | struct udevice *top[NODE_COUNT]; |
| 667 | struct udevice *child[NODE_COUNT]; |
| 668 | struct udevice *grandchild[NODE_COUNT]; |
| 669 | struct udevice *dev; |
| 670 | int total; |
| 671 | int ret; |
| 672 | int i; |
| 673 | |
| 674 | /* We don't care about the numbering for this test */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 675 | uts->skip_post_probe = 1; |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 676 | |
| 677 | ut_assert(NODE_COUNT > 5); |
| 678 | |
| 679 | /* First create 10 top-level children */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 680 | ut_assertok(create_children(uts, uts->root, NODE_COUNT, 0, top)); |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 681 | |
| 682 | /* Now a few have their own children */ |
| 683 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 684 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
| 685 | |
| 686 | /* And grandchildren */ |
| 687 | for (i = 0; i < NODE_COUNT; i++) |
| 688 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
| 689 | i == 2 ? grandchild : NULL)); |
| 690 | |
| 691 | /* Check total number of devices */ |
| 692 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 693 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 694 | |
| 695 | /* Probe everything */ |
| 696 | for (i = 0; i < total; i++) |
| 697 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 698 | |
| 699 | /* Re-parent top-level children with no grandchildren. */ |
| 700 | ut_assertok(device_reparent(top[3], top[0])); |
| 701 | /* try to get devices */ |
| 702 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 703 | dev; |
| 704 | ret = uclass_find_next_device(&dev)) { |
| 705 | ut_assert(!ret); |
| 706 | ut_assertnonnull(dev); |
| 707 | } |
| 708 | |
| 709 | ut_assertok(device_reparent(top[4], top[0])); |
| 710 | /* try to get devices */ |
| 711 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 712 | dev; |
| 713 | ret = uclass_find_next_device(&dev)) { |
| 714 | ut_assert(!ret); |
| 715 | ut_assertnonnull(dev); |
| 716 | } |
| 717 | |
| 718 | /* Re-parent top-level children with grandchildren. */ |
| 719 | ut_assertok(device_reparent(top[2], top[0])); |
| 720 | /* try to get devices */ |
| 721 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 722 | dev; |
| 723 | ret = uclass_find_next_device(&dev)) { |
| 724 | ut_assert(!ret); |
| 725 | ut_assertnonnull(dev); |
| 726 | } |
| 727 | |
| 728 | ut_assertok(device_reparent(top[5], top[2])); |
| 729 | /* try to get devices */ |
| 730 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 731 | dev; |
| 732 | ret = uclass_find_next_device(&dev)) { |
| 733 | ut_assert(!ret); |
| 734 | ut_assertnonnull(dev); |
| 735 | } |
| 736 | |
| 737 | /* Re-parent grandchildren. */ |
| 738 | ut_assertok(device_reparent(grandchild[0], top[1])); |
| 739 | /* try to get devices */ |
| 740 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 741 | dev; |
| 742 | ret = uclass_find_next_device(&dev)) { |
| 743 | ut_assert(!ret); |
| 744 | ut_assertnonnull(dev); |
| 745 | } |
| 746 | |
| 747 | ut_assertok(device_reparent(grandchild[1], top[1])); |
| 748 | /* try to get devices */ |
| 749 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 750 | dev; |
| 751 | ret = uclass_find_next_device(&dev)) { |
| 752 | ut_assert(!ret); |
| 753 | ut_assertnonnull(dev); |
| 754 | } |
| 755 | |
| 756 | /* Remove re-pareneted devices. */ |
| 757 | ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL)); |
| 758 | /* try to get devices */ |
| 759 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 760 | dev; |
| 761 | ret = uclass_find_next_device(&dev)) { |
| 762 | ut_assert(!ret); |
| 763 | ut_assertnonnull(dev); |
| 764 | } |
| 765 | |
| 766 | ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL)); |
| 767 | /* try to get devices */ |
| 768 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 769 | dev; |
| 770 | ret = uclass_find_next_device(&dev)) { |
| 771 | ut_assert(!ret); |
| 772 | ut_assertnonnull(dev); |
| 773 | } |
| 774 | |
| 775 | ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL)); |
| 776 | /* try to get devices */ |
| 777 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 778 | dev; |
| 779 | ret = uclass_find_next_device(&dev)) { |
| 780 | ut_assert(!ret); |
| 781 | ut_assertnonnull(dev); |
| 782 | } |
| 783 | |
| 784 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
| 785 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 786 | dev; |
| 787 | ret = uclass_find_next_device(&dev)) { |
| 788 | ut_assert(!ret); |
| 789 | ut_assertnonnull(dev); |
| 790 | } |
| 791 | |
| 792 | ut_assertok(device_remove(grandchild[0], 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(grandchild[1], DM_REMOVE_NORMAL)); |
| 802 | /* try to get devices */ |
| 803 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 804 | dev; |
| 805 | ret = uclass_find_next_device(&dev)) { |
| 806 | ut_assert(!ret); |
| 807 | ut_assertnonnull(dev); |
| 808 | } |
| 809 | |
| 810 | /* Try the same with unbind */ |
| 811 | ut_assertok(device_unbind(top[3])); |
| 812 | ut_assertok(device_unbind(top[4])); |
| 813 | ut_assertok(device_unbind(top[5])); |
| 814 | ut_assertok(device_unbind(top[2])); |
| 815 | |
| 816 | ut_assertok(device_unbind(grandchild[0])); |
| 817 | ut_assertok(device_unbind(grandchild[1])); |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | DM_TEST(dm_test_device_reparent, 0); |
| 822 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 823 | /* Test that pre-relocation devices work as expected */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 824 | static int dm_test_pre_reloc(struct unit_test_state *uts) |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 825 | { |
| 826 | struct udevice *dev; |
| 827 | |
| 828 | /* The normal driver should refuse to bind before relocation */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 829 | ut_asserteq(-EPERM, device_bind_by_name(uts->root, true, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 830 | &driver_info_manual, &dev)); |
| 831 | |
| 832 | /* But this one is marked pre-reloc */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 833 | ut_assertok(device_bind_by_name(uts->root, true, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 834 | &driver_info_pre_reloc, &dev)); |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | DM_TEST(dm_test_pre_reloc, 0); |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 839 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 840 | /* |
| 841 | * Test that removal of devices, either via the "normal" device_remove() |
| 842 | * API or via the device driver selective flag works as expected |
| 843 | */ |
| 844 | static int dm_test_remove_active_dma(struct unit_test_state *uts) |
| 845 | { |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 846 | struct udevice *dev; |
| 847 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 848 | 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] | 849 | &dev)); |
| 850 | ut_assert(dev); |
| 851 | |
| 852 | /* Probe the device */ |
| 853 | ut_assertok(device_probe(dev)); |
| 854 | |
| 855 | /* Test if device is active right now */ |
| 856 | ut_asserteq(true, device_active(dev)); |
| 857 | |
| 858 | /* Remove the device via selective remove flag */ |
| 859 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 860 | |
| 861 | /* Test if device is inactive right now */ |
| 862 | ut_asserteq(false, device_active(dev)); |
| 863 | |
| 864 | /* Probe the device again */ |
| 865 | ut_assertok(device_probe(dev)); |
| 866 | |
| 867 | /* Test if device is active right now */ |
| 868 | ut_asserteq(true, device_active(dev)); |
| 869 | |
| 870 | /* Remove the device via "normal" remove API */ |
| 871 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
| 872 | |
| 873 | /* Test if device is inactive right now */ |
| 874 | ut_asserteq(false, device_active(dev)); |
| 875 | |
| 876 | /* |
| 877 | * Test if a device without the active DMA flags is not removed upon |
| 878 | * the active DMA remove call |
| 879 | */ |
| 880 | ut_assertok(device_unbind(dev)); |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 881 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 882 | &dev)); |
| 883 | ut_assert(dev); |
| 884 | |
| 885 | /* Probe the device */ |
| 886 | ut_assertok(device_probe(dev)); |
| 887 | |
| 888 | /* Test if device is active right now */ |
| 889 | ut_asserteq(true, device_active(dev)); |
| 890 | |
| 891 | /* Remove the device via selective remove flag */ |
| 892 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 893 | |
| 894 | /* Test if device is still active right now */ |
| 895 | ut_asserteq(true, device_active(dev)); |
| 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | DM_TEST(dm_test_remove_active_dma, 0); |
| 900 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 901 | /* Test removal of 'vital' devices */ |
| 902 | static int dm_test_remove_vital(struct unit_test_state *uts) |
| 903 | { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 904 | struct udevice *normal, *dma, *vital, *dma_vital; |
| 905 | |
| 906 | /* Skip the behaviour in test_post_probe() */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 907 | uts->skip_post_probe = 1; |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 908 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 909 | ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 910 | &normal)); |
| 911 | ut_assertnonnull(normal); |
| 912 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 913 | 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] | 914 | &dma)); |
| 915 | ut_assertnonnull(dma); |
| 916 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 917 | ut_assertok(device_bind_by_name(uts->root, false, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 918 | &driver_info_vital_clk, &vital)); |
| 919 | ut_assertnonnull(vital); |
| 920 | |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 921 | ut_assertok(device_bind_by_name(uts->root, false, |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 922 | &driver_info_act_dma_vital_clk, |
| 923 | &dma_vital)); |
| 924 | ut_assertnonnull(dma_vital); |
| 925 | |
| 926 | /* Probe the devices */ |
| 927 | ut_assertok(device_probe(normal)); |
| 928 | ut_assertok(device_probe(dma)); |
| 929 | ut_assertok(device_probe(vital)); |
| 930 | ut_assertok(device_probe(dma_vital)); |
| 931 | |
| 932 | /* Check that devices are active right now */ |
| 933 | ut_asserteq(true, device_active(normal)); |
| 934 | ut_asserteq(true, device_active(dma)); |
| 935 | ut_asserteq(true, device_active(vital)); |
| 936 | ut_asserteq(true, device_active(dma_vital)); |
| 937 | |
| 938 | /* Remove active devices via selective remove flag */ |
| 939 | dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL); |
| 940 | |
| 941 | /* |
| 942 | * Check that this only has an effect on the dma device, since two |
| 943 | * devices are vital and the third does not have active DMA |
| 944 | */ |
| 945 | ut_asserteq(true, device_active(normal)); |
| 946 | ut_asserteq(false, device_active(dma)); |
| 947 | ut_asserteq(true, device_active(vital)); |
| 948 | ut_asserteq(true, device_active(dma_vital)); |
| 949 | |
| 950 | /* Remove active devices via selective remove flag */ |
| 951 | ut_assertok(device_probe(dma)); |
| 952 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 953 | |
| 954 | /* This should have affected both active-dma devices */ |
| 955 | ut_asserteq(true, device_active(normal)); |
| 956 | ut_asserteq(false, device_active(dma)); |
| 957 | ut_asserteq(true, device_active(vital)); |
| 958 | ut_asserteq(false, device_active(dma_vital)); |
| 959 | |
| 960 | /* Remove non-vital devices */ |
| 961 | ut_assertok(device_probe(dma)); |
| 962 | ut_assertok(device_probe(dma_vital)); |
| 963 | dm_remove_devices_flags(DM_REMOVE_NON_VITAL); |
| 964 | |
| 965 | /* This should have affected only non-vital devices */ |
| 966 | ut_asserteq(false, device_active(normal)); |
| 967 | ut_asserteq(false, device_active(dma)); |
| 968 | ut_asserteq(true, device_active(vital)); |
| 969 | ut_asserteq(true, device_active(dma_vital)); |
| 970 | |
| 971 | /* Remove vital devices via normal remove flag */ |
| 972 | ut_assertok(device_probe(normal)); |
| 973 | ut_assertok(device_probe(dma)); |
| 974 | dm_remove_devices_flags(DM_REMOVE_NORMAL); |
| 975 | |
| 976 | /* Check that all devices are inactive right now */ |
| 977 | ut_asserteq(false, device_active(normal)); |
| 978 | ut_asserteq(false, device_active(dma)); |
| 979 | ut_asserteq(false, device_active(vital)); |
| 980 | ut_asserteq(false, device_active(dma_vital)); |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | DM_TEST(dm_test_remove_vital, 0); |
| 985 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 986 | static int dm_test_uclass_before_ready(struct unit_test_state *uts) |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 987 | { |
| 988 | struct uclass *uc; |
| 989 | |
| 990 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 991 | |
Simon Glass | 51a0eac | 2015-04-19 07:21:02 -0600 | [diff] [blame] | 992 | gd->dm_root = NULL; |
| 993 | gd->dm_root_f = NULL; |
| 994 | memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); |
| 995 | |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 996 | ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); |
Simon Glass | 8e9eacf | 2021-08-01 12:05:23 -0600 | [diff] [blame] | 997 | ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc)); |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 998 | |
| 999 | return 0; |
| 1000 | } |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 1001 | DM_TEST(dm_test_uclass_before_ready, 0); |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 1002 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1003 | static int dm_test_uclass_devices_find(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1004 | { |
| 1005 | struct udevice *dev; |
| 1006 | int ret; |
| 1007 | |
| 1008 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 1009 | dev; |
| 1010 | ret = uclass_find_next_device(&dev)) { |
| 1011 | ut_assert(!ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1012 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Simon Glass | 0bb4427 | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 1015 | ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1016 | ut_assertnull(dev); |
Marcel Ziswiler | 75ec16f | 2019-02-01 16:01:07 +0100 | [diff] [blame] | 1017 | |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1020 | DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1021 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1022 | 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] | 1023 | { |
| 1024 | struct udevice *finddev; |
| 1025 | struct udevice *testdev; |
| 1026 | int findret, ret; |
| 1027 | |
| 1028 | /* |
| 1029 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 1030 | * use its name and try to find it by uclass_find_device_by_name(). |
| 1031 | * Then, on success check if: |
| 1032 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 1033 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 1034 | * |
| 1035 | * We assume that, each uclass's device name is unique, so if not, then |
| 1036 | * this will fail on checking condition: testdev == finddev, since the |
| 1037 | * uclass_find_device_by_name(), returns the first device by given name. |
| 1038 | */ |
| 1039 | for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev); |
| 1040 | testdev; |
| 1041 | ret = uclass_find_next_device(&testdev)) { |
| 1042 | ut_assertok(ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1043 | ut_assertnonnull(testdev); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1044 | |
| 1045 | findret = uclass_find_device_by_name(UCLASS_TEST_FDT, |
| 1046 | testdev->name, |
| 1047 | &finddev); |
| 1048 | |
| 1049 | ut_assertok(findret); |
| 1050 | ut_assert(testdev); |
| 1051 | ut_asserteq_str(testdev->name, finddev->name); |
| 1052 | ut_asserteq_ptr(testdev, finddev); |
| 1053 | } |
| 1054 | |
| 1055 | return 0; |
| 1056 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1057 | 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] | 1058 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1059 | static int dm_test_uclass_devices_get(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1060 | { |
| 1061 | struct udevice *dev; |
| 1062 | int ret; |
| 1063 | |
| 1064 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 1065 | dev; |
| 1066 | ret = uclass_next_device(&dev)) { |
| 1067 | ut_assert(!ret); |
| 1068 | ut_assert(dev); |
| 1069 | ut_assert(device_active(dev)); |
| 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_get, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1075 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1076 | 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] | 1077 | { |
| 1078 | struct udevice *finddev; |
| 1079 | struct udevice *testdev; |
| 1080 | int ret, findret; |
| 1081 | |
| 1082 | /* |
| 1083 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 1084 | * use its name and try to get it by uclass_get_device_by_name(). |
| 1085 | * On success check if: |
| 1086 | * - returned finddev' is active |
| 1087 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 1088 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 1089 | * |
| 1090 | * We asserts that the 'testdev' is active on each loop entry, so we |
| 1091 | * could be sure that the 'finddev' is activated too, but for sure |
| 1092 | * we check it again. |
| 1093 | * |
| 1094 | * We assume that, each uclass's device name is unique, so if not, then |
| 1095 | * this will fail on checking condition: testdev == finddev, since the |
| 1096 | * uclass_get_device_by_name(), returns the first device by given name. |
| 1097 | */ |
| 1098 | for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev); |
| 1099 | testdev; |
| 1100 | ret = uclass_next_device(&testdev)) { |
| 1101 | ut_assertok(ret); |
| 1102 | ut_assert(testdev); |
| 1103 | ut_assert(device_active(testdev)); |
| 1104 | |
| 1105 | findret = uclass_get_device_by_name(UCLASS_TEST_FDT, |
| 1106 | testdev->name, |
| 1107 | &finddev); |
| 1108 | |
| 1109 | ut_assertok(findret); |
| 1110 | ut_assert(finddev); |
| 1111 | ut_assert(device_active(finddev)); |
| 1112 | ut_asserteq_str(testdev->name, finddev->name); |
| 1113 | ut_asserteq_ptr(testdev, finddev); |
| 1114 | } |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1118 | 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] | 1119 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1120 | 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] | 1121 | { |
| 1122 | struct udevice *dev; |
| 1123 | |
| 1124 | ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev)); |
| 1125 | ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev)); |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1129 | DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA); |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 1130 | |
| 1131 | static int dm_test_uclass_names(struct unit_test_state *uts) |
| 1132 | { |
| 1133 | ut_asserteq_str("test", uclass_get_name(UCLASS_TEST)); |
| 1134 | ut_asserteq(UCLASS_TEST, uclass_get_by_name("test")); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1138 | DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1139 | |
| 1140 | static int dm_test_inactive_child(struct unit_test_state *uts) |
| 1141 | { |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1142 | struct udevice *parent, *dev1, *dev2; |
| 1143 | |
| 1144 | /* Skip the behaviour in test_post_probe() */ |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 1145 | uts->skip_post_probe = 1; |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1146 | |
| 1147 | ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent)); |
| 1148 | |
| 1149 | /* |
| 1150 | * Create a child but do not activate it. Calling the function again |
| 1151 | * should return the same child. |
| 1152 | */ |
| 1153 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1154 | UCLASS_TEST, &dev1)); |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1155 | ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv), |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 1156 | "test_child", 0, ofnode_null(), &dev1)); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1157 | |
| 1158 | ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST, |
| 1159 | &dev2)); |
| 1160 | ut_asserteq_ptr(dev1, dev2); |
| 1161 | |
| 1162 | ut_assertok(device_probe(dev1)); |
| 1163 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1164 | UCLASS_TEST, &dev2)); |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1168 | DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA); |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1169 | |
| 1170 | /* Make sure all bound devices have a sequence number */ |
| 1171 | static int dm_test_all_have_seq(struct unit_test_state *uts) |
| 1172 | { |
| 1173 | struct udevice *dev; |
| 1174 | struct uclass *uc; |
| 1175 | |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 1176 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1177 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1178 | if (dev->seq_ == -1) |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1179 | printf("Device '%s' has no seq (%d)\n", |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1180 | dev->name, dev->seq_); |
| 1181 | ut_assert(dev->seq_ != -1); |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA); |
Nicolas Saenz Julienne | 892e9b4 | 2021-01-12 13:55:25 +0100 | [diff] [blame] | 1188 | |
Simon Glass | 2697425 | 2021-07-05 16:32:42 -0600 | [diff] [blame] | 1189 | #if CONFIG_IS_ENABLED(DM_DMA) |
Nicolas Saenz Julienne | 892e9b4 | 2021-01-12 13:55:25 +0100 | [diff] [blame] | 1190 | static int dm_test_dma_offset(struct unit_test_state *uts) |
| 1191 | { |
| 1192 | struct udevice *dev; |
| 1193 | ofnode node; |
| 1194 | |
| 1195 | /* Make sure the bus's dma-ranges aren't taken into account here */ |
| 1196 | node = ofnode_path("/mmio-bus@0"); |
| 1197 | ut_assert(ofnode_valid(node)); |
| 1198 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev)); |
| 1199 | ut_asserteq_64(0, dev->dma_offset); |
| 1200 | |
| 1201 | /* Device behind a bus with dma-ranges */ |
| 1202 | node = ofnode_path("/mmio-bus@0/subnode@0"); |
| 1203 | ut_assert(ofnode_valid(node)); |
| 1204 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev)); |
| 1205 | ut_asserteq_64(-0x10000000ULL, dev->dma_offset); |
| 1206 | |
| 1207 | /* This one has no dma-ranges */ |
| 1208 | node = ofnode_path("/mmio-bus@1"); |
| 1209 | ut_assert(ofnode_valid(node)); |
| 1210 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev)); |
| 1211 | node = ofnode_path("/mmio-bus@1/subnode@0"); |
| 1212 | ut_assert(ofnode_valid(node)); |
| 1213 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev)); |
| 1214 | ut_asserteq_64(0, dev->dma_offset); |
| 1215 | |
| 1216 | return 0; |
| 1217 | } |
| 1218 | 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] | 1219 | #endif |