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  * @file
28  * This file implements the SVGA interface into this winsys, defined
29  * in drivers/svga/svga_winsys.h.
30  *
31  * @author Keith Whitwell
32  * @author Jose Fonseca
33  */
34 
35 
36 #include "svga_cmd.h"
37 #include "svga3d_caps.h"
38 
39 #include "util/u_inlines.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "pipebuffer/pb_buffer.h"
43 #include "pipebuffer/pb_bufmgr.h"
44 #include "svga_winsys.h"
45 #include "vmw_context.h"
46 #include "vmw_screen.h"
47 #include "vmw_surface.h"
48 #include "vmw_buffer.h"
49 #include "vmw_fence.h"
50 
51 
52 static struct svga_winsys_buffer *
vmw_svga_winsys_buffer_create(struct svga_winsys_screen * sws,unsigned alignment,unsigned usage,unsigned size)53 vmw_svga_winsys_buffer_create(struct svga_winsys_screen *sws,
54                               unsigned alignment,
55                               unsigned usage,
56                               unsigned size)
57 {
58    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
59    struct pb_desc desc;
60    struct pb_manager *provider;
61    struct pb_buffer *buffer;
62 
63    memset(&desc, 0, sizeof desc);
64    desc.alignment = alignment;
65    desc.usage = usage;
66 
67    if (usage == SVGA_BUFFER_USAGE_PINNED) {
68       if (vws->pools.query_fenced == NULL && !vmw_query_pools_init(vws))
69 	 return NULL;
70       provider = vws->pools.query_fenced;
71    } else
72       provider = vws->pools.gmr_fenced;
73 
74    assert(provider);
75    buffer = provider->create_buffer(provider, size, &desc);
76 
77    if(!buffer && provider == vws->pools.gmr_fenced) {
78 
79       assert(provider);
80       provider = vws->pools.gmr_slab_fenced;
81       buffer = provider->create_buffer(provider, size, &desc);
82    }
83 
84    if (!buffer)
85       return NULL;
86 
87    return vmw_svga_winsys_buffer(buffer);
88 }
89 
90 
91 static void *
vmw_svga_winsys_buffer_map(struct svga_winsys_screen * sws,struct svga_winsys_buffer * buf,unsigned flags)92 vmw_svga_winsys_buffer_map(struct svga_winsys_screen *sws,
93                            struct svga_winsys_buffer *buf,
94                            unsigned flags)
95 {
96    (void)sws;
97    return pb_map(vmw_pb_buffer(buf), flags, NULL);
98 }
99 
100 
101 static void
vmw_svga_winsys_buffer_unmap(struct svga_winsys_screen * sws,struct svga_winsys_buffer * buf)102 vmw_svga_winsys_buffer_unmap(struct svga_winsys_screen *sws,
103                              struct svga_winsys_buffer *buf)
104 {
105    (void)sws;
106    pb_unmap(vmw_pb_buffer(buf));
107 }
108 
109 
110 static void
vmw_svga_winsys_buffer_destroy(struct svga_winsys_screen * sws,struct svga_winsys_buffer * buf)111 vmw_svga_winsys_buffer_destroy(struct svga_winsys_screen *sws,
112                                struct svga_winsys_buffer *buf)
113 {
114    struct pb_buffer *pbuf = vmw_pb_buffer(buf);
115    (void)sws;
116    pb_reference(&pbuf, NULL);
117 }
118 
119 
120 static void
vmw_svga_winsys_fence_reference(struct svga_winsys_screen * sws,struct pipe_fence_handle ** pdst,struct pipe_fence_handle * src)121 vmw_svga_winsys_fence_reference(struct svga_winsys_screen *sws,
122                                 struct pipe_fence_handle **pdst,
123                                 struct pipe_fence_handle *src)
124 {
125     struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
126 
127     vmw_fence_reference(vws, pdst, src);
128 }
129 
130 
131 static int
vmw_svga_winsys_fence_signalled(struct svga_winsys_screen * sws,struct pipe_fence_handle * fence,unsigned flag)132 vmw_svga_winsys_fence_signalled(struct svga_winsys_screen *sws,
133                                 struct pipe_fence_handle *fence,
134                                 unsigned flag)
135 {
136    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
137 
138    return vmw_fence_signalled(vws, fence, flag);
139 }
140 
141 
142 static int
vmw_svga_winsys_fence_finish(struct svga_winsys_screen * sws,struct pipe_fence_handle * fence,unsigned flag)143 vmw_svga_winsys_fence_finish(struct svga_winsys_screen *sws,
144                              struct pipe_fence_handle *fence,
145                              unsigned flag)
146 {
147    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
148 
149    return vmw_fence_finish(vws, fence, flag);
150 }
151 
152 
153 
154 static struct svga_winsys_surface *
vmw_svga_winsys_surface_create(struct svga_winsys_screen * sws,SVGA3dSurfaceFlags flags,SVGA3dSurfaceFormat format,SVGA3dSize size,uint32 numFaces,uint32 numMipLevels)155 vmw_svga_winsys_surface_create(struct svga_winsys_screen *sws,
156                                SVGA3dSurfaceFlags flags,
157                                SVGA3dSurfaceFormat format,
158                                SVGA3dSize size,
159                                uint32 numFaces,
160                                uint32 numMipLevels)
161 {
162    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
163    struct vmw_svga_winsys_surface *surface;
164 
165    surface = CALLOC_STRUCT(vmw_svga_winsys_surface);
166    if(!surface)
167       goto no_surface;
168 
169    pipe_reference_init(&surface->refcnt, 1);
170    p_atomic_set(&surface->validated, 0);
171    surface->screen = vws;
172    surface->sid = vmw_ioctl_surface_create(vws,
173                                            flags, format, size,
174                                            numFaces, numMipLevels);
175    if(surface->sid == SVGA3D_INVALID_ID)
176       goto no_sid;
177 
178    return svga_winsys_surface(surface);
179 
180 no_sid:
181    FREE(surface);
182 no_surface:
183    return NULL;
184 }
185 
186 
187 static boolean
vmw_svga_winsys_surface_is_flushed(struct svga_winsys_screen * sws,struct svga_winsys_surface * surface)188 vmw_svga_winsys_surface_is_flushed(struct svga_winsys_screen *sws,
189                                    struct svga_winsys_surface *surface)
190 {
191    struct vmw_svga_winsys_surface *vsurf = vmw_svga_winsys_surface(surface);
192    return (p_atomic_read(&vsurf->validated) == 0);
193 }
194 
195 
196 static void
vmw_svga_winsys_surface_ref(struct svga_winsys_screen * sws,struct svga_winsys_surface ** pDst,struct svga_winsys_surface * src)197 vmw_svga_winsys_surface_ref(struct svga_winsys_screen *sws,
198 			    struct svga_winsys_surface **pDst,
199 			    struct svga_winsys_surface *src)
200 {
201    struct vmw_svga_winsys_surface *d_vsurf = vmw_svga_winsys_surface(*pDst);
202    struct vmw_svga_winsys_surface *s_vsurf = vmw_svga_winsys_surface(src);
203 
204    vmw_svga_winsys_surface_reference(&d_vsurf, s_vsurf);
205    *pDst = svga_winsys_surface(d_vsurf);
206 }
207 
208 
209 static void
vmw_svga_winsys_destroy(struct svga_winsys_screen * sws)210 vmw_svga_winsys_destroy(struct svga_winsys_screen *sws)
211 {
212    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
213 
214    vmw_winsys_destroy(vws);
215 }
216 
217 
218 static SVGA3dHardwareVersion
vmw_svga_winsys_get_hw_version(struct svga_winsys_screen * sws)219 vmw_svga_winsys_get_hw_version(struct svga_winsys_screen *sws)
220 {
221    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
222 
223    return (SVGA3dHardwareVersion) vws->ioctl.hwversion;
224 }
225 
226 
227 static boolean
vmw_svga_winsys_get_cap(struct svga_winsys_screen * sws,SVGA3dDevCapIndex index,SVGA3dDevCapResult * result)228 vmw_svga_winsys_get_cap(struct svga_winsys_screen *sws,
229                         SVGA3dDevCapIndex index,
230                         SVGA3dDevCapResult *result)
231 {
232    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
233    const uint32 *capsBlock;
234    const SVGA3dCapsRecord *capsRecord = NULL;
235    uint32 offset;
236    const SVGA3dCapPair *capArray;
237    int numCaps, first, last;
238 
239    if(vws->ioctl.hwversion < SVGA3D_HWVERSION_WS6_B1)
240       return FALSE;
241 
242    /*
243     * Search linearly through the caps block records for the specified type.
244     */
245    capsBlock = (const uint32 *)vws->ioctl.buffer;
246    for (offset = 0; capsBlock[offset] != 0; offset += capsBlock[offset]) {
247       const SVGA3dCapsRecord *record;
248       assert(offset < SVGA_FIFO_3D_CAPS_SIZE);
249       record = (const SVGA3dCapsRecord *) (capsBlock + offset);
250       if ((record->header.type >= SVGA3DCAPS_RECORD_DEVCAPS_MIN) &&
251           (record->header.type <= SVGA3DCAPS_RECORD_DEVCAPS_MAX) &&
252           (!capsRecord || (record->header.type > capsRecord->header.type))) {
253          capsRecord = record;
254       }
255    }
256 
257    if(!capsRecord)
258       return FALSE;
259 
260    /*
261     * Calculate the number of caps from the size of the record.
262     */
263    capArray = (const SVGA3dCapPair *) capsRecord->data;
264    numCaps = (int) ((capsRecord->header.length * sizeof(uint32) -
265                      sizeof capsRecord->header) / (2 * sizeof(uint32)));
266 
267    /*
268     * Binary-search for the cap with the specified index.
269     */
270    for (first = 0, last = numCaps - 1; first <= last; ) {
271       int mid = (first + last) / 2;
272 
273       if ((SVGA3dDevCapIndex) capArray[mid][0] == index) {
274          /*
275           * Found it.
276           */
277          result->u = capArray[mid][1];
278          return TRUE;
279       }
280 
281       /*
282        * Divide and conquer.
283        */
284       if ((SVGA3dDevCapIndex) capArray[mid][0] > index) {
285          last = mid - 1;
286       } else {
287          first = mid + 1;
288       }
289    }
290 
291    return FALSE;
292 }
293 
294 
295 boolean
vmw_winsys_screen_init_svga(struct vmw_winsys_screen * vws)296 vmw_winsys_screen_init_svga(struct vmw_winsys_screen *vws)
297 {
298    vws->base.destroy = vmw_svga_winsys_destroy;
299    vws->base.get_hw_version = vmw_svga_winsys_get_hw_version;
300    vws->base.get_cap = vmw_svga_winsys_get_cap;
301    vws->base.context_create = vmw_svga_winsys_context_create;
302    vws->base.surface_create = vmw_svga_winsys_surface_create;
303    vws->base.surface_is_flushed = vmw_svga_winsys_surface_is_flushed;
304    vws->base.surface_reference = vmw_svga_winsys_surface_ref;
305    vws->base.buffer_create = vmw_svga_winsys_buffer_create;
306    vws->base.buffer_map = vmw_svga_winsys_buffer_map;
307    vws->base.buffer_unmap = vmw_svga_winsys_buffer_unmap;
308    vws->base.buffer_destroy = vmw_svga_winsys_buffer_destroy;
309    vws->base.fence_reference = vmw_svga_winsys_fence_reference;
310    vws->base.fence_signalled = vmw_svga_winsys_fence_signalled;
311    vws->base.fence_finish = vmw_svga_winsys_fence_finish;
312 
313    return TRUE;
314 }
315 
316 
317