Statistics
| Branch: | Revision:

root / hw / vmware_vga.c @ 1e04d4d6

History | View | Annotate | Download (36.1 kB)

1 d34cab9f ths
/*
2 d34cab9f ths
 * QEMU VMware-SVGA "chipset".
3 d34cab9f ths
 *
4 d34cab9f ths
 * Copyright (c) 2007 Andrzej Zaborowski  <balrog@zabor.org>
5 d34cab9f ths
 *
6 d34cab9f ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 d34cab9f ths
 * of this software and associated documentation files (the "Software"), to deal
8 d34cab9f ths
 * in the Software without restriction, including without limitation the rights
9 d34cab9f ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 d34cab9f ths
 * copies of the Software, and to permit persons to whom the Software is
11 d34cab9f ths
 * furnished to do so, subject to the following conditions:
12 d34cab9f ths
 *
13 d34cab9f ths
 * The above copyright notice and this permission notice shall be included in
14 d34cab9f ths
 * all copies or substantial portions of the Software.
15 d34cab9f ths
 *
16 d34cab9f ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 d34cab9f ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 d34cab9f ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 d34cab9f ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 d34cab9f ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 d34cab9f ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 d34cab9f ths
 * THE SOFTWARE.
23 d34cab9f ths
 */
24 87ecb68b pbrook
#include "hw.h"
25 b3c3f123 Dave Airlie
#include "loader.h"
26 87ecb68b pbrook
#include "console.h"
27 87ecb68b pbrook
#include "pci.h"
28 18e08a55 Michael S. Tsirkin
#include "vmware_vga.h"
29 d34cab9f ths
30 d34cab9f ths
#define VERBOSE
31 d34cab9f ths
#undef DIRECT_VRAM
32 d34cab9f ths
#define HW_RECT_ACCEL
33 d34cab9f ths
#define HW_FILL_ACCEL
34 d34cab9f ths
#define HW_MOUSE_ACCEL
35 d34cab9f ths
36 d34cab9f ths
# include "vga_int.h"
37 d34cab9f ths
38 d34cab9f ths
struct vmsvga_state_s {
39 4e12cd94 Avi Kivity
    VGACommonState vga;
40 d34cab9f ths
41 d34cab9f ths
    int width;
42 d34cab9f ths
    int height;
43 d34cab9f ths
    int invalidated;
44 d34cab9f ths
    int depth;
45 d34cab9f ths
    int bypp;
46 d34cab9f ths
    int enable;
47 d34cab9f ths
    int config;
48 d34cab9f ths
    struct {
49 d34cab9f ths
        int id;
50 d34cab9f ths
        int x;
51 d34cab9f ths
        int y;
52 d34cab9f ths
        int on;
53 d34cab9f ths
    } cursor;
54 d34cab9f ths
55 d34cab9f ths
    int index;
56 d34cab9f ths
    int scratch_size;
57 d34cab9f ths
    uint32_t *scratch;
58 d34cab9f ths
    int new_width;
59 d34cab9f ths
    int new_height;
60 d34cab9f ths
    uint32_t guest;
61 d34cab9f ths
    uint32_t svgaid;
62 d34cab9f ths
    uint32_t wred;
63 d34cab9f ths
    uint32_t wgreen;
64 d34cab9f ths
    uint32_t wblue;
65 d34cab9f ths
    int syncing;
66 d34cab9f ths
    int fb_size;
67 d34cab9f ths
68 b1950430 Avi Kivity
    MemoryRegion fifo_ram;
69 f351d050 Dave Airlie
    uint8_t *fifo_ptr;
70 f351d050 Dave Airlie
    unsigned int fifo_size;
71 f351d050 Dave Airlie
72 d34cab9f ths
    union {
73 d34cab9f ths
        uint32_t *fifo;
74 d34cab9f ths
        struct __attribute__((__packed__)) {
75 d34cab9f ths
            uint32_t min;
76 d34cab9f ths
            uint32_t max;
77 d34cab9f ths
            uint32_t next_cmd;
78 d34cab9f ths
            uint32_t stop;
79 d34cab9f ths
            /* Add registers here when adding capabilities.  */
80 d34cab9f ths
            uint32_t fifo[0];
81 d34cab9f ths
        } *cmd;
82 d34cab9f ths
    };
83 d34cab9f ths
84 d34cab9f ths
#define REDRAW_FIFO_LEN        512
85 d34cab9f ths
    struct vmsvga_rect_s {
86 d34cab9f ths
        int x, y, w, h;
87 d34cab9f ths
    } redraw_fifo[REDRAW_FIFO_LEN];
88 d34cab9f ths
    int redraw_fifo_first, redraw_fifo_last;
89 d34cab9f ths
};
90 d34cab9f ths
91 d34cab9f ths
struct pci_vmsvga_state_s {
92 d34cab9f ths
    PCIDevice card;
93 d34cab9f ths
    struct vmsvga_state_s chip;
94 b1950430 Avi Kivity
    MemoryRegion io_bar;
95 d34cab9f ths
};
96 d34cab9f ths
97 d34cab9f ths
#define SVGA_MAGIC                0x900000UL
98 d34cab9f ths
#define SVGA_MAKE_ID(ver)        (SVGA_MAGIC << 8 | (ver))
99 d34cab9f ths
#define SVGA_ID_0                SVGA_MAKE_ID(0)
100 d34cab9f ths
#define SVGA_ID_1                SVGA_MAKE_ID(1)
101 d34cab9f ths
#define SVGA_ID_2                SVGA_MAKE_ID(2)
102 d34cab9f ths
103 d34cab9f ths
#define SVGA_LEGACY_BASE_PORT        0x4560
104 d34cab9f ths
#define SVGA_INDEX_PORT                0x0
105 d34cab9f ths
#define SVGA_VALUE_PORT                0x1
106 d34cab9f ths
#define SVGA_BIOS_PORT                0x2
107 d34cab9f ths
108 d34cab9f ths
#define SVGA_VERSION_2
109 d34cab9f ths
110 d34cab9f ths
#ifdef SVGA_VERSION_2
111 d34cab9f ths
# define SVGA_ID                SVGA_ID_2
112 d34cab9f ths
# define SVGA_IO_BASE                SVGA_LEGACY_BASE_PORT
113 d34cab9f ths
# define SVGA_IO_MUL                1
114 d34cab9f ths
# define SVGA_FIFO_SIZE                0x10000
115 d34cab9f ths
# define SVGA_PCI_DEVICE_ID        PCI_DEVICE_ID_VMWARE_SVGA2
116 d34cab9f ths
#else
117 d34cab9f ths
# define SVGA_ID                SVGA_ID_1
118 d34cab9f ths
# define SVGA_IO_BASE                SVGA_LEGACY_BASE_PORT
119 d34cab9f ths
# define SVGA_IO_MUL                4
120 d34cab9f ths
# define SVGA_FIFO_SIZE                0x10000
121 d34cab9f ths
# define SVGA_PCI_DEVICE_ID        PCI_DEVICE_ID_VMWARE_SVGA
122 d34cab9f ths
#endif
123 d34cab9f ths
124 d34cab9f ths
enum {
125 d34cab9f ths
    /* ID 0, 1 and 2 registers */
126 d34cab9f ths
    SVGA_REG_ID = 0,
127 d34cab9f ths
    SVGA_REG_ENABLE = 1,
128 d34cab9f ths
    SVGA_REG_WIDTH = 2,
129 d34cab9f ths
    SVGA_REG_HEIGHT = 3,
130 d34cab9f ths
    SVGA_REG_MAX_WIDTH = 4,
131 d34cab9f ths
    SVGA_REG_MAX_HEIGHT = 5,
132 d34cab9f ths
    SVGA_REG_DEPTH = 6,
133 d34cab9f ths
    SVGA_REG_BITS_PER_PIXEL = 7,        /* Current bpp in the guest */
134 d34cab9f ths
    SVGA_REG_PSEUDOCOLOR = 8,
135 d34cab9f ths
    SVGA_REG_RED_MASK = 9,
136 d34cab9f ths
    SVGA_REG_GREEN_MASK = 10,
137 d34cab9f ths
    SVGA_REG_BLUE_MASK = 11,
138 d34cab9f ths
    SVGA_REG_BYTES_PER_LINE = 12,
139 d34cab9f ths
    SVGA_REG_FB_START = 13,
140 d34cab9f ths
    SVGA_REG_FB_OFFSET = 14,
141 d34cab9f ths
    SVGA_REG_VRAM_SIZE = 15,
142 d34cab9f ths
    SVGA_REG_FB_SIZE = 16,
143 d34cab9f ths
144 d34cab9f ths
    /* ID 1 and 2 registers */
145 d34cab9f ths
    SVGA_REG_CAPABILITIES = 17,
146 d34cab9f ths
    SVGA_REG_MEM_START = 18,                /* Memory for command FIFO */
147 d34cab9f ths
    SVGA_REG_MEM_SIZE = 19,
148 d34cab9f ths
    SVGA_REG_CONFIG_DONE = 20,                /* Set when memory area configured */
149 d34cab9f ths
    SVGA_REG_SYNC = 21,                        /* Write to force synchronization */
150 d34cab9f ths
    SVGA_REG_BUSY = 22,                        /* Read to check if sync is done */
151 d34cab9f ths
    SVGA_REG_GUEST_ID = 23,                /* Set guest OS identifier */
152 d34cab9f ths
    SVGA_REG_CURSOR_ID = 24,                /* ID of cursor */
153 d34cab9f ths
    SVGA_REG_CURSOR_X = 25,                /* Set cursor X position */
154 d34cab9f ths
    SVGA_REG_CURSOR_Y = 26,                /* Set cursor Y position */
155 d34cab9f ths
    SVGA_REG_CURSOR_ON = 27,                /* Turn cursor on/off */
156 d34cab9f ths
    SVGA_REG_HOST_BITS_PER_PIXEL = 28,        /* Current bpp in the host */
157 d34cab9f ths
    SVGA_REG_SCRATCH_SIZE = 29,                /* Number of scratch registers */
158 d34cab9f ths
    SVGA_REG_MEM_REGS = 30,                /* Number of FIFO registers */
159 d34cab9f ths
    SVGA_REG_NUM_DISPLAYS = 31,                /* Number of guest displays */
160 d34cab9f ths
    SVGA_REG_PITCHLOCK = 32,                /* Fixed pitch for all modes */
161 d34cab9f ths
162 d34cab9f ths
    SVGA_PALETTE_BASE = 1024,                /* Base of SVGA color map */
163 d34cab9f ths
    SVGA_PALETTE_END  = SVGA_PALETTE_BASE + 767,
164 d34cab9f ths
    SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
165 d34cab9f ths
};
166 d34cab9f ths
167 d34cab9f ths
#define SVGA_CAP_NONE                        0
168 d34cab9f ths
#define SVGA_CAP_RECT_FILL                (1 << 0)
169 d34cab9f ths
#define SVGA_CAP_RECT_COPY                (1 << 1)
170 d34cab9f ths
#define SVGA_CAP_RECT_PAT_FILL                (1 << 2)
171 d34cab9f ths
#define SVGA_CAP_LEGACY_OFFSCREEN        (1 << 3)
172 d34cab9f ths
#define SVGA_CAP_RASTER_OP                (1 << 4)
173 d34cab9f ths
#define SVGA_CAP_CURSOR                        (1 << 5)
174 d34cab9f ths
#define SVGA_CAP_CURSOR_BYPASS                (1 << 6)
175 d34cab9f ths
#define SVGA_CAP_CURSOR_BYPASS_2        (1 << 7)
176 d34cab9f ths
#define SVGA_CAP_8BIT_EMULATION                (1 << 8)
177 d34cab9f ths
#define SVGA_CAP_ALPHA_CURSOR                (1 << 9)
178 d34cab9f ths
#define SVGA_CAP_GLYPH                        (1 << 10)
179 d34cab9f ths
#define SVGA_CAP_GLYPH_CLIPPING                (1 << 11)
180 d34cab9f ths
#define SVGA_CAP_OFFSCREEN_1                (1 << 12)
181 d34cab9f ths
#define SVGA_CAP_ALPHA_BLEND                (1 << 13)
182 d34cab9f ths
#define SVGA_CAP_3D                        (1 << 14)
183 d34cab9f ths
#define SVGA_CAP_EXTENDED_FIFO                (1 << 15)
184 d34cab9f ths
#define SVGA_CAP_MULTIMON                (1 << 16)
185 d34cab9f ths
#define SVGA_CAP_PITCHLOCK                (1 << 17)
186 d34cab9f ths
187 d34cab9f ths
/*
188 d34cab9f ths
 * FIFO offsets (seen as an array of 32-bit words)
189 d34cab9f ths
 */
