root / hw / qxl.c @ 30f6da66
History | View | Annotate | Download (56.6 kB)
1 |
/*
|
---|---|
2 |
* Copyright (C) 2010 Red Hat, Inc.
|
3 |
*
|
4 |
* written by Yaniv Kamay, Izik Eidus, Gerd Hoffmann
|
5 |
* maintained by Gerd Hoffmann <kraxel@redhat.com>
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License as
|
9 |
* published by the Free Software Foundation; either version 2 or
|
10 |
* (at your option) version 3 of the License.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
|
21 |
#include "qemu-common.h" |
22 |
#include "qemu-timer.h" |
23 |
#include "qemu-queue.h" |
24 |
#include "monitor.h" |
25 |
#include "sysemu.h" |
26 |
|
27 |
#include "qxl.h" |
28 |
|
29 |
#undef SPICE_RING_PROD_ITEM
|
30 |
#define SPICE_RING_PROD_ITEM(r, ret) { \
|
31 |
typeof(r) start = r; \ |
32 |
typeof(r) end = r + 1; \
|
33 |
uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r); \ |
34 |
typeof(&(r)->items[prod]) m_item = &(r)->items[prod]; \ |
35 |
if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \ |
36 |
abort(); \ |
37 |
} \ |
38 |
ret = &m_item->el; \ |
39 |
} |
40 |
|
41 |
#undef SPICE_RING_CONS_ITEM
|
42 |
#define SPICE_RING_CONS_ITEM(r, ret) { \
|
43 |
typeof(r) start = r; \ |
44 |
typeof(r) end = r + 1; \
|
45 |
uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \ |
46 |
typeof(&(r)->items[cons]) m_item = &(r)->items[cons]; \ |
47 |
if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \ |
48 |
abort(); \ |
49 |
} \ |
50 |
ret = &m_item->el; \ |
51 |
} |
52 |
|
53 |
#undef ALIGN
|
54 |
#define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) |
55 |
|
56 |
#define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9" |
57 |
|
58 |
#define QXL_MODE(_x, _y, _b, _o) \
|
59 |
{ .x_res = _x, \ |
60 |
.y_res = _y, \ |
61 |
.bits = _b, \ |
62 |
.stride = (_x) * (_b) / 8, \
|
63 |
.x_mili = PIXEL_SIZE * (_x), \ |
64 |
.y_mili = PIXEL_SIZE * (_y), \ |
65 |
.orientation = _o, \ |
66 |
} |
67 |
|
68 |
#define QXL_MODE_16_32(x_res, y_res, orientation) \
|
69 |
QXL_MODE(x_res, y_res, 16, orientation), \
|
70 |
QXL_MODE(x_res, y_res, 32, orientation)
|
71 |
|
72 |
#define QXL_MODE_EX(x_res, y_res) \
|
73 |
QXL_MODE_16_32(x_res, y_res, 0), \
|
74 |
QXL_MODE_16_32(y_res, x_res, 1), \
|
75 |
QXL_MODE_16_32(x_res, y_res, 2), \
|
76 |
QXL_MODE_16_32(y_res, x_res, 3)
|
77 |
|
78 |
static QXLMode qxl_modes[] = {
|
79 |
QXL_MODE_EX(640, 480), |
80 |
QXL_MODE_EX(800, 480), |
81 |
QXL_MODE_EX(800, 600), |
82 |
QXL_MODE_EX(832, 624), |
83 |
QXL_MODE_EX(960, 640), |
84 |
QXL_MODE_EX(1024, 600), |
85 |
QXL_MODE_EX(1024, 768), |
86 |
QXL_MODE_EX(1152, 864), |
87 |
QXL_MODE_EX(1152, 870), |
88 |
QXL_MODE_EX(1280, 720), |
89 |
QXL_MODE_EX(1280, 760), |
90 |
QXL_MODE_EX(1280, 768), |
91 |
QXL_MODE_EX(1280, 800), |
92 |
QXL_MODE_EX(1280, 960), |
93 |
QXL_MODE_EX(1280, 1024), |
94 |
QXL_MODE_EX(1360, 768), |
95 |
QXL_MODE_EX(1366, 768), |
96 |
QXL_MODE_EX(1400, 1050), |
97 |
QXL_MODE_EX(1440, 900), |
98 |
QXL_MODE_EX(1600, 900), |
99 |
QXL_MODE_EX(1600, 1200), |
100 |
QXL_MODE_EX(1680, 1050), |
101 |
QXL_MODE_EX(1920, 1080), |
102 |
#if VGA_RAM_SIZE >= (16 * 1024 * 1024) |
103 |
/* these modes need more than 8 MB video memory */
|
104 |
QXL_MODE_EX(1920, 1200), |
105 |
QXL_MODE_EX(1920, 1440), |
106 |
QXL_MODE_EX(2048, 1536), |
107 |
QXL_MODE_EX(2560, 1440), |
108 |
QXL_MODE_EX(2560, 1600), |
109 |
#endif
|
110 |
#if VGA_RAM_SIZE >= (32 * 1024 * 1024) |
111 |
/* these modes need more than 16 MB video memory */
|
112 |
QXL_MODE_EX(2560, 2048), |
113 |
QXL_MODE_EX(2800, 2100), |
114 |
QXL_MODE_EX(3200, 2400), |
115 |
#endif
|
116 |
}; |
117 |
|
118 |
static PCIQXLDevice *qxl0;
|
119 |
|
120 |
static void qxl_send_events(PCIQXLDevice *d, uint32_t events); |
121 |
static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async); |
122 |
static void qxl_reset_memslots(PCIQXLDevice *d); |
123 |
static void qxl_reset_surfaces(PCIQXLDevice *d); |
124 |
static void qxl_ring_set_dirty(PCIQXLDevice *qxl); |
125 |
|
126 |
void qxl_guest_bug(PCIQXLDevice *qxl, const char *msg, ...) |
127 |
{ |
128 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
129 |
qxl_send_events(qxl, QXL_INTERRUPT_ERROR); |
130 |
#endif
|
131 |
if (qxl->guestdebug) {
|
132 |
va_list ap; |
133 |
va_start(ap, msg); |
134 |
fprintf(stderr, "qxl-%d: guest bug: ", qxl->id);
|
135 |
vfprintf(stderr, msg, ap); |
136 |
fprintf(stderr, "\n");
|
137 |
va_end(ap); |
138 |
} |
139 |
} |
140 |
|
141 |
|
142 |
void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
|
143 |
struct QXLRect *area, struct QXLRect *dirty_rects, |
144 |
uint32_t num_dirty_rects, |
145 |
uint32_t clear_dirty_region, |
146 |
qxl_async_io async) |
147 |
{ |
148 |
if (async == QXL_SYNC) {
|
149 |
qxl->ssd.worker->update_area(qxl->ssd.worker, surface_id, area, |
150 |
dirty_rects, num_dirty_rects, clear_dirty_region); |
151 |
} else {
|
152 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
153 |
spice_qxl_update_area_async(&qxl->ssd.qxl, surface_id, area, |
154 |
clear_dirty_region, 0);
|
155 |
#else
|
156 |
abort(); |
157 |
#endif
|
158 |
} |
159 |
} |
160 |
|
161 |
static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice *qxl, |
162 |
uint32_t id) |
163 |
{ |
164 |
qemu_mutex_lock(&qxl->track_lock); |
165 |
qxl->guest_surfaces.cmds[id] = 0;
|
166 |
qxl->guest_surfaces.count--; |
167 |
qemu_mutex_unlock(&qxl->track_lock); |
168 |
} |
169 |
|
170 |
static void qxl_spice_destroy_surface_wait(PCIQXLDevice *qxl, uint32_t id, |
171 |
qxl_async_io async) |
172 |
{ |
173 |
if (async) {
|
174 |
#if SPICE_INTERFACE_QXL_MINOR < 1 |
175 |
abort(); |
176 |
#else
|
177 |
spice_qxl_destroy_surface_async(&qxl->ssd.qxl, id, |
178 |
(uint64_t)id); |
179 |
#endif
|
180 |
} else {
|
181 |
qxl->ssd.worker->destroy_surface_wait(qxl->ssd.worker, id); |
182 |
qxl_spice_destroy_surface_wait_complete(qxl, id); |
183 |
} |
184 |
} |
185 |
|
186 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
187 |
static void qxl_spice_flush_surfaces_async(PCIQXLDevice *qxl) |
188 |
{ |
189 |
spice_qxl_flush_surfaces_async(&qxl->ssd.qxl, 0);
|
190 |
} |
191 |
#endif
|
192 |
|
193 |
void qxl_spice_loadvm_commands(PCIQXLDevice *qxl, struct QXLCommandExt *ext, |
194 |
uint32_t count) |
195 |
{ |
196 |
qxl->ssd.worker->loadvm_commands(qxl->ssd.worker, ext, count); |
197 |
} |
198 |
|
199 |
void qxl_spice_oom(PCIQXLDevice *qxl)
|
200 |
{ |
201 |
qxl->ssd.worker->oom(qxl->ssd.worker); |
202 |
} |
203 |
|
204 |
void qxl_spice_reset_memslots(PCIQXLDevice *qxl)
|
205 |
{ |
206 |
qxl->ssd.worker->reset_memslots(qxl->ssd.worker); |
207 |
} |
208 |
|
209 |
static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice *qxl) |
210 |
{ |
211 |
qemu_mutex_lock(&qxl->track_lock); |
212 |
memset(&qxl->guest_surfaces.cmds, 0, sizeof(qxl->guest_surfaces.cmds)); |
213 |
qxl->guest_surfaces.count = 0;
|
214 |
qemu_mutex_unlock(&qxl->track_lock); |
215 |
} |
216 |
|
217 |
static void qxl_spice_destroy_surfaces(PCIQXLDevice *qxl, qxl_async_io async) |
218 |
{ |
219 |
if (async) {
|
220 |
#if SPICE_INTERFACE_QXL_MINOR < 1 |
221 |
abort(); |
222 |
#else
|
223 |
spice_qxl_destroy_surfaces_async(&qxl->ssd.qxl, 0);
|
224 |
#endif
|
225 |
} else {
|
226 |
qxl->ssd.worker->destroy_surfaces(qxl->ssd.worker); |
227 |
qxl_spice_destroy_surfaces_complete(qxl); |
228 |
} |
229 |
} |
230 |
|
231 |
void qxl_spice_reset_image_cache(PCIQXLDevice *qxl)
|
232 |
{ |
233 |
qxl->ssd.worker->reset_image_cache(qxl->ssd.worker); |
234 |
} |
235 |
|
236 |
void qxl_spice_reset_cursor(PCIQXLDevice *qxl)
|
237 |
{ |
238 |
qxl->ssd.worker->reset_cursor(qxl->ssd.worker); |
239 |
qemu_mutex_lock(&qxl->track_lock); |
240 |
qxl->guest_cursor = 0;
|
241 |
qemu_mutex_unlock(&qxl->track_lock); |
242 |
} |
243 |
|
244 |
|
245 |
static inline uint32_t msb_mask(uint32_t val) |
246 |
{ |
247 |
uint32_t mask; |
248 |
|
249 |
do {
|
250 |
mask = ~(val - 1) & val;
|
251 |
val &= ~mask; |
252 |
} while (mask < val);
|
253 |
|
254 |
return mask;
|
255 |
} |
256 |
|
257 |
static ram_addr_t qxl_rom_size(void) |
258 |
{ |
259 |
uint32_t rom_size = sizeof(QXLRom) + sizeof(QXLModes) + sizeof(qxl_modes); |
260 |
rom_size = MAX(rom_size, TARGET_PAGE_SIZE); |
261 |
rom_size = msb_mask(rom_size * 2 - 1); |
262 |
return rom_size;
|
263 |
} |
264 |
|
265 |
static void init_qxl_rom(PCIQXLDevice *d) |
266 |
{ |
267 |
QXLRom *rom = memory_region_get_ram_ptr(&d->rom_bar); |
268 |
QXLModes *modes = (QXLModes *)(rom + 1);
|
269 |
uint32_t ram_header_size; |
270 |
uint32_t surface0_area_size; |
271 |
uint32_t num_pages; |
272 |
uint32_t fb, maxfb = 0;
|
273 |
int i;
|
274 |
|
275 |
memset(rom, 0, d->rom_size);
|
276 |
|
277 |
rom->magic = cpu_to_le32(QXL_ROM_MAGIC); |
278 |
rom->id = cpu_to_le32(d->id); |
279 |
rom->log_level = cpu_to_le32(d->guestdebug); |
280 |
rom->modes_offset = cpu_to_le32(sizeof(QXLRom));
|
281 |
|
282 |
rom->slot_gen_bits = MEMSLOT_GENERATION_BITS; |
283 |
rom->slot_id_bits = MEMSLOT_SLOT_BITS; |
284 |
rom->slots_start = 1;
|
285 |
rom->slots_end = NUM_MEMSLOTS - 1;
|
286 |
rom->n_surfaces = cpu_to_le32(NUM_SURFACES); |
287 |
|
288 |
modes->n_modes = cpu_to_le32(ARRAY_SIZE(qxl_modes)); |
289 |
for (i = 0; i < modes->n_modes; i++) { |
290 |
fb = qxl_modes[i].y_res * qxl_modes[i].stride; |
291 |
if (maxfb < fb) {
|
292 |
maxfb = fb; |
293 |
} |
294 |
modes->modes[i].id = cpu_to_le32(i); |
295 |
modes->modes[i].x_res = cpu_to_le32(qxl_modes[i].x_res); |
296 |
modes->modes[i].y_res = cpu_to_le32(qxl_modes[i].y_res); |
297 |
modes->modes[i].bits = cpu_to_le32(qxl_modes[i].bits); |
298 |
modes->modes[i].stride = cpu_to_le32(qxl_modes[i].stride); |
299 |
modes->modes[i].x_mili = cpu_to_le32(qxl_modes[i].x_mili); |
300 |
modes->modes[i].y_mili = cpu_to_le32(qxl_modes[i].y_mili); |
301 |
modes->modes[i].orientation = cpu_to_le32(qxl_modes[i].orientation); |
302 |
} |
303 |
if (maxfb < VGA_RAM_SIZE && d->id == 0) |
304 |
maxfb = VGA_RAM_SIZE; |
305 |
|
306 |
ram_header_size = ALIGN(sizeof(QXLRam), 4096); |
307 |
surface0_area_size = ALIGN(maxfb, 4096);
|
308 |
num_pages = d->vga.vram_size; |
309 |
num_pages -= ram_header_size; |
310 |
num_pages -= surface0_area_size; |
311 |
num_pages = num_pages / TARGET_PAGE_SIZE; |
312 |
|
313 |
rom->draw_area_offset = cpu_to_le32(0);
|
314 |
rom->surface0_area_size = cpu_to_le32(surface0_area_size); |
315 |
rom->pages_offset = cpu_to_le32(surface0_area_size); |
316 |
rom->num_pages = cpu_to_le32(num_pages); |
317 |
rom->ram_header_offset = cpu_to_le32(d->vga.vram_size - ram_header_size); |
318 |
|
319 |
d->shadow_rom = *rom; |
320 |
d->rom = rom; |
321 |
d->modes = modes; |
322 |
} |
323 |
|
324 |
static void init_qxl_ram(PCIQXLDevice *d) |
325 |
{ |
326 |
uint8_t *buf; |
327 |
uint64_t *item; |
328 |
|
329 |
buf = d->vga.vram_ptr; |
330 |
d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset)); |
331 |
d->ram->magic = cpu_to_le32(QXL_RAM_MAGIC); |
332 |
d->ram->int_pending = cpu_to_le32(0);
|
333 |
d->ram->int_mask = cpu_to_le32(0);
|
334 |
SPICE_RING_INIT(&d->ram->cmd_ring); |
335 |
SPICE_RING_INIT(&d->ram->cursor_ring); |
336 |
SPICE_RING_INIT(&d->ram->release_ring); |
337 |
SPICE_RING_PROD_ITEM(&d->ram->release_ring, item); |
338 |
*item = 0;
|
339 |
qxl_ring_set_dirty(d); |
340 |
} |
341 |
|
342 |
/* can be called from spice server thread context */
|
343 |
static void qxl_set_dirty(MemoryRegion *mr, ram_addr_t addr, ram_addr_t end) |
344 |
{ |
345 |
while (addr < end) {
|
346 |
memory_region_set_dirty(mr, addr); |
347 |
addr += TARGET_PAGE_SIZE; |
348 |
} |
349 |
} |
350 |
|
351 |
static void qxl_rom_set_dirty(PCIQXLDevice *qxl) |
352 |
{ |
353 |
qxl_set_dirty(&qxl->rom_bar, 0, qxl->rom_size);
|
354 |
} |
355 |
|
356 |
/* called from spice server thread context only */
|
357 |
static void qxl_ram_set_dirty(PCIQXLDevice *qxl, void *ptr) |
358 |
{ |
359 |
void *base = qxl->vga.vram_ptr;
|
360 |
intptr_t offset; |
361 |
|
362 |
offset = ptr - base; |
363 |
offset &= ~(TARGET_PAGE_SIZE-1);
|
364 |
assert(offset < qxl->vga.vram_size); |
365 |
qxl_set_dirty(&qxl->vga.vram, offset, offset + TARGET_PAGE_SIZE); |
366 |
} |
367 |
|
368 |
/* can be called from spice server thread context */
|
369 |
static void qxl_ring_set_dirty(PCIQXLDevice *qxl) |
370 |
{ |
371 |
ram_addr_t addr = qxl->shadow_rom.ram_header_offset; |
372 |
ram_addr_t end = qxl->vga.vram_size; |
373 |
qxl_set_dirty(&qxl->vga.vram, addr, end); |
374 |
} |
375 |
|
376 |
/*
|
377 |
* keep track of some command state, for savevm/loadvm.
|
378 |
* called from spice server thread context only
|
379 |
*/
|
380 |
static void qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext) |
381 |
{ |
382 |
switch (le32_to_cpu(ext->cmd.type)) {
|
383 |
case QXL_CMD_SURFACE:
|
384 |
{ |
385 |
QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id); |
386 |
uint32_t id = le32_to_cpu(cmd->surface_id); |
387 |
PANIC_ON(id >= NUM_SURFACES); |
388 |
qemu_mutex_lock(&qxl->track_lock); |
389 |
if (cmd->type == QXL_SURFACE_CMD_CREATE) {
|
390 |
qxl->guest_surfaces.cmds[id] = ext->cmd.data; |
391 |
qxl->guest_surfaces.count++; |
392 |
if (qxl->guest_surfaces.max < qxl->guest_surfaces.count)
|
393 |
qxl->guest_surfaces.max = qxl->guest_surfaces.count; |
394 |
} |
395 |
if (cmd->type == QXL_SURFACE_CMD_DESTROY) {
|
396 |
qxl->guest_surfaces.cmds[id] = 0;
|
397 |
qxl->guest_surfaces.count--; |
398 |
} |
399 |
qemu_mutex_unlock(&qxl->track_lock); |
400 |
break;
|
401 |
} |
402 |
case QXL_CMD_CURSOR:
|
403 |
{ |
404 |
QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id); |
405 |
if (cmd->type == QXL_CURSOR_SET) {
|
406 |
qemu_mutex_lock(&qxl->track_lock); |
407 |
qxl->guest_cursor = ext->cmd.data; |
408 |
qemu_mutex_unlock(&qxl->track_lock); |
409 |
} |
410 |
break;
|
411 |
} |
412 |
} |
413 |
} |
414 |
|
415 |
/* spice display interface callbacks */
|
416 |
|
417 |
static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker) |
418 |
{ |
419 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
420 |
|
421 |
dprint(qxl, 1, "%s:\n", __FUNCTION__); |
422 |
qxl->ssd.worker = qxl_worker; |
423 |
} |
424 |
|
425 |
static void interface_set_compression_level(QXLInstance *sin, int level) |
426 |
{ |
427 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
428 |
|
429 |
dprint(qxl, 1, "%s: %d\n", __FUNCTION__, level); |
430 |
qxl->shadow_rom.compression_level = cpu_to_le32(level); |
431 |
qxl->rom->compression_level = cpu_to_le32(level); |
432 |
qxl_rom_set_dirty(qxl); |
433 |
} |
434 |
|
435 |
static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time) |
436 |
{ |
437 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
438 |
|
439 |
qxl->shadow_rom.mm_clock = cpu_to_le32(mm_time); |
440 |
qxl->rom->mm_clock = cpu_to_le32(mm_time); |
441 |
qxl_rom_set_dirty(qxl); |
442 |
} |
443 |
|
444 |
static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info) |
445 |
{ |
446 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
447 |
|
448 |
dprint(qxl, 1, "%s:\n", __FUNCTION__); |
449 |
info->memslot_gen_bits = MEMSLOT_GENERATION_BITS; |
450 |
info->memslot_id_bits = MEMSLOT_SLOT_BITS; |
451 |
info->num_memslots = NUM_MEMSLOTS; |
452 |
info->num_memslots_groups = NUM_MEMSLOTS_GROUPS; |
453 |
info->internal_groupslot_id = 0;
|
454 |
info->qxl_ram_size = le32_to_cpu(qxl->shadow_rom.num_pages) << TARGET_PAGE_BITS; |
455 |
info->n_surfaces = NUM_SURFACES; |
456 |
} |
457 |
|
458 |
static const char *qxl_mode_to_string(int mode) |
459 |
{ |
460 |
switch (mode) {
|
461 |
case QXL_MODE_COMPAT:
|
462 |
return "compat"; |
463 |
case QXL_MODE_NATIVE:
|
464 |
return "native"; |
465 |
case QXL_MODE_UNDEFINED:
|
466 |
return "undefined"; |
467 |
case QXL_MODE_VGA:
|
468 |
return "vga"; |
469 |
} |
470 |
return "INVALID"; |
471 |
} |
472 |
|
473 |
static const char *io_port_to_string(uint32_t io_port) |
474 |
{ |
475 |
if (io_port >= QXL_IO_RANGE_SIZE) {
|
476 |
return "out of range"; |
477 |
} |
478 |
static const char *io_port_to_string[QXL_IO_RANGE_SIZE + 1] = { |
479 |
[QXL_IO_NOTIFY_CMD] = "QXL_IO_NOTIFY_CMD",
|
480 |
[QXL_IO_NOTIFY_CURSOR] = "QXL_IO_NOTIFY_CURSOR",
|
481 |
[QXL_IO_UPDATE_AREA] = "QXL_IO_UPDATE_AREA",
|
482 |
[QXL_IO_UPDATE_IRQ] = "QXL_IO_UPDATE_IRQ",
|
483 |
[QXL_IO_NOTIFY_OOM] = "QXL_IO_NOTIFY_OOM",
|
484 |
[QXL_IO_RESET] = "QXL_IO_RESET",
|
485 |
[QXL_IO_SET_MODE] = "QXL_IO_SET_MODE",
|
486 |
[QXL_IO_LOG] = "QXL_IO_LOG",
|
487 |
[QXL_IO_MEMSLOT_ADD] = "QXL_IO_MEMSLOT_ADD",
|
488 |
[QXL_IO_MEMSLOT_DEL] = "QXL_IO_MEMSLOT_DEL",
|
489 |
[QXL_IO_DETACH_PRIMARY] = "QXL_IO_DETACH_PRIMARY",
|
490 |
[QXL_IO_ATTACH_PRIMARY] = "QXL_IO_ATTACH_PRIMARY",
|
491 |
[QXL_IO_CREATE_PRIMARY] = "QXL_IO_CREATE_PRIMARY",
|
492 |
[QXL_IO_DESTROY_PRIMARY] = "QXL_IO_DESTROY_PRIMARY",
|
493 |
[QXL_IO_DESTROY_SURFACE_WAIT] = "QXL_IO_DESTROY_SURFACE_WAIT",
|
494 |
[QXL_IO_DESTROY_ALL_SURFACES] = "QXL_IO_DESTROY_ALL_SURFACES",
|
495 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
496 |
[QXL_IO_UPDATE_AREA_ASYNC] = "QXL_IO_UPDATE_AREA_ASYNC",
|
497 |
[QXL_IO_MEMSLOT_ADD_ASYNC] = "QXL_IO_MEMSLOT_ADD_ASYNC",
|
498 |
[QXL_IO_CREATE_PRIMARY_ASYNC] = "QXL_IO_CREATE_PRIMARY_ASYNC",
|
499 |
[QXL_IO_DESTROY_PRIMARY_ASYNC] = "QXL_IO_DESTROY_PRIMARY_ASYNC",
|
500 |
[QXL_IO_DESTROY_SURFACE_ASYNC] = "QXL_IO_DESTROY_SURFACE_ASYNC",
|
501 |
[QXL_IO_DESTROY_ALL_SURFACES_ASYNC] |
502 |
= "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
|
503 |
[QXL_IO_FLUSH_SURFACES_ASYNC] = "QXL_IO_FLUSH_SURFACES_ASYNC",
|
504 |
[QXL_IO_FLUSH_RELEASE] = "QXL_IO_FLUSH_RELEASE",
|
505 |
#endif
|
506 |
}; |
507 |
return io_port_to_string[io_port];
|
508 |
} |
509 |
|
510 |
/* called from spice server thread context only */
|
511 |
static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext) |
512 |
{ |
513 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
514 |
SimpleSpiceUpdate *update; |
515 |
QXLCommandRing *ring; |
516 |
QXLCommand *cmd; |
517 |
int notify, ret;
|
518 |
|
519 |
switch (qxl->mode) {
|
520 |
case QXL_MODE_VGA:
|
521 |
dprint(qxl, 2, "%s: vga\n", __FUNCTION__); |
522 |
ret = false;
|
523 |
qemu_mutex_lock(&qxl->ssd.lock); |
524 |
if (qxl->ssd.update != NULL) { |
525 |
update = qxl->ssd.update; |
526 |
qxl->ssd.update = NULL;
|
527 |
*ext = update->ext; |
528 |
ret = true;
|
529 |
} |
530 |
qemu_mutex_unlock(&qxl->ssd.lock); |
531 |
if (ret) {
|
532 |
dprint(qxl, 2, "%s %s\n", __FUNCTION__, qxl_mode_to_string(qxl->mode)); |
533 |
qxl_log_command(qxl, "vga", ext);
|
534 |
} |
535 |
return ret;
|
536 |
case QXL_MODE_COMPAT:
|
537 |
case QXL_MODE_NATIVE:
|
538 |
case QXL_MODE_UNDEFINED:
|
539 |
dprint(qxl, 4, "%s: %s\n", __FUNCTION__, qxl_mode_to_string(qxl->mode)); |
540 |
ring = &qxl->ram->cmd_ring; |
541 |
if (SPICE_RING_IS_EMPTY(ring)) {
|
542 |
return false; |
543 |
} |
544 |
dprint(qxl, 2, "%s: %s\n", __FUNCTION__, qxl_mode_to_string(qxl->mode)); |
545 |
SPICE_RING_CONS_ITEM(ring, cmd); |
546 |
ext->cmd = *cmd; |
547 |
ext->group_id = MEMSLOT_GROUP_GUEST; |
548 |
ext->flags = qxl->cmdflags; |
549 |
SPICE_RING_POP(ring, notify); |
550 |
qxl_ring_set_dirty(qxl); |
551 |
if (notify) {
|
552 |
qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY); |
553 |
} |
554 |
qxl->guest_primary.commands++; |
555 |
qxl_track_command(qxl, ext); |
556 |
qxl_log_command(qxl, "cmd", ext);
|
557 |
return true; |
558 |
default:
|
559 |
return false; |
560 |
} |
561 |
} |
562 |
|
563 |
/* called from spice server thread context only */
|
564 |
static int interface_req_cmd_notification(QXLInstance *sin) |
565 |
{ |
566 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
567 |
int wait = 1; |
568 |
|
569 |
switch (qxl->mode) {
|
570 |
case QXL_MODE_COMPAT:
|
571 |
case QXL_MODE_NATIVE:
|
572 |
case QXL_MODE_UNDEFINED:
|
573 |
SPICE_RING_CONS_WAIT(&qxl->ram->cmd_ring, wait); |
574 |
qxl_ring_set_dirty(qxl); |
575 |
break;
|
576 |
default:
|
577 |
/* nothing */
|
578 |
break;
|
579 |
} |
580 |
return wait;
|
581 |
} |
582 |
|
583 |
/* called from spice server thread context only */
|
584 |
static inline void qxl_push_free_res(PCIQXLDevice *d, int flush) |
585 |
{ |
586 |
QXLReleaseRing *ring = &d->ram->release_ring; |
587 |
uint64_t *item; |
588 |
int notify;
|
589 |
|
590 |
#define QXL_FREE_BUNCH_SIZE 32 |
591 |
|
592 |
if (ring->prod - ring->cons + 1 == ring->num_items) { |
593 |
/* ring full -- can't push */
|
594 |
return;
|
595 |
} |
596 |
if (!flush && d->oom_running) {
|
597 |
/* collect everything from oom handler before pushing */
|
598 |
return;
|
599 |
} |
600 |
if (!flush && d->num_free_res < QXL_FREE_BUNCH_SIZE) {
|
601 |
/* collect a bit more before pushing */
|
602 |
return;
|
603 |
} |
604 |
|
605 |
SPICE_RING_PUSH(ring, notify); |
606 |
dprint(d, 2, "free: push %d items, notify %s, ring %d/%d [%d,%d]\n", |
607 |
d->num_free_res, notify ? "yes" : "no", |
608 |
ring->prod - ring->cons, ring->num_items, |
609 |
ring->prod, ring->cons); |
610 |
if (notify) {
|
611 |
qxl_send_events(d, QXL_INTERRUPT_DISPLAY); |
612 |
} |
613 |
SPICE_RING_PROD_ITEM(ring, item); |
614 |
*item = 0;
|
615 |
d->num_free_res = 0;
|
616 |
d->last_release = NULL;
|
617 |
qxl_ring_set_dirty(d); |
618 |
} |
619 |
|
620 |
/* called from spice server thread context only */
|
621 |
static void interface_release_resource(QXLInstance *sin, |
622 |
struct QXLReleaseInfoExt ext)
|
623 |
{ |
624 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
625 |
QXLReleaseRing *ring; |
626 |
uint64_t *item, id; |
627 |
|
628 |
if (ext.group_id == MEMSLOT_GROUP_HOST) {
|
629 |
/* host group -> vga mode update request */
|
630 |
qemu_spice_destroy_update(&qxl->ssd, (void*)ext.info->id);
|
631 |
return;
|
632 |
} |
633 |
|
634 |
/*
|
635 |
* ext->info points into guest-visible memory
|
636 |
* pci bar 0, $command.release_info
|
637 |
*/
|
638 |
ring = &qxl->ram->release_ring; |
639 |
SPICE_RING_PROD_ITEM(ring, item); |
640 |
if (*item == 0) { |
641 |
/* stick head into the ring */
|
642 |
id = ext.info->id; |
643 |
ext.info->next = 0;
|
644 |
qxl_ram_set_dirty(qxl, &ext.info->next); |
645 |
*item = id; |
646 |
qxl_ring_set_dirty(qxl); |
647 |
} else {
|
648 |
/* append item to the list */
|
649 |
qxl->last_release->next = ext.info->id; |
650 |
qxl_ram_set_dirty(qxl, &qxl->last_release->next); |
651 |
ext.info->next = 0;
|
652 |
qxl_ram_set_dirty(qxl, &ext.info->next); |
653 |
} |
654 |
qxl->last_release = ext.info; |
655 |
qxl->num_free_res++; |
656 |
dprint(qxl, 3, "%4d\r", qxl->num_free_res); |
657 |
qxl_push_free_res(qxl, 0);
|
658 |
} |
659 |
|
660 |
/* called from spice server thread context only */
|
661 |
static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext) |
662 |
{ |
663 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
664 |
QXLCursorRing *ring; |
665 |
QXLCommand *cmd; |
666 |
int notify;
|
667 |
|
668 |
switch (qxl->mode) {
|
669 |
case QXL_MODE_COMPAT:
|
670 |
case QXL_MODE_NATIVE:
|
671 |
case QXL_MODE_UNDEFINED:
|
672 |
ring = &qxl->ram->cursor_ring; |
673 |
if (SPICE_RING_IS_EMPTY(ring)) {
|
674 |
return false; |
675 |
} |
676 |
SPICE_RING_CONS_ITEM(ring, cmd); |
677 |
ext->cmd = *cmd; |
678 |
ext->group_id = MEMSLOT_GROUP_GUEST; |
679 |
ext->flags = qxl->cmdflags; |
680 |
SPICE_RING_POP(ring, notify); |
681 |
qxl_ring_set_dirty(qxl); |
682 |
if (notify) {
|
683 |
qxl_send_events(qxl, QXL_INTERRUPT_CURSOR); |
684 |
} |
685 |
qxl->guest_primary.commands++; |
686 |
qxl_track_command(qxl, ext); |
687 |
qxl_log_command(qxl, "csr", ext);
|
688 |
if (qxl->id == 0) { |
689 |
qxl_render_cursor(qxl, ext); |
690 |
} |
691 |
return true; |
692 |
default:
|
693 |
return false; |
694 |
} |
695 |
} |
696 |
|
697 |
/* called from spice server thread context only */
|
698 |
static int interface_req_cursor_notification(QXLInstance *sin) |
699 |
{ |
700 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
701 |
int wait = 1; |
702 |
|
703 |
switch (qxl->mode) {
|
704 |
case QXL_MODE_COMPAT:
|
705 |
case QXL_MODE_NATIVE:
|
706 |
case QXL_MODE_UNDEFINED:
|
707 |
SPICE_RING_CONS_WAIT(&qxl->ram->cursor_ring, wait); |
708 |
qxl_ring_set_dirty(qxl); |
709 |
break;
|
710 |
default:
|
711 |
/* nothing */
|
712 |
break;
|
713 |
} |
714 |
return wait;
|
715 |
} |
716 |
|
717 |
/* called from spice server thread context */
|
718 |
static void interface_notify_update(QXLInstance *sin, uint32_t update_id) |
719 |
{ |
720 |
fprintf(stderr, "%s: abort()\n", __FUNCTION__);
|
721 |
abort(); |
722 |
} |
723 |
|
724 |
/* called from spice server thread context only */
|
725 |
static int interface_flush_resources(QXLInstance *sin) |
726 |
{ |
727 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
728 |
int ret;
|
729 |
|
730 |
dprint(qxl, 1, "free: guest flush (have %d)\n", qxl->num_free_res); |
731 |
ret = qxl->num_free_res; |
732 |
if (ret) {
|
733 |
qxl_push_free_res(qxl, 1);
|
734 |
} |
735 |
return ret;
|
736 |
} |
737 |
|
738 |
static void qxl_create_guest_primary_complete(PCIQXLDevice *d); |
739 |
|
740 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
741 |
|
742 |
/* called from spice server thread context only */
|
743 |
static void interface_async_complete(QXLInstance *sin, uint64_t cookie) |
744 |
{ |
745 |
PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl); |
746 |
uint32_t current_async; |
747 |
|
748 |
qemu_mutex_lock(&qxl->async_lock); |
749 |
current_async = qxl->current_async; |
750 |
qxl->current_async = QXL_UNDEFINED_IO; |
751 |
qemu_mutex_unlock(&qxl->async_lock); |
752 |
|
753 |
dprint(qxl, 2, "async_complete: %d (%ld) done\n", current_async, cookie); |
754 |
switch (current_async) {
|
755 |
case QXL_IO_CREATE_PRIMARY_ASYNC:
|
756 |
qxl_create_guest_primary_complete(qxl); |
757 |
break;
|
758 |
case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
|
759 |
qxl_spice_destroy_surfaces_complete(qxl); |
760 |
break;
|
761 |
case QXL_IO_DESTROY_SURFACE_ASYNC:
|
762 |
qxl_spice_destroy_surface_wait_complete(qxl, (uint32_t)cookie); |
763 |
break;
|
764 |
} |
765 |
qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD); |
766 |
} |
767 |
|
768 |
#endif
|
769 |
|
770 |
static const QXLInterface qxl_interface = { |
771 |
.base.type = SPICE_INTERFACE_QXL, |
772 |
.base.description = "qxl gpu",
|
773 |
.base.major_version = SPICE_INTERFACE_QXL_MAJOR, |
774 |
.base.minor_version = SPICE_INTERFACE_QXL_MINOR, |
775 |
|
776 |
.attache_worker = interface_attach_worker, |
777 |
.set_compression_level = interface_set_compression_level, |
778 |
.set_mm_time = interface_set_mm_time, |
779 |
.get_init_info = interface_get_init_info, |
780 |
|
781 |
/* the callbacks below are called from spice server thread context */
|
782 |
.get_command = interface_get_command, |
783 |
.req_cmd_notification = interface_req_cmd_notification, |
784 |
.release_resource = interface_release_resource, |
785 |
.get_cursor_command = interface_get_cursor_command, |
786 |
.req_cursor_notification = interface_req_cursor_notification, |
787 |
.notify_update = interface_notify_update, |
788 |
.flush_resources = interface_flush_resources, |
789 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
790 |
.async_complete = interface_async_complete, |
791 |
#endif
|
792 |
}; |
793 |
|
794 |
static void qxl_enter_vga_mode(PCIQXLDevice *d) |
795 |
{ |
796 |
if (d->mode == QXL_MODE_VGA) {
|
797 |
return;
|
798 |
} |
799 |
dprint(d, 1, "%s\n", __FUNCTION__); |
800 |
qemu_spice_create_host_primary(&d->ssd); |
801 |
d->mode = QXL_MODE_VGA; |
802 |
memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty)); |
803 |
} |
804 |
|
805 |
static void qxl_exit_vga_mode(PCIQXLDevice *d) |
806 |
{ |
807 |
if (d->mode != QXL_MODE_VGA) {
|
808 |
return;
|
809 |
} |
810 |
dprint(d, 1, "%s\n", __FUNCTION__); |
811 |
qxl_destroy_primary(d, QXL_SYNC); |
812 |
} |
813 |
|
814 |
static void qxl_update_irq(PCIQXLDevice *d) |
815 |
{ |
816 |
uint32_t pending = le32_to_cpu(d->ram->int_pending); |
817 |
uint32_t mask = le32_to_cpu(d->ram->int_mask); |
818 |
int level = !!(pending & mask);
|
819 |
qemu_set_irq(d->pci.irq[0], level);
|
820 |
qxl_ring_set_dirty(d); |
821 |
} |
822 |
|
823 |
static void qxl_check_state(PCIQXLDevice *d) |
824 |
{ |
825 |
QXLRam *ram = d->ram; |
826 |
|
827 |
assert(!d->ssd.running || SPICE_RING_IS_EMPTY(&ram->cmd_ring)); |
828 |
assert(!d->ssd.running || SPICE_RING_IS_EMPTY(&ram->cursor_ring)); |
829 |
} |
830 |
|
831 |
static void qxl_reset_state(PCIQXLDevice *d) |
832 |
{ |
833 |
QXLRom *rom = d->rom; |
834 |
|
835 |
qxl_check_state(d); |
836 |
d->shadow_rom.update_id = cpu_to_le32(0);
|
837 |
*rom = d->shadow_rom; |
838 |
qxl_rom_set_dirty(d); |
839 |
init_qxl_ram(d); |
840 |
d->num_free_res = 0;
|
841 |
d->last_release = NULL;
|
842 |
memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty)); |
843 |
} |
844 |
|
845 |
static void qxl_soft_reset(PCIQXLDevice *d) |
846 |
{ |
847 |
dprint(d, 1, "%s:\n", __FUNCTION__); |
848 |
qxl_check_state(d); |
849 |
|
850 |
if (d->id == 0) { |
851 |
qxl_enter_vga_mode(d); |
852 |
} else {
|
853 |
d->mode = QXL_MODE_UNDEFINED; |
854 |
} |
855 |
} |
856 |
|
857 |
static void qxl_hard_reset(PCIQXLDevice *d, int loadvm) |
858 |
{ |
859 |
dprint(d, 1, "%s: start%s\n", __FUNCTION__, |
860 |
loadvm ? " (loadvm)" : ""); |
861 |
|
862 |
qxl_spice_reset_cursor(d); |
863 |
qxl_spice_reset_image_cache(d); |
864 |
qxl_reset_surfaces(d); |
865 |
qxl_reset_memslots(d); |
866 |
|
867 |
/* pre loadvm reset must not touch QXLRam. This lives in
|
868 |
* device memory, is migrated together with RAM and thus
|
869 |
* already loaded at this point */
|
870 |
if (!loadvm) {
|
871 |
qxl_reset_state(d); |
872 |
} |
873 |
qemu_spice_create_host_memslot(&d->ssd); |
874 |
qxl_soft_reset(d); |
875 |
|
876 |
dprint(d, 1, "%s: done\n", __FUNCTION__); |
877 |
} |
878 |
|
879 |
static void qxl_reset_handler(DeviceState *dev) |
880 |
{ |
881 |
PCIQXLDevice *d = DO_UPCAST(PCIQXLDevice, pci.qdev, dev); |
882 |
qxl_hard_reset(d, 0);
|
883 |
} |
884 |
|
885 |
static void qxl_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
886 |
{ |
887 |
VGACommonState *vga = opaque; |
888 |
PCIQXLDevice *qxl = container_of(vga, PCIQXLDevice, vga); |
889 |
|
890 |
if (qxl->mode != QXL_MODE_VGA) {
|
891 |
dprint(qxl, 1, "%s\n", __FUNCTION__); |
892 |
qxl_destroy_primary(qxl, QXL_SYNC); |
893 |
qxl_soft_reset(qxl); |
894 |
} |
895 |
vga_ioport_write(opaque, addr, val); |
896 |
} |
897 |
|
898 |
static void qxl_add_memslot(PCIQXLDevice *d, uint32_t slot_id, uint64_t delta, |
899 |
qxl_async_io async) |
900 |
{ |
901 |
static const int regions[] = { |
902 |
QXL_RAM_RANGE_INDEX, |
903 |
QXL_VRAM_RANGE_INDEX, |
904 |
}; |
905 |
uint64_t guest_start; |
906 |
uint64_t guest_end; |
907 |
int pci_region;
|
908 |
pcibus_t pci_start; |
909 |
pcibus_t pci_end; |
910 |
intptr_t virt_start; |
911 |
QXLDevMemSlot memslot; |
912 |
int i;
|
913 |
|
914 |
guest_start = le64_to_cpu(d->guest_slots[slot_id].slot.mem_start); |
915 |
guest_end = le64_to_cpu(d->guest_slots[slot_id].slot.mem_end); |
916 |
|
917 |
dprint(d, 1, "%s: slot %d: guest phys 0x%" PRIx64 " - 0x%" PRIx64 "\n", |
918 |
__FUNCTION__, slot_id, |
919 |
guest_start, guest_end); |
920 |
|
921 |
PANIC_ON(slot_id >= NUM_MEMSLOTS); |
922 |
PANIC_ON(guest_start > guest_end); |
923 |
|
924 |
for (i = 0; i < ARRAY_SIZE(regions); i++) { |
925 |
pci_region = regions[i]; |
926 |
pci_start = d->pci.io_regions[pci_region].addr; |
927 |
pci_end = pci_start + d->pci.io_regions[pci_region].size; |
928 |
/* mapped? */
|
929 |
if (pci_start == -1) { |
930 |
continue;
|
931 |
} |
932 |
/* start address in range ? */
|
933 |
if (guest_start < pci_start || guest_start > pci_end) {
|
934 |
continue;
|
935 |
} |
936 |
/* end address in range ? */
|
937 |
if (guest_end > pci_end) {
|
938 |
continue;
|
939 |
} |
940 |
/* passed */
|
941 |
break;
|
942 |
} |
943 |
PANIC_ON(i == ARRAY_SIZE(regions)); /* finished loop without match */
|
944 |
|
945 |
switch (pci_region) {
|
946 |
case QXL_RAM_RANGE_INDEX:
|
947 |
virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vga.vram); |
948 |
break;
|
949 |
case QXL_VRAM_RANGE_INDEX:
|
950 |
virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vram_bar); |
951 |
break;
|
952 |
default:
|
953 |
/* should not happen */
|
954 |
abort(); |
955 |
} |
956 |
|
957 |
memslot.slot_id = slot_id; |
958 |
memslot.slot_group_id = MEMSLOT_GROUP_GUEST; /* guest group */
|
959 |
memslot.virt_start = virt_start + (guest_start - pci_start); |
960 |
memslot.virt_end = virt_start + (guest_end - pci_start); |
961 |
memslot.addr_delta = memslot.virt_start - delta; |
962 |
memslot.generation = d->rom->slot_generation = 0;
|
963 |
qxl_rom_set_dirty(d); |
964 |
|
965 |
dprint(d, 1, "%s: slot %d: host virt 0x%lx - 0x%lx\n", |
966 |
__FUNCTION__, memslot.slot_id, |
967 |
memslot.virt_start, memslot.virt_end); |
968 |
|
969 |
qemu_spice_add_memslot(&d->ssd, &memslot, async); |
970 |
d->guest_slots[slot_id].ptr = (void*)memslot.virt_start;
|
971 |
d->guest_slots[slot_id].size = memslot.virt_end - memslot.virt_start; |
972 |
d->guest_slots[slot_id].delta = delta; |
973 |
d->guest_slots[slot_id].active = 1;
|
974 |
} |
975 |
|
976 |
static void qxl_del_memslot(PCIQXLDevice *d, uint32_t slot_id) |
977 |
{ |
978 |
dprint(d, 1, "%s: slot %d\n", __FUNCTION__, slot_id); |
979 |
qemu_spice_del_memslot(&d->ssd, MEMSLOT_GROUP_HOST, slot_id); |
980 |
d->guest_slots[slot_id].active = 0;
|
981 |
} |
982 |
|
983 |
static void qxl_reset_memslots(PCIQXLDevice *d) |
984 |
{ |
985 |
dprint(d, 1, "%s:\n", __FUNCTION__); |
986 |
qxl_spice_reset_memslots(d); |
987 |
memset(&d->guest_slots, 0, sizeof(d->guest_slots)); |
988 |
} |
989 |
|
990 |
static void qxl_reset_surfaces(PCIQXLDevice *d) |
991 |
{ |
992 |
dprint(d, 1, "%s:\n", __FUNCTION__); |
993 |
d->mode = QXL_MODE_UNDEFINED; |
994 |
qxl_spice_destroy_surfaces(d, QXL_SYNC); |
995 |
} |
996 |
|
997 |
/* called from spice server thread context only */
|
998 |
void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id) |
999 |
{ |
1000 |
uint64_t phys = le64_to_cpu(pqxl); |
1001 |
uint32_t slot = (phys >> (64 - 8)) & 0xff; |
1002 |
uint64_t offset = phys & 0xffffffffffff;
|
1003 |
|
1004 |
switch (group_id) {
|
1005 |
case MEMSLOT_GROUP_HOST:
|
1006 |
return (void*)offset; |
1007 |
case MEMSLOT_GROUP_GUEST:
|
1008 |
PANIC_ON(slot > NUM_MEMSLOTS); |
1009 |
PANIC_ON(!qxl->guest_slots[slot].active); |
1010 |
PANIC_ON(offset < qxl->guest_slots[slot].delta); |
1011 |
offset -= qxl->guest_slots[slot].delta; |
1012 |
PANIC_ON(offset > qxl->guest_slots[slot].size) |
1013 |
return qxl->guest_slots[slot].ptr + offset;
|
1014 |
default:
|
1015 |
PANIC_ON(1);
|
1016 |
} |
1017 |
} |
1018 |
|
1019 |
static void qxl_create_guest_primary_complete(PCIQXLDevice *qxl) |
1020 |
{ |
1021 |
/* for local rendering */
|
1022 |
qxl_render_resize(qxl); |
1023 |
} |
1024 |
|
1025 |
static void qxl_create_guest_primary(PCIQXLDevice *qxl, int loadvm, |
1026 |
qxl_async_io async) |
1027 |
{ |
1028 |
QXLDevSurfaceCreate surface; |
1029 |
QXLSurfaceCreate *sc = &qxl->guest_primary.surface; |
1030 |
|
1031 |
assert(qxl->mode != QXL_MODE_NATIVE); |
1032 |
qxl_exit_vga_mode(qxl); |
1033 |
|
1034 |
dprint(qxl, 1, "%s: %dx%d\n", __FUNCTION__, |
1035 |
le32_to_cpu(sc->width), le32_to_cpu(sc->height)); |
1036 |
|
1037 |
surface.format = le32_to_cpu(sc->format); |
1038 |
surface.height = le32_to_cpu(sc->height); |
1039 |
surface.mem = le64_to_cpu(sc->mem); |
1040 |
surface.position = le32_to_cpu(sc->position); |
1041 |
surface.stride = le32_to_cpu(sc->stride); |
1042 |
surface.width = le32_to_cpu(sc->width); |
1043 |
surface.type = le32_to_cpu(sc->type); |
1044 |
surface.flags = le32_to_cpu(sc->flags); |
1045 |
|
1046 |
surface.mouse_mode = true;
|
1047 |
surface.group_id = MEMSLOT_GROUP_GUEST; |
1048 |
if (loadvm) {
|
1049 |
surface.flags |= QXL_SURF_FLAG_KEEP_DATA; |
1050 |
} |
1051 |
|
1052 |
qxl->mode = QXL_MODE_NATIVE; |
1053 |
qxl->cmdflags = 0;
|
1054 |
qemu_spice_create_primary_surface(&qxl->ssd, 0, &surface, async);
|
1055 |
|
1056 |
if (async == QXL_SYNC) {
|
1057 |
qxl_create_guest_primary_complete(qxl); |
1058 |
} |
1059 |
} |
1060 |
|
1061 |
/* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
|
1062 |
* done (in QXL_SYNC case), 0 otherwise. */
|
1063 |
static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async) |
1064 |
{ |
1065 |
if (d->mode == QXL_MODE_UNDEFINED) {
|
1066 |
return 0; |
1067 |
} |
1068 |
|
1069 |
dprint(d, 1, "%s\n", __FUNCTION__); |
1070 |
|
1071 |
d->mode = QXL_MODE_UNDEFINED; |
1072 |
qemu_spice_destroy_primary_surface(&d->ssd, 0, async);
|
1073 |
qxl_spice_reset_cursor(d); |
1074 |
return 1; |
1075 |
} |
1076 |
|
1077 |
static void qxl_set_mode(PCIQXLDevice *d, int modenr, int loadvm) |
1078 |
{ |
1079 |
pcibus_t start = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr; |
1080 |
pcibus_t end = d->pci.io_regions[QXL_RAM_RANGE_INDEX].size + start; |
1081 |
QXLMode *mode = d->modes->modes + modenr; |
1082 |
uint64_t devmem = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr; |
1083 |
QXLMemSlot slot = { |
1084 |
.mem_start = start, |
1085 |
.mem_end = end |
1086 |
}; |
1087 |
QXLSurfaceCreate surface = { |
1088 |
.width = mode->x_res, |
1089 |
.height = mode->y_res, |
1090 |
.stride = -mode->x_res * 4,
|
1091 |
.format = SPICE_SURFACE_FMT_32_xRGB, |
1092 |
.flags = loadvm ? QXL_SURF_FLAG_KEEP_DATA : 0,
|
1093 |
.mouse_mode = true,
|
1094 |
.mem = devmem + d->shadow_rom.draw_area_offset, |
1095 |
}; |
1096 |
|
1097 |
dprint(d, 1, "%s: mode %d [ %d x %d @ %d bpp devmem 0x%" PRIx64 " ]\n", |
1098 |
__func__, modenr, mode->x_res, mode->y_res, mode->bits, devmem); |
1099 |
if (!loadvm) {
|
1100 |
qxl_hard_reset(d, 0);
|
1101 |
} |
1102 |
|
1103 |
d->guest_slots[0].slot = slot;
|
1104 |
qxl_add_memslot(d, 0, devmem, QXL_SYNC);
|
1105 |
|
1106 |
d->guest_primary.surface = surface; |
1107 |
qxl_create_guest_primary(d, 0, QXL_SYNC);
|
1108 |
|
1109 |
d->mode = QXL_MODE_COMPAT; |
1110 |
d->cmdflags = QXL_COMMAND_FLAG_COMPAT; |
1111 |
#ifdef QXL_COMMAND_FLAG_COMPAT_16BPP /* new in spice 0.6.1 */ |
1112 |
if (mode->bits == 16) { |
1113 |
d->cmdflags |= QXL_COMMAND_FLAG_COMPAT_16BPP; |
1114 |
} |
1115 |
#endif
|
1116 |
d->shadow_rom.mode = cpu_to_le32(modenr); |
1117 |
d->rom->mode = cpu_to_le32(modenr); |
1118 |
qxl_rom_set_dirty(d); |
1119 |
} |
1120 |
|
1121 |
static void ioport_write(void *opaque, target_phys_addr_t addr, |
1122 |
uint64_t val, unsigned size)
|
1123 |
{ |
1124 |
PCIQXLDevice *d = opaque; |
1125 |
uint32_t io_port = addr; |
1126 |
qxl_async_io async = QXL_SYNC; |
1127 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1128 |
uint32_t orig_io_port = io_port; |
1129 |
#endif
|
1130 |
|
1131 |
switch (io_port) {
|
1132 |
case QXL_IO_RESET:
|
1133 |
case QXL_IO_SET_MODE:
|
1134 |
case QXL_IO_MEMSLOT_ADD:
|
1135 |
case QXL_IO_MEMSLOT_DEL:
|
1136 |
case QXL_IO_CREATE_PRIMARY:
|
1137 |
case QXL_IO_UPDATE_IRQ:
|
1138 |
case QXL_IO_LOG:
|
1139 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1140 |
case QXL_IO_MEMSLOT_ADD_ASYNC:
|
1141 |
case QXL_IO_CREATE_PRIMARY_ASYNC:
|
1142 |
#endif
|
1143 |
break;
|
1144 |
default:
|
1145 |
if (d->mode != QXL_MODE_VGA) {
|
1146 |
break;
|
1147 |
} |
1148 |
dprint(d, 1, "%s: unexpected port 0x%x (%s) in vga mode\n", |
1149 |
__func__, io_port, io_port_to_string(io_port)); |
1150 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1151 |
/* be nice to buggy guest drivers */
|
1152 |
if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&
|
1153 |
io_port <= QXL_IO_DESTROY_ALL_SURFACES_ASYNC) { |
1154 |
qxl_send_events(d, QXL_INTERRUPT_IO_CMD); |
1155 |
} |
1156 |
#endif
|
1157 |
return;
|
1158 |
} |
1159 |
|
1160 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1161 |
/* we change the io_port to avoid ifdeffery in the main switch */
|
1162 |
orig_io_port = io_port; |
1163 |
switch (io_port) {
|
1164 |
case QXL_IO_UPDATE_AREA_ASYNC:
|
1165 |
io_port = QXL_IO_UPDATE_AREA; |
1166 |
goto async_common;
|
1167 |
case QXL_IO_MEMSLOT_ADD_ASYNC:
|
1168 |
io_port = QXL_IO_MEMSLOT_ADD; |
1169 |
goto async_common;
|
1170 |
case QXL_IO_CREATE_PRIMARY_ASYNC:
|
1171 |
io_port = QXL_IO_CREATE_PRIMARY; |
1172 |
goto async_common;
|
1173 |
case QXL_IO_DESTROY_PRIMARY_ASYNC:
|
1174 |
io_port = QXL_IO_DESTROY_PRIMARY; |
1175 |
goto async_common;
|
1176 |
case QXL_IO_DESTROY_SURFACE_ASYNC:
|
1177 |
io_port = QXL_IO_DESTROY_SURFACE_WAIT; |
1178 |
goto async_common;
|
1179 |
case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
|
1180 |
io_port = QXL_IO_DESTROY_ALL_SURFACES; |
1181 |
goto async_common;
|
1182 |
case QXL_IO_FLUSH_SURFACES_ASYNC:
|
1183 |
async_common:
|
1184 |
async = QXL_ASYNC; |
1185 |
qemu_mutex_lock(&d->async_lock); |
1186 |
if (d->current_async != QXL_UNDEFINED_IO) {
|
1187 |
qxl_guest_bug(d, "%d async started before last (%d) complete",
|
1188 |
io_port, d->current_async); |
1189 |
qemu_mutex_unlock(&d->async_lock); |
1190 |
return;
|
1191 |
} |
1192 |
d->current_async = orig_io_port; |
1193 |
qemu_mutex_unlock(&d->async_lock); |
1194 |
dprint(d, 2, "start async %d (%"PRId64")\n", io_port, val); |
1195 |
break;
|
1196 |
default:
|
1197 |
break;
|
1198 |
} |
1199 |
#endif
|
1200 |
|
1201 |
switch (io_port) {
|
1202 |
case QXL_IO_UPDATE_AREA:
|
1203 |
{ |
1204 |
QXLRect update = d->ram->update_area; |
1205 |
qxl_spice_update_area(d, d->ram->update_surface, |
1206 |
&update, NULL, 0, 0, async); |
1207 |
break;
|
1208 |
} |
1209 |
case QXL_IO_NOTIFY_CMD:
|
1210 |
qemu_spice_wakeup(&d->ssd); |
1211 |
break;
|
1212 |
case QXL_IO_NOTIFY_CURSOR:
|
1213 |
qemu_spice_wakeup(&d->ssd); |
1214 |
break;
|
1215 |
case QXL_IO_UPDATE_IRQ:
|
1216 |
qxl_update_irq(d); |
1217 |
break;
|
1218 |
case QXL_IO_NOTIFY_OOM:
|
1219 |
if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
|
1220 |
break;
|
1221 |
} |
1222 |
d->oom_running = 1;
|
1223 |
qxl_spice_oom(d); |
1224 |
d->oom_running = 0;
|
1225 |
break;
|
1226 |
case QXL_IO_SET_MODE:
|
1227 |
dprint(d, 1, "QXL_SET_MODE %d\n", (int)val); |
1228 |
qxl_set_mode(d, val, 0);
|
1229 |
break;
|
1230 |
case QXL_IO_LOG:
|
1231 |
if (d->guestdebug) {
|
1232 |
fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id, |
1233 |
qemu_get_clock_ns(vm_clock), d->ram->log_buf); |
1234 |
} |
1235 |
break;
|
1236 |
case QXL_IO_RESET:
|
1237 |
dprint(d, 1, "QXL_IO_RESET\n"); |
1238 |
qxl_hard_reset(d, 0);
|
1239 |
break;
|
1240 |
case QXL_IO_MEMSLOT_ADD:
|
1241 |
if (val >= NUM_MEMSLOTS) {
|
1242 |
qxl_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
|
1243 |
break;
|
1244 |
} |
1245 |
if (d->guest_slots[val].active) {
|
1246 |
qxl_guest_bug(d, "QXL_IO_MEMSLOT_ADD: memory slot already active");
|
1247 |
break;
|
1248 |
} |
1249 |
d->guest_slots[val].slot = d->ram->mem_slot; |
1250 |
qxl_add_memslot(d, val, 0, async);
|
1251 |
break;
|
1252 |
case QXL_IO_MEMSLOT_DEL:
|
1253 |
if (val >= NUM_MEMSLOTS) {
|
1254 |
qxl_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
|
1255 |
break;
|
1256 |
} |
1257 |
qxl_del_memslot(d, val); |
1258 |
break;
|
1259 |
case QXL_IO_CREATE_PRIMARY:
|
1260 |
if (val != 0) { |
1261 |
qxl_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
|
1262 |
async); |
1263 |
goto cancel_async;
|
1264 |
} |
1265 |
dprint(d, 1, "QXL_IO_CREATE_PRIMARY async=%d\n", async); |
1266 |
d->guest_primary.surface = d->ram->create_surface; |
1267 |
qxl_create_guest_primary(d, 0, async);
|
1268 |
break;
|
1269 |
case QXL_IO_DESTROY_PRIMARY:
|
1270 |
if (val != 0) { |
1271 |
qxl_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
|
1272 |
async); |
1273 |
goto cancel_async;
|
1274 |
} |
1275 |
dprint(d, 1, "QXL_IO_DESTROY_PRIMARY (async=%d) (%s)\n", async, |
1276 |
qxl_mode_to_string(d->mode)); |
1277 |
if (!qxl_destroy_primary(d, async)) {
|
1278 |
dprint(d, 1, "QXL_IO_DESTROY_PRIMARY_ASYNC in %s, ignored\n", |
1279 |
qxl_mode_to_string(d->mode)); |
1280 |
goto cancel_async;
|
1281 |
} |
1282 |
break;
|
1283 |
case QXL_IO_DESTROY_SURFACE_WAIT:
|
1284 |
if (val >= NUM_SURFACES) {
|
1285 |
qxl_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
|
1286 |
"%d >= NUM_SURFACES", async, val);
|
1287 |
goto cancel_async;
|
1288 |
} |
1289 |
qxl_spice_destroy_surface_wait(d, val, async); |
1290 |
break;
|
1291 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1292 |
case QXL_IO_FLUSH_RELEASE: {
|
1293 |
QXLReleaseRing *ring = &d->ram->release_ring; |
1294 |
if (ring->prod - ring->cons + 1 == ring->num_items) { |
1295 |
fprintf(stderr, |
1296 |
"ERROR: no flush, full release ring [p%d,%dc]\n",
|
1297 |
ring->prod, ring->cons); |
1298 |
} |
1299 |
qxl_push_free_res(d, 1 /* flush */); |
1300 |
dprint(d, 1, "QXL_IO_FLUSH_RELEASE exit (%s, s#=%d, res#=%d,%p)\n", |
1301 |
qxl_mode_to_string(d->mode), d->guest_surfaces.count, |
1302 |
d->num_free_res, d->last_release); |
1303 |
break;
|
1304 |
} |
1305 |
case QXL_IO_FLUSH_SURFACES_ASYNC:
|
1306 |
dprint(d, 1, "QXL_IO_FLUSH_SURFACES_ASYNC" |
1307 |
" (%"PRId64") (%s, s#=%d, res#=%d)\n", |
1308 |
val, qxl_mode_to_string(d->mode), d->guest_surfaces.count, |
1309 |
d->num_free_res); |
1310 |
qxl_spice_flush_surfaces_async(d); |
1311 |
break;
|
1312 |
#endif
|
1313 |
case QXL_IO_DESTROY_ALL_SURFACES:
|
1314 |
d->mode = QXL_MODE_UNDEFINED; |
1315 |
qxl_spice_destroy_surfaces(d, async); |
1316 |
break;
|
1317 |
default:
|
1318 |
fprintf(stderr, "%s: ioport=0x%x, abort()\n", __FUNCTION__, io_port);
|
1319 |
abort(); |
1320 |
} |
1321 |
return;
|
1322 |
cancel_async:
|
1323 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1324 |
if (async) {
|
1325 |
qxl_send_events(d, QXL_INTERRUPT_IO_CMD); |
1326 |
qemu_mutex_lock(&d->async_lock); |
1327 |
d->current_async = QXL_UNDEFINED_IO; |
1328 |
qemu_mutex_unlock(&d->async_lock); |
1329 |
} |
1330 |
#else
|
1331 |
return;
|
1332 |
#endif
|
1333 |
} |
1334 |
|
1335 |
static uint64_t ioport_read(void *opaque, target_phys_addr_t addr, |
1336 |
unsigned size)
|
1337 |
{ |
1338 |
PCIQXLDevice *d = opaque; |
1339 |
|
1340 |
dprint(d, 1, "%s: unexpected\n", __FUNCTION__); |
1341 |
return 0xff; |
1342 |
} |
1343 |
|
1344 |
static const MemoryRegionOps qxl_io_ops = { |
1345 |
.read = ioport_read, |
1346 |
.write = ioport_write, |
1347 |
.valid = { |
1348 |
.min_access_size = 1,
|
1349 |
.max_access_size = 1,
|
1350 |
}, |
1351 |
}; |
1352 |
|
1353 |
static void pipe_read(void *opaque) |
1354 |
{ |
1355 |
PCIQXLDevice *d = opaque; |
1356 |
char dummy;
|
1357 |
int len;
|
1358 |
|
1359 |
do {
|
1360 |
len = read(d->pipe[0], &dummy, sizeof(dummy)); |
1361 |
} while (len == sizeof(dummy)); |
1362 |
qxl_update_irq(d); |
1363 |
} |
1364 |
|
1365 |
static void qxl_send_events(PCIQXLDevice *d, uint32_t events) |
1366 |
{ |
1367 |
uint32_t old_pending; |
1368 |
uint32_t le_events = cpu_to_le32(events); |
1369 |
|
1370 |
assert(d->ssd.running); |
1371 |
old_pending = __sync_fetch_and_or(&d->ram->int_pending, le_events); |
1372 |
if ((old_pending & le_events) == le_events) {
|
1373 |
return;
|
1374 |
} |
1375 |
if (qemu_thread_is_self(&d->main)) {
|
1376 |
qxl_update_irq(d); |
1377 |
} else {
|
1378 |
if (write(d->pipe[1], d, 1) != 1) { |
1379 |
dprint(d, 1, "%s: write to pipe failed\n", __FUNCTION__); |
1380 |
} |
1381 |
} |
1382 |
} |
1383 |
|
1384 |
static void init_pipe_signaling(PCIQXLDevice *d) |
1385 |
{ |
1386 |
if (pipe(d->pipe) < 0) { |
1387 |
dprint(d, 1, "%s: pipe creation failed\n", __FUNCTION__); |
1388 |
return;
|
1389 |
} |
1390 |
fcntl(d->pipe[0], F_SETFL, O_NONBLOCK);
|
1391 |
fcntl(d->pipe[1], F_SETFL, O_NONBLOCK);
|
1392 |
fcntl(d->pipe[0], F_SETOWN, getpid());
|
1393 |
|
1394 |
qemu_thread_get_self(&d->main); |
1395 |
qemu_set_fd_handler(d->pipe[0], pipe_read, NULL, d); |
1396 |
} |
1397 |
|
1398 |
/* graphics console */
|
1399 |
|
1400 |
static void qxl_hw_update(void *opaque) |
1401 |
{ |
1402 |
PCIQXLDevice *qxl = opaque; |
1403 |
VGACommonState *vga = &qxl->vga; |
1404 |
|
1405 |
switch (qxl->mode) {
|
1406 |
case QXL_MODE_VGA:
|
1407 |
vga->update(vga); |
1408 |
break;
|
1409 |
case QXL_MODE_COMPAT:
|
1410 |
case QXL_MODE_NATIVE:
|
1411 |
qxl_render_update(qxl); |
1412 |
break;
|
1413 |
default:
|
1414 |
break;
|
1415 |
} |
1416 |
} |
1417 |
|
1418 |
static void qxl_hw_invalidate(void *opaque) |
1419 |
{ |
1420 |
PCIQXLDevice *qxl = opaque; |
1421 |
VGACommonState *vga = &qxl->vga; |
1422 |
|
1423 |
vga->invalidate(vga); |
1424 |
} |
1425 |
|
1426 |
static void qxl_hw_screen_dump(void *opaque, const char *filename) |
1427 |
{ |
1428 |
PCIQXLDevice *qxl = opaque; |
1429 |
VGACommonState *vga = &qxl->vga; |
1430 |
|
1431 |
switch (qxl->mode) {
|
1432 |
case QXL_MODE_COMPAT:
|
1433 |
case QXL_MODE_NATIVE:
|
1434 |
qxl_render_update(qxl); |
1435 |
ppm_save(filename, qxl->ssd.ds->surface); |
1436 |
break;
|
1437 |
case QXL_MODE_VGA:
|
1438 |
vga->screen_dump(vga, filename); |
1439 |
break;
|
1440 |
default:
|
1441 |
break;
|
1442 |
} |
1443 |
} |
1444 |
|
1445 |
static void qxl_hw_text_update(void *opaque, console_ch_t *chardata) |
1446 |
{ |
1447 |
PCIQXLDevice *qxl = opaque; |
1448 |
VGACommonState *vga = &qxl->vga; |
1449 |
|
1450 |
if (qxl->mode == QXL_MODE_VGA) {
|
1451 |
vga->text_update(vga, chardata); |
1452 |
return;
|
1453 |
} |
1454 |
} |
1455 |
|
1456 |
static void qxl_vm_change_state_handler(void *opaque, int running, |
1457 |
RunState state) |
1458 |
{ |
1459 |
PCIQXLDevice *qxl = opaque; |
1460 |
qemu_spice_vm_change_state_handler(&qxl->ssd, running, state); |
1461 |
|
1462 |
if (running) {
|
1463 |
/*
|
1464 |
* if qxl_send_events was called from spice server context before
|
1465 |
* migration ended, qxl_update_irq for these events might not have been
|
1466 |
* called
|
1467 |
*/
|
1468 |
qxl_update_irq(qxl); |
1469 |
} else if (qxl->mode == QXL_MODE_NATIVE) { |
1470 |
/* dirty all vram (which holds surfaces) and devram (primary surface)
|
1471 |
* to make sure they are saved */
|
1472 |
/* FIXME #1: should go out during "live" stage */
|
1473 |
/* FIXME #2: we only need to save the areas which are actually used */
|
1474 |
qxl_set_dirty(&qxl->vram_bar, 0, qxl->vram_size);
|
1475 |
qxl_set_dirty(&qxl->vga.vram, qxl->shadow_rom.draw_area_offset, |
1476 |
qxl->shadow_rom.surface0_area_size); |
1477 |
} |
1478 |
} |
1479 |
|
1480 |
/* display change listener */
|
1481 |
|
1482 |
static void display_update(struct DisplayState *ds, int x, int y, int w, int h) |
1483 |
{ |
1484 |
if (qxl0->mode == QXL_MODE_VGA) {
|
1485 |
qemu_spice_display_update(&qxl0->ssd, x, y, w, h); |
1486 |
} |
1487 |
} |
1488 |
|
1489 |
static void display_resize(struct DisplayState *ds) |
1490 |
{ |
1491 |
if (qxl0->mode == QXL_MODE_VGA) {
|
1492 |
qemu_spice_display_resize(&qxl0->ssd); |
1493 |
} |
1494 |
} |
1495 |
|
1496 |
static void display_refresh(struct DisplayState *ds) |
1497 |
{ |
1498 |
if (qxl0->mode == QXL_MODE_VGA) {
|
1499 |
qemu_spice_display_refresh(&qxl0->ssd); |
1500 |
} |
1501 |
} |
1502 |
|
1503 |
static DisplayChangeListener display_listener = {
|
1504 |
.dpy_update = display_update, |
1505 |
.dpy_resize = display_resize, |
1506 |
.dpy_refresh = display_refresh, |
1507 |
}; |
1508 |
|
1509 |
static int qxl_init_common(PCIQXLDevice *qxl) |
1510 |
{ |
1511 |
uint8_t* config = qxl->pci.config; |
1512 |
uint32_t pci_device_rev; |
1513 |
uint32_t io_size; |
1514 |
|
1515 |
qxl->mode = QXL_MODE_UNDEFINED; |
1516 |
qxl->generation = 1;
|
1517 |
qxl->num_memslots = NUM_MEMSLOTS; |
1518 |
qxl->num_surfaces = NUM_SURFACES; |
1519 |
qemu_mutex_init(&qxl->track_lock); |
1520 |
qemu_mutex_init(&qxl->async_lock); |
1521 |
qxl->current_async = QXL_UNDEFINED_IO; |
1522 |
|
1523 |
switch (qxl->revision) {
|
1524 |
case 1: /* spice 0.4 -- qxl-1 */ |
1525 |
pci_device_rev = QXL_REVISION_STABLE_V04; |
1526 |
break;
|
1527 |
case 2: /* spice 0.6 -- qxl-2 */ |
1528 |
pci_device_rev = QXL_REVISION_STABLE_V06; |
1529 |
break;
|
1530 |
#if SPICE_INTERFACE_QXL_MINOR >= 1 |
1531 |
case 3: /* qxl-3 */ |
1532 |
#endif
|
1533 |
default:
|
1534 |
pci_device_rev = QXL_DEFAULT_REVISION; |
1535 |
break;
|
1536 |
} |
1537 |
|
1538 |
pci_set_byte(&config[PCI_REVISION_ID], pci_device_rev); |
1539 |
pci_set_byte(&config[PCI_INTERRUPT_PIN], 1);
|
1540 |
|
1541 |
qxl->rom_size = qxl_rom_size(); |
1542 |
memory_region_init_ram(&qxl->rom_bar, &qxl->pci.qdev, "qxl.vrom",
|
1543 |
qxl->rom_size); |
1544 |
init_qxl_rom(qxl); |
1545 |
init_qxl_ram(qxl); |
1546 |
|
1547 |
if (qxl->vram_size < 16 * 1024 * 1024) { |
1548 |
qxl->vram_size = 16 * 1024 * 1024; |
1549 |
} |
1550 |
if (qxl->revision == 1) { |
1551 |
qxl->vram_size = 4096;
|
1552 |
} |
1553 |
qxl->vram_size = msb_mask(qxl->vram_size * 2 - 1); |
1554 |
memory_region_init_ram(&qxl->vram_bar, &qxl->pci.qdev, "qxl.vram",
|
1555 |
qxl->vram_size); |
1556 |
|
1557 |
io_size = msb_mask(QXL_IO_RANGE_SIZE * 2 - 1); |
1558 |
if (qxl->revision == 1) { |
1559 |
io_size = 8;
|
1560 |
} |
1561 |
|
1562 |
memory_region_init_io(&qxl->io_bar, &qxl_io_ops, qxl, |
1563 |
"qxl-ioports", io_size);
|
1564 |
if (qxl->id == 0) { |
1565 |
vga_dirty_log_start(&qxl->vga); |
1566 |
} |
1567 |
|
1568 |
|
1569 |
pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX, |
1570 |
PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar); |
1571 |
|
1572 |
pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX, |
1573 |
PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar); |
1574 |
|
1575 |
pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX, |
1576 |
PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram); |
1577 |
|
1578 |
pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX, |
1579 |
PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vram_bar); |
1580 |
|
1581 |
qxl->ssd.qxl.base.sif = &qxl_interface.base; |
1582 |
qxl->ssd.qxl.id = qxl->id; |
1583 |
qemu_spice_add_interface(&qxl->ssd.qxl.base); |
1584 |
qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl); |
1585 |
|
1586 |
init_pipe_signaling(qxl); |
1587 |
qxl_reset_state(qxl); |
1588 |
|
1589 |
return 0; |
1590 |
} |
1591 |
|
1592 |
static int qxl_init_primary(PCIDevice *dev) |
1593 |
{ |
1594 |
PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev); |
1595 |
VGACommonState *vga = &qxl->vga; |
1596 |
ram_addr_t ram_size = msb_mask(qxl->vga.vram_size * 2 - 1); |
1597 |
|
1598 |
qxl->id = 0;
|
1599 |
|
1600 |
if (ram_size < 32 * 1024 * 1024) { |
1601 |
ram_size = 32 * 1024 * 1024; |
1602 |
} |
1603 |
vga_common_init(vga, ram_size); |
1604 |
vga_init(vga, pci_address_space(dev), pci_address_space_io(dev), false);
|
1605 |
register_ioport_write(0x3c0, 16, 1, qxl_vga_ioport_write, vga); |
1606 |
register_ioport_write(0x3b4, 2, 1, qxl_vga_ioport_write, vga); |
1607 |
register_ioport_write(0x3d4, 2, 1, qxl_vga_ioport_write, vga); |
1608 |
register_ioport_write(0x3ba, 1, 1, qxl_vga_ioport_write, vga); |
1609 |
register_ioport_write(0x3da, 1, 1, qxl_vga_ioport_write, vga); |
1610 |
|
1611 |
vga->ds = graphic_console_init(qxl_hw_update, qxl_hw_invalidate, |
1612 |
qxl_hw_screen_dump, qxl_hw_text_update, qxl); |
1613 |
qemu_spice_display_init_common(&qxl->ssd, vga->ds); |
1614 |
|
1615 |
qxl0 = qxl; |
1616 |
register_displaychangelistener(vga->ds, &display_listener); |
1617 |
|
1618 |
return qxl_init_common(qxl);
|
1619 |
} |
1620 |
|
1621 |
static int qxl_init_secondary(PCIDevice *dev) |
1622 |
{ |
1623 |
static int device_id = 1; |
1624 |
PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev); |
1625 |
ram_addr_t ram_size = msb_mask(qxl->vga.vram_size * 2 - 1); |
1626 |
|
1627 |
qxl->id = device_id++; |
1628 |
|
1629 |
if (ram_size < 16 * 1024 * 1024) { |
1630 |
ram_size = 16 * 1024 * 1024; |
1631 |
} |
1632 |
qxl->vga.vram_size = ram_size; |
1633 |
memory_region_init_ram(&qxl->vga.vram, &qxl->pci.qdev, "qxl.vgavram",
|
1634 |
qxl->vga.vram_size); |
1635 |
qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram); |
1636 |
|
1637 |
return qxl_init_common(qxl);
|
1638 |
} |
1639 |
|
1640 |
static void qxl_pre_save(void *opaque) |
1641 |
{ |
1642 |
PCIQXLDevice* d = opaque; |
1643 |
uint8_t *ram_start = d->vga.vram_ptr; |
1644 |
|
1645 |
dprint(d, 1, "%s:\n", __FUNCTION__); |
1646 |
if (d->last_release == NULL) { |
1647 |
d->last_release_offset = 0;
|
1648 |
} else {
|
1649 |
d->last_release_offset = (uint8_t *)d->last_release - ram_start; |
1650 |
} |
1651 |
assert(d->last_release_offset < d->vga.vram_size); |
1652 |
} |
1653 |
|
1654 |
static int qxl_pre_load(void *opaque) |
1655 |
{ |
1656 |
PCIQXLDevice* d = opaque; |
1657 |
|
1658 |
dprint(d, 1, "%s: start\n", __FUNCTION__); |
1659 |
qxl_hard_reset(d, 1);
|
1660 |
qxl_exit_vga_mode(d); |
1661 |
dprint(d, 1, "%s: done\n", __FUNCTION__); |
1662 |
return 0; |
1663 |
} |
1664 |
|
1665 |
static int qxl_post_load(void *opaque, int version) |
1666 |
{ |
1667 |
PCIQXLDevice* d = opaque; |
1668 |
uint8_t *ram_start = d->vga.vram_ptr; |
1669 |
QXLCommandExt *cmds; |
1670 |
int in, out, i, newmode;
|
1671 |
|
1672 |
dprint(d, 1, "%s: start\n", __FUNCTION__); |
1673 |
|
1674 |
assert(d->last_release_offset < d->vga.vram_size); |
1675 |
if (d->last_release_offset == 0) { |
1676 |
d->last_release = NULL;
|
1677 |
} else {
|
1678 |
d->last_release = (QXLReleaseInfo *)(ram_start + d->last_release_offset); |
1679 |
} |
1680 |
|
1681 |
d->modes = (QXLModes*)((uint8_t*)d->rom + d->rom->modes_offset); |
1682 |
|
1683 |
dprint(d, 1, "%s: restore mode (%s)\n", __FUNCTION__, |
1684 |
qxl_mode_to_string(d->mode)); |
1685 |
newmode = d->mode; |
1686 |
d->mode = QXL_MODE_UNDEFINED; |
1687 |
switch (newmode) {
|
1688 |
case QXL_MODE_UNDEFINED:
|
1689 |
break;
|
1690 |
case QXL_MODE_VGA:
|
1691 |
qxl_enter_vga_mode(d); |
1692 |
break;
|
1693 |
case QXL_MODE_NATIVE:
|
1694 |
for (i = 0; i < NUM_MEMSLOTS; i++) { |
1695 |
if (!d->guest_slots[i].active) {
|
1696 |
continue;
|
1697 |
} |
1698 |
qxl_add_memslot(d, i, 0, QXL_SYNC);
|
1699 |
} |
1700 |
qxl_create_guest_primary(d, 1, QXL_SYNC);
|
1701 |
|
1702 |
/* replay surface-create and cursor-set commands */
|
1703 |
cmds = g_malloc0(sizeof(QXLCommandExt) * (NUM_SURFACES + 1)); |
1704 |
for (in = 0, out = 0; in < NUM_SURFACES; in++) { |
1705 |
if (d->guest_surfaces.cmds[in] == 0) { |
1706 |
continue;
|
1707 |
} |
1708 |
cmds[out].cmd.data = d->guest_surfaces.cmds[in]; |
1709 |
cmds[out].cmd.type = QXL_CMD_SURFACE; |
1710 |
cmds[out].group_id = MEMSLOT_GROUP_GUEST; |
1711 |
out++; |
1712 |
} |
1713 |
if (d->guest_cursor) {
|
1714 |
cmds[out].cmd.data = d->guest_cursor; |
1715 |
cmds[out].cmd.type = QXL_CMD_CURSOR; |
1716 |
cmds[out].group_id = MEMSLOT_GROUP_GUEST; |
1717 |
out++; |
1718 |
} |
1719 |
qxl_spice_loadvm_commands(d, cmds, out); |
1720 |
g_free(cmds); |
1721 |
|
1722 |
break;
|
1723 |
case QXL_MODE_COMPAT:
|
1724 |
qxl_set_mode(d, d->shadow_rom.mode, 1);
|
1725 |
break;
|
1726 |
} |
1727 |
dprint(d, 1, "%s: done\n", __FUNCTION__); |
1728 |
|
1729 |
return 0; |
1730 |
} |
1731 |
|
1732 |
#define QXL_SAVE_VERSION 21 |
1733 |
|
1734 |
static VMStateDescription qxl_memslot = {
|
1735 |
.name = "qxl-memslot",
|
1736 |
.version_id = QXL_SAVE_VERSION, |
1737 |
.minimum_version_id = QXL_SAVE_VERSION, |
1738 |
.fields = (VMStateField[]) { |
1739 |
VMSTATE_UINT64(slot.mem_start, struct guest_slots),
|
1740 |
VMSTATE_UINT64(slot.mem_end, struct guest_slots),
|
1741 |
VMSTATE_UINT32(active, struct guest_slots),
|
1742 |
VMSTATE_END_OF_LIST() |
1743 |
} |
1744 |
}; |
1745 |
|
1746 |
static VMStateDescription qxl_surface = {
|
1747 |
.name = "qxl-surface",
|
1748 |
.version_id = QXL_SAVE_VERSION, |
1749 |
.minimum_version_id = QXL_SAVE_VERSION, |
1750 |
.fields = (VMStateField[]) { |
1751 |
VMSTATE_UINT32(width, QXLSurfaceCreate), |
1752 |
VMSTATE_UINT32(height, QXLSurfaceCreate), |
1753 |
VMSTATE_INT32(stride, QXLSurfaceCreate), |
1754 |
VMSTATE_UINT32(format, QXLSurfaceCreate), |
1755 |
VMSTATE_UINT32(position, QXLSurfaceCreate), |
1756 |
VMSTATE_UINT32(mouse_mode, QXLSurfaceCreate), |
1757 |
VMSTATE_UINT32(flags, QXLSurfaceCreate), |
1758 |
VMSTATE_UINT32(type, QXLSurfaceCreate), |
1759 |
VMSTATE_UINT64(mem, QXLSurfaceCreate), |
1760 |
VMSTATE_END_OF_LIST() |
1761 |
} |
1762 |
}; |
1763 |
|
1764 |
static VMStateDescription qxl_vmstate = {
|
1765 |
.name = "qxl",
|
1766 |
.version_id = QXL_SAVE_VERSION, |
1767 |
.minimum_version_id = QXL_SAVE_VERSION, |
1768 |
.pre_save = qxl_pre_save, |
1769 |
.pre_load = qxl_pre_load, |
1770 |
.post_load = qxl_post_load, |
1771 |
.fields = (VMStateField []) { |
1772 |
VMSTATE_PCI_DEVICE(pci, PCIQXLDevice), |
1773 |
VMSTATE_STRUCT(vga, PCIQXLDevice, 0, vmstate_vga_common, VGACommonState),
|
1774 |
VMSTATE_UINT32(shadow_rom.mode, PCIQXLDevice), |
1775 |
VMSTATE_UINT32(num_free_res, PCIQXLDevice), |
1776 |
VMSTATE_UINT32(last_release_offset, PCIQXLDevice), |
1777 |
VMSTATE_UINT32(mode, PCIQXLDevice), |
1778 |
VMSTATE_UINT32(ssd.unique, PCIQXLDevice), |
1779 |
VMSTATE_INT32_EQUAL(num_memslots, PCIQXLDevice), |
1780 |
VMSTATE_STRUCT_ARRAY(guest_slots, PCIQXLDevice, NUM_MEMSLOTS, 0,
|
1781 |
qxl_memslot, struct guest_slots),
|
1782 |
VMSTATE_STRUCT(guest_primary.surface, PCIQXLDevice, 0,
|
1783 |
qxl_surface, QXLSurfaceCreate), |
1784 |
VMSTATE_INT32_EQUAL(num_surfaces, PCIQXLDevice), |
1785 |
VMSTATE_ARRAY(guest_surfaces.cmds, PCIQXLDevice, NUM_SURFACES, 0,
|
1786 |
vmstate_info_uint64, uint64_t), |
1787 |
VMSTATE_UINT64(guest_cursor, PCIQXLDevice), |
1788 |
VMSTATE_END_OF_LIST() |
1789 |
}, |
1790 |
}; |
1791 |
|
1792 |
static Property qxl_properties[] = {
|
1793 |
DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size,
|
1794 |
64 * 1024 * 1024), |
1795 |
DEFINE_PROP_UINT32("vram_size", PCIQXLDevice, vram_size,
|
1796 |
64 * 1024 * 1024), |
1797 |
DEFINE_PROP_UINT32("revision", PCIQXLDevice, revision,
|
1798 |
QXL_DEFAULT_REVISION), |
1799 |
DEFINE_PROP_UINT32("debug", PCIQXLDevice, debug, 0), |
1800 |
DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice, guestdebug, 0), |
1801 |
DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice, cmdlog, 0), |
1802 |
DEFINE_PROP_END_OF_LIST(), |
1803 |
}; |
1804 |
|
1805 |
static PCIDeviceInfo qxl_info_primary = {
|
1806 |
.qdev.name = "qxl-vga",
|
1807 |
.qdev.desc = "Spice QXL GPU (primary, vga compatible)",
|
1808 |
.qdev.size = sizeof(PCIQXLDevice),
|
1809 |
.qdev.reset = qxl_reset_handler, |
1810 |
.qdev.vmsd = &qxl_vmstate, |
1811 |
.no_hotplug = 1,
|
1812 |
.init = qxl_init_primary, |
1813 |
.romfile = "vgabios-qxl.bin",
|
1814 |
.vendor_id = REDHAT_PCI_VENDOR_ID, |
1815 |
.device_id = QXL_DEVICE_ID_STABLE, |
1816 |
.class_id = PCI_CLASS_DISPLAY_VGA, |
1817 |
.qdev.props = qxl_properties, |
1818 |
}; |
1819 |
|
1820 |
static PCIDeviceInfo qxl_info_secondary = {
|
1821 |
.qdev.name = "qxl",
|
1822 |
.qdev.desc = "Spice QXL GPU (secondary)",
|
1823 |
.qdev.size = sizeof(PCIQXLDevice),
|
1824 |
.qdev.reset = qxl_reset_handler, |
1825 |
.qdev.vmsd = &qxl_vmstate, |
1826 |
.init = qxl_init_secondary, |
1827 |
.vendor_id = REDHAT_PCI_VENDOR_ID, |
1828 |
.device_id = QXL_DEVICE_ID_STABLE, |
1829 |
.class_id = PCI_CLASS_DISPLAY_OTHER, |
1830 |
.qdev.props = qxl_properties, |
1831 |
}; |
1832 |
|
1833 |
static void qxl_register(void) |
1834 |
{ |
1835 |
pci_qdev_register(&qxl_info_primary); |
1836 |
pci_qdev_register(&qxl_info_secondary); |
1837 |
} |
1838 |
|
1839 |
device_init(qxl_register); |