xref: /openbmc/u-boot/test/dm/core.c (revision ed09a554)
1 /*
2  * Tests for the core driver model code
3  *
4  * Copyright (c) 2013 Google, Inc
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <errno.h>
11 #include <dm.h>
12 #include <fdtdec.h>
13 #include <malloc.h>
14 #include <dm/device-internal.h>
15 #include <dm/root.h>
16 #include <dm/ut.h>
17 #include <dm/util.h>
18 #include <dm/test.h>
19 #include <dm/uclass-internal.h>
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 enum {
24 	TEST_INTVAL1		= 0,
25 	TEST_INTVAL2		= 3,
26 	TEST_INTVAL3		= 6,
27 	TEST_INTVAL_MANUAL	= 101112,
28 	TEST_INTVAL_PRE_RELOC	= 7,
29 };
30 
31 static const struct dm_test_pdata test_pdata[] = {
32 	{ .ping_add		= TEST_INTVAL1, },
33 	{ .ping_add		= TEST_INTVAL2, },
34 	{ .ping_add		= TEST_INTVAL3, },
35 };
36 
37 static const struct dm_test_pdata test_pdata_manual = {
38 	.ping_add		= TEST_INTVAL_MANUAL,
39 };
40 
41 static const struct dm_test_pdata test_pdata_pre_reloc = {
42 	.ping_add		= TEST_INTVAL_PRE_RELOC,
43 };
44 
45 U_BOOT_DEVICE(dm_test_info1) = {
46 	.name = "test_drv",
47 	.platdata = &test_pdata[0],
48 };
49 
50 U_BOOT_DEVICE(dm_test_info2) = {
51 	.name = "test_drv",
52 	.platdata = &test_pdata[1],
53 };
54 
55 U_BOOT_DEVICE(dm_test_info3) = {
56 	.name = "test_drv",
57 	.platdata = &test_pdata[2],
58 };
59 
60 static struct driver_info driver_info_manual = {
61 	.name = "test_manual_drv",
62 	.platdata = &test_pdata_manual,
63 };
64 
65 static struct driver_info driver_info_pre_reloc = {
66 	.name = "test_pre_reloc_drv",
67 	.platdata = &test_pdata_manual,
68 };
69 
70 void dm_leak_check_start(struct dm_test_state *dms)
71 {
72 	dms->start = mallinfo();
73 	if (!dms->start.uordblks)
74 		puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
75 }
76 
77 int dm_leak_check_end(struct dm_test_state *dms)
78 {
79 	struct mallinfo end;
80 	int id;
81 
82 	/* Don't delete the root class, since we started with that */
83 	for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
84 		struct uclass *uc;
85 
86 		uc = uclass_find(id);
87 		if (!uc)
88 			continue;
89 		ut_assertok(uclass_destroy(uc));
90 	}
91 
92 	end = mallinfo();
93 	ut_asserteq(dms->start.uordblks, end.uordblks);
94 
95 	return 0;
96 }
97 
98 /* Test that binding with platdata occurs correctly */
99 static int dm_test_autobind(struct dm_test_state *dms)
100 {
101 	struct udevice *dev;
102 
103 	/*
104 	 * We should have a single class (UCLASS_ROOT) and a single root
105 	 * device with no children.
106 	 */
107 	ut_assert(dms->root);
108 	ut_asserteq(1, list_count_items(&gd->uclass_root));
109 	ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
110 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
111 
112 	ut_assertok(dm_scan_platdata(false));
113 
114 	/* We should have our test class now at least, plus more children */
115 	ut_assert(1 < list_count_items(&gd->uclass_root));
116 	ut_assert(0 < list_count_items(&gd->dm_root->child_head));
117 
118 	/* Our 3 dm_test_infox children should be bound to the test uclass */
119 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
120 
121 	/* No devices should be probed */
122 	list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
123 		ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
124 
125 	/* Our test driver should have been bound 3 times */
126 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
127 
128 	return 0;
129 }
130 DM_TEST(dm_test_autobind, 0);
131 
132 /* Test that autoprobe finds all the expected devices */
133 static int dm_test_autoprobe(struct dm_test_state *dms)
134 {
135 	int expected_base_add;
136 	struct udevice *dev;
137 	struct uclass *uc;
138 	int i;
139 
140 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
141 	ut_assert(uc);
142 
143 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
144 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
145 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
146 
147 	/* The root device should not be activated until needed */
148 	ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
149 
150 	/*
151 	 * We should be able to find the three test devices, and they should
152 	 * all be activated as they are used (lazy activation, required by
153 	 * U-Boot)
154 	 */
155 	for (i = 0; i < 3; i++) {
156 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
157 		ut_assert(dev);
158 		ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
159 			   "Driver %d/%s already activated", i, dev->name);
160 
161 		/* This should activate it */
162 		ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
163 		ut_assert(dev);
164 		ut_assert(dev->flags & DM_FLAG_ACTIVATED);
165 
166 		/* Activating a device should activate the root device */
167 		if (!i)
168 			ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
169 	}
170 
171 	/*
172 	 * Our 3 dm_test_info children should be passed to pre_probe and
173 	 * post_probe
174 	 */
175 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
176 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
177 
178 	/* Also we can check the per-device data */
179 	expected_base_add = 0;
180 	for (i = 0; i < 3; i++) {
181 		struct dm_test_uclass_perdev_priv *priv;
182 		struct dm_test_pdata *pdata;
183 
184 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
185 		ut_assert(dev);
186 
187 		priv = dev_get_uclass_priv(dev);
188 		ut_assert(priv);
189 		ut_asserteq(expected_base_add, priv->base_add);
190 
191 		pdata = dev->platdata;
192 		expected_base_add += pdata->ping_add;
193 	}
194 
195 	return 0;
196 }
197 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
198 
199 /* Check that we see the correct platdata in each device */
200 static int dm_test_platdata(struct dm_test_state *dms)
201 {
202 	const struct dm_test_pdata *pdata;
203 	struct udevice *dev;
204 	int i;
205 
206 	for (i = 0; i < 3; i++) {
207 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
208 		ut_assert(dev);
209 		pdata = dev->platdata;
210 		ut_assert(pdata->ping_add == test_pdata[i].ping_add);
211 	}
212 
213 	return 0;
214 }
215 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
216 
217 /* Test that we can bind, probe, remove, unbind a driver */
218 static int dm_test_lifecycle(struct dm_test_state *dms)
219 {
220 	int op_count[DM_TEST_OP_COUNT];
221 	struct udevice *dev, *test_dev;
222 	int pingret;
223 	int ret;
224 
225 	memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
226 
227 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
228 					&dev));
229 	ut_assert(dev);
230 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
231 			== op_count[DM_TEST_OP_BIND] + 1);
232 	ut_assert(!dev->priv);
233 
234 	/* Probe the device - it should fail allocating private data */
235 	dms->force_fail_alloc = 1;
236 	ret = device_probe(dev);
237 	ut_assert(ret == -ENOMEM);
238 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
239 			== op_count[DM_TEST_OP_PROBE] + 1);
240 	ut_assert(!dev->priv);
241 
242 	/* Try again without the alloc failure */
243 	dms->force_fail_alloc = 0;
244 	ut_assertok(device_probe(dev));
245 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
246 			== op_count[DM_TEST_OP_PROBE] + 2);
247 	ut_assert(dev->priv);
248 
249 	/* This should be device 3 in the uclass */
250 	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
251 	ut_assert(dev == test_dev);
252 
253 	/* Try ping */
254 	ut_assertok(test_ping(dev, 100, &pingret));
255 	ut_assert(pingret == 102);
256 
257 	/* Now remove device 3 */
258 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
259 	ut_assertok(device_remove(dev));
260 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
261 
262 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
263 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
264 	ut_assertok(device_unbind(dev));
265 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
266 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
267 
268 	return 0;
269 }
270 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
271 
272 /* Test that we can bind/unbind and the lists update correctly */
273 static int dm_test_ordering(struct dm_test_state *dms)
274 {
275 	struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
276 	int pingret;
277 
278 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
279 					&dev));
280 	ut_assert(dev);
281 
282 	/* Bind two new devices (numbers 4 and 5) */
283 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
284 					&dev_penultimate));
285 	ut_assert(dev_penultimate);
286 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
287 					&dev_last));
288 	ut_assert(dev_last);
289 
290 	/* Now remove device 3 */
291 	ut_assertok(device_remove(dev));
292 	ut_assertok(device_unbind(dev));
293 
294 	/* The device numbering should have shifted down one */
295 	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
296 	ut_assert(dev_penultimate == test_dev);
297 	ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
298 	ut_assert(dev_last == test_dev);
299 
300 	/* Add back the original device 3, now in position 5 */
301 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
302 					&dev));
303 	ut_assert(dev);
304 
305 	/* Try ping */
306 	ut_assertok(test_ping(dev, 100, &pingret));
307 	ut_assert(pingret == 102);
308 
309 	/* Remove 3 and 4 */
310 	ut_assertok(device_remove(dev_penultimate));
311 	ut_assertok(device_unbind(dev_penultimate));
312 	ut_assertok(device_remove(dev_last));
313 	ut_assertok(device_unbind(dev_last));
314 
315 	/* Our device should now be in position 3 */
316 	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
317 	ut_assert(dev == test_dev);
318 
319 	/* Now remove device 3 */
320 	ut_assertok(device_remove(dev));
321 	ut_assertok(device_unbind(dev));
322 
323 	return 0;
324 }
325 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
326 
327 /* Check that we can perform operations on a device (do a ping) */
328 int dm_check_operations(struct dm_test_state *dms, struct udevice *dev,
329 			uint32_t base, struct dm_test_priv *priv)
330 {
331 	int expected;
332 	int pingret;
333 
334 	/* Getting the child device should allocate platdata / priv */
335 	ut_assertok(testfdt_ping(dev, 10, &pingret));
336 	ut_assert(dev->priv);
337 	ut_assert(dev->platdata);
338 
339 	expected = 10 + base;
340 	ut_asserteq(expected, pingret);
341 
342 	/* Do another ping */
343 	ut_assertok(testfdt_ping(dev, 20, &pingret));
344 	expected = 20 + base;
345 	ut_asserteq(expected, pingret);
346 
347 	/* Now check the ping_total */
348 	priv = dev->priv;
349 	ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
350 		    priv->ping_total);
351 
352 	return 0;
353 }
354 
355 /* Check that we can perform operations on devices */
356 static int dm_test_operations(struct dm_test_state *dms)
357 {
358 	struct udevice *dev;
359 	int i;
360 
361 	/*
362 	 * Now check that the ping adds are what we expect. This is using the
363 	 * ping-add property in each node.
364 	 */
365 	for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
366 		uint32_t base;
367 
368 		ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
369 
370 		/*
371 		 * Get the 'reg' property, which tells us what the ping add
372 		 * should be. We don't use the platdata because we want
373 		 * to test the code that sets that up (testfdt_drv_probe()).
374 		 */
375 		base = test_pdata[i].ping_add;
376 		debug("dev=%d, base=%d\n", i, base);
377 
378 		ut_assert(!dm_check_operations(dms, dev, base, dev->priv));
379 	}
380 
381 	return 0;
382 }
383 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
384 
385 /* Remove all drivers and check that things work */
386 static int dm_test_remove(struct dm_test_state *dms)
387 {
388 	struct udevice *dev;
389 	int i;
390 
391 	for (i = 0; i < 3; i++) {
392 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
393 		ut_assert(dev);
394 		ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
395 			   "Driver %d/%s not activated", i, dev->name);
396 		ut_assertok(device_remove(dev));
397 		ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
398 			   "Driver %d/%s should have deactivated", i,
399 			   dev->name);
400 		ut_assert(!dev->priv);
401 	}
402 
403 	return 0;
404 }
405 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
406 
407 /* Remove and recreate everything, check for memory leaks */
408 static int dm_test_leak(struct dm_test_state *dms)
409 {
410 	int i;
411 
412 	for (i = 0; i < 2; i++) {
413 		struct udevice *dev;
414 		int ret;
415 		int id;
416 
417 		dm_leak_check_start(dms);
418 
419 		ut_assertok(dm_scan_platdata(false));
420 		ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
421 
422 		/* Scanning the uclass is enough to probe all the devices */
423 		for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
424 			for (ret = uclass_first_device(UCLASS_TEST, &dev);
425 			     dev;
426 			     ret = uclass_next_device(&dev))
427 				;
428 			ut_assertok(ret);
429 		}
430 
431 		ut_assertok(dm_leak_check_end(dms));
432 	}
433 
434 	return 0;
435 }
436 DM_TEST(dm_test_leak, 0);
437 
438 /* Test uclass init/destroy methods */
439 static int dm_test_uclass(struct dm_test_state *dms)
440 {
441 	struct uclass *uc;
442 
443 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
444 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
445 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
446 	ut_assert(uc->priv);
447 
448 	ut_assertok(uclass_destroy(uc));
449 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
450 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
451 
452 	return 0;
453 }
454 DM_TEST(dm_test_uclass, 0);
455 
456 /**
457  * create_children() - Create children of a parent node
458  *
459  * @dms:	Test system state
460  * @parent:	Parent device
461  * @count:	Number of children to create
462  * @key:	Key value to put in first child. Subsequence children
463  *		receive an incrementing value
464  * @child:	If not NULL, then the child device pointers are written into
465  *		this array.
466  * @return 0 if OK, -ve on error
467  */
468 static int create_children(struct dm_test_state *dms, struct udevice *parent,
469 			   int count, int key, struct udevice *child[])
470 {
471 	struct udevice *dev;
472 	int i;
473 
474 	for (i = 0; i < count; i++) {
475 		struct dm_test_pdata *pdata;
476 
477 		ut_assertok(device_bind_by_name(parent, false,
478 						&driver_info_manual, &dev));
479 		pdata = calloc(1, sizeof(*pdata));
480 		pdata->ping_add = key + i;
481 		dev->platdata = pdata;
482 		if (child)
483 			child[i] = dev;
484 	}
485 
486 	return 0;
487 }
488 
489 #define NODE_COUNT	10
490 
491 static int dm_test_children(struct dm_test_state *dms)
492 {
493 	struct udevice *top[NODE_COUNT];
494 	struct udevice *child[NODE_COUNT];
495 	struct udevice *grandchild[NODE_COUNT];
496 	struct udevice *dev;
497 	int total;
498 	int ret;
499 	int i;
500 
501 	/* We don't care about the numbering for this test */
502 	dms->skip_post_probe = 1;
503 
504 	ut_assert(NODE_COUNT > 5);
505 
506 	/* First create 10 top-level children */
507 	ut_assertok(create_children(dms, dms->root, NODE_COUNT, 0, top));
508 
509 	/* Now a few have their own children */
510 	ut_assertok(create_children(dms, top[2], NODE_COUNT, 2, NULL));
511 	ut_assertok(create_children(dms, top[5], NODE_COUNT, 5, child));
512 
513 	/* And grandchildren */
514 	for (i = 0; i < NODE_COUNT; i++)
515 		ut_assertok(create_children(dms, child[i], NODE_COUNT, 50 * i,
516 					    i == 2 ? grandchild : NULL));
517 
518 	/* Check total number of devices */
519 	total = NODE_COUNT * (3 + NODE_COUNT);
520 	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
521 
522 	/* Try probing one of the grandchildren */
523 	ut_assertok(uclass_get_device(UCLASS_TEST,
524 				      NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
525 	ut_asserteq_ptr(grandchild[0], dev);
526 
527 	/*
528 	 * This should have probed the child and top node also, for a total
529 	 * of 3 nodes.
530 	 */
531 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
532 
533 	/* Probe the other grandchildren */
534 	for (i = 1; i < NODE_COUNT; i++)
535 		ut_assertok(device_probe(grandchild[i]));
536 
537 	ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
538 
539 	/* Probe everything */
540 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
541 	     dev;
542 	     ret = uclass_next_device(&dev))
543 		;
544 	ut_assertok(ret);
545 
546 	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
547 
548 	/* Remove a top-level child and check that the children are removed */
549 	ut_assertok(device_remove(top[2]));
550 	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
551 	dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
552 
553 	/* Try one with grandchildren */
554 	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
555 	ut_asserteq_ptr(dev, top[5]);
556 	ut_assertok(device_remove(dev));
557 	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
558 		    dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
559 
560 	/* Try the same with unbind */
561 	ut_assertok(device_unbind(top[2]));
562 	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
563 	dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
564 
565 	/* Try one with grandchildren */
566 	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
567 	ut_asserteq_ptr(dev, top[6]);
568 	ut_assertok(device_unbind(top[5]));
569 	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
570 		    dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
571 
572 	return 0;
573 }
574 DM_TEST(dm_test_children, 0);
575 
576 /* Test that pre-relocation devices work as expected */
577 static int dm_test_pre_reloc(struct dm_test_state *dms)
578 {
579 	struct udevice *dev;
580 
581 	/* The normal driver should refuse to bind before relocation */
582 	ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
583 						&driver_info_manual, &dev));
584 
585 	/* But this one is marked pre-reloc */
586 	ut_assertok(device_bind_by_name(dms->root, true,
587 					&driver_info_pre_reloc, &dev));
588 
589 	return 0;
590 }
591 DM_TEST(dm_test_pre_reloc, 0);
592 
593 static int dm_test_uclass_before_ready(struct dm_test_state *dms)
594 {
595 	struct uclass *uc;
596 
597 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
598 
599 	memset(gd, '\0', sizeof(*gd));
600 	ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
601 
602 	return 0;
603 }
604 
605 DM_TEST(dm_test_uclass_before_ready, 0);
606 
607 static int dm_test_device_get_uclass_id(struct dm_test_state *dms)
608 {
609 	struct udevice *dev;
610 
611 	ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
612 	ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
613 
614 	return 0;
615 }
616 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
617