190 d34cab9f ths
enum {
191 d34cab9f ths
    /*
192 d34cab9f ths
     * The original defined FIFO offsets
193 d34cab9f ths
     */
194 d34cab9f ths
    SVGA_FIFO_MIN = 0,
195 d34cab9f ths
    SVGA_FIFO_MAX,        /* The distance from MIN to MAX must be at least 10K */
196 d34cab9f ths
    SVGA_FIFO_NEXT_CMD,
197 d34cab9f ths
    SVGA_FIFO_STOP,
198 d34cab9f ths
199 d34cab9f ths
    /*
200 d34cab9f ths
     * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
201 d34cab9f ths
     */
202 d34cab9f ths
    SVGA_FIFO_CAPABILITIES = 4,
203 d34cab9f ths
    SVGA_FIFO_FLAGS,
204 d34cab9f ths
    SVGA_FIFO_FENCE,
205 d34cab9f ths
    SVGA_FIFO_3D_HWVERSION,
206 d34cab9f ths
    SVGA_FIFO_PITCHLOCK,
207 d34cab9f ths
};
208 d34cab9f ths
209 d34cab9f ths
#define SVGA_FIFO_CAP_NONE                0
210 d34cab9f ths
#define SVGA_FIFO_CAP_FENCE                (1 << 0)
211 d34cab9f ths
#define SVGA_FIFO_CAP_ACCELFRONT        (1 << 1)
212 d34cab9f ths
#define SVGA_FIFO_CAP_PITCHLOCK                (1 << 2)
213 d34cab9f ths
214 d34cab9f ths
#define SVGA_FIFO_FLAG_NONE                0
215 d34cab9f ths
#define SVGA_FIFO_FLAG_ACCELFRONT        (1 << 0)
216 d34cab9f ths
217 d34cab9f ths
/* These values can probably be changed arbitrarily.  */
218 d34cab9f ths
#define SVGA_SCRATCH_SIZE                0x8000
219 d34cab9f ths
#define SVGA_MAX_WIDTH                        2360
220 d34cab9f ths
#define SVGA_MAX_HEIGHT                        1770
221 d34cab9f ths
222 d34cab9f ths
#ifdef VERBOSE
223 d34cab9f ths
# define GUEST_OS_BASE                0x5001
224 d34cab9f ths
static const char *vmsvga_guest_id[] = {
225 f707cfba balrog
    [0x00] = "Dos",
226 f707cfba balrog
    [0x01] = "Windows 3.1",
227 f707cfba balrog
    [0x02] = "Windows 95",
228 f707cfba balrog
    [0x03] = "Windows 98",
229 f707cfba balrog
    [0x04] = "Windows ME",
230 f707cfba balrog
    [0x05] = "Windows NT",
231 f707cfba balrog
    [0x06] = "Windows 2000",
232 f707cfba balrog
    [0x07] = "Linux",
233 f707cfba balrog
    [0x08] = "OS/2",
234 511d2b14 blueswir1
    [0x09] = "an unknown OS",
235 f707cfba balrog
    [0x0a] = "BSD",
236 f707cfba balrog
    [0x0b] = "Whistler",
237 511d2b14 blueswir1
    [0x0c] = "an unknown OS",
238 511d2b14 blueswir1
    [0x0d] = "an unknown OS",
239 511d2b14 blueswir1
    [0x0e] = "an unknown OS",
240 511d2b14 blueswir1
    [0x0f] = "an unknown OS",
241 511d2b14 blueswir1
    [0x10] = "an unknown OS",
242 511d2b14 blueswir1
    [0x11] = "an unknown OS",
243 511d2b14 blueswir1
    [0x12] = "an unknown OS",
244 511d2b14 blueswir1
    [0x13] = "an unknown OS",
245 511d2b14 blueswir1
    [0x14] = "an unknown OS",
246 f707cfba balrog
    [0x15] = "Windows 2003",
247 d34cab9f ths
};
248 d34cab9f ths
#endif
249 d34cab9f ths
250 d34cab9f ths
enum {
251 d34cab9f ths
    SVGA_CMD_INVALID_CMD = 0,
252 d34cab9f ths
    SVGA_CMD_UPDATE = 1,
253 d34cab9f ths
    SVGA_CMD_RECT_FILL = 2,
254 d34cab9f ths
    SVGA_CMD_RECT_COPY = 3,
255 d34cab9f ths
    SVGA_CMD_DEFINE_BITMAP = 4,
256 d34cab9f ths
    SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
257 d34cab9f ths
    SVGA_CMD_DEFINE_PIXMAP = 6,
258 d34cab9f ths
    SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
259 d34cab9f ths
    SVGA_CMD_RECT_BITMAP_FILL = 8,
260 d34cab9f ths
    SVGA_CMD_RECT_PIXMAP_FILL = 9,
261 d34cab9f ths
    SVGA_CMD_RECT_BITMAP_COPY = 10,
262 d34cab9f ths
    SVGA_CMD_RECT_PIXMAP_COPY = 11,
263 d34cab9f ths
    SVGA_CMD_FREE_OBJECT = 12,
264 d34cab9f ths
    SVGA_CMD_RECT_ROP_FILL = 13,
265 d34cab9f ths
    SVGA_CMD_RECT_ROP_COPY = 14,
266 d34cab9f ths
    SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
267 d34cab9f ths
    SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
268 d34cab9f ths
    SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
269 d34cab9f ths
    SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
270 d34cab9f ths
    SVGA_CMD_DEFINE_CURSOR = 19,
271 d34cab9f ths
    SVGA_CMD_DISPLAY_CURSOR = 20,
272 d34cab9f ths
    SVGA_CMD_MOVE_CURSOR = 21,
273 d34cab9f ths
    SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
274 d34cab9f ths
    SVGA_CMD_DRAW_GLYPH = 23,
275 d34cab9f ths
    SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
276 d34cab9f ths
    SVGA_CMD_UPDATE_VERBOSE = 25,
277 d34cab9f ths
    SVGA_CMD_SURFACE_FILL = 26,
278 d34cab9f ths
    SVGA_CMD_SURFACE_COPY = 27,
279 d34cab9f ths
    SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
280 d34cab9f ths
    SVGA_CMD_FRONT_ROP_FILL = 29,
281 d34cab9f ths
    SVGA_CMD_FENCE = 30,
282 d34cab9f ths
};
283 d34cab9f ths
284 d34cab9f ths
/* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
285 d34cab9f ths
enum {
286 d34cab9f ths
    SVGA_CURSOR_ON_HIDE = 0,
287 d34cab9f ths
    SVGA_CURSOR_ON_SHOW = 1,
288 d34cab9f ths
    SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
289 d34cab9f ths
    SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
290 d34cab9f ths
};
291 d34cab9f ths
292 d34cab9f ths
static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
293 d34cab9f ths
                int x, int y, int w, int h)
294 d34cab9f ths
{
295 d34cab9f ths
#ifndef DIRECT_VRAM
296 a8fbaf96 balrog
    int line;
297 a8fbaf96 balrog
    int bypl;
298 a8fbaf96 balrog
    int width;
299 a8fbaf96 balrog
    int start;
300 a8fbaf96 balrog
    uint8_t *src;
301 a8fbaf96 balrog
    uint8_t *dst;
302 a8fbaf96 balrog
303 a8fbaf96 balrog
    if (x + w > s->width) {
304 a8fbaf96 balrog
        fprintf(stderr, "%s: update width too large x: %d, w: %d\n",
305 a8fbaf96 balrog
                        __FUNCTION__, x, w);
306 a8fbaf96 balrog
        x = MIN(x, s->width);
307 a8fbaf96 balrog
        w = s->width - x;
308 a8fbaf96 balrog
    }
309 a8fbaf96 balrog
310 a8fbaf96 balrog
    if (y + h > s->height) {
311 a8fbaf96 balrog
        fprintf(stderr, "%s: update height too large y: %d, h: %d\n",
312 a8fbaf96 balrog
                        __FUNCTION__, y, h);
313 a8fbaf96 balrog
        y = MIN(y, s->height);
314 a8fbaf96 balrog
        h = s->height - y;
315 a8fbaf96 balrog
    }
316 a8fbaf96 balrog
317 a8fbaf96 balrog
    line = h;
318 a8fbaf96 balrog
    bypl = s->bypp * s->width;
319 a8fbaf96 balrog
    width = s->bypp * w;
320 a8fbaf96 balrog
    start = s->bypp * x + bypl * y;
321 4e12cd94 Avi Kivity
    src = s->vga.vram_ptr + start;
322 4e12cd94 Avi Kivity
    dst = ds_get_data(s->vga.ds) + start;
323 d34cab9f ths
324 d34cab9f ths
    for (; line > 0; line --, src += bypl, dst += bypl)
325 d34cab9f ths
        memcpy(dst, src, width);
326 d34cab9f ths
#endif
327 d34cab9f ths
328 4e12cd94 Avi Kivity
    dpy_update(s->vga.ds, x, y, w, h);
329 d34cab9f ths
}
330 d34cab9f ths
331 d34cab9f ths
static inline void vmsvga_update_screen(struct vmsvga_state_s *s)
332 d34cab9f ths
{
333 d34cab9f ths
#ifndef DIRECT_VRAM
334 4e12cd94 Avi Kivity
    memcpy(ds_get_data(s->vga.ds), s->vga.vram_ptr, s->bypp * s->width * s->height);
335 d34cab9f ths
#endif
336 d34cab9f ths
337 4e12cd94 Avi Kivity
    dpy_update(s->vga.ds, 0, 0, s->width, s->height);
338 d34cab9f ths
}
339 d34cab9f ths
340 d34cab9f ths
#ifdef DIRECT_VRAM
341 d34cab9f ths
# define vmsvga_update_rect_delayed        vmsvga_update_rect
342 d34cab9f ths
#else
343 d34cab9f ths
static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
344 d34cab9f ths
                int x, int y, int w, int h)
345 d34cab9f ths
{
346 d34cab9f ths
    struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last ++];
347 d34cab9f ths
    s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
348 d34cab9f ths
    rect->x = x;
349 d34cab9f ths
    rect->y = y;
350 d34cab9f ths
    rect->w = w;
351 d34cab9f ths
    rect->h = h;
352 d34cab9f ths
}
353 d34cab9f ths
#endif
354 d34cab9f ths
355 d34cab9f ths
static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
356 d34cab9f ths
{
357 d34cab9f ths
    struct vmsvga_rect_s *rect;
358 d34cab9f ths
    if (s->invalidated) {
359 d34cab9f ths
        s->redraw_fifo_first = s->redraw_fifo_last;
360 d34cab9f ths
        return;
361 d34cab9f ths
    }
362 d34cab9f ths
    /* Overlapping region updates can be optimised out here - if someone
363 d34cab9f ths
     * knows a smart algorithm to do that, please share.  */
