1/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22*/
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <string.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include <ctype.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <signal.h>
37#include <time.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/ioctl.h>
41#include <sys/time.h>
42#include <stdarg.h>
43#include <stdint.h>
44
45#include "drm.h"
46#include "xf86drmMode.h"
47#include "xf86drm.h"
48
49#include "CUnit/Basic.h"
50
51#include "amdgpu_test.h"
52
53/**
54 *  Open handles for amdgpu devices
55 *
56 */
57int drm_amdgpu[MAX_CARDS_SUPPORTED];
58
59/** The table of all known test suites to run */
60static CU_SuiteInfo suites[] = {
61	{
62		.pName = "Basic Tests",
63		.pInitFunc = suite_basic_tests_init,
64		.pCleanupFunc = suite_basic_tests_clean,
65		.pTests = basic_tests,
66	},
67	{
68		.pName = "BO Tests",
69		.pInitFunc = suite_bo_tests_init,
70		.pCleanupFunc = suite_bo_tests_clean,
71		.pTests = bo_tests,
72	},
73	{
74		.pName = "CS Tests",
75		.pInitFunc = suite_cs_tests_init,
76		.pCleanupFunc = suite_cs_tests_clean,
77		.pTests = cs_tests,
78	},
79	{
80		.pName = "VCE Tests",
81		.pInitFunc = suite_vce_tests_init,
82		.pCleanupFunc = suite_vce_tests_clean,
83		.pTests = vce_tests,
84	},
85	CU_SUITE_INFO_NULL,
86};
87
88
89/** Display information about all  suites and their tests */
90static void display_test_suites(void)
91{
92	int iSuite;
93	int iTest;
94
95	printf("Suites\n");
96
97	for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
98		printf("Suite id = %d: Name '%s'\n",
99				iSuite + 1, suites[iSuite].pName);
100
101		for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
102			iTest++) {
103			printf("	Test id %d: Name: '%s'\n", iTest + 1,
104					suites[iSuite].pTests[iTest].pName);
105		}
106	}
107}
108
109
110/** Help string for command line parameters */
111static const char usage[] = "Usage: %s [-hl] [<-s <suite id>> [-t <test id>]]\n"
112				"where:\n"
113				"       l - Display all suites and their tests\n"
114				"       h - Display this help\n";
115/** Specified options strings for getopt */
116static const char options[]   = "hls:t:";
117
118/* The main() function for setting up and running the tests.
119 * Returns a CUE_SUCCESS on successful running, another
120 * CUnit error code on failure.
121 */
122int main(int argc, char **argv)
123{
124	int c;			/* Character received from getopt */
125	int i = 0;
126	int suite_id = -1;	/* By default run everything */
127	int test_id  = -1;	/* By default run all tests in the suite */
128	CU_pSuite pSuite = NULL;
129	CU_pTest  pTest  = NULL;
130
131	int aval = drmAvailable();
132
133	if (aval == 0) {
134		fprintf(stderr, "DRM driver is not available\n");
135		exit(EXIT_FAILURE);
136	}
137
138
139	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
140		drm_amdgpu[i] = -1;
141
142
143	/* Parse command line string */
144	opterr = 0;		/* Do not print error messages from getopt */
145	while ((c = getopt(argc, argv, options)) != -1) {
146		switch (c) {
147		case 'l':
148			display_test_suites();
149			exit(EXIT_SUCCESS);
150		case 's':
151			suite_id = atoi(optarg);
152			break;
153		case 't':
154			test_id = atoi(optarg);
155			break;
156		case '?':
157		case 'h':
158			fprintf(stderr, usage, argv[0]);
159			exit(EXIT_SUCCESS);
160		default:
161			fprintf(stderr, usage, argv[0]);
162			exit(EXIT_FAILURE);
163		}
164	}
165
166	/* Try to open all possible radeon connections
167	 * Right now: Open only the 0.
168	 */
169	printf("Try to open the card 0..\n");
170	drm_amdgpu[0] = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
171
172	if (drm_amdgpu[0] < 0) {
173		perror("Cannot open /dev/dri/card0\n");
174		exit(EXIT_FAILURE);
175	}
176
177	/** Display version of DRM driver */
178	drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
179
180	if (retval == NULL) {
181		perror("Could not get information about DRM driver");
182		exit(EXIT_FAILURE);
183	}
184
185	printf("DRM Driver: Name: [%s] : Date [%s] : Description [%s]\n",
186	       retval->name, retval->date, retval->desc);
187
188	drmFreeVersion(retval);
189
190	/* Initialize test suites to run */
191
192	/* initialize the CUnit test registry */
193	if (CUE_SUCCESS != CU_initialize_registry()) {
194		close(drm_amdgpu[0]);
195		return CU_get_error();
196	}
197
198	/* Register suites. */
199	if (CU_register_suites(suites) != CUE_SUCCESS) {
200		fprintf(stderr, "suite registration failed - %s\n",
201				CU_get_error_msg());
202		CU_cleanup_registry();
203		close(drm_amdgpu[0]);
204		exit(EXIT_FAILURE);
205	}
206
207	/* Run tests using the CUnit Basic interface */
208	CU_basic_set_mode(CU_BRM_VERBOSE);
209
210	if (suite_id != -1) {	/* If user specify particular suite? */
211		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
212						CU_get_registry());
213
214		if (pSuite) {
215			if (test_id != -1) {   /* If user specify test id */
216				pTest = CU_get_test_by_index(
217						(unsigned int) test_id,
218						pSuite);
219				if (pTest)
220					CU_basic_run_test(pSuite, pTest);
221				else {
222					fprintf(stderr, "Invalid test id: %d\n",
223								test_id);
224					CU_cleanup_registry();
225					close(drm_amdgpu[0]);
226					exit(EXIT_FAILURE);
227				}
228			} else
229				CU_basic_run_suite(pSuite);
230		} else {
231			fprintf(stderr, "Invalid suite id : %d\n",
232					suite_id);
233			CU_cleanup_registry();
234			close(drm_amdgpu[0]);
235			exit(EXIT_FAILURE);
236		}
237	} else
238		CU_basic_run_tests();
239
240	CU_cleanup_registry();
241	close(drm_amdgpu[0]);
242	return CU_get_error();
243}
244