qxl.c 76.7 KB
Newer Older
G
Gerd Hoffmann 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * written by Yaniv Kamay, Izik Eidus, Gerd Hoffmann
 * maintained by Gerd Hoffmann <kraxel@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 or
 * (at your option) version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

21
#include <zlib.h>
22
#include <stdint.h>
23

G
Gerd Hoffmann 已提交
24
#include "qemu-common.h"
25 26
#include "qemu/timer.h"
#include "qemu/queue.h"
27
#include "qemu/atomic.h"
28
#include "monitor/monitor.h"
29
#include "sysemu/sysemu.h"
A
Alon Levy 已提交
30
#include "trace.h"
G
Gerd Hoffmann 已提交
31

32
#include "qxl.h"
G
Gerd Hoffmann 已提交
33

34 35 36
/*
 * NOTE: SPICE_RING_PROD_ITEM accesses memory on the pci bar and as
 * such can be changed by the guest, so to avoid a guest trigerrable
37
 * abort we just qxl_set_guest_bug and set the return to NULL. Still
38 39
 * it may happen as a result of emulator bug as well.
 */
G
Gerd Hoffmann 已提交
40
#undef SPICE_RING_PROD_ITEM
41
#define SPICE_RING_PROD_ITEM(qxl, r, ret) {                             \
G
Gerd Hoffmann 已提交
42
        uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r);           \
43
        if (prod >= ARRAY_SIZE((r)->items)) {                           \
44
            qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
45
                          "%u >= %zu", prod, ARRAY_SIZE((r)->items));   \
46 47
            ret = NULL;                                                 \
        } else {                                                        \
48
            ret = &(r)->items[prod].el;                                 \
G
Gerd Hoffmann 已提交
49 50 51 52
        }                                                               \
    }

#undef SPICE_RING_CONS_ITEM
53
#define SPICE_RING_CONS_ITEM(qxl, r, ret) {                             \
G
Gerd Hoffmann 已提交
54
        uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r);           \
55
        if (cons >= ARRAY_SIZE((r)->items)) {                           \
56
            qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
57
                          "%u >= %zu", cons, ARRAY_SIZE((r)->items));   \
58 59
            ret = NULL;                                                 \
        } else {                                                        \
60
            ret = &(r)->items[cons].el;                                 \
G
Gerd Hoffmann 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        }                                                               \
    }

#undef ALIGN
#define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))

#define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9" 

#define QXL_MODE(_x, _y, _b, _o)                  \
    {   .x_res = _x,                              \
        .y_res = _y,                              \
        .bits  = _b,                              \
        .stride = (_x) * (_b) / 8,                \
        .x_mili = PIXEL_SIZE * (_x),              \
        .y_mili = PIXEL_SIZE * (_y),              \
        .orientation = _o,                        \
    }

#define QXL_MODE_16_32(x_res, y_res, orientation) \
    QXL_MODE(x_res, y_res, 16, orientation),      \
    QXL_MODE(x_res, y_res, 32, orientation)

#define QXL_MODE_EX(x_res, y_res)                 \
    QXL_MODE_16_32(x_res, y_res, 0),              \
A
Alon Levy 已提交
85
    QXL_MODE_16_32(x_res, y_res, 1)
G
Gerd Hoffmann 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

static QXLMode qxl_modes[] = {
    QXL_MODE_EX(640, 480),
    QXL_MODE_EX(800, 480),
    QXL_MODE_EX(800, 600),
    QXL_MODE_EX(832, 624),
    QXL_MODE_EX(960, 640),
    QXL_MODE_EX(1024, 600),
    QXL_MODE_EX(1024, 768),
    QXL_MODE_EX(1152, 864),
    QXL_MODE_EX(1152, 870),
    QXL_MODE_EX(1280, 720),
    QXL_MODE_EX(1280, 760),
    QXL_MODE_EX(1280, 768),
    QXL_MODE_EX(1280, 800),
    QXL_MODE_EX(1280, 960),
    QXL_MODE_EX(1280, 1024),
    QXL_MODE_EX(1360, 768),
    QXL_MODE_EX(1366, 768),
    QXL_MODE_EX(1400, 1050),
    QXL_MODE_EX(1440, 900),
    QXL_MODE_EX(1600, 900),
    QXL_MODE_EX(1600, 1200),
    QXL_MODE_EX(1680, 1050),
    QXL_MODE_EX(1920, 1080),
    /* these modes need more than 8 MB video memory */
    QXL_MODE_EX(1920, 1200),
    QXL_MODE_EX(1920, 1440),
114
    QXL_MODE_EX(2000, 2000),
G
Gerd Hoffmann 已提交
115
    QXL_MODE_EX(2048, 1536),
116
    QXL_MODE_EX(2048, 2048),
G
Gerd Hoffmann 已提交
117 118 119 120 121 122
    QXL_MODE_EX(2560, 1440),
    QXL_MODE_EX(2560, 1600),
    /* these modes need more than 16 MB video memory */
    QXL_MODE_EX(2560, 2048),
    QXL_MODE_EX(2800, 2100),
    QXL_MODE_EX(3200, 2400),
123
    /* these modes need more than 32 MB video memory */
G
Gerd Hoffmann 已提交
124 125
    QXL_MODE_EX(3840, 2160), /* 4k mainstream */
    QXL_MODE_EX(4096, 2160), /* 4k            */
126
    /* these modes need more than 64 MB video memory */
G
Gerd Hoffmann 已提交
127
    QXL_MODE_EX(7680, 4320), /* 8k mainstream */
128
    /* these modes need more than 128 MB video memory */
G
Gerd Hoffmann 已提交
129
    QXL_MODE_EX(8192, 4320), /* 8k            */
G
Gerd Hoffmann 已提交
130 131 132
};

static void qxl_send_events(PCIQXLDevice *d, uint32_t events);
133
static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async);
G
Gerd Hoffmann 已提交
134 135 136 137
static void qxl_reset_memslots(PCIQXLDevice *d);
static void qxl_reset_surfaces(PCIQXLDevice *d);
static void qxl_ring_set_dirty(PCIQXLDevice *qxl);

138 139
static void qxl_hw_update(void *opaque);

140
void qxl_set_guest_bug(PCIQXLDevice *qxl, const char *msg, ...)
141
{
A
Alon Levy 已提交
142
    trace_qxl_set_guest_bug(qxl->id);
143
    qxl_send_events(qxl, QXL_INTERRUPT_ERROR);
144
    qxl->guest_bug = 1;
145
    if (qxl->guestdebug) {
146 147 148 149 150 151
        va_list ap;
        va_start(ap, msg);
        fprintf(stderr, "qxl-%d: guest bug: ", qxl->id);
        vfprintf(stderr, msg, ap);
        fprintf(stderr, "\n");
        va_end(ap);
152 153 154
    }
}

155 156 157 158
static void qxl_clear_guest_bug(PCIQXLDevice *qxl)
{
    qxl->guest_bug = 0;
}
G
Gerd Hoffmann 已提交
159 160 161 162

void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
                           struct QXLRect *area, struct QXLRect *dirty_rects,
                           uint32_t num_dirty_rects,
163
                           uint32_t clear_dirty_region,
A
Alon Levy 已提交
164
                           qxl_async_io async, struct QXLCookie *cookie)
G
Gerd Hoffmann 已提交
165
{
A
Alon Levy 已提交
166 167 168 169
    trace_qxl_spice_update_area(qxl->id, surface_id, area->left, area->right,
                                area->top, area->bottom);
    trace_qxl_spice_update_area_rest(qxl->id, num_dirty_rects,
                                     clear_dirty_region);
170
    if (async == QXL_SYNC) {
171
        spice_qxl_update_area(&qxl->ssd.qxl, surface_id, area,
172 173
                        dirty_rects, num_dirty_rects, clear_dirty_region);
    } else {
A
Alon Levy 已提交
174
        assert(cookie != NULL);
175
        spice_qxl_update_area_async(&qxl->ssd.qxl, surface_id, area,
176
                                    clear_dirty_region, (uintptr_t)cookie);
177
    }
G
Gerd Hoffmann 已提交
178 179
}

180 181
static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice *qxl,
                                                    uint32_t id)
G
Gerd Hoffmann 已提交
182
{
A
Alon Levy 已提交
183
    trace_qxl_spice_destroy_surface_wait_complete(qxl->id, id);
184 185 186 187
    qemu_mutex_lock(&qxl->track_lock);
    qxl->guest_surfaces.cmds[id] = 0;
    qxl->guest_surfaces.count--;
    qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
188 189
}

190 191 192
static void qxl_spice_destroy_surface_wait(PCIQXLDevice *qxl, uint32_t id,
                                           qxl_async_io async)
{
A
Alon Levy 已提交
193 194
    QXLCookie *cookie;

A
Alon Levy 已提交
195
    trace_qxl_spice_destroy_surface_wait(qxl->id, id, async);
196
    if (async) {
A
Alon Levy 已提交
197 198 199
        cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
                                QXL_IO_DESTROY_SURFACE_ASYNC);
        cookie->u.surface_id = id;
200
        spice_qxl_destroy_surface_async(&qxl->ssd.qxl, id, (uintptr_t)cookie);
201
    } else {
202
        spice_qxl_destroy_surface_wait(&qxl->ssd.qxl, id);
203
        qxl_spice_destroy_surface_wait_complete(qxl, id);
204 205 206
    }
}

207 208
static void qxl_spice_flush_surfaces_async(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
209 210
    trace_qxl_spice_flush_surfaces_async(qxl->id, qxl->guest_surfaces.count,
                                         qxl->num_free_res);
A
Alon Levy 已提交
211
    spice_qxl_flush_surfaces_async(&qxl->ssd.qxl,
212 213
        (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
                                  QXL_IO_FLUSH_SURFACES_ASYNC));
214 215
}

G
Gerd Hoffmann 已提交
216 217 218
void qxl_spice_loadvm_commands(PCIQXLDevice *qxl, struct QXLCommandExt *ext,
                               uint32_t count)
{
A
Alon Levy 已提交
219
    trace_qxl_spice_loadvm_commands(qxl->id, ext, count);
220
    spice_qxl_loadvm_commands(&qxl->ssd.qxl, ext, count);
G
Gerd Hoffmann 已提交
221 222 223 224
}

void qxl_spice_oom(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
225
    trace_qxl_spice_oom(qxl->id);
226
    spice_qxl_oom(&qxl->ssd.qxl);
G
Gerd Hoffmann 已提交
227 228 229 230
}

void qxl_spice_reset_memslots(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
231
    trace_qxl_spice_reset_memslots(qxl->id);
232
    spice_qxl_reset_memslots(&qxl->ssd.qxl);
G
Gerd Hoffmann 已提交
233 234
}

235
static void qxl_spice_destroy_surfaces_complete(PCIQXLDevice *qxl)
G
Gerd Hoffmann 已提交
236
{
A
Alon Levy 已提交
237
    trace_qxl_spice_destroy_surfaces_complete(qxl->id);
238
    qemu_mutex_lock(&qxl->track_lock);
239
    memset(qxl->guest_surfaces.cmds, 0,
240
           sizeof(qxl->guest_surfaces.cmds[0]) * qxl->ssd.num_surfaces);
241 242
    qxl->guest_surfaces.count = 0;
    qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
243 244
}

