[RFC PATCH 04/10] vfio: Prepare existing code for Apple VFIO backend

Scott J. Goldman posted 10 patches 6 days, 11 hours ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, Roman Bolshakov <rbolshakov@ddn.com>, Phil Dennis-Jordan <phil@philjordan.eu>, "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Pierrick Bouvier <pierrick.bouvier@linaro.org>, John Levon <john.levon@nutanix.com>, Thanos Makatos <thanos.makatos@nutanix.com>, "Cédric Le Goater" <clg@redhat.com>, Alex Williamson <alex@shazbot.org>, Tony Krowiak <akrowiak@linux.ibm.com>, Halil Pasic <pasic@linux.ibm.com>, Jason Herne <jjherne@linux.ibm.com>, Cornelia Huck <cohuck@redhat.com>, Eric Farman <farman@linux.ibm.com>, Matthew Rosato <mjrosato@linux.ibm.com>, "Scott J. Goldman" <scottjgo@gmail.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
[RFC PATCH 04/10] vfio: Prepare existing code for Apple VFIO backend
Posted by Scott J. Goldman 6 days, 11 hours ago
From: "Scott J. Goldman" <scottjg@umich.edu>

Adjust the shared VFIO code so a non-Linux backend can plug in:

- vfio_device_get_name(): accept a device that already has a name set
  without requiring sysfsdev or an fd.
- vfio_device_is_mdev(): return true on Darwin — the dext mediates all
  device access and manages DMA mappings explicitly, so the mdev
  assumptions (software-managed DMA, balloon-safe) hold.
- vfio_device_attach(): select the Apple IOMMU container type on Darwin.
- vfio_pci_realize(): allow realize when name is pre-set (no sysfsdev),
  and add a no_bar_quirks flag so subclasses can skip BAR quirk setup.
- Add TYPE_VFIO_IOMMU_APPLE and TYPE_VFIO_APPLE_PCI type strings.

Signed-off-by: Scott J. Goldman <scottjgo@gmail.com>
---
 hw/vfio/device.c                 | 20 ++++++++++++++++++++
 hw/vfio/pci.c                    |  8 +++++---
 hw/vfio/pci.h                    |  1 +
 hw/vfio/types.h                  |  2 ++
 include/hw/vfio/vfio-container.h |  1 +
 5 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 973fc35b59..338becffa7 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -316,6 +316,13 @@ bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp)
     struct stat st;
 
     if (vbasedev->fd < 0) {
+        if (!vbasedev->sysfsdev) {
+            if (vbasedev->name) {
+                return true;
+            }
+            error_setg(errp, "No provided host device");
+            return false;
+        }
         if (stat(vbasedev->sysfsdev, &st) < 0) {
             error_setg_errno(errp, errno, "no such host device");
             error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->sysfsdev);
@@ -404,7 +411,16 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev)
     g_autofree char *tmp = NULL;
 
     if (!vbasedev->sysfsdev) {
+#ifdef CONFIG_DARWIN
+        /*
+         * On Darwin the dext mediates all device access and manages DMA
+         * mappings explicitly, so the mdev assumptions (software-managed
+         * DMA, balloon-safe) hold.
+         */
+        return true;
+#else
         return false;
+#endif
     }
 
     tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev);
@@ -462,9 +478,13 @@ bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
 bool vfio_device_attach(char *name, VFIODevice *vbasedev,
                         AddressSpace *as, Error **errp)
 {
+#ifdef CONFIG_DARWIN
+    const char *iommu_type = TYPE_VFIO_IOMMU_APPLE;
+#else
     const char *iommu_type = vbasedev->iommufd ?
                              TYPE_VFIO_IOMMU_IOMMUFD :
                              TYPE_VFIO_IOMMU_LEGACY;
+#endif
 
     return vfio_device_attach_by_iommu_type(iommu_type, name, vbasedev,
                                             as, errp);
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 5a1c2d8c2e..cf817d9ae7 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -3483,7 +3483,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
     char uuid[UUID_STR_LEN];
     g_autofree char *name = NULL;
 
-    if (vbasedev->fd < 0 && !vbasedev->sysfsdev) {
+    if (vbasedev->fd < 0 && !vbasedev->sysfsdev && !vbasedev->name) {
         if (!(~vdev->host.domain || ~vdev->host.bus ||
               ~vdev->host.slot || ~vdev->host.function)) {
             error_setg(errp, "No provided host device");
@@ -3558,8 +3558,10 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
         vfio_vga_quirk_setup(vdev);
     }
 
-    for (i = 0; i < PCI_ROM_SLOT; i++) {
-        vfio_bar_quirk_setup(vdev, i);
+    if (!vdev->no_bar_quirks) {
+        for (i = 0; i < PCI_ROM_SLOT; i++) {
+            vfio_bar_quirk_setup(vdev, i);
+        }
     }
 
     if (!vfio_pci_interrupt_setup(vdev, errp)) {
diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
index d6495d7f29..424acd71b6 100644
--- a/hw/vfio/pci.h
+++ b/hw/vfio/pci.h
@@ -187,6 +187,7 @@ struct VFIOPCIDevice {
     bool defer_kvm_irq_routing;
     bool clear_parent_atomics_on_exit;
     bool skip_vsc_check;
+    bool no_bar_quirks;
     uint16_t vpasid_cap_offset;
     VFIODisplay *dpy;
     Notifier irqchip_change_notifier;
diff --git a/hw/vfio/types.h b/hw/vfio/types.h
index 5482d90808..b44c234ac4 100644
--- a/hw/vfio/types.h
+++ b/hw/vfio/types.h
@@ -20,4 +20,6 @@
 
 #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
 
+#define TYPE_VFIO_APPLE_PCI "vfio-apple-pci"
+
 #endif /* HW_VFIO_VFIO_TYPES_H */
diff --git a/include/hw/vfio/vfio-container.h b/include/hw/vfio/vfio-container.h
index a7d5c5ed67..5ccbabccb4 100644
--- a/include/hw/vfio/vfio-container.h
+++ b/include/hw/vfio/vfio-container.h
@@ -117,6 +117,7 @@ vfio_container_get_page_size_mask(const VFIOContainer *bcontainer)
 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
 #define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user"
+#define TYPE_VFIO_IOMMU_APPLE TYPE_VFIO_IOMMU "-apple"
 
 struct VFIOIOMMUClass {
     ObjectClass parent_class;
-- 
2.50.1 (Apple Git-155)