qxl: register QemuConsole for secondary cards
[qemu/ar7.git] / hw / display / qxl.c
blob437f8d0eeec4f3838b15657be4b7c4b6033465c7
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
4 * written by Yaniv Kamay, Izik Eidus, Gerd Hoffmann
5 * maintained by Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
21 #include <zlib.h>
23 #include "qemu-common.h"
24 #include "qemu/timer.h"
25 #include "qemu/queue.h"
26 #include "monitor/monitor.h"
27 #include "sysemu/sysemu.h"
28 #include "trace.h"
30 #include "qxl.h"
33 * NOTE: SPICE_RING_PROD_ITEM accesses memory on the pci bar and as
34 * such can be changed by the guest, so to avoid a guest trigerrable
35 * abort we just qxl_set_guest_bug and set the return to NULL. Still
36 * it may happen as a result of emulator bug as well.
38 #undef SPICE_RING_PROD_ITEM
39 #define SPICE_RING_PROD_ITEM(qxl, r, ret) { \
40 uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r); \
41 if (prod >= ARRAY_SIZE((r)->items)) { \
42 qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
43 "%u >= %zu", prod, ARRAY_SIZE((r)->items)); \
44 ret = NULL; \
45 } else { \
46 ret = &(r)->items[prod].el; \
47 } \
50 #undef SPICE_RING_CONS_ITEM
51 #define SPICE_RING_CONS_ITEM(qxl, r, ret) { \
52 uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \
53 if (cons >= ARRAY_SIZE((r)->items)) { \
54 qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
55 "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \
56 ret = NULL; \
57 } else { \
58 ret = &(r)->items[cons].el; \
59 } \
62 #undef ALIGN
63 #define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
65 #define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9"
67 #define QXL_MODE(_x, _y, _b, _o) \
68 { .x_res = _x, \
69 .y_res = _y, \
70 .bits = _b, \
71 .stride = (_x) * (_b) / 8, \
72 .x_mili = PIXEL_SIZE * (_x), \
73 .y_mili = PIXEL_SIZE * (_y), \
74 .orientation = _o, \
77 #define QXL_MODE_16_32(x_res, y_res, orientation) \
78 QXL_MODE(x_res, y_res, 16, orientation), \
79 QXL_MODE(x_res, y_res, 32, orientation)
81 #define QXL_MODE_EX(x_res, y_res) \
82 QXL_MODE_16_32(x_res, y_res, 0), \
83 QXL_MODE_16_32(x_res, y_res, 1)
85 static QXLMode qxl_modes[] = {
86 QXL_MODE_EX(640, 480),
87 QXL_MODE_EX(800, 480),
88 QXL_MODE_EX(800, 600),
89 QXL_MODE_EX(832, 624),
90 QXL_MODE_EX(960, 640),
91 QXL_MODE_EX(1024, 600),
92 QXL_MODE_EX(1024, 768),
93 QXL_MODE_EX(1152, 864),
94 QXL_MODE_EX(1152, 870),
95 QXL_MODE_EX(1280, 720),
96 QXL_MODE_EX(1280, 760),
97 QXL_MODE_EX(1280, 768),
98 QXL_MODE_EX(1280, 800),
99 QXL_MODE_EX(1280, 960),
100 QXL_MODE_EX(1280, 1024),
101 QXL_MODE_EX(1360, 768),
102 QXL_MODE_EX(1366, 768),
103 QXL_MODE_EX(1400, 1050),
104 QXL_MODE_EX(1440, 900),
105 QXL_MODE_EX(1600, 900),
106 QXL_MODE_EX(1600, 1200),
107 QXL_MODE_EX(1680, 1050),
108 QXL_MODE_EX(1920, 1080),
109 /* these modes need more than 8 MB video memory */
110 QXL_MODE_EX(1920, 1200),
111 QXL_MODE_EX(1920, 1440),
112 QXL_MODE_EX(2048, 1536),
113 QXL_MODE_EX(2560, 1440),
114 QXL_MODE_EX(2560, 1600),
115 /* these modes need more than 16 MB video memory */
116 QXL_MODE_EX(2560, 2048),
117 QXL_MODE_EX(2800, 2100),
118 QXL_MODE_EX(3200, 2400),
121 static void qxl_send_events(PCIQXLDevice *d, uint32_t events);
122 static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async);
123 static void qxl_reset_memslots(PCIQXLDevice *d);
124 static void qxl_reset_surfaces(PCIQXLDevice *d);
125 static void qxl_ring_set_dirty(PCIQXLDevice *qxl);
127 void qxl_set_guest_bug(PCIQXLDevice *qxl, const char *msg, ...)
129 trace_qxl_set_guest_bug(qxl->id);
130 qxl_send_events(qxl, QXL_INTERRUPT_ERROR);
131 qxl->guest_bug = 1;
132 if (qxl->guestdebug) {
133 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);
142 static void qxl_clear_guest_bug(PCIQXLDevice *qxl)
144 qxl->guest_bug = 0;
147 void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
148 struct QXLRect *area, struct QXLRect *dirty_rects,
149 uint32_t num_dirty_rects,
150 uint32_t clear_dirty_region,
151 qxl_async_io async, struct QXLCookie *cookie)
153 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);
157 if (async == QXL_SYNC) {
158 qxl->ssd.worker->update_area(qxl->ssd.worker, surface_id, area,
159 dirty_rects, num_dirty_rects, clear_dirty_region);
160 } else {
161 assert(cookie != NULL);
162 spice_qxl_update_area_async(&qxl->ssd.qxl, surface_id, area,
163 clear_dirty_region, (uintptr_t)cookie);
167 static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice *qxl,
168 uint32_t id)
170 trace_qxl_spice_destroy_surface_wait_complete(qxl->id, id);
171 qemu_mutex_lock(&qxl->track_lock);
172 qxl->guest_surfaces.cmds[id] = 0;
173 qxl->guest_surfaces.count--;
174 qemu_mutex_unlock(&qxl->track_lock);
177 static void qxl_spice_destroy_surface_wait(PCIQXLDevice *qxl, uint32_t id,
178 qxl_async_io async)
180 QXLCookie *cookie;
182 trace_qxl_spice_destroy_surface_wait(qxl->id, id, async);
183 if (async) {
184 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
185 QXL_IO_DESTROY_SURFACE_ASYNC);
186 cookie->u.surface_id = id;
187 spice_qxl_destroy_surface_async(&qxl->ssd.qxl, id, (uintptr_t)cookie);
188 } else {
189 qxl->ssd.worker->destroy_surface_wait(qxl->ssd.worker, id);
190 qxl_spice_destroy_surface_wait_complete(qxl, id);
194 static void qxl_spice_flush_surfaces_async(PCIQXLDevice *qxl)
196 trace_qxl_spice_flush_surfaces_async(qxl->id, qxl->guest_surfaces.count,
197 qxl->num_free_res);
198 spice_qxl_flush_surfaces_async(&qxl->ssd.qxl,
199 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
200 QXL_IO_FLUSH_SURFACES_ASYNC));
203 void qxl_spice_loadvm_commands(PCIQXLDevice *qxl, struct QXLCommandExt *ext,
204 uint32_t count)
206 trace_qxl_spice_loadvm_commands(qxl->id, ext, count);
207 qxl->ssd.worker->loadvm_commands(qxl->ssd.worker, ext, count);
210 void qxl_spice_oom(PCIQXLDevice *qxl)
212 trace_qxl_spice_oom(qxl->id);
213 qxl->ssd.worker->oom(qxl->ssd.worker);
216 void qxl_spice_reset_memslots(PCIQXLDevice *qxl)
218 trace_qxl_spice_reset_memslots(qxl->id);
219 qxl->ssd.worker->reset_memslots(qxl->ssd.worker);
222 static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice *qxl)
224 trace_qxl_spice_destroy_surfaces_complete(qxl->id);
225 qemu_mutex_lock(&qxl->track_lock);
226 memset(qxl->guest_surfaces.cmds, 0,
227 sizeof(qxl->guest_surfaces.cmds) * qxl->ssd.num_surfaces);
228 qxl->guest_surfaces.count = 0;
229 qemu_mutex_unlock(&qxl->track_lock);
232 static void qxl_spice_destroy_surfaces(PCIQXLDevice *qxl, qxl_async_io async)
234 trace_qxl_spice_destroy_surfaces(qxl->id, async);
235 if (async) {
236 spice_qxl_destroy_surfaces_async(&qxl->ssd.qxl,
237 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
238 QXL_IO_DESTROY_ALL_SURFACES_ASYNC));
239 } else {
240 qxl->ssd.worker->destroy_surfaces(qxl->ssd.worker);
241 qxl_spice_destroy_surfaces_complete(qxl);
245 static void qxl_spice_monitors_config_async(PCIQXLDevice *qxl, int replay)
247 trace_qxl_spice_monitors_config(qxl->id);
248 if (replay) {
250 * don't use QXL_COOKIE_TYPE_IO:
251 * - we are not running yet (post_load), we will assert
252 * in send_events
253 * - this is not a guest io, but a reply, so async_io isn't set.
255 spice_qxl_monitors_config_async(&qxl->ssd.qxl,
256 qxl->guest_monitors_config,
257 MEMSLOT_GROUP_GUEST,
258 (uintptr_t)qxl_cookie_new(
259 QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
260 0));
261 } else {
262 qxl->guest_monitors_config = qxl->ram->monitors_config;
263 spice_qxl_monitors_config_async(&qxl->ssd.qxl,
264 qxl->ram->monitors_config,
265 MEMSLOT_GROUP_GUEST,
266 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
267 QXL_IO_MONITORS_CONFIG_ASYNC));
271 void qxl_spice_reset_image_cache(PCIQXLDevice *qxl)
273 trace_qxl_spice_reset_image_cache(qxl->id);
274 qxl->ssd.worker->reset_image_cache(qxl->ssd.worker);
277 void qxl_spice_reset_cursor(PCIQXLDevice *qxl)
279 trace_qxl_spice_reset_cursor(qxl->id);
280 qxl->ssd.worker->reset_cursor(qxl->ssd.worker);
281 qemu_mutex_lock(&qxl->track_lock);
282 qxl->guest_cursor = 0;
283 qemu_mutex_unlock(&qxl->track_lock);
284 if (qxl->ssd.cursor) {
285 cursor_put(qxl->ssd.cursor);
287 qxl->ssd.cursor = cursor_builtin_hidden();
291 static inline uint32_t msb_mask(uint32_t val)
293 uint32_t mask;
295 do {
296 mask = ~(val - 1) & val;
297 val &= ~mask;
298 } while (mask < val);
300 return mask;
303 static ram_addr_t qxl_rom_size(void)
305 uint32_t required_rom_size = sizeof(QXLRom) + sizeof(QXLModes) +
306 sizeof(qxl_modes);
307 uint32_t rom_size = 8192; /* two pages */
309 required_rom_size = MAX(required_rom_size, TARGET_PAGE_SIZE);
310 required_rom_size = msb_mask(required_rom_size * 2 - 1);
311 assert(required_rom_size <= rom_size);
312 return rom_size;
315 static void init_qxl_rom(PCIQXLDevice *d)
317 QXLRom *rom = memory_region_get_ram_ptr(&d->rom_bar);
318 QXLModes *modes = (QXLModes *)(rom + 1);
319 uint32_t ram_header_size;
320 uint32_t surface0_area_size;
321 uint32_t num_pages;
322 uint32_t fb;
323 int i, n;
325 memset(rom, 0, d->rom_size);
327 rom->magic = cpu_to_le32(QXL_ROM_MAGIC);
328 rom->id = cpu_to_le32(d->id);
329 rom->log_level = cpu_to_le32(d->guestdebug);
330 rom->modes_offset = cpu_to_le32(sizeof(QXLRom));
332 rom->slot_gen_bits = MEMSLOT_GENERATION_BITS;
333 rom->slot_id_bits = MEMSLOT_SLOT_BITS;
334 rom->slots_start = 1;
335 rom->slots_end = NUM_MEMSLOTS - 1;
336 rom->n_surfaces = cpu_to_le32(d->ssd.num_surfaces);
338 for (i = 0, n = 0; i < ARRAY_SIZE(qxl_modes); i++) {
339 fb = qxl_modes[i].y_res * qxl_modes[i].stride;
340 if (fb > d->vgamem_size) {
341 continue;
343 modes->modes[n].id = cpu_to_le32(i);
344 modes->modes[n].x_res = cpu_to_le32(qxl_modes[i].x_res);
345 modes->modes[n].y_res = cpu_to_le32(qxl_modes[i].y_res);
346 modes->modes[n].bits = cpu_to_le32(qxl_modes[i].bits);
347 modes->modes[n].stride = cpu_to_le32(qxl_modes[i].stride);
348 modes->modes[n].x_mili = cpu_to_le32(qxl_modes[i].x_mili);
349 modes->modes[n].y_mili = cpu_to_le32(qxl_modes[i].y_mili);
350 modes->modes[n].orientation = cpu_to_le32(qxl_modes[i].orientation);
351 n++;
353 modes->n_modes = cpu_to_le32(n);
355 ram_header_size = ALIGN(sizeof(QXLRam), 4096);
356 surface0_area_size = ALIGN(d->vgamem_size, 4096);
357 num_pages = d->vga.vram_size;
358 num_pages -= ram_header_size;
359 num_pages -= surface0_area_size;
360 num_pages = num_pages / TARGET_PAGE_SIZE;
362 rom->draw_area_offset = cpu_to_le32(0);
363 rom->surface0_area_size = cpu_to_le32(surface0_area_size);
364 rom->pages_offset = cpu_to_le32(surface0_area_size);
365 rom->num_pages = cpu_to_le32(num_pages);
366 rom->ram_header_offset = cpu_to_le32(d->vga.vram_size - ram_header_size);
368 d->shadow_rom = *rom;
369 d->rom = rom;
370 d->modes = modes;
373 static void init_qxl_ram(PCIQXLDevice *d)
375 uint8_t *buf;
376 uint64_t *item;
378 buf = d->vga.vram_ptr;
379 d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset));
380 d->ram->magic = cpu_to_le32(QXL_RAM_MAGIC);
381 d->ram->int_pending = cpu_to_le32(0);
382 d->ram->int_mask = cpu_to_le32(0);
383 d->ram->update_surface = 0;
384 SPICE_RING_INIT(&d->ram->cmd_ring);
385 SPICE_RING_INIT(&d->ram->cursor_ring);
386 SPICE_RING_INIT(&d->ram->release_ring);
387 SPICE_RING_PROD_ITEM(d, &d->ram->release_ring, item);
388 assert(item);
389 *item = 0;
390 qxl_ring_set_dirty(d);
393 /* can be called from spice server thread context */
394 static void qxl_set_dirty(MemoryRegion *mr, ram_addr_t addr, ram_addr_t end)
396 memory_region_set_dirty(mr, addr, end - addr);
399 static void qxl_rom_set_dirty(PCIQXLDevice *qxl)
401 qxl_set_dirty(&qxl->rom_bar, 0, qxl->rom_size);
404 /* called from spice server thread context only */
405 static void qxl_ram_set_dirty(PCIQXLDevice *qxl, void *ptr)
407 void *base = qxl->vga.vram_ptr;
408 intptr_t offset;
410 offset = ptr - base;
411 offset &= ~(TARGET_PAGE_SIZE-1);
412 assert(offset < qxl->vga.vram_size);
413 qxl_set_dirty(&qxl->vga.vram, offset, offset + TARGET_PAGE_SIZE);
416 /* can be called from spice server thread context */
417 static void qxl_ring_set_dirty(PCIQXLDevice *qxl)
419 ram_addr_t addr = qxl->shadow_rom.ram_header_offset;
420 ram_addr_t end = qxl->vga.vram_size;
421 qxl_set_dirty(&qxl->vga.vram, addr, end);
425 * keep track of some command state, for savevm/loadvm.
426 * called from spice server thread context only
428 static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
430 switch (le32_to_cpu(ext->cmd.type)) {
431 case QXL_CMD_SURFACE:
433 QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
435 if (!cmd) {
436 return 1;
438 uint32_t id = le32_to_cpu(cmd->surface_id);
440 if (id >= qxl->ssd.num_surfaces) {
441 qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE id %d >= %d", id,
442 qxl->ssd.num_surfaces);
443 return 1;
445 if (cmd->type == QXL_SURFACE_CMD_CREATE &&
446 (cmd->u.surface_create.stride & 0x03) != 0) {
447 qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
448 cmd->u.surface_create.stride);
449 return 1;
451 qemu_mutex_lock(&qxl->track_lock);
452 if (cmd->type == QXL_SURFACE_CMD_CREATE) {
453 qxl->guest_surfaces.cmds[id] = ext->cmd.data;
454 qxl->guest_surfaces.count++;
455 if (qxl->guest_surfaces.max < qxl->guest_surfaces.count)
456 qxl->guest_surfaces.max = qxl->guest_surfaces.count;
458 if (cmd->type == QXL_SURFACE_CMD_DESTROY) {
459 qxl->guest_surfaces.cmds[id] = 0;
460 qxl->guest_surfaces.count--;
462 qemu_mutex_unlock(&qxl->track_lock);
463 break;
465 case QXL_CMD_CURSOR:
467 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
469 if (!cmd) {
470 return 1;
472 if (cmd->type == QXL_CURSOR_SET) {
473 qemu_mutex_lock(&qxl->track_lock);
474 qxl->guest_cursor = ext->cmd.data;
475 qemu_mutex_unlock(&qxl->track_lock);
477 break;
480 return 0;
483 /* spice display interface callbacks */
485 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
487 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
489 trace_qxl_interface_attach_worker(qxl->id);
490 qxl->ssd.worker = qxl_worker;
493 static void interface_set_compression_level(QXLInstance *sin, int level)
495 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
497 trace_qxl_interface_set_compression_level(qxl->id, level);
498 qxl->shadow_rom.compression_level = cpu_to_le32(level);
499 qxl->rom->compression_level = cpu_to_le32(level);
500 qxl_rom_set_dirty(qxl);
503 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
505 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
507 trace_qxl_interface_set_mm_time(qxl->id, mm_time);
508 qxl->shadow_rom.mm_clock = cpu_to_le32(mm_time);
509 qxl->rom->mm_clock = cpu_to_le32(mm_time);
510 qxl_rom_set_dirty(qxl);
513 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
515 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
517 trace_qxl_interface_get_init_info(qxl->id);
518 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
519 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
520 info->num_memslots = NUM_MEMSLOTS;
521 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
522 info->internal_groupslot_id = 0;
523 info->qxl_ram_size = le32_to_cpu(qxl->shadow_rom.num_pages) << TARGET_PAGE_BITS;
524 info->n_surfaces = qxl->ssd.num_surfaces;
527 static const char *qxl_mode_to_string(int mode)
529 switch (mode) {
530 case QXL_MODE_COMPAT:
531 return "compat";
532 case QXL_MODE_NATIVE:
533 return "native";
534 case QXL_MODE_UNDEFINED:
535 return "undefined";
536 case QXL_MODE_VGA:
537 return "vga";
539 return "INVALID";
542 static const char *io_port_to_string(uint32_t io_port)
544 if (io_port >= QXL_IO_RANGE_SIZE) {
545 return "out of range";
547 static const char *io_port_to_string[QXL_IO_RANGE_SIZE + 1] = {
548 [QXL_IO_NOTIFY_CMD] = "QXL_IO_NOTIFY_CMD",
549 [QXL_IO_NOTIFY_CURSOR] = "QXL_IO_NOTIFY_CURSOR",
550 [QXL_IO_UPDATE_AREA] = "QXL_IO_UPDATE_AREA",
551 [QXL_IO_UPDATE_IRQ] = "QXL_IO_UPDATE_IRQ",
552 [QXL_IO_NOTIFY_OOM] = "QXL_IO_NOTIFY_OOM",
553 [QXL_IO_RESET] = "QXL_IO_RESET",
554 [QXL_IO_SET_MODE] = "QXL_IO_SET_MODE",
555 [QXL_IO_LOG] = "QXL_IO_LOG",
556 [QXL_IO_MEMSLOT_ADD] = "QXL_IO_MEMSLOT_ADD",
557 [QXL_IO_MEMSLOT_DEL] = "QXL_IO_MEMSLOT_DEL",
558 [QXL_IO_DETACH_PRIMARY] = "QXL_IO_DETACH_PRIMARY",
559 [QXL_IO_ATTACH_PRIMARY] = "QXL_IO_ATTACH_PRIMARY",
560 [QXL_IO_CREATE_PRIMARY] = "QXL_IO_CREATE_PRIMARY",
561 [QXL_IO_DESTROY_PRIMARY] = "QXL_IO_DESTROY_PRIMARY",
562 [QXL_IO_DESTROY_SURFACE_WAIT] = "QXL_IO_DESTROY_SURFACE_WAIT",
563 [QXL_IO_DESTROY_ALL_SURFACES] = "QXL_IO_DESTROY_ALL_SURFACES",
564 [QXL_IO_UPDATE_AREA_ASYNC] = "QXL_IO_UPDATE_AREA_ASYNC",
565 [QXL_IO_MEMSLOT_ADD_ASYNC] = "QXL_IO_MEMSLOT_ADD_ASYNC",
566 [QXL_IO_CREATE_PRIMARY_ASYNC] = "QXL_IO_CREATE_PRIMARY_ASYNC",
567 [QXL_IO_DESTROY_PRIMARY_ASYNC] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
568 [QXL_IO_DESTROY_SURFACE_ASYNC] = "QXL_IO_DESTROY_SURFACE_ASYNC",
569 [QXL_IO_DESTROY_ALL_SURFACES_ASYNC]
570 = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
571 [QXL_IO_FLUSH_SURFACES_ASYNC] = "QXL_IO_FLUSH_SURFACES_ASYNC",
572 [QXL_IO_FLUSH_RELEASE] = "QXL_IO_FLUSH_RELEASE",
573 [QXL_IO_MONITORS_CONFIG_ASYNC] = "QXL_IO_MONITORS_CONFIG_ASYNC",
575 return io_port_to_string[io_port];
578 /* called from spice server thread context only */
579 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
581 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
582 SimpleSpiceUpdate *update;
583 QXLCommandRing *ring;
584 QXLCommand *cmd;
585 int notify, ret;
587 trace_qxl_ring_command_check(qxl->id, qxl_mode_to_string(qxl->mode));
589 switch (qxl->mode) {
590 case QXL_MODE_VGA:
591 ret = false;
592 qemu_mutex_lock(&qxl->ssd.lock);
593 update = QTAILQ_FIRST(&qxl->ssd.updates);
594 if (update != NULL) {
595 QTAILQ_REMOVE(&qxl->ssd.updates, update, next);
596 *ext = update->ext;
597 ret = true;
599 qemu_mutex_unlock(&qxl->ssd.lock);
600 if (ret) {
601 trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
602 qxl_log_command(qxl, "vga", ext);
604 return ret;
605 case QXL_MODE_COMPAT:
606 case QXL_MODE_NATIVE:
607 case QXL_MODE_UNDEFINED:
608 ring = &qxl->ram->cmd_ring;
609 if (qxl->guest_bug || SPICE_RING_IS_EMPTY(ring)) {
610 return false;
612 SPICE_RING_CONS_ITEM(qxl, ring, cmd);
613 if (!cmd) {
614 return false;
616 ext->cmd = *cmd;
617 ext->group_id = MEMSLOT_GROUP_GUEST;
618 ext->flags = qxl->cmdflags;
619 SPICE_RING_POP(ring, notify);
620 qxl_ring_set_dirty(qxl);
621 if (notify) {
622 qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
624 qxl->guest_primary.commands++;
625 qxl_track_command(qxl, ext);
626 qxl_log_command(qxl, "cmd", ext);
627 trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
628 return true;
629 default:
630 return false;
634 /* called from spice server thread context only */
635 static int interface_req_cmd_notification(QXLInstance *sin)
637 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
638 int wait = 1;
640 trace_qxl_ring_command_req_notification(qxl->id);
641 switch (qxl->mode) {
642 case QXL_MODE_COMPAT:
643 case QXL_MODE_NATIVE:
644 case QXL_MODE_UNDEFINED:
645 SPICE_RING_CONS_WAIT(&qxl->ram->cmd_ring, wait);
646 qxl_ring_set_dirty(qxl);
647 break;
648 default:
649 /* nothing */
650 break;
652 return wait;
655 /* called from spice server thread context only */
656 static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
658 QXLReleaseRing *ring = &d->ram->release_ring;
659 uint64_t *item;
660 int notify;
662 #define QXL_FREE_BUNCH_SIZE 32
664 if (ring->prod - ring->cons + 1 == ring->num_items) {
665 /* ring full -- can't push */
666 return;
668 if (!flush && d->oom_running) {
669 /* collect everything from oom handler before pushing */
670 return;
672 if (!flush && d->num_free_res < QXL_FREE_BUNCH_SIZE) {
673 /* collect a bit more before pushing */
674 return;
677 SPICE_RING_PUSH(ring, notify);
678 trace_qxl_ring_res_push(d->id, qxl_mode_to_string(d->mode),
679 d->guest_surfaces.count, d->num_free_res,
680 d->last_release, notify ? "yes" : "no");
681 trace_qxl_ring_res_push_rest(d->id, ring->prod - ring->cons,
682 ring->num_items, ring->prod, ring->cons);
683 if (notify) {
684 qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
686 SPICE_RING_PROD_ITEM(d, ring, item);
687 if (!item) {
688 return;
690 *item = 0;
691 d->num_free_res = 0;
692 d->last_release = NULL;
693 qxl_ring_set_dirty(d);
696 /* called from spice server thread context only */
697 static void interface_release_resource(QXLInstance *sin,
698 struct QXLReleaseInfoExt ext)
700 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
701 QXLReleaseRing *ring;
702 uint64_t *item, id;
704 if (ext.group_id == MEMSLOT_GROUP_HOST) {
705 /* host group -> vga mode update request */
706 qemu_spice_destroy_update(&qxl->ssd, (void *)(intptr_t)ext.info->id);
707 return;
711 * ext->info points into guest-visible memory
712 * pci bar 0, $command.release_info
714 ring = &qxl->ram->release_ring;
715 SPICE_RING_PROD_ITEM(qxl, ring, item);
716 if (!item) {
717 return;
719 if (*item == 0) {
720 /* stick head into the ring */
721 id = ext.info->id;
722 ext.info->next = 0;
723 qxl_ram_set_dirty(qxl, &ext.info->next);
724 *item = id;
725 qxl_ring_set_dirty(qxl);
726 } else {
727 /* append item to the list */
728 qxl->last_release->next = ext.info->id;
729 qxl_ram_set_dirty(qxl, &qxl->last_release->next);
730 ext.info->next = 0;
731 qxl_ram_set_dirty(qxl, &ext.info->next);
733 qxl->last_release = ext.info;
734 qxl->num_free_res++;
735 trace_qxl_ring_res_put(qxl->id, qxl->num_free_res);
736 qxl_push_free_res(qxl, 0);
739 /* called from spice server thread context only */
740 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
742 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
743 QXLCursorRing *ring;
744 QXLCommand *cmd;
745 int notify;
747 trace_qxl_ring_cursor_check(qxl->id, qxl_mode_to_string(qxl->mode));
749 switch (qxl->mode) {
750 case QXL_MODE_COMPAT:
751 case QXL_MODE_NATIVE:
752 case QXL_MODE_UNDEFINED:
753 ring = &qxl->ram->cursor_ring;
754 if (SPICE_RING_IS_EMPTY(ring)) {
755 return false;
757 SPICE_RING_CONS_ITEM(qxl, ring, cmd);
758 if (!cmd) {
759 return false;
761 ext->cmd = *cmd;
762 ext->group_id = MEMSLOT_GROUP_GUEST;
763 ext->flags = qxl->cmdflags;
764 SPICE_RING_POP(ring, notify);
765 qxl_ring_set_dirty(qxl);
766 if (notify) {
767 qxl_send_events(qxl, QXL_INTERRUPT_CURSOR);
769 qxl->guest_primary.commands++;
770 qxl_track_command(qxl, ext);
771 qxl_log_command(qxl, "csr", ext);
772 if (qxl->id == 0) {
773 qxl_render_cursor(qxl, ext);
775 trace_qxl_ring_cursor_get(qxl->id, qxl_mode_to_string(qxl->mode));
776 return true;
777 default:
778 return false;
782 /* called from spice server thread context only */
783 static int interface_req_cursor_notification(QXLInstance *sin)
785 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
786 int wait = 1;
788 trace_qxl_ring_cursor_req_notification(qxl->id);
789 switch (qxl->mode) {
790 case QXL_MODE_COMPAT:
791 case QXL_MODE_NATIVE:
792 case QXL_MODE_UNDEFINED:
793 SPICE_RING_CONS_WAIT(&qxl->ram->cursor_ring, wait);
794 qxl_ring_set_dirty(qxl);
795 break;
796 default:
797 /* nothing */
798 break;
800 return wait;
803 /* called from spice server thread context */
804 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
807 * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
808 * use by xf86-video-qxl and is defined out in the qxl windows driver.
809 * Probably was at some earlier version that is prior to git start (2009),
810 * and is still guest trigerrable.
812 fprintf(stderr, "%s: deprecated\n", __func__);
815 /* called from spice server thread context only */
816 static int interface_flush_resources(QXLInstance *sin)
818 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
819 int ret;
821 ret = qxl->num_free_res;
822 if (ret) {
823 qxl_push_free_res(qxl, 1);
825 return ret;
828 static void qxl_create_guest_primary_complete(PCIQXLDevice *d);
830 /* called from spice server thread context only */
831 static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
833 uint32_t current_async;
835 qemu_mutex_lock(&qxl->async_lock);
836 current_async = qxl->current_async;
837 qxl->current_async = QXL_UNDEFINED_IO;
838 qemu_mutex_unlock(&qxl->async_lock);
840 trace_qxl_interface_async_complete_io(qxl->id, current_async, cookie);
841 if (!cookie) {
842 fprintf(stderr, "qxl: %s: error, cookie is NULL\n", __func__);
843 return;
845 if (cookie && current_async != cookie->io) {
846 fprintf(stderr,
847 "qxl: %s: error: current_async = %d != %"
848 PRId64 " = cookie->io\n", __func__, current_async, cookie->io);
850 switch (current_async) {
851 case QXL_IO_MEMSLOT_ADD_ASYNC:
852 case QXL_IO_DESTROY_PRIMARY_ASYNC:
853 case QXL_IO_UPDATE_AREA_ASYNC:
854 case QXL_IO_FLUSH_SURFACES_ASYNC:
855 case QXL_IO_MONITORS_CONFIG_ASYNC:
856 break;
857 case QXL_IO_CREATE_PRIMARY_ASYNC:
858 qxl_create_guest_primary_complete(qxl);
859 break;
860 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
861 qxl_spice_destroy_surfaces_complete(qxl);
862 break;
863 case QXL_IO_DESTROY_SURFACE_ASYNC:
864 qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
865 break;
866 default:
867 fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
868 current_async);
870 qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
873 /* called from spice server thread context only */
874 static void interface_update_area_complete(QXLInstance *sin,
875 uint32_t surface_id,
876 QXLRect *dirty, uint32_t num_updated_rects)
878 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
879 int i;
880 int qxl_i;
882 qemu_mutex_lock(&qxl->ssd.lock);
883 if (surface_id != 0 || !qxl->render_update_cookie_num) {
884 qemu_mutex_unlock(&qxl->ssd.lock);
885 return;
887 trace_qxl_interface_update_area_complete(qxl->id, surface_id, dirty->left,
888 dirty->right, dirty->top, dirty->bottom);
889 trace_qxl_interface_update_area_complete_rest(qxl->id, num_updated_rects);
890 if (qxl->num_dirty_rects + num_updated_rects > QXL_NUM_DIRTY_RECTS) {
892 * overflow - treat this as a full update. Not expected to be common.
894 trace_qxl_interface_update_area_complete_overflow(qxl->id,
895 QXL_NUM_DIRTY_RECTS);
896 qxl->guest_primary.resized = 1;
898 if (qxl->guest_primary.resized) {
900 * Don't bother copying or scheduling the bh since we will flip
901 * the whole area anyway on completion of the update_area async call
903 qemu_mutex_unlock(&qxl->ssd.lock);
904 return;
906 qxl_i = qxl->num_dirty_rects;
907 for (i = 0; i < num_updated_rects; i++) {
908 qxl->dirty[qxl_i++] = dirty[i];
910 qxl->num_dirty_rects += num_updated_rects;
911 trace_qxl_interface_update_area_complete_schedule_bh(qxl->id,
912 qxl->num_dirty_rects);
913 qemu_bh_schedule(qxl->update_area_bh);
914 qemu_mutex_unlock(&qxl->ssd.lock);
917 /* called from spice server thread context only */
918 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
920 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
921 QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
923 switch (cookie->type) {
924 case QXL_COOKIE_TYPE_IO:
925 interface_async_complete_io(qxl, cookie);
926 g_free(cookie);
927 break;
928 case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA:
929 qxl_render_update_area_done(qxl, cookie);
930 break;
931 case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG:
932 break;
933 default:
934 fprintf(stderr, "qxl: %s: unexpected cookie type %d\n",
935 __func__, cookie->type);
936 g_free(cookie);
940 /* called from spice server thread context only */
941 static void interface_set_client_capabilities(QXLInstance *sin,
942 uint8_t client_present,
943 uint8_t caps[58])
945 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
947 if (qxl->revision < 4) {
948 trace_qxl_set_client_capabilities_unsupported_by_revision(qxl->id,
949 qxl->revision);
950 return;
953 if (runstate_check(RUN_STATE_INMIGRATE) ||
954 runstate_check(RUN_STATE_POSTMIGRATE)) {
955 return;
958 qxl->shadow_rom.client_present = client_present;
959 memcpy(qxl->shadow_rom.client_capabilities, caps,
960 sizeof(qxl->shadow_rom.client_capabilities));
961 qxl->rom->client_present = client_present;
962 memcpy(qxl->rom->client_capabilities, caps,
963 sizeof(qxl->rom->client_capabilities));
964 qxl_rom_set_dirty(qxl);
966 qxl_send_events(qxl, QXL_INTERRUPT_CLIENT);
969 static uint32_t qxl_crc32(const uint8_t *p, unsigned len)
972 * zlib xors the seed with 0xffffffff, and xors the result
973 * again with 0xffffffff; Both are not done with linux's crc32,
974 * which we want to be compatible with, so undo that.
976 return crc32(0xffffffff, p, len) ^ 0xffffffff;
979 /* called from main context only */
980 static int interface_client_monitors_config(QXLInstance *sin,
981 VDAgentMonitorsConfig *monitors_config)
983 PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
984 QXLRom *rom = memory_region_get_ram_ptr(&qxl->rom_bar);
985 int i;
987 if (qxl->revision < 4) {
988 trace_qxl_client_monitors_config_unsupported_by_device(qxl->id,
989 qxl->revision);
990 return 0;
993 * Older windows drivers set int_mask to 0 when their ISR is called,
994 * then later set it to ~0. So it doesn't relate to the actual interrupts
995 * handled. However, they are old, so clearly they don't support this
996 * interrupt
998 if (qxl->ram->int_mask == 0 || qxl->ram->int_mask == ~0 ||
999 !(qxl->ram->int_mask & QXL_INTERRUPT_CLIENT_MONITORS_CONFIG)) {
1000 trace_qxl_client_monitors_config_unsupported_by_guest(qxl->id,
1001 qxl->ram->int_mask,
1002 monitors_config);
1003 return 0;
1005 if (!monitors_config) {
1006 return 1;
1008 memset(&rom->client_monitors_config, 0,
1009 sizeof(rom->client_monitors_config));
1010 rom->client_monitors_config.count = monitors_config->num_of_monitors;
1011 /* monitors_config->flags ignored */
1012 if (rom->client_monitors_config.count >=
1013 ARRAY_SIZE(rom->client_monitors_config.heads)) {
1014 trace_qxl_client_monitors_config_capped(qxl->id,
1015 monitors_config->num_of_monitors,
1016 ARRAY_SIZE(rom->client_monitors_config.heads));
1017 rom->client_monitors_config.count =
1018 ARRAY_SIZE(rom->client_monitors_config.heads);
1020 for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
1021 VDAgentMonConfig *monitor = &monitors_config->monitors[i];
1022 QXLURect *rect = &rom->client_monitors_config.heads[i];
1023 /* monitor->depth ignored */
1024 rect->left = monitor->x;
1025 rect->top = monitor->y;
1026 rect->right = monitor->x + monitor->width;
1027 rect->bottom = monitor->y + monitor->height;
1029 rom->client_monitors_config_crc = qxl_crc32(
1030 (const uint8_t *)&rom->client_monitors_config,
1031 sizeof(rom->client_monitors_config));
1032 trace_qxl_client_monitors_config_crc(qxl->id,
1033 sizeof(rom->client_monitors_config),
1034 rom->client_monitors_config_crc);
1036 trace_qxl_interrupt_client_monitors_config(qxl->id,
1037 rom->client_monitors_config.count,
1038 rom->client_monitors_config.heads);
1039 qxl_send_events(qxl, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG);
1040 return 1;
1043 static const QXLInterface qxl_interface = {
1044 .base.type = SPICE_INTERFACE_QXL,
1045 .base.description = "qxl gpu",
1046 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
1047 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
1049 .attache_worker = interface_attach_worker,
1050 .set_compression_level = interface_set_compression_level,
1051 .set_mm_time = interface_set_mm_time,
1052 .get_init_info = interface_get_init_info,
1054 /* the callbacks below are called from spice server thread context */
1055 .get_command = interface_get_command,
1056 .req_cmd_notification = interface_req_cmd_notification,
1057 .release_resource = interface_release_resource,
1058 .get_cursor_command = interface_get_cursor_command,
1059 .req_cursor_notification = interface_req_cursor_notification,
1060 .notify_update = interface_notify_update,
1061 .flush_resources = interface_flush_resources,
1062 .async_complete = interface_async_complete,
1063 .update_area_complete = interface_update_area_complete,
1064 .set_client_capabilities = interface_set_client_capabilities,
1065 .client_monitors_config = interface_client_monitors_config,
1068 static void qxl_enter_vga_mode(PCIQXLDevice *d)
1070 if (d->mode == QXL_MODE_VGA) {
1071 return;
1073 trace_qxl_enter_vga_mode(d->id);
1074 qemu_spice_create_host_primary(&d->ssd);
1075 d->mode = QXL_MODE_VGA;
1076 vga_dirty_log_start(&d->vga);
1077 graphic_hw_update(d->vga.con);
1080 static void qxl_exit_vga_mode(PCIQXLDevice *d)
1082 if (d->mode != QXL_MODE_VGA) {
1083 return;
1085 trace_qxl_exit_vga_mode(d->id);
1086 vga_dirty_log_stop(&d->vga);
1087 qxl_destroy_primary(d, QXL_SYNC);
1090 static void qxl_update_irq(PCIQXLDevice *d)
1092 uint32_t pending = le32_to_cpu(d->ram->int_pending);
1093 uint32_t mask = le32_to_cpu(d->ram->int_mask);
1094 int level = !!(pending & mask);
1095 qemu_set_irq(d->pci.irq[0], level);
1096 qxl_ring_set_dirty(d);
1099 static void qxl_check_state(PCIQXLDevice *d)
1101 QXLRam *ram = d->ram;
1102 int spice_display_running = qemu_spice_display_is_running(&d->ssd);
1104 assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cmd_ring));
1105 assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cursor_ring));
1108 static void qxl_reset_state(PCIQXLDevice *d)
1110 QXLRom *rom = d->rom;
1112 qxl_check_state(d);
1113 d->shadow_rom.update_id = cpu_to_le32(0);
1114 *rom = d->shadow_rom;
1115 qxl_rom_set_dirty(d);
1116 init_qxl_ram(d);
1117 d->num_free_res = 0;
1118 d->last_release = NULL;
1119 memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty));
1122 static void qxl_soft_reset(PCIQXLDevice *d)
1124 trace_qxl_soft_reset(d->id);
1125 qxl_check_state(d);
1126 qxl_clear_guest_bug(d);
1127 d->current_async = QXL_UNDEFINED_IO;
1129 if (d->id == 0) {
1130 qxl_enter_vga_mode(d);
1131 } else {
1132 d->mode = QXL_MODE_UNDEFINED;
1136 static void qxl_hard_reset(PCIQXLDevice *d, int loadvm)
1138 trace_qxl_hard_reset(d->id, loadvm);
1140 qxl_spice_reset_cursor(d);
1141 qxl_spice_reset_image_cache(d);
1142 qxl_reset_surfaces(d);
1143 qxl_reset_memslots(d);
1145 /* pre loadvm reset must not touch QXLRam. This lives in
1146 * device memory, is migrated together with RAM and thus
1147 * already loaded at this point */
1148 if (!loadvm) {
1149 qxl_reset_state(d);
1151 qemu_spice_create_host_memslot(&d->ssd);
1152 qxl_soft_reset(d);
1155 static void qxl_reset_handler(DeviceState *dev)
1157 PCIQXLDevice *d = DO_UPCAST(PCIQXLDevice, pci.qdev, dev);
1159 qxl_hard_reset(d, 0);
1162 static void qxl_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1164 VGACommonState *vga = opaque;
1165 PCIQXLDevice *qxl = container_of(vga, PCIQXLDevice, vga);
1167 trace_qxl_io_write_vga(qxl->id, qxl_mode_to_string(qxl->mode), addr, val);
1168 if (qxl->mode != QXL_MODE_VGA) {
1169 qxl_destroy_primary(qxl, QXL_SYNC);
1170 qxl_soft_reset(qxl);
1172 vga_ioport_write(opaque, addr, val);
1175 static const MemoryRegionPortio qxl_vga_portio_list[] = {
1176 { 0x04, 2, 1, .read = vga_ioport_read,
1177 .write = qxl_vga_ioport_write }, /* 3b4 */
1178 { 0x0a, 1, 1, .read = vga_ioport_read,
1179 .write = qxl_vga_ioport_write }, /* 3ba */
1180 { 0x10, 16, 1, .read = vga_ioport_read,
1181 .write = qxl_vga_ioport_write }, /* 3c0 */
1182 { 0x24, 2, 1, .read = vga_ioport_read,
1183 .write = qxl_vga_ioport_write }, /* 3d4 */
1184 { 0x2a, 1, 1, .read = vga_ioport_read,
1185 .write = qxl_vga_ioport_write }, /* 3da */
1186 PORTIO_END_OF_LIST(),
1189 static int qxl_add_memslot(PCIQXLDevice *d, uint32_t slot_id, uint64_t delta,
1190 qxl_async_io async)
1192 static const int regions[] = {
1193 QXL_RAM_RANGE_INDEX,
1194 QXL_VRAM_RANGE_INDEX,
1195 QXL_VRAM64_RANGE_INDEX,
1197 uint64_t guest_start;
1198 uint64_t guest_end;
1199 int pci_region;
1200 pcibus_t pci_start;
1201 pcibus_t pci_end;
1202 intptr_t virt_start;
1203 QXLDevMemSlot memslot;
1204 int i;
1206 guest_start = le64_to_cpu(d->guest_slots[slot_id].slot.mem_start);
1207 guest_end = le64_to_cpu(d->guest_slots[slot_id].slot.mem_end);
1209 trace_qxl_memslot_add_guest(d->id, slot_id, guest_start, guest_end);
1211 if (slot_id >= NUM_MEMSLOTS) {
1212 qxl_set_guest_bug(d, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__,
1213 slot_id, NUM_MEMSLOTS);
1214 return 1;
1216 if (guest_start > guest_end) {
1217 qxl_set_guest_bug(d, "%s: guest_start > guest_end 0x%" PRIx64
1218 " > 0x%" PRIx64, __func__, guest_start, guest_end);
1219 return 1;
1222 for (i = 0; i < ARRAY_SIZE(regions); i++) {
1223 pci_region = regions[i];
1224 pci_start = d->pci.io_regions[pci_region].addr;
1225 pci_end = pci_start + d->pci.io_regions[pci_region].size;
1226 /* mapped? */
1227 if (pci_start == -1) {
1228 continue;
1230 /* start address in range ? */
1231 if (guest_start < pci_start || guest_start > pci_end) {
1232 continue;
1234 /* end address in range ? */
1235 if (guest_end > pci_end) {
1236 continue;
1238 /* passed */
1239 break;
1241 if (i == ARRAY_SIZE(regions)) {
1242 qxl_set_guest_bug(d, "%s: finished loop without match", __func__);
1243 return 1;
1246 switch (pci_region) {
1247 case QXL_RAM_RANGE_INDEX:
1248 virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vga.vram);
1249 break;
1250 case QXL_VRAM_RANGE_INDEX:
1251 case 4 /* vram 64bit */:
1252 virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vram_bar);
1253 break;
1254 default:
1255 /* should not happen */
1256 qxl_set_guest_bug(d, "%s: pci_region = %d", __func__, pci_region);
1257 return 1;
1260 memslot.slot_id = slot_id;
1261 memslot.slot_group_id = MEMSLOT_GROUP_GUEST; /* guest group */
1262 memslot.virt_start = virt_start + (guest_start - pci_start);
1263 memslot.virt_end = virt_start + (guest_end - pci_start);
1264 memslot.addr_delta = memslot.virt_start - delta;
1265 memslot.generation = d->rom->slot_generation = 0;
1266 qxl_rom_set_dirty(d);
1268 qemu_spice_add_memslot(&d->ssd, &memslot, async);
1269 d->guest_slots[slot_id].ptr = (void*)memslot.virt_start;
1270 d->guest_slots[slot_id].size = memslot.virt_end - memslot.virt_start;
1271 d->guest_slots[slot_id].delta = delta;
1272 d->guest_slots[slot_id].active = 1;
1273 return 0;
1276 static void qxl_del_memslot(PCIQXLDevice *d, uint32_t slot_id)
1278 qemu_spice_del_memslot(&d->ssd, MEMSLOT_GROUP_HOST, slot_id);
1279 d->guest_slots[slot_id].active = 0;
1282 static void qxl_reset_memslots(PCIQXLDevice *d)
1284 qxl_spice_reset_memslots(d);
1285 memset(&d->guest_slots, 0, sizeof(d->guest_slots));
1288 static void qxl_reset_surfaces(PCIQXLDevice *d)
1290 trace_qxl_reset_surfaces(d->id);
1291 d->mode = QXL_MODE_UNDEFINED;
1292 qxl_spice_destroy_surfaces(d, QXL_SYNC);
1295 /* can be also called from spice server thread context */
1296 void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id)
1298 uint64_t phys = le64_to_cpu(pqxl);
1299 uint32_t slot = (phys >> (64 - 8)) & 0xff;
1300 uint64_t offset = phys & 0xffffffffffff;
1302 switch (group_id) {
1303 case MEMSLOT_GROUP_HOST:
1304 return (void *)(intptr_t)offset;
1305 case MEMSLOT_GROUP_GUEST:
1306 if (slot >= NUM_MEMSLOTS) {
1307 qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
1308 NUM_MEMSLOTS);
1309 return NULL;
1311 if (!qxl->guest_slots[slot].active) {
1312 qxl_set_guest_bug(qxl, "inactive slot %d\n", slot);
1313 return NULL;
1315 if (offset < qxl->guest_slots[slot].delta) {
1316 qxl_set_guest_bug(qxl,
1317 "slot %d offset %"PRIu64" < delta %"PRIu64"\n",
1318 slot, offset, qxl->guest_slots[slot].delta);
1319 return NULL;
1321 offset -= qxl->guest_slots[slot].delta;
1322 if (offset > qxl->guest_slots[slot].size) {
1323 qxl_set_guest_bug(qxl,
1324 "slot %d offset %"PRIu64" > size %"PRIu64"\n",
1325 slot, offset, qxl->guest_slots[slot].size);
1326 return NULL;
1328 return qxl->guest_slots[slot].ptr + offset;
1330 return NULL;
1333 static void qxl_create_guest_primary_complete(PCIQXLDevice *qxl)
1335 /* for local rendering */
1336 qxl_render_resize(qxl);
1339 static void qxl_create_guest_primary(PCIQXLDevice *qxl, int loadvm,
1340 qxl_async_io async)
1342 QXLDevSurfaceCreate surface;
1343 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
1344 int size;
1345 int requested_height = le32_to_cpu(sc->height);
1346 int requested_stride = le32_to_cpu(sc->stride);
1348 size = abs(requested_stride) * requested_height;
1349 if (size > qxl->vgamem_size) {
1350 qxl_set_guest_bug(qxl, "%s: requested primary larger then framebuffer"
1351 " size", __func__);
1352 return;
1355 if (qxl->mode == QXL_MODE_NATIVE) {
1356 qxl_set_guest_bug(qxl, "%s: nop since already in QXL_MODE_NATIVE",
1357 __func__);
1359 qxl_exit_vga_mode(qxl);
1361 surface.format = le32_to_cpu(sc->format);
1362 surface.height = le32_to_cpu(sc->height);
1363 surface.mem = le64_to_cpu(sc->mem);
1364 surface.position = le32_to_cpu(sc->position);
1365 surface.stride = le32_to_cpu(sc->stride);
1366 surface.width = le32_to_cpu(sc->width);
1367 surface.type = le32_to_cpu(sc->type);
1368 surface.flags = le32_to_cpu(sc->flags);
1369 trace_qxl_create_guest_primary(qxl->id, sc->width, sc->height, sc->mem,
1370 sc->format, sc->position);
1371 trace_qxl_create_guest_primary_rest(qxl->id, sc->stride, sc->type,
1372 sc->flags);
1374 if ((surface.stride & 0x3) != 0) {
1375 qxl_set_guest_bug(qxl, "primary surface stride = %d %% 4 != 0",
1376 surface.stride);
1377 return;
1380 surface.mouse_mode = true;
1381 surface.group_id = MEMSLOT_GROUP_GUEST;
1382 if (loadvm) {
1383 surface.flags |= QXL_SURF_FLAG_KEEP_DATA;
1386 qxl->mode = QXL_MODE_NATIVE;
1387 qxl->cmdflags = 0;
1388 qemu_spice_create_primary_surface(&qxl->ssd, 0, &surface, async);
1390 if (async == QXL_SYNC) {
1391 qxl_create_guest_primary_complete(qxl);
1395 /* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
1396 * done (in QXL_SYNC case), 0 otherwise. */
1397 static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async)
1399 if (d->mode == QXL_MODE_UNDEFINED) {
1400 return 0;
1402 trace_qxl_destroy_primary(d->id);
1403 d->mode = QXL_MODE_UNDEFINED;
1404 qemu_spice_destroy_primary_surface(&d->ssd, 0, async);
1405 qxl_spice_reset_cursor(d);
1406 return 1;
1409 static void qxl_set_mode(PCIQXLDevice *d, int modenr, int loadvm)
1411 pcibus_t start = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
1412 pcibus_t end = d->pci.io_regions[QXL_RAM_RANGE_INDEX].size + start;
1413 QXLMode *mode = d->modes->modes + modenr;
1414 uint64_t devmem = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
1415 QXLMemSlot slot = {
1416 .mem_start = start,
1417 .mem_end = end
1419 QXLSurfaceCreate surface = {
1420 .width = mode->x_res,
1421 .height = mode->y_res,
1422 .stride = -mode->x_res * 4,
1423 .format = SPICE_SURFACE_FMT_32_xRGB,
1424 .flags = loadvm ? QXL_SURF_FLAG_KEEP_DATA : 0,
1425 .mouse_mode = true,
1426 .mem = devmem + d->shadow_rom.draw_area_offset,
1429 trace_qxl_set_mode(d->id, modenr, mode->x_res, mode->y_res, mode->bits,
1430 devmem);
1431 if (!loadvm) {
1432 qxl_hard_reset(d, 0);
1435 d->guest_slots[0].slot = slot;
1436 assert(qxl_add_memslot(d, 0, devmem, QXL_SYNC) == 0);
1438 d->guest_primary.surface = surface;
1439 qxl_create_guest_primary(d, 0, QXL_SYNC);
1441 d->mode = QXL_MODE_COMPAT;
1442 d->cmdflags = QXL_COMMAND_FLAG_COMPAT;
1443 if (mode->bits == 16) {
1444 d->cmdflags |= QXL_COMMAND_FLAG_COMPAT_16BPP;
1446 d->shadow_rom.mode = cpu_to_le32(modenr);
1447 d->rom->mode = cpu_to_le32(modenr);
1448 qxl_rom_set_dirty(d);
1451 static void ioport_write(void *opaque, hwaddr addr,
1452 uint64_t val, unsigned size)
1454 PCIQXLDevice *d = opaque;
1455 uint32_t io_port = addr;
1456 qxl_async_io async = QXL_SYNC;
1457 uint32_t orig_io_port = io_port;
1459 if (d->guest_bug && io_port != QXL_IO_RESET) {
1460 return;
1463 if (d->revision <= QXL_REVISION_STABLE_V10 &&
1464 io_port > QXL_IO_FLUSH_RELEASE) {
1465 qxl_set_guest_bug(d, "unsupported io %d for revision %d\n",
1466 io_port, d->revision);
1467 return;
1470 switch (io_port) {
1471 case QXL_IO_RESET:
1472 case QXL_IO_SET_MODE:
1473 case QXL_IO_MEMSLOT_ADD:
1474 case QXL_IO_MEMSLOT_DEL:
1475 case QXL_IO_CREATE_PRIMARY:
1476 case QXL_IO_UPDATE_IRQ:
1477 case QXL_IO_LOG:
1478 case QXL_IO_MEMSLOT_ADD_ASYNC:
1479 case QXL_IO_CREATE_PRIMARY_ASYNC:
1480 break;
1481 default:
1482 if (d->mode != QXL_MODE_VGA) {
1483 break;
1485 trace_qxl_io_unexpected_vga_mode(d->id,
1486 addr, val, io_port_to_string(io_port));
1487 /* be nice to buggy guest drivers */
1488 if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&
1489 io_port < QXL_IO_RANGE_SIZE) {
1490 qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
1492 return;
1495 /* we change the io_port to avoid ifdeffery in the main switch */
1496 orig_io_port = io_port;
1497 switch (io_port) {
1498 case QXL_IO_UPDATE_AREA_ASYNC:
1499 io_port = QXL_IO_UPDATE_AREA;
1500 goto async_common;
1501 case QXL_IO_MEMSLOT_ADD_ASYNC:
1502 io_port = QXL_IO_MEMSLOT_ADD;
1503 goto async_common;
1504 case QXL_IO_CREATE_PRIMARY_ASYNC:
1505 io_port = QXL_IO_CREATE_PRIMARY;
1506 goto async_common;
1507 case QXL_IO_DESTROY_PRIMARY_ASYNC:
1508 io_port = QXL_IO_DESTROY_PRIMARY;
1509 goto async_common;
1510 case QXL_IO_DESTROY_SURFACE_ASYNC:
1511 io_port = QXL_IO_DESTROY_SURFACE_WAIT;
1512 goto async_common;
1513 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
1514 io_port = QXL_IO_DESTROY_ALL_SURFACES;
1515 goto async_common;
1516 case QXL_IO_FLUSH_SURFACES_ASYNC:
1517 case QXL_IO_MONITORS_CONFIG_ASYNC:
1518 async_common:
1519 async = QXL_ASYNC;
1520 qemu_mutex_lock(&d->async_lock);
1521 if (d->current_async != QXL_UNDEFINED_IO) {
1522 qxl_set_guest_bug(d, "%d async started before last (%d) complete",
1523 io_port, d->current_async);
1524 qemu_mutex_unlock(&d->async_lock);
1525 return;
1527 d->current_async = orig_io_port;
1528 qemu_mutex_unlock(&d->async_lock);
1529 break;
1530 default:
1531 break;
1533 trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode), addr, val, size,
1534 async);
1536 switch (io_port) {
1537 case QXL_IO_UPDATE_AREA:
1539 QXLCookie *cookie = NULL;
1540 QXLRect update = d->ram->update_area;
1542 if (d->ram->update_surface > d->ssd.num_surfaces) {
1543 qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
1544 d->ram->update_surface);
1545 break;
1547 if (update.left >= update.right || update.top >= update.bottom ||
1548 update.left < 0 || update.top < 0) {
1549 qxl_set_guest_bug(d,
1550 "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
1551 update.left, update.top, update.right, update.bottom);
1552 break;
1554 if (async == QXL_ASYNC) {
1555 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
1556 QXL_IO_UPDATE_AREA_ASYNC);
1557 cookie->u.area = update;
1559 qxl_spice_update_area(d, d->ram->update_surface,
1560 cookie ? &cookie->u.area : &update,
1561 NULL, 0, 0, async, cookie);
1562 break;
1564 case QXL_IO_NOTIFY_CMD:
1565 qemu_spice_wakeup(&d->ssd);
1566 break;
1567 case QXL_IO_NOTIFY_CURSOR:
1568 qemu_spice_wakeup(&d->ssd);
1569 break;
1570 case QXL_IO_UPDATE_IRQ:
1571 qxl_update_irq(d);
1572 break;
1573 case QXL_IO_NOTIFY_OOM:
1574 if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
1575 break;
1577 d->oom_running = 1;
1578 qxl_spice_oom(d);
1579 d->oom_running = 0;
1580 break;
1581 case QXL_IO_SET_MODE:
1582 qxl_set_mode(d, val, 0);
1583 break;
1584 case QXL_IO_LOG:
1585 trace_qxl_io_log(d->id, d->ram->log_buf);
1586 if (d->guestdebug) {
1587 fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,
1588 qemu_get_clock_ns(vm_clock), d->ram->log_buf);
1590 break;
1591 case QXL_IO_RESET:
1592 qxl_hard_reset(d, 0);
1593 break;
1594 case QXL_IO_MEMSLOT_ADD:
1595 if (val >= NUM_MEMSLOTS) {
1596 qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
1597 break;
1599 if (d->guest_slots[val].active) {
1600 qxl_set_guest_bug(d,
1601 "QXL_IO_MEMSLOT_ADD: memory slot already active");
1602 break;
1604 d->guest_slots[val].slot = d->ram->mem_slot;
1605 qxl_add_memslot(d, val, 0, async);
1606 break;
1607 case QXL_IO_MEMSLOT_DEL:
1608 if (val >= NUM_MEMSLOTS) {
1609 qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
1610 break;
1612 qxl_del_memslot(d, val);
1613 break;
1614 case QXL_IO_CREATE_PRIMARY:
1615 if (val != 0) {
1616 qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
1617 async);
1618 goto cancel_async;
1620 d->guest_primary.surface = d->ram->create_surface;
1621 qxl_create_guest_primary(d, 0, async);
1622 break;
1623 case QXL_IO_DESTROY_PRIMARY:
1624 if (val != 0) {
1625 qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
1626 async);
1627 goto cancel_async;
1629 if (!qxl_destroy_primary(d, async)) {
1630 trace_qxl_io_destroy_primary_ignored(d->id,
1631 qxl_mode_to_string(d->mode));
1632 goto cancel_async;
1634 break;
1635 case QXL_IO_DESTROY_SURFACE_WAIT:
1636 if (val >= d->ssd.num_surfaces) {
1637 qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
1638 "%" PRIu64 " >= NUM_SURFACES", async, val);
1639 goto cancel_async;
1641 qxl_spice_destroy_surface_wait(d, val, async);
1642 break;
1643 case QXL_IO_FLUSH_RELEASE: {
1644 QXLReleaseRing *ring = &d->ram->release_ring;
1645 if (ring->prod - ring->cons + 1 == ring->num_items) {
1646 fprintf(stderr,
1647 "ERROR: no flush, full release ring [p%d,%dc]\n",
1648 ring->prod, ring->cons);
1650 qxl_push_free_res(d, 1 /* flush */);
1651 break;
1653 case QXL_IO_FLUSH_SURFACES_ASYNC:
1654 qxl_spice_flush_surfaces_async(d);
1655 break;
1656 case QXL_IO_DESTROY_ALL_SURFACES:
1657 d->mode = QXL_MODE_UNDEFINED;
1658 qxl_spice_destroy_surfaces(d, async);
1659 break;
1660 case QXL_IO_MONITORS_CONFIG_ASYNC:
1661 qxl_spice_monitors_config_async(d, 0);
1662 break;
1663 default:
1664 qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);
1666 return;
1667 cancel_async:
1668 if (async) {
1669 qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
1670 qemu_mutex_lock(&d->async_lock);
1671 d->current_async = QXL_UNDEFINED_IO;
1672 qemu_mutex_unlock(&d->async_lock);
1676 static uint64_t ioport_read(void *opaque, hwaddr addr,
1677 unsigned size)
1679 PCIQXLDevice *qxl = opaque;
1681 trace_qxl_io_read_unexpected(qxl->id);
1682 return 0xff;
1685 static const MemoryRegionOps qxl_io_ops = {
1686 .read = ioport_read,
1687 .write = ioport_write,
1688 .valid = {
1689 .min_access_size = 1,
1690 .max_access_size = 1,
1694 static void pipe_read(void *opaque)
1696 PCIQXLDevice *d = opaque;
1697 char dummy;
1698 int len;
1700 do {
1701 len = read(d->pipe[0], &dummy, sizeof(dummy));
1702 } while (len == sizeof(dummy));
1703 qxl_update_irq(d);
1706 static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
1708 uint32_t old_pending;
1709 uint32_t le_events = cpu_to_le32(events);
1711 trace_qxl_send_events(d->id, events);
1712 if (!qemu_spice_display_is_running(&d->ssd)) {
1713 /* spice-server tracks guest running state and should not do this */
1714 fprintf(stderr, "%s: spice-server bug: guest stopped, ignoring\n",
1715 __func__);
1716 trace_qxl_send_events_vm_stopped(d->id, events);
1717 return;
1719 old_pending = __sync_fetch_and_or(&d->ram->int_pending, le_events);
1720 if ((old_pending & le_events) == le_events) {
1721 return;
1723 if (qemu_thread_is_self(&d->main)) {
1724 qxl_update_irq(d);
1725 } else {
1726 if (write(d->pipe[1], d, 1) != 1) {
1727 dprint(d, 1, "%s: write to pipe failed\n", __func__);
1732 static void init_pipe_signaling(PCIQXLDevice *d)
1734 if (pipe(d->pipe) < 0) {
1735 fprintf(stderr, "%s:%s: qxl pipe creation failed\n",
1736 __FILE__, __func__);
1737 exit(1);
1739 fcntl(d->pipe[0], F_SETFL, O_NONBLOCK);
1740 fcntl(d->pipe[1], F_SETFL, O_NONBLOCK);
1741 fcntl(d->pipe[0], F_SETOWN, getpid());
1743 qemu_thread_get_self(&d->main);
1744 qemu_set_fd_handler(d->pipe[0], pipe_read, NULL, d);
1747 /* graphics console */
1749 static void qxl_hw_update(void *opaque)
1751 PCIQXLDevice *qxl = opaque;
1752 VGACommonState *vga = &qxl->vga;
1754 switch (qxl->mode) {
1755 case QXL_MODE_VGA:
1756 vga->hw_ops->gfx_update(vga);
1757 break;
1758 case QXL_MODE_COMPAT:
1759 case QXL_MODE_NATIVE:
1760 qxl_render_update(qxl);
1761 break;
1762 default:
1763 break;
1767 static void qxl_hw_invalidate(void *opaque)
1769 PCIQXLDevice *qxl = opaque;
1770 VGACommonState *vga = &qxl->vga;
1772 if (qxl->mode == QXL_MODE_VGA) {
1773 vga->hw_ops->invalidate(vga);
1774 return;
1778 static void qxl_hw_text_update(void *opaque, console_ch_t *chardata)
1780 PCIQXLDevice *qxl = opaque;
1781 VGACommonState *vga = &qxl->vga;
1783 if (qxl->mode == QXL_MODE_VGA) {
1784 vga->hw_ops->text_update(vga, chardata);
1785 return;
1789 static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
1791 uintptr_t vram_start;
1792 int i;
1794 if (qxl->mode != QXL_MODE_NATIVE && qxl->mode != QXL_MODE_COMPAT) {
1795 return;
1798 /* dirty the primary surface */
1799 qxl_set_dirty(&qxl->vga.vram, qxl->shadow_rom.draw_area_offset,
1800 qxl->shadow_rom.surface0_area_size);
1802 vram_start = (uintptr_t)memory_region_get_ram_ptr(&qxl->vram_bar);
1804 /* dirty the off-screen surfaces */
1805 for (i = 0; i < qxl->ssd.num_surfaces; i++) {
1806 QXLSurfaceCmd *cmd;
1807 intptr_t surface_offset;
1808 int surface_size;
1810 if (qxl->guest_surfaces.cmds[i] == 0) {
1811 continue;
1814 cmd = qxl_phys2virt(qxl, qxl->guest_surfaces.cmds[i],
1815 MEMSLOT_GROUP_GUEST);
1816 assert(cmd);
1817 assert(cmd->type == QXL_SURFACE_CMD_CREATE);
1818 surface_offset = (intptr_t)qxl_phys2virt(qxl,
1819 cmd->u.surface_create.data,
1820 MEMSLOT_GROUP_GUEST);
1821 assert(surface_offset);
1822 surface_offset -= vram_start;
1823 surface_size = cmd->u.surface_create.height *
1824 abs(cmd->u.surface_create.stride);
1825 trace_qxl_surfaces_dirty(qxl->id, i, (int)surface_offset, surface_size);
1826 qxl_set_dirty(&qxl->vram_bar, surface_offset, surface_size);
1830 static void qxl_vm_change_state_handler(void *opaque, int running,
1831 RunState state)
1833 PCIQXLDevice *qxl = opaque;
1835 if (running) {
1837 * if qxl_send_events was called from spice server context before
1838 * migration ended, qxl_update_irq for these events might not have been
1839 * called
1841 qxl_update_irq(qxl);
1842 } else {
1843 /* make sure surfaces are saved before migration */
1844 qxl_dirty_surfaces(qxl);
1848 /* display change listener */
1850 static void display_update(DisplayChangeListener *dcl,
1851 int x, int y, int w, int h)
1853 PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
1855 if (qxl->mode == QXL_MODE_VGA) {
1856 qemu_spice_display_update(&qxl->ssd, x, y, w, h);
1860 static void display_switch(DisplayChangeListener *dcl,
1861 struct DisplaySurface *surface)
1863 PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
1865 qxl->ssd.ds = surface;
1866 if (qxl->mode == QXL_MODE_VGA) {
1867 qemu_spice_display_switch(&qxl->ssd, surface);
1871 static void display_refresh(DisplayChangeListener *dcl)
1873 PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);
1875 if (qxl->mode == QXL_MODE_VGA) {
1876 qemu_spice_display_refresh(&qxl->ssd);
1877 } else {
1878 qemu_mutex_lock(&qxl->ssd.lock);
1879 qemu_spice_cursor_refresh_unlocked(&qxl->ssd);
1880 qemu_mutex_unlock(&qxl->ssd.lock);
1884 static DisplayChangeListenerOps display_listener_ops = {
1885 .dpy_name = "spice/qxl",
1886 .dpy_gfx_update = display_update,
1887 .dpy_gfx_switch = display_switch,
1888 .dpy_refresh = display_refresh,
1891 static void qxl_init_ramsize(PCIQXLDevice *qxl)
1893 /* vga mode framebuffer / primary surface (bar 0, first part) */
1894 if (qxl->vgamem_size_mb < 8) {
1895 qxl->vgamem_size_mb = 8;
1897 qxl->vgamem_size = qxl->vgamem_size_mb * 1024 * 1024;
1899 /* vga ram (bar 0, total) */
1900 if (qxl->ram_size_mb != -1) {
1901 qxl->vga.vram_size = qxl->ram_size_mb * 1024 * 1024;
1903 if (qxl->vga.vram_size < qxl->vgamem_size * 2) {
1904 qxl->vga.vram_size = qxl->vgamem_size * 2;
1907 /* vram32 (surfaces, 32bit, bar 1) */
1908 if (qxl->vram32_size_mb != -1) {
1909 qxl->vram32_size = qxl->vram32_size_mb * 1024 * 1024;
1911 if (qxl->vram32_size < 4096) {
1912 qxl->vram32_size = 4096;
1915 /* vram (surfaces, 64bit, bar 4+5) */
1916 if (qxl->vram_size_mb != -1) {
1917 qxl->vram_size = qxl->vram_size_mb * 1024 * 1024;
1919 if (qxl->vram_size < qxl->vram32_size) {
1920 qxl->vram_size = qxl->vram32_size;
1923 if (qxl->revision == 1) {
1924 qxl->vram32_size = 4096;
1925 qxl->vram_size = 4096;
1927 qxl->vgamem_size = msb_mask(qxl->vgamem_size * 2 - 1);
1928 qxl->vga.vram_size = msb_mask(qxl->vga.vram_size * 2 - 1);
1929 qxl->vram32_size = msb_mask(qxl->vram32_size * 2 - 1);
1930 qxl->vram_size = msb_mask(qxl->vram_size * 2 - 1);
1933 static int qxl_init_common(PCIQXLDevice *qxl)
1935 uint8_t* config = qxl->pci.config;
1936 uint32_t pci_device_rev;
1937 uint32_t io_size;
1939 qxl->mode = QXL_MODE_UNDEFINED;
1940 qxl->generation = 1;
1941 qxl->num_memslots = NUM_MEMSLOTS;
1942 qemu_mutex_init(&qxl->track_lock);
1943 qemu_mutex_init(&qxl->async_lock);
1944 qxl->current_async = QXL_UNDEFINED_IO;
1945 qxl->guest_bug = 0;
1947 switch (qxl->revision) {
1948 case 1: /* spice 0.4 -- qxl-1 */
1949 pci_device_rev = QXL_REVISION_STABLE_V04;
1950 io_size = 8;
1951 break;
1952 case 2: /* spice 0.6 -- qxl-2 */
1953 pci_device_rev = QXL_REVISION_STABLE_V06;
1954 io_size = 16;
1955 break;
1956 case 3: /* qxl-3 */
1957 pci_device_rev = QXL_REVISION_STABLE_V10;
1958 io_size = 32; /* PCI region size must be pow2 */
1959 break;
1960 case 4: /* qxl-4 */
1961 pci_device_rev = QXL_REVISION_STABLE_V12;
1962 io_size = msb_mask(QXL_IO_RANGE_SIZE * 2 - 1);
1963 break;
1964 default:
1965 error_report("Invalid revision %d for qxl device (max %d)",
1966 qxl->revision, QXL_DEFAULT_REVISION);
1967 return -1;
1970 pci_set_byte(&config[PCI_REVISION_ID], pci_device_rev);
1971 pci_set_byte(&config[PCI_INTERRUPT_PIN], 1);
1973 qxl->rom_size = qxl_rom_size();
1974 memory_region_init_ram(&qxl->rom_bar, "qxl.vrom", qxl->rom_size);
1975 vmstate_register_ram(&qxl->rom_bar, &qxl->pci.qdev);
1976 init_qxl_rom(qxl);
1977 init_qxl_ram(qxl);
1979 qxl->guest_surfaces.cmds = g_new0(QXLPHYSICAL, qxl->ssd.num_surfaces);
1980 memory_region_init_ram(&qxl->vram_bar, "qxl.vram", qxl->vram_size);
1981 vmstate_register_ram(&qxl->vram_bar, &qxl->pci.qdev);
1982 memory_region_init_alias(&qxl->vram32_bar, "qxl.vram32", &qxl->vram_bar,
1983 0, qxl->vram32_size);
1985 memory_region_init_io(&qxl->io_bar, &qxl_io_ops, qxl,
1986 "qxl-ioports", io_size);
1987 if (qxl->id == 0) {
1988 vga_dirty_log_start(&qxl->vga);
1990 memory_region_set_flush_coalesced(&qxl->io_bar);
1993 pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX,
1994 PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar);
1996 pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX,
1997 PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar);
1999 pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX,
2000 PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram);
2002 pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX,
2003 PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vram32_bar);
2005 if (qxl->vram32_size < qxl->vram_size) {
2007 * Make the 64bit vram bar show up only in case it is
2008 * configured to be larger than the 32bit vram bar.
2010 pci_register_bar(&qxl->pci, QXL_VRAM64_RANGE_INDEX,
2011 PCI_BASE_ADDRESS_SPACE_MEMORY |
2012 PCI_BASE_ADDRESS_MEM_TYPE_64 |
2013 PCI_BASE_ADDRESS_MEM_PREFETCH,
2014 &qxl->vram_bar);
2017 /* print pci bar details */
2018 dprint(qxl, 1, "ram/%s: %d MB [region 0]\n",
2019 qxl->id == 0 ? "pri" : "sec",
2020 qxl->vga.vram_size / (1024*1024));
2021 dprint(qxl, 1, "vram/32: %d MB [region 1]\n",
2022 qxl->vram32_size / (1024*1024));
2023 dprint(qxl, 1, "vram/64: %d MB %s\n",
2024 qxl->vram_size / (1024*1024),
2025 qxl->vram32_size < qxl->vram_size ? "[region 4]" : "[unmapped]");
2027 qxl->ssd.qxl.base.sif = &qxl_interface.base;
2028 qxl->ssd.qxl.id = qxl->id;
2029 if (qemu_spice_add_interface(&qxl->ssd.qxl.base) != 0) {
2030 error_report("qxl interface %d.%d not supported by spice-server",
2031 SPICE_INTERFACE_QXL_MAJOR, SPICE_INTERFACE_QXL_MINOR);
2032 return -1;
2034 qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl);
2036 init_pipe_signaling(qxl);
2037 qxl_reset_state(qxl);
2039 qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);
2041 return 0;
2044 static const GraphicHwOps qxl_ops = {
2045 .invalidate = qxl_hw_invalidate,
2046 .gfx_update = qxl_hw_update,
2047 .text_update = qxl_hw_text_update,
2050 static int qxl_init_primary(PCIDevice *dev)
2052 PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev);
2053 VGACommonState *vga = &qxl->vga;
2054 PortioList *qxl_vga_port_list = g_new(PortioList, 1);
2055 DisplayState *ds;
2056 int rc;
2058 qxl->id = 0;
2059 qxl_init_ramsize(qxl);
2060 vga->vram_size_mb = qxl->vga.vram_size >> 20;
2061 vga_common_init(vga);
2062 vga_init(vga, pci_address_space(dev), pci_address_space_io(dev), false);
2063 portio_list_init(qxl_vga_port_list, qxl_vga_portio_list, vga, "vga");
2064 portio_list_add(qxl_vga_port_list, pci_address_space_io(dev), 0x3b0);
2066 vga->con = graphic_console_init(&qxl_ops, qxl);
2067 qemu_spice_display_init_common(&qxl->ssd);
2069 rc = qxl_init_common(qxl);
2070 if (rc != 0) {
2071 return rc;
2074 qxl->ssd.dcl.ops = &display_listener_ops;
2075 qxl->ssd.dcl.con = vga->con;
2076 ds = qemu_console_displaystate(vga->con);
2077 register_displaychangelistener(ds, &qxl->ssd.dcl);
2078 return rc;
2081 static int qxl_init_secondary(PCIDevice *dev)
2083 static int device_id = 1;
2084 PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev);
2086 qxl->id = device_id++;
2087 qxl_init_ramsize(qxl);
2088 memory_region_init_ram(&qxl->vga.vram, "qxl.vgavram", qxl->vga.vram_size);
2089 vmstate_register_ram(&qxl->vga.vram, &qxl->pci.qdev);
2090 qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram);
2091 qxl->vga.con = graphic_console_init(&qxl_ops, qxl);
2093 return qxl_init_common(qxl);
2096 static void qxl_pre_save(void *opaque)
2098 PCIQXLDevice* d = opaque;
2099 uint8_t *ram_start = d->vga.vram_ptr;
2101 trace_qxl_pre_save(d->id);
2102 if (d->last_release == NULL) {
2103 d->last_release_offset = 0;
2104 } else {
2105 d->last_release_offset = (uint8_t *)d->last_release - ram_start;
2107 assert(d->last_release_offset < d->vga.vram_size);
2110 static int qxl_pre_load(void *opaque)
2112 PCIQXLDevice* d = opaque;
2114 trace_qxl_pre_load(d->id);
2115 qxl_hard_reset(d, 1);
2116 qxl_exit_vga_mode(d);
2117 return 0;
2120 static void qxl_create_memslots(PCIQXLDevice *d)
2122 int i;
2124 for (i = 0; i < NUM_MEMSLOTS; i++) {
2125 if (!d->guest_slots[i].active) {
2126 continue;
2128 qxl_add_memslot(d, i, 0, QXL_SYNC);
2132 static int qxl_post_load(void *opaque, int version)
2134 PCIQXLDevice* d = opaque;
2135 uint8_t *ram_start = d->vga.vram_ptr;
2136 QXLCommandExt *cmds;
2137 int in, out, newmode;
2139 assert(d->last_release_offset < d->vga.vram_size);
2140 if (d->last_release_offset == 0) {
2141 d->last_release = NULL;
2142 } else {
2143 d->last_release = (QXLReleaseInfo *)(ram_start + d->last_release_offset);
2146 d->modes = (QXLModes*)((uint8_t*)d->rom + d->rom->modes_offset);
2148 trace_qxl_post_load(d->id, qxl_mode_to_string(d->mode));
2149 newmode = d->mode;
2150 d->mode = QXL_MODE_UNDEFINED;
2152 switch (newmode) {
2153 case QXL_MODE_UNDEFINED:
2154 qxl_create_memslots(d);
2155 break;
2156 case QXL_MODE_VGA:
2157 qxl_create_memslots(d);
2158 qxl_enter_vga_mode(d);
2159 break;
2160 case QXL_MODE_NATIVE:
2161 qxl_create_memslots(d);
2162 qxl_create_guest_primary(d, 1, QXL_SYNC);
2164 /* replay surface-create and cursor-set commands */
2165 cmds = g_malloc0(sizeof(QXLCommandExt) * (d->ssd.num_surfaces + 1));
2166 for (in = 0, out = 0; in < d->ssd.num_surfaces; in++) {
2167 if (d->guest_surfaces.cmds[in] == 0) {
2168 continue;
2170 cmds[out].cmd.data = d->guest_surfaces.cmds[in];
2171 cmds[out].cmd.type = QXL_CMD_SURFACE;
2172 cmds[out].group_id = MEMSLOT_GROUP_GUEST;
2173 out++;
2175 if (d->guest_cursor) {
2176 cmds[out].cmd.data = d->guest_cursor;
2177 cmds[out].cmd.type = QXL_CMD_CURSOR;
2178 cmds[out].group_id = MEMSLOT_GROUP_GUEST;
2179 out++;
2181 qxl_spice_loadvm_commands(d, cmds, out);
2182 g_free(cmds);
2183 if (d->guest_monitors_config) {
2184 qxl_spice_monitors_config_async(d, 1);
2186 break;
2187 case QXL_MODE_COMPAT:
2188 /* note: no need to call qxl_create_memslots, qxl_set_mode
2189 * creates the mem slot. */
2190 qxl_set_mode(d, d->shadow_rom.mode, 1);
2191 break;
2193 return 0;
2196 #define QXL_SAVE_VERSION 21
2198 static bool qxl_monitors_config_needed(void *opaque)
2200 PCIQXLDevice *qxl = opaque;
2202 return qxl->guest_monitors_config != 0;
2206 static VMStateDescription qxl_memslot = {
2207 .name = "qxl-memslot",
2208 .version_id = QXL_SAVE_VERSION,
2209 .minimum_version_id = QXL_SAVE_VERSION,
2210 .fields = (VMStateField[]) {
2211 VMSTATE_UINT64(slot.mem_start, struct guest_slots),
2212 VMSTATE_UINT64(slot.mem_end, struct guest_slots),
2213 VMSTATE_UINT32(active, struct guest_slots),
2214 VMSTATE_END_OF_LIST()
2218 static VMStateDescription qxl_surface = {
2219 .name = "qxl-surface",
2220 .version_id = QXL_SAVE_VERSION,
2221 .minimum_version_id = QXL_SAVE_VERSION,
2222 .fields = (VMStateField[]) {
2223 VMSTATE_UINT32(width, QXLSurfaceCreate),
2224 VMSTATE_UINT32(height, QXLSurfaceCreate),
2225 VMSTATE_INT32(stride, QXLSurfaceCreate),
2226 VMSTATE_UINT32(format, QXLSurfaceCreate),
2227 VMSTATE_UINT32(position, QXLSurfaceCreate),
2228 VMSTATE_UINT32(mouse_mode, QXLSurfaceCreate),
2229 VMSTATE_UINT32(flags, QXLSurfaceCreate),
2230 VMSTATE_UINT32(type, QXLSurfaceCreate),
2231 VMSTATE_UINT64(mem, QXLSurfaceCreate),
2232 VMSTATE_END_OF_LIST()
2236 static VMStateDescription qxl_vmstate_monitors_config = {
2237 .name = "qxl/monitors-config",
2238 .version_id = 1,
2239 .minimum_version_id = 1,
2240 .fields = (VMStateField[]) {
2241 VMSTATE_UINT64(guest_monitors_config, PCIQXLDevice),
2242 VMSTATE_END_OF_LIST()
2246 static VMStateDescription qxl_vmstate = {
2247 .name = "qxl",
2248 .version_id = QXL_SAVE_VERSION,
2249 .minimum_version_id = QXL_SAVE_VERSION,
2250 .pre_save = qxl_pre_save,
2251 .pre_load = qxl_pre_load,
2252 .post_load = qxl_post_load,
2253 .fields = (VMStateField[]) {
2254 VMSTATE_PCI_DEVICE(pci, PCIQXLDevice),
2255 VMSTATE_STRUCT(vga, PCIQXLDevice, 0, vmstate_vga_common, VGACommonState),
2256 VMSTATE_UINT32(shadow_rom.mode, PCIQXLDevice),
2257 VMSTATE_UINT32(num_free_res, PCIQXLDevice),
2258 VMSTATE_UINT32(last_release_offset, PCIQXLDevice),
2259 VMSTATE_UINT32(mode, PCIQXLDevice),
2260 VMSTATE_UINT32(ssd.unique, PCIQXLDevice),
2261 VMSTATE_INT32_EQUAL(num_memslots, PCIQXLDevice),
2262 VMSTATE_STRUCT_ARRAY(guest_slots, PCIQXLDevice, NUM_MEMSLOTS, 0,
2263 qxl_memslot, struct guest_slots),
2264 VMSTATE_STRUCT(guest_primary.surface, PCIQXLDevice, 0,
2265 qxl_surface, QXLSurfaceCreate),
2266 VMSTATE_INT32_EQUAL(ssd.num_surfaces, PCIQXLDevice),
2267 VMSTATE_VARRAY_INT32(guest_surfaces.cmds, PCIQXLDevice,
2268 ssd.num_surfaces, 0,
2269 vmstate_info_uint64, uint64_t),
2270 VMSTATE_UINT64(guest_cursor, PCIQXLDevice),
2271 VMSTATE_END_OF_LIST()
2273 .subsections = (VMStateSubsection[]) {
2275 .vmsd = &qxl_vmstate_monitors_config,
2276 .needed = qxl_monitors_config_needed,
2277 }, {
2278 /* empty */
2283 static Property qxl_properties[] = {
2284 DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size,
2285 64 * 1024 * 1024),
2286 DEFINE_PROP_UINT32("vram_size", PCIQXLDevice, vram32_size,
2287 64 * 1024 * 1024),
2288 DEFINE_PROP_UINT32("revision", PCIQXLDevice, revision,
2289 QXL_DEFAULT_REVISION),
2290 DEFINE_PROP_UINT32("debug", PCIQXLDevice, debug, 0),
2291 DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice, guestdebug, 0),
2292 DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice, cmdlog, 0),
2293 DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice, ram_size_mb, -1),
2294 DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice, vram32_size_mb, -1),
2295 DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice, vram_size_mb, -1),
2296 DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice, vgamem_size_mb, 16),
2297 DEFINE_PROP_INT32("surfaces", PCIQXLDevice, ssd.num_surfaces, 1024),
2298 DEFINE_PROP_END_OF_LIST(),
2301 static void qxl_primary_class_init(ObjectClass *klass, void *data)
2303 DeviceClass *dc = DEVICE_CLASS(klass);
2304 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2306 k->no_hotplug = 1;
2307 k->init = qxl_init_primary;
2308 k->romfile = "vgabios-qxl.bin";
2309 k->vendor_id = REDHAT_PCI_VENDOR_ID;
2310 k->device_id = QXL_DEVICE_ID_STABLE;
2311 k->class_id = PCI_CLASS_DISPLAY_VGA;
2312 dc->desc = "Spice QXL GPU (primary, vga compatible)";
2313 dc->reset = qxl_reset_handler;
2314 dc->vmsd = &qxl_vmstate;
2315 dc->props = qxl_properties;
2318 static const TypeInfo qxl_primary_info = {
2319 .name = "qxl-vga",
2320 .parent = TYPE_PCI_DEVICE,
2321 .instance_size = sizeof(PCIQXLDevice),
2322 .class_init = qxl_primary_class_init,
2325 static void qxl_secondary_class_init(ObjectClass *klass, void *data)
2327 DeviceClass *dc = DEVICE_CLASS(klass);
2328 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2330 k->init = qxl_init_secondary;
2331 k->vendor_id = REDHAT_PCI_VENDOR_ID;
2332 k->device_id = QXL_DEVICE_ID_STABLE;
2333 k->class_id = PCI_CLASS_DISPLAY_OTHER;
2334 dc->desc = "Spice QXL GPU (secondary)";
2335 dc->reset = qxl_reset_handler;
2336 dc->vmsd = &qxl_vmstate;
2337 dc->props = qxl_properties;
2340 static const TypeInfo qxl_secondary_info = {
2341 .name = "qxl",
2342 .parent = TYPE_PCI_DEVICE,
2343 .instance_size = sizeof(PCIQXLDevice),
2344 .class_init = qxl_secondary_class_init,
2347 static void qxl_register_types(void)
2349 type_register_static(&qxl_primary_info);
2350 type_register_static(&qxl_secondary_info);
2353 type_init(qxl_register_types)