1 /**********************************************************
2 * Copyright 2009-2015 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
27 #include "pipe/p_compiler.h"
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_format.h"
32 #include "vmw_context.h"
33 #include "vmw_screen.h"
34 #include "vmw_surface.h"
35 #include "vmw_buffer.h"
36 #include "svga_drm_public.h"
37 #include "svga3d_surfacedefs.h"
39 #include "state_tracker/drm_driver.h"
41 #include "vmwgfx_drm.h"
47 struct dri1_api_version
{
53 static struct svga_winsys_surface
*
54 vmw_drm_surface_from_handle(struct svga_winsys_screen
*sws
,
55 struct winsys_handle
*whandle
,
56 SVGA3dSurfaceFormat
*format
);
58 static struct svga_winsys_surface
*
59 vmw_drm_gb_surface_from_handle(struct svga_winsys_screen
*sws
,
60 struct winsys_handle
*whandle
,
61 SVGA3dSurfaceFormat
*format
);
63 vmw_drm_surface_get_handle(struct svga_winsys_screen
*sws
,
64 struct svga_winsys_surface
*surface
,
66 struct winsys_handle
*whandle
);
68 static struct dri1_api_version drm_required
= { 2, 1, 0 };
69 static struct dri1_api_version drm_compat
= { 2, 0, 0 };
72 vmw_dri1_check_version(const struct dri1_api_version
*cur
,
73 const struct dri1_api_version
*required
,
74 const struct dri1_api_version
*compat
,
75 const char component
[])
77 if (cur
->major
> required
->major
&& cur
->major
<= compat
->major
)
79 if (cur
->major
== required
->major
&& cur
->minor
>= required
->minor
)
82 vmw_error("%s version failure.\n", component
);
83 vmw_error("%s version is %d.%d.%d and this driver can only work\n"
84 "with versions %d.%d.x through %d.x.x.\n",
86 cur
->major
, cur
->minor
, cur
->patch_level
,
87 required
->major
, required
->minor
, compat
->major
);
91 /* This is actually the entrypoint to the entire driver,
92 * called by the target bootstrap code.
94 struct svga_winsys_screen
*
95 svga_drm_winsys_screen_create(int fd
)
97 struct vmw_winsys_screen
*vws
;
98 struct dri1_api_version drm_ver
;
101 ver
= drmGetVersion(fd
);
105 drm_ver
.major
= ver
->version_major
;
106 drm_ver
.minor
= ver
->version_minor
;
107 drm_ver
.patch_level
= 0; /* ??? */
110 if (!vmw_dri1_check_version(&drm_ver
, &drm_required
,
111 &drm_compat
, "vmwgfx drm driver"))
114 vws
= vmw_winsys_create(fd
);
118 /* XXX do this properly */
119 vws
->base
.surface_from_handle
= vws
->base
.have_gb_objects
?
120 vmw_drm_gb_surface_from_handle
: vmw_drm_surface_from_handle
;
121 vws
->base
.surface_get_handle
= vmw_drm_surface_get_handle
;
130 * vmw_drm_gb_surface_from_handle - Create a shared surface
132 * @sws: Screen to register the surface with.
133 * @whandle: struct winsys_handle identifying the kernel surface object
134 * @format: On successful return points to a value describing the
137 * Returns a refcounted pointer to a struct svga_winsys_surface
138 * embedded in a struct vmw_svga_winsys_surface on success or NULL
141 static struct svga_winsys_surface
*
142 vmw_drm_gb_surface_from_handle(struct svga_winsys_screen
*sws
,
143 struct winsys_handle
*whandle
,
144 SVGA3dSurfaceFormat
*format
)
146 struct vmw_svga_winsys_surface
*vsrf
;
147 struct svga_winsys_surface
*ssrf
;
148 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
149 SVGA3dSurfaceAllFlags flags
;
151 struct vmw_buffer_desc desc
;
152 struct pb_manager
*provider
= vws
->pools
.gmr
;
153 struct pb_buffer
*pb_buf
;
157 if (whandle
->offset
!= 0) {
158 fprintf(stderr
, "Attempt to import unsupported winsys offset %u\n",
163 ret
= vmw_ioctl_gb_surface_ref(vws
, whandle
, &flags
, format
,
164 &mip_levels
, &handle
, &desc
.region
);
167 fprintf(stderr
, "Failed referencing shared surface. SID %d.\n"
169 whandle
->handle
, ret
, strerror(-ret
));
173 if (mip_levels
!= 1) {
174 fprintf(stderr
, "Incorrect number of mipmap levels on shared surface."
175 " SID %d, levels %d\n",
176 whandle
->handle
, mip_levels
);
180 vsrf
= CALLOC_STRUCT(vmw_svga_winsys_surface
);
184 pipe_reference_init(&vsrf
->refcnt
, 1);
185 p_atomic_set(&vsrf
->validated
, 0);
188 vsrf
->size
= vmw_region_size(desc
.region
);
191 * Synchronize backing buffers of shared surfaces using the
192 * kernel, since we don't pass fence objects around between
195 desc
.pb_desc
.alignment
= 4096;
196 desc
.pb_desc
.usage
= VMW_BUFFER_USAGE_SHARED
| VMW_BUFFER_USAGE_SYNC
;
197 pb_buf
= provider
->create_buffer(provider
, vsrf
->size
, &desc
.pb_desc
);
198 vsrf
->buf
= vmw_svga_winsys_buffer_wrap(pb_buf
);
201 ssrf
= svga_winsys_surface(vsrf
);
208 vmw_ioctl_region_destroy(desc
.region
);
209 vmw_ioctl_surface_destroy(vws
, whandle
->handle
);
213 static struct svga_winsys_surface
*
214 vmw_drm_surface_from_handle(struct svga_winsys_screen
*sws
,
215 struct winsys_handle
*whandle
,
216 SVGA3dSurfaceFormat
*format
)
218 struct vmw_svga_winsys_surface
*vsrf
;
219 struct svga_winsys_surface
*ssrf
;
220 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
221 union drm_vmw_surface_reference_arg arg
;
222 struct drm_vmw_surface_arg
*req
= &arg
.req
;
223 struct drm_vmw_surface_create_req
*rep
= &arg
.rep
;
225 struct drm_vmw_size size
;
226 SVGA3dSize base_size
;
230 if (whandle
->offset
!= 0) {
231 fprintf(stderr
, "Attempt to import unsupported winsys offset %u\n",
236 switch (whandle
->type
) {
237 case WINSYS_HANDLE_TYPE_SHARED
:
238 case WINSYS_HANDLE_TYPE_KMS
:
239 handle
= whandle
->handle
;
241 case WINSYS_HANDLE_TYPE_FD
:
242 ret
= drmPrimeFDToHandle(vws
->ioctl
.drm_fd
, whandle
->handle
,
245 vmw_error("Failed to get handle from prime fd %d.\n",
246 (int) whandle
->handle
);
251 vmw_error("Attempt to import unsupported handle type %d.\n",
256 memset(&arg
, 0, sizeof(arg
));
258 rep
->size_addr
= (unsigned long)&size
;
260 ret
= drmCommandWriteRead(vws
->ioctl
.drm_fd
, DRM_VMW_REF_SURFACE
,
264 * Need to close the handle we got from prime.
266 if (whandle
->type
== WINSYS_HANDLE_TYPE_FD
)
267 vmw_ioctl_surface_destroy(vws
, handle
);
271 * Any attempt to share something other than a surface, like a dumb
272 * kms buffer, should fail here.
274 vmw_error("Failed referencing shared surface. SID %d.\n"
276 handle
, ret
, strerror(-ret
));
280 if (rep
->mip_levels
[0] != 1) {
281 vmw_error("Incorrect number of mipmap levels on shared surface."
282 " SID %d, levels %d\n",
283 handle
, rep
->mip_levels
[0]);
287 for (i
=1; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
) {
288 if (rep
->mip_levels
[i
] != 0) {
289 vmw_error("Incorrect number of faces levels on shared surface."
290 " SID %d, face %d present.\n",
296 vsrf
= CALLOC_STRUCT(vmw_svga_winsys_surface
);
300 pipe_reference_init(&vsrf
->refcnt
, 1);
301 p_atomic_set(&vsrf
->validated
, 0);
304 ssrf
= svga_winsys_surface(vsrf
);
305 *format
= rep
->format
;
307 /* Estimate usage, for early flushing. */
309 base_size
.width
= size
.width
;
310 base_size
.height
= size
.height
;
311 base_size
.depth
= size
.depth
;
312 vsrf
->size
= svga3dsurface_get_serialized_size(rep
->format
, base_size
,
319 vmw_ioctl_surface_destroy(vws
, handle
);
325 vmw_drm_surface_get_handle(struct svga_winsys_screen
*sws
,
326 struct svga_winsys_surface
*surface
,
328 struct winsys_handle
*whandle
)
330 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
331 struct vmw_svga_winsys_surface
*vsrf
;
337 vsrf
= vmw_svga_winsys_surface(surface
);
338 whandle
->handle
= vsrf
->sid
;
339 whandle
->stride
= stride
;
342 switch (whandle
->type
) {
343 case WINSYS_HANDLE_TYPE_SHARED
:
344 case WINSYS_HANDLE_TYPE_KMS
:
345 whandle
->handle
= vsrf
->sid
;
347 case WINSYS_HANDLE_TYPE_FD
:
348 ret
= drmPrimeHandleToFD(vws
->ioctl
.drm_fd
, vsrf
->sid
, DRM_CLOEXEC
,
349 (int *)&whandle
->handle
);
351 vmw_error("Failed to get file descriptor from prime.\n");
356 vmw_error("Attempt to export unsupported handle type %d.\n",