virtio-iommu-pci.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "qemu/osdep.h"
  12. #include "virtio-pci.h"
  13. #include "hw/virtio/virtio-iommu.h"
  14. #include "hw/qdev-properties.h"
  15. #include "qapi/error.h"
  16. #include "hw/boards.h"
  17. typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
  18. /*
  19. * virtio-iommu-pci: This extends VirtioPCIProxy.
  20. *
  21. */
  22. #define VIRTIO_IOMMU_PCI(obj) \
  23. OBJECT_CHECK(VirtIOIOMMUPCI, (obj), TYPE_VIRTIO_IOMMU_PCI)
  24. struct VirtIOIOMMUPCI {
  25. VirtIOPCIProxy parent_obj;
  26. VirtIOIOMMU vdev;
  27. };
  28. static Property virtio_iommu_pci_properties[] = {
  29. DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
  30. DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI,
  31. vdev.nb_reserved_regions, vdev.reserved_regions,
  32. qdev_prop_reserved_region, ReservedRegion),
  33. DEFINE_PROP_END_OF_LIST(),
  34. };
  35. static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
  36. {
  37. VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
  38. DeviceState *vdev = DEVICE(&dev->vdev);
  39. VirtIOIOMMU *s = VIRTIO_IOMMU(vdev);
  40. if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
  41. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  42. error_setg(errp,
  43. "%s machine fails to create iommu-map device tree bindings",
  44. mc->name);
  45. error_append_hint(errp,
  46. "Check your machine implements a hotplug handler "
  47. "for the virtio-iommu-pci device\n");
  48. error_append_hint(errp, "Check the guest is booted without FW or with "
  49. "-no-acpi\n");
  50. return;
  51. }
  52. for (int i = 0; i < s->nb_reserved_regions; i++) {
  53. if (s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_RESERVED &&
  54. s->reserved_regions[i].type != VIRTIO_IOMMU_RESV_MEM_T_MSI) {
  55. error_setg(errp, "reserved region %d has an invalid type", i);
  56. error_append_hint(errp, "Valid values are 0 and 1\n");
  57. }
  58. }
  59. object_property_set_link(OBJECT(dev), "primary-bus",
  60. OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
  61. &error_abort);
  62. qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
  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. static void virtio_iommu_pci_instance_init(Object *obj)
  79. {
  80. VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(obj);
  81. virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
  82. TYPE_VIRTIO_IOMMU);
  83. }
  84. static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = {
  85. .base_name = TYPE_VIRTIO_IOMMU_PCI,
  86. .generic_name = "virtio-iommu-pci",
  87. .transitional_name = "virtio-iommu-pci-transitional",
  88. .non_transitional_name = "virtio-iommu-pci-non-transitional",
  89. .instance_size = sizeof(VirtIOIOMMUPCI),
  90. .instance_init = virtio_iommu_pci_instance_init,
  91. .class_init = virtio_iommu_pci_class_init,
  92. };
  93. static void virtio_iommu_pci_register(void)
  94. {
  95. virtio_pci_types_register(&virtio_iommu_pci_info);
  96. }
  97. type_init(virtio_iommu_pci_register)