blob: d5627119ec62a03475a75f5430a5ce620bc9b6c7 [file] [log] [blame]
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
4 * written by Yaniv Kamay, Izik Eidus, Gerd Hoffmann
5 * maintained by Gerd Hoffmann <kraxel@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
Peter Maydell47df5152016-01-26 18:17:13 +000021#include "qemu/osdep.h"
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -030022#include "qemu/units.h"
Alon Levya639ab02012-09-12 16:13:28 +030023#include <zlib.h>
24
Markus Armbrustere688df62018-02-01 12:18:31 +010025#include "qapi/error.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/timer.h"
27#include "qemu/queue.h"
Paolo Bonzini5444e762013-05-13 13:29:47 +020028#include "qemu/atomic.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020029#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020030#include "qemu/module.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020031#include "hw/qdev-properties.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020032#include "sysemu/runstate.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020033#include "migration/blocker.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020034#include "migration/vmstate.h"
Alon Levyc480bb72012-03-18 13:46:14 +010035#include "trace.h"
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020036
Paolo Bonzini47b43a12013-03-18 17:36:02 +010037#include "qxl.h"
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020038
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020039#undef SPICE_RING_CONS_ITEM
Alon Levy0b81c472012-04-25 12:13:21 +030040#define SPICE_RING_CONS_ITEM(qxl, r, ret) { \
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020041 uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \
Markus Armbrusterbc5f92e2013-01-10 14:24:49 +010042 if (cons >= ARRAY_SIZE((r)->items)) { \
Alon Levy0a530542012-05-24 12:38:12 +030043 qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
Markus Armbrusterbc5f92e2013-01-10 14:24:49 +010044 "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \
Alon Levy0b81c472012-04-25 12:13:21 +030045 ret = NULL; \
46 } else { \
Markus Armbrusterbc5f92e2013-01-10 14:24:49 +010047 ret = &(r)->items[cons].el; \
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020048 } \
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020049 }
50
51#undef ALIGN
52#define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
53
54#define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9"
55
56#define QXL_MODE(_x, _y, _b, _o) \
57 { .x_res = _x, \
58 .y_res = _y, \
59 .bits = _b, \
60 .stride = (_x) * (_b) / 8, \
61 .x_mili = PIXEL_SIZE * (_x), \
62 .y_mili = PIXEL_SIZE * (_y), \
63 .orientation = _o, \
64 }
65
66#define QXL_MODE_16_32(x_res, y_res, orientation) \
67 QXL_MODE(x_res, y_res, 16, orientation), \
68 QXL_MODE(x_res, y_res, 32, orientation)
69
70#define QXL_MODE_EX(x_res, y_res) \
71 QXL_MODE_16_32(x_res, y_res, 0), \
Alon Levy038c1872013-01-21 14:48:07 +020072 QXL_MODE_16_32(x_res, y_res, 1)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020073
74static QXLMode qxl_modes[] = {
75 QXL_MODE_EX(640, 480),
76 QXL_MODE_EX(800, 480),
77 QXL_MODE_EX(800, 600),
78 QXL_MODE_EX(832, 624),
79 QXL_MODE_EX(960, 640),
80 QXL_MODE_EX(1024, 600),
81 QXL_MODE_EX(1024, 768),
82 QXL_MODE_EX(1152, 864),
83 QXL_MODE_EX(1152, 870),
84 QXL_MODE_EX(1280, 720),
85 QXL_MODE_EX(1280, 760),
86 QXL_MODE_EX(1280, 768),
87 QXL_MODE_EX(1280, 800),
88 QXL_MODE_EX(1280, 960),
89 QXL_MODE_EX(1280, 1024),
90 QXL_MODE_EX(1360, 768),
91 QXL_MODE_EX(1366, 768),
92 QXL_MODE_EX(1400, 1050),
93 QXL_MODE_EX(1440, 900),
94 QXL_MODE_EX(1600, 900),
95 QXL_MODE_EX(1600, 1200),
96 QXL_MODE_EX(1680, 1050),
97 QXL_MODE_EX(1920, 1080),
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020098 /* these modes need more than 8 MB video memory */
99 QXL_MODE_EX(1920, 1200),
100 QXL_MODE_EX(1920, 1440),
Gerd Hoffmann5c74fb22013-04-04 10:15:34 +0200101 QXL_MODE_EX(2000, 2000),
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200102 QXL_MODE_EX(2048, 1536),
Gerd Hoffmann5c74fb22013-04-04 10:15:34 +0200103 QXL_MODE_EX(2048, 2048),
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200104 QXL_MODE_EX(2560, 1440),
105 QXL_MODE_EX(2560, 1600),
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200106 /* these modes need more than 16 MB video memory */
107 QXL_MODE_EX(2560, 2048),
108 QXL_MODE_EX(2800, 2100),
109 QXL_MODE_EX(3200, 2400),
Radim Krčmář03d98252015-02-17 17:30:50 +0100110 /* these modes need more than 32 MB video memory */
Gerd Hoffmannd4bcb192013-03-15 11:53:47 +0100111 QXL_MODE_EX(3840, 2160), /* 4k mainstream */
112 QXL_MODE_EX(4096, 2160), /* 4k */
Radim Krčmář03d98252015-02-17 17:30:50 +0100113 /* these modes need more than 64 MB video memory */
Gerd Hoffmannd4bcb192013-03-15 11:53:47 +0100114 QXL_MODE_EX(7680, 4320), /* 8k mainstream */
Radim Krčmář03d98252015-02-17 17:30:50 +0100115 /* these modes need more than 128 MB video memory */
Gerd Hoffmannd4bcb192013-03-15 11:53:47 +0100116 QXL_MODE_EX(8192, 4320), /* 8k */
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200117};
118
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200119static void qxl_send_events(PCIQXLDevice *d, uint32_t events);
Alon Levy5ff4e362011-07-20 12:20:58 +0300120static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200121static void qxl_reset_memslots(PCIQXLDevice *d);
122static void qxl_reset_surfaces(PCIQXLDevice *d);
123static void qxl_ring_set_dirty(PCIQXLDevice *qxl);
124
Gerd Hoffmann15162332014-09-24 17:05:45 +0200125static void qxl_hw_update(void *opaque);
126
Alon Levy0a530542012-05-24 12:38:12 +0300127void qxl_set_guest_bug(PCIQXLDevice *qxl, const char *msg, ...)
Gerd Hoffmann2bce0402011-07-20 12:20:55 +0300128{
Alon Levy917ae082012-09-12 16:13:26 +0300129 trace_qxl_set_guest_bug(qxl->id);
Gerd Hoffmann2bce0402011-07-20 12:20:55 +0300130 qxl_send_events(qxl, QXL_INTERRUPT_ERROR);
Alon Levy087e6a42012-05-24 19:18:54 +0300131 qxl->guest_bug = 1;
Gerd Hoffmann2bce0402011-07-20 12:20:55 +0300132 if (qxl->guestdebug) {
Alon Levy76353922011-07-20 12:20:56 +0300133 va_list ap;
134 va_start(ap, msg);
135 fprintf(stderr, "qxl-%d: guest bug: ", qxl->id);
136 vfprintf(stderr, msg, ap);
137 fprintf(stderr, "\n");
138 va_end(ap);
Gerd Hoffmann2bce0402011-07-20 12:20:55 +0300139 }
140}
141
Alon Levy087e6a42012-05-24 19:18:54 +0300142static void qxl_clear_guest_bug(PCIQXLDevice *qxl)
143{
144 qxl->guest_bug = 0;
145}
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300146
147void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
148 struct QXLRect *area, struct QXLRect *dirty_rects,
149 uint32_t num_dirty_rects,
Alon Levy5ff4e362011-07-20 12:20:58 +0300150 uint32_t clear_dirty_region,
Alon Levy2e1a98c2012-02-24 23:19:30 +0200151 qxl_async_io async, struct QXLCookie *cookie)
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300152{
Alon Levyc480bb72012-03-18 13:46:14 +0100153 trace_qxl_spice_update_area(qxl->id, surface_id, area->left, area->right,
154 area->top, area->bottom);
155 trace_qxl_spice_update_area_rest(qxl->id, num_dirty_rects,
156 clear_dirty_region);
Alon Levy5ff4e362011-07-20 12:20:58 +0300157 if (async == QXL_SYNC) {
Marc-André Lureau26defe82013-10-04 13:10:46 +0200158 spice_qxl_update_area(&qxl->ssd.qxl, surface_id, area,
Alon Levy5ff4e362011-07-20 12:20:58 +0300159 dirty_rects, num_dirty_rects, clear_dirty_region);
160 } else {
Alon Levy2e1a98c2012-02-24 23:19:30 +0200161 assert(cookie != NULL);
Alon Levy5ff4e362011-07-20 12:20:58 +0300162 spice_qxl_update_area_async(&qxl->ssd.qxl, surface_id, area,
Peter Maydell5dba0d42012-03-16 13:50:04 +0000163 clear_dirty_region, (uintptr_t)cookie);
Alon Levy5ff4e362011-07-20 12:20:58 +0300164 }
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300165}
166
Alon Levy5ff4e362011-07-20 12:20:58 +0300167static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice *qxl,
168 uint32_t id)
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300169{
Alon Levyc480bb72012-03-18 13:46:14 +0100170 trace_qxl_spice_destroy_surface_wait_complete(qxl->id, id);
Gerd Hoffmann14898cf2011-07-20 12:20:53 +0300171 qemu_mutex_lock(&qxl->track_lock);
Gerd Hoffmann14898cf2011-07-20 12:20:53 +0300172 qxl->guest_surfaces.cmds[id] = 0;
173 qxl->guest_surfaces.count--;
174 qemu_mutex_unlock(&qxl->track_lock);
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300175}
176
Alon Levy5ff4e362011-07-20 12:20:58 +0300177static void qxl_spice_destroy_surface_wait(PCIQXLDevice *qxl, uint32_t id,
178 qxl_async_io async)
179{
Alon Levy2e1a98c2012-02-24 23:19:30 +0200180 QXLCookie *cookie;
181
Alon Levyc480bb72012-03-18 13:46:14 +0100182 trace_qxl_spice_destroy_surface_wait(qxl->id, id, async);
Alon Levy5ff4e362011-07-20 12:20:58 +0300183 if (async) {
Alon Levy2e1a98c2012-02-24 23:19:30 +0200184 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
185 QXL_IO_DESTROY_SURFACE_ASYNC);
186 cookie->u.surface_id = id;
Peter Maydell5dba0d42012-03-16 13:50:04 +0000187 spice_qxl_destroy_surface_async(&qxl->ssd.qxl, id, (uintptr_t)cookie);
Alon Levy5ff4e362011-07-20 12:20:58 +0300188 } else {
Marc-André Lureau26defe82013-10-04 13:10:46 +0200189 spice_qxl_destroy_surface_wait(&qxl->ssd.qxl, id);
Uri Lublin753b8b02012-09-11 10:09:58 +0300190 qxl_spice_destroy_surface_wait_complete(qxl, id);
Alon Levy5ff4e362011-07-20 12:20:58 +0300191 }
192}
193
Alon Levy3e16b9c2011-07-20 12:20:59 +0300194static void qxl_spice_flush_surfaces_async(PCIQXLDevice *qxl)
195{
Alon Levyc480bb72012-03-18 13:46:14 +0100196 trace_qxl_spice_flush_surfaces_async(qxl->id, qxl->guest_surfaces.count,
197 qxl->num_free_res);
Alon Levy2e1a98c2012-02-24 23:19:30 +0200198 spice_qxl_flush_surfaces_async(&qxl->ssd.qxl,
Peter Maydell5dba0d42012-03-16 13:50:04 +0000199 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
200 QXL_IO_FLUSH_SURFACES_ASYNC));
Alon Levy3e16b9c2011-07-20 12:20:59 +0300201}
Alon Levy3e16b9c2011-07-20 12:20:59 +0300202
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300203void qxl_spice_loadvm_commands(PCIQXLDevice *qxl, struct QXLCommandExt *ext,
204 uint32_t count)
205{
Alon Levyc480bb72012-03-18 13:46:14 +0100206 trace_qxl_spice_loadvm_commands(qxl->id, ext, count);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200207 spice_qxl_loadvm_commands(&qxl->ssd.qxl, ext, count);
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300208}
209
210void qxl_spice_oom(PCIQXLDevice *qxl)
211{
Alon Levyc480bb72012-03-18 13:46:14 +0100212 trace_qxl_spice_oom(qxl->id);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200213 spice_qxl_oom(&qxl->ssd.qxl);
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300214}
215
216void qxl_spice_reset_memslots(PCIQXLDevice *qxl)
217{
Alon Levyc480bb72012-03-18 13:46:14 +0100218 trace_qxl_spice_reset_memslots(qxl->id);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200219 spice_qxl_reset_memslots(&qxl->ssd.qxl);
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300220}
221
Alon Levy5ff4e362011-07-20 12:20:58 +0300222static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice *qxl)
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300223{
Alon Levyc480bb72012-03-18 13:46:14 +0100224 trace_qxl_spice_destroy_surfaces_complete(qxl->id);
Gerd Hoffmann14898cf2011-07-20 12:20:53 +0300225 qemu_mutex_lock(&qxl->track_lock);
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200226 memset(qxl->guest_surfaces.cmds, 0,
Alon Levy8bb9f512013-03-13 17:58:35 +0200227 sizeof(qxl->guest_surfaces.cmds[0]) * qxl->ssd.num_surfaces);
Gerd Hoffmann14898cf2011-07-20 12:20:53 +0300228 qxl->guest_surfaces.count = 0;
229 qemu_mutex_unlock(&qxl->track_lock);
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300230}
231
Alon Levy5ff4e362011-07-20 12:20:58 +0300232static void qxl_spice_destroy_surfaces(PCIQXLDevice *qxl, qxl_async_io async)
233{
Alon Levyc480bb72012-03-18 13:46:14 +0100234 trace_qxl_spice_destroy_surfaces(qxl->id, async);
Alon Levy5ff4e362011-07-20 12:20:58 +0300235 if (async) {
Alon Levy2e1a98c2012-02-24 23:19:30 +0200236 spice_qxl_destroy_surfaces_async(&qxl->ssd.qxl,
Peter Maydell5dba0d42012-03-16 13:50:04 +0000237 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
238 QXL_IO_DESTROY_ALL_SURFACES_ASYNC));
Alon Levy5ff4e362011-07-20 12:20:58 +0300239 } else {
Marc-André Lureau26defe82013-10-04 13:10:46 +0200240 spice_qxl_destroy_surfaces(&qxl->ssd.qxl);
Alon Levy5ff4e362011-07-20 12:20:58 +0300241 qxl_spice_destroy_surfaces_complete(qxl);
242 }
243}
244
Alon Levy020af1c2012-08-22 11:16:25 +0300245static void qxl_spice_monitors_config_async(PCIQXLDevice *qxl, int replay)
246{
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200247 QXLMonitorsConfig *cfg;
248
Alon Levy020af1c2012-08-22 11:16:25 +0300249 trace_qxl_spice_monitors_config(qxl->id);
Alon Levy020af1c2012-08-22 11:16:25 +0300250 if (replay) {
251 /*
252 * don't use QXL_COOKIE_TYPE_IO:
253 * - we are not running yet (post_load), we will assert
254 * in send_events
255 * - this is not a guest io, but a reply, so async_io isn't set.
256 */
257 spice_qxl_monitors_config_async(&qxl->ssd.qxl,
258 qxl->guest_monitors_config,
259 MEMSLOT_GROUP_GUEST,
260 (uintptr_t)qxl_cookie_new(
261 QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
262 0));
263 } else {
Lukáš Hrázkýbe812c02019-02-15 16:09:19 +0100264/* >= release 0.12.6, < release 0.14.2 */
265#if SPICE_SERVER_VERSION >= 0x000c06 && SPICE_SERVER_VERSION < 0x000e02
Frediano Ziglio567161f2015-07-06 07:56:38 +0100266 if (qxl->max_outputs) {
Frediano Ziglioa52b2cb2015-07-20 09:43:23 +0100267 spice_qxl_set_max_monitors(&qxl->ssd.qxl, qxl->max_outputs);
Frediano Ziglio567161f2015-07-06 07:56:38 +0100268 }
269#endif
Alon Levy020af1c2012-08-22 11:16:25 +0300270 qxl->guest_monitors_config = qxl->ram->monitors_config;
271 spice_qxl_monitors_config_async(&qxl->ssd.qxl,
272 qxl->ram->monitors_config,
273 MEMSLOT_GROUP_GUEST,
274 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
275 QXL_IO_MONITORS_CONFIG_ASYNC));
276 }
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200277
278 cfg = qxl_phys2virt(qxl, qxl->guest_monitors_config, MEMSLOT_GROUP_GUEST);
Gerd Hoffmann2f99f802018-10-05 15:46:08 +0200279 if (cfg != NULL && cfg->count == 1) {
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200280 qxl->guest_primary.resized = 1;
281 qxl->guest_head0_width = cfg->heads[0].width;
282 qxl->guest_head0_height = cfg->heads[0].height;
283 } else {
284 qxl->guest_head0_width = 0;
285 qxl->guest_head0_height = 0;
286 }
Alon Levy020af1c2012-08-22 11:16:25 +0300287}
288
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300289void qxl_spice_reset_image_cache(PCIQXLDevice *qxl)
290{
Alon Levyc480bb72012-03-18 13:46:14 +0100291 trace_qxl_spice_reset_image_cache(qxl->id);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200292 spice_qxl_reset_image_cache(&qxl->ssd.qxl);
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300293}
294
295void qxl_spice_reset_cursor(PCIQXLDevice *qxl)
296{
Alon Levyc480bb72012-03-18 13:46:14 +0100297 trace_qxl_spice_reset_cursor(qxl->id);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200298 spice_qxl_reset_cursor(&qxl->ssd.qxl);
Yonit Halperin30f6da62011-10-18 18:58:54 +0200299 qemu_mutex_lock(&qxl->track_lock);
300 qxl->guest_cursor = 0;
301 qemu_mutex_unlock(&qxl->track_lock);
Gerd Hoffmann958c2bc2012-09-14 22:17:44 +0200302 if (qxl->ssd.cursor) {
303 cursor_put(qxl->ssd.cursor);
304 }
305 qxl->ssd.cursor = cursor_builtin_hidden();
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +0300306}
307
Gerd Hoffmann6f663d72017-04-21 11:22:34 +0200308static uint32_t qxl_crc32(const uint8_t *p, unsigned len)
309{
310 /*
311 * zlib xors the seed with 0xffffffff, and xors the result
312 * again with 0xffffffff; Both are not done with linux's crc32,
313 * which we want to be compatible with, so undo that.
314 */
315 return crc32(0xffffffff, p, len) ^ 0xffffffff;
316}
317
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200318static ram_addr_t qxl_rom_size(void)
319{
Michael S. Tsirkindf458922017-01-25 23:48:51 +0200320#define QXL_REQUIRED_SZ (sizeof(QXLRom) + sizeof(QXLModes) + sizeof(qxl_modes))
321#define QXL_ROM_SZ 8192
Alon Levy13d1fd42012-06-10 18:05:06 +0300322
Michael S. Tsirkindf458922017-01-25 23:48:51 +0200323 QEMU_BUILD_BUG_ON(QXL_REQUIRED_SZ > QXL_ROM_SZ);
324 return QXL_ROM_SZ;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200325}
326
327static void init_qxl_rom(PCIQXLDevice *d)
328{
Avi Kivityb1950432011-08-08 16:08:57 +0300329 QXLRom *rom = memory_region_get_ram_ptr(&d->rom_bar);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200330 QXLModes *modes = (QXLModes *)(rom + 1);
331 uint32_t ram_header_size;
332 uint32_t surface0_area_size;
333 uint32_t num_pages;
Alon Levy13d1fd42012-06-10 18:05:06 +0300334 uint32_t fb;
335 int i, n;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200336
337 memset(rom, 0, d->rom_size);
338
339 rom->magic = cpu_to_le32(QXL_ROM_MAGIC);
340 rom->id = cpu_to_le32(d->id);
341 rom->log_level = cpu_to_le32(d->guestdebug);
342 rom->modes_offset = cpu_to_le32(sizeof(QXLRom));
343
344 rom->slot_gen_bits = MEMSLOT_GENERATION_BITS;
345 rom->slot_id_bits = MEMSLOT_SLOT_BITS;
346 rom->slots_start = 1;
347 rom->slots_end = NUM_MEMSLOTS - 1;
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200348 rom->n_surfaces = cpu_to_le32(d->ssd.num_surfaces);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200349
Alon Levy13d1fd42012-06-10 18:05:06 +0300350 for (i = 0, n = 0; i < ARRAY_SIZE(qxl_modes); i++) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200351 fb = qxl_modes[i].y_res * qxl_modes[i].stride;
Alon Levy13d1fd42012-06-10 18:05:06 +0300352 if (fb > d->vgamem_size) {
353 continue;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200354 }
Alon Levy13d1fd42012-06-10 18:05:06 +0300355 modes->modes[n].id = cpu_to_le32(i);
356 modes->modes[n].x_res = cpu_to_le32(qxl_modes[i].x_res);
357 modes->modes[n].y_res = cpu_to_le32(qxl_modes[i].y_res);
358 modes->modes[n].bits = cpu_to_le32(qxl_modes[i].bits);
359 modes->modes[n].stride = cpu_to_le32(qxl_modes[i].stride);
360 modes->modes[n].x_mili = cpu_to_le32(qxl_modes[i].x_mili);
361 modes->modes[n].y_mili = cpu_to_le32(qxl_modes[i].y_mili);
362 modes->modes[n].orientation = cpu_to_le32(qxl_modes[i].orientation);
363 n++;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200364 }
Alon Levy13d1fd42012-06-10 18:05:06 +0300365 modes->n_modes = cpu_to_le32(n);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200366
367 ram_header_size = ALIGN(sizeof(QXLRam), 4096);
Alon Levy13d1fd42012-06-10 18:05:06 +0300368 surface0_area_size = ALIGN(d->vgamem_size, 4096);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200369 num_pages = d->vga.vram_size;
370 num_pages -= ram_header_size;
371 num_pages -= surface0_area_size;
Gerd Hoffmann9efc2d82013-09-11 13:14:25 +0200372 num_pages = num_pages / QXL_PAGE_SIZE;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200373
Radim Krčmář876d5162015-02-17 17:30:51 +0100374 assert(ram_header_size + surface0_area_size <= d->vga.vram_size);
375
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200376 rom->draw_area_offset = cpu_to_le32(0);
377 rom->surface0_area_size = cpu_to_le32(surface0_area_size);
378 rom->pages_offset = cpu_to_le32(surface0_area_size);
379 rom->num_pages = cpu_to_le32(num_pages);
380 rom->ram_header_offset = cpu_to_le32(d->vga.vram_size - ram_header_size);
381
Gerd Hoffmann6f663d72017-04-21 11:22:34 +0200382 if (d->xres && d->yres) {
383 /* needs linux kernel 4.12+ to work */
384 rom->client_monitors_config.count = 1;
385 rom->client_monitors_config.heads[0].left = 0;
386 rom->client_monitors_config.heads[0].top = 0;
387 rom->client_monitors_config.heads[0].right = cpu_to_le32(d->xres);
388 rom->client_monitors_config.heads[0].bottom = cpu_to_le32(d->yres);
389 rom->client_monitors_config_crc = qxl_crc32(
390 (const uint8_t *)&rom->client_monitors_config,
391 sizeof(rom->client_monitors_config));
392 }
393
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200394 d->shadow_rom = *rom;
395 d->rom = rom;
396 d->modes = modes;
397}
398
399static void init_qxl_ram(PCIQXLDevice *d)
400{
401 uint8_t *buf;
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100402 uint32_t prod;
403 QXLReleaseRing *ring;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200404
405 buf = d->vga.vram_ptr;
406 d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset));
407 d->ram->magic = cpu_to_le32(QXL_RAM_MAGIC);
408 d->ram->int_pending = cpu_to_le32(0);
409 d->ram->int_mask = cpu_to_le32(0);
Alon Levy9f0f3522011-10-23 17:03:52 +0200410 d->ram->update_surface = 0;
Anthony PERARD329f97f2013-06-17 17:38:26 +0100411 d->ram->monitors_config = 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200412 SPICE_RING_INIT(&d->ram->cmd_ring);
413 SPICE_RING_INIT(&d->ram->cursor_ring);
414 SPICE_RING_INIT(&d->ram->release_ring);
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100415
416 ring = &d->ram->release_ring;
417 prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
418 assert(prod < ARRAY_SIZE(ring->items));
419 ring->items[prod].el = 0;
420
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200421 qxl_ring_set_dirty(d);
422}
423
424/* can be called from spice server thread context */
Avi Kivityb1950432011-08-08 16:08:57 +0300425static void qxl_set_dirty(MemoryRegion *mr, ram_addr_t addr, ram_addr_t end)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200426{
Blue Swirlfd4aa972011-10-16 16:04:59 +0000427 memory_region_set_dirty(mr, addr, end - addr);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200428}
429
430static void qxl_rom_set_dirty(PCIQXLDevice *qxl)
431{
Avi Kivityb1950432011-08-08 16:08:57 +0300432 qxl_set_dirty(&qxl->rom_bar, 0, qxl->rom_size);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200433}
434
435/* called from spice server thread context only */
436static void qxl_ram_set_dirty(PCIQXLDevice *qxl, void *ptr)
437{
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200438 void *base = qxl->vga.vram_ptr;
439 intptr_t offset;
440
441 offset = ptr - base;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200442 assert(offset < qxl->vga.vram_size);
Gerd Hoffmannb0297b42013-09-11 13:15:48 +0200443 qxl_set_dirty(&qxl->vga.vram, offset, offset + 3);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200444}
445
446/* can be called from spice server thread context */
447static void qxl_ring_set_dirty(PCIQXLDevice *qxl)
448{
Avi Kivityb1950432011-08-08 16:08:57 +0300449 ram_addr_t addr = qxl->shadow_rom.ram_header_offset;
450 ram_addr_t end = qxl->vga.vram_size;
451 qxl_set_dirty(&qxl->vga.vram, addr, end);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200452}
453
454/*
455 * keep track of some command state, for savevm/loadvm.
456 * called from spice server thread context only
457 */
Alon Levyfae2afb2012-04-25 12:13:18 +0300458static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200459{
460 switch (le32_to_cpu(ext->cmd.type)) {
461 case QXL_CMD_SURFACE:
462 {
463 QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
Alon Levyfae2afb2012-04-25 12:13:18 +0300464
465 if (!cmd) {
466 return 1;
467 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200468 uint32_t id = le32_to_cpu(cmd->surface_id);
Alon Levy47eddfb2012-04-25 12:13:19 +0300469
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200470 if (id >= qxl->ssd.num_surfaces) {
Alon Levy0a530542012-05-24 12:38:12 +0300471 qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE id %d >= %d", id,
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200472 qxl->ssd.num_surfaces);
Alon Levy47eddfb2012-04-25 12:13:19 +0300473 return 1;
474 }
Alon Levy48f4ba62012-10-15 14:54:03 +0200475 if (cmd->type == QXL_SURFACE_CMD_CREATE &&
476 (cmd->u.surface_create.stride & 0x03) != 0) {
477 qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
478 cmd->u.surface_create.stride);
479 return 1;
480 }
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700481 WITH_QEMU_LOCK_GUARD(&qxl->track_lock) {
482 if (cmd->type == QXL_SURFACE_CMD_CREATE) {
483 qxl->guest_surfaces.cmds[id] = ext->cmd.data;
484 qxl->guest_surfaces.count++;
485 if (qxl->guest_surfaces.max < qxl->guest_surfaces.count) {
486 qxl->guest_surfaces.max = qxl->guest_surfaces.count;
487 }
488 }
489 if (cmd->type == QXL_SURFACE_CMD_DESTROY) {
490 qxl->guest_surfaces.cmds[id] = 0;
491 qxl->guest_surfaces.count--;
492 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200493 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200494 break;
495 }
496 case QXL_CMD_CURSOR:
497 {
498 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
Alon Levyfae2afb2012-04-25 12:13:18 +0300499
500 if (!cmd) {
501 return 1;
502 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200503 if (cmd->type == QXL_CURSOR_SET) {
Yonit Halperin30f6da62011-10-18 18:58:54 +0200504 qemu_mutex_lock(&qxl->track_lock);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200505 qxl->guest_cursor = ext->cmd.data;
Yonit Halperin30f6da62011-10-18 18:58:54 +0200506 qemu_mutex_unlock(&qxl->track_lock);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200507 }
Gerd Hoffmanndbb5fb82017-03-06 09:31:51 +0100508 if (cmd->type == QXL_CURSOR_HIDE) {
509 qemu_mutex_lock(&qxl->track_lock);
510 qxl->guest_cursor = 0;
511 qemu_mutex_unlock(&qxl->track_lock);
512 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200513 break;
514 }
515 }
Alon Levyfae2afb2012-04-25 12:13:18 +0300516 return 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200517}
518
519/* spice display interface callbacks */
520
521static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
522{
523 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
524
Alon Levyc480bb72012-03-18 13:46:14 +0100525 trace_qxl_interface_attach_worker(qxl->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200526}
527
528static void interface_set_compression_level(QXLInstance *sin, int level)
529{
530 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
531
Alon Levyc480bb72012-03-18 13:46:14 +0100532 trace_qxl_interface_set_compression_level(qxl->id, level);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200533 qxl->shadow_rom.compression_level = cpu_to_le32(level);
534 qxl->rom->compression_level = cpu_to_le32(level);
535 qxl_rom_set_dirty(qxl);
536}
537
John Snow015e02f2016-06-29 18:41:35 -0400538#if SPICE_NEEDS_SET_MM_TIME
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200539static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
540{
541 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
542
Gerd Hoffmann641381c2015-05-12 11:54:34 +0200543 if (!qemu_spice_display_is_running(&qxl->ssd)) {
544 return;
545 }
546
Alon Levyc480bb72012-03-18 13:46:14 +0100547 trace_qxl_interface_set_mm_time(qxl->id, mm_time);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200548 qxl->shadow_rom.mm_clock = cpu_to_le32(mm_time);
549 qxl->rom->mm_clock = cpu_to_le32(mm_time);
550 qxl_rom_set_dirty(qxl);
551}
John Snow015e02f2016-06-29 18:41:35 -0400552#endif
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200553
554static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
555{
556 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
557
Alon Levyc480bb72012-03-18 13:46:14 +0100558 trace_qxl_interface_get_init_info(qxl->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200559 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
560 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
561 info->num_memslots = NUM_MEMSLOTS;
562 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
563 info->internal_groupslot_id = 0;
Gerd Hoffmann9efc2d82013-09-11 13:14:25 +0200564 info->qxl_ram_size =
565 le32_to_cpu(qxl->shadow_rom.num_pages) << QXL_PAGE_BITS;
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200566 info->n_surfaces = qxl->ssd.num_surfaces;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200567}
568
Alon Levy5b778702011-06-24 15:02:47 +0200569static const char *qxl_mode_to_string(int mode)
570{
571 switch (mode) {
572 case QXL_MODE_COMPAT:
573 return "compat";
574 case QXL_MODE_NATIVE:
575 return "native";
576 case QXL_MODE_UNDEFINED:
577 return "undefined";
578 case QXL_MODE_VGA:
579 return "vga";
580 }
581 return "INVALID";
582}
583
Alon Levy8b92e292011-07-20 12:20:54 +0300584static const char *io_port_to_string(uint32_t io_port)
585{
586 if (io_port >= QXL_IO_RANGE_SIZE) {
587 return "out of range";
588 }
589 static const char *io_port_to_string[QXL_IO_RANGE_SIZE + 1] = {
590 [QXL_IO_NOTIFY_CMD] = "QXL_IO_NOTIFY_CMD",
591 [QXL_IO_NOTIFY_CURSOR] = "QXL_IO_NOTIFY_CURSOR",
592 [QXL_IO_UPDATE_AREA] = "QXL_IO_UPDATE_AREA",
593 [QXL_IO_UPDATE_IRQ] = "QXL_IO_UPDATE_IRQ",
594 [QXL_IO_NOTIFY_OOM] = "QXL_IO_NOTIFY_OOM",
595 [QXL_IO_RESET] = "QXL_IO_RESET",
596 [QXL_IO_SET_MODE] = "QXL_IO_SET_MODE",
597 [QXL_IO_LOG] = "QXL_IO_LOG",
598 [QXL_IO_MEMSLOT_ADD] = "QXL_IO_MEMSLOT_ADD",
599 [QXL_IO_MEMSLOT_DEL] = "QXL_IO_MEMSLOT_DEL",
600 [QXL_IO_DETACH_PRIMARY] = "QXL_IO_DETACH_PRIMARY",
601 [QXL_IO_ATTACH_PRIMARY] = "QXL_IO_ATTACH_PRIMARY",
602 [QXL_IO_CREATE_PRIMARY] = "QXL_IO_CREATE_PRIMARY",
603 [QXL_IO_DESTROY_PRIMARY] = "QXL_IO_DESTROY_PRIMARY",
604 [QXL_IO_DESTROY_SURFACE_WAIT] = "QXL_IO_DESTROY_SURFACE_WAIT",
605 [QXL_IO_DESTROY_ALL_SURFACES] = "QXL_IO_DESTROY_ALL_SURFACES",
Alon Levy8b92e292011-07-20 12:20:54 +0300606 [QXL_IO_UPDATE_AREA_ASYNC] = "QXL_IO_UPDATE_AREA_ASYNC",
607 [QXL_IO_MEMSLOT_ADD_ASYNC] = "QXL_IO_MEMSLOT_ADD_ASYNC",
608 [QXL_IO_CREATE_PRIMARY_ASYNC] = "QXL_IO_CREATE_PRIMARY_ASYNC",
609 [QXL_IO_DESTROY_PRIMARY_ASYNC] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
610 [QXL_IO_DESTROY_SURFACE_ASYNC] = "QXL_IO_DESTROY_SURFACE_ASYNC",
611 [QXL_IO_DESTROY_ALL_SURFACES_ASYNC]
612 = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
613 [QXL_IO_FLUSH_SURFACES_ASYNC] = "QXL_IO_FLUSH_SURFACES_ASYNC",
614 [QXL_IO_FLUSH_RELEASE] = "QXL_IO_FLUSH_RELEASE",
Alon Levy020af1c2012-08-22 11:16:25 +0300615 [QXL_IO_MONITORS_CONFIG_ASYNC] = "QXL_IO_MONITORS_CONFIG_ASYNC",
Alon Levy8b92e292011-07-20 12:20:54 +0300616 };
617 return io_port_to_string[io_port];
618}
619
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200620/* called from spice server thread context only */
621static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
622{
623 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
624 SimpleSpiceUpdate *update;
625 QXLCommandRing *ring;
626 QXLCommand *cmd;
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200627 int notify, ret;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200628
Alon Levyc480bb72012-03-18 13:46:14 +0100629 trace_qxl_ring_command_check(qxl->id, qxl_mode_to_string(qxl->mode));
630
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200631 switch (qxl->mode) {
632 case QXL_MODE_VGA:
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200633 ret = false;
634 qemu_mutex_lock(&qxl->ssd.lock);
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200635 update = QTAILQ_FIRST(&qxl->ssd.updates);
636 if (update != NULL) {
637 QTAILQ_REMOVE(&qxl->ssd.updates, update, next);
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200638 *ext = update->ext;
639 ret = true;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200640 }
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200641 qemu_mutex_unlock(&qxl->ssd.lock);
Alon Levy212496c2011-05-18 17:34:36 +0300642 if (ret) {
Alon Levyc480bb72012-03-18 13:46:14 +0100643 trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
Alon Levy212496c2011-05-18 17:34:36 +0300644 qxl_log_command(qxl, "vga", ext);
645 }
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200646 return ret;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200647 case QXL_MODE_COMPAT:
648 case QXL_MODE_NATIVE:
649 case QXL_MODE_UNDEFINED:
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200650 ring = &qxl->ram->cmd_ring;
Alon Levy087e6a42012-05-24 19:18:54 +0300651 if (qxl->guest_bug || SPICE_RING_IS_EMPTY(ring)) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200652 return false;
653 }
Alon Levy0b81c472012-04-25 12:13:21 +0300654 SPICE_RING_CONS_ITEM(qxl, ring, cmd);
655 if (!cmd) {
656 return false;
657 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200658 ext->cmd = *cmd;
659 ext->group_id = MEMSLOT_GROUP_GUEST;
660 ext->flags = qxl->cmdflags;
661 SPICE_RING_POP(ring, notify);
662 qxl_ring_set_dirty(qxl);
663 if (notify) {
664 qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
665 }
666 qxl->guest_primary.commands++;
667 qxl_track_command(qxl, ext);
668 qxl_log_command(qxl, "cmd", ext);
Gerd Hoffmann86dbcdd2017-04-10 13:31:31 +0200669 {
670 /*
671 * Windows 8 drivers place qxl commands in the vram
672 * (instead of the ram) bar. We can't live migrate such a
673 * guest, so add a migration blocker in case we detect
674 * this, to avoid triggering the assert in pre_save().
675 *
676 * https://cgit.freedesktop.org/spice/win32/qxl-wddm-dod/commit/?id=f6e099db39e7d0787f294d5fd0dce328b5210faa
677 */
678 void *msg = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
679 if (msg != NULL && (
680 msg < (void *)qxl->vga.vram_ptr ||
681 msg > ((void *)qxl->vga.vram_ptr + qxl->vga.vram_size))) {
682 if (!qxl->migration_blocker) {
683 Error *local_err = NULL;
684 error_setg(&qxl->migration_blocker,
685 "qxl: guest bug: command not in ram bar");
686 migrate_add_blocker(qxl->migration_blocker, &local_err);
687 if (local_err) {
688 error_report_err(local_err);
689 }
690 }
691 }
692 }
Alon Levy0b81c472012-04-25 12:13:21 +0300693 trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200694 return true;
695 default:
696 return false;
697 }
698}
699
700/* called from spice server thread context only */
701static int interface_req_cmd_notification(QXLInstance *sin)
702{
703 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
704 int wait = 1;
705
Alon Levyc480bb72012-03-18 13:46:14 +0100706 trace_qxl_ring_command_req_notification(qxl->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200707 switch (qxl->mode) {
708 case QXL_MODE_COMPAT:
709 case QXL_MODE_NATIVE:
710 case QXL_MODE_UNDEFINED:
711 SPICE_RING_CONS_WAIT(&qxl->ram->cmd_ring, wait);
712 qxl_ring_set_dirty(qxl);
713 break;
714 default:
715 /* nothing */
716 break;
717 }
718 return wait;
719}
720
721/* called from spice server thread context only */
722static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
723{
724 QXLReleaseRing *ring = &d->ram->release_ring;
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100725 uint32_t prod;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200726 int notify;
727
728#define QXL_FREE_BUNCH_SIZE 32
729
730 if (ring->prod - ring->cons + 1 == ring->num_items) {
731 /* ring full -- can't push */
732 return;
733 }
734 if (!flush && d->oom_running) {
735 /* collect everything from oom handler before pushing */
736 return;
737 }
738 if (!flush && d->num_free_res < QXL_FREE_BUNCH_SIZE) {
739 /* collect a bit more before pushing */
740 return;
741 }
742
743 SPICE_RING_PUSH(ring, notify);
Alon Levyc480bb72012-03-18 13:46:14 +0100744 trace_qxl_ring_res_push(d->id, qxl_mode_to_string(d->mode),
745 d->guest_surfaces.count, d->num_free_res,
746 d->last_release, notify ? "yes" : "no");
747 trace_qxl_ring_res_push_rest(d->id, ring->prod - ring->cons,
748 ring->num_items, ring->prod, ring->cons);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200749 if (notify) {
750 qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
751 }
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100752
753 ring = &d->ram->release_ring;
754 prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
755 if (prod >= ARRAY_SIZE(ring->items)) {
756 qxl_set_guest_bug(d, "SPICE_RING_PROD_ITEM indices mismatch "
757 "%u >= %zu", prod, ARRAY_SIZE(ring->items));
Alon Levy0b81c472012-04-25 12:13:21 +0300758 return;
759 }
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100760 ring->items[prod].el = 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200761 d->num_free_res = 0;
762 d->last_release = NULL;
763 qxl_ring_set_dirty(d);
764}
765
766/* called from spice server thread context only */
767static void interface_release_resource(QXLInstance *sin,
Chih-Min Chaoc9f88ce2015-04-09 02:04:14 +0800768 QXLReleaseInfoExt ext)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200769{
770 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
771 QXLReleaseRing *ring;
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100772 uint32_t prod;
773 uint64_t id;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200774
Prasad J Panditd52680f2019-04-25 12:05:34 +0530775 if (!ext.info) {
776 return;
777 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200778 if (ext.group_id == MEMSLOT_GROUP_HOST) {
779 /* host group -> vga mode update request */
Gerd Hoffmanne8e23b72014-06-20 08:12:44 +0200780 QXLCommandExt *cmdext = (void *)(intptr_t)(ext.info->id);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200781 SimpleSpiceUpdate *update;
782 g_assert(cmdext->cmd.type == QXL_CMD_DRAW);
783 update = container_of(cmdext, SimpleSpiceUpdate, ext);
784 qemu_spice_destroy_update(&qxl->ssd, update);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200785 return;
786 }
787
788 /*
789 * ext->info points into guest-visible memory
790 * pci bar 0, $command.release_info
791 */
792 ring = &qxl->ram->release_ring;
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100793 prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
794 if (prod >= ARRAY_SIZE(ring->items)) {
795 qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch "
796 "%u >= %zu", prod, ARRAY_SIZE(ring->items));
Alon Levy0b81c472012-04-25 12:13:21 +0300797 return;
798 }
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100799 if (ring->items[prod].el == 0) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200800 /* stick head into the ring */
801 id = ext.info->id;
802 ext.info->next = 0;
803 qxl_ram_set_dirty(qxl, &ext.info->next);
Daniel P. Berrangé94932c92019-04-12 13:16:26 +0100804 ring->items[prod].el = id;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200805 qxl_ring_set_dirty(qxl);
806 } else {
807 /* append item to the list */
808 qxl->last_release->next = ext.info->id;
809 qxl_ram_set_dirty(qxl, &qxl->last_release->next);
810 ext.info->next = 0;
811 qxl_ram_set_dirty(qxl, &ext.info->next);
812 }
813 qxl->last_release = ext.info;
814 qxl->num_free_res++;
Alon Levyc480bb72012-03-18 13:46:14 +0100815 trace_qxl_ring_res_put(qxl->id, qxl->num_free_res);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200816 qxl_push_free_res(qxl, 0);
817}
818
819/* called from spice server thread context only */
820static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
821{
822 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
823 QXLCursorRing *ring;
824 QXLCommand *cmd;
825 int notify;
826
Alon Levyc480bb72012-03-18 13:46:14 +0100827 trace_qxl_ring_cursor_check(qxl->id, qxl_mode_to_string(qxl->mode));
828
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200829 switch (qxl->mode) {
830 case QXL_MODE_COMPAT:
831 case QXL_MODE_NATIVE:
832 case QXL_MODE_UNDEFINED:
833 ring = &qxl->ram->cursor_ring;
834 if (SPICE_RING_IS_EMPTY(ring)) {
835 return false;
836 }
Alon Levy0b81c472012-04-25 12:13:21 +0300837 SPICE_RING_CONS_ITEM(qxl, ring, cmd);
838 if (!cmd) {
839 return false;
840 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200841 ext->cmd = *cmd;
842 ext->group_id = MEMSLOT_GROUP_GUEST;
843 ext->flags = qxl->cmdflags;
844 SPICE_RING_POP(ring, notify);
845 qxl_ring_set_dirty(qxl);
846 if (notify) {
847 qxl_send_events(qxl, QXL_INTERRUPT_CURSOR);
848 }
849 qxl->guest_primary.commands++;
850 qxl_track_command(qxl, ext);
851 qxl_log_command(qxl, "csr", ext);
Gerd Hoffmann60e94e42018-10-12 13:45:40 +0200852 if (qxl->have_vga) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200853 qxl_render_cursor(qxl, ext);
854 }
Alon Levyc480bb72012-03-18 13:46:14 +0100855 trace_qxl_ring_cursor_get(qxl->id, qxl_mode_to_string(qxl->mode));
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200856 return true;
857 default:
858 return false;
859 }
860}
861
862/* called from spice server thread context only */
863static int interface_req_cursor_notification(QXLInstance *sin)
864{
865 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
866 int wait = 1;
867
Alon Levyc480bb72012-03-18 13:46:14 +0100868 trace_qxl_ring_cursor_req_notification(qxl->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200869 switch (qxl->mode) {
870 case QXL_MODE_COMPAT:
871 case QXL_MODE_NATIVE:
872 case QXL_MODE_UNDEFINED:
873 SPICE_RING_CONS_WAIT(&qxl->ram->cursor_ring, wait);
874 qxl_ring_set_dirty(qxl);
875 break;
876 default:
877 /* nothing */
878 break;
879 }
880 return wait;
881}
882
883/* called from spice server thread context */
884static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
885{
Alon Levybaeae402012-04-25 12:13:23 +0300886 /*
887 * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
888 * use by xf86-video-qxl and is defined out in the qxl windows driver.
889 * Probably was at some earlier version that is prior to git start (2009),
890 * and is still guest trigerrable.
891 */
892 fprintf(stderr, "%s: deprecated\n", __func__);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200893}
894
895/* called from spice server thread context only */
896static int interface_flush_resources(QXLInstance *sin)
897{
898 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
899 int ret;
900
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200901 ret = qxl->num_free_res;
902 if (ret) {
903 qxl_push_free_res(qxl, 1);
904 }
905 return ret;
906}
907
Alon Levy5ff4e362011-07-20 12:20:58 +0300908static void qxl_create_guest_primary_complete(PCIQXLDevice *d);
909
Alon Levy5ff4e362011-07-20 12:20:58 +0300910/* called from spice server thread context only */
Alon Levy2e1a98c2012-02-24 23:19:30 +0200911static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
Alon Levy5ff4e362011-07-20 12:20:58 +0300912{
Alon Levy5ff4e362011-07-20 12:20:58 +0300913 uint32_t current_async;
914
915 qemu_mutex_lock(&qxl->async_lock);
916 current_async = qxl->current_async;
917 qxl->current_async = QXL_UNDEFINED_IO;
918 qemu_mutex_unlock(&qxl->async_lock);
919
Alon Levyc480bb72012-03-18 13:46:14 +0100920 trace_qxl_interface_async_complete_io(qxl->id, current_async, cookie);
Alon Levy2e1a98c2012-02-24 23:19:30 +0200921 if (!cookie) {
922 fprintf(stderr, "qxl: %s: error, cookie is NULL\n", __func__);
923 return;
924 }
925 if (cookie && current_async != cookie->io) {
926 fprintf(stderr,
Alon Levy2fce7ed2012-04-25 12:13:20 +0300927 "qxl: %s: error: current_async = %d != %"
928 PRId64 " = cookie->io\n", __func__, current_async, cookie->io);
Alon Levy2e1a98c2012-02-24 23:19:30 +0200929 }
Alon Levy5ff4e362011-07-20 12:20:58 +0300930 switch (current_async) {
Alon Levy81fb6f12012-02-24 23:19:31 +0200931 case QXL_IO_MEMSLOT_ADD_ASYNC:
932 case QXL_IO_DESTROY_PRIMARY_ASYNC:
933 case QXL_IO_UPDATE_AREA_ASYNC:
934 case QXL_IO_FLUSH_SURFACES_ASYNC:
Alon Levy020af1c2012-08-22 11:16:25 +0300935 case QXL_IO_MONITORS_CONFIG_ASYNC:
Alon Levy81fb6f12012-02-24 23:19:31 +0200936 break;
Alon Levy5ff4e362011-07-20 12:20:58 +0300937 case QXL_IO_CREATE_PRIMARY_ASYNC:
938 qxl_create_guest_primary_complete(qxl);
939 break;
940 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
941 qxl_spice_destroy_surfaces_complete(qxl);
942 break;
943 case QXL_IO_DESTROY_SURFACE_ASYNC:
Alon Levy2e1a98c2012-02-24 23:19:30 +0200944 qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
Alon Levy5ff4e362011-07-20 12:20:58 +0300945 break;
Alon Levy81fb6f12012-02-24 23:19:31 +0200946 default:
947 fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
948 current_async);
Alon Levy5ff4e362011-07-20 12:20:58 +0300949 }
950 qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
951}
952
Alon Levy2e1a98c2012-02-24 23:19:30 +0200953/* called from spice server thread context only */
Alon Levy81fb6f12012-02-24 23:19:31 +0200954static void interface_update_area_complete(QXLInstance *sin,
955 uint32_t surface_id,
956 QXLRect *dirty, uint32_t num_updated_rects)
957{
958 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
959 int i;
960 int qxl_i;
961
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700962 QEMU_LOCK_GUARD(&qxl->ssd.lock);
Gerd Hoffmann2f5ae772016-06-08 16:11:41 +0200963 if (surface_id != 0 || !num_updated_rects ||
964 !qxl->render_update_cookie_num) {
Alon Levy81fb6f12012-02-24 23:19:31 +0200965 return;
966 }
Alon Levyc480bb72012-03-18 13:46:14 +0100967 trace_qxl_interface_update_area_complete(qxl->id, surface_id, dirty->left,
968 dirty->right, dirty->top, dirty->bottom);
969 trace_qxl_interface_update_area_complete_rest(qxl->id, num_updated_rects);
Alon Levy81fb6f12012-02-24 23:19:31 +0200970 if (qxl->num_dirty_rects + num_updated_rects > QXL_NUM_DIRTY_RECTS) {
971 /*
972 * overflow - treat this as a full update. Not expected to be common.
973 */
Alon Levyc480bb72012-03-18 13:46:14 +0100974 trace_qxl_interface_update_area_complete_overflow(qxl->id,
975 QXL_NUM_DIRTY_RECTS);
Alon Levy81fb6f12012-02-24 23:19:31 +0200976 qxl->guest_primary.resized = 1;
977 }
978 if (qxl->guest_primary.resized) {
979 /*
980 * Don't bother copying or scheduling the bh since we will flip
981 * the whole area anyway on completion of the update_area async call
982 */
Alon Levy81fb6f12012-02-24 23:19:31 +0200983 return;
984 }
985 qxl_i = qxl->num_dirty_rects;
986 for (i = 0; i < num_updated_rects; i++) {
987 qxl->dirty[qxl_i++] = dirty[i];
988 }
989 qxl->num_dirty_rects += num_updated_rects;
Alon Levyc480bb72012-03-18 13:46:14 +0100990 trace_qxl_interface_update_area_complete_schedule_bh(qxl->id,
991 qxl->num_dirty_rects);
Alon Levy81fb6f12012-02-24 23:19:31 +0200992 qemu_bh_schedule(qxl->update_area_bh);
Alon Levy81fb6f12012-02-24 23:19:31 +0200993}
994
995/* called from spice server thread context only */
Alon Levy2e1a98c2012-02-24 23:19:30 +0200996static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
997{
998 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
Peter Maydell5dba0d42012-03-16 13:50:04 +0000999 QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
Alon Levy2e1a98c2012-02-24 23:19:30 +02001000
1001 switch (cookie->type) {
1002 case QXL_COOKIE_TYPE_IO:
1003 interface_async_complete_io(qxl, cookie);
Alon Levy81fb6f12012-02-24 23:19:31 +02001004 g_free(cookie);
1005 break;
1006 case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA:
1007 qxl_render_update_area_done(qxl, cookie);
Alon Levy2e1a98c2012-02-24 23:19:30 +02001008 break;
Alon Levy020af1c2012-08-22 11:16:25 +03001009 case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG:
1010 break;
Alon Levy2e1a98c2012-02-24 23:19:30 +02001011 default:
1012 fprintf(stderr, "qxl: %s: unexpected cookie type %d\n",
1013 __func__, cookie->type);
Alon Levy81fb6f12012-02-24 23:19:31 +02001014 g_free(cookie);
Alon Levy2e1a98c2012-02-24 23:19:30 +02001015 }
Alon Levy2e1a98c2012-02-24 23:19:30 +02001016}
1017
Søren Sandmann Pedersenc10018d2012-09-04 10:14:48 -04001018/* called from spice server thread context only */
1019static void interface_set_client_capabilities(QXLInstance *sin,
1020 uint8_t client_present,
1021 uint8_t caps[58])
1022{
1023 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
1024
Alon Levye0ac6092013-01-21 14:48:06 +02001025 if (qxl->revision < 4) {
1026 trace_qxl_set_client_capabilities_unsupported_by_revision(qxl->id,
1027 qxl->revision);
1028 return;
1029 }
1030
Hans de Goedeab902982012-09-07 21:48:22 +02001031 if (runstate_check(RUN_STATE_INMIGRATE) ||
1032 runstate_check(RUN_STATE_POSTMIGRATE)) {
1033 return;
1034 }
1035
Søren Sandmann Pedersenc10018d2012-09-04 10:14:48 -04001036 qxl->shadow_rom.client_present = client_present;
Markus Armbruster08688af2013-01-10 14:24:50 +01001037 memcpy(qxl->shadow_rom.client_capabilities, caps,
1038 sizeof(qxl->shadow_rom.client_capabilities));
Søren Sandmann Pedersenc10018d2012-09-04 10:14:48 -04001039 qxl->rom->client_present = client_present;
Markus Armbruster08688af2013-01-10 14:24:50 +01001040 memcpy(qxl->rom->client_capabilities, caps,
1041 sizeof(qxl->rom->client_capabilities));
Søren Sandmann Pedersenc10018d2012-09-04 10:14:48 -04001042 qxl_rom_set_dirty(qxl);
1043
1044 qxl_send_events(qxl, QXL_INTERRUPT_CLIENT);
1045}
1046
Christophe Fergeau6c756502016-10-28 16:48:40 +02001047static bool qxl_rom_monitors_config_changed(QXLRom *rom,
1048 VDAgentMonitorsConfig *monitors_config,
1049 unsigned int max_outputs)
1050{
1051 int i;
1052 unsigned int monitors_count;
1053
1054 monitors_count = MIN(monitors_config->num_of_monitors, max_outputs);
1055
1056 if (rom->client_monitors_config.count != monitors_count) {
1057 return true;
1058 }
1059
1060 for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
1061 VDAgentMonConfig *monitor = &monitors_config->monitors[i];
1062 QXLURect *rect = &rom->client_monitors_config.heads[i];
1063 /* monitor->depth ignored */
1064 if ((rect->left != monitor->x) ||
1065 (rect->top != monitor->y) ||
1066 (rect->right != monitor->x + monitor->width) ||
1067 (rect->bottom != monitor->y + monitor->height)) {
1068 return true;
1069 }
1070 }
1071
1072 return false;
1073}
1074
Alon Levya639ab02012-09-12 16:13:28 +03001075/* called from main context only */
1076static int interface_client_monitors_config(QXLInstance *sin,
1077 VDAgentMonitorsConfig *monitors_config)
1078{
1079 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
1080 QXLRom *rom = memory_region_get_ram_ptr(&qxl->rom_bar);
1081 int i;
Frediano Ziglio567161f2015-07-06 07:56:38 +01001082 unsigned max_outputs = ARRAY_SIZE(rom->client_monitors_config.heads);
Christophe Fergeau6c756502016-10-28 16:48:40 +02001083 bool config_changed = false;
Alon Levya639ab02012-09-12 16:13:28 +03001084
Alon Levye0ac6092013-01-21 14:48:06 +02001085 if (qxl->revision < 4) {
1086 trace_qxl_client_monitors_config_unsupported_by_device(qxl->id,
1087 qxl->revision);
1088 return 0;
1089 }
Alon Levya639ab02012-09-12 16:13:28 +03001090 /*
1091 * Older windows drivers set int_mask to 0 when their ISR is called,
1092 * then later set it to ~0. So it doesn't relate to the actual interrupts
1093 * handled. However, they are old, so clearly they don't support this
1094 * interrupt
1095 */
1096 if (qxl->ram->int_mask == 0 || qxl->ram->int_mask == ~0 ||
1097 !(qxl->ram->int_mask & QXL_INTERRUPT_CLIENT_MONITORS_CONFIG)) {
1098 trace_qxl_client_monitors_config_unsupported_by_guest(qxl->id,
1099 qxl->ram->int_mask,
1100 monitors_config);
1101 return 0;
1102 }
1103 if (!monitors_config) {
1104 return 1;
1105 }
Frediano Ziglio567161f2015-07-06 07:56:38 +01001106
1107#if SPICE_SERVER_VERSION >= 0x000c06 /* release 0.12.6 */
1108 /* limit number of outputs based on setting limit */
1109 if (qxl->max_outputs && qxl->max_outputs <= max_outputs) {
1110 max_outputs = qxl->max_outputs;
1111 }
1112#endif
1113
Christophe Fergeau6c756502016-10-28 16:48:40 +02001114 config_changed = qxl_rom_monitors_config_changed(rom,
1115 monitors_config,
1116 max_outputs);
1117
Alon Levya639ab02012-09-12 16:13:28 +03001118 memset(&rom->client_monitors_config, 0,
1119 sizeof(rom->client_monitors_config));
1120 rom->client_monitors_config.count = monitors_config->num_of_monitors;
1121 /* monitors_config->flags ignored */
Frediano Ziglio567161f2015-07-06 07:56:38 +01001122 if (rom->client_monitors_config.count >= max_outputs) {
Alon Levya639ab02012-09-12 16:13:28 +03001123 trace_qxl_client_monitors_config_capped(qxl->id,
1124 monitors_config->num_of_monitors,
Frediano Ziglio567161f2015-07-06 07:56:38 +01001125 max_outputs);
1126 rom->client_monitors_config.count = max_outputs;
Alon Levya639ab02012-09-12 16:13:28 +03001127 }
1128 for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
1129 VDAgentMonConfig *monitor = &monitors_config->monitors[i];
1130 QXLURect *rect = &rom->client_monitors_config.heads[i];
1131 /* monitor->depth ignored */
1132 rect->left = monitor->x;
1133 rect->top = monitor->y;
1134 rect->right = monitor->x + monitor->width;
1135 rect->bottom = monitor->y + monitor->height;
1136 }
1137 rom->client_monitors_config_crc = qxl_crc32(
1138 (const uint8_t *)&rom->client_monitors_config,
1139 sizeof(rom->client_monitors_config));
1140 trace_qxl_client_monitors_config_crc(qxl->id,
1141 sizeof(rom->client_monitors_config),
1142 rom->client_monitors_config_crc);
1143
1144 trace_qxl_interrupt_client_monitors_config(qxl->id,
1145 rom->client_monitors_config.count,
1146 rom->client_monitors_config.heads);
Christophe Fergeau6c756502016-10-28 16:48:40 +02001147 if (config_changed) {
1148 qxl_send_events(qxl, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG);
1149 }
Alon Levya639ab02012-09-12 16:13:28 +03001150 return 1;
1151}
Alon Levya639ab02012-09-12 16:13:28 +03001152
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001153static const QXLInterface qxl_interface = {
1154 .base.type = SPICE_INTERFACE_QXL,
1155 .base.description = "qxl gpu",
1156 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
1157 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
1158
1159 .attache_worker = interface_attach_worker,
1160 .set_compression_level = interface_set_compression_level,
John Snow015e02f2016-06-29 18:41:35 -04001161#if SPICE_NEEDS_SET_MM_TIME
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001162 .set_mm_time = interface_set_mm_time,
John Snow015e02f2016-06-29 18:41:35 -04001163#endif
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001164 .get_init_info = interface_get_init_info,
1165
1166 /* the callbacks below are called from spice server thread context */
1167 .get_command = interface_get_command,
1168 .req_cmd_notification = interface_req_cmd_notification,
1169 .release_resource = interface_release_resource,
1170 .get_cursor_command = interface_get_cursor_command,
1171 .req_cursor_notification = interface_req_cursor_notification,
1172 .notify_update = interface_notify_update,
1173 .flush_resources = interface_flush_resources,
Alon Levy5ff4e362011-07-20 12:20:58 +03001174 .async_complete = interface_async_complete,
Alon Levy81fb6f12012-02-24 23:19:31 +02001175 .update_area_complete = interface_update_area_complete,
Søren Sandmann Pedersenc10018d2012-09-04 10:14:48 -04001176 .set_client_capabilities = interface_set_client_capabilities,
Alon Levya639ab02012-09-12 16:13:28 +03001177 .client_monitors_config = interface_client_monitors_config,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001178};
1179
Gerd Hoffmann15162332014-09-24 17:05:45 +02001180static const GraphicHwOps qxl_ops = {
1181 .gfx_update = qxl_hw_update,
Marc-André Lureau4d631622015-08-24 13:20:49 +02001182 .gfx_update_async = true,
Gerd Hoffmann15162332014-09-24 17:05:45 +02001183};
1184
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001185static void qxl_enter_vga_mode(PCIQXLDevice *d)
1186{
1187 if (d->mode == QXL_MODE_VGA) {
1188 return;
1189 }
Alon Levyc480bb72012-03-18 13:46:14 +01001190 trace_qxl_enter_vga_mode(d->id);
Hans de Goede0a2b5e32013-04-23 10:18:16 +02001191 spice_qxl_driver_unload(&d->ssd.qxl);
Gerd Hoffmann15162332014-09-24 17:05:45 +02001192 graphic_console_set_hwops(d->ssd.dcl.con, d->vga.hw_ops, &d->vga);
Gerd Hoffmann3dcadce2014-11-04 14:16:12 +01001193 update_displaychangelistener(&d->ssd.dcl, GUI_REFRESH_INTERVAL_DEFAULT);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001194 qemu_spice_create_host_primary(&d->ssd);
1195 d->mode = QXL_MODE_VGA;
Marc-André Lureaua703d3a2017-04-06 14:05:13 +02001196 qemu_spice_display_switch(&d->ssd, d->ssd.ds);
Alon Levy0f7bfd82012-05-24 19:18:53 +03001197 vga_dirty_log_start(&d->vga);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +01001198 graphic_hw_update(d->vga.con);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001199}
1200
1201static void qxl_exit_vga_mode(PCIQXLDevice *d)
1202{
1203 if (d->mode != QXL_MODE_VGA) {
1204 return;
1205 }
Alon Levyc480bb72012-03-18 13:46:14 +01001206 trace_qxl_exit_vga_mode(d->id);
Gerd Hoffmann15162332014-09-24 17:05:45 +02001207 graphic_console_set_hwops(d->ssd.dcl.con, &qxl_ops, d);
Gerd Hoffmann3dcadce2014-11-04 14:16:12 +01001208 update_displaychangelistener(&d->ssd.dcl, GUI_REFRESH_INTERVAL_IDLE);
Alon Levy0f7bfd82012-05-24 19:18:53 +03001209 vga_dirty_log_stop(&d->vga);
Alon Levy5ff4e362011-07-20 12:20:58 +03001210 qxl_destroy_primary(d, QXL_SYNC);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001211}
1212
Yonit Halperin40010ae2011-09-05 08:45:59 +03001213static void qxl_update_irq(PCIQXLDevice *d)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001214{
1215 uint32_t pending = le32_to_cpu(d->ram->int_pending);
1216 uint32_t mask = le32_to_cpu(d->ram->int_mask);
1217 int level = !!(pending & mask);
Marcel Apfelbaum9e64f8a2013-10-07 10:36:39 +03001218 pci_set_irq(&d->pci, level);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001219 qxl_ring_set_dirty(d);
1220}
1221
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001222static void qxl_check_state(PCIQXLDevice *d)
1223{
1224 QXLRam *ram = d->ram;
Yonit Halperin71d388d2012-08-21 11:51:56 +03001225 int spice_display_running = qemu_spice_display_is_running(&d->ssd);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001226
Yonit Halperin71d388d2012-08-21 11:51:56 +03001227 assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cmd_ring));
1228 assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cursor_ring));
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001229}
1230
1231static void qxl_reset_state(PCIQXLDevice *d)
1232{
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001233 QXLRom *rom = d->rom;
1234
Yonit Halperinbe48e992011-08-09 16:12:40 +03001235 qxl_check_state(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001236 d->shadow_rom.update_id = cpu_to_le32(0);
1237 *rom = d->shadow_rom;
1238 qxl_rom_set_dirty(d);
1239 init_qxl_ram(d);
1240 d->num_free_res = 0;
1241 d->last_release = NULL;
1242 memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty));
Alon Levyf06b8522014-01-20 12:44:19 +02001243 qxl_update_irq(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001244}
1245
1246static void qxl_soft_reset(PCIQXLDevice *d)
1247{
Alon Levyc480bb72012-03-18 13:46:14 +01001248 trace_qxl_soft_reset(d->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001249 qxl_check_state(d);
Alon Levy087e6a42012-05-24 19:18:54 +03001250 qxl_clear_guest_bug(d);
Gerd Hoffmann05fa1c72015-10-20 09:57:30 +02001251 qemu_mutex_lock(&d->async_lock);
Alon Levya5f68c22012-06-11 09:24:01 +03001252 d->current_async = QXL_UNDEFINED_IO;
Gerd Hoffmann05fa1c72015-10-20 09:57:30 +02001253 qemu_mutex_unlock(&d->async_lock);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001254
Gerd Hoffmann60e94e42018-10-12 13:45:40 +02001255 if (d->have_vga) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001256 qxl_enter_vga_mode(d);
1257 } else {
1258 d->mode = QXL_MODE_UNDEFINED;
1259 }
1260}
1261
1262static void qxl_hard_reset(PCIQXLDevice *d, int loadvm)
1263{
Gerd Hoffmann75c70e32013-12-09 16:03:49 +01001264 bool startstop = qemu_spice_display_is_running(&d->ssd);
1265
Alon Levyc480bb72012-03-18 13:46:14 +01001266 trace_qxl_hard_reset(d->id, loadvm);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001267
Gerd Hoffmann75c70e32013-12-09 16:03:49 +01001268 if (startstop) {
1269 qemu_spice_display_stop();
1270 }
1271
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +03001272 qxl_spice_reset_cursor(d);
1273 qxl_spice_reset_image_cache(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001274 qxl_reset_surfaces(d);
1275 qxl_reset_memslots(d);
1276
1277 /* pre loadvm reset must not touch QXLRam. This lives in
1278 * device memory, is migrated together with RAM and thus
1279 * already loaded at this point */
1280 if (!loadvm) {
1281 qxl_reset_state(d);
1282 }
1283 qemu_spice_create_host_memslot(&d->ssd);
1284 qxl_soft_reset(d);
Gerd Hoffmann75c70e32013-12-09 16:03:49 +01001285
Gerd Hoffmann86dbcdd2017-04-10 13:31:31 +02001286 if (d->migration_blocker) {
1287 migrate_del_blocker(d->migration_blocker);
1288 error_free(d->migration_blocker);
1289 d->migration_blocker = NULL;
1290 }
1291
Gerd Hoffmann75c70e32013-12-09 16:03:49 +01001292 if (startstop) {
1293 qemu_spice_display_start();
1294 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001295}
1296
1297static void qxl_reset_handler(DeviceState *dev)
1298{
Gongleic69f6c72015-05-12 17:27:10 +08001299 PCIQXLDevice *d = PCI_QXL(PCI_DEVICE(dev));
Alon Levyc480bb72012-03-18 13:46:14 +01001300
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001301 qxl_hard_reset(d, 0);
1302}
1303
1304static void qxl_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1305{
1306 VGACommonState *vga = opaque;
1307 PCIQXLDevice *qxl = container_of(vga, PCIQXLDevice, vga);
1308
Alon Levyc480bb72012-03-18 13:46:14 +01001309 trace_qxl_io_write_vga(qxl->id, qxl_mode_to_string(qxl->mode), addr, val);
Gerd Hoffmanned71c092020-02-06 08:43:58 +01001310 if (qxl->mode != QXL_MODE_VGA &&
1311 qxl->revision <= QXL_REVISION_STABLE_V12) {
Alon Levy5ff4e362011-07-20 12:20:58 +03001312 qxl_destroy_primary(qxl, QXL_SYNC);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001313 qxl_soft_reset(qxl);
1314 }
1315 vga_ioport_write(opaque, addr, val);
1316}
1317
Gerd Hoffmannf67ab772011-11-03 18:21:54 +01001318static const MemoryRegionPortio qxl_vga_portio_list[] = {
1319 { 0x04, 2, 1, .read = vga_ioport_read,
1320 .write = qxl_vga_ioport_write }, /* 3b4 */
1321 { 0x0a, 1, 1, .read = vga_ioport_read,
1322 .write = qxl_vga_ioport_write }, /* 3ba */
1323 { 0x10, 16, 1, .read = vga_ioport_read,
1324 .write = qxl_vga_ioport_write }, /* 3c0 */
1325 { 0x24, 2, 1, .read = vga_ioport_read,
1326 .write = qxl_vga_ioport_write }, /* 3d4 */
1327 { 0x2a, 1, 1, .read = vga_ioport_read,
1328 .write = qxl_vga_ioport_write }, /* 3da */
1329 PORTIO_END_OF_LIST(),
1330};
1331
Alon Levye954ea22012-04-25 12:13:24 +03001332static int qxl_add_memslot(PCIQXLDevice *d, uint32_t slot_id, uint64_t delta,
1333 qxl_async_io async)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001334{
1335 static const int regions[] = {
1336 QXL_RAM_RANGE_INDEX,
1337 QXL_VRAM_RANGE_INDEX,
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02001338 QXL_VRAM64_RANGE_INDEX,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001339 };
1340 uint64_t guest_start;
1341 uint64_t guest_end;
1342 int pci_region;
1343 pcibus_t pci_start;
1344 pcibus_t pci_end;
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001345 MemoryRegion *mr;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001346 intptr_t virt_start;
1347 QXLDevMemSlot memslot;
1348 int i;
1349
1350 guest_start = le64_to_cpu(d->guest_slots[slot_id].slot.mem_start);
1351 guest_end = le64_to_cpu(d->guest_slots[slot_id].slot.mem_end);
1352
Alon Levyc480bb72012-03-18 13:46:14 +01001353 trace_qxl_memslot_add_guest(d->id, slot_id, guest_start, guest_end);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001354
Alon Levye954ea22012-04-25 12:13:24 +03001355 if (slot_id >= NUM_MEMSLOTS) {
Alon Levy0a530542012-05-24 12:38:12 +03001356 qxl_set_guest_bug(d, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__,
Alon Levye954ea22012-04-25 12:13:24 +03001357 slot_id, NUM_MEMSLOTS);
1358 return 1;
1359 }
1360 if (guest_start > guest_end) {
Alon Levy0a530542012-05-24 12:38:12 +03001361 qxl_set_guest_bug(d, "%s: guest_start > guest_end 0x%" PRIx64
Alon Levye954ea22012-04-25 12:13:24 +03001362 " > 0x%" PRIx64, __func__, guest_start, guest_end);
1363 return 1;
1364 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001365
1366 for (i = 0; i < ARRAY_SIZE(regions); i++) {
1367 pci_region = regions[i];
1368 pci_start = d->pci.io_regions[pci_region].addr;
1369 pci_end = pci_start + d->pci.io_regions[pci_region].size;
1370 /* mapped? */
1371 if (pci_start == -1) {
1372 continue;
1373 }
1374 /* start address in range ? */
1375 if (guest_start < pci_start || guest_start > pci_end) {
1376 continue;
1377 }
1378 /* end address in range ? */
1379 if (guest_end > pci_end) {
1380 continue;
1381 }
1382 /* passed */
1383 break;
1384 }
Alon Levye954ea22012-04-25 12:13:24 +03001385 if (i == ARRAY_SIZE(regions)) {
Alon Levy0a530542012-05-24 12:38:12 +03001386 qxl_set_guest_bug(d, "%s: finished loop without match", __func__);
Alon Levye954ea22012-04-25 12:13:24 +03001387 return 1;
1388 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001389
1390 switch (pci_region) {
1391 case QXL_RAM_RANGE_INDEX:
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001392 mr = &d->vga.vram;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001393 break;
1394 case QXL_VRAM_RANGE_INDEX:
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02001395 case 4 /* vram 64bit */:
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001396 mr = &d->vram_bar;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001397 break;
1398 default:
1399 /* should not happen */
Alon Levy0a530542012-05-24 12:38:12 +03001400 qxl_set_guest_bug(d, "%s: pci_region = %d", __func__, pci_region);
Alon Levye954ea22012-04-25 12:13:24 +03001401 return 1;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001402 }
1403
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001404 virt_start = (intptr_t)memory_region_get_ram_ptr(mr);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001405 memslot.slot_id = slot_id;
1406 memslot.slot_group_id = MEMSLOT_GROUP_GUEST; /* guest group */
1407 memslot.virt_start = virt_start + (guest_start - pci_start);
1408 memslot.virt_end = virt_start + (guest_end - pci_start);
1409 memslot.addr_delta = memslot.virt_start - delta;
1410 memslot.generation = d->rom->slot_generation = 0;
1411 qxl_rom_set_dirty(d);
1412
Alon Levy5ff4e362011-07-20 12:20:58 +03001413 qemu_spice_add_memslot(&d->ssd, &memslot, async);
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001414 d->guest_slots[slot_id].mr = mr;
1415 d->guest_slots[slot_id].offset = memslot.virt_start - virt_start;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001416 d->guest_slots[slot_id].size = memslot.virt_end - memslot.virt_start;
1417 d->guest_slots[slot_id].delta = delta;
1418 d->guest_slots[slot_id].active = 1;
Alon Levye954ea22012-04-25 12:13:24 +03001419 return 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001420}
1421
1422static void qxl_del_memslot(PCIQXLDevice *d, uint32_t slot_id)
1423{
Gerd Hoffmann5c59d112011-07-20 12:20:50 +03001424 qemu_spice_del_memslot(&d->ssd, MEMSLOT_GROUP_HOST, slot_id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001425 d->guest_slots[slot_id].active = 0;
1426}
1427
1428static void qxl_reset_memslots(PCIQXLDevice *d)
1429{
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +03001430 qxl_spice_reset_memslots(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001431 memset(&d->guest_slots, 0, sizeof(d->guest_slots));
1432}
1433
1434static void qxl_reset_surfaces(PCIQXLDevice *d)
1435{
Alon Levyc480bb72012-03-18 13:46:14 +01001436 trace_qxl_reset_surfaces(d->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001437 d->mode = QXL_MODE_UNDEFINED;
Alon Levy5ff4e362011-07-20 12:20:58 +03001438 qxl_spice_destroy_surfaces(d, QXL_SYNC);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001439}
1440
Yonit Halperine25139b2012-02-15 11:22:15 +02001441/* can be also called from spice server thread context */
Gerd Hoffmann726bdf62016-06-22 14:07:22 +02001442static bool qxl_get_check_slot_offset(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
1443 uint32_t *s, uint64_t *o)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001444{
1445 uint64_t phys = le64_to_cpu(pqxl);
1446 uint32_t slot = (phys >> (64 - 8)) & 0xff;
1447 uint64_t offset = phys & 0xffffffffffff;
1448
Gerd Hoffmann726bdf62016-06-22 14:07:22 +02001449 if (slot >= NUM_MEMSLOTS) {
1450 qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
1451 NUM_MEMSLOTS);
1452 return false;
1453 }
1454 if (!qxl->guest_slots[slot].active) {
1455 qxl_set_guest_bug(qxl, "inactive slot %d\n", slot);
1456 return false;
1457 }
1458 if (offset < qxl->guest_slots[slot].delta) {
1459 qxl_set_guest_bug(qxl,
Alon Levy0a530542012-05-24 12:38:12 +03001460 "slot %d offset %"PRIu64" < delta %"PRIu64"\n",
Alon Levy4b635c52012-04-25 12:13:17 +03001461 slot, offset, qxl->guest_slots[slot].delta);
Gerd Hoffmann726bdf62016-06-22 14:07:22 +02001462 return false;
1463 }
1464 offset -= qxl->guest_slots[slot].delta;
1465 if (offset > qxl->guest_slots[slot].size) {
1466 qxl_set_guest_bug(qxl,
Alon Levy0a530542012-05-24 12:38:12 +03001467 "slot %d offset %"PRIu64" > size %"PRIu64"\n",
Alon Levy4b635c52012-04-25 12:13:17 +03001468 slot, offset, qxl->guest_slots[slot].size);
Gerd Hoffmann726bdf62016-06-22 14:07:22 +02001469 return false;
1470 }
1471
1472 *s = slot;
1473 *o = offset;
1474 return true;
1475}
1476
1477/* can be also called from spice server thread context */
1478void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id)
1479{
1480 uint64_t offset;
1481 uint32_t slot;
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001482 void *ptr;
Gerd Hoffmann726bdf62016-06-22 14:07:22 +02001483
1484 switch (group_id) {
1485 case MEMSLOT_GROUP_HOST:
1486 offset = le64_to_cpu(pqxl) & 0xffffffffffff;
1487 return (void *)(intptr_t)offset;
1488 case MEMSLOT_GROUP_GUEST:
1489 if (!qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset)) {
Alon Levy4b635c52012-04-25 12:13:17 +03001490 return NULL;
1491 }
Gerd Hoffmann3cb51582016-06-22 14:07:23 +02001492 ptr = memory_region_get_ram_ptr(qxl->guest_slots[slot].mr);
1493 ptr += qxl->guest_slots[slot].offset;
1494 ptr += offset;
1495 return ptr;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001496 }
Alon Levy4b635c52012-04-25 12:13:17 +03001497 return NULL;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001498}
1499
Alon Levy5ff4e362011-07-20 12:20:58 +03001500static void qxl_create_guest_primary_complete(PCIQXLDevice *qxl)
1501{
1502 /* for local rendering */
1503 qxl_render_resize(qxl);
1504}
1505
1506static void qxl_create_guest_primary(PCIQXLDevice *qxl, int loadvm,
1507 qxl_async_io async)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001508{
1509 QXLDevSurfaceCreate surface;
1510 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
Alon Levy3761abb2014-01-20 18:57:12 +02001511 uint32_t requested_height = le32_to_cpu(sc->height);
Alon Levy13d1fd42012-06-10 18:05:06 +03001512 int requested_stride = le32_to_cpu(sc->stride);
1513
Alon Levy3761abb2014-01-20 18:57:12 +02001514 if (requested_stride == INT32_MIN ||
1515 abs(requested_stride) * (uint64_t)requested_height
1516 > qxl->vgamem_size) {
1517 qxl_set_guest_bug(qxl, "%s: requested primary larger than framebuffer"
1518 " stride %d x height %" PRIu32 " > %" PRIu32,
1519 __func__, requested_stride, requested_height,
1520 qxl->vgamem_size);
Alon Levy13d1fd42012-06-10 18:05:06 +03001521 return;
1522 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001523
Alon Levyddf9f4b2012-04-25 12:43:31 +03001524 if (qxl->mode == QXL_MODE_NATIVE) {
Alon Levy0a530542012-05-24 12:38:12 +03001525 qxl_set_guest_bug(qxl, "%s: nop since already in QXL_MODE_NATIVE",
Alon Levyddf9f4b2012-04-25 12:43:31 +03001526 __func__);
1527 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001528 qxl_exit_vga_mode(qxl);
1529
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001530 surface.format = le32_to_cpu(sc->format);
1531 surface.height = le32_to_cpu(sc->height);
1532 surface.mem = le64_to_cpu(sc->mem);
1533 surface.position = le32_to_cpu(sc->position);
1534 surface.stride = le32_to_cpu(sc->stride);
1535 surface.width = le32_to_cpu(sc->width);
1536 surface.type = le32_to_cpu(sc->type);
1537 surface.flags = le32_to_cpu(sc->flags);
Alon Levyc480bb72012-03-18 13:46:14 +01001538 trace_qxl_create_guest_primary(qxl->id, sc->width, sc->height, sc->mem,
1539 sc->format, sc->position);
1540 trace_qxl_create_guest_primary_rest(qxl->id, sc->stride, sc->type,
1541 sc->flags);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001542
Alon Levy48f4ba62012-10-15 14:54:03 +02001543 if ((surface.stride & 0x3) != 0) {
1544 qxl_set_guest_bug(qxl, "primary surface stride = %d %% 4 != 0",
1545 surface.stride);
1546 return;
1547 }
1548
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001549 surface.mouse_mode = true;
1550 surface.group_id = MEMSLOT_GROUP_GUEST;
1551 if (loadvm) {
1552 surface.flags |= QXL_SURF_FLAG_KEEP_DATA;
1553 }
1554
1555 qxl->mode = QXL_MODE_NATIVE;
1556 qxl->cmdflags = 0;
Alon Levy5ff4e362011-07-20 12:20:58 +03001557 qemu_spice_create_primary_surface(&qxl->ssd, 0, &surface, async);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001558
Alon Levy5ff4e362011-07-20 12:20:58 +03001559 if (async == QXL_SYNC) {
1560 qxl_create_guest_primary_complete(qxl);
1561 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001562}
1563
Alon Levy5ff4e362011-07-20 12:20:58 +03001564/* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
1565 * done (in QXL_SYNC case), 0 otherwise. */
1566static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001567{
1568 if (d->mode == QXL_MODE_UNDEFINED) {
Alon Levy5ff4e362011-07-20 12:20:58 +03001569 return 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001570 }
Alon Levyc480bb72012-03-18 13:46:14 +01001571 trace_qxl_destroy_primary(d->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001572 d->mode = QXL_MODE_UNDEFINED;
Alon Levy5ff4e362011-07-20 12:20:58 +03001573 qemu_spice_destroy_primary_surface(&d->ssd, 0, async);
Yonit Halperin30f6da62011-10-18 18:58:54 +02001574 qxl_spice_reset_cursor(d);
Alon Levy5ff4e362011-07-20 12:20:58 +03001575 return 1;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001576}
1577
Gerd Hoffmann9c704342014-02-19 11:40:17 +01001578static void qxl_set_mode(PCIQXLDevice *d, unsigned int modenr, int loadvm)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001579{
1580 pcibus_t start = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
1581 pcibus_t end = d->pci.io_regions[QXL_RAM_RANGE_INDEX].size + start;
1582 QXLMode *mode = d->modes->modes + modenr;
1583 uint64_t devmem = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
1584 QXLMemSlot slot = {
1585 .mem_start = start,
1586 .mem_end = end
1587 };
Gerd Hoffmann9c704342014-02-19 11:40:17 +01001588
1589 if (modenr >= d->modes->n_modes) {
1590 qxl_set_guest_bug(d, "mode number out of range");
1591 return;
1592 }
1593
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001594 QXLSurfaceCreate surface = {
1595 .width = mode->x_res,
1596 .height = mode->y_res,
1597 .stride = -mode->x_res * 4,
1598 .format = SPICE_SURFACE_FMT_32_xRGB,
1599 .flags = loadvm ? QXL_SURF_FLAG_KEEP_DATA : 0,
1600 .mouse_mode = true,
1601 .mem = devmem + d->shadow_rom.draw_area_offset,
1602 };
1603
Alon Levyc480bb72012-03-18 13:46:14 +01001604 trace_qxl_set_mode(d->id, modenr, mode->x_res, mode->y_res, mode->bits,
1605 devmem);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001606 if (!loadvm) {
1607 qxl_hard_reset(d, 0);
1608 }
1609
1610 d->guest_slots[0].slot = slot;
Alon Levye954ea22012-04-25 12:13:24 +03001611 assert(qxl_add_memslot(d, 0, devmem, QXL_SYNC) == 0);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001612
1613 d->guest_primary.surface = surface;
Alon Levy5ff4e362011-07-20 12:20:58 +03001614 qxl_create_guest_primary(d, 0, QXL_SYNC);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001615
1616 d->mode = QXL_MODE_COMPAT;
1617 d->cmdflags = QXL_COMMAND_FLAG_COMPAT;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001618 if (mode->bits == 16) {
1619 d->cmdflags |= QXL_COMMAND_FLAG_COMPAT_16BPP;
1620 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001621 d->shadow_rom.mode = cpu_to_le32(modenr);
1622 d->rom->mode = cpu_to_le32(modenr);
1623 qxl_rom_set_dirty(d);
1624}
1625
Avi Kivitya8170e52012-10-23 12:30:10 +02001626static void ioport_write(void *opaque, hwaddr addr,
Avi Kivityb1950432011-08-08 16:08:57 +03001627 uint64_t val, unsigned size)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001628{
1629 PCIQXLDevice *d = opaque;
Avi Kivityb1950432011-08-08 16:08:57 +03001630 uint32_t io_port = addr;
Alon Levy5ff4e362011-07-20 12:20:58 +03001631 qxl_async_io async = QXL_SYNC;
Philippe Mathieu-Daudé380e6d82020-02-15 17:15:56 +01001632 uint32_t orig_io_port;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001633
Alon Levyd96aafc2012-10-02 11:39:14 +02001634 if (d->guest_bug && io_port != QXL_IO_RESET) {
Alon Levy087e6a42012-05-24 19:18:54 +03001635 return;
1636 }
1637
Alon Levy020af1c2012-08-22 11:16:25 +03001638 if (d->revision <= QXL_REVISION_STABLE_V10 &&
Gerd Hoffmannffe01e52012-09-25 13:56:40 +02001639 io_port > QXL_IO_FLUSH_RELEASE) {
Alon Levy020af1c2012-08-22 11:16:25 +03001640 qxl_set_guest_bug(d, "unsupported io %d for revision %d\n",
1641 io_port, d->revision);
1642 return;
1643 }
1644
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001645 switch (io_port) {
1646 case QXL_IO_RESET:
1647 case QXL_IO_SET_MODE:
1648 case QXL_IO_MEMSLOT_ADD:
1649 case QXL_IO_MEMSLOT_DEL:
1650 case QXL_IO_CREATE_PRIMARY:
Gerd Hoffmann81144d12011-06-24 12:23:44 +02001651 case QXL_IO_UPDATE_IRQ:
Alon Levya3d14052011-06-29 13:57:11 +02001652 case QXL_IO_LOG:
Alon Levy5ff4e362011-07-20 12:20:58 +03001653 case QXL_IO_MEMSLOT_ADD_ASYNC:
1654 case QXL_IO_CREATE_PRIMARY_ASYNC:
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001655 break;
1656 default:
Alon Levye21a2982011-07-20 12:20:57 +03001657 if (d->mode != QXL_MODE_VGA) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001658 break;
Alon Levye21a2982011-07-20 12:20:57 +03001659 }
Alon Levyc480bb72012-03-18 13:46:14 +01001660 trace_qxl_io_unexpected_vga_mode(d->id,
Alon Levy917ae082012-09-12 16:13:26 +03001661 addr, val, io_port_to_string(io_port));
Alon Levy5ff4e362011-07-20 12:20:58 +03001662 /* be nice to buggy guest drivers */
1663 if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&
Alon Levy020af1c2012-08-22 11:16:25 +03001664 io_port < QXL_IO_RANGE_SIZE) {
Alon Levy5ff4e362011-07-20 12:20:58 +03001665 qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
1666 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001667 return;
1668 }
1669
Alon Levy5ff4e362011-07-20 12:20:58 +03001670 /* we change the io_port to avoid ifdeffery in the main switch */
1671 orig_io_port = io_port;
1672 switch (io_port) {
1673 case QXL_IO_UPDATE_AREA_ASYNC:
1674 io_port = QXL_IO_UPDATE_AREA;
1675 goto async_common;
1676 case QXL_IO_MEMSLOT_ADD_ASYNC:
1677 io_port = QXL_IO_MEMSLOT_ADD;
1678 goto async_common;
1679 case QXL_IO_CREATE_PRIMARY_ASYNC:
1680 io_port = QXL_IO_CREATE_PRIMARY;
1681 goto async_common;
1682 case QXL_IO_DESTROY_PRIMARY_ASYNC:
1683 io_port = QXL_IO_DESTROY_PRIMARY;
1684 goto async_common;
1685 case QXL_IO_DESTROY_SURFACE_ASYNC:
1686 io_port = QXL_IO_DESTROY_SURFACE_WAIT;
1687 goto async_common;
1688 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
1689 io_port = QXL_IO_DESTROY_ALL_SURFACES;
Alon Levy3e16b9c2011-07-20 12:20:59 +03001690 goto async_common;
1691 case QXL_IO_FLUSH_SURFACES_ASYNC:
Alon Levy020af1c2012-08-22 11:16:25 +03001692 case QXL_IO_MONITORS_CONFIG_ASYNC:
Alon Levy5ff4e362011-07-20 12:20:58 +03001693async_common:
1694 async = QXL_ASYNC;
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001695 WITH_QEMU_LOCK_GUARD(&d->async_lock) {
1696 if (d->current_async != QXL_UNDEFINED_IO) {
1697 qxl_set_guest_bug(d, "%d async started before last (%d) complete",
1698 io_port, d->current_async);
1699 return;
1700 }
1701 d->current_async = orig_io_port;
Alon Levy5ff4e362011-07-20 12:20:58 +03001702 }
Alon Levy5ff4e362011-07-20 12:20:58 +03001703 break;
1704 default:
1705 break;
1706 }
Gerd Hoffmann18b20382013-09-05 17:30:05 +02001707 trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode),
1708 addr, io_port_to_string(addr),
1709 val, size, async);
Alon Levy5ff4e362011-07-20 12:20:58 +03001710
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001711 switch (io_port) {
1712 case QXL_IO_UPDATE_AREA:
1713 {
Alon Levy81fb6f12012-02-24 23:19:31 +02001714 QXLCookie *cookie = NULL;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001715 QXLRect update = d->ram->update_area;
Alon Levy81fb6f12012-02-24 23:19:31 +02001716
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02001717 if (d->ram->update_surface > d->ssd.num_surfaces) {
Alon Levy511b13e2012-08-21 13:51:31 +03001718 qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
1719 d->ram->update_surface);
Michael Tokarev36a03e02012-09-19 17:41:26 +04001720 break;
Alon Levy511b13e2012-08-21 13:51:31 +03001721 }
Michael Tokarev36a03e02012-09-19 17:41:26 +04001722 if (update.left >= update.right || update.top >= update.bottom ||
1723 update.left < 0 || update.top < 0) {
Alon Levy511b13e2012-08-21 13:51:31 +03001724 qxl_set_guest_bug(d,
1725 "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
1726 update.left, update.top, update.right, update.bottom);
Marc-André Lureau9e5a25f2014-10-10 19:05:11 +02001727 if (update.left == update.right || update.top == update.bottom) {
1728 /* old drivers may provide empty area, keep going */
1729 qxl_clear_guest_bug(d);
1730 goto cancel_async;
1731 }
Dunrong Huangccc29602012-08-31 00:44:44 +08001732 break;
1733 }
Alon Levy81fb6f12012-02-24 23:19:31 +02001734 if (async == QXL_ASYNC) {
1735 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
1736 QXL_IO_UPDATE_AREA_ASYNC);
1737 cookie->u.area = update;
1738 }
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +03001739 qxl_spice_update_area(d, d->ram->update_surface,
Alon Levy81fb6f12012-02-24 23:19:31 +02001740 cookie ? &cookie->u.area : &update,
1741 NULL, 0, 0, async, cookie);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001742 break;
1743 }
1744 case QXL_IO_NOTIFY_CMD:
Gerd Hoffmann5c59d112011-07-20 12:20:50 +03001745 qemu_spice_wakeup(&d->ssd);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001746 break;
1747 case QXL_IO_NOTIFY_CURSOR:
Gerd Hoffmann5c59d112011-07-20 12:20:50 +03001748 qemu_spice_wakeup(&d->ssd);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001749 break;
1750 case QXL_IO_UPDATE_IRQ:
Yonit Halperin40010ae2011-09-05 08:45:59 +03001751 qxl_update_irq(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001752 break;
1753 case QXL_IO_NOTIFY_OOM:
1754 if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
1755 break;
1756 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001757 d->oom_running = 1;
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +03001758 qxl_spice_oom(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001759 d->oom_running = 0;
1760 break;
1761 case QXL_IO_SET_MODE:
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001762 qxl_set_mode(d, val, 0);
1763 break;
1764 case QXL_IO_LOG:
Peter Maydelld4aceb22020-01-20 15:11:42 +00001765 if (trace_event_get_state_backends(TRACE_QXL_IO_LOG) || d->guestdebug) {
Daniel P. Berrangé00f42692019-01-23 12:00:13 +00001766 /* We cannot trust the guest to NUL terminate d->ram->log_buf */
1767 char *log_buf = g_strndup((const char *)d->ram->log_buf,
1768 sizeof(d->ram->log_buf));
1769 trace_qxl_io_log(d->id, log_buf);
1770 if (d->guestdebug) {
1771 fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,
1772 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), log_buf);
1773 }
1774 g_free(log_buf);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001775 }
1776 break;
1777 case QXL_IO_RESET:
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001778 qxl_hard_reset(d, 0);
1779 break;
1780 case QXL_IO_MEMSLOT_ADD:
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001781 if (val >= NUM_MEMSLOTS) {
Alon Levy0a530542012-05-24 12:38:12 +03001782 qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001783 break;
1784 }
1785 if (d->guest_slots[val].active) {
Alon Levy0a530542012-05-24 12:38:12 +03001786 qxl_set_guest_bug(d,
1787 "QXL_IO_MEMSLOT_ADD: memory slot already active");
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001788 break;
1789 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001790 d->guest_slots[val].slot = d->ram->mem_slot;
Alon Levy5ff4e362011-07-20 12:20:58 +03001791 qxl_add_memslot(d, val, 0, async);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001792 break;
1793 case QXL_IO_MEMSLOT_DEL:
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001794 if (val >= NUM_MEMSLOTS) {
Alon Levy0a530542012-05-24 12:38:12 +03001795 qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001796 break;
1797 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001798 qxl_del_memslot(d, val);
1799 break;
1800 case QXL_IO_CREATE_PRIMARY:
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001801 if (val != 0) {
Alon Levy0a530542012-05-24 12:38:12 +03001802 qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
Alon Levy5ff4e362011-07-20 12:20:58 +03001803 async);
1804 goto cancel_async;
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001805 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001806 d->guest_primary.surface = d->ram->create_surface;
Alon Levy5ff4e362011-07-20 12:20:58 +03001807 qxl_create_guest_primary(d, 0, async);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001808 break;
1809 case QXL_IO_DESTROY_PRIMARY:
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001810 if (val != 0) {
Alon Levy0a530542012-05-24 12:38:12 +03001811 qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
Alon Levy5ff4e362011-07-20 12:20:58 +03001812 async);
1813 goto cancel_async;
Gerd Hoffmann2bce0402011-07-20 12:20:55 +03001814 }
Alon Levy5ff4e362011-07-20 12:20:58 +03001815 if (!qxl_destroy_primary(d, async)) {
Alon Levyc480bb72012-03-18 13:46:14 +01001816 trace_qxl_io_destroy_primary_ignored(d->id,
1817 qxl_mode_to_string(d->mode));
Alon Levy5ff4e362011-07-20 12:20:58 +03001818 goto cancel_async;
1819 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001820 break;
1821 case QXL_IO_DESTROY_SURFACE_WAIT:
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02001822 if (val >= d->ssd.num_surfaces) {
Alon Levy0a530542012-05-24 12:38:12 +03001823 qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
Stefan Weil5f8daf22012-04-01 22:07:30 +02001824 "%" PRIu64 " >= NUM_SURFACES", async, val);
Alon Levy5ff4e362011-07-20 12:20:58 +03001825 goto cancel_async;
1826 }
1827 qxl_spice_destroy_surface_wait(d, val, async);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001828 break;
Alon Levy3e16b9c2011-07-20 12:20:59 +03001829 case QXL_IO_FLUSH_RELEASE: {
1830 QXLReleaseRing *ring = &d->ram->release_ring;
1831 if (ring->prod - ring->cons + 1 == ring->num_items) {
1832 fprintf(stderr,
1833 "ERROR: no flush, full release ring [p%d,%dc]\n",
1834 ring->prod, ring->cons);
1835 }
1836 qxl_push_free_res(d, 1 /* flush */);
Alon Levy3e16b9c2011-07-20 12:20:59 +03001837 break;
1838 }
1839 case QXL_IO_FLUSH_SURFACES_ASYNC:
Alon Levy3e16b9c2011-07-20 12:20:59 +03001840 qxl_spice_flush_surfaces_async(d);
1841 break;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001842 case QXL_IO_DESTROY_ALL_SURFACES:
Alon Levy5ff4e362011-07-20 12:20:58 +03001843 d->mode = QXL_MODE_UNDEFINED;
1844 qxl_spice_destroy_surfaces(d, async);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001845 break;
Alon Levy020af1c2012-08-22 11:16:25 +03001846 case QXL_IO_MONITORS_CONFIG_ASYNC:
1847 qxl_spice_monitors_config_async(d, 0);
1848 break;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001849 default:
Alon Levy0a530542012-05-24 12:38:12 +03001850 qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001851 }
Alon Levy5ff4e362011-07-20 12:20:58 +03001852 return;
1853cancel_async:
Alon Levy5ff4e362011-07-20 12:20:58 +03001854 if (async) {
1855 qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
1856 qemu_mutex_lock(&d->async_lock);
1857 d->current_async = QXL_UNDEFINED_IO;
1858 qemu_mutex_unlock(&d->async_lock);
1859 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001860}
1861
Avi Kivitya8170e52012-10-23 12:30:10 +02001862static uint64_t ioport_read(void *opaque, hwaddr addr,
Avi Kivityb1950432011-08-08 16:08:57 +03001863 unsigned size)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001864{
Alon Levy917ae082012-09-12 16:13:26 +03001865 PCIQXLDevice *qxl = opaque;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001866
Alon Levy917ae082012-09-12 16:13:26 +03001867 trace_qxl_io_read_unexpected(qxl->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001868 return 0xff;
1869}
1870
Avi Kivityb1950432011-08-08 16:08:57 +03001871static const MemoryRegionOps qxl_io_ops = {
1872 .read = ioport_read,
1873 .write = ioport_write,
1874 .valid = {
1875 .min_access_size = 1,
1876 .max_access_size = 1,
1877 },
1878};
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001879
Gerd Hoffmann4a46c992013-10-29 13:29:43 +01001880static void qxl_update_irq_bh(void *opaque)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001881{
1882 PCIQXLDevice *d = opaque;
Yonit Halperin40010ae2011-09-05 08:45:59 +03001883 qxl_update_irq(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001884}
1885
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001886static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
1887{
1888 uint32_t old_pending;
1889 uint32_t le_events = cpu_to_le32(events);
1890
Alon Levy917ae082012-09-12 16:13:26 +03001891 trace_qxl_send_events(d->id, events);
Alon Levy511aefb2012-11-01 14:56:00 +02001892 if (!qemu_spice_display_is_running(&d->ssd)) {
1893 /* spice-server tracks guest running state and should not do this */
1894 fprintf(stderr, "%s: spice-server bug: guest stopped, ignoring\n",
1895 __func__);
1896 trace_qxl_send_events_vm_stopped(d->id, events);
1897 return;
1898 }
Peter Maydell5a358b32018-09-27 16:55:38 +01001899 /*
1900 * Older versions of Spice forgot to define the QXLRam struct
1901 * with the '__aligned__(4)' attribute. clang 7 and newer will
1902 * thus warn that atomic_fetch_or(&d->ram->int_pending, ...)
1903 * might be a misaligned atomic access, and will generate an
1904 * out-of-line call for it, which results in a link error since
1905 * we don't currently link against libatomic.
1906 *
1907 * In fact we set up d->ram in init_qxl_ram() so it always starts
1908 * at a 4K boundary, so we know that &d->ram->int_pending is
1909 * naturally aligned for a uint32_t. Newer Spice versions
1910 * (with Spice commit beda5ec7a6848be20c0cac2a9a8ef2a41e8069c1)
1911 * will fix the bug directly. To deal with older versions,
1912 * we tell the compiler to assume the address really is aligned.
1913 * Any compiler which cares about the misalignment will have
1914 * __builtin_assume_aligned.
1915 */
1916#ifdef HAS_ASSUME_ALIGNED
1917#define ALIGNED_UINT32_PTR(P) ((uint32_t *)__builtin_assume_aligned(P, 4))
1918#else
1919#define ALIGNED_UINT32_PTR(P) ((uint32_t *)P)
1920#endif
1921
1922 old_pending = atomic_fetch_or(ALIGNED_UINT32_PTR(&d->ram->int_pending),
1923 le_events);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001924 if ((old_pending & le_events) == le_events) {
1925 return;
1926 }
Gerd Hoffmann4a46c992013-10-29 13:29:43 +01001927 qemu_bh_schedule(d->update_irq);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001928}
1929
1930/* graphics console */
1931
1932static void qxl_hw_update(void *opaque)
1933{
1934 PCIQXLDevice *qxl = opaque;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001935
Gerd Hoffmann15162332014-09-24 17:05:45 +02001936 qxl_render_update(qxl);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001937}
1938
Gerd Hoffmann1331eab2016-06-22 14:07:24 +02001939static void qxl_dirty_one_surface(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
1940 uint32_t height, int32_t stride)
1941{
Gerd Hoffmanne0127d22016-07-13 14:33:06 +02001942 uint64_t offset, size;
1943 uint32_t slot;
Gerd Hoffmann1331eab2016-06-22 14:07:24 +02001944 bool rc;
1945
1946 rc = qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset);
1947 assert(rc == true);
Gerd Hoffmanne0127d22016-07-13 14:33:06 +02001948 size = (uint64_t)height * abs(stride);
1949 trace_qxl_surfaces_dirty(qxl->id, offset, size);
Gerd Hoffmann1331eab2016-06-22 14:07:24 +02001950 qxl_set_dirty(qxl->guest_slots[slot].mr,
Gerd Hoffmanne0127d22016-07-13 14:33:06 +02001951 qxl->guest_slots[slot].offset + offset,
1952 qxl->guest_slots[slot].offset + offset + size);
Gerd Hoffmann1331eab2016-06-22 14:07:24 +02001953}
1954
Yonit Halperine25139b2012-02-15 11:22:15 +02001955static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
1956{
Yonit Halperine25139b2012-02-15 11:22:15 +02001957 int i;
1958
Yonit Halperin2aa9e852012-02-15 11:22:16 +02001959 if (qxl->mode != QXL_MODE_NATIVE && qxl->mode != QXL_MODE_COMPAT) {
Yonit Halperine25139b2012-02-15 11:22:15 +02001960 return;
1961 }
1962
1963 /* dirty the primary surface */
Gerd Hoffmann1331eab2016-06-22 14:07:24 +02001964 qxl_dirty_one_surface(qxl, qxl->guest_primary.surface.mem,
1965 qxl->guest_primary.surface.height,
1966 qxl->guest_primary.surface.stride);
Yonit Halperine25139b2012-02-15 11:22:15 +02001967
1968 /* dirty the off-screen surfaces */
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02001969 for (i = 0; i < qxl->ssd.num_surfaces; i++) {
Yonit Halperine25139b2012-02-15 11:22:15 +02001970 QXLSurfaceCmd *cmd;
Yonit Halperine25139b2012-02-15 11:22:15 +02001971
1972 if (qxl->guest_surfaces.cmds[i] == 0) {
1973 continue;
1974 }
1975
1976 cmd = qxl_phys2virt(qxl, qxl->guest_surfaces.cmds[i],
1977 MEMSLOT_GROUP_GUEST);
Alon Levyfae2afb2012-04-25 12:13:18 +03001978 assert(cmd);
Yonit Halperine25139b2012-02-15 11:22:15 +02001979 assert(cmd->type == QXL_SURFACE_CMD_CREATE);
Gerd Hoffmann1331eab2016-06-22 14:07:24 +02001980 qxl_dirty_one_surface(qxl, cmd->u.surface_create.data,
1981 cmd->u.surface_create.height,
1982 cmd->u.surface_create.stride);
Yonit Halperine25139b2012-02-15 11:22:15 +02001983 }
1984}
1985
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001986static void qxl_vm_change_state_handler(void *opaque, int running,
1987 RunState state)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001988{
1989 PCIQXLDevice *qxl = opaque;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001990
Yonit Halperinefbf2952011-09-05 08:45:58 +03001991 if (running) {
1992 /*
1993 * if qxl_send_events was called from spice server context before
Yonit Halperin40010ae2011-09-05 08:45:59 +03001994 * migration ended, qxl_update_irq for these events might not have been
Yonit Halperinefbf2952011-09-05 08:45:58 +03001995 * called
1996 */
Yonit Halperin40010ae2011-09-05 08:45:59 +03001997 qxl_update_irq(qxl);
Yonit Halperine25139b2012-02-15 11:22:15 +02001998 } else {
1999 /* make sure surfaces are saved before migration */
2000 qxl_dirty_surfaces(qxl);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002001 }
2002}
2003
2004/* display change listener */
2005
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002006static void display_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002007 int x, int y, int w, int h)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002008{
Gerd Hoffmannc6c06852013-02-28 12:15:00 +01002009 PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
2010
2011 if (qxl->mode == QXL_MODE_VGA) {
2012 qemu_spice_display_update(&qxl->ssd, x, y, w, h);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002013 }
2014}
2015
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01002016static void display_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01002017 struct DisplaySurface *surface)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002018{
Gerd Hoffmannc6c06852013-02-28 12:15:00 +01002019 PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
2020
Gerd Hoffmann71874c12013-02-28 16:42:28 +01002021 qxl->ssd.ds = surface;
Gerd Hoffmannc6c06852013-02-28 12:15:00 +01002022 if (qxl->mode == QXL_MODE_VGA) {
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01002023 qemu_spice_display_switch(&qxl->ssd, surface);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002024 }
2025}
2026
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01002027static void display_refresh(DisplayChangeListener *dcl)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002028{
Gerd Hoffmannc6c06852013-02-28 12:15:00 +01002029 PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
2030
2031 if (qxl->mode == QXL_MODE_VGA) {
2032 qemu_spice_display_refresh(&qxl->ssd);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002033 }
2034}
2035
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002036static DisplayChangeListenerOps display_listener_ops = {
2037 .dpy_name = "spice/qxl",
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +02002038 .dpy_gfx_update = display_update,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01002039 .dpy_gfx_switch = display_switch,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002040 .dpy_refresh = display_refresh,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002041};
2042
Alon Levy13d1fd42012-06-10 18:05:06 +03002043static void qxl_init_ramsize(PCIQXLDevice *qxl)
Gerd Hoffmanna9741922012-02-17 15:02:40 +01002044{
Alon Levy13d1fd42012-06-10 18:05:06 +03002045 /* vga mode framebuffer / primary surface (bar 0, first part) */
2046 if (qxl->vgamem_size_mb < 8) {
2047 qxl->vgamem_size_mb = 8;
2048 }
Radim Krčmář876d5162015-02-17 17:30:51 +01002049 /* XXX: we round vgamem_size_mb up to a nearest power of two and it must be
2050 * less than vga_common_init()'s maximum on qxl->vga.vram_size (512 now).
2051 */
2052 if (qxl->vgamem_size_mb > 256) {
2053 qxl->vgamem_size_mb = 256;
2054 }
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002055 qxl->vgamem_size = qxl->vgamem_size_mb * MiB;
Alon Levy13d1fd42012-06-10 18:05:06 +03002056
2057 /* vga ram (bar 0, total) */
Gerd Hoffmann017438e2012-02-17 15:03:24 +01002058 if (qxl->ram_size_mb != -1) {
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002059 qxl->vga.vram_size = qxl->ram_size_mb * MiB;
Gerd Hoffmann017438e2012-02-17 15:03:24 +01002060 }
Alon Levy13d1fd42012-06-10 18:05:06 +03002061 if (qxl->vga.vram_size < qxl->vgamem_size * 2) {
2062 qxl->vga.vram_size = qxl->vgamem_size * 2;
Gerd Hoffmanna9741922012-02-17 15:02:40 +01002063 }
2064
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02002065 /* vram32 (surfaces, 32bit, bar 1) */
2066 if (qxl->vram32_size_mb != -1) {
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002067 qxl->vram32_size = qxl->vram32_size_mb * MiB;
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02002068 }
2069 if (qxl->vram32_size < 4096) {
2070 qxl->vram32_size = 4096;
2071 }
2072
2073 /* vram (surfaces, 64bit, bar 4+5) */
Gerd Hoffmann017438e2012-02-17 15:03:24 +01002074 if (qxl->vram_size_mb != -1) {
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002075 qxl->vram_size = (uint64_t)qxl->vram_size_mb * MiB;
Gerd Hoffmann017438e2012-02-17 15:03:24 +01002076 }
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02002077 if (qxl->vram_size < qxl->vram32_size) {
2078 qxl->vram_size = qxl->vram32_size;
Gerd Hoffmanna9741922012-02-17 15:02:40 +01002079 }
2080
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02002081 if (qxl->revision == 1) {
2082 qxl->vram32_size = 4096;
2083 qxl->vram_size = 4096;
2084 }
Radim Krčmářbb7443f2015-02-17 17:30:52 +01002085 qxl->vgamem_size = pow2ceil(qxl->vgamem_size);
2086 qxl->vga.vram_size = pow2ceil(qxl->vga.vram_size);
2087 qxl->vram32_size = pow2ceil(qxl->vram32_size);
2088 qxl->vram_size = pow2ceil(qxl->vram_size);
Gerd Hoffmanna9741922012-02-17 15:02:40 +01002089}
2090
Markus Armbruster042a24d2015-01-19 15:52:36 +01002091static void qxl_realize_common(PCIQXLDevice *qxl, Error **errp)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002092{
2093 uint8_t* config = qxl->pci.config;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002094 uint32_t pci_device_rev;
2095 uint32_t io_size;
2096
Paolo Bonzini47025a02017-08-15 01:15:52 +02002097 qemu_spice_display_init_common(&qxl->ssd);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002098 qxl->mode = QXL_MODE_UNDEFINED;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002099 qxl->num_memslots = NUM_MEMSLOTS;
Gerd Hoffmann14898cf2011-07-20 12:20:53 +03002100 qemu_mutex_init(&qxl->track_lock);
Alon Levy5ff4e362011-07-20 12:20:58 +03002101 qemu_mutex_init(&qxl->async_lock);
2102 qxl->current_async = QXL_UNDEFINED_IO;
Alon Levy087e6a42012-05-24 19:18:54 +03002103 qxl->guest_bug = 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002104
2105 switch (qxl->revision) {
2106 case 1: /* spice 0.4 -- qxl-1 */
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002107 pci_device_rev = QXL_REVISION_STABLE_V04;
Uri Lublin3f6297b2012-05-10 16:24:53 +03002108 io_size = 8;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002109 break;
2110 case 2: /* spice 0.6 -- qxl-2 */
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002111 pci_device_rev = QXL_REVISION_STABLE_V06;
Uri Lublin3f6297b2012-05-10 16:24:53 +03002112 io_size = 16;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002113 break;
Gerd Hoffmann9197a7c2011-07-20 12:21:00 +03002114 case 3: /* qxl-3 */
Alon Levy020af1c2012-08-22 11:16:25 +03002115 pci_device_rev = QXL_REVISION_STABLE_V10;
2116 io_size = 32; /* PCI region size must be pow2 */
2117 break;
Alon Levy020af1c2012-08-22 11:16:25 +03002118 case 4: /* qxl-4 */
2119 pci_device_rev = QXL_REVISION_STABLE_V12;
Radim Krčmářbb7443f2015-02-17 17:30:52 +01002120 io_size = pow2ceil(QXL_IO_RANGE_SIZE);
Gerd Hoffmann9197a7c2011-07-20 12:21:00 +03002121 break;
Gerd Hoffmanned71c092020-02-06 08:43:58 +01002122 case 5: /* qxl-5 */
2123 pci_device_rev = QXL_REVISION_STABLE_V12 + 1;
2124 io_size = pow2ceil(QXL_IO_RANGE_SIZE);
2125 break;
Alon Levy36839d32012-08-21 13:51:32 +03002126 default:
Markus Armbruster042a24d2015-01-19 15:52:36 +01002127 error_setg(errp, "Invalid revision %d for qxl device (max %d)",
2128 qxl->revision, QXL_DEFAULT_REVISION);
2129 return;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002130 }
2131
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002132 pci_set_byte(&config[PCI_REVISION_ID], pci_device_rev);
2133 pci_set_byte(&config[PCI_INTERRUPT_PIN], 1);
2134
2135 qxl->rom_size = qxl_rom_size();
Gerd Hoffmann44b5c1e2020-02-25 06:59:19 +01002136 memory_region_init_rom(&qxl->rom_bar, OBJECT(qxl), "qxl.vrom",
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +02002137 qxl->rom_size, &error_fatal);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002138 init_qxl_rom(qxl);
2139 init_qxl_ram(qxl);
2140
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02002141 qxl->guest_surfaces.cmds = g_new0(QXLPHYSICAL, qxl->ssd.num_surfaces);
Peter Maydellce66d772017-07-07 15:42:56 +01002142 memory_region_init_ram(&qxl->vram_bar, OBJECT(qxl), "qxl.vram",
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +02002143 qxl->vram_size, &error_fatal);
Paolo Bonzini3eadad52013-06-06 21:25:08 -04002144 memory_region_init_alias(&qxl->vram32_bar, OBJECT(qxl), "qxl.vram32",
2145 &qxl->vram_bar, 0, qxl->vram32_size);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002146
Paolo Bonzini3eadad52013-06-06 21:25:08 -04002147 memory_region_init_io(&qxl->io_bar, OBJECT(qxl), &qxl_io_ops, qxl,
Avi Kivityb1950432011-08-08 16:08:57 +03002148 "qxl-ioports", io_size);
Gerd Hoffmann60e94e42018-10-12 13:45:40 +02002149 if (qxl->have_vga) {
Avi Kivityb1950432011-08-08 16:08:57 +03002150 vga_dirty_log_start(&qxl->vga);
2151 }
Jan Kiszkabd8f2f52012-08-23 13:02:33 +02002152 memory_region_set_flush_coalesced(&qxl->io_bar);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002153
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002154
Avi Kivitye824b2c2011-08-08 16:09:31 +03002155 pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX,
2156 PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002157
Avi Kivitye824b2c2011-08-08 16:09:31 +03002158 pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX,
2159 PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar);
Avi Kivityb1950432011-08-08 16:08:57 +03002160
Avi Kivitye824b2c2011-08-08 16:09:31 +03002161 pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX,
2162 PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram);
Avi Kivityb1950432011-08-08 16:08:57 +03002163
Avi Kivitye824b2c2011-08-08 16:09:31 +03002164 pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX,
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02002165 PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vram32_bar);
2166
2167 if (qxl->vram32_size < qxl->vram_size) {
2168 /*
2169 * Make the 64bit vram bar show up only in case it is
2170 * configured to be larger than the 32bit vram bar.
2171 */
2172 pci_register_bar(&qxl->pci, QXL_VRAM64_RANGE_INDEX,
2173 PCI_BASE_ADDRESS_SPACE_MEMORY |
2174 PCI_BASE_ADDRESS_MEM_TYPE_64 |
2175 PCI_BASE_ADDRESS_MEM_PREFETCH,
2176 &qxl->vram_bar);
2177 }
2178
2179 /* print pci bar details */
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002180 dprint(qxl, 1, "ram/%s: %" PRId64 " MB [region 0]\n",
Gerd Hoffmann60e94e42018-10-12 13:45:40 +02002181 qxl->have_vga ? "pri" : "sec", qxl->vga.vram_size / MiB);
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002182 dprint(qxl, 1, "vram/32: %" PRIx64 " MB [region 1]\n",
2183 qxl->vram32_size / MiB);
2184 dprint(qxl, 1, "vram/64: %" PRIx64 " MB %s\n",
2185 qxl->vram_size / MiB,
Gerd Hoffmann6f2b1752011-10-14 18:05:48 +02002186 qxl->vram32_size < qxl->vram_size ? "[region 4]" : "[unmapped]");
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002187
2188 qxl->ssd.qxl.base.sif = &qxl_interface.base;
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02002189 if (qemu_spice_add_display_interface(&qxl->ssd.qxl, qxl->vga.con) != 0) {
Markus Armbruster042a24d2015-01-19 15:52:36 +01002190 error_setg(errp, "qxl interface %d.%d not supported by spice-server",
2191 SPICE_INTERFACE_QXL_MAJOR, SPICE_INTERFACE_QXL_MINOR);
2192 return;
Alon Levye25a0652012-10-03 20:13:58 +02002193 }
Lukáš Hrázkýbe812c02019-02-15 16:09:19 +01002194
2195#if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
2196 char device_address[256] = "";
2197 if (qemu_spice_fill_device_address(qxl->vga.con, device_address, 256)) {
2198 spice_qxl_set_device_info(&qxl->ssd.qxl,
2199 device_address,
2200 0,
2201 qxl->max_outputs);
2202 }
2203#endif
2204
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002205 qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl);
2206
Gerd Hoffmann4a46c992013-10-29 13:29:43 +01002207 qxl->update_irq = qemu_bh_new(qxl_update_irq_bh, qxl);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002208 qxl_reset_state(qxl);
2209
Alon Levy81fb6f12012-02-24 23:19:31 +02002210 qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);
Gerd Hoffmann0b2824e2014-11-04 13:59:59 +01002211 qxl->ssd.cursor_bh = qemu_bh_new(qemu_spice_cursor_refresh_bh, &qxl->ssd);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002212}
2213
Markus Armbruster042a24d2015-01-19 15:52:36 +01002214static void qxl_realize_primary(PCIDevice *dev, Error **errp)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002215{
Gongleic69f6c72015-05-12 17:27:10 +08002216 PCIQXLDevice *qxl = PCI_QXL(dev);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002217 VGACommonState *vga = &qxl->vga;
Markus Armbruster042a24d2015-01-19 15:52:36 +01002218 Error *local_err = NULL;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002219
Alon Levy13d1fd42012-06-10 18:05:06 +03002220 qxl_init_ramsize(qxl);
Gerd Hoffmann54a85d42014-08-26 14:16:30 +02002221 vga->vbe_size = qxl->vgamem_size;
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002222 vga->vram_size_mb = qxl->vga.vram_size / MiB;
Gerd Hoffmann1fcfdc42018-07-02 18:33:44 +02002223 vga_common_init(vga, OBJECT(dev));
Paolo Bonzini712f0cc2013-06-06 21:21:13 -04002224 vga_init(vga, OBJECT(dev),
2225 pci_address_space(dev), pci_address_space_io(dev), false);
Kirill Batuzov848696b2014-04-29 17:38:39 +04002226 portio_list_init(&qxl->vga_port_list, OBJECT(dev), qxl_vga_portio_list,
Paolo Bonzinidb10ca92013-06-06 21:19:53 -04002227 vga, "vga");
Kirill Batuzov848696b2014-04-29 17:38:39 +04002228 portio_list_set_flush_coalesced(&qxl->vga_port_list);
2229 portio_list_add(&qxl->vga_port_list, pci_address_space_io(dev), 0x3b0);
Gerd Hoffmann60e94e42018-10-12 13:45:40 +02002230 qxl->have_vga = true;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002231
Gerd Hoffmann56437062014-01-24 15:35:21 +01002232 vga->con = graphic_console_init(DEVICE(dev), 0, &qxl_ops, qxl);
Gerd Hoffmann60e94e42018-10-12 13:45:40 +02002233 qxl->id = qemu_console_get_index(vga->con); /* == channel_id */
2234 if (qxl->id != 0) {
2235 error_setg(errp, "primary qxl-vga device must be console 0 "
2236 "(first display device on the command line)");
2237 return;
2238 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002239
Markus Armbruster042a24d2015-01-19 15:52:36 +01002240 qxl_realize_common(qxl, &local_err);
2241 if (local_err) {
2242 error_propagate(errp, local_err);
2243 return;
Gerd Hoffmannbdd4df32012-11-02 09:37:27 +01002244 }
2245
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002246 qxl->ssd.dcl.ops = &display_listener_ops;
Gerd Hoffmann284d1c62013-03-15 15:45:54 +01002247 qxl->ssd.dcl.con = vga->con;
Gerd Hoffmann52090892013-04-23 15:44:31 +02002248 register_displaychangelistener(&qxl->ssd.dcl);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002249}
2250
Markus Armbruster042a24d2015-01-19 15:52:36 +01002251static void qxl_realize_secondary(PCIDevice *dev, Error **errp)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002252{
Gongleic69f6c72015-05-12 17:27:10 +08002253 PCIQXLDevice *qxl = PCI_QXL(dev);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002254
Alon Levy13d1fd42012-06-10 18:05:06 +03002255 qxl_init_ramsize(qxl);
Peter Maydellce66d772017-07-07 15:42:56 +01002256 memory_region_init_ram(&qxl->vga.vram, OBJECT(dev), "qxl.vgavram",
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +02002257 qxl->vga.vram_size, &error_fatal);
Avi Kivityb1950432011-08-08 16:08:57 +03002258 qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram);
Gerd Hoffmann56437062014-01-24 15:35:21 +01002259 qxl->vga.con = graphic_console_init(DEVICE(dev), 0, &qxl_ops, qxl);
Gerd Hoffmann60e94e42018-10-12 13:45:40 +02002260 qxl->id = qemu_console_get_index(qxl->vga.con); /* == channel_id */
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002261
Markus Armbruster042a24d2015-01-19 15:52:36 +01002262 qxl_realize_common(qxl, errp);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002263}
2264
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +01002265static int qxl_pre_save(void *opaque)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002266{
2267 PCIQXLDevice* d = opaque;
2268 uint8_t *ram_start = d->vga.vram_ptr;
2269
Alon Levyc480bb72012-03-18 13:46:14 +01002270 trace_qxl_pre_save(d->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002271 if (d->last_release == NULL) {
2272 d->last_release_offset = 0;
2273 } else {
2274 d->last_release_offset = (uint8_t *)d->last_release - ram_start;
2275 }
2276 assert(d->last_release_offset < d->vga.vram_size);
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +01002277
2278 return 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002279}
2280
2281static int qxl_pre_load(void *opaque)
2282{
2283 PCIQXLDevice* d = opaque;
2284
Alon Levyc480bb72012-03-18 13:46:14 +01002285 trace_qxl_pre_load(d->id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002286 qxl_hard_reset(d, 1);
2287 qxl_exit_vga_mode(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002288 return 0;
2289}
2290
Alon Levy54825d22011-10-25 15:39:50 +02002291static void qxl_create_memslots(PCIQXLDevice *d)
2292{
2293 int i;
2294
2295 for (i = 0; i < NUM_MEMSLOTS; i++) {
2296 if (!d->guest_slots[i].active) {
2297 continue;
2298 }
Alon Levy54825d22011-10-25 15:39:50 +02002299 qxl_add_memslot(d, i, 0, QXL_SYNC);
2300 }
2301}
2302
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002303static int qxl_post_load(void *opaque, int version)
2304{
2305 PCIQXLDevice* d = opaque;
2306 uint8_t *ram_start = d->vga.vram_ptr;
2307 QXLCommandExt *cmds;
Alon Levy54825d22011-10-25 15:39:50 +02002308 int in, out, newmode;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002309
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002310 assert(d->last_release_offset < d->vga.vram_size);
2311 if (d->last_release_offset == 0) {
2312 d->last_release = NULL;
2313 } else {
2314 d->last_release = (QXLReleaseInfo *)(ram_start + d->last_release_offset);
2315 }
2316
2317 d->modes = (QXLModes*)((uint8_t*)d->rom + d->rom->modes_offset);
2318
Alon Levyc480bb72012-03-18 13:46:14 +01002319 trace_qxl_post_load(d->id, qxl_mode_to_string(d->mode));
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002320 newmode = d->mode;
2321 d->mode = QXL_MODE_UNDEFINED;
Alon Levy54825d22011-10-25 15:39:50 +02002322
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002323 switch (newmode) {
2324 case QXL_MODE_UNDEFINED:
Yonit Halperinfa98efe2012-11-28 10:08:22 -05002325 qxl_create_memslots(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002326 break;
2327 case QXL_MODE_VGA:
Alon Levy54825d22011-10-25 15:39:50 +02002328 qxl_create_memslots(d);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002329 qxl_enter_vga_mode(d);
2330 break;
2331 case QXL_MODE_NATIVE:
Alon Levy54825d22011-10-25 15:39:50 +02002332 qxl_create_memslots(d);
Alon Levy5ff4e362011-07-20 12:20:58 +03002333 qxl_create_guest_primary(d, 1, QXL_SYNC);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002334
2335 /* replay surface-create and cursor-set commands */
Markus Armbruster9de68632015-10-29 16:55:21 +01002336 cmds = g_new0(QXLCommandExt, d->ssd.num_surfaces + 1);
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02002337 for (in = 0, out = 0; in < d->ssd.num_surfaces; in++) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002338 if (d->guest_surfaces.cmds[in] == 0) {
2339 continue;
2340 }
2341 cmds[out].cmd.data = d->guest_surfaces.cmds[in];
2342 cmds[out].cmd.type = QXL_CMD_SURFACE;
2343 cmds[out].group_id = MEMSLOT_GROUP_GUEST;
2344 out++;
2345 }
Yonit Halperin30f6da62011-10-18 18:58:54 +02002346 if (d->guest_cursor) {
2347 cmds[out].cmd.data = d->guest_cursor;
2348 cmds[out].cmd.type = QXL_CMD_CURSOR;
2349 cmds[out].group_id = MEMSLOT_GROUP_GUEST;
2350 out++;
2351 }
Gerd Hoffmannaee32bf2011-07-20 12:20:52 +03002352 qxl_spice_loadvm_commands(d, cmds, out);
Anthony Liguori7267c092011-08-20 22:09:37 -05002353 g_free(cmds);
Alon Levy020af1c2012-08-22 11:16:25 +03002354 if (d->guest_monitors_config) {
2355 qxl_spice_monitors_config_async(d, 1);
2356 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002357 break;
2358 case QXL_MODE_COMPAT:
Alon Levy54825d22011-10-25 15:39:50 +02002359 /* note: no need to call qxl_create_memslots, qxl_set_mode
2360 * creates the mem slot. */
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002361 qxl_set_mode(d, d->shadow_rom.mode, 1);
2362 break;
2363 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002364 return 0;
2365}
2366
Gerd Hoffmannb67737a2011-01-05 18:05:52 +01002367#define QXL_SAVE_VERSION 21
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002368
Alon Levy020af1c2012-08-22 11:16:25 +03002369static bool qxl_monitors_config_needed(void *opaque)
2370{
2371 PCIQXLDevice *qxl = opaque;
2372
2373 return qxl->guest_monitors_config != 0;
2374}
2375
2376
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002377static VMStateDescription qxl_memslot = {
2378 .name = "qxl-memslot",
2379 .version_id = QXL_SAVE_VERSION,
2380 .minimum_version_id = QXL_SAVE_VERSION,
2381 .fields = (VMStateField[]) {
2382 VMSTATE_UINT64(slot.mem_start, struct guest_slots),
2383 VMSTATE_UINT64(slot.mem_end, struct guest_slots),
2384 VMSTATE_UINT32(active, struct guest_slots),
2385 VMSTATE_END_OF_LIST()
2386 }
2387};
2388
2389static VMStateDescription qxl_surface = {
2390 .name = "qxl-surface",
2391 .version_id = QXL_SAVE_VERSION,
2392 .minimum_version_id = QXL_SAVE_VERSION,
2393 .fields = (VMStateField[]) {
2394 VMSTATE_UINT32(width, QXLSurfaceCreate),
2395 VMSTATE_UINT32(height, QXLSurfaceCreate),
2396 VMSTATE_INT32(stride, QXLSurfaceCreate),
2397 VMSTATE_UINT32(format, QXLSurfaceCreate),
2398 VMSTATE_UINT32(position, QXLSurfaceCreate),
2399 VMSTATE_UINT32(mouse_mode, QXLSurfaceCreate),
2400 VMSTATE_UINT32(flags, QXLSurfaceCreate),
2401 VMSTATE_UINT32(type, QXLSurfaceCreate),
2402 VMSTATE_UINT64(mem, QXLSurfaceCreate),
2403 VMSTATE_END_OF_LIST()
2404 }
2405};
2406
Alon Levy020af1c2012-08-22 11:16:25 +03002407static VMStateDescription qxl_vmstate_monitors_config = {
2408 .name = "qxl/monitors-config",
2409 .version_id = 1,
2410 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +02002411 .needed = qxl_monitors_config_needed,
Alon Levy020af1c2012-08-22 11:16:25 +03002412 .fields = (VMStateField[]) {
2413 VMSTATE_UINT64(guest_monitors_config, PCIQXLDevice),
2414 VMSTATE_END_OF_LIST()
2415 },
2416};
2417
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002418static VMStateDescription qxl_vmstate = {
2419 .name = "qxl",
2420 .version_id = QXL_SAVE_VERSION,
2421 .minimum_version_id = QXL_SAVE_VERSION,
2422 .pre_save = qxl_pre_save,
2423 .pre_load = qxl_pre_load,
2424 .post_load = qxl_post_load,
Alon Levy020af1c2012-08-22 11:16:25 +03002425 .fields = (VMStateField[]) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002426 VMSTATE_PCI_DEVICE(pci, PCIQXLDevice),
2427 VMSTATE_STRUCT(vga, PCIQXLDevice, 0, vmstate_vga_common, VGACommonState),
2428 VMSTATE_UINT32(shadow_rom.mode, PCIQXLDevice),
2429 VMSTATE_UINT32(num_free_res, PCIQXLDevice),
2430 VMSTATE_UINT32(last_release_offset, PCIQXLDevice),
2431 VMSTATE_UINT32(mode, PCIQXLDevice),
2432 VMSTATE_UINT32(ssd.unique, PCIQXLDevice),
Halil Pasicd2164ad2017-06-23 16:48:23 +02002433 VMSTATE_INT32_EQUAL(num_memslots, PCIQXLDevice, NULL),
Gerd Hoffmannb67737a2011-01-05 18:05:52 +01002434 VMSTATE_STRUCT_ARRAY(guest_slots, PCIQXLDevice, NUM_MEMSLOTS, 0,
2435 qxl_memslot, struct guest_slots),
2436 VMSTATE_STRUCT(guest_primary.surface, PCIQXLDevice, 0,
2437 qxl_surface, QXLSurfaceCreate),
Halil Pasicd2164ad2017-06-23 16:48:23 +02002438 VMSTATE_INT32_EQUAL(ssd.num_surfaces, PCIQXLDevice, NULL),
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02002439 VMSTATE_VARRAY_INT32(guest_surfaces.cmds, PCIQXLDevice,
2440 ssd.num_surfaces, 0,
2441 vmstate_info_uint64, uint64_t),
Gerd Hoffmannb67737a2011-01-05 18:05:52 +01002442 VMSTATE_UINT64(guest_cursor, PCIQXLDevice),
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002443 VMSTATE_END_OF_LIST()
2444 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +02002445 .subsections = (const VMStateDescription*[]) {
2446 &qxl_vmstate_monitors_config,
2447 NULL
Alon Levy020af1c2012-08-22 11:16:25 +03002448 }
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002449};
2450
Gerd Hoffmann78e60ba2011-10-17 14:11:16 +02002451static Property qxl_properties[] = {
Philippe Mathieu-Daudéf0353b02018-06-25 09:42:06 -03002452 DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size, 64 * MiB),
2453 DEFINE_PROP_UINT64("vram_size", PCIQXLDevice, vram32_size, 64 * MiB),
Gerd Hoffmann78e60ba2011-10-17 14:11:16 +02002454 DEFINE_PROP_UINT32("revision", PCIQXLDevice, revision,
2455 QXL_DEFAULT_REVISION),
2456 DEFINE_PROP_UINT32("debug", PCIQXLDevice, debug, 0),
2457 DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice, guestdebug, 0),
2458 DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice, cmdlog, 0),
Gerd Hoffmann017438e2012-02-17 15:03:24 +01002459 DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice, ram_size_mb, -1),
Alon Levy79ce3562012-03-29 22:24:38 +02002460 DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice, vram32_size_mb, -1),
2461 DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice, vram_size_mb, -1),
Gerd Hoffmann9e56edc2012-06-11 10:42:53 +02002462 DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice, vgamem_size_mb, 16),
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +02002463 DEFINE_PROP_INT32("surfaces", PCIQXLDevice, ssd.num_surfaces, 1024),
Frediano Ziglio567161f2015-07-06 07:56:38 +01002464#if SPICE_SERVER_VERSION >= 0x000c06 /* release 0.12.6 */
2465 DEFINE_PROP_UINT16("max_outputs", PCIQXLDevice, max_outputs, 0),
2466#endif
Gerd Hoffmann6f663d72017-04-21 11:22:34 +02002467 DEFINE_PROP_UINT32("xres", PCIQXLDevice, xres, 0),
2468 DEFINE_PROP_UINT32("yres", PCIQXLDevice, yres, 0),
Gerd Hoffmann1fcfdc42018-07-02 18:33:44 +02002469 DEFINE_PROP_BOOL("global-vmstate", PCIQXLDevice, vga.global_vmstate, false),
Gerd Hoffmann78e60ba2011-10-17 14:11:16 +02002470 DEFINE_PROP_END_OF_LIST(),
2471};
2472
Gongleic69f6c72015-05-12 17:27:10 +08002473static void qxl_pci_class_init(ObjectClass *klass, void *data)
2474{
2475 DeviceClass *dc = DEVICE_CLASS(klass);
2476 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2477
2478 k->vendor_id = REDHAT_PCI_VENDOR_ID;
2479 k->device_id = QXL_DEVICE_ID_STABLE;
2480 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
2481 dc->reset = qxl_reset_handler;
2482 dc->vmsd = &qxl_vmstate;
Marc-André Lureau4f67d302020-01-10 19:30:32 +04002483 device_class_set_props(dc, qxl_properties);
Gongleic69f6c72015-05-12 17:27:10 +08002484}
2485
2486static const TypeInfo qxl_pci_type_info = {
2487 .name = TYPE_PCI_QXL,
2488 .parent = TYPE_PCI_DEVICE,
2489 .instance_size = sizeof(PCIQXLDevice),
2490 .abstract = true,
2491 .class_init = qxl_pci_class_init,
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -03002492 .interfaces = (InterfaceInfo[]) {
2493 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
2494 { },
2495 },
Gongleic69f6c72015-05-12 17:27:10 +08002496};
2497
Anthony Liguori40021f02011-12-04 12:22:06 -06002498static void qxl_primary_class_init(ObjectClass *klass, void *data)
2499{
Anthony Liguori39bffca2011-12-07 21:34:16 -06002500 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06002501 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2502
Markus Armbruster042a24d2015-01-19 15:52:36 +01002503 k->realize = qxl_realize_primary;
Anthony Liguori40021f02011-12-04 12:22:06 -06002504 k->romfile = "vgabios-qxl.bin";
Anthony Liguori40021f02011-12-04 12:22:06 -06002505 k->class_id = PCI_CLASS_DISPLAY_VGA;
Anthony Liguori39bffca2011-12-07 21:34:16 -06002506 dc->desc = "Spice QXL GPU (primary, vga compatible)";
Igor Mammedov2897ae02014-02-05 16:36:48 +01002507 dc->hotpluggable = false;
Anthony Liguori40021f02011-12-04 12:22:06 -06002508}
2509
Andreas Färber8c43a6f2013-01-10 16:19:07 +01002510static const TypeInfo qxl_primary_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -06002511 .name = "qxl-vga",
Gongleic69f6c72015-05-12 17:27:10 +08002512 .parent = TYPE_PCI_QXL,
Anthony Liguori39bffca2011-12-07 21:34:16 -06002513 .class_init = qxl_primary_class_init,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002514};
2515
Anthony Liguori40021f02011-12-04 12:22:06 -06002516static void qxl_secondary_class_init(ObjectClass *klass, void *data)
2517{
Anthony Liguori39bffca2011-12-07 21:34:16 -06002518 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06002519 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2520
Markus Armbruster042a24d2015-01-19 15:52:36 +01002521 k->realize = qxl_realize_secondary;
Anthony Liguori40021f02011-12-04 12:22:06 -06002522 k->class_id = PCI_CLASS_DISPLAY_OTHER;
Anthony Liguori39bffca2011-12-07 21:34:16 -06002523 dc->desc = "Spice QXL GPU (secondary)";
Anthony Liguori40021f02011-12-04 12:22:06 -06002524}
2525
Andreas Färber8c43a6f2013-01-10 16:19:07 +01002526static const TypeInfo qxl_secondary_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -06002527 .name = "qxl",
Gongleic69f6c72015-05-12 17:27:10 +08002528 .parent = TYPE_PCI_QXL,
Anthony Liguori39bffca2011-12-07 21:34:16 -06002529 .class_init = qxl_secondary_class_init,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002530};
2531
Andreas Färber83f7d432012-02-09 15:20:55 +01002532static void qxl_register_types(void)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002533{
Gongleic69f6c72015-05-12 17:27:10 +08002534 type_register_static(&qxl_pci_type_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -06002535 type_register_static(&qxl_primary_info);
2536 type_register_static(&qxl_secondary_info);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02002537}
2538
Andreas Färber83f7d432012-02-09 15:20:55 +01002539type_init(qxl_register_types)