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_memory.h"
32 #include "util/ralloc.h"
34 #include "vc4_context.h"
35 #include "vc4_screen.h"
37 static bool dump_stats
= false;
40 vc4_bo_dump_stats(struct vc4_screen
*screen
)
42 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
44 fprintf(stderr
, " BOs allocated: %d\n", screen
->bo_count
);
45 fprintf(stderr
, " BOs size: %dkb\n", screen
->bo_size
/ 1024);
46 fprintf(stderr
, " BOs cached: %d\n", cache
->bo_count
);
47 fprintf(stderr
, " BOs cached size: %dkb\n", cache
->bo_size
/ 1024);
49 if (!list_empty(&cache
->time_list
)) {
50 struct vc4_bo
*first
= LIST_ENTRY(struct vc4_bo
,
51 cache
->time_list
.next
,
53 struct vc4_bo
*last
= LIST_ENTRY(struct vc4_bo
,
54 cache
->time_list
.prev
,
57 fprintf(stderr
, " oldest cache time: %ld\n",
58 (long)first
->free_time
);
59 fprintf(stderr
, " newest cache time: %ld\n",
60 (long)last
->free_time
);
63 clock_gettime(CLOCK_MONOTONIC
, &time
);
64 fprintf(stderr
, " now: %ld\n",
70 vc4_bo_remove_from_cache(struct vc4_bo_cache
*cache
, struct vc4_bo
*bo
)
72 list_del(&bo
->time_list
);
73 list_del(&bo
->size_list
);
75 cache
->bo_size
-= bo
->size
;
78 static struct vc4_bo
*
79 vc4_bo_from_cache(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
81 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
82 uint32_t page_index
= size
/ 4096 - 1;
84 if (cache
->size_list_size
<= page_index
)
87 struct vc4_bo
*bo
= NULL
;
88 pipe_mutex_lock(cache
->lock
);
89 if (!list_empty(&cache
->size_list
[page_index
])) {
90 bo
= LIST_ENTRY(struct vc4_bo
, cache
->size_list
[page_index
].next
,
93 /* Check that the BO has gone idle. If not, then we want to
94 * allocate something new instead, since we assume that the
95 * user will proceed to CPU map it and fill it with stuff.
97 if (!vc4_bo_wait(bo
, 0, NULL
)) {
98 pipe_mutex_unlock(cache
->lock
);
102 pipe_reference_init(&bo
->reference
, 1);
103 vc4_bo_remove_from_cache(cache
, bo
);
107 pipe_mutex_unlock(cache
->lock
);
112 vc4_bo_alloc(struct vc4_screen
*screen
, uint32_t size
, const char *name
)
117 size
= align(size
, 4096);
119 bo
= vc4_bo_from_cache(screen
, size
, name
);
122 fprintf(stderr
, "Allocated %s %dkb from cache:\n",
124 vc4_bo_dump_stats(screen
);
129 bo
= CALLOC_STRUCT(vc4_bo
);
133 pipe_reference_init(&bo
->reference
, 1);
139 if (!using_vc4_simulator
) {
140 struct drm_vc4_create_bo create
;
141 memset(&create
, 0, sizeof(create
));
145 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_BO
, &create
);
146 bo
->handle
= create
.handle
;
148 struct drm_mode_create_dumb create
;
149 memset(&create
, 0, sizeof(create
));
153 create
.height
= (size
+ 127) / 128;
155 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create
);
156 bo
->handle
= create
.handle
;
157 assert(create
.size
>= size
);
160 fprintf(stderr
, "create ioctl failure\n");
165 screen
->bo_size
+= bo
->size
;
167 fprintf(stderr
, "Allocated %s %dkb:\n", name
, size
/ 1024);
168 vc4_bo_dump_stats(screen
);
175 vc4_bo_last_unreference(struct vc4_bo
*bo
)
177 struct vc4_screen
*screen
= bo
->screen
;
179 struct timespec time
;
180 clock_gettime(CLOCK_MONOTONIC
, &time
);
181 pipe_mutex_lock(screen
->bo_cache
.lock
);
182 vc4_bo_last_unreference_locked_timed(bo
, time
.tv_sec
);
183 pipe_mutex_unlock(screen
->bo_cache
.lock
);
187 vc4_bo_free(struct vc4_bo
*bo
)
189 struct vc4_screen
*screen
= bo
->screen
;
192 #ifdef USE_VC4_SIMULATOR
193 if (bo
->simulator_winsys_map
) {
195 bo
->map
= bo
->simulator_winsys_map
;
198 munmap(bo
->map
, bo
->size
);
201 struct drm_gem_close c
;
202 memset(&c
, 0, sizeof(c
));
203 c
.handle
= bo
->handle
;
204 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
206 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
209 screen
->bo_size
-= bo
->size
;
212 fprintf(stderr
, "Freed %s%s%dkb:\n",
213 bo
->name
? bo
->name
: "",
216 vc4_bo_dump_stats(screen
);
223 free_stale_bos(struct vc4_screen
*screen
, time_t time
)
225 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
226 bool freed_any
= false;
228 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
230 if (dump_stats
&& !freed_any
) {
231 fprintf(stderr
, "Freeing stale BOs:\n");
232 vc4_bo_dump_stats(screen
);
236 /* If it's more than a second old, free it. */
237 if (time
- bo
->free_time
> 2) {
238 vc4_bo_remove_from_cache(cache
, bo
);
245 if (dump_stats
&& freed_any
) {
246 fprintf(stderr
, "Freed stale BOs:\n");
247 vc4_bo_dump_stats(screen
);
252 vc4_bo_last_unreference_locked_timed(struct vc4_bo
*bo
, time_t time
)
254 struct vc4_screen
*screen
= bo
->screen
;
255 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
256 uint32_t page_index
= bo
->size
/ 4096 - 1;
263 if (cache
->size_list_size
<= page_index
) {
264 struct list_head
*new_list
=
265 ralloc_array(screen
, struct list_head
, page_index
+ 1);
267 /* Move old list contents over (since the array has moved, and
268 * therefore the pointers to the list heads have to change).
270 for (int i
= 0; i
< cache
->size_list_size
; i
++) {
271 struct list_head
*old_head
= &cache
->size_list
[i
];
272 if (list_empty(old_head
))
273 list_inithead(&new_list
[i
]);
275 new_list
[i
].next
= old_head
->next
;
276 new_list
[i
].prev
= old_head
->prev
;
277 new_list
[i
].next
->prev
= &new_list
[i
];
278 new_list
[i
].prev
->next
= &new_list
[i
];
281 for (int i
= cache
->size_list_size
; i
< page_index
+ 1; i
++)
282 list_inithead(&new_list
[i
]);
284 cache
->size_list
= new_list
;
285 cache
->size_list_size
= page_index
+ 1;
288 bo
->free_time
= time
;
289 list_addtail(&bo
->size_list
, &cache
->size_list
[page_index
]);
290 list_addtail(&bo
->time_list
, &cache
->time_list
);
292 cache
->bo_size
+= bo
->size
;
294 fprintf(stderr
, "Freed %s %dkb to cache:\n",
295 bo
->name
, bo
->size
/ 1024);
296 vc4_bo_dump_stats(screen
);
300 free_stale_bos(screen
, time
);
303 static struct vc4_bo
*
304 vc4_bo_open_handle(struct vc4_screen
*screen
,
305 uint32_t winsys_stride
,
306 uint32_t handle
, uint32_t size
)
308 struct vc4_bo
*bo
= CALLOC_STRUCT(vc4_bo
);
312 pipe_reference_init(&bo
->reference
, 1);
319 #ifdef USE_VC4_SIMULATOR
321 bo
->simulator_winsys_map
= bo
->map
;
322 bo
->simulator_winsys_stride
= winsys_stride
;
323 bo
->map
= malloc(bo
->size
);
330 vc4_bo_open_name(struct vc4_screen
*screen
, uint32_t name
,
331 uint32_t winsys_stride
)
333 struct drm_gem_open o
= {
336 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
338 fprintf(stderr
, "Failed to open bo %d: %s\n",
339 name
, strerror(errno
));
343 return vc4_bo_open_handle(screen
, winsys_stride
, o
.handle
, o
.size
);
347 vc4_bo_open_dmabuf(struct vc4_screen
*screen
, int fd
, uint32_t winsys_stride
)
350 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
353 fprintf(stderr
, "Failed to get vc4 handle for dmabuf %d\n", fd
);
357 /* Determine the size of the bo we were handed. */
358 size
= lseek(fd
, 0, SEEK_END
);
360 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
364 return vc4_bo_open_handle(screen
, winsys_stride
, handle
, size
);
368 vc4_bo_get_dmabuf(struct vc4_bo
*bo
)
371 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
374 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
384 vc4_bo_alloc_shader(struct vc4_screen
*screen
, const void *data
, uint32_t size
)
389 bo
= CALLOC_STRUCT(vc4_bo
);
393 pipe_reference_init(&bo
->reference
, 1);
395 bo
->size
= align(size
, 4096);
397 bo
->private = false; /* Make sure it doesn't go back to the cache. */
399 if (!using_vc4_simulator
) {
400 struct drm_vc4_create_shader_bo create
= {
402 .data
= (uintptr_t)data
,
405 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_VC4_CREATE_SHADER_BO
,
407 bo
->handle
= create
.handle
;
409 struct drm_mode_create_dumb create
;
410 memset(&create
, 0, sizeof(create
));
414 create
.height
= (size
+ 127) / 128;
416 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create
);
417 bo
->handle
= create
.handle
;
418 assert(create
.size
>= size
);
421 memcpy(bo
->map
, data
, size
);
424 fprintf(stderr
, "create shader ioctl failure\n");
429 screen
->bo_size
+= bo
->size
;
431 fprintf(stderr
, "Allocated shader %dkb:\n", size
/ 1024);
432 vc4_bo_dump_stats(screen
);
439 vc4_bo_flink(struct vc4_bo
*bo
, uint32_t *name
)
441 struct drm_gem_flink flink
= {
442 .handle
= bo
->handle
,
444 int ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
446 fprintf(stderr
, "Failed to flink bo %d: %s\n",
447 bo
->handle
, strerror(errno
));
458 static int vc4_wait_seqno_ioctl(int fd
, uint64_t seqno
, uint64_t timeout_ns
)
460 if (using_vc4_simulator
)
463 struct drm_vc4_wait_seqno wait
= {
465 .timeout_ns
= timeout_ns
,
467 int ret
= drmIoctl(fd
, DRM_IOCTL_VC4_WAIT_SEQNO
, &wait
);
476 vc4_wait_seqno(struct vc4_screen
*screen
, uint64_t seqno
, uint64_t timeout_ns
,
479 if (screen
->finished_seqno
>= seqno
)
482 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
483 if (vc4_wait_seqno_ioctl(screen
->fd
, seqno
, 0) == -ETIME
) {
484 fprintf(stderr
, "Blocking on seqno %lld for %s\n",
485 (long long)seqno
, reason
);
489 int ret
= vc4_wait_seqno_ioctl(screen
->fd
, seqno
, timeout_ns
);
492 fprintf(stderr
, "wait failed: %d\n", ret
);
499 screen
->finished_seqno
= seqno
;
503 static int vc4_wait_bo_ioctl(int fd
, uint32_t handle
, uint64_t timeout_ns
)
505 if (using_vc4_simulator
)
508 struct drm_vc4_wait_bo wait
= {
510 .timeout_ns
= timeout_ns
,
512 int ret
= drmIoctl(fd
, DRM_IOCTL_VC4_WAIT_BO
, &wait
);
521 vc4_bo_wait(struct vc4_bo
*bo
, uint64_t timeout_ns
, const char *reason
)
523 struct vc4_screen
*screen
= bo
->screen
;
525 if (unlikely(vc4_debug
& VC4_DEBUG_PERF
) && timeout_ns
&& reason
) {
526 if (vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, 0) == -ETIME
) {
527 fprintf(stderr
, "Blocking on %s BO for %s\n",
532 int ret
= vc4_wait_bo_ioctl(screen
->fd
, bo
->handle
, timeout_ns
);
535 fprintf(stderr
, "wait failed: %d\n", ret
);
546 vc4_bo_map_unsynchronized(struct vc4_bo
*bo
)
554 if (!using_vc4_simulator
) {
555 struct drm_vc4_mmap_bo map
;
556 memset(&map
, 0, sizeof(map
));
557 map
.handle
= bo
->handle
;
558 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_VC4_MMAP_BO
, &map
);
561 struct drm_mode_map_dumb map
;
562 memset(&map
, 0, sizeof(map
));
563 map
.handle
= bo
->handle
;
564 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_MODE_MAP_DUMB
, &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
);
584 vc4_bo_map(struct vc4_bo
*bo
)
586 void *map
= vc4_bo_map_unsynchronized(bo
);
588 bool ok
= vc4_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
, "bo map");
590 fprintf(stderr
, "BO wait for map failed\n");
598 vc4_bufmgr_destroy(struct pipe_screen
*pscreen
)
600 struct vc4_screen
*screen
= vc4_screen(pscreen
);
601 struct vc4_bo_cache
*cache
= &screen
->bo_cache
;
603 list_for_each_entry_safe(struct vc4_bo
, bo
, &cache
->time_list
,
605 vc4_bo_remove_from_cache(cache
, bo
);
610 fprintf(stderr
, "BO stats after screen destroy:\n");
611 vc4_bo_dump_stats(screen
);