3 * Purpose: 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.
10 #include <linux/err.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>
22 #include <linux/acpi_iort.h>
23 #include <linux/slab.h>
24 #include <linux/irqdomain.h>
25 #include <linux/of_irq.h>
29 static int pci_msi_enable = 1;
30 int pci_msi_ignore_mask;
32 #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
34 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
35 static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
37 struct irq_domain *domain;
39 domain = dev_get_msi_domain(&dev->dev);
40 if (domain && irq_domain_is_hierarchy(domain))
41 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
43 return arch_setup_msi_irqs(dev, nvec, type);
46 static void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
48 struct irq_domain *domain;
50 domain = dev_get_msi_domain(&dev->dev);
51 if (domain && irq_domain_is_hierarchy(domain))
52 msi_domain_free_irqs(domain, &dev->dev);
54 arch_teardown_msi_irqs(dev);
57 #define pci_msi_setup_msi_irqs arch_setup_msi_irqs
58 #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
63 int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
65 struct msi_controller *chip = dev->bus->msi;
68 if (!chip || !chip->setup_irq)
71 err = chip->setup_irq(chip, dev, desc);
75 irq_set_chip_data(desc->irq, chip);
80 void __weak arch_teardown_msi_irq(unsigned int irq)
82 struct msi_controller *chip = irq_get_chip_data(irq);
84 if (!chip || !chip->teardown_irq)
87 chip->teardown_irq(chip, irq);
90 int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
92 struct msi_controller *chip = dev->bus->msi;
93 struct msi_desc *entry;
96 if (chip && chip->setup_irqs)
97 return chip->setup_irqs(chip, dev, nvec, type);
99 * If an architecture wants to support multiple MSI, it needs to
100 * override arch_setup_msi_irqs()
102 if (type == PCI_CAP_ID_MSI && nvec > 1)
105 for_each_pci_msi_entry(entry, dev) {
106 ret = arch_setup_msi_irq(dev, entry);
117 * We have a default implementation available as a separate non-weak
118 * function, as it is used by the Xen x86 PCI code
120 void default_teardown_msi_irqs(struct pci_dev *dev)
123 struct msi_desc *entry;
125 for_each_pci_msi_entry(entry, dev)
127 for (i = 0; i < entry->nvec_used; i++)
128 arch_teardown_msi_irq(entry->irq + i);
131 void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
133 return default_teardown_msi_irqs(dev);
136 static void default_restore_msi_irq(struct pci_dev *dev, int irq)
138 struct msi_desc *entry;
141 if (dev->msix_enabled) {
142 for_each_pci_msi_entry(entry, dev) {
143 if (irq == entry->irq)
146 } else if (dev->msi_enabled) {
147 entry = irq_get_msi_desc(irq);
151 __pci_write_msi_msg(entry, &entry->msg);
154 void __weak arch_restore_msi_irqs(struct pci_dev *dev)
156 return default_restore_msi_irqs(dev);
159 static inline __attribute_const__ u32 msi_mask(unsigned x)
161 /* Don't shift by >= width of type */
164 return (1 << (1 << x)) - 1;
168 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
169 * mask all MSI interrupts by clearing the MSI enable bit does not work
170 * reliably as devices without an INTx disable bit will then generate a
171 * level IRQ which will never be cleared.
173 void __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
175 raw_spinlock_t *lock = &desc->dev->msi_lock;
178 if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
181 raw_spin_lock_irqsave(lock, flags);
182 desc->masked &= ~mask;
183 desc->masked |= flag;
184 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
186 raw_spin_unlock_irqrestore(lock, flags);
189 static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
191 __pci_msi_desc_mask_irq(desc, mask, flag);
194 static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
196 return desc->mask_base +
197 desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
201 * This internal function does not flush PCI writes to the device.
202 * All users must ensure that they read from the device before either
203 * assuming that the device state is up to date, or returning out of this
204 * file. This saves a few milliseconds when initialising devices with lots
205 * of MSI-X interrupts.
207 u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag)
209 u32 mask_bits = desc->masked;
211 if (pci_msi_ignore_mask)
214 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
215 if (flag & PCI_MSIX_ENTRY_CTRL_MASKBIT)
216 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
217 writel(mask_bits, pci_msix_desc_addr(desc) + PCI_MSIX_ENTRY_VECTOR_CTRL);
222 static void msix_mask_irq(struct msi_desc *desc, u32 flag)
224 desc->masked = __pci_msix_desc_mask_irq(desc, flag);
227 static void msi_set_mask_bit(struct irq_data *data, u32 flag)
229 struct msi_desc *desc = irq_data_get_msi_desc(data);
231 if (desc->msi_attrib.is_msix) {
232 msix_mask_irq(desc, flag);
233 readl(desc->mask_base); /* Flush write to device */
235 unsigned offset = data->irq - desc->irq;
236 msi_mask_irq(desc, 1 << offset, flag << offset);
241 * pci_msi_mask_irq - Generic irq chip callback to mask PCI/MSI interrupts
242 * @data: pointer to irqdata associated to that interrupt
244 void pci_msi_mask_irq(struct irq_data *data)
246 msi_set_mask_bit(data, 1);
248 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
251 * pci_msi_unmask_irq - Generic irq chip callback to unmask PCI/MSI interrupts
252 * @data: pointer to irqdata associated to that interrupt
254 void pci_msi_unmask_irq(struct irq_data *data)
256 msi_set_mask_bit(data, 0);
258 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
260 void default_restore_msi_irqs(struct pci_dev *dev)
262 struct msi_desc *entry;
264 for_each_pci_msi_entry(entry, dev)
265 default_restore_msi_irq(dev, entry->irq);
268 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
270 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
272 BUG_ON(dev->current_state != PCI_D0);
274 if (entry->msi_attrib.is_msix) {
275 void __iomem *base = pci_msix_desc_addr(entry);
277 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
278 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
279 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
281 int pos = dev->msi_cap;
284 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
286 if (entry->msi_attrib.is_64) {
287 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
289 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
292 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
298 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
300 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
302 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
303 /* Don't touch the hardware now */
304 } else if (entry->msi_attrib.is_msix) {
305 void __iomem *base = pci_msix_desc_addr(entry);
306 bool unmasked = !(entry->masked & PCI_MSIX_ENTRY_CTRL_MASKBIT);
309 * The specification mandates that the entry is masked
310 * when the message is modified:
312 * "If software changes the Address or Data value of an
313 * entry while the entry is unmasked, the result is
317 __pci_msix_desc_mask_irq(entry, PCI_MSIX_ENTRY_CTRL_MASKBIT);
319 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
320 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
321 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
324 __pci_msix_desc_mask_irq(entry, 0);
326 /* Ensure that the writes are visible in the device */
327 readl(base + PCI_MSIX_ENTRY_DATA);
329 int pos = dev->msi_cap;
332 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
333 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
334 msgctl |= entry->msi_attrib.multiple << 4;
335 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
337 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
339 if (entry->msi_attrib.is_64) {
340 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
342 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
345 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
348 /* Ensure that the writes are visible in the device */
349 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
354 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
356 struct msi_desc *entry = irq_get_msi_desc(irq);
358 __pci_write_msi_msg(entry, msg);
360 EXPORT_SYMBOL_GPL(pci_write_msi_msg);
362 static void free_msi_irqs(struct pci_dev *dev)
364 struct list_head *msi_list = dev_to_msi_list(&dev->dev);
365 struct msi_desc *entry, *tmp;
366 struct attribute **msi_attrs;
367 struct device_attribute *dev_attr;
370 for_each_pci_msi_entry(entry, dev)
372 for (i = 0; i < entry->nvec_used; i++)
373 BUG_ON(irq_has_action(entry->irq + i));
375 if (dev->msi_irq_groups) {
376 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
377 msi_attrs = dev->msi_irq_groups[0]->attrs;
378 while (msi_attrs[count]) {
379 dev_attr = container_of(msi_attrs[count],
380 struct device_attribute, attr);
381 kfree(dev_attr->attr.name);
386 kfree(dev->msi_irq_groups[0]);
387 kfree(dev->msi_irq_groups);
388 dev->msi_irq_groups = NULL;
391 pci_msi_teardown_msi_irqs(dev);
393 list_for_each_entry_safe(entry, tmp, msi_list, list) {
394 if (entry->msi_attrib.is_msix) {
395 if (list_is_last(&entry->list, msi_list))
396 iounmap(entry->mask_base);
399 list_del(&entry->list);
400 free_msi_entry(entry);
404 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
406 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
407 pci_intx(dev, enable);
410 static void __pci_restore_msi_state(struct pci_dev *dev)
413 struct msi_desc *entry;
415 if (!dev->msi_enabled)
418 entry = irq_get_msi_desc(dev->irq);
420 pci_intx_for_msi(dev, 0);
421 pci_msi_set_enable(dev, 0);
422 arch_restore_msi_irqs(dev);
424 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
425 msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
427 control &= ~PCI_MSI_FLAGS_QSIZE;
428 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
429 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
432 static void __pci_restore_msix_state(struct pci_dev *dev)
434 struct msi_desc *entry;
436 if (!dev->msix_enabled)
438 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
440 /* route the table */
441 pci_intx_for_msi(dev, 0);
442 pci_msix_clear_and_set_ctrl(dev, 0,
443 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
445 arch_restore_msi_irqs(dev);
446 for_each_pci_msi_entry(entry, dev)
447 msix_mask_irq(entry, entry->masked);
449 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
452 void pci_restore_msi_state(struct pci_dev *dev)
454 __pci_restore_msi_state(dev);
455 __pci_restore_msix_state(dev);
457 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
459 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
462 struct msi_desc *entry;
466 retval = kstrtoul(attr->attr.name, 10, &irq);
470 entry = irq_get_msi_desc(irq);
472 return sprintf(buf, "%s\n",
473 entry->msi_attrib.is_msix ? "msix" : "msi");
478 static int populate_msi_sysfs(struct pci_dev *pdev)
480 struct attribute **msi_attrs;
481 struct attribute *msi_attr;
482 struct device_attribute *msi_dev_attr;
483 struct attribute_group *msi_irq_group;
484 const struct attribute_group **msi_irq_groups;
485 struct msi_desc *entry;
491 /* Determine how many msi entries we have */
492 for_each_pci_msi_entry(entry, pdev)
493 num_msi += entry->nvec_used;
497 /* Dynamically create the MSI attributes for the PCI device */
498 msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
501 for_each_pci_msi_entry(entry, pdev) {
502 for (i = 0; i < entry->nvec_used; i++) {
503 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
506 msi_attrs[count] = &msi_dev_attr->attr;
508 sysfs_attr_init(&msi_dev_attr->attr);
509 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
511 if (!msi_dev_attr->attr.name)
513 msi_dev_attr->attr.mode = S_IRUGO;
514 msi_dev_attr->show = msi_mode_show;
519 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
522 msi_irq_group->name = "msi_irqs";
523 msi_irq_group->attrs = msi_attrs;
525 msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
527 goto error_irq_group;
528 msi_irq_groups[0] = msi_irq_group;
530 ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
532 goto error_irq_groups;
533 pdev->msi_irq_groups = msi_irq_groups;
538 kfree(msi_irq_groups);
540 kfree(msi_irq_group);
543 msi_attr = msi_attrs[count];
545 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
546 kfree(msi_attr->name);
549 msi_attr = msi_attrs[count];
555 static struct msi_desc *
556 msi_setup_entry(struct pci_dev *dev, int nvec, const struct irq_affinity *affd)
558 struct cpumask *masks = NULL;
559 struct msi_desc *entry;
563 masks = irq_create_affinity_masks(nvec, affd);
566 /* MSI Entry Initialization */
567 entry = alloc_msi_entry(&dev->dev, nvec, masks);
571 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
573 entry->msi_attrib.is_msix = 0;
574 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
575 entry->msi_attrib.entry_nr = 0;
576 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
577 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
578 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
579 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
581 if (control & PCI_MSI_FLAGS_64BIT)
582 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
584 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
586 /* Save the initial mask status */
587 if (entry->msi_attrib.maskbit)
588 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
595 static int msi_verify_entries(struct pci_dev *dev)
597 struct msi_desc *entry;
599 for_each_pci_msi_entry(entry, dev) {
600 if (!dev->no_64bit_msi || !entry->msg.address_hi)
602 dev_err(&dev->dev, "Device has broken 64-bit MSI but arch"
603 " tried to assign one above 4G\n");
610 * msi_capability_init - configure device's MSI capability structure
611 * @dev: pointer to the pci_dev data structure of MSI device function
612 * @nvec: number of interrupts to allocate
613 * @affd: description of automatic irq affinity assignments (may be %NULL)
615 * Setup the MSI capability structure of the device with the requested
616 * number of interrupts. A return value of zero indicates the successful
617 * setup of an entry with the new MSI irq. A negative return value indicates
618 * an error, and a positive return value indicates the number of interrupts
619 * which could have been allocated.
621 static int msi_capability_init(struct pci_dev *dev, int nvec,
622 const struct irq_affinity *affd)
624 struct msi_desc *entry;
628 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
630 entry = msi_setup_entry(dev, nvec, affd);
634 /* All MSIs are unmasked by default, Mask them all */
635 mask = msi_mask(entry->msi_attrib.multi_cap);
636 msi_mask_irq(entry, mask, mask);
638 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
640 /* Configure MSI capability structure */
641 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
643 msi_mask_irq(entry, mask, 0);
648 ret = msi_verify_entries(dev);
650 msi_mask_irq(entry, mask, 0);
655 ret = populate_msi_sysfs(dev);
657 msi_mask_irq(entry, mask, 0);
662 /* Set MSI enabled bits */
663 pci_intx_for_msi(dev, 0);
664 pci_msi_set_enable(dev, 1);
665 dev->msi_enabled = 1;
667 pcibios_free_irq(dev);
668 dev->irq = entry->irq;
672 static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
674 resource_size_t phys_addr;
679 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
681 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
682 flags = pci_resource_flags(dev, bir);
683 if (!flags || (flags & IORESOURCE_UNSET))
686 table_offset &= PCI_MSIX_TABLE_OFFSET;
687 phys_addr = pci_resource_start(dev, bir) + table_offset;
689 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
692 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
693 struct msix_entry *entries, int nvec,
694 const struct irq_affinity *affd)
696 struct cpumask *curmsk, *masks = NULL;
697 struct msi_desc *entry;
702 masks = irq_create_affinity_masks(nvec, affd);
704 for (i = 0, curmsk = masks; i < nvec; i++) {
705 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
711 /* No enough memory. Don't try again */
716 entry->msi_attrib.is_msix = 1;
717 entry->msi_attrib.is_64 = 1;
720 entry->msi_attrib.entry_nr = entries[i].entry;
722 entry->msi_attrib.entry_nr = i;
723 entry->msi_attrib.default_irq = dev->irq;
724 entry->mask_base = base;
726 addr = pci_msix_desc_addr(entry);
728 entry->masked = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
730 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
740 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
742 struct msi_desc *entry;
744 for_each_pci_msi_entry(entry, dev) {
746 entries->vector = entry->irq;
752 static void msix_mask_all(void __iomem *base, int tsize)
754 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
757 if (pci_msi_ignore_mask)
760 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
761 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
765 * msix_capability_init - configure device's MSI-X capability
766 * @dev: pointer to the pci_dev data structure of MSI-X device function
767 * @entries: pointer to an array of struct msix_entry entries
768 * @nvec: number of @entries
769 * @affd: Optional pointer to enable automatic affinity assignement
771 * Setup the MSI-X capability structure of device function with a
772 * single MSI-X irq. A return of zero indicates the successful setup of
773 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
775 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
776 int nvec, const struct irq_affinity *affd)
783 * Some devices require MSI-X to be enabled before the MSI-X
784 * registers can be accessed. Mask all the vectors to prevent
785 * interrupts coming in before they're fully set up.
787 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
788 PCI_MSIX_FLAGS_ENABLE);
790 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
791 /* Request & Map MSI-X table region */
792 tsize = msix_table_size(control);
793 base = msix_map_region(dev, tsize);
799 ret = msix_setup_entries(dev, base, entries, nvec, affd);
803 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
807 /* Check if all MSI entries honor device restrictions */
808 ret = msi_verify_entries(dev);
812 msix_update_entries(dev, entries);
814 ret = populate_msi_sysfs(dev);
818 /* Set MSI-X enabled bits and unmask the function */
819 pci_intx_for_msi(dev, 0);
820 dev->msix_enabled = 1;
823 * Ensure that all table entries are masked to prevent
824 * stale entries from firing in a crash kernel.
826 * Done late to deal with a broken Marvell NVME device
827 * which takes the MSI-X mask bits into account even
828 * when MSI-X is disabled, which prevents MSI delivery.
830 msix_mask_all(base, tsize);
831 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
833 pcibios_free_irq(dev);
839 * If we had some success, report the number of irqs
840 * we succeeded in setting up.
842 struct msi_desc *entry;
845 for_each_pci_msi_entry(entry, dev) {
857 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
863 * pci_msi_supported - check whether MSI may be enabled on a device
864 * @dev: pointer to the pci_dev data structure of MSI device function
865 * @nvec: how many MSIs have been requested ?
867 * Look at global flags, the device itself, and its parent buses
868 * to determine if MSI/-X are supported for the device. If MSI/-X is
869 * supported return 1, else return 0.
871 static int pci_msi_supported(struct pci_dev *dev, int nvec)
875 /* MSI must be globally enabled and supported by the device */
879 if (!dev || dev->no_msi || dev->current_state != PCI_D0)
883 * You can't ask to have 0 or less MSIs configured.
885 * b) the list manipulation code assumes nvec >= 1.
891 * Any bridge which does NOT route MSI transactions from its
892 * secondary bus to its primary bus must set NO_MSI flag on
893 * the secondary pci_bus.
894 * We expect only arch-specific PCI host bus controller driver
895 * or quirks for specific PCI bridges to be setting NO_MSI.
897 for (bus = dev->bus; bus; bus = bus->parent)
898 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
905 * pci_msi_vec_count - Return the number of MSI vectors a device can send
906 * @dev: device to report about
908 * This function returns the number of MSI vectors a device requested via
909 * Multiple Message Capable register. It returns a negative errno if the
910 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
911 * and returns a power of two, up to a maximum of 2^5 (32), according to the
914 int pci_msi_vec_count(struct pci_dev *dev)
922 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
923 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
927 EXPORT_SYMBOL(pci_msi_vec_count);
929 static void pci_msi_shutdown(struct pci_dev *dev)
931 struct msi_desc *desc;
934 if (!pci_msi_enable || !dev || !dev->msi_enabled)
937 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
938 desc = first_pci_msi_entry(dev);
940 pci_msi_set_enable(dev, 0);
941 pci_intx_for_msi(dev, 1);
942 dev->msi_enabled = 0;
944 /* Return the device with MSI unmasked as initial states */
945 mask = msi_mask(desc->msi_attrib.multi_cap);
946 msi_mask_irq(desc, mask, 0);
948 /* Restore dev->irq to its default pin-assertion irq */
949 dev->irq = desc->msi_attrib.default_irq;
950 pcibios_alloc_irq(dev);
953 void pci_disable_msi(struct pci_dev *dev)
955 if (!pci_msi_enable || !dev || !dev->msi_enabled)
958 pci_msi_shutdown(dev);
961 EXPORT_SYMBOL(pci_disable_msi);
964 * pci_msix_vec_count - return the number of device's MSI-X table entries
965 * @dev: pointer to the pci_dev data structure of MSI-X device function
966 * This function returns the number of device's MSI-X table entries and
967 * therefore the number of MSI-X vectors device is capable of sending.
968 * It returns a negative errno if the device is not capable of sending MSI-X
971 int pci_msix_vec_count(struct pci_dev *dev)
978 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
979 return msix_table_size(control);
981 EXPORT_SYMBOL(pci_msix_vec_count);
983 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
984 int nvec, const struct irq_affinity *affd)
989 if (!pci_msi_supported(dev, nvec))
992 nr_entries = pci_msix_vec_count(dev);
995 if (nvec > nr_entries)
999 /* Check for any invalid entries */
1000 for (i = 0; i < nvec; i++) {
1001 if (entries[i].entry >= nr_entries)
1002 return -EINVAL; /* invalid entry */
1003 for (j = i + 1; j < nvec; j++) {
1004 if (entries[i].entry == entries[j].entry)
1005 return -EINVAL; /* duplicate entry */
1010 /* Check whether driver already requested for MSI irq */
1011 if (dev->msi_enabled) {
1012 dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
1015 return msix_capability_init(dev, entries, nvec, affd);
1018 static void pci_msix_shutdown(struct pci_dev *dev)
1020 struct msi_desc *entry;
1022 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1025 if (pci_dev_is_disconnected(dev)) {
1026 dev->msix_enabled = 0;
1030 /* Return the device with MSI-X masked as initial states */
1031 for_each_pci_msi_entry(entry, dev)
1032 __pci_msix_desc_mask_irq(entry, 1);
1034 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1035 pci_intx_for_msi(dev, 1);
1036 dev->msix_enabled = 0;
1037 pcibios_alloc_irq(dev);
1040 void pci_disable_msix(struct pci_dev *dev)
1042 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1045 pci_msix_shutdown(dev);
1048 EXPORT_SYMBOL(pci_disable_msix);
1050 void pci_no_msi(void)
1056 * pci_msi_enabled - is MSI enabled?
1058 * Returns true if MSI has not been disabled by the command-line option
1061 int pci_msi_enabled(void)
1063 return pci_msi_enable;
1065 EXPORT_SYMBOL(pci_msi_enabled);
1067 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
1068 const struct irq_affinity *affd)
1073 if (!pci_msi_supported(dev, minvec))
1076 /* Check whether driver already requested MSI-X irqs */
1077 if (dev->msix_enabled) {
1079 "can't enable MSI (MSI-X already enabled)\n");
1083 if (maxvec < minvec)
1086 if (WARN_ON_ONCE(dev->msi_enabled))
1089 nvec = pci_msi_vec_count(dev);
1100 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1105 rc = msi_capability_init(dev, nvec, affd);
1118 /* deprecated, don't use */
1119 int pci_enable_msi(struct pci_dev *dev)
1121 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1126 EXPORT_SYMBOL(pci_enable_msi);
1128 static int __pci_enable_msix_range(struct pci_dev *dev,
1129 struct msix_entry *entries, int minvec,
1130 int maxvec, const struct irq_affinity *affd)
1132 int rc, nvec = maxvec;
1134 if (maxvec < minvec)
1137 if (WARN_ON_ONCE(dev->msix_enabled))
1142 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1147 rc = __pci_enable_msix(dev, entries, nvec, affd);
1161 * pci_enable_msix_range - configure device's MSI-X capability structure
1162 * @dev: pointer to the pci_dev data structure of MSI-X device function
1163 * @entries: pointer to an array of MSI-X entries
1164 * @minvec: minimum number of MSI-X irqs requested
1165 * @maxvec: maximum number of MSI-X irqs requested
1167 * Setup the MSI-X capability structure of device function with a maximum
1168 * possible number of interrupts in the range between @minvec and @maxvec
1169 * upon its software driver call to request for MSI-X mode enabled on its
1170 * hardware device function. It returns a negative errno if an error occurs.
1171 * If it succeeds, it returns the actual number of interrupts allocated and
1172 * indicates the successful configuration of MSI-X capability structure
1173 * with new allocated MSI-X interrupts.
1175 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1176 int minvec, int maxvec)
1178 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
1180 EXPORT_SYMBOL(pci_enable_msix_range);
1183 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1184 * @dev: PCI device to operate on
1185 * @min_vecs: minimum number of vectors required (must be >= 1)
1186 * @max_vecs: maximum (desired) number of vectors
1187 * @flags: flags or quirks for the allocation
1188 * @affd: optional description of the affinity requirements
1190 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1191 * vectors if available, and fall back to a single legacy vector
1192 * if neither is available. Return the number of vectors allocated,
1193 * (which might be smaller than @max_vecs) if successful, or a negative
1194 * error code on error. If less than @min_vecs interrupt vectors are
1195 * available for @dev the function will fail with -ENOSPC.
1197 * To get the Linux IRQ number used for a vector that can be passed to
1198 * request_irq() use the pci_irq_vector() helper.
1200 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1201 unsigned int max_vecs, unsigned int flags,
1202 const struct irq_affinity *affd)
1204 static const struct irq_affinity msi_default_affd;
1205 int msix_vecs = -ENOSPC;
1206 int msi_vecs = -ENOSPC;
1208 if (flags & PCI_IRQ_AFFINITY) {
1210 affd = &msi_default_affd;
1216 if (flags & PCI_IRQ_MSIX) {
1217 msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs,
1223 if (flags & PCI_IRQ_MSI) {
1224 msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs,
1230 /* use legacy irq if allowed */
1231 if (flags & PCI_IRQ_LEGACY) {
1232 if (min_vecs == 1 && dev->irq) {
1238 if (msix_vecs == -ENOSPC)
1242 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1245 * pci_free_irq_vectors - free previously allocated IRQs for a device
1246 * @dev: PCI device to operate on
1248 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1250 void pci_free_irq_vectors(struct pci_dev *dev)
1252 pci_disable_msix(dev);
1253 pci_disable_msi(dev);
1255 EXPORT_SYMBOL(pci_free_irq_vectors);
1258 * pci_irq_vector - return Linux IRQ number of a device vector
1259 * @dev: PCI device to operate on
1260 * @nr: device-relative interrupt vector index (0-based).
1262 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1264 if (dev->msix_enabled) {
1265 struct msi_desc *entry;
1268 for_each_pci_msi_entry(entry, dev) {
1277 if (dev->msi_enabled) {
1278 struct msi_desc *entry = first_pci_msi_entry(dev);
1280 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1283 if (WARN_ON_ONCE(nr > 0))
1287 return dev->irq + nr;
1289 EXPORT_SYMBOL(pci_irq_vector);
1292 * pci_irq_get_affinity - return the affinity of a particular msi vector
1293 * @dev: PCI device to operate on
1294 * @nr: device-relative interrupt vector index (0-based).
1296 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1298 if (dev->msix_enabled) {
1299 struct msi_desc *entry;
1302 for_each_pci_msi_entry(entry, dev) {
1304 return entry->affinity;
1309 } else if (dev->msi_enabled) {
1310 struct msi_desc *entry = first_pci_msi_entry(dev);
1312 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1313 nr >= entry->nvec_used))
1316 return &entry->affinity[nr];
1318 return cpu_possible_mask;
1321 EXPORT_SYMBOL(pci_irq_get_affinity);
1324 * pci_irq_get_node - return the numa node of a particular msi vector
1325 * @pdev: PCI device to operate on
1326 * @vec: device-relative interrupt vector index (0-based).
1328 int pci_irq_get_node(struct pci_dev *pdev, int vec)
1330 const struct cpumask *mask;
1332 mask = pci_irq_get_affinity(pdev, vec);
1334 return local_memory_node(cpu_to_node(cpumask_first(mask)));
1335 return dev_to_node(&pdev->dev);
1337 EXPORT_SYMBOL(pci_irq_get_node);
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 * @dev: Pointer to the PCI device
1374 * @desc: Pointer to the msi descriptor
1376 * The ID number is only used within the irqdomain.
1378 irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
1379 struct msi_desc *desc)
1381 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1382 PCI_DEVID(dev->bus->number, dev->devfn) << 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 for @dev
1393 * @domain: The interrupt domain to check
1394 * @info: The domain info for verification
1395 * @dev: The device to check
1398 * 0 if the functionality is supported
1399 * 1 if Multi MSI is requested, but the domain does not support it
1400 * -ENOTSUPP otherwise
1402 int pci_msi_domain_check_cap(struct irq_domain *domain,
1403 struct msi_domain_info *info, struct device *dev)
1405 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1407 /* Special handling to support __pci_enable_msi_range() */
1408 if (pci_msi_desc_is_multi_msi(desc) &&
1409 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1411 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1417 static int pci_msi_domain_handle_error(struct irq_domain *domain,
1418 struct msi_desc *desc, int error)
1420 /* Special handling to support __pci_enable_msi_range() */
1421 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1427 #ifdef GENERIC_MSI_DOMAIN_OPS
1428 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1429 struct msi_desc *desc)
1432 arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
1436 #define pci_msi_domain_set_desc NULL
1439 static struct msi_domain_ops pci_msi_domain_ops_default = {
1440 .set_desc = pci_msi_domain_set_desc,
1441 .msi_check = pci_msi_domain_check_cap,
1442 .handle_error = pci_msi_domain_handle_error,
1445 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1447 struct msi_domain_ops *ops = info->ops;
1450 info->ops = &pci_msi_domain_ops_default;
1452 if (ops->set_desc == NULL)
1453 ops->set_desc = pci_msi_domain_set_desc;
1454 if (ops->msi_check == NULL)
1455 ops->msi_check = pci_msi_domain_check_cap;
1456 if (ops->handle_error == NULL)
1457 ops->handle_error = pci_msi_domain_handle_error;
1461 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1463 struct irq_chip *chip = info->chip;
1466 if (!chip->irq_write_msi_msg)
1467 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1468 if (!chip->irq_mask)
1469 chip->irq_mask = pci_msi_mask_irq;
1470 if (!chip->irq_unmask)
1471 chip->irq_unmask = pci_msi_unmask_irq;
1475 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1476 * @fwnode: Optional fwnode of the interrupt controller
1477 * @info: MSI domain info
1478 * @parent: Parent irq domain
1480 * Updates the domain and chip ops and creates a MSI interrupt domain.
1483 * A domain pointer or NULL in case of failure.
1485 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
1486 struct msi_domain_info *info,
1487 struct irq_domain *parent)
1489 struct irq_domain *domain;
1491 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1492 pci_msi_domain_update_dom_ops(info);
1493 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1494 pci_msi_domain_update_chip_ops(info);
1496 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
1498 domain = msi_create_irq_domain(fwnode, info, parent);
1502 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
1505 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
1508 * Users of the generic MSI infrastructure expect a device to have a single ID,
1509 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1510 * DMA phantom functions tend to still emit MSIs from the real function number,
1511 * so we ignore those and only consider topological aliases where either the
1512 * alias device or RID appears on a different bus number. We also make the
1513 * reasonable assumption that bridges are walked in an upstream direction (so
1514 * the last one seen wins), and the much braver assumption that the most likely
1515 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1516 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1517 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1518 * for taking ownership all we can really do is close our eyes and hope...
1520 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1523 u8 bus = PCI_BUS_NUM(*pa);
1525 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1532 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1533 * @domain: The interrupt domain
1534 * @pdev: The PCI device.
1536 * The RID for a device is formed from the alias, with a firmware
1537 * supplied mapping applied
1541 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1543 struct device_node *of_node;
1544 u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
1546 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1548 of_node = irq_domain_get_of_node(domain);
1549 rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
1550 iort_msi_map_rid(&pdev->dev, rid);
1556 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1557 * @pdev: The PCI device
1559 * Use the firmware data to find a device-specific MSI domain
1560 * (i.e. not one that is set as a default).
1562 * Returns: The corresponding MSI domain or NULL if none has been found.
1564 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1566 struct irq_domain *dom;
1567 u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
1569 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1570 dom = of_msi_map_get_device_domain(&pdev->dev, rid);
1572 dom = iort_get_device_domain(&pdev->dev, rid);
1575 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */