406d07a52963ccb65da7902ddf80a64452c9ffc7
[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(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
557                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
558
559         /* This test was ran on GFX8 and GFX9 only */
560         if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
561                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
562                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
563
564         /* This test was ran on GFX9 only */
565         if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV) {
566                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (GFX)", CU_FALSE))
567                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
568                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test (Compute)", CU_FALSE))
569                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
570         }
571
572         /* This test was ran on GFX9 only */
573         if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
574                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
575                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
576
577         /* This test was ran on GFX9 only */
578         //if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
579                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "GPU reset Test", CU_FALSE))
580                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
581
582         /* You need at least 2 devices for this */
583         if (drm_count < 2)
584                 if (amdgpu_set_test_active(HOTUNPLUG_TESTS_STR, "Unplug with exported fence", CU_FALSE))
585                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
586 }
587
588 int test_device_index;
589
590 int amdgpu_open_device_on_test_index(int render_node)
591 {
592         int i;
593
594         if (amdgpu_open_devices(open_render_node) <= 0) {
595                 perror("Cannot open AMDGPU device");
596                 return -1;
597         }
598
599         if (test_device_index >= 0) {
600                 /* Most tests run on device of drm_amdgpu[0].
601                  * Swap the chosen device to drm_amdgpu[0].
602                  */
603                 i = drm_amdgpu[0];
604                 drm_amdgpu[0] = drm_amdgpu[test_device_index];
605                 drm_amdgpu[test_device_index] = i;
606         }
607
608         return 0;
609
610
611 }
612
613
614 static bool amdgpu_node_is_drm(int maj, int min)
615 {
616 #ifdef __linux__
617     char path[64];
618     struct stat sbuf;
619
620     snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
621              maj, min);
622     return stat(path, &sbuf) == 0;
623 #elif defined(__FreeBSD__)
624     char name[SPECNAMELEN];
625
626     if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
627       return 0;
628     /* Handle drm/ and dri/ as both are present in different FreeBSD version
629      * FreeBSD on amd64/i386/powerpc external kernel modules create node in
630      * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
631      * only device nodes in /dev/dri/ */
632     return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
633 #else
634     return maj == DRM_MAJOR;
635 #endif
636 }
637
638 char *amdgpu_get_device_from_fd(int fd)
639 {
640 #ifdef __linux__
641     struct stat sbuf;
642     char path[PATH_MAX + 1];
643     unsigned int maj, min;
644
645     if (fstat(fd, &sbuf))
646         return NULL;
647
648     maj = major(sbuf.st_rdev);
649     min = minor(sbuf.st_rdev);
650
651     if (!amdgpu_node_is_drm(maj, min) || !S_ISCHR(sbuf.st_mode))
652         return NULL;
653
654     snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
655     return strdup(path);
656 #else
657     return NULL;
658 #endif
659 }
660
661 /* The main() function for setting up and running the tests.
662  * Returns a CUE_SUCCESS on successful running, another
663  * CUnit error code on failure.
664  */
665 int main(int argc, char **argv)
666 {
667         int c;                  /* Character received from getopt */
668         int i = 0;
669         int suite_id = -1;      /* By default run everything */
670         int test_id  = -1;      /* By default run all tests in the suite */
671         int pci_bus_id = -1;    /* By default PC bus ID is not specified */
672         int pci_device_id = 0;  /* By default PC device ID is zero */
673         int display_devices = 0;/* By default not to display devices' info */
674         CU_pSuite pSuite = NULL;
675         CU_pTest  pTest  = NULL;
676         int display_list = 0;
677         int force_run = 0;
678
679         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
680                 drm_amdgpu[i] = -1;
681
682
683         /* Parse command line string */
684         opterr = 0;             /* Do not print error messages from getopt */
685         while ((c = getopt(argc, argv, options)) != -1) {
686                 switch (c) {
687                 case 'l':
688                         display_list = 1;
689                         break;
690                 case 's':
691                         suite_id = atoi(optarg);
692                         break;
693                 case 't':
694                         test_id = atoi(optarg);
695                         break;
696                 case 'b':
697                         pci_bus_id = atoi(optarg);
698                         break;
699                 case 'd':
700                         sscanf(optarg, "%x", &pci_device_id);
701                         break;
702                 case 'p':
703                         display_devices = 1;
704                         break;
705                 case 'r':
706                         open_render_node = 1;
707                         break;
708                 case 'f':
709                         force_run = 1;
710                         break;
711                 case '?':
712                 case 'h':
713                         fprintf(stderr, usage, argv[0]);
714                         exit(EXIT_SUCCESS);
715                 default:
716                         fprintf(stderr, usage, argv[0]);
717                         exit(EXIT_FAILURE);
718                 }
719         }
720
721         if (amdgpu_open_devices(open_render_node) <= 0) {
722                 perror("Cannot open AMDGPU device");
723                 exit(EXIT_FAILURE);
724         }
725
726         if (drm_amdgpu[0] < 0) {
727                 perror("Cannot open AMDGPU device");
728                 exit(EXIT_FAILURE);
729         }
730
731         if (display_devices) {
732                 amdgpu_print_devices();
733                 amdgpu_close_devices();
734                 exit(EXIT_SUCCESS);
735         }
736
737         if (pci_bus_id > 0 || pci_device_id) {
738                 /* A device was specified to run the test */
739                 test_device_index = amdgpu_find_device(pci_bus_id,
740                                                        pci_device_id);
741
742                 if (test_device_index >= 0) {
743                         /* Most tests run on device of drm_amdgpu[0].
744                          * Swap the chosen device to drm_amdgpu[0].
745                          */
746                         i = drm_amdgpu[0];
747                         drm_amdgpu[0] = drm_amdgpu[test_device_index];
748                         drm_amdgpu[test_device_index] = i;
749                 } else {
750                         fprintf(stderr,
751                                 "The specified GPU device does not exist.\n");
752                         exit(EXIT_FAILURE);
753                 }
754         }
755
756         /* Initialize test suites to run */
757
758         /* initialize the CUnit test registry */
759         if (CUE_SUCCESS != CU_initialize_registry()) {
760                 amdgpu_close_devices();
761                 return CU_get_error();
762         }
763
764         /* Register suites. */
765         if (CU_register_suites(suites) != CUE_SUCCESS) {
766                 fprintf(stderr, "suite registration failed - %s\n",
767                                 CU_get_error_msg());
768                 CU_cleanup_registry();
769                 amdgpu_close_devices();
770                 exit(EXIT_FAILURE);
771         }
772
773         /* Run tests using the CUnit Basic interface */
774         CU_basic_set_mode(CU_BRM_VERBOSE);
775
776         /* Disable suites and individual tests based on misc. conditions */
777         amdgpu_disable_suites();
778
779         if (display_list) {
780                 display_test_suites();
781                 goto end;
782         }
783
784         if (suite_id != -1) {   /* If user specify particular suite? */
785                 pSuite = CU_get_suite_by_index((unsigned int) suite_id,
786                                                 CU_get_registry());
787
788                 if (pSuite) {
789
790                         if (force_run)
791                                 CU_set_suite_active(pSuite, CU_TRUE);
792
793                         if (test_id != -1) {   /* If user specify test id */
794                                 pTest = CU_get_test_by_index(
795                                                 (unsigned int) test_id,
796                                                 pSuite);
797                                 if (pTest) {
798                                         if (force_run)
799                                                 CU_set_test_active(pTest, CU_TRUE);
800
801                                         CU_basic_run_test(pSuite, pTest);
802                                 }
803                                 else {
804                                         fprintf(stderr, "Invalid test id: %d\n",
805                                                                 test_id);
806                                         CU_cleanup_registry();
807                                         amdgpu_close_devices();
808                                         exit(EXIT_FAILURE);
809                                 }
810                         } else
811                                 CU_basic_run_suite(pSuite);
812                 } else {
813                         fprintf(stderr, "Invalid suite id : %d\n",
814                                         suite_id);
815                         CU_cleanup_registry();
816                         amdgpu_close_devices();
817                         exit(EXIT_FAILURE);
818                 }
819         } else
820                 CU_basic_run_tests();
821
822 end:
823         CU_cleanup_registry();
824         amdgpu_close_devices();
825         return CU_get_error();
826 }