OSDN Git Service

99255038baf118abfecf7ac95781b40cd51f327b
[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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <signal.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/time.h>
42 #include <stdarg.h>
43 #include <stdint.h>
44
45 #include "drm.h"
46 #include "xf86drmMode.h"
47 #include "xf86drm.h"
48
49 #include "CUnit/Basic.h"
50
51 #include "amdgpu_test.h"
52
53 /**
54  *  Open handles for amdgpu devices
55  *
56  */
57 int drm_amdgpu[MAX_CARDS_SUPPORTED];
58
59 /** Open render node to test */
60 int open_render_node = 0;       /* By default run most tests on primary node */
61
62 /** The table of all known test suites to run */
63 static CU_SuiteInfo suites[] = {
64         {
65                 .pName = "Basic Tests",
66                 .pInitFunc = suite_basic_tests_init,
67                 .pCleanupFunc = suite_basic_tests_clean,
68                 .pTests = basic_tests,
69         },
70         {
71                 .pName = "BO Tests",
72                 .pInitFunc = suite_bo_tests_init,
73                 .pCleanupFunc = suite_bo_tests_clean,
74                 .pTests = bo_tests,
75         },
76         {
77                 .pName = "CS Tests",
78                 .pInitFunc = suite_cs_tests_init,
79                 .pCleanupFunc = suite_cs_tests_clean,
80                 .pTests = cs_tests,
81         },
82         {
83                 .pName = "VCE Tests",
84                 .pInitFunc = suite_vce_tests_init,
85                 .pCleanupFunc = suite_vce_tests_clean,
86                 .pTests = vce_tests,
87         },
88         {
89                 .pName = "VCN Tests",
90                 .pInitFunc = suite_vcn_tests_init,
91                 .pCleanupFunc = suite_vcn_tests_clean,
92                 .pTests = vcn_tests,
93         },
94         {
95                 .pName = "UVD ENC Tests",
96                 .pInitFunc = suite_uvd_enc_tests_init,
97                 .pCleanupFunc = suite_uvd_enc_tests_clean,
98                 .pTests = uvd_enc_tests,
99         },
100         {
101                 .pName = "Deadlock Tests",
102                 .pInitFunc = suite_deadlock_tests_init,
103                 .pCleanupFunc = suite_deadlock_tests_clean,
104                 .pTests = deadlock_tests,
105         },
106         CU_SUITE_INFO_NULL,
107 };
108
109
110 /** Display information about all  suites and their tests */
111 static void display_test_suites(void)
112 {
113         int iSuite;
114         int iTest;
115
116         printf("Suites\n");
117
118         for (iSuite = 0; suites[iSuite].pName != NULL; iSuite++) {
119                 printf("Suite id = %d: Name '%s'\n",
120                                 iSuite + 1, suites[iSuite].pName);
121
122                 for (iTest = 0; suites[iSuite].pTests[iTest].pName != NULL;
123                         iTest++) {
124                         printf("        Test id %d: Name: '%s'\n", iTest + 1,
125                                         suites[iSuite].pTests[iTest].pName);
126                 }
127         }
128 }
129
130
131 /** Help string for command line parameters */
132 static const char usage[] =
133         "Usage: %s [-hlpr] [<-s <suite id>> [-t <test id>]] "
134         "[-b <pci_bus_id> [-d <pci_device_id>]]\n"
135         "where:\n"
136         "       l - Display all suites and their tests\n"
137         "       r - Run the tests on render node\n"
138         "       b - Specify device's PCI bus id to run tests\n"
139         "       d - Specify device's PCI device id to run tests (optional)\n"
140         "       p - Display information of AMDGPU devices in system\n"
141         "       h - Display this help\n";
142 /** Specified options strings for getopt */
143 static const char options[]   = "hlrps:t:b:d:";
144
145 /* Open AMD devices.
146  * Return the number of AMD device openned.
147  */
148 static int amdgpu_open_devices(int open_render_node)
149 {
150         drmDevicePtr devices[MAX_CARDS_SUPPORTED];
151         int ret;
152         int i;
153         int drm_node;
154         int amd_index = 0;
155         int drm_count;
156         int fd;
157         drmVersionPtr version;
158
159         drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED);
160
161         if (drm_count < 0) {
162                 fprintf(stderr,
163                         "drmGetDevices2() returned an error %d\n",
164                         drm_count);
165                 return 0;
166         }
167
168         for (i = 0; i < drm_count; i++) {
169                 /* If this is not PCI device, skip*/
170                 if (devices[i]->bustype != DRM_BUS_PCI)
171                         continue;
172
173                 /* If this is not AMD GPU vender ID, skip*/
174                 if (devices[i]->deviceinfo.pci->vendor_id != 0x1002)
175                         continue;
176
177                 if (open_render_node)
178                         drm_node = DRM_NODE_RENDER;
179                 else
180                         drm_node = DRM_NODE_PRIMARY;
181
182                 fd = -1;
183                 if (devices[i]->available_nodes & 1 << drm_node)
184                         fd = open(
185                                 devices[i]->nodes[drm_node],
186                                 O_RDWR | O_CLOEXEC);
187
188                 /* This node is not available. */
189                 if (fd < 0) continue;
190
191                 version = drmGetVersion(fd);
192                 if (!version) {
193                         fprintf(stderr,
194                                 "Warning: Cannot get version for %s."
195                                 "Error is %s\n",
196                                 devices[i]->nodes[drm_node],
197                                 strerror(errno));
198                         close(fd);
199                         continue;
200                 }
201
202                 if (strcmp(version->name, "amdgpu")) {
203                         /* This is not AMDGPU driver, skip.*/
204                         drmFreeVersion(version);
205                         close(fd);
206                         continue;
207                 }
208
209                 drmFreeVersion(version);
210
211                 drm_amdgpu[amd_index] = fd;
212                 amd_index++;
213         }
214
215         drmFreeDevices(devices, drm_count);
216         return amd_index;
217 }
218
219 /* Close AMD devices.
220  */
221 static void amdgpu_close_devices()
222 {
223         int i;
224         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
225                 if (drm_amdgpu[i] >=0)
226                         close(drm_amdgpu[i]);
227 }
228
229 /* Print AMD devices information */
230 static void amdgpu_print_devices()
231 {
232         int i;
233         drmDevicePtr device;
234
235         /* Open the first AMD devcie to print driver information. */
236         if (drm_amdgpu[0] >=0) {
237                 /* Display AMD driver version information.*/
238                 drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]);
239
240                 if (retval == NULL) {
241                         perror("Cannot get version for AMDGPU device");
242                         return;
243                 }
244
245                 printf("Driver name: %s, Date: %s, Description: %s.\n",
246                         retval->name, retval->date, retval->desc);
247                 drmFreeVersion(retval);
248         }
249
250         /* Display information of AMD devices */
251         printf("Devices:\n");
252         for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >=0; i++)
253                 if (drmGetDevice2(drm_amdgpu[i],
254                         DRM_DEVICE_GET_PCI_REVISION,
255                         &device) == 0) {
256                         if (device->bustype == DRM_BUS_PCI) {
257                                 printf("PCI ");
258                                 printf(" domain:%04x",
259                                         device->businfo.pci->domain);
260                                 printf(" bus:%02x",
261                                         device->businfo.pci->bus);
262                                 printf(" device:%02x",
263                                         device->businfo.pci->dev);
264                                 printf(" function:%01x",
265                                         device->businfo.pci->func);
266                                 printf(" vendor_id:%04x",
267                                         device->deviceinfo.pci->vendor_id);
268                                 printf(" device_id:%04x",
269                                         device->deviceinfo.pci->device_id);
270                                 printf(" subvendor_id:%04x",
271                                         device->deviceinfo.pci->subvendor_id);
272                                 printf(" subdevice_id:%04x",
273                                         device->deviceinfo.pci->subdevice_id);
274                                 printf(" revision_id:%02x",
275                                         device->deviceinfo.pci->revision_id);
276                                 printf("\n");
277                         }
278                         drmFreeDevice(&device);
279                 }
280 }
281
282 /* Find a match AMD device in PCI bus
283  * Return the index of the device or -1 if not found
284  */
285 static int amdgpu_find_device(uint8_t bus, uint16_t dev)
286 {
287         int i;
288         drmDevicePtr device;
289
290         for (i = 0; i < MAX_CARDS_SUPPORTED && drm_amdgpu[i] >= 0; i++) {
291                 if (drmGetDevice2(drm_amdgpu[i],
292                         DRM_DEVICE_GET_PCI_REVISION,
293                         &device) == 0) {
294                         if (device->bustype == DRM_BUS_PCI)
295                                 if ((bus == 0xFF || device->businfo.pci->bus == bus) &&
296                                         device->deviceinfo.pci->device_id == dev) {
297                                         drmFreeDevice(&device);
298                                         return i;
299                                 }
300
301                         drmFreeDevice(&device);
302                 }
303         }
304
305         return -1;
306 }
307
308 /* The main() function for setting up and running the tests.
309  * Returns a CUE_SUCCESS on successful running, another
310  * CUnit error code on failure.
311  */
312 int main(int argc, char **argv)
313 {
314         int c;                  /* Character received from getopt */
315         int i = 0;
316         int suite_id = -1;      /* By default run everything */
317         int test_id  = -1;      /* By default run all tests in the suite */
318         int pci_bus_id = -1;    /* By default PC bus ID is not specified */
319         int pci_device_id = 0;  /* By default PC device ID is zero */
320         int display_devices = 0;/* By default not to display devices' info */
321         CU_pSuite pSuite = NULL;
322         CU_pTest  pTest  = NULL;
323         int test_device_index;
324
325         for (i = 0; i < MAX_CARDS_SUPPORTED; i++)
326                 drm_amdgpu[i] = -1;
327
328
329         /* Parse command line string */
330         opterr = 0;             /* Do not print error messages from getopt */
331         while ((c = getopt(argc, argv, options)) != -1) {
332                 switch (c) {
333                 case 'l':
334                         display_test_suites();
335                         exit(EXIT_SUCCESS);
336                 case 's':
337                         suite_id = atoi(optarg);
338                         break;
339                 case 't':
340                         test_id = atoi(optarg);
341                         break;
342                 case 'b':
343                         pci_bus_id = atoi(optarg);
344                         break;
345                 case 'd':
346                         sscanf(optarg, "%x", &pci_device_id);
347                         break;
348                 case 'p':
349                         display_devices = 1;
350                         break;
351                 case 'r':
352                         open_render_node = 1;
353                         break;
354                 case '?':
355                 case 'h':
356                         fprintf(stderr, usage, argv[0]);
357                         exit(EXIT_SUCCESS);
358                 default:
359                         fprintf(stderr, usage, argv[0]);
360                         exit(EXIT_FAILURE);
361                 }
362         }
363
364         if (amdgpu_open_devices(open_render_node) <= 0) {
365                 perror("Cannot open AMDGPU device");
366                 exit(EXIT_FAILURE);
367         }
368
369         if (drm_amdgpu[0] < 0) {
370                 perror("Cannot open AMDGPU device");
371                 exit(EXIT_FAILURE);
372         }
373
374         if (display_devices) {
375                 amdgpu_print_devices();
376                 amdgpu_close_devices();
377                 exit(EXIT_SUCCESS);
378         }
379
380         if (pci_bus_id > 0 || pci_device_id) {
381                 /* A device was specified to run the test */
382                 test_device_index = amdgpu_find_device(pci_bus_id,
383                                                        pci_device_id);
384
385                 if (test_device_index >= 0) {
386                         /* Most tests run on device of drm_amdgpu[0].
387                          * Swap the chosen device to drm_amdgpu[0].
388                          */
389                         i = drm_amdgpu[0];
390                         drm_amdgpu[0] = drm_amdgpu[test_device_index];
391                         drm_amdgpu[test_device_index] = i;
392                 } else {
393                         fprintf(stderr,
394                                 "The specified GPU device does not exist.\n");
395                         exit(EXIT_FAILURE);
396                 }
397         }
398
399         /* Initialize test suites to run */
400
401         /* initialize the CUnit test registry */
402         if (CUE_SUCCESS != CU_initialize_registry()) {
403                 amdgpu_close_devices();
404                 return CU_get_error();
405         }
406
407         /* Register suites. */
408         if (CU_register_suites(suites) != CUE_SUCCESS) {
409                 fprintf(stderr, "suite registration failed - %s\n",
410                                 CU_get_error_msg());
411                 CU_cleanup_registry();
412                 amdgpu_close_devices();
413                 exit(EXIT_FAILURE);
414         }
415
416         /* Run tests using the CUnit Basic interface */
417         CU_basic_set_mode(CU_BRM_VERBOSE);
418
419         if (suite_id != -1) {   /* If user specify particular suite? */
420                 pSuite = CU_get_suite_by_index((unsigned int) suite_id,
421                                                 CU_get_registry());
422
423                 if (pSuite) {
424                         if (test_id != -1) {   /* If user specify test id */
425                                 pTest = CU_get_test_by_index(
426                                                 (unsigned int) test_id,
427                                                 pSuite);
428                                 if (pTest)
429                                         CU_basic_run_test(pSuite, pTest);
430                                 else {
431                                         fprintf(stderr, "Invalid test id: %d\n",
432                                                                 test_id);
433                                         CU_cleanup_registry();
434                                         amdgpu_close_devices();
435                                         exit(EXIT_FAILURE);
436                                 }
437                         } else
438                                 CU_basic_run_suite(pSuite);
439                 } else {
440                         fprintf(stderr, "Invalid suite id : %d\n",
441                                         suite_id);
442                         CU_cleanup_registry();
443                         amdgpu_close_devices();
444                         exit(EXIT_FAILURE);
445                 }
446         } else
447                 CU_basic_run_tests();
448
449         CU_cleanup_registry();
450         amdgpu_close_devices();
451         return CU_get_error();
452 }