2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
46 #include "xf86drmMode.h"
49 #include "CUnit/Basic.h"
51 #include "amdgpu_test.h"
54 * Open handles for amdgpu devices
57 int drm_amdgpu[MAX_CARDS_SUPPORTED];
59 /** Open render node to test */
60 int open_render_node = 0; /* By default run most tests on primary node */
62 /** The table of all known test suites to run */
63 static CU_SuiteInfo suites[] = {
65 .pName = "Basic Tests",
66 .pInitFunc = suite_basic_tests_init,
67 .pCleanupFunc = suite_basic_tests_clean,
68 .pTests = basic_tests,
72 .pInitFunc = suite_bo_tests_init,
73 .pCleanupFunc = suite_bo_tests_clean,
78 .pInitFunc = suite_cs_tests_init,
79 .pCleanupFunc = suite_cs_tests_clean,
84 .pInitFunc = suite_vce_tests_init,
85 .pCleanupFunc = suite_vce_tests_clean,
90 .pInitFunc = suite_vcn_tests_init,
91 .pCleanupFunc = suite_vcn_tests_clean,
95 .pName = "UVD ENC Tests",
96 .pInitFunc = suite_uvd_enc_tests_init,
97 .pCleanupFunc = suite_uvd_enc_tests_clean,
98 .pTests = uvd_enc_tests,
101 .pName = "Deadlock Tests",
102 .pInitFunc = suite_deadlock_tests_init,
103 .pCleanupFunc = suite_deadlock_tests_clean,
104 .pTests = deadlock_tests,
110 /** Display information about all suites and their tests */
111 static void display_test_suites(void)
118 for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
119 printf("Suite id = %d: Name '%s'\n",
120 iSuite + 1, suites[iSuite].pName);
122 for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
124 printf(" Test id %d: Name: '%s'\n", iTest + 1,
125 suites[iSuite].pTests[iTest].pName);
131 /** Help string for command line parameters */
132 static const char usage[] =
133 "Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>]] "
134 "[-b <pci_bus_id> [-d <pci_device_id>]]\n"
136 " l - Display all suites and their tests\n"
137 " r - Run the tests on render node\n"
138 " b - Specify device's PCI bus id to run tests\n"
139 " d - Specify device's PCI device id to run tests (optional)\n"
140 " p - Display information of AMDGPU devices in system\n"
141 " h - Display this help\n";
142 /** Specified options strings for getopt */
143 static const char options[] = "hlrps:t:b:d:";
146 * Return the number of AMD device openned.
148 static int amdgpu_open_devices(int open_render_node)
150 drmDevicePtr devices[MAX_CARDS_SUPPORTED];
157 drmVersionPtr version;
159 drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
163 "drmGetDevices2() returned an error %d\n",
168 for (i = 0; i < drm_count; i++) {
169 /* If this is not PCI device, skip*/
170 if (devices[i]->bustype != DRM_BUS_PCI)
173 /* If this is not AMD GPU vender ID, skip*/
174 if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
177 if (open_render_node)
178 drm_node = DRM_NODE_RENDER;
180 drm_node = DRM_NODE_PRIMARY;
183 if (devices[i]->available_nodes & 1 << drm_node)
185 devices[i]->nodes[drm_node],
188 /* This node is not available. */
189 if (fd < 0) continue;
191 version = drmGetVersion(fd);
194 "Warning: Cannot get version for %s."
196 devices[i]->nodes[drm_node],
202 if (strcmp(version->name, "amdgpu")) {
203 /* This is not AMDGPU driver, skip.*/
204 drmFreeVersion(version);
209 drmFreeVersion(version);
211 drm_amdgpu[amd_index] = fd;
215 drmFreeDevices(devices, drm_count);
219 /* Close AMD devices.
221 static void amdgpu_close_devices()
224 for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
225 if (drm_amdgpu[i] >=0)
226 close(drm_amdgpu[i]);
229 /* Print AMD devices information */
230 static void amdgpu_print_devices()
235 /* Open the first AMD devcie to print driver information. */
236 if (drm_amdgpu[0] >=0) {
237 /* Display AMD driver version information.*/
238 drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
240 if (retval == NULL) {
241 perror("Cannot get version for AMDGPU device");
245 printf("Driver name: %s, Date: %s, Description: %s.\n",
246 retval->name, retval->date, retval->desc);
247 drmFreeVersion(retval);
250 /* Display information of AMD devices */
251 printf("Devices:\n");
252 for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
253 if (drmGetDevice2(drm_amdgpu[i],
254 DRM_DEVICE_GET_PCI_REVISION,
256 if (device->bustype == DRM_BUS_PCI) {
258 printf(" domain:%04x",
259 device->businfo.pci->domain);
261 device->businfo.pci->bus);
262 printf(" device:%02x",
263 device->businfo.pci->dev);
264 printf(" function:%01x",
265 device->businfo.pci->func);
266 printf(" vendor_id:%04x",
267 device->deviceinfo.pci->vendor_id);
268 printf(" device_id:%04x",
269 device->deviceinfo.pci->device_id);
270 printf(" subvendor_id:%04x",
271 device->deviceinfo.pci->subvendor_id);
272 printf(" subdevice_id:%04x",
273 device->deviceinfo.pci->subdevice_id);
274 printf(" revision_id:%02x",
275 device->deviceinfo.pci->revision_id);
278 drmFreeDevice(&device);
282 /* Find a match AMD device in PCI bus
283 * Return the index of the device or -1 if not found
285 static int amdgpu_find_device(uint8_t bus, uint16_t dev)
290 for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
291 if (drmGetDevice2(drm_amdgpu[i],
292 DRM_DEVICE_GET_PCI_REVISION,
294 if (device->bustype == DRM_BUS_PCI)
295 if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
296 device->deviceinfo.pci->device_id == dev) {
297 drmFreeDevice(&device);
301 drmFreeDevice(&device);
308 /* The main() function for setting up and running the tests.
309 * Returns a CUE_SUCCESS on successful running, another
310 * CUnit error code on failure.
312 int main(int argc, char **argv)
314 int c; /* Character received from getopt */
316 int suite_id = -1; /* By default run everything */
317 int test_id = -1; /* By default run all tests in the suite */
318 int pci_bus_id = -1; /* By default PC bus ID is not specified */
319 int pci_device_id = 0; /* By default PC device ID is zero */
320 int display_devices = 0;/* By default not to display devices' info */
321 CU_pSuite pSuite = NULL;
322 CU_pTest pTest = NULL;
323 int test_device_index;
325 for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
329 /* Parse command line string */
330 opterr = 0; /* Do not print error messages from getopt */
331 while ((c = getopt(argc, argv, options)) != -1) {
334 display_test_suites();
337 suite_id = atoi(optarg);
340 test_id = atoi(optarg);
343 pci_bus_id = atoi(optarg);
346 sscanf(optarg, "%x", &pci_device_id);
352 open_render_node = 1;
356 fprintf(stderr, usage, argv[0]);
359 fprintf(stderr, usage, argv[0]);
364 if (amdgpu_open_devices(open_render_node) <= 0) {
365 perror("Cannot open AMDGPU device");
369 if (drm_amdgpu[0] < 0) {
370 perror("Cannot open AMDGPU device");
374 if (display_devices) {
375 amdgpu_print_devices();
376 amdgpu_close_devices();
380 if (pci_bus_id > 0 || pci_device_id) {
381 /* A device was specified to run the test */
382 test_device_index = amdgpu_find_device(pci_bus_id,
385 if (test_device_index >= 0) {
386 /* Most tests run on device of drm_amdgpu[0].
387 * Swap the chosen device to drm_amdgpu[0].
390 drm_amdgpu[0] = drm_amdgpu[test_device_index];
391 drm_amdgpu[test_device_index] = i;
394 "The specified GPU device does not exist.\n");
399 /* Initialize test suites to run */
401 /* initialize the CUnit test registry */
402 if (CUE_SUCCESS != CU_initialize_registry()) {
403 amdgpu_close_devices();
404 return CU_get_error();
407 /* Register suites. */
408 if (CU_register_suites(suites) != CUE_SUCCESS) {
409 fprintf(stderr, "suite registration failed - %s\n",
411 CU_cleanup_registry();
412 amdgpu_close_devices();
416 /* Run tests using the CUnit Basic interface */
417 CU_basic_set_mode(CU_BRM_VERBOSE);
419 if (suite_id != -1) { /* If user specify particular suite? */
420 pSuite = CU_get_suite_by_index((unsigned int) suite_id,
424 if (test_id != -1) { /* If user specify test id */
425 pTest = CU_get_test_by_index(
426 (unsigned int) test_id,
429 CU_basic_run_test(pSuite, pTest);
431 fprintf(stderr, "Invalid test id: %d\n",
433 CU_cleanup_registry();
434 amdgpu_close_devices();
438 CU_basic_run_suite(pSuite);
440 fprintf(stderr, "Invalid suite id : %d\n",
442 CU_cleanup_registry();
443 amdgpu_close_devices();
447 CU_basic_run_tests();
449 CU_cleanup_registry();
450 amdgpu_close_devices();
451 return CU_get_error();