1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2007-2008 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/** 29bf215546Sopenharmony_ci * \file 30bf215546Sopenharmony_ci * Buffer cache. 31bf215546Sopenharmony_ci * 32bf215546Sopenharmony_ci * \author Jose Fonseca <jfonseca-at-vmware-dot-com> 33bf215546Sopenharmony_ci * \author Thomas Hellström <thellstrom-at-vmware-dot-com> 34bf215546Sopenharmony_ci */ 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#include "pipe/p_compiler.h" 38bf215546Sopenharmony_ci#include "util/u_debug.h" 39bf215546Sopenharmony_ci#include "os/os_thread.h" 40bf215546Sopenharmony_ci#include "util/u_memory.h" 41bf215546Sopenharmony_ci#include "util/list.h" 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci#include "pb_buffer.h" 44bf215546Sopenharmony_ci#include "pb_bufmgr.h" 45bf215546Sopenharmony_ci#include "pb_cache.h" 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci/** 49bf215546Sopenharmony_ci * Wrapper around a pipe buffer which adds delayed destruction. 50bf215546Sopenharmony_ci */ 51bf215546Sopenharmony_cistruct pb_cache_buffer 52bf215546Sopenharmony_ci{ 53bf215546Sopenharmony_ci struct pb_buffer base; 54bf215546Sopenharmony_ci struct pb_buffer *buffer; 55bf215546Sopenharmony_ci struct pb_cache_manager *mgr; 56bf215546Sopenharmony_ci struct pb_cache_entry cache_entry; 57bf215546Sopenharmony_ci}; 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_cistruct pb_cache_manager 61bf215546Sopenharmony_ci{ 62bf215546Sopenharmony_ci struct pb_manager base; 63bf215546Sopenharmony_ci struct pb_manager *provider; 64bf215546Sopenharmony_ci struct pb_cache cache; 65bf215546Sopenharmony_ci}; 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_cistatic inline struct pb_cache_buffer * 69bf215546Sopenharmony_cipb_cache_buffer(struct pb_buffer *buf) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci assert(buf); 72bf215546Sopenharmony_ci return (struct pb_cache_buffer *)buf; 73bf215546Sopenharmony_ci} 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_cistatic inline struct pb_cache_manager * 77bf215546Sopenharmony_cipb_cache_manager(struct pb_manager *mgr) 78bf215546Sopenharmony_ci{ 79bf215546Sopenharmony_ci assert(mgr); 80bf215546Sopenharmony_ci return (struct pb_cache_manager *)mgr; 81bf215546Sopenharmony_ci} 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_civoid 85bf215546Sopenharmony_cipb_cache_manager_remove_buffer(struct pb_buffer *pb_buf) 86bf215546Sopenharmony_ci{ 87bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(pb_buf); 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci /* the buffer won't be added if mgr is NULL */ 90bf215546Sopenharmony_ci buf->mgr = NULL; 91bf215546Sopenharmony_ci} 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci/** 94bf215546Sopenharmony_ci * Actually destroy the buffer. 95bf215546Sopenharmony_ci */ 96bf215546Sopenharmony_cistatic void 97bf215546Sopenharmony_ci_pb_cache_buffer_destroy(void *winsys, struct pb_buffer *pb_buf) 98bf215546Sopenharmony_ci{ 99bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(pb_buf); 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci assert(!pipe_is_referenced(&buf->base.reference)); 102bf215546Sopenharmony_ci pb_reference(&buf->buffer, NULL); 103bf215546Sopenharmony_ci FREE(buf); 104bf215546Sopenharmony_ci} 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_cistatic void 108bf215546Sopenharmony_cipb_cache_buffer_destroy(void *winsys, struct pb_buffer *_buf) 109bf215546Sopenharmony_ci{ 110bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 111bf215546Sopenharmony_ci struct pb_cache_manager *mgr = buf->mgr; 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci if (!mgr) { 114bf215546Sopenharmony_ci pb_reference(&buf->buffer, NULL); 115bf215546Sopenharmony_ci FREE(buf); 116bf215546Sopenharmony_ci return; 117bf215546Sopenharmony_ci } 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci pb_cache_add_buffer(&buf->cache_entry); 120bf215546Sopenharmony_ci} 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_cistatic void * 124bf215546Sopenharmony_cipb_cache_buffer_map(struct pb_buffer *_buf, 125bf215546Sopenharmony_ci enum pb_usage_flags flags, void *flush_ctx) 126bf215546Sopenharmony_ci{ 127bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 128bf215546Sopenharmony_ci return pb_map(buf->buffer, flags, flush_ctx); 129bf215546Sopenharmony_ci} 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_cistatic void 133bf215546Sopenharmony_cipb_cache_buffer_unmap(struct pb_buffer *_buf) 134bf215546Sopenharmony_ci{ 135bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 136bf215546Sopenharmony_ci pb_unmap(buf->buffer); 137bf215546Sopenharmony_ci} 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_cistatic enum pipe_error 141bf215546Sopenharmony_cipb_cache_buffer_validate(struct pb_buffer *_buf, 142bf215546Sopenharmony_ci struct pb_validate *vl, 143bf215546Sopenharmony_ci enum pb_usage_flags flags) 144bf215546Sopenharmony_ci{ 145bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 146bf215546Sopenharmony_ci return pb_validate(buf->buffer, vl, flags); 147bf215546Sopenharmony_ci} 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_cistatic void 151bf215546Sopenharmony_cipb_cache_buffer_fence(struct pb_buffer *_buf, 152bf215546Sopenharmony_ci struct pipe_fence_handle *fence) 153bf215546Sopenharmony_ci{ 154bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 155bf215546Sopenharmony_ci pb_fence(buf->buffer, fence); 156bf215546Sopenharmony_ci} 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci 159bf215546Sopenharmony_cistatic void 160bf215546Sopenharmony_cipb_cache_buffer_get_base_buffer(struct pb_buffer *_buf, 161bf215546Sopenharmony_ci struct pb_buffer **base_buf, 162bf215546Sopenharmony_ci pb_size *offset) 163bf215546Sopenharmony_ci{ 164bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 165bf215546Sopenharmony_ci pb_get_base_buffer(buf->buffer, base_buf, offset); 166bf215546Sopenharmony_ci} 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ciconst struct pb_vtbl 170bf215546Sopenharmony_cipb_cache_buffer_vtbl = { 171bf215546Sopenharmony_ci pb_cache_buffer_destroy, 172bf215546Sopenharmony_ci pb_cache_buffer_map, 173bf215546Sopenharmony_ci pb_cache_buffer_unmap, 174bf215546Sopenharmony_ci pb_cache_buffer_validate, 175bf215546Sopenharmony_ci pb_cache_buffer_fence, 176bf215546Sopenharmony_ci pb_cache_buffer_get_base_buffer 177bf215546Sopenharmony_ci}; 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_cistatic bool 181bf215546Sopenharmony_cipb_cache_can_reclaim_buffer(void *winsys, struct pb_buffer *_buf) 182bf215546Sopenharmony_ci{ 183bf215546Sopenharmony_ci struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci if (buf->mgr->provider->is_buffer_busy) { 186bf215546Sopenharmony_ci if (buf->mgr->provider->is_buffer_busy(buf->mgr->provider, buf->buffer)) 187bf215546Sopenharmony_ci return false; 188bf215546Sopenharmony_ci } else { 189bf215546Sopenharmony_ci void *ptr = pb_map(buf->buffer, PB_USAGE_DONTBLOCK, NULL); 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci if (!ptr) 192bf215546Sopenharmony_ci return false; 193bf215546Sopenharmony_ci 194bf215546Sopenharmony_ci pb_unmap(buf->buffer); 195bf215546Sopenharmony_ci } 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci return true; 198bf215546Sopenharmony_ci} 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_cistatic struct pb_buffer * 202bf215546Sopenharmony_cipb_cache_manager_create_buffer(struct pb_manager *_mgr, 203bf215546Sopenharmony_ci pb_size size, 204bf215546Sopenharmony_ci const struct pb_desc *desc) 205bf215546Sopenharmony_ci{ 206bf215546Sopenharmony_ci struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 207bf215546Sopenharmony_ci struct pb_cache_buffer *buf; 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci /* get a buffer from the cache */ 210bf215546Sopenharmony_ci buf = (struct pb_cache_buffer *) 211bf215546Sopenharmony_ci pb_cache_reclaim_buffer(&mgr->cache, size, desc->alignment, 212bf215546Sopenharmony_ci desc->usage, 0); 213bf215546Sopenharmony_ci if (buf) 214bf215546Sopenharmony_ci return &buf->base; 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci /* create a new one */ 217bf215546Sopenharmony_ci buf = CALLOC_STRUCT(pb_cache_buffer); 218bf215546Sopenharmony_ci if (!buf) 219bf215546Sopenharmony_ci return NULL; 220bf215546Sopenharmony_ci 221bf215546Sopenharmony_ci buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc); 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci /* Empty the cache and try again. */ 224bf215546Sopenharmony_ci if (!buf->buffer) { 225bf215546Sopenharmony_ci pb_cache_release_all_buffers(&mgr->cache); 226bf215546Sopenharmony_ci buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc); 227bf215546Sopenharmony_ci } 228bf215546Sopenharmony_ci 229bf215546Sopenharmony_ci if(!buf->buffer) { 230bf215546Sopenharmony_ci FREE(buf); 231bf215546Sopenharmony_ci return NULL; 232bf215546Sopenharmony_ci } 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci assert(pipe_is_referenced(&buf->buffer->reference)); 235bf215546Sopenharmony_ci assert(pb_check_alignment(desc->alignment, 1u << buf->buffer->alignment_log2)); 236bf215546Sopenharmony_ci assert(buf->buffer->size >= size); 237bf215546Sopenharmony_ci 238bf215546Sopenharmony_ci pipe_reference_init(&buf->base.reference, 1); 239bf215546Sopenharmony_ci buf->base.alignment_log2 = buf->buffer->alignment_log2; 240bf215546Sopenharmony_ci buf->base.usage = buf->buffer->usage; 241bf215546Sopenharmony_ci buf->base.size = buf->buffer->size; 242bf215546Sopenharmony_ci 243bf215546Sopenharmony_ci buf->base.vtbl = &pb_cache_buffer_vtbl; 244bf215546Sopenharmony_ci buf->mgr = mgr; 245bf215546Sopenharmony_ci pb_cache_init_entry(&mgr->cache, &buf->cache_entry, &buf->base, 0); 246bf215546Sopenharmony_ci 247bf215546Sopenharmony_ci return &buf->base; 248bf215546Sopenharmony_ci} 249bf215546Sopenharmony_ci 250bf215546Sopenharmony_ci 251bf215546Sopenharmony_cistatic void 252bf215546Sopenharmony_cipb_cache_manager_flush(struct pb_manager *_mgr) 253bf215546Sopenharmony_ci{ 254bf215546Sopenharmony_ci struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci pb_cache_release_all_buffers(&mgr->cache); 257bf215546Sopenharmony_ci 258bf215546Sopenharmony_ci assert(mgr->provider->flush); 259bf215546Sopenharmony_ci if(mgr->provider->flush) 260bf215546Sopenharmony_ci mgr->provider->flush(mgr->provider); 261bf215546Sopenharmony_ci} 262bf215546Sopenharmony_ci 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_cistatic void 265bf215546Sopenharmony_cipb_cache_manager_destroy(struct pb_manager *_mgr) 266bf215546Sopenharmony_ci{ 267bf215546Sopenharmony_ci struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 268bf215546Sopenharmony_ci 269bf215546Sopenharmony_ci pb_cache_deinit(&mgr->cache); 270bf215546Sopenharmony_ci FREE(mgr); 271bf215546Sopenharmony_ci} 272bf215546Sopenharmony_ci 273bf215546Sopenharmony_ci/** 274bf215546Sopenharmony_ci * Create a caching buffer manager 275bf215546Sopenharmony_ci * 276bf215546Sopenharmony_ci * @param provider The buffer manager to which cache miss buffer requests 277bf215546Sopenharmony_ci * should be redirected. 278bf215546Sopenharmony_ci * @param usecs Unused buffers may be released from the cache after this 279bf215546Sopenharmony_ci * time 280bf215546Sopenharmony_ci * @param size_factor Declare buffers that are size_factor times bigger than 281bf215546Sopenharmony_ci * the requested size as cache hits. 282bf215546Sopenharmony_ci * @param bypass_usage Bitmask. If (requested usage & bypass_usage) != 0, 283bf215546Sopenharmony_ci * buffer allocation requests are redirected to the provider. 284bf215546Sopenharmony_ci * @param maximum_cache_size Maximum size of all unused buffers the cache can 285bf215546Sopenharmony_ci * hold. 286bf215546Sopenharmony_ci */ 287bf215546Sopenharmony_cistruct pb_manager * 288bf215546Sopenharmony_cipb_cache_manager_create(struct pb_manager *provider, 289bf215546Sopenharmony_ci unsigned usecs, 290bf215546Sopenharmony_ci float size_factor, 291bf215546Sopenharmony_ci unsigned bypass_usage, 292bf215546Sopenharmony_ci uint64_t maximum_cache_size) 293bf215546Sopenharmony_ci{ 294bf215546Sopenharmony_ci struct pb_cache_manager *mgr; 295bf215546Sopenharmony_ci 296bf215546Sopenharmony_ci if (!provider) 297bf215546Sopenharmony_ci return NULL; 298bf215546Sopenharmony_ci 299bf215546Sopenharmony_ci mgr = CALLOC_STRUCT(pb_cache_manager); 300bf215546Sopenharmony_ci if (!mgr) 301bf215546Sopenharmony_ci return NULL; 302bf215546Sopenharmony_ci 303bf215546Sopenharmony_ci mgr->base.destroy = pb_cache_manager_destroy; 304bf215546Sopenharmony_ci mgr->base.create_buffer = pb_cache_manager_create_buffer; 305bf215546Sopenharmony_ci mgr->base.flush = pb_cache_manager_flush; 306bf215546Sopenharmony_ci mgr->provider = provider; 307bf215546Sopenharmony_ci pb_cache_init(&mgr->cache, 1, usecs, size_factor, bypass_usage, 308bf215546Sopenharmony_ci maximum_cache_size, NULL, 309bf215546Sopenharmony_ci _pb_cache_buffer_destroy, 310bf215546Sopenharmony_ci pb_cache_can_reclaim_buffer); 311bf215546Sopenharmony_ci return &mgr->base; 312bf215546Sopenharmony_ci} 313