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