xref: /linux/drivers/iommu/of_iommu.c (revision fbf46565c67c626849c7ce2a326972d3008d2a91)
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/pci.h>
16 #include <linux/slab.h>
17 #include <linux/fsl/mc.h>
18 
19 #define NO_IOMMU	1
20 
21 static int of_iommu_xlate(struct device *dev,
22 			  struct of_phandle_args *iommu_spec)
23 {
24 	const struct iommu_ops *ops;
25 	struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
26 	int ret;
27 
28 	ops = iommu_ops_from_fwnode(fwnode);
29 	if ((ops && !ops->of_xlate) ||
30 	    !of_device_is_available(iommu_spec->np))
31 		return NO_IOMMU;
32 
33 	ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
34 	if (ret)
35 		return ret;
36 	/*
37 	 * The otherwise-empty fwspec handily serves to indicate the specific
38 	 * IOMMU device we're waiting for, which will be useful if we ever get
39 	 * a proper probe-ordering dependency mechanism in future.
40 	 */
41 	if (!ops)
42 		return driver_deferred_probe_check_state(dev);
43 
44 	if (!try_module_get(ops->owner))
45 		return -ENODEV;
46 
47 	ret = ops->of_xlate(dev, iommu_spec);
48 	module_put(ops->owner);
49 	return ret;
50 }
51 
52 static int of_iommu_configure_dev_id(struct device_node *master_np,
53 				     struct device *dev,
54 				     const u32 *id)
55 {
56 	struct of_phandle_args iommu_spec = { .args_count = 1 };
57 	int err;
58 
59 	err = of_map_id(master_np, *id, "iommu-map",
60 			 "iommu-map-mask", &iommu_spec.np,
61 			 iommu_spec.args);
62 	if (err)
63 		return err == -ENODEV ? NO_IOMMU : err;
64 
65 	err = of_iommu_xlate(dev, &iommu_spec);
66 	of_node_put(iommu_spec.np);
67 	return err;
68 }
69 
70 static int of_iommu_configure_dev(struct device_node *master_np,
71 				  struct device *dev)
72 {
73 	struct of_phandle_args iommu_spec;
74 	int err = NO_IOMMU, idx = 0;
75 
76 	while (!of_parse_phandle_with_args(master_np, "iommus",
77 					   "#iommu-cells",
78 					   idx, &iommu_spec)) {
79 		err = of_iommu_xlate(dev, &iommu_spec);
80 		of_node_put(iommu_spec.np);
81 		idx++;
82 		if (err)
83 			break;
84 	}
85 
86 	return err;
87 }
88 
89 struct of_pci_iommu_alias_info {
90 	struct device *dev;
91 	struct device_node *np;
92 };
93 
94 static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
95 {
96 	struct of_pci_iommu_alias_info *info = data;
97 	u32 input_id = alias;
98 
99 	return of_iommu_configure_dev_id(info->np, info->dev, &input_id);
100 }
101 
102 static int of_iommu_configure_device(struct device_node *master_np,
103 				     struct device *dev, const u32 *id)
104 {
105 	return (id) ? of_iommu_configure_dev_id(master_np, dev, id) :
106 		      of_iommu_configure_dev(master_np, dev);
107 }
108 
109 const struct iommu_ops *of_iommu_configure(struct device *dev,
110 					   struct device_node *master_np,
111 					   const u32 *id)
112 {
113 	const struct iommu_ops *ops = NULL;
114 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
115 	int err = NO_IOMMU;
116 
117 	if (!master_np)
118 		return NULL;
119 
120 	if (fwspec) {
121 		if (fwspec->ops)
122 			return fwspec->ops;
123 
124 		/* In the deferred case, start again from scratch */
125 		iommu_fwspec_free(dev);
126 	}
127 
128 	/*
129 	 * We don't currently walk up the tree looking for a parent IOMMU.
130 	 * See the `Notes:' section of
131 	 * Documentation/devicetree/bindings/iommu/iommu.txt
132 	 */
133 	if (dev_is_pci(dev)) {
134 		struct of_pci_iommu_alias_info info = {
135 			.dev = dev,
136 			.np = master_np,
137 		};
138 
139 		pci_request_acs();
140 		err = pci_for_each_dma_alias(to_pci_dev(dev),
141 					     of_pci_iommu_init, &info);
142 	} else {
143 		err = of_iommu_configure_device(master_np, dev, id);
144 	}
145 
146 	/*
147 	 * Two success conditions can be represented by non-negative err here:
148 	 * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
149 	 *  0 : we found an IOMMU, and dev->fwspec is initialised appropriately
150 	 * <0 : any actual error
151 	 */
152 	if (!err) {
153 		/* The fwspec pointer changed, read it again */
154 		fwspec = dev_iommu_fwspec_get(dev);
155 		ops    = fwspec->ops;
156 	}
157 	/*
158 	 * If we have reason to believe the IOMMU driver missed the initial
159 	 * probe for dev, replay it to get things in order.
160 	 */
161 	if (!err && dev->bus && !device_iommu_mapped(dev))
162 		err = iommu_probe_device(dev);
163 
164 	/* Ignore all other errors apart from EPROBE_DEFER */
165 	if (err == -EPROBE_DEFER) {
166 		ops = ERR_PTR(err);
167 	} else if (err < 0) {
168 		dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
169 		ops = NULL;
170 	}
171 
172 	return ops;
173 }
174