vugpu.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "libvhost-user-glib.h"
  17. #include "standard-headers/linux/virtio_gpu.h"
  18. #include "qemu/queue.h"
  19. #include "qemu/iov.h"
  20. #include "qemu/bswap.h"
  21. #include "vugbm.h"
  22. typedef enum VhostUserGpuRequest {
  23. VHOST_USER_GPU_NONE = 0,
  24. VHOST_USER_GPU_GET_PROTOCOL_FEATURES,
  25. VHOST_USER_GPU_SET_PROTOCOL_FEATURES,
  26. VHOST_USER_GPU_GET_DISPLAY_INFO,
  27. VHOST_USER_GPU_CURSOR_POS,
  28. VHOST_USER_GPU_CURSOR_POS_HIDE,
  29. VHOST_USER_GPU_CURSOR_UPDATE,
  30. VHOST_USER_GPU_SCANOUT,
  31. VHOST_USER_GPU_UPDATE,
  32. VHOST_USER_GPU_DMABUF_SCANOUT,
  33. VHOST_USER_GPU_DMABUF_UPDATE,
  34. } VhostUserGpuRequest;
  35. typedef struct VhostUserGpuDisplayInfoReply {
  36. struct virtio_gpu_resp_display_info info;
  37. } VhostUserGpuDisplayInfoReply;
  38. typedef struct VhostUserGpuCursorPos {
  39. uint32_t scanout_id;
  40. uint32_t x;
  41. uint32_t y;
  42. } QEMU_PACKED VhostUserGpuCursorPos;
  43. typedef struct VhostUserGpuCursorUpdate {
  44. VhostUserGpuCursorPos pos;
  45. uint32_t hot_x;
  46. uint32_t hot_y;
  47. uint32_t data[64 * 64];
  48. } QEMU_PACKED VhostUserGpuCursorUpdate;
  49. typedef struct VhostUserGpuScanout {
  50. uint32_t scanout_id;
  51. uint32_t width;
  52. uint32_t height;
  53. } QEMU_PACKED VhostUserGpuScanout;
  54. typedef struct VhostUserGpuUpdate {
  55. uint32_t scanout_id;
  56. uint32_t x;
  57. uint32_t y;
  58. uint32_t width;
  59. uint32_t height;
  60. uint8_t data[];
  61. } QEMU_PACKED VhostUserGpuUpdate;
  62. typedef struct VhostUserGpuDMABUFScanout {
  63. uint32_t scanout_id;
  64. uint32_t x;
  65. uint32_t y;
  66. uint32_t width;
  67. uint32_t height;
  68. uint32_t fd_width;
  69. uint32_t fd_height;
  70. uint32_t fd_stride;
  71. uint32_t fd_flags;
  72. int fd_drm_fourcc;
  73. } QEMU_PACKED VhostUserGpuDMABUFScanout;
  74. typedef struct VhostUserGpuMsg {
  75. uint32_t request; /* VhostUserGpuRequest */
  76. uint32_t flags;
  77. uint32_t size; /* the following payload size */
  78. union {
  79. VhostUserGpuCursorPos cursor_pos;
  80. VhostUserGpuCursorUpdate cursor_update;
  81. VhostUserGpuScanout scanout;
  82. VhostUserGpuUpdate update;
  83. VhostUserGpuDMABUFScanout dmabuf_scanout;
  84. struct virtio_gpu_resp_display_info display_info;
  85. uint64_t u64;
  86. } payload;
  87. } QEMU_PACKED VhostUserGpuMsg;
  88. static VhostUserGpuMsg m __attribute__ ((unused));
  89. #define VHOST_USER_GPU_HDR_SIZE \
  90. (sizeof(m.request) + sizeof(m.flags) + sizeof(m.size))
  91. #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4
  92. struct virtio_gpu_scanout {
  93. uint32_t width, height;
  94. int x, y;
  95. int invalidate;
  96. uint32_t resource_id;
  97. };
  98. typedef struct VuGpu {
  99. VugDev dev;
  100. struct virtio_gpu_config virtio_config;
  101. struct vugbm_device gdev;
  102. int sock_fd;
  103. int drm_rnode_fd;
  104. GSource *renderer_source;
  105. guint wait_in;
  106. bool virgl;
  107. bool virgl_inited;
  108. uint32_t inflight;
  109. struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUTS];
  110. QTAILQ_HEAD(, virtio_gpu_simple_resource) reslist;
  111. QTAILQ_HEAD(, virtio_gpu_ctrl_command) fenceq;
  112. } VuGpu;
  113. enum {
  114. VG_CMD_STATE_NEW,
  115. VG_CMD_STATE_PENDING,
  116. VG_CMD_STATE_FINISHED,
  117. };
  118. struct virtio_gpu_ctrl_command {
  119. VuVirtqElement elem;
  120. VuVirtq *vq;
  121. struct virtio_gpu_ctrl_hdr cmd_hdr;
  122. uint32_t error;
  123. int state;
  124. QTAILQ_ENTRY(virtio_gpu_ctrl_command) next;
  125. };
  126. #define VUGPU_FILL_CMD(out) do { \
  127. size_t s; \
  128. s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
  129. &out, sizeof(out)); \
  130. if (s != sizeof(out)) { \
  131. g_critical("%s: command size incorrect %zu vs %zu", \
  132. __func__, s, sizeof(out)); \
  133. return; \
  134. } \
  135. } while (0)
  136. void vg_ctrl_response(VuGpu *g,
  137. struct virtio_gpu_ctrl_command *cmd,
  138. struct virtio_gpu_ctrl_hdr *resp,
  139. size_t resp_len);
  140. void vg_ctrl_response_nodata(VuGpu *g,
  141. struct virtio_gpu_ctrl_command *cmd,
  142. enum virtio_gpu_ctrl_type type);
  143. int vg_create_mapping_iov(VuGpu *g,
  144. struct virtio_gpu_resource_attach_backing *ab,
  145. struct virtio_gpu_ctrl_command *cmd,
  146. struct iovec **iov);
  147. void vg_cleanup_mapping_iov(VuGpu *g, struct iovec *iov, uint32_t count);
  148. void vg_get_display_info(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd);
  149. void vg_wait_ok(VuGpu *g);
  150. void vg_send_msg(VuGpu *g, const VhostUserGpuMsg *msg, int fd);
  151. bool vg_recv_msg(VuGpu *g, uint32_t expect_req, uint32_t expect_size,
  152. gpointer payload);
  153. #endif