io_uring: ensure finish_wait() is always called in __io_uring_task_cancel()
[linux/fpc-iii.git] / drivers / pci / msi.c
blob3162f88fe940a7171719c05e0a8c231b5998297f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7 * Copyright (C) 2016 Christoph Hellwig.
8 */
10 #include <linux/err.h>
11 #include <linux/mm.h>
12 #include <linux/irq.h>
13 #include <linux/interrupt.h>
14 #include <linux/export.h>
15 #include <linux/ioport.h>
16 #include <linux/pci.h>
17 #include <linux/proc_fs.h>
18 #include <linux/msi.h>
19 #include <linux/smp.h>
20 #include <linux/errno.h>
21 #include <linux/io.h>
22 #include <linux/acpi_iort.h>
23 #include <linux/slab.h>
24 #include <linux/irqdomain.h>
25 #include <linux/of_irq.h>
27 #include "pci.h"
29 #ifdef CONFIG_PCI_MSI
31 static int pci_msi_enable = 1;
32 int pci_msi_ignore_mask;
34 #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
36 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
37 static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
39 struct irq_domain *domain;
41 domain = dev_get_msi_domain(&dev->dev);
42 if (domain && irq_domain_is_hierarchy(domain))
43 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
45 return arch_setup_msi_irqs(dev, nvec, type);
48 static void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
50 struct irq_domain *domain;
52 domain = dev_get_msi_domain(&dev->dev);
53 if (domain && irq_domain_is_hierarchy(domain))
54 msi_domain_free_irqs(domain, &dev->dev);
55 else
56 arch_teardown_msi_irqs(dev);
58 #else
59 #define pci_msi_setup_msi_irqs arch_setup_msi_irqs
60 #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
61 #endif
63 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
64 /* Arch hooks */
65 int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
67 struct msi_controller *chip = dev->bus->msi;
68 int err;
70 if (!chip || !chip->setup_irq)
71 return -EINVAL;
73 err = chip->setup_irq(chip, dev, desc);
74 if (err < 0)
75 return err;
77 irq_set_chip_data(desc->irq, chip);
79 return 0;
82 void __weak arch_teardown_msi_irq(unsigned int irq)
84 struct msi_controller *chip = irq_get_chip_data(irq);
86 if (!chip || !chip->teardown_irq)
87 return;
89 chip->teardown_irq(chip, irq);
92 int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
94 struct msi_controller *chip = dev->bus->msi;
95 struct msi_desc *entry;
96 int ret;
98 if (chip && chip->setup_irqs)
99 return chip->setup_irqs(chip, dev, nvec, type);
101 * If an architecture wants to support multiple MSI, it needs to
102 * override arch_setup_msi_irqs()
104 if (type == PCI_CAP_ID_MSI && nvec > 1)
105 return 1;
107 for_each_pci_msi_entry(entry, dev) {
108 ret = arch_setup_msi_irq(dev, entry);
109 if (ret < 0)
110 return ret;
111 if (ret > 0)
112 return -ENOSPC;
115 return 0;
119 * We have a default implementation available as a separate non-weak
120 * function, as it is used by the Xen x86 PCI code
122 void default_teardown_msi_irqs(struct pci_dev *dev)
124 int i;
125 struct msi_desc *entry;
127 for_each_pci_msi_entry(entry, dev)
128 if (entry->irq)
129 for (i = 0; i < entry->nvec_used; i++)
130 arch_teardown_msi_irq(entry->irq + i);
133 void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
135 return default_teardown_msi_irqs(dev);
137 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
139 static void default_restore_msi_irq(struct pci_dev *dev, int irq)
141 struct msi_desc *entry;
143 entry = NULL;
144 if (dev->msix_enabled) {
145 for_each_pci_msi_entry(entry, dev) {
146 if (irq == entry->irq)
147 break;
149 } else if (dev->msi_enabled) {
150 entry = irq_get_msi_desc(irq);
153 if (entry)
154 __pci_write_msi_msg(entry, &entry->msg);
157 void __weak arch_restore_msi_irqs(struct pci_dev *dev)
159 return default_restore_msi_irqs(dev);
162 static inline __attribute_const__ u32 msi_mask(unsigned x)
164 /* Don't shift by >= width of type */
165 if (x >= 5)
166 return 0xffffffff;
167 return (1 << (1 << x)) - 1;
171 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
172 * mask all MSI interrupts by clearing the MSI enable bit does not work
173 * reliably as devices without an INTx disable bit will then generate a
174 * level IRQ which will never be cleared.
176 u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
178 u32 mask_bits = desc->masked;
180 if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
181 return 0;
183 mask_bits &= ~mask;
184 mask_bits |= flag;
185 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
186 mask_bits);
188 return mask_bits;
191 static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
193 desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag);
196 static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
198 if (desc->msi_attrib.is_virtual)
199 return NULL;
201 return desc->mask_base +
202 desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
206 * This internal function does not flush PCI writes to the device.
207 * All users must ensure that they read from the device before either
208 * assuming that the device state is up to date, or returning out of this
209 * file. This saves a few milliseconds when initialising devices with lots
210 * of MSI-X interrupts.
212 u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag)
214 u32 mask_bits = desc->masked;
215 void __iomem *desc_addr;
217 if (pci_msi_ignore_mask)
218 return 0;
220 desc_addr = pci_msix_desc_addr(desc);
221 if (!desc_addr)
222 return 0;
224 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
225 if (flag & PCI_MSIX_ENTRY_CTRL_MASKBIT)
226 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
228 writel(mask_bits, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
230 return mask_bits;
233 static void msix_mask_irq(struct msi_desc *desc, u32 flag)
235 desc->masked = __pci_msix_desc_mask_irq(desc, flag);
238 static void msi_set_mask_bit(struct irq_data *data, u32 flag)
240 struct msi_desc *desc = irq_data_get_msi_desc(data);
242 if (desc->msi_attrib.is_msix) {
243 msix_mask_irq(desc, flag);
244 readl(desc->mask_base); /* Flush write to device */
245 } else {
246 unsigned offset = data->irq - desc->irq;
247 msi_mask_irq(desc, 1 << offset, flag << offset);
252 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
253 * @data: pointer to irqdata associated to that interrupt
255 void pci_msi_mask_irq(struct irq_data *data)
257 msi_set_mask_bit(data, 1);
259 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
262 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
263 * @data: pointer to irqdata associated to that interrupt
265 void pci_msi_unmask_irq(struct irq_data *data)
267 msi_set_mask_bit(data, 0);
269 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
271 void default_restore_msi_irqs(struct pci_dev *dev)
273 struct msi_desc *entry;
275 for_each_pci_msi_entry(entry, dev)
276 default_restore_msi_irq(dev, entry->irq);
279 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
281 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
283 BUG_ON(dev->current_state != PCI_D0);
285 if (entry->msi_attrib.is_msix) {
286 void __iomem *base = pci_msix_desc_addr(entry);
288 if (!base) {
289 WARN_ON(1);
290 return;
293 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
294 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
295 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
296 } else {
297 int pos = dev->msi_cap;
298 u16 data;
300 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
301 &msg->address_lo);
302 if (entry->msi_attrib.is_64) {
303 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
304 &msg->address_hi);
305 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
306 } else {
307 msg->address_hi = 0;
308 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
310 msg->data = data;
314 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
316 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
318 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
319 /* Don't touch the hardware now */
320 } else if (entry->msi_attrib.is_msix) {
321 void __iomem *base = pci_msix_desc_addr(entry);
323 if (!base)
324 goto skip;
326 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
327 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
328 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
329 } else {
330 int pos = dev->msi_cap;
331 u16 msgctl;
333 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
334 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
335 msgctl |= entry->msi_attrib.multiple << 4;
336 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
338 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
339 msg->address_lo);
340 if (entry->msi_attrib.is_64) {
341 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
342 msg->address_hi);
343 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
344 msg->data);
345 } else {
346 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
347 msg->data);
351 skip:
352 entry->msg = *msg;
354 if (entry->write_msi_msg)
355 entry->write_msi_msg(entry, entry->write_msi_msg_data);
359 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
361 struct msi_desc *entry = irq_get_msi_desc(irq);
363 __pci_write_msi_msg(entry, msg);
365 EXPORT_SYMBOL_GPL(pci_write_msi_msg);
367 static void free_msi_irqs(struct pci_dev *dev)
369 struct list_head *msi_list = dev_to_msi_list(&dev->dev);
370 struct msi_desc *entry, *tmp;
371 struct attribute **msi_attrs;
372 struct device_attribute *dev_attr;
373 int i, count = 0;
375 for_each_pci_msi_entry(entry, dev)
376 if (entry->irq)
377 for (i = 0; i < entry->nvec_used; i++)
378 BUG_ON(irq_has_action(entry->irq + i));
380 pci_msi_teardown_msi_irqs(dev);
382 list_for_each_entry_safe(entry, tmp, msi_list, list) {
383 if (entry->msi_attrib.is_msix) {
384 if (list_is_last(&entry->list, msi_list))
385 iounmap(entry->mask_base);
388 list_del(&entry->list);
389 free_msi_entry(entry);
392 if (dev->msi_irq_groups) {
393 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
394 msi_attrs = dev->msi_irq_groups[0]->attrs;
395 while (msi_attrs[count]) {
396 dev_attr = container_of(msi_attrs[count],
397 struct device_attribute, attr);
398 kfree(dev_attr->attr.name);
399 kfree(dev_attr);
400 ++count;
402 kfree(msi_attrs);
403 kfree(dev->msi_irq_groups[0]);
404 kfree(dev->msi_irq_groups);
405 dev->msi_irq_groups = NULL;
409 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
411 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
412 pci_intx(dev, enable);
415 static void pci_msi_set_enable(struct pci_dev *dev, int enable)
417 u16 control;
419 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
420 control &= ~PCI_MSI_FLAGS_ENABLE;
421 if (enable)
422 control |= PCI_MSI_FLAGS_ENABLE;
423 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
426 static void __pci_restore_msi_state(struct pci_dev *dev)
428 u16 control;
429 struct msi_desc *entry;
431 if (!dev->msi_enabled)
432 return;
434 entry = irq_get_msi_desc(dev->irq);
436 pci_intx_for_msi(dev, 0);
437 pci_msi_set_enable(dev, 0);
438 arch_restore_msi_irqs(dev);
440 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
441 msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
442 entry->masked);
443 control &= ~PCI_MSI_FLAGS_QSIZE;
444 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
445 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
448 static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
450 u16 ctrl;
452 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
453 ctrl &= ~clear;
454 ctrl |= set;
455 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
458 static void __pci_restore_msix_state(struct pci_dev *dev)
460 struct msi_desc *entry;
462 if (!dev->msix_enabled)
463 return;
464 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
466 /* route the table */
467 pci_intx_for_msi(dev, 0);
468 pci_msix_clear_and_set_ctrl(dev, 0,
469 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
471 arch_restore_msi_irqs(dev);
472 for_each_pci_msi_entry(entry, dev)
473 msix_mask_irq(entry, entry->masked);
475 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
478 void pci_restore_msi_state(struct pci_dev *dev)
480 __pci_restore_msi_state(dev);
481 __pci_restore_msix_state(dev);
483 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
485 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
486 char *buf)
488 struct msi_desc *entry;
489 unsigned long irq;
490 int retval;
492 retval = kstrtoul(attr->attr.name, 10, &irq);
493 if (retval)
494 return retval;
496 entry = irq_get_msi_desc(irq);
497 if (entry)
498 return sprintf(buf, "%s\n",
499 entry->msi_attrib.is_msix ? "msix" : "msi");
501 return -ENODEV;
504 static int populate_msi_sysfs(struct pci_dev *pdev)
506 struct attribute **msi_attrs;
507 struct attribute *msi_attr;
508 struct device_attribute *msi_dev_attr;
509 struct attribute_group *msi_irq_group;
510 const struct attribute_group **msi_irq_groups;
511 struct msi_desc *entry;
512 int ret = -ENOMEM;
513 int num_msi = 0;
514 int count = 0;
515 int i;
517 /* Determine how many msi entries we have */
518 for_each_pci_msi_entry(entry, pdev)
519 num_msi += entry->nvec_used;
520 if (!num_msi)
521 return 0;
523 /* Dynamically create the MSI attributes for the PCI device */
524 msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL);
525 if (!msi_attrs)
526 return -ENOMEM;
527 for_each_pci_msi_entry(entry, pdev) {
528 for (i = 0; i < entry->nvec_used; i++) {
529 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
530 if (!msi_dev_attr)
531 goto error_attrs;
532 msi_attrs[count] = &msi_dev_attr->attr;
534 sysfs_attr_init(&msi_dev_attr->attr);
535 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
536 entry->irq + i);
537 if (!msi_dev_attr->attr.name)
538 goto error_attrs;
539 msi_dev_attr->attr.mode = S_IRUGO;
540 msi_dev_attr->show = msi_mode_show;
541 ++count;
545 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
546 if (!msi_irq_group)
547 goto error_attrs;
548 msi_irq_group->name = "msi_irqs";
549 msi_irq_group->attrs = msi_attrs;
551 msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL);
552 if (!msi_irq_groups)
553 goto error_irq_group;
554 msi_irq_groups[0] = msi_irq_group;
556 ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
557 if (ret)
558 goto error_irq_groups;
559 pdev->msi_irq_groups = msi_irq_groups;
561 return 0;
563 error_irq_groups:
564 kfree(msi_irq_groups);
565 error_irq_group:
566 kfree(msi_irq_group);
567 error_attrs:
568 count = 0;
569 msi_attr = msi_attrs[count];
570 while (msi_attr) {
571 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
572 kfree(msi_attr->name);
573 kfree(msi_dev_attr);
574 ++count;
575 msi_attr = msi_attrs[count];
577 kfree(msi_attrs);
578 return ret;
581 static struct msi_desc *
582 msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
584 struct irq_affinity_desc *masks = NULL;
585 struct msi_desc *entry;
586 u16 control;
588 if (affd)
589 masks = irq_create_affinity_masks(nvec, affd);
591 /* MSI Entry Initialization */
592 entry = alloc_msi_entry(&dev->dev, nvec, masks);
593 if (!entry)
594 goto out;
596 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
598 entry->msi_attrib.is_msix = 0;
599 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
600 entry->msi_attrib.is_virtual = 0;
601 entry->msi_attrib.entry_nr = 0;
602 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
603 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
604 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
605 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
607 if (control & PCI_MSI_FLAGS_64BIT)
608 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
609 else
610 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
612 /* Save the initial mask status */
613 if (entry->msi_attrib.maskbit)
614 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
616 out:
617 kfree(masks);
618 return entry;
621 static int msi_verify_entries(struct pci_dev *dev)
623 struct msi_desc *entry;
625 for_each_pci_msi_entry(entry, dev) {
626 if (entry->msg.address_hi && dev->no_64bit_msi) {
627 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
628 entry->msg.address_hi, entry->msg.address_lo);
629 return -EIO;
632 return 0;
636 * msi_capability_init - configure device's MSI capability structure
637 * @dev: pointer to the pci_dev data structure of MSI device function
638 * @nvec: number of interrupts to allocate
639 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
641 * Setup the MSI capability structure of the device with the requested
642 * number of interrupts. A return value of zero indicates the successful
643 * setup of an entry with the new MSI IRQ. A negative return value indicates
644 * an error, and a positive return value indicates the number of interrupts
645 * which could have been allocated.
647 static int msi_capability_init(struct pci_dev *dev, int nvec,
648 struct irq_affinity *affd)
650 struct msi_desc *entry;
651 int ret;
652 unsigned mask;
654 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
656 entry = msi_setup_entry(dev, nvec, affd);
657 if (!entry)
658 return -ENOMEM;
660 /* All MSIs are unmasked by default; mask them all */
661 mask = msi_mask(entry->msi_attrib.multi_cap);
662 msi_mask_irq(entry, mask, mask);
664 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
666 /* Configure MSI capability structure */
667 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
668 if (ret) {
669 msi_mask_irq(entry, mask, ~mask);
670 free_msi_irqs(dev);
671 return ret;
674 ret = msi_verify_entries(dev);
675 if (ret) {
676 msi_mask_irq(entry, mask, ~mask);
677 free_msi_irqs(dev);
678 return ret;
681 ret = populate_msi_sysfs(dev);
682 if (ret) {
683 msi_mask_irq(entry, mask, ~mask);
684 free_msi_irqs(dev);
685 return ret;
688 /* Set MSI enabled bits */
689 pci_intx_for_msi(dev, 0);
690 pci_msi_set_enable(dev, 1);
691 dev->msi_enabled = 1;
693 pcibios_free_irq(dev);
694 dev->irq = entry->irq;
695 return 0;
698 static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
700 resource_size_t phys_addr;
701 u32 table_offset;
702 unsigned long flags;
703 u8 bir;
705 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
706 &table_offset);
707 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
708 flags = pci_resource_flags(dev, bir);
709 if (!flags || (flags & IORESOURCE_UNSET))
710 return NULL;
712 table_offset &= PCI_MSIX_TABLE_OFFSET;
713 phys_addr = pci_resource_start(dev, bir) + table_offset;
715 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
718 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
719 struct msix_entry *entries, int nvec,
720 struct irq_affinity *affd)
722 struct irq_affinity_desc *curmsk, *masks = NULL;
723 struct msi_desc *entry;
724 int ret, i;
725 int vec_count = pci_msix_vec_count(dev);
727 if (affd)
728 masks = irq_create_affinity_masks(nvec, affd);
730 for (i = 0, curmsk = masks; i < nvec; i++) {
731 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
732 if (!entry) {
733 if (!i)
734 iounmap(base);
735 else
736 free_msi_irqs(dev);
737 /* No enough memory. Don't try again */
738 ret = -ENOMEM;
739 goto out;
742 entry->msi_attrib.is_msix = 1;
743 entry->msi_attrib.is_64 = 1;
744 if (entries)
745 entry->msi_attrib.entry_nr = entries[i].entry;
746 else
747 entry->msi_attrib.entry_nr = i;
749 entry->msi_attrib.is_virtual =
750 entry->msi_attrib.entry_nr >= vec_count;
752 entry->msi_attrib.default_irq = dev->irq;
753 entry->mask_base = base;
755 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
756 if (masks)
757 curmsk++;
759 ret = 0;
760 out:
761 kfree(masks);
762 return ret;
765 static void msix_program_entries(struct pci_dev *dev,
766 struct msix_entry *entries)
768 struct msi_desc *entry;
769 int i = 0;
770 void __iomem *desc_addr;
772 for_each_pci_msi_entry(entry, dev) {
773 if (entries)
774 entries[i++].vector = entry->irq;
776 desc_addr = pci_msix_desc_addr(entry);
777 if (desc_addr)
778 entry->masked = readl(desc_addr +
779 PCI_MSIX_ENTRY_VECTOR_CTRL);
780 else
781 entry->masked = 0;
783 msix_mask_irq(entry, 1);
788 * msix_capability_init - configure device's MSI-X capability
789 * @dev: pointer to the pci_dev data structure of MSI-X device function
790 * @entries: pointer to an array of struct msix_entry entries
791 * @nvec: number of @entries
792 * @affd: Optional pointer to enable automatic affinity assignment
794 * Setup the MSI-X capability structure of device function with a
795 * single MSI-X IRQ. A return of zero indicates the successful setup of
796 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
798 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
799 int nvec, struct irq_affinity *affd)
801 int ret;
802 u16 control;
803 void __iomem *base;
805 /* Ensure MSI-X is disabled while it is set up */
806 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
808 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
809 /* Request & Map MSI-X table region */
810 base = msix_map_region(dev, msix_table_size(control));
811 if (!base)
812 return -ENOMEM;
814 ret = msix_setup_entries(dev, base, entries, nvec, affd);
815 if (ret)
816 return ret;
818 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
819 if (ret)
820 goto out_avail;
822 /* Check if all MSI entries honor device restrictions */
823 ret = msi_verify_entries(dev);
824 if (ret)
825 goto out_free;
828 * Some devices require MSI-X to be enabled before we can touch the
829 * MSI-X registers. We need to mask all the vectors to prevent
830 * interrupts coming in before they're fully set up.
832 pci_msix_clear_and_set_ctrl(dev, 0,
833 PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
835 msix_program_entries(dev, entries);
837 ret = populate_msi_sysfs(dev);
838 if (ret)
839 goto out_free;
841 /* Set MSI-X enabled bits and unmask the function */
842 pci_intx_for_msi(dev, 0);
843 dev->msix_enabled = 1;
844 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
846 pcibios_free_irq(dev);
847 return 0;
849 out_avail:
850 if (ret < 0) {
852 * If we had some success, report the number of IRQs
853 * we succeeded in setting up.
855 struct msi_desc *entry;
856 int avail = 0;
858 for_each_pci_msi_entry(entry, dev) {
859 if (entry->irq != 0)
860 avail++;
862 if (avail != 0)
863 ret = avail;
866 out_free:
867 free_msi_irqs(dev);
869 return ret;
873 * pci_msi_supported - check whether MSI may be enabled on a device
874 * @dev: pointer to the pci_dev data structure of MSI device function
875 * @nvec: how many MSIs have been requested?
877 * Look at global flags, the device itself, and its parent buses
878 * to determine if MSI/-X are supported for the device. If MSI/-X is
879 * supported return 1, else return 0.
881 static int pci_msi_supported(struct pci_dev *dev, int nvec)
883 struct pci_bus *bus;
885 /* MSI must be globally enabled and supported by the device */
886 if (!pci_msi_enable)
887 return 0;
889 if (!dev || dev->no_msi)
890 return 0;
893 * You can't ask to have 0 or less MSIs configured.
894 * a) it's stupid ..
895 * b) the list manipulation code assumes nvec >= 1.
897 if (nvec < 1)
898 return 0;
901 * Any bridge which does NOT route MSI transactions from its
902 * secondary bus to its primary bus must set NO_MSI flag on
903 * the secondary pci_bus.
904 * We expect only arch-specific PCI host bus controller driver
905 * or quirks for specific PCI bridges to be setting NO_MSI.
907 for (bus = dev->bus; bus; bus = bus->parent)
908 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
909 return 0;
911 return 1;
915 * pci_msi_vec_count - Return the number of MSI vectors a device can send
916 * @dev: device to report about
918 * This function returns the number of MSI vectors a device requested via
919 * Multiple Message Capable register. It returns a negative errno if the
920 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
921 * and returns a power of two, up to a maximum of 2^5 (32), according to the
922 * MSI specification.
924 int pci_msi_vec_count(struct pci_dev *dev)
926 int ret;
927 u16 msgctl;
929 if (!dev->msi_cap)
930 return -EINVAL;
932 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
933 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
935 return ret;
937 EXPORT_SYMBOL(pci_msi_vec_count);
939 static void pci_msi_shutdown(struct pci_dev *dev)
941 struct msi_desc *desc;
942 u32 mask;
944 if (!pci_msi_enable || !dev || !dev->msi_enabled)
945 return;
947 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
948 desc = first_pci_msi_entry(dev);
950 pci_msi_set_enable(dev, 0);
951 pci_intx_for_msi(dev, 1);
952 dev->msi_enabled = 0;
954 /* Return the device with MSI unmasked as initial states */
955 mask = msi_mask(desc->msi_attrib.multi_cap);
956 /* Keep cached state to be restored */
957 __pci_msi_desc_mask_irq(desc, mask, ~mask);
959 /* Restore dev->irq to its default pin-assertion IRQ */
960 dev->irq = desc->msi_attrib.default_irq;
961 pcibios_alloc_irq(dev);
964 void pci_disable_msi(struct pci_dev *dev)
966 if (!pci_msi_enable || !dev || !dev->msi_enabled)
967 return;
969 pci_msi_shutdown(dev);
970 free_msi_irqs(dev);
972 EXPORT_SYMBOL(pci_disable_msi);
975 * pci_msix_vec_count - return the number of device's MSI-X table entries
976 * @dev: pointer to the pci_dev data structure of MSI-X device function
977 * This function returns the number of device's MSI-X table entries and
978 * therefore the number of MSI-X vectors device is capable of sending.
979 * It returns a negative errno if the device is not capable of sending MSI-X
980 * interrupts.
982 int pci_msix_vec_count(struct pci_dev *dev)
984 u16 control;
986 if (!dev->msix_cap)
987 return -EINVAL;
989 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
990 return msix_table_size(control);
992 EXPORT_SYMBOL(pci_msix_vec_count);
994 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
995 int nvec, struct irq_affinity *affd, int flags)
997 int nr_entries;
998 int i, j;
1000 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
1001 return -EINVAL;
1003 nr_entries = pci_msix_vec_count(dev);
1004 if (nr_entries < 0)
1005 return nr_entries;
1006 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
1007 return nr_entries;
1009 if (entries) {
1010 /* Check for any invalid entries */
1011 for (i = 0; i < nvec; i++) {
1012 if (entries[i].entry >= nr_entries)
1013 return -EINVAL; /* invalid entry */
1014 for (j = i + 1; j < nvec; j++) {
1015 if (entries[i].entry == entries[j].entry)
1016 return -EINVAL; /* duplicate entry */
1021 /* Check whether driver already requested for MSI IRQ */
1022 if (dev->msi_enabled) {
1023 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
1024 return -EINVAL;
1026 return msix_capability_init(dev, entries, nvec, affd);
1029 static void pci_msix_shutdown(struct pci_dev *dev)
1031 struct msi_desc *entry;
1033 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1034 return;
1036 if (pci_dev_is_disconnected(dev)) {
1037 dev->msix_enabled = 0;
1038 return;
1041 /* Return the device with MSI-X masked as initial states */
1042 for_each_pci_msi_entry(entry, dev) {
1043 /* Keep cached states to be restored */
1044 __pci_msix_desc_mask_irq(entry, 1);
1047 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1048 pci_intx_for_msi(dev, 1);
1049 dev->msix_enabled = 0;
1050 pcibios_alloc_irq(dev);
1053 void pci_disable_msix(struct pci_dev *dev)
1055 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1056 return;
1058 pci_msix_shutdown(dev);
1059 free_msi_irqs(dev);
1061 EXPORT_SYMBOL(pci_disable_msix);
1063 void pci_no_msi(void)
1065 pci_msi_enable = 0;
1069 * pci_msi_enabled - is MSI enabled?
1071 * Returns true if MSI has not been disabled by the command-line option
1072 * pci=nomsi.
1074 int pci_msi_enabled(void)
1076 return pci_msi_enable;
1078 EXPORT_SYMBOL(pci_msi_enabled);
1080 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
1081 struct irq_affinity *affd)
1083 int nvec;
1084 int rc;
1086 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
1087 return -EINVAL;
1089 /* Check whether driver already requested MSI-X IRQs */
1090 if (dev->msix_enabled) {
1091 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
1092 return -EINVAL;
1095 if (maxvec < minvec)
1096 return -ERANGE;
1098 if (WARN_ON_ONCE(dev->msi_enabled))
1099 return -EINVAL;
1101 nvec = pci_msi_vec_count(dev);
1102 if (nvec < 0)
1103 return nvec;
1104 if (nvec < minvec)
1105 return -ENOSPC;
1107 if (nvec > maxvec)
1108 nvec = maxvec;
1110 for (;;) {
1111 if (affd) {
1112 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1113 if (nvec < minvec)
1114 return -ENOSPC;
1117 rc = msi_capability_init(dev, nvec, affd);
1118 if (rc == 0)
1119 return nvec;
1121 if (rc < 0)
1122 return rc;
1123 if (rc < minvec)
1124 return -ENOSPC;
1126 nvec = rc;
1130 /* deprecated, don't use */
1131 int pci_enable_msi(struct pci_dev *dev)
1133 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1134 if (rc < 0)
1135 return rc;
1136 return 0;
1138 EXPORT_SYMBOL(pci_enable_msi);
1140 static int __pci_enable_msix_range(struct pci_dev *dev,
1141 struct msix_entry *entries, int minvec,
1142 int maxvec, struct irq_affinity *affd,
1143 int flags)
1145 int rc, nvec = maxvec;
1147 if (maxvec < minvec)
1148 return -ERANGE;
1150 if (WARN_ON_ONCE(dev->msix_enabled))
1151 return -EINVAL;
1153 for (;;) {
1154 if (affd) {
1155 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1156 if (nvec < minvec)
1157 return -ENOSPC;
1160 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
1161 if (rc == 0)
1162 return nvec;
1164 if (rc < 0)
1165 return rc;
1166 if (rc < minvec)
1167 return -ENOSPC;
1169 nvec = rc;
1174 * pci_enable_msix_range - configure device's MSI-X capability structure
1175 * @dev: pointer to the pci_dev data structure of MSI-X device function
1176 * @entries: pointer to an array of MSI-X entries
1177 * @minvec: minimum number of MSI-X IRQs requested
1178 * @maxvec: maximum number of MSI-X IRQs requested
1180 * Setup the MSI-X capability structure of device function with a maximum
1181 * possible number of interrupts in the range between @minvec and @maxvec
1182 * upon its software driver call to request for MSI-X mode enabled on its
1183 * hardware device function. It returns a negative errno if an error occurs.
1184 * If it succeeds, it returns the actual number of interrupts allocated and
1185 * indicates the successful configuration of MSI-X capability structure
1186 * with new allocated MSI-X interrupts.
1188 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1189 int minvec, int maxvec)
1191 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
1193 EXPORT_SYMBOL(pci_enable_msix_range);
1196 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1197 * @dev: PCI device to operate on
1198 * @min_vecs: minimum number of vectors required (must be >= 1)
1199 * @max_vecs: maximum (desired) number of vectors
1200 * @flags: flags or quirks for the allocation
1201 * @affd: optional description of the affinity requirements
1203 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1204 * vectors if available, and fall back to a single legacy vector
1205 * if neither is available. Return the number of vectors allocated,
1206 * (which might be smaller than @max_vecs) if successful, or a negative
1207 * error code on error. If less than @min_vecs interrupt vectors are
1208 * available for @dev the function will fail with -ENOSPC.
1210 * To get the Linux IRQ number used for a vector that can be passed to
1211 * request_irq() use the pci_irq_vector() helper.
1213 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1214 unsigned int max_vecs, unsigned int flags,
1215 struct irq_affinity *affd)
1217 struct irq_affinity msi_default_affd = {0};
1218 int nvecs = -ENOSPC;
1220 if (flags & PCI_IRQ_AFFINITY) {
1221 if (!affd)
1222 affd = &msi_default_affd;
1223 } else {
1224 if (WARN_ON(affd))
1225 affd = NULL;
1228 if (flags & PCI_IRQ_MSIX) {
1229 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1230 affd, flags);
1231 if (nvecs > 0)
1232 return nvecs;
1235 if (flags & PCI_IRQ_MSI) {
1236 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1237 if (nvecs > 0)
1238 return nvecs;
1241 /* use legacy IRQ if allowed */
1242 if (flags & PCI_IRQ_LEGACY) {
1243 if (min_vecs == 1 && dev->irq) {
1245 * Invoke the affinity spreading logic to ensure that
1246 * the device driver can adjust queue configuration
1247 * for the single interrupt case.
1249 if (affd)
1250 irq_create_affinity_masks(1, affd);
1251 pci_intx(dev, 1);
1252 return 1;
1256 return nvecs;
1258 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1261 * pci_free_irq_vectors - free previously allocated IRQs for a device
1262 * @dev: PCI device to operate on
1264 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1266 void pci_free_irq_vectors(struct pci_dev *dev)
1268 pci_disable_msix(dev);
1269 pci_disable_msi(dev);
1271 EXPORT_SYMBOL(pci_free_irq_vectors);
1274 * pci_irq_vector - return Linux IRQ number of a device vector
1275 * @dev: PCI device to operate on
1276 * @nr: device-relative interrupt vector index (0-based).
1278 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1280 if (dev->msix_enabled) {
1281 struct msi_desc *entry;
1282 int i = 0;
1284 for_each_pci_msi_entry(entry, dev) {
1285 if (i == nr)
1286 return entry->irq;
1287 i++;
1289 WARN_ON_ONCE(1);
1290 return -EINVAL;
1293 if (dev->msi_enabled) {
1294 struct msi_desc *entry = first_pci_msi_entry(dev);
1296 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1297 return -EINVAL;
1298 } else {
1299 if (WARN_ON_ONCE(nr > 0))
1300 return -EINVAL;
1303 return dev->irq + nr;
1305 EXPORT_SYMBOL(pci_irq_vector);
1308 * pci_irq_get_affinity - return the affinity of a particular MSI vector
1309 * @dev: PCI device to operate on
1310 * @nr: device-relative interrupt vector index (0-based).
1312 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1314 if (dev->msix_enabled) {
1315 struct msi_desc *entry;
1316 int i = 0;
1318 for_each_pci_msi_entry(entry, dev) {
1319 if (i == nr)
1320 return &entry->affinity->mask;
1321 i++;
1323 WARN_ON_ONCE(1);
1324 return NULL;
1325 } else if (dev->msi_enabled) {
1326 struct msi_desc *entry = first_pci_msi_entry(dev);
1328 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1329 nr >= entry->nvec_used))
1330 return NULL;
1332 return &entry->affinity[nr].mask;
1333 } else {
1334 return cpu_possible_mask;
1337 EXPORT_SYMBOL(pci_irq_get_affinity);
1339 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1341 return to_pci_dev(desc->dev);
1343 EXPORT_SYMBOL(msi_desc_to_pci_dev);
1345 void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1347 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1349 return dev->bus->sysdata;
1351 EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1353 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1355 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1356 * @irq_data: Pointer to interrupt data of the MSI interrupt
1357 * @msg: Pointer to the message
1359 void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1361 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
1364 * For MSI-X desc->irq is always equal to irq_data->irq. For
1365 * MSI only the first interrupt of MULTI MSI passes the test.
1367 if (desc->irq == irq_data->irq)
1368 __pci_write_msi_msg(desc, msg);
1372 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1373 * @desc: Pointer to the MSI descriptor
1375 * The ID number is only used within the irqdomain.
1377 static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
1379 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1381 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1382 pci_dev_id(dev) << 11 |
1383 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1386 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1388 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1392 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
1393 * for @dev
1394 * @domain: The interrupt domain to check
1395 * @info: The domain info for verification
1396 * @dev: The device to check
1398 * Returns:
1399 * 0 if the functionality is supported
1400 * 1 if Multi MSI is requested, but the domain does not support it
1401 * -ENOTSUPP otherwise
1403 int pci_msi_domain_check_cap(struct irq_domain *domain,
1404 struct msi_domain_info *info, struct device *dev)
1406 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1408 /* Special handling to support __pci_enable_msi_range() */
1409 if (pci_msi_desc_is_multi_msi(desc) &&
1410 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1411 return 1;
1412 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1413 return -ENOTSUPP;
1415 return 0;
1418 static int pci_msi_domain_handle_error(struct irq_domain *domain,
1419 struct msi_desc *desc, int error)
1421 /* Special handling to support __pci_enable_msi_range() */
1422 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1423 return 1;
1425 return error;
1428 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1429 struct msi_desc *desc)
1431 arg->desc = desc;
1432 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
1435 static struct msi_domain_ops pci_msi_domain_ops_default = {
1436 .set_desc = pci_msi_domain_set_desc,
1437 .msi_check = pci_msi_domain_check_cap,
1438 .handle_error = pci_msi_domain_handle_error,
1441 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1443 struct msi_domain_ops *ops = info->ops;
1445 if (ops == NULL) {
1446 info->ops = &pci_msi_domain_ops_default;
1447 } else {
1448 if (ops->set_desc == NULL)
1449 ops->set_desc = pci_msi_domain_set_desc;
1450 if (ops->msi_check == NULL)
1451 ops->msi_check = pci_msi_domain_check_cap;
1452 if (ops->handle_error == NULL)
1453 ops->handle_error = pci_msi_domain_handle_error;
1457 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1459 struct irq_chip *chip = info->chip;
1461 BUG_ON(!chip);
1462 if (!chip->irq_write_msi_msg)
1463 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1464 if (!chip->irq_mask)
1465 chip->irq_mask = pci_msi_mask_irq;
1466 if (!chip->irq_unmask)
1467 chip->irq_unmask = pci_msi_unmask_irq;
1471 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1472 * @fwnode: Optional fwnode of the interrupt controller
1473 * @info: MSI domain info
1474 * @parent: Parent irq domain
1476 * Updates the domain and chip ops and creates a MSI interrupt domain.
1478 * Returns:
1479 * A domain pointer or NULL in case of failure.
1481 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
1482 struct msi_domain_info *info,
1483 struct irq_domain *parent)
1485 struct irq_domain *domain;
1487 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1488 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1490 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1491 pci_msi_domain_update_dom_ops(info);
1492 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1493 pci_msi_domain_update_chip_ops(info);
1495 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
1496 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1497 info->flags |= MSI_FLAG_MUST_REACTIVATE;
1499 /* PCI-MSI is oneshot-safe */
1500 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1502 domain = msi_create_irq_domain(fwnode, info, parent);
1503 if (!domain)
1504 return NULL;
1506 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
1507 return domain;
1509 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
1512 * Users of the generic MSI infrastructure expect a device to have a single ID,
1513 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1514 * DMA phantom functions tend to still emit MSIs from the real function number,
1515 * so we ignore those and only consider topological aliases where either the
1516 * alias device or RID appears on a different bus number. We also make the
1517 * reasonable assumption that bridges are walked in an upstream direction (so
1518 * the last one seen wins), and the much braver assumption that the most likely
1519 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1520 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1521 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1522 * for taking ownership all we can really do is close our eyes and hope...
1524 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1526 u32 *pa = data;
1527 u8 bus = PCI_BUS_NUM(*pa);
1529 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1530 *pa = alias;
1532 return 0;
1536 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1537 * @domain: The interrupt domain
1538 * @pdev: The PCI device.
1540 * The RID for a device is formed from the alias, with a firmware
1541 * supplied mapping applied
1543 * Returns: The RID.
1545 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1547 struct device_node *of_node;
1548 u32 rid = pci_dev_id(pdev);
1550 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1552 of_node = irq_domain_get_of_node(domain);
1553 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
1554 iort_msi_map_id(&pdev->dev, rid);
1556 return rid;
1560 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1561 * @pdev: The PCI device
1563 * Use the firmware data to find a device-specific MSI domain
1564 * (i.e. not one that is set as a default).
1566 * Returns: The corresponding MSI domain or NULL if none has been found.
1568 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1570 struct irq_domain *dom;
1571 u32 rid = pci_dev_id(pdev);
1573 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1574 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
1575 if (!dom)
1576 dom = iort_get_device_domain(&pdev->dev, rid,
1577 DOMAIN_BUS_PCI_MSI);
1578 return dom;
1582 * pci_dev_has_special_msi_domain - Check whether the device is handled by
1583 * a non-standard PCI-MSI domain
1584 * @pdev: The PCI device to check.
1586 * Returns: True if the device irqdomain or the bus irqdomain is
1587 * non-standard PCI/MSI.
1589 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
1591 struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
1593 if (!dom)
1594 dom = dev_get_msi_domain(&pdev->bus->dev);
1596 if (!dom)
1597 return true;
1599 return dom->bus_token != DOMAIN_BUS_PCI_MSI;
1602 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
1603 #endif /* CONFIG_PCI_MSI */
1605 void pci_msi_init(struct pci_dev *dev)
1607 u16 ctrl;
1610 * Disable the MSI hardware to avoid screaming interrupts
1611 * during boot. This is the power on reset default so
1612 * usually this should be a noop.
1614 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1615 if (!dev->msi_cap)
1616 return;
1618 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
1619 if (ctrl & PCI_MSI_FLAGS_ENABLE)
1620 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS,
1621 ctrl & ~PCI_MSI_FLAGS_ENABLE);
1623 if (!(ctrl & PCI_MSI_FLAGS_64BIT))
1624 dev->no_64bit_msi = 1;
1627 void pci_msix_init(struct pci_dev *dev)
1629 u16 ctrl;
1631 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1632 if (!dev->msix_cap)
1633 return;
1635 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
1636 if (ctrl & PCI_MSIX_FLAGS_ENABLE)
1637 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS,
1638 ctrl & ~PCI_MSIX_FLAGS_ENABLE);