iommu/arm-smmu: Treat IOMMU_DOMAIN_DMA as bypass for now
[cascardo/linux.git] / drivers / iommu / arm-smmu-v3.c
index 2087534..4ff73ff 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <linux/delay.h>
+#include <linux/dma-iommu.h>
 #include <linux/err.h>
 #include <linux/interrupt.h>
 #include <linux/iommu.h>
@@ -1396,7 +1397,7 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
 {
        struct arm_smmu_domain *smmu_domain;
 
-       if (type != IOMMU_DOMAIN_UNMANAGED)
+       if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
                return NULL;
 
        /*
@@ -1408,6 +1409,12 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
        if (!smmu_domain)
                return NULL;
 
+       if (type == IOMMU_DOMAIN_DMA &&
+           iommu_get_dma_cookie(&smmu_domain->domain)) {
+               kfree(smmu_domain);
+               return NULL;
+       }
+
        mutex_init(&smmu_domain->init_mutex);
        spin_lock_init(&smmu_domain->pgtbl_lock);
        return &smmu_domain->domain;
@@ -1436,6 +1443,7 @@ static void arm_smmu_domain_free(struct iommu_domain *domain)
        struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
        struct arm_smmu_device *smmu = smmu_domain->smmu;
 
+       iommu_put_dma_cookie(domain);
        free_io_pgtable_ops(smmu_domain->pgtbl_ops);
 
        /* Free the CD and ASID, if we allocated them */
@@ -1630,6 +1638,17 @@ static int arm_smmu_install_ste_for_group(struct arm_smmu_group *smmu_group)
        return 0;
 }
 
+static void arm_smmu_detach_dev(struct device *dev)
+{
+       struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
+
+       smmu_group->ste.bypass = true;
+       if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
+               dev_warn(dev, "failed to install bypass STE\n");
+
+       smmu_group->domain = NULL;
+}
+
 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 {
        int ret = 0;
@@ -1642,7 +1661,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 
        /* Already attached to a different domain? */
        if (smmu_group->domain && smmu_group->domain != smmu_domain)
-               return -EEXIST;
+               arm_smmu_detach_dev(dev);
 
        smmu = smmu_group->smmu;
        mutex_lock(&smmu_domain->init_mutex);
@@ -1668,7 +1687,12 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
                goto out_unlock;
 
        smmu_group->domain      = smmu_domain;
-       smmu_group->ste.bypass  = false;
+
+       /*
+        * FIXME: This should always be "false" once we have IOMMU-backed
+        * DMA ops for all devices behind the SMMU.
+        */
+       smmu_group->ste.bypass  = domain->type == IOMMU_DOMAIN_DMA;
 
        ret = arm_smmu_install_ste_for_group(smmu_group);
        if (IS_ERR_VALUE(ret))
@@ -1679,25 +1703,6 @@ out_unlock:
        return ret;
 }
 
-static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
-{
-       struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
-       struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
-
-       BUG_ON(!smmu_domain);
-       BUG_ON(!smmu_group);
-
-       mutex_lock(&smmu_domain->init_mutex);
-       BUG_ON(smmu_group->domain != smmu_domain);
-
-       smmu_group->ste.bypass = true;
-       if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
-               dev_warn(dev, "failed to install bypass STE\n");
-
-       smmu_group->domain = NULL;
-       mutex_unlock(&smmu_domain->init_mutex);
-}
-
 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
                        phys_addr_t paddr, size_t size, int prot)
 {
@@ -1935,7 +1940,6 @@ static struct iommu_ops arm_smmu_ops = {
        .domain_alloc           = arm_smmu_domain_alloc,
        .domain_free            = arm_smmu_domain_free,
        .attach_dev             = arm_smmu_attach_dev,
-       .detach_dev             = arm_smmu_detach_dev,
        .map                    = arm_smmu_map,
        .unmap                  = arm_smmu_unmap,
        .iova_to_phys           = arm_smmu_iova_to_phys,