blob: badaf7cef479013de23a179ff9804b91d7ea5f94 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw.h"
Dave Airlieb3c3f122009-12-11 15:47:44 +100025#include "loader.h"
pbrook87ecb682007-11-17 17:14:51 +000026#include "console.h"
27#include "pci.h"
thsd34cab92007-04-02 01:10:46 +000028
Jan Kiszkaca0508d2011-08-22 19:12:09 +020029#undef VERBOSE
thsd34cab92007-04-02 01:10:46 +000030#define HW_RECT_ACCEL
31#define HW_FILL_ACCEL
32#define HW_MOUSE_ACCEL
33
thsd34cab92007-04-02 01:10:46 +000034# include "vga_int.h"
thsd34cab92007-04-02 01:10:46 +000035
36struct vmsvga_state_s {
Avi Kivity4e12cd92009-05-03 22:25:16 +030037 VGACommonState vga;
thsd34cab92007-04-02 01:10:46 +000038
39 int width;
40 int height;
41 int invalidated;
42 int depth;
43 int bypp;
44 int enable;
45 int config;
46 struct {
47 int id;
48 int x;
49 int y;
50 int on;
51 } cursor;
52
thsd34cab92007-04-02 01:10:46 +000053 int index;
54 int scratch_size;
55 uint32_t *scratch;
56 int new_width;
57 int new_height;
58 uint32_t guest;
59 uint32_t svgaid;
60 uint32_t wred;
61 uint32_t wgreen;
62 uint32_t wblue;
63 int syncing;
64 int fb_size;
65
Avi Kivityb1950432011-08-08 16:08:57 +030066 MemoryRegion fifo_ram;
Dave Airlief351d052009-12-18 08:08:06 +100067 uint8_t *fifo_ptr;
68 unsigned int fifo_size;
Dave Airlief351d052009-12-18 08:08:06 +100069
thsd34cab92007-04-02 01:10:46 +000070 union {
71 uint32_t *fifo;
Stefan Weil541dc0d2011-08-31 12:38:01 +020072 struct QEMU_PACKED {
thsd34cab92007-04-02 01:10:46 +000073 uint32_t min;
74 uint32_t max;
75 uint32_t next_cmd;
76 uint32_t stop;
77 /* Add registers here when adding capabilities. */
78 uint32_t fifo[0];
79 } *cmd;
80 };
81
82#define REDRAW_FIFO_LEN 512
83 struct vmsvga_rect_s {
84 int x, y, w, h;
85 } redraw_fifo[REDRAW_FIFO_LEN];
86 int redraw_fifo_first, redraw_fifo_last;
87};
88
89struct pci_vmsvga_state_s {
90 PCIDevice card;
91 struct vmsvga_state_s chip;
Avi Kivityb1950432011-08-08 16:08:57 +030092 MemoryRegion io_bar;
thsd34cab92007-04-02 01:10:46 +000093};
94
95#define SVGA_MAGIC 0x900000UL
96#define SVGA_MAKE_ID(ver) (SVGA_MAGIC << 8 | (ver))
97#define SVGA_ID_0 SVGA_MAKE_ID(0)
98#define SVGA_ID_1 SVGA_MAKE_ID(1)
99#define SVGA_ID_2 SVGA_MAKE_ID(2)
100
101#define SVGA_LEGACY_BASE_PORT 0x4560
102#define SVGA_INDEX_PORT 0x0
103#define SVGA_VALUE_PORT 0x1
104#define SVGA_BIOS_PORT 0x2
105
106#define SVGA_VERSION_2
107
108#ifdef SVGA_VERSION_2
109# define SVGA_ID SVGA_ID_2
110# define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
111# define SVGA_IO_MUL 1
112# define SVGA_FIFO_SIZE 0x10000
thsd34cab92007-04-02 01:10:46 +0000113# define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA2
114#else
115# define SVGA_ID SVGA_ID_1
116# define SVGA_IO_BASE SVGA_LEGACY_BASE_PORT
117# define SVGA_IO_MUL 4
118# define SVGA_FIFO_SIZE 0x10000
thsd34cab92007-04-02 01:10:46 +0000119# define SVGA_PCI_DEVICE_ID PCI_DEVICE_ID_VMWARE_SVGA
120#endif
121
122enum {
123 /* ID 0, 1 and 2 registers */
124 SVGA_REG_ID = 0,
125 SVGA_REG_ENABLE = 1,
126 SVGA_REG_WIDTH = 2,
127 SVGA_REG_HEIGHT = 3,
128 SVGA_REG_MAX_WIDTH = 4,
129 SVGA_REG_MAX_HEIGHT = 5,
130 SVGA_REG_DEPTH = 6,
131 SVGA_REG_BITS_PER_PIXEL = 7, /* Current bpp in the guest */
132 SVGA_REG_PSEUDOCOLOR = 8,
133 SVGA_REG_RED_MASK = 9,
134 SVGA_REG_GREEN_MASK = 10,
135 SVGA_REG_BLUE_MASK = 11,
136 SVGA_REG_BYTES_PER_LINE = 12,
137 SVGA_REG_FB_START = 13,
138 SVGA_REG_FB_OFFSET = 14,
139 SVGA_REG_VRAM_SIZE = 15,
140 SVGA_REG_FB_SIZE = 16,
141
142 /* ID 1 and 2 registers */
143 SVGA_REG_CAPABILITIES = 17,
144 SVGA_REG_MEM_START = 18, /* Memory for command FIFO */
145 SVGA_REG_MEM_SIZE = 19,
146 SVGA_REG_CONFIG_DONE = 20, /* Set when memory area configured */
147 SVGA_REG_SYNC = 21, /* Write to force synchronization */
148 SVGA_REG_BUSY = 22, /* Read to check if sync is done */
149 SVGA_REG_GUEST_ID = 23, /* Set guest OS identifier */
150 SVGA_REG_CURSOR_ID = 24, /* ID of cursor */
151 SVGA_REG_CURSOR_X = 25, /* Set cursor X position */
152 SVGA_REG_CURSOR_Y = 26, /* Set cursor Y position */
153 SVGA_REG_CURSOR_ON = 27, /* Turn cursor on/off */
154 SVGA_REG_HOST_BITS_PER_PIXEL = 28, /* Current bpp in the host */
155 SVGA_REG_SCRATCH_SIZE = 29, /* Number of scratch registers */
156 SVGA_REG_MEM_REGS = 30, /* Number of FIFO registers */
157 SVGA_REG_NUM_DISPLAYS = 31, /* Number of guest displays */
158 SVGA_REG_PITCHLOCK = 32, /* Fixed pitch for all modes */
159
160 SVGA_PALETTE_BASE = 1024, /* Base of SVGA color map */
161 SVGA_PALETTE_END = SVGA_PALETTE_BASE + 767,
162 SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
163};
164
165#define SVGA_CAP_NONE 0
166#define SVGA_CAP_RECT_FILL (1 << 0)
167#define SVGA_CAP_RECT_COPY (1 << 1)
168#define SVGA_CAP_RECT_PAT_FILL (1 << 2)
169#define SVGA_CAP_LEGACY_OFFSCREEN (1 << 3)
170#define SVGA_CAP_RASTER_OP (1 << 4)
171#define SVGA_CAP_CURSOR (1 << 5)
172#define SVGA_CAP_CURSOR_BYPASS (1 << 6)
173#define SVGA_CAP_CURSOR_BYPASS_2 (1 << 7)
174#define SVGA_CAP_8BIT_EMULATION (1 << 8)
175#define SVGA_CAP_ALPHA_CURSOR (1 << 9)
176#define SVGA_CAP_GLYPH (1 << 10)
177#define SVGA_CAP_GLYPH_CLIPPING (1 << 11)
178#define SVGA_CAP_OFFSCREEN_1 (1 << 12)
179#define SVGA_CAP_ALPHA_BLEND (1 << 13)
180#define SVGA_CAP_3D (1 << 14)
181#define SVGA_CAP_EXTENDED_FIFO (1 << 15)
182#define SVGA_CAP_MULTIMON (1 << 16)
183#define SVGA_CAP_PITCHLOCK (1 << 17)
184
185/*
186 * FIFO offsets (seen as an array of 32-bit words)
187 */
188enum {
189 /*
190 * The original defined FIFO offsets
191 */
192 SVGA_FIFO_MIN = 0,
193 SVGA_FIFO_MAX, /* The distance from MIN to MAX must be at least 10K */
194 SVGA_FIFO_NEXT_CMD,
195 SVGA_FIFO_STOP,
196
197 /*
198 * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
199 */
200 SVGA_FIFO_CAPABILITIES = 4,
201 SVGA_FIFO_FLAGS,
202 SVGA_FIFO_FENCE,
203 SVGA_FIFO_3D_HWVERSION,
204 SVGA_FIFO_PITCHLOCK,
205};
206
207#define SVGA_FIFO_CAP_NONE 0
208#define SVGA_FIFO_CAP_FENCE (1 << 0)
209#define SVGA_FIFO_CAP_ACCELFRONT (1 << 1)
210#define SVGA_FIFO_CAP_PITCHLOCK (1 << 2)
211
212#define SVGA_FIFO_FLAG_NONE 0
213#define SVGA_FIFO_FLAG_ACCELFRONT (1 << 0)
214
215/* These values can probably be changed arbitrarily. */
216#define SVGA_SCRATCH_SIZE 0x8000
217#define SVGA_MAX_WIDTH 2360
218#define SVGA_MAX_HEIGHT 1770
219
220#ifdef VERBOSE
221# define GUEST_OS_BASE 0x5001
222static const char *vmsvga_guest_id[] = {
balrogf707cfb2007-05-13 13:26:49 +0000223 [0x00] = "Dos",
224 [0x01] = "Windows 3.1",
225 [0x02] = "Windows 95",
226 [0x03] = "Windows 98",
227 [0x04] = "Windows ME",
228 [0x05] = "Windows NT",
229 [0x06] = "Windows 2000",
230 [0x07] = "Linux",
231 [0x08] = "OS/2",
blueswir1511d2b12009-03-07 15:32:56 +0000232 [0x09] = "an unknown OS",
balrogf707cfb2007-05-13 13:26:49 +0000233 [0x0a] = "BSD",
234 [0x0b] = "Whistler",
blueswir1511d2b12009-03-07 15:32:56 +0000235 [0x0c] = "an unknown OS",
236 [0x0d] = "an unknown OS",
237 [0x0e] = "an unknown OS",
238 [0x0f] = "an unknown OS",
239 [0x10] = "an unknown OS",
240 [0x11] = "an unknown OS",
241 [0x12] = "an unknown OS",
242 [0x13] = "an unknown OS",
243 [0x14] = "an unknown OS",
balrogf707cfb2007-05-13 13:26:49 +0000244 [0x15] = "Windows 2003",
thsd34cab92007-04-02 01:10:46 +0000245};
246#endif
247
248enum {
249 SVGA_CMD_INVALID_CMD = 0,
250 SVGA_CMD_UPDATE = 1,
251 SVGA_CMD_RECT_FILL = 2,
252 SVGA_CMD_RECT_COPY = 3,
253 SVGA_CMD_DEFINE_BITMAP = 4,
254 SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
255 SVGA_CMD_DEFINE_PIXMAP = 6,
256 SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
257 SVGA_CMD_RECT_BITMAP_FILL = 8,
258 SVGA_CMD_RECT_PIXMAP_FILL = 9,
259 SVGA_CMD_RECT_BITMAP_COPY = 10,
260 SVGA_CMD_RECT_PIXMAP_COPY = 11,
261 SVGA_CMD_FREE_OBJECT = 12,
262 SVGA_CMD_RECT_ROP_FILL = 13,
263 SVGA_CMD_RECT_ROP_COPY = 14,
264 SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
265 SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
266 SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
267 SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
268 SVGA_CMD_DEFINE_CURSOR = 19,
269 SVGA_CMD_DISPLAY_CURSOR = 20,
270 SVGA_CMD_MOVE_CURSOR = 21,
271 SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
272 SVGA_CMD_DRAW_GLYPH = 23,
273 SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
274 SVGA_CMD_UPDATE_VERBOSE = 25,
275 SVGA_CMD_SURFACE_FILL = 26,
276 SVGA_CMD_SURFACE_COPY = 27,
277 SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
278 SVGA_CMD_FRONT_ROP_FILL = 29,
279 SVGA_CMD_FENCE = 30,
280};
281
282/* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
283enum {
284 SVGA_CURSOR_ON_HIDE = 0,
285 SVGA_CURSOR_ON_SHOW = 1,
286 SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
287 SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
288};
289
290static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
291 int x, int y, int w, int h)
292{
balroga8fbaf92008-03-06 20:43:34 +0000293 int line;
294 int bypl;
295 int width;
296 int start;
297 uint8_t *src;
298 uint8_t *dst;
299
300 if (x + w > s->width) {
301 fprintf(stderr, "%s: update width too large x: %d, w: %d\n",
302 __FUNCTION__, x, w);
303 x = MIN(x, s->width);
304 w = s->width - x;
305 }
306
307 if (y + h > s->height) {
308 fprintf(stderr, "%s: update height too large y: %d, h: %d\n",
309 __FUNCTION__, y, h);
310 y = MIN(y, s->height);
311 h = s->height - y;
312 }
313
314 line = h;
315 bypl = s->bypp * s->width;
316 width = s->bypp * w;
317 start = s->bypp * x + bypl * y;
Avi Kivity4e12cd92009-05-03 22:25:16 +0300318 src = s->vga.vram_ptr + start;
319 dst = ds_get_data(s->vga.ds) + start;
thsd34cab92007-04-02 01:10:46 +0000320
321 for (; line > 0; line --, src += bypl, dst += bypl)
322 memcpy(dst, src, width);
thsd34cab92007-04-02 01:10:46 +0000323
Avi Kivity4e12cd92009-05-03 22:25:16 +0300324 dpy_update(s->vga.ds, x, y, w, h);
thsd34cab92007-04-02 01:10:46 +0000325}
326
327static inline void vmsvga_update_screen(struct vmsvga_state_s *s)
328{
Jan Kiszka8d121d42011-08-22 19:12:10 +0200329 memcpy(ds_get_data(s->vga.ds), s->vga.vram_ptr,
330 s->bypp * s->width * s->height);
Avi Kivity4e12cd92009-05-03 22:25:16 +0300331 dpy_update(s->vga.ds, 0, 0, s->width, s->height);
thsd34cab92007-04-02 01:10:46 +0000332}
333
thsd34cab92007-04-02 01:10:46 +0000334static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
335 int x, int y, int w, int h)
336{
337 struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last ++];
338 s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
339 rect->x = x;
340 rect->y = y;
341 rect->w = w;
342 rect->h = h;
343}
thsd34cab92007-04-02 01:10:46 +0000344
345static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
346{
347 struct vmsvga_rect_s *rect;
348 if (s->invalidated) {
349 s->redraw_fifo_first = s->redraw_fifo_last;
350 return;
351 }
352 /* Overlapping region updates can be optimised out here - if someone
353 * knows a smart algorithm to do that, please share. */
354 while (s->redraw_fifo_first != s->redraw_fifo_last) {
355 rect = &s->redraw_fifo[s->redraw_fifo_first ++];
356 s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
357 vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
358 }
359}
360
361#ifdef HW_RECT_ACCEL
362static inline void vmsvga_copy_rect(struct vmsvga_state_s *s,
363 int x0, int y0, int x1, int y1, int w, int h)
364{
Avi Kivity4e12cd92009-05-03 22:25:16 +0300365 uint8_t *vram = s->vga.vram_ptr;
thsd34cab92007-04-02 01:10:46 +0000366 int bypl = s->bypp * s->width;
367 int width = s->bypp * w;
368 int line = h;
369 uint8_t *ptr[2];
370
Jan Kiszka8d121d42011-08-22 19:12:10 +0200371 if (y1 > y0) {
372 ptr[0] = vram + s->bypp * x0 + bypl * (y0 + h - 1);
373 ptr[1] = vram + s->bypp * x1 + bypl * (y1 + h - 1);
374 for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl) {
375 memmove(ptr[1], ptr[0], width);
376 }
377 } else {
378 ptr[0] = vram + s->bypp * x0 + bypl * y0;
379 ptr[1] = vram + s->bypp * x1 + bypl * y1;
380 for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl) {
381 memmove(ptr[1], ptr[0], width);
thsd34cab92007-04-02 01:10:46 +0000382 }
383 }
384
385 vmsvga_update_rect_delayed(s, x1, y1, w, h);
386}
387#endif
388
389#ifdef HW_FILL_ACCEL
390static inline void vmsvga_fill_rect(struct vmsvga_state_s *s,
391 uint32_t c, int x, int y, int w, int h)
392{
Avi Kivity4e12cd92009-05-03 22:25:16 +0300393 uint8_t *vram = s->vga.vram_ptr;
thsd34cab92007-04-02 01:10:46 +0000394 int bypp = s->bypp;
395 int bypl = bypp * s->width;
396 int width = bypp * w;
397 int line = h;
398 int column;
399 uint8_t *fst = vram + bypp * x + bypl * y;
400 uint8_t *dst;
401 uint8_t *src;
402 uint8_t col[4];
403
Jan Kiszka8d121d42011-08-22 19:12:10 +0200404 col[0] = c;
405 col[1] = c >> 8;
406 col[2] = c >> 16;
407 col[3] = c >> 24;
thsd34cab92007-04-02 01:10:46 +0000408
Jan Kiszka8d121d42011-08-22 19:12:10 +0200409 if (line--) {
410 dst = fst;
411 src = col;
412 for (column = width; column > 0; column--) {
413 *(dst++) = *(src++);
414 if (src - col == bypp) {
415 src = col;
thsd34cab92007-04-02 01:10:46 +0000416 }
Jan Kiszka8d121d42011-08-22 19:12:10 +0200417 }
418 dst = fst;
419 for (; line > 0; line--) {
420 dst += bypl;
421 memcpy(dst, fst, width);
thsd34cab92007-04-02 01:10:46 +0000422 }
423 }
424
425 vmsvga_update_rect_delayed(s, x, y, w, h);
426}
427#endif
428
429struct vmsvga_cursor_definition_s {
430 int width;
431 int height;
432 int id;
433 int bpp;
434 int hot_x;
435 int hot_y;
436 uint32_t mask[1024];
Dave Airlie8095cb32009-12-18 08:08:11 +1000437 uint32_t image[4096];
thsd34cab92007-04-02 01:10:46 +0000438};
439
440#define SVGA_BITMAP_SIZE(w, h) ((((w) + 31) >> 5) * (h))
441#define SVGA_PIXMAP_SIZE(w, h, bpp) (((((w) * (bpp)) + 31) >> 5) * (h))
442
443#ifdef HW_MOUSE_ACCEL
444static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
445 struct vmsvga_cursor_definition_s *c)
446{
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200447 QEMUCursor *qc;
448 int i, pixels;
449
450 qc = cursor_alloc(c->width, c->height);
451 qc->hot_x = c->hot_x;
452 qc->hot_y = c->hot_y;
453 switch (c->bpp) {
454 case 1:
455 cursor_set_mono(qc, 0xffffff, 0x000000, (void*)c->image,
456 1, (void*)c->mask);
457#ifdef DEBUG
458 cursor_print_ascii_art(qc, "vmware/mono");
459#endif
460 break;
461 case 32:
462 /* fill alpha channel from mask, set color to zero */
463 cursor_set_mono(qc, 0x000000, 0x000000, (void*)c->mask,
464 1, (void*)c->mask);
465 /* add in rgb values */
466 pixels = c->width * c->height;
467 for (i = 0; i < pixels; i++) {
468 qc->data[i] |= c->image[i] & 0xffffff;
469 }
470#ifdef DEBUG
471 cursor_print_ascii_art(qc, "vmware/32bit");
472#endif
473 break;
474 default:
475 fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
476 __FUNCTION__, c->bpp);
477 cursor_put(qc);
478 qc = cursor_builtin_left_ptr();
479 }
thsd34cab92007-04-02 01:10:46 +0000480
Avi Kivity4e12cd92009-05-03 22:25:16 +0300481 if (s->vga.ds->cursor_define)
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200482 s->vga.ds->cursor_define(qc);
483 cursor_put(qc);
thsd34cab92007-04-02 01:10:46 +0000484}
485#endif
486
balrogff9cf2c2008-07-16 04:45:12 +0000487#define CMD(f) le32_to_cpu(s->cmd->f)
488
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200489static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
thsd34cab92007-04-02 01:10:46 +0000490{
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200491 int num;
thsd34cab92007-04-02 01:10:46 +0000492 if (!s->config || !s->enable)
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200493 return 0;
494 num = CMD(next_cmd) - CMD(stop);
495 if (num < 0)
496 num += CMD(max) - CMD(min);
497 return num >> 2;
thsd34cab92007-04-02 01:10:46 +0000498}
499
balrogff9cf2c2008-07-16 04:45:12 +0000500static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
thsd34cab92007-04-02 01:10:46 +0000501{
balrogff9cf2c2008-07-16 04:45:12 +0000502 uint32_t cmd = s->fifo[CMD(stop) >> 2];
503 s->cmd->stop = cpu_to_le32(CMD(stop) + 4);
504 if (CMD(stop) >= CMD(max))
thsd34cab92007-04-02 01:10:46 +0000505 s->cmd->stop = s->cmd->min;
506 return cmd;
507}
508
balrogff9cf2c2008-07-16 04:45:12 +0000509static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
510{
511 return le32_to_cpu(vmsvga_fifo_read_raw(s));
512}
513
thsd34cab92007-04-02 01:10:46 +0000514static void vmsvga_fifo_run(struct vmsvga_state_s *s)
515{
516 uint32_t cmd, colour;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200517 int args, len;
thsd34cab92007-04-02 01:10:46 +0000518 int x, y, dx, dy, width, height;
519 struct vmsvga_cursor_definition_s cursor;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200520 uint32_t cmd_start;
521
522 len = vmsvga_fifo_length(s);
523 while (len > 0) {
524 /* May need to go back to the start of the command if incomplete */
525 cmd_start = s->cmd->stop;
526
thsd34cab92007-04-02 01:10:46 +0000527 switch (cmd = vmsvga_fifo_read(s)) {
528 case SVGA_CMD_UPDATE:
529 case SVGA_CMD_UPDATE_VERBOSE:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200530 len -= 5;
531 if (len < 0)
532 goto rewind;
533
thsd34cab92007-04-02 01:10:46 +0000534 x = vmsvga_fifo_read(s);
535 y = vmsvga_fifo_read(s);
536 width = vmsvga_fifo_read(s);
537 height = vmsvga_fifo_read(s);
538 vmsvga_update_rect_delayed(s, x, y, width, height);
539 break;
540
541 case SVGA_CMD_RECT_FILL:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200542 len -= 6;
543 if (len < 0)
544 goto rewind;
545
thsd34cab92007-04-02 01:10:46 +0000546 colour = vmsvga_fifo_read(s);
547 x = vmsvga_fifo_read(s);
548 y = vmsvga_fifo_read(s);
549 width = vmsvga_fifo_read(s);
550 height = vmsvga_fifo_read(s);
551#ifdef HW_FILL_ACCEL
552 vmsvga_fill_rect(s, colour, x, y, width, height);
553 break;
554#else
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200555 args = 0;
thsd34cab92007-04-02 01:10:46 +0000556 goto badcmd;
557#endif
558
559 case SVGA_CMD_RECT_COPY:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200560 len -= 7;
561 if (len < 0)
562 goto rewind;
563
thsd34cab92007-04-02 01:10:46 +0000564 x = vmsvga_fifo_read(s);
565 y = vmsvga_fifo_read(s);
566 dx = vmsvga_fifo_read(s);
567 dy = vmsvga_fifo_read(s);
568 width = vmsvga_fifo_read(s);
569 height = vmsvga_fifo_read(s);
570#ifdef HW_RECT_ACCEL
571 vmsvga_copy_rect(s, x, y, dx, dy, width, height);
572 break;
573#else
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200574 args = 0;
thsd34cab92007-04-02 01:10:46 +0000575 goto badcmd;
576#endif
577
578 case SVGA_CMD_DEFINE_CURSOR:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200579 len -= 8;
580 if (len < 0)
581 goto rewind;
582
thsd34cab92007-04-02 01:10:46 +0000583 cursor.id = vmsvga_fifo_read(s);
584 cursor.hot_x = vmsvga_fifo_read(s);
585 cursor.hot_y = vmsvga_fifo_read(s);
586 cursor.width = x = vmsvga_fifo_read(s);
587 cursor.height = y = vmsvga_fifo_read(s);
588 vmsvga_fifo_read(s);
589 cursor.bpp = vmsvga_fifo_read(s);
Roland Dreierf2d928d2010-01-05 20:43:34 -0800590
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200591 args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
Andrzej Zaborowski9f810be2010-09-10 02:30:04 +0200592 if (SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
593 SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image)
594 goto badcmd;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200595
596 len -= args;
597 if (len < 0)
598 goto rewind;
Roland Dreierf2d928d2010-01-05 20:43:34 -0800599
thsd34cab92007-04-02 01:10:46 +0000600 for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args ++)
balrogff9cf2c2008-07-16 04:45:12 +0000601 cursor.mask[args] = vmsvga_fifo_read_raw(s);
thsd34cab92007-04-02 01:10:46 +0000602 for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args ++)
balrogff9cf2c2008-07-16 04:45:12 +0000603 cursor.image[args] = vmsvga_fifo_read_raw(s);
thsd34cab92007-04-02 01:10:46 +0000604#ifdef HW_MOUSE_ACCEL
605 vmsvga_cursor_define(s, &cursor);
606 break;
607#else
608 args = 0;
609 goto badcmd;
610#endif
611
612 /*
613 * Other commands that we at least know the number of arguments
614 * for so we can avoid FIFO desync if driver uses them illegally.
615 */
616 case SVGA_CMD_DEFINE_ALPHA_CURSOR:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200617 len -= 6;
618 if (len < 0)
619 goto rewind;
620
thsd34cab92007-04-02 01:10:46 +0000621 vmsvga_fifo_read(s);
622 vmsvga_fifo_read(s);
623 vmsvga_fifo_read(s);
624 x = vmsvga_fifo_read(s);
625 y = vmsvga_fifo_read(s);
626 args = x * y;
627 goto badcmd;
628 case SVGA_CMD_RECT_ROP_FILL:
629 args = 6;
630 goto badcmd;
631 case SVGA_CMD_RECT_ROP_COPY:
632 args = 7;
633 goto badcmd;
634 case SVGA_CMD_DRAW_GLYPH_CLIPPED:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200635 len -= 4;
636 if (len < 0)
637 goto rewind;
638
thsd34cab92007-04-02 01:10:46 +0000639 vmsvga_fifo_read(s);
640 vmsvga_fifo_read(s);
641 args = 7 + (vmsvga_fifo_read(s) >> 2);
642 goto badcmd;
643 case SVGA_CMD_SURFACE_ALPHA_BLEND:
644 args = 12;
645 goto badcmd;
646
647 /*
648 * Other commands that are not listed as depending on any
649 * CAPABILITIES bits, but are not described in the README either.
650 */
651 case SVGA_CMD_SURFACE_FILL:
652 case SVGA_CMD_SURFACE_COPY:
653 case SVGA_CMD_FRONT_ROP_FILL:
654 case SVGA_CMD_FENCE:
655 case SVGA_CMD_INVALID_CMD:
656 break; /* Nop */
657
658 default:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200659 args = 0;
thsd34cab92007-04-02 01:10:46 +0000660 badcmd:
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200661 len -= args;
662 if (len < 0)
663 goto rewind;
thsd34cab92007-04-02 01:10:46 +0000664 while (args --)
665 vmsvga_fifo_read(s);
666 printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
667 __FUNCTION__, cmd);
668 break;
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200669
670 rewind:
671 s->cmd->stop = cmd_start;
672 break;
thsd34cab92007-04-02 01:10:46 +0000673 }
Andrzej Zaborowski4dedc072010-09-10 02:23:31 +0200674 }
thsd34cab92007-04-02 01:10:46 +0000675
676 s->syncing = 0;
677}
678
679static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
680{
Juan Quintela467d44b2009-10-14 17:49:08 +0200681 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +0000682 return s->index;
683}
684
685static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
686{
Juan Quintela467d44b2009-10-14 17:49:08 +0200687 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +0000688 s->index = index;
689}
690
691static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
692{
693 uint32_t caps;
Juan Quintela467d44b2009-10-14 17:49:08 +0200694 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +0000695 switch (s->index) {
696 case SVGA_REG_ID:
697 return s->svgaid;
698
699 case SVGA_REG_ENABLE:
700 return s->enable;
701
702 case SVGA_REG_WIDTH:
703 return s->width;
704
705 case SVGA_REG_HEIGHT:
706 return s->height;
707
708 case SVGA_REG_MAX_WIDTH:
709 return SVGA_MAX_WIDTH;
710
711 case SVGA_REG_MAX_HEIGHT:
balrogf707cfb2007-05-13 13:26:49 +0000712 return SVGA_MAX_HEIGHT;
thsd34cab92007-04-02 01:10:46 +0000713
714 case SVGA_REG_DEPTH:
715 return s->depth;
716
717 case SVGA_REG_BITS_PER_PIXEL:
718 return (s->depth + 7) & ~7;
719
720 case SVGA_REG_PSEUDOCOLOR:
721 return 0x0;
722
723 case SVGA_REG_RED_MASK:
724 return s->wred;
725 case SVGA_REG_GREEN_MASK:
726 return s->wgreen;
727 case SVGA_REG_BLUE_MASK:
728 return s->wblue;
729
730 case SVGA_REG_BYTES_PER_LINE:
731 return ((s->depth + 7) >> 3) * s->new_width;
732
Avi Kivity7b619b92011-08-08 16:08:56 +0300733 case SVGA_REG_FB_START: {
734 struct pci_vmsvga_state_s *pci_vmsvga
735 = container_of(s, struct pci_vmsvga_state_s, chip);
736 return pci_get_bar_addr(&pci_vmsvga->card, 1);
737 }
thsd34cab92007-04-02 01:10:46 +0000738
739 case SVGA_REG_FB_OFFSET:
740 return 0x0;
741
742 case SVGA_REG_VRAM_SIZE:
Dave Airlief351d052009-12-18 08:08:06 +1000743 return s->vga.vram_size;
thsd34cab92007-04-02 01:10:46 +0000744
745 case SVGA_REG_FB_SIZE:
746 return s->fb_size;
747
748 case SVGA_REG_CAPABILITIES:
749 caps = SVGA_CAP_NONE;
750#ifdef HW_RECT_ACCEL
751 caps |= SVGA_CAP_RECT_COPY;
752#endif
753#ifdef HW_FILL_ACCEL
754 caps |= SVGA_CAP_RECT_FILL;
755#endif
756#ifdef HW_MOUSE_ACCEL
Avi Kivity4e12cd92009-05-03 22:25:16 +0300757 if (s->vga.ds->mouse_set)
thsd34cab92007-04-02 01:10:46 +0000758 caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
759 SVGA_CAP_CURSOR_BYPASS;
760#endif
761 return caps;
762
Avi Kivityb1950432011-08-08 16:08:57 +0300763 case SVGA_REG_MEM_START: {
764 struct pci_vmsvga_state_s *pci_vmsvga
765 = container_of(s, struct pci_vmsvga_state_s, chip);
766 return pci_get_bar_addr(&pci_vmsvga->card, 2);
767 }
thsd34cab92007-04-02 01:10:46 +0000768
769 case SVGA_REG_MEM_SIZE:
Dave Airlief351d052009-12-18 08:08:06 +1000770 return s->fifo_size;
thsd34cab92007-04-02 01:10:46 +0000771
772 case SVGA_REG_CONFIG_DONE:
773 return s->config;
774
775 case SVGA_REG_SYNC:
776 case SVGA_REG_BUSY:
777 return s->syncing;
778
779 case SVGA_REG_GUEST_ID:
780 return s->guest;
781
782 case SVGA_REG_CURSOR_ID:
783 return s->cursor.id;
784
785 case SVGA_REG_CURSOR_X:
786 return s->cursor.x;
787
788 case SVGA_REG_CURSOR_Y:
789 return s->cursor.x;
790
791 case SVGA_REG_CURSOR_ON:
792 return s->cursor.on;
793
794 case SVGA_REG_HOST_BITS_PER_PIXEL:
795 return (s->depth + 7) & ~7;
796
797 case SVGA_REG_SCRATCH_SIZE:
798 return s->scratch_size;
799
800 case SVGA_REG_MEM_REGS:
801 case SVGA_REG_NUM_DISPLAYS:
802 case SVGA_REG_PITCHLOCK:
803 case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
804 return 0;
805
806 default:
807 if (s->index >= SVGA_SCRATCH_BASE &&
808 s->index < SVGA_SCRATCH_BASE + s->scratch_size)
809 return s->scratch[s->index - SVGA_SCRATCH_BASE];
810 printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
811 }
812
813 return 0;
814}
815
816static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
817{
Juan Quintela467d44b2009-10-14 17:49:08 +0200818 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +0000819 switch (s->index) {
820 case SVGA_REG_ID:
821 if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0)
822 s->svgaid = value;
823 break;
824
825 case SVGA_REG_ENABLE:
balrogf707cfb2007-05-13 13:26:49 +0000826 s->enable = value;
827 s->config &= !!value;
thsd34cab92007-04-02 01:10:46 +0000828 s->width = -1;
829 s->height = -1;
830 s->invalidated = 1;
Avi Kivity4e12cd92009-05-03 22:25:16 +0300831 s->vga.invalidate(&s->vga);
Anthony Liguorib5cc6e32009-12-18 08:08:10 +1000832 if (s->enable) {
Andrzej Zaborowski9f810be2010-09-10 02:30:04 +0200833 s->fb_size = ((s->depth + 7) >> 3) * s->new_width * s->new_height;
834 vga_dirty_log_stop(&s->vga);
835 } else {
836 vga_dirty_log_start(&s->vga);
837 }
thsd34cab92007-04-02 01:10:46 +0000838 break;
839
840 case SVGA_REG_WIDTH:
841 s->new_width = value;
842 s->invalidated = 1;
843 break;
844
845 case SVGA_REG_HEIGHT:
846 s->new_height = value;
847 s->invalidated = 1;
848 break;
849
850 case SVGA_REG_DEPTH:
851 case SVGA_REG_BITS_PER_PIXEL:
852 if (value != s->depth) {
853 printf("%s: Bad colour depth: %i bits\n", __FUNCTION__, value);
854 s->config = 0;
855 }
856 break;
857
858 case SVGA_REG_CONFIG_DONE:
859 if (value) {
Dave Airlief351d052009-12-18 08:08:06 +1000860 s->fifo = (uint32_t *) s->fifo_ptr;
thsd34cab92007-04-02 01:10:46 +0000861 /* Check range and alignment. */
balrogff9cf2c2008-07-16 04:45:12 +0000862 if ((CMD(min) | CMD(max) |
863 CMD(next_cmd) | CMD(stop)) & 3)
thsd34cab92007-04-02 01:10:46 +0000864 break;
balrogff9cf2c2008-07-16 04:45:12 +0000865 if (CMD(min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo)
thsd34cab92007-04-02 01:10:46 +0000866 break;
balrogff9cf2c2008-07-16 04:45:12 +0000867 if (CMD(max) > SVGA_FIFO_SIZE)
thsd34cab92007-04-02 01:10:46 +0000868 break;
balrogff9cf2c2008-07-16 04:45:12 +0000869 if (CMD(max) < CMD(min) + 10 * 1024)
thsd34cab92007-04-02 01:10:46 +0000870 break;
871 }
balrogf707cfb2007-05-13 13:26:49 +0000872 s->config = !!value;
thsd34cab92007-04-02 01:10:46 +0000873 break;
874
875 case SVGA_REG_SYNC:
876 s->syncing = 1;
877 vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
878 break;
879
880 case SVGA_REG_GUEST_ID:
881 s->guest = value;
882#ifdef VERBOSE
883 if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
malcb1503cd2008-12-22 20:33:55 +0000884 ARRAY_SIZE(vmsvga_guest_id))
thsd34cab92007-04-02 01:10:46 +0000885 printf("%s: guest runs %s.\n", __FUNCTION__,
886 vmsvga_guest_id[value - GUEST_OS_BASE]);
887#endif
888 break;
889
890 case SVGA_REG_CURSOR_ID:
891 s->cursor.id = value;
892 break;
893
894 case SVGA_REG_CURSOR_X:
895 s->cursor.x = value;
896 break;
897
898 case SVGA_REG_CURSOR_Y:
899 s->cursor.y = value;
900 break;
901
902 case SVGA_REG_CURSOR_ON:
903 s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
904 s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
905#ifdef HW_MOUSE_ACCEL
Avi Kivity4e12cd92009-05-03 22:25:16 +0300906 if (s->vga.ds->mouse_set && value <= SVGA_CURSOR_ON_SHOW)
907 s->vga.ds->mouse_set(s->cursor.x, s->cursor.y, s->cursor.on);
thsd34cab92007-04-02 01:10:46 +0000908#endif
909 break;
910
911 case SVGA_REG_MEM_REGS:
912 case SVGA_REG_NUM_DISPLAYS:
913 case SVGA_REG_PITCHLOCK:
914 case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
915 break;
916
917 default:
918 if (s->index >= SVGA_SCRATCH_BASE &&
919 s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
920 s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
921 break;
922 }
923 printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
924 }
925}
926
927static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
928{
929 printf("%s: what are we supposed to return?\n", __FUNCTION__);
930 return 0xcafe;
931}
932
933static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
934{
935 printf("%s: what are we supposed to do with (%08x)?\n",
936 __FUNCTION__, data);
937}
938
939static inline void vmsvga_size(struct vmsvga_state_s *s)
940{
941 if (s->new_width != s->width || s->new_height != s->height) {
942 s->width = s->new_width;
943 s->height = s->new_height;
Avi Kivity4e12cd92009-05-03 22:25:16 +0300944 qemu_console_resize(s->vga.ds, s->width, s->height);
thsd34cab92007-04-02 01:10:46 +0000945 s->invalidated = 1;
946 }
947}
948
949static void vmsvga_update_display(void *opaque)
950{
Juan Quintela467d44b2009-10-14 17:49:08 +0200951 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +0000952 if (!s->enable) {
Avi Kivity4e12cd92009-05-03 22:25:16 +0300953 s->vga.update(&s->vga);
thsd34cab92007-04-02 01:10:46 +0000954 return;
955 }
956
957 vmsvga_size(s);
958
959 vmsvga_fifo_run(s);
960 vmsvga_update_rect_flush(s);
961
962 /*
963 * Is it more efficient to look at vram VGA-dirty bits or wait
964 * for the driver to issue SVGA_CMD_UPDATE?
965 */
966 if (s->invalidated) {
967 s->invalidated = 0;
968 vmsvga_update_screen(s);
969 }
970}
971
Jan Kiszka8a9501b2011-08-22 19:12:08 +0200972static void vmsvga_reset(DeviceState *dev)
thsd34cab92007-04-02 01:10:46 +0000973{
Jan Kiszka8a9501b2011-08-22 19:12:08 +0200974 struct pci_vmsvga_state_s *pci =
975 DO_UPCAST(struct pci_vmsvga_state_s, card.qdev, dev);
976 struct vmsvga_state_s *s = &pci->chip;
977
thsd34cab92007-04-02 01:10:46 +0000978 s->index = 0;
979 s->enable = 0;
980 s->config = 0;
981 s->width = -1;
982 s->height = -1;
983 s->svgaid = SVGA_ID;
thsd34cab92007-04-02 01:10:46 +0000984 s->cursor.on = 0;
985 s->redraw_fifo_first = 0;
986 s->redraw_fifo_last = 0;
thsd34cab92007-04-02 01:10:46 +0000987 s->syncing = 0;
Anthony Liguorib5cc6e32009-12-18 08:08:10 +1000988
989 vga_dirty_log_start(&s->vga);
thsd34cab92007-04-02 01:10:46 +0000990}
991
992static void vmsvga_invalidate_display(void *opaque)
993{
Juan Quintela467d44b2009-10-14 17:49:08 +0200994 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +0000995 if (!s->enable) {
Avi Kivity4e12cd92009-05-03 22:25:16 +0300996 s->vga.invalidate(&s->vga);
thsd34cab92007-04-02 01:10:46 +0000997 return;
998 }
999
1000 s->invalidated = 1;
1001}
1002
balrogf707cfb2007-05-13 13:26:49 +00001003/* save the vga display in a PPM image even if no display is
1004 available */
Luiz Capitulinod7098132012-05-21 16:41:37 -03001005static void vmsvga_screen_dump(void *opaque, const char *filename, bool cswitch,
1006 Error **errp)
thsd34cab92007-04-02 01:10:46 +00001007{
Juan Quintela467d44b2009-10-14 17:49:08 +02001008 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +00001009 if (!s->enable) {
Luiz Capitulinod7098132012-05-21 16:41:37 -03001010 s->vga.screen_dump(&s->vga, filename, cswitch, errp);
thsd34cab92007-04-02 01:10:46 +00001011 return;
1012 }
1013
balrogf707cfb2007-05-13 13:26:49 +00001014 if (s->depth == 32) {
aliguorie07d6302009-01-16 19:07:10 +00001015 DisplaySurface *ds = qemu_create_displaysurface_from(s->width,
Avi Kivity4e12cd92009-05-03 22:25:16 +03001016 s->height, 32, ds_get_linesize(s->vga.ds), s->vga.vram_ptr);
Luiz Capitulinod6631742012-05-24 10:42:25 -03001017 ppm_save(filename, ds, errp);
Anthony Liguori7267c092011-08-20 22:09:37 -05001018 g_free(ds);
balrogf707cfb2007-05-13 13:26:49 +00001019 }
thsd34cab92007-04-02 01:10:46 +00001020}
1021
Anthony Liguoric227f092009-10-01 16:12:16 -05001022static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
balrog4d3b6f62008-02-10 16:33:14 +00001023{
Juan Quintela467d44b2009-10-14 17:49:08 +02001024 struct vmsvga_state_s *s = opaque;
balrog4d3b6f62008-02-10 16:33:14 +00001025
Avi Kivity4e12cd92009-05-03 22:25:16 +03001026 if (s->vga.text_update)
1027 s->vga.text_update(&s->vga, chardata);
balrog4d3b6f62008-02-10 16:33:14 +00001028}
1029
Juan Quintelabacbe282009-10-14 19:30:22 +02001030static int vmsvga_post_load(void *opaque, int version_id)
thsd34cab92007-04-02 01:10:46 +00001031{
Juan Quintelabacbe282009-10-14 19:30:22 +02001032 struct vmsvga_state_s *s = opaque;
thsd34cab92007-04-02 01:10:46 +00001033
1034 s->invalidated = 1;
1035 if (s->config)
Dave Airlief351d052009-12-18 08:08:06 +10001036 s->fifo = (uint32_t *) s->fifo_ptr;
thsd34cab92007-04-02 01:10:46 +00001037
1038 return 0;
1039}
1040
Blue Swirld05ac8f2009-12-04 20:44:44 +00001041static const VMStateDescription vmstate_vmware_vga_internal = {
Juan Quintelabacbe282009-10-14 19:30:22 +02001042 .name = "vmware_vga_internal",
1043 .version_id = 0,
1044 .minimum_version_id = 0,
1045 .minimum_version_id_old = 0,
1046 .post_load = vmsvga_post_load,
1047 .fields = (VMStateField []) {
1048 VMSTATE_INT32_EQUAL(depth, struct vmsvga_state_s),
1049 VMSTATE_INT32(enable, struct vmsvga_state_s),
1050 VMSTATE_INT32(config, struct vmsvga_state_s),
1051 VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
1052 VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
1053 VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
1054 VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
1055 VMSTATE_INT32(index, struct vmsvga_state_s),
1056 VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
1057 scratch_size, 0, vmstate_info_uint32, uint32_t),
1058 VMSTATE_INT32(new_width, struct vmsvga_state_s),
1059 VMSTATE_INT32(new_height, struct vmsvga_state_s),
1060 VMSTATE_UINT32(guest, struct vmsvga_state_s),
1061 VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
1062 VMSTATE_INT32(syncing, struct vmsvga_state_s),
1063 VMSTATE_INT32(fb_size, struct vmsvga_state_s),
1064 VMSTATE_END_OF_LIST()
1065 }
1066};
1067
Blue Swirld05ac8f2009-12-04 20:44:44 +00001068static const VMStateDescription vmstate_vmware_vga = {
Juan Quintelabacbe282009-10-14 19:30:22 +02001069 .name = "vmware_vga",
1070 .version_id = 0,
1071 .minimum_version_id = 0,
1072 .minimum_version_id_old = 0,
1073 .fields = (VMStateField []) {
1074 VMSTATE_PCI_DEVICE(card, struct pci_vmsvga_state_s),
1075 VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
1076 vmstate_vmware_vga_internal, struct vmsvga_state_s),
1077 VMSTATE_END_OF_LIST()
1078 }
1079};
1080
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001081static void vmsvga_init(struct vmsvga_state_s *s,
Richard Henderson0a039dc2011-08-16 08:27:39 -07001082 MemoryRegion *address_space, MemoryRegion *io)
thsd34cab92007-04-02 01:10:46 +00001083{
thsd34cab92007-04-02 01:10:46 +00001084 s->scratch_size = SVGA_SCRATCH_SIZE;
Anthony Liguori7267c092011-08-20 22:09:37 -05001085 s->scratch = g_malloc(s->scratch_size * 4);
thsd34cab92007-04-02 01:10:46 +00001086
Anthony Liguoria6109ff2009-12-18 08:08:09 +10001087 s->vga.ds = graphic_console_init(vmsvga_update_display,
1088 vmsvga_invalidate_display,
1089 vmsvga_screen_dump,
1090 vmsvga_text_update, s);
1091
Andrzej Zaborowski4445b0a2009-08-23 19:00:58 +02001092
Dave Airlief351d052009-12-18 08:08:06 +10001093 s->fifo_size = SVGA_FIFO_SIZE;
Avi Kivityc5705a72011-12-20 15:59:12 +02001094 memory_region_init_ram(&s->fifo_ram, "vmsvga.fifo", s->fifo_size);
1095 vmstate_register_ram_global(&s->fifo_ram);
Avi Kivityb1950432011-08-08 16:08:57 +03001096 s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
Dave Airlief351d052009-12-18 08:08:06 +10001097
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001098 vga_common_init(&s->vga);
Richard Henderson0a039dc2011-08-16 08:27:39 -07001099 vga_init(&s->vga, address_space, io, true);
Alex Williamson0be71e32010-06-25 11:09:07 -06001100 vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
balroge93a5f42008-07-16 04:31:20 +00001101
Jan Kiszka8a9501b2011-08-22 19:12:08 +02001102 s->depth = ds_get_bits_per_pixel(s->vga.ds);
1103 s->bypp = ds_get_bytes_per_pixel(s->vga.ds);
1104 switch (s->depth) {
1105 case 8:
1106 s->wred = 0x00000007;
1107 s->wgreen = 0x00000038;
1108 s->wblue = 0x000000c0;
1109 break;
1110 case 15:
1111 s->wred = 0x0000001f;
1112 s->wgreen = 0x000003e0;
1113 s->wblue = 0x00007c00;
1114 break;
1115 case 16:
1116 s->wred = 0x0000001f;
1117 s->wgreen = 0x000007e0;
1118 s->wblue = 0x0000f800;
1119 break;
1120 case 24:
1121 s->wred = 0x00ff0000;
1122 s->wgreen = 0x0000ff00;
1123 s->wblue = 0x000000ff;
1124 break;
1125 case 32:
1126 s->wred = 0x00ff0000;
1127 s->wgreen = 0x0000ff00;
1128 s->wblue = 0x000000ff;
1129 break;
1130 }
thsd34cab92007-04-02 01:10:46 +00001131}
1132
Avi Kivitya8170e52012-10-23 12:30:10 +02001133static uint64_t vmsvga_io_read(void *opaque, hwaddr addr,
Avi Kivityb1950432011-08-08 16:08:57 +03001134 unsigned size)
balrog1492a3c2008-01-14 01:52:52 +00001135{
Avi Kivityb1950432011-08-08 16:08:57 +03001136 struct vmsvga_state_s *s = opaque;
balrog1492a3c2008-01-14 01:52:52 +00001137
Avi Kivityb1950432011-08-08 16:08:57 +03001138 switch (addr) {
1139 case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
1140 case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
1141 case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
1142 default: return -1u;
1143 }
balrog1492a3c2008-01-14 01:52:52 +00001144}
1145
Avi Kivitya8170e52012-10-23 12:30:10 +02001146static void vmsvga_io_write(void *opaque, hwaddr addr,
Avi Kivityb1950432011-08-08 16:08:57 +03001147 uint64_t data, unsigned size)
balrog3016d802008-03-06 20:28:49 +00001148{
Avi Kivityb1950432011-08-08 16:08:57 +03001149 struct vmsvga_state_s *s = opaque;
balrog3016d802008-03-06 20:28:49 +00001150
Avi Kivityb1950432011-08-08 16:08:57 +03001151 switch (addr) {
1152 case SVGA_IO_MUL * SVGA_INDEX_PORT:
Blue Swirl0ed8b6f2012-07-08 06:56:53 +00001153 vmsvga_index_write(s, addr, data);
1154 break;
Avi Kivityb1950432011-08-08 16:08:57 +03001155 case SVGA_IO_MUL * SVGA_VALUE_PORT:
Blue Swirl0ed8b6f2012-07-08 06:56:53 +00001156 vmsvga_value_write(s, addr, data);
1157 break;
Avi Kivityb1950432011-08-08 16:08:57 +03001158 case SVGA_IO_MUL * SVGA_BIOS_PORT:
Blue Swirl0ed8b6f2012-07-08 06:56:53 +00001159 vmsvga_bios_write(s, addr, data);
1160 break;
Avi Kivityb1950432011-08-08 16:08:57 +03001161 }
balrog3016d802008-03-06 20:28:49 +00001162}
1163
Avi Kivityb1950432011-08-08 16:08:57 +03001164static const MemoryRegionOps vmsvga_io_ops = {
1165 .read = vmsvga_io_read,
1166 .write = vmsvga_io_write,
1167 .endianness = DEVICE_LITTLE_ENDIAN,
1168 .valid = {
1169 .min_access_size = 4,
1170 .max_access_size = 4,
1171 },
1172};
Dave Airlief351d052009-12-18 08:08:06 +10001173
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001174static int pci_vmsvga_initfn(PCIDevice *dev)
thsd34cab92007-04-02 01:10:46 +00001175{
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001176 struct pci_vmsvga_state_s *s =
1177 DO_UPCAST(struct pci_vmsvga_state_s, card, dev);
Avi Kivityb1950432011-08-08 16:08:57 +03001178 MemoryRegion *iomem;
1179
Avi Kivityb1950432011-08-08 16:08:57 +03001180 iomem = &s->chip.vga.vram;
Avi Kivityb1950432011-08-08 16:08:57 +03001181
Michael S. Tsirkin3fa0f952009-12-10 18:51:49 +02001182 s->card.config[PCI_CACHE_LINE_SIZE] = 0x08; /* Cache line size */
1183 s->card.config[PCI_LATENCY_TIMER] = 0x40; /* Latency timer */
Michael S. Tsirkin3fa0f952009-12-10 18:51:49 +02001184 s->card.config[PCI_INTERRUPT_LINE] = 0xff; /* End */
thsd34cab92007-04-02 01:10:46 +00001185
Avi Kivityb1950432011-08-08 16:08:57 +03001186 memory_region_init_io(&s->io_bar, &vmsvga_io_ops, &s->chip,
1187 "vmsvga-io", 0x10);
Jan Kiszkabd8f2f52012-08-23 13:02:33 +02001188 memory_region_set_flush_coalesced(&s->io_bar);
Avi Kivitye824b2c2011-08-08 16:09:31 +03001189 pci_register_bar(&s->card, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
Dave Airlief351d052009-12-18 08:08:06 +10001190
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001191 vmsvga_init(&s->chip, pci_address_space(dev),
Richard Henderson0a039dc2011-08-16 08:27:39 -07001192 pci_address_space_io(dev));
thsd34cab92007-04-02 01:10:46 +00001193
Avi Kivitye824b2c2011-08-08 16:09:31 +03001194 pci_register_bar(&s->card, 1, PCI_BASE_ADDRESS_MEM_PREFETCH, iomem);
1195 pci_register_bar(&s->card, 2, PCI_BASE_ADDRESS_MEM_PREFETCH,
1196 &s->chip.fifo_ram);
Avi Kivityb1950432011-08-08 16:08:57 +03001197
Gerd Hoffmann281a26b2010-11-17 12:06:44 +01001198 if (!dev->rom_bar) {
1199 /* compatibility with pc-0.13 and older */
Avi Kivitybe20f9e2011-08-15 17:17:37 +03001200 vga_init_vbe(&s->chip.vga, pci_address_space(dev));
Gerd Hoffmann281a26b2010-11-17 12:06:44 +01001201 }
1202
Gerd Hoffmann81a322d2009-08-14 10:36:05 +02001203 return 0;
thsd34cab92007-04-02 01:10:46 +00001204}
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001205
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001206static Property vga_vmware_properties[] = {
1207 DEFINE_PROP_UINT32("vgamem_mb", struct pci_vmsvga_state_s,
Gerd Hoffmann9e56edc2012-06-11 10:42:53 +02001208 chip.vga.vram_size_mb, 16),
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001209 DEFINE_PROP_END_OF_LIST(),
1210};
1211
Anthony Liguori40021f02011-12-04 12:22:06 -06001212static void vmsvga_class_init(ObjectClass *klass, void *data)
1213{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001214 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06001215 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Isaku Yamahata310faae2011-05-25 10:58:04 +09001216
Anthony Liguori40021f02011-12-04 12:22:06 -06001217 k->no_hotplug = 1;
1218 k->init = pci_vmsvga_initfn;
1219 k->romfile = "vgabios-vmware.bin";
1220 k->vendor_id = PCI_VENDOR_ID_VMWARE;
1221 k->device_id = SVGA_PCI_DEVICE_ID;
1222 k->class_id = PCI_CLASS_DISPLAY_VGA;
1223 k->subsystem_vendor_id = PCI_VENDOR_ID_VMWARE;
1224 k->subsystem_id = SVGA_PCI_DEVICE_ID;
Anthony Liguori39bffca2011-12-07 21:34:16 -06001225 dc->reset = vmsvga_reset;
1226 dc->vmsd = &vmstate_vmware_vga;
Gerd Hoffmann4a1e2442012-05-24 09:59:44 +02001227 dc->props = vga_vmware_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -06001228}
1229
Anthony Liguori39bffca2011-12-07 21:34:16 -06001230static TypeInfo vmsvga_info = {
1231 .name = "vmware-svga",
1232 .parent = TYPE_PCI_DEVICE,
1233 .instance_size = sizeof(struct pci_vmsvga_state_s),
1234 .class_init = vmsvga_class_init,
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001235};
1236
Andreas Färber83f7d432012-02-09 15:20:55 +01001237static void vmsvga_register_types(void)
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001238{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001239 type_register_static(&vmsvga_info);
Gerd Hoffmanna414c302009-07-28 18:18:00 +02001240}
Andreas Färber83f7d432012-02-09 15:20:55 +01001241
1242type_init(vmsvga_register_types)