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"
35 anv_ioctl(int fd
, unsigned long request
, void *arg
)
40 ret
= ioctl(fd
, request
, arg
);
41 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
47 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
49 * Return gem handle, or 0 on failure. Gem handles are never 0.
52 anv_gem_create(struct anv_device
*device
, uint64_t size
)
54 struct drm_i915_gem_create gem_create
= {
58 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
);
60 /* FIXME: What do we do if this fails? */
64 return gem_create
.handle
;
68 anv_gem_close(struct anv_device
*device
, uint32_t gem_handle
)
70 struct drm_gem_close close
= {
74 anv_ioctl(device
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
78 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
81 anv_gem_mmap(struct anv_device
*device
, uint32_t gem_handle
,
82 uint64_t offset
, uint64_t size
, uint32_t flags
)
84 struct drm_i915_gem_mmap gem_mmap
= {
91 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &gem_mmap
);
95 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap
.addr_ptr
, gem_mmap
.size
, 0, 1));
96 return (void *)(uintptr_t) gem_mmap
.addr_ptr
;
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().
103 anv_gem_munmap(void *p
, uint64_t size
)
105 VG(VALGRIND_FREELIKE_BLOCK(p
, 0));
110 anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
)
112 struct drm_i915_gem_userptr userptr
= {
113 .user_ptr
= (__u64
)((unsigned long) mem
),
118 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &userptr
);
122 return userptr
.handle
;
126 anv_gem_set_caching(struct anv_device
*device
,
127 uint32_t gem_handle
, uint32_t caching
)
129 struct drm_i915_gem_caching gem_caching
= {
130 .handle
= gem_handle
,
134 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_CACHING
, &gem_caching
);
138 anv_gem_set_domain(struct anv_device
*device
, uint32_t gem_handle
,
139 uint32_t read_domains
, uint32_t write_domain
)
141 struct drm_i915_gem_set_domain gem_set_domain
= {
142 .handle
= gem_handle
,
143 .read_domains
= read_domains
,
144 .write_domain
= write_domain
,
147 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &gem_set_domain
);
151 * Returns 0, 1, or negative to indicate error
154 anv_gem_busy(struct anv_device
*device
, uint32_t gem_handle
)
156 struct drm_i915_gem_busy busy
= {
157 .handle
= gem_handle
,
160 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_BUSY
, &busy
);
164 return busy
.busy
!= 0;
168 * On error, \a timeout_ns holds the remaining time.
171 anv_gem_wait(struct anv_device
*device
, uint32_t gem_handle
, int64_t *timeout_ns
)
173 struct drm_i915_gem_wait wait
= {
174 .bo_handle
= gem_handle
,
175 .timeout_ns
= *timeout_ns
,
179 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
180 *timeout_ns
= wait
.timeout_ns
;
186 anv_gem_execbuffer(struct anv_device
*device
,
187 struct drm_i915_gem_execbuffer2
*execbuf
)
189 if (execbuf
->flags
& I915_EXEC_FENCE_OUT
)
190 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
, execbuf
);
192 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2
, execbuf
);
196 anv_gem_set_tiling(struct anv_device
*device
,
197 uint32_t gem_handle
, uint32_t stride
, uint32_t tiling
)
201 /* set_tiling overwrites the input on the error path, so we have to open
205 struct drm_i915_gem_set_tiling set_tiling
= {
206 .handle
= gem_handle
,
207 .tiling_mode
= tiling
,
211 ret
= ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
212 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
218 anv_gem_get_param(int fd
, uint32_t param
)
222 drm_i915_getparam_t gp
= {
227 int ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
235 anv_gem_get_bit6_swizzle(int fd
, uint32_t tiling
)
237 struct drm_gem_close close
;
240 struct drm_i915_gem_create gem_create
= {
244 if (anv_ioctl(fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
)) {
245 assert(!"Failed to create GEM BO");
249 bool swizzled
= false;
251 /* set_tiling overwrites the input on the error path, so we have to open
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,
261 ret
= ioctl(fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
262 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
265 assert(!"Failed to set BO tiling");
266 goto close_and_return
;
269 struct drm_i915_gem_get_tiling get_tiling
= {
270 .handle
= gem_create
.handle
,
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
;
278 swizzled
= get_tiling
.swizzle_mode
!= I915_BIT_6_SWIZZLE_NONE
;
282 memset(&close
, 0, sizeof(close
));
283 close
.handle
= gem_create
.handle
;
284 anv_ioctl(fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
290 anv_gem_create_context(struct anv_device
*device
)
292 struct drm_i915_gem_context_create create
= { 0 };
294 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
298 return create
.ctx_id
;
302 anv_gem_destroy_context(struct anv_device
*device
, int context
)
304 struct drm_i915_gem_context_destroy destroy
= {
308 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &destroy
);
312 anv_gem_get_context_param(int fd
, int context
, uint32_t param
, uint64_t *value
)
314 struct drm_i915_gem_context_param gp
= {
319 int ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM
, &gp
);
328 anv_gem_get_aperture(int fd
, uint64_t *size
)
330 struct drm_i915_gem_get_aperture aperture
= { 0 };
332 int ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GEM_GET_APERTURE
, &aperture
);
336 *size
= aperture
.aper_available_size
;
342 anv_gem_supports_48b_addresses(int fd
)
344 struct drm_i915_gem_exec_object2 obj
= {
345 .flags
= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
,
348 struct drm_i915_gem_execbuffer2 execbuf
= {
349 .buffers_ptr
= (uintptr_t)&obj
,
354 int ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2
, &execbuf
);
356 return ret
== -1 && errno
== ENOENT
;
360 anv_gem_gpu_get_reset_stats(struct anv_device
*device
,
361 uint32_t *active
, uint32_t *pending
)
363 struct drm_i915_reset_stats stats
= {
364 .ctx_id
= device
->context_id
,
367 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GET_RESET_STATS
, &stats
);
369 *active
= stats
.batch_active
;
370 *pending
= stats
.batch_pending
;
377 anv_gem_handle_to_fd(struct anv_device
*device
, uint32_t gem_handle
)
379 struct drm_prime_handle args
= {
380 .handle
= gem_handle
,
381 .flags
= DRM_CLOEXEC
,
384 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
392 anv_gem_fd_to_handle(struct anv_device
*device
, int fd
)
394 struct drm_prime_handle args
= {
398 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_PRIME_FD_TO_HANDLE
, &args
);
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>
411 struct sync_merge_data
{
419 #define SYNC_IOC_MAGIC '>'
420 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
424 anv_gem_sync_file_merge(struct anv_device
*device
, int fd1
, int fd2
)
426 const char name
[] = "anv merge fence";
427 struct sync_merge_data args
= {
431 memcpy(args
.name
, name
, sizeof(name
));
433 int ret
= anv_ioctl(fd1
, SYNC_IOC_MERGE
, &args
);
441 anv_gem_syncobj_create(struct anv_device
*device
, uint32_t flags
)
443 struct drm_syncobj_create args
= {
447 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_CREATE
, &args
);
455 anv_gem_syncobj_destroy(struct anv_device
*device
, uint32_t handle
)
457 struct drm_syncobj_destroy args
= {
461 anv_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_DESTROY
, &args
);
465 anv_gem_syncobj_handle_to_fd(struct anv_device
*device
, uint32_t handle
)
467 struct drm_syncobj_handle args
= {
471 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
479 anv_gem_syncobj_fd_to_handle(struct anv_device
*device
, int fd
)
481 struct drm_syncobj_handle args
= {
485 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE
, &args
);