]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
iommu: Resolve fwspec ops automatically
authorRobin Murphy <robin.murphy@arm.com>
Tue, 2 Jul 2024 11:40:48 +0000 (12:40 +0100)
committerWill Deacon <will@kernel.org>
Thu, 4 Jul 2024 13:36:03 +0000 (14:36 +0100)
There's no real need for callers to resolve ops from a fwnode in order
to then pass both to iommu_fwspec_init() - it's simpler and more sensible
for that to resolve the ops itself. This in turn means we can centralise
the notion of checking for a present driver, and enforce that fwspecs
aren't allocated unless and until we know they will be usable.

Also use this opportunity to modernise with some "new" helpers that
arrived shortly after this code was first written; the generic
fwnode_handle_get() clears up that ugly get/put mismatch, while
of_fwnode_handle() can now abstract those open-coded dereferences.

Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Link: https://lore.kernel.org/r/0e2727adeb8cd73274425322f2f793561bdc927e.1719919669.git.robin.murphy@arm.com
Signed-off-by: Will Deacon <will@kernel.org>
drivers/acpi/arm64/iort.c
drivers/acpi/scan.c
drivers/acpi/viot.c
drivers/iommu/arm/arm-smmu/arm-smmu.c
drivers/iommu/iommu-priv.h
drivers/iommu/iommu.c
drivers/iommu/mtk_iommu_v1.c
drivers/iommu/of_iommu.c
drivers/iommu/tegra-smmu.c
include/acpi/acpi_bus.h
include/linux/iommu.h

index c0b1c2c194442b71eeea02d56838117eb41d2e9a..1b39e9ae7ac1788c49fa2501242ffab865344a70 100644 (file)
@@ -1221,10 +1221,10 @@ static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
                            u32 streamid)
 {
-       const struct iommu_ops *ops;
        struct fwnode_handle *iort_fwnode;
 
-       if (!node)
+       /* If there's no SMMU driver at all, give up now */
+       if (!node || !iort_iommu_driver_enabled(node->type))
                return -ENODEV;
 
        iort_fwnode = iort_get_fwnode(node);
@@ -1232,19 +1232,10 @@ static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
                return -ENODEV;
 
        /*
-        * If the ops look-up fails, this means that either
-        * the SMMU drivers have not been probed yet or that
-        * the SMMU drivers are not built in the kernel;
-        * Depending on whether the SMMU drivers are built-in
-        * in the kernel or not, defer the IOMMU configuration
-        * or just abort it.
+        * If the SMMU drivers are enabled but not loaded/probed
+        * yet, this will defer.
         */
-       ops = iommu_ops_from_fwnode(iort_fwnode);
-       if (!ops)
-               return iort_iommu_driver_enabled(node->type) ?
-                      -EPROBE_DEFER : -ENODEV;
-
-       return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode, ops);
+       return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode);
 }
 
 struct iort_pci_alias_info {
index 503773707e012719b2adf95e5759ece3ed3b4fe7..8d5a589db1419824d1d9159540707de5ff27b1ee 100644 (file)
@@ -1577,12 +1577,11 @@ int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map)
 
 #ifdef CONFIG_IOMMU_API
 int acpi_iommu_fwspec_init(struct device *dev, u32 id,
-                          struct fwnode_handle *fwnode,
-                          const struct iommu_ops *ops)
+                          struct fwnode_handle *fwnode)
 {
        int ret;
 
-       ret = iommu_fwspec_init(dev, fwnode, ops);
+       ret = iommu_fwspec_init(dev, fwnode);
        if (ret)
                return ret;
 
@@ -1639,8 +1638,7 @@ static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in)
 #else /* !CONFIG_IOMMU_API */
 
 int acpi_iommu_fwspec_init(struct device *dev, u32 id,
-                          struct fwnode_handle *fwnode,
-                          const struct iommu_ops *ops)
+                          struct fwnode_handle *fwnode)
 {
        return -ENODEV;
 }
index c8025921c129b22cf5e4ce49217c178d8b68bd40..2aa69a2fba73b64816ca4b3b86b88de6ea025911 100644 (file)
@@ -307,21 +307,14 @@ void __init acpi_viot_init(void)
 static int viot_dev_iommu_init(struct device *dev, struct viot_iommu *viommu,
                               u32 epid)
 {
-       const struct iommu_ops *ops;
-
-       if (!viommu)
+       if (!viommu || !IS_ENABLED(CONFIG_VIRTIO_IOMMU))
                return -ENODEV;
 
        /* We're not translating ourself */
        if (device_match_fwnode(dev, viommu->fwnode))
                return -EINVAL;
 
-       ops = iommu_ops_from_fwnode(viommu->fwnode);
-       if (!ops)
-               return IS_ENABLED(CONFIG_VIRTIO_IOMMU) ?
-                       -EPROBE_DEFER : -ENODEV;
-
-       return acpi_iommu_fwspec_init(dev, epid, viommu->fwnode, ops);
+       return acpi_iommu_fwspec_init(dev, epid, viommu->fwnode);
 }
 
 static int viot_pci_dev_iommu_init(struct pci_dev *pdev, u16 dev_id, void *data)
index 87c81f75cf844bd58492091bede23a1f2cb867ee..c200e6d3aed535f8ba3d8d80217cdb8935b2821a 100644 (file)
@@ -178,8 +178,7 @@ static int arm_smmu_register_legacy_master(struct device *dev,
                it.cur_count = 1;
        }
 
-       err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
-                               &arm_smmu_ops);
+       err = iommu_fwspec_init(dev, NULL);
        if (err)
                return err;
 
