qxl.c 72.1 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 21 22 23 24 25
/*
 * 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/>.
 */

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

#include "qxl.h"

30 31 32 33 34
#ifndef CONFIG_QXL_IO_MONITORS_CONFIG_ASYNC
/* spice-protocol is too old, add missing definitions */
#define QXL_IO_MONITORS_CONFIG_ASYNC (QXL_IO_FLUSH_RELEASE + 1)
#endif

35 36 37
/*
 * 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
38
 * abort we just qxl_set_guest_bug and set the return to NULL. Still
39 40
 * it may happen as a result of emulator bug as well.
 */
G
Gerd Hoffmann 已提交
41
#undef SPICE_RING_PROD_ITEM
42
#define SPICE_RING_PROD_ITEM(qxl, r, ret) {                             \
G
Gerd Hoffmann 已提交
43 44 45 46 47
        typeof(r) start = r;                                            \
        typeof(r) end = r + 1;                                          \
        uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r);           \
        typeof(&(r)->items[prod]) m_item = &(r)->items[prod];           \
        if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \
48
            qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
49 50 51 52 53
                          "! %p <= %p < %p", (uint8_t *)start,          \
                          (uint8_t *)m_item, (uint8_t *)end);           \
            ret = NULL;                                                 \
        } else {                                                        \
            ret = &m_item->el;                                          \
G
Gerd Hoffmann 已提交
54 55 56 57
        }                                                               \
    }

#undef SPICE_RING_CONS_ITEM
58
#define SPICE_RING_CONS_ITEM(qxl, r, ret) {                             \
G
Gerd Hoffmann 已提交
59 60 61 62 63
        typeof(r) start = r;                                            \
        typeof(r) end = r + 1;                                          \
        uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r);           \
        typeof(&(r)->items[cons]) m_item = &(r)->items[cons];           \
        if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \
64
            qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \
65 66 67 68 69
                          "! %p <= %p < %p", (uint8_t *)start,          \
                          (uint8_t *)m_item, (uint8_t *)end);           \
            ret = NULL;                                                 \
        } else {                                                        \
            ret = &m_item->el;                                          \
G
Gerd Hoffmann 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }                                                               \
    }

#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),              \
    QXL_MODE_16_32(y_res, x_res, 1),              \
    QXL_MODE_16_32(x_res, y_res, 2),              \
    QXL_MODE_16_32(y_res, x_res, 3)

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),
    QXL_MODE_EX(2048, 1536),
    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),
};

static PCIQXLDevice *qxl0;

static void qxl_send_events(PCIQXLDevice *d, uint32_t events);
137
static int qxl_destroy_primary(PCIQXLDevice *d, qxl_async_io async);
G
Gerd Hoffmann 已提交
138 139 140 141
static void qxl_reset_memslots(PCIQXLDevice *d);
static void qxl_reset_surfaces(PCIQXLDevice *d);
static void qxl_ring_set_dirty(PCIQXLDevice *qxl);

142
void qxl_set_guest_bug(PCIQXLDevice *qxl, const char *msg, ...)
143 144
{
    qxl_send_events(qxl, QXL_INTERRUPT_ERROR);
145
    qxl->guest_bug = 1;
146
    if (qxl->guestdebug) {
147 148 149 150 151 152
        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);
153 154 155
    }
}

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

void qxl_spice_update_area(PCIQXLDevice *qxl, uint32_t surface_id,
                           struct QXLRect *area, struct QXLRect *dirty_rects,
                           uint32_t num_dirty_rects,
164
                           uint32_t clear_dirty_region,
A
Alon Levy 已提交
165
                           qxl_async_io async, struct QXLCookie *cookie)
G
Gerd Hoffmann 已提交
166
{
A
Alon Levy 已提交
167 168 169 170
    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);
171 172 173 174
    if (async == QXL_SYNC) {
        qxl->ssd.worker->update_area(qxl->ssd.worker, surface_id, area,
                        dirty_rects, num_dirty_rects, clear_dirty_region);
    } else {
A
Alon Levy 已提交
175
        assert(cookie != NULL);
176
        spice_qxl_update_area_async(&qxl->ssd.qxl, surface_id, area,
177
                                    clear_dirty_region, (uintptr_t)cookie);
178
    }
G
Gerd Hoffmann 已提交
179 180
}

181 182
static void qxl_spice_destroy_surface_wait_complete(PCIQXLDevice *qxl,
                                                    uint32_t id)
G
Gerd Hoffmann 已提交
183
{
A
Alon Levy 已提交
184
    trace_qxl_spice_destroy_surface_wait_complete(qxl->id, id);
185 186 187 188
    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 已提交
189 190
}

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

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

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);
G
Gerd Hoffmann 已提交
220 221 222 223 224
    qxl->ssd.worker->loadvm_commands(qxl->ssd.worker, ext, count);
}

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

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

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 240
    memset(qxl->guest_surfaces.cmds, 0,
           sizeof(qxl->guest_surfaces.cmds) * 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 253 254 255 256 257
    } else {
        qxl->ssd.worker->destroy_surfaces(qxl->ssd.worker);
        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 284 285 286 287 288 289 290
static void qxl_spice_monitors_config_async(PCIQXLDevice *qxl, int replay)
{
    trace_qxl_spice_monitors_config(qxl->id);
/* 0x000b01 == 0.11.1 */
#if SPICE_SERVER_VERSION >= 0x000b01 && \
    defined(CONFIG_QXL_IO_MONITORS_CONFIG_ASYNC)
    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));
    }
#else
    fprintf(stderr, "qxl: too old spice-protocol/spice-server for "
            "QXL_IO_MONITORS_CONFIG_ASYNC\n");
#endif
}

G
Gerd Hoffmann 已提交
291 292
void qxl_spice_reset_image_cache(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
293
    trace_qxl_spice_reset_image_cache(qxl->id);
G
Gerd Hoffmann 已提交
294 295 296 297 298
    qxl->ssd.worker->reset_image_cache(qxl->ssd.worker);
}

void qxl_spice_reset_cursor(PCIQXLDevice *qxl)
{
A
Alon Levy 已提交
299
    trace_qxl_spice_reset_cursor(qxl->id);
G
Gerd Hoffmann 已提交
300
    qxl->ssd.worker->reset_cursor(qxl->ssd.worker);
Y
Yonit Halperin 已提交
301 302 303
    qemu_mutex_lock(&qxl->track_lock);
    qxl->guest_cursor = 0;
    qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
304 305 306
}


G
Gerd Hoffmann 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static inline uint32_t msb_mask(uint32_t val)
{
    uint32_t mask;

    do {
        mask = ~(val - 1) & val;
        val &= ~mask;
    } while (mask < val);

    return mask;
}

static ram_addr_t qxl_rom_size(void)
{
    uint32_t rom_size = sizeof(QXLRom) + sizeof(QXLModes) + sizeof(qxl_modes);
322

G
Gerd Hoffmann 已提交
323 324 325 326 327 328 329
    rom_size = MAX(rom_size, TARGET_PAGE_SIZE);
    rom_size = msb_mask(rom_size * 2 - 1);
    return rom_size;
}

