xref: /qemu/hw/vfio/display.c (revision 8a2b516ba2855c4530388051de2b8d17bc780ea8)
1 /*
2  * display support for mdev based vgpu devices
3  *
4  * Copyright Red Hat, Inc. 2017
5  *
6  * Authors:
7  *    Gerd Hoffmann
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include <linux/vfio.h>
15 #include <sys/ioctl.h>
16 
17 #include "qemu/error-report.h"
18 #include "hw/display/edid.h"
19 #include "ui/console.h"
20 #include "qapi/error.h"
21 #include "pci.h"
22 #include "trace.h"
23 
24 #ifndef DRM_PLANE_TYPE_PRIMARY
25 # define DRM_PLANE_TYPE_PRIMARY 1
26 # define DRM_PLANE_TYPE_CURSOR  2
27 #endif
28 
29 #define pread_field(_fd, _reg, _ptr, _fld)                              \
30     (sizeof(_ptr->_fld) !=                                              \
31      pread(_fd, &(_ptr->_fld), sizeof(_ptr->_fld),                      \
32            _reg->offset + offsetof(typeof(*_ptr), _fld)))
33 
34 #define pwrite_field(_fd, _reg, _ptr, _fld)                             \
35     (sizeof(_ptr->_fld) !=                                              \
36      pwrite(_fd, &(_ptr->_fld), sizeof(_ptr->_fld),                     \
37             _reg->offset + offsetof(typeof(*_ptr), _fld)))
38 
39 
40 static void vfio_display_edid_link_up(void *opaque)
41 {
42     VFIOPCIDevice *vdev = opaque;
43     VFIODisplay *dpy = vdev->dpy;
44     int fd = vdev->vbasedev.fd;
45 
46     dpy->edid_regs->link_state = VFIO_DEVICE_GFX_LINK_STATE_UP;
47     if (pwrite_field(fd, dpy->edid_info, dpy->edid_regs, link_state)) {
48         goto err;
49     }
50     trace_vfio_display_edid_link_up();
51     return;
52 
53 err:
54     trace_vfio_display_edid_write_error();
55 }
56 
57 static void vfio_display_edid_update(VFIOPCIDevice *vdev, bool enabled,
58                                      int prefx, int prefy)
59 {
60     VFIODisplay *dpy = vdev->dpy;
61     int fd = vdev->vbasedev.fd;
62     qemu_edid_info edid = {
63         .maxx  = dpy->edid_regs->max_xres,
64         .maxy  = dpy->edid_regs->max_yres,
65         .prefx = prefx ?: vdev->display_xres,
66         .prefy = prefy ?: vdev->display_yres,
67     };
68 
69     timer_del(dpy->edid_link_timer);
70     dpy->edid_regs->link_state = VFIO_DEVICE_GFX_LINK_STATE_DOWN;
71     if (pwrite_field(fd, dpy->edid_info, dpy->edid_regs, link_state)) {
72         goto err;
73     }
74     trace_vfio_display_edid_link_down();
75 
76     if (!enabled) {
77         return;
78     }
79 
80     if (edid.maxx && edid.prefx > edid.maxx) {
81         edid.prefx = edid.maxx;
82     }
83     if (edid.maxy && edid.prefy > edid.maxy) {
84         edid.prefy = edid.maxy;
85     }
86     qemu_edid_generate(dpy->edid_blob,
87                        dpy->edid_regs->edid_max_size,
88                        &edid);
89     trace_vfio_display_edid_update(edid.prefx, edid.prefy);
90 
91     dpy->edid_regs->edid_size = qemu_edid_size(dpy->edid_blob);
92     if (pwrite_field(fd, dpy->edid_info, dpy->edid_regs, edid_size)) {
93         goto err;
94     }
95     if (pwrite(fd, dpy->edid_blob, dpy->edid_regs->edid_size,
96                dpy->edid_info->offset + dpy->edid_regs->edid_offset)
97         != dpy->edid_regs->edid_size) {
98         goto err;
99     }
100 
101     timer_mod(dpy->edid_link_timer,
102               qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 100);
103     return;
104 
105 err:
106     trace_vfio_display_edid_write_error();
107 }
108 
109 static void vfio_display_edid_ui_info(void *opaque, uint32_t idx,
110                                       QemuUIInfo *info)
111 {
112     VFIOPCIDevice *vdev = opaque;
113     VFIODisplay *dpy = vdev->dpy;
114 
115     if (!dpy->edid_regs) {
116         return;
117     }
118 
119     if (info->width && info->height) {
120         vfio_display_edid_update(vdev, true, info->width, info->height);
121     } else {
122         vfio_display_edid_update(vdev, false, 0, 0);
123     }
124 }
125 
126 static bool vfio_display_edid_init(VFIOPCIDevice *vdev, Error **errp)
127 {
128     VFIODisplay *dpy = vdev->dpy;
129     int fd = vdev->vbasedev.fd;
130     int ret;
131 
132     ret = vfio_get_dev_region_info(&vdev->vbasedev,
133                                    VFIO_REGION_TYPE_GFX,
134                                    VFIO_REGION_SUBTYPE_GFX_EDID,
135                                    &dpy->edid_info);
136     if (ret) {
137         /* Failed to get GFX edid info, allow to go through without edid. */
138         return true;
139     }
140 
141     trace_vfio_display_edid_available();
142     dpy->edid_regs = g_new0(struct vfio_region_gfx_edid, 1);
143     if (pread_field(fd, dpy->edid_info, dpy->edid_regs, edid_offset)) {
144         goto err;
145     }
146     if (pread_field(fd, dpy->edid_info, dpy->edid_regs, edid_max_size)) {
147         goto err;
148     }
149     if (pread_field(fd, dpy->edid_info, dpy->edid_regs, max_xres)) {
150         goto err;
151     }
152     if (pread_field(fd, dpy->edid_info, dpy->edid_regs, max_yres)) {
153         goto err;
154     }
155 
156     dpy->edid_blob = g_malloc0(dpy->edid_regs->edid_max_size);
157 
158     /* if xres + yres properties are unset use the maximum resolution */
159     if (!vdev->display_xres) {
160         vdev->display_xres = dpy->edid_regs->max_xres;
161     }
162     if (!vdev->display_yres) {
163         vdev->display_yres = dpy->edid_regs->max_yres;
164     }
165 
166     dpy->edid_link_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
167                                         vfio_display_edid_link_up, vdev);
168 
169     vfio_display_edid_update(vdev, true, 0, 0);
170     return true;
171 
172 err:
173     error_setg(errp, "vfio: failed to read GFX edid field");
174     trace_vfio_display_edid_write_error();
175     g_free(dpy->edid_info);
176     g_free(dpy->edid_regs);
177     dpy->edid_info = NULL;
178     dpy->edid_regs = NULL;
179     return false;
180 }
181 
182 static void vfio_display_edid_exit(VFIODisplay *dpy)
183 {
184     if (!dpy->edid_regs) {
185         return;
186     }
187 
188     g_free(dpy->edid_info);
189     g_free(dpy->edid_regs);
190     g_free(dpy->edid_blob);
191     timer_free(dpy->edid_link_timer);
192 }
193 
194 static void vfio_display_update_cursor(VFIODMABuf *dmabuf,
195                                        struct vfio_device_gfx_plane_info *plane)
196 {
197     if (dmabuf->pos_x != plane->x_pos || dmabuf->pos_y != plane->y_pos) {
198         dmabuf->pos_x      = plane->x_pos;
199         dmabuf->pos_y      = plane->y_pos;
200         dmabuf->pos_updates++;
201     }
202     if (dmabuf->hot_x != plane->x_hot || dmabuf->hot_y != plane->y_hot) {
203         dmabuf->hot_x      = plane->x_hot;
204         dmabuf->hot_y      = plane->y_hot;
205         dmabuf->hot_updates++;
206     }
207 }
208 
209 static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
210                                            uint32_t plane_type)
211 {
212     VFIODisplay *dpy = vdev->dpy;
213     struct vfio_device_gfx_plane_info plane;
214     VFIODMABuf *dmabuf;
215     int fd, ret;
216 
217     memset(&plane, 0, sizeof(plane));
218     plane.argsz = sizeof(plane);
219     plane.flags = VFIO_GFX_PLANE_TYPE_DMABUF;
220     plane.drm_plane_type = plane_type;
221     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
222     if (ret < 0) {
223         return NULL;
224     }
225     if (!plane.drm_format || !plane.size) {
226         return NULL;
227     }
228 
229     QTAILQ_FOREACH(dmabuf, &dpy->dmabuf.bufs, next) {
230         if (dmabuf->dmabuf_id == plane.dmabuf_id) {
231             /* found in list, move to head, return it */
232             QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
233             QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
234             if (plane_type == DRM_PLANE_TYPE_CURSOR) {
235                 vfio_display_update_cursor(dmabuf, &plane);
236             }
237             return dmabuf;
238         }
239     }
240 
241     fd = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &plane.dmabuf_id);
242     if (fd < 0) {
243         return NULL;
244     }
245 
246     dmabuf = g_new0(VFIODMABuf, 1);
247     dmabuf->dmabuf_id  = plane.dmabuf_id;
248     dmabuf->buf = qemu_dmabuf_new(plane.width, plane.height,
249                                   plane.stride, 0, 0, plane.width,
250                                   plane.height, plane.drm_format,
251                                   plane.drm_format_mod, fd, false, false);
252 
253     if (plane_type == DRM_PLANE_TYPE_CURSOR) {
254         vfio_display_update_cursor(dmabuf, &plane);
255     }
256 
257     QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
258     return dmabuf;
259 }
260 
261 static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)
262 {
263     QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
264 
265     qemu_dmabuf_close(dmabuf->buf);
266     dpy_gl_release_dmabuf(dpy->con, dmabuf->buf);
267     g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free);
268     g_free(dmabuf);
269 }
270 
271 static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev)
272 {
273     VFIODisplay *dpy = vdev->dpy;
274     VFIODMABuf *dmabuf, *tmp;
275     uint32_t keep = 5;
276 
277     QTAILQ_FOREACH_SAFE(dmabuf, &dpy->dmabuf.bufs, next, tmp) {
278         if (keep > 0) {
279             keep--;
280             continue;
281         }
282         assert(dmabuf != dpy->dmabuf.primary);
283         vfio_display_free_one_dmabuf(dpy, dmabuf);
284     }
285 }
286 
287 static void vfio_display_dmabuf_update(void *opaque)
288 {
289     VFIOPCIDevice *vdev = opaque;
290     VFIODisplay *dpy = vdev->dpy;
291     VFIODMABuf *primary, *cursor;
292     uint32_t width, height;
293     bool free_bufs = false, new_cursor = false;
294 
295     primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY);
296     if (primary == NULL) {
297         if (dpy->ramfb) {
298             ramfb_display_update(dpy->con, dpy->ramfb);
299         }
300         return;
301     }
302 
303     width = qemu_dmabuf_get_width(primary->buf);
304     height = qemu_dmabuf_get_height(primary->buf);
305 
306     if (dpy->dmabuf.primary != primary) {
307         dpy->dmabuf.primary = primary;
308         qemu_console_resize(dpy->con, width, height);
309         dpy_gl_scanout_dmabuf(dpy->con, primary->buf);
310         free_bufs = true;
311     }
312 
313     cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR);
314     if (dpy->dmabuf.cursor != cursor) {
315         dpy->dmabuf.cursor = cursor;
316         new_cursor = true;
317         free_bufs = true;
318     }
319 
320     if (cursor && (new_cursor || cursor->hot_updates)) {
321         bool have_hot = (cursor->hot_x != 0xffffffff &&
322                          cursor->hot_y != 0xffffffff);
323         dpy_gl_cursor_dmabuf(dpy->con, cursor->buf, have_hot,
324                              cursor->hot_x, cursor->hot_y);
325         cursor->hot_updates = 0;
326     } else if (!cursor && new_cursor) {
327         dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0);
328     }
329 
330     if (cursor && cursor->pos_updates) {
331         dpy_gl_cursor_position(dpy->con,
332                                cursor->pos_x,
333                                cursor->pos_y);
334         cursor->pos_updates = 0;
335     }
336 
337     dpy_gl_update(dpy->con, 0, 0, width, height);
338 
339     if (free_bufs) {
340         vfio_display_free_dmabufs(vdev);
341     }
342 }
343 
344 static int vfio_display_get_flags(void *opaque)
345 {
346     return GRAPHIC_FLAGS_GL | GRAPHIC_FLAGS_DMABUF;
347 }
348 
349 static const GraphicHwOps vfio_display_dmabuf_ops = {
350     .get_flags  = vfio_display_get_flags,
351     .gfx_update = vfio_display_dmabuf_update,
352     .ui_info    = vfio_display_edid_ui_info,
353 };
354 
355 static bool vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp)
356 {
357     if (!display_opengl) {
358         error_setg(errp, "vfio-display-dmabuf: opengl not available");
359         return false;
360     }
361 
362     vdev->dpy = g_new0(VFIODisplay, 1);
363     vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
364                                           &vfio_display_dmabuf_ops,
365                                           vdev);
366     if (vdev->enable_ramfb) {
367         vdev->dpy->ramfb = ramfb_setup(errp);
368         if (!vdev->dpy->ramfb) {
369             return false;
370         }
371     }
372     return vfio_display_edid_init(vdev, errp);
373 }
374 
375 static void vfio_display_dmabuf_exit(VFIODisplay *dpy)
376 {
377     VFIODMABuf *dmabuf;
378 
379     if (QTAILQ_EMPTY(&dpy->dmabuf.bufs)) {
380         return;
381     }
382 
383     while ((dmabuf = QTAILQ_FIRST(&dpy->dmabuf.bufs)) != NULL) {
384         vfio_display_free_one_dmabuf(dpy, dmabuf);
385     }
386 }
387 
388 /* ---------------------------------------------------------------------- */
389 void vfio_display_reset(VFIOPCIDevice *vdev)
390 {
391     if (!vdev || !vdev->dpy || !vdev->dpy->con ||
392         !vdev->dpy->dmabuf.primary) {
393         return;
394     }
395 
396     dpy_gl_scanout_disable(vdev->dpy->con);
397     vfio_display_dmabuf_exit(vdev->dpy);
398     dpy_gfx_update_full(vdev->dpy->con);
399 }
400 
401 static void vfio_display_region_update(void *opaque)
402 {
403     VFIOPCIDevice *vdev = opaque;
404     VFIODisplay *dpy = vdev->dpy;
405     struct vfio_device_gfx_plane_info plane = {
406         .argsz = sizeof(plane),
407         .flags = VFIO_GFX_PLANE_TYPE_REGION
408     };
409     pixman_format_code_t format;
410     int ret;
411 
412     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
413     if (ret < 0) {
414         error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s",
415                      strerror(errno));
416         return;
417     }
418     if (!plane.drm_format || !plane.size) {
419         if (dpy->ramfb) {
420             ramfb_display_update(dpy->con, dpy->ramfb);
421             dpy->region.surface = NULL;
422         }
423         return;
424     }
425     format = qemu_drm_format_to_pixman(plane.drm_format);
426     if (!format) {
427         return;
428     }
429 
430     if (dpy->region.buffer.size &&
431         dpy->region.buffer.nr != plane.region_index) {
432         /* region changed */
433         vfio_region_exit(&dpy->region.buffer);
434         vfio_region_finalize(&dpy->region.buffer);
435         dpy->region.surface = NULL;
436     }
437 
438     if (dpy->region.surface &&
439         (surface_width(dpy->region.surface) != plane.width ||
440          surface_height(dpy->region.surface) != plane.height ||
441          surface_format(dpy->region.surface) != format)) {
442         /* size changed */
443         dpy->region.surface = NULL;
444     }
445 
446     if (!dpy->region.buffer.size) {
447         /* mmap region */
448         ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev,
449                                 &dpy->region.buffer,
450                                 plane.region_index,
451                                 "display");
452         if (ret != 0) {
453             error_report("%s: vfio_region_setup(%d): %s",
454                          __func__, plane.region_index, strerror(-ret));
455             goto err;
456         }
457         ret = vfio_region_mmap(&dpy->region.buffer);
458         if (ret != 0) {
459             error_report("%s: vfio_region_mmap(%d): %s", __func__,
460                          plane.region_index, strerror(-ret));
461             goto err;
462         }
463         assert(dpy->region.buffer.mmaps[0].mmap != NULL);
464     }
465 
466     if (dpy->region.surface == NULL) {
467         /* create surface */
468         dpy->region.surface = qemu_create_displaysurface_from
469             (plane.width, plane.height, format,
470              plane.stride, dpy->region.buffer.mmaps[0].mmap);
471         dpy_gfx_replace_surface(dpy->con, dpy->region.surface);
472     }
473 
474     /* full screen update */
475     dpy_gfx_update(dpy->con, 0, 0,
476                    surface_width(dpy->region.surface),
477                    surface_height(dpy->region.surface));
478     return;
479 
480 err:
481     vfio_region_exit(&dpy->region.buffer);
482     vfio_region_finalize(&dpy->region.buffer);
483 }
484 
485 static const GraphicHwOps vfio_display_region_ops = {
486     .gfx_update = vfio_display_region_update,
487 };
488 
489 static bool vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp)
490 {
491     vdev->dpy = g_new0(VFIODisplay, 1);
492     vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
493                                           &vfio_display_region_ops,
494                                           vdev);
495     if (vdev->enable_ramfb) {
496         vdev->dpy->ramfb = ramfb_setup(errp);
497         if (!vdev->dpy->ramfb) {
498             return false;
499         }
500     }
501     return true;
502 }
503 
504 static void vfio_display_region_exit(VFIODisplay *dpy)
505 {
506     if (!dpy->region.buffer.size) {
507         return;
508     }
509 
510     vfio_region_exit(&dpy->region.buffer);
511     vfio_region_finalize(&dpy->region.buffer);
512 }
513 
514 /* ---------------------------------------------------------------------- */
515 
516 bool vfio_display_probe(VFIOPCIDevice *vdev, Error **errp)
517 {
518     struct vfio_device_gfx_plane_info probe;
519     int ret;
520 
521     memset(&probe, 0, sizeof(probe));
522     probe.argsz = sizeof(probe);
523     probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF;
524     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
525     if (ret == 0) {
526         return vfio_display_dmabuf_init(vdev, errp);
527     }
528 
529     memset(&probe, 0, sizeof(probe));
530     probe.argsz = sizeof(probe);
531     probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION;
532     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
533     if (ret == 0) {
534         return vfio_display_region_init(vdev, errp);
535     }
536 
537     if (vdev->display == ON_OFF_AUTO_AUTO) {
538         /* not an error in automatic mode */
539         return true;
540     }
541 
542     error_setg(errp, "vfio: device doesn't support any (known) display method");
543     return false;
544 }
545 
546 void vfio_display_finalize(VFIOPCIDevice *vdev)
547 {
548     if (!vdev->dpy) {
549         return;
550     }
551 
552     graphic_console_close(vdev->dpy->con);
553     vfio_display_dmabuf_exit(vdev->dpy);
554     vfio_display_region_exit(vdev->dpy);
555     vfio_display_edid_exit(vdev->dpy);
556     g_free(vdev->dpy);
557 }
558 
559 static bool migrate_needed(void *opaque)
560 {
561     VFIODisplay *dpy = opaque;
562     bool ramfb_exists = dpy->ramfb != NULL;
563 
564     /* see vfio_display_migration_needed() */
565     assert(ramfb_exists);
566     return ramfb_exists;
567 }
568 
569 const VMStateDescription vfio_display_vmstate = {
570     .name = "VFIODisplay",
571     .version_id = 1,
572     .minimum_version_id = 1,
573     .needed = migrate_needed,
574     .fields = (const VMStateField[]) {
575         VMSTATE_STRUCT_POINTER(ramfb, VFIODisplay, ramfb_vmstate, RAMFBState),
576         VMSTATE_END_OF_LIST(),
577     }
578 };
579