virtio-gpu.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Virtio GPU Device
  3. *
  4. * Copyright Red Hat, Inc. 2013-2014
  5. *
  6. * Authors:
  7. * Dave Airlie <airlied@redhat.com>
  8. * Gerd Hoffmann <kraxel@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2.
  11. * See the COPYING file in the top-level directory.
  12. */
  13. #ifndef HW_VIRTIO_GPU_H
  14. #define HW_VIRTIO_GPU_H
  15. #include "qemu/queue.h"
  16. #include "ui/qemu-pixman.h"
  17. #include "ui/console.h"
  18. #include "hw/virtio/virtio.h"
  19. #include "hw/pci/pci.h"
  20. #include "standard-headers/linux/virtio_gpu.h"
  21. #define TYPE_VIRTIO_GPU "virtio-gpu-device"
  22. #define VIRTIO_GPU(obj) \
  23. OBJECT_CHECK(VirtIOGPU, (obj), TYPE_VIRTIO_GPU)
  24. #define VIRTIO_ID_GPU 16
  25. struct virtio_gpu_simple_resource {
  26. uint32_t resource_id;
  27. uint32_t width;
  28. uint32_t height;
  29. uint32_t format;
  30. uint64_t *addrs;
  31. struct iovec *iov;
  32. unsigned int iov_cnt;
  33. uint32_t scanout_bitmask;
  34. pixman_image_t *image;
  35. QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
  36. };
  37. struct virtio_gpu_scanout {
  38. QemuConsole *con;
  39. DisplaySurface *ds;
  40. uint32_t width, height;
  41. int x, y;
  42. int invalidate;
  43. uint32_t resource_id;
  44. struct virtio_gpu_update_cursor cursor;
  45. QEMUCursor *current_cursor;
  46. };
  47. struct virtio_gpu_requested_state {
  48. uint32_t width, height;
  49. int x, y;
  50. };
  51. enum virtio_gpu_conf_flags {
  52. VIRTIO_GPU_FLAG_VIRGL_ENABLED = 1,
  53. VIRTIO_GPU_FLAG_STATS_ENABLED,
  54. };
  55. #define virtio_gpu_virgl_enabled(_cfg) \
  56. (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED))
  57. #define virtio_gpu_stats_enabled(_cfg) \
  58. (_cfg.flags & (1 << VIRTIO_GPU_FLAG_STATS_ENABLED))
  59. struct virtio_gpu_conf {
  60. uint32_t max_outputs;
  61. uint32_t flags;
  62. };
  63. struct virtio_gpu_ctrl_command {
  64. VirtQueueElement elem;
  65. VirtQueue *vq;
  66. struct virtio_gpu_ctrl_hdr cmd_hdr;
  67. uint32_t error;
  68. bool waiting;
  69. bool finished;
  70. QTAILQ_ENTRY(virtio_gpu_ctrl_command) next;
  71. };
  72. typedef struct VirtIOGPU {
  73. VirtIODevice parent_obj;
  74. QEMUBH *ctrl_bh;
  75. QEMUBH *cursor_bh;
  76. VirtQueue *ctrl_vq;
  77. VirtQueue *cursor_vq;
  78. int enable;
  79. int config_size;
  80. DeviceState *qdev;
  81. QTAILQ_HEAD(, virtio_gpu_simple_resource) reslist;
  82. QTAILQ_HEAD(, virtio_gpu_ctrl_command) cmdq;
  83. QTAILQ_HEAD(, virtio_gpu_ctrl_command) fenceq;
  84. struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUTS];
  85. struct virtio_gpu_requested_state req_state[VIRTIO_GPU_MAX_SCANOUTS];
  86. struct virtio_gpu_conf conf;
  87. int enabled_output_bitmask;
  88. struct virtio_gpu_config virtio_config;
  89. bool use_virgl_renderer;
  90. bool renderer_inited;
  91. int renderer_blocked;
  92. QEMUTimer *fence_poll;
  93. QEMUTimer *print_stats;
  94. uint32_t inflight;
  95. struct {
  96. uint32_t max_inflight;
  97. uint32_t requests;
  98. uint32_t req_3d;
  99. uint32_t bytes_3d;
  100. } stats;
  101. } VirtIOGPU;
  102. extern const GraphicHwOps virtio_gpu_ops;
  103. /* to share between PCI and VGA */
  104. #define DEFINE_VIRTIO_GPU_PCI_PROPERTIES(_state) \
  105. DEFINE_PROP_BIT("ioeventfd", _state, flags, \
  106. VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), \
  107. DEFINE_PROP_UINT32("vectors", _state, nvectors, 3)
  108. #define VIRTIO_GPU_FILL_CMD(out) do { \
  109. size_t s; \
  110. s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
  111. &out, sizeof(out)); \
  112. if (s != sizeof(out)) { \
  113. qemu_log_mask(LOG_GUEST_ERROR, \
  114. "%s: command size incorrect %zu vs %zu\n", \
  115. __func__, s, sizeof(out)); \
  116. return; \
  117. } \
  118. } while (0)
  119. /* virtio-gpu.c */
  120. void virtio_gpu_ctrl_response(VirtIOGPU *g,
  121. struct virtio_gpu_ctrl_command *cmd,
  122. struct virtio_gpu_ctrl_hdr *resp,
  123. size_t resp_len);
  124. void virtio_gpu_ctrl_response_nodata(VirtIOGPU *g,
  125. struct virtio_gpu_ctrl_command *cmd,
  126. enum virtio_gpu_ctrl_type type);
  127. void virtio_gpu_get_display_info(VirtIOGPU *g,
  128. struct virtio_gpu_ctrl_command *cmd);
  129. int virtio_gpu_create_mapping_iov(struct virtio_gpu_resource_attach_backing *ab,
  130. struct virtio_gpu_ctrl_command *cmd,
  131. uint64_t **addr, struct iovec **iov);
  132. void virtio_gpu_cleanup_mapping_iov(struct iovec *iov, uint32_t count);
  133. void virtio_gpu_process_cmdq(VirtIOGPU *g);
  134. /* virtio-gpu-3d.c */
  135. void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
  136. struct virtio_gpu_ctrl_command *cmd);
  137. void virtio_gpu_virgl_fence_poll(VirtIOGPU *g);
  138. void virtio_gpu_virgl_reset(VirtIOGPU *g);
  139. int virtio_gpu_virgl_init(VirtIOGPU *g);
  140. #endif