vk: Indent tables to align '=' at column 48
[mesa.git] / src / vulkan / gem.c
1 /*
2 * Copyright © 2015 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 #define _DEFAULT_SOURCE
25
26 #include <sys/ioctl.h>
27 #include <sys/mman.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32
33 #include "private.h"
34
35 #ifdef HAVE_VALGRIND
36 #include <valgrind.h>
37 #include <memcheck.h>
38 #define VG(x) x
39 #else
40 #define VG(x)
41 #endif
42
43 #define VG_CLEAR(s) VG(memset(&s, 0, sizeof(s)))
44
45 static int
46 anv_ioctl(int fd, unsigned long request, void *arg)
47 {
48 int ret;
49
50 do {
51 ret = ioctl(fd, request, arg);
52 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
53
54 return ret;
55 }
56
57 /**
58 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
59 *
60 * Return gem handle, or 0 on failure. Gem handles are never 0.
61 */
62 uint32_t
63 anv_gem_create(struct anv_device *device, size_t size)
64 {
65 struct drm_i915_gem_create gem_create;
66 int ret;
67
68 VG_CLEAR(gem_create);
69 gem_create.size = size;
70
71 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
72 if (ret != 0) {
73 /* FIXME: What do we do if this fails? */
74 return 0;
75 }
76
77 return gem_create.handle;
78 }
79
80 void
81 anv_gem_close(struct anv_device *device, int gem_handle)
82 {
83 struct drm_gem_close close;
84
85 VG_CLEAR(close);
86 close.handle = gem_handle;
87 anv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
88 }
89
90 /**
91 * Wrapper around DRM_IOCTL_I915_GEM_MMAP.
92 */
93 void*
94 anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
95 uint64_t offset, uint64_t size)
96 {
97 struct drm_i915_gem_mmap gem_mmap;
98 int ret;
99
100 gem_mmap.handle = gem_handle;
101 VG_CLEAR(gem_mmap.pad);
102 gem_mmap.offset = offset;
103 gem_mmap.size = size;
104 VG_CLEAR(gem_mmap.addr_ptr);
105
106 #ifdef I915_MMAP_WC
107 gem_mmap.flags = 0;
108 #endif
109
110 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
111 if (ret != 0) {
112 /* FIXME: Is NULL the right error return? Cf MAP_INVALID */
113 return NULL;
114 }
115
116 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap.addr_ptr, gem_mmap.size, 0, 1));
117 return (void *)(uintptr_t) gem_mmap.addr_ptr;
118 }
119
120 /* This is just a wrapper around munmap, but it also notifies valgrind that
121 * this map is no longer valid. Pair this with anv_gem_mmap().
122 */
123 void
124 anv_gem_munmap(void *p, uint64_t size)
125 {
126 munmap(p, size);
127 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
128 }
129
130 int
131 anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
132 {
133 struct drm_i915_gem_userptr userptr;
134 int ret;
135
136 VG_CLEAR(userptr);
137 userptr.user_ptr = (__u64)((unsigned long) mem);
138 userptr.user_size = size;
139 userptr.flags = 0;
140
141 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
142 if (ret == -1)
143 return 0;
144
145 return userptr.handle;
146 }
147
148 /**
149 * On error, \a timeout_ns holds the remaining time.
150 */
151 int
152 anv_gem_wait(struct anv_device *device, int gem_handle, int64_t *timeout_ns)
153 {
154 struct drm_i915_gem_wait wait;
155 int ret;
156
157 VG_CLEAR(wait);
158 wait.bo_handle = gem_handle;
159 wait.timeout_ns = *timeout_ns;
160 wait.flags = 0;
161
162 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
163 *timeout_ns = wait.timeout_ns;
164
165 return ret;
166 }
167
168 int
169 anv_gem_execbuffer(struct anv_device *device,
170 struct drm_i915_gem_execbuffer2 *execbuf)
171 {
172 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
173 }
174
175 int
176 anv_gem_set_tiling(struct anv_device *device,
177 int gem_handle, uint32_t stride, uint32_t tiling)
178 {
179 struct drm_i915_gem_set_tiling set_tiling;
180 int ret;
181
182 /* set_tiling overwrites the input on the error path, so we have to open
183 * code anv_ioctl.
184 */
185
186 do {
187 VG_CLEAR(set_tiling);
188 set_tiling.handle = gem_handle;
189 set_tiling.tiling_mode = I915_TILING_X;
190 set_tiling.stride = stride;
191
192 ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
193 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
194
195 return ret;
196 }
197
198 int
199 anv_gem_get_param(int fd, uint32_t param)
200 {
201 drm_i915_getparam_t gp;
202 int ret, tmp;
203
204 VG_CLEAR(gp);
205 gp.param = param;
206 gp.value = &tmp;
207 ret = anv_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
208 if (ret == 0)
209 return tmp;
210
211 return 0;
212 }
213
214 int
215 anv_gem_create_context(struct anv_device *device)
216 {
217 struct drm_i915_gem_context_create create;
218 int ret;
219
220 VG_CLEAR(create);
221
222 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
223 if (ret == -1)
224 return -1;
225
226 return create.ctx_id;
227 }
228
229 int
230 anv_gem_destroy_context(struct anv_device *device, int context)
231 {
232 struct drm_i915_gem_context_destroy destroy;
233
234 VG_CLEAR(destroy);
235 destroy.ctx_id = context;
236
237 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
238 }
239
240 int
241 anv_gem_get_aperture(struct anv_device *device, uint64_t *size)
242 {
243 struct drm_i915_gem_get_aperture aperture;
244 int ret;
245
246 VG_CLEAR(aperture);
247 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
248 if (ret == -1)
249 return -1;
250
251 *size = aperture.aper_available_size;
252
253 return 0;
254 }
255
256 int
257 anv_gem_handle_to_fd(struct anv_device *device, int gem_handle)
258 {
259 struct drm_prime_handle args;
260 int ret;
261
262 VG_CLEAR(args);
263 args.handle = gem_handle;
264 args.flags = DRM_CLOEXEC;
265
266 ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
267 if (ret == -1)
268 return -1;
269
270 return args.fd;
271 }
272
273 int
274 anv_gem_fd_to_handle(struct anv_device *device, int fd)
275 {
276 struct drm_prime_handle args;
277 int ret;
278
279 VG_CLEAR(args);
280 args.fd = fd;
281
282 ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
283 if (ret == -1)
284 return 0;
285
286 return args.handle;
287 }