static void init_qxl_rom(PCIQXLDevice *d)
{
330
    QXLRom *rom = memory_region_get_ram_ptr(&d->rom_bar);
G
Gerd Hoffmann 已提交
331 332 333 334
    QXLModes *modes = (QXLModes *)(rom + 1);
    uint32_t ram_header_size;
    uint32_t surface0_area_size;
    uint32_t num_pages;
335 336
    uint32_t fb;
    int i, n;
G
Gerd Hoffmann 已提交
337 338 339 340 341 342 343 344 345 346 347 348

    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;
349
    rom->n_surfaces    = cpu_to_le32(d->ssd.num_surfaces);
G
Gerd Hoffmann 已提交
350

351
    for (i = 0, n = 0; i < ARRAY_SIZE(qxl_modes); i++) {
G
Gerd Hoffmann 已提交
352
        fb = qxl_modes[i].y_res * qxl_modes[i].stride;
353 354
        if (fb > d->vgamem_size) {
            continue;
G
Gerd Hoffmann 已提交
355
        }
356 357 358 359 360 361 362 363 364 365 366
        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 已提交
367 368

    ram_header_size    = ALIGN(sizeof(QXLRam), 4096);
369
    surface0_area_size = ALIGN(d->vgamem_size, 4096);
G
Gerd Hoffmann 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    num_pages          = d->vga.vram_size;
    num_pages         -= ram_header_size;
    num_pages         -= surface0_area_size;
    num_pages          = num_pages / TARGET_PAGE_SIZE;

    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 已提交
396
    d->ram->update_surface = 0;
G
Gerd Hoffmann 已提交
397 398 399
    SPICE_RING_INIT(&d->ram->cmd_ring);
    SPICE_RING_INIT(&d->ram->cursor_ring);
    SPICE_RING_INIT(&d->ram->release_ring);
400 401
    SPICE_RING_PROD_ITEM(d, &d->ram->release_ring, item);
    assert(item);
G
Gerd Hoffmann 已提交
402 403 404 405 406
    *item = 0;
    qxl_ring_set_dirty(d);
}

/* can be called from spice server thread context */
407
static void qxl_set_dirty(MemoryRegion *mr, ram_addr_t addr, ram_addr_t end)
G
Gerd Hoffmann 已提交
408
{
409
    memory_region_set_dirty(mr, addr, end - addr);
G
Gerd Hoffmann 已提交
410 411 412 413
}

static void qxl_rom_set_dirty(PCIQXLDevice *qxl)
{
414
    qxl_set_dirty(&qxl->rom_bar, 0, qxl->rom_size);
G
Gerd Hoffmann 已提交
415 416 417 418 419 420 421 422 423 424 425
}

/* 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;
    offset &= ~(TARGET_PAGE_SIZE-1);
    assert(offset < qxl->vga.vram_size);
426
    qxl_set_dirty(&qxl->vga.vram, offset, offset + TARGET_PAGE_SIZE);
G
Gerd Hoffmann 已提交
427 428 429 430 431
}

/* can be called from spice server thread context */
static void qxl_ring_set_dirty(PCIQXLDevice *qxl)
{
432 433 434
    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 已提交
435 436 437 438 439 440
}

/*
 * keep track of some command state, for savevm/loadvm.
 * called from spice server thread context only
 */
441
static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
G
Gerd Hoffmann 已提交
442 443 444 445 446
{
    switch (le32_to_cpu(ext->cmd.type)) {
    case QXL_CMD_SURFACE:
    {
        QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
447 448 449 450

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

453
        if (id >= qxl->ssd.num_surfaces) {
454
            qxl_set_guest_bug(qxl, "QXL_CMD_SURFACE id %d >= %d", id,
455
                              qxl->ssd.num_surfaces);
456 457
            return 1;
        }
458
        qemu_mutex_lock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
459 460 461 462 463 464 465 466 467 468
        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--;
        }
469
        qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
470 471 472 473 474
        break;
    }
    case QXL_CMD_CURSOR:
    {
        QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
475 476 477 478

        if (!cmd) {
            return 1;
        }
G
Gerd Hoffmann 已提交
479
        if (cmd->type == QXL_CURSOR_SET) {
Y
Yonit Halperin 已提交
480
            qemu_mutex_lock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
481
            qxl->guest_cursor = ext->cmd.data;
Y
Yonit Halperin 已提交
482
            qemu_mutex_unlock(&qxl->track_lock);
G
Gerd Hoffmann 已提交
483 484 485 486
        }
        break;
    }
    }
487
    return 0;
G
Gerd Hoffmann 已提交
488 489 490 491 492 493 494 495
}

/* 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 已提交
496
    trace_qxl_interface_attach_worker(qxl->id);
G
Gerd Hoffmann 已提交
497 498 499 500 501 502 503
    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 已提交
504
    trace_qxl_interface_set_compression_level(qxl->id, level);
G
Gerd Hoffmann 已提交
505 506 507 508 509 510 511 512 513
    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 已提交
514
    trace_qxl_interface_set_mm_time(qxl->id, mm_time);
G
Gerd Hoffmann 已提交
515 516 517 518 519 520 521 522 523
    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 已提交
524
    trace_qxl_interface_get_init_info(qxl->id);
G
Gerd Hoffmann 已提交
525 526 527 528 529 530
    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;
    info->qxl_ram_size = le32_to_cpu(qxl->shadow_rom.num_pages) << TARGET_PAGE_BITS;
531
    info->n_surfaces = qxl->ssd.num_surfaces;
G
Gerd Hoffmann 已提交
532 533
}

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
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 已提交
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 574 575 576 577 578 579
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",
580
        [QXL_IO_MONITORS_CONFIG_ASYNC]  = "QXL_IO_MONITORS_CONFIG_ASYNC",
A
Alon Levy 已提交
581 582 583 584
    };
    return io_port_to_string[io_port];
}

G
Gerd Hoffmann 已提交
585 586 587 588 589 590 591
/* 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;
592
    int notify, ret;
G
Gerd Hoffmann 已提交
593

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

G
Gerd Hoffmann 已提交
596 597
    switch (qxl->mode) {
    case QXL_MODE_VGA:
598 599 600 601 602 603 604
        ret = false;
        qemu_mutex_lock(&qxl->ssd.lock);
        if (qxl->ssd.update != NULL) {
            update = qxl->ssd.update;
            qxl->ssd.update = NULL;
            *ext = update->ext;
            ret = true;
G
Gerd Hoffmann 已提交
605
        }
606
        qemu_mutex_unlock(&qxl->ssd.lock);
A
Alon Levy 已提交
607
        if (ret) {
A
Alon Levy 已提交
608
            trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
A
Alon Levy 已提交
609 610
            qxl_log_command(qxl, "vga", ext);
        }
611
        return ret;
G
Gerd Hoffmann 已提交
612 613 614 615
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
    case QXL_MODE_UNDEFINED:
        ring = &qxl->ram->cmd_ring;
616
        if (qxl->guest_bug || SPICE_RING_IS_EMPTY(ring)) {
G
Gerd Hoffmann 已提交
617 618
            return false;
        }
619 620 621 622
        SPICE_RING_CONS_ITEM(qxl, ring, cmd);
        if (!cmd) {
            return false;
        }
G
Gerd Hoffmann 已提交
623 624 625 626 627 628 629 630 631 632 633
        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);
634
        trace_qxl_ring_command_get(qxl->id, qxl_mode_to_string(qxl->mode));
G
Gerd Hoffmann 已提交
635 636 637 638 639 640 641 642 643 644 645 646
        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 已提交
647
    trace_qxl_ring_command_req_notification(qxl->id);
G
Gerd Hoffmann 已提交
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 679 680 681 682 683 684
    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 已提交
685 686 687 688 689
    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 已提交
690 691 692
    if (notify) {
        qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
    }
693 694 695 696
    SPICE_RING_PROD_ITEM(d, ring, item);
    if (!item) {
        return;
    }
G
Gerd Hoffmann 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    *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,
                                       struct QXLReleaseInfoExt ext)
{
    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 已提交
713
        qemu_spice_destroy_update(&qxl->ssd, (void *)(intptr_t)ext.info->id);
G
Gerd Hoffmann 已提交
714 715 716 717 718 719 720 721
        return;
    }

    /*
     * ext->info points into guest-visible memory
     * pci bar 0, $command.release_info
     */
    ring = &qxl->ram->release_ring;
