xref: /qemu/hw/virtio/virtio-iommu-pci.c (revision 5325cc34a2ca985283134c7e264be7851b112d4e)
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 
148b4eb09eSEric Auger #include "virtio-pci.h"
158b4eb09eSEric Auger #include "hw/virtio/virtio-iommu.h"
168b4eb09eSEric Auger #include "hw/qdev-properties.h"
178b4eb09eSEric Auger #include "qapi/error.h"
188b4eb09eSEric Auger #include "hw/boards.h"
198b4eb09eSEric Auger 
208b4eb09eSEric Auger typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
218b4eb09eSEric Auger 
228b4eb09eSEric Auger /*
238b4eb09eSEric Auger  * virtio-iommu-pci: This extends VirtioPCIProxy.
248b4eb09eSEric Auger  *
258b4eb09eSEric Auger  */
268b4eb09eSEric Auger #define VIRTIO_IOMMU_PCI(obj) \
278b4eb09eSEric Auger         OBJECT_CHECK(VirtIOIOMMUPCI, (obj), TYPE_VIRTIO_IOMMU_PCI)
288b4eb09eSEric Auger 
298b4eb09eSEric Auger struct VirtIOIOMMUPCI {
308b4eb09eSEric Auger     VirtIOPCIProxy parent_obj;
318b4eb09eSEric Auger     VirtIOIOMMU vdev;
328b4eb09eSEric Auger };
338b4eb09eSEric Auger 
348b4eb09eSEric Auger static Property virtio_iommu_pci_properties[] = {
358b4eb09eSEric Auger     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
368077b8e5SEric Auger     DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI,
378077b8e5SEric Auger                       vdev.nb_reserved_regions, vdev.reserved_regions,
388077b8e5SEric Auger                       qdev_prop_reserved_region, ReservedRegion),
398b4eb09eSEric Auger     DEFINE_PROP_END_OF_LIST(),
408b4eb09eSEric Auger };
418b4eb09eSEric Auger 
428b4eb09eSEric Auger static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
438b4eb09eSEric Auger {
448b4eb09eSEric Auger     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
458b4eb09eSEric Auger     DeviceState *vdev = DEVICE(&dev->vdev);
468077b8e5SEric Auger     VirtIOIOMMU *s = VIRTIO_IOMMU(vdev);
478b4eb09eSEric Auger 
488b4eb09eSEric Auger     if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
498b4eb09eSEric Auger         MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
508b4eb09eSEric Auger 
518b4eb09eSEric Auger         error_setg(errp,
528b4eb09eSEric Auger                    "%s machine fails to create iommu-map device tree bindings",
538b4eb09eSEric Auger                    mc->name);
548b4eb09eSEric Auger         error_append_hint(errp,
55590090b4SPeter Maydell                           "Check your machine implements a hotplug handler "
568b4eb09eSEric Auger                           "for the virtio-iommu-pci device\n");
578b4eb09eSEric Auger         error_append_hint(errp, "Check the guest is booted without FW or with "
588b4eb09eSEric Auger                           "-no-acpi\n");
598b4eb09eSEric Auger         return;
608b4eb09eSEric Auger     }
618077b8e5SEric Auger     for (int i = 0; i < s->nb_reserved_regions; i++) {
628077b8e5SEric Auger         if (s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED &&
638077b8e5SEric Auger             s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) {
648077b8e5SEric Auger             error_setg(errp, "reserved region %d has an invalid type", i);
658077b8e5SEric Auger             error_append_hint(errp, "Valid values are 0 and 1\n");
668077b8e5SEric Auger         }
678077b8e5SEric Auger     }
68*5325cc34SMarkus Armbruster     object_property_set_link(OBJECT(dev), "primary-bus",
698b4eb09eSEric Auger                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
70*5325cc34SMarkus Armbruster                              &error_abort);
7199ba777eSMarkus Armbruster     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
728b4eb09eSEric Auger }
738b4eb09eSEric Auger 
748b4eb09eSEric Auger static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data)
758b4eb09eSEric Auger {
768b4eb09eSEric Auger     DeviceClass *dc = DEVICE_CLASS(klass);
778b4eb09eSEric Auger     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
788b4eb09eSEric Auger     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
798b4eb09eSEric Auger     k->realize = virtio_iommu_pci_realize;
808b4eb09eSEric Auger     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
818b4eb09eSEric Auger     device_class_set_props(dc, virtio_iommu_pci_properties);
828b4eb09eSEric Auger     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
838b4eb09eSEric Auger     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_IOMMU;
848b4eb09eSEric Auger     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
858b4eb09eSEric Auger     pcidev_k->class_id = PCI_CLASS_OTHERS;
868b4eb09eSEric Auger     dc->hotpluggable = false;
878b4eb09eSEric Auger }
888b4eb09eSEric Auger 
898b4eb09eSEric Auger static void virtio_iommu_pci_instance_init(Object *obj)
908b4eb09eSEric Auger {
918b4eb09eSEric Auger     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
928b4eb09eSEric Auger 
938b4eb09eSEric Auger     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
948b4eb09eSEric Auger                                 TYPE_VIRTIO_IOMMU);
958b4eb09eSEric Auger }
968b4eb09eSEric Auger 
978b4eb09eSEric Auger static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
988b4eb09eSEric Auger     .base_name             = TYPE_VIRTIO_IOMMU_PCI,
998b4eb09eSEric Auger     .generic_name          = "virtio-iommu-pci",
1008b4eb09eSEric Auger     .transitional_name     = "virtio-iommu-pci-transitional",
1018b4eb09eSEric Auger     .non_transitional_name = "virtio-iommu-pci-non-transitional",
1028b4eb09eSEric Auger     .instance_size = sizeof(VirtIOIOMMUPCI),
1038b4eb09eSEric Auger     .instance_init = virtio_iommu_pci_instance_init,
1048b4eb09eSEric Auger     .class_init    = virtio_iommu_pci_class_init,
1058b4eb09eSEric Auger };
1068b4eb09eSEric Auger 
1078b4eb09eSEric Auger static void virtio_iommu_pci_register(void)
1088b4eb09eSEric Auger {
1098b4eb09eSEric Auger     virtio_pci_types_register(&virtio_iommu_pci_info);
1108b4eb09eSEric Auger }
1118b4eb09eSEric Auger 
1128b4eb09eSEric Auger type_init(virtio_iommu_pci_register)
1138b4eb09eSEric Auger 
1148b4eb09eSEric Auger 
115