| 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 "qapi/error.h" |
| 20 | #include "pci.h" |
| 21 | #include "vfio-display.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_device_get_region_info_type(&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 | uint32_t offset = 0; |
| 217 | |
| 218 | memset(&plane, 0, sizeof(plane)); |
| 219 | plane.argsz = sizeof(plane); |
| 220 | plane.flags = VFIO_GFX_PLANE_TYPE_DMABUF; |
| 221 | plane.drm_plane_type = plane_type; |
| 222 | ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane); |
| 223 | if (ret < 0) { |
| 224 | return NULL; |
| 225 | } |
| 226 | if (!plane.drm_format || !plane.size) { |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | QTAILQ_FOREACH(dmabuf, &dpy->dmabuf.bufs, next) { |
| 231 | if (dmabuf->dmabuf_id == plane.dmabuf_id) { |
| 232 | /* found in list, move to head, return it */ |
| 233 | QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next); |
| 234 | QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next); |
| 235 | if (plane_type == DRM_PLANE_TYPE_CURSOR) { |
| 236 | vfio_display_update_cursor(dmabuf, &plane); |
| 237 | } |
| 238 | return dmabuf; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | fd = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &plane.dmabuf_id); |
| 243 | if (fd < 0) { |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | dmabuf = g_new0(VFIODMABuf, 1); |
| 248 | dmabuf->dmabuf_id = plane.dmabuf_id; |
| 249 | dmabuf->buf = qemu_dmabuf_new(plane.width, plane.height, &offset, |
| 250 | &plane.stride, 0, 0, plane.width, |
| 251 | plane.height, plane.drm_format, |
| 252 | plane.drm_format_mod, &fd, 1, false, false); |
| 253 | |
| 254 | if (plane_type == DRM_PLANE_TYPE_CURSOR) { |
| 255 | vfio_display_update_cursor(dmabuf, &plane); |
| 256 | } |
| 257 | |
| 258 | QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next); |
| 259 | return dmabuf; |
| 260 | } |
| 261 | |
| 262 | static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf) |
| 263 | { |
| 264 | QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next); |
| 265 | |
| 266 | qemu_dmabuf_close(dmabuf->buf); |
| 267 | qemu_console_gl_release_dmabuf(dpy->con, dmabuf->buf); |
| 268 | g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free); |
| 269 | g_free(dmabuf); |
| 270 | } |
| 271 | |
| 272 | static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev) |
| 273 | { |
| 274 | VFIODisplay *dpy = vdev->dpy; |
| 275 | VFIODMABuf *dmabuf, *tmp; |
| 276 | uint32_t keep = 5; |
| 277 | |
| 278 | QTAILQ_FOREACH_SAFE(dmabuf, &dpy->dmabuf.bufs, next, tmp) { |
| 279 | if (keep > 0) { |
| 280 | keep--; |
| 281 | continue; |
| 282 | } |
| 283 | assert(dmabuf != dpy->dmabuf.primary); |
| 284 | vfio_display_free_one_dmabuf(dpy, dmabuf); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static bool vfio_display_dmabuf_update(void *opaque) |
| 289 | { |
| 290 | VFIOPCIDevice *vdev = opaque; |
| 291 | VFIODisplay *dpy = vdev->dpy; |
| 292 | VFIODMABuf *primary, *cursor; |
| 293 | uint32_t width, height; |
| 294 | bool free_bufs = false, new_cursor = false; |
| 295 | |
| 296 | primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY); |
| 297 | if (primary == NULL) { |
| 298 | if (dpy->ramfb) { |
| 299 | ramfb_display_update(dpy->con, dpy->ramfb); |
| 300 | } |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | width = qemu_dmabuf_get_width(primary->buf); |
| 305 | height = qemu_dmabuf_get_height(primary->buf); |
| 306 | |
| 307 | if (dpy->dmabuf.primary != primary) { |
| 308 | dpy->dmabuf.primary = primary; |
| 309 | qemu_console_resize(dpy->con, width, height); |
| 310 | qemu_console_gl_scanout_dmabuf(dpy->con, primary->buf); |
| 311 | free_bufs = true; |
| 312 | } |
| 313 | |
| 314 | cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR); |
| 315 | if (dpy->dmabuf.cursor != cursor) { |
| 316 | dpy->dmabuf.cursor = cursor; |
| 317 | new_cursor = true; |
| 318 | free_bufs = true; |
| 319 | } |
| 320 | |
| 321 | if (cursor && (new_cursor || cursor->hot_updates)) { |
| 322 | bool have_hot = (cursor->hot_x != 0xffffffff && |
| 323 | cursor->hot_y != 0xffffffff); |
| 324 | qemu_console_gl_cursor_dmabuf(dpy->con, cursor->buf, have_hot, |
| 325 | cursor->hot_x, cursor->hot_y); |
| 326 | cursor->hot_updates = 0; |
| 327 | } else if (!cursor && new_cursor) { |
| 328 | qemu_console_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0); |
| 329 | } |
| 330 | |
| 331 | if (cursor && cursor->pos_updates) { |
| 332 | qemu_console_gl_cursor_position(dpy->con, |
| 333 | cursor->pos_x, |
| 334 | cursor->pos_y); |
| 335 | cursor->pos_updates = 0; |
| 336 | } |
| 337 | |
| 338 | qemu_console_gl_update(dpy->con, 0, 0, width, height); |
| 339 | |
| 340 | if (free_bufs) { |
| 341 | vfio_display_free_dmabufs(vdev); |
| 342 | } |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | static int vfio_display_get_flags(void *opaque) |
| 348 | { |
| 349 | return GRAPHIC_FLAGS_GL | GRAPHIC_FLAGS_DMABUF; |
| 350 | } |
| 351 | |
| 352 | static const GraphicHwOps vfio_display_dmabuf_ops = { |
| 353 | .get_flags = vfio_display_get_flags, |
| 354 | .gfx_update = vfio_display_dmabuf_update, |
| 355 | .ui_info = vfio_display_edid_ui_info, |
| 356 | }; |
| 357 | |
| 358 | static bool vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp) |
| 359 | { |
| 360 | if (!display_opengl) { |
| 361 | error_setg(errp, "vfio-display-dmabuf: opengl not available"); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | vdev->dpy = g_new0(VFIODisplay, 1); |
| 366 | vdev->dpy->con = qemu_graphic_console_create(DEVICE(vdev), 0, |
| 367 | &vfio_display_dmabuf_ops, |
| 368 | vdev); |
| 369 | if (vdev->enable_ramfb) { |
| 370 | vdev->dpy->ramfb = ramfb_setup(vdev->use_legacy_x86_rom, errp); |
| 371 | if (!vdev->dpy->ramfb) { |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | return vfio_display_edid_init(vdev, errp); |
| 376 | } |
| 377 | |
| 378 | static void vfio_display_dmabuf_exit(VFIODisplay *dpy) |
| 379 | { |
| 380 | VFIODMABuf *dmabuf; |
| 381 | |
| 382 | if (QTAILQ_EMPTY(&dpy->dmabuf.bufs)) { |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | while ((dmabuf = QTAILQ_FIRST(&dpy->dmabuf.bufs)) != NULL) { |
| 387 | vfio_display_free_one_dmabuf(dpy, dmabuf); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* ---------------------------------------------------------------------- */ |
| 392 | void vfio_display_reset(VFIOPCIDevice *vdev) |
| 393 | { |
| 394 | if (!vdev || !vdev->dpy || !vdev->dpy->con || |
| 395 | !vdev->dpy->dmabuf.primary) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | qemu_console_gl_scanout_disable(vdev->dpy->con); |
| 400 | vfio_display_dmabuf_exit(vdev->dpy); |
| 401 | qemu_console_update_full(vdev->dpy->con); |
| 402 | } |
| 403 | |
| 404 | static bool vfio_display_region_update(void *opaque) |
| 405 | { |
| 406 | VFIOPCIDevice *vdev = opaque; |
| 407 | VFIODisplay *dpy = vdev->dpy; |
| 408 | struct vfio_device_gfx_plane_info plane = { |
| 409 | .argsz = sizeof(plane), |
| 410 | .flags = VFIO_GFX_PLANE_TYPE_REGION |
| 411 | }; |
| 412 | pixman_format_code_t format; |
| 413 | int ret; |
| 414 | |
| 415 | ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane); |
| 416 | if (ret < 0) { |
| 417 | error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s", |
| 418 | strerror(errno)); |
| 419 | return true; |
| 420 | } |
| 421 | if (!plane.drm_format || !plane.size) { |
| 422 | if (dpy->ramfb) { |
| 423 | ramfb_display_update(dpy->con, dpy->ramfb); |
| 424 | dpy->region.surface = NULL; |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | format = qemu_drm_format_to_pixman(plane.drm_format); |
| 429 | if (!format) { |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | if (dpy->region.buffer.size && |
| 434 | dpy->region.buffer.nr != plane.region_index) { |
| 435 | /* region changed */ |
| 436 | vfio_region_exit(&dpy->region.buffer); |
| 437 | vfio_region_finalize(&dpy->region.buffer); |
| 438 | dpy->region.surface = NULL; |
| 439 | } |
| 440 | |
| 441 | if (dpy->region.surface && |
| 442 | (surface_width(dpy->region.surface) != plane.width || |
| 443 | surface_height(dpy->region.surface) != plane.height || |
| 444 | surface_format(dpy->region.surface) != format)) { |
| 445 | /* size changed */ |
| 446 | dpy->region.surface = NULL; |
| 447 | } |
| 448 | |
| 449 | if (!dpy->region.buffer.size) { |
| 450 | /* mmap region */ |
| 451 | Error *error = NULL; |
| 452 | ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev, |
| 453 | &dpy->region.buffer, |
| 454 | plane.region_index, |
| 455 | "display", &error); |
| 456 | if (ret != 0) { |
| 457 | error_report_err(error); |
| 458 | goto err; |
| 459 | } |
| 460 | ret = vfio_region_mmap(&dpy->region.buffer); |
| 461 | if (ret != 0) { |
| 462 | error_report("%s: vfio_region_mmap(%d): %s", __func__, |
| 463 | plane.region_index, strerror(-ret)); |
| 464 | goto err; |
| 465 | } |
| 466 | assert(dpy->region.buffer.mmaps[0].mmap != NULL); |
| 467 | } |
| 468 | |
| 469 | if (dpy->region.surface == NULL) { |
| 470 | /* create surface */ |
| 471 | dpy->region.surface = qemu_create_displaysurface_from |
| 472 | (plane.width, plane.height, format, |
| 473 | plane.stride, dpy->region.buffer.mmaps[0].mmap); |
| 474 | qemu_console_set_surface(dpy->con, dpy->region.surface); |
| 475 | } |
| 476 | |
| 477 | /* full screen update */ |
| 478 | qemu_console_update(dpy->con, 0, 0, |
| 479 | surface_width(dpy->region.surface), |
| 480 | surface_height(dpy->region.surface)); |
| 481 | return true; |
| 482 | |
| 483 | err: |
| 484 | vfio_region_exit(&dpy->region.buffer); |
| 485 | vfio_region_finalize(&dpy->region.buffer); |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | static const GraphicHwOps vfio_display_region_ops = { |
| 490 | .gfx_update = vfio_display_region_update, |
| 491 | }; |
| 492 | |
| 493 | static bool vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp) |
| 494 | { |
| 495 | vdev->dpy = g_new0(VFIODisplay, 1); |
| 496 | vdev->dpy->con = qemu_graphic_console_create(DEVICE(vdev), 0, |
| 497 | &vfio_display_region_ops, |
| 498 | vdev); |
| 499 | if (vdev->enable_ramfb) { |
| 500 | vdev->dpy->ramfb = ramfb_setup(vdev->use_legacy_x86_rom, errp); |
| 501 | if (!vdev->dpy->ramfb) { |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | /* ---------------------------------------------------------------------- */ |
| 510 | |
| 511 | bool vfio_display_probe(VFIOPCIDevice *vdev, Error **errp) |
| 512 | { |
| 513 | struct vfio_device_gfx_plane_info probe; |
| 514 | int ret; |
| 515 | |
| 516 | memset(&probe, 0, sizeof(probe)); |
| 517 | probe.argsz = sizeof(probe); |
| 518 | probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF; |
| 519 | ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe); |
| 520 | if (ret == 0) { |
| 521 | return vfio_display_dmabuf_init(vdev, errp); |
| 522 | } |
| 523 | |
| 524 | memset(&probe, 0, sizeof(probe)); |
| 525 | probe.argsz = sizeof(probe); |
| 526 | probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION; |
| 527 | ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe); |
| 528 | if (ret == 0) { |
| 529 | return vfio_display_region_init(vdev, errp); |
| 530 | } |
| 531 | |
| 532 | if (vdev->display == ON_OFF_AUTO_AUTO) { |
| 533 | /* not an error in automatic mode */ |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | error_setg(errp, "vfio: device doesn't support any (known) display method"); |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | void vfio_display_exit(VFIOPCIDevice *vdev) |
| 542 | { |
| 543 | if (!vdev->dpy) { |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | vfio_display_dmabuf_exit(vdev->dpy); |
| 548 | qemu_graphic_console_close(vdev->dpy->con); |
| 549 | if (vdev->dpy->region.buffer.size) { |
| 550 | vfio_region_exit(&vdev->dpy->region.buffer); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void vfio_display_finalize(VFIOPCIDevice *vdev) |
| 555 | { |
| 556 | if (!vdev->dpy) { |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | if (vdev->dpy->region.buffer.size) { |
| 561 | vfio_region_finalize(&vdev->dpy->region.buffer); |
| 562 | } |
| 563 | vfio_display_edid_exit(vdev->dpy); |
| 564 | g_free(vdev->dpy); |
| 565 | vdev->dpy = NULL; |
| 566 | } |
| 567 | |
| 568 | static bool migrate_needed(void *opaque) |
| 569 | { |
| 570 | VFIODisplay *dpy = opaque; |
| 571 | bool ramfb_exists = dpy->ramfb != NULL; |
| 572 | |
| 573 | /* see vfio_display_migration_needed() */ |
| 574 | assert(ramfb_exists); |
| 575 | return ramfb_exists; |
| 576 | } |
| 577 | |
| 578 | const VMStateDescription vfio_display_vmstate = { |
| 579 | .name = "VFIODisplay", |
| 580 | .version_id = 1, |
| 581 | .minimum_version_id = 1, |
| 582 | .needed = migrate_needed, |
| 583 | .fields = (const VMStateField[]) { |
| 584 | VMSTATE_STRUCT_POINTER(ramfb, VFIODisplay, ramfb_vmstate, RAMFBState), |
| 585 | VMSTATE_END_OF_LIST(), |
| 586 | } |
| 587 | }; |