Debian Codegraph @

drivers/irqchip/irq-gic-its-msi-parent.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
3 // Author: Marc Zyngier <[email protected]>
4 // Copyright (C) 2022 Linutronix GmbH
5 // Copyright (C) 2022 Intel
6
7 #include <linux/acpi_iort.h>
8 #include <linux/of_address.h>
9 #include <linux/pci.h>
10
11 #include "irq-gic-its-msi-parent.h"
12 #include <linux/irqchip/irq-msi-lib.h>
13
14 #define ITS_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
15 MSI_FLAG_USE_DEF_CHIP_OPS | \
16 MSI_FLAG_PCI_MSI_MASK_PARENT)
17
18 #define ITS_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
19 MSI_FLAG_PCI_MSIX | \
20 MSI_FLAG_MULTI_PCI_MSI)
21
22 static int its_translate_frame_address(struct fwnode_handle *msi_node, phys_addr_t *pa)
23 {
24 struct resource res;
25 int ret;
26
27 if (is_of_node(msi_node)) {
28 struct device_node *msi_np = to_of_node(msi_node);
29
30 ret = of_property_match_string(msi_np, "reg-names", "ns-translate");
31 if (ret < 0)
32 return ret;
33
34 ret = of_address_to_resource(msi_np, ret, &res);
35 if (ret)
36 return ret;
37 } else {
38 ret = iort_its_translate_pa(msi_node, &res.start);
39 }
40
41 *pa = res.start;
42 return 0;
43 }
44
45 #ifdef CONFIG_PCI_MSI
46 static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
47 {
48 int msi, msix, *count = data;
49
50 msi = max(pci_msi_vec_count(pdev), 0);
51 msix = max(pci_msix_vec_count(pdev), 0);
52 *count += max(msi, msix);
53
54 return 0;
55 }
56
57 static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
58 {
59 struct pci_dev **alias_dev = data;
60
61 *alias_dev = pdev;
62
63 return 0;
64 }
65
66 static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
67 int nvec, msi_alloc_info_t *info)
68 {
69 struct pci_dev *pdev, *alias_dev;
70 struct msi_domain_info *msi_info;
71 int alias_count = 0, minnvec = 1;
72
73 if (!dev_is_pci(dev))
74 return -EINVAL;
75
76 pdev = to_pci_dev(dev);
77 /*
78 * If pdev is downstream of any aliasing bridges, take an upper
79 * bound of how many other vectors could map to the same DevID.
80 * Also tell the ITS that the signalling will come from a proxy
81 * device, and that special allocation rules apply.
82 */
83 pci_for_each_dma_alias(pdev, its_get_pci_alias, &alias_dev);
84 if (alias_dev != pdev) {
85 if (alias_dev->subordinate)
86 pci_walk_bus(alias_dev->subordinate,
87 its_pci_msi_vec_count, &alias_count);
88 info->flags |= MSI_ALLOC_FLAGS_PROXY_DEVICE;
89 }
90
91 /* ITS specific DeviceID, as the core ITS ignores dev. */
92 info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain->parent, pdev);
93
94 /*
95 * Always allocate a power of 2, and special case device 0 for
96 * broken systems where the DevID is not wired (and all devices
97 * appear as DevID 0). For that reason, we generously allocate a
98 * minimum of 32 MSIs for DevID 0. If you want more because all
99 * your devices are aliasing to DevID 0, consider fixing your HW.
100 */
101 nvec = max(nvec, alias_count);
102 if (!info->scratchpad[0].ul)
103 minnvec = 32;
104 nvec = max_t(int, minnvec, roundup_pow_of_two(nvec));
105
106 msi_info = msi_get_domain_info(domain->parent);
107 return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
108 }
109
110 static int its_v5_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
111 int nvec, msi_alloc_info_t *info)
112 {
113 struct fwnode_handle *msi_node = NULL;
114 struct msi_domain_info *msi_info;
115 struct pci_dev *pdev;
116 phys_addr_t pa;
117 u32 rid;
118 int ret;
119
120 if (!dev_is_pci(dev))
121 return -EINVAL;
122
123 pdev = to_pci_dev(dev);
124
125 rid = pci_msi_map_rid_ctlr_node(domain->parent, pdev, &msi_node);
126 if (!msi_node)
127 return -ENODEV;
128
129 ret = its_translate_frame_address(msi_node, &pa);
130 if (ret)
131 return -ENODEV;
132
133 fwnode_handle_put(msi_node);
134
135 /* ITS specific DeviceID */
136 info->scratchpad[0].ul = rid;
137 /* ITS translate frame physical address */
138 info->scratchpad[1].ul = pa;
139
140 /* Always allocate power of two vectors */
141 nvec = roundup_pow_of_two(nvec);
142
143 msi_info = msi_get_domain_info(domain->parent);
144 return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
145 }
146 #else /* CONFIG_PCI_MSI */
147 #define its_pci_msi_prepare NULL
148 #define its_v5_pci_msi_prepare NULL
149 #endif /* !CONFIG_PCI_MSI */
150
151 static int of_pmsi_get_msi_info(struct irq_domain *domain, struct device *dev, u32 *dev_id,
152 phys_addr_t *pa)
153 {
154 struct of_phandle_iterator it;
155 int ret;
156
157 /* Suck the DeviceID out of the msi-parent property */
158 of_for_each_phandle(&it, ret, dev->of_node, "msi-parent", "#msi-cells", -1) {
159 /* GICv5 ITS domain matches the MSI controller node parent */
160 struct device_node *np __free(device_node) = pa ? of_get_parent(it.node)
161 : of_node_get(it.node);
162
163 if (np == irq_domain_get_of_node(domain)) {
164 u32 args;
165
166 if (WARN_ON(of_phandle_iterator_args(&it, &args, 1) != 1))
167 ret = -EINVAL;
168
169 if (!ret && pa)
170 ret = its_translate_frame_address(of_fwnode_handle(it.node), pa);
171
172 if (!ret)
173 *dev_id = args;
174
175 of_node_put(it.node);
176 return ret;
177 }
178 }
179
180 struct device_node *msi_ctrl __free(device_node) = NULL;
181
182 return of_map_id(dev->of_node, dev->id, "msi-map", "msi-map-mask", &msi_ctrl, dev_id);
183 }
184
185 static int its_pmsi_prepare(struct irq_domain *domain, struct device *dev,
186 int nvec, msi_alloc_info_t *info)
187 {
188 struct msi_domain_info *msi_info;
189 u32 dev_id;
190 int ret;
191
192 if (dev->of_node)
193 ret = of_pmsi_get_msi_info(domain->parent, dev, &dev_id, NULL);
194 else
195 ret = iort_pmsi_get_msi_info(dev, &dev_id, NULL);
196 if (ret)
197 return ret;
198
199 /* ITS specific DeviceID, as the core ITS ignores dev. */
200 info->scratchpad[0].ul = dev_id;
201
202 /* Allocate at least 32 MSIs, and always as a power of 2 */
203 nvec = max_t(int, 32, roundup_pow_of_two(nvec));
204
205 msi_info = msi_get_domain_info(domain->parent);
206 return msi_info->ops->msi_prepare(domain->parent,
207 dev, nvec, info);
208 }
209
210 static int its_v5_pmsi_prepare(struct irq_domain *domain, struct device *dev,
211 int nvec, msi_alloc_info_t *info)
212 {
213 struct msi_domain_info *msi_info;
214 phys_addr_t pa;
215 u32 dev_id;
216 int ret;
217
218 if (dev->of_node)
219 ret = of_pmsi_get_msi_info(domain->parent, dev, &dev_id, &pa);
220 else
221 ret = iort_pmsi_get_msi_info(dev, &dev_id, &pa);
222 if (ret)
223 return ret;
224
225 /* ITS specific DeviceID */
226 info->scratchpad[0].ul = dev_id;
227 /* ITS translate frame physical address */
228 info->scratchpad[1].ul = pa;
229
230 /* Allocate always as a power of 2 */
231 nvec = roundup_pow_of_two(nvec);
232
233 msi_info = msi_get_domain_info(domain->parent);
234 return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
235 }
236
237 static void its_msi_teardown(struct irq_domain *domain, msi_alloc_info_t *info)
238 {
239 struct msi_domain_info *msi_info;
240
241 msi_info = msi_get_domain_info(domain->parent);
242 msi_info->ops->msi_teardown(domain->parent, info);
243 }
244
245 static bool its_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
246 struct irq_domain *real_parent, struct msi_domain_info *info)
247 {
248 if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
249 return false;
250
251 switch(info->bus_token) {
252 case DOMAIN_BUS_PCI_DEVICE_MSI:
253 case DOMAIN_BUS_PCI_DEVICE_MSIX:
254 /*
255 * FIXME: This probably should be done after a (not yet
256 * existing) post domain creation callback once to make
257 * support for dynamic post-enable MSI-X allocations
258 * work without having to reevaluate the domain size
259 * over and over. It is known already at allocation
260 * time via info->hwsize.
261 *
262 * That should work perfectly fine for MSI/MSI-X but needs
263 * some thoughts for purely software managed MSI domains
264 * where the index space is only limited artificially via
265 * %MSI_MAX_INDEX.
266 */
267 info->ops->msi_prepare = its_pci_msi_prepare;
268 info->ops->msi_teardown = its_msi_teardown;
269 break;
270 case DOMAIN_BUS_DEVICE_MSI:
271 case DOMAIN_BUS_WIRED_TO_MSI:
272 /*
273 * FIXME: See the above PCI prepare comment. The domain
274 * size is also known at domain creation time.
275 */
276 info->ops->msi_prepare = its_pmsi_prepare;
277 info->ops->msi_teardown = its_msi_teardown;
278 break;
279 default:
280 /* Confused. How did the lib return true? */
281 WARN_ON_ONCE(1);
282 return false;
283 }
284
285 return true;
286 }
287
288 static bool its_v5_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
289 struct irq_domain *real_parent, struct msi_domain_info *info)
290 {
291 if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
292 return false;
293
294 switch (info->bus_token) {
295 case DOMAIN_BUS_PCI_DEVICE_MSI:
296 case DOMAIN_BUS_PCI_DEVICE_MSIX:
297 info->ops->msi_prepare = its_v5_pci_msi_prepare;
298 info->ops->msi_teardown = its_msi_teardown;
299 break;
300 case DOMAIN_BUS_DEVICE_MSI:
301 case DOMAIN_BUS_WIRED_TO_MSI:
302 info->ops->msi_prepare = its_v5_pmsi_prepare;
303 info->ops->msi_teardown = its_msi_teardown;
304 break;
305 default:
306 /* Confused. How did the lib return true? */
307 WARN_ON_ONCE(1);
308 return false;
309 }
310
311 return true;
312 }
313
314 const struct msi_parent_ops gic_v3_its_msi_parent_ops = {
315 .supported_flags = ITS_MSI_FLAGS_SUPPORTED,
316 .required_flags = ITS_MSI_FLAGS_REQUIRED,
317 .chip_flags = MSI_CHIP_FLAG_SET_EOI,
318 .bus_select_token = DOMAIN_BUS_NEXUS,
319 .bus_select_mask = MATCH_PCI_MSI | MATCH_PLATFORM_MSI,
320 .prefix = "ITS-",
321 .init_dev_msi_info = its_init_dev_msi_info,
322 };
323
324 const struct msi_parent_ops gic_v5_its_msi_parent_ops = {
325 .supported_flags = ITS_MSI_FLAGS_SUPPORTED,
326 .required_flags = ITS_MSI_FLAGS_REQUIRED,
327 .chip_flags = MSI_CHIP_FLAG_SET_EOI,
328 .bus_select_token = DOMAIN_BUS_NEXUS,
329 .bus_select_mask = MATCH_PCI_MSI | MATCH_PLATFORM_MSI,
330 .prefix = "ITS-v5-",
331 .init_dev_msi_info = its_v5_init_dev_msi_info,
332 };