722 723 724 725
    SPICE_RING_PROD_ITEM(qxl, ring, item);
    if (!item) {
        return;
    }
G
Gerd Hoffmann 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    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 已提交
742
    trace_qxl_ring_res_put(qxl->id, qxl->num_free_res);
G
Gerd Hoffmann 已提交
743 744 745 746 747 748 749 750 751 752 753
    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 已提交
754 755
    trace_qxl_ring_cursor_check(qxl->id, qxl_mode_to_string(qxl->mode));

G
Gerd Hoffmann 已提交
756 757 758 759 760 761 762 763
    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;
        }
764 765 766 767
        SPICE_RING_CONS_ITEM(qxl, ring, cmd);
        if (!cmd) {
            return false;
        }
G
Gerd Hoffmann 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781
        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 已提交
782
        trace_qxl_ring_cursor_get(qxl->id, qxl_mode_to_string(qxl->mode));
G
Gerd Hoffmann 已提交
783 784 785 786 787 788 789 790 791 792 793 794
        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 已提交
795
    trace_qxl_ring_cursor_req_notification(qxl->id);
G
Gerd Hoffmann 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    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)
{
813 814 815 816 817 818 819
    /*
     * 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 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
}

/* 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;
}

835 836 837
static void qxl_create_guest_primary_complete(PCIQXLDevice *d);

/* called from spice server thread context only */
A
Alon Levy 已提交
838
static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
839 840 841 842 843 844 845 846
{
    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 已提交
847
    trace_qxl_interface_async_complete_io(qxl->id, current_async, cookie);
A
Alon Levy 已提交
848 849 850 851 852 853
    if (!cookie) {
        fprintf(stderr, "qxl: %s: error, cookie is NULL\n", __func__);
        return;
    }
    if (cookie && current_async != cookie->io) {
        fprintf(stderr,
A
Alon Levy 已提交
854 855
                "qxl: %s: error: current_async = %d != %"
                PRId64 " = cookie->io\n", __func__, current_async, cookie->io);
A
Alon Levy 已提交
856
    }
857
    switch (current_async) {
A
Alon Levy 已提交
858 859 860 861
    case QXL_IO_MEMSLOT_ADD_ASYNC:
    case QXL_IO_DESTROY_PRIMARY_ASYNC:
    case QXL_IO_UPDATE_AREA_ASYNC:
    case QXL_IO_FLUSH_SURFACES_ASYNC:
862
    case QXL_IO_MONITORS_CONFIG_ASYNC:
A
Alon Levy 已提交
863
        break;
864 865 866 867 868 869 870
    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 已提交
871
        qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
872
        break;
A
Alon Levy 已提交
873 874 875
    default:
        fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
                current_async);
876 877 878 879
    }
    qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
}

A
Alon Levy 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893
/* 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 已提交
894 895 896
    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 已提交
897 898 899 900
    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 已提交
901 902
        trace_qxl_interface_update_area_complete_overflow(qxl->id,
                                                          QXL_NUM_DIRTY_RECTS);
A
Alon Levy 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
        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 已提交
918 919
    trace_qxl_interface_update_area_complete_schedule_bh(qxl->id,
                                                         qxl->num_dirty_rects);
A
Alon Levy 已提交
920 921 922 923
    qemu_bh_schedule(qxl->update_area_bh);
    qemu_mutex_unlock(&qxl->ssd.lock);
}

A
Alon Levy 已提交
924 925 926 927
/* 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);
928
    QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
A
Alon Levy 已提交
929 930 931 932

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

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
#if SPICE_SERVER_VERSION >= 0x000b04

/* 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);

    qxl->shadow_rom.client_present = client_present;
    memcpy(qxl->shadow_rom.client_capabilities, caps, sizeof(caps));
    qxl->rom->client_present = client_present;
    memcpy(qxl->rom->client_capabilities, caps, sizeof(caps));
    qxl_rom_set_dirty(qxl);

    qxl_send_events(qxl, QXL_INTERRUPT_CLIENT);
}

#endif

G
Gerd Hoffmann 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
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,
986
    .async_complete          = interface_async_complete,
A
Alon Levy 已提交
987
    .update_area_complete    = interface_update_area_complete,
988 989 990
#if SPICE_SERVER_VERSION >= 0x000b04
    .set_client_capabilities = interface_set_client_capabilities,
#endif
G
Gerd Hoffmann 已提交
991 992 993 994 995 996 997
};

static void qxl_enter_vga_mode(PCIQXLDevice *d)
{
    if (d->mode == QXL_MODE_VGA) {
        return;
    }
A
Alon Levy 已提交
998
    trace_qxl_enter_vga_mode(d->id);
G
Gerd Hoffmann 已提交
999 1000 1001
    qemu_spice_create_host_primary(&d->ssd);
    d->mode = QXL_MODE_VGA;
    memset(&d->ssd.dirty, 0, sizeof(d->ssd.dirty));
1002
    vga_dirty_log_start(&d->vga);
G
Gerd Hoffmann 已提交
1003 1004 1005 1006 1007 1008 1009
}

static void qxl_exit_vga_mode(PCIQXLDevice *d)
{
    if (d->mode != QXL_MODE_VGA) {
        return;
    }
A
Alon Levy 已提交
1010
    trace_qxl_exit_vga_mode(d->id);
1011
    vga_dirty_log_stop(&d->vga);
1012
    qxl_destroy_primary(d, QXL_SYNC);
G
Gerd Hoffmann 已提交
1013 1014
}

1015
static void qxl_update_irq(PCIQXLDevice *d)
G
Gerd Hoffmann 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
{
    uint32_t pending = le32_to_cpu(d->ram->int_pending);
    uint32_t mask    = le32_to_cpu(d->ram->int_mask);
    int level = !!(pending & mask);
    qemu_set_irq(d->pci.irq[0], level);
    qxl_ring_set_dirty(d);
}

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

1029 1030
    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 已提交
1031 1032 1033 1034 1035 1036
}

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

1037
    qxl_check_state(d);
G
Gerd Hoffmann 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    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));
}

static void qxl_soft_reset(PCIQXLDevice *d)
{
A
Alon Levy 已提交
1049
    trace_qxl_soft_reset(d->id);
G
Gerd Hoffmann 已提交
1050
    qxl_check_state(d);
1051
    qxl_clear_guest_bug(d);
1052
    d->current_async = QXL_UNDEFINED_IO;
G
Gerd Hoffmann 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

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

static void qxl_hard_reset(PCIQXLDevice *d, int loadvm)
{
A
Alon Levy 已提交
1063
    trace_qxl_hard_reset(d->id, loadvm);
G
Gerd Hoffmann 已提交
1064

G
Gerd Hoffmann 已提交
1065 1066
    qxl_spice_reset_cursor(d);
    qxl_spice_reset_image_cache(d);
G
Gerd Hoffmann 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    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);
}

static void qxl_reset_handler(DeviceState *dev)
{
    PCIQXLDevice *d = DO_UPCAST(PCIQXLDevice, pci.qdev, dev);
A
Alon Levy 已提交
1083

G
Gerd Hoffmann 已提交
1084 1085 1086 1087 1088 1089 1090 1091
    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 已提交
1092
    trace_qxl_io_write_vga(qxl->id, qxl_mode_to_string(qxl->mode), addr, val);
G
Gerd Hoffmann 已提交
1093
    if (qxl->mode != QXL_MODE_VGA) {
1094
        qxl_destroy_primary(qxl, QXL_SYNC);
G
Gerd Hoffmann 已提交
1095 1096 1097 1098 1099
        qxl_soft_reset(qxl);
    }
    vga_ioport_write(opaque, addr, val);
}

G
Gerd Hoffmann 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
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(),
};

1114 1115
static int qxl_add_memslot(PCIQXLDevice *d, uint32_t slot_id, uint64_t delta,
                           qxl_async_io async)
G
Gerd Hoffmann 已提交
1116 1117 1118 1119
{
    static const int regions[] = {
        QXL_RAM_RANGE_INDEX,
        QXL_VRAM_RANGE_INDEX,
G
Gerd Hoffmann 已提交
1120
        QXL_VRAM64_RANGE_INDEX,
G
Gerd Hoffmann 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    };
    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 已提交
1134
    trace_qxl_memslot_add_guest(d->id, slot_id, guest_start, guest_end);
G
Gerd Hoffmann 已提交
1135

1136
    if (slot_id >= NUM_MEMSLOTS) {
1137
        qxl_set_guest_bug(d, "%s: slot_id >= NUM_MEMSLOTS %d >= %d", __func__,
1138 1139 1140 1141
                      slot_id, NUM_MEMSLOTS);
        return 1;
    }
    if (guest_start > guest_end) {
1142
        qxl_set_guest_bug(d, "%s: guest_start > guest_end 0x%" PRIx64
1143 1144 1145
                         " > 0x%" PRIx64, __func__, guest_start, guest_end);
        return 1;
    }
G
Gerd Hoffmann 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

    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;
    }
1166
    if (i == ARRAY_SIZE(regions)) {
1167
        qxl_set_guest_bug(d, "%s: finished loop without match", __func__);
1168 1169
        return 1;
    }
G
Gerd Hoffmann 已提交
1170 1171 1172

    switch (pci_region) {
    case QXL_RAM_RANGE_INDEX:
1173
        virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vga.vram);
G
Gerd Hoffmann 已提交
1174 1175
        break;
    case QXL_VRAM_RANGE_INDEX:
G
Gerd Hoffmann 已提交
1176
    case 4 /* vram 64bit */:
