amdgpu_test.c revision 41687f09
13f012e29Smrg/*
23f012e29Smrg * Copyright 2014 Advanced Micro Devices, Inc.
33f012e29Smrg *
43f012e29Smrg * Permission is hereby granted, free of charge, to any person obtaining a
53f012e29Smrg * copy of this software and associated documentation files (the "Software"),
63f012e29Smrg * to deal in the Software without restriction, including without limitation
73f012e29Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83f012e29Smrg * and/or sell copies of the Software, and to permit persons to whom the
93f012e29Smrg * Software is furnished to do so, subject to the following conditions:
103f012e29Smrg *
113f012e29Smrg * The above copyright notice and this permission notice shall be included in
123f012e29Smrg * all copies or substantial portions of the Software.
133f012e29Smrg *
143f012e29Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
153f012e29Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
163f012e29Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
173f012e29Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
183f012e29Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193f012e29Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
203f012e29Smrg * OTHER DEALINGS IN THE SOFTWARE.
213f012e29Smrg *
223f012e29Smrg*/
233f012e29Smrg
243f012e29Smrg#include <string.h>
253f012e29Smrg#include <stdio.h>
263f012e29Smrg#include <stdlib.h>
273f012e29Smrg#include <unistd.h>
283f012e29Smrg#include <string.h>
293f012e29Smrg#include <ctype.h>
303f012e29Smrg#include <fcntl.h>
313f012e29Smrg#include <errno.h>
323f012e29Smrg#include <signal.h>
333f012e29Smrg#include <time.h>
343f012e29Smrg#include <sys/types.h>
353f012e29Smrg#include <sys/stat.h>
363f012e29Smrg#include <sys/ioctl.h>
373f012e29Smrg#include <sys/time.h>
383f012e29Smrg#include <stdarg.h>
393f012e29Smrg#include <stdint.h>
403f012e29Smrg
413f012e29Smrg#include "drm.h"
423f012e29Smrg#include "xf86drmMode.h"
433f012e29Smrg#include "xf86drm.h"
443f012e29Smrg
453f012e29Smrg#include "CUnit/Basic.h"
463f012e29Smrg
473f012e29Smrg#include "amdgpu_test.h"
4800a23bdaSmrg#include "amdgpu_internal.h"
4900a23bdaSmrg
5000a23bdaSmrg/* Test suite names */
5100a23bdaSmrg#define BASIC_TESTS_STR "Basic Tests"
5200a23bdaSmrg#define BO_TESTS_STR "BO Tests"
5300a23bdaSmrg#define CS_TESTS_STR "CS Tests"
5400a23bdaSmrg#define VCE_TESTS_STR "VCE Tests"
5500a23bdaSmrg#define VCN_TESTS_STR "VCN Tests"
5600a23bdaSmrg#define UVD_ENC_TESTS_STR "UVD ENC Tests"
5700a23bdaSmrg#define DEADLOCK_TESTS_STR "Deadlock Tests"
5800a23bdaSmrg#define VM_TESTS_STR "VM Tests"
595324fb0dSmrg#define RAS_TESTS_STR "RAS Tests"
605324fb0dSmrg#define SYNCOBJ_TIMELINE_TESTS_STR "SYNCOBJ TIMELINE Tests"
6141687f09Smrg#define SECURITY_TESTS_STR "Security Tests"
623f012e29Smrg
633f012e29Smrg/**
643f012e29Smrg *  Open handles for amdgpu devices
653f012e29Smrg *
663f012e29Smrg */
673f012e29Smrgint drm_amdgpu[MAX_CARDS_SUPPORTED];
683f012e29Smrg
69037b3c26Smrg/** Open render node to test */
70037b3c26Smrgint open_render_node = 0;	/* By default run most tests on primary node */
71037b3c26Smrg
723f012e29Smrg/** The table of all known test suites to run */
733f012e29Smrgstatic CU_SuiteInfo suites[] = {
743f012e29Smrg	{
7500a23bdaSmrg		.pName = BASIC_TESTS_STR,
763f012e29Smrg		.pInitFunc = suite_basic_tests_init,
773f012e29Smrg		.pCleanupFunc = suite_basic_tests_clean,
783f012e29Smrg		.pTests = basic_tests,
793f012e29Smrg	},
803f012e29Smrg	{
8100a23bdaSmrg		.pName = BO_TESTS_STR,
823f012e29Smrg		.pInitFunc = suite_bo_tests_init,
833f012e29Smrg		.pCleanupFunc = suite_bo_tests_clean,
843f012e29Smrg		.pTests = bo_tests,
853f012e29Smrg	},
863f012e29Smrg	{
8700a23bdaSmrg		.pName = CS_TESTS_STR,
883f012e29Smrg		.pInitFunc = suite_cs_tests_init,
893f012e29Smrg		.pCleanupFunc = suite_cs_tests_clean,
903f012e29Smrg		.pTests = cs_tests,
913f012e29Smrg	},
923f012e29Smrg	{
9300a23bdaSmrg		.pName = VCE_TESTS_STR,
943f012e29Smrg		.pInitFunc = suite_vce_tests_init,
953f012e29Smrg		.pCleanupFunc = suite_vce_tests_clean,
963f012e29Smrg		.pTests = vce_tests,
973f012e29Smrg	},
98d8807b2fSmrg	{
9900a23bdaSmrg		.pName = VCN_TESTS_STR,
100d8807b2fSmrg		.pInitFunc = suite_vcn_tests_init,
101d8807b2fSmrg		.pCleanupFunc = suite_vcn_tests_clean,
102d8807b2fSmrg		.pTests = vcn_tests,
103d8807b2fSmrg	},
104d8807b2fSmrg	{
10500a23bdaSmrg		.pName = UVD_ENC_TESTS_STR,
106d8807b2fSmrg		.pInitFunc = suite_uvd_enc_tests_init,
107d8807b2fSmrg		.pCleanupFunc = suite_uvd_enc_tests_clean,
108d8807b2fSmrg		.pTests = uvd_enc_tests,
109d8807b2fSmrg	},
11000a23bdaSmrg	{
11100a23bdaSmrg		.pName = DEADLOCK_TESTS_STR,
11200a23bdaSmrg		.pInitFunc = suite_deadlock_tests_init,
11300a23bdaSmrg		.pCleanupFunc = suite_deadlock_tests_clean,
11400a23bdaSmrg		.pTests = deadlock_tests,
11500a23bdaSmrg	},
11600a23bdaSmrg	{
11700a23bdaSmrg		.pName = VM_TESTS_STR,
11800a23bdaSmrg		.pInitFunc = suite_vm_tests_init,
11900a23bdaSmrg		.pCleanupFunc = suite_vm_tests_clean,
12000a23bdaSmrg		.pTests = vm_tests,
12100a23bdaSmrg	},
1225324fb0dSmrg	{
1235324fb0dSmrg		.pName = RAS_TESTS_STR,
1245324fb0dSmrg		.pInitFunc = suite_ras_tests_init,
1255324fb0dSmrg		.pCleanupFunc = suite_ras_tests_clean,
1265324fb0dSmrg		.pTests = ras_tests,
1275324fb0dSmrg	},
1285324fb0dSmrg	{
1295324fb0dSmrg		.pName = SYNCOBJ_TIMELINE_TESTS_STR,
1305324fb0dSmrg		.pInitFunc = suite_syncobj_timeline_tests_init,
1315324fb0dSmrg		.pCleanupFunc = suite_syncobj_timeline_tests_clean,
1325324fb0dSmrg		.pTests = syncobj_timeline_tests,
1335324fb0dSmrg	},
13441687f09Smrg	{
13541687f09Smrg		.pName = SECURITY_TESTS_STR,
13641687f09Smrg		.pInitFunc = suite_security_tests_init,
13741687f09Smrg		.pCleanupFunc = suite_security_tests_clean,
13841687f09Smrg		.pTests = security_tests,
13941687f09Smrg	},
14000a23bdaSmrg
1413f012e29Smrg	CU_SUITE_INFO_NULL,
1423f012e29Smrg};
1433f012e29Smrg
14400a23bdaSmrgtypedef CU_BOOL (*active__stat_func)(void);
14500a23bdaSmrg
14600a23bdaSmrgtypedef struct Suites_Active_Status {
14700a23bdaSmrg	char*             pName;
14800a23bdaSmrg	active__stat_func pActive;
14900a23bdaSmrg}Suites_Active_Status;
1503f012e29Smrg
15100a23bdaSmrgstatic CU_BOOL always_active()
15200a23bdaSmrg{
15300a23bdaSmrg	return CU_TRUE;
15400a23bdaSmrg}
15500a23bdaSmrg
15600a23bdaSmrgstatic Suites_Active_Status suites_active_stat[] = {
15700a23bdaSmrg		{
15800a23bdaSmrg			.pName = BASIC_TESTS_STR,
15941687f09Smrg			.pActive = suite_basic_tests_enable,
16000a23bdaSmrg		},
16100a23bdaSmrg		{
16200a23bdaSmrg			.pName = BO_TESTS_STR,
16300a23bdaSmrg			.pActive = always_active,
16400a23bdaSmrg		},
16500a23bdaSmrg		{
16600a23bdaSmrg			.pName = CS_TESTS_STR,
16700a23bdaSmrg			.pActive = suite_cs_tests_enable,
16800a23bdaSmrg		},
16900a23bdaSmrg		{
17000a23bdaSmrg			.pName = VCE_TESTS_STR,
17100a23bdaSmrg			.pActive = suite_vce_tests_enable,
17200a23bdaSmrg		},
17300a23bdaSmrg		{
17400a23bdaSmrg			.pName = VCN_TESTS_STR,
17500a23bdaSmrg			.pActive = suite_vcn_tests_enable,
17600a23bdaSmrg		},
17700a23bdaSmrg		{
17800a23bdaSmrg			.pName = UVD_ENC_TESTS_STR,
17900a23bdaSmrg			.pActive = suite_uvd_enc_tests_enable,
18000a23bdaSmrg		},
18100a23bdaSmrg		{
18200a23bdaSmrg			.pName = DEADLOCK_TESTS_STR,
18300a23bdaSmrg			.pActive = suite_deadlock_tests_enable,
18400a23bdaSmrg		},
18500a23bdaSmrg		{
18600a23bdaSmrg			.pName = VM_TESTS_STR,
18700a23bdaSmrg			.pActive = suite_vm_tests_enable,
18800a23bdaSmrg		},
1895324fb0dSmrg		{
1905324fb0dSmrg			.pName = RAS_TESTS_STR,
1915324fb0dSmrg			.pActive = suite_ras_tests_enable,
1925324fb0dSmrg		},
1935324fb0dSmrg		{
1945324fb0dSmrg			.pName = SYNCOBJ_TIMELINE_TESTS_STR,
1955324fb0dSmrg			.pActive = suite_syncobj_timeline_tests_enable,
1965324fb0dSmrg		},
19741687f09Smrg		{
19841687f09Smrg			.pName = SECURITY_TESTS_STR,
19941687f09Smrg			.pActive = suite_security_tests_enable,
20041687f09Smrg		},
20100a23bdaSmrg};
20200a23bdaSmrg
20300a23bdaSmrg
20400a23bdaSmrg/*
20500a23bdaSmrg * Display information about all  suites and their tests
20600a23bdaSmrg *
20700a23bdaSmrg * NOTE: Must be run after registry is initialized and suites registered.
20800a23bdaSmrg */
2093f012e29Smrgstatic void display_test_suites(void)
2103f012e29Smrg{
2113f012e29Smrg	int iSuite;
2123f012e29Smrg	int iTest;
21300a23bdaSmrg	CU_pSuite pSuite = NULL;
21400a23bdaSmrg	CU_pTest  pTest  = NULL;
2153f012e29Smrg
2169bd392adSmrg	printf("%5s: %2s: %8s: %s\n", "What", "ID", "Status", "Name");
2173f012e29Smrg
2183f012e29Smrg	for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
21900a23bdaSmrg
22000a23bdaSmrg		pSuite = CU_get_suite_by_index((unsigned int) iSuite + 1,
2219bd392adSmrg					       CU_get_registry());
22200a23bdaSmrg
22300a23bdaSmrg		if (!pSuite) {
22400a23bdaSmrg			fprintf(stderr, "Invalid suite id : %d\n", iSuite + 1);
22500a23bdaSmrg			continue;
22600a23bdaSmrg		}
22700a23bdaSmrg
2289bd392adSmrg		printf("Suite: %2d: %8s: %s\n",
2299bd392adSmrg		       iSuite + 1,
2309bd392adSmrg		       pSuite->fActive ? "ENABLED" : "DISABLED",
2319bd392adSmrg		       suites[iSuite].pName);
23200a23bdaSmrg
2339bd392adSmrg		if (!pSuite->fActive)
2349bd392adSmrg			continue;
2353f012e29Smrg
2363f012e29Smrg		for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
2379bd392adSmrg		     iTest++) {
23800a23bdaSmrg			pTest = CU_get_test_by_index((unsigned int) iTest + 1,
2399bd392adSmrg						     pSuite);
24000a23bdaSmrg			if (!pTest) {
24100a23bdaSmrg				fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
24200a23bdaSmrg				continue;
24300a23bdaSmrg			}
2449bd392adSmrg			printf(" Test: %2d: %8s: %s\n",
2459bd392adSmrg			       iTest + 1,
2469bd392adSmrg			       pSuite->fActive && pTest->fActive ? "ENABLED" : "DISABLED",
2479bd392adSmrg			       suites[iSuite].pTests[iTest].pName);
2483f012e29Smrg		}
2493f012e29Smrg	}
2503f012e29Smrg}
2513f012e29Smrg
2523f012e29Smrg/** Help string for command line parameters */
253037b3c26Smrgstatic const char usage[] =
25400a23bdaSmrg	"Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
255037b3c26Smrg	"[-b <pci_bus_id> [-d <pci_device_id>]]\n"
256037b3c26Smrg	"where:\n"
257037b3c26Smrg	"       l - Display all suites and their tests\n"
258037b3c26Smrg	"       r - Run the tests on render node\n"
259037b3c26Smrg	"       b - Specify device's PCI bus id to run tests\n"
260037b3c26Smrg	"       d - Specify device's PCI device id to run tests (optional)\n"
261037b3c26Smrg	"       p - Display information of AMDGPU devices in system\n"
26200a23bdaSmrg	"       f - Force executing inactive suite or test\n"
263037b3c26Smrg	"       h - Display this help\n";
2643f012e29Smrg/** Specified options strings for getopt */
26500a23bdaSmrgstatic const char options[]   = "hlrps:t:b:d:f";
266037b3c26Smrg
267037b3c26Smrg/* Open AMD devices.
2685324fb0dSmrg * Return the number of AMD device opened.
269037b3c26Smrg */
270037b3c26Smrgstatic int amdgpu_open_devices(int open_render_node)
271037b3c26Smrg{
272037b3c26Smrg	drmDevicePtr devices[MAX_CARDS_SUPPORTED];
273037b3c26Smrg	int i;
274037b3c26Smrg	int drm_node;
275037b3c26Smrg	int amd_index = 0;
276037b3c26Smrg	int drm_count;
277037b3c26Smrg	int fd;
278037b3c26Smrg	drmVersionPtr version;
279037b3c26Smrg
280037b3c26Smrg	drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
281037b3c26Smrg
282037b3c26Smrg	if (drm_count < 0) {
283037b3c26Smrg		fprintf(stderr,
284037b3c26Smrg			"drmGetDevices2() returned an error %d\n",
285037b3c26Smrg			drm_count);
286037b3c26Smrg		return 0;
287037b3c26Smrg	}
288037b3c26Smrg
289037b3c26Smrg	for (i = 0; i < drm_count; i++) {
290037b3c26Smrg		/* If this is not PCI device, skip*/
291037b3c26Smrg		if (devices[i]->bustype != DRM_BUS_PCI)
292037b3c26Smrg			continue;
293037b3c26Smrg
294037b3c26Smrg		/* If this is not AMD GPU vender ID, skip*/
295037b3c26Smrg		if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
296037b3c26Smrg			continue;
297037b3c26Smrg
298037b3c26Smrg		if (open_render_node)
299037b3c26Smrg			drm_node = DRM_NODE_RENDER;
300037b3c26Smrg		else
301037b3c26Smrg			drm_node = DRM_NODE_PRIMARY;
302037b3c26Smrg
303037b3c26Smrg		fd = -1;
304037b3c26Smrg		if (devices[i]->available_nodes & 1 << drm_node)
305037b3c26Smrg			fd = open(
306037b3c26Smrg				devices[i]->nodes[drm_node],
307037b3c26Smrg				O_RDWR | O_CLOEXEC);
308037b3c26Smrg
309037b3c26Smrg		/* This node is not available. */
310037b3c26Smrg		if (fd < 0) continue;
311037b3c26Smrg
312037b3c26Smrg		version = drmGetVersion(fd);
313037b3c26Smrg		if (!version) {
314037b3c26Smrg			fprintf(stderr,
315037b3c26Smrg				"Warning: Cannot get version for %s."
316037b3c26Smrg				"Error is %s\n",
317037b3c26Smrg				devices[i]->nodes[drm_node],
318037b3c26Smrg				strerror(errno));
319037b3c26Smrg			close(fd);
320037b3c26Smrg			continue;
321037b3c26Smrg		}
322037b3c26Smrg
323037b3c26Smrg		if (strcmp(version->name, "amdgpu")) {
324037b3c26Smrg			/* This is not AMDGPU driver, skip.*/
325037b3c26Smrg			drmFreeVersion(version);
326037b3c26Smrg			close(fd);
327037b3c26Smrg			continue;
328037b3c26Smrg		}
329037b3c26Smrg
330037b3c26Smrg		drmFreeVersion(version);
331037b3c26Smrg
332037b3c26Smrg		drm_amdgpu[amd_index] = fd;
333037b3c26Smrg		amd_index++;
334037b3c26Smrg	}
335037b3c26Smrg
336037b3c26Smrg	drmFreeDevices(devices, drm_count);
337037b3c26Smrg	return amd_index;
338037b3c26Smrg}
339037b3c26Smrg
340037b3c26Smrg/* Close AMD devices.
341037b3c26Smrg */
342037b3c26Smrgstatic void amdgpu_close_devices()
343037b3c26Smrg{
344037b3c26Smrg	int i;
345037b3c26Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
346037b3c26Smrg		if (drm_amdgpu[i] >=0)
347037b3c26Smrg			close(drm_amdgpu[i]);
348037b3c26Smrg}
349037b3c26Smrg
350037b3c26Smrg/* Print AMD devices information */
351037b3c26Smrgstatic void amdgpu_print_devices()
352037b3c26Smrg{
353037b3c26Smrg	int i;
354037b3c26Smrg	drmDevicePtr device;
355037b3c26Smrg
3565324fb0dSmrg	/* Open the first AMD device to print driver information. */
357037b3c26Smrg	if (drm_amdgpu[0] >=0) {
358037b3c26Smrg		/* Display AMD driver version information.*/
359037b3c26Smrg		drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
360037b3c26Smrg
361037b3c26Smrg		if (retval == NULL) {
362037b3c26Smrg			perror("Cannot get version for AMDGPU device");
363037b3c26Smrg			return;
364037b3c26Smrg		}
365037b3c26Smrg
366037b3c26Smrg		printf("Driver name: %s, Date: %s, Description: %s.\n",
367037b3c26Smrg			retval->name, retval->date, retval->desc);
368037b3c26Smrg		drmFreeVersion(retval);
369037b3c26Smrg	}
370037b3c26Smrg
371037b3c26Smrg	/* Display information of AMD devices */
372037b3c26Smrg	printf("Devices:\n");
373037b3c26Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
374037b3c26Smrg		if (drmGetDevice2(drm_amdgpu[i],
375037b3c26Smrg			DRM_DEVICE_GET_PCI_REVISION,
376037b3c26Smrg			&device) == 0) {
377037b3c26Smrg			if (device->bustype == DRM_BUS_PCI) {
378037b3c26Smrg				printf("PCI ");
379037b3c26Smrg				printf(" domain:%04x",
380037b3c26Smrg					device->businfo.pci->domain);
381037b3c26Smrg				printf(" bus:%02x",
382037b3c26Smrg					device->businfo.pci->bus);
383037b3c26Smrg				printf(" device:%02x",
384037b3c26Smrg					device->businfo.pci->dev);
385037b3c26Smrg				printf(" function:%01x",
386037b3c26Smrg					device->businfo.pci->func);
387037b3c26Smrg				printf(" vendor_id:%04x",
388037b3c26Smrg					device->deviceinfo.pci->vendor_id);
389037b3c26Smrg				printf(" device_id:%04x",
390037b3c26Smrg					device->deviceinfo.pci->device_id);
391037b3c26Smrg				printf(" subvendor_id:%04x",
392037b3c26Smrg					device->deviceinfo.pci->subvendor_id);
393037b3c26Smrg				printf(" subdevice_id:%04x",
394037b3c26Smrg					device->deviceinfo.pci->subdevice_id);
395037b3c26Smrg				printf(" revision_id:%02x",
396037b3c26Smrg					device->deviceinfo.pci->revision_id);
397037b3c26Smrg				printf("\n");
398037b3c26Smrg			}
399037b3c26Smrg			drmFreeDevice(&device);
400037b3c26Smrg		}
401037b3c26Smrg}
402037b3c26Smrg
403037b3c26Smrg/* Find a match AMD device in PCI bus
404037b3c26Smrg * Return the index of the device or -1 if not found
405037b3c26Smrg */
406d8807b2fSmrgstatic int amdgpu_find_device(uint8_t bus, uint16_t dev)
407037b3c26Smrg{
408037b3c26Smrg	int i;
409037b3c26Smrg	drmDevicePtr device;
410037b3c26Smrg
411d8807b2fSmrg	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
412037b3c26Smrg		if (drmGetDevice2(drm_amdgpu[i],
413037b3c26Smrg			DRM_DEVICE_GET_PCI_REVISION,
414037b3c26Smrg			&device) == 0) {
415037b3c26Smrg			if (device->bustype == DRM_BUS_PCI)
416d8807b2fSmrg				if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
417d8807b2fSmrg					device->deviceinfo.pci->device_id == dev) {
418037b3c26Smrg					drmFreeDevice(&device);
419037b3c26Smrg					return i;
420037b3c26Smrg				}
421037b3c26Smrg
422037b3c26Smrg			drmFreeDevice(&device);
423037b3c26Smrg		}
424d8807b2fSmrg	}
425037b3c26Smrg
426037b3c26Smrg	return -1;
427037b3c26Smrg}
4283f012e29Smrg
42900a23bdaSmrgstatic void amdgpu_disable_suites()
43000a23bdaSmrg{
43100a23bdaSmrg	amdgpu_device_handle device_handle;
43200a23bdaSmrg	uint32_t major_version, minor_version, family_id;
43300a23bdaSmrg	int i;
43400a23bdaSmrg	int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
43500a23bdaSmrg
43600a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
43700a23bdaSmrg				   &minor_version, &device_handle))
43800a23bdaSmrg		return;
43900a23bdaSmrg
44000a23bdaSmrg	family_id = device_handle->info.family_id;
44100a23bdaSmrg
44200a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
44300a23bdaSmrg		return;
44400a23bdaSmrg
44500a23bdaSmrg	/* Set active status for suites based on their policies */
44600a23bdaSmrg	for (i = 0; i < size; ++i)
44700a23bdaSmrg		if (amdgpu_set_suite_active(suites_active_stat[i].pName,
44800a23bdaSmrg				suites_active_stat[i].pActive()))
44900a23bdaSmrg			fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
45000a23bdaSmrg
45100a23bdaSmrg	/* Explicitly disable specific tests due to known bugs or preferences */
45200a23bdaSmrg	/*
45300a23bdaSmrg	* BUG: Compute ring stalls and never recovers when the address is
45400a23bdaSmrg	* written after the command already submitted
45500a23bdaSmrg	*/
4566532f28eSmrg	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4576532f28eSmrg			"compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
45800a23bdaSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
45900a23bdaSmrg
4605324fb0dSmrg	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4615324fb0dSmrg				"sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4625324fb0dSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4635324fb0dSmrg
4649bd392adSmrg	/* This test was ran on GFX9 only */
4659bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4669bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4679bd392adSmrg				"gfx ring bad dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4689bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4699bd392adSmrg
4709bd392adSmrg	/* This test was ran on GFX9 only */
4719bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4729bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4739bd392adSmrg				"compute ring bad dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
4749bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4759bd392adSmrg
4769bd392adSmrg	/* This test was ran on GFX9 only */
4779bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4789bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4799bd392adSmrg				"gfx ring bad slow dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4809bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4819bd392adSmrg
4829bd392adSmrg	/* This test was ran on GFX9 only */
4839bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4849bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4859bd392adSmrg				"compute ring bad slow dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
4869bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4879bd392adSmrg
4889bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4899bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4909bd392adSmrg				"gfx ring bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4919bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4929bd392adSmrg
4939bd392adSmrg	/* This test was ran on GFX9 only */
4949bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4959bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4969bd392adSmrg				"gfx ring slow bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4979bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4989bd392adSmrg
49900a23bdaSmrg	/* This test was ran on GFX8 and GFX9 only */
50000a23bdaSmrg	if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
50100a23bdaSmrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
50200a23bdaSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5035324fb0dSmrg
5045324fb0dSmrg	/* This test was ran on GFX9 only */
50588f8a8d2Smrg	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
50688f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
50788f8a8d2Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
50888f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
5095324fb0dSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
51088f8a8d2Smrg	}
5115324fb0dSmrg
5125324fb0dSmrg	/* This test was ran on GFX9 only */
5135324fb0dSmrg	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5145324fb0dSmrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
5155324fb0dSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
51688f8a8d2Smrg
51788f8a8d2Smrg	/* This test was ran on GFX9 only */
51888f8a8d2Smrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
51988f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
52088f8a8d2Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
52100a23bdaSmrg}
52200a23bdaSmrg
5233f012e29Smrg/* The main() function for setting up and running the tests.
5243f012e29Smrg * Returns a CUE_SUCCESS on successful running, another
5253f012e29Smrg * CUnit error code on failure.
5263f012e29Smrg */
5273f012e29Smrgint main(int argc, char **argv)
5283f012e29Smrg{
5293f012e29Smrg	int c;			/* Character received from getopt */
5303f012e29Smrg	int i = 0;
5313f012e29Smrg	int suite_id = -1;	/* By default run everything */
5323f012e29Smrg	int test_id  = -1;	/* By default run all tests in the suite */
533037b3c26Smrg	int pci_bus_id = -1;    /* By default PC bus ID is not specified */
534037b3c26Smrg	int pci_device_id = 0;  /* By default PC device ID is zero */
535037b3c26Smrg	int display_devices = 0;/* By default not to display devices' info */
5363f012e29Smrg	CU_pSuite pSuite = NULL;
5373f012e29Smrg	CU_pTest  pTest  = NULL;
538037b3c26Smrg	int test_device_index;
53900a23bdaSmrg	int display_list = 0;
54000a23bdaSmrg	int force_run = 0;
5413f012e29Smrg
5423f012e29Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
5433f012e29Smrg		drm_amdgpu[i] = -1;
5443f012e29Smrg
5453f012e29Smrg
5463f012e29Smrg	/* Parse command line string */
5473f012e29Smrg	opterr = 0;		/* Do not print error messages from getopt */
5483f012e29Smrg	while ((c = getopt(argc, argv, options)) != -1) {
5493f012e29Smrg		switch (c) {
5503f012e29Smrg		case 'l':
55100a23bdaSmrg			display_list = 1;
55200a23bdaSmrg			break;
5533f012e29Smrg		case 's':
5543f012e29Smrg			suite_id = atoi(optarg);
5553f012e29Smrg			break;
5563f012e29Smrg		case 't':
5573f012e29Smrg			test_id = atoi(optarg);
5583f012e29Smrg			break;
559037b3c26Smrg		case 'b':
560037b3c26Smrg			pci_bus_id = atoi(optarg);
561037b3c26Smrg			break;
562037b3c26Smrg		case 'd':
563d8807b2fSmrg			sscanf(optarg, "%x", &pci_device_id);
564037b3c26Smrg			break;
565037b3c26Smrg		case 'p':
566037b3c26Smrg			display_devices = 1;
567037b3c26Smrg			break;
568037b3c26Smrg		case 'r':
569037b3c26Smrg			open_render_node = 1;
570037b3c26Smrg			break;
57100a23bdaSmrg		case 'f':
57200a23bdaSmrg			force_run = 1;
57300a23bdaSmrg			break;
5743f012e29Smrg		case '?':
5753f012e29Smrg		case 'h':
5763f012e29Smrg			fprintf(stderr, usage, argv[0]);
5773f012e29Smrg			exit(EXIT_SUCCESS);
5783f012e29Smrg		default:
5793f012e29Smrg			fprintf(stderr, usage, argv[0]);
5803f012e29Smrg			exit(EXIT_FAILURE);
5813f012e29Smrg		}
5823f012e29Smrg	}
5833f012e29Smrg
584037b3c26Smrg	if (amdgpu_open_devices(open_render_node) <= 0) {
585037b3c26Smrg		perror("Cannot open AMDGPU device");
5863f012e29Smrg		exit(EXIT_FAILURE);
5873f012e29Smrg	}
5883f012e29Smrg
589037b3c26Smrg	if (drm_amdgpu[0] < 0) {
590037b3c26Smrg		perror("Cannot open AMDGPU device");
5913f012e29Smrg		exit(EXIT_FAILURE);
5923f012e29Smrg	}
5933f012e29Smrg
594037b3c26Smrg	if (display_devices) {
595037b3c26Smrg		amdgpu_print_devices();
596037b3c26Smrg		amdgpu_close_devices();
597037b3c26Smrg		exit(EXIT_SUCCESS);
598037b3c26Smrg	}
5993f012e29Smrg
600d8807b2fSmrg	if (pci_bus_id > 0 || pci_device_id) {
601037b3c26Smrg		/* A device was specified to run the test */
602d8807b2fSmrg		test_device_index = amdgpu_find_device(pci_bus_id,
603d8807b2fSmrg						       pci_device_id);
604037b3c26Smrg
605037b3c26Smrg		if (test_device_index >= 0) {
606037b3c26Smrg			/* Most tests run on device of drm_amdgpu[0].
607037b3c26Smrg			 * Swap the chosen device to drm_amdgpu[0].
608037b3c26Smrg			 */
609037b3c26Smrg			i = drm_amdgpu[0];
610037b3c26Smrg			drm_amdgpu[0] = drm_amdgpu[test_device_index];
611037b3c26Smrg			drm_amdgpu[test_device_index] = i;
612037b3c26Smrg		} else {
613037b3c26Smrg			fprintf(stderr,
614037b3c26Smrg				"The specified GPU device does not exist.\n");
615037b3c26Smrg			exit(EXIT_FAILURE);
616037b3c26Smrg		}
617037b3c26Smrg	}
6183f012e29Smrg
6193f012e29Smrg	/* Initialize test suites to run */
6203f012e29Smrg
6213f012e29Smrg	/* initialize the CUnit test registry */
6223f012e29Smrg	if (CUE_SUCCESS != CU_initialize_registry()) {
623037b3c26Smrg		amdgpu_close_devices();
6243f012e29Smrg		return CU_get_error();
6253f012e29Smrg	}
6263f012e29Smrg
6273f012e29Smrg	/* Register suites. */
6283f012e29Smrg	if (CU_register_suites(suites) != CUE_SUCCESS) {
6293f012e29Smrg		fprintf(stderr, "suite registration failed - %s\n",
6303f012e29Smrg				CU_get_error_msg());
6313f012e29Smrg		CU_cleanup_registry();
632037b3c26Smrg		amdgpu_close_devices();
6333f012e29Smrg		exit(EXIT_FAILURE);
6343f012e29Smrg	}
6353f012e29Smrg
6363f012e29Smrg	/* Run tests using the CUnit Basic interface */
6373f012e29Smrg	CU_basic_set_mode(CU_BRM_VERBOSE);
6383f012e29Smrg
63900a23bdaSmrg	/* Disable suites and individual tests based on misc. conditions */
64000a23bdaSmrg	amdgpu_disable_suites();
64100a23bdaSmrg
64200a23bdaSmrg	if (display_list) {
64300a23bdaSmrg		display_test_suites();
64400a23bdaSmrg		goto end;
64500a23bdaSmrg	}
64600a23bdaSmrg
6473f012e29Smrg	if (suite_id != -1) {	/* If user specify particular suite? */
6483f012e29Smrg		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
6493f012e29Smrg						CU_get_registry());
6503f012e29Smrg
6513f012e29Smrg		if (pSuite) {
65200a23bdaSmrg
65300a23bdaSmrg			if (force_run)
65400a23bdaSmrg				CU_set_suite_active(pSuite, CU_TRUE);
65500a23bdaSmrg
6563f012e29Smrg			if (test_id != -1) {   /* If user specify test id */
6573f012e29Smrg				pTest = CU_get_test_by_index(
6583f012e29Smrg						(unsigned int) test_id,
6593f012e29Smrg						pSuite);
66000a23bdaSmrg				if (pTest) {
66100a23bdaSmrg					if (force_run)
66200a23bdaSmrg						CU_set_test_active(pTest, CU_TRUE);
66300a23bdaSmrg
6643f012e29Smrg					CU_basic_run_test(pSuite, pTest);
66500a23bdaSmrg				}
6663f012e29Smrg				else {
6673f012e29Smrg					fprintf(stderr, "Invalid test id: %d\n",
6683f012e29Smrg								test_id);
6693f012e29Smrg					CU_cleanup_registry();
670037b3c26Smrg					amdgpu_close_devices();
6713f012e29Smrg					exit(EXIT_FAILURE);
6723f012e29Smrg				}
6733f012e29Smrg			} else
6743f012e29Smrg				CU_basic_run_suite(pSuite);
6753f012e29Smrg		} else {
6763f012e29Smrg			fprintf(stderr, "Invalid suite id : %d\n",
6773f012e29Smrg					suite_id);
6783f012e29Smrg			CU_cleanup_registry();
679037b3c26Smrg			amdgpu_close_devices();
6803f012e29Smrg			exit(EXIT_FAILURE);
6813f012e29Smrg		}
6823f012e29Smrg	} else
6833f012e29Smrg		CU_basic_run_tests();
6843f012e29Smrg
68500a23bdaSmrgend:
6863f012e29Smrg	CU_cleanup_registry();
687037b3c26Smrg	amdgpu_close_devices();
6883f012e29Smrg	return CU_get_error();
6893f012e29Smrg}
690