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