Allow iommu_change_dev_def_domain() to create a new default domain, keeping
the same as current.

Also remove comment about the function purpose, which will become stale.

Signed-off-by: John Garry <john.ga...@huawei.com>
---
 drivers/iommu/iommu.c | 49 ++++++++++++++++++++++---------------------
 include/linux/iommu.h |  1 +
 2 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 0dd766030baf..10bb10c2a210 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2863,6 +2863,7 @@ u32 iommu_sva_get_pasid(struct iommu_sva *handle)
 }
 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
 
+
 /*
  * Changes the default domain of an iommu group that has *only* one device
  *
@@ -2873,10 +2874,6 @@ EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
  *
  * Returns 0 on success and error code on failure
  *
- * Note:
- * 1. Presently, this function is called only when user requests to change the
- *    group's default domain type through 
/sys/kernel/iommu_groups/<grp_id>/type
- *    Please take a closer look if intended to use for other purposes.
  */
 static int iommu_change_dev_def_domain(struct iommu_group *group,
                                       struct device *prev_dev, int type)
@@ -2929,28 +2926,32 @@ static int iommu_change_dev_def_domain(struct 
iommu_group *group,
                goto out;
        }
 
-       dev_def_dom = iommu_get_def_domain_type(dev);
-       if (!type) {
+       if (type == __IOMMU_DOMAIN_SAME) {
+               type = prev_dom->type;
+       } else {
+               dev_def_dom = iommu_get_def_domain_type(dev);
+               if (!type) {
+                       /*
+                        * If the user hasn't requested any specific type of 
domain and
+                        * if the device supports both the domains, then 
default to the
+                        * domain the device was booted with
+                        */
+                       type = dev_def_dom ? : iommu_def_domain_type;
+               } else if (dev_def_dom && type != dev_def_dom) {
+                       dev_err_ratelimited(prev_dev, "Device cannot be in %s 
domain\n",
+                                           iommu_domain_type_str(type));
+                       ret = -EINVAL;
+                       goto out;
+               }
+
                /*
-                * If the user hasn't requested any specific type of domain and
-                * if the device supports both the domains, then default to the
-                * domain the device was booted with
+                * Switch to a new domain only if the requested domain type is 
different
+                * from the existing default domain type
                 */
-               type = dev_def_dom ? : iommu_def_domain_type;
-       } else if (dev_def_dom && type != dev_def_dom) {
-               dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n",
-                                   iommu_domain_type_str(type));
-               ret = -EINVAL;
-               goto out;
-       }
-
-       /*
-        * Switch to a new domain only if the requested domain type is different
-        * from the existing default domain type
-        */
-       if (prev_dom->type == type) {
-               ret = 0;
-               goto out;
+               if (prev_dom->type == type) {
+                       ret = 0;
+                       goto out;
+               }
        }
 
        /* We can bring up a flush queue without tearing down the domain */
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 9208eca4b0d1..b141cf71c7af 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -63,6 +63,7 @@ struct iommu_domain_geometry {
                                              implementation              */
 #define __IOMMU_DOMAIN_PT      (1U << 2)  /* Domain is identity mapped   */
 #define __IOMMU_DOMAIN_DMA_FQ  (1U << 3)  /* DMA-API uses flush queue    */
+#define __IOMMU_DOMAIN_SAME    (1U << 4)  /* Keep same type (internal)   */
 
 /*
  * This are the possible domain-types
-- 
2.26.2

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

Reply via email to