vmware_vga.c 36.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * QEMU VMware-SVGA "chipset".
 *
 * Copyright (c) 2007 Andrzej Zaborowski  <balrog@zabor.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
#include "hw/hw.h"
#include "hw/loader.h"
26
#include "ui/console.h"
27
#include "hw/pci/pci.h"
28

J
Jan Kiszka 已提交
29
#undef VERBOSE
30 31 32 33
#define HW_RECT_ACCEL
#define HW_FILL_ACCEL
#define HW_MOUSE_ACCEL

34
#include "vga_int.h"
35 36

/* See http://vmware-svga.sf.net/ for some documentation on VMWare SVGA */
37 38

struct vmsvga_state_s {
39
    VGACommonState vga;
40 41

    int invalidated;
42 43
    int depth;
    int bypp;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    int enable;
    int config;
    struct {
        int id;
        int x;
        int y;
        int on;
    } cursor;

    int index;
    int scratch_size;
    uint32_t *scratch;
    int new_width;
    int new_height;
    uint32_t guest;
    uint32_t svgaid;
    int syncing;

62
    MemoryRegion fifo_ram;
63 64 65
    uint8_t *fifo_ptr;
    unsigned int fifo_size;

66 67
    union {
        uint32_t *fifo;
68
        struct QEMU_PACKED {
69 70 71 72 73 74 75 76 77
            uint32_t min;
            uint32_t max;
            uint32_t next_cmd;
            uint32_t stop;
            /* Add registers here when adding capabilities.  */
            uint32_t fifo[0];
        } *cmd;
    };

B
BALATON Zoltan 已提交
78
#define REDRAW_FIFO_LEN  512
79 80 81 82 83 84 85 86 87
    struct vmsvga_rect_s {
        int x, y, w, h;
    } redraw_fifo[REDRAW_FIFO_LEN];
    int redraw_fifo_first, redraw_fifo_last;
};

struct pci_vmsvga_state_s {
    PCIDevice card;
    struct vmsvga_state_s chip;
88
    MemoryRegion io_bar;
89 90
};

B
BALATON Zoltan 已提交
91 92 93 94 95
#define SVGA_MAGIC              0x900000UL
#define SVGA_MAKE_ID(ver)       (SVGA_MAGIC << 8 | (ver))
#define SVGA_ID_0               SVGA_MAKE_ID(0)
#define SVGA_ID_1               SVGA_MAKE_ID(1)
#define SVGA_ID_2               SVGA_MAKE_ID(2)
96

B
BALATON Zoltan 已提交
97 98 99 100
#define SVGA_LEGACY_BASE_PORT   0x4560
#define SVGA_INDEX_PORT         0x0
#define SVGA_VALUE_PORT         0x1
#define SVGA_BIOS_PORT          0x2
101 102 103 104

#define SVGA_VERSION_2

#ifdef SVGA_VERSION_2
B
BALATON Zoltan 已提交
105 106 107 108 109
# define SVGA_ID                SVGA_ID_2
# define SVGA_IO_BASE           SVGA_LEGACY_BASE_PORT
# define SVGA_IO_MUL            1
# define SVGA_FIFO_SIZE         0x10000
# define SVGA_PCI_DEVICE_ID     PCI_DEVICE_ID_VMWARE_SVGA2
110
#else
B
BALATON Zoltan 已提交
111 112 113 114 115
# define SVGA_ID                SVGA_ID_1
# define SVGA_IO_BASE           SVGA_LEGACY_BASE_PORT
# define SVGA_IO_MUL            4
# define SVGA_FIFO_SIZE         0x10000
# define SVGA_PCI_DEVICE_ID     PCI_DEVICE_ID_VMWARE_SVGA
116 117 118 119 120 121 122 123 124 125 126
#endif

enum {
    /* ID 0, 1 and 2 registers */
    SVGA_REG_ID = 0,
    SVGA_REG_ENABLE = 1,
    SVGA_REG_WIDTH = 2,
    SVGA_REG_HEIGHT = 3,
    SVGA_REG_MAX_WIDTH = 4,
    SVGA_REG_MAX_HEIGHT = 5,
    SVGA_REG_DEPTH = 6,
B
BALATON Zoltan 已提交
127
    SVGA_REG_BITS_PER_PIXEL = 7,        /* Current bpp in the guest */
128 129 130 131 132 133 134 135 136 137 138 139
    SVGA_REG_PSEUDOCOLOR = 8,
    SVGA_REG_RED_MASK = 9,
    SVGA_REG_GREEN_MASK = 10,
    SVGA_REG_BLUE_MASK = 11,
    SVGA_REG_BYTES_PER_LINE = 12,
    SVGA_REG_FB_START = 13,
    SVGA_REG_FB_OFFSET = 14,
    SVGA_REG_VRAM_SIZE = 15,
    SVGA_REG_FB_SIZE = 16,

    /* ID 1 and 2 registers */
    SVGA_REG_CAPABILITIES = 17,
B
BALATON Zoltan 已提交
140
    SVGA_REG_MEM_START = 18,            /* Memory for command FIFO */
141
    SVGA_REG_MEM_SIZE = 19,
B
BALATON Zoltan 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    SVGA_REG_CONFIG_DONE = 20,          /* Set when memory area configured */
    SVGA_REG_SYNC = 21,                 /* Write to force synchronization */
    SVGA_REG_BUSY = 22,                 /* Read to check if sync is done */
    SVGA_REG_GUEST_ID = 23,             /* Set guest OS identifier */
    SVGA_REG_CURSOR_ID = 24,            /* ID of cursor */
    SVGA_REG_CURSOR_X = 25,             /* Set cursor X position */
    SVGA_REG_CURSOR_Y = 26,             /* Set cursor Y position */
    SVGA_REG_CURSOR_ON = 27,            /* Turn cursor on/off */
    SVGA_REG_HOST_BITS_PER_PIXEL = 28,  /* Current bpp in the host */
    SVGA_REG_SCRATCH_SIZE = 29,         /* Number of scratch registers */
    SVGA_REG_MEM_REGS = 30,             /* Number of FIFO registers */
    SVGA_REG_NUM_DISPLAYS = 31,         /* Number of guest displays */
    SVGA_REG_PITCHLOCK = 32,            /* Fixed pitch for all modes */

