1 /* 2 * vhost-user GPU Device 3 * 4 * Copyright Red Hat, Inc. 2018 5 * 6 * Authors: 7 * Marc-André Lureau <marcandre.lureau@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "hw/qdev-properties.h" 15 #include "hw/virtio/virtio-gpu.h" 16 #include "chardev/char-fe.h" 17 #include "qapi/error.h" 18 #include "migration/blocker.h" 19 20 typedef enum VhostUserGpuRequest { 21 VHOST_USER_GPU_NONE = 0, 22 VHOST_USER_GPU_GET_PROTOCOL_FEATURES, 23 VHOST_USER_GPU_SET_PROTOCOL_FEATURES, 24 VHOST_USER_GPU_GET_DISPLAY_INFO, 25 VHOST_USER_GPU_CURSOR_POS, 26 VHOST_USER_GPU_CURSOR_POS_HIDE, 27 VHOST_USER_GPU_CURSOR_UPDATE, 28 VHOST_USER_GPU_SCANOUT, 29 VHOST_USER_GPU_UPDATE, 30 VHOST_USER_GPU_DMABUF_SCANOUT, 31 VHOST_USER_GPU_DMABUF_UPDATE, 32 } VhostUserGpuRequest; 33 34 typedef struct VhostUserGpuDisplayInfoReply { 35 struct virtio_gpu_resp_display_info info; 36 } VhostUserGpuDisplayInfoReply; 37 38 typedef struct VhostUserGpuCursorPos { 39 uint32_t scanout_id; 40 uint32_t x; 41 uint32_t y; 42 } QEMU_PACKED VhostUserGpuCursorPos; 43 44 typedef struct VhostUserGpuCursorUpdate { 45 VhostUserGpuCursorPos pos; 46 uint32_t hot_x; 47 uint32_t hot_y; 48 uint32_t data[64 * 64]; 49 } QEMU_PACKED VhostUserGpuCursorUpdate; 50 51 typedef struct VhostUserGpuScanout { 52 uint32_t scanout_id; 53 uint32_t width; 54 uint32_t height; 55 } QEMU_PACKED VhostUserGpuScanout; 56 57 typedef struct VhostUserGpuUpdate { 58 uint32_t scanout_id; 59 uint32_t x; 60 uint32_t y; 61 uint32_t width; 62 uint32_t height; 63 uint8_t data[]; 64 } QEMU_PACKED VhostUserGpuUpdate; 65 66 typedef struct VhostUserGpuDMABUFScanout { 67 uint32_t scanout_id; 68 uint32_t x; 69 uint32_t y; 70 uint32_t width; 71 uint32_t height; 72 uint32_t fd_width; 73 uint32_t fd_height; 74 uint32_t fd_stride; 75 uint32_t fd_flags; 76 int fd_drm_fourcc; 77 } QEMU_PACKED VhostUserGpuDMABUFScanout; 78 79 typedef struct VhostUserGpuMsg { 80 uint32_t request; /* VhostUserGpuRequest */ 81 uint32_t flags; 82 uint32_t size; /* the following payload size */ 83 union { 84 VhostUserGpuCursorPos cursor_pos; 85 VhostUserGpuCursorUpdate cursor_update; 86 VhostUserGpuScanout scanout; 87 VhostUserGpuUpdate update; 88 VhostUserGpuDMABUFScanout dmabuf_scanout; 89 struct virtio_gpu_resp_display_info display_info; 90 uint64_t u64; 91 } payload; 92 } QEMU_PACKED VhostUserGpuMsg; 93 94 static VhostUserGpuMsg m __attribute__ ((unused)); 95 #define VHOST_USER_GPU_HDR_SIZE \ 96 (sizeof(m.request) + sizeof(m.size) + sizeof(m.flags)) 97 98 #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4 99 100 static void vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked); 101 102 static void 103 vhost_user_gpu_handle_cursor(VhostUserGPU *g, VhostUserGpuMsg *msg) 104 { 105 VhostUserGpuCursorPos *pos = &msg->payload.cursor_pos; 106 struct virtio_gpu_scanout *s; 107 108 if (pos->scanout_id >= g->parent_obj.conf.max_outputs) { 109 return; 110 } 111 s = &g->parent_obj.scanout[pos->scanout_id]; 112 113 if (msg->request == VHOST_USER_GPU_CURSOR_UPDATE) { 114 VhostUserGpuCursorUpdate *up = &msg->payload.cursor_update; 115 if (!s->current_cursor) { 116 s->current_cursor = cursor_alloc(64, 64); 117 } 118 119 s->current_cursor->hot_x = up->hot_x; 120 s->current_cursor->hot_y = up->hot_y; 121 122 memcpy(s->current_cursor->data, up->data, 123 64 * 64 * sizeof(uint32_t)); 124 125 dpy_cursor_define(s->con, s->current_cursor); 126 } 127 128 dpy_mouse_set(s->con, pos->x, pos->y, 129 msg->request != VHOST_USER_GPU_CURSOR_POS_HIDE); 130 } 131 132 static void 133 vhost_user_gpu_send_msg(VhostUserGPU *g, const VhostUserGpuMsg *msg) 134 { 135 qemu_chr_fe_write(&g->vhost_chr, (uint8_t *)msg, 136 VHOST_USER_GPU_HDR_SIZE + msg->size); 137 } 138 139 static void 140 vhost_user_gpu_unblock(VhostUserGPU *g) 141 { 142 VhostUserGpuMsg msg = { 143 .request = VHOST_USER_GPU_DMABUF_UPDATE, 144 .flags = VHOST_USER_GPU_MSG_FLAG_REPLY, 145 }; 146 147 vhost_user_gpu_send_msg(g, &msg); 148 } 149 150 static void 151 vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg) 152 { 153 QemuConsole *con = NULL; 154 struct virtio_gpu_scanout *s; 155 156 switch (msg->request) { 157 case VHOST_USER_GPU_GET_PROTOCOL_FEATURES: { 158 VhostUserGpuMsg reply = { 159 .request = msg->request, 160 .flags = VHOST_USER_GPU_MSG_FLAG_REPLY, 161 .size = sizeof(uint64_t), 162 }; 163 164 vhost_user_gpu_send_msg(g, &reply); 165 break; 166 } 167 case VHOST_USER_GPU_SET_PROTOCOL_FEATURES: { 168 break; 169 } 170 case VHOST_USER_GPU_GET_DISPLAY_INFO: { 171 struct virtio_gpu_resp_display_info display_info = { {} }; 172 VhostUserGpuMsg reply = { 173 .request = msg->request, 174 .flags = VHOST_USER_GPU_MSG_FLAG_REPLY, 175 .size = sizeof(struct virtio_gpu_resp_display_info), 176 }; 177 178 display_info.hdr.type = VIRTIO_GPU_RESP_OK_DISPLAY_INFO; 179 virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g), &display_info); 180 memcpy(&reply.payload.display_info, &display_info, 181 sizeof(display_info)); 182 vhost_user_gpu_send_msg(g, &reply); 183 break; 184 } 185 case VHOST_USER_GPU_SCANOUT: { 186 VhostUserGpuScanout *m = &msg->payload.scanout; 187 188 if (m->scanout_id >= g->parent_obj.conf.max_outputs) { 189 return; 190 } 191 192 g->parent_obj.enable = 1; 193 s = &g->parent_obj.scanout[m->scanout_id]; 194 con = s->con; 195 196 if (m->scanout_id == 0 && m->width == 0) { 197 s->ds = qemu_create_message_surface(640, 480, 198 "Guest disabled display."); 199 dpy_gfx_replace_surface(con, s->ds); 200 } else { 201 s->ds = qemu_create_displaysurface(m->width, m->height); 202 /* replace surface on next update */ 203 } 204 205 break; 206 } 207 case VHOST_USER_GPU_DMABUF_SCANOUT: { 208 VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout; 209 int fd = qemu_chr_fe_get_msgfd(&g->vhost_chr); 210 QemuDmaBuf *dmabuf; 211 212 if (m->scanout_id >= g->parent_obj.conf.max_outputs) { 213 error_report("invalid scanout: %d", m->scanout_id); 214 if (fd >= 0) { 215 close(fd); 216 } 217 break; 218 } 219 220 g->parent_obj.enable = 1; 221 con = g->parent_obj.scanout[m->scanout_id].con; 222 dmabuf = &g->dmabuf[m->scanout_id]; 223 if (dmabuf->fd >= 0) { 224 close(dmabuf->fd); 225 dmabuf->fd = -1; 226 } 227 dpy_gl_release_dmabuf(con, dmabuf); 228 if (fd == -1) { 229 dpy_gl_scanout_disable(con); 230 break; 231 } 232 *dmabuf = (QemuDmaBuf) { 233 .fd = fd, 234 .width = m->fd_width, 235 .height = m->fd_height, 236 .stride = m->fd_stride, 237 .fourcc = m->fd_drm_fourcc, 238 .y0_top = m->fd_flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP, 239 }; 240 dpy_gl_scanout_dmabuf(con, dmabuf); 241 break; 242 } 243 case VHOST_USER_GPU_DMABUF_UPDATE: { 244 VhostUserGpuUpdate *m = &msg->payload.update; 245 246 if (m->scanout_id >= g->parent_obj.conf.max_outputs || 247 !g->parent_obj.scanout[m->scanout_id].con) { 248 error_report("invalid scanout update: %d", m->scanout_id); 249 vhost_user_gpu_unblock(g); 250 break; 251 } 252 253 con = g->parent_obj.scanout[m->scanout_id].con; 254 if (!console_has_gl(con)) { 255 error_report("console doesn't support GL!"); 256 vhost_user_gpu_unblock(g); 257 break; 258 } 259 dpy_gl_update(con, m->x, m->y, m->width, m->height); 260 g->backend_blocked = true; 261 break; 262 } 263 case VHOST_USER_GPU_UPDATE: { 264 VhostUserGpuUpdate *m = &msg->payload.update; 265 266 if (m->scanout_id >= g->parent_obj.conf.max_outputs) { 267 break; 268 } 269 s = &g->parent_obj.scanout[m->scanout_id]; 270 con = s->con; 271 pixman_image_t *image = 272 pixman_image_create_bits(PIXMAN_x8r8g8b8, 273 m->width, 274 m->height, 275 (uint32_t *)m->data, 276 m->width * 4); 277 278 pixman_image_composite(PIXMAN_OP_SRC, 279 image, NULL, s->ds->image, 280 0, 0, 0, 0, m->x, m->y, m->width, m->height); 281 282 pixman_image_unref(image); 283 if (qemu_console_surface(con) != s->ds) { 284 dpy_gfx_replace_surface(con, s->ds); 285 } else { 286 dpy_gfx_update(con, m->x, m->y, m->width, m->height); 287 } 288 break; 289 } 290 default: 291 g_warning("unhandled message %d %d", msg->request, msg->size); 292 } 293 294 if (con && qemu_console_is_gl_blocked(con)) { 295 vhost_user_gpu_update_blocked(g, true); 296 } 297 } 298 299 static void 300 vhost_user_gpu_chr_read(void *opaque) 301 { 302 VhostUserGPU *g = opaque; 303 VhostUserGpuMsg *msg = NULL; 304 VhostUserGpuRequest request; 305 uint32_t size, flags; 306 int r; 307 308 r = qemu_chr_fe_read_all(&g->vhost_chr, 309 (uint8_t *)&request, sizeof(uint32_t)); 310 if (r != sizeof(uint32_t)) { 311 error_report("failed to read msg header: %d, %d", r, errno); 312 goto end; 313 } 314 315 r = qemu_chr_fe_read_all(&g->vhost_chr, 316 (uint8_t *)&flags, sizeof(uint32_t)); 317 if (r != sizeof(uint32_t)) { 318 error_report("failed to read msg flags"); 319 goto end; 320 } 321 322 r = qemu_chr_fe_read_all(&g->vhost_chr, 323 (uint8_t *)&size, sizeof(uint32_t)); 324 if (r != sizeof(uint32_t)) { 325 error_report("failed to read msg size"); 326 goto end; 327 } 328 329 msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size); 330 g_return_if_fail(msg != NULL); 331 332 r = qemu_chr_fe_read_all(&g->vhost_chr, 333 (uint8_t *)&msg->payload, size); 334 if (r != size) { 335 error_report("failed to read msg payload %d != %d", r, size); 336 goto end; 337 } 338 339 msg->request = request; 340 msg->flags = size; 341 msg->size = size; 342 343 if (request == VHOST_USER_GPU_CURSOR_UPDATE || 344 request == VHOST_USER_GPU_CURSOR_POS || 345 request == VHOST_USER_GPU_CURSOR_POS_HIDE) { 346 vhost_user_gpu_handle_cursor(g, msg); 347 } else { 348 vhost_user_gpu_handle_display(g, msg); 349 } 350 351 end: 352 g_free(msg); 353 } 354 355 static void 356 vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked) 357 { 358 qemu_set_fd_handler(g->vhost_gpu_fd, 359 blocked ? NULL : vhost_user_gpu_chr_read, NULL, g); 360 } 361 362 static void 363 vhost_user_gpu_gl_flushed(VirtIOGPUBase *b) 364 { 365 VhostUserGPU *g = VHOST_USER_GPU(b); 366 367 if (g->backend_blocked) { 368 vhost_user_gpu_unblock(VHOST_USER_GPU(g)); 369 g->backend_blocked = false; 370 } 371 372 vhost_user_gpu_update_blocked(VHOST_USER_GPU(g), false); 373 } 374 375 static bool 376 vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp) 377 { 378 Chardev *chr; 379 int sv[2]; 380 381 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { 382 error_setg_errno(errp, errno, "socketpair() failed"); 383 return false; 384 } 385 386 chr = CHARDEV(object_new(TYPE_CHARDEV_SOCKET)); 387 if (!chr || qemu_chr_add_client(chr, sv[0]) == -1) { 388 error_setg(errp, "Failed to make socket chardev"); 389 goto err; 390 } 391 if (!qemu_chr_fe_init(&g->vhost_chr, chr, errp)) { 392 goto err; 393 } 394 if (vhost_user_gpu_set_socket(&g->vhost->dev, sv[1]) < 0) { 395 error_setg(errp, "Failed to set vhost-user-gpu socket"); 396 qemu_chr_fe_deinit(&g->vhost_chr, false); 397 goto err; 398 } 399 400 g->vhost_gpu_fd = sv[0]; 401 vhost_user_gpu_update_blocked(g, false); 402 close(sv[1]); 403 return true; 404 405 err: 406 close(sv[0]); 407 close(sv[1]); 408 if (chr) { 409 object_unref(OBJECT(chr)); 410 } 411 return false; 412 } 413 414 static void 415 vhost_user_gpu_get_config(VirtIODevice *vdev, uint8_t *config_data) 416 { 417 VhostUserGPU *g = VHOST_USER_GPU(vdev); 418 VirtIOGPUBase *b = VIRTIO_GPU_BASE(vdev); 419 struct virtio_gpu_config *vgconfig = 420 (struct virtio_gpu_config *)config_data; 421 int ret; 422 423 memset(config_data, 0, sizeof(struct virtio_gpu_config)); 424 425 ret = vhost_dev_get_config(&g->vhost->dev, 426 config_data, sizeof(struct virtio_gpu_config)); 427 if (ret) { 428 error_report("vhost-user-gpu: get device config space failed"); 429 return; 430 } 431 432 /* those fields are managed by qemu */ 433 vgconfig->num_scanouts = b->virtio_config.num_scanouts; 434 vgconfig->events_read = b->virtio_config.events_read; 435 vgconfig->events_clear = b->virtio_config.events_clear; 436 } 437 438 static void 439 vhost_user_gpu_set_config(VirtIODevice *vdev, 440 const uint8_t *config_data) 441 { 442 VhostUserGPU *g = VHOST_USER_GPU(vdev); 443 VirtIOGPUBase *b = VIRTIO_GPU_BASE(vdev); 444 const struct virtio_gpu_config *vgconfig = 445 (const struct virtio_gpu_config *)config_data; 446 int ret; 447 448 if (vgconfig->events_clear) { 449 b->virtio_config.events_read &= ~vgconfig->events_clear; 450 } 451 452 ret = vhost_dev_set_config(&g->vhost->dev, config_data, 453 0, sizeof(struct virtio_gpu_config), 454 VHOST_SET_CONFIG_TYPE_MASTER); 455 if (ret) { 456 error_report("vhost-user-gpu: set device config space failed"); 457 return; 458 } 459 } 460 461 static void 462 vhost_user_gpu_set_status(VirtIODevice *vdev, uint8_t val) 463 { 464 VhostUserGPU *g = VHOST_USER_GPU(vdev); 465 Error *err = NULL; 466 467 if (val & VIRTIO_CONFIG_S_DRIVER_OK && vdev->vm_running) { 468 if (!vhost_user_gpu_do_set_socket(g, &err)) { 469 error_report_err(err); 470 return; 471 } 472 vhost_user_backend_start(g->vhost); 473 } else { 474 /* unblock any wait and stop processing */ 475 if (g->vhost_gpu_fd != -1) { 476 vhost_user_gpu_update_blocked(g, true); 477 qemu_chr_fe_deinit(&g->vhost_chr, true); 478 g->vhost_gpu_fd = -1; 479 } 480 vhost_user_backend_stop(g->vhost); 481 } 482 } 483 484 static bool 485 vhost_user_gpu_guest_notifier_pending(VirtIODevice *vdev, int idx) 486 { 487 VhostUserGPU *g = VHOST_USER_GPU(vdev); 488 489 return vhost_virtqueue_pending(&g->vhost->dev, idx); 490 } 491 492 static void 493 vhost_user_gpu_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask) 494 { 495 VhostUserGPU *g = VHOST_USER_GPU(vdev); 496 497 vhost_virtqueue_mask(&g->vhost->dev, vdev, idx, mask); 498 } 499 500 static void 501 vhost_user_gpu_instance_init(Object *obj) 502 { 503 VhostUserGPU *g = VHOST_USER_GPU(obj); 504 505 g->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND)); 506 object_property_add_alias(obj, "chardev", 507 OBJECT(g->vhost), "chardev"); 508 } 509 510 static void 511 vhost_user_gpu_instance_finalize(Object *obj) 512 { 513 VhostUserGPU *g = VHOST_USER_GPU(obj); 514 515 object_unref(OBJECT(g->vhost)); 516 } 517 518 static void 519 vhost_user_gpu_reset(VirtIODevice *vdev) 520 { 521 VhostUserGPU *g = VHOST_USER_GPU(vdev); 522 523 virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev)); 524 525 vhost_user_backend_stop(g->vhost); 526 } 527 528 static int 529 vhost_user_gpu_config_change(struct vhost_dev *dev) 530 { 531 error_report("vhost-user-gpu: unhandled backend config change"); 532 return -1; 533 } 534 535 static const VhostDevConfigOps config_ops = { 536 .vhost_dev_config_notifier = vhost_user_gpu_config_change, 537 }; 538 539 static void 540 vhost_user_gpu_device_realize(DeviceState *qdev, Error **errp) 541 { 542 VhostUserGPU *g = VHOST_USER_GPU(qdev); 543 VirtIODevice *vdev = VIRTIO_DEVICE(g); 544 545 vhost_dev_set_config_notifier(&g->vhost->dev, &config_ops); 546 if (vhost_user_backend_dev_init(g->vhost, vdev, 2, errp) < 0) { 547 return; 548 } 549 550 /* existing backend may send DMABUF, so let's add that requirement */ 551 g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED; 552 if (virtio_has_feature(g->vhost->dev.features, VIRTIO_GPU_F_VIRGL)) { 553 g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED; 554 } 555 if (virtio_has_feature(g->vhost->dev.features, VIRTIO_GPU_F_EDID)) { 556 g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_EDID_ENABLED; 557 } else { 558 error_report("EDID requested but the backend doesn't support it."); 559 g->parent_obj.conf.flags &= ~(1 << VIRTIO_GPU_FLAG_EDID_ENABLED); 560 } 561 562 if (!virtio_gpu_base_device_realize(qdev, NULL, NULL, errp)) { 563 return; 564 } 565 566 g->vhost_gpu_fd = -1; 567 } 568 569 static Property vhost_user_gpu_properties[] = { 570 VIRTIO_GPU_BASE_PROPERTIES(VhostUserGPU, parent_obj.conf), 571 DEFINE_PROP_END_OF_LIST(), 572 }; 573 574 static void 575 vhost_user_gpu_class_init(ObjectClass *klass, void *data) 576 { 577 DeviceClass *dc = DEVICE_CLASS(klass); 578 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 579 VirtIOGPUBaseClass *vgc = VIRTIO_GPU_BASE_CLASS(klass); 580 581 vgc->gl_flushed = vhost_user_gpu_gl_flushed; 582 583 vdc->realize = vhost_user_gpu_device_realize; 584 vdc->reset = vhost_user_gpu_reset; 585 vdc->set_status = vhost_user_gpu_set_status; 586 vdc->guest_notifier_mask = vhost_user_gpu_guest_notifier_mask; 587 vdc->guest_notifier_pending = vhost_user_gpu_guest_notifier_pending; 588 vdc->get_config = vhost_user_gpu_get_config; 589 vdc->set_config = vhost_user_gpu_set_config; 590 591 device_class_set_props(dc, vhost_user_gpu_properties); 592 } 593 594 static const TypeInfo vhost_user_gpu_info = { 595 .name = TYPE_VHOST_USER_GPU, 596 .parent = TYPE_VIRTIO_GPU_BASE, 597 .instance_size = sizeof(VhostUserGPU), 598 .instance_init = vhost_user_gpu_instance_init, 599 .instance_finalize = vhost_user_gpu_instance_finalize, 600 .class_init = vhost_user_gpu_class_init, 601 }; 602 603 static void vhost_user_gpu_register_types(void) 604 { 605 type_register_static(&vhost_user_gpu_info); 606 } 607 608 type_init(vhost_user_gpu_register_types) 609