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
25 #include "hw/loader.h"
26 #include "ui/console.h"
27 #include "hw/pci/pci.h"
32 #define HW_MOUSE_ACCEL
36 /* See http://vmware-svga.sf.net/ for some documentation on VMWare SVGA */
38 struct vmsvga_state_s
{
61 MemoryRegion fifo_ram
;
63 unsigned int fifo_size
;
72 /* Add registers here when adding capabilities. */
77 #define REDRAW_FIFO_LEN 512
78 struct vmsvga_rect_s
{
80 } redraw_fifo
[REDRAW_FIFO_LEN
];
81 int redraw_fifo_first
, redraw_fifo_last
;
84 struct pci_vmsvga_state_s
{
86 struct vmsvga_state_s chip
;
90 #define SVGA_MAGIC 0x900000UL
91 #define SVGA_MAKE_ID(ver) (SVGA_MAGIC << 8 | (ver))
92 #define SVGA_ID_0 SVGA_MAKE_ID(0)
93 #define SVGA_ID_1 SVGA_MAKE_ID(1)
94 #define SVGA_ID_2 SVGA_MAKE_ID(2)
96 #define SVGA_LEGACY_BASE_PORT 0x4560
97 #define SVGA_INDEX_PORT 0x0
98 #define SVGA_VALUE_PORT 0x1
99 #define SVGA_BIOS_PORT 0x2
101 #define SVGA_VERSION_2
103 #ifdef SVGA_VERSION_2
104 # define SVGA_ID SVGA_ID_2
105 # define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
106 # define SVGA_IO_MUL 1
107 # define SVGA_FIFO_SIZE 0x10000
108 # define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA2
110 # define SVGA_ID SVGA_ID_1
111 # define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
112 # define SVGA_IO_MUL 4
113 # define SVGA_FIFO_SIZE 0x10000
114 # define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA
118 /* ID 0, 1 and 2 registers */
123 SVGA_REG_MAX_WIDTH
= 4,
124 SVGA_REG_MAX_HEIGHT
= 5,
126 SVGA_REG_BITS_PER_PIXEL
= 7, /* Current bpp in the guest */
127 SVGA_REG_PSEUDOCOLOR
= 8,
128 SVGA_REG_RED_MASK
= 9,
129 SVGA_REG_GREEN_MASK
= 10,
130 SVGA_REG_BLUE_MASK
= 11,
131 SVGA_REG_BYTES_PER_LINE
= 12,
132 SVGA_REG_FB_START
= 13,
133 SVGA_REG_FB_OFFSET
= 14,
134 SVGA_REG_VRAM_SIZE
= 15,
135 SVGA_REG_FB_SIZE
= 16,
137 /* ID 1 and 2 registers */
138 SVGA_REG_CAPABILITIES
= 17,
139 SVGA_REG_MEM_START
= 18, /* Memory for command FIFO */
140 SVGA_REG_MEM_SIZE
= 19,
141 SVGA_REG_CONFIG_DONE
= 20, /* Set when memory area configured */
142 SVGA_REG_SYNC
= 21, /* Write to force synchronization */
143 SVGA_REG_BUSY
= 22, /* Read to check if sync is done */
144 SVGA_REG_GUEST_ID
= 23, /* Set guest OS identifier */
145 SVGA_REG_CURSOR_ID
= 24, /* ID of cursor */
146 SVGA_REG_CURSOR_X
= 25, /* Set cursor X position */
147 SVGA_REG_CURSOR_Y
= 26, /* Set cursor Y position */
148 SVGA_REG_CURSOR_ON
= 27, /* Turn cursor on/off */
149 SVGA_REG_HOST_BITS_PER_PIXEL
= 28, /* Current bpp in the host */
150 SVGA_REG_SCRATCH_SIZE
= 29, /* Number of scratch registers */
151 SVGA_REG_MEM_REGS
= 30, /* Number of FIFO registers */
152 SVGA_REG_NUM_DISPLAYS
= 31, /* Number of guest displays */
153 SVGA_REG_PITCHLOCK
= 32, /* Fixed pitch for all modes */
155 SVGA_PALETTE_BASE
= 1024, /* Base of SVGA color map */
156 SVGA_PALETTE_END
= SVGA_PALETTE_BASE
+ 767,
157 SVGA_SCRATCH_BASE
= SVGA_PALETTE_BASE
+ 768,
160 #define SVGA_CAP_NONE 0
161 #define SVGA_CAP_RECT_FILL (1 << 0)
162 #define SVGA_CAP_RECT_COPY (1 << 1)
163 #define SVGA_CAP_RECT_PAT_FILL (1 << 2)
164 #define SVGA_CAP_LEGACY_OFFSCREEN (1 << 3)
165 #define SVGA_CAP_RASTER_OP (1 << 4)
166 #define SVGA_CAP_CURSOR (1 << 5)
167 #define SVGA_CAP_CURSOR_BYPASS (1 << 6)
168 #define SVGA_CAP_CURSOR_BYPASS_2 (1 << 7)
169 #define SVGA_CAP_8BIT_EMULATION (1 << 8)
170 #define SVGA_CAP_ALPHA_CURSOR (1 << 9)
171 #define SVGA_CAP_GLYPH (1 << 10)
172 #define SVGA_CAP_GLYPH_CLIPPING (1 << 11)
173 #define SVGA_CAP_OFFSCREEN_1 (1 << 12)
174 #define SVGA_CAP_ALPHA_BLEND (1 << 13)
175 #define SVGA_CAP_3D (1 << 14)
176 #define SVGA_CAP_EXTENDED_FIFO (1 << 15)
177 #define SVGA_CAP_MULTIMON (1 << 16)
178 #define SVGA_CAP_PITCHLOCK (1 << 17)
181 * FIFO offsets (seen as an array of 32-bit words)
185 * The original defined FIFO offsets
188 SVGA_FIFO_MAX
, /* The distance from MIN to MAX must be at least 10K */
193 * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
195 SVGA_FIFO_CAPABILITIES
= 4,
198 SVGA_FIFO_3D_HWVERSION
,
202 #define SVGA_FIFO_CAP_NONE 0
203 #define SVGA_FIFO_CAP_FENCE (1 << 0)
204 #define SVGA_FIFO_CAP_ACCELFRONT (1 << 1)
205 #define SVGA_FIFO_CAP_PITCHLOCK (1 << 2)
207 #define SVGA_FIFO_FLAG_NONE 0
208 #define SVGA_FIFO_FLAG_ACCELFRONT (1 << 0)
210 /* These values can probably be changed arbitrarily. */
211 #define SVGA_SCRATCH_SIZE 0x8000
212 #define SVGA_MAX_WIDTH 2360
213 #define SVGA_MAX_HEIGHT 1770
216 # define GUEST_OS_BASE 0x5001
217 static const char *vmsvga_guest_id
[] = {
219 [0x01] = "Windows 3.1",
220 [0x02] = "Windows 95",
221 [0x03] = "Windows 98",
222 [0x04] = "Windows ME",
223 [0x05] = "Windows NT",
224 [0x06] = "Windows 2000",
227 [0x09] = "an unknown OS",
230 [0x0c] = "an unknown OS",
231 [0x0d] = "an unknown OS",
232 [0x0e] = "an unknown OS",
233 [0x0f] = "an unknown OS",
234 [0x10] = "an unknown OS",
235 [0x11] = "an unknown OS",
236 [0x12] = "an unknown OS",
237 [0x13] = "an unknown OS",
238 [0x14] = "an unknown OS",
239 [0x15] = "Windows 2003",
244 SVGA_CMD_INVALID_CMD
= 0,
246 SVGA_CMD_RECT_FILL
= 2,
247 SVGA_CMD_RECT_COPY
= 3,
248 SVGA_CMD_DEFINE_BITMAP
= 4,
249 SVGA_CMD_DEFINE_BITMAP_SCANLINE
= 5,
250 SVGA_CMD_DEFINE_PIXMAP
= 6,
251 SVGA_CMD_DEFINE_PIXMAP_SCANLINE
= 7,
252 SVGA_CMD_RECT_BITMAP_FILL
= 8,
253 SVGA_CMD_RECT_PIXMAP_FILL
= 9,
254 SVGA_CMD_RECT_BITMAP_COPY
= 10,
255 SVGA_CMD_RECT_PIXMAP_COPY
= 11,
256 SVGA_CMD_FREE_OBJECT
= 12,
257 SVGA_CMD_RECT_ROP_FILL
= 13,
258 SVGA_CMD_RECT_ROP_COPY
= 14,
259 SVGA_CMD_RECT_ROP_BITMAP_FILL
= 15,
260 SVGA_CMD_RECT_ROP_PIXMAP_FILL
= 16,
261 SVGA_CMD_RECT_ROP_BITMAP_COPY
= 17,
262 SVGA_CMD_RECT_ROP_PIXMAP_COPY
= 18,
263 SVGA_CMD_DEFINE_CURSOR
= 19,
264 SVGA_CMD_DISPLAY_CURSOR
= 20,
265 SVGA_CMD_MOVE_CURSOR
= 21,
266 SVGA_CMD_DEFINE_ALPHA_CURSOR
= 22,
267 SVGA_CMD_DRAW_GLYPH
= 23,
268 SVGA_CMD_DRAW_GLYPH_CLIPPED
= 24,
269 SVGA_CMD_UPDATE_VERBOSE
= 25,
270 SVGA_CMD_SURFACE_FILL
= 26,
271 SVGA_CMD_SURFACE_COPY
= 27,
272 SVGA_CMD_SURFACE_ALPHA_BLEND
= 28,
273 SVGA_CMD_FRONT_ROP_FILL
= 29,
277 /* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
279 SVGA_CURSOR_ON_HIDE
= 0,
280 SVGA_CURSOR_ON_SHOW
= 1,
281 SVGA_CURSOR_ON_REMOVE_FROM_FB
= 2,
282 SVGA_CURSOR_ON_RESTORE_TO_FB
= 3,
285 static inline void vmsvga_update_rect(struct vmsvga_state_s
*s
,
286 int x
, int y
, int w
, int h
)
288 DisplaySurface
*surface
= qemu_console_surface(s
->vga
.con
);
297 fprintf(stderr
, "%s: update x was < 0 (%d)\n", __func__
, x
);
302 fprintf(stderr
, "%s: update w was < 0 (%d)\n", __func__
, w
);
305 if (x
+ w
> surface_width(surface
)) {
306 fprintf(stderr
, "%s: update width too large x: %d, w: %d\n",
308 x
= MIN(x
, surface_width(surface
));
309 w
= surface_width(surface
) - x
;
313 fprintf(stderr
, "%s: update y was < 0 (%d)\n", __func__
, y
);
318 fprintf(stderr
, "%s: update h was < 0 (%d)\n", __func__
, h
);
321 if (y
+ h
> surface_height(surface
)) {
322 fprintf(stderr
, "%s: update height too large y: %d, h: %d\n",
324 y
= MIN(y
, surface_height(surface
));
325 h
= surface_height(surface
) - y
;
328 bypl
= surface_stride(surface
);
329 width
= surface_bytes_per_pixel(surface
) * w
;
330 start
= surface_bytes_per_pixel(surface
) * x
+ bypl
* y
;
331 src
= s
->vga
.vram_ptr
+ start
;
332 dst
= surface_data(surface
) + start
;
334 for (line
= h
; line
> 0; line
--, src
+= bypl
, dst
+= bypl
) {
335 memcpy(dst
, src
, width
);
337 dpy_gfx_update(s
->vga
.con
, x
, y
, w
, h
);
340 static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s
*s
,
341 int x
, int y
, int w
, int h
)
343 struct vmsvga_rect_s
*rect
= &s
->redraw_fifo
[s
->redraw_fifo_last
++];
345 s
->redraw_fifo_last
&= REDRAW_FIFO_LEN
- 1;
352 static inline void vmsvga_update_rect_flush(struct vmsvga_state_s
*s
)
354 struct vmsvga_rect_s
*rect
;
356 if (s
->invalidated
) {
357 s
->redraw_fifo_first
= s
->redraw_fifo_last
;
360 /* Overlapping region updates can be optimised out here - if someone
361 * knows a smart algorithm to do that, please share. */
362 while (s
->redraw_fifo_first
!= s
->redraw_fifo_last
) {
363 rect
= &s
->redraw_fifo
[s
->redraw_fifo_first
++];
364 s
->redraw_fifo_first
&= REDRAW_FIFO_LEN
- 1;
365 vmsvga_update_rect(s
, rect
->x
, rect
->y
, rect
->w
, rect
->h
);
370 static inline void vmsvga_copy_rect(struct vmsvga_state_s
*s
,
371 int x0
, int y0
, int x1
, int y1
, int w
, int h
)
373 DisplaySurface
*surface
= qemu_console_surface(s
->vga
.con
);
374 uint8_t *vram
= s
->vga
.vram_ptr
;
375 int bypl
= surface_stride(surface
);
376 int bypp
= surface_bytes_per_pixel(surface
);
377 int width
= bypp
* w
;
382 ptr
[0] = vram
+ bypp
* x0
+ bypl
* (y0
+ h
- 1);
383 ptr
[1] = vram
+ bypp
* x1
+ bypl
* (y1
+ h
- 1);
384 for (; line
> 0; line
--, ptr
[0] -= bypl
, ptr
[1] -= bypl
) {
385 memmove(ptr
[1], ptr
[0], width
);
388 ptr
[0] = vram
+ bypp
* x0
+ bypl
* y0
;
389 ptr
[1] = vram
+ bypp
* x1
+ bypl
* y1
;
390 for (; line
> 0; line
--, ptr
[0] += bypl
, ptr
[1] += bypl
) {
391 memmove(ptr
[1], ptr
[0], width
);
395 vmsvga_update_rect_delayed(s
, x1
, y1
, w
, h
);
400 static inline void vmsvga_fill_rect(struct vmsvga_state_s
*s
,
401 uint32_t c
, int x
, int y
, int w
, int h
)
403 DisplaySurface
*surface
= qemu_console_surface(s
->vga
.con
);
404 int bypl
= surface_stride(surface
);
405 int width
= surface_bytes_per_pixel(surface
) * w
;
418 fst
= s
->vga
.vram_ptr
+ surface_bytes_per_pixel(surface
) * x
+ bypl
* y
;
423 for (column
= width
; column
> 0; column
--) {
425 if (src
- col
== surface_bytes_per_pixel(surface
)) {
430 for (; line
> 0; line
--) {
432 memcpy(dst
, fst
, width
);
436 vmsvga_update_rect_delayed(s
, x
, y
, w
, h
);
440 struct vmsvga_cursor_definition_s
{
448 uint32_t image
[4096];
451 #define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
452 #define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
454 #ifdef HW_MOUSE_ACCEL
455 static inline void vmsvga_cursor_define(struct vmsvga_state_s
*s
,
456 struct vmsvga_cursor_definition_s
*c
)
461 qc
= cursor_alloc(c
->width
, c
->height
);
462 qc
->hot_x
= c
->hot_x
;
463 qc
->hot_y
= c
->hot_y
;
466 cursor_set_mono(qc
, 0xffffff, 0x000000, (void *)c
->image
,
469 cursor_print_ascii_art(qc
, "vmware/mono");
473 /* fill alpha channel from mask, set color to zero */
474 cursor_set_mono(qc
, 0x000000, 0x000000, (void *)c
->mask
,
476 /* add in rgb values */
477 pixels
= c
->width
* c
->height
;
478 for (i
= 0; i
< pixels
; i
++) {
479 qc
->data
[i
] |= c
->image
[i
] & 0xffffff;
482 cursor_print_ascii_art(qc
, "vmware/32bit");
486 fprintf(stderr
, "%s: unhandled bpp %d, using fallback cursor\n",
489 qc
= cursor_builtin_left_ptr();
492 dpy_cursor_define(s
->vga
.con
, qc
);
497 #define CMD(f) le32_to_cpu(s->cmd->f)
499 static inline int vmsvga_fifo_length(struct vmsvga_state_s
*s
)
503 if (!s
->config
|| !s
->enable
) {
506 num
= CMD(next_cmd
) - CMD(stop
);
508 num
+= CMD(max
) - CMD(min
);
513 static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s
*s
)
515 uint32_t cmd
= s
->fifo
[CMD(stop
) >> 2];
517 s
->cmd
->stop
= cpu_to_le32(CMD(stop
) + 4);
518 if (CMD(stop
) >= CMD(max
)) {
519 s
->cmd
->stop
= s
->cmd
->min
;
524 static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s
*s
)
526 return le32_to_cpu(vmsvga_fifo_read_raw(s
));
529 static void vmsvga_fifo_run(struct vmsvga_state_s
*s
)
531 uint32_t cmd
, colour
;
533 int x
, y
, dx
, dy
, width
, height
;
534 struct vmsvga_cursor_definition_s cursor
;
537 len
= vmsvga_fifo_length(s
);
539 /* May need to go back to the start of the command if incomplete */
540 cmd_start
= s
->cmd
->stop
;
542 switch (cmd
= vmsvga_fifo_read(s
)) {
543 case SVGA_CMD_UPDATE
:
544 case SVGA_CMD_UPDATE_VERBOSE
:
550 x
= vmsvga_fifo_read(s
);
551 y
= vmsvga_fifo_read(s
);
552 width
= vmsvga_fifo_read(s
);
553 height
= vmsvga_fifo_read(s
);
554 vmsvga_update_rect_delayed(s
, x
, y
, width
, height
);
557 case SVGA_CMD_RECT_FILL
:
563 colour
= vmsvga_fifo_read(s
);
564 x
= vmsvga_fifo_read(s
);
565 y
= vmsvga_fifo_read(s
);
566 width
= vmsvga_fifo_read(s
);
567 height
= vmsvga_fifo_read(s
);
569 vmsvga_fill_rect(s
, colour
, x
, y
, width
, height
);
576 case SVGA_CMD_RECT_COPY
:
582 x
= vmsvga_fifo_read(s
);
583 y
= vmsvga_fifo_read(s
);
584 dx
= vmsvga_fifo_read(s
);
585 dy
= vmsvga_fifo_read(s
);
586 width
= vmsvga_fifo_read(s
);
587 height
= vmsvga_fifo_read(s
);
589 vmsvga_copy_rect(s
, x
, y
, dx
, dy
, width
, height
);
596 case SVGA_CMD_DEFINE_CURSOR
:
602 cursor
.id
= vmsvga_fifo_read(s
);
603 cursor
.hot_x
= vmsvga_fifo_read(s
);
604 cursor
.hot_y
= vmsvga_fifo_read(s
);
605 cursor
.width
= x
= vmsvga_fifo_read(s
);
606 cursor
.height
= y
= vmsvga_fifo_read(s
);
608 cursor
.bpp
= vmsvga_fifo_read(s
);
610 args
= SVGA_BITMAP_SIZE(x
, y
) + SVGA_PIXMAP_SIZE(x
, y
, cursor
.bpp
);
611 if (SVGA_BITMAP_SIZE(x
, y
) > sizeof cursor
.mask
||
612 SVGA_PIXMAP_SIZE(x
, y
, cursor
.bpp
) > sizeof cursor
.image
) {
621 for (args
= 0; args
< SVGA_BITMAP_SIZE(x
, y
); args
++) {
622 cursor
.mask
[args
] = vmsvga_fifo_read_raw(s
);
624 for (args
= 0; args
< SVGA_PIXMAP_SIZE(x
, y
, cursor
.bpp
); args
++) {
625 cursor
.image
[args
] = vmsvga_fifo_read_raw(s
);
627 #ifdef HW_MOUSE_ACCEL
628 vmsvga_cursor_define(s
, &cursor
);
636 * Other commands that we at least know the number of arguments
637 * for so we can avoid FIFO desync if driver uses them illegally.
639 case SVGA_CMD_DEFINE_ALPHA_CURSOR
:
647 x
= vmsvga_fifo_read(s
);
648 y
= vmsvga_fifo_read(s
);
651 case SVGA_CMD_RECT_ROP_FILL
:
654 case SVGA_CMD_RECT_ROP_COPY
:
657 case SVGA_CMD_DRAW_GLYPH_CLIPPED
:
664 args
= 7 + (vmsvga_fifo_read(s
) >> 2);
666 case SVGA_CMD_SURFACE_ALPHA_BLEND
:
671 * Other commands that are not listed as depending on any
672 * CAPABILITIES bits, but are not described in the README either.
674 case SVGA_CMD_SURFACE_FILL
:
675 case SVGA_CMD_SURFACE_COPY
:
676 case SVGA_CMD_FRONT_ROP_FILL
:
678 case SVGA_CMD_INVALID_CMD
:
691 printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
696 s
->cmd
->stop
= cmd_start
;
704 static uint32_t vmsvga_index_read(void *opaque
, uint32_t address
)
706 struct vmsvga_state_s
*s
= opaque
;
711 static void vmsvga_index_write(void *opaque
, uint32_t address
, uint32_t index
)
713 struct vmsvga_state_s
*s
= opaque
;
718 static uint32_t vmsvga_value_read(void *opaque
, uint32_t address
)
721 struct vmsvga_state_s
*s
= opaque
;
722 DisplaySurface
*surface
= qemu_console_surface(s
->vga
.con
);
731 case SVGA_REG_ENABLE
:
736 ret
= s
->new_width
? s
->new_width
: surface_width(surface
);
739 case SVGA_REG_HEIGHT
:
740 ret
= s
->new_height
? s
->new_height
: surface_height(surface
);
743 case SVGA_REG_MAX_WIDTH
:
744 ret
= SVGA_MAX_WIDTH
;
747 case SVGA_REG_MAX_HEIGHT
:
748 ret
= SVGA_MAX_HEIGHT
;
752 ret
= (s
->new_depth
== 32) ? 24 : s
->new_depth
;
755 case SVGA_REG_BITS_PER_PIXEL
:
756 case SVGA_REG_HOST_BITS_PER_PIXEL
:
760 case SVGA_REG_PSEUDOCOLOR
:
764 case SVGA_REG_RED_MASK
:
765 pf
= qemu_default_pixelformat(s
->new_depth
);
769 case SVGA_REG_GREEN_MASK
:
770 pf
= qemu_default_pixelformat(s
->new_depth
);
774 case SVGA_REG_BLUE_MASK
:
775 pf
= qemu_default_pixelformat(s
->new_depth
);
779 case SVGA_REG_BYTES_PER_LINE
:
781 ret
= (s
->new_depth
* s
->new_width
) / 8;
783 ret
= surface_stride(surface
);
787 case SVGA_REG_FB_START
: {
788 struct pci_vmsvga_state_s
*pci_vmsvga
789 = container_of(s
, struct pci_vmsvga_state_s
, chip
);
790 ret
= pci_get_bar_addr(&pci_vmsvga
->card
, 1);
794 case SVGA_REG_FB_OFFSET
:
798 case SVGA_REG_VRAM_SIZE
:
799 ret
= s
->vga
.vram_size
; /* No physical VRAM besides the framebuffer */
802 case SVGA_REG_FB_SIZE
:
803 ret
= s
->vga
.vram_size
;
806 case SVGA_REG_CAPABILITIES
:
807 caps
= SVGA_CAP_NONE
;
809 caps
|= SVGA_CAP_RECT_COPY
;
812 caps
|= SVGA_CAP_RECT_FILL
;
814 #ifdef HW_MOUSE_ACCEL
815 if (dpy_cursor_define_supported(s
->vga
.con
)) {
816 caps
|= SVGA_CAP_CURSOR
| SVGA_CAP_CURSOR_BYPASS_2
|
817 SVGA_CAP_CURSOR_BYPASS
;
823 case SVGA_REG_MEM_START
: {
824 struct pci_vmsvga_state_s
*pci_vmsvga
825 = container_of(s
, struct pci_vmsvga_state_s
, chip
);
826 ret
= pci_get_bar_addr(&pci_vmsvga
->card
, 2);
830 case SVGA_REG_MEM_SIZE
:
834 case SVGA_REG_CONFIG_DONE
:
843 case SVGA_REG_GUEST_ID
:
847 case SVGA_REG_CURSOR_ID
:
851 case SVGA_REG_CURSOR_X
:
855 case SVGA_REG_CURSOR_Y
:
859 case SVGA_REG_CURSOR_ON
:
863 case SVGA_REG_SCRATCH_SIZE
:
864 ret
= s
->scratch_size
;
867 case SVGA_REG_MEM_REGS
:
868 case SVGA_REG_NUM_DISPLAYS
:
869 case SVGA_REG_PITCHLOCK
:
870 case SVGA_PALETTE_BASE
... SVGA_PALETTE_END
:
875 if (s
->index
>= SVGA_SCRATCH_BASE
&&
876 s
->index
< SVGA_SCRATCH_BASE
+ s
->scratch_size
) {
877 ret
= s
->scratch
[s
->index
- SVGA_SCRATCH_BASE
];
880 printf("%s: Bad register %02x\n", __func__
, s
->index
);
885 if (s
->index
>= SVGA_SCRATCH_BASE
) {
886 trace_vmware_scratch_read(s
->index
, ret
);
887 } else if (s
->index
>= SVGA_PALETTE_BASE
) {
888 trace_vmware_palette_read(s
->index
, ret
);
890 trace_vmware_value_read(s
->index
, ret
);
895 static void vmsvga_value_write(void *opaque
, uint32_t address
, uint32_t value
)
897 struct vmsvga_state_s
*s
= opaque
;
899 if (s
->index
>= SVGA_SCRATCH_BASE
) {
900 trace_vmware_scratch_write(s
->index
, value
);
901 } else if (s
->index
>= SVGA_PALETTE_BASE
) {
902 trace_vmware_palette_write(s
->index
, value
);
904 trace_vmware_value_write(s
->index
, value
);
908 if (value
== SVGA_ID_2
|| value
== SVGA_ID_1
|| value
== SVGA_ID_0
) {
913 case SVGA_REG_ENABLE
:
916 s
->vga
.hw_ops
->invalidate(&s
->vga
);
917 if (s
->enable
&& s
->config
) {
918 vga_dirty_log_stop(&s
->vga
);
920 vga_dirty_log_start(&s
->vga
);
925 if (value
<= SVGA_MAX_WIDTH
) {
926 s
->new_width
= value
;
929 printf("%s: Bad width: %i\n", __func__
, value
);
933 case SVGA_REG_HEIGHT
:
934 if (value
<= SVGA_MAX_HEIGHT
) {
935 s
->new_height
= value
;
938 printf("%s: Bad height: %i\n", __func__
, value
);
942 case SVGA_REG_BITS_PER_PIXEL
:
944 printf("%s: Bad bits per pixel: %i bits\n", __func__
, value
);
950 case SVGA_REG_CONFIG_DONE
:
952 s
->fifo
= (uint32_t *) s
->fifo_ptr
;
953 /* Check range and alignment. */
954 if ((CMD(min
) | CMD(max
) | CMD(next_cmd
) | CMD(stop
)) & 3) {
957 if (CMD(min
) < (uint8_t *) s
->cmd
->fifo
- (uint8_t *) s
->fifo
) {
960 if (CMD(max
) > SVGA_FIFO_SIZE
) {
963 if (CMD(max
) < CMD(min
) + 10 * 1024) {
966 vga_dirty_log_stop(&s
->vga
);
973 vmsvga_fifo_run(s
); /* Or should we just wait for update_display? */
976 case SVGA_REG_GUEST_ID
:
979 if (value
>= GUEST_OS_BASE
&& value
< GUEST_OS_BASE
+
980 ARRAY_SIZE(vmsvga_guest_id
)) {
981 printf("%s: guest runs %s.\n", __func__
,
982 vmsvga_guest_id
[value
- GUEST_OS_BASE
]);
987 case SVGA_REG_CURSOR_ID
:
988 s
->cursor
.id
= value
;
991 case SVGA_REG_CURSOR_X
:
995 case SVGA_REG_CURSOR_Y
:
999 case SVGA_REG_CURSOR_ON
:
1000 s
->cursor
.on
|= (value
== SVGA_CURSOR_ON_SHOW
);
1001 s
->cursor
.on
&= (value
!= SVGA_CURSOR_ON_HIDE
);
1002 #ifdef HW_MOUSE_ACCEL
1003 if (value
<= SVGA_CURSOR_ON_SHOW
) {
1004 dpy_mouse_set(s
->vga
.con
, s
->cursor
.x
, s
->cursor
.y
, s
->cursor
.on
);
1009 case SVGA_REG_DEPTH
:
1010 case SVGA_REG_MEM_REGS
:
1011 case SVGA_REG_NUM_DISPLAYS
:
1012 case SVGA_REG_PITCHLOCK
:
1013 case SVGA_PALETTE_BASE
... SVGA_PALETTE_END
:
1017 if (s
->index
>= SVGA_SCRATCH_BASE
&&
1018 s
->index
< SVGA_SCRATCH_BASE
+ s
->scratch_size
) {
1019 s
->scratch
[s
->index
- SVGA_SCRATCH_BASE
] = value
;
1022 printf("%s: Bad register %02x\n", __func__
, s
->index
);
1026 static uint32_t vmsvga_bios_read(void *opaque
, uint32_t address
)
1028 printf("%s: what are we supposed to return?\n", __func__
);
1032 static void vmsvga_bios_write(void *opaque
, uint32_t address
, uint32_t data
)
1034 printf("%s: what are we supposed to do with (%08x)?\n", __func__
, data
);
1037 static inline void vmsvga_check_size(struct vmsvga_state_s
*s
)
1039 DisplaySurface
*surface
= qemu_console_surface(s
->vga
.con
);
1041 if (s
->new_width
!= surface_width(surface
) ||
1042 s
->new_height
!= surface_height(surface
) ||
1043 s
->new_depth
!= surface_bits_per_pixel(surface
)) {
1044 int stride
= (s
->new_depth
* s
->new_width
) / 8;
1045 trace_vmware_setmode(s
->new_width
, s
->new_height
, s
->new_depth
);
1046 surface
= qemu_create_displaysurface_from(s
->new_width
, s
->new_height
,
1047 s
->new_depth
, stride
,
1048 s
->vga
.vram_ptr
, false);
1049 dpy_gfx_replace_surface(s
->vga
.con
, surface
);
1054 static void vmsvga_update_display(void *opaque
)
1056 struct vmsvga_state_s
*s
= opaque
;
1057 DisplaySurface
*surface
;
1061 s
->vga
.hw_ops
->gfx_update(&s
->vga
);
1065 vmsvga_check_size(s
);
1066 surface
= qemu_console_surface(s
->vga
.con
);
1069 vmsvga_update_rect_flush(s
);
1072 * Is it more efficient to look at vram VGA-dirty bits or wait
1073 * for the driver to issue SVGA_CMD_UPDATE?
1075 if (memory_region_is_logging(&s
->vga
.vram
)) {
1076 vga_sync_dirty_bitmap(&s
->vga
);
1077 dirty
= memory_region_get_dirty(&s
->vga
.vram
, 0,
1078 surface_stride(surface
) * surface_height(surface
),
1081 if (s
->invalidated
|| dirty
) {
1083 dpy_gfx_update(s
->vga
.con
, 0, 0,
1084 surface_width(surface
), surface_height(surface
));
1087 memory_region_reset_dirty(&s
->vga
.vram
, 0,
1088 surface_stride(surface
) * surface_height(surface
),
1093 static void vmsvga_reset(DeviceState
*dev
)
1095 struct pci_vmsvga_state_s
*pci
=
1096 DO_UPCAST(struct pci_vmsvga_state_s
, card
.qdev
, dev
);
1097 struct vmsvga_state_s
*s
= &pci
->chip
;
1102 s
->svgaid
= SVGA_ID
;
1104 s
->redraw_fifo_first
= 0;
1105 s
->redraw_fifo_last
= 0;
1108 vga_dirty_log_start(&s
->vga
);
1111 static void vmsvga_invalidate_display(void *opaque
)
1113 struct vmsvga_state_s
*s
= opaque
;
1115 s
->vga
.hw_ops
->invalidate(&s
->vga
);
1122 static void vmsvga_text_update(void *opaque
, console_ch_t
*chardata
)
1124 struct vmsvga_state_s
*s
= opaque
;
1126 if (s
->vga
.hw_ops
->text_update
) {
1127 s
->vga
.hw_ops
->text_update(&s
->vga
, chardata
);
1131 static int vmsvga_post_load(void *opaque
, int version_id
)
1133 struct vmsvga_state_s
*s
= opaque
;
1137 s
->fifo
= (uint32_t *) s
->fifo_ptr
;
1142 static const VMStateDescription vmstate_vmware_vga_internal
= {
1143 .name
= "vmware_vga_internal",
1145 .minimum_version_id
= 0,
1146 .minimum_version_id_old
= 0,
1147 .post_load
= vmsvga_post_load
,
1148 .fields
= (VMStateField
[]) {
1149 VMSTATE_INT32_EQUAL(new_depth
, struct vmsvga_state_s
),
1150 VMSTATE_INT32(enable
, struct vmsvga_state_s
),
1151 VMSTATE_INT32(config
, struct vmsvga_state_s
),
1152 VMSTATE_INT32(cursor
.id
, struct vmsvga_state_s
),
1153 VMSTATE_INT32(cursor
.x
, struct vmsvga_state_s
),
1154 VMSTATE_INT32(cursor
.y
, struct vmsvga_state_s
),
1155 VMSTATE_INT32(cursor
.on
, struct vmsvga_state_s
),
1156 VMSTATE_INT32(index
, struct vmsvga_state_s
),
1157 VMSTATE_VARRAY_INT32(scratch
, struct vmsvga_state_s
,
1158 scratch_size
, 0, vmstate_info_uint32
, uint32_t),
1159 VMSTATE_INT32(new_width
, struct vmsvga_state_s
),
1160 VMSTATE_INT32(new_height
, struct vmsvga_state_s
),
1161 VMSTATE_UINT32(guest
, struct vmsvga_state_s
),
1162 VMSTATE_UINT32(svgaid
, struct vmsvga_state_s
),
1163 VMSTATE_INT32(syncing
, struct vmsvga_state_s
),
1164 VMSTATE_UNUSED(4), /* was fb_size */
1165 VMSTATE_END_OF_LIST()
1169 static const VMStateDescription vmstate_vmware_vga
= {
1170 .name
= "vmware_vga",
1172 .minimum_version_id
= 0,
1173 .minimum_version_id_old
= 0,
1174 .fields
= (VMStateField
[]) {
1175 VMSTATE_PCI_DEVICE(card
, struct pci_vmsvga_state_s
),
1176 VMSTATE_STRUCT(chip
, struct pci_vmsvga_state_s
, 0,
1177 vmstate_vmware_vga_internal
, struct vmsvga_state_s
),
1178 VMSTATE_END_OF_LIST()
1182 static const GraphicHwOps vmsvga_ops
= {
1183 .invalidate
= vmsvga_invalidate_display
,
1184 .gfx_update
= vmsvga_update_display
,
1185 .text_update
= vmsvga_text_update
,
1188 static void vmsvga_init(DeviceState
*dev
, struct vmsvga_state_s
*s
,
1189 MemoryRegion
*address_space
, MemoryRegion
*io
)
1191 s
->scratch_size
= SVGA_SCRATCH_SIZE
;
1192 s
->scratch
= g_malloc(s
->scratch_size
* 4);
1194 s
->vga
.con
= graphic_console_init(dev
, &vmsvga_ops
, s
);
1196 s
->fifo_size
= SVGA_FIFO_SIZE
;
1197 memory_region_init_ram(&s
->fifo_ram
, "vmsvga.fifo", s
->fifo_size
);
1198 vmstate_register_ram_global(&s
->fifo_ram
);
1199 s
->fifo_ptr
= memory_region_get_ram_ptr(&s
->fifo_ram
);
1201 vga_common_init(&s
->vga
);
1202 vga_init(&s
->vga
, address_space
, io
, true);
1203 vmstate_register(NULL
, 0, &vmstate_vga_common
, &s
->vga
);
1207 static uint64_t vmsvga_io_read(void *opaque
, hwaddr addr
, unsigned size
)
1209 struct vmsvga_state_s
*s
= opaque
;
1212 case SVGA_IO_MUL
* SVGA_INDEX_PORT
: return vmsvga_index_read(s
, addr
);
1213 case SVGA_IO_MUL
* SVGA_VALUE_PORT
: return vmsvga_value_read(s
, addr
);
1214 case SVGA_IO_MUL
* SVGA_BIOS_PORT
: return vmsvga_bios_read(s
, addr
);
1215 default: return -1u;
1219 static void vmsvga_io_write(void *opaque
, hwaddr addr
,
1220 uint64_t data
, unsigned size
)
1222 struct vmsvga_state_s
*s
= opaque
;
1225 case SVGA_IO_MUL
* SVGA_INDEX_PORT
:
1226 vmsvga_index_write(s
, addr
, data
);
1228 case SVGA_IO_MUL
* SVGA_VALUE_PORT
:
1229 vmsvga_value_write(s
, addr
, data
);
1231 case SVGA_IO_MUL
* SVGA_BIOS_PORT
:
1232 vmsvga_bios_write(s
, addr
, data
);
1237 static const MemoryRegionOps vmsvga_io_ops
= {
1238 .read
= vmsvga_io_read
,
1239 .write
= vmsvga_io_write
,
1240 .endianness
= DEVICE_LITTLE_ENDIAN
,
1242 .min_access_size
= 4,
1243 .max_access_size
= 4,
1247 static int pci_vmsvga_initfn(PCIDevice
*dev
)
1249 struct pci_vmsvga_state_s
*s
=
1250 DO_UPCAST(struct pci_vmsvga_state_s
, card
, dev
);
1252 s
->card
.config
[PCI_CACHE_LINE_SIZE
] = 0x08; /* Cache line size */
1253 s
->card
.config
[PCI_LATENCY_TIMER
] = 0x40; /* Latency timer */
1254 s
->card
.config
[PCI_INTERRUPT_LINE
] = 0xff; /* End */
1256 memory_region_init_io(&s
->io_bar
, &vmsvga_io_ops
, &s
->chip
,
1258 memory_region_set_flush_coalesced(&s
->io_bar
);
1259 pci_register_bar(&s
->card
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_bar
);
1261 vmsvga_init(DEVICE(dev
), &s
->chip
,
1262 pci_address_space(dev
), pci_address_space_io(dev
));
1264 pci_register_bar(&s
->card
, 1, PCI_BASE_ADDRESS_MEM_PREFETCH
,
1266 pci_register_bar(&s
->card
, 2, PCI_BASE_ADDRESS_MEM_PREFETCH
,
1269 if (!dev
->rom_bar
) {
1270 /* compatibility with pc-0.13 and older */
1271 vga_init_vbe(&s
->chip
.vga
, pci_address_space(dev
));
1277 static Property vga_vmware_properties
[] = {
1278 DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s
,
1279 chip
.vga
.vram_size_mb
, 16),
1280 DEFINE_PROP_END_OF_LIST(),
1283 static void vmsvga_class_init(ObjectClass
*klass
, void *data
)
1285 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1286 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1289 k
->init
= pci_vmsvga_initfn
;
1290 k
->romfile
= "vgabios-vmware.bin";
1291 k
->vendor_id
= PCI_VENDOR_ID_VMWARE
;
1292 k
->device_id
= SVGA_PCI_DEVICE_ID
;
1293 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
1294 k
->subsystem_vendor_id
= PCI_VENDOR_ID_VMWARE
;
1295 k
->subsystem_id
= SVGA_PCI_DEVICE_ID
;
1296 dc
->reset
= vmsvga_reset
;
1297 dc
->vmsd
= &vmstate_vmware_vga
;
1298 dc
->props
= vga_vmware_properties
;
1301 static const TypeInfo vmsvga_info
= {
1302 .name
= "vmware-svga",
1303 .parent
= TYPE_PCI_DEVICE
,
1304 .instance_size
= sizeof(struct pci_vmsvga_state_s
),
1305 .class_init
= vmsvga_class_init
,
1308 static void vmsvga_register_types(void)
1310 type_register_static(&vmsvga_info
);
1313 type_init(vmsvga_register_types
)