245 246
static void qxl_spice_destroy_surfaces(PCIQXLDevice *qxl, qxl_async_io async)
{
A
Alon Levy 已提交
247
    trace_qxl_spice_destroy_surfaces(qxl->id, async);
248
    if (async) {
A
Alon Levy 已提交
249
        spice_qxl_destroy_surfaces_async(&qxl->ssd.qxl,
250 251
                (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
                                          QXL_IO_DESTROY_ALL_SURFACES_ASYNC));
252
    } else {
253
        spice_qxl_destroy_surfaces(&qxl->ssd.qxl);
254 255 256 257
        qxl_spice_destroy_surfaces_complete(qxl);
    }
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
static void qxl_spice_monitors_config_async(PCIQXLDevice *qxl, int replay)
{
    trace_qxl_spice_monitors_config(qxl->id);
    if (replay) {
        /*
         * don't use QXL_COOKIE_TYPE_IO:
         *  - we are not running yet (post_load), we will assert
         *    in send_events
         *  - this is not a guest io, but a reply, so async_io isn't set.
         */
        spice_qxl_monitors_config_async(&qxl->ssd.qxl,
                qxl->guest_monitors_config,
                MEMSLOT_GROUP_GUEST,
                (uintptr_t)qxl_cookie_new(
                    QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
                    0));
    } else {
        qxl->guest_monitors_config = qxl->ram->monitors_config;
        spice_qxl_monitors_config_async(&qxl->ssd.qxl,
                qxl->ram->monitors_config,
                MEMSLOT_GROUP_GUEST,
                (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
                                          QXL_IO_MONITORS_CONFIG_ASYNC));
    }
}

G
Gerd Hoffmann 已提交
284 285
void qxl_spice_reset_image_cache(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
286
    trace_qxl_spice_reset_image_cache(qxl->id);
287
    spice_qxl_reset_image_cache(&qxl->ssd.qxl);
G
Gerd Hoffmann 已提交
288 289 290 291
}

void qxl_spice_reset_cursor(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
292
    trace_qxl_spice_reset_cursor(qxl->id);
293
    spice_qxl_reset_cursor(&qxl->ssd.qxl);
Y
Yonit Halperin 已提交
294 295 296
    qemu_mutex_lock(&qxl->track_lock);
    qxl->guest_cursor = 0;
    qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
297 298 299 300
    if (qxl->ssd.cursor) {
        cursor_put(qxl->ssd.cursor);
    }
    qxl->ssd.cursor = cursor_builtin_hidden();
G
Gerd Hoffmann 已提交
301 302
}

G
Gerd Hoffmann 已提交
303 304
static ram_addr_t qxl_rom_size(void)
{
A
Alon Levy 已提交
305 306 307
    uint32_t required_rom_size = sizeof(QXLRom) + sizeof(QXLModes) +
                                 sizeof(qxl_modes);
    uint32_t rom_size = 8192; /* two pages */
308

G
Gerd Hoffmann 已提交
309
    QEMU_BUILD_BUG_ON(required_rom_size > rom_size);
G
Gerd Hoffmann 已提交
310 311 312 313 314
    return rom_size;
}

static void init_qxl_rom(PCIQXLDevice *d)
{
315
    QXLRom *rom = memory_region_get_ram_ptr(&d->rom_bar);
G
Gerd Hoffmann 已提交
316 317 318 319
    QXLModes *modes = (QXLModes *)(rom + 1);
    uint32_t ram_header_size;
    uint32_t surface0_area_size;
    uint32_t num_pages;
320 321
    uint32_t fb;
    int i, n;
G
Gerd Hoffmann 已提交
322 323 324 325 326 327 328 329 330 331 332 333

    memset(rom, 0, d->rom_size);

    rom->magic         = cpu_to_le32(QXL_ROM_MAGIC);
    rom->id            = cpu_to_le32(d->id);
    rom->log_level     = cpu_to_le32(d->guestdebug);
    rom->modes_offset  = cpu_to_le32(sizeof(QXLRom));

    rom->slot_gen_bits = MEMSLOT_GENERATION_BITS;
    rom->slot_id_bits  = MEMSLOT_SLOT_BITS;
    rom->slots_start   = 1;
    rom->slots_end     = NUM_MEMSLOTS - 1;
334
    rom->n_surfaces    = cpu_to_le32(d->ssd.num_surfaces);
G
Gerd Hoffmann 已提交
335

336
    for (i = 0, n = 0; i < ARRAY_SIZE(qxl_modes); i++) {
G
Gerd Hoffmann 已提交
337
        fb = qxl_modes[i].y_res * qxl_modes[i].stride;
338 339
        if (fb > d->vgamem_size) {
            continue;
G
Gerd Hoffmann 已提交
340
        }
341 342 343 344 345 346 347 348 349 350 351
        modes->modes[n].id          = cpu_to_le32(i);
        modes->modes[n].x_res       = cpu_to_le32(qxl_modes[i].x_res);
        modes->modes[n].y_res       = cpu_to_le32(qxl_modes[i].y_res);
        modes->modes[n].bits        = cpu_to_le32(qxl_modes[i].bits);
        modes->modes[n].stride      = cpu_to_le32(qxl_modes[i].stride);
        modes->modes[n].x_mili      = cpu_to_le32(qxl_modes[i].x_mili);
        modes->modes[n].y_mili      = cpu_to_le32(qxl_modes[i].y_mili);
        modes->modes[n].orientation = cpu_to_le32(qxl_modes[i].orientation);
        n++;
    }
    modes->n_modes     = cpu_to_le32(n);
G
Gerd Hoffmann 已提交
352 353

    ram_header_size    = ALIGN(sizeof(QXLRam), 4096);
354
    surface0_area_size = ALIGN(d->vgamem_size, 4096);
G
Gerd Hoffmann 已提交
355 356 357
    num_pages          = d->vga.vram_size;
    num_pages         -= ram_header_size;
    num_pages         -= surface0_area_size;
358
    num_pages          = num_pages / QXL_PAGE_SIZE;
G
Gerd Hoffmann 已提交
359

360 361
    assert(ram_header_size + surface0_area_size <= d->vga.vram_size);

G
Gerd Hoffmann 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    rom->draw_area_offset   = cpu_to_le32(0);
    rom->surface0_area_size = cpu_to_le32(surface0_area_size);
    rom->pages_offset       = cpu_to_le32(surface0_area_size);
    rom->num_pages          = cpu_to_le32(num_pages);
    rom->ram_header_offset  = cpu_to_le32(d->vga.vram_size - ram_header_size);

    d->shadow_rom = *rom;
    d->rom        = rom;
    d->modes      = modes;
}

static void init_qxl_ram(PCIQXLDevice *d)
{
    uint8_t *buf;
    uint64_t *item;

    buf = d->vga.vram_ptr;
    d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset));
    d->ram->magic       = cpu_to_le32(QXL_RAM_MAGIC);
    d->ram->int_pending = cpu_to_le32(0);
    d->ram->int_mask    = cpu_to_le32(0);
A
Alon Levy 已提交
383
    d->ram->update_surface = 0;
A
Anthony PERARD 已提交
384
    d->ram->monitors_config = 0;
G
Gerd Hoffmann 已提交
385 386 387
    SPICE_RING_INIT(&d->ram->cmd_ring);
    SPICE_RING_INIT(&d->ram->cursor_ring);
    SPICE_RING_INIT(&d->ram->release_ring);
388 389
    SPICE_RING_PROD_ITEM(d, &d->ram->release_ring, item);
    assert(item);
G
Gerd Hoffmann 已提交
390 391 392 393 394
    *item = 0;
    qxl_ring_set_dirty(d);
}

/* can be called from spice server thread context */
395
static void qxl_set_dirty(MemoryRegion *mr, ram_addr_t addr, ram_addr_t end)
G
Gerd Hoffmann 已提交
396
{
397
    memory_region_set_dirty(mr, addr, end - addr);
G
Gerd Hoffmann 已提交
398 399 400 401
}

static void qxl_rom_set_dirty(PCIQXLDevice *qxl)
{
402
    qxl_set_dirty(&qxl->rom_bar, 0, qxl->rom_size);
G
Gerd Hoffmann 已提交
403 404 405 406 407 408 409 410 411 412
}

/* called from spice server thread context only */
static void qxl_ram_set_dirty(PCIQXLDevice *qxl, void *ptr)
{
    void *base = qxl->vga.vram_ptr;
    intptr_t offset;

    offset = ptr - base;
    assert(offset < qxl->vga.vram_size);
G
Gerd Hoffmann 已提交
413
    qxl_set_dirty(&qxl->vga.vram, offset, offset + 3);
G
Gerd Hoffmann 已提交
414 415 416 417 418
}

/* can be called from spice server thread context */
static void qxl_ring_set_dirty(PCIQXLDevice *qxl)
{
419 420 421
    ram_addr_t addr = qxl->shadow_rom.ram_header_offset;
    ram_addr_t end  = qxl->vga.vram_size;
    qxl_set_dirty(&qxl->vga.vram, addr, end);
G
Gerd Hoffmann 已提交
422 423 424 425 426 427
}

/*
 * keep track of some command state, for savevm/loadvm.
 * called from spice server thread context only
 */
428
static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
G
Gerd Hoffmann 已提交
429 430 431 432 433
{
    switch (le32_to_cpu(ext->cmd.type)) {
    case QXL_CMD_SURFACE:
    {
        QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
434 435 436 437

        if (!cmd) {
            return 1;
        }
G
Gerd Hoffmann 已提交
438
        uint32_t id = le32_to_cpu(cmd->surface_id);
439

440
        if (id >= qxl->ssd.num_surfaces) {
441
            qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE id %d >= %d", id,
442
                              qxl->ssd.num_surfaces);
443 444
            return 1;
        }
445 446 447 448 449 450
        if (cmd->type == QXL_SURFACE_CMD_CREATE &&
            (cmd->u.surface_create.stride & 0x03) != 0) {
            qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE stride = %d %% 4 != 0\n",
                              cmd->u.surface_create.stride);
            return 1;
        }
451
        qemu_mutex_lock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
452 453 454 455 456 457 458 459 460 461
        if (cmd->type == QXL_SURFACE_CMD_CREATE) {
            qxl->guest_surfaces.cmds[id] = ext->cmd.data;
            qxl->guest_surfaces.count++;
            if (qxl->guest_surfaces.max < qxl->guest_surfaces.count)
                qxl->guest_surfaces.max = qxl->guest_surfaces.count;
        }
        if (cmd->type == QXL_SURFACE_CMD_DESTROY) {
            qxl->guest_surfaces.cmds[id] = 0;
            qxl->guest_surfaces.count--;
        }
462
        qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
463 464 465 466 467
        break;
    }
    case QXL_CMD_CURSOR:
    {
        QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
468 469 470 471

        if (!cmd) {
            return 1;
        }
G
Gerd Hoffmann 已提交
472
        if (cmd->type == QXL_CURSOR_SET) {
Y
Yonit Halperin 已提交
473
            qemu_mutex_lock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
474
            qxl->guest_cursor = ext->cmd.data;
Y
Yonit Halperin 已提交
475
            qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
476 477 478 479
        }
        break;
    }
    }
480
    return 0;
G
Gerd Hoffmann 已提交
481 482 483 484 485 486 487 488
}

/* spice display interface callbacks */

static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);

A
Alon Levy 已提交
489
    trace_qxl_interface_attach_worker(qxl->id);
G
Gerd Hoffmann 已提交
490 491 492 493 494 495 496
    qxl->ssd.worker = qxl_worker;
}

static void interface_set_compression_level(QXLInstance *sin, int level)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);

A
Alon Levy 已提交
497
    trace_qxl_interface_set_compression_level(qxl->id, level);
G
Gerd Hoffmann 已提交
498 499 500 501 502 503 504 505 506
    qxl->shadow_rom.compression_level = cpu_to_le32(level);
    qxl->rom->compression_level = cpu_to_le32(level);
    qxl_rom_set_dirty(qxl);
}

static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);

A
Alon Levy 已提交
507
    trace_qxl_interface_set_mm_time(qxl->id, mm_time);
G
Gerd Hoffmann 已提交
508 509 510 511 512 513 514 515 516
    qxl->shadow_rom.mm_clock = cpu_to_le32(mm_time);
    qxl->rom->mm_clock = cpu_to_le32(mm_time);
    qxl_rom_set_dirty(qxl);
}

static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);

A
Alon Levy 已提交
517
    trace_qxl_interface_get_init_info(qxl->id);
G
Gerd Hoffmann 已提交
518 519 520 521 522
    info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
    info->memslot_id_bits = MEMSLOT_SLOT_BITS;
    info->num_memslots = NUM_MEMSLOTS;
    info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
    info->internal_groupslot_id = 0;
523 524
    info->qxl_ram_size =
        le32_to_cpu(qxl->shadow_rom.num_pages) << QXL_PAGE_BITS;
525
    info->n_surfaces = qxl->ssd.num_surfaces;
G
Gerd Hoffmann 已提交
526 527
}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static const char *qxl_mode_to_string(int mode)
{
    switch (mode) {
    case QXL_MODE_COMPAT:
        return "compat";
    case QXL_MODE_NATIVE:
        return "native";
    case QXL_MODE_UNDEFINED:
        return "undefined";
    case QXL_MODE_VGA:
        return "vga";
    }
    return "INVALID";
}

A
Alon Levy 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
static const char *io_port_to_string(uint32_t io_port)
{
    if (io_port >= QXL_IO_RANGE_SIZE) {
        return "out of range";
    }
    static const char *io_port_to_string[QXL_IO_RANGE_SIZE + 1] = {
        [QXL_IO_NOTIFY_CMD]             = "QXL_IO_NOTIFY_CMD",
        [QXL_IO_NOTIFY_CURSOR]          = "QXL_IO_NOTIFY_CURSOR",
        [QXL_IO_UPDATE_AREA]            = "QXL_IO_UPDATE_AREA",
        [QXL_IO_UPDATE_IRQ]             = "QXL_IO_UPDATE_IRQ",
        [QXL_IO_NOTIFY_OOM]             = "QXL_IO_NOTIFY_OOM",
        [QXL_IO_RESET]                  = "QXL_IO_RESET",
        [QXL_IO_SET_MODE]               = "QXL_IO_SET_MODE",
        [QXL_IO_LOG]                    = "QXL_IO_LOG",
        [QXL_IO_MEMSLOT_ADD]            = "QXL_IO_MEMSLOT_ADD",
        [QXL_IO_MEMSLOT_DEL]            = "QXL_IO_MEMSLOT_DEL",
        [QXL_IO_DETACH_PRIMARY]         = "QXL_IO_DETACH_PRIMARY",
        [QXL_IO_ATTACH_PRIMARY]         = "QXL_IO_ATTACH_PRIMARY",
        [QXL_IO_CREATE_PRIMARY]         = "QXL_IO_CREATE_PRIMARY",
        [QXL_IO_DESTROY_PRIMARY]        = "QXL_IO_DESTROY_PRIMARY",
        [QXL_IO_DESTROY_SURFACE_WAIT]   = "QXL_IO_DESTROY_SURFACE_WAIT",
        [QXL_IO_DESTROY_ALL_SURFACES]   = "QXL_IO_DESTROY_ALL_SURFACES",
        [QXL_IO_UPDATE_AREA_ASYNC]      = "QXL_IO_UPDATE_AREA_ASYNC",
        [QXL_IO_MEMSLOT_ADD_ASYNC]      = "QXL_IO_MEMSLOT_ADD_ASYNC",
        [QXL_IO_CREATE_PRIMARY_ASYNC]   = "QXL_IO_CREATE_PRIMARY_ASYNC",
        [QXL_IO_DESTROY_PRIMARY_ASYNC]  = "QXL_IO_DESTROY_PRIMARY_ASYNC",
        [QXL_IO_DESTROY_SURFACE_ASYNC]  = "QXL_IO_DESTROY_SURFACE_ASYNC",
        [QXL_IO_DESTROY_ALL_SURFACES_ASYNC]
                                        = "QXL_IO_DESTROY_ALL_SURFACES_ASYNC",
        [QXL_IO_FLUSH_SURFACES_ASYNC]   = "QXL_IO_FLUSH_SURFACES_ASYNC",
        [QXL_IO_FLUSH_RELEASE]          = "QXL_IO_FLUSH_RELEASE",
574
        [QXL_IO_MONITORS_CONFIG_ASYNC]  = "QXL_IO_MONITORS_CONFIG_ASYNC",
A
Alon Levy 已提交
575 576 577 578
    };
    return io_port_to_string[io_port];
}

