~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/winsys/svga/drm/vmw_screen_dri.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**********************************************************
2
 
 * Copyright 2009-2015 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 "pipe/p_compiler.h"
28
 
#include "util/u_inlines.h"
29
 
#include "util/u_memory.h"
30
 
#include "util/format/u_format.h"
31
 
 
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"
38
 
 
39
 
#include "frontend/drm_driver.h"
40
 
 
41
 
#include "vmwgfx_drm.h"
42
 
#include <xf86drm.h>
43
 
 
44
 
#include <stdio.h>
45
 
#include <fcntl.h>
46
 
 
47
 
struct dri1_api_version {
48
 
   int major;
49
 
   int minor;
50
 
   int patch_level;
51
 
};
52
 
 
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);
57
 
 
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);
62
 
static boolean
63
 
vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
64
 
                           struct svga_winsys_surface *surface,
65
 
                           unsigned stride,
66
 
                           struct winsys_handle *whandle);
67
 
 
68
 
static struct dri1_api_version drm_required = { 2, 1, 0 };
69
 
static struct dri1_api_version drm_compat = { 2, 0, 0 };
70
 
 
71
 
static boolean
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[])
76
 
{
77
 
   if (cur->major > required->major && cur->major <= compat->major)
78
 
      return TRUE;
79
 
   if (cur->major == required->major && cur->minor >= required->minor)
80
 
      return TRUE;
81
 
 
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",
85
 
             component,
86
 
             cur->major, cur->minor, cur->patch_level,
87
 
             required->major, required->minor, compat->major);
88
 
   return FALSE;
89
 
}
90
 
 
91
 
/* This is actually the entrypoint to the entire driver,
92
 
 * called by the target bootstrap code.
93
 
 */
94
 
struct svga_winsys_screen *
95
 
svga_drm_winsys_screen_create(int fd)
96
 
{
97
 
   struct vmw_winsys_screen *vws;
98
 
   struct dri1_api_version drm_ver;
99
 
   drmVersionPtr ver;
100
 
 
101
 
   ver = drmGetVersion(fd);
102
 
   if (ver == NULL)
103
 
      return NULL;
104
 
 
105
 
   drm_ver.major = ver->version_major;
106
 
   drm_ver.minor = ver->version_minor;
107
 
   drm_ver.patch_level = 0; /* ??? */
108
 
 
109
 
   drmFreeVersion(ver);
110
 
   if (!vmw_dri1_check_version(&drm_ver, &drm_required,
111
 
                               &drm_compat, "vmwgfx drm driver"))
112
 
      return NULL;
113
 
 
114
 
   vws = vmw_winsys_create(fd);
115
 
   if (!vws)
116
 
      goto out_no_vws;
117
 
 
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;
122
 
 
123
 
   return &vws->base;
124
 
 
125
 
out_no_vws:
126
 
   return NULL;
127
 
}
128
 
 
129
 
/**
130
 
 * vmw_drm_gb_surface_from_handle - Create a shared surface
131
 
 *
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
135
 
 * surface format.
136
 
 *
137
 
 * Returns a refcounted pointer to a struct svga_winsys_surface
138
 
 * embedded in a struct vmw_svga_winsys_surface on success or NULL
139
 
 * on failure.
140
 
 */
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)
145
 
{
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;
150
 
    uint32_t mip_levels;
151
 
    struct vmw_buffer_desc desc;
152
 
    struct pb_manager *provider = vws->pools.gmr;
153
 
    struct pb_buffer *pb_buf;
154
 
    uint32_t handle;
155
 
    int ret;
156
 
 
157
 
    if (whandle->offset != 0) {
158
 
       fprintf(stderr, "Attempt to import unsupported winsys offset %u\n",
159
 
               whandle->offset);
160
 
       return NULL;
161
 
    }
162
 
 
163
 
    ret = vmw_ioctl_gb_surface_ref(vws, whandle, &flags, format,
164
 
                                   &mip_levels, &handle, &desc.region);
165
 
 
166
 
    if (ret) {
167
 
        fprintf(stderr, "Failed referencing shared surface. SID %d.\n"
168
 
                "Error %d (%s).\n",
169
 
                whandle->handle, ret, strerror(-ret));
170
 
        return NULL;
171
 
    }
172
 
 
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);
177
 
       goto out_mip;
178
 
    }
179
 
 
180
 
    vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
181
 
    if (!vsrf)
182
 
        goto out_mip;
183
 
 
184
 
    pipe_reference_init(&vsrf->refcnt, 1);
185
 
    p_atomic_set(&vsrf->validated, 0);
186
 
    vsrf->screen = vws;
187
 
    vsrf->sid = handle;
188
 
    vsrf->size = vmw_region_size(desc.region);
189
 
 
190
 
    /*
191
 
     * Synchronize backing buffers of shared surfaces using the
192
 
     * kernel, since we don't pass fence objects around between
193
 
     * processes.
194
 
     */
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);
199
 
    if (!vsrf->buf)
200
 
       goto out_no_buf;
201
 
    ssrf = svga_winsys_surface(vsrf);
202
 
 
203
 
    return ssrf;