364 d34cab9f ths
    while (s->redraw_fifo_first != s->redraw_fifo_last) {
365 d34cab9f ths
        rect = &s->redraw_fifo[s->redraw_fifo_first ++];
366 d34cab9f ths
        s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
367 d34cab9f ths
        vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
368 d34cab9f ths
    }
369 d34cab9f ths
}
370 d34cab9f ths
371 d34cab9f ths
#ifdef HW_RECT_ACCEL
372 d34cab9f ths
static inline void vmsvga_copy_rect(struct vmsvga_state_s *s,
373 d34cab9f ths
                int x0, int y0, int x1, int y1, int w, int h)
374 d34cab9f ths
{
375 d34cab9f ths
# ifdef DIRECT_VRAM
376 0e1f5a0c aliguori
    uint8_t *vram = ds_get_data(s->ds);
377 d34cab9f ths
# else
378 4e12cd94 Avi Kivity
    uint8_t *vram = s->vga.vram_ptr;
379 d34cab9f ths
# endif
380 d34cab9f ths
    int bypl = s->bypp * s->width;
381 d34cab9f ths
    int width = s->bypp * w;
382 d34cab9f ths
    int line = h;
383 d34cab9f ths
    uint8_t *ptr[2];
384 d34cab9f ths
385 d34cab9f ths
# ifdef DIRECT_VRAM
386 d34cab9f ths
    if (s->ds->dpy_copy)
387 3023f332 aliguori
        qemu_console_copy(s->ds, x0, y0, x1, y1, w, h);
388 d34cab9f ths
    else
389 d34cab9f ths
# endif
390 d34cab9f ths
    {
391 d34cab9f ths
        if (y1 > y0) {
392 d34cab9f ths
            ptr[0] = vram + s->bypp * x0 + bypl * (y0 + h - 1);
393 d34cab9f ths
            ptr[1] = vram + s->bypp * x1 + bypl * (y1 + h - 1);
394 d34cab9f ths
            for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl)
395 d34cab9f ths
                memmove(ptr[1], ptr[0], width);
396 d34cab9f ths
        } else {
397 d34cab9f ths
            ptr[0] = vram + s->bypp * x0 + bypl * y0;
398 d34cab9f ths
            ptr[1] = vram + s->bypp * x1 + bypl * y1;
399 d34cab9f ths
            for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl)
400 d34cab9f ths
                memmove(ptr[1], ptr[0], width);
401 d34cab9f ths
        }
402 d34cab9f ths
    }
403 d34cab9f ths
404 d34cab9f ths
    vmsvga_update_rect_delayed(s, x1, y1, w, h);
405 d34cab9f ths
}
406 d34cab9f ths
#endif
407 d34cab9f ths
408 d34cab9f ths
#ifdef HW_FILL_ACCEL
409 d34cab9f ths
static inline void vmsvga_fill_rect(struct vmsvga_state_s *s,
410 d34cab9f ths
                uint32_t c, int x, int y, int w, int h)
411 d34cab9f ths
{
412 d34cab9f ths
# ifdef DIRECT_VRAM
413 0e1f5a0c aliguori
    uint8_t *vram = ds_get_data(s->ds);
414 d34cab9f ths
# else
415 4e12cd94 Avi Kivity
    uint8_t *vram = s->vga.vram_ptr;
416 d34cab9f ths
# endif
417 d34cab9f ths
    int bypp = s->bypp;
418 d34cab9f ths
    int bypl = bypp * s->width;
419 d34cab9f ths
    int width = bypp * w;
420 d34cab9f ths
    int line = h;
421 d34cab9f ths
    int column;
422 d34cab9f ths
    uint8_t *fst = vram + bypp * x + bypl * y;
423 d34cab9f ths
    uint8_t *dst;
424 d34cab9f ths
    uint8_t *src;
425 d34cab9f ths
    uint8_t col[4];
426 d34cab9f ths
427 d34cab9f ths
# ifdef DIRECT_VRAM
428 d34cab9f ths
    if (s->ds->dpy_fill)
429 d34cab9f ths
        s->ds->dpy_fill(s->ds, x, y, w, h, c);
430 d34cab9f ths
    else
431 d34cab9f ths
# endif
432 d34cab9f ths
    {
433 d34cab9f ths
        col[0] = c;
434 d34cab9f ths
        col[1] = c >> 8;
435 d34cab9f ths
        col[2] = c >> 16;
436 d34cab9f ths
        col[3] = c >> 24;
437 d34cab9f ths
438 d34cab9f ths
        if (line --) {
439 d34cab9f ths
            dst = fst;
440 d34cab9f ths
            src = col;
441 d34cab9f ths
            for (column = width; column > 0; column --) {
442 d34cab9f ths
                *(dst ++) = *(src ++);
443 d34cab9f ths
                if (src - col == bypp)
444 d34cab9f ths
                    src = col;
445 d34cab9f ths
            }
446 d34cab9f ths
            dst = fst;
447 d34cab9f ths
            for (; line > 0; line --) {
448 d34cab9f ths
                dst += bypl;
449 d34cab9f ths
                memcpy(dst, fst, width);
450 d34cab9f ths
            }
451 d34cab9f ths
        }
452 d34cab9f ths
    }
453 d34cab9f ths
454 d34cab9f ths
    vmsvga_update_rect_delayed(s, x, y, w, h);
455 d34cab9f ths
}
456 d34cab9f ths
#endif
457 d34cab9f ths
458 d34cab9f ths
struct vmsvga_cursor_definition_s {
459 d34cab9f ths
    int width;
460 d34cab9f ths
    int height;
461 d34cab9f ths
    int id;
462 d34cab9f ths
    int bpp;
463 d34cab9f ths
    int hot_x;
464 d34cab9f ths
    int hot_y;
465 d34cab9f ths
    uint32_t mask[1024];
466 8095cb3e Dave Airlie
    uint32_t image[4096];
467 d34cab9f ths
};
468 d34cab9f ths
469 d34cab9f ths
#define SVGA_BITMAP_SIZE(w, h)                ((((w) + 31) >> 5) * (h))
470 d34cab9f ths
#define SVGA_PIXMAP_SIZE(w, h, bpp)        (((((w) * (bpp)) + 31) >> 5) * (h))
471 d34cab9f ths
472 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
473 d34cab9f ths
static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
474 d34cab9f ths
                struct vmsvga_cursor_definition_s *c)
