On 2022/3/21 19:56, Jason Gunthorpe wrote:
On Sun, Mar 20, 2022 at 02:40:23PM +0800, Lu Baolu wrote:
Add support for SVA domain allocation and provide an SVA-specific
iommu_domain_ops.

Signed-off-by: Lu Baolu <baolu...@linux.intel.com>
  include/linux/intel-iommu.h |  1 +
  drivers/iommu/intel/iommu.c | 12 ++++++++++++
  drivers/iommu/intel/svm.c   | 34 ++++++++++++++++++++++++++++++++++
  3 files changed, 47 insertions(+)

diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h
index 2f9891cb3d00..c14283137fb5 100644
+++ b/include/linux/intel-iommu.h
@@ -744,6 +744,7 @@ void intel_svm_unbind(struct iommu_sva *handle);
  u32 intel_svm_get_pasid(struct iommu_sva *handle);
  int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
                            struct iommu_page_response *msg);
+extern const struct iommu_domain_ops intel_svm_domain_ops;
struct intel_svm_dev {
        struct list_head list;
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index c1b91bce1530..d55dca3eacf8 100644
+++ b/drivers/iommu/intel/iommu.c
@@ -4318,6 +4318,18 @@ static struct iommu_domain 
*intel_iommu_domain_alloc(unsigned type)
                return domain;
        case IOMMU_DOMAIN_IDENTITY:
                return &si_domain->domain;
+#ifdef CONFIG_INTEL_IOMMU_SVM
+       case IOMMU_DOMAIN_SVA:
+               dmar_domain = alloc_domain(type);
+               if (!dmar_domain) {
+                       pr_err("Can't allocate sva domain\n");

Don't put random pr_err's/etc in drivers. At least try to use dev_err

+                       return NULL;
+               }
+               domain = &dmar_domain->domain;
+               domain->ops = &intel_svm_domain_ops;
+
+               return domain;
+#endif /* CONFIG_INTEL_IOMMU_SVM */
        default:
                return NULL;
        }
diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c
index ee5ecde5b318..b9f4dd7057d1 100644
+++ b/drivers/iommu/intel/svm.c
@@ -932,3 +932,37 @@ int intel_svm_page_response(struct device *dev,
        mutex_unlock(&pasid_mutex);
        return ret;
  }
+
+static int intel_svm_attach_dev_pasid(struct iommu_domain *domain,
+                                     struct device *dev, ioasid_t pasid)
+{
+       struct device_domain_info *info = dev_iommu_priv_get(dev);
+       struct mm_struct *mm = domain->sva_cookie;
+       struct intel_iommu *iommu = info->iommu;
+       struct iommu_sva *sva;
+
+       mutex_lock(&pasid_mutex);
+       sva = intel_svm_bind_mm(iommu, dev, mm);
+       mutex_unlock(&pasid_mutex);
+
+       return IS_ERR_OR_NULL(sva);

Never use IS_ERR_OR_NULL(), fix whatever is wrong in intel_svm_bind_mm()
that it can return NULL on failure.

+const struct iommu_domain_ops intel_svm_domain_ops = {
+       .attach_dev_pasid       = intel_svm_attach_dev_pasid,
+       .detach_dev_pasid       = intel_svm_detach_dev_pasid,

Lets have consistent language either this is called SVA or SVM but not
both.

Thanks a lot for above comments. All make sense to me.

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

Reply via email to