204
 
 
205
 
out_no_buf:
206
 
    FREE(vsrf);
207
 
out_mip:
208
 
    vmw_ioctl_region_destroy(desc.region);
209
 
    vmw_ioctl_surface_destroy(vws, whandle->handle);
210
 
    return NULL;
211
 
}
212
 
 
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)
217
 
{
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;
224
 
    uint32_t handle = 0;
225
 
    struct drm_vmw_size size;
226
 
    SVGA3dSize base_size;
227
 
    int ret;
228
 
    int i;
229
 
 
230
 
    if (whandle->offset != 0) {
231
 
       fprintf(stderr, "Attempt to import unsupported winsys offset %u\n",
232
 
               whandle->offset);
233
 
       return NULL;
234
 
    }
235
 
 
236
 
    switch (whandle->type) {
237
 
    case WINSYS_HANDLE_TYPE_SHARED:
238
 
    case WINSYS_HANDLE_TYPE_KMS:
239
 
       handle = whandle->handle;
240
 
       break;
241
 
    case WINSYS_HANDLE_TYPE_FD:
242
 
       ret = drmPrimeFDToHandle(vws->ioctl.drm_fd, whandle->handle,
243
 
                                &handle);
244
 
       if (ret) {
245
 
          vmw_error("Failed to get handle from prime fd %d.\n",
246
 
                    (int) whandle->handle);
247
 
          return NULL;
248
 
       }
249
 
       break;
250
 
    default:
251
 
       vmw_error("Attempt to import unsupported handle type %d.\n",
252
 
                 whandle->type);
253
 
       return NULL;
254
 
    }
255
 
 
256
 
    memset(&arg, 0, sizeof(arg));
257
 
    req->sid = handle;
258
 
    rep->size_addr = (unsigned long)&size;
259
 
 
260
 
    ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_REF_SURFACE,
261
 
                              &arg, sizeof(arg));
262
 
 
263
 
    /*
264
 
     * Need to close the handle we got from prime.
265
 
     */
266
 
    if (whandle->type == WINSYS_HANDLE_TYPE_FD)
267
 
       vmw_ioctl_surface_destroy(vws, handle);
268
 
 
269
 
    if (ret) {
270
 
       /*
271
 
        * Any attempt to share something other than a surface, like a dumb
272
 
        * kms buffer, should fail here.
273
 
        */
274
 
       vmw_error("Failed referencing shared surface. SID %d.\n"
275
 
                 "Error %d (%s).\n",
276
 
                 handle, ret, strerror(-ret));
277
 
       return NULL;
278
 
    }
279
 
 
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]);
284
 
        goto out_mip;
285
 
    }
286
 
 
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",
291
 
                      handle, i);
292
 
            goto out_mip;
293
 
        }
294
 
   }
295
 
 
296
 
    vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
297
 
    if (!vsrf)
298
 
        goto out_mip;
299
 
 
300
 
    pipe_reference_init(&vsrf->refcnt, 1);
301
 
    p_atomic_set(&vsrf->validated, 0);
302
 
    vsrf->screen = vws;
303
 
    vsrf->sid = handle;
304
 
    ssrf = svga_winsys_surface(vsrf);
305
 
    *format = rep->format;
306
 
 
307
 
    /* Estimate usage, for early flushing. */
308
 
 
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,
313
 
                                                   rep->mip_levels[0],
314
 
                                                   FALSE);
315
 
 
316
 
    return ssrf;
317
 
 
318
 
out_mip:
319
 
    vmw_ioctl_surface_destroy(vws, handle);
320
 
 
321
 
    return NULL;
322
 
}
323
 
 
324
 
static boolean
325
 
vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
326
 
                           struct svga_winsys_surface *surface,
327
 
                           unsigned stride,
328
 
                           struct winsys_handle *whandle)
329
 
{
330
 
    struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
331
 
    struct vmw_svga_winsys_surface *vsrf;
332
 
    int ret;
333
 
 
334
 
    if (!surface)
335
 
        return FALSE;
336
 
 
337
 
    vsrf = vmw_svga_winsys_surface(surface);
338
 
    whandle->handle = vsrf->sid;
339
 
    whandle->stride = stride;
340
 
    whandle->offset = 0;
341
 
 
342
 
    switch (whandle->type) {
343
 
    case WINSYS_HANDLE_TYPE_SHARED:
344
 
    case WINSYS_HANDLE_TYPE_KMS:
345
 
       whandle->handle = vsrf->sid;
346
 
       break;
347
 
    case WINSYS_HANDLE_TYPE_FD:
348
 
       ret = drmPrimeHandleToFD(vws->ioctl.drm_fd, vsrf->sid, DRM_CLOEXEC,
349
 
                                (int *)&whandle->handle);
350
 
       if (ret) {
351
 
          vmw_error("Failed to get file descriptor from prime.\n");
352
 
          return FALSE;
353
 
       }
354
 
       break;
355
 
    default:
356
 
       vmw_error("Attempt to export unsupported handle type %d.\n",
357
 
                 whandle->type);
358
 
       return FALSE;
359
 
    }
360
 
 
361
 
    return TRUE;
362
 
}