475 d34cab9f ths
{
476 fbe6d7a4 Gerd Hoffmann
    QEMUCursor *qc;
477 fbe6d7a4 Gerd Hoffmann
    int i, pixels;
478 fbe6d7a4 Gerd Hoffmann
479 fbe6d7a4 Gerd Hoffmann
    qc = cursor_alloc(c->width, c->height);
480 fbe6d7a4 Gerd Hoffmann
    qc->hot_x = c->hot_x;
481 fbe6d7a4 Gerd Hoffmann
    qc->hot_y = c->hot_y;
482 fbe6d7a4 Gerd Hoffmann
    switch (c->bpp) {
483 fbe6d7a4 Gerd Hoffmann
    case 1:
484 fbe6d7a4 Gerd Hoffmann
        cursor_set_mono(qc, 0xffffff, 0x000000, (void*)c->image,
485 fbe6d7a4 Gerd Hoffmann
                        1, (void*)c->mask);
486 fbe6d7a4 Gerd Hoffmann
#ifdef DEBUG
487 fbe6d7a4 Gerd Hoffmann
        cursor_print_ascii_art(qc, "vmware/mono");
488 fbe6d7a4 Gerd Hoffmann
#endif
489 fbe6d7a4 Gerd Hoffmann
        break;
490 fbe6d7a4 Gerd Hoffmann
    case 32:
491 fbe6d7a4 Gerd Hoffmann
        /* fill alpha channel from mask, set color to zero */
492 fbe6d7a4 Gerd Hoffmann
        cursor_set_mono(qc, 0x000000, 0x000000, (void*)c->mask,
493 fbe6d7a4 Gerd Hoffmann
                        1, (void*)c->mask);
494 fbe6d7a4 Gerd Hoffmann
        /* add in rgb values */
495 fbe6d7a4 Gerd Hoffmann
        pixels = c->width * c->height;
496 fbe6d7a4 Gerd Hoffmann
        for (i = 0; i < pixels; i++) {
497 fbe6d7a4 Gerd Hoffmann
            qc->data[i] |= c->image[i] & 0xffffff;
498 fbe6d7a4 Gerd Hoffmann
        }
499 fbe6d7a4 Gerd Hoffmann
#ifdef DEBUG
500 fbe6d7a4 Gerd Hoffmann
        cursor_print_ascii_art(qc, "vmware/32bit");
501 fbe6d7a4 Gerd Hoffmann
#endif
502 fbe6d7a4 Gerd Hoffmann
        break;
503 fbe6d7a4 Gerd Hoffmann
    default:
504 fbe6d7a4 Gerd Hoffmann
        fprintf(stderr, "%s: unhandled bpp %d, using fallback cursor\n",
505 fbe6d7a4 Gerd Hoffmann
                __FUNCTION__, c->bpp);
506 fbe6d7a4 Gerd Hoffmann
        cursor_put(qc);
507 fbe6d7a4 Gerd Hoffmann
        qc = cursor_builtin_left_ptr();
508 fbe6d7a4 Gerd Hoffmann
    }
509 d34cab9f ths
510 4e12cd94 Avi Kivity
    if (s->vga.ds->cursor_define)
511 fbe6d7a4 Gerd Hoffmann
        s->vga.ds->cursor_define(qc);
512 fbe6d7a4 Gerd Hoffmann
    cursor_put(qc);
513 d34cab9f ths
}
514 d34cab9f ths
#endif
515 d34cab9f ths
516 ff9cf2cb balrog
#define CMD(f)        le32_to_cpu(s->cmd->f)
517 ff9cf2cb balrog
518 4dedc07f Andrzej Zaborowski
static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)
519 d34cab9f ths
{
520 4dedc07f Andrzej Zaborowski
    int num;
521 d34cab9f ths
    if (!s->config || !s->enable)
522 4dedc07f Andrzej Zaborowski
        return 0;
523 4dedc07f Andrzej Zaborowski
    num = CMD(next_cmd) - CMD(stop);
524 4dedc07f Andrzej Zaborowski
    if (num < 0)
525 4dedc07f Andrzej Zaborowski
        num += CMD(max) - CMD(min);
526 4dedc07f Andrzej Zaborowski
    return num >> 2;
527 d34cab9f ths
}
528 d34cab9f ths
529 ff9cf2cb balrog
static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
530 d34cab9f ths
{
531 ff9cf2cb balrog
    uint32_t cmd = s->fifo[CMD(stop) >> 2];
532 ff9cf2cb balrog
    s->cmd->stop = cpu_to_le32(CMD(stop) + 4);
533 ff9cf2cb balrog
    if (CMD(stop) >= CMD(max))
534 d34cab9f ths
        s->cmd->stop = s->cmd->min;
535 d34cab9f ths
    return cmd;
536 d34cab9f ths
}
537 d34cab9f ths
538 ff9cf2cb balrog
static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
539 ff9cf2cb balrog
{
540 ff9cf2cb balrog
    return le32_to_cpu(vmsvga_fifo_read_raw(s));
541 ff9cf2cb balrog
}
542 ff9cf2cb balrog
543 d34cab9f ths
static void vmsvga_fifo_run(struct vmsvga_state_s *s)
544 d34cab9f ths
{
545 d34cab9f ths
    uint32_t cmd, colour;
546 4dedc07f Andrzej Zaborowski
    int args, len;
547 d34cab9f ths
    int x, y, dx, dy, width, height;
548 d34cab9f ths
    struct vmsvga_cursor_definition_s cursor;
549 4dedc07f Andrzej Zaborowski
    uint32_t cmd_start;
550 4dedc07f Andrzej Zaborowski
551 4dedc07f Andrzej Zaborowski
    len = vmsvga_fifo_length(s);
552 4dedc07f Andrzej Zaborowski
    while (len > 0) {
553 4dedc07f Andrzej Zaborowski
        /* May need to go back to the start of the command if incomplete */
554 4dedc07f Andrzej Zaborowski
        cmd_start = s->cmd->stop;
555 4dedc07f Andrzej Zaborowski
556 d34cab9f ths
        switch (cmd = vmsvga_fifo_read(s)) {
557 d34cab9f ths
        case SVGA_CMD_UPDATE:
558 d34cab9f ths
        case SVGA_CMD_UPDATE_VERBOSE:
559 4dedc07f Andrzej Zaborowski
            len -= 5;
560 4dedc07f Andrzej Zaborowski
            if (len < 0)
561 4dedc07f Andrzej Zaborowski
                goto rewind;
562 4dedc07f Andrzej Zaborowski
563 d34cab9f ths
            x = vmsvga_fifo_read(s);
564 d34cab9f ths
            y = vmsvga_fifo_read(s);
565 d34cab9f ths
            width = vmsvga_fifo_read(s);
566 d34cab9f ths
            height = vmsvga_fifo_read(s);
567 d34cab9f ths
            vmsvga_update_rect_delayed(s, x, y, width, height);
568 d34cab9f ths
            break;
569 d34cab9f ths
570 d34cab9f ths
        case SVGA_CMD_RECT_FILL:
571 4dedc07f Andrzej Zaborowski
            len -= 6;
572 4dedc07f Andrzej Zaborowski
            if (len < 0)
573 4dedc07f Andrzej Zaborowski
                goto rewind;
574 4dedc07f Andrzej Zaborowski
575 d34cab9f ths
            colour = vmsvga_fifo_read(s);
576 d34cab9f ths
            x = vmsvga_fifo_read(s);
577 d34cab9f ths
            y = vmsvga_fifo_read(s);
578 d34cab9f ths
            width = vmsvga_fifo_read(s);
579 d34cab9f ths
            height = vmsvga_fifo_read(s);
580 d34cab9f ths
#ifdef HW_FILL_ACCEL
581 d34cab9f ths
            vmsvga_fill_rect(s, colour, x, y, width, height);
582 d34cab9f ths
            break;
583 d34cab9f ths
#else
584 4dedc07f Andrzej Zaborowski
            args = 0;
585 d34cab9f ths
            goto badcmd;
586 d34cab9f ths
#endif
587 d34cab9f ths
588 d34cab9f ths
        case SVGA_CMD_RECT_COPY:
589 4dedc07f Andrzej Zaborowski
            len -= 7;
590 4dedc07f Andrzej Zaborowski
            if (len < 0)
591 4dedc07f Andrzej Zaborowski
                goto rewind;
592 4dedc07f Andrzej Zaborowski
593 d34cab9f ths
            x = vmsvga_fifo_read(s);
594 d34cab9f ths
            y = vmsvga_fifo_read(s);
595 d34cab9f ths
            dx = vmsvga_fifo_read(s);
596 d34cab9f ths
            dy = vmsvga_fifo_read(s);
597 d34cab9f ths
            width = vmsvga_fifo_read(s);
598 d34cab9f ths
            height = vmsvga_fifo_read(s);
599 d34cab9f ths
#ifdef HW_RECT_ACCEL
600 d34cab9f ths
            vmsvga_copy_rect(s, x, y, dx, dy, width, height);
601 d34cab9f ths
            break;
602 d34cab9f ths
#else
603 4dedc07f Andrzej Zaborowski
            args = 0;
604 d34cab9f ths
            goto badcmd;
605 d34cab9f ths
#endif
606 d34cab9f ths
607 d34cab9f ths
        case SVGA_CMD_DEFINE_CURSOR:
608 4dedc07f Andrzej Zaborowski
            len -= 8;
609 4dedc07f Andrzej Zaborowski
            if (len < 0)
610 4dedc07f Andrzej Zaborowski
                goto rewind;
611 4dedc07f Andrzej Zaborowski
612 d34cab9f ths
            cursor.id = vmsvga_fifo_read(s);
613 d34cab9f ths
            cursor.hot_x = vmsvga_fifo_read(s);
614 d34cab9f ths
            cursor.hot_y = vmsvga_fifo_read(s);
615 d34cab9f ths
            cursor.width = x = vmsvga_fifo_read(s);
616 d34cab9f ths
            cursor.height = y = vmsvga_fifo_read(s);
617 d34cab9f ths
            vmsvga_fifo_read(s);
618 d34cab9f ths
            cursor.bpp = vmsvga_fifo_read(s);
619 f2d928d4 Roland Dreier
620 4dedc07f Andrzej Zaborowski
            args = SVGA_BITMAP_SIZE(x, y) + SVGA_PIXMAP_SIZE(x, y, cursor.bpp);
621 9f810beb Andrzej Zaborowski
            if (SVGA_BITMAP_SIZE(x, y) > sizeof cursor.mask ||
622 9f810beb Andrzej Zaborowski
                SVGA_PIXMAP_SIZE(x, y, cursor.bpp) > sizeof cursor.image)
623 9f810beb Andrzej Zaborowski
                    goto badcmd;
624 4dedc07f Andrzej Zaborowski
625 4dedc07f Andrzej Zaborowski
            len -= args;
626 4dedc07f Andrzej Zaborowski
            if (len < 0)
627 4dedc07f Andrzej Zaborowski
                goto rewind;
628 f2d928d4 Roland Dreier
629 d34cab9f ths
            for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args ++)
630 ff9cf2cb balrog
                cursor.mask[args] = vmsvga_fifo_read_raw(s);
631 d34cab9f ths
            for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args ++)
632 ff9cf2cb balrog
                cursor.image[args] = vmsvga_fifo_read_raw(s);
633 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
634 d34cab9f ths
            vmsvga_cursor_define(s, &cursor);
635 d34cab9f ths
            break;
636 d34cab9f ths
#else
637 d34cab9f ths
            args = 0;
638 d34cab9f ths
            goto badcmd;
639 d34cab9f ths
#endif
640 d34cab9f ths
641 d34cab9f ths
        /*
642 d34cab9f ths
         * Other commands that we at least know the number of arguments
643 d34cab9f ths
         * for so we can avoid FIFO desync if driver uses them illegally.
644 d34cab9f ths
         */
645 d34cab9f ths
        case SVGA_CMD_DEFINE_ALPHA_CURSOR:
646 4dedc07f Andrzej Zaborowski
            len -= 6;
647 4dedc07f Andrzej Zaborowski
            if (len < 0)
648 4dedc07f Andrzej Zaborowski
                goto rewind;
649 4dedc07f Andrzej Zaborowski
650 d34cab9f ths
            vmsvga_fifo_read(s);
651 d34cab9f ths
            vmsvga_fifo_read(s);
652 d34cab9f ths
            vmsvga_fifo_read(s);
653 d34cab9f ths
            x = vmsvga_fifo_read(s);
654 d34cab9f ths
            y = vmsvga_fifo_read(s);
655 d34cab9f ths
            args = x * y;
656 d34cab9f ths
            goto badcmd;
657 d34cab9f ths
        case SVGA_CMD_RECT_ROP_FILL:
658 d34cab9f ths
            args = 6;
659 d34cab9f ths
            goto badcmd;
660 d34cab9f ths
        case SVGA_CMD_RECT_ROP_COPY:
661 d34cab9f ths
            args = 7;
662 d34cab9f ths
            goto badcmd;
663 d34cab9f ths
        case SVGA_CMD_DRAW_GLYPH_CLIPPED:
664 4dedc07f Andrzej Zaborowski
            len -= 4;
665 4dedc07f Andrzej Zaborowski
            if (len < 0)
666 4dedc07f Andrzej Zaborowski
                goto rewind;
667 4dedc07f Andrzej Zaborowski
668 d34cab9f ths
            vmsvga_fifo_read(s);
669 d34cab9f ths
            vmsvga_fifo_read(s);
670 d34cab9f ths
            args = 7 + (vmsvga_fifo_read(s) >> 2);
671 d34cab9f ths
            goto badcmd;
672 d34cab9f ths
        case SVGA_CMD_SURFACE_ALPHA_BLEND:
673 d34cab9f ths
            args = 12;
674 d34cab9f ths
            goto badcmd;
675 d34cab9f ths
676 d34cab9f ths
        /*
677 d34cab9f ths
         * Other commands that are not listed as depending on any
678 d34cab9f ths
         * CAPABILITIES bits, but are not described in the README either.
679 d34cab9f ths
         */
680 d34cab9f ths
        case SVGA_CMD_SURFACE_FILL:
681 d34cab9f ths
        case SVGA_CMD_SURFACE_COPY:
682 d34cab9f ths
        case SVGA_CMD_FRONT_ROP_FILL:
683 d34cab9f ths
        case SVGA_CMD_FENCE:
684 d34cab9f ths
        case SVGA_CMD_INVALID_CMD:
685 d34cab9f ths
            break; /* Nop */
686 d34cab9f ths
687 d34cab9f ths
        default:
688 4dedc07f Andrzej Zaborowski
            args = 0;
689 d34cab9f ths
        badcmd:
690 4dedc07f Andrzej Zaborowski
            len -= args;
691 4dedc07f Andrzej Zaborowski
            if (len < 0)
692 4dedc07f Andrzej Zaborowski
                goto rewind;
693 d34cab9f ths
            while (args --)
694 d34cab9f ths
                vmsvga_fifo_read(s);
695 d34cab9f ths
            printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
696 d34cab9f ths
                            __FUNCTION__, cmd);
