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/u_string.h"
34 #include "util/ralloc.h"
36 #include "vc4_context.h"
37 #include "vc4_screen.h"
47 static bool dump_stats
= false;
50 vc4_bo_cache_free_all(struct vc4_bo_cache
*cache
);
53 vc4_bo_debug_describe(char* buf
, const struct vc4_bo
*ptr
)
55 util_sprintf(buf
, "vc4_bo<%s,%u,%u>", ptr
->name
? ptr
->name
: "?",
56 ptr
->handle
, ptr
->size
);
60 vc4_bo_label(struct vc4_screen
*screen
, struct vc4_bo
*bo
, const char *fmt
, ...)
62 /* Perform BO labeling by default on debug builds (so that you get
63 * whole-system allocation information), or if VC4_DEBUG=surf is set
64 * (for debugging a single app's allocation).
67 if (!(vc4_debug
& VC4_DEBUG_SURFACE
))
72 char *name
= ralloc_vasprintf(NULL
, fmt
, va
);
75 struct drm_vc4_label_bo label
= {
78 .name
= (uintptr_t)name
,
80 vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_LABEL_BO
, &label
);
86 vc4_bo_dump_stats(struct vc4_screen
*screen
)
88 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
90 fprintf(stderr
, " BOs allocated: %d\n", screen
->bo_count
);
91 fprintf(stderr
, " BOs size: %dkb\n", screen
->bo_size
/ 1024);
92 fprintf(stderr
, " BOs cached: %d\n", cache
->bo_count
);
93 fprintf(stderr
, " BOs cached size: %dkb\n", cache
->bo_size
/ 1024);
95 if (!list_empty(&cache
->time_list
)) {
96 struct vc4_bo
*first
= LIST_ENTRY(struct vc4_bo
,
97 cache
->time_list
.next
,
99 struct vc4_bo
*last
= LIST_ENTRY(struct vc4_bo
,
100 cache
->time_list
.prev
,
103 fprintf(stderr
, " oldest cache time: %ld\n",
104 (long)first
->free_time
);
105 fprintf(stderr
, " newest cache time: %ld\n",
106 (long)last
->free_time
);
108 struct timespec time
;
109 clock_gettime(CLOCK_MONOTONIC
, &time
);
110 fprintf(stderr
, " now: %ld\n",
116 vc4_bo_remove_from_cache(struct vc4_bo_cache
*cache
, struct vc4_bo
*bo
)
118 list_del(&bo
->time_list
);
119 list_del(&bo
->size_list
);
121 cache
->bo_size
-= bo
->size
;
124 static void vc4_bo_purgeable(struct vc4_bo
*bo
)
126 struct drm_vc4_gem_madvise arg
= {
127 .handle
= bo
->handle
,
128 .madv
= VC4_MADV_DONTNEED
,
131 if (bo
->screen
->has_madvise
)
132 vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_GEM_MADVISE
, &arg
);
135 static bool vc4_bo_unpurgeable(struct vc4_bo
*bo
)
137 struct drm_vc4_gem_madvise arg
= {
138 .handle
= bo
->handle
,
139 .madv
= VC4_MADV_WILLNEED
,
142 if (!bo
->screen
->has_madvise
)
145 if (vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_GEM_MADVISE
, &arg
))
152 vc4_bo_free(struct vc4_bo
*bo
)
154 struct vc4_screen
*screen
= bo
->screen
;
157 if (using_vc4_simulator
&& bo
->name
&&
158 strcmp(bo
->name
, "winsys") == 0) {
161 munmap(bo
->map
, bo
->size
);
162 VG(VALGRIND_FREELIKE_BLOCK(bo
->map
, 0));
166 struct drm_gem_close c
;
167 memset(&c
, 0, sizeof(c
));
168 c
.handle
= bo
->handle
;
169 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
171 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
174 screen
->bo_size
-= bo
->size
;
177 fprintf(stderr
, "Freed %s%s%dkb:\n",
178 bo
->name
? bo
->name
: "",
181 vc4_bo_dump_stats(screen
);
187 static struct vc4_bo
*
188 vc4_bo_from_cache(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
190 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
191 uint32_t page_index
= size
/ 4096 - 1;
192 struct vc4_bo
*iter
, *tmp
, *bo
= NULL
;
194 if (cache
->size_list_size
<= page_index
)
197 mtx_lock(&cache
->lock
);
198 LIST_FOR_EACH_ENTRY_SAFE(iter
, tmp
, &cache
->size_list
[page_index
],
200 /* Check that the BO has gone idle. If not, then none of the
201 * other BOs (pushed to the list after later rendering) are
202 * likely to be idle, either.
204 if (!vc4_bo_wait(iter
, 0, NULL
))
207 if (!vc4_bo_unpurgeable(iter
)) {
208 /* The BO has been purged. Free it and try to find
209 * another one in the cache.
211 vc4_bo_remove_from_cache(cache
, iter
);
217 pipe_reference_init(&bo
->reference
, 1);
218 vc4_bo_remove_from_cache(cache
, bo
);
220 vc4_bo_label(screen
, bo
, "%s", name
);
224 mtx_unlock(&cache
->lock
);
229 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
231 bool cleared_and_retried
= false;
232 struct drm_vc4_create_bo create
;
236 size
= align(size
, 4096);
238 bo
= vc4_bo_from_cache(screen
, size
, name
);
241 fprintf(stderr
, "Allocated %s %dkb from cache:\n",
243 vc4_bo_dump_stats(screen
);
248 bo
= CALLOC_STRUCT(vc4_bo
);
252 pipe_reference_init(&bo
->reference
, 1);
259 memset(&create
, 0, sizeof(create
));
262 ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_BO
, &create
);
263 bo
->handle
= create
.handle
;
266 if (!list_empty(&screen
->bo_cache
.time_list
) &&
267 !cleared_and_retried
) {
268 cleared_and_retried
= true;
269 vc4_bo_cache_free_all(&screen
->bo_cache
);
278 screen
->bo_size
+= bo
->size
;
280 fprintf(stderr
, "Allocated %s %dkb:\n", name
, size
/ 1024);
281 vc4_bo_dump_stats(screen
);
284 vc4_bo_label(screen
, bo
, "%s", name
);
290 vc4_bo_last_unreference(struct vc4_bo
*bo
)
292 struct vc4_screen
*screen
= bo
->screen
;
294 struct timespec time
;
295 clock_gettime(CLOCK_MONOTONIC
, &time
);
296 mtx_lock(&screen
->bo_cache
.lock
);
297 vc4_bo_last_unreference_locked_timed(bo
, time
.tv_sec
);
298 mtx_unlock(&screen
->bo_cache
.lock
);
302 free_stale_bos(struct vc4_screen
*screen
, time_t time
)
304 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
305 bool freed_any
= false;
307 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
309 if (dump_stats
&& !freed_any
) {
310 fprintf(stderr
, "Freeing stale BOs:\n");
311 vc4_bo_dump_stats(screen
);
315 /* If it's more than a second old, free it. */
316 if (time
- bo
->free_time
> 2) {
317 vc4_bo_remove_from_cache(cache
, bo
);
324 if (dump_stats
&& freed_any
) {
325 fprintf(stderr
, "Freed stale BOs:\n");
326 vc4_bo_dump_stats(screen
);
331 vc4_bo_cache_free_all(struct vc4_bo_cache
*cache
)
333 mtx_lock(&cache
->lock
);
334 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
336 vc4_bo_remove_from_cache(cache
, bo
);
339 mtx_unlock(&cache
->lock
);
343 vc4_bo_last_unreference_locked_timed(struct vc4_bo
*bo
, time_t time
)
345 struct vc4_screen
*screen
= bo
->screen
;
346 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
347 uint32_t page_index
= bo
->size
/ 4096 - 1;
354 if (cache
->size_list_size
<= page_index
) {
355 struct list_head
*new_list
=
356 ralloc_array(screen
, struct list_head
, page_index
+ 1);
358 /* Move old list contents over (since the array has moved, and
359 * therefore the pointers to the list heads have to change).
361 for (int i
= 0; i
< cache
->size_list_size
; i
++)
362 list_replace(&cache
->size_list
[i
], &new_list
[i
]);
363 for (int i
= cache
->size_list_size
; i
< page_index
+ 1; i
++)
364 list_inithead(&new_list
[i
]);
366 cache
->size_list
= new_list
;
367 cache
->size_list_size
= page_index
+ 1;
370 vc4_bo_purgeable(bo
);
371 bo
->free_time
= time
;
372 list_addtail(&bo
->size_list
, &cache
->size_list
[page_index
]);
373 list_addtail(&bo
->time_list
, &cache
->time_list
);
375 cache
->bo_size
+= bo
->size
;
377 fprintf(stderr
, "Freed %s %dkb to cache:\n",
378 bo
->name
, bo
->size
/ 1024);
379 vc4_bo_dump_stats(screen
);
382 vc4_bo_label(screen
, bo
, "mesa cache");
384 free_stale_bos(screen
, time
);
387 static struct vc4_bo
*
388 vc4_bo_open_handle(struct vc4_screen
*screen
,
389 uint32_t handle
, uint32_t size
)
395 mtx_lock(&screen
->bo_handles_mutex
);
397 bo
= util_hash_table_get(screen
->bo_handles
, (void*)(uintptr_t)handle
);
399 vc4_bo_reference(bo
);
403 bo
= CALLOC_STRUCT(vc4_bo
);
404 pipe_reference_init(&bo
->reference
, 1);
411 #ifdef USE_VC4_SIMULATOR
412 vc4_simulator_open_from_handle(screen
->fd
, bo
->handle
, bo
->size
);
413 bo
->map
= malloc(bo
->size
);
416 util_hash_table_set(screen
->bo_handles
, (void *)(uintptr_t)handle
, bo
);
419 mtx_unlock(&screen
->bo_handles_mutex
);
424 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
)
426 struct drm_gem_open o
= {
429 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
431 fprintf(stderr
, "Failed to open bo %d: %s\n",
432 name
, strerror(errno
));
436 return vc4_bo_open_handle(screen
, o
.handle
, o
.size
);
440 vc4_bo_open_dmabuf(struct vc4_screen
*screen
, int fd
)
443 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
446 fprintf(stderr
, "Failed to get vc4 handle for dmabuf %d\n", fd
);
450 /* Determine the size of the bo we were handed. */
451 size
= lseek(fd
, 0, SEEK_END
);
453 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
457 return vc4_bo_open_handle(screen
, handle
, size
);
461 vc4_bo_get_dmabuf(struct vc4_bo
*bo
)
464 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
467 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
472 mtx_lock(&bo
->screen
->bo_handles_mutex
);
474 util_hash_table_set(bo
->screen
->bo_handles
, (void *)(uintptr_t)bo
->handle
, bo
);
475 mtx_unlock(&bo
->screen
->bo_handles_mutex
);
481 vc4_bo_alloc_shader(struct vc4_screen
*screen
, const void *data
, uint32_t size
)
486 bo
= CALLOC_STRUCT(vc4_bo
);
490 pipe_reference_init(&bo
->reference
, 1);
492 bo
->size
= align(size
, 4096);
494 bo
->private = false; /* Make sure it doesn't go back to the cache. */
496 struct drm_vc4_create_shader_bo create
= {
498 .data
= (uintptr_t)data
,
501 ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_SHADER_BO
,
503 bo
->handle
= create
.handle
;
506 fprintf(stderr
, "create shader ioctl failure\n");
511 screen
->bo_size
+= bo
->size
;
513 fprintf(stderr
, "Allocated shader %dkb:\n", bo
->size
/ 1024);
514 vc4_bo_dump_stats(screen
);
521 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
523 struct drm_gem_flink flink
= {
524 .handle
= bo
->handle
,
526 int ret
= vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
528 fprintf(stderr
, "Failed to flink bo %d: %s\n",
529 bo
->handle
, strerror(errno
));
540 static int vc4_wait_seqno_ioctl(int fd
, uint64_t seqno
, uint64_t timeout_ns
)
542 struct drm_vc4_wait_seqno wait
= {
544 .timeout_ns
= timeout_ns
,
546 int ret
= vc4_ioctl(fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
555 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
,
558 if (screen
->finished_seqno
>= seqno
)
561 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
562 if (vc4_wait_seqno_ioctl(screen
->fd
, seqno
, 0) == -ETIME
) {
563 fprintf(stderr
, "Blocking on seqno %lld for %s\n",
564 (long long)seqno
, reason
);
568 int ret
= vc4_wait_seqno_ioctl(screen
->fd
, seqno
, timeout_ns
);
571 fprintf(stderr
, "wait failed: %d\n", ret
);
578 screen
->finished_seqno
= seqno
;
582 static int vc4_wait_bo_ioctl(int fd
, uint32_t handle
, uint64_t timeout_ns
)
584 struct drm_vc4_wait_bo wait
= {
586 .timeout_ns
= timeout_ns
,
588 int ret
= vc4_ioctl(fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
597 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
, const char *reason
)
599 struct vc4_screen
*screen
= bo
->screen
;
601 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
602 if (vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, 0) == -ETIME
) {
603 fprintf(stderr
, "Blocking on %s BO for %s\n",
608 int ret
= vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, timeout_ns
);
611 fprintf(stderr
, "wait failed: %d\n", ret
);
622 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
630 struct drm_vc4_mmap_bo map
;
631 memset(&map
, 0, sizeof(map
));
632 map
.handle
= bo
->handle
;
633 ret
= vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_MMAP_BO
, &map
);
636 fprintf(stderr
, "map ioctl failure\n");
640 bo
->map
= mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
641 bo
->screen
->fd
, offset
);
642 if (bo
->map
== MAP_FAILED
) {
643 fprintf(stderr
, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
644 bo
->handle
, (long long)offset
, bo
->size
);
647 VG(VALGRIND_MALLOCLIKE_BLOCK(bo
->map
, bo
->size
, 0, false));
653 vc4_bo_map(struct vc4_bo
*bo
)
655 void *map
= vc4_bo_map_unsynchronized(bo
);
657 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
, "bo map");
659 fprintf(stderr
, "BO wait for map failed\n");
667 vc4_bufmgr_destroy(struct pipe_screen
*pscreen
)
669 struct vc4_screen
*screen
= vc4_screen(pscreen
);
670 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
672 vc4_bo_cache_free_all(cache
);
675 fprintf(stderr
, "BO stats after screen destroy:\n");
676 vc4_bo_dump_stats(screen
);