xref: /openbmc/qemu/hw/virtio/virtio-iommu-pci.c (revision e3a99063)
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 
20 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
21 
22 /*
23  * virtio-iommu-pci: This extends VirtioPCIProxy.
24  *
25  */
26 #define VIRTIO_IOMMU_PCI(obj) \
27         OBJECT_CHECK(VirtIOIOMMUPCI, (obj), TYPE_VIRTIO_IOMMU_PCI)
28 
29 struct VirtIOIOMMUPCI {
30     VirtIOPCIProxy parent_obj;
31     VirtIOIOMMU vdev;
32 };
33 
34 static Property virtio_iommu_pci_properties[] = {
35     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
36     DEFINE_PROP_END_OF_LIST(),
37 };
38 
39 static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
40 {
41     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
42     DeviceState *vdev = DEVICE(&dev->vdev);
43 
44     if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
45         MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
46 
47         error_setg(errp,
48                    "%s machine fails to create iommu-map device tree bindings",
49                    mc->name);
50         error_append_hint(errp,
51                           "Check you machine implements a hotplug handler "
52                           "for the virtio-iommu-pci device\n");
53         error_append_hint(errp, "Check the guest is booted without FW or with "
54                           "-no-acpi\n");
55         return;
56     }
57     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
58     object_property_set_link(OBJECT(dev),
59                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
60                              "primary-bus", errp);
61     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
62 }
63 
64 static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data)
65 {
66     DeviceClass *dc = DEVICE_CLASS(klass);
67     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
68     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
69     k->realize = virtio_iommu_pci_realize;
70     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
71     device_class_set_props(dc, virtio_iommu_pci_properties);
72     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
73     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_IOMMU;
74     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
75     pcidev_k->class_id = PCI_CLASS_OTHERS;
76     dc->hotpluggable = false;
77 }
78 
79 static void virtio_iommu_pci_instance_init(Object *obj)
80 {
81     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
82 
83     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
84                                 TYPE_VIRTIO_IOMMU);
85 }
86 
87 static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
88     .base_name             = TYPE_VIRTIO_IOMMU_PCI,
89     .generic_name          = "virtio-iommu-pci",
90     .transitional_name     = "virtio-iommu-pci-transitional",
91     .non_transitional_name = "virtio-iommu-pci-non-transitional",
92     .instance_size = sizeof(VirtIOIOMMUPCI),
93     .instance_init = virtio_iommu_pci_instance_init,
94     .class_init    = virtio_iommu_pci_class_init,
95 };
96 
97 static void virtio_iommu_pci_register(void)
98 {
99     virtio_pci_types_register(&virtio_iommu_pci_info);
100 }
101 
102 type_init(virtio_iommu_pci_register)
103 
104 
105