697 d34cab9f ths
            break;
698 4dedc07f Andrzej Zaborowski
699 4dedc07f Andrzej Zaborowski
        rewind:
700 4dedc07f Andrzej Zaborowski
            s->cmd->stop = cmd_start;
701 4dedc07f Andrzej Zaborowski
            break;
702 d34cab9f ths
        }
703 4dedc07f Andrzej Zaborowski
    }
704 d34cab9f ths
705 d34cab9f ths
    s->syncing = 0;
706 d34cab9f ths
}
707 d34cab9f ths
708 d34cab9f ths
static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
709 d34cab9f ths
{
710 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
711 d34cab9f ths
    return s->index;
712 d34cab9f ths
}
713 d34cab9f ths
714 d34cab9f ths
static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
715 d34cab9f ths
{
716 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
717 d34cab9f ths
    s->index = index;
718 d34cab9f ths
}
719 d34cab9f ths
720 d34cab9f ths
static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
721 d34cab9f ths
{
722 d34cab9f ths
    uint32_t caps;
723 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
724 d34cab9f ths
    switch (s->index) {
725 d34cab9f ths
    case SVGA_REG_ID:
726 d34cab9f ths
        return s->svgaid;
727 d34cab9f ths
728 d34cab9f ths
    case SVGA_REG_ENABLE:
729 d34cab9f ths
        return s->enable;
730 d34cab9f ths
731 d34cab9f ths
    case SVGA_REG_WIDTH:
732 d34cab9f ths
        return s->width;
733 d34cab9f ths
734 d34cab9f ths
    case SVGA_REG_HEIGHT:
735 d34cab9f ths
        return s->height;
736 d34cab9f ths
737 d34cab9f ths
    case SVGA_REG_MAX_WIDTH:
738 d34cab9f ths
        return SVGA_MAX_WIDTH;
739 d34cab9f ths
740 d34cab9f ths
    case SVGA_REG_MAX_HEIGHT:
741 f707cfba balrog
        return SVGA_MAX_HEIGHT;
742 d34cab9f ths
743 d34cab9f ths
    case SVGA_REG_DEPTH:
744 d34cab9f ths
        return s->depth;
745 d34cab9f ths
746 d34cab9f ths
    case SVGA_REG_BITS_PER_PIXEL:
747 d34cab9f ths
        return (s->depth + 7) & ~7;
748 d34cab9f ths
749 d34cab9f ths
    case SVGA_REG_PSEUDOCOLOR:
750 d34cab9f ths
        return 0x0;
751 d34cab9f ths
752 d34cab9f ths
    case SVGA_REG_RED_MASK:
753 d34cab9f ths
        return s->wred;
754 d34cab9f ths
    case SVGA_REG_GREEN_MASK:
755 d34cab9f ths
        return s->wgreen;
756 d34cab9f ths
    case SVGA_REG_BLUE_MASK:
757 d34cab9f ths
        return s->wblue;
758 d34cab9f ths
759 d34cab9f ths
    case SVGA_REG_BYTES_PER_LINE:
760 d34cab9f ths
        return ((s->depth + 7) >> 3) * s->new_width;
761 d34cab9f ths
762 7b619b9a Avi Kivity
    case SVGA_REG_FB_START: {
763 7b619b9a Avi Kivity
        struct pci_vmsvga_state_s *pci_vmsvga
764 7b619b9a Avi Kivity
            = container_of(s, struct pci_vmsvga_state_s, chip);
765 7b619b9a Avi Kivity
        return pci_get_bar_addr(&pci_vmsvga->card, 1);
766 7b619b9a Avi Kivity
    }
767 d34cab9f ths
768 d34cab9f ths
    case SVGA_REG_FB_OFFSET:
769 d34cab9f ths
        return 0x0;
770 d34cab9f ths
771 d34cab9f ths
    case SVGA_REG_VRAM_SIZE:
772 f351d050 Dave Airlie
        return s->vga.vram_size;
773 d34cab9f ths
774 d34cab9f ths
    case SVGA_REG_FB_SIZE:
775 d34cab9f ths
        return s->fb_size;
776 d34cab9f ths
777 d34cab9f ths
    case SVGA_REG_CAPABILITIES:
778 d34cab9f ths
        caps = SVGA_CAP_NONE;
779 d34cab9f ths
#ifdef HW_RECT_ACCEL
780 d34cab9f ths
        caps |= SVGA_CAP_RECT_COPY;
781 d34cab9f ths
#endif
782 d34cab9f ths
#ifdef HW_FILL_ACCEL
783 d34cab9f ths
        caps |= SVGA_CAP_RECT_FILL;
784 d34cab9f ths
#endif
785 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
786 4e12cd94 Avi Kivity
        if (s->vga.ds->mouse_set)
787 d34cab9f ths
            caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
788 d34cab9f ths
                    SVGA_CAP_CURSOR_BYPASS;
789 d34cab9f ths
#endif
790 d34cab9f ths
        return caps;
791 d34cab9f ths
792 b1950430 Avi Kivity
    case SVGA_REG_MEM_START: {
793 b1950430 Avi Kivity
        struct pci_vmsvga_state_s *pci_vmsvga
794 b1950430 Avi Kivity
            = container_of(s, struct pci_vmsvga_state_s, chip);
795 b1950430 Avi Kivity
        return pci_get_bar_addr(&pci_vmsvga->card, 2);
796 b1950430 Avi Kivity
    }
797 d34cab9f ths
798 d34cab9f ths
    case SVGA_REG_MEM_SIZE:
799 f351d050 Dave Airlie
        return s->fifo_size;
800 d34cab9f ths
801 d34cab9f ths
    case SVGA_REG_CONFIG_DONE:
802 d34cab9f ths
        return s->config;
803 d34cab9f ths
804 d34cab9f ths
    case SVGA_REG_SYNC:
805 d34cab9f ths
    case SVGA_REG_BUSY:
806 d34cab9f ths
        return s->syncing;
807 d34cab9f ths
808 d34cab9f ths
    case SVGA_REG_GUEST_ID:
809 d34cab9f ths
        return s->guest;
810 d34cab9f ths
811 d34cab9f ths
    case SVGA_REG_CURSOR_ID:
812 d34cab9f ths
        return s->cursor.id;
813 d34cab9f ths
814 d34cab9f ths
    case SVGA_REG_CURSOR_X:
815 d34cab9f ths
        return s->cursor.x;
816 d34cab9f ths
817 d34cab9f ths
    case SVGA_REG_CURSOR_Y:
818 d34cab9f ths
        return s->cursor.x;
819 d34cab9f ths
820 d34cab9f ths
    case SVGA_REG_CURSOR_ON:
821 d34cab9f ths
        return s->cursor.on;
822 d34cab9f ths
823 d34cab9f ths
    case SVGA_REG_HOST_BITS_PER_PIXEL:
824 d34cab9f ths
        return (s->depth + 7) & ~7;
825 d34cab9f ths
826 d34cab9f ths
    case SVGA_REG_SCRATCH_SIZE:
827 d34cab9f ths
        return s->scratch_size;
828 d34cab9f ths
829 d34cab9f ths
    case SVGA_REG_MEM_REGS:
830 d34cab9f ths
    case SVGA_REG_NUM_DISPLAYS:
831 d34cab9f ths
    case SVGA_REG_PITCHLOCK:
832 d34cab9f ths
    case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
833 d34cab9f ths
        return 0;
834 d34cab9f ths
835 d34cab9f ths
    default:
836 d34cab9f ths
        if (s->index >= SVGA_SCRATCH_BASE &&
837 d34cab9f ths
                s->index < SVGA_SCRATCH_BASE + s->scratch_size)
838 d34cab9f ths
            return s->scratch[s->index - SVGA_SCRATCH_BASE];
839 d34cab9f ths
        printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
840 d34cab9f ths
    }
841 d34cab9f ths
842 d34cab9f ths
    return 0;
843 d34cab9f ths
}
844 d34cab9f ths
845 d34cab9f ths
static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
846 d34cab9f ths
{
847 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
848 d34cab9f ths
    switch (s->index) {
849 d34cab9f ths
    case SVGA_REG_ID:
850 d34cab9f ths
        if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0)
851 d34cab9f ths
            s->svgaid = value;
852 d34cab9f ths
        break;
853 d34cab9f ths
854 d34cab9f ths
    case SVGA_REG_ENABLE:
855 f707cfba balrog
        s->enable = value;
856 f707cfba balrog
        s->config &= !!value;
857 d34cab9f ths
        s->width = -1;
858 d34cab9f ths
        s->height = -1;
859 d34cab9f ths
        s->invalidated = 1;
860 4e12cd94 Avi Kivity
        s->vga.invalidate(&s->vga);
861 b5cc6e32 Anthony Liguori
        if (s->enable) {
862 9f810beb Andrzej Zaborowski
            s->fb_size = ((s->depth + 7) >> 3) * s->new_width * s->new_height;
863 9f810beb Andrzej Zaborowski
            vga_dirty_log_stop(&s->vga);
864 9f810beb Andrzej Zaborowski
        } else {
865 9f810beb Andrzej Zaborowski
            vga_dirty_log_start(&s->vga);
866 9f810beb Andrzej Zaborowski
        }
867 d34cab9f ths
        break;
868 d34cab9f ths
869 d34cab9f ths
    case SVGA_REG_WIDTH:
870 d34cab9f ths
        s->new_width = value;
871 d34cab9f ths
        s->invalidated = 1;
872 d34cab9f ths
        break;
873 d34cab9f ths
874 d34cab9f ths
    case SVGA_REG_HEIGHT:
875 d34cab9f ths
        s->new_height = value;
876 d34cab9f ths
        s->invalidated = 1;
877 d34cab9f ths
        break;
878 d34cab9f ths
879 d34cab9f ths
    case SVGA_REG_DEPTH:
880 d34cab9f ths
    case SVGA_REG_BITS_PER_PIXEL:
881 d34cab9f ths
        if (value != s->depth) {
882 d34cab9f ths
            printf("%s: Bad colour depth: %i bits\n", __FUNCTION__, value);
883 d34cab9f ths
            s->config = 0;
884 d34cab9f ths
        }
885 d34cab9f ths
        break;
886 d34cab9f ths
887 d34cab9f ths
    case SVGA_REG_CONFIG_DONE:
888 d34cab9f ths
        if (value) {
889 f351d050 Dave Airlie
            s->fifo = (uint32_t *) s->fifo_ptr;
890 d34cab9f ths
            /* Check range and alignment.  */
891 ff9cf2cb balrog
            if ((CMD(min) | CMD(max) |
892 ff9cf2cb balrog
                        CMD(next_cmd) | CMD(stop)) & 3)
893 d34cab9f ths
                break;
894 ff9cf2cb balrog
            if (CMD(min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo)
895 d34cab9f ths
                break;
896 ff9cf2cb balrog
            if (CMD(max) > SVGA_FIFO_SIZE)
897 d34cab9f ths
                break;
898 ff9cf2cb balrog
            if (CMD(max) < CMD(min) + 10 * 1024)
899 d34cab9f ths
                break;
900 d34cab9f ths
        }
901 f707cfba balrog
        s->config = !!value;
902 d34cab9f ths
        break;
903 d34cab9f ths
904 d34cab9f ths
    case SVGA_REG_SYNC:
905 d34cab9f ths
        s->syncing = 1;
906 d34cab9f ths
        vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
907 d34cab9f ths
        break;
908 d34cab9f ths
909 d34cab9f ths
    case SVGA_REG_GUEST_ID:
910 d34cab9f ths
        s->guest = value;
911 d34cab9f ths
#ifdef VERBOSE
912 d34cab9f ths
        if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
913 b1503cda malc
                ARRAY_SIZE(vmsvga_guest_id))
914 d34cab9f ths
            printf("%s: guest runs %s.\n", __FUNCTION__,
915 d34cab9f ths
                            vmsvga_guest_id[value - GUEST_OS_BASE]);
916 d34cab9f ths
#endif
917 d34cab9f ths
        break;
918 d34cab9f ths
919 d34cab9f ths
    case SVGA_REG_CURSOR_ID:
920 d34cab9f ths
        s->cursor.id = value;
921 d34cab9f ths
        break;
922 d34cab9f ths
923 d34cab9f ths
    case SVGA_REG_CURSOR_X:
924 d34cab9f ths
        s->cursor.x = value;
925 d34cab9f ths
        break;
926 d34cab9f ths
927 d34cab9f ths
    case SVGA_REG_CURSOR_Y:
928 d34cab9f ths
        s->cursor.y = value;
929 d34cab9f ths
        break;
930 d34cab9f ths
931 d34cab9f ths
    case SVGA_REG_CURSOR_ON:
932 d34cab9f ths
        s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
933 d34cab9f ths
        s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
934 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
935 4e12cd94 Avi Kivity
        if (s->vga.ds->mouse_set && value <= SVGA_CURSOR_ON_SHOW)
936 4e12cd94 Avi Kivity
            s->vga.ds->mouse_set(s->cursor.x, s->cursor.y, s->cursor.on);
937 d34cab9f ths
#endif
938 d34cab9f ths
        break;
939 d34cab9f ths
940 d34cab9f ths
    case SVGA_REG_MEM_REGS:
941 d34cab9f ths
    case SVGA_REG_NUM_DISPLAYS:
942 d34cab9f ths
    case SVGA_REG_PITCHLOCK:
943 d34cab9f ths
    case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
944 d34cab9f ths
        break;
945 d34cab9f ths
946 d34cab9f ths
    default:
947 d34cab9f ths
        if (s->index >= SVGA_SCRATCH_BASE &&
948 d34cab9f ths
                s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
949 d34cab9f ths
            s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
950 d34cab9f ths
            break;
951 d34cab9f ths
        }
952 d34cab9f ths
        printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
953 d34cab9f ths
    }
