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 #define _DEFAULT_SOURCE
26 #include <sys/ioctl.h>
43 #define VG_CLEAR(s) VG(memset(&s, 0, sizeof(s)))
46 anv_ioctl(int fd
, unsigned long request
, void *arg
)
51 ret
= ioctl(fd
, request
, arg
);
52 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
58 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
60 * Return gem handle, or 0 on failure. Gem handles are never 0.
63 anv_gem_create(struct anv_device
*device
, size_t size
)
65 struct drm_i915_gem_create gem_create
;
69 gem_create
.size
= size
;
71 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CREATE
, &gem_create
);
73 /* FIXME: What do we do if this fails? */
77 return gem_create
.handle
;
81 anv_gem_close(struct anv_device
*device
, int gem_handle
)
83 struct drm_gem_close close
;
86 close
.handle
= gem_handle
;
87 anv_ioctl(device
->fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
91 * Wrapper around DRM_IOCTL_I915_GEM_MMAP.
94 anv_gem_mmap(struct anv_device
*device
, uint32_t gem_handle
,
95 uint64_t offset
, uint64_t size
)
97 struct drm_i915_gem_mmap gem_mmap
;
100 gem_mmap
.handle
= gem_handle
;
101 VG_CLEAR(gem_mmap
.pad
);
102 gem_mmap
.offset
= offset
;
103 gem_mmap
.size
= size
;
104 VG_CLEAR(gem_mmap
.addr_ptr
);
110 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_MMAP
, &gem_mmap
);
112 /* FIXME: Is NULL the right error return? Cf MAP_INVALID */
116 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap
.addr_ptr
, gem_mmap
.size
, 0, 1));
117 return (void *)(uintptr_t) gem_mmap
.addr_ptr
;
120 /* This is just a wrapper around munmap, but it also notifies valgrind that
121 * this map is no longer valid. Pair this with anv_gem_mmap().
124 anv_gem_munmap(void *p
, uint64_t size
)
127 VG(VALGRIND_FREELIKE_BLOCK(p
, 0));
131 anv_gem_userptr(struct anv_device
*device
, void *mem
, size_t size
)
133 struct drm_i915_gem_userptr userptr
;
137 userptr
.user_ptr
= (__u64
)((unsigned long) mem
);
138 userptr
.user_size
= size
;
141 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_USERPTR
, &userptr
);
145 return userptr
.handle
;
149 * On error, \a timeout_ns holds the remaining time.
152 anv_gem_wait(struct anv_device
*device
, int gem_handle
, int64_t *timeout_ns
)
154 struct drm_i915_gem_wait wait
;
158 wait
.bo_handle
= gem_handle
;
159 wait
.timeout_ns
= *timeout_ns
;
162 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_WAIT
, &wait
);
163 *timeout_ns
= wait
.timeout_ns
;
169 anv_gem_execbuffer(struct anv_device
*device
,
170 struct drm_i915_gem_execbuffer2
*execbuf
)
172 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_EXECBUFFER2
, execbuf
);
176 anv_gem_set_tiling(struct anv_device
*device
,
177 int gem_handle
, uint32_t stride
, uint32_t tiling
)
179 struct drm_i915_gem_set_tiling set_tiling
;
182 /* set_tiling overwrites the input on the error path, so we have to open
187 VG_CLEAR(set_tiling
);
188 set_tiling
.handle
= gem_handle
;
189 set_tiling
.tiling_mode
= I915_TILING_X
;
190 set_tiling
.stride
= stride
;
192 ret
= ioctl(device
->fd
, DRM_IOCTL_I915_GEM_SET_TILING
, &set_tiling
);
193 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
199 anv_gem_get_param(int fd
, uint32_t param
)
201 drm_i915_getparam_t gp
;
207 ret
= anv_ioctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
215 anv_gem_create_context(struct anv_device
*device
)
217 struct drm_i915_gem_context_create create
;
222 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_CREATE
, &create
);
226 return create
.ctx_id
;
230 anv_gem_destroy_context(struct anv_device
*device
, int context
)
232 struct drm_i915_gem_context_destroy destroy
;
235 destroy
.ctx_id
= context
;
237 return anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY
, &destroy
);
241 anv_gem_get_aperture(struct anv_device
*device
, uint64_t *size
)
243 struct drm_i915_gem_get_aperture aperture
;
247 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_I915_GEM_GET_APERTURE
, &aperture
);
251 *size
= aperture
.aper_available_size
;
257 anv_gem_handle_to_fd(struct anv_device
*device
, int gem_handle
)
259 struct drm_prime_handle args
;
263 args
.handle
= gem_handle
;
264 args
.flags
= DRM_CLOEXEC
;
266 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
274 anv_gem_fd_to_handle(struct anv_device
*device
, int fd
)
276 struct drm_prime_handle args
;
282 ret
= anv_ioctl(device
->fd
, DRM_IOCTL_PRIME_FD_TO_HANDLE
, &args
);