anv: remove define _DEFAULT_SOURCE
[mesa.git] / src / intel / vulkan / anv_gem.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <sys/ioctl.h>
25 #include <sys/mman.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include "anv_private.h"
32
33 static int
34 anv_ioctl(int fd, unsigned long request, void *arg)
35 {
36 int ret;
37
38 do {
39 ret = ioctl(fd, request, arg);
40 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
41
42 return ret;
43 }
44
45 /**
46 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
47 *
48 * Return gem handle, or 0 on failure. Gem handles are never 0.
49 */
50 uint32_t
51 anv_gem_create(struct anv_device *device, size_t size)
52 {
53 struct drm_i915_gem_create gem_create = {
54 .size = size,
55 };
56
57 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
58 if (ret != 0) {
59 /* FIXME: What do we do if this fails? */
60 return 0;
61 }
62
63 return gem_create.handle;
64 }
65
66 void
67 anv_gem_close(struct anv_device *device, uint32_t gem_handle)
68 {
69 struct drm_gem_close close = {
70 .handle = gem_handle,
71 };
72
73 anv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
74 }
75
76 /**
77 * Wrapper around DRM_IOCTL_I915_GEM_MMAP.
78 */
79 void*
80 anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
81 uint64_t offset, uint64_t size, uint32_t flags)
82 {
83 struct drm_i915_gem_mmap gem_mmap = {
84 .handle = gem_handle,
85 .offset = offset,
86 .size = size,
87 .flags = flags,
88 };
89
90 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
91 if (ret != 0) {
92 /* FIXME: Is NULL the right error return? Cf MAP_INVALID */
93 return NULL;
94 }
95
96 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap.addr_ptr, gem_mmap.size, 0, 1));
97 return (void *)(uintptr_t) gem_mmap.addr_ptr;
98 }
99
100 /* This is just a wrapper around munmap, but it also notifies valgrind that
101 * this map is no longer valid. Pair this with anv_gem_mmap().
102 */
103 void
104 anv_gem_munmap(void *p, uint64_t size)
105 {
106 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
107 munmap(p, size);
108 }
109
110 uint32_t
111 anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
112 {
113 struct drm_i915_gem_userptr userptr = {
114 .user_ptr = (__u64)((unsigned long) mem),
115 .user_size = size,
116 .flags = 0,
117 };
118
119 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
120 if (ret == -1)
121 return 0;
122
123 return userptr.handle;
124 }
125
126 int
127 anv_gem_set_caching(struct anv_device *device,
128 uint32_t gem_handle, uint32_t caching)
129 {
130 struct drm_i915_gem_caching gem_caching = {
131 .handle = gem_handle,
132 .caching = caching,
133 };
134
135 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
136 }
137
138 int
139 anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
140 uint32_t read_domains, uint32_t write_domain)
141 {
142 struct drm_i915_gem_set_domain gem_set_domain = {
143 .handle = gem_handle,
144 .read_domains = read_domains,
145 .write_domain = write_domain,
146 };
147
148 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &gem_set_domain);
149 }
150
151 /**
152 * On error, \a timeout_ns holds the remaining time.
153 */
154 int
155 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
156 {
157 struct drm_i915_gem_wait wait = {
158 .bo_handle = gem_handle,
159 .timeout_ns = *timeout_ns,
160 .flags = 0,
161 };
162
163 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
164 *timeout_ns = wait.timeout_ns;
165
166 return ret;
167 }
168
169 int
170 anv_gem_execbuffer(struct anv_device *device,
171 struct drm_i915_gem_execbuffer2 *execbuf)
172 {
173 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
174 }
175
176 int
177 anv_gem_set_tiling(struct anv_device *device,
178 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
179 {
180 int ret;
181
182 /* set_tiling overwrites the input on the error path, so we have to open
183 * code anv_ioctl.
184 */
185 do {
186 struct drm_i915_gem_set_tiling set_tiling = {
187 .handle = gem_handle,
188 .tiling_mode = tiling,
189 .stride = stride,
190 };
191
192 ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
193 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
194
195 return ret;
196 }
197
198 int
199 anv_gem_get_param(int fd, uint32_t param)
200 {
201 int tmp;
202
203 drm_i915_getparam_t gp = {
204 .param = param,
205 .value = &tmp,
206 };
207
208 int ret = anv_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
209 if (ret == 0)
210 return tmp;
211
212 return 0;
213 }
214
215 bool
216 anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
217 {
218 struct drm_gem_close close;
219 int ret;
220
221 struct drm_i915_gem_create gem_create = {
222 .size = 4096,
223 };
224
225 if (anv_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create)) {
226 assert(!"Failed to create GEM BO");
227 return false;
228 }
229
230 bool swizzled = false;
231
232 /* set_tiling overwrites the input on the error path, so we have to open
233 * code anv_ioctl.
234 */
235 do {
236 struct drm_i915_gem_set_tiling set_tiling = {
237 .handle = gem_create.handle,
238 .tiling_mode = tiling,
239 .stride = tiling == I915_TILING_X ? 512 : 128,
240 };
241
242 ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
243 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
244
245 if (ret != 0) {
246 assert(!"Failed to set BO tiling");
247 goto close_and_return;
248 }
249
250 struct drm_i915_gem_get_tiling get_tiling = {
251 .handle = gem_create.handle,
252 };
253
254 if (anv_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
255 assert(!"Failed to get BO tiling");
256 goto close_and_return;
257 }
258
259 swizzled = get_tiling.swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
260
261 close_and_return:
262
263 memset(&close, 0, sizeof(close));
264 close.handle = gem_create.handle;
265 anv_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
266
267 return swizzled;
268 }
269
270 int
271 anv_gem_create_context(struct anv_device *device)
272 {
273 struct drm_i915_gem_context_create create = { 0 };
274
275 int ret = anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
276 if (ret == -1)
277 return -1;
278
279 return create.ctx_id;
280 }
281
282 int
283 anv_gem_destroy_context(struct anv_device *device, int context)
284 {
285 struct drm_i915_gem_context_destroy destroy = {
286 .ctx_id = context,
287 };
288
289 return anv_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
290 }
291
292 int
293 anv_gem_get_aperture(int fd, uint64_t *size)
294 {
295 struct drm_i915_gem_get_aperture aperture = { 0 };
296
297 int ret = anv_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
298 if (ret == -1)
299 return -1;
300
301 *size = aperture.aper_available_size;
302
303 return 0;
304 }
305
306 int
307 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
308 {
309 struct drm_prime_handle args = {
310 .handle = gem_handle,
311 .flags = DRM_CLOEXEC,
312 };
313
314 int ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
315 if (ret == -1)
316 return -1;
317
318 return args.fd;
319 }
320
321 uint32_t
322 anv_gem_fd_to_handle(struct anv_device *device, int fd)
323 {
324 struct drm_prime_handle args = {
325 .fd = fd,
326 };
327
328 int ret = anv_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
329 if (ret == -1)
330 return 0;
331
332 return args.handle;
333 }