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