OSDN Git Service

fix various typos
[android-x86/external-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
41 #include "drm.h"
42 #include "xf86drmMode.h"
43 #include "xf86drm.h"
44
45 #include "CUnit/Basic.h"
46
47 #include "amdgpu_test.h"
48 #include "amdgpu_internal.h"
49
50 /* Test suite names */
51 #define BASIC_TESTS_STR "Basic Tests"
52 #define BO_TESTS_STR "BO Tests"
53 #define CS_TESTS_STR "CS Tests"
54 #define VCE_TESTS_STR "VCE Tests"
55 #define VCN_TESTS_STR "VCN Tests"
56 #define UVD_ENC_TESTS_STR "UVD ENC Tests"
57 #define DEADLOCK_TESTS_STR "Deadlock Tests"
58 #define VM_TESTS_STR "VM Tests"
59 #define RAS_TESTS_STR "RAS Tests"
60
61 /**
62  *  Open handles for amdgpu devices
63  *
64  */
65 int drm_amdgpu[MAX_CARDS_SUPPORTED];
66
67 /** Open render node to test */
68 int open_render_node = 0;       /* By default run most tests on primary node */
69
70 /** The table of all known test suites to run */
71 static CU_SuiteInfo suites[] = {
72         {
73                 .pName = BASIC_TESTS_STR,
74                 .pInitFunc = suite_basic_tests_init,
75                 .pCleanupFunc = suite_basic_tests_clean,
76                 .pTests = basic_tests,
77         },
78         {
79                 .pName = BO_TESTS_STR,
80                 .pInitFunc = suite_bo_tests_init,
81                 .pCleanupFunc = suite_bo_tests_clean,
82                 .pTests = bo_tests,
83         },
84         {
85                 .pName = CS_TESTS_STR,
86                 .pInitFunc = suite_cs_tests_init,
87                 .pCleanupFunc = suite_cs_tests_clean,
88                 .pTests = cs_tests,
89         },
90         {
91                 .pName = VCE_TESTS_STR,
92                 .pInitFunc = suite_vce_tests_init,
93                 .pCleanupFunc = suite_vce_tests_clean,
94                 .pTests = vce_tests,
95         },
96         {
97                 .pName = VCN_TESTS_STR,
98                 .pInitFunc = suite_vcn_tests_init,
99                 .pCleanupFunc = suite_vcn_tests_clean,
100                 .pTests = vcn_tests,
101         },
102         {
103                 .pName = UVD_ENC_TESTS_STR,
104                 .pInitFunc = suite_uvd_enc_tests_init,
105                 .pCleanupFunc = suite_uvd_enc_tests_clean,
106                 .pTests = uvd_enc_tests,
107         },
108         {
109                 .pName = DEADLOCK_TESTS_STR,
110                 .pInitFunc = suite_deadlock_tests_init,
111                 .pCleanupFunc = suite_deadlock_tests_clean,
112                 .pTests = deadlock_tests,
113         },
114         {
115                 .pName = VM_TESTS_STR,
116                 .pInitFunc = suite_vm_tests_init,
117                 .pCleanupFunc = suite_vm_tests_clean,
118                 .pTests = vm_tests,
119         },
120         {
121                 .pName = RAS_TESTS_STR,
122                 .pInitFunc = suite_ras_tests_init,
123                 .pCleanupFunc = suite_ras_tests_clean,
124                 .pTests = ras_tests,
125         },
126
127         CU_SUITE_INFO_NULL,
128 };
129
130 typedef CU_BOOL (*active__stat_func)(void);
131
132 typedef struct Suites_Active_Status {
133         char*             pName;
134         active__stat_func pActive;
135 }Suites_Active_Status;
136
137 static CU_BOOL always_active()
138 {
139         return CU_TRUE;
140 }
141
142 static Suites_Active_Status suites_active_stat[] = {
143                 {
144                         .pName = BASIC_TESTS_STR,
145                         .pActive = always_active,
146                 },
147                 {
148                         .pName = BO_TESTS_STR,
149                         .pActive = always_active,
150                 },
151                 {
152                         .pName = CS_TESTS_STR,
153                         .pActive = suite_cs_tests_enable,
154                 },
155                 {
156                         .pName = VCE_TESTS_STR,
157                         .pActive = suite_vce_tests_enable,
158                 },
159                 {
160                         .pName = VCN_TESTS_STR,
161                         .pActive = suite_vcn_tests_enable,
162                 },
163                 {
164                         .pName = UVD_ENC_TESTS_STR,
165                         .pActive = suite_uvd_enc_tests_enable,
166                 },
167                 {
168                         .pName = DEADLOCK_TESTS_STR,
169                         .pActive = suite_deadlock_tests_enable,
170                 },
171                 {
172                         .pName = VM_TESTS_STR,
173                         .pActive = suite_vm_tests_enable,
174                 },
175                 {
176                         .pName = RAS_TESTS_STR,
177                         .pActive = suite_ras_tests_enable,
178                 },
179 };
180
181
182 /*
183  * Display information about all  suites and their tests
184  *
185  * NOTE: Must be run after registry is initialized and suites registered.
186  */
187 static void display_test_suites(void)
188 {
189         int iSuite;
190         int iTest;
191         CU_pSuite pSuite = NULL;
192         CU_pTest  pTest  = NULL;
193
194         printf("Suites\n");
195
196         for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
197
198                 pSuite = CU_get_suite_by_index((unsigned int) iSuite + 1,
199                                                       CU_get_registry());
200
201                 if (!pSuite) {
202                         fprintf(stderr, "Invalid suite id : %d\n", iSuite + 1);
203                         continue;
204                 }
205
206                 printf("Suite id = %d: Name '%s status: %s'\n",
207                                 iSuite + 1, suites[iSuite].pName,
208                                 pSuite->fActive ? "ENABLED" : "DISABLED");
209
210
211
212                 for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
213                         iTest++) {
214
215                         pTest = CU_get_test_by_index((unsigned int) iTest + 1,
216                                                                         pSuite);
217
218                         if (!pTest) {
219                                 fprintf(stderr, "Invalid test id : %d\n", iTest + 1);
220                                 continue;
221                         }
222
223                         printf("Test id %d: Name: '%s status: %s'\n", iTest + 1,
224                                         suites[iSuite].pTests[iTest].pName,
225                                         pSuite->fActive && pTest->fActive ?
226                                                      "ENABLED" : "DISABLED");
227                 }
228         }
229 }
230
231
232 /** Help string for command line parameters */
233 static const char usage[] =
234         "Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>] [-f]] "
235         "[-b <pci_bus_id> [-d <pci_device_id>]]\n"
236         "where:\n"
237         "       l - Display all suites and their tests\n"
238         "       r - Run the tests on render node\n"
239         "       b - Specify device's PCI bus id to run tests\n"
240         "       d - Specify device's PCI device id to run tests (optional)\n"
241         "       p - Display information of AMDGPU devices in system\n"
242         "       f - Force executing inactive suite or test\n"
243         "       h - Display this help\n";
244 /** Specified options strings for getopt */
245 static const char options[]   = "hlrps:t:b:d:f";
246
247 /* Open AMD devices.
248  * Return the number of AMD device opened.
249  */
250 static int amdgpu_open_devices(int open_render_node)
251 {
252         drmDevicePtr devices[MAX_CARDS_SUPPORTED];
253         int i;
254         int drm_node;
255         int amd_index = 0;
256         int drm_count;
257         int fd;
258         drmVersionPtr version;
259
260         drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
261
262         if (drm_count < 0) {
263                 fprintf(stderr,
264                         "drmGetDevices2() returned an error %d\n",
265                         drm_count);
266                 return 0;
267         }
268
269         for (i = 0; i < drm_count; i++) {
270                 /* If this is not PCI device, skip*/
271                 if (devices[i]->bustype != DRM_BUS_PCI)
272                         continue;
273
274                 /* If this is not AMD GPU vender ID, skip*/
275                 if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
276                         continue;
277
278                 if (open_render_node)
279                         drm_node = DRM_NODE_RENDER;
280                 else
281                         drm_node = DRM_NODE_PRIMARY;
282
283                 fd = -1;
284                 if (devices[i]->available_nodes & 1 << drm_node)
285                         fd = open(
286                                 devices[i]->nodes[drm_node],
287                                 O_RDWR | O_CLOEXEC);
288
289                 /* This node is not available. */
290                 if (fd < 0) continue;
291
292                 version = drmGetVersion(fd);
293                 if (!version) {
294                         fprintf(stderr,
295                                 "Warning: Cannot get version for %s."
296                                 "Error is %s\n",
297                                 devices[i]->nodes[drm_node],
298                                 strerror(errno));
299                         close(fd);
300                         continue;
301                 }
302
303                 if (strcmp(version->name, "amdgpu")) {
304                         /* This is not AMDGPU driver, skip.*/
305                         drmFreeVersion(version);
306                         close(fd);
307                         continue;
308                 }
309
310                 drmFreeVersion(version);
311
312                 drm_amdgpu[amd_index] = fd;
313                 amd_index++;
314         }
315
316         drmFreeDevices(devices, drm_count);
317         return amd_index;
318 }
319
320 /* Close AMD devices.
321  */
322 static void amdgpu_close_devices()
323 {
324         int i;
325         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
326                 if (drm_amdgpu[i] >=0)
327                         close(drm_amdgpu[i]);
328 }
329
330 /* Print AMD devices information */
331 static void amdgpu_print_devices()
332 {
333         int i;
334         drmDevicePtr device;
335
336         /* Open the first AMD device to print driver information. */
337         if (drm_amdgpu[0] >=0) {
338                 /* Display AMD driver version information.*/
339                 drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
340
341                 if (retval == NULL) {
342                         perror("Cannot get version for AMDGPU device");
343                         return;
344                 }
345
346                 printf("Driver name: %s, Date: %s, Description: %s.\n",
347                         retval->name, retval->date, retval->desc);
348                 drmFreeVersion(retval);
349         }
350
351         /* Display information of AMD devices */
352         printf("Devices:\n");
353         for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
354                 if (drmGetDevice2(drm_amdgpu[i],
355                         DRM_DEVICE_GET_PCI_REVISION,
356                         &device) == 0) {
357                         if (device->bustype == DRM_BUS_PCI) {
358                                 printf("PCI ");
359                                 printf(" domain:%04x",
360                                         device->businfo.pci->domain);
361                                 printf(" bus:%02x",
362                                         device->businfo.pci->bus);
363                                 printf(" device:%02x",
364                                         device->businfo.pci->dev);
365                                 printf(" function:%01x",
366                                         device->businfo.pci->func);
367                                 printf(" vendor_id:%04x",
368                                         device->deviceinfo.pci->vendor_id);
369                                 printf(" device_id:%04x",
370                                         device->deviceinfo.pci->device_id);
371                                 printf(" subvendor_id:%04x",
372                                         device->deviceinfo.pci->subvendor_id);
373                                 printf(" subdevice_id:%04x",
374                                         device->deviceinfo.pci->subdevice_id);
375                                 printf(" revision_id:%02x",
376                                         device->deviceinfo.pci->revision_id);
377                                 printf("\n");
378                         }
379                         drmFreeDevice(&device);
380                 }
381 }
382
383 /* Find a match AMD device in PCI bus
384  * Return the index of the device or -1 if not found
385  */
386 static int amdgpu_find_device(uint8_t bus, uint16_t dev)
387 {
388         int i;
389         drmDevicePtr device;
390
391         for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
392                 if (drmGetDevice2(drm_amdgpu[i],
393                         DRM_DEVICE_GET_PCI_REVISION,
394                         &device) == 0) {
395                         if (device->bustype == DRM_BUS_PCI)
396                                 if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
397                                         device->deviceinfo.pci->device_id == dev) {
398                                         drmFreeDevice(&device);
399                                         return i;
400                                 }
401
402                         drmFreeDevice(&device);
403                 }
404         }
405
406         return -1;
407 }
408
409 static void amdgpu_disable_suites()
410 {
411         amdgpu_device_handle device_handle;
412         uint32_t major_version, minor_version, family_id;
413         int i;
414         int size = sizeof(suites_active_stat) / sizeof(suites_active_stat[0]);
415
416         if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
417                                    &minor_version, &device_handle))
418                 return;
419
420         family_id = device_handle->info.family_id;
421
422         if (amdgpu_device_deinitialize(device_handle))
423                 return;
424
425         /* Set active status for suites based on their policies */
426         for (i = 0; i < size; ++i)
427                 if (amdgpu_set_suite_active(suites_active_stat[i].pName,
428                                 suites_active_stat[i].pActive()))
429                         fprintf(stderr, "suite deactivation failed - %s\n", CU_get_error_msg());
430
431         /* Explicitly disable specific tests due to known bugs or preferences */
432         /*
433         * BUG: Compute ring stalls and never recovers when the address is
434         * written after the command already submitted
435         */
436         if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
437                         "compute ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
438                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
439
440         if (amdgpu_set_test_active(DEADLOCK_TESTS_STR,
441                                 "sdma ring block test (set amdgpu.lockup_timeout=50)", CU_FALSE))
442                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
443
444         if (amdgpu_set_test_active(BO_TESTS_STR, "Metadata", CU_FALSE))
445                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
446
447         if (amdgpu_set_test_active(BASIC_TESTS_STR, "bo eviction Test", CU_FALSE))
448                 fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
449
450         /* This test was ran on GFX8 and GFX9 only */
451         if (family_id < AMDGPU_FAMILY_VI || family_id > AMDGPU_FAMILY_RV)
452                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Sync dependency Test", CU_FALSE))
453                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
454
455         /* This test was ran on GFX9 only */
456         if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
457                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Dispatch Test", CU_FALSE))
458                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
459
460         /* This test was ran on GFX9 only */
461         if (family_id < AMDGPU_FAMILY_AI || family_id > AMDGPU_FAMILY_RV)
462                 if (amdgpu_set_test_active(BASIC_TESTS_STR, "Draw Test", CU_FALSE))
463                         fprintf(stderr, "test deactivation failed - %s\n", CU_get_error_msg());
464 }
465
466 /* The main() function for setting up and running the tests.
467  * Returns a CUE_SUCCESS on successful running, another
468  * CUnit error code on failure.
469  */
470 int main(int argc, char **argv)
471 {
472         int c;                  /* Character received from getopt */
473         int i = 0;
474         int suite_id = -1;      /* By default run everything */
475         int test_id  = -1;      /* By default run all tests in the suite */
476         int pci_bus_id = -1;    /* By default PC bus ID is not specified */
477         int pci_device_id = 0;  /* By default PC device ID is zero */
478         int display_devices = 0;/* By default not to display devices' info */
479         CU_pSuite pSuite = NULL;
480         CU_pTest  pTest  = NULL;
481         int test_device_index;
482         int display_list = 0;
483         int force_run = 0;
484
485         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
486                 drm_amdgpu[i] = -1;
487
488
489         /* Parse command line string */
490         opterr = 0;             /* Do not print error messages from getopt */
491         while ((c = getopt(argc, argv, options)) != -1) {
492                 switch (c) {
493                 case 'l':
494                         display_list = 1;
495                         break;
496                 case 's':
497                         suite_id = atoi(optarg);
498                         break;
499                 case 't':
500                         test_id = atoi(optarg);
501                         break;
502                 case 'b':
503                         pci_bus_id = atoi(optarg);
504                         break;
505                 case 'd':
506                         sscanf(optarg, "%x", &pci_device_id);
507                         break;
508                 case 'p':
509                         display_devices = 1;
510                         break;
511                 case 'r':
512                         open_render_node = 1;
513                         break;
514                 case 'f':
515                         force_run = 1;
516                         break;
517                 case '?':
518                 case 'h':
519                         fprintf(stderr, usage, argv[0]);
520                         exit(EXIT_SUCCESS);
521                 default:
522                         fprintf(stderr, usage, argv[0]);
523                         exit(EXIT_FAILURE);
524                 }
525         }
526
527         if (amdgpu_open_devices(open_render_node) <= 0) {
528                 perror("Cannot open AMDGPU device");
529                 exit(EXIT_FAILURE);
530         }
531
532         if (drm_amdgpu[0] < 0) {
533                 perror("Cannot open AMDGPU device");
534                 exit(EXIT_FAILURE);
535         }
536
537         if (display_devices) {
538                 amdgpu_print_devices();
539                 amdgpu_close_devices();
540                 exit(EXIT_SUCCESS);
541         }
542
543         if (pci_bus_id > 0 || pci_device_id) {
544                 /* A device was specified to run the test */
545                 test_device_index = amdgpu_find_device(pci_bus_id,
546                                                        pci_device_id);
547
548                 if (test_device_index >= 0) {
549                         /* Most tests run on device of drm_amdgpu[0].
550                          * Swap the chosen device to drm_amdgpu[0].
551                          */
552                         i = drm_amdgpu[0];
553                         drm_amdgpu[0] = drm_amdgpu[test_device_index];
554                         drm_amdgpu[test_device_index] = i;
555                 } else {
556                         fprintf(stderr,
557                                 "The specified GPU device does not exist.\n");
558                         exit(EXIT_FAILURE);
559                 }
560         }
561
562         /* Initialize test suites to run */
563
564         /* initialize the CUnit test registry */
565         if (CUE_SUCCESS != CU_initialize_registry()) {
566                 amdgpu_close_devices();
567                 return CU_get_error();
568         }
569
570         /* Register suites. */
571         if (CU_register_suites(suites) != CUE_SUCCESS) {
572                 fprintf(stderr, "suite registration failed - %s\n",
573                                 CU_get_error_msg());
574                 CU_cleanup_registry();
575                 amdgpu_close_devices();
576                 exit(EXIT_FAILURE);
577         }
578
579         /* Run tests using the CUnit Basic interface */
580         CU_basic_set_mode(CU_BRM_VERBOSE);
581
582         /* Disable suites and individual tests based on misc. conditions */
583         amdgpu_disable_suites();
584
585         if (display_list) {
586                 display_test_suites();
587                 goto end;
588         }
589
590         if (suite_id != -1) {   /* If user specify particular suite? */
591                 pSuite = CU_get_suite_by_index((unsigned int) suite_id,
592                                                 CU_get_registry());
593
594                 if (pSuite) {
595
596                         if (force_run)
597                                 CU_set_suite_active(pSuite, CU_TRUE);
598
599                         if (test_id != -1) {   /* If user specify test id */
600                                 pTest = CU_get_test_by_index(
601                                                 (unsigned int) test_id,
602                                                 pSuite);
603                                 if (pTest) {
604                                         if (force_run)
605                                                 CU_set_test_active(pTest, CU_TRUE);
606
607                                         CU_basic_run_test(pSuite, pTest);
608                                 }
609                                 else {
610                                         fprintf(stderr, "Invalid test id: %d\n",
611                                                                 test_id);
612                                         CU_cleanup_registry();
613                                         amdgpu_close_devices();
614                                         exit(EXIT_FAILURE);
615                                 }
616                         } else
617                                 CU_basic_run_suite(pSuite);
618                 } else {
619                         fprintf(stderr, "Invalid suite id : %d\n",
620                                         suite_id);
621                         CU_cleanup_registry();
622                         amdgpu_close_devices();
623                         exit(EXIT_FAILURE);
624                 }
625         } else
626                 CU_basic_run_tests();
627
628 end:
629         CU_cleanup_registry();
630         amdgpu_close_devices();
631         return CU_get_error();
632 }