xref: /openbmc/linux/drivers/iommu/of_iommu.c (revision fa0656b40e0fb0135d7d229c8ab0261fe741af7b)
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>
27*fa0656b4SNipun Gupta #include <linux/fsl/mc.h>
284e0ee78fSHiroshi Doyu 
29da4b0275SRobin Murphy #define NO_IOMMU	1
30da4b0275SRobin Murphy 
314e0ee78fSHiroshi Doyu /**
324e0ee78fSHiroshi Doyu  * of_get_dma_window - Parse *dma-window property and returns 0 if found.
334e0ee78fSHiroshi Doyu  *
344e0ee78fSHiroshi Doyu  * @dn: device node
354e0ee78fSHiroshi Doyu  * @prefix: prefix for property name if any
364e0ee78fSHiroshi Doyu  * @index: index to start to parse
374e0ee78fSHiroshi Doyu  * @busno: Returns busno if supported. Otherwise pass NULL
384e0ee78fSHiroshi Doyu  * @addr: Returns address that DMA starts
394e0ee78fSHiroshi Doyu  * @size: Returns the range that DMA can handle
404e0ee78fSHiroshi Doyu  *
414e0ee78fSHiroshi Doyu  * This supports different formats flexibly. "prefix" can be
424e0ee78fSHiroshi Doyu  * configured if any. "busno" and "index" are optionally
434e0ee78fSHiroshi Doyu  * specified. Set 0(or NULL) if not used.
444e0ee78fSHiroshi Doyu  */
454e0ee78fSHiroshi Doyu int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
464e0ee78fSHiroshi Doyu 		      unsigned long *busno, dma_addr_t *addr, size_t *size)
474e0ee78fSHiroshi Doyu {
484e0ee78fSHiroshi Doyu 	const __be32 *dma_window, *end;
494e0ee78fSHiroshi Doyu 	int bytes, cur_index = 0;
504e0ee78fSHiroshi Doyu 	char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
514e0ee78fSHiroshi Doyu 
524e0ee78fSHiroshi Doyu 	if (!dn || !addr || !size)
534e0ee78fSHiroshi Doyu 		return -EINVAL;
544e0ee78fSHiroshi Doyu 
554e0ee78fSHiroshi Doyu 	if (!prefix)
564e0ee78fSHiroshi Doyu 		prefix = "";
574e0ee78fSHiroshi Doyu 
584e0ee78fSHiroshi Doyu 	snprintf(propname, sizeof(propname), "%sdma-window", prefix);
594e0ee78fSHiroshi Doyu 	snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
604e0ee78fSHiroshi Doyu 	snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
614e0ee78fSHiroshi Doyu 
624e0ee78fSHiroshi Doyu 	dma_window = of_get_property(dn, propname, &bytes);
634e0ee78fSHiroshi Doyu 	if (!dma_window)
644e0ee78fSHiroshi Doyu 		return -ENODEV;
654e0ee78fSHiroshi Doyu 	end = dma_window + bytes / sizeof(*dma_window);
664e0ee78fSHiroshi Doyu 
674e0ee78fSHiroshi Doyu 	while (dma_window < end) {
684e0ee78fSHiroshi Doyu 		u32 cells;
694e0ee78fSHiroshi Doyu 		const void *prop;
704e0ee78fSHiroshi Doyu 
714e0ee78fSHiroshi Doyu 		/* busno is one cell if supported */
724e0ee78fSHiroshi Doyu 		if (busno)
734e0ee78fSHiroshi Doyu 			*busno = be32_to_cpup(dma_window++);
744e0ee78fSHiroshi Doyu 
754e0ee78fSHiroshi Doyu 		prop = of_get_property(dn, addrname, NULL);
764e0ee78fSHiroshi Doyu 		if (!prop)
774e0ee78fSHiroshi Doyu 			prop = of_get_property(dn, "#address-cells", NULL);
784e0ee78fSHiroshi Doyu 
794e0ee78fSHiroshi Doyu 		cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
804e0ee78fSHiroshi Doyu 		if (!cells)
814e0ee78fSHiroshi Doyu 			return -EINVAL;
824e0ee78fSHiroshi Doyu 		*addr = of_read_number(dma_window, cells);
834e0ee78fSHiroshi Doyu 		dma_window += cells;
844e0ee78fSHiroshi Doyu 
854e0ee78fSHiroshi Doyu 		prop = of_get_property(dn, sizename, NULL);
864e0ee78fSHiroshi Doyu 		cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
874e0ee78fSHiroshi Doyu 		if (!cells)
884e0ee78fSHiroshi Doyu 			return -EINVAL;
894e0ee78fSHiroshi Doyu 		*size = of_read_number(dma_window, cells);
904e0ee78fSHiroshi Doyu 		dma_window += cells;
914e0ee78fSHiroshi Doyu 
924e0ee78fSHiroshi Doyu 		if (cur_index++ == index)
934e0ee78fSHiroshi Doyu 			break;
944e0ee78fSHiroshi Doyu 	}
954e0ee78fSHiroshi Doyu 	return 0;
964e0ee78fSHiroshi Doyu }
974e0ee78fSHiroshi Doyu EXPORT_SYMBOL_GPL(of_get_dma_window);
981cd076bfSWill Deacon 
99da4b0275SRobin Murphy static int of_iommu_xlate(struct device *dev,
100da4b0275SRobin Murphy 			  struct of_phandle_args *iommu_spec)
1012a0c5754SRobin Murphy {
1022a0c5754SRobin Murphy 	const struct iommu_ops *ops;
1032a0c5754SRobin Murphy 	struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
1042a0c5754SRobin Murphy 	int err;
1052a0c5754SRobin Murphy 
1062a0c5754SRobin Murphy 	ops = iommu_ops_from_fwnode(fwnode);
107d7b05582SRobin Murphy 	if ((ops && !ops->of_xlate) ||
108ac6bbf0cSRob Herring 	    !of_device_is_available(iommu_spec->np))
109da4b0275SRobin Murphy 		return NO_IOMMU;
1102a0c5754SRobin Murphy 
1112a0c5754SRobin Murphy 	err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
1122a0c5754SRobin Murphy 	if (err)
113da4b0275SRobin Murphy 		return err;
114d7b05582SRobin Murphy 	/*
115d7b05582SRobin Murphy 	 * The otherwise-empty fwspec handily serves to indicate the specific
116d7b05582SRobin Murphy 	 * IOMMU device we're waiting for, which will be useful if we ever get
117d7b05582SRobin Murphy 	 * a proper probe-ordering dependency mechanism in future.
118d7b05582SRobin Murphy 	 */
119d7b05582SRobin Murphy 	if (!ops)
12078f307beSRob Herring 		return driver_deferred_probe_check_state(dev);
1212a0c5754SRobin Murphy 
122da4b0275SRobin Murphy 	return ops->of_xlate(dev, iommu_spec);
1232a0c5754SRobin Murphy }
1242a0c5754SRobin Murphy 
125d87beb74SRobin Murphy struct of_pci_iommu_alias_info {
126d87beb74SRobin Murphy 	struct device *dev;
127d87beb74SRobin Murphy 	struct device_node *np;
128d87beb74SRobin Murphy };
129b996444cSRobin Murphy 
130d87beb74SRobin Murphy static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
131b996444cSRobin Murphy {
132d87beb74SRobin Murphy 	struct of_pci_iommu_alias_info *info = data;
133d87beb74SRobin Murphy 	struct of_phandle_args iommu_spec = { .args_count = 1 };
1342a0c5754SRobin Murphy 	int err;
135b996444cSRobin Murphy 
1362a6db719SNipun Gupta 	err = of_map_rid(info->np, alias, "iommu-map", "iommu-map-mask",
1372a6db719SNipun Gupta 			 &iommu_spec.np, 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 
146*fa0656b4SNipun Gupta static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev,
147*fa0656b4SNipun Gupta 				struct device_node *master_np)
148*fa0656b4SNipun Gupta {
149*fa0656b4SNipun Gupta 	struct of_phandle_args iommu_spec = { .args_count = 1 };
150*fa0656b4SNipun Gupta 	int err;
151*fa0656b4SNipun Gupta 
152*fa0656b4SNipun Gupta 	err = of_map_rid(master_np, mc_dev->icid, "iommu-map",
153*fa0656b4SNipun Gupta 			 "iommu-map-mask", &iommu_spec.np,
154*fa0656b4SNipun Gupta 			 iommu_spec.args);
155*fa0656b4SNipun Gupta 	if (err)
156*fa0656b4SNipun Gupta 		return err == -ENODEV ? NO_IOMMU : err;
157*fa0656b4SNipun Gupta 
158*fa0656b4SNipun Gupta 	err = of_iommu_xlate(&mc_dev->dev, &iommu_spec);
159*fa0656b4SNipun Gupta 	of_node_put(iommu_spec.np);
160*fa0656b4SNipun Gupta 	return err;
161*fa0656b4SNipun Gupta }
162*fa0656b4SNipun Gupta 
1632a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev,
1642a0c5754SRobin Murphy 					   struct device_node *master_np)
1652a0c5754SRobin Murphy {
166d87beb74SRobin Murphy 	const struct iommu_ops *ops = NULL;
167d7b05582SRobin Murphy 	struct iommu_fwspec *fwspec = dev->iommu_fwspec;
168da4b0275SRobin Murphy 	int err = NO_IOMMU;
1692a0c5754SRobin Murphy 
1702a0c5754SRobin Murphy 	if (!master_np)
1717eba1d51SWill Deacon 		return NULL;
1722a0c5754SRobin Murphy 
173d7b05582SRobin Murphy 	if (fwspec) {
174d7b05582SRobin Murphy 		if (fwspec->ops)
175d7b05582SRobin Murphy 			return fwspec->ops;
176d7b05582SRobin Murphy 
177d7b05582SRobin Murphy 		/* In the deferred case, start again from scratch */
178d7b05582SRobin Murphy 		iommu_fwspec_free(dev);
179d7b05582SRobin Murphy 	}
180d7b05582SRobin Murphy 
181d87beb74SRobin Murphy 	/*
182d87beb74SRobin Murphy 	 * We don't currently walk up the tree looking for a parent IOMMU.
183d87beb74SRobin Murphy 	 * See the `Notes:' section of
184d87beb74SRobin Murphy 	 * Documentation/devicetree/bindings/iommu/iommu.txt
185d87beb74SRobin Murphy 	 */
186d87beb74SRobin Murphy 	if (dev_is_pci(dev)) {
187d87beb74SRobin Murphy 		struct of_pci_iommu_alias_info info = {
188d87beb74SRobin Murphy 			.dev = dev,
189d87beb74SRobin Murphy 			.np = master_np,
190d87beb74SRobin Murphy 		};
191d87beb74SRobin Murphy 
192d87beb74SRobin Murphy 		err = pci_for_each_dma_alias(to_pci_dev(dev),
193d87beb74SRobin Murphy 					     of_pci_iommu_init, &info);
194*fa0656b4SNipun Gupta 	} else if (dev_is_fsl_mc(dev)) {
195*fa0656b4SNipun Gupta 		err = of_fsl_mc_iommu_init(to_fsl_mc_device(dev), master_np);
196d87beb74SRobin Murphy 	} else {
197d87beb74SRobin Murphy 		struct of_phandle_args iommu_spec;
198d87beb74SRobin Murphy 		int idx = 0;
199d87beb74SRobin Murphy 
200d87beb74SRobin Murphy 		while (!of_parse_phandle_with_args(master_np, "iommus",
201d87beb74SRobin Murphy 						   "#iommu-cells",
202d87beb74SRobin Murphy 						   idx, &iommu_spec)) {
203da4b0275SRobin Murphy 			err = of_iommu_xlate(dev, &iommu_spec);
204d87beb74SRobin Murphy 			of_node_put(iommu_spec.np);
205d87beb74SRobin Murphy 			idx++;
206da4b0275SRobin Murphy 			if (err)
207d87beb74SRobin Murphy 				break;
208d87beb74SRobin Murphy 		}
209d87beb74SRobin Murphy 	}
210da4b0275SRobin Murphy 
211da4b0275SRobin Murphy 	/*
212da4b0275SRobin Murphy 	 * Two success conditions can be represented by non-negative err here:
213da4b0275SRobin Murphy 	 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
214da4b0275SRobin Murphy 	 *  0 : we found an IOMMU, and dev->fwspec is initialised appropriately
215da4b0275SRobin Murphy 	 * <0 : any actual error
216da4b0275SRobin Murphy 	 */
217da4b0275SRobin Murphy 	if (!err)
218da4b0275SRobin Murphy 		ops = dev->iommu_fwspec->ops;
219d7b05582SRobin Murphy 	/*
220d7b05582SRobin Murphy 	 * If we have reason to believe the IOMMU driver missed the initial
221d7b05582SRobin Murphy 	 * add_device callback for dev, replay it to get things in order.
222d7b05582SRobin Murphy 	 */
223da4b0275SRobin Murphy 	if (ops && ops->add_device && dev->bus && !dev->iommu_group)
224d87beb74SRobin Murphy 		err = ops->add_device(dev);
2252a0c5754SRobin Murphy 
226a37b19a3SSricharan R 	/* Ignore all other errors apart from EPROBE_DEFER */
227da4b0275SRobin Murphy 	if (err == -EPROBE_DEFER) {
228da4b0275SRobin Murphy 		ops = ERR_PTR(err);
229da4b0275SRobin Murphy 	} else if (err < 0) {
230da4b0275SRobin Murphy 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
231a37b19a3SSricharan R 		ops = NULL;
232a37b19a3SSricharan R 	}
233a37b19a3SSricharan R 
2347b07cbefSLaurent Pinchart 	return ops;
2357eba1d51SWill Deacon }
236