]> git.proxmox.com Git - mirror_qemu.git/blob - hw/display/virtio-vga.c
virtio-gpu: fix crashes upon warm reboot with vga mode
[mirror_qemu.git] / hw / display / virtio-vga.c
1 #include "qemu/osdep.h"
2 #include "hw/hw.h"
3 #include "hw/pci/pci.h"
4 #include "vga_int.h"
5 #include "hw/virtio/virtio-pci.h"
6 #include "qapi/error.h"
7
8 /*
9 * virtio-vga: This extends VirtioPCIProxy.
10 */
11 #define TYPE_VIRTIO_VGA "virtio-vga"
12 #define VIRTIO_VGA(obj) \
13 OBJECT_CHECK(VirtIOVGA, (obj), TYPE_VIRTIO_VGA)
14
15 typedef struct VirtIOVGA {
16 VirtIOPCIProxy parent_obj;
17 VirtIOGPU vdev;
18 VGACommonState vga;
19 MemoryRegion vga_mrs[3];
20 } VirtIOVGA;
21
22 static void virtio_vga_invalidate_display(void *opaque)
23 {
24 VirtIOVGA *vvga = opaque;
25
26 if (vvga->vdev.enable) {
27 virtio_gpu_ops.invalidate(&vvga->vdev);
28 } else {
29 vvga->vga.hw_ops->invalidate(&vvga->vga);
30 }
31 }
32
33 static void virtio_vga_update_display(void *opaque)
34 {
35 VirtIOVGA *vvga = opaque;
36
37 if (vvga->vdev.enable) {
38 virtio_gpu_ops.gfx_update(&vvga->vdev);
39 } else {
40 vvga->vga.hw_ops->gfx_update(&vvga->vga);
41 }
42 }
43
44 static void virtio_vga_text_update(void *opaque, console_ch_t *chardata)
45 {
46 VirtIOVGA *vvga = opaque;
47
48 if (vvga->vdev.enable) {
49 if (virtio_gpu_ops.text_update) {
50 virtio_gpu_ops.text_update(&vvga->vdev, chardata);
51 }
52 } else {
53 if (vvga->vga.hw_ops->text_update) {
54 vvga->vga.hw_ops->text_update(&vvga->vga, chardata);
55 }
56 }
57 }
58
59 static int virtio_vga_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
60 {
61 VirtIOVGA *vvga = opaque;
62
63 if (virtio_gpu_ops.ui_info) {
64 return virtio_gpu_ops.ui_info(&vvga->vdev, idx, info);
65 }
66 return -1;
67 }
68
69 static void virtio_vga_gl_block(void *opaque, bool block)
70 {
71 VirtIOVGA *vvga = opaque;
72
73 if (virtio_gpu_ops.gl_block) {
74 virtio_gpu_ops.gl_block(&vvga->vdev, block);
75 }
76 }
77
78 static void virtio_vga_disable_scanout(VirtIOGPU *g, int scanout_id)
79 {
80 VirtIOVGA *vvga = container_of(g, VirtIOVGA, vdev);
81
82 if (scanout_id == 0) {
83 /* reset surface if needed */
84 vvga->vga.graphic_mode = -1;
85 }
86 }
87
88 static const GraphicHwOps virtio_vga_ops = {
89 .invalidate = virtio_vga_invalidate_display,
90 .gfx_update = virtio_vga_update_display,
91 .text_update = virtio_vga_text_update,
92 .ui_info = virtio_vga_ui_info,
93 .gl_block = virtio_vga_gl_block,
94 };
95
96 static const VMStateDescription vmstate_virtio_vga = {
97 .name = "virtio-vga",
98 .version_id = 2,
99 .minimum_version_id = 2,
100 .fields = (VMStateField[]) {
101 /* no pci stuff here, saving the virtio device will handle that */
102 VMSTATE_STRUCT(vga, VirtIOVGA, 0, vmstate_vga_common, VGACommonState),
103 VMSTATE_END_OF_LIST()
104 }
105 };
106
107 /* VGA device wrapper around PCI device around virtio GPU */
108 static void virtio_vga_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
109 {
110 VirtIOVGA *vvga = VIRTIO_VGA(vpci_dev);
111 VirtIOGPU *g = &vvga->vdev;
112 VGACommonState *vga = &vvga->vga;
113 Error *err = NULL;
114 uint32_t offset;
115 int i;
116
117 /* init vga compat bits */
118 vga->vram_size_mb = 8;
119 vga_common_init(vga, OBJECT(vpci_dev));
120 vga_init(vga, OBJECT(vpci_dev), pci_address_space(&vpci_dev->pci_dev),
121 pci_address_space_io(&vpci_dev->pci_dev), true);
122 pci_register_bar(&vpci_dev->pci_dev, 0,
123 PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
124
125 /*
126 * Configure virtio bar and regions
127 *
128 * We use bar #2 for the mmio regions, to be compatible with stdvga.
129 * virtio regions are moved to the end of bar #2, to make room for
130 * the stdvga mmio registers at the start of bar #2.
131 */
132 vpci_dev->modern_mem_bar_idx = 2;
133 vpci_dev->msix_bar_idx = 4;
134
135 if (!(vpci_dev->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ)) {
136 /*
137 * with page-per-vq=off there is no padding space we can use
138 * for the stdvga registers. Make the common and isr regions
139 * smaller then.
140 */
141 vpci_dev->common.size /= 2;
142 vpci_dev->isr.size /= 2;
143 }
144
145 offset = memory_region_size(&vpci_dev->modern_bar);
146 offset -= vpci_dev->notify.size;
147 vpci_dev->notify.offset = offset;
148 offset -= vpci_dev->device.size;
149 vpci_dev->device.offset = offset;
150 offset -= vpci_dev->isr.size;
151 vpci_dev->isr.offset = offset;
152 offset -= vpci_dev->common.size;
153 vpci_dev->common.offset = offset;
154
155 /* init virtio bits */
156 qdev_set_parent_bus(DEVICE(g), BUS(&vpci_dev->bus));
157 virtio_pci_force_virtio_1(vpci_dev);
158 object_property_set_bool(OBJECT(g), true, "realized", &err);
159 if (err) {
160 error_propagate(errp, err);
161 return;
162 }
163
164 /* add stdvga mmio regions */
165 pci_std_vga_mmio_region_init(vga, OBJECT(vvga), &vpci_dev->modern_bar,
166 vvga->vga_mrs, true);
167
168 vga->con = g->scanout[0].con;
169 g->disable_scanout = virtio_vga_disable_scanout;
170 graphic_console_set_hwops(vga->con, &virtio_vga_ops, vvga);
171
172 for (i = 0; i < g->conf.max_outputs; i++) {
173 object_property_set_link(OBJECT(g->scanout[i].con),
174 OBJECT(vpci_dev),
175 "device", errp);
176 }
177 }
178
179 static void virtio_vga_reset(DeviceState *dev)
180 {
181 VirtIOVGA *vvga = VIRTIO_VGA(dev);
182 vvga->vdev.enable = 0;
183
184 vga_dirty_log_start(&vvga->vga);
185 }
186
187 static Property virtio_vga_properties[] = {
188 DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy),
189 DEFINE_PROP_END_OF_LIST(),
190 };
191
192 static void virtio_vga_class_init(ObjectClass *klass, void *data)
193 {
194 DeviceClass *dc = DEVICE_CLASS(klass);
195 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
196 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
197
198 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
199 dc->props = virtio_vga_properties;
200 dc->reset = virtio_vga_reset;
201 dc->vmsd = &vmstate_virtio_vga;
202 dc->hotpluggable = false;
203
204 k->realize = virtio_vga_realize;
205 pcidev_k->romfile = "vgabios-virtio.bin";
206 pcidev_k->class_id = PCI_CLASS_DISPLAY_VGA;
207 }
208
209 static void virtio_vga_inst_initfn(Object *obj)
210 {
211 VirtIOVGA *dev = VIRTIO_VGA(obj);
212
213 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
214 TYPE_VIRTIO_GPU);
215 }
216
217 static TypeInfo virtio_vga_info = {
218 .name = TYPE_VIRTIO_VGA,
219 .parent = TYPE_VIRTIO_PCI,
220 .instance_size = sizeof(struct VirtIOVGA),
221 .instance_init = virtio_vga_inst_initfn,
222 .class_init = virtio_vga_class_init,
223 };
224
225 static void virtio_vga_register_types(void)
226 {
227 type_register_static(&virtio_vga_info);
228 }
229
230 type_init(virtio_vga_register_types)