4 * Copyright Red Hat, Inc. 2013-2014
7 * Dave Airlie <airlied@redhat.com>
8 * Gerd Hoffmann <kraxel@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2.
11 * See the COPYING file in the top-level directory.
14 #ifndef HW_VIRTIO_GPU_H
15 #define HW_VIRTIO_GPU_H
17 #include "qemu/queue.h"
18 #include "ui/qemu-pixman.h"
19 #include "ui/console.h"
20 #include "hw/virtio/virtio.h"
22 #include "sysemu/vhost-user-backend.h"
24 #include "standard-headers/linux/virtio_gpu.h"
25 #include "standard-headers/linux/virtio_ids.h"
26 #include "qom/object.h"
28 #define TYPE_VIRTIO_GPU_BASE "virtio-gpu-base"
29 OBJECT_DECLARE_TYPE(VirtIOGPUBase
, VirtIOGPUBaseClass
,
32 #define TYPE_VIRTIO_GPU "virtio-gpu-device"
33 OBJECT_DECLARE_TYPE(VirtIOGPU
, VirtIOGPUClass
, VIRTIO_GPU
)
35 #define TYPE_VIRTIO_GPU_GL "virtio-gpu-gl-device"
36 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOGPUGL
, VIRTIO_GPU_GL
)
38 #define TYPE_VHOST_USER_GPU "vhost-user-gpu"
39 OBJECT_DECLARE_SIMPLE_TYPE(VhostUserGPU
, VHOST_USER_GPU
)
41 #define TYPE_VIRTIO_GPU_RUTABAGA "virtio-gpu-rutabaga-device"
42 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOGPURutabaga
, VIRTIO_GPU_RUTABAGA
)
44 struct virtio_gpu_simple_resource
{
52 uint32_t scanout_bitmask
;
53 pixman_image_t
*image
;
64 QTAILQ_ENTRY(virtio_gpu_simple_resource
) next
;
67 struct virtio_gpu_framebuffer
{
68 pixman_format_code_t format
;
70 uint32_t width
, height
;
75 struct virtio_gpu_scanout
{
78 uint32_t width
, height
;
82 struct virtio_gpu_update_cursor cursor
;
83 QEMUCursor
*current_cursor
;
84 struct virtio_gpu_framebuffer fb
;
87 struct virtio_gpu_requested_state
{
88 uint16_t width_mm
, height_mm
;
89 uint32_t width
, height
;
90 uint32_t refresh_rate
;
94 enum virtio_gpu_base_conf_flags
{
95 VIRTIO_GPU_FLAG_VIRGL_ENABLED
= 1,
96 VIRTIO_GPU_FLAG_STATS_ENABLED
,
97 VIRTIO_GPU_FLAG_EDID_ENABLED
,
98 VIRTIO_GPU_FLAG_DMABUF_ENABLED
,
99 VIRTIO_GPU_FLAG_BLOB_ENABLED
,
100 VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED
,
101 VIRTIO_GPU_FLAG_RUTABAGA_ENABLED
,
104 #define virtio_gpu_virgl_enabled(_cfg) \
105 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED))
106 #define virtio_gpu_stats_enabled(_cfg) \
107 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_STATS_ENABLED))
108 #define virtio_gpu_edid_enabled(_cfg) \
109 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_EDID_ENABLED))
110 #define virtio_gpu_dmabuf_enabled(_cfg) \
111 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED))
112 #define virtio_gpu_blob_enabled(_cfg) \
113 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED))
114 #define virtio_gpu_context_init_enabled(_cfg) \
115 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED))
116 #define virtio_gpu_rutabaga_enabled(_cfg) \
117 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_RUTABAGA_ENABLED))
118 #define virtio_gpu_hostmem_enabled(_cfg) \
121 struct virtio_gpu_base_conf
{
122 uint32_t max_outputs
;
129 struct virtio_gpu_ctrl_command
{
130 VirtQueueElement elem
;
132 struct virtio_gpu_ctrl_hdr cmd_hdr
;
135 QTAILQ_ENTRY(virtio_gpu_ctrl_command
) next
;
138 struct VirtIOGPUBase
{
139 VirtIODevice parent_obj
;
141 Error
*migration_blocker
;
143 struct virtio_gpu_base_conf conf
;
144 struct virtio_gpu_config virtio_config
;
145 const GraphicHwOps
*hw_ops
;
147 int renderer_blocked
;
150 MemoryRegion hostmem
;
152 struct virtio_gpu_scanout scanout
[VIRTIO_GPU_MAX_SCANOUTS
];
154 int enabled_output_bitmask
;
155 struct virtio_gpu_requested_state req_state
[VIRTIO_GPU_MAX_SCANOUTS
];
158 struct VirtIOGPUBaseClass
{
159 VirtioDeviceClass parent
;
161 void (*gl_flushed
)(VirtIOGPUBase
*g
);
164 #define VIRTIO_GPU_BASE_PROPERTIES(_state, _conf) \
165 DEFINE_PROP_UINT32("max_outputs", _state, _conf.max_outputs, 1), \
166 DEFINE_PROP_BIT("edid", _state, _conf.flags, \
167 VIRTIO_GPU_FLAG_EDID_ENABLED, true), \
168 DEFINE_PROP_UINT32("xres", _state, _conf.xres, 1280), \
169 DEFINE_PROP_UINT32("yres", _state, _conf.yres, 800)
171 typedef struct VGPUDMABuf
{
174 QTAILQ_ENTRY(VGPUDMABuf
) next
;
178 VirtIOGPUBase parent_obj
;
180 uint64_t conf_max_hostmem
;
183 VirtQueue
*cursor_vq
;
191 QTAILQ_HEAD(, virtio_gpu_simple_resource
) reslist
;
192 QTAILQ_HEAD(, virtio_gpu_ctrl_command
) cmdq
;
193 QTAILQ_HEAD(, virtio_gpu_ctrl_command
) fenceq
;
197 bool processing_cmdq
;
198 QEMUTimer
*fence_poll
;
199 QEMUTimer
*print_stats
;
203 uint32_t max_inflight
;
210 QTAILQ_HEAD(, VGPUDMABuf
) bufs
;
211 VGPUDMABuf
*primary
[VIRTIO_GPU_MAX_SCANOUTS
];
215 struct VirtIOGPUClass
{
216 VirtIOGPUBaseClass parent
;
218 void (*handle_ctrl
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
219 void (*process_cmd
)(VirtIOGPU
*g
, struct virtio_gpu_ctrl_command
*cmd
);
220 void (*update_cursor_data
)(VirtIOGPU
*g
,
221 struct virtio_gpu_scanout
*s
,
222 uint32_t resource_id
);
223 void (*resource_destroy
)(VirtIOGPU
*g
,
224 struct virtio_gpu_simple_resource
*res
,
229 struct VirtIOGPU parent_obj
;
231 bool renderer_inited
;
235 struct VhostUserGPU
{
236 VirtIOGPUBase parent_obj
;
238 VhostUserBackend
*vhost
;
239 int vhost_gpu_fd
; /* closed by the chardev */
240 CharBackend vhost_chr
;
241 QemuDmaBuf dmabuf
[VIRTIO_GPU_MAX_SCANOUTS
];
242 bool backend_blocked
;
245 #define MAX_SLOTS 4096
247 struct MemoryRegionInfo
{
250 uint32_t resource_id
;
255 struct VirtIOGPURutabaga
{
256 VirtIOGPU parent_obj
;
257 struct MemoryRegionInfo memory_regions
[MAX_SLOTS
];
258 uint64_t capset_mask
;
259 char *wayland_socket_path
;
262 uint32_t num_capsets
;
263 struct rutabaga
*rutabaga
;
266 #define VIRTIO_GPU_FILL_CMD(out) do { \
267 size_t virtiogpufillcmd_s_ = \
268 iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
269 &out, sizeof(out)); \
270 if (virtiogpufillcmd_s_ != sizeof(out)) { \
271 qemu_log_mask(LOG_GUEST_ERROR, \
272 "%s: command size incorrect %zu vs %zu\n", \
273 __func__, virtiogpufillcmd_s_, sizeof(out)); \
278 /* virtio-gpu-base.c */
279 bool virtio_gpu_base_device_realize(DeviceState
*qdev
,
280 VirtIOHandleOutput ctrl_cb
,
281 VirtIOHandleOutput cursor_cb
,
283 void virtio_gpu_base_device_unrealize(DeviceState
*qdev
);
284 void virtio_gpu_base_reset(VirtIOGPUBase
*g
);
285 void virtio_gpu_base_fill_display_info(VirtIOGPUBase
*g
,
286 struct virtio_gpu_resp_display_info
*dpy_info
);
288 void virtio_gpu_base_generate_edid(VirtIOGPUBase
*g
, int scanout
,
289 struct virtio_gpu_resp_edid
*edid
);
291 struct virtio_gpu_simple_resource
*
292 virtio_gpu_find_resource(VirtIOGPU
*g
, uint32_t resource_id
);
294 void virtio_gpu_ctrl_response(VirtIOGPU
*g
,
295 struct virtio_gpu_ctrl_command
*cmd
,
296 struct virtio_gpu_ctrl_hdr
*resp
,
298 void virtio_gpu_ctrl_response_nodata(VirtIOGPU
*g
,
299 struct virtio_gpu_ctrl_command
*cmd
,
300 enum virtio_gpu_ctrl_type type
);
301 void virtio_gpu_get_display_info(VirtIOGPU
*g
,
302 struct virtio_gpu_ctrl_command
*cmd
);
303 void virtio_gpu_get_edid(VirtIOGPU
*g
,
304 struct virtio_gpu_ctrl_command
*cmd
);
305 int virtio_gpu_create_mapping_iov(VirtIOGPU
*g
,
306 uint32_t nr_entries
, uint32_t offset
,
307 struct virtio_gpu_ctrl_command
*cmd
,
308 uint64_t **addr
, struct iovec
**iov
,
310 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU
*g
,
311 struct iovec
*iov
, uint32_t count
);
312 void virtio_gpu_cleanup_mapping(VirtIOGPU
*g
,
313 struct virtio_gpu_simple_resource
*res
);
314 void virtio_gpu_process_cmdq(VirtIOGPU
*g
);
315 void virtio_gpu_device_realize(DeviceState
*qdev
, Error
**errp
);
316 void virtio_gpu_reset(VirtIODevice
*vdev
);
317 void virtio_gpu_simple_process_cmd(VirtIOGPU
*g
, struct virtio_gpu_ctrl_command
*cmd
);
318 void virtio_gpu_update_cursor_data(VirtIOGPU
*g
,
319 struct virtio_gpu_scanout
*s
,
320 uint32_t resource_id
);
322 /* virtio-gpu-udmabuf.c */
323 bool virtio_gpu_have_udmabuf(void);
324 void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource
*res
);
325 void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource
*res
);
326 int virtio_gpu_update_dmabuf(VirtIOGPU
*g
,
328 struct virtio_gpu_simple_resource
*res
,
329 struct virtio_gpu_framebuffer
*fb
,
330 struct virtio_gpu_rect
*r
);
332 /* virtio-gpu-3d.c */
333 void virtio_gpu_virgl_process_cmd(VirtIOGPU
*g
,
334 struct virtio_gpu_ctrl_command
*cmd
);
335 void virtio_gpu_virgl_fence_poll(VirtIOGPU
*g
);
336 void virtio_gpu_virgl_reset_scanout(VirtIOGPU
*g
);
337 void virtio_gpu_virgl_reset(VirtIOGPU
*g
);
338 int virtio_gpu_virgl_init(VirtIOGPU
*g
);
339 int virtio_gpu_virgl_get_num_capsets(VirtIOGPU
*g
);