69458d62c869c9028810ce534b8d0f1b34baf334
[platform/kernel/u-boot.git] / test / dm / test-main.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <console.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <asm/state.h>
14 #include <dm/test.h>
15 #include <dm/root.h>
16 #include <dm/uclass-internal.h>
17 #include <test/test.h>
18 #include <test/test.h>
19 #include <test/ut.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 struct unit_test_state global_dm_test_state;
24 static struct dm_test_state _global_priv_dm_test_state;
25
26 /* Get ready for testing */
27 static int dm_test_init(struct unit_test_state *uts, bool of_live)
28 {
29         struct dm_test_state *dms = uts->priv;
30
31         memset(dms, '\0', sizeof(*dms));
32         gd->dm_root = NULL;
33         if (!CONFIG_IS_ENABLED(OF_PLATDATA))
34                 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
35         state_reset_for_test(state_get_current());
36
37         /* Determine whether to make the live tree available */
38         gd_set_of_root(of_live ? uts->of_root : NULL);
39         ut_assertok(dm_init(of_live));
40         dms->root = dm_root();
41
42         return 0;
43 }
44
45 /* Ensure all the test devices are probed */
46 static int do_autoprobe(struct unit_test_state *uts)
47 {
48         struct udevice *dev;
49         int ret;
50
51         /* Scanning the uclass is enough to probe all the devices */
52         for (ret = uclass_first_device(UCLASS_TEST, &dev);
53              dev;
54              ret = uclass_next_device(&dev))
55                 ;
56
57         return ret;
58 }
59
60 static int dm_test_destroy(struct unit_test_state *uts)
61 {
62         int id;
63
64         for (id = 0; id < UCLASS_COUNT; id++) {
65                 struct uclass *uc;
66
67                 /*
68                  * If the uclass doesn't exist we don't want to create it. So
69                  * check that here before we call uclass_find_device().
70                  */
71                 uc = uclass_find(id);
72                 if (!uc)
73                         continue;
74                 ut_assertok(uclass_destroy(uc));
75         }
76
77         return 0;
78 }
79
80 static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
81                       bool of_live)
82 {
83         struct sandbox_state *state = state_get_current();
84         const char *fname = strrchr(test->file, '/') + 1;
85
86         printf("Test: %s: %s%s\n", test->name, fname,
87                !of_live ? " (flat tree)" : "");
88         ut_assertok(dm_test_init(uts, of_live));
89
90         uts->start = mallinfo();
91         if (test->flags & UT_TESTF_SCAN_PDATA)
92                 ut_assertok(dm_scan_plat(false));
93         if (test->flags & UT_TESTF_PROBE_TEST)
94                 ut_assertok(do_autoprobe(uts));
95         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
96             (test->flags & UT_TESTF_SCAN_FDT))
97                 ut_assertok(dm_extended_scan(false));
98
99         /*
100          * Silence the console and rely on console recording to get
101          * our output.
102          */
103         console_record_reset_enable();
104         if (!state->show_test_output)
105                 gd->flags |= GD_FLG_SILENT;
106         test->func(uts);
107         gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
108         state_set_skip_delays(false);
109
110         ut_assertok(dm_test_destroy(uts));
111
112         return 0;
113 }
114
115 /**
116  * dm_test_run_on_flattree() - Check if we should run a test with flat DT
117  *
118  * This skips long/slow tests where there is not much value in running a flat
119  * DT test in addition to a live DT test.
120  *
121  * @return true to run the given test on the flat device tree
122  */
123 static bool dm_test_run_on_flattree(struct unit_test *test)
124 {
125         const char *fname = strrchr(test->file, '/') + 1;
126
127         return !strstr(fname, "video") || strstr(test->name, "video_base");
128 }
129
130 static bool test_matches(const char *test_name, const char *find_name)
131 {
132         if (!find_name)
133                 return true;
134
135         if (!strcmp(test_name, find_name))
136                 return true;
137
138         /* All tests have this prefix */
139         if (!strncmp(test_name, "dm_test_", 8))
140                 test_name += 8;
141
142         if (!strcmp(test_name, find_name))
143                 return true;
144
145         return false;
146 }
147
148 int dm_test_main(const char *test_name)
149 {
150         struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
151         const int n_ents = ll_entry_count(struct unit_test, dm_test);
152         struct unit_test_state *uts = &global_dm_test_state;
153         struct unit_test *test;
154         int found;
155
156         uts->priv = &_global_priv_dm_test_state;
157         uts->fail_count = 0;
158
159         if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
160                 /*
161                  * If we have no device tree, or it only has a root node, then
162                  * these * tests clearly aren't going to work...
163                  */
164                 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
165                         puts("Please run with test device tree:\n"
166                              "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
167                         ut_assert(gd->fdt_blob);
168                 }
169         }
170
171         if (!test_name)
172                 printf("Running %d driver model tests\n", n_ents);
173         else
174
175         found = 0;
176         uts->of_root = gd_of_root();
177         for (test = tests; test < tests + n_ents; test++) {
178                 const char *name = test->name;
179                 int runs;
180
181                 if (!test_matches(name, test_name))
182                         continue;
183
184                 /* Run with the live tree if possible */
185                 runs = 0;
186                 if (CONFIG_IS_ENABLED(OF_LIVE)) {
187                         if (!(test->flags & UT_TESTF_FLAT_TREE)) {
188                                 ut_assertok(dm_do_test(uts, test, true));
189                                 runs++;
190                         }
191                 }
192
193                 /*
194                  * Run with the flat tree if we couldn't run it with live tree,
195                  * or it is a core test.
196                  */
197                 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
198                     (!runs || dm_test_run_on_flattree(test))) {
199                         ut_assertok(dm_do_test(uts, test, false));
200                         runs++;
201                 }
202                 found++;
203         }
204
205         if (test_name && !found)
206                 printf("Test '%s' not found\n", test_name);
207         else
208                 printf("Failures: %d\n", uts->fail_count);
209
210         /* Put everything back to normal so that sandbox works as expected */
211         gd_set_of_root(uts->of_root);
212         gd->dm_root = NULL;
213         ut_assertok(dm_init(CONFIG_IS_ENABLED(OF_LIVE)));
214         dm_scan_plat(false);
215         if (!CONFIG_IS_ENABLED(OF_PLATDATA))
216                 dm_scan_fdt(false);
217
218         return uts->fail_count ? CMD_RET_FAILURE : 0;
219 }
220
221 int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
222 {
223         const char *test_name = NULL;
224
225         if (argc > 1)
226                 test_name = argv[1];
227
228         return dm_test_main(test_name);
229 }