amdgpu_test.c revision 5324fb0d
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 */
66int drm_amdgpu[MAX_CARDS_SUPPORTED];
67
68/** Open render node to test */
69int open_render_node = 0;	/* By default run most tests on primary node */
70
71/** The table of all known test suites to run */
72static 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
137typedef CU_BOOL (*active__stat_func)(void);
138
139typedef struct Suites_Active_Status {
140	char*             pName;
141	active__stat_func pActive;
142}Suites_Active_Status;
143
144static CU_BOOL always_active()
145{
146	return CU_TRUE;
147}
148
149static 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 */
198static 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 */
244static 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 */
256static const char options[]   = "hlrps:t:b:d:f";
257
258/* Open AMD devices.
259 * Return the number of AMD device opened.
260 */
261static 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 */
333static 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 */
342static 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 */
397static 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
420static 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", 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(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
474			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
475}
476
477/* The main() function for setting up and running the tests.
478 * Returns a CUE_SUCCESS on successful running, another
479 * CUnit error code on failure.
480 */
481int main(int argc, char **argv)
482{
483	int c;			/* Character received from getopt */
484	int i = 0;
485	int suite_id = -1;	/* By default run everything */
486	int test_id  = -1;	/* By default run all tests in the suite */
487	int pci_bus_id = -1;    /* By default PC bus ID is not specified */
488	int pci_device_id = 0;  /* By default PC device ID is zero */
489	int display_devices = 0;/* By default not to display devices' info */
490	CU_pSuite pSuite = NULL;
491	CU_pTest  pTest  = NULL;
492	int test_device_index;
493	int display_list = 0;
494	int force_run = 0;
495
496	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
497		drm_amdgpu[i] = -1;
498
499
500	/* Parse command line string */
501	opterr = 0;		/* Do not print error messages from getopt */
502	while ((c = getopt(argc, argv, options)) != -1) {
503		switch (c) {
504		case 'l':
505			display_list = 1;
506			break;
507		case 's':
508			suite_id = atoi(optarg);
509			break;
510		case 't':
511			test_id = atoi(optarg);
512			break;
513		case 'b':
514			pci_bus_id = atoi(optarg);
515			break;
516		case 'd':
517			sscanf(optarg, "%x", &pci_device_id);
518			break;
519		case 'p':
520			display_devices = 1;
521			break;
522		case 'r':
523			open_render_node = 1;
524			break;
525		case 'f':
526			force_run = 1;
527			break;
528		case '?':
529		case 'h':
530			fprintf(stderr, usage, argv[0]);
531			exit(EXIT_SUCCESS);
532		default:
533			fprintf(stderr, usage, argv[0]);
534			exit(EXIT_FAILURE);
535		}
536	}
537
538	if (amdgpu_open_devices(open_render_node) <= 0) {
539		perror("Cannot open AMDGPU device");
540		exit(EXIT_FAILURE);
541	}
542
543	if (drm_amdgpu[0] < 0) {
544		perror("Cannot open AMDGPU device");
545		exit(EXIT_FAILURE);
546	}
547
548	if (display_devices) {
549		amdgpu_print_devices();
550		amdgpu_close_devices();
551		exit(EXIT_SUCCESS);
552	}
553
554	if (pci_bus_id > 0 || pci_device_id) {
555		/* A device was specified to run the test */
556		test_device_index = amdgpu_find_device(pci_bus_id,
557						       pci_device_id);
558
559		if (test_device_index >= 0) {
560			/* Most tests run on device of drm_amdgpu[0].
561			 * Swap the chosen device to drm_amdgpu[0].
562			 */
563			i = drm_amdgpu[0];
564			drm_amdgpu[0] = drm_amdgpu[test_device_index];
565			drm_amdgpu[test_device_index] = i;
566		} else {
567			fprintf(stderr,
568				"The specified GPU device does not exist.\n");
569			exit(EXIT_FAILURE);
570		}
571	}
572
573	/* Initialize test suites to run */
574
575	/* initialize the CUnit test registry */
576	if (CUE_SUCCESS != CU_initialize_registry()) {
577		amdgpu_close_devices();
578		return CU_get_error();
579	}
580
581	/* Register suites. */
582	if (CU_register_suites(suites) != CUE_SUCCESS) {
583		fprintf(stderr, "suite registration failed - %s\n",
584				CU_get_error_msg());
585		CU_cleanup_registry();
586		amdgpu_close_devices();
587		exit(EXIT_FAILURE);
588	}
589
590	/* Run tests using the CUnit Basic interface */
591	CU_basic_set_mode(CU_BRM_VERBOSE);
592
593	/* Disable suites and individual tests based on misc. conditions */
594	amdgpu_disable_suites();
595
596	if (display_list) {
597		display_test_suites();
598		goto end;
599	}
600
601	if (suite_id != -1) {	/* If user specify particular suite? */
602		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
603						CU_get_registry());
604
605		if (pSuite) {
606
607			if (force_run)
608				CU_set_suite_active(pSuite, CU_TRUE);
609
610			if (test_id != -1) {   /* If user specify test id */
611				pTest = CU_get_test_by_index(
612						(unsigned int) test_id,
613						pSuite);
614				if (pTest) {
615					if (force_run)
616						CU_set_test_active(pTest, CU_TRUE);
617
618					CU_basic_run_test(pSuite, pTest);
619				}
620				else {
621					fprintf(stderr, "Invalid test id: %d\n",
622								test_id);
623					CU_cleanup_registry();
624					amdgpu_close_devices();
625					exit(EXIT_FAILURE);
626				}
627			} else
628				CU_basic_run_suite(pSuite);
629		} else {
630			fprintf(stderr, "Invalid suite id : %d\n",
631					suite_id);
632			CU_cleanup_registry();
633			amdgpu_close_devices();
634			exit(EXIT_FAILURE);
635		}
636	} else
637		CU_basic_run_tests();
638
639end:
640	CU_cleanup_registry();
641	amdgpu_close_devices();
642	return CU_get_error();
643}
644