Statistics
| Branch: | Revision:

root / hw / vmware_vga.c @ 4e12cd94

History | View | Annotate | Download (34.5 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 87ecb68b pbrook
#include "console.h"
26 87ecb68b pbrook
#include "pci.h"
27 d34cab9f ths
28 d34cab9f ths
#define VERBOSE
29 d34cab9f ths
#define EMBED_STDVGA
30 d34cab9f ths
#undef DIRECT_VRAM
31 d34cab9f ths
#define HW_RECT_ACCEL
32 d34cab9f ths
#define HW_FILL_ACCEL
33 d34cab9f ths
#define HW_MOUSE_ACCEL
34 d34cab9f ths
35 d34cab9f ths
#ifdef EMBED_STDVGA
36 d34cab9f ths
# include "vga_int.h"
37 d34cab9f ths
#endif
38 d34cab9f ths
39 d34cab9f ths
struct vmsvga_state_s {
40 d34cab9f ths
#ifdef EMBED_STDVGA
41 4e12cd94 Avi Kivity
    VGACommonState vga;
42 d34cab9f ths
#endif
43 d34cab9f ths
44 d34cab9f ths
    int width;
45 d34cab9f ths
    int height;
46 d34cab9f ths
    int invalidated;
47 d34cab9f ths
    int depth;
48 d34cab9f ths
    int bypp;
49 d34cab9f ths
    int enable;
50 d34cab9f ths
    int config;
51 d34cab9f ths
    struct {
52 d34cab9f ths
        int id;
53 d34cab9f ths
        int x;
54 d34cab9f ths
        int y;
55 d34cab9f ths
        int on;
56 d34cab9f ths
    } cursor;
57 d34cab9f ths
58 d34cab9f ths
#ifndef EMBED_STDVGA
59 d34cab9f ths
    DisplayState *ds;
60 d34cab9f ths
    int vram_size;
61 6f9bc132 balrog
    ram_addr_t vram_offset;
62 b584726d pbrook
    uint8_t *vram_ptr;
63 d34cab9f ths
#endif
64 3016d80b balrog
    target_phys_addr_t vram_base;
65 d34cab9f ths
66 d34cab9f ths
    int index;
67 d34cab9f ths
    int scratch_size;
68 d34cab9f ths
    uint32_t *scratch;
69 d34cab9f ths
    int new_width;
70 d34cab9f ths
    int new_height;
71 d34cab9f ths
    uint32_t guest;
72 d34cab9f ths
    uint32_t svgaid;
73 d34cab9f ths
    uint32_t wred;
74 d34cab9f ths
    uint32_t wgreen;
75 d34cab9f ths
    uint32_t wblue;
76 d34cab9f ths
    int syncing;
77 d34cab9f ths
    int fb_size;
78 d34cab9f ths
79 d34cab9f ths
    union {
80 d34cab9f ths
        uint32_t *fifo;
81 d34cab9f ths
        struct __attribute__((__packed__)) {
82 d34cab9f ths
            uint32_t min;
83 d34cab9f ths
            uint32_t max;
84 d34cab9f ths
            uint32_t next_cmd;
85 d34cab9f ths
            uint32_t stop;
86 d34cab9f ths
            /* Add registers here when adding capabilities.  */
87 d34cab9f ths
            uint32_t fifo[0];
88 d34cab9f ths
        } *cmd;
89 d34cab9f ths
    };
90 d34cab9f ths
91 d34cab9f ths
#define REDRAW_FIFO_LEN        512
92 d34cab9f ths
    struct vmsvga_rect_s {
93 d34cab9f ths
        int x, y, w, h;
94 d34cab9f ths
    } redraw_fifo[REDRAW_FIFO_LEN];
95 d34cab9f ths
    int redraw_fifo_first, redraw_fifo_last;
96 d34cab9f ths
};
97 d34cab9f ths
98 d34cab9f ths
struct pci_vmsvga_state_s {
99 d34cab9f ths
    PCIDevice card;
100 d34cab9f ths
    struct vmsvga_state_s chip;
101 d34cab9f ths
};
102 d34cab9f ths
103 d34cab9f ths
#define SVGA_MAGIC                0x900000UL
104 d34cab9f ths
#define SVGA_MAKE_ID(ver)        (SVGA_MAGIC << 8 | (ver))
105 d34cab9f ths
#define SVGA_ID_0                SVGA_MAKE_ID(0)
106 d34cab9f ths
#define SVGA_ID_1                SVGA_MAKE_ID(1)
107 d34cab9f ths
#define SVGA_ID_2                SVGA_MAKE_ID(2)
108 d34cab9f ths
109 d34cab9f ths
#define SVGA_LEGACY_BASE_PORT        0x4560
110 d34cab9f ths
#define SVGA_INDEX_PORT                0x0
111 d34cab9f ths
#define SVGA_VALUE_PORT                0x1
112 d34cab9f ths
#define SVGA_BIOS_PORT                0x2
113 d34cab9f ths
114 d34cab9f ths
#define SVGA_VERSION_2
115 d34cab9f ths
116 d34cab9f ths
#ifdef SVGA_VERSION_2
117 d34cab9f ths
# define SVGA_ID                SVGA_ID_2
118 d34cab9f ths
# define SVGA_IO_BASE                SVGA_LEGACY_BASE_PORT
119 d34cab9f ths
# define SVGA_IO_MUL                1
120 d34cab9f ths
# define SVGA_FIFO_SIZE                0x10000
121 1f72aae5 balrog
# define SVGA_MEM_BASE                0xe0000000
122 d34cab9f ths
# define SVGA_PCI_DEVICE_ID        PCI_DEVICE_ID_VMWARE_SVGA2
123 d34cab9f ths
#else
124 d34cab9f ths
# define SVGA_ID                SVGA_ID_1
125 d34cab9f ths
# define SVGA_IO_BASE                SVGA_LEGACY_BASE_PORT
126 d34cab9f ths
# define SVGA_IO_MUL                4
127 d34cab9f ths
# define SVGA_FIFO_SIZE                0x10000
128 1f72aae5 balrog
# define SVGA_MEM_BASE                0xe0000000
129 d34cab9f ths
# define SVGA_PCI_DEVICE_ID        PCI_DEVICE_ID_VMWARE_SVGA
130 d34cab9f ths
#endif
131 d34cab9f ths
132 d34cab9f ths
enum {
133 d34cab9f ths
    /* ID 0, 1 and 2 registers */
134 d34cab9f ths
    SVGA_REG_ID = 0,
135 d34cab9f ths
    SVGA_REG_ENABLE = 1,
136 d34cab9f ths
    SVGA_REG_WIDTH = 2,
137 d34cab9f ths
    SVGA_REG_HEIGHT = 3,
138 d34cab9f ths
    SVGA_REG_MAX_WIDTH = 4,
139 d34cab9f ths
    SVGA_REG_MAX_HEIGHT = 5,
140 d34cab9f ths
    SVGA_REG_DEPTH = 6,
141 d34cab9f ths
    SVGA_REG_BITS_PER_PIXEL = 7,        /* Current bpp in the guest */
142 d34cab9f ths
    SVGA_REG_PSEUDOCOLOR = 8,
143 d34cab9f ths
    SVGA_REG_RED_MASK = 9,
144 d34cab9f ths
    SVGA_REG_GREEN_MASK = 10,
145 d34cab9f ths
    SVGA_REG_BLUE_MASK = 11,
146 d34cab9f ths
    SVGA_REG_BYTES_PER_LINE = 12,
147 d34cab9f ths
    SVGA_REG_FB_START = 13,
148 d34cab9f ths
    SVGA_REG_FB_OFFSET = 14,
149 d34cab9f ths
    SVGA_REG_VRAM_SIZE = 15,
150 d34cab9f ths
    SVGA_REG_FB_SIZE = 16,
151 d34cab9f ths
152 d34cab9f ths
    /* ID 1 and 2 registers */
153 d34cab9f ths
    SVGA_REG_CAPABILITIES = 17,
154 d34cab9f ths
    SVGA_REG_MEM_START = 18,                /* Memory for command FIFO */
155 d34cab9f ths
    SVGA_REG_MEM_SIZE = 19,
156 d34cab9f ths
    SVGA_REG_CONFIG_DONE = 20,                /* Set when memory area configured */
157 d34cab9f ths
    SVGA_REG_SYNC = 21,                        /* Write to force synchronization */
158 d34cab9f ths
    SVGA_REG_BUSY = 22,                        /* Read to check if sync is done */
159 d34cab9f ths
    SVGA_REG_GUEST_ID = 23,                /* Set guest OS identifier */
160 d34cab9f ths
    SVGA_REG_CURSOR_ID = 24,                /* ID of cursor */
161 d34cab9f ths
    SVGA_REG_CURSOR_X = 25,                /* Set cursor X position */
162 d34cab9f ths
    SVGA_REG_CURSOR_Y = 26,                /* Set cursor Y position */
163 d34cab9f ths
    SVGA_REG_CURSOR_ON = 27,                /* Turn cursor on/off */
164 d34cab9f ths
    SVGA_REG_HOST_BITS_PER_PIXEL = 28,        /* Current bpp in the host */
165 d34cab9f ths
    SVGA_REG_SCRATCH_SIZE = 29,                /* Number of scratch registers */
166 d34cab9f ths
    SVGA_REG_MEM_REGS = 30,                /* Number of FIFO registers */
167 d34cab9f ths
    SVGA_REG_NUM_DISPLAYS = 31,                /* Number of guest displays */
168 d34cab9f ths
    SVGA_REG_PITCHLOCK = 32,                /* Fixed pitch for all modes */
169 d34cab9f ths
170 d34cab9f ths
    SVGA_PALETTE_BASE = 1024,                /* Base of SVGA color map */
171 d34cab9f ths
    SVGA_PALETTE_END  = SVGA_PALETTE_BASE + 767,
172 d34cab9f ths
    SVGA_SCRATCH_BASE = SVGA_PALETTE_BASE + 768,
173 d34cab9f ths
};
174 d34cab9f ths
175 d34cab9f ths
#define SVGA_CAP_NONE                        0
176 d34cab9f ths
#define SVGA_CAP_RECT_FILL                (1 << 0)
177 d34cab9f ths
#define SVGA_CAP_RECT_COPY                (1 << 1)
178 d34cab9f ths
#define SVGA_CAP_RECT_PAT_FILL                (1 << 2)
179 d34cab9f ths
#define SVGA_CAP_LEGACY_OFFSCREEN        (1 << 3)
180 d34cab9f ths
#define SVGA_CAP_RASTER_OP                (1 << 4)
181 d34cab9f ths
#define SVGA_CAP_CURSOR                        (1 << 5)
182 d34cab9f ths
#define SVGA_CAP_CURSOR_BYPASS                (1 << 6)
183 d34cab9f ths
#define SVGA_CAP_CURSOR_BYPASS_2        (1 << 7)
184 d34cab9f ths
#define SVGA_CAP_8BIT_EMULATION                (1 << 8)
185 d34cab9f ths
#define SVGA_CAP_ALPHA_CURSOR                (1 << 9)
186 d34cab9f ths
#define SVGA_CAP_GLYPH                        (1 << 10)
187 d34cab9f ths
#define SVGA_CAP_GLYPH_CLIPPING                (1 << 11)
188 d34cab9f ths
#define SVGA_CAP_OFFSCREEN_1                (1 << 12)
189 d34cab9f ths
#define SVGA_CAP_ALPHA_BLEND                (1 << 13)
190 d34cab9f ths
#define SVGA_CAP_3D                        (1 << 14)
191 d34cab9f ths
#define SVGA_CAP_EXTENDED_FIFO                (1 << 15)
192 d34cab9f ths
#define SVGA_CAP_MULTIMON                (1 << 16)
193 d34cab9f ths
#define SVGA_CAP_PITCHLOCK                (1 << 17)
194 d34cab9f ths
195 d34cab9f ths
/*
196 d34cab9f ths
 * FIFO offsets (seen as an array of 32-bit words)
197 d34cab9f ths
 */
198 d34cab9f ths
enum {
199 d34cab9f ths
    /*
200 d34cab9f ths
     * The original defined FIFO offsets
201 d34cab9f ths
     */
202 d34cab9f ths
    SVGA_FIFO_MIN = 0,
203 d34cab9f ths
    SVGA_FIFO_MAX,        /* The distance from MIN to MAX must be at least 10K */
204 d34cab9f ths
    SVGA_FIFO_NEXT_CMD,
205 d34cab9f ths
    SVGA_FIFO_STOP,
206 d34cab9f ths
207 d34cab9f ths
    /*
208 d34cab9f ths
     * Additional offsets added as of SVGA_CAP_EXTENDED_FIFO
209 d34cab9f ths
     */
210 d34cab9f ths
    SVGA_FIFO_CAPABILITIES = 4,
211 d34cab9f ths
    SVGA_FIFO_FLAGS,
212 d34cab9f ths
    SVGA_FIFO_FENCE,
213 d34cab9f ths
    SVGA_FIFO_3D_HWVERSION,
214 d34cab9f ths
    SVGA_FIFO_PITCHLOCK,
215 d34cab9f ths
};
216 d34cab9f ths
217 d34cab9f ths
#define SVGA_FIFO_CAP_NONE                0
218 d34cab9f ths
#define SVGA_FIFO_CAP_FENCE                (1 << 0)
219 d34cab9f ths
#define SVGA_FIFO_CAP_ACCELFRONT        (1 << 1)
220 d34cab9f ths
#define SVGA_FIFO_CAP_PITCHLOCK                (1 << 2)
221 d34cab9f ths
222 d34cab9f ths
#define SVGA_FIFO_FLAG_NONE                0
223 d34cab9f ths
#define SVGA_FIFO_FLAG_ACCELFRONT        (1 << 0)
224 d34cab9f ths
225 d34cab9f ths
/* These values can probably be changed arbitrarily.  */
226 d34cab9f ths
#define SVGA_SCRATCH_SIZE                0x8000
227 d34cab9f ths
#define SVGA_MAX_WIDTH                        2360
228 d34cab9f ths
#define SVGA_MAX_HEIGHT                        1770
229 d34cab9f ths
230 d34cab9f ths
#ifdef VERBOSE
231 d34cab9f ths
# define GUEST_OS_BASE                0x5001
232 d34cab9f ths
static const char *vmsvga_guest_id[] = {
233 f707cfba balrog
    [0x00] = "Dos",
234 f707cfba balrog
    [0x01] = "Windows 3.1",
235 f707cfba balrog
    [0x02] = "Windows 95",
236 f707cfba balrog
    [0x03] = "Windows 98",
237 f707cfba balrog
    [0x04] = "Windows ME",
238 f707cfba balrog
    [0x05] = "Windows NT",
239 f707cfba balrog
    [0x06] = "Windows 2000",
240 f707cfba balrog
    [0x07] = "Linux",
241 f707cfba balrog
    [0x08] = "OS/2",
242 511d2b14 blueswir1
    [0x09] = "an unknown OS",
243 f707cfba balrog
    [0x0a] = "BSD",
244 f707cfba balrog
    [0x0b] = "Whistler",
245 511d2b14 blueswir1
    [0x0c] = "an unknown OS",
246 511d2b14 blueswir1
    [0x0d] = "an unknown OS",
247 511d2b14 blueswir1
    [0x0e] = "an unknown OS",
248 511d2b14 blueswir1
    [0x0f] = "an unknown OS",
249 511d2b14 blueswir1
    [0x10] = "an unknown OS",
250 511d2b14 blueswir1
    [0x11] = "an unknown OS",
251 511d2b14 blueswir1
    [0x12] = "an unknown OS",
252 511d2b14 blueswir1
    [0x13] = "an unknown OS",
253 511d2b14 blueswir1
    [0x14] = "an unknown OS",
254 f707cfba balrog
    [0x15] = "Windows 2003",
255 d34cab9f ths
};
256 d34cab9f ths
#endif
257 d34cab9f ths
258 d34cab9f ths
enum {
259 d34cab9f ths
    SVGA_CMD_INVALID_CMD = 0,
260 d34cab9f ths
    SVGA_CMD_UPDATE = 1,
261 d34cab9f ths
    SVGA_CMD_RECT_FILL = 2,
262 d34cab9f ths
    SVGA_CMD_RECT_COPY = 3,
263 d34cab9f ths
    SVGA_CMD_DEFINE_BITMAP = 4,
264 d34cab9f ths
    SVGA_CMD_DEFINE_BITMAP_SCANLINE = 5,
265 d34cab9f ths
    SVGA_CMD_DEFINE_PIXMAP = 6,
266 d34cab9f ths
    SVGA_CMD_DEFINE_PIXMAP_SCANLINE = 7,
267 d34cab9f ths
    SVGA_CMD_RECT_BITMAP_FILL = 8,
268 d34cab9f ths
    SVGA_CMD_RECT_PIXMAP_FILL = 9,
269 d34cab9f ths
    SVGA_CMD_RECT_BITMAP_COPY = 10,
270 d34cab9f ths
    SVGA_CMD_RECT_PIXMAP_COPY = 11,
271 d34cab9f ths
    SVGA_CMD_FREE_OBJECT = 12,
272 d34cab9f ths
    SVGA_CMD_RECT_ROP_FILL = 13,
273 d34cab9f ths
    SVGA_CMD_RECT_ROP_COPY = 14,
274 d34cab9f ths
    SVGA_CMD_RECT_ROP_BITMAP_FILL = 15,
275 d34cab9f ths
    SVGA_CMD_RECT_ROP_PIXMAP_FILL = 16,
276 d34cab9f ths
    SVGA_CMD_RECT_ROP_BITMAP_COPY = 17,
277 d34cab9f ths
    SVGA_CMD_RECT_ROP_PIXMAP_COPY = 18,
278 d34cab9f ths
    SVGA_CMD_DEFINE_CURSOR = 19,
279 d34cab9f ths
    SVGA_CMD_DISPLAY_CURSOR = 20,
280 d34cab9f ths
    SVGA_CMD_MOVE_CURSOR = 21,
281 d34cab9f ths
    SVGA_CMD_DEFINE_ALPHA_CURSOR = 22,
282 d34cab9f ths
    SVGA_CMD_DRAW_GLYPH = 23,
283 d34cab9f ths
    SVGA_CMD_DRAW_GLYPH_CLIPPED = 24,
284 d34cab9f ths
    SVGA_CMD_UPDATE_VERBOSE = 25,
285 d34cab9f ths
    SVGA_CMD_SURFACE_FILL = 26,
286 d34cab9f ths
    SVGA_CMD_SURFACE_COPY = 27,
287 d34cab9f ths
    SVGA_CMD_SURFACE_ALPHA_BLEND = 28,
288 d34cab9f ths
    SVGA_CMD_FRONT_ROP_FILL = 29,
289 d34cab9f ths
    SVGA_CMD_FENCE = 30,
290 d34cab9f ths
};
291 d34cab9f ths
292 d34cab9f ths
/* Legal values for the SVGA_REG_CURSOR_ON register in cursor bypass mode */
293 d34cab9f ths
enum {
294 d34cab9f ths
    SVGA_CURSOR_ON_HIDE = 0,
295 d34cab9f ths
    SVGA_CURSOR_ON_SHOW = 1,
296 d34cab9f ths
    SVGA_CURSOR_ON_REMOVE_FROM_FB = 2,
297 d34cab9f ths
    SVGA_CURSOR_ON_RESTORE_TO_FB = 3,
298 d34cab9f ths
};
299 d34cab9f ths
300 d34cab9f ths
static inline void vmsvga_update_rect(struct vmsvga_state_s *s,
301 d34cab9f ths
                int x, int y, int w, int h)
302 d34cab9f ths
{
303 d34cab9f ths
#ifndef DIRECT_VRAM
304 a8fbaf96 balrog
    int line;
305 a8fbaf96 balrog
    int bypl;
306 a8fbaf96 balrog
    int width;
307 a8fbaf96 balrog
    int start;
308 a8fbaf96 balrog
    uint8_t *src;
309 a8fbaf96 balrog
    uint8_t *dst;
310 a8fbaf96 balrog
311 a8fbaf96 balrog
    if (x + w > s->width) {
312 a8fbaf96 balrog
        fprintf(stderr, "%s: update width too large x: %d, w: %d\n",
313 a8fbaf96 balrog
                        __FUNCTION__, x, w);
314 a8fbaf96 balrog
        x = MIN(x, s->width);
315 a8fbaf96 balrog
        w = s->width - x;
316 a8fbaf96 balrog
    }
317 a8fbaf96 balrog
318 a8fbaf96 balrog
    if (y + h > s->height) {
319 a8fbaf96 balrog
        fprintf(stderr, "%s: update height too large y: %d, h: %d\n",
320 a8fbaf96 balrog
                        __FUNCTION__, y, h);
321 a8fbaf96 balrog
        y = MIN(y, s->height);
322 a8fbaf96 balrog
        h = s->height - y;
323 a8fbaf96 balrog
    }
324 a8fbaf96 balrog
325 a8fbaf96 balrog
    line = h;
326 a8fbaf96 balrog
    bypl = s->bypp * s->width;
327 a8fbaf96 balrog
    width = s->bypp * w;
328 a8fbaf96 balrog
    start = s->bypp * x + bypl * y;
329 4e12cd94 Avi Kivity
    src = s->vga.vram_ptr + start;
330 4e12cd94 Avi Kivity
    dst = ds_get_data(s->vga.ds) + start;
331 d34cab9f ths
332 d34cab9f ths
    for (; line > 0; line --, src += bypl, dst += bypl)
333 d34cab9f ths
        memcpy(dst, src, width);
334 d34cab9f ths
#endif
335 d34cab9f ths
336 4e12cd94 Avi Kivity
    dpy_update(s->vga.ds, x, y, w, h);
337 d34cab9f ths
}
338 d34cab9f ths
339 d34cab9f ths
static inline void vmsvga_update_screen(struct vmsvga_state_s *s)
340 d34cab9f ths
{
341 d34cab9f ths
#ifndef DIRECT_VRAM
342 4e12cd94 Avi Kivity
    memcpy(ds_get_data(s->vga.ds), s->vga.vram_ptr, s->bypp * s->width * s->height);
343 d34cab9f ths
#endif
344 d34cab9f ths
345 4e12cd94 Avi Kivity
    dpy_update(s->vga.ds, 0, 0, s->width, s->height);
346 d34cab9f ths
}
347 d34cab9f ths
348 d34cab9f ths
#ifdef DIRECT_VRAM
349 d34cab9f ths
# define vmsvga_update_rect_delayed        vmsvga_update_rect
350 d34cab9f ths
#else
351 d34cab9f ths
static inline void vmsvga_update_rect_delayed(struct vmsvga_state_s *s,
352 d34cab9f ths
                int x, int y, int w, int h)
353 d34cab9f ths
{
354 d34cab9f ths
    struct vmsvga_rect_s *rect = &s->redraw_fifo[s->redraw_fifo_last ++];
355 d34cab9f ths
    s->redraw_fifo_last &= REDRAW_FIFO_LEN - 1;
356 d34cab9f ths
    rect->x = x;
357 d34cab9f ths
    rect->y = y;
358 d34cab9f ths
    rect->w = w;
359 d34cab9f ths
    rect->h = h;
360 d34cab9f ths
}
361 d34cab9f ths
#endif
362 d34cab9f ths
363 d34cab9f ths
static inline void vmsvga_update_rect_flush(struct vmsvga_state_s *s)
364 d34cab9f ths
{
365 d34cab9f ths
    struct vmsvga_rect_s *rect;
366 d34cab9f ths
    if (s->invalidated) {
367 d34cab9f ths
        s->redraw_fifo_first = s->redraw_fifo_last;
368 d34cab9f ths
        return;
369 d34cab9f ths
    }
370 d34cab9f ths
    /* Overlapping region updates can be optimised out here - if someone
371 d34cab9f ths
     * knows a smart algorithm to do that, please share.  */
372 d34cab9f ths
    while (s->redraw_fifo_first != s->redraw_fifo_last) {
373 d34cab9f ths
        rect = &s->redraw_fifo[s->redraw_fifo_first ++];
374 d34cab9f ths
        s->redraw_fifo_first &= REDRAW_FIFO_LEN - 1;
375 d34cab9f ths
        vmsvga_update_rect(s, rect->x, rect->y, rect->w, rect->h);
376 d34cab9f ths
    }
377 d34cab9f ths
}
378 d34cab9f ths
379 d34cab9f ths
#ifdef HW_RECT_ACCEL
380 d34cab9f ths
static inline void vmsvga_copy_rect(struct vmsvga_state_s *s,
381 d34cab9f ths
                int x0, int y0, int x1, int y1, int w, int h)
382 d34cab9f ths
{
383 d34cab9f ths
# ifdef DIRECT_VRAM
384 0e1f5a0c aliguori
    uint8_t *vram = ds_get_data(s->ds);
385 d34cab9f ths
# else
386 4e12cd94 Avi Kivity
    uint8_t *vram = s->vga.vram_ptr;
387 d34cab9f ths
# endif
388 d34cab9f ths
    int bypl = s->bypp * s->width;
389 d34cab9f ths
    int width = s->bypp * w;
390 d34cab9f ths
    int line = h;
391 d34cab9f ths
    uint8_t *ptr[2];
392 d34cab9f ths
393 d34cab9f ths
# ifdef DIRECT_VRAM
394 d34cab9f ths
    if (s->ds->dpy_copy)
395 3023f332 aliguori
        qemu_console_copy(s->ds, x0, y0, x1, y1, w, h);
396 d34cab9f ths
    else
397 d34cab9f ths
# endif
398 d34cab9f ths
    {
399 d34cab9f ths
        if (y1 > y0) {
400 d34cab9f ths
            ptr[0] = vram + s->bypp * x0 + bypl * (y0 + h - 1);
401 d34cab9f ths
            ptr[1] = vram + s->bypp * x1 + bypl * (y1 + h - 1);
402 d34cab9f ths
            for (; line > 0; line --, ptr[0] -= bypl, ptr[1] -= bypl)
403 d34cab9f ths
                memmove(ptr[1], ptr[0], width);
404 d34cab9f ths
        } else {
405 d34cab9f ths
            ptr[0] = vram + s->bypp * x0 + bypl * y0;
406 d34cab9f ths
            ptr[1] = vram + s->bypp * x1 + bypl * y1;
407 d34cab9f ths
            for (; line > 0; line --, ptr[0] += bypl, ptr[1] += bypl)
408 d34cab9f ths
                memmove(ptr[1], ptr[0], width);
409 d34cab9f ths
        }
410 d34cab9f ths
    }
411 d34cab9f ths
412 d34cab9f ths
    vmsvga_update_rect_delayed(s, x1, y1, w, h);
413 d34cab9f ths
}
414 d34cab9f ths
#endif
415 d34cab9f ths
416 d34cab9f ths
#ifdef HW_FILL_ACCEL
417 d34cab9f ths
static inline void vmsvga_fill_rect(struct vmsvga_state_s *s,
418 d34cab9f ths
                uint32_t c, int x, int y, int w, int h)
419 d34cab9f ths
{
420 d34cab9f ths
# ifdef DIRECT_VRAM
421 0e1f5a0c aliguori
    uint8_t *vram = ds_get_data(s->ds);
422 d34cab9f ths
# else
423 4e12cd94 Avi Kivity
    uint8_t *vram = s->vga.vram_ptr;
424 d34cab9f ths
# endif
425 d34cab9f ths
    int bypp = s->bypp;
426 d34cab9f ths
    int bypl = bypp * s->width;
427 d34cab9f ths
    int width = bypp * w;
428 d34cab9f ths
    int line = h;
429 d34cab9f ths
    int column;
430 d34cab9f ths
    uint8_t *fst = vram + bypp * x + bypl * y;
431 d34cab9f ths
    uint8_t *dst;
432 d34cab9f ths
    uint8_t *src;
433 d34cab9f ths
    uint8_t col[4];
434 d34cab9f ths
435 d34cab9f ths
# ifdef DIRECT_VRAM
436 d34cab9f ths
    if (s->ds->dpy_fill)
437 d34cab9f ths
        s->ds->dpy_fill(s->ds, x, y, w, h, c);
438 d34cab9f ths
    else
439 d34cab9f ths
# endif
440 d34cab9f ths
    {
441 d34cab9f ths
        col[0] = c;
442 d34cab9f ths
        col[1] = c >> 8;
443 d34cab9f ths
        col[2] = c >> 16;
444 d34cab9f ths
        col[3] = c >> 24;
445 d34cab9f ths
446 d34cab9f ths
        if (line --) {
447 d34cab9f ths
            dst = fst;
448 d34cab9f ths
            src = col;
449 d34cab9f ths
            for (column = width; column > 0; column --) {
450 d34cab9f ths
                *(dst ++) = *(src ++);
451 d34cab9f ths
                if (src - col == bypp)
452 d34cab9f ths
                    src = col;
453 d34cab9f ths
            }
454 d34cab9f ths
            dst = fst;
455 d34cab9f ths
            for (; line > 0; line --) {
456 d34cab9f ths
                dst += bypl;
457 d34cab9f ths
                memcpy(dst, fst, width);
458 d34cab9f ths
            }
459 d34cab9f ths
        }
460 d34cab9f ths
    }
461 d34cab9f ths
462 d34cab9f ths
    vmsvga_update_rect_delayed(s, x, y, w, h);
463 d34cab9f ths
}
464 d34cab9f ths
#endif
465 d34cab9f ths
466 d34cab9f ths
struct vmsvga_cursor_definition_s {
467 d34cab9f ths
    int width;
468 d34cab9f ths
    int height;
469 d34cab9f ths
    int id;
470 d34cab9f ths
    int bpp;
471 d34cab9f ths
    int hot_x;
472 d34cab9f ths
    int hot_y;
473 d34cab9f ths
    uint32_t mask[1024];
474 d34cab9f ths
    uint32_t image[1024];
475 d34cab9f ths
};
476 d34cab9f ths
477 d34cab9f ths
#define SVGA_BITMAP_SIZE(w, h)                ((((w) + 31) >> 5) * (h))
478 d34cab9f ths
#define SVGA_PIXMAP_SIZE(w, h, bpp)        (((((w) * (bpp)) + 31) >> 5) * (h))
479 d34cab9f ths
480 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
481 d34cab9f ths
static inline void vmsvga_cursor_define(struct vmsvga_state_s *s,
482 d34cab9f ths
                struct vmsvga_cursor_definition_s *c)
483 d34cab9f ths
{
484 d34cab9f ths
    int i;
485 d34cab9f ths
    for (i = SVGA_BITMAP_SIZE(c->width, c->height) - 1; i >= 0; i --)
486 d34cab9f ths
        c->mask[i] = ~c->mask[i];
487 d34cab9f ths
488 4e12cd94 Avi Kivity
    if (s->vga.ds->cursor_define)
489 4e12cd94 Avi Kivity
        s->vga.ds->cursor_define(c->width, c->height, c->bpp, c->hot_x, c->hot_y,
490 d34cab9f ths
                        (uint8_t *) c->image, (uint8_t *) c->mask);
491 d34cab9f ths
}
492 d34cab9f ths
#endif
493 d34cab9f ths
494 ff9cf2cb balrog
#define CMD(f)        le32_to_cpu(s->cmd->f)
495 ff9cf2cb balrog
496 d34cab9f ths
static inline int vmsvga_fifo_empty(struct vmsvga_state_s *s)
497 d34cab9f ths
{
498 d34cab9f ths
    if (!s->config || !s->enable)
499 f707cfba balrog
        return 1;
500 d34cab9f ths
    return (s->cmd->next_cmd == s->cmd->stop);
501 d34cab9f ths
}
502 d34cab9f ths
503 ff9cf2cb balrog
static inline uint32_t vmsvga_fifo_read_raw(struct vmsvga_state_s *s)
504 d34cab9f ths
{
505 ff9cf2cb balrog
    uint32_t cmd = s->fifo[CMD(stop) >> 2];
506 ff9cf2cb balrog
    s->cmd->stop = cpu_to_le32(CMD(stop) + 4);
507 ff9cf2cb balrog
    if (CMD(stop) >= CMD(max))
508 d34cab9f ths
        s->cmd->stop = s->cmd->min;
509 d34cab9f ths
    return cmd;
510 d34cab9f ths
}
511 d34cab9f ths
512 ff9cf2cb balrog
static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)
513 ff9cf2cb balrog
{
514 ff9cf2cb balrog
    return le32_to_cpu(vmsvga_fifo_read_raw(s));
515 ff9cf2cb balrog
}
516 ff9cf2cb balrog
517 d34cab9f ths
static void vmsvga_fifo_run(struct vmsvga_state_s *s)
518 d34cab9f ths
{
519 d34cab9f ths
    uint32_t cmd, colour;
520 d34cab9f ths
    int args = 0;
521 d34cab9f ths
    int x, y, dx, dy, width, height;
522 d34cab9f ths
    struct vmsvga_cursor_definition_s cursor;
523 d34cab9f ths
    while (!vmsvga_fifo_empty(s))
524 d34cab9f ths
        switch (cmd = vmsvga_fifo_read(s)) {
525 d34cab9f ths
        case SVGA_CMD_UPDATE:
526 d34cab9f ths
        case SVGA_CMD_UPDATE_VERBOSE:
527 d34cab9f ths
            x = vmsvga_fifo_read(s);
528 d34cab9f ths
            y = vmsvga_fifo_read(s);
529 d34cab9f ths
            width = vmsvga_fifo_read(s);
530 d34cab9f ths
            height = vmsvga_fifo_read(s);
531 d34cab9f ths
            vmsvga_update_rect_delayed(s, x, y, width, height);
532 d34cab9f ths
            break;
533 d34cab9f ths
534 d34cab9f ths
        case SVGA_CMD_RECT_FILL:
535 d34cab9f ths
            colour = vmsvga_fifo_read(s);
536 d34cab9f ths
            x = vmsvga_fifo_read(s);
537 d34cab9f ths
            y = vmsvga_fifo_read(s);
538 d34cab9f ths
            width = vmsvga_fifo_read(s);
539 d34cab9f ths
            height = vmsvga_fifo_read(s);
540 d34cab9f ths
#ifdef HW_FILL_ACCEL
541 d34cab9f ths
            vmsvga_fill_rect(s, colour, x, y, width, height);
542 d34cab9f ths
            break;
543 d34cab9f ths
#else
544 d34cab9f ths
            goto badcmd;
545 d34cab9f ths
#endif
546 d34cab9f ths
547 d34cab9f ths
        case SVGA_CMD_RECT_COPY:
548 d34cab9f ths
            x = vmsvga_fifo_read(s);
549 d34cab9f ths
            y = vmsvga_fifo_read(s);
550 d34cab9f ths
            dx = vmsvga_fifo_read(s);
551 d34cab9f ths
            dy = vmsvga_fifo_read(s);
552 d34cab9f ths
            width = vmsvga_fifo_read(s);
553 d34cab9f ths
            height = vmsvga_fifo_read(s);
554 d34cab9f ths
#ifdef HW_RECT_ACCEL
555 d34cab9f ths
            vmsvga_copy_rect(s, x, y, dx, dy, width, height);
556 d34cab9f ths
            break;
557 d34cab9f ths
#else
558 d34cab9f ths
            goto badcmd;
559 d34cab9f ths
#endif
560 d34cab9f ths
561 d34cab9f ths
        case SVGA_CMD_DEFINE_CURSOR:
562 d34cab9f ths
            cursor.id = vmsvga_fifo_read(s);
563 d34cab9f ths
            cursor.hot_x = vmsvga_fifo_read(s);
564 d34cab9f ths
            cursor.hot_y = vmsvga_fifo_read(s);
565 d34cab9f ths
            cursor.width = x = vmsvga_fifo_read(s);
566 d34cab9f ths
            cursor.height = y = vmsvga_fifo_read(s);
567 d34cab9f ths
            vmsvga_fifo_read(s);
568 d34cab9f ths
            cursor.bpp = vmsvga_fifo_read(s);
569 d34cab9f ths
            for (args = 0; args < SVGA_BITMAP_SIZE(x, y); args ++)
570 ff9cf2cb balrog
                cursor.mask[args] = vmsvga_fifo_read_raw(s);
571 d34cab9f ths
            for (args = 0; args < SVGA_PIXMAP_SIZE(x, y, cursor.bpp); args ++)
572 ff9cf2cb balrog
                cursor.image[args] = vmsvga_fifo_read_raw(s);
573 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
574 d34cab9f ths
            vmsvga_cursor_define(s, &cursor);
575 d34cab9f ths
            break;
576 d34cab9f ths
#else
577 d34cab9f ths
            args = 0;
578 d34cab9f ths
            goto badcmd;
579 d34cab9f ths
#endif
580 d34cab9f ths
581 d34cab9f ths
        /*
582 d34cab9f ths
         * Other commands that we at least know the number of arguments
583 d34cab9f ths
         * for so we can avoid FIFO desync if driver uses them illegally.
584 d34cab9f ths
         */
585 d34cab9f ths
        case SVGA_CMD_DEFINE_ALPHA_CURSOR:
586 d34cab9f ths
            vmsvga_fifo_read(s);
587 d34cab9f ths
            vmsvga_fifo_read(s);
588 d34cab9f ths
            vmsvga_fifo_read(s);
589 d34cab9f ths
            x = vmsvga_fifo_read(s);
590 d34cab9f ths
            y = vmsvga_fifo_read(s);
591 d34cab9f ths
            args = x * y;
592 d34cab9f ths
            goto badcmd;
593 d34cab9f ths
        case SVGA_CMD_RECT_ROP_FILL:
594 d34cab9f ths
            args = 6;
595 d34cab9f ths
            goto badcmd;
596 d34cab9f ths
        case SVGA_CMD_RECT_ROP_COPY:
597 d34cab9f ths
            args = 7;
598 d34cab9f ths
            goto badcmd;
599 d34cab9f ths
        case SVGA_CMD_DRAW_GLYPH_CLIPPED:
600 d34cab9f ths
            vmsvga_fifo_read(s);
601 d34cab9f ths
            vmsvga_fifo_read(s);
602 d34cab9f ths
            args = 7 + (vmsvga_fifo_read(s) >> 2);
603 d34cab9f ths
            goto badcmd;
604 d34cab9f ths
        case SVGA_CMD_SURFACE_ALPHA_BLEND:
605 d34cab9f ths
            args = 12;
606 d34cab9f ths
            goto badcmd;
607 d34cab9f ths
608 d34cab9f ths
        /*
609 d34cab9f ths
         * Other commands that are not listed as depending on any
610 d34cab9f ths
         * CAPABILITIES bits, but are not described in the README either.
611 d34cab9f ths
         */
612 d34cab9f ths
        case SVGA_CMD_SURFACE_FILL:
613 d34cab9f ths
        case SVGA_CMD_SURFACE_COPY:
614 d34cab9f ths
        case SVGA_CMD_FRONT_ROP_FILL:
615 d34cab9f ths
        case SVGA_CMD_FENCE:
616 d34cab9f ths
        case SVGA_CMD_INVALID_CMD:
617 d34cab9f ths
            break; /* Nop */
618 d34cab9f ths
619 d34cab9f ths
        default:
620 d34cab9f ths
        badcmd:
621 d34cab9f ths
            while (args --)
622 d34cab9f ths
                vmsvga_fifo_read(s);
623 d34cab9f ths
            printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
624 d34cab9f ths
                            __FUNCTION__, cmd);
625 d34cab9f ths
            break;
626 d34cab9f ths
        }
