xref: /qemu/hw/virtio/virtio-iommu-pci.c (revision c1dc0a1d)
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 "hw/pci/pci_bus.h"
21 #include "qom/object.h"
22 
23 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
24 
25 /*
26  * virtio-iommu-pci: This extends VirtioPCIProxy.
27  *
28  */
29 DECLARE_INSTANCE_CHECKER(VirtIOIOMMUPCI, VIRTIO_IOMMU_PCI,
30                          TYPE_VIRTIO_IOMMU_PCI)
31 
32 struct VirtIOIOMMUPCI {
33     VirtIOPCIProxy parent_obj;
34     VirtIOIOMMU vdev;
35 };
36 
37 static Property virtio_iommu_pci_properties[] = {
38     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
39     DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI,
40                       vdev.nr_prop_resv_regions, vdev.prop_resv_regions,
41                       qdev_prop_reserved_region, ReservedRegion),
42     DEFINE_PROP_END_OF_LIST(),
43 };
44 
45 static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
46 {
47     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
48     PCIBus *pbus = pci_get_bus(&vpci_dev->pci_dev);
49     DeviceState *vdev = DEVICE(&dev->vdev);
50     VirtIOIOMMU *s = VIRTIO_IOMMU(vdev);
51 
52     if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
53         error_setg(errp, "Check your machine implements a hotplug handler "
54                          "for the virtio-iommu-pci device");
55         return;
56     }
57     for (int i = 0; i < s->nr_prop_resv_regions; i++) {
58         if (s->prop_resv_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED &&
59             s->prop_resv_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) {
60             error_setg(errp, "reserved region %d has an invalid type", i);
61             error_append_hint(errp, "Valid values are 0 and 1\n");
62             return;
63         }
64     }
65     if (!pci_bus_is_root(pbus)) {
66         error_setg(errp, "virtio-iommu-pci must be plugged on the root bus");
67         return;
68     }
69 
70     object_property_set_link(OBJECT(dev), "primary-bus",
71                              OBJECT(pbus), &error_abort);
72 
73     virtio_pci_force_virtio_1(vpci_dev);
74     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
75 }
76 
77 static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data)
78 {
79     DeviceClass *dc = DEVICE_CLASS(klass);
80     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
81     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
82     k->realize = virtio_iommu_pci_realize;
83     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
84     device_class_set_props(dc, virtio_iommu_pci_properties);
85     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
86     pcidev_k->class_id = PCI_CLASS_OTHERS;
87     dc->hotpluggable = false;
88 }
89 
90 static void virtio_iommu_pci_instance_init(Object *obj)
91 {
92     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
93 
94     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
95                                 TYPE_VIRTIO_IOMMU);
96 }
97 
98 static void virtio_iommu_pci_instance_finalize(Object *obj)
99 {
100     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
101 
102     g_free(dev->vdev.prop_resv_regions);
103 }
104 
105 static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
106     .generic_name  = TYPE_VIRTIO_IOMMU_PCI,
107     .instance_size = sizeof(VirtIOIOMMUPCI),
108     .instance_init = virtio_iommu_pci_instance_init,
109     .instance_finalize = virtio_iommu_pci_instance_finalize,
110     .class_init    = virtio_iommu_pci_class_init,
111 };
112 
113 static void virtio_iommu_pci_register(void)
114 {
115     virtio_pci_types_register(&virtio_iommu_pci_info);
116 }
117 
118 type_init(virtio_iommu_pci_register)
119 
120 
121