3 * Purpose: PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
11 #include <linux/irq.h>
12 #include <linux/interrupt.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/pci.h>
16 #include <linux/proc_fs.h>
17 #include <linux/msi.h>
18 #include <linux/smp.h>
20 #include <asm/errno.h>
26 static int pci_msi_enable = 1;
30 int __attribute__ ((weak))
31 arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
36 int __attribute__ ((weak))
37 arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
42 int __attribute__ ((weak))
43 arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
45 struct msi_desc *entry;
48 list_for_each_entry(entry, &dev->msi_list, list) {
49 ret = arch_setup_msi_irq(dev, entry);
57 void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
62 void __attribute__ ((weak))
63 arch_teardown_msi_irqs(struct pci_dev *dev)
65 struct msi_desc *entry;
67 list_for_each_entry(entry, &dev->msi_list, list) {
69 arch_teardown_msi_irq(entry->irq);
73 static void __msi_set_enable(struct pci_dev *dev, int pos, int enable)
78 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
79 control &= ~PCI_MSI_FLAGS_ENABLE;
81 control |= PCI_MSI_FLAGS_ENABLE;
82 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
86 static void msi_set_enable(struct pci_dev *dev, int enable)
88 __msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable);
91 static void msix_set_enable(struct pci_dev *dev, int enable)
96 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
98 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
99 control &= ~PCI_MSIX_FLAGS_ENABLE;
101 control |= PCI_MSIX_FLAGS_ENABLE;
102 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
106 static inline __attribute_const__ u32 msi_mask(unsigned x)
108 /* Don't shift by >= width of type */
111 return (1 << (1 << x)) - 1;
114 static void msix_flush_writes(struct irq_desc *desc)
116 struct msi_desc *entry;
118 entry = get_irq_desc_msi(desc);
119 BUG_ON(!entry || !entry->dev);
120 switch (entry->msi_attrib.type) {
124 case PCI_CAP_ID_MSIX:
126 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
127 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
128 readl(entry->mask_base + offset);
138 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
139 * mask all MSI interrupts by clearing the MSI enable bit does not work
140 * reliably as devices without an INTx disable bit will then generate a
141 * level IRQ which will never be cleared.
143 * Returns 1 if it succeeded in masking the interrupt and 0 if the device
144 * doesn't support MSI masking.
146 static int msi_set_mask_bits(struct irq_desc *desc, u32 mask, u32 flag)
148 struct msi_desc *entry;
150 entry = get_irq_desc_msi(desc);
151 BUG_ON(!entry || !entry->dev);
152 switch (entry->msi_attrib.type) {
154 if (entry->msi_attrib.maskbit) {
158 pos = (long)entry->mask_base;
159 pci_read_config_dword(entry->dev, pos, &mask_bits);
160 mask_bits &= ~(mask);
161 mask_bits |= flag & mask;
162 pci_write_config_dword(entry->dev, pos, mask_bits);
167 case PCI_CAP_ID_MSIX:
169 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
170 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
171 writel(flag, entry->mask_base + offset);
172 readl(entry->mask_base + offset);
179 entry->msi_attrib.masked = !!flag;
183 void read_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
185 struct msi_desc *entry = get_irq_desc_msi(desc);
186 switch(entry->msi_attrib.type) {
189 struct pci_dev *dev = entry->dev;
190 int pos = entry->msi_attrib.pos;
193 pci_read_config_dword(dev, msi_lower_address_reg(pos),
195 if (entry->msi_attrib.is_64) {
196 pci_read_config_dword(dev, msi_upper_address_reg(pos),
198 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
201 pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
206 case PCI_CAP_ID_MSIX:
209 base = entry->mask_base +
210 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
212 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
213 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
214 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
222 void read_msi_msg(unsigned int irq, struct msi_msg *msg)
224 struct irq_desc *desc = irq_to_desc(irq);
226 read_msi_msg_desc(desc, msg);
229 void write_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
231 struct msi_desc *entry = get_irq_desc_msi(desc);
232 switch (entry->msi_attrib.type) {
235 struct pci_dev *dev = entry->dev;
236 int pos = entry->msi_attrib.pos;
238 pci_write_config_dword(dev, msi_lower_address_reg(pos),
240 if (entry->msi_attrib.is_64) {
241 pci_write_config_dword(dev, msi_upper_address_reg(pos),
243 pci_write_config_word(dev, msi_data_reg(pos, 1),
246 pci_write_config_word(dev, msi_data_reg(pos, 0),
251 case PCI_CAP_ID_MSIX:
254 base = entry->mask_base +
255 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
257 writel(msg->address_lo,
258 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
259 writel(msg->address_hi,
260 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
261 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
270 void write_msi_msg(unsigned int irq, struct msi_msg *msg)
272 struct irq_desc *desc = irq_to_desc(irq);
274 write_msi_msg_desc(desc, msg);
277 void mask_msi_irq(unsigned int irq)
279 struct irq_desc *desc = irq_to_desc(irq);
281 msi_set_mask_bits(desc, 1, 1);
282 msix_flush_writes(desc);
285 void unmask_msi_irq(unsigned int irq)
287 struct irq_desc *desc = irq_to_desc(irq);
289 msi_set_mask_bits(desc, 1, 0);
290 msix_flush_writes(desc);
293 static int msi_free_irqs(struct pci_dev* dev);
295 static struct msi_desc* alloc_msi_entry(void)
297 struct msi_desc *entry;
299 entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
303 INIT_LIST_HEAD(&entry->list);
310 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
312 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
313 pci_intx(dev, enable);
316 static void __pci_restore_msi_state(struct pci_dev *dev)
320 struct msi_desc *entry;
322 if (!dev->msi_enabled)
325 entry = get_irq_msi(dev->irq);
326 pos = entry->msi_attrib.pos;
328 pci_intx_for_msi(dev, 0);
329 msi_set_enable(dev, 0);
330 write_msi_msg(dev->irq, &entry->msg);
331 if (entry->msi_attrib.maskbit) {
332 struct irq_desc *desc = irq_to_desc(dev->irq);
333 msi_set_mask_bits(desc, entry->msi_attrib.maskbits_mask,
334 entry->msi_attrib.masked);
337 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
338 control &= ~PCI_MSI_FLAGS_QSIZE;
339 control |= PCI_MSI_FLAGS_ENABLE;
340 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
343 static void __pci_restore_msix_state(struct pci_dev *dev)
346 struct msi_desc *entry;
349 if (!dev->msix_enabled)
352 /* route the table */
353 pci_intx_for_msi(dev, 0);
354 msix_set_enable(dev, 0);
356 list_for_each_entry(entry, &dev->msi_list, list) {
357 struct irq_desc *desc = irq_to_desc(entry->irq);
358 write_msi_msg(entry->irq, &entry->msg);
359 msi_set_mask_bits(desc, 1, entry->msi_attrib.masked);
362 BUG_ON(list_empty(&dev->msi_list));
363 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
364 pos = entry->msi_attrib.pos;
365 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
366 control &= ~PCI_MSIX_FLAGS_MASKALL;
367 control |= PCI_MSIX_FLAGS_ENABLE;
368 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
371 void pci_restore_msi_state(struct pci_dev *dev)
373 __pci_restore_msi_state(dev);
374 __pci_restore_msix_state(dev);
376 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
379 * msi_capability_init - configure device's MSI capability structure
380 * @dev: pointer to the pci_dev data structure of MSI device function
382 * Setup the MSI capability structure of device function with a single
383 * MSI irq, regardless of device function is capable of handling
384 * multiple messages. A return of zero indicates the successful setup
385 * of an entry zero with the new MSI irq or non-zero for otherwise.
387 static int msi_capability_init(struct pci_dev *dev)
389 struct msi_desc *entry;
393 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
395 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
396 pci_read_config_word(dev, msi_control_reg(pos), &control);
397 /* MSI Entry Initialization */
398 entry = alloc_msi_entry();
402 entry->msi_attrib.type = PCI_CAP_ID_MSI;
403 entry->msi_attrib.is_64 = is_64bit_address(control);
404 entry->msi_attrib.entry_nr = 0;
405 entry->msi_attrib.maskbit = is_mask_bit_support(control);
406 entry->msi_attrib.masked = 1;
407 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
408 entry->msi_attrib.pos = pos;
410 if (entry->msi_attrib.maskbit) {
411 unsigned int base, maskbits, temp;
413 base = msi_mask_bits_reg(pos, entry->msi_attrib.is_64);
414 entry->mask_base = (void __iomem *)(long)base;
416 /* All MSIs are unmasked by default, Mask them all */
417 pci_read_config_dword(dev, base, &maskbits);
418 temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);
420 pci_write_config_dword(dev, base, maskbits);
421 entry->msi_attrib.maskbits_mask = temp;
423 list_add_tail(&entry->list, &dev->msi_list);
425 /* Configure MSI capability structure */
426 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
432 /* Set MSI enabled bits */
433 pci_intx_for_msi(dev, 0);
434 msi_set_enable(dev, 1);
435 dev->msi_enabled = 1;
437 dev->irq = entry->irq;
442 * msix_capability_init - configure device's MSI-X capability
443 * @dev: pointer to the pci_dev data structure of MSI-X device function
444 * @entries: pointer to an array of struct msix_entry entries
445 * @nvec: number of @entries
447 * Setup the MSI-X capability structure of device function with a
448 * single MSI-X irq. A return of zero indicates the successful setup of
449 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
451 static int msix_capability_init(struct pci_dev *dev,
452 struct msix_entry *entries, int nvec)
454 struct msi_desc *entry;
455 int pos, i, j, nr_entries, ret;
456 unsigned long phys_addr;
462 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
464 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
465 /* Request & Map MSI-X table region */
466 pci_read_config_word(dev, msi_control_reg(pos), &control);
467 nr_entries = multi_msix_capable(control);
469 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
470 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
471 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
472 phys_addr = pci_resource_start (dev, bir) + table_offset;
473 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
477 /* MSI-X Table Initialization */
478 for (i = 0; i < nvec; i++) {
479 entry = alloc_msi_entry();
483 j = entries[i].entry;
484 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
485 entry->msi_attrib.is_64 = 1;
486 entry->msi_attrib.entry_nr = j;
487 entry->msi_attrib.maskbit = 1;
488 entry->msi_attrib.masked = 1;
489 entry->msi_attrib.default_irq = dev->irq;
490 entry->msi_attrib.pos = pos;
492 entry->mask_base = base;
494 list_add_tail(&entry->list, &dev->msi_list);
497 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
500 list_for_each_entry(entry, &dev->msi_list, list) {
501 if (entry->irq != 0) {
508 /* If we had some success report the number of irqs
509 * we succeeded in setting up.
517 list_for_each_entry(entry, &dev->msi_list, list) {
518 entries[i].vector = entry->irq;
519 set_irq_msi(entry->irq, entry);
522 /* Set MSI-X enabled bits */
523 pci_intx_for_msi(dev, 0);
524 msix_set_enable(dev, 1);
525 dev->msix_enabled = 1;
531 * pci_msi_check_device - check whether MSI may be enabled on a device
532 * @dev: pointer to the pci_dev data structure of MSI device function
533 * @nvec: how many MSIs have been requested ?
534 * @type: are we checking for MSI or MSI-X ?
536 * Look at global flags, the device itself, and its parent busses
537 * to determine if MSI/-X are supported for the device. If MSI/-X is
538 * supported return 0, else return an error code.
540 static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
545 /* MSI must be globally enabled and supported by the device */
546 if (!pci_msi_enable || !dev || dev->no_msi)
550 * You can't ask to have 0 or less MSIs configured.
552 * b) the list manipulation code assumes nvec >= 1.
557 /* Any bridge which does NOT route MSI transactions from it's
558 * secondary bus to it's primary bus must set NO_MSI flag on
559 * the secondary pci_bus.
560 * We expect only arch-specific PCI host bus controller driver
561 * or quirks for specific PCI bridges to be setting NO_MSI.
563 for (bus = dev->bus; bus; bus = bus->parent)
564 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
567 ret = arch_msi_check_device(dev, nvec, type);
571 if (!pci_find_capability(dev, type))
578 * pci_enable_msi - configure device's MSI capability structure
579 * @dev: pointer to the pci_dev data structure of MSI device function
581 * Setup the MSI capability structure of device function with
582 * a single MSI irq upon its software driver call to request for
583 * MSI mode enabled on its hardware device function. A return of zero
584 * indicates the successful setup of an entry zero with the new MSI
585 * irq or non-zero for otherwise.
587 int pci_enable_msi(struct pci_dev* dev)
591 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
595 WARN_ON(!!dev->msi_enabled);
597 /* Check whether driver already requested for MSI-X irqs */
598 if (dev->msix_enabled) {
599 dev_info(&dev->dev, "can't enable MSI "
600 "(MSI-X already enabled)\n");
603 status = msi_capability_init(dev);
606 EXPORT_SYMBOL(pci_enable_msi);
608 void pci_msi_shutdown(struct pci_dev* dev)
610 struct msi_desc *entry;
612 if (!pci_msi_enable || !dev || !dev->msi_enabled)
615 msi_set_enable(dev, 0);
616 pci_intx_for_msi(dev, 1);
617 dev->msi_enabled = 0;
619 BUG_ON(list_empty(&dev->msi_list));
620 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
621 /* Return the the pci reset with msi irqs unmasked */
622 if (entry->msi_attrib.maskbit) {
623 u32 mask = entry->msi_attrib.maskbits_mask;
624 struct irq_desc *desc = irq_to_desc(dev->irq);
625 msi_set_mask_bits(desc, mask, ~mask);
627 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
630 /* Restore dev->irq to its default pin-assertion irq */
631 dev->irq = entry->msi_attrib.default_irq;
633 void pci_disable_msi(struct pci_dev* dev)
635 struct msi_desc *entry;
637 if (!pci_msi_enable || !dev || !dev->msi_enabled)
640 pci_msi_shutdown(dev);
642 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
643 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
648 EXPORT_SYMBOL(pci_disable_msi);
650 static int msi_free_irqs(struct pci_dev* dev)
652 struct msi_desc *entry, *tmp;
654 list_for_each_entry(entry, &dev->msi_list, list) {
656 BUG_ON(irq_has_action(entry->irq));
659 arch_teardown_msi_irqs(dev);
661 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
662 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
663 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
664 * PCI_MSIX_ENTRY_SIZE
665 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
667 if (list_is_last(&entry->list, &dev->msi_list))
668 iounmap(entry->mask_base);
670 list_del(&entry->list);
678 * pci_enable_msix - configure device's MSI-X capability structure
679 * @dev: pointer to the pci_dev data structure of MSI-X device function
680 * @entries: pointer to an array of MSI-X entries
681 * @nvec: number of MSI-X irqs requested for allocation by device driver
683 * Setup the MSI-X capability structure of device function with the number
684 * of requested irqs upon its software driver call to request for
685 * MSI-X mode enabled on its hardware device function. A return of zero
686 * indicates the successful configuration of MSI-X capability structure
687 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
688 * Or a return of > 0 indicates that driver request is exceeding the number
689 * of irqs available. Driver should use the returned value to re-send
692 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
694 int status, pos, nr_entries;
701 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
705 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
706 pci_read_config_word(dev, msi_control_reg(pos), &control);
707 nr_entries = multi_msix_capable(control);
708 if (nvec > nr_entries)
711 /* Check for any invalid entries */
712 for (i = 0; i < nvec; i++) {
713 if (entries[i].entry >= nr_entries)
714 return -EINVAL; /* invalid entry */
715 for (j = i + 1; j < nvec; j++) {
716 if (entries[i].entry == entries[j].entry)
717 return -EINVAL; /* duplicate entry */
720 WARN_ON(!!dev->msix_enabled);
722 /* Check whether driver already requested for MSI irq */
723 if (dev->msi_enabled) {
724 dev_info(&dev->dev, "can't enable MSI-X "
725 "(MSI IRQ already assigned)\n");
728 status = msix_capability_init(dev, entries, nvec);
731 EXPORT_SYMBOL(pci_enable_msix);
733 static void msix_free_all_irqs(struct pci_dev *dev)
738 void pci_msix_shutdown(struct pci_dev* dev)
740 if (!pci_msi_enable || !dev || !dev->msix_enabled)
743 msix_set_enable(dev, 0);
744 pci_intx_for_msi(dev, 1);
745 dev->msix_enabled = 0;
747 void pci_disable_msix(struct pci_dev* dev)
749 if (!pci_msi_enable || !dev || !dev->msix_enabled)
752 pci_msix_shutdown(dev);
754 msix_free_all_irqs(dev);
756 EXPORT_SYMBOL(pci_disable_msix);
759 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
760 * @dev: pointer to the pci_dev data structure of MSI(X) device function
762 * Being called during hotplug remove, from which the device function
763 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
764 * allocated for this device function, are reclaimed to unused state,
765 * which may be used later on.
767 void msi_remove_pci_irq_vectors(struct pci_dev* dev)
769 if (!pci_msi_enable || !dev)
772 if (dev->msi_enabled)
775 if (dev->msix_enabled)
776 msix_free_all_irqs(dev);
779 void pci_no_msi(void)
785 * pci_msi_enabled - is MSI enabled?
787 * Returns true if MSI has not been disabled by the command-line option
790 int pci_msi_enabled(void)
792 return pci_msi_enable;
794 EXPORT_SYMBOL(pci_msi_enabled);
796 void pci_msi_init_pci_dev(struct pci_dev *dev)
798 INIT_LIST_HEAD(&dev->msi_list);