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)
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
26 #include "ui/console.h"
29 #define ATI_DEBUG_HW_CURSOR 0
34 } ati_model_aliases[] = {
35 { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF },
36 { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY },
39 enum { VGA_MODE, EXT_MODE };
41 static void ati_vga_switch_mode(ATIVGAState *s)
44 s->mode, !!(s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN));
45 if (s->regs.crtc_gen_cntl & CRTC2_EXT_DISP_EN) {
46 /* Extended mode enabled */
48 if (s->regs.crtc_gen_cntl & CRTC2_EN) {
49 /* CRT controller enabled, use CRTC values */
50 uint32_t offs = s->regs.crtc_offset & 0x07ffffff;
51 int stride = (s->regs.crtc_pitch & 0x7ff) * 8;
55 if (s->regs.crtc_h_total_disp == 0) {
56 s->regs.crtc_h_total_disp = ((640 / 8) - 1) << 16;
58 if (s->regs.crtc_v_total_disp == 0) {
59 s->regs.crtc_v_total_disp = (480 - 1) << 16;
61 h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
62 v = (s->regs.crtc_v_total_disp >> 16) + 1;
63 switch (s->regs.crtc_gen_cntl & CRTC_PIX_WIDTH_MASK) {
64 case CRTC_PIX_WIDTH_4BPP:
67 case CRTC_PIX_WIDTH_8BPP:
70 case CRTC_PIX_WIDTH_15BPP:
73 case CRTC_PIX_WIDTH_16BPP:
76 case CRTC_PIX_WIDTH_24BPP:
79 case CRTC_PIX_WIDTH_32BPP:
83 qemu_log_mask(LOG_UNIMP, "Unsupported bpp value\n");
86 DPRINTF("Switching to %dx%d %d %d @ %x\n", h, v, stride, bpp, offs);
87 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
88 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
89 /* reset VBE regs then set up mode */
90 s->vga.vbe_regs[VBE_DISPI_INDEX_XRES] = h;
91 s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] = v;
92 s->vga.vbe_regs[VBE_DISPI_INDEX_BPP] = bpp;
93 /* enable mode via ioport so it updates vga regs */
94 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
95 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_ENABLED |
96 VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM |
97 (s->regs.dac_cntl & DAC_8BIT_EN ? VBE_DISPI_8BIT_DAC : 0));
98 /* now set offset and stride after enable as that resets these */
100 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_VIRT_WIDTH);
101 vbe_ioport_write_data(&s->vga, 0, stride);
102 if (offs % stride == 0) {
103 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_Y_OFFSET);
104 vbe_ioport_write_data(&s->vga, 0, offs / stride);
106 /* FIXME what to do with this? */
107 error_report("VGA offset is not multiple of pitch, "
108 "expect bad picture");
113 /* VGA mode enabled */
115 vbe_ioport_write_index(&s->vga, 0, VBE_DISPI_INDEX_ENABLE);
116 vbe_ioport_write_data(&s->vga, 0, VBE_DISPI_DISABLED);
120 /* Used by host side hardware cursor */
121 static void ati_cursor_define(ATIVGAState *s)
127 if ((s->regs.cur_offset & BIT(31)) || s->cursor_guest_mode) {
128 return; /* Do not update cursor if locked or rendered by guest */
130 /* FIXME handle cur_hv_offs correctly */
131 src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
132 s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
133 (s->regs.cur_hv_offs & 0xffff) * 16;
134 for (i = 0; i < 64; i++) {
135 for (j = 0; j < 8; j++, idx++) {
136 data[idx] = src[i * 16 + j];
137 data[512 + idx] = src[i * 16 + j + 8];
141 s->cursor = cursor_alloc(64, 64);
143 cursor_set_mono(s->cursor, s->regs.cur_color1, s->regs.cur_color0,
144 &data[512], 1, &data[0]);
145 dpy_cursor_define(s->vga.con, s->cursor);
148 /* Alternatively support guest rendered hardware cursor */
149 static void ati_cursor_invalidate(VGACommonState *vga)
151 ATIVGAState *s = container_of(vga, ATIVGAState, vga);
152 int size = (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ? 64 : 0;
154 if (s->regs.cur_offset & BIT(31)) {
155 return; /* Do not update cursor if locked */
157 if (s->cursor_size != size ||
158 vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
159 vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
160 s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
161 (s->regs.cur_hv_offs & 0xffff) * 16) {
162 /* Remove old cursor then update and show new one if needed */
163 vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
164 vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
165 vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
166 s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
167 (s->regs.cur_hv_offs & 0xffff) * 16;
168 s->cursor_size = size;
170 vga_invalidate_scanlines(vga,
171 vga->hw_cursor_y, vga->hw_cursor_y + 63);
176 static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
178 ATIVGAState *s = container_of(vga, ATIVGAState, vga);
180 uint32_t *dp = (uint32_t *)d;
183 if (!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN) ||
184 scr_y < vga->hw_cursor_y || scr_y >= vga->hw_cursor_y + 64 ||
185 scr_y > s->regs.crtc_v_total_disp >> 16) {
188 /* FIXME handle cur_hv_offs correctly */
189 src = s->vga.vram_ptr + (s->regs.crtc_offset & 0x07ffffff) +
190 s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
191 dp = &dp[vga->hw_cursor_x];
192 h = ((s->regs.crtc_h_total_disp >> 16) + 1) * 8;
193 for (i = 0; i < 8; i++) {
195 uint8_t abits = src[i];
196 uint8_t xbits = src[i + 8];
197 for (j = 0; j < 8; j++, abits <<= 1, xbits <<= 1) {
198 if (abits & BIT(7)) {
199 if (xbits & BIT(7)) {
200 color = dp[i * 8 + j] ^ 0xffffffff; /* complement */
202 continue; /* transparent, no change */
205 color = (xbits & BIT(7) ? s->regs.cur_color1 :
206 s->regs.cur_color0) << 8 | 0xff;
208 if (vga->hw_cursor_x + i * 8 + j >= h) {
209 return; /* end of screen, don't span to next line */
211 dp[i * 8 + j] = color;
216 static inline uint64_t ati_reg_read_offs(uint32_t reg, int offs,
219 if (offs == 0 && size == 4) {
222 return extract32(reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE);
226 static uint64_t ati_mm_read(void *opaque, hwaddr addr, unsigned int size)
228 ATIVGAState *s = opaque;
233 val = s->regs.mm_index;
235 case MM_DATA ... MM_DATA + 3:
236 /* indexed access to regs or memory */
237 if (s->regs.mm_index & BIT(31)) {
238 if (s->regs.mm_index <= s->vga.vram_size - size) {
242 val |= s->vga.vram_ptr[s->regs.mm_index + i--];
246 val = ati_mm_read(s, s->regs.mm_index + addr - MM_DATA, size);
249 case BIOS_0_SCRATCH ... BUS_CNTL - 1:
251 int i = (addr - BIOS_0_SCRATCH) / 4;
252 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
255 val = ati_reg_read_offs(s->regs.bios_scratch[i],
256 addr - (BIOS_0_SCRATCH + i * 4), size);
259 case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
260 val = ati_reg_read_offs(s->regs.crtc_gen_cntl,
261 addr - CRTC_GEN_CNTL, size);
263 case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
264 val = ati_reg_read_offs(s->regs.crtc_ext_cntl,
265 addr - CRTC_EXT_CNTL, size);
268 val = s->regs.dac_cntl;
270 /* case GPIO_MONID: FIXME hook up DDC I2C here */
272 /* FIXME unaligned access */
273 val = vga_ioport_read(&s->vga, VGA_PEL_IR) << 16;
274 val |= vga_ioport_read(&s->vga, VGA_PEL_IW) & 0xff;
277 val = vga_ioport_read(&s->vga, VGA_PEL_D);
280 val = s->vga.vram_size;
287 val = 64; /* free CMDFIFO entries */
289 case CRTC_H_TOTAL_DISP:
290 val = s->regs.crtc_h_total_disp;
292 case CRTC_H_SYNC_STRT_WID:
293 val = s->regs.crtc_h_sync_strt_wid;
295 case CRTC_V_TOTAL_DISP:
296 val = s->regs.crtc_v_total_disp;
298 case CRTC_V_SYNC_STRT_WID:
299 val = s->regs.crtc_v_sync_strt_wid;
302 val = s->regs.crtc_offset;
304 case CRTC_OFFSET_CNTL:
305 val = s->regs.crtc_offset_cntl;
308 val = s->regs.crtc_pitch;
310 case 0xf00 ... 0xfff:
311 val = pci_default_read_config(&s->dev, addr - 0xf00, size);
314 val = s->regs.cur_offset;
316 case CUR_HORZ_VERT_POSN:
317 val = s->regs.cur_hv_pos;
318 val |= s->regs.cur_offset & BIT(31);
320 case CUR_HORZ_VERT_OFF:
321 val = s->regs.cur_hv_offs;
322 val |= s->regs.cur_offset & BIT(31);
325 val = s->regs.cur_color0;
328 val = s->regs.cur_color1;
331 val = s->regs.dst_offset;
334 val = s->regs.dst_pitch;
335 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
336 val &= s->regs.dst_tile << 16;
340 val = s->regs.dst_width;
343 val = s->regs.dst_height;
357 case DP_GUI_MASTER_CNTL:
358 val = s->regs.dp_gui_master_cntl;
361 val = s->regs.src_offset;
364 val = s->regs.src_pitch;
365 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
366 val &= s->regs.src_tile << 16;
369 case DP_BRUSH_BKGD_CLR:
370 val = s->regs.dp_brush_bkgd_clr;
372 case DP_BRUSH_FRGD_CLR:
373 val = s->regs.dp_brush_frgd_clr;
375 case DP_SRC_FRGD_CLR:
376 val = s->regs.dp_src_frgd_clr;
378 case DP_SRC_BKGD_CLR:
379 val = s->regs.dp_src_bkgd_clr;
382 val = s->regs.dp_cntl;
385 val = s->regs.dp_datatype;
388 val = s->regs.dp_mix;
391 val = s->regs.dp_write_mask;
394 val = s->regs.default_offset;
397 val = s->regs.default_pitch;
399 case DEFAULT_SC_BOTTOM_RIGHT:
400 val = s->regs.default_sc_bottom_right;
405 if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
406 trace_ati_mm_read(size, addr, ati_reg_name(addr & ~3ULL), val);
411 static inline void ati_reg_write_offs(uint32_t *reg, int offs,
412 uint64_t data, unsigned int size)
414 if (offs == 0 && size == 4) {
417 *reg = deposit32(*reg, offs * BITS_PER_BYTE, size * BITS_PER_BYTE,
422 static void ati_mm_write(void *opaque, hwaddr addr,
423 uint64_t data, unsigned int size)
425 ATIVGAState *s = opaque;
427 if (addr < CUR_OFFSET || addr > CUR_CLR1 || ATI_DEBUG_HW_CURSOR) {
428 trace_ati_mm_write(size, addr, ati_reg_name(addr & ~3ULL), data);
432 s->regs.mm_index = data;
434 case MM_DATA ... MM_DATA + 3:
435 /* indexed access to regs or memory */
436 if (s->regs.mm_index & BIT(31)) {
437 if (s->regs.mm_index <= s->vga.vram_size - size) {
440 s->vga.vram_ptr[s->regs.mm_index + i] = data & 0xff;
445 ati_mm_write(s, s->regs.mm_index + addr - MM_DATA, data, size);
448 case BIOS_0_SCRATCH ... BUS_CNTL - 1:
450 int i = (addr - BIOS_0_SCRATCH) / 4;
451 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF && i > 3) {
454 ati_reg_write_offs(&s->regs.bios_scratch[i],
455 addr - (BIOS_0_SCRATCH + i * 4), data, size);
458 case CRTC_GEN_CNTL ... CRTC_GEN_CNTL + 3:
460 uint32_t val = s->regs.crtc_gen_cntl;
461 ati_reg_write_offs(&s->regs.crtc_gen_cntl,
462 addr - CRTC_GEN_CNTL, data, size);
463 if ((val & CRTC2_CUR_EN) != (s->regs.crtc_gen_cntl & CRTC2_CUR_EN)) {
464 if (s->cursor_guest_mode) {
465 s->vga.force_shadow = !!(s->regs.crtc_gen_cntl & CRTC2_CUR_EN);
467 if (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) {
468 ati_cursor_define(s);
470 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
471 s->regs.cur_hv_pos & 0xffff,
472 (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) != 0);
475 if ((val & (CRTC2_EXT_DISP_EN | CRTC2_EN)) !=
476 (s->regs.crtc_gen_cntl & (CRTC2_EXT_DISP_EN | CRTC2_EN))) {
477 ati_vga_switch_mode(s);
481 case CRTC_EXT_CNTL ... CRTC_EXT_CNTL + 3:
483 uint32_t val = s->regs.crtc_ext_cntl;
484 ati_reg_write_offs(&s->regs.crtc_ext_cntl,
485 addr - CRTC_EXT_CNTL, data, size);
486 if (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS) {
487 DPRINTF("Display disabled\n");
488 s->vga.ar_index &= ~BIT(5);
490 DPRINTF("Display enabled\n");
491 s->vga.ar_index |= BIT(5);
492 ati_vga_switch_mode(s);
494 if ((val & CRT_CRTC_DISPLAY_DIS) !=
495 (s->regs.crtc_ext_cntl & CRT_CRTC_DISPLAY_DIS)) {
496 ati_vga_switch_mode(s);
501 s->regs.dac_cntl = data & 0xffffe3ff;
502 s->vga.dac_8bit = !!(data & DAC_8BIT_EN);
504 /* case GPIO_MONID: FIXME hook up DDC I2C here */
505 case PALETTE_INDEX ... PALETTE_INDEX + 3:
507 vga_ioport_write(&s->vga, VGA_PEL_IR, (data >> 16) & 0xff);
508 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
510 if (addr == PALETTE_INDEX) {
511 vga_ioport_write(&s->vga, VGA_PEL_IW, data & 0xff);
513 vga_ioport_write(&s->vga, VGA_PEL_IR, data & 0xff);
517 case PALETTE_DATA ... PALETTE_DATA + 3:
518 data <<= addr - PALETTE_DATA;
519 data = bswap32(data) >> 8;
520 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
522 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
524 vga_ioport_write(&s->vga, VGA_PEL_D, data & 0xff);
526 case CRTC_H_TOTAL_DISP:
527 s->regs.crtc_h_total_disp = data & 0x07ff07ff;
529 case CRTC_H_SYNC_STRT_WID:
530 s->regs.crtc_h_sync_strt_wid = data & 0x17bf1fff;
532 case CRTC_V_TOTAL_DISP:
533 s->regs.crtc_v_total_disp = data & 0x0fff0fff;
535 case CRTC_V_SYNC_STRT_WID:
536 s->regs.crtc_v_sync_strt_wid = data & 0x9f0fff;
539 s->regs.crtc_offset = data & 0xc7ffffff;
541 case CRTC_OFFSET_CNTL:
542 s->regs.crtc_offset_cntl = data; /* FIXME */
545 s->regs.crtc_pitch = data & 0x07ff07ff;
547 case 0xf00 ... 0xfff:
548 /* read-only copy of PCI config space so ignore writes */
551 if (s->regs.cur_offset != (data & 0x87fffff0)) {
552 s->regs.cur_offset = data & 0x87fffff0;
553 ati_cursor_define(s);
556 case CUR_HORZ_VERT_POSN:
557 s->regs.cur_hv_pos = data & 0x3fff0fff;
558 if (data & BIT(31)) {
559 s->regs.cur_offset |= data & BIT(31);
560 } else if (s->regs.cur_offset & BIT(31)) {
561 s->regs.cur_offset &= ~BIT(31);
562 ati_cursor_define(s);
564 if (!s->cursor_guest_mode &&
565 (s->regs.crtc_gen_cntl & CRTC2_CUR_EN) && !(data & BIT(31))) {
566 dpy_mouse_set(s->vga.con, s->regs.cur_hv_pos >> 16,
567 s->regs.cur_hv_pos & 0xffff, 1);
570 case CUR_HORZ_VERT_OFF:
571 s->regs.cur_hv_offs = data & 0x3f003f;
572 if (data & BIT(31)) {
573 s->regs.cur_offset |= data & BIT(31);
574 } else if (s->regs.cur_offset & BIT(31)) {
575 s->regs.cur_offset &= ~BIT(31);
576 ati_cursor_define(s);
580 if (s->regs.cur_color0 != (data & 0xffffff)) {
581 s->regs.cur_color0 = data & 0xffffff;
582 ati_cursor_define(s);
587 * Update cursor unconditionally here because some clients set up
588 * other registers before actually writing cursor data to memory at
589 * offset so we would miss cursor change unless always updating here
591 s->regs.cur_color1 = data & 0xffffff;
592 ati_cursor_define(s);
595 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
596 s->regs.dst_offset = data & 0xfffffff0;
598 s->regs.dst_offset = data & 0xfffffc00;
602 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
603 s->regs.dst_pitch = data & 0x3fff;
604 s->regs.dst_tile = (data >> 16) & 1;
606 s->regs.dst_pitch = data & 0x3ff0;
610 if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY) {
611 s->regs.dst_tile = data & 3;
615 s->regs.dst_width = data & 0x3fff;
619 s->regs.dst_height = data & 0x3fff;
622 s->regs.src_x = data & 0x3fff;
625 s->regs.src_y = data & 0x3fff;
628 s->regs.dst_x = data & 0x3fff;
631 s->regs.dst_y = data & 0x3fff;
633 case SRC_PITCH_OFFSET:
634 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
635 s->regs.src_offset = (data & 0x1fffff) << 5;
636 s->regs.src_pitch = (data >> 21) & 0x3ff;
637 s->regs.src_tile = data >> 31;
639 s->regs.src_offset = (data & 0x3fffff) << 11;
640 s->regs.src_pitch = (data & 0x3fc00000) >> 16;
641 s->regs.src_tile = (data >> 30) & 1;
644 case DST_PITCH_OFFSET:
645 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
646 s->regs.dst_offset = (data & 0x1fffff) << 5;
647 s->regs.dst_pitch = (data >> 21) & 0x3ff;
648 s->regs.dst_tile = data >> 31;
650 s->regs.dst_offset = (data & 0x3fffff) << 11;
651 s->regs.dst_pitch = (data & 0x3fc00000) >> 16;
652 s->regs.dst_tile = data >> 30;
656 s->regs.src_x = data & 0x3fff;
657 s->regs.src_y = (data >> 16) & 0x3fff;
660 s->regs.dst_x = data & 0x3fff;
661 s->regs.dst_y = (data >> 16) & 0x3fff;
663 case DST_HEIGHT_WIDTH:
664 s->regs.dst_width = data & 0x3fff;
665 s->regs.dst_height = (data >> 16) & 0x3fff;
668 case DP_GUI_MASTER_CNTL:
669 s->regs.dp_gui_master_cntl = data & 0xf800000f;
670 s->regs.dp_datatype = (data & 0x0f00) >> 8 | (data & 0x30f0) << 4 |
671 (data & 0x4000) << 16;
672 s->regs.dp_mix = (data & GMC_ROP3_MASK) | (data & 0x7000000) >> 16;
675 s->regs.dst_x = data & 0x3fff;
676 s->regs.dst_width = (data >> 16) & 0x3fff;
680 s->regs.src_y = data & 0x3fff;
681 s->regs.src_x = (data >> 16) & 0x3fff;
684 s->regs.dst_y = data & 0x3fff;
685 s->regs.dst_x = (data >> 16) & 0x3fff;
687 case DST_WIDTH_HEIGHT:
688 s->regs.dst_height = data & 0x3fff;
689 s->regs.dst_width = (data >> 16) & 0x3fff;
693 s->regs.dst_y = data & 0x3fff;
694 s->regs.dst_height = (data >> 16) & 0x3fff;
697 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
698 s->regs.src_offset = data & 0xfffffff0;
700 s->regs.src_offset = data & 0xfffffc00;
704 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
705 s->regs.src_pitch = data & 0x3fff;
706 s->regs.src_tile = (data >> 16) & 1;
708 s->regs.src_pitch = data & 0x3ff0;
711 case DP_BRUSH_BKGD_CLR:
712 s->regs.dp_brush_bkgd_clr = data;
714 case DP_BRUSH_FRGD_CLR:
715 s->regs.dp_brush_frgd_clr = data;
718 s->regs.dp_cntl = data;
721 s->regs.dp_datatype = data & 0xe0070f0f;
724 s->regs.dp_mix = data & 0x00ff0700;
727 s->regs.dp_write_mask = data;
730 data &= (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF ?
731 0x03fffc00 : 0xfffffc00);
732 s->regs.default_offset = data;
735 if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
736 s->regs.default_pitch = data & 0x103ff;
739 case DEFAULT_SC_BOTTOM_RIGHT:
740 s->regs.default_sc_bottom_right = data & 0x3fff3fff;
747 static const MemoryRegionOps ati_mm_ops = {
749 .write = ati_mm_write,
750 .endianness = DEVICE_LITTLE_ENDIAN,
753 static void ati_vga_realize(PCIDevice *dev, Error **errp)
755 ATIVGAState *s = ATI_VGA(dev);
756 VGACommonState *vga = &s->vga;
760 for (i = 0; i < ARRAY_SIZE(ati_model_aliases); i++) {
761 if (!strcmp(s->model, ati_model_aliases[i].name)) {
762 s->dev_id = ati_model_aliases[i].dev_id;
766 if (i >= ARRAY_SIZE(ati_model_aliases)) {
767 warn_report("Unknown ATI VGA model name, "
768 "using default rage128p");
771 if (s->dev_id != PCI_DEVICE_ID_ATI_RAGE128_PF &&
772 s->dev_id != PCI_DEVICE_ID_ATI_RADEON_QY) {
773 error_setg(errp, "Unknown ATI VGA device id, "
774 "only 0x5046 and 0x5159 are supported");
777 pci_set_word(dev->config + PCI_DEVICE_ID, s->dev_id);
779 if (s->dev_id == PCI_DEVICE_ID_ATI_RADEON_QY &&
780 s->vga.vram_size_mb < 16) {
781 warn_report("Too small video memory for device id");
782 s->vga.vram_size_mb = 16;
786 vga_common_init(vga, OBJECT(s));
787 vga_init(vga, OBJECT(s), pci_address_space(dev),
788 pci_address_space_io(dev), true);
789 vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga);
790 if (s->cursor_guest_mode) {
791 vga->cursor_invalidate = ati_cursor_invalidate;
792 vga->cursor_draw_line = ati_cursor_draw_line;
795 /* mmio register space */
796 memory_region_init_io(&s->mm, OBJECT(s), &ati_mm_ops, s,
797 "ati.mmregs", 0x4000);
798 /* io space is alias to beginning of mmregs */
799 memory_region_init_alias(&s->io, OBJECT(s), "ati.io", &s->mm, 0, 0x100);
801 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
802 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
803 pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mm);
806 static void ati_vga_reset(DeviceState *dev)
808 ATIVGAState *s = ATI_VGA(dev);
811 vga_common_reset(&s->vga);
815 static void ati_vga_exit(PCIDevice *dev)
817 ATIVGAState *s = ATI_VGA(dev);
819 graphic_console_close(s->vga.con);
822 static Property ati_vga_properties[] = {
823 DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState, vga.vram_size_mb, 16),
824 DEFINE_PROP_STRING("model", ATIVGAState, model),
825 DEFINE_PROP_UINT16("x-device-id", ATIVGAState, dev_id,
826 PCI_DEVICE_ID_ATI_RAGE128_PF),
827 DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState, cursor_guest_mode, false),
828 DEFINE_PROP_END_OF_LIST()
831 static void ati_vga_class_init(ObjectClass *klass, void *data)
833 DeviceClass *dc = DEVICE_CLASS(klass);
834 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
836 dc->reset = ati_vga_reset;
837 dc->props = ati_vga_properties;
838 dc->hotpluggable = false;
839 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
841 k->class_id = PCI_CLASS_DISPLAY_VGA;
842 k->vendor_id = PCI_VENDOR_ID_ATI;
843 k->device_id = PCI_DEVICE_ID_ATI_RAGE128_PF;
844 k->romfile = "vgabios-stdvga.bin";
845 k->realize = ati_vga_realize;
846 k->exit = ati_vga_exit;
849 static const TypeInfo ati_vga_info = {
850 .name = TYPE_ATI_VGA,
851 .parent = TYPE_PCI_DEVICE,
852 .instance_size = sizeof(ATIVGAState),
853 .class_init = ati_vga_class_init,
854 .interfaces = (InterfaceInfo[]) {
855 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
860 static void ati_vga_register_types(void)
862 type_register_static(&ati_vga_info);
865 type_init(ati_vga_register_types)