xref: /qemu/hw/display/ati.c (revision bbfff19688d2e1d10ea1becdfe82f7b8c068ede9)
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 
19*bbfff196SMarkus Armbruster #include "qemu/osdep.h"
20862b4a29SBALATON Zoltan #include "ati_int.h"
21862b4a29SBALATON Zoltan #include "ati_regs.h"
22862b4a29SBALATON Zoltan #include "vga_regs.h"
23862b4a29SBALATON Zoltan #include "qemu/log.h"
24862b4a29SBALATON Zoltan #include "qemu/error-report.h"
25862b4a29SBALATON Zoltan #include "qapi/error.h"
26862b4a29SBALATON Zoltan #include "hw/hw.h"
27862b4a29SBALATON Zoltan #include "ui/console.h"
28862b4a29SBALATON Zoltan #include "trace.h"
29862b4a29SBALATON Zoltan 
30862b4a29SBALATON Zoltan #define ATI_DEBUG_HW_CURSOR 0
31862b4a29SBALATON Zoltan 
32862b4a29SBALATON Zoltan static const struct {
33862b4a29SBALATON Zoltan     const char *name;
34862b4a29SBALATON Zoltan     uint16_t dev_id;
35862b4a29SBALATON Zoltan } ati_model_aliases[] = {
36862b4a29SBALATON Zoltan     { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
37862b4a29SBALATON Zoltan     { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
38862b4a29SBALATON Zoltan };
39862b4a29SBALATON Zoltan 
40862b4a29SBALATON Zoltan enum { VGA_MODE, EXT_MODE };
41862b4a29SBALATON Zoltan 
42862b4a29SBALATON Zoltan static void ati_vga_switch_mode(ATIVGAState *s)
43862b4a29SBALATON Zoltan {
44862b4a29SBALATON Zoltan     DPRINTF("%d -> %d\n",
45862b4a29SBALATON Zoltan             s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
46862b4a29SBALATON Zoltan     if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
47862b4a29SBALATON Zoltan         /* Extended mode enabled */
48862b4a29SBALATON Zoltan         s->mode = EXT_MODE;
49862b4a29SBALATON Zoltan         if (s->regs.crtc_gen_cntl & CRTC2_EN) {
50862b4a29SBALATON Zoltan             /* CRT controller enabled, use CRTC values */
51862b4a29SBALATON Zoltan             uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
52862b4a29SBALATON Zoltan             int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
53862b4a29SBALATON Zoltan             int bpp = 0;
54862b4a29SBALATON Zoltan             int h, v;
55862b4a29SBALATON Zoltan 
56862b4a29SBALATON Zoltan             if (s->regs.crtc_h_total_disp == 0) {
57862b4a29SBALATON Zoltan                 s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
58862b4a29SBALATON Zoltan             }
59862b4a29SBALATON Zoltan             if (s->regs.crtc_v_total_disp == 0) {
60862b4a29SBALATON Zoltan                 s->regs.crtc_v_total_disp = (480 - 1) << 16;
61862b4a29SBALATON Zoltan             }
62862b4a29SBALATON Zoltan             h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
63862b4a29SBALATON Zoltan             v = (s->regs.crtc_v_total_disp >> 16) + 1;
64862b4a29SBALATON Zoltan             switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
65862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_4BPP:
66862b4a29SBALATON Zoltan                 bpp = 4;
67862b4a29SBALATON Zoltan                 break;
68862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_8BPP:
69862b4a29SBALATON Zoltan                 bpp = 8;
70862b4a29SBALATON Zoltan                 break;
71862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_15BPP:
72862b4a29SBALATON Zoltan                 bpp = 15;
73862b4a29SBALATON Zoltan                 break;
74862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_16BPP:
75862b4a29SBALATON Zoltan                 bpp = 16;
76862b4a29SBALATON Zoltan                 break;
77862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_24BPP:
78862b4a29SBALATON Zoltan                 bpp = 24;
79862b4a29SBALATON Zoltan                 break;
80862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_32BPP:
81862b4a29SBALATON Zoltan                 bpp = 32;
82862b4a29SBALATON Zoltan                 break;
83862b4a29SBALATON Zoltan             default:
84862b4a29SBALATON Zoltan                 qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
85862b4a29SBALATON Zoltan             }
86862b4a29SBALATON Zoltan             assert(bpp != 0);
87862b4a29SBALATON Zoltan             DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
88862b4a29SBALATON Zoltan             vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
89862b4a29SBALATON Zoltan             vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
90862b4a29SBALATON Zoltan             /* reset VBE regs then set up mode */
91862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
92862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
93862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
94862b4a29SBALATON Zoltan             /* enable mode via ioport so it updates vga regs */
95862b4a29SBALATON Zoltan             vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
96862b4a29SBALATON Zoltan             vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
97862b4a29SBALATON Zoltan                 VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
98862b4a29SBALATON Zoltan                 (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
99862b4a29SBALATON Zoltan             /* now set offset and stride after enable as that resets these */
100862b4a29SBALATON Zoltan             if (stride) {
101862b4a29SBALATON Zoltan                 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
102862b4a29SBALATON Zoltan                 vbe_ioport_write_data(&s->vga, 0, stride);
103862b4a29SBALATON Zoltan                 if (offs % stride == 0) {
104862b4a29SBALATON Zoltan                     vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
105862b4a29SBALATON Zoltan                     vbe_ioport_write_data(&s->vga, 0, offs / stride);
106862b4a29SBALATON Zoltan                 } else {
107862b4a29SBALATON Zoltan                     /* FIXME what to do with this? */
108862b4a29SBALATON Zoltan                     error_report("VGA offset is not multiple of pitch, "
109862b4a29SBALATON Zoltan                                  "expect bad picture");
110862b4a29SBALATON Zoltan                 }
111862b4a29SBALATON Zoltan             }
112862b4a29SBALATON Zoltan         }
113862b4a29SBALATON Zoltan     } else {
114862b4a29SBALATON Zoltan         /* VGA mode enabled */
115862b4a29SBALATON Zoltan         s->mode = VGA_MODE;
116862b4a29SBALATON Zoltan         vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
117862b4a29SBALATON Zoltan         vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
118862b4a29SBALATON Zoltan     }
119862b4a29SBALATON Zoltan }
120862b4a29SBALATON Zoltan 
121862b4a29SBALATON Zoltan /* Used by host side hardware cursor */
122862b4a29SBALATON Zoltan static void ati_cursor_define(ATIVGAState *s)
123862b4a29SBALATON Zoltan {
124862b4a29SBALATON Zoltan     uint8_t data[1024];
125862b4a29SBALATON Zoltan     uint8_t *src;
126862b4a29SBALATON Zoltan     int i, j, idx = 0;
127862b4a29SBALATON Zoltan 
128862b4a29SBALATON Zoltan     if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
129862b4a29SBALATON Zoltan         return; /* Do not update cursor if locked or rendered by guest */
130862b4a29SBALATON Zoltan     }
131862b4a29SBALATON Zoltan     /* FIXME handle cur_hv_offs correctly */
132862b4a29SBALATON Zoltan     src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
133862b4a29SBALATON Zoltan           s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
134862b4a29SBALATON Zoltan           (s->regs.cur_hv_offs & 0xffff) * 16;
135862b4a29SBALATON Zoltan     for (i = 0; i < 64; i++) {
136862b4a29SBALATON Zoltan         for (j = 0; j < 8; j++, idx++) {
137862b4a29SBALATON Zoltan             data[idx] = src[i * 16 + j];
138862b4a29SBALATON Zoltan             data[512 + idx] = src[i * 16 + j + 8];
139862b4a29SBALATON Zoltan         }
140862b4a29SBALATON Zoltan     }
141862b4a29SBALATON Zoltan     if (!s->cursor) {
142862b4a29SBALATON Zoltan         s->cursor = cursor_alloc(64, 64);
143862b4a29SBALATON Zoltan     }
144862b4a29SBALATON Zoltan     cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
145862b4a29SBALATON Zoltan                     &data[512], 1, &data[0]);
146862b4a29SBALATON Zoltan     dpy_cursor_define(s->vga.con, s->cursor);
147862b4a29SBALATON Zoltan }
148862b4a29SBALATON Zoltan 
149862b4a29SBALATON Zoltan /* Alternatively support guest rendered hardware cursor */
150862b4a29SBALATON Zoltan static void ati_cursor_invalidate(VGACommonState *vga)
151862b4a29SBALATON Zoltan {
152862b4a29SBALATON Zoltan     ATIVGAState *s = container_of(vga, ATIVGAState, vga);
153862b4a29SBALATON Zoltan     int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
154862b4a29SBALATON Zoltan 
155862b4a29SBALATON Zoltan     if (s->regs.cur_offset & BIT(31)) {
156862b4a29SBALATON Zoltan         return; /* Do not update cursor if locked */
157862b4a29SBALATON Zoltan     }
158862b4a29SBALATON Zoltan     if (s->cursor_size != size ||
159862b4a29SBALATON Zoltan         vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
160862b4a29SBALATON Zoltan         vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
161862b4a29SBALATON Zoltan         s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
162862b4a29SBALATON Zoltan         (s->regs.cur_hv_offs & 0xffff) * 16) {
163862b4a29SBALATON Zoltan         /* Remove old cursor then update and show new one if needed */
164862b4a29SBALATON Zoltan         vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
165862b4a29SBALATON Zoltan         vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
166862b4a29SBALATON Zoltan         vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
167862b4a29SBALATON Zoltan         s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
168862b4a29SBALATON Zoltan                            (s->regs.cur_hv_offs & 0xffff) * 16;
169862b4a29SBALATON Zoltan         s->cursor_size = size;
170862b4a29SBALATON Zoltan         if (size) {
171862b4a29SBALATON Zoltan             vga_invalidate_scanlines(vga,
172862b4a29SBALATON Zoltan                                      vga->hw_cursor_y, vga->hw_cursor_y + 63);
173862b4a29SBALATON Zoltan         }
174862b4a29SBALATON Zoltan     }
175862b4a29SBALATON Zoltan }
176862b4a29SBALATON Zoltan 
177862b4a29SBALATON Zoltan static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
178862b4a29SBALATON Zoltan {
179862b4a29SBALATON Zoltan     ATIVGAState *s = container_of(vga, ATIVGAState, vga);
180862b4a29SBALATON Zoltan     uint8_t *src;
181862b4a29SBALATON Zoltan     uint32_t *dp = (uint32_t *)d;
182862b4a29SBALATON Zoltan     int i, j, h;
183862b4a29SBALATON Zoltan 
184862b4a29SBALATON Zoltan     if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
185862b4a29SBALATON Zoltan         scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
186862b4a29SBALATON Zoltan         scr_y > s->regs.crtc_v_total_disp >> 16) {
187862b4a29SBALATON Zoltan         return;
188862b4a29SBALATON Zoltan     }
189862b4a29SBALATON Zoltan     /* FIXME handle cur_hv_offs correctly */
190862b4a29SBALATON Zoltan     src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
191862b4a29SBALATON Zoltan           s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
192862b4a29SBALATON Zoltan     dp = &dp[vga->hw_cursor_x];
193862b4a29SBALATON Zoltan     h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
194862b4a29SBALATON Zoltan     for (i = 0; i < 8; i++) {
195862b4a29SBALATON Zoltan         uint32_t color;
196862b4a29SBALATON Zoltan         uint8_t abits = src[i];
197862b4a29SBALATON Zoltan         uint8_t xbits = src[i + 8];
198862b4a29SBALATON Zoltan         for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
199862b4a29SBALATON Zoltan             if (abits & BIT(7)) {
200862b4a29SBALATON Zoltan                 if (xbits & BIT(7)) {
201862b4a29SBALATON Zoltan                     color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
202862b4a29SBALATON Zoltan                 } else {
203862b4a29SBALATON Zoltan                     continue; /* transparent, no change */
204862b4a29SBALATON Zoltan                 }
205862b4a29SBALATON Zoltan             } else {
206862b4a29SBALATON Zoltan                 color = (xbits & BIT(7) ? s->regs.cur_color1 :
207862b4a29SBALATON Zoltan                                           s->regs.cur_color0) << 8 | 0xff;
208862b4a29SBALATON Zoltan             }
209862b4a29SBALATON Zoltan             if (vga->hw_cursor_x + i * 8 + j >= h) {
210862b4a29SBALATON Zoltan                 return; /* end of screen, don't span to next line */
211862b4a29SBALATON Zoltan             }
212862b4a29SBALATON Zoltan             dp[i * 8 + j] = color;
213862b4a29SBALATON Zoltan         }
214862b4a29SBALATON Zoltan     }
215862b4a29SBALATON Zoltan }
216862b4a29SBALATON Zoltan 
217862b4a29SBALATON Zoltan static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
218862b4a29SBALATON Zoltan                                          unsigned int size)
219862b4a29SBALATON Zoltan {
220862b4a29SBALATON Zoltan     if (offs == 0 && size == 4) {
221862b4a29SBALATON Zoltan         return reg;
222862b4a29SBALATON Zoltan     } else {
223862b4a29SBALATON Zoltan         return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
224862b4a29SBALATON Zoltan     }
225862b4a29SBALATON Zoltan }
226862b4a29SBALATON Zoltan 
227862b4a29SBALATON Zoltan static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
228862b4a29SBALATON Zoltan {
229862b4a29SBALATON Zoltan     ATIVGAState *s = opaque;
230862b4a29SBALATON Zoltan     uint64_t val = 0;
231862b4a29SBALATON Zoltan 
232862b4a29SBALATON Zoltan     switch (addr) {
233862b4a29SBALATON Zoltan     case MM_INDEX:
234862b4a29SBALATON Zoltan         val = s->regs.mm_index;
235862b4a29SBALATON Zoltan         break;
236862b4a29SBALATON Zoltan     case MM_DATA ... MM_DATA + 3:
237862b4a29SBALATON Zoltan         /* indexed access to regs or memory */
238862b4a29SBALATON Zoltan         if (s->regs.mm_index & BIT(31)) {
239339534d4SBALATON Zoltan             uint32_t idx = s->regs.mm_index & ~BIT(31);
240339534d4SBALATON Zoltan             if (idx <= s->vga.vram_size - size) {
241339534d4SBALATON Zoltan                 val = ldn_le_p(s->vga.vram_ptr + idx, size);
242862b4a29SBALATON Zoltan             }
243862b4a29SBALATON Zoltan         } else {
244862b4a29SBALATON Zoltan             val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
245862b4a29SBALATON Zoltan         }
246862b4a29SBALATON Zoltan         break;
247862b4a29SBALATON Zoltan     case BIOS_0_SCRATCH ... BUS_CNTL - 1:
248862b4a29SBALATON Zoltan     {
249862b4a29SBALATON Zoltan         int i = (addr - BIOS_0_SCRATCH) / 4;
250862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
251862b4a29SBALATON Zoltan             break;
252862b4a29SBALATON Zoltan         }
253862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.bios_scratch[i],
254862b4a29SBALATON Zoltan                                 addr - (BIOS_0_SCRATCH + i * 4), size);
255862b4a29SBALATON Zoltan         break;
256862b4a29SBALATON Zoltan     }
257862b4a29SBALATON Zoltan     case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
258862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
259862b4a29SBALATON Zoltan                                 addr - CRTC_GEN_CNTL, size);
260862b4a29SBALATON Zoltan         break;
261862b4a29SBALATON Zoltan     case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
262862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
263862b4a29SBALATON Zoltan                                 addr - CRTC_EXT_CNTL, size);
264862b4a29SBALATON Zoltan         break;
265862b4a29SBALATON Zoltan     case DAC_CNTL:
266862b4a29SBALATON Zoltan         val = s->regs.dac_cntl;
267862b4a29SBALATON Zoltan         break;
268862b4a29SBALATON Zoltan /*    case GPIO_MONID: FIXME hook up DDC I2C here */
269862b4a29SBALATON Zoltan     case PALETTE_INDEX:
270862b4a29SBALATON Zoltan         /* FIXME unaligned access */
271862b4a29SBALATON Zoltan         val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
272862b4a29SBALATON Zoltan         val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
273862b4a29SBALATON Zoltan         break;
274862b4a29SBALATON Zoltan     case PALETTE_DATA:
275862b4a29SBALATON Zoltan         val = vga_ioport_read(&s->vga, VGA_PEL_D);
276862b4a29SBALATON Zoltan         break;
277862b4a29SBALATON Zoltan     case CNFG_MEMSIZE:
278862b4a29SBALATON Zoltan         val = s->vga.vram_size;
279862b4a29SBALATON Zoltan         break;
280862b4a29SBALATON Zoltan     case MC_STATUS:
281862b4a29SBALATON Zoltan         val = 5;
282862b4a29SBALATON Zoltan         break;
283862b4a29SBALATON Zoltan     case RBBM_STATUS:
284862b4a29SBALATON Zoltan     case GUI_STAT:
285862b4a29SBALATON Zoltan         val = 64; /* free CMDFIFO entries */
286862b4a29SBALATON Zoltan         break;
287862b4a29SBALATON Zoltan     case CRTC_H_TOTAL_DISP:
288862b4a29SBALATON Zoltan         val = s->regs.crtc_h_total_disp;
289862b4a29SBALATON Zoltan         break;
290862b4a29SBALATON Zoltan     case CRTC_H_SYNC_STRT_WID:
291862b4a29SBALATON Zoltan         val = s->regs.crtc_h_sync_strt_wid;
292862b4a29SBALATON Zoltan         break;
293862b4a29SBALATON Zoltan     case CRTC_V_TOTAL_DISP:
294862b4a29SBALATON Zoltan         val = s->regs.crtc_v_total_disp;
295862b4a29SBALATON Zoltan         break;
296862b4a29SBALATON Zoltan     case CRTC_V_SYNC_STRT_WID:
297862b4a29SBALATON Zoltan         val = s->regs.crtc_v_sync_strt_wid;
298862b4a29SBALATON Zoltan         break;
299862b4a29SBALATON Zoltan     case CRTC_OFFSET:
300862b4a29SBALATON Zoltan         val = s->regs.crtc_offset;
301862b4a29SBALATON Zoltan         break;
302862b4a29SBALATON Zoltan     case CRTC_OFFSET_CNTL:
303862b4a29SBALATON Zoltan         val = s->regs.crtc_offset_cntl;
304862b4a29SBALATON Zoltan         break;
305862b4a29SBALATON Zoltan     case CRTC_PITCH:
306862b4a29SBALATON Zoltan         val = s->regs.crtc_pitch;
307862b4a29SBALATON Zoltan         break;
308862b4a29SBALATON Zoltan     case 0xf00 ... 0xfff:
309862b4a29SBALATON Zoltan         val = pci_default_read_config(&s->dev, addr - 0xf00, size);
310862b4a29SBALATON Zoltan         break;
311862b4a29SBALATON Zoltan     case CUR_OFFSET:
312862b4a29SBALATON Zoltan         val = s->regs.cur_offset;
313862b4a29SBALATON Zoltan         break;
314862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_POSN:
315862b4a29SBALATON Zoltan         val = s->regs.cur_hv_pos;
316862b4a29SBALATON Zoltan         val |= s->regs.cur_offset & BIT(31);
317862b4a29SBALATON Zoltan         break;
318862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_OFF:
319862b4a29SBALATON Zoltan         val = s->regs.cur_hv_offs;
320862b4a29SBALATON Zoltan         val |= s->regs.cur_offset & BIT(31);
321862b4a29SBALATON Zoltan         break;
322862b4a29SBALATON Zoltan     case CUR_CLR0:
323862b4a29SBALATON Zoltan         val = s->regs.cur_color0;
324862b4a29SBALATON Zoltan         break;
325862b4a29SBALATON Zoltan     case CUR_CLR1:
326862b4a29SBALATON Zoltan         val = s->regs.cur_color1;
327862b4a29SBALATON Zoltan         break;
328862b4a29SBALATON Zoltan     case DST_OFFSET:
329862b4a29SBALATON Zoltan         val = s->regs.dst_offset;
330862b4a29SBALATON Zoltan         break;
331862b4a29SBALATON Zoltan     case DST_PITCH:
332862b4a29SBALATON Zoltan         val = s->regs.dst_pitch;
333862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
334862b4a29SBALATON Zoltan             val &= s->regs.dst_tile << 16;
335862b4a29SBALATON Zoltan         }
336862b4a29SBALATON Zoltan         break;
337862b4a29SBALATON Zoltan     case DST_WIDTH:
338862b4a29SBALATON Zoltan         val = s->regs.dst_width;
339862b4a29SBALATON Zoltan         break;
340862b4a29SBALATON Zoltan     case DST_HEIGHT:
341862b4a29SBALATON Zoltan         val = s->regs.dst_height;
342862b4a29SBALATON Zoltan         break;
343862b4a29SBALATON Zoltan     case SRC_X:
344862b4a29SBALATON Zoltan         val = s->regs.src_x;
345862b4a29SBALATON Zoltan         break;
346862b4a29SBALATON Zoltan     case SRC_Y:
347862b4a29SBALATON Zoltan         val = s->regs.src_y;
348862b4a29SBALATON Zoltan         break;
349862b4a29SBALATON Zoltan     case DST_X:
350862b4a29SBALATON Zoltan         val = s->regs.dst_x;
351862b4a29SBALATON Zoltan         break;
352862b4a29SBALATON Zoltan     case DST_Y:
353862b4a29SBALATON Zoltan         val = s->regs.dst_y;
354862b4a29SBALATON Zoltan         break;
355862b4a29SBALATON Zoltan     case DP_GUI_MASTER_CNTL:
356862b4a29SBALATON Zoltan         val = s->regs.dp_gui_master_cntl;
357862b4a29SBALATON Zoltan         break;
358862b4a29SBALATON Zoltan     case SRC_OFFSET:
359862b4a29SBALATON Zoltan         val = s->regs.src_offset;
360862b4a29SBALATON Zoltan         break;
361862b4a29SBALATON Zoltan     case SRC_PITCH:
362862b4a29SBALATON Zoltan         val = s->regs.src_pitch;
363862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
364862b4a29SBALATON Zoltan             val &= s->regs.src_tile << 16;
365862b4a29SBALATON Zoltan         }
366862b4a29SBALATON Zoltan         break;
367862b4a29SBALATON Zoltan     case DP_BRUSH_BKGD_CLR:
368862b4a29SBALATON Zoltan         val = s->regs.dp_brush_bkgd_clr;
369862b4a29SBALATON Zoltan         break;
370862b4a29SBALATON Zoltan     case DP_BRUSH_FRGD_CLR:
371862b4a29SBALATON Zoltan         val = s->regs.dp_brush_frgd_clr;
372862b4a29SBALATON Zoltan         break;
373862b4a29SBALATON Zoltan     case DP_SRC_FRGD_CLR:
374862b4a29SBALATON Zoltan         val = s->regs.dp_src_frgd_clr;
375862b4a29SBALATON Zoltan         break;
376862b4a29SBALATON Zoltan     case DP_SRC_BKGD_CLR:
377862b4a29SBALATON Zoltan         val = s->regs.dp_src_bkgd_clr;
378862b4a29SBALATON Zoltan         break;
379862b4a29SBALATON Zoltan     case DP_CNTL:
380862b4a29SBALATON Zoltan         val = s->regs.dp_cntl;
381862b4a29SBALATON Zoltan         break;
382862b4a29SBALATON Zoltan     case DP_DATATYPE:
383862b4a29SBALATON Zoltan         val = s->regs.dp_datatype;
384862b4a29SBALATON Zoltan         break;
385862b4a29SBALATON Zoltan     case DP_MIX:
386862b4a29SBALATON Zoltan         val = s->regs.dp_mix;
387862b4a29SBALATON Zoltan         break;
388862b4a29SBALATON Zoltan     case DP_WRITE_MASK:
389862b4a29SBALATON Zoltan         val = s->regs.dp_write_mask;
390862b4a29SBALATON Zoltan         break;
391862b4a29SBALATON Zoltan     case DEFAULT_OFFSET:
392862b4a29SBALATON Zoltan         val = s->regs.default_offset;
393862b4a29SBALATON Zoltan         break;
394862b4a29SBALATON Zoltan     case DEFAULT_PITCH:
395862b4a29SBALATON Zoltan         val = s->regs.default_pitch;
396862b4a29SBALATON Zoltan         break;
397862b4a29SBALATON Zoltan     case DEFAULT_SC_BOTTOM_RIGHT:
398862b4a29SBALATON Zoltan         val = s->regs.default_sc_bottom_right;
399862b4a29SBALATON Zoltan         break;
400862b4a29SBALATON Zoltan     default:
401862b4a29SBALATON Zoltan         break;
402862b4a29SBALATON Zoltan     }
403862b4a29SBALATON Zoltan     if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
404862b4a29SBALATON Zoltan         trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
405862b4a29SBALATON Zoltan     }
406862b4a29SBALATON Zoltan     return val;
407862b4a29SBALATON Zoltan }
408862b4a29SBALATON Zoltan 
409862b4a29SBALATON Zoltan static inline void ati_reg_write_offs(uint32_t *reg, int offs,
410862b4a29SBALATON Zoltan                                       uint64_t data, unsigned int size)
411862b4a29SBALATON Zoltan {
412862b4a29SBALATON Zoltan     if (offs == 0 && size == 4) {
413862b4a29SBALATON Zoltan         *reg = data;
414862b4a29SBALATON Zoltan     } else {
415862b4a29SBALATON Zoltan         *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
416862b4a29SBALATON Zoltan                          data);
417862b4a29SBALATON Zoltan     }
418862b4a29SBALATON Zoltan }
419862b4a29SBALATON Zoltan 
420862b4a29SBALATON Zoltan static void ati_mm_write(void *opaque, hwaddr addr,
421862b4a29SBALATON Zoltan                            uint64_t data, unsigned int size)
422862b4a29SBALATON Zoltan {
423862b4a29SBALATON Zoltan     ATIVGAState *s = opaque;
424862b4a29SBALATON Zoltan 
425862b4a29SBALATON Zoltan     if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
426862b4a29SBALATON Zoltan         trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
427862b4a29SBALATON Zoltan     }
428862b4a29SBALATON Zoltan     switch (addr) {
429862b4a29SBALATON Zoltan     case MM_INDEX:
430862b4a29SBALATON Zoltan         s->regs.mm_index = data;
431862b4a29SBALATON Zoltan         break;
432862b4a29SBALATON Zoltan     case MM_DATA ... MM_DATA + 3:
433862b4a29SBALATON Zoltan         /* indexed access to regs or memory */
434862b4a29SBALATON Zoltan         if (s->regs.mm_index & BIT(31)) {
435339534d4SBALATON Zoltan             uint32_t idx = s->regs.mm_index & ~BIT(31);
436339534d4SBALATON Zoltan             if (idx <= s->vga.vram_size - size) {
437339534d4SBALATON Zoltan                 stn_le_p(s->vga.vram_ptr + idx, size, data);
438862b4a29SBALATON Zoltan             }
439862b4a29SBALATON Zoltan         } else {
440862b4a29SBALATON Zoltan             ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
441862b4a29SBALATON Zoltan         }
442862b4a29SBALATON Zoltan         break;
443862b4a29SBALATON Zoltan     case BIOS_0_SCRATCH ... BUS_CNTL - 1:
444862b4a29SBALATON Zoltan     {
445862b4a29SBALATON Zoltan         int i = (addr - BIOS_0_SCRATCH) / 4;
446862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
447862b4a29SBALATON Zoltan             break;
448862b4a29SBALATON Zoltan         }
449862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.bios_scratch[i],
450862b4a29SBALATON Zoltan                            addr - (BIOS_0_SCRATCH + i * 4), data, size);
451862b4a29SBALATON Zoltan         break;
452862b4a29SBALATON Zoltan     }
453862b4a29SBALATON Zoltan     case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
454862b4a29SBALATON Zoltan     {
455862b4a29SBALATON Zoltan         uint32_t val = s->regs.crtc_gen_cntl;
456862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.crtc_gen_cntl,
457862b4a29SBALATON Zoltan                            addr - CRTC_GEN_CNTL, data, size);
458862b4a29SBALATON Zoltan         if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
459862b4a29SBALATON Zoltan             if (s->cursor_guest_mode) {
460862b4a29SBALATON Zoltan                 s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
461862b4a29SBALATON Zoltan             } else {
462862b4a29SBALATON Zoltan                 if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
463862b4a29SBALATON Zoltan                     ati_cursor_define(s);
464862b4a29SBALATON Zoltan                 }
465862b4a29SBALATON Zoltan                 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
466862b4a29SBALATON Zoltan                               s->regs.cur_hv_pos & 0xffff,
467862b4a29SBALATON Zoltan                               (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
468862b4a29SBALATON Zoltan             }
469862b4a29SBALATON Zoltan         }
470862b4a29SBALATON Zoltan         if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
471862b4a29SBALATON Zoltan             (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
472862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
473862b4a29SBALATON Zoltan         }
474862b4a29SBALATON Zoltan         break;
475862b4a29SBALATON Zoltan     }
476862b4a29SBALATON Zoltan     case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
477862b4a29SBALATON Zoltan     {
478862b4a29SBALATON Zoltan         uint32_t val = s->regs.crtc_ext_cntl;
479862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.crtc_ext_cntl,
480862b4a29SBALATON Zoltan                            addr - CRTC_EXT_CNTL, data, size);
481862b4a29SBALATON Zoltan         if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
482862b4a29SBALATON Zoltan             DPRINTF("Display disabled\n");
483862b4a29SBALATON Zoltan             s->vga.ar_index &= ~BIT(5);
484862b4a29SBALATON Zoltan         } else {
485862b4a29SBALATON Zoltan             DPRINTF("Display enabled\n");
486862b4a29SBALATON Zoltan             s->vga.ar_index |= BIT(5);
487862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
488862b4a29SBALATON Zoltan         }
489862b4a29SBALATON Zoltan         if ((val & CRT_CRTC_DISPLAY_DIS) !=
490862b4a29SBALATON Zoltan             (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
491862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
492862b4a29SBALATON Zoltan         }
493862b4a29SBALATON Zoltan         break;
494862b4a29SBALATON Zoltan     }
495862b4a29SBALATON Zoltan     case DAC_CNTL:
496862b4a29SBALATON Zoltan         s->regs.dac_cntl = data & 0xffffe3ff;
497862b4a29SBALATON Zoltan         s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
498862b4a29SBALATON Zoltan         break;
499862b4a29SBALATON Zoltan /*    case GPIO_MONID: FIXME hook up DDC I2C here */
500862b4a29SBALATON Zoltan     case PALETTE_INDEX ... PALETTE_INDEX + 3:
501862b4a29SBALATON Zoltan         if (size == 4) {
502862b4a29SBALATON Zoltan             vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
503862b4a29SBALATON Zoltan             vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
504862b4a29SBALATON Zoltan         } else {
505862b4a29SBALATON Zoltan             if (addr == PALETTE_INDEX) {
506862b4a29SBALATON Zoltan                 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
507862b4a29SBALATON Zoltan             } else {
508862b4a29SBALATON Zoltan                 vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
509862b4a29SBALATON Zoltan             }
510862b4a29SBALATON Zoltan         }
511862b4a29SBALATON Zoltan         break;
512862b4a29SBALATON Zoltan     case PALETTE_DATA ... PALETTE_DATA + 3:
513862b4a29SBALATON Zoltan         data <<= addr - PALETTE_DATA;
514862b4a29SBALATON Zoltan         data = bswap32(data) >> 8;
515862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
516862b4a29SBALATON Zoltan         data >>= 8;
517862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
518862b4a29SBALATON Zoltan         data >>= 8;
519862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
520862b4a29SBALATON Zoltan         break;
521862b4a29SBALATON Zoltan     case CRTC_H_TOTAL_DISP:
522862b4a29SBALATON Zoltan         s->regs.crtc_h_total_disp = data & 0x07ff07ff;
523862b4a29SBALATON Zoltan         break;
524862b4a29SBALATON Zoltan     case CRTC_H_SYNC_STRT_WID:
525862b4a29SBALATON Zoltan         s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
526862b4a29SBALATON Zoltan         break;
527862b4a29SBALATON Zoltan     case CRTC_V_TOTAL_DISP:
528862b4a29SBALATON Zoltan         s->regs.crtc_v_total_disp = data & 0x0fff0fff;
529862b4a29SBALATON Zoltan         break;
530862b4a29SBALATON Zoltan     case CRTC_V_SYNC_STRT_WID:
531862b4a29SBALATON Zoltan         s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
532862b4a29SBALATON Zoltan         break;
533862b4a29SBALATON Zoltan     case CRTC_OFFSET:
534862b4a29SBALATON Zoltan         s->regs.crtc_offset = data & 0xc7ffffff;
535862b4a29SBALATON Zoltan         break;
536862b4a29SBALATON Zoltan     case CRTC_OFFSET_CNTL:
537862b4a29SBALATON Zoltan         s->regs.crtc_offset_cntl = data; /* FIXME */
538862b4a29SBALATON Zoltan         break;
539862b4a29SBALATON Zoltan     case CRTC_PITCH:
540862b4a29SBALATON Zoltan         s->regs.crtc_pitch = data & 0x07ff07ff;
541862b4a29SBALATON Zoltan         break;
542862b4a29SBALATON Zoltan     case 0xf00 ... 0xfff:
543862b4a29SBALATON Zoltan         /* read-only copy of PCI config space so ignore writes */
544862b4a29SBALATON Zoltan         break;
545862b4a29SBALATON Zoltan     case CUR_OFFSET:
546862b4a29SBALATON Zoltan         if (s->regs.cur_offset != (data & 0x87fffff0)) {
547862b4a29SBALATON Zoltan             s->regs.cur_offset = data & 0x87fffff0;
548862b4a29SBALATON Zoltan             ati_cursor_define(s);
549862b4a29SBALATON Zoltan         }
550862b4a29SBALATON Zoltan         break;
551862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_POSN:
552862b4a29SBALATON Zoltan         s->regs.cur_hv_pos = data & 0x3fff0fff;
553862b4a29SBALATON Zoltan         if (data & BIT(31)) {
554862b4a29SBALATON Zoltan             s->regs.cur_offset |= data & BIT(31);
555862b4a29SBALATON Zoltan         } else if (s->regs.cur_offset & BIT(31)) {
556862b4a29SBALATON Zoltan             s->regs.cur_offset &= ~BIT(31);
557862b4a29SBALATON Zoltan             ati_cursor_define(s);
558862b4a29SBALATON Zoltan         }
559862b4a29SBALATON Zoltan         if (!s->cursor_guest_mode &&
560862b4a29SBALATON Zoltan             (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(data & BIT(31))) {
561862b4a29SBALATON Zoltan             dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
562862b4a29SBALATON Zoltan                           s->regs.cur_hv_pos & 0xffff, 1);
563862b4a29SBALATON Zoltan         }
564862b4a29SBALATON Zoltan         break;
565862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_OFF:
566862b4a29SBALATON Zoltan         s->regs.cur_hv_offs = data & 0x3f003f;
567862b4a29SBALATON Zoltan         if (data & BIT(31)) {
568862b4a29SBALATON Zoltan             s->regs.cur_offset |= data & BIT(31);
569862b4a29SBALATON Zoltan         } else if (s->regs.cur_offset & BIT(31)) {
570862b4a29SBALATON Zoltan             s->regs.cur_offset &= ~BIT(31);
571862b4a29SBALATON Zoltan             ati_cursor_define(s);
572862b4a29SBALATON Zoltan         }
573862b4a29SBALATON Zoltan         break;
574862b4a29SBALATON Zoltan     case CUR_CLR0:
575862b4a29SBALATON Zoltan         if (s->regs.cur_color0 != (data & 0xffffff)) {
576862b4a29SBALATON Zoltan             s->regs.cur_color0 = data & 0xffffff;
577862b4a29SBALATON Zoltan             ati_cursor_define(s);
578862b4a29SBALATON Zoltan         }
579862b4a29SBALATON Zoltan         break;
580862b4a29SBALATON Zoltan     case CUR_CLR1:
581862b4a29SBALATON Zoltan         /*
582862b4a29SBALATON Zoltan          * Update cursor unconditionally here because some clients set up
583862b4a29SBALATON Zoltan          * other registers before actually writing cursor data to memory at
584862b4a29SBALATON Zoltan          * offset so we would miss cursor change unless always updating here
585862b4a29SBALATON Zoltan          */
586862b4a29SBALATON Zoltan         s->regs.cur_color1 = data & 0xffffff;
587862b4a29SBALATON Zoltan         ati_cursor_define(s);
588862b4a29SBALATON Zoltan         break;
589862b4a29SBALATON Zoltan     case DST_OFFSET:
590862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
591862b4a29SBALATON Zoltan             s->regs.dst_offset = data & 0xfffffff0;
592862b4a29SBALATON Zoltan         } else {
593862b4a29SBALATON Zoltan             s->regs.dst_offset = data & 0xfffffc00;
594862b4a29SBALATON Zoltan         }
595862b4a29SBALATON Zoltan         break;
596862b4a29SBALATON Zoltan     case DST_PITCH:
597862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
598862b4a29SBALATON Zoltan             s->regs.dst_pitch = data & 0x3fff;
599862b4a29SBALATON Zoltan             s->regs.dst_tile = (data >> 16) & 1;
600862b4a29SBALATON Zoltan         } else {
601862b4a29SBALATON Zoltan             s->regs.dst_pitch = data & 0x3ff0;
602862b4a29SBALATON Zoltan         }
603862b4a29SBALATON Zoltan         break;
604862b4a29SBALATON Zoltan     case DST_TILE:
605862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
606862b4a29SBALATON Zoltan             s->regs.dst_tile = data & 3;
607862b4a29SBALATON Zoltan         }
608862b4a29SBALATON Zoltan         break;
609862b4a29SBALATON Zoltan     case DST_WIDTH:
610862b4a29SBALATON Zoltan         s->regs.dst_width = data & 0x3fff;
611862b4a29SBALATON Zoltan         ati_2d_blt(s);
612862b4a29SBALATON Zoltan         break;
613862b4a29SBALATON Zoltan     case DST_HEIGHT:
614862b4a29SBALATON Zoltan         s->regs.dst_height = data & 0x3fff;
615862b4a29SBALATON Zoltan         break;
616862b4a29SBALATON Zoltan     case SRC_X:
617862b4a29SBALATON Zoltan         s->regs.src_x = data & 0x3fff;
618862b4a29SBALATON Zoltan         break;
619862b4a29SBALATON Zoltan     case SRC_Y:
620862b4a29SBALATON Zoltan         s->regs.src_y = data & 0x3fff;
621862b4a29SBALATON Zoltan         break;
622862b4a29SBALATON Zoltan     case DST_X:
623862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
624862b4a29SBALATON Zoltan         break;
625862b4a29SBALATON Zoltan     case DST_Y:
626862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
627862b4a29SBALATON Zoltan         break;
628862b4a29SBALATON Zoltan     case SRC_PITCH_OFFSET:
629862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
630862b4a29SBALATON Zoltan             s->regs.src_offset = (data & 0x1fffff) << 5;
631862b4a29SBALATON Zoltan             s->regs.src_pitch = (data >> 21) & 0x3ff;
632862b4a29SBALATON Zoltan             s->regs.src_tile = data >> 31;
633862b4a29SBALATON Zoltan         } else {
634862b4a29SBALATON Zoltan             s->regs.src_offset = (data & 0x3fffff) << 11;
635862b4a29SBALATON Zoltan             s->regs.src_pitch = (data & 0x3fc00000) >> 16;
636862b4a29SBALATON Zoltan             s->regs.src_tile = (data >> 30) & 1;
637862b4a29SBALATON Zoltan         }
638862b4a29SBALATON Zoltan         break;
639862b4a29SBALATON Zoltan     case DST_PITCH_OFFSET:
640862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
641862b4a29SBALATON Zoltan             s->regs.dst_offset = (data & 0x1fffff) << 5;
642862b4a29SBALATON Zoltan             s->regs.dst_pitch = (data >> 21) & 0x3ff;
643862b4a29SBALATON Zoltan             s->regs.dst_tile = data >> 31;
644862b4a29SBALATON Zoltan         } else {
645862b4a29SBALATON Zoltan             s->regs.dst_offset = (data & 0x3fffff) << 11;
646862b4a29SBALATON Zoltan             s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
647862b4a29SBALATON Zoltan             s->regs.dst_tile = data >> 30;
648862b4a29SBALATON Zoltan         }
649862b4a29SBALATON Zoltan         break;
650862b4a29SBALATON Zoltan     case SRC_Y_X:
651862b4a29SBALATON Zoltan         s->regs.src_x = data & 0x3fff;
652862b4a29SBALATON Zoltan         s->regs.src_y = (data >> 16) & 0x3fff;
653862b4a29SBALATON Zoltan         break;
654862b4a29SBALATON Zoltan     case DST_Y_X:
655862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
656862b4a29SBALATON Zoltan         s->regs.dst_y = (data >> 16) & 0x3fff;
657862b4a29SBALATON Zoltan         break;
658862b4a29SBALATON Zoltan     case DST_HEIGHT_WIDTH:
659862b4a29SBALATON Zoltan         s->regs.dst_width = data & 0x3fff;
660862b4a29SBALATON Zoltan         s->regs.dst_height = (data >> 16) & 0x3fff;
661862b4a29SBALATON Zoltan         ati_2d_blt(s);
662862b4a29SBALATON Zoltan         break;
663862b4a29SBALATON Zoltan     case DP_GUI_MASTER_CNTL:
664862b4a29SBALATON Zoltan         s->regs.dp_gui_master_cntl = data & 0xf800000f;
665862b4a29SBALATON Zoltan         s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
666862b4a29SBALATON Zoltan                               (data & 0x4000) << 16;
667862b4a29SBALATON Zoltan         s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
668862b4a29SBALATON Zoltan         break;
669862b4a29SBALATON Zoltan     case DST_WIDTH_X:
670862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
671862b4a29SBALATON Zoltan         s->regs.dst_width = (data >> 16) & 0x3fff;
672862b4a29SBALATON Zoltan         ati_2d_blt(s);
673862b4a29SBALATON Zoltan         break;
674862b4a29SBALATON Zoltan     case SRC_X_Y:
675862b4a29SBALATON Zoltan         s->regs.src_y = data & 0x3fff;
676862b4a29SBALATON Zoltan         s->regs.src_x = (data >> 16) & 0x3fff;
677862b4a29SBALATON Zoltan         break;
678862b4a29SBALATON Zoltan     case DST_X_Y:
679862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
680862b4a29SBALATON Zoltan         s->regs.dst_x = (data >> 16) & 0x3fff;
681862b4a29SBALATON Zoltan         break;
682862b4a29SBALATON Zoltan     case DST_WIDTH_HEIGHT:
683862b4a29SBALATON Zoltan         s->regs.dst_height = data & 0x3fff;
684862b4a29SBALATON Zoltan         s->regs.dst_width = (data >> 16) & 0x3fff;
685862b4a29SBALATON Zoltan         ati_2d_blt(s);
686862b4a29SBALATON Zoltan         break;
687862b4a29SBALATON Zoltan     case DST_HEIGHT_Y:
688862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
689862b4a29SBALATON Zoltan         s->regs.dst_height = (data >> 16) & 0x3fff;
690862b4a29SBALATON Zoltan         break;
691862b4a29SBALATON Zoltan     case SRC_OFFSET:
692862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
693862b4a29SBALATON Zoltan             s->regs.src_offset = data & 0xfffffff0;
694862b4a29SBALATON Zoltan         } else {
695862b4a29SBALATON Zoltan             s->regs.src_offset = data & 0xfffffc00;
696862b4a29SBALATON Zoltan         }
697862b4a29SBALATON Zoltan         break;
698862b4a29SBALATON Zoltan     case SRC_PITCH:
699862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
700862b4a29SBALATON Zoltan             s->regs.src_pitch = data & 0x3fff;
701862b4a29SBALATON Zoltan             s->regs.src_tile = (data >> 16) & 1;
702862b4a29SBALATON Zoltan         } else {
703862b4a29SBALATON Zoltan             s->regs.src_pitch = data & 0x3ff0;
704862b4a29SBALATON Zoltan         }
705862b4a29SBALATON Zoltan         break;
706862b4a29SBALATON Zoltan     case DP_BRUSH_BKGD_CLR:
707862b4a29SBALATON Zoltan         s->regs.dp_brush_bkgd_clr = data;
708862b4a29SBALATON Zoltan         break;
709862b4a29SBALATON Zoltan     case DP_BRUSH_FRGD_CLR:
710862b4a29SBALATON Zoltan         s->regs.dp_brush_frgd_clr = data;
711862b4a29SBALATON Zoltan         break;
712862b4a29SBALATON Zoltan     case DP_CNTL:
713862b4a29SBALATON Zoltan         s->regs.dp_cntl = data;
714862b4a29SBALATON Zoltan         break;
715862b4a29SBALATON Zoltan     case DP_DATATYPE:
716862b4a29SBALATON Zoltan         s->regs.dp_datatype = data & 0xe0070f0f;
717862b4a29SBALATON Zoltan         break;
718862b4a29SBALATON Zoltan     case DP_MIX:
719862b4a29SBALATON Zoltan         s->regs.dp_mix = data & 0x00ff0700;
720862b4a29SBALATON Zoltan         break;
721862b4a29SBALATON Zoltan     case DP_WRITE_MASK:
722862b4a29SBALATON Zoltan         s->regs.dp_write_mask = data;
723862b4a29SBALATON Zoltan         break;
724862b4a29SBALATON Zoltan     case DEFAULT_OFFSET:
725862b4a29SBALATON Zoltan         data &= (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF ?
726862b4a29SBALATON Zoltan                  0x03fffc00 : 0xfffffc00);
727862b4a29SBALATON Zoltan         s->regs.default_offset = data;
728862b4a29SBALATON Zoltan         break;
729862b4a29SBALATON Zoltan     case DEFAULT_PITCH:
730862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
731862b4a29SBALATON Zoltan             s->regs.default_pitch = data & 0x103ff;
732862b4a29SBALATON Zoltan         }
733862b4a29SBALATON Zoltan         break;
734862b4a29SBALATON Zoltan     case DEFAULT_SC_BOTTOM_RIGHT:
735862b4a29SBALATON Zoltan         s->regs.default_sc_bottom_right = data & 0x3fff3fff;
736862b4a29SBALATON Zoltan         break;
737862b4a29SBALATON Zoltan     default:
738862b4a29SBALATON Zoltan         break;
739862b4a29SBALATON Zoltan     }
740862b4a29SBALATON Zoltan }
741862b4a29SBALATON Zoltan 
742862b4a29SBALATON Zoltan static const MemoryRegionOps ati_mm_ops = {
743862b4a29SBALATON Zoltan     .read = ati_mm_read,
744862b4a29SBALATON Zoltan     .write = ati_mm_write,
745862b4a29SBALATON Zoltan     .endianness = DEVICE_LITTLE_ENDIAN,
746862b4a29SBALATON Zoltan };
747862b4a29SBALATON Zoltan 
748862b4a29SBALATON Zoltan static void ati_vga_realize(PCIDevice *dev, Error **errp)
749862b4a29SBALATON Zoltan {
750862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
751862b4a29SBALATON Zoltan     VGACommonState *vga = &s->vga;
752862b4a29SBALATON Zoltan 
753862b4a29SBALATON Zoltan     if (s->model) {
754862b4a29SBALATON Zoltan         int i;
755862b4a29SBALATON Zoltan         for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
756862b4a29SBALATON Zoltan             if (!strcmp(s->model, ati_model_aliases[i].name)) {
757862b4a29SBALATON Zoltan                 s->dev_id = ati_model_aliases[i].dev_id;
758862b4a29SBALATON Zoltan                 break;
759862b4a29SBALATON Zoltan             }
760862b4a29SBALATON Zoltan         }
761862b4a29SBALATON Zoltan         if (i >= ARRAY_SIZE(ati_model_aliases)) {
762862b4a29SBALATON Zoltan             warn_report("Unknown ATI VGA model name, "
763862b4a29SBALATON Zoltan                         "using default rage128p");
764862b4a29SBALATON Zoltan         }
765862b4a29SBALATON Zoltan     }
766862b4a29SBALATON Zoltan     if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
767862b4a29SBALATON Zoltan         s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
768862b4a29SBALATON Zoltan         error_setg(errp, "Unknown ATI VGA device id, "
769862b4a29SBALATON Zoltan                    "only 0x5046 and 0x5159 are supported");
770862b4a29SBALATON Zoltan         return;
771862b4a29SBALATON Zoltan     }
772862b4a29SBALATON Zoltan     pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
773862b4a29SBALATON Zoltan 
774862b4a29SBALATON Zoltan     if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
775862b4a29SBALATON Zoltan         s->vga.vram_size_mb < 16) {
776862b4a29SBALATON Zoltan         warn_report("Too small video memory for device id");
777862b4a29SBALATON Zoltan         s->vga.vram_size_mb = 16;
778862b4a29SBALATON Zoltan     }
779862b4a29SBALATON Zoltan 
780862b4a29SBALATON Zoltan     /* init vga bits */
781862b4a29SBALATON Zoltan     vga_common_init(vga, OBJECT(s));
782862b4a29SBALATON Zoltan     vga_init(vga, OBJECT(s), pci_address_space(dev),
783862b4a29SBALATON Zoltan              pci_address_space_io(dev), true);
784862b4a29SBALATON Zoltan     vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga);
785862b4a29SBALATON Zoltan     if (s->cursor_guest_mode) {
786862b4a29SBALATON Zoltan         vga->cursor_invalidate = ati_cursor_invalidate;
787862b4a29SBALATON Zoltan         vga->cursor_draw_line = ati_cursor_draw_line;
788862b4a29SBALATON Zoltan     }
789862b4a29SBALATON Zoltan 
790862b4a29SBALATON Zoltan     /* mmio register space */
791862b4a29SBALATON Zoltan     memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
792862b4a29SBALATON Zoltan                           "ati.mmregs", 0x4000);
793862b4a29SBALATON Zoltan     /* io space is alias to beginning of mmregs */
794862b4a29SBALATON Zoltan     memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
795862b4a29SBALATON Zoltan 
796862b4a29SBALATON Zoltan     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
797862b4a29SBALATON Zoltan     pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
798862b4a29SBALATON Zoltan     pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
799862b4a29SBALATON Zoltan }
800862b4a29SBALATON Zoltan 
801862b4a29SBALATON Zoltan static void ati_vga_reset(DeviceState *dev)
802862b4a29SBALATON Zoltan {
803862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
804862b4a29SBALATON Zoltan 
805862b4a29SBALATON Zoltan     /* reset vga */
806862b4a29SBALATON Zoltan     vga_common_reset(&s->vga);
807862b4a29SBALATON Zoltan     s->mode = VGA_MODE;
808862b4a29SBALATON Zoltan }
809862b4a29SBALATON Zoltan 
810862b4a29SBALATON Zoltan static void ati_vga_exit(PCIDevice *dev)
811862b4a29SBALATON Zoltan {
812862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
813862b4a29SBALATON Zoltan 
814862b4a29SBALATON Zoltan     graphic_console_close(s->vga.con);
815862b4a29SBALATON Zoltan }
816862b4a29SBALATON Zoltan 
817862b4a29SBALATON Zoltan static Property ati_vga_properties[] = {
818862b4a29SBALATON Zoltan     DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
819862b4a29SBALATON Zoltan     DEFINE_PROP_STRING("model", ATIVGAState, model),
820862b4a29SBALATON Zoltan     DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
821862b4a29SBALATON Zoltan                        PCI_DEVICE_ID_ATI_RAGE128_PF),
822862b4a29SBALATON Zoltan     DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
823862b4a29SBALATON Zoltan     DEFINE_PROP_END_OF_LIST()
824862b4a29SBALATON Zoltan };
825862b4a29SBALATON Zoltan 
826862b4a29SBALATON Zoltan static void ati_vga_class_init(ObjectClass *klass, void *data)
827862b4a29SBALATON Zoltan {
828862b4a29SBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
829862b4a29SBALATON Zoltan     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
830862b4a29SBALATON Zoltan 
831862b4a29SBALATON Zoltan     dc->reset = ati_vga_reset;
832862b4a29SBALATON Zoltan     dc->props = ati_vga_properties;
833862b4a29SBALATON Zoltan     dc->hotpluggable = false;
834862b4a29SBALATON Zoltan     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
835862b4a29SBALATON Zoltan 
836862b4a29SBALATON Zoltan     k->class_id = PCI_CLASS_DISPLAY_VGA;
837862b4a29SBALATON Zoltan     k->vendor_id = PCI_VENDOR_ID_ATI;
838862b4a29SBALATON Zoltan     k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
839862b4a29SBALATON Zoltan     k->romfile = "vgabios-stdvga.bin";
840862b4a29SBALATON Zoltan     k->realize = ati_vga_realize;
841862b4a29SBALATON Zoltan     k->exit = ati_vga_exit;
842862b4a29SBALATON Zoltan }
843862b4a29SBALATON Zoltan 
844862b4a29SBALATON Zoltan static const TypeInfo ati_vga_info = {
845862b4a29SBALATON Zoltan     .name = TYPE_ATI_VGA,
846862b4a29SBALATON Zoltan     .parent = TYPE_PCI_DEVICE,
847862b4a29SBALATON Zoltan     .instance_size = sizeof(ATIVGAState),
848862b4a29SBALATON Zoltan     .class_init = ati_vga_class_init,
849862b4a29SBALATON Zoltan     .interfaces = (InterfaceInfo[]) {
850862b4a29SBALATON Zoltan           { INTERFACE_CONVENTIONAL_PCI_DEVICE },
851862b4a29SBALATON Zoltan           { },
852862b4a29SBALATON Zoltan     },
853862b4a29SBALATON Zoltan };
854862b4a29SBALATON Zoltan 
855862b4a29SBALATON Zoltan static void ati_vga_register_types(void)
856862b4a29SBALATON Zoltan {
857862b4a29SBALATON Zoltan     type_register_static(&ati_vga_info);
858862b4a29SBALATON Zoltan }
859862b4a29SBALATON Zoltan 
860862b4a29SBALATON Zoltan type_init(ati_vga_register_types)
861