627 d34cab9f ths
628 d34cab9f ths
    s->syncing = 0;
629 d34cab9f ths
}
630 d34cab9f ths
631 d34cab9f ths
static uint32_t vmsvga_index_read(void *opaque, uint32_t address)
632 d34cab9f ths
{
633 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
634 d34cab9f ths
    return s->index;
635 d34cab9f ths
}
636 d34cab9f ths
637 d34cab9f ths
static void vmsvga_index_write(void *opaque, uint32_t address, uint32_t index)
638 d34cab9f ths
{
639 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
640 d34cab9f ths
    s->index = index;
641 d34cab9f ths
}
642 d34cab9f ths
643 d34cab9f ths
static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
644 d34cab9f ths
{
645 d34cab9f ths
    uint32_t caps;
646 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
647 d34cab9f ths
    switch (s->index) {
648 d34cab9f ths
    case SVGA_REG_ID:
649 d34cab9f ths
        return s->svgaid;
650 d34cab9f ths
651 d34cab9f ths
    case SVGA_REG_ENABLE:
652 d34cab9f ths
        return s->enable;
653 d34cab9f ths
654 d34cab9f ths
    case SVGA_REG_WIDTH:
655 d34cab9f ths
        return s->width;
656 d34cab9f ths
657 d34cab9f ths
    case SVGA_REG_HEIGHT:
658 d34cab9f ths
        return s->height;
659 d34cab9f ths
660 d34cab9f ths
    case SVGA_REG_MAX_WIDTH:
661 d34cab9f ths
        return SVGA_MAX_WIDTH;
662 d34cab9f ths
663 d34cab9f ths
    case SVGA_REG_MAX_HEIGHT:
664 f707cfba balrog
        return SVGA_MAX_HEIGHT;
665 d34cab9f ths
666 d34cab9f ths
    case SVGA_REG_DEPTH:
667 d34cab9f ths
        return s->depth;
668 d34cab9f ths
669 d34cab9f ths
    case SVGA_REG_BITS_PER_PIXEL:
670 d34cab9f ths
        return (s->depth + 7) & ~7;
671 d34cab9f ths
672 d34cab9f ths
    case SVGA_REG_PSEUDOCOLOR:
673 d34cab9f ths
        return 0x0;
674 d34cab9f ths
675 d34cab9f ths
    case SVGA_REG_RED_MASK:
676 d34cab9f ths
        return s->wred;
677 d34cab9f ths
    case SVGA_REG_GREEN_MASK:
678 d34cab9f ths
        return s->wgreen;
679 d34cab9f ths
    case SVGA_REG_BLUE_MASK:
680 d34cab9f ths
        return s->wblue;
681 d34cab9f ths
682 d34cab9f ths
    case SVGA_REG_BYTES_PER_LINE:
683 d34cab9f ths
        return ((s->depth + 7) >> 3) * s->new_width;
684 d34cab9f ths
685 d34cab9f ths
    case SVGA_REG_FB_START:
686 3016d80b balrog
        return s->vram_base;
687 d34cab9f ths
688 d34cab9f ths
    case SVGA_REG_FB_OFFSET:
689 d34cab9f ths
        return 0x0;
690 d34cab9f ths
691 d34cab9f ths
    case SVGA_REG_VRAM_SIZE:
692 4e12cd94 Avi Kivity
        return s->vga.vram_size - SVGA_FIFO_SIZE;
693 d34cab9f ths
694 d34cab9f ths
    case SVGA_REG_FB_SIZE:
695 d34cab9f ths
        return s->fb_size;
696 d34cab9f ths
697 d34cab9f ths
    case SVGA_REG_CAPABILITIES:
698 d34cab9f ths
        caps = SVGA_CAP_NONE;
699 d34cab9f ths
#ifdef HW_RECT_ACCEL
700 d34cab9f ths
        caps |= SVGA_CAP_RECT_COPY;
701 d34cab9f ths
#endif
702 d34cab9f ths
#ifdef HW_FILL_ACCEL
703 d34cab9f ths
        caps |= SVGA_CAP_RECT_FILL;
704 d34cab9f ths
#endif
705 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
706 4e12cd94 Avi Kivity
        if (s->vga.ds->mouse_set)
707 d34cab9f ths
            caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
708 d34cab9f ths
                    SVGA_CAP_CURSOR_BYPASS;
709 d34cab9f ths
#endif
710 d34cab9f ths
        return caps;
711 d34cab9f ths
712 d34cab9f ths
    case SVGA_REG_MEM_START:
713 4e12cd94 Avi Kivity
        return s->vram_base + s->vga.vram_size - SVGA_FIFO_SIZE;
714 d34cab9f ths
715 d34cab9f ths
    case SVGA_REG_MEM_SIZE:
716 d34cab9f ths
        return SVGA_FIFO_SIZE;
717 d34cab9f ths
718 d34cab9f ths
    case SVGA_REG_CONFIG_DONE:
719 d34cab9f ths
        return s->config;
720 d34cab9f ths
721 d34cab9f ths
    case SVGA_REG_SYNC:
722 d34cab9f ths
    case SVGA_REG_BUSY:
723 d34cab9f ths
        return s->syncing;
724 d34cab9f ths
725 d34cab9f ths
    case SVGA_REG_GUEST_ID:
726 d34cab9f ths
        return s->guest;
727 d34cab9f ths
728 d34cab9f ths
    case SVGA_REG_CURSOR_ID:
729 d34cab9f ths
        return s->cursor.id;
730 d34cab9f ths
731 d34cab9f ths
    case SVGA_REG_CURSOR_X:
732 d34cab9f ths
        return s->cursor.x;
733 d34cab9f ths
734 d34cab9f ths
    case SVGA_REG_CURSOR_Y:
735 d34cab9f ths
        return s->cursor.x;
736 d34cab9f ths
737 d34cab9f ths
    case SVGA_REG_CURSOR_ON:
738 d34cab9f ths
        return s->cursor.on;
739 d34cab9f ths
740 d34cab9f ths
    case SVGA_REG_HOST_BITS_PER_PIXEL:
741 d34cab9f ths
        return (s->depth + 7) & ~7;
742 d34cab9f ths
743 d34cab9f ths
    case SVGA_REG_SCRATCH_SIZE:
744 d34cab9f ths
        return s->scratch_size;
745 d34cab9f ths
746 d34cab9f ths
    case SVGA_REG_MEM_REGS:
747 d34cab9f ths
    case SVGA_REG_NUM_DISPLAYS:
748 d34cab9f ths
    case SVGA_REG_PITCHLOCK:
749 d34cab9f ths
    case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
750 d34cab9f ths
        return 0;
751 d34cab9f ths
752 d34cab9f ths
    default:
753 d34cab9f ths
        if (s->index >= SVGA_SCRATCH_BASE &&
754 d34cab9f ths
                s->index < SVGA_SCRATCH_BASE + s->scratch_size)
755 d34cab9f ths
            return s->scratch[s->index - SVGA_SCRATCH_BASE];
756 d34cab9f ths
        printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
757 d34cab9f ths
    }
758 d34cab9f ths
759 d34cab9f ths
    return 0;
760 d34cab9f ths
}
761 d34cab9f ths
762 d34cab9f ths
static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)
763 d34cab9f ths
{
764 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
765 d34cab9f ths
    switch (s->index) {
766 d34cab9f ths
    case SVGA_REG_ID:
767 d34cab9f ths
        if (value == SVGA_ID_2 || value == SVGA_ID_1 || value == SVGA_ID_0)
768 d34cab9f ths
            s->svgaid = value;
769 d34cab9f ths
        break;
770 d34cab9f ths
771 d34cab9f ths
    case SVGA_REG_ENABLE:
772 f707cfba balrog
        s->enable = value;
773 f707cfba balrog
        s->config &= !!value;
774 d34cab9f ths
        s->width = -1;
775 d34cab9f ths
        s->height = -1;
776 d34cab9f ths
        s->invalidated = 1;
777 d34cab9f ths
#ifdef EMBED_STDVGA
778 4e12cd94 Avi Kivity
        s->vga.invalidate(&s->vga);
779 d34cab9f ths
#endif
780 d34cab9f ths
        if (s->enable)
781 d34cab9f ths
            s->fb_size = ((s->depth + 7) >> 3) * s->new_width * s->new_height;
782 d34cab9f ths
        break;
783 d34cab9f ths
784 d34cab9f ths
    case SVGA_REG_WIDTH:
785 d34cab9f ths
        s->new_width = value;
786 d34cab9f ths
        s->invalidated = 1;
787 d34cab9f ths
        break;
788 d34cab9f ths
789 d34cab9f ths
    case SVGA_REG_HEIGHT:
790 d34cab9f ths
        s->new_height = value;
791 d34cab9f ths
        s->invalidated = 1;
792 d34cab9f ths
        break;
793 d34cab9f ths
794 d34cab9f ths
    case SVGA_REG_DEPTH:
795 d34cab9f ths
    case SVGA_REG_BITS_PER_PIXEL:
796 d34cab9f ths
        if (value != s->depth) {
797 d34cab9f ths
            printf("%s: Bad colour depth: %i bits\n", __FUNCTION__, value);
798 d34cab9f ths
            s->config = 0;
799 d34cab9f ths
        }
800 d34cab9f ths
        break;
801 d34cab9f ths
802 d34cab9f ths
    case SVGA_REG_CONFIG_DONE:
803 d34cab9f ths
        if (value) {
804 4e12cd94 Avi Kivity
            s->fifo = (uint32_t *) &s->vga.vram_ptr[s->vga.vram_size - SVGA_FIFO_SIZE];
805 d34cab9f ths
            /* Check range and alignment.  */
806 ff9cf2cb balrog
            if ((CMD(min) | CMD(max) |
807 ff9cf2cb balrog
                        CMD(next_cmd) | CMD(stop)) & 3)
808 d34cab9f ths
                break;
809 ff9cf2cb balrog
            if (CMD(min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo)
810 d34cab9f ths
                break;
811 ff9cf2cb balrog
            if (CMD(max) > SVGA_FIFO_SIZE)
812 d34cab9f ths
                break;
813 ff9cf2cb balrog
            if (CMD(max) < CMD(min) + 10 * 1024)
814 d34cab9f ths
                break;
815 d34cab9f ths
        }
816 f707cfba balrog
        s->config = !!value;
817 d34cab9f ths
        break;
818 d34cab9f ths
819 d34cab9f ths
    case SVGA_REG_SYNC:
820 d34cab9f ths
        s->syncing = 1;
821 d34cab9f ths
        vmsvga_fifo_run(s); /* Or should we just wait for update_display? */
822 d34cab9f ths
        break;
823 d34cab9f ths
824 d34cab9f ths
    case SVGA_REG_GUEST_ID:
825 d34cab9f ths
        s->guest = value;
826 d34cab9f ths
#ifdef VERBOSE
827 d34cab9f ths
        if (value >= GUEST_OS_BASE && value < GUEST_OS_BASE +
828 b1503cda malc
                ARRAY_SIZE(vmsvga_guest_id))
829 d34cab9f ths
            printf("%s: guest runs %s.\n", __FUNCTION__,
830 d34cab9f ths
                            vmsvga_guest_id[value - GUEST_OS_BASE]);
831 d34cab9f ths
#endif
832 d34cab9f ths
        break;
833 d34cab9f ths
834 d34cab9f ths
    case SVGA_REG_CURSOR_ID:
835 d34cab9f ths
        s->cursor.id = value;
836 d34cab9f ths
        break;
837 d34cab9f ths
838 d34cab9f ths
    case SVGA_REG_CURSOR_X:
839 d34cab9f ths
        s->cursor.x = value;
840 d34cab9f ths
        break;
841 d34cab9f ths
842 d34cab9f ths
    case SVGA_REG_CURSOR_Y:
843 d34cab9f ths
        s->cursor.y = value;
844 d34cab9f ths
        break;
845 d34cab9f ths
846 d34cab9f ths
    case SVGA_REG_CURSOR_ON:
847 d34cab9f ths
        s->cursor.on |= (value == SVGA_CURSOR_ON_SHOW);
848 d34cab9f ths
        s->cursor.on &= (value != SVGA_CURSOR_ON_HIDE);
849 d34cab9f ths
#ifdef HW_MOUSE_ACCEL
850 4e12cd94 Avi Kivity
        if (s->vga.ds->mouse_set && value <= SVGA_CURSOR_ON_SHOW)
851 4e12cd94 Avi Kivity
            s->vga.ds->mouse_set(s->cursor.x, s->cursor.y, s->cursor.on);
852 d34cab9f ths
#endif
853 d34cab9f ths
        break;
854 d34cab9f ths
855 d34cab9f ths
    case SVGA_REG_MEM_REGS:
856 d34cab9f ths
    case SVGA_REG_NUM_DISPLAYS:
857 d34cab9f ths
    case SVGA_REG_PITCHLOCK:
858 d34cab9f ths
    case SVGA_PALETTE_BASE ... SVGA_PALETTE_END:
859 d34cab9f ths
        break;
860 d34cab9f ths
861 d34cab9f ths
    default:
862 d34cab9f ths
        if (s->index >= SVGA_SCRATCH_BASE &&
863 d34cab9f ths
                s->index < SVGA_SCRATCH_BASE + s->scratch_size) {
864 d34cab9f ths
            s->scratch[s->index - SVGA_SCRATCH_BASE] = value;
865 d34cab9f ths
            break;
866 d34cab9f ths
        }
867 d34cab9f ths
        printf("%s: Bad register %02x\n", __FUNCTION__, s->index);
868 d34cab9f ths
    }
869 d34cab9f ths
}
870 d34cab9f ths
871 d34cab9f ths
static uint32_t vmsvga_bios_read(void *opaque, uint32_t address)
872 d34cab9f ths
{
873 d34cab9f ths
    printf("%s: what are we supposed to return?\n", __FUNCTION__);
874 d34cab9f ths
    return 0xcafe;
875 d34cab9f ths
}
876 d34cab9f ths
877 d34cab9f ths
static void vmsvga_bios_write(void *opaque, uint32_t address, uint32_t data)
878 d34cab9f ths
{
879 d34cab9f ths
    printf("%s: what are we supposed to do with (%08x)?\n",
880 d34cab9f ths
                    __FUNCTION__, data);
881 d34cab9f ths
}
882 d34cab9f ths
883 d34cab9f ths
static inline void vmsvga_size(struct vmsvga_state_s *s)
884 d34cab9f ths
{
885 d34cab9f ths
    if (s->new_width != s->width || s->new_height != s->height) {
886 d34cab9f ths
        s->width = s->new_width;
887 d34cab9f ths
        s->height = s->new_height;
888 4e12cd94 Avi Kivity
        qemu_console_resize(s->vga.ds, s->width, s->height);
889 d34cab9f ths
        s->invalidated = 1;
890 d34cab9f ths
    }
891 d34cab9f ths
}
892 d34cab9f ths
893 d34cab9f ths
static void vmsvga_update_display(void *opaque)
894 d34cab9f ths
{
895 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
896 d34cab9f ths
    if (!s->enable) {
897 d34cab9f ths
#ifdef EMBED_STDVGA
898 4e12cd94 Avi Kivity
        s->vga.update(&s->vga);
899 d34cab9f ths
#endif
900 d34cab9f ths
        return;
901 d34cab9f ths
    }
902 d34cab9f ths
903 d34cab9f ths
    vmsvga_size(s);
904 d34cab9f ths
905 d34cab9f ths
    vmsvga_fifo_run(s);
906 d34cab9f ths
    vmsvga_update_rect_flush(s);
907 d34cab9f ths
908 d34cab9f ths
    /*
909 d34cab9f ths
     * Is it more efficient to look at vram VGA-dirty bits or wait
910 d34cab9f ths
     * for the driver to issue SVGA_CMD_UPDATE?
911 d34cab9f ths
     */
912 d34cab9f ths
    if (s->invalidated) {
913 d34cab9f ths
        s->invalidated = 0;
914 d34cab9f ths
        vmsvga_update_screen(s);
915 d34cab9f ths
    }
916 d34cab9f ths
}
917 d34cab9f ths
918 d34cab9f ths
static void vmsvga_reset(struct vmsvga_state_s *s)
919 d34cab9f ths
{
920 d34cab9f ths
    s->index = 0;
921 d34cab9f ths
    s->enable = 0;
922 d34cab9f ths
    s->config = 0;
923 d34cab9f ths
    s->width = -1;
924 d34cab9f ths
    s->height = -1;
925 d34cab9f ths
    s->svgaid = SVGA_ID;
926 3023f332 aliguori
    s->depth = 24;
927 d34cab9f ths
    s->bypp = (s->depth + 7) >> 3;
928 d34cab9f ths
    s->cursor.on = 0;
929 d34cab9f ths
    s->redraw_fifo_first = 0;
930 d34cab9f ths
    s->redraw_fifo_last = 0;
931 d34cab9f ths
    switch (s->depth) {
932 d34cab9f ths
    case 8:
933 d34cab9f ths
        s->wred   = 0x00000007;
934 d34cab9f ths
        s->wgreen = 0x00000038;
935 d34cab9f ths
        s->wblue  = 0x000000c0;
936 d34cab9f ths
        break;
937 d34cab9f ths
    case 15:
938 d34cab9f ths
        s->wred   = 0x0000001f;
939 d34cab9f ths
        s->wgreen = 0x000003e0;
940 d34cab9f ths
        s->wblue  = 0x00007c00;
941 d34cab9f ths
        break;
942 d34cab9f ths
    case 16:
943 d34cab9f ths
        s->wred   = 0x0000001f;
944 d34cab9f ths
        s->wgreen = 0x000007e0;
945 d34cab9f ths
        s->wblue  = 0x0000f800;
946 d34cab9f ths
        break;
947 d34cab9f ths
    case 24:
948 f707cfba balrog
        s->wred   = 0x00ff0000;
949 d34cab9f ths
        s->wgreen = 0x0000ff00;
950 f707cfba balrog
        s->wblue  = 0x000000ff;
951 d34cab9f ths
        break;
952 d34cab9f ths
    case 32:
953 f707cfba balrog
        s->wred   = 0x00ff0000;
954 d34cab9f ths
        s->wgreen = 0x0000ff00;
955 f707cfba balrog
        s->wblue  = 0x000000ff;
956 d34cab9f ths
        break;
957 d34cab9f ths
    }
958 d34cab9f ths
    s->syncing = 0;
959 d34cab9f ths
}
960 d34cab9f ths
961 d34cab9f ths
static void vmsvga_invalidate_display(void *opaque)
962 d34cab9f ths
{
963 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
964 d34cab9f ths
    if (!s->enable) {
965 d34cab9f ths
#ifdef EMBED_STDVGA
966 4e12cd94 Avi Kivity
        s->vga.invalidate(&s->vga);
967 d34cab9f ths
#endif
968 d34cab9f ths
        return;
969 d34cab9f ths
    }
970 d34cab9f ths
971 d34cab9f ths
    s->invalidated = 1;
972 d34cab9f ths
}
973 d34cab9f ths
974 f707cfba balrog
/* save the vga display in a PPM image even if no display is
975 f707cfba balrog
   available */
976 d34cab9f ths
static void vmsvga_screen_dump(void *opaque, const char *filename)
977 d34cab9f ths
{
978 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
979 d34cab9f ths
    if (!s->enable) {
980 d34cab9f ths
#ifdef EMBED_STDVGA
981 4e12cd94 Avi Kivity
        s->vga.screen_dump(&s->vga, filename);
982 d34cab9f ths
#endif
983 d34cab9f ths
        return;
984 d34cab9f ths
    }
985 d34cab9f ths
986 f707cfba balrog
    if (s->depth == 32) {
987 e07d630a aliguori
        DisplaySurface *ds = qemu_create_displaysurface_from(s->width,
988 4e12cd94 Avi Kivity
                s->height, 32, ds_get_linesize(s->vga.ds), s->vga.vram_ptr);
989 e07d630a aliguori
        ppm_save(filename, ds);
990 e07d630a aliguori
        qemu_free(ds);
991 f707cfba balrog
    }
992 d34cab9f ths
}
993 d34cab9f ths
994 4d3b6f6e balrog
static void vmsvga_text_update(void *opaque, console_ch_t *chardata)
995 4d3b6f6e balrog
{
996 4d3b6f6e balrog
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
997 4d3b6f6e balrog
998 4e12cd94 Avi Kivity
    if (s->vga.text_update)
999 4e12cd94 Avi Kivity
        s->vga.text_update(&s->vga, chardata);
1000 4d3b6f6e balrog
}
1001 4d3b6f6e balrog
1002 d34cab9f ths
#ifdef DIRECT_VRAM
1003 d34cab9f ths
static uint32_t vmsvga_vram_readb(void *opaque, target_phys_addr_t addr)
1004 d34cab9f ths
{
1005 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
1006 d34cab9f ths
    if (addr < s->fb_size)
1007 0e1f5a0c aliguori
        return *(uint8_t *) (ds_get_data(s->ds) + addr);
1008 d34cab9f ths
    else
1009 b584726d pbrook
        return *(uint8_t *) (s->vram_ptr + addr);
1010 d34cab9f ths
}
1011 d34cab9f ths
1012 d34cab9f ths
static uint32_t vmsvga_vram_readw(void *opaque, target_phys_addr_t addr)
1013 d34cab9f ths
{
1014 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
1015 d34cab9f ths
    if (addr < s->fb_size)
1016 0e1f5a0c aliguori
        return *(uint16_t *) (ds_get_data(s->ds) + addr);
1017 d34cab9f ths
    else
1018 b584726d pbrook
        return *(uint16_t *) (s->vram_ptr + addr);
1019 d34cab9f ths
}
1020 d34cab9f ths
1021 d34cab9f ths
static uint32_t vmsvga_vram_readl(void *opaque, target_phys_addr_t addr)
1022 d34cab9f ths
{
1023 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
1024 d34cab9f ths
    if (addr < s->fb_size)
1025 0e1f5a0c aliguori
        return *(uint32_t *) (ds_get_data(s->ds) + addr);
1026 d34cab9f ths
    else
1027 b584726d pbrook
        return *(uint32_t *) (s->vram_ptr + addr);
1028 d34cab9f ths
}
1029 d34cab9f ths
1030 d34cab9f ths
static void vmsvga_vram_writeb(void *opaque, target_phys_addr_t addr,
1031 d34cab9f ths
                uint32_t value)
1032 d34cab9f ths
{
1033 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
1034 d34cab9f ths
    if (addr < s->fb_size)
1035 0e1f5a0c aliguori
        *(uint8_t *) (ds_get_data(s->ds) + addr) = value;
1036 d34cab9f ths
    else
1037 b584726d pbrook
        *(uint8_t *) (s->vram_ptr + addr) = value;
1038 d34cab9f ths
}
1039 d34cab9f ths
1040 d34cab9f ths
static void vmsvga_vram_writew(void *opaque, target_phys_addr_t addr,
1041 d34cab9f ths
                uint32_t value)
1042 d34cab9f ths
{
1043 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
1044 d34cab9f ths
    if (addr < s->fb_size)
1045 0e1f5a0c aliguori
        *(uint16_t *) (ds_get_data(s->ds) + addr) = value;
1046 d34cab9f ths
    else
1047 b584726d pbrook
        *(uint16_t *) (s->vram_ptr + addr) = value;
1048 d34cab9f ths
}
1049 d34cab9f ths
1050 d34cab9f ths
static void vmsvga_vram_writel(void *opaque, target_phys_addr_t addr,
1051 d34cab9f ths
                uint32_t value)
1052 d34cab9f ths
{
1053 d34cab9f ths
    struct vmsvga_state_s *s = (struct vmsvga_state_s *) opaque;
1054 d34cab9f ths
    if (addr < s->fb_size)
1055 0e1f5a0c aliguori
        *(uint32_t *) (ds_get_data(s->ds) + addr) = value;
1056 d34cab9f ths
    else
1057 b584726d pbrook
        *(uint32_t *) (s->vram_ptr + addr) = value;
1058 d34cab9f ths
}
1059 d34cab9f ths
1060 d34cab9f ths
static CPUReadMemoryFunc *vmsvga_vram_read[] = {
1061 d34cab9f ths
    vmsvga_vram_readb,
1062 d34cab9f ths
    vmsvga_vram_readw,
1063 d34cab9f ths
    vmsvga_vram_readl,
1064 d34cab9f ths
};
1065 d34cab9f ths
1066 d34cab9f ths
static CPUWriteMemoryFunc *vmsvga_vram_write[] = {
1067 d34cab9f ths
    vmsvga_vram_writeb,
1068 d34cab9f ths
    vmsvga_vram_writew,
1069 d34cab9f ths
    vmsvga_vram_writel,
1070 d34cab9f ths
};
1071 d34cab9f ths
#endif
1072 d34cab9f ths
1073 d34cab9f ths
static void vmsvga_save(struct vmsvga_state_s *s, QEMUFile *f)
1074 d34cab9f ths
{
1075 bee8d684 ths
    qemu_put_be32(f, s->depth);
1076 bee8d684 ths
    qemu_put_be32(f, s->enable);
1077 bee8d684 ths
    qemu_put_be32(f, s->config);
1078 bee8d684 ths
    qemu_put_be32(f, s->cursor.id);
1079 bee8d684 ths
    qemu_put_be32(f, s->cursor.x);
1080 bee8d684 ths
    qemu_put_be32(f, s->cursor.y);
1081 bee8d684 ths
    qemu_put_be32(f, s->cursor.on);
1082 bee8d684 ths
    qemu_put_be32(f, s->index);
1083 d34cab9f ths
    qemu_put_buffer(f, (uint8_t *) s->scratch, s->scratch_size * 4);
1084 bee8d684 ths
    qemu_put_be32(f, s->new_width);
1085 bee8d684 ths
    qemu_put_be32(f, s->new_height);
1086 d34cab9f ths
    qemu_put_be32s(f, &s->guest);
1087 d34cab9f ths
    qemu_put_be32s(f, &s->svgaid);
1088 bee8d684 ths
    qemu_put_be32(f, s->syncing);
1089 bee8d684 ths
    qemu_put_be32(f, s->fb_size);
1090 d34cab9f ths
}
1091 d34cab9f ths
1092 d34cab9f ths
static int vmsvga_load(struct vmsvga_state_s *s, QEMUFile *f)
1093 d34cab9f ths
{
1094 d34cab9f ths
    int depth;
1095 bee8d684 ths
    depth=qemu_get_be32(f);
1096 bee8d684 ths
    s->enable=qemu_get_be32(f);
1097 bee8d684 ths
    s->config=qemu_get_be32(f);
1098 bee8d684 ths
    s->cursor.id=qemu_get_be32(f);
1099 bee8d684 ths
    s->cursor.x=qemu_get_be32(f);
1100 bee8d684 ths
    s->cursor.y=qemu_get_be32(f);
1101 bee8d684 ths
    s->cursor.on=qemu_get_be32(f);
1102 bee8d684 ths
    s->index=qemu_get_be32(f);
1103 d34cab9f ths
    qemu_get_buffer(f, (uint8_t *) s->scratch, s->scratch_size * 4);
1104 bee8d684 ths
    s->new_width=qemu_get_be32(f);
1105 bee8d684 ths
    s->new_height=qemu_get_be32(f);
1106 d34cab9f ths
    qemu_get_be32s(f, &s->guest);
1107 d34cab9f ths
    qemu_get_be32s(f, &s->svgaid);
1108 bee8d684 ths
    s->syncing=qemu_get_be32(f);
1109 bee8d684 ths
    s->fb_size=qemu_get_be32(f);
1110 d34cab9f ths
1111 d34cab9f ths
    if (s->enable && depth != s->depth) {
1112 d34cab9f ths
        printf("%s: need colour depth of %i bits to resume operation.\n",
1113 d34cab9f ths
                        __FUNCTION__, depth);
1114 d34cab9f ths
        return -EINVAL;
1115 d34cab9f ths
    }
1116 d34cab9f ths
1117 d34cab9f ths
    s->invalidated = 1;
1118 d34cab9f ths
    if (s->config)
1119 4e12cd94 Avi Kivity
        s->fifo = (uint32_t *) &s->vga.vram_ptr[s->vga.vram_size - SVGA_FIFO_SIZE];
1120 d34cab9f ths
1121 d34cab9f ths
    return 0;
1122 d34cab9f ths
}
1123 d34cab9f ths
1124 b584726d pbrook
static void vmsvga_init(struct vmsvga_state_s *s, int vga_ram_size)
1125 d34cab9f ths
{
1126 d34cab9f ths
    s->scratch_size = SVGA_SCRATCH_SIZE;
1127 d34cab9f ths
    s->scratch = (uint32_t *) qemu_malloc(s->scratch_size * 4);
1128 d34cab9f ths
1129 d34cab9f ths
    vmsvga_reset(s);
1130 d34cab9f ths
1131 d34cab9f ths
#ifdef EMBED_STDVGA
1132 b584726d pbrook
    vga_common_init((VGAState *) s, vga_ram_size);
1133 d34cab9f ths
    vga_init((VGAState *) s);
1134 b584726d pbrook
#else
1135 b584726d pbrook
    s->vram_size = vga_ram_size;
1136 b584726d pbrook
    s->vram_offset = qemu_ram_alloc(vga_ram_size);
1137 b584726d pbrook
    s->vram_ptr = qemu_get_ram_ptr(s->vram_offset);
1138 d34cab9f ths
#endif
1139 e93a5f4f balrog
1140 4e12cd94 Avi Kivity
    s->vga.ds = graphic_console_init(vmsvga_update_display,
1141 4e12cd94 Avi Kivity
                                     vmsvga_invalidate_display,
1142 4e12cd94 Avi Kivity
                                     vmsvga_screen_dump,
1143 4e12cd94 Avi Kivity
                                     vmsvga_text_update, &s->vga);
1144 931ea435 balrog
1145 931ea435 balrog
#ifdef CONFIG_BOCHS_VBE
1146 931ea435 balrog
    /* XXX: use optimized standard vga accesses */
1147 931ea435 balrog
    cpu_register_physical_memory(VBE_DISPI_LFB_PHYSICAL_ADDRESS,
1148 4e12cd94 Avi Kivity
                                 vga_ram_size, s->vga.vram_offset);
1149 931ea435 balrog
#endif
1150 d34cab9f ths
}
1151 d34cab9f ths
1152 d34cab9f ths
static void pci_vmsvga_save(QEMUFile *f, void *opaque)
1153 d34cab9f ths
{
1154 d34cab9f ths
    struct pci_vmsvga_state_s *s = (struct pci_vmsvga_state_s *) opaque;
1155 d34cab9f ths
    pci_device_save(&s->card, f);
1156 d34cab9f ths
    vmsvga_save(&s->chip, f);
1157 d34cab9f ths
}
1158 d34cab9f ths
1159 d34cab9f ths
static int pci_vmsvga_load(QEMUFile *f, void *opaque, int version_id)
1160 d34cab9f ths
{
1161 d34cab9f ths
    struct pci_vmsvga_state_s *s = (struct pci_vmsvga_state_s *) opaque;
1162 d34cab9f ths
    int ret;
1163 d34cab9f ths
1164 d34cab9f ths
    ret = pci_device_load(&s->card, f);
1165 d34cab9f ths
    if (ret < 0)
1166 d34cab9f ths
        return ret;
1167 d34cab9f ths
1168 d34cab9f ths
    ret = vmsvga_load(&s->chip, f);
1169 d34cab9f ths
    if (ret < 0)
1170 d34cab9f ths
        return ret;
1171 d34cab9f ths
1172 d34cab9f ths
    return 0;
1173 d34cab9f ths
}
1174 d34cab9f ths
1175 1492a3c4 balrog
static void pci_vmsvga_map_ioport(PCIDevice *pci_dev, int region_num,
1176 1492a3c4 balrog
                uint32_t addr, uint32_t size, int type)
1177 1492a3c4 balrog
{
1178 1492a3c4 balrog
    struct pci_vmsvga_state_s *d = (struct pci_vmsvga_state_s *) pci_dev;
1179 1492a3c4 balrog
    struct vmsvga_state_s *s = &d->chip;
1180 1492a3c4 balrog
1181 1492a3c4 balrog
    register_ioport_read(addr + SVGA_IO_MUL * SVGA_INDEX_PORT,
1182 1492a3c4 balrog
                    1, 4, vmsvga_index_read, s);
1183 1492a3c4 balrog
    register_ioport_write(addr + SVGA_IO_MUL * SVGA_INDEX_PORT,
1184 1492a3c4 balrog
                    1, 4, vmsvga_index_write, s);
1185 1492a3c4 balrog
    register_ioport_read(addr + SVGA_IO_MUL * SVGA_VALUE_PORT,
1186 1492a3c4 balrog
                    1, 4, vmsvga_value_read, s);
1187 1492a3c4 balrog
    register_ioport_write(addr + SVGA_IO_MUL * SVGA_VALUE_PORT,
1188 1492a3c4 balrog
                    1, 4, vmsvga_value_write, s);
1189 1492a3c4 balrog
    register_ioport_read(addr + SVGA_IO_MUL * SVGA_BIOS_PORT,
1190 1492a3c4 balrog
                    1, 4, vmsvga_bios_read, s);
1191 1492a3c4 balrog
    register_ioport_write(addr + SVGA_IO_MUL * SVGA_BIOS_PORT,
1192 1492a3c4 balrog
                    1, 4, vmsvga_bios_write, s);
1193 1492a3c4 balrog
}
1194 1492a3c4 balrog
1195 3016d80b balrog
static void pci_vmsvga_map_mem(PCIDevice *pci_dev, int region_num,
1196 3016d80b balrog
                uint32_t addr, uint32_t size, int type)
1197 3016d80b balrog
{
1198 3016d80b balrog
    struct pci_vmsvga_state_s *d = (struct pci_vmsvga_state_s *) pci_dev;
1199 3016d80b balrog
    struct vmsvga_state_s *s = &d->chip;
1200 ff9cf2cb balrog
    ram_addr_t iomemtype;
1201 3016d80b balrog
1202 3016d80b balrog
    s->vram_base = addr;
1203 3016d80b balrog
#ifdef DIRECT_VRAM
1204 3016d80b balrog
    iomemtype = cpu_register_io_memory(0, vmsvga_vram_read,
1205 3016d80b balrog
                    vmsvga_vram_write, s);
1206 3016d80b balrog
#else
1207 4e12cd94 Avi Kivity
    iomemtype = s->vga.vram_offset | IO_MEM_RAM;
1208 3016d80b balrog
#endif
1209 4e12cd94 Avi Kivity
    cpu_register_physical_memory(s->vram_base, s->vga.vram_size,
1210 3016d80b balrog
                    iomemtype);
1211 3016d80b balrog
}
1212 3016d80b balrog
1213 b584726d pbrook
void pci_vmsvga_init(PCIBus *bus, int vga_ram_size)
1214 d34cab9f ths
{
1215 d34cab9f ths
    struct pci_vmsvga_state_s *s;
1216 d34cab9f ths
1217 d34cab9f ths
    /* Setup PCI configuration */
1218 d34cab9f ths
    s = (struct pci_vmsvga_state_s *)
1219 d34cab9f ths
        pci_register_device(bus, "QEMUware SVGA",
1220 d34cab9f ths
                sizeof(struct pci_vmsvga_state_s), -1, 0, 0);
1221 deb54399 aliguori
    pci_config_set_vendor_id(s->card.config, PCI_VENDOR_ID_VMWARE);
1222 deb54399 aliguori
    pci_config_set_device_id(s->card.config, SVGA_PCI_DEVICE_ID);
1223 d34cab9f ths
    s->card.config[PCI_COMMAND]                = 0x07;                /* I/O + Memory */
1224 173a543b blueswir1
    pci_config_set_class(s->card.config, PCI_CLASS_DISPLAY_VGA);
1225 d34cab9f ths
    s->card.config[0x0c]                = 0x08;                /* Cache line size */
1226 d34cab9f ths
    s->card.config[0x0d]                = 0x40;                /* Latency timer */
1227 6407f373 Isaku Yamahata
    s->card.config[PCI_HEADER_TYPE]        = PCI_HEADER_TYPE_NORMAL;
1228 d34cab9f ths
    s->card.config[0x2c]                = PCI_VENDOR_ID_VMWARE & 0xff;
1229 d34cab9f ths
    s->card.config[0x2d]                = PCI_VENDOR_ID_VMWARE >> 8;
1230 d34cab9f ths
    s->card.config[0x2e]                = SVGA_PCI_DEVICE_ID & 0xff;
1231 d34cab9f ths
    s->card.config[0x2f]                = SVGA_PCI_DEVICE_ID >> 8;
1232 d34cab9f ths
    s->card.config[0x3c]                = 0xff;                /* End */
1233 d34cab9f ths
1234 1492a3c4 balrog
    pci_register_io_region(&s->card, 0, 0x10,
1235 1492a3c4 balrog
                    PCI_ADDRESS_SPACE_IO, pci_vmsvga_map_ioport);
1236 2408b77b aurel32
    pci_register_io_region(&s->card, 1, vga_ram_size,
1237 3016d80b balrog
                    PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_vmsvga_map_mem);
1238 1492a3c4 balrog
1239 b584726d pbrook
    vmsvga_init(&s->chip, vga_ram_size);
1240 d34cab9f ths
1241 d34cab9f ths
    register_savevm("vmware_vga", 0, 0, pci_vmsvga_save, pci_vmsvga_load, s);
1242 d34cab9f ths
}