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