2 * Copyright (C) 2010 Red Hat, Inc.
4 * written by Yaniv Kamay, Izik Eidus, Gerd Hoffmann
5 * maintained by Gerd Hoffmann <kraxel@redhat.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/units.h"
25 #include "qapi/error.h"
26 #include "qemu/timer.h"
27 #include "qemu/queue.h"
28 #include "qemu/atomic.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/module.h"
31 #include "hw/qdev-properties.h"
32 #include "sysemu/runstate.h"
33 #include "migration/vmstate.h"
38 #undef SPICE_RING_CONS_ITEM
39 #define SPICE_RING_CONS_ITEM(qxl, r, ret) { \
40 uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \
41 if (cons >= ARRAY_SIZE((r)->items)) { \
42 qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
43 "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \
46 ret = &(r)->items[cons].el; \
51 #define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
53 #define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9"
55 #define QXL_MODE(_x, _y, _b, _o) \
59 .stride = (_x) * (_b) / 8, \
60 .x_mili = PIXEL_SIZE * (_x), \
61 .y_mili = PIXEL_SIZE * (_y), \
65 #define QXL_MODE_16_32(x_res, y_res, orientation) \
66 QXL_MODE(x_res, y_res, 16, orientation), \
67 QXL_MODE(x_res, y_res, 32, orientation)
69 #define QXL_MODE_EX(x_res, y_res) \
70 QXL_MODE_16_32(x_res, y_res, 0), \
71 QXL_MODE_16_32(x_res, y_res, 1)
73 static QXLMode qxl_modes
[] = {
74 QXL_MODE_EX(640, 480),
75 QXL_MODE_EX(800, 480),
76 QXL_MODE_EX(800, 600),
77 QXL_MODE_EX(832, 624),
78 QXL_MODE_EX(960, 640),
79 QXL_MODE_EX(1024, 600),
80 QXL_MODE_EX(1024, 768),
81 QXL_MODE_EX(1152, 864),
82 QXL_MODE_EX(1152, 870),
83 QXL_MODE_EX(1280, 720),
84 QXL_MODE_EX(1280, 760),
85 QXL_MODE_EX(1280, 768),
86 QXL_MODE_EX(1280, 800),
87 QXL_MODE_EX(1280, 960),
88 QXL_MODE_EX(1280, 1024),
89 QXL_MODE_EX(1360, 768),
90 QXL_MODE_EX(1366, 768),
91 QXL_MODE_EX(1400, 1050),
92 QXL_MODE_EX(1440, 900),
93 QXL_MODE_EX(1600, 900),
94 QXL_MODE_EX(1600, 1200),
95 QXL_MODE_EX(1680, 1050),
96 QXL_MODE_EX(1920, 1080),
97 /* these modes need more than 8 MB video memory */
98 QXL_MODE_EX(1920, 1200),
99 QXL_MODE_EX(1920, 1440),
100 QXL_MODE_EX(2000, 2000),
101 QXL_MODE_EX(2048, 1536),
102 QXL_MODE_EX(2048, 2048),
103 QXL_MODE_EX(2560, 1440),
104 QXL_MODE_EX(2560, 1600),
105 /* these modes need more than 16 MB video memory */
106 QXL_MODE_EX(2560, 2048),
107 QXL_MODE_EX(2800, 2100),
108 QXL_MODE_EX(3200, 2400),
109 /* these modes need more than 32 MB video memory */
110 QXL_MODE_EX(3840, 2160), /* 4k mainstream */
111 QXL_MODE_EX(4096, 2160), /* 4k */
112 /* these modes need more than 64 MB video memory */
113 QXL_MODE_EX(7680, 4320), /* 8k mainstream */
114 /* these modes need more than 128 MB video memory */
115 QXL_MODE_EX(8192, 4320), /* 8k */
118 static void qxl_send_events(PCIQXLDevice
*d
, uint32_t events
);
119 static int qxl_destroy_primary(PCIQXLDevice
*d
, qxl_async_io async
);
120 static void qxl_reset_memslots(PCIQXLDevice
*d
);
121 static void qxl_reset_surfaces(PCIQXLDevice
*d
);
122 static void qxl_ring_set_dirty(PCIQXLDevice
*qxl
);
124 static void qxl_hw_update(void *opaque
);
126 void qxl_set_guest_bug(PCIQXLDevice
*qxl
, const char *msg
, ...)
128 trace_qxl_set_guest_bug(qxl
->id
);
129 qxl_send_events(qxl
, QXL_INTERRUPT_ERROR
);
131 if (qxl
->guestdebug
) {
134 fprintf(stderr
, "qxl-%d: guest bug: ", qxl
->id
);
135 vfprintf(stderr
, msg
, ap
);
136 fprintf(stderr
, "\n");
141 static void qxl_clear_guest_bug(PCIQXLDevice
*qxl
)
146 void qxl_spice_update_area(PCIQXLDevice
*qxl
, uint32_t surface_id
,
147 struct QXLRect
*area
, struct QXLRect
*dirty_rects
,
148 uint32_t num_dirty_rects
,
149 uint32_t clear_dirty_region
,
150 qxl_async_io async
, struct QXLCookie
*cookie
)
152 trace_qxl_spice_update_area(qxl
->id
, surface_id
, area
->left
, area
->right
,
153 area
->top
, area
->bottom
);
154 trace_qxl_spice_update_area_rest(qxl
->id
, num_dirty_rects
,
156 if (async
== QXL_SYNC
) {
157 spice_qxl_update_area(&qxl
->ssd
.qxl
, surface_id
, area
,
158 dirty_rects
, num_dirty_rects
, clear_dirty_region
);
160 assert(cookie
!= NULL
);
161 spice_qxl_update_area_async(&qxl
->ssd
.qxl
, surface_id
, area
,
162 clear_dirty_region
, (uintptr_t)cookie
);
166 static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice
*qxl
,
169 trace_qxl_spice_destroy_surface_wait_complete(qxl
->id
, id
);
170 qemu_mutex_lock(&qxl
->track_lock
);
171 qxl
->guest_surfaces
.cmds
[id
] = 0;
172 qxl
->guest_surfaces
.count
--;
173 qemu_mutex_unlock(&qxl
->track_lock
);
176 static void qxl_spice_destroy_surface_wait(PCIQXLDevice
*qxl
, uint32_t id
,
181 trace_qxl_spice_destroy_surface_wait(qxl
->id
, id
, async
);
183 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
184 QXL_IO_DESTROY_SURFACE_ASYNC
);
185 cookie
->u
.surface_id
= id
;
186 spice_qxl_destroy_surface_async(&qxl
->ssd
.qxl
, id
, (uintptr_t)cookie
);
188 spice_qxl_destroy_surface_wait(&qxl
->ssd
.qxl
, id
);
189 qxl_spice_destroy_surface_wait_complete(qxl
, id
);
193 static void qxl_spice_flush_surfaces_async(PCIQXLDevice
*qxl
)
195 trace_qxl_spice_flush_surfaces_async(qxl
->id
, qxl
->guest_surfaces
.count
,
197 spice_qxl_flush_surfaces_async(&qxl
->ssd
.qxl
,
198 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
199 QXL_IO_FLUSH_SURFACES_ASYNC
));
202 void qxl_spice_loadvm_commands(PCIQXLDevice
*qxl
, struct QXLCommandExt
*ext
,
205 trace_qxl_spice_loadvm_commands(qxl
->id
, ext
, count
);
206 spice_qxl_loadvm_commands(&qxl
->ssd
.qxl
, ext
, count
);
209 void qxl_spice_oom(PCIQXLDevice
*qxl
)
211 trace_qxl_spice_oom(qxl
->id
);
212 spice_qxl_oom(&qxl
->ssd
.qxl
);
215 void qxl_spice_reset_memslots(PCIQXLDevice
*qxl
)
217 trace_qxl_spice_reset_memslots(qxl
->id
);
218 spice_qxl_reset_memslots(&qxl
->ssd
.qxl
);
221 static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice
*qxl
)
223 trace_qxl_spice_destroy_surfaces_complete(qxl
->id
);
224 qemu_mutex_lock(&qxl
->track_lock
);
225 memset(qxl
->guest_surfaces
.cmds
, 0,
226 sizeof(qxl
->guest_surfaces
.cmds
[0]) * qxl
->ssd
.num_surfaces
);
227 qxl
->guest_surfaces
.count
= 0;
228 qemu_mutex_unlock(&qxl
->track_lock
);
231 static void qxl_spice_destroy_surfaces(PCIQXLDevice
*qxl
, qxl_async_io async
)
233 trace_qxl_spice_destroy_surfaces(qxl
->id
, async
);
235 spice_qxl_destroy_surfaces_async(&qxl
->ssd
.qxl
,
236 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
237 QXL_IO_DESTROY_ALL_SURFACES_ASYNC
));
239 spice_qxl_destroy_surfaces(&qxl
->ssd
.qxl
);
240 qxl_spice_destroy_surfaces_complete(qxl
);
244 static void qxl_spice_monitors_config_async(PCIQXLDevice
*qxl
, int replay
)
246 QXLMonitorsConfig
*cfg
;
248 trace_qxl_spice_monitors_config(qxl
->id
);
251 * don't use QXL_COOKIE_TYPE_IO:
252 * - we are not running yet (post_load), we will assert
254 * - this is not a guest io, but a reply, so async_io isn't set.
256 spice_qxl_monitors_config_async(&qxl
->ssd
.qxl
,
257 qxl
->guest_monitors_config
,
259 (uintptr_t)qxl_cookie_new(
260 QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG
,
263 #if SPICE_SERVER_VERSION < 0x000e02 /* release 0.14.2 */
264 if (qxl
->max_outputs
) {
265 spice_qxl_set_max_monitors(&qxl
->ssd
.qxl
, qxl
->max_outputs
);
268 qxl
->guest_monitors_config
= qxl
->ram
->monitors_config
;
269 spice_qxl_monitors_config_async(&qxl
->ssd
.qxl
,
270 qxl
->ram
->monitors_config
,
272 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
273 QXL_IO_MONITORS_CONFIG_ASYNC
));
276 cfg
= qxl_phys2virt(qxl
, qxl
->guest_monitors_config
, MEMSLOT_GROUP_GUEST
,
277 sizeof(QXLMonitorsConfig
));
278 if (cfg
!= NULL
&& cfg
->count
== 1) {
279 qxl
->guest_primary
.resized
= 1;
280 qxl
->guest_head0_width
= cfg
->heads
[0].width
;
281 qxl
->guest_head0_height
= cfg
->heads
[0].height
;
283 qxl
->guest_head0_width
= 0;
284 qxl
->guest_head0_height
= 0;
288 void qxl_spice_reset_image_cache(PCIQXLDevice
*qxl
)
290 trace_qxl_spice_reset_image_cache(qxl
->id
);
291 spice_qxl_reset_image_cache(&qxl
->ssd
.qxl
);
294 void qxl_spice_reset_cursor(PCIQXLDevice
*qxl
)
296 trace_qxl_spice_reset_cursor(qxl
->id
);
297 spice_qxl_reset_cursor(&qxl
->ssd
.qxl
);
298 qemu_mutex_lock(&qxl
->track_lock
);
299 qxl
->guest_cursor
= 0;
300 qemu_mutex_unlock(&qxl
->track_lock
);
301 if (qxl
->ssd
.cursor
) {
302 cursor_put(qxl
->ssd
.cursor
);
304 qxl
->ssd
.cursor
= cursor_builtin_hidden();
307 static uint32_t qxl_crc32(const uint8_t *p
, unsigned len
)
310 * zlib xors the seed with 0xffffffff, and xors the result
311 * again with 0xffffffff; Both are not done with linux's crc32,
312 * which we want to be compatible with, so undo that.
314 return crc32(0xffffffff, p
, len
) ^ 0xffffffff;
317 static ram_addr_t
qxl_rom_size(void)
319 #define QXL_REQUIRED_SZ (sizeof(QXLRom) + sizeof(QXLModes) + sizeof(qxl_modes))
320 #define QXL_ROM_SZ 8192
322 QEMU_BUILD_BUG_ON(QXL_REQUIRED_SZ
> QXL_ROM_SZ
);
323 return QEMU_ALIGN_UP(QXL_REQUIRED_SZ
, qemu_real_host_page_size());
326 static void init_qxl_rom(PCIQXLDevice
*d
)
328 QXLRom
*rom
= memory_region_get_ram_ptr(&d
->rom_bar
);
329 QXLModes
*modes
= (QXLModes
*)(rom
+ 1);
330 uint32_t ram_header_size
;
331 uint32_t surface0_area_size
;
336 memset(rom
, 0, d
->rom_size
);
338 rom
->magic
= cpu_to_le32(QXL_ROM_MAGIC
);
339 rom
->id
= cpu_to_le32(d
->id
);
340 rom
->log_level
= cpu_to_le32(d
->guestdebug
);
341 rom
->modes_offset
= cpu_to_le32(sizeof(QXLRom
));
343 rom
->slot_gen_bits
= MEMSLOT_GENERATION_BITS
;
344 rom
->slot_id_bits
= MEMSLOT_SLOT_BITS
;
345 rom
->slots_start
= 1;
346 rom
->slots_end
= NUM_MEMSLOTS
- 1;
347 rom
->n_surfaces
= cpu_to_le32(d
->ssd
.num_surfaces
);
349 for (i
= 0, n
= 0; i
< ARRAY_SIZE(qxl_modes
); i
++) {
350 fb
= qxl_modes
[i
].y_res
* qxl_modes
[i
].stride
;
351 if (fb
> d
->vgamem_size
) {
354 modes
->modes
[n
].id
= cpu_to_le32(i
);
355 modes
->modes
[n
].x_res
= cpu_to_le32(qxl_modes
[i
].x_res
);
356 modes
->modes
[n
].y_res
= cpu_to_le32(qxl_modes
[i
].y_res
);
357 modes
->modes
[n
].bits
= cpu_to_le32(qxl_modes
[i
].bits
);
358 modes
->modes
[n
].stride
= cpu_to_le32(qxl_modes
[i
].stride
);
359 modes
->modes
[n
].x_mili
= cpu_to_le32(qxl_modes
[i
].x_mili
);
360 modes
->modes
[n
].y_mili
= cpu_to_le32(qxl_modes
[i
].y_mili
);
361 modes
->modes
[n
].orientation
= cpu_to_le32(qxl_modes
[i
].orientation
);
364 modes
->n_modes
= cpu_to_le32(n
);
366 ram_header_size
= ALIGN(sizeof(QXLRam
), 4096);
367 surface0_area_size
= ALIGN(d
->vgamem_size
, 4096);
368 num_pages
= d
->vga
.vram_size
;
369 num_pages
-= ram_header_size
;
370 num_pages
-= surface0_area_size
;
371 num_pages
= num_pages
/ QXL_PAGE_SIZE
;
373 assert(ram_header_size
+ surface0_area_size
<= d
->vga
.vram_size
);
375 rom
->draw_area_offset
= cpu_to_le32(0);
376 rom
->surface0_area_size
= cpu_to_le32(surface0_area_size
);
377 rom
->pages_offset
= cpu_to_le32(surface0_area_size
);
378 rom
->num_pages
= cpu_to_le32(num_pages
);
379 rom
->ram_header_offset
= cpu_to_le32(d
->vga
.vram_size
- ram_header_size
);
381 if (d
->xres
&& d
->yres
) {
382 /* needs linux kernel 4.12+ to work */
383 rom
->client_monitors_config
.count
= 1;
384 rom
->client_monitors_config
.heads
[0].left
= 0;
385 rom
->client_monitors_config
.heads
[0].top
= 0;
386 rom
->client_monitors_config
.heads
[0].right
= cpu_to_le32(d
->xres
);
387 rom
->client_monitors_config
.heads
[0].bottom
= cpu_to_le32(d
->yres
);
388 rom
->client_monitors_config_crc
= qxl_crc32(
389 (const uint8_t *)&rom
->client_monitors_config
,
390 sizeof(rom
->client_monitors_config
));
393 d
->shadow_rom
= *rom
;
398 static void init_qxl_ram(PCIQXLDevice
*d
)
402 QXLReleaseRing
*ring
;
404 buf
= d
->vga
.vram_ptr
;
405 d
->ram
= (QXLRam
*)(buf
+ le32_to_cpu(d
->shadow_rom
.ram_header_offset
));
406 d
->ram
->magic
= cpu_to_le32(QXL_RAM_MAGIC
);
407 d
->ram
->int_pending
= cpu_to_le32(0);
408 d
->ram
->int_mask
= cpu_to_le32(0);
409 d
->ram
->update_surface
= 0;
410 d
->ram
->monitors_config
= 0;
411 SPICE_RING_INIT(&d
->ram
->cmd_ring
);
412 SPICE_RING_INIT(&d
->ram
->cursor_ring
);
413 SPICE_RING_INIT(&d
->ram
->release_ring
);
415 ring
= &d
->ram
->release_ring
;
416 prod
= ring
->prod
& SPICE_RING_INDEX_MASK(ring
);
417 assert(prod
< ARRAY_SIZE(ring
->items
));
418 ring
->items
[prod
].el
= 0;
420 qxl_ring_set_dirty(d
);
423 /* can be called from spice server thread context */
424 static void qxl_set_dirty(MemoryRegion
*mr
, ram_addr_t addr
, ram_addr_t end
)
426 memory_region_set_dirty(mr
, addr
, end
- addr
);
429 static void qxl_rom_set_dirty(PCIQXLDevice
*qxl
)
431 qxl_set_dirty(&qxl
->rom_bar
, 0, qxl
->rom_size
);
434 /* called from spice server thread context only */
435 static void qxl_ram_set_dirty(PCIQXLDevice
*qxl
, void *ptr
)
437 void *base
= qxl
->vga
.vram_ptr
;
441 assert(offset
< qxl
->vga
.vram_size
);
442 qxl_set_dirty(&qxl
->vga
.vram
, offset
, offset
+ 3);
445 /* can be called from spice server thread context */
446 static void qxl_ring_set_dirty(PCIQXLDevice
*qxl
)
448 ram_addr_t addr
= qxl
->shadow_rom
.ram_header_offset
;
449 ram_addr_t end
= qxl
->vga
.vram_size
;
450 qxl_set_dirty(&qxl
->vga
.vram
, addr
, end
);
454 * keep track of some command state, for savevm/loadvm.
455 * called from spice server thread context only
457 static int qxl_track_command(PCIQXLDevice
*qxl
, struct QXLCommandExt
*ext
)
459 switch (le32_to_cpu(ext
->cmd
.type
)) {
460 case QXL_CMD_SURFACE
:
462 QXLSurfaceCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
,
463 sizeof(QXLSurfaceCmd
));
468 uint32_t id
= le32_to_cpu(cmd
->surface_id
);
470 if (id
>= qxl
->ssd
.num_surfaces
) {
471 qxl_set_guest_bug(qxl
, "QXL_CMD_SURFACE id %d >= %d", id
,
472 qxl
->ssd
.num_surfaces
);
475 if (cmd
->type
== QXL_SURFACE_CMD_CREATE
&&
476 (cmd
->u
.surface_create
.stride
& 0x03) != 0) {
477 qxl_set_guest_bug(qxl
, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
478 cmd
->u
.surface_create
.stride
);
481 WITH_QEMU_LOCK_GUARD(&qxl
->track_lock
) {
482 if (cmd
->type
== QXL_SURFACE_CMD_CREATE
) {
483 qxl
->guest_surfaces
.cmds
[id
] = ext
->cmd
.data
;
484 qxl
->guest_surfaces
.count
++;
485 if (qxl
->guest_surfaces
.max
< qxl
->guest_surfaces
.count
) {
486 qxl
->guest_surfaces
.max
= qxl
->guest_surfaces
.count
;
489 if (cmd
->type
== QXL_SURFACE_CMD_DESTROY
) {
490 qxl
->guest_surfaces
.cmds
[id
] = 0;
491 qxl
->guest_surfaces
.count
--;
498 QXLCursorCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
,
499 sizeof(QXLCursorCmd
));
504 if (cmd
->type
== QXL_CURSOR_SET
) {
505 qemu_mutex_lock(&qxl
->track_lock
);
506 qxl
->guest_cursor
= ext
->cmd
.data
;
507 qemu_mutex_unlock(&qxl
->track_lock
);
509 if (cmd
->type
== QXL_CURSOR_HIDE
) {
510 qemu_mutex_lock(&qxl
->track_lock
);
511 qxl
->guest_cursor
= 0;
512 qemu_mutex_unlock(&qxl
->track_lock
);
520 /* spice display interface callbacks */
522 static void interface_attached_worker(QXLInstance
*sin
)
524 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
526 trace_qxl_interface_attach_worker(qxl
->id
);
529 #if !(SPICE_HAS_ATTACHED_WORKER)
530 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
532 interface_attached_worker(sin
);
536 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
538 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
540 trace_qxl_interface_set_compression_level(qxl
->id
, level
);
541 qxl
->shadow_rom
.compression_level
= cpu_to_le32(level
);
542 qxl
->rom
->compression_level
= cpu_to_le32(level
);
543 qxl_rom_set_dirty(qxl
);
546 #if SPICE_NEEDS_SET_MM_TIME
547 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
549 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
551 if (!qemu_spice_display_is_running(&qxl
->ssd
)) {
555 trace_qxl_interface_set_mm_time(qxl
->id
, mm_time
);
556 qxl
->shadow_rom
.mm_clock
= cpu_to_le32(mm_time
);
557 qxl
->rom
->mm_clock
= cpu_to_le32(mm_time
);
558 qxl_rom_set_dirty(qxl
);
562 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
564 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
566 trace_qxl_interface_get_init_info(qxl
->id
);
567 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
568 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
569 info
->num_memslots
= NUM_MEMSLOTS
;
570 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
571 info
->internal_groupslot_id
= 0;
573 le32_to_cpu(qxl
->shadow_rom
.num_pages
) << QXL_PAGE_BITS
;
574 info
->n_surfaces
= qxl
->ssd
.num_surfaces
;
577 static const char *qxl_mode_to_string(int mode
)
580 case QXL_MODE_COMPAT
:
582 case QXL_MODE_NATIVE
:
584 case QXL_MODE_UNDEFINED
:
592 static const char *io_port_to_string(uint32_t io_port
)
594 if (io_port
>= QXL_IO_RANGE_SIZE
) {
595 return "out of range";
597 static const char *io_port_to_string
[QXL_IO_RANGE_SIZE
+ 1] = {
598 [QXL_IO_NOTIFY_CMD
] = "QXL_IO_NOTIFY_CMD",
599 [QXL_IO_NOTIFY_CURSOR
] = "QXL_IO_NOTIFY_CURSOR",
600 [QXL_IO_UPDATE_AREA
] = "QXL_IO_UPDATE_AREA",
601 [QXL_IO_UPDATE_IRQ
] = "QXL_IO_UPDATE_IRQ",
602 [QXL_IO_NOTIFY_OOM
] = "QXL_IO_NOTIFY_OOM",
603 [QXL_IO_RESET
] = "QXL_IO_RESET",
604 [QXL_IO_SET_MODE
] = "QXL_IO_SET_MODE",
605 [QXL_IO_LOG
] = "QXL_IO_LOG",
606 [QXL_IO_MEMSLOT_ADD
] = "QXL_IO_MEMSLOT_ADD",
607 [QXL_IO_MEMSLOT_DEL
] = "QXL_IO_MEMSLOT_DEL",
608 [QXL_IO_DETACH_PRIMARY
] = "QXL_IO_DETACH_PRIMARY",
609 [QXL_IO_ATTACH_PRIMARY
] = "QXL_IO_ATTACH_PRIMARY",
610 [QXL_IO_CREATE_PRIMARY
] = "QXL_IO_CREATE_PRIMARY",
611 [QXL_IO_DESTROY_PRIMARY
] = "QXL_IO_DESTROY_PRIMARY",
612 [QXL_IO_DESTROY_SURFACE_WAIT
] = "QXL_IO_DESTROY_SURFACE_WAIT",
613 [QXL_IO_DESTROY_ALL_SURFACES
] = "QXL_IO_DESTROY_ALL_SURFACES",
614 [QXL_IO_UPDATE_AREA_ASYNC
] = "QXL_IO_UPDATE_AREA_ASYNC",
615 [QXL_IO_MEMSLOT_ADD_ASYNC
] = "QXL_IO_MEMSLOT_ADD_ASYNC",
616 [QXL_IO_CREATE_PRIMARY_ASYNC
] = "QXL_IO_CREATE_PRIMARY_ASYNC",
617 [QXL_IO_DESTROY_PRIMARY_ASYNC
] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
618 [QXL_IO_DESTROY_SURFACE_ASYNC
] = "QXL_IO_DESTROY_SURFACE_ASYNC",
619 [QXL_IO_DESTROY_ALL_SURFACES_ASYNC
]
620 = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
621 [QXL_IO_FLUSH_SURFACES_ASYNC
] = "QXL_IO_FLUSH_SURFACES_ASYNC",
622 [QXL_IO_FLUSH_RELEASE
] = "QXL_IO_FLUSH_RELEASE",
623 [QXL_IO_MONITORS_CONFIG_ASYNC
] = "QXL_IO_MONITORS_CONFIG_ASYNC",
625 return io_port_to_string
[io_port
];
628 /* called from spice server thread context only */
629 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
631 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
632 SimpleSpiceUpdate
*update
;
633 QXLCommandRing
*ring
;
637 trace_qxl_ring_command_check(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
642 qemu_mutex_lock(&qxl
->ssd
.lock
);
643 update
= QTAILQ_FIRST(&qxl
->ssd
.updates
);
644 if (update
!= NULL
) {
645 QTAILQ_REMOVE(&qxl
->ssd
.updates
, update
, next
);
649 qemu_mutex_unlock(&qxl
->ssd
.lock
);
651 trace_qxl_ring_command_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
652 qxl_log_command(qxl
, "vga", ext
);
655 case QXL_MODE_COMPAT
:
656 case QXL_MODE_NATIVE
:
657 case QXL_MODE_UNDEFINED
:
658 ring
= &qxl
->ram
->cmd_ring
;
659 if (qxl
->guest_bug
|| SPICE_RING_IS_EMPTY(ring
)) {
662 SPICE_RING_CONS_ITEM(qxl
, ring
, cmd
);
667 ext
->group_id
= MEMSLOT_GROUP_GUEST
;
668 ext
->flags
= qxl
->cmdflags
;
669 SPICE_RING_POP(ring
, notify
);
670 qxl_ring_set_dirty(qxl
);
672 qxl_send_events(qxl
, QXL_INTERRUPT_DISPLAY
);
674 qxl
->guest_primary
.commands
++;
675 qxl_track_command(qxl
, ext
);
676 qxl_log_command(qxl
, "cmd", ext
);
677 trace_qxl_ring_command_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
684 /* called from spice server thread context only */
685 static int interface_req_cmd_notification(QXLInstance
*sin
)
687 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
690 trace_qxl_ring_command_req_notification(qxl
->id
);
692 case QXL_MODE_COMPAT
:
693 case QXL_MODE_NATIVE
:
694 case QXL_MODE_UNDEFINED
:
695 SPICE_RING_CONS_WAIT(&qxl
->ram
->cmd_ring
, wait
);
696 qxl_ring_set_dirty(qxl
);
705 /* called from spice server thread context only */
706 static inline void qxl_push_free_res(PCIQXLDevice
*d
, int flush
)
708 QXLReleaseRing
*ring
= &d
->ram
->release_ring
;
712 #define QXL_FREE_BUNCH_SIZE 32
714 if (ring
->prod
- ring
->cons
+ 1 == ring
->num_items
) {
715 /* ring full -- can't push */
718 if (!flush
&& d
->oom_running
) {
719 /* collect everything from oom handler before pushing */
722 if (!flush
&& d
->num_free_res
< QXL_FREE_BUNCH_SIZE
) {
723 /* collect a bit more before pushing */
727 SPICE_RING_PUSH(ring
, notify
);
728 trace_qxl_ring_res_push(d
->id
, qxl_mode_to_string(d
->mode
),
729 d
->guest_surfaces
.count
, d
->num_free_res
,
730 d
->last_release
, notify
? "yes" : "no");
731 trace_qxl_ring_res_push_rest(d
->id
, ring
->prod
- ring
->cons
,
732 ring
->num_items
, ring
->prod
, ring
->cons
);
734 qxl_send_events(d
, QXL_INTERRUPT_DISPLAY
);
737 ring
= &d
->ram
->release_ring
;
738 prod
= ring
->prod
& SPICE_RING_INDEX_MASK(ring
);
739 if (prod
>= ARRAY_SIZE(ring
->items
)) {
740 qxl_set_guest_bug(d
, "SPICE_RING_PROD_ITEM indices mismatch "
741 "%u >= %zu", prod
, ARRAY_SIZE(ring
->items
));
744 ring
->items
[prod
].el
= 0;
746 d
->last_release
= NULL
;
747 qxl_ring_set_dirty(d
);
750 /* called from spice server thread context only */
751 static void interface_release_resource(QXLInstance
*sin
,
752 QXLReleaseInfoExt ext
)
754 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
755 QXLReleaseRing
*ring
;
762 if (ext
.group_id
== MEMSLOT_GROUP_HOST
) {
763 /* host group -> vga mode update request */
764 QXLCommandExt
*cmdext
= (void *)(intptr_t)(ext
.info
->id
);
765 SimpleSpiceUpdate
*update
;
766 g_assert(cmdext
->cmd
.type
== QXL_CMD_DRAW
);
767 update
= container_of(cmdext
, SimpleSpiceUpdate
, ext
);
768 qemu_spice_destroy_update(&qxl
->ssd
, update
);
773 * ext->info points into guest-visible memory
774 * pci bar 0, $command.release_info
776 ring
= &qxl
->ram
->release_ring
;
777 prod
= ring
->prod
& SPICE_RING_INDEX_MASK(ring
);
778 if (prod
>= ARRAY_SIZE(ring
->items
)) {
779 qxl_set_guest_bug(qxl
, "SPICE_RING_PROD_ITEM indices mismatch "
780 "%u >= %zu", prod
, ARRAY_SIZE(ring
->items
));
783 if (ring
->items
[prod
].el
== 0) {
784 /* stick head into the ring */
787 qxl_ram_set_dirty(qxl
, &ext
.info
->next
);
788 ring
->items
[prod
].el
= id
;
789 qxl_ring_set_dirty(qxl
);
791 /* append item to the list */
792 qxl
->last_release
->next
= ext
.info
->id
;
793 qxl_ram_set_dirty(qxl
, &qxl
->last_release
->next
);
795 qxl_ram_set_dirty(qxl
, &ext
.info
->next
);
797 qxl
->last_release
= ext
.info
;
799 trace_qxl_ring_res_put(qxl
->id
, qxl
->num_free_res
);
800 qxl_push_free_res(qxl
, 0);
803 /* called from spice server thread context only */
804 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
806 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
811 trace_qxl_ring_cursor_check(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
814 case QXL_MODE_COMPAT
:
815 case QXL_MODE_NATIVE
:
816 case QXL_MODE_UNDEFINED
:
817 ring
= &qxl
->ram
->cursor_ring
;
818 if (SPICE_RING_IS_EMPTY(ring
)) {
821 SPICE_RING_CONS_ITEM(qxl
, ring
, cmd
);
826 ext
->group_id
= MEMSLOT_GROUP_GUEST
;
827 ext
->flags
= qxl
->cmdflags
;
828 SPICE_RING_POP(ring
, notify
);
829 qxl_ring_set_dirty(qxl
);
831 qxl_send_events(qxl
, QXL_INTERRUPT_CURSOR
);
833 qxl
->guest_primary
.commands
++;
834 qxl_track_command(qxl
, ext
);
835 qxl_log_command(qxl
, "csr", ext
);
837 qxl_render_cursor(qxl
, ext
);
839 trace_qxl_ring_cursor_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
846 /* called from spice server thread context only */
847 static int interface_req_cursor_notification(QXLInstance
*sin
)
849 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
852 trace_qxl_ring_cursor_req_notification(qxl
->id
);
854 case QXL_MODE_COMPAT
:
855 case QXL_MODE_NATIVE
:
856 case QXL_MODE_UNDEFINED
:
857 SPICE_RING_CONS_WAIT(&qxl
->ram
->cursor_ring
, wait
);
858 qxl_ring_set_dirty(qxl
);
867 /* called from spice server thread context */
868 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
871 * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
872 * use by xf86-video-qxl and is defined out in the qxl windows driver.
873 * Probably was at some earlier version that is prior to git start (2009),
874 * and is still guest trigerrable.
876 fprintf(stderr
, "%s: deprecated\n", __func__
);
879 /* called from spice server thread context only */
880 static int interface_flush_resources(QXLInstance
*sin
)
882 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
885 ret
= qxl
->num_free_res
;
887 qxl_push_free_res(qxl
, 1);
892 static void qxl_create_guest_primary_complete(PCIQXLDevice
*d
);
894 /* called from spice server thread context only */
895 static void interface_async_complete_io(PCIQXLDevice
*qxl
, QXLCookie
*cookie
)
897 uint32_t current_async
;
899 qemu_mutex_lock(&qxl
->async_lock
);
900 current_async
= qxl
->current_async
;
901 qxl
->current_async
= QXL_UNDEFINED_IO
;
902 qemu_mutex_unlock(&qxl
->async_lock
);
904 trace_qxl_interface_async_complete_io(qxl
->id
, current_async
, cookie
);
906 fprintf(stderr
, "qxl: %s: error, cookie is NULL\n", __func__
);
909 if (cookie
&& current_async
!= cookie
->io
) {
911 "qxl: %s: error: current_async = %d != %"
912 PRId64
" = cookie->io\n", __func__
, current_async
, cookie
->io
);
914 switch (current_async
) {
915 case QXL_IO_MEMSLOT_ADD_ASYNC
:
916 case QXL_IO_DESTROY_PRIMARY_ASYNC
:
917 case QXL_IO_UPDATE_AREA_ASYNC
:
918 case QXL_IO_FLUSH_SURFACES_ASYNC
:
919 case QXL_IO_MONITORS_CONFIG_ASYNC
:
921 case QXL_IO_CREATE_PRIMARY_ASYNC
:
922 qxl_create_guest_primary_complete(qxl
);
924 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC
:
925 qxl_spice_destroy_surfaces_complete(qxl
);
927 case QXL_IO_DESTROY_SURFACE_ASYNC
:
928 qxl_spice_destroy_surface_wait_complete(qxl
, cookie
->u
.surface_id
);
931 fprintf(stderr
, "qxl: %s: unexpected current_async %u\n", __func__
,
934 qxl_send_events(qxl
, QXL_INTERRUPT_IO_CMD
);
937 /* called from spice server thread context only */
938 static void interface_update_area_complete(QXLInstance
*sin
,
940 QXLRect
*dirty
, uint32_t num_updated_rects
)
942 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
946 QEMU_LOCK_GUARD(&qxl
->ssd
.lock
);
947 if (surface_id
!= 0 || !num_updated_rects
||
948 !qxl
->render_update_cookie_num
) {
951 trace_qxl_interface_update_area_complete(qxl
->id
, surface_id
, dirty
->left
,
952 dirty
->right
, dirty
->top
, dirty
->bottom
);
953 trace_qxl_interface_update_area_complete_rest(qxl
->id
, num_updated_rects
);
954 if (qxl
->num_dirty_rects
+ num_updated_rects
> QXL_NUM_DIRTY_RECTS
) {
956 * overflow - treat this as a full update. Not expected to be common.
958 trace_qxl_interface_update_area_complete_overflow(qxl
->id
,
959 QXL_NUM_DIRTY_RECTS
);
960 qxl
->guest_primary
.resized
= 1;
962 if (qxl
->guest_primary
.resized
) {
964 * Don't bother copying or scheduling the bh since we will flip
965 * the whole area anyway on completion of the update_area async call
969 qxl_i
= qxl
->num_dirty_rects
;
970 for (i
= 0; i
< num_updated_rects
; i
++) {
971 qxl
->dirty
[qxl_i
++] = dirty
[i
];
973 qxl
->num_dirty_rects
+= num_updated_rects
;
974 trace_qxl_interface_update_area_complete_schedule_bh(qxl
->id
,
975 qxl
->num_dirty_rects
);
976 qemu_bh_schedule(qxl
->update_area_bh
);
979 /* called from spice server thread context only */
980 static void interface_async_complete(QXLInstance
*sin
, uint64_t cookie_token
)
982 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
983 QXLCookie
*cookie
= (QXLCookie
*)(uintptr_t)cookie_token
;
985 switch (cookie
->type
) {
986 case QXL_COOKIE_TYPE_IO
:
987 interface_async_complete_io(qxl
, cookie
);
990 case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA
:
991 qxl_render_update_area_done(qxl
, cookie
);
993 case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG
:
996 fprintf(stderr
, "qxl: %s: unexpected cookie type %d\n",
997 __func__
, cookie
->type
);
1002 /* called from spice server thread context only */
1003 static void interface_set_client_capabilities(QXLInstance
*sin
,
1004 uint8_t client_present
,
1007 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
1009 if (qxl
->revision
< 4) {
1010 trace_qxl_set_client_capabilities_unsupported_by_revision(qxl
->id
,
1015 if (runstate_check(RUN_STATE_INMIGRATE
) ||
1016 runstate_check(RUN_STATE_POSTMIGRATE
)) {
1020 qxl
->shadow_rom
.client_present
= client_present
;
1021 memcpy(qxl
->shadow_rom
.client_capabilities
, caps
,
1022 sizeof(qxl
->shadow_rom
.client_capabilities
));
1023 qxl
->rom
->client_present
= client_present
;
1024 memcpy(qxl
->rom
->client_capabilities
, caps
,
1025 sizeof(qxl
->rom
->client_capabilities
));
1026 qxl_rom_set_dirty(qxl
);
1028 qxl_send_events(qxl
, QXL_INTERRUPT_CLIENT
);
1031 static bool qxl_rom_monitors_config_changed(QXLRom
*rom
,
1032 VDAgentMonitorsConfig
*monitors_config
,
1033 unsigned int max_outputs
)
1036 unsigned int monitors_count
;
1038 monitors_count
= MIN(monitors_config
->num_of_monitors
, max_outputs
);
1040 if (rom
->client_monitors_config
.count
!= monitors_count
) {
1044 for (i
= 0 ; i
< rom
->client_monitors_config
.count
; ++i
) {
1045 VDAgentMonConfig
*monitor
= &monitors_config
->monitors
[i
];
1046 QXLURect
*rect
= &rom
->client_monitors_config
.heads
[i
];
1047 /* monitor->depth ignored */
1048 if ((rect
->left
!= monitor
->x
) ||
1049 (rect
->top
!= monitor
->y
) ||
1050 (rect
->right
!= monitor
->x
+ monitor
->width
) ||
1051 (rect
->bottom
!= monitor
->y
+ monitor
->height
)) {
1059 /* called from main context only */
1060 static int interface_client_monitors_config(QXLInstance
*sin
,
1061 VDAgentMonitorsConfig
*monitors_config
)
1063 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
1064 QXLRom
*rom
= memory_region_get_ram_ptr(&qxl
->rom_bar
);
1066 unsigned max_outputs
= ARRAY_SIZE(rom
->client_monitors_config
.heads
);
1067 bool config_changed
= false;
1069 if (qxl
->revision
< 4) {
1070 trace_qxl_client_monitors_config_unsupported_by_device(qxl
->id
,
1075 * Older windows drivers set int_mask to 0 when their ISR is called,
1076 * then later set it to ~0. So it doesn't relate to the actual interrupts
1077 * handled. However, they are old, so clearly they don't support this
1080 if (qxl
->ram
->int_mask
== 0 || qxl
->ram
->int_mask
== ~0 ||
1081 !(qxl
->ram
->int_mask
& QXL_INTERRUPT_CLIENT_MONITORS_CONFIG
)) {
1082 trace_qxl_client_monitors_config_unsupported_by_guest(qxl
->id
,
1087 if (!monitors_config
) {
1091 /* limit number of outputs based on setting limit */
1092 if (qxl
->max_outputs
&& qxl
->max_outputs
<= max_outputs
) {
1093 max_outputs
= qxl
->max_outputs
;
1096 config_changed
= qxl_rom_monitors_config_changed(rom
,
1100 memset(&rom
->client_monitors_config
, 0,
1101 sizeof(rom
->client_monitors_config
));
1102 rom
->client_monitors_config
.count
= monitors_config
->num_of_monitors
;
1103 /* monitors_config->flags ignored */
1104 if (rom
->client_monitors_config
.count
>= max_outputs
) {
1105 trace_qxl_client_monitors_config_capped(qxl
->id
,
1106 monitors_config
->num_of_monitors
,
1108 rom
->client_monitors_config
.count
= max_outputs
;
1110 for (i
= 0 ; i
< rom
->client_monitors_config
.count
; ++i
) {
1111 VDAgentMonConfig
*monitor
= &monitors_config
->monitors
[i
];
1112 QXLURect
*rect
= &rom
->client_monitors_config
.heads
[i
];
1113 /* monitor->depth ignored */
1114 rect
->left
= monitor
->x
;
1115 rect
->top
= monitor
->y
;
1116 rect
->right
= monitor
->x
+ monitor
->width
;
1117 rect
->bottom
= monitor
->y
+ monitor
->height
;
1119 rom
->client_monitors_config_crc
= qxl_crc32(
1120 (const uint8_t *)&rom
->client_monitors_config
,
1121 sizeof(rom
->client_monitors_config
));
1122 trace_qxl_client_monitors_config_crc(qxl
->id
,
1123 sizeof(rom
->client_monitors_config
),
1124 rom
->client_monitors_config_crc
);
1126 trace_qxl_interrupt_client_monitors_config(qxl
->id
,
1127 rom
->client_monitors_config
.count
,
1128 rom
->client_monitors_config
.heads
);
1129 if (config_changed
) {
1130 qxl_send_events(qxl
, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG
);
1135 static const QXLInterface qxl_interface
= {
1136 .base
.type
= SPICE_INTERFACE_QXL
,
1137 .base
.description
= "qxl gpu",
1138 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
1139 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
1141 #if SPICE_HAS_ATTACHED_WORKER
1142 .attached_worker
= interface_attached_worker
,
1144 .attache_worker
= interface_attach_worker
,
1147 .set_compression_level
= interface_set_compression_level
,
1148 #if SPICE_NEEDS_SET_MM_TIME
1149 .set_mm_time
= interface_set_mm_time
,
1151 .get_init_info
= interface_get_init_info
,
1153 /* the callbacks below are called from spice server thread context */
1154 .get_command
= interface_get_command
,
1155 .req_cmd_notification
= interface_req_cmd_notification
,
1156 .release_resource
= interface_release_resource
,
1157 .get_cursor_command
= interface_get_cursor_command
,
1158 .req_cursor_notification
= interface_req_cursor_notification
,
1159 .notify_update
= interface_notify_update
,
1160 .flush_resources
= interface_flush_resources
,
1161 .async_complete
= interface_async_complete
,
1162 .update_area_complete
= interface_update_area_complete
,
1163 .set_client_capabilities
= interface_set_client_capabilities
,
1164 .client_monitors_config
= interface_client_monitors_config
,
1167 static const GraphicHwOps qxl_ops
= {
1168 .gfx_update
= qxl_hw_update
,
1169 .gfx_update_async
= true,
1172 static void qxl_enter_vga_mode(PCIQXLDevice
*d
)
1174 if (d
->mode
== QXL_MODE_VGA
) {
1177 trace_qxl_enter_vga_mode(d
->id
);
1178 spice_qxl_driver_unload(&d
->ssd
.qxl
);
1179 graphic_console_set_hwops(d
->ssd
.dcl
.con
, d
->vga
.hw_ops
, &d
->vga
);
1180 update_displaychangelistener(&d
->ssd
.dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
1181 qemu_spice_create_host_primary(&d
->ssd
);
1182 d
->mode
= QXL_MODE_VGA
;
1183 qemu_spice_display_switch(&d
->ssd
, d
->ssd
.ds
);
1184 vga_dirty_log_start(&d
->vga
);
1185 graphic_hw_update(d
->vga
.con
);
1188 static void qxl_exit_vga_mode(PCIQXLDevice
*d
)
1190 if (d
->mode
!= QXL_MODE_VGA
) {
1193 trace_qxl_exit_vga_mode(d
->id
);
1194 graphic_console_set_hwops(d
->ssd
.dcl
.con
, &qxl_ops
, d
);
1195 update_displaychangelistener(&d
->ssd
.dcl
, GUI_REFRESH_INTERVAL_IDLE
);
1196 vga_dirty_log_stop(&d
->vga
);
1197 qxl_destroy_primary(d
, QXL_SYNC
);
1200 static void qxl_update_irq(PCIQXLDevice
*d
)
1202 uint32_t pending
= le32_to_cpu(d
->ram
->int_pending
);
1203 uint32_t mask
= le32_to_cpu(d
->ram
->int_mask
);
1204 int level
= !!(pending
& mask
);
1205 pci_set_irq(&d
->pci
, level
);
1206 qxl_ring_set_dirty(d
);
1209 static void qxl_check_state(PCIQXLDevice
*d
)
1211 QXLRam
*ram
= d
->ram
;
1212 int spice_display_running
= qemu_spice_display_is_running(&d
->ssd
);
1214 assert(!spice_display_running
|| SPICE_RING_IS_EMPTY(&ram
->cmd_ring
));
1215 assert(!spice_display_running
|| SPICE_RING_IS_EMPTY(&ram
->cursor_ring
));
1218 static void qxl_reset_state(PCIQXLDevice
*d
)
1220 QXLRom
*rom
= d
->rom
;
1223 d
->shadow_rom
.update_id
= cpu_to_le32(0);
1224 *rom
= d
->shadow_rom
;
1225 qxl_rom_set_dirty(d
);
1227 d
->num_free_res
= 0;
1228 d
->last_release
= NULL
;
1229 memset(&d
->ssd
.dirty
, 0, sizeof(d
->ssd
.dirty
));
1233 static void qxl_soft_reset(PCIQXLDevice
*d
)
1235 trace_qxl_soft_reset(d
->id
);
1237 qxl_clear_guest_bug(d
);
1238 qemu_mutex_lock(&d
->async_lock
);
1239 d
->current_async
= QXL_UNDEFINED_IO
;
1240 qemu_mutex_unlock(&d
->async_lock
);
1243 qxl_enter_vga_mode(d
);
1245 d
->mode
= QXL_MODE_UNDEFINED
;
1249 static void qxl_hard_reset(PCIQXLDevice
*d
, int loadvm
)
1251 bool startstop
= qemu_spice_display_is_running(&d
->ssd
);
1253 trace_qxl_hard_reset(d
->id
, loadvm
);
1256 qemu_spice_display_stop();
1259 qxl_spice_reset_cursor(d
);
1260 qxl_spice_reset_image_cache(d
);
1261 qxl_reset_surfaces(d
);
1262 qxl_reset_memslots(d
);
1264 /* pre loadvm reset must not touch QXLRam. This lives in
1265 * device memory, is migrated together with RAM and thus
1266 * already loaded at this point */
1270 qemu_spice_create_host_memslot(&d
->ssd
);
1274 qemu_spice_display_start();
1278 static void qxl_reset_handler(DeviceState
*dev
)
1280 PCIQXLDevice
*d
= PCI_QXL(PCI_DEVICE(dev
));
1282 qxl_hard_reset(d
, 0);
1285 static void qxl_vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1287 VGACommonState
*vga
= opaque
;
1288 PCIQXLDevice
*qxl
= container_of(vga
, PCIQXLDevice
, vga
);
1290 trace_qxl_io_write_vga(qxl
->id
, qxl_mode_to_string(qxl
->mode
), addr
, val
);
1291 if (qxl
->mode
!= QXL_MODE_VGA
&&
1292 qxl
->revision
<= QXL_REVISION_STABLE_V12
) {
1293 qxl_destroy_primary(qxl
, QXL_SYNC
);
1294 qxl_soft_reset(qxl
);
1296 vga_ioport_write(opaque
, addr
, val
);
1299 static const MemoryRegionPortio qxl_vga_portio_list
[] = {
1300 { 0x04, 2, 1, .read
= vga_ioport_read
,
1301 .write
= qxl_vga_ioport_write
}, /* 3b4 */
1302 { 0x0a, 1, 1, .read
= vga_ioport_read
,
1303 .write
= qxl_vga_ioport_write
}, /* 3ba */
1304 { 0x10, 16, 1, .read
= vga_ioport_read
,
1305 .write
= qxl_vga_ioport_write
}, /* 3c0 */
1306 { 0x24, 2, 1, .read
= vga_ioport_read
,
1307 .write
= qxl_vga_ioport_write
}, /* 3d4 */
1308 { 0x2a, 1, 1, .read
= vga_ioport_read
,
1309 .write
= qxl_vga_ioport_write
}, /* 3da */
1310 PORTIO_END_OF_LIST(),
1313 static int qxl_add_memslot(PCIQXLDevice
*d
, uint32_t slot_id
, uint64_t delta
,
1316 static const int regions
[] = {
1317 QXL_RAM_RANGE_INDEX
,
1318 QXL_VRAM_RANGE_INDEX
,
1319 QXL_VRAM64_RANGE_INDEX
,
1321 uint64_t guest_start
;
1327 intptr_t virt_start
;
1328 QXLDevMemSlot memslot
;
1331 guest_start
= le64_to_cpu(d
->guest_slots
[slot_id
].slot
.mem_start
);
1332 guest_end
= le64_to_cpu(d
->guest_slots
[slot_id
].slot
.mem_end
);
1334 trace_qxl_memslot_add_guest(d
->id
, slot_id
, guest_start
, guest_end
);
1336 if (slot_id
>= NUM_MEMSLOTS
) {
1337 qxl_set_guest_bug(d
, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__
,
1338 slot_id
, NUM_MEMSLOTS
);
1341 if (guest_start
> guest_end
) {
1342 qxl_set_guest_bug(d
, "%s: guest_start > guest_end 0x%" PRIx64
1343 " > 0x%" PRIx64
, __func__
, guest_start
, guest_end
);
1347 for (i
= 0; i
< ARRAY_SIZE(regions
); i
++) {
1348 pci_region
= regions
[i
];
1349 pci_start
= d
->pci
.io_regions
[pci_region
].addr
;
1350 pci_end
= pci_start
+ d
->pci
.io_regions
[pci_region
].size
;
1352 if (pci_start
== -1) {
1355 /* start address in range ? */
1356 if (guest_start
< pci_start
|| guest_start
> pci_end
) {
1359 /* end address in range ? */
1360 if (guest_end
> pci_end
) {
1366 if (i
== ARRAY_SIZE(regions
)) {
1367 qxl_set_guest_bug(d
, "%s: finished loop without match", __func__
);
1371 switch (pci_region
) {
1372 case QXL_RAM_RANGE_INDEX
:
1375 case QXL_VRAM_RANGE_INDEX
:
1376 case 4 /* vram 64bit */:
1380 /* should not happen */
1381 qxl_set_guest_bug(d
, "%s: pci_region = %d", __func__
, pci_region
);
1384 assert(guest_end
- pci_start
<= memory_region_size(mr
));
1386 virt_start
= (intptr_t)memory_region_get_ram_ptr(mr
);
1387 memslot
.slot_id
= slot_id
;
1388 memslot
.slot_group_id
= MEMSLOT_GROUP_GUEST
; /* guest group */
1389 memslot
.virt_start
= virt_start
+ (guest_start
- pci_start
);
1390 memslot
.virt_end
= virt_start
+ (guest_end
- pci_start
);
1391 memslot
.addr_delta
= memslot
.virt_start
- delta
;
1392 memslot
.generation
= d
->rom
->slot_generation
= 0;
1393 qxl_rom_set_dirty(d
);
1395 qemu_spice_add_memslot(&d
->ssd
, &memslot
, async
);
1396 d
->guest_slots
[slot_id
].mr
= mr
;
1397 d
->guest_slots
[slot_id
].offset
= memslot
.virt_start
- virt_start
;
1398 d
->guest_slots
[slot_id
].size
= memslot
.virt_end
- memslot
.virt_start
;
1399 d
->guest_slots
[slot_id
].delta
= delta
;
1400 d
->guest_slots
[slot_id
].active
= 1;
1404 static void qxl_del_memslot(PCIQXLDevice
*d
, uint32_t slot_id
)
1406 qemu_spice_del_memslot(&d
->ssd
, MEMSLOT_GROUP_HOST
, slot_id
);
1407 d
->guest_slots
[slot_id
].active
= 0;
1410 static void qxl_reset_memslots(PCIQXLDevice
*d
)
1412 qxl_spice_reset_memslots(d
);
1413 memset(&d
->guest_slots
, 0, sizeof(d
->guest_slots
));
1416 static void qxl_reset_surfaces(PCIQXLDevice
*d
)
1418 trace_qxl_reset_surfaces(d
->id
);
1419 d
->mode
= QXL_MODE_UNDEFINED
;
1420 qxl_spice_destroy_surfaces(d
, QXL_SYNC
);
1423 /* can be also called from spice server thread context */
1424 static bool qxl_get_check_slot_offset(PCIQXLDevice
*qxl
, QXLPHYSICAL pqxl
,
1425 uint32_t *s
, uint64_t *o
,
1426 size_t size_requested
)
1428 uint64_t phys
= le64_to_cpu(pqxl
);
1429 uint32_t slot
= (phys
>> (64 - 8)) & 0xff;
1430 uint64_t offset
= phys
& 0xffffffffffff;
1431 uint64_t size_available
;
1433 if (slot
>= NUM_MEMSLOTS
) {
1434 qxl_set_guest_bug(qxl
, "slot too large %d >= %d", slot
,
1438 if (!qxl
->guest_slots
[slot
].active
) {
1439 qxl_set_guest_bug(qxl
, "inactive slot %d\n", slot
);
1442 if (offset
< qxl
->guest_slots
[slot
].delta
) {
1443 qxl_set_guest_bug(qxl
,
1444 "slot %d offset %"PRIu64
" < delta %"PRIu64
"\n",
1445 slot
, offset
, qxl
->guest_slots
[slot
].delta
);
1448 offset
-= qxl
->guest_slots
[slot
].delta
;
1449 if (offset
> qxl
->guest_slots
[slot
].size
) {
1450 qxl_set_guest_bug(qxl
,
1451 "slot %d offset %"PRIu64
" > size %"PRIu64
"\n",
1452 slot
, offset
, qxl
->guest_slots
[slot
].size
);
1455 size_available
= memory_region_size(qxl
->guest_slots
[slot
].mr
);
1456 if (qxl
->guest_slots
[slot
].offset
+ offset
>= size_available
) {
1457 qxl_set_guest_bug(qxl
,
1458 "slot %d offset %"PRIu64
" > region size %"PRIu64
"\n",
1459 slot
, qxl
->guest_slots
[slot
].offset
+ offset
,
1463 size_available
-= qxl
->guest_slots
[slot
].offset
+ offset
;
1464 if (size_requested
> size_available
) {
1465 qxl_set_guest_bug(qxl
,
1466 "slot %d offset %"PRIu64
" size %zu: "
1467 "overrun by %"PRIu64
" bytes\n",
1468 slot
, offset
, size_requested
,
1469 size_requested
- size_available
);
1478 /* can be also called from spice server thread context */
1479 void *qxl_phys2virt(PCIQXLDevice
*qxl
, QXLPHYSICAL pqxl
, int group_id
,
1487 case MEMSLOT_GROUP_HOST
:
1488 offset
= le64_to_cpu(pqxl
) & 0xffffffffffff;
1489 return (void *)(intptr_t)offset
;
1490 case MEMSLOT_GROUP_GUEST
:
1491 if (!qxl_get_check_slot_offset(qxl
, pqxl
, &slot
, &offset
, size
)) {
1494 ptr
= memory_region_get_ram_ptr(qxl
->guest_slots
[slot
].mr
);
1495 ptr
+= qxl
->guest_slots
[slot
].offset
;
1502 static void qxl_create_guest_primary_complete(PCIQXLDevice
*qxl
)
1504 /* for local rendering */
1505 qxl_render_resize(qxl
);
1508 static void qxl_create_guest_primary(PCIQXLDevice
*qxl
, int loadvm
,
1511 QXLDevSurfaceCreate surface
;
1512 QXLSurfaceCreate
*sc
= &qxl
->guest_primary
.surface
;
1513 uint32_t requested_height
= le32_to_cpu(sc
->height
);
1514 int requested_stride
= le32_to_cpu(sc
->stride
);
1516 if (requested_stride
== INT32_MIN
||
1517 abs(requested_stride
) * (uint64_t)requested_height
1518 > qxl
->vgamem_size
) {
1519 qxl_set_guest_bug(qxl
, "%s: requested primary larger than framebuffer"
1520 " stride %d x height %" PRIu32
" > %" PRIu32
,
1521 __func__
, requested_stride
, requested_height
,
1526 if (qxl
->mode
== QXL_MODE_NATIVE
) {
1527 qxl_set_guest_bug(qxl
, "%s: nop since already in QXL_MODE_NATIVE",
1530 qxl_exit_vga_mode(qxl
);
1532 surface
.format
= le32_to_cpu(sc
->format
);
1533 surface
.height
= le32_to_cpu(sc
->height
);
1534 surface
.mem
= le64_to_cpu(sc
->mem
);
1535 surface
.position
= le32_to_cpu(sc
->position
);
1536 surface
.stride
= le32_to_cpu(sc
->stride
);
1537 surface
.width
= le32_to_cpu(sc
->width
);
1538 surface
.type
= le32_to_cpu(sc
->type
);
1539 surface
.flags
= le32_to_cpu(sc
->flags
);
1540 trace_qxl_create_guest_primary(qxl
->id
, sc
->width
, sc
->height
, sc
->mem
,
1541 sc
->format
, sc
->position
);
1542 trace_qxl_create_guest_primary_rest(qxl
->id
, sc
->stride
, sc
->type
,
1545 if ((surface
.stride
& 0x3) != 0) {
1546 qxl_set_guest_bug(qxl
, "primary surface stride = %d %% 4 != 0",
1551 surface
.mouse_mode
= true;
1552 surface
.group_id
= MEMSLOT_GROUP_GUEST
;
1554 surface
.flags
|= QXL_SURF_FLAG_KEEP_DATA
;
1557 qxl
->mode
= QXL_MODE_NATIVE
;
1559 qemu_spice_create_primary_surface(&qxl
->ssd
, 0, &surface
, async
);
1561 if (async
== QXL_SYNC
) {
1562 qxl_create_guest_primary_complete(qxl
);
1566 /* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
1567 * done (in QXL_SYNC case), 0 otherwise. */
1568 static int qxl_destroy_primary(PCIQXLDevice
*d
, qxl_async_io async
)
1570 if (d
->mode
== QXL_MODE_UNDEFINED
) {
1573 trace_qxl_destroy_primary(d
->id
);
1574 d
->mode
= QXL_MODE_UNDEFINED
;
1575 qemu_spice_destroy_primary_surface(&d
->ssd
, 0, async
);
1576 qxl_spice_reset_cursor(d
);
1580 static void qxl_set_mode(PCIQXLDevice
*d
, unsigned int modenr
, int loadvm
)
1582 pcibus_t start
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].addr
;
1583 pcibus_t end
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].size
+ start
;
1584 QXLMode
*mode
= d
->modes
->modes
+ modenr
;
1585 uint64_t devmem
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].addr
;
1591 if (modenr
>= d
->modes
->n_modes
) {
1592 qxl_set_guest_bug(d
, "mode number out of range");
1596 QXLSurfaceCreate surface
= {
1597 .width
= mode
->x_res
,
1598 .height
= mode
->y_res
,
1599 .stride
= -mode
->x_res
* 4,
1600 .format
= SPICE_SURFACE_FMT_32_xRGB
,
1601 .flags
= loadvm
? QXL_SURF_FLAG_KEEP_DATA
: 0,
1603 .mem
= devmem
+ d
->shadow_rom
.draw_area_offset
,
1606 trace_qxl_set_mode(d
->id
, modenr
, mode
->x_res
, mode
->y_res
, mode
->bits
,
1609 qxl_hard_reset(d
, 0);
1612 d
->guest_slots
[0].slot
= slot
;
1613 assert(qxl_add_memslot(d
, 0, devmem
, QXL_SYNC
) == 0);
1615 d
->guest_primary
.surface
= surface
;
1616 qxl_create_guest_primary(d
, 0, QXL_SYNC
);
1618 d
->mode
= QXL_MODE_COMPAT
;
1619 d
->cmdflags
= QXL_COMMAND_FLAG_COMPAT
;
1620 if (mode
->bits
== 16) {
1621 d
->cmdflags
|= QXL_COMMAND_FLAG_COMPAT_16BPP
;
1623 d
->shadow_rom
.mode
= cpu_to_le32(modenr
);
1624 d
->rom
->mode
= cpu_to_le32(modenr
);
1625 qxl_rom_set_dirty(d
);
1628 static void ioport_write(void *opaque
, hwaddr addr
,
1629 uint64_t val
, unsigned size
)
1631 PCIQXLDevice
*d
= opaque
;
1632 uint32_t io_port
= addr
;
1633 qxl_async_io async
= QXL_SYNC
;
1634 uint32_t orig_io_port
;
1636 if (d
->guest_bug
&& io_port
!= QXL_IO_RESET
) {
1640 if (d
->revision
<= QXL_REVISION_STABLE_V10
&&
1641 io_port
> QXL_IO_FLUSH_RELEASE
) {
1642 qxl_set_guest_bug(d
, "unsupported io %d for revision %d\n",
1643 io_port
, d
->revision
);
1649 case QXL_IO_SET_MODE
:
1650 case QXL_IO_MEMSLOT_ADD
:
1651 case QXL_IO_MEMSLOT_DEL
:
1652 case QXL_IO_CREATE_PRIMARY
:
1653 case QXL_IO_UPDATE_IRQ
:
1655 case QXL_IO_MEMSLOT_ADD_ASYNC
:
1656 case QXL_IO_CREATE_PRIMARY_ASYNC
:
1659 if (d
->mode
!= QXL_MODE_VGA
) {
1662 trace_qxl_io_unexpected_vga_mode(d
->id
,
1663 addr
, val
, io_port_to_string(io_port
));
1664 /* be nice to buggy guest drivers */
1665 if (io_port
>= QXL_IO_UPDATE_AREA_ASYNC
&&
1666 io_port
< QXL_IO_RANGE_SIZE
) {
1667 qxl_send_events(d
, QXL_INTERRUPT_IO_CMD
);
1672 /* we change the io_port to avoid ifdeffery in the main switch */
1673 orig_io_port
= io_port
;
1675 case QXL_IO_UPDATE_AREA_ASYNC
:
1676 io_port
= QXL_IO_UPDATE_AREA
;
1678 case QXL_IO_MEMSLOT_ADD_ASYNC
:
1679 io_port
= QXL_IO_MEMSLOT_ADD
;
1681 case QXL_IO_CREATE_PRIMARY_ASYNC
:
1682 io_port
= QXL_IO_CREATE_PRIMARY
;
1684 case QXL_IO_DESTROY_PRIMARY_ASYNC
:
1685 io_port
= QXL_IO_DESTROY_PRIMARY
;
1687 case QXL_IO_DESTROY_SURFACE_ASYNC
:
1688 io_port
= QXL_IO_DESTROY_SURFACE_WAIT
;
1690 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC
:
1691 io_port
= QXL_IO_DESTROY_ALL_SURFACES
;
1693 case QXL_IO_FLUSH_SURFACES_ASYNC
:
1694 case QXL_IO_MONITORS_CONFIG_ASYNC
:
1697 WITH_QEMU_LOCK_GUARD(&d
->async_lock
) {
1698 if (d
->current_async
!= QXL_UNDEFINED_IO
) {
1699 qxl_set_guest_bug(d
, "%d async started before last (%d) complete",
1700 io_port
, d
->current_async
);
1703 d
->current_async
= orig_io_port
;
1709 trace_qxl_io_write(d
->id
, qxl_mode_to_string(d
->mode
),
1710 addr
, io_port_to_string(addr
),
1714 case QXL_IO_UPDATE_AREA
:
1716 QXLCookie
*cookie
= NULL
;
1717 QXLRect update
= d
->ram
->update_area
;
1719 if (d
->ram
->update_surface
> d
->ssd
.num_surfaces
) {
1720 qxl_set_guest_bug(d
, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
1721 d
->ram
->update_surface
);
1724 if (update
.left
>= update
.right
|| update
.top
>= update
.bottom
||
1725 update
.left
< 0 || update
.top
< 0) {
1726 qxl_set_guest_bug(d
,
1727 "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
1728 update
.left
, update
.top
, update
.right
, update
.bottom
);
1729 if (update
.left
== update
.right
|| update
.top
== update
.bottom
) {
1730 /* old drivers may provide empty area, keep going */
1731 qxl_clear_guest_bug(d
);
1736 if (async
== QXL_ASYNC
) {
1737 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
1738 QXL_IO_UPDATE_AREA_ASYNC
);
1739 cookie
->u
.area
= update
;
1741 qxl_spice_update_area(d
, d
->ram
->update_surface
,
1742 cookie
? &cookie
->u
.area
: &update
,
1743 NULL
, 0, 0, async
, cookie
);
1746 case QXL_IO_NOTIFY_CMD
:
1747 qemu_spice_wakeup(&d
->ssd
);
1749 case QXL_IO_NOTIFY_CURSOR
:
1750 qemu_spice_wakeup(&d
->ssd
);
1752 case QXL_IO_UPDATE_IRQ
:
1755 case QXL_IO_NOTIFY_OOM
:
1756 if (!SPICE_RING_IS_EMPTY(&d
->ram
->release_ring
)) {
1763 case QXL_IO_SET_MODE
:
1764 qxl_set_mode(d
, val
, 0);
1767 #ifdef CONFIG_MODULES
1770 * trace_event_get_state_backends() does not work for modules,
1771 * it leads to "undefined symbol: qemu_qxl_io_log_semaphore"
1775 if (trace_event_get_state_backends(TRACE_QXL_IO_LOG
) || d
->guestdebug
) {
1777 /* We cannot trust the guest to NUL terminate d->ram->log_buf */
1778 char *log_buf
= g_strndup((const char *)d
->ram
->log_buf
,
1779 sizeof(d
->ram
->log_buf
));
1780 trace_qxl_io_log(d
->id
, log_buf
);
1781 if (d
->guestdebug
) {
1782 fprintf(stderr
, "qxl/guest-%d: %" PRId64
": %s", d
->id
,
1783 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), log_buf
);
1789 qxl_hard_reset(d
, 0);
1791 case QXL_IO_MEMSLOT_ADD
:
1792 if (val
>= NUM_MEMSLOTS
) {
1793 qxl_set_guest_bug(d
, "QXL_IO_MEMSLOT_ADD: val out of range");
1796 if (d
->guest_slots
[val
].active
) {
1797 qxl_set_guest_bug(d
,
1798 "QXL_IO_MEMSLOT_ADD: memory slot already active");
1801 d
->guest_slots
[val
].slot
= d
->ram
->mem_slot
;
1802 qxl_add_memslot(d
, val
, 0, async
);
1804 case QXL_IO_MEMSLOT_DEL
:
1805 if (val
>= NUM_MEMSLOTS
) {
1806 qxl_set_guest_bug(d
, "QXL_IO_MEMSLOT_DEL: val out of range");
1809 qxl_del_memslot(d
, val
);
1811 case QXL_IO_CREATE_PRIMARY
:
1813 qxl_set_guest_bug(d
, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
1817 d
->guest_primary
.surface
= d
->ram
->create_surface
;
1818 qxl_create_guest_primary(d
, 0, async
);
1820 case QXL_IO_DESTROY_PRIMARY
:
1822 qxl_set_guest_bug(d
, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
1826 if (!qxl_destroy_primary(d
, async
)) {
1827 trace_qxl_io_destroy_primary_ignored(d
->id
,
1828 qxl_mode_to_string(d
->mode
));
1832 case QXL_IO_DESTROY_SURFACE_WAIT
:
1833 if (val
>= d
->ssd
.num_surfaces
) {
1834 qxl_set_guest_bug(d
, "QXL_IO_DESTROY_SURFACE (async=%d):"
1835 "%" PRIu64
" >= NUM_SURFACES", async
, val
);
1838 qxl_spice_destroy_surface_wait(d
, val
, async
);
1840 case QXL_IO_FLUSH_RELEASE
: {
1841 QXLReleaseRing
*ring
= &d
->ram
->release_ring
;
1842 if (ring
->prod
- ring
->cons
+ 1 == ring
->num_items
) {
1844 "ERROR: no flush, full release ring [p%d,%dc]\n",
1845 ring
->prod
, ring
->cons
);
1847 qxl_push_free_res(d
, 1 /* flush */);
1850 case QXL_IO_FLUSH_SURFACES_ASYNC
:
1851 qxl_spice_flush_surfaces_async(d
);
1853 case QXL_IO_DESTROY_ALL_SURFACES
:
1854 d
->mode
= QXL_MODE_UNDEFINED
;
1855 qxl_spice_destroy_surfaces(d
, async
);
1857 case QXL_IO_MONITORS_CONFIG_ASYNC
:
1858 qxl_spice_monitors_config_async(d
, 0);
1861 qxl_set_guest_bug(d
, "%s: unexpected ioport=0x%x\n", __func__
, io_port
);
1866 qxl_send_events(d
, QXL_INTERRUPT_IO_CMD
);
1867 qemu_mutex_lock(&d
->async_lock
);
1868 d
->current_async
= QXL_UNDEFINED_IO
;
1869 qemu_mutex_unlock(&d
->async_lock
);
1873 static uint64_t ioport_read(void *opaque
, hwaddr addr
,
1876 PCIQXLDevice
*qxl
= opaque
;
1878 trace_qxl_io_read_unexpected(qxl
->id
);
1882 static const MemoryRegionOps qxl_io_ops
= {
1883 .read
= ioport_read
,
1884 .write
= ioport_write
,
1886 .min_access_size
= 1,
1887 .max_access_size
= 1,
1891 static void qxl_update_irq_bh(void *opaque
)
1893 PCIQXLDevice
*d
= opaque
;
1897 static void qxl_send_events(PCIQXLDevice
*d
, uint32_t events
)
1899 uint32_t old_pending
;
1900 uint32_t le_events
= cpu_to_le32(events
);
1902 trace_qxl_send_events(d
->id
, events
);
1903 if (!qemu_spice_display_is_running(&d
->ssd
)) {
1904 /* spice-server tracks guest running state and should not do this */
1905 fprintf(stderr
, "%s: spice-server bug: guest stopped, ignoring\n",
1907 trace_qxl_send_events_vm_stopped(d
->id
, events
);
1911 * Older versions of Spice forgot to define the QXLRam struct
1912 * with the '__aligned__(4)' attribute. clang 7 and newer will
1913 * thus warn that qatomic_fetch_or(&d->ram->int_pending, ...)
1914 * might be a misaligned atomic access, and will generate an
1915 * out-of-line call for it, which results in a link error since
1916 * we don't currently link against libatomic.
1918 * In fact we set up d->ram in init_qxl_ram() so it always starts
1919 * at a 4K boundary, so we know that &d->ram->int_pending is
1920 * naturally aligned for a uint32_t. Newer Spice versions
1921 * (with Spice commit beda5ec7a6848be20c0cac2a9a8ef2a41e8069c1)
1922 * will fix the bug directly. To deal with older versions,
1923 * we tell the compiler to assume the address really is aligned.
1924 * Any compiler which cares about the misalignment will have
1925 * __builtin_assume_aligned.
1927 #ifdef HAS_ASSUME_ALIGNED
1928 #define ALIGNED_UINT32_PTR(P) ((uint32_t *)__builtin_assume_aligned(P, 4))
1930 #define ALIGNED_UINT32_PTR(P) ((uint32_t *)P)
1933 old_pending
= qatomic_fetch_or(ALIGNED_UINT32_PTR(&d
->ram
->int_pending
),
1935 if ((old_pending
& le_events
) == le_events
) {
1938 qemu_bh_schedule(d
->update_irq
);
1941 /* graphics console */
1943 static void qxl_hw_update(void *opaque
)
1945 PCIQXLDevice
*qxl
= opaque
;
1947 qxl_render_update(qxl
);
1950 static void qxl_dirty_one_surface(PCIQXLDevice
*qxl
, QXLPHYSICAL pqxl
,
1951 uint32_t height
, int32_t stride
)
1953 uint64_t offset
, size
;
1957 size
= (uint64_t)height
* abs(stride
);
1958 rc
= qxl_get_check_slot_offset(qxl
, pqxl
, &slot
, &offset
, size
);
1960 trace_qxl_surfaces_dirty(qxl
->id
, offset
, size
);
1961 qxl_set_dirty(qxl
->guest_slots
[slot
].mr
,
1962 qxl
->guest_slots
[slot
].offset
+ offset
,
1963 qxl
->guest_slots
[slot
].offset
+ offset
+ size
);
1966 static void qxl_dirty_surfaces(PCIQXLDevice
*qxl
)
1970 if (qxl
->mode
!= QXL_MODE_NATIVE
&& qxl
->mode
!= QXL_MODE_COMPAT
) {
1974 /* dirty the primary surface */
1975 qxl_dirty_one_surface(qxl
, qxl
->guest_primary
.surface
.mem
,
1976 qxl
->guest_primary
.surface
.height
,
1977 qxl
->guest_primary
.surface
.stride
);
1979 /* dirty the off-screen surfaces */
1980 for (i
= 0; i
< qxl
->ssd
.num_surfaces
; i
++) {
1983 if (qxl
->guest_surfaces
.cmds
[i
] == 0) {
1987 cmd
= qxl_phys2virt(qxl
, qxl
->guest_surfaces
.cmds
[i
],
1988 MEMSLOT_GROUP_GUEST
, sizeof(QXLSurfaceCmd
));
1990 assert(cmd
->type
== QXL_SURFACE_CMD_CREATE
);
1991 qxl_dirty_one_surface(qxl
, cmd
->u
.surface_create
.data
,
1992 cmd
->u
.surface_create
.height
,
1993 cmd
->u
.surface_create
.stride
);
1997 static void qxl_vm_change_state_handler(void *opaque
, bool running
,
2000 PCIQXLDevice
*qxl
= opaque
;
2004 * if qxl_send_events was called from spice server context before
2005 * migration ended, qxl_update_irq for these events might not have been
2008 qxl_update_irq(qxl
);
2010 /* make sure surfaces are saved before migration */
2011 qxl_dirty_surfaces(qxl
);
2015 /* display change listener */
2017 static void display_update(DisplayChangeListener
*dcl
,
2018 int x
, int y
, int w
, int h
)
2020 PCIQXLDevice
*qxl
= container_of(dcl
, PCIQXLDevice
, ssd
.dcl
);
2022 if (qxl
->mode
== QXL_MODE_VGA
) {
2023 qemu_spice_display_update(&qxl
->ssd
, x
, y
, w
, h
);
2027 static void display_switch(DisplayChangeListener
*dcl
,
2028 struct DisplaySurface
*surface
)
2030 PCIQXLDevice
*qxl
= container_of(dcl
, PCIQXLDevice
, ssd
.dcl
);
2032 qxl
->ssd
.ds
= surface
;
2033 if (qxl
->mode
== QXL_MODE_VGA
) {
2034 qemu_spice_display_switch(&qxl
->ssd
, surface
);
2038 static void display_refresh(DisplayChangeListener
*dcl
)
2040 PCIQXLDevice
*qxl
= container_of(dcl
, PCIQXLDevice
, ssd
.dcl
);
2042 if (qxl
->mode
== QXL_MODE_VGA
) {
2043 qemu_spice_display_refresh(&qxl
->ssd
);
2047 static DisplayChangeListenerOps display_listener_ops
= {
2048 .dpy_name
= "spice/qxl",
2049 .dpy_gfx_update
= display_update
,
2050 .dpy_gfx_switch
= display_switch
,
2051 .dpy_refresh
= display_refresh
,
2054 static void qxl_init_ramsize(PCIQXLDevice
*qxl
)
2056 /* vga mode framebuffer / primary surface (bar 0, first part) */
2057 if (qxl
->vgamem_size_mb
< 8) {
2058 qxl
->vgamem_size_mb
= 8;
2060 /* XXX: we round vgamem_size_mb up to a nearest power of two and it must be
2061 * less than vga_common_init()'s maximum on qxl->vga.vram_size (512 now).
2063 if (qxl
->vgamem_size_mb
> 256) {
2064 qxl
->vgamem_size_mb
= 256;
2066 qxl
->vgamem_size
= qxl
->vgamem_size_mb
* MiB
;
2068 /* vga ram (bar 0, total) */
2069 if (qxl
->ram_size_mb
!= -1) {
2070 qxl
->vga
.vram_size
= qxl
->ram_size_mb
* MiB
;
2072 if (qxl
->vga
.vram_size
< qxl
->vgamem_size
* 2) {
2073 qxl
->vga
.vram_size
= qxl
->vgamem_size
* 2;
2076 /* vram32 (surfaces, 32bit, bar 1) */
2077 if (qxl
->vram32_size_mb
!= -1) {
2078 qxl
->vram32_size
= qxl
->vram32_size_mb
* MiB
;
2080 if (qxl
->vram32_size
< 4096) {
2081 qxl
->vram32_size
= 4096;
2084 /* vram (surfaces, 64bit, bar 4+5) */
2085 if (qxl
->vram_size_mb
!= -1) {
2086 qxl
->vram_size
= (uint64_t)qxl
->vram_size_mb
* MiB
;
2088 if (qxl
->vram_size
< qxl
->vram32_size
) {
2089 qxl
->vram_size
= qxl
->vram32_size
;
2092 if (qxl
->revision
== 1) {
2093 qxl
->vram32_size
= 4096;
2094 qxl
->vram_size
= 4096;
2096 qxl
->vgamem_size
= pow2ceil(qxl
->vgamem_size
);
2097 qxl
->vga
.vram_size
= pow2ceil(qxl
->vga
.vram_size
);
2098 qxl
->vram32_size
= pow2ceil(qxl
->vram32_size
);
2099 qxl
->vram_size
= pow2ceil(qxl
->vram_size
);
2102 static void qxl_realize_common(PCIQXLDevice
*qxl
, Error
**errp
)
2104 uint8_t* config
= qxl
->pci
.config
;
2105 uint32_t pci_device_rev
;
2108 qemu_spice_display_init_common(&qxl
->ssd
);
2109 qxl
->mode
= QXL_MODE_UNDEFINED
;
2110 qxl
->num_memslots
= NUM_MEMSLOTS
;
2111 qemu_mutex_init(&qxl
->track_lock
);
2112 qemu_mutex_init(&qxl
->async_lock
);
2113 qxl
->current_async
= QXL_UNDEFINED_IO
;
2116 switch (qxl
->revision
) {
2117 case 1: /* spice 0.4 -- qxl-1 */
2118 pci_device_rev
= QXL_REVISION_STABLE_V04
;
2121 case 2: /* spice 0.6 -- qxl-2 */
2122 pci_device_rev
= QXL_REVISION_STABLE_V06
;
2126 pci_device_rev
= QXL_REVISION_STABLE_V10
;
2127 io_size
= 32; /* PCI region size must be pow2 */
2130 pci_device_rev
= QXL_REVISION_STABLE_V12
;
2131 io_size
= pow2ceil(QXL_IO_RANGE_SIZE
);
2134 pci_device_rev
= QXL_REVISION_STABLE_V12
+ 1;
2135 io_size
= pow2ceil(QXL_IO_RANGE_SIZE
);
2138 error_setg(errp
, "Invalid revision %d for qxl device (max %d)",
2139 qxl
->revision
, QXL_DEFAULT_REVISION
);
2143 pci_set_byte(&config
[PCI_REVISION_ID
], pci_device_rev
);
2144 pci_set_byte(&config
[PCI_INTERRUPT_PIN
], 1);
2146 qxl
->rom_size
= qxl_rom_size();
2147 memory_region_init_rom(&qxl
->rom_bar
, OBJECT(qxl
), "qxl.vrom",
2148 qxl
->rom_size
, &error_fatal
);
2152 qxl
->guest_surfaces
.cmds
= g_new0(QXLPHYSICAL
, qxl
->ssd
.num_surfaces
);
2153 memory_region_init_ram(&qxl
->vram_bar
, OBJECT(qxl
), "qxl.vram",
2154 qxl
->vram_size
, &error_fatal
);
2155 memory_region_init_alias(&qxl
->vram32_bar
, OBJECT(qxl
), "qxl.vram32",
2156 &qxl
->vram_bar
, 0, qxl
->vram32_size
);
2158 memory_region_init_io(&qxl
->io_bar
, OBJECT(qxl
), &qxl_io_ops
, qxl
,
2159 "qxl-ioports", io_size
);
2160 if (qxl
->have_vga
) {
2161 vga_dirty_log_start(&qxl
->vga
);
2163 memory_region_set_flush_coalesced(&qxl
->io_bar
);
2166 pci_register_bar(&qxl
->pci
, QXL_IO_RANGE_INDEX
,
2167 PCI_BASE_ADDRESS_SPACE_IO
, &qxl
->io_bar
);
2169 pci_register_bar(&qxl
->pci
, QXL_ROM_RANGE_INDEX
,
2170 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->rom_bar
);
2172 pci_register_bar(&qxl
->pci
, QXL_RAM_RANGE_INDEX
,
2173 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->vga
.vram
);
2175 pci_register_bar(&qxl
->pci
, QXL_VRAM_RANGE_INDEX
,
2176 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->vram32_bar
);
2178 if (qxl
->vram32_size
< qxl
->vram_size
) {
2180 * Make the 64bit vram bar show up only in case it is
2181 * configured to be larger than the 32bit vram bar.
2183 pci_register_bar(&qxl
->pci
, QXL_VRAM64_RANGE_INDEX
,
2184 PCI_BASE_ADDRESS_SPACE_MEMORY
|
2185 PCI_BASE_ADDRESS_MEM_TYPE_64
|
2186 PCI_BASE_ADDRESS_MEM_PREFETCH
,
2190 /* print pci bar details */
2191 dprint(qxl
, 1, "ram/%s: %" PRId64
" MB [region 0]\n",
2192 qxl
->have_vga
? "pri" : "sec", qxl
->vga
.vram_size
/ MiB
);
2193 dprint(qxl
, 1, "vram/32: %" PRIx64
" MB [region 1]\n",
2194 qxl
->vram32_size
/ MiB
);
2195 dprint(qxl
, 1, "vram/64: %" PRIx64
" MB %s\n",
2196 qxl
->vram_size
/ MiB
,
2197 qxl
->vram32_size
< qxl
->vram_size
? "[region 4]" : "[unmapped]");
2199 qxl
->ssd
.qxl
.base
.sif
= &qxl_interface
.base
;
2200 if (qemu_spice_add_display_interface(&qxl
->ssd
.qxl
, qxl
->vga
.con
) != 0) {
2201 error_setg(errp
, "qxl interface %d.%d not supported by spice-server",
2202 SPICE_INTERFACE_QXL_MAJOR
, SPICE_INTERFACE_QXL_MINOR
);
2206 #if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
2208 char device_address
[256] = "";
2209 if (qemu_console_fill_device_address(qxl
->vga
.con
,
2210 device_address
, sizeof(device_address
),
2212 spice_qxl_set_device_info(&qxl
->ssd
.qxl
,
2217 error_report_err(err
);
2221 qemu_add_vm_change_state_handler(qxl_vm_change_state_handler
, qxl
);
2223 qxl
->update_irq
= qemu_bh_new(qxl_update_irq_bh
, qxl
);
2224 qxl_reset_state(qxl
);
2226 qxl
->update_area_bh
= qemu_bh_new(qxl_render_update_area_bh
, qxl
);
2227 qxl
->ssd
.cursor_bh
= qemu_bh_new(qemu_spice_cursor_refresh_bh
, &qxl
->ssd
);
2230 static void qxl_realize_primary(PCIDevice
*dev
, Error
**errp
)
2232 PCIQXLDevice
*qxl
= PCI_QXL(dev
);
2233 VGACommonState
*vga
= &qxl
->vga
;
2234 Error
*local_err
= NULL
;
2236 qxl_init_ramsize(qxl
);
2237 vga
->vbe_size
= qxl
->vgamem_size
;
2238 vga
->vram_size_mb
= qxl
->vga
.vram_size
/ MiB
;
2239 vga_common_init(vga
, OBJECT(dev
), &local_err
);
2241 error_propagate(errp
, local_err
);
2244 vga_init(vga
, OBJECT(dev
),
2245 pci_address_space(dev
), pci_address_space_io(dev
), false);
2246 portio_list_init(&qxl
->vga_port_list
, OBJECT(dev
), qxl_vga_portio_list
,
2248 portio_list_set_flush_coalesced(&qxl
->vga_port_list
);
2249 portio_list_add(&qxl
->vga_port_list
, pci_address_space_io(dev
), 0x3b0);
2250 qxl
->have_vga
= true;
2252 vga
->con
= graphic_console_init(DEVICE(dev
), 0, &qxl_ops
, qxl
);
2253 qxl
->id
= qemu_console_get_index(vga
->con
); /* == channel_id */
2255 error_setg(errp
, "primary qxl-vga device must be console 0 "
2256 "(first display device on the command line)");
2260 qxl_realize_common(qxl
, &local_err
);
2262 error_propagate(errp
, local_err
);
2266 qxl
->ssd
.dcl
.ops
= &display_listener_ops
;
2267 qxl
->ssd
.dcl
.con
= vga
->con
;
2268 register_displaychangelistener(&qxl
->ssd
.dcl
);
2271 static void qxl_realize_secondary(PCIDevice
*dev
, Error
**errp
)
2273 PCIQXLDevice
*qxl
= PCI_QXL(dev
);
2275 qxl_init_ramsize(qxl
);
2276 memory_region_init_ram(&qxl
->vga
.vram
, OBJECT(dev
), "qxl.vgavram",
2277 qxl
->vga
.vram_size
, &error_fatal
);
2278 qxl
->vga
.vram_ptr
= memory_region_get_ram_ptr(&qxl
->vga
.vram
);
2279 qxl
->vga
.con
= graphic_console_init(DEVICE(dev
), 0, &qxl_ops
, qxl
);
2280 qxl
->ssd
.dcl
.con
= qxl
->vga
.con
;
2281 qxl
->id
= qemu_console_get_index(qxl
->vga
.con
); /* == channel_id */
2283 qxl_realize_common(qxl
, errp
);
2286 static int qxl_pre_save(void *opaque
)
2288 PCIQXLDevice
* d
= opaque
;
2289 uint8_t *ram_start
= d
->vga
.vram_ptr
;
2291 trace_qxl_pre_save(d
->id
);
2292 if (d
->last_release
== NULL
) {
2293 d
->last_release_offset
= 0;
2295 d
->last_release_offset
= (uint8_t *)d
->last_release
- ram_start
;
2297 if (d
->last_release_offset
>= d
->vga
.vram_size
) {
2304 static int qxl_pre_load(void *opaque
)
2306 PCIQXLDevice
* d
= opaque
;
2308 trace_qxl_pre_load(d
->id
);
2309 qxl_hard_reset(d
, 1);
2310 qxl_exit_vga_mode(d
);
2314 static void qxl_create_memslots(PCIQXLDevice
*d
)
2318 for (i
= 0; i
< NUM_MEMSLOTS
; i
++) {
2319 if (!d
->guest_slots
[i
].active
) {
2322 qxl_add_memslot(d
, i
, 0, QXL_SYNC
);
2326 static int qxl_post_load(void *opaque
, int version
)
2328 PCIQXLDevice
* d
= opaque
;
2329 uint8_t *ram_start
= d
->vga
.vram_ptr
;
2330 QXLCommandExt
*cmds
;
2331 int in
, out
, newmode
;
2333 assert(d
->last_release_offset
< d
->vga
.vram_size
);
2334 if (d
->last_release_offset
== 0) {
2335 d
->last_release
= NULL
;
2337 d
->last_release
= (QXLReleaseInfo
*)(ram_start
+ d
->last_release_offset
);
2340 d
->modes
= (QXLModes
*)((uint8_t*)d
->rom
+ d
->rom
->modes_offset
);
2342 trace_qxl_post_load(d
->id
, qxl_mode_to_string(d
->mode
));
2344 d
->mode
= QXL_MODE_UNDEFINED
;
2347 case QXL_MODE_UNDEFINED
:
2348 qxl_create_memslots(d
);
2351 qxl_create_memslots(d
);
2352 qxl_enter_vga_mode(d
);
2354 case QXL_MODE_NATIVE
:
2355 qxl_create_memslots(d
);
2356 qxl_create_guest_primary(d
, 1, QXL_SYNC
);
2358 /* replay surface-create and cursor-set commands */
2359 cmds
= g_new0(QXLCommandExt
, d
->ssd
.num_surfaces
+ 1);
2360 for (in
= 0, out
= 0; in
< d
->ssd
.num_surfaces
; in
++) {
2361 if (d
->guest_surfaces
.cmds
[in
] == 0) {
2364 cmds
[out
].cmd
.data
= d
->guest_surfaces
.cmds
[in
];
2365 cmds
[out
].cmd
.type
= QXL_CMD_SURFACE
;
2366 cmds
[out
].group_id
= MEMSLOT_GROUP_GUEST
;
2369 if (d
->guest_cursor
) {
2370 cmds
[out
].cmd
.data
= d
->guest_cursor
;
2371 cmds
[out
].cmd
.type
= QXL_CMD_CURSOR
;
2372 cmds
[out
].group_id
= MEMSLOT_GROUP_GUEST
;
2375 qxl_spice_loadvm_commands(d
, cmds
, out
);
2377 if (d
->guest_monitors_config
) {
2378 qxl_spice_monitors_config_async(d
, 1);
2381 case QXL_MODE_COMPAT
:
2382 /* note: no need to call qxl_create_memslots, qxl_set_mode
2383 * creates the mem slot. */
2384 qxl_set_mode(d
, d
->shadow_rom
.mode
, 1);
2390 #define QXL_SAVE_VERSION 21
2392 static bool qxl_monitors_config_needed(void *opaque
)
2394 PCIQXLDevice
*qxl
= opaque
;
2396 return qxl
->guest_monitors_config
!= 0;
2400 static const VMStateDescription qxl_memslot
= {
2401 .name
= "qxl-memslot",
2402 .version_id
= QXL_SAVE_VERSION
,
2403 .minimum_version_id
= QXL_SAVE_VERSION
,
2404 .fields
= (VMStateField
[]) {
2405 VMSTATE_UINT64(slot
.mem_start
, struct guest_slots
),
2406 VMSTATE_UINT64(slot
.mem_end
, struct guest_slots
),
2407 VMSTATE_UINT32(active
, struct guest_slots
),
2408 VMSTATE_END_OF_LIST()
2412 static const VMStateDescription qxl_surface
= {
2413 .name
= "qxl-surface",
2414 .version_id
= QXL_SAVE_VERSION
,
2415 .minimum_version_id
= QXL_SAVE_VERSION
,
2416 .fields
= (VMStateField
[]) {
2417 VMSTATE_UINT32(width
, QXLSurfaceCreate
),
2418 VMSTATE_UINT32(height
, QXLSurfaceCreate
),
2419 VMSTATE_INT32(stride
, QXLSurfaceCreate
),
2420 VMSTATE_UINT32(format
, QXLSurfaceCreate
),
2421 VMSTATE_UINT32(position
, QXLSurfaceCreate
),
2422 VMSTATE_UINT32(mouse_mode
, QXLSurfaceCreate
),
2423 VMSTATE_UINT32(flags
, QXLSurfaceCreate
),
2424 VMSTATE_UINT32(type
, QXLSurfaceCreate
),
2425 VMSTATE_UINT64(mem
, QXLSurfaceCreate
),
2426 VMSTATE_END_OF_LIST()
2430 static const VMStateDescription qxl_vmstate_monitors_config
= {
2431 .name
= "qxl/monitors-config",
2433 .minimum_version_id
= 1,
2434 .needed
= qxl_monitors_config_needed
,
2435 .fields
= (VMStateField
[]) {
2436 VMSTATE_UINT64(guest_monitors_config
, PCIQXLDevice
),
2437 VMSTATE_END_OF_LIST()
2441 static const VMStateDescription qxl_vmstate
= {
2443 .version_id
= QXL_SAVE_VERSION
,
2444 .minimum_version_id
= QXL_SAVE_VERSION
,
2445 .pre_save
= qxl_pre_save
,
2446 .pre_load
= qxl_pre_load
,
2447 .post_load
= qxl_post_load
,
2448 .fields
= (VMStateField
[]) {
2449 VMSTATE_PCI_DEVICE(pci
, PCIQXLDevice
),
2450 VMSTATE_STRUCT(vga
, PCIQXLDevice
, 0, vmstate_vga_common
, VGACommonState
),
2451 VMSTATE_UINT32(shadow_rom
.mode
, PCIQXLDevice
),
2452 VMSTATE_UINT32(num_free_res
, PCIQXLDevice
),
2453 VMSTATE_UINT32(last_release_offset
, PCIQXLDevice
),
2454 VMSTATE_UINT32(mode
, PCIQXLDevice
),
2455 VMSTATE_UINT32(ssd
.unique
, PCIQXLDevice
),
2456 VMSTATE_INT32_EQUAL(num_memslots
, PCIQXLDevice
, NULL
),
2457 VMSTATE_STRUCT_ARRAY(guest_slots
, PCIQXLDevice
, NUM_MEMSLOTS
, 0,
2458 qxl_memslot
, struct guest_slots
),
2459 VMSTATE_STRUCT(guest_primary
.surface
, PCIQXLDevice
, 0,
2460 qxl_surface
, QXLSurfaceCreate
),
2461 VMSTATE_INT32_EQUAL(ssd
.num_surfaces
, PCIQXLDevice
, NULL
),
2462 VMSTATE_VARRAY_INT32(guest_surfaces
.cmds
, PCIQXLDevice
,
2463 ssd
.num_surfaces
, 0,
2464 vmstate_info_uint64
, uint64_t),
2465 VMSTATE_UINT64(guest_cursor
, PCIQXLDevice
),
2466 VMSTATE_END_OF_LIST()
2468 .subsections
= (const VMStateDescription
*[]) {
2469 &qxl_vmstate_monitors_config
,
2474 static Property qxl_properties
[] = {
2475 DEFINE_PROP_UINT32("ram_size", PCIQXLDevice
, vga
.vram_size
, 64 * MiB
),
2476 DEFINE_PROP_UINT64("vram_size", PCIQXLDevice
, vram32_size
, 64 * MiB
),
2477 DEFINE_PROP_UINT32("revision", PCIQXLDevice
, revision
,
2478 QXL_DEFAULT_REVISION
),
2479 DEFINE_PROP_UINT32("debug", PCIQXLDevice
, debug
, 0),
2480 DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice
, guestdebug
, 0),
2481 DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice
, cmdlog
, 0),
2482 DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice
, ram_size_mb
, -1),
2483 DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice
, vram32_size_mb
, -1),
2484 DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice
, vram_size_mb
, -1),
2485 DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice
, vgamem_size_mb
, 16),
2486 DEFINE_PROP_INT32("surfaces", PCIQXLDevice
, ssd
.num_surfaces
, 1024),
2487 DEFINE_PROP_UINT16("max_outputs", PCIQXLDevice
, max_outputs
, 0),
2488 DEFINE_PROP_UINT32("xres", PCIQXLDevice
, xres
, 0),
2489 DEFINE_PROP_UINT32("yres", PCIQXLDevice
, yres
, 0),
2490 DEFINE_PROP_BOOL("global-vmstate", PCIQXLDevice
, vga
.global_vmstate
, false),
2491 DEFINE_PROP_END_OF_LIST(),
2494 static void qxl_pci_class_init(ObjectClass
*klass
, void *data
)
2496 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2497 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2499 k
->vendor_id
= REDHAT_PCI_VENDOR_ID
;
2500 k
->device_id
= QXL_DEVICE_ID_STABLE
;
2501 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
2502 dc
->reset
= qxl_reset_handler
;
2503 dc
->vmsd
= &qxl_vmstate
;
2504 device_class_set_props(dc
, qxl_properties
);
2507 static const TypeInfo qxl_pci_type_info
= {
2508 .name
= TYPE_PCI_QXL
,
2509 .parent
= TYPE_PCI_DEVICE
,
2510 .instance_size
= sizeof(PCIQXLDevice
),
2512 .class_init
= qxl_pci_class_init
,
2513 .interfaces
= (InterfaceInfo
[]) {
2514 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2519 static void qxl_primary_class_init(ObjectClass
*klass
, void *data
)
2521 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2522 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2524 k
->realize
= qxl_realize_primary
;
2525 k
->romfile
= "vgabios-qxl.bin";
2526 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
2527 dc
->desc
= "Spice QXL GPU (primary, vga compatible)";
2528 dc
->hotpluggable
= false;
2531 static const TypeInfo qxl_primary_info
= {
2533 .parent
= TYPE_PCI_QXL
,
2534 .class_init
= qxl_primary_class_init
,
2536 module_obj("qxl-vga");
2537 module_kconfig(QXL
);
2539 static void qxl_secondary_class_init(ObjectClass
*klass
, void *data
)
2541 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2542 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2544 k
->realize
= qxl_realize_secondary
;
2545 k
->class_id
= PCI_CLASS_DISPLAY_OTHER
;
2546 dc
->desc
= "Spice QXL GPU (secondary)";
2549 static const TypeInfo qxl_secondary_info
= {
2551 .parent
= TYPE_PCI_QXL
,
2552 .class_init
= qxl_secondary_class_init
,
2556 static void qxl_register_types(void)
2558 type_register_static(&qxl_pci_type_info
);
2559 type_register_static(&qxl_primary_info
);
2560 type_register_static(&qxl_secondary_info
);
2563 type_init(qxl_register_types
)
2565 module_dep("ui-spice-core");