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 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <sys/time.h>
38 #include <stdarg.h>
39 #include <stdint.h>
40 
41 #include "drm.h"
42 #include "xf86drmMode.h"
43 #include "xf86drm.h"
44 
45 #include "CUnit/Basic.h"
46 
47 #include "amdgpu_test.h"
48 #include "amdgpu_internal.h"
49 
50 /* Test suite names */
51 #define BASIC_TESTS_STR "Basic Tests"
52 #define BO_TESTS_STR "BO Tests"
53 #define CS_TESTS_STR "CS Tests"
54 #define VCE_TESTS_STR "VCE Tests"
55 #define VCN_TESTS_STR "VCN Tests"
56 #define UVD_ENC_TESTS_STR "UVD ENC Tests"
57 #define DEADLOCK_TESTS_STR "Deadlock Tests"
58 #define VM_TESTS_STR "VM Tests"
59 #define RAS_TESTS_STR "RAS Tests"
60 #define SYNCOBJ_TIMELINE_TESTS_STR "SYNCOBJ TIMELINE Tests"
61 #define SECURITY_TESTS_STR "Security Tests"
62 
63 /**
64  *  Open handles for amdgpu devices
65  *
66  */
67 int drm_amdgpu[MAX_CARDS_SUPPORTED];
68 
69 /** Open render node to test */
70 int open_render_node = 0;	/* By default run most tests on primary node */
71 
72 /** The table of all known test suites to run */
73 static CU_SuiteInfo suites[] = {
74 	{
75 		.pName = BASIC_TESTS_STR,
76 		.pInitFunc = suite_basic_tests_init,
77 		.pCleanupFunc = suite_basic_tests_clean,
78 		.pTests = basic_tests,
79 	},
80 	{
81 		.pName = BO_TESTS_STR,
82 		.pInitFunc = suite_bo_tests_init,
83 		.pCleanupFunc = suite_bo_tests_clean,
84 		.pTests = bo_tests,
85 	},
86 	{
87 		.pName = CS_TESTS_STR,
88 		.pInitFunc = suite_cs_tests_init,
89 		.pCleanupFunc = suite_cs_tests_clean,
90 		.pTests = cs_tests,
91 	},
92 	{
93 		.pName = VCE_TESTS_STR,
94 		.pInitFunc = suite_vce_tests_init,
95 		.pCleanupFunc = suite_vce_tests_clean,
96 		.pTests = vce_tests,
97 	},
98 	{
99 		.pName = VCN_TESTS_STR,
100 		.pInitFunc = suite_vcn_tests_init,
101 		.pCleanupFunc = suite_vcn_tests_clean,
102 		.pTests = vcn_tests,
103 	},
104 	{
105 		.pName = UVD_ENC_TESTS_STR,
106 		.pInitFunc = suite_uvd_enc_tests_init,
107 		.pCleanupFunc = suite_uvd_enc_tests_clean,
108 		.pTests = uvd_enc_tests,
109 	},
110 	{
111 		.pName = DEADLOCK_TESTS_STR,
112 		.pInitFunc = suite_deadlock_tests_init,
113 		.pCleanupFunc = suite_deadlock_tests_clean,
114 		.pTests = deadlock_tests,
115 	},
116 	{
117 		.pName = VM_TESTS_STR,
118 		.pInitFunc = suite_vm_tests_init,
119 		.pCleanupFunc = suite_vm_tests_clean,
120 		.pTests = vm_tests,
121 	},
122 	{
123 		.pName = RAS_TESTS_STR,
124 		.pInitFunc = suite_ras_tests_init,
125 		.pCleanupFunc = suite_ras_tests_clean,
126 		.pTests = ras_tests,
127 	},
128 	{
129 		.pName = SYNCOBJ_TIMELINE_TESTS_STR,
130 		.pInitFunc = suite_syncobj_timeline_tests_init,
131 		.pCleanupFunc = suite_syncobj_timeline_tests_clean,
132 		.pTests = syncobj_timeline_tests,
133 	},
134 	{
135 		.pName = SECURITY_TESTS_STR,
136 		.pInitFunc = suite_security_tests_init,
137 		.pCleanupFunc = suite_security_tests_clean,
138 		.pTests = security_tests,
139 	},
140 
141 	CU_SUITE_INFO_NULL,
142 };
143 
144 typedef CU_BOOL (*active__stat_func)(void);
145 
146 typedef struct Suites_Active_Status {
147 	char*             pName;
148 	active__stat_func pActive;
149 }Suites_Active_Status;
150 
always_active()151 static CU_BOOL always_active()
152 {
153 	return CU_TRUE;
154 }
155 
156 static Suites_Active_Status suites_active_stat[] = {
157 		{
158 			.pName = BASIC_TESTS_STR,
159 			.pActive = suite_basic_tests_enable,
160 		},
161 		{
162 			.pName = BO_TESTS_STR,
163 			.pActive = always_active,
164 		},
165 		{
166 			.pName = CS_TESTS_STR,
167 			.pActive = suite_cs_tests_enable,
168 		},
169 		{
170 			.pName = VCE_TESTS_STR,
171 			.pActive = suite_vce_tests_enable,
172 		},
173 		{
174 			.pName = VCN_TESTS_STR,
175 			.pActive = suite_vcn_tests_enable,
176 		},
177 		{
178 			.pName = UVD_ENC_TESTS_STR,
179 			.pActive = suite_uvd_enc_tests_enable,
180 		},
181 		{
182 			.pName = DEADLOCK_TESTS_STR,
183 			.pActive = suite_deadlock_tests_enable,
184 		},
185 		{
186 			.pName = VM_TESTS_STR,
187 			.pActive = suite_vm_tests_enable,
188 		},
189 		{
190 			.pName = RAS_TESTS_STR,
191 			.pActive = suite_ras_tests_enable,
192 		},
193 		{
194 			.pName = SYNCOBJ_TIMELINE_TESTS_STR,
195 			.pActive = suite_syncobj_timeline_tests_enable,
196 		},
197 		{
198 			.pName = SECURITY_TESTS_STR,
199 			.pActive = suite_security_tests_enable,
200 		},
201 };
202 
203 
204 /*
205  * Display information about all  suites and their tests
206  *
207  * NOTE: Must be run after registry is initialized and suites registered.
208  */
display_test_suites(void)209 static void display_test_suites(void)
210 {
211 	int iSuite;
212 	int iTest;
213 	CU_pSuite pSuite = NULL;
214 	CU_pTest  pTest  = NULL;
215 
216 	printf("%5s: %2s: %8s: %s\n", "What", "ID", "Status", "Name");
217 
218 	for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
219 
220 		pSuite = CU_get_suite_by_index((unsigned int) iSuite + 1,
221 					       CU_get_registry());
222 
223 		if (!pSuite) {
224 			fprintf(stderr, "Invalid suite id : %d\n", iSuite + 1);
225 			continue;
226 		}
227 
228 		printf("Suite: %2d: %8s: %s\n",
229 		       iSuite + 1,
230 		       pSuite->fActive ? "ENABLED" : "DISABLED",
231 		       suites[iSuite].pName);
232 
233 		if (!pSuite->fActive)
234 			continue;
235 
236 		for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
237 		     iTest++) {
238 			pTest = CU_get_test_by_index((unsigned int) iTest + 1,
239 						     pSuite);
240 			if (!pTest) {
241 				fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
242 				continue;
243 			}
244 			printf(" Test: %2d: %8s: %s\n",
245 			       iTest + 1,
246 			       pSuite->fActive && pTest->fActive ? "ENABLED" : "DISABLED",
247 			       suites[iSuite].pTests[iTest].pName);
248 		}
249 	}
250 }
251 
252 /** Help string for command line parameters */
253 static const char usage[] =
254 	"Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
255 	"[-b <pci_bus_id> [-d <pci_device_id>]]\n"
256 	"where:\n"
257 	"       l - Display all suites and their tests\n"
258 	"       r - Run the tests on render node\n"
259 	"       b - Specify device's PCI bus id to run tests\n"
260 	"       d - Specify device's PCI device id to run tests (optional)\n"
261 	"       p - Display information of AMDGPU devices in system\n"
262 	"       f - Force executing inactive suite or test\n"
263 	"       h - Display this help\n";
264 /** Specified options strings for getopt */
265 static const char options[]   = "hlrps:t:b:d:f";
266 
267 /* Open AMD devices.
268  * Return the number of AMD device opened.
269  */
amdgpu_open_devices(int open_render_node)270 static int amdgpu_open_devices(int open_render_node)
271 {
272 	drmDevicePtr devices[MAX_CARDS_SUPPORTED];
273 	int i;
274 	int drm_node;
275 	int amd_index = 0;
276 	int drm_count;
277 	int fd;
278 	drmVersionPtr version;
279 
280 	drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
281 
282 	if (drm_count < 0) {
283 		fprintf(stderr,
284 			"drmGetDevices2() returned an error %d\n",
285 			drm_count);
286 		return 0;
287 	}
288 
289 	for (i = 0; i < drm_count; i++) {
290 		/* If this is not PCI device, skip*/
291 		if (devices[i]->bustype != DRM_BUS_PCI)
292 			continue;
293 
294 		/* If this is not AMD GPU vender ID, skip*/
295 		if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
296 			continue;
297 
298 		if (open_render_node)
299 			drm_node = DRM_NODE_RENDER;
300 		else
301 			drm_node = DRM_NODE_PRIMARY;
302 
303 		fd = -1;
304 		if (devices[i]->available_nodes & 1 << drm_node)
305 			fd = open(
306 				devices[i]->nodes[drm_node],
307 				O_RDWR | O_CLOEXEC);
308 
309 		/* This node is not available. */
310 		if (fd < 0) continue;
311 
312 		version = drmGetVersion(fd);
313 		if (!version) {
314 			fprintf(stderr,
315 				"Warning: Cannot get version for %s."
316 				"Error is %s\n",
317 				devices[i]->nodes[drm_node],
318 				strerror(errno));
319 			close(fd);
320 			continue;
321 		}
322 
323 		if (strcmp(version->name, "amdgpu")) {
324 			/* This is not AMDGPU driver, skip.*/
325 			drmFreeVersion(version);
326 			close(fd);
327 			continue;
328 		}
329 
330 		drmFreeVersion(version);
331 
332 		drm_amdgpu[amd_index] = fd;
333 		amd_index++;
334 	}
335 
336 	drmFreeDevices(devices, drm_count);
337 	return amd_index;
338 }
339 
340 /* Close AMD devices.
341  */
amdgpu_close_devices()342 static void amdgpu_close_devices()
343 {
344 	int i;
345 	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
346 		if (drm_amdgpu[i] >=0)
347 			close(drm_amdgpu[i]);
348 }
349 
350 /* Print AMD devices information */
amdgpu_print_devices()351 static void amdgpu_print_devices()
352 {
353 	int i;
354 	drmDevicePtr device;
355 
356 	/* Open the first AMD device to print driver information. */
357 	if (drm_amdgpu[0] >=0) {
358 		/* Display AMD driver version information.*/
359 		drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
360 
361 		if (retval == NULL) {
362 			perror("Cannot get version for AMDGPU device");
363 			return;
364 		}
365 
366 		printf("Driver name: %s, Date: %s, Description: %s.\n",
367 			retval->name, retval->date, retval->desc);
368 		drmFreeVersion(retval);
369 	}
370 
371 	/* Display information of AMD devices */
372 	printf("Devices:\n");
373 	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
374 		if (drmGetDevice2(drm_amdgpu[i],
375 			DRM_DEVICE_GET_PCI_REVISION,
376 			&device) == 0) {
377 			if (device->bustype == DRM_BUS_PCI) {
378 				printf("PCI ");
379 				printf(" domain:%04x",
380 					device->businfo.pci->domain);
381 				printf(" bus:%02x",
382 					device->businfo.pci->bus);
383 				printf(" device:%02x",
384 					device->businfo.pci->dev);
385 				printf(" function:%01x",
386 					device->businfo.pci->func);
387 				printf(" vendor_id:%04x",
388 					device->deviceinfo.pci->vendor_id);
389 				printf(" device_id:%04x",
390 					device->deviceinfo.pci->device_id);
391 				printf(" subvendor_id:%04x",
392 					device->deviceinfo.pci->subvendor_id);
393 				printf(" subdevice_id:%04x",
394 					device->deviceinfo.pci->subdevice_id);
395 				printf(" revision_id:%02x",
396 					device->deviceinfo.pci->revision_id);
397 				printf("\n");
398 			}
399 			drmFreeDevice(&device);
400 		}
401 }
402 
403 /* Find a match AMD device in PCI bus
404  * Return the index of the device or -1 if not found
405  */
amdgpu_find_device(uint8_t bus,uint16_t dev)406 static int amdgpu_find_device(uint8_t bus, uint16_t dev)
407 {
408 	int i;
409 	drmDevicePtr device;
410 
411 	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
412 		if (drmGetDevice2(drm_amdgpu[i],
413 			DRM_DEVICE_GET_PCI_REVISION,
414 			&device) == 0) {
415 			if (device->bustype == DRM_BUS_PCI)
416 				if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
417 					device->deviceinfo.pci->device_id == dev) {
418 					drmFreeDevice(&device);
419 					return i;
420 				}
421 
422 			drmFreeDevice(&device);
423 		}
424 	}
425 
426 	return -1;
427 }
428 
amdgpu_disable_suites()429 static void amdgpu_disable_suites()
430 {
431 	amdgpu_device_handle device_handle;
432 	uint32_t major_version, minor_version, family_id;
433 	int i;
434 	int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
435 
436 	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
437 				   &minor_version, &device_handle))
438 		return;
439 
440 	family_id = device_handle->info.family_id;
441 
442 	if (amdgpu_device_deinitialize(device_handle))
443 		return;
444 
445 	/* Set active status for suites based on their policies */
446 	for (i = 0; i < size; ++i)
447 		if (amdgpu_set_suite_active(suites_active_stat[i].pName,
448 				suites_active_stat[i].pActive()))
449 			fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
450 
451 	/* Explicitly disable specific tests due to known bugs or preferences */
452 	/*
453 	* BUG: Compute ring stalls and never recovers when the address is
454 	* written after the command already submitted
455 	*/
456 	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
457 			"compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
458 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
459 
460 	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
461 				"sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
462 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
463 
464 	/* This test was ran on GFX9 only */
465 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
466 		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
467 				"gfx ring bad dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
468 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
469 
470 	/* This test was ran on GFX9 only */
471 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
472 		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
473 				"compute ring bad dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
474 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
475 
476 	/* This test was ran on GFX9 only */
477 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
478 		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
479 				"gfx ring bad slow dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
480 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
481 
482 	/* This test was ran on GFX9 only */
483 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
484 		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
485 				"compute ring bad slow dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
486 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
487 
488 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
489 		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
490 				"gfx ring bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
491 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
492 
493 	/* This test was ran on GFX9 only */
494 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
495 		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
496 				"gfx ring slow bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
497 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
498 
499 	if (amdgpu_set_test_active(BO_TESTS_STR, "Metadata", CU_FALSE))
500 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
501 
502 	if (amdgpu_set_test_active(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
503 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
504 
505 	/* This test was ran on GFX8 and GFX9 only */
506 	if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
507 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
508 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
509 
510 	/* This test was ran on GFX9 only */
511 	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
512 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
513 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
514 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
515 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
516 	}
517 
518 	/* This test was ran on GFX9 only */
519 	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
520 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
521 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
522 
523 	/* This test was ran on GFX9 only */
524 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
525 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
526 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
527 }
528 
529 /* The main() function for setting up and running the tests.
530  * Returns a CUE_SUCCESS on successful running, another
531  * CUnit error code on failure.
532  */
main(int argc,char ** argv)533 int main(int argc, char **argv)
534 {
535 	int c;			/* Character received from getopt */
536 	int i = 0;
537 	int suite_id = -1;	/* By default run everything */
538 	int test_id  = -1;	/* By default run all tests in the suite */
539 	int pci_bus_id = -1;    /* By default PC bus ID is not specified */
540 	int pci_device_id = 0;  /* By default PC device ID is zero */
541 	int display_devices = 0;/* By default not to display devices' info */
542 	CU_pSuite pSuite = NULL;
543 	CU_pTest  pTest  = NULL;
544 	int test_device_index;
545 	int display_list = 0;
546 	int force_run = 0;
547 
548 	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
549 		drm_amdgpu[i] = -1;
550 
551 
552 	/* Parse command line string */
553 	opterr = 0;		/* Do not print error messages from getopt */
554 	while ((c = getopt(argc, argv, options)) != -1) {
555 		switch (c) {
556 		case 'l':
557 			display_list = 1;
558 			break;
559 		case 's':
560 			suite_id = atoi(optarg);
561 			break;
562 		case 't':
563 			test_id = atoi(optarg);
564 			break;
565 		case 'b':
566 			pci_bus_id = atoi(optarg);
567 			break;
568 		case 'd':
569 			sscanf(optarg, "%x", &pci_device_id);
570 			break;
571 		case 'p':
572 			display_devices = 1;
573 			break;
574 		case 'r':
575 			open_render_node = 1;
576 			break;
577 		case 'f':
578 			force_run = 1;
579 			break;
580 		case '?':
581 		case 'h':
582 			fprintf(stderr, usage, argv[0]);
583 			exit(EXIT_SUCCESS);
584 		default:
585 			fprintf(stderr, usage, argv[0]);
586 			exit(EXIT_FAILURE);
587 		}
588 	}
589 
590 	if (amdgpu_open_devices(open_render_node) <= 0) {
591 		perror("Cannot open AMDGPU device");
592 		exit(EXIT_FAILURE);
593 	}
594 
595 	if (drm_amdgpu[0] < 0) {
596 		perror("Cannot open AMDGPU device");
597 		exit(EXIT_FAILURE);
598 	}
599 
600 	if (display_devices) {
601 		amdgpu_print_devices();
602 		amdgpu_close_devices();
603 		exit(EXIT_SUCCESS);
604 	}
605 
606 	if (pci_bus_id > 0 || pci_device_id) {
607 		/* A device was specified to run the test */
608 		test_device_index = amdgpu_find_device(pci_bus_id,
609 						       pci_device_id);
610 
611 		if (test_device_index >= 0) {
612 			/* Most tests run on device of drm_amdgpu[0].
613 			 * Swap the chosen device to drm_amdgpu[0].
614 			 */
615 			i = drm_amdgpu[0];
616 			drm_amdgpu[0] = drm_amdgpu[test_device_index];
617 			drm_amdgpu[test_device_index] = i;
618 		} else {
619 			fprintf(stderr,
620 				"The specified GPU device does not exist.\n");
621 			exit(EXIT_FAILURE);
622 		}
623 	}
624 
625 	/* Initialize test suites to run */
626 
627 	/* initialize the CUnit test registry */
628 	if (CUE_SUCCESS != CU_initialize_registry()) {
629 		amdgpu_close_devices();
630 		return CU_get_error();
631 	}
632 
633 	/* Register suites. */
634 	if (CU_register_suites(suites) != CUE_SUCCESS) {
635 		fprintf(stderr, "suite registration failed - %s\n",
636 				CU_get_error_msg());
637 		CU_cleanup_registry();
638 		amdgpu_close_devices();
639 		exit(EXIT_FAILURE);
640 	}
641 
642 	/* Run tests using the CUnit Basic interface */
643 	CU_basic_set_mode(CU_BRM_VERBOSE);
644 
645 	/* Disable suites and individual tests based on misc. conditions */
646 	amdgpu_disable_suites();
647 
648 	if (display_list) {
649 		display_test_suites();
650 		goto end;
651 	}
652 
653 	if (suite_id != -1) {	/* If user specify particular suite? */
654 		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
655 						CU_get_registry());
656 
657 		if (pSuite) {
658 
659 			if (force_run)
660 				CU_set_suite_active(pSuite, CU_TRUE);
661 
662 			if (test_id != -1) {   /* If user specify test id */
663 				pTest = CU_get_test_by_index(
664 						(unsigned int) test_id,
665 						pSuite);
666 				if (pTest) {
667 					if (force_run)
668 						CU_set_test_active(pTest, CU_TRUE);
669 
670 					CU_basic_run_test(pSuite, pTest);
671 				}
672 				else {
673 					fprintf(stderr, "Invalid test id: %d\n",
674 								test_id);
675 					CU_cleanup_registry();
676 					amdgpu_close_devices();
677 					exit(EXIT_FAILURE);
678 				}
679 			} else
680 				CU_basic_run_suite(pSuite);
681 		} else {
682 			fprintf(stderr, "Invalid suite id : %d\n",
683 					suite_id);
684 			CU_cleanup_registry();
685 			amdgpu_close_devices();
686 			exit(EXIT_FAILURE);
687 		}
688 	} else
689 		CU_basic_run_tests();
690 
691 end:
692 	CU_cleanup_registry();
693 	amdgpu_close_devices();
694 	return CU_get_error();
695 }
696