    SVGA_PALETTE_BASE = 1024,           /* Base of SVGA color map */
157 158 159 160
    SVGA_PALETTE_END  = SVGA_PALETTE_BASE + 767,
    SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
};

B
BALATON Zoltan 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#define SVGA_CAP_NONE                   0
#define SVGA_CAP_RECT_FILL              (1 << 0)
#define SVGA_CAP_RECT_COPY              (1 << 1)
#define SVGA_CAP_RECT_PAT_FILL          (1 << 2)
#define SVGA_CAP_LEGACY_OFFSCREEN       (1 << 3)
#define SVGA_CAP_RASTER_OP              (1 << 4)
#define SVGA_CAP_CURSOR                 (1 << 5)
#define SVGA_CAP_CURSOR_BYPASS          (1 << 6)
#define SVGA_CAP_CURSOR_BYPASS_2        (1 << 7)
#define SVGA_CAP_8BIT_EMULATION         (1 << 8)
#define SVGA_CAP_ALPHA_CURSOR           (1 << 9)
#define SVGA_CAP_GLYPH                  (1 << 10)
#define SVGA_CAP_GLYPH_CLIPPING         (1 << 11)
#define SVGA_CAP_OFFSCREEN_1            (1 << 12)
#define SVGA_CAP_ALPHA_BLEND            (1 << 13)
#define SVGA_CAP_3D                     (1 << 14)
#define SVGA_CAP_EXTENDED_FIFO          (1 << 15)
#define SVGA_CAP_MULTIMON               (1 << 16)
#define SVGA_CAP_PITCHLOCK              (1 << 17)
180 181 182 183 184 185 186 187 188

/*
 * FIFO offsets (seen as an array of 32-bit words)
 */
enum {
    /*
     * The original defined FIFO offsets
     */
    SVGA_FIFO_MIN = 0,
B
BALATON Zoltan 已提交
189
    SVGA_FIFO_MAX,      /* The distance from MIN to MAX must be at least 10K */
190 191 192 193 194 195 196 197 198 199 200 201 202
    SVGA_FIFO_NEXT_CMD,
    SVGA_FIFO_STOP,

    /*
     * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
     */
    SVGA_FIFO_CAPABILITIES = 4,
    SVGA_FIFO_FLAGS,
    SVGA_FIFO_FENCE,
    SVGA_FIFO_3D_HWVERSION,
    SVGA_FIFO_PITCHLOCK,
};

B
BALATON Zoltan 已提交
203 204 205 206
#define SVGA_FIFO_CAP_NONE              0
#define SVGA_FIFO_CAP_FENCE             (1 << 0)
#define SVGA_FIFO_CAP_ACCELFRONT        (1 << 1)
#define SVGA_FIFO_CAP_PITCHLOCK         (1 << 2)
207

B
BALATON Zoltan 已提交
208 209
#define SVGA_FIFO_FLAG_NONE             0
#define SVGA_FIFO_FLAG_ACCELFRONT       (1 << 0)
210 211

/* These values can probably be changed arbitrarily.  */
B
BALATON Zoltan 已提交
212 213 214
#define SVGA_SCRATCH_SIZE               0x8000
#define SVGA_MAX_WIDTH                  2360
#define SVGA_MAX_HEIGHT                 1770
215 216

#ifdef VERBOSE
B
BALATON Zoltan 已提交
217
# define GUEST_OS_BASE          0x5001
218
static const char *vmsvga_guest_id[] = {
219 220 221 222 223 224 225 226 227
    [0x00] = "Dos",
    [0x01] = "Windows 3.1",
    [0x02] = "Windows 95",
    [0x03] = "Windows 98",
    [0x04] = "Windows ME",
    [0x05] = "Windows NT",
    [0x06] = "Windows 2000",
    [0x07] = "Linux",
    [0x08] = "OS/2",
228
    [0x09] = "an unknown OS",
229 230
    [0x0a] = "BSD",
    [0x0b] = "Whistler",
231 232 233 234 235 236 237 238 239
    [0x0c] = "an unknown OS",
    [0x0d] = "an unknown OS",
    [0x0e] = "an unknown OS",
    [0x0f] = "an unknown OS",
    [0x10] = "an unknown OS",
    [0x11] = "an unknown OS",
    [0x12] = "an unknown OS",
    [0x13] = "an unknown OS",
    [0x14] = "an unknown OS",
240
    [0x15] = "Windows 2003",
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 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
};
#endif

enum {
    SVGA_CMD_INVALID_CMD = 0,
    SVGA_CMD_UPDATE = 1,
    SVGA_CMD_RECT_FILL = 2,
    SVGA_CMD_RECT_COPY = 3,
    SVGA_CMD_DEFINE_BITMAP = 4,
    SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
    SVGA_CMD_DEFINE_PIXMAP = 6,
    SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
    SVGA_CMD_RECT_BITMAP_FILL = 8,
    SVGA_CMD_RECT_PIXMAP_FILL = 9,
    SVGA_CMD_RECT_BITMAP_COPY = 10,
    SVGA_CMD_RECT_PIXMAP_COPY = 11,
    SVGA_CMD_FREE_OBJECT = 12,
    SVGA_CMD_RECT_ROP_FILL = 13,
    SVGA_CMD_RECT_ROP_COPY = 14,
    SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
    SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
    SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
    SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
    SVGA_CMD_DEFINE_CURSOR = 19,
    SVGA_CMD_DISPLAY_CURSOR = 20,
    SVGA_CMD_MOVE_CURSOR = 21,
    SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
    SVGA_CMD_DRAW_GLYPH = 23,
    SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
    SVGA_CMD_UPDATE_VERBOSE = 25,
    SVGA_CMD_SURFACE_FILL = 26,
    SVGA_CMD_SURFACE_COPY = 27,
    SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
    SVGA_CMD_FRONT_ROP_FILL = 29,
    SVGA_CMD_FENCE = 30,
};

/* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
enum {
    SVGA_CURSOR_ON_HIDE = 0,
    SVGA_CURSOR_ON_SHOW = 1,
    SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
    SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
};

static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
                int x, int y, int w, int h)
{
289
    DisplaySurface *surface = qemu_console_surface(s->vga.con);
290 291 292 293 294 295 296
    int line;
    int bypl;
    int width;
    int start;
    uint8_t *src;
    uint8_t *dst;

297 298 299 300 301 302 303 304 305
    if (x < 0) {
        fprintf(stderr, "%s: update x was < 0 (%d)\n", __func__, x);
        w += x;
        x = 0;
    }
    if (w < 0) {
        fprintf(stderr, "%s: update w was < 0 (%d)\n", __func__, w);
        w = 0;
    }
306
    if (x + w > surface_width(surface)) {
307
        fprintf(stderr, "%s: update width too large x: %d, w: %d\n",
B
BALATON Zoltan 已提交
308
                __func__, x, w);
309 310
        x = MIN(x, surface_width(surface));
        w = surface_width(surface) - x;
311 312
    }

313 314 315 316 317 318 319 320 321
    if (y < 0) {
        fprintf(stderr, "%s: update y was < 0 (%d)\n",  __func__, y);
        h += y;
        y = 0;
    }
    if (h < 0) {
        fprintf(stderr, "%s: update h was < 0 (%d)\n",  __func__, h);
        h = 0;
    }
322
    if (y + h > surface_height(surface)) {
323
        fprintf(stderr, "%s: update height too large y: %d, h: %d\n",
B
BALATON Zoltan 已提交
324
                __func__, y, h);
325 326
        y = MIN(y, surface_height(surface));
        h = surface_height(surface) - y;
327 328
    }

329 330 331
    bypl = surface_stride(surface);
    width = surface_bytes_per_pixel(surface) * w;
    start = surface_bytes_per_pixel(surface) * x + bypl * y;
332
    src = s->vga.vram_ptr + start;
333
    dst = surface_data(surface) + start;
334

B
BALATON Zoltan 已提交
335
    for (line = h; line > 0; line--, src += bypl, dst += bypl) {
336
        memcpy(dst, src, width);
B
BALATON Zoltan 已提交
337
    }
338
    dpy_gfx_update(s->vga.con, x, y, w, h);
339 340 341 342 343
}

static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
                int x, int y, int w, int h)
{
B
BALATON Zoltan 已提交
344 345
    struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];

346 347 348 349 350 351 352 353 354 355
    s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
    rect->x = x;
    rect->y = y;
    rect->w = w;
    rect->h = h;
}

static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
{
    struct vmsvga_rect_s *rect;
B
BALATON Zoltan 已提交
356

357 358 359 360 361 362 363
    if (s->invalidated) {
        s->redraw_fifo_first = s->redraw_fifo_last;
        return;
    }
    /* Overlapping region updates can be optimised out here - if someone
     * knows a smart algorithm to do that, please share.  */
    while (s->redraw_fifo_first != s->redraw_fifo_last) {
B
BALATON Zoltan 已提交
364
        rect = &s->redraw_fifo[s->redraw_fifo_first++];
365 366 367 368 369 370 371 372 373
        s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
        vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
    }
}

#ifdef HW_RECT_ACCEL
static inline void vmsvga_copy_rect(struct vmsvga_state_s *s,
                int x0, int y0, int x1, int y1, int w, int h)
{
374
    DisplaySurface *surface = qemu_console_surface(s->vga.con);
375
    uint8_t *vram = s->vga.vram_ptr;
376 377
    int bypl = surface_stride(surface);
    int bypp = surface_bytes_per_pixel(surface);
378
    int width = bypp * w;
379 380 381
    int line = h;
    uint8_t *ptr[2];

382
    if (y1 > y0) {
383 384
        ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
        ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
385 386 387 388
        for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
            memmove(ptr[1], ptr[0], width);
        }
    } else {
389 390
        ptr[0] = vram + bypp * x0 + bypl * y0;
        ptr[1] = vram + bypp * x1 + bypl * y1;
391 392
        for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
            memmove(ptr[1], ptr[0], width);
393 394 395 396 397 398 399 400 401 402 403
        }
    }

    vmsvga_update_rect_delayed(s, x1, y1, w, h);
}
#endif

#ifdef HW_FILL_ACCEL
static inline void vmsvga_fill_rect(struct vmsvga_state_s *s,
                uint32_t c, int x, int y, int w, int h)
{
404 405 406
    DisplaySurface *surface = qemu_console_surface(s->vga.con);
    int bypl = surface_stride(surface);
    int width = surface_bytes_per_pixel(surface) * w;
407 408
    int line = h;
    int column;
409
    uint8_t *fst;
410 411 412 413
    uint8_t *dst;
    uint8_t *src;
    uint8_t col[4];

414 415 416 417 418
    col[0] = c;
    col[1] = c >> 8;
    col[2] = c >> 16;
    col[3] = c >> 24;

419
    fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
420

421 422 423 424 425
    if (line--) {
        dst = fst;
        src = col;
        for (column = width; column > 0; column--) {
            *(dst++) = *(src++);
426
            if (src - col == surface_bytes_per_pixel(surface)) {
427
                src = col;
428 429
            }
        }
430 431 432 433 434
        dst = fst;
        for (; line > 0; line--) {
            dst += bypl;
            memcpy(dst, fst, width);
        }
435 436 437 438 439 440 441 442 443 444 445 446 447 448
    }

    vmsvga_update_rect_delayed(s, x, y, w, h);
}
#endif

struct vmsvga_cursor_definition_s {
    int width;
    int height;
    int id;
    int bpp;
    int hot_x;
    int hot_y;
    uint32_t mask[1024];
D
Dave Airlie 已提交
449
    uint32_t image[4096];
450 451
};

B
BALATON Zoltan 已提交
452 453
#define SVGA_BITMAP_SIZE(w, h)          ((((w) + 31) >> 5) * (h))
#define SVGA_PIXMAP_SIZE(w, h, bpp)     (((((w) * (bpp)) + 31) >> 5) * (h))
454 455 456 457 458

#ifdef HW_MOUSE_ACCEL
static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
                struct vmsvga_cursor_definition_s *c)
{
459 460 461 462 463 464 465 466
    QEMUCursor *qc;
    int i, pixels;

