[PATCH v4 15/17] iommu: Allow IOMMU_RESV_DIRECT to work on ARM

Jason Gunthorpe
2023-04-12T13:51:55Z

For now several ARM drivers do not allow mappings to be created until a
domain is attached. This means they do not technically support
IOMMU_RESV_DIRECT as it requires the 1:1 maps to work continuously.

Currently if the platform requests these maps on ARM systems they are
silently ignored.

Work around this by trying again to establish the direct mappings after
the domain is attached if the pre-attach attempt failed.

In the long run the drivers will be fixed to fully setup domains when they
are created without waiting for attachment.

Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommu.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 8744b15f0bd1f0..4c0908402f0139 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2860,6 +2860,7 @@ static int iommu_setup_default_domain(struct iommu_group *group,
struct iommu_domain *old_dom = group->default_domain;
struct group_device *gdev;
struct iommu_domain *dom;
+ bool direct_failed;
int req_type;
int ret;

@@ -2893,8 +2894,11 @@ static int iommu_setup_default_domain(struct iommu_group *group,
* mapped before their device is attached, in order to guarantee
* continuity with any FW activity
*/
- for_each_group_device(group, gdev)
- iommu_create_device_direct_mappings(dom, gdev->dev);
+ direct_failed = false;
+ for_each_group_device(group, gdev) {
+ if (iommu_create_device_direct_mappings(dom, gdev->dev))
+ direct_failed = true;
+ }

/* We must set default_domain early for __iommu_device_set_domain */
group->default_domain = dom;
@@ -2918,6 +2922,17 @@ static int iommu_setup_default_domain(struct iommu_group *group,
}
}

+ /*
+ * Drivers are supposed to allow mappings to be installed in a domain
+ * before device attachment, but some don't. Hack around this defect by
+ * trying again after attaching. If this happens it means the device
+ * will not continuously have the IOMMU_RESV_DIRECT map.
+ */
+ if (direct_failed) {
+ for_each_group_device(group, gdev)
+ iommu_create_device_direct_mappings(dom, gdev->dev);
+ }
+
out_free:
if (old_dom)
iommu_domain_free(old_dom);
--
2.40.0