1 /*
2 * Copyright © 2008 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "igt.h"
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include "drm.h"
38
39 IGT_TEST_DESCRIPTION("Tests for flink - a way to export a gem object by name");
40
41 static void
test_flink(int fd)42 test_flink(int fd)
43 {
44 struct drm_i915_gem_create create;
45 struct drm_gem_flink flink;
46 struct drm_gem_open open_struct;
47 int ret;
48
49 igt_debug("Testing flink and open.\n");
50
51 memset(&create, 0, sizeof(create));
52 create.size = 16 * 1024;
53 ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
54 igt_assert_eq(ret, 0);
55
56 flink.handle = create.handle;
57 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
58 igt_assert_eq(ret, 0);
59
60 open_struct.name = flink.name;
61 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
62 igt_assert_eq(ret, 0);
63 igt_assert(open_struct.handle != 0);
64 }
65
66 static void
test_double_flink(int fd)67 test_double_flink(int fd)
68 {
69 struct drm_i915_gem_create create;
70 struct drm_gem_flink flink;
71 struct drm_gem_flink flink2;
72 int ret;
73
74 igt_debug("Testing repeated flink.\n");
75
76 memset(&create, 0, sizeof(create));
77 create.size = 16 * 1024;
78 ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
79 igt_assert_eq(ret, 0);
80
81 flink.handle = create.handle;
82 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
83 igt_assert_eq(ret, 0);
84
85 flink2.handle = create.handle;
86 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink2);
87 igt_assert_eq(ret, 0);
88
89 /* flinks for same gem object share the same name */
90 igt_assert(flink2.name == flink.name);
91 }
92
93 static void
test_bad_flink(int fd)94 test_bad_flink(int fd)
95 {
96 struct drm_gem_flink flink;
97 int ret;
98
99 igt_debug("Testing error return on bad flink ioctl.\n");
100
101 flink.handle = 0x10101010;
102 ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
103 igt_assert(ret == -1 && errno == ENOENT);
104 }
105
106 static void
test_bad_open(int fd)107 test_bad_open(int fd)
108 {
109 struct drm_gem_open open_struct;
110 int ret;
111
112 igt_debug("Testing error return on bad open ioctl.\n");
113
114 open_struct.name = 0x10101010;
115 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
116
117 igt_assert(ret == -1 && errno == ENOENT);
118 }
119
120 static void
test_flink_lifetime(int fd)121 test_flink_lifetime(int fd)
122 {
123 struct drm_i915_gem_create create;
124 struct drm_gem_flink flink;
125 struct drm_gem_open open_struct;
126 int ret, fd2;
127
128 igt_debug("Testing flink lifetime.\n");
129
130 fd2 = drm_open_driver(DRIVER_INTEL);
131
132 memset(&create, 0, sizeof(create));
133 create.size = 16 * 1024;
134 ret = ioctl(fd2, DRM_IOCTL_I915_GEM_CREATE, &create);
135 igt_assert_eq(ret, 0);
136
137 flink.handle = create.handle;
138 ret = ioctl(fd2, DRM_IOCTL_GEM_FLINK, &flink);
139 igt_assert_eq(ret, 0);
140
141 /* Open a second reference to the gem object with different fd */
142 open_struct.name = flink.name;
143 ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open_struct);
144 igt_assert_eq(ret, 0);
145 igt_assert(open_struct.handle != 0);
146
147 close(fd2);
148 fd2 = drm_open_driver(DRIVER_INTEL);
149
150 /* Flink name remains valid due to the second reference */
151 open_struct.name = flink.name;
152 ret = ioctl(fd2, DRM_IOCTL_GEM_OPEN, &open_struct);
153 igt_assert_eq(ret, 0);
154 igt_assert(open_struct.handle != 0);
155 }
156
157 int fd;
158
159 igt_main
160 {
161 igt_fixture
162 fd = drm_open_driver(DRIVER_INTEL);
163
164 igt_subtest("basic")
165 test_flink(fd);
166 igt_subtest("double-flink")
167 test_double_flink(fd);
168 igt_subtest("bad-flink")
169 test_bad_flink(fd);
170 igt_subtest("bad-open")
171 test_bad_open(fd);
172
173 /* Flink lifetime is limited to that of the gem object it points to */
174 igt_subtest("flink-lifetime")
175 test_flink_lifetime(fd);
176 }
177