anv/gem: Add a flags parameter to syncobj_create
[mesa.git] / src / intel / vulkan / anv_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 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/mman.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #include "anv_private.h"
33
34 static int
35 anv_ioctl(int fd, unsigned long request, void *arg)
36 {
37 int ret;
38
39 do {
40 ret = ioctl(fd, request, arg);
41 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
42
43 return ret;
44 }
45
46 /**
47 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
48 *
49 * Return gem handle, or 0 on failure. Gem handles are never 0.
50 */
51 uint32_t
52 anv_gem_create(struct anv_device *device, uint64_t size)
53 {
54 struct drm_i915_gem_create gem_create = {
55 .size = size,
56 };
57
58 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
59 if (ret != 0) {
60 /* FIXME: What do we do if this fails? */
61 return 0;
62 }
63
64 return gem_create.handle;
65 }
66
67 void
68 anv_gem_close(struct anv_device *device, uint32_t gem_handle)
69 {
70 struct drm_gem_close close = {
71 .handle = gem_handle,
72 };
73
74 anv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
75 }
76
77 /**
78 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
79 */
80 void*
81 anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
82 uint64_t offset, uint64_t size, uint32_t flags)
83 {
84 struct drm_i915_gem_mmap gem_mmap = {
85 .handle = gem_handle,
86 .offset = offset,
87 .size = size,
88 .flags = flags,
89 };
90
91 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
92 if (ret != 0)
93 return MAP_FAILED;
94
95 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap.addr_ptr, gem_mmap.size, 0, 1));
96 return (void *)(uintptr_t) gem_mmap.addr_ptr;
97 }
98
99 /* This is just a wrapper around munmap, but it also notifies valgrind that
100 * this map is no longer valid. Pair this with anv_gem_mmap().
101 */
102 void
103 anv_gem_munmap(void *p, uint64_t size)
104 {
105 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
106 munmap(p, size);
107 }
108
109 uint32_t
110 anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
111 {
112 struct drm_i915_gem_userptr userptr = {
113 .user_ptr = (__u64)((unsigned long) mem),
114 .user_size = size,
115 .flags = 0,
116 };
117
118 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
119 if (ret == -1)
120 return 0;
121
122 return userptr.handle;
123 }
124
125 int
126 anv_gem_set_caching(struct anv_device *device,
127 uint32_t gem_handle, uint32_t caching)
128 {
129 struct drm_i915_gem_caching gem_caching = {
130 .handle = gem_handle,
131 .caching = caching,
132 };
133
134 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
135 }
136
137 int
138 anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
139 uint32_t read_domains, uint32_t write_domain)
140 {
141 struct drm_i915_gem_set_domain gem_set_domain = {
142 .handle = gem_handle,
143 .read_domains = read_domains,
144 .write_domain = write_domain,
145 };
146
147 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &gem_set_domain);
148 }
149
150 /**
151 * Returns 0, 1, or negative to indicate error
152 */
153 int
154 anv_gem_busy(struct anv_device *device, uint32_t gem_handle)
155 {
156 struct drm_i915_gem_busy busy = {
157 .handle = gem_handle,
158 };
159
160 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
161 if (ret < 0)
162 return ret;
163
164 return busy.busy != 0;
165 }
166
167 /**
168 * On error, \a timeout_ns holds the remaining time.
169 */
170 int
171 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
172 {
173 struct drm_i915_gem_wait wait = {
174 .bo_handle = gem_handle,
175 .timeout_ns = *timeout_ns,
176 .flags = 0,
177 };
178
179 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
180 *timeout_ns = wait.timeout_ns;
181
182 return ret;
183 }
184
185 int
186 anv_gem_execbuffer(struct anv_device *device,
187 struct drm_i915_gem_execbuffer2 *execbuf)
188 {
189 if (execbuf->flags & I915_EXEC_FENCE_OUT)
190 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR, execbuf);
191 else
192 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
193 }
194
195 int
196 anv_gem_set_tiling(struct anv_device *device,
197 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
198 {
199 int ret;
200
201 /* set_tiling overwrites the input on the error path, so we have to open
202 * code anv_ioctl.
203 */
204 do {
205 struct drm_i915_gem_set_tiling set_tiling = {
206 .handle = gem_handle,
207 .tiling_mode = tiling,
208 .stride = stride,
209 };
210
211 ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
212 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
213
214 return ret;
215 }
216
217 int
218 anv_gem_get_param(int fd, uint32_t param)
219 {
220 int tmp;
221
222 drm_i915_getparam_t gp = {
223 .param = param,
224 .value = &tmp,
225 };
226
227 int ret = anv_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
228 if (ret == 0)
229 return tmp;
230
231 return 0;
232 }
233
234 bool
235 anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
236 {
237 struct drm_gem_close close;
238 int ret;
239
240 struct drm_i915_gem_create gem_create = {
241 .size = 4096,
242 };
243
244 if (anv_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create)) {
245 assert(!"Failed to create GEM BO");
246 return false;
247 }
248
249 bool swizzled = false;
250
251 /* set_tiling overwrites the input on the error path, so we have to open
252 * code anv_ioctl.
253 */
254 do {
255 struct drm_i915_gem_set_tiling set_tiling = {
256 .handle = gem_create.handle,
257 .tiling_mode = tiling,
258 .stride = tiling == I915_TILING_X ? 512 : 128,
259 };
260
261 ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
262 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
263
264 if (ret != 0) {
265 assert(!"Failed to set BO tiling");
266 goto close_and_return;
267 }
268
269 struct drm_i915_gem_get_tiling get_tiling = {
270 .handle = gem_create.handle,
271 };
272
273 if (anv_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
274 assert(!"Failed to get BO tiling");
275 goto close_and_return;
276 }
277
278 swizzled = get_tiling.swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
279
280 close_and_return:
281
282 memset(&close, 0, sizeof(close));
283 close.handle = gem_create.handle;
284 anv_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
285
286 return swizzled;
287 }
288
289 int
290 anv_gem_create_context(struct anv_device *device)
291 {
292 struct drm_i915_gem_context_create create = { 0 };
293
294 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
295 if (ret == -1)
296 return -1;
297
298 return create.ctx_id;
299 }
300
301 int
302 anv_gem_destroy_context(struct anv_device *device, int context)
303 {
304 struct drm_i915_gem_context_destroy destroy = {
305 .ctx_id = context,
306 };
307
308 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
309 }
310
311 int
312 anv_gem_get_context_param(int fd, int context, uint32_t param, uint64_t *value)
313 {
314 struct drm_i915_gem_context_param gp = {
315 .ctx_id = context,
316 .param = param,
317 };
318
319 int ret = anv_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &gp);
320 if (ret == -1)
321 return -1;
322
323 *value = gp.value;
324 return 0;
325 }
326
327 int
328 anv_gem_get_aperture(int fd, uint64_t *size)
329 {
330 struct drm_i915_gem_get_aperture aperture = { 0 };
331
332 int ret = anv_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
333 if (ret == -1)
334 return -1;
335
336 *size = aperture.aper_available_size;
337
338 return 0;
339 }
340
341 bool
342 anv_gem_supports_48b_addresses(int fd)
343 {
344 struct drm_i915_gem_exec_object2 obj = {
345 .flags = EXEC_OBJECT_SUPPORTS_48B_ADDRESS,
346 };
347
348 struct drm_i915_gem_execbuffer2 execbuf = {
349 .buffers_ptr = (uintptr_t)&obj,
350 .buffer_count = 1,
351 .rsvd1 = 0xffffffu,
352 };
353
354 int ret = anv_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
355
356 return ret == -1 && errno == ENOENT;
357 }
358
359 int
360 anv_gem_gpu_get_reset_stats(struct anv_device *device,
361 uint32_t *active, uint32_t *pending)
362 {
363 struct drm_i915_reset_stats stats = {
364 .ctx_id = device->context_id,
365 };
366
367 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
368 if (ret == 0) {
369 *active = stats.batch_active;
370 *pending = stats.batch_pending;
371 }
372
373 return ret;
374 }
375
376 int
377 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
378 {
379 struct drm_prime_handle args = {
380 .handle = gem_handle,
381 .flags = DRM_CLOEXEC,
382 };
383
384 int ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
385 if (ret == -1)
386 return -1;
387
388 return args.fd;
389 }
390
391 uint32_t
392 anv_gem_fd_to_handle(struct anv_device *device, int fd)
393 {
394 struct drm_prime_handle args = {
395 .fd = fd,
396 };
397
398 int ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
399 if (ret == -1)
400 return 0;
401
402 return args.handle;
403 }
404
405 #ifndef SYNC_IOC_MAGIC
406 /* duplicated from linux/sync_file.h to avoid build-time dependency
407 * on new (v4.7) kernel headers. Once distro's are mostly using
408 * something newer than v4.7 drop this and #include <linux/sync_file.h>
409 * instead.
410 */
411 struct sync_merge_data {
412 char name[32];
413 __s32 fd2;
414 __s32 fence;
415 __u32 flags;
416 __u32 pad;
417 };
418
419 #define SYNC_IOC_MAGIC '>'
420 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
421 #endif
422
423 int
424 anv_gem_sync_file_merge(struct anv_device *device, int fd1, int fd2)
425 {
426 const char name[] = "anv merge fence";
427 struct sync_merge_data args = {
428 .fd2 = fd2,
429 .fence = -1,
430 };
431 memcpy(args.name, name, sizeof(name));
432
433 int ret = anv_ioctl(fd1, SYNC_IOC_MERGE, &args);
434 if (ret == -1)
435 return -1;
436
437 return args.fence;
438 }
439
440 uint32_t
441 anv_gem_syncobj_create(struct anv_device *device, uint32_t flags)
442 {
443 struct drm_syncobj_create args = {
444 .flags = flags,
445 };
446
447 int ret = anv_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
448 if (ret)
449 return 0;
450
451 return args.handle;
452 }
453
454 void
455 anv_gem_syncobj_destroy(struct anv_device *device, uint32_t handle)
456 {
457 struct drm_syncobj_destroy args = {
458 .handle = handle,
459 };
460
461 anv_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
462 }
463
464 int
465 anv_gem_syncobj_handle_to_fd(struct anv_device *device, uint32_t handle)
466 {
467 struct drm_syncobj_handle args = {
468 .handle = handle,
469 };
470
471 int ret = anv_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
472 if (ret)
473 return -1;
474
475 return args.fd;
476 }
477
478 uint32_t
479 anv_gem_syncobj_fd_to_handle(struct anv_device *device, int fd)
480 {
481 struct drm_syncobj_handle args = {
482 .fd = fd,
483 };
484
485 int ret = anv_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
486 if (ret)
487 return 0;
488
489 return args.handle;
490 }