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 uint32_t idx
= s
->regs
.mm_index
& ~BIT(31);
239 if (idx
<= s
->vga
.vram_size
- size
) {
240 val
= ldn_le_p(s
->vga
.vram_ptr
+ idx
, size
);
243 val
= ati_mm_read(s
, s
->regs
.mm_index
+ addr
- MM_DATA
, size
);
246 case BIOS_0_SCRATCH
... BUS_CNTL
- 1:
248 int i
= (addr
- BIOS_0_SCRATCH
) / 4;
249 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
&& i
> 3) {
252 val
= ati_reg_read_offs(s
->regs
.bios_scratch
[i
],
253 addr
- (BIOS_0_SCRATCH
+ i
* 4), size
);
256 case CRTC_GEN_CNTL
... CRTC_GEN_CNTL
+ 3:
257 val
= ati_reg_read_offs(s
->regs
.crtc_gen_cntl
,
258 addr
- CRTC_GEN_CNTL
, size
);
260 case CRTC_EXT_CNTL
... CRTC_EXT_CNTL
+ 3:
261 val
= ati_reg_read_offs(s
->regs
.crtc_ext_cntl
,
262 addr
- CRTC_EXT_CNTL
, size
);
265 val
= s
->regs
.dac_cntl
;
267 /* case GPIO_MONID: FIXME hook up DDC I2C here */
269 /* FIXME unaligned access */
270 val
= vga_ioport_read(&s
->vga
, VGA_PEL_IR
) << 16;
271 val
|= vga_ioport_read(&s
->vga
, VGA_PEL_IW
) & 0xff;
274 val
= vga_ioport_read(&s
->vga
, VGA_PEL_D
);
277 val
= s
->vga
.vram_size
;
284 val
= 64; /* free CMDFIFO entries */
286 case CRTC_H_TOTAL_DISP
:
287 val
= s
->regs
.crtc_h_total_disp
;
289 case CRTC_H_SYNC_STRT_WID
:
290 val
= s
->regs
.crtc_h_sync_strt_wid
;
292 case CRTC_V_TOTAL_DISP
:
293 val
= s
->regs
.crtc_v_total_disp
;
295 case CRTC_V_SYNC_STRT_WID
:
296 val
= s
->regs
.crtc_v_sync_strt_wid
;
299 val
= s
->regs
.crtc_offset
;
301 case CRTC_OFFSET_CNTL
:
302 val
= s
->regs
.crtc_offset_cntl
;
305 val
= s
->regs
.crtc_pitch
;
307 case 0xf00 ... 0xfff:
308 val
= pci_default_read_config(&s
->dev
, addr
- 0xf00, size
);
311 val
= s
->regs
.cur_offset
;
313 case CUR_HORZ_VERT_POSN
:
314 val
= s
->regs
.cur_hv_pos
;
315 val
|= s
->regs
.cur_offset
& BIT(31);
317 case CUR_HORZ_VERT_OFF
:
318 val
= s
->regs
.cur_hv_offs
;
319 val
|= s
->regs
.cur_offset
& BIT(31);
322 val
= s
->regs
.cur_color0
;
325 val
= s
->regs
.cur_color1
;
328 val
= s
->regs
.dst_offset
;
331 val
= s
->regs
.dst_pitch
;
332 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
333 val
&= s
->regs
.dst_tile
<< 16;
337 val
= s
->regs
.dst_width
;
340 val
= s
->regs
.dst_height
;
354 case DP_GUI_MASTER_CNTL
:
355 val
= s
->regs
.dp_gui_master_cntl
;
358 val
= s
->regs
.src_offset
;
361 val
= s
->regs
.src_pitch
;
362 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
363 val
&= s
->regs
.src_tile
<< 16;
366 case DP_BRUSH_BKGD_CLR
:
367 val
= s
->regs
.dp_brush_bkgd_clr
;
369 case DP_BRUSH_FRGD_CLR
:
370 val
= s
->regs
.dp_brush_frgd_clr
;
372 case DP_SRC_FRGD_CLR
:
373 val
= s
->regs
.dp_src_frgd_clr
;
375 case DP_SRC_BKGD_CLR
:
376 val
= s
->regs
.dp_src_bkgd_clr
;
379 val
= s
->regs
.dp_cntl
;
382 val
= s
->regs
.dp_datatype
;
385 val
= s
->regs
.dp_mix
;
388 val
= s
->regs
.dp_write_mask
;
391 val
= s
->regs
.default_offset
;
394 val
= s
->regs
.default_pitch
;
396 case DEFAULT_SC_BOTTOM_RIGHT
:
397 val
= s
->regs
.default_sc_bottom_right
;
402 if (addr
< CUR_OFFSET
|| addr
> CUR_CLR1
|| ATI_DEBUG_HW_CURSOR
) {
403 trace_ati_mm_read(size
, addr
, ati_reg_name(addr
& ~3ULL), val
);
408 static inline void ati_reg_write_offs(uint32_t *reg
, int offs
,
409 uint64_t data
, unsigned int size
)
411 if (offs
== 0 && size
== 4) {
414 *reg
= deposit32(*reg
, offs
* BITS_PER_BYTE
, size
* BITS_PER_BYTE
,
419 static void ati_mm_write(void *opaque
, hwaddr addr
,
420 uint64_t data
, unsigned int size
)
422 ATIVGAState
*s
= opaque
;
424 if (addr
< CUR_OFFSET
|| addr
> CUR_CLR1
|| ATI_DEBUG_HW_CURSOR
) {
425 trace_ati_mm_write(size
, addr
, ati_reg_name(addr
& ~3ULL), data
);
429 s
->regs
.mm_index
= data
;
431 case MM_DATA
... MM_DATA
+ 3:
432 /* indexed access to regs or memory */
433 if (s
->regs
.mm_index
& BIT(31)) {
434 uint32_t idx
= s
->regs
.mm_index
& ~BIT(31);
435 if (idx
<= s
->vga
.vram_size
- size
) {
436 stn_le_p(s
->vga
.vram_ptr
+ idx
, size
, data
);
439 ati_mm_write(s
, s
->regs
.mm_index
+ addr
- MM_DATA
, data
, size
);
442 case BIOS_0_SCRATCH
... BUS_CNTL
- 1:
444 int i
= (addr
- BIOS_0_SCRATCH
) / 4;
445 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
&& i
> 3) {
448 ati_reg_write_offs(&s
->regs
.bios_scratch
[i
],
449 addr
- (BIOS_0_SCRATCH
+ i
* 4), data
, size
);
452 case CRTC_GEN_CNTL
... CRTC_GEN_CNTL
+ 3:
454 uint32_t val
= s
->regs
.crtc_gen_cntl
;
455 ati_reg_write_offs(&s
->regs
.crtc_gen_cntl
,
456 addr
- CRTC_GEN_CNTL
, data
, size
);
457 if ((val
& CRTC2_CUR_EN
) != (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
)) {
458 if (s
->cursor_guest_mode
) {
459 s
->vga
.force_shadow
= !!(s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
);
461 if (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) {
462 ati_cursor_define(s
);
464 dpy_mouse_set(s
->vga
.con
, s
->regs
.cur_hv_pos
>> 16,
465 s
->regs
.cur_hv_pos
& 0xffff,
466 (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) != 0);
469 if ((val
& (CRTC2_EXT_DISP_EN
| CRTC2_EN
)) !=
470 (s
->regs
.crtc_gen_cntl
& (CRTC2_EXT_DISP_EN
| CRTC2_EN
))) {
471 ati_vga_switch_mode(s
);
475 case CRTC_EXT_CNTL
... CRTC_EXT_CNTL
+ 3:
477 uint32_t val
= s
->regs
.crtc_ext_cntl
;
478 ati_reg_write_offs(&s
->regs
.crtc_ext_cntl
,
479 addr
- CRTC_EXT_CNTL
, data
, size
);
480 if (s
->regs
.crtc_ext_cntl
& CRT_CRTC_DISPLAY_DIS
) {
481 DPRINTF("Display disabled\n");
482 s
->vga
.ar_index
&= ~BIT(5);
484 DPRINTF("Display enabled\n");
485 s
->vga
.ar_index
|= BIT(5);
486 ati_vga_switch_mode(s
);
488 if ((val
& CRT_CRTC_DISPLAY_DIS
) !=
489 (s
->regs
.crtc_ext_cntl
& CRT_CRTC_DISPLAY_DIS
)) {
490 ati_vga_switch_mode(s
);
495 s
->regs
.dac_cntl
= data
& 0xffffe3ff;
496 s
->vga
.dac_8bit
= !!(data
& DAC_8BIT_EN
);
498 /* case GPIO_MONID: FIXME hook up DDC I2C here */
499 case PALETTE_INDEX
... PALETTE_INDEX
+ 3:
501 vga_ioport_write(&s
->vga
, VGA_PEL_IR
, (data
>> 16) & 0xff);
502 vga_ioport_write(&s
->vga
, VGA_PEL_IW
, data
& 0xff);
504 if (addr
== PALETTE_INDEX
) {
505 vga_ioport_write(&s
->vga
, VGA_PEL_IW
, data
& 0xff);
507 vga_ioport_write(&s
->vga
, VGA_PEL_IR
, data
& 0xff);
511 case PALETTE_DATA
... PALETTE_DATA
+ 3:
512 data
<<= addr
- PALETTE_DATA
;
513 data
= bswap32(data
) >> 8;
514 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
516 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
518 vga_ioport_write(&s
->vga
, VGA_PEL_D
, data
& 0xff);
520 case CRTC_H_TOTAL_DISP
:
521 s
->regs
.crtc_h_total_disp
= data
& 0x07ff07ff;
523 case CRTC_H_SYNC_STRT_WID
:
524 s
->regs
.crtc_h_sync_strt_wid
= data
& 0x17bf1fff;
526 case CRTC_V_TOTAL_DISP
:
527 s
->regs
.crtc_v_total_disp
= data
& 0x0fff0fff;
529 case CRTC_V_SYNC_STRT_WID
:
530 s
->regs
.crtc_v_sync_strt_wid
= data
& 0x9f0fff;
533 s
->regs
.crtc_offset
= data
& 0xc7ffffff;
535 case CRTC_OFFSET_CNTL
:
536 s
->regs
.crtc_offset_cntl
= data
; /* FIXME */
539 s
->regs
.crtc_pitch
= data
& 0x07ff07ff;
541 case 0xf00 ... 0xfff:
542 /* read-only copy of PCI config space so ignore writes */
545 if (s
->regs
.cur_offset
!= (data
& 0x87fffff0)) {
546 s
->regs
.cur_offset
= data
& 0x87fffff0;
547 ati_cursor_define(s
);
550 case CUR_HORZ_VERT_POSN
:
551 s
->regs
.cur_hv_pos
= data
& 0x3fff0fff;
552 if (data
& BIT(31)) {
553 s
->regs
.cur_offset
|= data
& BIT(31);
554 } else if (s
->regs
.cur_offset
& BIT(31)) {
555 s
->regs
.cur_offset
&= ~BIT(31);
556 ati_cursor_define(s
);
558 if (!s
->cursor_guest_mode
&&
559 (s
->regs
.crtc_gen_cntl
& CRTC2_CUR_EN
) && !(data
& BIT(31))) {
560 dpy_mouse_set(s
->vga
.con
, s
->regs
.cur_hv_pos
>> 16,
561 s
->regs
.cur_hv_pos
& 0xffff, 1);
564 case CUR_HORZ_VERT_OFF
:
565 s
->regs
.cur_hv_offs
= data
& 0x3f003f;
566 if (data
& BIT(31)) {
567 s
->regs
.cur_offset
|= data
& BIT(31);
568 } else if (s
->regs
.cur_offset
& BIT(31)) {
569 s
->regs
.cur_offset
&= ~BIT(31);
570 ati_cursor_define(s
);
574 if (s
->regs
.cur_color0
!= (data
& 0xffffff)) {
575 s
->regs
.cur_color0
= data
& 0xffffff;
576 ati_cursor_define(s
);
581 * Update cursor unconditionally here because some clients set up
582 * other registers before actually writing cursor data to memory at
583 * offset so we would miss cursor change unless always updating here
585 s
->regs
.cur_color1
= data
& 0xffffff;
586 ati_cursor_define(s
);
589 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
590 s
->regs
.dst_offset
= data
& 0xfffffff0;
592 s
->regs
.dst_offset
= data
& 0xfffffc00;
596 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
597 s
->regs
.dst_pitch
= data
& 0x3fff;
598 s
->regs
.dst_tile
= (data
>> 16) & 1;
600 s
->regs
.dst_pitch
= data
& 0x3ff0;
604 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RADEON_QY
) {
605 s
->regs
.dst_tile
= data
& 3;
609 s
->regs
.dst_width
= data
& 0x3fff;
613 s
->regs
.dst_height
= data
& 0x3fff;
616 s
->regs
.src_x
= data
& 0x3fff;
619 s
->regs
.src_y
= data
& 0x3fff;
622 s
->regs
.dst_x
= data
& 0x3fff;
625 s
->regs
.dst_y
= data
& 0x3fff;
627 case SRC_PITCH_OFFSET
:
628 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
629 s
->regs
.src_offset
= (data
& 0x1fffff) << 5;
630 s
->regs
.src_pitch
= (data
>> 21) & 0x3ff;
631 s
->regs
.src_tile
= data
>> 31;
633 s
->regs
.src_offset
= (data
& 0x3fffff) << 11;
634 s
->regs
.src_pitch
= (data
& 0x3fc00000) >> 16;
635 s
->regs
.src_tile
= (data
>> 30) & 1;
638 case DST_PITCH_OFFSET
:
639 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
640 s
->regs
.dst_offset
= (data
& 0x1fffff) << 5;
641 s
->regs
.dst_pitch
= (data
>> 21) & 0x3ff;
642 s
->regs
.dst_tile
= data
>> 31;
644 s
->regs
.dst_offset
= (data
& 0x3fffff) << 11;
645 s
->regs
.dst_pitch
= (data
& 0x3fc00000) >> 16;
646 s
->regs
.dst_tile
= data
>> 30;
650 s
->regs
.src_x
= data
& 0x3fff;
651 s
->regs
.src_y
= (data
>> 16) & 0x3fff;
654 s
->regs
.dst_x
= data
& 0x3fff;
655 s
->regs
.dst_y
= (data
>> 16) & 0x3fff;
657 case DST_HEIGHT_WIDTH
:
658 s
->regs
.dst_width
= data
& 0x3fff;
659 s
->regs
.dst_height
= (data
>> 16) & 0x3fff;
662 case DP_GUI_MASTER_CNTL
:
663 s
->regs
.dp_gui_master_cntl
= data
& 0xf800000f;
664 s
->regs
.dp_datatype
= (data
& 0x0f00) >> 8 | (data
& 0x30f0) << 4 |
665 (data
& 0x4000) << 16;
666 s
->regs
.dp_mix
= (data
& GMC_ROP3_MASK
) | (data
& 0x7000000) >> 16;
669 s
->regs
.dst_x
= data
& 0x3fff;
670 s
->regs
.dst_width
= (data
>> 16) & 0x3fff;
674 s
->regs
.src_y
= data
& 0x3fff;
675 s
->regs
.src_x
= (data
>> 16) & 0x3fff;
678 s
->regs
.dst_y
= data
& 0x3fff;
679 s
->regs
.dst_x
= (data
>> 16) & 0x3fff;
681 case DST_WIDTH_HEIGHT
:
682 s
->regs
.dst_height
= data
& 0x3fff;
683 s
->regs
.dst_width
= (data
>> 16) & 0x3fff;
687 s
->regs
.dst_y
= data
& 0x3fff;
688 s
->regs
.dst_height
= (data
>> 16) & 0x3fff;
691 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
692 s
->regs
.src_offset
= data
& 0xfffffff0;
694 s
->regs
.src_offset
= data
& 0xfffffc00;
698 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
699 s
->regs
.src_pitch
= data
& 0x3fff;
700 s
->regs
.src_tile
= (data
>> 16) & 1;
702 s
->regs
.src_pitch
= data
& 0x3ff0;
705 case DP_BRUSH_BKGD_CLR
:
706 s
->regs
.dp_brush_bkgd_clr
= data
;
708 case DP_BRUSH_FRGD_CLR
:
709 s
->regs
.dp_brush_frgd_clr
= data
;
712 s
->regs
.dp_cntl
= data
;
715 s
->regs
.dp_datatype
= data
& 0xe0070f0f;
718 s
->regs
.dp_mix
= data
& 0x00ff0700;
721 s
->regs
.dp_write_mask
= data
;
724 data
&= (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
?
725 0x03fffc00 : 0xfffffc00);
726 s
->regs
.default_offset
= data
;
729 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
730 s
->regs
.default_pitch
= data
& 0x103ff;
733 case DEFAULT_SC_BOTTOM_RIGHT
:
734 s
->regs
.default_sc_bottom_right
= data
& 0x3fff3fff;
741 static const MemoryRegionOps ati_mm_ops
= {
743 .write
= ati_mm_write
,
744 .endianness
= DEVICE_LITTLE_ENDIAN
,
747 static void ati_vga_realize(PCIDevice
*dev
, Error
**errp
)
749 ATIVGAState
*s
= ATI_VGA(dev
);
750 VGACommonState
*vga
= &s
->vga
;
754 for (i
= 0; i
< ARRAY_SIZE(ati_model_aliases
); i
++) {
755 if (!strcmp(s
->model
, ati_model_aliases
[i
].name
)) {
756 s
->dev_id
= ati_model_aliases
[i
].dev_id
;
760 if (i
>= ARRAY_SIZE(ati_model_aliases
)) {
761 warn_report("Unknown ATI VGA model name, "
762 "using default rage128p");
765 if (s
->dev_id
!= PCI_DEVICE_ID_ATI_RAGE128_PF
&&
766 s
->dev_id
!= PCI_DEVICE_ID_ATI_RADEON_QY
) {
767 error_setg(errp
, "Unknown ATI VGA device id, "
768 "only 0x5046 and 0x5159 are supported");
771 pci_set_word(dev
->config
+ PCI_DEVICE_ID
, s
->dev_id
);
773 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RADEON_QY
&&
774 s
->vga
.vram_size_mb
< 16) {
775 warn_report("Too small video memory for device id");
776 s
->vga
.vram_size_mb
= 16;
780 vga_common_init(vga
, OBJECT(s
));
781 vga_init(vga
, OBJECT(s
), pci_address_space(dev
),
782 pci_address_space_io(dev
), true);
783 vga
->con
= graphic_console_init(DEVICE(s
), 0, s
->vga
.hw_ops
, &s
->vga
);
784 if (s
->cursor_guest_mode
) {
785 vga
->cursor_invalidate
= ati_cursor_invalidate
;
786 vga
->cursor_draw_line
= ati_cursor_draw_line
;
789 /* mmio register space */
790 memory_region_init_io(&s
->mm
, OBJECT(s
), &ati_mm_ops
, s
,
791 "ati.mmregs", 0x4000);
792 /* io space is alias to beginning of mmregs */
793 memory_region_init_alias(&s
->io
, OBJECT(s
), "ati.io", &s
->mm
, 0, 0x100);
795 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_MEM_PREFETCH
, &vga
->vram
);
796 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io
);
797 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mm
);
800 static void ati_vga_reset(DeviceState
*dev
)
802 ATIVGAState
*s
= ATI_VGA(dev
);
805 vga_common_reset(&s
->vga
);
809 static void ati_vga_exit(PCIDevice
*dev
)
811 ATIVGAState
*s
= ATI_VGA(dev
);
813 graphic_console_close(s
->vga
.con
);
816 static Property ati_vga_properties
[] = {
817 DEFINE_PROP_UINT32("vgamem_mb", ATIVGAState
, vga
.vram_size_mb
, 16),
818 DEFINE_PROP_STRING("model", ATIVGAState
, model
),
819 DEFINE_PROP_UINT16("x-device-id", ATIVGAState
, dev_id
,
820 PCI_DEVICE_ID_ATI_RAGE128_PF
),
821 DEFINE_PROP_BOOL("guest_hwcursor", ATIVGAState
, cursor_guest_mode
, false),
822 DEFINE_PROP_END_OF_LIST()
825 static void ati_vga_class_init(ObjectClass
*klass
, void *data
)
827 DeviceClass
*dc
= DEVICE_CLASS(klass
);
828 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
830 dc
->reset
= ati_vga_reset
;
831 dc
->props
= ati_vga_properties
;
832 dc
->hotpluggable
= false;
833 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
835 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
836 k
->vendor_id
= PCI_VENDOR_ID_ATI
;
837 k
->device_id
= PCI_DEVICE_ID_ATI_RAGE128_PF
;
838 k
->romfile
= "vgabios-stdvga.bin";
839 k
->realize
= ati_vga_realize
;
840 k
->exit
= ati_vga_exit
;
843 static const TypeInfo ati_vga_info
= {
844 .name
= TYPE_ATI_VGA
,
845 .parent
= TYPE_PCI_DEVICE
,
846 .instance_size
= sizeof(ATIVGAState
),
847 .class_init
= ati_vga_class_init
,
848 .interfaces
= (InterfaceInfo
[]) {
849 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
854 static void ati_vga_register_types(void)
856 type_register_static(&ati_vga_info
);
859 type_init(ati_vga_register_types
)