iommu/vt-d: Add domain_alloc_user op
authorYi Liu <yi.l.liu@intel.com>
Thu, 28 Sep 2023 07:15:28 +0000 (00:15 -0700)
committerJason Gunthorpe <jgg@nvidia.com>
Tue, 10 Oct 2023 16:31:24 +0000 (13:31 -0300)
Add the domain_alloc_user() op implementation. It supports allocating
domains to be used as parent under nested translation.

Unlike other drivers VT-D uses only a single page table format so it only
needs to check if the HW can support nesting.

Link: https://lore.kernel.org/r/20230928071528.26258-7-yi.l.liu@intel.com
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/iommu/intel/iommu.c

index 5db283c17e0dc5cd7ffd0147efec316595df53c8..017aed5813d8d3f5af1288bff6bcb3d64937cf05 100644 (file)
@@ -4074,6 +4074,33 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
        return NULL;
 }
 
+static struct iommu_domain *
+intel_iommu_domain_alloc_user(struct device *dev, u32 flags)
+{
+       struct iommu_domain *domain;
+       struct intel_iommu *iommu;
+
+       if (flags & (~IOMMU_HWPT_ALLOC_NEST_PARENT))
+               return ERR_PTR(-EOPNOTSUPP);
+
+       iommu = device_to_iommu(dev, NULL, NULL);
+       if (!iommu)
+               return ERR_PTR(-ENODEV);
+
+       if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !ecap_nest(iommu->ecap))
+               return ERR_PTR(-EOPNOTSUPP);
+
+       /*
+        * domain_alloc_user op needs to fully initialize a domain
+        * before return, so uses iommu_domain_alloc() here for
+        * simple.
+        */
+       domain = iommu_domain_alloc(dev->bus);
+       if (!domain)
+               domain = ERR_PTR(-ENOMEM);
+       return domain;
+}
+
 static void intel_iommu_domain_free(struct iommu_domain *domain)
 {
        if (domain != &si_domain->domain && domain != &blocking_domain)
@@ -4807,6 +4834,7 @@ const struct iommu_ops intel_iommu_ops = {
        .capable                = intel_iommu_capable,
        .hw_info                = intel_iommu_hw_info,
        .domain_alloc           = intel_iommu_domain_alloc,
+       .domain_alloc_user      = intel_iommu_domain_alloc_user,
        .probe_device           = intel_iommu_probe_device,
        .probe_finalize         = intel_iommu_probe_finalize,
        .release_device         = intel_iommu_release_device,