G
Gerd Hoffmann 已提交
579 580 581 582 583 584 585
/* called from spice server thread context only */
static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    SimpleSpiceUpdate *update;
    QXLCommandRing *ring;
    QXLCommand *cmd;
586
    int notify, ret;
G
Gerd Hoffmann 已提交
587

A
Alon Levy 已提交
588 589
    trace_qxl_ring_command_check(qxl->id, qxl_mode_to_string(qxl->mode));

G
Gerd Hoffmann 已提交
590 591
    switch (qxl->mode) {
    case QXL_MODE_VGA:
592 593
        ret = false;
        qemu_mutex_lock(&qxl->ssd.lock);
594 595 596
        update = QTAILQ_FIRST(&qxl->ssd.updates);
        if (update != NULL) {
            QTAILQ_REMOVE(&qxl->ssd.updates, update, next);
597 598
            *ext = update->ext;
            ret = true;
G
Gerd Hoffmann 已提交
599
        }
600
        qemu_mutex_unlock(&qxl->ssd.lock);
A
Alon Levy 已提交
601
        if (ret) {
A
Alon Levy 已提交
602
            trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
A
Alon Levy 已提交
603 604
            qxl_log_command(qxl, "vga", ext);
        }
605
        return ret;
G
Gerd Hoffmann 已提交
606 607 608 609
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
    case QXL_MODE_UNDEFINED:
        ring = &qxl->ram->cmd_ring;
610
        if (qxl->guest_bug || SPICE_RING_IS_EMPTY(ring)) {
G
Gerd Hoffmann 已提交
611 612
            return false;
        }
613 614 615 616
        SPICE_RING_CONS_ITEM(qxl, ring, cmd);
        if (!cmd) {
            return false;
        }
G
Gerd Hoffmann 已提交
617 618 619 620 621 622 623 624 625 626 627
        ext->cmd      = *cmd;
        ext->group_id = MEMSLOT_GROUP_GUEST;
        ext->flags    = qxl->cmdflags;
        SPICE_RING_POP(ring, notify);
        qxl_ring_set_dirty(qxl);
        if (notify) {
            qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
        }
        qxl->guest_primary.commands++;
        qxl_track_command(qxl, ext);
        qxl_log_command(qxl, "cmd", ext);
628
        trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
G
Gerd Hoffmann 已提交
629 630 631 632 633 634 635 636 637 638 639 640
        return true;
    default:
        return false;
    }
}

/* called from spice server thread context only */
static int interface_req_cmd_notification(QXLInstance *sin)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    int wait = 1;

A
Alon Levy 已提交
641
    trace_qxl_ring_command_req_notification(qxl->id);
G
Gerd Hoffmann 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    switch (qxl->mode) {
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
    case QXL_MODE_UNDEFINED:
        SPICE_RING_CONS_WAIT(&qxl->ram->cmd_ring, wait);
        qxl_ring_set_dirty(qxl);
        break;
    default:
        /* nothing */
        break;
    }
    return wait;
}

/* called from spice server thread context only */
static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
{
    QXLReleaseRing *ring = &d->ram->release_ring;
    uint64_t *item;
    int notify;

#define QXL_FREE_BUNCH_SIZE 32

    if (ring->prod - ring->cons + 1 == ring->num_items) {
        /* ring full -- can't push */
        return;
    }
    if (!flush && d->oom_running) {
        /* collect everything from oom handler before pushing */
        return;
    }
    if (!flush && d->num_free_res < QXL_FREE_BUNCH_SIZE) {
        /* collect a bit more before pushing */
        return;
    }

    SPICE_RING_PUSH(ring, notify);
A
Alon Levy 已提交
679 680 681 682 683
    trace_qxl_ring_res_push(d->id, qxl_mode_to_string(d->mode),
           d->guest_surfaces.count, d->num_free_res,
           d->last_release, notify ? "yes" : "no");
    trace_qxl_ring_res_push_rest(d->id, ring->prod - ring->cons,
           ring->num_items, ring->prod, ring->cons);
G
Gerd Hoffmann 已提交
684 685 686
    if (notify) {
        qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
    }
687 688 689 690
    SPICE_RING_PROD_ITEM(d, ring, item);
    if (!item) {
        return;
    }
G
Gerd Hoffmann 已提交
691 692 693 694 695 696 697 698
    *item = 0;
    d->num_free_res = 0;
    d->last_release = NULL;
    qxl_ring_set_dirty(d);
}

/* called from spice server thread context only */
static void interface_release_resource(QXLInstance *sin,
699
                                       QXLReleaseInfoExt ext)
G
Gerd Hoffmann 已提交
700 701 702 703 704 705 706
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    QXLReleaseRing *ring;
    uint64_t *item, id;

    if (ext.group_id == MEMSLOT_GROUP_HOST) {
        /* host group -> vga mode update request */
G
Gerd Hoffmann 已提交
707
        QXLCommandExt *cmdext = (void *)(intptr_t)(ext.info->id);
G
Gerd Hoffmann 已提交
708 709 710 711
        SimpleSpiceUpdate *update;
        g_assert(cmdext->cmd.type == QXL_CMD_DRAW);
        update = container_of(cmdext, SimpleSpiceUpdate, ext);
        qemu_spice_destroy_update(&qxl->ssd, update);
G
Gerd Hoffmann 已提交
712 713 714 715 716 717 718 719
        return;
    }

    /*
     * ext->info points into guest-visible memory
     * pci bar 0, $command.release_info
     */
    ring = &qxl->ram->release_ring;
720 721 722 723
    SPICE_RING_PROD_ITEM(qxl, ring, item);
    if (!item) {
        return;
    }
G
Gerd Hoffmann 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    if (*item == 0) {
        /* stick head into the ring */
        id = ext.info->id;
        ext.info->next = 0;
        qxl_ram_set_dirty(qxl, &ext.info->next);
        *item = id;
        qxl_ring_set_dirty(qxl);
    } else {
        /* append item to the list */
        qxl->last_release->next = ext.info->id;
        qxl_ram_set_dirty(qxl, &qxl->last_release->next);
        ext.info->next = 0;
        qxl_ram_set_dirty(qxl, &ext.info->next);
    }
    qxl->last_release = ext.info;
    qxl->num_free_res++;
A
Alon Levy 已提交
740
    trace_qxl_ring_res_put(qxl->id, qxl->num_free_res);
G
Gerd Hoffmann 已提交
741 742 743 744 745 746 747 748 749 750 751
    qxl_push_free_res(qxl, 0);
}

/* called from spice server thread context only */
static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    QXLCursorRing *ring;
    QXLCommand *cmd;
    int notify;

A
Alon Levy 已提交
752 753
    trace_qxl_ring_cursor_check(qxl->id, qxl_mode_to_string(qxl->mode));

G
Gerd Hoffmann 已提交
754 755 756 757 758 759 760 761
    switch (qxl->mode) {
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
    case QXL_MODE_UNDEFINED:
        ring = &qxl->ram->cursor_ring;
        if (SPICE_RING_IS_EMPTY(ring)) {
            return false;
        }
762 763 764 765
        SPICE_RING_CONS_ITEM(qxl, ring, cmd);
        if (!cmd) {
            return false;
        }
G
Gerd Hoffmann 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779
        ext->cmd      = *cmd;
        ext->group_id = MEMSLOT_GROUP_GUEST;
        ext->flags    = qxl->cmdflags;
        SPICE_RING_POP(ring, notify);
        qxl_ring_set_dirty(qxl);
        if (notify) {
            qxl_send_events(qxl, QXL_INTERRUPT_CURSOR);
        }
        qxl->guest_primary.commands++;
        qxl_track_command(qxl, ext);
        qxl_log_command(qxl, "csr", ext);
        if (qxl->id == 0) {
            qxl_render_cursor(qxl, ext);
        }
A
Alon Levy 已提交
780
        trace_qxl_ring_cursor_get(qxl->id, qxl_mode_to_string(qxl->mode));
G
Gerd Hoffmann 已提交
781 782 783 784 785 786 787 788 789 790 791 792
        return true;
    default:
        return false;
    }
}

/* called from spice server thread context only */
static int interface_req_cursor_notification(QXLInstance *sin)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    int wait = 1;

A
Alon Levy 已提交
793
    trace_qxl_ring_cursor_req_notification(qxl->id);
G
Gerd Hoffmann 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    switch (qxl->mode) {
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
    case QXL_MODE_UNDEFINED:
        SPICE_RING_CONS_WAIT(&qxl->ram->cursor_ring, wait);
        qxl_ring_set_dirty(qxl);
        break;
    default:
        /* nothing */
        break;
    }
    return wait;
}

/* called from spice server thread context */
static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
{
811 812 813 814 815 816 817
    /*
     * Called by spice-server as a result of a QXL_CMD_UPDATE which is not in
     * use by xf86-video-qxl and is defined out in the qxl windows driver.
     * Probably was at some earlier version that is prior to git start (2009),
     * and is still guest trigerrable.
     */
    fprintf(stderr, "%s: deprecated\n", __func__);
G
Gerd Hoffmann 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
}

/* called from spice server thread context only */
static int interface_flush_resources(QXLInstance *sin)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    int ret;

    ret = qxl->num_free_res;
    if (ret) {
        qxl_push_free_res(qxl, 1);
    }
    return ret;
}

833 834 835
static void qxl_create_guest_primary_complete(PCIQXLDevice *d);

/* called from spice server thread context only */
A
Alon Levy 已提交
836
static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
837 838 839 840 841 842 843 844
{
    uint32_t current_async;

    qemu_mutex_lock(&qxl->async_lock);
    current_async = qxl->current_async;
    qxl->current_async = QXL_UNDEFINED_IO;
    qemu_mutex_unlock(&qxl->async_lock);

A
Alon Levy 已提交
845
    trace_qxl_interface_async_complete_io(qxl->id, current_async, cookie);
A
Alon Levy 已提交
846 847 848 849 850 851
    if (!cookie) {
        fprintf(stderr, "qxl: %s: error, cookie is NULL\n", __func__);
        return;
    }
    if (cookie && current_async != cookie->io) {
        fprintf(stderr,
A
Alon Levy 已提交
852 853
                "qxl: %s: error: current_async = %d != %"
                PRId64 " = cookie->io\n", __func__, current_async, cookie->io);
A
Alon Levy 已提交
854
    }
855
    switch (current_async) {
A
Alon Levy 已提交
856 857 858 859
    case QXL_IO_MEMSLOT_ADD_ASYNC:
    case QXL_IO_DESTROY_PRIMARY_ASYNC:
    case QXL_IO_UPDATE_AREA_ASYNC:
    case QXL_IO_FLUSH_SURFACES_ASYNC:
860
    case QXL_IO_MONITORS_CONFIG_ASYNC:
A
Alon Levy 已提交
861
        break;
862 863 864 865 866 867 868
    case QXL_IO_CREATE_PRIMARY_ASYNC:
        qxl_create_guest_primary_complete(qxl);
        break;
    case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
        qxl_spice_destroy_surfaces_complete(qxl);
        break;
    case QXL_IO_DESTROY_SURFACE_ASYNC:
A
Alon Levy 已提交
869
        qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
870
        break;
A
Alon Levy 已提交
871 872 873
    default:
        fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
                current_async);
874 875 876 877
    }
    qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
}

A
Alon Levy 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891
/* called from spice server thread context only */
static void interface_update_area_complete(QXLInstance *sin,
        uint32_t surface_id,
        QXLRect *dirty, uint32_t num_updated_rects)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    int i;
    int qxl_i;

    qemu_mutex_lock(&qxl->ssd.lock);
    if (surface_id != 0 || !qxl->render_update_cookie_num) {
        qemu_mutex_unlock(&qxl->ssd.lock);
        return;
    }
A
Alon Levy 已提交
892 893 894
    trace_qxl_interface_update_area_complete(qxl->id, surface_id, dirty->left,
            dirty->right, dirty->top, dirty->bottom);
    trace_qxl_interface_update_area_complete_rest(qxl->id, num_updated_rects);
A
Alon Levy 已提交
895 896 897 898
    if (qxl->num_dirty_rects + num_updated_rects > QXL_NUM_DIRTY_RECTS) {
        /*
         * overflow - treat this as a full update. Not expected to be common.
         */
A
Alon Levy 已提交
899 900
        trace_qxl_interface_update_area_complete_overflow(qxl->id,
                                                          QXL_NUM_DIRTY_RECTS);
A
Alon Levy 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
        qxl->guest_primary.resized = 1;
    }
    if (qxl->guest_primary.resized) {
        /*
         * Don't bother copying or scheduling the bh since we will flip
         * the whole area anyway on completion of the update_area async call
         */
        qemu_mutex_unlock(&qxl->ssd.lock);
        return;
    }
    qxl_i = qxl->num_dirty_rects;
    for (i = 0; i < num_updated_rects; i++) {
        qxl->dirty[qxl_i++] = dirty[i];
    }
    qxl->num_dirty_rects += num_updated_rects;
A
Alon Levy 已提交
916 917
    trace_qxl_interface_update_area_complete_schedule_bh(qxl->id,
                                                         qxl->num_dirty_rects);
