amdgpu_test.c revision 4babd585
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>
404babd585Smrg#ifdef __linux__
414babd585Smrg#include <linux/limits.h>
424babd585Smrg#elif __FreeBSD__
434babd585Smrg/* SPECNAMELEN in FreeBSD is defined here: */
444babd585Smrg#include <sys/param.h>
454babd585Smrg#endif
464babd585Smrg#ifdef MAJOR_IN_MKDEV
474babd585Smrg#include <sys/mkdev.h>
484babd585Smrg#endif
494babd585Smrg#ifdef MAJOR_IN_SYSMACROS
504babd585Smrg#include <sys/sysmacros.h>
514babd585Smrg#endif
523f012e29Smrg
533f012e29Smrg#include "drm.h"
543f012e29Smrg#include "xf86drmMode.h"
553f012e29Smrg#include "xf86drm.h"
563f012e29Smrg
573f012e29Smrg#include "CUnit/Basic.h"
583f012e29Smrg
593f012e29Smrg#include "amdgpu_test.h"
6000a23bdaSmrg#include "amdgpu_internal.h"
6100a23bdaSmrg
6200a23bdaSmrg/* Test suite names */
6300a23bdaSmrg#define BASIC_TESTS_STR "Basic Tests"
6400a23bdaSmrg#define BO_TESTS_STR "BO Tests"
6500a23bdaSmrg#define CS_TESTS_STR "CS Tests"
6600a23bdaSmrg#define VCE_TESTS_STR "VCE Tests"
6700a23bdaSmrg#define VCN_TESTS_STR "VCN Tests"
6800a23bdaSmrg#define UVD_ENC_TESTS_STR "UVD ENC Tests"
6900a23bdaSmrg#define DEADLOCK_TESTS_STR "Deadlock Tests"
7000a23bdaSmrg#define VM_TESTS_STR "VM Tests"
715324fb0dSmrg#define RAS_TESTS_STR "RAS Tests"
725324fb0dSmrg#define SYNCOBJ_TIMELINE_TESTS_STR "SYNCOBJ TIMELINE Tests"
7341687f09Smrg#define SECURITY_TESTS_STR "Security Tests"
744babd585Smrg#define HOTUNPLUG_TESTS_STR "Hotunplug Tests"
753f012e29Smrg
763f012e29Smrg/**
773f012e29Smrg *  Open handles for amdgpu devices
783f012e29Smrg *
793f012e29Smrg */
803f012e29Smrgint drm_amdgpu[MAX_CARDS_SUPPORTED];
813f012e29Smrg
82037b3c26Smrg/** Open render node to test */
83037b3c26Smrgint open_render_node = 0;	/* By default run most tests on primary node */
84037b3c26Smrg
853f012e29Smrg/** The table of all known test suites to run */
863f012e29Smrgstatic CU_SuiteInfo suites[] = {
873f012e29Smrg	{
8800a23bdaSmrg		.pName = BASIC_TESTS_STR,
893f012e29Smrg		.pInitFunc = suite_basic_tests_init,
903f012e29Smrg		.pCleanupFunc = suite_basic_tests_clean,
913f012e29Smrg		.pTests = basic_tests,
923f012e29Smrg	},
933f012e29Smrg	{
9400a23bdaSmrg		.pName = BO_TESTS_STR,
953f012e29Smrg		.pInitFunc = suite_bo_tests_init,
963f012e29Smrg		.pCleanupFunc = suite_bo_tests_clean,
973f012e29Smrg		.pTests = bo_tests,
983f012e29Smrg	},
993f012e29Smrg	{
10000a23bdaSmrg		.pName = CS_TESTS_STR,
1013f012e29Smrg		.pInitFunc = suite_cs_tests_init,
1023f012e29Smrg		.pCleanupFunc = suite_cs_tests_clean,
1033f012e29Smrg		.pTests = cs_tests,
1043f012e29Smrg	},
1053f012e29Smrg	{
10600a23bdaSmrg		.pName = VCE_TESTS_STR,
1073f012e29Smrg		.pInitFunc = suite_vce_tests_init,
1083f012e29Smrg		.pCleanupFunc = suite_vce_tests_clean,
1093f012e29Smrg		.pTests = vce_tests,
1103f012e29Smrg	},
111d8807b2fSmrg	{
11200a23bdaSmrg		.pName = VCN_TESTS_STR,
113d8807b2fSmrg		.pInitFunc = suite_vcn_tests_init,
114d8807b2fSmrg		.pCleanupFunc = suite_vcn_tests_clean,
115d8807b2fSmrg		.pTests = vcn_tests,
116d8807b2fSmrg	},
117d8807b2fSmrg	{
11800a23bdaSmrg		.pName = UVD_ENC_TESTS_STR,
119d8807b2fSmrg		.pInitFunc = suite_uvd_enc_tests_init,
120d8807b2fSmrg		.pCleanupFunc = suite_uvd_enc_tests_clean,
121d8807b2fSmrg		.pTests = uvd_enc_tests,
122d8807b2fSmrg	},
12300a23bdaSmrg	{
12400a23bdaSmrg		.pName = DEADLOCK_TESTS_STR,
12500a23bdaSmrg		.pInitFunc = suite_deadlock_tests_init,
12600a23bdaSmrg		.pCleanupFunc = suite_deadlock_tests_clean,
12700a23bdaSmrg		.pTests = deadlock_tests,
12800a23bdaSmrg	},
12900a23bdaSmrg	{
13000a23bdaSmrg		.pName = VM_TESTS_STR,
13100a23bdaSmrg		.pInitFunc = suite_vm_tests_init,
13200a23bdaSmrg		.pCleanupFunc = suite_vm_tests_clean,
13300a23bdaSmrg		.pTests = vm_tests,
13400a23bdaSmrg	},
1355324fb0dSmrg	{
1365324fb0dSmrg		.pName = RAS_TESTS_STR,
1375324fb0dSmrg		.pInitFunc = suite_ras_tests_init,
1385324fb0dSmrg		.pCleanupFunc = suite_ras_tests_clean,
1395324fb0dSmrg		.pTests = ras_tests,
1405324fb0dSmrg	},
1415324fb0dSmrg	{
1425324fb0dSmrg		.pName = SYNCOBJ_TIMELINE_TESTS_STR,
1435324fb0dSmrg		.pInitFunc = suite_syncobj_timeline_tests_init,
1445324fb0dSmrg		.pCleanupFunc = suite_syncobj_timeline_tests_clean,
1455324fb0dSmrg		.pTests = syncobj_timeline_tests,
1465324fb0dSmrg	},
14741687f09Smrg	{
14841687f09Smrg		.pName = SECURITY_TESTS_STR,
14941687f09Smrg		.pInitFunc = suite_security_tests_init,
15041687f09Smrg		.pCleanupFunc = suite_security_tests_clean,
15141687f09Smrg		.pTests = security_tests,
15241687f09Smrg	},
1534babd585Smrg	{
1544babd585Smrg		.pName = HOTUNPLUG_TESTS_STR,
1554babd585Smrg		.pInitFunc = suite_hotunplug_tests_init,
1564babd585Smrg		.pCleanupFunc = suite_hotunplug_tests_clean,
1574babd585Smrg		.pTests = hotunplug_tests,
1584babd585Smrg	},
15900a23bdaSmrg
1603f012e29Smrg	CU_SUITE_INFO_NULL,
1613f012e29Smrg};
1623f012e29Smrg
16300a23bdaSmrgtypedef CU_BOOL (*active__stat_func)(void);
16400a23bdaSmrg
16500a23bdaSmrgtypedef struct Suites_Active_Status {
16600a23bdaSmrg	char*             pName;
16700a23bdaSmrg	active__stat_func pActive;
16800a23bdaSmrg}Suites_Active_Status;
1693f012e29Smrg
17000a23bdaSmrgstatic CU_BOOL always_active()
17100a23bdaSmrg{
17200a23bdaSmrg	return CU_TRUE;
17300a23bdaSmrg}
17400a23bdaSmrg
17500a23bdaSmrgstatic Suites_Active_Status suites_active_stat[] = {
17600a23bdaSmrg		{
17700a23bdaSmrg			.pName = BASIC_TESTS_STR,
17841687f09Smrg			.pActive = suite_basic_tests_enable,
17900a23bdaSmrg		},
18000a23bdaSmrg		{
18100a23bdaSmrg			.pName = BO_TESTS_STR,
18200a23bdaSmrg			.pActive = always_active,
18300a23bdaSmrg		},
18400a23bdaSmrg		{
18500a23bdaSmrg			.pName = CS_TESTS_STR,
18600a23bdaSmrg			.pActive = suite_cs_tests_enable,
18700a23bdaSmrg		},
18800a23bdaSmrg		{
18900a23bdaSmrg			.pName = VCE_TESTS_STR,
19000a23bdaSmrg			.pActive = suite_vce_tests_enable,
19100a23bdaSmrg		},
19200a23bdaSmrg		{
19300a23bdaSmrg			.pName = VCN_TESTS_STR,
19400a23bdaSmrg			.pActive = suite_vcn_tests_enable,
19500a23bdaSmrg		},
19600a23bdaSmrg		{
19700a23bdaSmrg			.pName = UVD_ENC_TESTS_STR,
19800a23bdaSmrg			.pActive = suite_uvd_enc_tests_enable,
19900a23bdaSmrg		},
20000a23bdaSmrg		{
20100a23bdaSmrg			.pName = DEADLOCK_TESTS_STR,
20200a23bdaSmrg			.pActive = suite_deadlock_tests_enable,
20300a23bdaSmrg		},
20400a23bdaSmrg		{
20500a23bdaSmrg			.pName = VM_TESTS_STR,
20600a23bdaSmrg			.pActive = suite_vm_tests_enable,
20700a23bdaSmrg		},
2085324fb0dSmrg		{
2095324fb0dSmrg			.pName = RAS_TESTS_STR,
2105324fb0dSmrg			.pActive = suite_ras_tests_enable,
2115324fb0dSmrg		},
2125324fb0dSmrg		{
2135324fb0dSmrg			.pName = SYNCOBJ_TIMELINE_TESTS_STR,
2145324fb0dSmrg			.pActive = suite_syncobj_timeline_tests_enable,
2155324fb0dSmrg		},
21641687f09Smrg		{
21741687f09Smrg			.pName = SECURITY_TESTS_STR,
21841687f09Smrg			.pActive = suite_security_tests_enable,
21941687f09Smrg		},
2204babd585Smrg		{
2214babd585Smrg			.pName = HOTUNPLUG_TESTS_STR,
2224babd585Smrg			.pActive = suite_hotunplug_tests_enable,
2234babd585Smrg		},
22400a23bdaSmrg};
22500a23bdaSmrg
22600a23bdaSmrg
22700a23bdaSmrg/*
22800a23bdaSmrg * Display information about all  suites and their tests
22900a23bdaSmrg *
23000a23bdaSmrg * NOTE: Must be run after registry is initialized and suites registered.
23100a23bdaSmrg */
2323f012e29Smrgstatic void display_test_suites(void)
2333f012e29Smrg{
2343f012e29Smrg	int iSuite;
2353f012e29Smrg	int iTest;
23600a23bdaSmrg	CU_pSuite pSuite = NULL;
23700a23bdaSmrg	CU_pTest  pTest  = NULL;
2383f012e29Smrg
2399bd392adSmrg	printf("%5s: %2s: %8s: %s\n", "What", "ID", "Status", "Name");
2403f012e29Smrg
2413f012e29Smrg	for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
24200a23bdaSmrg
24300a23bdaSmrg		pSuite = CU_get_suite_by_index((unsigned int) iSuite + 1,
2449bd392adSmrg					       CU_get_registry());
24500a23bdaSmrg
24600a23bdaSmrg		if (!pSuite) {
24700a23bdaSmrg			fprintf(stderr, "Invalid suite id : %d\n", iSuite + 1);
24800a23bdaSmrg			continue;
24900a23bdaSmrg		}
25000a23bdaSmrg
2519bd392adSmrg		printf("Suite: %2d: %8s: %s\n",
2529bd392adSmrg		       iSuite + 1,
2539bd392adSmrg		       pSuite->fActive ? "ENABLED" : "DISABLED",
2549bd392adSmrg		       suites[iSuite].pName);
25500a23bdaSmrg
2569bd392adSmrg		if (!pSuite->fActive)
2579bd392adSmrg			continue;
2583f012e29Smrg
2593f012e29Smrg		for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
2609bd392adSmrg		     iTest++) {
26100a23bdaSmrg			pTest = CU_get_test_by_index((unsigned int) iTest + 1,
2629bd392adSmrg						     pSuite);
26300a23bdaSmrg			if (!pTest) {
26400a23bdaSmrg				fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
26500a23bdaSmrg				continue;
26600a23bdaSmrg			}
2679bd392adSmrg			printf(" Test: %2d: %8s: %s\n",
2689bd392adSmrg			       iTest + 1,
2699bd392adSmrg			       pSuite->fActive && pTest->fActive ? "ENABLED" : "DISABLED",
2709bd392adSmrg			       suites[iSuite].pTests[iTest].pName);
2713f012e29Smrg		}
2723f012e29Smrg	}
2733f012e29Smrg}
2743f012e29Smrg
2753f012e29Smrg/** Help string for command line parameters */
276037b3c26Smrgstatic const char usage[] =
27700a23bdaSmrg	"Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
278037b3c26Smrg	"[-b <pci_bus_id> [-d <pci_device_id>]]\n"
279037b3c26Smrg	"where:\n"
280037b3c26Smrg	"       l - Display all suites and their tests\n"
281037b3c26Smrg	"       r - Run the tests on render node\n"
282037b3c26Smrg	"       b - Specify device's PCI bus id to run tests\n"
283037b3c26Smrg	"       d - Specify device's PCI device id to run tests (optional)\n"
284037b3c26Smrg	"       p - Display information of AMDGPU devices in system\n"
28500a23bdaSmrg	"       f - Force executing inactive suite or test\n"
286037b3c26Smrg	"       h - Display this help\n";
2873f012e29Smrg/** Specified options strings for getopt */
28800a23bdaSmrgstatic const char options[]   = "hlrps:t:b:d:f";
289037b3c26Smrg
290037b3c26Smrg/* Open AMD devices.
2915324fb0dSmrg * Return the number of AMD device opened.
292037b3c26Smrg */
293037b3c26Smrgstatic int amdgpu_open_devices(int open_render_node)
294037b3c26Smrg{
295037b3c26Smrg	drmDevicePtr devices[MAX_CARDS_SUPPORTED];
296037b3c26Smrg	int i;
297037b3c26Smrg	int drm_node;
298037b3c26Smrg	int amd_index = 0;
299037b3c26Smrg	int drm_count;
300037b3c26Smrg	int fd;
301037b3c26Smrg	drmVersionPtr version;
302037b3c26Smrg
303037b3c26Smrg	drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
304037b3c26Smrg
305037b3c26Smrg	if (drm_count < 0) {
306037b3c26Smrg		fprintf(stderr,
307037b3c26Smrg			"drmGetDevices2() returned an error %d\n",
308037b3c26Smrg			drm_count);
309037b3c26Smrg		return 0;
310037b3c26Smrg	}
311037b3c26Smrg
312037b3c26Smrg	for (i = 0; i < drm_count; i++) {
313037b3c26Smrg		/* If this is not PCI device, skip*/
314037b3c26Smrg		if (devices[i]->bustype != DRM_BUS_PCI)
315037b3c26Smrg			continue;
316037b3c26Smrg
317037b3c26Smrg		/* If this is not AMD GPU vender ID, skip*/
318037b3c26Smrg		if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
319037b3c26Smrg			continue;
320037b3c26Smrg
321037b3c26Smrg		if (open_render_node)
322037b3c26Smrg			drm_node = DRM_NODE_RENDER;
323037b3c26Smrg		else
324037b3c26Smrg			drm_node = DRM_NODE_PRIMARY;
325037b3c26Smrg
326037b3c26Smrg		fd = -1;
327037b3c26Smrg		if (devices[i]->available_nodes & 1 << drm_node)
328037b3c26Smrg			fd = open(
329037b3c26Smrg				devices[i]->nodes[drm_node],
330037b3c26Smrg				O_RDWR | O_CLOEXEC);
331037b3c26Smrg
332037b3c26Smrg		/* This node is not available. */
333037b3c26Smrg		if (fd < 0) continue;
334037b3c26Smrg
335037b3c26Smrg		version = drmGetVersion(fd);
336037b3c26Smrg		if (!version) {
337037b3c26Smrg			fprintf(stderr,
338037b3c26Smrg				"Warning: Cannot get version for %s."
339037b3c26Smrg				"Error is %s\n",
340037b3c26Smrg				devices[i]->nodes[drm_node],
341037b3c26Smrg				strerror(errno));
342037b3c26Smrg			close(fd);
343037b3c26Smrg			continue;
344037b3c26Smrg		}
345037b3c26Smrg
346037b3c26Smrg		if (strcmp(version->name, "amdgpu")) {
347037b3c26Smrg			/* This is not AMDGPU driver, skip.*/
348037b3c26Smrg			drmFreeVersion(version);
349037b3c26Smrg			close(fd);
350037b3c26Smrg			continue;
351037b3c26Smrg		}
352037b3c26Smrg
353037b3c26Smrg		drmFreeVersion(version);
354037b3c26Smrg
355037b3c26Smrg		drm_amdgpu[amd_index] = fd;
356037b3c26Smrg		amd_index++;
357037b3c26Smrg	}
358037b3c26Smrg
359037b3c26Smrg	drmFreeDevices(devices, drm_count);
360037b3c26Smrg	return amd_index;
361037b3c26Smrg}
362037b3c26Smrg
363037b3c26Smrg/* Close AMD devices.
364037b3c26Smrg */
3654babd585Smrgvoid amdgpu_close_devices()
366037b3c26Smrg{
367037b3c26Smrg	int i;
368037b3c26Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
3694babd585Smrg		if (drm_amdgpu[i] >=0) {
370037b3c26Smrg			close(drm_amdgpu[i]);
3714babd585Smrg		}
372037b3c26Smrg}
373037b3c26Smrg
374037b3c26Smrg/* Print AMD devices information */
375037b3c26Smrgstatic void amdgpu_print_devices()
376037b3c26Smrg{
377037b3c26Smrg	int i;
378037b3c26Smrg	drmDevicePtr device;
379037b3c26Smrg
3805324fb0dSmrg	/* Open the first AMD device to print driver information. */
381037b3c26Smrg	if (drm_amdgpu[0] >=0) {
382037b3c26Smrg		/* Display AMD driver version information.*/
383037b3c26Smrg		drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
384037b3c26Smrg
385037b3c26Smrg		if (retval == NULL) {
386037b3c26Smrg			perror("Cannot get version for AMDGPU device");
387037b3c26Smrg			return;
388037b3c26Smrg		}
389037b3c26Smrg
390037b3c26Smrg		printf("Driver name: %s, Date: %s, Description: %s.\n",
391037b3c26Smrg			retval->name, retval->date, retval->desc);
392037b3c26Smrg		drmFreeVersion(retval);
393037b3c26Smrg	}
394037b3c26Smrg
395037b3c26Smrg	/* Display information of AMD devices */
396037b3c26Smrg	printf("Devices:\n");
397037b3c26Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
398037b3c26Smrg		if (drmGetDevice2(drm_amdgpu[i],
399037b3c26Smrg			DRM_DEVICE_GET_PCI_REVISION,
400037b3c26Smrg			&device) == 0) {
401037b3c26Smrg			if (device->bustype == DRM_BUS_PCI) {
402037b3c26Smrg				printf("PCI ");
403037b3c26Smrg				printf(" domain:%04x",
404037b3c26Smrg					device->businfo.pci->domain);
405037b3c26Smrg				printf(" bus:%02x",
406037b3c26Smrg					device->businfo.pci->bus);
407037b3c26Smrg				printf(" device:%02x",
408037b3c26Smrg					device->businfo.pci->dev);
409037b3c26Smrg				printf(" function:%01x",
410037b3c26Smrg					device->businfo.pci->func);
411037b3c26Smrg				printf(" vendor_id:%04x",
412037b3c26Smrg					device->deviceinfo.pci->vendor_id);
413037b3c26Smrg				printf(" device_id:%04x",
414037b3c26Smrg					device->deviceinfo.pci->device_id);
415037b3c26Smrg				printf(" subvendor_id:%04x",
416037b3c26Smrg					device->deviceinfo.pci->subvendor_id);
417037b3c26Smrg				printf(" subdevice_id:%04x",
418037b3c26Smrg					device->deviceinfo.pci->subdevice_id);
419037b3c26Smrg				printf(" revision_id:%02x",
420037b3c26Smrg					device->deviceinfo.pci->revision_id);
421037b3c26Smrg				printf("\n");
422037b3c26Smrg			}
423037b3c26Smrg			drmFreeDevice(&device);
424037b3c26Smrg		}
425037b3c26Smrg}
426037b3c26Smrg
427037b3c26Smrg/* Find a match AMD device in PCI bus
428037b3c26Smrg * Return the index of the device or -1 if not found
429037b3c26Smrg */
430d8807b2fSmrgstatic int amdgpu_find_device(uint8_t bus, uint16_t dev)
431037b3c26Smrg{
432037b3c26Smrg	int i;
433037b3c26Smrg	drmDevicePtr device;
434037b3c26Smrg
435d8807b2fSmrg	for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
436037b3c26Smrg		if (drmGetDevice2(drm_amdgpu[i],
437037b3c26Smrg			DRM_DEVICE_GET_PCI_REVISION,
438037b3c26Smrg			&device) == 0) {
439037b3c26Smrg			if (device->bustype == DRM_BUS_PCI)
440d8807b2fSmrg				if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
441d8807b2fSmrg					device->deviceinfo.pci->device_id == dev) {
442037b3c26Smrg					drmFreeDevice(&device);
443037b3c26Smrg					return i;
444037b3c26Smrg				}
445037b3c26Smrg
446037b3c26Smrg			drmFreeDevice(&device);
447037b3c26Smrg		}
448d8807b2fSmrg	}
449037b3c26Smrg
450037b3c26Smrg	return -1;
451037b3c26Smrg}
4523f012e29Smrg
45300a23bdaSmrgstatic void amdgpu_disable_suites()
45400a23bdaSmrg{
45500a23bdaSmrg	amdgpu_device_handle device_handle;
45600a23bdaSmrg	uint32_t major_version, minor_version, family_id;
4574babd585Smrg	drmDevicePtr devices[MAX_CARDS_SUPPORTED];
4584babd585Smrg	int i, drm_count;
45900a23bdaSmrg	int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
46000a23bdaSmrg
46100a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
46200a23bdaSmrg				   &minor_version, &device_handle))
46300a23bdaSmrg		return;
46400a23bdaSmrg
46500a23bdaSmrg	family_id = device_handle->info.family_id;
46600a23bdaSmrg
46700a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
46800a23bdaSmrg		return;
46900a23bdaSmrg
4704babd585Smrg	drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
4714babd585Smrg
47200a23bdaSmrg	/* Set active status for suites based on their policies */
47300a23bdaSmrg	for (i = 0; i < size; ++i)
47400a23bdaSmrg		if (amdgpu_set_suite_active(suites_active_stat[i].pName,
47500a23bdaSmrg				suites_active_stat[i].pActive()))
47600a23bdaSmrg			fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
47700a23bdaSmrg
47800a23bdaSmrg	/* Explicitly disable specific tests due to known bugs or preferences */
47900a23bdaSmrg	/*
48000a23bdaSmrg	* BUG: Compute ring stalls and never recovers when the address is
48100a23bdaSmrg	* written after the command already submitted
48200a23bdaSmrg	*/
4836532f28eSmrg	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4846532f28eSmrg			"compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
48500a23bdaSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
48600a23bdaSmrg
4875324fb0dSmrg	if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4885324fb0dSmrg				"sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4895324fb0dSmrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4905324fb0dSmrg
4919bd392adSmrg	/* This test was ran on GFX9 only */
4929bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4939bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
4949bd392adSmrg				"gfx ring bad dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
4959bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
4969bd392adSmrg
4979bd392adSmrg	/* This test was ran on GFX9 only */
4989bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
4999bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
5009bd392adSmrg				"compute ring bad dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
5019bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5029bd392adSmrg
5039bd392adSmrg	/* This test was ran on GFX9 only */
5049bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5059bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
5069bd392adSmrg				"gfx ring bad slow dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
5079bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5089bd392adSmrg
5099bd392adSmrg	/* This test was ran on GFX9 only */
5109bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5119bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
5129bd392adSmrg				"compute ring bad slow dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
5139bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5149bd392adSmrg
5159bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5169bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
5179bd392adSmrg				"gfx ring bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
5189bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5199bd392adSmrg
5209bd392adSmrg	/* This test was ran on GFX9 only */
5219bd392adSmrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5229bd392adSmrg		if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
5239bd392adSmrg				"gfx ring slow bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
5249bd392adSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5259bd392adSmrg
5264babd585Smrg	if (amdgpu_set_test_active(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
5274babd585Smrg		fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5284babd585Smrg
52900a23bdaSmrg	/* This test was ran on GFX8 and GFX9 only */
53000a23bdaSmrg	if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
53100a23bdaSmrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
53200a23bdaSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5335324fb0dSmrg
5345324fb0dSmrg	/* This test was ran on GFX9 only */
53588f8a8d2Smrg	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
53688f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
53788f8a8d2Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
53888f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
5395324fb0dSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
54088f8a8d2Smrg	}
5415324fb0dSmrg
5425324fb0dSmrg	/* This test was ran on GFX9 only */
5435324fb0dSmrg	if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
5445324fb0dSmrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
5455324fb0dSmrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
54688f8a8d2Smrg
54788f8a8d2Smrg	/* This test was ran on GFX9 only */
54888f8a8d2Smrg	//if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
54988f8a8d2Smrg		if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
55088f8a8d2Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5514babd585Smrg
5524babd585Smrg	/* You need at least 2 devices for this	*/
5534babd585Smrg	if (drm_count < 2)
5544babd585Smrg		if (amdgpu_set_test_active(HOTUNPLUG_TESTS_STR, "Unplug with exported fence", CU_FALSE))
5554babd585Smrg			fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
5564babd585Smrg}
5574babd585Smrg
5584babd585Smrgint test_device_index;
5594babd585Smrg
5604babd585Smrgint amdgpu_open_device_on_test_index(int render_node)
5614babd585Smrg{
5624babd585Smrg	int i;
5634babd585Smrg
5644babd585Smrg	if (amdgpu_open_devices(open_render_node) <= 0) {
5654babd585Smrg		perror("Cannot open AMDGPU device");
5664babd585Smrg		return -1;
5674babd585Smrg	}
5684babd585Smrg
5694babd585Smrg	if (test_device_index >= 0) {
5704babd585Smrg		/* Most tests run on device of drm_amdgpu[0].
5714babd585Smrg		 * Swap the chosen device to drm_amdgpu[0].
5724babd585Smrg		 */
5734babd585Smrg		i = drm_amdgpu[0];
5744babd585Smrg		drm_amdgpu[0] = drm_amdgpu[test_device_index];
5754babd585Smrg		drm_amdgpu[test_device_index] = i;
5764babd585Smrg	}
5774babd585Smrg
5784babd585Smrg	return 0;
5794babd585Smrg
5804babd585Smrg
5814babd585Smrg}
5824babd585Smrg
5834babd585Smrg
5844babd585Smrgstatic bool amdgpu_node_is_drm(int maj, int min)
5854babd585Smrg{
5864babd585Smrg#ifdef __linux__
5874babd585Smrg    char path[64];
5884babd585Smrg    struct stat sbuf;
5894babd585Smrg
5904babd585Smrg    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
5914babd585Smrg             maj, min);
5924babd585Smrg    return stat(path, &sbuf) == 0;
5934babd585Smrg#elif defined(__FreeBSD__)
5944babd585Smrg    char name[SPECNAMELEN];
5954babd585Smrg
5964babd585Smrg    if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
5974babd585Smrg      return 0;
5984babd585Smrg    /* Handle drm/ and dri/ as both are present in different FreeBSD version
5994babd585Smrg     * FreeBSD on amd64/i386/powerpc external kernel modules create node in
6004babd585Smrg     * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
6014babd585Smrg     * only device nodes in /dev/dri/ */
6024babd585Smrg    return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
6034babd585Smrg#else
6044babd585Smrg    return maj == DRM_MAJOR;
6054babd585Smrg#endif
6064babd585Smrg}
6074babd585Smrg
6084babd585Smrgchar *amdgpu_get_device_from_fd(int fd)
6094babd585Smrg{
6104babd585Smrg#ifdef __linux__
6114babd585Smrg    struct stat sbuf;
6124babd585Smrg    char path[PATH_MAX + 1];
6134babd585Smrg    unsigned int maj, min;
6144babd585Smrg
6154babd585Smrg    if (fstat(fd, &sbuf))
6164babd585Smrg        return NULL;
6174babd585Smrg
6184babd585Smrg    maj = major(sbuf.st_rdev);
6194babd585Smrg    min = minor(sbuf.st_rdev);
6204babd585Smrg
6214babd585Smrg    if (!amdgpu_node_is_drm(maj, min) || !S_ISCHR(sbuf.st_mode))
6224babd585Smrg        return NULL;
6234babd585Smrg
6244babd585Smrg    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
6254babd585Smrg    return strdup(path);
6264babd585Smrg#else
6274babd585Smrg    return NULL;
6284babd585Smrg#endif
62900a23bdaSmrg}
63000a23bdaSmrg
6313f012e29Smrg/* The main() function for setting up and running the tests.
6323f012e29Smrg * Returns a CUE_SUCCESS on successful running, another
6333f012e29Smrg * CUnit error code on failure.
6343f012e29Smrg */
6353f012e29Smrgint main(int argc, char **argv)
6363f012e29Smrg{
6373f012e29Smrg	int c;			/* Character received from getopt */
6383f012e29Smrg	int i = 0;
6393f012e29Smrg	int suite_id = -1;	/* By default run everything */
6403f012e29Smrg	int test_id  = -1;	/* By default run all tests in the suite */
641037b3c26Smrg	int pci_bus_id = -1;    /* By default PC bus ID is not specified */
642037b3c26Smrg	int pci_device_id = 0;  /* By default PC device ID is zero */
643037b3c26Smrg	int display_devices = 0;/* By default not to display devices' info */
6443f012e29Smrg	CU_pSuite pSuite = NULL;
6453f012e29Smrg	CU_pTest  pTest  = NULL;
64600a23bdaSmrg	int display_list = 0;
64700a23bdaSmrg	int force_run = 0;
6483f012e29Smrg
6493f012e29Smrg	for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
6503f012e29Smrg		drm_amdgpu[i] = -1;
6513f012e29Smrg
6523f012e29Smrg
6533f012e29Smrg	/* Parse command line string */
6543f012e29Smrg	opterr = 0;		/* Do not print error messages from getopt */
6553f012e29Smrg	while ((c = getopt(argc, argv, options)) != -1) {
6563f012e29Smrg		switch (c) {
6573f012e29Smrg		case 'l':
65800a23bdaSmrg			display_list = 1;
65900a23bdaSmrg			break;
6603f012e29Smrg		case 's':
6613f012e29Smrg			suite_id = atoi(optarg);
6623f012e29Smrg			break;
6633f012e29Smrg		case 't':
6643f012e29Smrg			test_id = atoi(optarg);
6653f012e29Smrg			break;
666037b3c26Smrg		case 'b':
667037b3c26Smrg			pci_bus_id = atoi(optarg);
668037b3c26Smrg			break;
669037b3c26Smrg		case 'd':
670d8807b2fSmrg			sscanf(optarg, "%x", &pci_device_id);
671037b3c26Smrg			break;
672037b3c26Smrg		case 'p':
673037b3c26Smrg			display_devices = 1;
674037b3c26Smrg			break;
675037b3c26Smrg		case 'r':
676037b3c26Smrg			open_render_node = 1;
677037b3c26Smrg			break;
67800a23bdaSmrg		case 'f':
67900a23bdaSmrg			force_run = 1;
68000a23bdaSmrg			break;
6813f012e29Smrg		case '?':
6823f012e29Smrg		case 'h':
6833f012e29Smrg			fprintf(stderr, usage, argv[0]);
6843f012e29Smrg			exit(EXIT_SUCCESS);
6853f012e29Smrg		default:
6863f012e29Smrg			fprintf(stderr, usage, argv[0]);
6873f012e29Smrg			exit(EXIT_FAILURE);
6883f012e29Smrg		}
6893f012e29Smrg	}
6903f012e29Smrg
691037b3c26Smrg	if (amdgpu_open_devices(open_render_node) <= 0) {
692037b3c26Smrg		perror("Cannot open AMDGPU device");
6933f012e29Smrg		exit(EXIT_FAILURE);
6943f012e29Smrg	}
6953f012e29Smrg
696037b3c26Smrg	if (drm_amdgpu[0] < 0) {
697037b3c26Smrg		perror("Cannot open AMDGPU device");
6983f012e29Smrg		exit(EXIT_FAILURE);
6993f012e29Smrg	}
7003f012e29Smrg
701037b3c26Smrg	if (display_devices) {
702037b3c26Smrg		amdgpu_print_devices();
703037b3c26Smrg		amdgpu_close_devices();
704037b3c26Smrg		exit(EXIT_SUCCESS);
705037b3c26Smrg	}
7063f012e29Smrg
707d8807b2fSmrg	if (pci_bus_id > 0 || pci_device_id) {
708037b3c26Smrg		/* A device was specified to run the test */
709d8807b2fSmrg		test_device_index = amdgpu_find_device(pci_bus_id,
710d8807b2fSmrg						       pci_device_id);
711037b3c26Smrg
712037b3c26Smrg		if (test_device_index >= 0) {
713037b3c26Smrg			/* Most tests run on device of drm_amdgpu[0].
714037b3c26Smrg			 * Swap the chosen device to drm_amdgpu[0].
715037b3c26Smrg			 */
716037b3c26Smrg			i = drm_amdgpu[0];
717037b3c26Smrg			drm_amdgpu[0] = drm_amdgpu[test_device_index];
718037b3c26Smrg			drm_amdgpu[test_device_index] = i;
719037b3c26Smrg		} else {
720037b3c26Smrg			fprintf(stderr,
721037b3c26Smrg				"The specified GPU device does not exist.\n");
722037b3c26Smrg			exit(EXIT_FAILURE);
723037b3c26Smrg		}
724037b3c26Smrg	}
7253f012e29Smrg
7263f012e29Smrg	/* Initialize test suites to run */
7273f012e29Smrg
7283f012e29Smrg	/* initialize the CUnit test registry */
7293f012e29Smrg	if (CUE_SUCCESS != CU_initialize_registry()) {
730037b3c26Smrg		amdgpu_close_devices();
7313f012e29Smrg		return CU_get_error();
7323f012e29Smrg	}
7333f012e29Smrg
7343f012e29Smrg	/* Register suites. */
7353f012e29Smrg	if (CU_register_suites(suites) != CUE_SUCCESS) {
7363f012e29Smrg		fprintf(stderr, "suite registration failed - %s\n",
7373f012e29Smrg				CU_get_error_msg());
7383f012e29Smrg		CU_cleanup_registry();
739037b3c26Smrg		amdgpu_close_devices();
7403f012e29Smrg		exit(EXIT_FAILURE);
7413f012e29Smrg	}
7423f012e29Smrg
7433f012e29Smrg	/* Run tests using the CUnit Basic interface */
7443f012e29Smrg	CU_basic_set_mode(CU_BRM_VERBOSE);
7453f012e29Smrg
74600a23bdaSmrg	/* Disable suites and individual tests based on misc. conditions */
74700a23bdaSmrg	amdgpu_disable_suites();
74800a23bdaSmrg
74900a23bdaSmrg	if (display_list) {
75000a23bdaSmrg		display_test_suites();
75100a23bdaSmrg		goto end;
75200a23bdaSmrg	}
75300a23bdaSmrg
7543f012e29Smrg	if (suite_id != -1) {	/* If user specify particular suite? */
7553f012e29Smrg		pSuite = CU_get_suite_by_index((unsigned int) suite_id,
7563f012e29Smrg						CU_get_registry());
7573f012e29Smrg
7583f012e29Smrg		if (pSuite) {
75900a23bdaSmrg
76000a23bdaSmrg			if (force_run)
76100a23bdaSmrg				CU_set_suite_active(pSuite, CU_TRUE);
76200a23bdaSmrg
7633f012e29Smrg			if (test_id != -1) {   /* If user specify test id */
7643f012e29Smrg				pTest = CU_get_test_by_index(
7653f012e29Smrg						(unsigned int) test_id,
7663f012e29Smrg						pSuite);
76700a23bdaSmrg				if (pTest) {
76800a23bdaSmrg					if (force_run)
76900a23bdaSmrg						CU_set_test_active(pTest, CU_TRUE);
77000a23bdaSmrg
7713f012e29Smrg					CU_basic_run_test(pSuite, pTest);
77200a23bdaSmrg				}
7733f012e29Smrg				else {
7743f012e29Smrg					fprintf(stderr, "Invalid test id: %d\n",
7753f012e29Smrg								test_id);
7763f012e29Smrg					CU_cleanup_registry();
777037b3c26Smrg					amdgpu_close_devices();
7783f012e29Smrg					exit(EXIT_FAILURE);
7793f012e29Smrg				}
7803f012e29Smrg			} else
7813f012e29Smrg				CU_basic_run_suite(pSuite);
7823f012e29Smrg		} else {
7833f012e29Smrg			fprintf(stderr, "Invalid suite id : %d\n",
7843f012e29Smrg					suite_id);
7853f012e29Smrg			CU_cleanup_registry();
786037b3c26Smrg			amdgpu_close_devices();
7873f012e29Smrg			exit(EXIT_FAILURE);
7883f012e29Smrg		}
7893f012e29Smrg	} else
7903f012e29Smrg		CU_basic_run_tests();
7913f012e29Smrg
79200a23bdaSmrgend:
7933f012e29Smrg	CU_cleanup_registry();
794037b3c26Smrg	amdgpu_close_devices();
7953f012e29Smrg	return CU_get_error();
7963f012e29Smrg}
797