2
* Copyright © 2019 Raspberry Pi Ltd
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
24
#include "v3dv_private.h"
29
#include "drm-uapi/v3d_drm.h"
30
#include "util/u_memory.h"
32
/* Default max size of the bo cache, in MB.
34
* FIXME: we got this value when testing some apps using the rpi4 with 4GB,
35
* but it should depend on the total amount of RAM. But for that we would need
36
* to test on real hw with different amount of RAM. Using this value for now.
38
#define DEFAULT_MAX_BO_CACHE_SIZE 512
40
/* Discarded to use a V3D_DEBUG for this, as it would mean adding a run-time
41
* check for most of the calls
43
static const bool dump_stats = false;
46
bo_dump_stats(struct v3dv_device *device)
48
struct v3dv_bo_cache *cache = &device->bo_cache;
50
fprintf(stderr, " BOs allocated: %d\n", device->bo_count);
51
fprintf(stderr, " BOs size: %dkb\n", device->bo_size / 1024);
52
fprintf(stderr, " BOs cached: %d\n", cache->cache_count);
53
fprintf(stderr, " BOs cached size: %dkb\n", cache->cache_size / 1024);
55
if (!list_is_empty(&cache->time_list)) {
56
struct v3dv_bo *first = list_first_entry(&cache->time_list,
59
struct v3dv_bo *last = list_last_entry(&cache->time_list,
63
fprintf(stderr, " oldest cache time: %ld\n",
64
(long)first->free_time);
65
fprintf(stderr, " newest cache time: %ld\n",
66
(long)last->free_time);
69
clock_gettime(CLOCK_MONOTONIC, &time);
70
fprintf(stderr, " now: %lld\n",
71
(long long)time.tv_sec);
74
if (cache->size_list_size) {
75
uint32_t empty_size_list = 0;
76
for (uint32_t i = 0; i < cache->size_list_size; i++) {
77
if (list_is_empty(&cache->size_list[i]))
80
fprintf(stderr, " Empty size_list lists: %d\n", empty_size_list);
85
bo_remove_from_cache(struct v3dv_bo_cache *cache, struct v3dv_bo *bo)
87
list_del(&bo->time_list);
88
list_del(&bo->size_list);
91
cache->cache_size -= bo->size;
94
static struct v3dv_bo *
95
bo_from_cache(struct v3dv_device *device, uint32_t size, const char *name)
97
struct v3dv_bo_cache *cache = &device->bo_cache;
98
uint32_t page_index = size / 4096 - 1;
100
if (cache->size_list_size <= page_index)
103
struct v3dv_bo *bo = NULL;
105
mtx_lock(&cache->lock);
106
if (!list_is_empty(&cache->size_list[page_index])) {
107
bo = list_first_entry(&cache->size_list[page_index],
108
struct v3dv_bo, size_list);
110
/* Check that the BO has gone idle. If not, then we want to
111
* allocate something new instead, since we assume that the
112
* user will proceed to CPU map it and fill it with stuff.
114
if (!v3dv_bo_wait(device, bo, 0)) {
115
mtx_unlock(&cache->lock);
119
bo_remove_from_cache(cache, bo);
121
p_atomic_set(&bo->refcnt, 1);
123
mtx_unlock(&cache->lock);
128
bo_free(struct v3dv_device *device,
134
assert(p_atomic_read(&bo->refcnt) == 0);
137
v3dv_bo_unmap(device, bo);
139
/* Our BO structs are stored in a sparse array in the physical device,
140
* so we don't want to free the BO pointer, instead we want to reset it
141
* to 0, to signal that array entry as being free.
143
uint32_t handle = bo->handle;
144
memset(bo, 0, sizeof(*bo));
146
struct drm_gem_close c;
147
memset(&c, 0, sizeof(c));
149
int ret = v3dv_ioctl(device->pdevice->render_fd, DRM_IOCTL_GEM_CLOSE, &c);
151
fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno));
154
device->bo_size -= bo->size;
157
fprintf(stderr, "Freed %s%s%dkb:\n",
158
bo->name ? bo->name : "",
161
bo_dump_stats(device);
168
bo_cache_free_all(struct v3dv_device *device,
171
struct v3dv_bo_cache *cache = &device->bo_cache;
174
mtx_lock(&cache->lock);
175
list_for_each_entry_safe(struct v3dv_bo, bo, &cache->time_list,
177
bo_remove_from_cache(cache, bo);
181
mtx_unlock(&cache->lock);
186
v3dv_bo_init(struct v3dv_bo *bo,
193
p_atomic_set(&bo->refcnt, 1);
195
bo->handle_bit = 1ull << (handle % 64);
201
bo->private = private;
202
bo->dumb_handle = -1;
203
list_inithead(&bo->list_link);
207
v3dv_bo_alloc(struct v3dv_device *device,
214
const uint32_t page_align = 4096; /* Always allocate full pages */
215
size = align(size, page_align);
218
bo = bo_from_cache(device, size, name);
221
fprintf(stderr, "Allocated %s %dkb from cache:\n",
223
bo_dump_stats(device);
232
bool cleared_and_retried = false;
233
struct drm_v3d_create_bo create = {
237
int ret = v3dv_ioctl(device->pdevice->render_fd,
238
DRM_IOCTL_V3D_CREATE_BO, &create);
240
if (!list_is_empty(&device->bo_cache.time_list) &&
241
!cleared_and_retried) {
242
cleared_and_retried = true;
243
bo_cache_free_all(device, true);
247
fprintf(stderr, "Failed to allocate device memory for BO\n");
251
assert(create.offset % page_align == 0);
252
assert((create.offset & 0xffffffff) == create.offset);
254
bo = v3dv_device_lookup_bo(device->pdevice, create.handle);
255
assert(bo && bo->handle == 0);
257
v3dv_bo_init(bo, create.handle, size, create.offset, name, private);
260
device->bo_size += bo->size;
262
fprintf(stderr, "Allocated %s %dkb:\n", name, size / 1024);
263
bo_dump_stats(device);
270
v3dv_bo_map_unsynchronized(struct v3dv_device *device,
274
assert(bo != NULL && size <= bo->size);
279
struct drm_v3d_mmap_bo map;
280
memset(&map, 0, sizeof(map));
281
map.handle = bo->handle;
282
int ret = v3dv_ioctl(device->pdevice->render_fd,
283
DRM_IOCTL_V3D_MMAP_BO, &map);
285
fprintf(stderr, "map ioctl failure\n");
289
bo->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
290
device->pdevice->render_fd, map.offset);
291
if (bo->map == MAP_FAILED) {
292
fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
293
bo->handle, (long long)map.offset, (uint32_t)bo->size);
296
VG(VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, false));
304
v3dv_bo_wait(struct v3dv_device *device,
308
struct drm_v3d_wait_bo wait = {
309
.handle = bo->handle,
310
.timeout_ns = timeout_ns,
312
return v3dv_ioctl(device->pdevice->render_fd,
313
DRM_IOCTL_V3D_WAIT_BO, &wait) == 0;
317
v3dv_bo_map(struct v3dv_device *device, struct v3dv_bo *bo, uint32_t size)
319
assert(bo && size <= bo->size);
321
bool ok = v3dv_bo_map_unsynchronized(device, bo, size);
325
ok = v3dv_bo_wait(device, bo, PIPE_TIMEOUT_INFINITE);
327
fprintf(stderr, "memory wait for map failed\n");
335
v3dv_bo_unmap(struct v3dv_device *device, struct v3dv_bo *bo)
337
assert(bo && bo->map && bo->map_size > 0);
339
munmap(bo->map, bo->map_size);
340
VG(VALGRIND_FREELIKE_BLOCK(bo->map, 0));
346
reallocate_size_list(struct v3dv_bo_cache *cache,
347
struct v3dv_device *device,
350
struct list_head *new_list =
351
vk_alloc(&device->vk.alloc, sizeof(struct list_head) * size, 8,
352
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
355
fprintf(stderr, "Failed to allocate host memory for cache bo list\n");
358
struct list_head *old_list = cache->size_list;
360
/* Move old list contents over (since the array has moved, and
361
* therefore the pointers to the list heads have to change).
363
for (int i = 0; i < cache->size_list_size; i++) {
364
struct list_head *old_head = &cache->size_list[i];
365
if (list_is_empty(old_head)) {
366
list_inithead(&new_list[i]);
368
new_list[i].next = old_head->next;
369
new_list[i].prev = old_head->prev;
370
new_list[i].next->prev = &new_list[i];
371
new_list[i].prev->next = &new_list[i];
374
for (int i = cache->size_list_size; i < size; i++)
375
list_inithead(&new_list[i]);
377
cache->size_list = new_list;
378
cache->size_list_size = size;
379
vk_free(&device->vk.alloc, old_list);
385
v3dv_bo_cache_init(struct v3dv_device *device)
388
device->bo_count = 0;
389
list_inithead(&device->bo_cache.time_list);
390
/* FIXME: perhaps set a initial size for the size-list, to avoid run-time
393
device->bo_cache.size_list_size = 0;
395
const char *max_cache_size_str = getenv("V3DV_MAX_BO_CACHE_SIZE");
396
if (max_cache_size_str == NULL)
397
device->bo_cache.max_cache_size = DEFAULT_MAX_BO_CACHE_SIZE;
399
device->bo_cache.max_cache_size = atoll(max_cache_size_str);
402
fprintf(stderr, "MAX BO CACHE SIZE: %iMB\n", device->bo_cache.max_cache_size);
405
device->bo_cache.max_cache_size *= 1024 * 1024;
406
device->bo_cache.cache_count = 0;
407
device->bo_cache.cache_size = 0;
411
v3dv_bo_cache_destroy(struct v3dv_device *device)
413
bo_cache_free_all(device, true);
414
vk_free(&device->vk.alloc, device->bo_cache.size_list);
417
fprintf(stderr, "BO stats after screen destroy:\n");
418
bo_dump_stats(device);
424
free_stale_bos(struct v3dv_device *device,
427
struct v3dv_bo_cache *cache = &device->bo_cache;
428
bool freed_any = false;
430
list_for_each_entry_safe(struct v3dv_bo, bo, &cache->time_list,
432
/* If it's more than a second old, free it. */
433
if (time - bo->free_time > 2) {
434
if (dump_stats && !freed_any) {
435
fprintf(stderr, "Freeing stale BOs:\n");
436
bo_dump_stats(device);
440
bo_remove_from_cache(cache, bo);
447
if (dump_stats && freed_any) {
448
fprintf(stderr, "Freed stale BOs:\n");
449
bo_dump_stats(device);
454
v3dv_bo_free(struct v3dv_device *device,
460
if (!p_atomic_dec_zero(&bo->refcnt))
463
struct timespec time;
464
struct v3dv_bo_cache *cache = &device->bo_cache;
465
uint32_t page_index = bo->size / 4096 - 1;
468
bo->size > cache->max_cache_size - cache->cache_size) {
469
clock_gettime(CLOCK_MONOTONIC, &time);
470
mtx_lock(&cache->lock);
471
free_stale_bos(device, time.tv_sec);
472
mtx_unlock(&cache->lock);
476
bo->size > cache->max_cache_size - cache->cache_size) {
477
return bo_free(device, bo);
480
clock_gettime(CLOCK_MONOTONIC, &time);
481
mtx_lock(&cache->lock);
483
if (cache->size_list_size <= page_index) {
484
if (!reallocate_size_list(cache, device, page_index + 1)) {
485
bool outcome = bo_free(device, bo);
486
/* If the reallocation failed, it usually means that we are out of
487
* memory, so we also free all the bo cache. We need to call it to
488
* not use the cache lock, as we are already under it.
490
bo_cache_free_all(device, false);
491
mtx_unlock(&cache->lock);
496
bo->free_time = time.tv_sec;
497
list_addtail(&bo->size_list, &cache->size_list[page_index]);
498
list_addtail(&bo->time_list, &cache->time_list);
500
cache->cache_count++;
501
cache->cache_size += bo->size;
504
fprintf(stderr, "Freed %s %dkb to cache:\n",
505
bo->name, bo->size / 1024);
506
bo_dump_stats(device);
510
free_stale_bos(device, time.tv_sec);
512
mtx_unlock(&cache->lock);