| 1 | /* |
| 2 | * Virtio video device |
| 3 | * |
| 4 | * Copyright Red Hat |
| 5 | * |
| 6 | * Authors: |
| 7 | * Dave Airlie |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "qemu/osdep.h" |
| 15 | #include "qapi/error.h" |
| 16 | #include "qemu/module.h" |
| 17 | #include "hw/pci/pci.h" |
| 18 | #include "hw/core/qdev-properties.h" |
| 19 | #include "hw/virtio/virtio.h" |
| 20 | #include "hw/virtio/virtio-bus.h" |
| 21 | #include "hw/virtio/virtio-gpu-pci.h" |
| 22 | #include "qom/object.h" |
| 23 | |
| 24 | static const Property virtio_gpu_pci_base_properties[] = { |
| 25 | DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy), |
| 26 | }; |
| 27 | |
| 28 | static void virtio_gpu_pci_base_realize(VirtIOPCIProxy *vpci_dev, Error **errp) |
| 29 | { |
| 30 | VirtIOGPUPCIBase *vgpu = VIRTIO_GPU_PCI_BASE(vpci_dev); |
| 31 | VirtIOGPUBase *g = vgpu->vgpu; |
| 32 | DeviceState *vdev = DEVICE(g); |
| 33 | int i; |
| 34 | |
| 35 | if (virtio_gpu_hostmem_enabled(g->conf)) { |
| 36 | vpci_dev->msix_bar_idx = 1; |
| 37 | vpci_dev->modern_mem_bar_idx = 2; |
| 38 | memory_region_init(&g->hostmem, OBJECT(g), "virtio-gpu-hostmem", |
| 39 | g->conf.hostmem); |
| 40 | pci_register_bar(&vpci_dev->pci_dev, 4, |
| 41 | PCI_BASE_ADDRESS_SPACE_MEMORY | |
| 42 | PCI_BASE_ADDRESS_MEM_PREFETCH | |
| 43 | PCI_BASE_ADDRESS_MEM_TYPE_64, |
| 44 | &g->hostmem); |
| 45 | virtio_pci_add_shm_cap(vpci_dev, 4, 0, g->conf.hostmem, |
| 46 | VIRTIO_GPU_SHM_ID_HOST_VISIBLE); |
| 47 | } |
| 48 | |
| 49 | virtio_pci_force_virtio_1(vpci_dev); |
| 50 | if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | for (i = 0; i < g->conf.max_outputs; i++) { |
| 55 | object_property_set_link(OBJECT(g->scanout[i].con), "device", |
| 56 | OBJECT(vpci_dev), &error_abort); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void virtio_gpu_pci_base_class_init(ObjectClass *klass, const void *data) |
| 61 | { |
| 62 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 63 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); |
| 64 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
| 65 | |
| 66 | set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories); |
| 67 | device_class_set_props(dc, virtio_gpu_pci_base_properties); |
| 68 | dc->hotpluggable = false; |
| 69 | k->realize = virtio_gpu_pci_base_realize; |
| 70 | pcidev_k->class_id = PCI_CLASS_DISPLAY_OTHER; |
| 71 | } |
| 72 | |
| 73 | static const TypeInfo virtio_gpu_pci_base_info = { |
| 74 | .name = TYPE_VIRTIO_GPU_PCI_BASE, |
| 75 | .parent = TYPE_VIRTIO_PCI, |
| 76 | .instance_size = sizeof(VirtIOGPUPCIBase), |
| 77 | .class_init = virtio_gpu_pci_base_class_init, |
| 78 | .abstract = true |
| 79 | }; |
| 80 | module_obj(TYPE_VIRTIO_GPU_PCI_BASE); |
| 81 | module_kconfig(VIRTIO_PCI); |
| 82 | |
| 83 | #define TYPE_VIRTIO_GPU_PCI "virtio-gpu-pci" |
| 84 | typedef struct VirtIOGPUPCI VirtIOGPUPCI; |
| 85 | DECLARE_INSTANCE_CHECKER(VirtIOGPUPCI, VIRTIO_GPU_PCI, |
| 86 | TYPE_VIRTIO_GPU_PCI) |
| 87 | |
| 88 | struct VirtIOGPUPCI { |
| 89 | VirtIOGPUPCIBase parent_obj; |
| 90 | VirtIOGPU vdev; |
| 91 | }; |
| 92 | |
| 93 | static void virtio_gpu_initfn(Object *obj) |
| 94 | { |
| 95 | VirtIOGPUPCI *dev = VIRTIO_GPU_PCI(obj); |
| 96 | |
| 97 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), |
| 98 | TYPE_VIRTIO_GPU); |
| 99 | VIRTIO_GPU_PCI_BASE(obj)->vgpu = VIRTIO_GPU_BASE(&dev->vdev); |
| 100 | } |
| 101 | |
| 102 | static const VirtioPCIDeviceTypeInfo virtio_gpu_pci_info = { |
| 103 | .generic_name = TYPE_VIRTIO_GPU_PCI, |
| 104 | .parent = TYPE_VIRTIO_GPU_PCI_BASE, |
| 105 | .instance_size = sizeof(VirtIOGPUPCI), |
| 106 | .instance_init = virtio_gpu_initfn, |
| 107 | }; |
| 108 | module_obj(TYPE_VIRTIO_GPU_PCI); |
| 109 | |
| 110 | static void virtio_gpu_pci_register_types(void) |
| 111 | { |
| 112 | type_register_static(&virtio_gpu_pci_base_info); |
| 113 | virtio_pci_types_register(&virtio_gpu_pci_info); |
| 114 | } |
| 115 | |
| 116 | type_init(virtio_gpu_pci_register_types) |