1177
        virt_start = (intptr_t)memory_region_get_ram_ptr(&d->vram_bar);
G
Gerd Hoffmann 已提交
1178 1179 1180
        break;
    default:
        /* should not happen */
1181
        qxl_set_guest_bug(d, "%s: pci_region = %d", __func__, pci_region);
1182
        return 1;
G
Gerd Hoffmann 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    }

    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);

1193
    qemu_spice_add_memslot(&d->ssd, &memslot, async);
G
Gerd Hoffmann 已提交
1194 1195 1196 1197
    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;
1198
    return 0;
G
Gerd Hoffmann 已提交
1199 1200 1201 1202
}

static void qxl_del_memslot(PCIQXLDevice *d, uint32_t slot_id)
{
1203
    qemu_spice_del_memslot(&d->ssd, MEMSLOT_GROUP_HOST, slot_id);
G
Gerd Hoffmann 已提交
1204 1205 1206 1207 1208
    d->guest_slots[slot_id].active = 0;
}

static void qxl_reset_memslots(PCIQXLDevice *d)
{
G
Gerd Hoffmann 已提交
1209
    qxl_spice_reset_memslots(d);
G
Gerd Hoffmann 已提交
1210 1211 1212 1213 1214
    memset(&d->guest_slots, 0, sizeof(d->guest_slots));
}

static void qxl_reset_surfaces(PCIQXLDevice *d)
{
A
Alon Levy 已提交
1215
    trace_qxl_reset_surfaces(d->id);
G
Gerd Hoffmann 已提交
1216
    d->mode = QXL_MODE_UNDEFINED;
1217
    qxl_spice_destroy_surfaces(d, QXL_SYNC);
G
Gerd Hoffmann 已提交
1218 1219
}

1220
/* can be also called from spice server thread context */
G
Gerd Hoffmann 已提交
1221 1222 1223 1224 1225 1226 1227 1228
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 已提交
1229
        return (void *)(intptr_t)offset;
G
Gerd Hoffmann 已提交
1230
    case MEMSLOT_GROUP_GUEST:
1231
        if (slot >= NUM_MEMSLOTS) {
1232 1233
            qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
                              NUM_MEMSLOTS);
1234 1235 1236
            return NULL;
        }
        if (!qxl->guest_slots[slot].active) {
1237
            qxl_set_guest_bug(qxl, "inactive slot %d\n", slot);
1238 1239 1240
            return NULL;
        }
        if (offset < qxl->guest_slots[slot].delta) {
1241 1242
            qxl_set_guest_bug(qxl,
                          "slot %d offset %"PRIu64" < delta %"PRIu64"\n",
1243 1244 1245
                          slot, offset, qxl->guest_slots[slot].delta);
            return NULL;
        }
G
Gerd Hoffmann 已提交
1246
        offset -= qxl->guest_slots[slot].delta;
1247
        if (offset > qxl->guest_slots[slot].size) {
1248 1249
            qxl_set_guest_bug(qxl,
                          "slot %d offset %"PRIu64" > size %"PRIu64"\n",
1250 1251 1252
                          slot, offset, qxl->guest_slots[slot].size);
            return NULL;
        }
G
Gerd Hoffmann 已提交
1253 1254
        return qxl->guest_slots[slot].ptr + offset;
    }
1255
    return NULL;
G
Gerd Hoffmann 已提交
1256 1257
}

1258 1259 1260 1261 1262 1263 1264 1265
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 已提交
1266 1267 1268
{
    QXLDevSurfaceCreate surface;
    QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    int size;
    int requested_height = le32_to_cpu(sc->height);
    int requested_stride = le32_to_cpu(sc->stride);

    size = abs(requested_stride) * requested_height;
    if (size > qxl->vgamem_size) {
        qxl_set_guest_bug(qxl, "%s: requested primary larger then framebuffer"
                               " size", __func__);
        return;
    }
G
Gerd Hoffmann 已提交
1279

1280
    if (qxl->mode == QXL_MODE_NATIVE) {
1281
        qxl_set_guest_bug(qxl, "%s: nop since already in QXL_MODE_NATIVE",
1282 1283
                      __func__);
    }
G
Gerd Hoffmann 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
    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 已提交
1294 1295 1296 1297
    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 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306

    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;
1307
    qemu_spice_create_primary_surface(&qxl->ssd, 0, &surface, async);
G
Gerd Hoffmann 已提交
1308

1309 1310 1311
    if (async == QXL_SYNC) {
        qxl_create_guest_primary_complete(qxl);
    }
G
Gerd Hoffmann 已提交
1312 1313
}