A
Alon Levy 已提交
918 919 920 921
    qemu_bh_schedule(qxl->update_area_bh);
    qemu_mutex_unlock(&qxl->ssd.lock);
}

A
Alon Levy 已提交
922 923 924 925
/* called from spice server thread context only */
static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
926
    QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
A
Alon Levy 已提交
927 928 929 930

    switch (cookie->type) {
    case QXL_COOKIE_TYPE_IO:
        interface_async_complete_io(qxl, cookie);
A
Alon Levy 已提交
931 932 933 934
        g_free(cookie);
        break;
    case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA:
        qxl_render_update_area_done(qxl, cookie);
A
Alon Levy 已提交
935
        break;
936 937
    case QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG:
        break;
A
Alon Levy 已提交
938 939 940
    default:
        fprintf(stderr, "qxl: %s: unexpected cookie type %d\n",
                __func__, cookie->type);
A
Alon Levy 已提交
941
        g_free(cookie);
A
Alon Levy 已提交
942 943 944
    }
}

945 946 947 948 949 950 951
/* called from spice server thread context only */
static void interface_set_client_capabilities(QXLInstance *sin,
                                              uint8_t client_present,
                                              uint8_t caps[58])
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);

952 953 954 955 956 957
    if (qxl->revision < 4) {
        trace_qxl_set_client_capabilities_unsupported_by_revision(qxl->id,
                                                              qxl->revision);
        return;
    }

958 959 960 961 962
    if (runstate_check(RUN_STATE_INMIGRATE) ||
        runstate_check(RUN_STATE_POSTMIGRATE)) {
        return;
    }

963
    qxl->shadow_rom.client_present = client_present;
964 965
    memcpy(qxl->shadow_rom.client_capabilities, caps,
           sizeof(qxl->shadow_rom.client_capabilities));
966
    qxl->rom->client_present = client_present;
967 968
    memcpy(qxl->rom->client_capabilities, caps,
           sizeof(qxl->rom->client_capabilities));
969 970 971 972 973
    qxl_rom_set_dirty(qxl);

    qxl_send_events(qxl, QXL_INTERRUPT_CLIENT);
}

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
static uint32_t qxl_crc32(const uint8_t *p, unsigned len)
{
    /*
     * zlib xors the seed with 0xffffffff, and xors the result
     * again with 0xffffffff; Both are not done with linux's crc32,
     * which we want to be compatible with, so undo that.
     */
    return crc32(0xffffffff, p, len) ^ 0xffffffff;
}

/* called from main context only */
static int interface_client_monitors_config(QXLInstance *sin,
                                        VDAgentMonitorsConfig *monitors_config)
{
    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
    QXLRom *rom = memory_region_get_ram_ptr(&qxl->rom_bar);
    int i;

992 993 994 995 996
    if (qxl->revision < 4) {
        trace_qxl_client_monitors_config_unsupported_by_device(qxl->id,
                                                               qxl->revision);
        return 0;
    }
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    /*
     * Older windows drivers set int_mask to 0 when their ISR is called,
     * then later set it to ~0. So it doesn't relate to the actual interrupts
     * handled. However, they are old, so clearly they don't support this
     * interrupt
     */
    if (qxl->ram->int_mask == 0 || qxl->ram->int_mask == ~0 ||
        !(qxl->ram->int_mask & QXL_INTERRUPT_CLIENT_MONITORS_CONFIG)) {
        trace_qxl_client_monitors_config_unsupported_by_guest(qxl->id,
                                                            qxl->ram->int_mask,
                                                            monitors_config);
        return 0;
    }
    if (!monitors_config) {
        return 1;
    }
    memset(&rom->client_monitors_config, 0,
           sizeof(rom->client_monitors_config));
    rom->client_monitors_config.count = monitors_config->num_of_monitors;
    /* monitors_config->flags ignored */
    if (rom->client_monitors_config.count >=
            ARRAY_SIZE(rom->client_monitors_config.heads)) {
        trace_qxl_client_monitors_config_capped(qxl->id,
                                monitors_config->num_of_monitors,
                                ARRAY_SIZE(rom->client_monitors_config.heads));
        rom->client_monitors_config.count =
            ARRAY_SIZE(rom->client_monitors_config.heads);
    }
    for (i = 0 ; i < rom->client_monitors_config.count ; ++i) {
        VDAgentMonConfig *monitor = &monitors_config->monitors[i];
        QXLURect *rect = &rom->client_monitors_config.heads[i];
        /* monitor->depth ignored */
        rect->left = monitor->x;
        rect->top = monitor->y;
        rect->right = monitor->x + monitor->width;
        rect->bottom = monitor->y + monitor->height;
    }
    rom->client_monitors_config_crc = qxl_crc32(
            (const uint8_t *)&rom->client_monitors_config,
            sizeof(rom->client_monitors_config));
    trace_qxl_client_monitors_config_crc(qxl->id,
            sizeof(rom->client_monitors_config),
            rom->client_monitors_config_crc);

    trace_qxl_interrupt_client_monitors_config(qxl->id,
                        rom->client_monitors_config.count,
                        rom->client_monitors_config.heads);
    qxl_send_events(qxl, QXL_INTERRUPT_CLIENT_MONITORS_CONFIG);
    return 1;
}

G
Gerd Hoffmann 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
static const QXLInterface qxl_interface = {
    .base.type               = SPICE_INTERFACE_QXL,
    .base.description        = "qxl gpu",
    .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
    .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,

    .attache_worker          = interface_attach_worker,
    .set_compression_level   = interface_set_compression_level,
    .set_mm_time             = interface_set_mm_time,
    .get_init_info           = interface_get_init_info,

    /* the callbacks below are called from spice server thread context */
    .get_command             = interface_get_command,
    .req_cmd_notification    = interface_req_cmd_notification,
    .release_resource        = interface_release_resource,
    .get_cursor_command      = interface_get_cursor_command,
    .req_cursor_notification = interface_req_cursor_notification,
    .notify_update           = interface_notify_update,
    .flush_resources         = interface_flush_resources,
1067
    .async_complete          = interface_async_complete,
A
Alon Levy 已提交
1068
    .update_area_complete    = interface_update_area_complete,
1069
    .set_client_capabilities = interface_set_client_capabilities,
1070
    .client_monitors_config = interface_client_monitors_config,
G
Gerd Hoffmann 已提交
1071 1072
};

1073 1074 1075 1076
static const GraphicHwOps qxl_ops = {
    .gfx_update  = qxl_hw_update,
};

G
Gerd Hoffmann 已提交
1077 1078 1079 1080 1081
static void qxl_enter_vga_mode(PCIQXLDevice *d)
{
    if (d->mode == QXL_MODE_VGA) {
        return;
    }
A
Alon Levy 已提交
1082
    trace_qxl_enter_vga_mode(d->id);
1083 1084 1085
#if SPICE_SERVER_VERSION >= 0x000c03 /* release 0.12.3 */
    spice_qxl_driver_unload(&d->ssd.qxl);
#endif
1086
    graphic_console_set_hwops(d->ssd.dcl.con, d->vga.hw_ops, &d->vga);
1087
    update_displaychangelistener(&d->ssd.dcl, GUI_REFRESH_INTERVAL_DEFAULT);
G
Gerd Hoffmann 已提交
1088 1089
    qemu_spice_create_host_primary(&d->ssd);
    d->mode = QXL_MODE_VGA;
1090
    vga_dirty_log_start(&d->vga);
1091
    graphic_hw_update(d->vga.con);
G
Gerd Hoffmann 已提交
1092 1093 1094 1095 1096 1097 1098
}

static void qxl_exit_vga_mode(PCIQXLDevice *d)
{
    if (d->mode != QXL_MODE_VGA) {
        return;
    }
A
Alon Levy 已提交
1099
    trace_qxl_exit_vga_mode(d->id);
1100
    graphic_console_set_hwops(d->ssd.dcl.con, &qxl_ops, d);
1101
    update_displaychangelistener(&d->ssd.dcl, GUI_REFRESH_INTERVAL_IDLE);
1102
    vga_dirty_log_stop(&d->vga);
1103
    qxl_destroy_primary(d, QXL_SYNC);
G
Gerd Hoffmann 已提交
1104 1105
}

1106
static void qxl_update_irq(PCIQXLDevice *d)
G
Gerd Hoffmann 已提交
1107 1108 1109 1110
{
    uint32_t pending = le32_to_cpu(d->ram->int_pending);
    uint32_t mask    = le32_to_cpu(d->ram->int_mask);
    int level = !!(pending & mask);
1111
    pci_set_irq(&d->pci, level);
G
Gerd Hoffmann 已提交
1112 1113 1114 1115 1116 1117
    qxl_ring_set_dirty(d);
}

static void qxl_check_state(PCIQXLDevice *d)
{
    QXLRam *ram = d->ram;
1118
    int spice_display_running = qemu_spice_display_is_running(&d->ssd);
G
Gerd Hoffmann 已提交
1119

1120 1121
    assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cmd_ring));
    assert(!spice_display_running || SPICE_RING_IS_EMPTY(&ram->cursor_ring));
G
Gerd Hoffmann 已提交
1122 1123 1124 1125 1126 1127
}

static void qxl_reset_state(PCIQXLDevice *d)
{
    QXLRom *rom = d->rom;

1128
    qxl_check_state(d);
G
Gerd Hoffmann 已提交
1129 1130 1131 1132 1133 1134 1135
    d->shadow_rom.update_id = cpu_to_le32(0);
    *rom = d->shadow_rom;
    qxl_rom_set_dirty(d);
    init_qxl_ram(d);
    d->num_free_res = 0;
    d->last_release = NULL;
    memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty));
A
Alon Levy 已提交
1136
    qxl_update_irq(d);
G
Gerd Hoffmann 已提交
1137 1138 1139 1140
}

static void qxl_soft_reset(PCIQXLDevice *d)
{
A
Alon Levy 已提交
1141
    trace_qxl_soft_reset(d->id);
G
Gerd Hoffmann 已提交
1142
    qxl_check_state(d);
1143
    qxl_clear_guest_bug(d);
1144
    d->current_async = QXL_UNDEFINED_IO;
G
Gerd Hoffmann 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

    if (d->id == 0) {
        qxl_enter_vga_mode(d);
    } else {
        d->mode = QXL_MODE_UNDEFINED;
    }
}

static void qxl_hard_reset(PCIQXLDevice *d, int loadvm)
{
1155 1156
    bool startstop = qemu_spice_display_is_running(&d->ssd);

A
Alon Levy 已提交
1157
    trace_qxl_hard_reset(d->id, loadvm);
G
Gerd Hoffmann 已提交
1158

1159 1160 1161 1162
    if (startstop) {
        qemu_spice_display_stop();
    }

G
Gerd Hoffmann 已提交
1163 1164
    qxl_spice_reset_cursor(d);
    qxl_spice_reset_image_cache(d);
G
Gerd Hoffmann 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
    qxl_reset_surfaces(d);
    qxl_reset_memslots(d);

    /* pre loadvm reset must not touch QXLRam.  This lives in
     * device memory, is migrated together with RAM and thus
     * already loaded at this point */
    if (!loadvm) {
        qxl_reset_state(d);
    }
    qemu_spice_create_host_memslot(&d->ssd);
    qxl_soft_reset(d);
1176 1177 1178 1179

    if (startstop) {
        qemu_spice_display_start();
    }
G
Gerd Hoffmann 已提交
1180 1181 1182 1183
}

static void qxl_reset_handler(DeviceState *dev)
{
G
Gonglei 已提交
1184
    PCIQXLDevice *d = PCI_QXL(PCI_DEVICE(dev));
A
Alon Levy 已提交
1185

G
Gerd Hoffmann 已提交
1186 1187 1188 1189 1190 1191 1192 1193
    qxl_hard_reset(d, 0);
}

static void qxl_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VGACommonState *vga = opaque;
    PCIQXLDevice *qxl = container_of(vga, PCIQXLDevice, vga);

A
Alon Levy 已提交
1194
    trace_qxl_io_write_vga(qxl->id, qxl_mode_to_string(qxl->mode), addr, val);
G
Gerd Hoffmann 已提交
1195
    if (qxl->mode != QXL_MODE_VGA) {
1196
        qxl_destroy_primary(qxl, QXL_SYNC);
G
Gerd Hoffmann 已提交
1197 1198 1199 1200 1201
        qxl_soft_reset(qxl);
    }
    vga_ioport_write(opaque, addr, val);
}

G
Gerd Hoffmann 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
static const MemoryRegionPortio qxl_vga_portio_list[] = {
    { 0x04,  2, 1, .read  = vga_ioport_read,
                   .write = qxl_vga_ioport_write }, /* 3b4 */
    { 0x0a,  1, 1, .read  = vga_ioport_read,
                   .write = qxl_vga_ioport_write }, /* 3ba */
    { 0x10, 16, 1, .read  = vga_ioport_read,
                   .write = qxl_vga_ioport_write }, /* 3c0 */
    { 0x24,  2, 1, .read  = vga_ioport_read,
                   .write = qxl_vga_ioport_write }, /* 3d4 */
    { 0x2a,  1, 1, .read  = vga_ioport_read,
                   .write = qxl_vga_ioport_write }, /* 3da */
    PORTIO_END_OF_LIST(),
};

1216 1217
static int qxl_add_memslot(PCIQXLDevice *d, uint32_t slot_id, uint64_t delta,
                           qxl_async_io async)
