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