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/>.
24 #include "qemu-common.h"
25 #include "qemu/timer.h"
26 #include "qemu/queue.h"
27 #include "qemu/atomic.h"
28 #include "monitor/monitor.h"
29 #include "sysemu/sysemu.h"
35 * NOTE: SPICE_RING_PROD_ITEM accesses memory on the pci bar and as
36 * such can be changed by the guest, so to avoid a guest trigerrable
37 * abort we just qxl_set_guest_bug and set the return to NULL. Still
38 * it may happen as a result of emulator bug as well.
40 #undef SPICE_RING_PROD_ITEM
41 #define SPICE_RING_PROD_ITEM(qxl, r, ret) { \
42 uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r); \
43 if (prod >= ARRAY_SIZE((r)->items)) { \
44 qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
45 "%u >= %zu", prod, ARRAY_SIZE((r)->items)); \
48 ret = &(r)->items[prod].el; \
52 #undef SPICE_RING_CONS_ITEM
53 #define SPICE_RING_CONS_ITEM(qxl, r, ret) { \
54 uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \
55 if (cons >= ARRAY_SIZE((r)->items)) { \
56 qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
57 "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \
60 ret = &(r)->items[cons].el; \
65 #define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
67 #define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9"
69 #define QXL_MODE(_x, _y, _b, _o) \
73 .stride = (_x) * (_b) / 8, \
74 .x_mili = PIXEL_SIZE * (_x), \
75 .y_mili = PIXEL_SIZE * (_y), \
79 #define QXL_MODE_16_32(x_res, y_res, orientation) \
80 QXL_MODE(x_res, y_res, 16, orientation), \
81 QXL_MODE(x_res, y_res, 32, orientation)
83 #define QXL_MODE_EX(x_res, y_res) \
84 QXL_MODE_16_32(x_res, y_res, 0), \
85 QXL_MODE_16_32(x_res, y_res, 1)
87 static QXLMode qxl_modes
[] = {
88 QXL_MODE_EX(640, 480),
89 QXL_MODE_EX(800, 480),
90 QXL_MODE_EX(800, 600),
91 QXL_MODE_EX(832, 624),
92 QXL_MODE_EX(960, 640),
93 QXL_MODE_EX(1024, 600),
94 QXL_MODE_EX(1024, 768),
95 QXL_MODE_EX(1152, 864),
96 QXL_MODE_EX(1152, 870),
97 QXL_MODE_EX(1280, 720),
98 QXL_MODE_EX(1280, 760),
99 QXL_MODE_EX(1280, 768),
100 QXL_MODE_EX(1280, 800),
101 QXL_MODE_EX(1280, 960),
102 QXL_MODE_EX(1280, 1024),
103 QXL_MODE_EX(1360, 768),
104 QXL_MODE_EX(1366, 768),
105 QXL_MODE_EX(1400, 1050),
106 QXL_MODE_EX(1440, 900),
107 QXL_MODE_EX(1600, 900),
108 QXL_MODE_EX(1600, 1200),
109 QXL_MODE_EX(1680, 1050),
110 QXL_MODE_EX(1920, 1080),
111 /* these modes need more than 8 MB video memory */
112 QXL_MODE_EX(1920, 1200),
113 QXL_MODE_EX(1920, 1440),
114 QXL_MODE_EX(2000, 2000),
115 QXL_MODE_EX(2048, 1536),
116 QXL_MODE_EX(2048, 2048),
117 QXL_MODE_EX(2560, 1440),
118 QXL_MODE_EX(2560, 1600),
119 /* these modes need more than 16 MB video memory */
120 QXL_MODE_EX(2560, 2048),
121 QXL_MODE_EX(2800, 2100),
122 QXL_MODE_EX(3200, 2400),
123 QXL_MODE_EX(3840, 2160), /* 4k mainstream */
124 QXL_MODE_EX(4096, 2160), /* 4k */
125 QXL_MODE_EX(7680, 4320), /* 8k mainstream */
126 QXL_MODE_EX(8192, 4320), /* 8k */
129 static void qxl_send_events(PCIQXLDevice
*d
, uint32_t events
);
130 static int qxl_destroy_primary(PCIQXLDevice
*d
, qxl_async_io async
);
131 static void qxl_reset_memslots(PCIQXLDevice
*d
);
132 static void qxl_reset_surfaces(PCIQXLDevice
*d
);
133 static void qxl_ring_set_dirty(PCIQXLDevice
*qxl
);
135 void qxl_set_guest_bug(PCIQXLDevice
*qxl
, const char *msg
, ...)
137 trace_qxl_set_guest_bug(qxl
->id
);
138 qxl_send_events(qxl
, QXL_INTERRUPT_ERROR
);
140 if (qxl
->guestdebug
) {
143 fprintf(stderr
, "qxl-%d: guest bug: ", qxl
->id
);
144 vfprintf(stderr
, msg
, ap
);
145 fprintf(stderr
, "\n");
150 static void qxl_clear_guest_bug(PCIQXLDevice
*qxl
)
155 void qxl_spice_update_area(PCIQXLDevice
*qxl
, uint32_t surface_id
,
156 struct QXLRect
*area
, struct QXLRect
*dirty_rects
,
157 uint32_t num_dirty_rects
,
158 uint32_t clear_dirty_region
,
159 qxl_async_io async
, struct QXLCookie
*cookie
)
161 trace_qxl_spice_update_area(qxl
->id
, surface_id
, area
->left
, area
->right
,
162 area
->top
, area
->bottom
);
163 trace_qxl_spice_update_area_rest(qxl
->id
, num_dirty_rects
,
165 if (async
== QXL_SYNC
) {
166 spice_qxl_update_area(&qxl
->ssd
.qxl
, surface_id
, area
,
167 dirty_rects
, num_dirty_rects
, clear_dirty_region
);
169 assert(cookie
!= NULL
);
170 spice_qxl_update_area_async(&qxl
->ssd
.qxl
, surface_id
, area
,
171 clear_dirty_region
, (uintptr_t)cookie
);
175 static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice
*qxl
,
178 trace_qxl_spice_destroy_surface_wait_complete(qxl
->id
, id
);
179 qemu_mutex_lock(&qxl
->track_lock
);
180 qxl
->guest_surfaces
.cmds
[id
] = 0;
181 qxl
->guest_surfaces
.count
--;
182 qemu_mutex_unlock(&qxl
->track_lock
);
185 static void qxl_spice_destroy_surface_wait(PCIQXLDevice
*qxl
, uint32_t id
,
190 trace_qxl_spice_destroy_surface_wait(qxl
->id
, id
, async
);
192 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
193 QXL_IO_DESTROY_SURFACE_ASYNC
);
194 cookie
->u
.surface_id
= id
;
195 spice_qxl_destroy_surface_async(&qxl
->ssd
.qxl
, id
, (uintptr_t)cookie
);
197 spice_qxl_destroy_surface_wait(&qxl
->ssd
.qxl
, id
);
198 qxl_spice_destroy_surface_wait_complete(qxl
, id
);
202 static void qxl_spice_flush_surfaces_async(PCIQXLDevice
*qxl
)
204 trace_qxl_spice_flush_surfaces_async(qxl
->id
, qxl
->guest_surfaces
.count
,
206 spice_qxl_flush_surfaces_async(&qxl
->ssd
.qxl
,
207 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
208 QXL_IO_FLUSH_SURFACES_ASYNC
));
211 void qxl_spice_loadvm_commands(PCIQXLDevice
*qxl
, struct QXLCommandExt
*ext
,
214 trace_qxl_spice_loadvm_commands(qxl
->id
, ext
, count
);
215 spice_qxl_loadvm_commands(&qxl
->ssd
.qxl
, ext
, count
);
218 void qxl_spice_oom(PCIQXLDevice
*qxl
)
220 trace_qxl_spice_oom(qxl
->id
);
221 spice_qxl_oom(&qxl
->ssd
.qxl
);
224 void qxl_spice_reset_memslots(PCIQXLDevice
*qxl
)
226 trace_qxl_spice_reset_memslots(qxl
->id
);
227 spice_qxl_reset_memslots(&qxl
->ssd
.qxl
);
230 static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice
*qxl
)
232 trace_qxl_spice_destroy_surfaces_complete(qxl
->id
);
233 qemu_mutex_lock(&qxl
->track_lock
);
234 memset(qxl
->guest_surfaces
.cmds
, 0,
235 sizeof(qxl
->guest_surfaces
.cmds
[0]) * qxl
->ssd
.num_surfaces
);
236 qxl
->guest_surfaces
.count
= 0;
237 qemu_mutex_unlock(&qxl
->track_lock
);
240 static void qxl_spice_destroy_surfaces(PCIQXLDevice
*qxl
, qxl_async_io async
)
242 trace_qxl_spice_destroy_surfaces(qxl
->id
, async
);
244 spice_qxl_destroy_surfaces_async(&qxl
->ssd
.qxl
,
245 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
246 QXL_IO_DESTROY_ALL_SURFACES_ASYNC
));
248 spice_qxl_destroy_surfaces(&qxl
->ssd
.qxl
);
249 qxl_spice_destroy_surfaces_complete(qxl
);
253 static void qxl_spice_monitors_config_async(PCIQXLDevice
*qxl
, int replay
)
255 trace_qxl_spice_monitors_config(qxl
->id
);
258 * don't use QXL_COOKIE_TYPE_IO:
259 * - we are not running yet (post_load), we will assert
261 * - this is not a guest io, but a reply, so async_io isn't set.
263 spice_qxl_monitors_config_async(&qxl
->ssd
.qxl
,
264 qxl
->guest_monitors_config
,
266 (uintptr_t)qxl_cookie_new(
267 QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG
,
270 qxl
->guest_monitors_config
= qxl
->ram
->monitors_config
;
271 spice_qxl_monitors_config_async(&qxl
->ssd
.qxl
,
272 qxl
->ram
->monitors_config
,
274 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
275 QXL_IO_MONITORS_CONFIG_ASYNC
));
279 void qxl_spice_reset_image_cache(PCIQXLDevice
*qxl
)
281 trace_qxl_spice_reset_image_cache(qxl
->id
);
282 spice_qxl_reset_image_cache(&qxl
->ssd
.qxl
);
285 void qxl_spice_reset_cursor(PCIQXLDevice
*qxl
)
287 trace_qxl_spice_reset_cursor(qxl
->id
);
288 spice_qxl_reset_cursor(&qxl
->ssd
.qxl
);
289 qemu_mutex_lock(&qxl
->track_lock
);
290 qxl
->guest_cursor
= 0;
291 qemu_mutex_unlock(&qxl
->track_lock
);
292 if (qxl
->ssd
.cursor
) {
293 cursor_put(qxl
->ssd
.cursor
);
295 qxl
->ssd
.cursor
= cursor_builtin_hidden();
299 static inline uint32_t msb_mask(uint32_t val
)
304 mask
= ~(val
- 1) & val
;
306 } while (mask
< val
);
311 static ram_addr_t
qxl_rom_size(void)
313 uint32_t required_rom_size
= sizeof(QXLRom
) + sizeof(QXLModes
) +
315 uint32_t rom_size
= 8192; /* two pages */
317 QEMU_BUILD_BUG_ON(required_rom_size
> rom_size
);
321 static void init_qxl_rom(PCIQXLDevice
*d
)
323 QXLRom
*rom
= memory_region_get_ram_ptr(&d
->rom_bar
);
324 QXLModes
*modes
= (QXLModes
*)(rom
+ 1);
325 uint32_t ram_header_size
;
326 uint32_t surface0_area_size
;
331 memset(rom
, 0, d
->rom_size
);
333 rom
->magic
= cpu_to_le32(QXL_ROM_MAGIC
);
334 rom
->id
= cpu_to_le32(d
->id
);
335 rom
->log_level
= cpu_to_le32(d
->guestdebug
);
336 rom
->modes_offset
= cpu_to_le32(sizeof(QXLRom
));
338 rom
->slot_gen_bits
= MEMSLOT_GENERATION_BITS
;
339 rom
->slot_id_bits
= MEMSLOT_SLOT_BITS
;
340 rom
->slots_start
= 1;
341 rom
->slots_end
= NUM_MEMSLOTS
- 1;
342 rom
->n_surfaces
= cpu_to_le32(d
->ssd
.num_surfaces
);
344 for (i
= 0, n
= 0; i
< ARRAY_SIZE(qxl_modes
); i
++) {
345 fb
= qxl_modes
[i
].y_res
* qxl_modes
[i
].stride
;
346 if (fb
> d
->vgamem_size
) {
349 modes
->modes
[n
].id
= cpu_to_le32(i
);
350 modes
->modes
[n
].x_res
= cpu_to_le32(qxl_modes
[i
].x_res
);
351 modes
->modes
[n
].y_res
= cpu_to_le32(qxl_modes
[i
].y_res
);
352 modes
->modes
[n
].bits
= cpu_to_le32(qxl_modes
[i
].bits
);
353 modes
->modes
[n
].stride
= cpu_to_le32(qxl_modes
[i
].stride
);
354 modes
->modes
[n
].x_mili
= cpu_to_le32(qxl_modes
[i
].x_mili
);
355 modes
->modes
[n
].y_mili
= cpu_to_le32(qxl_modes
[i
].y_mili
);
356 modes
->modes
[n
].orientation
= cpu_to_le32(qxl_modes
[i
].orientation
);
359 modes
->n_modes
= cpu_to_le32(n
);
361 ram_header_size
= ALIGN(sizeof(QXLRam
), 4096);
362 surface0_area_size
= ALIGN(d
->vgamem_size
, 4096);
363 num_pages
= d
->vga
.vram_size
;
364 num_pages
-= ram_header_size
;
365 num_pages
-= surface0_area_size
;
366 num_pages
= num_pages
/ QXL_PAGE_SIZE
;
368 rom
->draw_area_offset
= cpu_to_le32(0);
369 rom
->surface0_area_size
= cpu_to_le32(surface0_area_size
);
370 rom
->pages_offset
= cpu_to_le32(surface0_area_size
);
371 rom
->num_pages
= cpu_to_le32(num_pages
);
372 rom
->ram_header_offset
= cpu_to_le32(d
->vga
.vram_size
- ram_header_size
);
374 d
->shadow_rom
= *rom
;
379 static void init_qxl_ram(PCIQXLDevice
*d
)
384 buf
= d
->vga
.vram_ptr
;
385 d
->ram
= (QXLRam
*)(buf
+ le32_to_cpu(d
->shadow_rom
.ram_header_offset
));
386 d
->ram
->magic
= cpu_to_le32(QXL_RAM_MAGIC
);
387 d
->ram
->int_pending
= cpu_to_le32(0);
388 d
->ram
->int_mask
= cpu_to_le32(0);
389 d
->ram
->update_surface
= 0;
390 d
->ram
->monitors_config
= 0;
391 SPICE_RING_INIT(&d
->ram
->cmd_ring
);
392 SPICE_RING_INIT(&d
->ram
->cursor_ring
);
393 SPICE_RING_INIT(&d
->ram
->release_ring
);
394 SPICE_RING_PROD_ITEM(d
, &d
->ram
->release_ring
, item
);
397 qxl_ring_set_dirty(d
);
400 /* can be called from spice server thread context */
401 static void qxl_set_dirty(MemoryRegion
*mr
, ram_addr_t addr
, ram_addr_t end
)
403 memory_region_set_dirty(mr
, addr
, end
- addr
);
406 static void qxl_rom_set_dirty(PCIQXLDevice
*qxl
)
408 qxl_set_dirty(&qxl
->rom_bar
, 0, qxl
->rom_size
);
411 /* called from spice server thread context only */
412 static void qxl_ram_set_dirty(PCIQXLDevice
*qxl
, void *ptr
)
414 void *base
= qxl
->vga
.vram_ptr
;
418 assert(offset
< qxl
->vga
.vram_size
);
419 qxl_set_dirty(&qxl
->vga
.vram
, offset
, offset
+ 3);
422 /* can be called from spice server thread context */
423 static void qxl_ring_set_dirty(PCIQXLDevice
*qxl
)
425 ram_addr_t addr
= qxl
->shadow_rom
.ram_header_offset
;
426 ram_addr_t end
= qxl
->vga
.vram_size
;
427 qxl_set_dirty(&qxl
->vga
.vram
, addr
, end
);
431 * keep track of some command state, for savevm/loadvm.
432 * called from spice server thread context only
434 static int qxl_track_command(PCIQXLDevice
*qxl
, struct QXLCommandExt
*ext
)
436 switch (le32_to_cpu(ext
->cmd
.type
)) {
437 case QXL_CMD_SURFACE
:
439 QXLSurfaceCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
);
444 uint32_t id
= le32_to_cpu(cmd
->surface_id
);
446 if (id
>= qxl
->ssd
.num_surfaces
) {
447 qxl_set_guest_bug(qxl
, "QXL_CMD_SURFACE id %d >= %d", id
,
448 qxl
->ssd
.num_surfaces
);
451 if (cmd
->type
== QXL_SURFACE_CMD_CREATE
&&
452 (cmd
->u
.surface_create
.stride
& 0x03) != 0) {
453 qxl_set_guest_bug(qxl
, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
454 cmd
->u
.surface_create
.stride
);
457 qemu_mutex_lock(&qxl
->track_lock
);
458 if (cmd
->type
== QXL_SURFACE_CMD_CREATE
) {
459 qxl
->guest_surfaces
.cmds
[id
] = ext
->cmd
.data
;
460 qxl
->guest_surfaces
.count
++;
461 if (qxl
->guest_surfaces
.max
< qxl
->guest_surfaces
.count
)
462 qxl
->guest_surfaces
.max
= qxl
->guest_surfaces
.count
;
464 if (cmd
->type
== QXL_SURFACE_CMD_DESTROY
) {
465 qxl
->guest_surfaces
.cmds
[id
] = 0;
466 qxl
->guest_surfaces
.count
--;
468 qemu_mutex_unlock(&qxl
->track_lock
);
473 QXLCursorCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
);
478 if (cmd
->type
== QXL_CURSOR_SET
) {
479 qemu_mutex_lock(&qxl
->track_lock
);
480 qxl
->guest_cursor
= ext
->cmd
.data
;
481 qemu_mutex_unlock(&qxl
->track_lock
);
489 /* spice display interface callbacks */
491 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
493 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
495 trace_qxl_interface_attach_worker(qxl
->id
);
496 qxl
->ssd
.worker
= qxl_worker
;
499 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
501 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
503 trace_qxl_interface_set_compression_level(qxl
->id
, level
);
504 qxl
->shadow_rom
.compression_level
= cpu_to_le32(level
);
505 qxl
->rom
->compression_level
= cpu_to_le32(level
);
506 qxl_rom_set_dirty(qxl
);
509 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
511 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
513 trace_qxl_interface_set_mm_time(qxl
->id
, mm_time
);
514 qxl
->shadow_rom
.mm_clock
= cpu_to_le32(mm_time
);
515 qxl
->rom
->mm_clock
= cpu_to_le32(mm_time
);
516 qxl_rom_set_dirty(qxl
);
519 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
521 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
523 trace_qxl_interface_get_init_info(qxl
->id
);
524 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
525 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
526 info
->num_memslots
= NUM_MEMSLOTS
;
527 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
528 info
->internal_groupslot_id
= 0;
530 le32_to_cpu(qxl
->shadow_rom
.num_pages
) << QXL_PAGE_BITS
;
531 info
->n_surfaces
= qxl
->ssd
.num_surfaces
;
534 static const char *qxl_mode_to_string(int mode
)
537 case QXL_MODE_COMPAT
:
539 case QXL_MODE_NATIVE
:
541 case QXL_MODE_UNDEFINED
:
549 static const char *io_port_to_string(uint32_t io_port
)
551 if (io_port
>= QXL_IO_RANGE_SIZE
) {
552 return "out of range";
554 static const char *io_port_to_string
[QXL_IO_RANGE_SIZE
+ 1] = {
555 [QXL_IO_NOTIFY_CMD
] = "QXL_IO_NOTIFY_CMD",
556 [QXL_IO_NOTIFY_CURSOR
] = "QXL_IO_NOTIFY_CURSOR",
557 [QXL_IO_UPDATE_AREA
] = "QXL_IO_UPDATE_AREA",
558 [QXL_IO_UPDATE_IRQ
] = "QXL_IO_UPDATE_IRQ",
559 [QXL_IO_NOTIFY_OOM
] = "QXL_IO_NOTIFY_OOM",
560 [QXL_IO_RESET
] = "QXL_IO_RESET",
561 [QXL_IO_SET_MODE
] = "QXL_IO_SET_MODE",
562 [QXL_IO_LOG
] = "QXL_IO_LOG",
563 [QXL_IO_MEMSLOT_ADD
] = "QXL_IO_MEMSLOT_ADD",
564 [QXL_IO_MEMSLOT_DEL
] = "QXL_IO_MEMSLOT_DEL",
565 [QXL_IO_DETACH_PRIMARY
] = "QXL_IO_DETACH_PRIMARY",
566 [QXL_IO_ATTACH_PRIMARY
] = "QXL_IO_ATTACH_PRIMARY",
567 [QXL_IO_CREATE_PRIMARY
] = "QXL_IO_CREATE_PRIMARY",
568 [QXL_IO_DESTROY_PRIMARY
] = "QXL_IO_DESTROY_PRIMARY",
569 [QXL_IO_DESTROY_SURFACE_WAIT
] = "QXL_IO_DESTROY_SURFACE_WAIT",
570 [QXL_IO_DESTROY_ALL_SURFACES
] = "QXL_IO_DESTROY_ALL_SURFACES",
571 [QXL_IO_UPDATE_AREA_ASYNC
] = "QXL_IO_UPDATE_AREA_ASYNC",
572 [QXL_IO_MEMSLOT_ADD_ASYNC
] = "QXL_IO_MEMSLOT_ADD_ASYNC",
573 [QXL_IO_CREATE_PRIMARY_ASYNC
] = "QXL_IO_CREATE_PRIMARY_ASYNC",
574 [QXL_IO_DESTROY_PRIMARY_ASYNC
] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
575 [QXL_IO_DESTROY_SURFACE_ASYNC
] = "QXL_IO_DESTROY_SURFACE_ASYNC",
576 [QXL_IO_DESTROY_ALL_SURFACES_ASYNC
]
577 = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
578 [QXL_IO_FLUSH_SURFACES_ASYNC
] = "QXL_IO_FLUSH_SURFACES_ASYNC",
579 [QXL_IO_FLUSH_RELEASE
] = "QXL_IO_FLUSH_RELEASE",
580 [QXL_IO_MONITORS_CONFIG_ASYNC
] = "QXL_IO_MONITORS_CONFIG_ASYNC",
582 return io_port_to_string
[io_port
];
585 /* called from spice server thread context only */
586 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
588 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
589 SimpleSpiceUpdate
*update
;
590 QXLCommandRing
*ring
;
594 trace_qxl_ring_command_check(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
599 qemu_mutex_lock(&qxl
->ssd
.lock
);
600 update
= QTAILQ_FIRST(&qxl
->ssd
.updates
);
601 if (update
!= NULL
) {
602 QTAILQ_REMOVE(&qxl
->ssd
.updates
, update
, next
);
606 qemu_mutex_unlock(&qxl
->ssd
.lock
);
608 trace_qxl_ring_command_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
609 qxl_log_command(qxl
, "vga", ext
);
612 case QXL_MODE_COMPAT
:
613 case QXL_MODE_NATIVE
:
614 case QXL_MODE_UNDEFINED
:
615 ring
= &qxl
->ram
->cmd_ring
;
616 if (qxl
->guest_bug
|| SPICE_RING_IS_EMPTY(ring
)) {
619 SPICE_RING_CONS_ITEM(qxl
, ring
, cmd
);
624 ext
->group_id
= MEMSLOT_GROUP_GUEST
;
625 ext
->flags
= qxl
->cmdflags
;
626 SPICE_RING_POP(ring
, notify
);
627 qxl_ring_set_dirty(qxl
);
629 qxl_send_events(qxl
, QXL_INTERRUPT_DISPLAY
);
631 qxl
->guest_primary
.commands
++;
632 qxl_track_command(qxl
, ext
);
633 qxl_log_command(qxl
, "cmd", ext
);
634 trace_qxl_ring_command_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
641 /* called from spice server thread context only */
642 static int interface_req_cmd_notification(QXLInstance
*sin
)
644 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
647 trace_qxl_ring_command_req_notification(qxl
->id
);
649 case QXL_MODE_COMPAT
:
650 case QXL_MODE_NATIVE
:
651 case QXL_MODE_UNDEFINED
:
652 SPICE_RING_CONS_WAIT(&qxl
->ram
->cmd_ring
, wait
);
653 qxl_ring_set_dirty(qxl
);
662 /* called from spice server thread context only */
663 static inline void qxl_push_free_res(PCIQXLDevice
*d
, int flush
)
665 QXLReleaseRing
*ring
= &d
->ram
->release_ring
;
669 #define QXL_FREE_BUNCH_SIZE 32
671 if (ring
->prod
- ring
->cons
+ 1 == ring
->num_items
) {
672 /* ring full -- can't push */
675 if (!flush
&& d
->oom_running
) {
676 /* collect everything from oom handler before pushing */
679 if (!flush
&& d
->num_free_res
< QXL_FREE_BUNCH_SIZE
) {
680 /* collect a bit more before pushing */
684 SPICE_RING_PUSH(ring
, notify
);
685 trace_qxl_ring_res_push(d
->id
, qxl_mode_to_string(d
->mode
),
686 d
->guest_surfaces
.count
, d
->num_free_res
,
687 d
->last_release
, notify
? "yes" : "no");
688 trace_qxl_ring_res_push_rest(d
->id
, ring
->prod
- ring
->cons
,
689 ring
->num_items
, ring
->prod
, ring
->cons
);
691 qxl_send_events(d
, QXL_INTERRUPT_DISPLAY
);
693 SPICE_RING_PROD_ITEM(d
, ring
, item
);
699 d
->last_release
= NULL
;
700 qxl_ring_set_dirty(d
);
703 /* called from spice server thread context only */
704 static void interface_release_resource(QXLInstance
*sin
,
705 struct QXLReleaseInfoExt ext
)
707 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
708 QXLReleaseRing
*ring
;
711 if (ext
.group_id
== MEMSLOT_GROUP_HOST
) {
712 /* host group -> vga mode update request */
713 QXLCommandExt
*cmdext
= (void *)(ext
.info
->id
);
714 SimpleSpiceUpdate
*update
;
715 g_assert(cmdext
->cmd
.type
== QXL_CMD_DRAW
);
716 update
= container_of(cmdext
, SimpleSpiceUpdate
, ext
);
717 qemu_spice_destroy_update(&qxl
->ssd
, update
);
722 * ext->info points into guest-visible memory
723 * pci bar 0, $command.release_info
725 ring
= &qxl
->ram
->release_ring
;
726 SPICE_RING_PROD_ITEM(qxl
, ring
, item
);
731 /* stick head into the ring */
734 qxl_ram_set_dirty(qxl
, &ext
.info
->next
);
736 qxl_ring_set_dirty(qxl
);
738 /* append item to the list */
739 qxl
->last_release
->next
= ext
.info
->id
;
740 qxl_ram_set_dirty(qxl
, &qxl
->last_release
->next
);
742 qxl_ram_set_dirty(qxl
, &ext
.info
->next
);
744 qxl
->last_release
= ext
.info
;
746 trace_qxl_ring_res_put(qxl
->id
, qxl
->num_free_res
);
747 qxl_push_free_res(qxl
, 0);
750 /* called from spice server thread context only */
751 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
753 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
758 trace_qxl_ring_cursor_check(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
761 case QXL_MODE_COMPAT
:
762 case QXL_MODE_NATIVE
:
763 case QXL_MODE_UNDEFINED
:
764 ring
= &qxl
->ram
->cursor_ring
;
765 if (SPICE_RING_IS_EMPTY(ring
)) {
768 SPICE_RING_CONS_ITEM(qxl
, ring
, cmd
);
773 ext
->group_id
= MEMSLOT_GROUP_GUEST
;
774 ext
->flags
= qxl
->cmdflags
;
775 SPICE_RING_POP(ring
, notify
);
776 qxl_ring_set_dirty(qxl
);
778 qxl_send_events(qxl
, QXL_INTERRUPT_CURSOR
);
780 qxl
->guest_primary
.commands
++;
781 qxl_track_command(qxl
, ext
);
782 qxl_log_command(qxl
, "csr", ext
);
784 qxl_render_cursor(qxl
, ext
);
786 trace_qxl_ring_cursor_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
793 /* called from spice server thread context only */
794 static int interface_req_cursor_notification(QXLInstance
*sin
)
796 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
799 trace_qxl_ring_cursor_req_notification(qxl
->id
);
801 case QXL_MODE_COMPAT
:
802 case QXL_MODE_NATIVE
:
803 case QXL_MODE_UNDEFINED
:
804 SPICE_RING_CONS_WAIT(&qxl
->ram
->cursor_ring
, wait
);
805 qxl_ring_set_dirty(qxl
);
814 /* called from spice server thread context */
815 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
818 * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
819 * use by xf86-video-qxl and is defined out in the qxl windows driver.
820 * Probably was at some earlier version that is prior to git start (2009),
821 * and is still guest trigerrable.
823 fprintf(stderr
, "%s: deprecated\n", __func__
);
826 /* called from spice server thread context only */
827 static int interface_flush_resources(QXLInstance
*sin
)
829 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
832 ret
= qxl
->num_free_res
;
834 qxl_push_free_res(qxl
, 1);
839 static void qxl_create_guest_primary_complete(PCIQXLDevice
*d
);
841 /* called from spice server thread context only */
842 static void interface_async_complete_io(PCIQXLDevice
*qxl
, QXLCookie
*cookie
)
844 uint32_t current_async
;
846 qemu_mutex_lock(&qxl
->async_lock
);
847 current_async
= qxl
->current_async
;
848 qxl
->current_async
= QXL_UNDEFINED_IO
;
849 qemu_mutex_unlock(&qxl
->async_lock
);
851 trace_qxl_interface_async_complete_io(qxl
->id
, current_async
, cookie
);
853 fprintf(stderr
, "qxl: %s: error, cookie is NULL\n", __func__
);
856 if (cookie
&& current_async
!= cookie
->io
) {
858 "qxl: %s: error: current_async = %d != %"
859 PRId64
" = cookie->io\n", __func__
, current_async
, cookie
->io
);
861 switch (current_async
) {
862 case QXL_IO_MEMSLOT_ADD_ASYNC
:
863 case QXL_IO_DESTROY_PRIMARY_ASYNC
:
864 case QXL_IO_UPDATE_AREA_ASYNC
:
865 case QXL_IO_FLUSH_SURFACES_ASYNC
:
866 case QXL_IO_MONITORS_CONFIG_ASYNC
:
868 case QXL_IO_CREATE_PRIMARY_ASYNC
:
869 qxl_create_guest_primary_complete(qxl
);
871 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC
:
872 qxl_spice_destroy_surfaces_complete(qxl
);
874 case QXL_IO_DESTROY_SURFACE_ASYNC
:
875 qxl_spice_destroy_surface_wait_complete(qxl
, cookie
->u
.surface_id
);
878 fprintf(stderr
, "qxl: %s: unexpected current_async %d\n", __func__
,
881 qxl_send_events(qxl
, QXL_INTERRUPT_IO_CMD
);
884 /* called from spice server thread context only */
885 static void interface_update_area_complete(QXLInstance
*sin
,
887 QXLRect
*dirty
, uint32_t num_updated_rects
)
889 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
893 qemu_mutex_lock(&qxl
->ssd
.lock
);
894 if (surface_id
!= 0 || !qxl
->render_update_cookie_num
) {
895 qemu_mutex_unlock(&qxl
->ssd
.lock
);
898 trace_qxl_interface_update_area_complete(qxl
->id
, surface_id
, dirty
->left
,
899 dirty
->right
, dirty
->top
, dirty
->bottom
);
900 trace_qxl_interface_update_area_complete_rest(qxl
->id
, num_updated_rects
);
901 if (qxl
->num_dirty_rects
+ num_updated_rects
> QXL_NUM_DIRTY_RECTS
) {
903 * overflow - treat this as a full update. Not expected to be common.
905 trace_qxl_interface_update_area_complete_overflow(qxl
->id
,
906 QXL_NUM_DIRTY_RECTS
);
907 qxl
->guest_primary
.resized
= 1;
909 if (qxl
->guest_primary
.resized
) {
911 * Don't bother copying or scheduling the bh since we will flip
912 * the whole area anyway on completion of the update_area async call
914 qemu_mutex_unlock(&qxl
->ssd
.lock
);
917 qxl_i
= qxl
->num_dirty_rects
;
918 for (i
= 0; i
< num_updated_rects
; i
++) {
919 qxl
->dirty
[qxl_i
++] = dirty
[i
];
921 qxl
->num_dirty_rects
+= num_updated_rects
;
922 trace_qxl_interface_update_area_complete_schedule_bh(qxl
->id
,
923 qxl
->num_dirty_rects
);
924 qemu_bh_schedule(qxl
->update_area_bh
);
925 qemu_mutex_unlock(&qxl
->ssd
.lock
);
928 /* called from spice server thread context only */
929 static void interface_async_complete(QXLInstance
*sin
, uint64_t cookie_token
)
931 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
932 QXLCookie
*cookie
= (QXLCookie
*)(uintptr_t)cookie_token
;
934 switch (cookie
->type
) {
935 case QXL_COOKIE_TYPE_IO
:
936 interface_async_complete_io(qxl
, cookie
);
939 case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA
:
940 qxl_render_update_area_done(qxl
, cookie
);
942 case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG
:
945 fprintf(stderr
, "qxl: %s: unexpected cookie type %d\n",
946 __func__
, cookie
->type
);
951 /* called from spice server thread context only */
952 static void interface_set_client_capabilities(QXLInstance
*sin
,
953 uint8_t client_present
,
956 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
958 if (qxl
->revision
< 4) {
959 trace_qxl_set_client_capabilities_unsupported_by_revision(qxl
->id
,
964 if (runstate_check(RUN_STATE_INMIGRATE
) ||
965 runstate_check(RUN_STATE_POSTMIGRATE
)) {
969 qxl
->shadow_rom
.client_present
= client_present
;
970 memcpy(qxl
->shadow_rom
.client_capabilities
, caps
,
971 sizeof(qxl
->shadow_rom
.client_capabilities
));
972 qxl
->rom
->client_present
= client_present
;
973 memcpy(qxl
->rom
->client_capabilities
, caps
,
974 sizeof(qxl
->rom
->client_capabilities
));
975 qxl_rom_set_dirty(qxl
);
977 qxl_send_events(qxl
, QXL_INTERRUPT_CLIENT
);
980 static uint32_t qxl_crc32(const uint8_t *p
, unsigned len
)
983 * zlib xors the seed with 0xffffffff, and xors the result
984 * again with 0xffffffff; Both are not done with linux's crc32,
985 * which we want to be compatible with, so undo that.
987 return crc32(0xffffffff, p
, len
) ^ 0xffffffff;
990 /* called from main context only */
991 static int interface_client_monitors_config(QXLInstance
*sin
,
992 VDAgentMonitorsConfig
*monitors_config
)
994 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
995 QXLRom
*rom
= memory_region_get_ram_ptr(&qxl
->rom_bar
);
998 if (qxl
->revision
< 4) {
999 trace_qxl_client_monitors_config_unsupported_by_device(qxl
->id
,
1004 * Older windows drivers set int_mask to 0 when their ISR is called,
1005 * then later set it to ~0. So it doesn't relate to the actual interrupts
1006 * handled. However, they are old, so clearly they don't support this
1009 if (qxl
->ram
->int_mask
== 0 || qxl
->ram
->int_mask
== ~0 ||
1010 !(qxl
->ram
->int_mask
& QXL_INTERRUPT_CLIENT_MONITORS_CONFIG
)) {
1011 trace_qxl_client_monitors_config_unsupported_by_guest(qxl
->id
,
1016 if (!monitors_config
) {
1019 memset(&rom
->client_monitors_config
, 0,
1020 sizeof(rom
->client_monitors_config
));
1021 rom
->client_monitors_config
.count
= monitors_config
->num_of_monitors
;
1022 /* monitors_config->flags ignored */
1023 if (rom
->client_monitors_config
.count
>=
1024 ARRAY_SIZE(rom
->client_monitors_config
.heads
)) {
1025 trace_qxl_client_monitors_config_capped(qxl
->id
,
1026 monitors_config
->num_of_monitors
,
1027 ARRAY_SIZE(rom
->client_monitors_config
.heads
));
1028 rom
->client_monitors_config
.count
=
1029 ARRAY_SIZE(rom
->client_monitors_config
.heads
);
1031 for (i
= 0 ; i
< rom
->client_monitors_config
.count
; ++i
) {
1032 VDAgentMonConfig
*monitor
= &monitors_config
->monitors
[i
];
1033 QXLURect
*rect
= &rom
->client_monitors_config
.heads
[i
];
1034 /* monitor->depth ignored */
1035 rect
->left
= monitor
->x
;
1036 rect
->top
= monitor
->y
;
1037 rect
->right
= monitor
->x
+ monitor
->width
;
1038 rect
->bottom
= monitor
->y
+ monitor
->height
;
1040 rom
->client_monitors_config_crc
= qxl_crc32(
1041 (const uint8_t *)&rom
->client_monitors_config
,
1042 sizeof(rom
->client_monitors_config
));
1043 trace_qxl_client_monitors_config_crc(qxl
->id
,
1044 sizeof(rom
->client_monitors_config
),
1045 rom
->client_monitors_config_crc
);
1047 trace_qxl_interrupt_client_monitors_config(qxl
->id
,
1048 rom
->client_monitors_config
.count
,
1049 rom
->client_monitors_config
.heads
);
1050 qxl_send_events(qxl
, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG
);
1054 static const QXLInterface qxl_interface
= {
1055 .base
.type
= SPICE_INTERFACE_QXL
,
1056 .base
.description
= "qxl gpu",
1057 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
1058 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
1060 .attache_worker
= interface_attach_worker
,
1061 .set_compression_level
= interface_set_compression_level
,
1062 .set_mm_time
= interface_set_mm_time
,
1063 .get_init_info
= interface_get_init_info
,
1065 /* the callbacks below are called from spice server thread context */
1066 .get_command
= interface_get_command
,
1067 .req_cmd_notification
= interface_req_cmd_notification
,
1068 .release_resource
= interface_release_resource
,
1069 .get_cursor_command
= interface_get_cursor_command
,
1070 .req_cursor_notification
= interface_req_cursor_notification
,
1071 .notify_update
= interface_notify_update
,
1072 .flush_resources
= interface_flush_resources
,
1073 .async_complete
= interface_async_complete
,
1074 .update_area_complete
= interface_update_area_complete
,
1075 .set_client_capabilities
= interface_set_client_capabilities
,
1076 .client_monitors_config
= interface_client_monitors_config
,
1079 static void qxl_enter_vga_mode(PCIQXLDevice
*d
)
1081 if (d
->mode
== QXL_MODE_VGA
) {
1084 trace_qxl_enter_vga_mode(d
->id
);
1085 #if SPICE_SERVER_VERSION >= 0x000c03 /* release 0.12.3 */
1086 spice_qxl_driver_unload(&d
->ssd
.qxl
);
1088 qemu_spice_create_host_primary(&d
->ssd
);
1089 d
->mode
= QXL_MODE_VGA
;
1090 vga_dirty_log_start(&d
->vga
);
1091 graphic_hw_update(d
->vga
.con
);
1094 static void qxl_exit_vga_mode(PCIQXLDevice
*d
)
1096 if (d
->mode
!= QXL_MODE_VGA
) {
1099 trace_qxl_exit_vga_mode(d
->id
);
1100 vga_dirty_log_stop(&d
->vga
);
1101 qxl_destroy_primary(d
, QXL_SYNC
);
1104 static void qxl_update_irq(PCIQXLDevice
*d
)
1106 uint32_t pending
= le32_to_cpu(d
->ram
->int_pending
);
1107 uint32_t mask
= le32_to_cpu(d
->ram
->int_mask
);
1108 int level
= !!(pending
& mask
);
1109 pci_set_irq(&d
->pci
, level
);
1110 qxl_ring_set_dirty(d
);
1113 static void qxl_check_state(PCIQXLDevice
*d
)
1115 QXLRam
*ram
= d
->ram
;
1116 int spice_display_running
= qemu_spice_display_is_running(&d
->ssd
);
1118 assert(!spice_display_running
|| SPICE_RING_IS_EMPTY(&ram
->cmd_ring
));
1119 assert(!spice_display_running
|| SPICE_RING_IS_EMPTY(&ram
->cursor_ring
));
1122 static void qxl_reset_state(PCIQXLDevice
*d
)
1124 QXLRom
*rom
= d
->rom
;
1127 d
->shadow_rom
.update_id
= cpu_to_le32(0);
1128 *rom
= d
->shadow_rom
;
1129 qxl_rom_set_dirty(d
);
1131 d
->num_free_res
= 0;
1132 d
->last_release
= NULL
;
1133 memset(&d
->ssd
.dirty
, 0, sizeof(d
->ssd
.dirty
));
1137 static void qxl_soft_reset(PCIQXLDevice
*d
)
1139 trace_qxl_soft_reset(d
->id
);
1141 qxl_clear_guest_bug(d
);
1142 d
->current_async
= QXL_UNDEFINED_IO
;
1145 qxl_enter_vga_mode(d
);
1147 d
->mode
= QXL_MODE_UNDEFINED
;
1151 static void qxl_hard_reset(PCIQXLDevice
*d
, int loadvm
)
1153 bool startstop
= qemu_spice_display_is_running(&d
->ssd
);
1155 trace_qxl_hard_reset(d
->id
, loadvm
);
1158 qemu_spice_display_stop();
1161 qxl_spice_reset_cursor(d
);
1162 qxl_spice_reset_image_cache(d
);
1163 qxl_reset_surfaces(d
);
1164 qxl_reset_memslots(d
);
1166 /* pre loadvm reset must not touch QXLRam. This lives in
1167 * device memory, is migrated together with RAM and thus
1168 * already loaded at this point */
1172 qemu_spice_create_host_memslot(&d
->ssd
);
1176 qemu_spice_display_start();
1180 static void qxl_reset_handler(DeviceState
*dev
)
1182 PCIQXLDevice
*d
= DO_UPCAST(PCIQXLDevice
, pci
.qdev
, dev
);
1184 qxl_hard_reset(d
, 0);
1187 static void qxl_vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1189 VGACommonState
*vga
= opaque
;
1190 PCIQXLDevice
*qxl
= container_of(vga
, PCIQXLDevice
, vga
);
1192 trace_qxl_io_write_vga(qxl
->id
, qxl_mode_to_string(qxl
->mode
), addr
, val
);
1193 if (qxl
->mode
!= QXL_MODE_VGA
) {
1194 qxl_destroy_primary(qxl
, QXL_SYNC
);
1195 qxl_soft_reset(qxl
);
1197 vga_ioport_write(opaque
, addr
, val
);
1200 static const MemoryRegionPortio qxl_vga_portio_list
[] = {
1201 { 0x04, 2, 1, .read
= vga_ioport_read
,
1202 .write
= qxl_vga_ioport_write
}, /* 3b4 */
1203 { 0x0a, 1, 1, .read
= vga_ioport_read
,
1204 .write
= qxl_vga_ioport_write
}, /* 3ba */
1205 { 0x10, 16, 1, .read
= vga_ioport_read
,
1206 .write
= qxl_vga_ioport_write
}, /* 3c0 */
1207 { 0x24, 2, 1, .read
= vga_ioport_read
,
1208 .write
= qxl_vga_ioport_write
}, /* 3d4 */
1209 { 0x2a, 1, 1, .read
= vga_ioport_read
,
1210 .write
= qxl_vga_ioport_write
}, /* 3da */
1211 PORTIO_END_OF_LIST(),
1214 static int qxl_add_memslot(PCIQXLDevice
*d
, uint32_t slot_id
, uint64_t delta
,
1217 static const int regions
[] = {
1218 QXL_RAM_RANGE_INDEX
,
1219 QXL_VRAM_RANGE_INDEX
,
1220 QXL_VRAM64_RANGE_INDEX
,
1222 uint64_t guest_start
;
1227 intptr_t virt_start
;
1228 QXLDevMemSlot memslot
;
1231 guest_start
= le64_to_cpu(d
->guest_slots
[slot_id
].slot
.mem_start
);
1232 guest_end
= le64_to_cpu(d
->guest_slots
[slot_id
].slot
.mem_end
);
1234 trace_qxl_memslot_add_guest(d
->id
, slot_id
, guest_start
, guest_end
);
1236 if (slot_id
>= NUM_MEMSLOTS
) {
1237 qxl_set_guest_bug(d
, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__
,
1238 slot_id
, NUM_MEMSLOTS
);
1241 if (guest_start
> guest_end
) {
1242 qxl_set_guest_bug(d
, "%s: guest_start > guest_end 0x%" PRIx64
1243 " > 0x%" PRIx64
, __func__
, guest_start
, guest_end
);
1247 for (i
= 0; i
< ARRAY_SIZE(regions
); i
++) {
1248 pci_region
= regions
[i
];
1249 pci_start
= d
->pci
.io_regions
[pci_region
].addr
;
1250 pci_end
= pci_start
+ d
->pci
.io_regions
[pci_region
].size
;
1252 if (pci_start
== -1) {
1255 /* start address in range ? */
1256 if (guest_start
< pci_start
|| guest_start
> pci_end
) {
1259 /* end address in range ? */
1260 if (guest_end
> pci_end
) {
1266 if (i
== ARRAY_SIZE(regions
)) {
1267 qxl_set_guest_bug(d
, "%s: finished loop without match", __func__
);
1271 switch (pci_region
) {
1272 case QXL_RAM_RANGE_INDEX
:
1273 virt_start
= (intptr_t)memory_region_get_ram_ptr(&d
->vga
.vram
);
1275 case QXL_VRAM_RANGE_INDEX
:
1276 case 4 /* vram 64bit */:
1277 virt_start
= (intptr_t)memory_region_get_ram_ptr(&d
->vram_bar
);
1280 /* should not happen */
1281 qxl_set_guest_bug(d
, "%s: pci_region = %d", __func__
, pci_region
);
1285 memslot
.slot_id
= slot_id
;
1286 memslot
.slot_group_id
= MEMSLOT_GROUP_GUEST
; /* guest group */
1287 memslot
.virt_start
= virt_start
+ (guest_start
- pci_start
);
1288 memslot
.virt_end
= virt_start
+ (guest_end
- pci_start
);
1289 memslot
.addr_delta
= memslot
.virt_start
- delta
;
1290 memslot
.generation
= d
->rom
->slot_generation
= 0;
1291 qxl_rom_set_dirty(d
);
1293 qemu_spice_add_memslot(&d
->ssd
, &memslot
, async
);
1294 d
->guest_slots
[slot_id
].ptr
= (void*)memslot
.virt_start
;
1295 d
->guest_slots
[slot_id
].size
= memslot
.virt_end
- memslot
.virt_start
;
1296 d
->guest_slots
[slot_id
].delta
= delta
;
1297 d
->guest_slots
[slot_id
].active
= 1;
1301 static void qxl_del_memslot(PCIQXLDevice
*d
, uint32_t slot_id
)
1303 qemu_spice_del_memslot(&d
->ssd
, MEMSLOT_GROUP_HOST
, slot_id
);
1304 d
->guest_slots
[slot_id
].active
= 0;
1307 static void qxl_reset_memslots(PCIQXLDevice
*d
)
1309 qxl_spice_reset_memslots(d
);
1310 memset(&d
->guest_slots
, 0, sizeof(d
->guest_slots
));
1313 static void qxl_reset_surfaces(PCIQXLDevice
*d
)
1315 trace_qxl_reset_surfaces(d
->id
);
1316 d
->mode
= QXL_MODE_UNDEFINED
;
1317 qxl_spice_destroy_surfaces(d
, QXL_SYNC
);
1320 /* can be also called from spice server thread context */
1321 void *qxl_phys2virt(PCIQXLDevice
*qxl
, QXLPHYSICAL pqxl
, int group_id
)
1323 uint64_t phys
= le64_to_cpu(pqxl
);
1324 uint32_t slot
= (phys
>> (64 - 8)) & 0xff;
1325 uint64_t offset
= phys
& 0xffffffffffff;
1328 case MEMSLOT_GROUP_HOST
:
1329 return (void *)(intptr_t)offset
;
1330 case MEMSLOT_GROUP_GUEST
:
1331 if (slot
>= NUM_MEMSLOTS
) {
1332 qxl_set_guest_bug(qxl
, "slot too large %d >= %d", slot
,
1336 if (!qxl
->guest_slots
[slot
].active
) {
1337 qxl_set_guest_bug(qxl
, "inactive slot %d\n", slot
);
1340 if (offset
< qxl
->guest_slots
[slot
].delta
) {
1341 qxl_set_guest_bug(qxl
,
1342 "slot %d offset %"PRIu64
" < delta %"PRIu64
"\n",
1343 slot
, offset
, qxl
->guest_slots
[slot
].delta
);
1346 offset
-= qxl
->guest_slots
[slot
].delta
;
1347 if (offset
> qxl
->guest_slots
[slot
].size
) {
1348 qxl_set_guest_bug(qxl
,
1349 "slot %d offset %"PRIu64
" > size %"PRIu64
"\n",
1350 slot
, offset
, qxl
->guest_slots
[slot
].size
);
1353 return qxl
->guest_slots
[slot
].ptr
+ offset
;
1358 static void qxl_create_guest_primary_complete(PCIQXLDevice
*qxl
)
1360 /* for local rendering */
1361 qxl_render_resize(qxl
);
1364 static void qxl_create_guest_primary(PCIQXLDevice
*qxl
, int loadvm
,
1367 QXLDevSurfaceCreate surface
;
1368 QXLSurfaceCreate
*sc
= &qxl
->guest_primary
.surface
;
1369 uint32_t requested_height
= le32_to_cpu(sc
->height
);
1370 int requested_stride
= le32_to_cpu(sc
->stride
);
1372 if (requested_stride
== INT32_MIN
||
1373 abs(requested_stride
) * (uint64_t)requested_height
1374 > qxl
->vgamem_size
) {
1375 qxl_set_guest_bug(qxl
, "%s: requested primary larger than framebuffer"
1376 " stride %d x height %" PRIu32
" > %" PRIu32
,
1377 __func__
, requested_stride
, requested_height
,
1382 if (qxl
->mode
== QXL_MODE_NATIVE
) {
1383 qxl_set_guest_bug(qxl
, "%s: nop since already in QXL_MODE_NATIVE",
1386 qxl_exit_vga_mode(qxl
);
1388 surface
.format
= le32_to_cpu(sc
->format
);
1389 surface
.height
= le32_to_cpu(sc
->height
);
1390 surface
.mem
= le64_to_cpu(sc
->mem
);
1391 surface
.position
= le32_to_cpu(sc
->position
);
1392 surface
.stride
= le32_to_cpu(sc
->stride
);
1393 surface
.width
= le32_to_cpu(sc
->width
);
1394 surface
.type
= le32_to_cpu(sc
->type
);
1395 surface
.flags
= le32_to_cpu(sc
->flags
);
1396 trace_qxl_create_guest_primary(qxl
->id
, sc
->width
, sc
->height
, sc
->mem
,
1397 sc
->format
, sc
->position
);
1398 trace_qxl_create_guest_primary_rest(qxl
->id
, sc
->stride
, sc
->type
,
1401 if ((surface
.stride
& 0x3) != 0) {
1402 qxl_set_guest_bug(qxl
, "primary surface stride = %d %% 4 != 0",
1407 surface
.mouse_mode
= true;
1408 surface
.group_id
= MEMSLOT_GROUP_GUEST
;
1410 surface
.flags
|= QXL_SURF_FLAG_KEEP_DATA
;
1413 qxl
->mode
= QXL_MODE_NATIVE
;
1415 qemu_spice_create_primary_surface(&qxl
->ssd
, 0, &surface
, async
);
1417 if (async
== QXL_SYNC
) {
1418 qxl_create_guest_primary_complete(qxl
);
1422 /* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
1423 * done (in QXL_SYNC case), 0 otherwise. */
1424 static int qxl_destroy_primary(PCIQXLDevice
*d
, qxl_async_io async
)
1426 if (d
->mode
== QXL_MODE_UNDEFINED
) {
1429 trace_qxl_destroy_primary(d
->id
);
1430 d
->mode
= QXL_MODE_UNDEFINED
;
1431 qemu_spice_destroy_primary_surface(&d
->ssd
, 0, async
);
1432 qxl_spice_reset_cursor(d
);
1436 static void qxl_set_mode(PCIQXLDevice
*d
, unsigned int modenr
, int loadvm
)
1438 pcibus_t start
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].addr
;
1439 pcibus_t end
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].size
+ start
;
1440 QXLMode
*mode
= d
->modes
->modes
+ modenr
;
1441 uint64_t devmem
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].addr
;
1447 if (modenr
>= d
->modes
->n_modes
) {
1448 qxl_set_guest_bug(d
, "mode number out of range");
1452 QXLSurfaceCreate surface
= {
1453 .width
= mode
->x_res
,
1454 .height
= mode
->y_res
,
1455 .stride
= -mode
->x_res
* 4,
1456 .format
= SPICE_SURFACE_FMT_32_xRGB
,
1457 .flags
= loadvm
? QXL_SURF_FLAG_KEEP_DATA
: 0,
1459 .mem
= devmem
+ d
->shadow_rom
.draw_area_offset
,
1462 trace_qxl_set_mode(d
->id
, modenr
, mode
->x_res
, mode
->y_res
, mode
->bits
,
1465 qxl_hard_reset(d
, 0);
1468 d
->guest_slots
[0].slot
= slot
;
1469 assert(qxl_add_memslot(d
, 0, devmem
, QXL_SYNC
) == 0);
1471 d
->guest_primary
.surface
= surface
;
1472 qxl_create_guest_primary(d
, 0, QXL_SYNC
);
1474 d
->mode
= QXL_MODE_COMPAT
;
1475 d
->cmdflags
= QXL_COMMAND_FLAG_COMPAT
;
1476 if (mode
->bits
== 16) {
1477 d
->cmdflags
|= QXL_COMMAND_FLAG_COMPAT_16BPP
;
1479 d
->shadow_rom
.mode
= cpu_to_le32(modenr
);
1480 d
->rom
->mode
= cpu_to_le32(modenr
);
1481 qxl_rom_set_dirty(d
);
1484 static void ioport_write(void *opaque
, hwaddr addr
,
1485 uint64_t val
, unsigned size
)
1487 PCIQXLDevice
*d
= opaque
;
1488 uint32_t io_port
= addr
;
1489 qxl_async_io async
= QXL_SYNC
;
1490 uint32_t orig_io_port
= io_port
;
1492 if (d
->guest_bug
&& io_port
!= QXL_IO_RESET
) {
1496 if (d
->revision
<= QXL_REVISION_STABLE_V10
&&
1497 io_port
> QXL_IO_FLUSH_RELEASE
) {
1498 qxl_set_guest_bug(d
, "unsupported io %d for revision %d\n",
1499 io_port
, d
->revision
);
1505 case QXL_IO_SET_MODE
:
1506 case QXL_IO_MEMSLOT_ADD
:
1507 case QXL_IO_MEMSLOT_DEL
:
1508 case QXL_IO_CREATE_PRIMARY
:
1509 case QXL_IO_UPDATE_IRQ
:
1511 case QXL_IO_MEMSLOT_ADD_ASYNC
:
1512 case QXL_IO_CREATE_PRIMARY_ASYNC
:
1515 if (d
->mode
!= QXL_MODE_VGA
) {
1518 trace_qxl_io_unexpected_vga_mode(d
->id
,
1519 addr
, val
, io_port_to_string(io_port
));
1520 /* be nice to buggy guest drivers */
1521 if (io_port
>= QXL_IO_UPDATE_AREA_ASYNC
&&
1522 io_port
< QXL_IO_RANGE_SIZE
) {
1523 qxl_send_events(d
, QXL_INTERRUPT_IO_CMD
);
1528 /* we change the io_port to avoid ifdeffery in the main switch */
1529 orig_io_port
= io_port
;
1531 case QXL_IO_UPDATE_AREA_ASYNC
:
1532 io_port
= QXL_IO_UPDATE_AREA
;
1534 case QXL_IO_MEMSLOT_ADD_ASYNC
:
1535 io_port
= QXL_IO_MEMSLOT_ADD
;
1537 case QXL_IO_CREATE_PRIMARY_ASYNC
:
1538 io_port
= QXL_IO_CREATE_PRIMARY
;
1540 case QXL_IO_DESTROY_PRIMARY_ASYNC
:
1541 io_port
= QXL_IO_DESTROY_PRIMARY
;
1543 case QXL_IO_DESTROY_SURFACE_ASYNC
:
1544 io_port
= QXL_IO_DESTROY_SURFACE_WAIT
;
1546 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC
:
1547 io_port
= QXL_IO_DESTROY_ALL_SURFACES
;
1549 case QXL_IO_FLUSH_SURFACES_ASYNC
:
1550 case QXL_IO_MONITORS_CONFIG_ASYNC
:
1553 qemu_mutex_lock(&d
->async_lock
);
1554 if (d
->current_async
!= QXL_UNDEFINED_IO
) {
1555 qxl_set_guest_bug(d
, "%d async started before last (%d) complete",
1556 io_port
, d
->current_async
);
1557 qemu_mutex_unlock(&d
->async_lock
);
1560 d
->current_async
= orig_io_port
;
1561 qemu_mutex_unlock(&d
->async_lock
);
1566 trace_qxl_io_write(d
->id
, qxl_mode_to_string(d
->mode
),
1567 addr
, io_port_to_string(addr
),
1571 case QXL_IO_UPDATE_AREA
:
1573 QXLCookie
*cookie
= NULL
;
1574 QXLRect update
= d
->ram
->update_area
;
1576 if (d
->ram
->update_surface
> d
->ssd
.num_surfaces
) {
1577 qxl_set_guest_bug(d
, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
1578 d
->ram
->update_surface
);
1581 if (update
.left
>= update
.right
|| update
.top
>= update
.bottom
||
1582 update
.left
< 0 || update
.top
< 0) {
1583 qxl_set_guest_bug(d
,
1584 "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
1585 update
.left
, update
.top
, update
.right
, update
.bottom
);
1588 if (async
== QXL_ASYNC
) {
1589 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
1590 QXL_IO_UPDATE_AREA_ASYNC
);
1591 cookie
->u
.area
= update
;
1593 qxl_spice_update_area(d
, d
->ram
->update_surface
,
1594 cookie
? &cookie
->u
.area
: &update
,
1595 NULL
, 0, 0, async
, cookie
);
1598 case QXL_IO_NOTIFY_CMD
:
1599 qemu_spice_wakeup(&d
->ssd
);
1601 case QXL_IO_NOTIFY_CURSOR
:
1602 qemu_spice_wakeup(&d
->ssd
);
1604 case QXL_IO_UPDATE_IRQ
:
1607 case QXL_IO_NOTIFY_OOM
:
1608 if (!SPICE_RING_IS_EMPTY(&d
->ram
->release_ring
)) {
1615 case QXL_IO_SET_MODE
:
1616 qxl_set_mode(d
, val
, 0);
1619 trace_qxl_io_log(d
->id
, d
->ram
->log_buf
);
1620 if (d
->guestdebug
) {
1621 fprintf(stderr
, "qxl/guest-%d: %" PRId64
": %s", d
->id
,
1622 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), d
->ram
->log_buf
);
1626 qxl_hard_reset(d
, 0);
1628 case QXL_IO_MEMSLOT_ADD
:
1629 if (val
>= NUM_MEMSLOTS
) {
1630 qxl_set_guest_bug(d
, "QXL_IO_MEMSLOT_ADD: val out of range");
1633 if (d
->guest_slots
[val
].active
) {
1634 qxl_set_guest_bug(d
,
1635 "QXL_IO_MEMSLOT_ADD: memory slot already active");
1638 d
->guest_slots
[val
].slot
= d
->ram
->mem_slot
;
1639 qxl_add_memslot(d
, val
, 0, async
);
1641 case QXL_IO_MEMSLOT_DEL
:
1642 if (val
>= NUM_MEMSLOTS
) {
1643 qxl_set_guest_bug(d
, "QXL_IO_MEMSLOT_DEL: val out of range");
1646 qxl_del_memslot(d
, val
);
1648 case QXL_IO_CREATE_PRIMARY
:
1650 qxl_set_guest_bug(d
, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
1654 d
->guest_primary
.surface
= d
->ram
->create_surface
;
1655 qxl_create_guest_primary(d
, 0, async
);
1657 case QXL_IO_DESTROY_PRIMARY
:
1659 qxl_set_guest_bug(d
, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
1663 if (!qxl_destroy_primary(d
, async
)) {
1664 trace_qxl_io_destroy_primary_ignored(d
->id
,
1665 qxl_mode_to_string(d
->mode
));
1669 case QXL_IO_DESTROY_SURFACE_WAIT
:
1670 if (val
>= d
->ssd
.num_surfaces
) {
1671 qxl_set_guest_bug(d
, "QXL_IO_DESTROY_SURFACE (async=%d):"
1672 "%" PRIu64
" >= NUM_SURFACES", async
, val
);
1675 qxl_spice_destroy_surface_wait(d
, val
, async
);
1677 case QXL_IO_FLUSH_RELEASE
: {
1678 QXLReleaseRing
*ring
= &d
->ram
->release_ring
;
1679 if (ring
->prod
- ring
->cons
+ 1 == ring
->num_items
) {
1681 "ERROR: no flush, full release ring [p%d,%dc]\n",
1682 ring
->prod
, ring
->cons
);
1684 qxl_push_free_res(d
, 1 /* flush */);
1687 case QXL_IO_FLUSH_SURFACES_ASYNC
:
1688 qxl_spice_flush_surfaces_async(d
);
1690 case QXL_IO_DESTROY_ALL_SURFACES
:
1691 d
->mode
= QXL_MODE_UNDEFINED
;
1692 qxl_spice_destroy_surfaces(d
, async
);
1694 case QXL_IO_MONITORS_CONFIG_ASYNC
:
1695 qxl_spice_monitors_config_async(d
, 0);
1698 qxl_set_guest_bug(d
, "%s: unexpected ioport=0x%x\n", __func__
, io_port
);
1703 qxl_send_events(d
, QXL_INTERRUPT_IO_CMD
);
1704 qemu_mutex_lock(&d
->async_lock
);
1705 d
->current_async
= QXL_UNDEFINED_IO
;
1706 qemu_mutex_unlock(&d
->async_lock
);
1710 static uint64_t ioport_read(void *opaque
, hwaddr addr
,
1713 PCIQXLDevice
*qxl
= opaque
;
1715 trace_qxl_io_read_unexpected(qxl
->id
);
1719 static const MemoryRegionOps qxl_io_ops
= {
1720 .read
= ioport_read
,
1721 .write
= ioport_write
,
1723 .min_access_size
= 1,
1724 .max_access_size
= 1,
1728 static void qxl_update_irq_bh(void *opaque
)
1730 PCIQXLDevice
*d
= opaque
;
1734 static void qxl_send_events(PCIQXLDevice
*d
, uint32_t events
)
1736 uint32_t old_pending
;
1737 uint32_t le_events
= cpu_to_le32(events
);
1739 trace_qxl_send_events(d
->id
, events
);
1740 if (!qemu_spice_display_is_running(&d
->ssd
)) {
1741 /* spice-server tracks guest running state and should not do this */
1742 fprintf(stderr
, "%s: spice-server bug: guest stopped, ignoring\n",
1744 trace_qxl_send_events_vm_stopped(d
->id
, events
);
1747 old_pending
= atomic_fetch_or(&d
->ram
->int_pending
, le_events
);
1748 if ((old_pending
& le_events
) == le_events
) {
1751 qemu_bh_schedule(d
->update_irq
);
1754 /* graphics console */
1756 static void qxl_hw_update(void *opaque
)
1758 PCIQXLDevice
*qxl
= opaque
;
1759 VGACommonState
*vga
= &qxl
->vga
;
1761 switch (qxl
->mode
) {
1763 vga
->hw_ops
->gfx_update(vga
);
1765 case QXL_MODE_COMPAT
:
1766 case QXL_MODE_NATIVE
:
1767 qxl_render_update(qxl
);
1774 static void qxl_hw_invalidate(void *opaque
)
1776 PCIQXLDevice
*qxl
= opaque
;
1777 VGACommonState
*vga
= &qxl
->vga
;
1779 if (qxl
->mode
== QXL_MODE_VGA
) {
1780 vga
->hw_ops
->invalidate(vga
);
1785 static void qxl_hw_text_update(void *opaque
, console_ch_t
*chardata
)
1787 PCIQXLDevice
*qxl
= opaque
;
1788 VGACommonState
*vga
= &qxl
->vga
;
1790 if (qxl
->mode
== QXL_MODE_VGA
) {
1791 vga
->hw_ops
->text_update(vga
, chardata
);
1796 static void qxl_dirty_surfaces(PCIQXLDevice
*qxl
)
1798 uintptr_t vram_start
;
1801 if (qxl
->mode
!= QXL_MODE_NATIVE
&& qxl
->mode
!= QXL_MODE_COMPAT
) {
1805 /* dirty the primary surface */
1806 qxl_set_dirty(&qxl
->vga
.vram
, qxl
->shadow_rom
.draw_area_offset
,
1807 qxl
->shadow_rom
.surface0_area_size
);
1809 vram_start
= (uintptr_t)memory_region_get_ram_ptr(&qxl
->vram_bar
);
1811 /* dirty the off-screen surfaces */
1812 for (i
= 0; i
< qxl
->ssd
.num_surfaces
; i
++) {
1814 intptr_t surface_offset
;
1817 if (qxl
->guest_surfaces
.cmds
[i
] == 0) {
1821 cmd
= qxl_phys2virt(qxl
, qxl
->guest_surfaces
.cmds
[i
],
1822 MEMSLOT_GROUP_GUEST
);
1824 assert(cmd
->type
== QXL_SURFACE_CMD_CREATE
);
1825 surface_offset
= (intptr_t)qxl_phys2virt(qxl
,
1826 cmd
->u
.surface_create
.data
,
1827 MEMSLOT_GROUP_GUEST
);
1828 assert(surface_offset
);
1829 surface_offset
-= vram_start
;
1830 surface_size
= cmd
->u
.surface_create
.height
*
1831 abs(cmd
->u
.surface_create
.stride
);
1832 trace_qxl_surfaces_dirty(qxl
->id
, i
, (int)surface_offset
, surface_size
);
1833 qxl_set_dirty(&qxl
->vram_bar
, surface_offset
, surface_size
);
1837 static void qxl_vm_change_state_handler(void *opaque
, int running
,
1840 PCIQXLDevice
*qxl
= opaque
;
1844 * if qxl_send_events was called from spice server context before
1845 * migration ended, qxl_update_irq for these events might not have been
1848 qxl_update_irq(qxl
);
1850 /* make sure surfaces are saved before migration */
1851 qxl_dirty_surfaces(qxl
);
1855 /* display change listener */
1857 static void display_update(DisplayChangeListener
*dcl
,
1858 int x
, int y
, int w
, int h
)
1860 PCIQXLDevice
*qxl
= container_of(dcl
, PCIQXLDevice
, ssd
.dcl
);
1862 if (qxl
->mode
== QXL_MODE_VGA
) {
1863 qemu_spice_display_update(&qxl
->ssd
, x
, y
, w
, h
);
1867 static void display_switch(DisplayChangeListener
*dcl
,
1868 struct DisplaySurface
*surface
)
1870 PCIQXLDevice
*qxl
= container_of(dcl
, PCIQXLDevice
, ssd
.dcl
);
1872 qxl
->ssd
.ds
= surface
;
1873 if (qxl
->mode
== QXL_MODE_VGA
) {
1874 qemu_spice_display_switch(&qxl
->ssd
, surface
);
1878 static void display_refresh(DisplayChangeListener
*dcl
)
1880 PCIQXLDevice
*qxl
= container_of(dcl
, PCIQXLDevice
, ssd
.dcl
);
1882 if (qxl
->mode
== QXL_MODE_VGA
) {
1883 qemu_spice_display_refresh(&qxl
->ssd
);
1885 qemu_mutex_lock(&qxl
->ssd
.lock
);
1886 qemu_spice_cursor_refresh_unlocked(&qxl
->ssd
);
1887 qemu_mutex_unlock(&qxl
->ssd
.lock
);
1891 static DisplayChangeListenerOps display_listener_ops
= {
1892 .dpy_name
= "spice/qxl",
1893 .dpy_gfx_update
= display_update
,
1894 .dpy_gfx_switch
= display_switch
,
1895 .dpy_refresh
= display_refresh
,
1898 static void qxl_init_ramsize(PCIQXLDevice
*qxl
)
1900 /* vga mode framebuffer / primary surface (bar 0, first part) */
1901 if (qxl
->vgamem_size_mb
< 8) {
1902 qxl
->vgamem_size_mb
= 8;
1904 qxl
->vgamem_size
= qxl
->vgamem_size_mb
* 1024 * 1024;
1906 /* vga ram (bar 0, total) */
1907 if (qxl
->ram_size_mb
!= -1) {
1908 qxl
->vga
.vram_size
= qxl
->ram_size_mb
* 1024 * 1024;
1910 if (qxl
->vga
.vram_size
< qxl
->vgamem_size
* 2) {
1911 qxl
->vga
.vram_size
= qxl
->vgamem_size
* 2;
1914 /* vram32 (surfaces, 32bit, bar 1) */
1915 if (qxl
->vram32_size_mb
!= -1) {
1916 qxl
->vram32_size
= qxl
->vram32_size_mb
* 1024 * 1024;
1918 if (qxl
->vram32_size
< 4096) {
1919 qxl
->vram32_size
= 4096;
1922 /* vram (surfaces, 64bit, bar 4+5) */
1923 if (qxl
->vram_size_mb
!= -1) {
1924 qxl
->vram_size
= qxl
->vram_size_mb
* 1024 * 1024;
1926 if (qxl
->vram_size
< qxl
->vram32_size
) {
1927 qxl
->vram_size
= qxl
->vram32_size
;
1930 if (qxl
->revision
== 1) {
1931 qxl
->vram32_size
= 4096;
1932 qxl
->vram_size
= 4096;
1934 qxl
->vgamem_size
= msb_mask(qxl
->vgamem_size
* 2 - 1);
1935 qxl
->vga
.vram_size
= msb_mask(qxl
->vga
.vram_size
* 2 - 1);
1936 qxl
->vram32_size
= msb_mask(qxl
->vram32_size
* 2 - 1);
1937 qxl
->vram_size
= msb_mask(qxl
->vram_size
* 2 - 1);
1940 static int qxl_init_common(PCIQXLDevice
*qxl
)
1942 uint8_t* config
= qxl
->pci
.config
;
1943 uint32_t pci_device_rev
;
1946 qxl
->mode
= QXL_MODE_UNDEFINED
;
1947 qxl
->generation
= 1;
1948 qxl
->num_memslots
= NUM_MEMSLOTS
;
1949 qemu_mutex_init(&qxl
->track_lock
);
1950 qemu_mutex_init(&qxl
->async_lock
);
1951 qxl
->current_async
= QXL_UNDEFINED_IO
;
1954 switch (qxl
->revision
) {
1955 case 1: /* spice 0.4 -- qxl-1 */
1956 pci_device_rev
= QXL_REVISION_STABLE_V04
;
1959 case 2: /* spice 0.6 -- qxl-2 */
1960 pci_device_rev
= QXL_REVISION_STABLE_V06
;
1964 pci_device_rev
= QXL_REVISION_STABLE_V10
;
1965 io_size
= 32; /* PCI region size must be pow2 */
1968 pci_device_rev
= QXL_REVISION_STABLE_V12
;
1969 io_size
= msb_mask(QXL_IO_RANGE_SIZE
* 2 - 1);
1972 error_report("Invalid revision %d for qxl device (max %d)",
1973 qxl
->revision
, QXL_DEFAULT_REVISION
);
1977 pci_set_byte(&config
[PCI_REVISION_ID
], pci_device_rev
);
1978 pci_set_byte(&config
[PCI_INTERRUPT_PIN
], 1);
1980 qxl
->rom_size
= qxl_rom_size();
1981 memory_region_init_ram(&qxl
->rom_bar
, OBJECT(qxl
), "qxl.vrom",
1983 vmstate_register_ram(&qxl
->rom_bar
, &qxl
->pci
.qdev
);
1987 qxl
->guest_surfaces
.cmds
= g_new0(QXLPHYSICAL
, qxl
->ssd
.num_surfaces
);
1988 memory_region_init_ram(&qxl
->vram_bar
, OBJECT(qxl
), "qxl.vram",
1990 vmstate_register_ram(&qxl
->vram_bar
, &qxl
->pci
.qdev
);
1991 memory_region_init_alias(&qxl
->vram32_bar
, OBJECT(qxl
), "qxl.vram32",
1992 &qxl
->vram_bar
, 0, qxl
->vram32_size
);
1994 memory_region_init_io(&qxl
->io_bar
, OBJECT(qxl
), &qxl_io_ops
, qxl
,
1995 "qxl-ioports", io_size
);
1997 vga_dirty_log_start(&qxl
->vga
);
1999 memory_region_set_flush_coalesced(&qxl
->io_bar
);
2002 pci_register_bar(&qxl
->pci
, QXL_IO_RANGE_INDEX
,
2003 PCI_BASE_ADDRESS_SPACE_IO
, &qxl
->io_bar
);
2005 pci_register_bar(&qxl
->pci
, QXL_ROM_RANGE_INDEX
,
2006 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->rom_bar
);
2008 pci_register_bar(&qxl
->pci
, QXL_RAM_RANGE_INDEX
,
2009 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->vga
.vram
);
2011 pci_register_bar(&qxl
->pci
, QXL_VRAM_RANGE_INDEX
,
2012 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->vram32_bar
);
2014 if (qxl
->vram32_size
< qxl
->vram_size
) {
2016 * Make the 64bit vram bar show up only in case it is
2017 * configured to be larger than the 32bit vram bar.
2019 pci_register_bar(&qxl
->pci
, QXL_VRAM64_RANGE_INDEX
,
2020 PCI_BASE_ADDRESS_SPACE_MEMORY
|
2021 PCI_BASE_ADDRESS_MEM_TYPE_64
|
2022 PCI_BASE_ADDRESS_MEM_PREFETCH
,
2026 /* print pci bar details */
2027 dprint(qxl
, 1, "ram/%s: %d MB [region 0]\n",
2028 qxl
->id
== 0 ? "pri" : "sec",
2029 qxl
->vga
.vram_size
/ (1024*1024));
2030 dprint(qxl
, 1, "vram/32: %d MB [region 1]\n",
2031 qxl
->vram32_size
/ (1024*1024));
2032 dprint(qxl
, 1, "vram/64: %d MB %s\n",
2033 qxl
->vram_size
/ (1024*1024),
2034 qxl
->vram32_size
< qxl
->vram_size
? "[region 4]" : "[unmapped]");
2036 qxl
->ssd
.qxl
.base
.sif
= &qxl_interface
.base
;
2037 if (qemu_spice_add_display_interface(&qxl
->ssd
.qxl
, qxl
->vga
.con
) != 0) {
2038 error_report("qxl interface %d.%d not supported by spice-server",
2039 SPICE_INTERFACE_QXL_MAJOR
, SPICE_INTERFACE_QXL_MINOR
);
2042 qemu_add_vm_change_state_handler(qxl_vm_change_state_handler
, qxl
);
2044 qxl
->update_irq
= qemu_bh_new(qxl_update_irq_bh
, qxl
);
2045 qxl_reset_state(qxl
);
2047 qxl
->update_area_bh
= qemu_bh_new(qxl_render_update_area_bh
, qxl
);
2052 static const GraphicHwOps qxl_ops
= {
2053 .invalidate
= qxl_hw_invalidate
,
2054 .gfx_update
= qxl_hw_update
,
2055 .text_update
= qxl_hw_text_update
,
2058 static int qxl_init_primary(PCIDevice
*dev
)
2060 PCIQXLDevice
*qxl
= DO_UPCAST(PCIQXLDevice
, pci
, dev
);
2061 VGACommonState
*vga
= &qxl
->vga
;
2065 qxl_init_ramsize(qxl
);
2066 vga
->vram_size_mb
= qxl
->vga
.vram_size
>> 20;
2067 vga_common_init(vga
, OBJECT(dev
), true);
2068 vga_init(vga
, OBJECT(dev
),
2069 pci_address_space(dev
), pci_address_space_io(dev
), false);
2070 portio_list_init(&qxl
->vga_port_list
, OBJECT(dev
), qxl_vga_portio_list
,
2072 portio_list_set_flush_coalesced(&qxl
->vga_port_list
);
2073 portio_list_add(&qxl
->vga_port_list
, pci_address_space_io(dev
), 0x3b0);
2075 vga
->con
= graphic_console_init(DEVICE(dev
), 0, &qxl_ops
, qxl
);
2076 qemu_spice_display_init_common(&qxl
->ssd
);
2078 rc
= qxl_init_common(qxl
);
2083 qxl
->ssd
.dcl
.ops
= &display_listener_ops
;
2084 qxl
->ssd
.dcl
.con
= vga
->con
;
2085 register_displaychangelistener(&qxl
->ssd
.dcl
);
2089 static int qxl_init_secondary(PCIDevice
*dev
)
2091 static int device_id
= 1;
2092 PCIQXLDevice
*qxl
= DO_UPCAST(PCIQXLDevice
, pci
, dev
);
2094 qxl
->id
= device_id
++;
2095 qxl_init_ramsize(qxl
);
2096 memory_region_init_ram(&qxl
->vga
.vram
, OBJECT(dev
), "qxl.vgavram",
2097 qxl
->vga
.vram_size
);
2098 vmstate_register_ram(&qxl
->vga
.vram
, &qxl
->pci
.qdev
);
2099 qxl
->vga
.vram_ptr
= memory_region_get_ram_ptr(&qxl
->vga
.vram
);
2100 qxl
->vga
.con
= graphic_console_init(DEVICE(dev
), 0, &qxl_ops
, qxl
);
2102 return qxl_init_common(qxl
);
2105 static void qxl_pre_save(void *opaque
)
2107 PCIQXLDevice
* d
= opaque
;
2108 uint8_t *ram_start
= d
->vga
.vram_ptr
;
2110 trace_qxl_pre_save(d
->id
);
2111 if (d
->last_release
== NULL
) {
2112 d
->last_release_offset
= 0;
2114 d
->last_release_offset
= (uint8_t *)d
->last_release
- ram_start
;
2116 assert(d
->last_release_offset
< d
->vga
.vram_size
);
2119 static int qxl_pre_load(void *opaque
)
2121 PCIQXLDevice
* d
= opaque
;
2123 trace_qxl_pre_load(d
->id
);
2124 qxl_hard_reset(d
, 1);
2125 qxl_exit_vga_mode(d
);
2129 static void qxl_create_memslots(PCIQXLDevice
*d
)
2133 for (i
= 0; i
< NUM_MEMSLOTS
; i
++) {
2134 if (!d
->guest_slots
[i
].active
) {
2137 qxl_add_memslot(d
, i
, 0, QXL_SYNC
);
2141 static int qxl_post_load(void *opaque
, int version
)
2143 PCIQXLDevice
* d
= opaque
;
2144 uint8_t *ram_start
= d
->vga
.vram_ptr
;
2145 QXLCommandExt
*cmds
;
2146 int in
, out
, newmode
;
2148 assert(d
->last_release_offset
< d
->vga
.vram_size
);
2149 if (d
->last_release_offset
== 0) {
2150 d
->last_release
= NULL
;
2152 d
->last_release
= (QXLReleaseInfo
*)(ram_start
+ d
->last_release_offset
);
2155 d
->modes
= (QXLModes
*)((uint8_t*)d
->rom
+ d
->rom
->modes_offset
);
2157 trace_qxl_post_load(d
->id
, qxl_mode_to_string(d
->mode
));
2159 d
->mode
= QXL_MODE_UNDEFINED
;
2162 case QXL_MODE_UNDEFINED
:
2163 qxl_create_memslots(d
);
2166 qxl_create_memslots(d
);
2167 qxl_enter_vga_mode(d
);
2169 case QXL_MODE_NATIVE
:
2170 qxl_create_memslots(d
);
2171 qxl_create_guest_primary(d
, 1, QXL_SYNC
);
2173 /* replay surface-create and cursor-set commands */
2174 cmds
= g_malloc0(sizeof(QXLCommandExt
) * (d
->ssd
.num_surfaces
+ 1));
2175 for (in
= 0, out
= 0; in
< d
->ssd
.num_surfaces
; in
++) {
2176 if (d
->guest_surfaces
.cmds
[in
] == 0) {
2179 cmds
[out
].cmd
.data
= d
->guest_surfaces
.cmds
[in
];
2180 cmds
[out
].cmd
.type
= QXL_CMD_SURFACE
;
2181 cmds
[out
].group_id
= MEMSLOT_GROUP_GUEST
;
2184 if (d
->guest_cursor
) {
2185 cmds
[out
].cmd
.data
= d
->guest_cursor
;
2186 cmds
[out
].cmd
.type
= QXL_CMD_CURSOR
;
2187 cmds
[out
].group_id
= MEMSLOT_GROUP_GUEST
;
2190 qxl_spice_loadvm_commands(d
, cmds
, out
);
2192 if (d
->guest_monitors_config
) {
2193 qxl_spice_monitors_config_async(d
, 1);
2196 case QXL_MODE_COMPAT
:
2197 /* note: no need to call qxl_create_memslots, qxl_set_mode
2198 * creates the mem slot. */
2199 qxl_set_mode(d
, d
->shadow_rom
.mode
, 1);
2205 #define QXL_SAVE_VERSION 21
2207 static bool qxl_monitors_config_needed(void *opaque
)
2209 PCIQXLDevice
*qxl
= opaque
;
2211 return qxl
->guest_monitors_config
!= 0;
2215 static VMStateDescription qxl_memslot
= {
2216 .name
= "qxl-memslot",
2217 .version_id
= QXL_SAVE_VERSION
,
2218 .minimum_version_id
= QXL_SAVE_VERSION
,
2219 .fields
= (VMStateField
[]) {
2220 VMSTATE_UINT64(slot
.mem_start
, struct guest_slots
),
2221 VMSTATE_UINT64(slot
.mem_end
, struct guest_slots
),
2222 VMSTATE_UINT32(active
, struct guest_slots
),
2223 VMSTATE_END_OF_LIST()
2227 static VMStateDescription qxl_surface
= {
2228 .name
= "qxl-surface",
2229 .version_id
= QXL_SAVE_VERSION
,
2230 .minimum_version_id
= QXL_SAVE_VERSION
,
2231 .fields
= (VMStateField
[]) {
2232 VMSTATE_UINT32(width
, QXLSurfaceCreate
),
2233 VMSTATE_UINT32(height
, QXLSurfaceCreate
),
2234 VMSTATE_INT32(stride
, QXLSurfaceCreate
),
2235 VMSTATE_UINT32(format
, QXLSurfaceCreate
),
2236 VMSTATE_UINT32(position
, QXLSurfaceCreate
),
2237 VMSTATE_UINT32(mouse_mode
, QXLSurfaceCreate
),
2238 VMSTATE_UINT32(flags
, QXLSurfaceCreate
),
2239 VMSTATE_UINT32(type
, QXLSurfaceCreate
),
2240 VMSTATE_UINT64(mem
, QXLSurfaceCreate
),
2241 VMSTATE_END_OF_LIST()
2245 static VMStateDescription qxl_vmstate_monitors_config
= {
2246 .name
= "qxl/monitors-config",
2248 .minimum_version_id
= 1,
2249 .fields
= (VMStateField
[]) {
2250 VMSTATE_UINT64(guest_monitors_config
, PCIQXLDevice
),
2251 VMSTATE_END_OF_LIST()
2255 static VMStateDescription qxl_vmstate
= {
2257 .version_id
= QXL_SAVE_VERSION
,
2258 .minimum_version_id
= QXL_SAVE_VERSION
,
2259 .pre_save
= qxl_pre_save
,
2260 .pre_load
= qxl_pre_load
,
2261 .post_load
= qxl_post_load
,
2262 .fields
= (VMStateField
[]) {
2263 VMSTATE_PCI_DEVICE(pci
, PCIQXLDevice
),
2264 VMSTATE_STRUCT(vga
, PCIQXLDevice
, 0, vmstate_vga_common
, VGACommonState
),
2265 VMSTATE_UINT32(shadow_rom
.mode
, PCIQXLDevice
),
2266 VMSTATE_UINT32(num_free_res
, PCIQXLDevice
),
2267 VMSTATE_UINT32(last_release_offset
, PCIQXLDevice
),
2268 VMSTATE_UINT32(mode
, PCIQXLDevice
),
2269 VMSTATE_UINT32(ssd
.unique
, PCIQXLDevice
),
2270 VMSTATE_INT32_EQUAL(num_memslots
, PCIQXLDevice
),
2271 VMSTATE_STRUCT_ARRAY(guest_slots
, PCIQXLDevice
, NUM_MEMSLOTS
, 0,
2272 qxl_memslot
, struct guest_slots
),
2273 VMSTATE_STRUCT(guest_primary
.surface
, PCIQXLDevice
, 0,
2274 qxl_surface
, QXLSurfaceCreate
),
2275 VMSTATE_INT32_EQUAL(ssd
.num_surfaces
, PCIQXLDevice
),
2276 VMSTATE_VARRAY_INT32(guest_surfaces
.cmds
, PCIQXLDevice
,
2277 ssd
.num_surfaces
, 0,
2278 vmstate_info_uint64
, uint64_t),
2279 VMSTATE_UINT64(guest_cursor
, PCIQXLDevice
),
2280 VMSTATE_END_OF_LIST()
2282 .subsections
= (VMStateSubsection
[]) {
2284 .vmsd
= &qxl_vmstate_monitors_config
,
2285 .needed
= qxl_monitors_config_needed
,
2292 static Property qxl_properties
[] = {
2293 DEFINE_PROP_UINT32("ram_size", PCIQXLDevice
, vga
.vram_size
,
2295 DEFINE_PROP_UINT32("vram_size", PCIQXLDevice
, vram32_size
,
2297 DEFINE_PROP_UINT32("revision", PCIQXLDevice
, revision
,
2298 QXL_DEFAULT_REVISION
),
2299 DEFINE_PROP_UINT32("debug", PCIQXLDevice
, debug
, 0),
2300 DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice
, guestdebug
, 0),
2301 DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice
, cmdlog
, 0),
2302 DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice
, ram_size_mb
, -1),
2303 DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice
, vram32_size_mb
, -1),
2304 DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice
, vram_size_mb
, -1),
2305 DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice
, vgamem_size_mb
, 16),
2306 DEFINE_PROP_INT32("surfaces", PCIQXLDevice
, ssd
.num_surfaces
, 1024),
2307 DEFINE_PROP_END_OF_LIST(),
2310 static void qxl_primary_class_init(ObjectClass
*klass
, void *data
)
2312 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2313 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2315 k
->init
= qxl_init_primary
;
2316 k
->romfile
= "vgabios-qxl.bin";
2317 k
->vendor_id
= REDHAT_PCI_VENDOR_ID
;
2318 k
->device_id
= QXL_DEVICE_ID_STABLE
;
2319 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
2320 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
2321 dc
->desc
= "Spice QXL GPU (primary, vga compatible)";
2322 dc
->reset
= qxl_reset_handler
;
2323 dc
->vmsd
= &qxl_vmstate
;
2324 dc
->props
= qxl_properties
;
2325 dc
->hotpluggable
= false;
2328 static const TypeInfo qxl_primary_info
= {
2330 .parent
= TYPE_PCI_DEVICE
,
2331 .instance_size
= sizeof(PCIQXLDevice
),
2332 .class_init
= qxl_primary_class_init
,
2335 static void qxl_secondary_class_init(ObjectClass
*klass
, void *data
)
2337 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2338 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2340 k
->init
= qxl_init_secondary
;
2341 k
->vendor_id
= REDHAT_PCI_VENDOR_ID
;
2342 k
->device_id
= QXL_DEVICE_ID_STABLE
;
2343 k
->class_id
= PCI_CLASS_DISPLAY_OTHER
;
2344 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
2345 dc
->desc
= "Spice QXL GPU (secondary)";
2346 dc
->reset
= qxl_reset_handler
;
2347 dc
->vmsd
= &qxl_vmstate
;
2348 dc
->props
= qxl_properties
;
2351 static const TypeInfo qxl_secondary_info
= {
2353 .parent
= TYPE_PCI_DEVICE
,
2354 .instance_size
= sizeof(PCIQXLDevice
),
2355 .class_init
= qxl_secondary_class_init
,
2358 static void qxl_register_types(void)
2360 type_register_static(&qxl_primary_info
);
2361 type_register_static(&qxl_secondary_info
);
2364 type_init(qxl_register_types
)