Multiple fsl-mc devices may be placed in the same IOMMU group because
they cannot be isolated from each other. These devices must either be
entirely under kernel control or userspace control, never a mixture. This
checks and sets DMA ownership during driver binding, and release the
ownership during driver unbinding.

Driver may set a new flag (suppress_auto_claim_dma_owner) to disable auto
claiming DMA_OWNER_DMA_API ownership in the binding process. For instance,
the userspace framework drivers (vfio etc.) which need to manually claim
DMA_OWNER_PRIVATE_DOMAIN_USER when assigning a device to userspace.

Signed-off-by: Lu Baolu <baolu...@linux.intel.com>
---
 include/linux/fsl/mc.h          |  5 +++++
 drivers/bus/fsl-mc/fsl-mc-bus.c | 26 ++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/include/linux/fsl/mc.h b/include/linux/fsl/mc.h
index e026f6c48b49..3a26421ee3dc 100644
--- a/include/linux/fsl/mc.h
+++ b/include/linux/fsl/mc.h
@@ -32,6 +32,10 @@ struct fsl_mc_io;
  * @shutdown: Function called at shutdown time to quiesce the device
  * @suspend: Function called when a device is stopped
  * @resume: Function called when a device is resumed
+ * @suppress_auto_claim_dma_owner: Disable auto claiming of kernel DMA owner.
+ *             Drivers which don't require DMA or want to manually claim the
+ *             owner type (e.g. userspace driver frameworks) could set this
+ *             flag.
  *
  * Generic DPAA device driver object for device drivers that are registered
  * with a DPRC bus. This structure is to be embedded in each device-specific
@@ -45,6 +49,7 @@ struct fsl_mc_driver {
        void (*shutdown)(struct fsl_mc_device *dev);
        int (*suspend)(struct fsl_mc_device *dev, pm_message_t state);
        int (*resume)(struct fsl_mc_device *dev);
+       bool suppress_auto_claim_dma_owner;
 };
 
 #define to_fsl_mc_driver(_drv) \
diff --git a/drivers/bus/fsl-mc/fsl-mc-bus.c b/drivers/bus/fsl-mc/fsl-mc-bus.c
index 8fd4a356a86e..048a837a7116 100644
--- a/drivers/bus/fsl-mc/fsl-mc-bus.c
+++ b/drivers/bus/fsl-mc/fsl-mc-bus.c
@@ -140,15 +140,36 @@ static int fsl_mc_dma_configure(struct device *dev)
 {
        struct device *dma_dev = dev;
        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
+       struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
        u32 input_id = mc_dev->icid;
+       int ret;
+
+       if (!mc_drv->suppress_auto_claim_dma_owner) {
+               ret = iommu_device_set_dma_owner(dev, DMA_OWNER_DMA_API, NULL);
+               if (ret)
+                       return ret;
+       }
 
        while (dev_is_fsl_mc(dma_dev))
                dma_dev = dma_dev->parent;
 
        if (dev_of_node(dma_dev))
-               return of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
+               ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
+       else
+               ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
+
+       if (ret && !mc_drv->suppress_auto_claim_dma_owner)
+               iommu_device_release_dma_owner(dev, DMA_OWNER_DMA_API);
+
+       return ret;
+}
+
+static void fsl_mc_dma_cleanup(struct device *dev)
+{
+       struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
 
-       return acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
+       if (!mc_drv->suppress_auto_claim_dma_owner)
+               iommu_device_release_dma_owner(dev, DMA_OWNER_DMA_API);
 }
 
 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
@@ -312,6 +333,7 @@ struct bus_type fsl_mc_bus_type = {
        .match = fsl_mc_bus_match,
        .uevent = fsl_mc_bus_uevent,
        .dma_configure  = fsl_mc_dma_configure,
+       .dma_cleanup = fsl_mc_dma_cleanup,
        .dev_groups = fsl_mc_dev_groups,
        .bus_groups = fsl_mc_bus_groups,
 };
-- 
2.25.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Reply via email to