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"
39 static bool dump_stats
= false;
42 vc4_bo_cache_free_all(struct vc4_bo_cache
*cache
);
45 vc4_bo_debug_describe(char* buf
, const struct vc4_bo
*ptr
)
47 sprintf(buf
, "vc4_bo<%s,%u,%u>", ptr
->name
? ptr
->name
: "?",
48 ptr
->handle
, ptr
->size
);
52 vc4_bo_label(struct vc4_screen
*screen
, struct vc4_bo
*bo
, const char *fmt
, ...)
54 /* Perform BO labeling by default on debug builds (so that you get
55 * whole-system allocation information), or if VC4_DEBUG=surf is set
56 * (for debugging a single app's allocation).
59 if (!(vc4_debug
& VC4_DEBUG_SURFACE
))
64 char *name
= ralloc_vasprintf(NULL
, fmt
, va
);
67 struct drm_vc4_label_bo label
= {
70 .name
= (uintptr_t)name
,
72 vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_LABEL_BO
, &label
);
78 vc4_bo_dump_stats(struct vc4_screen
*screen
)
80 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
82 fprintf(stderr
, " BOs allocated: %d\n", screen
->bo_count
);
83 fprintf(stderr
, " BOs size: %dkb\n", screen
->bo_size
/ 1024);
84 fprintf(stderr
, " BOs cached: %d\n", cache
->bo_count
);
85 fprintf(stderr
, " BOs cached size: %dkb\n", cache
->bo_size
/ 1024);
87 if (!list_is_empty(&cache
->time_list
)) {
88 struct vc4_bo
*first
= LIST_ENTRY(struct vc4_bo
,
89 cache
->time_list
.next
,
91 struct vc4_bo
*last
= LIST_ENTRY(struct vc4_bo
,
92 cache
->time_list
.prev
,
95 fprintf(stderr
, " oldest cache time: %ld\n",
96 (long)first
->free_time
);
97 fprintf(stderr
, " newest cache time: %ld\n",
98 (long)last
->free_time
);
100 struct timespec time
;
101 clock_gettime(CLOCK_MONOTONIC
, &time
);
102 fprintf(stderr
, " now: %ld\n",
108 vc4_bo_remove_from_cache(struct vc4_bo_cache
*cache
, struct vc4_bo
*bo
)
110 list_del(&bo
->time_list
);
111 list_del(&bo
->size_list
);
113 cache
->bo_size
-= bo
->size
;
116 static void vc4_bo_purgeable(struct vc4_bo
*bo
)
118 struct drm_vc4_gem_madvise arg
= {
119 .handle
= bo
->handle
,
120 .madv
= VC4_MADV_DONTNEED
,
123 if (bo
->screen
->has_madvise
)
124 vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_GEM_MADVISE
, &arg
);
127 static bool vc4_bo_unpurgeable(struct vc4_bo
*bo
)
129 struct drm_vc4_gem_madvise arg
= {
130 .handle
= bo
->handle
,
131 .madv
= VC4_MADV_WILLNEED
,
134 if (!bo
->screen
->has_madvise
)
137 if (vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_GEM_MADVISE
, &arg
))
144 vc4_bo_free(struct vc4_bo
*bo
)
146 struct vc4_screen
*screen
= bo
->screen
;
149 if (using_vc4_simulator
&& bo
->name
&&
150 strcmp(bo
->name
, "winsys") == 0) {
153 munmap(bo
->map
, bo
->size
);
154 VG(VALGRIND_FREELIKE_BLOCK(bo
->map
, 0));
158 struct drm_gem_close c
;
159 memset(&c
, 0, sizeof(c
));
160 c
.handle
= bo
->handle
;
161 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
163 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
166 screen
->bo_size
-= bo
->size
;
169 fprintf(stderr
, "Freed %s%s%dkb:\n",
170 bo
->name
? bo
->name
: "",
173 vc4_bo_dump_stats(screen
);
179 static struct vc4_bo
*
180 vc4_bo_from_cache(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
182 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
183 uint32_t page_index
= size
/ 4096 - 1;
184 struct vc4_bo
*iter
, *tmp
, *bo
= NULL
;
186 if (cache
->size_list_size
<= page_index
)
189 mtx_lock(&cache
->lock
);
190 LIST_FOR_EACH_ENTRY_SAFE(iter
, tmp
, &cache
->size_list
[page_index
],
192 /* Check that the BO has gone idle. If not, then none of the
193 * other BOs (pushed to the list after later rendering) are
194 * likely to be idle, either.
196 if (!vc4_bo_wait(iter
, 0, NULL
))
199 if (!vc4_bo_unpurgeable(iter
)) {
200 /* The BO has been purged. Free it and try to find
201 * another one in the cache.
203 vc4_bo_remove_from_cache(cache
, iter
);
209 pipe_reference_init(&bo
->reference
, 1);
210 vc4_bo_remove_from_cache(cache
, bo
);
212 vc4_bo_label(screen
, bo
, "%s", name
);
216 mtx_unlock(&cache
->lock
);
221 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
223 bool cleared_and_retried
= false;
224 struct drm_vc4_create_bo create
;
228 size
= align(size
, 4096);
230 bo
= vc4_bo_from_cache(screen
, size
, name
);
233 fprintf(stderr
, "Allocated %s %dkb from cache:\n",
235 vc4_bo_dump_stats(screen
);
240 bo
= CALLOC_STRUCT(vc4_bo
);
244 pipe_reference_init(&bo
->reference
, 1);
251 memset(&create
, 0, sizeof(create
));
254 ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_BO
, &create
);
255 bo
->handle
= create
.handle
;
258 if (!list_is_empty(&screen
->bo_cache
.time_list
) &&
259 !cleared_and_retried
) {
260 cleared_and_retried
= true;
261 vc4_bo_cache_free_all(&screen
->bo_cache
);
270 screen
->bo_size
+= bo
->size
;
272 fprintf(stderr
, "Allocated %s %dkb:\n", name
, size
/ 1024);
273 vc4_bo_dump_stats(screen
);
276 vc4_bo_label(screen
, bo
, "%s", name
);
282 vc4_bo_last_unreference(struct vc4_bo
*bo
)
284 struct vc4_screen
*screen
= bo
->screen
;
286 struct timespec time
;
287 clock_gettime(CLOCK_MONOTONIC
, &time
);
288 mtx_lock(&screen
->bo_cache
.lock
);
289 vc4_bo_last_unreference_locked_timed(bo
, time
.tv_sec
);
290 mtx_unlock(&screen
->bo_cache
.lock
);
294 free_stale_bos(struct vc4_screen
*screen
, time_t time
)
296 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
297 bool freed_any
= false;
299 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
301 if (dump_stats
&& !freed_any
) {
302 fprintf(stderr
, "Freeing stale BOs:\n");
303 vc4_bo_dump_stats(screen
);
307 /* If it's more than a second old, free it. */
308 if (time
- bo
->free_time
> 2) {
309 vc4_bo_remove_from_cache(cache
, bo
);
316 if (dump_stats
&& freed_any
) {
317 fprintf(stderr
, "Freed stale BOs:\n");
318 vc4_bo_dump_stats(screen
);
323 vc4_bo_cache_free_all(struct vc4_bo_cache
*cache
)
325 mtx_lock(&cache
->lock
);
326 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
328 vc4_bo_remove_from_cache(cache
, bo
);
331 mtx_unlock(&cache
->lock
);
335 vc4_bo_last_unreference_locked_timed(struct vc4_bo
*bo
, time_t time
)
337 struct vc4_screen
*screen
= bo
->screen
;
338 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
339 uint32_t page_index
= bo
->size
/ 4096 - 1;
346 if (cache
->size_list_size
<= page_index
) {
347 struct list_head
*new_list
=
348 ralloc_array(screen
, struct list_head
, page_index
+ 1);
350 /* Move old list contents over (since the array has moved, and
351 * therefore the pointers to the list heads have to change).
353 for (int i
= 0; i
< cache
->size_list_size
; i
++)
354 list_replace(&cache
->size_list
[i
], &new_list
[i
]);
355 for (int i
= cache
->size_list_size
; i
< page_index
+ 1; i
++)
356 list_inithead(&new_list
[i
]);
358 cache
->size_list
= new_list
;
359 cache
->size_list_size
= page_index
+ 1;
362 vc4_bo_purgeable(bo
);
363 bo
->free_time
= time
;
364 list_addtail(&bo
->size_list
, &cache
->size_list
[page_index
]);
365 list_addtail(&bo
->time_list
, &cache
->time_list
);
367 cache
->bo_size
+= bo
->size
;
369 fprintf(stderr
, "Freed %s %dkb to cache:\n",
370 bo
->name
, bo
->size
/ 1024);
371 vc4_bo_dump_stats(screen
);
374 vc4_bo_label(screen
, bo
, "mesa cache");
376 free_stale_bos(screen
, time
);
379 static struct vc4_bo
*
380 vc4_bo_open_handle(struct vc4_screen
*screen
,
381 uint32_t handle
, uint32_t size
)
387 mtx_lock(&screen
->bo_handles_mutex
);
389 bo
= util_hash_table_get(screen
->bo_handles
, (void*)(uintptr_t)handle
);
391 vc4_bo_reference(bo
);
395 bo
= CALLOC_STRUCT(vc4_bo
);
396 pipe_reference_init(&bo
->reference
, 1);
403 #ifdef USE_VC4_SIMULATOR
404 vc4_simulator_open_from_handle(screen
->fd
, bo
->handle
, bo
->size
);
405 bo
->map
= malloc(bo
->size
);
408 _mesa_hash_table_insert(screen
->bo_handles
, (void *)(uintptr_t)handle
, bo
);
411 mtx_unlock(&screen
->bo_handles_mutex
);
416 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
)
418 struct drm_gem_open o
= {
421 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
423 fprintf(stderr
, "Failed to open bo %d: %s\n",
424 name
, strerror(errno
));
428 return vc4_bo_open_handle(screen
, o
.handle
, o
.size
);
432 vc4_bo_open_dmabuf(struct vc4_screen
*screen
, int fd
)
435 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
438 fprintf(stderr
, "Failed to get vc4 handle for dmabuf %d\n", fd
);
442 /* Determine the size of the bo we were handed. */
443 size
= lseek(fd
, 0, SEEK_END
);
445 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
449 return vc4_bo_open_handle(screen
, handle
, size
);
453 vc4_bo_get_dmabuf(struct vc4_bo
*bo
)
456 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
459 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
464 mtx_lock(&bo
->screen
->bo_handles_mutex
);
466 _mesa_hash_table_insert(bo
->screen
->bo_handles
, (void *)(uintptr_t)bo
->handle
, bo
);
467 mtx_unlock(&bo
->screen
->bo_handles_mutex
);
473 vc4_bo_alloc_shader(struct vc4_screen
*screen
, const void *data
, uint32_t size
)
478 bo
= CALLOC_STRUCT(vc4_bo
);
482 pipe_reference_init(&bo
->reference
, 1);
484 bo
->size
= align(size
, 4096);
486 bo
->private = false; /* Make sure it doesn't go back to the cache. */
488 struct drm_vc4_create_shader_bo create
= {
490 .data
= (uintptr_t)data
,
493 ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_SHADER_BO
,
495 bo
->handle
= create
.handle
;
498 fprintf(stderr
, "create shader ioctl failure\n");
503 screen
->bo_size
+= bo
->size
;
505 fprintf(stderr
, "Allocated shader %dkb:\n", bo
->size
/ 1024);
506 vc4_bo_dump_stats(screen
);
513 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
515 struct drm_gem_flink flink
= {
516 .handle
= bo
->handle
,
518 int ret
= vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
520 fprintf(stderr
, "Failed to flink bo %d: %s\n",
521 bo
->handle
, strerror(errno
));
532 static int vc4_wait_seqno_ioctl(int fd
, uint64_t seqno
, uint64_t timeout_ns
)
534 struct drm_vc4_wait_seqno wait
= {
536 .timeout_ns
= timeout_ns
,
538 int ret
= vc4_ioctl(fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
547 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
,
550 if (screen
->finished_seqno
>= seqno
)
553 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
554 if (vc4_wait_seqno_ioctl(screen
->fd
, seqno
, 0) == -ETIME
) {
555 fprintf(stderr
, "Blocking on seqno %lld for %s\n",
556 (long long)seqno
, reason
);
560 int ret
= vc4_wait_seqno_ioctl(screen
->fd
, seqno
, timeout_ns
);
563 fprintf(stderr
, "wait failed: %d\n", ret
);
570 screen
->finished_seqno
= seqno
;
574 static int vc4_wait_bo_ioctl(int fd
, uint32_t handle
, uint64_t timeout_ns
)
576 struct drm_vc4_wait_bo wait
= {
578 .timeout_ns
= timeout_ns
,
580 int ret
= vc4_ioctl(fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
589 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
, const char *reason
)
591 struct vc4_screen
*screen
= bo
->screen
;
593 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
594 if (vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, 0) == -ETIME
) {
595 fprintf(stderr
, "Blocking on %s BO for %s\n",
600 int ret
= vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, timeout_ns
);
603 fprintf(stderr
, "wait failed: %d\n", ret
);
614 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
622 struct drm_vc4_mmap_bo map
;
623 memset(&map
, 0, sizeof(map
));
624 map
.handle
= bo
->handle
;
625 ret
= vc4_ioctl(bo
->screen
->fd
, DRM_IOCTL_VC4_MMAP_BO
, &map
);
628 fprintf(stderr
, "map ioctl failure\n");
632 bo
->map
= mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
633 bo
->screen
->fd
, offset
);
634 if (bo
->map
== MAP_FAILED
) {
635 fprintf(stderr
, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
636 bo
->handle
, (long long)offset
, bo
->size
);
639 VG(VALGRIND_MALLOCLIKE_BLOCK(bo
->map
, bo
->size
, 0, false));
645 vc4_bo_map(struct vc4_bo
*bo
)
647 void *map
= vc4_bo_map_unsynchronized(bo
);
649 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
, "bo map");
651 fprintf(stderr
, "BO wait for map failed\n");
659 vc4_bufmgr_destroy(struct pipe_screen
*pscreen
)
661 struct vc4_screen
*screen
= vc4_screen(pscreen
);
662 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
664 vc4_bo_cache_free_all(cache
);
667 fprintf(stderr
, "BO stats after screen destroy:\n");
668 vc4_bo_dump_stats(screen
);