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