64fe2e40e42cf96d53391e38464c51b91f6b2373
[mesa.git] / src / gallium / drivers / vc4 / vc4_bufmgr.c
1 /*
2 * Copyright © 2014 Broadcom
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 <errno.h>
25 #include <err.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28 #include <xf86drm.h>
29 #include <xf86drmMode.h>
30
31 #include "util/u_memory.h"
32
33 #include "vc4_context.h"
34 #include "vc4_screen.h"
35
36 struct vc4_bo *
37 vc4_bo_alloc(struct vc4_screen *screen, uint32_t size, const char *name)
38 {
39 struct vc4_bo *bo = CALLOC_STRUCT(vc4_bo);
40 if (!bo)
41 return NULL;
42
43 pipe_reference_init(&bo->reference, 1);
44 bo->screen = screen;
45 bo->size = size;
46 bo->name = name;
47
48 struct drm_mode_create_dumb create;
49 memset(&create, 0, sizeof(create));
50
51 create.width = 128;
52 create.bpp = 8;
53 create.height = (size + 127) / 128;
54
55 int ret = drmIoctl(screen->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
56 if (ret != 0) {
57 fprintf(stderr, "create ioctl failure\n");
58 abort();
59 }
60
61 bo->handle = create.handle;
62 assert(create.size >= size);
63
64 return bo;
65 }
66
67 void
68 vc4_bo_free(struct vc4_bo *bo)
69 {
70 struct vc4_screen *screen = bo->screen;
71
72 if (bo->map) {
73 #ifdef USE_VC4_SIMULATOR
74 if (bo->simulator_winsys_map) {
75 free(bo->map);
76 bo->map = bo->simulator_winsys_map;
77 }
78 #endif
79 munmap(bo->map, bo->size);
80 }
81
82 struct drm_gem_close c;
83 memset(&c, 0, sizeof(c));
84 c.handle = bo->handle;
85 int ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &c);
86 if (ret != 0)
87 fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno));
88
89 free(bo);
90 }
91
92 static struct vc4_bo *
93 vc4_bo_open_handle(struct vc4_screen *screen,
94 uint32_t winsys_stride,
95 uint32_t handle, uint32_t size)
96 {
97 struct vc4_bo *bo = CALLOC_STRUCT(vc4_bo);
98
99 assert(size);
100
101 pipe_reference_init(&bo->reference, 1);
102 bo->screen = screen;
103 bo->handle = handle;
104 bo->size = size;
105 bo->name = "winsys";
106
107 #ifdef USE_VC4_SIMULATOR
108 vc4_bo_map(bo);
109 bo->simulator_winsys_map = bo->map;
110 bo->simulator_winsys_stride = winsys_stride;
111 bo->map = malloc(bo->size);
112 #endif
113
114 return bo;
115 }
116
117 struct vc4_bo *
118 vc4_bo_open_name(struct vc4_screen *screen, uint32_t name,
119 uint32_t winsys_stride)
120 {
121 struct drm_gem_open o = {
122 .name = name
123 };
124 int ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_OPEN, &o);
125 if (ret) {
126 fprintf(stderr, "Failed to open bo %d: %s\n",
127 name, strerror(errno));
128 return NULL;
129 }
130
131 return vc4_bo_open_handle(screen, winsys_stride, o.handle, o.size);
132 }
133
134 struct vc4_bo *
135 vc4_bo_open_dmabuf(struct vc4_screen *screen, int fd, uint32_t winsys_stride)
136 {
137 uint32_t handle;
138 int ret = drmPrimeFDToHandle(screen->fd, fd, &handle);
139 int size;
140 if (ret) {
141 fprintf(stderr, "Failed to get vc4 handle for dmabuf %d\n", fd);
142 return NULL;
143 }
144
145 /* Determine the size of the bo we were handed. */
146 size = lseek(fd, 0, SEEK_END);
147 if (size == -1) {
148 fprintf(stderr, "Couldn't get size of dmabuf fd %d.\n", fd);
149 return NULL;
150 }
151
152 return vc4_bo_open_handle(screen, winsys_stride, handle, size);
153 }
154
155 int
156 vc4_bo_get_dmabuf(struct vc4_bo *bo)
157 {
158 int fd;
159 int ret = drmPrimeHandleToFD(bo->screen->fd, bo->handle,
160 O_CLOEXEC, &fd);
161 if (ret != 0) {
162 fprintf(stderr, "Failed to export gem bo %d to dmabuf\n",
163 bo->handle);
164 return -1;
165 }
166
167 return fd;
168 }
169
170 struct vc4_bo *
171 vc4_bo_alloc_mem(struct vc4_screen *screen, const void *data, uint32_t size,
172 const char *name)
173 {
174 void *map;
175 struct vc4_bo *bo;
176
177 bo = vc4_bo_alloc(screen, size, name);
178 map = vc4_bo_map(bo);
179 memcpy(map, data, size);
180 return bo;
181 }
182
183 bool
184 vc4_bo_flink(struct vc4_bo *bo, uint32_t *name)
185 {
186 struct drm_gem_flink flink = {
187 .handle = bo->handle,
188 };
189 int ret = drmIoctl(bo->screen->fd, DRM_IOCTL_GEM_FLINK, &flink);
190 if (ret) {
191 fprintf(stderr, "Failed to flink bo %d: %s\n",
192 bo->handle, strerror(errno));
193 free(bo);
194 return false;
195 }
196
197 *name = flink.name;
198
199 return true;
200 }
201
202 bool
203 vc4_wait_seqno(struct vc4_screen *screen, uint64_t seqno, uint64_t timeout_ns)
204 {
205 #ifndef USE_VC4_SIMULATOR
206 struct drm_vc4_wait_seqno wait;
207 memset(&wait, 0, sizeof(wait));
208 wait.seqno = seqno;
209 wait.timeout_ns = timeout_ns;
210
211 int ret = drmIoctl(screen->fd, DRM_IOCTL_VC4_WAIT_SEQNO, &wait);
212 if (ret == -ETIME) {
213 return false;
214 } else if (ret != 0) {
215 fprintf(stderr, "wait failed\n");
216 abort();
217 } else {
218 screen->finished_seqno = wait.seqno;
219 return true;
220 }
221 #else
222 return true;
223 #endif
224 }
225
226 bool
227 vc4_bo_wait(struct vc4_bo *bo, uint64_t timeout_ns)
228 {
229 #ifndef USE_VC4_SIMULATOR
230 struct vc4_screen *screen = bo->screen;
231
232 struct drm_vc4_wait_bo wait;
233 memset(&wait, 0, sizeof(wait));
234 wait.handle = bo->handle;
235 wait.timeout_ns = timeout_ns;
236
237 int ret = drmIoctl(screen->fd, DRM_IOCTL_VC4_WAIT_BO, &wait);
238 if (ret == -ETIME) {
239 return false;
240 } else if (ret != 0) {
241 fprintf(stderr, "wait failed\n");
242 abort();
243 } else {
244 return true;
245 }
246 #else
247 return true;
248 #endif
249 }
250
251 void *
252 vc4_bo_map_unsynchronized(struct vc4_bo *bo)
253 {
254 int ret;
255
256 if (bo->map)
257 return bo->map;
258
259 struct drm_mode_map_dumb map;
260 memset(&map, 0, sizeof(map));
261 map.handle = bo->handle;
262 ret = drmIoctl(bo->screen->fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
263 if (ret != 0) {
264 fprintf(stderr, "map ioctl failure\n");
265 abort();
266 }
267
268 bo->map = mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
269 bo->screen->fd, map.offset);
270 if (bo->map == MAP_FAILED) {
271 fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
272 bo->handle, (long long)map.offset, bo->size);
273 abort();
274 }
275
276 return bo->map;
277 }
278
279 void *
280 vc4_bo_map(struct vc4_bo *bo)
281 {
282 void *map = vc4_bo_map_unsynchronized(bo);
283
284 bool ok = vc4_bo_wait(bo, PIPE_TIMEOUT_INFINITE);
285 if (!ok) {
286 fprintf(stderr, "BO wait for map failed\n");
287 abort();
288 }
289
290 return map;
291 }