17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2019 Raspberry Pi 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 217ec681f3Smrg * IN THE SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#include "v3dv_private.h" 257ec681f3Smrg 267ec681f3Smrg#include <errno.h> 277ec681f3Smrg#include <sys/mman.h> 287ec681f3Smrg 297ec681f3Smrg#include "drm-uapi/v3d_drm.h" 307ec681f3Smrg#include "util/u_memory.h" 317ec681f3Smrg 327ec681f3Smrg/* Default max size of the bo cache, in MB. 337ec681f3Smrg * 347ec681f3Smrg * FIXME: we got this value when testing some apps using the rpi4 with 4GB, 357ec681f3Smrg * but it should depend on the total amount of RAM. But for that we would need 367ec681f3Smrg * to test on real hw with different amount of RAM. Using this value for now. 377ec681f3Smrg */ 387ec681f3Smrg#define DEFAULT_MAX_BO_CACHE_SIZE 512 397ec681f3Smrg 407ec681f3Smrg/* Discarded to use a V3D_DEBUG for this, as it would mean adding a run-time 417ec681f3Smrg * check for most of the calls 427ec681f3Smrg */ 437ec681f3Smrgstatic const bool dump_stats = false; 447ec681f3Smrg 457ec681f3Smrgstatic void 467ec681f3Smrgbo_dump_stats(struct v3dv_device *device) 477ec681f3Smrg{ 487ec681f3Smrg struct v3dv_bo_cache *cache = &device->bo_cache; 497ec681f3Smrg 507ec681f3Smrg fprintf(stderr, " BOs allocated: %d\n", device->bo_count); 517ec681f3Smrg fprintf(stderr, " BOs size: %dkb\n", device->bo_size / 1024); 527ec681f3Smrg fprintf(stderr, " BOs cached: %d\n", cache->cache_count); 537ec681f3Smrg fprintf(stderr, " BOs cached size: %dkb\n", cache->cache_size / 1024); 547ec681f3Smrg 557ec681f3Smrg if (!list_is_empty(&cache->time_list)) { 567ec681f3Smrg struct v3dv_bo *first = list_first_entry(&cache->time_list, 577ec681f3Smrg struct v3dv_bo, 587ec681f3Smrg time_list); 597ec681f3Smrg struct v3dv_bo *last = list_last_entry(&cache->time_list, 607ec681f3Smrg struct v3dv_bo, 617ec681f3Smrg time_list); 627ec681f3Smrg 637ec681f3Smrg fprintf(stderr, " oldest cache time: %ld\n", 647ec681f3Smrg (long)first->free_time); 657ec681f3Smrg fprintf(stderr, " newest cache time: %ld\n", 667ec681f3Smrg (long)last->free_time); 677ec681f3Smrg 687ec681f3Smrg struct timespec time; 697ec681f3Smrg clock_gettime(CLOCK_MONOTONIC, &time); 707ec681f3Smrg fprintf(stderr, " now: %ld\n", 717ec681f3Smrg time.tv_sec); 727ec681f3Smrg } 737ec681f3Smrg 747ec681f3Smrg if (cache->size_list_size) { 757ec681f3Smrg uint32_t empty_size_list = 0; 767ec681f3Smrg for (uint32_t i = 0; i < cache->size_list_size; i++) { 777ec681f3Smrg if (list_is_empty(&cache->size_list[i])) 787ec681f3Smrg empty_size_list++; 797ec681f3Smrg } 807ec681f3Smrg fprintf(stderr, " Empty size_list lists: %d\n", empty_size_list); 817ec681f3Smrg } 827ec681f3Smrg} 837ec681f3Smrg 847ec681f3Smrgstatic void 857ec681f3Smrgbo_remove_from_cache(struct v3dv_bo_cache *cache, struct v3dv_bo *bo) 867ec681f3Smrg{ 877ec681f3Smrg list_del(&bo->time_list); 887ec681f3Smrg list_del(&bo->size_list); 897ec681f3Smrg 907ec681f3Smrg cache->cache_count--; 917ec681f3Smrg cache->cache_size -= bo->size; 927ec681f3Smrg} 937ec681f3Smrg 947ec681f3Smrgstatic struct v3dv_bo * 957ec681f3Smrgbo_from_cache(struct v3dv_device *device, uint32_t size, const char *name) 967ec681f3Smrg{ 977ec681f3Smrg struct v3dv_bo_cache *cache = &device->bo_cache; 987ec681f3Smrg uint32_t page_index = size / 4096 - 1; 997ec681f3Smrg 1007ec681f3Smrg if (cache->size_list_size <= page_index) 1017ec681f3Smrg return NULL; 1027ec681f3Smrg 1037ec681f3Smrg struct v3dv_bo *bo = NULL; 1047ec681f3Smrg 1057ec681f3Smrg mtx_lock(&cache->lock); 1067ec681f3Smrg if (!list_is_empty(&cache->size_list[page_index])) { 1077ec681f3Smrg bo = list_first_entry(&cache->size_list[page_index], 1087ec681f3Smrg struct v3dv_bo, size_list); 1097ec681f3Smrg 1107ec681f3Smrg /* Check that the BO has gone idle. If not, then we want to 1117ec681f3Smrg * allocate something new instead, since we assume that the 1127ec681f3Smrg * user will proceed to CPU map it and fill it with stuff. 1137ec681f3Smrg */ 1147ec681f3Smrg if (!v3dv_bo_wait(device, bo, 0)) { 1157ec681f3Smrg mtx_unlock(&cache->lock); 1167ec681f3Smrg return NULL; 1177ec681f3Smrg } 1187ec681f3Smrg 1197ec681f3Smrg bo_remove_from_cache(cache, bo); 1207ec681f3Smrg 1217ec681f3Smrg bo->name = name; 1227ec681f3Smrg } 1237ec681f3Smrg mtx_unlock(&cache->lock); 1247ec681f3Smrg return bo; 1257ec681f3Smrg} 1267ec681f3Smrg 1277ec681f3Smrgstatic bool 1287ec681f3Smrgbo_free(struct v3dv_device *device, 1297ec681f3Smrg struct v3dv_bo *bo) 1307ec681f3Smrg{ 1317ec681f3Smrg if (!bo) 1327ec681f3Smrg return true; 1337ec681f3Smrg 1347ec681f3Smrg if (bo->map) 1357ec681f3Smrg v3dv_bo_unmap(device, bo); 1367ec681f3Smrg 1377ec681f3Smrg struct drm_gem_close c; 1387ec681f3Smrg memset(&c, 0, sizeof(c)); 1397ec681f3Smrg c.handle = bo->handle; 1407ec681f3Smrg int ret = v3dv_ioctl(device->pdevice->render_fd, DRM_IOCTL_GEM_CLOSE, &c); 1417ec681f3Smrg if (ret != 0) 1427ec681f3Smrg fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno)); 1437ec681f3Smrg 1447ec681f3Smrg device->bo_count--; 1457ec681f3Smrg device->bo_size -= bo->size; 1467ec681f3Smrg 1477ec681f3Smrg if (dump_stats) { 1487ec681f3Smrg fprintf(stderr, "Freed %s%s%dkb:\n", 1497ec681f3Smrg bo->name ? bo->name : "", 1507ec681f3Smrg bo->name ? " " : "", 1517ec681f3Smrg bo->size / 1024); 1527ec681f3Smrg bo_dump_stats(device); 1537ec681f3Smrg } 1547ec681f3Smrg 1557ec681f3Smrg vk_free(&device->vk.alloc, bo); 1567ec681f3Smrg 1577ec681f3Smrg return ret == 0; 1587ec681f3Smrg} 1597ec681f3Smrg 1607ec681f3Smrgstatic void 1617ec681f3Smrgbo_cache_free_all(struct v3dv_device *device, 1627ec681f3Smrg bool with_lock) 1637ec681f3Smrg{ 1647ec681f3Smrg struct v3dv_bo_cache *cache = &device->bo_cache; 1657ec681f3Smrg 1667ec681f3Smrg if (with_lock) 1677ec681f3Smrg mtx_lock(&cache->lock); 1687ec681f3Smrg list_for_each_entry_safe(struct v3dv_bo, bo, &cache->time_list, 1697ec681f3Smrg time_list) { 1707ec681f3Smrg bo_remove_from_cache(cache, bo); 1717ec681f3Smrg bo_free(device, bo); 1727ec681f3Smrg } 1737ec681f3Smrg if (with_lock) 1747ec681f3Smrg mtx_unlock(&cache->lock); 1757ec681f3Smrg 1767ec681f3Smrg} 1777ec681f3Smrg 1787ec681f3Smrgvoid 1797ec681f3Smrgv3dv_bo_init(struct v3dv_bo *bo, 1807ec681f3Smrg uint32_t handle, 1817ec681f3Smrg uint32_t size, 1827ec681f3Smrg uint32_t offset, 1837ec681f3Smrg const char *name, 1847ec681f3Smrg bool private) 1857ec681f3Smrg{ 1867ec681f3Smrg bo->handle = handle; 1877ec681f3Smrg bo->handle_bit = 1ull << (handle % 64); 1887ec681f3Smrg bo->size = size; 1897ec681f3Smrg bo->offset = offset; 1907ec681f3Smrg bo->map = NULL; 1917ec681f3Smrg bo->map_size = 0; 1927ec681f3Smrg bo->name = name; 1937ec681f3Smrg bo->private = private; 1947ec681f3Smrg bo->dumb_handle = -1; 1957ec681f3Smrg list_inithead(&bo->list_link); 1967ec681f3Smrg} 1977ec681f3Smrg 1987ec681f3Smrgstruct v3dv_bo * 1997ec681f3Smrgv3dv_bo_alloc(struct v3dv_device *device, 2007ec681f3Smrg uint32_t size, 2017ec681f3Smrg const char *name, 2027ec681f3Smrg bool private) 2037ec681f3Smrg{ 2047ec681f3Smrg struct v3dv_bo *bo; 2057ec681f3Smrg 2067ec681f3Smrg const uint32_t page_align = 4096; /* Always allocate full pages */ 2077ec681f3Smrg size = align(size, page_align); 2087ec681f3Smrg 2097ec681f3Smrg if (private) { 2107ec681f3Smrg bo = bo_from_cache(device, size, name); 2117ec681f3Smrg if (bo) { 2127ec681f3Smrg if (dump_stats) { 2137ec681f3Smrg fprintf(stderr, "Allocated %s %dkb from cache:\n", 2147ec681f3Smrg name, size / 1024); 2157ec681f3Smrg bo_dump_stats(device); 2167ec681f3Smrg } 2177ec681f3Smrg return bo; 2187ec681f3Smrg } 2197ec681f3Smrg } 2207ec681f3Smrg 2217ec681f3Smrg bo = vk_alloc(&device->vk.alloc, sizeof(struct v3dv_bo), 8, 2227ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); 2237ec681f3Smrg 2247ec681f3Smrg if (!bo) { 2257ec681f3Smrg fprintf(stderr, "Failed to allocate host memory for BO\n"); 2267ec681f3Smrg return NULL; 2277ec681f3Smrg } 2287ec681f3Smrg 2297ec681f3Smrg retry: 2307ec681f3Smrg ; 2317ec681f3Smrg 2327ec681f3Smrg bool cleared_and_retried = false; 2337ec681f3Smrg struct drm_v3d_create_bo create = { 2347ec681f3Smrg .size = size 2357ec681f3Smrg }; 2367ec681f3Smrg 2377ec681f3Smrg int ret = v3dv_ioctl(device->pdevice->render_fd, 2387ec681f3Smrg DRM_IOCTL_V3D_CREATE_BO, &create); 2397ec681f3Smrg if (ret != 0) { 2407ec681f3Smrg if (!list_is_empty(&device->bo_cache.time_list) && 2417ec681f3Smrg !cleared_and_retried) { 2427ec681f3Smrg cleared_and_retried = true; 2437ec681f3Smrg bo_cache_free_all(device, true); 2447ec681f3Smrg goto retry; 2457ec681f3Smrg } 2467ec681f3Smrg 2477ec681f3Smrg vk_free(&device->vk.alloc, bo); 2487ec681f3Smrg fprintf(stderr, "Failed to allocate device memory for BO\n"); 2497ec681f3Smrg return NULL; 2507ec681f3Smrg } 2517ec681f3Smrg 2527ec681f3Smrg assert(create.offset % page_align == 0); 2537ec681f3Smrg assert((create.offset & 0xffffffff) == create.offset); 2547ec681f3Smrg 2557ec681f3Smrg v3dv_bo_init(bo, create.handle, size, create.offset, name, private); 2567ec681f3Smrg 2577ec681f3Smrg device->bo_count++; 2587ec681f3Smrg device->bo_size += bo->size; 2597ec681f3Smrg if (dump_stats) { 2607ec681f3Smrg fprintf(stderr, "Allocated %s %dkb:\n", name, size / 1024); 2617ec681f3Smrg bo_dump_stats(device); 2627ec681f3Smrg } 2637ec681f3Smrg 2647ec681f3Smrg return bo; 2657ec681f3Smrg} 2667ec681f3Smrg 2677ec681f3Smrgbool 2687ec681f3Smrgv3dv_bo_map_unsynchronized(struct v3dv_device *device, 2697ec681f3Smrg struct v3dv_bo *bo, 2707ec681f3Smrg uint32_t size) 2717ec681f3Smrg{ 2727ec681f3Smrg assert(bo != NULL && size <= bo->size); 2737ec681f3Smrg 2747ec681f3Smrg if (bo->map) 2757ec681f3Smrg return bo->map; 2767ec681f3Smrg 2777ec681f3Smrg struct drm_v3d_mmap_bo map; 2787ec681f3Smrg memset(&map, 0, sizeof(map)); 2797ec681f3Smrg map.handle = bo->handle; 2807ec681f3Smrg int ret = v3dv_ioctl(device->pdevice->render_fd, 2817ec681f3Smrg DRM_IOCTL_V3D_MMAP_BO, &map); 2827ec681f3Smrg if (ret != 0) { 2837ec681f3Smrg fprintf(stderr, "map ioctl failure\n"); 2847ec681f3Smrg return false; 2857ec681f3Smrg } 2867ec681f3Smrg 2877ec681f3Smrg bo->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, 2887ec681f3Smrg device->pdevice->render_fd, map.offset); 2897ec681f3Smrg if (bo->map == MAP_FAILED) { 2907ec681f3Smrg fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n", 2917ec681f3Smrg bo->handle, (long long)map.offset, (uint32_t)bo->size); 2927ec681f3Smrg return false; 2937ec681f3Smrg } 2947ec681f3Smrg VG(VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, false)); 2957ec681f3Smrg 2967ec681f3Smrg bo->map_size = size; 2977ec681f3Smrg 2987ec681f3Smrg return true; 2997ec681f3Smrg} 3007ec681f3Smrg 3017ec681f3Smrgbool 3027ec681f3Smrgv3dv_bo_wait(struct v3dv_device *device, 3037ec681f3Smrg struct v3dv_bo *bo, 3047ec681f3Smrg uint64_t timeout_ns) 3057ec681f3Smrg{ 3067ec681f3Smrg struct drm_v3d_wait_bo wait = { 3077ec681f3Smrg .handle = bo->handle, 3087ec681f3Smrg .timeout_ns = timeout_ns, 3097ec681f3Smrg }; 3107ec681f3Smrg return v3dv_ioctl(device->pdevice->render_fd, 3117ec681f3Smrg DRM_IOCTL_V3D_WAIT_BO, &wait) == 0; 3127ec681f3Smrg} 3137ec681f3Smrg 3147ec681f3Smrgbool 3157ec681f3Smrgv3dv_bo_map(struct v3dv_device *device, struct v3dv_bo *bo, uint32_t size) 3167ec681f3Smrg{ 3177ec681f3Smrg assert(bo && size <= bo->size); 3187ec681f3Smrg 3197ec681f3Smrg bool ok = v3dv_bo_map_unsynchronized(device, bo, size); 3207ec681f3Smrg if (!ok) 3217ec681f3Smrg return false; 3227ec681f3Smrg 3237ec681f3Smrg ok = v3dv_bo_wait(device, bo, PIPE_TIMEOUT_INFINITE); 3247ec681f3Smrg if (!ok) { 3257ec681f3Smrg fprintf(stderr, "memory wait for map failed\n"); 3267ec681f3Smrg return false; 3277ec681f3Smrg } 3287ec681f3Smrg 3297ec681f3Smrg return true; 3307ec681f3Smrg} 3317ec681f3Smrg 3327ec681f3Smrgvoid 3337ec681f3Smrgv3dv_bo_unmap(struct v3dv_device *device, struct v3dv_bo *bo) 3347ec681f3Smrg{ 3357ec681f3Smrg assert(bo && bo->map && bo->map_size > 0); 3367ec681f3Smrg 3377ec681f3Smrg munmap(bo->map, bo->map_size); 3387ec681f3Smrg VG(VALGRIND_FREELIKE_BLOCK(bo->map, 0)); 3397ec681f3Smrg bo->map = NULL; 3407ec681f3Smrg bo->map_size = 0; 3417ec681f3Smrg} 3427ec681f3Smrg 3437ec681f3Smrgstatic boolean 3447ec681f3Smrgreallocate_size_list(struct v3dv_bo_cache *cache, 3457ec681f3Smrg struct v3dv_device *device, 3467ec681f3Smrg uint32_t size) 3477ec681f3Smrg{ 3487ec681f3Smrg struct list_head *new_list = 3497ec681f3Smrg vk_alloc(&device->vk.alloc, sizeof(struct list_head) * size, 8, 3507ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); 3517ec681f3Smrg 3527ec681f3Smrg if (!new_list) { 3537ec681f3Smrg fprintf(stderr, "Failed to allocate host memory for cache bo list\n"); 3547ec681f3Smrg return false; 3557ec681f3Smrg } 3567ec681f3Smrg struct list_head *old_list = cache->size_list; 3577ec681f3Smrg 3587ec681f3Smrg /* Move old list contents over (since the array has moved, and 3597ec681f3Smrg * therefore the pointers to the list heads have to change). 3607ec681f3Smrg */ 3617ec681f3Smrg for (int i = 0; i < cache->size_list_size; i++) { 3627ec681f3Smrg struct list_head *old_head = &cache->size_list[i]; 3637ec681f3Smrg if (list_is_empty(old_head)) { 3647ec681f3Smrg list_inithead(&new_list[i]); 3657ec681f3Smrg } else { 3667ec681f3Smrg new_list[i].next = old_head->next; 3677ec681f3Smrg new_list[i].prev = old_head->prev; 3687ec681f3Smrg new_list[i].next->prev = &new_list[i]; 3697ec681f3Smrg new_list[i].prev->next = &new_list[i]; 3707ec681f3Smrg } 3717ec681f3Smrg } 3727ec681f3Smrg for (int i = cache->size_list_size; i < size; i++) 3737ec681f3Smrg list_inithead(&new_list[i]); 3747ec681f3Smrg 3757ec681f3Smrg cache->size_list = new_list; 3767ec681f3Smrg cache->size_list_size = size; 3777ec681f3Smrg vk_free(&device->vk.alloc, old_list); 3787ec681f3Smrg 3797ec681f3Smrg return true; 3807ec681f3Smrg} 3817ec681f3Smrg 3827ec681f3Smrgvoid 3837ec681f3Smrgv3dv_bo_cache_init(struct v3dv_device *device) 3847ec681f3Smrg{ 3857ec681f3Smrg device->bo_size = 0; 3867ec681f3Smrg device->bo_count = 0; 3877ec681f3Smrg list_inithead(&device->bo_cache.time_list); 3887ec681f3Smrg /* FIXME: perhaps set a initial size for the size-list, to avoid run-time 3897ec681f3Smrg * reallocations 3907ec681f3Smrg */ 3917ec681f3Smrg device->bo_cache.size_list_size = 0; 3927ec681f3Smrg 3937ec681f3Smrg const char *max_cache_size_str = getenv("V3DV_MAX_BO_CACHE_SIZE"); 3947ec681f3Smrg if (max_cache_size_str == NULL) 3957ec681f3Smrg device->bo_cache.max_cache_size = DEFAULT_MAX_BO_CACHE_SIZE; 3967ec681f3Smrg else 3977ec681f3Smrg device->bo_cache.max_cache_size = atoll(max_cache_size_str); 3987ec681f3Smrg 3997ec681f3Smrg if (dump_stats) { 4007ec681f3Smrg fprintf(stderr, "MAX BO CACHE SIZE: %iMB\n", device->bo_cache.max_cache_size); 4017ec681f3Smrg } 4027ec681f3Smrg 4037ec681f3Smrg device->bo_cache.max_cache_size *= 1024 * 1024; 4047ec681f3Smrg device->bo_cache.cache_count = 0; 4057ec681f3Smrg device->bo_cache.cache_size = 0; 4067ec681f3Smrg} 4077ec681f3Smrg 4087ec681f3Smrgvoid 4097ec681f3Smrgv3dv_bo_cache_destroy(struct v3dv_device *device) 4107ec681f3Smrg{ 4117ec681f3Smrg bo_cache_free_all(device, true); 4127ec681f3Smrg vk_free(&device->vk.alloc, device->bo_cache.size_list); 4137ec681f3Smrg 4147ec681f3Smrg if (dump_stats) { 4157ec681f3Smrg fprintf(stderr, "BO stats after screen destroy:\n"); 4167ec681f3Smrg bo_dump_stats(device); 4177ec681f3Smrg } 4187ec681f3Smrg} 4197ec681f3Smrg 4207ec681f3Smrg 4217ec681f3Smrgstatic void 4227ec681f3Smrgfree_stale_bos(struct v3dv_device *device, 4237ec681f3Smrg time_t time) 4247ec681f3Smrg{ 4257ec681f3Smrg struct v3dv_bo_cache *cache = &device->bo_cache; 4267ec681f3Smrg bool freed_any = false; 4277ec681f3Smrg 4287ec681f3Smrg list_for_each_entry_safe(struct v3dv_bo, bo, &cache->time_list, 4297ec681f3Smrg time_list) { 4307ec681f3Smrg /* If it's more than a second old, free it. */ 4317ec681f3Smrg if (time - bo->free_time > 2) { 4327ec681f3Smrg if (dump_stats && !freed_any) { 4337ec681f3Smrg fprintf(stderr, "Freeing stale BOs:\n"); 4347ec681f3Smrg bo_dump_stats(device); 4357ec681f3Smrg freed_any = true; 4367ec681f3Smrg } 4377ec681f3Smrg 4387ec681f3Smrg bo_remove_from_cache(cache, bo); 4397ec681f3Smrg bo_free(device, bo); 4407ec681f3Smrg } else { 4417ec681f3Smrg break; 4427ec681f3Smrg } 4437ec681f3Smrg } 4447ec681f3Smrg 4457ec681f3Smrg if (dump_stats && freed_any) { 4467ec681f3Smrg fprintf(stderr, "Freed stale BOs:\n"); 4477ec681f3Smrg bo_dump_stats(device); 4487ec681f3Smrg } 4497ec681f3Smrg} 4507ec681f3Smrg 4517ec681f3Smrgbool 4527ec681f3Smrgv3dv_bo_free(struct v3dv_device *device, 4537ec681f3Smrg struct v3dv_bo *bo) 4547ec681f3Smrg{ 4557ec681f3Smrg if (!bo) 4567ec681f3Smrg return true; 4577ec681f3Smrg 4587ec681f3Smrg struct timespec time; 4597ec681f3Smrg struct v3dv_bo_cache *cache = &device->bo_cache; 4607ec681f3Smrg uint32_t page_index = bo->size / 4096 - 1; 4617ec681f3Smrg 4627ec681f3Smrg if (bo->private && 4637ec681f3Smrg bo->size > cache->max_cache_size - cache->cache_size) { 4647ec681f3Smrg clock_gettime(CLOCK_MONOTONIC, &time); 4657ec681f3Smrg mtx_lock(&cache->lock); 4667ec681f3Smrg free_stale_bos(device, time.tv_sec); 4677ec681f3Smrg mtx_unlock(&cache->lock); 4687ec681f3Smrg } 4697ec681f3Smrg 4707ec681f3Smrg if (!bo->private || 4717ec681f3Smrg bo->size > cache->max_cache_size - cache->cache_size) { 4727ec681f3Smrg return bo_free(device, bo); 4737ec681f3Smrg } 4747ec681f3Smrg 4757ec681f3Smrg clock_gettime(CLOCK_MONOTONIC, &time); 4767ec681f3Smrg mtx_lock(&cache->lock); 4777ec681f3Smrg 4787ec681f3Smrg if (cache->size_list_size <= page_index) { 4797ec681f3Smrg if (!reallocate_size_list(cache, device, page_index + 1)) { 4807ec681f3Smrg bool outcome = bo_free(device, bo); 4817ec681f3Smrg /* If the reallocation failed, it usually means that we are out of 4827ec681f3Smrg * memory, so we also free all the bo cache. We need to call it to 4837ec681f3Smrg * not use the cache lock, as we are already under it. 4847ec681f3Smrg */ 4857ec681f3Smrg bo_cache_free_all(device, false); 4867ec681f3Smrg mtx_unlock(&cache->lock); 4877ec681f3Smrg return outcome; 4887ec681f3Smrg } 4897ec681f3Smrg } 4907ec681f3Smrg 4917ec681f3Smrg bo->free_time = time.tv_sec; 4927ec681f3Smrg list_addtail(&bo->size_list, &cache->size_list[page_index]); 4937ec681f3Smrg list_addtail(&bo->time_list, &cache->time_list); 4947ec681f3Smrg 4957ec681f3Smrg cache->cache_count++; 4967ec681f3Smrg cache->cache_size += bo->size; 4977ec681f3Smrg 4987ec681f3Smrg if (dump_stats) { 4997ec681f3Smrg fprintf(stderr, "Freed %s %dkb to cache:\n", 5007ec681f3Smrg bo->name, bo->size / 1024); 5017ec681f3Smrg bo_dump_stats(device); 5027ec681f3Smrg } 5037ec681f3Smrg bo->name = NULL; 5047ec681f3Smrg 5057ec681f3Smrg free_stale_bos(device, time.tv_sec); 5067ec681f3Smrg 5077ec681f3Smrg mtx_unlock(&cache->lock); 5087ec681f3Smrg 5097ec681f3Smrg return true; 5107ec681f3Smrg} 511