2 * Copyright © 2014-2017 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 "v3d_context.h"
36 #include "v3d_screen.h"
38 static bool dump_stats
= false;
41 v3d_bo_cache_free_all(struct v3d_bo_cache
*cache
);
44 v3d_bo_dump_stats(struct v3d_screen
*screen
)
46 struct v3d_bo_cache
*cache
= &screen
->bo_cache
;
48 uint32_t cache_count
= 0;
49 uint32_t cache_size
= 0;
50 list_for_each_entry(struct v3d_bo
, bo
, &cache
->time_list
, time_list
) {
52 cache_size
+= bo
->size
;
55 fprintf(stderr
, " BOs allocated: %d\n", screen
->bo_count
);
56 fprintf(stderr
, " BOs size: %dkb\n", screen
->bo_size
/ 1024);
57 fprintf(stderr
, " BOs cached: %d\n", cache_count
);
58 fprintf(stderr
, " BOs cached size: %dkb\n", cache_size
/ 1024);
60 if (!list_is_empty(&cache
->time_list
)) {
61 struct v3d_bo
*first
= list_first_entry(&cache
->time_list
,
64 struct v3d_bo
*last
= list_last_entry(&cache
->time_list
,
68 fprintf(stderr
, " oldest cache time: %ld\n",
69 (long)first
->free_time
);
70 fprintf(stderr
, " newest cache time: %ld\n",
71 (long)last
->free_time
);
74 clock_gettime(CLOCK_MONOTONIC
, &time
);
75 fprintf(stderr
, " now: %ld\n",
81 v3d_bo_remove_from_cache(struct v3d_bo_cache
*cache
, struct v3d_bo
*bo
)
83 list_del(&bo
->time_list
);
84 list_del(&bo
->size_list
);
87 static struct v3d_bo
*
88 v3d_bo_from_cache(struct v3d_screen
*screen
, uint32_t size
, const char *name
)
90 struct v3d_bo_cache
*cache
= &screen
->bo_cache
;
91 uint32_t page_index
= size
/ 4096 - 1;
93 if (cache
->size_list_size
<= page_index
)
96 struct v3d_bo
*bo
= NULL
;
97 mtx_lock(&cache
->lock
);
98 if (!list_is_empty(&cache
->size_list
[page_index
])) {
99 bo
= list_first_entry(&cache
->size_list
[page_index
],
100 struct v3d_bo
, size_list
);
102 /* Check that the BO has gone idle. If not, then we want to
103 * allocate something new instead, since we assume that the
104 * user will proceed to CPU map it and fill it with stuff.
106 if (!v3d_bo_wait(bo
, 0, NULL
)) {
107 mtx_unlock(&cache
->lock
);
111 pipe_reference_init(&bo
->reference
, 1);
112 v3d_bo_remove_from_cache(cache
, bo
);
116 mtx_unlock(&cache
->lock
);
121 v3d_bo_alloc(struct v3d_screen
*screen
, uint32_t size
, const char *name
)
126 /* The CLIF dumping requires that there is no whitespace in the name.
128 assert(!strchr(name
, ' '));
130 size
= align(size
, 4096);
132 bo
= v3d_bo_from_cache(screen
, size
, name
);
135 fprintf(stderr
, "Allocated %s %dkb from cache:\n",
137 v3d_bo_dump_stats(screen
);
142 bo
= CALLOC_STRUCT(v3d_bo
);
146 pipe_reference_init(&bo
->reference
, 1);
155 bool cleared_and_retried
= false;
156 struct drm_v3d_create_bo create
= {
160 ret
= v3d_ioctl(screen
->fd
, DRM_IOCTL_V3D_CREATE_BO
, &create
);
161 bo
->handle
= create
.handle
;
162 bo
->offset
= create
.offset
;
165 if (!list_is_empty(&screen
->bo_cache
.time_list
) &&
166 !cleared_and_retried
) {
167 cleared_and_retried
= true;
168 v3d_bo_cache_free_all(&screen
->bo_cache
);
177 screen
->bo_size
+= bo
->size
;
179 fprintf(stderr
, "Allocated %s %dkb:\n", name
, size
/ 1024);
180 v3d_bo_dump_stats(screen
);
187 v3d_bo_last_unreference(struct v3d_bo
*bo
)
189 struct v3d_screen
*screen
= bo
->screen
;
191 struct timespec time
;
192 clock_gettime(CLOCK_MONOTONIC
, &time
);
193 mtx_lock(&screen
->bo_cache
.lock
);
194 v3d_bo_last_unreference_locked_timed(bo
, time
.tv_sec
);
195 mtx_unlock(&screen
->bo_cache
.lock
);
199 v3d_bo_free(struct v3d_bo
*bo
)
201 struct v3d_screen
*screen
= bo
->screen
;
204 if (using_v3d_simulator
&& bo
->name
&&
205 strcmp(bo
->name
, "winsys") == 0) {
208 munmap(bo
->map
, bo
->size
);
209 VG(VALGRIND_FREELIKE_BLOCK(bo
->map
, 0));
213 struct drm_gem_close c
;
214 memset(&c
, 0, sizeof(c
));
215 c
.handle
= bo
->handle
;
216 int ret
= v3d_ioctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &c
);
218 fprintf(stderr
, "close object %d: %s\n", bo
->handle
, strerror(errno
));
221 screen
->bo_size
-= bo
->size
;
224 fprintf(stderr
, "Freed %s%s%dkb:\n",
225 bo
->name
? bo
->name
: "",
228 v3d_bo_dump_stats(screen
);
235 free_stale_bos(struct v3d_screen
*screen
, time_t time
)
237 struct v3d_bo_cache
*cache
= &screen
->bo_cache
;
238 bool freed_any
= false;
240 list_for_each_entry_safe(struct v3d_bo
, bo
, &cache
->time_list
,
242 /* If it's more than a second old, free it. */
243 if (time
- bo
->free_time
> 2) {
244 if (dump_stats
&& !freed_any
) {
245 fprintf(stderr
, "Freeing stale BOs:\n");
246 v3d_bo_dump_stats(screen
);
249 v3d_bo_remove_from_cache(cache
, bo
);
256 if (dump_stats
&& freed_any
) {
257 fprintf(stderr
, "Freed stale BOs:\n");
258 v3d_bo_dump_stats(screen
);
263 v3d_bo_cache_free_all(struct v3d_bo_cache
*cache
)
265 mtx_lock(&cache
->lock
);
266 list_for_each_entry_safe(struct v3d_bo
, bo
, &cache
->time_list
,
268 v3d_bo_remove_from_cache(cache
, bo
);
271 mtx_unlock(&cache
->lock
);
275 v3d_bo_last_unreference_locked_timed(struct v3d_bo
*bo
, time_t time
)
277 struct v3d_screen
*screen
= bo
->screen
;
278 struct v3d_bo_cache
*cache
= &screen
->bo_cache
;
279 uint32_t page_index
= bo
->size
/ 4096 - 1;
286 if (cache
->size_list_size
<= page_index
) {
287 struct list_head
*new_list
=
288 ralloc_array(screen
, struct list_head
, page_index
+ 1);
290 /* Move old list contents over (since the array has moved, and
291 * therefore the pointers to the list heads have to change).
293 for (int i
= 0; i
< cache
->size_list_size
; i
++) {
294 struct list_head
*old_head
= &cache
->size_list
[i
];
295 if (list_is_empty(old_head
))
296 list_inithead(&new_list
[i
]);
298 new_list
[i
].next
= old_head
->next
;
299 new_list
[i
].prev
= old_head
->prev
;
300 new_list
[i
].next
->prev
= &new_list
[i
];
301 new_list
[i
].prev
->next
= &new_list
[i
];
304 for (int i
= cache
->size_list_size
; i
< page_index
+ 1; i
++)
305 list_inithead(&new_list
[i
]);
307 cache
->size_list
= new_list
;
308 cache
->size_list_size
= page_index
+ 1;
311 bo
->free_time
= time
;
312 list_addtail(&bo
->size_list
, &cache
->size_list
[page_index
]);
313 list_addtail(&bo
->time_list
, &cache
->time_list
);
315 fprintf(stderr
, "Freed %s %dkb to cache:\n",
316 bo
->name
, bo
->size
/ 1024);
317 v3d_bo_dump_stats(screen
);
321 free_stale_bos(screen
, time
);
324 static struct v3d_bo
*
325 v3d_bo_open_handle(struct v3d_screen
*screen
,
326 uint32_t handle
, uint32_t size
)
332 mtx_lock(&screen
->bo_handles_mutex
);
334 bo
= util_hash_table_get(screen
->bo_handles
, (void*)(uintptr_t)handle
);
336 pipe_reference(NULL
, &bo
->reference
);
340 bo
= CALLOC_STRUCT(v3d_bo
);
341 pipe_reference_init(&bo
->reference
, 1);
348 #ifdef USE_V3D_SIMULATOR
349 v3d_simulator_open_from_handle(screen
->fd
, bo
->handle
, bo
->size
);
350 bo
->map
= malloc(bo
->size
);
353 struct drm_v3d_get_bo_offset get
= {
356 int ret
= v3d_ioctl(screen
->fd
, DRM_IOCTL_V3D_GET_BO_OFFSET
, &get
);
358 fprintf(stderr
, "Failed to get BO offset: %s\n",
365 bo
->offset
= get
.offset
;
366 assert(bo
->offset
!= 0);
368 _mesa_hash_table_insert(screen
->bo_handles
, (void *)(uintptr_t)handle
, bo
);
371 screen
->bo_size
+= bo
->size
;
374 mtx_unlock(&screen
->bo_handles_mutex
);
379 v3d_bo_open_name(struct v3d_screen
*screen
, uint32_t name
)
381 struct drm_gem_open o
= {
384 int ret
= v3d_ioctl(screen
->fd
, DRM_IOCTL_GEM_OPEN
, &o
);
386 fprintf(stderr
, "Failed to open bo %d: %s\n",
387 name
, strerror(errno
));
391 return v3d_bo_open_handle(screen
, o
.handle
, o
.size
);
395 v3d_bo_open_dmabuf(struct v3d_screen
*screen
, int fd
)
398 int ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &handle
);
401 fprintf(stderr
, "Failed to get v3d handle for dmabuf %d\n", fd
);
405 /* Determine the size of the bo we were handed. */
406 size
= lseek(fd
, 0, SEEK_END
);
408 fprintf(stderr
, "Couldn't get size of dmabuf fd %d.\n", fd
);
412 return v3d_bo_open_handle(screen
, handle
, size
);
416 v3d_bo_get_dmabuf(struct v3d_bo
*bo
)
419 int ret
= drmPrimeHandleToFD(bo
->screen
->fd
, bo
->handle
,
422 fprintf(stderr
, "Failed to export gem bo %d to dmabuf\n",
427 mtx_lock(&bo
->screen
->bo_handles_mutex
);
429 _mesa_hash_table_insert(bo
->screen
->bo_handles
, (void *)(uintptr_t)bo
->handle
, bo
);
430 mtx_unlock(&bo
->screen
->bo_handles_mutex
);
436 v3d_bo_flink(struct v3d_bo
*bo
, uint32_t *name
)
438 struct drm_gem_flink flink
= {
439 .handle
= bo
->handle
,
441 int ret
= v3d_ioctl(bo
->screen
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
443 fprintf(stderr
, "Failed to flink bo %d: %s\n",
444 bo
->handle
, strerror(errno
));
455 static int v3d_wait_bo_ioctl(int fd
, uint32_t handle
, uint64_t timeout_ns
)
457 struct drm_v3d_wait_bo wait
= {
459 .timeout_ns
= timeout_ns
,
461 int ret
= v3d_ioctl(fd
, DRM_IOCTL_V3D_WAIT_BO
, &wait
);
470 v3d_bo_wait(struct v3d_bo
*bo
, uint64_t timeout_ns
, const char *reason
)
472 struct v3d_screen
*screen
= bo
->screen
;
474 if (unlikely(V3D_DEBUG
& V3D_DEBUG_PERF
) && timeout_ns
&& reason
) {
475 if (v3d_wait_bo_ioctl(screen
->fd
, bo
->handle
, 0) == -ETIME
) {
476 fprintf(stderr
, "Blocking on %s BO for %s\n",
481 int ret
= v3d_wait_bo_ioctl(screen
->fd
, bo
->handle
, timeout_ns
);
484 fprintf(stderr
, "wait failed: %d\n", ret
);
495 v3d_bo_map_unsynchronized(struct v3d_bo
*bo
)
503 struct drm_v3d_mmap_bo map
;
504 memset(&map
, 0, sizeof(map
));
505 map
.handle
= bo
->handle
;
506 ret
= v3d_ioctl(bo
->screen
->fd
, DRM_IOCTL_V3D_MMAP_BO
, &map
);
509 fprintf(stderr
, "map ioctl failure\n");
513 bo
->map
= mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
514 bo
->screen
->fd
, offset
);
515 if (bo
->map
== MAP_FAILED
) {
516 fprintf(stderr
, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
517 bo
->handle
, (long long)offset
, bo
->size
);
520 VG(VALGRIND_MALLOCLIKE_BLOCK(bo
->map
, bo
->size
, 0, false));
526 v3d_bo_map(struct v3d_bo
*bo
)
528 void *map
= v3d_bo_map_unsynchronized(bo
);
530 bool ok
= v3d_bo_wait(bo
, PIPE_TIMEOUT_INFINITE
, "bo map");
532 fprintf(stderr
, "BO wait for map failed\n");
540 v3d_bufmgr_destroy(struct pipe_screen
*pscreen
)
542 struct v3d_screen
*screen
= v3d_screen(pscreen
);
543 struct v3d_bo_cache
*cache
= &screen
->bo_cache
;
545 v3d_bo_cache_free_all(cache
);
548 fprintf(stderr
, "BO stats after screen destroy:\n");
549 v3d_bo_dump_stats(screen
);