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("Suites\n");
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 id = %d: Name '%s status: %s'\n",
218 				iSuite + 1, suites[iSuite].pName,
219 				pSuite->fActive ? "ENABLED" : "DISABLED");
220 
221 
222 
223 		for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
224 			iTest++) {
225 
226 			pTest = CU_get_test_by_index((unsigned int) iTest + 1,
227 									pSuite);
228 
229 			if (!pTest) {
230 				fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
231 				continue;
232 			}
233 
234 			printf("Test id %d: Name: '%s status: %s'\n", iTest + 1,
235 					suites[iSuite].pTests[iTest].pName,
236 					pSuite->fActive && pTest->fActive ?
237 						     "ENABLED" : "DISABLED");
238 		}
239 	}
240 }
241 
242 
243 /** Help string for command line parameters */
244 static const char usage[] =
245 	"Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
246 	"[-b <pci_bus_id> [-d <pci_device_id>]]\n"
247 	"where:\n"
248 	"       l - Display all suites and their tests\n"
249 	"       r - Run the tests on render node\n"
250 	"       b - Specify device's PCI bus id to run tests\n"
251 	"       d - Specify device's PCI device id to run tests (optional)\n"
252 	"       p - Display information of AMDGPU devices in system\n"
253 	"       f - Force executing inactive suite or test\n"
254 	"       h - Display this help\n";
255 /** Specified options strings for getopt */
256 static const char options[]   = "hlrps:t:b:d:f";
257 
258 /* Open AMD devices.
259  * Return the number of AMD device opened.
260  */
amdgpu_open_devices(int open_render_node)261 static int amdgpu_open_devices(int open_render_node)
262 {
263 	drmDevicePtr devices[MAX_CARDS_SUPPORTED];
264 	int i;
265 	int drm_node;
266 	int amd_index = 0;
267 	int drm_count;
268 	int fd;
269 	drmVersionPtr version;
270 
271 	drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
272 
273 	if (drm_count < 0) {
274 		fprintf(stderr,
275 			"drmGetDevices2() returned an error %d\n",
276 			drm_count);
277 		return 0;
278 	}
279 
280 	for (i = 0; i < drm_count; i++) {
281 		/* If this is not PCI device, skip*/
282 		if (devices[i]->bustype != DRM_BUS_PCI)
283 			continue;
284 
285 		/* If this is not AMD GPU vender ID, skip*/
286 		if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
287 			continue;
288 
289 		if (open_render_node)
290 			drm_node = DRM_NODE_RENDER;
291 		else
292 			drm_node = DRM_NODE_PRIMARY;
293 
294 		fd = -1;
295 		if (devices[i]->available_nodes & 1 << drm_node)
296 			fd = open(
297 				devices[i]->nodes[drm_node],
298 				O_RDWR | O_CLOEXEC);
299 
300 		/* This node is not available. */
301 		if (fd < 0) continue;
302 
303 		version = drmGetVersion(fd);
304 		if (!version) {
305 			fprintf(stderr,
306 				"Warning: Cannot get version for %s."
307 				"Error is %s\n",
308 				devices[i]->nodes[drm_node],
309 				strerror(errno));
310 			close(fd);
311 			continue;
312 		}
313 
314 		if (strcmp(version->name, "amdgpu")) {
315 			/* This is not AMDGPU driver, skip.*/
316 			drmFreeVersion(version);
317 			close(fd);
318 			continue;
319 		}
320 
321 		drmFreeVersion(version);
322 
323 		drm_amdgpu[amd_index] = fd;
324 		amd_index++;
325 	}
326 
327 	drmFreeDevices(devices, drm_count);
328 	return amd_index;
329 }
330 
331 /* Close AMD devices.
332  */
amdgpu_close_devices()333 static void amdgpu_close_devices()
334 {
335 	int i;
336 	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
337 		if (drm_amdgpu[i] >=0)
338 			close(drm_amdgpu[i]);
339 }
340 
341 /* Print AMD devices information */
amdgpu_print_devices()342 static void amdgpu_print_devices()
343 {
344 	int i;
345 	drmDevicePtr device;
346 
347 	/* Open the first AMD device to print driver information. */
348 	if (drm_amdgpu[0] >=0) {
349 		/* Display AMD driver version information.*/
350 		drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
351 
352 		if (retval == NULL) {
353 			perror("Cannot get version for AMDGPU device");
354 			return;
355 		}
356 
357 		printf("Driver name: %s, Date: %s, Description: %s.\n",
358 			retval->name, retval->date, retval->desc);
359 		drmFreeVersion(retval);
360 	}
361 
362 	/* Display information of AMD devices */
363 	printf("Devices:\n");
364 	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
365 		if (drmGetDevice2(drm_amdgpu[i],
366 			DRM_DEVICE_GET_PCI_REVISION,
367 			&device) == 0) {
368 			if (device->bustype == DRM_BUS_PCI) {
369 				printf("PCI ");
370 				printf(" domain:%04x",
371 					device->businfo.pci->domain);
372 				printf(" bus:%02x",
373 					device->businfo.pci->bus);
374 				printf(" device:%02x",
375 					device->businfo.pci->dev);
376 				printf(" function:%01x",
377 					device->businfo.pci->func);
378 				printf(" vendor_id:%04x",
379 					device->deviceinfo.pci->vendor_id);
380 				printf(" device_id:%04x",
381 					device->deviceinfo.pci->device_id);
382 				printf(" subvendor_id:%04x",
383 					device->deviceinfo.pci->subvendor_id);
384 				printf(" subdevice_id:%04x",
385 					device->deviceinfo.pci->subdevice_id);
386 				printf(" revision_id:%02x",
387 					device->deviceinfo.pci->revision_id);
388 				printf("\n");
389 			}
390 			drmFreeDevice(&device);
391 		}
392 }
393 
394 /* Find a match AMD device in PCI bus
395  * Return the index of the device or -1 if not found
396  */
amdgpu_find_device(uint8_t bus,uint16_t dev)397 static int amdgpu_find_device(uint8_t bus, uint16_t dev)
398 {
399 	int i;
400 	drmDevicePtr device;
401 
402 	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
403 		if (drmGetDevice2(drm_amdgpu[i],
404 			DRM_DEVICE_GET_PCI_REVISION,
405 			&device) == 0) {
406 			if (device->bustype == DRM_BUS_PCI)
407 				if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
408 					device->deviceinfo.pci->device_id == dev) {
409 					drmFreeDevice(&device);
410 					return i;
411 				}
412 
413 			drmFreeDevice(&device);
414 		}
415 	}
416 
417 	return -1;
418 }
419 
amdgpu_disable_suites()420 static void amdgpu_disable_suites()
421 {
422 	amdgpu_device_handle device_handle;
423 	uint32_t major_version, minor_version, family_id;
424 	int i;
425 	int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
426 
427 	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
428 				   &minor_version, &device_handle))
429 		return;
430 
431 	family_id = device_handle->info.family_id;
432 
433 	if (amdgpu_device_deinitialize(device_handle))
434 		return;
435 
436 	/* Set active status for suites based on their policies */
437 	for (i = 0; i < size; ++i)
438 		if (amdgpu_set_suite_active(suites_active_stat[i].pName,
439 				suites_active_stat[i].pActive()))
440 			fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
441 
442 	/* Explicitly disable specific tests due to known bugs or preferences */
443 	/*
444 	* BUG: Compute ring stalls and never recovers when the address is
445 	* written after the command already submitted
446 	*/
447 	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
448 			"compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
449 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
450 
451 	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
452 				"sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
453 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
454 
455 	if (amdgpu_set_test_active(BO_TESTS_STR, "Metadata", CU_FALSE))
456 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
457 
458 	if (amdgpu_set_test_active(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
459 		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
460 
461 	/* This test was ran on GFX8 and GFX9 only */
462 	if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
463 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
464 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
465 
466 	/* This test was ran on GFX9 only */
467 	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
468 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
469 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
470 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
471 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
472 	}
473 
474 	/* This test was ran on GFX9 only */
475 	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
476 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
477 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
478 
479 	/* This test was ran on GFX9 only */
480 	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
481 		if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
482 			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
483 }
484 
485 /* The main() function for setting up and running the tests.
486  * Returns a CUE_SUCCESS on successful running, another
487  * CUnit error code on failure.
488  */
main(int argc,char ** argv)489 int main(int argc, char **argv)
490 {
491 	int c;			/* Character received from getopt */
492 	int i = 0;
493 	int suite_id = -1;	/* By default run everything */
494 	int test_id  = -1;	/* By default run all tests in the suite */
495 	int pci_bus_id = -1;    /* By default PC bus ID is not specified */
496 	int pci_device_id = 0;  /* By default PC device ID is zero */
497 	int display_devices = 0;/* By default not to display devices' info */
498 	CU_pSuite pSuite = NULL;
499 	CU_pTest  pTest  = NULL;
500 	int test_device_index;
501 	int display_list = 0;
502 	int force_run = 0;
503 
504 	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
505 		drm_amdgpu[i] = -1;
506 
507 
508 	/* Parse command line string */
509 	opterr = 0;		/* Do not print error messages from getopt */
510 	while ((c = getopt(argc, argv, options)) != -1) {
511 		switch (c) {
512 		case 'l':
513 			display_list = 1;
514 			break;
515 		case 's':
516 			suite_id = atoi(optarg);
517 			break;
518 		case 't':
519 			test_id = atoi(optarg);
520 			break;
521 		case 'b':
522 			pci_bus_id = atoi(optarg);
523 			break;
524 		case 'd':
525 			sscanf(optarg, "%x", &pci_device_id);
526 			break;
527 		case 'p':
528 			display_devices = 1;
529 			break;
530 		case 'r':
531 			open_render_node = 1;
532 			break;
533 		case 'f':
534 			force_run = 1;
535 			break;
536 		case '?':
537 		case 'h':
538 			fprintf(stderr, usage, argv[0]);
539 			exit(EXIT_SUCCESS);
540 		default:
541 			fprintf(stderr, usage, argv[0]);
542 			exit(EXIT_FAILURE);
543 		}
544 	}
545 
546 	if (amdgpu_open_devices(open_render_node) <= 0) {
547 		perror("Cannot open AMDGPU device");
548 		exit(EXIT_FAILURE);
549 	}
550 
551 	if (drm_amdgpu[0] < 0) {
552 		perror("Cannot open AMDGPU device");
553 		exit(EXIT_FAILURE);
554 	}
555 
556 	if (display_devices) {
557 		amdgpu_print_devices();
558 		amdgpu_close_devices();
559 		exit(EXIT_SUCCESS);
560 	}
561 
562 	if (pci_bus_id > 0 || pci_device_id) {
563 		/* A device was specified to run the test */
564 		test_device_index = amdgpu_find_device(pci_bus_id,
565 						       pci_device_id);
566 
567 		if (test_device_index >= 0) {
568 			/* Most tests run on device of drm_amdgpu[0].
569 			 * Swap the chosen device to drm_amdgpu[0].
570 			 */
571 			i = drm_amdgpu[0];
572 			drm_amdgpu[0] = drm_amdgpu[test_device_index];
573 			drm_amdgpu[test_device_index] = i;
574 		} else {
575 			fprintf(stderr,
576 				"The specified GPU device does not exist.\n");
577 			exit(EXIT_FAILURE);
578 		}
579 	}
580 
581 	/* Initialize test suites to run */
582 
583 	/* initialize the CUnit test registry */
584 	if (CUE_SUCCESS != CU_initialize_registry()) {
585 		amdgpu_close_devices();
586 		return CU_get_error();
587 	}
588 
589 	/* Register suites. */
590 	if (CU_register_suites(suites) != CUE_SUCCESS) {
591 		fprintf(stderr, "suite registration failed - %s\n",
592 				CU_get_error_msg());
593 		CU_cleanup_registry();
594 		amdgpu_close_devices();
595 		exit(EXIT_FAILURE);
596 	}
597 
598 	/* Run tests using the CUnit Basic interface */
599 	CU_basic_set_mode(CU_BRM_VERBOSE);
600 
601 	/* Disable suites and individual tests based on misc. conditions */
602 	amdgpu_disable_suites();
603 
604 	if (display_list) {
605 		display_test_suites();
606 		goto end;
607 	}
608 
609 	if (suite_id != -1) {	/* If user specify particular suite? */
610 		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
611 						CU_get_registry());
612 
613 		if (pSuite) {
614 
615 			if (force_run)
616 				CU_set_suite_active(pSuite, CU_TRUE);
617 
618 			if (test_id != -1) {   /* If user specify test id */
619 				pTest = CU_get_test_by_index(
620 						(unsigned int) test_id,
621 						pSuite);
622 				if (pTest) {
623 					if (force_run)
624 						CU_set_test_active(pTest, CU_TRUE);
625 
626 					CU_basic_run_test(pSuite, pTest);
627 				}
628 				else {
629 					fprintf(stderr, "Invalid test id: %d\n",
630 								test_id);
631 					CU_cleanup_registry();
632 					amdgpu_close_devices();
633 					exit(EXIT_FAILURE);
634 				}
635 			} else
636 				CU_basic_run_suite(pSuite);
637 		} else {
638 			fprintf(stderr, "Invalid suite id : %d\n",
639 					suite_id);
640 			CU_cleanup_registry();
641 			amdgpu_close_devices();
642 			exit(EXIT_FAILURE);
643 		}
644 	} else
645 		CU_basic_run_tests();
646 
647 end:
648 	CU_cleanup_registry();
649 	amdgpu_close_devices();
650 	return CU_get_error();
651 }
652