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>
31 #include <linux/sync_file.h>
33 #include "anv_private.h"
34 #include "common/gen_defines.h"
35 #include "common/gen_gem.h"
38 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
40 * Return gem handle, or 0 on failure. Gem handles are never 0.
43 anv_gem_create(struct anv_device
*device
, uint64_t size
)
45 struct drm_i915_gem_create gem_create
= {
49 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
);
51 /* FIXME: What do we do if this fails? */
55 return gem_create
.handle
;
59 anv_gem_close(struct anv_device
*device
, uint32_t gem_handle
)
61 struct drm_gem_close close
= {
65 gen_ioctl(device
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
69 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
72 anv_gem_mmap_offset(struct anv_device
*device
, uint32_t gem_handle
,
73 uint64_t offset
, uint64_t size
, uint32_t flags
)
75 struct drm_i915_gem_mmap_offset gem_mmap
= {
77 .flags
= (flags
& I915_MMAP_WC
) ?
78 I915_MMAP_OFFSET_WC
: I915_MMAP_OFFSET_WB
,
82 /* Get the fake offset back */
83 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_MMAP_OFFSET
, &gem_mmap
);
88 void *map
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
89 device
->fd
, gem_mmap
.offset
);
94 anv_gem_mmap_legacy(struct anv_device
*device
, uint32_t gem_handle
,
95 uint64_t offset
, uint64_t size
, uint32_t flags
)
97 struct drm_i915_gem_mmap gem_mmap
= {
104 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &gem_mmap
);
108 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap
.addr_ptr
, gem_mmap
.size
, 0, 1));
109 return (void *)(uintptr_t) gem_mmap
.addr_ptr
;
113 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
116 anv_gem_mmap(struct anv_device
*device
, uint32_t gem_handle
,
117 uint64_t offset
, uint64_t size
, uint32_t flags
)
119 if (device
->physical
->has_mmap_offset
)
120 return anv_gem_mmap_offset(device
, gem_handle
, offset
, size
, flags
);
122 return anv_gem_mmap_legacy(device
, gem_handle
, offset
, size
, flags
);
125 /* This is just a wrapper around munmap, but it also notifies valgrind that
126 * this map is no longer valid. Pair this with anv_gem_mmap().
129 anv_gem_munmap(struct anv_device
*device
, void *p
, uint64_t size
)
131 if (!device
->physical
->has_mmap_offset
)
132 VG(VALGRIND_FREELIKE_BLOCK(p
, 0));
137 anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
)
139 struct drm_i915_gem_userptr userptr
= {
140 .user_ptr
= (__u64
)((unsigned long) mem
),
145 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &userptr
);
149 return userptr
.handle
;
153 anv_gem_set_caching(struct anv_device
*device
,
154 uint32_t gem_handle
, uint32_t caching
)
156 struct drm_i915_gem_caching gem_caching
= {
157 .handle
= gem_handle
,
161 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_CACHING
, &gem_caching
);
165 anv_gem_set_domain(struct anv_device
*device
, uint32_t gem_handle
,
166 uint32_t read_domains
, uint32_t write_domain
)
168 struct drm_i915_gem_set_domain gem_set_domain
= {
169 .handle
= gem_handle
,
170 .read_domains
= read_domains
,
171 .write_domain
= write_domain
,
174 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &gem_set_domain
);
178 * Returns 0, 1, or negative to indicate error
181 anv_gem_busy(struct anv_device
*device
, uint32_t gem_handle
)
183 struct drm_i915_gem_busy busy
= {
184 .handle
= gem_handle
,
187 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
191 return busy
.busy
!= 0;
195 * On error, \a timeout_ns holds the remaining time.
198 anv_gem_wait(struct anv_device
*device
, uint32_t gem_handle
, int64_t *timeout_ns
)
200 struct drm_i915_gem_wait wait
= {
201 .bo_handle
= gem_handle
,
202 .timeout_ns
= *timeout_ns
,
206 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
207 *timeout_ns
= wait
.timeout_ns
;
213 anv_gem_execbuffer(struct anv_device
*device
,
214 struct drm_i915_gem_execbuffer2
*execbuf
)
216 if (execbuf
->flags
& I915_EXEC_FENCE_OUT
)
217 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
, execbuf
);
219 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2
, execbuf
);
222 /** Return -1 on error. */
224 anv_gem_get_tiling(struct anv_device
*device
, uint32_t gem_handle
)
226 struct drm_i915_gem_get_tiling get_tiling
= {
227 .handle
= gem_handle
,
230 if (gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
)) {
231 assert(!"Failed to get BO tiling");
235 return get_tiling
.tiling_mode
;
239 anv_gem_set_tiling(struct anv_device
*device
,
240 uint32_t gem_handle
, uint32_t stride
, uint32_t tiling
)
244 /* set_tiling overwrites the input on the error path, so we have to open
248 struct drm_i915_gem_set_tiling set_tiling
= {
249 .handle
= gem_handle
,
250 .tiling_mode
= tiling
,
254 ret
= ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
255 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
261 anv_gem_get_param(int fd
, uint32_t param
)
265 drm_i915_getparam_t gp
= {
270 int ret
= gen_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
278 anv_gem_get_bit6_swizzle(int fd
, uint32_t tiling
)
280 struct drm_gem_close close
;
283 struct drm_i915_gem_create gem_create
= {
287 if (gen_ioctl(fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
)) {
288 assert(!"Failed to create GEM BO");
292 bool swizzled
= false;
294 /* set_tiling overwrites the input on the error path, so we have to open
298 struct drm_i915_gem_set_tiling set_tiling
= {
299 .handle
= gem_create
.handle
,
300 .tiling_mode
= tiling
,
301 .stride
= tiling
== I915_TILING_X
? 512 : 128,
304 ret
= ioctl(fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
305 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
308 assert(!"Failed to set BO tiling");
309 goto close_and_return
;
312 struct drm_i915_gem_get_tiling get_tiling
= {
313 .handle
= gem_create
.handle
,
316 if (gen_ioctl(fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
)) {
317 assert(!"Failed to get BO tiling");
318 goto close_and_return
;
321 swizzled
= get_tiling
.swizzle_mode
!= I915_BIT_6_SWIZZLE_NONE
;
325 memset(&close
, 0, sizeof(close
));
326 close
.handle
= gem_create
.handle
;
327 gen_ioctl(fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
333 anv_gem_has_context_priority(int fd
)
335 return !anv_gem_set_context_param(fd
, 0, I915_CONTEXT_PARAM_PRIORITY
,
336 GEN_CONTEXT_MEDIUM_PRIORITY
);
340 anv_gem_create_context(struct anv_device
*device
)
342 struct drm_i915_gem_context_create create
= { 0 };
344 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
348 return create
.ctx_id
;
352 anv_gem_destroy_context(struct anv_device
*device
, int context
)
354 struct drm_i915_gem_context_destroy destroy
= {
358 return gen_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &destroy
);
362 anv_gem_set_context_param(int fd
, int context
, uint32_t param
, uint64_t value
)
364 struct drm_i915_gem_context_param p
= {
371 if (gen_ioctl(fd
, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM
, &p
))
377 anv_gem_get_context_param(int fd
, int context
, uint32_t param
, uint64_t *value
)
379 struct drm_i915_gem_context_param gp
= {
384 int ret
= gen_ioctl(fd
, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM
, &gp
);
393 anv_gem_gpu_get_reset_stats(struct anv_device
*device
,
394 uint32_t *active
, uint32_t *pending
)
396 struct drm_i915_reset_stats stats
= {
397 .ctx_id
= device
->context_id
,
400 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_GET_RESET_STATS
, &stats
);
402 *active
= stats
.batch_active
;
403 *pending
= stats
.batch_pending
;
410 anv_gem_handle_to_fd(struct anv_device
*device
, uint32_t gem_handle
)
412 struct drm_prime_handle args
= {
413 .handle
= gem_handle
,
414 .flags
= DRM_CLOEXEC
,
417 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
425 anv_gem_fd_to_handle(struct anv_device
*device
, int fd
)
427 struct drm_prime_handle args
= {
431 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_PRIME_FD_TO_HANDLE
, &args
);
439 anv_gem_reg_read(struct anv_device
*device
, uint32_t offset
, uint64_t *result
)
441 struct drm_i915_reg_read args
= {
445 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_I915_REG_READ
, &args
);
452 anv_gem_sync_file_merge(struct anv_device
*device
, int fd1
, int fd2
)
454 struct sync_merge_data args
= {
455 .name
= "anv merge fence",
460 int ret
= gen_ioctl(fd1
, SYNC_IOC_MERGE
, &args
);
468 anv_gem_syncobj_create(struct anv_device
*device
, uint32_t flags
)
470 struct drm_syncobj_create args
= {
474 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_CREATE
, &args
);
482 anv_gem_syncobj_destroy(struct anv_device
*device
, uint32_t handle
)
484 struct drm_syncobj_destroy args
= {
488 gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_DESTROY
, &args
);
492 anv_gem_syncobj_handle_to_fd(struct anv_device
*device
, uint32_t handle
)
494 struct drm_syncobj_handle args
= {
498 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
506 anv_gem_syncobj_fd_to_handle(struct anv_device
*device
, int fd
)
508 struct drm_syncobj_handle args
= {
512 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE
, &args
);
520 anv_gem_syncobj_export_sync_file(struct anv_device
*device
, uint32_t handle
)
522 struct drm_syncobj_handle args
= {
524 .flags
= DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
,
527 int ret
= gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
535 anv_gem_syncobj_import_sync_file(struct anv_device
*device
,
536 uint32_t handle
, int fd
)
538 struct drm_syncobj_handle args
= {
541 .flags
= DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
,
544 return gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE
, &args
);
548 anv_gem_syncobj_reset(struct anv_device
*device
, uint32_t handle
)
550 struct drm_syncobj_array args
= {
551 .handles
= (uint64_t)(uintptr_t)&handle
,
555 gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_RESET
, &args
);
559 anv_gem_supports_syncobj_wait(int fd
)
561 return gen_gem_supports_syncobj_wait(fd
);
565 anv_gem_syncobj_wait(struct anv_device
*device
,
566 uint32_t *handles
, uint32_t num_handles
,
567 int64_t abs_timeout_ns
, bool wait_all
)
569 struct drm_syncobj_wait args
= {
570 .handles
= (uint64_t)(uintptr_t)handles
,
571 .count_handles
= num_handles
,
572 .timeout_nsec
= abs_timeout_ns
,
573 .flags
= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT
,
577 args
.flags
|= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
;
579 return gen_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_WAIT
, &args
);