2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
32 #include "anv_private.h"
33 #include "common/gen_defines.h"
34 #include "common/gen_gem.h"
37 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
39 * Return gem handle, or 0 on failure. Gem handles are never 0.
42 anv_gem_create(struct anv_device
*device
, uint64_t size
)
44 struct drm_i915_gem_create gem_create
= {
48 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
);
50 /* FIXME: What do we do if this fails? */
54 return gem_create
.handle
;
58 anv_gem_close(struct anv_device
*device
, uint32_t gem_handle
)
60 struct drm_gem_close close
= {
64 gen_ioctl(device
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
68 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
71 anv_gem_mmap(struct anv_device
*device
, uint32_t gem_handle
,
72 uint64_t offset
, uint64_t size
, uint32_t flags
)
74 struct drm_i915_gem_mmap gem_mmap
= {
81 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &gem_mmap
);
85 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap
.addr_ptr
, gem_mmap
.size
, 0, 1));
86 return (void *)(uintptr_t) gem_mmap
.addr_ptr
;
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().
93 anv_gem_munmap(void *p
, uint64_t size
)
95 VG(VALGRIND_FREELIKE_BLOCK(p
, 0));
100 anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
)
102 struct drm_i915_gem_userptr userptr
= {
103 .user_ptr
= (__u64
)((unsigned long) mem
),
108 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &userptr
);
112 return userptr
.handle
;
116 anv_gem_set_caching(struct anv_device
*device
,
117 uint32_t gem_handle
, uint32_t caching
)
119 struct drm_i915_gem_caching gem_caching
= {
120 .handle
= gem_handle
,
124 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_CACHING
, &gem_caching
);
128 anv_gem_set_domain(struct anv_device
*device
, uint32_t gem_handle
,
129 uint32_t read_domains
, uint32_t write_domain
)
131 struct drm_i915_gem_set_domain gem_set_domain
= {
132 .handle
= gem_handle
,
133 .read_domains
= read_domains
,
134 .write_domain
= write_domain
,
137 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &gem_set_domain
);
141 * Returns 0, 1, or negative to indicate error
144 anv_gem_busy(struct anv_device
*device
, uint32_t gem_handle
)
146 struct drm_i915_gem_busy busy
= {
147 .handle
= gem_handle
,
150 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
154 return busy
.busy
!= 0;
158 * On error, \a timeout_ns holds the remaining time.
161 anv_gem_wait(struct anv_device
*device
, uint32_t gem_handle
, int64_t *timeout_ns
)
163 struct drm_i915_gem_wait wait
= {
164 .bo_handle
= gem_handle
,
165 .timeout_ns
= *timeout_ns
,
169 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
170 *timeout_ns
= wait
.timeout_ns
;
176 anv_gem_execbuffer(struct anv_device
*device
,
177 struct drm_i915_gem_execbuffer2
*execbuf
)
179 if (execbuf
->flags
& I915_EXEC_FENCE_OUT
)
180 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
, execbuf
);
182 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2
, execbuf
);
185 /** Return -1 on error. */
187 anv_gem_get_tiling(struct anv_device
*device
, uint32_t gem_handle
)
189 struct drm_i915_gem_get_tiling get_tiling
= {
190 .handle
= gem_handle
,
193 if (gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
)) {
194 assert(!"Failed to get BO tiling");
198 return get_tiling
.tiling_mode
;
202 anv_gem_set_tiling(struct anv_device
*device
,
203 uint32_t gem_handle
, uint32_t stride
, uint32_t tiling
)
207 /* set_tiling overwrites the input on the error path, so we have to open
211 struct drm_i915_gem_set_tiling set_tiling
= {
212 .handle
= gem_handle
,
213 .tiling_mode
= tiling
,
217 ret
= ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
218 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
224 anv_gem_get_param(int fd
, uint32_t param
)
228 drm_i915_getparam_t gp
= {
233 int ret
= gen_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
241 anv_gem_get_bit6_swizzle(int fd
, uint32_t tiling
)
243 struct drm_gem_close close
;
246 struct drm_i915_gem_create gem_create
= {
250 if (gen_ioctl(fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
)) {
251 assert(!"Failed to create GEM BO");
255 bool swizzled
= false;
257 /* set_tiling overwrites the input on the error path, so we have to open
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,
267 ret
= ioctl(fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
268 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
271 assert(!"Failed to set BO tiling");
272 goto close_and_return
;
275 struct drm_i915_gem_get_tiling get_tiling
= {
276 .handle
= gem_create
.handle
,
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
;
284 swizzled
= get_tiling
.swizzle_mode
!= I915_BIT_6_SWIZZLE_NONE
;
288 memset(&close
, 0, sizeof(close
));
289 close
.handle
= gem_create
.handle
;
290 gen_ioctl(fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
296 anv_gem_has_context_priority(int fd
)
298 return !anv_gem_set_context_param(fd
, 0, I915_CONTEXT_PARAM_PRIORITY
,
299 GEN_CONTEXT_MEDIUM_PRIORITY
);
303 anv_gem_create_context(struct anv_device
*device
)
305 struct drm_i915_gem_context_create create
= { 0 };
307 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
311 return create
.ctx_id
;
315 anv_gem_destroy_context(struct anv_device
*device
, int context
)
317 struct drm_i915_gem_context_destroy destroy
= {
321 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &destroy
);
325 anv_gem_set_context_param(int fd
, int context
, uint32_t param
, uint64_t value
)
327 struct drm_i915_gem_context_param p
= {
334 if (gen_ioctl(fd
, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM
, &p
))
340 anv_gem_get_context_param(int fd
, int context
, uint32_t param
, uint64_t *value
)
342 struct drm_i915_gem_context_param gp
= {
347 int ret
= gen_ioctl(fd
, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM
, &gp
);
356 anv_gem_get_aperture(int fd
, uint64_t *size
)
358 struct drm_i915_gem_get_aperture aperture
= { 0 };
360 int ret
= gen_ioctl(fd
, DRM_IOCTL_I915_GEM_GET_APERTURE
, &aperture
);
364 *size
= aperture
.aper_available_size
;
370 anv_gem_gpu_get_reset_stats(struct anv_device
*device
,
371 uint32_t *active
, uint32_t *pending
)
373 struct drm_i915_reset_stats stats
= {
374 .ctx_id
= device
->context_id
,
377 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GET_RESET_STATS
, &stats
);
379 *active
= stats
.batch_active
;
380 *pending
= stats
.batch_pending
;
387 anv_gem_handle_to_fd(struct anv_device
*device
, uint32_t gem_handle
)
389 struct drm_prime_handle args
= {
390 .handle
= gem_handle
,
391 .flags
= DRM_CLOEXEC
,
394 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
402 anv_gem_fd_to_handle(struct anv_device
*device
, int fd
)
404 struct drm_prime_handle args
= {
408 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_PRIME_FD_TO_HANDLE
, &args
);
416 anv_gem_reg_read(struct anv_device
*device
, uint32_t offset
, uint64_t *result
)
418 struct drm_i915_reg_read args
= {
422 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_REG_READ
, &args
);
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>
434 struct sync_merge_data
{
442 #define SYNC_IOC_MAGIC '>'
443 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
447 anv_gem_sync_file_merge(struct anv_device
*device
, int fd1
, int fd2
)
449 struct sync_merge_data args
= {
450 .name
= "anv merge fence",
455 int ret
= gen_ioctl(fd1
, SYNC_IOC_MERGE
, &args
);
463 anv_gem_syncobj_create(struct anv_device
*device
, uint32_t flags
)
465 struct drm_syncobj_create args
= {
469 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_CREATE
, &args
);
477 anv_gem_syncobj_destroy(struct anv_device
*device
, uint32_t handle
)
479 struct drm_syncobj_destroy args
= {
483 gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_DESTROY
, &args
);
487 anv_gem_syncobj_handle_to_fd(struct anv_device
*device
, uint32_t handle
)
489 struct drm_syncobj_handle args
= {
493 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
501 anv_gem_syncobj_fd_to_handle(struct anv_device
*device
, int fd
)
503 struct drm_syncobj_handle args
= {
507 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE
, &args
);
515 anv_gem_syncobj_export_sync_file(struct anv_device
*device
, uint32_t handle
)
517 struct drm_syncobj_handle args
= {
519 .flags
= DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
,
522 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
530 anv_gem_syncobj_import_sync_file(struct anv_device
*device
,
531 uint32_t handle
, int fd
)
533 struct drm_syncobj_handle args
= {
536 .flags
= DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
,
539 return gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE
, &args
);
543 anv_gem_syncobj_reset(struct anv_device
*device
, uint32_t handle
)
545 struct drm_syncobj_array args
= {
546 .handles
= (uint64_t)(uintptr_t)&handle
,
550 gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_RESET
, &args
);
554 anv_gem_supports_syncobj_wait(int fd
)
558 struct drm_syncobj_create create
= {
561 ret
= gen_ioctl(fd
, DRM_IOCTL_SYNCOBJ_CREATE
, &create
);
565 uint32_t syncobj
= create
.handle
;
567 struct drm_syncobj_wait wait
= {
568 .handles
= (uint64_t)(uintptr_t)&create
,
571 .flags
= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT
,
573 ret
= gen_ioctl(fd
, DRM_IOCTL_SYNCOBJ_WAIT
, &wait
);
575 struct drm_syncobj_destroy destroy
= {
578 gen_ioctl(fd
, DRM_IOCTL_SYNCOBJ_DESTROY
, &destroy
);
580 /* If it timed out, then we have the ioctl and it supports the
581 * DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT flag.
583 return ret
== -1 && errno
== ETIME
;
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
)
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
,
599 args
.flags
|= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
;
601 return gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_WAIT
, &args
);