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"
24 #include "qemu/module.h"
25 #include "qemu/error-report.h"
26 #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 /* reset VBE regs then set up mode */
93 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_XRES
] = h
;
94 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_YRES
] = v
;
95 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_BPP
] = bpp
;
96 /* enable mode via ioport so it updates vga regs */
97 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_ENABLE
);
98 vbe_ioport_write_data(&s
->vga
, 0, VBE_DISPI_ENABLED
|
99 VBE_DISPI_LFB_ENABLED
| VBE_DISPI_NOCLEARMEM
|
100 (s
->regs
.dac_cntl
& DAC_8BIT_EN
? VBE_DISPI_8BIT_DAC
: 0));
101 /* now set offset and stride after enable as that resets these */
103 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_VIRT_WIDTH
);
104 vbe_ioport_write_data(&s
->vga
, 0, stride
);
105 if (offs
% stride
== 0) {
106 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_Y_OFFSET
);
107 vbe_ioport_write_data(&s
->vga
, 0, offs
/ stride
);
109 /* FIXME what to do with this? */
110 error_report("VGA offset is not multiple of pitch, "
111 "expect bad picture");
116 /* VGA mode enabled */
118 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_ENABLE
);
119 vbe_ioport_write_data(&s
->vga
, 0, VBE_DISPI_DISABLED
);
123 /* Used by host side hardware cursor */
124 static void ati_cursor_define(ATIVGAState
*s
)
130 if ((s
->regs
.cur_offset
& BIT(31)) || s
->cursor_guest_mode
) {
131 return; /* Do not update cursor if locked or rendered by guest */
133 /* FIXME handle cur_hv_offs correctly */
134 src
= s
->vga
.vram_ptr
+ (s
->regs
.crtc_offset
& 0x07ffffff) +
135 s
->regs
.cur_offset
- (s
->regs
.cur_hv_offs
>> 16) -
136 (s
->regs
.cur_hv_offs
& 0xffff) * 16;
137 for (i
= 0; i
< 64; i
++) {
138 for (j
= 0; j
< 8; j
++, idx
++) {
139 data
[idx
] = src
[i
* 16 + j
];
140 data
[512 + idx
] = src
[i
* 16 + j
+ 8];
144 s
->cursor
= cursor_alloc(64, 64);
146 cursor_set_mono(s
->cursor
, s
->regs
.cur_color1
, s
->regs
.cur_color0
,
147 &data
[512], 1, &data
[0]);
148 dpy_cursor_define(s
->vga
.con
, s
->cursor
);
151 /* Alternatively support guest rendered hardware cursor */
152 static void ati_cursor_invalidate(VGACommonState
*vga
)
154 ATIVGAState
*s
= container_of(vga
, ATIVGAState
, vga
);
155 int size
= (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) ? 64 : 0;
157 if (s
->regs
.cur_offset
& BIT(31)) {
158 return; /* Do not update cursor if locked */
160 if (s
->cursor_size
!= size
||
161 vga
->hw_cursor_x
!= s
->regs
.cur_hv_pos
>> 16 ||
162 vga
->hw_cursor_y
!= (s
->regs
.cur_hv_pos
& 0xffff) ||
163 s
->cursor_offset
!= s
->regs
.cur_offset
- (s
->regs
.cur_hv_offs
>> 16) -
164 (s
->regs
.cur_hv_offs
& 0xffff) * 16) {
165 /* Remove old cursor then update and show new one if needed */
166 vga_invalidate_scanlines(vga
, vga
->hw_cursor_y
, vga
->hw_cursor_y
+ 63);
167 vga
->hw_cursor_x
= s
->regs
.cur_hv_pos
>> 16;
168 vga
->hw_cursor_y
= s
->regs
.cur_hv_pos
& 0xffff;
169 s
->cursor_offset
= s
->regs
.cur_offset
- (s
->regs
.cur_hv_offs
>> 16) -
170 (s
->regs
.cur_hv_offs
& 0xffff) * 16;
171 s
->cursor_size
= size
;
173 vga_invalidate_scanlines(vga
,
174 vga
->hw_cursor_y
, vga
->hw_cursor_y
+ 63);
179 static void ati_cursor_draw_line(VGACommonState
*vga
, uint8_t *d
, int scr_y
)
181 ATIVGAState
*s
= container_of(vga
, ATIVGAState
, vga
);
183 uint32_t *dp
= (uint32_t *)d
;
186 if (!(s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) ||
187 scr_y
< vga
->hw_cursor_y
|| scr_y
>= vga
->hw_cursor_y
+ 64 ||
188 scr_y
> s
->regs
.crtc_v_total_disp
>> 16) {
191 /* FIXME handle cur_hv_offs correctly */
192 src
= s
->vga
.vram_ptr
+ (s
->regs
.crtc_offset
& 0x07ffffff) +
193 s
->cursor_offset
+ (scr_y
- vga
->hw_cursor_y
) * 16;
194 dp
= &dp
[vga
->hw_cursor_x
];
195 h
= ((s
->regs
.crtc_h_total_disp
>> 16) + 1) * 8;
196 for (i
= 0; i
< 8; i
++) {
198 uint8_t abits
= src
[i
];
199 uint8_t xbits
= src
[i
+ 8];
200 for (j
= 0; j
< 8; j
++, abits
<<= 1, xbits
<<= 1) {
201 if (abits
& BIT(7)) {
202 if (xbits
& BIT(7)) {
203 color
= dp
[i
* 8 + j
] ^ 0xffffffff; /* complement */
205 continue; /* transparent, no change */
208 color
= (xbits
& BIT(7) ? s
->regs
.cur_color1
:
209 s
->regs
.cur_color0
) << 8 | 0xff;
211 if (vga
->hw_cursor_x
+ i
* 8 + j
>= h
) {
212 return; /* end of screen, don't span to next line */
214 dp
[i
* 8 + j
] = color
;
219 static uint64_t ati_i2c(bitbang_i2c_interface
*i2c
, uint64_t data
, int base
)
221 bool c
= (data
& BIT(base
+ 17) ? !!(data
& BIT(base
+ 1)) : 1);
222 bool d
= (data
& BIT(base
+ 16) ? !!(data
& BIT(base
)) : 1);
224 bitbang_i2c_set(i2c
, BITBANG_I2C_SCL
, c
);
225 d
= bitbang_i2c_set(i2c
, BITBANG_I2C_SDA
, d
);
229 data
|= BIT(base
+ 9);
232 data
|= BIT(base
+ 8);
237 static inline uint64_t ati_reg_read_offs(uint32_t reg
, int offs
,
240 if (offs
== 0 && size
== 4) {
243 return extract32(reg
, offs
* BITS_PER_BYTE
, size
* BITS_PER_BYTE
);
247 static uint64_t ati_mm_read(void *opaque
, hwaddr addr
, unsigned int size
)
249 ATIVGAState
*s
= opaque
;
254 val
= s
->regs
.mm_index
;
256 case MM_DATA
... MM_DATA
+ 3:
257 /* indexed access to regs or memory */
258 if (s
->regs
.mm_index
& BIT(31)) {
259 uint32_t idx
= s
->regs
.mm_index
& ~BIT(31);
260 if (idx
<= s
->vga
.vram_size
- size
) {
261 val
= ldn_le_p(s
->vga
.vram_ptr
+ idx
, size
);
264 val
= ati_mm_read(s
, s
->regs
.mm_index
+ addr
- MM_DATA
, size
);
267 case BIOS_0_SCRATCH
... BUS_CNTL
- 1:
269 int i
= (addr
- BIOS_0_SCRATCH
) / 4;
270 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
&& i
> 3) {
273 val
= ati_reg_read_offs(s
->regs
.bios_scratch
[i
],
274 addr
- (BIOS_0_SCRATCH
+ i
* 4), size
);
277 case CRTC_GEN_CNTL
... CRTC_GEN_CNTL
+ 3:
278 val
= ati_reg_read_offs(s
->regs
.crtc_gen_cntl
,
279 addr
- CRTC_GEN_CNTL
, size
);
281 case CRTC_EXT_CNTL
... CRTC_EXT_CNTL
+ 3:
282 val
= ati_reg_read_offs(s
->regs
.crtc_ext_cntl
,
283 addr
- CRTC_EXT_CNTL
, size
);
286 val
= s
->regs
.dac_cntl
;
289 val
= s
->regs
.gpio_vga_ddc
;
292 val
= s
->regs
.gpio_dvi_ddc
;
294 case GPIO_MONID
... GPIO_MONID
+ 3:
295 val
= ati_reg_read_offs(s
->regs
.gpio_monid
,
296 addr
- GPIO_MONID
, size
);
299 /* FIXME unaligned access */
300 val
= vga_ioport_read(&s
->vga
, VGA_PEL_IR
) << 16;
301 val
|= vga_ioport_read(&s
->vga
, VGA_PEL_IW
) & 0xff;
304 val
= vga_ioport_read(&s
->vga
, VGA_PEL_D
);
307 val
= s
->vga
.vram_size
;
314 val
= 64; /* free CMDFIFO entries */
316 case CRTC_H_TOTAL_DISP
:
317 val
= s
->regs
.crtc_h_total_disp
;
319 case CRTC_H_SYNC_STRT_WID
:
320 val
= s
->regs
.crtc_h_sync_strt_wid
;
322 case CRTC_V_TOTAL_DISP
:
323 val
= s
->regs
.crtc_v_total_disp
;
325 case CRTC_V_SYNC_STRT_WID
:
326 val
= s
->regs
.crtc_v_sync_strt_wid
;
329 val
= s
->regs
.crtc_offset
;
331 case CRTC_OFFSET_CNTL
:
332 val
= s
->regs
.crtc_offset_cntl
;
335 val
= s
->regs
.crtc_pitch
;
337 case 0xf00 ... 0xfff:
338 val
= pci_default_read_config(&s
->dev
, addr
- 0xf00, size
);
341 val
= s
->regs
.cur_offset
;
343 case CUR_HORZ_VERT_POSN
:
344 val
= s
->regs
.cur_hv_pos
;
345 val
|= s
->regs
.cur_offset
& BIT(31);
347 case CUR_HORZ_VERT_OFF
:
348 val
= s
->regs
.cur_hv_offs
;
349 val
|= s
->regs
.cur_offset
& BIT(31);
352 val
= s
->regs
.cur_color0
;
355 val
= s
->regs
.cur_color1
;
358 val
= s
->regs
.dst_offset
;
361 val
= s
->regs
.dst_pitch
;
362 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
363 val
&= s
->regs
.dst_tile
<< 16;
367 val
= s
->regs
.dst_width
;
370 val
= s
->regs
.dst_height
;
384 case DP_GUI_MASTER_CNTL
:
385 val
= s
->regs
.dp_gui_master_cntl
;
388 val
= s
->regs
.src_offset
;
391 val
= s
->regs
.src_pitch
;
392 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
393 val
&= s
->regs
.src_tile
<< 16;
396 case DP_BRUSH_BKGD_CLR
:
397 val
= s
->regs
.dp_brush_bkgd_clr
;
399 case DP_BRUSH_FRGD_CLR
:
400 val
= s
->regs
.dp_brush_frgd_clr
;
402 case DP_SRC_FRGD_CLR
:
403 val
= s
->regs
.dp_src_frgd_clr
;
405 case DP_SRC_BKGD_CLR
:
406 val
= s
->regs
.dp_src_bkgd_clr
;
409 val
= s
->regs
.dp_cntl
;
412 val
= s
->regs
.dp_datatype
;
415 val
= s
->regs
.dp_mix
;
418 val
= s
->regs
.dp_write_mask
;
421 val
= s
->regs
.default_offset
;
424 val
= s
->regs
.default_pitch
;
426 case DEFAULT_SC_BOTTOM_RIGHT
:
427 val
= s
->regs
.default_sc_bottom_right
;
432 if (addr
< CUR_OFFSET
|| addr
> CUR_CLR1
|| ATI_DEBUG_HW_CURSOR
) {
433 trace_ati_mm_read(size
, addr
, ati_reg_name(addr
& ~3ULL), val
);
438 static inline void ati_reg_write_offs(uint32_t *reg
, int offs
,
439 uint64_t data
, unsigned int size
)
441 if (offs
== 0 && size
== 4) {
444 *reg
= deposit32(*reg
, offs
* BITS_PER_BYTE
, size
* BITS_PER_BYTE
,
449 static void ati_mm_write(void *opaque
, hwaddr addr
,
450 uint64_t data
, unsigned int size
)
452 ATIVGAState
*s
= opaque
;
454 if (addr
< CUR_OFFSET
|| addr
> CUR_CLR1
|| ATI_DEBUG_HW_CURSOR
) {
455 trace_ati_mm_write(size
, addr
, ati_reg_name(addr
& ~3ULL), data
);
459 s
->regs
.mm_index
= data
;
461 case MM_DATA
... MM_DATA
+ 3:
462 /* indexed access to regs or memory */
463 if (s
->regs
.mm_index
& BIT(31)) {
464 uint32_t idx
= s
->regs
.mm_index
& ~BIT(31);
465 if (idx
<= s
->vga
.vram_size
- size
) {
466 stn_le_p(s
->vga
.vram_ptr
+ idx
, size
, data
);
469 ati_mm_write(s
, s
->regs
.mm_index
+ addr
- MM_DATA
, data
, size
);
472 case BIOS_0_SCRATCH
... BUS_CNTL
- 1:
474 int i
= (addr
- BIOS_0_SCRATCH
) / 4;
475 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
&& i
> 3) {
478 ati_reg_write_offs(&s
->regs
.bios_scratch
[i
],
479 addr
- (BIOS_0_SCRATCH
+ i
* 4), data
, size
);
482 case CRTC_GEN_CNTL
... CRTC_GEN_CNTL
+ 3:
484 uint32_t val
= s
->regs
.crtc_gen_cntl
;
485 ati_reg_write_offs(&s
->regs
.crtc_gen_cntl
,
486 addr
- CRTC_GEN_CNTL
, data
, size
);
487 if ((val
& CRTC2_CUR_EN
) != (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
)) {
488 if (s
->cursor_guest_mode
) {
489 s
->vga
.force_shadow
= !!(s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
);
491 if (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) {
492 ati_cursor_define(s
);
494 dpy_mouse_set(s
->vga
.con
, s
->regs
.cur_hv_pos
>> 16,
495 s
->regs
.cur_hv_pos
& 0xffff,
496 (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) != 0);
499 if ((val
& (CRTC2_EXT_DISP_EN
| CRTC2_EN
)) !=
500 (s
->regs
.crtc_gen_cntl
& (CRTC2_EXT_DISP_EN
| CRTC2_EN
))) {
501 ati_vga_switch_mode(s
);
505 case CRTC_EXT_CNTL
... CRTC_EXT_CNTL
+ 3:
507 uint32_t val
= s
->regs
.crtc_ext_cntl
;
508 ati_reg_write_offs(&s
->regs
.crtc_ext_cntl
,
509 addr
- CRTC_EXT_CNTL
, data
, size
);
510 if (s
->regs
.crtc_ext_cntl
& CRT_CRTC_DISPLAY_DIS
) {
511 DPRINTF("Display disabled\n");
512 s
->vga
.ar_index
&= ~BIT(5);
514 DPRINTF("Display enabled\n");
515 s
->vga
.ar_index
|= BIT(5);
516 ati_vga_switch_mode(s
);
518 if ((val
& CRT_CRTC_DISPLAY_DIS
) !=
519 (s
->regs
.crtc_ext_cntl
& CRT_CRTC_DISPLAY_DIS
)) {
520 ati_vga_switch_mode(s
);
525 s
->regs
.dac_cntl
= data
& 0xffffe3ff;
526 s
->vga
.dac_8bit
= !!(data
& DAC_8BIT_EN
);
529 if (s
->dev_id
!= PCI_DEVICE_ID_ATI_RAGE128_PF
) {
530 /* FIXME: Maybe add a property to select VGA or DVI port? */
534 if (s
->dev_id
!= PCI_DEVICE_ID_ATI_RAGE128_PF
) {
535 s
->regs
.gpio_dvi_ddc
= ati_i2c(s
->bbi2c
, data
, 0);
538 case GPIO_MONID
... GPIO_MONID
+ 3:
539 /* FIXME What does Radeon have here? */
540 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
541 ati_reg_write_offs(&s
->regs
.gpio_monid
,
542 addr
- GPIO_MONID
, data
, size
);
544 * Rage128p accesses DDC used to get EDID via these bits.
545 * Only touch i2c when write overlaps 3rd byte because some
546 * drivers access this reg via multiple partial writes and
547 * without this spurious bits would be sent.
549 if ((s
->regs
.gpio_monid
& BIT(25)) &&
550 addr
<= GPIO_MONID
+ 2 && addr
+ size
> GPIO_MONID
+ 2) {
551 s
->regs
.gpio_monid
= ati_i2c(s
->bbi2c
, s
->regs
.gpio_monid
, 1);
555 case PALETTE_INDEX
... PALETTE_INDEX
+ 3:
557 vga_ioport_write(&s
->vga
, VGA_PEL_IR
, (data
>> 16) & 0xff);
558 vga_ioport_write(&s
->vga
, VGA_PEL_IW
, data
& 0xff);
560 if (addr
== PALETTE_INDEX
) {
561 vga_ioport_write(&s
->vga
, VGA_PEL_IW
, data
& 0xff);
563 vga_ioport_write(&s
->vga
, VGA_PEL_IR
, data
& 0xff);
567 case PALETTE_DATA
... PALETTE_DATA
+ 3:
568 data
<<= addr
- PALETTE_DATA
;
569 data
= bswap32(data
) >> 8;
570 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
572 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
574 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
576 case CRTC_H_TOTAL_DISP
:
577 s
->regs
.crtc_h_total_disp
= data
& 0x07ff07ff;
579 case CRTC_H_SYNC_STRT_WID
:
580 s
->regs
.crtc_h_sync_strt_wid
= data
& 0x17bf1fff;
582 case CRTC_V_TOTAL_DISP
:
583 s
->regs
.crtc_v_total_disp
= data
& 0x0fff0fff;
585 case CRTC_V_SYNC_STRT_WID
:
586 s
->regs
.crtc_v_sync_strt_wid
= data
& 0x9f0fff;
589 s
->regs
.crtc_offset
= data
& 0xc7ffffff;
591 case CRTC_OFFSET_CNTL
:
592 s
->regs
.crtc_offset_cntl
= data
; /* FIXME */
595 s
->regs
.crtc_pitch
= data
& 0x07ff07ff;
597 case 0xf00 ... 0xfff:
598 /* read-only copy of PCI config space so ignore writes */
601 if (s
->regs
.cur_offset
!= (data
& 0x87fffff0)) {
602 s
->regs
.cur_offset
= data
& 0x87fffff0;
603 ati_cursor_define(s
);
606 case CUR_HORZ_VERT_POSN
:
607 s
->regs
.cur_hv_pos
= data
& 0x3fff0fff;
608 if (data
& BIT(31)) {
609 s
->regs
.cur_offset
|= data
& BIT(31);
610 } else if (s
->regs
.cur_offset
& BIT(31)) {
611 s
->regs
.cur_offset
&= ~BIT(31);
612 ati_cursor_define(s
);
614 if (!s
->cursor_guest_mode
&&
615 (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) && !(data
& BIT(31))) {
616 dpy_mouse_set(s
->vga
.con
, s
->regs
.cur_hv_pos
>> 16,
617 s
->regs
.cur_hv_pos
& 0xffff, 1);
620 case CUR_HORZ_VERT_OFF
:
621 s
->regs
.cur_hv_offs
= data
& 0x3f003f;
622 if (data
& BIT(31)) {
623 s
->regs
.cur_offset
|= data
& BIT(31);
624 } else if (s
->regs
.cur_offset
& BIT(31)) {
625 s
->regs
.cur_offset
&= ~BIT(31);
626 ati_cursor_define(s
);
630 if (s
->regs
.cur_color0
!= (data
& 0xffffff)) {
631 s
->regs
.cur_color0
= data
& 0xffffff;
632 ati_cursor_define(s
);
637 * Update cursor unconditionally here because some clients set up
638 * other registers before actually writing cursor data to memory at
639 * offset so we would miss cursor change unless always updating here
641 s
->regs
.cur_color1
= data
& 0xffffff;
642 ati_cursor_define(s
);
645 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
646 s
->regs
.dst_offset
= data
& 0xfffffff0;
648 s
->regs
.dst_offset
= data
& 0xfffffc00;
652 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
653 s
->regs
.dst_pitch
= data
& 0x3fff;
654 s
->regs
.dst_tile
= (data
>> 16) & 1;
656 s
->regs
.dst_pitch
= data
& 0x3ff0;
660 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RADEON_QY
) {
661 s
->regs
.dst_tile
= data
& 3;
665 s
->regs
.dst_width
= data
& 0x3fff;
669 s
->regs
.dst_height
= data
& 0x3fff;
672 s
->regs
.src_x
= data
& 0x3fff;
675 s
->regs
.src_y
= data
& 0x3fff;
678 s
->regs
.dst_x
= data
& 0x3fff;
681 s
->regs
.dst_y
= data
& 0x3fff;
683 case SRC_PITCH_OFFSET
:
684 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
685 s
->regs
.src_offset
= (data
& 0x1fffff) << 5;
686 s
->regs
.src_pitch
= (data
>> 21) & 0x3ff;
687 s
->regs
.src_tile
= data
>> 31;
689 s
->regs
.src_offset
= (data
& 0x3fffff) << 11;
690 s
->regs
.src_pitch
= (data
& 0x3fc00000) >> 16;
691 s
->regs
.src_tile
= (data
>> 30) & 1;
694 case DST_PITCH_OFFSET
:
695 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
696 s
->regs
.dst_offset
= (data
& 0x1fffff) << 5;
697 s
->regs
.dst_pitch
= (data
>> 21) & 0x3ff;
698 s
->regs
.dst_tile
= data
>> 31;
700 s
->regs
.dst_offset
= (data
& 0x3fffff) << 11;
701 s
->regs
.dst_pitch
= (data
& 0x3fc00000) >> 16;
702 s
->regs
.dst_tile
= data
>> 30;
706 s
->regs
.src_x
= data
& 0x3fff;
707 s
->regs
.src_y
= (data
>> 16) & 0x3fff;
710 s
->regs
.dst_x
= data
& 0x3fff;
711 s
->regs
.dst_y
= (data
>> 16) & 0x3fff;
713 case DST_HEIGHT_WIDTH
:
714 s
->regs
.dst_width
= data
& 0x3fff;
715 s
->regs
.dst_height
= (data
>> 16) & 0x3fff;
718 case DP_GUI_MASTER_CNTL
:
719 s
->regs
.dp_gui_master_cntl
= data
& 0xf800000f;
720 s
->regs
.dp_datatype
= (data
& 0x0f00) >> 8 | (data
& 0x30f0) << 4 |
721 (data
& 0x4000) << 16;
722 s
->regs
.dp_mix
= (data
& GMC_ROP3_MASK
) | (data
& 0x7000000) >> 16;
725 s
->regs
.dst_x
= data
& 0x3fff;
726 s
->regs
.dst_width
= (data
>> 16) & 0x3fff;
730 s
->regs
.src_y
= data
& 0x3fff;
731 s
->regs
.src_x
= (data
>> 16) & 0x3fff;
734 s
->regs
.dst_y
= data
& 0x3fff;
735 s
->regs
.dst_x
= (data
>> 16) & 0x3fff;
737 case DST_WIDTH_HEIGHT
:
738 s
->regs
.dst_height
= data
& 0x3fff;
739 s
->regs
.dst_width
= (data
>> 16) & 0x3fff;
743 s
->regs
.dst_y
= data
& 0x3fff;
744 s
->regs
.dst_height
= (data
>> 16) & 0x3fff;
747 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
748 s
->regs
.src_offset
= data
& 0xfffffff0;
750 s
->regs
.src_offset
= data
& 0xfffffc00;
754 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
755 s
->regs
.src_pitch
= data
& 0x3fff;
756 s
->regs
.src_tile
= (data
>> 16) & 1;
758 s
->regs
.src_pitch
= data
& 0x3ff0;
761 case DP_BRUSH_BKGD_CLR
:
762 s
->regs
.dp_brush_bkgd_clr
= data
;
764 case DP_BRUSH_FRGD_CLR
:
765 s
->regs
.dp_brush_frgd_clr
= data
;
768 s
->regs
.dp_cntl
= data
;
771 s
->regs
.dp_datatype
= data
& 0xe0070f0f;
774 s
->regs
.dp_mix
= data
& 0x00ff0700;
777 s
->regs
.dp_write_mask
= data
;
780 data
&= (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
?
781 0x03fffc00 : 0xfffffc00);
782 s
->regs
.default_offset
= data
;
785 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
786 s
->regs
.default_pitch
= data
& 0x103ff;
789 case DEFAULT_SC_BOTTOM_RIGHT
:
790 s
->regs
.default_sc_bottom_right
= data
& 0x3fff3fff;
797 static const MemoryRegionOps ati_mm_ops
= {
799 .write
= ati_mm_write
,
800 .endianness
= DEVICE_LITTLE_ENDIAN
,
803 static void ati_vga_realize(PCIDevice
*dev
, Error
**errp
)
805 ATIVGAState
*s
= ATI_VGA(dev
);
806 VGACommonState
*vga
= &s
->vga
;
810 for (i
= 0; i
< ARRAY_SIZE(ati_model_aliases
); i
++) {
811 if (!strcmp(s
->model
, ati_model_aliases
[i
].name
)) {
812 s
->dev_id
= ati_model_aliases
[i
].dev_id
;
816 if (i
>= ARRAY_SIZE(ati_model_aliases
)) {
817 warn_report("Unknown ATI VGA model name, "
818 "using default rage128p");
821 if (s
->dev_id
!= PCI_DEVICE_ID_ATI_RAGE128_PF
&&
822 s
->dev_id
!= PCI_DEVICE_ID_ATI_RADEON_QY
) {
823 error_setg(errp
, "Unknown ATI VGA device id, "
824 "only 0x5046 and 0x5159 are supported");
827 pci_set_word(dev
->config
+ PCI_DEVICE_ID
, s
->dev_id
);
829 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RADEON_QY
&&
830 s
->vga
.vram_size_mb
< 16) {
831 warn_report("Too small video memory for device id");
832 s
->vga
.vram_size_mb
= 16;
836 vga_common_init(vga
, OBJECT(s
));
837 vga_init(vga
, OBJECT(s
), pci_address_space(dev
),
838 pci_address_space_io(dev
), true);
839 vga
->con
= graphic_console_init(DEVICE(s
), 0, s
->vga
.hw_ops
, &s
->vga
);
840 if (s
->cursor_guest_mode
) {
841 vga
->cursor_invalidate
= ati_cursor_invalidate
;
842 vga
->cursor_draw_line
= ati_cursor_draw_line
;
846 I2CBus
*i2cbus
= i2c_init_bus(DEVICE(s
), "ati-vga.ddc");
847 s
->bbi2c
= bitbang_i2c_init(i2cbus
);
848 I2CSlave
*i2cddc
= I2C_SLAVE(qdev_create(BUS(i2cbus
), TYPE_I2CDDC
));
849 i2c_set_slave_address(i2cddc
, 0x50);
851 /* mmio register space */
852 memory_region_init_io(&s
->mm
, OBJECT(s
), &ati_mm_ops
, s
,
853 "ati.mmregs", 0x4000);
854 /* io space is alias to beginning of mmregs */
855 memory_region_init_alias(&s
->io
, OBJECT(s
), "ati.io", &s
->mm
, 0, 0x100);
857 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_MEM_PREFETCH
, &vga
->vram
);
858 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io
);
859 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mm
);
862 static void ati_vga_reset(DeviceState
*dev
)
864 ATIVGAState
*s
= ATI_VGA(dev
);
867 vga_common_reset(&s
->vga
);
871 static void ati_vga_exit(PCIDevice
*dev
)
873 ATIVGAState
*s
= ATI_VGA(dev
);
875 graphic_console_close(s
->vga
.con
);
879 static Property ati_vga_properties
[] = {
880 DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState
, vga
.vram_size_mb
, 16),
881 DEFINE_PROP_STRING("model", ATIVGAState
, model
),
882 DEFINE_PROP_UINT16("x-device-id", ATIVGAState
, dev_id
,
883 PCI_DEVICE_ID_ATI_RAGE128_PF
),
884 DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState
, cursor_guest_mode
, false),
885 DEFINE_PROP_END_OF_LIST()
888 static void ati_vga_class_init(ObjectClass
*klass
, void *data
)
890 DeviceClass
*dc
= DEVICE_CLASS(klass
);
891 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
893 dc
->reset
= ati_vga_reset
;
894 dc
->props
= ati_vga_properties
;
895 dc
->hotpluggable
= false;
896 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
898 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
899 k
->vendor_id
= PCI_VENDOR_ID_ATI
;
900 k
->device_id
= PCI_DEVICE_ID_ATI_RAGE128_PF
;
901 k
->romfile
= "vgabios-stdvga.bin";
902 k
->realize
= ati_vga_realize
;
903 k
->exit
= ati_vga_exit
;
906 static const TypeInfo ati_vga_info
= {
907 .name
= TYPE_ATI_VGA
,
908 .parent
= TYPE_PCI_DEVICE
,
909 .instance_size
= sizeof(ATIVGAState
),
910 .class_init
= ati_vga_class_init
,
911 .interfaces
= (InterfaceInfo
[]) {
912 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
917 static void ati_vga_register_types(void)
919 type_register_static(&ati_vga_info
);
922 type_init(ati_vga_register_types
)