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>
31 #include "anv_private.h"
34 anv_ioctl(int fd
, unsigned long request
, void *arg
)
39 ret
= ioctl(fd
, request
, arg
);
40 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
46 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
48 * Return gem handle, or 0 on failure. Gem handles are never 0.
51 anv_gem_create(struct anv_device
*device
, size_t size
)
53 struct drm_i915_gem_create gem_create
= {
57 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
);
59 /* FIXME: What do we do if this fails? */
63 return gem_create
.handle
;
67 anv_gem_close(struct anv_device
*device
, uint32_t gem_handle
)
69 struct drm_gem_close close
= {
73 anv_ioctl(device
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
77 * Wrapper around DRM_IOCTL_I915_GEM_MMAP.
80 anv_gem_mmap(struct anv_device
*device
, uint32_t gem_handle
,
81 uint64_t offset
, uint64_t size
, uint32_t flags
)
83 struct drm_i915_gem_mmap gem_mmap
= {
90 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &gem_mmap
);
94 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap
.addr_ptr
, gem_mmap
.size
, 0, 1));
95 return (void *)(uintptr_t) gem_mmap
.addr_ptr
;
98 /* This is just a wrapper around munmap, but it also notifies valgrind that
99 * this map is no longer valid. Pair this with anv_gem_mmap().
102 anv_gem_munmap(void *p
, uint64_t size
)
104 VG(VALGRIND_FREELIKE_BLOCK(p
, 0));
109 anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
)
111 struct drm_i915_gem_userptr userptr
= {
112 .user_ptr
= (__u64
)((unsigned long) mem
),
117 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &userptr
);
121 return userptr
.handle
;
125 anv_gem_set_caching(struct anv_device
*device
,
126 uint32_t gem_handle
, uint32_t caching
)
128 struct drm_i915_gem_caching gem_caching
= {
129 .handle
= gem_handle
,
133 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_CACHING
, &gem_caching
);
137 anv_gem_set_domain(struct anv_device
*device
, uint32_t gem_handle
,
138 uint32_t read_domains
, uint32_t write_domain
)
140 struct drm_i915_gem_set_domain gem_set_domain
= {
141 .handle
= gem_handle
,
142 .read_domains
= read_domains
,
143 .write_domain
= write_domain
,
146 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_DOMAIN
, &gem_set_domain
);
150 * On error, \a timeout_ns holds the remaining time.
153 anv_gem_wait(struct anv_device
*device
, uint32_t gem_handle
, int64_t *timeout_ns
)
155 struct drm_i915_gem_wait wait
= {
156 .bo_handle
= gem_handle
,
157 .timeout_ns
= *timeout_ns
,
161 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
162 *timeout_ns
= wait
.timeout_ns
;
168 anv_gem_execbuffer(struct anv_device
*device
,
169 struct drm_i915_gem_execbuffer2
*execbuf
)
171 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2
, execbuf
);
175 anv_gem_set_tiling(struct anv_device
*device
,
176 uint32_t gem_handle
, uint32_t stride
, uint32_t tiling
)
180 /* set_tiling overwrites the input on the error path, so we have to open
184 struct drm_i915_gem_set_tiling set_tiling
= {
185 .handle
= gem_handle
,
186 .tiling_mode
= tiling
,
190 ret
= ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
191 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
197 anv_gem_get_param(int fd
, uint32_t param
)
201 drm_i915_getparam_t gp
= {
206 int ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
214 anv_gem_get_bit6_swizzle(int fd
, uint32_t tiling
)
216 struct drm_gem_close close
;
219 struct drm_i915_gem_create gem_create
= {
223 if (anv_ioctl(fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
)) {
224 assert(!"Failed to create GEM BO");
228 bool swizzled
= false;
230 /* set_tiling overwrites the input on the error path, so we have to open
234 struct drm_i915_gem_set_tiling set_tiling
= {
235 .handle
= gem_create
.handle
,
236 .tiling_mode
= tiling
,
237 .stride
= tiling
== I915_TILING_X
? 512 : 128,
240 ret
= ioctl(fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
241 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
244 assert(!"Failed to set BO tiling");
245 goto close_and_return
;
248 struct drm_i915_gem_get_tiling get_tiling
= {
249 .handle
= gem_create
.handle
,
252 if (anv_ioctl(fd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
)) {
253 assert(!"Failed to get BO tiling");
254 goto close_and_return
;
257 swizzled
= get_tiling
.swizzle_mode
!= I915_BIT_6_SWIZZLE_NONE
;
261 memset(&close
, 0, sizeof(close
));
262 close
.handle
= gem_create
.handle
;
263 anv_ioctl(fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
269 anv_gem_create_context(struct anv_device
*device
)
271 struct drm_i915_gem_context_create create
= { 0 };
273 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
277 return create
.ctx_id
;
281 anv_gem_destroy_context(struct anv_device
*device
, int context
)
283 struct drm_i915_gem_context_destroy destroy
= {
287 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &destroy
);
291 anv_gem_get_aperture(int fd
, uint64_t *size
)
293 struct drm_i915_gem_get_aperture aperture
= { 0 };
295 int ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GEM_GET_APERTURE
, &aperture
);
299 *size
= aperture
.aper_available_size
;
305 anv_gem_handle_to_fd(struct anv_device
*device
, uint32_t gem_handle
)
307 struct drm_prime_handle args
= {
308 .handle
= gem_handle
,
309 .flags
= DRM_CLOEXEC
,
312 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
320 anv_gem_fd_to_handle(struct anv_device
*device
, int fd
)
322 struct drm_prime_handle args
= {
326 int ret
= anv_ioctl(device
->fd
, DRM_IOCTL_PRIME_FD_TO_HANDLE
, &args
);