amdgpu_test.c revision 9bd392ad
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"
613f012e29Smrg
623f012e29Smrg/**
633f012e29Smrg *  Open handles for amdgpu devices
643f012e29Smrg *
653f012e29Smrg */
663f012e29Smrgint drm_amdgpu[MAX_CARDS_SUPPORTED];
673f012e29Smrg
68037b3c26Smrg/** Open render node to test */
69037b3c26Smrgint open_render_node = 0;	/* By default run most tests on primary node */
70037b3c26Smrg
713f012e29Smrg/** The table of all known test suites to run */
723f012e29Smrgstatic CU_SuiteInfo suites[] = {
733f012e29Smrg	{
7400a23bdaSmrg		.pName = BASIC_TESTS_STR,
753f012e29Smrg		.pInitFunc = suite_basic_tests_init,
763f012e29Smrg		.pCleanupFunc = suite_basic_tests_clean,
773f012e29Smrg		.pTests = basic_tests,
783f012e29Smrg	},
793f012e29Smrg	{
8000a23bdaSmrg		.pName = BO_TESTS_STR,
813f012e29Smrg		.pInitFunc = suite_bo_tests_init,
823f012e29Smrg		.pCleanupFunc = suite_bo_tests_clean,
833f012e29Smrg		.pTests = bo_tests,
843f012e29Smrg	},
853f012e29Smrg	{
8600a23bdaSmrg		.pName = CS_TESTS_STR,
873f012e29Smrg		.pInitFunc = suite_cs_tests_init,
883f012e29Smrg		.pCleanupFunc = suite_cs_tests_clean,
893f012e29Smrg		.pTests = cs_tests,
903f012e29Smrg	},
913f012e29Smrg	{
9200a23bdaSmrg		.pName = VCE_TESTS_STR,
933f012e29Smrg		.pInitFunc = suite_vce_tests_init,
943f012e29Smrg		.pCleanupFunc = suite_vce_tests_clean,
953f012e29Smrg		.pTests = vce_tests,
963f012e29Smrg	},
97d8807b2fSmrg	{
9800a23bdaSmrg		.pName = VCN_TESTS_STR,
99d8807b2fSmrg		.pInitFunc = suite_vcn_tests_init,
100d8807b2fSmrg		.pCleanupFunc = suite_vcn_tests_clean,
101d8807b2fSmrg		.pTests = vcn_tests,
102d8807b2fSmrg	},
103d8807b2fSmrg	{
10400a23bdaSmrg		.pName = UVD_ENC_TESTS_STR,
105d8807b2fSmrg		.pInitFunc = suite_uvd_enc_tests_init,
106d8807b2fSmrg		.pCleanupFunc = suite_uvd_enc_tests_clean,
107d8807b2fSmrg		.pTests = uvd_enc_tests,
108d8807b2fSmrg	},
10900a23bdaSmrg	{
11000a23bdaSmrg		.pName = DEADLOCK_TESTS_STR,
11100a23bdaSmrg		.pInitFunc = suite_deadlock_tests_init,
11200a23bdaSmrg		.pCleanupFunc = suite_deadlock_tests_clean,
11300a23bdaSmrg		.pTests = deadlock_tests,
11400a23bdaSmrg	},
11500a23bdaSmrg	{
11600a23bdaSmrg		.pName = VM_TESTS_STR,
11700a23bdaSmrg		.pInitFunc = suite_vm_tests_init,
11800a23bdaSmrg		.pCleanupFunc = suite_vm_tests_clean,
11900a23bdaSmrg		.pTests = vm_tests,
12000a23bdaSmrg	},
1215324fb0dSmrg	{
1225324fb0dSmrg		.pName = RAS_TESTS_STR,
1235324fb0dSmrg		.pInitFunc = suite_ras_tests_init,
1245324fb0dSmrg		.pCleanupFunc = suite_ras_tests_clean,
1255324fb0dSmrg		.pTests = ras_tests,
1265324fb0dSmrg	},
1275324fb0dSmrg	{
1285324fb0dSmrg		.pName = SYNCOBJ_TIMELINE_TESTS_STR,
1295324fb0dSmrg		.pInitFunc = suite_syncobj_timeline_tests_init,
1305324fb0dSmrg		.pCleanupFunc = suite_syncobj_timeline_tests_clean,
1315324fb0dSmrg		.pTests = syncobj_timeline_tests,
1325324fb0dSmrg	},
13300a23bdaSmrg
1343f012e29Smrg	CU_SUITE_INFO_NULL,
1353f012e29Smrg};
1363f012e29Smrg
13700a23bdaSmrgtypedef CU_BOOL (*active__stat_func)(void);
13800a23bdaSmrg
13900a23bdaSmrgtypedef struct Suites_Active_Status {
14000a23bdaSmrg	char*             pName;
14100a23bdaSmrg	active__stat_func pActive;
14200a23bdaSmrg}Suites_Active_Status;
1433f012e29Smrg
14400a23bdaSmrgstatic CU_BOOL always_active()
14500a23bdaSmrg{
14600a23bdaSmrg	return CU_TRUE;
14700a23bdaSmrg}
14800a23bdaSmrg
14900a23bdaSmrgstatic Suites_Active_Status suites_active_stat[] = {
15000a23bdaSmrg		{
15100a23bdaSmrg			.pName = BASIC_TESTS_STR,
15200a23bdaSmrg			.pActive = always_active,
15300a23bdaSmrg		},
15400a23bdaSmrg		{
15500a23bdaSmrg			.pName = BO_TESTS_STR,
15600a23bdaSmrg			.pActive = always_active,
15700a23bdaSmrg		},
15800a23bdaSmrg		{
15900a23bdaSmrg			.pName = CS_TESTS_STR,
16000a23bdaSmrg			.pActive = suite_cs_tests_enable,
16100a23bdaSmrg		},
16200a23bdaSmrg		{
16300a23bdaSmrg			.pName = VCE_TESTS_STR,
16400a23bdaSmrg			.pActive = suite_vce_tests_enable,
16500a23bdaSmrg		},
16600a23bdaSmrg		{
16700a23bdaSmrg			.pName = VCN_TESTS_STR,
16800a23bdaSmrg			.pActive = suite_vcn_tests_enable,
16900a23bdaSmrg		},
17000a23bdaSmrg		{
17100a23bdaSmrg			.pName = UVD_ENC_TESTS_STR,
17200a23bdaSmrg			.pActive = suite_uvd_enc_tests_enable,
17300a23bdaSmrg		},
17400a23bdaSmrg		{
17500a23bdaSmrg			.pName = DEADLOCK_TESTS_STR,
17600a23bdaSmrg			.pActive = suite_deadlock_tests_enable,
17700a23bdaSmrg		},
17800a23bdaSmrg		{
17900a23bdaSmrg			.pName = VM_TESTS_STR,
18000a23bdaSmrg			.pActive = suite_vm_tests_enable,
18100a23bdaSmrg		},
1825324fb0dSmrg		{
1835324fb0dSmrg			.pName = RAS_TESTS_STR,
1845324fb0dSmrg			.pActive = suite_ras_tests_enable,
1855324fb0dSmrg		},
1865324fb0dSmrg		{
1875324fb0dSmrg			.pName = SYNCOBJ_TIMELINE_TESTS_STR,
1885324fb0dSmrg			.pActive = suite_syncobj_timeline_tests_enable,
1895324fb0dSmrg		},
19000a23bdaSmrg};
19100a23bdaSmrg
19200a23bdaSmrg
19300a23bdaSmrg/*
19400a23bdaSmrg * Display information about all  suites and their tests
19500a23bdaSmrg *
19600a23bdaSmrg * NOTE: Must be run after registry is initialized and suites registered.
19700a23bdaSmrg */
1983f012e29Smrgstatic void display_test_suites(void)
1993f012e29Smrg{
2003f012e29Smrg	int iSuite;
2013f012e29Smrg	int iTest;
20200a23bdaSmrg	CU_pSuite pSuite = NULL;
20300a23bdaSmrg	CU_pTest  pTest  = NULL;
2043f012e29Smrg
2059bd392adSmrg	printf("%5s: %2s: %8s: %s\n", "What", "ID", "Status", "Name");
2063f012e29Smrg
2073f012e29Smrg	for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
20800a23bdaSmrg
20900a23bdaSmrg		pSuite = CU_get_suite_by_index((unsigned int) iSuite + 1,
2109bd392adSmrg					       CU_get_registry());
21100a23bdaSmrg
21200a23bdaSmrg		if (!pSuite) {
21300a23bdaSmrg			fprintf(stderr, "Invalid suite id : %d\n", iSuite + 1);
21400a23bdaSmrg			continue;
21500a23bdaSmrg		}
21600a23bdaSmrg
2179bd392adSmrg		printf("Suite: %2d: %8s: %s\n",
2189bd392adSmrg		       iSuite + 1,
2199bd392adSmrg		       pSuite->fActive ? "ENABLED" : "DISABLED",
2209bd392adSmrg		       suites[iSuite].pName);
22100a23bdaSmrg
2229bd392adSmrg		if (!pSuite->fActive)
2239bd392adSmrg			continue;
2243f012e29Smrg
2253f012e29Smrg		for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
2269bd392adSmrg		     iTest++) {
22700a23bdaSmrg			pTest = CU_get_test_by_index((unsigned int) iTest + 1,
2289bd392adSmrg						     pSuite);
22900a23bdaSmrg			if (!pTest) {
23000a23bdaSmrg				fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
23100a23bdaSmrg				continue;
23200a23bdaSmrg			}
2339bd392adSmrg			printf(" Test: %2d: %8s: %s\n",
2349bd392adSmrg			       iTest + 1,
2359bd392adSmrg			       pSuite->fActive && pTest->fActive ? "ENABLED" : "DISABLED",
2369bd392adSmrg			       suites[iSuite].pTests[iTest].pName);
2373f012e29Smrg		}
2383f012e29Smrg	}
2393f012e29Smrg}
2403f012e29Smrg
2413f012e29Smrg/** Help string for command line parameters */
242037b3c26Smrgstatic const char usage[] =
24300a23bdaSmrg	"Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
244037b3c26Smrg	"[-b <pci_bus_id> [-d <pci_device_id>]]\n"
245037b3c26Smrg	"where:\n"
246037b3c26Smrg	"       l - Display all suites and their tests\n"
247037b3c26Smrg	"       r - Run the tests on render node\n"
248037b3c26Smrg	"       b - Specify device's PCI bus id to run tests\n"
249037b3c26Smrg	"       d - Specify device's PCI device id to run tests (optional)\n"
250037b3c26Smrg	"       p - Display information of AMDGPU devices in system\n"
25100a23bdaSmrg	"       f - Force executing inactive suite or test\n"
252037b3c26Smrg	"       h - Display this help\n";
2533f012e29Smrg/** Specified options strings for getopt */
25400a23bdaSmrgstatic const char options[]   = "hlrps:t:b:d:f";
255037b3c26Smrg
256037b3c26Smrg/* Open AMD devices.
2575324fb0dSmrg * Return the number of AMD device opened.
258037b3c26Smrg */
259037b3c26Smrgstatic int amdgpu_open_devices(int open_render_node)
260037b3c26Smrg{
261037b3c26Smrg	drmDevicePtr devices[MAX_CARDS_SUPPORTED];
262037b3c26Smrg	int i;
263037b3c26Smrg	int drm_node;
264037b3c26Smrg	int amd_index = 0;
265037b3c26Smrg	int drm_count;
266037b3c26Smrg	int fd;
267037b3c26Smrg	drmVersionPtr version;
268037b3c26Smrg
269037b3c26Smrg	drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
270037b3c26Smrg
271037b3c26Smrg	if (drm_count < 0) {
272037b3c26Smrg		fprintf(stderr,
273037b3c26Smrg			"drmGetDevices2() returned an error %d\n",
274037b3c26Smrg			drm_count);
275037b3c26Smrg		return 0;
276037b3c26Smrg	}
277037b3c26Smrg
278037b3c26Smrg	for (i = 0; i < drm_count; i++) {
279037b3c26Smrg		/* If this is not PCI device, skip*/
280037b3c26Smrg		if (devices[i]->bustype != DRM_BUS_PCI)
281037b3c26Smrg			continue;
282037b3c26Smrg
283037b3c26Smrg		/* If this is not AMD GPU vender ID, skip*/
284037b3c26Smrg		if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
285037b3c26Smrg			continue;
286037b3c26Smrg
287037b3c26Smrg		if (open_render_node)
288037b3c26Smrg			drm_node = DRM_NODE_RENDER;
289037b3c26Smrg		else
290037b3c26Smrg			drm_node = DRM_NODE_PRIMARY;
291037b3c26Smrg
292037b3c26Smrg		fd = -1;
293037b3c26Smrg		if (devices[i]->available_nodes & 1 << drm_node)
294037b3c26Smrg			fd = open(
295037b3c26Smrg				devices[i]->nodes[drm_node],
296037b3c26Smrg				O_RDWR | O_CLOEXEC);
297037b3c26Smrg
298037b3c26Smrg		/* This node is not available. */
299037b3c26Smrg		if (fd < 0) continue;
300037b3c26Smrg
301037b3c26Smrg		version = drmGetVersion(fd);
302037b3c26Smrg		if (!version) {
303037b3c26Smrg			fprintf(stderr,
304037b3c26Smrg				"Warning: Cannot get version for %s."
305037b3c26Smrg				"Error is %s\n",
306037b3c26Smrg				devices[i]->nodes[drm_node],
307037b3c26Smrg				strerror(errno));
308037b3c26Smrg			close(fd);
309037b3c26Smrg			continue;
310037b3c26Smrg		}
311037b3c26Smrg
312037b3c26Smrg		if (strcmp(version->name, "amdgpu")) {
313037b3c26Smrg			/* This is not AMDGPU driver, skip.*/
314037b3c26Smrg			drmFreeVersion(version);
315037b3c26Smrg			close(fd);
316037b3c26Smrg			continue;
317037b3c26Smrg		}
318037b3c26Smrg
319037b3c26Smrg		drmFreeVersion(version);
320037b3c26Smrg
321037b3c26Smrg		drm_amdgpu[amd_index] = fd;
322037b3c26Smrg		amd_index++;
323037b3c26Smrg	}
324037b3c26Smrg
325037b3c26Smrg	drmFreeDevices(devices, drm_count);
326037b3c26Smrg	return amd_index;
327037b3c26Smrg}
328037b3c26Smrg
329037b3c26Smrg/* Close AMD devices.
330037b3c26Smrg */
331037b3c26Smrgstatic void amdgpu_close_devices()
332037b3c26Smrg{
333037b3c26Smrg	int i;
334037b3c26Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
335037b3c26Smrg		if (drm_amdgpu[i] >=0)
336037b3c26Smrg			close(drm_amdgpu[i]);
337037b3c26Smrg}
338037b3c26Smrg
339037b3c26Smrg/* Print AMD devices information */
340037b3c26Smrgstatic void amdgpu_print_devices()
341037b3c26Smrg{
342037b3c26Smrg	int i;
343037b3c26Smrg	drmDevicePtr device;
344037b3c26Smrg
3455324fb0dSmrg	/* Open the first AMD device to print driver information. */
346037b3c26Smrg	if (drm_amdgpu[0] >=0) {
347037b3c26Smrg		/* Display AMD driver version information.*/
348037b3c26Smrg		drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
349037b3c26Smrg
350037b3c26Smrg		if (retval == NULL) {
351037b3c26Smrg			perror("Cannot get version for AMDGPU device");
352037b3c26Smrg			return;
353037b3c26Smrg		}
354037b3c26Smrg
355037b3c26Smrg		printf("Driver name: %s, Date: %s, Description: %s.\n",
356037b3c26Smrg			retval->name, retval->date, retval->desc);
357037b3c26Smrg		drmFreeVersion(retval);
358037b3c26Smrg	}
359037b3c26Smrg
360037b3c26Smrg	/* Display information of AMD devices */
361037b3c26Smrg	printf("Devices:\n");
362037b3c26Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
363037b3c26Smrg		if (drmGetDevice2(drm_amdgpu[i],
364037b3c26Smrg			DRM_DEVICE_GET_PCI_REVISION,
365037b3c26Smrg			&device) == 0) {
366037b3c26Smrg			if (device->bustype == DRM_BUS_PCI) {
367037b3c26Smrg				printf("PCI ");
368037b3c26Smrg				printf(" domain:%04x",
369037b3c26Smrg					device->businfo.pci->domain);
370037b3c26Smrg				printf(" bus:%02x",
371037b3c26Smrg					device->businfo.pci->bus);
372037b3c26Smrg				printf(" device:%02x",
373037b3c26Smrg					device->businfo.pci->dev);
374037b3c26Smrg				printf(" function:%01x",
375037b3c26Smrg					device->businfo.pci->func);
376037b3c26Smrg				printf(" vendor_id:%04x",
377037b3c26Smrg					device->deviceinfo.pci->vendor_id);
378037b3c26Smrg				printf(" device_id:%04x",
379037b3c26Smrg					device->deviceinfo.pci->device_id);
380037b3c26Smrg				printf(" subvendor_id:%04x",
381037b3c26Smrg					device->deviceinfo.pci->subvendor_id);
382037b3c26Smrg				printf(" subdevice_id:%04x",
383037b3c26Smrg					device->deviceinfo.pci->subdevice_id);
384037b3c26Smrg				printf(" revision_id:%02x",
385037b3c26Smrg					device->deviceinfo.pci->revision_id);
386037b3c26Smrg				printf("\n");
387037b3c26Smrg			}
388037b3c26Smrg			drmFreeDevice(&device);
389037b3c26Smrg		}
390037b3c26Smrg}
391037b3c26Smrg
392037b3c26Smrg/* Find a match AMD device in PCI bus
393037b3c26Smrg * Return the index of the device or -1 if not found
394037b3c26Smrg */
395d8807b2fSmrgstatic int amdgpu_find_device(uint8_t bus, uint16_t dev)
396037b3c26Smrg{
397037b3c26Smrg	int i;
398037b3c26Smrg	drmDevicePtr device;
399037b3c26Smrg
400d8807b2fSmrg	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
401037b3c26Smrg		if (drmGetDevice2(drm_amdgpu[i],
402037b3c26Smrg			DRM_DEVICE_GET_PCI_REVISION,
403037b3c26Smrg			&device) == 0) {
404037b3c26Smrg			if (device->bustype == DRM_BUS_PCI)
405d8807b2fSmrg				if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
406d8807b2fSmrg					device->deviceinfo.pci->device_id == dev) {
407037b3c26Smrg					drmFreeDevice(&device);
408037b3c26Smrg					return i;
409037b3c26Smrg				}
410037b3c26Smrg
411037b3c26Smrg			drmFreeDevice(&device);
412037b3c26Smrg		}
413d8807b2fSmrg	}
414037b3c26Smrg
415037b3c26Smrg	return -1;
416037b3c26Smrg}
4173f012e29Smrg
41800a23bdaSmrgstatic void amdgpu_disable_suites()
41900a23bdaSmrg{
42000a23bdaSmrg	amdgpu_device_handle device_handle;
42100a23bdaSmrg	uint32_t major_version, minor_version, family_id;
42200a23bdaSmrg	int i;
42300a23bdaSmrg	int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
42400a23bdaSmrg
42500a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
42600a23bdaSmrg				   &minor_version, &device_handle))
42700a23bdaSmrg		return;
42800a23bdaSmrg
42900a23bdaSmrg	family_id = device_handle->info.family_id;
43000a23bdaSmrg
43100a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
43200a23bdaSmrg		return;
43300a23bdaSmrg
43400a23bdaSmrg	/* Set active status for suites based on their policies */
43500a23bdaSmrg	for (i = 0; i < size; ++i)
43600a23bdaSmrg		if (amdgpu_set_suite_active(suites_active_stat[i].pName,
43700a23bdaSmrg				suites_active_stat[i].pActive()))
43800a23bdaSmrg			fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
43900a23bdaSmrg
44000a23bdaSmrg	/* Explicitly disable specific tests due to known bugs or preferences */
44100a23bdaSmrg	/*
44200a23bdaSmrg	* BUG: Compute ring stalls and never recovers when the address is
44300a23bdaSmrg	* written after the command already submitted
44400a23bdaSmrg	*/
4456532f28eSmrg	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4466532f28eSmrg			"compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
44700a23bdaSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
44800a23bdaSmrg
4495324fb0dSmrg	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4505324fb0dSmrg				"sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4515324fb0dSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4525324fb0dSmrg
4539bd392adSmrg	/* This test was ran on GFX9 only */
4549bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4559bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4569bd392adSmrg				"gfx ring bad dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4579bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4589bd392adSmrg
4599bd392adSmrg	/* This test was ran on GFX9 only */
4609bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4619bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4629bd392adSmrg				"compute ring bad dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
4639bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4649bd392adSmrg
4659bd392adSmrg	/* This test was ran on GFX9 only */
4669bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4679bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4689bd392adSmrg				"gfx ring bad slow dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4699bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4709bd392adSmrg
4719bd392adSmrg	/* This test was ran on GFX9 only */
4729bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4739bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4749bd392adSmrg				"compute ring bad slow dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
4759bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4769bd392adSmrg
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 draw 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				"gfx ring slow bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4869bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4879bd392adSmrg
48800a23bdaSmrg	if (amdgpu_set_test_active(BO_TESTS_STR, "Metadata", CU_FALSE))
48900a23bdaSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
49000a23bdaSmrg
49100a23bdaSmrg	if (amdgpu_set_test_active(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
49200a23bdaSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
49300a23bdaSmrg
49400a23bdaSmrg	/* This test was ran on GFX8 and GFX9 only */
49500a23bdaSmrg	if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
49600a23bdaSmrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
49700a23bdaSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4985324fb0dSmrg
4995324fb0dSmrg	/* This test was ran on GFX9 only */
50088f8a8d2Smrg	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
50188f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
50288f8a8d2Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
50388f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
5045324fb0dSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
50588f8a8d2Smrg	}
5065324fb0dSmrg
5075324fb0dSmrg	/* This test was ran on GFX9 only */
5085324fb0dSmrg	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5095324fb0dSmrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
5105324fb0dSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
51188f8a8d2Smrg
51288f8a8d2Smrg	/* This test was ran on GFX9 only */
51388f8a8d2Smrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
51488f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
51588f8a8d2Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
51600a23bdaSmrg}
51700a23bdaSmrg
5183f012e29Smrg/* The main() function for setting up and running the tests.
5193f012e29Smrg * Returns a CUE_SUCCESS on successful running, another
5203f012e29Smrg * CUnit error code on failure.
5213f012e29Smrg */
5223f012e29Smrgint main(int argc, char **argv)
5233f012e29Smrg{
5243f012e29Smrg	int c;			/* Character received from getopt */
5253f012e29Smrg	int i = 0;
5263f012e29Smrg	int suite_id = -1;	/* By default run everything */
5273f012e29Smrg	int test_id  = -1;	/* By default run all tests in the suite */
528037b3c26Smrg	int pci_bus_id = -1;    /* By default PC bus ID is not specified */
529037b3c26Smrg	int pci_device_id = 0;  /* By default PC device ID is zero */
530037b3c26Smrg	int display_devices = 0;/* By default not to display devices' info */
5313f012e29Smrg	CU_pSuite pSuite = NULL;
5323f012e29Smrg	CU_pTest  pTest  = NULL;
533037b3c26Smrg	int test_device_index;
53400a23bdaSmrg	int display_list = 0;
53500a23bdaSmrg	int force_run = 0;
5363f012e29Smrg
5373f012e29Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
5383f012e29Smrg		drm_amdgpu[i] = -1;
5393f012e29Smrg
5403f012e29Smrg
5413f012e29Smrg	/* Parse command line string */
5423f012e29Smrg	opterr = 0;		/* Do not print error messages from getopt */
5433f012e29Smrg	while ((c = getopt(argc, argv, options)) != -1) {
5443f012e29Smrg		switch (c) {
5453f012e29Smrg		case 'l':
54600a23bdaSmrg			display_list = 1;
54700a23bdaSmrg			break;
5483f012e29Smrg		case 's':
5493f012e29Smrg			suite_id = atoi(optarg);
5503f012e29Smrg			break;
5513f012e29Smrg		case 't':
5523f012e29Smrg			test_id = atoi(optarg);
5533f012e29Smrg			break;
554037b3c26Smrg		case 'b':
555037b3c26Smrg			pci_bus_id = atoi(optarg);
556037b3c26Smrg			break;
557037b3c26Smrg		case 'd':
558d8807b2fSmrg			sscanf(optarg, "%x", &pci_device_id);
559037b3c26Smrg			break;
560037b3c26Smrg		case 'p':
561037b3c26Smrg			display_devices = 1;
562037b3c26Smrg			break;
563037b3c26Smrg		case 'r':
564037b3c26Smrg			open_render_node = 1;
565037b3c26Smrg			break;
56600a23bdaSmrg		case 'f':
56700a23bdaSmrg			force_run = 1;
56800a23bdaSmrg			break;
5693f012e29Smrg		case '?':
5703f012e29Smrg		case 'h':
5713f012e29Smrg			fprintf(stderr, usage, argv[0]);
5723f012e29Smrg			exit(EXIT_SUCCESS);
5733f012e29Smrg		default:
5743f012e29Smrg			fprintf(stderr, usage, argv[0]);
5753f012e29Smrg			exit(EXIT_FAILURE);
5763f012e29Smrg		}
5773f012e29Smrg	}
5783f012e29Smrg
579037b3c26Smrg	if (amdgpu_open_devices(open_render_node) <= 0) {
580037b3c26Smrg		perror("Cannot open AMDGPU device");
5813f012e29Smrg		exit(EXIT_FAILURE);
5823f012e29Smrg	}
5833f012e29Smrg
584037b3c26Smrg	if (drm_amdgpu[0] < 0) {
585037b3c26Smrg		perror("Cannot open AMDGPU device");
5863f012e29Smrg		exit(EXIT_FAILURE);
5873f012e29Smrg	}
5883f012e29Smrg
589037b3c26Smrg	if (display_devices) {
590037b3c26Smrg		amdgpu_print_devices();
591037b3c26Smrg		amdgpu_close_devices();
592037b3c26Smrg		exit(EXIT_SUCCESS);
593037b3c26Smrg	}
5943f012e29Smrg
595d8807b2fSmrg	if (pci_bus_id > 0 || pci_device_id) {
596037b3c26Smrg		/* A device was specified to run the test */
597d8807b2fSmrg		test_device_index = amdgpu_find_device(pci_bus_id,
598d8807b2fSmrg						       pci_device_id);
599037b3c26Smrg
600037b3c26Smrg		if (test_device_index >= 0) {
601037b3c26Smrg			/* Most tests run on device of drm_amdgpu[0].
602037b3c26Smrg			 * Swap the chosen device to drm_amdgpu[0].
603037b3c26Smrg			 */
604037b3c26Smrg			i = drm_amdgpu[0];
605037b3c26Smrg			drm_amdgpu[0] = drm_amdgpu[test_device_index];
606037b3c26Smrg			drm_amdgpu[test_device_index] = i;
607037b3c26Smrg		} else {
608037b3c26Smrg			fprintf(stderr,
609037b3c26Smrg				"The specified GPU device does not exist.\n");
610037b3c26Smrg			exit(EXIT_FAILURE);
611037b3c26Smrg		}
612037b3c26Smrg	}
6133f012e29Smrg
6143f012e29Smrg	/* Initialize test suites to run */
6153f012e29Smrg
6163f012e29Smrg	/* initialize the CUnit test registry */
6173f012e29Smrg	if (CUE_SUCCESS != CU_initialize_registry()) {
618037b3c26Smrg		amdgpu_close_devices();
6193f012e29Smrg		return CU_get_error();
6203f012e29Smrg	}
6213f012e29Smrg
6223f012e29Smrg	/* Register suites. */
6233f012e29Smrg	if (CU_register_suites(suites) != CUE_SUCCESS) {
6243f012e29Smrg		fprintf(stderr, "suite registration failed - %s\n",
6253f012e29Smrg				CU_get_error_msg());
6263f012e29Smrg		CU_cleanup_registry();
627037b3c26Smrg		amdgpu_close_devices();
6283f012e29Smrg		exit(EXIT_FAILURE);
6293f012e29Smrg	}
6303f012e29Smrg
6313f012e29Smrg	/* Run tests using the CUnit Basic interface */
6323f012e29Smrg	CU_basic_set_mode(CU_BRM_VERBOSE);
6333f012e29Smrg
63400a23bdaSmrg	/* Disable suites and individual tests based on misc. conditions */
63500a23bdaSmrg	amdgpu_disable_suites();
63600a23bdaSmrg
63700a23bdaSmrg	if (display_list) {
63800a23bdaSmrg		display_test_suites();
63900a23bdaSmrg		goto end;
64000a23bdaSmrg	}
64100a23bdaSmrg
6423f012e29Smrg	if (suite_id != -1) {	/* If user specify particular suite? */
6433f012e29Smrg		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
6443f012e29Smrg						CU_get_registry());
6453f012e29Smrg
6463f012e29Smrg		if (pSuite) {
64700a23bdaSmrg
64800a23bdaSmrg			if (force_run)
64900a23bdaSmrg				CU_set_suite_active(pSuite, CU_TRUE);
65000a23bdaSmrg
6513f012e29Smrg			if (test_id != -1) {   /* If user specify test id */
6523f012e29Smrg				pTest = CU_get_test_by_index(
6533f012e29Smrg						(unsigned int) test_id,
6543f012e29Smrg						pSuite);
65500a23bdaSmrg				if (pTest) {
65600a23bdaSmrg					if (force_run)
65700a23bdaSmrg						CU_set_test_active(pTest, CU_TRUE);
65800a23bdaSmrg
6593f012e29Smrg					CU_basic_run_test(pSuite, pTest);
66000a23bdaSmrg				}
6613f012e29Smrg				else {
6623f012e29Smrg					fprintf(stderr, "Invalid test id: %d\n",
6633f012e29Smrg								test_id);
6643f012e29Smrg					CU_cleanup_registry();
665037b3c26Smrg					amdgpu_close_devices();
6663f012e29Smrg					exit(EXIT_FAILURE);
6673f012e29Smrg				}
6683f012e29Smrg			} else
6693f012e29Smrg				CU_basic_run_suite(pSuite);
6703f012e29Smrg		} else {
6713f012e29Smrg			fprintf(stderr, "Invalid suite id : %d\n",
6723f012e29Smrg					suite_id);
6733f012e29Smrg			CU_cleanup_registry();
674037b3c26Smrg			amdgpu_close_devices();
6753f012e29Smrg			exit(EXIT_FAILURE);
6763f012e29Smrg		}
6773f012e29Smrg	} else
6783f012e29Smrg		CU_basic_run_tests();
6793f012e29Smrg
68000a23bdaSmrgend:
6813f012e29Smrg	CU_cleanup_registry();
682037b3c26Smrg	amdgpu_close_devices();
6833f012e29Smrg	return CU_get_error();
6843f012e29Smrg}
685