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>
19 #include <linux/errno.h>
22 #include <xen/evtchn.h>
27 static int pci_msi_enable = 1;
29 static LIST_HEAD(msi_dev_head);
30 DEFINE_SPINLOCK(msi_dev_lock);
34 struct list_head list;
35 spinlock_t pirq_list_lock;
36 struct list_head pirq_list_head;
37 /* Store default pre-assigned irq */
38 unsigned int default_irq;
41 struct msi_pirq_entry {
42 struct list_head list;
49 #ifndef arch_msi_check_device
50 int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
56 #ifndef arch_setup_msi_irqs
57 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
59 struct msi_desc *entry;
63 * If an architecture wants to support multiple MSI, it needs to
64 * override arch_setup_msi_irqs()
66 if (type == PCI_CAP_ID_MSI && nvec > 1)
69 list_for_each_entry(entry, &dev->msi_list, list) {
70 ret = arch_setup_msi_irq(dev, entry);
81 #ifndef arch_teardown_msi_irqs
82 void arch_teardown_msi_irqs(struct pci_dev *dev)
84 struct msi_desc *entry;
86 list_for_each_entry(entry, &dev->msi_list, list) {
90 nvec = 1 << entry->msi_attrib.multiple;
91 for (i = 0; i < nvec; i++)
92 arch_teardown_msi_irq(entry->irq + i);
97 static void msi_set_enable(struct pci_dev *dev, int pos, int enable)
103 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
104 control &= ~PCI_MSI_FLAGS_ENABLE;
106 control |= PCI_MSI_FLAGS_ENABLE;
107 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
110 static void msix_set_enable(struct pci_dev *dev, int enable)
115 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
117 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
118 control &= ~PCI_MSIX_FLAGS_ENABLE;
120 control |= PCI_MSIX_FLAGS_ENABLE;
121 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
125 static struct msi_dev_list *get_msi_dev_pirq_list(struct pci_dev *dev)
127 struct msi_dev_list *msi_dev_list, *ret = NULL;
130 spin_lock_irqsave(&msi_dev_lock, flags);
132 list_for_each_entry(msi_dev_list, &msi_dev_head, list)
133 if ( msi_dev_list->dev == dev )
137 spin_unlock_irqrestore(&msi_dev_lock, flags);
141 /* Has not allocate msi_dev until now. */
142 ret = kzalloc(sizeof(struct msi_dev_list), GFP_ATOMIC);
144 /* Failed to allocate msi_dev structure */
146 spin_unlock_irqrestore(&msi_dev_lock, flags);
151 spin_lock_init(&ret->pirq_list_lock);
152 INIT_LIST_HEAD(&ret->pirq_list_head);
153 list_add_tail(&ret->list, &msi_dev_head);
154 spin_unlock_irqrestore(&msi_dev_lock, flags);
158 static int attach_pirq_entry(int pirq, int entry_nr,
159 struct msi_dev_list *msi_dev_entry)
161 struct msi_pirq_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
167 entry->entry_nr = entry_nr;
168 spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
169 list_add_tail(&entry->list, &msi_dev_entry->pirq_list_head);
170 spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
174 static void detach_pirq_entry(int entry_nr,
175 struct msi_dev_list *msi_dev_entry)
178 struct msi_pirq_entry *pirq_entry;
180 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
181 if (pirq_entry->entry_nr == entry_nr) {
182 spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
183 list_del(&pirq_entry->list);
184 spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
192 * pciback will provide device's owner
194 static int (*get_owner)(struct pci_dev *dev);
196 int register_msi_get_owner(int (*func)(struct pci_dev *dev))
199 printk(KERN_WARNING "register msi_get_owner again\n");
205 EXPORT_SYMBOL(register_msi_get_owner);
207 int unregister_msi_get_owner(int (*func)(struct pci_dev *dev))
209 if (get_owner != func)
214 EXPORT_SYMBOL(unregister_msi_get_owner);
216 static int msi_get_dev_owner(struct pci_dev *dev)
220 BUG_ON(!is_initial_xendomain());
221 if (get_owner && (owner = get_owner(dev)) >= 0) {
222 dev_info(&dev->dev, "get owner: %x \n", owner);
229 static int msi_unmap_pirq(struct pci_dev *dev, int pirq)
231 struct physdev_unmap_pirq unmap;
234 unmap.domid = msi_get_dev_owner(dev);
235 /* See comments in msi_map_vector, input parameter pirq means
236 * irq number only if the device belongs to dom0 itself.
238 unmap.pirq = (unmap.domid != DOMID_SELF)
239 ? pirq : evtchn_get_xen_pirq(pirq);
241 if ((rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap)))
242 dev_warn(&dev->dev, "unmap irq %x failed\n", pirq);
247 if (unmap.domid == DOMID_SELF)
248 evtchn_map_pirq(pirq, 0);
253 static u64 find_table_base(struct pci_dev *dev, int pos)
259 pci_read_config_dword(dev, msix_table_offset_reg(pos), ®);
260 bar = reg & PCI_MSIX_FLAGS_BIRMASK;
262 flags = pci_resource_flags(dev, bar);
263 if (flags & (IORESOURCE_DISABLED | IORESOURCE_UNSET | IORESOURCE_BUSY))
266 return pci_resource_start(dev, bar);
270 * Protected by msi_lock
272 static int msi_map_vector(struct pci_dev *dev, int entry_nr, u64 table_base)
274 struct physdev_map_pirq map_irq;
276 domid_t domid = DOMID_SELF;
278 domid = msi_get_dev_owner(dev);
280 map_irq.domid = domid;
281 map_irq.type = MAP_PIRQ_TYPE_MSI;
284 map_irq.bus = dev->bus->number;
285 map_irq.devfn = dev->devfn;
286 map_irq.entry_nr = entry_nr;
287 map_irq.table_base = table_base;
289 if ((rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq)))
290 dev_warn(&dev->dev, "map irq failed\n");
294 /* This happens when MSI support is not enabled in older Xen. */
295 if (rc == 0 && map_irq.pirq < 0)
298 BUG_ON(map_irq.pirq <= 0);
300 /* If mapping of this particular MSI is on behalf of another domain,
301 * we do not need to get an irq in dom0. This also implies:
302 * dev->irq in dom0 will be 'Xen pirq' if this device belongs to
303 * to another domain, and will be 'Linux irq' if it belongs to dom0.
305 if (domid == DOMID_SELF) {
306 rc = evtchn_map_pirq(-1, map_irq.pirq);
307 dev_printk(KERN_DEBUG, &dev->dev,
308 "irq %d (%d) for MSI/MSI-X\n",
312 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for dom%d MSI/MSI-X\n",
313 map_irq.pirq, domid);
317 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
319 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
320 pci_intx(dev, enable);
323 void pci_restore_msi_state(struct pci_dev *dev)
326 struct physdev_restore_msi restore;
328 if (!dev->msi_enabled && !dev->msix_enabled)
331 pci_intx_for_msi(dev, 0);
332 if (dev->msi_enabled) {
333 int pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
335 msi_set_enable(dev, pos, 0);
337 if (dev->msix_enabled)
338 msix_set_enable(dev, 0);
340 restore.bus = dev->bus->number;
341 restore.devfn = dev->devfn;
342 rc = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi, &restore);
343 WARN(rc && rc != -ENOSYS, "restore_msi -> %d\n", rc);
345 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
348 * msi_capability_init - configure device's MSI capability structure
349 * @dev: pointer to the pci_dev data structure of MSI device function
350 * @nvec: number of interrupts to allocate
352 * Setup the MSI capability structure of the device with the requested
353 * number of interrupts. A return value of zero indicates the successful
354 * setup of an entry with the new MSI irq. A negative return value indicates
355 * an error, and a positive return value indicates the number of interrupts
356 * which could have been allocated.
358 static int msi_capability_init(struct pci_dev *dev, int nvec)
363 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
364 msi_set_enable(dev, pos, 0); /* Disable MSI during set up */
366 pci_read_config_word(dev, msi_control_reg(pos), &control);
368 WARN_ON(nvec > 1); /* XXX */
369 pirq = msi_map_vector(dev, 0, 0);
373 /* Set MSI enabled bits */
374 pci_intx_for_msi(dev, 0);
375 msi_set_enable(dev, pos, 1);
376 dev->msi_enabled = 1;
383 * msix_capability_init - configure device's MSI-X capability
384 * @dev: pointer to the pci_dev data structure of MSI-X device function
385 * @entries: pointer to an array of struct msix_entry entries
386 * @nvec: number of @entries
388 * Setup the MSI-X capability structure of device function with a
389 * single MSI-X irq. A return of zero indicates the successful setup of
390 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
392 static int msix_capability_init(struct pci_dev *dev,
393 struct msix_entry *entries, int nvec)
396 int pirq, i, j, mapped, pos;
398 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
399 struct msi_pirq_entry *pirq_entry;
404 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
406 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
407 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
409 /* Ensure MSI-X is disabled while it is set up */
410 control &= ~PCI_MSIX_FLAGS_ENABLE;
411 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
413 table_base = find_table_base(dev, pos);
418 * Some devices require MSI-X to be enabled before we can touch the
419 * MSI-X registers. We need to mask all the vectors to prevent
420 * interrupts coming in before they're fully set up.
422 control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
423 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
425 for (i = 0; i < nvec; i++) {
427 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
428 if (pirq_entry->entry_nr == entries[i].entry) {
430 "msix entry %d was not freed\n",
432 (entries + i)->vector = pirq_entry->pirq;
439 pirq = msi_map_vector(dev, entries[i].entry, table_base);
442 attach_pirq_entry(pirq, entries[i].entry, msi_dev_entry);
443 (entries + i)->vector = pirq;
448 for (j = --i; j >= 0; j--) {
449 msi_unmap_pirq(dev, entries[j].vector);
450 detach_pirq_entry(entries[j].entry, msi_dev_entry);
451 entries[j].vector = 0;
453 /* If we had some success report the number of irqs
454 * we succeeded in setting up.
461 /* Set MSI-X enabled bits and unmask the function */
462 pci_intx_for_msi(dev, 0);
463 dev->msix_enabled = 1;
465 control &= ~PCI_MSIX_FLAGS_MASKALL;
466 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
472 * pci_msi_check_device - check whether MSI may be enabled on a device
473 * @dev: pointer to the pci_dev data structure of MSI device function
474 * @nvec: how many MSIs have been requested ?
475 * @type: are we checking for MSI or MSI-X ?
477 * Look at global flags, the device itself, and its parent busses
478 * to determine if MSI/-X are supported for the device. If MSI/-X is
479 * supported return 0, else return an error code.
481 static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
486 /* MSI must be globally enabled and supported by the device */
487 if (!pci_msi_enable || !dev || dev->no_msi)
491 * You can't ask to have 0 or less MSIs configured.
493 * b) the list manipulation code assumes nvec >= 1.
499 * Any bridge which does NOT route MSI transactions from its
500 * secondary bus to its primary bus must set NO_MSI flag on
501 * the secondary pci_bus.
502 * We expect only arch-specific PCI host bus controller driver
503 * or quirks for specific PCI bridges to be setting NO_MSI.
505 for (bus = dev->bus; bus; bus = bus->parent)
506 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
509 ret = arch_msi_check_device(dev, nvec, type);
513 if (!pci_find_capability(dev, type))
520 * pci_enable_msi_block - configure device's MSI capability structure
521 * @dev: device to configure
522 * @nvec: number of interrupts to configure
524 * Allocate IRQs for a device with the MSI capability.
525 * This function returns a negative errno if an error occurs. If it
526 * is unable to allocate the number of interrupts requested, it returns
527 * the number of interrupts it might be able to allocate. If it successfully
528 * allocates at least the number of interrupts requested, it returns 0 and
529 * updates the @dev's irq member to the lowest new interrupt number; the
530 * other interrupt numbers allocated to this device are consecutive.
532 extern int pci_frontend_enable_msi(struct pci_dev *dev);
533 int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
535 int temp, status, pos, maxvec;
537 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
539 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
542 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
543 maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
547 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
551 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
552 if (!is_initial_xendomain())
557 WARN_ON(nvec > 1); /* XXX */
558 ret = pci_frontend_enable_msi(dev);
562 dev->irq = evtchn_map_pirq(-1, dev->irq);
563 dev->msi_enabled = 1;
564 msi_dev_entry->default_irq = temp;
572 /* Check whether driver already requested MSI-X irqs */
573 if (dev->msix_enabled) {
574 dev_info(&dev->dev, "can't enable MSI "
575 "(MSI-X already enabled)\n");
579 status = msi_capability_init(dev, nvec);
581 msi_dev_entry->default_irq = temp;
585 EXPORT_SYMBOL(pci_enable_msi_block);
587 extern void pci_frontend_disable_msi(struct pci_dev* dev);
588 void pci_msi_shutdown(struct pci_dev *dev)
591 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
593 if (!pci_msi_enable || !dev || !dev->msi_enabled)
596 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
597 if (!is_initial_xendomain()) {
598 evtchn_map_pirq(dev->irq, 0);
599 pci_frontend_disable_msi(dev);
600 dev->irq = msi_dev_entry->default_irq;
601 dev->msi_enabled = 0;
607 /* Restore dev->irq to its default pin-assertion vector */
608 dev->irq = msi_dev_entry->default_irq;
609 msi_unmap_pirq(dev, pirq);
611 /* Disable MSI mode */
612 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
613 msi_set_enable(dev, pos, 0);
614 pci_intx_for_msi(dev, 1);
615 dev->msi_enabled = 0;
618 void pci_disable_msi(struct pci_dev *dev)
620 pci_msi_shutdown(dev);
622 EXPORT_SYMBOL(pci_disable_msi);
625 * pci_msix_table_size - return the number of device's MSI-X table entries
626 * @dev: pointer to the pci_dev data structure of MSI-X device function
628 int pci_msix_table_size(struct pci_dev *dev)
633 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
637 pci_read_config_word(dev, msi_control_reg(pos), &control);
638 return multi_msix_capable(control);
642 * pci_enable_msix - configure device's MSI-X capability structure
643 * @dev: pointer to the pci_dev data structure of MSI-X device function
644 * @entries: pointer to an array of MSI-X entries
645 * @nvec: number of MSI-X irqs requested for allocation by device driver
647 * Setup the MSI-X capability structure of device function with the number
648 * of requested irqs upon its software driver call to request for
649 * MSI-X mode enabled on its hardware device function. A return of zero
650 * indicates the successful configuration of MSI-X capability structure
651 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
652 * Or a return of > 0 indicates that driver request is exceeding the number
653 * of irqs or MSI-X vectors available. Driver should use the returned value to
654 * re-send its request.
656 extern int pci_frontend_enable_msix(struct pci_dev *dev,
657 struct msix_entry *entries, int nvec);
658 int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
660 int status, nr_entries;
662 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
667 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
668 if (!is_initial_xendomain()) {
669 struct msi_pirq_entry *pirq_entry;
673 ret = pci_frontend_enable_msix(dev, entries, nvec);
676 "got %x from frontend_enable_msix\n", ret);
679 dev->msix_enabled = 1;
680 msi_dev_entry->default_irq = temp;
682 for (i = 0; i < nvec; i++) {
685 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
686 if (pirq_entry->entry_nr == entries[i].entry) {
687 irq = pirq_entry->pirq;
688 BUG_ON(entries[i].vector != evtchn_get_xen_pirq(irq));
689 entries[i].vector = irq;
696 irq = evtchn_map_pirq(-1, entries[i].vector);
697 attach_pirq_entry(irq, entries[i].entry, msi_dev_entry);
698 entries[i].vector = irq;
704 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
708 nr_entries = pci_msix_table_size(dev);
709 if (nvec > nr_entries)
712 /* Check for any invalid entries */
713 for (i = 0; i < nvec; i++) {
714 if (entries[i].entry >= nr_entries)
715 return -EINVAL; /* invalid entry */
716 for (j = i + 1; j < nvec; j++) {
717 if (entries[i].entry == entries[j].entry)
718 return -EINVAL; /* duplicate entry */
723 /* Check whether driver already requested for MSI vector */
724 if (dev->msi_enabled) {
725 dev_info(&dev->dev, "can't enable MSI-X "
726 "(MSI IRQ already assigned)\n");
730 status = msix_capability_init(dev, entries, nvec);
733 msi_dev_entry->default_irq = temp;
737 EXPORT_SYMBOL(pci_enable_msix);
739 extern void pci_frontend_disable_msix(struct pci_dev* dev);
740 void pci_msix_shutdown(struct pci_dev *dev)
742 if (!pci_msi_enable || !dev || !dev->msix_enabled)
745 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
746 if (!is_initial_xendomain()) {
747 struct msi_dev_list *msi_dev_entry;
748 struct msi_pirq_entry *pirq_entry, *tmp;
750 pci_frontend_disable_msix(dev);
752 msi_dev_entry = get_msi_dev_pirq_list(dev);
753 list_for_each_entry_safe(pirq_entry, tmp,
754 &msi_dev_entry->pirq_list_head, list) {
755 evtchn_map_pirq(pirq_entry->pirq, 0);
756 list_del(&pirq_entry->list);
760 dev->irq = msi_dev_entry->default_irq;
761 dev->msix_enabled = 0;
766 msi_remove_pci_irq_vectors(dev);
768 /* Disable MSI mode */
769 msix_set_enable(dev, 0);
770 pci_intx_for_msi(dev, 1);
771 dev->msix_enabled = 0;
774 void pci_disable_msix(struct pci_dev *dev)
776 pci_msix_shutdown(dev);
778 EXPORT_SYMBOL(pci_disable_msix);
781 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
782 * @dev: pointer to the pci_dev data structure of MSI(X) device function
784 * Being called during hotplug remove, from which the device function
785 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
786 * allocated for this device function, are reclaimed to unused state,
787 * which may be used later on.
789 void msi_remove_pci_irq_vectors(struct pci_dev *dev)
792 struct msi_dev_list *msi_dev_entry;
793 struct msi_pirq_entry *pirq_entry, *tmp;
795 if (!pci_msi_enable || !dev)
798 msi_dev_entry = get_msi_dev_pirq_list(dev);
800 spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
801 if (!list_empty(&msi_dev_entry->pirq_list_head))
802 list_for_each_entry_safe(pirq_entry, tmp,
803 &msi_dev_entry->pirq_list_head, list) {
804 msi_unmap_pirq(dev, pirq_entry->pirq);
805 list_del(&pirq_entry->list);
808 spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
809 dev->irq = msi_dev_entry->default_irq;
812 void pci_no_msi(void)
818 * pci_msi_enabled - is MSI enabled?
820 * Returns true if MSI has not been disabled by the command-line option
823 int pci_msi_enabled(void)
825 return pci_msi_enable;
827 EXPORT_SYMBOL(pci_msi_enabled);
829 void pci_msi_init_pci_dev(struct pci_dev *dev)
832 INIT_LIST_HEAD(&dev->msi_list);