1 #include "qemu/osdep.h"
3 #include "hw/pci/pci.h"
4 #include "ui/console.h"
6 #include "hw/virtio/virtio-pci.h"
9 * virtio-vga: This extends VirtioPCIProxy.
11 #define TYPE_VIRTIO_VGA "virtio-vga"
12 #define VIRTIO_VGA(obj) \
13 OBJECT_CHECK(VirtIOVGA, (obj), TYPE_VIRTIO_VGA)
15 typedef struct VirtIOVGA
{
16 VirtIOPCIProxy parent_obj
;
19 MemoryRegion vga_mrs
[3];
22 static void virtio_vga_invalidate_display(void *opaque
)
24 VirtIOVGA
*vvga
= opaque
;
26 if (vvga
->vdev
.enable
) {
27 virtio_gpu_ops
.invalidate(&vvga
->vdev
);
29 vvga
->vga
.hw_ops
->invalidate(&vvga
->vga
);
33 static void virtio_vga_update_display(void *opaque
)
35 VirtIOVGA
*vvga
= opaque
;
37 if (vvga
->vdev
.enable
) {
38 virtio_gpu_ops
.gfx_update(&vvga
->vdev
);
40 vvga
->vga
.hw_ops
->gfx_update(&vvga
->vga
);
44 static void virtio_vga_text_update(void *opaque
, console_ch_t
*chardata
)
46 VirtIOVGA
*vvga
= opaque
;
48 if (vvga
->vdev
.enable
) {
49 if (virtio_gpu_ops
.text_update
) {
50 virtio_gpu_ops
.text_update(&vvga
->vdev
, chardata
);
53 if (vvga
->vga
.hw_ops
->text_update
) {
54 vvga
->vga
.hw_ops
->text_update(&vvga
->vga
, chardata
);
59 static int virtio_vga_ui_info(void *opaque
, uint32_t idx
, QemuUIInfo
*info
)
61 VirtIOVGA
*vvga
= opaque
;
63 if (virtio_gpu_ops
.ui_info
) {
64 return virtio_gpu_ops
.ui_info(&vvga
->vdev
, idx
, info
);
69 static void virtio_vga_gl_block(void *opaque
, bool block
)
71 VirtIOVGA
*vvga
= opaque
;
73 if (virtio_gpu_ops
.gl_block
) {
74 virtio_gpu_ops
.gl_block(&vvga
->vdev
, block
);
78 static const GraphicHwOps virtio_vga_ops
= {
79 .invalidate
= virtio_vga_invalidate_display
,
80 .gfx_update
= virtio_vga_update_display
,
81 .text_update
= virtio_vga_text_update
,
82 .ui_info
= virtio_vga_ui_info
,
83 .gl_block
= virtio_vga_gl_block
,
86 /* VGA device wrapper around PCI device around virtio GPU */
87 static void virtio_vga_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
89 VirtIOVGA
*vvga
= VIRTIO_VGA(vpci_dev
);
90 VirtIOGPU
*g
= &vvga
->vdev
;
91 VGACommonState
*vga
= &vvga
->vga
;
95 /* init vga compat bits */
96 vga
->vram_size_mb
= 8;
97 vga_common_init(vga
, OBJECT(vpci_dev
), false);
98 vga_init(vga
, OBJECT(vpci_dev
), pci_address_space(&vpci_dev
->pci_dev
),
99 pci_address_space_io(&vpci_dev
->pci_dev
), true);
100 pci_register_bar(&vpci_dev
->pci_dev
, 0,
101 PCI_BASE_ADDRESS_MEM_PREFETCH
, &vga
->vram
);
104 * Configure virtio bar and regions
106 * We use bar #2 for the mmio regions, to be compatible with stdvga.
107 * virtio regions are moved to the end of bar #2, to make room for
108 * the stdvga mmio registers at the start of bar #2.
110 vpci_dev
->modern_mem_bar
= 2;
111 vpci_dev
->msix_bar
= 4;
112 offset
= memory_region_size(&vpci_dev
->modern_bar
);
113 offset
-= vpci_dev
->notify
.size
;
114 vpci_dev
->notify
.offset
= offset
;
115 offset
-= vpci_dev
->device
.size
;
116 vpci_dev
->device
.offset
= offset
;
117 offset
-= vpci_dev
->isr
.size
;
118 vpci_dev
->isr
.offset
= offset
;
119 offset
-= vpci_dev
->common
.size
;
120 vpci_dev
->common
.offset
= offset
;
122 /* init virtio bits */
123 qdev_set_parent_bus(DEVICE(g
), BUS(&vpci_dev
->bus
));
124 /* force virtio-1.0 */
125 vpci_dev
->flags
&= ~VIRTIO_PCI_FLAG_DISABLE_MODERN
;
126 vpci_dev
->flags
|= VIRTIO_PCI_FLAG_DISABLE_LEGACY
;
127 object_property_set_bool(OBJECT(g
), true, "realized", errp
);
129 /* add stdvga mmio regions */
130 pci_std_vga_mmio_region_init(vga
, &vpci_dev
->modern_bar
,
131 vvga
->vga_mrs
, true);
133 vga
->con
= g
->scanout
[0].con
;
134 graphic_console_set_hwops(vga
->con
, &virtio_vga_ops
, vvga
);
136 for (i
= 0; i
< g
->conf
.max_outputs
; i
++) {
137 object_property_set_link(OBJECT(g
->scanout
[i
].con
),
143 static void virtio_vga_reset(DeviceState
*dev
)
145 VirtIOVGA
*vvga
= VIRTIO_VGA(dev
);
146 vvga
->vdev
.enable
= 0;
148 vga_dirty_log_start(&vvga
->vga
);
151 static Property virtio_vga_properties
[] = {
152 DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy
),
153 DEFINE_PROP_END_OF_LIST(),
156 static void virtio_vga_class_init(ObjectClass
*klass
, void *data
)
158 DeviceClass
*dc
= DEVICE_CLASS(klass
);
159 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
160 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
162 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
163 dc
->props
= virtio_vga_properties
;
164 dc
->reset
= virtio_vga_reset
;
165 dc
->hotpluggable
= false;
167 k
->realize
= virtio_vga_realize
;
168 pcidev_k
->romfile
= "vgabios-virtio.bin";
169 pcidev_k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
172 static void virtio_vga_inst_initfn(Object
*obj
)
174 VirtIOVGA
*dev
= VIRTIO_VGA(obj
);
176 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
180 static TypeInfo virtio_vga_info
= {
181 .name
= TYPE_VIRTIO_VGA
,
182 .parent
= TYPE_VIRTIO_PCI
,
183 .instance_size
= sizeof(struct VirtIOVGA
),
184 .instance_init
= virtio_vga_inst_initfn
,
185 .class_init
= virtio_vga_class_init
,
188 static void virtio_vga_register_types(void)
190 type_register_static(&virtio_vga_info
);
193 type_init(virtio_vga_register_types
)