xref: /qemu/hw/virtio/virtio-iommu-pci.c (revision e1b1f5341a860e87c6f208e2eea7a3b2bab454dd)
18b4eb09eSEric Auger /*
28b4eb09eSEric Auger  * Virtio IOMMU PCI Bindings
38b4eb09eSEric Auger  *
48b4eb09eSEric Auger  * Copyright (c) 2019 Red Hat, Inc.
58b4eb09eSEric Auger  * Written by Eric Auger
68b4eb09eSEric Auger  *
78b4eb09eSEric Auger  *  This program is free software; you can redistribute it and/or modify
88b4eb09eSEric Auger  *  it under the terms of the GNU General Public License version 2 or
98b4eb09eSEric Auger  *  (at your option) any later version.
108b4eb09eSEric Auger  */
118b4eb09eSEric Auger 
128b4eb09eSEric Auger #include "qemu/osdep.h"
138b4eb09eSEric Auger 
14*e1b1f534SAlex Bennée #include "hw/virtio/virtio-pci.h"
158b4eb09eSEric Auger #include "hw/virtio/virtio-iommu.h"
168b4eb09eSEric Auger #include "hw/qdev-properties.h"
17ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
188b4eb09eSEric Auger #include "qapi/error.h"
198b4eb09eSEric Auger #include "hw/boards.h"
20db1015e9SEduardo Habkost #include "qom/object.h"
218b4eb09eSEric Auger 
228b4eb09eSEric Auger typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
238b4eb09eSEric Auger 
248b4eb09eSEric Auger /*
258b4eb09eSEric Auger  * virtio-iommu-pci: This extends VirtioPCIProxy.
268b4eb09eSEric Auger  *
278b4eb09eSEric Auger  */
288110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(VirtIOIOMMUPCI, VIRTIO_IOMMU_PCI,
298110fa1dSEduardo Habkost                          TYPE_VIRTIO_IOMMU_PCI)
308b4eb09eSEric Auger 
318b4eb09eSEric Auger struct VirtIOIOMMUPCI {
328b4eb09eSEric Auger     VirtIOPCIProxy parent_obj;
338b4eb09eSEric Auger     VirtIOIOMMU vdev;
348b4eb09eSEric Auger };
358b4eb09eSEric Auger 
368b4eb09eSEric Auger static Property virtio_iommu_pci_properties[] = {
378b4eb09eSEric Auger     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
388077b8e5SEric Auger     DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI,
398077b8e5SEric Auger                       vdev.nb_reserved_regions, vdev.reserved_regions,
408077b8e5SEric Auger                       qdev_prop_reserved_region, ReservedRegion),
418b4eb09eSEric Auger     DEFINE_PROP_END_OF_LIST(),
428b4eb09eSEric Auger };
438b4eb09eSEric Auger 
448b4eb09eSEric Auger static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
458b4eb09eSEric Auger {
468b4eb09eSEric Auger     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
478b4eb09eSEric Auger     DeviceState *vdev = DEVICE(&dev->vdev);
488077b8e5SEric Auger     VirtIOIOMMU *s = VIRTIO_IOMMU(vdev);
498b4eb09eSEric Auger 
508b4eb09eSEric Auger     if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
51092cba03SJean-Philippe Brucker         error_setg(errp, "Check your machine implements a hotplug handler "
52092cba03SJean-Philippe Brucker                          "for the virtio-iommu-pci device");
538b4eb09eSEric Auger         return;
548b4eb09eSEric Auger     }
558077b8e5SEric Auger     for (int i = 0; i < s->nb_reserved_regions; i++) {
568077b8e5SEric Auger         if (s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED &&
578077b8e5SEric Auger             s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) {
588077b8e5SEric Auger             error_setg(errp, "reserved region %d has an invalid type", i);
598077b8e5SEric Auger             error_append_hint(errp, "Valid values are 0 and 1\n");
608077b8e5SEric Auger         }
618077b8e5SEric Auger     }
625325cc34SMarkus Armbruster     object_property_set_link(OBJECT(dev), "primary-bus",
638b4eb09eSEric Auger                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
645325cc34SMarkus Armbruster                              &error_abort);
658f39562aSEric Auger     virtio_pci_force_virtio_1(vpci_dev);
6699ba777eSMarkus Armbruster     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
678b4eb09eSEric Auger }
688b4eb09eSEric Auger 
698b4eb09eSEric Auger static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data)
708b4eb09eSEric Auger {
718b4eb09eSEric Auger     DeviceClass *dc = DEVICE_CLASS(klass);
728b4eb09eSEric Auger     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
738b4eb09eSEric Auger     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
748b4eb09eSEric Auger     k->realize = virtio_iommu_pci_realize;
758b4eb09eSEric Auger     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
768b4eb09eSEric Auger     device_class_set_props(dc, virtio_iommu_pci_properties);
778b4eb09eSEric Auger     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
788b4eb09eSEric Auger     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_IOMMU;
798b4eb09eSEric Auger     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
808b4eb09eSEric Auger     pcidev_k->class_id = PCI_CLASS_OTHERS;
818b4eb09eSEric Auger     dc->hotpluggable = false;
828b4eb09eSEric Auger }
838b4eb09eSEric Auger 
848b4eb09eSEric Auger static void virtio_iommu_pci_instance_init(Object *obj)
858b4eb09eSEric Auger {
868b4eb09eSEric Auger     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
878b4eb09eSEric Auger 
888b4eb09eSEric Auger     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
898b4eb09eSEric Auger                                 TYPE_VIRTIO_IOMMU);
908b4eb09eSEric Auger }
918b4eb09eSEric Auger 
928b4eb09eSEric Auger static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
9319d20e91SEric Auger     .generic_name          = TYPE_VIRTIO_IOMMU_PCI,
948b4eb09eSEric Auger     .instance_size = sizeof(VirtIOIOMMUPCI),
958b4eb09eSEric Auger     .instance_init = virtio_iommu_pci_instance_init,
968b4eb09eSEric Auger     .class_init    = virtio_iommu_pci_class_init,
978b4eb09eSEric Auger };
988b4eb09eSEric Auger 
998b4eb09eSEric Auger static void virtio_iommu_pci_register(void)
1008b4eb09eSEric Auger {
1018b4eb09eSEric Auger     virtio_pci_types_register(&virtio_iommu_pci_info);
1028b4eb09eSEric Auger }
1038b4eb09eSEric Auger 
1048b4eb09eSEric Auger type_init(virtio_iommu_pci_register)
1058b4eb09eSEric Auger 
1068b4eb09eSEric Auger 
107