2 * Copyright © 2014 Broadcom
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:
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
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
29 #include <xf86drmMode.h>
31 #include "util/u_memory.h"
33 #include "vc4_context.h"
34 #include "vc4_screen.h"
37 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
39 struct vc4_bo
*bo
= CALLOC_STRUCT(vc4_bo
);
43 pipe_reference_init(&bo
->reference
, 1);
48 struct drm_mode_create_dumb create
;
49 memset(&create
, 0, sizeof(create
));
53 create
.height
= (size
+ 127) / 128;
55 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create
);
57 fprintf(stderr
, "create ioctl failure\n");
61 bo
->handle
= create
.handle
;
62 assert(create
.size
>= size
);
68 vc4_bo_free(struct vc4_bo
*bo
)
70 struct vc4_screen
*screen
= bo
->screen
;
73 #ifdef USE_VC4_SIMULATOR
74 if (bo
->simulator_winsys_map
) {
76 bo
->map
= bo
->simulator_winsys_map
;
79 munmap(bo
->map
, bo
->size
);
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
);
87 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
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
)
97 struct vc4_bo
*bo
= CALLOC_STRUCT(vc4_bo
);
101 pipe_reference_init(&bo
->reference
, 1);
107 #ifdef USE_VC4_SIMULATOR
109 bo
->simulator_winsys_map
= bo
->map
;
110 bo
->simulator_winsys_stride
= winsys_stride
;
111 bo
->map
= malloc(bo
->size
);
118 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
,
119 uint32_t winsys_stride
)
121 struct drm_gem_open o
= {
124 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
126 fprintf(stderr
, "Failed to open bo %d: %s\n",
127 name
, strerror(errno
));
131 return vc4_bo_open_handle(screen
, winsys_stride
, o
.handle
, o
.size
);
135 vc4_bo_open_dmabuf(struct vc4_screen
*screen
, int fd
, uint32_t winsys_stride
)
138 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
141 fprintf(stderr
, "Failed to get vc4 handle for dmabuf %d\n", fd
);
145 /* Determine the size of the bo we were handed. */
146 size
= lseek(fd
, 0, SEEK_END
);
148 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
152 return vc4_bo_open_handle(screen
, winsys_stride
, handle
, size
);
156 vc4_bo_get_dmabuf(struct vc4_bo
*bo
)
159 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
162 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
171 vc4_bo_alloc_mem(struct vc4_screen
*screen
, const void *data
, uint32_t size
,
177 bo
= vc4_bo_alloc(screen
, size
, name
);
178 map
= vc4_bo_map(bo
);
179 memcpy(map
, data
, size
);
184 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
186 struct drm_gem_flink flink
= {
187 .handle
= bo
->handle
,
189 int ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
191 fprintf(stderr
, "Failed to flink bo %d: %s\n",
192 bo
->handle
, strerror(errno
));
203 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
)
205 #ifndef USE_VC4_SIMULATOR
206 struct drm_vc4_wait_seqno wait
;
207 memset(&wait
, 0, sizeof(wait
));
209 wait
.timeout_ns
= timeout_ns
;
211 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
214 } else if (ret
!= 0) {
215 fprintf(stderr
, "wait failed\n");
218 screen
->finished_seqno
= wait
.seqno
;
227 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
)
229 #ifndef USE_VC4_SIMULATOR
230 struct vc4_screen
*screen
= bo
->screen
;
232 struct drm_vc4_wait_bo wait
;
233 memset(&wait
, 0, sizeof(wait
));
234 wait
.handle
= bo
->handle
;
235 wait
.timeout_ns
= timeout_ns
;
237 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
240 } else if (ret
!= 0) {
241 fprintf(stderr
, "wait failed\n");
252 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
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
);
264 fprintf(stderr
, "map ioctl failure\n");
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
);
280 vc4_bo_map(struct vc4_bo
*bo
)
282 void *map
= vc4_bo_map_unsynchronized(bo
);
284 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
);
286 fprintf(stderr
, "BO wait for map failed\n");