xref: /qemu/hw/display/ati.c (revision d634c883ca07c28da2cb84c019659694f05d8b3a)
1862b4a29SBALATON Zoltan /*
2862b4a29SBALATON Zoltan  * QEMU ATI SVGA emulation
3862b4a29SBALATON Zoltan  *
4862b4a29SBALATON Zoltan  * Copyright (c) 2019 BALATON Zoltan
5862b4a29SBALATON Zoltan  *
6862b4a29SBALATON Zoltan  * This work is licensed under the GNU GPL license version 2 or later.
7862b4a29SBALATON Zoltan  */
8862b4a29SBALATON Zoltan 
9862b4a29SBALATON Zoltan /*
10862b4a29SBALATON Zoltan  * WARNING:
11862b4a29SBALATON Zoltan  * This is very incomplete and only enough for Linux console and some
12862b4a29SBALATON Zoltan  * unaccelerated X output at the moment.
13862b4a29SBALATON Zoltan  * Currently it's little more than a frame buffer with minimal functions,
14862b4a29SBALATON Zoltan  * other more advanced features of the hardware are yet to be implemented.
15862b4a29SBALATON Zoltan  * We only aim for Rage 128 Pro (and some RV100) and 2D only at first,
16862b4a29SBALATON Zoltan  * No 3D at all yet (maybe after 2D works, but feel free to improve it)
17862b4a29SBALATON Zoltan  */
18862b4a29SBALATON Zoltan 
19bbfff196SMarkus Armbruster #include "qemu/osdep.h"
20862b4a29SBALATON Zoltan #include "ati_int.h"
21862b4a29SBALATON Zoltan #include "ati_regs.h"
22aab0e2a6SGerd Hoffmann #include "vga-access.h"
23a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
24862b4a29SBALATON Zoltan #include "vga_regs.h"
25862b4a29SBALATON Zoltan #include "qemu/log.h"
260b8fa32fSMarkus Armbruster #include "qemu/module.h"
27862b4a29SBALATON Zoltan #include "qemu/error-report.h"
28862b4a29SBALATON Zoltan #include "qapi/error.h"
29862b4a29SBALATON Zoltan #include "ui/console.h"
30c82c7336SBALATON Zoltan #include "hw/display/i2c-ddc.h"
31862b4a29SBALATON Zoltan #include "trace.h"
32862b4a29SBALATON Zoltan 
33862b4a29SBALATON Zoltan #define ATI_DEBUG_HW_CURSOR 0
34862b4a29SBALATON Zoltan 
35862b4a29SBALATON Zoltan static const struct {
36862b4a29SBALATON Zoltan     const char *name;
37862b4a29SBALATON Zoltan     uint16_t dev_id;
38862b4a29SBALATON Zoltan } ati_model_aliases[] = {
39862b4a29SBALATON Zoltan     { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
40862b4a29SBALATON Zoltan     { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
41862b4a29SBALATON Zoltan };
42862b4a29SBALATON Zoltan 
43862b4a29SBALATON Zoltan enum { VGA_MODE, EXT_MODE };
44862b4a29SBALATON Zoltan 
45862b4a29SBALATON Zoltan static void ati_vga_switch_mode(ATIVGAState *s)
46862b4a29SBALATON Zoltan {
47862b4a29SBALATON Zoltan     DPRINTF("%d -> %d\n",
48862b4a29SBALATON Zoltan             s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
49862b4a29SBALATON Zoltan     if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
50862b4a29SBALATON Zoltan         /* Extended mode enabled */
51862b4a29SBALATON Zoltan         s->mode = EXT_MODE;
52862b4a29SBALATON Zoltan         if (s->regs.crtc_gen_cntl & CRTC2_EN) {
53862b4a29SBALATON Zoltan             /* CRT controller enabled, use CRTC values */
54c026350aSBALATON Zoltan             /* FIXME Should these be the same as VGA CRTC regs? */
55862b4a29SBALATON Zoltan             uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
56862b4a29SBALATON Zoltan             int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
57862b4a29SBALATON Zoltan             int bpp = 0;
58862b4a29SBALATON Zoltan             int h, v;
59862b4a29SBALATON Zoltan 
60862b4a29SBALATON Zoltan             if (s->regs.crtc_h_total_disp == 0) {
61862b4a29SBALATON Zoltan                 s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
62862b4a29SBALATON Zoltan             }
63862b4a29SBALATON Zoltan             if (s->regs.crtc_v_total_disp == 0) {
64862b4a29SBALATON Zoltan                 s->regs.crtc_v_total_disp = (480 - 1) << 16;
65862b4a29SBALATON Zoltan             }
66862b4a29SBALATON Zoltan             h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
67862b4a29SBALATON Zoltan             v = (s->regs.crtc_v_total_disp >> 16) + 1;
68862b4a29SBALATON Zoltan             switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
69862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_4BPP:
70862b4a29SBALATON Zoltan                 bpp = 4;
71862b4a29SBALATON Zoltan                 break;
72862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_8BPP:
73862b4a29SBALATON Zoltan                 bpp = 8;
74862b4a29SBALATON Zoltan                 break;
75862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_15BPP:
76862b4a29SBALATON Zoltan                 bpp = 15;
77862b4a29SBALATON Zoltan                 break;
78862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_16BPP:
79862b4a29SBALATON Zoltan                 bpp = 16;
80862b4a29SBALATON Zoltan                 break;
81862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_24BPP:
82862b4a29SBALATON Zoltan                 bpp = 24;
83862b4a29SBALATON Zoltan                 break;
84862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_32BPP:
85862b4a29SBALATON Zoltan                 bpp = 32;
86862b4a29SBALATON Zoltan                 break;
87862b4a29SBALATON Zoltan             default:
88862b4a29SBALATON Zoltan                 qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
89862b4a29SBALATON Zoltan             }
90862b4a29SBALATON Zoltan             assert(bpp != 0);
91862b4a29SBALATON Zoltan             DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
92862b4a29SBALATON Zoltan             vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
93862b4a29SBALATON Zoltan             vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
948bb9a2b2SBALATON Zoltan             s->vga.big_endian_fb = (s->regs.config_cntl & APER_0_ENDIAN ||
958bb9a2b2SBALATON Zoltan                                     s->regs.config_cntl & APER_1_ENDIAN ?
968bb9a2b2SBALATON Zoltan                                     true : false);
97862b4a29SBALATON Zoltan             /* reset VBE regs then set up mode */
98862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
99862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
100862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
101862b4a29SBALATON Zoltan             /* enable mode via ioport so it updates vga regs */
102862b4a29SBALATON Zoltan             vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
103862b4a29SBALATON Zoltan             vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
104862b4a29SBALATON Zoltan                 VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
105862b4a29SBALATON Zoltan                 (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
106862b4a29SBALATON Zoltan             /* now set offset and stride after enable as that resets these */
107862b4a29SBALATON Zoltan             if (stride) {
108c026350aSBALATON Zoltan                 int bypp = DIV_ROUND_UP(bpp, BITS_PER_BYTE);
109c026350aSBALATON Zoltan 
110862b4a29SBALATON Zoltan                 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
111862b4a29SBALATON Zoltan                 vbe_ioport_write_data(&s->vga, 0, stride);
112c026350aSBALATON Zoltan                 stride *= bypp;
113c026350aSBALATON Zoltan                 if (offs % stride) {
114c026350aSBALATON Zoltan                     DPRINTF("CRTC offset is not multiple of pitch\n");
115c026350aSBALATON Zoltan                     vbe_ioport_write_index(&s->vga, 0,
116c026350aSBALATON Zoltan                                            VBE_DISPI_INDEX_X_OFFSET);
117c026350aSBALATON Zoltan                     vbe_ioport_write_data(&s->vga, 0, offs % stride / bypp);
118c026350aSBALATON Zoltan                 }
119862b4a29SBALATON Zoltan                 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
120862b4a29SBALATON Zoltan                 vbe_ioport_write_data(&s->vga, 0, offs / stride);
121c026350aSBALATON Zoltan                 DPRINTF("VBE offset (%d,%d), vbe_start_addr=%x\n",
122c026350aSBALATON Zoltan                         s->vga.vbe_regs[VBE_DISPI_INDEX_X_OFFSET],
123c026350aSBALATON Zoltan                         s->vga.vbe_regs[VBE_DISPI_INDEX_Y_OFFSET],
124c026350aSBALATON Zoltan                         s->vga.vbe_start_addr);
125862b4a29SBALATON Zoltan             }
126862b4a29SBALATON Zoltan         }
127862b4a29SBALATON Zoltan     } else {
128862b4a29SBALATON Zoltan         /* VGA mode enabled */
129862b4a29SBALATON Zoltan         s->mode = VGA_MODE;
130862b4a29SBALATON Zoltan         vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
131862b4a29SBALATON Zoltan         vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
132862b4a29SBALATON Zoltan     }
133862b4a29SBALATON Zoltan }
134862b4a29SBALATON Zoltan 
135862b4a29SBALATON Zoltan /* Used by host side hardware cursor */
136862b4a29SBALATON Zoltan static void ati_cursor_define(ATIVGAState *s)
137862b4a29SBALATON Zoltan {
138862b4a29SBALATON Zoltan     uint8_t data[1024];
139aab0e2a6SGerd Hoffmann     uint32_t srcoff;
140862b4a29SBALATON Zoltan     int i, j, idx = 0;
141862b4a29SBALATON Zoltan 
142862b4a29SBALATON Zoltan     if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
143862b4a29SBALATON Zoltan         return; /* Do not update cursor if locked or rendered by guest */
144862b4a29SBALATON Zoltan     }
145862b4a29SBALATON Zoltan     /* FIXME handle cur_hv_offs correctly */
146aab0e2a6SGerd Hoffmann     srcoff = s->regs.cur_offset -
147747d7ad2SBALATON Zoltan         (s->regs.cur_hv_offs >> 16) - (s->regs.cur_hv_offs & 0xffff) * 16;
148862b4a29SBALATON Zoltan     for (i = 0; i < 64; i++) {
149862b4a29SBALATON Zoltan         for (j = 0; j < 8; j++, idx++) {
150aab0e2a6SGerd Hoffmann             data[idx] = vga_read_byte(&s->vga, srcoff + i * 16 + j);
151aab0e2a6SGerd Hoffmann             data[512 + idx] = vga_read_byte(&s->vga, srcoff + i * 16 + j + 8);
152862b4a29SBALATON Zoltan         }
153862b4a29SBALATON Zoltan     }
154862b4a29SBALATON Zoltan     if (!s->cursor) {
155862b4a29SBALATON Zoltan         s->cursor = cursor_alloc(64, 64);
156862b4a29SBALATON Zoltan     }
157862b4a29SBALATON Zoltan     cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
158862b4a29SBALATON Zoltan                     &data[512], 1, &data[0]);
159862b4a29SBALATON Zoltan     dpy_cursor_define(s->vga.con, s->cursor);
160862b4a29SBALATON Zoltan }
161862b4a29SBALATON Zoltan 
162862b4a29SBALATON Zoltan /* Alternatively support guest rendered hardware cursor */
163862b4a29SBALATON Zoltan static void ati_cursor_invalidate(VGACommonState *vga)
164862b4a29SBALATON Zoltan {
165862b4a29SBALATON Zoltan     ATIVGAState *s = container_of(vga, ATIVGAState, vga);
166862b4a29SBALATON Zoltan     int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
167862b4a29SBALATON Zoltan 
168862b4a29SBALATON Zoltan     if (s->regs.cur_offset & BIT(31)) {
169862b4a29SBALATON Zoltan         return; /* Do not update cursor if locked */
170862b4a29SBALATON Zoltan     }
171862b4a29SBALATON Zoltan     if (s->cursor_size != size ||
172862b4a29SBALATON Zoltan         vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
173862b4a29SBALATON Zoltan         vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
174862b4a29SBALATON Zoltan         s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
175862b4a29SBALATON Zoltan         (s->regs.cur_hv_offs & 0xffff) * 16) {
176862b4a29SBALATON Zoltan         /* Remove old cursor then update and show new one if needed */
177862b4a29SBALATON Zoltan         vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
178862b4a29SBALATON Zoltan         vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
179862b4a29SBALATON Zoltan         vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
180862b4a29SBALATON Zoltan         s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
181862b4a29SBALATON Zoltan                            (s->regs.cur_hv_offs & 0xffff) * 16;
182862b4a29SBALATON Zoltan         s->cursor_size = size;
183862b4a29SBALATON Zoltan         if (size) {
184862b4a29SBALATON Zoltan             vga_invalidate_scanlines(vga,
185862b4a29SBALATON Zoltan                                      vga->hw_cursor_y, vga->hw_cursor_y + 63);
186862b4a29SBALATON Zoltan         }
187862b4a29SBALATON Zoltan     }
188862b4a29SBALATON Zoltan }
189862b4a29SBALATON Zoltan 
190862b4a29SBALATON Zoltan static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
191862b4a29SBALATON Zoltan {
192862b4a29SBALATON Zoltan     ATIVGAState *s = container_of(vga, ATIVGAState, vga);
193aab0e2a6SGerd Hoffmann     uint32_t srcoff;
194862b4a29SBALATON Zoltan     uint32_t *dp = (uint32_t *)d;
195862b4a29SBALATON Zoltan     int i, j, h;
196862b4a29SBALATON Zoltan 
197862b4a29SBALATON Zoltan     if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
198862b4a29SBALATON Zoltan         scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
199862b4a29SBALATON Zoltan         scr_y > s->regs.crtc_v_total_disp >> 16) {
200862b4a29SBALATON Zoltan         return;
201862b4a29SBALATON Zoltan     }
202862b4a29SBALATON Zoltan     /* FIXME handle cur_hv_offs correctly */
203aab0e2a6SGerd Hoffmann     srcoff = s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
204862b4a29SBALATON Zoltan     dp = &dp[vga->hw_cursor_x];
205862b4a29SBALATON Zoltan     h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
206862b4a29SBALATON Zoltan     for (i = 0; i < 8; i++) {
207862b4a29SBALATON Zoltan         uint32_t color;
208aab0e2a6SGerd Hoffmann         uint8_t abits = vga_read_byte(vga, srcoff + i);
209aab0e2a6SGerd Hoffmann         uint8_t xbits = vga_read_byte(vga, srcoff + i + 8);
210862b4a29SBALATON Zoltan         for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
211862b4a29SBALATON Zoltan             if (abits & BIT(7)) {
212862b4a29SBALATON Zoltan                 if (xbits & BIT(7)) {
213862b4a29SBALATON Zoltan                     color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
214862b4a29SBALATON Zoltan                 } else {
215862b4a29SBALATON Zoltan                     continue; /* transparent, no change */
216862b4a29SBALATON Zoltan                 }
217862b4a29SBALATON Zoltan             } else {
218862b4a29SBALATON Zoltan                 color = (xbits & BIT(7) ? s->regs.cur_color1 :
21950bc6af5SBALATON Zoltan                                           s->regs.cur_color0) | 0xff000000;
220862b4a29SBALATON Zoltan             }
221862b4a29SBALATON Zoltan             if (vga->hw_cursor_x + i * 8 + j >= h) {
222862b4a29SBALATON Zoltan                 return; /* end of screen, don't span to next line */
223862b4a29SBALATON Zoltan             }
224862b4a29SBALATON Zoltan             dp[i * 8 + j] = color;
225862b4a29SBALATON Zoltan         }
226862b4a29SBALATON Zoltan     }
227862b4a29SBALATON Zoltan }
228862b4a29SBALATON Zoltan 
229c82c7336SBALATON Zoltan static uint64_t ati_i2c(bitbang_i2c_interface *i2c, uint64_t data, int base)
230c82c7336SBALATON Zoltan {
231c82c7336SBALATON Zoltan     bool c = (data & BIT(base + 17) ? !!(data & BIT(base + 1)) : 1);
232c82c7336SBALATON Zoltan     bool d = (data & BIT(base + 16) ? !!(data & BIT(base)) : 1);
233c82c7336SBALATON Zoltan 
234c82c7336SBALATON Zoltan     bitbang_i2c_set(i2c, BITBANG_I2C_SCL, c);
235c82c7336SBALATON Zoltan     d = bitbang_i2c_set(i2c, BITBANG_I2C_SDA, d);
236c82c7336SBALATON Zoltan 
237c82c7336SBALATON Zoltan     data &= ~0xf00ULL;
238c82c7336SBALATON Zoltan     if (c) {
239c82c7336SBALATON Zoltan         data |= BIT(base + 9);
240c82c7336SBALATON Zoltan     }
241c82c7336SBALATON Zoltan     if (d) {
242c82c7336SBALATON Zoltan         data |= BIT(base + 8);
243c82c7336SBALATON Zoltan     }
244c82c7336SBALATON Zoltan     return data;
245c82c7336SBALATON Zoltan }
246c82c7336SBALATON Zoltan 
247b7105d28SBALATON Zoltan static void ati_vga_update_irq(ATIVGAState *s)
248b7105d28SBALATON Zoltan {
249b7105d28SBALATON Zoltan     pci_set_irq(&s->dev, !!(s->regs.gen_int_status & s->regs.gen_int_cntl));
250b7105d28SBALATON Zoltan }
251b7105d28SBALATON Zoltan 
252b7105d28SBALATON Zoltan static void ati_vga_vblank_irq(void *opaque)
253b7105d28SBALATON Zoltan {
254b7105d28SBALATON Zoltan     ATIVGAState *s = opaque;
255b7105d28SBALATON Zoltan 
256b7105d28SBALATON Zoltan     timer_mod(&s->vblank_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
257b7105d28SBALATON Zoltan               NANOSECONDS_PER_SECOND / 60);
258b7105d28SBALATON Zoltan     s->regs.gen_int_status |= CRTC_VBLANK_INT;
259b7105d28SBALATON Zoltan     ati_vga_update_irq(s);
260b7105d28SBALATON Zoltan }
261b7105d28SBALATON Zoltan 
262862b4a29SBALATON Zoltan static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
263862b4a29SBALATON Zoltan                                          unsigned int size)
264862b4a29SBALATON Zoltan {
265862b4a29SBALATON Zoltan     if (offs == 0 && size == 4) {
266862b4a29SBALATON Zoltan         return reg;
267862b4a29SBALATON Zoltan     } else {
268862b4a29SBALATON Zoltan         return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
269862b4a29SBALATON Zoltan     }
270862b4a29SBALATON Zoltan }
271862b4a29SBALATON Zoltan 
272862b4a29SBALATON Zoltan static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
273862b4a29SBALATON Zoltan {
274862b4a29SBALATON Zoltan     ATIVGAState *s = opaque;
275862b4a29SBALATON Zoltan     uint64_t val = 0;
276862b4a29SBALATON Zoltan 
277862b4a29SBALATON Zoltan     switch (addr) {
278862b4a29SBALATON Zoltan     case MM_INDEX:
279862b4a29SBALATON Zoltan         val = s->regs.mm_index;
280862b4a29SBALATON Zoltan         break;
281862b4a29SBALATON Zoltan     case MM_DATA ... MM_DATA + 3:
282862b4a29SBALATON Zoltan         /* indexed access to regs or memory */
283862b4a29SBALATON Zoltan         if (s->regs.mm_index & BIT(31)) {
284339534d4SBALATON Zoltan             uint32_t idx = s->regs.mm_index & ~BIT(31);
285339534d4SBALATON Zoltan             if (idx <= s->vga.vram_size - size) {
286339534d4SBALATON Zoltan                 val = ldn_le_p(s->vga.vram_ptr + idx, size);
287862b4a29SBALATON Zoltan             }
288a98610c4SPrasad J Pandit         } else if (s->regs.mm_index > MM_DATA + 3) {
289862b4a29SBALATON Zoltan             val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
290a98610c4SPrasad J Pandit         } else {
291a98610c4SPrasad J Pandit             qemu_log_mask(LOG_GUEST_ERROR,
292a98610c4SPrasad J Pandit                 "ati_mm_read: mm_index too small: %u\n", s->regs.mm_index);
293862b4a29SBALATON Zoltan         }
294862b4a29SBALATON Zoltan         break;
295862b4a29SBALATON Zoltan     case BIOS_0_SCRATCH ... BUS_CNTL - 1:
296862b4a29SBALATON Zoltan     {
297862b4a29SBALATON Zoltan         int i = (addr - BIOS_0_SCRATCH) / 4;
298862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
299862b4a29SBALATON Zoltan             break;
300862b4a29SBALATON Zoltan         }
301862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.bios_scratch[i],
302862b4a29SBALATON Zoltan                                 addr - (BIOS_0_SCRATCH + i * 4), size);
303862b4a29SBALATON Zoltan         break;
304862b4a29SBALATON Zoltan     }
305b7105d28SBALATON Zoltan     case GEN_INT_CNTL:
306b7105d28SBALATON Zoltan         val = s->regs.gen_int_cntl;
307b7105d28SBALATON Zoltan         break;
308b7105d28SBALATON Zoltan     case GEN_INT_STATUS:
309b7105d28SBALATON Zoltan         val = s->regs.gen_int_status;
310b7105d28SBALATON Zoltan         break;
311862b4a29SBALATON Zoltan     case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
312862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
313862b4a29SBALATON Zoltan                                 addr - CRTC_GEN_CNTL, size);
314862b4a29SBALATON Zoltan         break;
315862b4a29SBALATON Zoltan     case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
316862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
317862b4a29SBALATON Zoltan                                 addr - CRTC_EXT_CNTL, size);
318862b4a29SBALATON Zoltan         break;
319862b4a29SBALATON Zoltan     case DAC_CNTL:
320862b4a29SBALATON Zoltan         val = s->regs.dac_cntl;
321862b4a29SBALATON Zoltan         break;
322c82c7336SBALATON Zoltan     case GPIO_VGA_DDC:
323c82c7336SBALATON Zoltan         val = s->regs.gpio_vga_ddc;
324c82c7336SBALATON Zoltan         break;
325c82c7336SBALATON Zoltan     case GPIO_DVI_DDC:
326c82c7336SBALATON Zoltan         val = s->regs.gpio_dvi_ddc;
327c82c7336SBALATON Zoltan         break;
328c82c7336SBALATON Zoltan     case GPIO_MONID ... GPIO_MONID + 3:
329c82c7336SBALATON Zoltan         val = ati_reg_read_offs(s->regs.gpio_monid,
330c82c7336SBALATON Zoltan                                 addr - GPIO_MONID, size);
331c82c7336SBALATON Zoltan         break;
332862b4a29SBALATON Zoltan     case PALETTE_INDEX:
333862b4a29SBALATON Zoltan         /* FIXME unaligned access */
334862b4a29SBALATON Zoltan         val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
335862b4a29SBALATON Zoltan         val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
336862b4a29SBALATON Zoltan         break;
337862b4a29SBALATON Zoltan     case PALETTE_DATA:
338862b4a29SBALATON Zoltan         val = vga_ioport_read(&s->vga, VGA_PEL_D);
339862b4a29SBALATON Zoltan         break;
3408bb9a2b2SBALATON Zoltan     case CNFG_CNTL:
3418bb9a2b2SBALATON Zoltan         val = s->regs.config_cntl;
3428bb9a2b2SBALATON Zoltan         break;
343862b4a29SBALATON Zoltan     case CNFG_MEMSIZE:
344862b4a29SBALATON Zoltan         val = s->vga.vram_size;
345862b4a29SBALATON Zoltan         break;
3461d8d4d86SBALATON Zoltan     case CONFIG_APER_0_BASE:
3471d8d4d86SBALATON Zoltan     case CONFIG_APER_1_BASE:
3481d8d4d86SBALATON Zoltan         val = pci_default_read_config(&s->dev,
3491d8d4d86SBALATON Zoltan                                       PCI_BASE_ADDRESS_0, size) & 0xfffffff0;
3501d8d4d86SBALATON Zoltan         break;
3511d8d4d86SBALATON Zoltan     case CONFIG_APER_SIZE:
3521d8d4d86SBALATON Zoltan         val = s->vga.vram_size;
3531d8d4d86SBALATON Zoltan         break;
3541d8d4d86SBALATON Zoltan     case CONFIG_REG_1_BASE:
3551d8d4d86SBALATON Zoltan         val = pci_default_read_config(&s->dev,
3561d8d4d86SBALATON Zoltan                                       PCI_BASE_ADDRESS_2, size) & 0xfffffff0;
3571d8d4d86SBALATON Zoltan         break;
3581d8d4d86SBALATON Zoltan     case CONFIG_REG_APER_SIZE:
3591d8d4d86SBALATON Zoltan         val = memory_region_size(&s->mm);
3601d8d4d86SBALATON Zoltan         break;
361862b4a29SBALATON Zoltan     case MC_STATUS:
362862b4a29SBALATON Zoltan         val = 5;
363862b4a29SBALATON Zoltan         break;
364862b4a29SBALATON Zoltan     case RBBM_STATUS:
365862b4a29SBALATON Zoltan     case GUI_STAT:
366862b4a29SBALATON Zoltan         val = 64; /* free CMDFIFO entries */
367862b4a29SBALATON Zoltan         break;
368862b4a29SBALATON Zoltan     case CRTC_H_TOTAL_DISP:
369862b4a29SBALATON Zoltan         val = s->regs.crtc_h_total_disp;
370862b4a29SBALATON Zoltan         break;
371862b4a29SBALATON Zoltan     case CRTC_H_SYNC_STRT_WID:
372862b4a29SBALATON Zoltan         val = s->regs.crtc_h_sync_strt_wid;
373862b4a29SBALATON Zoltan         break;
374862b4a29SBALATON Zoltan     case CRTC_V_TOTAL_DISP:
375862b4a29SBALATON Zoltan         val = s->regs.crtc_v_total_disp;
376862b4a29SBALATON Zoltan         break;
377862b4a29SBALATON Zoltan     case CRTC_V_SYNC_STRT_WID:
378862b4a29SBALATON Zoltan         val = s->regs.crtc_v_sync_strt_wid;
379862b4a29SBALATON Zoltan         break;
380862b4a29SBALATON Zoltan     case CRTC_OFFSET:
381862b4a29SBALATON Zoltan         val = s->regs.crtc_offset;
382862b4a29SBALATON Zoltan         break;
383862b4a29SBALATON Zoltan     case CRTC_OFFSET_CNTL:
384862b4a29SBALATON Zoltan         val = s->regs.crtc_offset_cntl;
385862b4a29SBALATON Zoltan         break;
386862b4a29SBALATON Zoltan     case CRTC_PITCH:
387862b4a29SBALATON Zoltan         val = s->regs.crtc_pitch;
388862b4a29SBALATON Zoltan         break;
389862b4a29SBALATON Zoltan     case 0xf00 ... 0xfff:
390862b4a29SBALATON Zoltan         val = pci_default_read_config(&s->dev, addr - 0xf00, size);
391862b4a29SBALATON Zoltan         break;
392*d634c883SBALATON Zoltan     case CUR_OFFSET ... CUR_OFFSET + 3:
393*d634c883SBALATON Zoltan         val = ati_reg_read_offs(s->regs.cur_offset, addr - CUR_OFFSET, size);
394862b4a29SBALATON Zoltan         break;
395*d634c883SBALATON Zoltan     case CUR_HORZ_VERT_POSN ... CUR_HORZ_VERT_POSN + 3:
396*d634c883SBALATON Zoltan         val = ati_reg_read_offs(s->regs.cur_hv_pos,
397*d634c883SBALATON Zoltan                                 addr - CUR_HORZ_VERT_POSN, size);
398*d634c883SBALATON Zoltan         if (addr + size > CUR_HORZ_VERT_POSN + 3) {
399*d634c883SBALATON Zoltan             val |= (s->regs.cur_offset & BIT(31)) >> (4 - size);
400*d634c883SBALATON Zoltan         }
401862b4a29SBALATON Zoltan         break;
402*d634c883SBALATON Zoltan     case CUR_HORZ_VERT_OFF ... CUR_HORZ_VERT_OFF + 3:
403*d634c883SBALATON Zoltan         val = ati_reg_read_offs(s->regs.cur_hv_offs,
404*d634c883SBALATON Zoltan                                 addr - CUR_HORZ_VERT_OFF, size);
405*d634c883SBALATON Zoltan         if (addr + size > CUR_HORZ_VERT_OFF + 3) {
406*d634c883SBALATON Zoltan             val |= (s->regs.cur_offset & BIT(31)) >> (4 - size);
407*d634c883SBALATON Zoltan         }
408862b4a29SBALATON Zoltan         break;
409*d634c883SBALATON Zoltan     case CUR_CLR0 ... CUR_CLR0 + 3:
410*d634c883SBALATON Zoltan         val = ati_reg_read_offs(s->regs.cur_color0, addr - CUR_CLR0, size);
411862b4a29SBALATON Zoltan         break;
412*d634c883SBALATON Zoltan     case CUR_CLR1 ... CUR_CLR1 + 3:
413*d634c883SBALATON Zoltan         val = ati_reg_read_offs(s->regs.cur_color1, addr - CUR_CLR1, size);
414862b4a29SBALATON Zoltan         break;
415862b4a29SBALATON Zoltan     case DST_OFFSET:
416862b4a29SBALATON Zoltan         val = s->regs.dst_offset;
417862b4a29SBALATON Zoltan         break;
418862b4a29SBALATON Zoltan     case DST_PITCH:
419862b4a29SBALATON Zoltan         val = s->regs.dst_pitch;
420862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
421862b4a29SBALATON Zoltan             val &= s->regs.dst_tile << 16;
422862b4a29SBALATON Zoltan         }
423862b4a29SBALATON Zoltan         break;
424862b4a29SBALATON Zoltan     case DST_WIDTH:
425862b4a29SBALATON Zoltan         val = s->regs.dst_width;
426862b4a29SBALATON Zoltan         break;
427862b4a29SBALATON Zoltan     case DST_HEIGHT:
428862b4a29SBALATON Zoltan         val = s->regs.dst_height;
429862b4a29SBALATON Zoltan         break;
430862b4a29SBALATON Zoltan     case SRC_X:
431862b4a29SBALATON Zoltan         val = s->regs.src_x;
432862b4a29SBALATON Zoltan         break;
433862b4a29SBALATON Zoltan     case SRC_Y:
434862b4a29SBALATON Zoltan         val = s->regs.src_y;
435862b4a29SBALATON Zoltan         break;
436862b4a29SBALATON Zoltan     case DST_X:
437862b4a29SBALATON Zoltan         val = s->regs.dst_x;
438862b4a29SBALATON Zoltan         break;
439862b4a29SBALATON Zoltan     case DST_Y:
440862b4a29SBALATON Zoltan         val = s->regs.dst_y;
441862b4a29SBALATON Zoltan         break;
442862b4a29SBALATON Zoltan     case DP_GUI_MASTER_CNTL:
443862b4a29SBALATON Zoltan         val = s->regs.dp_gui_master_cntl;
444862b4a29SBALATON Zoltan         break;
445862b4a29SBALATON Zoltan     case SRC_OFFSET:
446862b4a29SBALATON Zoltan         val = s->regs.src_offset;
447862b4a29SBALATON Zoltan         break;
448862b4a29SBALATON Zoltan     case SRC_PITCH:
449862b4a29SBALATON Zoltan         val = s->regs.src_pitch;
450862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
451862b4a29SBALATON Zoltan             val &= s->regs.src_tile << 16;
452862b4a29SBALATON Zoltan         }
453862b4a29SBALATON Zoltan         break;
454862b4a29SBALATON Zoltan     case DP_BRUSH_BKGD_CLR:
455862b4a29SBALATON Zoltan         val = s->regs.dp_brush_bkgd_clr;
456862b4a29SBALATON Zoltan         break;
457862b4a29SBALATON Zoltan     case DP_BRUSH_FRGD_CLR:
458862b4a29SBALATON Zoltan         val = s->regs.dp_brush_frgd_clr;
459862b4a29SBALATON Zoltan         break;
460862b4a29SBALATON Zoltan     case DP_SRC_FRGD_CLR:
461862b4a29SBALATON Zoltan         val = s->regs.dp_src_frgd_clr;
462862b4a29SBALATON Zoltan         break;
463862b4a29SBALATON Zoltan     case DP_SRC_BKGD_CLR:
464862b4a29SBALATON Zoltan         val = s->regs.dp_src_bkgd_clr;
465862b4a29SBALATON Zoltan         break;
466862b4a29SBALATON Zoltan     case DP_CNTL:
467862b4a29SBALATON Zoltan         val = s->regs.dp_cntl;
468862b4a29SBALATON Zoltan         break;
469862b4a29SBALATON Zoltan     case DP_DATATYPE:
470862b4a29SBALATON Zoltan         val = s->regs.dp_datatype;
471862b4a29SBALATON Zoltan         break;
472862b4a29SBALATON Zoltan     case DP_MIX:
473862b4a29SBALATON Zoltan         val = s->regs.dp_mix;
474862b4a29SBALATON Zoltan         break;
475862b4a29SBALATON Zoltan     case DP_WRITE_MASK:
476862b4a29SBALATON Zoltan         val = s->regs.dp_write_mask;
477862b4a29SBALATON Zoltan         break;
478862b4a29SBALATON Zoltan     case DEFAULT_OFFSET:
479862b4a29SBALATON Zoltan         val = s->regs.default_offset;
480866ad5f5SBALATON Zoltan         if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
481866ad5f5SBALATON Zoltan             val >>= 10;
482866ad5f5SBALATON Zoltan             val |= s->regs.default_pitch << 16;
483866ad5f5SBALATON Zoltan             val |= s->regs.default_tile << 30;
484866ad5f5SBALATON Zoltan         }
485862b4a29SBALATON Zoltan         break;
486862b4a29SBALATON Zoltan     case DEFAULT_PITCH:
487862b4a29SBALATON Zoltan         val = s->regs.default_pitch;
488866ad5f5SBALATON Zoltan         val |= s->regs.default_tile << 16;
489862b4a29SBALATON Zoltan         break;
490862b4a29SBALATON Zoltan     case DEFAULT_SC_BOTTOM_RIGHT:
491862b4a29SBALATON Zoltan         val = s->regs.default_sc_bottom_right;
492862b4a29SBALATON Zoltan         break;
493862b4a29SBALATON Zoltan     default:
494862b4a29SBALATON Zoltan         break;
495862b4a29SBALATON Zoltan     }
496862b4a29SBALATON Zoltan     if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
497862b4a29SBALATON Zoltan         trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
498862b4a29SBALATON Zoltan     }
499862b4a29SBALATON Zoltan     return val;
500862b4a29SBALATON Zoltan }
501862b4a29SBALATON Zoltan 
502862b4a29SBALATON Zoltan static inline void ati_reg_write_offs(uint32_t *reg, int offs,
503862b4a29SBALATON Zoltan                                       uint64_t data, unsigned int size)
504862b4a29SBALATON Zoltan {
505862b4a29SBALATON Zoltan     if (offs == 0 && size == 4) {
506862b4a29SBALATON Zoltan         *reg = data;
507862b4a29SBALATON Zoltan     } else {
508862b4a29SBALATON Zoltan         *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
509862b4a29SBALATON Zoltan                          data);
510862b4a29SBALATON Zoltan     }
511862b4a29SBALATON Zoltan }
512862b4a29SBALATON Zoltan 
513862b4a29SBALATON Zoltan static void ati_mm_write(void *opaque, hwaddr addr,
514862b4a29SBALATON Zoltan                            uint64_t data, unsigned int size)
515862b4a29SBALATON Zoltan {
516862b4a29SBALATON Zoltan     ATIVGAState *s = opaque;
517862b4a29SBALATON Zoltan 
518862b4a29SBALATON Zoltan     if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
519862b4a29SBALATON Zoltan         trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
520862b4a29SBALATON Zoltan     }
521862b4a29SBALATON Zoltan     switch (addr) {
522862b4a29SBALATON Zoltan     case MM_INDEX:
523b0588cb5SBALATON Zoltan         s->regs.mm_index = data & ~3;
524862b4a29SBALATON Zoltan         break;
525862b4a29SBALATON Zoltan     case MM_DATA ... MM_DATA + 3:
526862b4a29SBALATON Zoltan         /* indexed access to regs or memory */
527862b4a29SBALATON Zoltan         if (s->regs.mm_index & BIT(31)) {
528339534d4SBALATON Zoltan             uint32_t idx = s->regs.mm_index & ~BIT(31);
529339534d4SBALATON Zoltan             if (idx <= s->vga.vram_size - size) {
530339534d4SBALATON Zoltan                 stn_le_p(s->vga.vram_ptr + idx, size, data);
531862b4a29SBALATON Zoltan             }
532a98610c4SPrasad J Pandit         } else if (s->regs.mm_index > MM_DATA + 3) {
533862b4a29SBALATON Zoltan             ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
534a98610c4SPrasad J Pandit         } else {
535a98610c4SPrasad J Pandit             qemu_log_mask(LOG_GUEST_ERROR,
536a98610c4SPrasad J Pandit                 "ati_mm_write: mm_index too small: %u\n", s->regs.mm_index);
537862b4a29SBALATON Zoltan         }
538862b4a29SBALATON Zoltan         break;
539862b4a29SBALATON Zoltan     case BIOS_0_SCRATCH ... BUS_CNTL - 1:
540862b4a29SBALATON Zoltan     {
541862b4a29SBALATON Zoltan         int i = (addr - BIOS_0_SCRATCH) / 4;
542862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
543862b4a29SBALATON Zoltan             break;
544862b4a29SBALATON Zoltan         }
545862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.bios_scratch[i],
546862b4a29SBALATON Zoltan                            addr - (BIOS_0_SCRATCH + i * 4), data, size);
547862b4a29SBALATON Zoltan         break;
548862b4a29SBALATON Zoltan     }
549b7105d28SBALATON Zoltan     case GEN_INT_CNTL:
550b7105d28SBALATON Zoltan         s->regs.gen_int_cntl = data;
551b7105d28SBALATON Zoltan         if (data & CRTC_VBLANK_INT) {
552b7105d28SBALATON Zoltan             ati_vga_vblank_irq(s);
553b7105d28SBALATON Zoltan         } else {
554b7105d28SBALATON Zoltan             timer_del(&s->vblank_timer);
555b7105d28SBALATON Zoltan             ati_vga_update_irq(s);
556b7105d28SBALATON Zoltan         }
557b7105d28SBALATON Zoltan         break;
558b7105d28SBALATON Zoltan     case GEN_INT_STATUS:
559b7105d28SBALATON Zoltan         data &= (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF ?
560b7105d28SBALATON Zoltan                  0x000f040fUL : 0xfc080effUL);
561b7105d28SBALATON Zoltan         s->regs.gen_int_status &= ~data;
562b7105d28SBALATON Zoltan         ati_vga_update_irq(s);
563b7105d28SBALATON Zoltan         break;
564862b4a29SBALATON Zoltan     case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
565862b4a29SBALATON Zoltan     {
566862b4a29SBALATON Zoltan         uint32_t val = s->regs.crtc_gen_cntl;
567862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.crtc_gen_cntl,
568862b4a29SBALATON Zoltan                            addr - CRTC_GEN_CNTL, data, size);
569862b4a29SBALATON Zoltan         if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
570862b4a29SBALATON Zoltan             if (s->cursor_guest_mode) {
571862b4a29SBALATON Zoltan                 s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
572862b4a29SBALATON Zoltan             } else {
573862b4a29SBALATON Zoltan                 if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
574862b4a29SBALATON Zoltan                     ati_cursor_define(s);
575862b4a29SBALATON Zoltan                 }
576862b4a29SBALATON Zoltan                 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
577862b4a29SBALATON Zoltan                               s->regs.cur_hv_pos & 0xffff,
578862b4a29SBALATON Zoltan                               (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
579862b4a29SBALATON Zoltan             }
580862b4a29SBALATON Zoltan         }
581862b4a29SBALATON Zoltan         if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
582862b4a29SBALATON Zoltan             (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
583862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
584862b4a29SBALATON Zoltan         }
585862b4a29SBALATON Zoltan         break;
586862b4a29SBALATON Zoltan     }
587862b4a29SBALATON Zoltan     case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
588862b4a29SBALATON Zoltan     {
589862b4a29SBALATON Zoltan         uint32_t val = s->regs.crtc_ext_cntl;
590862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.crtc_ext_cntl,
591862b4a29SBALATON Zoltan                            addr - CRTC_EXT_CNTL, data, size);
592862b4a29SBALATON Zoltan         if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
593862b4a29SBALATON Zoltan             DPRINTF("Display disabled\n");
594862b4a29SBALATON Zoltan             s->vga.ar_index &= ~BIT(5);
595862b4a29SBALATON Zoltan         } else {
596862b4a29SBALATON Zoltan             DPRINTF("Display enabled\n");
597862b4a29SBALATON Zoltan             s->vga.ar_index |= BIT(5);
598862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
599862b4a29SBALATON Zoltan         }
600862b4a29SBALATON Zoltan         if ((val & CRT_CRTC_DISPLAY_DIS) !=
601862b4a29SBALATON Zoltan             (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
602862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
603862b4a29SBALATON Zoltan         }
604862b4a29SBALATON Zoltan         break;
605862b4a29SBALATON Zoltan     }
606862b4a29SBALATON Zoltan     case DAC_CNTL:
607862b4a29SBALATON Zoltan         s->regs.dac_cntl = data & 0xffffe3ff;
608862b4a29SBALATON Zoltan         s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
609862b4a29SBALATON Zoltan         break;
610c82c7336SBALATON Zoltan     case GPIO_VGA_DDC:
611c82c7336SBALATON Zoltan         if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
612c82c7336SBALATON Zoltan             /* FIXME: Maybe add a property to select VGA or DVI port? */
613c82c7336SBALATON Zoltan         }
614c82c7336SBALATON Zoltan         break;
615c82c7336SBALATON Zoltan     case GPIO_DVI_DDC:
616c82c7336SBALATON Zoltan         if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
61741742927SPeter Maydell             s->regs.gpio_dvi_ddc = ati_i2c(&s->bbi2c, data, 0);
618c82c7336SBALATON Zoltan         }
619c82c7336SBALATON Zoltan         break;
620c82c7336SBALATON Zoltan     case GPIO_MONID ... GPIO_MONID + 3:
621c82c7336SBALATON Zoltan         /* FIXME What does Radeon have here? */
622c82c7336SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
623c82c7336SBALATON Zoltan             ati_reg_write_offs(&s->regs.gpio_monid,
624c82c7336SBALATON Zoltan                                addr - GPIO_MONID, data, size);
625c82c7336SBALATON Zoltan             /*
626c82c7336SBALATON Zoltan              * Rage128p accesses DDC used to get EDID via these bits.
627006388a8SBALATON Zoltan              * Because some drivers access this via multiple byte writes
628006388a8SBALATON Zoltan              * we have to be careful when we send bits to avoid spurious
629006388a8SBALATON Zoltan              * changes in bitbang_i2c state. So only do it when mask is set
630006388a8SBALATON Zoltan              * and either the enable bits are changed or output bits changed
631006388a8SBALATON Zoltan              * while enabled.
632c82c7336SBALATON Zoltan              */
633c82c7336SBALATON Zoltan             if ((s->regs.gpio_monid & BIT(25)) &&
634006388a8SBALATON Zoltan                 ((addr <= GPIO_MONID + 2 && addr + size > GPIO_MONID + 2) ||
635006388a8SBALATON Zoltan                  (addr == GPIO_MONID && (s->regs.gpio_monid & 0x60000)))) {
63641742927SPeter Maydell                 s->regs.gpio_monid = ati_i2c(&s->bbi2c, s->regs.gpio_monid, 1);
637c82c7336SBALATON Zoltan             }
638c82c7336SBALATON Zoltan         }
639c82c7336SBALATON Zoltan         break;
640862b4a29SBALATON Zoltan     case PALETTE_INDEX ... PALETTE_INDEX + 3:
641862b4a29SBALATON Zoltan         if (size == 4) {
642862b4a29SBALATON Zoltan             vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
643862b4a29SBALATON Zoltan             vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
644862b4a29SBALATON Zoltan         } else {
645862b4a29SBALATON Zoltan             if (addr == PALETTE_INDEX) {
646862b4a29SBALATON Zoltan                 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
647862b4a29SBALATON Zoltan             } else {
648862b4a29SBALATON Zoltan                 vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
649862b4a29SBALATON Zoltan             }
650862b4a29SBALATON Zoltan         }
651862b4a29SBALATON Zoltan         break;
652862b4a29SBALATON Zoltan     case PALETTE_DATA ... PALETTE_DATA + 3:
653862b4a29SBALATON Zoltan         data <<= addr - PALETTE_DATA;
654862b4a29SBALATON Zoltan         data = bswap32(data) >> 8;
655862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
656862b4a29SBALATON Zoltan         data >>= 8;
657862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
658862b4a29SBALATON Zoltan         data >>= 8;
659862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
660862b4a29SBALATON Zoltan         break;
6618bb9a2b2SBALATON Zoltan     case CNFG_CNTL:
6628bb9a2b2SBALATON Zoltan         s->regs.config_cntl = data;
6638bb9a2b2SBALATON Zoltan         break;
664862b4a29SBALATON Zoltan     case CRTC_H_TOTAL_DISP:
665862b4a29SBALATON Zoltan         s->regs.crtc_h_total_disp = data & 0x07ff07ff;
666862b4a29SBALATON Zoltan         break;
667862b4a29SBALATON Zoltan     case CRTC_H_SYNC_STRT_WID:
668862b4a29SBALATON Zoltan         s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
669862b4a29SBALATON Zoltan         break;
670862b4a29SBALATON Zoltan     case CRTC_V_TOTAL_DISP:
671862b4a29SBALATON Zoltan         s->regs.crtc_v_total_disp = data & 0x0fff0fff;
672862b4a29SBALATON Zoltan         break;
673862b4a29SBALATON Zoltan     case CRTC_V_SYNC_STRT_WID:
674862b4a29SBALATON Zoltan         s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
675862b4a29SBALATON Zoltan         break;
676862b4a29SBALATON Zoltan     case CRTC_OFFSET:
677862b4a29SBALATON Zoltan         s->regs.crtc_offset = data & 0xc7ffffff;
678862b4a29SBALATON Zoltan         break;
679862b4a29SBALATON Zoltan     case CRTC_OFFSET_CNTL:
680862b4a29SBALATON Zoltan         s->regs.crtc_offset_cntl = data; /* FIXME */
681862b4a29SBALATON Zoltan         break;
682862b4a29SBALATON Zoltan     case CRTC_PITCH:
683862b4a29SBALATON Zoltan         s->regs.crtc_pitch = data & 0x07ff07ff;
684862b4a29SBALATON Zoltan         break;
685862b4a29SBALATON Zoltan     case 0xf00 ... 0xfff:
686862b4a29SBALATON Zoltan         /* read-only copy of PCI config space so ignore writes */
687862b4a29SBALATON Zoltan         break;
688*d634c883SBALATON Zoltan     case CUR_OFFSET ... CUR_OFFSET + 3:
689*d634c883SBALATON Zoltan     {
690*d634c883SBALATON Zoltan         uint32_t t = s->regs.cur_offset;
691*d634c883SBALATON Zoltan 
692*d634c883SBALATON Zoltan         ati_reg_write_offs(&t, addr - CUR_OFFSET, data, size);
693*d634c883SBALATON Zoltan         t &= 0x87fffff0;
694*d634c883SBALATON Zoltan         if (s->regs.cur_offset != t) {
695*d634c883SBALATON Zoltan             s->regs.cur_offset = t;
696862b4a29SBALATON Zoltan             ati_cursor_define(s);
697862b4a29SBALATON Zoltan         }
698862b4a29SBALATON Zoltan         break;
699*d634c883SBALATON Zoltan     }
700*d634c883SBALATON Zoltan     case CUR_HORZ_VERT_POSN ... CUR_HORZ_VERT_POSN + 3:
701*d634c883SBALATON Zoltan     {
702*d634c883SBALATON Zoltan         uint32_t t = s->regs.cur_hv_pos | (s->regs.cur_offset & BIT(31));
703*d634c883SBALATON Zoltan 
704*d634c883SBALATON Zoltan         ati_reg_write_offs(&t, addr - CUR_HORZ_VERT_POSN, data, size);
705*d634c883SBALATON Zoltan         s->regs.cur_hv_pos = t & 0x3fff0fff;
706*d634c883SBALATON Zoltan         if (t & BIT(31)) {
707*d634c883SBALATON Zoltan             s->regs.cur_offset |= t & BIT(31);
708862b4a29SBALATON Zoltan         } else if (s->regs.cur_offset & BIT(31)) {
709862b4a29SBALATON Zoltan             s->regs.cur_offset &= ~BIT(31);
710862b4a29SBALATON Zoltan             ati_cursor_define(s);
711862b4a29SBALATON Zoltan         }
712862b4a29SBALATON Zoltan         if (!s->cursor_guest_mode &&
713*d634c883SBALATON Zoltan             (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(t & BIT(31))) {
714862b4a29SBALATON Zoltan             dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
715862b4a29SBALATON Zoltan                           s->regs.cur_hv_pos & 0xffff, 1);
716862b4a29SBALATON Zoltan         }
717862b4a29SBALATON Zoltan         break;
718*d634c883SBALATON Zoltan     }
719862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_OFF:
720*d634c883SBALATON Zoltan     {
721*d634c883SBALATON Zoltan         uint32_t t = s->regs.cur_hv_offs | (s->regs.cur_offset & BIT(31));
722*d634c883SBALATON Zoltan 
723*d634c883SBALATON Zoltan         ati_reg_write_offs(&t, addr - CUR_HORZ_VERT_OFF, data, size);
724*d634c883SBALATON Zoltan         s->regs.cur_hv_offs = t & 0x3f003f;
725*d634c883SBALATON Zoltan         if (t & BIT(31)) {
726*d634c883SBALATON Zoltan             s->regs.cur_offset |= t & BIT(31);
727862b4a29SBALATON Zoltan         } else if (s->regs.cur_offset & BIT(31)) {
728862b4a29SBALATON Zoltan             s->regs.cur_offset &= ~BIT(31);
729862b4a29SBALATON Zoltan             ati_cursor_define(s);
730862b4a29SBALATON Zoltan         }
731862b4a29SBALATON Zoltan         break;
732*d634c883SBALATON Zoltan     }
733*d634c883SBALATON Zoltan     case CUR_CLR0 ... CUR_CLR0 + 3:
734*d634c883SBALATON Zoltan     {
735*d634c883SBALATON Zoltan         uint32_t t = s->regs.cur_color0;
736*d634c883SBALATON Zoltan 
737*d634c883SBALATON Zoltan         ati_reg_write_offs(&t, addr - CUR_CLR0, data, size);
738*d634c883SBALATON Zoltan         t &= 0xffffff;
739*d634c883SBALATON Zoltan         if (s->regs.cur_color0 != t) {
740*d634c883SBALATON Zoltan             s->regs.cur_color0 = t;
741862b4a29SBALATON Zoltan             ati_cursor_define(s);
742862b4a29SBALATON Zoltan         }
743862b4a29SBALATON Zoltan         break;
744*d634c883SBALATON Zoltan     }
745*d634c883SBALATON Zoltan     case CUR_CLR1 ... CUR_CLR1 + 3:
746862b4a29SBALATON Zoltan         /*
747862b4a29SBALATON Zoltan          * Update cursor unconditionally here because some clients set up
748862b4a29SBALATON Zoltan          * other registers before actually writing cursor data to memory at
749862b4a29SBALATON Zoltan          * offset so we would miss cursor change unless always updating here
750862b4a29SBALATON Zoltan          */
751*d634c883SBALATON Zoltan         ati_reg_write_offs(&s->regs.cur_color1, addr - CUR_CLR1, data, size);
752*d634c883SBALATON Zoltan         s->regs.cur_color1 &= 0xffffff;
753862b4a29SBALATON Zoltan         ati_cursor_define(s);
754862b4a29SBALATON Zoltan         break;
755862b4a29SBALATON Zoltan     case DST_OFFSET:
756862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
757862b4a29SBALATON Zoltan             s->regs.dst_offset = data & 0xfffffff0;
758862b4a29SBALATON Zoltan         } else {
759862b4a29SBALATON Zoltan             s->regs.dst_offset = data & 0xfffffc00;
760862b4a29SBALATON Zoltan         }
761862b4a29SBALATON Zoltan         break;
762862b4a29SBALATON Zoltan     case DST_PITCH:
763862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
764862b4a29SBALATON Zoltan             s->regs.dst_pitch = data & 0x3fff;
765862b4a29SBALATON Zoltan             s->regs.dst_tile = (data >> 16) & 1;
766862b4a29SBALATON Zoltan         } else {
767862b4a29SBALATON Zoltan             s->regs.dst_pitch = data & 0x3ff0;
768862b4a29SBALATON Zoltan         }
769862b4a29SBALATON Zoltan         break;
770862b4a29SBALATON Zoltan     case DST_TILE:
771862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
772862b4a29SBALATON Zoltan             s->regs.dst_tile = data & 3;
773862b4a29SBALATON Zoltan         }
774862b4a29SBALATON Zoltan         break;
775862b4a29SBALATON Zoltan     case DST_WIDTH:
776862b4a29SBALATON Zoltan         s->regs.dst_width = data & 0x3fff;
777862b4a29SBALATON Zoltan         ati_2d_blt(s);
778862b4a29SBALATON Zoltan         break;
779862b4a29SBALATON Zoltan     case DST_HEIGHT:
780862b4a29SBALATON Zoltan         s->regs.dst_height = data & 0x3fff;
781862b4a29SBALATON Zoltan         break;
782862b4a29SBALATON Zoltan     case SRC_X:
783862b4a29SBALATON Zoltan         s->regs.src_x = data & 0x3fff;
784862b4a29SBALATON Zoltan         break;
785862b4a29SBALATON Zoltan     case SRC_Y:
786862b4a29SBALATON Zoltan         s->regs.src_y = data & 0x3fff;
787862b4a29SBALATON Zoltan         break;
788862b4a29SBALATON Zoltan     case DST_X:
789862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
790862b4a29SBALATON Zoltan         break;
791862b4a29SBALATON Zoltan     case DST_Y:
792862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
793862b4a29SBALATON Zoltan         break;
794862b4a29SBALATON Zoltan     case SRC_PITCH_OFFSET:
795862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
796146dd326SBALATON Zoltan             s->regs.src_offset = (data & 0x1fffff) << 5;
797866ad5f5SBALATON Zoltan             s->regs.src_pitch = (data & 0x7fe00000) >> 21;
798862b4a29SBALATON Zoltan             s->regs.src_tile = data >> 31;
799862b4a29SBALATON Zoltan         } else {
800866ad5f5SBALATON Zoltan             s->regs.src_offset = (data & 0x3fffff) << 10;
801862b4a29SBALATON Zoltan             s->regs.src_pitch = (data & 0x3fc00000) >> 16;
802862b4a29SBALATON Zoltan             s->regs.src_tile = (data >> 30) & 1;
803862b4a29SBALATON Zoltan         }
804862b4a29SBALATON Zoltan         break;
805862b4a29SBALATON Zoltan     case DST_PITCH_OFFSET:
806862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
807146dd326SBALATON Zoltan             s->regs.dst_offset = (data & 0x1fffff) << 5;
808866ad5f5SBALATON Zoltan             s->regs.dst_pitch = (data & 0x7fe00000) >> 21;
809862b4a29SBALATON Zoltan             s->regs.dst_tile = data >> 31;
810862b4a29SBALATON Zoltan         } else {
811866ad5f5SBALATON Zoltan             s->regs.dst_offset = (data & 0x3fffff) << 10;
812862b4a29SBALATON Zoltan             s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
813862b4a29SBALATON Zoltan             s->regs.dst_tile = data >> 30;
814862b4a29SBALATON Zoltan         }
815862b4a29SBALATON Zoltan         break;
816862b4a29SBALATON Zoltan     case SRC_Y_X:
817862b4a29SBALATON Zoltan         s->regs.src_x = data & 0x3fff;
818862b4a29SBALATON Zoltan         s->regs.src_y = (data >> 16) & 0x3fff;
819862b4a29SBALATON Zoltan         break;
820862b4a29SBALATON Zoltan     case DST_Y_X:
821862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
822862b4a29SBALATON Zoltan         s->regs.dst_y = (data >> 16) & 0x3fff;
823862b4a29SBALATON Zoltan         break;
824862b4a29SBALATON Zoltan     case DST_HEIGHT_WIDTH:
825862b4a29SBALATON Zoltan         s->regs.dst_width = data & 0x3fff;
826862b4a29SBALATON Zoltan         s->regs.dst_height = (data >> 16) & 0x3fff;
827862b4a29SBALATON Zoltan         ati_2d_blt(s);
828862b4a29SBALATON Zoltan         break;
829862b4a29SBALATON Zoltan     case DP_GUI_MASTER_CNTL:
830862b4a29SBALATON Zoltan         s->regs.dp_gui_master_cntl = data & 0xf800000f;
831862b4a29SBALATON Zoltan         s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
832862b4a29SBALATON Zoltan                               (data & 0x4000) << 16;
833862b4a29SBALATON Zoltan         s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
834862b4a29SBALATON Zoltan         break;
835862b4a29SBALATON Zoltan     case DST_WIDTH_X:
836862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
837862b4a29SBALATON Zoltan         s->regs.dst_width = (data >> 16) & 0x3fff;
838862b4a29SBALATON Zoltan         ati_2d_blt(s);
839862b4a29SBALATON Zoltan         break;
840862b4a29SBALATON Zoltan     case SRC_X_Y:
841862b4a29SBALATON Zoltan         s->regs.src_y = data & 0x3fff;
842862b4a29SBALATON Zoltan         s->regs.src_x = (data >> 16) & 0x3fff;
843862b4a29SBALATON Zoltan         break;
844862b4a29SBALATON Zoltan     case DST_X_Y:
845862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
846862b4a29SBALATON Zoltan         s->regs.dst_x = (data >> 16) & 0x3fff;
847862b4a29SBALATON Zoltan         break;
848862b4a29SBALATON Zoltan     case DST_WIDTH_HEIGHT:
849862b4a29SBALATON Zoltan         s->regs.dst_height = data & 0x3fff;
850862b4a29SBALATON Zoltan         s->regs.dst_width = (data >> 16) & 0x3fff;
851862b4a29SBALATON Zoltan         ati_2d_blt(s);
852862b4a29SBALATON Zoltan         break;
853862b4a29SBALATON Zoltan     case DST_HEIGHT_Y:
854862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
855862b4a29SBALATON Zoltan         s->regs.dst_height = (data >> 16) & 0x3fff;
856862b4a29SBALATON Zoltan         break;
857862b4a29SBALATON Zoltan     case SRC_OFFSET:
858862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
859862b4a29SBALATON Zoltan             s->regs.src_offset = data & 0xfffffff0;
860862b4a29SBALATON Zoltan         } else {
861862b4a29SBALATON Zoltan             s->regs.src_offset = data & 0xfffffc00;
862862b4a29SBALATON Zoltan         }
863862b4a29SBALATON Zoltan         break;
864862b4a29SBALATON Zoltan     case SRC_PITCH:
865862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
866862b4a29SBALATON Zoltan             s->regs.src_pitch = data & 0x3fff;
867862b4a29SBALATON Zoltan             s->regs.src_tile = (data >> 16) & 1;
868862b4a29SBALATON Zoltan         } else {
869862b4a29SBALATON Zoltan             s->regs.src_pitch = data & 0x3ff0;
870862b4a29SBALATON Zoltan         }
871862b4a29SBALATON Zoltan         break;
872862b4a29SBALATON Zoltan     case DP_BRUSH_BKGD_CLR:
873862b4a29SBALATON Zoltan         s->regs.dp_brush_bkgd_clr = data;
874862b4a29SBALATON Zoltan         break;
875862b4a29SBALATON Zoltan     case DP_BRUSH_FRGD_CLR:
876862b4a29SBALATON Zoltan         s->regs.dp_brush_frgd_clr = data;
877862b4a29SBALATON Zoltan         break;
878862b4a29SBALATON Zoltan     case DP_CNTL:
879862b4a29SBALATON Zoltan         s->regs.dp_cntl = data;
880862b4a29SBALATON Zoltan         break;
881862b4a29SBALATON Zoltan     case DP_DATATYPE:
882862b4a29SBALATON Zoltan         s->regs.dp_datatype = data & 0xe0070f0f;
883862b4a29SBALATON Zoltan         break;
884862b4a29SBALATON Zoltan     case DP_MIX:
885862b4a29SBALATON Zoltan         s->regs.dp_mix = data & 0x00ff0700;
886862b4a29SBALATON Zoltan         break;
887862b4a29SBALATON Zoltan     case DP_WRITE_MASK:
888862b4a29SBALATON Zoltan         s->regs.dp_write_mask = data;
889862b4a29SBALATON Zoltan         break;
890862b4a29SBALATON Zoltan     case DEFAULT_OFFSET:
891866ad5f5SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
892866ad5f5SBALATON Zoltan             s->regs.default_offset = data & 0xfffffff0;
893866ad5f5SBALATON Zoltan         } else {
894866ad5f5SBALATON Zoltan             /* Radeon has DEFAULT_PITCH_OFFSET here like DST_PITCH_OFFSET */
895866ad5f5SBALATON Zoltan             s->regs.default_offset = (data & 0x3fffff) << 10;
896866ad5f5SBALATON Zoltan             s->regs.default_pitch = (data & 0x3fc00000) >> 16;
897866ad5f5SBALATON Zoltan             s->regs.default_tile = data >> 30;
898866ad5f5SBALATON Zoltan         }
899862b4a29SBALATON Zoltan         break;
900862b4a29SBALATON Zoltan     case DEFAULT_PITCH:
901862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
902866ad5f5SBALATON Zoltan             s->regs.default_pitch = data & 0x3fff;
903866ad5f5SBALATON Zoltan             s->regs.default_tile = (data >> 16) & 1;
904862b4a29SBALATON Zoltan         }
905862b4a29SBALATON Zoltan         break;
906862b4a29SBALATON Zoltan     case DEFAULT_SC_BOTTOM_RIGHT:
907862b4a29SBALATON Zoltan         s->regs.default_sc_bottom_right = data & 0x3fff3fff;
908862b4a29SBALATON Zoltan         break;
909862b4a29SBALATON Zoltan     default:
910862b4a29SBALATON Zoltan         break;
911862b4a29SBALATON Zoltan     }
912862b4a29SBALATON Zoltan }
913862b4a29SBALATON Zoltan 
914862b4a29SBALATON Zoltan static const MemoryRegionOps ati_mm_ops = {
915862b4a29SBALATON Zoltan     .read = ati_mm_read,
916862b4a29SBALATON Zoltan     .write = ati_mm_write,
917862b4a29SBALATON Zoltan     .endianness = DEVICE_LITTLE_ENDIAN,
918862b4a29SBALATON Zoltan };
919862b4a29SBALATON Zoltan 
920862b4a29SBALATON Zoltan static void ati_vga_realize(PCIDevice *dev, Error **errp)
921862b4a29SBALATON Zoltan {
922862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
923862b4a29SBALATON Zoltan     VGACommonState *vga = &s->vga;
924862b4a29SBALATON Zoltan 
925862b4a29SBALATON Zoltan     if (s->model) {
926862b4a29SBALATON Zoltan         int i;
927862b4a29SBALATON Zoltan         for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
928862b4a29SBALATON Zoltan             if (!strcmp(s->model, ati_model_aliases[i].name)) {
929862b4a29SBALATON Zoltan                 s->dev_id = ati_model_aliases[i].dev_id;
930862b4a29SBALATON Zoltan                 break;
931862b4a29SBALATON Zoltan             }
932862b4a29SBALATON Zoltan         }
933862b4a29SBALATON Zoltan         if (i >= ARRAY_SIZE(ati_model_aliases)) {
934862b4a29SBALATON Zoltan             warn_report("Unknown ATI VGA model name, "
935862b4a29SBALATON Zoltan                         "using default rage128p");
936862b4a29SBALATON Zoltan         }
937862b4a29SBALATON Zoltan     }
938862b4a29SBALATON Zoltan     if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
939862b4a29SBALATON Zoltan         s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
940862b4a29SBALATON Zoltan         error_setg(errp, "Unknown ATI VGA device id, "
941862b4a29SBALATON Zoltan                    "only 0x5046 and 0x5159 are supported");
942862b4a29SBALATON Zoltan         return;
943862b4a29SBALATON Zoltan     }
944862b4a29SBALATON Zoltan     pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
945862b4a29SBALATON Zoltan 
946862b4a29SBALATON Zoltan     if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
947862b4a29SBALATON Zoltan         s->vga.vram_size_mb < 16) {
948862b4a29SBALATON Zoltan         warn_report("Too small video memory for device id");
949862b4a29SBALATON Zoltan         s->vga.vram_size_mb = 16;
950862b4a29SBALATON Zoltan     }
951862b4a29SBALATON Zoltan 
952862b4a29SBALATON Zoltan     /* init vga bits */
953862b4a29SBALATON Zoltan     vga_common_init(vga, OBJECT(s));
954862b4a29SBALATON Zoltan     vga_init(vga, OBJECT(s), pci_address_space(dev),
955862b4a29SBALATON Zoltan              pci_address_space_io(dev), true);
956862b4a29SBALATON Zoltan     vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga);
957862b4a29SBALATON Zoltan     if (s->cursor_guest_mode) {
958862b4a29SBALATON Zoltan         vga->cursor_invalidate = ati_cursor_invalidate;
959862b4a29SBALATON Zoltan         vga->cursor_draw_line = ati_cursor_draw_line;
960862b4a29SBALATON Zoltan     }
961862b4a29SBALATON Zoltan 
962c82c7336SBALATON Zoltan     /* ddc, edid */
963c82c7336SBALATON Zoltan     I2CBus *i2cbus = i2c_init_bus(DEVICE(s), "ati-vga.ddc");
96441742927SPeter Maydell     bitbang_i2c_init(&s->bbi2c, i2cbus);
965df707969SMarkus Armbruster     I2CSlave *i2cddc = I2C_SLAVE(qdev_new(TYPE_I2CDDC));
966c82c7336SBALATON Zoltan     i2c_set_slave_address(i2cddc, 0x50);
967df707969SMarkus Armbruster     qdev_realize_and_unref(DEVICE(i2cddc), BUS(i2cbus), &error_abort);
968c82c7336SBALATON Zoltan 
969862b4a29SBALATON Zoltan     /* mmio register space */
970862b4a29SBALATON Zoltan     memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
971862b4a29SBALATON Zoltan                           "ati.mmregs", 0x4000);
972862b4a29SBALATON Zoltan     /* io space is alias to beginning of mmregs */
973862b4a29SBALATON Zoltan     memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
974862b4a29SBALATON Zoltan 
975862b4a29SBALATON Zoltan     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
976862b4a29SBALATON Zoltan     pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
977862b4a29SBALATON Zoltan     pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
978b7105d28SBALATON Zoltan 
979b7105d28SBALATON Zoltan     /* most interrupts are not yet emulated but MacOS needs at least VBlank */
980b7105d28SBALATON Zoltan     dev->config[PCI_INTERRUPT_PIN] = 1;
981b7105d28SBALATON Zoltan     timer_init_ns(&s->vblank_timer, QEMU_CLOCK_VIRTUAL, ati_vga_vblank_irq, s);
982862b4a29SBALATON Zoltan }
983862b4a29SBALATON Zoltan 
984862b4a29SBALATON Zoltan static void ati_vga_reset(DeviceState *dev)
985862b4a29SBALATON Zoltan {
986862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
987862b4a29SBALATON Zoltan 
988b7105d28SBALATON Zoltan     timer_del(&s->vblank_timer);
989b7105d28SBALATON Zoltan     ati_vga_update_irq(s);
990b7105d28SBALATON Zoltan 
991862b4a29SBALATON Zoltan     /* reset vga */
992862b4a29SBALATON Zoltan     vga_common_reset(&s->vga);
993862b4a29SBALATON Zoltan     s->mode = VGA_MODE;
994862b4a29SBALATON Zoltan }
995862b4a29SBALATON Zoltan 
996862b4a29SBALATON Zoltan static void ati_vga_exit(PCIDevice *dev)
997862b4a29SBALATON Zoltan {
998862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
999862b4a29SBALATON Zoltan 
1000b7105d28SBALATON Zoltan     timer_del(&s->vblank_timer);
1001862b4a29SBALATON Zoltan     graphic_console_close(s->vga.con);
1002862b4a29SBALATON Zoltan }
1003862b4a29SBALATON Zoltan 
1004862b4a29SBALATON Zoltan static Property ati_vga_properties[] = {
1005862b4a29SBALATON Zoltan     DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
1006862b4a29SBALATON Zoltan     DEFINE_PROP_STRING("model", ATIVGAState, model),
1007862b4a29SBALATON Zoltan     DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
1008862b4a29SBALATON Zoltan                        PCI_DEVICE_ID_ATI_RAGE128_PF),
1009862b4a29SBALATON Zoltan     DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
1010862b4a29SBALATON Zoltan     DEFINE_PROP_END_OF_LIST()
1011862b4a29SBALATON Zoltan };
1012862b4a29SBALATON Zoltan 
1013862b4a29SBALATON Zoltan static void ati_vga_class_init(ObjectClass *klass, void *data)
1014862b4a29SBALATON Zoltan {
1015862b4a29SBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
1016862b4a29SBALATON Zoltan     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1017862b4a29SBALATON Zoltan 
1018862b4a29SBALATON Zoltan     dc->reset = ati_vga_reset;
10194f67d30bSMarc-André Lureau     device_class_set_props(dc, ati_vga_properties);
1020862b4a29SBALATON Zoltan     dc->hotpluggable = false;
1021862b4a29SBALATON Zoltan     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1022862b4a29SBALATON Zoltan 
1023862b4a29SBALATON Zoltan     k->class_id = PCI_CLASS_DISPLAY_VGA;
1024862b4a29SBALATON Zoltan     k->vendor_id = PCI_VENDOR_ID_ATI;
1025862b4a29SBALATON Zoltan     k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
1026263807f4SGerd Hoffmann     k->romfile = "vgabios-ati.bin";
1027862b4a29SBALATON Zoltan     k->realize = ati_vga_realize;
1028862b4a29SBALATON Zoltan     k->exit = ati_vga_exit;
1029862b4a29SBALATON Zoltan }
1030862b4a29SBALATON Zoltan 
1031862b4a29SBALATON Zoltan static const TypeInfo ati_vga_info = {
1032862b4a29SBALATON Zoltan     .name = TYPE_ATI_VGA,
1033862b4a29SBALATON Zoltan     .parent = TYPE_PCI_DEVICE,
1034862b4a29SBALATON Zoltan     .instance_size = sizeof(ATIVGAState),
1035862b4a29SBALATON Zoltan     .class_init = ati_vga_class_init,
1036862b4a29SBALATON Zoltan     .interfaces = (InterfaceInfo[]) {
1037862b4a29SBALATON Zoltan           { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1038862b4a29SBALATON Zoltan           { },
1039862b4a29SBALATON Zoltan     },
1040862b4a29SBALATON Zoltan };
1041862b4a29SBALATON Zoltan 
1042862b4a29SBALATON Zoltan static void ati_vga_register_types(void)
1043862b4a29SBALATON Zoltan {
1044862b4a29SBALATON Zoltan     type_register_static(&ati_vga_info);
1045862b4a29SBALATON Zoltan }
1046862b4a29SBALATON Zoltan 
1047862b4a29SBALATON Zoltan type_init(ati_vga_register_types)
1048