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