tests/amdgpu: add sdma slow copy linear hang test
[platform/upstream/libdrm.git] / tests / amdgpu / amdgpu_test.c
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 #ifdef __linux__
41 #include <linux/limits.h>
42 #elif __FreeBSD__
43 /* SPECNAMELEN in FreeBSD is defined here: */
44 #include <sys/param.h>
45 #endif
46 #ifdef MAJOR_IN_MKDEV
47 #include <sys/mkdev.h>
48 #endif
49 #ifdef MAJOR_IN_SYSMACROS
50 #include <sys/sysmacros.h>
51 #endif
52
53 #include "drm.h"
54 #include "xf86drmMode.h"
55 #include "xf86drm.h"
56
57 #include "CUnit/Basic.h"
58
59 #include "amdgpu_test.h"
60 #include "amdgpu_internal.h"
61
62 /* Test suite names */
63 #define BASIC_TESTS_STR "Basic Tests"
64 #define BO_TESTS_STR "BO Tests"
65 #define CS_TESTS_STR "CS Tests"
66 #define VCE_TESTS_STR "VCE Tests"
67 #define VCN_TESTS_STR "VCN Tests"
68 #define JPEG_TESTS_STR "JPEG Tests"
69 #define UVD_ENC_TESTS_STR "UVD ENC Tests"
70 #define DEADLOCK_TESTS_STR "Deadlock Tests"
71 #define VM_TESTS_STR "VM Tests"
72 #define RAS_TESTS_STR "RAS Tests"
73 #define SYNCOBJ_TIMELINE_TESTS_STR "SYNCOBJ TIMELINE Tests"
74 #define SECURITY_TESTS_STR "Security Tests"
75 #define HOTUNPLUG_TESTS_STR "Hotunplug Tests"
76 #define CP_DMA_TESTS_STR "CP DMA Tests"
77
78 /**
79  *  Open handles for amdgpu devices
80  *
81  */
82 int drm_amdgpu[MAX_CARDS_SUPPORTED];
83
84 /** Open render node to test */
85 int open_render_node = 0;       /* By default run most tests on primary node */
86
87 /** The table of all known test suites to run */
88 static CU_SuiteInfo suites[] = {
89         {
90                 .pName = BASIC_TESTS_STR,
91                 .pInitFunc = suite_basic_tests_init,
92                 .pCleanupFunc = suite_basic_tests_clean,
93                 .pTests = basic_tests,
94         },
95         {
96                 .pName = BO_TESTS_STR,
97                 .pInitFunc = suite_bo_tests_init,
98                 .pCleanupFunc = suite_bo_tests_clean,
99                 .pTests = bo_tests,
100         },
101         {
102                 .pName = CS_TESTS_STR,
103                 .pInitFunc = suite_cs_tests_init,
104                 .pCleanupFunc = suite_cs_tests_clean,
105                 .pTests = cs_tests,
106         },
107         {
108                 .pName = VCE_TESTS_STR,
109                 .pInitFunc = suite_vce_tests_init,
110                 .pCleanupFunc = suite_vce_tests_clean,
111                 .pTests = vce_tests,
112         },
113         {
114                 .pName = VCN_TESTS_STR,
115                 .pInitFunc = suite_vcn_tests_init,
116                 .pCleanupFunc = suite_vcn_tests_clean,
117                 .pTests = vcn_tests,
118         },
119         {
120                 .pName = JPEG_TESTS_STR,
121                 .pInitFunc = suite_jpeg_tests_init,
122                 .pCleanupFunc = suite_jpeg_tests_clean,
123                 .pTests = jpeg_tests,
124         },
125         {
126                 .pName = UVD_ENC_TESTS_STR,
127                 .pInitFunc = suite_uvd_enc_tests_init,
128                 .pCleanupFunc = suite_uvd_enc_tests_clean,
129                 .pTests = uvd_enc_tests,
130         },
131         {
132                 .pName = DEADLOCK_TESTS_STR,
133                 .pInitFunc = suite_deadlock_tests_init,
134                 .pCleanupFunc = suite_deadlock_tests_clean,
135                 .pTests = deadlock_tests,
136         },
137         {
138                 .pName = VM_TESTS_STR,
139                 .pInitFunc = suite_vm_tests_init,
140                 .pCleanupFunc = suite_vm_tests_clean,
141                 .pTests = vm_tests,
142         },
143         {
144                 .pName = RAS_TESTS_STR,
145                 .pInitFunc = suite_ras_tests_init,
146                 .pCleanupFunc = suite_ras_tests_clean,
147                 .pTests = ras_tests,
148         },
149         {
150                 .pName = SYNCOBJ_TIMELINE_TESTS_STR,
151                 .pInitFunc = suite_syncobj_timeline_tests_init,
152                 .pCleanupFunc = suite_syncobj_timeline_tests_clean,
153                 .pTests = syncobj_timeline_tests,
154         },
155         {
156                 .pName = SECURITY_TESTS_STR,
157                 .pInitFunc = suite_security_tests_init,
158                 .pCleanupFunc = suite_security_tests_clean,
159                 .pTests = security_tests,
160         },
161         {
162                 .pName = HOTUNPLUG_TESTS_STR,
163                 .pInitFunc = suite_hotunplug_tests_init,
164                 .pCleanupFunc = suite_hotunplug_tests_clean,
165                 .pTests = hotunplug_tests,
166         },
167         {
168                 .pName = CP_DMA_TESTS_STR,
169                 .pInitFunc = suite_cp_dma_tests_init,
170                 .pCleanupFunc = suite_cp_dma_tests_clean,
171                 .pTests = cp_dma_tests,
172         },
173
174         CU_SUITE_INFO_NULL,
175 };
176
177 typedef CU_BOOL (*active__stat_func)(void);
178
179 typedef struct Suites_Active_Status {
180         char*             pName;
181         active__stat_func pActive;
182 }Suites_Active_Status;
183
184 static CU_BOOL always_active()
185 {
186         return CU_TRUE;
187 }
188
189 static Suites_Active_Status suites_active_stat[] = {
190                 {
191                         .pName = BASIC_TESTS_STR,
192                         .pActive = suite_basic_tests_enable,
193                 },
194                 {
195                         .pName = BO_TESTS_STR,
196                         .pActive = always_active,
197                 },
198                 {
199                         .pName = CS_TESTS_STR,
200                         .pActive = suite_cs_tests_enable,
201                 },
202                 {
203                         .pName = VCE_TESTS_STR,
204                         .pActive = suite_vce_tests_enable,
205                 },
206                 {
207                         .pName = VCN_TESTS_STR,
208                         .pActive = suite_vcn_tests_enable,
209                 },
210                 {
211                         .pName = JPEG_TESTS_STR,
212                         .pActive = suite_jpeg_tests_enable,
213                 },
214                 {
215                         .pName = UVD_ENC_TESTS_STR,
216                         .pActive = suite_uvd_enc_tests_enable,
217                 },
218                 {
219                         .pName = DEADLOCK_TESTS_STR,
220                         .pActive = suite_deadlock_tests_enable,
221                 },
222                 {
223                         .pName = VM_TESTS_STR,
224                         .pActive = suite_vm_tests_enable,
225                 },
226                 {
227                         .pName = RAS_TESTS_STR,
228                         .pActive = suite_ras_tests_enable,
229                 },
230                 {
231                         .pName = SYNCOBJ_TIMELINE_TESTS_STR,
232                         .pActive = suite_syncobj_timeline_tests_enable,
233                 },
234                 {
235                         .pName = SECURITY_TESTS_STR,
236                         .pActive = suite_security_tests_enable,
237                 },
238                 {
239                         .pName = HOTUNPLUG_TESTS_STR,
240                         .pActive = suite_hotunplug_tests_enable,
241                 },
242                 {
243                         .pName = CP_DMA_TESTS_STR,
244                         .pActive = suite_cp_dma_tests_enable,
245                 },
246 };
247
248
249 /*
250  * Display information about all  suites and their tests
251  *
252  * NOTE: Must be run after registry is initialized and suites registered.
253  */
254 static void display_test_suites(void)
255 {
256         int iSuite;
257         int iTest;
258         CU_pSuite pSuite = NULL;
259         CU_pTest  pTest  = NULL;
260
261         printf("%5s: %2s: %8s: %s\n", "What", "ID", "Status", "Name");
262
263         for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
264
265                 pSuite = CU_get_suite_by_index((unsigned int) iSuite + 1,
266                                                CU_get_registry());
267
268                 if (!pSuite) {
269                         fprintf(stderr, "Invalid suite id : %d\n", iSuite + 1);
270                         continue;
271                 }
272
273                 printf("Suite: %2d: %8s: %s\n",
274                        iSuite + 1,
275                        pSuite->fActive ? "ENABLED" : "DISABLED",
276                        suites[iSuite].pName);
277
278                 if (!pSuite->fActive)
279                         continue;
280
281                 for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
282                      iTest++) {
283                         pTest = CU_get_test_by_index((unsigned int) iTest + 1,
284                                                      pSuite);
285                         if (!pTest) {
286                                 fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
287                                 continue;
288                         }
289                         printf(" Test: %2d: %8s: %s\n",
290                                iTest + 1,
291                                pSuite->fActive && pTest->fActive ? "ENABLED" : "DISABLED",
292                                suites[iSuite].pTests[iTest].pName);
293                 }
294         }
295 }
296
297 /** Help string for command line parameters */
298 static const char usage[] =
299         "Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
300         "[-b <pci_bus_id> [-d <pci_device_id>]]\n"
301         "where:\n"
302         "       l - Display all suites and their tests\n"
303         "       r - Run the tests on render node\n"
304         "       b - Specify device's PCI bus id to run tests\n"
305         "       d - Specify device's PCI device id to run tests (optional)\n"
306         "       p - Display information of AMDGPU devices in system\n"
307         "       f - Force executing inactive suite or test\n"
308         "       h - Display this help\n";
309 /** Specified options strings for getopt */
310 static const char options[]   = "hlrps:t:b:d:f";
311
312 /* Open AMD devices.
313  * Return the number of AMD device opened.
314  */
315 static int amdgpu_open_devices(int open_render_node)
316 {
317         drmDevicePtr devices[MAX_CARDS_SUPPORTED];
318         int i;
319         int drm_node;
320         int amd_index = 0;
321         int drm_count;
322         int fd;
323         drmVersionPtr version;
324
325         for (i = 0; i < MAX_CARDS_SUPPORTED; i++) {
326                 drm_amdgpu[i] = -1;
327         }
328
329         drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
330
331         if (drm_count < 0) {
332                 fprintf(stderr,
333                         "drmGetDevices2() returned an error %d\n",
334                         drm_count);
335                 return 0;
336         }
337
338         for (i = 0; i < drm_count; i++) {
339                 /* If this is not PCI device, skip*/
340                 if (devices[i]->bustype != DRM_BUS_PCI)
341                         continue;
342
343                 /* If this is not AMD GPU vender ID, skip*/
344                 if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
345                         continue;
346
347                 if (open_render_node)
348                         drm_node = DRM_NODE_RENDER;
349                 else
350                         drm_node = DRM_NODE_PRIMARY;
351
352                 fd = -1;
353                 if (devices[i]->available_nodes & 1 << drm_node)
354                         fd = open(
355                                 devices[i]->nodes[drm_node],
356                                 O_RDWR | O_CLOEXEC);
357
358                 /* This node is not available. */
359                 if (fd < 0) continue;
360
361                 version = drmGetVersion(fd);
362                 if (!version) {
363                         fprintf(stderr,
364                                 "Warning: Cannot get version for %s."
365                                 "Error is %s\n",
366                                 devices[i]->nodes[drm_node],
367                                 strerror(errno));
368                         close(fd);
369                         continue;
370                 }
371
372                 if (strcmp(version->name, "amdgpu")) {
373                         /* This is not AMDGPU driver, skip.*/
374                         drmFreeVersion(version);
375                         close(fd);
376                         continue;
377                 }
378
379                 drmFreeVersion(version);
380
381                 drm_amdgpu[amd_index] = fd;
382                 amd_index++;
383         }
384
385         drmFreeDevices(devices, drm_count);
386         return amd_index;
387 }
388
389 /* Close AMD devices.
390  */
391 void amdgpu_close_devices()
392 {
393         int i;
394         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
395                 if (drm_amdgpu[i] >=0) {
396                         close(drm_amdgpu[i]);
397                 }
398 }
399
400 /* Print AMD devices information */
401 static void amdgpu_print_devices()
402 {
403         int i;
404         drmDevicePtr device;
405
406         /* Open the first AMD device to print driver information. */
407         if (drm_amdgpu[0] >=0) {
408                 /* Display AMD driver version information.*/
409                 drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
410
411                 if (retval == NULL) {
412                         perror("Cannot get version for AMDGPU device");
413                         return;
414                 }
415
416                 printf("Driver name: %s, Date: %s, Description: %s.\n",
417                         retval->name, retval->date, retval->desc);
418                 drmFreeVersion(retval);
419         }
420
421         /* Display information of AMD devices */
422         printf("Devices:\n");
423         for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
424                 if (drmGetDevice2(drm_amdgpu[i],
425                         DRM_DEVICE_GET_PCI_REVISION,
426                         &device) == 0) {
427                         if (device->bustype == DRM_BUS_PCI) {
428                                 printf("PCI ");
429                                 printf(" domain:%04x",
430                                         device->businfo.pci->domain);
431                                 printf(" bus:%02x",
432                                         device->businfo.pci->bus);
433                                 printf(" device:%02x",
434                                         device->businfo.pci->dev);
435                                 printf(" function:%01x",
436                                         device->businfo.pci->func);
437                                 printf(" vendor_id:%04x",
438                                         device->deviceinfo.pci->vendor_id);
439                                 printf(" device_id:%04x",
440                                         device->deviceinfo.pci->device_id);
441                                 printf(" subvendor_id:%04x",
442                                         device->deviceinfo.pci->subvendor_id);
443                                 printf(" subdevice_id:%04x",
444                                         device->deviceinfo.pci->subdevice_id);
445                                 printf(" revision_id:%02x",
446                                         device->deviceinfo.pci->revision_id);
447                                 printf("\n");
448                         }
449                         drmFreeDevice(&device);
450                 }
451 }
452
453 /* Find a match AMD device in PCI bus
454  * Return the index of the device or -1 if not found
455  */
456 static int amdgpu_find_device(uint8_t bus, uint16_t dev)
457 {
458         int i;
459         drmDevicePtr device;
460
461         for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
462                 if (drmGetDevice2(drm_amdgpu[i],
463                         DRM_DEVICE_GET_PCI_REVISION,
464                         &device) == 0) {
465                         if (device->bustype == DRM_BUS_PCI)
466                                 if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
467                                         device->deviceinfo.pci->device_id == dev) {
468                                         drmFreeDevice(&device);
469                                         return i;
470                                 }
471
472                         drmFreeDevice(&device);
473                 }
474         }
475
476         return -1;
477 }
478
479 static void amdgpu_disable_suites()
480 {
481         amdgpu_device_handle device_handle;
482         uint32_t major_version, minor_version, family_id;
483         drmDevicePtr devices[MAX_CARDS_SUPPORTED];
484         int i, drm_count;
485         int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
486
487         if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
488                                    &minor_version, &device_handle))
489                 return;
490
491         family_id = device_handle->info.family_id;
492
493         if (amdgpu_device_deinitialize(device_handle))
494                 return;
495
496         drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
497
498         /* Set active status for suites based on their policies */
499         for (i = 0; i < size; ++i)
500                 if (amdgpu_set_suite_active(suites_active_stat[i].pName,
501                                 suites_active_stat[i].pActive()))
502                         fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
503
504         /* Explicitly disable specific tests due to known bugs or preferences */
505         /*
506         * BUG: Compute ring stalls and never recovers when the address is
507         * written after the command already submitted
508         */
509         if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
510                         "compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
511                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
512
513         if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
514                                 "sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
515                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
516
517         /* This test was ran on GFX9 only */
518         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
519                 if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
520                                 "gfx ring bad dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
521                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
522
523         /* This test was ran on GFX9 only */
524         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
525                 if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
526                                 "compute ring bad dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
527                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
528
529         /* This test was ran on GFX9 only */
530         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
531                 if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
532                                 "gfx ring bad slow dispatch test (set amdgpu.lockup_timeout=50)", CU_FALSE))
533                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
534
535         /* This test was ran on GFX9 only */
536         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
537                 if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
538                                 "compute ring bad slow dispatch test (set amdgpu.lockup_timeout=50,50)", CU_FALSE))
539                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
540
541         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
542                 if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
543                                 "gfx ring bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
544                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
545
546         /* This test was ran on GFX9 only */
547         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
548                 if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
549                                 "gfx ring slow bad draw test (set amdgpu.lockup_timeout=50)", CU_FALSE))
550                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
551
552         if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
553                         "sdma ring corrupted header test (set amdgpu.lockup_timeout=50)", CU_FALSE))
554                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
555
556         if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
557                         "sdma ring slow linear copy test (set amdgpu.lockup_timeout=50)", CU_FALSE))
558                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
559
560         if (amdgpu_set_test_active(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
561                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
562
563         /* This test was ran on GFX8 and GFX9 only */
564         if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
565                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
566                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
567
568         /* This test was ran on GFX9 only */
569         if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
570                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
571                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
572                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
573                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
574         }
575
576         /* This test was ran on GFX9 only */
577         if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
578                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
579                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
580
581         /* This test was ran on GFX9 only */
582         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
583                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
584                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
585
586         /* You need at least 2 devices for this */
587         if (drm_count < 2)
588                 if (amdgpu_set_test_active(HOTUNPLUG_TESTS_STR, "Unplug with exported fence", CU_FALSE))
589                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
590 }
591
592 int test_device_index;
593
594 int amdgpu_open_device_on_test_index(int render_node)
595 {
596         int i;
597
598         if (amdgpu_open_devices(open_render_node) <= 0) {
599                 perror("Cannot open AMDGPU device");
600                 return -1;
601         }
602
603         if (test_device_index >= 0) {
604                 /* Most tests run on device of drm_amdgpu[0].
605                  * Swap the chosen device to drm_amdgpu[0].
606                  */
607                 i = drm_amdgpu[0];
608                 drm_amdgpu[0] = drm_amdgpu[test_device_index];
609                 drm_amdgpu[test_device_index] = i;
610         }
611
612         return 0;
613
614
615 }
616
617
618 static bool amdgpu_node_is_drm(int maj, int min)
619 {
620 #ifdef __linux__
621     char path[64];
622     struct stat sbuf;
623
624     snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
625              maj, min);
626     return stat(path, &sbuf) == 0;
627 #elif defined(__FreeBSD__)
628     char name[SPECNAMELEN];
629
630     if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
631       return 0;
632     /* Handle drm/ and dri/ as both are present in different FreeBSD version
633      * FreeBSD on amd64/i386/powerpc external kernel modules create node in
634      * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
635      * only device nodes in /dev/dri/ */
636     return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
637 #else
638     return maj == DRM_MAJOR;
639 #endif
640 }
641
642 char *amdgpu_get_device_from_fd(int fd)
643 {
644 #ifdef __linux__
645     struct stat sbuf;
646     char path[PATH_MAX + 1];
647     unsigned int maj, min;
648
649     if (fstat(fd, &sbuf))
650         return NULL;
651
652     maj = major(sbuf.st_rdev);
653     min = minor(sbuf.st_rdev);
654
655     if (!amdgpu_node_is_drm(maj, min) || !S_ISCHR(sbuf.st_mode))
656         return NULL;
657
658     snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
659     return strdup(path);
660 #else
661     return NULL;
662 #endif
663 }
664
665 /* The main() function for setting up and running the tests.
666  * Returns a CUE_SUCCESS on successful running, another
667  * CUnit error code on failure.
668  */
669 int main(int argc, char **argv)
670 {
671         int c;                  /* Character received from getopt */
672         int i = 0;
673         int suite_id = -1;      /* By default run everything */
674         int test_id  = -1;      /* By default run all tests in the suite */
675         int pci_bus_id = -1;    /* By default PC bus ID is not specified */
676         int pci_device_id = 0;  /* By default PC device ID is zero */
677         int display_devices = 0;/* By default not to display devices' info */
678         CU_pSuite pSuite = NULL;
679         CU_pTest  pTest  = NULL;
680         int display_list = 0;
681         int force_run = 0;
682
683         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
684                 drm_amdgpu[i] = -1;
685
686
687         /* Parse command line string */
688         opterr = 0;             /* Do not print error messages from getopt */
689         while ((c = getopt(argc, argv, options)) != -1) {
690                 switch (c) {
691                 case 'l':
692                         display_list = 1;
693                         break;
694                 case 's':
695                         suite_id = atoi(optarg);
696                         break;
697                 case 't':
698                         test_id = atoi(optarg);
699                         break;
700                 case 'b':
701                         pci_bus_id = atoi(optarg);
702                         break;
703                 case 'd':
704                         sscanf(optarg, "%x", &pci_device_id);
705                         break;
706                 case 'p':
707                         display_devices = 1;
708                         break;
709                 case 'r':
710                         open_render_node = 1;
711                         break;
712                 case 'f':
713                         force_run = 1;
714                         break;
715                 case '?':
716                 case 'h':
717                         fprintf(stderr, usage, argv[0]);
718                         exit(EXIT_SUCCESS);
719                 default:
720                         fprintf(stderr, usage, argv[0]);
721                         exit(EXIT_FAILURE);
722                 }
723         }
724
725         if (amdgpu_open_devices(open_render_node) <= 0) {
726                 perror("Cannot open AMDGPU device");
727                 exit(EXIT_FAILURE);
728         }
729
730         if (drm_amdgpu[0] < 0) {
731                 perror("Cannot open AMDGPU device");
732                 exit(EXIT_FAILURE);
733         }
734
735         if (display_devices) {
736                 amdgpu_print_devices();
737                 amdgpu_close_devices();
738                 exit(EXIT_SUCCESS);
739         }
740
741         if (pci_bus_id > 0 || pci_device_id) {
742                 /* A device was specified to run the test */
743                 test_device_index = amdgpu_find_device(pci_bus_id,
744                                                        pci_device_id);
745
746                 if (test_device_index >= 0) {
747                         /* Most tests run on device of drm_amdgpu[0].
748                          * Swap the chosen device to drm_amdgpu[0].
749                          */
750                         i = drm_amdgpu[0];
751                         drm_amdgpu[0] = drm_amdgpu[test_device_index];
752                         drm_amdgpu[test_device_index] = i;
753                 } else {
754                         fprintf(stderr,
755                                 "The specified GPU device does not exist.\n");
756                         exit(EXIT_FAILURE);
757                 }
758         }
759
760         /* Initialize test suites to run */
761
762         /* initialize the CUnit test registry */
763         if (CUE_SUCCESS != CU_initialize_registry()) {
764                 amdgpu_close_devices();
765                 return CU_get_error();
766         }
767
768         /* Register suites. */
769         if (CU_register_suites(suites) != CUE_SUCCESS) {
770                 fprintf(stderr, "suite registration failed - %s\n",
771                                 CU_get_error_msg());
772                 CU_cleanup_registry();
773                 amdgpu_close_devices();
774                 exit(EXIT_FAILURE);
775         }
776
777         /* Run tests using the CUnit Basic interface */
778         CU_basic_set_mode(CU_BRM_VERBOSE);
779
780         /* Disable suites and individual tests based on misc. conditions */
781         amdgpu_disable_suites();
782
783         if (display_list) {
784                 display_test_suites();
785                 goto end;
786         }
787
788         if (suite_id != -1) {   /* If user specify particular suite? */
789                 pSuite = CU_get_suite_by_index((unsigned int) suite_id,
790                                                 CU_get_registry());
791
792                 if (pSuite) {
793
794                         if (force_run)
795                                 CU_set_suite_active(pSuite, CU_TRUE);
796
797                         if (test_id != -1) {   /* If user specify test id */
798                                 pTest = CU_get_test_by_index(
799                                                 (unsigned int) test_id,
800                                                 pSuite);
801                                 if (pTest) {
802                                         if (force_run)
803                                                 CU_set_test_active(pTest, CU_TRUE);
804
805                                         CU_basic_run_test(pSuite, pTest);
806                                 }
807                                 else {
808                                         fprintf(stderr, "Invalid test id: %d\n",
809                                                                 test_id);
810                                         CU_cleanup_registry();
811                                         amdgpu_close_devices();
812                                         exit(EXIT_FAILURE);
813                                 }
814                         } else
815                                 CU_basic_run_suite(pSuite);
816                 } else {
817                         fprintf(stderr, "Invalid suite id : %d\n",
818                                         suite_id);
819                         CU_cleanup_registry();
820                         amdgpu_close_devices();
821                         exit(EXIT_FAILURE);
822                 }
823         } else
824                 CU_basic_run_tests();
825
826 end:
827         CU_cleanup_registry();
828         amdgpu_close_devices();
829         return CU_get_error();
830 }