2 * QEMU PCI bochs display adapter.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
8 #include "qemu/osdep.h"
9 #include "qemu/module.h"
10 #include "qemu/units.h"
11 #include "hw/pci/pci_device.h"
12 #include "hw/qdev-properties.h"
13 #include "migration/vmstate.h"
14 #include "hw/display/bochs-vbe.h"
15 #include "hw/display/edid.h"
17 #include "qapi/error.h"
19 #include "ui/console.h"
20 #include "ui/qemu-pixman.h"
21 #include "qom/object.h"
23 typedef struct BochsDisplayMode
{
24 pixman_format_code_t format
;
33 struct BochsDisplayState
{
48 qemu_edid_info edid_info
;
49 uint8_t edid_blob
[256];
51 /* device registers */
52 uint16_t vbe_regs
[VBE_DISPI_INDEX_NB
];
56 BochsDisplayMode mode
;
59 #define TYPE_BOCHS_DISPLAY "bochs-display"
60 OBJECT_DECLARE_SIMPLE_TYPE(BochsDisplayState
, BOCHS_DISPLAY
)
62 static const VMStateDescription vmstate_bochs_display
= {
63 .name
= "bochs-display",
64 .fields
= (const VMStateField
[]) {
65 VMSTATE_PCI_DEVICE(pci
, BochsDisplayState
),
66 VMSTATE_UINT16_ARRAY(vbe_regs
, BochsDisplayState
, VBE_DISPI_INDEX_NB
),
67 VMSTATE_BOOL(big_endian_fb
, BochsDisplayState
),
72 static uint64_t bochs_display_vbe_read(void *ptr
, hwaddr addr
,
75 BochsDisplayState
*s
= ptr
;
76 unsigned int index
= addr
>> 1;
79 case VBE_DISPI_INDEX_ID
:
81 case VBE_DISPI_INDEX_VIDEO_MEMORY_64K
:
82 return s
->vgamem
/ (64 * KiB
);
85 if (index
>= ARRAY_SIZE(s
->vbe_regs
)) {
88 return s
->vbe_regs
[index
];
91 static void bochs_display_vbe_write(void *ptr
, hwaddr addr
,
92 uint64_t val
, unsigned size
)
94 BochsDisplayState
*s
= ptr
;
95 unsigned int index
= addr
>> 1;
97 if (index
>= ARRAY_SIZE(s
->vbe_regs
)) {
100 s
->vbe_regs
[index
] = val
;
103 static const MemoryRegionOps bochs_display_vbe_ops
= {
104 .read
= bochs_display_vbe_read
,
105 .write
= bochs_display_vbe_write
,
106 .valid
.min_access_size
= 1,
107 .valid
.max_access_size
= 4,
108 .impl
.min_access_size
= 2,
109 .impl
.max_access_size
= 2,
110 .endianness
= DEVICE_LITTLE_ENDIAN
,
113 static uint64_t bochs_display_qext_read(void *ptr
, hwaddr addr
,
116 BochsDisplayState
*s
= ptr
;
119 case PCI_VGA_QEXT_REG_SIZE
:
120 return PCI_VGA_QEXT_SIZE
;
121 case PCI_VGA_QEXT_REG_BYTEORDER
:
122 return s
->big_endian_fb
?
123 PCI_VGA_QEXT_BIG_ENDIAN
: PCI_VGA_QEXT_LITTLE_ENDIAN
;
129 static void bochs_display_qext_write(void *ptr
, hwaddr addr
,
130 uint64_t val
, unsigned size
)
132 BochsDisplayState
*s
= ptr
;
135 case PCI_VGA_QEXT_REG_BYTEORDER
:
136 if (val
== PCI_VGA_QEXT_BIG_ENDIAN
) {
137 s
->big_endian_fb
= true;
139 if (val
== PCI_VGA_QEXT_LITTLE_ENDIAN
) {
140 s
->big_endian_fb
= false;
146 static const MemoryRegionOps bochs_display_qext_ops
= {
147 .read
= bochs_display_qext_read
,
148 .write
= bochs_display_qext_write
,
149 .valid
.min_access_size
= 4,
150 .valid
.max_access_size
= 4,
151 .endianness
= DEVICE_LITTLE_ENDIAN
,
154 static int bochs_display_get_mode(BochsDisplayState
*s
,
155 BochsDisplayMode
*mode
)
157 uint16_t *vbe
= s
->vbe_regs
;
160 if (!(vbe
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
164 memset(mode
, 0, sizeof(*mode
));
165 switch (vbe
[VBE_DISPI_INDEX_BPP
]) {
167 /* best effort: support native endianness only */
168 mode
->format
= PIXMAN_r5g6b5
;
172 mode
->format
= s
->big_endian_fb
174 : PIXMAN_LE_x8r8g8b8
;
181 mode
->width
= vbe
[VBE_DISPI_INDEX_XRES
];
182 mode
->height
= vbe
[VBE_DISPI_INDEX_YRES
];
183 virt_width
= vbe
[VBE_DISPI_INDEX_VIRT_WIDTH
];
184 if (virt_width
< mode
->width
) {
185 virt_width
= mode
->width
;
187 mode
->stride
= virt_width
* mode
->bytepp
;
188 mode
->size
= (uint64_t)mode
->stride
* mode
->height
;
189 mode
->offset
= ((uint64_t)vbe
[VBE_DISPI_INDEX_X_OFFSET
] * mode
->bytepp
+
190 (uint64_t)vbe
[VBE_DISPI_INDEX_Y_OFFSET
] * mode
->stride
);
192 if (mode
->width
< 64 || mode
->height
< 64) {
195 if (mode
->offset
+ mode
->size
> s
->vgamem
) {
201 static void bochs_display_update(void *opaque
)
203 BochsDisplayState
*s
= opaque
;
204 DirtyBitmapSnapshot
*snap
= NULL
;
205 bool full_update
= false;
206 BochsDisplayMode mode
;
212 ret
= bochs_display_get_mode(s
, &mode
);
214 /* no (valid) video mode */
218 if (memcmp(&s
->mode
, &mode
, sizeof(mode
)) != 0) {
219 /* video mode switch */
221 ptr
= memory_region_get_ram_ptr(&s
->vram
);
222 ds
= qemu_create_displaysurface_from(mode
.width
,
227 dpy_gfx_replace_surface(s
->con
, ds
);
232 dpy_gfx_update_full(s
->con
);
234 snap
= memory_region_snapshot_and_clear_dirty(&s
->vram
,
235 mode
.offset
, mode
.size
,
238 for (y
= 0; y
< mode
.height
; y
++) {
239 dirty
= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
240 mode
.offset
+ mode
.stride
* y
,
242 if (dirty
&& ys
< 0) {
245 if (!dirty
&& ys
>= 0) {
246 dpy_gfx_update(s
->con
, 0, ys
,
252 dpy_gfx_update(s
->con
, 0, ys
,
260 static const GraphicHwOps bochs_display_gfx_ops
= {
261 .gfx_update
= bochs_display_update
,
264 static void bochs_display_realize(PCIDevice
*dev
, Error
**errp
)
266 BochsDisplayState
*s
= BOCHS_DISPLAY(dev
);
267 Object
*obj
= OBJECT(dev
);
270 if (s
->vgamem
< 4 * MiB
) {
271 error_setg(errp
, "bochs-display: video memory too small");
274 if (s
->vgamem
> 256 * MiB
) {
275 error_setg(errp
, "bochs-display: video memory too big");
278 s
->vgamem
= pow2ceil(s
->vgamem
);
280 s
->con
= graphic_console_init(DEVICE(dev
), 0, &bochs_display_gfx_ops
, s
);
282 memory_region_init_ram(&s
->vram
, obj
, "bochs-display-vram", s
->vgamem
,
284 memory_region_init_io(&s
->vbe
, obj
, &bochs_display_vbe_ops
, s
,
285 "bochs dispi interface", PCI_VGA_BOCHS_SIZE
);
286 memory_region_init_io(&s
->qext
, obj
, &bochs_display_qext_ops
, s
,
287 "qemu extended regs", PCI_VGA_QEXT_SIZE
);
289 memory_region_init_io(&s
->mmio
, obj
, &unassigned_io_ops
, NULL
,
290 "bochs-display-mmio", PCI_VGA_MMIO_SIZE
);
291 memory_region_add_subregion(&s
->mmio
, PCI_VGA_BOCHS_OFFSET
, &s
->vbe
);
292 memory_region_add_subregion(&s
->mmio
, PCI_VGA_QEXT_OFFSET
, &s
->qext
);
294 pci_set_byte(&s
->pci
.config
[PCI_REVISION_ID
], 2);
295 pci_register_bar(&s
->pci
, 0, PCI_BASE_ADDRESS_MEM_PREFETCH
, &s
->vram
);
296 pci_register_bar(&s
->pci
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mmio
);
298 if (s
->enable_edid
) {
299 qemu_edid_generate(s
->edid_blob
, sizeof(s
->edid_blob
), &s
->edid_info
);
300 qemu_edid_region_io(&s
->edid
, obj
, s
->edid_blob
, sizeof(s
->edid_blob
));
301 memory_region_add_subregion(&s
->mmio
, 0, &s
->edid
);
304 if (pci_bus_is_express(pci_get_bus(dev
))) {
305 ret
= pcie_endpoint_cap_init(dev
, 0x80);
308 dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
311 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
314 static bool bochs_display_get_big_endian_fb(Object
*obj
, Error
**errp
)
316 BochsDisplayState
*s
= BOCHS_DISPLAY(obj
);
318 return s
->big_endian_fb
;
321 static void bochs_display_set_big_endian_fb(Object
*obj
, bool value
,
324 BochsDisplayState
*s
= BOCHS_DISPLAY(obj
);
326 s
->big_endian_fb
= value
;
329 static void bochs_display_init(Object
*obj
)
331 PCIDevice
*dev
= PCI_DEVICE(obj
);
333 /* Expose framebuffer byteorder via QOM */
334 object_property_add_bool(obj
, "big-endian-framebuffer",
335 bochs_display_get_big_endian_fb
,
336 bochs_display_set_big_endian_fb
);
338 dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
341 static void bochs_display_exit(PCIDevice
*dev
)
343 BochsDisplayState
*s
= BOCHS_DISPLAY(dev
);
345 graphic_console_close(s
->con
);
348 static Property bochs_display_properties
[] = {
349 DEFINE_PROP_SIZE("vgamem", BochsDisplayState
, vgamem
, 16 * MiB
),
350 DEFINE_PROP_BOOL("edid", BochsDisplayState
, enable_edid
, true),
351 DEFINE_EDID_PROPERTIES(BochsDisplayState
, edid_info
),
352 DEFINE_PROP_END_OF_LIST(),
355 static void bochs_display_class_init(ObjectClass
*klass
, void *data
)
357 DeviceClass
*dc
= DEVICE_CLASS(klass
);
358 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
360 k
->class_id
= PCI_CLASS_DISPLAY_OTHER
;
361 k
->vendor_id
= PCI_VENDOR_ID_QEMU
;
362 k
->device_id
= PCI_DEVICE_ID_QEMU_VGA
;
364 k
->realize
= bochs_display_realize
;
365 k
->romfile
= "vgabios-bochs-display.bin";
366 k
->exit
= bochs_display_exit
;
367 dc
->vmsd
= &vmstate_bochs_display
;
368 device_class_set_props(dc
, bochs_display_properties
);
369 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
372 static const TypeInfo bochs_display_type_info
= {
373 .name
= TYPE_BOCHS_DISPLAY
,
374 .parent
= TYPE_PCI_DEVICE
,
375 .instance_size
= sizeof(BochsDisplayState
),
376 .instance_init
= bochs_display_init
,
377 .class_init
= bochs_display_class_init
,
378 .interfaces
= (InterfaceInfo
[]) {
379 { INTERFACE_PCIE_DEVICE
},
380 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
385 static void bochs_display_register_types(void)
387 type_register_static(&bochs_display_type_info
);
390 type_init(bochs_display_register_types
)