From: Jason Gunthorpe <j...@nvidia.com>

Now, ATC invalidation can be done with the vIOMMU invalidation op. A guest
owned IOMMU_DOMAIN_NESTED can do an ATS too. Allow it to pass in the EATS
field via the vSTE words.

Signed-off-by: Jason Gunthorpe <j...@nvidia.com>
Signed-off-by: Nicolin Chen <nicol...@nvidia.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  4 ++-
 include/uapi/linux/iommufd.h                  |  2 +-
 .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c     | 31 ++++++++++++++++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 27 +++++++++++++---
 4 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h 
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
index 000af931a30c..470bc3ee25ef 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
@@ -305,7 +305,7 @@ static inline u32 arm_smmu_strtab_l2_idx(u32 sid)
 #define STRTAB_STE_1_NESTING_ALLOWED                            \
        cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR |   \
                    STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH |   \
-                   STRTAB_STE_1_S1STALLD)
+                   STRTAB_STE_1_S1STALLD | STRTAB_STE_1_EATS)
 
 /*
  * Context descriptors.
@@ -838,6 +838,7 @@ struct arm_smmu_domain {
 struct arm_smmu_nested_domain {
        struct iommu_domain domain;
        struct arm_smmu_domain *s2_parent;
+       bool enable_ats : 1;
 
        __le64 ste[2];
 };
@@ -879,6 +880,7 @@ struct arm_smmu_master_domain {
        struct list_head devices_elm;
        struct arm_smmu_master *master;
        ioasid_t ssid;
+       bool nested_ats_flush : 1;
 };
 
 static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index d9e510ce67cf..9527a4ecfd56 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -404,7 +404,7 @@ struct iommu_hwpt_vtd_s1 {
  *       a user stage-1 Context Descriptor Table. Must be little-endian.
  *       Allowed fields: (Refer to "5.2 Stream Table Entry" in SMMUv3 HW Spec)
  *       - word-0: V, Cfg, S1Fmt, S1ContextPtr, S1CDMax
- *       - word-1: S1DSS, S1CIR, S1COR, S1CSH, S1STALLD
+ *       - word-1: EATS, S1DSS, S1CIR, S1COR, S1CSH, S1STALLD
  *
  * -EIO will be returned if @ste is not legal or contains any non-allowed 
field.
  * Cfg can be used to select a S1, Bypass or Abort configuration. A Bypass
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c 
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
index 1b82579eb252..b491017921df 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
@@ -103,8 +103,6 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain 
*domain,
                .master = master,
                .old_domain = iommu_get_domain_for_dev(dev),
                .ssid = IOMMU_NO_PASID,
-               /* Currently invalidation of ATC is not supported */
-               .disable_ats = true,
        };
        struct arm_smmu_ste ste;
        int ret;
@@ -115,6 +113,15 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain 
*domain,
                return -EBUSY;
 
        mutex_lock(&arm_smmu_asid_lock);