index 5f731d994803c6bb644ad8b078da026be1c51f28..078cafcf49b47e2916b95e48c54b646def7afeca 100644 (file)
@@ -17,6 +17,8 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)
        return dev->iommu->iommu_dev->ops;
 }
 
+const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode);
+
 int iommu_group_replace_domain(struct iommu_group *group,
                               struct iommu_domain *new_domain);
 
index 9df7cc75c1bc57c93b4991439088b6605e359ce8..7618c4285cf9aa6ad1ec1a29b5edcce1dcccd040 100644 (file)
@@ -2822,11 +2822,14 @@ const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode
        return ops;
 }
 
-int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
-                     const struct iommu_ops *ops)
+int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode)
 {
+       const struct iommu_ops *ops = iommu_ops_from_fwnode(iommu_fwnode);
        struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 
+       if (!ops)
+               return -EPROBE_DEFER;
+
        if (fwspec)
                return ops == fwspec->ops ? 0 : -EINVAL;
 
@@ -2838,7 +2841,7 @@ int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
        if (!fwspec)
                return -ENOMEM;
 
-       of_node_get(to_of_node(iommu_fwnode));
+       fwnode_handle_get(iommu_fwnode);
        fwspec->iommu_fwnode = iommu_fwnode;
        fwspec->ops = ops;
        dev_iommu_fwspec_set(dev, fwspec);
index 2b64ea46318f9527d68a135d22b4f1ef98dab7e5..c6ea5b4baff3be9d3b518175ba5816e69f94e132 100644 (file)
@@ -412,7 +412,7 @@ static int mtk_iommu_v1_create_mapping(struct device *dev,
                return -EINVAL;
        }
 
-       ret = iommu_fwspec_init(dev, &args->np->fwnode, &mtk_iommu_v1_ops);
+       ret = iommu_fwspec_init(dev, of_fwnode_handle(args->np));
        if (ret)
                return ret;
 
index 3afe0b48a48db937baa711dc4264d865a9c5f0d7..08c523ad55ad81d40e0b30168118df91ed0d7020 100644 (file)
@@ -21,26 +21,19 @@ static int of_iommu_xlate(struct device *dev,
                          struct of_phandle_args *iommu_spec)
 {
        const struct iommu_ops *ops;
-       struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
        int ret;
 
-       ops = iommu_ops_from_fwnode(fwnode);
-       if ((ops && !ops->of_xlate) ||
-           !of_device_is_available(iommu_spec->np))
+       if (!of_device_is_available(iommu_spec->np))
                return -ENODEV;
 
-       ret = iommu_fwspec_init(dev, fwnode, ops);
+       ret = iommu_fwspec_init(dev, of_fwnode_handle(iommu_spec->np));
+       if (ret == -EPROBE_DEFER)
+               return driver_deferred_probe_check_state(dev);
        if (ret)
                return ret;
-       /*
-        * The otherwise-empty fwspec handily serves to indicate the specific
-        * IOMMU device we're waiting for, which will be useful if we ever get
-        * a proper probe-ordering dependency mechanism in future.
-        */
-       if (!ops)
-               return driver_deferred_probe_check_state(dev);
 
-       if (!try_module_get(ops->owner))
+       ops = dev_iommu_fwspec_get(dev)->ops;
+       if (!ops->of_xlate || !try_module_get(ops->owner))
                return -ENODEV;
 
        ret = ops->of_xlate(dev, iommu_spec);
index f86c7ae91814ff697c8f8b63c9fc183d0e74fd08..4365d9936e68676f50da245e287005675ae1f44e 100644 (file)
@@ -837,7 +837,7 @@ static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
        const struct iommu_ops *ops = smmu->iommu.ops;
        int err;
 
-       err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
+       err = iommu_fwspec_init(dev, of_fwnode_handle(dev->of_node));
        if (err < 0) {
                dev_err(dev, "failed to initialize fwspec: %d\n", err);
                return err;
index 1a4dfd7a1c4a7d92caa1fe4f5afdf49ec1a80aba..9d815837e2975aa3c5514340e54d80de4d41667d 100644 (file)
@@ -736,8 +736,7 @@ struct iommu_ops;
 bool acpi_dma_supported(const struct acpi_device *adev);
 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev);
 int acpi_iommu_fwspec_init(struct device *dev, u32 id,
-                          struct fwnode_handle *fwnode,
-                          const struct iommu_ops *ops);
+                          struct fwnode_handle *fwnode);
 int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map);
 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
                           const u32 *input_id);
index 17b3f36ad843ee98922bfad936262b67524d0886..81893aad9ee49536e42600c2c0f9f47c36c245a6 100644 (file)
@@ -1005,11 +1005,9 @@ struct iommu_mm_data {
        struct list_head        sva_handles;
 };
 
-int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
-                     const struct iommu_ops *ops);
+int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode);
 void iommu_fwspec_free(struct device *dev);
 int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids);
-const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode);
 
 static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
 {
@@ -1315,8 +1313,7 @@ static inline void iommu_device_unlink(struct device *dev, struct device *link)
 }
 
 static inline int iommu_fwspec_init(struct device *dev,
-                                   struct fwnode_handle *iommu_fwnode,
-                                   const struct iommu_ops *ops)
+                                   struct fwnode_handle *iommu_fwnode)
 {
        return -ENODEV;
 }
@@ -1331,12 +1328,6 @@ static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
        return -ENODEV;
 }
 
-static inline
-const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode)
-{
-       return NULL;
-}
-
 static inline int
 iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
 {