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