]> git.proxmox.com Git - mirror_qemu.git/blame - hw/display/virtio-gpu-3d.c
hw/misc: sifive_u_otp: Use error_report() when block operation fails
[mirror_qemu.git] / hw / display / virtio-gpu-3d.c
CommitLineData
9d9e1521
GH
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 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
9b8bfe21 14#include "qemu/osdep.h"
9d9e1521
GH
15#include "qemu/iov.h"
16#include "trace.h"
17#include "hw/virtio/virtio.h"
18#include "hw/virtio/virtio-gpu.h"
19
a9c94277 20#include <virglrenderer.h>
9d9e1521
GH
21
22static struct virgl_renderer_callbacks virtio_gpu_3d_cbs;
23
24static void virgl_cmd_create_resource_2d(VirtIOGPU *g,
25 struct virtio_gpu_ctrl_command *cmd)
26{
27 struct virtio_gpu_resource_create_2d c2d;
28 struct virgl_renderer_resource_create_args args;
29
30 VIRTIO_GPU_FILL_CMD(c2d);
31 trace_virtio_gpu_cmd_res_create_2d(c2d.resource_id, c2d.format,
32 c2d.width, c2d.height);
33
34 args.handle = c2d.resource_id;
35 args.target = 2;
36 args.format = c2d.format;
37 args.bind = (1 << 1);
38 args.width = c2d.width;
39 args.height = c2d.height;
40 args.depth = 1;
41 args.array_size = 1;
42 args.last_level = 0;
43 args.nr_samples = 0;
44 args.flags = VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP;
45 virgl_renderer_resource_create(&args, NULL, 0);
46}
47
48static void virgl_cmd_create_resource_3d(VirtIOGPU *g,
49 struct virtio_gpu_ctrl_command *cmd)
50{
51 struct virtio_gpu_resource_create_3d c3d;
52 struct virgl_renderer_resource_create_args args;
53
54 VIRTIO_GPU_FILL_CMD(c3d);
55 trace_virtio_gpu_cmd_res_create_3d(c3d.resource_id, c3d.format,
56 c3d.width, c3d.height, c3d.depth);
57
58 args.handle = c3d.resource_id;
59 args.target = c3d.target;
60 args.format = c3d.format;
61 args.bind = c3d.bind;
62 args.width = c3d.width;
63 args.height = c3d.height;
64 args.depth = c3d.depth;
65 args.array_size = c3d.array_size;
66 args.last_level = c3d.last_level;
67 args.nr_samples = c3d.nr_samples;
68 args.flags = c3d.flags;
69 virgl_renderer_resource_create(&args, NULL, 0);
70}
71
72static void virgl_cmd_resource_unref(VirtIOGPU *g,
73 struct virtio_gpu_ctrl_command *cmd)
74{
75 struct virtio_gpu_resource_unref unref;
5e8e3c4c
GH
76 struct iovec *res_iovs = NULL;
77 int num_iovs = 0;
9d9e1521
GH
78
79 VIRTIO_GPU_FILL_CMD(unref);
80 trace_virtio_gpu_cmd_res_unref(unref.resource_id);
81
5e8e3c4c
GH
82 virgl_renderer_resource_detach_iov(unref.resource_id,
83 &res_iovs,
84 &num_iovs);
85 if (res_iovs != NULL && num_iovs != 0) {
3bb68f79 86 virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
5e8e3c4c 87 }
9d9e1521
GH
88 virgl_renderer_resource_unref(unref.resource_id);
89}
90
91static void virgl_cmd_context_create(VirtIOGPU *g,
92 struct virtio_gpu_ctrl_command *cmd)
93{
94 struct virtio_gpu_ctx_create cc;
95
96 VIRTIO_GPU_FILL_CMD(cc);
97 trace_virtio_gpu_cmd_ctx_create(cc.hdr.ctx_id,
98 cc.debug_name);
99
100 virgl_renderer_context_create(cc.hdr.ctx_id, cc.nlen,
101 cc.debug_name);
102}
103
104static void virgl_cmd_context_destroy(VirtIOGPU *g,
105 struct virtio_gpu_ctrl_command *cmd)
106{
107 struct virtio_gpu_ctx_destroy cd;
108
109 VIRTIO_GPU_FILL_CMD(cd);
110 trace_virtio_gpu_cmd_ctx_destroy(cd.hdr.ctx_id);
111
112 virgl_renderer_context_destroy(cd.hdr.ctx_id);
113}
114
115static void virtio_gpu_rect_update(VirtIOGPU *g, int idx, int x, int y,
116 int width, int height)
117{
50d8e25e 118 if (!g->parent_obj.scanout[idx].con) {
9d9e1521
GH
119 return;
120 }
121
50d8e25e 122 dpy_gl_update(g->parent_obj.scanout[idx].con, x, y, width, height);
9d9e1521
GH
123}
124
125static void virgl_cmd_resource_flush(VirtIOGPU *g,
126 struct virtio_gpu_ctrl_command *cmd)
127{
128 struct virtio_gpu_resource_flush rf;
129 int i;
130
131 VIRTIO_GPU_FILL_CMD(rf);
132 trace_virtio_gpu_cmd_res_flush(rf.resource_id,
133 rf.r.width, rf.r.height, rf.r.x, rf.r.y);
134
50d8e25e
MAL
135 for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
136 if (g->parent_obj.scanout[i].resource_id != rf.resource_id) {
9d9e1521
GH
137 continue;
138 }
139 virtio_gpu_rect_update(g, i, rf.r.x, rf.r.y, rf.r.width, rf.r.height);
140 }
141}
142
143static void virgl_cmd_set_scanout(VirtIOGPU *g,
144 struct virtio_gpu_ctrl_command *cmd)
145{
146 struct virtio_gpu_set_scanout ss;
147 struct virgl_renderer_resource_info info;
148 int ret;
149
150 VIRTIO_GPU_FILL_CMD(ss);
151 trace_virtio_gpu_cmd_set_scanout(ss.scanout_id, ss.resource_id,
152 ss.r.width, ss.r.height, ss.r.x, ss.r.y);
153
50d8e25e 154 if (ss.scanout_id >= g->parent_obj.conf.max_outputs) {
9d9e1521
GH
155 qemu_log_mask(LOG_GUEST_ERROR, "%s: illegal scanout id specified %d",
156 __func__, ss.scanout_id);
157 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
158 return;
159 }
50d8e25e 160 g->parent_obj.enable = 1;
9d9e1521
GH
161
162 memset(&info, 0, sizeof(info));
163
164 if (ss.resource_id && ss.r.width && ss.r.height) {
165 ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
166 if (ret == -1) {
167 qemu_log_mask(LOG_GUEST_ERROR,
168 "%s: illegal resource specified %d\n",
169 __func__, ss.resource_id);
170 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
171 return;
172 }
50d8e25e 173 qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
9d9e1521
GH
174 ss.r.width, ss.r.height);
175 virgl_renderer_force_ctx_0();
50d8e25e
MAL
176 dpy_gl_scanout_texture(
177 g->parent_obj.scanout[ss.scanout_id].con, info.tex_id,
178 info.flags & 1 /* FIXME: Y_0_TOP */,
179 info.width, info.height,
180 ss.r.x, ss.r.y, ss.r.width, ss.r.height);
9d9e1521
GH
181 } else {
182 if (ss.scanout_id != 0) {
50d8e25e
MAL
183 dpy_gfx_replace_surface(
184 g->parent_obj.scanout[ss.scanout_id].con, NULL);
9d9e1521 185 }
50d8e25e 186 dpy_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
9d9e1521 187 }
50d8e25e 188 g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
9d9e1521
GH
189}
190
191static void virgl_cmd_submit_3d(VirtIOGPU *g,
192 struct virtio_gpu_ctrl_command *cmd)
193{
194 struct virtio_gpu_cmd_submit cs;
195 void *buf;
196 size_t s;
197
198 VIRTIO_GPU_FILL_CMD(cs);
199 trace_virtio_gpu_cmd_ctx_submit(cs.hdr.ctx_id, cs.size);
200
201 buf = g_malloc(cs.size);
202 s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
203 sizeof(cs), buf, cs.size);
204 if (s != cs.size) {
205 qemu_log_mask(LOG_GUEST_ERROR, "%s: size mismatch (%zd/%d)",
206 __func__, s, cs.size);
207 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
8d94c1ca 208 goto out;
9d9e1521
GH
209 }
210
50d8e25e 211 if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
9d9e1521
GH
212 g->stats.req_3d++;
213 g->stats.bytes_3d += cs.size;
214 }
215
216 virgl_renderer_submit_cmd(buf, cs.hdr.ctx_id, cs.size / 4);
217
8d94c1ca 218out:
9d9e1521
GH
219 g_free(buf);
220}
221
222static void virgl_cmd_transfer_to_host_2d(VirtIOGPU *g,
223 struct virtio_gpu_ctrl_command *cmd)
224{
225 struct virtio_gpu_transfer_to_host_2d t2d;
226 struct virtio_gpu_box box;
227
228 VIRTIO_GPU_FILL_CMD(t2d);
229 trace_virtio_gpu_cmd_res_xfer_toh_2d(t2d.resource_id);
230
231 box.x = t2d.r.x;
232 box.y = t2d.r.y;
233 box.z = 0;
234 box.w = t2d.r.width;
235 box.h = t2d.r.height;
236 box.d = 1;
237
238 virgl_renderer_transfer_write_iov(t2d.resource_id,
239 0,
240 0,
241 0,
242 0,
243 (struct virgl_box *)&box,
244 t2d.offset, NULL, 0);
245}
246
247static void virgl_cmd_transfer_to_host_3d(VirtIOGPU *g,
248 struct virtio_gpu_ctrl_command *cmd)
249{
250 struct virtio_gpu_transfer_host_3d t3d;
251
252 VIRTIO_GPU_FILL_CMD(t3d);
253 trace_virtio_gpu_cmd_res_xfer_toh_3d(t3d.resource_id);
254
255 virgl_renderer_transfer_write_iov(t3d.resource_id,
256 t3d.hdr.ctx_id,
257 t3d.level,
258 t3d.stride,
259 t3d.layer_stride,
260 (struct virgl_box *)&t3d.box,
261 t3d.offset, NULL, 0);
262}
263
264static void
265virgl_cmd_transfer_from_host_3d(VirtIOGPU *g,
266 struct virtio_gpu_ctrl_command *cmd)
267{
268 struct virtio_gpu_transfer_host_3d tf3d;
269
270 VIRTIO_GPU_FILL_CMD(tf3d);
271 trace_virtio_gpu_cmd_res_xfer_fromh_3d(tf3d.resource_id);
272
273 virgl_renderer_transfer_read_iov(tf3d.resource_id,
274 tf3d.hdr.ctx_id,
275 tf3d.level,
276 tf3d.stride,
277 tf3d.layer_stride,
278 (struct virgl_box *)&tf3d.box,
279 tf3d.offset, NULL, 0);
280}
281
282
283static void virgl_resource_attach_backing(VirtIOGPU *g,
284 struct virtio_gpu_ctrl_command *cmd)
285{
286 struct virtio_gpu_resource_attach_backing att_rb;
287 struct iovec *res_iovs;
288 int ret;
289
290 VIRTIO_GPU_FILL_CMD(att_rb);
291 trace_virtio_gpu_cmd_res_back_attach(att_rb.resource_id);
292
3bb68f79 293 ret = virtio_gpu_create_mapping_iov(g, &att_rb, cmd, NULL, &res_iovs);
9d9e1521
GH
294 if (ret != 0) {
295 cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
296 return;
297 }
298
33243031
LQ
299 ret = virgl_renderer_resource_attach_iov(att_rb.resource_id,
300 res_iovs, att_rb.nr_entries);
301
302 if (ret != 0)
3bb68f79 303 virtio_gpu_cleanup_mapping_iov(g, res_iovs, att_rb.nr_entries);
9d9e1521
GH
304}
305
306static void virgl_resource_detach_backing(VirtIOGPU *g,
307 struct virtio_gpu_ctrl_command *cmd)
308{
309 struct virtio_gpu_resource_detach_backing detach_rb;
310 struct iovec *res_iovs = NULL;
311 int num_iovs = 0;
312
313 VIRTIO_GPU_FILL_CMD(detach_rb);
314 trace_virtio_gpu_cmd_res_back_detach(detach_rb.resource_id);
315
316 virgl_renderer_resource_detach_iov(detach_rb.resource_id,
317 &res_iovs,
318 &num_iovs);
319 if (res_iovs == NULL || num_iovs == 0) {
320 return;
321 }
3bb68f79 322 virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
9d9e1521
GH
323}
324
325
326static void virgl_cmd_ctx_attach_resource(VirtIOGPU *g,
327 struct virtio_gpu_ctrl_command *cmd)
328{
329 struct virtio_gpu_ctx_resource att_res;
330
331 VIRTIO_GPU_FILL_CMD(att_res);
332 trace_virtio_gpu_cmd_ctx_res_attach(att_res.hdr.ctx_id,
333 att_res.resource_id);
334
335 virgl_renderer_ctx_attach_resource(att_res.hdr.ctx_id, att_res.resource_id);
336}
337
338static void virgl_cmd_ctx_detach_resource(VirtIOGPU *g,
339 struct virtio_gpu_ctrl_command *cmd)
340{
341 struct virtio_gpu_ctx_resource det_res;
342
343 VIRTIO_GPU_FILL_CMD(det_res);
344 trace_virtio_gpu_cmd_ctx_res_detach(det_res.hdr.ctx_id,
345 det_res.resource_id);
346
347 virgl_renderer_ctx_detach_resource(det_res.hdr.ctx_id, det_res.resource_id);
348}
349
350static void virgl_cmd_get_capset_info(VirtIOGPU *g,
351 struct virtio_gpu_ctrl_command *cmd)
352{
353 struct virtio_gpu_get_capset_info info;
354 struct virtio_gpu_resp_capset_info resp;
355
356 VIRTIO_GPU_FILL_CMD(info);
357
42a8dadc 358 memset(&resp, 0, sizeof(resp));
9d9e1521
GH
359 if (info.capset_index == 0) {
360 resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL;
361 virgl_renderer_get_cap_set(resp.capset_id,
362 &resp.capset_max_version,
363 &resp.capset_max_size);
5643cc94
DA
364 } else if (info.capset_index == 1) {
365 resp.capset_id = VIRTIO_GPU_CAPSET_VIRGL2;
366 virgl_renderer_get_cap_set(resp.capset_id,
367 &resp.capset_max_version,
368 &resp.capset_max_size);
9d9e1521
GH
369 } else {
370 resp.capset_max_version = 0;
371 resp.capset_max_size = 0;
372 }
373 resp.hdr.type = VIRTIO_GPU_RESP_OK_CAPSET_INFO;
374 virtio_gpu_ctrl_response(g, cmd, &resp.hdr, sizeof(resp));
375}
376
377static void virgl_cmd_get_capset(VirtIOGPU *g,
378 struct virtio_gpu_ctrl_command *cmd)
379{
380 struct virtio_gpu_get_capset gc;
381 struct virtio_gpu_resp_capset *resp;
382 uint32_t max_ver, max_size;
383 VIRTIO_GPU_FILL_CMD(gc);
384
385 virgl_renderer_get_cap_set(gc.capset_id, &max_ver,
386 &max_size);
abd7f08b
PP
387 if (!max_size) {
388 cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
389 return;
390 }
9d9e1521 391
85d9d044 392 resp = g_malloc0(sizeof(*resp) + max_size);
9d9e1521
GH
393 resp->hdr.type = VIRTIO_GPU_RESP_OK_CAPSET;
394 virgl_renderer_fill_caps(gc.capset_id,
395 gc.capset_version,
396 (void *)resp->capset_data);
397 virtio_gpu_ctrl_response(g, cmd, &resp->hdr, sizeof(*resp) + max_size);
398 g_free(resp);
399}
400
401void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
402 struct virtio_gpu_ctrl_command *cmd)
403{
404 VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
405
406 virgl_renderer_force_ctx_0();
407 switch (cmd->cmd_hdr.type) {
408 case VIRTIO_GPU_CMD_CTX_CREATE:
409 virgl_cmd_context_create(g, cmd);
410 break;
411 case VIRTIO_GPU_CMD_CTX_DESTROY:
412 virgl_cmd_context_destroy(g, cmd);
413 break;
414 case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D:
415 virgl_cmd_create_resource_2d(g, cmd);
416 break;
417 case VIRTIO_GPU_CMD_RESOURCE_CREATE_3D:
418 virgl_cmd_create_resource_3d(g, cmd);
419 break;
420 case VIRTIO_GPU_CMD_SUBMIT_3D:
421 virgl_cmd_submit_3d(g, cmd);
422 break;
423 case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D:
424 virgl_cmd_transfer_to_host_2d(g, cmd);
425 break;
426 case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D:
427 virgl_cmd_transfer_to_host_3d(g, cmd);
428 break;
429 case VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D:
430 virgl_cmd_transfer_from_host_3d(g, cmd);
431 break;
432 case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING:
433 virgl_resource_attach_backing(g, cmd);
434 break;
435 case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
436 virgl_resource_detach_backing(g, cmd);
437 break;
438 case VIRTIO_GPU_CMD_SET_SCANOUT:
439 virgl_cmd_set_scanout(g, cmd);
440 break;
441 case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
442 virgl_cmd_resource_flush(g, cmd);
443 break;
444 case VIRTIO_GPU_CMD_RESOURCE_UNREF:
445 virgl_cmd_resource_unref(g, cmd);
446 break;
447 case VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE:
448 /* TODO add security */
449 virgl_cmd_ctx_attach_resource(g, cmd);
450 break;
451 case VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE:
452 /* TODO add security */
453 virgl_cmd_ctx_detach_resource(g, cmd);
454 break;
455 case VIRTIO_GPU_CMD_GET_CAPSET_INFO:
456 virgl_cmd_get_capset_info(g, cmd);
457 break;
458 case VIRTIO_GPU_CMD_GET_CAPSET:
459 virgl_cmd_get_capset(g, cmd);
460 break;
461
462 case VIRTIO_GPU_CMD_GET_DISPLAY_INFO:
463 virtio_gpu_get_display_info(g, cmd);
464 break;
1ed2cb32
GH
465 case VIRTIO_GPU_CMD_GET_EDID:
466 virtio_gpu_get_edid(g, cmd);
467 break;
9d9e1521
GH
468 default:
469 cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
470 break;
471 }
472
473 if (cmd->finished) {
474 return;
475 }
476 if (cmd->error) {
477 fprintf(stderr, "%s: ctrl 0x%x, error 0x%x\n", __func__,
478 cmd->cmd_hdr.type, cmd->error);
479 virtio_gpu_ctrl_response_nodata(g, cmd, cmd->error);
480 return;
481 }
482 if (!(cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE)) {
483 virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
484 return;
485 }
486
487 trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
488 virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
489}
490
491static void virgl_write_fence(void *opaque, uint32_t fence)
492{
493 VirtIOGPU *g = opaque;
494 struct virtio_gpu_ctrl_command *cmd, *tmp;
495
496 QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
497 /*
7d37435b
PB
498 * the guest can end up emitting fences out of order
499 * so we should check all fenced cmds not just the first one.
500 */
9d9e1521
GH
501 if (cmd->cmd_hdr.fence_id > fence) {
502 continue;
503 }
504 trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id);
505 virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
506 QTAILQ_REMOVE(&g->fenceq, cmd, next);
507 g_free(cmd);
508 g->inflight--;
50d8e25e 509 if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
9d9e1521
GH
510 fprintf(stderr, "inflight: %3d (-)\r", g->inflight);
511 }
512 }
513}
514
515static virgl_renderer_gl_context
516virgl_create_context(void *opaque, int scanout_idx,
517 struct virgl_renderer_gl_ctx_param *params)
518{
519 VirtIOGPU *g = opaque;
520 QEMUGLContext ctx;
521 QEMUGLParams qparams;
522
523 qparams.major_ver = params->major_ver;
524 qparams.minor_ver = params->minor_ver;
525
50d8e25e 526 ctx = dpy_gl_ctx_create(g->parent_obj.scanout[scanout_idx].con, &qparams);
9d9e1521
GH
527 return (virgl_renderer_gl_context)ctx;
528}
529
530static void virgl_destroy_context(void *opaque, virgl_renderer_gl_context ctx)
531{
532 VirtIOGPU *g = opaque;
533 QEMUGLContext qctx = (QEMUGLContext)ctx;
534
50d8e25e 535 dpy_gl_ctx_destroy(g->parent_obj.scanout[0].con, qctx);
9d9e1521
GH
536}
537
538static int virgl_make_context_current(void *opaque, int scanout_idx,
539 virgl_renderer_gl_context ctx)
540{
541 VirtIOGPU *g = opaque;
542 QEMUGLContext qctx = (QEMUGLContext)ctx;
543
50d8e25e
MAL
544 return dpy_gl_ctx_make_current(g->parent_obj.scanout[scanout_idx].con,
545 qctx);
9d9e1521
GH
546}
547
548static struct virgl_renderer_callbacks virtio_gpu_3d_cbs = {
549 .version = 1,
550 .write_fence = virgl_write_fence,
551 .create_gl_context = virgl_create_context,
552 .destroy_gl_context = virgl_destroy_context,
553 .make_current = virgl_make_context_current,
554};
555
556static void virtio_gpu_print_stats(void *opaque)
557{
558 VirtIOGPU *g = opaque;
559
560 if (g->stats.requests) {
561 fprintf(stderr, "stats: vq req %4d, %3d -- 3D %4d (%5d)\n",
562 g->stats.requests,
563 g->stats.max_inflight,
564 g->stats.req_3d,
565 g->stats.bytes_3d);
566 g->stats.requests = 0;
567 g->stats.max_inflight = 0;
568 g->stats.req_3d = 0;
569 g->stats.bytes_3d = 0;
570 } else {
571 fprintf(stderr, "stats: idle\r");
572 }
573 timer_mod(g->print_stats, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
574}
575
576static void virtio_gpu_fence_poll(void *opaque)
577{
578 VirtIOGPU *g = opaque;
579
580 virgl_renderer_poll();
0c55a1cf
GH
581 virtio_gpu_process_cmdq(g);
582 if (!QTAILQ_EMPTY(&g->cmdq) || !QTAILQ_EMPTY(&g->fenceq)) {
9d9e1521
GH
583 timer_mod(g->fence_poll, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 10);
584 }
585}
586
587void virtio_gpu_virgl_fence_poll(VirtIOGPU *g)
588{
589 virtio_gpu_fence_poll(g);
590}
591
592void virtio_gpu_virgl_reset(VirtIOGPU *g)
593{
594 int i;
595
3745d59e 596 virgl_renderer_reset();
50d8e25e 597 for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
9d9e1521 598 if (i != 0) {
50d8e25e 599 dpy_gfx_replace_surface(g->parent_obj.scanout[i].con, NULL);
9d9e1521 600 }
50d8e25e 601 dpy_gl_scanout_disable(g->parent_obj.scanout[i].con);
9d9e1521
GH
602 }
603}
604
605int virtio_gpu_virgl_init(VirtIOGPU *g)
606{
607 int ret;
608
609 ret = virgl_renderer_init(g, 0, &virtio_gpu_3d_cbs);
610 if (ret != 0) {
611 return ret;
612 }
613
614 g->fence_poll = timer_new_ms(QEMU_CLOCK_VIRTUAL,
615 virtio_gpu_fence_poll, g);
616
50d8e25e 617 if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
9d9e1521
GH
618 g->print_stats = timer_new_ms(QEMU_CLOCK_VIRTUAL,
619 virtio_gpu_print_stats, g);
620 timer_mod(g->print_stats, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
621 }
622 return 0;
623}
624
5643cc94
DA
625int virtio_gpu_virgl_get_num_capsets(VirtIOGPU *g)
626{
627 uint32_t capset2_max_ver, capset2_max_size;
628 virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_VIRGL2,
629 &capset2_max_ver,
630 &capset2_max_size);
631
632 return capset2_max_ver ? 2 : 1;
633}