xref: /openbmc/u-boot/test/dm/test-main.c (revision 96ffe870110be14f9597b53f4b0f35c7b2fee3ac)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22e7d35d2SSimon Glass /*
32e7d35d2SSimon Glass  * Copyright (c) 2013 Google, Inc
42e7d35d2SSimon Glass  */
52e7d35d2SSimon Glass 
62e7d35d2SSimon Glass #include <common.h>
740441e0bSJoe Hershberger #include <command.h>
824b852a7SSimon Glass #include <console.h>
92e7d35d2SSimon Glass #include <dm.h>
102e7d35d2SSimon Glass #include <errno.h>
11756ac0bbSSimon Glass #include <malloc.h>
123884c98cSSimon Glass #include <asm/state.h>
132e7d35d2SSimon Glass #include <dm/test.h>
142e7d35d2SSimon Glass #include <dm/root.h>
152e7d35d2SSimon Glass #include <dm/uclass-internal.h>
16e721b882SJoe Hershberger #include <test/ut.h>
172e7d35d2SSimon Glass 
182e7d35d2SSimon Glass DECLARE_GLOBAL_DATA_PTR;
192e7d35d2SSimon Glass 
20e721b882SJoe Hershberger struct unit_test_state global_dm_test_state;
21e721b882SJoe Hershberger static struct dm_test_state _global_priv_dm_test_state;
222e7d35d2SSimon Glass 
232e7d35d2SSimon Glass /* Get ready for testing */
dm_test_init(struct unit_test_state * uts,bool of_live)24c166c47bSSimon Glass static int dm_test_init(struct unit_test_state *uts, bool of_live)
252e7d35d2SSimon Glass {
26e721b882SJoe Hershberger 	struct dm_test_state *dms = uts->priv;
27e721b882SJoe Hershberger 
282e7d35d2SSimon Glass 	memset(dms, '\0', sizeof(*dms));
292e7d35d2SSimon Glass 	gd->dm_root = NULL;
302e7d35d2SSimon Glass 	memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
3134b744beSSimon Glass 	state_reset_for_test(state_get_current());
322e7d35d2SSimon Glass 
33c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE
34c166c47bSSimon Glass 	/* Determine whether to make the live tree available */
35c166c47bSSimon Glass 	gd->of_root = of_live ? uts->of_root : NULL;
36c166c47bSSimon Glass #endif
37c166c47bSSimon Glass 	ut_assertok(dm_init(of_live));
382e7d35d2SSimon Glass 	dms->root = dm_root();
392e7d35d2SSimon Glass 
402e7d35d2SSimon Glass 	return 0;
412e7d35d2SSimon Glass }
422e7d35d2SSimon Glass 
432e7d35d2SSimon Glass /* Ensure all the test devices are probed */
do_autoprobe(struct unit_test_state * uts)44e721b882SJoe Hershberger static int do_autoprobe(struct unit_test_state *uts)
452e7d35d2SSimon Glass {
4654c5d08aSHeiko Schocher 	struct udevice *dev;
472e7d35d2SSimon Glass 	int ret;
482e7d35d2SSimon Glass 
492e7d35d2SSimon Glass 	/* Scanning the uclass is enough to probe all the devices */
502e7d35d2SSimon Glass 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
512e7d35d2SSimon Glass 	     dev;
522e7d35d2SSimon Glass 	     ret = uclass_next_device(&dev))
532e7d35d2SSimon Glass 		;
542e7d35d2SSimon Glass 
552e7d35d2SSimon Glass 	return ret;
562e7d35d2SSimon Glass }
572e7d35d2SSimon Glass 
dm_test_destroy(struct unit_test_state * uts)58e721b882SJoe Hershberger static int dm_test_destroy(struct unit_test_state *uts)
592e7d35d2SSimon Glass {
602e7d35d2SSimon Glass 	int id;
612e7d35d2SSimon Glass 
622e7d35d2SSimon Glass 	for (id = 0; id < UCLASS_COUNT; id++) {
632e7d35d2SSimon Glass 		struct uclass *uc;
642e7d35d2SSimon Glass 
652e7d35d2SSimon Glass 		/*
662e7d35d2SSimon Glass 		 * If the uclass doesn't exist we don't want to create it. So
672e7d35d2SSimon Glass 		 * check that here before we call uclass_find_device()/
682e7d35d2SSimon Glass 		 */
692e7d35d2SSimon Glass 		uc = uclass_find(id);
702e7d35d2SSimon Glass 		if (!uc)
712e7d35d2SSimon Glass 			continue;
722e7d35d2SSimon Glass 		ut_assertok(uclass_destroy(uc));
732e7d35d2SSimon Glass 	}
742e7d35d2SSimon Glass 
752e7d35d2SSimon Glass 	return 0;
762e7d35d2SSimon Glass }
772e7d35d2SSimon Glass 
dm_do_test(struct unit_test_state * uts,struct unit_test * test,bool of_live)78c166c47bSSimon Glass static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
79c166c47bSSimon Glass 		      bool of_live)
80f86db10cSSimon Glass {
81f86db10cSSimon Glass 	struct sandbox_state *state = state_get_current();
82801587bdSSimon Glass 	const char *fname = strrchr(test->file, '/') + 1;
83f86db10cSSimon Glass 
84c166c47bSSimon Glass 	printf("Test: %s: %s%s\n", test->name, fname,
85c166c47bSSimon Glass 	       !of_live ? " (flat tree)" : "");
86c166c47bSSimon Glass 	ut_assertok(dm_test_init(uts, of_live));
87f86db10cSSimon Glass 
88f86db10cSSimon Glass 	uts->start = mallinfo();
89f86db10cSSimon Glass 	if (test->flags & DM_TESTF_SCAN_PDATA)
90f86db10cSSimon Glass 		ut_assertok(dm_scan_platdata(false));
91f86db10cSSimon Glass 	if (test->flags & DM_TESTF_PROBE_TEST)
92f86db10cSSimon Glass 		ut_assertok(do_autoprobe(uts));
93f86db10cSSimon Glass 	if (test->flags & DM_TESTF_SCAN_FDT)
94ee87a097SPatrice Chotard 		ut_assertok(dm_extended_scan_fdt(gd->fdt_blob, false));
95f86db10cSSimon Glass 
96f86db10cSSimon Glass 	/*
97*96ffe870SMichal Simek 	 * Silence the console and rely on console recording to get
98f86db10cSSimon Glass 	 * our output.
99f86db10cSSimon Glass 	 */
100f86db10cSSimon Glass 	console_record_reset();
101f86db10cSSimon Glass 	if (!state->show_test_output)
102f86db10cSSimon Glass 		gd->flags |= GD_FLG_SILENT;
103f86db10cSSimon Glass 	test->func(uts);
104f86db10cSSimon Glass 	gd->flags &= ~GD_FLG_SILENT;
105f86db10cSSimon Glass 	state_set_skip_delays(false);
106f86db10cSSimon Glass 
107f86db10cSSimon Glass 	ut_assertok(dm_test_destroy(uts));
108f86db10cSSimon Glass 
109f86db10cSSimon Glass 	return 0;
110f86db10cSSimon Glass }
111f86db10cSSimon Glass 
1126fb2f579SSimon Glass /**
1136fb2f579SSimon Glass  * dm_test_run_on_flattree() - Check if we should run a test with flat DT
1146fb2f579SSimon Glass  *
1156fb2f579SSimon Glass  * This skips long/slow tests where there is not much value in running a flat
1166fb2f579SSimon Glass  * DT test in addition to a live DT test.
1176fb2f579SSimon Glass  *
1186fb2f579SSimon Glass  * @return true to run the given test on the flat device tree
1196fb2f579SSimon Glass  */
dm_test_run_on_flattree(struct unit_test * test)1206fb2f579SSimon Glass static bool dm_test_run_on_flattree(struct unit_test *test)
1216fb2f579SSimon Glass {
1226fb2f579SSimon Glass 	const char *fname = strrchr(test->file, '/') + 1;
1236fb2f579SSimon Glass 
1246fb2f579SSimon Glass 	return !strstr(fname, "video") || strstr(test->name, "video_base");
1256fb2f579SSimon Glass }
1266fb2f579SSimon Glass 
dm_test_main(const char * test_name)12740441e0bSJoe Hershberger static int dm_test_main(const char *test_name)
1282e7d35d2SSimon Glass {
129e721b882SJoe Hershberger 	struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
130e721b882SJoe Hershberger 	const int n_ents = ll_entry_count(struct unit_test, dm_test);
131e721b882SJoe Hershberger 	struct unit_test_state *uts = &global_dm_test_state;
132e721b882SJoe Hershberger 	struct unit_test *test;
133c02790ceSSimon Glass 	int run_count;
1342e7d35d2SSimon Glass 
135c166c47bSSimon Glass 	uts->priv = &_global_priv_dm_test_state;
13626e1beccSStephen Warren 	uts->fail_count = 0;
13726e1beccSStephen Warren 
1382e7d35d2SSimon Glass 	/*
1392e7d35d2SSimon Glass 	 * If we have no device tree, or it only has a root node, then these
1402e7d35d2SSimon Glass 	 * tests clearly aren't going to work...
1412e7d35d2SSimon Glass 	 */
1422e7d35d2SSimon Glass 	if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
1432e7d35d2SSimon Glass 		puts("Please run with test device tree:\n"
144f64000c3SPrzemyslaw Marczak 		     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
1452e7d35d2SSimon Glass 		ut_assert(gd->fdt_blob);
1462e7d35d2SSimon Glass 	}
1472e7d35d2SSimon Glass 
14857f54d55SSimon Glass 	if (!test_name)
1492e7d35d2SSimon Glass 		printf("Running %d driver model tests\n", n_ents);
1502e7d35d2SSimon Glass 
151c02790ceSSimon Glass 	run_count = 0;
152c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE
153c166c47bSSimon Glass 	uts->of_root = gd->of_root;
154c166c47bSSimon Glass #endif
1552e7d35d2SSimon Glass 	for (test = tests; test < tests + n_ents; test++) {
156c02790ceSSimon Glass 		const char *name = test->name;
1576fb2f579SSimon Glass 		int runs;
158c02790ceSSimon Glass 
159c02790ceSSimon Glass 		/* All tests have this prefix */
160c02790ceSSimon Glass 		if (!strncmp(name, "dm_test_", 8))
161c02790ceSSimon Glass 			name += 8;
162c02790ceSSimon Glass 		if (test_name && strcmp(test_name, name))
16357f54d55SSimon Glass 			continue;
1646fb2f579SSimon Glass 
1656fb2f579SSimon Glass 		/* Run with the live tree if possible */
1666fb2f579SSimon Glass 		runs = 0;
1676fb2f579SSimon Glass 		if (IS_ENABLED(CONFIG_OF_LIVE)) {
1686fb2f579SSimon Glass 			if (!(test->flags & DM_TESTF_FLAT_TREE)) {
1696fb2f579SSimon Glass 				ut_assertok(dm_do_test(uts, test, true));
1706fb2f579SSimon Glass 				runs++;
1716fb2f579SSimon Glass 			}
1726fb2f579SSimon Glass 		}
1736fb2f579SSimon Glass 
1746fb2f579SSimon Glass 		/*
1756fb2f579SSimon Glass 		 * Run with the flat tree if we couldn't run it with live tree,
1766fb2f579SSimon Glass 		 * or it is a core test.
1776fb2f579SSimon Glass 		 */
1786fb2f579SSimon Glass 		if (!(test->flags & DM_TESTF_LIVE_TREE) &&
1796fb2f579SSimon Glass 		    (!runs || dm_test_run_on_flattree(test))) {
180c166c47bSSimon Glass 			ut_assertok(dm_do_test(uts, test, false));
1816fb2f579SSimon Glass 			runs++;
1826fb2f579SSimon Glass 		}
1836fb2f579SSimon Glass 		run_count += runs;
1842e7d35d2SSimon Glass 	}
1852e7d35d2SSimon Glass 
186c02790ceSSimon Glass 	if (test_name && !run_count)
187c02790ceSSimon Glass 		printf("Test '%s' not found\n", test_name);
188c02790ceSSimon Glass 	else
189e721b882SJoe Hershberger 		printf("Failures: %d\n", uts->fail_count);
1902e7d35d2SSimon Glass 
191b6227d39SJoe Hershberger 	gd->dm_root = NULL;
19219c8205eSSimon Glass 	ut_assertok(dm_init(false));
193b6227d39SJoe Hershberger 	dm_scan_platdata(false);
194b6227d39SJoe Hershberger 	dm_scan_fdt(gd->fdt_blob, false);
195b6227d39SJoe Hershberger 
1967cccc66aSJoe Hershberger 	return uts->fail_count ? CMD_RET_FAILURE : 0;
1972e7d35d2SSimon Glass }
19840441e0bSJoe Hershberger 
do_ut_dm(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])19940441e0bSJoe Hershberger int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
20040441e0bSJoe Hershberger {
20140441e0bSJoe Hershberger 	const char *test_name = NULL;
20240441e0bSJoe Hershberger 
20340441e0bSJoe Hershberger 	if (argc > 1)
20440441e0bSJoe Hershberger 		test_name = argv[1];
20540441e0bSJoe Hershberger 
20640441e0bSJoe Hershberger 	return dm_test_main(test_name);
20740441e0bSJoe Hershberger }
208