blob: 6599cf078d22f794e9eb54450f602ce022136fe8 [file] [log] [blame]
thsd34cab92007-04-02 01:10:46 +00001/*
2 * QEMU VMware-SVGA "chipset".
3 *
4 * Copyright (c) 2007 Andrzej Zaborowski <balrog@zabor.org>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
Peter Maydell47df5152016-01-26 18:17:13 +000024#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010026#include "hw/hw.h"
27#include "hw/loader.h"
Stefan Weilac860482013-11-10 14:20:16 +010028#include "trace.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010029#include "ui/console.h"
Peter Lieven2f487a3d2014-03-17 18:38:58 +010030#include "ui/vnc.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010031#include "hw/pci/pci.h"
thsd34cab92007-04-02 01:10:46 +000032
Jan Kiszkaca0508d2011-08-22 19:12:09 +020033#undef VERBOSE
thsd34cab92007-04-02 01:10:46 +000034#define HW_RECT_ACCEL
35#define HW_FILL_ACCEL
36#define HW_MOUSE_ACCEL
37
Paolo Bonzini47b43a12013-03-18 17:36:02 +010038#include "vga_int.h"
BALATON Zoltan5b9575c2012-11-03 12:47:08 +010039
40/* See http://vmware-svga.sf.net/ for some documentation on VMWare SVGA */
thsd34cab92007-04-02 01:10:46 +000041
42struct vmsvga_state_s {
Avi Kivity4e12cd92009-05-03 22:25:16 +030043 VGACommonState vga;
thsd34cab92007-04-02 01:10:46 +000044
thsd34cab92007-04-02 01:10:46 +000045 int invalidated;
thsd34cab92007-04-02 01:10:46 +000046 int enable;
47 int config;
48 struct {
49 int id;
50 int x;
51 int y;
52 int on;
53 } cursor;
54
thsd34cab92007-04-02 01:10:46 +000055 int index;
56 int scratch_size;
57 uint32_t *scratch;
58 int new_width;
59 int new_height;
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +010060 int new_depth;
thsd34cab92007-04-02 01:10:46 +000061 uint32_t guest;
62 uint32_t svgaid;
thsd34cab92007-04-02 01:10:46 +000063 int syncing;
thsd34cab92007-04-02 01:10:46 +000064
Avi Kivityb1950432011-08-08 16:08:57 +030065 MemoryRegion fifo_ram;
Dave Airlief351d052009-12-18 08:08:06 +100066 uint8_t *fifo_ptr;
67 unsigned int fifo_size;
Dave Airlief351d052009-12-18 08:08:06 +100068
Gerd Hoffmann7e486f72016-05-30 09:09:20 +020069 uint32_t *fifo;
70 uint32_t fifo_min;
71 uint32_t fifo_max;
72 uint32_t fifo_next;
73 uint32_t fifo_stop;
thsd34cab92007-04-02 01:10:46 +000074
BALATON Zoltan0d793792012-11-03 12:47:08 +010075#define REDRAW_FIFO_LEN 512
thsd34cab92007-04-02 01:10:46 +000076 struct vmsvga_rect_s {
77 int x, y, w, h;
78 } redraw_fifo[REDRAW_FIFO_LEN];
79 int redraw_fifo_first, redraw_fifo_last;
80};
81
Peter Crosthwaite39d45982013-06-24 16:58:45 +100082#define TYPE_VMWARE_SVGA "vmware-svga"
83
84#define VMWARE_SVGA(obj) \
85 OBJECT_CHECK(struct pci_vmsvga_state_s, (obj), TYPE_VMWARE_SVGA)
86
thsd34cab92007-04-02 01:10:46 +000087struct pci_vmsvga_state_s {
Andreas Färberaf21c742013-06-30 15:01:36 +020088 /*< private >*/
89 PCIDevice parent_obj;
90 /*< public >*/
91
thsd34cab92007-04-02 01:10:46 +000092 struct vmsvga_state_s chip;
Avi Kivityb1950432011-08-08 16:08:57 +030093 MemoryRegion io_bar;
thsd34cab92007-04-02 01:10:46 +000094};
95
BALATON Zoltan0d793792012-11-03 12:47:08 +010096#define SVGA_MAGIC 0x900000UL
97#define SVGA_MAKE_ID(ver) (SVGA_MAGIC << 8 | (ver))
98#define SVGA_ID_0 SVGA_MAKE_ID(0)
99#define SVGA_ID_1 SVGA_MAKE_ID(1)
100#define SVGA_ID_2 SVGA_MAKE_ID(2)
thsd34cab92007-04-02 01:10:46 +0000101
BALATON Zoltan0d793792012-11-03 12:47:08 +0100102#define SVGA_LEGACY_BASE_PORT 0x4560
103#define SVGA_INDEX_PORT 0x0
104#define SVGA_VALUE_PORT 0x1
105#define SVGA_BIOS_PORT 0x2
thsd34cab92007-04-02 01:10:46 +0000106
107#define SVGA_VERSION_2
108
109#ifdef SVGA_VERSION_2
BALATON Zoltan0d793792012-11-03 12:47:08 +0100110# define SVGA_ID SVGA_ID_2
111# define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
112# define SVGA_IO_MUL 1
113# define SVGA_FIFO_SIZE 0x10000
114# define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA2
thsd34cab92007-04-02 01:10:46 +0000115#else
BALATON Zoltan0d793792012-11-03 12:47:08 +0100116# define SVGA_ID SVGA_ID_1
117# define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
118# define SVGA_IO_MUL 4
119# define SVGA_FIFO_SIZE 0x10000
120# define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA
thsd34cab92007-04-02 01:10:46 +0000121#endif
122
123enum {
124 /* ID 0, 1 and 2 registers */
125 SVGA_REG_ID = 0,
126 SVGA_REG_ENABLE = 1,
127 SVGA_REG_WIDTH = 2,
128 SVGA_REG_HEIGHT = 3,
129 SVGA_REG_MAX_WIDTH = 4,
130 SVGA_REG_MAX_HEIGHT = 5,
131 SVGA_REG_DEPTH = 6,
BALATON Zoltan0d793792012-11-03 12:47:08 +0100132 SVGA_REG_BITS_PER_PIXEL = 7, /* Current bpp in the guest */
thsd34cab92007-04-02 01:10:46 +0000133 SVGA_REG_PSEUDOCOLOR = 8,
134 SVGA_REG_RED_MASK = 9,
135 SVGA_REG_GREEN_MASK = 10,
136 SVGA_REG_BLUE_MASK = 11,
137 SVGA_REG_BYTES_PER_LINE = 12,
138 SVGA_REG_FB_START = 13,
139 SVGA_REG_FB_OFFSET = 14,
140 SVGA_REG_VRAM_SIZE = 15,
141 SVGA_REG_FB_SIZE = 16,
142
143 /* ID 1 and 2 registers */
144 SVGA_REG_CAPABILITIES = 17,
BALATON Zoltan0d793792012-11-03 12:47:08 +0100145 SVGA_REG_MEM_START = 18, /* Memory for command FIFO */
thsd34cab92007-04-02 01:10:46 +0000146 SVGA_REG_MEM_SIZE = 19,
BALATON Zoltan0d793792012-11-03 12:47:08 +0100147 SVGA_REG_CONFIG_DONE = 20, /* Set when memory area configured */
148 SVGA_REG_SYNC = 21, /* Write to force synchronization */
149 SVGA_REG_BUSY = 22, /* Read to check if sync is done */
150 SVGA_REG_GUEST_ID = 23, /* Set guest OS identifier */
151 SVGA_REG_CURSOR_ID = 24, /* ID of cursor */
152 SVGA_REG_CURSOR_X = 25, /* Set cursor X position */
153 SVGA_REG_CURSOR_Y = 26, /* Set cursor Y position */
154 SVGA_REG_CURSOR_ON = 27, /* Turn cursor on/off */
155 SVGA_REG_HOST_BITS_PER_PIXEL = 28, /* Current bpp in the host */
156 SVGA_REG_SCRATCH_SIZE = 29, /* Number of scratch registers */
157 SVGA_REG_MEM_REGS = 30, /* Number of FIFO registers */
158 SVGA_REG_NUM_DISPLAYS = 31, /* Number of guest displays */
159 SVGA_REG_PITCHLOCK = 32, /* Fixed pitch for all modes */
thsd34cab92007-04-02 01:10:46 +0000160
BALATON Zoltan0d793792012-11-03 12:47:08 +0100161 SVGA_PALETTE_BASE = 1024, /* Base of SVGA color map */
thsd34cab92007-04-02 01:10:46 +0000162 SVGA_PALETTE_END = SVGA_PALETTE_BASE + 767,
163 SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
164};
165
BALATON Zoltan0d793792012-11-03 12:47:08 +0100166#define SVGA_CAP_NONE 0
167#define SVGA_CAP_RECT_FILL (1 << 0)
168#define SVGA_CAP_RECT_COPY (1 << 1)
169#define SVGA_CAP_RECT_PAT_FILL (1 << 2)
170#define SVGA_CAP_LEGACY_OFFSCREEN (1 << 3)
171#define SVGA_CAP_RASTER_OP (1 << 4)
172#define SVGA_CAP_CURSOR (1 << 5)
173#define SVGA_CAP_CURSOR_BYPASS (1 << 6)
174#define SVGA_CAP_CURSOR_BYPASS_2 (1 << 7)
175#define SVGA_CAP_8BIT_EMULATION (1 << 8)
176#define SVGA_CAP_ALPHA_CURSOR (1 << 9)
177#define SVGA_CAP_GLYPH (1 << 10)
178#define SVGA_CAP_GLYPH_CLIPPING (1 << 11)
179#define SVGA_CAP_OFFSCREEN_1 (1 << 12)
180#define SVGA_CAP_ALPHA_BLEND (1 << 13)
181#define SVGA_CAP_3D (1 << 14)
182#define SVGA_CAP_EXTENDED_FIFO (1 << 15)
183#define SVGA_CAP_MULTIMON (1 << 16)
184#define SVGA_CAP_PITCHLOCK (1 << 17)
thsd34cab92007-04-02 01:10:46 +0000185
186/*
187 * FIFO offsets (seen as an array of 32-bit words)
188 */
189enum {
190 /*
191 * The original defined FIFO offsets
192 */
193 SVGA_FIFO_MIN = 0,
BALATON Zoltan0d793792012-11-03 12:47:08 +0100194 SVGA_FIFO_MAX, /* The distance from MIN to MAX must be at least 10K */
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200195 SVGA_FIFO_NEXT,
thsd34cab92007-04-02 01:10:46 +0000196 SVGA_FIFO_STOP,
197
198 /*
199 * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
200 */
201 SVGA_FIFO_CAPABILITIES = 4,
202 SVGA_FIFO_FLAGS,
203 SVGA_FIFO_FENCE,
204 SVGA_FIFO_3D_HWVERSION,
205 SVGA_FIFO_PITCHLOCK,
206};
207
BALATON Zoltan0d793792012-11-03 12:47:08 +0100208#define SVGA_FIFO_CAP_NONE 0
209#define SVGA_FIFO_CAP_FENCE (1 << 0)
210#define SVGA_FIFO_CAP_ACCELFRONT (1 << 1)
211#define SVGA_FIFO_CAP_PITCHLOCK (1 << 2)
thsd34cab92007-04-02 01:10:46 +0000212
BALATON Zoltan0d793792012-11-03 12:47:08 +0100213#define SVGA_FIFO_FLAG_NONE 0
214#define SVGA_FIFO_FLAG_ACCELFRONT (1 << 0)
thsd34cab92007-04-02 01:10:46 +0000215
216/* These values can probably be changed arbitrarily. */
BALATON Zoltan0d793792012-11-03 12:47:08 +0100217#define SVGA_SCRATCH_SIZE 0x8000
Peter Lieven2f487a3d2014-03-17 18:38:58 +0100218#define SVGA_MAX_WIDTH ROUND_UP(2360, VNC_DIRTY_PIXELS_PER_BIT)
BALATON Zoltan0d793792012-11-03 12:47:08 +0100219#define SVGA_MAX_HEIGHT 1770
thsd34cab92007-04-02 01:10:46 +0000220
221#ifdef VERBOSE
BALATON Zoltan0d793792012-11-03 12:47:08 +0100222# define GUEST_OS_BASE 0x5001
thsd34cab92007-04-02 01:10:46 +0000223static const char *vmsvga_guest_id[] = {
balrogf707cfb2007-05-13 13:26:49 +0000224 [0x00] = "Dos",
225 [0x01] = "Windows 3.1",
226 [0x02] = "Windows 95",
227 [0x03] = "Windows 98",
228 [0x04] = "Windows ME",
229 [0x05] = "Windows NT",
230 [0x06] = "Windows 2000",
231 [0x07] = "Linux",
232 [0x08] = "OS/2",
blueswir1511d2b12009-03-07 15:32:56 +0000233 [0x09] = "an unknown OS",
balrogf707cfb2007-05-13 13:26:49 +0000234 [0x0a] = "BSD",
235 [0x0b] = "Whistler",
blueswir1511d2b12009-03-07 15:32:56 +0000236 [0x0c] = "an unknown OS",
237 [0x0d] = "an unknown OS",
238 [0x0e] = "an unknown OS",
239 [0x0f] = "an unknown OS",
240 [0x10] = "an unknown OS",
241 [0x11] = "an unknown OS",
242 [0x12] = "an unknown OS",
243 [0x13] = "an unknown OS",
244 [0x14] = "an unknown OS",
balrogf707cfb2007-05-13 13:26:49 +0000245 [0x15] = "Windows 2003",
thsd34cab92007-04-02 01:10:46 +0000246};
247#endif
248
249enum {
250 SVGA_CMD_INVALID_CMD = 0,
251 SVGA_CMD_UPDATE = 1,
252 SVGA_CMD_RECT_FILL = 2,
253 SVGA_CMD_RECT_COPY = 3,
254 SVGA_CMD_DEFINE_BITMAP = 4,
255 SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
256 SVGA_CMD_DEFINE_PIXMAP = 6,
257 SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
258 SVGA_CMD_RECT_BITMAP_FILL = 8,
259 SVGA_CMD_RECT_PIXMAP_FILL = 9,
260 SVGA_CMD_RECT_BITMAP_COPY = 10,
261 SVGA_CMD_RECT_PIXMAP_COPY = 11,
262 SVGA_CMD_FREE_OBJECT = 12,
263 SVGA_CMD_RECT_ROP_FILL = 13,
264 SVGA_CMD_RECT_ROP_COPY = 14,
265 SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
266 SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
267 SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
268 SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
269 SVGA_CMD_DEFINE_CURSOR = 19,
270 SVGA_CMD_DISPLAY_CURSOR = 20,
271 SVGA_CMD_MOVE_CURSOR = 21,
272 SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
273 SVGA_CMD_DRAW_GLYPH = 23,
274 SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
275 SVGA_CMD_UPDATE_VERBOSE = 25,
276 SVGA_CMD_SURFACE_FILL = 26,
277 SVGA_CMD_SURFACE_COPY = 27,
278 SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
279 SVGA_CMD_FRONT_ROP_FILL = 29,
280 SVGA_CMD_FENCE = 30,
281};
282
283/* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
284enum {
285 SVGA_CURSOR_ON_HIDE = 0,
286 SVGA_CURSOR_ON_SHOW = 1,
287 SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
288 SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
289};
290
Gerd Hoffmann07258902014-10-06 11:51:54 +0200291static inline bool vmsvga_verify_rect(DisplaySurface *surface,
292 const char *name,
293 int x, int y, int w, int h)
294{
295 if (x < 0) {
296 fprintf(stderr, "%s: x was < 0 (%d)\n", name, x);
297 return false;
298 }
299 if (x > SVGA_MAX_WIDTH) {
300 fprintf(stderr, "%s: x was > %d (%d)\n", name, SVGA_MAX_WIDTH, x);
301 return false;
302 }
303 if (w < 0) {
304 fprintf(stderr, "%s: w was < 0 (%d)\n", name, w);
305 return false;
306 }
307 if (w > SVGA_MAX_WIDTH) {
308 fprintf(stderr, "%s: w was > %d (%d)\n", name, SVGA_MAX_WIDTH, w);
309 return false;
310 }
311 if (x + w > surface_width(surface)) {
312 fprintf(stderr, "%s: width was > %d (x: %d, w: %d)\n",
313 name, surface_width(surface), x, w);
314 return false;
315 }
316
317 if (y < 0) {
318 fprintf(stderr, "%s: y was < 0 (%d)\n", name, y);
319 return false;
320 }
321 if (y > SVGA_MAX_HEIGHT) {
322 fprintf(stderr, "%s: y was > %d (%d)\n", name, SVGA_MAX_HEIGHT, y);
323 return false;
324 }
325 if (h < 0) {
326 fprintf(stderr, "%s: h was < 0 (%d)\n", name, h);
327 return false;
328 }
329 if (h > SVGA_MAX_HEIGHT) {
330 fprintf(stderr, "%s: h was > %d (%d)\n", name, SVGA_MAX_HEIGHT, h);
331 return false;
332 }
333 if (y + h > surface_height(surface)) {
334 fprintf(stderr, "%s: update height > %d (y: %d, h: %d)\n",
335 name, surface_height(surface), y, h);
336 return false;
337 }
338
339 return true;
340}
341
thsd34cab92007-04-02 01:10:46 +0000342static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
Gerd Hoffmann07258902014-10-06 11:51:54 +0200343 int x, int y, int w, int h)
thsd34cab92007-04-02 01:10:46 +0000344{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100345 DisplaySurface *surface = qemu_console_surface(s->vga.con);
balroga8fbaf92008-03-06 20:43:34 +0000346 int line;
347 int bypl;
348 int width;
349 int start;
350 uint8_t *src;
351 uint8_t *dst;
352
Gerd Hoffmann1735fe12014-10-06 11:58:22 +0200353 if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
354 /* go for a fullscreen update as fallback */
Michael Tokarev8cb6bfb2013-01-25 21:23:24 +0400355 x = 0;
Michael Tokarev8cb6bfb2013-01-25 21:23:24 +0400356 y = 0;
Gerd Hoffmann1735fe12014-10-06 11:58:22 +0200357 w = surface_width(surface);
358 h = surface_height(surface);
balroga8fbaf92008-03-06 20:43:34 +0000359 }
360
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100361 bypl = surface_stride(surface);
362 width = surface_bytes_per_pixel(surface) * w;
363 start = surface_bytes_per_pixel(surface) * x + bypl * y;
Avi Kivity4e12cd92009-05-03 22:25:16 +0300364 src = s->vga.vram_ptr + start;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100365 dst = surface_data(surface) + start;
thsd34cab92007-04-02 01:10:46 +0000366
BALATON Zoltan0d793792012-11-03 12:47:08 +0100367 for (line = h; line > 0; line--, src += bypl, dst += bypl) {
thsd34cab92007-04-02 01:10:46 +0000368 memcpy(dst, src, width);
BALATON Zoltan0d793792012-11-03 12:47:08 +0100369 }
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100370 dpy_gfx_update(s->vga.con, x, y, w, h);
thsd34cab92007-04-02 01:10:46 +0000371}
372
thsd34cab92007-04-02 01:10:46 +0000373static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
374 int x, int y, int w, int h)
375{
BALATON Zoltan0d793792012-11-03 12:47:08 +0100376 struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last++];
377
thsd34cab92007-04-02 01:10:46 +0000378 s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
379 rect->x = x;
380 rect->y = y;
381 rect->w = w;
382 rect->h = h;
383}
thsd34cab92007-04-02 01:10:46 +0000384
385static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
386{
387 struct vmsvga_rect_s *rect;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100388
thsd34cab92007-04-02 01:10:46 +0000389 if (s->invalidated) {
390 s->redraw_fifo_first = s->redraw_fifo_last;
391 return;
392 }
393 /* Overlapping region updates can be optimised out here - if someone
394 * knows a smart algorithm to do that, please share. */
395 while (s->redraw_fifo_first != s->redraw_fifo_last) {
BALATON Zoltan0d793792012-11-03 12:47:08 +0100396 rect = &s->redraw_fifo[s->redraw_fifo_first++];
thsd34cab92007-04-02 01:10:46 +0000397 s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
398 vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
399 }
400}
401
402#ifdef HW_RECT_ACCEL
Gerd Hoffmann61b41b42014-10-06 11:58:51 +0200403static inline int vmsvga_copy_rect(struct vmsvga_state_s *s,
thsd34cab92007-04-02 01:10:46 +0000404 int x0, int y0, int x1, int y1, int w, int h)
405{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100406 DisplaySurface *surface = qemu_console_surface(s->vga.con);
Avi Kivity4e12cd92009-05-03 22:25:16 +0300407 uint8_t *vram = s->vga.vram_ptr;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100408 int bypl = surface_stride(surface);
409 int bypp = surface_bytes_per_pixel(surface);
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100410 int width = bypp * w;
thsd34cab92007-04-02 01:10:46 +0000411 int line = h;
412 uint8_t *ptr[2];
413
Gerd Hoffmann61b41b42014-10-06 11:58:51 +0200414 if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/src", x0, y0, w, h)) {
415 return -1;
416 }
417 if (!vmsvga_verify_rect(surface, "vmsvga_copy_rect/dst", x1, y1, w, h)) {
418 return -1;
419 }
420
Jan Kiszka8d121d42011-08-22 19:12:10 +0200421 if (y1 > y0) {
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100422 ptr[0] = vram + bypp * x0 + bypl * (y0 + h - 1);
423 ptr[1] = vram + bypp * x1 + bypl * (y1 + h - 1);
Jan Kiszka8d121d42011-08-22 19:12:10 +0200424 for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
425 memmove(ptr[1], ptr[0], width);
426 }
427 } else {
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100428 ptr[0] = vram + bypp * x0 + bypl * y0;
429 ptr[1] = vram + bypp * x1 + bypl * y1;
Jan Kiszka8d121d42011-08-22 19:12:10 +0200430 for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
431 memmove(ptr[1], ptr[0], width);
thsd34cab92007-04-02 01:10:46 +0000432 }
433 }
434
435 vmsvga_update_rect_delayed(s, x1, y1, w, h);
Gerd Hoffmann61b41b42014-10-06 11:58:51 +0200436 return 0;
thsd34cab92007-04-02 01:10:46 +0000437}
438#endif
439
440#ifdef HW_FILL_ACCEL
Gerd Hoffmannbd9ccd82014-10-06 11:59:51 +0200441static inline int vmsvga_fill_rect(struct vmsvga_state_s *s,
thsd34cab92007-04-02 01:10:46 +0000442 uint32_t c, int x, int y, int w, int h)
443{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100444 DisplaySurface *surface = qemu_console_surface(s->vga.con);
445 int bypl = surface_stride(surface);
446 int width = surface_bytes_per_pixel(surface) * w;
thsd34cab92007-04-02 01:10:46 +0000447 int line = h;
448 int column;
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100449 uint8_t *fst;
thsd34cab92007-04-02 01:10:46 +0000450 uint8_t *dst;
451 uint8_t *src;
452 uint8_t col[4];
453
Gerd Hoffmannbd9ccd82014-10-06 11:59:51 +0200454 if (!vmsvga_verify_rect(surface, __func__, x, y, w, h)) {
455 return -1;
456 }
457
Jan Kiszka8d121d42011-08-22 19:12:10 +0200458 col[0] = c;
459 col[1] = c >> 8;
460 col[2] = c >> 16;
461 col[3] = c >> 24;
thsd34cab92007-04-02 01:10:46 +0000462
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100463 fst = s->vga.vram_ptr + surface_bytes_per_pixel(surface) * x + bypl * y;
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100464
Jan Kiszka8d121d42011-08-22 19:12:10 +0200465 if (line--) {
466 dst = fst;
467 src = col;
468 for (column = width; column > 0; column--) {
469 *(dst++) = *(src++);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100470 if (src - col == surface_bytes_per_pixel(surface)) {
Jan Kiszka8d121d42011-08-22 19:12:10 +0200471 src = col;
thsd34cab92007-04-02 01:10:46 +0000472 }
Jan Kiszka8d121d42011-08-22 19:12:10 +0200473 }
474 dst = fst;
475 for (; line > 0; line--) {
476 dst += bypl;
477 memcpy(dst, fst, width);
thsd34cab92007-04-02 01:10:46 +0000478 }
479 }
480
481 vmsvga_update_rect_delayed(s, x, y, w, h);
Gerd Hoffmannbd9ccd82014-10-06 11:59:51 +0200482 return 0;
thsd34cab92007-04-02 01:10:46 +0000483}
484#endif
485
486struct vmsvga_cursor_definition_s {
Gerd Hoffmann5829b092015-09-29 09:58:05 +0200487 uint32_t width;
488 uint32_t height;
thsd34cab92007-04-02 01:10:46 +0000489 int id;
Gerd Hoffmann5829b092015-09-29 09:58:05 +0200490 uint32_t bpp;
thsd34cab92007-04-02 01:10:46 +0000491 int hot_x;
492 int hot_y;
493 uint32_t mask[1024];
Dave Airlie8095cb32009-12-18 08:08:11 +1000494 uint32_t image[4096];
thsd34cab92007-04-02 01:10:46 +0000495};
496
BALATON Zoltan0d793792012-11-03 12:47:08 +0100497#define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
498#define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
thsd34cab92007-04-02 01:10:46 +0000499
500#ifdef HW_MOUSE_ACCEL
501static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
502 struct vmsvga_cursor_definition_s *c)
503{
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200504 QEMUCursor *qc;
505 int i, pixels;
506
507 qc = cursor_alloc(c->width, c->height);
508 qc->hot_x = c->hot_x;
509 qc->hot_y = c->hot_y;
510 switch (c->bpp) {
511 case 1:
BALATON Zoltan0d793792012-11-03 12:47:08 +0100512 cursor_set_mono(qc, 0xffffff, 0x000000, (void *)c->image,
513 1, (void *)c->mask);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200514#ifdef DEBUG
515 cursor_print_ascii_art(qc, "vmware/mono");
516#endif
517 break;
518 case 32:
519 /* fill alpha channel from mask, set color to zero */
BALATON Zoltan0d793792012-11-03 12:47:08 +0100520 cursor_set_mono(qc, 0x000000, 0x000000, (void *)c->mask,
521 1, (void *)c->mask);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200522 /* add in rgb values */
523 pixels = c->width * c->height;
524 for (i = 0; i < pixels; i++) {
525 qc->data[i] |= c->image[i] & 0xffffff;
526 }
527#ifdef DEBUG
528 cursor_print_ascii_art(qc, "vmware/32bit");
529#endif
530 break;
531 default:
532 fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
BALATON Zoltan0d793792012-11-03 12:47:08 +0100533 __func__, c->bpp);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200534 cursor_put(qc);
535 qc = cursor_builtin_left_ptr();
536 }
thsd34cab92007-04-02 01:10:46 +0000537
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100538 dpy_cursor_define(s->vga.con, qc);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200539 cursor_put(qc);
thsd34cab92007-04-02 01:10:46 +0000540}
541#endif
542
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200543static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
thsd34cab92007-04-02 01:10:46 +0000544{
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200545 int num;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100546
547 if (!s->config || !s->enable) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200548 return 0;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100549 }
Gerd Hoffmann52136022016-05-30 09:09:18 +0200550
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200551 s->fifo_min = le32_to_cpu(s->fifo[SVGA_FIFO_MIN]);
552 s->fifo_max = le32_to_cpu(s->fifo[SVGA_FIFO_MAX]);
553 s->fifo_next = le32_to_cpu(s->fifo[SVGA_FIFO_NEXT]);
554 s->fifo_stop = le32_to_cpu(s->fifo[SVGA_FIFO_STOP]);
555
Gerd Hoffmann52136022016-05-30 09:09:18 +0200556 /* Check range and alignment. */
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200557 if ((s->fifo_min | s->fifo_max | s->fifo_next | s->fifo_stop) & 3) {
Gerd Hoffmann52136022016-05-30 09:09:18 +0200558 return 0;
559 }
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200560 if (s->fifo_min < sizeof(uint32_t) * 4) {
Gerd Hoffmann52136022016-05-30 09:09:18 +0200561 return 0;
562 }
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200563 if (s->fifo_max > SVGA_FIFO_SIZE ||
564 s->fifo_min >= SVGA_FIFO_SIZE ||
565 s->fifo_stop >= SVGA_FIFO_SIZE ||
566 s->fifo_next >= SVGA_FIFO_SIZE) {
Gerd Hoffmann52136022016-05-30 09:09:18 +0200567 return 0;
568 }
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200569 if (s->fifo_max < s->fifo_min + 10 * 1024) {
Gerd Hoffmann52136022016-05-30 09:09:18 +0200570 return 0;
571 }
572
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200573 num = s->fifo_next - s->fifo_stop;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100574 if (num < 0) {
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200575 num += s->fifo_max - s->fifo_min;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100576 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200577 return num >> 2;
thsd34cab92007-04-02 01:10:46 +0000578}
579
balrogff9cf2c2008-07-16 04:45:12 +0000580static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
thsd34cab92007-04-02 01:10:46 +0000581{
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200582 uint32_t cmd = s->fifo[s->fifo_stop >> 2];
BALATON Zoltan0d793792012-11-03 12:47:08 +0100583
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200584 s->fifo_stop += 4;
585 if (s->fifo_stop >= s->fifo_max) {
586 s->fifo_stop = s->fifo_min;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100587 }
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200588 s->fifo[SVGA_FIFO_STOP] = cpu_to_le32(s->fifo_stop);
thsd34cab92007-04-02 01:10:46 +0000589 return cmd;
590}
591
balrogff9cf2c2008-07-16 04:45:12 +0000592static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
593{
594 return le32_to_cpu(vmsvga_fifo_read_raw(s));
595}
596
thsd34cab92007-04-02 01:10:46 +0000597static void vmsvga_fifo_run(struct vmsvga_state_s *s)
598{
599 uint32_t cmd, colour;
Gerd Hoffmann4e68a0e2016-05-30 09:09:21 +0200600 int args, len, maxloop = 1024;
thsd34cab92007-04-02 01:10:46 +0000601 int x, y, dx, dy, width, height;
602 struct vmsvga_cursor_definition_s cursor;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200603 uint32_t cmd_start;
604
605 len = vmsvga_fifo_length(s);
Gerd Hoffmann4e68a0e2016-05-30 09:09:21 +0200606 while (len > 0 && --maxloop > 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200607 /* May need to go back to the start of the command if incomplete */
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200608 cmd_start = s->fifo_stop;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200609
thsd34cab92007-04-02 01:10:46 +0000610 switch (cmd = vmsvga_fifo_read(s)) {
611 case SVGA_CMD_UPDATE:
612 case SVGA_CMD_UPDATE_VERBOSE:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200613 len -= 5;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100614 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200615 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100616 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200617
thsd34cab92007-04-02 01:10:46 +0000618 x = vmsvga_fifo_read(s);
619 y = vmsvga_fifo_read(s);
620 width = vmsvga_fifo_read(s);
621 height = vmsvga_fifo_read(s);
622 vmsvga_update_rect_delayed(s, x, y, width, height);
623 break;
624
625 case SVGA_CMD_RECT_FILL:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200626 len -= 6;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100627 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200628 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100629 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200630
thsd34cab92007-04-02 01:10:46 +0000631 colour = vmsvga_fifo_read(s);
632 x = vmsvga_fifo_read(s);
633 y = vmsvga_fifo_read(s);
634 width = vmsvga_fifo_read(s);
635 height = vmsvga_fifo_read(s);
636#ifdef HW_FILL_ACCEL
Gerd Hoffmannbd9ccd82014-10-06 11:59:51 +0200637 if (vmsvga_fill_rect(s, colour, x, y, width, height) == 0) {
638 break;
639 }
640#endif
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200641 args = 0;
thsd34cab92007-04-02 01:10:46 +0000642 goto badcmd;
thsd34cab92007-04-02 01:10:46 +0000643
644 case SVGA_CMD_RECT_COPY:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200645 len -= 7;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100646 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200647 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100648 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200649
thsd34cab92007-04-02 01:10:46 +0000650 x = vmsvga_fifo_read(s);
651 y = vmsvga_fifo_read(s);
652 dx = vmsvga_fifo_read(s);
653 dy = vmsvga_fifo_read(s);
654 width = vmsvga_fifo_read(s);
655 height = vmsvga_fifo_read(s);
656#ifdef HW_RECT_ACCEL
Gerd Hoffmann61b41b42014-10-06 11:58:51 +0200657 if (vmsvga_copy_rect(s, x, y, dx, dy, width, height) == 0) {
658 break;
659 }
660#endif
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200661 args = 0;
thsd34cab92007-04-02 01:10:46 +0000662 goto badcmd;
thsd34cab92007-04-02 01:10:46 +0000663
664 case SVGA_CMD_DEFINE_CURSOR:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200665 len -= 8;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100666 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200667 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100668 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200669
thsd34cab92007-04-02 01:10:46 +0000670 cursor.id = vmsvga_fifo_read(s);
671 cursor.hot_x = vmsvga_fifo_read(s);
672 cursor.hot_y = vmsvga_fifo_read(s);
673 cursor.width = x = vmsvga_fifo_read(s);
674 cursor.height = y = vmsvga_fifo_read(s);
675 vmsvga_fifo_read(s);
676 cursor.bpp = vmsvga_fifo_read(s);
Roland Dreierf2d928d2010-01-05 20:43:34 -0800677
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200678 args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
Prasad J Pandit167d97a2016-09-08 18:15:54 +0530679 if (cursor.width > 256
680 || cursor.height > 256
681 || cursor.bpp > 32
682 || SVGA_BITMAP_SIZE(x, y)
683 > sizeof(cursor.mask) / sizeof(cursor.mask[0])
684 || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
685 > sizeof(cursor.image) / sizeof(cursor.image[0])) {
Andrzej Zaborowski9f810be2010-09-10 02:30:04 +0200686 goto badcmd;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100687 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200688
689 len -= args;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100690 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200691 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100692 }
Roland Dreierf2d928d2010-01-05 20:43:34 -0800693
BALATON Zoltan0d793792012-11-03 12:47:08 +0100694 for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args++) {
balrogff9cf2c2008-07-16 04:45:12 +0000695 cursor.mask[args] = vmsvga_fifo_read_raw(s);
BALATON Zoltan0d793792012-11-03 12:47:08 +0100696 }
697 for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args++) {
balrogff9cf2c2008-07-16 04:45:12 +0000698 cursor.image[args] = vmsvga_fifo_read_raw(s);
BALATON Zoltan0d793792012-11-03 12:47:08 +0100699 }
thsd34cab92007-04-02 01:10:46 +0000700#ifdef HW_MOUSE_ACCEL
701 vmsvga_cursor_define(s, &cursor);
702 break;
703#else
704 args = 0;
705 goto badcmd;
706#endif
707
708 /*
709 * Other commands that we at least know the number of arguments
710 * for so we can avoid FIFO desync if driver uses them illegally.
711 */
712 case SVGA_CMD_DEFINE_ALPHA_CURSOR:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200713 len -= 6;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100714 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200715 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100716 }
thsd34cab92007-04-02 01:10:46 +0000717 vmsvga_fifo_read(s);
718 vmsvga_fifo_read(s);
719 vmsvga_fifo_read(s);
720 x = vmsvga_fifo_read(s);
721 y = vmsvga_fifo_read(s);
722 args = x * y;
723 goto badcmd;
724 case SVGA_CMD_RECT_ROP_FILL:
725 args = 6;
726 goto badcmd;
727 case SVGA_CMD_RECT_ROP_COPY:
728 args = 7;
729 goto badcmd;
730 case SVGA_CMD_DRAW_GLYPH_CLIPPED:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200731 len -= 4;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100732 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200733 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100734 }
thsd34cab92007-04-02 01:10:46 +0000735 vmsvga_fifo_read(s);
736 vmsvga_fifo_read(s);
737 args = 7 + (vmsvga_fifo_read(s) >> 2);
738 goto badcmd;
739 case SVGA_CMD_SURFACE_ALPHA_BLEND:
740 args = 12;
741 goto badcmd;
742
743 /*
744 * Other commands that are not listed as depending on any
745 * CAPABILITIES bits, but are not described in the README either.
746 */
747 case SVGA_CMD_SURFACE_FILL:
748 case SVGA_CMD_SURFACE_COPY:
749 case SVGA_CMD_FRONT_ROP_FILL:
750 case SVGA_CMD_FENCE:
751 case SVGA_CMD_INVALID_CMD:
752 break; /* Nop */
753
754 default:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200755 args = 0;
thsd34cab92007-04-02 01:10:46 +0000756 badcmd:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200757 len -= args;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100758 if (len < 0) {
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200759 goto rewind;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100760 }
761 while (args--) {
thsd34cab92007-04-02 01:10:46 +0000762 vmsvga_fifo_read(s);
BALATON Zoltan0d793792012-11-03 12:47:08 +0100763 }
thsd34cab92007-04-02 01:10:46 +0000764 printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
BALATON Zoltan0d793792012-11-03 12:47:08 +0100765 __func__, cmd);
thsd34cab92007-04-02 01:10:46 +0000766 break;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200767
768 rewind:
Gerd Hoffmann7e486f72016-05-30 09:09:20 +0200769 s->fifo_stop = cmd_start;
770 s->fifo[SVGA_FIFO_STOP] = cpu_to_le32(s->fifo_stop);
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200771 break;
thsd34cab92007-04-02 01:10:46 +0000772 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200773 }
thsd34cab92007-04-02 01:10:46 +0000774
775 s->syncing = 0;
776}
777
778static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
779{
Juan Quintela467d44b2009-10-14 17:49:08 +0200780 struct vmsvga_state_s *s = opaque;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100781
thsd34cab92007-04-02 01:10:46 +0000782 return s->index;
783}
784
785static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
786{
Juan Quintela467d44b2009-10-14 17:49:08 +0200787 struct vmsvga_state_s *s = opaque;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100788
thsd34cab92007-04-02 01:10:46 +0000789 s->index = index;
790}
791
792static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
793{
794 uint32_t caps;
Juan Quintela467d44b2009-10-14 17:49:08 +0200795 struct vmsvga_state_s *s = opaque;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100796 DisplaySurface *surface = qemu_console_surface(s->vga.con);
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100797 PixelFormat pf;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100798 uint32_t ret;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100799
thsd34cab92007-04-02 01:10:46 +0000800 switch (s->index) {
801 case SVGA_REG_ID:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100802 ret = s->svgaid;
803 break;
thsd34cab92007-04-02 01:10:46 +0000804
805 case SVGA_REG_ENABLE:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100806 ret = s->enable;
807 break;
thsd34cab92007-04-02 01:10:46 +0000808
809 case SVGA_REG_WIDTH:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100810 ret = s->new_width ? s->new_width : surface_width(surface);
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100811 break;
thsd34cab92007-04-02 01:10:46 +0000812
813 case SVGA_REG_HEIGHT:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100814 ret = s->new_height ? s->new_height : surface_height(surface);
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100815 break;
thsd34cab92007-04-02 01:10:46 +0000816
817 case SVGA_REG_MAX_WIDTH:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100818 ret = SVGA_MAX_WIDTH;
819 break;
thsd34cab92007-04-02 01:10:46 +0000820
821 case SVGA_REG_MAX_HEIGHT:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100822 ret = SVGA_MAX_HEIGHT;
823 break;
thsd34cab92007-04-02 01:10:46 +0000824
825 case SVGA_REG_DEPTH:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100826 ret = (s->new_depth == 32) ? 24 : s->new_depth;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100827 break;
thsd34cab92007-04-02 01:10:46 +0000828
829 case SVGA_REG_BITS_PER_PIXEL:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100830 case SVGA_REG_HOST_BITS_PER_PIXEL:
831 ret = s->new_depth;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100832 break;
thsd34cab92007-04-02 01:10:46 +0000833
834 case SVGA_REG_PSEUDOCOLOR:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100835 ret = 0x0;
836 break;
thsd34cab92007-04-02 01:10:46 +0000837
838 case SVGA_REG_RED_MASK:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100839 pf = qemu_default_pixelformat(s->new_depth);
840 ret = pf.rmask;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100841 break;
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100842
thsd34cab92007-04-02 01:10:46 +0000843 case SVGA_REG_GREEN_MASK:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100844 pf = qemu_default_pixelformat(s->new_depth);
845 ret = pf.gmask;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100846 break;
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100847
thsd34cab92007-04-02 01:10:46 +0000848 case SVGA_REG_BLUE_MASK:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100849 pf = qemu_default_pixelformat(s->new_depth);
850 ret = pf.bmask;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100851 break;
thsd34cab92007-04-02 01:10:46 +0000852
853 case SVGA_REG_BYTES_PER_LINE:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +0100854 if (s->new_width) {
855 ret = (s->new_depth * s->new_width) / 8;
856 } else {
857 ret = surface_stride(surface);
858 }
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100859 break;
thsd34cab92007-04-02 01:10:46 +0000860
Avi Kivity7b619b92011-08-08 16:08:56 +0300861 case SVGA_REG_FB_START: {
862 struct pci_vmsvga_state_s *pci_vmsvga
863 = container_of(s, struct pci_vmsvga_state_s, chip);
Andreas Färberaf21c742013-06-30 15:01:36 +0200864 ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 1);
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100865 break;
Avi Kivity7b619b92011-08-08 16:08:56 +0300866 }
thsd34cab92007-04-02 01:10:46 +0000867
868 case SVGA_REG_FB_OFFSET:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100869 ret = 0x0;
870 break;
thsd34cab92007-04-02 01:10:46 +0000871
872 case SVGA_REG_VRAM_SIZE:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100873 ret = s->vga.vram_size; /* No physical VRAM besides the framebuffer */
874 break;
thsd34cab92007-04-02 01:10:46 +0000875
876 case SVGA_REG_FB_SIZE:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100877 ret = s->vga.vram_size;
878 break;
thsd34cab92007-04-02 01:10:46 +0000879
880 case SVGA_REG_CAPABILITIES:
881 caps = SVGA_CAP_NONE;
882#ifdef HW_RECT_ACCEL
883 caps |= SVGA_CAP_RECT_COPY;
884#endif
885#ifdef HW_FILL_ACCEL
886 caps |= SVGA_CAP_RECT_FILL;
887#endif
888#ifdef HW_MOUSE_ACCEL
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100889 if (dpy_cursor_define_supported(s->vga.con)) {
thsd34cab92007-04-02 01:10:46 +0000890 caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
891 SVGA_CAP_CURSOR_BYPASS;
Gerd Hoffmannbf2fde72012-09-12 07:56:45 +0200892 }
thsd34cab92007-04-02 01:10:46 +0000893#endif
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100894 ret = caps;
895 break;
thsd34cab92007-04-02 01:10:46 +0000896
Avi Kivityb1950432011-08-08 16:08:57 +0300897 case SVGA_REG_MEM_START: {
898 struct pci_vmsvga_state_s *pci_vmsvga
899 = container_of(s, struct pci_vmsvga_state_s, chip);
Andreas Färberaf21c742013-06-30 15:01:36 +0200900 ret = pci_get_bar_addr(PCI_DEVICE(pci_vmsvga), 2);
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100901 break;
Avi Kivityb1950432011-08-08 16:08:57 +0300902 }
thsd34cab92007-04-02 01:10:46 +0000903
904 case SVGA_REG_MEM_SIZE:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100905 ret = s->fifo_size;
906 break;
thsd34cab92007-04-02 01:10:46 +0000907
908 case SVGA_REG_CONFIG_DONE:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100909 ret = s->config;
910 break;
thsd34cab92007-04-02 01:10:46 +0000911
912 case SVGA_REG_SYNC:
913 case SVGA_REG_BUSY:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100914 ret = s->syncing;
915 break;
thsd34cab92007-04-02 01:10:46 +0000916
917 case SVGA_REG_GUEST_ID:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100918 ret = s->guest;
919 break;
thsd34cab92007-04-02 01:10:46 +0000920
921 case SVGA_REG_CURSOR_ID:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100922 ret = s->cursor.id;
923 break;
thsd34cab92007-04-02 01:10:46 +0000924
925 case SVGA_REG_CURSOR_X:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100926 ret = s->cursor.x;
927 break;
thsd34cab92007-04-02 01:10:46 +0000928
929 case SVGA_REG_CURSOR_Y:
Nicolas Owense2bb4ae2014-06-08 22:19:17 -0700930 ret = s->cursor.y;
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100931 break;
thsd34cab92007-04-02 01:10:46 +0000932
933 case SVGA_REG_CURSOR_ON:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100934 ret = s->cursor.on;
935 break;
thsd34cab92007-04-02 01:10:46 +0000936
thsd34cab92007-04-02 01:10:46 +0000937 case SVGA_REG_SCRATCH_SIZE:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100938 ret = s->scratch_size;
939 break;
thsd34cab92007-04-02 01:10:46 +0000940
941 case SVGA_REG_MEM_REGS:
942 case SVGA_REG_NUM_DISPLAYS:
943 case SVGA_REG_PITCHLOCK:
944 case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100945 ret = 0;
946 break;
thsd34cab92007-04-02 01:10:46 +0000947
948 default:
949 if (s->index >= SVGA_SCRATCH_BASE &&
BALATON Zoltan0d793792012-11-03 12:47:08 +0100950 s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100951 ret = s->scratch[s->index - SVGA_SCRATCH_BASE];
952 break;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100953 }
954 printf("%s: Bad register %02x\n", __func__, s->index);
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100955 ret = 0;
956 break;
thsd34cab92007-04-02 01:10:46 +0000957 }
958
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100959 if (s->index >= SVGA_SCRATCH_BASE) {
960 trace_vmware_scratch_read(s->index, ret);
961 } else if (s->index >= SVGA_PALETTE_BASE) {
962 trace_vmware_palette_read(s->index, ret);
963 } else {
964 trace_vmware_value_read(s->index, ret);
965 }
966 return ret;
thsd34cab92007-04-02 01:10:46 +0000967}
968
969static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
970{
Juan Quintela467d44b2009-10-14 17:49:08 +0200971 struct vmsvga_state_s *s = opaque;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100972
Gerd Hoffmann7a6404c2013-03-25 09:53:35 +0100973 if (s->index >= SVGA_SCRATCH_BASE) {
974 trace_vmware_scratch_write(s->index, value);
975 } else if (s->index >= SVGA_PALETTE_BASE) {
976 trace_vmware_palette_write(s->index, value);
977 } else {
978 trace_vmware_value_write(s->index, value);
979 }
thsd34cab92007-04-02 01:10:46 +0000980 switch (s->index) {
981 case SVGA_REG_ID:
BALATON Zoltan0d793792012-11-03 12:47:08 +0100982 if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0) {
thsd34cab92007-04-02 01:10:46 +0000983 s->svgaid = value;
BALATON Zoltan0d793792012-11-03 12:47:08 +0100984 }
thsd34cab92007-04-02 01:10:46 +0000985 break;
986
987 case SVGA_REG_ENABLE:
BALATON Zoltanb51d7b22012-11-03 12:47:08 +0100988 s->enable = !!value;
thsd34cab92007-04-02 01:10:46 +0000989 s->invalidated = 1;
Gerd Hoffmann380cd052013-03-13 14:04:18 +0100990 s->vga.hw_ops->invalidate(&s->vga);
BALATON Zoltanb51d7b22012-11-03 12:47:08 +0100991 if (s->enable && s->config) {
Andrzej Zaborowski9f810be2010-09-10 02:30:04 +0200992 vga_dirty_log_stop(&s->vga);
993 } else {
994 vga_dirty_log_start(&s->vga);
995 }
thsd34cab92007-04-02 01:10:46 +0000996 break;
997
998 case SVGA_REG_WIDTH:
BALATON Zoltanaa32b382012-11-03 12:47:08 +0100999 if (value <= SVGA_MAX_WIDTH) {
1000 s->new_width = value;
1001 s->invalidated = 1;
1002 } else {
1003 printf("%s: Bad width: %i\n", __func__, value);
1004 }
thsd34cab92007-04-02 01:10:46 +00001005 break;
1006
1007 case SVGA_REG_HEIGHT:
BALATON Zoltanaa32b382012-11-03 12:47:08 +01001008 if (value <= SVGA_MAX_HEIGHT) {
1009 s->new_height = value;
1010 s->invalidated = 1;
1011 } else {
1012 printf("%s: Bad height: %i\n", __func__, value);
1013 }
thsd34cab92007-04-02 01:10:46 +00001014 break;
1015
thsd34cab92007-04-02 01:10:46 +00001016 case SVGA_REG_BITS_PER_PIXEL:
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001017 if (value != 32) {
BALATON Zoltan5b9575c2012-11-03 12:47:08 +01001018 printf("%s: Bad bits per pixel: %i bits\n", __func__, value);
thsd34cab92007-04-02 01:10:46 +00001019 s->config = 0;
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001020 s->invalidated = 1;
thsd34cab92007-04-02 01:10:46 +00001021 }
1022 break;
1023
1024 case SVGA_REG_CONFIG_DONE:
1025 if (value) {
Dave Airlief351d052009-12-18 08:08:06 +10001026 s->fifo = (uint32_t *) s->fifo_ptr;
BALATON Zoltanb51d7b22012-11-03 12:47:08 +01001027 vga_dirty_log_stop(&s->vga);
thsd34cab92007-04-02 01:10:46 +00001028 }
balrogf707cfb2007-05-13 13:26:49 +00001029 s->config = !!value;
thsd34cab92007-04-02 01:10:46 +00001030 break;
1031
1032 case SVGA_REG_SYNC:
1033 s->syncing = 1;
1034 vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
1035 break;
1036
1037 case SVGA_REG_GUEST_ID:
1038 s->guest = value;
1039#ifdef VERBOSE
1040 if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
BALATON Zoltan0d793792012-11-03 12:47:08 +01001041 ARRAY_SIZE(vmsvga_guest_id)) {
1042 printf("%s: guest runs %s.\n", __func__,
1043 vmsvga_guest_id[value - GUEST_OS_BASE]);
1044 }
thsd34cab92007-04-02 01:10:46 +00001045#endif
1046 break;
1047
1048 case SVGA_REG_CURSOR_ID:
1049 s->cursor.id = value;
1050 break;
1051
1052 case SVGA_REG_CURSOR_X:
1053 s->cursor.x = value;
1054 break;
1055
1056 case SVGA_REG_CURSOR_Y:
1057 s->cursor.y = value;
1058 break;
1059
1060 case SVGA_REG_CURSOR_ON:
1061 s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
1062 s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
1063#ifdef HW_MOUSE_ACCEL
Gerd Hoffmannbf2fde72012-09-12 07:56:45 +02001064 if (value <= SVGA_CURSOR_ON_SHOW) {
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001065 dpy_mouse_set(s->vga.con, s->cursor.x, s->cursor.y, s->cursor.on);
Gerd Hoffmannbf2fde72012-09-12 07:56:45 +02001066 }
thsd34cab92007-04-02 01:10:46 +00001067#endif
1068 break;
1069
BALATON Zoltan5b9575c2012-11-03 12:47:08 +01001070 case SVGA_REG_DEPTH:
thsd34cab92007-04-02 01:10:46 +00001071 case SVGA_REG_MEM_REGS:
1072 case SVGA_REG_NUM_DISPLAYS:
1073 case SVGA_REG_PITCHLOCK:
1074 case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
1075 break;
1076
1077 default:
1078 if (s->index >= SVGA_SCRATCH_BASE &&
1079 s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
1080 s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
1081 break;
1082 }
BALATON Zoltan0d793792012-11-03 12:47:08 +01001083 printf("%s: Bad register %02x\n", __func__, s->index);
thsd34cab92007-04-02 01:10:46 +00001084 }
1085}
1086
1087static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
1088{
BALATON Zoltan0d793792012-11-03 12:47:08 +01001089 printf("%s: what are we supposed to return?\n", __func__);
thsd34cab92007-04-02 01:10:46 +00001090 return 0xcafe;
1091}
1092
1093static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
1094{
BALATON Zoltan0d793792012-11-03 12:47:08 +01001095 printf("%s: what are we supposed to do with (%08x)?\n", __func__, data);
thsd34cab92007-04-02 01:10:46 +00001096}
1097
BALATON Zoltanaa32b382012-11-03 12:47:08 +01001098static inline void vmsvga_check_size(struct vmsvga_state_s *s)
thsd34cab92007-04-02 01:10:46 +00001099{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001100 DisplaySurface *surface = qemu_console_surface(s->vga.con);
1101
1102 if (s->new_width != surface_width(surface) ||
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001103 s->new_height != surface_height(surface) ||
1104 s->new_depth != surface_bits_per_pixel(surface)) {
1105 int stride = (s->new_depth * s->new_width) / 8;
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001106 pixman_format_code_t format =
1107 qemu_default_pixman_format(s->new_depth, true);
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001108 trace_vmware_setmode(s->new_width, s->new_height, s->new_depth);
1109 surface = qemu_create_displaysurface_from(s->new_width, s->new_height,
Gerd Hoffmann30f1e662014-06-18 11:03:15 +02001110 format, stride,
1111 s->vga.vram_ptr);
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001112 dpy_gfx_replace_surface(s->vga.con, surface);
thsd34cab92007-04-02 01:10:46 +00001113 s->invalidated = 1;
1114 }
1115}
1116
1117static void vmsvga_update_display(void *opaque)
1118{
Juan Quintela467d44b2009-10-14 17:49:08 +02001119 struct vmsvga_state_s *s = opaque;
Igor Mitsyanko17866fc2013-03-19 23:44:56 +04001120 DisplaySurface *surface;
BALATON Zoltanb51d7b22012-11-03 12:47:08 +01001121 bool dirty = false;
1122
thsd34cab92007-04-02 01:10:46 +00001123 if (!s->enable) {
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001124 s->vga.hw_ops->gfx_update(&s->vga);
thsd34cab92007-04-02 01:10:46 +00001125 return;
1126 }
1127
BALATON Zoltanaa32b382012-11-03 12:47:08 +01001128 vmsvga_check_size(s);
Igor Mitsyanko17866fc2013-03-19 23:44:56 +04001129 surface = qemu_console_surface(s->vga.con);
thsd34cab92007-04-02 01:10:46 +00001130
1131 vmsvga_fifo_run(s);
1132 vmsvga_update_rect_flush(s);
1133
1134 /*
1135 * Is it more efficient to look at vram VGA-dirty bits or wait
1136 * for the driver to issue SVGA_CMD_UPDATE?
1137 */
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +01001138 if (memory_region_is_logging(&s->vga.vram, DIRTY_MEMORY_VGA)) {
BALATON Zoltanb51d7b22012-11-03 12:47:08 +01001139 vga_sync_dirty_bitmap(&s->vga);
1140 dirty = memory_region_get_dirty(&s->vga.vram, 0,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001141 surface_stride(surface) * surface_height(surface),
BALATON Zoltanb51d7b22012-11-03 12:47:08 +01001142 DIRTY_MEMORY_VGA);
1143 }
1144 if (s->invalidated || dirty) {
thsd34cab92007-04-02 01:10:46 +00001145 s->invalidated = 0;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001146 dpy_gfx_update(s->vga.con, 0, 0,
1147 surface_width(surface), surface_height(surface));
BALATON Zoltanb51d7b22012-11-03 12:47:08 +01001148 }
1149 if (dirty) {
1150 memory_region_reset_dirty(&s->vga.vram, 0,
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001151 surface_stride(surface) * surface_height(surface),
BALATON Zoltanb51d7b22012-11-03 12:47:08 +01001152 DIRTY_MEMORY_VGA);
thsd34cab92007-04-02 01:10:46 +00001153 }
1154}
1155
Jan Kiszka8a9501b2011-08-22 19:12:08 +02001156static void vmsvga_reset(DeviceState *dev)
thsd34cab92007-04-02 01:10:46 +00001157{
Peter Crosthwaite39d45982013-06-24 16:58:45 +10001158 struct pci_vmsvga_state_s *pci = VMWARE_SVGA(dev);
Jan Kiszka8a9501b2011-08-22 19:12:08 +02001159 struct vmsvga_state_s *s = &pci->chip;
1160
thsd34cab92007-04-02 01:10:46 +00001161 s->index = 0;
1162 s->enable = 0;
1163 s->config = 0;
thsd34cab92007-04-02 01:10:46 +00001164 s->svgaid = SVGA_ID;
thsd34cab92007-04-02 01:10:46 +00001165 s->cursor.on = 0;
1166 s->redraw_fifo_first = 0;
1167 s->redraw_fifo_last = 0;
thsd34cab92007-04-02 01:10:46 +00001168 s->syncing = 0;
Anthony Liguorib5cc6e32009-12-18 08:08:10 +10001169
1170 vga_dirty_log_start(&s->vga);
thsd34cab92007-04-02 01:10:46 +00001171}
1172
1173static void vmsvga_invalidate_display(void *opaque)
1174{
Juan Quintela467d44b2009-10-14 17:49:08 +02001175 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +00001176 if (!s->enable) {
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001177 s->vga.hw_ops->invalidate(&s->vga);
thsd34cab92007-04-02 01:10:46 +00001178 return;
1179 }
1180
1181 s->invalidated = 1;
1182}
1183
Anthony Liguoric227f092009-10-01 16:12:16 -05001184static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001185{
Juan Quintela467d44b2009-10-14 17:49:08 +02001186 struct vmsvga_state_s *s = opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001187
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001188 if (s->vga.hw_ops->text_update) {
1189 s->vga.hw_ops->text_update(&s->vga, chardata);
BALATON Zoltan0d793792012-11-03 12:47:08 +01001190 }
balrog4d3b6f62008-02-10 16:33:14 +00001191}
1192
Juan Quintelabacbe282009-10-14 19:30:22 +02001193static int vmsvga_post_load(void *opaque, int version_id)
thsd34cab92007-04-02 01:10:46 +00001194{
Juan Quintelabacbe282009-10-14 19:30:22 +02001195 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +00001196
1197 s->invalidated = 1;
BALATON Zoltan0d793792012-11-03 12:47:08 +01001198 if (s->config) {
Dave Airlief351d052009-12-18 08:08:06 +10001199 s->fifo = (uint32_t *) s->fifo_ptr;
BALATON Zoltan0d793792012-11-03 12:47:08 +01001200 }
thsd34cab92007-04-02 01:10:46 +00001201 return 0;
1202}
1203
Blue Swirld05ac8f2009-12-04 20:44:44 +00001204static const VMStateDescription vmstate_vmware_vga_internal = {
Juan Quintelabacbe282009-10-14 19:30:22 +02001205 .name = "vmware_vga_internal",
1206 .version_id = 0,
1207 .minimum_version_id = 0,
Juan Quintelabacbe282009-10-14 19:30:22 +02001208 .post_load = vmsvga_post_load,
Juan Quintelad49805a2014-04-16 15:32:32 +02001209 .fields = (VMStateField[]) {
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001210 VMSTATE_INT32_EQUAL(new_depth, struct vmsvga_state_s),
Juan Quintelabacbe282009-10-14 19:30:22 +02001211 VMSTATE_INT32(enable, struct vmsvga_state_s),
1212 VMSTATE_INT32(config, struct vmsvga_state_s),
1213 VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
1214 VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
1215 VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
1216 VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
1217 VMSTATE_INT32(index, struct vmsvga_state_s),
1218 VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
1219 scratch_size, 0, vmstate_info_uint32, uint32_t),
1220 VMSTATE_INT32(new_width, struct vmsvga_state_s),
1221 VMSTATE_INT32(new_height, struct vmsvga_state_s),
1222 VMSTATE_UINT32(guest, struct vmsvga_state_s),
1223 VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
1224 VMSTATE_INT32(syncing, struct vmsvga_state_s),
BALATON Zoltan5b9575c2012-11-03 12:47:08 +01001225 VMSTATE_UNUSED(4), /* was fb_size */
Juan Quintelabacbe282009-10-14 19:30:22 +02001226 VMSTATE_END_OF_LIST()
1227 }
1228};
1229
Blue Swirld05ac8f2009-12-04 20:44:44 +00001230static const VMStateDescription vmstate_vmware_vga = {
Juan Quintelabacbe282009-10-14 19:30:22 +02001231 .name = "vmware_vga",
1232 .version_id = 0,
1233 .minimum_version_id = 0,
Juan Quintelad49805a2014-04-16 15:32:32 +02001234 .fields = (VMStateField[]) {
Andreas Färberaf21c742013-06-30 15:01:36 +02001235 VMSTATE_PCI_DEVICE(parent_obj, struct pci_vmsvga_state_s),
Juan Quintelabacbe282009-10-14 19:30:22 +02001236 VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
1237 vmstate_vmware_vga_internal, struct vmsvga_state_s),
1238 VMSTATE_END_OF_LIST()
1239 }
1240};
1241
Gerd Hoffmann380cd052013-03-13 14:04:18 +01001242static const GraphicHwOps vmsvga_ops = {
1243 .invalidate = vmsvga_invalidate_display,
1244 .gfx_update = vmsvga_update_display,
1245 .text_update = vmsvga_text_update,
1246};
1247
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001248static void vmsvga_init(DeviceState *dev, struct vmsvga_state_s *s,
Richard Henderson0a039dc2011-08-16 08:27:39 -07001249 MemoryRegion *address_space, MemoryRegion *io)
thsd34cab92007-04-02 01:10:46 +00001250{
thsd34cab92007-04-02 01:10:46 +00001251 s->scratch_size = SVGA_SCRATCH_SIZE;
Anthony Liguori7267c092011-08-20 22:09:37 -05001252 s->scratch = g_malloc(s->scratch_size * 4);
thsd34cab92007-04-02 01:10:46 +00001253
Gerd Hoffmann56437062014-01-24 15:35:21 +01001254 s->vga.con = graphic_console_init(dev, 0, &vmsvga_ops, s);
Andrzej Zaborowski4445b0a2009-08-23 19:00:58 +02001255
Dave Airlief351d052009-12-18 08:08:06 +10001256 s->fifo_size = SVGA_FIFO_SIZE;
Hu Tao49946532014-09-09 13:27:55 +08001257 memory_region_init_ram(&s->fifo_ram, NULL, "vmsvga.fifo", s->fifo_size,
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +02001258 &error_fatal);
Avi Kivityc5705a72011-12-20 15:59:12 +02001259 vmstate_register_ram_global(&s->fifo_ram);
Avi Kivityb1950432011-08-08 16:08:57 +03001260 s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
Dave Airlief351d052009-12-18 08:08:06 +10001261
Gerd Hoffmanne2bbfc82013-10-11 19:56:59 +02001262 vga_common_init(&s->vga, OBJECT(dev), true);
Paolo Bonzini712f0cc2013-06-06 21:21:13 -04001263 vga_init(&s->vga, OBJECT(dev), address_space, io, true);
Alex Williamson0be71e32010-06-25 11:09:07 -06001264 vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
Gerd Hoffmanneb2f9b02013-03-25 11:44:21 +01001265 s->new_depth = 32;
thsd34cab92007-04-02 01:10:46 +00001266}
1267
BALATON Zoltanaa32b382012-11-03 12:47:08 +01001268static uint64_t vmsvga_io_read(void *opaque, hwaddr addr, unsigned size)
balrog1492a3c2008-01-14 01:52:52 +00001269{
Avi Kivityb1950432011-08-08 16:08:57 +03001270 struct vmsvga_state_s *s = opaque;
balrog1492a3c2008-01-14 01:52:52 +00001271
Avi Kivityb1950432011-08-08 16:08:57 +03001272 switch (addr) {
1273 case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
1274 case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
1275 case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
1276 default: return -1u;
1277 }
balrog1492a3c2008-01-14 01:52:52 +00001278}
1279
Avi Kivitya8170e52012-10-23 12:30:10 +02001280static void vmsvga_io_write(void *opaque, hwaddr addr,
Avi Kivityb1950432011-08-08 16:08:57 +03001281 uint64_t data, unsigned size)
balrog3016d802008-03-06 20:28:49 +00001282{
Avi Kivityb1950432011-08-08 16:08:57 +03001283 struct vmsvga_state_s *s = opaque;
balrog3016d802008-03-06 20:28:49 +00001284
Avi Kivityb1950432011-08-08 16:08:57 +03001285 switch (addr) {
1286 case SVGA_IO_MUL * SVGA_INDEX_PORT:
Blue Swirl0ed8b6f2012-07-08 06:56:53 +00001287 vmsvga_index_write(s, addr, data);
1288 break;
Avi Kivityb1950432011-08-08 16:08:57 +03001289 case SVGA_IO_MUL * SVGA_VALUE_PORT:
Blue Swirl0ed8b6f2012-07-08 06:56:53 +00001290 vmsvga_value_write(s, addr, data);
1291 break;
Avi Kivityb1950432011-08-08 16:08:57 +03001292 case SVGA_IO_MUL * SVGA_BIOS_PORT:
Blue Swirl0ed8b6f2012-07-08 06:56:53 +00001293 vmsvga_bios_write(s, addr, data);
1294 break;
Avi Kivityb1950432011-08-08 16:08:57 +03001295 }
balrog3016d802008-03-06 20:28:49 +00001296}
1297
Avi Kivityb1950432011-08-08 16:08:57 +03001298static const MemoryRegionOps vmsvga_io_ops = {
1299 .read = vmsvga_io_read,
1300 .write = vmsvga_io_write,
1301 .endianness = DEVICE_LITTLE_ENDIAN,
1302 .valid = {
1303 .min_access_size = 4,
1304 .max_access_size = 4,
Jan Kiszka04e8cd52013-06-22 08:07:02 +02001305 .unaligned = true,
1306 },
1307 .impl = {
1308 .unaligned = true,
Avi Kivityb1950432011-08-08 16:08:57 +03001309 },
1310};
Dave Airlief351d052009-12-18 08:08:06 +10001311
Markus Armbruster9af21db2015-01-19 15:52:30 +01001312static void pci_vmsvga_realize(PCIDevice *dev, Error **errp)
thsd34cab92007-04-02 01:10:46 +00001313{
Peter Crosthwaite39d45982013-06-24 16:58:45 +10001314 struct pci_vmsvga_state_s *s = VMWARE_SVGA(dev);
Avi Kivityb1950432011-08-08 16:08:57 +03001315
Andreas Färberaf21c742013-06-30 15:01:36 +02001316 dev->config[PCI_CACHE_LINE_SIZE] = 0x08;
1317 dev->config[PCI_LATENCY_TIMER] = 0x40;
1318 dev->config[PCI_INTERRUPT_LINE] = 0xff; /* End */
thsd34cab92007-04-02 01:10:46 +00001319
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001320 memory_region_init_io(&s->io_bar, NULL, &vmsvga_io_ops, &s->chip,
Avi Kivityb1950432011-08-08 16:08:57 +03001321 "vmsvga-io", 0x10);
Jan Kiszkabd8f2f52012-08-23 13:02:33 +02001322 memory_region_set_flush_coalesced(&s->io_bar);
Andreas Färberaf21c742013-06-30 15:01:36 +02001323 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
Dave Airlief351d052009-12-18 08:08:06 +10001324
Gerd Hoffmannaa2beaa2013-04-17 10:21:27 +02001325 vmsvga_init(DEVICE(dev), &s->chip,
1326 pci_address_space(dev), pci_address_space_io(dev));
thsd34cab92007-04-02 01:10:46 +00001327
Andreas Färberaf21c742013-06-30 15:01:36 +02001328 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
BALATON Zoltanaa32b382012-11-03 12:47:08 +01001329 &s->chip.vga.vram);
Andreas Färberaf21c742013-06-30 15:01:36 +02001330 pci_register_bar(dev, 2, PCI_BASE_ADDRESS_MEM_PREFETCH,
Avi Kivitye824b2c2011-08-08 16:09:31 +03001331 &s->chip.fifo_ram);
Avi Kivityb1950432011-08-08 16:08:57 +03001332
Gerd Hoffmann281a26b2010-11-17 12:06:44 +01001333 if (!dev->rom_bar) {
1334 /* compatibility with pc-0.13 and older */
Paolo Bonzini83118322013-06-06 21:21:13 -04001335 vga_init_vbe(&s->chip.vga, OBJECT(dev), pci_address_space(dev));
Gerd Hoffmann281a26b2010-11-17 12:06:44 +01001336 }
thsd34cab92007-04-02 01:10:46 +00001337}
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001338
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001339static Property vga_vmware_properties[] = {
1340 DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s,
Gerd Hoffmann9e56edc2012-06-11 10:42:53 +02001341 chip.vga.vram_size_mb, 16),
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001342 DEFINE_PROP_END_OF_LIST(),
1343};
1344
Anthony Liguori40021f02011-12-04 12:22:06 -06001345static void vmsvga_class_init(ObjectClass *klass, void *data)
1346{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001347 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06001348 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Isaku Yamahata310faae2011-05-25 10:58:04 +09001349
Markus Armbruster9af21db2015-01-19 15:52:30 +01001350 k->realize = pci_vmsvga_realize;
Anthony Liguori40021f02011-12-04 12:22:06 -06001351 k->romfile = "vgabios-vmware.bin";
1352 k->vendor_id = PCI_VENDOR_ID_VMWARE;
1353 k->device_id = SVGA_PCI_DEVICE_ID;
1354 k->class_id = PCI_CLASS_DISPLAY_VGA;
1355 k->subsystem_vendor_id = PCI_VENDOR_ID_VMWARE;
1356 k->subsystem_id = SVGA_PCI_DEVICE_ID;
Anthony Liguori39bffca2011-12-07 21:34:16 -06001357 dc->reset = vmsvga_reset;
1358 dc->vmsd = &vmstate_vmware_vga;
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001359 dc->props = vga_vmware_properties;
Igor Mammedov2897ae02014-02-05 16:36:48 +01001360 dc->hotpluggable = false;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +03001361 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
Anthony Liguori40021f02011-12-04 12:22:06 -06001362}
1363
Andreas Färber8c43a6f2013-01-10 16:19:07 +01001364static const TypeInfo vmsvga_info = {
Peter Crosthwaite39d45982013-06-24 16:58:45 +10001365 .name = TYPE_VMWARE_SVGA,
Anthony Liguori39bffca2011-12-07 21:34:16 -06001366 .parent = TYPE_PCI_DEVICE,
1367 .instance_size = sizeof(struct pci_vmsvga_state_s),
1368 .class_init = vmsvga_class_init,
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001369};
1370
Andreas Färber83f7d432012-02-09 15:20:55 +01001371static void vmsvga_register_types(void)
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001372{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001373 type_register_static(&vmsvga_info);
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001374}
Andreas Färber83f7d432012-02-09 15:20:55 +01001375
1376type_init(vmsvga_register_types)