Move sanity and compatibility tests from the attach_dev callback to the
new test_dev callback function. The IOMMU core makes sure an attach_dev
call must be invoked after a successful test_dev call.

Signed-off-by: Nicolin Chen <[email protected]>
---
 drivers/iommu/sun50i-iommu.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c
index d3b190be18b5a..d7517cfb260d5 100644
--- a/drivers/iommu/sun50i-iommu.c
+++ b/drivers/iommu/sun50i-iommu.c
@@ -797,16 +797,21 @@ static struct iommu_domain sun50i_iommu_identity_domain = 
{
        .ops = &sun50i_iommu_identity_ops,
 };
 
+static int sun50i_iommu_domain_test_device(struct iommu_domain *domain,
+                                          struct device *dev, ioasid_t pasid,
+                                          struct iommu_domain *old)
+{
+       if (!sun50i_iommu_from_dev(dev))
+               return -ENODEV;
+       return 0;
+}
+
 static int sun50i_iommu_attach_device(struct iommu_domain *domain,
                                      struct device *dev,
                                      struct iommu_domain *old)
 {
        struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
-       struct sun50i_iommu *iommu;
-
-       iommu = sun50i_iommu_from_dev(dev);
-       if (!iommu)
-               return -ENODEV;
+       struct sun50i_iommu *iommu = sun50i_iommu_from_dev(dev);
 
        dev_dbg(dev, "Attaching to IOMMU domain\n");
 
@@ -851,6 +856,7 @@ static const struct iommu_ops sun50i_iommu_ops = {
        .of_xlate       = sun50i_iommu_of_xlate,
        .probe_device   = sun50i_iommu_probe_device,
        .default_domain_ops = &(const struct iommu_domain_ops) {
+               .test_dev       = sun50i_iommu_domain_test_device,
                .attach_dev     = sun50i_iommu_attach_device,
                .flush_iotlb_all = sun50i_iommu_flush_iotlb_all,
                .iotlb_sync_map = sun50i_iommu_iotlb_sync_map,
-- 
2.43.0


Reply via email to