Path: blob/21.2-virgl/src/freedreno/drm/freedreno_bo_cache.c
4564 views
/*1* Copyright (C) 2012-2018 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "freedreno_drmif.h"27#include "freedreno_priv.h"2829void bo_del(struct fd_bo *bo);30extern simple_mtx_t table_lock;3132static void33add_bucket(struct fd_bo_cache *cache, int size)34{35unsigned int i = cache->num_buckets;3637assert(i < ARRAY_SIZE(cache->cache_bucket));3839list_inithead(&cache->cache_bucket[i].list);40cache->cache_bucket[i].size = size;41cache->num_buckets++;42}4344/**45* @coarse: if true, only power-of-two bucket sizes, otherwise46* fill in for a bit smoother size curve..47*/48void49fd_bo_cache_init(struct fd_bo_cache *cache, int coarse)50{51unsigned long size, cache_max_size = 64 * 1024 * 1024;5253/* OK, so power of two buckets was too wasteful of memory.54* Give 3 other sizes between each power of two, to hopefully55* cover things accurately enough. (The alternative is56* probably to just go for exact matching of sizes, and assume57* that for things like composited window resize the tiled58* width/height alignment and rounding of sizes to pages will59* get us useful cache hit rates anyway)60*/61add_bucket(cache, 4096);62add_bucket(cache, 4096 * 2);63if (!coarse)64add_bucket(cache, 4096 * 3);6566/* Initialize the linked lists for BO reuse cache. */67for (size = 4 * 4096; size <= cache_max_size; size *= 2) {68add_bucket(cache, size);69if (!coarse) {70add_bucket(cache, size + size * 1 / 4);71add_bucket(cache, size + size * 2 / 4);72add_bucket(cache, size + size * 3 / 4);73}74}75}7677/* Frees older cached buffers. Called under table_lock */78void79fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time)80{81int i;8283if (cache->time == time)84return;8586for (i = 0; i < cache->num_buckets; i++) {87struct fd_bo_bucket *bucket = &cache->cache_bucket[i];88struct fd_bo *bo;8990while (!list_is_empty(&bucket->list)) {91bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);9293/* keep things in cache for at least 1 second: */94if (time && ((time - bo->free_time) <= 1))95break;9697VG_BO_OBTAIN(bo);98list_del(&bo->list);99bo_del(bo);100}101}102103cache->time = time;104}105106static struct fd_bo_bucket *107get_bucket(struct fd_bo_cache *cache, uint32_t size)108{109int i;110111/* hmm, this is what intel does, but I suppose we could calculate our112* way to the correct bucket size rather than looping..113*/114for (i = 0; i < cache->num_buckets; i++) {115struct fd_bo_bucket *bucket = &cache->cache_bucket[i];116if (bucket->size >= size) {117return bucket;118}119}120121return NULL;122}123124static struct fd_bo *125find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)126{127struct fd_bo *bo = NULL;128129/* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could130* skip the busy check.. if it is only going to be a render target131* then we probably don't need to stall..132*133* NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail134* (MRU, since likely to be in GPU cache), rather than head (LRU)..135*/136simple_mtx_lock(&table_lock);137if (!list_is_empty(&bucket->list)) {138bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);139/* TODO check for compatible flags? */140if (fd_bo_state(bo) == FD_BO_STATE_IDLE) {141list_del(&bo->list);142} else {143bo = NULL;144}145}146simple_mtx_unlock(&table_lock);147148return bo;149}150151/* NOTE: size is potentially rounded up to bucket size: */152struct fd_bo *153fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size, uint32_t flags)154{155struct fd_bo *bo = NULL;156struct fd_bo_bucket *bucket;157158*size = align(*size, 4096);159bucket = get_bucket(cache, *size);160161/* see if we can be green and recycle: */162retry:163if (bucket) {164*size = bucket->size;165bo = find_in_bucket(bucket, flags);166if (bo) {167VG_BO_OBTAIN(bo);168if (bo->funcs->madvise(bo, true) <= 0) {169/* we've lost the backing pages, delete and try again: */170simple_mtx_lock(&table_lock);171bo_del(bo);172simple_mtx_unlock(&table_lock);173goto retry;174}175p_atomic_set(&bo->refcnt, 1);176bo->flags = FD_RELOC_FLAGS_INIT;177return bo;178}179}180181return NULL;182}183184int185fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo)186{187struct fd_bo_bucket *bucket = get_bucket(cache, bo->size);188189/* see if we can be green and recycle: */190if (bucket) {191struct timespec time;192193bo->funcs->madvise(bo, false);194195clock_gettime(CLOCK_MONOTONIC, &time);196197bo->free_time = time.tv_sec;198VG_BO_RELEASE(bo);199list_addtail(&bo->list, &bucket->list);200fd_bo_cache_cleanup(cache, time.tv_sec);201202return 0;203}204205return -1;206}207208209