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