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
32 #include "anv_private.h"
33 #include "common/intel_gem.h"
34
35 #include "i915/anv_gem.h"
36
37 void *
anv_gem_mmap(struct anv_device * device,struct anv_bo * bo,uint64_t offset,uint64_t size)38 anv_gem_mmap(struct anv_device *device, struct anv_bo *bo, uint64_t offset,
39 uint64_t size)
40 {
41 void *map = device->kmd_backend->gem_mmap(device, bo, offset, size);
42
43 if (map != MAP_FAILED)
44 VG(VALGRIND_MALLOCLIKE_BLOCK(map, size, 0, 1));
45
46 return map;
47 }
48
49 /* This is just a wrapper around munmap, but it also notifies valgrind that
50 * this map is no longer valid. Pair this with gem_mmap().
51 */
52 void
anv_gem_munmap(struct anv_device * device,void * p,uint64_t size)53 anv_gem_munmap(struct anv_device *device, void *p, uint64_t size)
54 {
55 VG(VALGRIND_FREELIKE_BLOCK(p, 0));
56 munmap(p, size);
57 }
58
59 /**
60 * On error, \a timeout_ns holds the remaining time.
61 */
62 int
anv_gem_wait(struct anv_device * device,uint32_t gem_handle,int64_t * timeout_ns)63 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
64 {
65 switch (device->info->kmd_type) {
66 case INTEL_KMD_TYPE_I915:
67 return anv_i915_gem_wait(device, gem_handle, timeout_ns);
68 case INTEL_KMD_TYPE_XE:
69 return -1;
70 default:
71 unreachable("missing");
72 return -1;
73 }
74 }
75
76 /** Return -1 on error. */
77 int
anv_gem_get_tiling(struct anv_device * device,uint32_t gem_handle)78 anv_gem_get_tiling(struct anv_device *device, uint32_t gem_handle)
79 {
80 switch (device->info->kmd_type) {
81 case INTEL_KMD_TYPE_I915:
82 return anv_i915_gem_get_tiling(device, gem_handle);
83 case INTEL_KMD_TYPE_XE:
84 return -1;
85 default:
86 unreachable("missing");
87 return -1;
88 }
89 }
90
91 int
anv_gem_set_tiling(struct anv_device * device,uint32_t gem_handle,uint32_t stride,uint32_t tiling)92 anv_gem_set_tiling(struct anv_device *device,
93 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
94 {
95 switch (device->info->kmd_type) {
96 case INTEL_KMD_TYPE_I915:
97 return anv_i915_gem_set_tiling(device, gem_handle, stride, tiling);
98 case INTEL_KMD_TYPE_XE:
99 return 0;
100 default:
101 unreachable("missing");
102 return -1;
103 }
104 }
105
106 int
anv_gem_handle_to_fd(struct anv_device * device,uint32_t gem_handle)107 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
108 {
109 struct drm_prime_handle args = {
110 .handle = gem_handle,
111 .flags = DRM_CLOEXEC | DRM_RDWR,
112 };
113
114 int ret = intel_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
115 if (ret == -1)
116 return -1;
117
118 return args.fd;
119 }
120
121 uint32_t
anv_gem_fd_to_handle(struct anv_device * device,int fd)122 anv_gem_fd_to_handle(struct anv_device *device, int fd)
123 {
124 struct drm_prime_handle args = {
125 .fd = fd,
126 };
127
128 int ret = intel_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
129 if (ret == -1)
130 return 0;
131
132 return args.handle;
133 }
134
135 VkResult
anv_gem_import_bo_alloc_flags_to_bo_flags(struct anv_device * device,struct anv_bo * bo,enum anv_bo_alloc_flags alloc_flags,uint32_t * bo_flags)136 anv_gem_import_bo_alloc_flags_to_bo_flags(struct anv_device *device,
137 struct anv_bo *bo,
138 enum anv_bo_alloc_flags alloc_flags,
139 uint32_t *bo_flags)
140 {
141 switch (device->info->kmd_type) {
142 case INTEL_KMD_TYPE_I915:
143 return anv_i915_gem_import_bo_alloc_flags_to_bo_flags(device, bo,
144 alloc_flags,
145 bo_flags);
146 case INTEL_KMD_TYPE_XE:
147 *bo_flags = device->kmd_backend->bo_alloc_flags_to_bo_flags(device, alloc_flags);
148 return VK_SUCCESS;
149 default:
150 unreachable("missing");
151 return VK_ERROR_UNKNOWN;
152 }
153 }
154
anv_stub_kmd_backend_get(void)155 const struct anv_kmd_backend *anv_stub_kmd_backend_get(void)
156 {
157 return NULL;
158 }
159