svga/winsys: implement GBS support
[mesa.git] / src / gallium / winsys / svga / drm / vmw_screen_pools.c
1 /**********************************************************
2 * Copyright 2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27 #include "vmw_screen.h"
28
29 #include "vmw_buffer.h"
30 #include "vmw_fence.h"
31
32 #include "pipebuffer/pb_buffer.h"
33 #include "pipebuffer/pb_bufmgr.h"
34
35 /**
36 * vmw_pools_cleanup - Destroy the buffer pools.
37 *
38 * @vws: pointer to a struct vmw_winsys_screen.
39 */
40 void
41 vmw_pools_cleanup(struct vmw_winsys_screen *vws)
42 {
43 if (vws->pools.mob_shader_slab_fenced)
44 vws->pools.mob_shader_slab_fenced->destroy
45 (vws->pools.mob_shader_slab_fenced);
46 if (vws->pools.mob_shader_slab)
47 vws->pools.mob_shader_slab->destroy(vws->pools.mob_shader_slab);
48 if (vws->pools.mob_fenced)
49 vws->pools.mob_fenced->destroy(vws->pools.mob_fenced);
50 if (vws->pools.mob_cache)
51 vws->pools.mob_cache->destroy(vws->pools.mob_cache);
52
53 if (vws->pools.query_fenced)
54 vws->pools.query_fenced->destroy(vws->pools.query_fenced);
55 if (vws->pools.query_mm)
56 vws->pools.query_mm->destroy(vws->pools.query_mm);
57
58 if(vws->pools.gmr_fenced)
59 vws->pools.gmr_fenced->destroy(vws->pools.gmr_fenced);
60 if (vws->pools.gmr_mm)
61 vws->pools.gmr_mm->destroy(vws->pools.gmr_mm);
62 if (vws->pools.gmr_slab_fenced)
63 vws->pools.gmr_slab_fenced->destroy(vws->pools.gmr_slab_fenced);
64 if (vws->pools.gmr_slab)
65 vws->pools.gmr_slab->destroy(vws->pools.gmr_slab);
66
67 if(vws->pools.gmr)
68 vws->pools.gmr->destroy(vws->pools.gmr);
69 }
70
71
72 /**
73 * vmw_query_pools_init - Create a pool of query buffers.
74 *
75 * @vws: Pointer to a struct vmw_winsys_screen.
76 *
77 * Typically this pool should be created on demand when we
78 * detect that the app will be using queries. There's nothing
79 * special with this pool other than the backing kernel buffer size,
80 * which is limited to 8192.
81 */
82 boolean
83 vmw_query_pools_init(struct vmw_winsys_screen *vws)
84 {
85 vws->pools.query_mm = mm_bufmgr_create(vws->pools.gmr,
86 VMW_QUERY_POOL_SIZE,
87 3 /* 8 alignment */);
88 if (!vws->pools.query_mm)
89 return FALSE;
90
91 vws->pools.query_fenced = simple_fenced_bufmgr_create(
92 vws->pools.query_mm, vws->fence_ops);
93
94 if(!vws->pools.query_fenced)
95 goto out_no_query_fenced;
96
97 return TRUE;
98
99 out_no_query_fenced:
100 vws->pools.query_mm->destroy(vws->pools.query_mm);
101 return FALSE;
102 }
103
104 /**
105 * vmw_mob_pool_init - Create a pool of fenced kernel buffers.
106 *
107 * @vws: Pointer to a struct vmw_winsys_screen.
108 *
109 * Typically this pool should be created on demand when we
110 * detect that the app will be using MOB buffers.
111 */
112 boolean
113 vmw_mob_pools_init(struct vmw_winsys_screen *vws)
114 {
115 struct pb_desc desc;
116
117 vws->pools.mob_cache =
118 pb_cache_manager_create(vws->pools.gmr, 100000, 2,
119 VMW_BUFFER_USAGE_SHARED);
120 if (!vws->pools.mob_cache)
121 return FALSE;
122
123 vws->pools.mob_fenced =
124 simple_fenced_bufmgr_create(vws->pools.mob_cache,
125 vws->fence_ops);
126 if(!vws->pools.mob_fenced)
127 goto out_no_mob_fenced;
128
129 desc.alignment = 64;
130 desc.usage = ~(SVGA_BUFFER_USAGE_PINNED | VMW_BUFFER_USAGE_SHARED |
131 VMW_BUFFER_USAGE_SYNC);
132 vws->pools.mob_shader_slab =
133 pb_slab_range_manager_create(vws->pools.mob_cache,
134 64,
135 8192,
136 16384,
137 &desc);
138 if(!vws->pools.mob_shader_slab)
139 goto out_no_mob_shader_slab;
140
141 vws->pools.mob_shader_slab_fenced =
142 simple_fenced_bufmgr_create(vws->pools.mob_shader_slab,
143 vws->fence_ops);
144 if(!vws->pools.mob_fenced)
145 goto out_no_mob_shader_slab_fenced;
146
147 return TRUE;
148
149 out_no_mob_shader_slab_fenced:
150 vws->pools.mob_shader_slab->destroy(vws->pools.mob_shader_slab);
151 out_no_mob_shader_slab:
152 vws->pools.mob_fenced->destroy(vws->pools.mob_fenced);
153 out_no_mob_fenced:
154 vws->pools.mob_cache->destroy(vws->pools.mob_cache);
155 return FALSE;
156 }
157
158 /**
159 * vmw_pools_init - Create a pool of GMR buffers.
160 *
161 * @vws: Pointer to a struct vmw_winsys_screen.
162 */
163 boolean
164 vmw_pools_init(struct vmw_winsys_screen *vws)
165 {
166 struct pb_desc desc;
167
168 vws->pools.gmr = vmw_gmr_bufmgr_create(vws);
169 if(!vws->pools.gmr)
170 goto error;
171
172 if ((vws->base.have_gb_objects && vws->base.have_gb_dma) ||
173 !vws->base.have_gb_objects) {
174 /*
175 * A managed pool for DMA buffers.
176 */
177 vws->pools.gmr_mm = mm_bufmgr_create(vws->pools.gmr,
178 VMW_GMR_POOL_SIZE,
179 12 /* 4096 alignment */);
180 if(!vws->pools.gmr_mm)
181 goto error;
182
183 vws->pools.gmr_fenced = simple_fenced_bufmgr_create
184 (vws->pools.gmr_mm, vws->fence_ops);
185
186 #ifdef DEBUG
187 vws->pools.gmr_fenced = pb_debug_manager_create(vws->pools.gmr_fenced,
188 4096,
189 4096);
190 #endif
191 if(!vws->pools.gmr_fenced)
192 goto error;
193
194 /*
195 * The slab pool allocates buffers directly from the kernel except
196 * for very small buffers which are allocated from a slab in order
197 * not to waste memory, since a kernel buffer is a minimum 4096 bytes.
198 *
199 * Here we use it only for emergency in the case our pre-allocated
200 * managed buffer pool runs out of memory.
201 */
202
203 desc.alignment = 64;
204 desc.usage = ~(SVGA_BUFFER_USAGE_PINNED | SVGA_BUFFER_USAGE_SHADER |
205 VMW_BUFFER_USAGE_SHARED | VMW_BUFFER_USAGE_SYNC);
206 vws->pools.gmr_slab = pb_slab_range_manager_create(vws->pools.gmr,
207 64,
208 8192,
209 16384,
210 &desc);
211 if (!vws->pools.gmr_slab)
212 goto error;
213
214 vws->pools.gmr_slab_fenced =
215 simple_fenced_bufmgr_create(vws->pools.gmr_slab, vws->fence_ops);
216
217 if (!vws->pools.gmr_slab_fenced)
218 goto error;
219 }
220
221 vws->pools.query_fenced = NULL;
222 vws->pools.query_mm = NULL;
223 vws->pools.mob_cache = NULL;
224
225 if (vws->base.have_gb_objects && !vmw_mob_pools_init(vws))
226 goto error;
227
228 return TRUE;
229
230 error:
231 vmw_pools_cleanup(vws);
232 return FALSE;
233 }