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 */ 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 */ 45e721b882SJoe Hershberger static int do_autoprobe(struct unit_test_state *uts) 462e7d35d2SSimon Glass { 4754c5d08aSHeiko Schocher struct udevice *dev; 482e7d35d2SSimon Glass int ret; 492e7d35d2SSimon Glass 502e7d35d2SSimon Glass /* Scanning the uclass is enough to probe all the devices */ 512e7d35d2SSimon Glass for (ret = uclass_first_device(UCLASS_TEST, &dev); 522e7d35d2SSimon Glass dev; 532e7d35d2SSimon Glass ret = uclass_next_device(&dev)) 542e7d35d2SSimon Glass ; 552e7d35d2SSimon Glass 562e7d35d2SSimon Glass return ret; 572e7d35d2SSimon Glass } 582e7d35d2SSimon Glass 59e721b882SJoe Hershberger static int dm_test_destroy(struct unit_test_state *uts) 602e7d35d2SSimon Glass { 612e7d35d2SSimon Glass int id; 622e7d35d2SSimon Glass 632e7d35d2SSimon Glass for (id = 0; id < UCLASS_COUNT; id++) { 642e7d35d2SSimon Glass struct uclass *uc; 652e7d35d2SSimon Glass 662e7d35d2SSimon Glass /* 672e7d35d2SSimon Glass * If the uclass doesn't exist we don't want to create it. So 682e7d35d2SSimon Glass * check that here before we call uclass_find_device()/ 692e7d35d2SSimon Glass */ 702e7d35d2SSimon Glass uc = uclass_find(id); 712e7d35d2SSimon Glass if (!uc) 722e7d35d2SSimon Glass continue; 732e7d35d2SSimon Glass ut_assertok(uclass_destroy(uc)); 742e7d35d2SSimon Glass } 752e7d35d2SSimon Glass 762e7d35d2SSimon Glass return 0; 772e7d35d2SSimon Glass } 782e7d35d2SSimon Glass 79c166c47bSSimon Glass static int dm_do_test(struct unit_test_state *uts, struct unit_test *test, 80c166c47bSSimon Glass bool of_live) 81f86db10cSSimon Glass { 82f86db10cSSimon Glass struct sandbox_state *state = state_get_current(); 83801587bdSSimon Glass const char *fname = strrchr(test->file, '/') + 1; 84f86db10cSSimon Glass 85c166c47bSSimon Glass printf("Test: %s: %s%s\n", test->name, fname, 86c166c47bSSimon Glass !of_live ? " (flat tree)" : ""); 87c166c47bSSimon Glass ut_assertok(dm_test_init(uts, of_live)); 88f86db10cSSimon Glass 89f86db10cSSimon Glass uts->start = mallinfo(); 90f86db10cSSimon Glass if (test->flags & DM_TESTF_SCAN_PDATA) 91f86db10cSSimon Glass ut_assertok(dm_scan_platdata(false)); 92f86db10cSSimon Glass if (test->flags & DM_TESTF_PROBE_TEST) 93f86db10cSSimon Glass ut_assertok(do_autoprobe(uts)); 94f86db10cSSimon Glass if (test->flags & DM_TESTF_SCAN_FDT) 95*b048a4dbSPatrice Chotard ut_assertok(dm_extended_scan_fdt(gd->fdt_blob, false)); 96f86db10cSSimon Glass 97f86db10cSSimon Glass /* 98f86db10cSSimon Glass * Silence the console and rely on console reocrding to get 99f86db10cSSimon Glass * our output. 100f86db10cSSimon Glass */ 101f86db10cSSimon Glass console_record_reset(); 102f86db10cSSimon Glass if (!state->show_test_output) 103f86db10cSSimon Glass gd->flags |= GD_FLG_SILENT; 104f86db10cSSimon Glass test->func(uts); 105f86db10cSSimon Glass gd->flags &= ~GD_FLG_SILENT; 106f86db10cSSimon Glass state_set_skip_delays(false); 107f86db10cSSimon Glass 108f86db10cSSimon Glass ut_assertok(dm_test_destroy(uts)); 109f86db10cSSimon Glass 110f86db10cSSimon Glass return 0; 111f86db10cSSimon Glass } 112f86db10cSSimon Glass 1136fb2f579SSimon Glass /** 1146fb2f579SSimon Glass * dm_test_run_on_flattree() - Check if we should run a test with flat DT 1156fb2f579SSimon Glass * 1166fb2f579SSimon Glass * This skips long/slow tests where there is not much value in running a flat 1176fb2f579SSimon Glass * DT test in addition to a live DT test. 1186fb2f579SSimon Glass * 1196fb2f579SSimon Glass * @return true to run the given test on the flat device tree 1206fb2f579SSimon Glass */ 1216fb2f579SSimon Glass static bool dm_test_run_on_flattree(struct unit_test *test) 1226fb2f579SSimon Glass { 1236fb2f579SSimon Glass const char *fname = strrchr(test->file, '/') + 1; 1246fb2f579SSimon Glass 1256fb2f579SSimon Glass return !strstr(fname, "video") || strstr(test->name, "video_base"); 1266fb2f579SSimon Glass } 1276fb2f579SSimon Glass 12840441e0bSJoe Hershberger static int dm_test_main(const char *test_name) 1292e7d35d2SSimon Glass { 130e721b882SJoe Hershberger struct unit_test *tests = ll_entry_start(struct unit_test, dm_test); 131e721b882SJoe Hershberger const int n_ents = ll_entry_count(struct unit_test, dm_test); 132e721b882SJoe Hershberger struct unit_test_state *uts = &global_dm_test_state; 133e721b882SJoe Hershberger struct unit_test *test; 134c02790ceSSimon Glass int run_count; 1352e7d35d2SSimon Glass 136c166c47bSSimon Glass uts->priv = &_global_priv_dm_test_state; 13726e1beccSStephen Warren uts->fail_count = 0; 13826e1beccSStephen Warren 1392e7d35d2SSimon Glass /* 1402e7d35d2SSimon Glass * If we have no device tree, or it only has a root node, then these 1412e7d35d2SSimon Glass * tests clearly aren't going to work... 1422e7d35d2SSimon Glass */ 1432e7d35d2SSimon Glass if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) { 1442e7d35d2SSimon Glass puts("Please run with test device tree:\n" 145f64000c3SPrzemyslaw Marczak " ./u-boot -d arch/sandbox/dts/test.dtb\n"); 1462e7d35d2SSimon Glass ut_assert(gd->fdt_blob); 1472e7d35d2SSimon Glass } 1482e7d35d2SSimon Glass 14957f54d55SSimon Glass if (!test_name) 1502e7d35d2SSimon Glass printf("Running %d driver model tests\n", n_ents); 1512e7d35d2SSimon Glass 152c02790ceSSimon Glass run_count = 0; 153c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE 154c166c47bSSimon Glass uts->of_root = gd->of_root; 155c166c47bSSimon Glass #endif 1562e7d35d2SSimon Glass for (test = tests; test < tests + n_ents; test++) { 157c02790ceSSimon Glass const char *name = test->name; 1586fb2f579SSimon Glass int runs; 159c02790ceSSimon Glass 160c02790ceSSimon Glass /* All tests have this prefix */ 161c02790ceSSimon Glass if (!strncmp(name, "dm_test_", 8)) 162c02790ceSSimon Glass name += 8; 163c02790ceSSimon Glass if (test_name && strcmp(test_name, name)) 16457f54d55SSimon Glass continue; 1656fb2f579SSimon Glass 1666fb2f579SSimon Glass /* Run with the live tree if possible */ 1676fb2f579SSimon Glass runs = 0; 1686fb2f579SSimon Glass if (IS_ENABLED(CONFIG_OF_LIVE)) { 1696fb2f579SSimon Glass if (!(test->flags & DM_TESTF_FLAT_TREE)) { 1706fb2f579SSimon Glass ut_assertok(dm_do_test(uts, test, true)); 1716fb2f579SSimon Glass runs++; 1726fb2f579SSimon Glass } 1736fb2f579SSimon Glass } 1746fb2f579SSimon Glass 1756fb2f579SSimon Glass /* 1766fb2f579SSimon Glass * Run with the flat tree if we couldn't run it with live tree, 1776fb2f579SSimon Glass * or it is a core test. 1786fb2f579SSimon Glass */ 1796fb2f579SSimon Glass if (!(test->flags & DM_TESTF_LIVE_TREE) && 1806fb2f579SSimon Glass (!runs || dm_test_run_on_flattree(test))) { 181c166c47bSSimon Glass ut_assertok(dm_do_test(uts, test, false)); 1826fb2f579SSimon Glass runs++; 1836fb2f579SSimon Glass } 1846fb2f579SSimon Glass run_count += runs; 1852e7d35d2SSimon Glass } 1862e7d35d2SSimon Glass 187c02790ceSSimon Glass if (test_name && !run_count) 188c02790ceSSimon Glass printf("Test '%s' not found\n", test_name); 189c02790ceSSimon Glass else 190e721b882SJoe Hershberger printf("Failures: %d\n", uts->fail_count); 1912e7d35d2SSimon Glass 192b6227d39SJoe Hershberger gd->dm_root = NULL; 19319c8205eSSimon Glass ut_assertok(dm_init(false)); 194b6227d39SJoe Hershberger dm_scan_platdata(false); 195b6227d39SJoe Hershberger dm_scan_fdt(gd->fdt_blob, false); 196b6227d39SJoe Hershberger 1977cccc66aSJoe Hershberger return uts->fail_count ? CMD_RET_FAILURE : 0; 1982e7d35d2SSimon Glass } 19940441e0bSJoe Hershberger 20040441e0bSJoe Hershberger int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 20140441e0bSJoe Hershberger { 20240441e0bSJoe Hershberger const char *test_name = NULL; 20340441e0bSJoe Hershberger 20440441e0bSJoe Hershberger if (argc > 1) 20540441e0bSJoe Hershberger test_name = argv[1]; 20640441e0bSJoe Hershberger 20740441e0bSJoe Hershberger return dm_test_main(test_name); 20840441e0bSJoe Hershberger } 209