1314 1315 1316
/* 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 已提交
1317 1318
{
    if (d->mode == QXL_MODE_UNDEFINED) {
1319
        return 0;
G
Gerd Hoffmann 已提交
1320
    }
A
Alon Levy 已提交
1321
    trace_qxl_destroy_primary(d->id);
G
Gerd Hoffmann 已提交
1322
    d->mode = QXL_MODE_UNDEFINED;
1323
    qemu_spice_destroy_primary_surface(&d->ssd, 0, async);
Y
Yonit Halperin 已提交
1324
    qxl_spice_reset_cursor(d);
1325
    return 1;
G
Gerd Hoffmann 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
}

static void qxl_set_mode(PCIQXLDevice *d, int modenr, int loadvm)
{
    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
    };
    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 已提交
1348 1349
    trace_qxl_set_mode(d->id, modenr, mode->x_res, mode->y_res, mode->bits,
                       devmem);
G
Gerd Hoffmann 已提交
1350 1351 1352 1353 1354
    if (!loadvm) {
        qxl_hard_reset(d, 0);
    }

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

    d->guest_primary.surface = surface;
1358
    qxl_create_guest_primary(d, 0, QXL_SYNC);
G
Gerd Hoffmann 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369

    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);
}

1370 1371
static void ioport_write(void *opaque, target_phys_addr_t addr,
                         uint64_t val, unsigned size)
G
Gerd Hoffmann 已提交
1372 1373
{
    PCIQXLDevice *d = opaque;
1374
    uint32_t io_port = addr;
1375 1376
    qxl_async_io async = QXL_SYNC;
    uint32_t orig_io_port = io_port;
G
Gerd Hoffmann 已提交
1377

1378 1379 1380 1381
    if (d->guest_bug && !io_port == QXL_IO_RESET) {
        return;
    }

1382 1383 1384 1385 1386 1387 1388
    if (d->revision <= QXL_REVISION_STABLE_V10 &&
        io_port >= QXL_IO_FLUSH_SURFACES_ASYNC) {
        qxl_set_guest_bug(d, "unsupported io %d for revision %d\n",
            io_port, d->revision);
        return;
    }

G
Gerd Hoffmann 已提交
1389 1390 1391 1392 1393 1394
    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:
1395
    case QXL_IO_UPDATE_IRQ:
A
Alon Levy 已提交
1396
    case QXL_IO_LOG:
1397 1398
    case QXL_IO_MEMSLOT_ADD_ASYNC:
    case QXL_IO_CREATE_PRIMARY_ASYNC:
G
Gerd Hoffmann 已提交
1399 1400
        break;
    default:
1401
        if (d->mode != QXL_MODE_VGA) {
G
Gerd Hoffmann 已提交
1402
            break;
1403
        }
A
Alon Levy 已提交
1404 1405
        trace_qxl_io_unexpected_vga_mode(d->id,
            io_port, io_port_to_string(io_port));
1406 1407
        /* be nice to buggy guest drivers */
        if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&
1408
            io_port < QXL_IO_RANGE_SIZE) {
1409 1410
            qxl_send_events(d, QXL_INTERRUPT_IO_CMD);
        }
G
Gerd Hoffmann 已提交
1411 1412 1413
        return;
    }

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
    /* 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;
1434 1435
        goto async_common;
    case QXL_IO_FLUSH_SURFACES_ASYNC:
1436
    case QXL_IO_MONITORS_CONFIG_ASYNC:
1437 1438 1439 1440
async_common:
        async = QXL_ASYNC;
        qemu_mutex_lock(&d->async_lock);
        if (d->current_async != QXL_UNDEFINED_IO) {
1441
            qxl_set_guest_bug(d, "%d async started before last (%d) complete",
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
                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;
    }
A
Alon Levy 已提交
1452 1453
    trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode), addr, val, size,
                       async);
1454

G
Gerd Hoffmann 已提交
1455 1456 1457
    switch (io_port) {
    case QXL_IO_UPDATE_AREA:
    {
A
Alon Levy 已提交
1458
        QXLCookie *cookie = NULL;
G
Gerd Hoffmann 已提交
1459
        QXLRect update = d->ram->update_area;
A
Alon Levy 已提交
1460

1461
        if (d->ram->update_surface > d->ssd.num_surfaces) {
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
            qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",
                              d->ram->update_surface);
            return;
        }
        if (update.left >= update.right || update.top >= update.bottom) {
            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);
            return;
        }

A
Alon Levy 已提交
1473 1474 1475 1476 1477
        if (async == QXL_ASYNC) {
            cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
                                    QXL_IO_UPDATE_AREA_ASYNC);
            cookie->u.area = update;
        }
G
Gerd Hoffmann 已提交
1478
        qxl_spice_update_area(d, d->ram->update_surface,
A
Alon Levy 已提交
1479 1480
                              cookie ? &cookie->u.area : &update,
                              NULL, 0, 0, async, cookie);
G
Gerd Hoffmann 已提交
1481 1482 1483
        break;
    }
    case QXL_IO_NOTIFY_CMD:
1484
        qemu_spice_wakeup(&d->ssd);
G
Gerd Hoffmann 已提交
1485 1486
        break;
    case QXL_IO_NOTIFY_CURSOR:
1487
        qemu_spice_wakeup(&d->ssd);
G
Gerd Hoffmann 已提交
1488 1489
        break;
    case QXL_IO_UPDATE_IRQ:
1490
        qxl_update_irq(d);
G
Gerd Hoffmann 已提交
1491 1492 1493 1494 1495 1496
        break;
    case QXL_IO_NOTIFY_OOM:
        if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {
            break;
        }
        d->oom_running = 1;
G
Gerd Hoffmann 已提交
1497
        qxl_spice_oom(d);
G
Gerd Hoffmann 已提交
1498 1499 1500 1501 1502 1503 1504
        d->oom_running = 0;
        break;
    case QXL_IO_SET_MODE:
        qxl_set_mode(d, val, 0);
        break;
    case QXL_IO_LOG:
        if (d->guestdebug) {
P
Peter Maydell 已提交
1505
            fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,
A
Alon Levy 已提交
1506
                    qemu_get_clock_ns(vm_clock), d->ram->log_buf);
G
Gerd Hoffmann 已提交
1507 1508 1509 1510 1511 1512
        }
        break;
    case QXL_IO_RESET:
        qxl_hard_reset(d, 0);
        break;
    case QXL_IO_MEMSLOT_ADD:
1513
        if (val >= NUM_MEMSLOTS) {
1514
            qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");
1515 1516 1517
            break;
        }
        if (d->guest_slots[val].active) {
1518 1519
            qxl_set_guest_bug(d,
                        "QXL_IO_MEMSLOT_ADD: memory slot already active");
1520 1521
            break;
        }
G
Gerd Hoffmann 已提交
1522
        d->guest_slots[val].slot = d->ram->mem_slot;
1523
        qxl_add_memslot(d, val, 0, async);
G
Gerd Hoffmann 已提交
1524 1525
        break;
    case QXL_IO_MEMSLOT_DEL:
1526
        if (val >= NUM_MEMSLOTS) {
1527
            qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");
1528 1529
            break;
        }
G
Gerd Hoffmann 已提交
1530 1531 1532
        qxl_del_memslot(d, val);
        break;
    case QXL_IO_CREATE_PRIMARY:
1533
        if (val != 0) {
1534
            qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",
1535 1536
                          async);
            goto cancel_async;
1537
        }
G
Gerd Hoffmann 已提交
1538
        d->guest_primary.surface = d->ram->create_surface;
1539
        qxl_create_guest_primary(d, 0, async);
G
Gerd Hoffmann 已提交
1540 1541
        break;
    case QXL_IO_DESTROY_PRIMARY:
1542
        if (val != 0) {
1543
            qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",
1544 1545 1546 1547
                          async);
            goto cancel_async;
        }
        if (!qxl_destroy_primary(d, async)) {
A
Alon Levy 已提交
1548 1549
            trace_qxl_io_destroy_primary_ignored(d->id,
                                                 qxl_mode_to_string(d->mode));
1550
            goto cancel_async;
1551
        }
G
Gerd Hoffmann 已提交
1552 1553
        break;
    case QXL_IO_DESTROY_SURFACE_WAIT:
1554
        if (val >= d->ssd.num_surfaces) {
1555
            qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"
1556
                             "%" PRIu64 " >= NUM_SURFACES", async, val);
1557 1558 1559
            goto cancel_async;
        }
        qxl_spice_destroy_surface_wait(d, val, async);
G
Gerd Hoffmann 已提交
1560
        break;
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    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 已提交
1574
    case QXL_IO_DESTROY_ALL_SURFACES:
1575 1576
        d->mode = QXL_MODE_UNDEFINED;
        qxl_spice_destroy_surfaces(d, async);
G
Gerd Hoffmann 已提交
1577
        break;
1578 1579 1580
    case QXL_IO_MONITORS_CONFIG_ASYNC:
        qxl_spice_monitors_config_async(d, 0);
        break;
G
Gerd Hoffmann 已提交
1581
    default:
1582
        qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);
G
Gerd Hoffmann 已提交
1583
    }
1584 1585 1586 1587 1588 1589 1590 1591
    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 已提交
1592 1593
}

1594 1595
static uint64_t ioport_read(void *opaque, target_phys_addr_t addr,
                            unsigned size)
G
Gerd Hoffmann 已提交
1596 1597 1598
{
    PCIQXLDevice *d = opaque;

A
Alon Levy 已提交
1599
    trace_qxl_io_read_unexpected(d->id);
G
Gerd Hoffmann 已提交
1600 1601 1602
    return 0xff;
}

1603 1604 1605 1606 1607 1608 1609 1610
static const MemoryRegionOps qxl_io_ops = {
    .read = ioport_read,
    .write = ioport_write,
    .valid = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
};
G
Gerd Hoffmann 已提交
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

static void pipe_read(void *opaque)
{
    PCIQXLDevice *d = opaque;
    char dummy;
    int len;

    do {
        len = read(d->pipe[0], &dummy, sizeof(dummy));
    } while (len == sizeof(dummy));
1621
    qxl_update_irq(d);
G
Gerd Hoffmann 已提交
1622 1623 1624 1625 1626 1627 1628
}

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

1629
    assert(qemu_spice_display_is_running(&d->ssd));
G
Gerd Hoffmann 已提交
1630 1631 1632 1633
    old_pending = __sync_fetch_and_or(&d->ram->int_pending, le_events);
    if ((old_pending & le_events) == le_events) {
        return;
    }
J
Jan Kiszka 已提交
1634
    if (qemu_thread_is_self(&d->main)) {
1635
        qxl_update_irq(d);
G
Gerd Hoffmann 已提交
1636 1637
    } else {
        if (write(d->pipe[1], d, 1) != 1) {
1638
            dprint(d, 1, "%s: write to pipe failed\n", __func__);
G
Gerd Hoffmann 已提交
1639 1640 1641 1642 1643 1644
        }
    }
}

static void init_pipe_signaling(PCIQXLDevice *d)
{
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    if (pipe(d->pipe) < 0) {
        fprintf(stderr, "%s:%s: qxl pipe creation failed\n",
                __FILE__, __func__);
        exit(1);
    }
    fcntl(d->pipe[0], F_SETFL, O_NONBLOCK);
    fcntl(d->pipe[1], F_SETFL, O_NONBLOCK);
    fcntl(d->pipe[0], F_SETOWN, getpid());

    qemu_thread_get_self(&d->main);
    qemu_set_fd_handler(d->pipe[0], pipe_read, NULL, d);
G
Gerd Hoffmann 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
}

/* graphics console */