+       /*
+        * The VM has to control the actual ATS state at the PCI device because
+        * we forward the invalidations directly from the VM. If the VM doesn't
+        * think ATS is on it will not generate ATC flushes and the ATC will
+        * become incoherent. Since we can't access the actual virtual PCI ATS
+        * config bit here base this off the EATS value in the STE. If the EATS
+        * is set then the VM must generate ATC flushes.
+        */
+       state.disable_ats = !nested_domain->enable_ats;
        ret = arm_smmu_attach_prepare(&state, domain);
        if (ret) {
                mutex_unlock(&arm_smmu_asid_lock);
@@ -140,8 +147,10 @@ static const struct iommu_domain_ops arm_smmu_nested_ops = 
{
        .free = arm_smmu_domain_nested_free,
 };
 
-static int arm_smmu_validate_vste(struct iommu_hwpt_arm_smmuv3 *arg)
+static int arm_smmu_validate_vste(struct iommu_hwpt_arm_smmuv3 *arg,
+                                 bool *enable_ats)
 {
+       unsigned int eats;
        unsigned int cfg;
 
        if (!(arg->ste[0] & cpu_to_le64(STRTAB_STE_0_V))) {
@@ -158,6 +167,18 @@ static int arm_smmu_validate_vste(struct 
iommu_hwpt_arm_smmuv3 *arg)
        if (cfg != STRTAB_STE_0_CFG_ABORT && cfg != STRTAB_STE_0_CFG_BYPASS &&
            cfg != STRTAB_STE_0_CFG_S1_TRANS)
                return -EIO;
+
+       /*
+        * Only Full ATS or ATS UR is supported
+        * The EATS field will be set by arm_smmu_make_nested_domain_ste()
+        */
+       eats = FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(arg->ste[1]));
+       arg->ste[1] &= ~cpu_to_le64(STRTAB_STE_1_EATS);
+       if (eats != STRTAB_STE_1_EATS_ABT && eats != STRTAB_STE_1_EATS_TRANS)
+               return -EIO;
+
+       if (cfg == STRTAB_STE_0_CFG_S1_TRANS)
+               *enable_ats = (eats == STRTAB_STE_1_EATS_TRANS);
        return 0;
 }
 
@@ -170,6 +191,7 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags,
        struct arm_smmu_nested_domain *nested_domain;
        struct arm_smmu_domain *smmu_parent;
        struct iommu_hwpt_arm_smmuv3 arg;
+       bool enable_ats = false;
        int ret;
 
        if (flags || !(master->smmu->features & ARM_SMMU_FEAT_NESTING))
@@ -204,7 +226,7 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags,
        if (ret)
                return ERR_PTR(ret);
 
-       ret = arm_smmu_validate_vste(&arg);
+       ret = arm_smmu_validate_vste(&arg, &enable_ats);
        if (ret)
                return ERR_PTR(ret);
 
@@ -215,6 +237,7 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags,
        nested_domain->domain.type = IOMMU_DOMAIN_NESTED;
        nested_domain->domain.ops = &arm_smmu_nested_ops;
        nested_domain->s2_parent = smmu_parent;
+       nested_domain->enable_ats = enable_ats;
        nested_domain->ste[0] = arg.ste[0];
        nested_domain->ste[1] = arg.ste[1] & ~cpu_to_le64(STRTAB_STE_1_EATS);
 
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c 
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index a2bbd140e232..1cb4afe7a90a 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2107,7 +2107,16 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain 
*smmu_domain,
                if (!master->ats_enabled)
                        continue;
 
-               arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, &cmd);
+               if (master_domain->nested_ats_flush) {
+                       /*
+                        * If a S2 used as a nesting parent is changed we have
+                        * no option but to completely flush the ATC.
+                        */
+                       arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd);
+               } else {
+                       arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size,
+                                               &cmd);
+               }
 
                for (i = 0; i < master->num_streams; i++) {
                        cmd.atc.sid = master->streams[i].id;
@@ -2630,7 +2639,8 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master 
*master)
 
 static struct arm_smmu_master_domain *
 arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain,
-                           struct arm_smmu_master *master, ioasid_t ssid)
+                           struct arm_smmu_master *master, ioasid_t ssid,
+                           bool nested_ats_flush)
 {
        struct arm_smmu_master_domain *master_domain;
 
@@ -2639,7 +2649,8 @@ arm_smmu_find_master_domain(struct arm_smmu_domain 
*smmu_domain,
        list_for_each_entry(master_domain, &smmu_domain->devices,
                            devices_elm) {
                if (master_domain->master == master &&
-                   master_domain->ssid == ssid)
+                   master_domain->ssid == ssid &&
+                   master_domain->nested_ats_flush == nested_ats_flush)
                        return master_domain;
        }
        return NULL;
@@ -2670,13 +2681,18 @@ static void arm_smmu_remove_master_domain(struct 
arm_smmu_master *master,
 {
        struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(domain);
        struct arm_smmu_master_domain *master_domain;
+       bool nested_ats_flush = false;
        unsigned long flags;
 
        if (!smmu_domain)
                return;
 
+       if (domain->type == IOMMU_DOMAIN_NESTED)
+               nested_ats_flush = to_smmu_nested_domain(domain)->enable_ats;
+
        spin_lock_irqsave(&smmu_domain->devices_lock, flags);
-       master_domain = arm_smmu_find_master_domain(smmu_domain, master, ssid);
+       master_domain = arm_smmu_find_master_domain(smmu_domain, master, ssid,
+                                                   nested_ats_flush);
        if (master_domain) {
                list_del(&master_domain->devices_elm);
                kfree(master_domain);
@@ -2743,6 +2759,9 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_state 
*state,
                        return -ENOMEM;
                master_domain->master = master;
                master_domain->ssid = state->ssid;
+               if (new_domain->type == IOMMU_DOMAIN_NESTED)
+                       master_domain->nested_ats_flush =
+                               to_smmu_nested_domain(new_domain)->enable_ats;
 
                /*
                 * During prepare we want the current smmu_domain and new
-- 
2.43.0


Reply via email to