954 d34cab9f ths
}
955 d34cab9f ths
956 d34cab9f ths
static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
957 d34cab9f ths
{
958 d34cab9f ths
    printf("%s: what are we supposed to return?\n", __FUNCTION__);
959 d34cab9f ths
    return 0xcafe;
960 d34cab9f ths
}
961 d34cab9f ths
962 d34cab9f ths
static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
963 d34cab9f ths
{
964 d34cab9f ths
    printf("%s: what are we supposed to do with (%08x)?\n",
965 d34cab9f ths
                    __FUNCTION__, data);
966 d34cab9f ths
}
967 d34cab9f ths
968 d34cab9f ths
static inline void vmsvga_size(struct vmsvga_state_s *s)
969 d34cab9f ths
{
970 d34cab9f ths
    if (s->new_width != s->width || s->new_height != s->height) {
971 d34cab9f ths
        s->width = s->new_width;
972 d34cab9f ths
        s->height = s->new_height;
973 4e12cd94 Avi Kivity
        qemu_console_resize(s->vga.ds, s->width, s->height);
974 d34cab9f ths
        s->invalidated = 1;
975 d34cab9f ths
    }
976 d34cab9f ths
}
977 d34cab9f ths
978 d34cab9f ths
static void vmsvga_update_display(void *opaque)
979 d34cab9f ths
{
980 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
981 d34cab9f ths
    if (!s->enable) {
982 4e12cd94 Avi Kivity
        s->vga.update(&s->vga);
983 d34cab9f ths
        return;
984 d34cab9f ths
    }
985 d34cab9f ths
986 d34cab9f ths
    vmsvga_size(s);
987 d34cab9f ths
988 d34cab9f ths
    vmsvga_fifo_run(s);
989 d34cab9f ths
    vmsvga_update_rect_flush(s);
990 d34cab9f ths
991 d34cab9f ths
    /*
992 d34cab9f ths
     * Is it more efficient to look at vram VGA-dirty bits or wait
993 d34cab9f ths
     * for the driver to issue SVGA_CMD_UPDATE?
994 d34cab9f ths
     */
995 d34cab9f ths
    if (s->invalidated) {
996 d34cab9f ths
        s->invalidated = 0;
997 d34cab9f ths
        vmsvga_update_screen(s);
998 d34cab9f ths
    }
999 d34cab9f ths
}
1000 d34cab9f ths
1001 d34cab9f ths
static void vmsvga_reset(struct vmsvga_state_s *s)
1002 d34cab9f ths
{
1003 d34cab9f ths
    s->index = 0;
1004 d34cab9f ths
    s->enable = 0;
1005 d34cab9f ths
    s->config = 0;
1006 d34cab9f ths
    s->width = -1;
1007 d34cab9f ths
    s->height = -1;
1008 d34cab9f ths
    s->svgaid = SVGA_ID;
1009 a6109ff1 Anthony Liguori
    s->depth = ds_get_bits_per_pixel(s->vga.ds);
1010 a6109ff1 Anthony Liguori
    s->bypp = ds_get_bytes_per_pixel(s->vga.ds);
1011 d34cab9f ths
    s->cursor.on = 0;
1012 d34cab9f ths
    s->redraw_fifo_first = 0;
1013 d34cab9f ths
    s->redraw_fifo_last = 0;
1014 d34cab9f ths
    switch (s->depth) {
1015 d34cab9f ths
    case 8:
1016 d34cab9f ths
        s->wred   = 0x00000007;
1017 d34cab9f ths
        s->wgreen = 0x00000038;
1018 d34cab9f ths
        s->wblue  = 0x000000c0;
1019 d34cab9f ths
        break;
1020 d34cab9f ths
    case 15:
1021 d34cab9f ths
        s->wred   = 0x0000001f;
1022 d34cab9f ths
        s->wgreen = 0x000003e0;
1023 d34cab9f ths
        s->wblue  = 0x00007c00;
1024 d34cab9f ths
        break;
1025 d34cab9f ths
    case 16:
1026 d34cab9f ths
        s->wred   = 0x0000001f;
1027 d34cab9f ths
        s->wgreen = 0x000007e0;
1028 d34cab9f ths
        s->wblue  = 0x0000f800;
1029 d34cab9f ths
        break;
1030 d34cab9f ths
    case 24:
1031 f707cfba balrog
        s->wred   = 0x00ff0000;
1032 d34cab9f ths
        s->wgreen = 0x0000ff00;
1033 f707cfba balrog
        s->wblue  = 0x000000ff;
1034 d34cab9f ths
        break;
1035 d34cab9f ths
    case 32:
1036 f707cfba balrog
        s->wred   = 0x00ff0000;
1037 d34cab9f ths
        s->wgreen = 0x0000ff00;
1038 f707cfba balrog
        s->wblue  = 0x000000ff;
1039 d34cab9f ths
        break;
1040 d34cab9f ths
    }
1041 d34cab9f ths
    s->syncing = 0;
1042 b5cc6e32 Anthony Liguori
1043 b5cc6e32 Anthony Liguori
    vga_dirty_log_start(&s->vga);
1044 d34cab9f ths
}
1045 d34cab9f ths
1046 d34cab9f ths
static void vmsvga_invalidate_display(void *opaque)
1047 d34cab9f ths
{
1048 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1049 d34cab9f ths
    if (!s->enable) {
1050 4e12cd94 Avi Kivity
        s->vga.invalidate(&s->vga);
1051 d34cab9f ths
        return;
1052 d34cab9f ths
    }
1053 d34cab9f ths
1054 d34cab9f ths
    s->invalidated = 1;
1055 d34cab9f ths
}
1056 d34cab9f ths
1057 f707cfba balrog
/* save the vga display in a PPM image even if no display is
1058 f707cfba balrog
   available */