static void qxl_hw_update(void *opaque)
{
    PCIQXLDevice *qxl = opaque;
    VGACommonState *vga = &qxl->vga;

    switch (qxl->mode) {
    case QXL_MODE_VGA:
        vga->update(vga);
        break;
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
        qxl_render_update(qxl);
        break;
    default:
        break;
    }
}

static void qxl_hw_invalidate(void *opaque)
{
    PCIQXLDevice *qxl = opaque;
    VGACommonState *vga = &qxl->vga;

    vga->invalidate(vga);
}

1686 1687
static void qxl_hw_screen_dump(void *opaque, const char *filename, bool cswitch,
                               Error **errp)
G
Gerd Hoffmann 已提交
1688 1689 1690 1691 1692 1693 1694 1695
{
    PCIQXLDevice *qxl = opaque;
    VGACommonState *vga = &qxl->vga;

    switch (qxl->mode) {
    case QXL_MODE_COMPAT:
    case QXL_MODE_NATIVE:
        qxl_render_update(qxl);
1696
        ppm_save(filename, qxl->ssd.ds->surface, errp);
G
Gerd Hoffmann 已提交
1697 1698
        break;
    case QXL_MODE_VGA:
1699
        vga->screen_dump(vga, filename, cswitch, errp);
G
Gerd Hoffmann 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
        break;
    default:
        break;
    }
}

static void qxl_hw_text_update(void *opaque, console_ch_t *chardata)
{
    PCIQXLDevice *qxl = opaque;
    VGACommonState *vga = &qxl->vga;

    if (qxl->mode == QXL_MODE_VGA) {
        vga->text_update(vga, chardata);
        return;
    }
}

1717 1718 1719 1720 1721
static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
{
    intptr_t vram_start;
    int i;

1722
    if (qxl->mode != QXL_MODE_NATIVE && qxl->mode != QXL_MODE_COMPAT) {
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
        return;
    }

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

    vram_start =  (intptr_t)memory_region_get_ram_ptr(&qxl->vram_bar);

    /* dirty the off-screen surfaces */
1733
    for (i = 0; i < qxl->ssd.num_surfaces; i++) {
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
        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);
1744
        assert(cmd);
1745 1746 1747 1748
        assert(cmd->type == QXL_SURFACE_CMD_CREATE);
        surface_offset = (intptr_t)qxl_phys2virt(qxl,
                                                 cmd->u.surface_create.data,
                                                 MEMSLOT_GROUP_GUEST);
1749
        assert(surface_offset);
1750 1751 1752
        surface_offset -= vram_start;
        surface_size = cmd->u.surface_create.height *
                       abs(cmd->u.surface_create.stride);
A
Alon Levy 已提交
1753
        trace_qxl_surfaces_dirty(qxl->id, i, (int)surface_offset, surface_size);
1754 1755 1756 1757
        qxl_set_dirty(&qxl->vram_bar, surface_offset, surface_size);
    }
}

1758 1759
static void qxl_vm_change_state_handler(void *opaque, int running,
                                        RunState state)
G
Gerd Hoffmann 已提交
1760 1761
{
    PCIQXLDevice *qxl = opaque;
1762
    qemu_spice_vm_change_state_handler(&qxl->ssd, running, state);
G
Gerd Hoffmann 已提交
1763

1764 1765 1766
    if (running) {
        /*
         * if qxl_send_events was called from spice server context before
1767
         * migration ended, qxl_update_irq for these events might not have been
1768 1769
         * called
         */
1770
         qxl_update_irq(qxl);
1771 1772 1773
    } else {
        /* make sure surfaces are saved before migration */
        qxl_dirty_surfaces(qxl);
G
Gerd Hoffmann 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
    }
}

/* display change listener */

static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
{
    if (qxl0->mode == QXL_MODE_VGA) {
        qemu_spice_display_update(&qxl0->ssd, x, y, w, h);
    }
}

static void display_resize(struct DisplayState *ds)
{
    if (qxl0->mode == QXL_MODE_VGA) {
        qemu_spice_display_resize(&qxl0->ssd);
    }
}

static void display_refresh(struct DisplayState *ds)
{
    if (qxl0->mode == QXL_MODE_VGA) {
        qemu_spice_display_refresh(&qxl0->ssd);
1797 1798 1799 1800
    } else {
        qemu_mutex_lock(&qxl0->ssd.lock);
        qemu_spice_cursor_refresh_unlocked(&qxl0->ssd);
        qemu_mutex_unlock(&qxl0->ssd.lock);
G
Gerd Hoffmann 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809
    }
}

static DisplayChangeListener display_listener = {
    .dpy_update  = display_update,
    .dpy_resize  = display_resize,
    .dpy_refresh = display_refresh,
};

