OSDN Git Service

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