2 * QEMU ATI SVGA emulation
4 * Copyright (c) 2019 BALATON Zoltan
6 * This work is licensed under the GNU GPL license version 2 or later.
11 * This is very incomplete and only enough for Linux console and some
12 * unaccelerated X output at the moment.
13 * Currently it's little more than a frame buffer with minimal functions,
14 * other more advanced features of the hardware are yet to be implemented.
15 * We only aim for Rage 128 Pro (and some RV100) and 2D only at first,
16 * No 3D at all yet (maybe after 2D works, but feel free to improve it)
19 #include "qemu/osdep.h"
22 #include "hw/qdev-properties.h"
25 #include "qemu/module.h"
26 #include "qemu/error-report.h"
27 #include "qapi/error.h"
28 #include "ui/console.h"
29 #include "hw/display/i2c-ddc.h"
32 #define ATI_DEBUG_HW_CURSOR 0
37 } ati_model_aliases[] = {
38 { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
39 { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
42 enum { VGA_MODE, EXT_MODE };
44 static void ati_vga_switch_mode(ATIVGAState *s)
47 s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
48 if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
49 /* Extended mode enabled */
51 if (s->regs.crtc_gen_cntl & CRTC2_EN) {
52 /* CRT controller enabled, use CRTC values */
53 uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
54 int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
58 if (s->regs.crtc_h_total_disp == 0) {
59 s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
61 if (s->regs.crtc_v_total_disp == 0) {
62 s->regs.crtc_v_total_disp = (480 - 1) << 16;
64 h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
65 v = (s->regs.crtc_v_total_disp >> 16) + 1;
66 switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
67 case CRTC_PIX_WIDTH_4BPP:
70 case CRTC_PIX_WIDTH_8BPP:
73 case CRTC_PIX_WIDTH_15BPP:
76 case CRTC_PIX_WIDTH_16BPP:
79 case CRTC_PIX_WIDTH_24BPP:
82 case CRTC_PIX_WIDTH_32BPP:
86 qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
89 DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
90 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
91 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
92 s->vga.big_endian_fb = false;
93 /* reset VBE regs then set up mode */
94 s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
95 s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
96 s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
97 /* enable mode via ioport so it updates vga regs */
98 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
99 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
100 VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
101 (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
102 /* now set offset and stride after enable as that resets these */
104 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
105 vbe_ioport_write_data(&s->vga, 0, stride);
106 if (offs % stride == 0) {
107 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
108 vbe_ioport_write_data(&s->vga, 0, offs / stride);
110 /* FIXME what to do with this? */
111 error_report("VGA offset is not multiple of pitch, "
112 "expect bad picture");
117 /* VGA mode enabled */
119 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
120 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
124 /* Used by host side hardware cursor */
125 static void ati_cursor_define(ATIVGAState *s)
131 if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
132 return; /* Do not update cursor if locked or rendered by guest */
134 /* FIXME handle cur_hv_offs correctly */
135 src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
136 s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
137 (s->regs.cur_hv_offs & 0xffff) * 16;
138 for (i = 0; i < 64; i++) {
139 for (j = 0; j < 8; j++, idx++) {
140 data[idx] = src[i * 16 + j];
141 data[512 + idx] = src[i * 16 + j + 8];
145 s->cursor = cursor_alloc(64, 64);
147 cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
148 &data[512], 1, &data[0]);
149 dpy_cursor_define(s->vga.con, s->cursor);
152 /* Alternatively support guest rendered hardware cursor */
153 static void ati_cursor_invalidate(VGACommonState *vga)
155 ATIVGAState *s = container_of(vga, ATIVGAState, vga);
156 int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
158 if (s->regs.cur_offset & BIT(31)) {
159 return; /* Do not update cursor if locked */
161 if (s->cursor_size != size ||
162 vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
163 vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
164 s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
165 (s->regs.cur_hv_offs & 0xffff) * 16) {
166 /* Remove old cursor then update and show new one if needed */
167 vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
168 vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
169 vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
170 s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
171 (s->regs.cur_hv_offs & 0xffff) * 16;
172 s->cursor_size = size;
174 vga_invalidate_scanlines(vga,
175 vga->hw_cursor_y, vga->hw_cursor_y + 63);
180 static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
182 ATIVGAState *s = container_of(vga, ATIVGAState, vga);
184 uint32_t *dp = (uint32_t *)d;
187 if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
188 scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
189 scr_y > s->regs.crtc_v_total_disp >> 16) {
192 /* FIXME handle cur_hv_offs correctly */
193 src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
194 s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
195 dp = &dp[vga->hw_cursor_x];
196 h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
197 for (i = 0; i < 8; i++) {
199 uint8_t abits = src[i];
200 uint8_t xbits = src[i + 8];
201 for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
202 if (abits & BIT(7)) {
203 if (xbits & BIT(7)) {
204 color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
206 continue; /* transparent, no change */
209 color = (xbits & BIT(7) ? s->regs.cur_color1 :
210 s->regs.cur_color0) << 8 | 0xff;
212 if (vga->hw_cursor_x + i * 8 + j >= h) {
213 return; /* end of screen, don't span to next line */
215 dp[i * 8 + j] = color;
220 static uint64_t ati_i2c(bitbang_i2c_interface *i2c, uint64_t data, int base)
222 bool c = (data & BIT(base + 17) ? !!(data & BIT(base + 1)) : 1);
223 bool d = (data & BIT(base + 16) ? !!(data & BIT(base)) : 1);
225 bitbang_i2c_set(i2c, BITBANG_I2C_SCL, c);
226 d = bitbang_i2c_set(i2c, BITBANG_I2C_SDA, d);
230 data |= BIT(base + 9);
233 data |= BIT(base + 8);
238 static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
241 if (offs == 0 && size == 4) {
244 return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
248 static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
250 ATIVGAState *s = opaque;
255 val = s->regs.mm_index;
257 case MM_DATA ... MM_DATA + 3:
258 /* indexed access to regs or memory */
259 if (s->regs.mm_index & BIT(31)) {
260 uint32_t idx = s->regs.mm_index & ~BIT(31);
261 if (idx <= s->vga.vram_size - size) {
262 val = ldn_le_p(s->vga.vram_ptr + idx, size);
265 val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
268 case BIOS_0_SCRATCH ... BUS_CNTL - 1:
270 int i = (addr - BIOS_0_SCRATCH) / 4;
271 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
274 val = ati_reg_read_offs(s->regs.bios_scratch[i],
275 addr - (BIOS_0_SCRATCH + i * 4), size);
278 case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
279 val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
280 addr - CRTC_GEN_CNTL, size);
282 case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
283 val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
284 addr - CRTC_EXT_CNTL, size);
287 val = s->regs.dac_cntl;
290 val = s->regs.gpio_vga_ddc;
293 val = s->regs.gpio_dvi_ddc;
295 case GPIO_MONID ... GPIO_MONID + 3:
296 val = ati_reg_read_offs(s->regs.gpio_monid,
297 addr - GPIO_MONID, size);
300 /* FIXME unaligned access */
301 val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
302 val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
305 val = vga_ioport_read(&s->vga, VGA_PEL_D);
308 val = s->vga.vram_size;
315 val = 64; /* free CMDFIFO entries */
317 case CRTC_H_TOTAL_DISP:
318 val = s->regs.crtc_h_total_disp;
320 case CRTC_H_SYNC_STRT_WID:
321 val = s->regs.crtc_h_sync_strt_wid;
323 case CRTC_V_TOTAL_DISP:
324 val = s->regs.crtc_v_total_disp;
326 case CRTC_V_SYNC_STRT_WID:
327 val = s->regs.crtc_v_sync_strt_wid;
330 val = s->regs.crtc_offset;
332 case CRTC_OFFSET_CNTL:
333 val = s->regs.crtc_offset_cntl;
336 val = s->regs.crtc_pitch;
338 case 0xf00 ... 0xfff:
339 val = pci_default_read_config(&s->dev, addr - 0xf00, size);
342 val = s->regs.cur_offset;
344 case CUR_HORZ_VERT_POSN:
345 val = s->regs.cur_hv_pos;
346 val |= s->regs.cur_offset & BIT(31);
348 case CUR_HORZ_VERT_OFF:
349 val = s->regs.cur_hv_offs;
350 val |= s->regs.cur_offset & BIT(31);
353 val = s->regs.cur_color0;
356 val = s->regs.cur_color1;
359 val = s->regs.dst_offset;
362 val = s->regs.dst_pitch;
363 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
364 val &= s->regs.dst_tile << 16;
368 val = s->regs.dst_width;
371 val = s->regs.dst_height;
385 case DP_GUI_MASTER_CNTL:
386 val = s->regs.dp_gui_master_cntl;
389 val = s->regs.src_offset;
392 val = s->regs.src_pitch;
393 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
394 val &= s->regs.src_tile << 16;
397 case DP_BRUSH_BKGD_CLR:
398 val = s->regs.dp_brush_bkgd_clr;
400 case DP_BRUSH_FRGD_CLR:
401 val = s->regs.dp_brush_frgd_clr;
403 case DP_SRC_FRGD_CLR:
404 val = s->regs.dp_src_frgd_clr;
406 case DP_SRC_BKGD_CLR:
407 val = s->regs.dp_src_bkgd_clr;
410 val = s->regs.dp_cntl;
413 val = s->regs.dp_datatype;
416 val = s->regs.dp_mix;
419 val = s->regs.dp_write_mask;
422 val = s->regs.default_offset;
423 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
425 val |= s->regs.default_pitch << 16;
426 val |= s->regs.default_tile << 30;
430 val = s->regs.default_pitch;
431 val |= s->regs.default_tile << 16;
433 case DEFAULT_SC_BOTTOM_RIGHT:
434 val = s->regs.default_sc_bottom_right;
439 if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
440 trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
445 static inline void ati_reg_write_offs(uint32_t *reg, int offs,
446 uint64_t data, unsigned int size)
448 if (offs == 0 && size == 4) {
451 *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
456 static void ati_mm_write(void *opaque, hwaddr addr,
457 uint64_t data, unsigned int size)
459 ATIVGAState *s = opaque;
461 if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
462 trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
466 s->regs.mm_index = data;
468 case MM_DATA ... MM_DATA + 3:
469 /* indexed access to regs or memory */
470 if (s->regs.mm_index & BIT(31)) {
471 uint32_t idx = s->regs.mm_index & ~BIT(31);
472 if (idx <= s->vga.vram_size - size) {
473 stn_le_p(s->vga.vram_ptr + idx, size, data);
476 ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
479 case BIOS_0_SCRATCH ... BUS_CNTL - 1:
481 int i = (addr - BIOS_0_SCRATCH) / 4;
482 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
485 ati_reg_write_offs(&s->regs.bios_scratch[i],
486 addr - (BIOS_0_SCRATCH + i * 4), data, size);
489 case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
491 uint32_t val = s->regs.crtc_gen_cntl;
492 ati_reg_write_offs(&s->regs.crtc_gen_cntl,
493 addr - CRTC_GEN_CNTL, data, size);
494 if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
495 if (s->cursor_guest_mode) {
496 s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
498 if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
499 ati_cursor_define(s);
501 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
502 s->regs.cur_hv_pos & 0xffff,
503 (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
506 if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
507 (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
508 ati_vga_switch_mode(s);
512 case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
514 uint32_t val = s->regs.crtc_ext_cntl;
515 ati_reg_write_offs(&s->regs.crtc_ext_cntl,
516 addr - CRTC_EXT_CNTL, data, size);
517 if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
518 DPRINTF("Display disabled\n");
519 s->vga.ar_index &= ~BIT(5);
521 DPRINTF("Display enabled\n");
522 s->vga.ar_index |= BIT(5);
523 ati_vga_switch_mode(s);
525 if ((val & CRT_CRTC_DISPLAY_DIS) !=
526 (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
527 ati_vga_switch_mode(s);
532 s->regs.dac_cntl = data & 0xffffe3ff;
533 s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
536 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
537 /* FIXME: Maybe add a property to select VGA or DVI port? */
541 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
542 s->regs.gpio_dvi_ddc = ati_i2c(&s->bbi2c, data, 0);
545 case GPIO_MONID ... GPIO_MONID + 3:
546 /* FIXME What does Radeon have here? */
547 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
548 ati_reg_write_offs(&s->regs.gpio_monid,
549 addr - GPIO_MONID, data, size);
551 * Rage128p accesses DDC used to get EDID via these bits.
552 * Only touch i2c when write overlaps 3rd byte because some
553 * drivers access this reg via multiple partial writes and
554 * without this spurious bits would be sent.
556 if ((s->regs.gpio_monid & BIT(25)) &&
557 addr <= GPIO_MONID + 2 && addr + size > GPIO_MONID + 2) {
558 s->regs.gpio_monid = ati_i2c(&s->bbi2c, s->regs.gpio_monid, 1);
562 case PALETTE_INDEX ... PALETTE_INDEX + 3:
564 vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
565 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
567 if (addr == PALETTE_INDEX) {
568 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
570 vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
574 case PALETTE_DATA ... PALETTE_DATA + 3:
575 data <<= addr - PALETTE_DATA;
576 data = bswap32(data) >> 8;
577 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
579 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
581 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
583 case CRTC_H_TOTAL_DISP:
584 s->regs.crtc_h_total_disp = data & 0x07ff07ff;
586 case CRTC_H_SYNC_STRT_WID:
587 s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
589 case CRTC_V_TOTAL_DISP:
590 s->regs.crtc_v_total_disp = data & 0x0fff0fff;
592 case CRTC_V_SYNC_STRT_WID:
593 s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
596 s->regs.crtc_offset = data & 0xc7ffffff;
598 case CRTC_OFFSET_CNTL:
599 s->regs.crtc_offset_cntl = data; /* FIXME */
602 s->regs.crtc_pitch = data & 0x07ff07ff;
604 case 0xf00 ... 0xfff:
605 /* read-only copy of PCI config space so ignore writes */
608 if (s->regs.cur_offset != (data & 0x87fffff0)) {
609 s->regs.cur_offset = data & 0x87fffff0;
610 ati_cursor_define(s);
613 case CUR_HORZ_VERT_POSN:
614 s->regs.cur_hv_pos = data & 0x3fff0fff;
615 if (data & BIT(31)) {
616 s->regs.cur_offset |= data & BIT(31);
617 } else if (s->regs.cur_offset & BIT(31)) {
618 s->regs.cur_offset &= ~BIT(31);
619 ati_cursor_define(s);
621 if (!s->cursor_guest_mode &&
622 (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(data & BIT(31))) {
623 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
624 s->regs.cur_hv_pos & 0xffff, 1);
627 case CUR_HORZ_VERT_OFF:
628 s->regs.cur_hv_offs = data & 0x3f003f;
629 if (data & BIT(31)) {
630 s->regs.cur_offset |= data & BIT(31);
631 } else if (s->regs.cur_offset & BIT(31)) {
632 s->regs.cur_offset &= ~BIT(31);
633 ati_cursor_define(s);
637 if (s->regs.cur_color0 != (data & 0xffffff)) {
638 s->regs.cur_color0 = data & 0xffffff;
639 ati_cursor_define(s);
644 * Update cursor unconditionally here because some clients set up
645 * other registers before actually writing cursor data to memory at
646 * offset so we would miss cursor change unless always updating here
648 s->regs.cur_color1 = data & 0xffffff;
649 ati_cursor_define(s);
652 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
653 s->regs.dst_offset = data & 0xfffffff0;
655 s->regs.dst_offset = data & 0xfffffc00;
659 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
660 s->regs.dst_pitch = data & 0x3fff;
661 s->regs.dst_tile = (data >> 16) & 1;
663 s->regs.dst_pitch = data & 0x3ff0;
667 if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
668 s->regs.dst_tile = data & 3;
672 s->regs.dst_width = data & 0x3fff;
676 s->regs.dst_height = data & 0x3fff;
679 s->regs.src_x = data & 0x3fff;
682 s->regs.src_y = data & 0x3fff;
685 s->regs.dst_x = data & 0x3fff;
688 s->regs.dst_y = data & 0x3fff;
690 case SRC_PITCH_OFFSET:
691 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
692 s->regs.src_offset = (data & 0x1fffff) << 5;
693 s->regs.src_pitch = (data & 0x7fe00000) >> 21;
694 s->regs.src_tile = data >> 31;
696 s->regs.src_offset = (data & 0x3fffff) << 10;
697 s->regs.src_pitch = (data & 0x3fc00000) >> 16;
698 s->regs.src_tile = (data >> 30) & 1;
701 case DST_PITCH_OFFSET:
702 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
703 s->regs.dst_offset = (data & 0x1fffff) << 5;
704 s->regs.dst_pitch = (data & 0x7fe00000) >> 21;
705 s->regs.dst_tile = data >> 31;
707 s->regs.dst_offset = (data & 0x3fffff) << 10;
708 s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
709 s->regs.dst_tile = data >> 30;
713 s->regs.src_x = data & 0x3fff;
714 s->regs.src_y = (data >> 16) & 0x3fff;
717 s->regs.dst_x = data & 0x3fff;
718 s->regs.dst_y = (data >> 16) & 0x3fff;
720 case DST_HEIGHT_WIDTH:
721 s->regs.dst_width = data & 0x3fff;
722 s->regs.dst_height = (data >> 16) & 0x3fff;
725 case DP_GUI_MASTER_CNTL:
726 s->regs.dp_gui_master_cntl = data & 0xf800000f;
727 s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
728 (data & 0x4000) << 16;
729 s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
732 s->regs.dst_x = data & 0x3fff;
733 s->regs.dst_width = (data >> 16) & 0x3fff;
737 s->regs.src_y = data & 0x3fff;
738 s->regs.src_x = (data >> 16) & 0x3fff;
741 s->regs.dst_y = data & 0x3fff;
742 s->regs.dst_x = (data >> 16) & 0x3fff;
744 case DST_WIDTH_HEIGHT:
745 s->regs.dst_height = data & 0x3fff;
746 s->regs.dst_width = (data >> 16) & 0x3fff;
750 s->regs.dst_y = data & 0x3fff;
751 s->regs.dst_height = (data >> 16) & 0x3fff;
754 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
755 s->regs.src_offset = data & 0xfffffff0;
757 s->regs.src_offset = data & 0xfffffc00;
761 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
762 s->regs.src_pitch = data & 0x3fff;
763 s->regs.src_tile = (data >> 16) & 1;
765 s->regs.src_pitch = data & 0x3ff0;
768 case DP_BRUSH_BKGD_CLR:
769 s->regs.dp_brush_bkgd_clr = data;
771 case DP_BRUSH_FRGD_CLR:
772 s->regs.dp_brush_frgd_clr = data;
775 s->regs.dp_cntl = data;
778 s->regs.dp_datatype = data & 0xe0070f0f;
781 s->regs.dp_mix = data & 0x00ff0700;
784 s->regs.dp_write_mask = data;
787 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
788 s->regs.default_offset = data & 0xfffffff0;
790 /* Radeon has DEFAULT_PITCH_OFFSET here like DST_PITCH_OFFSET */
791 s->regs.default_offset = (data & 0x3fffff) << 10;
792 s->regs.default_pitch = (data & 0x3fc00000) >> 16;
793 s->regs.default_tile = data >> 30;
797 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
798 s->regs.default_pitch = data & 0x3fff;
799 s->regs.default_tile = (data >> 16) & 1;
802 case DEFAULT_SC_BOTTOM_RIGHT:
803 s->regs.default_sc_bottom_right = data & 0x3fff3fff;
810 static const MemoryRegionOps ati_mm_ops = {
812 .write = ati_mm_write,
813 .endianness = DEVICE_LITTLE_ENDIAN,
816 static void ati_vga_realize(PCIDevice *dev, Error **errp)
818 ATIVGAState *s = ATI_VGA(dev);
819 VGACommonState *vga = &s->vga;
823 for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
824 if (!strcmp(s->model, ati_model_aliases[i].name)) {
825 s->dev_id = ati_model_aliases[i].dev_id;
829 if (i >= ARRAY_SIZE(ati_model_aliases)) {
830 warn_report("Unknown ATI VGA model name, "
831 "using default rage128p");
834 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
835 s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
836 error_setg(errp, "Unknown ATI VGA device id, "
837 "only 0x5046 and 0x5159 are supported");
840 pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
842 if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
843 s->vga.vram_size_mb < 16) {
844 warn_report("Too small video memory for device id");
845 s->vga.vram_size_mb = 16;
849 vga_common_init(vga, OBJECT(s));
850 vga_init(vga, OBJECT(s), pci_address_space(dev),
851 pci_address_space_io(dev), true);
852 vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga);
853 if (s->cursor_guest_mode) {
854 vga->cursor_invalidate = ati_cursor_invalidate;
855 vga->cursor_draw_line = ati_cursor_draw_line;
859 I2CBus *i2cbus = i2c_init_bus(DEVICE(s), "ati-vga.ddc");
860 bitbang_i2c_init(&s->bbi2c, i2cbus);
861 I2CSlave *i2cddc = I2C_SLAVE(qdev_create(BUS(i2cbus), TYPE_I2CDDC));
862 i2c_set_slave_address(i2cddc, 0x50);
864 /* mmio register space */
865 memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
866 "ati.mmregs", 0x4000);
867 /* io space is alias to beginning of mmregs */
868 memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
870 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
871 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
872 pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
875 static void ati_vga_reset(DeviceState *dev)
877 ATIVGAState *s = ATI_VGA(dev);
880 vga_common_reset(&s->vga);
884 static void ati_vga_exit(PCIDevice *dev)
886 ATIVGAState *s = ATI_VGA(dev);
888 graphic_console_close(s->vga.con);
891 static Property ati_vga_properties[] = {
892 DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
893 DEFINE_PROP_STRING("model", ATIVGAState, model),
894 DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
895 PCI_DEVICE_ID_ATI_RAGE128_PF),
896 DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
897 DEFINE_PROP_END_OF_LIST()
900 static void ati_vga_class_init(ObjectClass *klass, void *data)
902 DeviceClass *dc = DEVICE_CLASS(klass);
903 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
905 dc->reset = ati_vga_reset;
906 dc->props = ati_vga_properties;
907 dc->hotpluggable = false;
908 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
910 k->class_id = PCI_CLASS_DISPLAY_VGA;
911 k->vendor_id = PCI_VENDOR_ID_ATI;
912 k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
913 k->romfile = "vgabios-ati.bin";
914 k->realize = ati_vga_realize;
915 k->exit = ati_vga_exit;
918 static const TypeInfo ati_vga_info = {
919 .name = TYPE_ATI_VGA,
920 .parent = TYPE_PCI_DEVICE,
921 .instance_size = sizeof(ATIVGAState),
922 .class_init = ati_vga_class_init,
923 .interfaces = (InterfaceInfo[]) {
924 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
929 static void ati_vga_register_types(void)
931 type_register_static(&ati_vga_info);
934 type_init(ati_vga_register_types)