    qc = cursor_alloc(c->width, c->height);
    qc->hot_x = c->hot_x;
    qc->hot_y = c->hot_y;
    switch (c->bpp) {
    case 1:
B
BALATON Zoltan 已提交
467 468
        cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
                        1, (void *)c->mask);
469 470 471 472 473 474
#ifdef DEBUG
        cursor_print_ascii_art(qc, "vmware/mono");
#endif
        break;
    case 32:
        /* fill alpha channel from mask, set color to zero */
B
BALATON Zoltan 已提交
475 476
        cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
                        1, (void *)c->mask);
477 478 479 480 481 482 483 484 485 486 487
        /* add in rgb values */
        pixels = c->width * c->height;
        for (i = 0; i < pixels; i++) {
            qc->data[i] |= c->image[i] & 0xffffff;
        }
#ifdef DEBUG
        cursor_print_ascii_art(qc, "vmware/32bit");
#endif
        break;
    default:
        fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
B
BALATON Zoltan 已提交
488
                __func__, c->bpp);
489 490 491
        cursor_put(qc);
        qc = cursor_builtin_left_ptr();
    }
492

493
    dpy_cursor_define(s->vga.con, qc);
494
    cursor_put(qc);
495 496 497
}
#endif

B
BALATON Zoltan 已提交
498
#define CMD(f)  le32_to_cpu(s->cmd->f)
499

500
static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
501
{
502
    int num;
B
BALATON Zoltan 已提交
503 504

    if (!s->config || !s->enable) {
505
        return 0;
B
BALATON Zoltan 已提交
506
    }
507
    num = CMD(next_cmd) - CMD(stop);
B
BALATON Zoltan 已提交
508
    if (num < 0) {
509
        num += CMD(max) - CMD(min);
B
BALATON Zoltan 已提交
510
    }
511
    return num >> 2;
512 513
}

514
static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
515
{
516
    uint32_t cmd = s->fifo[CMD(stop) >> 2];
B
BALATON Zoltan 已提交
517

518
    s->cmd->stop = cpu_to_le32(CMD(stop) + 4);
B
BALATON Zoltan 已提交
519
    if (CMD(stop) >= CMD(max)) {
520
        s->cmd->stop = s->cmd->min;
B
BALATON Zoltan 已提交
521
    }
522 523 524
    return cmd;
}

525 526 527 528 529
static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
{
    return le32_to_cpu(vmsvga_fifo_read_raw(s));
}

530 531 532
static void vmsvga_fifo_run(struct vmsvga_state_s *s)
{
    uint32_t cmd, colour;
533
    int args, len;
534 535
    int x, y, dx, dy, width, height;
    struct vmsvga_cursor_definition_s cursor;
536 537 538 539 540 541 542
    uint32_t cmd_start;

    len = vmsvga_fifo_length(s);
    while (len > 0) {
        /* May need to go back to the start of the command if incomplete */
        cmd_start = s->cmd->stop;

543 544 545
        switch (cmd = vmsvga_fifo_read(s)) {
        case SVGA_CMD_UPDATE:
        case SVGA_CMD_UPDATE_VERBOSE:
546
            len -= 5;
B
BALATON Zoltan 已提交
547
            if (len < 0) {
548
                goto rewind;
B
BALATON Zoltan 已提交
549
            }
550

551 552 553 554 555 556 557 558
            x = vmsvga_fifo_read(s);
            y = vmsvga_fifo_read(s);
            width = vmsvga_fifo_read(s);
            height = vmsvga_fifo_read(s);
            vmsvga_update_rect_delayed(s, x, y, width, height);
            break;

        case SVGA_CMD_RECT_FILL:
559
            len -= 6;
B
BALATON Zoltan 已提交
560
            if (len < 0) {
561
                goto rewind;
B
BALATON Zoltan 已提交
562
            }
563

564 565 566 567 568 569 570 571 572
            colour = vmsvga_fifo_read(s);
            x = vmsvga_fifo_read(s);
            y = vmsvga_fifo_read(s);
            width = vmsvga_fifo_read(s);
            height = vmsvga_fifo_read(s);
#ifdef HW_FILL_ACCEL
            vmsvga_fill_rect(s, colour, x, y, width, height);
            break;
#else
573
            args = 0;
574 575 576 577
            goto badcmd;
#endif

        case SVGA_CMD_RECT_COPY:
578
            len -= 7;
B
BALATON Zoltan 已提交
579
            if (len < 0) {
580
                goto rewind;
B
BALATON Zoltan 已提交
581
            }
582

583 584 585 586 587 588 589 590 591 592
            x = vmsvga_fifo_read(s);
            y = vmsvga_fifo_read(s);
            dx = vmsvga_fifo_read(s);
            dy = vmsvga_fifo_read(s);
            width = vmsvga_fifo_read(s);
            height = vmsvga_fifo_read(s);
#ifdef HW_RECT_ACCEL
            vmsvga_copy_rect(s, x, y, dx, dy, width, height);
            break;
#else
593
            args = 0;
594 595 596 597
            goto badcmd;
#endif

        case SVGA_CMD_DEFINE_CURSOR:
598
            len -= 8;
B
BALATON Zoltan 已提交
599
            if (len < 0) {
600
                goto rewind;
B
BALATON Zoltan 已提交
601
            }
602

603 604 605 606 607 608 609
            cursor.id = vmsvga_fifo_read(s);
            cursor.hot_x = vmsvga_fifo_read(s);
            cursor.hot_y = vmsvga_fifo_read(s);
            cursor.width = x = vmsvga_fifo_read(s);
            cursor.height = y = vmsvga_fifo_read(s);
            vmsvga_fifo_read(s);
            cursor.bpp = vmsvga_fifo_read(s);
610

611
            args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
612
            if (SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
B
BALATON Zoltan 已提交
613
                SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image) {
614
                    goto badcmd;
B
BALATON Zoltan 已提交
615
            }
616 617

            len -= args;
B
BALATON Zoltan 已提交
618
            if (len < 0) {
619
                goto rewind;
B
BALATON Zoltan 已提交
620
            }
621

B
BALATON Zoltan 已提交
622
            for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args++) {
623
                cursor.mask[args] = vmsvga_fifo_read_raw(s);
B
BALATON Zoltan 已提交
624 625
            }
            for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args++) {
626
                cursor.image[args] = vmsvga_fifo_read_raw(s);
B
BALATON Zoltan 已提交
627
            }
628 629 630 631 632 633 634 635 636 637 638 639 640
#ifdef HW_MOUSE_ACCEL
            vmsvga_cursor_define(s, &cursor);
            break;
#else
            args = 0;
            goto badcmd;
#endif

        /*
         * Other commands that we at least know the number of arguments
         * for so we can avoid FIFO desync if driver uses them illegally.
         */
        case SVGA_CMD_DEFINE_ALPHA_CURSOR:
641
            len -= 6;
B
BALATON Zoltan 已提交
642
            if (len < 0) {
643
                goto rewind;
B
BALATON Zoltan 已提交
644
            }
645 646 647 648 649 650 651 652 653 654 655 656 657 658
            vmsvga_fifo_read(s);
            vmsvga_fifo_read(s);
            vmsvga_fifo_read(s);
            x = vmsvga_fifo_read(s);
            y = vmsvga_fifo_read(s);
            args = x * y;
            goto badcmd;
        case SVGA_CMD_RECT_ROP_FILL:
            args = 6;
            goto badcmd;
        case SVGA_CMD_RECT_ROP_COPY:
            args = 7;
            goto badcmd;
        case SVGA_CMD_DRAW_GLYPH_CLIPPED:
659
            len -= 4;
B
BALATON Zoltan 已提交
660
            if (len < 0) {
661
                goto rewind;
B
BALATON Zoltan 已提交
662
            }
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
            vmsvga_fifo_read(s);
            vmsvga_fifo_read(s);
            args = 7 + (vmsvga_fifo_read(s) >> 2);
            goto badcmd;
        case SVGA_CMD_SURFACE_ALPHA_BLEND:
            args = 12;
            goto badcmd;

        /*
         * Other commands that are not listed as depending on any
         * CAPABILITIES bits, but are not described in the README either.
         */
        case SVGA_CMD_SURFACE_FILL:
        case SVGA_CMD_SURFACE_COPY:
        case SVGA_CMD_FRONT_ROP_FILL:
        case SVGA_CMD_FENCE:
        case SVGA_CMD_INVALID_CMD:
            break; /* Nop */

        default:
683
            args = 0;
684
        badcmd:
685
            len -= args;
B
BALATON Zoltan 已提交
686
            if (len < 0) {
687
                goto rewind;
B
BALATON Zoltan 已提交
688 689
            }
            while (args--) {
690
                vmsvga_fifo_read(s);
B
BALATON Zoltan 已提交
691
            }
692
            printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
B
BALATON Zoltan 已提交
693
                   __func__, cmd);
694
            break;
695 696 697 698

        rewind:
            s->cmd->stop = cmd_start;
            break;
699
        }
700
    }
701 702 703 704 705 706

    s->syncing = 0;
}

static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
{
707
    struct vmsvga_state_s *s = opaque;
B
BALATON Zoltan 已提交
708

709 710 711 712 713
    return s->index;
}

static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
{
714
    struct vmsvga_state_s *s = opaque;
B
BALATON Zoltan 已提交
715

716 717 718 719 720 721
    s->index = index;
}

static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
{
    uint32_t caps;
722
    struct vmsvga_state_s *s = opaque;
723
    DisplaySurface *surface = qemu_console_surface(s->vga.con);
B
BALATON Zoltan 已提交
724

725 726 727 728 729 730 731 732
    switch (s->index) {
    case SVGA_REG_ID:
        return s->svgaid;

    case SVGA_REG_ENABLE:
        return s->enable;

    case SVGA_REG_WIDTH:
733
        return surface_width(surface);
734 735

    case SVGA_REG_HEIGHT:
736
        return surface_height(surface);
737 738 739 740 741

    case SVGA_REG_MAX_WIDTH:
        return SVGA_MAX_WIDTH;

    case SVGA_REG_MAX_HEIGHT:
742
        return SVGA_MAX_HEIGHT;
743 744

    case SVGA_REG_DEPTH:
745
        return s->depth;
746 747

    case SVGA_REG_BITS_PER_PIXEL:
748
        return (s->depth + 7) & ~7;
749 750 751 752 753

    case SVGA_REG_PSEUDOCOLOR:
        return 0x0;

    case SVGA_REG_RED_MASK:
754
        return surface->pf.rmask;
755

756
    case SVGA_REG_GREEN_MASK:
757
        return surface->pf.gmask;
758

759
    case SVGA_REG_BLUE_MASK:
760
        return surface->pf.bmask;
761 762

    case SVGA_REG_BYTES_PER_LINE:
763
        return s->bypp * s->new_width;
764

765 766 767 768 769
    case SVGA_REG_FB_START: {
        struct pci_vmsvga_state_s *pci_vmsvga
            = container_of(s, struct pci_vmsvga_state_s, chip);
        return pci_get_bar_addr(&pci_vmsvga->card, 1);
    }
770 771 772 773 774

    case SVGA_REG_FB_OFFSET:
        return 0x0;

    case SVGA_REG_VRAM_SIZE:
775
        return s->vga.vram_size; /* No physical VRAM besides the framebuffer */
776 777

    case SVGA_REG_FB_SIZE:
778
        return s->vga.vram_size;
779 780 781 782 783 784 785 786 787 788

    case SVGA_REG_CAPABILITIES:
        caps = SVGA_CAP_NONE;
#ifdef HW_RECT_ACCEL
        caps |= SVGA_CAP_RECT_COPY;
#endif
#ifdef HW_FILL_ACCEL
        caps |= SVGA_CAP_RECT_FILL;
#endif
#ifdef HW_MOUSE_ACCEL
789
        if (dpy_cursor_define_supported(s->vga.con)) {
790 791
            caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
                    SVGA_CAP_CURSOR_BYPASS;
792
        }
793 794 795
#endif
        return caps;

796 797 798 799 800
    case SVGA_REG_MEM_START: {
        struct pci_vmsvga_state_s *pci_vmsvga
            = container_of(s, struct pci_vmsvga_state_s, chip);
        return pci_get_bar_addr(&pci_vmsvga->card, 2);
    }
801 802

    case SVGA_REG_MEM_SIZE:
803
        return s->fifo_size;
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

    case SVGA_REG_CONFIG_DONE:
        return s->config;

    case SVGA_REG_SYNC:
    case SVGA_REG_BUSY:
        return s->syncing;

    case SVGA_REG_GUEST_ID:
        return s->guest;

    case SVGA_REG_CURSOR_ID:
        return s->cursor.id;

    case SVGA_REG_CURSOR_X:
        return s->cursor.x;

    case SVGA_REG_CURSOR_Y:
        return s->cursor.x;

    case SVGA_REG_CURSOR_ON:
        return s->cursor.on;

    case SVGA_REG_HOST_BITS_PER_PIXEL:
828
        return (s->depth + 7) & ~7;
829 830 831 832 833 834 835 836 837 838 839 840

    case SVGA_REG_SCRATCH_SIZE:
        return s->scratch_size;

    case SVGA_REG_MEM_REGS:
    case SVGA_REG_NUM_DISPLAYS:
    case SVGA_REG_PITCHLOCK:
    case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
        return 0;

    default:
        if (s->index >= SVGA_SCRATCH_BASE &&
B
BALATON Zoltan 已提交
841
            s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
842
            return s->scratch[s->index - SVGA_SCRATCH_BASE];
B
BALATON Zoltan 已提交
843 844
        }
        printf("%s: Bad register %02x\n", __func__, s->index);
845 846 847 848 849 850 851
    }

    return 0;
}

static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
{
852
    struct vmsvga_state_s *s = opaque;
B
BALATON Zoltan 已提交
853

854 855
    switch (s->index) {
    case SVGA_REG_ID:
B
BALATON Zoltan 已提交
856
        if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0) {
857
            s->svgaid = value;
B
BALATON Zoltan 已提交
858
        }
859 860 861
        break;

    case SVGA_REG_ENABLE:
862
        s->enable = !!value;
863
        s->invalidated = 1;
864
        s->vga.invalidate(&s->vga);
865
        if (s->enable && s->config) {
866 867 868 869
            vga_dirty_log_stop(&s->vga);
        } else {
            vga_dirty_log_start(&s->vga);
        }
870 871 872
        break;

    case SVGA_REG_WIDTH:
873 874 875 876 877 878
        if (value <= SVGA_MAX_WIDTH) {
            s->new_width = value;
            s->invalidated = 1;
        } else {
            printf("%s: Bad width: %i\n", __func__, value);
        }
879 880 881
        break;

    case SVGA_REG_HEIGHT:
882 883 884 885 886 887
        if (value <= SVGA_MAX_HEIGHT) {
            s->new_height = value;
            s->invalidated = 1;
        } else {
            printf("%s: Bad height: %i\n", __func__, value);
        }
888 889 890
        break;

    case SVGA_REG_BITS_PER_PIXEL:
891
        if (value != s->depth) {
892
            printf("%s: Bad bits per pixel: %i bits\n", __func__, value);
893 894 895 896 897 898
            s->config = 0;
        }
        break;

    case SVGA_REG_CONFIG_DONE:
        if (value) {
899
            s->fifo = (uint32_t *) s->fifo_ptr;
900
            /* Check range and alignment.  */
B
BALATON Zoltan 已提交
901
            if ((CMD(min) | CMD(max) | CMD(next_cmd) | CMD(stop)) & 3) {
902
                break;
B
BALATON Zoltan 已提交
903 904
            }
            if (CMD(min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo) {
905
                break;
B
BALATON Zoltan 已提交
906 907
            }
            if (CMD(max) > SVGA_FIFO_SIZE) {
908
                break;
B
BALATON Zoltan 已提交
909 910
            }
            if (CMD(max) < CMD(min) + 10 * 1024) {
911
                break;
B
BALATON Zoltan 已提交
912
            }
913
            vga_dirty_log_stop(&s->vga);
914
        }
915
        s->config = !!value;
916 917 918 919 920 921 922 923 924 925 926
        break;

    case SVGA_REG_SYNC:
        s->syncing = 1;
        vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
        break;

    case SVGA_REG_GUEST_ID:
        s->guest = value;
#ifdef VERBOSE
        if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
B
BALATON Zoltan 已提交
927 928 929 930
            ARRAY_SIZE(vmsvga_guest_id)) {
            printf("%s: guest runs %s.\n", __func__,
                   vmsvga_guest_id[value - GUEST_OS_BASE]);
        }
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
#endif
        break;

    case SVGA_REG_CURSOR_ID:
        s->cursor.id = value;
        break;

    case SVGA_REG_CURSOR_X:
        s->cursor.x = value;
        break;

    case SVGA_REG_CURSOR_Y:
        s->cursor.y = value;
        break;

    case SVGA_REG_CURSOR_ON:
        s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
        s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
#ifdef HW_MOUSE_ACCEL
950
        if (value <= SVGA_CURSOR_ON_SHOW) {
951
            dpy_mouse_set(s->vga.con, s->cursor.x, s->cursor.y, s->cursor.on);
952
        }
953 954 955
#endif
        break;

956
    case SVGA_REG_DEPTH:
957 958 959 960 961 962 963 964 965 966 967 968
    case SVGA_REG_MEM_REGS:
    case SVGA_REG_NUM_DISPLAYS:
    case SVGA_REG_PITCHLOCK:
    case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
        break;

    default:
        if (s->index >= SVGA_SCRATCH_BASE &&
                s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
            s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
            break;
        }
B
BALATON Zoltan 已提交
969
        printf("%s: Bad register %02x\n", __func__, s->index);
970 971 972 973 974
    }
}

static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
{
B
BALATON Zoltan 已提交
975
    printf("%s: what are we supposed to return?\n", __func__);
976 977 978 979 980
    return 0xcafe;
}

static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
{
B
BALATON Zoltan 已提交
981
    printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
982 983
}

984
static inline void vmsvga_check_size(struct vmsvga_state_s *s)
985
{
986 987 988 989 990
    DisplaySurface *surface = qemu_console_surface(s->vga.con);

    if (s->new_width != surface_width(surface) ||
        s->new_height != surface_height(surface)) {
        qemu_console_resize(s->vga.con, s->new_width, s->new_height);
991 992 993 994 995 996
        s->invalidated = 1;
    }
}

static void vmsvga_update_display(void *opaque)
{
997
    struct vmsvga_state_s *s = opaque;
998
    DisplaySurface *surface;
999 1000
    bool dirty = false;

1001
    if (!s->enable) {
1002
        s->vga.update(&s->vga);
1003 1004 1005
        return;
    }

1006
    vmsvga_check_size(s);
1007
    surface = qemu_console_surface(s->vga.con);
1008 1009 1010 1011 1012 1013 1014 1015

    vmsvga_fifo_run(s);
    vmsvga_update_rect_flush(s);

    /*
     * Is it more efficient to look at vram VGA-dirty bits or wait
     * for the driver to issue SVGA_CMD_UPDATE?
     */
1016 1017 1018
    if (memory_region_is_logging(&s->vga.vram)) {
        vga_sync_dirty_bitmap(&s->vga);
        dirty = memory_region_get_dirty(&s->vga.vram, 0,
1019
            surface_stride(surface) * surface_height(surface),
1020 1021 1022
            DIRTY_MEMORY_VGA);
    }
    if (s->invalidated || dirty) {
1023
        s->invalidated = 0;
1024 1025 1026 1027
        memcpy(surface_data(surface), s->vga.vram_ptr,
               surface_stride(surface) * surface_height(surface));
        dpy_gfx_update(s->vga.con, 0, 0,
                   surface_width(surface), surface_height(surface));
1028 1029 1030
    }
    if (dirty) {
        memory_region_reset_dirty(&s->vga.vram, 0,
1031
            surface_stride(surface) * surface_height(surface),
1032
            DIRTY_MEMORY_VGA);
1033 1034 1035
    }
}

J
Jan Kiszka 已提交
1036
static void vmsvga_reset(DeviceState *dev)
1037
{
J
Jan Kiszka 已提交
1038 1039 1040 1041
    struct pci_vmsvga_state_s *pci =
        DO_UPCAST(struct pci_vmsvga_state_s, card.qdev, dev);
    struct vmsvga_state_s *s = &pci->chip;

1042 1043 1044 1045 1046 1047 1048 1049
    s->index = 0;
    s->enable = 0;
    s->config = 0;
    s->svgaid = SVGA_ID;
    s->cursor.on = 0;
    s->redraw_fifo_first = 0;
    s->redraw_fifo_last = 0;
    s->syncing = 0;
1050 1051

    vga_dirty_log_start(&s->vga);
1052 1053 1054 1055
}

static void vmsvga_invalidate_display(void *opaque)
{
1056
    struct vmsvga_state_s *s = opaque;
1057
    if (!s->enable) {
1058
        s->vga.invalidate(&s->vga);
1059 1060 1061 1062 1063 1064
        return;
    }

    s->invalidated = 1;
}

1065 1066
/* save the vga display in a PPM image even if no display is
   available */
1067 1068
static void vmsvga_screen_dump(void *opaque, const char *filename, bool cswitch,
                               Error **errp)
1069
{
1070
    struct vmsvga_state_s *s = opaque;
1071 1072
    DisplaySurface *surface = qemu_console_surface(s->vga.con);

1073
    if (!s->enable) {
1074
        s->vga.screen_dump(&s->vga, filename, cswitch, errp);
1075 1076 1077
        return;
    }

1078
    if (surface_bits_per_pixel(surface) == 32) {
1079
        DisplaySurface *ds = qemu_create_displaysurface_from(
1080 1081
                                 surface_width(surface),
                                 surface_height(surface),
1082
                                 32,
1083
                                 surface_stride(surface),
G
Gerd Hoffmann 已提交
1084
                                 s->vga.vram_ptr, false);
1085
        ppm_save(filename, ds, errp);
1086
        g_free(ds);
1087
    }
1088 1089
}

A
Anthony Liguori 已提交
1090
static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
B
balrog 已提交
1091
{
1092
    struct vmsvga_state_s *s = opaque;
B
balrog 已提交
1093

B
BALATON Zoltan 已提交
1094
    if (s->vga.text_update) {
1095
        s->vga.text_update(&s->vga, chardata);
B
BALATON Zoltan 已提交
1096
    }
B
balrog 已提交
1097 1098
}

J
Juan Quintela 已提交
1099
static int vmsvga_post_load(void *opaque, int version_id)
1100
{
J
Juan Quintela 已提交
1101
    struct vmsvga_state_s *s = opaque;
1102 1103

    s->invalidated = 1;
B
BALATON Zoltan 已提交
1104
    if (s->config) {
1105
        s->fifo = (uint32_t *) s->fifo_ptr;
B
BALATON Zoltan 已提交
1106
    }
1107 1108 1109
    return 0;
}

B
Blue Swirl 已提交
1110
static const VMStateDescription vmstate_vmware_vga_internal = {
J
Juan Quintela 已提交
1111 1112 1113 1114 1115
    .name = "vmware_vga_internal",
    .version_id = 0,
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
    .post_load = vmsvga_post_load,
B
BALATON Zoltan 已提交
1116
    .fields      = (VMStateField[]) {
1117
        VMSTATE_INT32_EQUAL(depth, struct vmsvga_state_s),
J
Juan Quintela 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
        VMSTATE_INT32(enable, struct vmsvga_state_s),
        VMSTATE_INT32(config, struct vmsvga_state_s),
        VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
        VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
        VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
        VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
        VMSTATE_INT32(index, struct vmsvga_state_s),
        VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
                             scratch_size, 0, vmstate_info_uint32, uint32_t),
        VMSTATE_INT32(new_width, struct vmsvga_state_s),
        VMSTATE_INT32(new_height, struct vmsvga_state_s),
        VMSTATE_UINT32(guest, struct vmsvga_state_s),
        VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
        VMSTATE_INT32(syncing, struct vmsvga_state_s),
1132
        VMSTATE_UNUSED(4), /* was fb_size */
J
Juan Quintela 已提交
1133 1134 1135 1136
        VMSTATE_END_OF_LIST()
    }
};

B
Blue Swirl 已提交
1137
static const VMStateDescription vmstate_vmware_vga = {
J
Juan Quintela 已提交
1138 1139 1140 1141
    .name = "vmware_vga",
    .version_id = 0,
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
B
BALATON Zoltan 已提交
1142
    .fields      = (VMStateField[]) {
J
Juan Quintela 已提交
1143 1144 1145 1146 1147 1148 1149
        VMSTATE_PCI_DEVICE(card, struct pci_vmsvga_state_s),
        VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
                       vmstate_vmware_vga_internal, struct vmsvga_state_s),
        VMSTATE_END_OF_LIST()
    }
};

G
Gerd Hoffmann 已提交
1150
static void vmsvga_init(struct vmsvga_state_s *s,
1151
                        MemoryRegion *address_space, MemoryRegion *io)
1152
{
1153 1154
    DisplaySurface *surface;

1155
    s->scratch_size = SVGA_SCRATCH_SIZE;
1156
    s->scratch = g_malloc(s->scratch_size * 4);
1157

1158 1159 1160 1161 1162
    s->vga.con = graphic_console_init(vmsvga_update_display,
                                      vmsvga_invalidate_display,
                                      vmsvga_screen_dump,
                                      vmsvga_text_update, s);
    surface = qemu_console_surface(s->vga.con);
1163

1164
    s->fifo_size = SVGA_FIFO_SIZE;
1165 1166
    memory_region_init_ram(&s->fifo_ram, "vmsvga.fifo", s->fifo_size);
    vmstate_register_ram_global(&s->fifo_ram);
1167
    s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
1168

G
Gerd Hoffmann 已提交
1169
    vga_common_init(&s->vga);
1170
    vga_init(&s->vga, address_space, io, true);
A
Alex Williamson 已提交
1171
    vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
1172 1173
    /* Save some values here in case they are changed later.
     * This is suspicious and needs more though why it is needed. */
1174 1175
    s->depth = surface_bits_per_pixel(surface);
    s->bypp = surface_bytes_per_pixel(surface);
1176 1177
}

1178
static uint64_t vmsvga_io_read(void *opaque, hwaddr addr, unsigned size)
1179
{
1180 1181 1182 1183 1184 1185 1186 1187
    struct vmsvga_state_s *s = opaque;

    switch (addr) {
    case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
    case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
    case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
    default: return -1u;
    }
1188 1189
}

A
Avi Kivity 已提交
1190
static void vmsvga_io_write(void *opaque, hwaddr addr,
1191
                            uint64_t data, unsigned size)
1192
{
1193
    struct vmsvga_state_s *s = opaque;
1194

1195 1196
    switch (addr) {
    case SVGA_IO_MUL * SVGA_INDEX_PORT:
B
Blue Swirl 已提交
1197 1198
        vmsvga_index_write(s, addr, data);
        break;
1199
    case SVGA_IO_MUL * SVGA_VALUE_PORT:
B
Blue Swirl 已提交
1200 1201
        vmsvga_value_write(s, addr, data);
        break;
1202
    case SVGA_IO_MUL * SVGA_BIOS_PORT:
B
Blue Swirl 已提交
1203 1204
        vmsvga_bios_write(s, addr, data);
        break;
1205
    }
1206 1207
}

1208 1209 1210 1211 1212 1213 1214 1215 1216
static const MemoryRegionOps vmsvga_io_ops = {
    .read = vmsvga_io_read,
    .write = vmsvga_io_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
};
1217

1218
static int pci_vmsvga_initfn(PCIDevice *dev)
1219
{
G
Gerd Hoffmann 已提交
1220 1221
    struct pci_vmsvga_state_s *s =
        DO_UPCAST(struct pci_vmsvga_state_s, card, dev);
1222

B
BALATON Zoltan 已提交
1223 1224 1225
    s->card.config[PCI_CACHE_LINE_SIZE] = 0x08;         /* Cache line size */
    s->card.config[PCI_LATENCY_TIMER] = 0x40;           /* Latency timer */
    s->card.config[PCI_INTERRUPT_LINE] = 0xff;          /* End */
1226

1227 1228
    memory_region_init_io(&s->io_bar, &vmsvga_io_ops, &s->chip,
                          "vmsvga-io", 0x10);
1229
    memory_region_set_flush_coalesced(&s->io_bar);
1230
    pci_register_bar(&s->card, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1231

1232
    vmsvga_init(&s->chip, pci_address_space(dev), pci_address_space_io(dev));
1233

1234 1235
    pci_register_bar(&s->card, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
                     &s->chip.vga.vram);
1236 1237
    pci_register_bar(&s->card, 2, PCI_BASE_ADDRESS_MEM_PREFETCH,
                     &s->chip.fifo_ram);
1238

1239 1240
    if (!dev->rom_bar) {
        /* compatibility with pc-0.13 and older */
1241
        vga_init_vbe(&s->chip.vga, pci_address_space(dev));
1242 1243
    }

1244
    return 0;
1245
}
G
Gerd Hoffmann 已提交
1246

G
Gerd Hoffmann 已提交
1247 1248
static Property vga_vmware_properties[] = {
    DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s,
G
Gerd Hoffmann 已提交
1249
                       chip.vga.vram_size_mb, 16),
G
Gerd Hoffmann 已提交
1250 1251 1252
    DEFINE_PROP_END_OF_LIST(),
};

1253 1254
static void vmsvga_class_init(ObjectClass *klass, void *data)
{
1255
    DeviceClass *dc = DEVICE_CLASS(klass);
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->no_hotplug = 1;
    k->init = pci_vmsvga_initfn;
    k->romfile = "vgabios-vmware.bin";
    k->vendor_id = PCI_VENDOR_ID_VMWARE;
    k->device_id = SVGA_PCI_DEVICE_ID;
    k->class_id = PCI_CLASS_DISPLAY_VGA;
    k->subsystem_vendor_id = PCI_VENDOR_ID_VMWARE;
    k->subsystem_id = SVGA_PCI_DEVICE_ID;
1266 1267
    dc->reset = vmsvga_reset;
    dc->vmsd = &vmstate_vmware_vga;
G
Gerd Hoffmann 已提交
1268
    dc->props = vga_vmware_properties;
1269 1270
}

1271
static const TypeInfo vmsvga_info = {
1272 1273 1274 1275
    .name          = "vmware-svga",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(struct pci_vmsvga_state_s),
    .class_init    = vmsvga_class_init,
G
Gerd Hoffmann 已提交
1276 1277
};

A
Andreas Färber 已提交
1278
static void vmsvga_register_types(void)
G
Gerd Hoffmann 已提交
1279
{
1280
    type_register_static(&vmsvga_info);
G
Gerd Hoffmann 已提交
1281
}
A
Andreas Färber 已提交
1282 1283

type_init(vmsvga_register_types)