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>
33 #include "anv_private.h"
35 #define VG_CLEAR(s) VG(memset(&s, 0, sizeof(s)))
38 anv_ioctl(int fd, unsigned long request, void *arg)
43 ret = ioctl(fd, request, arg);
44 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
50 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
52 * Return gem handle, or 0 on failure. Gem handles are never 0.
55 anv_gem_create(struct anv_device *device, size_t size)
57 struct drm_i915_gem_create gem_create;
61 gem_create.size = size;
63 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
65 /* FIXME: What do we do if this fails? */
69 return gem_create.handle;
73 anv_gem_close(struct anv_device *device, uint32_t gem_handle)
75 struct drm_gem_close close;
78 close.handle = gem_handle;
79 anv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
83 * Wrapper around DRM_IOCTL_I915_GEM_MMAP.
86 anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
87 uint64_t offset, uint64_t size, uint32_t flags)
89 struct drm_i915_gem_mmap gem_mmap;
92 gem_mmap.handle = gem_handle;
93 VG_CLEAR(gem_mmap.pad);
94 gem_mmap.offset = offset;
96 VG_CLEAR(gem_mmap.addr_ptr);
97 gem_mmap.flags = flags;
99 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
101 /* FIXME: Is NULL the right error return? Cf MAP_INVALID */
105 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap.addr_ptr, gem_mmap.size, 0, 1));
106 return (void *)(uintptr_t) gem_mmap.addr_ptr;
109 /* This is just a wrapper around munmap, but it also notifies valgrind that
110 * this map is no longer valid. Pair this with anv_gem_mmap().
113 anv_gem_munmap(void *p, uint64_t size)
115 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
120 anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
122 struct drm_i915_gem_userptr userptr;
126 userptr.user_ptr = (__u64)((unsigned long) mem);
127 userptr.user_size = size;
130 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
134 return userptr.handle;
138 anv_gem_set_caching(struct anv_device *device,
139 uint32_t gem_handle, uint32_t caching)
141 struct drm_i915_gem_caching gem_caching;
143 VG_CLEAR(gem_caching);
144 gem_caching.handle = gem_handle;
145 gem_caching.caching = caching;
147 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
151 anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
152 uint32_t read_domains, uint32_t write_domain)
154 struct drm_i915_gem_set_domain gem_set_domain;
156 VG_CLEAR(gem_set_domain);
157 gem_set_domain.handle = gem_handle;
158 gem_set_domain.read_domains = read_domains;
159 gem_set_domain.write_domain = write_domain;
161 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &gem_set_domain);
165 * On error, \a timeout_ns holds the remaining time.
168 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
170 struct drm_i915_gem_wait wait;
174 wait.bo_handle = gem_handle;
175 wait.timeout_ns = *timeout_ns;
178 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
179 *timeout_ns = wait.timeout_ns;
185 anv_gem_execbuffer(struct anv_device *device,
186 struct drm_i915_gem_execbuffer2 *execbuf)
188 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
192 anv_gem_set_tiling(struct anv_device *device,
193 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
195 struct drm_i915_gem_set_tiling set_tiling;
198 /* set_tiling overwrites the input on the error path, so we have to open
203 VG_CLEAR(set_tiling);
204 set_tiling.handle = gem_handle;
205 set_tiling.tiling_mode = tiling;
206 set_tiling.stride = stride;
208 ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
209 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
215 anv_gem_get_param(int fd, uint32_t param)
217 drm_i915_getparam_t gp;
223 ret = anv_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
231 anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
233 struct drm_gem_close close;
236 struct drm_i915_gem_create gem_create;
237 VG_CLEAR(gem_create);
238 gem_create.size = 4096;
240 if (anv_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create)) {
241 assert(!"Failed to create GEM BO");
245 bool swizzled = false;
247 /* set_tiling overwrites the input on the error path, so we have to open
250 struct drm_i915_gem_set_tiling set_tiling;
252 VG_CLEAR(set_tiling);
253 set_tiling.handle = gem_create.handle;
254 set_tiling.tiling_mode = tiling;
255 set_tiling.stride = tiling == I915_TILING_X ? 512 : 128;
257 ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
258 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
261 assert(!"Failed to set BO tiling");
262 goto close_and_return;
265 struct drm_i915_gem_get_tiling get_tiling;
266 VG_CLEAR(get_tiling);
267 get_tiling.handle = gem_create.handle;
269 if (anv_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
270 assert(!"Failed to get BO tiling");
271 goto close_and_return;
274 swizzled = get_tiling.swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
279 close.handle = gem_create.handle;
280 anv_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
286 anv_gem_create_context(struct anv_device *device)
288 struct drm_i915_gem_context_create create;
293 ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
297 return create.ctx_id;
301 anv_gem_destroy_context(struct anv_device *device, int context)
303 struct drm_i915_gem_context_destroy destroy;
306 destroy.ctx_id = context;
308 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
312 anv_gem_get_aperture(int fd, uint64_t *size)
314 struct drm_i915_gem_get_aperture aperture;
318 ret = anv_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
322 *size = aperture.aper_available_size;
328 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
330 struct drm_prime_handle args;
334 args.handle = gem_handle;
335 args.flags = DRM_CLOEXEC;
337 ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
345 anv_gem_fd_to_handle(struct anv_device *device, int fd)
347 struct drm_prime_handle args;
353 ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);