blob: f74c4308439e6ba08d2d615bc5f5f38ffde1e560 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
3 * Tests for the core driver model code
4 *
5 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07006 */
7
8#include <common.h>
9#include <errno.h>
10#include <dm.h>
11#include <fdtdec.h>
12#include <malloc.h>
13#include <dm/device-internal.h>
14#include <dm/root.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <dm/util.h>
16#include <dm/test.h>
17#include <dm/uclass-internal.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070019
20DECLARE_GLOBAL_DATA_PTR;
21
22enum {
23 TEST_INTVAL1 = 0,
24 TEST_INTVAL2 = 3,
25 TEST_INTVAL3 = 6,
26 TEST_INTVAL_MANUAL = 101112,
Simon Glassfef72b72014-07-23 06:55:03 -060027 TEST_INTVAL_PRE_RELOC = 7,
Simon Glassb2c1cac2014-02-26 15:59:21 -070028};
29
30static const struct dm_test_pdata test_pdata[] = {
31 { .ping_add = TEST_INTVAL1, },
32 { .ping_add = TEST_INTVAL2, },
33 { .ping_add = TEST_INTVAL3, },
34};
35
36static const struct dm_test_pdata test_pdata_manual = {
37 .ping_add = TEST_INTVAL_MANUAL,
38};
39
Simon Glassfef72b72014-07-23 06:55:03 -060040static const struct dm_test_pdata test_pdata_pre_reloc = {
41 .ping_add = TEST_INTVAL_PRE_RELOC,
42};
43
Simon Glassb2c1cac2014-02-26 15:59:21 -070044U_BOOT_DEVICE(dm_test_info1) = {
45 .name = "test_drv",
46 .platdata = &test_pdata[0],
47};
48
49U_BOOT_DEVICE(dm_test_info2) = {
50 .name = "test_drv",
51 .platdata = &test_pdata[1],
52};
53
54U_BOOT_DEVICE(dm_test_info3) = {
55 .name = "test_drv",
56 .platdata = &test_pdata[2],
57};
58
59static struct driver_info driver_info_manual = {
60 .name = "test_manual_drv",
61 .platdata = &test_pdata_manual,
62};
63
Simon Glassfef72b72014-07-23 06:55:03 -060064static struct driver_info driver_info_pre_reloc = {
65 .name = "test_pre_reloc_drv",
Tom Rinie75c02d2016-03-20 10:10:28 -040066 .platdata = &test_pdata_pre_reloc,
Simon Glassfef72b72014-07-23 06:55:03 -060067};
68
Stefan Roeseeaffda72017-03-27 11:02:43 +020069static struct driver_info driver_info_act_dma = {
70 .name = "test_act_dma_drv",
71};
72
Joe Hershberger3a77be52015-05-20 14:27:27 -050073void dm_leak_check_start(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060074{
Joe Hershberger3a77be52015-05-20 14:27:27 -050075 uts->start = mallinfo();
76 if (!uts->start.uordblks)
Simon Glass0927a6f2014-10-04 11:29:50 -060077 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
78}
79
Joe Hershberger3a77be52015-05-20 14:27:27 -050080int dm_leak_check_end(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060081{
82 struct mallinfo end;
Simon Glass94d22182015-09-12 08:45:20 -060083 int id, diff;
Simon Glass0927a6f2014-10-04 11:29:50 -060084
85 /* Don't delete the root class, since we started with that */
86 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
87 struct uclass *uc;
88
89 uc = uclass_find(id);
90 if (!uc)
91 continue;
92 ut_assertok(uclass_destroy(uc));
93 }
94
95 end = mallinfo();
Simon Glass94d22182015-09-12 08:45:20 -060096 diff = end.uordblks - uts->start.uordblks;
97 if (diff > 0)
98 printf("Leak: lost %#xd bytes\n", diff);
99 else if (diff < 0)
100 printf("Leak: gained %#xd bytes\n", -diff);
Joe Hershberger3a77be52015-05-20 14:27:27 -0500101 ut_asserteq(uts->start.uordblks, end.uordblks);
Simon Glass0927a6f2014-10-04 11:29:50 -0600102
103 return 0;
104}
105
Simon Glassb2c1cac2014-02-26 15:59:21 -0700106/* Test that binding with platdata occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500107static int dm_test_autobind(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700108{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500109 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200110 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700111
112 /*
113 * We should have a single class (UCLASS_ROOT) and a single root
114 * device with no children.
115 */
116 ut_assert(dms->root);
117 ut_asserteq(1, list_count_items(&gd->uclass_root));
118 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
119 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
120
Simon Glassfef72b72014-07-23 06:55:03 -0600121 ut_assertok(dm_scan_platdata(false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700122
123 /* We should have our test class now at least, plus more children */
124 ut_assert(1 < list_count_items(&gd->uclass_root));
125 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
126
127 /* Our 3 dm_test_infox children should be bound to the test uclass */
128 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
129
130 /* No devices should be probed */
131 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
132 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
133
134 /* Our test driver should have been bound 3 times */
135 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
136
137 return 0;
138}
139DM_TEST(dm_test_autobind, 0);
140
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200141/* Test that binding with uclass platdata allocation occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500142static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200143{
144 struct dm_test_perdev_uc_pdata *uc_pdata;
145 struct udevice *dev;
146 struct uclass *uc;
147
148 ut_assertok(uclass_get(UCLASS_TEST, &uc));
149 ut_assert(uc);
150
151 /**
152 * Test if test uclass driver requires allocation for the uclass
153 * platform data and then check the dev->uclass_platdata pointer.
154 */
155 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
156
157 for (uclass_find_first_device(UCLASS_TEST, &dev);
158 dev;
159 uclass_find_next_device(&dev)) {
160 ut_assert(dev);
161
162 uc_pdata = dev_get_uclass_platdata(dev);
163 ut_assert(uc_pdata);
164 }
165
166 return 0;
167}
168DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
169
170/* Test that binding with uclass platdata setting occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500171static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200172{
173 struct dm_test_perdev_uc_pdata *uc_pdata;
174 struct udevice *dev;
175
176 /**
177 * In the test_postbind() method of test uclass driver, the uclass
178 * platform data should be set to three test int values - test it.
179 */
180 for (uclass_find_first_device(UCLASS_TEST, &dev);
181 dev;
182 uclass_find_next_device(&dev)) {
183 ut_assert(dev);
184
185 uc_pdata = dev_get_uclass_platdata(dev);
186 ut_assert(uc_pdata);
187 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
188 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
189 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
190 }
191
192 return 0;
193}
194DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
195
Simon Glassb2c1cac2014-02-26 15:59:21 -0700196/* Test that autoprobe finds all the expected devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500197static int dm_test_autoprobe(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700198{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500199 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700200 int expected_base_add;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200201 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700202 struct uclass *uc;
203 int i;
204
205 ut_assertok(uclass_get(UCLASS_TEST, &uc));
206 ut_assert(uc);
207
208 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700209 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700210 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
211
212 /* The root device should not be activated until needed */
Simon Glass6d3b3e22014-07-23 06:55:00 -0600213 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700214
215 /*
216 * We should be able to find the three test devices, and they should
217 * all be activated as they are used (lazy activation, required by
218 * U-Boot)
219 */
220 for (i = 0; i < 3; i++) {
221 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
222 ut_assert(dev);
223 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
224 "Driver %d/%s already activated", i, dev->name);
225
226 /* This should activate it */
227 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
228 ut_assert(dev);
229 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
230
231 /* Activating a device should activate the root device */
232 if (!i)
233 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
234 }
235
Simon Glass9c1f3822015-03-05 12:25:22 -0700236 /*
237 * Our 3 dm_test_info children should be passed to pre_probe and
238 * post_probe
239 */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700240 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700241 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700242
243 /* Also we can check the per-device data */
244 expected_base_add = 0;
245 for (i = 0; i < 3; i++) {
246 struct dm_test_uclass_perdev_priv *priv;
247 struct dm_test_pdata *pdata;
248
249 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
250 ut_assert(dev);
251
Simon Glassde0977b2015-03-05 12:25:20 -0700252 priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700253 ut_assert(priv);
254 ut_asserteq(expected_base_add, priv->base_add);
255
256 pdata = dev->platdata;
257 expected_base_add += pdata->ping_add;
258 }
259
260 return 0;
261}
262DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
263
264/* Check that we see the correct platdata in each device */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500265static int dm_test_platdata(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700266{
267 const struct dm_test_pdata *pdata;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200268 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700269 int i;
270
271 for (i = 0; i < 3; i++) {
272 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
273 ut_assert(dev);
274 pdata = dev->platdata;
275 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
276 }
277
278 return 0;
279}
280DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
281
282/* Test that we can bind, probe, remove, unbind a driver */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500283static int dm_test_lifecycle(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700284{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500285 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700286 int op_count[DM_TEST_OP_COUNT];
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200287 struct udevice *dev, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700288 int pingret;
289 int ret;
290
291 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
292
Simon Glassfef72b72014-07-23 06:55:03 -0600293 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700294 &dev));
295 ut_assert(dev);
296 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
297 == op_count[DM_TEST_OP_BIND] + 1);
298 ut_assert(!dev->priv);
299
300 /* Probe the device - it should fail allocating private data */
301 dms->force_fail_alloc = 1;
302 ret = device_probe(dev);
303 ut_assert(ret == -ENOMEM);
304 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
305 == op_count[DM_TEST_OP_PROBE] + 1);
306 ut_assert(!dev->priv);
307
308 /* Try again without the alloc failure */
309 dms->force_fail_alloc = 0;
310 ut_assertok(device_probe(dev));
311 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
312 == op_count[DM_TEST_OP_PROBE] + 2);
313 ut_assert(dev->priv);
314
315 /* This should be device 3 in the uclass */
316 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
317 ut_assert(dev == test_dev);
318
319 /* Try ping */
320 ut_assertok(test_ping(dev, 100, &pingret));
321 ut_assert(pingret == 102);
322
323 /* Now remove device 3 */
324 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100325 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700326 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
327
328 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
329 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
330 ut_assertok(device_unbind(dev));
331 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
332 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
333
334 return 0;
335}
336DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
337
338/* Test that we can bind/unbind and the lists update correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500339static int dm_test_ordering(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700340{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500341 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200342 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700343 int pingret;
344
Simon Glassfef72b72014-07-23 06:55:03 -0600345 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700346 &dev));
347 ut_assert(dev);
348
349 /* Bind two new devices (numbers 4 and 5) */
Simon Glassfef72b72014-07-23 06:55:03 -0600350 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700351 &dev_penultimate));
352 ut_assert(dev_penultimate);
Simon Glassfef72b72014-07-23 06:55:03 -0600353 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700354 &dev_last));
355 ut_assert(dev_last);
356
357 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100358 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700359 ut_assertok(device_unbind(dev));
360
361 /* The device numbering should have shifted down one */
362 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
363 ut_assert(dev_penultimate == test_dev);
364 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
365 ut_assert(dev_last == test_dev);
366
367 /* Add back the original device 3, now in position 5 */
Simon Glassfef72b72014-07-23 06:55:03 -0600368 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
369 &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700370 ut_assert(dev);
371
372 /* Try ping */
373 ut_assertok(test_ping(dev, 100, &pingret));
374 ut_assert(pingret == 102);
375
376 /* Remove 3 and 4 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100377 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700378 ut_assertok(device_unbind(dev_penultimate));
Stefan Roese80b5bc92017-03-20 12:51:48 +0100379 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700380 ut_assertok(device_unbind(dev_last));
381
382 /* Our device should now be in position 3 */
383 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
384 ut_assert(dev == test_dev);
385
386 /* Now remove device 3 */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100387 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700388 ut_assertok(device_unbind(dev));
389
390 return 0;
391}
392DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
393
394/* Check that we can perform operations on a device (do a ping) */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500395int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700396 uint32_t base, struct dm_test_priv *priv)
397{
398 int expected;
399 int pingret;
400
401 /* Getting the child device should allocate platdata / priv */
402 ut_assertok(testfdt_ping(dev, 10, &pingret));
403 ut_assert(dev->priv);
404 ut_assert(dev->platdata);
405
406 expected = 10 + base;
407 ut_asserteq(expected, pingret);
408
409 /* Do another ping */
410 ut_assertok(testfdt_ping(dev, 20, &pingret));
411 expected = 20 + base;
412 ut_asserteq(expected, pingret);
413
414 /* Now check the ping_total */
415 priv = dev->priv;
416 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
417 priv->ping_total);
418
419 return 0;
420}
421
422/* Check that we can perform operations on devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500423static int dm_test_operations(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700424{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200425 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700426 int i;
427
428 /*
429 * Now check that the ping adds are what we expect. This is using the
430 * ping-add property in each node.
431 */
432 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
433 uint32_t base;
434
435 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
436
437 /*
438 * Get the 'reg' property, which tells us what the ping add
439 * should be. We don't use the platdata because we want
440 * to test the code that sets that up (testfdt_drv_probe()).
441 */
442 base = test_pdata[i].ping_add;
443 debug("dev=%d, base=%d\n", i, base);
444
Joe Hershberger3a77be52015-05-20 14:27:27 -0500445 ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700446 }
447
448 return 0;
449}
450DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
451
452/* Remove all drivers and check that things work */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500453static int dm_test_remove(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700454{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200455 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700456 int i;
457
458 for (i = 0; i < 3; i++) {
459 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
460 ut_assert(dev);
461 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
462 "Driver %d/%s not activated", i, dev->name);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100463 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700464 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
465 "Driver %d/%s should have deactivated", i,
466 dev->name);
467 ut_assert(!dev->priv);
468 }
469
470 return 0;
471}
472DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
473
474/* Remove and recreate everything, check for memory leaks */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500475static int dm_test_leak(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700476{
477 int i;
478
479 for (i = 0; i < 2; i++) {
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200480 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700481 int ret;
482 int id;
483
Joe Hershberger3a77be52015-05-20 14:27:27 -0500484 dm_leak_check_start(uts);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700485
Simon Glassfef72b72014-07-23 06:55:03 -0600486 ut_assertok(dm_scan_platdata(false));
487 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700488
489 /* Scanning the uclass is enough to probe all the devices */
490 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
491 for (ret = uclass_first_device(UCLASS_TEST, &dev);
492 dev;
493 ret = uclass_next_device(&dev))
494 ;
495 ut_assertok(ret);
496 }
497
Joe Hershberger3a77be52015-05-20 14:27:27 -0500498 ut_assertok(dm_leak_check_end(uts));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700499 }
500
501 return 0;
502}
503DM_TEST(dm_test_leak, 0);
504
505/* Test uclass init/destroy methods */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500506static int dm_test_uclass(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700507{
508 struct uclass *uc;
509
510 ut_assertok(uclass_get(UCLASS_TEST, &uc));
511 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
512 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
513 ut_assert(uc->priv);
514
515 ut_assertok(uclass_destroy(uc));
516 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
517 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
518
519 return 0;
520}
521DM_TEST(dm_test_uclass, 0);
522
523/**
524 * create_children() - Create children of a parent node
525 *
526 * @dms: Test system state
527 * @parent: Parent device
528 * @count: Number of children to create
529 * @key: Key value to put in first child. Subsequence children
530 * receive an incrementing value
531 * @child: If not NULL, then the child device pointers are written into
532 * this array.
533 * @return 0 if OK, -ve on error
534 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500535static int create_children(struct unit_test_state *uts, struct udevice *parent,
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200536 int count, int key, struct udevice *child[])
Simon Glassb2c1cac2014-02-26 15:59:21 -0700537{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200538 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700539 int i;
540
541 for (i = 0; i < count; i++) {
542 struct dm_test_pdata *pdata;
543
Simon Glassfef72b72014-07-23 06:55:03 -0600544 ut_assertok(device_bind_by_name(parent, false,
545 &driver_info_manual, &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700546 pdata = calloc(1, sizeof(*pdata));
547 pdata->ping_add = key + i;
548 dev->platdata = pdata;
549 if (child)
550 child[i] = dev;
551 }
552
553 return 0;
554}
555
556#define NODE_COUNT 10
557
Joe Hershberger3a77be52015-05-20 14:27:27 -0500558static int dm_test_children(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700559{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500560 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200561 struct udevice *top[NODE_COUNT];
562 struct udevice *child[NODE_COUNT];
563 struct udevice *grandchild[NODE_COUNT];
564 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700565 int total;
566 int ret;
567 int i;
568
569 /* We don't care about the numbering for this test */
570 dms->skip_post_probe = 1;
571
572 ut_assert(NODE_COUNT > 5);
573
574 /* First create 10 top-level children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500575 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700576
577 /* Now a few have their own children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500578 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
579 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700580
581 /* And grandchildren */
582 for (i = 0; i < NODE_COUNT; i++)
Joe Hershberger3a77be52015-05-20 14:27:27 -0500583 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700584 i == 2 ? grandchild : NULL));
585
586 /* Check total number of devices */
587 total = NODE_COUNT * (3 + NODE_COUNT);
588 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
589
590 /* Try probing one of the grandchildren */
591 ut_assertok(uclass_get_device(UCLASS_TEST,
592 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
593 ut_asserteq_ptr(grandchild[0], dev);
594
595 /*
596 * This should have probed the child and top node also, for a total
597 * of 3 nodes.
598 */
599 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
600
601 /* Probe the other grandchildren */
602 for (i = 1; i < NODE_COUNT; i++)
603 ut_assertok(device_probe(grandchild[i]));
604
605 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
606
607 /* Probe everything */
608 for (ret = uclass_first_device(UCLASS_TEST, &dev);
609 dev;
610 ret = uclass_next_device(&dev))
611 ;
612 ut_assertok(ret);
613
614 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
615
616 /* Remove a top-level child and check that the children are removed */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100617 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700618 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
619 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
620
621 /* Try one with grandchildren */
622 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
623 ut_asserteq_ptr(dev, top[5]);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100624 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700625 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
626 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
627
628 /* Try the same with unbind */
629 ut_assertok(device_unbind(top[2]));
630 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
631 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
632
633 /* Try one with grandchildren */
634 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
635 ut_asserteq_ptr(dev, top[6]);
636 ut_assertok(device_unbind(top[5]));
637 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
638 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
639
640 return 0;
641}
642DM_TEST(dm_test_children, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600643
644/* Test that pre-relocation devices work as expected */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500645static int dm_test_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600646{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500647 struct dm_test_state *dms = uts->priv;
Simon Glassfef72b72014-07-23 06:55:03 -0600648 struct udevice *dev;
649
650 /* The normal driver should refuse to bind before relocation */
651 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
652 &driver_info_manual, &dev));
653
654 /* But this one is marked pre-reloc */
655 ut_assertok(device_bind_by_name(dms->root, true,
656 &driver_info_pre_reloc, &dev));
657
658 return 0;
659}
660DM_TEST(dm_test_pre_reloc, 0);
Simon Glassde708672014-07-23 06:55:15 -0600661
Stefan Roeseeaffda72017-03-27 11:02:43 +0200662/*
663 * Test that removal of devices, either via the "normal" device_remove()
664 * API or via the device driver selective flag works as expected
665 */
666static int dm_test_remove_active_dma(struct unit_test_state *uts)
667{
668 struct dm_test_state *dms = uts->priv;
669 struct udevice *dev;
670
671 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
672 &dev));
673 ut_assert(dev);
674
675 /* Probe the device */
676 ut_assertok(device_probe(dev));
677
678 /* Test if device is active right now */
679 ut_asserteq(true, device_active(dev));
680
681 /* Remove the device via selective remove flag */
682 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
683
684 /* Test if device is inactive right now */
685 ut_asserteq(false, device_active(dev));
686
687 /* Probe the device again */
688 ut_assertok(device_probe(dev));
689
690 /* Test if device is active right now */
691 ut_asserteq(true, device_active(dev));
692
693 /* Remove the device via "normal" remove API */
694 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
695
696 /* Test if device is inactive right now */
697 ut_asserteq(false, device_active(dev));
698
699 /*
700 * Test if a device without the active DMA flags is not removed upon
701 * the active DMA remove call
702 */
703 ut_assertok(device_unbind(dev));
704 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
705 &dev));
706 ut_assert(dev);
707
708 /* Probe the device */
709 ut_assertok(device_probe(dev));
710
711 /* Test if device is active right now */
712 ut_asserteq(true, device_active(dev));
713
714 /* Remove the device via selective remove flag */
715 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
716
717 /* Test if device is still active right now */
718 ut_asserteq(true, device_active(dev));
719
720 return 0;
721}
722DM_TEST(dm_test_remove_active_dma, 0);
723
Joe Hershberger3a77be52015-05-20 14:27:27 -0500724static int dm_test_uclass_before_ready(struct unit_test_state *uts)
Simon Glassde708672014-07-23 06:55:15 -0600725{
726 struct uclass *uc;
727
728 ut_assertok(uclass_get(UCLASS_TEST, &uc));
729
Simon Glass51a0eac2015-04-19 07:21:02 -0600730 gd->dm_root = NULL;
731 gd->dm_root_f = NULL;
732 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
733
Simon Glassde708672014-07-23 06:55:15 -0600734 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
735
736 return 0;
737}
Simon Glassde708672014-07-23 06:55:15 -0600738DM_TEST(dm_test_uclass_before_ready, 0);
Simon Glass98fd5d12015-01-25 08:27:04 -0700739
Joe Hershberger3a77be52015-05-20 14:27:27 -0500740static int dm_test_uclass_devices_find(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200741{
742 struct udevice *dev;
743 int ret;
744
745 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
746 dev;
747 ret = uclass_find_next_device(&dev)) {
748 ut_assert(!ret);
749 ut_assert(dev);
750 }
751
Simon Glass0bb44272019-09-25 08:55:55 -0600752 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
Marcel Ziswiler75ec16f2019-02-01 16:01:07 +0100753 ut_assert(!dev);
754
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200755 return 0;
756}
757DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
758
Joe Hershberger3a77be52015-05-20 14:27:27 -0500759static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +0200760{
761 struct udevice *finddev;
762 struct udevice *testdev;
763 int findret, ret;
764
765 /*
766 * For each test device found in fdt like: "a-test", "b-test", etc.,
767 * use its name and try to find it by uclass_find_device_by_name().
768 * Then, on success check if:
769 * - current 'testdev' name is equal to the returned 'finddev' name
770 * - current 'testdev' pointer is equal to the returned 'finddev'
771 *
772 * We assume that, each uclass's device name is unique, so if not, then
773 * this will fail on checking condition: testdev == finddev, since the
774 * uclass_find_device_by_name(), returns the first device by given name.
775 */
776 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
777 testdev;
778 ret = uclass_find_next_device(&testdev)) {
779 ut_assertok(ret);
780 ut_assert(testdev);
781
782 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
783 testdev->name,
784 &finddev);
785
786 ut_assertok(findret);
787 ut_assert(testdev);
788 ut_asserteq_str(testdev->name, finddev->name);
789 ut_asserteq_ptr(testdev, finddev);
790 }
791
792 return 0;
793}
794DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
795
Joe Hershberger3a77be52015-05-20 14:27:27 -0500796static int dm_test_uclass_devices_get(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200797{
798 struct udevice *dev;
799 int ret;
800
801 for (ret = uclass_first_device(UCLASS_TEST, &dev);
802 dev;
803 ret = uclass_next_device(&dev)) {
804 ut_assert(!ret);
805 ut_assert(dev);
806 ut_assert(device_active(dev));
807 }
808
809 return 0;
810}
811DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
812
Joe Hershberger3a77be52015-05-20 14:27:27 -0500813static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +0200814{
815 struct udevice *finddev;
816 struct udevice *testdev;
817 int ret, findret;
818
819 /*
820 * For each test device found in fdt like: "a-test", "b-test", etc.,
821 * use its name and try to get it by uclass_get_device_by_name().
822 * On success check if:
823 * - returned finddev' is active
824 * - current 'testdev' name is equal to the returned 'finddev' name
825 * - current 'testdev' pointer is equal to the returned 'finddev'
826 *
827 * We asserts that the 'testdev' is active on each loop entry, so we
828 * could be sure that the 'finddev' is activated too, but for sure
829 * we check it again.
830 *
831 * We assume that, each uclass's device name is unique, so if not, then
832 * this will fail on checking condition: testdev == finddev, since the
833 * uclass_get_device_by_name(), returns the first device by given name.
834 */
835 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
836 testdev;
837 ret = uclass_next_device(&testdev)) {
838 ut_assertok(ret);
839 ut_assert(testdev);
840 ut_assert(device_active(testdev));
841
842 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
843 testdev->name,
844 &finddev);
845
846 ut_assertok(findret);
847 ut_assert(finddev);
848 ut_assert(device_active(finddev));
849 ut_asserteq_str(testdev->name, finddev->name);
850 ut_asserteq_ptr(testdev, finddev);
851 }
852
853 return 0;
854}
855DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
856
Joe Hershberger3a77be52015-05-20 14:27:27 -0500857static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
Simon Glass98fd5d12015-01-25 08:27:04 -0700858{
859 struct udevice *dev;
860
861 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
862 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
863
864 return 0;
865}
866DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
Simon Glass70e35b42017-12-28 13:14:15 -0700867
868static int dm_test_uclass_names(struct unit_test_state *uts)
869{
870 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
871 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
872
873 return 0;
874}
875DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA);
Simon Glassb775e872018-10-01 12:22:07 -0600876
877static int dm_test_inactive_child(struct unit_test_state *uts)
878{
879 struct dm_test_state *dms = uts->priv;
880 struct udevice *parent, *dev1, *dev2;
881
882 /* Skip the behaviour in test_post_probe() */
883 dms->skip_post_probe = 1;
884
885 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
886
887 /*
888 * Create a child but do not activate it. Calling the function again
889 * should return the same child.
890 */
891 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
892 UCLASS_TEST, &dev1));
893 ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv),
894 "test_child", 0, ofnode_null(), &dev1));
895
896 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
897 &dev2));
898 ut_asserteq_ptr(dev1, dev2);
899
900 ut_assertok(device_probe(dev1));
901 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
902 UCLASS_TEST, &dev2));
903
904 return 0;
905}
906DM_TEST(dm_test_inactive_child, DM_TESTF_SCAN_PDATA);