G
Gerd Hoffmann 已提交
1218 1219 1220 1221
{
    static const int regions[] = {
        QXL_RAM_RANGE_INDEX,
        QXL_VRAM_RANGE_INDEX,
G
Gerd Hoffmann 已提交
1222
        QXL_VRAM64_RANGE_INDEX,
G
Gerd Hoffmann 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
    };
    uint64_t guest_start;
    uint64_t guest_end;
    int pci_region;
    pcibus_t pci_start;
    pcibus_t pci_end;
    intptr_t virt_start;
    QXLDevMemSlot memslot;
    int i;

    guest_start = le64_to_cpu(d->guest_slots[slot_id].slot.mem_start);
    guest_end   = le64_to_cpu(d->guest_slots[slot_id].slot.mem_end);

A
Alon Levy 已提交
1236
    trace_qxl_memslot_add_guest(d->id, slot_id, guest_start, guest_end);
G
Gerd Hoffmann 已提交
1237

1238
    if (slot_id >= NUM_MEMSLOTS) {
1239
        qxl_set_guest_bug(d, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__,
1240 1241 1242 1243
                      slot_id, NUM_MEMSLOTS);
        return 1;
    }
    if (guest_start > guest_end) {
1244
        qxl_set_guest_bug(d, "%s: guest_start > guest_end 0x%" PRIx64
1245 1246 1247
                         " > 0x%" PRIx64, __func__, guest_start, guest_end);
        return 1;
    }
G
Gerd Hoffmann 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

    for (i = 0; i < ARRAY_SIZE(regions); i++) {
        pci_region = regions[i];
        pci_start = d->pci.io_regions[pci_region].addr;
        pci_end = pci_start + d->pci.io_regions[pci_region].size;
        /* mapped? */
        if (pci_start == -1) {
            continue;
        }
        /* start address in range ? */
        if (guest_start < pci_start || guest_start > pci_end) {
            continue;
        }
        /* end address in range ? */
        if (guest_end > pci_end) {
            continue;
        }
        /* passed */
        break;
    }
1268
    if (i == ARRAY_SIZE(regions)) {
1269
        qxl_set_guest_bug(d, "%s: finished loop without match", __func__);
1270 1271
        return 1;
    }
G
Gerd Hoffmann 已提交
1272 1273 1274

    switch (pci_region) {
    case QXL_RAM_RANGE_INDEX:
1275
        virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vga.vram);
G
Gerd Hoffmann 已提交
1276 1277
        break;
    case QXL_VRAM_RANGE_INDEX:
G
Gerd Hoffmann 已提交
1278
    case 4 /* vram 64bit */:
1279
        virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vram_bar);
G
Gerd Hoffmann 已提交
1280 1281 1282
        break;
    default:
        /* should not happen */
1283
        qxl_set_guest_bug(d, "%s: pci_region = %d", __func__, pci_region);
1284
        return 1;
G
Gerd Hoffmann 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    }

    memslot.slot_id = slot_id;
    memslot.slot_group_id = MEMSLOT_GROUP_GUEST; /* guest group */
    memslot.virt_start = virt_start + (guest_start - pci_start);
    memslot.virt_end   = virt_start + (guest_end   - pci_start);
    memslot.addr_delta = memslot.virt_start - delta;
    memslot.generation = d->rom->slot_generation = 0;
    qxl_rom_set_dirty(d);

1295
    qemu_spice_add_memslot(&d->ssd, &memslot, async);
G
Gerd Hoffmann 已提交
1296 1297 1298 1299
    d->guest_slots[slot_id].ptr = (void*)memslot.virt_start;
    d->guest_slots[slot_id].size = memslot.virt_end - memslot.virt_start;
    d->guest_slots[slot_id].delta = delta;
    d->guest_slots[slot_id].active = 1;
1300
    return 0;
G
Gerd Hoffmann 已提交
1301 1302 1303 1304
}

static void qxl_del_memslot(PCIQXLDevice *d, uint32_t slot_id)
{
1305
    qemu_spice_del_memslot(&d->ssd, MEMSLOT_GROUP_HOST, slot_id);
G
Gerd Hoffmann 已提交
1306 1307 1308 1309 1310
    d->guest_slots[slot_id].active = 0;
}

static void qxl_reset_memslots(PCIQXLDevice *d)
{
G
Gerd Hoffmann 已提交
1311
    qxl_spice_reset_memslots(d);
G
Gerd Hoffmann 已提交
1312 1313 1314 1315 1316
    memset(&d->guest_slots, 0, sizeof(d->guest_slots));
}

static void qxl_reset_surfaces(PCIQXLDevice *d)
{
A
Alon Levy 已提交
1317
    trace_qxl_reset_surfaces(d->id);
G
Gerd Hoffmann 已提交
1318
    d->mode = QXL_MODE_UNDEFINED;
1319
    qxl_spice_destroy_surfaces(d, QXL_SYNC);
G
Gerd Hoffmann 已提交
1320 1321
}

1322
/* can be also called from spice server thread context */
G
Gerd Hoffmann 已提交
1323 1324 1325 1326 1327 1328 1329 1330
void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id)
{
    uint64_t phys   = le64_to_cpu(pqxl);
    uint32_t slot   = (phys >> (64 -  8)) & 0xff;
    uint64_t offset = phys & 0xffffffffffff;

    switch (group_id) {
    case MEMSLOT_GROUP_HOST:
G
Gerd Hoffmann 已提交
1331
        return (void *)(intptr_t)offset;
G
Gerd Hoffmann 已提交
1332
    case MEMSLOT_GROUP_GUEST:
1333
        if (slot >= NUM_MEMSLOTS) {
1334 1335
            qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
                              NUM_MEMSLOTS);
1336 1337 1338
            return NULL;
        }
        if (!qxl->guest_slots[slot].active) {
1339
            qxl_set_guest_bug(qxl, "inactive slot %d\n", slot);
1340 1341 1342
            return NULL;
        }
        if (offset < qxl->guest_slots[slot].delta) {
1343 1344
            qxl_set_guest_bug(qxl,
                          "slot %d offset %"PRIu64" < delta %"PRIu64"\n",
1345 1346 1347
                          slot, offset, qxl->guest_slots[slot].delta);
            return NULL;
        }
G
Gerd Hoffmann 已提交
1348
        offset -= qxl->guest_slots[slot].delta;
1349
        if (offset > qxl->guest_slots[slot].size) {
1350 1351
            qxl_set_guest_bug(qxl,
                          "slot %d offset %"PRIu64" > size %"PRIu64"\n",
1352 1353 1354
                          slot, offset, qxl->guest_slots[slot].size);
            return NULL;
        }
G
Gerd Hoffmann 已提交
1355 1356
        return qxl->guest_slots[slot].ptr + offset;
    }
1357
    return NULL;
G
Gerd Hoffmann 已提交
1358 1359
}

1360 1361 1362 1363 1364 1365 1366 1367
static void qxl_create_guest_primary_complete(PCIQXLDevice *qxl)
{
    /* for local rendering */
    qxl_render_resize(qxl);
}

static void qxl_create_guest_primary(PCIQXLDevice *qxl, int loadvm,
                                     qxl_async_io async)
G
Gerd Hoffmann 已提交
1368 1369 1370
{
    QXLDevSurfaceCreate surface;
    QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
1371
    uint32_t requested_height = le32_to_cpu(sc->height);
1372 1373
    int requested_stride = le32_to_cpu(sc->stride);

1374 1375 1376 1377 1378 1379 1380
    if (requested_stride == INT32_MIN ||
        abs(requested_stride) * (uint64_t)requested_height
                                        > qxl->vgamem_size) {
        qxl_set_guest_bug(qxl, "%s: requested primary larger than framebuffer"
                               " stride %d x height %" PRIu32 " > %" PRIu32,
                               __func__, requested_stride, requested_height,
                               qxl->vgamem_size);
1381 1382
        return;
    }
G
Gerd Hoffmann 已提交
1383

1384
    if (qxl->mode == QXL_MODE_NATIVE) {
1385
        qxl_set_guest_bug(qxl, "%s: nop since already in QXL_MODE_NATIVE",
1386 1387
                      __func__);
    }
G
Gerd Hoffmann 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
    qxl_exit_vga_mode(qxl);

    surface.format     = le32_to_cpu(sc->format);
    surface.height     = le32_to_cpu(sc->height);
    surface.mem        = le64_to_cpu(sc->mem);
    surface.position   = le32_to_cpu(sc->position);
    surface.stride     = le32_to_cpu(sc->stride);
    surface.width      = le32_to_cpu(sc->width);
    surface.type       = le32_to_cpu(sc->type);
    surface.flags      = le32_to_cpu(sc->flags);
A
Alon Levy 已提交
1398 1399 1400 1401
    trace_qxl_create_guest_primary(qxl->id, sc->width, sc->height, sc->mem,
                                   sc->format, sc->position);
    trace_qxl_create_guest_primary_rest(qxl->id, sc->stride, sc->type,
                                        sc->flags);
G
Gerd Hoffmann 已提交
1402

1403 1404 1405 1406 1407 1408
    if ((surface.stride & 0x3) != 0) {
        qxl_set_guest_bug(qxl, "primary surface stride = %d %% 4 != 0",
                          surface.stride);
        return;
    }

G
Gerd Hoffmann 已提交
1409 1410 1411 1412 1413 1414 1415 1416
    surface.mouse_mode = true;
    surface.group_id   = MEMSLOT_GROUP_GUEST;
    if (loadvm) {
        surface.flags |= QXL_SURF_FLAG_KEEP_DATA;
    }

    qxl->mode = QXL_MODE_NATIVE;
    qxl->cmdflags = 0;
1417
    qemu_spice_create_primary_surface(&qxl->ssd, 0, &surface, async);
G
Gerd Hoffmann 已提交
1418

1419 1420 1421
    if (async == QXL_SYNC) {
        qxl_create_guest_primary_complete(qxl);
    }
G
Gerd Hoffmann 已提交
1422 1423
}

1424 1425 1426
/* return 1 if surface destoy was initiated (in QXL_ASYNC case) or
 * done (in QXL_SYNC case), 0 otherwise. */
static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async)
G
Gerd Hoffmann 已提交
1427 1428
{
    if (d->mode == QXL_MODE_UNDEFINED) {
1429
        return 0;
G
Gerd Hoffmann 已提交
1430
    }
A
Alon Levy 已提交
1431
    trace_qxl_destroy_primary(d->id);
G
Gerd Hoffmann 已提交
1432
    d->mode = QXL_MODE_UNDEFINED;
1433
    qemu_spice_destroy_primary_surface(&d->ssd, 0, async);
Y
Yonit Halperin 已提交
1434
    qxl_spice_reset_cursor(d);
1435
    return 1;
G
Gerd Hoffmann 已提交
1436 1437
}

G
Gerd Hoffmann 已提交
1438
static void qxl_set_mode(PCIQXLDevice *d, unsigned int modenr, int loadvm)
G
Gerd Hoffmann 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447
{
    pcibus_t start = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
    pcibus_t end   = d->pci.io_regions[QXL_RAM_RANGE_INDEX].size + start;
    QXLMode *mode = d->modes->modes + modenr;
    uint64_t devmem = d->pci.io_regions[QXL_RAM_RANGE_INDEX].addr;
    QXLMemSlot slot = {
        .mem_start = start,
        .mem_end = end
    };
G
Gerd Hoffmann 已提交
1448 1449 1450 1451 1452 1453

    if (modenr >= d->modes->n_modes) {
        qxl_set_guest_bug(d, "mode number out of range");
        return;
    }

G
Gerd Hoffmann 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    QXLSurfaceCreate surface = {
        .width      = mode->x_res,
        .height     = mode->y_res,
        .stride     = -mode->x_res * 4,
        .format     = SPICE_SURFACE_FMT_32_xRGB,
        .flags      = loadvm ? QXL_SURF_FLAG_KEEP_DATA : 0,
        .mouse_mode = true,
        .mem        = devmem + d->shadow_rom.draw_area_offset,
    };

A
Alon Levy 已提交
1464 1465
    trace_qxl_set_mode(d->id, modenr, mode->x_res, mode->y_res, mode->bits,
                       devmem);
G
Gerd Hoffmann 已提交
1466 1467 1468 1469 1470
    if (!loadvm) {
        qxl_hard_reset(d, 0);
    }

    d->guest_slots[0].slot = slot;
1471
    assert(qxl_add_memslot(d, 0, devmem, QXL_SYNC) == 0);
G
Gerd Hoffmann 已提交
1472 1473

    d->guest_primary.surface = surface;
1474
    qxl_create_guest_primary(d, 0, QXL_SYNC);
G
Gerd Hoffmann 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485

    d->mode = QXL_MODE_COMPAT;
    d->cmdflags = QXL_COMMAND_FLAG_COMPAT;
    if (mode->bits == 16) {
        d->cmdflags |= QXL_COMMAND_FLAG_COMPAT_16BPP;
    }
    d->shadow_rom.mode = cpu_to_le32(modenr);
    d->rom->mode = cpu_to_le32(modenr);
    qxl_rom_set_dirty(d);
}

A
Avi Kivity 已提交
1486
static void ioport_write(void *opaque, hwaddr addr,
1487
                         uint64_t val, unsigned size)
