vugpu.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Virtio vhost-user GPU Device
  3. *
  4. * Copyright Red Hat, Inc. 2013-2018
  5. *
  6. * Authors:
  7. * Dave Airlie <airlied@redhat.com>
  8. * Gerd Hoffmann <kraxel@redhat.com>
  9. * Marc-André Lureau <marcandre.lureau@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. */
  14. #ifndef VUGPU_H
  15. #define VUGPU_H
  16. #include "qemu/osdep.h"
  17. #include "contrib/libvhost-user/libvhost-user-glib.h"
  18. #include "standard-headers/linux/virtio_gpu.h"
  19. #include "qemu/queue.h"
  20. #include "qemu/iov.h"
  21. #include "qemu/bswap.h"
  22. #include "vugbm.h"
  23. typedef enum VhostUserGpuRequest {
  24. VHOST_USER_GPU_NONE = 0,
  25. VHOST_USER_GPU_GET_PROTOCOL_FEATURES,
  26. VHOST_USER_GPU_SET_PROTOCOL_FEATURES,
  27. VHOST_USER_GPU_GET_DISPLAY_INFO,
  28. VHOST_USER_GPU_CURSOR_POS,
  29. VHOST_USER_GPU_CURSOR_POS_HIDE,
  30. VHOST_USER_GPU_CURSOR_UPDATE,
  31. VHOST_USER_GPU_SCANOUT,
  32. VHOST_USER_GPU_UPDATE,
  33. VHOST_USER_GPU_DMABUF_SCANOUT,
  34. VHOST_USER_GPU_DMABUF_UPDATE,
  35. } VhostUserGpuRequest;
  36. typedef struct VhostUserGpuDisplayInfoReply {
  37. struct virtio_gpu_resp_display_info info;
  38. } VhostUserGpuDisplayInfoReply;
  39. typedef struct VhostUserGpuCursorPos {
  40. uint32_t scanout_id;
  41. uint32_t x;
  42. uint32_t y;
  43. } QEMU_PACKED VhostUserGpuCursorPos;
  44. typedef struct VhostUserGpuCursorUpdate {
  45. VhostUserGpuCursorPos pos;
  46. uint32_t hot_x;
  47. uint32_t hot_y;
  48. uint32_t data[64 * 64];
  49. } QEMU_PACKED VhostUserGpuCursorUpdate;
  50. typedef struct VhostUserGpuScanout {
  51. uint32_t scanout_id;
  52. uint32_t width;
  53. uint32_t height;
  54. } QEMU_PACKED VhostUserGpuScanout;
  55. typedef struct VhostUserGpuUpdate {
  56. uint32_t scanout_id;
  57. uint32_t x;
  58. uint32_t y;
  59. uint32_t width;
  60. uint32_t height;
  61. uint8_t data[];
  62. } QEMU_PACKED VhostUserGpuUpdate;
  63. typedef struct VhostUserGpuDMABUFScanout {
  64. uint32_t scanout_id;
  65. uint32_t x;
  66. uint32_t y;
  67. uint32_t width;
  68. uint32_t height;
  69. uint32_t fd_width;
  70. uint32_t fd_height;
  71. uint32_t fd_stride;
  72. uint32_t fd_flags;
  73. int fd_drm_fourcc;
  74. } QEMU_PACKED VhostUserGpuDMABUFScanout;
  75. typedef struct VhostUserGpuMsg {
  76. uint32_t request; /* VhostUserGpuRequest */
  77. uint32_t flags;
  78. uint32_t size; /* the following payload size */
  79. union {
  80. VhostUserGpuCursorPos cursor_pos;
  81. VhostUserGpuCursorUpdate cursor_update;
  82. VhostUserGpuScanout scanout;
  83. VhostUserGpuUpdate update;
  84. VhostUserGpuDMABUFScanout dmabuf_scanout;
  85. struct virtio_gpu_resp_display_info display_info;
  86. uint64_t u64;
  87. } payload;
  88. } QEMU_PACKED VhostUserGpuMsg;
  89. static VhostUserGpuMsg m __attribute__ ((unused));
  90. #define VHOST_USER_GPU_HDR_SIZE \
  91. (sizeof(m.request) + sizeof(m.flags) + sizeof(m.size))
  92. #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4
  93. struct virtio_gpu_scanout {
  94. uint32_t width, height;
  95. int x, y;
  96. int invalidate;
  97. uint32_t resource_id;
  98. };
  99. typedef struct VuGpu {
  100. VugDev dev;
  101. struct virtio_gpu_config virtio_config;
  102. struct vugbm_device gdev;
  103. int sock_fd;
  104. int drm_rnode_fd;
  105. GSource *renderer_source;
  106. guint wait_ok;
  107. bool virgl;
  108. bool virgl_inited;
  109. uint32_t inflight;
  110. struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUTS];
  111. QTAILQ_HEAD(, virtio_gpu_simple_resource) reslist;
  112. QTAILQ_HEAD(, virtio_gpu_ctrl_command) fenceq;
  113. } VuGpu;
  114. struct virtio_gpu_ctrl_command {
  115. VuVirtqElement elem;
  116. VuVirtq *vq;
  117. struct virtio_gpu_ctrl_hdr cmd_hdr;
  118. uint32_t error;
  119. bool finished;
  120. QTAILQ_ENTRY(virtio_gpu_ctrl_command) next;
  121. };
  122. #define VUGPU_FILL_CMD(out) do { \
  123. size_t s; \
  124. s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
  125. &out, sizeof(out)); \
  126. if (s != sizeof(out)) { \
  127. g_critical("%s: command size incorrect %zu vs %zu", \
  128. __func__, s, sizeof(out)); \
  129. return; \
  130. } \
  131. } while (0)
  132. void vg_ctrl_response(VuGpu *g,
  133. struct virtio_gpu_ctrl_command *cmd,
  134. struct virtio_gpu_ctrl_hdr *resp,
  135. size_t resp_len);
  136. void vg_ctrl_response_nodata(VuGpu *g,
  137. struct virtio_gpu_ctrl_command *cmd,
  138. enum virtio_gpu_ctrl_type type);
  139. int vg_create_mapping_iov(VuGpu *g,
  140. struct virtio_gpu_resource_attach_backing *ab,
  141. struct virtio_gpu_ctrl_command *cmd,
  142. struct iovec **iov);
  143. void vg_get_display_info(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd);
  144. void vg_wait_ok(VuGpu *g);
  145. void vg_send_msg(VuGpu *g, const VhostUserGpuMsg *msg, int fd);
  146. bool vg_recv_msg(VuGpu *g, uint32_t expect_req, uint32_t expect_size,
  147. gpointer payload);
  148. #endif