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
14e1b1f534SAlex 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"
20e72cfabfSEric Auger #include "hw/pci/pci_bus.h"
21db1015e9SEduardo Habkost #include "qom/object.h"
228b4eb09eSEric Auger
238b4eb09eSEric Auger typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
248b4eb09eSEric Auger
258b4eb09eSEric Auger /*
268b4eb09eSEric Auger * virtio-iommu-pci: This extends VirtioPCIProxy.
278b4eb09eSEric Auger *
288b4eb09eSEric Auger */
298110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(VirtIOIOMMUPCI, VIRTIO_IOMMU_PCI,
308110fa1dSEduardo Habkost TYPE_VIRTIO_IOMMU_PCI)
318b4eb09eSEric Auger
328b4eb09eSEric Auger struct VirtIOIOMMUPCI {
338b4eb09eSEric Auger VirtIOPCIProxy parent_obj;
348b4eb09eSEric Auger VirtIOIOMMU vdev;
358b4eb09eSEric Auger };
368b4eb09eSEric Auger
371577a918SRichard Henderson static const Property virtio_iommu_pci_properties[] = {
388b4eb09eSEric Auger DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
398077b8e5SEric Auger DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI,
4041cc70cdSEric Auger vdev.nr_prop_resv_regions, vdev.prop_resv_regions,
418077b8e5SEric Auger qdev_prop_reserved_region, ReservedRegion),
428b4eb09eSEric Auger };
438b4eb09eSEric Auger
virtio_iommu_pci_realize(VirtIOPCIProxy * vpci_dev,Error ** errp)448b4eb09eSEric Auger static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
458b4eb09eSEric Auger {
468b4eb09eSEric Auger VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
47e72cfabfSEric Auger PCIBus *pbus = pci_get_bus(&vpci_dev->pci_dev);
488b4eb09eSEric Auger DeviceState *vdev = DEVICE(&dev->vdev);
498077b8e5SEric Auger VirtIOIOMMU *s = VIRTIO_IOMMU(vdev);
508b4eb09eSEric Auger
518b4eb09eSEric Auger if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
52092cba03SJean-Philippe Brucker error_setg(errp, "Check your machine implements a hotplug handler "
53092cba03SJean-Philippe Brucker "for the virtio-iommu-pci device");
548b4eb09eSEric Auger return;
558b4eb09eSEric Auger }
5641cc70cdSEric Auger for (int i = 0; i < s->nr_prop_resv_regions; i++) {
5741cc70cdSEric Auger if (s->prop_resv_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED &&
5841cc70cdSEric Auger s->prop_resv_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) {
598077b8e5SEric Auger error_setg(errp, "reserved region %d has an invalid type", i);
608077b8e5SEric Auger error_append_hint(errp, "Valid values are 0 and 1\n");
61e72cfabfSEric Auger return;
628077b8e5SEric Auger }
638077b8e5SEric Auger }
64e72cfabfSEric Auger if (!pci_bus_is_root(pbus)) {
65e72cfabfSEric Auger error_setg(errp, "virtio-iommu-pci must be plugged on the root bus");
66e72cfabfSEric Auger return;
67e72cfabfSEric Auger }
68e72cfabfSEric Auger
695325cc34SMarkus Armbruster object_property_set_link(OBJECT(dev), "primary-bus",
70e72cfabfSEric Auger OBJECT(pbus), &error_abort);
71e72cfabfSEric Auger
728f39562aSEric Auger virtio_pci_force_virtio_1(vpci_dev);
7399ba777eSMarkus Armbruster qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
748b4eb09eSEric Auger }
758b4eb09eSEric Auger
virtio_iommu_pci_class_init(ObjectClass * klass,const void * data)76*12d1a768SPhilippe Mathieu-Daudé static void virtio_iommu_pci_class_init(ObjectClass *klass, const void *data)
778b4eb09eSEric Auger {
788b4eb09eSEric Auger DeviceClass *dc = DEVICE_CLASS(klass);
798b4eb09eSEric Auger VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
808b4eb09eSEric Auger PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
818b4eb09eSEric Auger k->realize = virtio_iommu_pci_realize;
828b4eb09eSEric Auger set_bit(DEVICE_CATEGORY_MISC, dc->categories);
838b4eb09eSEric Auger device_class_set_props(dc, virtio_iommu_pci_properties);
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
virtio_iommu_pci_instance_init(Object * obj)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
virtio_iommu_pci_instance_finalize(Object * obj)97c9a4aa06SPhilippe Mathieu-Daudé static void virtio_iommu_pci_instance_finalize(Object *obj)
98c9a4aa06SPhilippe Mathieu-Daudé {
99c9a4aa06SPhilippe Mathieu-Daudé VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
100c9a4aa06SPhilippe Mathieu-Daudé
101c9a4aa06SPhilippe Mathieu-Daudé g_free(dev->vdev.prop_resv_regions);
102c9a4aa06SPhilippe Mathieu-Daudé }
103c9a4aa06SPhilippe Mathieu-Daudé
1048b4eb09eSEric Auger static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
10519d20e91SEric Auger .generic_name = TYPE_VIRTIO_IOMMU_PCI,
1068b4eb09eSEric Auger .instance_size = sizeof(VirtIOIOMMUPCI),
1078b4eb09eSEric Auger .instance_init = virtio_iommu_pci_instance_init,
108c9a4aa06SPhilippe Mathieu-Daudé .instance_finalize = virtio_iommu_pci_instance_finalize,
1098b4eb09eSEric Auger .class_init = virtio_iommu_pci_class_init,
1108b4eb09eSEric Auger };
1118b4eb09eSEric Auger
virtio_iommu_pci_register(void)1128b4eb09eSEric Auger static void virtio_iommu_pci_register(void)
1138b4eb09eSEric Auger {
1148b4eb09eSEric Auger virtio_pci_types_register(&virtio_iommu_pci_info);
1158b4eb09eSEric Auger }
1168b4eb09eSEric Auger
1178b4eb09eSEric Auger type_init(virtio_iommu_pci_register)
1188b4eb09eSEric Auger
1198b4eb09eSEric Auger
120