G
Gerd Hoffmann 已提交
1488 1489
{
    PCIQXLDevice *d = opaque;
1490
    uint32_t io_port = addr;
1491 1492
    qxl_async_io async = QXL_SYNC;
    uint32_t orig_io_port = io_port;
G
Gerd Hoffmann 已提交
1493

1494
    if (d->guest_bug && io_port != QXL_IO_RESET) {
1495 1496 1497
        return;
    }

1498
    if (d->revision <= QXL_REVISION_STABLE_V10 &&
1499
        io_port > QXL_IO_FLUSH_RELEASE) {
1500 1501 1502 1503 1504
        qxl_set_guest_bug(d, "unsupported io %d for revision %d\n",
            io_port, d->revision);
        return;
    }

G
Gerd Hoffmann 已提交
1505 1506 1507 1508 1509 1510
    switch (io_port) {
    case QXL_IO_RESET:
    case QXL_IO_SET_MODE:
    case QXL_IO_MEMSLOT_ADD:
    case QXL_IO_MEMSLOT_DEL:
    case QXL_IO_CREATE_PRIMARY:
1511
    case QXL_IO_UPDATE_IRQ:
A
Alon Levy 已提交
1512
    case QXL_IO_LOG:
1513 1514
    case QXL_IO_MEMSLOT_ADD_ASYNC:
    case QXL_IO_CREATE_PRIMARY_ASYNC:
G
Gerd Hoffmann 已提交
1515 1516
        break;
    default:
1517
        if (d->mode != QXL_MODE_VGA) {
G
Gerd Hoffmann 已提交
1518
            break;
1519
        }
A
Alon Levy 已提交
1520
        trace_qxl_io_unexpected_vga_mode(d->id,
A
Alon Levy 已提交
1521
            addr, val, io_port_to_string(io_port));
1522 1523
        /* be nice to buggy guest drivers */
        if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&
1524
            io_port < QXL_IO_RANGE_SIZE) {
1525 1526
            qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
        }
G
Gerd Hoffmann 已提交
1527 1528 1529
        return;
    }

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
    /* we change the io_port to avoid ifdeffery in the main switch */
    orig_io_port = io_port;
    switch (io_port) {
    case QXL_IO_UPDATE_AREA_ASYNC:
        io_port = QXL_IO_UPDATE_AREA;
        goto async_common;
    case QXL_IO_MEMSLOT_ADD_ASYNC:
        io_port = QXL_IO_MEMSLOT_ADD;
        goto async_common;
    case QXL_IO_CREATE_PRIMARY_ASYNC:
        io_port = QXL_IO_CREATE_PRIMARY;
        goto async_common;
    case QXL_IO_DESTROY_PRIMARY_ASYNC:
        io_port = QXL_IO_DESTROY_PRIMARY;
        goto async_common;
    case QXL_IO_DESTROY_SURFACE_ASYNC:
        io_port = QXL_IO_DESTROY_SURFACE_WAIT;
        goto async_common;
    case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:
        io_port = QXL_IO_DESTROY_ALL_SURFACES;
1550 1551
        goto async_common;
    case QXL_IO_FLUSH_SURFACES_ASYNC:
1552
    case QXL_IO_MONITORS_CONFIG_ASYNC:
1553 1554 1555 1556
async_common:
        async = QXL_ASYNC;
        qemu_mutex_lock(&d->async_lock);
        if (d->current_async != QXL_UNDEFINED_IO) {
1557
            qxl_set_guest_bug(d, "%d async started before last (%d) complete",
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
                io_port, d->current_async);
            qemu_mutex_unlock(&d->async_lock);
            return;
        }
        d->current_async = orig_io_port;
        qemu_mutex_unlock(&d->async_lock);
        break;
    default:
        break;
    }
G
Gerd Hoffmann 已提交
1568 1569 1570
    trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode),
                       addr, io_port_to_string(addr),
                       val, size, async);
1571

G
Gerd Hoffmann 已提交
1572 1573 1574
    switch (io_port) {
    case QXL_IO_UPDATE_AREA:
    {
A
Alon Levy 已提交
1575
        QXLCookie *cookie = NULL;
G
Gerd Hoffmann 已提交
1576
        QXLRect update = d->ram->update_area;
A
Alon Levy 已提交
1577

1578
        if (d->ram->update_surface > d->ssd.num_surfaces) {
1579 1580
            qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
                              d->ram->update_surface);
1581
            break;
1582
        }
1583 1584
        if (update.left >= update.right || update.top >= update.bottom ||
            update.left < 0 || update.top < 0) {
1585 1586 1587
            qxl_set_guest_bug(d,
                    "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",
                    update.left, update.top, update.right, update.bottom);
1588 1589 1590 1591 1592
            if (update.left == update.right || update.top == update.bottom) {
                /* old drivers may provide empty area, keep going */
                qxl_clear_guest_bug(d);
                goto cancel_async;
            }
D
Dunrong Huang 已提交
1593 1594
            break;
        }
A
Alon Levy 已提交
1595 1596 1597 1598 1599
        if (async == QXL_ASYNC) {
            cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
                                    QXL_IO_UPDATE_AREA_ASYNC);
            cookie->u.area = update;
        }
G
Gerd Hoffmann 已提交
1600
        qxl_spice_update_area(d, d->ram->update_surface,
A
Alon Levy 已提交
1601 1602
                              cookie ? &cookie->u.area : &update,
                              NULL, 0, 0, async, cookie);
G
Gerd Hoffmann 已提交
1603 1604 1605
        break;
    }
    case QXL_IO_NOTIFY_CMD:
1606
        qemu_spice_wakeup(&d->ssd);
G
Gerd Hoffmann 已提交
1607 1608
        break;
    case QXL_IO_NOTIFY_CURSOR:
1609
        qemu_spice_wakeup(&d->ssd);
G
Gerd Hoffmann 已提交
1610 1611
        break;
    case QXL_IO_UPDATE_IRQ:
1612
        qxl_update_irq(d);
G
Gerd Hoffmann 已提交
1613 1614 1615 1616 1617 1618
        break;
    case QXL_IO_NOTIFY_OOM:
        if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
            break;
        }
        d->oom_running = 1;
G
Gerd Hoffmann 已提交
1619
        qxl_spice_oom(d);
G
Gerd Hoffmann 已提交
1620 1621 1622 1623 1624 1625
        d->oom_running = 0;
        break;
    case QXL_IO_SET_MODE:
        qxl_set_mode(d, val, 0);
        break;
    case QXL_IO_LOG:
A
Alon Levy 已提交
1626
        trace_qxl_io_log(d->id, d->ram->log_buf);
G
Gerd Hoffmann 已提交
1627
        if (d->guestdebug) {
P
Peter Maydell 已提交
1628
            fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,
1629
                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), d->ram->log_buf);
G
Gerd Hoffmann 已提交
1630 1631 1632 1633 1634 1635
        }
        break;
    case QXL_IO_RESET:
        qxl_hard_reset(d, 0);
        break;
    case QXL_IO_MEMSLOT_ADD:
1636
        if (val >= NUM_MEMSLOTS) {
1637
            qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
1638 1639 1640
            break;
        }
        if (d->guest_slots[val].active) {
1641 1642
            qxl_set_guest_bug(d,
                        "QXL_IO_MEMSLOT_ADD: memory slot already active");
1643 1644
            break;
        }
G
Gerd Hoffmann 已提交
1645
        d->guest_slots[val].slot = d->ram->mem_slot;
1646
        qxl_add_memslot(d, val, 0, async);
G
Gerd Hoffmann 已提交
1647 1648
        break;
    case QXL_IO_MEMSLOT_DEL:
1649
        if (val >= NUM_MEMSLOTS) {
1650
            qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
1651 1652
            break;
        }
G
Gerd Hoffmann 已提交
1653 1654 1655
        qxl_del_memslot(d, val);
        break;
    case QXL_IO_CREATE_PRIMARY:
1656
        if (val != 0) {
1657
            qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
1658 1659
                          async);
            goto cancel_async;
1660
        }
G
Gerd Hoffmann 已提交
1661
        d->guest_primary.surface = d->ram->create_surface;
1662
        qxl_create_guest_primary(d, 0, async);
G
Gerd Hoffmann 已提交
1663 1664
        break;
    case QXL_IO_DESTROY_PRIMARY:
1665
        if (val != 0) {
1666
            qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
1667 1668 1669 1670
                          async);
            goto cancel_async;
        }
        if (!qxl_destroy_primary(d, async)) {
A
Alon Levy 已提交
1671 1672
            trace_qxl_io_destroy_primary_ignored(d->id,
                                                 qxl_mode_to_string(d->mode));
1673
            goto cancel_async;
1674
        }
G
Gerd Hoffmann 已提交
1675 1676
        break;
    case QXL_IO_DESTROY_SURFACE_WAIT:
1677
        if (val >= d->ssd.num_surfaces) {
1678
            qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
1679
                             "%" PRIu64 " >= NUM_SURFACES", async, val);
1680 1681 1682
            goto cancel_async;
        }
        qxl_spice_destroy_surface_wait(d, val, async);
G
Gerd Hoffmann 已提交
1683
        break;
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    case QXL_IO_FLUSH_RELEASE: {
        QXLReleaseRing *ring = &d->ram->release_ring;
        if (ring->prod - ring->cons + 1 == ring->num_items) {
            fprintf(stderr,
                "ERROR: no flush, full release ring [p%d,%dc]\n",
                ring->prod, ring->cons);
        }
        qxl_push_free_res(d, 1 /* flush */);
        break;
    }
    case QXL_IO_FLUSH_SURFACES_ASYNC:
        qxl_spice_flush_surfaces_async(d);
        break;
G
Gerd Hoffmann 已提交
1697
    case QXL_IO_DESTROY_ALL_SURFACES:
1698 1699
        d->mode = QXL_MODE_UNDEFINED;
        qxl_spice_destroy_surfaces(d, async);
G
Gerd Hoffmann 已提交
1700
        break;
1701 1702 1703
    case QXL_IO_MONITORS_CONFIG_ASYNC:
        qxl_spice_monitors_config_async(d, 0);
        break;
G
Gerd Hoffmann 已提交
1704
    default:
1705
        qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);
G
Gerd Hoffmann 已提交
1706
    }
1707 1708 1709 1710 1711 1712 1713 1714
    return;
cancel_async:
    if (async) {
        qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
        qemu_mutex_lock(&d->async_lock);
        d->current_async = QXL_UNDEFINED_IO;
        qemu_mutex_unlock(&d->async_lock);
    }
G
Gerd Hoffmann 已提交
1715 1716
}

A
Avi Kivity 已提交
1717
static uint64_t ioport_read(void *opaque, hwaddr addr,
1718
                            unsigned size)
G
Gerd Hoffmann 已提交
1719
{
A
Alon Levy 已提交
1720
    PCIQXLDevice *qxl = opaque;
G
Gerd Hoffmann 已提交
1721

A
Alon Levy 已提交
1722
    trace_qxl_io_read_unexpected(qxl->id);
G
Gerd Hoffmann 已提交
1723 1724 1725
    return 0xff;
}

1726 1727 1728 1729 1730 1731 1732 1733
static const MemoryRegionOps qxl_io_ops = {
    .read = ioport_read,
    .write = ioport_write,
    .valid = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
};
G
Gerd Hoffmann 已提交
1734

1735
static void qxl_update_irq_bh(void *opaque)
G
Gerd Hoffmann 已提交
1736 1737
{
    PCIQXLDevice *d = opaque;
1738
    qxl_update_irq(d);
G
Gerd Hoffmann 已提交
1739 1740 1741 1742 1743 1744 1745
}

static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
{
    uint32_t old_pending;
    uint32_t le_events = cpu_to_le32(events);

A
Alon Levy 已提交
1746
    trace_qxl_send_events(d->id, events);
1747 1748 1749 1750 1751 1752 1753
    if (!qemu_spice_display_is_running(&d->ssd)) {
        /* spice-server tracks guest running state and should not do this */
        fprintf(stderr, "%s: spice-server bug: guest stopped, ignoring\n",
                __func__);
        trace_qxl_send_events_vm_stopped(d->id, events);
        return;
    }
1754
    old_pending = atomic_fetch_or(&d->ram->int_pending, le_events);
G
Gerd Hoffmann 已提交
1755 1756 1757
    if ((old_pending & le_events) == le_events) {
        return;
    }
1758
    qemu_bh_schedule(d->update_irq);
G
Gerd Hoffmann 已提交
1759 1760 1761 1762 1763 1764 1765 1766
}

/* graphics console */

static void qxl_hw_update(void *opaque)
{
    PCIQXLDevice *qxl = opaque;

1767
    qxl_render_update(qxl);
G
Gerd Hoffmann 已提交
1768 1769
}

1770 1771
static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
{
1772
    uintptr_t vram_start;
1773 1774
    int i;

1775
    if (qxl->mode != QXL_MODE_NATIVE && qxl->mode != QXL_MODE_COMPAT) {
1776 1777 1778 1779 1780 1781 1782
        return;
    }

    /* dirty the primary surface */
    qxl_set_dirty(&qxl->vga.vram, qxl->shadow_rom.draw_area_offset,
                  qxl->shadow_rom.surface0_area_size);

1783
    vram_start = (uintptr_t)memory_region_get_ram_ptr(&qxl->vram_bar);
1784 1785

    /* dirty the off-screen surfaces */
1786
    for (i = 0; i < qxl->ssd.num_surfaces; i++) {
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
        QXLSurfaceCmd *cmd;
        intptr_t surface_offset;
        int surface_size;

        if (qxl->guest_surfaces.cmds[i] == 0) {
            continue;
        }

        cmd = qxl_phys2virt(qxl, qxl->guest_surfaces.cmds[i],
                            MEMSLOT_GROUP_GUEST);
1797
        assert(cmd);
1798 1799 1800 1801
        assert(cmd->type == QXL_SURFACE_CMD_CREATE);
        surface_offset = (intptr_t)qxl_phys2virt(qxl,
                                                 cmd->u.surface_create.data,
                                                 MEMSLOT_GROUP_GUEST);
1802
        assert(surface_offset);
1803 1804 1805
        surface_offset -= vram_start;
        surface_size = cmd->u.surface_create.height *
                       abs(cmd->u.surface_create.stride);
A
Alon Levy 已提交
1806
        trace_qxl_surfaces_dirty(qxl->id, i, (int)surface_offset, surface_size);
1807 1808 1809 1810
        qxl_set_dirty(&qxl->vram_bar, surface_offset, surface_size);
    }
}

