qapi: Improve specificity of type/member descriptions
[qemu/armbru.git] / hw / vfio / display.c
blobbec864f482f46866bf292a5a63c31753a7c84eef
1 /*
2 * display support for mdev based vgpu devices
4 * Copyright Red Hat, Inc. 2017
6 * Authors:
7 * Gerd Hoffmann
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include <linux/vfio.h>
15 #include <sys/ioctl.h>
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"
24 #ifndef DRM_PLANE_TYPE_PRIMARY
25 # define DRM_PLANE_TYPE_PRIMARY 1
26 # define DRM_PLANE_TYPE_CURSOR 2
27 #endif
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)))
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)))
40 static void vfio_display_edid_link_up(void *opaque)
42 VFIOPCIDevice *vdev = opaque;
43 VFIODisplay *dpy = vdev->dpy;
44 int fd = vdev->vbasedev.fd;
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;
50 trace_vfio_display_edid_link_up();
51 return;
53 err:
54 trace_vfio_display_edid_write_error();
57 static void vfio_display_edid_update(VFIOPCIDevice *vdev, bool enabled,
58 int prefx, int prefy)
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,
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;
74 trace_vfio_display_edid_link_down();
76 if (!enabled) {
77 return;
80 if (edid.maxx && edid.prefx > edid.maxx) {
81 edid.prefx = edid.maxx;
83 if (edid.maxy && edid.prefy > edid.maxy) {
84 edid.prefy = edid.maxy;
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);
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;
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;
101 timer_mod(dpy->edid_link_timer,
102 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 100);
103 return;
105 err:
106 trace_vfio_display_edid_write_error();
107 return;
110 static void vfio_display_edid_ui_info(void *opaque, uint32_t idx,
111 QemuUIInfo *info)
113 VFIOPCIDevice *vdev = opaque;
114 VFIODisplay *dpy = vdev->dpy;
116 if (!dpy->edid_regs) {
117 return;
120 if (info->width && info->height) {
121 vfio_display_edid_update(vdev, true, info->width, info->height);
122 } else {
123 vfio_display_edid_update(vdev, false, 0, 0);
127 static void vfio_display_edid_init(VFIOPCIDevice *vdev)
129 VFIODisplay *dpy = vdev->dpy;
130 int fd = vdev->vbasedev.fd;
131 int ret;
133 ret = vfio_get_dev_region_info(&vdev->vbasedev,
134 VFIO_REGION_TYPE_GFX,
135 VFIO_REGION_SUBTYPE_GFX_EDID,
136 &dpy->edid_info);
137 if (ret) {
138 return;
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;
146 if (pread_field(fd, dpy->edid_info, dpy->edid_regs, edid_max_size)) {
147 goto err;
149 if (pread_field(fd, dpy->edid_info, dpy->edid_regs, max_xres)) {
150 goto err;
152 if (pread_field(fd, dpy->edid_info, dpy->edid_regs, max_yres)) {
153 goto err;
156 dpy->edid_blob = g_malloc0(dpy->edid_regs->edid_max_size);
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;
162 if (!vdev->display_yres) {
163 vdev->display_yres = dpy->edid_regs->max_yres;
166 dpy->edid_link_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
167 vfio_display_edid_link_up, vdev);
169 vfio_display_edid_update(vdev, true, 0, 0);
170 return;
172 err:
173 trace_vfio_display_edid_write_error();
174 g_free(dpy->edid_regs);
175 dpy->edid_regs = NULL;
176 return;
179 static void vfio_display_edid_exit(VFIODisplay *dpy)
181 if (!dpy->edid_regs) {
182 return;
185 g_free(dpy->edid_regs);
186 g_free(dpy->edid_blob);
187 timer_free(dpy->edid_link_timer);
190 static void vfio_display_update_cursor(VFIODMABuf *dmabuf,
191 struct vfio_device_gfx_plane_info *plane)
193 if (dmabuf->pos_x != plane->x_pos || dmabuf->pos_y != plane->y_pos) {
194 dmabuf->pos_x = plane->x_pos;
195 dmabuf->pos_y = plane->y_pos;
196 dmabuf->pos_updates++;
198 if (dmabuf->hot_x != plane->x_hot || dmabuf->hot_y != plane->y_hot) {
199 dmabuf->hot_x = plane->x_hot;
200 dmabuf->hot_y = plane->y_hot;
201 dmabuf->hot_updates++;
205 static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
206 uint32_t plane_type)
208 VFIODisplay *dpy = vdev->dpy;
209 struct vfio_device_gfx_plane_info plane;
210 VFIODMABuf *dmabuf;
211 int fd, ret;
213 memset(&plane, 0, sizeof(plane));
214 plane.argsz = sizeof(plane);
215 plane.flags = VFIO_GFX_PLANE_TYPE_DMABUF;
216 plane.drm_plane_type = plane_type;
217 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
218 if (ret < 0) {
219 return NULL;
221 if (!plane.drm_format || !plane.size) {
222 return NULL;
225 QTAILQ_FOREACH(dmabuf, &dpy->dmabuf.bufs, next) {
226 if (dmabuf->dmabuf_id == plane.dmabuf_id) {
227 /* found in list, move to head, return it */
228 QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
229 QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
230 if (plane_type == DRM_PLANE_TYPE_CURSOR) {
231 vfio_display_update_cursor(dmabuf, &plane);
233 return dmabuf;
237 fd = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &plane.dmabuf_id);
238 if (fd < 0) {
239 return NULL;
242 dmabuf = g_new0(VFIODMABuf, 1);
243 dmabuf->dmabuf_id = plane.dmabuf_id;
244 dmabuf->buf.width = plane.width;
245 dmabuf->buf.height = plane.height;
246 dmabuf->buf.stride = plane.stride;
247 dmabuf->buf.fourcc = plane.drm_format;
248 dmabuf->buf.modifier = plane.drm_format_mod;
249 dmabuf->buf.fd = fd;
250 if (plane_type == DRM_PLANE_TYPE_CURSOR) {
251 vfio_display_update_cursor(dmabuf, &plane);
254 QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
255 return dmabuf;
258 static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)
260 QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
261 dpy_gl_release_dmabuf(dpy->con, &dmabuf->buf);
262 close(dmabuf->buf.fd);
263 g_free(dmabuf);
266 static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev)
268 VFIODisplay *dpy = vdev->dpy;
269 VFIODMABuf *dmabuf, *tmp;
270 uint32_t keep = 5;
272 QTAILQ_FOREACH_SAFE(dmabuf, &dpy->dmabuf.bufs, next, tmp) {
273 if (keep > 0) {
274 keep--;
275 continue;
277 assert(dmabuf != dpy->dmabuf.primary);
278 vfio_display_free_one_dmabuf(dpy, dmabuf);
282 static void vfio_display_dmabuf_update(void *opaque)
284 VFIOPCIDevice *vdev = opaque;
285 VFIODisplay *dpy = vdev->dpy;
286 VFIODMABuf *primary, *cursor;
287 bool free_bufs = false, new_cursor = false;
289 primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY);
290 if (primary == NULL) {
291 if (dpy->ramfb) {
292 ramfb_display_update(dpy->con, dpy->ramfb);
294 return;
297 if (dpy->dmabuf.primary != primary) {
298 dpy->dmabuf.primary = primary;
299 qemu_console_resize(dpy->con,
300 primary->buf.width, primary->buf.height);
301 dpy_gl_scanout_dmabuf(dpy->con, &primary->buf);
302 free_bufs = true;
305 cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR);
306 if (dpy->dmabuf.cursor != cursor) {
307 dpy->dmabuf.cursor = cursor;
308 new_cursor = true;
309 free_bufs = true;
312 if (cursor && (new_cursor || cursor->hot_updates)) {
313 bool have_hot = (cursor->hot_x != 0xffffffff &&
314 cursor->hot_y != 0xffffffff);
315 dpy_gl_cursor_dmabuf(dpy->con, &cursor->buf, have_hot,
316 cursor->hot_x, cursor->hot_y);
317 cursor->hot_updates = 0;
318 } else if (!cursor && new_cursor) {
319 dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0);
322 if (cursor && cursor->pos_updates) {
323 dpy_gl_cursor_position(dpy->con,
324 cursor->pos_x,
325 cursor->pos_y);
326 cursor->pos_updates = 0;
329 dpy_gl_update(dpy->con, 0, 0, primary->buf.width, primary->buf.height);
331 if (free_bufs) {
332 vfio_display_free_dmabufs(vdev);
336 static int vfio_display_get_flags(void *opaque)
338 return GRAPHIC_FLAGS_GL | GRAPHIC_FLAGS_DMABUF;
341 static const GraphicHwOps vfio_display_dmabuf_ops = {
342 .get_flags = vfio_display_get_flags,
343 .gfx_update = vfio_display_dmabuf_update,
344 .ui_info = vfio_display_edid_ui_info,
347 static int vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp)
349 if (!display_opengl) {
350 error_setg(errp, "vfio-display-dmabuf: opengl not available");
351 return -1;
354 vdev->dpy = g_new0(VFIODisplay, 1);
355 vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
356 &vfio_display_dmabuf_ops,
357 vdev);
358 if (vdev->enable_ramfb) {
359 vdev->dpy->ramfb = ramfb_setup(errp);
361 vfio_display_edid_init(vdev);
362 return 0;
365 static void vfio_display_dmabuf_exit(VFIODisplay *dpy)
367 VFIODMABuf *dmabuf;
369 if (QTAILQ_EMPTY(&dpy->dmabuf.bufs)) {
370 return;
373 while ((dmabuf = QTAILQ_FIRST(&dpy->dmabuf.bufs)) != NULL) {
374 vfio_display_free_one_dmabuf(dpy, dmabuf);
378 /* ---------------------------------------------------------------------- */
379 void vfio_display_reset(VFIOPCIDevice *vdev)
381 if (!vdev || !vdev->dpy || !vdev->dpy->con ||
382 !vdev->dpy->dmabuf.primary) {
383 return;
386 dpy_gl_scanout_disable(vdev->dpy->con);
387 vfio_display_dmabuf_exit(vdev->dpy);
388 dpy_gfx_update_full(vdev->dpy->con);
391 static void vfio_display_region_update(void *opaque)
393 VFIOPCIDevice *vdev = opaque;
394 VFIODisplay *dpy = vdev->dpy;
395 struct vfio_device_gfx_plane_info plane = {
396 .argsz = sizeof(plane),
397 .flags = VFIO_GFX_PLANE_TYPE_REGION
399 pixman_format_code_t format;
400 int ret;
402 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
403 if (ret < 0) {
404 error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s",
405 strerror(errno));
406 return;
408 if (!plane.drm_format || !plane.size) {
409 if (dpy->ramfb) {
410 ramfb_display_update(dpy->con, dpy->ramfb);
411 dpy->region.surface = NULL;
413 return;
415 format = qemu_drm_format_to_pixman(plane.drm_format);
416 if (!format) {
417 return;
420 if (dpy->region.buffer.size &&
421 dpy->region.buffer.nr != plane.region_index) {
422 /* region changed */
423 vfio_region_exit(&dpy->region.buffer);
424 vfio_region_finalize(&dpy->region.buffer);
425 dpy->region.surface = NULL;
428 if (dpy->region.surface &&
429 (surface_width(dpy->region.surface) != plane.width ||
430 surface_height(dpy->region.surface) != plane.height ||
431 surface_format(dpy->region.surface) != format)) {
432 /* size changed */
433 dpy->region.surface = NULL;
436 if (!dpy->region.buffer.size) {
437 /* mmap region */
438 ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev,
439 &dpy->region.buffer,
440 plane.region_index,
441 "display");
442 if (ret != 0) {
443 error_report("%s: vfio_region_setup(%d): %s",
444 __func__, plane.region_index, strerror(-ret));
445 goto err;
447 ret = vfio_region_mmap(&dpy->region.buffer);
448 if (ret != 0) {
449 error_report("%s: vfio_region_mmap(%d): %s", __func__,
450 plane.region_index, strerror(-ret));
451 goto err;
453 assert(dpy->region.buffer.mmaps[0].mmap != NULL);
456 if (dpy->region.surface == NULL) {
457 /* create surface */
458 dpy->region.surface = qemu_create_displaysurface_from
459 (plane.width, plane.height, format,
460 plane.stride, dpy->region.buffer.mmaps[0].mmap);
461 dpy_gfx_replace_surface(dpy->con, dpy->region.surface);
464 /* full screen update */
465 dpy_gfx_update(dpy->con, 0, 0,
466 surface_width(dpy->region.surface),
467 surface_height(dpy->region.surface));
468 return;
470 err:
471 vfio_region_exit(&dpy->region.buffer);
472 vfio_region_finalize(&dpy->region.buffer);
475 static const GraphicHwOps vfio_display_region_ops = {
476 .gfx_update = vfio_display_region_update,
479 static int vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp)
481 vdev->dpy = g_new0(VFIODisplay, 1);
482 vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
483 &vfio_display_region_ops,
484 vdev);
485 if (vdev->enable_ramfb) {
486 vdev->dpy->ramfb = ramfb_setup(errp);
488 return 0;
491 static void vfio_display_region_exit(VFIODisplay *dpy)
493 if (!dpy->region.buffer.size) {
494 return;
497 vfio_region_exit(&dpy->region.buffer);
498 vfio_region_finalize(&dpy->region.buffer);
501 /* ---------------------------------------------------------------------- */
503 int vfio_display_probe(VFIOPCIDevice *vdev, Error **errp)
505 struct vfio_device_gfx_plane_info probe;
506 int ret;
508 memset(&probe, 0, sizeof(probe));
509 probe.argsz = sizeof(probe);
510 probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF;
511 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
512 if (ret == 0) {
513 return vfio_display_dmabuf_init(vdev, errp);
516 memset(&probe, 0, sizeof(probe));
517 probe.argsz = sizeof(probe);
518 probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION;
519 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
520 if (ret == 0) {
521 return vfio_display_region_init(vdev, errp);
524 if (vdev->display == ON_OFF_AUTO_AUTO) {
525 /* not an error in automatic mode */
526 return 0;
529 error_setg(errp, "vfio: device doesn't support any (known) display method");
530 return -1;
533 void vfio_display_finalize(VFIOPCIDevice *vdev)
535 if (!vdev->dpy) {
536 return;
539 graphic_console_close(vdev->dpy->con);
540 vfio_display_dmabuf_exit(vdev->dpy);
541 vfio_display_region_exit(vdev->dpy);
542 vfio_display_edid_exit(vdev->dpy);
543 g_free(vdev->dpy);