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 "vga-access.h"
23 #include "hw/qdev-properties.h"
26 #include "qemu/module.h"
27 #include "qemu/error-report.h"
28 #include "qapi/error.h"
29 #include "ui/console.h"
30 #include "hw/display/i2c-ddc.h"
33 #define ATI_DEBUG_HW_CURSOR 0
38 } ati_model_aliases[] = {
39 { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
40 { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
43 enum { VGA_MODE, EXT_MODE };
45 static void ati_vga_switch_mode(ATIVGAState *s)
48 s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
49 if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
50 /* Extended mode enabled */
52 if (s->regs.crtc_gen_cntl & CRTC2_EN) {
53 /* CRT controller enabled, use CRTC values */
54 /* FIXME Should these be the same as VGA CRTC regs? */
55 uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
56 int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
60 if (s->regs.crtc_h_total_disp == 0) {
61 s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
63 if (s->regs.crtc_v_total_disp == 0) {
64 s->regs.crtc_v_total_disp = (480 - 1) << 16;
66 h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
67 v = (s->regs.crtc_v_total_disp >> 16) + 1;
68 switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
69 case CRTC_PIX_WIDTH_4BPP:
72 case CRTC_PIX_WIDTH_8BPP:
75 case CRTC_PIX_WIDTH_15BPP:
78 case CRTC_PIX_WIDTH_16BPP:
81 case CRTC_PIX_WIDTH_24BPP:
84 case CRTC_PIX_WIDTH_32BPP:
88 qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
91 DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
92 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
93 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
94 s->vga.big_endian_fb = (s->regs.config_cntl & APER_0_ENDIAN ||
95 s->regs.config_cntl & APER_1_ENDIAN ?
97 /* reset VBE regs then set up mode */
98 s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
99 s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
100 s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
101 /* enable mode via ioport so it updates vga regs */
102 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
103 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
104 VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
105 (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
106 /* now set offset and stride after enable as that resets these */
108 int bypp = DIV_ROUND_UP(bpp, BITS_PER_BYTE);
110 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
111 vbe_ioport_write_data(&s->vga, 0, stride);
114 DPRINTF("CRTC offset is not multiple of pitch\n");
115 vbe_ioport_write_index(&s->vga, 0,
116 VBE_DISPI_INDEX_X_OFFSET);
117 vbe_ioport_write_data(&s->vga, 0, offs % stride / bypp);
119 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
120 vbe_ioport_write_data(&s->vga, 0, offs / stride);
121 DPRINTF("VBE offset (%d,%d), vbe_start_addr=%x\n",
122 s->vga.vbe_regs[VBE_DISPI_INDEX_X_OFFSET],
123 s->vga.vbe_regs[VBE_DISPI_INDEX_Y_OFFSET],
124 s->vga.vbe_start_addr);
128 /* VGA mode enabled */
130 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
131 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
135 /* Used by host side hardware cursor */
136 static void ati_cursor_define(ATIVGAState *s)
142 if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
143 return; /* Do not update cursor if locked or rendered by guest */
145 /* FIXME handle cur_hv_offs correctly */
146 srcoff = s->regs.cur_offset -
147 (s->regs.cur_hv_offs >> 16) - (s->regs.cur_hv_offs & 0xffff) * 16;
148 for (i = 0; i < 64; i++) {
149 for (j = 0; j < 8; j++, idx++) {
150 data[idx] = vga_read_byte(&s->vga, srcoff + i * 16 + j);
151 data[512 + idx] = vga_read_byte(&s->vga, srcoff + i * 16 + j + 8);
155 s->cursor = cursor_alloc(64, 64);
157 cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
158 &data[512], 1, &data[0]);
159 dpy_cursor_define(s->vga.con, s->cursor);
162 /* Alternatively support guest rendered hardware cursor */
163 static void ati_cursor_invalidate(VGACommonState *vga)
165 ATIVGAState *s = container_of(vga, ATIVGAState, vga);
166 int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
168 if (s->regs.cur_offset & BIT(31)) {
169 return; /* Do not update cursor if locked */
171 if (s->cursor_size != size ||
172 vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
173 vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
174 s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
175 (s->regs.cur_hv_offs & 0xffff) * 16) {
176 /* Remove old cursor then update and show new one if needed */
177 vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
178 vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
179 vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
180 s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
181 (s->regs.cur_hv_offs & 0xffff) * 16;
182 s->cursor_size = size;
184 vga_invalidate_scanlines(vga,
185 vga->hw_cursor_y, vga->hw_cursor_y + 63);
190 static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
192 ATIVGAState *s = container_of(vga, ATIVGAState, vga);
194 uint32_t *dp = (uint32_t *)d;
197 if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
198 scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
199 scr_y > s->regs.crtc_v_total_disp >> 16) {
202 /* FIXME handle cur_hv_offs correctly */
203 srcoff = s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
204 dp = &dp[vga->hw_cursor_x];
205 h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
206 for (i = 0; i < 8; i++) {
208 uint8_t abits = vga_read_byte(vga, srcoff + i);
209 uint8_t xbits = vga_read_byte(vga, srcoff + i + 8);
210 for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
211 if (abits & BIT(7)) {
212 if (xbits & BIT(7)) {
213 color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
215 continue; /* transparent, no change */
218 color = (xbits & BIT(7) ? s->regs.cur_color1 :
219 s->regs.cur_color0) | 0xff000000;
221 if (vga->hw_cursor_x + i * 8 + j >= h) {
222 return; /* end of screen, don't span to next line */
224 dp[i * 8 + j] = color;
229 static uint64_t ati_i2c(bitbang_i2c_interface *i2c, uint64_t data, int base)
231 bool c = (data & BIT(base + 17) ? !!(data & BIT(base + 1)) : 1);
232 bool d = (data & BIT(base + 16) ? !!(data & BIT(base)) : 1);
234 bitbang_i2c_set(i2c, BITBANG_I2C_SCL, c);
235 d = bitbang_i2c_set(i2c, BITBANG_I2C_SDA, d);
239 data |= BIT(base + 9);
242 data |= BIT(base + 8);
247 static void ati_vga_update_irq(ATIVGAState *s)
249 pci_set_irq(&s->dev, !!(s->regs.gen_int_status & s->regs.gen_int_cntl));
252 static void ati_vga_vblank_irq(void *opaque)
254 ATIVGAState *s = opaque;
256 timer_mod(&s->vblank_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
257 NANOSECONDS_PER_SECOND / 60);
258 s->regs.gen_int_status |= CRTC_VBLANK_INT;
259 ati_vga_update_irq(s);
262 static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
265 if (offs == 0 && size == 4) {
268 return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
272 static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
274 ATIVGAState *s = opaque;
279 val = s->regs.mm_index;
281 case MM_DATA ... MM_DATA + 3:
282 /* indexed access to regs or memory */
283 if (s->regs.mm_index & BIT(31)) {
284 uint32_t idx = s->regs.mm_index & ~BIT(31);
285 if (idx <= s->vga.vram_size - size) {
286 val = ldn_le_p(s->vga.vram_ptr + idx, size);
288 } else if (s->regs.mm_index > MM_DATA + 3) {
289 val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
291 qemu_log_mask(LOG_GUEST_ERROR,
292 "ati_mm_read: mm_index too small: %u\n", s->regs.mm_index);
295 case BIOS_0_SCRATCH ... BUS_CNTL - 1:
297 int i = (addr - BIOS_0_SCRATCH) / 4;
298 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
301 val = ati_reg_read_offs(s->regs.bios_scratch[i],
302 addr - (BIOS_0_SCRATCH + i * 4), size);
306 val = s->regs.gen_int_cntl;
309 val = s->regs.gen_int_status;
311 case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
312 val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
313 addr - CRTC_GEN_CNTL, size);
315 case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
316 val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
317 addr - CRTC_EXT_CNTL, size);
320 val = s->regs.dac_cntl;
323 val = s->regs.gpio_vga_ddc;
326 val = s->regs.gpio_dvi_ddc;
328 case GPIO_MONID ... GPIO_MONID + 3:
329 val = ati_reg_read_offs(s->regs.gpio_monid,
330 addr - GPIO_MONID, size);
333 /* FIXME unaligned access */
334 val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
335 val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
338 val = vga_ioport_read(&s->vga, VGA_PEL_D);
341 val = s->regs.config_cntl;
344 val = s->vga.vram_size;
346 case CONFIG_APER_0_BASE:
347 case CONFIG_APER_1_BASE:
348 val = pci_default_read_config(&s->dev,
349 PCI_BASE_ADDRESS_0, size) & 0xfffffff0;
351 case CONFIG_APER_SIZE:
352 val = s->vga.vram_size;
354 case CONFIG_REG_1_BASE:
355 val = pci_default_read_config(&s->dev,
356 PCI_BASE_ADDRESS_2, size) & 0xfffffff0;
358 case CONFIG_REG_APER_SIZE:
359 val = memory_region_size(&s->mm);
366 val = 64; /* free CMDFIFO entries */
368 case CRTC_H_TOTAL_DISP:
369 val = s->regs.crtc_h_total_disp;
371 case CRTC_H_SYNC_STRT_WID:
372 val = s->regs.crtc_h_sync_strt_wid;
374 case CRTC_V_TOTAL_DISP:
375 val = s->regs.crtc_v_total_disp;
377 case CRTC_V_SYNC_STRT_WID:
378 val = s->regs.crtc_v_sync_strt_wid;
381 val = s->regs.crtc_offset;
383 case CRTC_OFFSET_CNTL:
384 val = s->regs.crtc_offset_cntl;
387 val = s->regs.crtc_pitch;
389 case 0xf00 ... 0xfff:
390 val = pci_default_read_config(&s->dev, addr - 0xf00, size);
393 val = s->regs.cur_offset;
395 case CUR_HORZ_VERT_POSN:
396 val = s->regs.cur_hv_pos;
397 val |= s->regs.cur_offset & BIT(31);
399 case CUR_HORZ_VERT_OFF:
400 val = s->regs.cur_hv_offs;
401 val |= s->regs.cur_offset & BIT(31);
404 val = s->regs.cur_color0;
407 val = s->regs.cur_color1;
410 val = s->regs.dst_offset;
413 val = s->regs.dst_pitch;
414 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
415 val &= s->regs.dst_tile << 16;
419 val = s->regs.dst_width;
422 val = s->regs.dst_height;
436 case DP_GUI_MASTER_CNTL:
437 val = s->regs.dp_gui_master_cntl;
440 val = s->regs.src_offset;
443 val = s->regs.src_pitch;
444 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
445 val &= s->regs.src_tile << 16;
448 case DP_BRUSH_BKGD_CLR:
449 val = s->regs.dp_brush_bkgd_clr;
451 case DP_BRUSH_FRGD_CLR:
452 val = s->regs.dp_brush_frgd_clr;
454 case DP_SRC_FRGD_CLR:
455 val = s->regs.dp_src_frgd_clr;
457 case DP_SRC_BKGD_CLR:
458 val = s->regs.dp_src_bkgd_clr;
461 val = s->regs.dp_cntl;
464 val = s->regs.dp_datatype;
467 val = s->regs.dp_mix;
470 val = s->regs.dp_write_mask;
473 val = s->regs.default_offset;
474 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
476 val |= s->regs.default_pitch << 16;
477 val |= s->regs.default_tile << 30;
481 val = s->regs.default_pitch;
482 val |= s->regs.default_tile << 16;
484 case DEFAULT_SC_BOTTOM_RIGHT:
485 val = s->regs.default_sc_bottom_right;
490 if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
491 trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
496 static inline void ati_reg_write_offs(uint32_t *reg, int offs,
497 uint64_t data, unsigned int size)
499 if (offs == 0 && size == 4) {
502 *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
507 static void ati_mm_write(void *opaque, hwaddr addr,
508 uint64_t data, unsigned int size)
510 ATIVGAState *s = opaque;
512 if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
513 trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
517 s->regs.mm_index = data & ~3;
519 case MM_DATA ... MM_DATA + 3:
520 /* indexed access to regs or memory */
521 if (s->regs.mm_index & BIT(31)) {
522 uint32_t idx = s->regs.mm_index & ~BIT(31);
523 if (idx <= s->vga.vram_size - size) {
524 stn_le_p(s->vga.vram_ptr + idx, size, data);
526 } else if (s->regs.mm_index > MM_DATA + 3) {
527 ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
529 qemu_log_mask(LOG_GUEST_ERROR,
530 "ati_mm_write: mm_index too small: %u\n", s->regs.mm_index);
533 case BIOS_0_SCRATCH ... BUS_CNTL - 1:
535 int i = (addr - BIOS_0_SCRATCH) / 4;
536 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
539 ati_reg_write_offs(&s->regs.bios_scratch[i],
540 addr - (BIOS_0_SCRATCH + i * 4), data, size);
544 s->regs.gen_int_cntl = data;
545 if (data & CRTC_VBLANK_INT) {
546 ati_vga_vblank_irq(s);
548 timer_del(&s->vblank_timer);
549 ati_vga_update_irq(s);
553 data &= (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF ?
554 0x000f040fUL : 0xfc080effUL);
555 s->regs.gen_int_status &= ~data;
556 ati_vga_update_irq(s);
558 case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
560 uint32_t val = s->regs.crtc_gen_cntl;
561 ati_reg_write_offs(&s->regs.crtc_gen_cntl,
562 addr - CRTC_GEN_CNTL, data, size);
563 if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
564 if (s->cursor_guest_mode) {
565 s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
567 if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
568 ati_cursor_define(s);
570 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
571 s->regs.cur_hv_pos & 0xffff,
572 (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
575 if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
576 (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
577 ati_vga_switch_mode(s);
581 case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
583 uint32_t val = s->regs.crtc_ext_cntl;
584 ati_reg_write_offs(&s->regs.crtc_ext_cntl,
585 addr - CRTC_EXT_CNTL, data, size);
586 if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
587 DPRINTF("Display disabled\n");
588 s->vga.ar_index &= ~BIT(5);
590 DPRINTF("Display enabled\n");
591 s->vga.ar_index |= BIT(5);
592 ati_vga_switch_mode(s);
594 if ((val & CRT_CRTC_DISPLAY_DIS) !=
595 (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
596 ati_vga_switch_mode(s);
601 s->regs.dac_cntl = data & 0xffffe3ff;
602 s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
605 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
606 /* FIXME: Maybe add a property to select VGA or DVI port? */
610 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF) {
611 s->regs.gpio_dvi_ddc = ati_i2c(&s->bbi2c, data, 0);
614 case GPIO_MONID ... GPIO_MONID + 3:
615 /* FIXME What does Radeon have here? */
616 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
617 ati_reg_write_offs(&s->regs.gpio_monid,
618 addr - GPIO_MONID, data, size);
620 * Rage128p accesses DDC used to get EDID via these bits.
621 * Because some drivers access this via multiple byte writes
622 * we have to be careful when we send bits to avoid spurious
623 * changes in bitbang_i2c state. So only do it when mask is set
624 * and either the enable bits are changed or output bits changed
627 if ((s->regs.gpio_monid & BIT(25)) &&
628 ((addr <= GPIO_MONID + 2 && addr + size > GPIO_MONID + 2) ||
629 (addr == GPIO_MONID && (s->regs.gpio_monid & 0x60000)))) {
630 s->regs.gpio_monid = ati_i2c(&s->bbi2c, s->regs.gpio_monid, 1);
634 case PALETTE_INDEX ... PALETTE_INDEX + 3:
636 vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
637 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
639 if (addr == PALETTE_INDEX) {
640 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
642 vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
646 case PALETTE_DATA ... PALETTE_DATA + 3:
647 data <<= addr - PALETTE_DATA;
648 data = bswap32(data) >> 8;
649 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
651 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
653 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
656 s->regs.config_cntl = data;
658 case CRTC_H_TOTAL_DISP:
659 s->regs.crtc_h_total_disp = data & 0x07ff07ff;
661 case CRTC_H_SYNC_STRT_WID:
662 s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
664 case CRTC_V_TOTAL_DISP:
665 s->regs.crtc_v_total_disp = data & 0x0fff0fff;
667 case CRTC_V_SYNC_STRT_WID:
668 s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
671 s->regs.crtc_offset = data & 0xc7ffffff;
673 case CRTC_OFFSET_CNTL:
674 s->regs.crtc_offset_cntl = data; /* FIXME */
677 s->regs.crtc_pitch = data & 0x07ff07ff;
679 case 0xf00 ... 0xfff:
680 /* read-only copy of PCI config space so ignore writes */
683 if (s->regs.cur_offset != (data & 0x87fffff0)) {
684 s->regs.cur_offset = data & 0x87fffff0;
685 ati_cursor_define(s);
688 case CUR_HORZ_VERT_POSN:
689 s->regs.cur_hv_pos = data & 0x3fff0fff;
690 if (data & BIT(31)) {
691 s->regs.cur_offset |= data & BIT(31);
692 } else if (s->regs.cur_offset & BIT(31)) {
693 s->regs.cur_offset &= ~BIT(31);
694 ati_cursor_define(s);
696 if (!s->cursor_guest_mode &&
697 (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(data & BIT(31))) {
698 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
699 s->regs.cur_hv_pos & 0xffff, 1);
702 case CUR_HORZ_VERT_OFF:
703 s->regs.cur_hv_offs = data & 0x3f003f;
704 if (data & BIT(31)) {
705 s->regs.cur_offset |= data & BIT(31);
706 } else if (s->regs.cur_offset & BIT(31)) {
707 s->regs.cur_offset &= ~BIT(31);
708 ati_cursor_define(s);
712 if (s->regs.cur_color0 != (data & 0xffffff)) {
713 s->regs.cur_color0 = data & 0xffffff;
714 ati_cursor_define(s);
719 * Update cursor unconditionally here because some clients set up
720 * other registers before actually writing cursor data to memory at
721 * offset so we would miss cursor change unless always updating here
723 s->regs.cur_color1 = data & 0xffffff;
724 ati_cursor_define(s);
727 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
728 s->regs.dst_offset = data & 0xfffffff0;
730 s->regs.dst_offset = data & 0xfffffc00;
734 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
735 s->regs.dst_pitch = data & 0x3fff;
736 s->regs.dst_tile = (data >> 16) & 1;
738 s->regs.dst_pitch = data & 0x3ff0;
742 if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
743 s->regs.dst_tile = data & 3;
747 s->regs.dst_width = data & 0x3fff;
751 s->regs.dst_height = data & 0x3fff;
754 s->regs.src_x = data & 0x3fff;
757 s->regs.src_y = data & 0x3fff;
760 s->regs.dst_x = data & 0x3fff;
763 s->regs.dst_y = data & 0x3fff;
765 case SRC_PITCH_OFFSET:
766 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
767 s->regs.src_offset = (data & 0x1fffff) << 5;
768 s->regs.src_pitch = (data & 0x7fe00000) >> 21;
769 s->regs.src_tile = data >> 31;
771 s->regs.src_offset = (data & 0x3fffff) << 10;
772 s->regs.src_pitch = (data & 0x3fc00000) >> 16;
773 s->regs.src_tile = (data >> 30) & 1;
776 case DST_PITCH_OFFSET:
777 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
778 s->regs.dst_offset = (data & 0x1fffff) << 5;
779 s->regs.dst_pitch = (data & 0x7fe00000) >> 21;
780 s->regs.dst_tile = data >> 31;
782 s->regs.dst_offset = (data & 0x3fffff) << 10;
783 s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
784 s->regs.dst_tile = data >> 30;
788 s->regs.src_x = data & 0x3fff;
789 s->regs.src_y = (data >> 16) & 0x3fff;
792 s->regs.dst_x = data & 0x3fff;
793 s->regs.dst_y = (data >> 16) & 0x3fff;
795 case DST_HEIGHT_WIDTH:
796 s->regs.dst_width = data & 0x3fff;
797 s->regs.dst_height = (data >> 16) & 0x3fff;
800 case DP_GUI_MASTER_CNTL:
801 s->regs.dp_gui_master_cntl = data & 0xf800000f;
802 s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
803 (data & 0x4000) << 16;
804 s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
807 s->regs.dst_x = data & 0x3fff;
808 s->regs.dst_width = (data >> 16) & 0x3fff;
812 s->regs.src_y = data & 0x3fff;
813 s->regs.src_x = (data >> 16) & 0x3fff;
816 s->regs.dst_y = data & 0x3fff;
817 s->regs.dst_x = (data >> 16) & 0x3fff;
819 case DST_WIDTH_HEIGHT:
820 s->regs.dst_height = data & 0x3fff;
821 s->regs.dst_width = (data >> 16) & 0x3fff;
825 s->regs.dst_y = data & 0x3fff;
826 s->regs.dst_height = (data >> 16) & 0x3fff;
829 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
830 s->regs.src_offset = data & 0xfffffff0;
832 s->regs.src_offset = data & 0xfffffc00;
836 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
837 s->regs.src_pitch = data & 0x3fff;
838 s->regs.src_tile = (data >> 16) & 1;
840 s->regs.src_pitch = data & 0x3ff0;
843 case DP_BRUSH_BKGD_CLR:
844 s->regs.dp_brush_bkgd_clr = data;
846 case DP_BRUSH_FRGD_CLR:
847 s->regs.dp_brush_frgd_clr = data;
850 s->regs.dp_cntl = data;
853 s->regs.dp_datatype = data & 0xe0070f0f;
856 s->regs.dp_mix = data & 0x00ff0700;
859 s->regs.dp_write_mask = data;
862 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
863 s->regs.default_offset = data & 0xfffffff0;
865 /* Radeon has DEFAULT_PITCH_OFFSET here like DST_PITCH_OFFSET */
866 s->regs.default_offset = (data & 0x3fffff) << 10;
867 s->regs.default_pitch = (data & 0x3fc00000) >> 16;
868 s->regs.default_tile = data >> 30;
872 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
873 s->regs.default_pitch = data & 0x3fff;
874 s->regs.default_tile = (data >> 16) & 1;
877 case DEFAULT_SC_BOTTOM_RIGHT:
878 s->regs.default_sc_bottom_right = data & 0x3fff3fff;
885 static const MemoryRegionOps ati_mm_ops = {
887 .write = ati_mm_write,
888 .endianness = DEVICE_LITTLE_ENDIAN,
891 static void ati_vga_realize(PCIDevice *dev, Error **errp)
893 ATIVGAState *s = ATI_VGA(dev);
894 VGACommonState *vga = &s->vga;
898 for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
899 if (!strcmp(s->model, ati_model_aliases[i].name)) {
900 s->dev_id = ati_model_aliases[i].dev_id;
904 if (i >= ARRAY_SIZE(ati_model_aliases)) {
905 warn_report("Unknown ATI VGA model name, "
906 "using default rage128p");
909 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
910 s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
911 error_setg(errp, "Unknown ATI VGA device id, "
912 "only 0x5046 and 0x5159 are supported");
915 pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
917 if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
918 s->vga.vram_size_mb < 16) {
919 warn_report("Too small video memory for device id");
920 s->vga.vram_size_mb = 16;
924 vga_common_init(vga, OBJECT(s));
925 vga_init(vga, OBJECT(s), pci_address_space(dev),
926 pci_address_space_io(dev), true);
927 vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga);
928 if (s->cursor_guest_mode) {
929 vga->cursor_invalidate = ati_cursor_invalidate;
930 vga->cursor_draw_line = ati_cursor_draw_line;
934 I2CBus *i2cbus = i2c_init_bus(DEVICE(s), "ati-vga.ddc");
935 bitbang_i2c_init(&s->bbi2c, i2cbus);
936 I2CSlave *i2cddc = I2C_SLAVE(qdev_new(TYPE_I2CDDC));
937 i2c_set_slave_address(i2cddc, 0x50);
938 qdev_realize_and_unref(DEVICE(i2cddc), BUS(i2cbus), &error_abort);
940 /* mmio register space */
941 memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
942 "ati.mmregs", 0x4000);
943 /* io space is alias to beginning of mmregs */
944 memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
946 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
947 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
948 pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
950 /* most interrupts are not yet emulated but MacOS needs at least VBlank */
951 dev->config[PCI_INTERRUPT_PIN] = 1;
952 timer_init_ns(&s->vblank_timer, QEMU_CLOCK_VIRTUAL, ati_vga_vblank_irq, s);
955 static void ati_vga_reset(DeviceState *dev)
957 ATIVGAState *s = ATI_VGA(dev);
959 timer_del(&s->vblank_timer);
960 ati_vga_update_irq(s);
963 vga_common_reset(&s->vga);
967 static void ati_vga_exit(PCIDevice *dev)
969 ATIVGAState *s = ATI_VGA(dev);
971 timer_del(&s->vblank_timer);
972 graphic_console_close(s->vga.con);
975 static Property ati_vga_properties[] = {
976 DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
977 DEFINE_PROP_STRING("model", ATIVGAState, model),
978 DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
979 PCI_DEVICE_ID_ATI_RAGE128_PF),
980 DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
981 DEFINE_PROP_END_OF_LIST()
984 static void ati_vga_class_init(ObjectClass *klass, void *data)
986 DeviceClass *dc = DEVICE_CLASS(klass);
987 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
989 dc->reset = ati_vga_reset;
990 device_class_set_props(dc, ati_vga_properties);
991 dc->hotpluggable = false;
992 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
994 k->class_id = PCI_CLASS_DISPLAY_VGA;
995 k->vendor_id = PCI_VENDOR_ID_ATI;
996 k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
997 k->romfile = "vgabios-ati.bin";
998 k->realize = ati_vga_realize;
999 k->exit = ati_vga_exit;
1002 static const TypeInfo ati_vga_info = {
1003 .name = TYPE_ATI_VGA,
1004 .parent = TYPE_PCI_DEVICE,
1005 .instance_size = sizeof(ATIVGAState),
1006 .class_init = ati_vga_class_init,
1007 .interfaces = (InterfaceInfo[]) {
1008 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1013 static void ati_vga_register_types(void)
1015 type_register_static(&ati_vga_info);
1018 type_init(ati_vga_register_types)