1059 d34cab9f ths
static void vmsvga_screen_dump(void *opaque, const char *filename)
1060 d34cab9f ths
{
1061 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1062 d34cab9f ths
    if (!s->enable) {
1063 4e12cd94 Avi Kivity
        s->vga.screen_dump(&s->vga, filename);
1064 d34cab9f ths
        return;
1065 d34cab9f ths
    }
1066 d34cab9f ths
1067 f707cfba balrog
    if (s->depth == 32) {
1068 e07d630a aliguori
        DisplaySurface *ds = qemu_create_displaysurface_from(s->width,
1069 4e12cd94 Avi Kivity
                s->height, 32, ds_get_linesize(s->vga.ds), s->vga.vram_ptr);
1070 e07d630a aliguori
        ppm_save(filename, ds);
1071 e07d630a aliguori
        qemu_free(ds);
1072 f707cfba balrog
    }
1073 d34cab9f ths
}
1074 d34cab9f ths
1075 c227f099 Anthony Liguori
static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
1076 4d3b6f6e balrog
{
1077 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1078 4d3b6f6e balrog
1079 4e12cd94 Avi Kivity
    if (s->vga.text_update)
1080 4e12cd94 Avi Kivity
        s->vga.text_update(&s->vga, chardata);
1081 4d3b6f6e balrog
}
1082 4d3b6f6e balrog
1083 d34cab9f ths
#ifdef DIRECT_VRAM
1084 c227f099 Anthony Liguori
static uint32_t vmsvga_vram_readb(void *opaque, target_phys_addr_t addr)
1085 d34cab9f ths
{
1086 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1087 d34cab9f ths
    if (addr < s->fb_size)
1088 0e1f5a0c aliguori
        return *(uint8_t *) (ds_get_data(s->ds) + addr);
1089 d34cab9f ths
    else
1090 b584726d pbrook
        return *(uint8_t *) (s->vram_ptr + addr);
1091 d34cab9f ths
}
1092 d34cab9f ths
1093 c227f099 Anthony Liguori
static uint32_t vmsvga_vram_readw(void *opaque, target_phys_addr_t addr)
1094 d34cab9f ths
{
1095 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1096 d34cab9f ths
    if (addr < s->fb_size)
1097 0e1f5a0c aliguori
        return *(uint16_t *) (ds_get_data(s->ds) + addr);
1098 d34cab9f ths
    else
1099 b584726d pbrook
        return *(uint16_t *) (s->vram_ptr + addr);
1100 d34cab9f ths
}
1101 d34cab9f ths
1102 c227f099 Anthony Liguori
static uint32_t vmsvga_vram_readl(void *opaque, target_phys_addr_t addr)
1103 d34cab9f ths
{
1104 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1105 d34cab9f ths
    if (addr < s->fb_size)
1106 0e1f5a0c aliguori
        return *(uint32_t *) (ds_get_data(s->ds) + addr);
1107 d34cab9f ths
    else
1108 b584726d pbrook
        return *(uint32_t *) (s->vram_ptr + addr);
1109 d34cab9f ths
}
1110 d34cab9f ths
1111 c227f099 Anthony Liguori
static void vmsvga_vram_writeb(void *opaque, target_phys_addr_t addr,
1112 d34cab9f ths
                uint32_t value)
1113 d34cab9f ths
{
1114 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1115 d34cab9f ths
    if (addr < s->fb_size)
1116 0e1f5a0c aliguori
        *(uint8_t *) (ds_get_data(s->ds) + addr) = value;
1117 d34cab9f ths
    else
1118 b584726d pbrook
        *(uint8_t *) (s->vram_ptr + addr) = value;
1119 d34cab9f ths
}
1120 d34cab9f ths
1121 c227f099 Anthony Liguori
static void vmsvga_vram_writew(void *opaque, target_phys_addr_t addr,
1122 d34cab9f ths
                uint32_t value)
1123 d34cab9f ths
{
1124 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1125 d34cab9f ths
    if (addr < s->fb_size)
1126 0e1f5a0c aliguori
        *(uint16_t *) (ds_get_data(s->ds) + addr) = value;
1127 d34cab9f ths
    else
1128 b584726d pbrook
        *(uint16_t *) (s->vram_ptr + addr) = value;
1129 d34cab9f ths
}
1130 d34cab9f ths
1131 c227f099 Anthony Liguori
static void vmsvga_vram_writel(void *opaque, target_phys_addr_t addr,
1132 d34cab9f ths
                uint32_t value)
1133 d34cab9f ths
{
1134 467d44b2 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1135 d34cab9f ths
    if (addr < s->fb_size)
1136 0e1f5a0c aliguori
        *(uint32_t *) (ds_get_data(s->ds) + addr) = value;
1137 d34cab9f ths
    else
1138 b584726d pbrook
        *(uint32_t *) (s->vram_ptr + addr) = value;
1139 d34cab9f ths
}
1140 d34cab9f ths
1141 b1950430 Avi Kivity
static const MemoryRegionOps vmsvga_vram_io_ops = {
1142 b1950430 Avi Kivity
    .old_mmio = {
1143 b1950430 Avi Kivity
        .read = {
1144 b1950430 Avi Kivity
            vmsvga_vram_readb,
1145 b1950430 Avi Kivity
            vmsvga_vram_readw,
1146 b1950430 Avi Kivity
            vmsvga_vram_readl,
1147 b1950430 Avi Kivity
        },
1148 b1950430 Avi Kivity
        .write = {
1149 b1950430 Avi Kivity
            vmsvga_vram_writeb,
1150 b1950430 Avi Kivity
            vmsvga_vram_writew,
1151 b1950430 Avi Kivity
            vmsvga_vram_writel,
1152 b1950430 Avi Kivity
        },
1153 b1950430 Avi Kivity
    },
1154 b1950430 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
1155 b1950430 Avi Kivity
}
1156 d34cab9f ths
1157 d34cab9f ths
#endif
1158 d34cab9f ths
1159 bacbe284 Juan Quintela
static int vmsvga_post_load(void *opaque, int version_id)
1160 d34cab9f ths
{
1161 bacbe284 Juan Quintela
    struct vmsvga_state_s *s = opaque;
1162 d34cab9f ths
1163 d34cab9f ths
    s->invalidated = 1;
1164 d34cab9f ths
    if (s->config)
1165 f351d050 Dave Airlie
        s->fifo = (uint32_t *) s->fifo_ptr;
1166 d34cab9f ths
1167 d34cab9f ths
    return 0;
1168 d34cab9f ths
}
1169 d34cab9f ths
1170 d05ac8fa Blue Swirl
static const VMStateDescription vmstate_vmware_vga_internal = {
1171 bacbe284 Juan Quintela
    .name = "vmware_vga_internal",
1172 bacbe284 Juan Quintela
    .version_id = 0,
1173 bacbe284 Juan Quintela
    .minimum_version_id = 0,
1174 bacbe284 Juan Quintela
    .minimum_version_id_old = 0,
1175 bacbe284 Juan Quintela
    .post_load = vmsvga_post_load,
1176 bacbe284 Juan Quintela
    .fields      = (VMStateField []) {
1177 bacbe284 Juan Quintela
        VMSTATE_INT32_EQUAL(depth, struct vmsvga_state_s),
1178 bacbe284 Juan Quintela
        VMSTATE_INT32(enable, struct vmsvga_state_s),
1179 bacbe284 Juan Quintela
        VMSTATE_INT32(config, struct vmsvga_state_s),
1180 bacbe284 Juan Quintela
        VMSTATE_INT32(cursor.id, struct vmsvga_state_s),
1181 bacbe284 Juan Quintela
        VMSTATE_INT32(cursor.x, struct vmsvga_state_s),
1182 bacbe284 Juan Quintela
        VMSTATE_INT32(cursor.y, struct vmsvga_state_s),
1183 bacbe284 Juan Quintela
        VMSTATE_INT32(cursor.on, struct vmsvga_state_s),
1184 bacbe284 Juan Quintela
        VMSTATE_INT32(index, struct vmsvga_state_s),
1185 bacbe284 Juan Quintela
        VMSTATE_VARRAY_INT32(scratch, struct vmsvga_state_s,
1186 bacbe284 Juan Quintela
                             scratch_size, 0, vmstate_info_uint32, uint32_t),
1187 bacbe284 Juan Quintela
        VMSTATE_INT32(new_width, struct vmsvga_state_s),
1188 bacbe284 Juan Quintela
        VMSTATE_INT32(new_height, struct vmsvga_state_s),
1189 bacbe284 Juan Quintela
        VMSTATE_UINT32(guest, struct vmsvga_state_s),
1190 bacbe284 Juan Quintela
        VMSTATE_UINT32(svgaid, struct vmsvga_state_s),
1191 bacbe284 Juan Quintela
        VMSTATE_INT32(syncing, struct vmsvga_state_s),
1192 bacbe284 Juan Quintela
        VMSTATE_INT32(fb_size, struct vmsvga_state_s),
1193 bacbe284 Juan Quintela
        VMSTATE_END_OF_LIST()
1194 bacbe284 Juan Quintela
    }
1195 bacbe284 Juan Quintela
};
1196 bacbe284 Juan Quintela
1197 d05ac8fa Blue Swirl
static const VMStateDescription vmstate_vmware_vga = {
1198 bacbe284 Juan Quintela
    .name = "vmware_vga",
1199 bacbe284 Juan Quintela
    .version_id = 0,
1200 bacbe284 Juan Quintela
    .minimum_version_id = 0,
1201 bacbe284 Juan Quintela
    .minimum_version_id_old = 0,
1202 bacbe284 Juan Quintela
    .fields      = (VMStateField []) {
1203 bacbe284 Juan Quintela
        VMSTATE_PCI_DEVICE(card, struct pci_vmsvga_state_s),
1204 bacbe284 Juan Quintela
        VMSTATE_STRUCT(chip, struct pci_vmsvga_state_s, 0,
1205 bacbe284 Juan Quintela
                       vmstate_vmware_vga_internal, struct vmsvga_state_s),
1206 bacbe284 Juan Quintela
        VMSTATE_END_OF_LIST()
1207 bacbe284 Juan Quintela
    }
1208 bacbe284 Juan Quintela
};
1209 bacbe284 Juan Quintela
1210 b584726d pbrook
static void vmsvga_init(struct vmsvga_state_s *s, int vga_ram_size)
1211 d34cab9f ths
{
1212 d34cab9f ths
    s->scratch_size = SVGA_SCRATCH_SIZE;
1213 fe740c43 Juan Quintela
    s->scratch = qemu_malloc(s->scratch_size * 4);
1214 d34cab9f ths
1215 a6109ff1 Anthony Liguori
    s->vga.ds = graphic_console_init(vmsvga_update_display,
1216 a6109ff1 Anthony Liguori
                                     vmsvga_invalidate_display,
1217 a6109ff1 Anthony Liguori
                                     vmsvga_screen_dump,
1218 a6109ff1 Anthony Liguori
                                     vmsvga_text_update, s);
1219 a6109ff1 Anthony Liguori
1220 4445b0a6 Andrzej Zaborowski
1221 f351d050 Dave Airlie
    s->fifo_size = SVGA_FIFO_SIZE;
1222 b1950430 Avi Kivity
    memory_region_init_ram(&s->fifo_ram, NULL, "vmsvga.fifo", s->fifo_size);
1223 b1950430 Avi Kivity
    s->fifo_ptr = memory_region_get_ram_ptr(&s->fifo_ram);
1224 f351d050 Dave Airlie
1225 a4a2f59c Juan Quintela
    vga_common_init(&s->vga, vga_ram_size);
1226 a4a2f59c Juan Quintela
    vga_init(&s->vga);
1227 0be71e32 Alex Williamson
    vmstate_register(NULL, 0, &vmstate_vga_common, &s->vga);
1228 e93a5f4f balrog
1229 b5cc6e32 Anthony Liguori
    vmsvga_reset(s);
1230 d34cab9f ths
}
1231 d34cab9f ths
1232 b1950430 Avi Kivity
static uint64_t vmsvga_io_read(void *opaque, target_phys_addr_t addr,
1233 b1950430 Avi Kivity
                               unsigned size)
