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/smp_lock.h>
16 #include <linux/pci.h>
17 #include <linux/proc_fs.h>
18 #include <linux/msi.h>
20 #include <asm/errno.h>
27 static struct kmem_cache* msi_cachep;
29 static int pci_msi_enable = 1;
31 static int msi_cache_init(void)
33 msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
34 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
41 static void msi_set_enable(struct pci_dev *dev, int enable)
46 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
48 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
49 control &= ~PCI_MSI_FLAGS_ENABLE;
51 control |= PCI_MSI_FLAGS_ENABLE;
52 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
56 static void msix_set_enable(struct pci_dev *dev, int enable)
61 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
63 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
64 control &= ~PCI_MSIX_FLAGS_ENABLE;
66 control |= PCI_MSIX_FLAGS_ENABLE;
67 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
71 static void msi_set_mask_bit(unsigned int irq, int flag)
73 struct msi_desc *entry;
75 entry = get_irq_msi(irq);
76 BUG_ON(!entry || !entry->dev);
77 switch (entry->msi_attrib.type) {
79 if (entry->msi_attrib.maskbit) {
83 pos = (long)entry->mask_base;
84 pci_read_config_dword(entry->dev, pos, &mask_bits);
87 pci_write_config_dword(entry->dev, pos, mask_bits);
89 msi_set_enable(entry->dev, !flag);
94 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
95 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
96 writel(flag, entry->mask_base + offset);
105 void read_msi_msg(unsigned int irq, struct msi_msg *msg)
107 struct msi_desc *entry = get_irq_msi(irq);
108 switch(entry->msi_attrib.type) {
111 struct pci_dev *dev = entry->dev;
112 int pos = entry->msi_attrib.pos;
115 pci_read_config_dword(dev, msi_lower_address_reg(pos),
117 if (entry->msi_attrib.is_64) {
118 pci_read_config_dword(dev, msi_upper_address_reg(pos),
120 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
123 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
128 case PCI_CAP_ID_MSIX:
131 base = entry->mask_base +
132 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
134 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
135 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
136 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
144 void write_msi_msg(unsigned int irq, struct msi_msg *msg)
146 struct msi_desc *entry = get_irq_msi(irq);
147 switch (entry->msi_attrib.type) {
150 struct pci_dev *dev = entry->dev;
151 int pos = entry->msi_attrib.pos;
153 pci_write_config_dword(dev, msi_lower_address_reg(pos),
155 if (entry->msi_attrib.is_64) {
156 pci_write_config_dword(dev, msi_upper_address_reg(pos),
158 pci_write_config_word(dev, msi_data_reg(pos, 1),
161 pci_write_config_word(dev, msi_data_reg(pos, 0),
166 case PCI_CAP_ID_MSIX:
169 base = entry->mask_base +
170 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
172 writel(msg->address_lo,
173 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
174 writel(msg->address_hi,
175 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
176 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
184 void mask_msi_irq(unsigned int irq)
186 msi_set_mask_bit(irq, 1);
189 void unmask_msi_irq(unsigned int irq)
191 msi_set_mask_bit(irq, 0);
194 static int msi_free_irq(struct pci_dev* dev, int irq);
196 static int msi_init(void)
198 static int status = -ENOMEM;
203 status = msi_cache_init();
206 printk(KERN_WARNING "PCI: MSI cache init failed\n");
213 static struct msi_desc* alloc_msi_entry(void)
215 struct msi_desc *entry;
217 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
221 entry->link.tail = entry->link.head = 0; /* single message */
228 static int __pci_save_msi_state(struct pci_dev *dev)
232 struct pci_cap_saved_state *save_state;
235 if (!dev->msi_enabled)
238 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
242 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
245 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
248 cap = &save_state->data[0];
250 pci_read_config_dword(dev, pos, &cap[i++]);
251 control = cap[0] >> 16;
252 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
253 if (control & PCI_MSI_FLAGS_64BIT) {
254 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
255 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
257 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
258 if (control & PCI_MSI_FLAGS_MASKBIT)
259 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
260 save_state->cap_nr = PCI_CAP_ID_MSI;
261 pci_add_saved_cap(dev, save_state);
265 static void __pci_restore_msi_state(struct pci_dev *dev)
269 struct pci_cap_saved_state *save_state;
272 if (!dev->msi_enabled)
275 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
276 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
277 if (!save_state || pos <= 0)
279 cap = &save_state->data[0];
281 pci_intx(dev, 0); /* disable intx */
282 control = cap[i++] >> 16;
283 msi_set_enable(dev, 0);
284 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
285 if (control & PCI_MSI_FLAGS_64BIT) {
286 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
287 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
289 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
290 if (control & PCI_MSI_FLAGS_MASKBIT)
291 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
292 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
293 pci_remove_saved_cap(save_state);
297 static int __pci_save_msix_state(struct pci_dev *dev)
300 int irq, head, tail = 0;
302 struct pci_cap_saved_state *save_state;
304 if (!dev->msix_enabled)
307 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
311 /* save the capability */
312 pci_read_config_word(dev, msi_control_reg(pos), &control);
313 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
316 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
319 *((u16 *)&save_state->data[0]) = control;
322 irq = head = dev->first_msi_irq;
323 while (head != tail) {
324 struct msi_desc *entry;
326 entry = get_irq_msi(irq);
327 read_msi_msg(irq, &entry->msg_save);
329 tail = entry->link.tail;
333 save_state->cap_nr = PCI_CAP_ID_MSIX;
334 pci_add_saved_cap(dev, save_state);
338 int pci_save_msi_state(struct pci_dev *dev)
342 rc = __pci_save_msi_state(dev);
346 rc = __pci_save_msix_state(dev);
351 static void __pci_restore_msix_state(struct pci_dev *dev)
355 int irq, head, tail = 0;
356 struct msi_desc *entry;
357 struct pci_cap_saved_state *save_state;
359 if (!dev->msix_enabled)
362 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
365 save = *((u16 *)&save_state->data[0]);
366 pci_remove_saved_cap(save_state);
369 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
373 /* route the table */
374 pci_intx(dev, 0); /* disable intx */
375 msix_set_enable(dev, 0);
376 irq = head = dev->first_msi_irq;
377 while (head != tail) {
378 entry = get_irq_msi(irq);
379 write_msi_msg(irq, &entry->msg_save);
381 tail = entry->link.tail;
385 pci_write_config_word(dev, msi_control_reg(pos), save);
388 void pci_restore_msi_state(struct pci_dev *dev)
390 __pci_restore_msi_state(dev);
391 __pci_restore_msix_state(dev);
393 #endif /* CONFIG_PM */
396 * msi_capability_init - configure device's MSI capability structure
397 * @dev: pointer to the pci_dev data structure of MSI device function
399 * Setup the MSI capability structure of device function with a single
400 * MSI irq, regardless of device function is capable of handling
401 * multiple messages. A return of zero indicates the successful setup
402 * of an entry zero with the new MSI irq or non-zero for otherwise.
404 static int msi_capability_init(struct pci_dev *dev)
406 struct msi_desc *entry;
410 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
412 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
413 pci_read_config_word(dev, msi_control_reg(pos), &control);
414 /* MSI Entry Initialization */
415 entry = alloc_msi_entry();
419 entry->msi_attrib.type = PCI_CAP_ID_MSI;
420 entry->msi_attrib.is_64 = is_64bit_address(control);
421 entry->msi_attrib.entry_nr = 0;
422 entry->msi_attrib.maskbit = is_mask_bit_support(control);
423 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
424 entry->msi_attrib.pos = pos;
425 if (is_mask_bit_support(control)) {
426 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
427 is_64bit_address(control));
430 if (entry->msi_attrib.maskbit) {
431 unsigned int maskbits, temp;
432 /* All MSIs are unmasked by default, Mask them all */
433 pci_read_config_dword(dev,
434 msi_mask_bits_reg(pos, is_64bit_address(control)),
436 temp = (1 << multi_msi_capable(control));
437 temp = ((temp - 1) & ~temp);
439 pci_write_config_dword(dev,
440 msi_mask_bits_reg(pos, is_64bit_address(control)),
443 /* Configure MSI capability structure */
444 irq = arch_setup_msi_irq(dev, entry);
446 kmem_cache_free(msi_cachep, entry);
449 entry->link.head = irq;
450 entry->link.tail = irq;
451 dev->first_msi_irq = irq;
452 set_irq_msi(irq, entry);
454 /* Set MSI enabled bits */
455 pci_intx(dev, 0); /* disable intx */
456 msi_set_enable(dev, 1);
457 dev->msi_enabled = 1;
464 * msix_capability_init - configure device's MSI-X capability
465 * @dev: pointer to the pci_dev data structure of MSI-X device function
466 * @entries: pointer to an array of struct msix_entry entries
467 * @nvec: number of @entries
469 * Setup the MSI-X capability structure of device function with a
470 * single MSI-X irq. A return of zero indicates the successful setup of
471 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
473 static int msix_capability_init(struct pci_dev *dev,
474 struct msix_entry *entries, int nvec)
476 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
477 int irq, pos, i, j, nr_entries, temp = 0;
478 unsigned long phys_addr;
484 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
486 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
487 /* Request & Map MSI-X table region */
488 pci_read_config_word(dev, msi_control_reg(pos), &control);
489 nr_entries = multi_msix_capable(control);
491 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
492 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
493 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
494 phys_addr = pci_resource_start (dev, bir) + table_offset;
495 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
499 /* MSI-X Table Initialization */
500 for (i = 0; i < nvec; i++) {
501 entry = alloc_msi_entry();
505 j = entries[i].entry;
506 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
507 entry->msi_attrib.is_64 = 1;
508 entry->msi_attrib.entry_nr = j;
509 entry->msi_attrib.maskbit = 1;
510 entry->msi_attrib.default_irq = dev->irq;
511 entry->msi_attrib.pos = pos;
513 entry->mask_base = base;
515 /* Configure MSI-X capability structure */
516 irq = arch_setup_msi_irq(dev, entry);
518 kmem_cache_free(msi_cachep, entry);
521 entries[i].vector = irq;
523 entry->link.head = irq;
524 entry->link.tail = irq;
527 entry->link.head = temp;
528 entry->link.tail = tail->link.tail;
529 tail->link.tail = irq;
530 head->link.head = irq;
535 set_irq_msi(irq, entry);
540 for (; i >= 0; i--) {
541 irq = (entries + i)->vector;
542 msi_free_irq(dev, irq);
543 (entries + i)->vector = 0;
545 /* If we had some success report the number of irqs
546 * we succeeded in setting up.
552 dev->first_msi_irq = entries[0].vector;
553 /* Set MSI-X enabled bits */
554 pci_intx(dev, 0); /* disable intx */
555 msix_set_enable(dev, 1);
556 dev->msix_enabled = 1;
562 * pci_msi_supported - check whether MSI may be enabled on device
563 * @dev: pointer to the pci_dev data structure of MSI device function
565 * Look at global flags, the device itself, and its parent busses
566 * to return 0 if MSI are supported for the device.
569 int pci_msi_supported(struct pci_dev * dev)
573 /* MSI must be globally enabled and supported by the device */
574 if (!pci_msi_enable || !dev || dev->no_msi)
577 /* Any bridge which does NOT route MSI transactions from it's
578 * secondary bus to it's primary bus must set NO_MSI flag on
579 * the secondary pci_bus.
580 * We expect only arch-specific PCI host bus controller driver
581 * or quirks for specific PCI bridges to be setting NO_MSI.
583 for (bus = dev->bus; bus; bus = bus->parent)
584 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
591 * pci_enable_msi - configure device's MSI capability structure
592 * @dev: pointer to the pci_dev data structure of MSI device function
594 * Setup the MSI capability structure of device function with
595 * a single MSI irq upon its software driver call to request for
596 * MSI mode enabled on its hardware device function. A return of zero
597 * indicates the successful setup of an entry zero with the new MSI
598 * irq or non-zero for otherwise.
600 int pci_enable_msi(struct pci_dev* dev)
604 if (pci_msi_supported(dev) < 0)
611 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
615 WARN_ON(!!dev->msi_enabled);
617 /* Check whether driver already requested for MSI-X irqs */
618 if (dev->msix_enabled) {
619 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
620 "Device already has MSI-X enabled\n",
624 status = msi_capability_init(dev);
628 void pci_disable_msi(struct pci_dev* dev)
630 struct msi_desc *entry;
638 if (!dev->msi_enabled)
641 msi_set_enable(dev, 0);
642 pci_intx(dev, 1); /* enable intx */
643 dev->msi_enabled = 0;
645 entry = get_irq_msi(dev->first_msi_irq);
646 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
649 if (irq_has_action(dev->first_msi_irq)) {
650 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
651 "free_irq() on MSI irq %d\n",
652 pci_name(dev), dev->first_msi_irq);
653 BUG_ON(irq_has_action(dev->first_msi_irq));
655 default_irq = entry->msi_attrib.default_irq;
656 msi_free_irq(dev, dev->first_msi_irq);
658 /* Restore dev->irq to its default pin-assertion irq */
659 dev->irq = default_irq;
661 dev->first_msi_irq = 0;
664 static int msi_free_irq(struct pci_dev* dev, int irq)
666 struct msi_desc *entry;
667 int head, entry_nr, type;
670 entry = get_irq_msi(irq);
671 if (!entry || entry->dev != dev) {
674 type = entry->msi_attrib.type;
675 entry_nr = entry->msi_attrib.entry_nr;
676 head = entry->link.head;
677 base = entry->mask_base;
678 get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
679 get_irq_msi(entry->link.tail)->link.head = entry->link.head;
681 arch_teardown_msi_irq(irq);
682 kmem_cache_free(msi_cachep, entry);
684 if (type == PCI_CAP_ID_MSIX) {
685 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
686 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
696 * pci_enable_msix - configure device's MSI-X capability structure
697 * @dev: pointer to the pci_dev data structure of MSI-X device function
698 * @entries: pointer to an array of MSI-X entries
699 * @nvec: number of MSI-X irqs requested for allocation by device driver
701 * Setup the MSI-X capability structure of device function with the number
702 * of requested irqs upon its software driver call to request for
703 * MSI-X mode enabled on its hardware device function. A return of zero
704 * indicates the successful configuration of MSI-X capability structure
705 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
706 * Or a return of > 0 indicates that driver request is exceeding the number
707 * of irqs available. Driver should use the returned value to re-send
710 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
712 int status, pos, nr_entries;
716 if (!entries || pci_msi_supported(dev) < 0)
723 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
727 pci_read_config_word(dev, msi_control_reg(pos), &control);
728 nr_entries = multi_msix_capable(control);
729 if (nvec > nr_entries)
732 /* Check for any invalid entries */
733 for (i = 0; i < nvec; i++) {
734 if (entries[i].entry >= nr_entries)
735 return -EINVAL; /* invalid entry */
736 for (j = i + 1; j < nvec; j++) {
737 if (entries[i].entry == entries[j].entry)
738 return -EINVAL; /* duplicate entry */
741 WARN_ON(!!dev->msix_enabled);
743 /* Check whether driver already requested for MSI irq */
744 if (dev->msi_enabled) {
745 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
746 "Device already has an MSI irq assigned\n",
750 status = msix_capability_init(dev, entries, nvec);
754 void pci_disable_msix(struct pci_dev* dev)
756 int irq, head, tail = 0, warning = 0;
763 if (!dev->msix_enabled)
766 msix_set_enable(dev, 0);
767 pci_intx(dev, 1); /* enable intx */
768 dev->msix_enabled = 0;
770 irq = head = dev->first_msi_irq;
771 while (head != tail) {
772 tail = get_irq_msi(irq)->link.tail;
773 if (irq_has_action(irq))
775 else if (irq != head) /* Release MSI-X irq */
776 msi_free_irq(dev, irq);
779 msi_free_irq(dev, irq);
781 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
782 "free_irq() on all MSI-X irqs\n",
786 dev->first_msi_irq = 0;
790 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
791 * @dev: pointer to the pci_dev data structure of MSI(X) device function
793 * Being called during hotplug remove, from which the device function
794 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
795 * allocated for this device function, are reclaimed to unused state,
796 * which may be used later on.
798 void msi_remove_pci_irq_vectors(struct pci_dev* dev)
800 if (!pci_msi_enable || !dev)
803 if (dev->msi_enabled) {
804 if (irq_has_action(dev->first_msi_irq)) {
805 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
806 "called without free_irq() on MSI irq %d\n",
807 pci_name(dev), dev->first_msi_irq);
808 BUG_ON(irq_has_action(dev->first_msi_irq));
809 } else /* Release MSI irq assigned to this device */
810 msi_free_irq(dev, dev->first_msi_irq);
812 if (dev->msix_enabled) {
813 int irq, head, tail = 0, warning = 0;
814 void __iomem *base = NULL;
816 irq = head = dev->first_msi_irq;
817 while (head != tail) {
818 tail = get_irq_msi(irq)->link.tail;
819 base = get_irq_msi(irq)->mask_base;
820 if (irq_has_action(irq))
822 else if (irq != head) /* Release MSI-X irq */
823 msi_free_irq(dev, irq);
826 msi_free_irq(dev, irq);
829 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
830 "called without free_irq() on all MSI-X irqs\n",
837 void pci_no_msi(void)
842 EXPORT_SYMBOL(pci_enable_msi);
843 EXPORT_SYMBOL(pci_disable_msi);
844 EXPORT_SYMBOL(pci_enable_msix);
845 EXPORT_SYMBOL(pci_disable_msix);