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"
32 #include "util/ralloc.h"
34 #include "vc4_context.h"
35 #include "vc4_screen.h"
37 #define container_of(ptr, type, field) \
38 (type*)((char*)ptr - offsetof(type, field))
40 static struct vc4_bo
*
41 vc4_bo_from_cache(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
43 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
44 uint32_t page_index
= size
/ 4096 - 1;
46 if (cache
->size_list_size
<= page_index
)
49 struct vc4_bo
*bo
= NULL
;
50 pipe_mutex_lock(cache
->lock
);
51 if (!is_empty_list(&cache
->size_list
[page_index
])) {
52 struct simple_node
*node
= first_elem(&cache
->size_list
[page_index
]);
53 bo
= container_of(node
, struct vc4_bo
, size_list
);
55 /* Check that the BO has gone idle. If not, then we want to
56 * allocate something new instead, since we assume that the
57 * user will proceed to CPU map it and fill it with stuff.
59 if (!vc4_bo_wait(bo
, 0)) {
60 pipe_mutex_unlock(cache
->lock
);
64 pipe_reference_init(&bo
->reference
, 1);
65 remove_from_list(&bo
->time_list
);
66 remove_from_list(&bo
->size_list
);
70 pipe_mutex_unlock(cache
->lock
);
75 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
80 size
= align(size
, 4096);
82 bo
= vc4_bo_from_cache(screen
, size
, name
);
86 bo
= CALLOC_STRUCT(vc4_bo
);
90 pipe_reference_init(&bo
->reference
, 1);
96 if (!using_vc4_simulator
) {
97 struct drm_vc4_create_bo create
;
98 memset(&create
, 0, sizeof(create
));
102 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_BO
, &create
);
103 bo
->handle
= create
.handle
;
105 struct drm_mode_create_dumb create
;
106 memset(&create
, 0, sizeof(create
));
110 create
.height
= (size
+ 127) / 128;
112 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create
);
113 bo
->handle
= create
.handle
;
114 assert(create
.size
>= size
);
117 fprintf(stderr
, "create ioctl failure\n");
125 vc4_bo_last_unreference(struct vc4_bo
*bo
)
127 struct vc4_screen
*screen
= bo
->screen
;
129 struct timespec time
;
130 clock_gettime(CLOCK_MONOTONIC
, &time
);
131 pipe_mutex_lock(screen
->bo_cache
.lock
);
132 vc4_bo_last_unreference_locked_timed(bo
, time
.tv_sec
);
133 pipe_mutex_unlock(screen
->bo_cache
.lock
);
137 vc4_bo_free(struct vc4_bo
*bo
)
139 struct vc4_screen
*screen
= bo
->screen
;
142 #ifdef USE_VC4_SIMULATOR
143 if (bo
->simulator_winsys_map
) {
145 bo
->map
= bo
->simulator_winsys_map
;
148 munmap(bo
->map
, bo
->size
);
151 struct drm_gem_close c
;
152 memset(&c
, 0, sizeof(c
));
153 c
.handle
= bo
->handle
;
154 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
156 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
162 free_stale_bos(struct vc4_screen
*screen
, time_t time
)
164 while (!is_empty_list(&screen
->bo_cache
.time_list
)) {
165 struct simple_node
*node
=
166 first_elem(&screen
->bo_cache
.time_list
);
167 struct vc4_bo
*bo
= container_of(node
, struct vc4_bo
, time_list
);
169 /* If it's more than a second old, free it. */
170 if (time
- bo
->free_time
> 2) {
171 remove_from_list(&bo
->time_list
);
172 remove_from_list(&bo
->size_list
);
181 vc4_bo_last_unreference_locked_timed(struct vc4_bo
*bo
, time_t time
)
183 struct vc4_screen
*screen
= bo
->screen
;
184 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
185 uint32_t page_index
= bo
->size
/ 4096 - 1;
192 if (cache
->size_list_size
<= page_index
) {
193 struct simple_node
*new_list
=
194 ralloc_array(screen
, struct simple_node
, page_index
+ 1);
196 /* Move old list contents over (since the array has moved, and
197 * therefore the pointers to the list heads have to change.
199 for (int i
= 0; i
< cache
->size_list_size
; i
++) {
200 struct simple_node
*old_head
= &cache
->size_list
[i
];
201 if (is_empty_list(old_head
))
202 make_empty_list(&new_list
[i
]);
204 new_list
[i
].next
= old_head
->next
;
205 new_list
[i
].prev
= old_head
->prev
;
206 new_list
[i
].next
->prev
= &new_list
[i
];
207 new_list
[i
].prev
->next
= &new_list
[i
];
210 for (int i
= cache
->size_list_size
; i
< page_index
+ 1; i
++)
211 make_empty_list(&new_list
[i
]);
213 cache
->size_list
= new_list
;
214 cache
->size_list_size
= page_index
+ 1;
217 bo
->free_time
= time
;
218 insert_at_tail(&cache
->size_list
[page_index
], &bo
->size_list
);
219 insert_at_tail(&cache
->time_list
, &bo
->time_list
);
221 free_stale_bos(screen
, time
);
224 static struct vc4_bo
*
225 vc4_bo_open_handle(struct vc4_screen
*screen
,
226 uint32_t winsys_stride
,
227 uint32_t handle
, uint32_t size
)
229 struct vc4_bo
*bo
= CALLOC_STRUCT(vc4_bo
);
233 pipe_reference_init(&bo
->reference
, 1);
240 #ifdef USE_VC4_SIMULATOR
242 bo
->simulator_winsys_map
= bo
->map
;
243 bo
->simulator_winsys_stride
= winsys_stride
;
244 bo
->map
= malloc(bo
->size
);
251 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
,
252 uint32_t winsys_stride
)
254 struct drm_gem_open o
= {
257 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
259 fprintf(stderr
, "Failed to open bo %d: %s\n",
260 name
, strerror(errno
));
264 return vc4_bo_open_handle(screen
, winsys_stride
, o
.handle
, o
.size
);
268 vc4_bo_open_dmabuf(struct vc4_screen
*screen
, int fd
, uint32_t winsys_stride
)
271 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
274 fprintf(stderr
, "Failed to get vc4 handle for dmabuf %d\n", fd
);
278 /* Determine the size of the bo we were handed. */
279 size
= lseek(fd
, 0, SEEK_END
);
281 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
285 return vc4_bo_open_handle(screen
, winsys_stride
, handle
, size
);
289 vc4_bo_get_dmabuf(struct vc4_bo
*bo
)
292 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
295 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
305 vc4_bo_alloc_mem(struct vc4_screen
*screen
, const void *data
, uint32_t size
,
311 bo
= vc4_bo_alloc(screen
, size
, name
);
312 map
= vc4_bo_map(bo
);
313 memcpy(map
, data
, size
);
318 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
320 struct drm_gem_flink flink
= {
321 .handle
= bo
->handle
,
323 int ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
325 fprintf(stderr
, "Failed to flink bo %d: %s\n",
326 bo
->handle
, strerror(errno
));
338 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
)
340 if (screen
->finished_seqno
>= seqno
)
343 struct drm_vc4_wait_seqno wait
;
344 memset(&wait
, 0, sizeof(wait
));
346 wait
.timeout_ns
= timeout_ns
;
349 if (!using_vc4_simulator
)
350 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
352 wait
.seqno
= screen
->finished_seqno
;
357 screen
->finished_seqno
= wait
.seqno
;
361 if (errno
!= ETIME
) {
362 fprintf(stderr
, "wait failed: %d\n", ret
);
370 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
)
372 struct vc4_screen
*screen
= bo
->screen
;
374 struct drm_vc4_wait_bo wait
;
375 memset(&wait
, 0, sizeof(wait
));
376 wait
.handle
= bo
->handle
;
377 wait
.timeout_ns
= timeout_ns
;
380 if (!using_vc4_simulator
)
381 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
388 if (errno
!= ETIME
) {
389 fprintf(stderr
, "wait failed: %d\n", ret
);
397 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
405 if (!using_vc4_simulator
) {
406 struct drm_vc4_mmap_bo map
;
407 memset(&map
, 0, sizeof(map
));
408 map
.handle
= bo
->handle
;
409 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_VC4_MMAP_BO
, &map
);
412 struct drm_mode_map_dumb map
;
413 memset(&map
, 0, sizeof(map
));
414 map
.handle
= bo
->handle
;
415 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map
);
419 fprintf(stderr
, "map ioctl failure\n");
423 bo
->map
= mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
424 bo
->screen
->fd
, offset
);
425 if (bo
->map
== MAP_FAILED
) {
426 fprintf(stderr
, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
427 bo
->handle
, (long long)offset
, bo
->size
);
435 vc4_bo_map(struct vc4_bo
*bo
)
437 void *map
= vc4_bo_map_unsynchronized(bo
);
439 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
);
441 fprintf(stderr
, "BO wait for map failed\n");
449 vc4_bufmgr_destroy(struct pipe_screen
*pscreen
)
451 struct vc4_screen
*screen
= vc4_screen(pscreen
);
452 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
454 while (!is_empty_list(&cache
->time_list
)) {
455 struct simple_node
*node
= first_elem(&cache
->time_list
);
456 struct vc4_bo
*bo
= container_of(node
, struct vc4_bo
, time_list
);
458 remove_from_list(&bo
->time_list
);
459 remove_from_list(&bo
->size_list
);