anv: Advertise VK_KHR_shader_subgroup_extended_types
[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 #include "common/gen_defines.h"
34 #include "common/gen_gem.h"
35
36 /**
37 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
38 *
39 * Return gem handle, or 0 on failure. Gem handles are never 0.
40 */
41 uint32_t
42 anv_gem_create(struct anv_device *device, uint64_t size)
43 {
44 struct drm_i915_gem_create gem_create = {
45 .size = size,
46 };
47
48 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
49 if (ret != 0) {
50 /* FIXME: What do we do if this fails? */
51 return 0;
52 }
53
54 return gem_create.handle;
55 }
56
57 void
58 anv_gem_close(struct anv_device *device, uint32_t gem_handle)
59 {
60 struct drm_gem_close close = {
61 .handle = gem_handle,
62 };
63
64 gen_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
65 }
66
67 /**
68 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
69 */
70 void*
71 anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
72 uint64_t offset, uint64_t size, uint32_t flags)
73 {
74 struct drm_i915_gem_mmap gem_mmap = {
75 .handle = gem_handle,
76 .offset = offset,
77 .size = size,
78 .flags = flags,
79 };
80
81 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
82 if (ret != 0)
83 return MAP_FAILED;
84
85 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap.addr_ptr, gem_mmap.size, 0, 1));
86 return (void *)(uintptr_t) gem_mmap.addr_ptr;
87 }
88
89 /* This is just a wrapper around munmap, but it also notifies valgrind that
90 * this map is no longer valid. Pair this with anv_gem_mmap().
91 */
92 void
93 anv_gem_munmap(void *p, uint64_t size)
94 {
95 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
96 munmap(p, size);
97 }
98
99 uint32_t
100 anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
101 {
102 struct drm_i915_gem_userptr userptr = {
103 .user_ptr = (__u64)((unsigned long) mem),
104 .user_size = size,
105 .flags = 0,
106 };
107
108 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
109 if (ret == -1)
110 return 0;
111
112 return userptr.handle;
113 }
114
115 int
116 anv_gem_set_caching(struct anv_device *device,
117 uint32_t gem_handle, uint32_t caching)
118 {
119 struct drm_i915_gem_caching gem_caching = {
120 .handle = gem_handle,
121 .caching = caching,
122 };
123
124 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
125 }
126
127 int
128 anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
129 uint32_t read_domains, uint32_t write_domain)
130 {
131 struct drm_i915_gem_set_domain gem_set_domain = {
132 .handle = gem_handle,
133 .read_domains = read_domains,
134 .write_domain = write_domain,
135 };
136
137 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &gem_set_domain);
138 }
139
140 /**
141 * Returns 0, 1, or negative to indicate error
142 */
143 int
144 anv_gem_busy(struct anv_device *device, uint32_t gem_handle)
145 {
146 struct drm_i915_gem_busy busy = {
147 .handle = gem_handle,
148 };
149
150 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
151 if (ret < 0)
152 return ret;
153
154 return busy.busy != 0;
155 }
156
157 /**
158 * On error, \a timeout_ns holds the remaining time.
159 */
160 int
161 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
162 {
163 struct drm_i915_gem_wait wait = {
164 .bo_handle = gem_handle,
165 .timeout_ns = *timeout_ns,
166 .flags = 0,
167 };
168
169 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
170 *timeout_ns = wait.timeout_ns;
171
172 return ret;
173 }
174
175 int
176 anv_gem_execbuffer(struct anv_device *device,
177 struct drm_i915_gem_execbuffer2 *execbuf)
178 {
179 if (execbuf->flags & I915_EXEC_FENCE_OUT)
180 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR, execbuf);
181 else
182 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
183 }
184
185 /** Return -1 on error. */
186 int
187 anv_gem_get_tiling(struct anv_device *device, uint32_t gem_handle)
188 {
189 struct drm_i915_gem_get_tiling get_tiling = {
190 .handle = gem_handle,
191 };
192
193 if (gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
194 assert(!"Failed to get BO tiling");
195 return -1;
196 }
197
198 return get_tiling.tiling_mode;
199 }
200
201 int
202 anv_gem_set_tiling(struct anv_device *device,
203 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
204 {
205 int ret;
206
207 /* set_tiling overwrites the input on the error path, so we have to open
208 * code gen_ioctl.
209 */
210 do {
211 struct drm_i915_gem_set_tiling set_tiling = {
212 .handle = gem_handle,
213 .tiling_mode = tiling,
214 .stride = stride,
215 };
216
217 ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
218 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
219
220 return ret;
221 }
222
223 int
224 anv_gem_get_param(int fd, uint32_t param)
225 {
226 int tmp;
227
228 drm_i915_getparam_t gp = {
229 .param = param,
230 .value = &tmp,
231 };
232
233 int ret = gen_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
234 if (ret == 0)
235 return tmp;
236
237 return 0;
238 }
239
240 bool
241 anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
242 {
243 struct drm_gem_close close;
244 int ret;
245
246 struct drm_i915_gem_create gem_create = {
247 .size = 4096,
248 };
249
250 if (gen_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create)) {
251 assert(!"Failed to create GEM BO");
252 return false;
253 }
254
255 bool swizzled = false;
256
257 /* set_tiling overwrites the input on the error path, so we have to open
258 * code gen_ioctl.
259 */
260 do {
261 struct drm_i915_gem_set_tiling set_tiling = {
262 .handle = gem_create.handle,
263 .tiling_mode = tiling,
264 .stride = tiling == I915_TILING_X ? 512 : 128,
265 };
266
267 ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
268 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
269
270 if (ret != 0) {
271 assert(!"Failed to set BO tiling");
272 goto close_and_return;
273 }
274
275 struct drm_i915_gem_get_tiling get_tiling = {
276 .handle = gem_create.handle,
277 };
278
279 if (gen_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
280 assert(!"Failed to get BO tiling");
281 goto close_and_return;
282 }
283
284 swizzled = get_tiling.swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
285
286 close_and_return:
287
288 memset(&close, 0, sizeof(close));
289 close.handle = gem_create.handle;
290 gen_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
291
292 return swizzled;
293 }
294
295 bool
296 anv_gem_has_context_priority(int fd)
297 {
298 return !anv_gem_set_context_param(fd, 0, I915_CONTEXT_PARAM_PRIORITY,
299 GEN_CONTEXT_MEDIUM_PRIORITY);
300 }
301
302 int
303 anv_gem_create_context(struct anv_device *device)
304 {
305 struct drm_i915_gem_context_create create = { 0 };
306
307 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
308 if (ret == -1)
309 return -1;
310
311 return create.ctx_id;
312 }
313
314 int
315 anv_gem_destroy_context(struct anv_device *device, int context)
316 {
317 struct drm_i915_gem_context_destroy destroy = {
318 .ctx_id = context,
319 };
320
321 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
322 }
323
324 int
325 anv_gem_set_context_param(int fd, int context, uint32_t param, uint64_t value)
326 {
327 struct drm_i915_gem_context_param p = {
328 .ctx_id = context,
329 .param = param,
330 .value = value,
331 };
332 int err = 0;
333
334 if (gen_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &p))
335 err = -errno;
336 return err;
337 }
338
339 int
340 anv_gem_get_context_param(int fd, int context, uint32_t param, uint64_t *value)
341 {
342 struct drm_i915_gem_context_param gp = {
343 .ctx_id = context,
344 .param = param,
345 };
346
347 int ret = gen_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &gp);
348 if (ret == -1)
349 return -1;
350
351 *value = gp.value;
352 return 0;
353 }
354
355 int
356 anv_gem_get_aperture(int fd, uint64_t *size)
357 {
358 struct drm_i915_gem_get_aperture aperture = { 0 };
359
360 int ret = gen_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
361 if (ret == -1)
362 return -1;
363
364 *size = aperture.aper_available_size;
365
366 return 0;
367 }
368
369 int
370 anv_gem_gpu_get_reset_stats(struct anv_device *device,
371 uint32_t *active, uint32_t *pending)
372 {
373 struct drm_i915_reset_stats stats = {
374 .ctx_id = device->context_id,
375 };
376
377 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
378 if (ret == 0) {
379 *active = stats.batch_active;
380 *pending = stats.batch_pending;
381 }
382
383 return ret;
384 }
385
386 int
387 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
388 {
389 struct drm_prime_handle args = {
390 .handle = gem_handle,
391 .flags = DRM_CLOEXEC,
392 };
393
394 int ret = gen_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
395 if (ret == -1)
396 return -1;
397
398 return args.fd;
399 }
400
401 uint32_t
402 anv_gem_fd_to_handle(struct anv_device *device, int fd)
403 {
404 struct drm_prime_handle args = {
405 .fd = fd,
406 };
407
408 int ret = gen_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
409 if (ret == -1)
410 return 0;
411
412 return args.handle;
413 }
414
415 int
416 anv_gem_reg_read(struct anv_device *device, uint32_t offset, uint64_t *result)
417 {
418 struct drm_i915_reg_read args = {
419 .offset = offset
420 };
421
422 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_REG_READ, &args);
423
424 *result = args.val;
425 return ret;
426 }
427
428 #ifndef SYNC_IOC_MAGIC
429 /* duplicated from linux/sync_file.h to avoid build-time dependency
430 * on new (v4.7) kernel headers. Once distro's are mostly using
431 * something newer than v4.7 drop this and #include <linux/sync_file.h>
432 * instead.
433 */
434 struct sync_merge_data {
435 char name[32];
436 __s32 fd2;
437 __s32 fence;
438 __u32 flags;
439 __u32 pad;
440 };
441
442 #define SYNC_IOC_MAGIC '>'
443 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
444 #endif
445
446 int
447 anv_gem_sync_file_merge(struct anv_device *device, int fd1, int fd2)
448 {
449 struct sync_merge_data args = {
450 .name = "anv merge fence",
451 .fd2 = fd2,
452 .fence = -1,
453 };
454
455 int ret = gen_ioctl(fd1, SYNC_IOC_MERGE, &args);
456 if (ret == -1)
457 return -1;
458
459 return args.fence;
460 }
461
462 uint32_t
463 anv_gem_syncobj_create(struct anv_device *device, uint32_t flags)
464 {
465 struct drm_syncobj_create args = {
466 .flags = flags,
467 };
468
469 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
470 if (ret)
471 return 0;
472
473 return args.handle;
474 }
475
476 void
477 anv_gem_syncobj_destroy(struct anv_device *device, uint32_t handle)
478 {
479 struct drm_syncobj_destroy args = {
480 .handle = handle,
481 };
482
483 gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
484 }
485
486 int
487 anv_gem_syncobj_handle_to_fd(struct anv_device *device, uint32_t handle)
488 {
489 struct drm_syncobj_handle args = {
490 .handle = handle,
491 };
492
493 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
494 if (ret)
495 return -1;
496
497 return args.fd;
498 }
499
500 uint32_t
501 anv_gem_syncobj_fd_to_handle(struct anv_device *device, int fd)
502 {
503 struct drm_syncobj_handle args = {
504 .fd = fd,
505 };
506
507 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
508 if (ret)
509 return 0;
510
511 return args.handle;
512 }
513
514 int
515 anv_gem_syncobj_export_sync_file(struct anv_device *device, uint32_t handle)
516 {
517 struct drm_syncobj_handle args = {
518 .handle = handle,
519 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE,
520 };
521
522 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
523 if (ret)
524 return -1;
525
526 return args.fd;
527 }
528
529 int
530 anv_gem_syncobj_import_sync_file(struct anv_device *device,
531 uint32_t handle, int fd)
532 {
533 struct drm_syncobj_handle args = {
534 .handle = handle,
535 .fd = fd,
536 .flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE,
537 };
538
539 return gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
540 }
541
542 void
543 anv_gem_syncobj_reset(struct anv_device *device, uint32_t handle)
544 {
545 struct drm_syncobj_array args = {
546 .handles = (uint64_t)(uintptr_t)&handle,
547 .count_handles = 1,
548 };
549
550 gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
551 }
552
553 bool
554 anv_gem_supports_syncobj_wait(int fd)
555 {
556 int ret;
557
558 struct drm_syncobj_create create = {
559 .flags = 0,
560 };
561 ret = gen_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &create);
562 if (ret)
563 return false;
564
565 uint32_t syncobj = create.handle;
566
567 struct drm_syncobj_wait wait = {
568 .handles = (uint64_t)(uintptr_t)&create,
569 .count_handles = 1,
570 .timeout_nsec = 0,
571 .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
572 };
573 ret = gen_ioctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &wait);
574
575 struct drm_syncobj_destroy destroy = {
576 .handle = syncobj,
577 };
578 gen_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy);
579
580 /* If it timed out, then we have the ioctl and it supports the
581 * DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT flag.
582 */
583 return ret == -1 && errno == ETIME;
584 }
585
586 int
587 anv_gem_syncobj_wait(struct anv_device *device,
588 uint32_t *handles, uint32_t num_handles,
589 int64_t abs_timeout_ns, bool wait_all)
590 {
591 struct drm_syncobj_wait args = {
592 .handles = (uint64_t)(uintptr_t)handles,
593 .count_handles = num_handles,
594 .timeout_nsec = abs_timeout_ns,
595 .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
596 };
597
598 if (wait_all)
599 args.flags |= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL;
600
601 return gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
602 }