1 /* 2 * Virtio IOMMU PCI Bindings 3 * 4 * Copyright (c) 2019 Red Hat, Inc. 5 * Written by Eric Auger 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 or 9 * (at your option) any later version. 10 */ 11 12 #include "qemu/osdep.h" 13 14 #include "hw/virtio/virtio-pci.h" 15 #include "hw/virtio/virtio-iommu.h" 16 #include "hw/qdev-properties.h" 17 #include "hw/qdev-properties-system.h" 18 #include "qapi/error.h" 19 #include "hw/boards.h" 20 #include "qom/object.h" 21 22 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI; 23 24 /* 25 * virtio-iommu-pci: This extends VirtioPCIProxy. 26 * 27 */ 28 DECLARE_INSTANCE_CHECKER(VirtIOIOMMUPCI, VIRTIO_IOMMU_PCI, 29 TYPE_VIRTIO_IOMMU_PCI) 30 31 struct VirtIOIOMMUPCI { 32 VirtIOPCIProxy parent_obj; 33 VirtIOIOMMU vdev; 34 }; 35 36 static Property virtio_iommu_pci_properties[] = { 37 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 38 DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI, 39 vdev.nb_reserved_regions, vdev.reserved_regions, 40 qdev_prop_reserved_region, ReservedRegion), 41 DEFINE_PROP_END_OF_LIST(), 42 }; 43 44 static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) 45 { 46 VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev); 47 DeviceState *vdev = DEVICE(&dev->vdev); 48 VirtIOIOMMU *s = VIRTIO_IOMMU(vdev); 49 50 if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) { 51 error_setg(errp, "Check your machine implements a hotplug handler " 52 "for the virtio-iommu-pci device"); 53 return; 54 } 55 for (int i = 0; i < s->nb_reserved_regions; i++) { 56 if (s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED && 57 s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) { 58 error_setg(errp, "reserved region %d has an invalid type", i); 59 error_append_hint(errp, "Valid values are 0 and 1\n"); 60 } 61 } 62 object_property_set_link(OBJECT(dev), "primary-bus", 63 OBJECT(pci_get_bus(&vpci_dev->pci_dev)), 64 &error_abort); 65 virtio_pci_force_virtio_1(vpci_dev); 66 qdev_realize(vdev, BUS(&vpci_dev->bus), errp); 67 } 68 69 static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data) 70 { 71 DeviceClass *dc = DEVICE_CLASS(klass); 72 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 73 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 74 k->realize = virtio_iommu_pci_realize; 75 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 76 device_class_set_props(dc, virtio_iommu_pci_properties); 77 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 78 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_IOMMU; 79 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 80 pcidev_k->class_id = PCI_CLASS_OTHERS; 81 dc->hotpluggable = false; 82 } 83 84 static void virtio_iommu_pci_instance_init(Object *obj) 85 { 86 VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj); 87 88 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 89 TYPE_VIRTIO_IOMMU); 90 } 91 92 static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = { 93 .generic_name = TYPE_VIRTIO_IOMMU_PCI, 94 .instance_size = sizeof(VirtIOIOMMUPCI), 95 .instance_init = virtio_iommu_pci_instance_init, 96 .class_init = virtio_iommu_pci_class_init, 97 }; 98 99 static void virtio_iommu_pci_register(void) 100 { 101 virtio_pci_types_register(&virtio_iommu_pci_info); 102 } 103 104 type_init(virtio_iommu_pci_register) 105 106 107