4 * Copyright Red Hat, Inc. 2013-2014
7 * Dave Airlie <airlied@redhat.com>
8 * Gerd Hoffmann <kraxel@redhat.com>
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/units.h"
17 #include "system/cpus.h"
18 #include "ui/console.h"
21 #include "system/dma.h"
22 #include "system/system.h"
23 #include "hw/virtio/virtio.h"
24 #include "migration/qemu-file-types.h"
25 #include "hw/virtio/virtio-gpu.h"
26 #include "hw/virtio/virtio-gpu-bswap.h"
27 #include "hw/virtio/virtio-gpu-pixman.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/qdev-properties.h"
31 #include "qemu/memfd.h"
32 #include "qemu/module.h"
33 #include "qapi/error.h"
34 #include "qemu/error-report.h"
36 #define VIRTIO_GPU_VM_VERSION 1
38 static struct virtio_gpu_simple_resource
*
39 virtio_gpu_find_check_resource(VirtIOGPU
*g
, uint32_t resource_id
,
41 const char *caller
, uint32_t *error
);
43 static void virtio_gpu_reset_bh(void *opaque
);
45 void virtio_gpu_update_cursor_data(VirtIOGPU
*g
,
46 struct virtio_gpu_scanout
*s
,
49 struct virtio_gpu_simple_resource
*res
;
53 res
= virtio_gpu_find_check_resource(g
, resource_id
, false,
60 if (res
->blob_size
< (s
->current_cursor
->width
*
61 s
->current_cursor
->height
* 4)) {
66 if (pixman_image_get_width(res
->image
) != s
->current_cursor
->width
||
67 pixman_image_get_height(res
->image
) != s
->current_cursor
->height
) {
70 data
= pixman_image_get_data(res
->image
);
73 pixels
= s
->current_cursor
->width
* s
->current_cursor
->height
;
74 memcpy(s
->current_cursor
->data
, data
,
75 pixels
* sizeof(uint32_t));
78 static void update_cursor(VirtIOGPU
*g
, struct virtio_gpu_update_cursor
*cursor
)
80 struct virtio_gpu_scanout
*s
;
81 VirtIOGPUClass
*vgc
= VIRTIO_GPU_GET_CLASS(g
);
82 bool move
= cursor
->hdr
.type
== VIRTIO_GPU_CMD_MOVE_CURSOR
;
84 if (cursor
->pos
.scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
87 s
= &g
->parent_obj
.scanout
[cursor
->pos
.scanout_id
];
89 trace_virtio_gpu_update_cursor(cursor
->pos
.scanout_id
,
92 move
? "move" : "update",
96 if (!s
->current_cursor
) {
97 s
->current_cursor
= cursor_alloc(64, 64);
100 s
->current_cursor
->hot_x
= cursor
->hot_x
;
101 s
->current_cursor
->hot_y
= cursor
->hot_y
;
103 if (cursor
->resource_id
> 0) {
104 vgc
->update_cursor_data(g
, s
, cursor
->resource_id
);
106 dpy_cursor_define(s
->con
, s
->current_cursor
);
110 s
->cursor
.pos
.x
= cursor
->pos
.x
;
111 s
->cursor
.pos
.y
= cursor
->pos
.y
;
113 dpy_mouse_set(s
->con
, cursor
->pos
.x
, cursor
->pos
.y
, cursor
->resource_id
);
116 struct virtio_gpu_simple_resource
*
117 virtio_gpu_find_resource(VirtIOGPU
*g
, uint32_t resource_id
)
119 struct virtio_gpu_simple_resource
*res
;
121 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
122 if (res
->resource_id
== resource_id
) {
129 static struct virtio_gpu_simple_resource
*
130 virtio_gpu_find_check_resource(VirtIOGPU
*g
, uint32_t resource_id
,
131 bool require_backing
,
132 const char *caller
, uint32_t *error
)
134 struct virtio_gpu_simple_resource
*res
;
136 res
= virtio_gpu_find_resource(g
, resource_id
);
138 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid resource specified %d\n",
139 caller
, resource_id
);
141 *error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
146 if (require_backing
) {
147 if (!res
->iov
|| (!res
->image
&& !res
->blob
)) {
148 qemu_log_mask(LOG_GUEST_ERROR
, "%s: no backing storage %d\n",
149 caller
, resource_id
);
151 *error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
160 void virtio_gpu_ctrl_response(VirtIOGPU
*g
,
161 struct virtio_gpu_ctrl_command
*cmd
,
162 struct virtio_gpu_ctrl_hdr
*resp
,
167 if (cmd
->cmd_hdr
.flags
& VIRTIO_GPU_FLAG_FENCE
) {
168 resp
->flags
|= VIRTIO_GPU_FLAG_FENCE
;
169 resp
->fence_id
= cmd
->cmd_hdr
.fence_id
;
170 resp
->ctx_id
= cmd
->cmd_hdr
.ctx_id
;
172 virtio_gpu_ctrl_hdr_bswap(resp
);
173 s
= iov_from_buf(cmd
->elem
.in_sg
, cmd
->elem
.in_num
, 0, resp
, resp_len
);
175 qemu_log_mask(LOG_GUEST_ERROR
,
176 "%s: response size incorrect %zu vs %zu\n",
177 __func__
, s
, resp_len
);
179 virtqueue_push(cmd
->vq
, &cmd
->elem
, s
);
180 virtio_notify(VIRTIO_DEVICE(g
), cmd
->vq
);
181 cmd
->finished
= true;
184 void virtio_gpu_ctrl_response_nodata(VirtIOGPU
*g
,
185 struct virtio_gpu_ctrl_command
*cmd
,
186 enum virtio_gpu_ctrl_type type
)
188 struct virtio_gpu_ctrl_hdr resp
;
190 memset(&resp
, 0, sizeof(resp
));
192 virtio_gpu_ctrl_response(g
, cmd
, &resp
, sizeof(resp
));
195 void virtio_gpu_get_display_info(VirtIOGPU
*g
,
196 struct virtio_gpu_ctrl_command
*cmd
)
198 struct virtio_gpu_resp_display_info display_info
;
200 trace_virtio_gpu_cmd_get_display_info();
201 memset(&display_info
, 0, sizeof(display_info
));
202 display_info
.hdr
.type
= VIRTIO_GPU_RESP_OK_DISPLAY_INFO
;
203 virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g
), &display_info
);
204 virtio_gpu_ctrl_response(g
, cmd
, &display_info
.hdr
,
205 sizeof(display_info
));
208 void virtio_gpu_get_edid(VirtIOGPU
*g
,
209 struct virtio_gpu_ctrl_command
*cmd
)
211 struct virtio_gpu_resp_edid edid
;
212 struct virtio_gpu_cmd_get_edid get_edid
;
213 VirtIOGPUBase
*b
= VIRTIO_GPU_BASE(g
);
215 VIRTIO_GPU_FILL_CMD(get_edid
);
216 virtio_gpu_bswap_32(&get_edid
, sizeof(get_edid
));
218 if (get_edid
.scanout
>= b
->conf
.max_outputs
) {
219 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
223 trace_virtio_gpu_cmd_get_edid(get_edid
.scanout
);
224 memset(&edid
, 0, sizeof(edid
));
225 edid
.hdr
.type
= VIRTIO_GPU_RESP_OK_EDID
;
226 virtio_gpu_base_generate_edid(VIRTIO_GPU_BASE(g
), get_edid
.scanout
, &edid
);
227 virtio_gpu_ctrl_response(g
, cmd
, &edid
.hdr
, sizeof(edid
));
230 static uint32_t calc_image_hostmem(pixman_format_code_t pformat
,
231 uint32_t width
, uint32_t height
)
233 /* Copied from pixman/pixman-bits-image.c, skip integer overflow check.
234 * pixman_image_create_bits will fail in case it overflow.
237 int bpp
= PIXMAN_FORMAT_BPP(pformat
);
238 int stride
= ((width
* bpp
+ 0x1f) >> 5) * sizeof(uint32_t);
239 return height
* stride
;
242 static void virtio_gpu_resource_create_2d(VirtIOGPU
*g
,
243 struct virtio_gpu_ctrl_command
*cmd
)
245 pixman_format_code_t pformat
;
246 struct virtio_gpu_simple_resource
*res
;
247 struct virtio_gpu_resource_create_2d c2d
;
249 VIRTIO_GPU_FILL_CMD(c2d
);
250 virtio_gpu_bswap_32(&c2d
, sizeof(c2d
));
251 trace_virtio_gpu_cmd_res_create_2d(c2d
.resource_id
, c2d
.format
,
252 c2d
.width
, c2d
.height
);
254 if (c2d
.resource_id
== 0) {
255 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource id 0 is not allowed\n",
257 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
261 res
= virtio_gpu_find_resource(g
, c2d
.resource_id
);
263 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource already exists %d\n",
264 __func__
, c2d
.resource_id
);
265 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
269 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
271 res
->width
= c2d
.width
;
272 res
->height
= c2d
.height
;
273 res
->format
= c2d
.format
;
274 res
->resource_id
= c2d
.resource_id
;
276 pformat
= virtio_gpu_get_pixman_format(c2d
.format
);
278 qemu_log_mask(LOG_GUEST_ERROR
,
279 "%s: host couldn't handle guest format %d\n",
280 __func__
, c2d
.format
);
282 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
286 res
->hostmem
= calc_image_hostmem(pformat
, c2d
.width
, c2d
.height
);
287 if (res
->hostmem
+ g
->hostmem
< g
->conf_max_hostmem
) {
288 if (!qemu_pixman_image_new_shareable(
295 c2d
.height
? res
->hostmem
/ c2d
.height
: 0,
303 qemu_log_mask(LOG_GUEST_ERROR
,
304 "%s: resource creation failed %d %d %d\n",
305 __func__
, c2d
.resource_id
, c2d
.width
, c2d
.height
);
307 cmd
->error
= VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY
;
311 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
312 g
->hostmem
+= res
->hostmem
;
315 static void virtio_gpu_resource_create_blob(VirtIOGPU
*g
,
316 struct virtio_gpu_ctrl_command
*cmd
)
318 struct virtio_gpu_simple_resource
*res
;
319 struct virtio_gpu_resource_create_blob cblob
;
322 VIRTIO_GPU_FILL_CMD(cblob
);
323 virtio_gpu_create_blob_bswap(&cblob
);
324 trace_virtio_gpu_cmd_res_create_blob(cblob
.resource_id
, cblob
.size
);
326 if (cblob
.resource_id
== 0) {
327 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource id 0 is not allowed\n",
329 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
333 if (cblob
.blob_mem
!= VIRTIO_GPU_BLOB_MEM_GUEST
&&
334 cblob
.blob_flags
!= VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE
) {
335 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid memory type\n",
337 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
341 if (virtio_gpu_find_resource(g
, cblob
.resource_id
)) {
342 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource already exists %d\n",
343 __func__
, cblob
.resource_id
);
344 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
348 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
349 res
->resource_id
= cblob
.resource_id
;
350 res
->blob_size
= cblob
.size
;
352 ret
= virtio_gpu_create_mapping_iov(g
, cblob
.nr_entries
, sizeof(cblob
),
353 cmd
, &res
->addrs
, &res
->iov
,
356 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
361 virtio_gpu_init_udmabuf(res
);
362 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
365 void virtio_gpu_disable_scanout(VirtIOGPU
*g
, int scanout_id
)
367 struct virtio_gpu_scanout
*scanout
= &g
->parent_obj
.scanout
[scanout_id
];
368 struct virtio_gpu_simple_resource
*res
;
370 if (scanout
->resource_id
== 0) {
374 res
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
376 res
->scanout_bitmask
&= ~(1 << scanout_id
);
379 dpy_gfx_replace_surface(scanout
->con
, NULL
);
380 scanout
->resource_id
= 0;
386 static void virtio_gpu_resource_destroy(VirtIOGPU
*g
,
387 struct virtio_gpu_simple_resource
*res
,
392 if (res
->scanout_bitmask
) {
393 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
394 if (res
->scanout_bitmask
& (1 << i
)) {
395 virtio_gpu_disable_scanout(g
, i
);
400 qemu_pixman_image_unref(res
->image
);
401 virtio_gpu_cleanup_mapping(g
, res
);
402 QTAILQ_REMOVE(&g
->reslist
, res
, next
);
403 g
->hostmem
-= res
->hostmem
;
407 static void virtio_gpu_resource_unref(VirtIOGPU
*g
,
408 struct virtio_gpu_ctrl_command
*cmd
)
410 struct virtio_gpu_simple_resource
*res
;
411 struct virtio_gpu_resource_unref unref
;
413 VIRTIO_GPU_FILL_CMD(unref
);
414 virtio_gpu_bswap_32(&unref
, sizeof(unref
));
415 trace_virtio_gpu_cmd_res_unref(unref
.resource_id
);
417 res
= virtio_gpu_find_resource(g
, unref
.resource_id
);
419 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
420 __func__
, unref
.resource_id
);
421 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
425 * virtio_gpu_resource_destroy does not set any errors, so pass a NULL errp
428 virtio_gpu_resource_destroy(g
, res
, NULL
);
431 static void virtio_gpu_transfer_to_host_2d(VirtIOGPU
*g
,
432 struct virtio_gpu_ctrl_command
*cmd
)
434 struct virtio_gpu_simple_resource
*res
;
436 uint32_t src_offset
, dst_offset
, stride
;
437 pixman_format_code_t format
;
438 struct virtio_gpu_transfer_to_host_2d t2d
;
441 VIRTIO_GPU_FILL_CMD(t2d
);
442 virtio_gpu_t2d_bswap(&t2d
);
443 trace_virtio_gpu_cmd_res_xfer_toh_2d(t2d
.resource_id
);
445 res
= virtio_gpu_find_check_resource(g
, t2d
.resource_id
, true,
446 __func__
, &cmd
->error
);
447 if (!res
|| res
->blob
) {
451 if (t2d
.r
.x
> res
->width
||
452 t2d
.r
.y
> res
->height
||
453 t2d
.r
.width
> res
->width
||
454 t2d
.r
.height
> res
->height
||
455 t2d
.r
.x
+ t2d
.r
.width
> res
->width
||
456 t2d
.r
.y
+ t2d
.r
.height
> res
->height
) {
457 qemu_log_mask(LOG_GUEST_ERROR
, "%s: transfer bounds outside resource"
458 " bounds for resource %d: %d %d %d %d vs %d %d\n",
459 __func__
, t2d
.resource_id
, t2d
.r
.x
, t2d
.r
.y
,
460 t2d
.r
.width
, t2d
.r
.height
, res
->width
, res
->height
);
461 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
465 format
= pixman_image_get_format(res
->image
);
466 bpp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format
), 8);
467 stride
= pixman_image_get_stride(res
->image
);
468 img_data
= pixman_image_get_data(res
->image
);
470 if (t2d
.r
.x
|| t2d
.r
.width
!= pixman_image_get_width(res
->image
)) {
471 for (h
= 0; h
< t2d
.r
.height
; h
++) {
472 src_offset
= t2d
.offset
+ stride
* h
;
473 dst_offset
= (t2d
.r
.y
+ h
) * stride
+ (t2d
.r
.x
* bpp
);
475 iov_to_buf(res
->iov
, res
->iov_cnt
, src_offset
,
476 (uint8_t *)img_data
+ dst_offset
,
480 src_offset
= t2d
.offset
;
481 dst_offset
= t2d
.r
.y
* stride
+ t2d
.r
.x
* bpp
;
482 iov_to_buf(res
->iov
, res
->iov_cnt
, src_offset
,
483 (uint8_t *)img_data
+ dst_offset
,
484 stride
* t2d
.r
.height
);
488 static void virtio_gpu_resource_flush(VirtIOGPU
*g
,
489 struct virtio_gpu_ctrl_command
*cmd
)
491 struct virtio_gpu_simple_resource
*res
;
492 struct virtio_gpu_resource_flush rf
;
493 struct virtio_gpu_scanout
*scanout
;
495 bool within_bounds
= false;
496 bool update_submitted
= false;
499 VIRTIO_GPU_FILL_CMD(rf
);
500 virtio_gpu_bswap_32(&rf
, sizeof(rf
));
501 trace_virtio_gpu_cmd_res_flush(rf
.resource_id
,
502 rf
.r
.width
, rf
.r
.height
, rf
.r
.x
, rf
.r
.y
);
504 res
= virtio_gpu_find_check_resource(g
, rf
.resource_id
, false,
505 __func__
, &cmd
->error
);
511 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
512 scanout
= &g
->parent_obj
.scanout
[i
];
513 if (scanout
->resource_id
== res
->resource_id
&&
514 rf
.r
.x
< scanout
->x
+ scanout
->width
&&
515 rf
.r
.x
+ rf
.r
.width
>= scanout
->x
&&
516 rf
.r
.y
< scanout
->y
+ scanout
->height
&&
517 rf
.r
.y
+ rf
.r
.height
>= scanout
->y
) {
518 within_bounds
= true;
520 if (console_has_gl(scanout
->con
)) {
521 dpy_gl_update(scanout
->con
, 0, 0, scanout
->width
,
523 update_submitted
= true;
528 if (update_submitted
) {
531 if (!within_bounds
) {
532 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flush bounds outside scanouts"
533 " bounds for flush %d: %d %d %d %d\n",
534 __func__
, rf
.resource_id
, rf
.r
.x
, rf
.r
.y
,
535 rf
.r
.width
, rf
.r
.height
);
536 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
542 (rf
.r
.x
> res
->width
||
543 rf
.r
.y
> res
->height
||
544 rf
.r
.width
> res
->width
||
545 rf
.r
.height
> res
->height
||
546 rf
.r
.x
+ rf
.r
.width
> res
->width
||
547 rf
.r
.y
+ rf
.r
.height
> res
->height
)) {
548 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flush bounds outside resource"
549 " bounds for resource %d: %d %d %d %d vs %d %d\n",
550 __func__
, rf
.resource_id
, rf
.r
.x
, rf
.r
.y
,
551 rf
.r
.width
, rf
.r
.height
, res
->width
, res
->height
);
552 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
556 qemu_rect_init(&flush_rect
, rf
.r
.x
, rf
.r
.y
, rf
.r
.width
, rf
.r
.height
);
557 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
560 if (!(res
->scanout_bitmask
& (1 << i
))) {
563 scanout
= &g
->parent_obj
.scanout
[i
];
565 qemu_rect_init(&rect
, scanout
->x
, scanout
->y
,
566 scanout
->width
, scanout
->height
);
568 /* work out the area we need to update for each console */
569 if (qemu_rect_intersect(&flush_rect
, &rect
, &rect
)) {
570 qemu_rect_translate(&rect
, -scanout
->x
, -scanout
->y
);
571 dpy_gfx_update(g
->parent_obj
.scanout
[i
].con
,
572 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
577 static void virtio_unref_resource(pixman_image_t
*image
, void *data
)
579 pixman_image_unref(data
);
582 void virtio_gpu_update_scanout(VirtIOGPU
*g
,
584 struct virtio_gpu_simple_resource
*res
,
585 struct virtio_gpu_framebuffer
*fb
,
586 struct virtio_gpu_rect
*r
)
588 struct virtio_gpu_simple_resource
*ores
;
589 struct virtio_gpu_scanout
*scanout
;
591 scanout
= &g
->parent_obj
.scanout
[scanout_id
];
592 ores
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
594 ores
->scanout_bitmask
&= ~(1 << scanout_id
);
597 res
->scanout_bitmask
|= (1 << scanout_id
);
598 scanout
->resource_id
= res
->resource_id
;
601 scanout
->width
= r
->width
;
602 scanout
->height
= r
->height
;
606 static bool virtio_gpu_do_set_scanout(VirtIOGPU
*g
,
608 struct virtio_gpu_framebuffer
*fb
,
609 struct virtio_gpu_simple_resource
*res
,
610 struct virtio_gpu_rect
*r
,
613 struct virtio_gpu_scanout
*scanout
;
616 scanout
= &g
->parent_obj
.scanout
[scanout_id
];
618 if (r
->x
> fb
->width
||
622 r
->width
> fb
->width
||
623 r
->height
> fb
->height
||
624 r
->x
+ r
->width
> fb
->width
||
625 r
->y
+ r
->height
> fb
->height
) {
626 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout %d bounds for"
627 " resource %d, rect (%d,%d)+%d,%d, fb %d %d\n",
628 __func__
, scanout_id
, res
->resource_id
,
629 r
->x
, r
->y
, r
->width
, r
->height
,
630 fb
->width
, fb
->height
);
631 *error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
635 g
->parent_obj
.enable
= 1;
638 if (console_has_gl(scanout
->con
)) {
639 if (!virtio_gpu_update_dmabuf(g
, scanout_id
, res
, fb
, r
)) {
640 virtio_gpu_update_scanout(g
, scanout_id
, res
, fb
, r
);
642 *error
= VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY
;
650 data
= (uint8_t *)pixman_image_get_data(res
->image
);
653 /* create a surface for this scanout */
654 if ((res
->blob
&& !console_has_gl(scanout
->con
)) ||
656 surface_data(scanout
->ds
) != data
+ fb
->offset
||
657 scanout
->width
!= r
->width
||
658 scanout
->height
!= r
->height
) {
659 pixman_image_t
*rect
;
660 void *ptr
= data
+ fb
->offset
;
661 rect
= pixman_image_create_bits(fb
->format
, r
->width
, r
->height
,
665 pixman_image_ref(res
->image
);
666 pixman_image_set_destroy_function(rect
, virtio_unref_resource
,
670 /* realloc the surface ptr */
671 scanout
->ds
= qemu_create_displaysurface_pixman(rect
);
672 qemu_displaysurface_set_share_handle(scanout
->ds
, res
->share_handle
, fb
->offset
);
674 pixman_image_unref(rect
);
675 dpy_gfx_replace_surface(g
->parent_obj
.scanout
[scanout_id
].con
,
679 virtio_gpu_update_scanout(g
, scanout_id
, res
, fb
, r
);
683 static void virtio_gpu_set_scanout(VirtIOGPU
*g
,
684 struct virtio_gpu_ctrl_command
*cmd
)
686 struct virtio_gpu_simple_resource
*res
;
687 struct virtio_gpu_framebuffer fb
= { 0 };
688 struct virtio_gpu_set_scanout ss
;
690 VIRTIO_GPU_FILL_CMD(ss
);
691 virtio_gpu_bswap_32(&ss
, sizeof(ss
));
692 trace_virtio_gpu_cmd_set_scanout(ss
.scanout_id
, ss
.resource_id
,
693 ss
.r
.width
, ss
.r
.height
, ss
.r
.x
, ss
.r
.y
);
695 if (ss
.scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
696 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout id specified %d",
697 __func__
, ss
.scanout_id
);
698 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID
;
702 if (ss
.resource_id
== 0) {
703 virtio_gpu_disable_scanout(g
, ss
.scanout_id
);
707 res
= virtio_gpu_find_check_resource(g
, ss
.resource_id
, true,
708 __func__
, &cmd
->error
);
713 fb
.format
= pixman_image_get_format(res
->image
);
714 fb
.bytes_pp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb
.format
), 8);
715 fb
.width
= pixman_image_get_width(res
->image
);
716 fb
.height
= pixman_image_get_height(res
->image
);
717 fb
.stride
= pixman_image_get_stride(res
->image
);
718 fb
.offset
= ss
.r
.x
* fb
.bytes_pp
+ ss
.r
.y
* fb
.stride
;
720 virtio_gpu_do_set_scanout(g
, ss
.scanout_id
,
721 &fb
, res
, &ss
.r
, &cmd
->error
);
724 bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer
*fb
,
725 struct virtio_gpu_set_scanout_blob
*ss
,
730 fb
->format
= virtio_gpu_get_pixman_format(ss
->format
);
732 qemu_log_mask(LOG_GUEST_ERROR
,
733 "%s: host couldn't handle guest format %d\n",
734 __func__
, ss
->format
);
738 fb
->bytes_pp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb
->format
), 8);
739 fb
->width
= ss
->width
;
740 fb
->height
= ss
->height
;
741 fb
->stride
= ss
->strides
[0];
742 fb
->offset
= ss
->offsets
[0] + ss
->r
.x
* fb
->bytes_pp
+ ss
->r
.y
* fb
->stride
;
745 fbend
+= (uint64_t) fb
->stride
* ss
->r
.height
;
747 if (fbend
> blob_size
) {
748 qemu_log_mask(LOG_GUEST_ERROR
,
749 "%s: fb end out of range\n",
759 static void virtio_gpu_set_scanout_blob(VirtIOGPU
*g
,
760 struct virtio_gpu_ctrl_command
*cmd
)
762 struct virtio_gpu_simple_resource
*res
;
763 struct virtio_gpu_framebuffer fb
= { 0 };
764 struct virtio_gpu_set_scanout_blob ss
;
766 VIRTIO_GPU_FILL_CMD(ss
);
767 virtio_gpu_scanout_blob_bswap(&ss
);
768 trace_virtio_gpu_cmd_set_scanout_blob(ss
.scanout_id
, ss
.resource_id
,
769 ss
.r
.width
, ss
.r
.height
, ss
.r
.x
,
772 if (ss
.scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
773 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout id specified %d",
774 __func__
, ss
.scanout_id
);
775 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID
;
779 if (ss
.resource_id
== 0) {
780 virtio_gpu_disable_scanout(g
, ss
.scanout_id
);
784 res
= virtio_gpu_find_check_resource(g
, ss
.resource_id
, true,
785 __func__
, &cmd
->error
);
790 if (!virtio_gpu_scanout_blob_to_fb(&fb
, &ss
, res
->blob_size
)) {
791 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
795 virtio_gpu_do_set_scanout(g
, ss
.scanout_id
,
796 &fb
, res
, &ss
.r
, &cmd
->error
);
799 int virtio_gpu_create_mapping_iov(VirtIOGPU
*g
,
800 uint32_t nr_entries
, uint32_t offset
,
801 struct virtio_gpu_ctrl_command
*cmd
,
802 uint64_t **addr
, struct iovec
**iov
,
805 struct virtio_gpu_mem_entry
*ents
;
809 if (nr_entries
> 16384) {
810 qemu_log_mask(LOG_GUEST_ERROR
,
811 "%s: nr_entries is too big (%d > 16384)\n",
812 __func__
, nr_entries
);
816 esize
= sizeof(*ents
) * nr_entries
;
817 ents
= g_malloc(esize
);
818 s
= iov_to_buf(cmd
->elem
.out_sg
, cmd
->elem
.out_num
,
819 offset
, ents
, esize
);
821 qemu_log_mask(LOG_GUEST_ERROR
,
822 "%s: command data size incorrect %zu vs %zu\n",
832 for (e
= 0, v
= 0; e
< nr_entries
; e
++) {
833 uint64_t a
= le64_to_cpu(ents
[e
].addr
);
834 uint32_t l
= le32_to_cpu(ents
[e
].length
);
840 map
= dma_memory_map(VIRTIO_DEVICE(g
)->dma_as
, a
, &len
,
841 DMA_DIRECTION_TO_DEVICE
,
842 MEMTXATTRS_UNSPECIFIED
);
844 qemu_log_mask(LOG_GUEST_ERROR
, "%s: failed to map MMIO memory for"
845 " element %d\n", __func__
, e
);
846 virtio_gpu_cleanup_mapping_iov(g
, *iov
, v
);
857 *iov
= g_renew(struct iovec
, *iov
, v
+ 16);
859 *addr
= g_renew(uint64_t, *addr
, v
+ 16);
862 (*iov
)[v
].iov_base
= map
;
863 (*iov
)[v
].iov_len
= len
;
879 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU
*g
,
880 struct iovec
*iov
, uint32_t count
)
884 for (i
= 0; i
< count
; i
++) {
885 dma_memory_unmap(VIRTIO_DEVICE(g
)->dma_as
,
886 iov
[i
].iov_base
, iov
[i
].iov_len
,
887 DMA_DIRECTION_TO_DEVICE
,
893 void virtio_gpu_cleanup_mapping(VirtIOGPU
*g
,
894 struct virtio_gpu_simple_resource
*res
)
896 virtio_gpu_cleanup_mapping_iov(g
, res
->iov
, res
->iov_cnt
);
903 virtio_gpu_fini_udmabuf(res
);
908 virtio_gpu_resource_attach_backing(VirtIOGPU
*g
,
909 struct virtio_gpu_ctrl_command
*cmd
)
911 struct virtio_gpu_simple_resource
*res
;
912 struct virtio_gpu_resource_attach_backing ab
;
915 VIRTIO_GPU_FILL_CMD(ab
);
916 virtio_gpu_bswap_32(&ab
, sizeof(ab
));
917 trace_virtio_gpu_cmd_res_back_attach(ab
.resource_id
);
919 res
= virtio_gpu_find_resource(g
, ab
.resource_id
);
921 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
922 __func__
, ab
.resource_id
);
923 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
928 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
932 ret
= virtio_gpu_create_mapping_iov(g
, ab
.nr_entries
, sizeof(ab
), cmd
,
933 &res
->addrs
, &res
->iov
, &res
->iov_cnt
);
935 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
941 virtio_gpu_resource_detach_backing(VirtIOGPU
*g
,
942 struct virtio_gpu_ctrl_command
*cmd
)
944 struct virtio_gpu_simple_resource
*res
;
945 struct virtio_gpu_resource_detach_backing detach
;
947 VIRTIO_GPU_FILL_CMD(detach
);
948 virtio_gpu_bswap_32(&detach
, sizeof(detach
));
949 trace_virtio_gpu_cmd_res_back_detach(detach
.resource_id
);
951 res
= virtio_gpu_find_check_resource(g
, detach
.resource_id
, true,
952 __func__
, &cmd
->error
);
956 virtio_gpu_cleanup_mapping(g
, res
);
959 void virtio_gpu_simple_process_cmd(VirtIOGPU
*g
,
960 struct virtio_gpu_ctrl_command
*cmd
)
962 VIRTIO_GPU_FILL_CMD(cmd
->cmd_hdr
);
963 virtio_gpu_ctrl_hdr_bswap(&cmd
->cmd_hdr
);
965 switch (cmd
->cmd_hdr
.type
) {
966 case VIRTIO_GPU_CMD_GET_DISPLAY_INFO
:
967 virtio_gpu_get_display_info(g
, cmd
);
969 case VIRTIO_GPU_CMD_GET_EDID
:
970 virtio_gpu_get_edid(g
, cmd
);
972 case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D
:
973 virtio_gpu_resource_create_2d(g
, cmd
);
975 case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB
:
976 if (!virtio_gpu_blob_enabled(g
->parent_obj
.conf
)) {
977 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
980 virtio_gpu_resource_create_blob(g
, cmd
);
982 case VIRTIO_GPU_CMD_RESOURCE_UNREF
:
983 virtio_gpu_resource_unref(g
, cmd
);
985 case VIRTIO_GPU_CMD_RESOURCE_FLUSH
:
986 virtio_gpu_resource_flush(g
, cmd
);
988 case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D
:
989 virtio_gpu_transfer_to_host_2d(g
, cmd
);
991 case VIRTIO_GPU_CMD_SET_SCANOUT
:
992 virtio_gpu_set_scanout(g
, cmd
);
994 case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB
:
995 if (!virtio_gpu_blob_enabled(g
->parent_obj
.conf
)) {
996 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
999 virtio_gpu_set_scanout_blob(g
, cmd
);
1001 case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
:
1002 virtio_gpu_resource_attach_backing(g
, cmd
);
1004 case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING
:
1005 virtio_gpu_resource_detach_backing(g
, cmd
);
1008 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
1011 if (!cmd
->finished
) {
1012 if (!g
->parent_obj
.renderer_blocked
) {
1013 virtio_gpu_ctrl_response_nodata(g
, cmd
, cmd
->error
? cmd
->error
:
1014 VIRTIO_GPU_RESP_OK_NODATA
);
1019 static void virtio_gpu_handle_ctrl_cb(VirtIODevice
*vdev
, VirtQueue
*vq
)
1021 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1022 qemu_bh_schedule(g
->ctrl_bh
);
1025 static void virtio_gpu_handle_cursor_cb(VirtIODevice
*vdev
, VirtQueue
*vq
)
1027 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1028 qemu_bh_schedule(g
->cursor_bh
);
1031 void virtio_gpu_process_cmdq(VirtIOGPU
*g
)
1033 struct virtio_gpu_ctrl_command
*cmd
;
1034 VirtIOGPUClass
*vgc
= VIRTIO_GPU_GET_CLASS(g
);
1036 if (g
->processing_cmdq
) {
1039 g
->processing_cmdq
= true;
1040 while (!QTAILQ_EMPTY(&g
->cmdq
)) {
1041 cmd
= QTAILQ_FIRST(&g
->cmdq
);
1043 if (g
->parent_obj
.renderer_blocked
) {
1047 /* process command */
1048 vgc
->process_cmd(g
, cmd
);
1050 /* command suspended */
1051 if (!cmd
->finished
&& !(cmd
->cmd_hdr
.flags
& VIRTIO_GPU_FLAG_FENCE
)) {
1052 trace_virtio_gpu_cmd_suspended(cmd
->cmd_hdr
.type
);
1056 QTAILQ_REMOVE(&g
->cmdq
, cmd
, next
);
1057 if (virtio_gpu_stats_enabled(g
->parent_obj
.conf
)) {
1058 g
->stats
.requests
++;
1061 if (!cmd
->finished
) {
1062 QTAILQ_INSERT_TAIL(&g
->fenceq
, cmd
, next
);
1064 if (virtio_gpu_stats_enabled(g
->parent_obj
.conf
)) {
1065 if (g
->stats
.max_inflight
< g
->inflight
) {
1066 g
->stats
.max_inflight
= g
->inflight
;
1068 trace_virtio_gpu_inc_inflight_fences(g
->inflight
);
1074 g
->processing_cmdq
= false;
1077 static void virtio_gpu_process_fenceq(VirtIOGPU
*g
)
1079 struct virtio_gpu_ctrl_command
*cmd
, *tmp
;
1081 QTAILQ_FOREACH_SAFE(cmd
, &g
->fenceq
, next
, tmp
) {
1082 trace_virtio_gpu_fence_resp(cmd
->cmd_hdr
.fence_id
);
1083 virtio_gpu_ctrl_response_nodata(g
, cmd
, VIRTIO_GPU_RESP_OK_NODATA
);
1084 QTAILQ_REMOVE(&g
->fenceq
, cmd
, next
);
1087 if (virtio_gpu_stats_enabled(g
->parent_obj
.conf
)) {
1088 trace_virtio_gpu_dec_inflight_fences(g
->inflight
);
1093 static void virtio_gpu_handle_gl_flushed(VirtIOGPUBase
*b
)
1095 VirtIOGPU
*g
= container_of(b
, VirtIOGPU
, parent_obj
);
1097 virtio_gpu_process_fenceq(g
);
1098 virtio_gpu_process_cmdq(g
);
1101 static void virtio_gpu_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
1103 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1104 struct virtio_gpu_ctrl_command
*cmd
;
1106 if (!virtio_queue_ready(vq
)) {
1110 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
1114 cmd
->finished
= false;
1115 QTAILQ_INSERT_TAIL(&g
->cmdq
, cmd
, next
);
1116 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
1119 virtio_gpu_process_cmdq(g
);
1122 static void virtio_gpu_ctrl_bh(void *opaque
)
1124 VirtIOGPU
*g
= opaque
;
1125 VirtIOGPUClass
*vgc
= VIRTIO_GPU_GET_CLASS(g
);
1127 vgc
->handle_ctrl(VIRTIO_DEVICE(g
), g
->ctrl_vq
);
1130 static void virtio_gpu_handle_cursor(VirtIODevice
*vdev
, VirtQueue
*vq
)
1132 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1133 VirtQueueElement
*elem
;
1135 struct virtio_gpu_update_cursor cursor_info
;
1137 if (!virtio_queue_ready(vq
)) {
1141 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
1146 s
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
1147 &cursor_info
, sizeof(cursor_info
));
1148 if (s
!= sizeof(cursor_info
)) {
1149 qemu_log_mask(LOG_GUEST_ERROR
,
1150 "%s: cursor size incorrect %zu vs %zu\n",
1151 __func__
, s
, sizeof(cursor_info
));
1153 virtio_gpu_bswap_32(&cursor_info
, sizeof(cursor_info
));
1154 update_cursor(g
, &cursor_info
);
1156 virtqueue_push(vq
, elem
, 0);
1157 virtio_notify(vdev
, vq
);
1162 static void virtio_gpu_cursor_bh(void *opaque
)
1164 VirtIOGPU
*g
= opaque
;
1165 virtio_gpu_handle_cursor(&g
->parent_obj
.parent_obj
, g
->cursor_vq
);
1168 static bool scanout_vmstate_after_v2(void *opaque
, int version
)
1170 struct VirtIOGPUBase
*base
= container_of(opaque
, VirtIOGPUBase
, scanout
);
1171 struct VirtIOGPU
*gpu
= container_of(base
, VirtIOGPU
, parent_obj
);
1173 return gpu
->scanout_vmstate_version
>= 2;
1176 static const VMStateDescription vmstate_virtio_gpu_scanout
= {
1177 .name
= "virtio-gpu-one-scanout",
1179 .fields
= (const VMStateField
[]) {
1180 VMSTATE_UINT32(resource_id
, struct virtio_gpu_scanout
),
1181 VMSTATE_UINT32(width
, struct virtio_gpu_scanout
),
1182 VMSTATE_UINT32(height
, struct virtio_gpu_scanout
),
1183 VMSTATE_INT32(x
, struct virtio_gpu_scanout
),
1184 VMSTATE_INT32(y
, struct virtio_gpu_scanout
),
1185 VMSTATE_UINT32(cursor
.resource_id
, struct virtio_gpu_scanout
),
1186 VMSTATE_UINT32(cursor
.hot_x
, struct virtio_gpu_scanout
),
1187 VMSTATE_UINT32(cursor
.hot_y
, struct virtio_gpu_scanout
),
1188 VMSTATE_UINT32(cursor
.pos
.x
, struct virtio_gpu_scanout
),
1189 VMSTATE_UINT32(cursor
.pos
.y
, struct virtio_gpu_scanout
),
1190 VMSTATE_UINT32_TEST(fb
.format
, struct virtio_gpu_scanout
,
1191 scanout_vmstate_after_v2
),
1192 VMSTATE_UINT32_TEST(fb
.bytes_pp
, struct virtio_gpu_scanout
,
1193 scanout_vmstate_after_v2
),
1194 VMSTATE_UINT32_TEST(fb
.width
, struct virtio_gpu_scanout
,
1195 scanout_vmstate_after_v2
),
1196 VMSTATE_UINT32_TEST(fb
.height
, struct virtio_gpu_scanout
,
1197 scanout_vmstate_after_v2
),
1198 VMSTATE_UINT32_TEST(fb
.stride
, struct virtio_gpu_scanout
,
1199 scanout_vmstate_after_v2
),
1200 VMSTATE_UINT32_TEST(fb
.offset
, struct virtio_gpu_scanout
,
1201 scanout_vmstate_after_v2
),
1202 VMSTATE_END_OF_LIST()
1206 static const VMStateDescription vmstate_virtio_gpu_scanouts
= {
1207 .name
= "virtio-gpu-scanouts",
1209 .fields
= (const VMStateField
[]) {
1210 VMSTATE_INT32(parent_obj
.enable
, struct VirtIOGPU
),
1211 VMSTATE_UINT32_EQUAL(parent_obj
.conf
.max_outputs
,
1212 struct VirtIOGPU
, NULL
),
1213 VMSTATE_STRUCT_VARRAY_UINT32(parent_obj
.scanout
, struct VirtIOGPU
,
1214 parent_obj
.conf
.max_outputs
, 1,
1215 vmstate_virtio_gpu_scanout
,
1216 struct virtio_gpu_scanout
),
1217 VMSTATE_END_OF_LIST()
1221 static int virtio_gpu_save(QEMUFile
*f
, void *opaque
, size_t size
,
1222 const VMStateField
*field
, JSONWriter
*vmdesc
)
1224 VirtIOGPU
*g
= opaque
;
1225 struct virtio_gpu_simple_resource
*res
;
1228 /* in 2d mode we should never find unprocessed commands here */
1229 assert(QTAILQ_EMPTY(&g
->cmdq
));
1231 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
1232 if (res
->blob_size
) {
1235 qemu_put_be32(f
, res
->resource_id
);
1236 qemu_put_be32(f
, res
->width
);
1237 qemu_put_be32(f
, res
->height
);
1238 qemu_put_be32(f
, res
->format
);
1239 qemu_put_be32(f
, res
->iov_cnt
);
1240 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1241 qemu_put_be64(f
, res
->addrs
[i
]);
1242 qemu_put_be32(f
, res
->iov
[i
].iov_len
);
1244 qemu_put_buffer(f
, (void *)pixman_image_get_data(res
->image
),
1245 pixman_image_get_stride(res
->image
) * res
->height
);
1247 qemu_put_be32(f
, 0); /* end of list */
1249 return vmstate_save_state(f
, &vmstate_virtio_gpu_scanouts
, g
, NULL
);
1252 static bool virtio_gpu_load_restore_mapping(VirtIOGPU
*g
,
1253 struct virtio_gpu_simple_resource
*res
)
1257 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1258 hwaddr len
= res
->iov
[i
].iov_len
;
1259 res
->iov
[i
].iov_base
=
1260 dma_memory_map(VIRTIO_DEVICE(g
)->dma_as
, res
->addrs
[i
], &len
,
1261 DMA_DIRECTION_TO_DEVICE
, MEMTXATTRS_UNSPECIFIED
);
1263 if (!res
->iov
[i
].iov_base
|| len
!= res
->iov
[i
].iov_len
) {
1264 /* Clean up the half-a-mapping we just created... */
1265 if (res
->iov
[i
].iov_base
) {
1266 dma_memory_unmap(VIRTIO_DEVICE(g
)->dma_as
, res
->iov
[i
].iov_base
,
1267 len
, DMA_DIRECTION_TO_DEVICE
, 0);
1269 /* ...and the mappings for previous loop iterations */
1271 virtio_gpu_cleanup_mapping(g
, res
);
1276 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
1277 g
->hostmem
+= res
->hostmem
;
1281 static int virtio_gpu_load(QEMUFile
*f
, void *opaque
, size_t size
,
1282 const VMStateField
*field
)
1284 VirtIOGPU
*g
= opaque
;
1285 struct virtio_gpu_simple_resource
*res
;
1286 uint32_t resource_id
, pformat
;
1291 resource_id
= qemu_get_be32(f
);
1292 while (resource_id
!= 0) {
1293 res
= virtio_gpu_find_resource(g
, resource_id
);
1298 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
1299 res
->resource_id
= resource_id
;
1300 res
->width
= qemu_get_be32(f
);
1301 res
->height
= qemu_get_be32(f
);
1302 res
->format
= qemu_get_be32(f
);
1303 res
->iov_cnt
= qemu_get_be32(f
);
1306 pformat
= virtio_gpu_get_pixman_format(res
->format
);
1312 res
->hostmem
= calc_image_hostmem(pformat
, res
->width
, res
->height
);
1313 if (!qemu_pixman_image_new_shareable(&res
->image
,
1319 res
->height
? res
->hostmem
/ res
->height
: 0,
1325 res
->addrs
= g_new(uint64_t, res
->iov_cnt
);
1326 res
->iov
= g_new(struct iovec
, res
->iov_cnt
);
1329 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1330 res
->addrs
[i
] = qemu_get_be64(f
);
1331 res
->iov
[i
].iov_len
= qemu_get_be32(f
);
1333 qemu_get_buffer(f
, (void *)pixman_image_get_data(res
->image
),
1334 pixman_image_get_stride(res
->image
) * res
->height
);
1336 if (!virtio_gpu_load_restore_mapping(g
, res
)) {
1337 pixman_image_unref(res
->image
);
1342 resource_id
= qemu_get_be32(f
);
1345 /* load & apply scanout state */
1346 vmstate_load_state(f
, &vmstate_virtio_gpu_scanouts
, g
, 1);
1351 static int virtio_gpu_blob_save(QEMUFile
*f
, void *opaque
, size_t size
,
1352 const VMStateField
*field
, JSONWriter
*vmdesc
)
1354 VirtIOGPU
*g
= opaque
;
1355 struct virtio_gpu_simple_resource
*res
;
1358 /* in 2d mode we should never find unprocessed commands here */
1359 assert(QTAILQ_EMPTY(&g
->cmdq
));
1361 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
1362 if (!res
->blob_size
) {
1365 assert(!res
->image
);
1366 qemu_put_be32(f
, res
->resource_id
);
1367 qemu_put_be32(f
, res
->blob_size
);
1368 qemu_put_be32(f
, res
->iov_cnt
);
1369 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1370 qemu_put_be64(f
, res
->addrs
[i
]);
1371 qemu_put_be32(f
, res
->iov
[i
].iov_len
);
1374 qemu_put_be32(f
, 0); /* end of list */
1379 static int virtio_gpu_blob_load(QEMUFile
*f
, void *opaque
, size_t size
,
1380 const VMStateField
*field
)
1382 VirtIOGPU
*g
= opaque
;
1383 struct virtio_gpu_simple_resource
*res
;
1384 uint32_t resource_id
;
1387 resource_id
= qemu_get_be32(f
);
1388 while (resource_id
!= 0) {
1389 res
= virtio_gpu_find_resource(g
, resource_id
);
1394 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
1395 res
->resource_id
= resource_id
;
1396 res
->blob_size
= qemu_get_be32(f
);
1397 res
->iov_cnt
= qemu_get_be32(f
);
1398 res
->addrs
= g_new(uint64_t, res
->iov_cnt
);
1399 res
->iov
= g_new(struct iovec
, res
->iov_cnt
);
1402 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1403 res
->addrs
[i
] = qemu_get_be64(f
);
1404 res
->iov
[i
].iov_len
= qemu_get_be32(f
);
1407 if (!virtio_gpu_load_restore_mapping(g
, res
)) {
1412 virtio_gpu_init_udmabuf(res
);
1414 resource_id
= qemu_get_be32(f
);
1420 static int virtio_gpu_post_load(void *opaque
, int version_id
)
1422 VirtIOGPU
*g
= opaque
;
1423 struct virtio_gpu_scanout
*scanout
;
1424 struct virtio_gpu_simple_resource
*res
;
1427 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
1428 scanout
= &g
->parent_obj
.scanout
[i
];
1429 if (!scanout
->resource_id
) {
1433 res
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
1438 if (scanout
->fb
.format
!= 0) {
1440 struct virtio_gpu_rect r
= {
1443 .width
= scanout
->width
,
1444 .height
= scanout
->height
1447 if (!virtio_gpu_do_set_scanout(g
, i
, &scanout
->fb
, res
, &r
, &error
)) {
1451 /* legacy v1 migration support */
1455 scanout
->ds
= qemu_create_displaysurface_pixman(res
->image
);
1456 qemu_displaysurface_set_share_handle(scanout
->ds
, res
->share_handle
, 0);
1457 dpy_gfx_replace_surface(scanout
->con
, scanout
->ds
);
1460 dpy_gfx_update_full(scanout
->con
);
1461 if (scanout
->cursor
.resource_id
) {
1462 update_cursor(g
, &scanout
->cursor
);
1464 res
->scanout_bitmask
|= (1 << i
);
1470 void virtio_gpu_device_realize(DeviceState
*qdev
, Error
**errp
)
1472 VirtIODevice
*vdev
= VIRTIO_DEVICE(qdev
);
1473 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
1475 if (virtio_gpu_blob_enabled(g
->parent_obj
.conf
)) {
1476 if (!virtio_gpu_rutabaga_enabled(g
->parent_obj
.conf
) &&
1477 !virtio_gpu_virgl_enabled(g
->parent_obj
.conf
) &&
1478 !virtio_gpu_have_udmabuf()) {
1479 error_setg(errp
, "need rutabaga or udmabuf for blob resources");
1483 #ifdef VIRGL_VERSION_MAJOR
1484 #if VIRGL_VERSION_MAJOR < 1
1485 if (virtio_gpu_virgl_enabled(g
->parent_obj
.conf
)) {
1486 error_setg(errp
, "old virglrenderer, blob resources unsupported");
1493 if (virtio_gpu_venus_enabled(g
->parent_obj
.conf
)) {
1494 #ifdef VIRGL_VERSION_MAJOR
1495 #if VIRGL_VERSION_MAJOR >= 1
1496 if (!virtio_gpu_blob_enabled(g
->parent_obj
.conf
) ||
1497 !virtio_gpu_hostmem_enabled(g
->parent_obj
.conf
)) {
1498 error_setg(errp
, "venus requires enabled blob and hostmem options");
1502 error_setg(errp
, "old virglrenderer, venus unsupported");
1508 if (!virtio_gpu_base_device_realize(qdev
,
1509 virtio_gpu_handle_ctrl_cb
,
1510 virtio_gpu_handle_cursor_cb
,
1515 g
->ctrl_vq
= virtio_get_queue(vdev
, 0);
1516 g
->cursor_vq
= virtio_get_queue(vdev
, 1);
1517 g
->ctrl_bh
= virtio_bh_new_guarded(qdev
, virtio_gpu_ctrl_bh
, g
);
1518 g
->cursor_bh
= virtio_bh_new_guarded(qdev
, virtio_gpu_cursor_bh
, g
);
1519 g
->reset_bh
= qemu_bh_new(virtio_gpu_reset_bh
, g
);
1520 qemu_cond_init(&g
->reset_cond
);
1521 QTAILQ_INIT(&g
->reslist
);
1522 QTAILQ_INIT(&g
->cmdq
);
1523 QTAILQ_INIT(&g
->fenceq
);
1526 static void virtio_gpu_device_unrealize(DeviceState
*qdev
)
1528 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
1530 g_clear_pointer(&g
->ctrl_bh
, qemu_bh_delete
);
1531 g_clear_pointer(&g
->cursor_bh
, qemu_bh_delete
);
1532 g_clear_pointer(&g
->reset_bh
, qemu_bh_delete
);
1533 qemu_cond_destroy(&g
->reset_cond
);
1534 virtio_gpu_base_device_unrealize(qdev
);
1537 static void virtio_gpu_reset_bh(void *opaque
)
1539 VirtIOGPU
*g
= VIRTIO_GPU(opaque
);
1540 VirtIOGPUClass
*vgc
= VIRTIO_GPU_GET_CLASS(g
);
1541 struct virtio_gpu_simple_resource
*res
, *tmp
;
1542 uint32_t resource_id
;
1543 Error
*local_err
= NULL
;
1546 QTAILQ_FOREACH_SAFE(res
, &g
->reslist
, next
, tmp
) {
1547 resource_id
= res
->resource_id
;
1548 vgc
->resource_destroy(g
, res
, &local_err
);
1550 error_append_hint(&local_err
, "%s: %s resource_destroy"
1551 "for resource_id = %"PRIu32
" failed.\n",
1552 __func__
, object_get_typename(OBJECT(g
)),
1554 /* error_report_err frees the error object for us */
1555 error_report_err(local_err
);
1560 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
1561 dpy_gfx_replace_surface(g
->parent_obj
.scanout
[i
].con
, NULL
);
1564 g
->reset_finished
= true;
1565 qemu_cond_signal(&g
->reset_cond
);
1568 void virtio_gpu_reset(VirtIODevice
*vdev
)
1570 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1571 struct virtio_gpu_ctrl_command
*cmd
;
1573 if (qemu_in_vcpu_thread()) {
1574 g
->reset_finished
= false;
1575 qemu_bh_schedule(g
->reset_bh
);
1576 while (!g
->reset_finished
) {
1577 qemu_cond_wait_bql(&g
->reset_cond
);
1580 aio_bh_call(g
->reset_bh
);
1583 while (!QTAILQ_EMPTY(&g
->cmdq
)) {
1584 cmd
= QTAILQ_FIRST(&g
->cmdq
);
1585 QTAILQ_REMOVE(&g
->cmdq
, cmd
, next
);
1589 while (!QTAILQ_EMPTY(&g
->fenceq
)) {
1590 cmd
= QTAILQ_FIRST(&g
->fenceq
);
1591 QTAILQ_REMOVE(&g
->fenceq
, cmd
, next
);
1596 virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev
));
1600 virtio_gpu_get_config(VirtIODevice
*vdev
, uint8_t *config
)
1602 VirtIOGPUBase
*g
= VIRTIO_GPU_BASE(vdev
);
1604 memcpy(config
, &g
->virtio_config
, sizeof(g
->virtio_config
));
1608 virtio_gpu_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
1610 VirtIOGPUBase
*g
= VIRTIO_GPU_BASE(vdev
);
1611 const struct virtio_gpu_config
*vgconfig
=
1612 (const struct virtio_gpu_config
*)config
;
1614 if (vgconfig
->events_clear
) {
1615 g
->virtio_config
.events_read
&= ~vgconfig
->events_clear
;
1619 static bool virtio_gpu_blob_state_needed(void *opaque
)
1621 VirtIOGPU
*g
= VIRTIO_GPU(opaque
);
1623 return virtio_gpu_blob_enabled(g
->parent_obj
.conf
);
1626 const VMStateDescription vmstate_virtio_gpu_blob_state
= {
1627 .name
= "virtio-gpu/blob",
1628 .minimum_version_id
= VIRTIO_GPU_VM_VERSION
,
1629 .version_id
= VIRTIO_GPU_VM_VERSION
,
1630 .needed
= virtio_gpu_blob_state_needed
,
1631 .fields
= (const VMStateField
[]){
1633 .name
= "virtio-gpu/blob",
1634 .info
= &(const VMStateInfo
) {
1636 .get
= virtio_gpu_blob_load
,
1637 .put
= virtio_gpu_blob_save
,
1639 .flags
= VMS_SINGLE
,
1641 VMSTATE_END_OF_LIST()
1646 * For historical reasons virtio_gpu does not adhere to virtio migration
1647 * scheme as described in doc/virtio-migration.txt, in a sense that no
1648 * save/load callback are provided to the core. Instead the device data
1649 * is saved/loaded after the core data.
1651 * Because of this we need a special vmsd.
1653 static const VMStateDescription vmstate_virtio_gpu
= {
1654 .name
= "virtio-gpu",
1655 .minimum_version_id
= VIRTIO_GPU_VM_VERSION
,
1656 .version_id
= VIRTIO_GPU_VM_VERSION
,
1657 .fields
= (const VMStateField
[]) {
1658 VMSTATE_VIRTIO_DEVICE
/* core */,
1660 .name
= "virtio-gpu",
1661 .info
= &(const VMStateInfo
) {
1662 .name
= "virtio-gpu",
1663 .get
= virtio_gpu_load
,
1664 .put
= virtio_gpu_save
,
1666 .flags
= VMS_SINGLE
,
1668 VMSTATE_END_OF_LIST()
1670 .subsections
= (const VMStateDescription
* const []) {
1671 &vmstate_virtio_gpu_blob_state
,
1674 .post_load
= virtio_gpu_post_load
,
1677 static const Property virtio_gpu_properties
[] = {
1678 VIRTIO_GPU_BASE_PROPERTIES(VirtIOGPU
, parent_obj
.conf
),
1679 DEFINE_PROP_SIZE("max_hostmem", VirtIOGPU
, conf_max_hostmem
,
1681 DEFINE_PROP_BIT("blob", VirtIOGPU
, parent_obj
.conf
.flags
,
1682 VIRTIO_GPU_FLAG_BLOB_ENABLED
, false),
1683 DEFINE_PROP_SIZE("hostmem", VirtIOGPU
, parent_obj
.conf
.hostmem
, 0),
1684 DEFINE_PROP_UINT8("x-scanout-vmstate-version", VirtIOGPU
, scanout_vmstate_version
, 2),
1687 static void virtio_gpu_class_init(ObjectClass
*klass
, const void *data
)
1689 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1690 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1691 VirtIOGPUClass
*vgc
= VIRTIO_GPU_CLASS(klass
);
1692 VirtIOGPUBaseClass
*vgbc
= &vgc
->parent
;
1694 vgc
->handle_ctrl
= virtio_gpu_handle_ctrl
;
1695 vgc
->process_cmd
= virtio_gpu_simple_process_cmd
;
1696 vgc
->update_cursor_data
= virtio_gpu_update_cursor_data
;
1697 vgc
->resource_destroy
= virtio_gpu_resource_destroy
;
1698 vgbc
->gl_flushed
= virtio_gpu_handle_gl_flushed
;
1700 vdc
->realize
= virtio_gpu_device_realize
;
1701 vdc
->unrealize
= virtio_gpu_device_unrealize
;
1702 vdc
->reset
= virtio_gpu_reset
;
1703 vdc
->get_config
= virtio_gpu_get_config
;
1704 vdc
->set_config
= virtio_gpu_set_config
;
1706 dc
->vmsd
= &vmstate_virtio_gpu
;
1707 device_class_set_props(dc
, virtio_gpu_properties
);
1710 static const TypeInfo virtio_gpu_info
= {
1711 .name
= TYPE_VIRTIO_GPU
,
1712 .parent
= TYPE_VIRTIO_GPU_BASE
,
1713 .instance_size
= sizeof(VirtIOGPU
),
1714 .class_size
= sizeof(VirtIOGPUClass
),
1715 .class_init
= virtio_gpu_class_init
,
1717 module_obj(TYPE_VIRTIO_GPU
);
1718 module_kconfig(VIRTIO_GPU
);
1720 static void virtio_register_types(void)
1722 type_register_static(&virtio_gpu_info
);
1725 type_init(virtio_register_types
)