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