Debian Codegraph @

src/gallium/winsys/svga/drm/vmw_screen_dri.c

raw
1 /*
2 * Copyright (c) 2009-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "util/compiler.h"
9 #include "util/u_inlines.h"
10 #include "util/u_memory.h"
11 #include "util/format/u_format.h"
12
13 #include "vmw_context.h"
14 #include "vmw_screen.h"
15 #include "vmw_surface.h"
16 #include "vmw_buffer.h"
17 #include "svga_drm_public.h"
18 #include "vmw_surf_defs.h"
19
20 #include "frontend/drm_driver.h"
21
22 #include "vmwgfx_drm.h"
23 #include <xf86drm.h>
24 #include "drm-uapi/drm_fourcc.h"
25
26 #include <stdio.h>
27 #include <fcntl.h>
28
29 struct dri1_api_version {
30 int major;
31 int minor;
32 int patch_level;
33 };
34
35 static struct svga_winsys_surface *
36 vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,
37 struct winsys_handle *whandle,
38 SVGA3dSurfaceFormat *format);
39
40 static struct svga_winsys_surface *
41 vmw_drm_gb_surface_from_handle(struct svga_winsys_screen *sws,
42 struct winsys_handle *whandle,
43 SVGA3dSurfaceFormat *format);
44 static bool
45 vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
46 struct svga_winsys_surface *surface,
47 unsigned stride,
48 struct winsys_handle *whandle);
49
50 static struct dri1_api_version drm_required = { 2, 1, 0 };
51 static struct dri1_api_version drm_compat = { 2, 0, 0 };
52
53 static bool
54 vmw_dri1_check_version(const struct dri1_api_version *cur,
55 const struct dri1_api_version *required,
56 const struct dri1_api_version *compat,
57 const char component[])
58 {
59 if (cur->major > required->major && cur->major <= compat->major)
60 return true;
61 if (cur->major == required->major && cur->minor >= required->minor)
62 return true;
63
64 vmw_error("%s version failure.\n", component);
65 vmw_error("%s version is %d.%d.%d and this driver can only work\n"
66 "with versions %d.%d.x through %d.x.x.\n",
67 component,
68 cur->major, cur->minor, cur->patch_level,
69 required->major, required->minor, compat->major);
70 return false;
71 }
72
73 /* This is actually the entrypoint to the entire driver,
74 * called by the target bootstrap code.
75 */
76 struct svga_winsys_screen *
77 svga_drm_winsys_screen_create(int fd)
78 {
79 struct vmw_winsys_screen *vws;
80 struct dri1_api_version drm_ver;
81 drmVersionPtr ver;
82
83 ver = drmGetVersion(fd);
84 if (ver == NULL)
85 return NULL;
86
87 drm_ver.major = ver->version_major;
88 drm_ver.minor = ver->version_minor;
89 drm_ver.patch_level = 0; /* ??? */
90
91 drmFreeVersion(ver);
92 if (!vmw_dri1_check_version(&drm_ver, &drm_required,
93 &drm_compat, "vmwgfx drm driver"))
94 return NULL;
95
96 vws = vmw_winsys_create(fd);
97 if (!vws)
98 goto out_no_vws;
99
100 /* XXX do this properly */
101 vws->base.surface_from_handle = vws->base.have_gb_objects ?
102 vmw_drm_gb_surface_from_handle : vmw_drm_surface_from_handle;
103 vws->base.surface_get_handle = vmw_drm_surface_get_handle;
104
105 return &vws->base;
106
107 out_no_vws:
108 return NULL;
109 }
110
111 /**
112 * vmw_drm_gb_surface_from_handle - Create a shared surface
113 *
114 * @sws: Screen to register the surface with.
115 * @whandle: struct winsys_handle identifying the kernel surface object
116 * @format: On successful return points to a value describing the
117 * surface format.
118 *
119 * Returns a refcounted pointer to a struct svga_winsys_surface
120 * embedded in a struct vmw_svga_winsys_surface on success or NULL
121 * on failure.
122 */
123 static struct svga_winsys_surface *
124 vmw_drm_gb_surface_from_handle(struct svga_winsys_screen *sws,
125 struct winsys_handle *whandle,
126 SVGA3dSurfaceFormat *format)
127 {
128 struct vmw_svga_winsys_surface *vsrf;
129 struct svga_winsys_surface *ssrf;
130 struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
131 SVGA3dSurfaceAllFlags flags;
132 uint32_t mip_levels;
133 struct vmw_buffer_desc desc;
134 struct pb_manager *provider = vws->pools.dma_base;
135 struct pb_buffer *pb_buf;
136 uint32_t handle;
137 int ret;
138
139 if (whandle->offset != 0) {
140 fprintf(stderr, "Attempt to import unsupported winsys offset %u\n",
141 whandle->offset);
142 return NULL;
143 }
144
145 ret = vmw_ioctl_gb_surface_ref(vws, whandle, &flags, format,
146 &mip_levels, &handle, &desc.region);
147
148 if (ret) {
149 fprintf(stderr, "Failed referencing shared surface. SID %d.\n"
150 "Error %d (%s).\n",
151 whandle->handle, ret, strerror(-ret));
152 return NULL;
153 }
154
155 if (mip_levels != 1) {
156 fprintf(stderr, "Incorrect number of mipmap levels on shared surface."
157 " SID %d, levels %d\n",
158 whandle->handle, mip_levels);
159 goto out_mip;
160 }
161
162 vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
163 if (!vsrf)
164 goto out_mip;
165
166 pipe_reference_init(&vsrf->refcnt, 1);
167 p_atomic_set(&vsrf->validated, 0);
168 vsrf->screen = vws;
169 vsrf->sid = handle;
170 vsrf->size = vmw_region_size(desc.region);
171
172 /*
173 * Synchronize backing buffers of shared surfaces using the
174 * kernel, since we don't pass fence objects around between
175 * processes.
176 */
177 desc.pb_desc.alignment = 4096;
178 desc.pb_desc.usage = VMW_BUFFER_USAGE_SHARED | VMW_BUFFER_USAGE_SYNC;
179 pb_buf = provider->create_buffer(provider, vsrf->size, &desc.pb_desc);
180 vsrf->buf = vmw_svga_winsys_buffer_wrap(pb_buf);
181 if (!vsrf->buf)
182 goto out_no_buf;
183 ssrf = svga_winsys_surface(vsrf);
184
185 return ssrf;
186
187 out_no_buf:
188 FREE(vsrf);
189 out_mip:
190 vmw_ioctl_region_destroy(desc.region);
191 vmw_ioctl_surface_destroy(vws, whandle->handle);
192 return NULL;
193 }
194
195 static struct svga_winsys_surface *
196 vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,
197 struct winsys_handle *whandle,
198 SVGA3dSurfaceFormat *format)
199 {
200 struct vmw_svga_winsys_surface *vsrf;
201 struct svga_winsys_surface *ssrf;
202 struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
203 union drm_vmw_surface_reference_arg arg;
204 struct drm_vmw_surface_arg *req = &arg.req;
205 struct drm_vmw_surface_create_req *rep = &arg.rep;
206 uint32_t handle = 0;
207 struct drm_vmw_size size;
208 SVGA3dSize base_size;
209 int ret;
210 int i;
211
212 if (whandle->offset != 0) {
213 fprintf(stderr, "Attempt to import unsupported winsys offset %u\n",
214 whandle->offset);
215 return NULL;
216 }
217
218 switch (whandle->type) {
219 case WINSYS_HANDLE_TYPE_SHARED:
220 case WINSYS_HANDLE_TYPE_KMS:
221 handle = whandle->handle;
222 break;
223 case WINSYS_HANDLE_TYPE_FD:
224 ret = drmPrimeFDToHandle(vws->ioctl.drm_fd, whandle->handle,
225 &handle);
226 if (ret) {
227 vmw_error("Failed to get handle from prime fd %d.\n",
228 (int) whandle->handle);
229 return NULL;
230 }
231 break;
232 default:
233 vmw_error("Attempt to import unsupported handle type %d.\n",
234 whandle->type);
235 return NULL;
236 }
237
238 memset(&arg, 0, sizeof(arg));
239 req->sid = handle;
240 rep->size_addr = (unsigned long)&size;
241
242 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_REF_SURFACE,
243 &arg, sizeof(arg));
244
245 /*
246 * Need to close the handle we got from prime.
247 */
248 if (whandle->type == WINSYS_HANDLE_TYPE_FD)
249 vmw_ioctl_surface_destroy(vws, handle);
250
251 if (ret) {
252 /*
253 * Any attempt to share something other than a surface, like a dumb
254 * kms buffer, should fail here.
255 */
256 vmw_error("Failed referencing shared surface. SID %d.\n"
257 "Error %d (%s).\n",
258 handle, ret, strerror(-ret));
259 return NULL;
260 }
261
262 if (rep->mip_levels[0] != 1) {
263 vmw_error("Incorrect number of mipmap levels on shared surface."
264 " SID %d, levels %d\n",
265 handle, rep->mip_levels[0]);
266 goto out_mip;
267 }
268
269 for (i=1; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
270 if (rep->mip_levels[i] != 0) {
271 vmw_error("Incorrect number of faces levels on shared surface."
272 " SID %d, face %d present.\n",
273 handle, i);
274 goto out_mip;
275 }
276 }
277
278 vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
279 if (!vsrf)
280 goto out_mip;
281
282 pipe_reference_init(&vsrf->refcnt, 1);
283 p_atomic_set(&vsrf->validated, 0);
284 vsrf->screen = vws;
285 vsrf->sid = handle;
286 ssrf = svga_winsys_surface(vsrf);
287 *format = rep->format;
288
289 /* Estimate usage, for early flushing. */
290
291 base_size.width = size.width;
292 base_size.height = size.height;
293 base_size.depth = size.depth;
294 vsrf->size = vmw_surf_get_serialized_size(rep->format, base_size,
295 rep->mip_levels[0],
296 false);
297
298 return ssrf;
299
300 out_mip:
301 vmw_ioctl_surface_destroy(vws, handle);
302
303 return NULL;
304 }
305
306 static bool
307 vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
308 struct svga_winsys_surface *surface,
309 unsigned stride,
310 struct winsys_handle *whandle)
311 {
312 struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
313 struct vmw_svga_winsys_surface *vsrf;
314 int ret;
315
316 if (!surface)
317 return false;
318
319 vsrf = vmw_svga_winsys_surface(surface);
320 whandle->handle = vsrf->sid;
321 whandle->stride = stride;
322 whandle->offset = 0;
323 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
324
325 switch (whandle->type) {
326 case WINSYS_HANDLE_TYPE_SHARED:
327 case WINSYS_HANDLE_TYPE_KMS:
328 whandle->handle = vsrf->sid;
329 break;
330 case WINSYS_HANDLE_TYPE_FD:
331 ret = drmPrimeHandleToFD(vws->ioctl.drm_fd, vsrf->sid, DRM_CLOEXEC | DRM_RDWR,
332 (int *)&whandle->handle);
333 if (ret) {
334 vmw_error("Failed to get file descriptor from prime.\n");
335 return false;
336 }
337 break;
338 default:
339 vmw_error("Attempt to export unsupported handle type %d.\n",
340 whandle->type);
341 return false;
342 }
343
344 return true;
345 }