1811 1812
static void qxl_vm_change_state_handler(void *opaque, int running,
                                        RunState state)
G
Gerd Hoffmann 已提交
1813 1814 1815
{
    PCIQXLDevice *qxl = opaque;

1816 1817 1818
    if (running) {
        /*
         * if qxl_send_events was called from spice server context before
1819
         * migration ended, qxl_update_irq for these events might not have been
1820 1821
         * called
         */
1822
         qxl_update_irq(qxl);
1823 1824 1825
    } else {
        /* make sure surfaces are saved before migration */
        qxl_dirty_surfaces(qxl);
G
Gerd Hoffmann 已提交
1826 1827 1828 1829 1830
    }
}

/* display change listener */

1831 1832
static void display_update(DisplayChangeListener *dcl,
                           int x, int y, int w, int h)
G
Gerd Hoffmann 已提交
1833
{
G
Gerd Hoffmann 已提交
1834 1835 1836 1837
    PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);

    if (qxl->mode == QXL_MODE_VGA) {
        qemu_spice_display_update(&qxl->ssd, x, y, w, h);
G
Gerd Hoffmann 已提交
1838 1839 1840
    }
}

1841 1842
static void display_switch(DisplayChangeListener *dcl,
                           struct DisplaySurface *surface)
G
Gerd Hoffmann 已提交
1843
{
G
Gerd Hoffmann 已提交
1844 1845
    PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);

G
Gerd Hoffmann 已提交
1846
    qxl->ssd.ds = surface;
G
Gerd Hoffmann 已提交
1847
    if (qxl->mode == QXL_MODE_VGA) {
1848
        qemu_spice_display_switch(&qxl->ssd, surface);
G
Gerd Hoffmann 已提交
1849 1850 1851
    }
}

1852
static void display_refresh(DisplayChangeListener *dcl)
G
Gerd Hoffmann 已提交
1853
{
G
Gerd Hoffmann 已提交
1854 1855 1856 1857
    PCIQXLDevice *qxl = container_of(dcl, PCIQXLDevice, ssd.dcl);

    if (qxl->mode == QXL_MODE_VGA) {
        qemu_spice_display_refresh(&qxl->ssd);
G
Gerd Hoffmann 已提交
1858 1859 1860
    }
}

1861 1862
static DisplayChangeListenerOps display_listener_ops = {
    .dpy_name        = "spice/qxl",
1863
    .dpy_gfx_update  = display_update,
1864
    .dpy_gfx_switch  = display_switch,
1865
    .dpy_refresh     = display_refresh,
G
Gerd Hoffmann 已提交
1866 1867
};

1868
static void qxl_init_ramsize(PCIQXLDevice *qxl)
1869
{
1870 1871 1872 1873
    /* vga mode framebuffer / primary surface (bar 0, first part) */
    if (qxl->vgamem_size_mb < 8) {
        qxl->vgamem_size_mb = 8;
    }
1874 1875 1876 1877 1878 1879
    /* XXX: we round vgamem_size_mb up to a nearest power of two and it must be
     * less than vga_common_init()'s maximum on qxl->vga.vram_size (512 now).
     */
    if (qxl->vgamem_size_mb > 256) {
        qxl->vgamem_size_mb = 256;
    }
1880 1881 1882
    qxl->vgamem_size = qxl->vgamem_size_mb * 1024 * 1024;

    /* vga ram (bar 0, total) */
1883 1884 1885
    if (qxl->ram_size_mb != -1) {
        qxl->vga.vram_size = qxl->ram_size_mb * 1024 * 1024;
    }
1886 1887
    if (qxl->vga.vram_size < qxl->vgamem_size * 2) {
        qxl->vga.vram_size = qxl->vgamem_size * 2;
1888 1889
    }

G
Gerd Hoffmann 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898
    /* vram32 (surfaces, 32bit, bar 1) */
    if (qxl->vram32_size_mb != -1) {
        qxl->vram32_size = qxl->vram32_size_mb * 1024 * 1024;
    }
    if (qxl->vram32_size < 4096) {
        qxl->vram32_size = 4096;
    }

    /* vram (surfaces, 64bit, bar 4+5) */
1899 1900 1901
    if (qxl->vram_size_mb != -1) {
        qxl->vram_size = qxl->vram_size_mb * 1024 * 1024;
    }
G
Gerd Hoffmann 已提交
1902 1903
    if (qxl->vram_size < qxl->vram32_size) {
        qxl->vram_size = qxl->vram32_size;
1904
    }
G
Gerd Hoffmann 已提交
1905

1906
    if (qxl->revision == 1) {
G
Gerd Hoffmann 已提交
1907
        qxl->vram32_size = 4096;
1908 1909
        qxl->vram_size = 4096;
    }
1910 1911 1912 1913
    qxl->vgamem_size = pow2ceil(qxl->vgamem_size);
    qxl->vga.vram_size = pow2ceil(qxl->vga.vram_size);
    qxl->vram32_size = pow2ceil(qxl->vram32_size);
    qxl->vram_size = pow2ceil(qxl->vram_size);
1914 1915
}

M
Markus Armbruster 已提交
1916
static void qxl_realize_common(PCIQXLDevice *qxl, Error **errp)
G
Gerd Hoffmann 已提交
1917 1918 1919 1920 1921 1922 1923 1924
{
    uint8_t* config = qxl->pci.config;
    uint32_t pci_device_rev;
    uint32_t io_size;

    qxl->mode = QXL_MODE_UNDEFINED;
    qxl->generation = 1;
    qxl->num_memslots = NUM_MEMSLOTS;
1925
    qemu_mutex_init(&qxl->track_lock);
1926 1927
    qemu_mutex_init(&qxl->async_lock);
    qxl->current_async = QXL_UNDEFINED_IO;
1928
    qxl->guest_bug = 0;
G
Gerd Hoffmann 已提交
1929 1930 1931 1932

    switch (qxl->revision) {
    case 1: /* spice 0.4 -- qxl-1 */
        pci_device_rev = QXL_REVISION_STABLE_V04;
1933
        io_size = 8;
G
Gerd Hoffmann 已提交
1934 1935 1936
        break;
    case 2: /* spice 0.6 -- qxl-2 */
        pci_device_rev = QXL_REVISION_STABLE_V06;
1937
        io_size = 16;
G
Gerd Hoffmann 已提交
1938
        break;
G
Gerd Hoffmann 已提交
1939
    case 3: /* qxl-3 */
1940 1941 1942 1943 1944
        pci_device_rev = QXL_REVISION_STABLE_V10;
        io_size = 32; /* PCI region size must be pow2 */
        break;
    case 4: /* qxl-4 */
        pci_device_rev = QXL_REVISION_STABLE_V12;
1945
        io_size = pow2ceil(QXL_IO_RANGE_SIZE);
G
Gerd Hoffmann 已提交
1946
        break;
A
Alon Levy 已提交
1947
    default:
M
Markus Armbruster 已提交
1948 1949 1950
        error_setg(errp, "Invalid revision %d for qxl device (max %d)",
                   qxl->revision, QXL_DEFAULT_REVISION);
        return;
G
Gerd Hoffmann 已提交
1951 1952 1953 1954 1955 1956
    }

    pci_set_byte(&config[PCI_REVISION_ID], pci_device_rev);
    pci_set_byte(&config[PCI_INTERRUPT_PIN], 1);

    qxl->rom_size = qxl_rom_size();
1957
    memory_region_init_ram(&qxl->rom_bar, OBJECT(qxl), "qxl.vrom",
1958
                           qxl->rom_size, &error_abort);
1959
    vmstate_register_ram(&qxl->rom_bar, &qxl->pci.qdev);
G
Gerd Hoffmann 已提交
1960 1961 1962
    init_qxl_rom(qxl);
    init_qxl_ram(qxl);

1963
    qxl->guest_surfaces.cmds = g_new0(QXLPHYSICAL, qxl->ssd.num_surfaces);
1964
    memory_region_init_ram(&qxl->vram_bar, OBJECT(qxl), "qxl.vram",
1965
                           qxl->vram_size, &error_abort);
1966
    vmstate_register_ram(&qxl->vram_bar, &qxl->pci.qdev);
1967 1968
    memory_region_init_alias(&qxl->vram32_bar, OBJECT(qxl), "qxl.vram32",
                             &qxl->vram_bar, 0, qxl->vram32_size);
G
Gerd Hoffmann 已提交
1969

1970
    memory_region_init_io(&qxl->io_bar, OBJECT(qxl), &qxl_io_ops, qxl,
1971 1972 1973 1974
                          "qxl-ioports", io_size);
    if (qxl->id == 0) {
        vga_dirty_log_start(&qxl->vga);
    }
1975
    memory_region_set_flush_coalesced(&qxl->io_bar);
1976 1977


1978 1979
    pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX,
                     PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar);
G
Gerd Hoffmann 已提交
1980

1981 1982
    pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX,
                     PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar);
G
Gerd Hoffmann 已提交
1983

1984 1985
    pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX,
                     PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram);
G
Gerd Hoffmann 已提交
1986

1987
    pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX,
G
Gerd Hoffmann 已提交
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
                     PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vram32_bar);

    if (qxl->vram32_size < qxl->vram_size) {
        /*
         * Make the 64bit vram bar show up only in case it is
         * configured to be larger than the 32bit vram bar.
         */
        pci_register_bar(&qxl->pci, QXL_VRAM64_RANGE_INDEX,
                         PCI_BASE_ADDRESS_SPACE_MEMORY |
                         PCI_BASE_ADDRESS_MEM_TYPE_64 |
                         PCI_BASE_ADDRESS_MEM_PREFETCH,
                         &qxl->vram_bar);
    }

    /* print pci bar details */
    dprint(qxl, 1, "ram/%s: %d MB [region 0]\n",
           qxl->id == 0 ? "pri" : "sec",
           qxl->vga.vram_size / (1024*1024));
    dprint(qxl, 1, "vram/32: %d MB [region 1]\n",
           qxl->vram32_size / (1024*1024));
    dprint(qxl, 1, "vram/64: %d MB %s\n",
           qxl->vram_size / (1024*1024),
           qxl->vram32_size < qxl->vram_size ? "[region 4]" : "[unmapped]");
G
Gerd Hoffmann 已提交
2011 2012

    qxl->ssd.qxl.base.sif = &qxl_interface.base;
G
Gerd Hoffmann 已提交
2013
    if (qemu_spice_add_display_interface(&qxl->ssd.qxl, qxl->vga.con) != 0) {
M
Markus Armbruster 已提交
2014 2015 2016
        error_setg(errp, "qxl interface %d.%d not supported by spice-server",
                   SPICE_INTERFACE_QXL_MAJOR, SPICE_INTERFACE_QXL_MINOR);
        return;
2017
    }
G
Gerd Hoffmann 已提交
2018 2019
    qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl);

2020
    qxl->update_irq = qemu_bh_new(qxl_update_irq_bh, qxl);
G
Gerd Hoffmann 已提交
2021 2022
    qxl_reset_state(qxl);

A
Alon Levy 已提交
2023
    qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);
2024
    qxl->ssd.cursor_bh = qemu_bh_new(qemu_spice_cursor_refresh_bh, &qxl->ssd);
G
Gerd Hoffmann 已提交
2025 2026
}

M
Markus Armbruster 已提交
2027
static void qxl_realize_primary(PCIDevice *dev, Error **errp)
G
Gerd Hoffmann 已提交
2028
{
G
Gonglei 已提交
2029
    PCIQXLDevice *qxl = PCI_QXL(dev);
G
Gerd Hoffmann 已提交
2030
    VGACommonState *vga = &qxl->vga;
M
Markus Armbruster 已提交
2031
    Error *local_err = NULL;
G
Gerd Hoffmann 已提交
2032 2033

    qxl->id = 0;
2034
    qxl_init_ramsize(qxl);
2035
    vga->vbe_size = qxl->vgamem_size;
G
Gerd Hoffmann 已提交
2036
    vga->vram_size_mb = qxl->vga.vram_size >> 20;
G
Gerd Hoffmann 已提交
2037
    vga_common_init(vga, OBJECT(dev), true);
P
Paolo Bonzini 已提交
2038 2039
    vga_init(vga, OBJECT(dev),
             pci_address_space(dev), pci_address_space_io(dev), false);
2040
    portio_list_init(&qxl->vga_port_list, OBJECT(dev), qxl_vga_portio_list,
2041
                     vga, "vga");
2042 2043
    portio_list_set_flush_coalesced(&qxl->vga_port_list);
    portio_list_add(&qxl->vga_port_list, pci_address_space_io(dev), 0x3b0);
G
Gerd Hoffmann 已提交
2044

2045
    vga->con = graphic_console_init(DEVICE(dev), 0, &qxl_ops, qxl);
2046
    qemu_spice_display_init_common(&qxl->ssd);
G
Gerd Hoffmann 已提交
2047

M
Markus Armbruster 已提交
2048 2049 2050 2051
    qxl_realize_common(qxl, &local_err);
    if (local_err) {
        error_propagate(errp, local_err);
        return;
G
Gerd Hoffmann 已提交
2052 2053
    }

2054
    qxl->ssd.dcl.ops = &display_listener_ops;
2055
    qxl->ssd.dcl.con = vga->con;
2056
    register_displaychangelistener(&qxl->ssd.dcl);
G
Gerd Hoffmann 已提交
2057 2058
}

