1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
7 * Copyright (C) 2016 Christoph Hellwig.
9 #include <linux/bitfield.h>
10 #include <linux/err.h>
11 #include <linux/export.h>
12 #include <linux/irq.h>
17 int pci_msi_enable = 1;
18 int pci_msi_ignore_mask;
21 * pci_msi_supported - check whether MSI may be enabled on a device
22 * @dev: pointer to the pci_dev data structure of MSI device function
23 * @nvec: how many MSIs have been requested?
25 * Look at global flags, the device itself, and its parent buses
26 * to determine if MSI/-X are supported for the device. If MSI/-X is
27 * supported return 1, else return 0.
29 static int pci_msi_supported(struct pci_dev *dev, int nvec)
33 /* MSI must be globally enabled and supported by the device */
37 if (!dev || dev->no_msi)
41 * You can't ask to have 0 or less MSIs configured.
43 * b) the list manipulation code assumes nvec >= 1.
49 * Any bridge which does NOT route MSI transactions from its
50 * secondary bus to its primary bus must set NO_MSI flag on
51 * the secondary pci_bus.
53 * The NO_MSI flag can either be set directly by:
54 * - arch-specific PCI host bus controller drivers (deprecated)
55 * - quirks for specific PCI bridges
57 * or indirectly by platform-specific PCI host bridge drivers by
58 * advertising the 'msi_domain' property, which results in
59 * the NO_MSI flag when no MSI domain is found for this bridge
62 for (bus = dev->bus; bus; bus = bus->parent)
63 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
69 static void pcim_msi_release(void *pcidev)
71 struct pci_dev *dev = pcidev;
73 dev->is_msi_managed = false;
74 pci_free_irq_vectors(dev);
78 * Needs to be separate from pcim_release to prevent an ordering problem
79 * vs. msi_device_data_release() in the MSI core code.
81 static int pcim_setup_msi_release(struct pci_dev *dev)
85 if (!pci_is_managed(dev) || dev->is_msi_managed)
88 ret = devm_add_action(&dev->dev, pcim_msi_release, dev);
92 dev->is_msi_managed = true;
97 * Ordering vs. devres: msi device data has to be installed first so that
98 * pcim_msi_release() is invoked before it on device release.
100 static int pci_setup_msi_context(struct pci_dev *dev)
102 int ret = msi_setup_device_data(&dev->dev);
107 return pcim_setup_msi_release(dev);
111 * Helper functions for mask/unmask and MSI message handling
114 void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set)
116 raw_spinlock_t *lock = &to_pci_dev(desc->dev)->msi_lock;
119 if (!desc->pci.msi_attrib.can_mask)
122 raw_spin_lock_irqsave(lock, flags);
123 desc->pci.msi_mask &= ~clear;
124 desc->pci.msi_mask |= set;
125 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->pci.mask_pos,
127 raw_spin_unlock_irqrestore(lock, flags);
131 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
132 * @data: pointer to irqdata associated to that interrupt
134 void pci_msi_mask_irq(struct irq_data *data)
136 struct msi_desc *desc = irq_data_get_msi_desc(data);
138 __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
140 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
143 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
144 * @data: pointer to irqdata associated to that interrupt
146 void pci_msi_unmask_irq(struct irq_data *data)
148 struct msi_desc *desc = irq_data_get_msi_desc(data);
150 __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
152 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
154 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
156 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
158 BUG_ON(dev->current_state != PCI_D0);
160 if (entry->pci.msi_attrib.is_msix) {
161 void __iomem *base = pci_msix_desc_addr(entry);
163 if (WARN_ON_ONCE(entry->pci.msi_attrib.is_virtual))
166 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
167 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
168 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
170 int pos = dev->msi_cap;
173 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
175 if (entry->pci.msi_attrib.is_64) {
176 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
178 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
181 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
187 static inline void pci_write_msg_msi(struct pci_dev *dev, struct msi_desc *desc,
190 int pos = dev->msi_cap;
193 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
194 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
195 msgctl |= FIELD_PREP(PCI_MSI_FLAGS_QSIZE, desc->pci.msi_attrib.multiple);
196 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
198 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, msg->address_lo);
199 if (desc->pci.msi_attrib.is_64) {
200 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, msg->address_hi);
201 pci_write_config_word(dev, pos + PCI_MSI_DATA_64, msg->data);
203 pci_write_config_word(dev, pos + PCI_MSI_DATA_32, msg->data);
205 /* Ensure that the writes are visible in the device */
206 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
209 static inline void pci_write_msg_msix(struct msi_desc *desc, struct msi_msg *msg)
211 void __iomem *base = pci_msix_desc_addr(desc);
212 u32 ctrl = desc->pci.msix_ctrl;
213 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
215 if (desc->pci.msi_attrib.is_virtual)
218 * The specification mandates that the entry is masked
219 * when the message is modified:
221 * "If software changes the Address or Data value of an
222 * entry while the entry is unmasked, the result is
226 pci_msix_write_vector_ctrl(desc, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
228 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
229 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
230 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
233 pci_msix_write_vector_ctrl(desc, ctrl);
235 /* Ensure that the writes are visible in the device */
236 readl(base + PCI_MSIX_ENTRY_DATA);
239 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
241 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
243 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
244 /* Don't touch the hardware now */
245 } else if (entry->pci.msi_attrib.is_msix) {
246 pci_write_msg_msix(entry, msg);
248 pci_write_msg_msi(dev, entry, msg);
253 if (entry->write_msi_msg)
254 entry->write_msi_msg(entry, entry->write_msi_msg_data);
257 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
259 struct msi_desc *entry = irq_get_msi_desc(irq);
261 __pci_write_msi_msg(entry, msg);
263 EXPORT_SYMBOL_GPL(pci_write_msi_msg);
266 /* PCI/MSI specific functionality */
268 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
270 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
271 pci_intx(dev, enable);
274 static void pci_msi_set_enable(struct pci_dev *dev, int enable)
278 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
279 control &= ~PCI_MSI_FLAGS_ENABLE;
281 control |= PCI_MSI_FLAGS_ENABLE;
282 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
285 static int msi_setup_msi_desc(struct pci_dev *dev, int nvec,
286 struct irq_affinity_desc *masks)
288 struct msi_desc desc;
291 /* MSI Entry Initialization */
292 memset(&desc, 0, sizeof(desc));
294 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
295 /* Lies, damned lies, and MSIs */
296 if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING)
297 control |= PCI_MSI_FLAGS_MASKBIT;
298 /* Respect XEN's mask disabling */
299 if (pci_msi_ignore_mask)
300 control &= ~PCI_MSI_FLAGS_MASKBIT;
302 desc.nvec_used = nvec;
303 desc.pci.msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
304 desc.pci.msi_attrib.can_mask = !!(control & PCI_MSI_FLAGS_MASKBIT);
305 desc.pci.msi_attrib.default_irq = dev->irq;
306 desc.pci.msi_attrib.multi_cap = FIELD_GET(PCI_MSI_FLAGS_QMASK, control);
307 desc.pci.msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
308 desc.affinity = masks;
310 if (control & PCI_MSI_FLAGS_64BIT)
311 desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
313 desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
315 /* Save the initial mask status */
316 if (desc.pci.msi_attrib.can_mask)
317 pci_read_config_dword(dev, desc.pci.mask_pos, &desc.pci.msi_mask);
319 return msi_insert_msi_desc(&dev->dev, &desc);
322 static int msi_verify_entries(struct pci_dev *dev)
324 struct msi_desc *entry;
326 if (!dev->no_64bit_msi)
329 msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
330 if (entry->msg.address_hi) {
331 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
332 entry->msg.address_hi, entry->msg.address_lo);
336 return !entry ? 0 : -EIO;
340 * msi_capability_init - configure device's MSI capability structure
341 * @dev: pointer to the pci_dev data structure of MSI device function
342 * @nvec: number of interrupts to allocate
343 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
345 * Setup the MSI capability structure of the device with the requested
346 * number of interrupts. A return value of zero indicates the successful
347 * setup of an entry with the new MSI IRQ. A negative return value indicates
348 * an error, and a positive return value indicates the number of interrupts
349 * which could have been allocated.
351 static int msi_capability_init(struct pci_dev *dev, int nvec,
352 struct irq_affinity *affd)
354 struct irq_affinity_desc *masks = NULL;
355 struct msi_desc *entry, desc;
358 /* Reject multi-MSI early on irq domain enabled architectures */
359 if (nvec > 1 && !pci_msi_domain_supports(dev, MSI_FLAG_MULTI_PCI_MSI, ALLOW_LEGACY))
363 * Disable MSI during setup in the hardware, but mark it enabled
364 * so that setup code can evaluate it.
366 pci_msi_set_enable(dev, 0);
367 dev->msi_enabled = 1;
370 masks = irq_create_affinity_masks(nvec, affd);
372 msi_lock_descs(&dev->dev);
373 ret = msi_setup_msi_desc(dev, nvec, masks);
377 /* All MSIs are unmasked by default; mask them all */
378 entry = msi_first_desc(&dev->dev, MSI_DESC_ALL);
379 pci_msi_mask(entry, msi_multi_mask(entry));
381 * Copy the MSI descriptor for the error path because
382 * pci_msi_setup_msi_irqs() will free it for the hierarchical
383 * interrupt domain case.
385 memcpy(&desc, entry, sizeof(desc));
387 /* Configure MSI capability structure */
388 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
392 ret = msi_verify_entries(dev);
396 /* Set MSI enabled bits */
397 pci_intx_for_msi(dev, 0);
398 pci_msi_set_enable(dev, 1);
400 pcibios_free_irq(dev);
401 dev->irq = entry->irq;
405 pci_msi_unmask(&desc, msi_multi_mask(&desc));
406 pci_free_msi_irqs(dev);
408 dev->msi_enabled = 0;
410 msi_unlock_descs(&dev->dev);
415 int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
416 struct irq_affinity *affd)
421 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
424 /* Check whether driver already requested MSI-X IRQs */
425 if (dev->msix_enabled) {
426 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
433 if (WARN_ON_ONCE(dev->msi_enabled))
436 /* Test for the availability of MSI support */
437 if (!pci_msi_domain_supports(dev, 0, ALLOW_LEGACY))
440 nvec = pci_msi_vec_count(dev);
449 rc = pci_setup_msi_context(dev);
453 if (!pci_setup_msi_device_domain(dev))
458 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
463 rc = msi_capability_init(dev, nvec, affd);
477 * pci_msi_vec_count - Return the number of MSI vectors a device can send
478 * @dev: device to report about
480 * This function returns the number of MSI vectors a device requested via
481 * Multiple Message Capable register. It returns a negative errno if the
482 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
483 * and returns a power of two, up to a maximum of 2^5 (32), according to the
486 int pci_msi_vec_count(struct pci_dev *dev)
494 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
495 ret = 1 << FIELD_GET(PCI_MSI_FLAGS_QMASK, msgctl);
499 EXPORT_SYMBOL(pci_msi_vec_count);
502 * Architecture override returns true when the PCI MSI message should be
503 * written by the generic restore function.
505 bool __weak arch_restore_msi_irqs(struct pci_dev *dev)
510 void __pci_restore_msi_state(struct pci_dev *dev)
512 struct msi_desc *entry;
515 if (!dev->msi_enabled)
518 entry = irq_get_msi_desc(dev->irq);
520 pci_intx_for_msi(dev, 0);
521 pci_msi_set_enable(dev, 0);
522 if (arch_restore_msi_irqs(dev))
523 __pci_write_msi_msg(entry, &entry->msg);
525 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
526 pci_msi_update_mask(entry, 0, 0);
527 control &= ~PCI_MSI_FLAGS_QSIZE;
528 control |= PCI_MSI_FLAGS_ENABLE |
529 FIELD_PREP(PCI_MSI_FLAGS_QSIZE, entry->pci.msi_attrib.multiple);
530 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
533 void pci_msi_shutdown(struct pci_dev *dev)
535 struct msi_desc *desc;
537 if (!pci_msi_enable || !dev || !dev->msi_enabled)
540 pci_msi_set_enable(dev, 0);
541 pci_intx_for_msi(dev, 1);
542 dev->msi_enabled = 0;
544 /* Return the device with MSI unmasked as initial states */
545 desc = msi_first_desc(&dev->dev, MSI_DESC_ALL);
546 if (!WARN_ON_ONCE(!desc))
547 pci_msi_unmask(desc, msi_multi_mask(desc));
549 /* Restore dev->irq to its default pin-assertion IRQ */
550 dev->irq = desc->pci.msi_attrib.default_irq;
551 pcibios_alloc_irq(dev);
554 /* PCI/MSI-X specific functionality */
556 static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
560 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
563 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
566 static void __iomem *msix_map_region(struct pci_dev *dev,
567 unsigned int nr_entries)
569 resource_size_t phys_addr;
574 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
576 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
577 flags = pci_resource_flags(dev, bir);
578 if (!flags || (flags & IORESOURCE_UNSET))
581 table_offset &= PCI_MSIX_TABLE_OFFSET;
582 phys_addr = pci_resource_start(dev, bir) + table_offset;
584 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
588 * msix_prepare_msi_desc - Prepare a half initialized MSI descriptor for operation
589 * @dev: The PCI device for which the descriptor is prepared
590 * @desc: The MSI descriptor for preparation
592 * This is separate from msix_setup_msi_descs() below to handle dynamic
593 * allocations for MSI-X after initial enablement.
595 * Ideally the whole MSI-X setup would work that way, but there is no way to
596 * support this for the legacy arch_setup_msi_irqs() mechanism and for the
597 * fake irq domains like the x86 XEN one. Sigh...
599 * The descriptor is zeroed and only @desc::msi_index and @desc::affinity
600 * are set. When called from msix_setup_msi_descs() then the is_virtual
601 * attribute is initialized as well.
605 void msix_prepare_msi_desc(struct pci_dev *dev, struct msi_desc *desc)
608 desc->pci.msi_attrib.is_msix = 1;
609 desc->pci.msi_attrib.is_64 = 1;
610 desc->pci.msi_attrib.default_irq = dev->irq;
611 desc->pci.mask_base = dev->msix_base;
612 desc->pci.msi_attrib.can_mask = !pci_msi_ignore_mask &&
613 !desc->pci.msi_attrib.is_virtual;
615 if (desc->pci.msi_attrib.can_mask) {
616 void __iomem *addr = pci_msix_desc_addr(desc);
618 desc->pci.msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
622 static int msix_setup_msi_descs(struct pci_dev *dev, struct msix_entry *entries,
623 int nvec, struct irq_affinity_desc *masks)
625 int ret = 0, i, vec_count = pci_msix_vec_count(dev);
626 struct irq_affinity_desc *curmsk;
627 struct msi_desc desc;
629 memset(&desc, 0, sizeof(desc));
631 for (i = 0, curmsk = masks; i < nvec; i++, curmsk++) {
632 desc.msi_index = entries ? entries[i].entry : i;
633 desc.affinity = masks ? curmsk : NULL;
634 desc.pci.msi_attrib.is_virtual = desc.msi_index >= vec_count;
636 msix_prepare_msi_desc(dev, &desc);
638 ret = msi_insert_msi_desc(&dev->dev, &desc);
645 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
647 struct msi_desc *desc;
650 msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL) {
651 entries->vector = desc->irq;
657 static void msix_mask_all(void __iomem *base, int tsize)
659 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
662 if (pci_msi_ignore_mask)
665 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
666 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
669 static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
670 int nvec, struct irq_affinity *affd)
672 struct irq_affinity_desc *masks = NULL;
676 masks = irq_create_affinity_masks(nvec, affd);
678 msi_lock_descs(&dev->dev);
679 ret = msix_setup_msi_descs(dev, entries, nvec, masks);
683 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
687 /* Check if all MSI entries honor device restrictions */
688 ret = msi_verify_entries(dev);
692 msix_update_entries(dev, entries);
696 pci_free_msi_irqs(dev);
698 msi_unlock_descs(&dev->dev);
704 * msix_capability_init - configure device's MSI-X capability
705 * @dev: pointer to the pci_dev data structure of MSI-X device function
706 * @entries: pointer to an array of struct msix_entry entries
707 * @nvec: number of @entries
708 * @affd: Optional pointer to enable automatic affinity assignment
710 * Setup the MSI-X capability structure of device function with a
711 * single MSI-X IRQ. A return of zero indicates the successful setup of
712 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
714 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
715 int nvec, struct irq_affinity *affd)
721 * Some devices require MSI-X to be enabled before the MSI-X
722 * registers can be accessed. Mask all the vectors to prevent
723 * interrupts coming in before they're fully set up.
725 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
726 PCI_MSIX_FLAGS_ENABLE);
728 /* Mark it enabled so setup functions can query it */
729 dev->msix_enabled = 1;
731 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
732 /* Request & Map MSI-X table region */
733 tsize = msix_table_size(control);
734 dev->msix_base = msix_map_region(dev, tsize);
735 if (!dev->msix_base) {
740 ret = msix_setup_interrupts(dev, entries, nvec, affd);
745 pci_intx_for_msi(dev, 0);
748 * Ensure that all table entries are masked to prevent
749 * stale entries from firing in a crash kernel.
751 * Done late to deal with a broken Marvell NVME device
752 * which takes the MSI-X mask bits into account even
753 * when MSI-X is disabled, which prevents MSI delivery.
755 msix_mask_all(dev->msix_base, tsize);
756 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
758 pcibios_free_irq(dev);
762 dev->msix_enabled = 0;
763 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
768 static bool pci_msix_validate_entries(struct pci_dev *dev, struct msix_entry *entries, int nvec)
776 nogap = pci_msi_domain_supports(dev, MSI_FLAG_MSIX_CONTIGUOUS, DENY_LEGACY);
778 for (i = 0; i < nvec; i++) {
779 /* Check for duplicate entries */
780 for (j = i + 1; j < nvec; j++) {
781 if (entries[i].entry == entries[j].entry)
784 /* Check for unsupported gaps */
785 if (nogap && entries[i].entry != i)
791 int __pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, int minvec,
792 int maxvec, struct irq_affinity *affd, int flags)
794 int hwsize, rc, nvec = maxvec;
799 if (dev->msi_enabled) {
800 pci_info(dev, "can't enable MSI-X (MSI already enabled)\n");
804 if (WARN_ON_ONCE(dev->msix_enabled))
807 /* Check MSI-X early on irq domain enabled architectures */
808 if (!pci_msi_domain_supports(dev, MSI_FLAG_PCI_MSIX, ALLOW_LEGACY))
811 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
814 hwsize = pci_msix_vec_count(dev);
818 if (!pci_msix_validate_entries(dev, entries, nvec))
822 /* Keep the IRQ virtual hackery working */
823 if (flags & PCI_IRQ_VIRTUAL)
832 rc = pci_setup_msi_context(dev);
836 if (!pci_setup_msix_device_domain(dev, hwsize))
841 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
846 rc = msix_capability_init(dev, entries, nvec, affd);
859 void __pci_restore_msix_state(struct pci_dev *dev)
861 struct msi_desc *entry;
864 if (!dev->msix_enabled)
867 /* route the table */
868 pci_intx_for_msi(dev, 0);
869 pci_msix_clear_and_set_ctrl(dev, 0,
870 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
872 write_msg = arch_restore_msi_irqs(dev);
874 msi_lock_descs(&dev->dev);
875 msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
877 __pci_write_msi_msg(entry, &entry->msg);
878 pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl);
880 msi_unlock_descs(&dev->dev);
882 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
885 void pci_msix_shutdown(struct pci_dev *dev)
887 struct msi_desc *desc;
889 if (!pci_msi_enable || !dev || !dev->msix_enabled)
892 if (pci_dev_is_disconnected(dev)) {
893 dev->msix_enabled = 0;
897 /* Return the device with MSI-X masked as initial states */
898 msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL)
901 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
902 pci_intx_for_msi(dev, 1);
903 dev->msix_enabled = 0;
904 pcibios_alloc_irq(dev);
907 /* Common interfaces */
909 void pci_free_msi_irqs(struct pci_dev *dev)
911 pci_msi_teardown_msi_irqs(dev);
913 if (dev->msix_base) {
914 iounmap(dev->msix_base);
915 dev->msix_base = NULL;
919 /* Misc. infrastructure */
921 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
923 return to_pci_dev(desc->dev);
925 EXPORT_SYMBOL(msi_desc_to_pci_dev);
927 void pci_no_msi(void)