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
35699f15fdSMarc-André Lureau #ifdef CONFIG_PIXMAN
36699f15fdSMarc-André Lureau #define DEFAULT_X_PIXMAN 3
37699f15fdSMarc-André Lureau #else
38699f15fdSMarc-André Lureau #define DEFAULT_X_PIXMAN 0
39699f15fdSMarc-André Lureau #endif
40699f15fdSMarc-André Lureau
41862b4a29SBALATON Zoltan static const struct {
42862b4a29SBALATON Zoltan const char *name;
43862b4a29SBALATON Zoltan uint16_t dev_id;
44862b4a29SBALATON Zoltan } ati_model_aliases[] = {
45862b4a29SBALATON Zoltan { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
46862b4a29SBALATON Zoltan { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
47862b4a29SBALATON Zoltan };
48862b4a29SBALATON Zoltan
49862b4a29SBALATON Zoltan enum { VGA_MODE, EXT_MODE };
50862b4a29SBALATON Zoltan
ati_vga_switch_mode(ATIVGAState * s)51862b4a29SBALATON Zoltan static void ati_vga_switch_mode(ATIVGAState *s)
52862b4a29SBALATON Zoltan {
53862b4a29SBALATON Zoltan DPRINTF("%d -> %d\n",
54862b4a29SBALATON Zoltan s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
55862b4a29SBALATON Zoltan if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
56862b4a29SBALATON Zoltan /* Extended mode enabled */
57862b4a29SBALATON Zoltan s->mode = EXT_MODE;
58862b4a29SBALATON Zoltan if (s->regs.crtc_gen_cntl & CRTC2_EN) {
59862b4a29SBALATON Zoltan /* CRT controller enabled, use CRTC values */
60c026350aSBALATON Zoltan /* FIXME Should these be the same as VGA CRTC regs? */
61862b4a29SBALATON Zoltan uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
62862b4a29SBALATON Zoltan int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
63862b4a29SBALATON Zoltan int bpp = 0;
64862b4a29SBALATON Zoltan int h, v;
65862b4a29SBALATON Zoltan
66862b4a29SBALATON Zoltan if (s->regs.crtc_h_total_disp == 0) {
67862b4a29SBALATON Zoltan s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
68862b4a29SBALATON Zoltan }
69862b4a29SBALATON Zoltan if (s->regs.crtc_v_total_disp == 0) {
70862b4a29SBALATON Zoltan s->regs.crtc_v_total_disp = (480 - 1) << 16;
71862b4a29SBALATON Zoltan }
72862b4a29SBALATON Zoltan h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
73862b4a29SBALATON Zoltan v = (s->regs.crtc_v_total_disp >> 16) + 1;
74862b4a29SBALATON Zoltan switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
75862b4a29SBALATON Zoltan case CRTC_PIX_WIDTH_4BPP:
76862b4a29SBALATON Zoltan bpp = 4;
77862b4a29SBALATON Zoltan break;
78862b4a29SBALATON Zoltan case CRTC_PIX_WIDTH_8BPP:
79862b4a29SBALATON Zoltan bpp = 8;
80862b4a29SBALATON Zoltan break;
81862b4a29SBALATON Zoltan case CRTC_PIX_WIDTH_15BPP:
82862b4a29SBALATON Zoltan bpp = 15;
83862b4a29SBALATON Zoltan break;
84862b4a29SBALATON Zoltan case CRTC_PIX_WIDTH_16BPP:
85862b4a29SBALATON Zoltan bpp = 16;
86862b4a29SBALATON Zoltan break;
87862b4a29SBALATON Zoltan case CRTC_PIX_WIDTH_24BPP:
88862b4a29SBALATON Zoltan bpp = 24;
89862b4a29SBALATON Zoltan break;
90862b4a29SBALATON Zoltan case CRTC_PIX_WIDTH_32BPP:
91862b4a29SBALATON Zoltan bpp = 32;
92862b4a29SBALATON Zoltan break;
93862b4a29SBALATON Zoltan default:
94862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
9541977c65SBALATON Zoltan return;
96862b4a29SBALATON Zoltan }
97862b4a29SBALATON Zoltan DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
98862b4a29SBALATON Zoltan vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
99862b4a29SBALATON Zoltan vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
1008bb9a2b2SBALATON Zoltan s->vga.big_endian_fb = (s->regs.config_cntl & APER_0_ENDIAN ||
1018bb9a2b2SBALATON Zoltan s->regs.config_cntl & APER_1_ENDIAN ?
1028bb9a2b2SBALATON Zoltan true : false);
103862b4a29SBALATON Zoltan /* reset VBE regs then set up mode */
104862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
105862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
106862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
107862b4a29SBALATON Zoltan /* enable mode via ioport so it updates vga regs */
108862b4a29SBALATON Zoltan vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
109862b4a29SBALATON Zoltan vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
110862b4a29SBALATON Zoltan VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
111862b4a29SBALATON Zoltan (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
112862b4a29SBALATON Zoltan /* now set offset and stride after enable as that resets these */
113862b4a29SBALATON Zoltan if (stride) {
114c026350aSBALATON Zoltan int bypp = DIV_ROUND_UP(bpp, BITS_PER_BYTE);
115c026350aSBALATON Zoltan
116862b4a29SBALATON Zoltan vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
117862b4a29SBALATON Zoltan vbe_ioport_write_data(&s->vga, 0, stride);
118c026350aSBALATON Zoltan stride *= bypp;
119c026350aSBALATON Zoltan if (offs % stride) {
120c026350aSBALATON Zoltan DPRINTF("CRTC offset is not multiple of pitch\n");
121c026350aSBALATON Zoltan vbe_ioport_write_index(&s->vga, 0,
122c026350aSBALATON Zoltan VBE_DISPI_INDEX_X_OFFSET);
123c026350aSBALATON Zoltan vbe_ioport_write_data(&s->vga, 0, offs % stride / bypp);
124c026350aSBALATON Zoltan }
125862b4a29SBALATON Zoltan vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
126862b4a29SBALATON Zoltan vbe_ioport_write_data(&s->vga, 0, offs / stride);
127c026350aSBALATON Zoltan DPRINTF("VBE offset (%d,%d), vbe_start_addr=%x\n",
128c026350aSBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_X_OFFSET],
129c026350aSBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_Y_OFFSET],
130c026350aSBALATON Zoltan s->vga.vbe_start_addr);
131862b4a29SBALATON Zoltan }
132862b4a29SBALATON Zoltan }
133862b4a29SBALATON Zoltan } else {
134862b4a29SBALATON Zoltan /* VGA mode enabled */
135862b4a29SBALATON Zoltan s->mode = VGA_MODE;
136862b4a29SBALATON Zoltan vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
137862b4a29SBALATON Zoltan vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
138862b4a29SBALATON Zoltan }
139862b4a29SBALATON Zoltan }
140862b4a29SBALATON Zoltan
141862b4a29SBALATON Zoltan /* Used by host side hardware cursor */
ati_cursor_define(ATIVGAState * s)142862b4a29SBALATON Zoltan static void ati_cursor_define(ATIVGAState *s)
143862b4a29SBALATON Zoltan {
144862b4a29SBALATON Zoltan uint8_t data[1024];
145aab0e2a6SGerd Hoffmann uint32_t srcoff;
146862b4a29SBALATON Zoltan int i, j, idx = 0;
147862b4a29SBALATON Zoltan
148862b4a29SBALATON Zoltan if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
149862b4a29SBALATON Zoltan return; /* Do not update cursor if locked or rendered by guest */
150862b4a29SBALATON Zoltan }
151862b4a29SBALATON Zoltan /* FIXME handle cur_hv_offs correctly */
152aab0e2a6SGerd Hoffmann srcoff = s->regs.cur_offset -
153747d7ad2SBALATON Zoltan (s->regs.cur_hv_offs >> 16) - (s->regs.cur_hv_offs & 0xffff) * 16;
154862b4a29SBALATON Zoltan for (i = 0; i < 64; i++) {
155862b4a29SBALATON Zoltan for (j = 0; j < 8; j++, idx++) {
156aab0e2a6SGerd Hoffmann data[idx] = vga_read_byte(&s->vga, srcoff + i * 16 + j);
157aab0e2a6SGerd Hoffmann data[512 + idx] = vga_read_byte(&s->vga, srcoff + i * 16 + j + 8);
158862b4a29SBALATON Zoltan }
159862b4a29SBALATON Zoltan }
160862b4a29SBALATON Zoltan if (!s->cursor) {
161862b4a29SBALATON Zoltan s->cursor = cursor_alloc(64, 64);
162862b4a29SBALATON Zoltan }
163862b4a29SBALATON Zoltan cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
164862b4a29SBALATON Zoltan &data[512], 1, &data[0]);
165862b4a29SBALATON Zoltan dpy_cursor_define(s->vga.con, s->cursor);
166862b4a29SBALATON Zoltan }
167862b4a29SBALATON Zoltan
168862b4a29SBALATON Zoltan /* Alternatively support guest rendered hardware cursor */
ati_cursor_invalidate(VGACommonState * vga)169862b4a29SBALATON Zoltan static void ati_cursor_invalidate(VGACommonState *vga)
170862b4a29SBALATON Zoltan {
171862b4a29SBALATON Zoltan ATIVGAState *s = container_of(vga, ATIVGAState, vga);
172862b4a29SBALATON Zoltan int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
173862b4a29SBALATON Zoltan
174862b4a29SBALATON Zoltan if (s->regs.cur_offset & BIT(31)) {
175862b4a29SBALATON Zoltan return; /* Do not update cursor if locked */
176862b4a29SBALATON Zoltan }
177862b4a29SBALATON Zoltan if (s->cursor_size != size ||
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 /* Remove old cursor then update and show new one if needed */
183862b4a29SBALATON Zoltan vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
184862b4a29SBALATON Zoltan vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
185862b4a29SBALATON Zoltan vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
186862b4a29SBALATON Zoltan s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
187862b4a29SBALATON Zoltan (s->regs.cur_hv_offs & 0xffff) * 16;
188862b4a29SBALATON Zoltan s->cursor_size = size;
189862b4a29SBALATON Zoltan if (size) {
190862b4a29SBALATON Zoltan vga_invalidate_scanlines(vga,
191862b4a29SBALATON Zoltan vga->hw_cursor_y, vga->hw_cursor_y + 63);
192862b4a29SBALATON Zoltan }
193862b4a29SBALATON Zoltan }
194862b4a29SBALATON Zoltan }
195862b4a29SBALATON Zoltan
ati_cursor_draw_line(VGACommonState * vga,uint8_t * d,int scr_y)196862b4a29SBALATON Zoltan static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
197862b4a29SBALATON Zoltan {
198862b4a29SBALATON Zoltan ATIVGAState *s = container_of(vga, ATIVGAState, vga);
199aab0e2a6SGerd Hoffmann uint32_t srcoff;
200862b4a29SBALATON Zoltan uint32_t *dp = (uint32_t *)d;
201862b4a29SBALATON Zoltan int i, j, h;
202862b4a29SBALATON Zoltan
203862b4a29SBALATON Zoltan if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
204862b4a29SBALATON Zoltan scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
205862b4a29SBALATON Zoltan scr_y > s->regs.crtc_v_total_disp >> 16) {
206862b4a29SBALATON Zoltan return;
207862b4a29SBALATON Zoltan }
208862b4a29SBALATON Zoltan /* FIXME handle cur_hv_offs correctly */
209aab0e2a6SGerd Hoffmann srcoff = s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
210862b4a29SBALATON Zoltan dp = &dp[vga->hw_cursor_x];
211862b4a29SBALATON Zoltan h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
212862b4a29SBALATON Zoltan for (i = 0; i < 8; i++) {
213862b4a29SBALATON Zoltan uint32_t color;
214aab0e2a6SGerd Hoffmann uint8_t abits = vga_read_byte(vga, srcoff + i);
215aab0e2a6SGerd Hoffmann uint8_t xbits = vga_read_byte(vga, srcoff + i + 8);
216862b4a29SBALATON Zoltan for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
217862b4a29SBALATON Zoltan if (abits & BIT(7)) {
218862b4a29SBALATON Zoltan if (xbits & BIT(7)) {
219862b4a29SBALATON Zoltan color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
220862b4a29SBALATON Zoltan } else {
221862b4a29SBALATON Zoltan continue; /* transparent, no change */
222862b4a29SBALATON Zoltan }
223862b4a29SBALATON Zoltan } else {
224862b4a29SBALATON Zoltan color = (xbits & BIT(7) ? s->regs.cur_color1 :
22550bc6af5SBALATON Zoltan s->regs.cur_color0) | 0xff000000;
226862b4a29SBALATON Zoltan }
227862b4a29SBALATON Zoltan if (vga->hw_cursor_x + i * 8 + j >= h) {
228862b4a29SBALATON Zoltan return; /* end of screen, don't span to next line */
229862b4a29SBALATON Zoltan }
230862b4a29SBALATON Zoltan dp[i * 8 + j] = color;
231862b4a29SBALATON Zoltan }
232862b4a29SBALATON Zoltan }
233862b4a29SBALATON Zoltan }
234862b4a29SBALATON Zoltan
ati_i2c(bitbang_i2c_interface * i2c,uint64_t data,int base)235c82c7336SBALATON Zoltan static uint64_t ati_i2c(bitbang_i2c_interface *i2c, uint64_t data, int base)
236c82c7336SBALATON Zoltan {
237c82c7336SBALATON Zoltan bool c = (data & BIT(base + 17) ? !!(data & BIT(base + 1)) : 1);
238c82c7336SBALATON Zoltan bool d = (data & BIT(base + 16) ? !!(data & BIT(base)) : 1);
239c82c7336SBALATON Zoltan
240c82c7336SBALATON Zoltan bitbang_i2c_set(i2c, BITBANG_I2C_SCL, c);
241c82c7336SBALATON Zoltan d = bitbang_i2c_set(i2c, BITBANG_I2C_SDA, d);
242c82c7336SBALATON Zoltan
243c82c7336SBALATON Zoltan data &= ~0xf00ULL;
244c82c7336SBALATON Zoltan if (c) {
245c82c7336SBALATON Zoltan data |= BIT(base + 9);
246c82c7336SBALATON Zoltan }
247c82c7336SBALATON Zoltan if (d) {
248c82c7336SBALATON Zoltan data |= BIT(base + 8);
249c82c7336SBALATON Zoltan }
250c82c7336SBALATON Zoltan return data;
251c82c7336SBALATON Zoltan }
252c82c7336SBALATON Zoltan
ati_vga_update_irq(ATIVGAState * s)253b7105d28SBALATON Zoltan static void ati_vga_update_irq(ATIVGAState *s)
254b7105d28SBALATON Zoltan {
255b7105d28SBALATON Zoltan pci_set_irq(&s->dev, !!(s->regs.gen_int_status & s->regs.gen_int_cntl));
256b7105d28SBALATON Zoltan }
257b7105d28SBALATON Zoltan
ati_vga_vblank_irq(void * opaque)258b7105d28SBALATON Zoltan static void ati_vga_vblank_irq(void *opaque)
259b7105d28SBALATON Zoltan {
260b7105d28SBALATON Zoltan ATIVGAState *s = opaque;
261b7105d28SBALATON Zoltan
262b7105d28SBALATON Zoltan timer_mod(&s->vblank_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
263b7105d28SBALATON Zoltan NANOSECONDS_PER_SECOND / 60);
264b7105d28SBALATON Zoltan s->regs.gen_int_status |= CRTC_VBLANK_INT;
265b7105d28SBALATON Zoltan ati_vga_update_irq(s);
266b7105d28SBALATON Zoltan }
267b7105d28SBALATON Zoltan
ati_reg_read_offs(uint32_t reg,int offs,unsigned int size)268862b4a29SBALATON Zoltan static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
269862b4a29SBALATON Zoltan unsigned int size)
270862b4a29SBALATON Zoltan {
271862b4a29SBALATON Zoltan if (offs == 0 && size == 4) {
272862b4a29SBALATON Zoltan return reg;
273862b4a29SBALATON Zoltan } else {
274862b4a29SBALATON Zoltan return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
275862b4a29SBALATON Zoltan }
276862b4a29SBALATON Zoltan }
277862b4a29SBALATON Zoltan
ati_mm_read(void * opaque,hwaddr addr,unsigned int size)278862b4a29SBALATON Zoltan static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
279862b4a29SBALATON Zoltan {
280862b4a29SBALATON Zoltan ATIVGAState *s = opaque;
281862b4a29SBALATON Zoltan uint64_t val = 0;
282862b4a29SBALATON Zoltan
283862b4a29SBALATON Zoltan switch (addr) {
284862b4a29SBALATON Zoltan case MM_INDEX:
285862b4a29SBALATON Zoltan val = s->regs.mm_index;
286862b4a29SBALATON Zoltan break;
287862b4a29SBALATON Zoltan case MM_DATA ... MM_DATA + 3:
288862b4a29SBALATON Zoltan /* indexed access to regs or memory */
289862b4a29SBALATON Zoltan if (s->regs.mm_index & BIT(31)) {
290339534d4SBALATON Zoltan uint32_t idx = s->regs.mm_index & ~BIT(31);
291339534d4SBALATON Zoltan if (idx <= s->vga.vram_size - size) {
292339534d4SBALATON Zoltan val = ldn_le_p(s->vga.vram_ptr + idx, size);
293862b4a29SBALATON Zoltan }
294a98610c4SPrasad J Pandit } else if (s->regs.mm_index > MM_DATA + 3) {
295862b4a29SBALATON Zoltan val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
296a98610c4SPrasad J Pandit } else {
297a98610c4SPrasad J Pandit qemu_log_mask(LOG_GUEST_ERROR,
298a98610c4SPrasad J Pandit "ati_mm_read: mm_index too small: %u\n", s->regs.mm_index);
299862b4a29SBALATON Zoltan }
300862b4a29SBALATON Zoltan break;
301862b4a29SBALATON Zoltan case BIOS_0_SCRATCH ... BUS_CNTL - 1:
302862b4a29SBALATON Zoltan {
303862b4a29SBALATON Zoltan int i = (addr - BIOS_0_SCRATCH) / 4;
304862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
305862b4a29SBALATON Zoltan break;
306862b4a29SBALATON Zoltan }
307862b4a29SBALATON Zoltan val = ati_reg_read_offs(s->regs.bios_scratch[i],
308862b4a29SBALATON Zoltan addr - (BIOS_0_SCRATCH + i * 4), size);
309862b4a29SBALATON Zoltan break;
310862b4a29SBALATON Zoltan }
311b7105d28SBALATON Zoltan case GEN_INT_CNTL:
312b7105d28SBALATON Zoltan val = s->regs.gen_int_cntl;
313b7105d28SBALATON Zoltan break;
314b7105d28SBALATON Zoltan case GEN_INT_STATUS:
315b7105d28SBALATON Zoltan val = s->regs.gen_int_status;
316b7105d28SBALATON Zoltan break;
317862b4a29SBALATON Zoltan case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
318862b4a29SBALATON Zoltan val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
319862b4a29SBALATON Zoltan addr - CRTC_GEN_CNTL, size);
320862b4a29SBALATON Zoltan break;
321862b4a29SBALATON Zoltan case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
322862b4a29SBALATON Zoltan val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
323862b4a29SBALATON Zoltan addr - CRTC_EXT_CNTL, size);
324862b4a29SBALATON Zoltan break;
325862b4a29SBALATON Zoltan case DAC_CNTL:
326862b4a29SBALATON Zoltan val = s->regs.dac_cntl;
327862b4a29SBALATON Zoltan break;
328e876b340SBALATON Zoltan case GPIO_VGA_DDC ... GPIO_VGA_DDC + 3:
329e876b340SBALATON Zoltan val = ati_reg_read_offs(s->regs.gpio_vga_ddc,
330e876b340SBALATON Zoltan addr - GPIO_VGA_DDC, size);
331c82c7336SBALATON Zoltan break;
332e876b340SBALATON Zoltan case GPIO_DVI_DDC ... GPIO_DVI_DDC + 3:
333e876b340SBALATON Zoltan val = ati_reg_read_offs(s->regs.gpio_dvi_ddc,
334e876b340SBALATON Zoltan addr - GPIO_DVI_DDC, size);
335c82c7336SBALATON Zoltan break;
336c82c7336SBALATON Zoltan case GPIO_MONID ... GPIO_MONID + 3:
337c82c7336SBALATON Zoltan val = ati_reg_read_offs(s->regs.gpio_monid,
338c82c7336SBALATON Zoltan addr - GPIO_MONID, size);
339c82c7336SBALATON Zoltan break;
340862b4a29SBALATON Zoltan case PALETTE_INDEX:
341862b4a29SBALATON Zoltan /* FIXME unaligned access */
342862b4a29SBALATON Zoltan val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
343862b4a29SBALATON Zoltan val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
344862b4a29SBALATON Zoltan break;
345862b4a29SBALATON Zoltan case PALETTE_DATA:
346862b4a29SBALATON Zoltan val = vga_ioport_read(&s->vga, VGA_PEL_D);
347862b4a29SBALATON Zoltan break;
348bf9ac62aSBALATON Zoltan case PALETTE_30_DATA:
349bf9ac62aSBALATON Zoltan val = s->regs.palette[vga_ioport_read(&s->vga, VGA_PEL_IR)];
350bf9ac62aSBALATON Zoltan break;
3518bb9a2b2SBALATON Zoltan case CNFG_CNTL:
3528bb9a2b2SBALATON Zoltan val = s->regs.config_cntl;
3538bb9a2b2SBALATON Zoltan break;
354862b4a29SBALATON Zoltan case CNFG_MEMSIZE:
355862b4a29SBALATON Zoltan val = s->vga.vram_size;
356862b4a29SBALATON Zoltan break;
3571d8d4d86SBALATON Zoltan case CONFIG_APER_0_BASE:
3581d8d4d86SBALATON Zoltan case CONFIG_APER_1_BASE:
3591d8d4d86SBALATON Zoltan val = pci_default_read_config(&s->dev,
3601d8d4d86SBALATON Zoltan PCI_BASE_ADDRESS_0, size) & 0xfffffff0;
3611d8d4d86SBALATON Zoltan break;
3621d8d4d86SBALATON Zoltan case CONFIG_APER_SIZE:
363f7ecde05SBALATON Zoltan val = s->vga.vram_size / 2;
3641d8d4d86SBALATON Zoltan break;
3651d8d4d86SBALATON Zoltan case CONFIG_REG_1_BASE:
3661d8d4d86SBALATON Zoltan val = pci_default_read_config(&s->dev,
3671d8d4d86SBALATON Zoltan PCI_BASE_ADDRESS_2, size) & 0xfffffff0;
3681d8d4d86SBALATON Zoltan break;
3691d8d4d86SBALATON Zoltan case CONFIG_REG_APER_SIZE:
370f7ecde05SBALATON Zoltan val = memory_region_size(&s->mm) / 2;
371f7ecde05SBALATON Zoltan break;
372f7ecde05SBALATON Zoltan case HOST_PATH_CNTL:
373f7ecde05SBALATON Zoltan val = BIT(23); /* Radeon HDP_APER_CNTL */
3741d8d4d86SBALATON Zoltan break;
375862b4a29SBALATON Zoltan case MC_STATUS:
376862b4a29SBALATON Zoltan val = 5;
377862b4a29SBALATON Zoltan break;
3782bbcaa7cSBALATON Zoltan case MEM_SDRAM_MODE_REG:
3792bbcaa7cSBALATON Zoltan if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
3802bbcaa7cSBALATON Zoltan val = BIT(28) | BIT(20);
3812bbcaa7cSBALATON Zoltan }
3822bbcaa7cSBALATON Zoltan break;
383862b4a29SBALATON Zoltan case RBBM_STATUS:
384862b4a29SBALATON Zoltan case GUI_STAT:
385862b4a29SBALATON Zoltan val = 64; /* free CMDFIFO entries */
386862b4a29SBALATON Zoltan break;
387862b4a29SBALATON Zoltan case CRTC_H_TOTAL_DISP:
388862b4a29SBALATON Zoltan val = s->regs.crtc_h_total_disp;
389862b4a29SBALATON Zoltan break;
390862b4a29SBALATON Zoltan case CRTC_H_SYNC_STRT_WID:
391862b4a29SBALATON Zoltan val = s->regs.crtc_h_sync_strt_wid;
392862b4a29SBALATON Zoltan break;
393862b4a29SBALATON Zoltan case CRTC_V_TOTAL_DISP:
394862b4a29SBALATON Zoltan val = s->regs.crtc_v_total_disp;
395862b4a29SBALATON Zoltan break;
396862b4a29SBALATON Zoltan case CRTC_V_SYNC_STRT_WID:
397862b4a29SBALATON Zoltan val = s->regs.crtc_v_sync_strt_wid;
398862b4a29SBALATON Zoltan break;
399862b4a29SBALATON Zoltan case CRTC_OFFSET:
400862b4a29SBALATON Zoltan val = s->regs.crtc_offset;
401862b4a29SBALATON Zoltan break;
402862b4a29SBALATON Zoltan case CRTC_OFFSET_CNTL:
403862b4a29SBALATON Zoltan val = s->regs.crtc_offset_cntl;
404862b4a29SBALATON Zoltan break;
405862b4a29SBALATON Zoltan case CRTC_PITCH:
406862b4a29SBALATON Zoltan val = s->regs.crtc_pitch;
407862b4a29SBALATON Zoltan break;
408862b4a29SBALATON Zoltan case 0xf00 ... 0xfff:
409862b4a29SBALATON Zoltan val = pci_default_read_config(&s->dev, addr - 0xf00, size);
410862b4a29SBALATON Zoltan break;
411d634c883SBALATON Zoltan case CUR_OFFSET ... CUR_OFFSET + 3:
412d634c883SBALATON Zoltan val = ati_reg_read_offs(s->regs.cur_offset, addr - CUR_OFFSET, size);
413862b4a29SBALATON Zoltan break;
414d634c883SBALATON Zoltan case CUR_HORZ_VERT_POSN ... CUR_HORZ_VERT_POSN + 3:
415d634c883SBALATON Zoltan val = ati_reg_read_offs(s->regs.cur_hv_pos,
416d634c883SBALATON Zoltan addr - CUR_HORZ_VERT_POSN, size);
417d634c883SBALATON Zoltan if (addr + size > CUR_HORZ_VERT_POSN + 3) {
418d634c883SBALATON Zoltan val |= (s->regs.cur_offset & BIT(31)) >> (4 - size);
419d634c883SBALATON Zoltan }
420862b4a29SBALATON Zoltan break;
421d634c883SBALATON Zoltan case CUR_HORZ_VERT_OFF ... CUR_HORZ_VERT_OFF + 3:
422d634c883SBALATON Zoltan val = ati_reg_read_offs(s->regs.cur_hv_offs,
423d634c883SBALATON Zoltan addr - CUR_HORZ_VERT_OFF, size);
424d634c883SBALATON Zoltan if (addr + size > CUR_HORZ_VERT_OFF + 3) {
425d634c883SBALATON Zoltan val |= (s->regs.cur_offset & BIT(31)) >> (4 - size);
426d634c883SBALATON Zoltan }
427862b4a29SBALATON Zoltan break;
428d634c883SBALATON Zoltan case CUR_CLR0 ... CUR_CLR0 + 3:
429d634c883SBALATON Zoltan val = ati_reg_read_offs(s->regs.cur_color0, addr - CUR_CLR0, size);
430862b4a29SBALATON Zoltan break;
431d634c883SBALATON Zoltan case CUR_CLR1 ... CUR_CLR1 + 3:
432d634c883SBALATON Zoltan val = ati_reg_read_offs(s->regs.cur_color1, addr - CUR_CLR1, size);
433862b4a29SBALATON Zoltan break;
434862b4a29SBALATON Zoltan case DST_OFFSET:
435862b4a29SBALATON Zoltan val = s->regs.dst_offset;
436862b4a29SBALATON Zoltan break;
437862b4a29SBALATON Zoltan case DST_PITCH:
438862b4a29SBALATON Zoltan val = s->regs.dst_pitch;
439862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
440862b4a29SBALATON Zoltan val &= s->regs.dst_tile << 16;
441862b4a29SBALATON Zoltan }
442862b4a29SBALATON Zoltan break;
443862b4a29SBALATON Zoltan case DST_WIDTH:
444862b4a29SBALATON Zoltan val = s->regs.dst_width;
445862b4a29SBALATON Zoltan break;
446862b4a29SBALATON Zoltan case DST_HEIGHT:
447862b4a29SBALATON Zoltan val = s->regs.dst_height;
448862b4a29SBALATON Zoltan break;
449862b4a29SBALATON Zoltan case SRC_X:
450862b4a29SBALATON Zoltan val = s->regs.src_x;
451862b4a29SBALATON Zoltan break;
452862b4a29SBALATON Zoltan case SRC_Y:
453862b4a29SBALATON Zoltan val = s->regs.src_y;
454862b4a29SBALATON Zoltan break;
455862b4a29SBALATON Zoltan case DST_X:
456862b4a29SBALATON Zoltan val = s->regs.dst_x;
457862b4a29SBALATON Zoltan break;
458862b4a29SBALATON Zoltan case DST_Y:
459862b4a29SBALATON Zoltan val = s->regs.dst_y;
460862b4a29SBALATON Zoltan break;
461862b4a29SBALATON Zoltan case DP_GUI_MASTER_CNTL:
462862b4a29SBALATON Zoltan val = s->regs.dp_gui_master_cntl;
463862b4a29SBALATON Zoltan break;
464862b4a29SBALATON Zoltan case SRC_OFFSET:
465862b4a29SBALATON Zoltan val = s->regs.src_offset;
466862b4a29SBALATON Zoltan break;
467862b4a29SBALATON Zoltan case SRC_PITCH:
468862b4a29SBALATON Zoltan val = s->regs.src_pitch;
469862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
470862b4a29SBALATON Zoltan val &= s->regs.src_tile << 16;
471862b4a29SBALATON Zoltan }
472862b4a29SBALATON Zoltan break;
473862b4a29SBALATON Zoltan case DP_BRUSH_BKGD_CLR:
474862b4a29SBALATON Zoltan val = s->regs.dp_brush_bkgd_clr;
475862b4a29SBALATON Zoltan break;
476862b4a29SBALATON Zoltan case DP_BRUSH_FRGD_CLR:
477862b4a29SBALATON Zoltan val = s->regs.dp_brush_frgd_clr;
478862b4a29SBALATON Zoltan break;
479862b4a29SBALATON Zoltan case DP_SRC_FRGD_CLR:
480862b4a29SBALATON Zoltan val = s->regs.dp_src_frgd_clr;
481862b4a29SBALATON Zoltan break;
482862b4a29SBALATON Zoltan case DP_SRC_BKGD_CLR:
483862b4a29SBALATON Zoltan val = s->regs.dp_src_bkgd_clr;
484862b4a29SBALATON Zoltan break;
485862b4a29SBALATON Zoltan case DP_CNTL:
486862b4a29SBALATON Zoltan val = s->regs.dp_cntl;
487862b4a29SBALATON Zoltan break;
488862b4a29SBALATON Zoltan case DP_DATATYPE:
489862b4a29SBALATON Zoltan val = s->regs.dp_datatype;
490862b4a29SBALATON Zoltan break;
491862b4a29SBALATON Zoltan case DP_MIX:
492862b4a29SBALATON Zoltan val = s->regs.dp_mix;
493862b4a29SBALATON Zoltan break;
494862b4a29SBALATON Zoltan case DP_WRITE_MASK:
495862b4a29SBALATON Zoltan val = s->regs.dp_write_mask;
496862b4a29SBALATON Zoltan break;
497862b4a29SBALATON Zoltan case DEFAULT_OFFSET:
498862b4a29SBALATON Zoltan val = s->regs.default_offset;
499866ad5f5SBALATON Zoltan if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
500866ad5f5SBALATON Zoltan val >>= 10;
501866ad5f5SBALATON Zoltan val |= s->regs.default_pitch << 16;
502866ad5f5SBALATON Zoltan val |= s->regs.default_tile << 30;
503866ad5f5SBALATON Zoltan }
504862b4a29SBALATON Zoltan break;
505862b4a29SBALATON Zoltan case DEFAULT_PITCH:
506862b4a29SBALATON Zoltan val = s->regs.default_pitch;
507866ad5f5SBALATON Zoltan val |= s->regs.default_tile << 16;
508862b4a29SBALATON Zoltan break;
509862b4a29SBALATON Zoltan case DEFAULT_SC_BOTTOM_RIGHT:
510862b4a29SBALATON Zoltan val = s->regs.default_sc_bottom_right;
511862b4a29SBALATON Zoltan break;
512862b4a29SBALATON Zoltan default:
513862b4a29SBALATON Zoltan break;
514862b4a29SBALATON Zoltan }
515862b4a29SBALATON Zoltan if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
516862b4a29SBALATON Zoltan trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
517862b4a29SBALATON Zoltan }
518862b4a29SBALATON Zoltan return val;
519862b4a29SBALATON Zoltan }
520862b4a29SBALATON Zoltan
ati_reg_write_offs(uint32_t * reg,int offs,uint64_t data,unsigned int size)521862b4a29SBALATON Zoltan static inline void ati_reg_write_offs(uint32_t *reg, int offs,
522862b4a29SBALATON Zoltan uint64_t data, unsigned int size)
523862b4a29SBALATON Zoltan {
524862b4a29SBALATON Zoltan if (offs == 0 && size == 4) {
525862b4a29SBALATON Zoltan *reg = data;
526862b4a29SBALATON Zoltan } else {
527862b4a29SBALATON Zoltan *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
528862b4a29SBALATON Zoltan data);
529862b4a29SBALATON Zoltan }
530862b4a29SBALATON Zoltan }
531862b4a29SBALATON Zoltan
ati_mm_write(void * opaque,hwaddr addr,uint64_t data,unsigned int size)532862b4a29SBALATON Zoltan static void ati_mm_write(void *opaque, hwaddr addr,
533862b4a29SBALATON Zoltan uint64_t data, unsigned int size)
534862b4a29SBALATON Zoltan {
535862b4a29SBALATON Zoltan ATIVGAState *s = opaque;
536862b4a29SBALATON Zoltan
537862b4a29SBALATON Zoltan if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
538862b4a29SBALATON Zoltan trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
539862b4a29SBALATON Zoltan }
540862b4a29SBALATON Zoltan switch (addr) {
541862b4a29SBALATON Zoltan case MM_INDEX:
542b0588cb5SBALATON Zoltan s->regs.mm_index = data & ~3;
543862b4a29SBALATON Zoltan break;
544862b4a29SBALATON Zoltan case MM_DATA ... MM_DATA + 3:
545862b4a29SBALATON Zoltan /* indexed access to regs or memory */
546862b4a29SBALATON Zoltan if (s->regs.mm_index & BIT(31)) {
547339534d4SBALATON Zoltan uint32_t idx = s->regs.mm_index & ~BIT(31);
548339534d4SBALATON Zoltan if (idx <= s->vga.vram_size - size) {
549339534d4SBALATON Zoltan stn_le_p(s->vga.vram_ptr + idx, size, data);
550862b4a29SBALATON Zoltan }
551a98610c4SPrasad J Pandit } else if (s->regs.mm_index > MM_DATA + 3) {
552862b4a29SBALATON Zoltan ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
553a98610c4SPrasad J Pandit } else {
554a98610c4SPrasad J Pandit qemu_log_mask(LOG_GUEST_ERROR,
555a98610c4SPrasad J Pandit "ati_mm_write: mm_index too small: %u\n", s->regs.mm_index);
556862b4a29SBALATON Zoltan }
557862b4a29SBALATON Zoltan break;
558862b4a29SBALATON Zoltan case BIOS_0_SCRATCH ... BUS_CNTL - 1:
559862b4a29SBALATON Zoltan {
560862b4a29SBALATON Zoltan int i = (addr - BIOS_0_SCRATCH) / 4;
561862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
562862b4a29SBALATON Zoltan break;
563862b4a29SBALATON Zoltan }
564862b4a29SBALATON Zoltan ati_reg_write_offs(&s->regs.bios_scratch[i],
565862b4a29SBALATON Zoltan addr - (BIOS_0_SCRATCH + i * 4), data, size);
566862b4a29SBALATON Zoltan break;
567862b4a29SBALATON Zoltan }
568b7105d28SBALATON Zoltan case GEN_INT_CNTL:
569b7105d28SBALATON Zoltan s->regs.gen_int_cntl = data;
570b7105d28SBALATON Zoltan if (data & CRTC_VBLANK_INT) {
571b7105d28SBALATON Zoltan ati_vga_vblank_irq(s);
572b7105d28SBALATON Zoltan } else {
573b7105d28SBALATON Zoltan timer_del(&s->vblank_timer);
574b7105d28SBALATON Zoltan ati_vga_update_irq(s);
575b7105d28SBALATON Zoltan }
576b7105d28SBALATON Zoltan break;
577b7105d28SBALATON Zoltan case GEN_INT_STATUS:
578b7105d28SBALATON Zoltan data &= (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF ?
579b7105d28SBALATON Zoltan 0x000f040fUL : 0xfc080effUL);
580b7105d28SBALATON Zoltan s->regs.gen_int_status &= ~data;
581b7105d28SBALATON Zoltan ati_vga_update_irq(s);
582b7105d28SBALATON Zoltan break;
583862b4a29SBALATON Zoltan case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
584862b4a29SBALATON Zoltan {
585862b4a29SBALATON Zoltan uint32_t val = s->regs.crtc_gen_cntl;
586862b4a29SBALATON Zoltan ati_reg_write_offs(&s->regs.crtc_gen_cntl,
587862b4a29SBALATON Zoltan addr - CRTC_GEN_CNTL, data, size);
588862b4a29SBALATON Zoltan if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
589862b4a29SBALATON Zoltan if (s->cursor_guest_mode) {
590862b4a29SBALATON Zoltan s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
591862b4a29SBALATON Zoltan } else {
592862b4a29SBALATON Zoltan if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
593862b4a29SBALATON Zoltan ati_cursor_define(s);
594862b4a29SBALATON Zoltan }
595862b4a29SBALATON Zoltan dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
596862b4a29SBALATON Zoltan s->regs.cur_hv_pos & 0xffff,
597862b4a29SBALATON Zoltan (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
598862b4a29SBALATON Zoltan }
599862b4a29SBALATON Zoltan }
600862b4a29SBALATON Zoltan if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
601862b4a29SBALATON Zoltan (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
602862b4a29SBALATON Zoltan ati_vga_switch_mode(s);
603862b4a29SBALATON Zoltan }
604862b4a29SBALATON Zoltan break;
605862b4a29SBALATON Zoltan }
606862b4a29SBALATON Zoltan case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
607862b4a29SBALATON Zoltan {
608862b4a29SBALATON Zoltan uint32_t val = s->regs.crtc_ext_cntl;
609862b4a29SBALATON Zoltan ati_reg_write_offs(&s->regs.crtc_ext_cntl,
610862b4a29SBALATON Zoltan addr - CRTC_EXT_CNTL, data, size);
611862b4a29SBALATON Zoltan if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
612862b4a29SBALATON Zoltan DPRINTF("Display disabled\n");
613862b4a29SBALATON Zoltan s->vga.ar_index &= ~BIT(5);
614862b4a29SBALATON Zoltan } else {
615862b4a29SBALATON Zoltan DPRINTF("Display enabled\n");
616862b4a29SBALATON Zoltan s->vga.ar_index |= BIT(5);
617862b4a29SBALATON Zoltan ati_vga_switch_mode(s);
618862b4a29SBALATON Zoltan }
619862b4a29SBALATON Zoltan if ((val & CRT_CRTC_DISPLAY_DIS) !=
620862b4a29SBALATON Zoltan (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
621862b4a29SBALATON Zoltan ati_vga_switch_mode(s);
622862b4a29SBALATON Zoltan }
623862b4a29SBALATON Zoltan break;
624862b4a29SBALATON Zoltan }
625862b4a29SBALATON Zoltan case DAC_CNTL:
626862b4a29SBALATON Zoltan s->regs.dac_cntl = data & 0xffffe3ff;
627862b4a29SBALATON Zoltan s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
628862b4a29SBALATON Zoltan break;
629e876b340SBALATON Zoltan /*
630e876b340SBALATON Zoltan * GPIO regs for DDC access. Because some drivers access these via
631e876b340SBALATON Zoltan * multiple byte writes we have to be careful when we send bits to
632e876b340SBALATON Zoltan * avoid spurious changes in bitbang_i2c state. Only do it when either
633e876b340SBALATON Zoltan * the enable bits are changed or output bits changed while enabled.
634e876b340SBALATON Zoltan */
635e876b340SBALATON Zoltan case GPIO_VGA_DDC ... GPIO_VGA_DDC + 3:
636c82c7336SBALATON Zoltan if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
637c82c7336SBALATON Zoltan /* FIXME: Maybe add a property to select VGA or DVI port? */
638c82c7336SBALATON Zoltan }
639c82c7336SBALATON Zoltan break;
640e876b340SBALATON Zoltan case GPIO_DVI_DDC ... GPIO_DVI_DDC + 3:
641c82c7336SBALATON Zoltan if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
642e876b340SBALATON Zoltan ati_reg_write_offs(&s->regs.gpio_dvi_ddc,
643e876b340SBALATON Zoltan addr - GPIO_DVI_DDC, data, size);
644e876b340SBALATON Zoltan if ((addr <= GPIO_DVI_DDC + 2 && addr + size > GPIO_DVI_DDC + 2) ||
645e876b340SBALATON Zoltan (addr == GPIO_DVI_DDC && (s->regs.gpio_dvi_ddc & 0x30000))) {
646e876b340SBALATON Zoltan s->regs.gpio_dvi_ddc = ati_i2c(&s->bbi2c,
647e876b340SBALATON Zoltan s->regs.gpio_dvi_ddc, 0);
648e876b340SBALATON Zoltan }
649c82c7336SBALATON Zoltan }
650c82c7336SBALATON Zoltan break;
651c82c7336SBALATON Zoltan case GPIO_MONID ... GPIO_MONID + 3:
652c82c7336SBALATON Zoltan /* FIXME What does Radeon have here? */
653c82c7336SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
654e876b340SBALATON Zoltan /* Rage128p accesses DDC via MONID(1-2) with additional mask bit */
655c82c7336SBALATON Zoltan ati_reg_write_offs(&s->regs.gpio_monid,
656c82c7336SBALATON Zoltan addr - GPIO_MONID, data, size);
657c82c7336SBALATON Zoltan if ((s->regs.gpio_monid & BIT(25)) &&
658006388a8SBALATON Zoltan ((addr <= GPIO_MONID + 2 && addr + size > GPIO_MONID + 2) ||
659006388a8SBALATON Zoltan (addr == GPIO_MONID && (s->regs.gpio_monid & 0x60000)))) {
66041742927SPeter Maydell s->regs.gpio_monid = ati_i2c(&s->bbi2c, s->regs.gpio_monid, 1);
661c82c7336SBALATON Zoltan }
662c82c7336SBALATON Zoltan }
663c82c7336SBALATON Zoltan break;
664862b4a29SBALATON Zoltan case PALETTE_INDEX ... PALETTE_INDEX + 3:
665862b4a29SBALATON Zoltan if (size == 4) {
666862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
667862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
668862b4a29SBALATON Zoltan } else {
669862b4a29SBALATON Zoltan if (addr == PALETTE_INDEX) {
670862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
671862b4a29SBALATON Zoltan } else {
672862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
673862b4a29SBALATON Zoltan }
674862b4a29SBALATON Zoltan }
675862b4a29SBALATON Zoltan break;
676862b4a29SBALATON Zoltan case PALETTE_DATA ... PALETTE_DATA + 3:
677862b4a29SBALATON Zoltan data <<= addr - PALETTE_DATA;
678862b4a29SBALATON Zoltan data = bswap32(data) >> 8;
679862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
680862b4a29SBALATON Zoltan data >>= 8;
681862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
682862b4a29SBALATON Zoltan data >>= 8;
683862b4a29SBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
684862b4a29SBALATON Zoltan break;
685bf9ac62aSBALATON Zoltan case PALETTE_30_DATA:
686bf9ac62aSBALATON Zoltan s->regs.palette[vga_ioport_read(&s->vga, VGA_PEL_IW)] = data;
687bf9ac62aSBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_D, (data >> 22) & 0xff);
688bf9ac62aSBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_D, (data >> 12) & 0xff);
689bf9ac62aSBALATON Zoltan vga_ioport_write(&s->vga, VGA_PEL_D, (data >> 2) & 0xff);
690bf9ac62aSBALATON Zoltan break;
6918bb9a2b2SBALATON Zoltan case CNFG_CNTL:
6928bb9a2b2SBALATON Zoltan s->regs.config_cntl = data;
6938bb9a2b2SBALATON Zoltan break;
694862b4a29SBALATON Zoltan case CRTC_H_TOTAL_DISP:
695862b4a29SBALATON Zoltan s->regs.crtc_h_total_disp = data & 0x07ff07ff;
696862b4a29SBALATON Zoltan break;
697862b4a29SBALATON Zoltan case CRTC_H_SYNC_STRT_WID:
698862b4a29SBALATON Zoltan s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
699862b4a29SBALATON Zoltan break;
700862b4a29SBALATON Zoltan case CRTC_V_TOTAL_DISP:
701862b4a29SBALATON Zoltan s->regs.crtc_v_total_disp = data & 0x0fff0fff;
702862b4a29SBALATON Zoltan break;
703862b4a29SBALATON Zoltan case CRTC_V_SYNC_STRT_WID:
704862b4a29SBALATON Zoltan s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
705862b4a29SBALATON Zoltan break;
706862b4a29SBALATON Zoltan case CRTC_OFFSET:
707862b4a29SBALATON Zoltan s->regs.crtc_offset = data & 0xc7ffffff;
708862b4a29SBALATON Zoltan break;
709862b4a29SBALATON Zoltan case CRTC_OFFSET_CNTL:
710862b4a29SBALATON Zoltan s->regs.crtc_offset_cntl = data; /* FIXME */
711862b4a29SBALATON Zoltan break;
712862b4a29SBALATON Zoltan case CRTC_PITCH:
713862b4a29SBALATON Zoltan s->regs.crtc_pitch = data & 0x07ff07ff;
714862b4a29SBALATON Zoltan break;
715862b4a29SBALATON Zoltan case 0xf00 ... 0xfff:
716862b4a29SBALATON Zoltan /* read-only copy of PCI config space so ignore writes */
717862b4a29SBALATON Zoltan break;
718d634c883SBALATON Zoltan case CUR_OFFSET ... CUR_OFFSET + 3:
719d634c883SBALATON Zoltan {
720d634c883SBALATON Zoltan uint32_t t = s->regs.cur_offset;
721d634c883SBALATON Zoltan
722d634c883SBALATON Zoltan ati_reg_write_offs(&t, addr - CUR_OFFSET, data, size);
723d634c883SBALATON Zoltan t &= 0x87fffff0;
724d634c883SBALATON Zoltan if (s->regs.cur_offset != t) {
725d634c883SBALATON Zoltan s->regs.cur_offset = t;
726862b4a29SBALATON Zoltan ati_cursor_define(s);
727862b4a29SBALATON Zoltan }
728862b4a29SBALATON Zoltan break;
729d634c883SBALATON Zoltan }
730d634c883SBALATON Zoltan case CUR_HORZ_VERT_POSN ... CUR_HORZ_VERT_POSN + 3:
731d634c883SBALATON Zoltan {
732d634c883SBALATON Zoltan uint32_t t = s->regs.cur_hv_pos | (s->regs.cur_offset & BIT(31));
733d634c883SBALATON Zoltan
734d634c883SBALATON Zoltan ati_reg_write_offs(&t, addr - CUR_HORZ_VERT_POSN, data, size);
735d634c883SBALATON Zoltan s->regs.cur_hv_pos = t & 0x3fff0fff;
736d634c883SBALATON Zoltan if (t & BIT(31)) {
737d634c883SBALATON Zoltan s->regs.cur_offset |= t & BIT(31);
738862b4a29SBALATON Zoltan } else if (s->regs.cur_offset & BIT(31)) {
739862b4a29SBALATON Zoltan s->regs.cur_offset &= ~BIT(31);
740862b4a29SBALATON Zoltan ati_cursor_define(s);
741862b4a29SBALATON Zoltan }
742862b4a29SBALATON Zoltan if (!s->cursor_guest_mode &&
743d634c883SBALATON Zoltan (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(t & BIT(31))) {
744862b4a29SBALATON Zoltan dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
745a418e7aeSAkihiko Odaki s->regs.cur_hv_pos & 0xffff, true);
746862b4a29SBALATON Zoltan }
747862b4a29SBALATON Zoltan break;
748d634c883SBALATON Zoltan }
749862b4a29SBALATON Zoltan case CUR_HORZ_VERT_OFF:
750d634c883SBALATON Zoltan {
751d634c883SBALATON Zoltan uint32_t t = s->regs.cur_hv_offs | (s->regs.cur_offset & BIT(31));
752d634c883SBALATON Zoltan
753d634c883SBALATON Zoltan ati_reg_write_offs(&t, addr - CUR_HORZ_VERT_OFF, data, size);
754d634c883SBALATON Zoltan s->regs.cur_hv_offs = t & 0x3f003f;
755d634c883SBALATON Zoltan if (t & BIT(31)) {
756d634c883SBALATON Zoltan s->regs.cur_offset |= t & BIT(31);
757862b4a29SBALATON Zoltan } else if (s->regs.cur_offset & BIT(31)) {
758862b4a29SBALATON Zoltan s->regs.cur_offset &= ~BIT(31);
759862b4a29SBALATON Zoltan ati_cursor_define(s);
760862b4a29SBALATON Zoltan }
761862b4a29SBALATON Zoltan break;
762d634c883SBALATON Zoltan }
763d634c883SBALATON Zoltan case CUR_CLR0 ... CUR_CLR0 + 3:
764d634c883SBALATON Zoltan {
765d634c883SBALATON Zoltan uint32_t t = s->regs.cur_color0;
766d634c883SBALATON Zoltan
767d634c883SBALATON Zoltan ati_reg_write_offs(&t, addr - CUR_CLR0, data, size);
768d634c883SBALATON Zoltan t &= 0xffffff;
769d634c883SBALATON Zoltan if (s->regs.cur_color0 != t) {
770d634c883SBALATON Zoltan s->regs.cur_color0 = t;
771862b4a29SBALATON Zoltan ati_cursor_define(s);
772862b4a29SBALATON Zoltan }
773862b4a29SBALATON Zoltan break;
774d634c883SBALATON Zoltan }
775d634c883SBALATON Zoltan case CUR_CLR1 ... CUR_CLR1 + 3:
776862b4a29SBALATON Zoltan /*
777862b4a29SBALATON Zoltan * Update cursor unconditionally here because some clients set up
778862b4a29SBALATON Zoltan * other registers before actually writing cursor data to memory at
779862b4a29SBALATON Zoltan * offset so we would miss cursor change unless always updating here
780862b4a29SBALATON Zoltan */
781d634c883SBALATON Zoltan ati_reg_write_offs(&s->regs.cur_color1, addr - CUR_CLR1, data, size);
782d634c883SBALATON Zoltan s->regs.cur_color1 &= 0xffffff;
783862b4a29SBALATON Zoltan ati_cursor_define(s);
784862b4a29SBALATON Zoltan break;
785862b4a29SBALATON Zoltan case DST_OFFSET:
786862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
787862b4a29SBALATON Zoltan s->regs.dst_offset = data & 0xfffffff0;
788862b4a29SBALATON Zoltan } else {
789862b4a29SBALATON Zoltan s->regs.dst_offset = data & 0xfffffc00;
790862b4a29SBALATON Zoltan }
791862b4a29SBALATON Zoltan break;
792862b4a29SBALATON Zoltan case DST_PITCH:
793862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
794862b4a29SBALATON Zoltan s->regs.dst_pitch = data & 0x3fff;
795862b4a29SBALATON Zoltan s->regs.dst_tile = (data >> 16) & 1;
796862b4a29SBALATON Zoltan } else {
797862b4a29SBALATON Zoltan s->regs.dst_pitch = data & 0x3ff0;
798862b4a29SBALATON Zoltan }
799862b4a29SBALATON Zoltan break;
800862b4a29SBALATON Zoltan case DST_TILE:
801862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
802862b4a29SBALATON Zoltan s->regs.dst_tile = data & 3;
803862b4a29SBALATON Zoltan }
804862b4a29SBALATON Zoltan break;
805862b4a29SBALATON Zoltan case DST_WIDTH:
806862b4a29SBALATON Zoltan s->regs.dst_width = data & 0x3fff;
807862b4a29SBALATON Zoltan ati_2d_blt(s);
808862b4a29SBALATON Zoltan break;
809862b4a29SBALATON Zoltan case DST_HEIGHT:
810862b4a29SBALATON Zoltan s->regs.dst_height = data & 0x3fff;
811862b4a29SBALATON Zoltan break;
812862b4a29SBALATON Zoltan case SRC_X:
813862b4a29SBALATON Zoltan s->regs.src_x = data & 0x3fff;
814862b4a29SBALATON Zoltan break;
815862b4a29SBALATON Zoltan case SRC_Y:
816862b4a29SBALATON Zoltan s->regs.src_y = data & 0x3fff;
817862b4a29SBALATON Zoltan break;
818862b4a29SBALATON Zoltan case DST_X:
819862b4a29SBALATON Zoltan s->regs.dst_x = data & 0x3fff;
820862b4a29SBALATON Zoltan break;
821862b4a29SBALATON Zoltan case DST_Y:
822862b4a29SBALATON Zoltan s->regs.dst_y = data & 0x3fff;
823862b4a29SBALATON Zoltan break;
824862b4a29SBALATON Zoltan case SRC_PITCH_OFFSET:
825862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
826146dd326SBALATON Zoltan s->regs.src_offset = (data & 0x1fffff) << 5;
827866ad5f5SBALATON Zoltan s->regs.src_pitch = (data & 0x7fe00000) >> 21;
828862b4a29SBALATON Zoltan s->regs.src_tile = data >> 31;
829862b4a29SBALATON Zoltan } else {
830866ad5f5SBALATON Zoltan s->regs.src_offset = (data & 0x3fffff) << 10;
831862b4a29SBALATON Zoltan s->regs.src_pitch = (data & 0x3fc00000) >> 16;
832862b4a29SBALATON Zoltan s->regs.src_tile = (data >> 30) & 1;
833862b4a29SBALATON Zoltan }
834862b4a29SBALATON Zoltan break;
835862b4a29SBALATON Zoltan case DST_PITCH_OFFSET:
836862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
837146dd326SBALATON Zoltan s->regs.dst_offset = (data & 0x1fffff) << 5;
838866ad5f5SBALATON Zoltan s->regs.dst_pitch = (data & 0x7fe00000) >> 21;
839862b4a29SBALATON Zoltan s->regs.dst_tile = data >> 31;
840862b4a29SBALATON Zoltan } else {
841866ad5f5SBALATON Zoltan s->regs.dst_offset = (data & 0x3fffff) << 10;
842862b4a29SBALATON Zoltan s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
843862b4a29SBALATON Zoltan s->regs.dst_tile = data >> 30;
844862b4a29SBALATON Zoltan }
845862b4a29SBALATON Zoltan break;
846862b4a29SBALATON Zoltan case SRC_Y_X:
847862b4a29SBALATON Zoltan s->regs.src_x = data & 0x3fff;
848862b4a29SBALATON Zoltan s->regs.src_y = (data >> 16) & 0x3fff;
849862b4a29SBALATON Zoltan break;
850862b4a29SBALATON Zoltan case DST_Y_X:
851862b4a29SBALATON Zoltan s->regs.dst_x = data & 0x3fff;
852862b4a29SBALATON Zoltan s->regs.dst_y = (data >> 16) & 0x3fff;
853862b4a29SBALATON Zoltan break;
854862b4a29SBALATON Zoltan case DST_HEIGHT_WIDTH:
855862b4a29SBALATON Zoltan s->regs.dst_width = data & 0x3fff;
856862b4a29SBALATON Zoltan s->regs.dst_height = (data >> 16) & 0x3fff;
857862b4a29SBALATON Zoltan ati_2d_blt(s);
858862b4a29SBALATON Zoltan break;
859862b4a29SBALATON Zoltan case DP_GUI_MASTER_CNTL:
860862b4a29SBALATON Zoltan s->regs.dp_gui_master_cntl = data & 0xf800000f;
861862b4a29SBALATON Zoltan s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
862862b4a29SBALATON Zoltan (data & 0x4000) << 16;
863862b4a29SBALATON Zoltan s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
864862b4a29SBALATON Zoltan break;
865862b4a29SBALATON Zoltan case DST_WIDTH_X:
866862b4a29SBALATON Zoltan s->regs.dst_x = data & 0x3fff;
867862b4a29SBALATON Zoltan s->regs.dst_width = (data >> 16) & 0x3fff;
868862b4a29SBALATON Zoltan ati_2d_blt(s);
869862b4a29SBALATON Zoltan break;
870862b4a29SBALATON Zoltan case SRC_X_Y:
871862b4a29SBALATON Zoltan s->regs.src_y = data & 0x3fff;
872862b4a29SBALATON Zoltan s->regs.src_x = (data >> 16) & 0x3fff;
873862b4a29SBALATON Zoltan break;
874862b4a29SBALATON Zoltan case DST_X_Y:
875862b4a29SBALATON Zoltan s->regs.dst_y = data & 0x3fff;
876862b4a29SBALATON Zoltan s->regs.dst_x = (data >> 16) & 0x3fff;
877862b4a29SBALATON Zoltan break;
878862b4a29SBALATON Zoltan case DST_WIDTH_HEIGHT:
879862b4a29SBALATON Zoltan s->regs.dst_height = data & 0x3fff;
880862b4a29SBALATON Zoltan s->regs.dst_width = (data >> 16) & 0x3fff;
881862b4a29SBALATON Zoltan ati_2d_blt(s);
882862b4a29SBALATON Zoltan break;
883862b4a29SBALATON Zoltan case DST_HEIGHT_Y:
884862b4a29SBALATON Zoltan s->regs.dst_y = data & 0x3fff;
885862b4a29SBALATON Zoltan s->regs.dst_height = (data >> 16) & 0x3fff;
886862b4a29SBALATON Zoltan break;
887862b4a29SBALATON Zoltan case SRC_OFFSET:
888862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
889862b4a29SBALATON Zoltan s->regs.src_offset = data & 0xfffffff0;
890862b4a29SBALATON Zoltan } else {
891862b4a29SBALATON Zoltan s->regs.src_offset = data & 0xfffffc00;
892862b4a29SBALATON Zoltan }
893862b4a29SBALATON Zoltan break;
894862b4a29SBALATON Zoltan case SRC_PITCH:
895862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
896862b4a29SBALATON Zoltan s->regs.src_pitch = data & 0x3fff;
897862b4a29SBALATON Zoltan s->regs.src_tile = (data >> 16) & 1;
898862b4a29SBALATON Zoltan } else {
899862b4a29SBALATON Zoltan s->regs.src_pitch = data & 0x3ff0;
900862b4a29SBALATON Zoltan }
901862b4a29SBALATON Zoltan break;
902862b4a29SBALATON Zoltan case DP_BRUSH_BKGD_CLR:
903862b4a29SBALATON Zoltan s->regs.dp_brush_bkgd_clr = data;
904862b4a29SBALATON Zoltan break;
905862b4a29SBALATON Zoltan case DP_BRUSH_FRGD_CLR:
906862b4a29SBALATON Zoltan s->regs.dp_brush_frgd_clr = data;
907862b4a29SBALATON Zoltan break;
908862b4a29SBALATON Zoltan case DP_CNTL:
909862b4a29SBALATON Zoltan s->regs.dp_cntl = data;
910862b4a29SBALATON Zoltan break;
911862b4a29SBALATON Zoltan case DP_DATATYPE:
912862b4a29SBALATON Zoltan s->regs.dp_datatype = data & 0xe0070f0f;
913862b4a29SBALATON Zoltan break;
914862b4a29SBALATON Zoltan case DP_MIX:
915862b4a29SBALATON Zoltan s->regs.dp_mix = data & 0x00ff0700;
916862b4a29SBALATON Zoltan break;
917862b4a29SBALATON Zoltan case DP_WRITE_MASK:
918862b4a29SBALATON Zoltan s->regs.dp_write_mask = data;
919862b4a29SBALATON Zoltan break;
920862b4a29SBALATON Zoltan case DEFAULT_OFFSET:
921866ad5f5SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
922866ad5f5SBALATON Zoltan s->regs.default_offset = data & 0xfffffff0;
923866ad5f5SBALATON Zoltan } else {
924866ad5f5SBALATON Zoltan /* Radeon has DEFAULT_PITCH_OFFSET here like DST_PITCH_OFFSET */
925866ad5f5SBALATON Zoltan s->regs.default_offset = (data & 0x3fffff) << 10;
926866ad5f5SBALATON Zoltan s->regs.default_pitch = (data & 0x3fc00000) >> 16;
927866ad5f5SBALATON Zoltan s->regs.default_tile = data >> 30;
928866ad5f5SBALATON Zoltan }
929862b4a29SBALATON Zoltan break;
930862b4a29SBALATON Zoltan case DEFAULT_PITCH:
931862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
932866ad5f5SBALATON Zoltan s->regs.default_pitch = data & 0x3fff;
933866ad5f5SBALATON Zoltan s->regs.default_tile = (data >> 16) & 1;
934862b4a29SBALATON Zoltan }
935862b4a29SBALATON Zoltan break;
936862b4a29SBALATON Zoltan case DEFAULT_SC_BOTTOM_RIGHT:
937862b4a29SBALATON Zoltan s->regs.default_sc_bottom_right = data & 0x3fff3fff;
938862b4a29SBALATON Zoltan break;
939862b4a29SBALATON Zoltan default:
940862b4a29SBALATON Zoltan break;
941862b4a29SBALATON Zoltan }
942862b4a29SBALATON Zoltan }
943862b4a29SBALATON Zoltan
944862b4a29SBALATON Zoltan static const MemoryRegionOps ati_mm_ops = {
945862b4a29SBALATON Zoltan .read = ati_mm_read,
946862b4a29SBALATON Zoltan .write = ati_mm_write,
947862b4a29SBALATON Zoltan .endianness = DEVICE_LITTLE_ENDIAN,
948862b4a29SBALATON Zoltan };
949862b4a29SBALATON Zoltan
ati_vga_realize(PCIDevice * dev,Error ** errp)950862b4a29SBALATON Zoltan static void ati_vga_realize(PCIDevice *dev, Error **errp)
951862b4a29SBALATON Zoltan {
952862b4a29SBALATON Zoltan ATIVGAState *s = ATI_VGA(dev);
953862b4a29SBALATON Zoltan VGACommonState *vga = &s->vga;
954862b4a29SBALATON Zoltan
955699f15fdSMarc-André Lureau #ifndef CONFIG_PIXMAN
956699f15fdSMarc-André Lureau if (s->use_pixman != 0) {
957699f15fdSMarc-André Lureau warn_report("x-pixman != 0, not effective without PIXMAN");
958699f15fdSMarc-André Lureau }
959699f15fdSMarc-André Lureau #endif
960699f15fdSMarc-André Lureau
961862b4a29SBALATON Zoltan if (s->model) {
962862b4a29SBALATON Zoltan int i;
963862b4a29SBALATON Zoltan for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
964862b4a29SBALATON Zoltan if (!strcmp(s->model, ati_model_aliases[i].name)) {
965862b4a29SBALATON Zoltan s->dev_id = ati_model_aliases[i].dev_id;
966862b4a29SBALATON Zoltan break;
967862b4a29SBALATON Zoltan }
968862b4a29SBALATON Zoltan }
969862b4a29SBALATON Zoltan if (i >= ARRAY_SIZE(ati_model_aliases)) {
970862b4a29SBALATON Zoltan warn_report("Unknown ATI VGA model name, "
971862b4a29SBALATON Zoltan "using default rage128p");
972862b4a29SBALATON Zoltan }
973862b4a29SBALATON Zoltan }
974862b4a29SBALATON Zoltan if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
975862b4a29SBALATON Zoltan s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
976862b4a29SBALATON Zoltan error_setg(errp, "Unknown ATI VGA device id, "
977862b4a29SBALATON Zoltan "only 0x5046 and 0x5159 are supported");
978862b4a29SBALATON Zoltan return;
979862b4a29SBALATON Zoltan }
980862b4a29SBALATON Zoltan pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
981862b4a29SBALATON Zoltan
982862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
983862b4a29SBALATON Zoltan s->vga.vram_size_mb < 16) {
984862b4a29SBALATON Zoltan warn_report("Too small video memory for device id");
985862b4a29SBALATON Zoltan s->vga.vram_size_mb = 16;
986862b4a29SBALATON Zoltan }
987862b4a29SBALATON Zoltan
988862b4a29SBALATON Zoltan /* init vga bits */
9896832deb8SThomas Huth if (!vga_common_init(vga, OBJECT(s), errp)) {
9906832deb8SThomas Huth return;
9916832deb8SThomas Huth }
992862b4a29SBALATON Zoltan vga_init(vga, OBJECT(s), pci_address_space(dev),
993862b4a29SBALATON Zoltan pci_address_space_io(dev), true);
994ee1004bbSPhilippe Mathieu-Daudé vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, vga);
995862b4a29SBALATON Zoltan if (s->cursor_guest_mode) {
996862b4a29SBALATON Zoltan vga->cursor_invalidate = ati_cursor_invalidate;
997862b4a29SBALATON Zoltan vga->cursor_draw_line = ati_cursor_draw_line;
998862b4a29SBALATON Zoltan }
999862b4a29SBALATON Zoltan
1000c82c7336SBALATON Zoltan /* ddc, edid */
1001c82c7336SBALATON Zoltan I2CBus *i2cbus = i2c_init_bus(DEVICE(s), "ati-vga.ddc");
100241742927SPeter Maydell bitbang_i2c_init(&s->bbi2c, i2cbus);
1003df707969SMarkus Armbruster I2CSlave *i2cddc = I2C_SLAVE(qdev_new(TYPE_I2CDDC));
1004c8665a59SPhilippe Mathieu-Daudé i2c_slave_set_address(i2cddc, 0x50);
1005df707969SMarkus Armbruster qdev_realize_and_unref(DEVICE(i2cddc), BUS(i2cbus), &error_abort);
1006c82c7336SBALATON Zoltan
1007862b4a29SBALATON Zoltan /* mmio register space */
1008862b4a29SBALATON Zoltan memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
1009862b4a29SBALATON Zoltan "ati.mmregs", 0x4000);
1010862b4a29SBALATON Zoltan /* io space is alias to beginning of mmregs */
1011862b4a29SBALATON Zoltan memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
1012862b4a29SBALATON Zoltan
1013862b4a29SBALATON Zoltan pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
1014862b4a29SBALATON Zoltan pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
1015862b4a29SBALATON Zoltan pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
1016b7105d28SBALATON Zoltan
1017b7105d28SBALATON Zoltan /* most interrupts are not yet emulated but MacOS needs at least VBlank */
1018b7105d28SBALATON Zoltan dev->config[PCI_INTERRUPT_PIN] = 1;
1019b7105d28SBALATON Zoltan timer_init_ns(&s->vblank_timer, QEMU_CLOCK_VIRTUAL, ati_vga_vblank_irq, s);
1020862b4a29SBALATON Zoltan }
1021862b4a29SBALATON Zoltan
ati_vga_reset(DeviceState * dev)1022862b4a29SBALATON Zoltan static void ati_vga_reset(DeviceState *dev)
1023862b4a29SBALATON Zoltan {
1024862b4a29SBALATON Zoltan ATIVGAState *s = ATI_VGA(dev);
1025862b4a29SBALATON Zoltan
1026b7105d28SBALATON Zoltan timer_del(&s->vblank_timer);
1027b7105d28SBALATON Zoltan ati_vga_update_irq(s);
1028b7105d28SBALATON Zoltan
1029862b4a29SBALATON Zoltan /* reset vga */
1030862b4a29SBALATON Zoltan vga_common_reset(&s->vga);
1031862b4a29SBALATON Zoltan s->mode = VGA_MODE;
1032862b4a29SBALATON Zoltan }
1033862b4a29SBALATON Zoltan
ati_vga_exit(PCIDevice * dev)1034862b4a29SBALATON Zoltan static void ati_vga_exit(PCIDevice *dev)
1035862b4a29SBALATON Zoltan {
1036862b4a29SBALATON Zoltan ATIVGAState *s = ATI_VGA(dev);
1037862b4a29SBALATON Zoltan
1038b7105d28SBALATON Zoltan timer_del(&s->vblank_timer);
1039862b4a29SBALATON Zoltan graphic_console_close(s->vga.con);
1040862b4a29SBALATON Zoltan }
1041862b4a29SBALATON Zoltan
1042862b4a29SBALATON Zoltan static Property ati_vga_properties[] = {
1043862b4a29SBALATON Zoltan DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
1044862b4a29SBALATON Zoltan DEFINE_PROP_STRING("model", ATIVGAState, model),
1045862b4a29SBALATON Zoltan DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
1046862b4a29SBALATON Zoltan PCI_DEVICE_ID_ATI_RAGE128_PF),
1047862b4a29SBALATON Zoltan DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
1048699f15fdSMarc-André Lureau /* this is a debug option, prefer PROP_UINT over PROP_BIT for simplicity */
1049699f15fdSMarc-André Lureau DEFINE_PROP_UINT8("x-pixman", ATIVGAState, use_pixman, DEFAULT_X_PIXMAN),
1050862b4a29SBALATON Zoltan DEFINE_PROP_END_OF_LIST()
1051862b4a29SBALATON Zoltan };
1052862b4a29SBALATON Zoltan
ati_vga_class_init(ObjectClass * klass,void * data)1053862b4a29SBALATON Zoltan static void ati_vga_class_init(ObjectClass *klass, void *data)
1054862b4a29SBALATON Zoltan {
1055862b4a29SBALATON Zoltan DeviceClass *dc = DEVICE_CLASS(klass);
1056862b4a29SBALATON Zoltan PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1057862b4a29SBALATON Zoltan
1058*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, ati_vga_reset);
10594f67d30bSMarc-André Lureau device_class_set_props(dc, ati_vga_properties);
1060862b4a29SBALATON Zoltan dc->hotpluggable = false;
1061862b4a29SBALATON Zoltan set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1062862b4a29SBALATON Zoltan
1063862b4a29SBALATON Zoltan k->class_id = PCI_CLASS_DISPLAY_VGA;
1064862b4a29SBALATON Zoltan k->vendor_id = PCI_VENDOR_ID_ATI;
1065862b4a29SBALATON Zoltan k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
1066263807f4SGerd Hoffmann k->romfile = "vgabios-ati.bin";
1067862b4a29SBALATON Zoltan k->realize = ati_vga_realize;
1068862b4a29SBALATON Zoltan k->exit = ati_vga_exit;
1069862b4a29SBALATON Zoltan }
1070862b4a29SBALATON Zoltan
ati_vga_init(Object * o)107108730ee0SBALATON Zoltan static void ati_vga_init(Object *o)
107208730ee0SBALATON Zoltan {
107308730ee0SBALATON Zoltan object_property_set_description(o, "x-pixman", "Use pixman for: "
107408730ee0SBALATON Zoltan "1: fill, 2: blit");
107508730ee0SBALATON Zoltan }
107608730ee0SBALATON Zoltan
1077862b4a29SBALATON Zoltan static const TypeInfo ati_vga_info = {
1078862b4a29SBALATON Zoltan .name = TYPE_ATI_VGA,
1079862b4a29SBALATON Zoltan .parent = TYPE_PCI_DEVICE,
1080862b4a29SBALATON Zoltan .instance_size = sizeof(ATIVGAState),
1081862b4a29SBALATON Zoltan .class_init = ati_vga_class_init,
108208730ee0SBALATON Zoltan .instance_init = ati_vga_init,
1083862b4a29SBALATON Zoltan .interfaces = (InterfaceInfo[]) {
1084862b4a29SBALATON Zoltan { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1085862b4a29SBALATON Zoltan { },
1086862b4a29SBALATON Zoltan },
1087862b4a29SBALATON Zoltan };
1088862b4a29SBALATON Zoltan
ati_vga_register_types(void)1089862b4a29SBALATON Zoltan static void ati_vga_register_types(void)
1090862b4a29SBALATON Zoltan {
1091862b4a29SBALATON Zoltan type_register_static(&ati_vga_info);
1092862b4a29SBALATON Zoltan }
1093862b4a29SBALATON Zoltan
1094862b4a29SBALATON Zoltan type_init(ati_vga_register_types)
1095