M
Markus Armbruster 已提交
2059
static void qxl_realize_secondary(PCIDevice *dev, Error **errp)
G
Gerd Hoffmann 已提交
2060 2061
{
    static int device_id = 1;
G
Gonglei 已提交
2062
    PCIQXLDevice *qxl = PCI_QXL(dev);
G
Gerd Hoffmann 已提交
2063 2064

    qxl->id = device_id++;
2065
    qxl_init_ramsize(qxl);
2066
    memory_region_init_ram(&qxl->vga.vram, OBJECT(dev), "qxl.vgavram",
2067
                           qxl->vga.vram_size, &error_abort);
2068
    vmstate_register_ram(&qxl->vga.vram, &qxl->pci.qdev);
2069
    qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram);
2070
    qxl->vga.con = graphic_console_init(DEVICE(dev), 0, &qxl_ops, qxl);
G
Gerd Hoffmann 已提交
2071

M
Markus Armbruster 已提交
2072
    qxl_realize_common(qxl, errp);
G
Gerd Hoffmann 已提交
2073 2074 2075 2076 2077 2078 2079
}

static void qxl_pre_save(void *opaque)
{
    PCIQXLDevice* d = opaque;
    uint8_t *ram_start = d->vga.vram_ptr;

A
Alon Levy 已提交
2080
    trace_qxl_pre_save(d->id);
G
Gerd Hoffmann 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
    if (d->last_release == NULL) {
        d->last_release_offset = 0;
    } else {
        d->last_release_offset = (uint8_t *)d->last_release - ram_start;
    }
    assert(d->last_release_offset < d->vga.vram_size);
}

static int qxl_pre_load(void *opaque)
{
    PCIQXLDevice* d = opaque;

A
Alon Levy 已提交
2093
    trace_qxl_pre_load(d->id);
G
Gerd Hoffmann 已提交
2094 2095 2096 2097 2098
    qxl_hard_reset(d, 1);
    qxl_exit_vga_mode(d);
    return 0;
}

2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
static void qxl_create_memslots(PCIQXLDevice *d)
{
    int i;

    for (i = 0; i < NUM_MEMSLOTS; i++) {
        if (!d->guest_slots[i].active) {
            continue;
        }
        qxl_add_memslot(d, i, 0, QXL_SYNC);
    }
}

G
Gerd Hoffmann 已提交
2111 2112 2113 2114 2115
static int qxl_post_load(void *opaque, int version)
{
    PCIQXLDevice* d = opaque;
    uint8_t *ram_start = d->vga.vram_ptr;
    QXLCommandExt *cmds;
2116
    int in, out, newmode;
G
Gerd Hoffmann 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126

    assert(d->last_release_offset < d->vga.vram_size);
    if (d->last_release_offset == 0) {
        d->last_release = NULL;
    } else {
        d->last_release = (QXLReleaseInfo *)(ram_start + d->last_release_offset);
    }

    d->modes = (QXLModes*)((uint8_t*)d->rom + d->rom->modes_offset);

A
Alon Levy 已提交
2127
    trace_qxl_post_load(d->id, qxl_mode_to_string(d->mode));
G
Gerd Hoffmann 已提交
2128 2129
    newmode = d->mode;
    d->mode = QXL_MODE_UNDEFINED;
2130

G
Gerd Hoffmann 已提交
2131 2132
    switch (newmode) {
    case QXL_MODE_UNDEFINED:
2133
        qxl_create_memslots(d);
G
Gerd Hoffmann 已提交
2134 2135
        break;
    case QXL_MODE_VGA:
2136
        qxl_create_memslots(d);
G
Gerd Hoffmann 已提交
2137 2138 2139
        qxl_enter_vga_mode(d);
        break;
    case QXL_MODE_NATIVE:
2140
        qxl_create_memslots(d);
2141
        qxl_create_guest_primary(d, 1, QXL_SYNC);
G
Gerd Hoffmann 已提交
2142 2143

        /* replay surface-create and cursor-set commands */
2144 2145
        cmds = g_malloc0(sizeof(QXLCommandExt) * (d->ssd.num_surfaces + 1));
        for (in = 0, out = 0; in < d->ssd.num_surfaces; in++) {
G
Gerd Hoffmann 已提交
2146 2147 2148 2149 2150 2151 2152 2153
            if (d->guest_surfaces.cmds[in] == 0) {
                continue;
            }
            cmds[out].cmd.data = d->guest_surfaces.cmds[in];
            cmds[out].cmd.type = QXL_CMD_SURFACE;
            cmds[out].group_id = MEMSLOT_GROUP_GUEST;
            out++;
        }
Y
Yonit Halperin 已提交
2154 2155 2156 2157 2158 2159
        if (d->guest_cursor) {
            cmds[out].cmd.data = d->guest_cursor;
            cmds[out].cmd.type = QXL_CMD_CURSOR;
            cmds[out].group_id = MEMSLOT_GROUP_GUEST;
            out++;
        }
G
Gerd Hoffmann 已提交
2160
        qxl_spice_loadvm_commands(d, cmds, out);
2161
        g_free(cmds);
2162 2163 2164
        if (d->guest_monitors_config) {
            qxl_spice_monitors_config_async(d, 1);
        }
G
Gerd Hoffmann 已提交
2165 2166
        break;
    case QXL_MODE_COMPAT:
2167 2168
        /* note: no need to call qxl_create_memslots, qxl_set_mode
         * creates the mem slot. */
G
Gerd Hoffmann 已提交
2169 2170 2171 2172 2173 2174
        qxl_set_mode(d, d->shadow_rom.mode, 1);
        break;
    }
    return 0;
}

2175
#define QXL_SAVE_VERSION 21
G
Gerd Hoffmann 已提交
2176

2177 2178 2179 2180 2181 2182 2183 2184
static bool qxl_monitors_config_needed(void *opaque)
{
    PCIQXLDevice *qxl = opaque;

    return qxl->guest_monitors_config != 0;
}


G
Gerd Hoffmann 已提交
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
static VMStateDescription qxl_memslot = {
    .name               = "qxl-memslot",
    .version_id         = QXL_SAVE_VERSION,
    .minimum_version_id = QXL_SAVE_VERSION,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(slot.mem_start, struct guest_slots),
        VMSTATE_UINT64(slot.mem_end,   struct guest_slots),
        VMSTATE_UINT32(active,         struct guest_slots),
        VMSTATE_END_OF_LIST()
    }
};

static VMStateDescription qxl_surface = {
    .name               = "qxl-surface",
    .version_id         = QXL_SAVE_VERSION,
    .minimum_version_id = QXL_SAVE_VERSION,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(width,      QXLSurfaceCreate),
        VMSTATE_UINT32(height,     QXLSurfaceCreate),
        VMSTATE_INT32(stride,      QXLSurfaceCreate),
        VMSTATE_UINT32(format,     QXLSurfaceCreate),
        VMSTATE_UINT32(position,   QXLSurfaceCreate),
        VMSTATE_UINT32(mouse_mode, QXLSurfaceCreate),
        VMSTATE_UINT32(flags,      QXLSurfaceCreate),
        VMSTATE_UINT32(type,       QXLSurfaceCreate),
        VMSTATE_UINT64(mem,        QXLSurfaceCreate),
        VMSTATE_END_OF_LIST()
    }
};

2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
static VMStateDescription qxl_vmstate_monitors_config = {
    .name               = "qxl/monitors-config",
    .version_id         = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(guest_monitors_config, PCIQXLDevice),
        VMSTATE_END_OF_LIST()
    },
};

G
Gerd Hoffmann 已提交
2225 2226 2227 2228 2229 2230 2231
static VMStateDescription qxl_vmstate = {
    .name               = "qxl",
    .version_id         = QXL_SAVE_VERSION,
    .minimum_version_id = QXL_SAVE_VERSION,
    .pre_save           = qxl_pre_save,
    .pre_load           = qxl_pre_load,
    .post_load          = qxl_post_load,
2232
    .fields = (VMStateField[]) {
G
Gerd Hoffmann 已提交
2233 2234 2235 2236 2237 2238 2239
        VMSTATE_PCI_DEVICE(pci, PCIQXLDevice),
        VMSTATE_STRUCT(vga, PCIQXLDevice, 0, vmstate_vga_common, VGACommonState),
        VMSTATE_UINT32(shadow_rom.mode, PCIQXLDevice),
        VMSTATE_UINT32(num_free_res, PCIQXLDevice),
        VMSTATE_UINT32(last_release_offset, PCIQXLDevice),
        VMSTATE_UINT32(mode, PCIQXLDevice),
        VMSTATE_UINT32(ssd.unique, PCIQXLDevice),
2240 2241 2242 2243 2244
        VMSTATE_INT32_EQUAL(num_memslots, PCIQXLDevice),
        VMSTATE_STRUCT_ARRAY(guest_slots, PCIQXLDevice, NUM_MEMSLOTS, 0,
                             qxl_memslot, struct guest_slots),
        VMSTATE_STRUCT(guest_primary.surface, PCIQXLDevice, 0,
                       qxl_surface, QXLSurfaceCreate),
2245 2246 2247 2248
        VMSTATE_INT32_EQUAL(ssd.num_surfaces, PCIQXLDevice),
        VMSTATE_VARRAY_INT32(guest_surfaces.cmds, PCIQXLDevice,
                             ssd.num_surfaces, 0,
                             vmstate_info_uint64, uint64_t),
2249
        VMSTATE_UINT64(guest_cursor, PCIQXLDevice),
G
Gerd Hoffmann 已提交
2250 2251
        VMSTATE_END_OF_LIST()
    },
2252 2253 2254 2255 2256 2257 2258 2259
    .subsections = (VMStateSubsection[]) {
        {
            .vmsd = &qxl_vmstate_monitors_config,
            .needed = qxl_monitors_config_needed,
        }, {
            /* empty */
        }
    }
G
Gerd Hoffmann 已提交
2260 2261
};

G
Gerd Hoffmann 已提交
2262 2263 2264
static Property qxl_properties[] = {
        DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size,
                           64 * 1024 * 1024),
G
Gerd Hoffmann 已提交
2265
        DEFINE_PROP_UINT32("vram_size", PCIQXLDevice, vram32_size,
G
Gerd Hoffmann 已提交
2266 2267 2268 2269 2270 2271
                           64 * 1024 * 1024),
        DEFINE_PROP_UINT32("revision", PCIQXLDevice, revision,
                           QXL_DEFAULT_REVISION),
        DEFINE_PROP_UINT32("debug", PCIQXLDevice, debug, 0),
        DEFINE_PROP_UINT32("guestdebug", PCIQXLDevice, guestdebug, 0),
        DEFINE_PROP_UINT32("cmdlog", PCIQXLDevice, cmdlog, 0),
2272
        DEFINE_PROP_UINT32("ram_size_mb",  PCIQXLDevice, ram_size_mb, -1),
2273 2274
        DEFINE_PROP_UINT32("vram_size_mb", PCIQXLDevice, vram32_size_mb, -1),
        DEFINE_PROP_UINT32("vram64_size_mb", PCIQXLDevice, vram_size_mb, -1),
G
Gerd Hoffmann 已提交
2275
        DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice, vgamem_size_mb, 16),
2276
        DEFINE_PROP_INT32("surfaces", PCIQXLDevice, ssd.num_surfaces, 1024),
G
Gerd Hoffmann 已提交
2277 2278 2279
        DEFINE_PROP_END_OF_LIST(),
};

G
Gonglei 已提交
2280
static void qxl_pci_class_init(ObjectClass *klass, void *data)
2281
{
2282
    DeviceClass *dc = DEVICE_CLASS(klass);
2283 2284 2285 2286
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->vendor_id = REDHAT_PCI_VENDOR_ID;
    k->device_id = QXL_DEVICE_ID_STABLE;
2287
    set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
2288 2289 2290
    dc->reset = qxl_reset_handler;
    dc->vmsd = &qxl_vmstate;
    dc->props = qxl_properties;
G
Gonglei 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
}

static const TypeInfo qxl_pci_type_info = {
    .name = TYPE_PCI_QXL,
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIQXLDevice),
    .abstract = true,
    .class_init = qxl_pci_class_init,
};

static void qxl_primary_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->realize = qxl_realize_primary;
    k->romfile = "vgabios-qxl.bin";
    k->class_id = PCI_CLASS_DISPLAY_VGA;
    dc->desc = "Spice QXL GPU (primary, vga compatible)";
2310
    dc->hotpluggable = false;
2311 2312
}

2313
static const TypeInfo qxl_primary_info = {
2314
    .name          = "qxl-vga",
G
Gonglei 已提交
2315
    .parent        = TYPE_PCI_QXL,
2316
    .class_init    = qxl_primary_class_init,
G
Gerd Hoffmann 已提交
2317 2318
};

2319 2320
static void qxl_secondary_class_init(ObjectClass *klass, void *data)
{
2321
    DeviceClass *dc = DEVICE_CLASS(klass);
2322 2323
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

M
Markus Armbruster 已提交
2324
    k->realize = qxl_realize_secondary;
2325
    k->class_id = PCI_CLASS_DISPLAY_OTHER;
2326
    dc->desc = "Spice QXL GPU (secondary)";
2327 2328
}

2329
static const TypeInfo qxl_secondary_info = {
2330
    .name          = "qxl",
G
Gonglei 已提交
2331
    .parent        = TYPE_PCI_QXL,
2332
    .class_init    = qxl_secondary_class_init,
G
Gerd Hoffmann 已提交
2333 2334
};

A
Andreas Färber 已提交
2335
static void qxl_register_types(void)
G
Gerd Hoffmann 已提交
2336
{
G
Gonglei 已提交
2337
    type_register_static(&qxl_pci_type_info);
2338 2339
    type_register_static(&qxl_primary_info);
    type_register_static(&qxl_secondary_info);
G
Gerd Hoffmann 已提交
2340 2341
}

A
Andreas Färber 已提交
2342
type_init(qxl_register_types)