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