drm/virtio: add virtio_gpu_vbuf_ctrl_hdr
authorChia-I Wu <olvaffe@gmail.com>
Wed, 5 Feb 2020 18:19:47 +0000 (10:19 -0800)
committerGerd Hoffmann <kraxel@redhat.com>
Thu, 6 Feb 2020 10:58:08 +0000 (11:58 +0100)
It is a helper to return the virtio_gpu_ctrl_hdr in a vbuf.

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Link: http://patchwork.freedesktop.org/patch/msgid/20200205181955.202485-4-olvaffe@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
drivers/gpu/drm/virtio/virtgpu_vq.c

index 2b0a1ea1f7e00f528ac35b28f0fc800bf0818208..fdbf39b99b09ceeb226d04f52c25e7fcc679c78d 100644 (file)
@@ -95,7 +95,8 @@ virtio_gpu_get_vbuf(struct virtio_gpu_device *vgdev,
        if (!vbuf)
                return ERR_PTR(-ENOMEM);
 
-       BUG_ON(size > MAX_INLINE_CMD_SIZE);
+       BUG_ON(size > MAX_INLINE_CMD_SIZE ||
+              size < sizeof(struct virtio_gpu_ctrl_hdr));
        vbuf->buf = (void *)vbuf + sizeof(*vbuf);
        vbuf->size = size;
 
@@ -109,6 +110,16 @@ virtio_gpu_get_vbuf(struct virtio_gpu_device *vgdev,
        return vbuf;
 }
 
+static struct virtio_gpu_ctrl_hdr *
+virtio_gpu_vbuf_ctrl_hdr(struct virtio_gpu_vbuffer *vbuf)
+{
+       /* this assumes a vbuf contains a command that starts with a
+        * virtio_gpu_ctrl_hdr, which is true for both ctrl and cursor
+        * virtqueues.
+        */
+       return (struct virtio_gpu_ctrl_hdr *)vbuf->buf;
+}
+
 static void *virtio_gpu_alloc_cmd(struct virtio_gpu_device *vgdev,
                                  struct virtio_gpu_vbuffer **vbuffer_p,
                                  int size)
@@ -211,7 +222,7 @@ void virtio_gpu_dequeue_ctrl_func(struct work_struct *work)
                if (resp->type != cpu_to_le32(VIRTIO_GPU_RESP_OK_NODATA)) {
                        if (resp->type >= cpu_to_le32(VIRTIO_GPU_RESP_ERR_UNSPEC)) {
                                struct virtio_gpu_ctrl_hdr *cmd;
-                               cmd = (struct virtio_gpu_ctrl_hdr *)entry->buf;
+                               cmd = virtio_gpu_vbuf_ctrl_hdr(entry);
                                DRM_ERROR_RATELIMITED("response 0x%x (command 0x%x)\n",
                                                      le32_to_cpu(resp->type),
                                                      le32_to_cpu(cmd->type));
@@ -338,8 +349,7 @@ static bool virtio_gpu_queue_ctrl_buffer_locked(struct virtio_gpu_device *vgdev,
        ret = virtqueue_add_sgs(vq, sgs, outcnt, incnt, vbuf, GFP_ATOMIC);
        WARN_ON(ret);
 
-       trace_virtio_gpu_cmd_queue(vq,
-               (struct virtio_gpu_ctrl_hdr *)vbuf->buf);
+       trace_virtio_gpu_cmd_queue(vq, virtio_gpu_vbuf_ctrl_hdr(vbuf));
 
        notify = virtqueue_kick_prepare(vq);
 
@@ -458,7 +468,7 @@ retry:
                goto retry;
        } else {
                trace_virtio_gpu_cmd_queue(vq,
-                       (struct virtio_gpu_ctrl_hdr *)vbuf->buf);
+                       virtio_gpu_vbuf_ctrl_hdr(vbuf));
 
                notify = virtqueue_kick_prepare(vq);
        }