]> Git Repo - J-u-boot.git/blame - test/dm/bus.c
common: Drop asm/global_data.h from common header
[J-u-boot.git] / test / dm / bus.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
1ca7e206
SG
2/*
3 * Copyright (c) 2014 Google, Inc
1ca7e206
SG
4 */
5
6#include <common.h>
9f8037ea 7#ifdef CONFIG_SANDBOX
f7ae49fc 8#include <log.h>
9f8037ea
SG
9#include <os.h>
10#endif
1ca7e206 11#include <dm.h>
401d1c4f 12#include <asm/global_data.h>
240b9320 13#include <dm/device.h>
e59f458d 14#include <dm/device-internal.h>
1ca7e206 15#include <dm/test.h>
cdc133bd 16#include <dm/uclass-internal.h>
1ca7e206 17#include <dm/util.h>
0e1fad43 18#include <test/test.h>
e721b882 19#include <test/ut.h>
1ca7e206
SG
20
21DECLARE_GLOBAL_DATA_PTR;
22
1ca7e206 23/* Test that we can probe for children */
e721b882 24static int dm_test_bus_children(struct unit_test_state *uts)
1ca7e206 25{
88e6a60e 26 int num_devices = 9;
1ca7e206
SG
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
e721b882 40 ut_assert(!dm_check_devices(uts, num_devices));
1ca7e206
SG
41
42 return 0;
43}
e180c2b1 44DM_TEST(dm_test_bus_children, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
997c87bb
SG
45
46/* Test our functions for accessing children */
e721b882 47static int dm_test_bus_children_funcs(struct unit_test_state *uts)
997c87bb
SG
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));
73466df3 59 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
997c87bb
SG
60 ut_asserteq_str("c-test@5", dev->name);
61
62 /* Device with sequence number 0 should be accessible */
99175919
SG
63 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev));
64 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
73466df3 65 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
99175919 66 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
997c87bb 67 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
73466df3 68 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
99175919 69 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
997c87bb
SG
70
71 /* There is no device with sequence number 2 */
99175919
SG
72 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
73 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
997c87bb
SG
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
298afb52
SG
82 return 0;
83}
e180c2b1 84DM_TEST(dm_test_bus_children_funcs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
298afb52
SG
85
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));
4f414d39 93 ut_assertnonnull(bus);
298afb52 94
997c87bb
SG
95 /* Find a valid child */
96 node = fdt_path_offset(blob, "/some-bus/c-test@1");
298afb52 97 ut_assert(node > 0);
997c87bb 98 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
4f414d39 99 ut_assertnonnull(dev);
73466df3 100 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
997c87bb 101 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
4f414d39 102 ut_assertnonnull(dev);
73466df3 103 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
997c87bb
SG
104
105 return 0;
106}
298afb52 107DM_TEST(dm_test_bus_children_of_offset,
e180c2b1 108 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
e59f458d 109
a8981d4f 110/* Test that we can iterate through children */
e721b882 111static int dm_test_bus_children_iterators(struct unit_test_state *uts)
a8981d4f
SG
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() */
99175919 127 ut_assertok(device_find_child_by_seq(bus, 5, &dev));
a8981d4f
SG
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}
138DM_TEST(dm_test_bus_children_iterators,
e180c2b1 139 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
a8981d4f 140
e59f458d 141/* Test that the bus can store data about each child */
e721b882 142static int test_bus_parent_data(struct unit_test_state *uts)
e59f458d
SG
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 */
99175919 152 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
bcbe3d15 153 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
e59f458d 154 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
bcbe3d15 155 parent_data = dev_get_parent_priv(dev);
e59f458d
SG
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);
706865af 161 device_remove(dev, DM_REMOVE_NORMAL);
bcbe3d15 162 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
e59f458d
SG
163
164 /* Check that we can do this twice */
165 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
bcbe3d15 166 parent_data = dev_get_parent_priv(dev);
e59f458d
SG
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) {
bcbe3d15 177 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
e59f458d
SG
178 continue;
179 }
180 ut_assertok(device_probe(dev));
bcbe3d15 181 parent_data = dev_get_parent_priv(dev);
e59f458d
SG
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;
bcbe3d15 193 parent_data = dev_get_parent_priv(dev);
e59f458d
SG
194
195 ut_asserteq(value, parent_data->sum);
196 value += 5;
197 }
198
199 return 0;
200}
dac8db2c 201/* Test that the bus can store data about each child */
e721b882 202static int dm_test_bus_parent_data(struct unit_test_state *uts)
dac8db2c 203{
e721b882 204 return test_bus_parent_data(uts);
dac8db2c 205}
e180c2b1 206DM_TEST(dm_test_bus_parent_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
a327dee0 207
dac8db2c 208/* As above but the size is controlled by the uclass */
e721b882 209static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
dac8db2c 210{
e23eb614 211 struct driver *drv;
dac8db2c
SG
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));
e23eb614 218 drv = (struct driver *)bus->driver;
41575d8e 219 size = drv->per_child_auto;
9f8037ea
SG
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
41575d8e
SG
225 bus->uclass->uc_drv->per_child_auto = size;
226 drv->per_child_auto = 0;
e721b882 227 ret = test_bus_parent_data(uts);
dac8db2c
SG
228 if (ret)
229 return ret;
41575d8e
SG
230 bus->uclass->uc_drv->per_child_auto = 0;
231 drv->per_child_auto = size;
dac8db2c
SG
232
233 return 0;
234}
235DM_TEST(dm_test_bus_parent_data_uclass,
e180c2b1 236 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
dac8db2c 237
a327dee0 238/* Test that the bus ops are called when a child is probed/removed */
e721b882 239static int dm_test_bus_parent_ops(struct unit_test_state *uts)
a327dee0
SG
240{
241 struct dm_test_parent_data *parent_data;
242 struct udevice *bus, *dev;
243 struct uclass *uc;
244
079ac595 245 testbus_get_clear_removed();
a327dee0
SG
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;
bcbe3d15 253 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
a327dee0
SG
254
255 ut_assertok(device_probe(dev));
bcbe3d15 256 parent_data = dev_get_parent_priv(dev);
079ac595 257 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
a327dee0
SG
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;
bcbe3d15 264 parent_data = dev_get_parent_priv(dev);
079ac595 265 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
706865af 266 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
bcbe3d15 267 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
079ac595 268 ut_asserteq_ptr(testbus_get_clear_removed(), dev);
a327dee0 269 }
a327dee0
SG
270
271 return 0;
272}
e180c2b1 273DM_TEST(dm_test_bus_parent_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
cdc133bd 274
caa4daa2 275static int test_bus_parent_plat(struct unit_test_state *uts)
cdc133bd 276{
caa4daa2 277 struct dm_test_parent_plat *plat;
cdc133bd 278 struct udevice *bus, *dev;
cdc133bd
SG
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
240b9320 287 for (device_find_first_child(bus, &dev);
cdc133bd
SG
288 dev;
289 device_find_next_child(&dev)) {
290 /* Check that platform data is allocated */
caa4daa2 291 plat = dev_get_parent_plat(dev);
cdc133bd
SG
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);
706865af 301 device_remove(dev, DM_REMOVE_NORMAL);
cdc133bd 302
caa4daa2 303 ut_asserteq_ptr(plat, dev_get_parent_plat(dev));
cdc133bd
SG
304 ut_asserteq(1, plat->count);
305 ut_assertok(device_probe(dev));
cdc133bd 306 }
240b9320 307 ut_asserteq(3, device_get_child_count(bus));
cdc133bd
SG
308
309 /* Removing the bus should also have no effect (it is still bound) */
706865af 310 device_remove(bus, DM_REMOVE_NORMAL);
240b9320 311 for (device_find_first_child(bus, &dev);
cdc133bd
SG
312 dev;
313 device_find_next_child(&dev)) {
314 /* Check that platform data is allocated */
caa4daa2 315 plat = dev_get_parent_plat(dev);
cdc133bd
SG
316 ut_assert(plat != NULL);
317 ut_asserteq(1, plat->count);
cdc133bd 318 }
240b9320 319 ut_asserteq(3, device_get_child_count(bus));
cdc133bd
SG
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
caa4daa2 328 /* Now the child plat should be removed and re-added */
cdc133bd 329 device_probe(bus);
240b9320 330 for (device_find_first_child(bus, &dev);
cdc133bd
SG
331 dev;
332 device_find_next_child(&dev)) {
333 /* Check that platform data is allocated */
caa4daa2 334 plat = dev_get_parent_plat(dev);
cdc133bd
SG
335 ut_assert(plat != NULL);
336 ut_asserteq(0, plat->count);
cdc133bd 337 }
240b9320 338 ut_asserteq(3, device_get_child_count(bus));
cdc133bd
SG
339
340 return 0;
341}
ba8da9dc
SG
342
343/* Test that the bus can store platform data about each child */
caa4daa2 344static int dm_test_bus_parent_plat(struct unit_test_state *uts)
ba8da9dc 345{
caa4daa2 346 return test_bus_parent_plat(uts);
ba8da9dc 347}
caa4daa2 348DM_TEST(dm_test_bus_parent_plat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
ba8da9dc
SG
349
350/* As above but the size is controlled by the uclass */
caa4daa2 351static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts)
ba8da9dc
SG
352{
353 struct udevice *bus;
e23eb614 354 struct driver *drv;
ba8da9dc
SG
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));
e23eb614 360 drv = (struct driver *)bus->driver;
caa4daa2 361 size = drv->per_child_plat_auto;
9f8037ea
SG
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
caa4daa2
SG
366 bus->uclass->uc_drv->per_child_plat_auto = size;
367 drv->per_child_plat_auto = 0;
368 ret = test_bus_parent_plat(uts);
ba8da9dc
SG
369 if (ret)
370 return ret;
caa4daa2
SG
371 bus->uclass->uc_drv->per_child_plat_auto = 0;
372 drv->per_child_plat_auto = size;
ba8da9dc
SG
373
374 return 0;
375}
caa4daa2 376DM_TEST(dm_test_bus_parent_plat_uclass,
e180c2b1 377 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
0118ce79
SG
378
379/* Test that the child post_bind method is called */
e721b882 380static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
0118ce79 381{
caa4daa2 382 struct dm_test_parent_plat *plat;
0118ce79 383 struct udevice *bus, *dev;
0118ce79
SG
384
385 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
240b9320 386 for (device_find_first_child(bus, &dev);
0118ce79
SG
387 dev;
388 device_find_next_child(&dev)) {
389 /* Check that platform data is allocated */
caa4daa2 390 plat = dev_get_parent_plat(dev);
0118ce79
SG
391 ut_assert(plat != NULL);
392 ut_asserteq(1, plat->bind_flag);
0118ce79 393 }
240b9320 394 ut_asserteq(3, device_get_child_count(bus));
0118ce79
SG
395
396 return 0;
397}
e180c2b1 398DM_TEST(dm_test_bus_child_post_bind, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
081f2fcb
SG
399
400/* Test that the child post_bind method is called */
e721b882 401static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
081f2fcb 402{
caa4daa2 403 struct dm_test_parent_plat *plat;
081f2fcb 404 struct udevice *bus, *dev;
081f2fcb
SG
405
406 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
240b9320 407 for (device_find_first_child(bus, &dev);
081f2fcb
SG
408 dev;
409 device_find_next_child(&dev)) {
410 /* Check that platform data is allocated */
caa4daa2 411 plat = dev_get_parent_plat(dev);
081f2fcb
SG
412 ut_assert(plat != NULL);
413 ut_asserteq(2, plat->uclass_bind_flag);
081f2fcb 414 }
240b9320 415 ut_asserteq(3, device_get_child_count(bus));
081f2fcb
SG
416
417 return 0;
418}
419DM_TEST(dm_test_bus_child_post_bind_uclass,
e180c2b1 420 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
83c7e434
SG
421
422/*
423 * Test that the bus' uclass' child_pre_probe() is called before the
424 * device's probe() method
425 */
e721b882 426static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
83c7e434
SG
427{
428 struct udevice *bus, *dev;
83c7e434
SG
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));
240b9320 435 for (device_find_first_child(bus, &dev);
83c7e434
SG
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);
83c7e434 448 }
240b9320 449 ut_asserteq(3, device_get_child_count(bus));
83c7e434
SG
450
451 return 0;
452}
453DM_TEST(dm_test_bus_child_pre_probe_uclass,
e180c2b1 454 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
d92878aa
BM
455
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;
d92878aa
BM
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));
240b9320 469 for (device_find_first_child(bus, &dev);
d92878aa
BM
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);
d92878aa 481 }
240b9320 482 ut_asserteq(3, device_get_child_count(bus));
d92878aa
BM
483
484 return 0;
485}
486DM_TEST(dm_test_bus_child_post_probe_uclass,
e180c2b1 487 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
This page took 0.425841 seconds and 4 git commands to generate.