xref: /openbmc/linux/drivers/iommu/of_iommu.c (revision ac6bbf0cdf4206c517ac9789814c23e372ebce4d)
14e0ee78fSHiroshi Doyu /*
24e0ee78fSHiroshi Doyu  * OF helpers for IOMMU
34e0ee78fSHiroshi Doyu  *
44e0ee78fSHiroshi Doyu  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
54e0ee78fSHiroshi Doyu  *
64e0ee78fSHiroshi Doyu  * This program is free software; you can redistribute it and/or modify it
74e0ee78fSHiroshi Doyu  * under the terms and conditions of the GNU General Public License,
84e0ee78fSHiroshi Doyu  * version 2, as published by the Free Software Foundation.
94e0ee78fSHiroshi Doyu  *
104e0ee78fSHiroshi Doyu  * This program is distributed in the hope it will be useful, but WITHOUT
114e0ee78fSHiroshi Doyu  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
124e0ee78fSHiroshi Doyu  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
134e0ee78fSHiroshi Doyu  * more details.
144e0ee78fSHiroshi Doyu  *
154e0ee78fSHiroshi Doyu  * You should have received a copy of the GNU General Public License along with
164e0ee78fSHiroshi Doyu  * this program; if not, write to the Free Software Foundation, Inc.,
174e0ee78fSHiroshi Doyu  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
184e0ee78fSHiroshi Doyu  */
194e0ee78fSHiroshi Doyu 
204e0ee78fSHiroshi Doyu #include <linux/export.h>
217eba1d51SWill Deacon #include <linux/iommu.h>
224e0ee78fSHiroshi Doyu #include <linux/limits.h>
234e0ee78fSHiroshi Doyu #include <linux/of.h>
24cbff5634SBrian Norris #include <linux/of_iommu.h>
25b996444cSRobin Murphy #include <linux/of_pci.h>
26a42a7a1fSRobin Murphy #include <linux/slab.h>
274e0ee78fSHiroshi Doyu 
28da4b0275SRobin Murphy #define NO_IOMMU	1
29da4b0275SRobin Murphy 
304e0ee78fSHiroshi Doyu /**
314e0ee78fSHiroshi Doyu  * of_get_dma_window - Parse *dma-window property and returns 0 if found.
324e0ee78fSHiroshi Doyu  *
334e0ee78fSHiroshi Doyu  * @dn: device node
344e0ee78fSHiroshi Doyu  * @prefix: prefix for property name if any
354e0ee78fSHiroshi Doyu  * @index: index to start to parse
364e0ee78fSHiroshi Doyu  * @busno: Returns busno if supported. Otherwise pass NULL
374e0ee78fSHiroshi Doyu  * @addr: Returns address that DMA starts
384e0ee78fSHiroshi Doyu  * @size: Returns the range that DMA can handle
394e0ee78fSHiroshi Doyu  *
404e0ee78fSHiroshi Doyu  * This supports different formats flexibly. "prefix" can be
414e0ee78fSHiroshi Doyu  * configured if any. "busno" and "index" are optionally
424e0ee78fSHiroshi Doyu  * specified. Set 0(or NULL) if not used.
434e0ee78fSHiroshi Doyu  */
444e0ee78fSHiroshi Doyu int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
454e0ee78fSHiroshi Doyu 		      unsigned long *busno, dma_addr_t *addr, size_t *size)
464e0ee78fSHiroshi Doyu {
474e0ee78fSHiroshi Doyu 	const __be32 *dma_window, *end;
484e0ee78fSHiroshi Doyu 	int bytes, cur_index = 0;
494e0ee78fSHiroshi Doyu 	char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
504e0ee78fSHiroshi Doyu 
514e0ee78fSHiroshi Doyu 	if (!dn || !addr || !size)
524e0ee78fSHiroshi Doyu 		return -EINVAL;
534e0ee78fSHiroshi Doyu 
544e0ee78fSHiroshi Doyu 	if (!prefix)
554e0ee78fSHiroshi Doyu 		prefix = "";
564e0ee78fSHiroshi Doyu 
574e0ee78fSHiroshi Doyu 	snprintf(propname, sizeof(propname), "%sdma-window", prefix);
584e0ee78fSHiroshi Doyu 	snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
594e0ee78fSHiroshi Doyu 	snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
604e0ee78fSHiroshi Doyu 
614e0ee78fSHiroshi Doyu 	dma_window = of_get_property(dn, propname, &bytes);
624e0ee78fSHiroshi Doyu 	if (!dma_window)
634e0ee78fSHiroshi Doyu 		return -ENODEV;
644e0ee78fSHiroshi Doyu 	end = dma_window + bytes / sizeof(*dma_window);
654e0ee78fSHiroshi Doyu 
664e0ee78fSHiroshi Doyu 	while (dma_window < end) {
674e0ee78fSHiroshi Doyu 		u32 cells;
684e0ee78fSHiroshi Doyu 		const void *prop;
694e0ee78fSHiroshi Doyu 
704e0ee78fSHiroshi Doyu 		/* busno is one cell if supported */
714e0ee78fSHiroshi Doyu 		if (busno)
724e0ee78fSHiroshi Doyu 			*busno = be32_to_cpup(dma_window++);
734e0ee78fSHiroshi Doyu 
744e0ee78fSHiroshi Doyu 		prop = of_get_property(dn, addrname, NULL);
754e0ee78fSHiroshi Doyu 		if (!prop)
764e0ee78fSHiroshi Doyu 			prop = of_get_property(dn, "#address-cells", NULL);
774e0ee78fSHiroshi Doyu 
784e0ee78fSHiroshi Doyu 		cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
794e0ee78fSHiroshi Doyu 		if (!cells)
804e0ee78fSHiroshi Doyu 			return -EINVAL;
814e0ee78fSHiroshi Doyu 		*addr = of_read_number(dma_window, cells);
824e0ee78fSHiroshi Doyu 		dma_window += cells;
834e0ee78fSHiroshi Doyu 
844e0ee78fSHiroshi Doyu 		prop = of_get_property(dn, sizename, NULL);
854e0ee78fSHiroshi Doyu 		cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
864e0ee78fSHiroshi Doyu 		if (!cells)
874e0ee78fSHiroshi Doyu 			return -EINVAL;
884e0ee78fSHiroshi Doyu 		*size = of_read_number(dma_window, cells);
894e0ee78fSHiroshi Doyu 		dma_window += cells;
904e0ee78fSHiroshi Doyu 
914e0ee78fSHiroshi Doyu 		if (cur_index++ == index)
924e0ee78fSHiroshi Doyu 			break;
934e0ee78fSHiroshi Doyu 	}
944e0ee78fSHiroshi Doyu 	return 0;
954e0ee78fSHiroshi Doyu }
964e0ee78fSHiroshi Doyu EXPORT_SYMBOL_GPL(of_get_dma_window);
971cd076bfSWill Deacon 
98da4b0275SRobin Murphy static int of_iommu_xlate(struct device *dev,
99da4b0275SRobin Murphy 			  struct of_phandle_args *iommu_spec)
1002a0c5754SRobin Murphy {
1012a0c5754SRobin Murphy 	const struct iommu_ops *ops;
1022a0c5754SRobin Murphy 	struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
1032a0c5754SRobin Murphy 	int err;
1042a0c5754SRobin Murphy 
1052a0c5754SRobin Murphy 	ops = iommu_ops_from_fwnode(fwnode);
106d7b05582SRobin Murphy 	if ((ops && !ops->of_xlate) ||
107*ac6bbf0cSRob Herring 	    !of_device_is_available(iommu_spec->np))
108da4b0275SRobin Murphy 		return NO_IOMMU;
1092a0c5754SRobin Murphy 
1102a0c5754SRobin Murphy 	err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
1112a0c5754SRobin Murphy 	if (err)
112da4b0275SRobin Murphy 		return err;
113d7b05582SRobin Murphy 	/*
114d7b05582SRobin Murphy 	 * The otherwise-empty fwspec handily serves to indicate the specific
115d7b05582SRobin Murphy 	 * IOMMU device we're waiting for, which will be useful if we ever get
116d7b05582SRobin Murphy 	 * a proper probe-ordering dependency mechanism in future.
117d7b05582SRobin Murphy 	 */
118d7b05582SRobin Murphy 	if (!ops)
11978f307beSRob Herring 		return driver_deferred_probe_check_state(dev);
1202a0c5754SRobin Murphy 
121da4b0275SRobin Murphy 	return ops->of_xlate(dev, iommu_spec);
1222a0c5754SRobin Murphy }
1232a0c5754SRobin Murphy 
124d87beb74SRobin Murphy struct of_pci_iommu_alias_info {
125d87beb74SRobin Murphy 	struct device *dev;
126d87beb74SRobin Murphy 	struct device_node *np;
127d87beb74SRobin Murphy };
128b996444cSRobin Murphy 
129d87beb74SRobin Murphy static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
130b996444cSRobin Murphy {
131d87beb74SRobin Murphy 	struct of_pci_iommu_alias_info *info = data;
132d87beb74SRobin Murphy 	struct of_phandle_args iommu_spec = { .args_count = 1 };
1332a0c5754SRobin Murphy 	int err;
134b996444cSRobin Murphy 
135d87beb74SRobin Murphy 	err = of_pci_map_rid(info->np, alias, "iommu-map",
1362a0c5754SRobin Murphy 			     "iommu-map-mask", &iommu_spec.np,
1372a0c5754SRobin Murphy 			     iommu_spec.args);
1382a0c5754SRobin Murphy 	if (err)
139da4b0275SRobin Murphy 		return err == -ENODEV ? NO_IOMMU : err;
140b996444cSRobin Murphy 
141da4b0275SRobin Murphy 	err = of_iommu_xlate(info->dev, &iommu_spec);
142b996444cSRobin Murphy 	of_node_put(iommu_spec.np);
143da4b0275SRobin Murphy 	return err;
1442a0c5754SRobin Murphy }
1457eba1d51SWill Deacon 
1462a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev,
1472a0c5754SRobin Murphy 					   struct device_node *master_np)
1482a0c5754SRobin Murphy {
149d87beb74SRobin Murphy 	const struct iommu_ops *ops = NULL;
150d7b05582SRobin Murphy 	struct iommu_fwspec *fwspec = dev->iommu_fwspec;
151da4b0275SRobin Murphy 	int err = NO_IOMMU;
1522a0c5754SRobin Murphy 
1532a0c5754SRobin Murphy 	if (!master_np)
1547eba1d51SWill Deacon 		return NULL;
1552a0c5754SRobin Murphy 
156d7b05582SRobin Murphy 	if (fwspec) {
157d7b05582SRobin Murphy 		if (fwspec->ops)
158d7b05582SRobin Murphy 			return fwspec->ops;
159d7b05582SRobin Murphy 
160d7b05582SRobin Murphy 		/* In the deferred case, start again from scratch */
161d7b05582SRobin Murphy 		iommu_fwspec_free(dev);
162d7b05582SRobin Murphy 	}
163d7b05582SRobin Murphy 
164d87beb74SRobin Murphy 	/*
165d87beb74SRobin Murphy 	 * We don't currently walk up the tree looking for a parent IOMMU.
166d87beb74SRobin Murphy 	 * See the `Notes:' section of
167d87beb74SRobin Murphy 	 * Documentation/devicetree/bindings/iommu/iommu.txt
168d87beb74SRobin Murphy 	 */
169d87beb74SRobin Murphy 	if (dev_is_pci(dev)) {
170d87beb74SRobin Murphy 		struct of_pci_iommu_alias_info info = {
171d87beb74SRobin Murphy 			.dev = dev,
172d87beb74SRobin Murphy 			.np = master_np,
173d87beb74SRobin Murphy 		};
174d87beb74SRobin Murphy 
175d87beb74SRobin Murphy 		err = pci_for_each_dma_alias(to_pci_dev(dev),
176d87beb74SRobin Murphy 					     of_pci_iommu_init, &info);
177d87beb74SRobin Murphy 	} else {
178d87beb74SRobin Murphy 		struct of_phandle_args iommu_spec;
179d87beb74SRobin Murphy 		int idx = 0;
180d87beb74SRobin Murphy 
181d87beb74SRobin Murphy 		while (!of_parse_phandle_with_args(master_np, "iommus",
182d87beb74SRobin Murphy 						   "#iommu-cells",
183d87beb74SRobin Murphy 						   idx, &iommu_spec)) {
184da4b0275SRobin Murphy 			err = of_iommu_xlate(dev, &iommu_spec);
185d87beb74SRobin Murphy 			of_node_put(iommu_spec.np);
186d87beb74SRobin Murphy 			idx++;
187da4b0275SRobin Murphy 			if (err)
188d87beb74SRobin Murphy 				break;
189d87beb74SRobin Murphy 		}
190d87beb74SRobin Murphy 	}
191da4b0275SRobin Murphy 
192da4b0275SRobin Murphy 	/*
193da4b0275SRobin Murphy 	 * Two success conditions can be represented by non-negative err here:
194da4b0275SRobin Murphy 	 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
195da4b0275SRobin Murphy 	 *  0 : we found an IOMMU, and dev->fwspec is initialised appropriately
196da4b0275SRobin Murphy 	 * <0 : any actual error
197da4b0275SRobin Murphy 	 */
198da4b0275SRobin Murphy 	if (!err)
199da4b0275SRobin Murphy 		ops = dev->iommu_fwspec->ops;
200d7b05582SRobin Murphy 	/*
201d7b05582SRobin Murphy 	 * If we have reason to believe the IOMMU driver missed the initial
202d7b05582SRobin Murphy 	 * add_device callback for dev, replay it to get things in order.
203d7b05582SRobin Murphy 	 */
204da4b0275SRobin Murphy 	if (ops && ops->add_device && dev->bus && !dev->iommu_group)
205d87beb74SRobin Murphy 		err = ops->add_device(dev);
2062a0c5754SRobin Murphy 
207a37b19a3SSricharan R 	/* Ignore all other errors apart from EPROBE_DEFER */
208da4b0275SRobin Murphy 	if (err == -EPROBE_DEFER) {
209da4b0275SRobin Murphy 		ops = ERR_PTR(err);
210da4b0275SRobin Murphy 	} else if (err < 0) {
211da4b0275SRobin Murphy 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
212a37b19a3SSricharan R 		ops = NULL;
213a37b19a3SSricharan R 	}
214a37b19a3SSricharan R 
2157b07cbefSLaurent Pinchart 	return ops;
2167eba1d51SWill Deacon }
217