xref: /qemu/hw/display/virtio-gpu.c (revision 49a99ecb)
162232bf4SGerd Hoffmann /*
262232bf4SGerd Hoffmann  * Virtio GPU Device
362232bf4SGerd Hoffmann  *
462232bf4SGerd Hoffmann  * Copyright Red Hat, Inc. 2013-2014
562232bf4SGerd Hoffmann  *
662232bf4SGerd Hoffmann  * Authors:
762232bf4SGerd Hoffmann  *     Dave Airlie <airlied@redhat.com>
862232bf4SGerd Hoffmann  *     Gerd Hoffmann <kraxel@redhat.com>
962232bf4SGerd Hoffmann  *
102e252145SGerd Hoffmann  * This work is licensed under the terms of the GNU GPL, version 2 or later.
1162232bf4SGerd Hoffmann  * See the COPYING file in the top-level directory.
1262232bf4SGerd Hoffmann  */
1362232bf4SGerd Hoffmann 
149b8bfe21SPeter Maydell #include "qemu/osdep.h"
15f0353b0dSPhilippe Mathieu-Daudé #include "qemu/units.h"
1662232bf4SGerd Hoffmann #include "qemu/iov.h"
1762232bf4SGerd Hoffmann #include "ui/console.h"
1862232bf4SGerd Hoffmann #include "trace.h"
198da132a5SGerd Hoffmann #include "sysemu/dma.h"
202f780b6aSMarkus Armbruster #include "sysemu/sysemu.h"
2162232bf4SGerd Hoffmann #include "hw/virtio/virtio.h"
22ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h"
2362232bf4SGerd Hoffmann #include "hw/virtio/virtio-gpu.h"
24ad08e67aSMarc-André Lureau #include "hw/virtio/virtio-gpu-bswap.h"
2583a7d3c0SMarc-André Lureau #include "hw/virtio/virtio-gpu-pixman.h"
2662232bf4SGerd Hoffmann #include "hw/virtio/virtio-bus.h"
271ed2cb32SGerd Hoffmann #include "hw/display/edid.h"
28a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
2903dd024fSPaolo Bonzini #include "qemu/log.h"
300b8fa32fSMarkus Armbruster #include "qemu/module.h"
315e3d741cSMarc-André Lureau #include "qapi/error.h"
3250d8e25eSMarc-André Lureau #include "qemu/error-report.h"
3362232bf4SGerd Hoffmann 
340c244e50SGerd Hoffmann #define VIRTIO_GPU_VM_VERSION 1
350c244e50SGerd Hoffmann 
3662232bf4SGerd Hoffmann static struct virtio_gpu_simple_resource*
3762232bf4SGerd Hoffmann virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id);
3825c001a4SVivek Kasireddy static struct virtio_gpu_simple_resource *
3925c001a4SVivek Kasireddy virtio_gpu_find_check_resource(VirtIOGPU *g, uint32_t resource_id,
4025c001a4SVivek Kasireddy                                bool require_backing,
4125c001a4SVivek Kasireddy                                const char *caller, uint32_t *error);
4262232bf4SGerd Hoffmann 
433bb68f79SGerd Hoffmann static void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
443bb68f79SGerd Hoffmann                                        struct virtio_gpu_simple_resource *res);
45b8e23926SLi Qiang 
462c267d66SGerd Hoffmann void virtio_gpu_update_cursor_data(VirtIOGPU *g,
4762232bf4SGerd Hoffmann                                    struct virtio_gpu_scanout *s,
4862232bf4SGerd Hoffmann                                    uint32_t resource_id)
4962232bf4SGerd Hoffmann {
5062232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
5162232bf4SGerd Hoffmann     uint32_t pixels;
52bdd53f73SVivek Kasireddy     void *data;
5362232bf4SGerd Hoffmann 
5425c001a4SVivek Kasireddy     res = virtio_gpu_find_check_resource(g, resource_id, false,
5525c001a4SVivek Kasireddy                                          __func__, NULL);
5662232bf4SGerd Hoffmann     if (!res) {
5762232bf4SGerd Hoffmann         return;
5862232bf4SGerd Hoffmann     }
5962232bf4SGerd Hoffmann 
60bdd53f73SVivek Kasireddy     if (res->blob_size) {
61bdd53f73SVivek Kasireddy         if (res->blob_size < (s->current_cursor->width *
62bdd53f73SVivek Kasireddy                               s->current_cursor->height * 4)) {
63bdd53f73SVivek Kasireddy             return;
64bdd53f73SVivek Kasireddy         }
65bdd53f73SVivek Kasireddy         data = res->blob;
66bdd53f73SVivek Kasireddy     } else {
6762232bf4SGerd Hoffmann         if (pixman_image_get_width(res->image)  != s->current_cursor->width ||
6862232bf4SGerd Hoffmann             pixman_image_get_height(res->image) != s->current_cursor->height) {
6962232bf4SGerd Hoffmann             return;
7062232bf4SGerd Hoffmann         }
71bdd53f73SVivek Kasireddy         data = pixman_image_get_data(res->image);
72bdd53f73SVivek Kasireddy     }
7362232bf4SGerd Hoffmann 
7462232bf4SGerd Hoffmann     pixels = s->current_cursor->width * s->current_cursor->height;
75bdd53f73SVivek Kasireddy     memcpy(s->current_cursor->data, data,
7662232bf4SGerd Hoffmann            pixels * sizeof(uint32_t));
7762232bf4SGerd Hoffmann }
7862232bf4SGerd Hoffmann 
7962232bf4SGerd Hoffmann static void update_cursor(VirtIOGPU *g, struct virtio_gpu_update_cursor *cursor)
8062232bf4SGerd Hoffmann {
8162232bf4SGerd Hoffmann     struct virtio_gpu_scanout *s;
822c267d66SGerd Hoffmann     VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
830c244e50SGerd Hoffmann     bool move = cursor->hdr.type == VIRTIO_GPU_CMD_MOVE_CURSOR;
8462232bf4SGerd Hoffmann 
8550d8e25eSMarc-André Lureau     if (cursor->pos.scanout_id >= g->parent_obj.conf.max_outputs) {
8662232bf4SGerd Hoffmann         return;
8762232bf4SGerd Hoffmann     }
8850d8e25eSMarc-André Lureau     s = &g->parent_obj.scanout[cursor->pos.scanout_id];
8962232bf4SGerd Hoffmann 
90e9c1b459SGerd Hoffmann     trace_virtio_gpu_update_cursor(cursor->pos.scanout_id,
91e9c1b459SGerd Hoffmann                                    cursor->pos.x,
92e9c1b459SGerd Hoffmann                                    cursor->pos.y,
93e9c1b459SGerd Hoffmann                                    move ? "move" : "update",
94e9c1b459SGerd Hoffmann                                    cursor->resource_id);
95e9c1b459SGerd Hoffmann 
960c244e50SGerd Hoffmann     if (!move) {
9762232bf4SGerd Hoffmann         if (!s->current_cursor) {
9862232bf4SGerd Hoffmann             s->current_cursor = cursor_alloc(64, 64);
9962232bf4SGerd Hoffmann         }
10062232bf4SGerd Hoffmann 
10162232bf4SGerd Hoffmann         s->current_cursor->hot_x = cursor->hot_x;
10262232bf4SGerd Hoffmann         s->current_cursor->hot_y = cursor->hot_y;
10362232bf4SGerd Hoffmann 
10462232bf4SGerd Hoffmann         if (cursor->resource_id > 0) {
1052c267d66SGerd Hoffmann             vgc->update_cursor_data(g, s, cursor->resource_id);
10662232bf4SGerd Hoffmann         }
10762232bf4SGerd Hoffmann         dpy_cursor_define(s->con, s->current_cursor);
1080c244e50SGerd Hoffmann 
1090c244e50SGerd Hoffmann         s->cursor = *cursor;
1100c244e50SGerd Hoffmann     } else {
1110c244e50SGerd Hoffmann         s->cursor.pos.x = cursor->pos.x;
1120c244e50SGerd Hoffmann         s->cursor.pos.y = cursor->pos.y;
11362232bf4SGerd Hoffmann     }
11462232bf4SGerd Hoffmann     dpy_mouse_set(s->con, cursor->pos.x, cursor->pos.y,
11562232bf4SGerd Hoffmann                   cursor->resource_id ? 1 : 0);
11662232bf4SGerd Hoffmann }
11762232bf4SGerd Hoffmann 
11862232bf4SGerd Hoffmann static struct virtio_gpu_simple_resource *
11962232bf4SGerd Hoffmann virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id)
12062232bf4SGerd Hoffmann {
12162232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
12262232bf4SGerd Hoffmann 
12362232bf4SGerd Hoffmann     QTAILQ_FOREACH(res, &g->reslist, next) {
12462232bf4SGerd Hoffmann         if (res->resource_id == resource_id) {
12562232bf4SGerd Hoffmann             return res;
12662232bf4SGerd Hoffmann         }
12762232bf4SGerd Hoffmann     }
12862232bf4SGerd Hoffmann     return NULL;
12962232bf4SGerd Hoffmann }
13062232bf4SGerd Hoffmann 
13125c001a4SVivek Kasireddy static struct virtio_gpu_simple_resource *
13225c001a4SVivek Kasireddy virtio_gpu_find_check_resource(VirtIOGPU *g, uint32_t resource_id,
13325c001a4SVivek Kasireddy                                bool require_backing,
13425c001a4SVivek Kasireddy                                const char *caller, uint32_t *error)
13525c001a4SVivek Kasireddy {
13625c001a4SVivek Kasireddy     struct virtio_gpu_simple_resource *res;
13725c001a4SVivek Kasireddy 
13825c001a4SVivek Kasireddy     res = virtio_gpu_find_resource(g, resource_id);
13925c001a4SVivek Kasireddy     if (!res) {
14025c001a4SVivek Kasireddy         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid resource specified %d\n",
14125c001a4SVivek Kasireddy                       caller, resource_id);
14225c001a4SVivek Kasireddy         if (error) {
14325c001a4SVivek Kasireddy             *error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
14425c001a4SVivek Kasireddy         }
14525c001a4SVivek Kasireddy         return NULL;
14625c001a4SVivek Kasireddy     }
14725c001a4SVivek Kasireddy 
14825c001a4SVivek Kasireddy     if (require_backing) {
149e0933d91SVivek Kasireddy         if (!res->iov || (!res->image && !res->blob)) {
15025c001a4SVivek Kasireddy             qemu_log_mask(LOG_GUEST_ERROR, "%s: no backing storage %d\n",
15125c001a4SVivek Kasireddy                           caller, resource_id);
15225c001a4SVivek Kasireddy             if (error) {
15325c001a4SVivek Kasireddy                 *error = VIRTIO_GPU_RESP_ERR_UNSPEC;
15425c001a4SVivek Kasireddy             }
15525c001a4SVivek Kasireddy             return NULL;
15625c001a4SVivek Kasireddy         }
15725c001a4SVivek Kasireddy     }
15825c001a4SVivek Kasireddy 
15925c001a4SVivek Kasireddy     return res;
16025c001a4SVivek Kasireddy }
16125c001a4SVivek Kasireddy 
16262232bf4SGerd Hoffmann void virtio_gpu_ctrl_response(VirtIOGPU *g,
16362232bf4SGerd Hoffmann                               struct virtio_gpu_ctrl_command *cmd,
16462232bf4SGerd Hoffmann                               struct virtio_gpu_ctrl_hdr *resp,
16562232bf4SGerd Hoffmann                               size_t resp_len)
16662232bf4SGerd Hoffmann {
16762232bf4SGerd Hoffmann     size_t s;
16862232bf4SGerd Hoffmann 
16962232bf4SGerd Hoffmann     if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE) {
17062232bf4SGerd Hoffmann         resp->flags |= VIRTIO_GPU_FLAG_FENCE;
17162232bf4SGerd Hoffmann         resp->fence_id = cmd->cmd_hdr.fence_id;
17262232bf4SGerd Hoffmann         resp->ctx_id = cmd->cmd_hdr.ctx_id;
17362232bf4SGerd Hoffmann     }
1741715d6b5SFarhan Ali     virtio_gpu_ctrl_hdr_bswap(resp);
17562232bf4SGerd Hoffmann     s = iov_from_buf(cmd->elem.in_sg, cmd->elem.in_num, 0, resp, resp_len);
17662232bf4SGerd Hoffmann     if (s != resp_len) {
17762232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR,
17862232bf4SGerd Hoffmann                       "%s: response size incorrect %zu vs %zu\n",
17962232bf4SGerd Hoffmann                       __func__, s, resp_len);
18062232bf4SGerd Hoffmann     }
18162232bf4SGerd Hoffmann     virtqueue_push(cmd->vq, &cmd->elem, s);
18262232bf4SGerd Hoffmann     virtio_notify(VIRTIO_DEVICE(g), cmd->vq);
18362232bf4SGerd Hoffmann     cmd->finished = true;
18462232bf4SGerd Hoffmann }
18562232bf4SGerd Hoffmann 
18662232bf4SGerd Hoffmann void virtio_gpu_ctrl_response_nodata(VirtIOGPU *g,
18762232bf4SGerd Hoffmann                                      struct virtio_gpu_ctrl_command *cmd,
18862232bf4SGerd Hoffmann                                      enum virtio_gpu_ctrl_type type)
18962232bf4SGerd Hoffmann {
19062232bf4SGerd Hoffmann     struct virtio_gpu_ctrl_hdr resp;
19162232bf4SGerd Hoffmann 
19262232bf4SGerd Hoffmann     memset(&resp, 0, sizeof(resp));
19362232bf4SGerd Hoffmann     resp.type = type;
19462232bf4SGerd Hoffmann     virtio_gpu_ctrl_response(g, cmd, &resp, sizeof(resp));
19562232bf4SGerd Hoffmann }
19662232bf4SGerd Hoffmann 
19762232bf4SGerd Hoffmann void virtio_gpu_get_display_info(VirtIOGPU *g,
19862232bf4SGerd Hoffmann                                  struct virtio_gpu_ctrl_command *cmd)
19962232bf4SGerd Hoffmann {
20062232bf4SGerd Hoffmann     struct virtio_gpu_resp_display_info display_info;
20162232bf4SGerd Hoffmann 
20262232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_get_display_info();
20362232bf4SGerd Hoffmann     memset(&display_info, 0, sizeof(display_info));
20462232bf4SGerd Hoffmann     display_info.hdr.type = VIRTIO_GPU_RESP_OK_DISPLAY_INFO;
20550d8e25eSMarc-André Lureau     virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g), &display_info);
20662232bf4SGerd Hoffmann     virtio_gpu_ctrl_response(g, cmd, &display_info.hdr,
20762232bf4SGerd Hoffmann                              sizeof(display_info));
20862232bf4SGerd Hoffmann }
20962232bf4SGerd Hoffmann 
2101ed2cb32SGerd Hoffmann static void
2111ed2cb32SGerd Hoffmann virtio_gpu_generate_edid(VirtIOGPU *g, int scanout,
2121ed2cb32SGerd Hoffmann                          struct virtio_gpu_resp_edid *edid)
2131ed2cb32SGerd Hoffmann {
21450d8e25eSMarc-André Lureau     VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
2151ed2cb32SGerd Hoffmann     qemu_edid_info info = {
2164bf47f36SMarc-André Lureau         .width_mm = b->req_state[scanout].width_mm,
2174bf47f36SMarc-André Lureau         .height_mm = b->req_state[scanout].height_mm,
21850d8e25eSMarc-André Lureau         .prefx = b->req_state[scanout].width,
21950d8e25eSMarc-André Lureau         .prefy = b->req_state[scanout].height,
220b95b5631SAkihiko Odaki         .refresh_rate = b->req_state[scanout].refresh_rate,
2211ed2cb32SGerd Hoffmann     };
2221ed2cb32SGerd Hoffmann 
2231ed2cb32SGerd Hoffmann     edid->size = cpu_to_le32(sizeof(edid->edid));
2241ed2cb32SGerd Hoffmann     qemu_edid_generate(edid->edid, sizeof(edid->edid), &info);
2251ed2cb32SGerd Hoffmann }
2261ed2cb32SGerd Hoffmann 
2271ed2cb32SGerd Hoffmann void virtio_gpu_get_edid(VirtIOGPU *g,
2281ed2cb32SGerd Hoffmann                          struct virtio_gpu_ctrl_command *cmd)
2291ed2cb32SGerd Hoffmann {
2301ed2cb32SGerd Hoffmann     struct virtio_gpu_resp_edid edid;
2311ed2cb32SGerd Hoffmann     struct virtio_gpu_cmd_get_edid get_edid;
23250d8e25eSMarc-André Lureau     VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
2331ed2cb32SGerd Hoffmann 
2341ed2cb32SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(get_edid);
2351ed2cb32SGerd Hoffmann     virtio_gpu_bswap_32(&get_edid, sizeof(get_edid));
2361ed2cb32SGerd Hoffmann 
23750d8e25eSMarc-André Lureau     if (get_edid.scanout >= b->conf.max_outputs) {
2381ed2cb32SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
2391ed2cb32SGerd Hoffmann         return;
2401ed2cb32SGerd Hoffmann     }
2411ed2cb32SGerd Hoffmann 
2421ed2cb32SGerd Hoffmann     trace_virtio_gpu_cmd_get_edid(get_edid.scanout);
2431ed2cb32SGerd Hoffmann     memset(&edid, 0, sizeof(edid));
2441ed2cb32SGerd Hoffmann     edid.hdr.type = VIRTIO_GPU_RESP_OK_EDID;
2451ed2cb32SGerd Hoffmann     virtio_gpu_generate_edid(g, get_edid.scanout, &edid);
2461ed2cb32SGerd Hoffmann     virtio_gpu_ctrl_response(g, cmd, &edid.hdr, sizeof(edid));
2471ed2cb32SGerd Hoffmann }
2481ed2cb32SGerd Hoffmann 
249c53f5b89STao Wu static uint32_t calc_image_hostmem(pixman_format_code_t pformat,
250c53f5b89STao Wu                                    uint32_t width, uint32_t height)
251c53f5b89STao Wu {
252c53f5b89STao Wu     /* Copied from pixman/pixman-bits-image.c, skip integer overflow check.
253c53f5b89STao Wu      * pixman_image_create_bits will fail in case it overflow.
254c53f5b89STao Wu      */
255c53f5b89STao Wu 
256c53f5b89STao Wu     int bpp = PIXMAN_FORMAT_BPP(pformat);
257c53f5b89STao Wu     int stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
258c53f5b89STao Wu     return height * stride;
259c53f5b89STao Wu }
260c53f5b89STao Wu 
26162232bf4SGerd Hoffmann static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
26262232bf4SGerd Hoffmann                                           struct virtio_gpu_ctrl_command *cmd)
26362232bf4SGerd Hoffmann {
26462232bf4SGerd Hoffmann     pixman_format_code_t pformat;
26562232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
26662232bf4SGerd Hoffmann     struct virtio_gpu_resource_create_2d c2d;
26762232bf4SGerd Hoffmann 
26862232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(c2d);
2691715d6b5SFarhan Ali     virtio_gpu_bswap_32(&c2d, sizeof(c2d));
27062232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_res_create_2d(c2d.resource_id, c2d.format,
27162232bf4SGerd Hoffmann                                        c2d.width, c2d.height);
27262232bf4SGerd Hoffmann 
27362232bf4SGerd Hoffmann     if (c2d.resource_id == 0) {
27462232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
27562232bf4SGerd Hoffmann                       __func__);
27662232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
27762232bf4SGerd Hoffmann         return;
27862232bf4SGerd Hoffmann     }
27962232bf4SGerd Hoffmann 
28062232bf4SGerd Hoffmann     res = virtio_gpu_find_resource(g, c2d.resource_id);
28162232bf4SGerd Hoffmann     if (res) {
28262232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already exists %d\n",
28362232bf4SGerd Hoffmann                       __func__, c2d.resource_id);
28462232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
28562232bf4SGerd Hoffmann         return;
28662232bf4SGerd Hoffmann     }
28762232bf4SGerd Hoffmann 
28862232bf4SGerd Hoffmann     res = g_new0(struct virtio_gpu_simple_resource, 1);
28962232bf4SGerd Hoffmann 
29062232bf4SGerd Hoffmann     res->width = c2d.width;
29162232bf4SGerd Hoffmann     res->height = c2d.height;
29262232bf4SGerd Hoffmann     res->format = c2d.format;
29362232bf4SGerd Hoffmann     res->resource_id = c2d.resource_id;
29462232bf4SGerd Hoffmann 
29583a7d3c0SMarc-André Lureau     pformat = virtio_gpu_get_pixman_format(c2d.format);
29662232bf4SGerd Hoffmann     if (!pformat) {
29762232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR,
29862232bf4SGerd Hoffmann                       "%s: host couldn't handle guest format %d\n",
29962232bf4SGerd Hoffmann                       __func__, c2d.format);
300cb3a0522SLi Qiang         g_free(res);
30162232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
30262232bf4SGerd Hoffmann         return;
30362232bf4SGerd Hoffmann     }
3049b7621bcSGerd Hoffmann 
305c53f5b89STao Wu     res->hostmem = calc_image_hostmem(pformat, c2d.width, c2d.height);
30650d8e25eSMarc-André Lureau     if (res->hostmem + g->hostmem < g->conf_max_hostmem) {
30762232bf4SGerd Hoffmann         res->image = pixman_image_create_bits(pformat,
30862232bf4SGerd Hoffmann                                               c2d.width,
30962232bf4SGerd Hoffmann                                               c2d.height,
31062232bf4SGerd Hoffmann                                               NULL, 0);
3119b7621bcSGerd Hoffmann     }
31262232bf4SGerd Hoffmann 
31362232bf4SGerd Hoffmann     if (!res->image) {
31462232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR,
31562232bf4SGerd Hoffmann                       "%s: resource creation failed %d %d %d\n",
31662232bf4SGerd Hoffmann                       __func__, c2d.resource_id, c2d.width, c2d.height);
31762232bf4SGerd Hoffmann         g_free(res);
31862232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
31962232bf4SGerd Hoffmann         return;
32062232bf4SGerd Hoffmann     }
32162232bf4SGerd Hoffmann 
32262232bf4SGerd Hoffmann     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
3239b7621bcSGerd Hoffmann     g->hostmem += res->hostmem;
32462232bf4SGerd Hoffmann }
32562232bf4SGerd Hoffmann 
326e0933d91SVivek Kasireddy static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
327e0933d91SVivek Kasireddy                                             struct virtio_gpu_ctrl_command *cmd)
328e0933d91SVivek Kasireddy {
329e0933d91SVivek Kasireddy     struct virtio_gpu_simple_resource *res;
330e0933d91SVivek Kasireddy     struct virtio_gpu_resource_create_blob cblob;
331e0933d91SVivek Kasireddy     int ret;
332e0933d91SVivek Kasireddy 
333e0933d91SVivek Kasireddy     VIRTIO_GPU_FILL_CMD(cblob);
334e0933d91SVivek Kasireddy     virtio_gpu_create_blob_bswap(&cblob);
335e0933d91SVivek Kasireddy     trace_virtio_gpu_cmd_res_create_blob(cblob.resource_id, cblob.size);
336e0933d91SVivek Kasireddy 
337e0933d91SVivek Kasireddy     if (cblob.resource_id == 0) {
338e0933d91SVivek Kasireddy         qemu_log_mask(LOG_GUEST_ERROR, "%s: resource id 0 is not allowed\n",
339e0933d91SVivek Kasireddy                       __func__);
340e0933d91SVivek Kasireddy         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
341e0933d91SVivek Kasireddy         return;
342e0933d91SVivek Kasireddy     }
343e0933d91SVivek Kasireddy 
344dc2deabaSPhilippe Mathieu-Daudé     if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_GUEST &&
345dc2deabaSPhilippe Mathieu-Daudé         cblob.blob_flags != VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE) {
346dc2deabaSPhilippe Mathieu-Daudé         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid memory type\n",
347dc2deabaSPhilippe Mathieu-Daudé                       __func__);
348dc2deabaSPhilippe Mathieu-Daudé         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
349dc2deabaSPhilippe Mathieu-Daudé         return;
350dc2deabaSPhilippe Mathieu-Daudé     }
351dc2deabaSPhilippe Mathieu-Daudé 
352dc2deabaSPhilippe Mathieu-Daudé     if (virtio_gpu_find_resource(g, cblob.resource_id)) {
353e0933d91SVivek Kasireddy         qemu_log_mask(LOG_GUEST_ERROR, "%s: resource already exists %d\n",
354e0933d91SVivek Kasireddy                       __func__, cblob.resource_id);
355e0933d91SVivek Kasireddy         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
356e0933d91SVivek Kasireddy         return;
357e0933d91SVivek Kasireddy     }
358e0933d91SVivek Kasireddy 
359e0933d91SVivek Kasireddy     res = g_new0(struct virtio_gpu_simple_resource, 1);
360e0933d91SVivek Kasireddy     res->resource_id = cblob.resource_id;
361e0933d91SVivek Kasireddy     res->blob_size = cblob.size;
362e0933d91SVivek Kasireddy 
363e0933d91SVivek Kasireddy     ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
364e0933d91SVivek Kasireddy                                         cmd, &res->addrs, &res->iov,
365e0933d91SVivek Kasireddy                                         &res->iov_cnt);
366fc4d3f35SDongwon Kim     if (ret != 0) {
367e0933d91SVivek Kasireddy         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
368dc2deabaSPhilippe Mathieu-Daudé         g_free(res);
369e0933d91SVivek Kasireddy         return;
370e0933d91SVivek Kasireddy     }
371e0933d91SVivek Kasireddy 
372e0933d91SVivek Kasireddy     virtio_gpu_init_udmabuf(res);
373e0933d91SVivek Kasireddy     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
374e0933d91SVivek Kasireddy }
375e0933d91SVivek Kasireddy 
376da566a18SGerd Hoffmann static void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
377da566a18SGerd Hoffmann {
37850d8e25eSMarc-André Lureau     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
379da566a18SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
380da566a18SGerd Hoffmann 
381da566a18SGerd Hoffmann     if (scanout->resource_id == 0) {
382da566a18SGerd Hoffmann         return;
383da566a18SGerd Hoffmann     }
384da566a18SGerd Hoffmann 
385da566a18SGerd Hoffmann     res = virtio_gpu_find_resource(g, scanout->resource_id);
386da566a18SGerd Hoffmann     if (res) {
387da566a18SGerd Hoffmann         res->scanout_bitmask &= ~(1 << scanout_id);
388da566a18SGerd Hoffmann     }
389da566a18SGerd Hoffmann 
390ed8f3fe6SAkihiko Odaki     dpy_gfx_replace_surface(scanout->con, NULL);
391da566a18SGerd Hoffmann     scanout->resource_id = 0;
392da566a18SGerd Hoffmann     scanout->ds = NULL;
393da566a18SGerd Hoffmann     scanout->width = 0;
394da566a18SGerd Hoffmann     scanout->height = 0;
395da566a18SGerd Hoffmann }
396da566a18SGerd Hoffmann 
39762232bf4SGerd Hoffmann static void virtio_gpu_resource_destroy(VirtIOGPU *g,
39862232bf4SGerd Hoffmann                                         struct virtio_gpu_simple_resource *res)
39962232bf4SGerd Hoffmann {
4001fccd7c5SGerd Hoffmann     int i;
4011fccd7c5SGerd Hoffmann 
4021fccd7c5SGerd Hoffmann     if (res->scanout_bitmask) {
40350d8e25eSMarc-André Lureau         for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
4041fccd7c5SGerd Hoffmann             if (res->scanout_bitmask & (1 << i)) {
4051fccd7c5SGerd Hoffmann                 virtio_gpu_disable_scanout(g, i);
4061fccd7c5SGerd Hoffmann             }
4071fccd7c5SGerd Hoffmann         }
4081fccd7c5SGerd Hoffmann     }
4091fccd7c5SGerd Hoffmann 
41032db3c63SVivek Kasireddy     qemu_pixman_image_unref(res->image);
4113bb68f79SGerd Hoffmann     virtio_gpu_cleanup_mapping(g, res);
41262232bf4SGerd Hoffmann     QTAILQ_REMOVE(&g->reslist, res, next);
4139b7621bcSGerd Hoffmann     g->hostmem -= res->hostmem;
41462232bf4SGerd Hoffmann     g_free(res);
41562232bf4SGerd Hoffmann }
41662232bf4SGerd Hoffmann 
41762232bf4SGerd Hoffmann static void virtio_gpu_resource_unref(VirtIOGPU *g,
41862232bf4SGerd Hoffmann                                       struct virtio_gpu_ctrl_command *cmd)
41962232bf4SGerd Hoffmann {
42062232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
42162232bf4SGerd Hoffmann     struct virtio_gpu_resource_unref unref;
42262232bf4SGerd Hoffmann 
42362232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(unref);
4241715d6b5SFarhan Ali     virtio_gpu_bswap_32(&unref, sizeof(unref));
42562232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_res_unref(unref.resource_id);
42662232bf4SGerd Hoffmann 
42762232bf4SGerd Hoffmann     res = virtio_gpu_find_resource(g, unref.resource_id);
42862232bf4SGerd Hoffmann     if (!res) {
42962232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
43062232bf4SGerd Hoffmann                       __func__, unref.resource_id);
43162232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
43262232bf4SGerd Hoffmann         return;
43362232bf4SGerd Hoffmann     }
43462232bf4SGerd Hoffmann     virtio_gpu_resource_destroy(g, res);
43562232bf4SGerd Hoffmann }
43662232bf4SGerd Hoffmann 
43762232bf4SGerd Hoffmann static void virtio_gpu_transfer_to_host_2d(VirtIOGPU *g,
43862232bf4SGerd Hoffmann                                            struct virtio_gpu_ctrl_command *cmd)
43962232bf4SGerd Hoffmann {
44062232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
44162232bf4SGerd Hoffmann     int h;
44262232bf4SGerd Hoffmann     uint32_t src_offset, dst_offset, stride;
44362232bf4SGerd Hoffmann     int bpp;
44462232bf4SGerd Hoffmann     pixman_format_code_t format;
44562232bf4SGerd Hoffmann     struct virtio_gpu_transfer_to_host_2d t2d;
44662232bf4SGerd Hoffmann 
44762232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(t2d);
4481715d6b5SFarhan Ali     virtio_gpu_t2d_bswap(&t2d);
44962232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_res_xfer_toh_2d(t2d.resource_id);
45062232bf4SGerd Hoffmann 
45125c001a4SVivek Kasireddy     res = virtio_gpu_find_check_resource(g, t2d.resource_id, true,
45225c001a4SVivek Kasireddy                                          __func__, &cmd->error);
453e0933d91SVivek Kasireddy     if (!res || res->blob) {
45462232bf4SGerd Hoffmann         return;
45562232bf4SGerd Hoffmann     }
45662232bf4SGerd Hoffmann 
45762232bf4SGerd Hoffmann     if (t2d.r.x > res->width ||
45862232bf4SGerd Hoffmann         t2d.r.y > res->height ||
45962232bf4SGerd Hoffmann         t2d.r.width > res->width ||
46062232bf4SGerd Hoffmann         t2d.r.height > res->height ||
46162232bf4SGerd Hoffmann         t2d.r.x + t2d.r.width > res->width ||
46262232bf4SGerd Hoffmann         t2d.r.y + t2d.r.height > res->height) {
46362232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: transfer bounds outside resource"
46462232bf4SGerd Hoffmann                       " bounds for resource %d: %d %d %d %d vs %d %d\n",
46562232bf4SGerd Hoffmann                       __func__, t2d.resource_id, t2d.r.x, t2d.r.y,
46662232bf4SGerd Hoffmann                       t2d.r.width, t2d.r.height, res->width, res->height);
46762232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
46862232bf4SGerd Hoffmann         return;
46962232bf4SGerd Hoffmann     }
47062232bf4SGerd Hoffmann 
47162232bf4SGerd Hoffmann     format = pixman_image_get_format(res->image);
472e5f99037SMarc-André Lureau     bpp = DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format), 8);
47362232bf4SGerd Hoffmann     stride = pixman_image_get_stride(res->image);
47462232bf4SGerd Hoffmann 
47562232bf4SGerd Hoffmann     if (t2d.offset || t2d.r.x || t2d.r.y ||
47662232bf4SGerd Hoffmann         t2d.r.width != pixman_image_get_width(res->image)) {
47762232bf4SGerd Hoffmann         void *img_data = pixman_image_get_data(res->image);
47862232bf4SGerd Hoffmann         for (h = 0; h < t2d.r.height; h++) {
47962232bf4SGerd Hoffmann             src_offset = t2d.offset + stride * h;
48062232bf4SGerd Hoffmann             dst_offset = (t2d.r.y + h) * stride + (t2d.r.x * bpp);
48162232bf4SGerd Hoffmann 
48262232bf4SGerd Hoffmann             iov_to_buf(res->iov, res->iov_cnt, src_offset,
48362232bf4SGerd Hoffmann                        (uint8_t *)img_data
48462232bf4SGerd Hoffmann                        + dst_offset, t2d.r.width * bpp);
48562232bf4SGerd Hoffmann         }
48662232bf4SGerd Hoffmann     } else {
48762232bf4SGerd Hoffmann         iov_to_buf(res->iov, res->iov_cnt, 0,
48862232bf4SGerd Hoffmann                    pixman_image_get_data(res->image),
48962232bf4SGerd Hoffmann                    pixman_image_get_stride(res->image)
49062232bf4SGerd Hoffmann                    * pixman_image_get_height(res->image));
49162232bf4SGerd Hoffmann     }
49262232bf4SGerd Hoffmann }
49362232bf4SGerd Hoffmann 
49462232bf4SGerd Hoffmann static void virtio_gpu_resource_flush(VirtIOGPU *g,
49562232bf4SGerd Hoffmann                                       struct virtio_gpu_ctrl_command *cmd)
49662232bf4SGerd Hoffmann {
49762232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
49862232bf4SGerd Hoffmann     struct virtio_gpu_resource_flush rf;
49932db3c63SVivek Kasireddy     struct virtio_gpu_scanout *scanout;
50062232bf4SGerd Hoffmann     pixman_region16_t flush_region;
50162232bf4SGerd Hoffmann     int i;
50262232bf4SGerd Hoffmann 
50362232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(rf);
5041715d6b5SFarhan Ali     virtio_gpu_bswap_32(&rf, sizeof(rf));
50562232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_res_flush(rf.resource_id,
50662232bf4SGerd Hoffmann                                    rf.r.width, rf.r.height, rf.r.x, rf.r.y);
50762232bf4SGerd Hoffmann 
50825c001a4SVivek Kasireddy     res = virtio_gpu_find_check_resource(g, rf.resource_id, false,
50925c001a4SVivek Kasireddy                                          __func__, &cmd->error);
51032db3c63SVivek Kasireddy     if (!res) {
51162232bf4SGerd Hoffmann         return;
51262232bf4SGerd Hoffmann     }
51362232bf4SGerd Hoffmann 
51432db3c63SVivek Kasireddy     if (res->blob) {
51532db3c63SVivek Kasireddy         for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
51632db3c63SVivek Kasireddy             scanout = &g->parent_obj.scanout[i];
51732db3c63SVivek Kasireddy             if (scanout->resource_id == res->resource_id &&
51849a99ecbSDongwon Kim                 rf.r.x < scanout->x + scanout->width &&
51949a99ecbSDongwon Kim                 rf.r.x + rf.r.width >= scanout->x &&
52049a99ecbSDongwon Kim                 rf.r.y < scanout->y + scanout->height &&
52149a99ecbSDongwon Kim                 rf.r.y + rf.r.height >= scanout->y &&
52232db3c63SVivek Kasireddy                 console_has_gl(scanout->con)) {
52332db3c63SVivek Kasireddy                 dpy_gl_update(scanout->con, 0, 0, scanout->width,
52432db3c63SVivek Kasireddy                               scanout->height);
525e86a93f5SDongwon Kim             }
526e86a93f5SDongwon Kim         }
52732db3c63SVivek Kasireddy         return;
52832db3c63SVivek Kasireddy     }
52932db3c63SVivek Kasireddy 
53032db3c63SVivek Kasireddy     if (!res->blob &&
53132db3c63SVivek Kasireddy         (rf.r.x > res->width ||
53262232bf4SGerd Hoffmann         rf.r.y > res->height ||
53362232bf4SGerd Hoffmann         rf.r.width > res->width ||
53462232bf4SGerd Hoffmann         rf.r.height > res->height ||
53562232bf4SGerd Hoffmann         rf.r.x + rf.r.width > res->width ||
53632db3c63SVivek Kasireddy         rf.r.y + rf.r.height > res->height)) {
53762232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: flush bounds outside resource"
53862232bf4SGerd Hoffmann                       " bounds for resource %d: %d %d %d %d vs %d %d\n",
53962232bf4SGerd Hoffmann                       __func__, rf.resource_id, rf.r.x, rf.r.y,
54062232bf4SGerd Hoffmann                       rf.r.width, rf.r.height, res->width, res->height);
54162232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
54262232bf4SGerd Hoffmann         return;
54362232bf4SGerd Hoffmann     }
54462232bf4SGerd Hoffmann 
54562232bf4SGerd Hoffmann     pixman_region_init_rect(&flush_region,
54662232bf4SGerd Hoffmann                             rf.r.x, rf.r.y, rf.r.width, rf.r.height);
54750d8e25eSMarc-André Lureau     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
54862232bf4SGerd Hoffmann         pixman_region16_t region, finalregion;
54962232bf4SGerd Hoffmann         pixman_box16_t *extents;
55062232bf4SGerd Hoffmann 
55162232bf4SGerd Hoffmann         if (!(res->scanout_bitmask & (1 << i))) {
55262232bf4SGerd Hoffmann             continue;
55362232bf4SGerd Hoffmann         }
55450d8e25eSMarc-André Lureau         scanout = &g->parent_obj.scanout[i];
55562232bf4SGerd Hoffmann 
55662232bf4SGerd Hoffmann         pixman_region_init(&finalregion);
55762232bf4SGerd Hoffmann         pixman_region_init_rect(&region, scanout->x, scanout->y,
55862232bf4SGerd Hoffmann                                 scanout->width, scanout->height);
55962232bf4SGerd Hoffmann 
56062232bf4SGerd Hoffmann         pixman_region_intersect(&finalregion, &flush_region, &region);
56162232bf4SGerd Hoffmann         pixman_region_translate(&finalregion, -scanout->x, -scanout->y);
56262232bf4SGerd Hoffmann         extents = pixman_region_extents(&finalregion);
56362232bf4SGerd Hoffmann         /* work out the area we need to update for each console */
56450d8e25eSMarc-André Lureau         dpy_gfx_update(g->parent_obj.scanout[i].con,
56562232bf4SGerd Hoffmann                        extents->x1, extents->y1,
56662232bf4SGerd Hoffmann                        extents->x2 - extents->x1,
56762232bf4SGerd Hoffmann                        extents->y2 - extents->y1);
56862232bf4SGerd Hoffmann 
56962232bf4SGerd Hoffmann         pixman_region_fini(&region);
57062232bf4SGerd Hoffmann         pixman_region_fini(&finalregion);
57162232bf4SGerd Hoffmann     }
57262232bf4SGerd Hoffmann     pixman_region_fini(&flush_region);
57362232bf4SGerd Hoffmann }
57462232bf4SGerd Hoffmann 
575fa06e5cbSGerd Hoffmann static void virtio_unref_resource(pixman_image_t *image, void *data)
576fa06e5cbSGerd Hoffmann {
577fa06e5cbSGerd Hoffmann     pixman_image_unref(data);
578fa06e5cbSGerd Hoffmann }
579fa06e5cbSGerd Hoffmann 
58081cd9f71SVivek Kasireddy static void virtio_gpu_update_scanout(VirtIOGPU *g,
58181cd9f71SVivek Kasireddy                                       uint32_t scanout_id,
58281cd9f71SVivek Kasireddy                                       struct virtio_gpu_simple_resource *res,
58381cd9f71SVivek Kasireddy                                       struct virtio_gpu_rect *r)
58481cd9f71SVivek Kasireddy {
58581cd9f71SVivek Kasireddy     struct virtio_gpu_simple_resource *ores;
58681cd9f71SVivek Kasireddy     struct virtio_gpu_scanout *scanout;
58781cd9f71SVivek Kasireddy 
58881cd9f71SVivek Kasireddy     scanout = &g->parent_obj.scanout[scanout_id];
58981cd9f71SVivek Kasireddy     ores = virtio_gpu_find_resource(g, scanout->resource_id);
59081cd9f71SVivek Kasireddy     if (ores) {
59181cd9f71SVivek Kasireddy         ores->scanout_bitmask &= ~(1 << scanout_id);
59281cd9f71SVivek Kasireddy     }
59381cd9f71SVivek Kasireddy 
59481cd9f71SVivek Kasireddy     res->scanout_bitmask |= (1 << scanout_id);
59581cd9f71SVivek Kasireddy     scanout->resource_id = res->resource_id;
59681cd9f71SVivek Kasireddy     scanout->x = r->x;
59781cd9f71SVivek Kasireddy     scanout->y = r->y;
59881cd9f71SVivek Kasireddy     scanout->width = r->width;
59981cd9f71SVivek Kasireddy     scanout->height = r->height;
60081cd9f71SVivek Kasireddy }
60181cd9f71SVivek Kasireddy 
602e64d4b6aSVivek Kasireddy static void virtio_gpu_do_set_scanout(VirtIOGPU *g,
603e64d4b6aSVivek Kasireddy                                       uint32_t scanout_id,
604e64d4b6aSVivek Kasireddy                                       struct virtio_gpu_framebuffer *fb,
605e64d4b6aSVivek Kasireddy                                       struct virtio_gpu_simple_resource *res,
606e64d4b6aSVivek Kasireddy                                       struct virtio_gpu_rect *r,
607e64d4b6aSVivek Kasireddy                                       uint32_t *error)
608e64d4b6aSVivek Kasireddy {
609e64d4b6aSVivek Kasireddy     struct virtio_gpu_scanout *scanout;
610e64d4b6aSVivek Kasireddy     uint8_t *data;
611e64d4b6aSVivek Kasireddy 
612e64d4b6aSVivek Kasireddy     scanout = &g->parent_obj.scanout[scanout_id];
613e64d4b6aSVivek Kasireddy 
614e64d4b6aSVivek Kasireddy     if (r->x > fb->width ||
615e64d4b6aSVivek Kasireddy         r->y > fb->height ||
616e64d4b6aSVivek Kasireddy         r->width < 16 ||
617e64d4b6aSVivek Kasireddy         r->height < 16 ||
618e64d4b6aSVivek Kasireddy         r->width > fb->width ||
619e64d4b6aSVivek Kasireddy         r->height > fb->height ||
620e64d4b6aSVivek Kasireddy         r->x + r->width > fb->width ||
621e64d4b6aSVivek Kasireddy         r->y + r->height > fb->height) {
622e64d4b6aSVivek Kasireddy         qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout %d bounds for"
623e64d4b6aSVivek Kasireddy                       " resource %d, rect (%d,%d)+%d,%d, fb %d %d\n",
624e64d4b6aSVivek Kasireddy                       __func__, scanout_id, res->resource_id,
625e64d4b6aSVivek Kasireddy                       r->x, r->y, r->width, r->height,
626e64d4b6aSVivek Kasireddy                       fb->width, fb->height);
627e64d4b6aSVivek Kasireddy         *error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
628e64d4b6aSVivek Kasireddy         return;
629e64d4b6aSVivek Kasireddy     }
630e64d4b6aSVivek Kasireddy 
631e64d4b6aSVivek Kasireddy     g->parent_obj.enable = 1;
63232db3c63SVivek Kasireddy 
63332db3c63SVivek Kasireddy     if (res->blob) {
63432db3c63SVivek Kasireddy         if (console_has_gl(scanout->con)) {
635e86a93f5SDongwon Kim             if (!virtio_gpu_update_dmabuf(g, scanout_id, res, fb, r)) {
63632db3c63SVivek Kasireddy                 virtio_gpu_update_scanout(g, scanout_id, res, r);
63732db3c63SVivek Kasireddy                 return;
63832db3c63SVivek Kasireddy             }
63932db3c63SVivek Kasireddy         }
64032db3c63SVivek Kasireddy 
64132db3c63SVivek Kasireddy         data = res->blob;
64232db3c63SVivek Kasireddy     } else {
643e64d4b6aSVivek Kasireddy         data = (uint8_t *)pixman_image_get_data(res->image);
64432db3c63SVivek Kasireddy     }
645e64d4b6aSVivek Kasireddy 
646e64d4b6aSVivek Kasireddy     /* create a surface for this scanout */
64732db3c63SVivek Kasireddy     if ((res->blob && !console_has_gl(scanout->con)) ||
64832db3c63SVivek Kasireddy         !scanout->ds ||
649e64d4b6aSVivek Kasireddy         surface_data(scanout->ds) != data + fb->offset ||
650e64d4b6aSVivek Kasireddy         scanout->width != r->width ||
651e64d4b6aSVivek Kasireddy         scanout->height != r->height) {
652e64d4b6aSVivek Kasireddy         pixman_image_t *rect;
653e64d4b6aSVivek Kasireddy         void *ptr = data + fb->offset;
654e64d4b6aSVivek Kasireddy         rect = pixman_image_create_bits(fb->format, r->width, r->height,
655e64d4b6aSVivek Kasireddy                                         ptr, fb->stride);
656e64d4b6aSVivek Kasireddy 
657e64d4b6aSVivek Kasireddy         if (res->image) {
658e64d4b6aSVivek Kasireddy             pixman_image_ref(res->image);
659e64d4b6aSVivek Kasireddy             pixman_image_set_destroy_function(rect, virtio_unref_resource,
660e64d4b6aSVivek Kasireddy                                               res->image);
661e64d4b6aSVivek Kasireddy         }
662e64d4b6aSVivek Kasireddy 
663e64d4b6aSVivek Kasireddy         /* realloc the surface ptr */
664e64d4b6aSVivek Kasireddy         scanout->ds = qemu_create_displaysurface_pixman(rect);
665e64d4b6aSVivek Kasireddy         if (!scanout->ds) {
666e64d4b6aSVivek Kasireddy             *error = VIRTIO_GPU_RESP_ERR_UNSPEC;
667e64d4b6aSVivek Kasireddy             return;
668e64d4b6aSVivek Kasireddy         }
669e64d4b6aSVivek Kasireddy 
670e64d4b6aSVivek Kasireddy         pixman_image_unref(rect);
671e64d4b6aSVivek Kasireddy         dpy_gfx_replace_surface(g->parent_obj.scanout[scanout_id].con,
672e64d4b6aSVivek Kasireddy                                 scanout->ds);
673e64d4b6aSVivek Kasireddy     }
674e64d4b6aSVivek Kasireddy 
67581cd9f71SVivek Kasireddy     virtio_gpu_update_scanout(g, scanout_id, res, r);
676e64d4b6aSVivek Kasireddy }
677e64d4b6aSVivek Kasireddy 
67862232bf4SGerd Hoffmann static void virtio_gpu_set_scanout(VirtIOGPU *g,
67962232bf4SGerd Hoffmann                                    struct virtio_gpu_ctrl_command *cmd)
68062232bf4SGerd Hoffmann {
681e64d4b6aSVivek Kasireddy     struct virtio_gpu_simple_resource *res;
682e64d4b6aSVivek Kasireddy     struct virtio_gpu_framebuffer fb = { 0 };
68362232bf4SGerd Hoffmann     struct virtio_gpu_set_scanout ss;
68462232bf4SGerd Hoffmann 
68562232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(ss);
6861715d6b5SFarhan Ali     virtio_gpu_bswap_32(&ss, sizeof(ss));
68762232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_set_scanout(ss.scanout_id, ss.resource_id,
68862232bf4SGerd Hoffmann                                      ss.r.width, ss.r.height, ss.r.x, ss.r.y);
68962232bf4SGerd Hoffmann 
6904fa7b4ccSGerd Hoffmann     if (ss.scanout_id >= g->parent_obj.conf.max_outputs) {
6914fa7b4ccSGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout id specified %d",
6924fa7b4ccSGerd Hoffmann                       __func__, ss.scanout_id);
6934fa7b4ccSGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
6944fa7b4ccSGerd Hoffmann         return;
6954fa7b4ccSGerd Hoffmann     }
6964fa7b4ccSGerd Hoffmann 
69762232bf4SGerd Hoffmann     if (ss.resource_id == 0) {
698da566a18SGerd Hoffmann         virtio_gpu_disable_scanout(g, ss.scanout_id);
69962232bf4SGerd Hoffmann         return;
70062232bf4SGerd Hoffmann     }
70162232bf4SGerd Hoffmann 
70225c001a4SVivek Kasireddy     res = virtio_gpu_find_check_resource(g, ss.resource_id, true,
70325c001a4SVivek Kasireddy                                          __func__, &cmd->error);
70462232bf4SGerd Hoffmann     if (!res) {
70562232bf4SGerd Hoffmann         return;
70662232bf4SGerd Hoffmann     }
70762232bf4SGerd Hoffmann 
708e64d4b6aSVivek Kasireddy     fb.format = pixman_image_get_format(res->image);
709e64d4b6aSVivek Kasireddy     fb.bytes_pp = DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb.format), 8);
710e64d4b6aSVivek Kasireddy     fb.width  = pixman_image_get_width(res->image);
711e64d4b6aSVivek Kasireddy     fb.height = pixman_image_get_height(res->image);
712e64d4b6aSVivek Kasireddy     fb.stride = pixman_image_get_stride(res->image);
713e64d4b6aSVivek Kasireddy     fb.offset = ss.r.x * fb.bytes_pp + ss.r.y * fb.stride;
71462232bf4SGerd Hoffmann 
715e64d4b6aSVivek Kasireddy     virtio_gpu_do_set_scanout(g, ss.scanout_id,
716e64d4b6aSVivek Kasireddy                               &fb, res, &ss.r, &cmd->error);
71762232bf4SGerd Hoffmann }
71862232bf4SGerd Hoffmann 
71932db3c63SVivek Kasireddy static void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
72032db3c63SVivek Kasireddy                                         struct virtio_gpu_ctrl_command *cmd)
72132db3c63SVivek Kasireddy {
72232db3c63SVivek Kasireddy     struct virtio_gpu_simple_resource *res;
72332db3c63SVivek Kasireddy     struct virtio_gpu_framebuffer fb = { 0 };
72432db3c63SVivek Kasireddy     struct virtio_gpu_set_scanout_blob ss;
72532db3c63SVivek Kasireddy     uint64_t fbend;
72632db3c63SVivek Kasireddy 
72732db3c63SVivek Kasireddy     VIRTIO_GPU_FILL_CMD(ss);
72832db3c63SVivek Kasireddy     virtio_gpu_scanout_blob_bswap(&ss);
72932db3c63SVivek Kasireddy     trace_virtio_gpu_cmd_set_scanout_blob(ss.scanout_id, ss.resource_id,
73032db3c63SVivek Kasireddy                                           ss.r.width, ss.r.height, ss.r.x,
73132db3c63SVivek Kasireddy                                           ss.r.y);
73232db3c63SVivek Kasireddy 
7334fa7b4ccSGerd Hoffmann     if (ss.scanout_id >= g->parent_obj.conf.max_outputs) {
7344fa7b4ccSGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout id specified %d",
7354fa7b4ccSGerd Hoffmann                       __func__, ss.scanout_id);
7364fa7b4ccSGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
7374fa7b4ccSGerd Hoffmann         return;
7384fa7b4ccSGerd Hoffmann     }
7394fa7b4ccSGerd Hoffmann 
74032db3c63SVivek Kasireddy     if (ss.resource_id == 0) {
74132db3c63SVivek Kasireddy         virtio_gpu_disable_scanout(g, ss.scanout_id);
74232db3c63SVivek Kasireddy         return;
74332db3c63SVivek Kasireddy     }
74432db3c63SVivek Kasireddy 
74532db3c63SVivek Kasireddy     res = virtio_gpu_find_check_resource(g, ss.resource_id, true,
74632db3c63SVivek Kasireddy                                          __func__, &cmd->error);
74732db3c63SVivek Kasireddy     if (!res) {
74832db3c63SVivek Kasireddy         return;
74932db3c63SVivek Kasireddy     }
75032db3c63SVivek Kasireddy 
75132db3c63SVivek Kasireddy     fb.format = virtio_gpu_get_pixman_format(ss.format);
75232db3c63SVivek Kasireddy     if (!fb.format) {
75332db3c63SVivek Kasireddy         qemu_log_mask(LOG_GUEST_ERROR,
75432db3c63SVivek Kasireddy                       "%s: host couldn't handle guest format %d\n",
75532db3c63SVivek Kasireddy                       __func__, ss.format);
75632db3c63SVivek Kasireddy         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
75732db3c63SVivek Kasireddy         return;
75832db3c63SVivek Kasireddy     }
75932db3c63SVivek Kasireddy 
76032db3c63SVivek Kasireddy     fb.bytes_pp = DIV_ROUND_UP(PIXMAN_FORMAT_BPP(fb.format), 8);
76132db3c63SVivek Kasireddy     fb.width = ss.width;
76232db3c63SVivek Kasireddy     fb.height = ss.height;
76332db3c63SVivek Kasireddy     fb.stride = ss.strides[0];
76432db3c63SVivek Kasireddy     fb.offset = ss.offsets[0] + ss.r.x * fb.bytes_pp + ss.r.y * fb.stride;
76532db3c63SVivek Kasireddy 
76632db3c63SVivek Kasireddy     fbend = fb.offset;
76732db3c63SVivek Kasireddy     fbend += fb.stride * (ss.r.height - 1);
76832db3c63SVivek Kasireddy     fbend += fb.bytes_pp * ss.r.width;
76932db3c63SVivek Kasireddy     if (fbend > res->blob_size) {
77032db3c63SVivek Kasireddy         qemu_log_mask(LOG_GUEST_ERROR,
77132db3c63SVivek Kasireddy                       "%s: fb end out of range\n",
77232db3c63SVivek Kasireddy                       __func__);
77332db3c63SVivek Kasireddy         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
77432db3c63SVivek Kasireddy         return;
77532db3c63SVivek Kasireddy     }
77632db3c63SVivek Kasireddy 
77732db3c63SVivek Kasireddy     virtio_gpu_do_set_scanout(g, ss.scanout_id,
77832db3c63SVivek Kasireddy                               &fb, res, &ss.r, &cmd->error);
77932db3c63SVivek Kasireddy }
78032db3c63SVivek Kasireddy 
7813bb68f79SGerd Hoffmann int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
78270d37662SVivek Kasireddy                                   uint32_t nr_entries, uint32_t offset,
78362232bf4SGerd Hoffmann                                   struct virtio_gpu_ctrl_command *cmd,
7849049f8bcSGerd Hoffmann                                   uint64_t **addr, struct iovec **iov,
7859049f8bcSGerd Hoffmann                                   uint32_t *niov)
78662232bf4SGerd Hoffmann {
78762232bf4SGerd Hoffmann     struct virtio_gpu_mem_entry *ents;
78862232bf4SGerd Hoffmann     size_t esize, s;
7899049f8bcSGerd Hoffmann     int e, v;
79062232bf4SGerd Hoffmann 
79170d37662SVivek Kasireddy     if (nr_entries > 16384) {
79262232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR,
7932c84167bSGerd Hoffmann                       "%s: nr_entries is too big (%d > 16384)\n",
79470d37662SVivek Kasireddy                       __func__, nr_entries);
79562232bf4SGerd Hoffmann         return -1;
79662232bf4SGerd Hoffmann     }
79762232bf4SGerd Hoffmann 
79870d37662SVivek Kasireddy     esize = sizeof(*ents) * nr_entries;
79962232bf4SGerd Hoffmann     ents = g_malloc(esize);
80062232bf4SGerd Hoffmann     s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
80170d37662SVivek Kasireddy                    offset, ents, esize);
80262232bf4SGerd Hoffmann     if (s != esize) {
80362232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR,
80462232bf4SGerd Hoffmann                       "%s: command data size incorrect %zu vs %zu\n",
80562232bf4SGerd Hoffmann                       __func__, s, esize);
80662232bf4SGerd Hoffmann         g_free(ents);
80762232bf4SGerd Hoffmann         return -1;
80862232bf4SGerd Hoffmann     }
80962232bf4SGerd Hoffmann 
8109049f8bcSGerd Hoffmann     *iov = NULL;
8110c244e50SGerd Hoffmann     if (addr) {
8129049f8bcSGerd Hoffmann         *addr = NULL;
8130c244e50SGerd Hoffmann     }
81470d37662SVivek Kasireddy     for (e = 0, v = 0; e < nr_entries; e++) {
8159049f8bcSGerd Hoffmann         uint64_t a = le64_to_cpu(ents[e].addr);
8169049f8bcSGerd Hoffmann         uint32_t l = le32_to_cpu(ents[e].length);
8179049f8bcSGerd Hoffmann         hwaddr len;
8189049f8bcSGerd Hoffmann         void *map;
8199049f8bcSGerd Hoffmann 
8209049f8bcSGerd Hoffmann         do {
8219049f8bcSGerd Hoffmann             len = l;
822a1d4b0a3SPhilippe Mathieu-Daudé             map = dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, &len,
823a1d4b0a3SPhilippe Mathieu-Daudé                                  DMA_DIRECTION_TO_DEVICE,
824a1d4b0a3SPhilippe Mathieu-Daudé                                  MEMTXATTRS_UNSPECIFIED);
8259049f8bcSGerd Hoffmann             if (!map) {
82662232bf4SGerd Hoffmann                 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map MMIO memory for"
82770d37662SVivek Kasireddy                               " element %d\n", __func__, e);
8289049f8bcSGerd Hoffmann                 virtio_gpu_cleanup_mapping_iov(g, *iov, v);
82962232bf4SGerd Hoffmann                 g_free(ents);
83062232bf4SGerd Hoffmann                 *iov = NULL;
8310c244e50SGerd Hoffmann                 if (addr) {
8320c244e50SGerd Hoffmann                     g_free(*addr);
8330c244e50SGerd Hoffmann                     *addr = NULL;
8340c244e50SGerd Hoffmann                 }
83562232bf4SGerd Hoffmann                 return -1;
83662232bf4SGerd Hoffmann             }
8379049f8bcSGerd Hoffmann 
8389049f8bcSGerd Hoffmann             if (!(v % 16)) {
839b21e2380SMarkus Armbruster                 *iov = g_renew(struct iovec, *iov, v + 16);
8409049f8bcSGerd Hoffmann                 if (addr) {
841b21e2380SMarkus Armbruster                     *addr = g_renew(uint64_t, *addr, v + 16);
84262232bf4SGerd Hoffmann                 }
8439049f8bcSGerd Hoffmann             }
8449049f8bcSGerd Hoffmann             (*iov)[v].iov_base = map;
8459049f8bcSGerd Hoffmann             (*iov)[v].iov_len = len;
8469049f8bcSGerd Hoffmann             if (addr) {
8479049f8bcSGerd Hoffmann                 (*addr)[v] = a;
8489049f8bcSGerd Hoffmann             }
8499049f8bcSGerd Hoffmann 
8509049f8bcSGerd Hoffmann             a += len;
8519049f8bcSGerd Hoffmann             l -= len;
8529049f8bcSGerd Hoffmann             v += 1;
8539049f8bcSGerd Hoffmann         } while (l > 0);
8549049f8bcSGerd Hoffmann     }
8559049f8bcSGerd Hoffmann     *niov = v;
8569049f8bcSGerd Hoffmann 
85762232bf4SGerd Hoffmann     g_free(ents);
85862232bf4SGerd Hoffmann     return 0;
85962232bf4SGerd Hoffmann }
86062232bf4SGerd Hoffmann 
8613bb68f79SGerd Hoffmann void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g,
8623bb68f79SGerd Hoffmann                                     struct iovec *iov, uint32_t count)
86362232bf4SGerd Hoffmann {
86462232bf4SGerd Hoffmann     int i;
86562232bf4SGerd Hoffmann 
86662232bf4SGerd Hoffmann     for (i = 0; i < count; i++) {
8678da132a5SGerd Hoffmann         dma_memory_unmap(VIRTIO_DEVICE(g)->dma_as,
8688da132a5SGerd Hoffmann                          iov[i].iov_base, iov[i].iov_len,
8698da132a5SGerd Hoffmann                          DMA_DIRECTION_TO_DEVICE,
87062232bf4SGerd Hoffmann                          iov[i].iov_len);
87162232bf4SGerd Hoffmann     }
8727f3be0f2SGerd Hoffmann     g_free(iov);
87362232bf4SGerd Hoffmann }
87462232bf4SGerd Hoffmann 
8753bb68f79SGerd Hoffmann static void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
8763bb68f79SGerd Hoffmann                                        struct virtio_gpu_simple_resource *res)
87762232bf4SGerd Hoffmann {
8783bb68f79SGerd Hoffmann     virtio_gpu_cleanup_mapping_iov(g, res->iov, res->iov_cnt);
87962232bf4SGerd Hoffmann     res->iov = NULL;
88062232bf4SGerd Hoffmann     res->iov_cnt = 0;
8810c244e50SGerd Hoffmann     g_free(res->addrs);
8820c244e50SGerd Hoffmann     res->addrs = NULL;
883e0933d91SVivek Kasireddy 
884e0933d91SVivek Kasireddy     if (res->blob) {
885e0933d91SVivek Kasireddy         virtio_gpu_fini_udmabuf(res);
886e0933d91SVivek Kasireddy     }
88762232bf4SGerd Hoffmann }
88862232bf4SGerd Hoffmann 
88962232bf4SGerd Hoffmann static void
89062232bf4SGerd Hoffmann virtio_gpu_resource_attach_backing(VirtIOGPU *g,
89162232bf4SGerd Hoffmann                                    struct virtio_gpu_ctrl_command *cmd)
89262232bf4SGerd Hoffmann {
89362232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
89462232bf4SGerd Hoffmann     struct virtio_gpu_resource_attach_backing ab;
89562232bf4SGerd Hoffmann     int ret;
89662232bf4SGerd Hoffmann 
89762232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(ab);
8981715d6b5SFarhan Ali     virtio_gpu_bswap_32(&ab, sizeof(ab));
89962232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_res_back_attach(ab.resource_id);
90062232bf4SGerd Hoffmann 
90162232bf4SGerd Hoffmann     res = virtio_gpu_find_resource(g, ab.resource_id);
90262232bf4SGerd Hoffmann     if (!res) {
90362232bf4SGerd Hoffmann         qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal resource specified %d\n",
90462232bf4SGerd Hoffmann                       __func__, ab.resource_id);
90562232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
90662232bf4SGerd Hoffmann         return;
90762232bf4SGerd Hoffmann     }
90862232bf4SGerd Hoffmann 
909204f01b3SLi Qiang     if (res->iov) {
910204f01b3SLi Qiang         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
911204f01b3SLi Qiang         return;
912204f01b3SLi Qiang     }
913204f01b3SLi Qiang 
91470d37662SVivek Kasireddy     ret = virtio_gpu_create_mapping_iov(g, ab.nr_entries, sizeof(ab), cmd,
91570d37662SVivek Kasireddy                                         &res->addrs, &res->iov, &res->iov_cnt);
91662232bf4SGerd Hoffmann     if (ret != 0) {
91762232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
91862232bf4SGerd Hoffmann         return;
91962232bf4SGerd Hoffmann     }
92062232bf4SGerd Hoffmann }
92162232bf4SGerd Hoffmann 
92262232bf4SGerd Hoffmann static void
92362232bf4SGerd Hoffmann virtio_gpu_resource_detach_backing(VirtIOGPU *g,
92462232bf4SGerd Hoffmann                                    struct virtio_gpu_ctrl_command *cmd)
92562232bf4SGerd Hoffmann {
92662232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
92762232bf4SGerd Hoffmann     struct virtio_gpu_resource_detach_backing detach;
92862232bf4SGerd Hoffmann 
92962232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(detach);
9301715d6b5SFarhan Ali     virtio_gpu_bswap_32(&detach, sizeof(detach));
93162232bf4SGerd Hoffmann     trace_virtio_gpu_cmd_res_back_detach(detach.resource_id);
93262232bf4SGerd Hoffmann 
93325c001a4SVivek Kasireddy     res = virtio_gpu_find_check_resource(g, detach.resource_id, true,
93425c001a4SVivek Kasireddy                                          __func__, &cmd->error);
93525c001a4SVivek Kasireddy     if (!res) {
93662232bf4SGerd Hoffmann         return;
93762232bf4SGerd Hoffmann     }
9383bb68f79SGerd Hoffmann     virtio_gpu_cleanup_mapping(g, res);
93962232bf4SGerd Hoffmann }
94062232bf4SGerd Hoffmann 
9412f47691aSGerd Hoffmann void virtio_gpu_simple_process_cmd(VirtIOGPU *g,
94262232bf4SGerd Hoffmann                                    struct virtio_gpu_ctrl_command *cmd)
94362232bf4SGerd Hoffmann {
94462232bf4SGerd Hoffmann     VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
9451715d6b5SFarhan Ali     virtio_gpu_ctrl_hdr_bswap(&cmd->cmd_hdr);
94662232bf4SGerd Hoffmann 
94762232bf4SGerd Hoffmann     switch (cmd->cmd_hdr.type) {
94862232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_GET_DISPLAY_INFO:
94962232bf4SGerd Hoffmann         virtio_gpu_get_display_info(g, cmd);
95062232bf4SGerd Hoffmann         break;
9511ed2cb32SGerd Hoffmann     case VIRTIO_GPU_CMD_GET_EDID:
9521ed2cb32SGerd Hoffmann         virtio_gpu_get_edid(g, cmd);
9531ed2cb32SGerd Hoffmann         break;
95462232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D:
95562232bf4SGerd Hoffmann         virtio_gpu_resource_create_2d(g, cmd);
95662232bf4SGerd Hoffmann         break;
957e0933d91SVivek Kasireddy     case VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB:
958e0933d91SVivek Kasireddy         if (!virtio_gpu_blob_enabled(g->parent_obj.conf)) {
959e0933d91SVivek Kasireddy             cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
960e0933d91SVivek Kasireddy             break;
961e0933d91SVivek Kasireddy         }
962e0933d91SVivek Kasireddy         virtio_gpu_resource_create_blob(g, cmd);
963e0933d91SVivek Kasireddy         break;
96462232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_RESOURCE_UNREF:
96562232bf4SGerd Hoffmann         virtio_gpu_resource_unref(g, cmd);
96662232bf4SGerd Hoffmann         break;
96762232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
96862232bf4SGerd Hoffmann         virtio_gpu_resource_flush(g, cmd);
96962232bf4SGerd Hoffmann         break;
97062232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D:
97162232bf4SGerd Hoffmann         virtio_gpu_transfer_to_host_2d(g, cmd);
97262232bf4SGerd Hoffmann         break;
97362232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_SET_SCANOUT:
97462232bf4SGerd Hoffmann         virtio_gpu_set_scanout(g, cmd);
97562232bf4SGerd Hoffmann         break;
97632db3c63SVivek Kasireddy     case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
97732db3c63SVivek Kasireddy         if (!virtio_gpu_blob_enabled(g->parent_obj.conf)) {
97832db3c63SVivek Kasireddy             cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
97932db3c63SVivek Kasireddy             break;
98032db3c63SVivek Kasireddy         }
98132db3c63SVivek Kasireddy         virtio_gpu_set_scanout_blob(g, cmd);
98232db3c63SVivek Kasireddy         break;
98362232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING:
98462232bf4SGerd Hoffmann         virtio_gpu_resource_attach_backing(g, cmd);
98562232bf4SGerd Hoffmann         break;
98662232bf4SGerd Hoffmann     case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
98762232bf4SGerd Hoffmann         virtio_gpu_resource_detach_backing(g, cmd);
98862232bf4SGerd Hoffmann         break;
98962232bf4SGerd Hoffmann     default:
99062232bf4SGerd Hoffmann         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
99162232bf4SGerd Hoffmann         break;
99262232bf4SGerd Hoffmann     }
99362232bf4SGerd Hoffmann     if (!cmd->finished) {
994b3a5dfdeSVivek Kasireddy         if (!g->parent_obj.renderer_blocked) {
99562232bf4SGerd Hoffmann             virtio_gpu_ctrl_response_nodata(g, cmd, cmd->error ? cmd->error :
99662232bf4SGerd Hoffmann                                             VIRTIO_GPU_RESP_OK_NODATA);
99762232bf4SGerd Hoffmann         }
99862232bf4SGerd Hoffmann     }
999b3a5dfdeSVivek Kasireddy }
100062232bf4SGerd Hoffmann 
100162232bf4SGerd Hoffmann static void virtio_gpu_handle_ctrl_cb(VirtIODevice *vdev, VirtQueue *vq)
100262232bf4SGerd Hoffmann {
100362232bf4SGerd Hoffmann     VirtIOGPU *g = VIRTIO_GPU(vdev);
100462232bf4SGerd Hoffmann     qemu_bh_schedule(g->ctrl_bh);
100562232bf4SGerd Hoffmann }
100662232bf4SGerd Hoffmann 
100762232bf4SGerd Hoffmann static void virtio_gpu_handle_cursor_cb(VirtIODevice *vdev, VirtQueue *vq)
100862232bf4SGerd Hoffmann {
100962232bf4SGerd Hoffmann     VirtIOGPU *g = VIRTIO_GPU(vdev);
101062232bf4SGerd Hoffmann     qemu_bh_schedule(g->cursor_bh);
101162232bf4SGerd Hoffmann }
101262232bf4SGerd Hoffmann 
10130c55a1cfSGerd Hoffmann void virtio_gpu_process_cmdq(VirtIOGPU *g)
10143eb769fdSGerd Hoffmann {
10153eb769fdSGerd Hoffmann     struct virtio_gpu_ctrl_command *cmd;
10162f47691aSGerd Hoffmann     VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
10173eb769fdSGerd Hoffmann 
1018f8f3c271SMarc-André Lureau     if (g->processing_cmdq) {
1019f8f3c271SMarc-André Lureau         return;
1020f8f3c271SMarc-André Lureau     }
1021f8f3c271SMarc-André Lureau     g->processing_cmdq = true;
10223eb769fdSGerd Hoffmann     while (!QTAILQ_EMPTY(&g->cmdq)) {
10233eb769fdSGerd Hoffmann         cmd = QTAILQ_FIRST(&g->cmdq);
10243eb769fdSGerd Hoffmann 
102550d8e25eSMarc-André Lureau         if (g->parent_obj.renderer_blocked) {
10260c55a1cfSGerd Hoffmann             break;
10270c55a1cfSGerd Hoffmann         }
1028ad341aacSMarc-André Lureau 
1029ad341aacSMarc-André Lureau         /* process command */
10302f47691aSGerd Hoffmann         vgc->process_cmd(g, cmd);
1031ad341aacSMarc-André Lureau 
10323eb769fdSGerd Hoffmann         QTAILQ_REMOVE(&g->cmdq, cmd, next);
103350d8e25eSMarc-André Lureau         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
10343eb769fdSGerd Hoffmann             g->stats.requests++;
10353eb769fdSGerd Hoffmann         }
10363eb769fdSGerd Hoffmann 
10373eb769fdSGerd Hoffmann         if (!cmd->finished) {
10383eb769fdSGerd Hoffmann             QTAILQ_INSERT_TAIL(&g->fenceq, cmd, next);
10393eb769fdSGerd Hoffmann             g->inflight++;
104050d8e25eSMarc-André Lureau             if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
10413eb769fdSGerd Hoffmann                 if (g->stats.max_inflight < g->inflight) {
10423eb769fdSGerd Hoffmann                     g->stats.max_inflight = g->inflight;
10433eb769fdSGerd Hoffmann                 }
10443eb769fdSGerd Hoffmann                 fprintf(stderr, "inflight: %3d (+)\r", g->inflight);
10453eb769fdSGerd Hoffmann             }
10463eb769fdSGerd Hoffmann         } else {
10473eb769fdSGerd Hoffmann             g_free(cmd);
10483eb769fdSGerd Hoffmann         }
10493eb769fdSGerd Hoffmann     }
1050f8f3c271SMarc-André Lureau     g->processing_cmdq = false;
10513eb769fdSGerd Hoffmann }
10523eb769fdSGerd Hoffmann 
1053b3a5dfdeSVivek Kasireddy static void virtio_gpu_process_fenceq(VirtIOGPU *g)
1054b3a5dfdeSVivek Kasireddy {
1055b3a5dfdeSVivek Kasireddy     struct virtio_gpu_ctrl_command *cmd, *tmp;
1056b3a5dfdeSVivek Kasireddy 
1057b3a5dfdeSVivek Kasireddy     QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
1058b3a5dfdeSVivek Kasireddy         trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id);
1059b3a5dfdeSVivek Kasireddy         virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
1060b3a5dfdeSVivek Kasireddy         QTAILQ_REMOVE(&g->fenceq, cmd, next);
1061b3a5dfdeSVivek Kasireddy         g_free(cmd);
1062b3a5dfdeSVivek Kasireddy         g->inflight--;
1063b3a5dfdeSVivek Kasireddy         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
1064b3a5dfdeSVivek Kasireddy             fprintf(stderr, "inflight: %3d (-)\r", g->inflight);
1065b3a5dfdeSVivek Kasireddy         }
1066b3a5dfdeSVivek Kasireddy     }
1067b3a5dfdeSVivek Kasireddy }
1068b3a5dfdeSVivek Kasireddy 
1069b3a5dfdeSVivek Kasireddy static void virtio_gpu_handle_gl_flushed(VirtIOGPUBase *b)
1070b3a5dfdeSVivek Kasireddy {
1071b3a5dfdeSVivek Kasireddy     VirtIOGPU *g = container_of(b, VirtIOGPU, parent_obj);
1072b3a5dfdeSVivek Kasireddy 
1073b3a5dfdeSVivek Kasireddy     virtio_gpu_process_fenceq(g);
1074b3a5dfdeSVivek Kasireddy     virtio_gpu_process_cmdq(g);
1075b3a5dfdeSVivek Kasireddy }
1076b3a5dfdeSVivek Kasireddy 
107762232bf4SGerd Hoffmann static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
107862232bf4SGerd Hoffmann {
107962232bf4SGerd Hoffmann     VirtIOGPU *g = VIRTIO_GPU(vdev);
108062232bf4SGerd Hoffmann     struct virtio_gpu_ctrl_command *cmd;
108162232bf4SGerd Hoffmann 
108262232bf4SGerd Hoffmann     if (!virtio_queue_ready(vq)) {
108362232bf4SGerd Hoffmann         return;
108462232bf4SGerd Hoffmann     }
108562232bf4SGerd Hoffmann 
108651b19ebeSPaolo Bonzini     cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
108751b19ebeSPaolo Bonzini     while (cmd) {
108862232bf4SGerd Hoffmann         cmd->vq = vq;
108962232bf4SGerd Hoffmann         cmd->error = 0;
109062232bf4SGerd Hoffmann         cmd->finished = false;
10913eb769fdSGerd Hoffmann         QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
109251b19ebeSPaolo Bonzini         cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
109362232bf4SGerd Hoffmann     }
10949d9e1521SGerd Hoffmann 
10953eb769fdSGerd Hoffmann     virtio_gpu_process_cmdq(g);
109662232bf4SGerd Hoffmann }
109762232bf4SGerd Hoffmann 
109862232bf4SGerd Hoffmann static void virtio_gpu_ctrl_bh(void *opaque)
109962232bf4SGerd Hoffmann {
110062232bf4SGerd Hoffmann     VirtIOGPU *g = opaque;
1101cabbe8e5SGerd Hoffmann     VirtIOGPUClass *vgc = VIRTIO_GPU_GET_CLASS(g);
1102cabbe8e5SGerd Hoffmann 
1103cabbe8e5SGerd Hoffmann     vgc->handle_ctrl(&g->parent_obj.parent_obj, g->ctrl_vq);
110462232bf4SGerd Hoffmann }
110562232bf4SGerd Hoffmann 
110662232bf4SGerd Hoffmann static void virtio_gpu_handle_cursor(VirtIODevice *vdev, VirtQueue *vq)
110762232bf4SGerd Hoffmann {
110862232bf4SGerd Hoffmann     VirtIOGPU *g = VIRTIO_GPU(vdev);
110951b19ebeSPaolo Bonzini     VirtQueueElement *elem;
111062232bf4SGerd Hoffmann     size_t s;
111162232bf4SGerd Hoffmann     struct virtio_gpu_update_cursor cursor_info;
111262232bf4SGerd Hoffmann 
111362232bf4SGerd Hoffmann     if (!virtio_queue_ready(vq)) {
111462232bf4SGerd Hoffmann         return;
111562232bf4SGerd Hoffmann     }
111651b19ebeSPaolo Bonzini     for (;;) {
111751b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
111851b19ebeSPaolo Bonzini         if (!elem) {
111951b19ebeSPaolo Bonzini             break;
112051b19ebeSPaolo Bonzini         }
112151b19ebeSPaolo Bonzini 
112251b19ebeSPaolo Bonzini         s = iov_to_buf(elem->out_sg, elem->out_num, 0,
112362232bf4SGerd Hoffmann                        &cursor_info, sizeof(cursor_info));
112462232bf4SGerd Hoffmann         if (s != sizeof(cursor_info)) {
112562232bf4SGerd Hoffmann             qemu_log_mask(LOG_GUEST_ERROR,
112662232bf4SGerd Hoffmann                           "%s: cursor size incorrect %zu vs %zu\n",
112762232bf4SGerd Hoffmann                           __func__, s, sizeof(cursor_info));
112862232bf4SGerd Hoffmann         } else {
11291715d6b5SFarhan Ali             virtio_gpu_bswap_32(&cursor_info, sizeof(cursor_info));
113062232bf4SGerd Hoffmann             update_cursor(g, &cursor_info);
113162232bf4SGerd Hoffmann         }
113251b19ebeSPaolo Bonzini         virtqueue_push(vq, elem, 0);
113362232bf4SGerd Hoffmann         virtio_notify(vdev, vq);
113451b19ebeSPaolo Bonzini         g_free(elem);
113562232bf4SGerd Hoffmann     }
113662232bf4SGerd Hoffmann }
113762232bf4SGerd Hoffmann 
113862232bf4SGerd Hoffmann static void virtio_gpu_cursor_bh(void *opaque)
113962232bf4SGerd Hoffmann {
114062232bf4SGerd Hoffmann     VirtIOGPU *g = opaque;
114150d8e25eSMarc-André Lureau     virtio_gpu_handle_cursor(&g->parent_obj.parent_obj, g->cursor_vq);
114262232bf4SGerd Hoffmann }
114362232bf4SGerd Hoffmann 
11440c244e50SGerd Hoffmann static const VMStateDescription vmstate_virtio_gpu_scanout = {
11450c244e50SGerd Hoffmann     .name = "virtio-gpu-one-scanout",
11460c244e50SGerd Hoffmann     .version_id = 1,
11470c244e50SGerd Hoffmann     .fields = (VMStateField[]) {
11480c244e50SGerd Hoffmann         VMSTATE_UINT32(resource_id, struct virtio_gpu_scanout),
11490c244e50SGerd Hoffmann         VMSTATE_UINT32(width, struct virtio_gpu_scanout),
11500c244e50SGerd Hoffmann         VMSTATE_UINT32(height, struct virtio_gpu_scanout),
11510c244e50SGerd Hoffmann         VMSTATE_INT32(x, struct virtio_gpu_scanout),
11520c244e50SGerd Hoffmann         VMSTATE_INT32(y, struct virtio_gpu_scanout),
11530c244e50SGerd Hoffmann         VMSTATE_UINT32(cursor.resource_id, struct virtio_gpu_scanout),
11540c244e50SGerd Hoffmann         VMSTATE_UINT32(cursor.hot_x, struct virtio_gpu_scanout),
11550c244e50SGerd Hoffmann         VMSTATE_UINT32(cursor.hot_y, struct virtio_gpu_scanout),
11560c244e50SGerd Hoffmann         VMSTATE_UINT32(cursor.pos.x, struct virtio_gpu_scanout),
11570c244e50SGerd Hoffmann         VMSTATE_UINT32(cursor.pos.y, struct virtio_gpu_scanout),
11580c244e50SGerd Hoffmann         VMSTATE_END_OF_LIST()
11590c244e50SGerd Hoffmann     },
11600c244e50SGerd Hoffmann };
11610c244e50SGerd Hoffmann 
11620c244e50SGerd Hoffmann static const VMStateDescription vmstate_virtio_gpu_scanouts = {
11630c244e50SGerd Hoffmann     .name = "virtio-gpu-scanouts",
11640c244e50SGerd Hoffmann     .version_id = 1,
11650c244e50SGerd Hoffmann     .fields = (VMStateField[]) {
116650d8e25eSMarc-André Lureau         VMSTATE_INT32(parent_obj.enable, struct VirtIOGPU),
116750d8e25eSMarc-André Lureau         VMSTATE_UINT32_EQUAL(parent_obj.conf.max_outputs,
116850d8e25eSMarc-André Lureau                              struct VirtIOGPU, NULL),
116950d8e25eSMarc-André Lureau         VMSTATE_STRUCT_VARRAY_UINT32(parent_obj.scanout, struct VirtIOGPU,
117050d8e25eSMarc-André Lureau                                      parent_obj.conf.max_outputs, 1,
11710c244e50SGerd Hoffmann                                      vmstate_virtio_gpu_scanout,
11720c244e50SGerd Hoffmann                                      struct virtio_gpu_scanout),
11730c244e50SGerd Hoffmann         VMSTATE_END_OF_LIST()
11740c244e50SGerd Hoffmann     },
11750c244e50SGerd Hoffmann };
11760c244e50SGerd Hoffmann 
11772c21ee76SJianjun Duan static int virtio_gpu_save(QEMUFile *f, void *opaque, size_t size,
11783ddba9a9SMarkus Armbruster                            const VMStateField *field, JSONWriter *vmdesc)
11790c244e50SGerd Hoffmann {
11800c244e50SGerd Hoffmann     VirtIOGPU *g = opaque;
11810c244e50SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
11820c244e50SGerd Hoffmann     int i;
11830c244e50SGerd Hoffmann 
11840c244e50SGerd Hoffmann     /* in 2d mode we should never find unprocessed commands here */
11850c244e50SGerd Hoffmann     assert(QTAILQ_EMPTY(&g->cmdq));
11860c244e50SGerd Hoffmann 
11870c244e50SGerd Hoffmann     QTAILQ_FOREACH(res, &g->reslist, next) {
11880c244e50SGerd Hoffmann         qemu_put_be32(f, res->resource_id);
11890c244e50SGerd Hoffmann         qemu_put_be32(f, res->width);
11900c244e50SGerd Hoffmann         qemu_put_be32(f, res->height);
11910c244e50SGerd Hoffmann         qemu_put_be32(f, res->format);
11920c244e50SGerd Hoffmann         qemu_put_be32(f, res->iov_cnt);
11930c244e50SGerd Hoffmann         for (i = 0; i < res->iov_cnt; i++) {
11940c244e50SGerd Hoffmann             qemu_put_be64(f, res->addrs[i]);
11950c244e50SGerd Hoffmann             qemu_put_be32(f, res->iov[i].iov_len);
11960c244e50SGerd Hoffmann         }
11970c244e50SGerd Hoffmann         qemu_put_buffer(f, (void *)pixman_image_get_data(res->image),
11980c244e50SGerd Hoffmann                         pixman_image_get_stride(res->image) * res->height);
11990c244e50SGerd Hoffmann     }
12000c244e50SGerd Hoffmann     qemu_put_be32(f, 0); /* end of list */
12010c244e50SGerd Hoffmann 
12022f168d07SDr. David Alan Gilbert     return vmstate_save_state(f, &vmstate_virtio_gpu_scanouts, g, NULL);
12030c244e50SGerd Hoffmann }
12040c244e50SGerd Hoffmann 
12052c21ee76SJianjun Duan static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
120603fee66fSMarc-André Lureau                            const VMStateField *field)
12070c244e50SGerd Hoffmann {
12080c244e50SGerd Hoffmann     VirtIOGPU *g = opaque;
12090c244e50SGerd Hoffmann     struct virtio_gpu_simple_resource *res;
12100c244e50SGerd Hoffmann     struct virtio_gpu_scanout *scanout;
12110c244e50SGerd Hoffmann     uint32_t resource_id, pformat;
12128a502efdSHalil Pasic     int i;
12130c244e50SGerd Hoffmann 
1214039aa5dbSPeter Maydell     g->hostmem = 0;
1215039aa5dbSPeter Maydell 
12160c244e50SGerd Hoffmann     resource_id = qemu_get_be32(f);
12170c244e50SGerd Hoffmann     while (resource_id != 0) {
1218b0ee78ffSLi Qiang         res = virtio_gpu_find_resource(g, resource_id);
1219b0ee78ffSLi Qiang         if (res) {
1220b0ee78ffSLi Qiang             return -EINVAL;
1221b0ee78ffSLi Qiang         }
1222b0ee78ffSLi Qiang 
12230c244e50SGerd Hoffmann         res = g_new0(struct virtio_gpu_simple_resource, 1);
12240c244e50SGerd Hoffmann         res->resource_id = resource_id;
12250c244e50SGerd Hoffmann         res->width = qemu_get_be32(f);
12260c244e50SGerd Hoffmann         res->height = qemu_get_be32(f);
12270c244e50SGerd Hoffmann         res->format = qemu_get_be32(f);
12280c244e50SGerd Hoffmann         res->iov_cnt = qemu_get_be32(f);
12290c244e50SGerd Hoffmann 
12300c244e50SGerd Hoffmann         /* allocate */
123183a7d3c0SMarc-André Lureau         pformat = virtio_gpu_get_pixman_format(res->format);
12320c244e50SGerd Hoffmann         if (!pformat) {
1233c84f0f25SPeter Maydell             g_free(res);
12340c244e50SGerd Hoffmann             return -EINVAL;
12350c244e50SGerd Hoffmann         }
12360c244e50SGerd Hoffmann         res->image = pixman_image_create_bits(pformat,
12370c244e50SGerd Hoffmann                                               res->width, res->height,
12380c244e50SGerd Hoffmann                                               NULL, 0);
12390c244e50SGerd Hoffmann         if (!res->image) {
1240c84f0f25SPeter Maydell             g_free(res);
12410c244e50SGerd Hoffmann             return -EINVAL;
12420c244e50SGerd Hoffmann         }
12430c244e50SGerd Hoffmann 
1244c53f5b89STao Wu         res->hostmem = calc_image_hostmem(pformat, res->width, res->height);
1245039aa5dbSPeter Maydell 
12460c244e50SGerd Hoffmann         res->addrs = g_new(uint64_t, res->iov_cnt);
12470c244e50SGerd Hoffmann         res->iov = g_new(struct iovec, res->iov_cnt);
12480c244e50SGerd Hoffmann 
12490c244e50SGerd Hoffmann         /* read data */
12500c244e50SGerd Hoffmann         for (i = 0; i < res->iov_cnt; i++) {
12510c244e50SGerd Hoffmann             res->addrs[i] = qemu_get_be64(f);
12520c244e50SGerd Hoffmann             res->iov[i].iov_len = qemu_get_be32(f);
12530c244e50SGerd Hoffmann         }
12540c244e50SGerd Hoffmann         qemu_get_buffer(f, (void *)pixman_image_get_data(res->image),
12550c244e50SGerd Hoffmann                         pixman_image_get_stride(res->image) * res->height);
12560c244e50SGerd Hoffmann 
12570c244e50SGerd Hoffmann         /* restore mapping */
12580c244e50SGerd Hoffmann         for (i = 0; i < res->iov_cnt; i++) {
12590c244e50SGerd Hoffmann             hwaddr len = res->iov[i].iov_len;
12600c244e50SGerd Hoffmann             res->iov[i].iov_base =
1261a1d4b0a3SPhilippe Mathieu-Daudé                 dma_memory_map(VIRTIO_DEVICE(g)->dma_as, res->addrs[i], &len,
1262a1d4b0a3SPhilippe Mathieu-Daudé                                DMA_DIRECTION_TO_DEVICE,
1263a1d4b0a3SPhilippe Mathieu-Daudé                                MEMTXATTRS_UNSPECIFIED);
12643bb68f79SGerd Hoffmann 
12650c244e50SGerd Hoffmann             if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {
1266c84f0f25SPeter Maydell                 /* Clean up the half-a-mapping we just created... */
1267c84f0f25SPeter Maydell                 if (res->iov[i].iov_base) {
12688da132a5SGerd Hoffmann                     dma_memory_unmap(VIRTIO_DEVICE(g)->dma_as,
12698da132a5SGerd Hoffmann                                      res->iov[i].iov_base,
1270a7f85e03SGerd Hoffmann                                      len,
12718da132a5SGerd Hoffmann                                      DMA_DIRECTION_TO_DEVICE,
1272a7f85e03SGerd Hoffmann                                      0);
1273c84f0f25SPeter Maydell                 }
1274c84f0f25SPeter Maydell                 /* ...and the mappings for previous loop iterations */
1275c84f0f25SPeter Maydell                 res->iov_cnt = i;
12763bb68f79SGerd Hoffmann                 virtio_gpu_cleanup_mapping(g, res);
1277c84f0f25SPeter Maydell                 pixman_image_unref(res->image);
1278c84f0f25SPeter Maydell                 g_free(res);
12790c244e50SGerd Hoffmann                 return -EINVAL;
12800c244e50SGerd Hoffmann             }
12810c244e50SGerd Hoffmann         }
12820c244e50SGerd Hoffmann 
12830c244e50SGerd Hoffmann         QTAILQ_INSERT_HEAD(&g->reslist, res, next);
1284039aa5dbSPeter Maydell         g->hostmem += res->hostmem;
12850c244e50SGerd Hoffmann 
12860c244e50SGerd Hoffmann         resource_id = qemu_get_be32(f);
12870c244e50SGerd Hoffmann     }
12880c244e50SGerd Hoffmann 
12890c244e50SGerd Hoffmann     /* load & apply scanout state */
12900c244e50SGerd Hoffmann     vmstate_load_state(f, &vmstate_virtio_gpu_scanouts, g, 1);
129150d8e25eSMarc-André Lureau     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
129250d8e25eSMarc-André Lureau         scanout = &g->parent_obj.scanout[i];
12930c244e50SGerd Hoffmann         if (!scanout->resource_id) {
12940c244e50SGerd Hoffmann             continue;
12950c244e50SGerd Hoffmann         }
12960c244e50SGerd Hoffmann         res = virtio_gpu_find_resource(g, scanout->resource_id);
12970c244e50SGerd Hoffmann         if (!res) {
12980c244e50SGerd Hoffmann             return -EINVAL;
12990c244e50SGerd Hoffmann         }
13000c244e50SGerd Hoffmann         scanout->ds = qemu_create_displaysurface_pixman(res->image);
13010c244e50SGerd Hoffmann         if (!scanout->ds) {
13020c244e50SGerd Hoffmann             return -EINVAL;
13030c244e50SGerd Hoffmann         }
13040c244e50SGerd Hoffmann 
13050c244e50SGerd Hoffmann         dpy_gfx_replace_surface(scanout->con, scanout->ds);
130691155f8bSGerd Hoffmann         dpy_gfx_update_full(scanout->con);
130710750ee0SGerd Hoffmann         if (scanout->cursor.resource_id) {
13080c244e50SGerd Hoffmann             update_cursor(g, &scanout->cursor);
130910750ee0SGerd Hoffmann         }
13100c244e50SGerd Hoffmann         res->scanout_bitmask |= (1 << i);
13110c244e50SGerd Hoffmann     }
13120c244e50SGerd Hoffmann 
13130c244e50SGerd Hoffmann     return 0;
13140c244e50SGerd Hoffmann }
13150c244e50SGerd Hoffmann 
131637f86af0SGerd Hoffmann void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
131762232bf4SGerd Hoffmann {
131862232bf4SGerd Hoffmann     VirtIODevice *vdev = VIRTIO_DEVICE(qdev);
131962232bf4SGerd Hoffmann     VirtIOGPU *g = VIRTIO_GPU(qdev);
13209d9e1521SGerd Hoffmann 
1321cce386e1SVivek Kasireddy     if (virtio_gpu_blob_enabled(g->parent_obj.conf)) {
1322cce386e1SVivek Kasireddy         if (!virtio_gpu_have_udmabuf()) {
1323cce386e1SVivek Kasireddy             error_setg(errp, "cannot enable blob resources without udmabuf");
1324cce386e1SVivek Kasireddy             return;
1325cce386e1SVivek Kasireddy         }
1326cce386e1SVivek Kasireddy 
1327cce386e1SVivek Kasireddy         if (virtio_gpu_virgl_enabled(g->parent_obj.conf)) {
1328cce386e1SVivek Kasireddy             error_setg(errp, "blobs and virgl are not compatible (yet)");
1329cce386e1SVivek Kasireddy             return;
1330cce386e1SVivek Kasireddy         }
1331cce386e1SVivek Kasireddy     }
1332cce386e1SVivek Kasireddy 
133350d8e25eSMarc-André Lureau     if (!virtio_gpu_base_device_realize(qdev,
133450d8e25eSMarc-André Lureau                                         virtio_gpu_handle_ctrl_cb,
133550d8e25eSMarc-André Lureau                                         virtio_gpu_handle_cursor_cb,
133650d8e25eSMarc-André Lureau                                         errp)) {
1337fe44dc91SAshijeet Acharya         return;
1338fe44dc91SAshijeet Acharya     }
1339fe44dc91SAshijeet Acharya 
134050d8e25eSMarc-André Lureau     g->ctrl_vq = virtio_get_queue(vdev, 0);
134150d8e25eSMarc-André Lureau     g->cursor_vq = virtio_get_queue(vdev, 1);
134262232bf4SGerd Hoffmann     g->ctrl_bh = qemu_bh_new(virtio_gpu_ctrl_bh, g);
134362232bf4SGerd Hoffmann     g->cursor_bh = qemu_bh_new(virtio_gpu_cursor_bh, g);
134462232bf4SGerd Hoffmann     QTAILQ_INIT(&g->reslist);
13453eb769fdSGerd Hoffmann     QTAILQ_INIT(&g->cmdq);
134662232bf4SGerd Hoffmann     QTAILQ_INIT(&g->fenceq);
134762232bf4SGerd Hoffmann }
134862232bf4SGerd Hoffmann 
134976fa8b35SGerd Hoffmann void virtio_gpu_reset(VirtIODevice *vdev)
135062232bf4SGerd Hoffmann {
135162232bf4SGerd Hoffmann     VirtIOGPU *g = VIRTIO_GPU(vdev);
135262232bf4SGerd Hoffmann     struct virtio_gpu_simple_resource *res, *tmp;
1353dc84ed5bSGerd Hoffmann     struct virtio_gpu_ctrl_command *cmd;
135462232bf4SGerd Hoffmann 
135562232bf4SGerd Hoffmann     QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) {
135662232bf4SGerd Hoffmann         virtio_gpu_resource_destroy(g, res);
135762232bf4SGerd Hoffmann     }
13589d9e1521SGerd Hoffmann 
1359dc84ed5bSGerd Hoffmann     while (!QTAILQ_EMPTY(&g->cmdq)) {
1360dc84ed5bSGerd Hoffmann         cmd = QTAILQ_FIRST(&g->cmdq);
1361dc84ed5bSGerd Hoffmann         QTAILQ_REMOVE(&g->cmdq, cmd, next);
1362dc84ed5bSGerd Hoffmann         g_free(cmd);
1363dc84ed5bSGerd Hoffmann     }
1364dc84ed5bSGerd Hoffmann 
1365dc84ed5bSGerd Hoffmann     while (!QTAILQ_EMPTY(&g->fenceq)) {
1366dc84ed5bSGerd Hoffmann         cmd = QTAILQ_FIRST(&g->fenceq);
1367dc84ed5bSGerd Hoffmann         QTAILQ_REMOVE(&g->fenceq, cmd, next);
1368dc84ed5bSGerd Hoffmann         g->inflight--;
1369dc84ed5bSGerd Hoffmann         g_free(cmd);
1370dc84ed5bSGerd Hoffmann     }
1371dc84ed5bSGerd Hoffmann 
137250d8e25eSMarc-André Lureau     virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
137350d8e25eSMarc-André Lureau }
137450d8e25eSMarc-André Lureau 
137550d8e25eSMarc-André Lureau static void
137650d8e25eSMarc-André Lureau virtio_gpu_get_config(VirtIODevice *vdev, uint8_t *config)
137750d8e25eSMarc-André Lureau {
137850d8e25eSMarc-André Lureau     VirtIOGPUBase *g = VIRTIO_GPU_BASE(vdev);
137950d8e25eSMarc-André Lureau 
138050d8e25eSMarc-André Lureau     memcpy(config, &g->virtio_config, sizeof(g->virtio_config));
138150d8e25eSMarc-André Lureau }
138250d8e25eSMarc-André Lureau 
138350d8e25eSMarc-André Lureau static void
138450d8e25eSMarc-André Lureau virtio_gpu_set_config(VirtIODevice *vdev, const uint8_t *config)
138550d8e25eSMarc-André Lureau {
138650d8e25eSMarc-André Lureau     VirtIOGPUBase *g = VIRTIO_GPU_BASE(vdev);
138750d8e25eSMarc-André Lureau     const struct virtio_gpu_config *vgconfig =
138850d8e25eSMarc-André Lureau         (const struct virtio_gpu_config *)config;
138950d8e25eSMarc-André Lureau 
139050d8e25eSMarc-André Lureau     if (vgconfig->events_clear) {
139150d8e25eSMarc-André Lureau         g->virtio_config.events_read &= ~vgconfig->events_clear;
139250d8e25eSMarc-André Lureau     }
139362232bf4SGerd Hoffmann }
139462232bf4SGerd Hoffmann 
13958a502efdSHalil Pasic /*
13968a502efdSHalil Pasic  * For historical reasons virtio_gpu does not adhere to virtio migration
13978a502efdSHalil Pasic  * scheme as described in doc/virtio-migration.txt, in a sense that no
13988a502efdSHalil Pasic  * save/load callback are provided to the core. Instead the device data
13998a502efdSHalil Pasic  * is saved/loaded after the core data.
14008a502efdSHalil Pasic  *
14018a502efdSHalil Pasic  * Because of this we need a special vmsd.
14028a502efdSHalil Pasic  */
14038a502efdSHalil Pasic static const VMStateDescription vmstate_virtio_gpu = {
14048a502efdSHalil Pasic     .name = "virtio-gpu",
14058a502efdSHalil Pasic     .minimum_version_id = VIRTIO_GPU_VM_VERSION,
14068a502efdSHalil Pasic     .version_id = VIRTIO_GPU_VM_VERSION,
14078a502efdSHalil Pasic     .fields = (VMStateField[]) {
14088a502efdSHalil Pasic         VMSTATE_VIRTIO_DEVICE /* core */,
14098a502efdSHalil Pasic         {
14108a502efdSHalil Pasic             .name = "virtio-gpu",
14118a502efdSHalil Pasic             .info = &(const VMStateInfo) {
14128a502efdSHalil Pasic                         .name = "virtio-gpu",
14138a502efdSHalil Pasic                         .get = virtio_gpu_load,
14148a502efdSHalil Pasic                         .put = virtio_gpu_save,
14158a502efdSHalil Pasic             },
14168a502efdSHalil Pasic             .flags = VMS_SINGLE,
14178a502efdSHalil Pasic         } /* device */,
14188a502efdSHalil Pasic         VMSTATE_END_OF_LIST()
14198a502efdSHalil Pasic     },
14208a502efdSHalil Pasic };
14210fc07498SDr. David Alan Gilbert 
142262232bf4SGerd Hoffmann static Property virtio_gpu_properties[] = {
142350d8e25eSMarc-André Lureau     VIRTIO_GPU_BASE_PROPERTIES(VirtIOGPU, parent_obj.conf),
142450d8e25eSMarc-André Lureau     DEFINE_PROP_SIZE("max_hostmem", VirtIOGPU, conf_max_hostmem,
142550d8e25eSMarc-André Lureau                      256 * MiB),
1426cce386e1SVivek Kasireddy     DEFINE_PROP_BIT("blob", VirtIOGPU, parent_obj.conf.flags,
1427cce386e1SVivek Kasireddy                     VIRTIO_GPU_FLAG_BLOB_ENABLED, false),
142862232bf4SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
142962232bf4SGerd Hoffmann };
143062232bf4SGerd Hoffmann 
143162232bf4SGerd Hoffmann static void virtio_gpu_class_init(ObjectClass *klass, void *data)
143262232bf4SGerd Hoffmann {
143362232bf4SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
143462232bf4SGerd Hoffmann     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1435cabbe8e5SGerd Hoffmann     VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass);
1436b3a5dfdeSVivek Kasireddy     VirtIOGPUBaseClass *vgbc = &vgc->parent;
143762232bf4SGerd Hoffmann 
1438cabbe8e5SGerd Hoffmann     vgc->handle_ctrl = virtio_gpu_handle_ctrl;
14392f47691aSGerd Hoffmann     vgc->process_cmd = virtio_gpu_simple_process_cmd;
14402c267d66SGerd Hoffmann     vgc->update_cursor_data = virtio_gpu_update_cursor_data;
1441b3a5dfdeSVivek Kasireddy     vgbc->gl_flushed = virtio_gpu_handle_gl_flushed;
1442cabbe8e5SGerd Hoffmann 
144362232bf4SGerd Hoffmann     vdc->realize = virtio_gpu_device_realize;
144450d8e25eSMarc-André Lureau     vdc->reset = virtio_gpu_reset;
144562232bf4SGerd Hoffmann     vdc->get_config = virtio_gpu_get_config;
144662232bf4SGerd Hoffmann     vdc->set_config = virtio_gpu_set_config;
144762232bf4SGerd Hoffmann 
14480fc07498SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_gpu;
14494f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_gpu_properties);
145062232bf4SGerd Hoffmann }
145162232bf4SGerd Hoffmann 
145262232bf4SGerd Hoffmann static const TypeInfo virtio_gpu_info = {
145362232bf4SGerd Hoffmann     .name = TYPE_VIRTIO_GPU,
145450d8e25eSMarc-André Lureau     .parent = TYPE_VIRTIO_GPU_BASE,
145562232bf4SGerd Hoffmann     .instance_size = sizeof(VirtIOGPU),
1456cabbe8e5SGerd Hoffmann     .class_size = sizeof(VirtIOGPUClass),
145762232bf4SGerd Hoffmann     .class_init = virtio_gpu_class_init,
145862232bf4SGerd Hoffmann };
1459561d0f45SGerd Hoffmann module_obj(TYPE_VIRTIO_GPU);
146024ce7aa7SJose R. Ziviani module_kconfig(VIRTIO_GPU);
146162232bf4SGerd Hoffmann 
146262232bf4SGerd Hoffmann static void virtio_register_types(void)
146362232bf4SGerd Hoffmann {
146462232bf4SGerd Hoffmann     type_register_static(&virtio_gpu_info);
146562232bf4SGerd Hoffmann }
146662232bf4SGerd Hoffmann 
146762232bf4SGerd Hoffmann type_init(virtio_register_types)
1468