1810
static void qxl_init_ramsize(PCIQXLDevice *qxl)
1811
{
1812 1813 1814 1815 1816 1817 1818
    /* vga mode framebuffer / primary surface (bar 0, first part) */
    if (qxl->vgamem_size_mb < 8) {
        qxl->vgamem_size_mb = 8;
    }
    qxl->vgamem_size = qxl->vgamem_size_mb * 1024 * 1024;

    /* vga ram (bar 0, total) */
1819 1820 1821
    if (qxl->ram_size_mb != -1) {
        qxl->vga.vram_size = qxl->ram_size_mb * 1024 * 1024;
    }
1822 1823
    if (qxl->vga.vram_size < qxl->vgamem_size * 2) {
        qxl->vga.vram_size = qxl->vgamem_size * 2;
1824 1825
    }

G
Gerd Hoffmann 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834
    /* 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) */
1835 1836 1837
    if (qxl->vram_size_mb != -1) {
        qxl->vram_size = qxl->vram_size_mb * 1024 * 1024;
    }
G
Gerd Hoffmann 已提交
1838 1839
    if (qxl->vram_size < qxl->vram32_size) {
        qxl->vram_size = qxl->vram32_size;
1840
    }
G
Gerd Hoffmann 已提交
1841

1842
    if (qxl->revision == 1) {
G
Gerd Hoffmann 已提交
1843
        qxl->vram32_size = 4096;
1844 1845
        qxl->vram_size = 4096;
    }
1846
    qxl->vgamem_size = msb_mask(qxl->vgamem_size * 2 - 1);
1847
    qxl->vga.vram_size = msb_mask(qxl->vga.vram_size * 2 - 1);
G
Gerd Hoffmann 已提交
1848
    qxl->vram32_size = msb_mask(qxl->vram32_size * 2 - 1);
1849 1850 1851
    qxl->vram_size = msb_mask(qxl->vram_size * 2 - 1);
}

G
Gerd Hoffmann 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860
static int qxl_init_common(PCIQXLDevice *qxl)
{
    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;
1861
    qemu_mutex_init(&qxl->track_lock);
1862 1863
    qemu_mutex_init(&qxl->async_lock);
    qxl->current_async = QXL_UNDEFINED_IO;
1864
    qxl->guest_bug = 0;
G
Gerd Hoffmann 已提交
1865 1866 1867 1868

    switch (qxl->revision) {
    case 1: /* spice 0.4 -- qxl-1 */
        pci_device_rev = QXL_REVISION_STABLE_V04;
1869
        io_size = 8;
G
Gerd Hoffmann 已提交
1870 1871 1872
        break;
    case 2: /* spice 0.6 -- qxl-2 */
        pci_device_rev = QXL_REVISION_STABLE_V06;
1873
        io_size = 16;
G
Gerd Hoffmann 已提交
1874
        break;
G
Gerd Hoffmann 已提交
1875
    case 3: /* qxl-3 */
1876 1877 1878 1879 1880 1881 1882 1883
        pci_device_rev = QXL_REVISION_STABLE_V10;
        io_size = 32; /* PCI region size must be pow2 */
        break;
/* 0x000b01 == 0.11.1 */
#if SPICE_SERVER_VERSION >= 0x000b01 && \
        defined(CONFIG_QXL_IO_MONITORS_CONFIG_ASYNC)
    case 4: /* qxl-4 */
        pci_device_rev = QXL_REVISION_STABLE_V12;
1884
        io_size = msb_mask(QXL_IO_RANGE_SIZE * 2 - 1);
G
Gerd Hoffmann 已提交
1885
        break;
1886
#endif
A
Alon Levy 已提交
1887 1888 1889 1890
    default:
        error_report("Invalid revision %d for qxl device (max %d)",
                     qxl->revision, QXL_DEFAULT_REVISION);
        return -1;
G
Gerd Hoffmann 已提交
1891 1892 1893 1894 1895 1896
    }

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

    qxl->rom_size = qxl_rom_size();
1897 1898
    memory_region_init_ram(&qxl->rom_bar, "qxl.vrom", qxl->rom_size);
    vmstate_register_ram(&qxl->rom_bar, &qxl->pci.qdev);
G
Gerd Hoffmann 已提交
1899 1900 1901
    init_qxl_rom(qxl);
    init_qxl_ram(qxl);

1902
    qxl->guest_surfaces.cmds = g_new0(QXLPHYSICAL, qxl->ssd.num_surfaces);
1903 1904
    memory_region_init_ram(&qxl->vram_bar, "qxl.vram", qxl->vram_size);
    vmstate_register_ram(&qxl->vram_bar, &qxl->pci.qdev);
G
Gerd Hoffmann 已提交
1905 1906
    memory_region_init_alias(&qxl->vram32_bar, "qxl.vram32", &qxl->vram_bar,
                             0, qxl->vram32_size);
G
Gerd Hoffmann 已提交
1907

1908 1909 1910 1911 1912 1913 1914
    memory_region_init_io(&qxl->io_bar, &qxl_io_ops, qxl,
                          "qxl-ioports", io_size);
    if (qxl->id == 0) {
        vga_dirty_log_start(&qxl->vga);
    }


1915 1916
    pci_register_bar(&qxl->pci, QXL_IO_RANGE_INDEX,
                     PCI_BASE_ADDRESS_SPACE_IO, &qxl->io_bar);
G
Gerd Hoffmann 已提交
1917

1918 1919
    pci_register_bar(&qxl->pci, QXL_ROM_RANGE_INDEX,
                     PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->rom_bar);
G
Gerd Hoffmann 已提交
1920

1921 1922
    pci_register_bar(&qxl->pci, QXL_RAM_RANGE_INDEX,
                     PCI_BASE_ADDRESS_SPACE_MEMORY, &qxl->vga.vram);
G
Gerd Hoffmann 已提交
1923

1924
    pci_register_bar(&qxl->pci, QXL_VRAM_RANGE_INDEX,
G
Gerd Hoffmann 已提交
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
                     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 已提交
1948 1949 1950 1951 1952 1953 1954 1955 1956

    qxl->ssd.qxl.base.sif = &qxl_interface.base;
    qxl->ssd.qxl.id = qxl->id;
    qemu_spice_add_interface(&qxl->ssd.qxl.base);
    qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl);

    init_pipe_signaling(qxl);
    qxl_reset_state(qxl);

A
Alon Levy 已提交
1957 1958
    qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);

G
Gerd Hoffmann 已提交
1959 1960 1961 1962 1963 1964 1965
    return 0;
}

static int qxl_init_primary(PCIDevice *dev)
{
    PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev);
    VGACommonState *vga = &qxl->vga;
G
Gerd Hoffmann 已提交
1966
    PortioList *qxl_vga_port_list = g_new(PortioList, 1);
G
Gerd Hoffmann 已提交
1967 1968

    qxl->id = 0;
1969
    qxl_init_ramsize(qxl);
G
Gerd Hoffmann 已提交
1970 1971
    vga->vram_size_mb = qxl->vga.vram_size >> 20;
    vga_common_init(vga);
1972
    vga_init(vga, pci_address_space(dev), pci_address_space_io(dev), false);
G
Gerd Hoffmann 已提交
1973 1974
    portio_list_init(qxl_vga_port_list, qxl_vga_portio_list, vga, "vga");
    portio_list_add(qxl_vga_port_list, pci_address_space_io(dev), 0x3b0);
G
Gerd Hoffmann 已提交
1975 1976 1977

    vga->ds = graphic_console_init(qxl_hw_update, qxl_hw_invalidate,
                                   qxl_hw_screen_dump, qxl_hw_text_update, qxl);
1978
    qemu_spice_display_init_common(&qxl->ssd, vga->ds);
G
Gerd Hoffmann 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991

    qxl0 = qxl;
    register_displaychangelistener(vga->ds, &display_listener);

    return qxl_init_common(qxl);
}

static int qxl_init_secondary(PCIDevice *dev)
{
    static int device_id = 1;
    PCIQXLDevice *qxl = DO_UPCAST(PCIQXLDevice, pci, dev);

    qxl->id = device_id++;
1992
    qxl_init_ramsize(qxl);
1993 1994
    memory_region_init_ram(&qxl->vga.vram, "qxl.vgavram", qxl->vga.vram_size);
    vmstate_register_ram(&qxl->vga.vram, &qxl->pci.qdev);
1995
    qxl->vga.vram_ptr = memory_region_get_ram_ptr(&qxl->vga.vram);
G
Gerd Hoffmann 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004

    return qxl_init_common(qxl);
}

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