1234 1492a3c4 balrog
{
1235 b1950430 Avi Kivity
    struct vmsvga_state_s *s = opaque;
1236 b1950430 Avi Kivity
1237 b1950430 Avi Kivity
    switch (addr) {
1238 b1950430 Avi Kivity
    case SVGA_IO_MUL * SVGA_INDEX_PORT: return vmsvga_index_read(s, addr);
1239 b1950430 Avi Kivity
    case SVGA_IO_MUL * SVGA_VALUE_PORT: return vmsvga_value_read(s, addr);
1240 b1950430 Avi Kivity
    case SVGA_IO_MUL * SVGA_BIOS_PORT: return vmsvga_bios_read(s, addr);
1241 b1950430 Avi Kivity
    default: return -1u;
1242 b1950430 Avi Kivity
    }
1243 1492a3c4 balrog
}
1244 1492a3c4 balrog
1245 b1950430 Avi Kivity
static void vmsvga_io_write(void *opaque, target_phys_addr_t addr,
1246 b1950430 Avi Kivity
                            uint64_t data, unsigned size)
1247 3016d80b balrog
{
1248 b1950430 Avi Kivity
    struct vmsvga_state_s *s = opaque;
1249 ee3e41a9 Anthony Liguori
1250 b1950430 Avi Kivity
    switch (addr) {
1251 b1950430 Avi Kivity
    case SVGA_IO_MUL * SVGA_INDEX_PORT:
1252 b1950430 Avi Kivity
        return vmsvga_index_write(s, addr, data);
1253 b1950430 Avi Kivity
    case SVGA_IO_MUL * SVGA_VALUE_PORT:
1254 b1950430 Avi Kivity
        return vmsvga_value_write(s, addr, data);
1255 b1950430 Avi Kivity
    case SVGA_IO_MUL * SVGA_BIOS_PORT:
1256 b1950430 Avi Kivity
        return vmsvga_bios_write(s, addr, data);
1257 b1950430 Avi Kivity
    }
1258 3016d80b balrog
}
1259 3016d80b balrog
1260 b1950430 Avi Kivity
static const MemoryRegionOps vmsvga_io_ops = {
1261 b1950430 Avi Kivity
    .read = vmsvga_io_read,
1262 b1950430 Avi Kivity
    .write = vmsvga_io_write,
1263 b1950430 Avi Kivity
    .endianness = DEVICE_LITTLE_ENDIAN,
1264 b1950430 Avi Kivity
    .valid = {
1265 b1950430 Avi Kivity
        .min_access_size = 4,
1266 b1950430 Avi Kivity
        .max_access_size = 4,
1267 b1950430 Avi Kivity
    },
1268 b1950430 Avi Kivity
};
1269 f351d050 Dave Airlie
1270 81a322d4 Gerd Hoffmann
static int pci_vmsvga_initfn(PCIDevice *dev)
1271 d34cab9f ths
{
1272 a414c306 Gerd Hoffmann
    struct pci_vmsvga_state_s *s =
1273 a414c306 Gerd Hoffmann
        DO_UPCAST(struct pci_vmsvga_state_s, card, dev);
1274 b1950430 Avi Kivity
    MemoryRegion *iomem;
1275 b1950430 Avi Kivity
1276 b1950430 Avi Kivity
#ifdef DIRECT_VRAM
1277 b1950430 Avi Kivity
    DirectMem *directmem = qemu_malloc(sizeof(*directmem));
1278 b1950430 Avi Kivity
1279 b1950430 Avi Kivity
    iomem = &directmem->mr;
1280 b1950430 Avi Kivity
    memory_region_init_io(iomem, &vmsvga_vram_io_ops, &s->chip, "vmsvga",
1281 b1950430 Avi Kivity
                          memory_region_size(&s->chip.vga.vram));
1282 b1950430 Avi Kivity
#else
1283 b1950430 Avi Kivity
    iomem = &s->chip.vga.vram;
1284 b1950430 Avi Kivity
#endif
1285 b1950430 Avi Kivity
1286 b1950430 Avi Kivity
    vga_dirty_log_restart(&s->chip.vga);
1287 d34cab9f ths
1288 3fa0f955 Michael S. Tsirkin
    s->card.config[PCI_CACHE_LINE_SIZE]        = 0x08;                /* Cache line size */
1289 3fa0f955 Michael S. Tsirkin
    s->card.config[PCI_LATENCY_TIMER] = 0x40;                /* Latency timer */
1290 3fa0f955 Michael S. Tsirkin
    s->card.config[PCI_INTERRUPT_LINE] = 0xff;                /* End */
1291 d34cab9f ths
1292 b1950430 Avi Kivity
    memory_region_init_io(&s->io_bar, &vmsvga_io_ops, &s->chip,
1293 b1950430 Avi Kivity
                          "vmsvga-io", 0x10);
1294 b1950430 Avi Kivity
    pci_register_bar_region(&s->card, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1295 f351d050 Dave Airlie
1296 fbe1b595 Paul Brook
    vmsvga_init(&s->chip, VGA_RAM_SIZE);
1297 d34cab9f ths
1298 b1950430 Avi Kivity
    pci_register_bar_region(&s->card, 1, PCI_BASE_ADDRESS_MEM_PREFETCH, iomem);
1299 b1950430 Avi Kivity
    pci_register_bar_region(&s->card, 2, PCI_BASE_ADDRESS_MEM_PREFETCH,
1300 b1950430 Avi Kivity
                            &s->chip.fifo_ram);
1301 b1950430 Avi Kivity
1302 281a26b1 Gerd Hoffmann
    if (!dev->rom_bar) {
1303 281a26b1 Gerd Hoffmann
        /* compatibility with pc-0.13 and older */
1304 281a26b1 Gerd Hoffmann
        vga_init_vbe(&s->chip.vga);
1305 281a26b1 Gerd Hoffmann
    }
1306 281a26b1 Gerd Hoffmann
1307 81a322d4 Gerd Hoffmann
    return 0;
1308 d34cab9f ths
}
1309 a414c306 Gerd Hoffmann
1310 a414c306 Gerd Hoffmann
static PCIDeviceInfo vmsvga_info = {
1311 556cd098 Markus Armbruster
    .qdev.name    = "vmware-svga",
1312 a414c306 Gerd Hoffmann
    .qdev.size    = sizeof(struct pci_vmsvga_state_s),
1313 be73cfe2 Juan Quintela
    .qdev.vmsd    = &vmstate_vmware_vga,
1314 be92bbf7 Gerd Hoffmann
    .no_hotplug   = 1,
1315 a414c306 Gerd Hoffmann
    .init         = pci_vmsvga_initfn,
1316 4eccfec4 Gerd Hoffmann
    .romfile      = "vgabios-vmware.bin",
1317 310faaed Isaku Yamahata
1318 310faaed Isaku Yamahata
    .vendor_id    =  PCI_VENDOR_ID_VMWARE,
1319 310faaed Isaku Yamahata
    .device_id    = SVGA_PCI_DEVICE_ID,
1320 310faaed Isaku Yamahata
    .class_id     = PCI_CLASS_DISPLAY_VGA,
1321 310faaed Isaku Yamahata
    .subsystem_vendor_id = PCI_VENDOR_ID_VMWARE,
1322 310faaed Isaku Yamahata
    .subsystem_id = SVGA_PCI_DEVICE_ID,
1323 a414c306 Gerd Hoffmann
};
1324 a414c306 Gerd Hoffmann
1325 a414c306 Gerd Hoffmann
static void vmsvga_register(void)
1326 a414c306 Gerd Hoffmann
{
1327 a414c306 Gerd Hoffmann
    pci_qdev_register(&vmsvga_info);
1328 a414c306 Gerd Hoffmann
}
1329 a414c306 Gerd Hoffmann
device_init(vmsvga_register);