12e7d35d2SSimon Glass /*
22e7d35d2SSimon Glass * Copyright (c) 2013 Google, Inc
32e7d35d2SSimon Glass *
42e7d35d2SSimon Glass * SPDX-License-Identifier: GPL-2.0+
52e7d35d2SSimon Glass */
62e7d35d2SSimon Glass
72e7d35d2SSimon Glass #include <common.h>
840441e0bSJoe Hershberger #include <command.h>
924b852a7SSimon Glass #include <console.h>
102e7d35d2SSimon Glass #include <dm.h>
112e7d35d2SSimon Glass #include <errno.h>
12756ac0bbSSimon Glass #include <malloc.h>
133884c98cSSimon Glass #include <asm/state.h>
142e7d35d2SSimon Glass #include <dm/test.h>
152e7d35d2SSimon Glass #include <dm/root.h>
162e7d35d2SSimon Glass #include <dm/uclass-internal.h>
17e721b882SJoe Hershberger #include <test/ut.h>
182e7d35d2SSimon Glass
192e7d35d2SSimon Glass DECLARE_GLOBAL_DATA_PTR;
202e7d35d2SSimon Glass
21e721b882SJoe Hershberger struct unit_test_state global_dm_test_state;
22e721b882SJoe Hershberger static struct dm_test_state _global_priv_dm_test_state;
232e7d35d2SSimon Glass
242e7d35d2SSimon Glass /* Get ready for testing */
dm_test_init(struct unit_test_state * uts,bool of_live)25c166c47bSSimon Glass static int dm_test_init(struct unit_test_state *uts, bool of_live)
262e7d35d2SSimon Glass {
27e721b882SJoe Hershberger struct dm_test_state *dms = uts->priv;
28e721b882SJoe Hershberger
292e7d35d2SSimon Glass memset(dms, '\0', sizeof(*dms));
302e7d35d2SSimon Glass gd->dm_root = NULL;
312e7d35d2SSimon Glass memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
3234b744beSSimon Glass state_reset_for_test(state_get_current());
332e7d35d2SSimon Glass
34c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE
35c166c47bSSimon Glass /* Determine whether to make the live tree available */
36c166c47bSSimon Glass gd->of_root = of_live ? uts->of_root : NULL;
37c166c47bSSimon Glass #endif
38c166c47bSSimon Glass ut_assertok(dm_init(of_live));
392e7d35d2SSimon Glass dms->root = dm_root();
402e7d35d2SSimon Glass
412e7d35d2SSimon Glass return 0;
422e7d35d2SSimon Glass }
432e7d35d2SSimon Glass
442e7d35d2SSimon Glass /* Ensure all the test devices are probed */
do_autoprobe(struct unit_test_state * uts)45e721b882SJoe Hershberger static int do_autoprobe(struct unit_test_state *uts)
462e7d35d2SSimon Glass {
4754c5d08aSHeiko Schocher struct udevice *dev;
482e7d35d2SSimon Glass
492e7d35d2SSimon Glass /* Scanning the uclass is enough to probe all the devices */
50*5395ac06SMichal Suchanek for (uclass_first_device(UCLASS_TEST, &dev); dev;
51*5395ac06SMichal Suchanek uclass_next_device(&dev))
522e7d35d2SSimon Glass ;
532e7d35d2SSimon Glass
54*5395ac06SMichal Suchanek return 0;
552e7d35d2SSimon Glass }
562e7d35d2SSimon Glass
dm_test_destroy(struct unit_test_state * uts)57e721b882SJoe Hershberger static int dm_test_destroy(struct unit_test_state *uts)
582e7d35d2SSimon Glass {
592e7d35d2SSimon Glass int id;
602e7d35d2SSimon Glass
612e7d35d2SSimon Glass for (id = 0; id < UCLASS_COUNT; id++) {
622e7d35d2SSimon Glass struct uclass *uc;
632e7d35d2SSimon Glass
642e7d35d2SSimon Glass /*
652e7d35d2SSimon Glass * If the uclass doesn't exist we don't want to create it. So
662e7d35d2SSimon Glass * check that here before we call uclass_find_device()/
672e7d35d2SSimon Glass */
682e7d35d2SSimon Glass uc = uclass_find(id);
692e7d35d2SSimon Glass if (!uc)
702e7d35d2SSimon Glass continue;
712e7d35d2SSimon Glass ut_assertok(uclass_destroy(uc));
722e7d35d2SSimon Glass }
732e7d35d2SSimon Glass
742e7d35d2SSimon Glass return 0;
752e7d35d2SSimon Glass }
762e7d35d2SSimon Glass
dm_do_test(struct unit_test_state * uts,struct unit_test * test,bool of_live)77c166c47bSSimon Glass static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
78c166c47bSSimon Glass bool of_live)
79f86db10cSSimon Glass {
80f86db10cSSimon Glass struct sandbox_state *state = state_get_current();
81801587bdSSimon Glass const char *fname = strrchr(test->file, '/') + 1;
82f86db10cSSimon Glass
83c166c47bSSimon Glass printf("Test: %s: %s%s\n", test->name, fname,
84c166c47bSSimon Glass !of_live ? " (flat tree)" : "");
85c166c47bSSimon Glass ut_assertok(dm_test_init(uts, of_live));
86f86db10cSSimon Glass
87f86db10cSSimon Glass uts->start = mallinfo();
88f86db10cSSimon Glass if (test->flags & DM_TESTF_SCAN_PDATA)
89f86db10cSSimon Glass ut_assertok(dm_scan_platdata(false));
90f86db10cSSimon Glass if (test->flags & DM_TESTF_PROBE_TEST)
91f86db10cSSimon Glass ut_assertok(do_autoprobe(uts));
92f86db10cSSimon Glass if (test->flags & DM_TESTF_SCAN_FDT)
93b048a4dbSPatrice Chotard ut_assertok(dm_extended_scan_fdt(gd->fdt_blob, false));
94f86db10cSSimon Glass
95f86db10cSSimon Glass /*
96f86db10cSSimon Glass * Silence the console and rely on console reocrding to get
97f86db10cSSimon Glass * our output.
98f86db10cSSimon Glass */
99f86db10cSSimon Glass console_record_reset();
100f86db10cSSimon Glass if (!state->show_test_output)
101f86db10cSSimon Glass gd->flags |= GD_FLG_SILENT;
102f86db10cSSimon Glass test->func(uts);
103f86db10cSSimon Glass gd->flags &= ~GD_FLG_SILENT;
104f86db10cSSimon Glass state_set_skip_delays(false);
105f86db10cSSimon Glass
106f86db10cSSimon Glass ut_assertok(dm_test_destroy(uts));
107f86db10cSSimon Glass
108f86db10cSSimon Glass return 0;
109f86db10cSSimon Glass }
110f86db10cSSimon Glass
1116fb2f579SSimon Glass /**
1126fb2f579SSimon Glass * dm_test_run_on_flattree() - Check if we should run a test with flat DT
1136fb2f579SSimon Glass *
1146fb2f579SSimon Glass * This skips long/slow tests where there is not much value in running a flat
1156fb2f579SSimon Glass * DT test in addition to a live DT test.
1166fb2f579SSimon Glass *
1176fb2f579SSimon Glass * @return true to run the given test on the flat device tree
1186fb2f579SSimon Glass */
dm_test_run_on_flattree(struct unit_test * test)1196fb2f579SSimon Glass static bool dm_test_run_on_flattree(struct unit_test *test)
1206fb2f579SSimon Glass {
1216fb2f579SSimon Glass const char *fname = strrchr(test->file, '/') + 1;
1226fb2f579SSimon Glass
1236fb2f579SSimon Glass return !strstr(fname, "video") || strstr(test->name, "video_base");
1246fb2f579SSimon Glass }
1256fb2f579SSimon Glass
dm_test_main(const char * test_name)12640441e0bSJoe Hershberger static int dm_test_main(const char *test_name)
1272e7d35d2SSimon Glass {
128e721b882SJoe Hershberger struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
129e721b882SJoe Hershberger const int n_ents = ll_entry_count(struct unit_test, dm_test);
130e721b882SJoe Hershberger struct unit_test_state *uts = &global_dm_test_state;
131e721b882SJoe Hershberger struct unit_test *test;
132c02790ceSSimon Glass int run_count;
1332e7d35d2SSimon Glass
134c166c47bSSimon Glass uts->priv = &_global_priv_dm_test_state;
13526e1beccSStephen Warren uts->fail_count = 0;
13626e1beccSStephen Warren
1372e7d35d2SSimon Glass /*
1382e7d35d2SSimon Glass * If we have no device tree, or it only has a root node, then these
1392e7d35d2SSimon Glass * tests clearly aren't going to work...
1402e7d35d2SSimon Glass */
1412e7d35d2SSimon Glass if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
1422e7d35d2SSimon Glass puts("Please run with test device tree:\n"
143f64000c3SPrzemyslaw Marczak " ./u-boot -d arch/sandbox/dts/test.dtb\n");
1442e7d35d2SSimon Glass ut_assert(gd->fdt_blob);
1452e7d35d2SSimon Glass }
1462e7d35d2SSimon Glass
14757f54d55SSimon Glass if (!test_name)
1482e7d35d2SSimon Glass printf("Running %d driver model tests\n", n_ents);
1492e7d35d2SSimon Glass
150c02790ceSSimon Glass run_count = 0;
151c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE
152c166c47bSSimon Glass uts->of_root = gd->of_root;
153c166c47bSSimon Glass #endif
1542e7d35d2SSimon Glass for (test = tests; test < tests + n_ents; test++) {
155c02790ceSSimon Glass const char *name = test->name;
1566fb2f579SSimon Glass int runs;
157c02790ceSSimon Glass
158c02790ceSSimon Glass /* All tests have this prefix */
159c02790ceSSimon Glass if (!strncmp(name, "dm_test_", 8))
160c02790ceSSimon Glass name += 8;
161c02790ceSSimon Glass if (test_name && strcmp(test_name, name))
16257f54d55SSimon Glass continue;
1636fb2f579SSimon Glass
1646fb2f579SSimon Glass /* Run with the live tree if possible */
1656fb2f579SSimon Glass runs = 0;
1666fb2f579SSimon Glass if (IS_ENABLED(CONFIG_OF_LIVE)) {
1676fb2f579SSimon Glass if (!(test->flags & DM_TESTF_FLAT_TREE)) {
1686fb2f579SSimon Glass ut_assertok(dm_do_test(uts, test, true));
1696fb2f579SSimon Glass runs++;
1706fb2f579SSimon Glass }
1716fb2f579SSimon Glass }
1726fb2f579SSimon Glass
1736fb2f579SSimon Glass /*
1746fb2f579SSimon Glass * Run with the flat tree if we couldn't run it with live tree,
1756fb2f579SSimon Glass * or it is a core test.
1766fb2f579SSimon Glass */
1776fb2f579SSimon Glass if (!(test->flags & DM_TESTF_LIVE_TREE) &&
1786fb2f579SSimon Glass (!runs || dm_test_run_on_flattree(test))) {
179c166c47bSSimon Glass ut_assertok(dm_do_test(uts, test, false));
1806fb2f579SSimon Glass runs++;
1816fb2f579SSimon Glass }
1826fb2f579SSimon Glass run_count += runs;
1832e7d35d2SSimon Glass }
1842e7d35d2SSimon Glass
185c02790ceSSimon Glass if (test_name && !run_count)
186c02790ceSSimon Glass printf("Test '%s' not found\n", test_name);
187c02790ceSSimon Glass else
188e721b882SJoe Hershberger printf("Failures: %d\n", uts->fail_count);
1892e7d35d2SSimon Glass
190b6227d39SJoe Hershberger gd->dm_root = NULL;
19119c8205eSSimon Glass ut_assertok(dm_init(false));
192b6227d39SJoe Hershberger dm_scan_platdata(false);
193b6227d39SJoe Hershberger dm_scan_fdt(gd->fdt_blob, false);
194b6227d39SJoe Hershberger
1957cccc66aSJoe Hershberger return uts->fail_count ? CMD_RET_FAILURE : 0;
1962e7d35d2SSimon Glass }
19740441e0bSJoe Hershberger
do_ut_dm(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])19840441e0bSJoe Hershberger int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
19940441e0bSJoe Hershberger {
20040441e0bSJoe Hershberger const char *test_name = NULL;
20140441e0bSJoe Hershberger
20240441e0bSJoe Hershberger if (argc > 1)
20340441e0bSJoe Hershberger test_name = argv[1];
20440441e0bSJoe Hershberger
20540441e0bSJoe Hershberger return dm_test_main(test_name);
20640441e0bSJoe Hershberger }
207