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 "ui/console.h"
19 #include "sysemu/dma.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/virtio/virtio.h"
22 #include "migration/qemu-file-types.h"
23 #include "hw/virtio/virtio-gpu.h"
24 #include "hw/virtio/virtio-gpu-bswap.h"
25 #include "hw/virtio/virtio-gpu-pixman.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "hw/qdev-properties.h"
29 #include "qemu/module.h"
30 #include "qapi/error.h"
31 #include "qemu/error-report.h"
33 #define VIRTIO_GPU_VM_VERSION 1
35 static struct virtio_gpu_simple_resource
*
36 virtio_gpu_find_resource(VirtIOGPU
*g
, uint32_t resource_id
);
37 static struct virtio_gpu_simple_resource
*
38 virtio_gpu_find_check_resource(VirtIOGPU
*g
, uint32_t resource_id
,
40 const char *caller
, uint32_t *error
);
42 static void virtio_gpu_cleanup_mapping(VirtIOGPU
*g
,
43 struct virtio_gpu_simple_resource
*res
);
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
,
114 cursor
->resource_id
? 1 : 0);
117 static struct virtio_gpu_simple_resource
*
118 virtio_gpu_find_resource(VirtIOGPU
*g
, uint32_t resource_id
)
120 struct virtio_gpu_simple_resource
*res
;
122 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
123 if (res
->resource_id
== resource_id
) {
130 static struct virtio_gpu_simple_resource
*
131 virtio_gpu_find_check_resource(VirtIOGPU
*g
, uint32_t resource_id
,
132 bool require_backing
,
133 const char *caller
, uint32_t *error
)
135 struct virtio_gpu_simple_resource
*res
;
137 res
= virtio_gpu_find_resource(g
, resource_id
);
139 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid resource specified %d\n",
140 caller
, resource_id
);
142 *error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
147 if (require_backing
) {
148 if (!res
->iov
|| (!res
->image
&& !res
->blob
)) {
149 qemu_log_mask(LOG_GUEST_ERROR
, "%s: no backing storage %d\n",
150 caller
, resource_id
);
152 *error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
161 void virtio_gpu_ctrl_response(VirtIOGPU
*g
,
162 struct virtio_gpu_ctrl_command
*cmd
,
163 struct virtio_gpu_ctrl_hdr
*resp
,
168 if (cmd
->cmd_hdr
.flags
& VIRTIO_GPU_FLAG_FENCE
) {
169 resp
->flags
|= VIRTIO_GPU_FLAG_FENCE
;
170 resp
->fence_id
= cmd
->cmd_hdr
.fence_id
;
171 resp
->ctx_id
= cmd
->cmd_hdr
.ctx_id
;
173 virtio_gpu_ctrl_hdr_bswap(resp
);
174 s
= iov_from_buf(cmd
->elem
.in_sg
, cmd
->elem
.in_num
, 0, resp
, resp_len
);
176 qemu_log_mask(LOG_GUEST_ERROR
,
177 "%s: response size incorrect %zu vs %zu\n",
178 __func__
, s
, resp_len
);
180 virtqueue_push(cmd
->vq
, &cmd
->elem
, s
);
181 virtio_notify(VIRTIO_DEVICE(g
), cmd
->vq
);
182 cmd
->finished
= true;
185 void virtio_gpu_ctrl_response_nodata(VirtIOGPU
*g
,
186 struct virtio_gpu_ctrl_command
*cmd
,
187 enum virtio_gpu_ctrl_type type
)
189 struct virtio_gpu_ctrl_hdr resp
;
191 memset(&resp
, 0, sizeof(resp
));
193 virtio_gpu_ctrl_response(g
, cmd
, &resp
, sizeof(resp
));
196 void virtio_gpu_get_display_info(VirtIOGPU
*g
,
197 struct virtio_gpu_ctrl_command
*cmd
)
199 struct virtio_gpu_resp_display_info display_info
;
201 trace_virtio_gpu_cmd_get_display_info();
202 memset(&display_info
, 0, sizeof(display_info
));
203 display_info
.hdr
.type
= VIRTIO_GPU_RESP_OK_DISPLAY_INFO
;
204 virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g
), &display_info
);
205 virtio_gpu_ctrl_response(g
, cmd
, &display_info
.hdr
,
206 sizeof(display_info
));
209 void virtio_gpu_get_edid(VirtIOGPU
*g
,
210 struct virtio_gpu_ctrl_command
*cmd
)
212 struct virtio_gpu_resp_edid edid
;
213 struct virtio_gpu_cmd_get_edid get_edid
;
214 VirtIOGPUBase
*b
= VIRTIO_GPU_BASE(g
);
216 VIRTIO_GPU_FILL_CMD(get_edid
);
217 virtio_gpu_bswap_32(&get_edid
, sizeof(get_edid
));
219 if (get_edid
.scanout
>= b
->conf
.max_outputs
) {
220 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
224 trace_virtio_gpu_cmd_get_edid(get_edid
.scanout
);
225 memset(&edid
, 0, sizeof(edid
));
226 edid
.hdr
.type
= VIRTIO_GPU_RESP_OK_EDID
;
227 virtio_gpu_base_generate_edid(VIRTIO_GPU_BASE(g
), get_edid
.scanout
, &edid
);
228 virtio_gpu_ctrl_response(g
, cmd
, &edid
.hdr
, sizeof(edid
));
231 static uint32_t calc_image_hostmem(pixman_format_code_t pformat
,
232 uint32_t width
, uint32_t height
)
234 /* Copied from pixman/pixman-bits-image.c, skip integer overflow check.
235 * pixman_image_create_bits will fail in case it overflow.
238 int bpp
= PIXMAN_FORMAT_BPP(pformat
);
239 int stride
= ((width
* bpp
+ 0x1f) >> 5) * sizeof(uint32_t);
240 return height
* stride
;
245 win32_pixman_image_destroy(pixman_image_t
*image
, void *data
)
247 HANDLE handle
= data
;
249 qemu_win32_map_free(pixman_image_get_data(image
), handle
, &error_warn
);
253 static void virtio_gpu_resource_create_2d(VirtIOGPU
*g
,
254 struct virtio_gpu_ctrl_command
*cmd
)
256 pixman_format_code_t pformat
;
257 struct virtio_gpu_simple_resource
*res
;
258 struct virtio_gpu_resource_create_2d c2d
;
260 VIRTIO_GPU_FILL_CMD(c2d
);
261 virtio_gpu_bswap_32(&c2d
, sizeof(c2d
));
262 trace_virtio_gpu_cmd_res_create_2d(c2d
.resource_id
, c2d
.format
,
263 c2d
.width
, c2d
.height
);
265 if (c2d
.resource_id
== 0) {
266 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource id 0 is not allowed\n",
268 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
272 res
= virtio_gpu_find_resource(g
, c2d
.resource_id
);
274 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource already exists %d\n",
275 __func__
, c2d
.resource_id
);
276 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
280 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
282 res
->width
= c2d
.width
;
283 res
->height
= c2d
.height
;
284 res
->format
= c2d
.format
;
285 res
->resource_id
= c2d
.resource_id
;
287 pformat
= virtio_gpu_get_pixman_format(c2d
.format
);
289 qemu_log_mask(LOG_GUEST_ERROR
,
290 "%s: host couldn't handle guest format %d\n",
291 __func__
, c2d
.format
);
293 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
297 res
->hostmem
= calc_image_hostmem(pformat
, c2d
.width
, c2d
.height
);
298 if (res
->hostmem
+ g
->hostmem
< g
->conf_max_hostmem
) {
301 bits
= qemu_win32_map_alloc(res
->hostmem
, &res
->handle
, &error_warn
);
306 res
->image
= pixman_image_create_bits(
310 bits
, c2d
.height
? res
->hostmem
/ c2d
.height
: 0);
313 pixman_image_set_destroy_function(res
->image
, win32_pixman_image_destroy
, res
->handle
);
322 qemu_log_mask(LOG_GUEST_ERROR
,
323 "%s: resource creation failed %d %d %d\n",
324 __func__
, c2d
.resource_id
, c2d
.width
, c2d
.height
);
326 cmd
->error
= VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY
;
330 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
331 g
->hostmem
+= res
->hostmem
;
334 static void virtio_gpu_resource_create_blob(VirtIOGPU
*g
,
335 struct virtio_gpu_ctrl_command
*cmd
)
337 struct virtio_gpu_simple_resource
*res
;
338 struct virtio_gpu_resource_create_blob cblob
;
341 VIRTIO_GPU_FILL_CMD(cblob
);
342 virtio_gpu_create_blob_bswap(&cblob
);
343 trace_virtio_gpu_cmd_res_create_blob(cblob
.resource_id
, cblob
.size
);
345 if (cblob
.resource_id
== 0) {
346 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource id 0 is not allowed\n",
348 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
352 if (cblob
.blob_mem
!= VIRTIO_GPU_BLOB_MEM_GUEST
&&
353 cblob
.blob_flags
!= VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE
) {
354 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid memory type\n",
356 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
360 if (virtio_gpu_find_resource(g
, cblob
.resource_id
)) {
361 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource already exists %d\n",
362 __func__
, cblob
.resource_id
);
363 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
367 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
368 res
->resource_id
= cblob
.resource_id
;
369 res
->blob_size
= cblob
.size
;
371 ret
= virtio_gpu_create_mapping_iov(g
, cblob
.nr_entries
, sizeof(cblob
),
372 cmd
, &res
->addrs
, &res
->iov
,
375 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
380 virtio_gpu_init_udmabuf(res
);
381 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
384 static void virtio_gpu_disable_scanout(VirtIOGPU
*g
, int scanout_id
)
386 struct virtio_gpu_scanout
*scanout
= &g
->parent_obj
.scanout
[scanout_id
];
387 struct virtio_gpu_simple_resource
*res
;
389 if (scanout
->resource_id
== 0) {
393 res
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
395 res
->scanout_bitmask
&= ~(1 << scanout_id
);
398 dpy_gfx_replace_surface(scanout
->con
, NULL
);
399 scanout
->resource_id
= 0;
405 static void virtio_gpu_resource_destroy(VirtIOGPU
*g
,
406 struct virtio_gpu_simple_resource
*res
)
410 if (res
->scanout_bitmask
) {
411 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
412 if (res
->scanout_bitmask
& (1 << i
)) {
413 virtio_gpu_disable_scanout(g
, i
);
418 qemu_pixman_image_unref(res
->image
);
419 virtio_gpu_cleanup_mapping(g
, res
);
420 QTAILQ_REMOVE(&g
->reslist
, res
, next
);
421 g
->hostmem
-= res
->hostmem
;
425 static void virtio_gpu_resource_unref(VirtIOGPU
*g
,
426 struct virtio_gpu_ctrl_command
*cmd
)
428 struct virtio_gpu_simple_resource
*res
;
429 struct virtio_gpu_resource_unref unref
;
431 VIRTIO_GPU_FILL_CMD(unref
);
432 virtio_gpu_bswap_32(&unref
, sizeof(unref
));
433 trace_virtio_gpu_cmd_res_unref(unref
.resource_id
);
435 res
= virtio_gpu_find_resource(g
, unref
.resource_id
);
437 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
438 __func__
, unref
.resource_id
);
439 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
442 virtio_gpu_resource_destroy(g
, res
);
445 static void virtio_gpu_transfer_to_host_2d(VirtIOGPU
*g
,
446 struct virtio_gpu_ctrl_command
*cmd
)
448 struct virtio_gpu_simple_resource
*res
;
450 uint32_t src_offset
, dst_offset
, stride
;
451 pixman_format_code_t format
;
452 struct virtio_gpu_transfer_to_host_2d t2d
;
455 VIRTIO_GPU_FILL_CMD(t2d
);
456 virtio_gpu_t2d_bswap(&t2d
);
457 trace_virtio_gpu_cmd_res_xfer_toh_2d(t2d
.resource_id
);
459 res
= virtio_gpu_find_check_resource(g
, t2d
.resource_id
, true,
460 __func__
, &cmd
->error
);
461 if (!res
|| res
->blob
) {
465 if (t2d
.r
.x
> res
->width
||
466 t2d
.r
.y
> res
->height
||
467 t2d
.r
.width
> res
->width
||
468 t2d
.r
.height
> res
->height
||
469 t2d
.r
.x
+ t2d
.r
.width
> res
->width
||
470 t2d
.r
.y
+ t2d
.r
.height
> res
->height
) {
471 qemu_log_mask(LOG_GUEST_ERROR
, "%s: transfer bounds outside resource"
472 " bounds for resource %d: %d %d %d %d vs %d %d\n",
473 __func__
, t2d
.resource_id
, t2d
.r
.x
, t2d
.r
.y
,
474 t2d
.r
.width
, t2d
.r
.height
, res
->width
, res
->height
);
475 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
479 format
= pixman_image_get_format(res
->image
);
480 bpp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format
), 8);
481 stride
= pixman_image_get_stride(res
->image
);
482 img_data
= pixman_image_get_data(res
->image
);
484 if (t2d
.r
.x
|| t2d
.r
.width
!= pixman_image_get_width(res
->image
)) {
485 for (h
= 0; h
< t2d
.r
.height
; h
++) {
486 src_offset
= t2d
.offset
+ stride
* h
;
487 dst_offset
= (t2d
.r
.y
+ h
) * stride
+ (t2d
.r
.x
* bpp
);
489 iov_to_buf(res
->iov
, res
->iov_cnt
, src_offset
,
490 (uint8_t *)img_data
+ dst_offset
,
494 src_offset
= t2d
.offset
;
495 dst_offset
= t2d
.r
.y
* stride
+ t2d
.r
.x
* bpp
;
496 iov_to_buf(res
->iov
, res
->iov_cnt
, src_offset
,
497 (uint8_t *)img_data
+ dst_offset
,
498 stride
* t2d
.r
.height
);
502 static void virtio_gpu_resource_flush(VirtIOGPU
*g
,
503 struct virtio_gpu_ctrl_command
*cmd
)
505 struct virtio_gpu_simple_resource
*res
;
506 struct virtio_gpu_resource_flush rf
;
507 struct virtio_gpu_scanout
*scanout
;
508 pixman_region16_t flush_region
;
509 bool within_bounds
= false;
510 bool update_submitted
= false;
513 VIRTIO_GPU_FILL_CMD(rf
);
514 virtio_gpu_bswap_32(&rf
, sizeof(rf
));
515 trace_virtio_gpu_cmd_res_flush(rf
.resource_id
,
516 rf
.r
.width
, rf
.r
.height
, rf
.r
.x
, rf
.r
.y
);
518 res
= virtio_gpu_find_check_resource(g
, rf
.resource_id
, false,
519 __func__
, &cmd
->error
);
525 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
526 scanout
= &g
->parent_obj
.scanout
[i
];
527 if (scanout
->resource_id
== res
->resource_id
&&
528 rf
.r
.x
< scanout
->x
+ scanout
->width
&&
529 rf
.r
.x
+ rf
.r
.width
>= scanout
->x
&&
530 rf
.r
.y
< scanout
->y
+ scanout
->height
&&
531 rf
.r
.y
+ rf
.r
.height
>= scanout
->y
) {
532 within_bounds
= true;
534 if (console_has_gl(scanout
->con
)) {
535 dpy_gl_update(scanout
->con
, 0, 0, scanout
->width
,
537 update_submitted
= true;
542 if (update_submitted
) {
545 if (!within_bounds
) {
546 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flush bounds outside scanouts"
547 " bounds for flush %d: %d %d %d %d\n",
548 __func__
, rf
.resource_id
, rf
.r
.x
, rf
.r
.y
,
549 rf
.r
.width
, rf
.r
.height
);
550 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
556 (rf
.r
.x
> res
->width
||
557 rf
.r
.y
> res
->height
||
558 rf
.r
.width
> res
->width
||
559 rf
.r
.height
> res
->height
||
560 rf
.r
.x
+ rf
.r
.width
> res
->width
||
561 rf
.r
.y
+ rf
.r
.height
> res
->height
)) {
562 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flush bounds outside resource"
563 " bounds for resource %d: %d %d %d %d vs %d %d\n",
564 __func__
, rf
.resource_id
, rf
.r
.x
, rf
.r
.y
,
565 rf
.r
.width
, rf
.r
.height
, res
->width
, res
->height
);
566 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
570 pixman_region_init_rect(&flush_region
,
571 rf
.r
.x
, rf
.r
.y
, rf
.r
.width
, rf
.r
.height
);
572 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
573 pixman_region16_t region
, finalregion
;
574 pixman_box16_t
*extents
;
576 if (!(res
->scanout_bitmask
& (1 << i
))) {
579 scanout
= &g
->parent_obj
.scanout
[i
];
581 pixman_region_init(&finalregion
);
582 pixman_region_init_rect(®ion
, scanout
->x
, scanout
->y
,
583 scanout
->width
, scanout
->height
);
585 pixman_region_intersect(&finalregion
, &flush_region
, ®ion
);
586 pixman_region_translate(&finalregion
, -scanout
->x
, -scanout
->y
);
587 extents
= pixman_region_extents(&finalregion
);
588 /* work out the area we need to update for each console */
589 dpy_gfx_update(g
->parent_obj
.scanout
[i
].con
,
590 extents
->x1
, extents
->y1
,
591 extents
->x2
- extents
->x1
,
592 extents
->y2
- extents
->y1
);
594 pixman_region_fini(®ion
);
595 pixman_region_fini(&finalregion
);
597 pixman_region_fini(&flush_region
);
600 static void virtio_unref_resource(pixman_image_t
*image
, void *data
)
602 pixman_image_unref(data
);
605 static void virtio_gpu_update_scanout(VirtIOGPU
*g
,
607 struct virtio_gpu_simple_resource
*res
,
608 struct virtio_gpu_rect
*r
)
610 struct virtio_gpu_simple_resource
*ores
;
611 struct virtio_gpu_scanout
*scanout
;
613 scanout
= &g
->parent_obj
.scanout
[scanout_id
];
614 ores
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
616 ores
->scanout_bitmask
&= ~(1 << scanout_id
);
619 res
->scanout_bitmask
|= (1 << scanout_id
);
620 scanout
->resource_id
= res
->resource_id
;
623 scanout
->width
= r
->width
;
624 scanout
->height
= r
->height
;
627 static void virtio_gpu_do_set_scanout(VirtIOGPU
*g
,
629 struct virtio_gpu_framebuffer
*fb
,
630 struct virtio_gpu_simple_resource
*res
,
631 struct virtio_gpu_rect
*r
,
634 struct virtio_gpu_scanout
*scanout
;
637 scanout
= &g
->parent_obj
.scanout
[scanout_id
];
639 if (r
->x
> fb
->width
||
643 r
->width
> fb
->width
||
644 r
->height
> fb
->height
||
645 r
->x
+ r
->width
> fb
->width
||
646 r
->y
+ r
->height
> fb
->height
) {
647 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout %d bounds for"
648 " resource %d, rect (%d,%d)+%d,%d, fb %d %d\n",
649 __func__
, scanout_id
, res
->resource_id
,
650 r
->x
, r
->y
, r
->width
, r
->height
,
651 fb
->width
, fb
->height
);
652 *error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
656 g
->parent_obj
.enable
= 1;
659 if (console_has_gl(scanout
->con
)) {
660 if (!virtio_gpu_update_dmabuf(g
, scanout_id
, res
, fb
, r
)) {
661 virtio_gpu_update_scanout(g
, scanout_id
, res
, r
);
663 *error
= VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY
;
670 data
= (uint8_t *)pixman_image_get_data(res
->image
);
673 /* create a surface for this scanout */
674 if ((res
->blob
&& !console_has_gl(scanout
->con
)) ||
676 surface_data(scanout
->ds
) != data
+ fb
->offset
||
677 scanout
->width
!= r
->width
||
678 scanout
->height
!= r
->height
) {
679 pixman_image_t
*rect
;
680 void *ptr
= data
+ fb
->offset
;
681 rect
= pixman_image_create_bits(fb
->format
, r
->width
, r
->height
,
685 pixman_image_ref(res
->image
);
686 pixman_image_set_destroy_function(rect
, virtio_unref_resource
,
690 /* realloc the surface ptr */
691 scanout
->ds
= qemu_create_displaysurface_pixman(rect
);
693 *error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
697 qemu_displaysurface_win32_set_handle(scanout
->ds
, res
->handle
, fb
->offset
);
700 pixman_image_unref(rect
);
701 dpy_gfx_replace_surface(g
->parent_obj
.scanout
[scanout_id
].con
,
705 virtio_gpu_update_scanout(g
, scanout_id
, res
, r
);
708 static void virtio_gpu_set_scanout(VirtIOGPU
*g
,
709 struct virtio_gpu_ctrl_command
*cmd
)
711 struct virtio_gpu_simple_resource
*res
;
712 struct virtio_gpu_framebuffer fb
= { 0 };
713 struct virtio_gpu_set_scanout ss
;
715 VIRTIO_GPU_FILL_CMD(ss
);
716 virtio_gpu_bswap_32(&ss
, sizeof(ss
));
717 trace_virtio_gpu_cmd_set_scanout(ss
.scanout_id
, ss
.resource_id
,
718 ss
.r
.width
, ss
.r
.height
, ss
.r
.x
, ss
.r
.y
);
720 if (ss
.scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
721 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout id specified %d",
722 __func__
, ss
.scanout_id
);
723 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID
;
727 if (ss
.resource_id
== 0) {
728 virtio_gpu_disable_scanout(g
, ss
.scanout_id
);
732 res
= virtio_gpu_find_check_resource(g
, ss
.resource_id
, true,
733 __func__
, &cmd
->error
);
738 fb
.format
= pixman_image_get_format(res
->image
);
739 fb
.bytes_pp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb
.format
), 8);
740 fb
.width
= pixman_image_get_width(res
->image
);
741 fb
.height
= pixman_image_get_height(res
->image
);
742 fb
.stride
= pixman_image_get_stride(res
->image
);
743 fb
.offset
= ss
.r
.x
* fb
.bytes_pp
+ ss
.r
.y
* fb
.stride
;
745 virtio_gpu_do_set_scanout(g
, ss
.scanout_id
,
746 &fb
, res
, &ss
.r
, &cmd
->error
);
749 static void virtio_gpu_set_scanout_blob(VirtIOGPU
*g
,
750 struct virtio_gpu_ctrl_command
*cmd
)
752 struct virtio_gpu_simple_resource
*res
;
753 struct virtio_gpu_framebuffer fb
= { 0 };
754 struct virtio_gpu_set_scanout_blob ss
;
757 VIRTIO_GPU_FILL_CMD(ss
);
758 virtio_gpu_scanout_blob_bswap(&ss
);
759 trace_virtio_gpu_cmd_set_scanout_blob(ss
.scanout_id
, ss
.resource_id
,
760 ss
.r
.width
, ss
.r
.height
, ss
.r
.x
,
763 if (ss
.scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
764 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout id specified %d",
765 __func__
, ss
.scanout_id
);
766 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID
;
770 if (ss
.resource_id
== 0) {
771 virtio_gpu_disable_scanout(g
, ss
.scanout_id
);
775 res
= virtio_gpu_find_check_resource(g
, ss
.resource_id
, true,
776 __func__
, &cmd
->error
);
781 fb
.format
= virtio_gpu_get_pixman_format(ss
.format
);
783 qemu_log_mask(LOG_GUEST_ERROR
,
784 "%s: host couldn't handle guest format %d\n",
785 __func__
, ss
.format
);
786 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
790 fb
.bytes_pp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb
.format
), 8);
792 fb
.height
= ss
.height
;
793 fb
.stride
= ss
.strides
[0];
794 fb
.offset
= ss
.offsets
[0] + ss
.r
.x
* fb
.bytes_pp
+ ss
.r
.y
* fb
.stride
;
797 fbend
+= fb
.stride
* (ss
.r
.height
- 1);
798 fbend
+= fb
.bytes_pp
* ss
.r
.width
;
799 if (fbend
> res
->blob_size
) {
800 qemu_log_mask(LOG_GUEST_ERROR
,
801 "%s: fb end out of range\n",
803 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
807 virtio_gpu_do_set_scanout(g
, ss
.scanout_id
,
808 &fb
, res
, &ss
.r
, &cmd
->error
);
811 int virtio_gpu_create_mapping_iov(VirtIOGPU
*g
,
812 uint32_t nr_entries
, uint32_t offset
,
813 struct virtio_gpu_ctrl_command
*cmd
,
814 uint64_t **addr
, struct iovec
**iov
,
817 struct virtio_gpu_mem_entry
*ents
;
821 if (nr_entries
> 16384) {
822 qemu_log_mask(LOG_GUEST_ERROR
,
823 "%s: nr_entries is too big (%d > 16384)\n",
824 __func__
, nr_entries
);
828 esize
= sizeof(*ents
) * nr_entries
;
829 ents
= g_malloc(esize
);
830 s
= iov_to_buf(cmd
->elem
.out_sg
, cmd
->elem
.out_num
,
831 offset
, ents
, esize
);
833 qemu_log_mask(LOG_GUEST_ERROR
,
834 "%s: command data size incorrect %zu vs %zu\n",
844 for (e
= 0, v
= 0; e
< nr_entries
; e
++) {
845 uint64_t a
= le64_to_cpu(ents
[e
].addr
);
846 uint32_t l
= le32_to_cpu(ents
[e
].length
);
852 map
= dma_memory_map(VIRTIO_DEVICE(g
)->dma_as
, a
, &len
,
853 DMA_DIRECTION_TO_DEVICE
,
854 MEMTXATTRS_UNSPECIFIED
);
856 qemu_log_mask(LOG_GUEST_ERROR
, "%s: failed to map MMIO memory for"
857 " element %d\n", __func__
, e
);
858 virtio_gpu_cleanup_mapping_iov(g
, *iov
, v
);
869 *iov
= g_renew(struct iovec
, *iov
, v
+ 16);
871 *addr
= g_renew(uint64_t, *addr
, v
+ 16);
874 (*iov
)[v
].iov_base
= map
;
875 (*iov
)[v
].iov_len
= len
;
891 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU
*g
,
892 struct iovec
*iov
, uint32_t count
)
896 for (i
= 0; i
< count
; i
++) {
897 dma_memory_unmap(VIRTIO_DEVICE(g
)->dma_as
,
898 iov
[i
].iov_base
, iov
[i
].iov_len
,
899 DMA_DIRECTION_TO_DEVICE
,
905 static void virtio_gpu_cleanup_mapping(VirtIOGPU
*g
,
906 struct virtio_gpu_simple_resource
*res
)
908 virtio_gpu_cleanup_mapping_iov(g
, res
->iov
, res
->iov_cnt
);
915 virtio_gpu_fini_udmabuf(res
);
920 virtio_gpu_resource_attach_backing(VirtIOGPU
*g
,
921 struct virtio_gpu_ctrl_command
*cmd
)
923 struct virtio_gpu_simple_resource
*res
;
924 struct virtio_gpu_resource_attach_backing ab
;
927 VIRTIO_GPU_FILL_CMD(ab
);
928 virtio_gpu_bswap_32(&ab
, sizeof(ab
));
929 trace_virtio_gpu_cmd_res_back_attach(ab
.resource_id
);
931 res
= virtio_gpu_find_resource(g
, ab
.resource_id
);
933 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
934 __func__
, ab
.resource_id
);
935 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
940 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
944 ret
= virtio_gpu_create_mapping_iov(g
, ab
.nr_entries
, sizeof(ab
), cmd
,
945 &res
->addrs
, &res
->iov
, &res
->iov_cnt
);
947 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
953 virtio_gpu_resource_detach_backing(VirtIOGPU
*g
,
954 struct virtio_gpu_ctrl_command
*cmd
)
956 struct virtio_gpu_simple_resource
*res
;
957 struct virtio_gpu_resource_detach_backing detach
;
959 VIRTIO_GPU_FILL_CMD(detach
);
960 virtio_gpu_bswap_32(&detach
, sizeof(detach
));
961 trace_virtio_gpu_cmd_res_back_detach(detach
.resource_id
);
963 res
= virtio_gpu_find_check_resource(g
, detach
.resource_id
, true,
964 __func__
, &cmd
->error
);
968 virtio_gpu_cleanup_mapping(g
, res
);
971 void virtio_gpu_simple_process_cmd(VirtIOGPU
*g
,
972 struct virtio_gpu_ctrl_command
*cmd
)
974 VIRTIO_GPU_FILL_CMD(cmd
->cmd_hdr
);
975 virtio_gpu_ctrl_hdr_bswap(&cmd
->cmd_hdr
);
977 switch (cmd
->cmd_hdr
.type
) {
978 case VIRTIO_GPU_CMD_GET_DISPLAY_INFO
:
979 virtio_gpu_get_display_info(g
, cmd
);
981 case VIRTIO_GPU_CMD_GET_EDID
:
982 virtio_gpu_get_edid(g
, cmd
);
984 case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D
:
985 virtio_gpu_resource_create_2d(g
, cmd
);
987 case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB
:
988 if (!virtio_gpu_blob_enabled(g
->parent_obj
.conf
)) {
989 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
992 virtio_gpu_resource_create_blob(g
, cmd
);
994 case VIRTIO_GPU_CMD_RESOURCE_UNREF
:
995 virtio_gpu_resource_unref(g
, cmd
);
997 case VIRTIO_GPU_CMD_RESOURCE_FLUSH
:
998 virtio_gpu_resource_flush(g
, cmd
);
1000 case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D
:
1001 virtio_gpu_transfer_to_host_2d(g
, cmd
);
1003 case VIRTIO_GPU_CMD_SET_SCANOUT
:
1004 virtio_gpu_set_scanout(g
, cmd
);
1006 case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB
:
1007 if (!virtio_gpu_blob_enabled(g
->parent_obj
.conf
)) {
1008 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
1011 virtio_gpu_set_scanout_blob(g
, cmd
);
1013 case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
:
1014 virtio_gpu_resource_attach_backing(g
, cmd
);
1016 case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING
:
1017 virtio_gpu_resource_detach_backing(g
, cmd
);
1020 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
1023 if (!cmd
->finished
) {
1024 if (!g
->parent_obj
.renderer_blocked
) {
1025 virtio_gpu_ctrl_response_nodata(g
, cmd
, cmd
->error
? cmd
->error
:
1026 VIRTIO_GPU_RESP_OK_NODATA
);
1031 static void virtio_gpu_handle_ctrl_cb(VirtIODevice
*vdev
, VirtQueue
*vq
)
1033 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1034 qemu_bh_schedule(g
->ctrl_bh
);
1037 static void virtio_gpu_handle_cursor_cb(VirtIODevice
*vdev
, VirtQueue
*vq
)
1039 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1040 qemu_bh_schedule(g
->cursor_bh
);
1043 void virtio_gpu_process_cmdq(VirtIOGPU
*g
)
1045 struct virtio_gpu_ctrl_command
*cmd
;
1046 VirtIOGPUClass
*vgc
= VIRTIO_GPU_GET_CLASS(g
);
1048 if (g
->processing_cmdq
) {
1051 g
->processing_cmdq
= true;
1052 while (!QTAILQ_EMPTY(&g
->cmdq
)) {
1053 cmd
= QTAILQ_FIRST(&g
->cmdq
);
1055 if (g
->parent_obj
.renderer_blocked
) {
1059 /* process command */
1060 vgc
->process_cmd(g
, cmd
);
1062 QTAILQ_REMOVE(&g
->cmdq
, cmd
, next
);
1063 if (virtio_gpu_stats_enabled(g
->parent_obj
.conf
)) {
1064 g
->stats
.requests
++;
1067 if (!cmd
->finished
) {
1068 QTAILQ_INSERT_TAIL(&g
->fenceq
, cmd
, next
);
1070 if (virtio_gpu_stats_enabled(g
->parent_obj
.conf
)) {
1071 if (g
->stats
.max_inflight
< g
->inflight
) {
1072 g
->stats
.max_inflight
= g
->inflight
;
1074 fprintf(stderr
, "inflight: %3d (+)\r", g
->inflight
);
1080 g
->processing_cmdq
= false;
1083 static void virtio_gpu_process_fenceq(VirtIOGPU
*g
)
1085 struct virtio_gpu_ctrl_command
*cmd
, *tmp
;
1087 QTAILQ_FOREACH_SAFE(cmd
, &g
->fenceq
, next
, tmp
) {
1088 trace_virtio_gpu_fence_resp(cmd
->cmd_hdr
.fence_id
);
1089 virtio_gpu_ctrl_response_nodata(g
, cmd
, VIRTIO_GPU_RESP_OK_NODATA
);
1090 QTAILQ_REMOVE(&g
->fenceq
, cmd
, next
);
1093 if (virtio_gpu_stats_enabled(g
->parent_obj
.conf
)) {
1094 fprintf(stderr
, "inflight: %3d (-)\r", g
->inflight
);
1099 static void virtio_gpu_handle_gl_flushed(VirtIOGPUBase
*b
)
1101 VirtIOGPU
*g
= container_of(b
, VirtIOGPU
, parent_obj
);
1103 virtio_gpu_process_fenceq(g
);
1104 virtio_gpu_process_cmdq(g
);
1107 static void virtio_gpu_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
1109 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1110 struct virtio_gpu_ctrl_command
*cmd
;
1112 if (!virtio_queue_ready(vq
)) {
1116 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
1120 cmd
->finished
= false;
1121 QTAILQ_INSERT_TAIL(&g
->cmdq
, cmd
, next
);
1122 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
1125 virtio_gpu_process_cmdq(g
);
1128 static void virtio_gpu_ctrl_bh(void *opaque
)
1130 VirtIOGPU
*g
= opaque
;
1131 VirtIOGPUClass
*vgc
= VIRTIO_GPU_GET_CLASS(g
);
1133 vgc
->handle_ctrl(&g
->parent_obj
.parent_obj
, g
->ctrl_vq
);
1136 static void virtio_gpu_handle_cursor(VirtIODevice
*vdev
, VirtQueue
*vq
)
1138 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1139 VirtQueueElement
*elem
;
1141 struct virtio_gpu_update_cursor cursor_info
;
1143 if (!virtio_queue_ready(vq
)) {
1147 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
1152 s
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
1153 &cursor_info
, sizeof(cursor_info
));
1154 if (s
!= sizeof(cursor_info
)) {
1155 qemu_log_mask(LOG_GUEST_ERROR
,
1156 "%s: cursor size incorrect %zu vs %zu\n",
1157 __func__
, s
, sizeof(cursor_info
));
1159 virtio_gpu_bswap_32(&cursor_info
, sizeof(cursor_info
));
1160 update_cursor(g
, &cursor_info
);
1162 virtqueue_push(vq
, elem
, 0);
1163 virtio_notify(vdev
, vq
);
1168 static void virtio_gpu_cursor_bh(void *opaque
)
1170 VirtIOGPU
*g
= opaque
;
1171 virtio_gpu_handle_cursor(&g
->parent_obj
.parent_obj
, g
->cursor_vq
);
1174 static const VMStateDescription vmstate_virtio_gpu_scanout
= {
1175 .name
= "virtio-gpu-one-scanout",
1177 .fields
= (VMStateField
[]) {
1178 VMSTATE_UINT32(resource_id
, struct virtio_gpu_scanout
),
1179 VMSTATE_UINT32(width
, struct virtio_gpu_scanout
),
1180 VMSTATE_UINT32(height
, struct virtio_gpu_scanout
),
1181 VMSTATE_INT32(x
, struct virtio_gpu_scanout
),
1182 VMSTATE_INT32(y
, struct virtio_gpu_scanout
),
1183 VMSTATE_UINT32(cursor
.resource_id
, struct virtio_gpu_scanout
),
1184 VMSTATE_UINT32(cursor
.hot_x
, struct virtio_gpu_scanout
),
1185 VMSTATE_UINT32(cursor
.hot_y
, struct virtio_gpu_scanout
),
1186 VMSTATE_UINT32(cursor
.pos
.x
, struct virtio_gpu_scanout
),
1187 VMSTATE_UINT32(cursor
.pos
.y
, struct virtio_gpu_scanout
),
1188 VMSTATE_END_OF_LIST()
1192 static const VMStateDescription vmstate_virtio_gpu_scanouts
= {
1193 .name
= "virtio-gpu-scanouts",
1195 .fields
= (VMStateField
[]) {
1196 VMSTATE_INT32(parent_obj
.enable
, struct VirtIOGPU
),
1197 VMSTATE_UINT32_EQUAL(parent_obj
.conf
.max_outputs
,
1198 struct VirtIOGPU
, NULL
),
1199 VMSTATE_STRUCT_VARRAY_UINT32(parent_obj
.scanout
, struct VirtIOGPU
,
1200 parent_obj
.conf
.max_outputs
, 1,
1201 vmstate_virtio_gpu_scanout
,
1202 struct virtio_gpu_scanout
),
1203 VMSTATE_END_OF_LIST()
1207 static int virtio_gpu_save(QEMUFile
*f
, void *opaque
, size_t size
,
1208 const VMStateField
*field
, JSONWriter
*vmdesc
)
1210 VirtIOGPU
*g
= opaque
;
1211 struct virtio_gpu_simple_resource
*res
;
1214 /* in 2d mode we should never find unprocessed commands here */
1215 assert(QTAILQ_EMPTY(&g
->cmdq
));
1217 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
1218 qemu_put_be32(f
, res
->resource_id
);
1219 qemu_put_be32(f
, res
->width
);
1220 qemu_put_be32(f
, res
->height
);
1221 qemu_put_be32(f
, res
->format
);
1222 qemu_put_be32(f
, res
->iov_cnt
);
1223 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1224 qemu_put_be64(f
, res
->addrs
[i
]);
1225 qemu_put_be32(f
, res
->iov
[i
].iov_len
);
1227 qemu_put_buffer(f
, (void *)pixman_image_get_data(res
->image
),
1228 pixman_image_get_stride(res
->image
) * res
->height
);
1230 qemu_put_be32(f
, 0); /* end of list */
1232 return vmstate_save_state(f
, &vmstate_virtio_gpu_scanouts
, g
, NULL
);
1235 static int virtio_gpu_load(QEMUFile
*f
, void *opaque
, size_t size
,
1236 const VMStateField
*field
)
1238 VirtIOGPU
*g
= opaque
;
1239 struct virtio_gpu_simple_resource
*res
;
1240 struct virtio_gpu_scanout
*scanout
;
1241 uint32_t resource_id
, pformat
;
1247 resource_id
= qemu_get_be32(f
);
1248 while (resource_id
!= 0) {
1249 res
= virtio_gpu_find_resource(g
, resource_id
);
1254 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
1255 res
->resource_id
= resource_id
;
1256 res
->width
= qemu_get_be32(f
);
1257 res
->height
= qemu_get_be32(f
);
1258 res
->format
= qemu_get_be32(f
);
1259 res
->iov_cnt
= qemu_get_be32(f
);
1262 pformat
= virtio_gpu_get_pixman_format(res
->format
);
1268 res
->hostmem
= calc_image_hostmem(pformat
, res
->width
, res
->height
);
1270 bits
= qemu_win32_map_alloc(res
->hostmem
, &res
->handle
, &error_warn
);
1276 res
->image
= pixman_image_create_bits(
1278 res
->width
, res
->height
,
1279 bits
, res
->height
? res
->hostmem
/ res
->height
: 0);
1286 res
->addrs
= g_new(uint64_t, res
->iov_cnt
);
1287 res
->iov
= g_new(struct iovec
, res
->iov_cnt
);
1290 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1291 res
->addrs
[i
] = qemu_get_be64(f
);
1292 res
->iov
[i
].iov_len
= qemu_get_be32(f
);
1294 qemu_get_buffer(f
, (void *)pixman_image_get_data(res
->image
),
1295 pixman_image_get_stride(res
->image
) * res
->height
);
1297 /* restore mapping */
1298 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1299 hwaddr len
= res
->iov
[i
].iov_len
;
1300 res
->iov
[i
].iov_base
=
1301 dma_memory_map(VIRTIO_DEVICE(g
)->dma_as
, res
->addrs
[i
], &len
,
1302 DMA_DIRECTION_TO_DEVICE
,
1303 MEMTXATTRS_UNSPECIFIED
);
1305 if (!res
->iov
[i
].iov_base
|| len
!= res
->iov
[i
].iov_len
) {
1306 /* Clean up the half-a-mapping we just created... */
1307 if (res
->iov
[i
].iov_base
) {
1308 dma_memory_unmap(VIRTIO_DEVICE(g
)->dma_as
,
1309 res
->iov
[i
].iov_base
,
1311 DMA_DIRECTION_TO_DEVICE
,
1314 /* ...and the mappings for previous loop iterations */
1316 virtio_gpu_cleanup_mapping(g
, res
);
1317 pixman_image_unref(res
->image
);
1323 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
1324 g
->hostmem
+= res
->hostmem
;
1326 resource_id
= qemu_get_be32(f
);
1329 /* load & apply scanout state */
1330 vmstate_load_state(f
, &vmstate_virtio_gpu_scanouts
, g
, 1);
1331 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
1332 /* FIXME: should take scanout.r.{x,y} into account */
1333 scanout
= &g
->parent_obj
.scanout
[i
];
1334 if (!scanout
->resource_id
) {
1337 res
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
1341 scanout
->ds
= qemu_create_displaysurface_pixman(res
->image
);
1346 qemu_displaysurface_win32_set_handle(scanout
->ds
, res
->handle
, 0);
1349 dpy_gfx_replace_surface(scanout
->con
, scanout
->ds
);
1350 dpy_gfx_update_full(scanout
->con
);
1351 if (scanout
->cursor
.resource_id
) {
1352 update_cursor(g
, &scanout
->cursor
);
1354 res
->scanout_bitmask
|= (1 << i
);
1360 void virtio_gpu_device_realize(DeviceState
*qdev
, Error
**errp
)
1362 VirtIODevice
*vdev
= VIRTIO_DEVICE(qdev
);
1363 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
1365 if (virtio_gpu_blob_enabled(g
->parent_obj
.conf
)) {
1366 if (!virtio_gpu_have_udmabuf()) {
1367 error_setg(errp
, "cannot enable blob resources without udmabuf");
1371 if (virtio_gpu_virgl_enabled(g
->parent_obj
.conf
)) {
1372 error_setg(errp
, "blobs and virgl are not compatible (yet)");
1377 if (!virtio_gpu_base_device_realize(qdev
,
1378 virtio_gpu_handle_ctrl_cb
,
1379 virtio_gpu_handle_cursor_cb
,
1384 g
->ctrl_vq
= virtio_get_queue(vdev
, 0);
1385 g
->cursor_vq
= virtio_get_queue(vdev
, 1);
1386 g
->ctrl_bh
= qemu_bh_new_guarded(virtio_gpu_ctrl_bh
, g
,
1387 &qdev
->mem_reentrancy_guard
);
1388 g
->cursor_bh
= qemu_bh_new_guarded(virtio_gpu_cursor_bh
, g
,
1389 &qdev
->mem_reentrancy_guard
);
1390 QTAILQ_INIT(&g
->reslist
);
1391 QTAILQ_INIT(&g
->cmdq
);
1392 QTAILQ_INIT(&g
->fenceq
);
1395 void virtio_gpu_reset(VirtIODevice
*vdev
)
1397 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1398 struct virtio_gpu_simple_resource
*res
, *tmp
;
1399 struct virtio_gpu_ctrl_command
*cmd
;
1402 QTAILQ_FOREACH_SAFE(res
, &g
->reslist
, next
, tmp
) {
1403 virtio_gpu_resource_destroy(g
, res
);
1406 while (!QTAILQ_EMPTY(&g
->cmdq
)) {
1407 cmd
= QTAILQ_FIRST(&g
->cmdq
);
1408 QTAILQ_REMOVE(&g
->cmdq
, cmd
, next
);
1412 while (!QTAILQ_EMPTY(&g
->fenceq
)) {
1413 cmd
= QTAILQ_FIRST(&g
->fenceq
);
1414 QTAILQ_REMOVE(&g
->fenceq
, cmd
, next
);
1419 for (i
= 0; i
< g
->parent_obj
.conf
.max_outputs
; i
++) {
1420 dpy_gfx_replace_surface(g
->parent_obj
.scanout
[i
].con
, NULL
);
1423 virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev
));
1427 virtio_gpu_get_config(VirtIODevice
*vdev
, uint8_t *config
)
1429 VirtIOGPUBase
*g
= VIRTIO_GPU_BASE(vdev
);
1431 memcpy(config
, &g
->virtio_config
, sizeof(g
->virtio_config
));
1435 virtio_gpu_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
1437 VirtIOGPUBase
*g
= VIRTIO_GPU_BASE(vdev
);
1438 const struct virtio_gpu_config
*vgconfig
=
1439 (const struct virtio_gpu_config
*)config
;
1441 if (vgconfig
->events_clear
) {
1442 g
->virtio_config
.events_read
&= ~vgconfig
->events_clear
;
1447 * For historical reasons virtio_gpu does not adhere to virtio migration
1448 * scheme as described in doc/virtio-migration.txt, in a sense that no
1449 * save/load callback are provided to the core. Instead the device data
1450 * is saved/loaded after the core data.
1452 * Because of this we need a special vmsd.
1454 static const VMStateDescription vmstate_virtio_gpu
= {
1455 .name
= "virtio-gpu",
1456 .minimum_version_id
= VIRTIO_GPU_VM_VERSION
,
1457 .version_id
= VIRTIO_GPU_VM_VERSION
,
1458 .fields
= (VMStateField
[]) {
1459 VMSTATE_VIRTIO_DEVICE
/* core */,
1461 .name
= "virtio-gpu",
1462 .info
= &(const VMStateInfo
) {
1463 .name
= "virtio-gpu",
1464 .get
= virtio_gpu_load
,
1465 .put
= virtio_gpu_save
,
1467 .flags
= VMS_SINGLE
,
1469 VMSTATE_END_OF_LIST()
1473 static Property virtio_gpu_properties
[] = {
1474 VIRTIO_GPU_BASE_PROPERTIES(VirtIOGPU
, parent_obj
.conf
),
1475 DEFINE_PROP_SIZE("max_hostmem", VirtIOGPU
, conf_max_hostmem
,
1477 DEFINE_PROP_BIT("blob", VirtIOGPU
, parent_obj
.conf
.flags
,
1478 VIRTIO_GPU_FLAG_BLOB_ENABLED
, false),
1479 DEFINE_PROP_END_OF_LIST(),
1482 static void virtio_gpu_class_init(ObjectClass
*klass
, void *data
)
1484 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1485 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1486 VirtIOGPUClass
*vgc
= VIRTIO_GPU_CLASS(klass
);
1487 VirtIOGPUBaseClass
*vgbc
= &vgc
->parent
;
1489 vgc
->handle_ctrl
= virtio_gpu_handle_ctrl
;
1490 vgc
->process_cmd
= virtio_gpu_simple_process_cmd
;
1491 vgc
->update_cursor_data
= virtio_gpu_update_cursor_data
;
1492 vgbc
->gl_flushed
= virtio_gpu_handle_gl_flushed
;
1494 vdc
->realize
= virtio_gpu_device_realize
;
1495 vdc
->reset
= virtio_gpu_reset
;
1496 vdc
->get_config
= virtio_gpu_get_config
;
1497 vdc
->set_config
= virtio_gpu_set_config
;
1499 dc
->vmsd
= &vmstate_virtio_gpu
;
1500 device_class_set_props(dc
, virtio_gpu_properties
);
1503 static const TypeInfo virtio_gpu_info
= {
1504 .name
= TYPE_VIRTIO_GPU
,
1505 .parent
= TYPE_VIRTIO_GPU_BASE
,
1506 .instance_size
= sizeof(VirtIOGPU
),
1507 .class_size
= sizeof(VirtIOGPUClass
),
1508 .class_init
= virtio_gpu_class_init
,
1510 module_obj(TYPE_VIRTIO_GPU
);
1511 module_kconfig(VIRTIO_GPU
);
1513 static void virtio_register_types(void)
1515 type_register_static(&virtio_gpu_info
);
1518 type_init(virtio_register_types
)