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
28 #include <xf86drmMode.h>
30 #include "util/u_memory.h"
32 #include "vc4_context.h"
33 #include "vc4_screen.h"
36 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
38 struct vc4_bo
*bo
= CALLOC_STRUCT(vc4_bo
);
42 pipe_reference_init(&bo
->reference
, 1);
47 struct drm_mode_create_dumb create
;
48 memset(&create
, 0, sizeof(create
));
52 create
.height
= (size
+ 127) / 128;
54 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create
);
56 fprintf(stderr
, "create ioctl failure\n");
60 bo
->handle
= create
.handle
;
61 assert(create
.size
>= size
);
67 vc4_bo_free(struct vc4_bo
*bo
)
69 struct vc4_screen
*screen
= bo
->screen
;
72 #ifdef USE_VC4_SIMULATOR
73 if (bo
->simulator_winsys_map
) {
75 bo
->map
= bo
->simulator_winsys_map
;
78 munmap(bo
->map
, bo
->size
);
81 struct drm_gem_close c
;
82 memset(&c
, 0, sizeof(c
));
83 c
.handle
= bo
->handle
;
84 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
86 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
92 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
,
93 uint32_t winsys_stride
)
95 struct vc4_bo
*bo
= CALLOC_STRUCT(vc4_bo
);
97 struct drm_gem_open o
;
99 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
101 fprintf(stderr
, "Failed to open bo %d: %s\n",
102 name
, strerror(errno
));
107 pipe_reference_init(&bo
->reference
, 1);
109 bo
->handle
= o
.handle
;
113 #ifdef USE_VC4_SIMULATOR
115 bo
->simulator_winsys_map
= bo
->map
;
116 bo
->simulator_winsys_stride
= winsys_stride
;
117 bo
->map
= malloc(bo
->size
);
124 vc4_bo_alloc_mem(struct vc4_screen
*screen
, const void *data
, uint32_t size
,
130 bo
= vc4_bo_alloc(screen
, size
, name
);
131 map
= vc4_bo_map(bo
);
132 memcpy(map
, data
, size
);
137 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
139 struct drm_gem_flink flink
= {
140 .handle
= bo
->handle
,
142 int ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
144 fprintf(stderr
, "Failed to flink bo %d: %s\n",
145 bo
->handle
, strerror(errno
));
156 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
)
158 #ifndef USE_VC4_SIMULATOR
159 struct drm_vc4_wait_seqno wait
;
160 memset(&wait
, 0, sizeof(wait
));
162 wait
.timeout_ns
= timeout_ns
;
164 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
167 } else if (ret
!= 0) {
168 fprintf(stderr
, "wait failed\n");
171 screen
->finished_seqno
= wait
.seqno
;
180 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
)
182 #ifndef USE_VC4_SIMULATOR
183 struct vc4_screen
*screen
= bo
->screen
;
185 struct drm_vc4_wait_bo wait
;
186 memset(&wait
, 0, sizeof(wait
));
187 wait
.handle
= bo
->handle
;
188 wait
.timeout_ns
= timeout_ns
;
190 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
193 } else if (ret
!= 0) {
194 fprintf(stderr
, "wait failed\n");
205 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
212 struct drm_mode_map_dumb map
;
213 memset(&map
, 0, sizeof(map
));
214 map
.handle
= bo
->handle
;
215 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map
);
217 fprintf(stderr
, "map ioctl failure\n");
221 bo
->map
= mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
222 bo
->screen
->fd
, map
.offset
);
223 if (bo
->map
== MAP_FAILED
) {
224 fprintf(stderr
, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
225 bo
->handle
, (long long)map
.offset
, bo
->size
);
233 vc4_bo_map(struct vc4_bo
*bo
)
235 void *map
= vc4_bo_map_unsynchronized(bo
);
237 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
);
239 fprintf(stderr
, "BO wait for map failed\n");