Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / display / virtio-vga.c
1 #include "hw/hw.h"
2 #include "hw/pci/pci.h"
3 #include "ui/console.h"
4 #include "vga_int.h"
5 #include "hw/virtio/virtio-pci.h"
6
7 /*
8  * virtio-vga: This extends VirtioPCIProxy.
9  */
10 #define TYPE_VIRTIO_VGA "virtio-vga"
11 #define VIRTIO_VGA(obj) \
12         OBJECT_CHECK(VirtIOVGA, (obj), TYPE_VIRTIO_VGA)
13
14 typedef struct VirtIOVGA {
15     VirtIOPCIProxy parent_obj;
16     VirtIOGPU      vdev;
17     VGACommonState vga;
18     MemoryRegion   vga_mrs[3];
19 } VirtIOVGA;
20
21 static void virtio_vga_invalidate_display(void *opaque)
22 {
23     VirtIOVGA *vvga = opaque;
24
25     if (vvga->vdev.enable) {
26         virtio_gpu_ops.invalidate(&vvga->vdev);
27     } else {
28         vvga->vga.hw_ops->invalidate(&vvga->vga);
29     }
30 }
31
32 static void virtio_vga_update_display(void *opaque)
33 {
34     VirtIOVGA *vvga = opaque;
35
36     if (vvga->vdev.enable) {
37         virtio_gpu_ops.gfx_update(&vvga->vdev);
38     } else {
39         vvga->vga.hw_ops->gfx_update(&vvga->vga);
40     }
41 }
42
43 static void virtio_vga_text_update(void *opaque, console_ch_t *chardata)
44 {
45     VirtIOVGA *vvga = opaque;
46
47     if (vvga->vdev.enable) {
48         if (virtio_gpu_ops.text_update) {
49             virtio_gpu_ops.text_update(&vvga->vdev, chardata);
50         }
51     } else {
52         if (vvga->vga.hw_ops->text_update) {
53             vvga->vga.hw_ops->text_update(&vvga->vga, chardata);
54         }
55     }
56 }
57
58 static int virtio_vga_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
59 {
60     VirtIOVGA *vvga = opaque;
61
62     if (virtio_gpu_ops.ui_info) {
63         return virtio_gpu_ops.ui_info(&vvga->vdev, idx, info);
64     }
65     return -1;
66 }
67
68 static const GraphicHwOps virtio_vga_ops = {
69     .invalidate = virtio_vga_invalidate_display,
70     .gfx_update = virtio_vga_update_display,
71     .text_update = virtio_vga_text_update,
72     .ui_info = virtio_vga_ui_info,
73 };
74
75 /* VGA device wrapper around PCI device around virtio GPU */
76 static void virtio_vga_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
77 {
78     VirtIOVGA *vvga = VIRTIO_VGA(vpci_dev);
79     VirtIOGPU *g = &vvga->vdev;
80     VGACommonState *vga = &vvga->vga;
81     uint32_t offset;
82     int i;
83
84     /* init vga compat bits */
85     vga->vram_size_mb = 8;
86     vga_common_init(vga, OBJECT(vpci_dev), false);
87     vga_init(vga, OBJECT(vpci_dev), pci_address_space(&vpci_dev->pci_dev),
88              pci_address_space_io(&vpci_dev->pci_dev), true);
89     pci_register_bar(&vpci_dev->pci_dev, 0,
90                      PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram);
91
92     /*
93      * Configure virtio bar and regions
94      *
95      * We use bar #2 for the mmio regions, to be compatible with stdvga.
96      * virtio regions are moved to the end of bar #2, to make room for
97      * the stdvga mmio registers at the start of bar #2.
98      */
99     vpci_dev->modern_mem_bar = 2;
100     vpci_dev->msix_bar = 4;
101     offset = memory_region_size(&vpci_dev->modern_bar);
102     offset -= vpci_dev->notify.size;
103     vpci_dev->notify.offset = offset;
104     offset -= vpci_dev->device.size;
105     vpci_dev->device.offset = offset;
106     offset -= vpci_dev->isr.size;
107     vpci_dev->isr.offset = offset;
108     offset -= vpci_dev->common.size;
109     vpci_dev->common.offset = offset;
110
111     /* init virtio bits */
112     qdev_set_parent_bus(DEVICE(g), BUS(&vpci_dev->bus));
113     /* force virtio-1.0 */
114     vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
115     vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
116     object_property_set_bool(OBJECT(g), true, "realized", errp);
117
118     /* add stdvga mmio regions */
119     pci_std_vga_mmio_region_init(vga, &vpci_dev->modern_bar,
120                                  vvga->vga_mrs, true);
121
122     vga->con = g->scanout[0].con;
123     graphic_console_set_hwops(vga->con, &virtio_vga_ops, vvga);
124
125     for (i = 0; i < g->conf.max_outputs; i++) {
126         object_property_set_link(OBJECT(g->scanout[i].con),
127                                  OBJECT(vpci_dev),
128                                  "device", errp);
129     }
130 }
131
132 static void virtio_vga_reset(DeviceState *dev)
133 {
134     VirtIOVGA *vvga = VIRTIO_VGA(dev);
135     vvga->vdev.enable = 0;
136
137     vga_dirty_log_start(&vvga->vga);
138 }
139
140 static Property virtio_vga_properties[] = {
141     DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy),
142     DEFINE_PROP_END_OF_LIST(),
143 };
144
145 static void virtio_vga_class_init(ObjectClass *klass, void *data)
146 {
147     DeviceClass *dc = DEVICE_CLASS(klass);
148     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
149     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
150
151     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
152     dc->props = virtio_vga_properties;
153     dc->reset = virtio_vga_reset;
154     dc->hotpluggable = false;
155
156     k->realize = virtio_vga_realize;
157     pcidev_k->romfile = "vgabios-virtio.bin";
158     pcidev_k->class_id = PCI_CLASS_DISPLAY_VGA;
159 }
160
161 static void virtio_vga_inst_initfn(Object *obj)
162 {
163     VirtIOVGA *dev = VIRTIO_VGA(obj);
164
165     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
166                                 TYPE_VIRTIO_GPU);
167 }
168
169 static TypeInfo virtio_vga_info = {
170     .name          = TYPE_VIRTIO_VGA,
171     .parent        = TYPE_VIRTIO_PCI,
172     .instance_size = sizeof(struct VirtIOVGA),
173     .instance_init = virtio_vga_inst_initfn,
174     .class_init    = virtio_vga_class_init,
175 };
176
177 static void virtio_vga_register_types(void)
178 {
179     type_register_static(&virtio_vga_info);
180 }
181
182 type_init(virtio_vga_register_types)