1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 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 management.
31bf215546Sopenharmony_ci *
32bf215546Sopenharmony_ci * A buffer manager does only one basic thing: it creates buffers. Actually,
33bf215546Sopenharmony_ci * "buffer factory" would probably a more accurate description.
34bf215546Sopenharmony_ci *
35bf215546Sopenharmony_ci * You can chain buffer managers so that you can have a finer grained memory
36bf215546Sopenharmony_ci * management.
37bf215546Sopenharmony_ci *
38bf215546Sopenharmony_ci * For example, for a simple batch buffer manager you would chain:
39bf215546Sopenharmony_ci * - the native buffer manager, which provides DMA memory from the graphics
40bf215546Sopenharmony_ci * memory space;
41bf215546Sopenharmony_ci * - the fenced buffer manager, which will delay buffer destruction until the
42bf215546Sopenharmony_ci * the moment the card finishing processing it.
43bf215546Sopenharmony_ci *
44bf215546Sopenharmony_ci * \author Jose Fonseca <jfonseca@vmware.com>
45bf215546Sopenharmony_ci */
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci#ifndef PB_BUFMGR_H_
48bf215546Sopenharmony_ci#define PB_BUFMGR_H_
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#include "pb_buffer.h"
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci#ifdef __cplusplus
55bf215546Sopenharmony_ciextern "C" {
56bf215546Sopenharmony_ci#endif
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistruct pb_desc;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci/**
63bf215546Sopenharmony_ci * Abstract base class for all buffer managers.
64bf215546Sopenharmony_ci */
65bf215546Sopenharmony_cistruct pb_manager
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   void
68bf215546Sopenharmony_ci   (*destroy)( struct pb_manager *mgr );
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   struct pb_buffer *
71bf215546Sopenharmony_ci   (*create_buffer)( struct pb_manager *mgr,
72bf215546Sopenharmony_ci	             pb_size size,
73bf215546Sopenharmony_ci	             const struct pb_desc *desc);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   /**
76bf215546Sopenharmony_ci    * Flush all temporary-held buffers.
77bf215546Sopenharmony_ci    *
78bf215546Sopenharmony_ci    * Used mostly to aid debugging memory issues or to clean up resources when
79bf215546Sopenharmony_ci    * the drivers are long lived.
80bf215546Sopenharmony_ci    */
81bf215546Sopenharmony_ci   void
82bf215546Sopenharmony_ci   (*flush)( struct pb_manager *mgr );
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   boolean
85bf215546Sopenharmony_ci   (*is_buffer_busy)( struct pb_manager *mgr,
86bf215546Sopenharmony_ci                      struct pb_buffer *buf );
87bf215546Sopenharmony_ci};
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci/**
90bf215546Sopenharmony_ci * Static sub-allocator based the old memory manager.
91bf215546Sopenharmony_ci *
92bf215546Sopenharmony_ci * It managers buffers of different sizes. It does so by allocating a buffer
93bf215546Sopenharmony_ci * with the size of the heap, and then using the old mm memory manager to manage
94bf215546Sopenharmony_ci * that heap.
95bf215546Sopenharmony_ci */
96bf215546Sopenharmony_cistruct pb_manager *
97bf215546Sopenharmony_cimm_bufmgr_create(struct pb_manager *provider,
98bf215546Sopenharmony_ci                 pb_size size, pb_size align2);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci/**
101bf215546Sopenharmony_ci * Same as mm_bufmgr_create.
102bf215546Sopenharmony_ci *
103bf215546Sopenharmony_ci * Buffer will be release when the manager is destroyed.
104bf215546Sopenharmony_ci */
105bf215546Sopenharmony_cistruct pb_manager *
106bf215546Sopenharmony_cimm_bufmgr_create_from_buffer(struct pb_buffer *buffer,
107bf215546Sopenharmony_ci                             pb_size size, pb_size align2);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci/**
111bf215546Sopenharmony_ci * Slab sub-allocator.
112bf215546Sopenharmony_ci */
113bf215546Sopenharmony_cistruct pb_manager *
114bf215546Sopenharmony_cipb_slab_manager_create(struct pb_manager *provider,
115bf215546Sopenharmony_ci                       pb_size bufSize,
116bf215546Sopenharmony_ci                       pb_size slabSize,
117bf215546Sopenharmony_ci                       const struct pb_desc *desc);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci/**
120bf215546Sopenharmony_ci * Allow a range of buffer size, by aggregating multiple slabs sub-allocators
121bf215546Sopenharmony_ci * with different bucket sizes.
122bf215546Sopenharmony_ci */
123bf215546Sopenharmony_cistruct pb_manager *
124bf215546Sopenharmony_cipb_slab_range_manager_create(struct pb_manager *provider,
125bf215546Sopenharmony_ci                             pb_size minBufSize,
126bf215546Sopenharmony_ci                             pb_size maxBufSize,
127bf215546Sopenharmony_ci                             pb_size slabSize,
128bf215546Sopenharmony_ci                             const struct pb_desc *desc);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci/**
132bf215546Sopenharmony_ci * Time-based buffer cache.
133bf215546Sopenharmony_ci *
134bf215546Sopenharmony_ci * This manager keeps a cache of destroyed buffers during a time interval.
135bf215546Sopenharmony_ci */
136bf215546Sopenharmony_cistruct pb_manager *
137bf215546Sopenharmony_cipb_cache_manager_create(struct pb_manager *provider,
138bf215546Sopenharmony_ci                        unsigned usecs,
139bf215546Sopenharmony_ci                        float size_factor,
140bf215546Sopenharmony_ci                        unsigned bypass_usage,
141bf215546Sopenharmony_ci                        uint64_t maximum_cache_size);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci/**
144bf215546Sopenharmony_ci * Remove a buffer from the cache, but keep it alive.
145bf215546Sopenharmony_ci */
146bf215546Sopenharmony_civoid
147bf215546Sopenharmony_cipb_cache_manager_remove_buffer(struct pb_buffer *buf);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_cistruct pb_fence_ops;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci/**
152bf215546Sopenharmony_ci * Fenced buffer manager.
153bf215546Sopenharmony_ci *
154bf215546Sopenharmony_ci * This manager is just meant for convenience. It wraps the buffers returned
155bf215546Sopenharmony_ci * by another manager in fenced buffers, so that
156bf215546Sopenharmony_ci *
157bf215546Sopenharmony_ci * NOTE: the buffer manager that provides the buffers will be destroyed
158bf215546Sopenharmony_ci * at the same time.
159bf215546Sopenharmony_ci */
160bf215546Sopenharmony_cistruct pb_manager *
161bf215546Sopenharmony_cifenced_bufmgr_create(struct pb_manager *provider,
162bf215546Sopenharmony_ci                     struct pb_fence_ops *ops,
163bf215546Sopenharmony_ci                     pb_size max_buffer_size,
164bf215546Sopenharmony_ci                     pb_size max_cpu_total_size);
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci/**
167bf215546Sopenharmony_ci * Debug buffer manager to detect buffer under- and overflows.
168bf215546Sopenharmony_ci *
169bf215546Sopenharmony_ci * Under/overflow sizes should be a multiple of the largest alignment
170bf215546Sopenharmony_ci */
171bf215546Sopenharmony_cistruct pb_manager *
172bf215546Sopenharmony_cipb_debug_manager_create(struct pb_manager *provider,
173bf215546Sopenharmony_ci                        pb_size underflow_size, pb_size overflow_size);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci#ifdef __cplusplus
177bf215546Sopenharmony_ci}
178bf215546Sopenharmony_ci#endif
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci#endif /*PB_BUFMGR_H_*/
181