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,
586 unsigned int nr_entries)
588 resource_size_t phys_addr;
593 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
595 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
596 flags = pci_resource_flags(dev, bir);
597 if (!flags || (flags & IORESOURCE_UNSET))
600 table_offset &= PCI_MSIX_TABLE_OFFSET;
601 phys_addr = pci_resource_start(dev, bir) + table_offset;
603 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
606 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
607 struct msix_entry *entries, int nvec,
608 struct irq_affinity *affd)
610 struct irq_affinity_desc *curmsk, *masks = NULL;
611 struct msi_desc *entry;
614 int vec_count = pci_msix_vec_count(dev);
617 masks = irq_create_affinity_masks(nvec, affd);
619 for (i = 0, curmsk = masks; i < nvec; i++) {
620 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
626 /* No enough memory. Don't try again */
631 entry->msi_attrib.is_msix = 1;
632 entry->msi_attrib.is_64 = 1;
635 entry->msi_attrib.entry_nr = entries[i].entry;
637 entry->msi_attrib.entry_nr = i;
639 entry->msi_attrib.is_virtual =
640 entry->msi_attrib.entry_nr >= vec_count;
642 entry->msi_attrib.default_irq = dev->irq;
643 entry->mask_base = base;
645 if (!entry->msi_attrib.is_virtual) {
646 addr = pci_msix_desc_addr(entry);
647 entry->msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
650 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
660 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
662 struct msi_desc *entry;
664 for_each_pci_msi_entry(entry, dev) {
666 entries->vector = entry->irq;
672 static void msix_mask_all(void __iomem *base, int tsize)
674 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
677 if (pci_msi_ignore_mask)
680 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
681 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
685 * msix_capability_init - configure device's MSI-X capability
686 * @dev: pointer to the pci_dev data structure of MSI-X device function
687 * @entries: pointer to an array of struct msix_entry entries
688 * @nvec: number of @entries
689 * @affd: Optional pointer to enable automatic affinity assignment
691 * Setup the MSI-X capability structure of device function with a
692 * single MSI-X IRQ. A return of zero indicates the successful setup of
693 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
695 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
696 int nvec, struct irq_affinity *affd)
698 const struct attribute_group **groups;
704 * Some devices require MSI-X to be enabled before the MSI-X
705 * registers can be accessed. Mask all the vectors to prevent
706 * interrupts coming in before they're fully set up.
708 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
709 PCI_MSIX_FLAGS_ENABLE);
711 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
712 /* Request & Map MSI-X table region */
713 tsize = msix_table_size(control);
714 base = msix_map_region(dev, tsize);
720 /* Ensure that all table entries are masked. */
721 msix_mask_all(base, tsize);
723 ret = msix_setup_entries(dev, base, entries, nvec, affd);
727 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
731 /* Check if all MSI entries honor device restrictions */
732 ret = msi_verify_entries(dev);
736 msix_update_entries(dev, entries);
738 groups = msi_populate_sysfs(&dev->dev);
739 if (IS_ERR(groups)) {
740 ret = PTR_ERR(groups);
744 dev->msi_irq_groups = groups;
746 /* Set MSI-X enabled bits and unmask the function */
747 pci_intx_for_msi(dev, 0);
748 dev->msix_enabled = 1;
749 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
751 pcibios_free_irq(dev);
757 * If we had some success, report the number of IRQs
758 * we succeeded in setting up.
760 struct msi_desc *entry;
763 for_each_pci_msi_entry(entry, dev) {
775 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
781 * pci_msi_supported - check whether MSI may be enabled on a device
782 * @dev: pointer to the pci_dev data structure of MSI device function
783 * @nvec: how many MSIs have been requested?
785 * Look at global flags, the device itself, and its parent buses
786 * to determine if MSI/-X are supported for the device. If MSI/-X is
787 * supported return 1, else return 0.
789 static int pci_msi_supported(struct pci_dev *dev, int nvec)
793 /* MSI must be globally enabled and supported by the device */
797 if (!dev || dev->no_msi)
801 * You can't ask to have 0 or less MSIs configured.
803 * b) the list manipulation code assumes nvec >= 1.
809 * Any bridge which does NOT route MSI transactions from its
810 * secondary bus to its primary bus must set NO_MSI flag on
811 * the secondary pci_bus.
813 * The NO_MSI flag can either be set directly by:
814 * - arch-specific PCI host bus controller drivers (deprecated)
815 * - quirks for specific PCI bridges
817 * or indirectly by platform-specific PCI host bridge drivers by
818 * advertising the 'msi_domain' property, which results in
819 * the NO_MSI flag when no MSI domain is found for this bridge
822 for (bus = dev->bus; bus; bus = bus->parent)
823 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
830 * pci_msi_vec_count - Return the number of MSI vectors a device can send
831 * @dev: device to report about
833 * This function returns the number of MSI vectors a device requested via
834 * Multiple Message Capable register. It returns a negative errno if the
835 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
836 * and returns a power of two, up to a maximum of 2^5 (32), according to the
839 int pci_msi_vec_count(struct pci_dev *dev)
847 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
848 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
852 EXPORT_SYMBOL(pci_msi_vec_count);
854 static void pci_msi_shutdown(struct pci_dev *dev)
856 struct msi_desc *desc;
858 if (!pci_msi_enable || !dev || !dev->msi_enabled)
861 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
862 desc = first_pci_msi_entry(dev);
864 pci_msi_set_enable(dev, 0);
865 pci_intx_for_msi(dev, 1);
866 dev->msi_enabled = 0;
868 /* Return the device with MSI unmasked as initial states */
869 pci_msi_unmask(desc, msi_multi_mask(desc));
871 /* Restore dev->irq to its default pin-assertion IRQ */
872 dev->irq = desc->msi_attrib.default_irq;
873 pcibios_alloc_irq(dev);
876 void pci_disable_msi(struct pci_dev *dev)
878 if (!pci_msi_enable || !dev || !dev->msi_enabled)
881 pci_msi_shutdown(dev);
884 EXPORT_SYMBOL(pci_disable_msi);
887 * pci_msix_vec_count - return the number of device's MSI-X table entries
888 * @dev: pointer to the pci_dev data structure of MSI-X device function
889 * This function returns the number of device's MSI-X table entries and
890 * therefore the number of MSI-X vectors device is capable of sending.
891 * It returns a negative errno if the device is not capable of sending MSI-X
894 int pci_msix_vec_count(struct pci_dev *dev)
901 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
902 return msix_table_size(control);
904 EXPORT_SYMBOL(pci_msix_vec_count);
906 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
907 int nvec, struct irq_affinity *affd, int flags)
912 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
915 nr_entries = pci_msix_vec_count(dev);
918 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
922 /* Check for any invalid entries */
923 for (i = 0; i < nvec; i++) {
924 if (entries[i].entry >= nr_entries)
925 return -EINVAL; /* invalid entry */
926 for (j = i + 1; j < nvec; j++) {
927 if (entries[i].entry == entries[j].entry)
928 return -EINVAL; /* duplicate entry */
933 /* Check whether driver already requested for MSI IRQ */
934 if (dev->msi_enabled) {
935 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
938 return msix_capability_init(dev, entries, nvec, affd);
941 static void pci_msix_shutdown(struct pci_dev *dev)
943 struct msi_desc *entry;
945 if (!pci_msi_enable || !dev || !dev->msix_enabled)
948 if (pci_dev_is_disconnected(dev)) {
949 dev->msix_enabled = 0;
953 /* Return the device with MSI-X masked as initial states */
954 for_each_pci_msi_entry(entry, dev)
955 pci_msix_mask(entry);
957 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
958 pci_intx_for_msi(dev, 1);
959 dev->msix_enabled = 0;
960 pcibios_alloc_irq(dev);
963 void pci_disable_msix(struct pci_dev *dev)
965 if (!pci_msi_enable || !dev || !dev->msix_enabled)
968 pci_msix_shutdown(dev);
971 EXPORT_SYMBOL(pci_disable_msix);
973 void pci_no_msi(void)
979 * pci_msi_enabled - is MSI enabled?
981 * Returns true if MSI has not been disabled by the command-line option
984 int pci_msi_enabled(void)
986 return pci_msi_enable;
988 EXPORT_SYMBOL(pci_msi_enabled);
990 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
991 struct irq_affinity *affd)
996 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
999 /* Check whether driver already requested MSI-X IRQs */
1000 if (dev->msix_enabled) {
1001 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
1005 if (maxvec < minvec)
1008 if (WARN_ON_ONCE(dev->msi_enabled))
1011 nvec = pci_msi_vec_count(dev);
1022 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1027 rc = msi_capability_init(dev, nvec, affd);
1040 /* deprecated, don't use */
1041 int pci_enable_msi(struct pci_dev *dev)
1043 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1048 EXPORT_SYMBOL(pci_enable_msi);
1050 static int __pci_enable_msix_range(struct pci_dev *dev,
1051 struct msix_entry *entries, int minvec,
1052 int maxvec, struct irq_affinity *affd,
1055 int rc, nvec = maxvec;
1057 if (maxvec < minvec)
1060 if (WARN_ON_ONCE(dev->msix_enabled))
1065 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1070 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
1084 * pci_enable_msix_range - configure device's MSI-X capability structure
1085 * @dev: pointer to the pci_dev data structure of MSI-X device function
1086 * @entries: pointer to an array of MSI-X entries
1087 * @minvec: minimum number of MSI-X IRQs requested
1088 * @maxvec: maximum number of MSI-X IRQs requested
1090 * Setup the MSI-X capability structure of device function with a maximum
1091 * possible number of interrupts in the range between @minvec and @maxvec
1092 * upon its software driver call to request for MSI-X mode enabled on its
1093 * hardware device function. It returns a negative errno if an error occurs.
1094 * If it succeeds, it returns the actual number of interrupts allocated and
1095 * indicates the successful configuration of MSI-X capability structure
1096 * with new allocated MSI-X interrupts.
1098 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1099 int minvec, int maxvec)
1101 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
1103 EXPORT_SYMBOL(pci_enable_msix_range);
1106 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1107 * @dev: PCI device to operate on
1108 * @min_vecs: minimum number of vectors required (must be >= 1)
1109 * @max_vecs: maximum (desired) number of vectors
1110 * @flags: flags or quirks for the allocation
1111 * @affd: optional description of the affinity requirements
1113 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1114 * vectors if available, and fall back to a single legacy vector
1115 * if neither is available. Return the number of vectors allocated,
1116 * (which might be smaller than @max_vecs) if successful, or a negative
1117 * error code on error. If less than @min_vecs interrupt vectors are
1118 * available for @dev the function will fail with -ENOSPC.
1120 * To get the Linux IRQ number used for a vector that can be passed to
1121 * request_irq() use the pci_irq_vector() helper.
1123 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1124 unsigned int max_vecs, unsigned int flags,
1125 struct irq_affinity *affd)
1127 struct irq_affinity msi_default_affd = {0};
1128 int nvecs = -ENOSPC;
1130 if (flags & PCI_IRQ_AFFINITY) {
1132 affd = &msi_default_affd;
1138 if (flags & PCI_IRQ_MSIX) {
1139 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1145 if (flags & PCI_IRQ_MSI) {
1146 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1151 /* use legacy IRQ if allowed */
1152 if (flags & PCI_IRQ_LEGACY) {
1153 if (min_vecs == 1 && dev->irq) {
1155 * Invoke the affinity spreading logic to ensure that
1156 * the device driver can adjust queue configuration
1157 * for the single interrupt case.
1160 irq_create_affinity_masks(1, affd);
1168 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1171 * pci_free_irq_vectors - free previously allocated IRQs for a device
1172 * @dev: PCI device to operate on
1174 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1176 void pci_free_irq_vectors(struct pci_dev *dev)
1178 pci_disable_msix(dev);
1179 pci_disable_msi(dev);
1181 EXPORT_SYMBOL(pci_free_irq_vectors);
1184 * pci_irq_vector - return Linux IRQ number of a device vector
1185 * @dev: PCI device to operate on
1186 * @nr: device-relative interrupt vector index (0-based).
1188 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1190 if (dev->msix_enabled) {
1191 struct msi_desc *entry;
1194 for_each_pci_msi_entry(entry, dev) {
1203 if (dev->msi_enabled) {
1204 struct msi_desc *entry = first_pci_msi_entry(dev);
1206 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1209 if (WARN_ON_ONCE(nr > 0))
1213 return dev->irq + nr;
1215 EXPORT_SYMBOL(pci_irq_vector);
1218 * pci_irq_get_affinity - return the affinity of a particular MSI vector
1219 * @dev: PCI device to operate on
1220 * @nr: device-relative interrupt vector index (0-based).
1222 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1224 if (dev->msix_enabled) {
1225 struct msi_desc *entry;
1228 for_each_pci_msi_entry(entry, dev) {
1230 return &entry->affinity->mask;
1235 } else if (dev->msi_enabled) {
1236 struct msi_desc *entry = first_pci_msi_entry(dev);
1238 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1239 nr >= entry->nvec_used))
1242 return &entry->affinity[nr].mask;
1244 return cpu_possible_mask;
1247 EXPORT_SYMBOL(pci_irq_get_affinity);
1249 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1251 return to_pci_dev(desc->dev);
1253 EXPORT_SYMBOL(msi_desc_to_pci_dev);
1255 void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1257 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1259 return dev->bus->sysdata;
1261 EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1263 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1265 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1266 * @irq_data: Pointer to interrupt data of the MSI interrupt
1267 * @msg: Pointer to the message
1269 void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1271 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
1274 * For MSI-X desc->irq is always equal to irq_data->irq. For
1275 * MSI only the first interrupt of MULTI MSI passes the test.
1277 if (desc->irq == irq_data->irq)
1278 __pci_write_msi_msg(desc, msg);
1282 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1283 * @desc: Pointer to the MSI descriptor
1285 * The ID number is only used within the irqdomain.
1287 static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
1289 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1291 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1292 pci_dev_id(dev) << 11 |
1293 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1296 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1298 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1302 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
1304 * @domain: The interrupt domain to check
1305 * @info: The domain info for verification
1306 * @dev: The device to check
1309 * 0 if the functionality is supported
1310 * 1 if Multi MSI is requested, but the domain does not support it
1311 * -ENOTSUPP otherwise
1313 int pci_msi_domain_check_cap(struct irq_domain *domain,
1314 struct msi_domain_info *info, struct device *dev)
1316 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1318 /* Special handling to support __pci_enable_msi_range() */
1319 if (pci_msi_desc_is_multi_msi(desc) &&
1320 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1322 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1328 static int pci_msi_domain_handle_error(struct irq_domain *domain,
1329 struct msi_desc *desc, int error)
1331 /* Special handling to support __pci_enable_msi_range() */
1332 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1338 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1339 struct msi_desc *desc)
1342 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
1345 static struct msi_domain_ops pci_msi_domain_ops_default = {
1346 .set_desc = pci_msi_domain_set_desc,
1347 .msi_check = pci_msi_domain_check_cap,
1348 .handle_error = pci_msi_domain_handle_error,
1351 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1353 struct msi_domain_ops *ops = info->ops;
1356 info->ops = &pci_msi_domain_ops_default;
1358 if (ops->set_desc == NULL)
1359 ops->set_desc = pci_msi_domain_set_desc;
1360 if (ops->msi_check == NULL)
1361 ops->msi_check = pci_msi_domain_check_cap;
1362 if (ops->handle_error == NULL)
1363 ops->handle_error = pci_msi_domain_handle_error;
1367 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1369 struct irq_chip *chip = info->chip;
1372 if (!chip->irq_write_msi_msg)
1373 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1374 if (!chip->irq_mask)
1375 chip->irq_mask = pci_msi_mask_irq;
1376 if (!chip->irq_unmask)
1377 chip->irq_unmask = pci_msi_unmask_irq;
1381 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1382 * @fwnode: Optional fwnode of the interrupt controller
1383 * @info: MSI domain info
1384 * @parent: Parent irq domain
1386 * Updates the domain and chip ops and creates a MSI interrupt domain.
1389 * A domain pointer or NULL in case of failure.
1391 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
1392 struct msi_domain_info *info,
1393 struct irq_domain *parent)
1395 struct irq_domain *domain;
1397 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1398 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1400 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1401 pci_msi_domain_update_dom_ops(info);
1402 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1403 pci_msi_domain_update_chip_ops(info);
1405 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
1406 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1407 info->flags |= MSI_FLAG_MUST_REACTIVATE;
1409 /* PCI-MSI is oneshot-safe */
1410 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1412 domain = msi_create_irq_domain(fwnode, info, parent);
1416 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
1419 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
1422 * Users of the generic MSI infrastructure expect a device to have a single ID,
1423 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1424 * DMA phantom functions tend to still emit MSIs from the real function number,
1425 * so we ignore those and only consider topological aliases where either the
1426 * alias device or RID appears on a different bus number. We also make the
1427 * reasonable assumption that bridges are walked in an upstream direction (so
1428 * the last one seen wins), and the much braver assumption that the most likely
1429 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1430 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1431 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1432 * for taking ownership all we can really do is close our eyes and hope...
1434 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1437 u8 bus = PCI_BUS_NUM(*pa);
1439 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1446 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1447 * @domain: The interrupt domain
1448 * @pdev: The PCI device.
1450 * The RID for a device is formed from the alias, with a firmware
1451 * supplied mapping applied
1455 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1457 struct device_node *of_node;
1458 u32 rid = pci_dev_id(pdev);
1460 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1462 of_node = irq_domain_get_of_node(domain);
1463 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
1464 iort_msi_map_id(&pdev->dev, rid);
1470 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1471 * @pdev: The PCI device
1473 * Use the firmware data to find a device-specific MSI domain
1474 * (i.e. not one that is set as a default).
1476 * Returns: The corresponding MSI domain or NULL if none has been found.
1478 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1480 struct irq_domain *dom;
1481 u32 rid = pci_dev_id(pdev);
1483 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1484 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
1486 dom = iort_get_device_domain(&pdev->dev, rid,
1487 DOMAIN_BUS_PCI_MSI);
1492 * pci_dev_has_special_msi_domain - Check whether the device is handled by
1493 * a non-standard PCI-MSI domain
1494 * @pdev: The PCI device to check.
1496 * Returns: True if the device irqdomain or the bus irqdomain is
1497 * non-standard PCI/MSI.
1499 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
1501 struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
1504 dom = dev_get_msi_domain(&pdev->bus->dev);
1509 return dom->bus_token != DOMAIN_BUS_PCI_MSI;
1512 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
1513 #endif /* CONFIG_PCI_MSI */
1515 void pci_msi_init(struct pci_dev *dev)
1520 * Disable the MSI hardware to avoid screaming interrupts
1521 * during boot. This is the power on reset default so
1522 * usually this should be a noop.
1524 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1528 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
1529 if (ctrl & PCI_MSI_FLAGS_ENABLE)
1530 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS,
1531 ctrl & ~PCI_MSI_FLAGS_ENABLE);
1533 if (!(ctrl & PCI_MSI_FLAGS_64BIT))
1534 dev->no_64bit_msi = 1;
1537 void pci_msix_init(struct pci_dev *dev)
1541 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1545 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
1546 if (ctrl & PCI_MSIX_FLAGS_ENABLE)
1547 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS,
1548 ctrl & ~PCI_MSIX_FLAGS_ENABLE);