1 #include "qemu/osdep.h" 2 #include "hw/hw.h" 3 #include "hw/pci/pci.h" 4 #include "ui/console.h" 5 #include "vga_int.h" 6 #include "hw/virtio/virtio-pci.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 const GraphicHwOps virtio_vga_ops = { 70 .invalidate = virtio_vga_invalidate_display, 71 .gfx_update = virtio_vga_update_display, 72 .text_update = virtio_vga_text_update, 73 .ui_info = virtio_vga_ui_info, 74 }; 75 76 /* VGA device wrapper around PCI device around virtio GPU */ 77 static void virtio_vga_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 78 { 79 VirtIOVGA *vvga = VIRTIO_VGA(vpci_dev); 80 VirtIOGPU *g = &vvga->vdev; 81 VGACommonState *vga = &vvga->vga; 82 uint32_t offset; 83 int i; 84 85 /* init vga compat bits */ 86 vga->vram_size_mb = 8; 87 vga_common_init(vga, OBJECT(vpci_dev), false); 88 vga_init(vga, OBJECT(vpci_dev), pci_address_space(&vpci_dev->pci_dev), 89 pci_address_space_io(&vpci_dev->pci_dev), true); 90 pci_register_bar(&vpci_dev->pci_dev, 0, 91 PCI_BASE_ADDRESS_MEM_PREFETCH, &vga->vram); 92 93 /* 94 * Configure virtio bar and regions 95 * 96 * We use bar #2 for the mmio regions, to be compatible with stdvga. 97 * virtio regions are moved to the end of bar #2, to make room for 98 * the stdvga mmio registers at the start of bar #2. 99 */ 100 vpci_dev->modern_mem_bar = 2; 101 vpci_dev->msix_bar = 4; 102 offset = memory_region_size(&vpci_dev->modern_bar); 103 offset -= vpci_dev->notify.size; 104 vpci_dev->notify.offset = offset; 105 offset -= vpci_dev->device.size; 106 vpci_dev->device.offset = offset; 107 offset -= vpci_dev->isr.size; 108 vpci_dev->isr.offset = offset; 109 offset -= vpci_dev->common.size; 110 vpci_dev->common.offset = offset; 111 112 /* init virtio bits */ 113 qdev_set_parent_bus(DEVICE(g), BUS(&vpci_dev->bus)); 114 /* force virtio-1.0 */ 115 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN; 116 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY; 117 object_property_set_bool(OBJECT(g), true, "realized", errp); 118 119 /* add stdvga mmio regions */ 120 pci_std_vga_mmio_region_init(vga, &vpci_dev->modern_bar, 121 vvga->vga_mrs, true); 122 123 vga->con = g->scanout[0].con; 124 graphic_console_set_hwops(vga->con, &virtio_vga_ops, vvga); 125 126 for (i = 0; i < g->conf.max_outputs; i++) { 127 object_property_set_link(OBJECT(g->scanout[i].con), 128 OBJECT(vpci_dev), 129 "device", errp); 130 } 131 } 132 133 static void virtio_vga_reset(DeviceState *dev) 134 { 135 VirtIOVGA *vvga = VIRTIO_VGA(dev); 136 vvga->vdev.enable = 0; 137 138 vga_dirty_log_start(&vvga->vga); 139 } 140 141 static Property virtio_vga_properties[] = { 142 DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy), 143 DEFINE_PROP_END_OF_LIST(), 144 }; 145 146 static void virtio_vga_class_init(ObjectClass *klass, void *data) 147 { 148 DeviceClass *dc = DEVICE_CLASS(klass); 149 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 150 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 151 152 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories); 153 dc->props = virtio_vga_properties; 154 dc->reset = virtio_vga_reset; 155 dc->hotpluggable = false; 156 157 k->realize = virtio_vga_realize; 158 pcidev_k->romfile = "vgabios-virtio.bin"; 159 pcidev_k->class_id = PCI_CLASS_DISPLAY_VGA; 160 } 161 162 static void virtio_vga_inst_initfn(Object *obj) 163 { 164 VirtIOVGA *dev = VIRTIO_VGA(obj); 165 166 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 167 TYPE_VIRTIO_GPU); 168 } 169 170 static TypeInfo virtio_vga_info = { 171 .name = TYPE_VIRTIO_VGA, 172 .parent = TYPE_VIRTIO_PCI, 173 .instance_size = sizeof(struct VirtIOVGA), 174 .instance_init = virtio_vga_inst_initfn, 175 .class_init = virtio_vga_class_init, 176 }; 177 178 static void virtio_vga_register_types(void) 179 { 180 type_register_static(&virtio_vga_info); 181 } 182 183 type_init(virtio_vga_register_types) 184