A
Alon Levy 已提交
2005
    trace_qxl_pre_save(d->id);
G
Gerd Hoffmann 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
    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 已提交
2018
    trace_qxl_pre_load(d->id);
G
Gerd Hoffmann 已提交
2019 2020 2021 2022 2023
    qxl_hard_reset(d, 1);
    qxl_exit_vga_mode(d);
    return 0;
}

2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
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 已提交
2036 2037 2038 2039 2040
static int qxl_post_load(void *opaque, int version)
{
    PCIQXLDevice* d = opaque;
    uint8_t *ram_start = d->vga.vram_ptr;
    QXLCommandExt *cmds;
2041
    int in, out, newmode;
G
Gerd Hoffmann 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051

    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 已提交
2052
    trace_qxl_post_load(d->id, qxl_mode_to_string(d->mode));
G
Gerd Hoffmann 已提交
2053 2054
    newmode = d->mode;
    d->mode = QXL_MODE_UNDEFINED;
2055

G
Gerd Hoffmann 已提交
2056 2057 2058 2059
    switch (newmode) {
    case QXL_MODE_UNDEFINED:
        break;
    case QXL_MODE_VGA:
2060
        qxl_create_memslots(d);
G
Gerd Hoffmann 已提交
2061 2062 2063
        qxl_enter_vga_mode(d);
        break;
    case QXL_MODE_NATIVE:
2064
        qxl_create_memslots(d);
2065
        qxl_create_guest_primary(d, 1, QXL_SYNC);
G
Gerd Hoffmann 已提交
2066 2067

        /* replay surface-create and cursor-set commands */
2068 2069
        cmds = g_malloc0(sizeof(QXLCommandExt) * (d->ssd.num_surfaces + 1));
        for (in = 0, out = 0; in < d->ssd.num_surfaces; in++) {
G
Gerd Hoffmann 已提交
2070 2071 2072 2073 2074 2075 2076 2077
            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 已提交
2078 2079 2080 2081 2082 2083
        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 已提交
2084
        qxl_spice_loadvm_commands(d, cmds, out);
2085
        g_free(cmds);
2086 2087 2088
        if (d->guest_monitors_config) {
            qxl_spice_monitors_config_async(d, 1);
        }
G
Gerd Hoffmann 已提交
2089 2090
        break;
    case QXL_MODE_COMPAT:
2091 2092
        /* note: no need to call qxl_create_memslots, qxl_set_mode
         * creates the mem slot. */
G
Gerd Hoffmann 已提交
2093 2094 2095 2096 2097 2098
        qxl_set_mode(d, d->shadow_rom.mode, 1);
        break;
    }
    return 0;
}

2099
#define QXL_SAVE_VERSION 21
G
Gerd Hoffmann 已提交
2100

2101 2102 2103 2104 2105 2106 2107 2108
static bool qxl_monitors_config_needed(void *opaque)
{
    PCIQXLDevice *qxl = opaque;

    return qxl->guest_monitors_config != 0;
}


G
Gerd Hoffmann 已提交
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
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()
    }
};

2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
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 已提交
2149 2150 2151 2152 2153 2154 2155
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,
2156
    .fields = (VMStateField[]) {
G
Gerd Hoffmann 已提交
2157 2158 2159 2160 2161 2162 2163
        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),
2164 2165 2166 2167 2168
        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),
2169 2170 2171 2172
        VMSTATE_INT32_EQUAL(ssd.num_surfaces, PCIQXLDevice),
        VMSTATE_VARRAY_INT32(guest_surfaces.cmds, PCIQXLDevice,
                             ssd.num_surfaces, 0,
                             vmstate_info_uint64, uint64_t),
2173
        VMSTATE_UINT64(guest_cursor, PCIQXLDevice),
G
Gerd Hoffmann 已提交
2174 2175
        VMSTATE_END_OF_LIST()
    },
2176 2177 2178 2179 2180 2181 2182 2183
    .subsections = (VMStateSubsection[]) {
        {
            .vmsd = &qxl_vmstate_monitors_config,
            .needed = qxl_monitors_config_needed,
        }, {
            /* empty */
        }
    }
G
Gerd Hoffmann 已提交
2184 2185
};

G
Gerd Hoffmann 已提交
2186 2187 2188
static Property qxl_properties[] = {
        DEFINE_PROP_UINT32("ram_size", PCIQXLDevice, vga.vram_size,
                           64 * 1024 * 1024),
G
Gerd Hoffmann 已提交
2189
        DEFINE_PROP_UINT32("vram_size", PCIQXLDevice, vram32_size,
G
Gerd Hoffmann 已提交
2190 2191 2192 2193 2194 2195
                           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),
2196
        DEFINE_PROP_UINT32("ram_size_mb",  PCIQXLDevice, ram_size_mb, -1),
2197 2198
        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 已提交
2199
        DEFINE_PROP_UINT32("vgamem_mb", PCIQXLDevice, vgamem_size_mb, 16),
2200
        DEFINE_PROP_INT32("surfaces", PCIQXLDevice, ssd.num_surfaces, 1024),
G
Gerd Hoffmann 已提交
2201 2202 2203
        DEFINE_PROP_END_OF_LIST(),
};

2204 2205
static void qxl_primary_class_init(ObjectClass *klass, void *data)
{
2206
    DeviceClass *dc = DEVICE_CLASS(klass);
2207 2208 2209 2210 2211 2212 2213 2214
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->no_hotplug = 1;
    k->init = qxl_init_primary;
    k->romfile = "vgabios-qxl.bin";
    k->vendor_id = REDHAT_PCI_VENDOR_ID;
    k->device_id = QXL_DEVICE_ID_STABLE;
    k->class_id = PCI_CLASS_DISPLAY_VGA;
2215 2216 2217 2218
    dc->desc = "Spice QXL GPU (primary, vga compatible)";
    dc->reset = qxl_reset_handler;
    dc->vmsd = &qxl_vmstate;
    dc->props = qxl_properties;
2219 2220
}

2221 2222 2223 2224 2225
static TypeInfo qxl_primary_info = {
    .name          = "qxl-vga",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIQXLDevice),
    .class_init    = qxl_primary_class_init,
G
Gerd Hoffmann 已提交
2226 2227
};

2228 2229
static void qxl_secondary_class_init(ObjectClass *klass, void *data)
{
2230
    DeviceClass *dc = DEVICE_CLASS(klass);
2231 2232 2233 2234 2235 2236
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = qxl_init_secondary;
    k->vendor_id = REDHAT_PCI_VENDOR_ID;
    k->device_id = QXL_DEVICE_ID_STABLE;
    k->class_id = PCI_CLASS_DISPLAY_OTHER;
2237 2238 2239 2240
    dc->desc = "Spice QXL GPU (secondary)";
    dc->reset = qxl_reset_handler;
    dc->vmsd = &qxl_vmstate;
    dc->props = qxl_properties;
2241 2242
}

2243 2244 2245 2246 2247
static TypeInfo qxl_secondary_info = {
    .name          = "qxl",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIQXLDevice),
    .class_init    = qxl_secondary_class_init,
G
Gerd Hoffmann 已提交
2248 2249
};

A
Andreas Färber 已提交
2250
static void qxl_register_types(void)
G
Gerd Hoffmann 已提交
2251
{
2252 2253
    type_register_static(&qxl_primary_info);
    type_register_static(&qxl_secondary_info);
G
Gerd Hoffmann 已提交
2254 2255
}

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