13f012e29Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
23f012e29Smrg
33f012e29Smrg/*
43f012e29Smrg * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
53f012e29Smrg *
63f012e29Smrg * Permission is hereby granted, free of charge, to any person obtaining a
73f012e29Smrg * copy of this software and associated documentation files (the "Software"),
83f012e29Smrg * to deal in the Software without restriction, including without limitation
93f012e29Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
103f012e29Smrg * and/or sell copies of the Software, and to permit persons to whom the
113f012e29Smrg * Software is furnished to do so, subject to the following conditions:
123f012e29Smrg *
133f012e29Smrg * The above copyright notice and this permission notice (including the next
143f012e29Smrg * paragraph) shall be included in all copies or substantial portions of the
153f012e29Smrg * Software.
163f012e29Smrg *
173f012e29Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
183f012e29Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193f012e29Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
203f012e29Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
213f012e29Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
223f012e29Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
233f012e29Smrg * SOFTWARE.
243f012e29Smrg *
253f012e29Smrg * Authors:
263f012e29Smrg *    Rob Clark <robclark@freedesktop.org>
273f012e29Smrg */
283f012e29Smrg
293f012e29Smrg#include "freedreno_drmif.h"
303f012e29Smrg#include "freedreno_priv.h"
313f012e29Smrg
323f012e29Smrgdrm_private void bo_del(struct fd_bo *bo);
333f012e29Smrgdrm_private extern pthread_mutex_t table_lock;
343f012e29Smrg
353f012e29Smrgstatic void
363f012e29Smrgadd_bucket(struct fd_bo_cache *cache, int size)
373f012e29Smrg{
383f012e29Smrg	unsigned int i = cache->num_buckets;
393f012e29Smrg
403f012e29Smrg	assert(i < ARRAY_SIZE(cache->cache_bucket));
413f012e29Smrg
423f012e29Smrg	list_inithead(&cache->cache_bucket[i].list);
433f012e29Smrg	cache->cache_bucket[i].size = size;
443f012e29Smrg	cache->num_buckets++;
453f012e29Smrg}
463f012e29Smrg
473f012e29Smrg/**
483f012e29Smrg * @coarse: if true, only power-of-two bucket sizes, otherwise
493f012e29Smrg *    fill in for a bit smoother size curve..
503f012e29Smrg */
513f012e29Smrgdrm_private void
527cdc0497Smrgfd_bo_cache_init(struct fd_bo_cache *cache, int coarse)
533f012e29Smrg{
543f012e29Smrg	unsigned long size, cache_max_size = 64 * 1024 * 1024;
553f012e29Smrg
563f012e29Smrg	/* OK, so power of two buckets was too wasteful of memory.
573f012e29Smrg	 * Give 3 other sizes between each power of two, to hopefully
583f012e29Smrg	 * cover things accurately enough.  (The alternative is
593f012e29Smrg	 * probably to just go for exact matching of sizes, and assume
603f012e29Smrg	 * that for things like composited window resize the tiled
613f012e29Smrg	 * width/height alignment and rounding of sizes to pages will
623f012e29Smrg	 * get us useful cache hit rates anyway)
633f012e29Smrg	 */
643f012e29Smrg	add_bucket(cache, 4096);
653f012e29Smrg	add_bucket(cache, 4096 * 2);
667cdc0497Smrg	if (!coarse)
673f012e29Smrg		add_bucket(cache, 4096 * 3);
683f012e29Smrg
693f012e29Smrg	/* Initialize the linked lists for BO reuse cache. */
703f012e29Smrg	for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
713f012e29Smrg		add_bucket(cache, size);
727cdc0497Smrg		if (!coarse) {
733f012e29Smrg			add_bucket(cache, size + size * 1 / 4);
743f012e29Smrg			add_bucket(cache, size + size * 2 / 4);
753f012e29Smrg			add_bucket(cache, size + size * 3 / 4);
763f012e29Smrg		}
773f012e29Smrg	}
783f012e29Smrg}
793f012e29Smrg
803f012e29Smrg/* Frees older cached buffers.  Called under table_lock */
813f012e29Smrgdrm_private void
823f012e29Smrgfd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time)
833f012e29Smrg{
843f012e29Smrg	int i;
853f012e29Smrg
863f012e29Smrg	if (cache->time == time)
873f012e29Smrg		return;
883f012e29Smrg
893f012e29Smrg	for (i = 0; i < cache->num_buckets; i++) {
903f012e29Smrg		struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
913f012e29Smrg		struct fd_bo *bo;
923f012e29Smrg
933f012e29Smrg		while (!LIST_IS_EMPTY(&bucket->list)) {
943f012e29Smrg			bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
953f012e29Smrg
963f012e29Smrg			/* keep things in cache for at least 1 second: */
973f012e29Smrg			if (time && ((time - bo->free_time) <= 1))
983f012e29Smrg				break;
993f012e29Smrg
100d8807b2fSmrg			VG_BO_OBTAIN(bo);
1013f012e29Smrg			list_del(&bo->list);
1023f012e29Smrg			bo_del(bo);
1033f012e29Smrg		}
1043f012e29Smrg	}
1053f012e29Smrg
1063f012e29Smrg	cache->time = time;
1073f012e29Smrg}
1083f012e29Smrg
1093f012e29Smrgstatic struct fd_bo_bucket * get_bucket(struct fd_bo_cache *cache, uint32_t size)
1103f012e29Smrg{
1113f012e29Smrg	int i;
1123f012e29Smrg
1133f012e29Smrg	/* hmm, this is what intel does, but I suppose we could calculate our
1143f012e29Smrg	 * way to the correct bucket size rather than looping..
1153f012e29Smrg	 */
1163f012e29Smrg	for (i = 0; i < cache->num_buckets; i++) {
1173f012e29Smrg		struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
1183f012e29Smrg		if (bucket->size >= size) {
1193f012e29Smrg			return bucket;
1203f012e29Smrg		}
1213f012e29Smrg	}
1223f012e29Smrg
1233f012e29Smrg	return NULL;
1243f012e29Smrg}
1253f012e29Smrg
1263f012e29Smrgstatic int is_idle(struct fd_bo *bo)
1273f012e29Smrg{
1283f012e29Smrg	return fd_bo_cpu_prep(bo, NULL,
1293f012e29Smrg			DRM_FREEDRENO_PREP_READ |
1303f012e29Smrg			DRM_FREEDRENO_PREP_WRITE |
1313f012e29Smrg			DRM_FREEDRENO_PREP_NOSYNC) == 0;
1323f012e29Smrg}
1333f012e29Smrg
1343f012e29Smrgstatic struct fd_bo *find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)
1353f012e29Smrg{
1363f012e29Smrg	struct fd_bo *bo = NULL;
1373f012e29Smrg
1383f012e29Smrg	/* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
1393f012e29Smrg	 * skip the busy check.. if it is only going to be a render target
1403f012e29Smrg	 * then we probably don't need to stall..
1413f012e29Smrg	 *
1423f012e29Smrg	 * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
1433f012e29Smrg	 * (MRU, since likely to be in GPU cache), rather than head (LRU)..
1443f012e29Smrg	 */
1453f012e29Smrg	pthread_mutex_lock(&table_lock);
1463f012e29Smrg	if (!LIST_IS_EMPTY(&bucket->list)) {
1473f012e29Smrg		bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
1483f012e29Smrg		/* TODO check for compatible flags? */
1493f012e29Smrg		if (is_idle(bo)) {
1503f012e29Smrg			list_del(&bo->list);
1513f012e29Smrg		} else {
1523f012e29Smrg			bo = NULL;
1533f012e29Smrg		}
1543f012e29Smrg	}
1553f012e29Smrg	pthread_mutex_unlock(&table_lock);
1563f012e29Smrg
1573f012e29Smrg	return bo;
1583f012e29Smrg}
1593f012e29Smrg
1603f012e29Smrg/* NOTE: size is potentially rounded up to bucket size: */
1613f012e29Smrgdrm_private struct fd_bo *
1623f012e29Smrgfd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size, uint32_t flags)
1633f012e29Smrg{
1643f012e29Smrg	struct fd_bo *bo = NULL;
1653f012e29Smrg	struct fd_bo_bucket *bucket;
1663f012e29Smrg
1673f012e29Smrg	*size = ALIGN(*size, 4096);
1683f012e29Smrg	bucket = get_bucket(cache, *size);
1693f012e29Smrg
1703f012e29Smrg	/* see if we can be green and recycle: */
1713f012e29Smrgretry:
1723f012e29Smrg	if (bucket) {
1733f012e29Smrg		*size = bucket->size;
1743f012e29Smrg		bo = find_in_bucket(bucket, flags);
1753f012e29Smrg		if (bo) {
176d8807b2fSmrg			VG_BO_OBTAIN(bo);
1773f012e29Smrg			if (bo->funcs->madvise(bo, TRUE) <= 0) {
1783f012e29Smrg				/* we've lost the backing pages, delete and try again: */
1793f012e29Smrg				pthread_mutex_lock(&table_lock);
1803f012e29Smrg				bo_del(bo);
1813f012e29Smrg				pthread_mutex_unlock(&table_lock);
1823f012e29Smrg				goto retry;
1833f012e29Smrg			}
1843f012e29Smrg			atomic_set(&bo->refcnt, 1);
1853f012e29Smrg			fd_device_ref(bo->dev);
1863f012e29Smrg			return bo;
1873f012e29Smrg		}
1883f012e29Smrg	}
1893f012e29Smrg
1903f012e29Smrg	return NULL;
1913f012e29Smrg}
1923f012e29Smrg
1933f012e29Smrgdrm_private int
1943f012e29Smrgfd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo)
1953f012e29Smrg{
1963f012e29Smrg	struct fd_bo_bucket *bucket = get_bucket(cache, bo->size);
1973f012e29Smrg
1983f012e29Smrg	/* see if we can be green and recycle: */
1993f012e29Smrg	if (bucket) {
2003f012e29Smrg		struct timespec time;
2013f012e29Smrg
2023f012e29Smrg		bo->funcs->madvise(bo, FALSE);
2033f012e29Smrg
2043f012e29Smrg		clock_gettime(CLOCK_MONOTONIC, &time);
2053f012e29Smrg
2063f012e29Smrg		bo->free_time = time.tv_sec;
207d8807b2fSmrg		VG_BO_RELEASE(bo);
2083f012e29Smrg		list_addtail(&bo->list, &bucket->list);
2093f012e29Smrg		fd_bo_cache_cleanup(cache, time.tv_sec);
2103f012e29Smrg
2113f012e29Smrg		/* bo's in the bucket cache don't have a ref and
2123f012e29Smrg		 * don't hold a ref to the dev:
2133f012e29Smrg		 */
2143f012e29Smrg		fd_device_del_locked(bo->dev);
2153f012e29Smrg
2163f012e29Smrg		return 0;
2173f012e29Smrg	}
2183f012e29Smrg
2193f012e29Smrg	return -1;
2203f012e29Smrg}
221