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> |
Sughosh Ganu | 6012c1e | 2024-07-30 16:41:32 +0530 | [diff] [blame] | 17 | #include <linux/list.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 18 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | #include <test/ut.h> |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 23 | /* Test that we can probe for children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 24 | static int dm_test_bus_children(struct unit_test_state *uts) |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 25 | { |
Jean-Jacques Hiblot | 7387340 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 26 | int num_devices = 9; |
Simon Glass | 4071742 | 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)); |
Sughosh Ganu | 6012c1e | 2024-07-30 16:41:32 +0530 | [diff] [blame] | 31 | ut_asserteq(num_devices, list_count_nodes(&uc->dev_head)); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 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)); |
Sughosh Ganu | 6012c1e | 2024-07-30 16:41:32 +0530 | [diff] [blame] | 38 | ut_asserteq(num_devices, list_count_nodes(&uc->dev_head)); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 39 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 40 | ut_assert(!dm_check_devices(uts, num_devices)); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 41 | |
| 42 | return 0; |
| 43 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 44 | DM_TEST(dm_test_bus_children, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 45 | |
| 46 | /* Test our functions for accessing children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 47 | static int dm_test_bus_children_funcs(struct unit_test_state *uts) |
Simon Glass | 48d4e29 | 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 | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 59 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 48d4e29 | 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 | 07e1338 | 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 | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 65 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 66 | ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 67 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 68 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 69 | ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 70 | |
| 71 | /* There is no device with sequence number 2 */ |
Simon Glass | 07e1338 | 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 | 48d4e29 | 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 | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 82 | return 0; |
| 83 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 84 | DM_TEST(dm_test_bus_children_funcs, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 9b4057a | 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 | ac85552 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 93 | ut_assertnonnull(bus); |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 94 | |
Simon Glass | 48d4e29 | 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 | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 97 | ut_assert(node > 0); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 98 | ut_assertok(device_find_child_by_of_offset(bus, node, &dev)); |
Simon Glass | ac85552 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 99 | ut_assertnonnull(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 100 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 101 | ut_assertok(device_get_child_by_of_offset(bus, node, &dev)); |
Simon Glass | ac85552 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 102 | ut_assertnonnull(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 103 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
Simon Glass | 9b4057a | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 107 | DM_TEST(dm_test_bus_children_of_offset, |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 108 | UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_FLAT_TREE); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 109 | |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 110 | /* Test that we can iterate through children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 111 | static int dm_test_bus_children_iterators(struct unit_test_state *uts) |
Simon Glass | 44da735 | 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 | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 127 | ut_assertok(device_find_child_by_seq(bus, 5, &dev)); |
Simon Glass | 44da735 | 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 | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 139 | UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 140 | |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 141 | /* Test that the bus can store data about each child */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 142 | static int test_bus_parent_data(struct unit_test_state *uts) |
Simon Glass | 60d971b | 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 | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 152 | ut_assertok(device_find_child_by_seq(bus, 0, &dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 153 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 154 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 155 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 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 | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 161 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 162 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 60d971b | 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 | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 166 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 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 | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 177 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 178 | continue; |
| 179 | } |
| 180 | ut_assertok(device_probe(dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 181 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 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 | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 193 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 60d971b | 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 | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 201 | /* Test that the bus can store data about each child */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 202 | static int dm_test_bus_parent_data(struct unit_test_state *uts) |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 203 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 204 | return test_bus_parent_data(uts); |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 205 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 206 | DM_TEST(dm_test_bus_parent_data, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 207 | |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 208 | /* As above but the size is controlled by the uclass */ |
Joe Hershberger | 3a77be5 | 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 | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 210 | { |
Simon Glass | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 211 | struct driver *drv; |
Simon Glass | c23b428 | 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 | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 218 | drv = (struct driver *)bus->driver; |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 219 | size = drv->per_child_auto; |
Simon Glass | 4e766c2 | 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 | 8a2b47f | 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 | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 227 | ret = test_bus_parent_data(uts); |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 228 | if (ret) |
| 229 | return ret; |
Simon Glass | 8a2b47f | 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 | c23b428 | 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 | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 236 | UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 237 | |
Simon Glass | d45560d | 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 | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 239 | static int dm_test_bus_parent_ops(struct unit_test_state *uts) |
Simon Glass | d45560d | 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 | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 245 | testbus_get_clear_removed(); |
Simon Glass | d45560d | 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 | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 253 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 254 | |
| 255 | ut_assertok(device_probe(dev)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 256 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 257 | ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag); |
Simon Glass | d45560d | 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 | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 264 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 265 | ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 266 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 267 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 268 | ut_asserteq_ptr(testbus_get_clear_removed(), dev); |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 269 | } |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 273 | DM_TEST(dm_test_bus_parent_ops, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 274 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 275 | static int test_bus_parent_plat(struct unit_test_state *uts) |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 276 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 277 | struct dm_test_parent_plat *plat; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 278 | struct udevice *bus, *dev; |
Simon Glass | 11b6173 | 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 | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 287 | for (device_find_first_child(bus, &dev); |
Simon Glass | 11b6173 | 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 | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 291 | plat = dev_get_parent_plat(dev); |
Simon Glass | 11b6173 | 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 | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 301 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 302 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 303 | ut_asserteq_ptr(plat, dev_get_parent_plat(dev)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 304 | ut_asserteq(1, plat->count); |
| 305 | ut_assertok(device_probe(dev)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 306 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 307 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 11b6173 | 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 | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 310 | device_remove(bus, DM_REMOVE_NORMAL); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 311 | for (device_find_first_child(bus, &dev); |
Simon Glass | 11b6173 | 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 | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 315 | plat = dev_get_parent_plat(dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 316 | ut_assert(plat != NULL); |
| 317 | ut_asserteq(1, plat->count); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 318 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 319 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 11b6173 | 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 | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 328 | /* Now the child plat should be removed and re-added */ |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 329 | device_probe(bus); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 330 | for (device_find_first_child(bus, &dev); |
Simon Glass | 11b6173 | 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 | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 334 | plat = dev_get_parent_plat(dev); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 335 | ut_assert(plat != NULL); |
| 336 | ut_asserteq(0, plat->count); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 337 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 338 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 342 | |
| 343 | /* Test that the bus can store platform data about each child */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 344 | static int dm_test_bus_parent_plat(struct unit_test_state *uts) |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 345 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 346 | return test_bus_parent_plat(uts); |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 347 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 348 | DM_TEST(dm_test_bus_parent_plat, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 349 | |
| 350 | /* As above but the size is controlled by the uclass */ |
Simon Glass | 71fa5b4 | 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 | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 352 | { |
| 353 | struct udevice *bus; |
Simon Glass | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 354 | struct driver *drv; |
Simon Glass | 57f9540 | 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 | 2410d88 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 360 | drv = (struct driver *)bus->driver; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 361 | size = drv->per_child_plat_auto; |
Simon Glass | 4e766c2 | 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 | 71fa5b4 | 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 | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 369 | if (ret) |
| 370 | return ret; |
Simon Glass | 71fa5b4 | 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 | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 376 | DM_TEST(dm_test_bus_parent_plat_uclass, |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 377 | UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 378 | |
| 379 | /* Test that the child post_bind method is called */ |
Joe Hershberger | 3a77be5 | 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 | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 381 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 382 | struct dm_test_parent_plat *plat; |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 383 | struct udevice *bus, *dev; |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 384 | |
| 385 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 386 | for (device_find_first_child(bus, &dev); |
Simon Glass | a4a51a0 | 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 | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 390 | plat = dev_get_parent_plat(dev); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 391 | ut_assert(plat != NULL); |
| 392 | ut_asserteq(1, plat->bind_flag); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 393 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 394 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 398 | DM_TEST(dm_test_bus_child_post_bind, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 399 | |
| 400 | /* Test that the child post_bind method is called */ |
Joe Hershberger | 3a77be5 | 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 | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 402 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 403 | struct dm_test_parent_plat *plat; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 404 | struct udevice *bus, *dev; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 405 | |
| 406 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 407 | for (device_find_first_child(bus, &dev); |
Simon Glass | f4c9b3e | 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 | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 411 | plat = dev_get_parent_plat(dev); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 412 | ut_assert(plat != NULL); |
| 413 | ut_asserteq(2, plat->uclass_bind_flag); |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 414 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 415 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | f4c9b3e | 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 | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 420 | UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 5104b98 | 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 | 3a77be5 | 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 | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 427 | { |
| 428 | struct udevice *bus, *dev; |
Simon Glass | 5104b98 | 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 | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 435 | for (device_find_first_child(bus, &dev); |
Simon Glass | 5104b98 | 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 | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 448 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 449 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 5104b98 | 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 | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 454 | UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Bin Meng | d9bad17 | 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 | d9bad17 | 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 | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 469 | for (device_find_first_child(bus, &dev); |
Bin Meng | d9bad17 | 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 | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 481 | } |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 482 | ut_asserteq(3, device_get_child_count(bus)); |
Bin Meng | d9bad17 | 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 | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame^] | 487 | UTF_SCAN_PDATA | UTF_SCAN_FDT); |