Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * OF helpers for IOMMU
0004  *
0005  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/iommu.h>
0010 #include <linux/limits.h>
0011 #include <linux/module.h>
0012 #include <linux/msi.h>
0013 #include <linux/of.h>
0014 #include <linux/of_iommu.h>
0015 #include <linux/of_pci.h>
0016 #include <linux/pci.h>
0017 #include <linux/slab.h>
0018 #include <linux/fsl/mc.h>
0019 
0020 #define NO_IOMMU    1
0021 
0022 static int of_iommu_xlate(struct device *dev,
0023               struct of_phandle_args *iommu_spec)
0024 {
0025     const struct iommu_ops *ops;
0026     struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
0027     int ret;
0028 
0029     ops = iommu_ops_from_fwnode(fwnode);
0030     if ((ops && !ops->of_xlate) ||
0031         !of_device_is_available(iommu_spec->np))
0032         return NO_IOMMU;
0033 
0034     ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
0035     if (ret)
0036         return ret;
0037     /*
0038      * The otherwise-empty fwspec handily serves to indicate the specific
0039      * IOMMU device we're waiting for, which will be useful if we ever get
0040      * a proper probe-ordering dependency mechanism in future.
0041      */
0042     if (!ops)
0043         return driver_deferred_probe_check_state(dev);
0044 
0045     if (!try_module_get(ops->owner))
0046         return -ENODEV;
0047 
0048     ret = ops->of_xlate(dev, iommu_spec);
0049     module_put(ops->owner);
0050     return ret;
0051 }
0052 
0053 static int of_iommu_configure_dev_id(struct device_node *master_np,
0054                      struct device *dev,
0055                      const u32 *id)
0056 {
0057     struct of_phandle_args iommu_spec = { .args_count = 1 };
0058     int err;
0059 
0060     err = of_map_id(master_np, *id, "iommu-map",
0061              "iommu-map-mask", &iommu_spec.np,
0062              iommu_spec.args);
0063     if (err)
0064         return err == -ENODEV ? NO_IOMMU : err;
0065 
0066     err = of_iommu_xlate(dev, &iommu_spec);
0067     of_node_put(iommu_spec.np);
0068     return err;
0069 }
0070 
0071 static int of_iommu_configure_dev(struct device_node *master_np,
0072                   struct device *dev)
0073 {
0074     struct of_phandle_args iommu_spec;
0075     int err = NO_IOMMU, idx = 0;
0076 
0077     while (!of_parse_phandle_with_args(master_np, "iommus",
0078                        "#iommu-cells",
0079                        idx, &iommu_spec)) {
0080         err = of_iommu_xlate(dev, &iommu_spec);
0081         of_node_put(iommu_spec.np);
0082         idx++;
0083         if (err)
0084             break;
0085     }
0086 
0087     return err;
0088 }
0089 
0090 struct of_pci_iommu_alias_info {
0091     struct device *dev;
0092     struct device_node *np;
0093 };
0094 
0095 static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
0096 {
0097     struct of_pci_iommu_alias_info *info = data;
0098     u32 input_id = alias;
0099 
0100     return of_iommu_configure_dev_id(info->np, info->dev, &input_id);
0101 }
0102 
0103 static int of_iommu_configure_device(struct device_node *master_np,
0104                      struct device *dev, const u32 *id)
0105 {
0106     return (id) ? of_iommu_configure_dev_id(master_np, dev, id) :
0107               of_iommu_configure_dev(master_np, dev);
0108 }
0109 
0110 const struct iommu_ops *of_iommu_configure(struct device *dev,
0111                        struct device_node *master_np,
0112                        const u32 *id)
0113 {
0114     const struct iommu_ops *ops = NULL;
0115     struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
0116     int err = NO_IOMMU;
0117 
0118     if (!master_np)
0119         return NULL;
0120 
0121     if (fwspec) {
0122         if (fwspec->ops)
0123             return fwspec->ops;
0124 
0125         /* In the deferred case, start again from scratch */
0126         iommu_fwspec_free(dev);
0127     }
0128 
0129     /*
0130      * We don't currently walk up the tree looking for a parent IOMMU.
0131      * See the `Notes:' section of
0132      * Documentation/devicetree/bindings/iommu/iommu.txt
0133      */
0134     if (dev_is_pci(dev)) {
0135         struct of_pci_iommu_alias_info info = {
0136             .dev = dev,
0137             .np = master_np,
0138         };
0139 
0140         pci_request_acs();
0141         err = pci_for_each_dma_alias(to_pci_dev(dev),
0142                          of_pci_iommu_init, &info);
0143     } else {
0144         err = of_iommu_configure_device(master_np, dev, id);
0145     }
0146 
0147     /*
0148      * Two success conditions can be represented by non-negative err here:
0149      * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
0150      *  0 : we found an IOMMU, and dev->fwspec is initialised appropriately
0151      * <0 : any actual error
0152      */
0153     if (!err) {
0154         /* The fwspec pointer changed, read it again */
0155         fwspec = dev_iommu_fwspec_get(dev);
0156         ops    = fwspec->ops;
0157     }
0158     /*
0159      * If we have reason to believe the IOMMU driver missed the initial
0160      * probe for dev, replay it to get things in order.
0161      */
0162     if (!err && dev->bus && !device_iommu_mapped(dev))
0163         err = iommu_probe_device(dev);
0164 
0165     /* Ignore all other errors apart from EPROBE_DEFER */
0166     if (err == -EPROBE_DEFER) {
0167         ops = ERR_PTR(err);
0168     } else if (err < 0) {
0169         dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
0170         ops = NULL;
0171     }
0172 
0173     return ops;
0174 }