xref: /openbmc/u-boot/test/dm/bus.c (revision 790f7719)
1 /*
2  * Copyright (c) 2014 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <dm/device-internal.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/uclass-internal.h>
13 #include <dm/ut.h>
14 #include <dm/util.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 struct dm_test_parent_platdata {
19 	int count;
20 	int bind_flag;
21 	int uclass_bind_flag;
22 };
23 
24 enum {
25 	FLAG_CHILD_PROBED	= 10,
26 	FLAG_CHILD_REMOVED	= -7,
27 };
28 
29 static struct dm_test_state *test_state;
30 
31 static int testbus_drv_probe(struct udevice *dev)
32 {
33 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
34 }
35 
36 static int testbus_child_post_bind(struct udevice *dev)
37 {
38 	struct dm_test_parent_platdata *plat;
39 
40 	plat = dev_get_parent_platdata(dev);
41 	plat->bind_flag = 1;
42 	plat->uclass_bind_flag = 2;
43 
44 	return 0;
45 }
46 
47 static int testbus_child_pre_probe(struct udevice *dev)
48 {
49 	struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
50 
51 	parent_data->flag += FLAG_CHILD_PROBED;
52 
53 	return 0;
54 }
55 
56 static int testbus_child_pre_probe_uclass(struct udevice *dev)
57 {
58 	struct dm_test_priv *priv = dev_get_priv(dev);
59 
60 	priv->uclass_flag++;
61 
62 	return 0;
63 }
64 
65 static int testbus_child_post_remove(struct udevice *dev)
66 {
67 	struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
68 	struct dm_test_state *dms = test_state;
69 
70 	parent_data->flag += FLAG_CHILD_REMOVED;
71 	if (dms)
72 		dms->removed = dev;
73 
74 	return 0;
75 }
76 
77 static const struct udevice_id testbus_ids[] = {
78 	{
79 		.compatible = "denx,u-boot-test-bus",
80 		.data = DM_TEST_TYPE_FIRST },
81 	{ }
82 };
83 
84 U_BOOT_DRIVER(testbus_drv) = {
85 	.name	= "testbus_drv",
86 	.of_match	= testbus_ids,
87 	.id	= UCLASS_TEST_BUS,
88 	.probe	= testbus_drv_probe,
89 	.child_post_bind = testbus_child_post_bind,
90 	.priv_auto_alloc_size = sizeof(struct dm_test_priv),
91 	.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
92 	.per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
93 	.per_child_platdata_auto_alloc_size =
94 			sizeof(struct dm_test_parent_platdata),
95 	.child_pre_probe = testbus_child_pre_probe,
96 	.child_post_remove = testbus_child_post_remove,
97 };
98 
99 UCLASS_DRIVER(testbus) = {
100 	.name		= "testbus",
101 	.id		= UCLASS_TEST_BUS,
102 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
103 	.child_pre_probe = testbus_child_pre_probe_uclass,
104 };
105 
106 /* Test that we can probe for children */
107 static int dm_test_bus_children(struct dm_test_state *dms)
108 {
109 	int num_devices = 6;
110 	struct udevice *bus;
111 	struct uclass *uc;
112 
113 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
114 	ut_asserteq(num_devices, list_count_items(&uc->dev_head));
115 
116 	/* Probe the bus, which should yield 3 more devices */
117 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
118 	num_devices += 3;
119 
120 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
121 	ut_asserteq(num_devices, list_count_items(&uc->dev_head));
122 
123 	ut_assert(!dm_check_devices(dms, num_devices));
124 
125 	return 0;
126 }
127 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
128 
129 /* Test our functions for accessing children */
130 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
131 {
132 	const void *blob = gd->fdt_blob;
133 	struct udevice *bus, *dev;
134 	int node;
135 
136 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
137 
138 	/* device_get_child() */
139 	ut_assertok(device_get_child(bus, 0, &dev));
140 	ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
141 	ut_assertok(device_get_child_by_seq(bus, 5, &dev));
142 	ut_assert(dev->flags & DM_FLAG_ACTIVATED);
143 	ut_asserteq_str("c-test@5", dev->name);
144 
145 	/* Device with sequence number 0 should be accessible */
146 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
147 	ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
148 	ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
149 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
150 	ut_assertok(device_get_child_by_seq(bus, 0, &dev));
151 	ut_assert(dev->flags & DM_FLAG_ACTIVATED);
152 
153 	/* There is no device with sequence number 2 */
154 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
155 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
156 	ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
157 
158 	/* Looking for something that is not a child */
159 	node = fdt_path_offset(blob, "/junk");
160 	ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
161 	node = fdt_path_offset(blob, "/d-test");
162 	ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
163 
164 	/* Find a valid child */
165 	node = fdt_path_offset(blob, "/some-bus/c-test@1");
166 	ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
167 	ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
168 	ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
169 	ut_assert(dev->flags & DM_FLAG_ACTIVATED);
170 
171 	return 0;
172 }
173 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
174 
175 /* Test that we can iterate through children */
176 static int dm_test_bus_children_iterators(struct dm_test_state *dms)
177 {
178 	struct udevice *bus, *dev, *child;
179 
180 	/* Walk through the children one by one */
181 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
182 	ut_assertok(device_find_first_child(bus, &dev));
183 	ut_asserteq_str("c-test@5", dev->name);
184 	ut_assertok(device_find_next_child(&dev));
185 	ut_asserteq_str("c-test@0", dev->name);
186 	ut_assertok(device_find_next_child(&dev));
187 	ut_asserteq_str("c-test@1", dev->name);
188 	ut_assertok(device_find_next_child(&dev));
189 	ut_asserteq_ptr(dev, NULL);
190 
191 	/* Move to the next child without using device_find_first_child() */
192 	ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
193 	ut_asserteq_str("c-test@5", dev->name);
194 	ut_assertok(device_find_next_child(&dev));
195 	ut_asserteq_str("c-test@0", dev->name);
196 
197 	/* Try a device with no children */
198 	ut_assertok(device_find_first_child(dev, &child));
199 	ut_asserteq_ptr(child, NULL);
200 
201 	return 0;
202 }
203 DM_TEST(dm_test_bus_children_iterators,
204 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
205 
206 /* Test that the bus can store data about each child */
207 static int test_bus_parent_data(struct dm_test_state *dms)
208 {
209 	struct dm_test_parent_data *parent_data;
210 	struct udevice *bus, *dev;
211 	struct uclass *uc;
212 	int value;
213 
214 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
215 
216 	/* Check that parent data is allocated */
217 	ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
218 	ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
219 	ut_assertok(device_get_child_by_seq(bus, 0, &dev));
220 	parent_data = dev_get_parentdata(dev);
221 	ut_assert(NULL != parent_data);
222 
223 	/* Check that it starts at 0 and goes away when device is removed */
224 	parent_data->sum += 5;
225 	ut_asserteq(5, parent_data->sum);
226 	device_remove(dev);
227 	ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
228 
229 	/* Check that we can do this twice */
230 	ut_assertok(device_get_child_by_seq(bus, 0, &dev));
231 	parent_data = dev_get_parentdata(dev);
232 	ut_assert(NULL != parent_data);
233 	parent_data->sum += 5;
234 	ut_asserteq(5, parent_data->sum);
235 
236 	/* Add parent data to all children */
237 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
238 	value = 5;
239 	uclass_foreach_dev(dev, uc) {
240 		/* Ignore these if they are not on this bus */
241 		if (dev->parent != bus) {
242 			ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
243 			continue;
244 		}
245 		ut_assertok(device_probe(dev));
246 		parent_data = dev_get_parentdata(dev);
247 
248 		parent_data->sum = value;
249 		value += 5;
250 	}
251 
252 	/* Check it is still there */
253 	value = 5;
254 	uclass_foreach_dev(dev, uc) {
255 		/* Ignore these if they are not on this bus */
256 		if (dev->parent != bus)
257 			continue;
258 		parent_data = dev_get_parentdata(dev);
259 
260 		ut_asserteq(value, parent_data->sum);
261 		value += 5;
262 	}
263 
264 	return 0;
265 }
266 /* Test that the bus can store data about each child */
267 static int dm_test_bus_parent_data(struct dm_test_state *dms)
268 {
269 	return test_bus_parent_data(dms);
270 }
271 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
272 
273 /* As above but the size is controlled by the uclass */
274 static int dm_test_bus_parent_data_uclass(struct dm_test_state *dms)
275 {
276 	struct udevice *bus;
277 	int size;
278 	int ret;
279 
280 	/* Set the driver size to 0 so that the uclass size is used */
281 	ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
282 	size = bus->driver->per_child_auto_alloc_size;
283 	bus->uclass->uc_drv->per_child_auto_alloc_size = size;
284 	bus->driver->per_child_auto_alloc_size = 0;
285 	ret = test_bus_parent_data(dms);
286 	if (ret)
287 		return ret;
288 	bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
289 	bus->driver->per_child_auto_alloc_size = size;
290 
291 	return 0;
292 }
293 DM_TEST(dm_test_bus_parent_data_uclass,
294 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
295 
296 /* Test that the bus ops are called when a child is probed/removed */
297 static int dm_test_bus_parent_ops(struct dm_test_state *dms)
298 {
299 	struct dm_test_parent_data *parent_data;
300 	struct udevice *bus, *dev;
301 	struct uclass *uc;
302 
303 	test_state = dms;
304 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
305 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
306 
307 	uclass_foreach_dev(dev, uc) {
308 		/* Ignore these if they are not on this bus */
309 		if (dev->parent != bus)
310 			continue;
311 		ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
312 
313 		ut_assertok(device_probe(dev));
314 		parent_data = dev_get_parentdata(dev);
315 		ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
316 	}
317 
318 	uclass_foreach_dev(dev, uc) {
319 		/* Ignore these if they are not on this bus */
320 		if (dev->parent != bus)
321 			continue;
322 		parent_data = dev_get_parentdata(dev);
323 		ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
324 		ut_assertok(device_remove(dev));
325 		ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
326 		ut_asserteq_ptr(dms->removed, dev);
327 	}
328 	test_state = NULL;
329 
330 	return 0;
331 }
332 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
333 
334 static int test_bus_parent_platdata(struct dm_test_state *dms)
335 {
336 	struct dm_test_parent_platdata *plat;
337 	struct udevice *bus, *dev;
338 	int child_count;
339 
340 	/* Check that the bus has no children */
341 	ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
342 	device_find_first_child(bus, &dev);
343 	ut_asserteq_ptr(NULL, dev);
344 
345 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
346 
347 	for (device_find_first_child(bus, &dev), child_count = 0;
348 	     dev;
349 	     device_find_next_child(&dev)) {
350 		/* Check that platform data is allocated */
351 		plat = dev_get_parent_platdata(dev);
352 		ut_assert(plat != NULL);
353 
354 		/*
355 		 * Check that it is not affected by the device being
356 		 * probed/removed
357 		 */
358 		plat->count++;
359 		ut_asserteq(1, plat->count);
360 		device_probe(dev);
361 		device_remove(dev);
362 
363 		ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
364 		ut_asserteq(1, plat->count);
365 		ut_assertok(device_probe(dev));
366 		child_count++;
367 	}
368 	ut_asserteq(3, child_count);
369 
370 	/* Removing the bus should also have no effect (it is still bound) */
371 	device_remove(bus);
372 	for (device_find_first_child(bus, &dev), child_count = 0;
373 	     dev;
374 	     device_find_next_child(&dev)) {
375 		/* Check that platform data is allocated */
376 		plat = dev_get_parent_platdata(dev);
377 		ut_assert(plat != NULL);
378 		ut_asserteq(1, plat->count);
379 		child_count++;
380 	}
381 	ut_asserteq(3, child_count);
382 
383 	/* Unbind all the children */
384 	do {
385 		device_find_first_child(bus, &dev);
386 		if (dev)
387 			device_unbind(dev);
388 	} while (dev);
389 
390 	/* Now the child platdata should be removed and re-added */
391 	device_probe(bus);
392 	for (device_find_first_child(bus, &dev), child_count = 0;
393 	     dev;
394 	     device_find_next_child(&dev)) {
395 		/* Check that platform data is allocated */
396 		plat = dev_get_parent_platdata(dev);
397 		ut_assert(plat != NULL);
398 		ut_asserteq(0, plat->count);
399 		child_count++;
400 	}
401 	ut_asserteq(3, child_count);
402 
403 	return 0;
404 }
405 
406 /* Test that the bus can store platform data about each child */
407 static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
408 {
409 	return test_bus_parent_platdata(dms);
410 }
411 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
412 
413 /* As above but the size is controlled by the uclass */
414 static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
415 {
416 	struct udevice *bus;
417 	int size;
418 	int ret;
419 
420 	/* Set the driver size to 0 so that the uclass size is used */
421 	ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
422 	size = bus->driver->per_child_platdata_auto_alloc_size;
423 	bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
424 	bus->driver->per_child_platdata_auto_alloc_size = 0;
425 	ret = test_bus_parent_platdata(dms);
426 	if (ret)
427 		return ret;
428 	bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
429 	bus->driver->per_child_platdata_auto_alloc_size = size;
430 
431 	return 0;
432 }
433 DM_TEST(dm_test_bus_parent_platdata_uclass,
434 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
435 
436 /* Test that the child post_bind method is called */
437 static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
438 {
439 	struct dm_test_parent_platdata *plat;
440 	struct udevice *bus, *dev;
441 	int child_count;
442 
443 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
444 	for (device_find_first_child(bus, &dev), child_count = 0;
445 	     dev;
446 	     device_find_next_child(&dev)) {
447 		/* Check that platform data is allocated */
448 		plat = dev_get_parent_platdata(dev);
449 		ut_assert(plat != NULL);
450 		ut_asserteq(1, plat->bind_flag);
451 		child_count++;
452 	}
453 	ut_asserteq(3, child_count);
454 
455 	return 0;
456 }
457 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
458 
459 /* Test that the child post_bind method is called */
460 static int dm_test_bus_child_post_bind_uclass(struct dm_test_state *dms)
461 {
462 	struct dm_test_parent_platdata *plat;
463 	struct udevice *bus, *dev;
464 	int child_count;
465 
466 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
467 	for (device_find_first_child(bus, &dev), child_count = 0;
468 	     dev;
469 	     device_find_next_child(&dev)) {
470 		/* Check that platform data is allocated */
471 		plat = dev_get_parent_platdata(dev);
472 		ut_assert(plat != NULL);
473 		ut_asserteq(2, plat->uclass_bind_flag);
474 		child_count++;
475 	}
476 	ut_asserteq(3, child_count);
477 
478 	return 0;
479 }
480 DM_TEST(dm_test_bus_child_post_bind_uclass,
481 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
482 
483 /*
484  * Test that the bus' uclass' child_pre_probe() is called before the
485  * device's probe() method
486  */
487 static int dm_test_bus_child_pre_probe_uclass(struct dm_test_state *dms)
488 {
489 	struct udevice *bus, *dev;
490 	int child_count;
491 
492 	/*
493 	 * See testfdt_drv_probe() which effectively checks that the uclass
494 	 * flag is set before that method is called
495 	 */
496 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
497 	for (device_find_first_child(bus, &dev), child_count = 0;
498 	     dev;
499 	     device_find_next_child(&dev)) {
500 		struct dm_test_priv *priv = dev_get_priv(dev);
501 
502 		/* Check that things happened in the right order */
503 		ut_asserteq_ptr(NULL, priv);
504 		ut_assertok(device_probe(dev));
505 
506 		priv = dev_get_priv(dev);
507 		ut_assert(priv != NULL);
508 		ut_asserteq(1, priv->uclass_flag);
509 		ut_asserteq(1, priv->uclass_total);
510 		child_count++;
511 	}
512 	ut_asserteq(3, child_count);
513 
514 	return 0;
515 }
516 DM_TEST(dm_test_bus_child_pre_probe_uclass,
517 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
518