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"
16 #include "qemu-common.h"
18 #include "ui/console.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-gpu.h"
22 #include "hw/virtio/virtio-bus.h"
23 #include "migration/blocker.h"
25 #include "qapi/error.h"
27 #define VIRTIO_GPU_VM_VERSION 1
29 static struct virtio_gpu_simple_resource
*
30 virtio_gpu_find_resource(VirtIOGPU
*g
, uint32_t resource_id
);
32 static void virtio_gpu_cleanup_mapping(VirtIOGPU
*g
,
33 struct virtio_gpu_simple_resource
*res
);
36 virtio_gpu_ctrl_hdr_bswap(struct virtio_gpu_ctrl_hdr
*hdr
)
38 le32_to_cpus(&hdr
->type
);
39 le32_to_cpus(&hdr
->flags
);
40 le64_to_cpus(&hdr
->fence_id
);
41 le32_to_cpus(&hdr
->ctx_id
);
42 le32_to_cpus(&hdr
->padding
);
45 static void virtio_gpu_bswap_32(void *ptr
,
48 #ifdef HOST_WORDS_BIGENDIAN
51 struct virtio_gpu_ctrl_hdr
*hdr
= (struct virtio_gpu_ctrl_hdr
*) ptr
;
53 virtio_gpu_ctrl_hdr_bswap(hdr
);
55 i
= sizeof(struct virtio_gpu_ctrl_hdr
);
57 le32_to_cpus((uint32_t *)(ptr
+ i
));
58 i
= i
+ sizeof(uint32_t);
65 virtio_gpu_t2d_bswap(struct virtio_gpu_transfer_to_host_2d
*t2d
)
67 virtio_gpu_ctrl_hdr_bswap(&t2d
->hdr
);
68 le32_to_cpus(&t2d
->r
.x
);
69 le32_to_cpus(&t2d
->r
.y
);
70 le32_to_cpus(&t2d
->r
.width
);
71 le32_to_cpus(&t2d
->r
.height
);
72 le64_to_cpus(&t2d
->offset
);
73 le32_to_cpus(&t2d
->resource_id
);
74 le32_to_cpus(&t2d
->padding
);
78 #include <virglrenderer.h>
79 #define VIRGL(_g, _virgl, _simple, ...) \
81 if (_g->use_virgl_renderer) { \
82 _virgl(__VA_ARGS__); \
84 _simple(__VA_ARGS__); \
88 #define VIRGL(_g, _virgl, _simple, ...) \
90 _simple(__VA_ARGS__); \
94 static void update_cursor_data_simple(VirtIOGPU
*g
,
95 struct virtio_gpu_scanout
*s
,
98 struct virtio_gpu_simple_resource
*res
;
101 res
= virtio_gpu_find_resource(g
, resource_id
);
106 if (pixman_image_get_width(res
->image
) != s
->current_cursor
->width
||
107 pixman_image_get_height(res
->image
) != s
->current_cursor
->height
) {
111 pixels
= s
->current_cursor
->width
* s
->current_cursor
->height
;
112 memcpy(s
->current_cursor
->data
,
113 pixman_image_get_data(res
->image
),
114 pixels
* sizeof(uint32_t));
119 static void update_cursor_data_virgl(VirtIOGPU
*g
,
120 struct virtio_gpu_scanout
*s
,
121 uint32_t resource_id
)
123 uint32_t width
, height
;
124 uint32_t pixels
, *data
;
126 data
= virgl_renderer_get_cursor_data(resource_id
, &width
, &height
);
131 if (width
!= s
->current_cursor
->width
||
132 height
!= s
->current_cursor
->height
) {
137 pixels
= s
->current_cursor
->width
* s
->current_cursor
->height
;
138 memcpy(s
->current_cursor
->data
, data
, pixels
* sizeof(uint32_t));
144 static void update_cursor(VirtIOGPU
*g
, struct virtio_gpu_update_cursor
*cursor
)
146 struct virtio_gpu_scanout
*s
;
147 bool move
= cursor
->hdr
.type
== VIRTIO_GPU_CMD_MOVE_CURSOR
;
149 if (cursor
->pos
.scanout_id
>= g
->conf
.max_outputs
) {
152 s
= &g
->scanout
[cursor
->pos
.scanout_id
];
154 trace_virtio_gpu_update_cursor(cursor
->pos
.scanout_id
,
157 move
? "move" : "update",
158 cursor
->resource_id
);
161 if (!s
->current_cursor
) {
162 s
->current_cursor
= cursor_alloc(64, 64);
165 s
->current_cursor
->hot_x
= cursor
->hot_x
;
166 s
->current_cursor
->hot_y
= cursor
->hot_y
;
168 if (cursor
->resource_id
> 0) {
169 VIRGL(g
, update_cursor_data_virgl
, update_cursor_data_simple
,
170 g
, s
, cursor
->resource_id
);
172 dpy_cursor_define(s
->con
, s
->current_cursor
);
176 s
->cursor
.pos
.x
= cursor
->pos
.x
;
177 s
->cursor
.pos
.y
= cursor
->pos
.y
;
179 dpy_mouse_set(s
->con
, cursor
->pos
.x
, cursor
->pos
.y
,
180 cursor
->resource_id
? 1 : 0);
183 static void virtio_gpu_get_config(VirtIODevice
*vdev
, uint8_t *config
)
185 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
186 memcpy(config
, &g
->virtio_config
, sizeof(g
->virtio_config
));
189 static void virtio_gpu_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
191 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
192 struct virtio_gpu_config vgconfig
;
194 memcpy(&vgconfig
, config
, sizeof(g
->virtio_config
));
196 if (vgconfig
.events_clear
) {
197 g
->virtio_config
.events_read
&= ~vgconfig
.events_clear
;
201 static uint64_t virtio_gpu_get_features(VirtIODevice
*vdev
, uint64_t features
,
204 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
206 if (virtio_gpu_virgl_enabled(g
->conf
)) {
207 features
|= (1 << VIRTIO_GPU_F_VIRGL
);
212 static void virtio_gpu_set_features(VirtIODevice
*vdev
, uint64_t features
)
214 static const uint32_t virgl
= (1 << VIRTIO_GPU_F_VIRGL
);
215 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
217 g
->use_virgl_renderer
= ((features
& virgl
) == virgl
);
218 trace_virtio_gpu_features(g
->use_virgl_renderer
);
221 static void virtio_gpu_notify_event(VirtIOGPU
*g
, uint32_t event_type
)
223 g
->virtio_config
.events_read
|= event_type
;
224 virtio_notify_config(&g
->parent_obj
);
227 static struct virtio_gpu_simple_resource
*
228 virtio_gpu_find_resource(VirtIOGPU
*g
, uint32_t resource_id
)
230 struct virtio_gpu_simple_resource
*res
;
232 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
233 if (res
->resource_id
== resource_id
) {
240 void virtio_gpu_ctrl_response(VirtIOGPU
*g
,
241 struct virtio_gpu_ctrl_command
*cmd
,
242 struct virtio_gpu_ctrl_hdr
*resp
,
247 if (cmd
->cmd_hdr
.flags
& VIRTIO_GPU_FLAG_FENCE
) {
248 resp
->flags
|= VIRTIO_GPU_FLAG_FENCE
;
249 resp
->fence_id
= cmd
->cmd_hdr
.fence_id
;
250 resp
->ctx_id
= cmd
->cmd_hdr
.ctx_id
;
252 virtio_gpu_ctrl_hdr_bswap(resp
);
253 s
= iov_from_buf(cmd
->elem
.in_sg
, cmd
->elem
.in_num
, 0, resp
, resp_len
);
255 qemu_log_mask(LOG_GUEST_ERROR
,
256 "%s: response size incorrect %zu vs %zu\n",
257 __func__
, s
, resp_len
);
259 virtqueue_push(cmd
->vq
, &cmd
->elem
, s
);
260 virtio_notify(VIRTIO_DEVICE(g
), cmd
->vq
);
261 cmd
->finished
= true;
264 void virtio_gpu_ctrl_response_nodata(VirtIOGPU
*g
,
265 struct virtio_gpu_ctrl_command
*cmd
,
266 enum virtio_gpu_ctrl_type type
)
268 struct virtio_gpu_ctrl_hdr resp
;
270 memset(&resp
, 0, sizeof(resp
));
272 virtio_gpu_ctrl_response(g
, cmd
, &resp
, sizeof(resp
));
276 virtio_gpu_fill_display_info(VirtIOGPU
*g
,
277 struct virtio_gpu_resp_display_info
*dpy_info
)
281 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
282 if (g
->enabled_output_bitmask
& (1 << i
)) {
283 dpy_info
->pmodes
[i
].enabled
= 1;
284 dpy_info
->pmodes
[i
].r
.width
= cpu_to_le32(g
->req_state
[i
].width
);
285 dpy_info
->pmodes
[i
].r
.height
= cpu_to_le32(g
->req_state
[i
].height
);
290 void virtio_gpu_get_display_info(VirtIOGPU
*g
,
291 struct virtio_gpu_ctrl_command
*cmd
)
293 struct virtio_gpu_resp_display_info display_info
;
295 trace_virtio_gpu_cmd_get_display_info();
296 memset(&display_info
, 0, sizeof(display_info
));
297 display_info
.hdr
.type
= VIRTIO_GPU_RESP_OK_DISPLAY_INFO
;
298 virtio_gpu_fill_display_info(g
, &display_info
);
299 virtio_gpu_ctrl_response(g
, cmd
, &display_info
.hdr
,
300 sizeof(display_info
));
303 static pixman_format_code_t
get_pixman_format(uint32_t virtio_gpu_format
)
305 switch (virtio_gpu_format
) {
306 case VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM
:
307 return PIXMAN_BE_b8g8r8x8
;
308 case VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM
:
309 return PIXMAN_BE_b8g8r8a8
;
310 case VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM
:
311 return PIXMAN_BE_x8r8g8b8
;
312 case VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM
:
313 return PIXMAN_BE_a8r8g8b8
;
314 case VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM
:
315 return PIXMAN_BE_r8g8b8x8
;
316 case VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM
:
317 return PIXMAN_BE_r8g8b8a8
;
318 case VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM
:
319 return PIXMAN_BE_x8b8g8r8
;
320 case VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM
:
321 return PIXMAN_BE_a8b8g8r8
;
327 static uint32_t calc_image_hostmem(pixman_format_code_t pformat
,
328 uint32_t width
, uint32_t height
)
330 /* Copied from pixman/pixman-bits-image.c, skip integer overflow check.
331 * pixman_image_create_bits will fail in case it overflow.
334 int bpp
= PIXMAN_FORMAT_BPP(pformat
);
335 int stride
= ((width
* bpp
+ 0x1f) >> 5) * sizeof(uint32_t);
336 return height
* stride
;
339 static void virtio_gpu_resource_create_2d(VirtIOGPU
*g
,
340 struct virtio_gpu_ctrl_command
*cmd
)
342 pixman_format_code_t pformat
;
343 struct virtio_gpu_simple_resource
*res
;
344 struct virtio_gpu_resource_create_2d c2d
;
346 VIRTIO_GPU_FILL_CMD(c2d
);
347 virtio_gpu_bswap_32(&c2d
, sizeof(c2d
));
348 trace_virtio_gpu_cmd_res_create_2d(c2d
.resource_id
, c2d
.format
,
349 c2d
.width
, c2d
.height
);
351 if (c2d
.resource_id
== 0) {
352 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource id 0 is not allowed\n",
354 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
358 res
= virtio_gpu_find_resource(g
, c2d
.resource_id
);
360 qemu_log_mask(LOG_GUEST_ERROR
, "%s: resource already exists %d\n",
361 __func__
, c2d
.resource_id
);
362 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
366 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
368 res
->width
= c2d
.width
;
369 res
->height
= c2d
.height
;
370 res
->format
= c2d
.format
;
371 res
->resource_id
= c2d
.resource_id
;
373 pformat
= get_pixman_format(c2d
.format
);
375 qemu_log_mask(LOG_GUEST_ERROR
,
376 "%s: host couldn't handle guest format %d\n",
377 __func__
, c2d
.format
);
379 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
383 res
->hostmem
= calc_image_hostmem(pformat
, c2d
.width
, c2d
.height
);
384 if (res
->hostmem
+ g
->hostmem
< g
->conf
.max_hostmem
) {
385 res
->image
= pixman_image_create_bits(pformat
,
392 qemu_log_mask(LOG_GUEST_ERROR
,
393 "%s: resource creation failed %d %d %d\n",
394 __func__
, c2d
.resource_id
, c2d
.width
, c2d
.height
);
396 cmd
->error
= VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY
;
400 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
401 g
->hostmem
+= res
->hostmem
;
404 static void virtio_gpu_disable_scanout(VirtIOGPU
*g
, int scanout_id
)
406 struct virtio_gpu_scanout
*scanout
= &g
->scanout
[scanout_id
];
407 struct virtio_gpu_simple_resource
*res
;
408 DisplaySurface
*ds
= NULL
;
410 if (scanout
->resource_id
== 0) {
414 res
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
416 res
->scanout_bitmask
&= ~(1 << scanout_id
);
419 if (scanout_id
== 0) {
421 ds
= qemu_create_message_surface(scanout
->width
?: 640,
422 scanout
->height
?: 480,
423 "Guest disabled display.");
425 dpy_gfx_replace_surface(scanout
->con
, ds
);
426 scanout
->resource_id
= 0;
432 static void virtio_gpu_resource_destroy(VirtIOGPU
*g
,
433 struct virtio_gpu_simple_resource
*res
)
437 if (res
->scanout_bitmask
) {
438 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
439 if (res
->scanout_bitmask
& (1 << i
)) {
440 virtio_gpu_disable_scanout(g
, i
);
445 pixman_image_unref(res
->image
);
446 virtio_gpu_cleanup_mapping(g
, res
);
447 QTAILQ_REMOVE(&g
->reslist
, res
, next
);
448 g
->hostmem
-= res
->hostmem
;
452 static void virtio_gpu_resource_unref(VirtIOGPU
*g
,
453 struct virtio_gpu_ctrl_command
*cmd
)
455 struct virtio_gpu_simple_resource
*res
;
456 struct virtio_gpu_resource_unref unref
;
458 VIRTIO_GPU_FILL_CMD(unref
);
459 virtio_gpu_bswap_32(&unref
, sizeof(unref
));
460 trace_virtio_gpu_cmd_res_unref(unref
.resource_id
);
462 res
= virtio_gpu_find_resource(g
, unref
.resource_id
);
464 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
465 __func__
, unref
.resource_id
);
466 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
469 virtio_gpu_resource_destroy(g
, res
);
472 static void virtio_gpu_transfer_to_host_2d(VirtIOGPU
*g
,
473 struct virtio_gpu_ctrl_command
*cmd
)
475 struct virtio_gpu_simple_resource
*res
;
477 uint32_t src_offset
, dst_offset
, stride
;
479 pixman_format_code_t format
;
480 struct virtio_gpu_transfer_to_host_2d t2d
;
482 VIRTIO_GPU_FILL_CMD(t2d
);
483 virtio_gpu_t2d_bswap(&t2d
);
484 trace_virtio_gpu_cmd_res_xfer_toh_2d(t2d
.resource_id
);
486 res
= virtio_gpu_find_resource(g
, t2d
.resource_id
);
487 if (!res
|| !res
->iov
) {
488 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
489 __func__
, t2d
.resource_id
);
490 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
494 if (t2d
.r
.x
> res
->width
||
495 t2d
.r
.y
> res
->height
||
496 t2d
.r
.width
> res
->width
||
497 t2d
.r
.height
> res
->height
||
498 t2d
.r
.x
+ t2d
.r
.width
> res
->width
||
499 t2d
.r
.y
+ t2d
.r
.height
> res
->height
) {
500 qemu_log_mask(LOG_GUEST_ERROR
, "%s: transfer bounds outside resource"
501 " bounds for resource %d: %d %d %d %d vs %d %d\n",
502 __func__
, t2d
.resource_id
, t2d
.r
.x
, t2d
.r
.y
,
503 t2d
.r
.width
, t2d
.r
.height
, res
->width
, res
->height
);
504 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
508 format
= pixman_image_get_format(res
->image
);
509 bpp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format
), 8);
510 stride
= pixman_image_get_stride(res
->image
);
512 if (t2d
.offset
|| t2d
.r
.x
|| t2d
.r
.y
||
513 t2d
.r
.width
!= pixman_image_get_width(res
->image
)) {
514 void *img_data
= pixman_image_get_data(res
->image
);
515 for (h
= 0; h
< t2d
.r
.height
; h
++) {
516 src_offset
= t2d
.offset
+ stride
* h
;
517 dst_offset
= (t2d
.r
.y
+ h
) * stride
+ (t2d
.r
.x
* bpp
);
519 iov_to_buf(res
->iov
, res
->iov_cnt
, src_offset
,
521 + dst_offset
, t2d
.r
.width
* bpp
);
524 iov_to_buf(res
->iov
, res
->iov_cnt
, 0,
525 pixman_image_get_data(res
->image
),
526 pixman_image_get_stride(res
->image
)
527 * pixman_image_get_height(res
->image
));
531 static void virtio_gpu_resource_flush(VirtIOGPU
*g
,
532 struct virtio_gpu_ctrl_command
*cmd
)
534 struct virtio_gpu_simple_resource
*res
;
535 struct virtio_gpu_resource_flush rf
;
536 pixman_region16_t flush_region
;
539 VIRTIO_GPU_FILL_CMD(rf
);
540 virtio_gpu_bswap_32(&rf
, sizeof(rf
));
541 trace_virtio_gpu_cmd_res_flush(rf
.resource_id
,
542 rf
.r
.width
, rf
.r
.height
, rf
.r
.x
, rf
.r
.y
);
544 res
= virtio_gpu_find_resource(g
, rf
.resource_id
);
546 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
547 __func__
, rf
.resource_id
);
548 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
552 if (rf
.r
.x
> res
->width
||
553 rf
.r
.y
> res
->height
||
554 rf
.r
.width
> res
->width
||
555 rf
.r
.height
> res
->height
||
556 rf
.r
.x
+ rf
.r
.width
> res
->width
||
557 rf
.r
.y
+ rf
.r
.height
> res
->height
) {
558 qemu_log_mask(LOG_GUEST_ERROR
, "%s: flush bounds outside resource"
559 " bounds for resource %d: %d %d %d %d vs %d %d\n",
560 __func__
, rf
.resource_id
, rf
.r
.x
, rf
.r
.y
,
561 rf
.r
.width
, rf
.r
.height
, res
->width
, res
->height
);
562 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
566 pixman_region_init_rect(&flush_region
,
567 rf
.r
.x
, rf
.r
.y
, rf
.r
.width
, rf
.r
.height
);
568 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
569 struct virtio_gpu_scanout
*scanout
;
570 pixman_region16_t region
, finalregion
;
571 pixman_box16_t
*extents
;
573 if (!(res
->scanout_bitmask
& (1 << i
))) {
576 scanout
= &g
->scanout
[i
];
578 pixman_region_init(&finalregion
);
579 pixman_region_init_rect(®ion
, scanout
->x
, scanout
->y
,
580 scanout
->width
, scanout
->height
);
582 pixman_region_intersect(&finalregion
, &flush_region
, ®ion
);
583 pixman_region_translate(&finalregion
, -scanout
->x
, -scanout
->y
);
584 extents
= pixman_region_extents(&finalregion
);
585 /* work out the area we need to update for each console */
586 dpy_gfx_update(g
->scanout
[i
].con
,
587 extents
->x1
, extents
->y1
,
588 extents
->x2
- extents
->x1
,
589 extents
->y2
- extents
->y1
);
591 pixman_region_fini(®ion
);
592 pixman_region_fini(&finalregion
);
594 pixman_region_fini(&flush_region
);
597 static void virtio_unref_resource(pixman_image_t
*image
, void *data
)
599 pixman_image_unref(data
);
602 static void virtio_gpu_set_scanout(VirtIOGPU
*g
,
603 struct virtio_gpu_ctrl_command
*cmd
)
605 struct virtio_gpu_simple_resource
*res
, *ores
;
606 struct virtio_gpu_scanout
*scanout
;
607 pixman_format_code_t format
;
610 struct virtio_gpu_set_scanout ss
;
612 VIRTIO_GPU_FILL_CMD(ss
);
613 virtio_gpu_bswap_32(&ss
, sizeof(ss
));
614 trace_virtio_gpu_cmd_set_scanout(ss
.scanout_id
, ss
.resource_id
,
615 ss
.r
.width
, ss
.r
.height
, ss
.r
.x
, ss
.r
.y
);
617 if (ss
.scanout_id
>= g
->conf
.max_outputs
) {
618 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout id specified %d",
619 __func__
, ss
.scanout_id
);
620 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID
;
625 if (ss
.resource_id
== 0) {
626 virtio_gpu_disable_scanout(g
, ss
.scanout_id
);
630 /* create a surface for this scanout */
631 res
= virtio_gpu_find_resource(g
, ss
.resource_id
);
633 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
634 __func__
, ss
.resource_id
);
635 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
639 if (ss
.r
.x
> res
->width
||
640 ss
.r
.y
> res
->height
||
641 ss
.r
.width
> res
->width
||
642 ss
.r
.height
> res
->height
||
643 ss
.r
.x
+ ss
.r
.width
> res
->width
||
644 ss
.r
.y
+ ss
.r
.height
> res
->height
) {
645 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal scanout %d bounds for"
646 " resource %d, (%d,%d)+%d,%d vs %d %d\n",
647 __func__
, ss
.scanout_id
, ss
.resource_id
, ss
.r
.x
, ss
.r
.y
,
648 ss
.r
.width
, ss
.r
.height
, res
->width
, res
->height
);
649 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
;
653 scanout
= &g
->scanout
[ss
.scanout_id
];
655 format
= pixman_image_get_format(res
->image
);
656 bpp
= DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format
), 8);
657 offset
= (ss
.r
.x
* bpp
) + ss
.r
.y
* pixman_image_get_stride(res
->image
);
658 if (!scanout
->ds
|| surface_data(scanout
->ds
)
659 != ((uint8_t *)pixman_image_get_data(res
->image
) + offset
) ||
660 scanout
->width
!= ss
.r
.width
||
661 scanout
->height
!= ss
.r
.height
) {
662 pixman_image_t
*rect
;
663 void *ptr
= (uint8_t *)pixman_image_get_data(res
->image
) + offset
;
664 rect
= pixman_image_create_bits(format
, ss
.r
.width
, ss
.r
.height
, ptr
,
665 pixman_image_get_stride(res
->image
));
666 pixman_image_ref(res
->image
);
667 pixman_image_set_destroy_function(rect
, virtio_unref_resource
,
669 /* realloc the surface ptr */
670 scanout
->ds
= qemu_create_displaysurface_pixman(rect
);
672 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
675 pixman_image_unref(rect
);
676 dpy_gfx_replace_surface(g
->scanout
[ss
.scanout_id
].con
, scanout
->ds
);
679 ores
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
681 ores
->scanout_bitmask
&= ~(1 << ss
.scanout_id
);
684 res
->scanout_bitmask
|= (1 << ss
.scanout_id
);
685 scanout
->resource_id
= ss
.resource_id
;
688 scanout
->width
= ss
.r
.width
;
689 scanout
->height
= ss
.r
.height
;
692 int virtio_gpu_create_mapping_iov(VirtIOGPU
*g
,
693 struct virtio_gpu_resource_attach_backing
*ab
,
694 struct virtio_gpu_ctrl_command
*cmd
,
695 uint64_t **addr
, struct iovec
**iov
)
697 struct virtio_gpu_mem_entry
*ents
;
701 if (ab
->nr_entries
> 16384) {
702 qemu_log_mask(LOG_GUEST_ERROR
,
703 "%s: nr_entries is too big (%d > 16384)\n",
704 __func__
, ab
->nr_entries
);
708 esize
= sizeof(*ents
) * ab
->nr_entries
;
709 ents
= g_malloc(esize
);
710 s
= iov_to_buf(cmd
->elem
.out_sg
, cmd
->elem
.out_num
,
711 sizeof(*ab
), ents
, esize
);
713 qemu_log_mask(LOG_GUEST_ERROR
,
714 "%s: command data size incorrect %zu vs %zu\n",
720 *iov
= g_malloc0(sizeof(struct iovec
) * ab
->nr_entries
);
722 *addr
= g_malloc0(sizeof(uint64_t) * ab
->nr_entries
);
724 for (i
= 0; i
< ab
->nr_entries
; i
++) {
725 uint64_t a
= le64_to_cpu(ents
[i
].addr
);
726 uint32_t l
= le32_to_cpu(ents
[i
].length
);
728 (*iov
)[i
].iov_len
= l
;
729 (*iov
)[i
].iov_base
= cpu_physical_memory_map(a
, &len
, 1);
733 if (!(*iov
)[i
].iov_base
|| len
!= l
) {
734 qemu_log_mask(LOG_GUEST_ERROR
, "%s: failed to map MMIO memory for"
735 " resource %d element %d\n",
736 __func__
, ab
->resource_id
, i
);
737 virtio_gpu_cleanup_mapping_iov(g
, *iov
, i
);
751 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU
*g
,
752 struct iovec
*iov
, uint32_t count
)
756 for (i
= 0; i
< count
; i
++) {
757 cpu_physical_memory_unmap(iov
[i
].iov_base
, iov
[i
].iov_len
, 1,
763 static void virtio_gpu_cleanup_mapping(VirtIOGPU
*g
,
764 struct virtio_gpu_simple_resource
*res
)
766 virtio_gpu_cleanup_mapping_iov(g
, res
->iov
, res
->iov_cnt
);
774 virtio_gpu_resource_attach_backing(VirtIOGPU
*g
,
775 struct virtio_gpu_ctrl_command
*cmd
)
777 struct virtio_gpu_simple_resource
*res
;
778 struct virtio_gpu_resource_attach_backing ab
;
781 VIRTIO_GPU_FILL_CMD(ab
);
782 virtio_gpu_bswap_32(&ab
, sizeof(ab
));
783 trace_virtio_gpu_cmd_res_back_attach(ab
.resource_id
);
785 res
= virtio_gpu_find_resource(g
, ab
.resource_id
);
787 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
788 __func__
, ab
.resource_id
);
789 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
794 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
798 ret
= virtio_gpu_create_mapping_iov(g
, &ab
, cmd
, &res
->addrs
, &res
->iov
);
800 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
804 res
->iov_cnt
= ab
.nr_entries
;
808 virtio_gpu_resource_detach_backing(VirtIOGPU
*g
,
809 struct virtio_gpu_ctrl_command
*cmd
)
811 struct virtio_gpu_simple_resource
*res
;
812 struct virtio_gpu_resource_detach_backing detach
;
814 VIRTIO_GPU_FILL_CMD(detach
);
815 virtio_gpu_bswap_32(&detach
, sizeof(detach
));
816 trace_virtio_gpu_cmd_res_back_detach(detach
.resource_id
);
818 res
= virtio_gpu_find_resource(g
, detach
.resource_id
);
819 if (!res
|| !res
->iov
) {
820 qemu_log_mask(LOG_GUEST_ERROR
, "%s: illegal resource specified %d\n",
821 __func__
, detach
.resource_id
);
822 cmd
->error
= VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID
;
825 virtio_gpu_cleanup_mapping(g
, res
);
828 static void virtio_gpu_simple_process_cmd(VirtIOGPU
*g
,
829 struct virtio_gpu_ctrl_command
*cmd
)
831 VIRTIO_GPU_FILL_CMD(cmd
->cmd_hdr
);
832 virtio_gpu_ctrl_hdr_bswap(&cmd
->cmd_hdr
);
834 switch (cmd
->cmd_hdr
.type
) {
835 case VIRTIO_GPU_CMD_GET_DISPLAY_INFO
:
836 virtio_gpu_get_display_info(g
, cmd
);
838 case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D
:
839 virtio_gpu_resource_create_2d(g
, cmd
);
841 case VIRTIO_GPU_CMD_RESOURCE_UNREF
:
842 virtio_gpu_resource_unref(g
, cmd
);
844 case VIRTIO_GPU_CMD_RESOURCE_FLUSH
:
845 virtio_gpu_resource_flush(g
, cmd
);
847 case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D
:
848 virtio_gpu_transfer_to_host_2d(g
, cmd
);
850 case VIRTIO_GPU_CMD_SET_SCANOUT
:
851 virtio_gpu_set_scanout(g
, cmd
);
853 case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
:
854 virtio_gpu_resource_attach_backing(g
, cmd
);
856 case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING
:
857 virtio_gpu_resource_detach_backing(g
, cmd
);
860 cmd
->error
= VIRTIO_GPU_RESP_ERR_UNSPEC
;
863 if (!cmd
->finished
) {
864 virtio_gpu_ctrl_response_nodata(g
, cmd
, cmd
->error
? cmd
->error
:
865 VIRTIO_GPU_RESP_OK_NODATA
);
869 static void virtio_gpu_handle_ctrl_cb(VirtIODevice
*vdev
, VirtQueue
*vq
)
871 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
872 qemu_bh_schedule(g
->ctrl_bh
);
875 static void virtio_gpu_handle_cursor_cb(VirtIODevice
*vdev
, VirtQueue
*vq
)
877 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
878 qemu_bh_schedule(g
->cursor_bh
);
881 void virtio_gpu_process_cmdq(VirtIOGPU
*g
)
883 struct virtio_gpu_ctrl_command
*cmd
;
885 while (!QTAILQ_EMPTY(&g
->cmdq
)) {
886 cmd
= QTAILQ_FIRST(&g
->cmdq
);
888 /* process command */
889 VIRGL(g
, virtio_gpu_virgl_process_cmd
, virtio_gpu_simple_process_cmd
,
894 QTAILQ_REMOVE(&g
->cmdq
, cmd
, next
);
895 if (virtio_gpu_stats_enabled(g
->conf
)) {
899 if (!cmd
->finished
) {
900 QTAILQ_INSERT_TAIL(&g
->fenceq
, cmd
, next
);
902 if (virtio_gpu_stats_enabled(g
->conf
)) {
903 if (g
->stats
.max_inflight
< g
->inflight
) {
904 g
->stats
.max_inflight
= g
->inflight
;
906 fprintf(stderr
, "inflight: %3d (+)\r", g
->inflight
);
914 static void virtio_gpu_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
916 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
917 struct virtio_gpu_ctrl_command
*cmd
;
919 if (!virtio_queue_ready(vq
)) {
924 if (!g
->renderer_inited
&& g
->use_virgl_renderer
) {
925 virtio_gpu_virgl_init(g
);
926 g
->renderer_inited
= true;
930 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
934 cmd
->finished
= false;
935 cmd
->waiting
= false;
936 QTAILQ_INSERT_TAIL(&g
->cmdq
, cmd
, next
);
937 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
940 virtio_gpu_process_cmdq(g
);
943 if (g
->use_virgl_renderer
) {
944 virtio_gpu_virgl_fence_poll(g
);
949 static void virtio_gpu_ctrl_bh(void *opaque
)
951 VirtIOGPU
*g
= opaque
;
952 virtio_gpu_handle_ctrl(&g
->parent_obj
, g
->ctrl_vq
);
955 static void virtio_gpu_handle_cursor(VirtIODevice
*vdev
, VirtQueue
*vq
)
957 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
958 VirtQueueElement
*elem
;
960 struct virtio_gpu_update_cursor cursor_info
;
962 if (!virtio_queue_ready(vq
)) {
966 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
971 s
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
972 &cursor_info
, sizeof(cursor_info
));
973 if (s
!= sizeof(cursor_info
)) {
974 qemu_log_mask(LOG_GUEST_ERROR
,
975 "%s: cursor size incorrect %zu vs %zu\n",
976 __func__
, s
, sizeof(cursor_info
));
978 virtio_gpu_bswap_32(&cursor_info
, sizeof(cursor_info
));
979 update_cursor(g
, &cursor_info
);
981 virtqueue_push(vq
, elem
, 0);
982 virtio_notify(vdev
, vq
);
987 static void virtio_gpu_cursor_bh(void *opaque
)
989 VirtIOGPU
*g
= opaque
;
990 virtio_gpu_handle_cursor(&g
->parent_obj
, g
->cursor_vq
);
993 static void virtio_gpu_invalidate_display(void *opaque
)
997 static void virtio_gpu_update_display(void *opaque
)
1001 static void virtio_gpu_text_update(void *opaque
, console_ch_t
*chardata
)
1005 static int virtio_gpu_ui_info(void *opaque
, uint32_t idx
, QemuUIInfo
*info
)
1007 VirtIOGPU
*g
= opaque
;
1009 if (idx
>= g
->conf
.max_outputs
) {
1013 g
->req_state
[idx
].x
= info
->xoff
;
1014 g
->req_state
[idx
].y
= info
->yoff
;
1015 g
->req_state
[idx
].width
= info
->width
;
1016 g
->req_state
[idx
].height
= info
->height
;
1018 if (info
->width
&& info
->height
) {
1019 g
->enabled_output_bitmask
|= (1 << idx
);
1021 g
->enabled_output_bitmask
&= ~(1 << idx
);
1024 /* send event to guest */
1025 virtio_gpu_notify_event(g
, VIRTIO_GPU_EVENT_DISPLAY
);
1029 const GraphicHwOps virtio_gpu_ops
= {
1030 .invalidate
= virtio_gpu_invalidate_display
,
1031 .gfx_update
= virtio_gpu_update_display
,
1032 .text_update
= virtio_gpu_text_update
,
1033 .ui_info
= virtio_gpu_ui_info
,
1035 .gl_block
= virtio_gpu_gl_block
,
1039 static const VMStateDescription vmstate_virtio_gpu_scanout
= {
1040 .name
= "virtio-gpu-one-scanout",
1042 .fields
= (VMStateField
[]) {
1043 VMSTATE_UINT32(resource_id
, struct virtio_gpu_scanout
),
1044 VMSTATE_UINT32(width
, struct virtio_gpu_scanout
),
1045 VMSTATE_UINT32(height
, struct virtio_gpu_scanout
),
1046 VMSTATE_INT32(x
, struct virtio_gpu_scanout
),
1047 VMSTATE_INT32(y
, struct virtio_gpu_scanout
),
1048 VMSTATE_UINT32(cursor
.resource_id
, struct virtio_gpu_scanout
),
1049 VMSTATE_UINT32(cursor
.hot_x
, struct virtio_gpu_scanout
),
1050 VMSTATE_UINT32(cursor
.hot_y
, struct virtio_gpu_scanout
),
1051 VMSTATE_UINT32(cursor
.pos
.x
, struct virtio_gpu_scanout
),
1052 VMSTATE_UINT32(cursor
.pos
.y
, struct virtio_gpu_scanout
),
1053 VMSTATE_END_OF_LIST()
1057 static const VMStateDescription vmstate_virtio_gpu_scanouts
= {
1058 .name
= "virtio-gpu-scanouts",
1060 .fields
= (VMStateField
[]) {
1061 VMSTATE_INT32(enable
, struct VirtIOGPU
),
1062 VMSTATE_UINT32_EQUAL(conf
.max_outputs
, struct VirtIOGPU
, NULL
),
1063 VMSTATE_STRUCT_VARRAY_UINT32(scanout
, struct VirtIOGPU
,
1064 conf
.max_outputs
, 1,
1065 vmstate_virtio_gpu_scanout
,
1066 struct virtio_gpu_scanout
),
1067 VMSTATE_END_OF_LIST()
1071 static int virtio_gpu_save(QEMUFile
*f
, void *opaque
, size_t size
,
1072 VMStateField
*field
, QJSON
*vmdesc
)
1074 VirtIOGPU
*g
= opaque
;
1075 struct virtio_gpu_simple_resource
*res
;
1078 /* in 2d mode we should never find unprocessed commands here */
1079 assert(QTAILQ_EMPTY(&g
->cmdq
));
1081 QTAILQ_FOREACH(res
, &g
->reslist
, next
) {
1082 qemu_put_be32(f
, res
->resource_id
);
1083 qemu_put_be32(f
, res
->width
);
1084 qemu_put_be32(f
, res
->height
);
1085 qemu_put_be32(f
, res
->format
);
1086 qemu_put_be32(f
, res
->iov_cnt
);
1087 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1088 qemu_put_be64(f
, res
->addrs
[i
]);
1089 qemu_put_be32(f
, res
->iov
[i
].iov_len
);
1091 qemu_put_buffer(f
, (void *)pixman_image_get_data(res
->image
),
1092 pixman_image_get_stride(res
->image
) * res
->height
);
1094 qemu_put_be32(f
, 0); /* end of list */
1096 return vmstate_save_state(f
, &vmstate_virtio_gpu_scanouts
, g
, NULL
);
1099 static int virtio_gpu_load(QEMUFile
*f
, void *opaque
, size_t size
,
1100 VMStateField
*field
)
1102 VirtIOGPU
*g
= opaque
;
1103 struct virtio_gpu_simple_resource
*res
;
1104 struct virtio_gpu_scanout
*scanout
;
1105 uint32_t resource_id
, pformat
;
1110 resource_id
= qemu_get_be32(f
);
1111 while (resource_id
!= 0) {
1112 res
= g_new0(struct virtio_gpu_simple_resource
, 1);
1113 res
->resource_id
= resource_id
;
1114 res
->width
= qemu_get_be32(f
);
1115 res
->height
= qemu_get_be32(f
);
1116 res
->format
= qemu_get_be32(f
);
1117 res
->iov_cnt
= qemu_get_be32(f
);
1120 pformat
= get_pixman_format(res
->format
);
1125 res
->image
= pixman_image_create_bits(pformat
,
1126 res
->width
, res
->height
,
1133 res
->hostmem
= calc_image_hostmem(pformat
, res
->width
, res
->height
);
1135 res
->addrs
= g_new(uint64_t, res
->iov_cnt
);
1136 res
->iov
= g_new(struct iovec
, res
->iov_cnt
);
1139 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1140 res
->addrs
[i
] = qemu_get_be64(f
);
1141 res
->iov
[i
].iov_len
= qemu_get_be32(f
);
1143 qemu_get_buffer(f
, (void *)pixman_image_get_data(res
->image
),
1144 pixman_image_get_stride(res
->image
) * res
->height
);
1146 /* restore mapping */
1147 for (i
= 0; i
< res
->iov_cnt
; i
++) {
1148 hwaddr len
= res
->iov
[i
].iov_len
;
1149 res
->iov
[i
].iov_base
=
1150 cpu_physical_memory_map(res
->addrs
[i
], &len
, 1);
1152 if (!res
->iov
[i
].iov_base
|| len
!= res
->iov
[i
].iov_len
) {
1153 /* Clean up the half-a-mapping we just created... */
1154 if (res
->iov
[i
].iov_base
) {
1155 cpu_physical_memory_unmap(res
->iov
[i
].iov_base
,
1158 /* ...and the mappings for previous loop iterations */
1160 virtio_gpu_cleanup_mapping(g
, res
);
1161 pixman_image_unref(res
->image
);
1167 QTAILQ_INSERT_HEAD(&g
->reslist
, res
, next
);
1168 g
->hostmem
+= res
->hostmem
;
1170 resource_id
= qemu_get_be32(f
);
1173 /* load & apply scanout state */
1174 vmstate_load_state(f
, &vmstate_virtio_gpu_scanouts
, g
, 1);
1175 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
1176 scanout
= &g
->scanout
[i
];
1177 if (!scanout
->resource_id
) {
1180 res
= virtio_gpu_find_resource(g
, scanout
->resource_id
);
1184 scanout
->ds
= qemu_create_displaysurface_pixman(res
->image
);
1189 dpy_gfx_replace_surface(scanout
->con
, scanout
->ds
);
1190 dpy_gfx_update_full(scanout
->con
);
1191 if (scanout
->cursor
.resource_id
) {
1192 update_cursor(g
, &scanout
->cursor
);
1194 res
->scanout_bitmask
|= (1 << i
);
1200 static void virtio_gpu_device_realize(DeviceState
*qdev
, Error
**errp
)
1202 VirtIODevice
*vdev
= VIRTIO_DEVICE(qdev
);
1203 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
1205 Error
*local_err
= NULL
;
1208 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1209 error_setg(errp
, "virtio-gpu does not support vIOMMU yet");
1213 if (g
->conf
.max_outputs
> VIRTIO_GPU_MAX_SCANOUTS
) {
1214 error_setg(errp
, "invalid max_outputs > %d", VIRTIO_GPU_MAX_SCANOUTS
);
1218 g
->use_virgl_renderer
= false;
1219 #if !defined(CONFIG_VIRGL) || defined(HOST_WORDS_BIGENDIAN)
1222 have_virgl
= display_opengl
;
1225 g
->conf
.flags
&= ~(1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED
);
1228 if (virtio_gpu_virgl_enabled(g
->conf
)) {
1229 error_setg(&g
->migration_blocker
, "virgl is not yet migratable");
1230 migrate_add_blocker(g
->migration_blocker
, &local_err
);
1232 error_propagate(errp
, local_err
);
1233 error_free(g
->migration_blocker
);
1238 g
->config_size
= sizeof(struct virtio_gpu_config
);
1239 g
->virtio_config
.num_scanouts
= cpu_to_le32(g
->conf
.max_outputs
);
1240 virtio_init(VIRTIO_DEVICE(g
), "virtio-gpu", VIRTIO_ID_GPU
,
1243 g
->req_state
[0].width
= g
->conf
.xres
;
1244 g
->req_state
[0].height
= g
->conf
.yres
;
1246 if (virtio_gpu_virgl_enabled(g
->conf
)) {
1247 /* use larger control queue in 3d mode */
1248 g
->ctrl_vq
= virtio_add_queue(vdev
, 256, virtio_gpu_handle_ctrl_cb
);
1249 g
->cursor_vq
= virtio_add_queue(vdev
, 16, virtio_gpu_handle_cursor_cb
);
1251 #if defined(CONFIG_VIRGL)
1252 g
->virtio_config
.num_capsets
= virtio_gpu_virgl_get_num_capsets(g
);
1254 g
->virtio_config
.num_capsets
= 0;
1257 g
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_gpu_handle_ctrl_cb
);
1258 g
->cursor_vq
= virtio_add_queue(vdev
, 16, virtio_gpu_handle_cursor_cb
);
1261 g
->ctrl_bh
= qemu_bh_new(virtio_gpu_ctrl_bh
, g
);
1262 g
->cursor_bh
= qemu_bh_new(virtio_gpu_cursor_bh
, g
);
1263 QTAILQ_INIT(&g
->reslist
);
1264 QTAILQ_INIT(&g
->cmdq
);
1265 QTAILQ_INIT(&g
->fenceq
);
1267 g
->enabled_output_bitmask
= 1;
1270 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
1272 graphic_console_init(DEVICE(g
), i
, &virtio_gpu_ops
, g
);
1274 dpy_gfx_replace_surface(g
->scanout
[i
].con
, NULL
);
1279 static void virtio_gpu_device_unrealize(DeviceState
*qdev
, Error
**errp
)
1281 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
1282 if (g
->migration_blocker
) {
1283 migrate_del_blocker(g
->migration_blocker
);
1284 error_free(g
->migration_blocker
);
1288 static void virtio_gpu_instance_init(Object
*obj
)
1292 void virtio_gpu_reset(VirtIODevice
*vdev
)
1294 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
1295 struct virtio_gpu_simple_resource
*res
, *tmp
;
1300 QTAILQ_FOREACH_SAFE(res
, &g
->reslist
, next
, tmp
) {
1301 virtio_gpu_resource_destroy(g
, res
);
1303 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
1304 g
->scanout
[i
].resource_id
= 0;
1305 g
->scanout
[i
].width
= 0;
1306 g
->scanout
[i
].height
= 0;
1307 g
->scanout
[i
].x
= 0;
1308 g
->scanout
[i
].y
= 0;
1309 g
->scanout
[i
].ds
= NULL
;
1313 if (g
->use_virgl_renderer
) {
1314 virtio_gpu_virgl_reset(g
);
1315 g
->use_virgl_renderer
= 0;
1321 * For historical reasons virtio_gpu does not adhere to virtio migration
1322 * scheme as described in doc/virtio-migration.txt, in a sense that no
1323 * save/load callback are provided to the core. Instead the device data
1324 * is saved/loaded after the core data.
1326 * Because of this we need a special vmsd.
1328 static const VMStateDescription vmstate_virtio_gpu
= {
1329 .name
= "virtio-gpu",
1330 .minimum_version_id
= VIRTIO_GPU_VM_VERSION
,
1331 .version_id
= VIRTIO_GPU_VM_VERSION
,
1332 .fields
= (VMStateField
[]) {
1333 VMSTATE_VIRTIO_DEVICE
/* core */,
1335 .name
= "virtio-gpu",
1336 .info
= &(const VMStateInfo
) {
1337 .name
= "virtio-gpu",
1338 .get
= virtio_gpu_load
,
1339 .put
= virtio_gpu_save
,
1341 .flags
= VMS_SINGLE
,
1343 VMSTATE_END_OF_LIST()
1347 static Property virtio_gpu_properties
[] = {
1348 DEFINE_PROP_UINT32("max_outputs", VirtIOGPU
, conf
.max_outputs
, 1),
1349 DEFINE_PROP_SIZE("max_hostmem", VirtIOGPU
, conf
.max_hostmem
, 256 * MiB
),
1351 DEFINE_PROP_BIT("virgl", VirtIOGPU
, conf
.flags
,
1352 VIRTIO_GPU_FLAG_VIRGL_ENABLED
, true),
1353 DEFINE_PROP_BIT("stats", VirtIOGPU
, conf
.flags
,
1354 VIRTIO_GPU_FLAG_STATS_ENABLED
, false),
1356 DEFINE_PROP_UINT32("xres", VirtIOGPU
, conf
.xres
, 1024),
1357 DEFINE_PROP_UINT32("yres", VirtIOGPU
, conf
.yres
, 768),
1358 DEFINE_PROP_END_OF_LIST(),
1361 static void virtio_gpu_class_init(ObjectClass
*klass
, void *data
)
1363 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1364 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1366 vdc
->realize
= virtio_gpu_device_realize
;
1367 vdc
->unrealize
= virtio_gpu_device_unrealize
;
1368 vdc
->get_config
= virtio_gpu_get_config
;
1369 vdc
->set_config
= virtio_gpu_set_config
;
1370 vdc
->get_features
= virtio_gpu_get_features
;
1371 vdc
->set_features
= virtio_gpu_set_features
;
1373 vdc
->reset
= virtio_gpu_reset
;
1375 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
1376 dc
->props
= virtio_gpu_properties
;
1377 dc
->vmsd
= &vmstate_virtio_gpu
;
1378 dc
->hotpluggable
= false;
1381 static const TypeInfo virtio_gpu_info
= {
1382 .name
= TYPE_VIRTIO_GPU
,
1383 .parent
= TYPE_VIRTIO_DEVICE
,
1384 .instance_size
= sizeof(VirtIOGPU
),
1385 .instance_init
= virtio_gpu_instance_init
,
1386 .class_init
= virtio_gpu_class_init
,
1389 static void virtio_register_types(void)
1391 type_register_static(&virtio_gpu_info
);
1394 type_init(virtio_register_types
)
1396 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctrl_hdr
) != 24);
1397 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_update_cursor
) != 56);
1398 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_unref
) != 32);
1399 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_create_2d
) != 40);
1400 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_set_scanout
) != 48);
1401 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_flush
) != 48);
1402 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_transfer_to_host_2d
) != 56);
1403 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_mem_entry
) != 16);
1404 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_attach_backing
) != 32);
1405 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_detach_backing
) != 32);
1406 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resp_display_info
) != 408);
1408 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_transfer_host_3d
) != 72);
1409 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resource_create_3d
) != 72);
1410 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctx_create
) != 96);
1411 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctx_destroy
) != 24);
1412 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_ctx_resource
) != 32);
1413 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_cmd_submit
) != 32);
1414 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_get_capset_info
) != 32);
1415 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resp_capset_info
) != 40);
1416 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_get_capset
) != 32);
1417 QEMU_BUILD_BUG_ON(sizeof(struct virtio_gpu_resp_capset
) != 24);