xref: /qemu/hw/display/ati.c (revision 862b4a291dcf143fdb227e97feb7fd45e6466aca)
1*862b4a29SBALATON Zoltan /*
2*862b4a29SBALATON Zoltan  * QEMU ATI SVGA emulation
3*862b4a29SBALATON Zoltan  *
4*862b4a29SBALATON Zoltan  * Copyright (c) 2019 BALATON Zoltan
5*862b4a29SBALATON Zoltan  *
6*862b4a29SBALATON Zoltan  * This work is licensed under the GNU GPL license version 2 or later.
7*862b4a29SBALATON Zoltan  */
8*862b4a29SBALATON Zoltan 
9*862b4a29SBALATON Zoltan /*
10*862b4a29SBALATON Zoltan  * WARNING:
11*862b4a29SBALATON Zoltan  * This is very incomplete and only enough for Linux console and some
12*862b4a29SBALATON Zoltan  * unaccelerated X output at the moment.
13*862b4a29SBALATON Zoltan  * Currently it's little more than a frame buffer with minimal functions,
14*862b4a29SBALATON Zoltan  * other more advanced features of the hardware are yet to be implemented.
15*862b4a29SBALATON Zoltan  * We only aim for Rage 128 Pro (and some RV100) and 2D only at first,
16*862b4a29SBALATON Zoltan  * No 3D at all yet (maybe after 2D works, but feel free to improve it)
17*862b4a29SBALATON Zoltan  */
18*862b4a29SBALATON Zoltan 
19*862b4a29SBALATON Zoltan #include "ati_int.h"
20*862b4a29SBALATON Zoltan #include "ati_regs.h"
21*862b4a29SBALATON Zoltan #include "vga_regs.h"
22*862b4a29SBALATON Zoltan #include "qemu/log.h"
23*862b4a29SBALATON Zoltan #include "qemu/error-report.h"
24*862b4a29SBALATON Zoltan #include "qapi/error.h"
25*862b4a29SBALATON Zoltan #include "hw/hw.h"
26*862b4a29SBALATON Zoltan #include "ui/console.h"
27*862b4a29SBALATON Zoltan #include "trace.h"
28*862b4a29SBALATON Zoltan 
29*862b4a29SBALATON Zoltan #define ATI_DEBUG_HW_CURSOR 0
30*862b4a29SBALATON Zoltan 
31*862b4a29SBALATON Zoltan static const struct {
32*862b4a29SBALATON Zoltan     const char *name;
33*862b4a29SBALATON Zoltan     uint16_t dev_id;
34*862b4a29SBALATON Zoltan } ati_model_aliases[] = {
35*862b4a29SBALATON Zoltan     { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
36*862b4a29SBALATON Zoltan     { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
37*862b4a29SBALATON Zoltan };
38*862b4a29SBALATON Zoltan 
39*862b4a29SBALATON Zoltan enum { VGA_MODE, EXT_MODE };
40*862b4a29SBALATON Zoltan 
41*862b4a29SBALATON Zoltan static void ati_vga_switch_mode(ATIVGAState *s)
42*862b4a29SBALATON Zoltan {
43*862b4a29SBALATON Zoltan     DPRINTF("%d -> %d\n",
44*862b4a29SBALATON Zoltan             s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
45*862b4a29SBALATON Zoltan     if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
46*862b4a29SBALATON Zoltan         /* Extended mode enabled */
47*862b4a29SBALATON Zoltan         s->mode = EXT_MODE;
48*862b4a29SBALATON Zoltan         if (s->regs.crtc_gen_cntl & CRTC2_EN) {
49*862b4a29SBALATON Zoltan             /* CRT controller enabled, use CRTC values */
50*862b4a29SBALATON Zoltan             uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
51*862b4a29SBALATON Zoltan             int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
52*862b4a29SBALATON Zoltan             int bpp = 0;
53*862b4a29SBALATON Zoltan             int h, v;
54*862b4a29SBALATON Zoltan 
55*862b4a29SBALATON Zoltan             if (s->regs.crtc_h_total_disp == 0) {
56*862b4a29SBALATON Zoltan                 s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
57*862b4a29SBALATON Zoltan             }
58*862b4a29SBALATON Zoltan             if (s->regs.crtc_v_total_disp == 0) {
59*862b4a29SBALATON Zoltan                 s->regs.crtc_v_total_disp = (480 - 1) << 16;
60*862b4a29SBALATON Zoltan             }
61*862b4a29SBALATON Zoltan             h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
62*862b4a29SBALATON Zoltan             v = (s->regs.crtc_v_total_disp >> 16) + 1;
63*862b4a29SBALATON Zoltan             switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
64*862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_4BPP:
65*862b4a29SBALATON Zoltan                 bpp = 4;
66*862b4a29SBALATON Zoltan                 break;
67*862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_8BPP:
68*862b4a29SBALATON Zoltan                 bpp = 8;
69*862b4a29SBALATON Zoltan                 break;
70*862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_15BPP:
71*862b4a29SBALATON Zoltan                 bpp = 15;
72*862b4a29SBALATON Zoltan                 break;
73*862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_16BPP:
74*862b4a29SBALATON Zoltan                 bpp = 16;
75*862b4a29SBALATON Zoltan                 break;
76*862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_24BPP:
77*862b4a29SBALATON Zoltan                 bpp = 24;
78*862b4a29SBALATON Zoltan                 break;
79*862b4a29SBALATON Zoltan             case CRTC_PIX_WIDTH_32BPP:
80*862b4a29SBALATON Zoltan                 bpp = 32;
81*862b4a29SBALATON Zoltan                 break;
82*862b4a29SBALATON Zoltan             default:
83*862b4a29SBALATON Zoltan                 qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
84*862b4a29SBALATON Zoltan             }
85*862b4a29SBALATON Zoltan             assert(bpp != 0);
86*862b4a29SBALATON Zoltan             DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
87*862b4a29SBALATON Zoltan             vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
88*862b4a29SBALATON Zoltan             vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
89*862b4a29SBALATON Zoltan             /* reset VBE regs then set up mode */
90*862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
91*862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
92*862b4a29SBALATON Zoltan             s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
93*862b4a29SBALATON Zoltan             /* enable mode via ioport so it updates vga regs */
94*862b4a29SBALATON Zoltan             vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
95*862b4a29SBALATON Zoltan             vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
96*862b4a29SBALATON Zoltan                 VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
97*862b4a29SBALATON Zoltan                 (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
98*862b4a29SBALATON Zoltan             /* now set offset and stride after enable as that resets these */
99*862b4a29SBALATON Zoltan             if (stride) {
100*862b4a29SBALATON Zoltan                 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
101*862b4a29SBALATON Zoltan                 vbe_ioport_write_data(&s->vga, 0, stride);
102*862b4a29SBALATON Zoltan                 if (offs % stride == 0) {
103*862b4a29SBALATON Zoltan                     vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
104*862b4a29SBALATON Zoltan                     vbe_ioport_write_data(&s->vga, 0, offs / stride);
105*862b4a29SBALATON Zoltan                 } else {
106*862b4a29SBALATON Zoltan                     /* FIXME what to do with this? */
107*862b4a29SBALATON Zoltan                     error_report("VGA offset is not multiple of pitch, "
108*862b4a29SBALATON Zoltan                                  "expect bad picture");
109*862b4a29SBALATON Zoltan                 }
110*862b4a29SBALATON Zoltan             }
111*862b4a29SBALATON Zoltan         }
112*862b4a29SBALATON Zoltan     } else {
113*862b4a29SBALATON Zoltan         /* VGA mode enabled */
114*862b4a29SBALATON Zoltan         s->mode = VGA_MODE;
115*862b4a29SBALATON Zoltan         vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
116*862b4a29SBALATON Zoltan         vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
117*862b4a29SBALATON Zoltan     }
118*862b4a29SBALATON Zoltan }
119*862b4a29SBALATON Zoltan 
120*862b4a29SBALATON Zoltan /* Used by host side hardware cursor */
121*862b4a29SBALATON Zoltan static void ati_cursor_define(ATIVGAState *s)
122*862b4a29SBALATON Zoltan {
123*862b4a29SBALATON Zoltan     uint8_t data[1024];
124*862b4a29SBALATON Zoltan     uint8_t *src;
125*862b4a29SBALATON Zoltan     int i, j, idx = 0;
126*862b4a29SBALATON Zoltan 
127*862b4a29SBALATON Zoltan     if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
128*862b4a29SBALATON Zoltan         return; /* Do not update cursor if locked or rendered by guest */
129*862b4a29SBALATON Zoltan     }
130*862b4a29SBALATON Zoltan     /* FIXME handle cur_hv_offs correctly */
131*862b4a29SBALATON Zoltan     src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
132*862b4a29SBALATON Zoltan           s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
133*862b4a29SBALATON Zoltan           (s->regs.cur_hv_offs & 0xffff) * 16;
134*862b4a29SBALATON Zoltan     for (i = 0; i < 64; i++) {
135*862b4a29SBALATON Zoltan         for (j = 0; j < 8; j++, idx++) {
136*862b4a29SBALATON Zoltan             data[idx] = src[i * 16 + j];
137*862b4a29SBALATON Zoltan             data[512 + idx] = src[i * 16 + j + 8];
138*862b4a29SBALATON Zoltan         }
139*862b4a29SBALATON Zoltan     }
140*862b4a29SBALATON Zoltan     if (!s->cursor) {
141*862b4a29SBALATON Zoltan         s->cursor = cursor_alloc(64, 64);
142*862b4a29SBALATON Zoltan     }
143*862b4a29SBALATON Zoltan     cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
144*862b4a29SBALATON Zoltan                     &data[512], 1, &data[0]);
145*862b4a29SBALATON Zoltan     dpy_cursor_define(s->vga.con, s->cursor);
146*862b4a29SBALATON Zoltan }
147*862b4a29SBALATON Zoltan 
148*862b4a29SBALATON Zoltan /* Alternatively support guest rendered hardware cursor */
149*862b4a29SBALATON Zoltan static void ati_cursor_invalidate(VGACommonState *vga)
150*862b4a29SBALATON Zoltan {
151*862b4a29SBALATON Zoltan     ATIVGAState *s = container_of(vga, ATIVGAState, vga);
152*862b4a29SBALATON Zoltan     int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
153*862b4a29SBALATON Zoltan 
154*862b4a29SBALATON Zoltan     if (s->regs.cur_offset & BIT(31)) {
155*862b4a29SBALATON Zoltan         return; /* Do not update cursor if locked */
156*862b4a29SBALATON Zoltan     }
157*862b4a29SBALATON Zoltan     if (s->cursor_size != size ||
158*862b4a29SBALATON Zoltan         vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
159*862b4a29SBALATON Zoltan         vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
160*862b4a29SBALATON Zoltan         s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
161*862b4a29SBALATON Zoltan         (s->regs.cur_hv_offs & 0xffff) * 16) {
162*862b4a29SBALATON Zoltan         /* Remove old cursor then update and show new one if needed */
163*862b4a29SBALATON Zoltan         vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
164*862b4a29SBALATON Zoltan         vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
165*862b4a29SBALATON Zoltan         vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
166*862b4a29SBALATON Zoltan         s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
167*862b4a29SBALATON Zoltan                            (s->regs.cur_hv_offs & 0xffff) * 16;
168*862b4a29SBALATON Zoltan         s->cursor_size = size;
169*862b4a29SBALATON Zoltan         if (size) {
170*862b4a29SBALATON Zoltan             vga_invalidate_scanlines(vga,
171*862b4a29SBALATON Zoltan                                      vga->hw_cursor_y, vga->hw_cursor_y + 63);
172*862b4a29SBALATON Zoltan         }
173*862b4a29SBALATON Zoltan     }
174*862b4a29SBALATON Zoltan }
175*862b4a29SBALATON Zoltan 
176*862b4a29SBALATON Zoltan static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
177*862b4a29SBALATON Zoltan {
178*862b4a29SBALATON Zoltan     ATIVGAState *s = container_of(vga, ATIVGAState, vga);
179*862b4a29SBALATON Zoltan     uint8_t *src;
180*862b4a29SBALATON Zoltan     uint32_t *dp = (uint32_t *)d;
181*862b4a29SBALATON Zoltan     int i, j, h;
182*862b4a29SBALATON Zoltan 
183*862b4a29SBALATON Zoltan     if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
184*862b4a29SBALATON Zoltan         scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
185*862b4a29SBALATON Zoltan         scr_y > s->regs.crtc_v_total_disp >> 16) {
186*862b4a29SBALATON Zoltan         return;
187*862b4a29SBALATON Zoltan     }
188*862b4a29SBALATON Zoltan     /* FIXME handle cur_hv_offs correctly */
189*862b4a29SBALATON Zoltan     src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
190*862b4a29SBALATON Zoltan           s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
191*862b4a29SBALATON Zoltan     dp = &dp[vga->hw_cursor_x];
192*862b4a29SBALATON Zoltan     h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
193*862b4a29SBALATON Zoltan     for (i = 0; i < 8; i++) {
194*862b4a29SBALATON Zoltan         uint32_t color;
195*862b4a29SBALATON Zoltan         uint8_t abits = src[i];
196*862b4a29SBALATON Zoltan         uint8_t xbits = src[i + 8];
197*862b4a29SBALATON Zoltan         for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
198*862b4a29SBALATON Zoltan             if (abits & BIT(7)) {
199*862b4a29SBALATON Zoltan                 if (xbits & BIT(7)) {
200*862b4a29SBALATON Zoltan                     color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
201*862b4a29SBALATON Zoltan                 } else {
202*862b4a29SBALATON Zoltan                     continue; /* transparent, no change */
203*862b4a29SBALATON Zoltan                 }
204*862b4a29SBALATON Zoltan             } else {
205*862b4a29SBALATON Zoltan                 color = (xbits & BIT(7) ? s->regs.cur_color1 :
206*862b4a29SBALATON Zoltan                                           s->regs.cur_color0) << 8 | 0xff;
207*862b4a29SBALATON Zoltan             }
208*862b4a29SBALATON Zoltan             if (vga->hw_cursor_x + i * 8 + j >= h) {
209*862b4a29SBALATON Zoltan                 return; /* end of screen, don't span to next line */
210*862b4a29SBALATON Zoltan             }
211*862b4a29SBALATON Zoltan             dp[i * 8 + j] = color;
212*862b4a29SBALATON Zoltan         }
213*862b4a29SBALATON Zoltan     }
214*862b4a29SBALATON Zoltan }
215*862b4a29SBALATON Zoltan 
216*862b4a29SBALATON Zoltan static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
217*862b4a29SBALATON Zoltan                                          unsigned int size)
218*862b4a29SBALATON Zoltan {
219*862b4a29SBALATON Zoltan     if (offs == 0 && size == 4) {
220*862b4a29SBALATON Zoltan         return reg;
221*862b4a29SBALATON Zoltan     } else {
222*862b4a29SBALATON Zoltan         return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
223*862b4a29SBALATON Zoltan     }
224*862b4a29SBALATON Zoltan }
225*862b4a29SBALATON Zoltan 
226*862b4a29SBALATON Zoltan static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
227*862b4a29SBALATON Zoltan {
228*862b4a29SBALATON Zoltan     ATIVGAState *s = opaque;
229*862b4a29SBALATON Zoltan     uint64_t val = 0;
230*862b4a29SBALATON Zoltan 
231*862b4a29SBALATON Zoltan     switch (addr) {
232*862b4a29SBALATON Zoltan     case MM_INDEX:
233*862b4a29SBALATON Zoltan         val = s->regs.mm_index;
234*862b4a29SBALATON Zoltan         break;
235*862b4a29SBALATON Zoltan     case MM_DATA ... MM_DATA + 3:
236*862b4a29SBALATON Zoltan         /* indexed access to regs or memory */
237*862b4a29SBALATON Zoltan         if (s->regs.mm_index & BIT(31)) {
238*862b4a29SBALATON Zoltan             if (s->regs.mm_index <= s->vga.vram_size - size) {
239*862b4a29SBALATON Zoltan                 int i = size - 1;
240*862b4a29SBALATON Zoltan                 while (i >= 0) {
241*862b4a29SBALATON Zoltan                     val <<= 8;
242*862b4a29SBALATON Zoltan                     val |= s->vga.vram_ptr[s->regs.mm_index + i--];
243*862b4a29SBALATON Zoltan                 }
244*862b4a29SBALATON Zoltan             }
245*862b4a29SBALATON Zoltan         } else {
246*862b4a29SBALATON Zoltan             val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
247*862b4a29SBALATON Zoltan         }
248*862b4a29SBALATON Zoltan         break;
249*862b4a29SBALATON Zoltan     case BIOS_0_SCRATCH ... BUS_CNTL - 1:
250*862b4a29SBALATON Zoltan     {
251*862b4a29SBALATON Zoltan         int i = (addr - BIOS_0_SCRATCH) / 4;
252*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
253*862b4a29SBALATON Zoltan             break;
254*862b4a29SBALATON Zoltan         }
255*862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.bios_scratch[i],
256*862b4a29SBALATON Zoltan                                 addr - (BIOS_0_SCRATCH + i * 4), size);
257*862b4a29SBALATON Zoltan         break;
258*862b4a29SBALATON Zoltan     }
259*862b4a29SBALATON Zoltan     case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
260*862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
261*862b4a29SBALATON Zoltan                                 addr - CRTC_GEN_CNTL, size);
262*862b4a29SBALATON Zoltan         break;
263*862b4a29SBALATON Zoltan     case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
264*862b4a29SBALATON Zoltan         val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
265*862b4a29SBALATON Zoltan                                 addr - CRTC_EXT_CNTL, size);
266*862b4a29SBALATON Zoltan         break;
267*862b4a29SBALATON Zoltan     case DAC_CNTL:
268*862b4a29SBALATON Zoltan         val = s->regs.dac_cntl;
269*862b4a29SBALATON Zoltan         break;
270*862b4a29SBALATON Zoltan /*    case GPIO_MONID: FIXME hook up DDC I2C here */
271*862b4a29SBALATON Zoltan     case PALETTE_INDEX:
272*862b4a29SBALATON Zoltan         /* FIXME unaligned access */
273*862b4a29SBALATON Zoltan         val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
274*862b4a29SBALATON Zoltan         val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
275*862b4a29SBALATON Zoltan         break;
276*862b4a29SBALATON Zoltan     case PALETTE_DATA:
277*862b4a29SBALATON Zoltan         val = vga_ioport_read(&s->vga, VGA_PEL_D);
278*862b4a29SBALATON Zoltan         break;
279*862b4a29SBALATON Zoltan     case CNFG_MEMSIZE:
280*862b4a29SBALATON Zoltan         val = s->vga.vram_size;
281*862b4a29SBALATON Zoltan         break;
282*862b4a29SBALATON Zoltan     case MC_STATUS:
283*862b4a29SBALATON Zoltan         val = 5;
284*862b4a29SBALATON Zoltan         break;
285*862b4a29SBALATON Zoltan     case RBBM_STATUS:
286*862b4a29SBALATON Zoltan     case GUI_STAT:
287*862b4a29SBALATON Zoltan         val = 64; /* free CMDFIFO entries */
288*862b4a29SBALATON Zoltan         break;
289*862b4a29SBALATON Zoltan     case CRTC_H_TOTAL_DISP:
290*862b4a29SBALATON Zoltan         val = s->regs.crtc_h_total_disp;
291*862b4a29SBALATON Zoltan         break;
292*862b4a29SBALATON Zoltan     case CRTC_H_SYNC_STRT_WID:
293*862b4a29SBALATON Zoltan         val = s->regs.crtc_h_sync_strt_wid;
294*862b4a29SBALATON Zoltan         break;
295*862b4a29SBALATON Zoltan     case CRTC_V_TOTAL_DISP:
296*862b4a29SBALATON Zoltan         val = s->regs.crtc_v_total_disp;
297*862b4a29SBALATON Zoltan         break;
298*862b4a29SBALATON Zoltan     case CRTC_V_SYNC_STRT_WID:
299*862b4a29SBALATON Zoltan         val = s->regs.crtc_v_sync_strt_wid;
300*862b4a29SBALATON Zoltan         break;
301*862b4a29SBALATON Zoltan     case CRTC_OFFSET:
302*862b4a29SBALATON Zoltan         val = s->regs.crtc_offset;
303*862b4a29SBALATON Zoltan         break;
304*862b4a29SBALATON Zoltan     case CRTC_OFFSET_CNTL:
305*862b4a29SBALATON Zoltan         val = s->regs.crtc_offset_cntl;
306*862b4a29SBALATON Zoltan         break;
307*862b4a29SBALATON Zoltan     case CRTC_PITCH:
308*862b4a29SBALATON Zoltan         val = s->regs.crtc_pitch;
309*862b4a29SBALATON Zoltan         break;
310*862b4a29SBALATON Zoltan     case 0xf00 ... 0xfff:
311*862b4a29SBALATON Zoltan         val = pci_default_read_config(&s->dev, addr - 0xf00, size);
312*862b4a29SBALATON Zoltan         break;
313*862b4a29SBALATON Zoltan     case CUR_OFFSET:
314*862b4a29SBALATON Zoltan         val = s->regs.cur_offset;
315*862b4a29SBALATON Zoltan         break;
316*862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_POSN:
317*862b4a29SBALATON Zoltan         val = s->regs.cur_hv_pos;
318*862b4a29SBALATON Zoltan         val |= s->regs.cur_offset & BIT(31);
319*862b4a29SBALATON Zoltan         break;
320*862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_OFF:
321*862b4a29SBALATON Zoltan         val = s->regs.cur_hv_offs;
322*862b4a29SBALATON Zoltan         val |= s->regs.cur_offset & BIT(31);
323*862b4a29SBALATON Zoltan         break;
324*862b4a29SBALATON Zoltan     case CUR_CLR0:
325*862b4a29SBALATON Zoltan         val = s->regs.cur_color0;
326*862b4a29SBALATON Zoltan         break;
327*862b4a29SBALATON Zoltan     case CUR_CLR1:
328*862b4a29SBALATON Zoltan         val = s->regs.cur_color1;
329*862b4a29SBALATON Zoltan         break;
330*862b4a29SBALATON Zoltan     case DST_OFFSET:
331*862b4a29SBALATON Zoltan         val = s->regs.dst_offset;
332*862b4a29SBALATON Zoltan         break;
333*862b4a29SBALATON Zoltan     case DST_PITCH:
334*862b4a29SBALATON Zoltan         val = s->regs.dst_pitch;
335*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
336*862b4a29SBALATON Zoltan             val &= s->regs.dst_tile << 16;
337*862b4a29SBALATON Zoltan         }
338*862b4a29SBALATON Zoltan         break;
339*862b4a29SBALATON Zoltan     case DST_WIDTH:
340*862b4a29SBALATON Zoltan         val = s->regs.dst_width;
341*862b4a29SBALATON Zoltan         break;
342*862b4a29SBALATON Zoltan     case DST_HEIGHT:
343*862b4a29SBALATON Zoltan         val = s->regs.dst_height;
344*862b4a29SBALATON Zoltan         break;
345*862b4a29SBALATON Zoltan     case SRC_X:
346*862b4a29SBALATON Zoltan         val = s->regs.src_x;
347*862b4a29SBALATON Zoltan         break;
348*862b4a29SBALATON Zoltan     case SRC_Y:
349*862b4a29SBALATON Zoltan         val = s->regs.src_y;
350*862b4a29SBALATON Zoltan         break;
351*862b4a29SBALATON Zoltan     case DST_X:
352*862b4a29SBALATON Zoltan         val = s->regs.dst_x;
353*862b4a29SBALATON Zoltan         break;
354*862b4a29SBALATON Zoltan     case DST_Y:
355*862b4a29SBALATON Zoltan         val = s->regs.dst_y;
356*862b4a29SBALATON Zoltan         break;
357*862b4a29SBALATON Zoltan     case DP_GUI_MASTER_CNTL:
358*862b4a29SBALATON Zoltan         val = s->regs.dp_gui_master_cntl;
359*862b4a29SBALATON Zoltan         break;
360*862b4a29SBALATON Zoltan     case SRC_OFFSET:
361*862b4a29SBALATON Zoltan         val = s->regs.src_offset;
362*862b4a29SBALATON Zoltan         break;
363*862b4a29SBALATON Zoltan     case SRC_PITCH:
364*862b4a29SBALATON Zoltan         val = s->regs.src_pitch;
365*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
366*862b4a29SBALATON Zoltan             val &= s->regs.src_tile << 16;
367*862b4a29SBALATON Zoltan         }
368*862b4a29SBALATON Zoltan         break;
369*862b4a29SBALATON Zoltan     case DP_BRUSH_BKGD_CLR:
370*862b4a29SBALATON Zoltan         val = s->regs.dp_brush_bkgd_clr;
371*862b4a29SBALATON Zoltan         break;
372*862b4a29SBALATON Zoltan     case DP_BRUSH_FRGD_CLR:
373*862b4a29SBALATON Zoltan         val = s->regs.dp_brush_frgd_clr;
374*862b4a29SBALATON Zoltan         break;
375*862b4a29SBALATON Zoltan     case DP_SRC_FRGD_CLR:
376*862b4a29SBALATON Zoltan         val = s->regs.dp_src_frgd_clr;
377*862b4a29SBALATON Zoltan         break;
378*862b4a29SBALATON Zoltan     case DP_SRC_BKGD_CLR:
379*862b4a29SBALATON Zoltan         val = s->regs.dp_src_bkgd_clr;
380*862b4a29SBALATON Zoltan         break;
381*862b4a29SBALATON Zoltan     case DP_CNTL:
382*862b4a29SBALATON Zoltan         val = s->regs.dp_cntl;
383*862b4a29SBALATON Zoltan         break;
384*862b4a29SBALATON Zoltan     case DP_DATATYPE:
385*862b4a29SBALATON Zoltan         val = s->regs.dp_datatype;
386*862b4a29SBALATON Zoltan         break;
387*862b4a29SBALATON Zoltan     case DP_MIX:
388*862b4a29SBALATON Zoltan         val = s->regs.dp_mix;
389*862b4a29SBALATON Zoltan         break;
390*862b4a29SBALATON Zoltan     case DP_WRITE_MASK:
391*862b4a29SBALATON Zoltan         val = s->regs.dp_write_mask;
392*862b4a29SBALATON Zoltan         break;
393*862b4a29SBALATON Zoltan     case DEFAULT_OFFSET:
394*862b4a29SBALATON Zoltan         val = s->regs.default_offset;
395*862b4a29SBALATON Zoltan         break;
396*862b4a29SBALATON Zoltan     case DEFAULT_PITCH:
397*862b4a29SBALATON Zoltan         val = s->regs.default_pitch;
398*862b4a29SBALATON Zoltan         break;
399*862b4a29SBALATON Zoltan     case DEFAULT_SC_BOTTOM_RIGHT:
400*862b4a29SBALATON Zoltan         val = s->regs.default_sc_bottom_right;
401*862b4a29SBALATON Zoltan         break;
402*862b4a29SBALATON Zoltan     default:
403*862b4a29SBALATON Zoltan         break;
404*862b4a29SBALATON Zoltan     }
405*862b4a29SBALATON Zoltan     if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
406*862b4a29SBALATON Zoltan         trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
407*862b4a29SBALATON Zoltan     }
408*862b4a29SBALATON Zoltan     return val;
409*862b4a29SBALATON Zoltan }
410*862b4a29SBALATON Zoltan 
411*862b4a29SBALATON Zoltan static inline void ati_reg_write_offs(uint32_t *reg, int offs,
412*862b4a29SBALATON Zoltan                                       uint64_t data, unsigned int size)
413*862b4a29SBALATON Zoltan {
414*862b4a29SBALATON Zoltan     if (offs == 0 && size == 4) {
415*862b4a29SBALATON Zoltan         *reg = data;
416*862b4a29SBALATON Zoltan     } else {
417*862b4a29SBALATON Zoltan         *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
418*862b4a29SBALATON Zoltan                          data);
419*862b4a29SBALATON Zoltan     }
420*862b4a29SBALATON Zoltan }
421*862b4a29SBALATON Zoltan 
422*862b4a29SBALATON Zoltan static void ati_mm_write(void *opaque, hwaddr addr,
423*862b4a29SBALATON Zoltan                            uint64_t data, unsigned int size)
424*862b4a29SBALATON Zoltan {
425*862b4a29SBALATON Zoltan     ATIVGAState *s = opaque;
426*862b4a29SBALATON Zoltan 
427*862b4a29SBALATON Zoltan     if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
428*862b4a29SBALATON Zoltan         trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
429*862b4a29SBALATON Zoltan     }
430*862b4a29SBALATON Zoltan     switch (addr) {
431*862b4a29SBALATON Zoltan     case MM_INDEX:
432*862b4a29SBALATON Zoltan         s->regs.mm_index = data;
433*862b4a29SBALATON Zoltan         break;
434*862b4a29SBALATON Zoltan     case MM_DATA ... MM_DATA + 3:
435*862b4a29SBALATON Zoltan         /* indexed access to regs or memory */
436*862b4a29SBALATON Zoltan         if (s->regs.mm_index & BIT(31)) {
437*862b4a29SBALATON Zoltan             if (s->regs.mm_index <= s->vga.vram_size - size) {
438*862b4a29SBALATON Zoltan                 int i = 0;
439*862b4a29SBALATON Zoltan                 while (i < size) {
440*862b4a29SBALATON Zoltan                     s->vga.vram_ptr[s->regs.mm_index + i] = data & 0xff;
441*862b4a29SBALATON Zoltan                     data >>= 8;
442*862b4a29SBALATON Zoltan                 }
443*862b4a29SBALATON Zoltan             }
444*862b4a29SBALATON Zoltan         } else {
445*862b4a29SBALATON Zoltan             ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
446*862b4a29SBALATON Zoltan         }
447*862b4a29SBALATON Zoltan         break;
448*862b4a29SBALATON Zoltan     case BIOS_0_SCRATCH ... BUS_CNTL - 1:
449*862b4a29SBALATON Zoltan     {
450*862b4a29SBALATON Zoltan         int i = (addr - BIOS_0_SCRATCH) / 4;
451*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
452*862b4a29SBALATON Zoltan             break;
453*862b4a29SBALATON Zoltan         }
454*862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.bios_scratch[i],
455*862b4a29SBALATON Zoltan                            addr - (BIOS_0_SCRATCH + i * 4), data, size);
456*862b4a29SBALATON Zoltan         break;
457*862b4a29SBALATON Zoltan     }
458*862b4a29SBALATON Zoltan     case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
459*862b4a29SBALATON Zoltan     {
460*862b4a29SBALATON Zoltan         uint32_t val = s->regs.crtc_gen_cntl;
461*862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.crtc_gen_cntl,
462*862b4a29SBALATON Zoltan                            addr - CRTC_GEN_CNTL, data, size);
463*862b4a29SBALATON Zoltan         if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
464*862b4a29SBALATON Zoltan             if (s->cursor_guest_mode) {
465*862b4a29SBALATON Zoltan                 s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
466*862b4a29SBALATON Zoltan             } else {
467*862b4a29SBALATON Zoltan                 if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
468*862b4a29SBALATON Zoltan                     ati_cursor_define(s);
469*862b4a29SBALATON Zoltan                 }
470*862b4a29SBALATON Zoltan                 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
471*862b4a29SBALATON Zoltan                               s->regs.cur_hv_pos & 0xffff,
472*862b4a29SBALATON Zoltan                               (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
473*862b4a29SBALATON Zoltan             }
474*862b4a29SBALATON Zoltan         }
475*862b4a29SBALATON Zoltan         if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
476*862b4a29SBALATON Zoltan             (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
477*862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
478*862b4a29SBALATON Zoltan         }
479*862b4a29SBALATON Zoltan         break;
480*862b4a29SBALATON Zoltan     }
481*862b4a29SBALATON Zoltan     case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
482*862b4a29SBALATON Zoltan     {
483*862b4a29SBALATON Zoltan         uint32_t val = s->regs.crtc_ext_cntl;
484*862b4a29SBALATON Zoltan         ati_reg_write_offs(&s->regs.crtc_ext_cntl,
485*862b4a29SBALATON Zoltan                            addr - CRTC_EXT_CNTL, data, size);
486*862b4a29SBALATON Zoltan         if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
487*862b4a29SBALATON Zoltan             DPRINTF("Display disabled\n");
488*862b4a29SBALATON Zoltan             s->vga.ar_index &= ~BIT(5);
489*862b4a29SBALATON Zoltan         } else {
490*862b4a29SBALATON Zoltan             DPRINTF("Display enabled\n");
491*862b4a29SBALATON Zoltan             s->vga.ar_index |= BIT(5);
492*862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
493*862b4a29SBALATON Zoltan         }
494*862b4a29SBALATON Zoltan         if ((val & CRT_CRTC_DISPLAY_DIS) !=
495*862b4a29SBALATON Zoltan             (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
496*862b4a29SBALATON Zoltan             ati_vga_switch_mode(s);
497*862b4a29SBALATON Zoltan         }
498*862b4a29SBALATON Zoltan         break;
499*862b4a29SBALATON Zoltan     }
500*862b4a29SBALATON Zoltan     case DAC_CNTL:
501*862b4a29SBALATON Zoltan         s->regs.dac_cntl = data & 0xffffe3ff;
502*862b4a29SBALATON Zoltan         s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
503*862b4a29SBALATON Zoltan         break;
504*862b4a29SBALATON Zoltan /*    case GPIO_MONID: FIXME hook up DDC I2C here */
505*862b4a29SBALATON Zoltan     case PALETTE_INDEX ... PALETTE_INDEX + 3:
506*862b4a29SBALATON Zoltan         if (size == 4) {
507*862b4a29SBALATON Zoltan             vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
508*862b4a29SBALATON Zoltan             vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
509*862b4a29SBALATON Zoltan         } else {
510*862b4a29SBALATON Zoltan             if (addr == PALETTE_INDEX) {
511*862b4a29SBALATON Zoltan                 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
512*862b4a29SBALATON Zoltan             } else {
513*862b4a29SBALATON Zoltan                 vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
514*862b4a29SBALATON Zoltan             }
515*862b4a29SBALATON Zoltan         }
516*862b4a29SBALATON Zoltan         break;
517*862b4a29SBALATON Zoltan     case PALETTE_DATA ... PALETTE_DATA + 3:
518*862b4a29SBALATON Zoltan         data <<= addr - PALETTE_DATA;
519*862b4a29SBALATON Zoltan         data = bswap32(data) >> 8;
520*862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
521*862b4a29SBALATON Zoltan         data >>= 8;
522*862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
523*862b4a29SBALATON Zoltan         data >>= 8;
524*862b4a29SBALATON Zoltan         vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
525*862b4a29SBALATON Zoltan         break;
526*862b4a29SBALATON Zoltan     case CRTC_H_TOTAL_DISP:
527*862b4a29SBALATON Zoltan         s->regs.crtc_h_total_disp = data & 0x07ff07ff;
528*862b4a29SBALATON Zoltan         break;
529*862b4a29SBALATON Zoltan     case CRTC_H_SYNC_STRT_WID:
530*862b4a29SBALATON Zoltan         s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
531*862b4a29SBALATON Zoltan         break;
532*862b4a29SBALATON Zoltan     case CRTC_V_TOTAL_DISP:
533*862b4a29SBALATON Zoltan         s->regs.crtc_v_total_disp = data & 0x0fff0fff;
534*862b4a29SBALATON Zoltan         break;
535*862b4a29SBALATON Zoltan     case CRTC_V_SYNC_STRT_WID:
536*862b4a29SBALATON Zoltan         s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
537*862b4a29SBALATON Zoltan         break;
538*862b4a29SBALATON Zoltan     case CRTC_OFFSET:
539*862b4a29SBALATON Zoltan         s->regs.crtc_offset = data & 0xc7ffffff;
540*862b4a29SBALATON Zoltan         break;
541*862b4a29SBALATON Zoltan     case CRTC_OFFSET_CNTL:
542*862b4a29SBALATON Zoltan         s->regs.crtc_offset_cntl = data; /* FIXME */
543*862b4a29SBALATON Zoltan         break;
544*862b4a29SBALATON Zoltan     case CRTC_PITCH:
545*862b4a29SBALATON Zoltan         s->regs.crtc_pitch = data & 0x07ff07ff;
546*862b4a29SBALATON Zoltan         break;
547*862b4a29SBALATON Zoltan     case 0xf00 ... 0xfff:
548*862b4a29SBALATON Zoltan         /* read-only copy of PCI config space so ignore writes */
549*862b4a29SBALATON Zoltan         break;
550*862b4a29SBALATON Zoltan     case CUR_OFFSET:
551*862b4a29SBALATON Zoltan         if (s->regs.cur_offset != (data & 0x87fffff0)) {
552*862b4a29SBALATON Zoltan             s->regs.cur_offset = data & 0x87fffff0;
553*862b4a29SBALATON Zoltan             ati_cursor_define(s);
554*862b4a29SBALATON Zoltan         }
555*862b4a29SBALATON Zoltan         break;
556*862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_POSN:
557*862b4a29SBALATON Zoltan         s->regs.cur_hv_pos = data & 0x3fff0fff;
558*862b4a29SBALATON Zoltan         if (data & BIT(31)) {
559*862b4a29SBALATON Zoltan             s->regs.cur_offset |= data & BIT(31);
560*862b4a29SBALATON Zoltan         } else if (s->regs.cur_offset & BIT(31)) {
561*862b4a29SBALATON Zoltan             s->regs.cur_offset &= ~BIT(31);
562*862b4a29SBALATON Zoltan             ati_cursor_define(s);
563*862b4a29SBALATON Zoltan         }
564*862b4a29SBALATON Zoltan         if (!s->cursor_guest_mode &&
565*862b4a29SBALATON Zoltan             (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(data & BIT(31))) {
566*862b4a29SBALATON Zoltan             dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
567*862b4a29SBALATON Zoltan                           s->regs.cur_hv_pos & 0xffff, 1);
568*862b4a29SBALATON Zoltan         }
569*862b4a29SBALATON Zoltan         break;
570*862b4a29SBALATON Zoltan     case CUR_HORZ_VERT_OFF:
571*862b4a29SBALATON Zoltan         s->regs.cur_hv_offs = data & 0x3f003f;
572*862b4a29SBALATON Zoltan         if (data & BIT(31)) {
573*862b4a29SBALATON Zoltan             s->regs.cur_offset |= data & BIT(31);
574*862b4a29SBALATON Zoltan         } else if (s->regs.cur_offset & BIT(31)) {
575*862b4a29SBALATON Zoltan             s->regs.cur_offset &= ~BIT(31);
576*862b4a29SBALATON Zoltan             ati_cursor_define(s);
577*862b4a29SBALATON Zoltan         }
578*862b4a29SBALATON Zoltan         break;
579*862b4a29SBALATON Zoltan     case CUR_CLR0:
580*862b4a29SBALATON Zoltan         if (s->regs.cur_color0 != (data & 0xffffff)) {
581*862b4a29SBALATON Zoltan             s->regs.cur_color0 = data & 0xffffff;
582*862b4a29SBALATON Zoltan             ati_cursor_define(s);
583*862b4a29SBALATON Zoltan         }
584*862b4a29SBALATON Zoltan         break;
585*862b4a29SBALATON Zoltan     case CUR_CLR1:
586*862b4a29SBALATON Zoltan         /*
587*862b4a29SBALATON Zoltan          * Update cursor unconditionally here because some clients set up
588*862b4a29SBALATON Zoltan          * other registers before actually writing cursor data to memory at
589*862b4a29SBALATON Zoltan          * offset so we would miss cursor change unless always updating here
590*862b4a29SBALATON Zoltan          */
591*862b4a29SBALATON Zoltan         s->regs.cur_color1 = data & 0xffffff;
592*862b4a29SBALATON Zoltan         ati_cursor_define(s);
593*862b4a29SBALATON Zoltan         break;
594*862b4a29SBALATON Zoltan     case DST_OFFSET:
595*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
596*862b4a29SBALATON Zoltan             s->regs.dst_offset = data & 0xfffffff0;
597*862b4a29SBALATON Zoltan         } else {
598*862b4a29SBALATON Zoltan             s->regs.dst_offset = data & 0xfffffc00;
599*862b4a29SBALATON Zoltan         }
600*862b4a29SBALATON Zoltan         break;
601*862b4a29SBALATON Zoltan     case DST_PITCH:
602*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
603*862b4a29SBALATON Zoltan             s->regs.dst_pitch = data & 0x3fff;
604*862b4a29SBALATON Zoltan             s->regs.dst_tile = (data >> 16) & 1;
605*862b4a29SBALATON Zoltan         } else {
606*862b4a29SBALATON Zoltan             s->regs.dst_pitch = data & 0x3ff0;
607*862b4a29SBALATON Zoltan         }
608*862b4a29SBALATON Zoltan         break;
609*862b4a29SBALATON Zoltan     case DST_TILE:
610*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
611*862b4a29SBALATON Zoltan             s->regs.dst_tile = data & 3;
612*862b4a29SBALATON Zoltan         }
613*862b4a29SBALATON Zoltan         break;
614*862b4a29SBALATON Zoltan     case DST_WIDTH:
615*862b4a29SBALATON Zoltan         s->regs.dst_width = data & 0x3fff;
616*862b4a29SBALATON Zoltan         ati_2d_blt(s);
617*862b4a29SBALATON Zoltan         break;
618*862b4a29SBALATON Zoltan     case DST_HEIGHT:
619*862b4a29SBALATON Zoltan         s->regs.dst_height = data & 0x3fff;
620*862b4a29SBALATON Zoltan         break;
621*862b4a29SBALATON Zoltan     case SRC_X:
622*862b4a29SBALATON Zoltan         s->regs.src_x = data & 0x3fff;
623*862b4a29SBALATON Zoltan         break;
624*862b4a29SBALATON Zoltan     case SRC_Y:
625*862b4a29SBALATON Zoltan         s->regs.src_y = data & 0x3fff;
626*862b4a29SBALATON Zoltan         break;
627*862b4a29SBALATON Zoltan     case DST_X:
628*862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
629*862b4a29SBALATON Zoltan         break;
630*862b4a29SBALATON Zoltan     case DST_Y:
631*862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
632*862b4a29SBALATON Zoltan         break;
633*862b4a29SBALATON Zoltan     case SRC_PITCH_OFFSET:
634*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
635*862b4a29SBALATON Zoltan             s->regs.src_offset = (data & 0x1fffff) << 5;
636*862b4a29SBALATON Zoltan             s->regs.src_pitch = (data >> 21) & 0x3ff;
637*862b4a29SBALATON Zoltan             s->regs.src_tile = data >> 31;
638*862b4a29SBALATON Zoltan         } else {
639*862b4a29SBALATON Zoltan             s->regs.src_offset = (data & 0x3fffff) << 11;
640*862b4a29SBALATON Zoltan             s->regs.src_pitch = (data & 0x3fc00000) >> 16;
641*862b4a29SBALATON Zoltan             s->regs.src_tile = (data >> 30) & 1;
642*862b4a29SBALATON Zoltan         }
643*862b4a29SBALATON Zoltan         break;
644*862b4a29SBALATON Zoltan     case DST_PITCH_OFFSET:
645*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
646*862b4a29SBALATON Zoltan             s->regs.dst_offset = (data & 0x1fffff) << 5;
647*862b4a29SBALATON Zoltan             s->regs.dst_pitch = (data >> 21) & 0x3ff;
648*862b4a29SBALATON Zoltan             s->regs.dst_tile = data >> 31;
649*862b4a29SBALATON Zoltan         } else {
650*862b4a29SBALATON Zoltan             s->regs.dst_offset = (data & 0x3fffff) << 11;
651*862b4a29SBALATON Zoltan             s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
652*862b4a29SBALATON Zoltan             s->regs.dst_tile = data >> 30;
653*862b4a29SBALATON Zoltan         }
654*862b4a29SBALATON Zoltan         break;
655*862b4a29SBALATON Zoltan     case SRC_Y_X:
656*862b4a29SBALATON Zoltan         s->regs.src_x = data & 0x3fff;
657*862b4a29SBALATON Zoltan         s->regs.src_y = (data >> 16) & 0x3fff;
658*862b4a29SBALATON Zoltan         break;
659*862b4a29SBALATON Zoltan     case DST_Y_X:
660*862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
661*862b4a29SBALATON Zoltan         s->regs.dst_y = (data >> 16) & 0x3fff;
662*862b4a29SBALATON Zoltan         break;
663*862b4a29SBALATON Zoltan     case DST_HEIGHT_WIDTH:
664*862b4a29SBALATON Zoltan         s->regs.dst_width = data & 0x3fff;
665*862b4a29SBALATON Zoltan         s->regs.dst_height = (data >> 16) & 0x3fff;
666*862b4a29SBALATON Zoltan         ati_2d_blt(s);
667*862b4a29SBALATON Zoltan         break;
668*862b4a29SBALATON Zoltan     case DP_GUI_MASTER_CNTL:
669*862b4a29SBALATON Zoltan         s->regs.dp_gui_master_cntl = data & 0xf800000f;
670*862b4a29SBALATON Zoltan         s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
671*862b4a29SBALATON Zoltan                               (data & 0x4000) << 16;
672*862b4a29SBALATON Zoltan         s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
673*862b4a29SBALATON Zoltan         break;
674*862b4a29SBALATON Zoltan     case DST_WIDTH_X:
675*862b4a29SBALATON Zoltan         s->regs.dst_x = data & 0x3fff;
676*862b4a29SBALATON Zoltan         s->regs.dst_width = (data >> 16) & 0x3fff;
677*862b4a29SBALATON Zoltan         ati_2d_blt(s);
678*862b4a29SBALATON Zoltan         break;
679*862b4a29SBALATON Zoltan     case SRC_X_Y:
680*862b4a29SBALATON Zoltan         s->regs.src_y = data & 0x3fff;
681*862b4a29SBALATON Zoltan         s->regs.src_x = (data >> 16) & 0x3fff;
682*862b4a29SBALATON Zoltan         break;
683*862b4a29SBALATON Zoltan     case DST_X_Y:
684*862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
685*862b4a29SBALATON Zoltan         s->regs.dst_x = (data >> 16) & 0x3fff;
686*862b4a29SBALATON Zoltan         break;
687*862b4a29SBALATON Zoltan     case DST_WIDTH_HEIGHT:
688*862b4a29SBALATON Zoltan         s->regs.dst_height = data & 0x3fff;
689*862b4a29SBALATON Zoltan         s->regs.dst_width = (data >> 16) & 0x3fff;
690*862b4a29SBALATON Zoltan         ati_2d_blt(s);
691*862b4a29SBALATON Zoltan         break;
692*862b4a29SBALATON Zoltan     case DST_HEIGHT_Y:
693*862b4a29SBALATON Zoltan         s->regs.dst_y = data & 0x3fff;
694*862b4a29SBALATON Zoltan         s->regs.dst_height = (data >> 16) & 0x3fff;
695*862b4a29SBALATON Zoltan         break;
696*862b4a29SBALATON Zoltan     case SRC_OFFSET:
697*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
698*862b4a29SBALATON Zoltan             s->regs.src_offset = data & 0xfffffff0;
699*862b4a29SBALATON Zoltan         } else {
700*862b4a29SBALATON Zoltan             s->regs.src_offset = data & 0xfffffc00;
701*862b4a29SBALATON Zoltan         }
702*862b4a29SBALATON Zoltan         break;
703*862b4a29SBALATON Zoltan     case SRC_PITCH:
704*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
705*862b4a29SBALATON Zoltan             s->regs.src_pitch = data & 0x3fff;
706*862b4a29SBALATON Zoltan             s->regs.src_tile = (data >> 16) & 1;
707*862b4a29SBALATON Zoltan         } else {
708*862b4a29SBALATON Zoltan             s->regs.src_pitch = data & 0x3ff0;
709*862b4a29SBALATON Zoltan         }
710*862b4a29SBALATON Zoltan         break;
711*862b4a29SBALATON Zoltan     case DP_BRUSH_BKGD_CLR:
712*862b4a29SBALATON Zoltan         s->regs.dp_brush_bkgd_clr = data;
713*862b4a29SBALATON Zoltan         break;
714*862b4a29SBALATON Zoltan     case DP_BRUSH_FRGD_CLR:
715*862b4a29SBALATON Zoltan         s->regs.dp_brush_frgd_clr = data;
716*862b4a29SBALATON Zoltan         break;
717*862b4a29SBALATON Zoltan     case DP_CNTL:
718*862b4a29SBALATON Zoltan         s->regs.dp_cntl = data;
719*862b4a29SBALATON Zoltan         break;
720*862b4a29SBALATON Zoltan     case DP_DATATYPE:
721*862b4a29SBALATON Zoltan         s->regs.dp_datatype = data & 0xe0070f0f;
722*862b4a29SBALATON Zoltan         break;
723*862b4a29SBALATON Zoltan     case DP_MIX:
724*862b4a29SBALATON Zoltan         s->regs.dp_mix = data & 0x00ff0700;
725*862b4a29SBALATON Zoltan         break;
726*862b4a29SBALATON Zoltan     case DP_WRITE_MASK:
727*862b4a29SBALATON Zoltan         s->regs.dp_write_mask = data;
728*862b4a29SBALATON Zoltan         break;
729*862b4a29SBALATON Zoltan     case DEFAULT_OFFSET:
730*862b4a29SBALATON Zoltan         data &= (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF ?
731*862b4a29SBALATON Zoltan                  0x03fffc00 : 0xfffffc00);
732*862b4a29SBALATON Zoltan         s->regs.default_offset = data;
733*862b4a29SBALATON Zoltan         break;
734*862b4a29SBALATON Zoltan     case DEFAULT_PITCH:
735*862b4a29SBALATON Zoltan         if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
736*862b4a29SBALATON Zoltan             s->regs.default_pitch = data & 0x103ff;
737*862b4a29SBALATON Zoltan         }
738*862b4a29SBALATON Zoltan         break;
739*862b4a29SBALATON Zoltan     case DEFAULT_SC_BOTTOM_RIGHT:
740*862b4a29SBALATON Zoltan         s->regs.default_sc_bottom_right = data & 0x3fff3fff;
741*862b4a29SBALATON Zoltan         break;
742*862b4a29SBALATON Zoltan     default:
743*862b4a29SBALATON Zoltan         break;
744*862b4a29SBALATON Zoltan     }
745*862b4a29SBALATON Zoltan }
746*862b4a29SBALATON Zoltan 
747*862b4a29SBALATON Zoltan static const MemoryRegionOps ati_mm_ops = {
748*862b4a29SBALATON Zoltan     .read = ati_mm_read,
749*862b4a29SBALATON Zoltan     .write = ati_mm_write,
750*862b4a29SBALATON Zoltan     .endianness = DEVICE_LITTLE_ENDIAN,
751*862b4a29SBALATON Zoltan };
752*862b4a29SBALATON Zoltan 
753*862b4a29SBALATON Zoltan static void ati_vga_realize(PCIDevice *dev, Error **errp)
754*862b4a29SBALATON Zoltan {
755*862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
756*862b4a29SBALATON Zoltan     VGACommonState *vga = &s->vga;
757*862b4a29SBALATON Zoltan 
758*862b4a29SBALATON Zoltan     if (s->model) {
759*862b4a29SBALATON Zoltan         int i;
760*862b4a29SBALATON Zoltan         for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
761*862b4a29SBALATON Zoltan             if (!strcmp(s->model, ati_model_aliases[i].name)) {
762*862b4a29SBALATON Zoltan                 s->dev_id = ati_model_aliases[i].dev_id;
763*862b4a29SBALATON Zoltan                 break;
764*862b4a29SBALATON Zoltan             }
765*862b4a29SBALATON Zoltan         }
766*862b4a29SBALATON Zoltan         if (i >= ARRAY_SIZE(ati_model_aliases)) {
767*862b4a29SBALATON Zoltan             warn_report("Unknown ATI VGA model name, "
768*862b4a29SBALATON Zoltan                         "using default rage128p");
769*862b4a29SBALATON Zoltan         }
770*862b4a29SBALATON Zoltan     }
771*862b4a29SBALATON Zoltan     if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
772*862b4a29SBALATON Zoltan         s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
773*862b4a29SBALATON Zoltan         error_setg(errp, "Unknown ATI VGA device id, "
774*862b4a29SBALATON Zoltan                    "only 0x5046 and 0x5159 are supported");
775*862b4a29SBALATON Zoltan         return;
776*862b4a29SBALATON Zoltan     }
777*862b4a29SBALATON Zoltan     pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
778*862b4a29SBALATON Zoltan 
779*862b4a29SBALATON Zoltan     if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
780*862b4a29SBALATON Zoltan         s->vga.vram_size_mb < 16) {
781*862b4a29SBALATON Zoltan         warn_report("Too small video memory for device id");
782*862b4a29SBALATON Zoltan         s->vga.vram_size_mb = 16;
783*862b4a29SBALATON Zoltan     }
784*862b4a29SBALATON Zoltan 
785*862b4a29SBALATON Zoltan     /* init vga bits */
786*862b4a29SBALATON Zoltan     vga_common_init(vga, OBJECT(s));
787*862b4a29SBALATON Zoltan     vga_init(vga, OBJECT(s), pci_address_space(dev),
788*862b4a29SBALATON Zoltan              pci_address_space_io(dev), true);
789*862b4a29SBALATON Zoltan     vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga);
790*862b4a29SBALATON Zoltan     if (s->cursor_guest_mode) {
791*862b4a29SBALATON Zoltan         vga->cursor_invalidate = ati_cursor_invalidate;
792*862b4a29SBALATON Zoltan         vga->cursor_draw_line = ati_cursor_draw_line;
793*862b4a29SBALATON Zoltan     }
794*862b4a29SBALATON Zoltan 
795*862b4a29SBALATON Zoltan     /* mmio register space */
796*862b4a29SBALATON Zoltan     memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
797*862b4a29SBALATON Zoltan                           "ati.mmregs", 0x4000);
798*862b4a29SBALATON Zoltan     /* io space is alias to beginning of mmregs */
799*862b4a29SBALATON Zoltan     memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
800*862b4a29SBALATON Zoltan 
801*862b4a29SBALATON Zoltan     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
802*862b4a29SBALATON Zoltan     pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
803*862b4a29SBALATON Zoltan     pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
804*862b4a29SBALATON Zoltan }
805*862b4a29SBALATON Zoltan 
806*862b4a29SBALATON Zoltan static void ati_vga_reset(DeviceState *dev)
807*862b4a29SBALATON Zoltan {
808*862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
809*862b4a29SBALATON Zoltan 
810*862b4a29SBALATON Zoltan     /* reset vga */
811*862b4a29SBALATON Zoltan     vga_common_reset(&s->vga);
812*862b4a29SBALATON Zoltan     s->mode = VGA_MODE;
813*862b4a29SBALATON Zoltan }
814*862b4a29SBALATON Zoltan 
815*862b4a29SBALATON Zoltan static void ati_vga_exit(PCIDevice *dev)
816*862b4a29SBALATON Zoltan {
817*862b4a29SBALATON Zoltan     ATIVGAState *s = ATI_VGA(dev);
818*862b4a29SBALATON Zoltan 
819*862b4a29SBALATON Zoltan     graphic_console_close(s->vga.con);
820*862b4a29SBALATON Zoltan }
821*862b4a29SBALATON Zoltan 
822*862b4a29SBALATON Zoltan static Property ati_vga_properties[] = {
823*862b4a29SBALATON Zoltan     DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
824*862b4a29SBALATON Zoltan     DEFINE_PROP_STRING("model", ATIVGAState, model),
825*862b4a29SBALATON Zoltan     DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
826*862b4a29SBALATON Zoltan                        PCI_DEVICE_ID_ATI_RAGE128_PF),
827*862b4a29SBALATON Zoltan     DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
828*862b4a29SBALATON Zoltan     DEFINE_PROP_END_OF_LIST()
829*862b4a29SBALATON Zoltan };
830*862b4a29SBALATON Zoltan 
831*862b4a29SBALATON Zoltan static void ati_vga_class_init(ObjectClass *klass, void *data)
832*862b4a29SBALATON Zoltan {
833*862b4a29SBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
834*862b4a29SBALATON Zoltan     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
835*862b4a29SBALATON Zoltan 
836*862b4a29SBALATON Zoltan     dc->reset = ati_vga_reset;
837*862b4a29SBALATON Zoltan     dc->props = ati_vga_properties;
838*862b4a29SBALATON Zoltan     dc->hotpluggable = false;
839*862b4a29SBALATON Zoltan     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
840*862b4a29SBALATON Zoltan 
841*862b4a29SBALATON Zoltan     k->class_id = PCI_CLASS_DISPLAY_VGA;
842*862b4a29SBALATON Zoltan     k->vendor_id = PCI_VENDOR_ID_ATI;
843*862b4a29SBALATON Zoltan     k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
844*862b4a29SBALATON Zoltan     k->romfile = "vgabios-stdvga.bin";
845*862b4a29SBALATON Zoltan     k->realize = ati_vga_realize;
846*862b4a29SBALATON Zoltan     k->exit = ati_vga_exit;
847*862b4a29SBALATON Zoltan }
848*862b4a29SBALATON Zoltan 
849*862b4a29SBALATON Zoltan static const TypeInfo ati_vga_info = {
850*862b4a29SBALATON Zoltan     .name = TYPE_ATI_VGA,
851*862b4a29SBALATON Zoltan     .parent = TYPE_PCI_DEVICE,
852*862b4a29SBALATON Zoltan     .instance_size = sizeof(ATIVGAState),
853*862b4a29SBALATON Zoltan     .class_init = ati_vga_class_init,
854*862b4a29SBALATON Zoltan     .interfaces = (InterfaceInfo[]) {
855*862b4a29SBALATON Zoltan           { INTERFACE_CONVENTIONAL_PCI_DEVICE },
856*862b4a29SBALATON Zoltan           { },
857*862b4a29SBALATON Zoltan     },
858*862b4a29SBALATON Zoltan };
859*862b4a29SBALATON Zoltan 
860*862b4a29SBALATON Zoltan static void ati_vga_register_types(void)
861*862b4a29SBALATON Zoltan {
862*862b4a29SBALATON Zoltan     type_register_static(&ati_vga_info);
863*862b4a29SBALATON Zoltan }
864*862b4a29SBALATON Zoltan 
865*862b4a29SBALATON Zoltan type_init(ati_vga_register_types)
866