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 return; 108 } 109 110 static void vfio_display_edid_ui_info(void *opaque, uint32_t idx, 111 QemuUIInfo *info) 112 { 113 VFIOPCIDevice *vdev = opaque; 114 VFIODisplay *dpy = vdev->dpy; 115 116 if (!dpy->edid_regs) { 117 return; 118 } 119 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); 124 } 125 } 126 127 static void vfio_display_edid_init(VFIOPCIDevice *vdev) 128 { 129 VFIODisplay *dpy = vdev->dpy; 130 int fd = vdev->vbasedev.fd; 131 int ret; 132 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; 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; 171 172 err: 173 trace_vfio_display_edid_write_error(); 174 g_free(dpy->edid_regs); 175 dpy->edid_regs = NULL; 176 return; 177 } 178 179 static void vfio_display_edid_exit(VFIODisplay *dpy) 180 { 181 if (!dpy->edid_regs) { 182 return; 183 } 184 185 g_free(dpy->edid_regs); 186 g_free(dpy->edid_blob); 187 timer_free(dpy->edid_link_timer); 188 } 189 190 static void vfio_display_update_cursor(VFIODMABuf *dmabuf, 191 struct vfio_device_gfx_plane_info *plane) 192 { 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++; 197 } 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++; 202 } 203 } 204 205 static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev, 206 uint32_t plane_type) 207 { 208 VFIODisplay *dpy = vdev->dpy; 209 struct vfio_device_gfx_plane_info plane; 210 VFIODMABuf *dmabuf; 211 int fd, ret; 212 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; 220 } 221 if (!plane.drm_format || !plane.size) { 222 return NULL; 223 } 224 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); 232 } 233 return dmabuf; 234 } 235 } 236 237 fd = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &plane.dmabuf_id); 238 if (fd < 0) { 239 return NULL; 240 } 241 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.backing_width = plane.width; 247 dmabuf->buf.backing_height = plane.height; 248 dmabuf->buf.stride = plane.stride; 249 dmabuf->buf.fourcc = plane.drm_format; 250 dmabuf->buf.modifier = plane.drm_format_mod; 251 dmabuf->buf.fd = fd; 252 if (plane_type == DRM_PLANE_TYPE_CURSOR) { 253 vfio_display_update_cursor(dmabuf, &plane); 254 } 255 256 QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next); 257 return dmabuf; 258 } 259 260 static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf) 261 { 262 QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next); 263 264 qemu_dmabuf_close(&dmabuf->buf); 265 dpy_gl_release_dmabuf(dpy->con, &dmabuf->buf); 266 g_free(dmabuf); 267 } 268 269 static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev) 270 { 271 VFIODisplay *dpy = vdev->dpy; 272 VFIODMABuf *dmabuf, *tmp; 273 uint32_t keep = 5; 274 275 QTAILQ_FOREACH_SAFE(dmabuf, &dpy->dmabuf.bufs, next, tmp) { 276 if (keep > 0) { 277 keep--; 278 continue; 279 } 280 assert(dmabuf != dpy->dmabuf.primary); 281 vfio_display_free_one_dmabuf(dpy, dmabuf); 282 } 283 } 284 285 static void vfio_display_dmabuf_update(void *opaque) 286 { 287 VFIOPCIDevice *vdev = opaque; 288 VFIODisplay *dpy = vdev->dpy; 289 VFIODMABuf *primary, *cursor; 290 uint32_t width, height; 291 bool free_bufs = false, new_cursor = false; 292 293 primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY); 294 if (primary == NULL) { 295 if (dpy->ramfb) { 296 ramfb_display_update(dpy->con, dpy->ramfb); 297 } 298 return; 299 } 300 301 width = qemu_dmabuf_get_width(&primary->buf); 302 height = qemu_dmabuf_get_height(&primary->buf); 303 304 if (dpy->dmabuf.primary != primary) { 305 dpy->dmabuf.primary = primary; 306 qemu_console_resize(dpy->con, width, height); 307 dpy_gl_scanout_dmabuf(dpy->con, &primary->buf); 308 free_bufs = true; 309 } 310 311 cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR); 312 if (dpy->dmabuf.cursor != cursor) { 313 dpy->dmabuf.cursor = cursor; 314 new_cursor = true; 315 free_bufs = true; 316 } 317 318 if (cursor && (new_cursor || cursor->hot_updates)) { 319 bool have_hot = (cursor->hot_x != 0xffffffff && 320 cursor->hot_y != 0xffffffff); 321 dpy_gl_cursor_dmabuf(dpy->con, &cursor->buf, have_hot, 322 cursor->hot_x, cursor->hot_y); 323 cursor->hot_updates = 0; 324 } else if (!cursor && new_cursor) { 325 dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0); 326 } 327 328 if (cursor && cursor->pos_updates) { 329 dpy_gl_cursor_position(dpy->con, 330 cursor->pos_x, 331 cursor->pos_y); 332 cursor->pos_updates = 0; 333 } 334 335 dpy_gl_update(dpy->con, 0, 0, width, height); 336 337 if (free_bufs) { 338 vfio_display_free_dmabufs(vdev); 339 } 340 } 341 342 static int vfio_display_get_flags(void *opaque) 343 { 344 return GRAPHIC_FLAGS_GL | GRAPHIC_FLAGS_DMABUF; 345 } 346 347 static const GraphicHwOps vfio_display_dmabuf_ops = { 348 .get_flags = vfio_display_get_flags, 349 .gfx_update = vfio_display_dmabuf_update, 350 .ui_info = vfio_display_edid_ui_info, 351 }; 352 353 static int vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp) 354 { 355 if (!display_opengl) { 356 error_setg(errp, "vfio-display-dmabuf: opengl not available"); 357 return -1; 358 } 359 360 vdev->dpy = g_new0(VFIODisplay, 1); 361 vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0, 362 &vfio_display_dmabuf_ops, 363 vdev); 364 if (vdev->enable_ramfb) { 365 vdev->dpy->ramfb = ramfb_setup(errp); 366 } 367 vfio_display_edid_init(vdev); 368 return 0; 369 } 370 371 static void vfio_display_dmabuf_exit(VFIODisplay *dpy) 372 { 373 VFIODMABuf *dmabuf; 374 375 if (QTAILQ_EMPTY(&dpy->dmabuf.bufs)) { 376 return; 377 } 378 379 while ((dmabuf = QTAILQ_FIRST(&dpy->dmabuf.bufs)) != NULL) { 380 vfio_display_free_one_dmabuf(dpy, dmabuf); 381 } 382 } 383 384 /* ---------------------------------------------------------------------- */ 385 void vfio_display_reset(VFIOPCIDevice *vdev) 386 { 387 if (!vdev || !vdev->dpy || !vdev->dpy->con || 388 !vdev->dpy->dmabuf.primary) { 389 return; 390 } 391 392 dpy_gl_scanout_disable(vdev->dpy->con); 393 vfio_display_dmabuf_exit(vdev->dpy); 394 dpy_gfx_update_full(vdev->dpy->con); 395 } 396 397 static void vfio_display_region_update(void *opaque) 398 { 399 VFIOPCIDevice *vdev = opaque; 400 VFIODisplay *dpy = vdev->dpy; 401 struct vfio_device_gfx_plane_info plane = { 402 .argsz = sizeof(plane), 403 .flags = VFIO_GFX_PLANE_TYPE_REGION 404 }; 405 pixman_format_code_t format; 406 int ret; 407 408 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane); 409 if (ret < 0) { 410 error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s", 411 strerror(errno)); 412 return; 413 } 414 if (!plane.drm_format || !plane.size) { 415 if (dpy->ramfb) { 416 ramfb_display_update(dpy->con, dpy->ramfb); 417 dpy->region.surface = NULL; 418 } 419 return; 420 } 421 format = qemu_drm_format_to_pixman(plane.drm_format); 422 if (!format) { 423 return; 424 } 425 426 if (dpy->region.buffer.size && 427 dpy->region.buffer.nr != plane.region_index) { 428 /* region changed */ 429 vfio_region_exit(&dpy->region.buffer); 430 vfio_region_finalize(&dpy->region.buffer); 431 dpy->region.surface = NULL; 432 } 433 434 if (dpy->region.surface && 435 (surface_width(dpy->region.surface) != plane.width || 436 surface_height(dpy->region.surface) != plane.height || 437 surface_format(dpy->region.surface) != format)) { 438 /* size changed */ 439 dpy->region.surface = NULL; 440 } 441 442 if (!dpy->region.buffer.size) { 443 /* mmap region */ 444 ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev, 445 &dpy->region.buffer, 446 plane.region_index, 447 "display"); 448 if (ret != 0) { 449 error_report("%s: vfio_region_setup(%d): %s", 450 __func__, plane.region_index, strerror(-ret)); 451 goto err; 452 } 453 ret = vfio_region_mmap(&dpy->region.buffer); 454 if (ret != 0) { 455 error_report("%s: vfio_region_mmap(%d): %s", __func__, 456 plane.region_index, strerror(-ret)); 457 goto err; 458 } 459 assert(dpy->region.buffer.mmaps[0].mmap != NULL); 460 } 461 462 if (dpy->region.surface == NULL) { 463 /* create surface */ 464 dpy->region.surface = qemu_create_displaysurface_from 465 (plane.width, plane.height, format, 466 plane.stride, dpy->region.buffer.mmaps[0].mmap); 467 dpy_gfx_replace_surface(dpy->con, dpy->region.surface); 468 } 469 470 /* full screen update */ 471 dpy_gfx_update(dpy->con, 0, 0, 472 surface_width(dpy->region.surface), 473 surface_height(dpy->region.surface)); 474 return; 475 476 err: 477 vfio_region_exit(&dpy->region.buffer); 478 vfio_region_finalize(&dpy->region.buffer); 479 } 480 481 static const GraphicHwOps vfio_display_region_ops = { 482 .gfx_update = vfio_display_region_update, 483 }; 484 485 static int vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp) 486 { 487 vdev->dpy = g_new0(VFIODisplay, 1); 488 vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0, 489 &vfio_display_region_ops, 490 vdev); 491 if (vdev->enable_ramfb) { 492 vdev->dpy->ramfb = ramfb_setup(errp); 493 } 494 return 0; 495 } 496 497 static void vfio_display_region_exit(VFIODisplay *dpy) 498 { 499 if (!dpy->region.buffer.size) { 500 return; 501 } 502 503 vfio_region_exit(&dpy->region.buffer); 504 vfio_region_finalize(&dpy->region.buffer); 505 } 506 507 /* ---------------------------------------------------------------------- */ 508 509 int vfio_display_probe(VFIOPCIDevice *vdev, Error **errp) 510 { 511 struct vfio_device_gfx_plane_info probe; 512 int ret; 513 514 memset(&probe, 0, sizeof(probe)); 515 probe.argsz = sizeof(probe); 516 probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF; 517 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe); 518 if (ret == 0) { 519 return vfio_display_dmabuf_init(vdev, errp); 520 } 521 522 memset(&probe, 0, sizeof(probe)); 523 probe.argsz = sizeof(probe); 524 probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION; 525 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe); 526 if (ret == 0) { 527 return vfio_display_region_init(vdev, errp); 528 } 529 530 if (vdev->display == ON_OFF_AUTO_AUTO) { 531 /* not an error in automatic mode */ 532 return 0; 533 } 534 535 error_setg(errp, "vfio: device doesn't support any (known) display method"); 536 return -1; 537 } 538 539 void vfio_display_finalize(VFIOPCIDevice *vdev) 540 { 541 if (!vdev->dpy) { 542 return; 543 } 544 545 graphic_console_close(vdev->dpy->con); 546 vfio_display_dmabuf_exit(vdev->dpy); 547 vfio_display_region_exit(vdev->dpy); 548 vfio_display_edid_exit(vdev->dpy); 549 g_free(vdev->dpy); 550 } 551 552 static bool migrate_needed(void *opaque) 553 { 554 VFIODisplay *dpy = opaque; 555 bool ramfb_exists = dpy->ramfb != NULL; 556 557 /* see vfio_display_migration_needed() */ 558 assert(ramfb_exists); 559 return ramfb_exists; 560 } 561 562 const VMStateDescription vfio_display_vmstate = { 563 .name = "VFIODisplay", 564 .version_id = 1, 565 .minimum_version_id = 1, 566 .needed = migrate_needed, 567 .fields = (const VMStateField[]) { 568 VMSTATE_STRUCT_POINTER(ramfb, VFIODisplay, ramfb_vmstate, RAMFBState), 569 VMSTATE_END_OF_LIST(), 570 } 571 }; 572