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