Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 6 | #ifdef CONFIG_SANDBOX |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 8 | #include <os.h> |
| 9 | #endif |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 12 | #include <dm/device.h> |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 14 | #include <dm/test.h> |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 15 | #include <dm/uclass-internal.h> |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 16 | #include <dm/util.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 17 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | #include <test/ut.h> |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 22 | /* Test that we can probe for children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 23 | static int dm_test_bus_children(struct unit_test_state *uts) |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 24 | { |
Jean-Jacques Hiblot | 7387340 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 25 | int num_devices = 9; |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 26 | struct udevice *bus; |
| 27 | struct uclass *uc; |
| 28 | |
| 29 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 30 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 31 | |
| 32 | /* Probe the bus, which should yield 3 more devices */ |
| 33 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 34 | num_devices += 3; |
| 35 | |
| 36 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 37 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 38 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 39 | ut_assert(!dm_check_devices(uts, num_devices)); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 43 | DM_TEST(dm_test_bus_children, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 44 | |
| 45 | /* Test our functions for accessing children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 46 | static int dm_test_bus_children_funcs(struct unit_test_state *uts) |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 47 | { |
| 48 | const void *blob = gd->fdt_blob; |
| 49 | struct udevice *bus, *dev; |
| 50 | int node; |
| 51 | |
| 52 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 53 | |
| 54 | /* device_get_child() */ |
| 55 | ut_assertok(device_get_child(bus, 0, &dev)); |
| 56 | ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev)); |
| 57 | ut_assertok(device_get_child_by_seq(bus, 5, &dev)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 58 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 59 | ut_asserteq_str("c-test@5", dev->name); |
| 60 | |
| 61 | /* Device with sequence number 0 should be accessible */ |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 62 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev)); |
| 63 | ut_assertok(device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 64 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 65 | ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 66 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 67 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 68 | ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 69 | |
| 70 | /* There is no device with sequence number 2 */ |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 71 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev)); |
| 72 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 73 | ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev)); |
| 74 | |
| 75 | /* Looking for something that is not a child */ |
| 76 | node = fdt_path_offset(blob, "/junk"); |
| 77 | ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev)); |
| 78 | node = fdt_path_offset(blob, "/d-test"); |
| 79 | ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev)); |
| 80 | |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 83 | DM_TEST(dm_test_bus_children_funcs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 84 | |
| 85 | static int dm_test_bus_children_of_offset(struct unit_test_state *uts) |
| 86 | { |
| 87 | const void *blob = gd->fdt_blob; |
| 88 | struct udevice *bus, *dev; |
| 89 | int node; |
| 90 | |
| 91 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Simon Glass | ac85552 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 92 | ut_assertnonnull(bus); |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 93 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 94 | /* Find a valid child */ |
| 95 | node = fdt_path_offset(blob, "/some-bus/c-test@1"); |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 96 | ut_assert(node > 0); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 97 | ut_assertok(device_find_child_by_of_offset(bus, node, &dev)); |
Simon Glass | ac85552 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 98 | ut_assertnonnull(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 99 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 100 | ut_assertok(device_get_child_by_of_offset(bus, node, &dev)); |
Simon Glass | ac85552 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 101 | ut_assertnonnull(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 102 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 106 | DM_TEST(dm_test_bus_children_of_offset, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 107 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 108 | |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 109 | /* Test that we can iterate through children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 110 | static int dm_test_bus_children_iterators(struct unit_test_state *uts) |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 111 | { |
| 112 | struct udevice *bus, *dev, *child; |
| 113 | |
| 114 | /* Walk through the children one by one */ |
| 115 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 116 | ut_assertok(device_find_first_child(bus, &dev)); |
| 117 | ut_asserteq_str("c-test@5", dev->name); |
| 118 | ut_assertok(device_find_next_child(&dev)); |
| 119 | ut_asserteq_str("c-test@0", dev->name); |
| 120 | ut_assertok(device_find_next_child(&dev)); |
| 121 | ut_asserteq_str("c-test@1", dev->name); |
| 122 | ut_assertok(device_find_next_child(&dev)); |
| 123 | ut_asserteq_ptr(dev, NULL); |
| 124 | |
| 125 | /* Move to the next child without using device_find_first_child() */ |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 126 | ut_assertok(device_find_child_by_seq(bus, 5, &dev)); |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 127 | ut_asserteq_str("c-test@5", dev->name); |
| 128 | ut_assertok(device_find_next_child(&dev)); |
| 129 | ut_asserteq_str("c-test@0", dev->name); |
| 130 | |
| 131 | /* Try a device with no children */ |
| 132 | ut_assertok(device_find_first_child(dev, &child)); |
| 133 | ut_asserteq_ptr(child, NULL); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | DM_TEST(dm_test_bus_children_iterators, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 138 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 139 | |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 140 | /* Test that the bus can store data about each child */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 141 | static int test_bus_parent_data(struct unit_test_state *uts) |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 142 | { |
| 143 | struct dm_test_parent_data *parent_data; |
| 144 | struct udevice *bus, *dev; |
| 145 | struct uclass *uc; |
| 146 | int value; |
| 147 | |
| 148 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 149 | |
| 150 | /* Check that parent data is allocated */ |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 151 | ut_assertok(device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 152 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 153 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 154 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 155 | ut_assert(NULL != parent_data); |
| 156 | |
| 157 | /* Check that it starts at 0 and goes away when device is removed */ |
| 158 | parent_data->sum += 5; |
| 159 | ut_asserteq(5, parent_data->sum); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 160 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 161 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 162 | |
| 163 | /* Check that we can do this twice */ |
| 164 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 165 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 166 | ut_assert(NULL != parent_data); |
| 167 | parent_data->sum += 5; |
| 168 | ut_asserteq(5, parent_data->sum); |
| 169 | |
| 170 | /* Add parent data to all children */ |
| 171 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 172 | value = 5; |
| 173 | uclass_foreach_dev(dev, uc) { |
| 174 | /* Ignore these if they are not on this bus */ |
| 175 | if (dev->parent != bus) { |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 176 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 177 | continue; |
| 178 | } |
| 179 | ut_assertok(device_probe(dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 180 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 181 | |
| 182 | parent_data->sum = value; |
| 183 | value += 5; |
| 184 | } |
| 185 | |
| 186 | /* Check it is still there */ |
| 187 | value = 5; |
| 188 | uclass_foreach_dev(dev, uc) { |
| 189 | /* Ignore these if they are not on this bus */ |
| 190 | if (dev->parent != bus) |
| 191 | continue; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 192 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 193 | |
| 194 | ut_asserteq(value, parent_data->sum); |
| 195 | value += 5; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 200 | /* Test that the bus can store data about each child */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 201 | static int dm_test_bus_parent_data(struct unit_test_state *uts) |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 202 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 203 | return test_bus_parent_data(uts); |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 204 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 205 | DM_TEST(dm_test_bus_parent_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 206 | |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 207 | /* As above but the size is controlled by the uclass */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 208 | static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts) |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 209 | { |
Simon Glass | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 210 | struct driver *drv; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 211 | struct udevice *bus; |
| 212 | int size; |
| 213 | int ret; |
| 214 | |
| 215 | /* Set the driver size to 0 so that the uclass size is used */ |
| 216 | ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus)); |
Simon Glass | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 217 | drv = (struct driver *)bus->driver; |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 218 | size = drv->per_child_auto; |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 219 | |
| 220 | #ifdef CONFIG_SANDBOX |
| 221 | os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv)); |
| 222 | os_mprotect_allow(drv, sizeof(*drv)); |
| 223 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 224 | bus->uclass->uc_drv->per_child_auto = size; |
| 225 | drv->per_child_auto = 0; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 226 | ret = test_bus_parent_data(uts); |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 227 | if (ret) |
| 228 | return ret; |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 229 | bus->uclass->uc_drv->per_child_auto = 0; |
| 230 | drv->per_child_auto = size; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | DM_TEST(dm_test_bus_parent_data_uclass, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 235 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 236 | |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 237 | /* Test that the bus ops are called when a child is probed/removed */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 238 | static int dm_test_bus_parent_ops(struct unit_test_state *uts) |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 239 | { |
| 240 | struct dm_test_parent_data *parent_data; |
| 241 | struct udevice *bus, *dev; |
| 242 | struct uclass *uc; |
| 243 | |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 244 | testbus_get_clear_removed(); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 245 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 246 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 247 | |
| 248 | uclass_foreach_dev(dev, uc) { |
| 249 | /* Ignore these if they are not on this bus */ |
| 250 | if (dev->parent != bus) |
| 251 | continue; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 252 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 253 | |
| 254 | ut_assertok(device_probe(dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 255 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 256 | ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | uclass_foreach_dev(dev, uc) { |
| 260 | /* Ignore these if they are not on this bus */ |
| 261 | if (dev->parent != bus) |
| 262 | continue; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 263 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 264 | ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 265 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 266 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 267 | ut_asserteq_ptr(testbus_get_clear_removed(), dev); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 268 | } |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 272 | DM_TEST(dm_test_bus_parent_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 273 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 274 | static int test_bus_parent_plat(struct unit_test_state *uts) |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 275 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 276 | struct dm_test_parent_plat *plat; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 277 | struct udevice *bus, *dev; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 278 | |
| 279 | /* Check that the bus has no children */ |
| 280 | ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus)); |
| 281 | device_find_first_child(bus, &dev); |
| 282 | ut_asserteq_ptr(NULL, dev); |
| 283 | |
| 284 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 285 | |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 286 | for (device_find_first_child(bus, &dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 287 | dev; |
| 288 | device_find_next_child(&dev)) { |
| 289 | /* Check that platform data is allocated */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 290 | plat = dev_get_parent_plat(dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 291 | ut_assert(plat != NULL); |
| 292 | |
| 293 | /* |
| 294 | * Check that it is not affected by the device being |
| 295 | * probed/removed |
| 296 | */ |
| 297 | plat->count++; |
| 298 | ut_asserteq(1, plat->count); |
| 299 | device_probe(dev); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 300 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 301 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 302 | ut_asserteq_ptr(plat, dev_get_parent_plat(dev)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 303 | ut_asserteq(1, plat->count); |
| 304 | ut_assertok(device_probe(dev)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 305 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 306 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 307 | |
| 308 | /* Removing the bus should also have no effect (it is still bound) */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 309 | device_remove(bus, DM_REMOVE_NORMAL); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 310 | for (device_find_first_child(bus, &dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 311 | dev; |
| 312 | device_find_next_child(&dev)) { |
| 313 | /* Check that platform data is allocated */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 314 | plat = dev_get_parent_plat(dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 315 | ut_assert(plat != NULL); |
| 316 | ut_asserteq(1, plat->count); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 317 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 318 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 319 | |
| 320 | /* Unbind all the children */ |
| 321 | do { |
| 322 | device_find_first_child(bus, &dev); |
| 323 | if (dev) |
| 324 | device_unbind(dev); |
| 325 | } while (dev); |
| 326 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 327 | /* Now the child plat should be removed and re-added */ |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 328 | device_probe(bus); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 329 | for (device_find_first_child(bus, &dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 330 | dev; |
| 331 | device_find_next_child(&dev)) { |
| 332 | /* Check that platform data is allocated */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 333 | plat = dev_get_parent_plat(dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 334 | ut_assert(plat != NULL); |
| 335 | ut_asserteq(0, plat->count); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 336 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 337 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 341 | |
| 342 | /* Test that the bus can store platform data about each child */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 343 | static int dm_test_bus_parent_plat(struct unit_test_state *uts) |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 344 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 345 | return test_bus_parent_plat(uts); |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 346 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 347 | DM_TEST(dm_test_bus_parent_plat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 348 | |
| 349 | /* As above but the size is controlled by the uclass */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 350 | static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts) |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 351 | { |
| 352 | struct udevice *bus; |
Simon Glass | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 353 | struct driver *drv; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 354 | int size; |
| 355 | int ret; |
| 356 | |
| 357 | /* Set the driver size to 0 so that the uclass size is used */ |
| 358 | ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus)); |
Simon Glass | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 359 | drv = (struct driver *)bus->driver; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 360 | size = drv->per_child_plat_auto; |
Simon Glass | 4e766c2 | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 361 | #ifdef CONFIG_SANDBOX |
| 362 | os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv)); |
| 363 | os_mprotect_allow(drv, sizeof(*drv)); |
| 364 | #endif |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 365 | bus->uclass->uc_drv->per_child_plat_auto = size; |
| 366 | drv->per_child_plat_auto = 0; |
| 367 | ret = test_bus_parent_plat(uts); |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 368 | if (ret) |
| 369 | return ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 370 | bus->uclass->uc_drv->per_child_plat_auto = 0; |
| 371 | drv->per_child_plat_auto = size; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 375 | DM_TEST(dm_test_bus_parent_plat_uclass, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 376 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 377 | |
| 378 | /* Test that the child post_bind method is called */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 379 | static int dm_test_bus_child_post_bind(struct unit_test_state *uts) |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 380 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 381 | struct dm_test_parent_plat *plat; |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 382 | struct udevice *bus, *dev; |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 383 | |
| 384 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 385 | for (device_find_first_child(bus, &dev); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 386 | dev; |
| 387 | device_find_next_child(&dev)) { |
| 388 | /* Check that platform data is allocated */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 389 | plat = dev_get_parent_plat(dev); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 390 | ut_assert(plat != NULL); |
| 391 | ut_asserteq(1, plat->bind_flag); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 392 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 393 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 394 | |
| 395 | return 0; |
| 396 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 397 | DM_TEST(dm_test_bus_child_post_bind, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 398 | |
| 399 | /* Test that the child post_bind method is called */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 400 | static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts) |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 401 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 402 | struct dm_test_parent_plat *plat; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 403 | struct udevice *bus, *dev; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 404 | |
| 405 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 406 | for (device_find_first_child(bus, &dev); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 407 | dev; |
| 408 | device_find_next_child(&dev)) { |
| 409 | /* Check that platform data is allocated */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 410 | plat = dev_get_parent_plat(dev); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 411 | ut_assert(plat != NULL); |
| 412 | ut_asserteq(2, plat->uclass_bind_flag); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 413 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 414 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | DM_TEST(dm_test_bus_child_post_bind_uclass, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 419 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * Test that the bus' uclass' child_pre_probe() is called before the |
| 423 | * device's probe() method |
| 424 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 425 | static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts) |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 426 | { |
| 427 | struct udevice *bus, *dev; |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * See testfdt_drv_probe() which effectively checks that the uclass |
| 431 | * flag is set before that method is called |
| 432 | */ |
| 433 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 434 | for (device_find_first_child(bus, &dev); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 435 | dev; |
| 436 | device_find_next_child(&dev)) { |
| 437 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 438 | |
| 439 | /* Check that things happened in the right order */ |
| 440 | ut_asserteq_ptr(NULL, priv); |
| 441 | ut_assertok(device_probe(dev)); |
| 442 | |
| 443 | priv = dev_get_priv(dev); |
| 444 | ut_assert(priv != NULL); |
| 445 | ut_asserteq(1, priv->uclass_flag); |
| 446 | ut_asserteq(1, priv->uclass_total); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 447 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 448 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | DM_TEST(dm_test_bus_child_pre_probe_uclass, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 453 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Test that the bus' uclass' child_post_probe() is called after the |
| 457 | * device's probe() method |
| 458 | */ |
| 459 | static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts) |
| 460 | { |
| 461 | struct udevice *bus, *dev; |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * See testfdt_drv_probe() which effectively initializes that |
| 465 | * the uclass postp flag is set to a value |
| 466 | */ |
| 467 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 468 | for (device_find_first_child(bus, &dev); |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 469 | dev; |
| 470 | device_find_next_child(&dev)) { |
| 471 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 472 | |
| 473 | /* Check that things happened in the right order */ |
| 474 | ut_asserteq_ptr(NULL, priv); |
| 475 | ut_assertok(device_probe(dev)); |
| 476 | |
| 477 | priv = dev_get_priv(dev); |
| 478 | ut_assert(priv != NULL); |
| 479 | ut_asserteq(0, priv->uclass_postp); |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 480 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 481 | ut_asserteq(3, device_get_child_count(bus)); |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | DM_TEST(dm_test_bus_child_post_probe_uclass, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 486 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |