2 * QEMU VMware-SVGA "chipset".
4 * Copyright (c) 2007 Andrzej Zaborowski <balrog@zabor.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "vmware_vga.h"
34 #define HW_MOUSE_ACCEL
38 struct vmsvga_state_s {
55 target_phys_addr_t vram_base;
70 ram_addr_t fifo_offset;
72 unsigned int fifo_size;
73 target_phys_addr_t fifo_base;
77 struct __attribute__((__packed__)) {
82 /* Add registers here when adding capabilities. */
87 #define REDRAW_FIFO_LEN 512
88 struct vmsvga_rect_s {
90 } redraw_fifo[REDRAW_FIFO_LEN];
91 int redraw_fifo_first, redraw_fifo_last;
94 struct pci_vmsvga_state_s {
96 struct vmsvga_state_s chip;
99 #define SVGA_MAGIC 0x900000UL
100 #define SVGA_MAKE_ID(ver) (SVGA_MAGIC << 8 | (ver))
101 #define SVGA_ID_0 SVGA_MAKE_ID(0)
102 #define SVGA_ID_1 SVGA_MAKE_ID(1)
103 #define SVGA_ID_2 SVGA_MAKE_ID(2)
105 #define SVGA_LEGACY_BASE_PORT 0x4560
106 #define SVGA_INDEX_PORT 0x0
107 #define SVGA_VALUE_PORT 0x1
108 #define SVGA_BIOS_PORT 0x2
110 #define SVGA_VERSION_2
112 #ifdef SVGA_VERSION_2
113 # define SVGA_ID SVGA_ID_2
114 # define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
115 # define SVGA_IO_MUL 1
116 # define SVGA_FIFO_SIZE 0x10000
117 # define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA2
119 # define SVGA_ID SVGA_ID_1
120 # define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
121 # define SVGA_IO_MUL 4
122 # define SVGA_FIFO_SIZE 0x10000
123 # define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA
127 /* ID 0, 1 and 2 registers */
132 SVGA_REG_MAX_WIDTH = 4,
133 SVGA_REG_MAX_HEIGHT = 5,
135 SVGA_REG_BITS_PER_PIXEL = 7, /* Current bpp in the guest */
136 SVGA_REG_PSEUDOCOLOR = 8,
137 SVGA_REG_RED_MASK = 9,
138 SVGA_REG_GREEN_MASK = 10,
139 SVGA_REG_BLUE_MASK = 11,
140 SVGA_REG_BYTES_PER_LINE = 12,
141 SVGA_REG_FB_START = 13,
142 SVGA_REG_FB_OFFSET = 14,
143 SVGA_REG_VRAM_SIZE = 15,
144 SVGA_REG_FB_SIZE = 16,
146 /* ID 1 and 2 registers */
147 SVGA_REG_CAPABILITIES = 17,
148 SVGA_REG_MEM_START = 18, /* Memory for command FIFO */
149 SVGA_REG_MEM_SIZE = 19,
150 SVGA_REG_CONFIG_DONE = 20, /* Set when memory area configured */
151 SVGA_REG_SYNC = 21, /* Write to force synchronization */
152 SVGA_REG_BUSY = 22, /* Read to check if sync is done */
153 SVGA_REG_GUEST_ID = 23, /* Set guest OS identifier */
154 SVGA_REG_CURSOR_ID = 24, /* ID of cursor */
155 SVGA_REG_CURSOR_X = 25, /* Set cursor X position */
156 SVGA_REG_CURSOR_Y = 26, /* Set cursor Y position */
157 SVGA_REG_CURSOR_ON = 27, /* Turn cursor on/off */
158 SVGA_REG_HOST_BITS_PER_PIXEL = 28, /* Current bpp in the host */
159 SVGA_REG_SCRATCH_SIZE = 29, /* Number of scratch registers */
160 SVGA_REG_MEM_REGS = 30, /* Number of FIFO registers */
161 SVGA_REG_NUM_DISPLAYS = 31, /* Number of guest displays */
162 SVGA_REG_PITCHLOCK = 32, /* Fixed pitch for all modes */
164 SVGA_PALETTE_BASE = 1024, /* Base of SVGA color map */
165 SVGA_PALETTE_END = SVGA_PALETTE_BASE + 767,
166 SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
169 #define SVGA_CAP_NONE 0
170 #define SVGA_CAP_RECT_FILL (1 << 0)
171 #define SVGA_CAP_RECT_COPY (1 << 1)
172 #define SVGA_CAP_RECT_PAT_FILL (1 << 2)
173 #define SVGA_CAP_LEGACY_OFFSCREEN (1 << 3)
174 #define SVGA_CAP_RASTER_OP (1 << 4)
175 #define SVGA_CAP_CURSOR (1 << 5)
176 #define SVGA_CAP_CURSOR_BYPASS (1 << 6)
177 #define SVGA_CAP_CURSOR_BYPASS_2 (1 << 7)
178 #define SVGA_CAP_8BIT_EMULATION (1 << 8)
179 #define SVGA_CAP_ALPHA_CURSOR (1 << 9)
180 #define SVGA_CAP_GLYPH (1 << 10)
181 #define SVGA_CAP_GLYPH_CLIPPING (1 << 11)
182 #define SVGA_CAP_OFFSCREEN_1 (1 << 12)
183 #define SVGA_CAP_ALPHA_BLEND (1 << 13)
184 #define SVGA_CAP_3D (1 << 14)
185 #define SVGA_CAP_EXTENDED_FIFO (1 << 15)
186 #define SVGA_CAP_MULTIMON (1 << 16)
187 #define SVGA_CAP_PITCHLOCK (1 << 17)
190 * FIFO offsets (seen as an array of 32-bit words)
194 * The original defined FIFO offsets
197 SVGA_FIFO_MAX, /* The distance from MIN to MAX must be at least 10K */
202 * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
204 SVGA_FIFO_CAPABILITIES = 4,
207 SVGA_FIFO_3D_HWVERSION,
211 #define SVGA_FIFO_CAP_NONE 0
212 #define SVGA_FIFO_CAP_FENCE (1 << 0)
213 #define SVGA_FIFO_CAP_ACCELFRONT (1 << 1)
214 #define SVGA_FIFO_CAP_PITCHLOCK (1 << 2)
216 #define SVGA_FIFO_FLAG_NONE 0
217 #define SVGA_FIFO_FLAG_ACCELFRONT (1 << 0)
219 /* These values can probably be changed arbitrarily. */
220 #define SVGA_SCRATCH_SIZE 0x8000
221 #define SVGA_MAX_WIDTH 2360
222 #define SVGA_MAX_HEIGHT 1770
225 # define GUEST_OS_BASE 0x5001
226 static const char *vmsvga_guest_id[] = {
228 [0x01] = "Windows 3.1",
229 [0x02] = "Windows 95",
230 [0x03] = "Windows 98",
231 [0x04] = "Windows ME",
232 [0x05] = "Windows NT",
233 [0x06] = "Windows 2000",
236 [0x09] = "an unknown OS",
239 [0x0c] = "an unknown OS",
240 [0x0d] = "an unknown OS",
241 [0x0e] = "an unknown OS",
242 [0x0f] = "an unknown OS",
243 [0x10] = "an unknown OS",
244 [0x11] = "an unknown OS",
245 [0x12] = "an unknown OS",
246 [0x13] = "an unknown OS",
247 [0x14] = "an unknown OS",
248 [0x15] = "Windows 2003",
253 SVGA_CMD_INVALID_CMD = 0,
255 SVGA_CMD_RECT_FILL = 2,
256 SVGA_CMD_RECT_COPY = 3,
257 SVGA_CMD_DEFINE_BITMAP = 4,
258 SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
259 SVGA_CMD_DEFINE_PIXMAP = 6,
260 SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
261 SVGA_CMD_RECT_BITMAP_FILL = 8,
262 SVGA_CMD_RECT_PIXMAP_FILL = 9,
263 SVGA_CMD_RECT_BITMAP_COPY = 10,
264 SVGA_CMD_RECT_PIXMAP_COPY = 11,
265 SVGA_CMD_FREE_OBJECT = 12,
266 SVGA_CMD_RECT_ROP_FILL = 13,
267 SVGA_CMD_RECT_ROP_COPY = 14,
268 SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
269 SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
270 SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
271 SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
272 SVGA_CMD_DEFINE_CURSOR = 19,
273 SVGA_CMD_DISPLAY_CURSOR = 20,
274 SVGA_CMD_MOVE_CURSOR = 21,
275 SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
276 SVGA_CMD_DRAW_GLYPH = 23,
277 SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
278 SVGA_CMD_UPDATE_VERBOSE = 25,
279 SVGA_CMD_SURFACE_FILL = 26,
280 SVGA_CMD_SURFACE_COPY = 27,
281 SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
282 SVGA_CMD_FRONT_ROP_FILL = 29,
286 /* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
288 SVGA_CURSOR_ON_HIDE = 0,
289 SVGA_CURSOR_ON_SHOW = 1,
290 SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
291 SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
294 static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
295 int x, int y, int w, int h)
305 if (x + w > s->width) {
306 fprintf(stderr, "%s: update width too large x: %d, w: %d\n",
308 x = MIN(x, s->width);
312 if (y + h > s->height) {
313 fprintf(stderr, "%s: update height too large y: %d, h: %d\n",
315 y = MIN(y, s->height);
320 bypl = s->bypp * s->width;
322 start = s->bypp * x + bypl * y;
323 src = s->vga.vram_ptr + start;
324 dst = ds_get_data(s->vga.ds) + start;
326 for (; line > 0; line --, src += bypl, dst += bypl)
327 memcpy(dst, src, width);
330 dpy_update(s->vga.ds, x, y, w, h);
333 static inline void vmsvga_update_screen(struct vmsvga_state_s *s)
336 memcpy(ds_get_data(s->vga.ds), s->vga.vram_ptr, s->bypp * s->width * s->height);
339 dpy_update(s->vga.ds, 0, 0, s->width, s->height);
343 # define vmsvga_update_rect_delayed vmsvga_update_rect
345 static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
346 int x, int y, int w, int h)
348 struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last ++];
349 s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
357 static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
359 struct vmsvga_rect_s *rect;
360 if (s->invalidated) {
361 s->redraw_fifo_first = s->redraw_fifo_last;
364 /* Overlapping region updates can be optimised out here - if someone
365 * knows a smart algorithm to do that, please share. */
366 while (s->redraw_fifo_first != s->redraw_fifo_last) {
367 rect = &s->redraw_fifo[s->redraw_fifo_first ++];
368 s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
369 vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
374 static inline void vmsvga_copy_rect(struct vmsvga_state_s *s,
375 int x0, int y0, int x1, int y1, int w, int h)
378 uint8_t *vram = ds_get_data(s->ds);
380 uint8_t *vram = s->vga.vram_ptr;
382 int bypl = s->bypp * s->width;
383 int width = s->bypp * w;
389 qemu_console_copy(s->ds, x0, y0, x1, y1, w, h);
394 ptr[0] = vram + s->bypp * x0 + bypl * (y0 + h - 1);
395 ptr[1] = vram + s->bypp * x1 + bypl * (y1 + h - 1);
396 for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl)
397 memmove(ptr[1], ptr[0], width);
399 ptr[0] = vram + s->bypp * x0 + bypl * y0;
400 ptr[1] = vram + s->bypp * x1 + bypl * y1;
401 for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl)
402 memmove(ptr[1], ptr[0], width);
406 vmsvga_update_rect_delayed(s, x1, y1, w, h);
411 static inline void vmsvga_fill_rect(struct vmsvga_state_s *s,
412 uint32_t c, int x, int y, int w, int h)
415 uint8_t *vram = ds_get_data(s->ds);
417 uint8_t *vram = s->vga.vram_ptr;
420 int bypl = bypp * s->width;
421 int width = bypp * w;
424 uint8_t *fst = vram + bypp * x + bypl * y;
431 s->ds->dpy_fill(s->ds, x, y, w, h, c);
443 for (column = width; column > 0; column --) {
444 *(dst ++) = *(src ++);
445 if (src - col == bypp)
449 for (; line > 0; line --) {
451 memcpy(dst, fst, width);
456 vmsvga_update_rect_delayed(s, x, y, w, h);
460 struct vmsvga_cursor_definition_s {
468 uint32_t image[4096];
471 #define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
472 #define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
474 #ifdef HW_MOUSE_ACCEL
475 static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
476 struct vmsvga_cursor_definition_s *c)
481 qc = cursor_alloc(c->width, c->height);
482 qc->hot_x = c->hot_x;
483 qc->hot_y = c->hot_y;
486 cursor_set_mono(qc, 0xffffff, 0x000000, (void*)c->image,
489 cursor_print_ascii_art(qc, "vmware/mono");
493 /* fill alpha channel from mask, set color to zero */
494 cursor_set_mono(qc, 0x000000, 0x000000, (void*)c->mask,
496 /* add in rgb values */
497 pixels = c->width * c->height;
498 for (i = 0; i < pixels; i++) {
499 qc->data[i] |= c->image[i] & 0xffffff;
502 cursor_print_ascii_art(qc, "vmware/32bit");
506 fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
507 __FUNCTION__, c->bpp);
509 qc = cursor_builtin_left_ptr();
512 if (s->vga.ds->cursor_define)
513 s->vga.ds->cursor_define(qc);
518 #define CMD(f) le32_to_cpu(s->cmd->f)
520 static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
523 if (!s->config || !s->enable)
525 num = CMD(next_cmd) - CMD(stop);
527 num += CMD(max) - CMD(min);
531 static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
533 uint32_t cmd = s->fifo[CMD(stop) >> 2];
534 s->cmd->stop = cpu_to_le32(CMD(stop) + 4);
535 if (CMD(stop) >= CMD(max))
536 s->cmd->stop = s->cmd->min;
540 static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
542 return le32_to_cpu(vmsvga_fifo_read_raw(s));
545 static void vmsvga_fifo_run(struct vmsvga_state_s *s)
547 uint32_t cmd, colour;
549 int x, y, dx, dy, width, height;
550 struct vmsvga_cursor_definition_s cursor;
553 len = vmsvga_fifo_length(s);
555 /* May need to go back to the start of the command if incomplete */
556 cmd_start = s->cmd->stop;
558 switch (cmd = vmsvga_fifo_read(s)) {
559 case SVGA_CMD_UPDATE:
560 case SVGA_CMD_UPDATE_VERBOSE:
565 x = vmsvga_fifo_read(s);
566 y = vmsvga_fifo_read(s);
567 width = vmsvga_fifo_read(s);
568 height = vmsvga_fifo_read(s);
569 vmsvga_update_rect_delayed(s, x, y, width, height);
572 case SVGA_CMD_RECT_FILL:
577 colour = vmsvga_fifo_read(s);
578 x = vmsvga_fifo_read(s);
579 y = vmsvga_fifo_read(s);
580 width = vmsvga_fifo_read(s);
581 height = vmsvga_fifo_read(s);
583 vmsvga_fill_rect(s, colour, x, y, width, height);
590 case SVGA_CMD_RECT_COPY:
595 x = vmsvga_fifo_read(s);
596 y = vmsvga_fifo_read(s);
597 dx = vmsvga_fifo_read(s);
598 dy = vmsvga_fifo_read(s);
599 width = vmsvga_fifo_read(s);
600 height = vmsvga_fifo_read(s);
602 vmsvga_copy_rect(s, x, y, dx, dy, width, height);
609 case SVGA_CMD_DEFINE_CURSOR:
614 cursor.id = vmsvga_fifo_read(s);
615 cursor.hot_x = vmsvga_fifo_read(s);
616 cursor.hot_y = vmsvga_fifo_read(s);
617 cursor.width = x = vmsvga_fifo_read(s);
618 cursor.height = y = vmsvga_fifo_read(s);
620 cursor.bpp = vmsvga_fifo_read(s);
622 args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
623 if (SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
624 SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image)
631 for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args ++)
632 cursor.mask[args] = vmsvga_fifo_read_raw(s);
633 for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args ++)
634 cursor.image[args] = vmsvga_fifo_read_raw(s);
635 #ifdef HW_MOUSE_ACCEL
636 vmsvga_cursor_define(s, &cursor);
644 * Other commands that we at least know the number of arguments
645 * for so we can avoid FIFO desync if driver uses them illegally.
647 case SVGA_CMD_DEFINE_ALPHA_CURSOR:
655 x = vmsvga_fifo_read(s);
656 y = vmsvga_fifo_read(s);
659 case SVGA_CMD_RECT_ROP_FILL:
662 case SVGA_CMD_RECT_ROP_COPY:
665 case SVGA_CMD_DRAW_GLYPH_CLIPPED:
672 args = 7 + (vmsvga_fifo_read(s) >> 2);
674 case SVGA_CMD_SURFACE_ALPHA_BLEND:
679 * Other commands that are not listed as depending on any
680 * CAPABILITIES bits, but are not described in the README either.
682 case SVGA_CMD_SURFACE_FILL:
683 case SVGA_CMD_SURFACE_COPY:
684 case SVGA_CMD_FRONT_ROP_FILL:
686 case SVGA_CMD_INVALID_CMD:
697 printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
702 s->cmd->stop = cmd_start;
710 static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
712 struct vmsvga_state_s *s = opaque;
716 static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
718 struct vmsvga_state_s *s = opaque;
722 static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
725 struct vmsvga_state_s *s = opaque;
730 case SVGA_REG_ENABLE:
736 case SVGA_REG_HEIGHT:
739 case SVGA_REG_MAX_WIDTH:
740 return SVGA_MAX_WIDTH;
742 case SVGA_REG_MAX_HEIGHT:
743 return SVGA_MAX_HEIGHT;
748 case SVGA_REG_BITS_PER_PIXEL:
749 return (s->depth + 7) & ~7;
751 case SVGA_REG_PSEUDOCOLOR:
754 case SVGA_REG_RED_MASK:
756 case SVGA_REG_GREEN_MASK:
758 case SVGA_REG_BLUE_MASK:
761 case SVGA_REG_BYTES_PER_LINE:
762 return ((s->depth + 7) >> 3) * s->new_width;
764 case SVGA_REG_FB_START:
767 case SVGA_REG_FB_OFFSET:
770 case SVGA_REG_VRAM_SIZE:
771 return s->vga.vram_size;
773 case SVGA_REG_FB_SIZE:
776 case SVGA_REG_CAPABILITIES:
777 caps = SVGA_CAP_NONE;
779 caps |= SVGA_CAP_RECT_COPY;
782 caps |= SVGA_CAP_RECT_FILL;
784 #ifdef HW_MOUSE_ACCEL
785 if (s->vga.ds->mouse_set)
786 caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
787 SVGA_CAP_CURSOR_BYPASS;
791 case SVGA_REG_MEM_START:
794 case SVGA_REG_MEM_SIZE:
797 case SVGA_REG_CONFIG_DONE:
804 case SVGA_REG_GUEST_ID:
807 case SVGA_REG_CURSOR_ID:
810 case SVGA_REG_CURSOR_X:
813 case SVGA_REG_CURSOR_Y:
816 case SVGA_REG_CURSOR_ON:
819 case SVGA_REG_HOST_BITS_PER_PIXEL:
820 return (s->depth + 7) & ~7;
822 case SVGA_REG_SCRATCH_SIZE:
823 return s->scratch_size;
825 case SVGA_REG_MEM_REGS:
826 case SVGA_REG_NUM_DISPLAYS:
827 case SVGA_REG_PITCHLOCK:
828 case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
832 if (s->index >= SVGA_SCRATCH_BASE &&
833 s->index < SVGA_SCRATCH_BASE + s->scratch_size)
834 return s->scratch[s->index - SVGA_SCRATCH_BASE];
835 printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
841 static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
843 struct vmsvga_state_s *s = opaque;
846 if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0)
850 case SVGA_REG_ENABLE:
852 s->config &= !!value;
856 s->vga.invalidate(&s->vga);
858 s->fb_size = ((s->depth + 7) >> 3) * s->new_width * s->new_height;
859 vga_dirty_log_stop(&s->vga);
861 vga_dirty_log_start(&s->vga);
866 s->new_width = value;
870 case SVGA_REG_HEIGHT:
871 s->new_height = value;
876 case SVGA_REG_BITS_PER_PIXEL:
877 if (value != s->depth) {
878 printf("%s: Bad colour depth: %i bits\n", __FUNCTION__, value);
883 case SVGA_REG_CONFIG_DONE:
885 s->fifo = (uint32_t *) s->fifo_ptr;
886 /* Check range and alignment. */
887 if ((CMD(min) | CMD(max) |
888 CMD(next_cmd) | CMD(stop)) & 3)
890 if (CMD(min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo)
892 if (CMD(max) > SVGA_FIFO_SIZE)
894 if (CMD(max) < CMD(min) + 10 * 1024)
902 vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
905 case SVGA_REG_GUEST_ID:
908 if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
909 ARRAY_SIZE(vmsvga_guest_id))
910 printf("%s: guest runs %s.\n", __FUNCTION__,
911 vmsvga_guest_id[value - GUEST_OS_BASE]);
915 case SVGA_REG_CURSOR_ID:
916 s->cursor.id = value;
919 case SVGA_REG_CURSOR_X:
923 case SVGA_REG_CURSOR_Y:
927 case SVGA_REG_CURSOR_ON:
928 s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
929 s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
930 #ifdef HW_MOUSE_ACCEL
931 if (s->vga.ds->mouse_set && value <= SVGA_CURSOR_ON_SHOW)
932 s->vga.ds->mouse_set(s->cursor.x, s->cursor.y, s->cursor.on);
936 case SVGA_REG_MEM_REGS:
937 case SVGA_REG_NUM_DISPLAYS:
938 case SVGA_REG_PITCHLOCK:
939 case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
943 if (s->index >= SVGA_SCRATCH_BASE &&
944 s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
945 s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
948 printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
952 static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
954 printf("%s: what are we supposed to return?\n", __FUNCTION__);
958 static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
960 printf("%s: what are we supposed to do with (%08x)?\n",
964 static inline void vmsvga_size(struct vmsvga_state_s *s)
966 if (s->new_width != s->width || s->new_height != s->height) {
967 s->width = s->new_width;
968 s->height = s->new_height;
969 qemu_console_resize(s->vga.ds, s->width, s->height);
974 static void vmsvga_update_display(void *opaque)
976 struct vmsvga_state_s *s = opaque;
978 s->vga.update(&s->vga);
985 vmsvga_update_rect_flush(s);
988 * Is it more efficient to look at vram VGA-dirty bits or wait
989 * for the driver to issue SVGA_CMD_UPDATE?
991 if (s->invalidated) {
993 vmsvga_update_screen(s);
997 static void vmsvga_reset(struct vmsvga_state_s *s)
1004 s->svgaid = SVGA_ID;
1005 s->depth = ds_get_bits_per_pixel(s->vga.ds);
1006 s->bypp = ds_get_bytes_per_pixel(s->vga.ds);
1008 s->redraw_fifo_first = 0;
1009 s->redraw_fifo_last = 0;
1012 s->wred = 0x00000007;
1013 s->wgreen = 0x00000038;
1014 s->wblue = 0x000000c0;
1017 s->wred = 0x0000001f;
1018 s->wgreen = 0x000003e0;
1019 s->wblue = 0x00007c00;
1022 s->wred = 0x0000001f;
1023 s->wgreen = 0x000007e0;
1024 s->wblue = 0x0000f800;
1027 s->wred = 0x00ff0000;
1028 s->wgreen = 0x0000ff00;
1029 s->wblue = 0x000000ff;
1032 s->wred = 0x00ff0000;
1033 s->wgreen = 0x0000ff00;
1034 s->wblue = 0x000000ff;
1039 vga_dirty_log_start(&s->vga);
1042 static void vmsvga_invalidate_display(void *opaque)
1044 struct vmsvga_state_s *s = opaque;
1046 s->vga.invalidate(&s->vga);
1053 /* save the vga display in a PPM image even if no display is
1055 static void vmsvga_screen_dump(void *opaque, const char *filename)
1057 struct vmsvga_state_s *s = opaque;
1059 s->vga.screen_dump(&s->vga, filename);
1063 if (s->depth == 32) {
1064 DisplaySurface *ds = qemu_create_displaysurface_from(s->width,
1065 s->height, 32, ds_get_linesize(s->vga.ds), s->vga.vram_ptr);
1066 ppm_save(filename, ds);
1071 static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
1073 struct vmsvga_state_s *s = opaque;
1075 if (s->vga.text_update)
1076 s->vga.text_update(&s->vga, chardata);
1080 static uint32_t vmsvga_vram_readb(void *opaque, target_phys_addr_t addr)
1082 struct vmsvga_state_s *s = opaque;
1083 if (addr < s->fb_size)
1084 return *(uint8_t *) (ds_get_data(s->ds) + addr);
1086 return *(uint8_t *) (s->vram_ptr + addr);
1089 static uint32_t vmsvga_vram_readw(void *opaque, target_phys_addr_t addr)
1091 struct vmsvga_state_s *s = opaque;
1092 if (addr < s->fb_size)
1093 return *(uint16_t *) (ds_get_data(s->ds) + addr);
1095 return *(uint16_t *) (s->vram_ptr + addr);
1098 static uint32_t vmsvga_vram_readl(void *opaque, target_phys_addr_t addr)
1100 struct vmsvga_state_s *s = opaque;
1101 if (addr < s->fb_size)
1102 return *(uint32_t *) (ds_get_data(s->ds) + addr);
1104 return *(uint32_t *) (s->vram_ptr + addr);
1107 static void vmsvga_vram_writeb(void *opaque, target_phys_addr_t addr,
1110 struct vmsvga_state_s *s = opaque;
1111 if (addr < s->fb_size)
1112 *(uint8_t *) (ds_get_data(s->ds) + addr) = value;
1114 *(uint8_t *) (s->vram_ptr + addr) = value;
1117 static void vmsvga_vram_writew(void *opaque, target_phys_addr_t addr,
1120 struct vmsvga_state_s *s = opaque;
1121 if (addr < s->fb_size)
1122 *(uint16_t *) (ds_get_data(s->ds) + addr) = value;
1124 *(uint16_t *) (s->vram_ptr + addr) = value;
1127 static void vmsvga_vram_writel(void *opaque, target_phys_addr_t addr,
1130 struct vmsvga_state_s *s = opaque;
1131 if (addr < s->fb_size)
1132 *(uint32_t *) (ds_get_data(s->ds) + addr) = value;
1134 *(uint32_t *) (s->vram_ptr + addr) = value;
1137 static CPUReadMemoryFunc * const vmsvga_vram_read[] = {
1143 static CPUWriteMemoryFunc * const vmsvga_vram_write[] = {
1150 static int vmsvga_post_load(void *opaque, int version_id)
1152 struct vmsvga_state_s *s = opaque;
1156 s->fifo = (uint32_t *) s->fifo_ptr;
1161 static const VMStateDescription vmstate_vmware_vga_internal = {
1162 .name = "vmware_vga_internal",
1164 .minimum_version_id = 0,
1165 .minimum_version_id_old = 0,
1166 .post_load = vmsvga_post_load,
1167 .fields = (VMStateField []) {
1168 VMSTATE_INT32_EQUAL(depth, struct vmsvga_state_s),
1169 VMSTATE_INT32(enable, struct vmsvga_state_s),
1170 VMSTATE_INT32(config, struct vmsvga_state_s),
1171 VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
1172 VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
1173 VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
1174 VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
1175 VMSTATE_INT32(index, struct vmsvga_state_s),
1176 VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
1177 scratch_size, 0, vmstate_info_uint32, uint32_t),
1178 VMSTATE_INT32(new_width, struct vmsvga_state_s),
1179 VMSTATE_INT32(new_height, struct vmsvga_state_s),
1180 VMSTATE_UINT32(guest, struct vmsvga_state_s),
1181 VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
1182 VMSTATE_INT32(syncing, struct vmsvga_state_s),
1183 VMSTATE_INT32(fb_size, struct vmsvga_state_s),
1184 VMSTATE_END_OF_LIST()
1188 static const VMStateDescription vmstate_vmware_vga = {
1189 .name = "vmware_vga",
1191 .minimum_version_id = 0,
1192 .minimum_version_id_old = 0,
1193 .fields = (VMStateField []) {
1194 VMSTATE_PCI_DEVICE(card, struct pci_vmsvga_state_s),
1195 VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
1196 vmstate_vmware_vga_internal, struct vmsvga_state_s),
1197 VMSTATE_END_OF_LIST()
1201 static void vmsvga_init(struct vmsvga_state_s *s, int vga_ram_size)
1203 s->scratch_size = SVGA_SCRATCH_SIZE;
1204 s->scratch = qemu_malloc(s->scratch_size * 4);
1206 s->vga.ds = graphic_console_init(vmsvga_update_display,
1207 vmsvga_invalidate_display,
1209 vmsvga_text_update, s);
1212 s->fifo_size = SVGA_FIFO_SIZE;
1213 s->fifo_offset = qemu_ram_alloc(NULL, "vmsvga.fifo", s->fifo_size);
1214 s->fifo_ptr = qemu_get_ram_ptr(s->fifo_offset);
1216 vga_common_init(&s->vga, vga_ram_size);
1218 vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
1223 static void pci_vmsvga_map_ioport(PCIDevice *pci_dev, int region_num,
1224 pcibus_t addr, pcibus_t size, int type)
1226 struct pci_vmsvga_state_s *d = (struct pci_vmsvga_state_s *) pci_dev;
1227 struct vmsvga_state_s *s = &d->chip;
1229 register_ioport_read(addr + SVGA_IO_MUL * SVGA_INDEX_PORT,
1230 1, 4, vmsvga_index_read, s);
1231 register_ioport_write(addr + SVGA_IO_MUL * SVGA_INDEX_PORT,
1232 1, 4, vmsvga_index_write, s);
1233 register_ioport_read(addr + SVGA_IO_MUL * SVGA_VALUE_PORT,
1234 1, 4, vmsvga_value_read, s);
1235 register_ioport_write(addr + SVGA_IO_MUL * SVGA_VALUE_PORT,
1236 1, 4, vmsvga_value_write, s);
1237 register_ioport_read(addr + SVGA_IO_MUL * SVGA_BIOS_PORT,
1238 1, 4, vmsvga_bios_read, s);
1239 register_ioport_write(addr + SVGA_IO_MUL * SVGA_BIOS_PORT,
1240 1, 4, vmsvga_bios_write, s);
1243 static void pci_vmsvga_map_mem(PCIDevice *pci_dev, int region_num,
1244 pcibus_t addr, pcibus_t size, int type)
1246 struct pci_vmsvga_state_s *d = (struct pci_vmsvga_state_s *) pci_dev;
1247 struct vmsvga_state_s *s = &d->chip;
1248 ram_addr_t iomemtype;
1250 s->vram_base = addr;
1252 iomemtype = cpu_register_io_memory(vmsvga_vram_read,
1253 vmsvga_vram_write, s, DEVICE_NATIVE_ENDIAN);
1255 iomemtype = s->vga.vram_offset | IO_MEM_RAM;
1257 cpu_register_physical_memory(s->vram_base, s->vga.vram_size,
1260 s->vga.map_addr = addr;
1261 s->vga.map_end = addr + s->vga.vram_size;
1262 vga_dirty_log_restart(&s->vga);
1265 static void pci_vmsvga_map_fifo(PCIDevice *pci_dev, int region_num,
1266 pcibus_t addr, pcibus_t size, int type)
1268 struct pci_vmsvga_state_s *d = (struct pci_vmsvga_state_s *) pci_dev;
1269 struct vmsvga_state_s *s = &d->chip;
1270 ram_addr_t iomemtype;
1272 s->fifo_base = addr;
1273 iomemtype = s->fifo_offset | IO_MEM_RAM;
1274 cpu_register_physical_memory(s->fifo_base, s->fifo_size,
1278 static int pci_vmsvga_initfn(PCIDevice *dev)
1280 struct pci_vmsvga_state_s *s =
1281 DO_UPCAST(struct pci_vmsvga_state_s, card, dev);
1283 s->card.config[PCI_CACHE_LINE_SIZE] = 0x08; /* Cache line size */
1284 s->card.config[PCI_LATENCY_TIMER] = 0x40; /* Latency timer */
1285 s->card.config[PCI_INTERRUPT_LINE] = 0xff; /* End */
1287 pci_register_bar(&s->card, 0, 0x10,
1288 PCI_BASE_ADDRESS_SPACE_IO, pci_vmsvga_map_ioport);
1289 pci_register_bar(&s->card, 1, VGA_RAM_SIZE,
1290 PCI_BASE_ADDRESS_MEM_PREFETCH, pci_vmsvga_map_mem);
1292 pci_register_bar(&s->card, 2, SVGA_FIFO_SIZE,
1293 PCI_BASE_ADDRESS_MEM_PREFETCH, pci_vmsvga_map_fifo);
1295 vmsvga_init(&s->chip, VGA_RAM_SIZE);
1297 if (!dev->rom_bar) {
1298 /* compatibility with pc-0.13 and older */
1299 vga_init_vbe(&s->chip.vga);
1305 static PCIDeviceInfo vmsvga_info = {
1306 .qdev.name = "vmware-svga",
1307 .qdev.size = sizeof(struct pci_vmsvga_state_s),
1308 .qdev.vmsd = &vmstate_vmware_vga,
1310 .init = pci_vmsvga_initfn,
1311 .romfile = "vgabios-vmware.bin",
1313 .vendor_id = PCI_VENDOR_ID_VMWARE,
1314 .device_id = SVGA_PCI_DEVICE_ID,
1315 .class_id = PCI_CLASS_DISPLAY_VGA,
1316 .subsystem_vendor_id = PCI_VENDOR_ID_VMWARE,
1317 .subsystem_id = SVGA_PCI_DEVICE_ID,
1320 static void vmsvga_register(void)
1322 pci_qdev_register(&vmsvga_info);
1324 device_init(vmsvga_register);