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