1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Message Signaled Interrupt (MSI) - irqdomain support
4 */
5 #include <linux/acpi_iort.h>
6 #include <linux/irqdomain.h>
7 #include <linux/of_irq.h>
8
9 #include "msi.h"
10
pci_msi_setup_msi_irqs(struct pci_dev * dev,int nvec,int type)11 int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
12 {
13 struct irq_domain *domain;
14
15 domain = dev_get_msi_domain(&dev->dev);
16 if (domain && irq_domain_is_hierarchy(domain))
17 return msi_domain_alloc_irqs_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN, nvec);
18
19 return pci_msi_legacy_setup_msi_irqs(dev, nvec, type);
20 }
21
pci_msi_teardown_msi_irqs(struct pci_dev * dev)22 void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
23 {
24 struct irq_domain *domain;
25
26 domain = dev_get_msi_domain(&dev->dev);
27 if (domain && irq_domain_is_hierarchy(domain)) {
28 msi_domain_free_irqs_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN);
29 } else {
30 pci_msi_legacy_teardown_msi_irqs(dev);
31 msi_free_msi_descs(&dev->dev);
32 }
33 }
34
35 /**
36 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
37 * @irq_data: Pointer to interrupt data of the MSI interrupt
38 * @msg: Pointer to the message
39 */
pci_msi_domain_write_msg(struct irq_data * irq_data,struct msi_msg * msg)40 static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
41 {
42 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
43
44 /*
45 * For MSI-X desc->irq is always equal to irq_data->irq. For
46 * MSI only the first interrupt of MULTI MSI passes the test.
47 */
48 if (desc->irq == irq_data->irq)
49 __pci_write_msi_msg(desc, msg);
50 }
51
52 /**
53 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
54 * @desc: Pointer to the MSI descriptor
55 *
56 * The ID number is only used within the irqdomain.
57 */
pci_msi_domain_calc_hwirq(struct msi_desc * desc)58 static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
59 {
60 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
61
62 return (irq_hw_number_t)desc->msi_index |
63 pci_dev_id(dev) << 11 |
64 ((irq_hw_number_t)(pci_domain_nr(dev->bus) & 0xFFFFFFFF)) << 27;
65 }
66
pci_msi_domain_set_desc(msi_alloc_info_t * arg,struct msi_desc * desc)67 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
68 struct msi_desc *desc)
69 {
70 arg->desc = desc;
71 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
72 }
73
74 static struct msi_domain_ops pci_msi_domain_ops_default = {
75 .set_desc = pci_msi_domain_set_desc,
76 };
77
pci_msi_domain_update_dom_ops(struct msi_domain_info * info)78 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
79 {
80 struct msi_domain_ops *ops = info->ops;
81
82 if (ops == NULL) {
83 info->ops = &pci_msi_domain_ops_default;
84 } else {
85 if (ops->set_desc == NULL)
86 ops->set_desc = pci_msi_domain_set_desc;
87 }
88 }
89
pci_msi_domain_update_chip_ops(struct msi_domain_info * info)90 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
91 {
92 struct irq_chip *chip = info->chip;
93
94 BUG_ON(!chip);
95 if (!chip->irq_write_msi_msg)
96 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
97 if (!chip->irq_mask)
98 chip->irq_mask = pci_msi_mask_irq;
99 if (!chip->irq_unmask)
100 chip->irq_unmask = pci_msi_unmask_irq;
101 }
102
103 /**
104 * pci_msi_create_irq_domain - Create a MSI interrupt domain
105 * @fwnode: Optional fwnode of the interrupt controller
106 * @info: MSI domain info
107 * @parent: Parent irq domain
108 *
109 * Updates the domain and chip ops and creates a MSI interrupt domain.
110 *
111 * Returns:
112 * A domain pointer or NULL in case of failure.
113 */
pci_msi_create_irq_domain(struct fwnode_handle * fwnode,struct msi_domain_info * info,struct irq_domain * parent)114 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
115 struct msi_domain_info *info,
116 struct irq_domain *parent)
117 {
118 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
119 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
120
121 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
122 pci_msi_domain_update_dom_ops(info);
123 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
124 pci_msi_domain_update_chip_ops(info);
125
126 /* Let the core code free MSI descriptors when freeing interrupts */
127 info->flags |= MSI_FLAG_FREE_MSI_DESCS;
128
129 info->flags |= MSI_FLAG_ACTIVATE_EARLY | MSI_FLAG_DEV_SYSFS;
130 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
131 info->flags |= MSI_FLAG_MUST_REACTIVATE;
132
133 /* PCI-MSI is oneshot-safe */
134 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
135 /* Let the core update the bus token */
136 info->bus_token = DOMAIN_BUS_PCI_MSI;
137
138 return msi_create_irq_domain(fwnode, info, parent);
139 }
140 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
141
142 /*
143 * Per device MSI[-X] domain functionality
144 */
pci_device_domain_set_desc(msi_alloc_info_t * arg,struct msi_desc * desc)145 static void pci_device_domain_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
146 {
147 arg->desc = desc;
148 arg->hwirq = desc->msi_index;
149 }
150
cond_mask_parent(struct irq_data * data)151 static __always_inline void cond_mask_parent(struct irq_data *data)
152 {
153 struct msi_domain_info *info = data->domain->host_data;
154
155 if (unlikely(info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT))
156 irq_chip_mask_parent(data);
157 }
158
cond_unmask_parent(struct irq_data * data)159 static __always_inline void cond_unmask_parent(struct irq_data *data)
160 {
161 struct msi_domain_info *info = data->domain->host_data;
162
163 if (unlikely(info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT))
164 irq_chip_unmask_parent(data);
165 }
166
pci_irq_mask_msi(struct irq_data * data)167 static void pci_irq_mask_msi(struct irq_data *data)
168 {
169 struct msi_desc *desc = irq_data_get_msi_desc(data);
170
171 pci_msi_mask(desc, BIT(data->irq - desc->irq));
172 cond_mask_parent(data);
173 }
174
pci_irq_unmask_msi(struct irq_data * data)175 static void pci_irq_unmask_msi(struct irq_data *data)
176 {
177 struct msi_desc *desc = irq_data_get_msi_desc(data);
178
179 cond_unmask_parent(data);
180 pci_msi_unmask(desc, BIT(data->irq - desc->irq));
181 }
182
183 #ifdef CONFIG_GENERIC_IRQ_RESERVATION_MODE
184 # define MSI_REACTIVATE MSI_FLAG_MUST_REACTIVATE
185 #else
186 # define MSI_REACTIVATE 0
187 #endif
188
189 #define MSI_COMMON_FLAGS (MSI_FLAG_FREE_MSI_DESCS | \
190 MSI_FLAG_ACTIVATE_EARLY | \
191 MSI_FLAG_DEV_SYSFS | \
192 MSI_REACTIVATE)
193
194 static const struct msi_domain_template pci_msi_template = {
195 .chip = {
196 .name = "PCI-MSI",
197 .irq_mask = pci_irq_mask_msi,
198 .irq_unmask = pci_irq_unmask_msi,
199 .irq_write_msi_msg = pci_msi_domain_write_msg,
200 .flags = IRQCHIP_ONESHOT_SAFE,
201 },
202
203 .ops = {
204 .set_desc = pci_device_domain_set_desc,
205 },
206
207 .info = {
208 .flags = MSI_COMMON_FLAGS | MSI_FLAG_MULTI_PCI_MSI,
209 .bus_token = DOMAIN_BUS_PCI_DEVICE_MSI,
210 },
211 };
212
pci_irq_mask_msix(struct irq_data * data)213 static void pci_irq_mask_msix(struct irq_data *data)
214 {
215 pci_msix_mask(irq_data_get_msi_desc(data));
216 cond_mask_parent(data);
217 }
218
pci_irq_unmask_msix(struct irq_data * data)219 static void pci_irq_unmask_msix(struct irq_data *data)
220 {
221 cond_unmask_parent(data);
222 pci_msix_unmask(irq_data_get_msi_desc(data));
223 }
224
pci_msix_prepare_desc(struct irq_domain * domain,msi_alloc_info_t * arg,struct msi_desc * desc)225 void pci_msix_prepare_desc(struct irq_domain *domain, msi_alloc_info_t *arg,
226 struct msi_desc *desc)
227 {
228 /* Don't fiddle with preallocated MSI descriptors */
229 if (!desc->pci.mask_base)
230 msix_prepare_msi_desc(to_pci_dev(desc->dev), desc);
231 }
232 EXPORT_SYMBOL_GPL(pci_msix_prepare_desc);
233
234 static const struct msi_domain_template pci_msix_template = {
235 .chip = {
236 .name = "PCI-MSIX",
237 .irq_mask = pci_irq_mask_msix,
238 .irq_unmask = pci_irq_unmask_msix,
239 .irq_write_msi_msg = pci_msi_domain_write_msg,
240 .flags = IRQCHIP_ONESHOT_SAFE,
241 },
242
243 .ops = {
244 .prepare_desc = pci_msix_prepare_desc,
245 .set_desc = pci_device_domain_set_desc,
246 },
247
248 .info = {
249 .flags = MSI_COMMON_FLAGS | MSI_FLAG_PCI_MSIX |
250 MSI_FLAG_PCI_MSIX_ALLOC_DYN,
251 .bus_token = DOMAIN_BUS_PCI_DEVICE_MSIX,
252 },
253 };
254
pci_match_device_domain(struct pci_dev * pdev,enum irq_domain_bus_token bus_token)255 static bool pci_match_device_domain(struct pci_dev *pdev, enum irq_domain_bus_token bus_token)
256 {
257 return msi_match_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, bus_token);
258 }
259
pci_create_device_domain(struct pci_dev * pdev,const struct msi_domain_template * tmpl,unsigned int hwsize)260 static bool pci_create_device_domain(struct pci_dev *pdev, const struct msi_domain_template *tmpl,
261 unsigned int hwsize)
262 {
263 struct irq_domain *domain = dev_get_msi_domain(&pdev->dev);
264
265 if (!domain || !irq_domain_is_msi_parent(domain))
266 return true;
267
268 return msi_create_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, tmpl,
269 hwsize, NULL, NULL);
270 }
271
272 /**
273 * pci_setup_msi_device_domain - Setup a device MSI interrupt domain
274 * @pdev: The PCI device to create the domain on
275 * @hwsize: The maximum number of MSI vectors
276 *
277 * Return:
278 * True when:
279 * - The device does not have a MSI parent irq domain associated,
280 * which keeps the legacy architecture specific and the global
281 * PCI/MSI domain models working
282 * - The MSI domain exists already
283 * - The MSI domain was successfully allocated
284 * False when:
285 * - MSI-X is enabled
286 * - The domain creation fails.
287 *
288 * The created MSI domain is preserved until:
289 * - The device is removed
290 * - MSI is disabled and a MSI-X domain is created
291 */
pci_setup_msi_device_domain(struct pci_dev * pdev,unsigned int hwsize)292 bool pci_setup_msi_device_domain(struct pci_dev *pdev, unsigned int hwsize)
293 {
294 if (WARN_ON_ONCE(pdev->msix_enabled))
295 return false;
296
297 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
298 return true;
299 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
300 msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
301
302 return pci_create_device_domain(pdev, &pci_msi_template, hwsize);
303 }
304
305 /**
306 * pci_setup_msix_device_domain - Setup a device MSI-X interrupt domain
307 * @pdev: The PCI device to create the domain on
308 * @hwsize: The size of the MSI-X vector table
309 *
310 * Return:
311 * True when:
312 * - The device does not have a MSI parent irq domain associated,
313 * which keeps the legacy architecture specific and the global
314 * PCI/MSI domain models working
315 * - The MSI-X domain exists already
316 * - The MSI-X domain was successfully allocated
317 * False when:
318 * - MSI is enabled
319 * - The domain creation fails.
320 *
321 * The created MSI-X domain is preserved until:
322 * - The device is removed
323 * - MSI-X is disabled and a MSI domain is created
324 */
pci_setup_msix_device_domain(struct pci_dev * pdev,unsigned int hwsize)325 bool pci_setup_msix_device_domain(struct pci_dev *pdev, unsigned int hwsize)
326 {
327 if (WARN_ON_ONCE(pdev->msi_enabled))
328 return false;
329
330 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
331 return true;
332 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
333 msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
334
335 return pci_create_device_domain(pdev, &pci_msix_template, hwsize);
336 }
337
338 /**
339 * pci_msi_domain_supports - Check for support of a particular feature flag
340 * @pdev: The PCI device to operate on
341 * @feature_mask: The feature mask to check for (full match)
342 * @mode: If ALLOW_LEGACY this grants the feature when there is no irq domain
343 * associated to the device. If DENY_LEGACY the lack of an irq domain
344 * makes the feature unsupported
345 */
pci_msi_domain_supports(struct pci_dev * pdev,unsigned int feature_mask,enum support_mode mode)346 bool pci_msi_domain_supports(struct pci_dev *pdev, unsigned int feature_mask,
347 enum support_mode mode)
348 {
349 struct msi_domain_info *info;
350 struct irq_domain *domain;
351 unsigned int supported;
352
353 domain = dev_get_msi_domain(&pdev->dev);
354
355 if (!domain || !irq_domain_is_hierarchy(domain)) {
356 if (IS_ENABLED(CONFIG_PCI_MSI_ARCH_FALLBACKS))
357 return mode == ALLOW_LEGACY;
358 return false;
359 }
360
361 if (!irq_domain_is_msi_parent(domain)) {
362 /*
363 * For "global" PCI/MSI interrupt domains the associated
364 * msi_domain_info::flags is the authoritative source of
365 * information.
366 */
367 info = domain->host_data;
368 supported = info->flags;
369 } else {
370 /*
371 * For MSI parent domains the supported feature set
372 * is available in the parent ops. This makes checks
373 * possible before actually instantiating the
374 * per device domain because the parent is never
375 * expanding the PCI/MSI functionality.
376 */
377 supported = domain->msi_parent_ops->supported_flags;
378 }
379
380 return (supported & feature_mask) == feature_mask;
381 }
382
383 /*
384 * Users of the generic MSI infrastructure expect a device to have a single ID,
385 * so with DMA aliases we have to pick the least-worst compromise. Devices with
386 * DMA phantom functions tend to still emit MSIs from the real function number,
387 * so we ignore those and only consider topological aliases where either the
388 * alias device or RID appears on a different bus number. We also make the
389 * reasonable assumption that bridges are walked in an upstream direction (so
390 * the last one seen wins), and the much braver assumption that the most likely
391 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
392 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
393 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
394 * for taking ownership all we can really do is close our eyes and hope...
395 */
get_msi_id_cb(struct pci_dev * pdev,u16 alias,void * data)396 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
397 {
398 u32 *pa = data;
399 u8 bus = PCI_BUS_NUM(*pa);
400
401 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
402 *pa = alias;
403
404 return 0;
405 }
406
407 /**
408 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
409 * @domain: The interrupt domain
410 * @pdev: The PCI device.
411 *
412 * The RID for a device is formed from the alias, with a firmware
413 * supplied mapping applied
414 *
415 * Returns: The RID.
416 */
pci_msi_domain_get_msi_rid(struct irq_domain * domain,struct pci_dev * pdev)417 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
418 {
419 struct device_node *of_node;
420 u32 rid = pci_dev_id(pdev);
421
422 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
423
424 of_node = irq_domain_get_of_node(domain);
425 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
426 iort_msi_map_id(&pdev->dev, rid);
427
428 return rid;
429 }
430
431 /**
432 * pci_msi_map_rid_ctlr_node - Get the MSI controller node and MSI requester id (RID)
433 * @pdev: The PCI device
434 * @node: Pointer to store the MSI controller device node
435 *
436 * Use the firmware data to find the MSI controller node for @pdev.
437 * If found map the RID and initialize @node with it. @node value must
438 * be set to NULL on entry.
439 *
440 * Returns: The RID.
441 */
pci_msi_map_rid_ctlr_node(struct pci_dev * pdev,struct device_node ** node)442 u32 pci_msi_map_rid_ctlr_node(struct pci_dev *pdev, struct device_node **node)
443 {
444 u32 rid = pci_dev_id(pdev);
445
446 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
447
448 return of_msi_xlate(&pdev->dev, node, rid);
449 }
450
451 /**
452 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
453 * @pdev: The PCI device
454 *
455 * Use the firmware data to find a device-specific MSI domain
456 * (i.e. not one that is set as a default).
457 *
458 * Returns: The corresponding MSI domain or NULL if none has been found.
459 */
pci_msi_get_device_domain(struct pci_dev * pdev)460 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
461 {
462 struct irq_domain *dom;
463 u32 rid = pci_dev_id(pdev);
464
465 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
466 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
467 if (!dom)
468 dom = iort_get_device_domain(&pdev->dev, rid,
469 DOMAIN_BUS_PCI_MSI);
470 return dom;
471 }
472