1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
8 #include <linux/export.h>
9 #include <linux/iommu.h>
10 #include <linux/limits.h>
11 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/of_iommu.h>
15 #include <linux/of_pci.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 #include <linux/fsl/mc.h>
22 static int of_iommu_xlate(struct device *dev,
23 struct of_phandle_args *iommu_spec)
25 const struct iommu_ops *ops;
26 struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
29 ops = iommu_ops_from_fwnode(fwnode);
30 if ((ops && !ops->of_xlate) ||
31 !of_device_is_available(iommu_spec->np))
34 ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
38 * The otherwise-empty fwspec handily serves to indicate the specific
39 * IOMMU device we're waiting for, which will be useful if we ever get
40 * a proper probe-ordering dependency mechanism in future.
43 return driver_deferred_probe_check_state(dev);
45 if (!try_module_get(ops->owner))
48 ret = ops->of_xlate(dev, iommu_spec);
49 module_put(ops->owner);
53 static int of_iommu_configure_dev_id(struct device_node *master_np,
57 struct of_phandle_args iommu_spec = { .args_count = 1 };
60 err = of_map_id(master_np, *id, "iommu-map",
61 "iommu-map-mask", &iommu_spec.np,
64 return err == -ENODEV ? NO_IOMMU : err;
66 err = of_iommu_xlate(dev, &iommu_spec);
67 of_node_put(iommu_spec.np);
71 static int of_iommu_configure_dev(struct device_node *master_np,
74 struct of_phandle_args iommu_spec;
75 int err = NO_IOMMU, idx = 0;
77 while (!of_parse_phandle_with_args(master_np, "iommus",
80 err = of_iommu_xlate(dev, &iommu_spec);
81 of_node_put(iommu_spec.np);
90 struct of_pci_iommu_alias_info {
92 struct device_node *np;
95 static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
97 struct of_pci_iommu_alias_info *info = data;
100 return of_iommu_configure_dev_id(info->np, info->dev, &input_id);
103 static int of_iommu_configure_device(struct device_node *master_np,
104 struct device *dev, const u32 *id)
106 return (id) ? of_iommu_configure_dev_id(master_np, dev, id) :
107 of_iommu_configure_dev(master_np, dev);
110 const struct iommu_ops *of_iommu_configure(struct device *dev,
111 struct device_node *master_np,
114 const struct iommu_ops *ops = NULL;
115 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
125 /* In the deferred case, start again from scratch */
126 iommu_fwspec_free(dev);
130 * We don't currently walk up the tree looking for a parent IOMMU.
131 * See the `Notes:' section of
132 * Documentation/devicetree/bindings/iommu/iommu.txt
134 if (dev_is_pci(dev)) {
135 struct of_pci_iommu_alias_info info = {
141 err = pci_for_each_dma_alias(to_pci_dev(dev),
142 of_pci_iommu_init, &info);
144 err = of_iommu_configure_device(master_np, dev, id);
148 * Two success conditions can be represented by non-negative err here:
149 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
150 * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately
151 * <0 : any actual error
154 /* The fwspec pointer changed, read it again */
155 fwspec = dev_iommu_fwspec_get(dev);
159 * If we have reason to believe the IOMMU driver missed the initial
160 * probe for dev, replay it to get things in order.
162 if (!err && dev->bus)
163 err = iommu_probe_device(dev);
165 /* Ignore all other errors apart from EPROBE_DEFER */
166 if (err == -EPROBE_DEFER) {
168 } else if (err < 0) {
169 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
176 static enum iommu_resv_type __maybe_unused
177 iommu_resv_region_get_type(struct device *dev,
178 struct resource *phys,
179 phys_addr_t start, size_t length)
181 phys_addr_t end = start + length - 1;
184 * IOMMU regions without an associated physical region cannot be
185 * mapped and are simply reservations.
187 if (phys->start >= phys->end)
188 return IOMMU_RESV_RESERVED;
190 /* may be IOMMU_RESV_DIRECT_RELAXABLE for certain cases */
191 if (start == phys->start && end == phys->end)
192 return IOMMU_RESV_DIRECT;
194 dev_warn(dev, "treating non-direct mapping [%pr] -> [%pap-%pap] as reservation\n", &phys,
196 return IOMMU_RESV_RESERVED;
200 * of_iommu_get_resv_regions - reserved region driver helper for device tree
201 * @dev: device for which to get reserved regions
202 * @list: reserved region list
204 * IOMMU drivers can use this to implement their .get_resv_regions() callback
205 * for memory regions attached to a device tree node. See the reserved-memory
206 * device tree bindings on how to use these:
208 * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
210 void of_iommu_get_resv_regions(struct device *dev, struct list_head *list)
212 #if IS_ENABLED(CONFIG_OF_ADDRESS)
213 struct of_phandle_iterator it;
216 of_for_each_phandle(&it, err, dev->of_node, "memory-region", NULL, 0) {
217 const __be32 *maps, *end;
218 struct resource phys;
221 memset(&phys, 0, sizeof(phys));
224 * The "reg" property is optional and can be omitted by reserved-memory regions
225 * that represent reservations in the IOVA space, which are regions that should
228 if (of_find_property(it.node, "reg", NULL)) {
229 err = of_address_to_resource(it.node, 0, &phys);
231 dev_err(dev, "failed to parse memory region %pOF: %d\n",
237 maps = of_get_property(it.node, "iommu-addresses", &size);
241 end = maps + size / sizeof(__be32);
244 struct device_node *np;
247 phandle = be32_to_cpup(maps++);
248 np = of_find_node_by_phandle(phandle);
250 if (np == dev->of_node) {
251 int prot = IOMMU_READ | IOMMU_WRITE;
252 struct iommu_resv_region *region;
253 enum iommu_resv_type type;
257 maps = of_translate_dma_region(np, maps, &iova, &length);
258 type = iommu_resv_region_get_type(dev, &phys, iova, length);
260 region = iommu_alloc_resv_region(iova, length, prot, type,
263 list_add_tail(®ion->list, list);
269 EXPORT_SYMBOL(of_iommu_get_resv_regions);