2 * Copyright © 2014-2015 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_hash_table.h"
32 #include "util/u_memory.h"
33 #include "util/ralloc.h"
35 #include "vc4_context.h"
36 #include "vc4_screen.h"
46 static bool dump_stats
= false;
49 vc4_bo_cache_free_all(struct vc4_bo_cache
*cache
);
52 vc4_bo_dump_stats(struct vc4_screen
*screen
)
54 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
56 fprintf(stderr
, " BOs allocated: %d\n", screen
->bo_count
);
57 fprintf(stderr
, " BOs size: %dkb\n", screen
->bo_size
/ 1024);
58 fprintf(stderr
, " BOs cached: %d\n", cache
->bo_count
);
59 fprintf(stderr
, " BOs cached size: %dkb\n", cache
->bo_size
/ 1024);
61 if (!list_empty(&cache
->time_list
)) {
62 struct vc4_bo
*first
= LIST_ENTRY(struct vc4_bo
,
63 cache
->time_list
.next
,
65 struct vc4_bo
*last
= LIST_ENTRY(struct vc4_bo
,
66 cache
->time_list
.prev
,
69 fprintf(stderr
, " oldest cache time: %ld\n",
70 (long)first
->free_time
);
71 fprintf(stderr
, " newest cache time: %ld\n",
72 (long)last
->free_time
);
75 clock_gettime(CLOCK_MONOTONIC
, &time
);
76 fprintf(stderr
, " now: %ld\n",
82 vc4_bo_remove_from_cache(struct vc4_bo_cache
*cache
, struct vc4_bo
*bo
)
84 list_del(&bo
->time_list
);
85 list_del(&bo
->size_list
);
87 cache
->bo_size
-= bo
->size
;
90 static struct vc4_bo
*
91 vc4_bo_from_cache(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
93 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
94 uint32_t page_index
= size
/ 4096 - 1;
96 if (cache
->size_list_size
<= page_index
)
99 struct vc4_bo
*bo
= NULL
;
100 mtx_lock(&cache
->lock
);
101 if (!list_empty(&cache
->size_list
[page_index
])) {
102 bo
= LIST_ENTRY(struct vc4_bo
, cache
->size_list
[page_index
].next
,
105 /* Check that the BO has gone idle. If not, then we want to
106 * allocate something new instead, since we assume that the
107 * user will proceed to CPU map it and fill it with stuff.
109 if (!vc4_bo_wait(bo
, 0, NULL
)) {
110 mtx_unlock(&cache
->lock
);
114 pipe_reference_init(&bo
->reference
, 1);
115 vc4_bo_remove_from_cache(cache
, bo
);
119 mtx_unlock(&cache
->lock
);
124 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
129 size
= align(size
, 4096);
131 bo
= vc4_bo_from_cache(screen
, size
, name
);
134 fprintf(stderr
, "Allocated %s %dkb from cache:\n",
136 vc4_bo_dump_stats(screen
);
141 bo
= CALLOC_STRUCT(vc4_bo
);
145 pipe_reference_init(&bo
->reference
, 1);
154 bool cleared_and_retried
= false;
155 struct drm_vc4_create_bo create
= {
159 ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_BO
, &create
);
160 bo
->handle
= create
.handle
;
163 if (!list_empty(&screen
->bo_cache
.time_list
) &&
164 !cleared_and_retried
) {
165 cleared_and_retried
= true;
166 vc4_bo_cache_free_all(&screen
->bo_cache
);
175 screen
->bo_size
+= bo
->size
;
177 fprintf(stderr
, "Allocated %s %dkb:\n", name
, size
/ 1024);
178 vc4_bo_dump_stats(screen
);
185 vc4_bo_last_unreference(struct vc4_bo
*bo
)
187 struct vc4_screen
*screen
= bo
->screen
;
189 struct timespec time
;
190 clock_gettime(CLOCK_MONOTONIC
, &time
);
191 mtx_lock(&screen
->bo_cache
.lock
);
192 vc4_bo_last_unreference_locked_timed(bo
, time
.tv_sec
);
193 mtx_unlock(&screen
->bo_cache
.lock
);
197 vc4_bo_free(struct vc4_bo
*bo
)
199 struct vc4_screen
*screen
= bo
->screen
;
202 if (using_vc4_simulator
&& bo
->name
&&
203 strcmp(bo
->name
, "winsys") == 0) {
206 munmap(bo
->map
, bo
->size
);
207 VG(VALGRIND_FREELIKE_BLOCK(bo
->map
, 0));
211 struct drm_gem_close c
;
212 memset(&c
, 0, sizeof(c
));
213 c
.handle
= bo
->handle
;
214 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
216 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
219 screen
->bo_size
-= bo
->size
;
222 fprintf(stderr
, "Freed %s%s%dkb:\n",
223 bo
->name
? bo
->name
: "",
226 vc4_bo_dump_stats(screen
);
233 free_stale_bos(struct vc4_screen
*screen
, time_t time
)
235 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
236 bool freed_any
= false;
238 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
240 if (dump_stats
&& !freed_any
) {
241 fprintf(stderr
, "Freeing stale BOs:\n");
242 vc4_bo_dump_stats(screen
);
246 /* If it's more than a second old, free it. */
247 if (time
- bo
->free_time
> 2) {
248 vc4_bo_remove_from_cache(cache
, bo
);
255 if (dump_stats
&& freed_any
) {
256 fprintf(stderr
, "Freed stale BOs:\n");
257 vc4_bo_dump_stats(screen
);
262 vc4_bo_cache_free_all(struct vc4_bo_cache
*cache
)
264 mtx_lock(&cache
->lock
);
265 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
267 vc4_bo_remove_from_cache(cache
, bo
);
270 mtx_unlock(&cache
->lock
);
274 vc4_bo_last_unreference_locked_timed(struct vc4_bo
*bo
, time_t time
)
276 struct vc4_screen
*screen
= bo
->screen
;
277 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
278 uint32_t page_index
= bo
->size
/ 4096 - 1;
285 if (cache
->size_list_size
<= page_index
) {
286 struct list_head
*new_list
=
287 ralloc_array(screen
, struct list_head
, page_index
+ 1);
289 /* Move old list contents over (since the array has moved, and
290 * therefore the pointers to the list heads have to change).
292 for (int i
= 0; i
< cache
->size_list_size
; i
++)
293 list_replace(&cache
->size_list
[i
], &new_list
[i
]);
294 for (int i
= cache
->size_list_size
; i
< page_index
+ 1; i
++)
295 list_inithead(&new_list
[i
]);
297 cache
->size_list
= new_list
;
298 cache
->size_list_size
= page_index
+ 1;
301 bo
->free_time
= time
;
302 list_addtail(&bo
->size_list
, &cache
->size_list
[page_index
]);
303 list_addtail(&bo
->time_list
, &cache
->time_list
);
305 cache
->bo_size
+= bo
->size
;
307 fprintf(stderr
, "Freed %s %dkb to cache:\n",
308 bo
->name
, bo
->size
/ 1024);
309 vc4_bo_dump_stats(screen
);
313 free_stale_bos(screen
, time
);
316 static struct vc4_bo
*
317 vc4_bo_open_handle(struct vc4_screen
*screen
,
318 uint32_t winsys_stride
,
319 uint32_t handle
, uint32_t size
)
325 mtx_lock(&screen
->bo_handles_mutex
);
327 bo
= util_hash_table_get(screen
->bo_handles
, (void*)(uintptr_t)handle
);
329 pipe_reference(NULL
, &bo
->reference
);
333 bo
= CALLOC_STRUCT(vc4_bo
);
334 pipe_reference_init(&bo
->reference
, 1);
341 #ifdef USE_VC4_SIMULATOR
342 vc4_simulator_open_from_handle(screen
->fd
, winsys_stride
,
343 bo
->handle
, bo
->size
);
344 bo
->map
= malloc(bo
->size
);
347 util_hash_table_set(screen
->bo_handles
, (void *)(uintptr_t)handle
, bo
);
350 mtx_unlock(&screen
->bo_handles_mutex
);
355 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
,
356 uint32_t winsys_stride
)
358 struct drm_gem_open o
= {
361 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
363 fprintf(stderr
, "Failed to open bo %d: %s\n",
364 name
, strerror(errno
));
368 return vc4_bo_open_handle(screen
, winsys_stride
, o
.handle
, o
.size
);
372 vc4_bo_open_dmabuf(struct vc4_screen
*screen
, int fd
, uint32_t winsys_stride
)
375 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
378 fprintf(stderr
, "Failed to get vc4 handle for dmabuf %d\n", fd
);
382 /* Determine the size of the bo we were handed. */
383 size
= lseek(fd
, 0, SEEK_END
);
385 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
389 return vc4_bo_open_handle(screen
, winsys_stride
, handle
, size
);
393 vc4_bo_get_dmabuf(struct vc4_bo
*bo
)
396 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
399 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
404 mtx_lock(&bo
->screen
->bo_handles_mutex
);
406 util_hash_table_set(bo
->screen
->bo_handles
, (void *)(uintptr_t)bo
->handle
, bo
);
407 mtx_unlock(&bo
->screen
->bo_handles_mutex
);
413 vc4_bo_alloc_shader(struct vc4_screen
*screen
, const void *data
, uint32_t size
)
418 bo
= CALLOC_STRUCT(vc4_bo
);
422 pipe_reference_init(&bo
->reference
, 1);
424 bo
->size
= align(size
, 4096);
426 bo
->private = false; /* Make sure it doesn't go back to the cache. */
428 struct drm_vc4_create_shader_bo create
= {
430 .data
= (uintptr_t)data
,
433 ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_SHADER_BO
,
435 bo
->handle
= create
.handle
;
438 fprintf(stderr
, "create shader ioctl failure\n");
443 screen
->bo_size
+= bo
->size
;
445 fprintf(stderr
, "Allocated shader %dkb:\n", bo
->size
/ 1024);
446 vc4_bo_dump_stats(screen
);
453 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
455 struct drm_gem_flink flink
= {
456 .handle
= bo
->handle
,
458 int ret
= vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
460 fprintf(stderr
, "Failed to flink bo %d: %s\n",
461 bo
->handle
, strerror(errno
));
472 static int vc4_wait_seqno_ioctl(int fd
, uint64_t seqno
, uint64_t timeout_ns
)
474 struct drm_vc4_wait_seqno wait
= {
476 .timeout_ns
= timeout_ns
,
478 int ret
= vc4_ioctl(fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
487 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
,
490 if (screen
->finished_seqno
>= seqno
)
493 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
494 if (vc4_wait_seqno_ioctl(screen
->fd
, seqno
, 0) == -ETIME
) {
495 fprintf(stderr
, "Blocking on seqno %lld for %s\n",
496 (long long)seqno
, reason
);
500 int ret
= vc4_wait_seqno_ioctl(screen
->fd
, seqno
, timeout_ns
);
503 fprintf(stderr
, "wait failed: %d\n", ret
);
510 screen
->finished_seqno
= seqno
;
514 static int vc4_wait_bo_ioctl(int fd
, uint32_t handle
, uint64_t timeout_ns
)
516 struct drm_vc4_wait_bo wait
= {
518 .timeout_ns
= timeout_ns
,
520 int ret
= vc4_ioctl(fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
529 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
, const char *reason
)
531 struct vc4_screen
*screen
= bo
->screen
;
533 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
534 if (vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, 0) == -ETIME
) {
535 fprintf(stderr
, "Blocking on %s BO for %s\n",
540 int ret
= vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, timeout_ns
);
543 fprintf(stderr
, "wait failed: %d\n", ret
);
554 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
562 struct drm_vc4_mmap_bo map
;
563 memset(&map
, 0, sizeof(map
));
564 map
.handle
= bo
->handle
;
565 ret
= vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_MMAP_BO
, &map
);
568 fprintf(stderr
, "map ioctl failure\n");
572 bo
->map
= mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
573 bo
->screen
->fd
, offset
);
574 if (bo
->map
== MAP_FAILED
) {
575 fprintf(stderr
, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
576 bo
->handle
, (long long)offset
, bo
->size
);
579 VG(VALGRIND_MALLOCLIKE_BLOCK(bo
->map
, bo
->size
, 0, false));
585 vc4_bo_map(struct vc4_bo
*bo
)
587 void *map
= vc4_bo_map_unsynchronized(bo
);
589 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
, "bo map");
591 fprintf(stderr
, "BO wait for map failed\n");
599 vc4_bufmgr_destroy(struct pipe_screen
*pscreen
)
601 struct vc4_screen
*screen
= vc4_screen(pscreen
);
602 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
604 vc4_bo_cache_free_all(cache
);
607 fprintf(stderr
, "BO stats after screen destroy:\n");
608 vc4_bo_dump_stats(screen
);