1 // SPDX-License-Identifier: GPL-2.0
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.
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>
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);
56 arch_teardown_msi_irqs(dev);
59 #define pci_msi_setup_msi_irqs arch_setup_msi_irqs
60 #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
63 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
65 int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
70 void __weak arch_teardown_msi_irq(unsigned int irq)
74 int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
76 struct msi_desc *entry;
80 * If an architecture wants to support multiple MSI, it needs to
81 * override arch_setup_msi_irqs()
83 if (type == PCI_CAP_ID_MSI && nvec > 1)
86 for_each_pci_msi_entry(entry, dev) {
87 ret = arch_setup_msi_irq(dev, entry);
97 void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
100 struct msi_desc *entry;
102 for_each_pci_msi_entry(entry, dev)
104 for (i = 0; i < entry->nvec_used; i++)
105 arch_teardown_msi_irq(entry->irq + i);
107 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
109 static void default_restore_msi_irq(struct pci_dev *dev, int irq)
111 struct msi_desc *entry;
114 if (dev->msix_enabled) {
115 for_each_pci_msi_entry(entry, dev) {
116 if (irq == entry->irq)
119 } else if (dev->msi_enabled) {
120 entry = irq_get_msi_desc(irq);
124 __pci_write_msi_msg(entry, &entry->msg);
127 void __weak arch_restore_msi_irqs(struct pci_dev *dev)
129 return default_restore_msi_irqs(dev);
133 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
134 * mask all MSI interrupts by clearing the MSI enable bit does not work
135 * reliably as devices without an INTx disable bit will then generate a
136 * level IRQ which will never be cleared.
138 static inline __attribute_const__ u32 msi_multi_mask(struct msi_desc *desc)
140 /* Don't shift by >= width of type */
141 if (desc->msi_attrib.multi_cap >= 5)
143 return (1 << (1 << desc->msi_attrib.multi_cap)) - 1;
146 static noinline void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set)
148 raw_spinlock_t *lock = &desc->dev->msi_lock;
151 raw_spin_lock_irqsave(lock, flags);
152 desc->msi_mask &= ~clear;
153 desc->msi_mask |= set;
154 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
156 raw_spin_unlock_irqrestore(lock, flags);
159 static inline void pci_msi_mask(struct msi_desc *desc, u32 mask)
161 pci_msi_update_mask(desc, 0, mask);
164 static inline void pci_msi_unmask(struct msi_desc *desc, u32 mask)
166 pci_msi_update_mask(desc, mask, 0);
169 static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
171 return desc->mask_base + desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
175 * This internal function does not flush PCI writes to the device. All
176 * users must ensure that they read from the device before either assuming
177 * that the device state is up to date, or returning out of this file.
178 * It does not affect the msi_desc::msix_ctrl cache either. Use with care!
180 static void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl)
182 void __iomem *desc_addr = pci_msix_desc_addr(desc);
184 writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
187 static inline void pci_msix_mask(struct msi_desc *desc)
189 desc->msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
190 pci_msix_write_vector_ctrl(desc, desc->msix_ctrl);
191 /* Flush write to device */
192 readl(desc->mask_base);
195 static inline void pci_msix_unmask(struct msi_desc *desc)
197 desc->msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
198 pci_msix_write_vector_ctrl(desc, desc->msix_ctrl);
201 static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask)
203 if (pci_msi_ignore_mask || desc->msi_attrib.is_virtual)
206 if (desc->msi_attrib.is_msix)
208 else if (desc->msi_attrib.maskbit)
209 pci_msi_mask(desc, mask);
212 static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask)
214 if (pci_msi_ignore_mask || desc->msi_attrib.is_virtual)
217 if (desc->msi_attrib.is_msix)
218 pci_msix_unmask(desc);
219 else if (desc->msi_attrib.maskbit)
220 pci_msi_unmask(desc, mask);
224 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
225 * @data: pointer to irqdata associated to that interrupt
227 void pci_msi_mask_irq(struct irq_data *data)
229 struct msi_desc *desc = irq_data_get_msi_desc(data);
231 __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
233 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
236 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
237 * @data: pointer to irqdata associated to that interrupt
239 void pci_msi_unmask_irq(struct irq_data *data)
241 struct msi_desc *desc = irq_data_get_msi_desc(data);
243 __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
245 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
247 void default_restore_msi_irqs(struct pci_dev *dev)
249 struct msi_desc *entry;
251 for_each_pci_msi_entry(entry, dev)
252 default_restore_msi_irq(dev, entry->irq);
255 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
257 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
259 BUG_ON(dev->current_state != PCI_D0);
261 if (entry->msi_attrib.is_msix) {
262 void __iomem *base = pci_msix_desc_addr(entry);
264 if (WARN_ON_ONCE(entry->msi_attrib.is_virtual))
267 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
268 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
269 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
271 int pos = dev->msi_cap;
274 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
276 if (entry->msi_attrib.is_64) {
277 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
279 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
282 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
288 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
290 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
292 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
293 /* Don't touch the hardware now */
294 } else if (entry->msi_attrib.is_msix) {
295 void __iomem *base = pci_msix_desc_addr(entry);
296 u32 ctrl = entry->msix_ctrl;
297 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
299 if (entry->msi_attrib.is_virtual)
303 * The specification mandates that the entry is masked
304 * when the message is modified:
306 * "If software changes the Address or Data value of an
307 * entry while the entry is unmasked, the result is
311 pci_msix_write_vector_ctrl(entry, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
313 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
314 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
315 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
318 pci_msix_write_vector_ctrl(entry, ctrl);
320 /* Ensure that the writes are visible in the device */
321 readl(base + PCI_MSIX_ENTRY_DATA);
323 int pos = dev->msi_cap;
326 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
327 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
328 msgctl |= entry->msi_attrib.multiple << 4;
329 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
331 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
333 if (entry->msi_attrib.is_64) {
334 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
336 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
339 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
342 /* Ensure that the writes are visible in the device */
343 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
349 if (entry->write_msi_msg)
350 entry->write_msi_msg(entry, entry->write_msi_msg_data);
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;
368 for_each_pci_msi_entry(entry, dev)
370 for (i = 0; i < entry->nvec_used; i++)
371 BUG_ON(irq_has_action(entry->irq + i));
373 pci_msi_teardown_msi_irqs(dev);
375 list_for_each_entry_safe(entry, tmp, msi_list, list) {
376 if (entry->msi_attrib.is_msix) {
377 if (list_is_last(&entry->list, msi_list))
378 iounmap(entry->mask_base);
381 list_del(&entry->list);
382 free_msi_entry(entry);
385 if (dev->msi_irq_groups) {
386 msi_destroy_sysfs(&dev->dev, dev->msi_irq_groups);
387 dev->msi_irq_groups = NULL;
391 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
393 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
394 pci_intx(dev, enable);
397 static void pci_msi_set_enable(struct pci_dev *dev, int enable)
401 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
402 control &= ~PCI_MSI_FLAGS_ENABLE;
404 control |= PCI_MSI_FLAGS_ENABLE;
405 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
408 static void __pci_restore_msi_state(struct pci_dev *dev)
411 struct msi_desc *entry;
413 if (!dev->msi_enabled)
416 entry = irq_get_msi_desc(dev->irq);
418 pci_intx_for_msi(dev, 0);
419 pci_msi_set_enable(dev, 0);
420 arch_restore_msi_irqs(dev);
422 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
423 pci_msi_update_mask(entry, 0, 0);
424 control &= ~PCI_MSI_FLAGS_QSIZE;
425 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
426 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
429 static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
433 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
436 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
439 static void __pci_restore_msix_state(struct pci_dev *dev)
441 struct msi_desc *entry;
443 if (!dev->msix_enabled)
445 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
447 /* route the table */
448 pci_intx_for_msi(dev, 0);
449 pci_msix_clear_and_set_ctrl(dev, 0,
450 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
452 arch_restore_msi_irqs(dev);
453 for_each_pci_msi_entry(entry, dev)
454 pci_msix_write_vector_ctrl(entry, entry->msix_ctrl);
456 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
459 void pci_restore_msi_state(struct pci_dev *dev)
461 __pci_restore_msi_state(dev);
462 __pci_restore_msix_state(dev);
464 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
466 static struct msi_desc *
467 msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
469 struct irq_affinity_desc *masks = NULL;
470 struct msi_desc *entry;
474 masks = irq_create_affinity_masks(nvec, affd);
476 /* MSI Entry Initialization */
477 entry = alloc_msi_entry(&dev->dev, nvec, masks);
481 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
483 entry->msi_attrib.is_msix = 0;
484 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
485 entry->msi_attrib.is_virtual = 0;
486 entry->msi_attrib.entry_nr = 0;
487 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
488 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
489 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
490 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
492 if (control & PCI_MSI_FLAGS_64BIT)
493 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
495 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
497 /* Save the initial mask status */
498 if (entry->msi_attrib.maskbit)
499 pci_read_config_dword(dev, entry->mask_pos, &entry->msi_mask);
506 static int msi_verify_entries(struct pci_dev *dev)
508 struct msi_desc *entry;
510 if (!dev->no_64bit_msi)
513 for_each_pci_msi_entry(entry, dev) {
514 if (entry->msg.address_hi) {
515 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
516 entry->msg.address_hi, entry->msg.address_lo);
524 * msi_capability_init - configure device's MSI capability structure
525 * @dev: pointer to the pci_dev data structure of MSI device function
526 * @nvec: number of interrupts to allocate
527 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
529 * Setup the MSI capability structure of the device with the requested
530 * number of interrupts. A return value of zero indicates the successful
531 * setup of an entry with the new MSI IRQ. A negative return value indicates
532 * an error, and a positive return value indicates the number of interrupts
533 * which could have been allocated.
535 static int msi_capability_init(struct pci_dev *dev, int nvec,
536 struct irq_affinity *affd)
538 const struct attribute_group **groups;
539 struct msi_desc *entry;
542 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
544 entry = msi_setup_entry(dev, nvec, affd);
548 /* All MSIs are unmasked by default; mask them all */
549 pci_msi_mask(entry, msi_multi_mask(entry));
551 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
553 /* Configure MSI capability structure */
554 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
558 ret = msi_verify_entries(dev);
562 groups = msi_populate_sysfs(&dev->dev);
563 if (IS_ERR(groups)) {
564 ret = PTR_ERR(groups);
568 dev->msi_irq_groups = groups;
570 /* Set MSI enabled bits */
571 pci_intx_for_msi(dev, 0);
572 pci_msi_set_enable(dev, 1);
573 dev->msi_enabled = 1;
575 pcibios_free_irq(dev);
576 dev->irq = entry->irq;
580 pci_msi_unmask(entry, msi_multi_mask(entry));
585 static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
587 resource_size_t phys_addr;
592 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
594 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
595 flags = pci_resource_flags(dev, bir);
596 if (!flags || (flags & IORESOURCE_UNSET))
599 table_offset &= PCI_MSIX_TABLE_OFFSET;
600 phys_addr = pci_resource_start(dev, bir) + table_offset;
602 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
605 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
606 struct msix_entry *entries, int nvec,
607 struct irq_affinity *affd)
609 struct irq_affinity_desc *curmsk, *masks = NULL;
610 struct msi_desc *entry;
613 int vec_count = pci_msix_vec_count(dev);
616 masks = irq_create_affinity_masks(nvec, affd);
618 for (i = 0, curmsk = masks; i < nvec; i++) {
619 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
625 /* No enough memory. Don't try again */
630 entry->msi_attrib.is_msix = 1;
631 entry->msi_attrib.is_64 = 1;
634 entry->msi_attrib.entry_nr = entries[i].entry;
636 entry->msi_attrib.entry_nr = i;
638 entry->msi_attrib.is_virtual =
639 entry->msi_attrib.entry_nr >= vec_count;
641 entry->msi_attrib.default_irq = dev->irq;
642 entry->mask_base = base;
644 if (!entry->msi_attrib.is_virtual) {
645 addr = pci_msix_desc_addr(entry);
646 entry->msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
649 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
659 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
661 struct msi_desc *entry;
663 for_each_pci_msi_entry(entry, dev) {
665 entries->vector = entry->irq;
671 static void msix_mask_all(void __iomem *base, int tsize)
673 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
676 if (pci_msi_ignore_mask)
679 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
680 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
684 * msix_capability_init - configure device's MSI-X capability
685 * @dev: pointer to the pci_dev data structure of MSI-X device function
686 * @entries: pointer to an array of struct msix_entry entries
687 * @nvec: number of @entries
688 * @affd: Optional pointer to enable automatic affinity assignment
690 * Setup the MSI-X capability structure of device function with a
691 * single MSI-X IRQ. A return of zero indicates the successful setup of
692 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
694 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
695 int nvec, struct irq_affinity *affd)
697 const struct attribute_group **groups;
703 * Some devices require MSI-X to be enabled before the MSI-X
704 * registers can be accessed. Mask all the vectors to prevent
705 * interrupts coming in before they're fully set up.
707 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
708 PCI_MSIX_FLAGS_ENABLE);
710 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
711 /* Request & Map MSI-X table region */
712 tsize = msix_table_size(control);
713 base = msix_map_region(dev, tsize);
719 /* Ensure that all table entries are masked. */
720 msix_mask_all(base, tsize);
722 ret = msix_setup_entries(dev, base, entries, nvec, affd);
726 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
730 /* Check if all MSI entries honor device restrictions */
731 ret = msi_verify_entries(dev);
735 msix_update_entries(dev, entries);
737 groups = msi_populate_sysfs(&dev->dev);
738 if (IS_ERR(groups)) {
739 ret = PTR_ERR(groups);
743 dev->msi_irq_groups = groups;
745 /* Set MSI-X enabled bits and unmask the function */
746 pci_intx_for_msi(dev, 0);
747 dev->msix_enabled = 1;
748 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
750 pcibios_free_irq(dev);
756 * If we had some success, report the number of IRQs
757 * we succeeded in setting up.
759 struct msi_desc *entry;
762 for_each_pci_msi_entry(entry, dev) {
774 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
780 * pci_msi_supported - check whether MSI may be enabled on a device
781 * @dev: pointer to the pci_dev data structure of MSI device function
782 * @nvec: how many MSIs have been requested?
784 * Look at global flags, the device itself, and its parent buses
785 * to determine if MSI/-X are supported for the device. If MSI/-X is
786 * supported return 1, else return 0.
788 static int pci_msi_supported(struct pci_dev *dev, int nvec)
792 /* MSI must be globally enabled and supported by the device */
796 if (!dev || dev->no_msi)
800 * You can't ask to have 0 or less MSIs configured.
802 * b) the list manipulation code assumes nvec >= 1.
808 * Any bridge which does NOT route MSI transactions from its
809 * secondary bus to its primary bus must set NO_MSI flag on
810 * the secondary pci_bus.
812 * The NO_MSI flag can either be set directly by:
813 * - arch-specific PCI host bus controller drivers (deprecated)
814 * - quirks for specific PCI bridges
816 * or indirectly by platform-specific PCI host bridge drivers by
817 * advertising the 'msi_domain' property, which results in
818 * the NO_MSI flag when no MSI domain is found for this bridge
821 for (bus = dev->bus; bus; bus = bus->parent)
822 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
829 * pci_msi_vec_count - Return the number of MSI vectors a device can send
830 * @dev: device to report about
832 * This function returns the number of MSI vectors a device requested via
833 * Multiple Message Capable register. It returns a negative errno if the
834 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
835 * and returns a power of two, up to a maximum of 2^5 (32), according to the
838 int pci_msi_vec_count(struct pci_dev *dev)
846 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
847 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
851 EXPORT_SYMBOL(pci_msi_vec_count);
853 static void pci_msi_shutdown(struct pci_dev *dev)
855 struct msi_desc *desc;
857 if (!pci_msi_enable || !dev || !dev->msi_enabled)
860 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
861 desc = first_pci_msi_entry(dev);
863 pci_msi_set_enable(dev, 0);
864 pci_intx_for_msi(dev, 1);
865 dev->msi_enabled = 0;
867 /* Return the device with MSI unmasked as initial states */
868 pci_msi_unmask(desc, msi_multi_mask(desc));
870 /* Restore dev->irq to its default pin-assertion IRQ */
871 dev->irq = desc->msi_attrib.default_irq;
872 pcibios_alloc_irq(dev);
875 void pci_disable_msi(struct pci_dev *dev)
877 if (!pci_msi_enable || !dev || !dev->msi_enabled)
880 pci_msi_shutdown(dev);
883 EXPORT_SYMBOL(pci_disable_msi);
886 * pci_msix_vec_count - return the number of device's MSI-X table entries
887 * @dev: pointer to the pci_dev data structure of MSI-X device function
888 * This function returns the number of device's MSI-X table entries and
889 * therefore the number of MSI-X vectors device is capable of sending.
890 * It returns a negative errno if the device is not capable of sending MSI-X
893 int pci_msix_vec_count(struct pci_dev *dev)
900 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
901 return msix_table_size(control);
903 EXPORT_SYMBOL(pci_msix_vec_count);
905 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
906 int nvec, struct irq_affinity *affd, int flags)
911 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
914 nr_entries = pci_msix_vec_count(dev);
917 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
921 /* Check for any invalid entries */
922 for (i = 0; i < nvec; i++) {
923 if (entries[i].entry >= nr_entries)
924 return -EINVAL; /* invalid entry */
925 for (j = i + 1; j < nvec; j++) {
926 if (entries[i].entry == entries[j].entry)
927 return -EINVAL; /* duplicate entry */
932 /* Check whether driver already requested for MSI IRQ */
933 if (dev->msi_enabled) {
934 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
937 return msix_capability_init(dev, entries, nvec, affd);
940 static void pci_msix_shutdown(struct pci_dev *dev)
942 struct msi_desc *entry;
944 if (!pci_msi_enable || !dev || !dev->msix_enabled)
947 if (pci_dev_is_disconnected(dev)) {
948 dev->msix_enabled = 0;
952 /* Return the device with MSI-X masked as initial states */
953 for_each_pci_msi_entry(entry, dev)
954 pci_msix_mask(entry);
956 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
957 pci_intx_for_msi(dev, 1);
958 dev->msix_enabled = 0;
959 pcibios_alloc_irq(dev);
962 void pci_disable_msix(struct pci_dev *dev)
964 if (!pci_msi_enable || !dev || !dev->msix_enabled)
967 pci_msix_shutdown(dev);
970 EXPORT_SYMBOL(pci_disable_msix);
972 void pci_no_msi(void)
978 * pci_msi_enabled - is MSI enabled?
980 * Returns true if MSI has not been disabled by the command-line option
983 int pci_msi_enabled(void)
985 return pci_msi_enable;
987 EXPORT_SYMBOL(pci_msi_enabled);
989 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
990 struct irq_affinity *affd)
995 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
998 /* Check whether driver already requested MSI-X IRQs */
999 if (dev->msix_enabled) {
1000 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
1004 if (maxvec < minvec)
1007 if (WARN_ON_ONCE(dev->msi_enabled))
1010 nvec = pci_msi_vec_count(dev);
1021 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1026 rc = msi_capability_init(dev, nvec, affd);
1039 /* deprecated, don't use */
1040 int pci_enable_msi(struct pci_dev *dev)
1042 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1047 EXPORT_SYMBOL(pci_enable_msi);
1049 static int __pci_enable_msix_range(struct pci_dev *dev,
1050 struct msix_entry *entries, int minvec,
1051 int maxvec, struct irq_affinity *affd,
1054 int rc, nvec = maxvec;
1056 if (maxvec < minvec)
1059 if (WARN_ON_ONCE(dev->msix_enabled))
1064 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1069 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
1083 * pci_enable_msix_range - configure device's MSI-X capability structure
1084 * @dev: pointer to the pci_dev data structure of MSI-X device function
1085 * @entries: pointer to an array of MSI-X entries
1086 * @minvec: minimum number of MSI-X IRQs requested
1087 * @maxvec: maximum number of MSI-X IRQs requested
1089 * Setup the MSI-X capability structure of device function with a maximum
1090 * possible number of interrupts in the range between @minvec and @maxvec
1091 * upon its software driver call to request for MSI-X mode enabled on its
1092 * hardware device function. It returns a negative errno if an error occurs.
1093 * If it succeeds, it returns the actual number of interrupts allocated and
1094 * indicates the successful configuration of MSI-X capability structure
1095 * with new allocated MSI-X interrupts.
1097 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1098 int minvec, int maxvec)
1100 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
1102 EXPORT_SYMBOL(pci_enable_msix_range);
1105 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1106 * @dev: PCI device to operate on
1107 * @min_vecs: minimum number of vectors required (must be >= 1)
1108 * @max_vecs: maximum (desired) number of vectors
1109 * @flags: flags or quirks for the allocation
1110 * @affd: optional description of the affinity requirements
1112 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1113 * vectors if available, and fall back to a single legacy vector
1114 * if neither is available. Return the number of vectors allocated,
1115 * (which might be smaller than @max_vecs) if successful, or a negative
1116 * error code on error. If less than @min_vecs interrupt vectors are
1117 * available for @dev the function will fail with -ENOSPC.
1119 * To get the Linux IRQ number used for a vector that can be passed to
1120 * request_irq() use the pci_irq_vector() helper.
1122 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1123 unsigned int max_vecs, unsigned int flags,
1124 struct irq_affinity *affd)
1126 struct irq_affinity msi_default_affd = {0};
1127 int nvecs = -ENOSPC;
1129 if (flags & PCI_IRQ_AFFINITY) {
1131 affd = &msi_default_affd;
1137 if (flags & PCI_IRQ_MSIX) {
1138 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1144 if (flags & PCI_IRQ_MSI) {
1145 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1150 /* use legacy IRQ if allowed */
1151 if (flags & PCI_IRQ_LEGACY) {
1152 if (min_vecs == 1 && dev->irq) {
1154 * Invoke the affinity spreading logic to ensure that
1155 * the device driver can adjust queue configuration
1156 * for the single interrupt case.
1159 irq_create_affinity_masks(1, affd);
1167 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1170 * pci_free_irq_vectors - free previously allocated IRQs for a device
1171 * @dev: PCI device to operate on
1173 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1175 void pci_free_irq_vectors(struct pci_dev *dev)
1177 pci_disable_msix(dev);
1178 pci_disable_msi(dev);
1180 EXPORT_SYMBOL(pci_free_irq_vectors);
1183 * pci_irq_vector - return Linux IRQ number of a device vector
1184 * @dev: PCI device to operate on
1185 * @nr: device-relative interrupt vector index (0-based).
1187 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1189 if (dev->msix_enabled) {
1190 struct msi_desc *entry;
1193 for_each_pci_msi_entry(entry, dev) {
1202 if (dev->msi_enabled) {
1203 struct msi_desc *entry = first_pci_msi_entry(dev);
1205 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1208 if (WARN_ON_ONCE(nr > 0))
1212 return dev->irq + nr;
1214 EXPORT_SYMBOL(pci_irq_vector);
1217 * pci_irq_get_affinity - return the affinity of a particular MSI vector
1218 * @dev: PCI device to operate on
1219 * @nr: device-relative interrupt vector index (0-based).
1221 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1223 if (dev->msix_enabled) {
1224 struct msi_desc *entry;
1227 for_each_pci_msi_entry(entry, dev) {
1229 return &entry->affinity->mask;
1234 } else if (dev->msi_enabled) {
1235 struct msi_desc *entry = first_pci_msi_entry(dev);
1237 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1238 nr >= entry->nvec_used))
1241 return &entry->affinity[nr].mask;
1243 return cpu_possible_mask;
1246 EXPORT_SYMBOL(pci_irq_get_affinity);
1248 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1250 return to_pci_dev(desc->dev);
1252 EXPORT_SYMBOL(msi_desc_to_pci_dev);
1254 void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1256 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1258 return dev->bus->sysdata;
1260 EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1262 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1264 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1265 * @irq_data: Pointer to interrupt data of the MSI interrupt
1266 * @msg: Pointer to the message
1268 void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1270 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
1273 * For MSI-X desc->irq is always equal to irq_data->irq. For
1274 * MSI only the first interrupt of MULTI MSI passes the test.
1276 if (desc->irq == irq_data->irq)
1277 __pci_write_msi_msg(desc, msg);
1281 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1282 * @desc: Pointer to the MSI descriptor
1284 * The ID number is only used within the irqdomain.
1286 static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
1288 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1290 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1291 pci_dev_id(dev) << 11 |
1292 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1295 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1297 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1301 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
1303 * @domain: The interrupt domain to check
1304 * @info: The domain info for verification
1305 * @dev: The device to check
1308 * 0 if the functionality is supported
1309 * 1 if Multi MSI is requested, but the domain does not support it
1310 * -ENOTSUPP otherwise
1312 int pci_msi_domain_check_cap(struct irq_domain *domain,
1313 struct msi_domain_info *info, struct device *dev)
1315 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1317 /* Special handling to support __pci_enable_msi_range() */
1318 if (pci_msi_desc_is_multi_msi(desc) &&
1319 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1321 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1327 static int pci_msi_domain_handle_error(struct irq_domain *domain,
1328 struct msi_desc *desc, int error)
1330 /* Special handling to support __pci_enable_msi_range() */
1331 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1337 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1338 struct msi_desc *desc)
1341 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
1344 static struct msi_domain_ops pci_msi_domain_ops_default = {
1345 .set_desc = pci_msi_domain_set_desc,
1346 .msi_check = pci_msi_domain_check_cap,
1347 .handle_error = pci_msi_domain_handle_error,
1350 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1352 struct msi_domain_ops *ops = info->ops;
1355 info->ops = &pci_msi_domain_ops_default;
1357 if (ops->set_desc == NULL)
1358 ops->set_desc = pci_msi_domain_set_desc;
1359 if (ops->msi_check == NULL)
1360 ops->msi_check = pci_msi_domain_check_cap;
1361 if (ops->handle_error == NULL)
1362 ops->handle_error = pci_msi_domain_handle_error;
1366 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1368 struct irq_chip *chip = info->chip;
1371 if (!chip->irq_write_msi_msg)
1372 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1373 if (!chip->irq_mask)
1374 chip->irq_mask = pci_msi_mask_irq;
1375 if (!chip->irq_unmask)
1376 chip->irq_unmask = pci_msi_unmask_irq;
1380 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1381 * @fwnode: Optional fwnode of the interrupt controller
1382 * @info: MSI domain info
1383 * @parent: Parent irq domain
1385 * Updates the domain and chip ops and creates a MSI interrupt domain.
1388 * A domain pointer or NULL in case of failure.
1390 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
1391 struct msi_domain_info *info,
1392 struct irq_domain *parent)
1394 struct irq_domain *domain;
1396 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1397 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1399 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1400 pci_msi_domain_update_dom_ops(info);
1401 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1402 pci_msi_domain_update_chip_ops(info);
1404 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
1405 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1406 info->flags |= MSI_FLAG_MUST_REACTIVATE;
1408 /* PCI-MSI is oneshot-safe */
1409 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1411 domain = msi_create_irq_domain(fwnode, info, parent);
1415 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
1418 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
1421 * Users of the generic MSI infrastructure expect a device to have a single ID,
1422 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1423 * DMA phantom functions tend to still emit MSIs from the real function number,
1424 * so we ignore those and only consider topological aliases where either the
1425 * alias device or RID appears on a different bus number. We also make the
1426 * reasonable assumption that bridges are walked in an upstream direction (so
1427 * the last one seen wins), and the much braver assumption that the most likely
1428 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1429 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1430 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1431 * for taking ownership all we can really do is close our eyes and hope...
1433 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1436 u8 bus = PCI_BUS_NUM(*pa);
1438 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1445 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1446 * @domain: The interrupt domain
1447 * @pdev: The PCI device.
1449 * The RID for a device is formed from the alias, with a firmware
1450 * supplied mapping applied
1454 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1456 struct device_node *of_node;
1457 u32 rid = pci_dev_id(pdev);
1459 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1461 of_node = irq_domain_get_of_node(domain);
1462 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
1463 iort_msi_map_id(&pdev->dev, rid);
1469 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1470 * @pdev: The PCI device
1472 * Use the firmware data to find a device-specific MSI domain
1473 * (i.e. not one that is set as a default).
1475 * Returns: The corresponding MSI domain or NULL if none has been found.
1477 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1479 struct irq_domain *dom;
1480 u32 rid = pci_dev_id(pdev);
1482 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1483 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
1485 dom = iort_get_device_domain(&pdev->dev, rid,
1486 DOMAIN_BUS_PCI_MSI);
1491 * pci_dev_has_special_msi_domain - Check whether the device is handled by
1492 * a non-standard PCI-MSI domain
1493 * @pdev: The PCI device to check.
1495 * Returns: True if the device irqdomain or the bus irqdomain is
1496 * non-standard PCI/MSI.
1498 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
1500 struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
1503 dom = dev_get_msi_domain(&pdev->bus->dev);
1508 return dom->bus_token != DOMAIN_BUS_PCI_MSI;
1511 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
1512 #endif /* CONFIG_PCI_MSI */
1514 void pci_msi_init(struct pci_dev *dev)
1519 * Disable the MSI hardware to avoid screaming interrupts
1520 * during boot. This is the power on reset default so
1521 * usually this should be a noop.
1523 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1527 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
1528 if (ctrl & PCI_MSI_FLAGS_ENABLE)
1529 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS,
1530 ctrl & ~PCI_MSI_FLAGS_ENABLE);
1532 if (!(ctrl & PCI_MSI_FLAGS_64BIT))
1533 dev->no_64bit_msi = 1;
1536 void pci_msix_init(struct pci_dev *dev)
1540 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1544 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
1545 if (ctrl & PCI_MSIX_FLAGS_ENABLE)
1546 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS,
1547 ctrl & ~PCI_MSIX_FLAGS_ENABLE);