1 /*
2  * Copyright © 2016 Intel Corporation
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "igt.h"
25 #include "igt_vgem.h"
26 #include "igt_debugfs.h"
27 #include "igt_sysfs.h"
28 
29 #include <sys/mman.h>
30 #include <sys/poll.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
33 
34 IGT_TEST_DESCRIPTION("Extended sanity check of Virtual GEM module (vGEM).");
35 
has_prime_export(int fd)36 static bool has_prime_export(int fd)
37 {
38 	uint64_t value;
39 
40 	if (drmGetCap(fd, DRM_CAP_PRIME, &value))
41 		return false;
42 
43 	return value & DRM_PRIME_CAP_EXPORT;
44 }
45 
46 
test_nohang(int fd)47 static void test_nohang(int fd)
48 {
49 	struct vgem_bo bo;
50 	uint32_t fence;
51 	struct pollfd pfd;
52 
53 	/* A vGEM fence must expire automatically to prevent driver hangs */
54 
55 	igt_require(has_prime_export(fd));
56 	igt_require(vgem_has_fences(fd));
57 
58 	bo.width = 1;
59 	bo.height = 1;
60 	bo.bpp = 32;
61 	vgem_create(fd, &bo);
62 
63 	pfd.fd = prime_handle_to_fd(fd, bo.handle);
64 	pfd.events = POLLOUT;
65 
66 	fence = vgem_fence_attach(fd, &bo, 0);
67 
68 	igt_assert(poll(&pfd, 1, 0) == 0);
69 	igt_assert(poll(&pfd, 1, 60*1000) == 1);
70 
71 	igt_assert_eq(__vgem_fence_signal(fd, fence), -110);
72 	close(pfd.fd);
73 	gem_close(fd, bo.handle);
74 }
75 
76 igt_main
77 {
78 	int fd = -1;
79 
80 	igt_fixture {
81 		fd = drm_open_driver(DRIVER_VGEM);
82 	}
83 
84 	igt_subtest_f("nohang")
85 		test_nohang(fd);
86 
87 	igt_fixture {
88 		close(fd);
89 	}
90 }
91