xref: /openbmc/qemu/hw/virtio/virtio-iommu-pci.c (revision e6546342)
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 "virtio-pci.h"
15 #include "hw/virtio/virtio-iommu.h"
16 #include "hw/qdev-properties.h"
17 #include "qapi/error.h"
18 #include "hw/boards.h"
19 #include "qom/object.h"
20 
21 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
22 
23 /*
24  * virtio-iommu-pci: This extends VirtioPCIProxy.
25  *
26  */
27 DECLARE_INSTANCE_CHECKER(VirtIOIOMMUPCI, VIRTIO_IOMMU_PCI,
28                          TYPE_VIRTIO_IOMMU_PCI)
29 
30 struct VirtIOIOMMUPCI {
31     VirtIOPCIProxy parent_obj;
32     VirtIOIOMMU vdev;
33 };
34 
35 static Property virtio_iommu_pci_properties[] = {
36     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
37     DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI,
38                       vdev.nb_reserved_regions, vdev.reserved_regions,
39                       qdev_prop_reserved_region, ReservedRegion),
40     DEFINE_PROP_END_OF_LIST(),
41 };
42 
43 static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
44 {
45     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
46     DeviceState *vdev = DEVICE(&dev->vdev);
47     VirtIOIOMMU *s = VIRTIO_IOMMU(vdev);
48 
49     if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
50         MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
51 
52         error_setg(errp,
53                    "%s machine fails to create iommu-map device tree bindings",
54                    mc->name);
55         error_append_hint(errp,
56                           "Check your machine implements a hotplug handler "
57                           "for the virtio-iommu-pci device\n");
58         error_append_hint(errp, "Check the guest is booted without FW or with "
59                           "-no-acpi\n");
60         return;
61     }
62     for (int i = 0; i < s->nb_reserved_regions; i++) {
63         if (s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED &&
64             s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) {
65             error_setg(errp, "reserved region %d has an invalid type", i);
66             error_append_hint(errp, "Valid values are 0 and 1\n");
67         }
68     }
69     object_property_set_link(OBJECT(dev), "primary-bus",
70                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
71                              &error_abort);
72     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
73 }
74 
75 static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data)
76 {
77     DeviceClass *dc = DEVICE_CLASS(klass);
78     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
79     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
80     k->realize = virtio_iommu_pci_realize;
81     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
82     device_class_set_props(dc, virtio_iommu_pci_properties);
83     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
84     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_IOMMU;
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 const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
99     .base_name             = TYPE_VIRTIO_IOMMU_PCI,
100     .generic_name          = "virtio-iommu-pci",
101     .transitional_name     = "virtio-iommu-pci-transitional",
102     .non_transitional_name = "virtio-iommu-pci-non-transitional",
103     .instance_size = sizeof(VirtIOIOMMUPCI),
104     .instance_init = virtio_iommu_pci_instance_init,
105     .class_init    = virtio_iommu_pci_class_init,
106 };
107 
108 static void virtio_iommu_pci_register(void)
109 {
110     virtio_pci_types_register(&virtio_iommu_pci_info);
111 }
112 
113 type_init(virtio_iommu_pci_register)
114 
115 
116