1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * OF helpers for IOMMU
4 *
5 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
6 */
7
8#include <linux/export.h>
9#include <linux/iommu.h>
10#include <linux/limits.h>
11#include <linux/module.h>
12#include <linux/of.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>
19
20#define NO_IOMMU	1
21
22static int of_iommu_xlate(struct device *dev,
23			  struct of_phandle_args *iommu_spec)
24{
25	const struct iommu_ops *ops;
26	struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
27	int ret;
28
29	ops = iommu_ops_from_fwnode(fwnode);
30	if ((ops && !ops->of_xlate) ||
31	    !of_device_is_available(iommu_spec->np))
32		return NO_IOMMU;
33
34	ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
35	if (ret)
36		return ret;
37	/*
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.
41	 */
42	if (!ops)
43		return driver_deferred_probe_check_state(dev);
44
45	if (!try_module_get(ops->owner))
46		return -ENODEV;
47
48	ret = ops->of_xlate(dev, iommu_spec);
49	module_put(ops->owner);
50	return ret;
51}
52
53static int of_iommu_configure_dev_id(struct device_node *master_np,
54				     struct device *dev,
55				     const u32 *id)
56{
57	struct of_phandle_args iommu_spec = { .args_count = 1 };
58	int err;
59
60	err = of_map_id(master_np, *id, "iommu-map",
61			 "iommu-map-mask", &iommu_spec.np,
62			 iommu_spec.args);
63	if (err)
64		return err == -ENODEV ? NO_IOMMU : err;
65
66	err = of_iommu_xlate(dev, &iommu_spec);
67	of_node_put(iommu_spec.np);
68	return err;
69}
70
71static int of_iommu_configure_dev(struct device_node *master_np,
72				  struct device *dev)
73{
74	struct of_phandle_args iommu_spec;
75	int err = NO_IOMMU, idx = 0;
76
77	while (!of_parse_phandle_with_args(master_np, "iommus",
78					   "#iommu-cells",
79					   idx, &iommu_spec)) {
80		err = of_iommu_xlate(dev, &iommu_spec);
81		of_node_put(iommu_spec.np);
82		idx++;
83		if (err)
84			break;
85	}
86
87	return err;
88}
89
90struct of_pci_iommu_alias_info {
91	struct device *dev;
92	struct device_node *np;
93};
94
95static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
96{
97	struct of_pci_iommu_alias_info *info = data;
98	u32 input_id = alias;
99
100	return of_iommu_configure_dev_id(info->np, info->dev, &input_id);
101}
102
103static int of_iommu_configure_device(struct device_node *master_np,
104				     struct device *dev, const u32 *id)
105{
106	return (id) ? of_iommu_configure_dev_id(master_np, dev, id) :
107		      of_iommu_configure_dev(master_np, dev);
108}
109
110const struct iommu_ops *of_iommu_configure(struct device *dev,
111					   struct device_node *master_np,
112					   const u32 *id)
113{
114	const struct iommu_ops *ops = NULL;
115	struct iommu_fwspec *fwspec;
116	int err = NO_IOMMU;
117
118	if (!master_np)
119		return NULL;
120
121	/* Serialise to make dev->iommu stable under our potential fwspec */
122	mutex_lock(&iommu_probe_device_lock);
123	fwspec = dev_iommu_fwspec_get(dev);
124	if (fwspec) {
125		if (fwspec->ops) {
126			mutex_unlock(&iommu_probe_device_lock);
127			return fwspec->ops;
128		}
129		/* In the deferred case, start again from scratch */
130		iommu_fwspec_free(dev);
131	}
132
133	/*
134	 * We don't currently walk up the tree looking for a parent IOMMU.
135	 * See the `Notes:' section of
136	 * Documentation/devicetree/bindings/iommu/iommu.txt
137	 */
138	if (dev_is_pci(dev)) {
139		struct of_pci_iommu_alias_info info = {
140			.dev = dev,
141			.np = master_np,
142		};
143
144		pci_request_acs();
145		err = pci_for_each_dma_alias(to_pci_dev(dev),
146					     of_pci_iommu_init, &info);
147	} else {
148		err = of_iommu_configure_device(master_np, dev, id);
149	}
150
151	/*
152	 * Two success conditions can be represented by non-negative err here:
153	 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
154	 *  0 : we found an IOMMU, and dev->fwspec is initialised appropriately
155	 * <0 : any actual error
156	 */
157	if (!err) {
158		/* The fwspec pointer changed, read it again */
159		fwspec = dev_iommu_fwspec_get(dev);
160		ops    = fwspec->ops;
161	}
162	mutex_unlock(&iommu_probe_device_lock);
163
164	/*
165	 * If we have reason to believe the IOMMU driver missed the initial
166	 * probe for dev, replay it to get things in order.
167	 */
168	if (!err && dev->bus)
169		err = iommu_probe_device(dev);
170
171	/* Ignore all other errors apart from EPROBE_DEFER */
172	if (err == -EPROBE_DEFER) {
173		ops = ERR_PTR(err);
174	} else if (err < 0) {
175		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
176		ops = NULL;
177	}
178
179	return ops;
180}
181
182static enum iommu_resv_type __maybe_unused
183iommu_resv_region_get_type(struct device *dev,
184			   struct resource *phys,
185			   phys_addr_t start, size_t length)
186{
187	phys_addr_t end = start + length - 1;
188
189	/*
190	 * IOMMU regions without an associated physical region cannot be
191	 * mapped and are simply reservations.
192	 */
193	if (phys->start >= phys->end)
194		return IOMMU_RESV_RESERVED;
195
196	/* may be IOMMU_RESV_DIRECT_RELAXABLE for certain cases */
197	if (start == phys->start && end == phys->end)
198		return IOMMU_RESV_DIRECT;
199
200	dev_warn(dev, "treating non-direct mapping [%pr] -> [%pap-%pap] as reservation\n", phys,
201		 &start, &end);
202	return IOMMU_RESV_RESERVED;
203}
204
205/**
206 * of_iommu_get_resv_regions - reserved region driver helper for device tree
207 * @dev: device for which to get reserved regions
208 * @list: reserved region list
209 *
210 * IOMMU drivers can use this to implement their .get_resv_regions() callback
211 * for memory regions attached to a device tree node. See the reserved-memory
212 * device tree bindings on how to use these:
213 *
214 *   Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
215 */
216void of_iommu_get_resv_regions(struct device *dev, struct list_head *list)
217{
218#if IS_ENABLED(CONFIG_OF_ADDRESS)
219	struct of_phandle_iterator it;
220	int err;
221
222	of_for_each_phandle(&it, err, dev->of_node, "memory-region", NULL, 0) {
223		const __be32 *maps, *end;
224		struct resource phys;
225		int size;
226
227		memset(&phys, 0, sizeof(phys));
228
229		/*
230		 * The "reg" property is optional and can be omitted by reserved-memory regions
231		 * that represent reservations in the IOVA space, which are regions that should
232		 * not be mapped.
233		 */
234		if (of_find_property(it.node, "reg", NULL)) {
235			err = of_address_to_resource(it.node, 0, &phys);
236			if (err < 0) {
237				dev_err(dev, "failed to parse memory region %pOF: %d\n",
238					it.node, err);
239				continue;
240			}
241		}
242
243		maps = of_get_property(it.node, "iommu-addresses", &size);
244		if (!maps)
245			continue;
246
247		end = maps + size / sizeof(__be32);
248
249		while (maps < end) {
250			struct device_node *np;
251			u32 phandle;
252
253			phandle = be32_to_cpup(maps++);
254			np = of_find_node_by_phandle(phandle);
255
256			if (np == dev->of_node) {
257				int prot = IOMMU_READ | IOMMU_WRITE;
258				struct iommu_resv_region *region;
259				enum iommu_resv_type type;
260				phys_addr_t iova;
261				size_t length;
262
263				if (of_dma_is_coherent(dev->of_node))
264					prot |= IOMMU_CACHE;
265
266				maps = of_translate_dma_region(np, maps, &iova, &length);
267				if (length == 0) {
268					dev_warn(dev, "Cannot reserve IOVA region of 0 size\n");
269					continue;
270				}
271				type = iommu_resv_region_get_type(dev, &phys, iova, length);
272
273				region = iommu_alloc_resv_region(iova, length, prot, type,
274								 GFP_KERNEL);
275				if (region)
276					list_add_tail(&region->list, list);
277			}
278		}
279	}
280#endif
281}
282EXPORT_SYMBOL(of_iommu_get_resv_regions);
283