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/export.h>
11 #include <linux/irq.h>
16 static int pci_msi_enable = 1;
17 int pci_msi_ignore_mask;
19 static noinline void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set)
21 raw_spinlock_t *lock = &to_pci_dev(desc->dev)->msi_lock;
24 if (!desc->pci.msi_attrib.can_mask)
27 raw_spin_lock_irqsave(lock, flags);
28 desc->pci.msi_mask &= ~clear;
29 desc->pci.msi_mask |= set;
30 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->pci.mask_pos,
32 raw_spin_unlock_irqrestore(lock, flags);
35 static inline void pci_msi_mask(struct msi_desc *desc, u32 mask)
37 pci_msi_update_mask(desc, 0, mask);
40 static inline void pci_msi_unmask(struct msi_desc *desc, u32 mask)
42 pci_msi_update_mask(desc, mask, 0);
45 static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
47 return desc->pci.mask_base + desc->pci.msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
51 * This internal function does not flush PCI writes to the device. All
52 * users must ensure that they read from the device before either assuming
53 * that the device state is up to date, or returning out of this file.
54 * It does not affect the msi_desc::msix_ctrl cache either. Use with care!
56 static void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl)
58 void __iomem *desc_addr = pci_msix_desc_addr(desc);
60 if (desc->pci.msi_attrib.can_mask)
61 writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
64 static inline void pci_msix_mask(struct msi_desc *desc)
66 desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
67 pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
68 /* Flush write to device */
69 readl(desc->pci.mask_base);
72 static inline void pci_msix_unmask(struct msi_desc *desc)
74 desc->pci.msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
75 pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
78 static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask)
80 if (desc->pci.msi_attrib.is_msix)
83 pci_msi_mask(desc, mask);
86 static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask)
88 if (desc->pci.msi_attrib.is_msix)
89 pci_msix_unmask(desc);
91 pci_msi_unmask(desc, mask);
95 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
96 * @data: pointer to irqdata associated to that interrupt
98 void pci_msi_mask_irq(struct irq_data *data)
100 struct msi_desc *desc = irq_data_get_msi_desc(data);
102 __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
104 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
107 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
108 * @data: pointer to irqdata associated to that interrupt
110 void pci_msi_unmask_irq(struct irq_data *data)
112 struct msi_desc *desc = irq_data_get_msi_desc(data);
114 __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
116 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
118 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
120 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
122 BUG_ON(dev->current_state != PCI_D0);
124 if (entry->pci.msi_attrib.is_msix) {
125 void __iomem *base = pci_msix_desc_addr(entry);
127 if (WARN_ON_ONCE(entry->pci.msi_attrib.is_virtual))
130 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
131 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
132 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
134 int pos = dev->msi_cap;
137 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
139 if (entry->pci.msi_attrib.is_64) {
140 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
142 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
145 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
151 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
153 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
155 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
156 /* Don't touch the hardware now */
157 } else if (entry->pci.msi_attrib.is_msix) {
158 void __iomem *base = pci_msix_desc_addr(entry);
159 u32 ctrl = entry->pci.msix_ctrl;
160 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
162 if (entry->pci.msi_attrib.is_virtual)
166 * The specification mandates that the entry is masked
167 * when the message is modified:
169 * "If software changes the Address or Data value of an
170 * entry while the entry is unmasked, the result is
174 pci_msix_write_vector_ctrl(entry, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
176 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
177 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
178 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
181 pci_msix_write_vector_ctrl(entry, ctrl);
183 /* Ensure that the writes are visible in the device */
184 readl(base + PCI_MSIX_ENTRY_DATA);
186 int pos = dev->msi_cap;
189 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
190 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
191 msgctl |= entry->pci.msi_attrib.multiple << 4;
192 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
194 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
196 if (entry->pci.msi_attrib.is_64) {
197 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
199 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
202 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
205 /* Ensure that the writes are visible in the device */
206 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
212 if (entry->write_msi_msg)
213 entry->write_msi_msg(entry, entry->write_msi_msg_data);
217 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
219 struct msi_desc *entry = irq_get_msi_desc(irq);
221 __pci_write_msi_msg(entry, msg);
223 EXPORT_SYMBOL_GPL(pci_write_msi_msg);
225 static void free_msi_irqs(struct pci_dev *dev)
227 struct list_head *msi_list = dev_to_msi_list(&dev->dev);
228 struct msi_desc *entry, *tmp;
231 for_each_pci_msi_entry(entry, dev)
233 for (i = 0; i < entry->nvec_used; i++)
234 BUG_ON(irq_has_action(entry->irq + i));
236 if (dev->msi_irq_groups) {
237 msi_destroy_sysfs(&dev->dev, dev->msi_irq_groups);
238 dev->msi_irq_groups = NULL;
241 pci_msi_teardown_msi_irqs(dev);
243 list_for_each_entry_safe(entry, tmp, msi_list, list) {
244 list_del(&entry->list);
245 free_msi_entry(entry);
248 if (dev->msix_base) {
249 iounmap(dev->msix_base);
250 dev->msix_base = NULL;
254 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
256 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
257 pci_intx(dev, enable);
260 static void pci_msi_set_enable(struct pci_dev *dev, int enable)
264 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
265 control &= ~PCI_MSI_FLAGS_ENABLE;
267 control |= PCI_MSI_FLAGS_ENABLE;
268 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
272 * Architecture override returns true when the PCI MSI message should be
273 * written by the generic restore function.
275 bool __weak arch_restore_msi_irqs(struct pci_dev *dev)
280 static void __pci_restore_msi_state(struct pci_dev *dev)
282 struct msi_desc *entry;
285 if (!dev->msi_enabled)
288 entry = irq_get_msi_desc(dev->irq);
290 pci_intx_for_msi(dev, 0);
291 pci_msi_set_enable(dev, 0);
292 if (arch_restore_msi_irqs(dev))
293 __pci_write_msi_msg(entry, &entry->msg);
295 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
296 pci_msi_update_mask(entry, 0, 0);
297 control &= ~PCI_MSI_FLAGS_QSIZE;
298 control |= (entry->pci.msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
299 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
302 static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
306 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
309 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
312 static void __pci_restore_msix_state(struct pci_dev *dev)
314 struct msi_desc *entry;
317 if (!dev->msix_enabled)
319 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
321 /* route the table */
322 pci_intx_for_msi(dev, 0);
323 pci_msix_clear_and_set_ctrl(dev, 0,
324 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
326 write_msg = arch_restore_msi_irqs(dev);
328 for_each_pci_msi_entry(entry, dev) {
330 __pci_write_msi_msg(entry, &entry->msg);
331 pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl);
334 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
337 void pci_restore_msi_state(struct pci_dev *dev)
339 __pci_restore_msi_state(dev);
340 __pci_restore_msix_state(dev);
342 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
344 static struct msi_desc *
345 msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
347 struct irq_affinity_desc *masks = NULL;
348 struct msi_desc *entry;
352 masks = irq_create_affinity_masks(nvec, affd);
354 /* MSI Entry Initialization */
355 entry = alloc_msi_entry(&dev->dev, nvec, masks);
359 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
360 /* Lies, damned lies, and MSIs */
361 if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING)
362 control |= PCI_MSI_FLAGS_MASKBIT;
364 entry->pci.msi_attrib.is_msix = 0;
365 entry->pci.msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
366 entry->pci.msi_attrib.is_virtual = 0;
367 entry->pci.msi_attrib.entry_nr = 0;
368 entry->pci.msi_attrib.can_mask = !pci_msi_ignore_mask &&
369 !!(control & PCI_MSI_FLAGS_MASKBIT);
370 entry->pci.msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
371 entry->pci.msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
372 entry->pci.msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
374 if (control & PCI_MSI_FLAGS_64BIT)
375 entry->pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
377 entry->pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
379 /* Save the initial mask status */
380 if (entry->pci.msi_attrib.can_mask)
381 pci_read_config_dword(dev, entry->pci.mask_pos, &entry->pci.msi_mask);
388 static int msi_verify_entries(struct pci_dev *dev)
390 struct msi_desc *entry;
392 if (!dev->no_64bit_msi)
395 for_each_pci_msi_entry(entry, dev) {
396 if (entry->msg.address_hi) {
397 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
398 entry->msg.address_hi, entry->msg.address_lo);
406 * msi_capability_init - configure device's MSI capability structure
407 * @dev: pointer to the pci_dev data structure of MSI device function
408 * @nvec: number of interrupts to allocate
409 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
411 * Setup the MSI capability structure of the device with the requested
412 * number of interrupts. A return value of zero indicates the successful
413 * setup of an entry with the new MSI IRQ. A negative return value indicates
414 * an error, and a positive return value indicates the number of interrupts
415 * which could have been allocated.
417 static int msi_capability_init(struct pci_dev *dev, int nvec,
418 struct irq_affinity *affd)
420 const struct attribute_group **groups;
421 struct msi_desc *entry;
425 * Disable MSI during setup in the hardware, but mark it enabled
426 * so that setup code can evaluate it.
428 pci_msi_set_enable(dev, 0);
429 dev->msi_enabled = 1;
431 entry = msi_setup_entry(dev, nvec, affd);
437 /* All MSIs are unmasked by default; mask them all */
438 pci_msi_mask(entry, msi_multi_mask(entry));
440 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
442 /* Configure MSI capability structure */
443 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
447 ret = msi_verify_entries(dev);
451 groups = msi_populate_sysfs(&dev->dev);
452 if (IS_ERR(groups)) {
453 ret = PTR_ERR(groups);
457 dev->msi_irq_groups = groups;
459 /* Set MSI enabled bits */
460 pci_intx_for_msi(dev, 0);
461 pci_msi_set_enable(dev, 1);
463 pcibios_free_irq(dev);
464 dev->irq = entry->irq;
468 pci_msi_unmask(entry, msi_multi_mask(entry));
471 dev->msi_enabled = 0;
475 static void __iomem *msix_map_region(struct pci_dev *dev,
476 unsigned int nr_entries)
478 resource_size_t phys_addr;
483 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
485 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
486 flags = pci_resource_flags(dev, bir);
487 if (!flags || (flags & IORESOURCE_UNSET))
490 table_offset &= PCI_MSIX_TABLE_OFFSET;
491 phys_addr = pci_resource_start(dev, bir) + table_offset;
493 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
496 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
497 struct msix_entry *entries, int nvec,
498 struct irq_affinity *affd)
500 struct irq_affinity_desc *curmsk, *masks = NULL;
501 struct msi_desc *entry;
504 int vec_count = pci_msix_vec_count(dev);
507 masks = irq_create_affinity_masks(nvec, affd);
509 for (i = 0, curmsk = masks; i < nvec; i++) {
510 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
512 /* No enough memory. Don't try again */
517 entry->pci.msi_attrib.is_msix = 1;
518 entry->pci.msi_attrib.is_64 = 1;
521 entry->pci.msi_attrib.entry_nr = entries[i].entry;
523 entry->pci.msi_attrib.entry_nr = i;
525 entry->pci.msi_attrib.is_virtual =
526 entry->pci.msi_attrib.entry_nr >= vec_count;
528 entry->pci.msi_attrib.can_mask = !pci_msi_ignore_mask &&
529 !entry->pci.msi_attrib.is_virtual;
531 entry->pci.msi_attrib.default_irq = dev->irq;
532 entry->pci.mask_base = base;
534 if (entry->pci.msi_attrib.can_mask) {
535 addr = pci_msix_desc_addr(entry);
536 entry->pci.msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
539 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
549 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
551 struct msi_desc *entry;
554 for_each_pci_msi_entry(entry, dev) {
555 entries->vector = entry->irq;
561 static void msix_mask_all(void __iomem *base, int tsize)
563 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
566 if (pci_msi_ignore_mask)
569 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
570 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
574 * msix_capability_init - configure device's MSI-X capability
575 * @dev: pointer to the pci_dev data structure of MSI-X device function
576 * @entries: pointer to an array of struct msix_entry entries
577 * @nvec: number of @entries
578 * @affd: Optional pointer to enable automatic affinity assignment
580 * Setup the MSI-X capability structure of device function with a
581 * single MSI-X IRQ. A return of zero indicates the successful setup of
582 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
584 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
585 int nvec, struct irq_affinity *affd)
587 const struct attribute_group **groups;
593 * Some devices require MSI-X to be enabled before the MSI-X
594 * registers can be accessed. Mask all the vectors to prevent
595 * interrupts coming in before they're fully set up.
597 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
598 PCI_MSIX_FLAGS_ENABLE);
600 /* Mark it enabled so setup functions can query it */
601 dev->msix_enabled = 1;
603 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
604 /* Request & Map MSI-X table region */
605 tsize = msix_table_size(control);
606 base = msix_map_region(dev, tsize);
612 dev->msix_base = base;
614 ret = msix_setup_entries(dev, base, entries, nvec, affd);
618 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
622 /* Check if all MSI entries honor device restrictions */
623 ret = msi_verify_entries(dev);
627 msix_update_entries(dev, entries);
629 groups = msi_populate_sysfs(&dev->dev);
630 if (IS_ERR(groups)) {
631 ret = PTR_ERR(groups);
635 dev->msi_irq_groups = groups;
638 pci_intx_for_msi(dev, 0);
641 * Ensure that all table entries are masked to prevent
642 * stale entries from firing in a crash kernel.
644 * Done late to deal with a broken Marvell NVME device
645 * which takes the MSI-X mask bits into account even
646 * when MSI-X is disabled, which prevents MSI delivery.
648 msix_mask_all(base, tsize);
649 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
651 pcibios_free_irq(dev);
658 dev->msix_enabled = 0;
659 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
665 * pci_msi_supported - check whether MSI may be enabled on a device
666 * @dev: pointer to the pci_dev data structure of MSI device function
667 * @nvec: how many MSIs have been requested?
669 * Look at global flags, the device itself, and its parent buses
670 * to determine if MSI/-X are supported for the device. If MSI/-X is
671 * supported return 1, else return 0.
673 static int pci_msi_supported(struct pci_dev *dev, int nvec)
677 /* MSI must be globally enabled and supported by the device */
681 if (!dev || dev->no_msi)
685 * You can't ask to have 0 or less MSIs configured.
687 * b) the list manipulation code assumes nvec >= 1.
693 * Any bridge which does NOT route MSI transactions from its
694 * secondary bus to its primary bus must set NO_MSI flag on
695 * the secondary pci_bus.
697 * The NO_MSI flag can either be set directly by:
698 * - arch-specific PCI host bus controller drivers (deprecated)
699 * - quirks for specific PCI bridges
701 * or indirectly by platform-specific PCI host bridge drivers by
702 * advertising the 'msi_domain' property, which results in
703 * the NO_MSI flag when no MSI domain is found for this bridge
706 for (bus = dev->bus; bus; bus = bus->parent)
707 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
714 * pci_msi_vec_count - Return the number of MSI vectors a device can send
715 * @dev: device to report about
717 * This function returns the number of MSI vectors a device requested via
718 * Multiple Message Capable register. It returns a negative errno if the
719 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
720 * and returns a power of two, up to a maximum of 2^5 (32), according to the
723 int pci_msi_vec_count(struct pci_dev *dev)
731 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
732 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
736 EXPORT_SYMBOL(pci_msi_vec_count);
738 static void pci_msi_shutdown(struct pci_dev *dev)
740 struct msi_desc *desc;
742 if (!pci_msi_enable || !dev || !dev->msi_enabled)
745 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
746 desc = first_pci_msi_entry(dev);
748 pci_msi_set_enable(dev, 0);
749 pci_intx_for_msi(dev, 1);
750 dev->msi_enabled = 0;
752 /* Return the device with MSI unmasked as initial states */
753 pci_msi_unmask(desc, msi_multi_mask(desc));
755 /* Restore dev->irq to its default pin-assertion IRQ */
756 dev->irq = desc->pci.msi_attrib.default_irq;
757 pcibios_alloc_irq(dev);
760 void pci_disable_msi(struct pci_dev *dev)
762 if (!pci_msi_enable || !dev || !dev->msi_enabled)
765 pci_msi_shutdown(dev);
768 EXPORT_SYMBOL(pci_disable_msi);
771 * pci_msix_vec_count - return the number of device's MSI-X table entries
772 * @dev: pointer to the pci_dev data structure of MSI-X device function
773 * This function returns the number of device's MSI-X table entries and
774 * therefore the number of MSI-X vectors device is capable of sending.
775 * It returns a negative errno if the device is not capable of sending MSI-X
778 int pci_msix_vec_count(struct pci_dev *dev)
785 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
786 return msix_table_size(control);
788 EXPORT_SYMBOL(pci_msix_vec_count);
790 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
791 int nvec, struct irq_affinity *affd, int flags)
796 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
799 nr_entries = pci_msix_vec_count(dev);
802 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
806 /* Check for any invalid entries */
807 for (i = 0; i < nvec; i++) {
808 if (entries[i].entry >= nr_entries)
809 return -EINVAL; /* invalid entry */
810 for (j = i + 1; j < nvec; j++) {
811 if (entries[i].entry == entries[j].entry)
812 return -EINVAL; /* duplicate entry */
817 /* Check whether driver already requested for MSI IRQ */
818 if (dev->msi_enabled) {
819 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
822 return msix_capability_init(dev, entries, nvec, affd);
825 static void pci_msix_shutdown(struct pci_dev *dev)
827 struct msi_desc *entry;
829 if (!pci_msi_enable || !dev || !dev->msix_enabled)
832 if (pci_dev_is_disconnected(dev)) {
833 dev->msix_enabled = 0;
837 /* Return the device with MSI-X masked as initial states */
838 for_each_pci_msi_entry(entry, dev)
839 pci_msix_mask(entry);
841 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
842 pci_intx_for_msi(dev, 1);
843 dev->msix_enabled = 0;
844 pcibios_alloc_irq(dev);
847 void pci_disable_msix(struct pci_dev *dev)
849 if (!pci_msi_enable || !dev || !dev->msix_enabled)
852 pci_msix_shutdown(dev);
855 EXPORT_SYMBOL(pci_disable_msix);
857 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
858 struct irq_affinity *affd)
863 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
866 /* Check whether driver already requested MSI-X IRQs */
867 if (dev->msix_enabled) {
868 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
875 if (WARN_ON_ONCE(dev->msi_enabled))
878 nvec = pci_msi_vec_count(dev);
889 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
894 rc = msi_capability_init(dev, nvec, affd);
907 /* deprecated, don't use */
908 int pci_enable_msi(struct pci_dev *dev)
910 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
915 EXPORT_SYMBOL(pci_enable_msi);
917 static int __pci_enable_msix_range(struct pci_dev *dev,
918 struct msix_entry *entries, int minvec,
919 int maxvec, struct irq_affinity *affd,
922 int rc, nvec = maxvec;
927 if (WARN_ON_ONCE(dev->msix_enabled))
932 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
937 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
951 * pci_enable_msix_range - configure device's MSI-X capability structure
952 * @dev: pointer to the pci_dev data structure of MSI-X device function
953 * @entries: pointer to an array of MSI-X entries
954 * @minvec: minimum number of MSI-X IRQs requested
955 * @maxvec: maximum number of MSI-X IRQs requested
957 * Setup the MSI-X capability structure of device function with a maximum
958 * possible number of interrupts in the range between @minvec and @maxvec
959 * upon its software driver call to request for MSI-X mode enabled on its
960 * hardware device function. It returns a negative errno if an error occurs.
961 * If it succeeds, it returns the actual number of interrupts allocated and
962 * indicates the successful configuration of MSI-X capability structure
963 * with new allocated MSI-X interrupts.
965 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
966 int minvec, int maxvec)
968 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
970 EXPORT_SYMBOL(pci_enable_msix_range);
973 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
974 * @dev: PCI device to operate on
975 * @min_vecs: minimum number of vectors required (must be >= 1)
976 * @max_vecs: maximum (desired) number of vectors
977 * @flags: flags or quirks for the allocation
978 * @affd: optional description of the affinity requirements
980 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
981 * vectors if available, and fall back to a single legacy vector
982 * if neither is available. Return the number of vectors allocated,
983 * (which might be smaller than @max_vecs) if successful, or a negative
984 * error code on error. If less than @min_vecs interrupt vectors are
985 * available for @dev the function will fail with -ENOSPC.
987 * To get the Linux IRQ number used for a vector that can be passed to
988 * request_irq() use the pci_irq_vector() helper.
990 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
991 unsigned int max_vecs, unsigned int flags,
992 struct irq_affinity *affd)
994 struct irq_affinity msi_default_affd = {0};
997 if (flags & PCI_IRQ_AFFINITY) {
999 affd = &msi_default_affd;
1005 if (flags & PCI_IRQ_MSIX) {
1006 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1012 if (flags & PCI_IRQ_MSI) {
1013 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1018 /* use legacy IRQ if allowed */
1019 if (flags & PCI_IRQ_LEGACY) {
1020 if (min_vecs == 1 && dev->irq) {
1022 * Invoke the affinity spreading logic to ensure that
1023 * the device driver can adjust queue configuration
1024 * for the single interrupt case.
1027 irq_create_affinity_masks(1, affd);
1035 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1038 * pci_free_irq_vectors - free previously allocated IRQs for a device
1039 * @dev: PCI device to operate on
1041 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1043 void pci_free_irq_vectors(struct pci_dev *dev)
1045 pci_disable_msix(dev);
1046 pci_disable_msi(dev);
1048 EXPORT_SYMBOL(pci_free_irq_vectors);
1051 * pci_irq_vector - return Linux IRQ number of a device vector
1052 * @dev: PCI device to operate on
1053 * @nr: Interrupt vector index (0-based)
1055 * @nr has the following meanings depending on the interrupt mode:
1056 * MSI-X: The index in the MSI-X vector table
1057 * MSI: The index of the enabled MSI vectors
1060 * Return: The Linux interrupt number or -EINVAl if @nr is out of range.
1062 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1064 if (dev->msix_enabled) {
1065 struct msi_desc *entry;
1067 for_each_pci_msi_entry(entry, dev) {
1068 if (entry->pci.msi_attrib.entry_nr == nr)
1075 if (dev->msi_enabled) {
1076 struct msi_desc *entry = first_pci_msi_entry(dev);
1078 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1081 if (WARN_ON_ONCE(nr > 0))
1085 return dev->irq + nr;
1087 EXPORT_SYMBOL(pci_irq_vector);
1090 * pci_irq_get_affinity - return the affinity of a particular MSI vector
1091 * @dev: PCI device to operate on
1092 * @nr: device-relative interrupt vector index (0-based).
1094 * @nr has the following meanings depending on the interrupt mode:
1095 * MSI-X: The index in the MSI-X vector table
1096 * MSI: The index of the enabled MSI vectors
1099 * Return: A cpumask pointer or NULL if @nr is out of range
1101 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1103 if (dev->msix_enabled) {
1104 struct msi_desc *entry;
1106 for_each_pci_msi_entry(entry, dev) {
1107 if (entry->pci.msi_attrib.entry_nr == nr)
1108 return &entry->affinity->mask;
1112 } else if (dev->msi_enabled) {
1113 struct msi_desc *entry = first_pci_msi_entry(dev);
1115 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1116 nr >= entry->nvec_used))
1119 return &entry->affinity[nr].mask;
1121 return cpu_possible_mask;
1124 EXPORT_SYMBOL(pci_irq_get_affinity);
1126 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1128 return to_pci_dev(desc->dev);
1130 EXPORT_SYMBOL(msi_desc_to_pci_dev);
1132 void pci_no_msi(void)
1138 * pci_msi_enabled - is MSI enabled?
1140 * Returns true if MSI has not been disabled by the command-line option
1143 int pci_msi_enabled(void)
1145 return pci_msi_enable;
1147 EXPORT_SYMBOL(pci_msi_enabled);