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/>.
23 #include "qemu-common.h"
24 #include "qemu-timer.h"
25 #include "qemu-queue.h"
33 * NOTE: SPICE_RING_PROD_ITEM accesses memory on the pci bar and as
34 * such can be changed by the guest, so to avoid a guest trigerrable
35 * abort we just qxl_set_guest_bug and set the return to NULL. Still
36 * it may happen as a result of emulator bug as well.
38 #undef SPICE_RING_PROD_ITEM
39 #define SPICE_RING_PROD_ITEM(qxl, r, ret) { \
40 typeof(r) start = r; \
41 typeof(r) end = r + 1; \
42 uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r); \
43 typeof(&(r)->items[prod]) m_item = &(r)->items[prod]; \
44 if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \
45 qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
46 "! %p <= %p < %p", (uint8_t *)start, \
47 (uint8_t *)m_item, (uint8_t *)end); \
54 #undef SPICE_RING_CONS_ITEM
55 #define SPICE_RING_CONS_ITEM(qxl, r, ret) { \
56 typeof(r) start = r; \
57 typeof(r) end = r + 1; \
58 uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \
59 typeof(&(r)->items[cons]) m_item = &(r)->items[cons]; \
60 if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \
61 qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
62 "! %p <= %p < %p", (uint8_t *)start, \
63 (uint8_t *)m_item, (uint8_t *)end); \
71 #define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
73 #define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9"
75 #define QXL_MODE(_x, _y, _b, _o) \
79 .stride = (_x) * (_b) / 8, \
80 .x_mili = PIXEL_SIZE * (_x), \
81 .y_mili = PIXEL_SIZE * (_y), \
85 #define QXL_MODE_16_32(x_res, y_res, orientation) \
86 QXL_MODE(x_res, y_res, 16, orientation), \
87 QXL_MODE(x_res, y_res, 32, orientation)
89 #define QXL_MODE_EX(x_res, y_res) \
90 QXL_MODE_16_32(x_res, y_res, 0), \
91 QXL_MODE_16_32(y_res, x_res, 1), \
92 QXL_MODE_16_32(x_res, y_res, 2), \
93 QXL_MODE_16_32(y_res, x_res, 3)
95 static QXLMode qxl_modes
[] = {
96 QXL_MODE_EX(640, 480),
97 QXL_MODE_EX(800, 480),
98 QXL_MODE_EX(800, 600),
99 QXL_MODE_EX(832, 624),
100 QXL_MODE_EX(960, 640),
101 QXL_MODE_EX(1024, 600),
102 QXL_MODE_EX(1024, 768),
103 QXL_MODE_EX(1152, 864),
104 QXL_MODE_EX(1152, 870),
105 QXL_MODE_EX(1280, 720),
106 QXL_MODE_EX(1280, 760),
107 QXL_MODE_EX(1280, 768),
108 QXL_MODE_EX(1280, 800),
109 QXL_MODE_EX(1280, 960),
110 QXL_MODE_EX(1280, 1024),
111 QXL_MODE_EX(1360, 768),
112 QXL_MODE_EX(1366, 768),
113 QXL_MODE_EX(1400, 1050),
114 QXL_MODE_EX(1440, 900),
115 QXL_MODE_EX(1600, 900),
116 QXL_MODE_EX(1600, 1200),
117 QXL_MODE_EX(1680, 1050),
118 QXL_MODE_EX(1920, 1080),
119 /* these modes need more than 8 MB video memory */
120 QXL_MODE_EX(1920, 1200),
121 QXL_MODE_EX(1920, 1440),
122 QXL_MODE_EX(2048, 1536),
123 QXL_MODE_EX(2560, 1440),
124 QXL_MODE_EX(2560, 1600),
125 /* these modes need more than 16 MB video memory */
126 QXL_MODE_EX(2560, 2048),
127 QXL_MODE_EX(2800, 2100),
128 QXL_MODE_EX(3200, 2400),
131 static PCIQXLDevice
*qxl0
;
133 static void qxl_send_events(PCIQXLDevice
*d
, uint32_t events
);
134 static int qxl_destroy_primary(PCIQXLDevice
*d
, qxl_async_io async
);
135 static void qxl_reset_memslots(PCIQXLDevice
*d
);
136 static void qxl_reset_surfaces(PCIQXLDevice
*d
);
137 static void qxl_ring_set_dirty(PCIQXLDevice
*qxl
);
139 void qxl_set_guest_bug(PCIQXLDevice
*qxl
, const char *msg
, ...)
141 trace_qxl_set_guest_bug(qxl
->id
);
142 qxl_send_events(qxl
, QXL_INTERRUPT_ERROR
);
144 if (qxl
->guestdebug
) {
147 fprintf(stderr
, "qxl-%d: guest bug: ", qxl
->id
);
148 vfprintf(stderr
, msg
, ap
);
149 fprintf(stderr
, "\n");
154 static void qxl_clear_guest_bug(PCIQXLDevice
*qxl
)
159 void qxl_spice_update_area(PCIQXLDevice
*qxl
, uint32_t surface_id
,
160 struct QXLRect
*area
, struct QXLRect
*dirty_rects
,
161 uint32_t num_dirty_rects
,
162 uint32_t clear_dirty_region
,
163 qxl_async_io async
, struct QXLCookie
*cookie
)
165 trace_qxl_spice_update_area(qxl
->id
, surface_id
, area
->left
, area
->right
,
166 area
->top
, area
->bottom
);
167 trace_qxl_spice_update_area_rest(qxl
->id
, num_dirty_rects
,
169 if (async
== QXL_SYNC
) {
170 qxl
->ssd
.worker
->update_area(qxl
->ssd
.worker
, surface_id
, area
,
171 dirty_rects
, num_dirty_rects
, clear_dirty_region
);
173 assert(cookie
!= NULL
);
174 spice_qxl_update_area_async(&qxl
->ssd
.qxl
, surface_id
, area
,
175 clear_dirty_region
, (uintptr_t)cookie
);
179 static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice
*qxl
,
182 trace_qxl_spice_destroy_surface_wait_complete(qxl
->id
, id
);
183 qemu_mutex_lock(&qxl
->track_lock
);
184 qxl
->guest_surfaces
.cmds
[id
] = 0;
185 qxl
->guest_surfaces
.count
--;
186 qemu_mutex_unlock(&qxl
->track_lock
);
189 static void qxl_spice_destroy_surface_wait(PCIQXLDevice
*qxl
, uint32_t id
,
194 trace_qxl_spice_destroy_surface_wait(qxl
->id
, id
, async
);
196 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
197 QXL_IO_DESTROY_SURFACE_ASYNC
);
198 cookie
->u
.surface_id
= id
;
199 spice_qxl_destroy_surface_async(&qxl
->ssd
.qxl
, id
, (uintptr_t)cookie
);
201 qxl
->ssd
.worker
->destroy_surface_wait(qxl
->ssd
.worker
, id
);
202 qxl_spice_destroy_surface_wait_complete(qxl
, id
);
206 static void qxl_spice_flush_surfaces_async(PCIQXLDevice
*qxl
)
208 trace_qxl_spice_flush_surfaces_async(qxl
->id
, qxl
->guest_surfaces
.count
,
210 spice_qxl_flush_surfaces_async(&qxl
->ssd
.qxl
,
211 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
212 QXL_IO_FLUSH_SURFACES_ASYNC
));
215 void qxl_spice_loadvm_commands(PCIQXLDevice
*qxl
, struct QXLCommandExt
*ext
,
218 trace_qxl_spice_loadvm_commands(qxl
->id
, ext
, count
);
219 qxl
->ssd
.worker
->loadvm_commands(qxl
->ssd
.worker
, ext
, count
);
222 void qxl_spice_oom(PCIQXLDevice
*qxl
)
224 trace_qxl_spice_oom(qxl
->id
);
225 qxl
->ssd
.worker
->oom(qxl
->ssd
.worker
);
228 void qxl_spice_reset_memslots(PCIQXLDevice
*qxl
)
230 trace_qxl_spice_reset_memslots(qxl
->id
);
231 qxl
->ssd
.worker
->reset_memslots(qxl
->ssd
.worker
);
234 static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice
*qxl
)
236 trace_qxl_spice_destroy_surfaces_complete(qxl
->id
);
237 qemu_mutex_lock(&qxl
->track_lock
);
238 memset(qxl
->guest_surfaces
.cmds
, 0,
239 sizeof(qxl
->guest_surfaces
.cmds
) * qxl
->ssd
.num_surfaces
);
240 qxl
->guest_surfaces
.count
= 0;
241 qemu_mutex_unlock(&qxl
->track_lock
);
244 static void qxl_spice_destroy_surfaces(PCIQXLDevice
*qxl
, qxl_async_io async
)
246 trace_qxl_spice_destroy_surfaces(qxl
->id
, async
);
248 spice_qxl_destroy_surfaces_async(&qxl
->ssd
.qxl
,
249 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
250 QXL_IO_DESTROY_ALL_SURFACES_ASYNC
));
252 qxl
->ssd
.worker
->destroy_surfaces(qxl
->ssd
.worker
);
253 qxl_spice_destroy_surfaces_complete(qxl
);
257 static void qxl_spice_monitors_config_async(PCIQXLDevice
*qxl
, int replay
)
259 trace_qxl_spice_monitors_config(qxl
->id
);
262 * don't use QXL_COOKIE_TYPE_IO:
263 * - we are not running yet (post_load), we will assert
265 * - this is not a guest io, but a reply, so async_io isn't set.
267 spice_qxl_monitors_config_async(&qxl
->ssd
.qxl
,
268 qxl
->guest_monitors_config
,
270 (uintptr_t)qxl_cookie_new(
271 QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG
,
274 qxl
->guest_monitors_config
= qxl
->ram
->monitors_config
;
275 spice_qxl_monitors_config_async(&qxl
->ssd
.qxl
,
276 qxl
->ram
->monitors_config
,
278 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
279 QXL_IO_MONITORS_CONFIG_ASYNC
));
283 void qxl_spice_reset_image_cache(PCIQXLDevice
*qxl
)
285 trace_qxl_spice_reset_image_cache(qxl
->id
);
286 qxl
->ssd
.worker
->reset_image_cache(qxl
->ssd
.worker
);
289 void qxl_spice_reset_cursor(PCIQXLDevice
*qxl
)
291 trace_qxl_spice_reset_cursor(qxl
->id
);
292 qxl
->ssd
.worker
->reset_cursor(qxl
->ssd
.worker
);
293 qemu_mutex_lock(&qxl
->track_lock
);
294 qxl
->guest_cursor
= 0;
295 qemu_mutex_unlock(&qxl
->track_lock
);
296 if (qxl
->ssd
.cursor
) {
297 cursor_put(qxl
->ssd
.cursor
);
299 qxl
->ssd
.cursor
= cursor_builtin_hidden();
303 static inline uint32_t msb_mask(uint32_t val
)
308 mask
= ~(val
- 1) & val
;
310 } while (mask
< val
);
315 static ram_addr_t
qxl_rom_size(void)
317 uint32_t rom_size
= sizeof(QXLRom
) + sizeof(QXLModes
) + sizeof(qxl_modes
);
319 rom_size
= MAX(rom_size
, TARGET_PAGE_SIZE
);
320 rom_size
= msb_mask(rom_size
* 2 - 1);
324 static void init_qxl_rom(PCIQXLDevice
*d
)
326 QXLRom
*rom
= memory_region_get_ram_ptr(&d
->rom_bar
);
327 QXLModes
*modes
= (QXLModes
*)(rom
+ 1);
328 uint32_t ram_header_size
;
329 uint32_t surface0_area_size
;
334 memset(rom
, 0, d
->rom_size
);
336 rom
->magic
= cpu_to_le32(QXL_ROM_MAGIC
);
337 rom
->id
= cpu_to_le32(d
->id
);
338 rom
->log_level
= cpu_to_le32(d
->guestdebug
);
339 rom
->modes_offset
= cpu_to_le32(sizeof(QXLRom
));
341 rom
->slot_gen_bits
= MEMSLOT_GENERATION_BITS
;
342 rom
->slot_id_bits
= MEMSLOT_SLOT_BITS
;
343 rom
->slots_start
= 1;
344 rom
->slots_end
= NUM_MEMSLOTS
- 1;
345 rom
->n_surfaces
= cpu_to_le32(d
->ssd
.num_surfaces
);
347 for (i
= 0, n
= 0; i
< ARRAY_SIZE(qxl_modes
); i
++) {
348 fb
= qxl_modes
[i
].y_res
* qxl_modes
[i
].stride
;
349 if (fb
> d
->vgamem_size
) {
352 modes
->modes
[n
].id
= cpu_to_le32(i
);
353 modes
->modes
[n
].x_res
= cpu_to_le32(qxl_modes
[i
].x_res
);
354 modes
->modes
[n
].y_res
= cpu_to_le32(qxl_modes
[i
].y_res
);
355 modes
->modes
[n
].bits
= cpu_to_le32(qxl_modes
[i
].bits
);
356 modes
->modes
[n
].stride
= cpu_to_le32(qxl_modes
[i
].stride
);
357 modes
->modes
[n
].x_mili
= cpu_to_le32(qxl_modes
[i
].x_mili
);
358 modes
->modes
[n
].y_mili
= cpu_to_le32(qxl_modes
[i
].y_mili
);
359 modes
->modes
[n
].orientation
= cpu_to_le32(qxl_modes
[i
].orientation
);
362 modes
->n_modes
= cpu_to_le32(n
);
364 ram_header_size
= ALIGN(sizeof(QXLRam
), 4096);
365 surface0_area_size
= ALIGN(d
->vgamem_size
, 4096);
366 num_pages
= d
->vga
.vram_size
;
367 num_pages
-= ram_header_size
;
368 num_pages
-= surface0_area_size
;
369 num_pages
= num_pages
/ TARGET_PAGE_SIZE
;
371 rom
->draw_area_offset
= cpu_to_le32(0);
372 rom
->surface0_area_size
= cpu_to_le32(surface0_area_size
);
373 rom
->pages_offset
= cpu_to_le32(surface0_area_size
);
374 rom
->num_pages
= cpu_to_le32(num_pages
);
375 rom
->ram_header_offset
= cpu_to_le32(d
->vga
.vram_size
- ram_header_size
);
377 d
->shadow_rom
= *rom
;
382 static void init_qxl_ram(PCIQXLDevice
*d
)
387 buf
= d
->vga
.vram_ptr
;
388 d
->ram
= (QXLRam
*)(buf
+ le32_to_cpu(d
->shadow_rom
.ram_header_offset
));
389 d
->ram
->magic
= cpu_to_le32(QXL_RAM_MAGIC
);
390 d
->ram
->int_pending
= cpu_to_le32(0);
391 d
->ram
->int_mask
= cpu_to_le32(0);
392 d
->ram
->update_surface
= 0;
393 SPICE_RING_INIT(&d
->ram
->cmd_ring
);
394 SPICE_RING_INIT(&d
->ram
->cursor_ring
);
395 SPICE_RING_INIT(&d
->ram
->release_ring
);
396 SPICE_RING_PROD_ITEM(d
, &d
->ram
->release_ring
, item
);
399 qxl_ring_set_dirty(d
);
402 /* can be called from spice server thread context */
403 static void qxl_set_dirty(MemoryRegion
*mr
, ram_addr_t addr
, ram_addr_t end
)
405 memory_region_set_dirty(mr
, addr
, end
- addr
);
408 static void qxl_rom_set_dirty(PCIQXLDevice
*qxl
)
410 qxl_set_dirty(&qxl
->rom_bar
, 0, qxl
->rom_size
);
413 /* called from spice server thread context only */
414 static void qxl_ram_set_dirty(PCIQXLDevice
*qxl
, void *ptr
)
416 void *base
= qxl
->vga
.vram_ptr
;
420 offset
&= ~(TARGET_PAGE_SIZE
-1);
421 assert(offset
< qxl
->vga
.vram_size
);
422 qxl_set_dirty(&qxl
->vga
.vram
, offset
, offset
+ TARGET_PAGE_SIZE
);
425 /* can be called from spice server thread context */
426 static void qxl_ring_set_dirty(PCIQXLDevice
*qxl
)
428 ram_addr_t addr
= qxl
->shadow_rom
.ram_header_offset
;
429 ram_addr_t end
= qxl
->vga
.vram_size
;
430 qxl_set_dirty(&qxl
->vga
.vram
, addr
, end
);
434 * keep track of some command state, for savevm/loadvm.
435 * called from spice server thread context only
437 static int qxl_track_command(PCIQXLDevice
*qxl
, struct QXLCommandExt
*ext
)
439 switch (le32_to_cpu(ext
->cmd
.type
)) {
440 case QXL_CMD_SURFACE
:
442 QXLSurfaceCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
);
447 uint32_t id
= le32_to_cpu(cmd
->surface_id
);
449 if (id
>= qxl
->ssd
.num_surfaces
) {
450 qxl_set_guest_bug(qxl
, "QXL_CMD_SURFACE id %d >= %d", id
,
451 qxl
->ssd
.num_surfaces
);
454 if (cmd
->type
== QXL_SURFACE_CMD_CREATE
&&
455 (cmd
->u
.surface_create
.stride
& 0x03) != 0) {
456 qxl_set_guest_bug(qxl
, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
457 cmd
->u
.surface_create
.stride
);
460 qemu_mutex_lock(&qxl
->track_lock
);
461 if (cmd
->type
== QXL_SURFACE_CMD_CREATE
) {
462 qxl
->guest_surfaces
.cmds
[id
] = ext
->cmd
.data
;
463 qxl
->guest_surfaces
.count
++;
464 if (qxl
->guest_surfaces
.max
< qxl
->guest_surfaces
.count
)
465 qxl
->guest_surfaces
.max
= qxl
->guest_surfaces
.count
;
467 if (cmd
->type
== QXL_SURFACE_CMD_DESTROY
) {
468 qxl
->guest_surfaces
.cmds
[id
] = 0;
469 qxl
->guest_surfaces
.count
--;
471 qemu_mutex_unlock(&qxl
->track_lock
);
476 QXLCursorCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
);
481 if (cmd
->type
== QXL_CURSOR_SET
) {
482 qemu_mutex_lock(&qxl
->track_lock
);
483 qxl
->guest_cursor
= ext
->cmd
.data
;
484 qemu_mutex_unlock(&qxl
->track_lock
);
492 /* spice display interface callbacks */
494 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
496 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
498 trace_qxl_interface_attach_worker(qxl
->id
);
499 qxl
->ssd
.worker
= qxl_worker
;
502 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
504 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
506 trace_qxl_interface_set_compression_level(qxl
->id
, level
);
507 qxl
->shadow_rom
.compression_level
= cpu_to_le32(level
);
508 qxl
->rom
->compression_level
= cpu_to_le32(level
);
509 qxl_rom_set_dirty(qxl
);
512 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
514 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
516 trace_qxl_interface_set_mm_time(qxl
->id
, mm_time
);
517 qxl
->shadow_rom
.mm_clock
= cpu_to_le32(mm_time
);
518 qxl
->rom
->mm_clock
= cpu_to_le32(mm_time
);
519 qxl_rom_set_dirty(qxl
);
522 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
524 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
526 trace_qxl_interface_get_init_info(qxl
->id
);
527 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
528 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
529 info
->num_memslots
= NUM_MEMSLOTS
;
530 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
531 info
->internal_groupslot_id
= 0;
532 info
->qxl_ram_size
= le32_to_cpu(qxl
->shadow_rom
.num_pages
) << TARGET_PAGE_BITS
;
533 info
->n_surfaces
= qxl
->ssd
.num_surfaces
;
536 static const char *qxl_mode_to_string(int mode
)
539 case QXL_MODE_COMPAT
:
541 case QXL_MODE_NATIVE
:
543 case QXL_MODE_UNDEFINED
:
551 static const char *io_port_to_string(uint32_t io_port
)
553 if (io_port
>= QXL_IO_RANGE_SIZE
) {
554 return "out of range";
556 static const char *io_port_to_string
[QXL_IO_RANGE_SIZE
+ 1] = {
557 [QXL_IO_NOTIFY_CMD
] = "QXL_IO_NOTIFY_CMD",
558 [QXL_IO_NOTIFY_CURSOR
] = "QXL_IO_NOTIFY_CURSOR",
559 [QXL_IO_UPDATE_AREA
] = "QXL_IO_UPDATE_AREA",
560 [QXL_IO_UPDATE_IRQ
] = "QXL_IO_UPDATE_IRQ",
561 [QXL_IO_NOTIFY_OOM
] = "QXL_IO_NOTIFY_OOM",
562 [QXL_IO_RESET
] = "QXL_IO_RESET",
563 [QXL_IO_SET_MODE
] = "QXL_IO_SET_MODE",
564 [QXL_IO_LOG
] = "QXL_IO_LOG",
565 [QXL_IO_MEMSLOT_ADD
] = "QXL_IO_MEMSLOT_ADD",
566 [QXL_IO_MEMSLOT_DEL
] = "QXL_IO_MEMSLOT_DEL",
567 [QXL_IO_DETACH_PRIMARY
] = "QXL_IO_DETACH_PRIMARY",
568 [QXL_IO_ATTACH_PRIMARY
] = "QXL_IO_ATTACH_PRIMARY",
569 [QXL_IO_CREATE_PRIMARY
] = "QXL_IO_CREATE_PRIMARY",
570 [QXL_IO_DESTROY_PRIMARY
] = "QXL_IO_DESTROY_PRIMARY",
571 [QXL_IO_DESTROY_SURFACE_WAIT
] = "QXL_IO_DESTROY_SURFACE_WAIT",
572 [QXL_IO_DESTROY_ALL_SURFACES
] = "QXL_IO_DESTROY_ALL_SURFACES",
573 [QXL_IO_UPDATE_AREA_ASYNC
] = "QXL_IO_UPDATE_AREA_ASYNC",
574 [QXL_IO_MEMSLOT_ADD_ASYNC
] = "QXL_IO_MEMSLOT_ADD_ASYNC",
575 [QXL_IO_CREATE_PRIMARY_ASYNC
] = "QXL_IO_CREATE_PRIMARY_ASYNC",
576 [QXL_IO_DESTROY_PRIMARY_ASYNC
] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
577 [QXL_IO_DESTROY_SURFACE_ASYNC
] = "QXL_IO_DESTROY_SURFACE_ASYNC",
578 [QXL_IO_DESTROY_ALL_SURFACES_ASYNC
]
579 = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
580 [QXL_IO_FLUSH_SURFACES_ASYNC
] = "QXL_IO_FLUSH_SURFACES_ASYNC",
581 [QXL_IO_FLUSH_RELEASE
] = "QXL_IO_FLUSH_RELEASE",
582 [QXL_IO_MONITORS_CONFIG_ASYNC
] = "QXL_IO_MONITORS_CONFIG_ASYNC",
584 return io_port_to_string
[io_port
];
587 /* called from spice server thread context only */
588 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
590 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
591 SimpleSpiceUpdate
*update
;
592 QXLCommandRing
*ring
;
596 trace_qxl_ring_command_check(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
601 qemu_mutex_lock(&qxl
->ssd
.lock
);
602 update
= QTAILQ_FIRST(&qxl
->ssd
.updates
);
603 if (update
!= NULL
) {
604 QTAILQ_REMOVE(&qxl
->ssd
.updates
, update
, next
);
608 qemu_mutex_unlock(&qxl
->ssd
.lock
);
610 trace_qxl_ring_command_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
611 qxl_log_command(qxl
, "vga", ext
);
614 case QXL_MODE_COMPAT
:
615 case QXL_MODE_NATIVE
:
616 case QXL_MODE_UNDEFINED
:
617 ring
= &qxl
->ram
->cmd_ring
;
618 if (qxl
->guest_bug
|| SPICE_RING_IS_EMPTY(ring
)) {
621 SPICE_RING_CONS_ITEM(qxl
, ring
, cmd
);
626 ext
->group_id
= MEMSLOT_GROUP_GUEST
;
627 ext
->flags
= qxl
->cmdflags
;
628 SPICE_RING_POP(ring
, notify
);
629 qxl_ring_set_dirty(qxl
);
631 qxl_send_events(qxl
, QXL_INTERRUPT_DISPLAY
);
633 qxl
->guest_primary
.commands
++;
634 qxl_track_command(qxl
, ext
);
635 qxl_log_command(qxl
, "cmd", ext
);
636 trace_qxl_ring_command_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
643 /* called from spice server thread context only */
644 static int interface_req_cmd_notification(QXLInstance
*sin
)
646 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
649 trace_qxl_ring_command_req_notification(qxl
->id
);
651 case QXL_MODE_COMPAT
:
652 case QXL_MODE_NATIVE
:
653 case QXL_MODE_UNDEFINED
:
654 SPICE_RING_CONS_WAIT(&qxl
->ram
->cmd_ring
, wait
);
655 qxl_ring_set_dirty(qxl
);
664 /* called from spice server thread context only */
665 static inline void qxl_push_free_res(PCIQXLDevice
*d
, int flush
)
667 QXLReleaseRing
*ring
= &d
->ram
->release_ring
;
671 #define QXL_FREE_BUNCH_SIZE 32
673 if (ring
->prod
- ring
->cons
+ 1 == ring
->num_items
) {
674 /* ring full -- can't push */
677 if (!flush
&& d
->oom_running
) {
678 /* collect everything from oom handler before pushing */
681 if (!flush
&& d
->num_free_res
< QXL_FREE_BUNCH_SIZE
) {
682 /* collect a bit more before pushing */
686 SPICE_RING_PUSH(ring
, notify
);
687 trace_qxl_ring_res_push(d
->id
, qxl_mode_to_string(d
->mode
),
688 d
->guest_surfaces
.count
, d
->num_free_res
,
689 d
->last_release
, notify
? "yes" : "no");
690 trace_qxl_ring_res_push_rest(d
->id
, ring
->prod
- ring
->cons
,
691 ring
->num_items
, ring
->prod
, ring
->cons
);
693 qxl_send_events(d
, QXL_INTERRUPT_DISPLAY
);
695 SPICE_RING_PROD_ITEM(d
, ring
, item
);
701 d
->last_release
= NULL
;
702 qxl_ring_set_dirty(d
);
705 /* called from spice server thread context only */
706 static void interface_release_resource(QXLInstance
*sin
,
707 struct QXLReleaseInfoExt ext
)
709 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
710 QXLReleaseRing
*ring
;
713 if (ext
.group_id
== MEMSLOT_GROUP_HOST
) {
714 /* host group -> vga mode update request */
715 qemu_spice_destroy_update(&qxl
->ssd
, (void *)(intptr_t)ext
.info
->id
);
720 * ext->info points into guest-visible memory
721 * pci bar 0, $command.release_info
723 ring
= &qxl
->ram
->release_ring
;
724 SPICE_RING_PROD_ITEM(qxl
, ring
, item
);
729 /* stick head into the ring */
732 qxl_ram_set_dirty(qxl
, &ext
.info
->next
);
734 qxl_ring_set_dirty(qxl
);
736 /* append item to the list */
737 qxl
->last_release
->next
= ext
.info
->id
;
738 qxl_ram_set_dirty(qxl
, &qxl
->last_release
->next
);
740 qxl_ram_set_dirty(qxl
, &ext
.info
->next
);
742 qxl
->last_release
= ext
.info
;
744 trace_qxl_ring_res_put(qxl
->id
, qxl
->num_free_res
);
745 qxl_push_free_res(qxl
, 0);
748 /* called from spice server thread context only */
749 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
751 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
756 trace_qxl_ring_cursor_check(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
759 case QXL_MODE_COMPAT
:
760 case QXL_MODE_NATIVE
:
761 case QXL_MODE_UNDEFINED
:
762 ring
= &qxl
->ram
->cursor_ring
;
763 if (SPICE_RING_IS_EMPTY(ring
)) {
766 SPICE_RING_CONS_ITEM(qxl
, ring
, cmd
);
771 ext
->group_id
= MEMSLOT_GROUP_GUEST
;
772 ext
->flags
= qxl
->cmdflags
;
773 SPICE_RING_POP(ring
, notify
);
774 qxl_ring_set_dirty(qxl
);
776 qxl_send_events(qxl
, QXL_INTERRUPT_CURSOR
);
778 qxl
->guest_primary
.commands
++;
779 qxl_track_command(qxl
, ext
);
780 qxl_log_command(qxl
, "csr", ext
);
782 qxl_render_cursor(qxl
, ext
);
784 trace_qxl_ring_cursor_get(qxl
->id
, qxl_mode_to_string(qxl
->mode
));
791 /* called from spice server thread context only */
792 static int interface_req_cursor_notification(QXLInstance
*sin
)
794 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
797 trace_qxl_ring_cursor_req_notification(qxl
->id
);
799 case QXL_MODE_COMPAT
:
800 case QXL_MODE_NATIVE
:
801 case QXL_MODE_UNDEFINED
:
802 SPICE_RING_CONS_WAIT(&qxl
->ram
->cursor_ring
, wait
);
803 qxl_ring_set_dirty(qxl
);
812 /* called from spice server thread context */
813 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
816 * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
817 * use by xf86-video-qxl and is defined out in the qxl windows driver.
818 * Probably was at some earlier version that is prior to git start (2009),
819 * and is still guest trigerrable.
821 fprintf(stderr
, "%s: deprecated\n", __func__
);
824 /* called from spice server thread context only */
825 static int interface_flush_resources(QXLInstance
*sin
)
827 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
830 ret
= qxl
->num_free_res
;
832 qxl_push_free_res(qxl
, 1);
837 static void qxl_create_guest_primary_complete(PCIQXLDevice
*d
);
839 /* called from spice server thread context only */
840 static void interface_async_complete_io(PCIQXLDevice
*qxl
, QXLCookie
*cookie
)
842 uint32_t current_async
;
844 qemu_mutex_lock(&qxl
->async_lock
);
845 current_async
= qxl
->current_async
;
846 qxl
->current_async
= QXL_UNDEFINED_IO
;
847 qemu_mutex_unlock(&qxl
->async_lock
);
849 trace_qxl_interface_async_complete_io(qxl
->id
, current_async
, cookie
);
851 fprintf(stderr
, "qxl: %s: error, cookie is NULL\n", __func__
);
854 if (cookie
&& current_async
!= cookie
->io
) {
856 "qxl: %s: error: current_async = %d != %"
857 PRId64
" = cookie->io\n", __func__
, current_async
, cookie
->io
);
859 switch (current_async
) {
860 case QXL_IO_MEMSLOT_ADD_ASYNC
:
861 case QXL_IO_DESTROY_PRIMARY_ASYNC
:
862 case QXL_IO_UPDATE_AREA_ASYNC
:
863 case QXL_IO_FLUSH_SURFACES_ASYNC
:
864 case QXL_IO_MONITORS_CONFIG_ASYNC
:
866 case QXL_IO_CREATE_PRIMARY_ASYNC
:
867 qxl_create_guest_primary_complete(qxl
);
869 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC
:
870 qxl_spice_destroy_surfaces_complete(qxl
);
872 case QXL_IO_DESTROY_SURFACE_ASYNC
:
873 qxl_spice_destroy_surface_wait_complete(qxl
, cookie
->u
.surface_id
);
876 fprintf(stderr
, "qxl: %s: unexpected current_async %d\n", __func__
,
879 qxl_send_events(qxl
, QXL_INTERRUPT_IO_CMD
);
882 /* called from spice server thread context only */
883 static void interface_update_area_complete(QXLInstance
*sin
,
885 QXLRect
*dirty
, uint32_t num_updated_rects
)
887 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
891 qemu_mutex_lock(&qxl
->ssd
.lock
);
892 if (surface_id
!= 0 || !qxl
->render_update_cookie_num
) {
893 qemu_mutex_unlock(&qxl
->ssd
.lock
);
896 trace_qxl_interface_update_area_complete(qxl
->id
, surface_id
, dirty
->left
,
897 dirty
->right
, dirty
->top
, dirty
->bottom
);
898 trace_qxl_interface_update_area_complete_rest(qxl
->id
, num_updated_rects
);
899 if (qxl
->num_dirty_rects
+ num_updated_rects
> QXL_NUM_DIRTY_RECTS
) {
901 * overflow - treat this as a full update. Not expected to be common.
903 trace_qxl_interface_update_area_complete_overflow(qxl
->id
,
904 QXL_NUM_DIRTY_RECTS
);
905 qxl
->guest_primary
.resized
= 1;
907 if (qxl
->guest_primary
.resized
) {
909 * Don't bother copying or scheduling the bh since we will flip
910 * the whole area anyway on completion of the update_area async call
912 qemu_mutex_unlock(&qxl
->ssd
.lock
);
915 qxl_i
= qxl
->num_dirty_rects
;
916 for (i
= 0; i
< num_updated_rects
; i
++) {
917 qxl
->dirty
[qxl_i
++] = dirty
[i
];
919 qxl
->num_dirty_rects
+= num_updated_rects
;
920 trace_qxl_interface_update_area_complete_schedule_bh(qxl
->id
,
921 qxl
->num_dirty_rects
);
922 qemu_bh_schedule(qxl
->update_area_bh
);
923 qemu_mutex_unlock(&qxl
->ssd
.lock
);
926 /* called from spice server thread context only */
927 static void interface_async_complete(QXLInstance
*sin
, uint64_t cookie_token
)
929 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
930 QXLCookie
*cookie
= (QXLCookie
*)(uintptr_t)cookie_token
;
932 switch (cookie
->type
) {
933 case QXL_COOKIE_TYPE_IO
:
934 interface_async_complete_io(qxl
, cookie
);
937 case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA
:
938 qxl_render_update_area_done(qxl
, cookie
);
940 case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG
:
943 fprintf(stderr
, "qxl: %s: unexpected cookie type %d\n",
944 __func__
, cookie
->type
);
949 /* called from spice server thread context only */
950 static void interface_set_client_capabilities(QXLInstance
*sin
,
951 uint8_t client_present
,
954 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
956 if (runstate_check(RUN_STATE_INMIGRATE
) ||
957 runstate_check(RUN_STATE_POSTMIGRATE
)) {
961 qxl
->shadow_rom
.client_present
= client_present
;
962 memcpy(qxl
->shadow_rom
.client_capabilities
, caps
, sizeof(caps
));
963 qxl
->rom
->client_present
= client_present
;
964 memcpy(qxl
->rom
->client_capabilities
, caps
, sizeof(caps
));
965 qxl_rom_set_dirty(qxl
);
967 qxl_send_events(qxl
, QXL_INTERRUPT_CLIENT
);
970 static uint32_t qxl_crc32(const uint8_t *p
, unsigned len
)
973 * zlib xors the seed with 0xffffffff, and xors the result
974 * again with 0xffffffff; Both are not done with linux's crc32,
975 * which we want to be compatible with, so undo that.
977 return crc32(0xffffffff, p
, len
) ^ 0xffffffff;
980 /* called from main context only */
981 static int interface_client_monitors_config(QXLInstance
*sin
,
982 VDAgentMonitorsConfig
*monitors_config
)
984 PCIQXLDevice
*qxl
= container_of(sin
, PCIQXLDevice
, ssd
.qxl
);
985 QXLRom
*rom
= memory_region_get_ram_ptr(&qxl
->rom_bar
);
989 * Older windows drivers set int_mask to 0 when their ISR is called,
990 * then later set it to ~0. So it doesn't relate to the actual interrupts
991 * handled. However, they are old, so clearly they don't support this
994 if (qxl
->ram
->int_mask
== 0 || qxl
->ram
->int_mask
== ~0 ||
995 !(qxl
->ram
->int_mask
& QXL_INTERRUPT_CLIENT_MONITORS_CONFIG
)) {
996 trace_qxl_client_monitors_config_unsupported_by_guest(qxl
->id
,
1001 if (!monitors_config
) {
1004 memset(&rom
->client_monitors_config
, 0,
1005 sizeof(rom
->client_monitors_config
));
1006 rom
->client_monitors_config
.count
= monitors_config
->num_of_monitors
;
1007 /* monitors_config->flags ignored */
1008 if (rom
->client_monitors_config
.count
>=
1009 ARRAY_SIZE(rom
->client_monitors_config
.heads
)) {
1010 trace_qxl_client_monitors_config_capped(qxl
->id
,
1011 monitors_config
->num_of_monitors
,
1012 ARRAY_SIZE(rom
->client_monitors_config
.heads
));
1013 rom
->client_monitors_config
.count
=
1014 ARRAY_SIZE(rom
->client_monitors_config
.heads
);
1016 for (i
= 0 ; i
< rom
->client_monitors_config
.count
; ++i
) {
1017 VDAgentMonConfig
*monitor
= &monitors_config
->monitors
[i
];
1018 QXLURect
*rect
= &rom
->client_monitors_config
.heads
[i
];
1019 /* monitor->depth ignored */
1020 rect
->left
= monitor
->x
;
1021 rect
->top
= monitor
->y
;
1022 rect
->right
= monitor
->x
+ monitor
->width
;
1023 rect
->bottom
= monitor
->y
+ monitor
->height
;
1025 rom
->client_monitors_config_crc
= qxl_crc32(
1026 (const uint8_t *)&rom
->client_monitors_config
,
1027 sizeof(rom
->client_monitors_config
));
1028 trace_qxl_client_monitors_config_crc(qxl
->id
,
1029 sizeof(rom
->client_monitors_config
),
1030 rom
->client_monitors_config_crc
);
1032 trace_qxl_interrupt_client_monitors_config(qxl
->id
,
1033 rom
->client_monitors_config
.count
,
1034 rom
->client_monitors_config
.heads
);
1035 qxl_send_events(qxl
, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG
);
1039 static const QXLInterface qxl_interface
= {
1040 .base
.type
= SPICE_INTERFACE_QXL
,
1041 .base
.description
= "qxl gpu",
1042 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
1043 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
1045 .attache_worker
= interface_attach_worker
,
1046 .set_compression_level
= interface_set_compression_level
,
1047 .set_mm_time
= interface_set_mm_time
,
1048 .get_init_info
= interface_get_init_info
,
1050 /* the callbacks below are called from spice server thread context */
1051 .get_command
= interface_get_command
,
1052 .req_cmd_notification
= interface_req_cmd_notification
,
1053 .release_resource
= interface_release_resource
,
1054 .get_cursor_command
= interface_get_cursor_command
,
1055 .req_cursor_notification
= interface_req_cursor_notification
,
1056 .notify_update
= interface_notify_update
,
1057 .flush_resources
= interface_flush_resources
,
1058 .async_complete
= interface_async_complete
,
1059 .update_area_complete
= interface_update_area_complete
,
1060 .set_client_capabilities
= interface_set_client_capabilities
,
1061 .client_monitors_config
= interface_client_monitors_config
,
1064 static void qxl_enter_vga_mode(PCIQXLDevice
*d
)
1066 if (d
->mode
== QXL_MODE_VGA
) {
1069 trace_qxl_enter_vga_mode(d
->id
);
1070 qemu_spice_create_host_primary(&d
->ssd
);
1071 d
->mode
= QXL_MODE_VGA
;
1072 dpy_gfx_resize(d
->ssd
.ds
);
1073 vga_dirty_log_start(&d
->vga
);
1076 static void qxl_exit_vga_mode(PCIQXLDevice
*d
)
1078 if (d
->mode
!= QXL_MODE_VGA
) {
1081 trace_qxl_exit_vga_mode(d
->id
);
1082 vga_dirty_log_stop(&d
->vga
);
1083 qxl_destroy_primary(d
, QXL_SYNC
);
1086 static void qxl_update_irq(PCIQXLDevice
*d
)
1088 uint32_t pending
= le32_to_cpu(d
->ram
->int_pending
);
1089 uint32_t mask
= le32_to_cpu(d
->ram
->int_mask
);
1090 int level
= !!(pending
& mask
);
1091 qemu_set_irq(d
->pci
.irq
[0], level
);
1092 qxl_ring_set_dirty(d
);
1095 static void qxl_check_state(PCIQXLDevice
*d
)
1097 QXLRam
*ram
= d
->ram
;
1098 int spice_display_running
= qemu_spice_display_is_running(&d
->ssd
);
1100 assert(!spice_display_running
|| SPICE_RING_IS_EMPTY(&ram
->cmd_ring
));
1101 assert(!spice_display_running
|| SPICE_RING_IS_EMPTY(&ram
->cursor_ring
));
1104 static void qxl_reset_state(PCIQXLDevice
*d
)
1106 QXLRom
*rom
= d
->rom
;
1109 d
->shadow_rom
.update_id
= cpu_to_le32(0);
1110 *rom
= d
->shadow_rom
;
1111 qxl_rom_set_dirty(d
);
1113 d
->num_free_res
= 0;
1114 d
->last_release
= NULL
;
1115 memset(&d
->ssd
.dirty
, 0, sizeof(d
->ssd
.dirty
));
1118 static void qxl_soft_reset(PCIQXLDevice
*d
)
1120 trace_qxl_soft_reset(d
->id
);
1122 qxl_clear_guest_bug(d
);
1123 d
->current_async
= QXL_UNDEFINED_IO
;
1126 qxl_enter_vga_mode(d
);
1128 d
->mode
= QXL_MODE_UNDEFINED
;
1132 static void qxl_hard_reset(PCIQXLDevice
*d
, int loadvm
)
1134 trace_qxl_hard_reset(d
->id
, loadvm
);
1136 qxl_spice_reset_cursor(d
);
1137 qxl_spice_reset_image_cache(d
);
1138 qxl_reset_surfaces(d
);
1139 qxl_reset_memslots(d
);
1141 /* pre loadvm reset must not touch QXLRam. This lives in
1142 * device memory, is migrated together with RAM and thus
1143 * already loaded at this point */
1147 qemu_spice_create_host_memslot(&d
->ssd
);
1151 static void qxl_reset_handler(DeviceState
*dev
)
1153 PCIQXLDevice
*d
= DO_UPCAST(PCIQXLDevice
, pci
.qdev
, dev
);
1155 qxl_hard_reset(d
, 0);
1158 static void qxl_vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
1160 VGACommonState
*vga
= opaque
;
1161 PCIQXLDevice
*qxl
= container_of(vga
, PCIQXLDevice
, vga
);
1163 trace_qxl_io_write_vga(qxl
->id
, qxl_mode_to_string(qxl
->mode
), addr
, val
);
1164 if (qxl
->mode
!= QXL_MODE_VGA
) {
1165 qxl_destroy_primary(qxl
, QXL_SYNC
);
1166 qxl_soft_reset(qxl
);
1168 vga_ioport_write(opaque
, addr
, val
);
1171 static const MemoryRegionPortio qxl_vga_portio_list
[] = {
1172 { 0x04, 2, 1, .read
= vga_ioport_read
,
1173 .write
= qxl_vga_ioport_write
}, /* 3b4 */
1174 { 0x0a, 1, 1, .read
= vga_ioport_read
,
1175 .write
= qxl_vga_ioport_write
}, /* 3ba */
1176 { 0x10, 16, 1, .read
= vga_ioport_read
,
1177 .write
= qxl_vga_ioport_write
}, /* 3c0 */
1178 { 0x24, 2, 1, .read
= vga_ioport_read
,
1179 .write
= qxl_vga_ioport_write
}, /* 3d4 */
1180 { 0x2a, 1, 1, .read
= vga_ioport_read
,
1181 .write
= qxl_vga_ioport_write
}, /* 3da */
1182 PORTIO_END_OF_LIST(),
1185 static int qxl_add_memslot(PCIQXLDevice
*d
, uint32_t slot_id
, uint64_t delta
,
1188 static const int regions
[] = {
1189 QXL_RAM_RANGE_INDEX
,
1190 QXL_VRAM_RANGE_INDEX
,
1191 QXL_VRAM64_RANGE_INDEX
,
1193 uint64_t guest_start
;
1198 intptr_t virt_start
;
1199 QXLDevMemSlot memslot
;
1202 guest_start
= le64_to_cpu(d
->guest_slots
[slot_id
].slot
.mem_start
);
1203 guest_end
= le64_to_cpu(d
->guest_slots
[slot_id
].slot
.mem_end
);
1205 trace_qxl_memslot_add_guest(d
->id
, slot_id
, guest_start
, guest_end
);
1207 if (slot_id
>= NUM_MEMSLOTS
) {
1208 qxl_set_guest_bug(d
, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__
,
1209 slot_id
, NUM_MEMSLOTS
);
1212 if (guest_start
> guest_end
) {
1213 qxl_set_guest_bug(d
, "%s: guest_start > guest_end 0x%" PRIx64
1214 " > 0x%" PRIx64
, __func__
, guest_start
, guest_end
);
1218 for (i
= 0; i
< ARRAY_SIZE(regions
); i
++) {
1219 pci_region
= regions
[i
];
1220 pci_start
= d
->pci
.io_regions
[pci_region
].addr
;
1221 pci_end
= pci_start
+ d
->pci
.io_regions
[pci_region
].size
;
1223 if (pci_start
== -1) {
1226 /* start address in range ? */
1227 if (guest_start
< pci_start
|| guest_start
> pci_end
) {
1230 /* end address in range ? */
1231 if (guest_end
> pci_end
) {
1237 if (i
== ARRAY_SIZE(regions
)) {
1238 qxl_set_guest_bug(d
, "%s: finished loop without match", __func__
);
1242 switch (pci_region
) {
1243 case QXL_RAM_RANGE_INDEX
:
1244 virt_start
= (intptr_t)memory_region_get_ram_ptr(&d
->vga
.vram
);
1246 case QXL_VRAM_RANGE_INDEX
:
1247 case 4 /* vram 64bit */:
1248 virt_start
= (intptr_t)memory_region_get_ram_ptr(&d
->vram_bar
);
1251 /* should not happen */
1252 qxl_set_guest_bug(d
, "%s: pci_region = %d", __func__
, pci_region
);
1256 memslot
.slot_id
= slot_id
;
1257 memslot
.slot_group_id
= MEMSLOT_GROUP_GUEST
; /* guest group */
1258 memslot
.virt_start
= virt_start
+ (guest_start
- pci_start
);
1259 memslot
.virt_end
= virt_start
+ (guest_end
- pci_start
);
1260 memslot
.addr_delta
= memslot
.virt_start
- delta
;
1261 memslot
.generation
= d
->rom
->slot_generation
= 0;
1262 qxl_rom_set_dirty(d
);
1264 qemu_spice_add_memslot(&d
->ssd
, &memslot
, async
);
1265 d
->guest_slots
[slot_id
].ptr
= (void*)memslot
.virt_start
;
1266 d
->guest_slots
[slot_id
].size
= memslot
.virt_end
- memslot
.virt_start
;
1267 d
->guest_slots
[slot_id
].delta
= delta
;
1268 d
->guest_slots
[slot_id
].active
= 1;
1272 static void qxl_del_memslot(PCIQXLDevice
*d
, uint32_t slot_id
)
1274 qemu_spice_del_memslot(&d
->ssd
, MEMSLOT_GROUP_HOST
, slot_id
);
1275 d
->guest_slots
[slot_id
].active
= 0;
1278 static void qxl_reset_memslots(PCIQXLDevice
*d
)
1280 qxl_spice_reset_memslots(d
);
1281 memset(&d
->guest_slots
, 0, sizeof(d
->guest_slots
));
1284 static void qxl_reset_surfaces(PCIQXLDevice
*d
)
1286 trace_qxl_reset_surfaces(d
->id
);
1287 d
->mode
= QXL_MODE_UNDEFINED
;
1288 qxl_spice_destroy_surfaces(d
, QXL_SYNC
);
1291 /* can be also called from spice server thread context */
1292 void *qxl_phys2virt(PCIQXLDevice
*qxl
, QXLPHYSICAL pqxl
, int group_id
)
1294 uint64_t phys
= le64_to_cpu(pqxl
);
1295 uint32_t slot
= (phys
>> (64 - 8)) & 0xff;
1296 uint64_t offset
= phys
& 0xffffffffffff;
1299 case MEMSLOT_GROUP_HOST
:
1300 return (void *)(intptr_t)offset
;
1301 case MEMSLOT_GROUP_GUEST
:
1302 if (slot
>= NUM_MEMSLOTS
) {
1303 qxl_set_guest_bug(qxl
, "slot too large %d >= %d", slot
,
1307 if (!qxl
->guest_slots
[slot
].active
) {
1308 qxl_set_guest_bug(qxl
, "inactive slot %d\n", slot
);
1311 if (offset
< qxl
->guest_slots
[slot
].delta
) {
1312 qxl_set_guest_bug(qxl
,
1313 "slot %d offset %"PRIu64
" < delta %"PRIu64
"\n",
1314 slot
, offset
, qxl
->guest_slots
[slot
].delta
);
1317 offset
-= qxl
->guest_slots
[slot
].delta
;
1318 if (offset
> qxl
->guest_slots
[slot
].size
) {
1319 qxl_set_guest_bug(qxl
,
1320 "slot %d offset %"PRIu64
" > size %"PRIu64
"\n",
1321 slot
, offset
, qxl
->guest_slots
[slot
].size
);
1324 return qxl
->guest_slots
[slot
].ptr
+ offset
;
1329 static void qxl_create_guest_primary_complete(PCIQXLDevice
*qxl
)
1331 /* for local rendering */
1332 qxl_render_resize(qxl
);
1335 static void qxl_create_guest_primary(PCIQXLDevice
*qxl
, int loadvm
,
1338 QXLDevSurfaceCreate surface
;
1339 QXLSurfaceCreate
*sc
= &qxl
->guest_primary
.surface
;
1341 int requested_height
= le32_to_cpu(sc
->height
);
1342 int requested_stride
= le32_to_cpu(sc
->stride
);
1344 size
= abs(requested_stride
) * requested_height
;
1345 if (size
> qxl
->vgamem_size
) {
1346 qxl_set_guest_bug(qxl
, "%s: requested primary larger then framebuffer"
1351 if (qxl
->mode
== QXL_MODE_NATIVE
) {
1352 qxl_set_guest_bug(qxl
, "%s: nop since already in QXL_MODE_NATIVE",
1355 qxl_exit_vga_mode(qxl
);
1357 surface
.format
= le32_to_cpu(sc
->format
);
1358 surface
.height
= le32_to_cpu(sc
->height
);
1359 surface
.mem
= le64_to_cpu(sc
->mem
);
1360 surface
.position
= le32_to_cpu(sc
->position
);
1361 surface
.stride
= le32_to_cpu(sc
->stride
);
1362 surface
.width
= le32_to_cpu(sc
->width
);
1363 surface
.type
= le32_to_cpu(sc
->type
);
1364 surface
.flags
= le32_to_cpu(sc
->flags
);
1365 trace_qxl_create_guest_primary(qxl
->id
, sc
->width
, sc
->height
, sc
->mem
,
1366 sc
->format
, sc
->position
);
1367 trace_qxl_create_guest_primary_rest(qxl
->id
, sc
->stride
, sc
->type
,
1370 if ((surface
.stride
& 0x3) != 0) {
1371 qxl_set_guest_bug(qxl
, "primary surface stride = %d %% 4 != 0",
1376 surface
.mouse_mode
= true;
1377 surface
.group_id
= MEMSLOT_GROUP_GUEST
;
1379 surface
.flags
|= QXL_SURF_FLAG_KEEP_DATA
;
1382 qxl
->mode
= QXL_MODE_NATIVE
;
1384 qemu_spice_create_primary_surface(&qxl
->ssd
, 0, &surface
, async
);
1386 if (async
== QXL_SYNC
) {
1387 qxl_create_guest_primary_complete(qxl
);
1391 /* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
1392 * done (in QXL_SYNC case), 0 otherwise. */
1393 static int qxl_destroy_primary(PCIQXLDevice
*d
, qxl_async_io async
)
1395 if (d
->mode
== QXL_MODE_UNDEFINED
) {
1398 trace_qxl_destroy_primary(d
->id
);
1399 d
->mode
= QXL_MODE_UNDEFINED
;
1400 qemu_spice_destroy_primary_surface(&d
->ssd
, 0, async
);
1401 qxl_spice_reset_cursor(d
);
1405 static void qxl_set_mode(PCIQXLDevice
*d
, int modenr
, int loadvm
)
1407 pcibus_t start
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].addr
;
1408 pcibus_t end
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].size
+ start
;
1409 QXLMode
*mode
= d
->modes
->modes
+ modenr
;
1410 uint64_t devmem
= d
->pci
.io_regions
[QXL_RAM_RANGE_INDEX
].addr
;
1415 QXLSurfaceCreate surface
= {
1416 .width
= mode
->x_res
,
1417 .height
= mode
->y_res
,
1418 .stride
= -mode
->x_res
* 4,
1419 .format
= SPICE_SURFACE_FMT_32_xRGB
,
1420 .flags
= loadvm
? QXL_SURF_FLAG_KEEP_DATA
: 0,
1422 .mem
= devmem
+ d
->shadow_rom
.draw_area_offset
,
1425 trace_qxl_set_mode(d
->id
, modenr
, mode
->x_res
, mode
->y_res
, mode
->bits
,
1428 qxl_hard_reset(d
, 0);
1431 d
->guest_slots
[0].slot
= slot
;
1432 assert(qxl_add_memslot(d
, 0, devmem
, QXL_SYNC
) == 0);
1434 d
->guest_primary
.surface
= surface
;
1435 qxl_create_guest_primary(d
, 0, QXL_SYNC
);
1437 d
->mode
= QXL_MODE_COMPAT
;
1438 d
->cmdflags
= QXL_COMMAND_FLAG_COMPAT
;
1439 if (mode
->bits
== 16) {
1440 d
->cmdflags
|= QXL_COMMAND_FLAG_COMPAT_16BPP
;
1442 d
->shadow_rom
.mode
= cpu_to_le32(modenr
);
1443 d
->rom
->mode
= cpu_to_le32(modenr
);
1444 qxl_rom_set_dirty(d
);
1447 static void ioport_write(void *opaque
, hwaddr addr
,
1448 uint64_t val
, unsigned size
)
1450 PCIQXLDevice
*d
= opaque
;
1451 uint32_t io_port
= addr
;
1452 qxl_async_io async
= QXL_SYNC
;
1453 uint32_t orig_io_port
= io_port
;
1455 if (d
->guest_bug
&& io_port
!= QXL_IO_RESET
) {
1459 if (d
->revision
<= QXL_REVISION_STABLE_V10
&&
1460 io_port
> QXL_IO_FLUSH_RELEASE
) {
1461 qxl_set_guest_bug(d
, "unsupported io %d for revision %d\n",
1462 io_port
, d
->revision
);
1468 case QXL_IO_SET_MODE
:
1469 case QXL_IO_MEMSLOT_ADD
:
1470 case QXL_IO_MEMSLOT_DEL
:
1471 case QXL_IO_CREATE_PRIMARY
:
1472 case QXL_IO_UPDATE_IRQ
:
1474 case QXL_IO_MEMSLOT_ADD_ASYNC
:
1475 case QXL_IO_CREATE_PRIMARY_ASYNC
:
1478 if (d
->mode
!= QXL_MODE_VGA
) {
1481 trace_qxl_io_unexpected_vga_mode(d
->id
,
1482 addr
, val
, io_port_to_string(io_port
));
1483 /* be nice to buggy guest drivers */
1484 if (io_port
>= QXL_IO_UPDATE_AREA_ASYNC
&&
1485 io_port
< QXL_IO_RANGE_SIZE
) {
1486 qxl_send_events(d
, QXL_INTERRUPT_IO_CMD
);
1491 /* we change the io_port to avoid ifdeffery in the main switch */
1492 orig_io_port
= io_port
;
1494 case QXL_IO_UPDATE_AREA_ASYNC
:
1495 io_port
= QXL_IO_UPDATE_AREA
;
1497 case QXL_IO_MEMSLOT_ADD_ASYNC
:
1498 io_port
= QXL_IO_MEMSLOT_ADD
;
1500 case QXL_IO_CREATE_PRIMARY_ASYNC
:
1501 io_port
= QXL_IO_CREATE_PRIMARY
;
1503 case QXL_IO_DESTROY_PRIMARY_ASYNC
:
1504 io_port
= QXL_IO_DESTROY_PRIMARY
;
1506 case QXL_IO_DESTROY_SURFACE_ASYNC
:
1507 io_port
= QXL_IO_DESTROY_SURFACE_WAIT
;
1509 case QXL_IO_DESTROY_ALL_SURFACES_ASYNC
:
1510 io_port
= QXL_IO_DESTROY_ALL_SURFACES
;
1512 case QXL_IO_FLUSH_SURFACES_ASYNC
:
1513 case QXL_IO_MONITORS_CONFIG_ASYNC
:
1516 qemu_mutex_lock(&d
->async_lock
);
1517 if (d
->current_async
!= QXL_UNDEFINED_IO
) {
1518 qxl_set_guest_bug(d
, "%d async started before last (%d) complete",
1519 io_port
, d
->current_async
);
1520 qemu_mutex_unlock(&d
->async_lock
);
1523 d
->current_async
= orig_io_port
;
1524 qemu_mutex_unlock(&d
->async_lock
);
1529 trace_qxl_io_write(d
->id
, qxl_mode_to_string(d
->mode
), addr
, val
, size
,
1533 case QXL_IO_UPDATE_AREA
:
1535 QXLCookie
*cookie
= NULL
;
1536 QXLRect update
= d
->ram
->update_area
;
1538 if (d
->ram
->update_surface
> d
->ssd
.num_surfaces
) {
1539 qxl_set_guest_bug(d
, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
1540 d
->ram
->update_surface
);
1543 if (update
.left
>= update
.right
|| update
.top
>= update
.bottom
||
1544 update
.left
< 0 || update
.top
< 0) {
1545 qxl_set_guest_bug(d
,
1546 "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
1547 update
.left
, update
.top
, update
.right
, update
.bottom
);
1550 if (async
== QXL_ASYNC
) {
1551 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
1552 QXL_IO_UPDATE_AREA_ASYNC
);
1553 cookie
->u
.area
= update
;
1555 qxl_spice_update_area(d
, d
->ram
->update_surface
,
1556 cookie
? &cookie
->u
.area
: &update
,
1557 NULL
, 0, 0, async
, cookie
);
1560 case QXL_IO_NOTIFY_CMD
:
1561 qemu_spice_wakeup(&d
->ssd
);
1563 case QXL_IO_NOTIFY_CURSOR
:
1564 qemu_spice_wakeup(&d
->ssd
);
1566 case QXL_IO_UPDATE_IRQ
:
1569 case QXL_IO_NOTIFY_OOM
:
1570 if (!SPICE_RING_IS_EMPTY(&d
->ram
->release_ring
)) {
1577 case QXL_IO_SET_MODE
:
1578 qxl_set_mode(d
, val
, 0);
1581 trace_qxl_io_log(d
->id
, d
->ram
->log_buf
);
1582 if (d
->guestdebug
) {
1583 fprintf(stderr
, "qxl/guest-%d: %" PRId64
": %s", d
->id
,
1584 qemu_get_clock_ns(vm_clock
), d
->ram
->log_buf
);
1588 qxl_hard_reset(d
, 0);
1590 case QXL_IO_MEMSLOT_ADD
:
1591 if (val
>= NUM_MEMSLOTS
) {
1592 qxl_set_guest_bug(d
, "QXL_IO_MEMSLOT_ADD: val out of range");
1595 if (d
->guest_slots
[val
].active
) {
1596 qxl_set_guest_bug(d
,
1597 "QXL_IO_MEMSLOT_ADD: memory slot already active");
1600 d
->guest_slots
[val
].slot
= d
->ram
->mem_slot
;
1601 qxl_add_memslot(d
, val
, 0, async
);
1603 case QXL_IO_MEMSLOT_DEL
:
1604 if (val
>= NUM_MEMSLOTS
) {
1605 qxl_set_guest_bug(d
, "QXL_IO_MEMSLOT_DEL: val out of range");
1608 qxl_del_memslot(d
, val
);
1610 case QXL_IO_CREATE_PRIMARY
:
1612 qxl_set_guest_bug(d
, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
1616 d
->guest_primary
.surface
= d
->ram
->create_surface
;
1617 qxl_create_guest_primary(d
, 0, async
);
1619 case QXL_IO_DESTROY_PRIMARY
:
1621 qxl_set_guest_bug(d
, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
1625 if (!qxl_destroy_primary(d
, async
)) {
1626 trace_qxl_io_destroy_primary_ignored(d
->id
,
1627 qxl_mode_to_string(d
->mode
));
1631 case QXL_IO_DESTROY_SURFACE_WAIT
:
1632 if (val
>= d
->ssd
.num_surfaces
) {
1633 qxl_set_guest_bug(d
, "QXL_IO_DESTROY_SURFACE (async=%d):"
1634 "%" PRIu64
" >= NUM_SURFACES", async
, val
);
1637 qxl_spice_destroy_surface_wait(d
, val
, async
);
1639 case QXL_IO_FLUSH_RELEASE
: {
1640 QXLReleaseRing
*ring
= &d
->ram
->release_ring
;
1641 if (ring
->prod
- ring
->cons
+ 1 == ring
->num_items
) {
1643 "ERROR: no flush, full release ring [p%d,%dc]\n",
1644 ring
->prod
, ring
->cons
);
1646 qxl_push_free_res(d
, 1 /* flush */);
1649 case QXL_IO_FLUSH_SURFACES_ASYNC
:
1650 qxl_spice_flush_surfaces_async(d
);
1652 case QXL_IO_DESTROY_ALL_SURFACES
:
1653 d
->mode
= QXL_MODE_UNDEFINED
;
1654 qxl_spice_destroy_surfaces(d
, async
);
1656 case QXL_IO_MONITORS_CONFIG_ASYNC
:
1657 qxl_spice_monitors_config_async(d
, 0);
1660 qxl_set_guest_bug(d
, "%s: unexpected ioport=0x%x\n", __func__
, io_port
);
1665 qxl_send_events(d
, QXL_INTERRUPT_IO_CMD
);
1666 qemu_mutex_lock(&d
->async_lock
);
1667 d
->current_async
= QXL_UNDEFINED_IO
;
1668 qemu_mutex_unlock(&d
->async_lock
);
1672 static uint64_t ioport_read(void *opaque
, hwaddr addr
,
1675 PCIQXLDevice
*qxl
= opaque
;
1677 trace_qxl_io_read_unexpected(qxl
->id
);
1681 static const MemoryRegionOps qxl_io_ops
= {
1682 .read
= ioport_read
,
1683 .write
= ioport_write
,
1685 .min_access_size
= 1,
1686 .max_access_size
= 1,
1690 static void pipe_read(void *opaque
)
1692 PCIQXLDevice
*d
= opaque
;
1697 len
= read(d
->pipe
[0], &dummy
, sizeof(dummy
));
1698 } while (len
== sizeof(dummy
));
1702 static void qxl_send_events(PCIQXLDevice
*d
, uint32_t events
)
1704 uint32_t old_pending
;
1705 uint32_t le_events
= cpu_to_le32(events
);
1707 trace_qxl_send_events(d
->id
, events
);
1708 if (!qemu_spice_display_is_running(&d
->ssd
)) {
1709 /* spice-server tracks guest running state and should not do this */
1710 fprintf(stderr
, "%s: spice-server bug: guest stopped, ignoring\n",
1712 trace_qxl_send_events_vm_stopped(d
->id
, events
);
1715 old_pending
= __sync_fetch_and_or(&d
->ram
->int_pending
, le_events
);
1716 if ((old_pending
& le_events
) == le_events
) {
1719 if (qemu_thread_is_self(&d
->main
)) {
1722 if (write(d
->pipe
[1], d
, 1) != 1) {
1723 dprint(d
, 1, "%s: write to pipe failed\n", __func__
);
1728 static void init_pipe_signaling(PCIQXLDevice
*d
)
1730 if (pipe(d
->pipe
) < 0) {
1731 fprintf(stderr
, "%s:%s: qxl pipe creation failed\n",
1732 __FILE__
, __func__
);
1735 fcntl(d
->pipe
[0], F_SETFL
, O_NONBLOCK
);
1736 fcntl(d
->pipe
[1], F_SETFL
, O_NONBLOCK
);
1737 fcntl(d
->pipe
[0], F_SETOWN
, getpid());
1739 qemu_thread_get_self(&d
->main
);
1740 qemu_set_fd_handler(d
->pipe
[0], pipe_read
, NULL
, d
);
1743 /* graphics console */
1745 static void qxl_hw_update(void *opaque
)
1747 PCIQXLDevice
*qxl
= opaque
;
1748 VGACommonState
*vga
= &qxl
->vga
;
1750 switch (qxl
->mode
) {
1754 case QXL_MODE_COMPAT
:
1755 case QXL_MODE_NATIVE
:
1756 qxl_render_update(qxl
);
1763 static void qxl_hw_invalidate(void *opaque
)
1765 PCIQXLDevice
*qxl
= opaque
;
1766 VGACommonState
*vga
= &qxl
->vga
;
1768 vga
->invalidate(vga
);
1771 static void qxl_hw_screen_dump(void *opaque
, const char *filename
, bool cswitch
,
1774 PCIQXLDevice
*qxl
= opaque
;
1775 VGACommonState
*vga
= &qxl
->vga
;
1777 switch (qxl
->mode
) {
1778 case QXL_MODE_COMPAT
:
1779 case QXL_MODE_NATIVE
:
1780 qxl_render_update(qxl
);
1781 ppm_save(filename
, qxl
->ssd
.ds
->surface
, errp
);
1784 vga
->screen_dump(vga
, filename
, cswitch
, errp
);
1791 static void qxl_hw_text_update(void *opaque
, console_ch_t
*chardata
)
1793 PCIQXLDevice
*qxl
= opaque
;
1794 VGACommonState
*vga
= &qxl
->vga
;
1796 if (qxl
->mode
== QXL_MODE_VGA
) {
1797 vga
->text_update(vga
, chardata
);
1802 static void qxl_dirty_surfaces(PCIQXLDevice
*qxl
)
1804 uintptr_t vram_start
;
1807 if (qxl
->mode
!= QXL_MODE_NATIVE
&& qxl
->mode
!= QXL_MODE_COMPAT
) {
1811 /* dirty the primary surface */
1812 qxl_set_dirty(&qxl
->vga
.vram
, qxl
->shadow_rom
.draw_area_offset
,
1813 qxl
->shadow_rom
.surface0_area_size
);
1815 vram_start
= (uintptr_t)memory_region_get_ram_ptr(&qxl
->vram_bar
);
1817 /* dirty the off-screen surfaces */
1818 for (i
= 0; i
< qxl
->ssd
.num_surfaces
; i
++) {
1820 intptr_t surface_offset
;
1823 if (qxl
->guest_surfaces
.cmds
[i
] == 0) {
1827 cmd
= qxl_phys2virt(qxl
, qxl
->guest_surfaces
.cmds
[i
],
1828 MEMSLOT_GROUP_GUEST
);
1830 assert(cmd
->type
== QXL_SURFACE_CMD_CREATE
);
1831 surface_offset
= (intptr_t)qxl_phys2virt(qxl
,
1832 cmd
->u
.surface_create
.data
,
1833 MEMSLOT_GROUP_GUEST
);
1834 assert(surface_offset
);
1835 surface_offset
-= vram_start
;
1836 surface_size
= cmd
->u
.surface_create
.height
*
1837 abs(cmd
->u
.surface_create
.stride
);
1838 trace_qxl_surfaces_dirty(qxl
->id
, i
, (int)surface_offset
, surface_size
);
1839 qxl_set_dirty(&qxl
->vram_bar
, surface_offset
, surface_size
);
1843 static void qxl_vm_change_state_handler(void *opaque
, int running
,
1846 PCIQXLDevice
*qxl
= opaque
;
1850 * if qxl_send_events was called from spice server context before
1851 * migration ended, qxl_update_irq for these events might not have been
1854 qxl_update_irq(qxl
);
1856 /* make sure surfaces are saved before migration */
1857 qxl_dirty_surfaces(qxl
);
1861 /* display change listener */
1863 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
1865 if (qxl0
->mode
== QXL_MODE_VGA
) {
1866 qemu_spice_display_update(&qxl0
->ssd
, x
, y
, w
, h
);
1870 static void display_resize(struct DisplayState
*ds
)
1872 if (qxl0
->mode
== QXL_MODE_VGA
) {
1873 qemu_spice_display_resize(&qxl0
->ssd
);
1877 static void display_refresh(struct DisplayState
*ds
)
1879 if (qxl0
->mode
== QXL_MODE_VGA
) {
1880 qemu_spice_display_refresh(&qxl0
->ssd
);
1882 qemu_mutex_lock(&qxl0
->ssd
.lock
);
1883 qemu_spice_cursor_refresh_unlocked(&qxl0
->ssd
);
1884 qemu_mutex_unlock(&qxl0
->ssd
.lock
);
1888 static DisplayChangeListener display_listener
= {
1889 .dpy_gfx_update
= display_update
,
1890 .dpy_gfx_resize
= display_resize
,
1891 .dpy_refresh
= display_refresh
,
1894 static void qxl_init_ramsize(PCIQXLDevice
*qxl
)
1896 /* vga mode framebuffer / primary surface (bar 0, first part) */
1897 if (qxl
->vgamem_size_mb
< 8) {
1898 qxl
->vgamem_size_mb
= 8;
1900 qxl
->vgamem_size
= qxl
->vgamem_size_mb
* 1024 * 1024;
1902 /* vga ram (bar 0, total) */
1903 if (qxl
->ram_size_mb
!= -1) {
1904 qxl
->vga
.vram_size
= qxl
->ram_size_mb
* 1024 * 1024;
1906 if (qxl
->vga
.vram_size
< qxl
->vgamem_size
* 2) {
1907 qxl
->vga
.vram_size
= qxl
->vgamem_size
* 2;
1910 /* vram32 (surfaces, 32bit, bar 1) */
1911 if (qxl
->vram32_size_mb
!= -1) {
1912 qxl
->vram32_size
= qxl
->vram32_size_mb
* 1024 * 1024;
1914 if (qxl
->vram32_size
< 4096) {
1915 qxl
->vram32_size
= 4096;
1918 /* vram (surfaces, 64bit, bar 4+5) */
1919 if (qxl
->vram_size_mb
!= -1) {
1920 qxl
->vram_size
= qxl
->vram_size_mb
* 1024 * 1024;
1922 if (qxl
->vram_size
< qxl
->vram32_size
) {
1923 qxl
->vram_size
= qxl
->vram32_size
;
1926 if (qxl
->revision
== 1) {
1927 qxl
->vram32_size
= 4096;
1928 qxl
->vram_size
= 4096;
1930 qxl
->vgamem_size
= msb_mask(qxl
->vgamem_size
* 2 - 1);
1931 qxl
->vga
.vram_size
= msb_mask(qxl
->vga
.vram_size
* 2 - 1);
1932 qxl
->vram32_size
= msb_mask(qxl
->vram32_size
* 2 - 1);
1933 qxl
->vram_size
= msb_mask(qxl
->vram_size
* 2 - 1);
1936 static int qxl_init_common(PCIQXLDevice
*qxl
)
1938 uint8_t* config
= qxl
->pci
.config
;
1939 uint32_t pci_device_rev
;
1942 qxl
->mode
= QXL_MODE_UNDEFINED
;
1943 qxl
->generation
= 1;
1944 qxl
->num_memslots
= NUM_MEMSLOTS
;
1945 qemu_mutex_init(&qxl
->track_lock
);
1946 qemu_mutex_init(&qxl
->async_lock
);
1947 qxl
->current_async
= QXL_UNDEFINED_IO
;
1950 switch (qxl
->revision
) {
1951 case 1: /* spice 0.4 -- qxl-1 */
1952 pci_device_rev
= QXL_REVISION_STABLE_V04
;
1955 case 2: /* spice 0.6 -- qxl-2 */
1956 pci_device_rev
= QXL_REVISION_STABLE_V06
;
1960 pci_device_rev
= QXL_REVISION_STABLE_V10
;
1961 io_size
= 32; /* PCI region size must be pow2 */
1964 pci_device_rev
= QXL_REVISION_STABLE_V12
;
1965 io_size
= msb_mask(QXL_IO_RANGE_SIZE
* 2 - 1);
1968 error_report("Invalid revision %d for qxl device (max %d)",
1969 qxl
->revision
, QXL_DEFAULT_REVISION
);
1973 pci_set_byte(&config
[PCI_REVISION_ID
], pci_device_rev
);
1974 pci_set_byte(&config
[PCI_INTERRUPT_PIN
], 1);
1976 qxl
->rom_size
= qxl_rom_size();
1977 memory_region_init_ram(&qxl
->rom_bar
, "qxl.vrom", qxl
->rom_size
);
1978 vmstate_register_ram(&qxl
->rom_bar
, &qxl
->pci
.qdev
);
1982 qxl
->guest_surfaces
.cmds
= g_new0(QXLPHYSICAL
, qxl
->ssd
.num_surfaces
);
1983 memory_region_init_ram(&qxl
->vram_bar
, "qxl.vram", qxl
->vram_size
);
1984 vmstate_register_ram(&qxl
->vram_bar
, &qxl
->pci
.qdev
);
1985 memory_region_init_alias(&qxl
->vram32_bar
, "qxl.vram32", &qxl
->vram_bar
,
1986 0, qxl
->vram32_size
);
1988 memory_region_init_io(&qxl
->io_bar
, &qxl_io_ops
, qxl
,
1989 "qxl-ioports", io_size
);
1991 vga_dirty_log_start(&qxl
->vga
);
1993 memory_region_set_flush_coalesced(&qxl
->io_bar
);
1996 pci_register_bar(&qxl
->pci
, QXL_IO_RANGE_INDEX
,
1997 PCI_BASE_ADDRESS_SPACE_IO
, &qxl
->io_bar
);
1999 pci_register_bar(&qxl
->pci
, QXL_ROM_RANGE_INDEX
,
2000 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->rom_bar
);
2002 pci_register_bar(&qxl
->pci
, QXL_RAM_RANGE_INDEX
,
2003 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->vga
.vram
);
2005 pci_register_bar(&qxl
->pci
, QXL_VRAM_RANGE_INDEX
,
2006 PCI_BASE_ADDRESS_SPACE_MEMORY
, &qxl
->vram32_bar
);
2008 if (qxl
->vram32_size
< qxl
->vram_size
) {
2010 * Make the 64bit vram bar show up only in case it is
2011 * configured to be larger than the 32bit vram bar.
2013 pci_register_bar(&qxl
->pci
, QXL_VRAM64_RANGE_INDEX
,
2014 PCI_BASE_ADDRESS_SPACE_MEMORY
|
2015 PCI_BASE_ADDRESS_MEM_TYPE_64
|
2016 PCI_BASE_ADDRESS_MEM_PREFETCH
,
2020 /* print pci bar details */
2021 dprint(qxl
, 1, "ram/%s: %d MB [region 0]\n",
2022 qxl
->id
== 0 ? "pri" : "sec",
2023 qxl
->vga
.vram_size
/ (1024*1024));
2024 dprint(qxl
, 1, "vram/32: %d MB [region 1]\n",
2025 qxl
->vram32_size
/ (1024*1024));
2026 dprint(qxl
, 1, "vram/64: %d MB %s\n",
2027 qxl
->vram_size
/ (1024*1024),
2028 qxl
->vram32_size
< qxl
->vram_size
? "[region 4]" : "[unmapped]");
2030 qxl
->ssd
.qxl
.base
.sif
= &qxl_interface
.base
;
2031 qxl
->ssd
.qxl
.id
= qxl
->id
;
2032 if (qemu_spice_add_interface(&qxl
->ssd
.qxl
.base
) != 0) {
2033 error_report("qxl interface %d.%d not supported by spice-server\n",
2034 SPICE_INTERFACE_QXL_MAJOR
, SPICE_INTERFACE_QXL_MINOR
);
2037 qemu_add_vm_change_state_handler(qxl_vm_change_state_handler
, qxl
);
2039 init_pipe_signaling(qxl
);
2040 qxl_reset_state(qxl
);
2042 qxl
->update_area_bh
= qemu_bh_new(qxl_render_update_area_bh
, qxl
);
2047 static int qxl_init_primary(PCIDevice
*dev
)
2049 PCIQXLDevice
*qxl
= DO_UPCAST(PCIQXLDevice
, pci
, dev
);
2050 VGACommonState
*vga
= &qxl
->vga
;
2051 PortioList
*qxl_vga_port_list
= g_new(PortioList
, 1);
2055 qxl_init_ramsize(qxl
);
2056 vga
->vram_size_mb
= qxl
->vga
.vram_size
>> 20;
2057 vga_common_init(vga
);
2058 vga_init(vga
, pci_address_space(dev
), pci_address_space_io(dev
), false);
2059 portio_list_init(qxl_vga_port_list
, qxl_vga_portio_list
, vga
, "vga");
2060 portio_list_add(qxl_vga_port_list
, pci_address_space_io(dev
), 0x3b0);
2062 vga
->ds
= graphic_console_init(qxl_hw_update
, qxl_hw_invalidate
,
2063 qxl_hw_screen_dump
, qxl_hw_text_update
, qxl
);
2064 qemu_spice_display_init_common(&qxl
->ssd
, vga
->ds
);
2068 rc
= qxl_init_common(qxl
);
2073 register_displaychangelistener(vga
->ds
, &display_listener
);
2077 static int qxl_init_secondary(PCIDevice
*dev
)
2079 static int device_id
= 1;
2080 PCIQXLDevice
*qxl
= DO_UPCAST(PCIQXLDevice
, pci
, dev
);
2082 qxl
->id
= device_id
++;
2083 qxl_init_ramsize(qxl
);
2084 memory_region_init_ram(&qxl
->vga
.vram
, "qxl.vgavram", qxl
->vga
.vram_size
);
2085 vmstate_register_ram(&qxl
->vga
.vram
, &qxl
->pci
.qdev
);
2086 qxl
->vga
.vram_ptr
= memory_region_get_ram_ptr(&qxl
->vga
.vram
);
2088 return qxl_init_common(qxl
);
2091 static void qxl_pre_save(void *opaque
)
2093 PCIQXLDevice
* d
= opaque
;
2094 uint8_t *ram_start
= d
->vga
.vram_ptr
;
2096 trace_qxl_pre_save(d
->id
);
2097 if (d
->last_release
== NULL
) {
2098 d
->last_release_offset
= 0;
2100 d
->last_release_offset
= (uint8_t *)d
->last_release
- ram_start
;
2102 assert(d
->last_release_offset
< d
->vga
.vram_size
);
2105 static int qxl_pre_load(void *opaque
)
2107 PCIQXLDevice
* d
= opaque
;
2109 trace_qxl_pre_load(d
->id
);
2110 qxl_hard_reset(d
, 1);
2111 qxl_exit_vga_mode(d
);
2115 static void qxl_create_memslots(PCIQXLDevice
*d
)
2119 for (i
= 0; i
< NUM_MEMSLOTS
; i
++) {
2120 if (!d
->guest_slots
[i
].active
) {
2123 qxl_add_memslot(d
, i
, 0, QXL_SYNC
);
2127 static int qxl_post_load(void *opaque
, int version
)
2129 PCIQXLDevice
* d
= opaque
;
2130 uint8_t *ram_start
= d
->vga
.vram_ptr
;
2131 QXLCommandExt
*cmds
;
2132 int in
, out
, newmode
;
2134 assert(d
->last_release_offset
< d
->vga
.vram_size
);
2135 if (d
->last_release_offset
== 0) {
2136 d
->last_release
= NULL
;
2138 d
->last_release
= (QXLReleaseInfo
*)(ram_start
+ d
->last_release_offset
);
2141 d
->modes
= (QXLModes
*)((uint8_t*)d
->rom
+ d
->rom
->modes_offset
);
2143 trace_qxl_post_load(d
->id
, qxl_mode_to_string(d
->mode
));
2145 d
->mode
= QXL_MODE_UNDEFINED
;
2148 case QXL_MODE_UNDEFINED
:
2149 qxl_create_memslots(d
);
2152 qxl_create_memslots(d
);
2153 qxl_enter_vga_mode(d
);
2155 case QXL_MODE_NATIVE
:
2156 qxl_create_memslots(d
);
2157 qxl_create_guest_primary(d
, 1, QXL_SYNC
);
2159 /* replay surface-create and cursor-set commands */
2160 cmds
= g_malloc0(sizeof(QXLCommandExt
) * (d
->ssd
.num_surfaces
+ 1));
2161 for (in
= 0, out
= 0; in
< d
->ssd
.num_surfaces
; in
++) {
2162 if (d
->guest_surfaces
.cmds
[in
] == 0) {
2165 cmds
[out
].cmd
.data
= d
->guest_surfaces
.cmds
[in
];
2166 cmds
[out
].cmd
.type
= QXL_CMD_SURFACE
;
2167 cmds
[out
].group_id
= MEMSLOT_GROUP_GUEST
;
2170 if (d
->guest_cursor
) {
2171 cmds
[out
].cmd
.data
= d
->guest_cursor
;
2172 cmds
[out
].cmd
.type
= QXL_CMD_CURSOR
;
2173 cmds
[out
].group_id
= MEMSLOT_GROUP_GUEST
;
2176 qxl_spice_loadvm_commands(d
, cmds
, out
);
2178 if (d
->guest_monitors_config
) {
2179 qxl_spice_monitors_config_async(d
, 1);
2182 case QXL_MODE_COMPAT
:
2183 /* note: no need to call qxl_create_memslots, qxl_set_mode
2184 * creates the mem slot. */
2185 qxl_set_mode(d
, d
->shadow_rom
.mode
, 1);
2191 #define QXL_SAVE_VERSION 21
2193 static bool qxl_monitors_config_needed(void *opaque
)
2195 PCIQXLDevice
*qxl
= opaque
;
2197 return qxl
->guest_monitors_config
!= 0;
2201 static VMStateDescription qxl_memslot
= {
2202 .name
= "qxl-memslot",
2203 .version_id
= QXL_SAVE_VERSION
,
2204 .minimum_version_id
= QXL_SAVE_VERSION
,
2205 .fields
= (VMStateField
[]) {
2206 VMSTATE_UINT64(slot
.mem_start
, struct guest_slots
),
2207 VMSTATE_UINT64(slot
.mem_end
, struct guest_slots
),
2208 VMSTATE_UINT32(active
, struct guest_slots
),
2209 VMSTATE_END_OF_LIST()
2213 static VMStateDescription qxl_surface
= {
2214 .name
= "qxl-surface",
2215 .version_id
= QXL_SAVE_VERSION
,
2216 .minimum_version_id
= QXL_SAVE_VERSION
,
2217 .fields
= (VMStateField
[]) {
2218 VMSTATE_UINT32(width
, QXLSurfaceCreate
),
2219 VMSTATE_UINT32(height
, QXLSurfaceCreate
),
2220 VMSTATE_INT32(stride
, QXLSurfaceCreate
),
2221 VMSTATE_UINT32(format
, QXLSurfaceCreate
),
2222 VMSTATE_UINT32(position
, QXLSurfaceCreate
),
2223 VMSTATE_UINT32(mouse_mode
, QXLSurfaceCreate
),
2224 VMSTATE_UINT32(flags
, QXLSurfaceCreate
),
2225 VMSTATE_UINT32(type
, QXLSurfaceCreate
),
2226 VMSTATE_UINT64(mem
, QXLSurfaceCreate
),
2227 VMSTATE_END_OF_LIST()
2231 static VMStateDescription qxl_vmstate_monitors_config
= {
2232 .name
= "qxl/monitors-config",
2234 .minimum_version_id
= 1,
2235 .fields
= (VMStateField
[]) {
2236 VMSTATE_UINT64(guest_monitors_config
, PCIQXLDevice
),
2237 VMSTATE_END_OF_LIST()
2241 static VMStateDescription qxl_vmstate
= {
2243 .version_id
= QXL_SAVE_VERSION
,
2244 .minimum_version_id
= QXL_SAVE_VERSION
,
2245 .pre_save
= qxl_pre_save
,
2246 .pre_load
= qxl_pre_load
,
2247 .post_load
= qxl_post_load
,
2248 .fields
= (VMStateField
[]) {
2249 VMSTATE_PCI_DEVICE(pci
, PCIQXLDevice
),
2250 VMSTATE_STRUCT(vga
, PCIQXLDevice
, 0, vmstate_vga_common
, VGACommonState
),
2251 VMSTATE_UINT32(shadow_rom
.mode
, PCIQXLDevice
),
2252 VMSTATE_UINT32(num_free_res
, PCIQXLDevice
),
2253 VMSTATE_UINT32(last_release_offset
, PCIQXLDevice
),
2254 VMSTATE_UINT32(mode
, PCIQXLDevice
),
2255 VMSTATE_UINT32(ssd
.unique
, PCIQXLDevice
),
2256 VMSTATE_INT32_EQUAL(num_memslots
, PCIQXLDevice
),
2257 VMSTATE_STRUCT_ARRAY(guest_slots
, PCIQXLDevice
, NUM_MEMSLOTS
, 0,
2258 qxl_memslot
, struct guest_slots
),
2259 VMSTATE_STRUCT(guest_primary
.surface
, PCIQXLDevice
, 0,
2260 qxl_surface
, QXLSurfaceCreate
),
2261 VMSTATE_INT32_EQUAL(ssd
.num_surfaces
, PCIQXLDevice
),
2262 VMSTATE_VARRAY_INT32(guest_surfaces
.cmds
, PCIQXLDevice
,
2263 ssd
.num_surfaces
, 0,
2264 vmstate_info_uint64
, uint64_t),
2265 VMSTATE_UINT64(guest_cursor
, PCIQXLDevice
),
2266 VMSTATE_END_OF_LIST()
2268 .subsections
= (VMStateSubsection
[]) {
2270 .vmsd
= &qxl_vmstate_monitors_config
,
2271 .needed
= qxl_monitors_config_needed
,
2278 static Property qxl_properties
[] = {
2279 DEFINE_PROP_UINT32("ram_size", PCIQXLDevice
, vga
.vram_size
,
2281 DEFINE_PROP_UINT32("vram_size", PCIQXLDevice
, vram32_size
,
2283 DEFINE_PROP_UINT32("revision", PCIQXLDevice
, revision
,
2284 QXL_DEFAULT_REVISION
),
2285 DEFINE_PROP_UINT32("debug", PCIQXLDevice
, debug
, 0),
2286 DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice
, guestdebug
, 0),
2287 DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice
, cmdlog
, 0),
2288 DEFINE_PROP_UINT32("ram_size_mb", PCIQXLDevice
, ram_size_mb
, -1),
2289 DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice
, vram32_size_mb
, -1),
2290 DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice
, vram_size_mb
, -1),
2291 DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice
, vgamem_size_mb
, 16),
2292 DEFINE_PROP_INT32("surfaces", PCIQXLDevice
, ssd
.num_surfaces
, 1024),
2293 DEFINE_PROP_END_OF_LIST(),
2296 static void qxl_primary_class_init(ObjectClass
*klass
, void *data
)
2298 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2299 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2302 k
->init
= qxl_init_primary
;
2303 k
->romfile
= "vgabios-qxl.bin";
2304 k
->vendor_id
= REDHAT_PCI_VENDOR_ID
;
2305 k
->device_id
= QXL_DEVICE_ID_STABLE
;
2306 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
2307 dc
->desc
= "Spice QXL GPU (primary, vga compatible)";
2308 dc
->reset
= qxl_reset_handler
;
2309 dc
->vmsd
= &qxl_vmstate
;
2310 dc
->props
= qxl_properties
;
2313 static TypeInfo qxl_primary_info
= {
2315 .parent
= TYPE_PCI_DEVICE
,
2316 .instance_size
= sizeof(PCIQXLDevice
),
2317 .class_init
= qxl_primary_class_init
,
2320 static void qxl_secondary_class_init(ObjectClass
*klass
, void *data
)
2322 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2323 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2325 k
->init
= qxl_init_secondary
;
2326 k
->vendor_id
= REDHAT_PCI_VENDOR_ID
;
2327 k
->device_id
= QXL_DEVICE_ID_STABLE
;
2328 k
->class_id
= PCI_CLASS_DISPLAY_OTHER
;
2329 dc
->desc
= "Spice QXL GPU (secondary)";
2330 dc
->reset
= qxl_reset_handler
;
2331 dc
->vmsd
= &qxl_vmstate
;
2332 dc
->props
= qxl_properties
;
2335 static TypeInfo qxl_secondary_info
= {
2337 .parent
= TYPE_PCI_DEVICE
,
2338 .instance_size
= sizeof(PCIQXLDevice
),
2339 .class_init
= qxl_secondary_class_init
,
2342 static void qxl_register_types(void)
2344 type_register_static(&qxl_primary_info
);
2345 type_register_static(&qxl_secondary_info
);
2348 type_init(qxl_register_types
)