• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_iommu.h>
14 #include <linux/of_pci.h>
15 #include <linux/slab.h>
16 #include <linux/fsl/mc.h>
17 
18 #define NO_IOMMU	1
19 
20 /**
21  * of_get_dma_window - Parse *dma-window property and returns 0 if found.
22  *
23  * @dn: device node
24  * @prefix: prefix for property name if any
25  * @index: index to start to parse
26  * @busno: Returns busno if supported. Otherwise pass NULL
27  * @addr: Returns address that DMA starts
28  * @size: Returns the range that DMA can handle
29  *
30  * This supports different formats flexibly. "prefix" can be
31  * configured if any. "busno" and "index" are optionally
32  * specified. Set 0(or NULL) if not used.
33  */
of_get_dma_window(struct device_node * dn,const char * prefix,int index,unsigned long * busno,dma_addr_t * addr,size_t * size)34 int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
35 		      unsigned long *busno, dma_addr_t *addr, size_t *size)
36 {
37 	const __be32 *dma_window, *end;
38 	int bytes, cur_index = 0;
39 	char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
40 
41 	if (!dn || !addr || !size)
42 		return -EINVAL;
43 
44 	if (!prefix)
45 		prefix = "";
46 
47 	snprintf(propname, sizeof(propname), "%sdma-window", prefix);
48 	snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
49 	snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
50 
51 	dma_window = of_get_property(dn, propname, &bytes);
52 	if (!dma_window)
53 		return -ENODEV;
54 	end = dma_window + bytes / sizeof(*dma_window);
55 
56 	while (dma_window < end) {
57 		u32 cells;
58 		const void *prop;
59 
60 		/* busno is one cell if supported */
61 		if (busno)
62 			*busno = be32_to_cpup(dma_window++);
63 
64 		prop = of_get_property(dn, addrname, NULL);
65 		if (!prop)
66 			prop = of_get_property(dn, "#address-cells", NULL);
67 
68 		cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
69 		if (!cells)
70 			return -EINVAL;
71 		*addr = of_read_number(dma_window, cells);
72 		dma_window += cells;
73 
74 		prop = of_get_property(dn, sizename, NULL);
75 		cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
76 		if (!cells)
77 			return -EINVAL;
78 		*size = of_read_number(dma_window, cells);
79 		dma_window += cells;
80 
81 		if (cur_index++ == index)
82 			break;
83 	}
84 	return 0;
85 }
86 EXPORT_SYMBOL_GPL(of_get_dma_window);
87 
of_iommu_xlate(struct device * dev,struct of_phandle_args * iommu_spec)88 static int of_iommu_xlate(struct device *dev,
89 			  struct of_phandle_args *iommu_spec)
90 {
91 	const struct iommu_ops *ops;
92 	struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
93 	int ret;
94 
95 	ops = iommu_ops_from_fwnode(fwnode);
96 	if ((ops && !ops->of_xlate) ||
97 	    !of_device_is_available(iommu_spec->np))
98 		return NO_IOMMU;
99 
100 	ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
101 	if (ret)
102 		return ret;
103 	/*
104 	 * The otherwise-empty fwspec handily serves to indicate the specific
105 	 * IOMMU device we're waiting for, which will be useful if we ever get
106 	 * a proper probe-ordering dependency mechanism in future.
107 	 */
108 	if (!ops)
109 		return driver_deferred_probe_check_state(dev);
110 
111 	if (!try_module_get(ops->owner))
112 		return -ENODEV;
113 
114 	ret = ops->of_xlate(dev, iommu_spec);
115 	module_put(ops->owner);
116 	return ret;
117 }
118 
119 struct of_pci_iommu_alias_info {
120 	struct device *dev;
121 	struct device_node *np;
122 };
123 
of_pci_iommu_init(struct pci_dev * pdev,u16 alias,void * data)124 static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
125 {
126 	struct of_pci_iommu_alias_info *info = data;
127 	struct of_phandle_args iommu_spec = { .args_count = 1 };
128 	int err;
129 
130 	err = of_map_rid(info->np, alias, "iommu-map", "iommu-map-mask",
131 			 &iommu_spec.np, iommu_spec.args);
132 	if (err)
133 		return err == -ENODEV ? NO_IOMMU : err;
134 
135 	err = of_iommu_xlate(info->dev, &iommu_spec);
136 	of_node_put(iommu_spec.np);
137 	return err;
138 }
139 
of_fsl_mc_iommu_init(struct fsl_mc_device * mc_dev,struct device_node * master_np)140 static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev,
141 				struct device_node *master_np)
142 {
143 	struct of_phandle_args iommu_spec = { .args_count = 1 };
144 	int err;
145 
146 	err = of_map_rid(master_np, mc_dev->icid, "iommu-map",
147 			 "iommu-map-mask", &iommu_spec.np,
148 			 iommu_spec.args);
149 	if (err)
150 		return err == -ENODEV ? NO_IOMMU : err;
151 
152 	err = of_iommu_xlate(&mc_dev->dev, &iommu_spec);
153 	of_node_put(iommu_spec.np);
154 	return err;
155 }
156 
of_iommu_configure(struct device * dev,struct device_node * master_np)157 const struct iommu_ops *of_iommu_configure(struct device *dev,
158 					   struct device_node *master_np)
159 {
160 	const struct iommu_ops *ops = NULL;
161 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
162 	int err = NO_IOMMU;
163 
164 	if (!master_np)
165 		return NULL;
166 
167 	if (fwspec) {
168 		if (fwspec->ops)
169 			return fwspec->ops;
170 
171 		/* In the deferred case, start again from scratch */
172 		iommu_fwspec_free(dev);
173 	}
174 
175 	/*
176 	 * We don't currently walk up the tree looking for a parent IOMMU.
177 	 * See the `Notes:' section of
178 	 * Documentation/devicetree/bindings/iommu/iommu.txt
179 	 */
180 	if (dev_is_pci(dev)) {
181 		struct of_pci_iommu_alias_info info = {
182 			.dev = dev,
183 			.np = master_np,
184 		};
185 
186 		pci_request_acs();
187 		err = pci_for_each_dma_alias(to_pci_dev(dev),
188 					     of_pci_iommu_init, &info);
189 	} else if (dev_is_fsl_mc(dev)) {
190 		err = of_fsl_mc_iommu_init(to_fsl_mc_device(dev), master_np);
191 	} else {
192 		struct of_phandle_args iommu_spec;
193 		int idx = 0;
194 
195 		while (!of_parse_phandle_with_args(master_np, "iommus",
196 						   "#iommu-cells",
197 						   idx, &iommu_spec)) {
198 			err = of_iommu_xlate(dev, &iommu_spec);
199 			of_node_put(iommu_spec.np);
200 			idx++;
201 			if (err)
202 				break;
203 		}
204 	}
205 
206 
207 	/*
208 	 * Two success conditions can be represented by non-negative err here:
209 	 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
210 	 *  0 : we found an IOMMU, and dev->fwspec is initialised appropriately
211 	 * <0 : any actual error
212 	 */
213 	if (!err) {
214 		/* The fwspec pointer changed, read it again */
215 		fwspec = dev_iommu_fwspec_get(dev);
216 		ops    = fwspec->ops;
217 	}
218 	/*
219 	 * If we have reason to believe the IOMMU driver missed the initial
220 	 * probe for dev, replay it to get things in order.
221 	 */
222 	if (!err && dev->bus && !device_iommu_mapped(dev))
223 		err = iommu_probe_device(dev);
224 
225 	/* Ignore all other errors apart from EPROBE_DEFER */
226 	if (err == -EPROBE_DEFER) {
227 		ops = ERR_PTR(err);
228 	} else if (err < 0) {
229 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
230 		ops = NULL;
231 	}
232 
233 	return ops;
234 }
235