freedreno_bo_cache.c revision d8807b2f
1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Rob Clark <robclark@freedesktop.org>
27 */
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include "freedreno_drmif.h"
34#include "freedreno_priv.h"
35
36drm_private void bo_del(struct fd_bo *bo);
37drm_private extern pthread_mutex_t table_lock;
38
39static void
40add_bucket(struct fd_bo_cache *cache, int size)
41{
42	unsigned int i = cache->num_buckets;
43
44	assert(i < ARRAY_SIZE(cache->cache_bucket));
45
46	list_inithead(&cache->cache_bucket[i].list);
47	cache->cache_bucket[i].size = size;
48	cache->num_buckets++;
49}
50
51/**
52 * @coarse: if true, only power-of-two bucket sizes, otherwise
53 *    fill in for a bit smoother size curve..
54 */
55drm_private void
56fd_bo_cache_init(struct fd_bo_cache *cache, int course)
57{
58	unsigned long size, cache_max_size = 64 * 1024 * 1024;
59
60	/* OK, so power of two buckets was too wasteful of memory.
61	 * Give 3 other sizes between each power of two, to hopefully
62	 * cover things accurately enough.  (The alternative is
63	 * probably to just go for exact matching of sizes, and assume
64	 * that for things like composited window resize the tiled
65	 * width/height alignment and rounding of sizes to pages will
66	 * get us useful cache hit rates anyway)
67	 */
68	add_bucket(cache, 4096);
69	add_bucket(cache, 4096 * 2);
70	if (!course)
71		add_bucket(cache, 4096 * 3);
72
73	/* Initialize the linked lists for BO reuse cache. */
74	for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
75		add_bucket(cache, size);
76		if (!course) {
77			add_bucket(cache, size + size * 1 / 4);
78			add_bucket(cache, size + size * 2 / 4);
79			add_bucket(cache, size + size * 3 / 4);
80		}
81	}
82}
83
84/* Frees older cached buffers.  Called under table_lock */
85drm_private void
86fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time)
87{
88	int i;
89
90	if (cache->time == time)
91		return;
92
93	for (i = 0; i < cache->num_buckets; i++) {
94		struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
95		struct fd_bo *bo;
96
97		while (!LIST_IS_EMPTY(&bucket->list)) {
98			bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
99
100			/* keep things in cache for at least 1 second: */
101			if (time && ((time - bo->free_time) <= 1))
102				break;
103
104			VG_BO_OBTAIN(bo);
105			list_del(&bo->list);
106			bo_del(bo);
107		}
108	}
109
110	cache->time = time;
111}
112
113static struct fd_bo_bucket * get_bucket(struct fd_bo_cache *cache, uint32_t size)
114{
115	int i;
116
117	/* hmm, this is what intel does, but I suppose we could calculate our
118	 * way to the correct bucket size rather than looping..
119	 */
120	for (i = 0; i < cache->num_buckets; i++) {
121		struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
122		if (bucket->size >= size) {
123			return bucket;
124		}
125	}
126
127	return NULL;
128}
129
130static int is_idle(struct fd_bo *bo)
131{
132	return fd_bo_cpu_prep(bo, NULL,
133			DRM_FREEDRENO_PREP_READ |
134			DRM_FREEDRENO_PREP_WRITE |
135			DRM_FREEDRENO_PREP_NOSYNC) == 0;
136}
137
138static struct fd_bo *find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)
139{
140	struct fd_bo *bo = NULL;
141
142	/* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
143	 * skip the busy check.. if it is only going to be a render target
144	 * then we probably don't need to stall..
145	 *
146	 * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
147	 * (MRU, since likely to be in GPU cache), rather than head (LRU)..
148	 */
149	pthread_mutex_lock(&table_lock);
150	if (!LIST_IS_EMPTY(&bucket->list)) {
151		bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
152		/* TODO check for compatible flags? */
153		if (is_idle(bo)) {
154			list_del(&bo->list);
155		} else {
156			bo = NULL;
157		}
158	}
159	pthread_mutex_unlock(&table_lock);
160
161	return bo;
162}
163
164/* NOTE: size is potentially rounded up to bucket size: */
165drm_private struct fd_bo *
166fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size, uint32_t flags)
167{
168	struct fd_bo *bo = NULL;
169	struct fd_bo_bucket *bucket;
170
171	*size = ALIGN(*size, 4096);
172	bucket = get_bucket(cache, *size);
173
174	/* see if we can be green and recycle: */
175retry:
176	if (bucket) {
177		*size = bucket->size;
178		bo = find_in_bucket(bucket, flags);
179		if (bo) {
180			VG_BO_OBTAIN(bo);
181			if (bo->funcs->madvise(bo, TRUE) <= 0) {
182				/* we've lost the backing pages, delete and try again: */
183				pthread_mutex_lock(&table_lock);
184				bo_del(bo);
185				pthread_mutex_unlock(&table_lock);
186				goto retry;
187			}
188			atomic_set(&bo->refcnt, 1);
189			fd_device_ref(bo->dev);
190			return bo;
191		}
192	}
193
194	return NULL;
195}
196
197drm_private int
198fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo)
199{
200	struct fd_bo_bucket *bucket = get_bucket(cache, bo->size);
201
202	/* see if we can be green and recycle: */
203	if (bucket) {
204		struct timespec time;
205
206		bo->funcs->madvise(bo, FALSE);
207
208		clock_gettime(CLOCK_MONOTONIC, &time);
209
210		bo->free_time = time.tv_sec;
211		VG_BO_RELEASE(bo);
212		list_addtail(&bo->list, &bucket->list);
213		fd_bo_cache_cleanup(cache, time.tv_sec);
214
215		/* bo's in the bucket cache don't have a ref and
216		 * don't hold a ref to the dev:
217		 */
218		fd_device_del_locked(bo->dev);
219
220		return 0;
221	}
222
223	return -1;
224}
225