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"
31 #define ATI_DEBUG_HW_CURSOR 0
36 } ati_model_aliases
[] = {
37 { "rage128p", PCI_DEVICE_ID_ATI_RAGE128_PF
},
38 { "rv100", PCI_DEVICE_ID_ATI_RADEON_QY
},
41 enum { VGA_MODE
, EXT_MODE
};
43 static void ati_vga_switch_mode(ATIVGAState
*s
)
46 s
->mode
, !!(s
->regs
.crtc_gen_cntl
& CRTC2_EXT_DISP_EN
));
47 if (s
->regs
.crtc_gen_cntl
& CRTC2_EXT_DISP_EN
) {
48 /* Extended mode enabled */
50 if (s
->regs
.crtc_gen_cntl
& CRTC2_EN
) {
51 /* CRT controller enabled, use CRTC values */
52 uint32_t offs
= s
->regs
.crtc_offset
& 0x07ffffff;
53 int stride
= (s
->regs
.crtc_pitch
& 0x7ff) * 8;
57 if (s
->regs
.crtc_h_total_disp
== 0) {
58 s
->regs
.crtc_h_total_disp
= ((640 / 8) - 1) << 16;
60 if (s
->regs
.crtc_v_total_disp
== 0) {
61 s
->regs
.crtc_v_total_disp
= (480 - 1) << 16;
63 h
= ((s
->regs
.crtc_h_total_disp
>> 16) + 1) * 8;
64 v
= (s
->regs
.crtc_v_total_disp
>> 16) + 1;
65 switch (s
->regs
.crtc_gen_cntl
& CRTC_PIX_WIDTH_MASK
) {
66 case CRTC_PIX_WIDTH_4BPP
:
69 case CRTC_PIX_WIDTH_8BPP
:
72 case CRTC_PIX_WIDTH_15BPP
:
75 case CRTC_PIX_WIDTH_16BPP
:
78 case CRTC_PIX_WIDTH_24BPP
:
81 case CRTC_PIX_WIDTH_32BPP
:
85 qemu_log_mask(LOG_UNIMP
, "Unsupported bpp value\n");
88 DPRINTF("Switching to %dx%d %d %d @ %x\n", h
, v
, stride
, bpp
, offs
);
89 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_ENABLE
);
90 vbe_ioport_write_data(&s
->vga
, 0, VBE_DISPI_DISABLED
);
91 /* reset VBE regs then set up mode */
92 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_XRES
] = h
;
93 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_YRES
] = v
;
94 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_BPP
] = bpp
;
95 /* enable mode via ioport so it updates vga regs */
96 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_ENABLE
);
97 vbe_ioport_write_data(&s
->vga
, 0, VBE_DISPI_ENABLED
|
98 VBE_DISPI_LFB_ENABLED
| VBE_DISPI_NOCLEARMEM
|
99 (s
->regs
.dac_cntl
& DAC_8BIT_EN
? VBE_DISPI_8BIT_DAC
: 0));
100 /* now set offset and stride after enable as that resets these */
102 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_VIRT_WIDTH
);
103 vbe_ioport_write_data(&s
->vga
, 0, stride
);
104 if (offs
% stride
== 0) {
105 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_Y_OFFSET
);
106 vbe_ioport_write_data(&s
->vga
, 0, offs
/ stride
);
108 /* FIXME what to do with this? */
109 error_report("VGA offset is not multiple of pitch, "
110 "expect bad picture");
115 /* VGA mode enabled */
117 vbe_ioport_write_index(&s
->vga
, 0, VBE_DISPI_INDEX_ENABLE
);
118 vbe_ioport_write_data(&s
->vga
, 0, VBE_DISPI_DISABLED
);
122 /* Used by host side hardware cursor */
123 static void ati_cursor_define(ATIVGAState
*s
)
129 if ((s
->regs
.cur_offset
& BIT(31)) || s
->cursor_guest_mode
) {
130 return; /* Do not update cursor if locked or rendered by guest */
132 /* FIXME handle cur_hv_offs correctly */
133 src
= s
->vga
.vram_ptr
+ (s
->regs
.crtc_offset
& 0x07ffffff) +
134 s
->regs
.cur_offset
- (s
->regs
.cur_hv_offs
>> 16) -
135 (s
->regs
.cur_hv_offs
& 0xffff) * 16;
136 for (i
= 0; i
< 64; i
++) {
137 for (j
= 0; j
< 8; j
++, idx
++) {
138 data
[idx
] = src
[i
* 16 + j
];
139 data
[512 + idx
] = src
[i
* 16 + j
+ 8];
143 s
->cursor
= cursor_alloc(64, 64);
145 cursor_set_mono(s
->cursor
, s
->regs
.cur_color1
, s
->regs
.cur_color0
,
146 &data
[512], 1, &data
[0]);
147 dpy_cursor_define(s
->vga
.con
, s
->cursor
);
150 /* Alternatively support guest rendered hardware cursor */
151 static void ati_cursor_invalidate(VGACommonState
*vga
)
153 ATIVGAState
*s
= container_of(vga
, ATIVGAState
, vga
);
154 int size
= (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) ? 64 : 0;
156 if (s
->regs
.cur_offset
& BIT(31)) {
157 return; /* Do not update cursor if locked */
159 if (s
->cursor_size
!= size
||
160 vga
->hw_cursor_x
!= s
->regs
.cur_hv_pos
>> 16 ||
161 vga
->hw_cursor_y
!= (s
->regs
.cur_hv_pos
& 0xffff) ||
162 s
->cursor_offset
!= s
->regs
.cur_offset
- (s
->regs
.cur_hv_offs
>> 16) -
163 (s
->regs
.cur_hv_offs
& 0xffff) * 16) {
164 /* Remove old cursor then update and show new one if needed */
165 vga_invalidate_scanlines(vga
, vga
->hw_cursor_y
, vga
->hw_cursor_y
+ 63);
166 vga
->hw_cursor_x
= s
->regs
.cur_hv_pos
>> 16;
167 vga
->hw_cursor_y
= s
->regs
.cur_hv_pos
& 0xffff;
168 s
->cursor_offset
= s
->regs
.cur_offset
- (s
->regs
.cur_hv_offs
>> 16) -
169 (s
->regs
.cur_hv_offs
& 0xffff) * 16;
170 s
->cursor_size
= size
;
172 vga_invalidate_scanlines(vga
,
173 vga
->hw_cursor_y
, vga
->hw_cursor_y
+ 63);
178 static void ati_cursor_draw_line(VGACommonState
*vga
, uint8_t *d
, int scr_y
)
180 ATIVGAState
*s
= container_of(vga
, ATIVGAState
, vga
);
182 uint32_t *dp
= (uint32_t *)d
;
185 if (!(s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) ||
186 scr_y
< vga
->hw_cursor_y
|| scr_y
>= vga
->hw_cursor_y
+ 64 ||
187 scr_y
> s
->regs
.crtc_v_total_disp
>> 16) {
190 /* FIXME handle cur_hv_offs correctly */
191 src
= s
->vga
.vram_ptr
+ (s
->regs
.crtc_offset
& 0x07ffffff) +
192 s
->cursor_offset
+ (scr_y
- vga
->hw_cursor_y
) * 16;
193 dp
= &dp
[vga
->hw_cursor_x
];
194 h
= ((s
->regs
.crtc_h_total_disp
>> 16) + 1) * 8;
195 for (i
= 0; i
< 8; i
++) {
197 uint8_t abits
= src
[i
];
198 uint8_t xbits
= src
[i
+ 8];
199 for (j
= 0; j
< 8; j
++, abits
<<= 1, xbits
<<= 1) {
200 if (abits
& BIT(7)) {
201 if (xbits
& BIT(7)) {
202 color
= dp
[i
* 8 + j
] ^ 0xffffffff; /* complement */
204 continue; /* transparent, no change */
207 color
= (xbits
& BIT(7) ? s
->regs
.cur_color1
:
208 s
->regs
.cur_color0
) << 8 | 0xff;
210 if (vga
->hw_cursor_x
+ i
* 8 + j
>= h
) {
211 return; /* end of screen, don't span to next line */
213 dp
[i
* 8 + j
] = color
;
218 static inline uint64_t ati_reg_read_offs(uint32_t reg
, int offs
,
221 if (offs
== 0 && size
== 4) {
224 return extract32(reg
, offs
* BITS_PER_BYTE
, size
* BITS_PER_BYTE
);
228 static uint64_t ati_mm_read(void *opaque
, hwaddr addr
, unsigned int size
)
230 ATIVGAState
*s
= opaque
;
235 val
= s
->regs
.mm_index
;
237 case MM_DATA
... MM_DATA
+ 3:
238 /* indexed access to regs or memory */
239 if (s
->regs
.mm_index
& BIT(31)) {
240 uint32_t idx
= s
->regs
.mm_index
& ~BIT(31);
241 if (idx
<= s
->vga
.vram_size
- size
) {
242 val
= ldn_le_p(s
->vga
.vram_ptr
+ idx
, size
);
245 val
= ati_mm_read(s
, s
->regs
.mm_index
+ addr
- MM_DATA
, size
);
248 case BIOS_0_SCRATCH
... BUS_CNTL
- 1:
250 int i
= (addr
- BIOS_0_SCRATCH
) / 4;
251 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
&& i
> 3) {
254 val
= ati_reg_read_offs(s
->regs
.bios_scratch
[i
],
255 addr
- (BIOS_0_SCRATCH
+ i
* 4), size
);
258 case CRTC_GEN_CNTL
... CRTC_GEN_CNTL
+ 3:
259 val
= ati_reg_read_offs(s
->regs
.crtc_gen_cntl
,
260 addr
- CRTC_GEN_CNTL
, size
);
262 case CRTC_EXT_CNTL
... CRTC_EXT_CNTL
+ 3:
263 val
= ati_reg_read_offs(s
->regs
.crtc_ext_cntl
,
264 addr
- CRTC_EXT_CNTL
, size
);
267 val
= s
->regs
.dac_cntl
;
269 /* case GPIO_MONID: FIXME hook up DDC I2C here */
271 /* FIXME unaligned access */
272 val
= vga_ioport_read(&s
->vga
, VGA_PEL_IR
) << 16;
273 val
|= vga_ioport_read(&s
->vga
, VGA_PEL_IW
) & 0xff;
276 val
= vga_ioport_read(&s
->vga
, VGA_PEL_D
);
279 val
= s
->vga
.vram_size
;
286 val
= 64; /* free CMDFIFO entries */
288 case CRTC_H_TOTAL_DISP
:
289 val
= s
->regs
.crtc_h_total_disp
;
291 case CRTC_H_SYNC_STRT_WID
:
292 val
= s
->regs
.crtc_h_sync_strt_wid
;
294 case CRTC_V_TOTAL_DISP
:
295 val
= s
->regs
.crtc_v_total_disp
;
297 case CRTC_V_SYNC_STRT_WID
:
298 val
= s
->regs
.crtc_v_sync_strt_wid
;
301 val
= s
->regs
.crtc_offset
;
303 case CRTC_OFFSET_CNTL
:
304 val
= s
->regs
.crtc_offset_cntl
;
307 val
= s
->regs
.crtc_pitch
;
309 case 0xf00 ... 0xfff:
310 val
= pci_default_read_config(&s
->dev
, addr
- 0xf00, size
);
313 val
= s
->regs
.cur_offset
;
315 case CUR_HORZ_VERT_POSN
:
316 val
= s
->regs
.cur_hv_pos
;
317 val
|= s
->regs
.cur_offset
& BIT(31);
319 case CUR_HORZ_VERT_OFF
:
320 val
= s
->regs
.cur_hv_offs
;
321 val
|= s
->regs
.cur_offset
& BIT(31);
324 val
= s
->regs
.cur_color0
;
327 val
= s
->regs
.cur_color1
;
330 val
= s
->regs
.dst_offset
;
333 val
= s
->regs
.dst_pitch
;
334 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
335 val
&= s
->regs
.dst_tile
<< 16;
339 val
= s
->regs
.dst_width
;
342 val
= s
->regs
.dst_height
;
356 case DP_GUI_MASTER_CNTL
:
357 val
= s
->regs
.dp_gui_master_cntl
;
360 val
= s
->regs
.src_offset
;
363 val
= s
->regs
.src_pitch
;
364 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
365 val
&= s
->regs
.src_tile
<< 16;
368 case DP_BRUSH_BKGD_CLR
:
369 val
= s
->regs
.dp_brush_bkgd_clr
;
371 case DP_BRUSH_FRGD_CLR
:
372 val
= s
->regs
.dp_brush_frgd_clr
;
374 case DP_SRC_FRGD_CLR
:
375 val
= s
->regs
.dp_src_frgd_clr
;
377 case DP_SRC_BKGD_CLR
:
378 val
= s
->regs
.dp_src_bkgd_clr
;
381 val
= s
->regs
.dp_cntl
;
384 val
= s
->regs
.dp_datatype
;
387 val
= s
->regs
.dp_mix
;
390 val
= s
->regs
.dp_write_mask
;
393 val
= s
->regs
.default_offset
;
396 val
= s
->regs
.default_pitch
;
398 case DEFAULT_SC_BOTTOM_RIGHT
:
399 val
= s
->regs
.default_sc_bottom_right
;
404 if (addr
< CUR_OFFSET
|| addr
> CUR_CLR1
|| ATI_DEBUG_HW_CURSOR
) {
405 trace_ati_mm_read(size
, addr
, ati_reg_name(addr
& ~3ULL), val
);
410 static inline void ati_reg_write_offs(uint32_t *reg
, int offs
,
411 uint64_t data
, unsigned int size
)
413 if (offs
== 0 && size
== 4) {
416 *reg
= deposit32(*reg
, offs
* BITS_PER_BYTE
, size
* BITS_PER_BYTE
,
421 static void ati_mm_write(void *opaque
, hwaddr addr
,
422 uint64_t data
, unsigned int size
)
424 ATIVGAState
*s
= opaque
;
426 if (addr
< CUR_OFFSET
|| addr
> CUR_CLR1
|| ATI_DEBUG_HW_CURSOR
) {
427 trace_ati_mm_write(size
, addr
, ati_reg_name(addr
& ~3ULL), data
);
431 s
->regs
.mm_index
= data
;
433 case MM_DATA
... MM_DATA
+ 3:
434 /* indexed access to regs or memory */
435 if (s
->regs
.mm_index
& BIT(31)) {
436 uint32_t idx
= s
->regs
.mm_index
& ~BIT(31);
437 if (idx
<= s
->vga
.vram_size
- size
) {
438 stn_le_p(s
->vga
.vram_ptr
+ idx
, size
, data
);
441 ati_mm_write(s
, s
->regs
.mm_index
+ addr
- MM_DATA
, data
, size
);
444 case BIOS_0_SCRATCH
... BUS_CNTL
- 1:
446 int i
= (addr
- BIOS_0_SCRATCH
) / 4;
447 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
&& i
> 3) {
450 ati_reg_write_offs(&s
->regs
.bios_scratch
[i
],
451 addr
- (BIOS_0_SCRATCH
+ i
* 4), data
, size
);
454 case CRTC_GEN_CNTL
... CRTC_GEN_CNTL
+ 3:
456 uint32_t val
= s
->regs
.crtc_gen_cntl
;
457 ati_reg_write_offs(&s
->regs
.crtc_gen_cntl
,
458 addr
- CRTC_GEN_CNTL
, data
, size
);
459 if ((val
& CRTC2_CUR_EN
) != (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
)) {
460 if (s
->cursor_guest_mode
) {
461 s
->vga
.force_shadow
= !!(s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
);
463 if (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) {
464 ati_cursor_define(s
);
466 dpy_mouse_set(s
->vga
.con
, s
->regs
.cur_hv_pos
>> 16,
467 s
->regs
.cur_hv_pos
& 0xffff,
468 (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) != 0);
471 if ((val
& (CRTC2_EXT_DISP_EN
| CRTC2_EN
)) !=
472 (s
->regs
.crtc_gen_cntl
& (CRTC2_EXT_DISP_EN
| CRTC2_EN
))) {
473 ati_vga_switch_mode(s
);
477 case CRTC_EXT_CNTL
... CRTC_EXT_CNTL
+ 3:
479 uint32_t val
= s
->regs
.crtc_ext_cntl
;
480 ati_reg_write_offs(&s
->regs
.crtc_ext_cntl
,
481 addr
- CRTC_EXT_CNTL
, data
, size
);
482 if (s
->regs
.crtc_ext_cntl
& CRT_CRTC_DISPLAY_DIS
) {
483 DPRINTF("Display disabled\n");
484 s
->vga
.ar_index
&= ~BIT(5);
486 DPRINTF("Display enabled\n");
487 s
->vga
.ar_index
|= BIT(5);
488 ati_vga_switch_mode(s
);
490 if ((val
& CRT_CRTC_DISPLAY_DIS
) !=
491 (s
->regs
.crtc_ext_cntl
& CRT_CRTC_DISPLAY_DIS
)) {
492 ati_vga_switch_mode(s
);
497 s
->regs
.dac_cntl
= data
& 0xffffe3ff;
498 s
->vga
.dac_8bit
= !!(data
& DAC_8BIT_EN
);
500 /* case GPIO_MONID: FIXME hook up DDC I2C here */
501 case PALETTE_INDEX
... PALETTE_INDEX
+ 3:
503 vga_ioport_write(&s
->vga
, VGA_PEL_IR
, (data
>> 16) & 0xff);
504 vga_ioport_write(&s
->vga
, VGA_PEL_IW
, data
& 0xff);
506 if (addr
== PALETTE_INDEX
) {
507 vga_ioport_write(&s
->vga
, VGA_PEL_IW
, data
& 0xff);
509 vga_ioport_write(&s
->vga
, VGA_PEL_IR
, data
& 0xff);
513 case PALETTE_DATA
... PALETTE_DATA
+ 3:
514 data
<<= addr
- PALETTE_DATA
;
515 data
= bswap32(data
) >> 8;
516 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
518 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
520 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
522 case CRTC_H_TOTAL_DISP
:
523 s
->regs
.crtc_h_total_disp
= data
& 0x07ff07ff;
525 case CRTC_H_SYNC_STRT_WID
:
526 s
->regs
.crtc_h_sync_strt_wid
= data
& 0x17bf1fff;
528 case CRTC_V_TOTAL_DISP
:
529 s
->regs
.crtc_v_total_disp
= data
& 0x0fff0fff;
531 case CRTC_V_SYNC_STRT_WID
:
532 s
->regs
.crtc_v_sync_strt_wid
= data
& 0x9f0fff;
535 s
->regs
.crtc_offset
= data
& 0xc7ffffff;
537 case CRTC_OFFSET_CNTL
:
538 s
->regs
.crtc_offset_cntl
= data
; /* FIXME */
541 s
->regs
.crtc_pitch
= data
& 0x07ff07ff;
543 case 0xf00 ... 0xfff:
544 /* read-only copy of PCI config space so ignore writes */
547 if (s
->regs
.cur_offset
!= (data
& 0x87fffff0)) {
548 s
->regs
.cur_offset
= data
& 0x87fffff0;
549 ati_cursor_define(s
);
552 case CUR_HORZ_VERT_POSN
:
553 s
->regs
.cur_hv_pos
= data
& 0x3fff0fff;
554 if (data
& BIT(31)) {
555 s
->regs
.cur_offset
|= data
& BIT(31);
556 } else if (s
->regs
.cur_offset
& BIT(31)) {
557 s
->regs
.cur_offset
&= ~BIT(31);
558 ati_cursor_define(s
);
560 if (!s
->cursor_guest_mode
&&
561 (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) && !(data
& BIT(31))) {
562 dpy_mouse_set(s
->vga
.con
, s
->regs
.cur_hv_pos
>> 16,
563 s
->regs
.cur_hv_pos
& 0xffff, 1);
566 case CUR_HORZ_VERT_OFF
:
567 s
->regs
.cur_hv_offs
= data
& 0x3f003f;
568 if (data
& BIT(31)) {
569 s
->regs
.cur_offset
|= data
& BIT(31);
570 } else if (s
->regs
.cur_offset
& BIT(31)) {
571 s
->regs
.cur_offset
&= ~BIT(31);
572 ati_cursor_define(s
);
576 if (s
->regs
.cur_color0
!= (data
& 0xffffff)) {
577 s
->regs
.cur_color0
= data
& 0xffffff;
578 ati_cursor_define(s
);
583 * Update cursor unconditionally here because some clients set up
584 * other registers before actually writing cursor data to memory at
585 * offset so we would miss cursor change unless always updating here
587 s
->regs
.cur_color1
= data
& 0xffffff;
588 ati_cursor_define(s
);
591 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
592 s
->regs
.dst_offset
= data
& 0xfffffff0;
594 s
->regs
.dst_offset
= data
& 0xfffffc00;
598 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
599 s
->regs
.dst_pitch
= data
& 0x3fff;
600 s
->regs
.dst_tile
= (data
>> 16) & 1;
602 s
->regs
.dst_pitch
= data
& 0x3ff0;
606 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RADEON_QY
) {
607 s
->regs
.dst_tile
= data
& 3;
611 s
->regs
.dst_width
= data
& 0x3fff;
615 s
->regs
.dst_height
= data
& 0x3fff;
618 s
->regs
.src_x
= data
& 0x3fff;
621 s
->regs
.src_y
= data
& 0x3fff;
624 s
->regs
.dst_x
= data
& 0x3fff;
627 s
->regs
.dst_y
= data
& 0x3fff;
629 case SRC_PITCH_OFFSET
:
630 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
631 s
->regs
.src_offset
= (data
& 0x1fffff) << 5;
632 s
->regs
.src_pitch
= (data
>> 21) & 0x3ff;
633 s
->regs
.src_tile
= data
>> 31;
635 s
->regs
.src_offset
= (data
& 0x3fffff) << 11;
636 s
->regs
.src_pitch
= (data
& 0x3fc00000) >> 16;
637 s
->regs
.src_tile
= (data
>> 30) & 1;
640 case DST_PITCH_OFFSET
:
641 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
642 s
->regs
.dst_offset
= (data
& 0x1fffff) << 5;
643 s
->regs
.dst_pitch
= (data
>> 21) & 0x3ff;
644 s
->regs
.dst_tile
= data
>> 31;
646 s
->regs
.dst_offset
= (data
& 0x3fffff) << 11;
647 s
->regs
.dst_pitch
= (data
& 0x3fc00000) >> 16;
648 s
->regs
.dst_tile
= data
>> 30;
652 s
->regs
.src_x
= data
& 0x3fff;
653 s
->regs
.src_y
= (data
>> 16) & 0x3fff;
656 s
->regs
.dst_x
= data
& 0x3fff;
657 s
->regs
.dst_y
= (data
>> 16) & 0x3fff;
659 case DST_HEIGHT_WIDTH
:
660 s
->regs
.dst_width
= data
& 0x3fff;
661 s
->regs
.dst_height
= (data
>> 16) & 0x3fff;
664 case DP_GUI_MASTER_CNTL
:
665 s
->regs
.dp_gui_master_cntl
= data
& 0xf800000f;
666 s
->regs
.dp_datatype
= (data
& 0x0f00) >> 8 | (data
& 0x30f0) << 4 |
667 (data
& 0x4000) << 16;
668 s
->regs
.dp_mix
= (data
& GMC_ROP3_MASK
) | (data
& 0x7000000) >> 16;
671 s
->regs
.dst_x
= data
& 0x3fff;
672 s
->regs
.dst_width
= (data
>> 16) & 0x3fff;
676 s
->regs
.src_y
= data
& 0x3fff;
677 s
->regs
.src_x
= (data
>> 16) & 0x3fff;
680 s
->regs
.dst_y
= data
& 0x3fff;
681 s
->regs
.dst_x
= (data
>> 16) & 0x3fff;
683 case DST_WIDTH_HEIGHT
:
684 s
->regs
.dst_height
= data
& 0x3fff;
685 s
->regs
.dst_width
= (data
>> 16) & 0x3fff;
689 s
->regs
.dst_y
= data
& 0x3fff;
690 s
->regs
.dst_height
= (data
>> 16) & 0x3fff;
693 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
694 s
->regs
.src_offset
= data
& 0xfffffff0;
696 s
->regs
.src_offset
= data
& 0xfffffc00;
700 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
701 s
->regs
.src_pitch
= data
& 0x3fff;
702 s
->regs
.src_tile
= (data
>> 16) & 1;
704 s
->regs
.src_pitch
= data
& 0x3ff0;
707 case DP_BRUSH_BKGD_CLR
:
708 s
->regs
.dp_brush_bkgd_clr
= data
;
710 case DP_BRUSH_FRGD_CLR
:
711 s
->regs
.dp_brush_frgd_clr
= data
;
714 s
->regs
.dp_cntl
= data
;
717 s
->regs
.dp_datatype
= data
& 0xe0070f0f;
720 s
->regs
.dp_mix
= data
& 0x00ff0700;
723 s
->regs
.dp_write_mask
= data
;
726 data
&= (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
?
727 0x03fffc00 : 0xfffffc00);
728 s
->regs
.default_offset
= data
;
731 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
732 s
->regs
.default_pitch
= data
& 0x103ff;
735 case DEFAULT_SC_BOTTOM_RIGHT
:
736 s
->regs
.default_sc_bottom_right
= data
& 0x3fff3fff;
743 static const MemoryRegionOps ati_mm_ops
= {
745 .write
= ati_mm_write
,
746 .endianness
= DEVICE_LITTLE_ENDIAN
,
749 static void ati_vga_realize(PCIDevice
*dev
, Error
**errp
)
751 ATIVGAState
*s
= ATI_VGA(dev
);
752 VGACommonState
*vga
= &s
->vga
;
756 for (i
= 0; i
< ARRAY_SIZE(ati_model_aliases
); i
++) {
757 if (!strcmp(s
->model
, ati_model_aliases
[i
].name
)) {
758 s
->dev_id
= ati_model_aliases
[i
].dev_id
;
762 if (i
>= ARRAY_SIZE(ati_model_aliases
)) {
763 warn_report("Unknown ATI VGA model name, "
764 "using default rage128p");
767 if (s
->dev_id
!= PCI_DEVICE_ID_ATI_RAGE128_PF
&&
768 s
->dev_id
!= PCI_DEVICE_ID_ATI_RADEON_QY
) {
769 error_setg(errp
, "Unknown ATI VGA device id, "
770 "only 0x5046 and 0x5159 are supported");
773 pci_set_word(dev
->config
+ PCI_DEVICE_ID
, s
->dev_id
);
775 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RADEON_QY
&&
776 s
->vga
.vram_size_mb
< 16) {
777 warn_report("Too small video memory for device id");
778 s
->vga
.vram_size_mb
= 16;
782 vga_common_init(vga
, OBJECT(s
));
783 vga_init(vga
, OBJECT(s
), pci_address_space(dev
),
784 pci_address_space_io(dev
), true);
785 vga
->con
= graphic_console_init(DEVICE(s
), 0, s
->vga
.hw_ops
, &s
->vga
);
786 if (s
->cursor_guest_mode
) {
787 vga
->cursor_invalidate
= ati_cursor_invalidate
;
788 vga
->cursor_draw_line
= ati_cursor_draw_line
;
791 /* mmio register space */
792 memory_region_init_io(&s
->mm
, OBJECT(s
), &ati_mm_ops
, s
,
793 "ati.mmregs", 0x4000);
794 /* io space is alias to beginning of mmregs */
795 memory_region_init_alias(&s
->io
, OBJECT(s
), "ati.io", &s
->mm
, 0, 0x100);
797 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_MEM_PREFETCH
, &vga
->vram
);
798 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io
);
799 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mm
);
802 static void ati_vga_reset(DeviceState
*dev
)
804 ATIVGAState
*s
= ATI_VGA(dev
);
807 vga_common_reset(&s
->vga
);
811 static void ati_vga_exit(PCIDevice
*dev
)
813 ATIVGAState
*s
= ATI_VGA(dev
);
815 graphic_console_close(s
->vga
.con
);
818 static Property ati_vga_properties
[] = {
819 DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState
, vga
.vram_size_mb
, 16),
820 DEFINE_PROP_STRING("model", ATIVGAState
, model
),
821 DEFINE_PROP_UINT16("x-device-id", ATIVGAState
, dev_id
,
822 PCI_DEVICE_ID_ATI_RAGE128_PF
),
823 DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState
, cursor_guest_mode
, false),
824 DEFINE_PROP_END_OF_LIST()
827 static void ati_vga_class_init(ObjectClass
*klass
, void *data
)
829 DeviceClass
*dc
= DEVICE_CLASS(klass
);
830 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
832 dc
->reset
= ati_vga_reset
;
833 dc
->props
= ati_vga_properties
;
834 dc
->hotpluggable
= false;
835 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
837 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
838 k
->vendor_id
= PCI_VENDOR_ID_ATI
;
839 k
->device_id
= PCI_DEVICE_ID_ATI_RAGE128_PF
;
840 k
->romfile
= "vgabios-stdvga.bin";
841 k
->realize
= ati_vga_realize
;
842 k
->exit
= ati_vga_exit
;
845 static const TypeInfo ati_vga_info
= {
846 .name
= TYPE_ATI_VGA
,
847 .parent
= TYPE_PCI_DEVICE
,
848 .instance_size
= sizeof(ATIVGAState
),
849 .class_init
= ati_vga_class_init
,
850 .interfaces
= (InterfaceInfo
[]) {
851 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
856 static void ati_vga_register_types(void)
858 type_register_static(&ati_vga_info
);
861 type_init(ati_vga_register_types
)