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