4 * Copyright Red Hat, Inc. 2013-2014
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
17 #include "ui/console.h"
19 #include "hw/virtio/virtio.h"
20 #include "hw/virtio/virtio-gpu.h"
21 #include "hw/virtio/virtio-bus.h"
22 #include "migration/migration.h"
24 #include "qapi/error.h"
26 #define VIRTIO_GPU_VM_VERSION 1
28 static struct virtio_gpu_simple_resource*
29 virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id);
31 static void virtio_gpu_cleanup_mapping(struct virtio_gpu_simple_resource *res);
34 #include <virglrenderer.h>
35 #define VIRGL(_g, _virgl, _simple, ...) \
37 if (_g->use_virgl_renderer) { \
38 _virgl(__VA_ARGS__); \
40 _simple(__VA_ARGS__); \
44 #define VIRGL(_g, _virgl, _simple, ...) \
46 _simple(__VA_ARGS__); \
50 static void update_cursor_data_simple(VirtIOGPU *g,
51 struct virtio_gpu_scanout *s,
54 struct virtio_gpu_simple_resource *res;
57 res = virtio_gpu_find_resource(g, resource_id);
62 if (pixman_image_get_width(res->image) != s->current_cursor->width ||
63 pixman_image_get_height(res->image) != s->current_cursor->height) {
67 pixels = s->current_cursor->width * s->current_cursor->height;
68 memcpy(s->current_cursor->data,
69 pixman_image_get_data(res->image),
70 pixels * sizeof(uint32_t));
75 static void update_cursor_data_virgl(VirtIOGPU *g,
76 struct virtio_gpu_scanout *s,
79 uint32_t width, height;
80 uint32_t pixels, *data;
82 data = virgl_renderer_get_cursor_data(resource_id, &width, &height);
87 if (width != s->current_cursor->width ||
88 height != s->current_cursor->height) {
93 pixels = s->current_cursor->width * s->current_cursor->height;
94 memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t));
100 static void update_cursor(VirtIOGPU *g, struct virtio_gpu_update_cursor *cursor)
102 struct virtio_gpu_scanout *s;
103 bool move = cursor->hdr.type == VIRTIO_GPU_CMD_MOVE_CURSOR;
105 if (cursor->pos.scanout_id >= g->conf.max_outputs) {
108 s = &g->scanout[cursor->pos.scanout_id];
110 trace_virtio_gpu_update_cursor(cursor->pos.scanout_id,
113 move ? "move" : "update",
114 cursor->resource_id);
117 if (!s->current_cursor) {
118 s->current_cursor = cursor_alloc(64, 64);
121 s->current_cursor->hot_x = cursor->hot_x;
122 s->current_cursor->hot_y = cursor->hot_y;
124 if (cursor->resource_id > 0) {
125 VIRGL(g, update_cursor_data_virgl, update_cursor_data_simple,
126 g, s, cursor->resource_id);
128 dpy_cursor_define(s->con, s->current_cursor);
132 s->cursor.pos.x = cursor->pos.x;
133 s->cursor.pos.y = cursor->pos.y;
135 dpy_mouse_set(s->con, cursor->pos.x, cursor->pos.y,
136 cursor->resource_id ? 1 : 0);
139 static void virtio_gpu_get_config(VirtIODevice *vdev, uint8_t *config)
141 VirtIOGPU *g = VIRTIO_GPU(vdev);
142 memcpy(config, &g->virtio_config, sizeof(g->virtio_config));
145 static void virtio_gpu_set_config(VirtIODevice *vdev, const uint8_t *config)
147 VirtIOGPU *g = VIRTIO_GPU(vdev);
148 struct virtio_gpu_config vgconfig;
150 memcpy(&vgconfig, config, sizeof(g->virtio_config));
152 if (vgconfig.events_clear) {
153 g->virtio_config.events_read &= ~vgconfig.events_clear;
157 static uint64_t virtio_gpu_get_features(VirtIODevice *vdev, uint64_t features,
160 VirtIOGPU *g = VIRTIO_GPU(vdev);
162 if (virtio_gpu_virgl_enabled(g->conf)) {
163 features |= (1 << VIRTIO_GPU_F_VIRGL);
168 static void virtio_gpu_set_features(VirtIODevice *vdev, uint64_t features)
170 static const uint32_t virgl = (1 << VIRTIO_GPU_F_VIRGL);
171 VirtIOGPU *g = VIRTIO_GPU(vdev);
173 g->use_virgl_renderer = ((features & virgl) == virgl);
174 trace_virtio_gpu_features(g->use_virgl_renderer);
177 static void virtio_gpu_notify_event(VirtIOGPU *g, uint32_t event_type)
179 g->virtio_config.events_read |= event_type;
180 virtio_notify_config(&g->parent_obj);
183 static struct virtio_gpu_simple_resource *
184 virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id)
186 struct virtio_gpu_simple_resource *res;
188 QTAILQ_FOREACH(res, &g->reslist, next) {
189 if (res->resource_id == resource_id) {
196 void virtio_gpu_ctrl_response(VirtIOGPU *g,
197 struct virtio_gpu_ctrl_command *cmd,
198 struct virtio_gpu_ctrl_hdr *resp,
203 if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE) {
204 resp->flags |= VIRTIO_GPU_FLAG_FENCE;
205 resp->fence_id = cmd->cmd_hdr.fence_id;
206 resp->ctx_id = cmd->cmd_hdr.ctx_id;
208 s = iov_from_buf(cmd->elem.in_sg, cmd->elem.in_num, 0, resp, resp_len);
210 qemu_log_mask(LOG_GUEST_ERROR,
211 "%s: response size incorrect %zu vs %zu\n",
212 __func__, s, resp_len);
214 virtqueue_push(cmd->vq, &cmd->elem, s);
215 virtio_notify(VIRTIO_DEVICE(g), cmd->vq);
216 cmd->finished = true;
219 void virtio_gpu_ctrl_response_nodata(VirtIOGPU *g,
220 struct virtio_gpu_ctrl_command *cmd,
221 enum virtio_gpu_ctrl_type type)
223 struct virtio_gpu_ctrl_hdr resp;
225 memset(&resp, 0, sizeof(resp));
227 virtio_gpu_ctrl_response(g, cmd, &resp, sizeof(resp));
231 virtio_gpu_fill_display_info(VirtIOGPU *g,
232 struct virtio_gpu_resp_display_info *dpy_info)
236 for (i = 0; i < g->conf.max_outputs; i++) {
237 if (g->enabled_output_bitmask & (1 << i)) {
238 dpy_info->pmodes[i].enabled = 1;
239 dpy_info->pmodes[i].r.width = g->req_state[i].width;
240 dpy_info->pmodes[i].r.height = g->req_state[i].height;
245 void virtio_gpu_get_display_info(VirtIOGPU *g,
246 struct virtio_gpu_ctrl_command *cmd)
248 struct virtio_gpu_resp_display_info display_info;
250 trace_virtio_gpu_cmd_get_display_info();
251 memset(&display_info, 0, sizeof(display_info));
252 display_info.hdr.type = VIRTIO_GPU_RESP_OK_DISPLAY_INFO;
253 virtio_gpu_fill_display_info(g, &display_info);
254 virtio_gpu_ctrl_response(g, cmd, &display_info.hdr,
255 sizeof(display_info));
258 static pixman_format_code_t get_pixman_format(uint32_t virtio_gpu_format)
260 switch (virtio_gpu_format) {
261 case VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM:
262 return PIXMAN_BE_b8g8r8x8;
263 case VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM:
264 return PIXMAN_BE_b8g8r8a8;
265 case VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM:
266 return PIXMAN_BE_x8r8g8b8;
267 case VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM:
268 return PIXMAN_BE_a8r8g8b8;
269 case VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM:
270 return PIXMAN_BE_r8g8b8x8;
271 case VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM:
272 return PIXMAN_BE_r8g8b8a8;
273 case VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM:
274 return PIXMAN_BE_x8b8g8r8;
275 case VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM:
276 return PIXMAN_BE_a8b8g8r8;
282 static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
283 struct virtio_gpu_ctrl_command *cmd)
285 pixman_format_code_t pformat;
286 struct virtio_gpu_simple_resource *res;
287 struct virtio_gpu_resource_create_2d c2d;
289 VIRTIO_GPU_FILL_CMD(c2d);
290 trace_virtio_gpu_cmd_res_create_2d(c2d.resource_id, c2d.format,
291 c2d.width, c2d.height);
293 if (c2d.resource_id == 0) {
294 qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
296 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
300 res = virtio_gpu_find_resource(g, c2d.resource_id);
302 qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already exists %d\n",
303 __func__, c2d.resource_id);
304 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
308 res = g_new0(struct virtio_gpu_simple_resource, 1);
310 res->width = c2d.width;
311 res->height = c2d.height;
312 res->format = c2d.format;
313 res->resource_id = c2d.resource_id;
315 pformat = get_pixman_format(c2d.format);
317 qemu_log_mask(LOG_GUEST_ERROR,
318 "%s: host couldn't handle guest format %d\n",
319 __func__, c2d.format);
321 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
325 res->hostmem = PIXMAN_FORMAT_BPP(pformat) * c2d.width * c2d.height;
326 if (res->hostmem + g->hostmem < g->conf.max_hostmem) {
327 res->image = pixman_image_create_bits(pformat,
334 qemu_log_mask(LOG_GUEST_ERROR,
335 "%s: resource creation failed %d %d %d\n",
336 __func__, c2d.resource_id, c2d.width, c2d.height);
338 cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
342 QTAILQ_INSERT_HEAD(&g->reslist, res, next);
343 g->hostmem += res->hostmem;
346 static void virtio_gpu_resource_destroy(VirtIOGPU *g,
347 struct virtio_gpu_simple_resource *res)
349 pixman_image_unref(res->image);
350 virtio_gpu_cleanup_mapping(res);
351 QTAILQ_REMOVE(&g->reslist, res, next);
352 g->hostmem -= res->hostmem;
356 static void virtio_gpu_resource_unref(VirtIOGPU *g,
357 struct virtio_gpu_ctrl_command *cmd)
359 struct virtio_gpu_simple_resource *res;
360 struct virtio_gpu_resource_unref unref;
362 VIRTIO_GPU_FILL_CMD(unref);
363 trace_virtio_gpu_cmd_res_unref(unref.resource_id);
365 res = virtio_gpu_find_resource(g, unref.resource_id);
367 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
368 __func__, unref.resource_id);
369 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
372 virtio_gpu_resource_destroy(g, res);
375 static void virtio_gpu_transfer_to_host_2d(VirtIOGPU *g,
376 struct virtio_gpu_ctrl_command *cmd)
378 struct virtio_gpu_simple_resource *res;
380 uint32_t src_offset, dst_offset, stride;
382 pixman_format_code_t format;
383 struct virtio_gpu_transfer_to_host_2d t2d;
385 VIRTIO_GPU_FILL_CMD(t2d);
386 trace_virtio_gpu_cmd_res_xfer_toh_2d(t2d.resource_id);
388 res = virtio_gpu_find_resource(g, t2d.resource_id);
389 if (!res || !res->iov) {
390 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
391 __func__, t2d.resource_id);
392 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
396 if (t2d.r.x > res->width ||
397 t2d.r.y > res->height ||
398 t2d.r.width > res->width ||
399 t2d.r.height > res->height ||
400 t2d.r.x + t2d.r.width > res->width ||
401 t2d.r.y + t2d.r.height > res->height) {
402 qemu_log_mask(LOG_GUEST_ERROR, "%s: transfer bounds outside resource"
403 " bounds for resource %d: %d %d %d %d vs %d %d\n",
404 __func__, t2d.resource_id, t2d.r.x, t2d.r.y,
405 t2d.r.width, t2d.r.height, res->width, res->height);
406 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
410 format = pixman_image_get_format(res->image);
411 bpp = (PIXMAN_FORMAT_BPP(format) + 7) / 8;
412 stride = pixman_image_get_stride(res->image);
414 if (t2d.offset || t2d.r.x || t2d.r.y ||
415 t2d.r.width != pixman_image_get_width(res->image)) {
416 void *img_data = pixman_image_get_data(res->image);
417 for (h = 0; h < t2d.r.height; h++) {
418 src_offset = t2d.offset + stride * h;
419 dst_offset = (t2d.r.y + h) * stride + (t2d.r.x * bpp);
421 iov_to_buf(res->iov, res->iov_cnt, src_offset,
423 + dst_offset, t2d.r.width * bpp);
426 iov_to_buf(res->iov, res->iov_cnt, 0,
427 pixman_image_get_data(res->image),
428 pixman_image_get_stride(res->image)
429 * pixman_image_get_height(res->image));
433 static void virtio_gpu_resource_flush(VirtIOGPU *g,
434 struct virtio_gpu_ctrl_command *cmd)
436 struct virtio_gpu_simple_resource *res;
437 struct virtio_gpu_resource_flush rf;
438 pixman_region16_t flush_region;
441 VIRTIO_GPU_FILL_CMD(rf);
442 trace_virtio_gpu_cmd_res_flush(rf.resource_id,
443 rf.r.width, rf.r.height, rf.r.x, rf.r.y);
445 res = virtio_gpu_find_resource(g, rf.resource_id);
447 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
448 __func__, rf.resource_id);
449 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
453 if (rf.r.x > res->width ||
454 rf.r.y > res->height ||
455 rf.r.width > res->width ||
456 rf.r.height > res->height ||
457 rf.r.x + rf.r.width > res->width ||
458 rf.r.y + rf.r.height > res->height) {
459 qemu_log_mask(LOG_GUEST_ERROR, "%s: flush bounds outside resource"
460 " bounds for resource %d: %d %d %d %d vs %d %d\n",
461 __func__, rf.resource_id, rf.r.x, rf.r.y,
462 rf.r.width, rf.r.height, res->width, res->height);
463 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
467 pixman_region_init_rect(&flush_region,
468 rf.r.x, rf.r.y, rf.r.width, rf.r.height);
469 for (i = 0; i < g->conf.max_outputs; i++) {
470 struct virtio_gpu_scanout *scanout;
471 pixman_region16_t region, finalregion;
472 pixman_box16_t *extents;
474 if (!(res->scanout_bitmask & (1 << i))) {
477 scanout = &g->scanout[i];
479 pixman_region_init(&finalregion);
480 pixman_region_init_rect(®ion, scanout->x, scanout->y,
481 scanout->width, scanout->height);
483 pixman_region_intersect(&finalregion, &flush_region, ®ion);
484 pixman_region_translate(&finalregion, -scanout->x, -scanout->y);
485 extents = pixman_region_extents(&finalregion);
486 /* work out the area we need to update for each console */
487 dpy_gfx_update(g->scanout[i].con,
488 extents->x1, extents->y1,
489 extents->x2 - extents->x1,
490 extents->y2 - extents->y1);
492 pixman_region_fini(®ion);
493 pixman_region_fini(&finalregion);
495 pixman_region_fini(&flush_region);
498 static void virtio_unref_resource(pixman_image_t *image, void *data)
500 pixman_image_unref(data);
503 static void virtio_gpu_set_scanout(VirtIOGPU *g,
504 struct virtio_gpu_ctrl_command *cmd)
506 struct virtio_gpu_simple_resource *res;
507 struct virtio_gpu_scanout *scanout;
508 pixman_format_code_t format;
511 struct virtio_gpu_set_scanout ss;
513 VIRTIO_GPU_FILL_CMD(ss);
514 trace_virtio_gpu_cmd_set_scanout(ss.scanout_id, ss.resource_id,
515 ss.r.width, ss.r.height, ss.r.x, ss.r.y);
517 if (ss.scanout_id >= g->conf.max_outputs) {
518 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout id specified %d",
519 __func__, ss.scanout_id);
520 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
525 if (ss.resource_id == 0) {
526 scanout = &g->scanout[ss.scanout_id];
527 if (scanout->resource_id) {
528 res = virtio_gpu_find_resource(g, scanout->resource_id);
530 res->scanout_bitmask &= ~(1 << ss.scanout_id);
533 if (ss.scanout_id == 0) {
534 qemu_log_mask(LOG_GUEST_ERROR,
535 "%s: illegal scanout id specified %d",
536 __func__, ss.scanout_id);
537 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
540 dpy_gfx_replace_surface(g->scanout[ss.scanout_id].con, NULL);
547 /* create a surface for this scanout */
548 res = virtio_gpu_find_resource(g, ss.resource_id);
550 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
551 __func__, ss.resource_id);
552 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
556 if (ss.r.x > res->width ||
557 ss.r.y > res->height ||
558 ss.r.width > res->width ||
559 ss.r.height > res->height ||
560 ss.r.x + ss.r.width > res->width ||
561 ss.r.y + ss.r.height > res->height) {
562 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout %d bounds for"
563 " resource %d, (%d,%d)+%d,%d vs %d %d\n",
564 __func__, ss.scanout_id, ss.resource_id, ss.r.x, ss.r.y,
565 ss.r.width, ss.r.height, res->width, res->height);
566 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
570 scanout = &g->scanout[ss.scanout_id];
572 format = pixman_image_get_format(res->image);
573 bpp = (PIXMAN_FORMAT_BPP(format) + 7) / 8;
574 offset = (ss.r.x * bpp) + ss.r.y * pixman_image_get_stride(res->image);
575 if (!scanout->ds || surface_data(scanout->ds)
576 != ((uint8_t *)pixman_image_get_data(res->image) + offset) ||
577 scanout->width != ss.r.width ||
578 scanout->height != ss.r.height) {
579 pixman_image_t *rect;
580 void *ptr = (uint8_t *)pixman_image_get_data(res->image) + offset;
581 rect = pixman_image_create_bits(format, ss.r.width, ss.r.height, ptr,
582 pixman_image_get_stride(res->image));
583 pixman_image_ref(res->image);
584 pixman_image_set_destroy_function(rect, virtio_unref_resource,
586 /* realloc the surface ptr */
587 scanout->ds = qemu_create_displaysurface_pixman(rect);
589 cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
592 pixman_image_unref(rect);
593 dpy_gfx_replace_surface(g->scanout[ss.scanout_id].con, scanout->ds);
596 res->scanout_bitmask |= (1 << ss.scanout_id);
597 scanout->resource_id = ss.resource_id;
600 scanout->width = ss.r.width;
601 scanout->height = ss.r.height;
604 int virtio_gpu_create_mapping_iov(struct virtio_gpu_resource_attach_backing *ab,
605 struct virtio_gpu_ctrl_command *cmd,
606 uint64_t **addr, struct iovec **iov)
608 struct virtio_gpu_mem_entry *ents;
612 if (ab->nr_entries > 16384) {
613 qemu_log_mask(LOG_GUEST_ERROR,
614 "%s: nr_entries is too big (%d > 16384)\n",
615 __func__, ab->nr_entries);
619 esize = sizeof(*ents) * ab->nr_entries;
620 ents = g_malloc(esize);
621 s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
622 sizeof(*ab), ents, esize);
624 qemu_log_mask(LOG_GUEST_ERROR,
625 "%s: command data size incorrect %zu vs %zu\n",
631 *iov = g_malloc0(sizeof(struct iovec) * ab->nr_entries);
633 *addr = g_malloc0(sizeof(uint64_t) * ab->nr_entries);
635 for (i = 0; i < ab->nr_entries; i++) {
636 hwaddr len = ents[i].length;
637 (*iov)[i].iov_len = ents[i].length;
638 (*iov)[i].iov_base = cpu_physical_memory_map(ents[i].addr, &len, 1);
640 (*addr)[i] = ents[i].addr;
642 if (!(*iov)[i].iov_base || len != ents[i].length) {
643 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map MMIO memory for"
644 " resource %d element %d\n",
645 __func__, ab->resource_id, i);
646 virtio_gpu_cleanup_mapping_iov(*iov, i);
660 void virtio_gpu_cleanup_mapping_iov(struct iovec *iov, uint32_t count)
664 for (i = 0; i < count; i++) {
665 cpu_physical_memory_unmap(iov[i].iov_base, iov[i].iov_len, 1,
671 static void virtio_gpu_cleanup_mapping(struct virtio_gpu_simple_resource *res)
673 virtio_gpu_cleanup_mapping_iov(res->iov, res->iov_cnt);
681 virtio_gpu_resource_attach_backing(VirtIOGPU *g,
682 struct virtio_gpu_ctrl_command *cmd)
684 struct virtio_gpu_simple_resource *res;
685 struct virtio_gpu_resource_attach_backing ab;
688 VIRTIO_GPU_FILL_CMD(ab);
689 trace_virtio_gpu_cmd_res_back_attach(ab.resource_id);
691 res = virtio_gpu_find_resource(g, ab.resource_id);
693 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
694 __func__, ab.resource_id);
695 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
700 cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
704 ret = virtio_gpu_create_mapping_iov(&ab, cmd, &res->addrs, &res->iov);
706 cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
710 res->iov_cnt = ab.nr_entries;
714 virtio_gpu_resource_detach_backing(VirtIOGPU *g,
715 struct virtio_gpu_ctrl_command *cmd)
717 struct virtio_gpu_simple_resource *res;
718 struct virtio_gpu_resource_detach_backing detach;
720 VIRTIO_GPU_FILL_CMD(detach);
721 trace_virtio_gpu_cmd_res_back_detach(detach.resource_id);
723 res = virtio_gpu_find_resource(g, detach.resource_id);
724 if (!res || !res->iov) {
725 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
726 __func__, detach.resource_id);
727 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
730 virtio_gpu_cleanup_mapping(res);
733 static void virtio_gpu_simple_process_cmd(VirtIOGPU *g,
734 struct virtio_gpu_ctrl_command *cmd)
736 VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
738 switch (cmd->cmd_hdr.type) {
739 case VIRTIO_GPU_CMD_GET_DISPLAY_INFO:
740 virtio_gpu_get_display_info(g, cmd);
742 case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D:
743 virtio_gpu_resource_create_2d(g, cmd);
745 case VIRTIO_GPU_CMD_RESOURCE_UNREF:
746 virtio_gpu_resource_unref(g, cmd);
748 case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
749 virtio_gpu_resource_flush(g, cmd);
751 case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D:
752 virtio_gpu_transfer_to_host_2d(g, cmd);
754 case VIRTIO_GPU_CMD_SET_SCANOUT:
755 virtio_gpu_set_scanout(g, cmd);
757 case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING:
758 virtio_gpu_resource_attach_backing(g, cmd);
760 case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
761 virtio_gpu_resource_detach_backing(g, cmd);
764 cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
767 if (!cmd->finished) {
768 virtio_gpu_ctrl_response_nodata(g, cmd, cmd->error ? cmd->error :
769 VIRTIO_GPU_RESP_OK_NODATA);
773 static void virtio_gpu_handle_ctrl_cb(VirtIODevice *vdev, VirtQueue *vq)
775 VirtIOGPU *g = VIRTIO_GPU(vdev);
776 qemu_bh_schedule(g->ctrl_bh);
779 static void virtio_gpu_handle_cursor_cb(VirtIODevice *vdev, VirtQueue *vq)
781 VirtIOGPU *g = VIRTIO_GPU(vdev);
782 qemu_bh_schedule(g->cursor_bh);
785 void virtio_gpu_process_cmdq(VirtIOGPU *g)
787 struct virtio_gpu_ctrl_command *cmd;
789 while (!QTAILQ_EMPTY(&g->cmdq)) {
790 cmd = QTAILQ_FIRST(&g->cmdq);
792 /* process command */
793 VIRGL(g, virtio_gpu_virgl_process_cmd, virtio_gpu_simple_process_cmd,
798 QTAILQ_REMOVE(&g->cmdq, cmd, next);
799 if (virtio_gpu_stats_enabled(g->conf)) {
803 if (!cmd->finished) {
804 QTAILQ_INSERT_TAIL(&g->fenceq, cmd, next);
806 if (virtio_gpu_stats_enabled(g->conf)) {
807 if (g->stats.max_inflight < g->inflight) {
808 g->stats.max_inflight = g->inflight;
810 fprintf(stderr, "inflight: %3d (+)\r", g->inflight);
818 static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
820 VirtIOGPU *g = VIRTIO_GPU(vdev);
821 struct virtio_gpu_ctrl_command *cmd;
823 if (!virtio_queue_ready(vq)) {
828 if (!g->renderer_inited && g->use_virgl_renderer) {
829 virtio_gpu_virgl_init(g);
830 g->renderer_inited = true;
834 cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
838 cmd->finished = false;
839 cmd->waiting = false;
840 QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
841 cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
844 virtio_gpu_process_cmdq(g);
847 if (g->use_virgl_renderer) {
848 virtio_gpu_virgl_fence_poll(g);
853 static void virtio_gpu_ctrl_bh(void *opaque)
855 VirtIOGPU *g = opaque;
856 virtio_gpu_handle_ctrl(&g->parent_obj, g->ctrl_vq);
859 static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)
861 VirtIOGPU *g = VIRTIO_GPU(vdev);
862 VirtQueueElement *elem;
864 struct virtio_gpu_update_cursor cursor_info;
866 if (!virtio_queue_ready(vq)) {
870 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
875 s = iov_to_buf(elem->out_sg, elem->out_num, 0,
876 &cursor_info, sizeof(cursor_info));
877 if (s != sizeof(cursor_info)) {
878 qemu_log_mask(LOG_GUEST_ERROR,
879 "%s: cursor size incorrect %zu vs %zu\n",
880 __func__, s, sizeof(cursor_info));
882 update_cursor(g, &cursor_info);
884 virtqueue_push(vq, elem, 0);
885 virtio_notify(vdev, vq);
890 static void virtio_gpu_cursor_bh(void *opaque)
892 VirtIOGPU *g = opaque;
893 virtio_gpu_handle_cursor(&g->parent_obj, g->cursor_vq);
896 static void virtio_gpu_invalidate_display(void *opaque)
900 static void virtio_gpu_update_display(void *opaque)
904 static void virtio_gpu_text_update(void *opaque, console_ch_t *chardata)
908 static int virtio_gpu_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
910 VirtIOGPU *g = opaque;
912 if (idx >= g->conf.max_outputs) {
916 g->req_state[idx].x = info->xoff;
917 g->req_state[idx].y = info->yoff;
918 g->req_state[idx].width = info->width;
919 g->req_state[idx].height = info->height;
921 if (info->width && info->height) {
922 g->enabled_output_bitmask |= (1 << idx);
924 g->enabled_output_bitmask &= ~(1 << idx);
927 /* send event to guest */
928 virtio_gpu_notify_event(g, VIRTIO_GPU_EVENT_DISPLAY);
932 const GraphicHwOps virtio_gpu_ops = {
933 .invalidate = virtio_gpu_invalidate_display,
934 .gfx_update = virtio_gpu_update_display,
935 .text_update = virtio_gpu_text_update,
936 .ui_info = virtio_gpu_ui_info,
938 .gl_block = virtio_gpu_gl_block,
942 static const VMStateDescription vmstate_virtio_gpu_scanout = {
943 .name = "virtio-gpu-one-scanout",
945 .fields = (VMStateField[]) {
946 VMSTATE_UINT32(resource_id, struct virtio_gpu_scanout),
947 VMSTATE_UINT32(width, struct virtio_gpu_scanout),
948 VMSTATE_UINT32(height, struct virtio_gpu_scanout),
949 VMSTATE_INT32(x, struct virtio_gpu_scanout),
950 VMSTATE_INT32(y, struct virtio_gpu_scanout),
951 VMSTATE_UINT32(cursor.resource_id, struct virtio_gpu_scanout),
952 VMSTATE_UINT32(cursor.hot_x, struct virtio_gpu_scanout),
953 VMSTATE_UINT32(cursor.hot_y, struct virtio_gpu_scanout),
954 VMSTATE_UINT32(cursor.pos.x, struct virtio_gpu_scanout),
955 VMSTATE_UINT32(cursor.pos.y, struct virtio_gpu_scanout),
956 VMSTATE_END_OF_LIST()
960 static const VMStateDescription vmstate_virtio_gpu_scanouts = {
961 .name = "virtio-gpu-scanouts",
963 .fields = (VMStateField[]) {
964 VMSTATE_INT32(enable, struct VirtIOGPU),
965 VMSTATE_UINT32_EQUAL(conf.max_outputs, struct VirtIOGPU),
966 VMSTATE_STRUCT_VARRAY_UINT32(scanout, struct VirtIOGPU,
968 vmstate_virtio_gpu_scanout,
969 struct virtio_gpu_scanout),
970 VMSTATE_END_OF_LIST()
974 static int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
975 VMStateField *field, QJSON *vmdesc)
977 VirtIOGPU *g = opaque;
978 struct virtio_gpu_simple_resource *res;
981 /* in 2d mode we should never find unprocessed commands here */
982 assert(QTAILQ_EMPTY(&g->cmdq));
984 QTAILQ_FOREACH(res, &g->reslist, next) {
985 qemu_put_be32(f, res->resource_id);
986 qemu_put_be32(f, res->width);
987 qemu_put_be32(f, res->height);
988 qemu_put_be32(f, res->format);
989 qemu_put_be32(f, res->iov_cnt);
990 for (i = 0; i < res->iov_cnt; i++) {
991 qemu_put_be64(f, res->addrs[i]);
992 qemu_put_be32(f, res->iov[i].iov_len);
994 qemu_put_buffer(f, (void *)pixman_image_get_data(res->image),
995 pixman_image_get_stride(res->image) * res->height);
997 qemu_put_be32(f, 0); /* end of list */
999 vmstate_save_state(f, &vmstate_virtio_gpu_scanouts, g, NULL);
1004 static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
1005 VMStateField *field)
1007 VirtIOGPU *g = opaque;
1008 struct virtio_gpu_simple_resource *res;
1009 struct virtio_gpu_scanout *scanout;
1010 uint32_t resource_id, pformat;
1015 resource_id = qemu_get_be32(f);
1016 while (resource_id != 0) {
1017 res = g_new0(struct virtio_gpu_simple_resource, 1);
1018 res->resource_id = resource_id;
1019 res->width = qemu_get_be32(f);
1020 res->height = qemu_get_be32(f);
1021 res->format = qemu_get_be32(f);
1022 res->iov_cnt = qemu_get_be32(f);
1025 pformat = get_pixman_format(res->format);
1030 res->image = pixman_image_create_bits(pformat,
1031 res->width, res->height,
1038 res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width * res->height;
1040 res->addrs = g_new(uint64_t, res->iov_cnt);
1041 res->iov = g_new(struct iovec, res->iov_cnt);
1044 for (i = 0; i < res->iov_cnt; i++) {
1045 res->addrs[i] = qemu_get_be64(f);
1046 res->iov[i].iov_len = qemu_get_be32(f);
1048 qemu_get_buffer(f, (void *)pixman_image_get_data(res->image),
1049 pixman_image_get_stride(res->image) * res->height);
1051 /* restore mapping */
1052 for (i = 0; i < res->iov_cnt; i++) {
1053 hwaddr len = res->iov[i].iov_len;
1054 res->iov[i].iov_base =
1055 cpu_physical_memory_map(res->addrs[i], &len, 1);
1056 if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {
1057 /* Clean up the half-a-mapping we just created... */
1058 if (res->iov[i].iov_base) {
1059 cpu_physical_memory_unmap(res->iov[i].iov_base,
1062 /* ...and the mappings for previous loop iterations */
1064 virtio_gpu_cleanup_mapping(res);
1065 pixman_image_unref(res->image);
1071 QTAILQ_INSERT_HEAD(&g->reslist, res, next);
1072 g->hostmem += res->hostmem;
1074 resource_id = qemu_get_be32(f);
1077 /* load & apply scanout state */
1078 vmstate_load_state(f, &vmstate_virtio_gpu_scanouts, g, 1);
1079 for (i = 0; i < g->conf.max_outputs; i++) {
1080 scanout = &g->scanout[i];
1081 if (!scanout->resource_id) {
1084 res = virtio_gpu_find_resource(g, scanout->resource_id);
1088 scanout->ds = qemu_create_displaysurface_pixman(res->image);
1093 dpy_gfx_replace_surface(scanout->con, scanout->ds);
1094 dpy_gfx_update(scanout->con, 0, 0, scanout->width, scanout->height);
1095 update_cursor(g, &scanout->cursor);
1096 res->scanout_bitmask |= (1 << i);
1102 static void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
1104 VirtIODevice *vdev = VIRTIO_DEVICE(qdev);
1105 VirtIOGPU *g = VIRTIO_GPU(qdev);
1107 Error *local_err = NULL;
1110 if (g->conf.max_outputs > VIRTIO_GPU_MAX_SCANOUTS) {
1111 error_setg(errp, "invalid max_outputs > %d", VIRTIO_GPU_MAX_SCANOUTS);
1115 g->use_virgl_renderer = false;
1116 #if !defined(CONFIG_VIRGL) || defined(HOST_WORDS_BIGENDIAN)
1119 have_virgl = display_opengl;
1122 g->conf.flags &= ~(1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED);
1125 if (virtio_gpu_virgl_enabled(g->conf)) {
1126 error_setg(&g->migration_blocker, "virgl is not yet migratable");
1127 migrate_add_blocker(g->migration_blocker, &local_err);
1129 error_propagate(errp, local_err);
1130 error_free(g->migration_blocker);
1135 g->config_size = sizeof(struct virtio_gpu_config);
1136 g->virtio_config.num_scanouts = g->conf.max_outputs;
1137 virtio_init(VIRTIO_DEVICE(g), "virtio-gpu", VIRTIO_ID_GPU,
1140 g->req_state[0].width = g->conf.xres;
1141 g->req_state[0].height = g->conf.yres;
1143 if (virtio_gpu_virgl_enabled(g->conf)) {
1144 /* use larger control queue in 3d mode */
1145 g->ctrl_vq = virtio_add_queue(vdev, 256, virtio_gpu_handle_ctrl_cb);
1146 g->cursor_vq = virtio_add_queue(vdev, 16, virtio_gpu_handle_cursor_cb);
1147 g->virtio_config.num_capsets = 1;
1149 g->ctrl_vq = virtio_add_queue(vdev, 64, virtio_gpu_handle_ctrl_cb);
1150 g->cursor_vq = virtio_add_queue(vdev, 16, virtio_gpu_handle_cursor_cb);
1153 g->ctrl_bh = qemu_bh_new(virtio_gpu_ctrl_bh, g);
1154 g->cursor_bh = qemu_bh_new(virtio_gpu_cursor_bh, g);
1155 QTAILQ_INIT(&g->reslist);
1156 QTAILQ_INIT(&g->cmdq);
1157 QTAILQ_INIT(&g->fenceq);
1159 g->enabled_output_bitmask = 1;
1162 for (i = 0; i < g->conf.max_outputs; i++) {
1164 graphic_console_init(DEVICE(g), i, &virtio_gpu_ops, g);
1166 dpy_gfx_replace_surface(g->scanout[i].con, NULL);
1171 static void virtio_gpu_device_unrealize(DeviceState *qdev, Error **errp)
1173 VirtIOGPU *g = VIRTIO_GPU(qdev);
1174 if (g->migration_blocker) {
1175 migrate_del_blocker(g->migration_blocker);
1176 error_free(g->migration_blocker);
1180 static void virtio_gpu_instance_init(Object *obj)
1184 static void virtio_gpu_reset(VirtIODevice *vdev)
1186 VirtIOGPU *g = VIRTIO_GPU(vdev);
1187 struct virtio_gpu_simple_resource *res, *tmp;
1192 QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) {
1193 virtio_gpu_resource_destroy(g, res);
1195 for (i = 0; i < g->conf.max_outputs; i++) {
1197 g->req_state[i].x = 0;
1198 g->req_state[i].y = 0;
1200 g->req_state[0].width = 1024;
1201 g->req_state[0].height = 768;
1203 g->req_state[i].width = 0;
1204 g->req_state[i].height = 0;
1207 g->scanout[i].resource_id = 0;
1208 g->scanout[i].width = 0;
1209 g->scanout[i].height = 0;
1210 g->scanout[i].x = 0;
1211 g->scanout[i].y = 0;
1212 g->scanout[i].ds = NULL;
1214 g->enabled_output_bitmask = 1;
1217 if (g->use_virgl_renderer) {
1218 virtio_gpu_virgl_reset(g);
1219 g->use_virgl_renderer = 0;
1225 * For historical reasons virtio_gpu does not adhere to virtio migration
1226 * scheme as described in doc/virtio-migration.txt, in a sense that no
1227 * save/load callback are provided to the core. Instead the device data
1228 * is saved/loaded after the core data.
1230 * Because of this we need a special vmsd.
1232 static const VMStateDescription vmstate_virtio_gpu = {
1233 .name = "virtio-gpu",
1234 .minimum_version_id = VIRTIO_GPU_VM_VERSION,
1235 .version_id = VIRTIO_GPU_VM_VERSION,
1236 .fields = (VMStateField[]) {
1237 VMSTATE_VIRTIO_DEVICE /* core */,
1239 .name = "virtio-gpu",
1240 .info = &(const VMStateInfo) {
1241 .name = "virtio-gpu",
1242 .get = virtio_gpu_load,
1243 .put = virtio_gpu_save,
1245 .flags = VMS_SINGLE,
1247 VMSTATE_END_OF_LIST()
1251 static Property virtio_gpu_properties[] = {
1252 DEFINE_PROP_UINT32("max_outputs", VirtIOGPU, conf.max_outputs, 1),
1253 DEFINE_PROP_SIZE("max_hostmem", VirtIOGPU, conf.max_hostmem,
1256 DEFINE_PROP_BIT("virgl", VirtIOGPU, conf.flags,
1257 VIRTIO_GPU_FLAG_VIRGL_ENABLED, true),
1258 DEFINE_PROP_BIT("stats", VirtIOGPU, conf.flags,
1259 VIRTIO_GPU_FLAG_STATS_ENABLED, false),
1261 DEFINE_PROP_UINT32("xres", VirtIOGPU, conf.xres, 1024),
1262 DEFINE_PROP_UINT32("yres", VirtIOGPU, conf.yres, 768),
1263 DEFINE_PROP_END_OF_LIST(),
1266 static void virtio_gpu_class_init(ObjectClass *klass, void *data)
1268 DeviceClass *dc = DEVICE_CLASS(klass);
1269 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1271 vdc->realize = virtio_gpu_device_realize;
1272 vdc->unrealize = virtio_gpu_device_unrealize;
1273 vdc->get_config = virtio_gpu_get_config;
1274 vdc->set_config = virtio_gpu_set_config;
1275 vdc->get_features = virtio_gpu_get_features;
1276 vdc->set_features = virtio_gpu_set_features;
1278 vdc->reset = virtio_gpu_reset;
1280 dc->props = virtio_gpu_properties;
1281 dc->vmsd = &vmstate_virtio_gpu;
1282 dc->hotpluggable = false;
1285 static const TypeInfo virtio_gpu_info = {
1286 .name = TYPE_VIRTIO_GPU,
1287 .parent = TYPE_VIRTIO_DEVICE,
1288 .instance_size = sizeof(VirtIOGPU),
1289 .instance_init = virtio_gpu_instance_init,
1290 .class_init = virtio_gpu_class_init,
1293 static void virtio_register_types(void)
1295 type_register_static(&virtio_gpu_info);
1298 type_init(virtio_register_types)
1300 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctrl_hdr) != 24);
1301 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_update_cursor) != 56);
1302 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_unref) != 32);
1303 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_create_2d) != 40);
1304 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_set_scanout) != 48);
1305 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_flush) != 48);
1306 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_transfer_to_host_2d) != 56);
1307 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_mem_entry) != 16);
1308 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_attach_backing) != 32);
1309 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_detach_backing) != 32);
1310 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resp_display_info) != 408);
1312 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_transfer_host_3d) != 72);
1313 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_create_3d) != 72);
1314 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctx_create) != 96);
1315 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctx_destroy) != 24);
1316 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctx_resource) != 32);
1317 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_cmd_submit) != 32);
1318 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_get_capset_info) != 32);
1319 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resp_capset_info) != 40);
1320 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_get_capset) != 32);
1321 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resp_capset) != 24);