Path: blob/21.2-virgl/src/etnaviv/drm/etnaviv_bo_cache.c
4564 views
/*1* Copyright (C) 2016 Etnaviv Project2*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* Christian Gmeiner <[email protected]>24*/2526#include "etnaviv_priv.h"27#include "etnaviv_drmif.h"2829void _etna_bo_del(struct etna_bo *bo);3031static void add_bucket(struct etna_bo_cache *cache, int size)32{33unsigned i = cache->num_buckets;3435assert(i < ARRAY_SIZE(cache->cache_bucket));3637list_inithead(&cache->cache_bucket[i].list);38cache->cache_bucket[i].size = size;39cache->num_buckets++;40}4142void etna_bo_cache_init(struct etna_bo_cache *cache)43{44unsigned long size, cache_max_size = 64 * 1024 * 1024;4546/* OK, so power of two buckets was too wasteful of memory.47* Give 3 other sizes between each power of two, to hopefully48* cover things accurately enough. (The alternative is49* probably to just go for exact matching of sizes, and assume50* that for things like composited window resize the tiled51* width/height alignment and rounding of sizes to pages will52* get us useful cache hit rates anyway)53*/54add_bucket(cache, 4096);55add_bucket(cache, 4096 * 2);56add_bucket(cache, 4096 * 3);5758/* Initialize the linked lists for BO reuse cache. */59for (size = 4 * 4096; size <= cache_max_size; size *= 2) {60add_bucket(cache, size);61add_bucket(cache, size + size * 1 / 4);62add_bucket(cache, size + size * 2 / 4);63add_bucket(cache, size + size * 3 / 4);64}65}6667/* Frees older cached buffers. Called under etna_drm_table_lock */68void etna_bo_cache_cleanup(struct etna_bo_cache *cache, time_t time)69{70unsigned i;7172if (cache->time == time)73return;7475for (i = 0; i < cache->num_buckets; i++) {76struct etna_bo_bucket *bucket = &cache->cache_bucket[i];77struct etna_bo *bo;7879while (!list_is_empty(&bucket->list)) {80bo = LIST_ENTRY(struct etna_bo, bucket->list.next, list);8182/* keep things in cache for at least 1 second: */83if (time && ((time - bo->free_time) <= 1))84break;8586VG_BO_OBTAIN(bo);87list_del(&bo->list);88_etna_bo_del(bo);89}90}9192cache->time = time;93}9495static struct etna_bo_bucket *get_bucket(struct etna_bo_cache *cache, uint32_t size)96{97unsigned i;9899/* hmm, this is what intel does, but I suppose we could calculate our100* way to the correct bucket size rather than looping..101*/102for (i = 0; i < cache->num_buckets; i++) {103struct etna_bo_bucket *bucket = &cache->cache_bucket[i];104if (bucket->size >= size) {105return bucket;106}107}108109return NULL;110}111112static int is_idle(struct etna_bo *bo)113{114return etna_bo_cpu_prep(bo,115DRM_ETNA_PREP_READ |116DRM_ETNA_PREP_WRITE |117DRM_ETNA_PREP_NOSYNC) == 0;118}119120static struct etna_bo *find_in_bucket(struct etna_bo_bucket *bucket, uint32_t flags)121{122struct etna_bo *bo = NULL, *tmp;123124simple_mtx_lock(&etna_drm_table_lock);125126if (list_is_empty(&bucket->list))127goto out_unlock;128129LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &bucket->list, list) {130/* skip BOs with different flags */131if (bo->flags != flags)132continue;133134/* check if the first BO with matching flags is idle */135if (is_idle(bo)) {136list_delinit(&bo->list);137goto out_unlock;138}139140/* If the oldest BO is still busy, don't try younger ones */141break;142}143144/* There was no matching buffer found */145bo = NULL;146147out_unlock:148simple_mtx_unlock(&etna_drm_table_lock);149150return bo;151}152153/* allocate a new (un-tiled) buffer object154*155* NOTE: size is potentially rounded up to bucket size156*/157struct etna_bo *etna_bo_cache_alloc(struct etna_bo_cache *cache, uint32_t *size,158uint32_t flags)159{160struct etna_bo *bo;161struct etna_bo_bucket *bucket;162163*size = ALIGN(*size, 4096);164bucket = get_bucket(cache, *size);165166/* see if we can be green and recycle: */167if (bucket) {168*size = bucket->size;169bo = find_in_bucket(bucket, flags);170if (bo) {171VG_BO_OBTAIN(bo);172p_atomic_set(&bo->refcnt, 1);173etna_device_ref(bo->dev);174return bo;175}176}177178return NULL;179}180181int etna_bo_cache_free(struct etna_bo_cache *cache, struct etna_bo *bo)182{183struct etna_bo_bucket *bucket;184185simple_mtx_assert_locked(&etna_drm_table_lock);186187bucket = get_bucket(cache, bo->size);188189/* see if we can be green and recycle: */190if (bucket) {191struct timespec time;192193clock_gettime(CLOCK_MONOTONIC, &time);194195bo->free_time = time.tv_sec;196VG_BO_RELEASE(bo);197list_addtail(&bo->list, &bucket->list);198etna_bo_cache_cleanup(cache, time.tv_sec);199200/* bo's in the bucket cache don't have a ref and201* don't hold a ref to the dev:202*/203etna_device_del_locked(bo->dev);204205return 0;206}207208return -1;209}210211212