blob: 166a8427406f7b27b763bd5b289eedbac16f7c70 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass40717422014-07-23 06:55:18 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass40717422014-07-23 06:55:18 -06004 */
5
Simon Glass4e766c22018-10-01 21:12:32 -06006#ifdef CONFIG_SANDBOX
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass4e766c22018-10-01 21:12:32 -06008#include <os.h>
9#endif
Simon Glass40717422014-07-23 06:55:18 -060010#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Lokesh Vutlafa45a082019-09-04 16:01:26 +053012#include <dm/device.h>
Simon Glass60d971b2014-07-23 06:55:20 -060013#include <dm/device-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060014#include <dm/test.h>
Simon Glass11b61732015-01-25 08:27:01 -070015#include <dm/uclass-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060016#include <dm/util.h>
Sughosh Ganu6012c1e2024-07-30 16:41:32 +053017#include <linux/list.h>
Simon Glass75c4d412020-07-19 10:15:37 -060018#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glass40717422014-07-23 06:55:18 -060020
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass40717422014-07-23 06:55:18 -060023/* Test that we can probe for children */
Joe Hershberger3a77be52015-05-20 14:27:27 -050024static int dm_test_bus_children(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -060025{
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +053026 int num_devices = 9;
Simon Glass40717422014-07-23 06:55:18 -060027 struct udevice *bus;
28 struct uclass *uc;
29
30 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
Sughosh Ganu6012c1e2024-07-30 16:41:32 +053031 ut_asserteq(num_devices, list_count_nodes(&uc->dev_head));
Simon Glass40717422014-07-23 06:55:18 -060032
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 Ganu6012c1e2024-07-30 16:41:32 +053038 ut_asserteq(num_devices, list_count_nodes(&uc->dev_head));
Simon Glass40717422014-07-23 06:55:18 -060039
Joe Hershberger3a77be52015-05-20 14:27:27 -050040 ut_assert(!dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -060041
42 return 0;
43}
Simon Glass1a92f832024-08-22 07:57:48 -060044DM_TEST(dm_test_bus_children, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass48d4e292014-07-23 06:55:19 -060045
46/* Test our functions for accessing children */
Joe Hershberger3a77be52015-05-20 14:27:27 -050047static int dm_test_bus_children_funcs(struct unit_test_state *uts)
Simon Glass48d4e292014-07-23 06:55:19 -060048{
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 Glass6211d762020-12-19 10:40:10 -070059 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass48d4e292014-07-23 06:55:19 -060060 ut_asserteq_str("c-test@5", dev->name);
61
62 /* Device with sequence number 0 should be accessible */
Simon Glass07e13382020-12-16 21:20:29 -070063 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev));
64 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glass6211d762020-12-19 10:40:10 -070065 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass07e13382020-12-16 21:20:29 -070066 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -060067 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glass6211d762020-12-19 10:40:10 -070068 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass07e13382020-12-16 21:20:29 -070069 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -060070
71 /* There is no device with sequence number 2 */
Simon Glass07e13382020-12-16 21:20:29 -070072 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
73 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -060074 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 Glass9b4057a2017-05-18 20:09:43 -060082 return 0;
83}
Simon Glass1a92f832024-08-22 07:57:48 -060084DM_TEST(dm_test_bus_children_funcs, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass9b4057a2017-05-18 20:09:43 -060085
86static 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 Glassac855522017-06-07 10:28:44 -060093 ut_assertnonnull(bus);
Simon Glass9b4057a2017-05-18 20:09:43 -060094
Simon Glass48d4e292014-07-23 06:55:19 -060095 /* Find a valid child */
96 node = fdt_path_offset(blob, "/some-bus/c-test@1");
Simon Glass9b4057a2017-05-18 20:09:43 -060097 ut_assert(node > 0);
Simon Glass48d4e292014-07-23 06:55:19 -060098 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -060099 ut_assertnonnull(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700100 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass48d4e292014-07-23 06:55:19 -0600101 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600102 ut_assertnonnull(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700103 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass48d4e292014-07-23 06:55:19 -0600104
105 return 0;
106}
Simon Glass9b4057a2017-05-18 20:09:43 -0600107DM_TEST(dm_test_bus_children_of_offset,
Simon Glass1a92f832024-08-22 07:57:48 -0600108 UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_FLAT_TREE);
Simon Glass60d971b2014-07-23 06:55:20 -0600109
Simon Glass44da7352014-10-13 23:41:49 -0600110/* Test that we can iterate through children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500111static int dm_test_bus_children_iterators(struct unit_test_state *uts)
Simon Glass44da7352014-10-13 23:41:49 -0600112{
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 Glass07e13382020-12-16 21:20:29 -0700127 ut_assertok(device_find_child_by_seq(bus, 5, &dev));
Simon Glass44da7352014-10-13 23:41:49 -0600128 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}
138DM_TEST(dm_test_bus_children_iterators,
Simon Glass1a92f832024-08-22 07:57:48 -0600139 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass44da7352014-10-13 23:41:49 -0600140
Simon Glass60d971b2014-07-23 06:55:20 -0600141/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500142static int test_bus_parent_data(struct unit_test_state *uts)
Simon Glass60d971b2014-07-23 06:55:20 -0600143{
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 Glass07e13382020-12-16 21:20:29 -0700152 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600153 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600154 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600155 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600156 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 Roese80b5bc92017-03-20 12:51:48 +0100161 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassde44acf2015-09-28 23:32:01 -0600162 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600163
164 /* Check that we can do this twice */
165 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600166 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600167 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 Glassde44acf2015-09-28 23:32:01 -0600177 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600178 continue;
179 }
180 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600181 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600182
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 Glassde44acf2015-09-28 23:32:01 -0600193 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600194
195 ut_asserteq(value, parent_data->sum);
196 value += 5;
197 }
198
199 return 0;
200}
Simon Glassc23b4282015-01-25 08:27:06 -0700201/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500202static int dm_test_bus_parent_data(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700203{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500204 return test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700205}
Simon Glass1a92f832024-08-22 07:57:48 -0600206DM_TEST(dm_test_bus_parent_data, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassd45560d2014-07-23 06:55:21 -0600207
Simon Glassc23b4282015-01-25 08:27:06 -0700208/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500209static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700210{
Simon Glass2410d882015-03-25 12:21:51 -0600211 struct driver *drv;
Simon Glassc23b4282015-01-25 08:27:06 -0700212 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 Glass2410d882015-03-25 12:21:51 -0600218 drv = (struct driver *)bus->driver;
Simon Glass8a2b47f2020-12-03 16:55:17 -0700219 size = drv->per_child_auto;
Simon Glass4e766c22018-10-01 21:12:32 -0600220
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 Glass8a2b47f2020-12-03 16:55:17 -0700225 bus->uclass->uc_drv->per_child_auto = size;
226 drv->per_child_auto = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500227 ret = test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700228 if (ret)
229 return ret;
Simon Glass8a2b47f2020-12-03 16:55:17 -0700230 bus->uclass->uc_drv->per_child_auto = 0;
231 drv->per_child_auto = size;
Simon Glassc23b4282015-01-25 08:27:06 -0700232
233 return 0;
234}
235DM_TEST(dm_test_bus_parent_data_uclass,
Simon Glass1a92f832024-08-22 07:57:48 -0600236 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassc23b4282015-01-25 08:27:06 -0700237
Simon Glassd45560d2014-07-23 06:55:21 -0600238/* Test that the bus ops are called when a child is probed/removed */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500239static int dm_test_bus_parent_ops(struct unit_test_state *uts)
Simon Glassd45560d2014-07-23 06:55:21 -0600240{
241 struct dm_test_parent_data *parent_data;
242 struct udevice *bus, *dev;
243 struct uclass *uc;
244
Simon Glass4bf89722020-12-23 08:11:18 -0700245 testbus_get_clear_removed();
Simon Glassd45560d2014-07-23 06:55:21 -0600246 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 Glassde44acf2015-09-28 23:32:01 -0600253 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600254
255 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600256 parent_data = dev_get_parent_priv(dev);
Simon Glass4bf89722020-12-23 08:11:18 -0700257 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
Simon Glassd45560d2014-07-23 06:55:21 -0600258 }
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 Glassde44acf2015-09-28 23:32:01 -0600264 parent_data = dev_get_parent_priv(dev);
Simon Glass4bf89722020-12-23 08:11:18 -0700265 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100266 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassde44acf2015-09-28 23:32:01 -0600267 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass4bf89722020-12-23 08:11:18 -0700268 ut_asserteq_ptr(testbus_get_clear_removed(), dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600269 }
Simon Glassd45560d2014-07-23 06:55:21 -0600270
271 return 0;
272}
Simon Glass1a92f832024-08-22 07:57:48 -0600273DM_TEST(dm_test_bus_parent_ops, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass11b61732015-01-25 08:27:01 -0700274
Simon Glass71fa5b42020-12-03 16:55:18 -0700275static int test_bus_parent_plat(struct unit_test_state *uts)
Simon Glass11b61732015-01-25 08:27:01 -0700276{
Simon Glass71fa5b42020-12-03 16:55:18 -0700277 struct dm_test_parent_plat *plat;
Simon Glass11b61732015-01-25 08:27:01 -0700278 struct udevice *bus, *dev;
Simon Glass11b61732015-01-25 08:27:01 -0700279
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 Vutlafa45a082019-09-04 16:01:26 +0530287 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700288 dev;
289 device_find_next_child(&dev)) {
290 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700291 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700292 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 Roese80b5bc92017-03-20 12:51:48 +0100301 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass11b61732015-01-25 08:27:01 -0700302
Simon Glass71fa5b42020-12-03 16:55:18 -0700303 ut_asserteq_ptr(plat, dev_get_parent_plat(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700304 ut_asserteq(1, plat->count);
305 ut_assertok(device_probe(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700306 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530307 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700308
309 /* Removing the bus should also have no effect (it is still bound) */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100310 device_remove(bus, DM_REMOVE_NORMAL);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530311 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700312 dev;
313 device_find_next_child(&dev)) {
314 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700315 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700316 ut_assert(plat != NULL);
317 ut_asserteq(1, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700318 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530319 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700320
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 Glass71fa5b42020-12-03 16:55:18 -0700328 /* Now the child plat should be removed and re-added */
Simon Glass11b61732015-01-25 08:27:01 -0700329 device_probe(bus);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530330 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700331 dev;
332 device_find_next_child(&dev)) {
333 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700334 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700335 ut_assert(plat != NULL);
336 ut_asserteq(0, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700337 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530338 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700339
340 return 0;
341}
Simon Glass57f95402015-01-25 08:27:02 -0700342
343/* Test that the bus can store platform data about each child */
Simon Glass71fa5b42020-12-03 16:55:18 -0700344static int dm_test_bus_parent_plat(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700345{
Simon Glass71fa5b42020-12-03 16:55:18 -0700346 return test_bus_parent_plat(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700347}
Simon Glass1a92f832024-08-22 07:57:48 -0600348DM_TEST(dm_test_bus_parent_plat, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass57f95402015-01-25 08:27:02 -0700349
350/* As above but the size is controlled by the uclass */
Simon Glass71fa5b42020-12-03 16:55:18 -0700351static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700352{
353 struct udevice *bus;
Simon Glass2410d882015-03-25 12:21:51 -0600354 struct driver *drv;
Simon Glass57f95402015-01-25 08:27:02 -0700355 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 Glass2410d882015-03-25 12:21:51 -0600360 drv = (struct driver *)bus->driver;
Simon Glass71fa5b42020-12-03 16:55:18 -0700361 size = drv->per_child_plat_auto;
Simon Glass4e766c22018-10-01 21:12:32 -0600362#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 Glass71fa5b42020-12-03 16:55:18 -0700366 bus->uclass->uc_drv->per_child_plat_auto = size;
367 drv->per_child_plat_auto = 0;
368 ret = test_bus_parent_plat(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700369 if (ret)
370 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700371 bus->uclass->uc_drv->per_child_plat_auto = 0;
372 drv->per_child_plat_auto = size;
Simon Glass57f95402015-01-25 08:27:02 -0700373
374 return 0;
375}
Simon Glass71fa5b42020-12-03 16:55:18 -0700376DM_TEST(dm_test_bus_parent_plat_uclass,
Simon Glass1a92f832024-08-22 07:57:48 -0600377 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassa4a51a02015-01-25 08:27:03 -0700378
379/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500380static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
Simon Glassa4a51a02015-01-25 08:27:03 -0700381{
Simon Glass71fa5b42020-12-03 16:55:18 -0700382 struct dm_test_parent_plat *plat;
Simon Glassa4a51a02015-01-25 08:27:03 -0700383 struct udevice *bus, *dev;
Simon Glassa4a51a02015-01-25 08:27:03 -0700384
385 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530386 for (device_find_first_child(bus, &dev);
Simon Glassa4a51a02015-01-25 08:27:03 -0700387 dev;
388 device_find_next_child(&dev)) {
389 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700390 plat = dev_get_parent_plat(dev);
Simon Glassa4a51a02015-01-25 08:27:03 -0700391 ut_assert(plat != NULL);
392 ut_asserteq(1, plat->bind_flag);
Simon Glassa4a51a02015-01-25 08:27:03 -0700393 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530394 ut_asserteq(3, device_get_child_count(bus));
Simon Glassa4a51a02015-01-25 08:27:03 -0700395
396 return 0;
397}
Simon Glass1a92f832024-08-22 07:57:48 -0600398DM_TEST(dm_test_bus_child_post_bind, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700399
400/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500401static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700402{
Simon Glass71fa5b42020-12-03 16:55:18 -0700403 struct dm_test_parent_plat *plat;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700404 struct udevice *bus, *dev;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700405
406 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530407 for (device_find_first_child(bus, &dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700408 dev;
409 device_find_next_child(&dev)) {
410 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700411 plat = dev_get_parent_plat(dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700412 ut_assert(plat != NULL);
413 ut_asserteq(2, plat->uclass_bind_flag);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700414 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530415 ut_asserteq(3, device_get_child_count(bus));
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700416
417 return 0;
418}
419DM_TEST(dm_test_bus_child_post_bind_uclass,
Simon Glass1a92f832024-08-22 07:57:48 -0600420 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass5104b982015-01-25 08:27:10 -0700421
422/*
423 * Test that the bus' uclass' child_pre_probe() is called before the
424 * device's probe() method
425 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500426static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
Simon Glass5104b982015-01-25 08:27:10 -0700427{
428 struct udevice *bus, *dev;
Simon Glass5104b982015-01-25 08:27:10 -0700429
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 Vutlafa45a082019-09-04 16:01:26 +0530435 for (device_find_first_child(bus, &dev);
Simon Glass5104b982015-01-25 08:27:10 -0700436 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 Glass5104b982015-01-25 08:27:10 -0700448 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530449 ut_asserteq(3, device_get_child_count(bus));
Simon Glass5104b982015-01-25 08:27:10 -0700450
451 return 0;
452}
453DM_TEST(dm_test_bus_child_pre_probe_uclass,
Simon Glass1a92f832024-08-22 07:57:48 -0600454 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Bin Mengd9bad172018-10-15 02:20:58 -0700455
456/*
457 * Test that the bus' uclass' child_post_probe() is called after the
458 * device's probe() method
459 */
460static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
461{
462 struct udevice *bus, *dev;
Bin Mengd9bad172018-10-15 02:20:58 -0700463
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 Vutlafa45a082019-09-04 16:01:26 +0530469 for (device_find_first_child(bus, &dev);
Bin Mengd9bad172018-10-15 02:20:58 -0700470 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 Mengd9bad172018-10-15 02:20:58 -0700481 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530482 ut_asserteq(3, device_get_child_count(bus));
Bin Mengd9bad172018-10-15 02:20:58 -0700483
484 return 0;
485}
486DM_TEST(dm_test_bus_child_post_probe_uclass,
Simon Glass1a92f832024-08-22 07:57:48 -0600487 UTF_SCAN_PDATA | UTF_SCAN_FDT);