30774292b15a77c5b7ae5452d7bdb23c8fe5542d
[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/types.h>
26 #include <sys/mman.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <linux/sync_file.h>
32
33 #include "anv_private.h"
34 #include "common/gen_defines.h"
35 #include "common/gen_gem.h"
36
37 /**
38 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
39 *
40 * Return gem handle, or 0 on failure. Gem handles are never 0.
41 */
42 uint32_t
43 anv_gem_create(struct anv_device *device, uint64_t size)
44 {
45 struct drm_i915_gem_create gem_create = {
46 .size = size,
47 };
48
49 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
50 if (ret != 0) {
51 /* FIXME: What do we do if this fails? */
52 return 0;
53 }
54
55 return gem_create.handle;
56 }
57
58 void
59 anv_gem_close(struct anv_device *device, uint32_t gem_handle)
60 {
61 struct drm_gem_close close = {
62 .handle = gem_handle,
63 };
64
65 gen_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
66 }
67
68 /**
69 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
70 */
71 static void*
72 anv_gem_mmap_offset(struct anv_device *device, uint32_t gem_handle,
73 uint64_t offset, uint64_t size, uint32_t flags)
74 {
75 struct drm_i915_gem_mmap_offset gem_mmap = {
76 .handle = gem_handle,
77 .flags = (flags & I915_MMAP_WC) ?
78 I915_MMAP_OFFSET_WC : I915_MMAP_OFFSET_WB,
79 };
80 assert(offset == 0);
81
82 /* Get the fake offset back */
83 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET, &gem_mmap);
84 if (ret != 0)
85 return MAP_FAILED;
86
87 /* And map it */
88 void *map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
89 device->fd, gem_mmap.offset);
90 return map;
91 }
92
93 static void*
94 anv_gem_mmap_legacy(struct anv_device *device, uint32_t gem_handle,
95 uint64_t offset, uint64_t size, uint32_t flags)
96 {
97 struct drm_i915_gem_mmap gem_mmap = {
98 .handle = gem_handle,
99 .offset = offset,
100 .size = size,
101 .flags = flags,
102 };
103
104 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
105 if (ret != 0)
106 return MAP_FAILED;
107
108 VG(VALGRIND_MALLOCLIKE_BLOCK(gem_mmap.addr_ptr, gem_mmap.size, 0, 1));
109 return (void *)(uintptr_t) gem_mmap.addr_ptr;
110 }
111
112 /**
113 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
114 */
115 void*
116 anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
117 uint64_t offset, uint64_t size, uint32_t flags)
118 {
119 if (device->physical->has_mmap_offset)
120 return anv_gem_mmap_offset(device, gem_handle, offset, size, flags);
121 else
122 return anv_gem_mmap_legacy(device, gem_handle, offset, size, flags);
123 }
124
125 /* This is just a wrapper around munmap, but it also notifies valgrind that
126 * this map is no longer valid. Pair this with anv_gem_mmap().
127 */
128 void
129 anv_gem_munmap(struct anv_device *device, void *p, uint64_t size)
130 {
131 if (!device->physical->has_mmap_offset)
132 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
133 munmap(p, size);
134 }
135
136 uint32_t
137 anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
138 {
139 struct drm_i915_gem_userptr userptr = {
140 .user_ptr = (__u64)((unsigned long) mem),
141 .user_size = size,
142 .flags = 0,
143 };
144
145 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
146 if (ret == -1)
147 return 0;
148
149 return userptr.handle;
150 }
151
152 int
153 anv_gem_set_caching(struct anv_device *device,
154 uint32_t gem_handle, uint32_t caching)
155 {
156 struct drm_i915_gem_caching gem_caching = {
157 .handle = gem_handle,
158 .caching = caching,
159 };
160
161 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
162 }
163
164 int
165 anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
166 uint32_t read_domains, uint32_t write_domain)
167 {
168 struct drm_i915_gem_set_domain gem_set_domain = {
169 .handle = gem_handle,
170 .read_domains = read_domains,
171 .write_domain = write_domain,
172 };
173
174 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &gem_set_domain);
175 }
176
177 /**
178 * Returns 0, 1, or negative to indicate error
179 */
180 int
181 anv_gem_busy(struct anv_device *device, uint32_t gem_handle)
182 {
183 struct drm_i915_gem_busy busy = {
184 .handle = gem_handle,
185 };
186
187 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
188 if (ret < 0)
189 return ret;
190
191 return busy.busy != 0;
192 }
193
194 /**
195 * On error, \a timeout_ns holds the remaining time.
196 */
197 int
198 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
199 {
200 struct drm_i915_gem_wait wait = {
201 .bo_handle = gem_handle,
202 .timeout_ns = *timeout_ns,
203 .flags = 0,
204 };
205
206 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
207 *timeout_ns = wait.timeout_ns;
208
209 return ret;
210 }
211
212 int
213 anv_gem_execbuffer(struct anv_device *device,
214 struct drm_i915_gem_execbuffer2 *execbuf)
215 {
216 if (execbuf->flags & I915_EXEC_FENCE_OUT)
217 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR, execbuf);
218 else
219 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
220 }
221
222 /** Return -1 on error. */
223 int
224 anv_gem_get_tiling(struct anv_device *device, uint32_t gem_handle)
225 {
226 struct drm_i915_gem_get_tiling get_tiling = {
227 .handle = gem_handle,
228 };
229
230 if (gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
231 assert(!"Failed to get BO tiling");
232 return -1;
233 }
234
235 return get_tiling.tiling_mode;
236 }
237
238 int
239 anv_gem_set_tiling(struct anv_device *device,
240 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
241 {
242 int ret;
243
244 /* set_tiling overwrites the input on the error path, so we have to open
245 * code gen_ioctl.
246 */
247 do {
248 struct drm_i915_gem_set_tiling set_tiling = {
249 .handle = gem_handle,
250 .tiling_mode = tiling,
251 .stride = stride,
252 };
253
254 ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
255 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
256
257 return ret;
258 }
259
260 int
261 anv_gem_get_param(int fd, uint32_t param)
262 {
263 int tmp;
264
265 drm_i915_getparam_t gp = {
266 .param = param,
267 .value = &tmp,
268 };
269
270 int ret = gen_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
271 if (ret == 0)
272 return tmp;
273
274 return 0;
275 }
276
277 bool
278 anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
279 {
280 struct drm_gem_close close;
281 int ret;
282
283 struct drm_i915_gem_create gem_create = {
284 .size = 4096,
285 };
286
287 if (gen_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create)) {
288 assert(!"Failed to create GEM BO");
289 return false;
290 }
291
292 bool swizzled = false;
293
294 /* set_tiling overwrites the input on the error path, so we have to open
295 * code gen_ioctl.
296 */
297 do {
298 struct drm_i915_gem_set_tiling set_tiling = {
299 .handle = gem_create.handle,
300 .tiling_mode = tiling,
301 .stride = tiling == I915_TILING_X ? 512 : 128,
302 };
303
304 ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
305 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
306
307 if (ret != 0) {
308 assert(!"Failed to set BO tiling");
309 goto close_and_return;
310 }
311
312 struct drm_i915_gem_get_tiling get_tiling = {
313 .handle = gem_create.handle,
314 };
315
316 if (gen_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
317 assert(!"Failed to get BO tiling");
318 goto close_and_return;
319 }
320
321 swizzled = get_tiling.swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
322
323 close_and_return:
324
325 memset(&close, 0, sizeof(close));
326 close.handle = gem_create.handle;
327 gen_ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
328
329 return swizzled;
330 }
331
332 bool
333 anv_gem_has_context_priority(int fd)
334 {
335 return !anv_gem_set_context_param(fd, 0, I915_CONTEXT_PARAM_PRIORITY,
336 GEN_CONTEXT_MEDIUM_PRIORITY);
337 }
338
339 int
340 anv_gem_create_context(struct anv_device *device)
341 {
342 struct drm_i915_gem_context_create create = { 0 };
343
344 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
345 if (ret == -1)
346 return -1;
347
348 return create.ctx_id;
349 }
350
351 int
352 anv_gem_destroy_context(struct anv_device *device, int context)
353 {
354 struct drm_i915_gem_context_destroy destroy = {
355 .ctx_id = context,
356 };
357
358 return gen_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
359 }
360
361 int
362 anv_gem_set_context_param(int fd, int context, uint32_t param, uint64_t value)
363 {
364 struct drm_i915_gem_context_param p = {
365 .ctx_id = context,
366 .param = param,
367 .value = value,
368 };
369 int err = 0;
370
371 if (gen_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &p))
372 err = -errno;
373 return err;
374 }
375
376 int
377 anv_gem_get_context_param(int fd, int context, uint32_t param, uint64_t *value)
378 {
379 struct drm_i915_gem_context_param gp = {
380 .ctx_id = context,
381 .param = param,
382 };
383
384 int ret = gen_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &gp);
385 if (ret == -1)
386 return -1;
387
388 *value = gp.value;
389 return 0;
390 }
391
392 int
393 anv_gem_gpu_get_reset_stats(struct anv_device *device,
394 uint32_t *active, uint32_t *pending)
395 {
396 struct drm_i915_reset_stats stats = {
397 .ctx_id = device->context_id,
398 };
399
400 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
401 if (ret == 0) {
402 *active = stats.batch_active;
403 *pending = stats.batch_pending;
404 }
405
406 return ret;
407 }
408
409 int
410 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
411 {
412 struct drm_prime_handle args = {
413 .handle = gem_handle,
414 .flags = DRM_CLOEXEC,
415 };
416
417 int ret = gen_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
418 if (ret == -1)
419 return -1;
420
421 return args.fd;
422 }
423
424 uint32_t
425 anv_gem_fd_to_handle(struct anv_device *device, int fd)
426 {
427 struct drm_prime_handle args = {
428 .fd = fd,
429 };
430
431 int ret = gen_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
432 if (ret == -1)
433 return 0;
434
435 return args.handle;
436 }
437
438 int
439 anv_gem_reg_read(struct anv_device *device, uint32_t offset, uint64_t *result)
440 {
441 struct drm_i915_reg_read args = {
442 .offset = offset
443 };
444
445 int ret = gen_ioctl(device->fd, DRM_IOCTL_I915_REG_READ, &args);
446
447 *result = args.val;
448 return ret;
449 }
450
451 int
452 anv_gem_sync_file_merge(struct anv_device *device, int fd1, int fd2)
453 {
454 struct sync_merge_data args = {
455 .name = "anv merge fence",
456 .fd2 = fd2,
457 .fence = -1,
458 };
459
460 int ret = gen_ioctl(fd1, SYNC_IOC_MERGE, &args);
461 if (ret == -1)
462 return -1;
463
464 return args.fence;
465 }
466
467 uint32_t
468 anv_gem_syncobj_create(struct anv_device *device, uint32_t flags)
469 {
470 struct drm_syncobj_create args = {
471 .flags = flags,
472 };
473
474 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
475 if (ret)
476 return 0;
477
478 return args.handle;
479 }
480
481 void
482 anv_gem_syncobj_destroy(struct anv_device *device, uint32_t handle)
483 {
484 struct drm_syncobj_destroy args = {
485 .handle = handle,
486 };
487
488 gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
489 }
490
491 int
492 anv_gem_syncobj_handle_to_fd(struct anv_device *device, uint32_t handle)
493 {
494 struct drm_syncobj_handle args = {
495 .handle = handle,
496 };
497
498 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
499 if (ret)
500 return -1;
501
502 return args.fd;
503 }
504
505 uint32_t
506 anv_gem_syncobj_fd_to_handle(struct anv_device *device, int fd)
507 {
508 struct drm_syncobj_handle args = {
509 .fd = fd,
510 };
511
512 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
513 if (ret)
514 return 0;
515
516 return args.handle;
517 }
518
519 int
520 anv_gem_syncobj_export_sync_file(struct anv_device *device, uint32_t handle)
521 {
522 struct drm_syncobj_handle args = {
523 .handle = handle,
524 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE,
525 };
526
527 int ret = gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
528 if (ret)
529 return -1;
530
531 return args.fd;
532 }
533
534 int
535 anv_gem_syncobj_import_sync_file(struct anv_device *device,
536 uint32_t handle, int fd)
537 {
538 struct drm_syncobj_handle args = {
539 .handle = handle,
540 .fd = fd,
541 .flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE,
542 };
543
544 return gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
545 }
546
547 void
548 anv_gem_syncobj_reset(struct anv_device *device, uint32_t handle)
549 {
550 struct drm_syncobj_array args = {
551 .handles = (uint64_t)(uintptr_t)&handle,
552 .count_handles = 1,
553 };
554
555 gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
556 }
557
558 bool
559 anv_gem_supports_syncobj_wait(int fd)
560 {
561 return gen_gem_supports_syncobj_wait(fd);
562 }
563
564 int
565 anv_gem_syncobj_wait(struct anv_device *device,
566 uint32_t *handles, uint32_t num_handles,
567 int64_t abs_timeout_ns, bool wait_all)
568 {
569 struct drm_syncobj_wait args = {
570 .handles = (uint64_t)(uintptr_t)handles,
571 .count_handles = num_handles,
572 .timeout_nsec = abs_timeout_ns,
573 .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
574 };
575
576 if (wait_all)
577 args.flags |= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL;
578
579 return gen_ioctl(device->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
580 }