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>
21 #include <linux/slab.h>
23 #include <xen/evtchn.h>
28 static int pci_msi_enable = 1;
30 static LIST_HEAD(msi_dev_head);
31 DEFINE_SPINLOCK(msi_dev_lock);
35 struct list_head list;
36 spinlock_t pirq_list_lock;
37 struct list_head pirq_list_head;
38 /* Store default pre-assigned irq */
39 unsigned int default_irq;
42 struct msi_pirq_entry {
43 struct list_head list;
50 #ifndef arch_msi_check_device
51 int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
57 #ifndef arch_setup_msi_irqs
58 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
60 struct msi_desc *entry;
64 * If an architecture wants to support multiple MSI, it needs to
65 * override arch_setup_msi_irqs()
67 if (type == PCI_CAP_ID_MSI && nvec > 1)
70 list_for_each_entry(entry, &dev->msi_list, list) {
71 ret = arch_setup_msi_irq(dev, entry);
82 #ifndef arch_teardown_msi_irqs
83 void arch_teardown_msi_irqs(struct pci_dev *dev)
85 struct msi_desc *entry;
87 list_for_each_entry(entry, &dev->msi_list, list) {
91 nvec = 1 << entry->msi_attrib.multiple;
92 for (i = 0; i < nvec; i++)
93 arch_teardown_msi_irq(entry->irq + i);
98 static void msi_set_enable(struct pci_dev *dev, int pos, int enable)
104 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
105 control &= ~PCI_MSI_FLAGS_ENABLE;
107 control |= PCI_MSI_FLAGS_ENABLE;
108 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
111 static void msix_set_enable(struct pci_dev *dev, int enable)
116 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
118 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
119 control &= ~PCI_MSIX_FLAGS_ENABLE;
121 control |= PCI_MSIX_FLAGS_ENABLE;
122 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
126 static struct msi_dev_list *get_msi_dev_pirq_list(struct pci_dev *dev)
128 struct msi_dev_list *msi_dev_list, *ret = NULL;
131 spin_lock_irqsave(&msi_dev_lock, flags);
133 list_for_each_entry(msi_dev_list, &msi_dev_head, list)
134 if ( msi_dev_list->dev == dev )
138 spin_unlock_irqrestore(&msi_dev_lock, flags);
142 /* Has not allocate msi_dev until now. */
143 ret = kzalloc(sizeof(struct msi_dev_list), GFP_ATOMIC);
145 /* Failed to allocate msi_dev structure */
147 spin_unlock_irqrestore(&msi_dev_lock, flags);
152 spin_lock_init(&ret->pirq_list_lock);
153 INIT_LIST_HEAD(&ret->pirq_list_head);
154 list_add_tail(&ret->list, &msi_dev_head);
155 spin_unlock_irqrestore(&msi_dev_lock, flags);
159 static int attach_pirq_entry(int pirq, int entry_nr,
160 struct msi_dev_list *msi_dev_entry)
162 struct msi_pirq_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
168 entry->entry_nr = entry_nr;
169 spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
170 list_add_tail(&entry->list, &msi_dev_entry->pirq_list_head);
171 spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
175 static void detach_pirq_entry(int entry_nr,
176 struct msi_dev_list *msi_dev_entry)
179 struct msi_pirq_entry *pirq_entry;
181 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
182 if (pirq_entry->entry_nr == entry_nr) {
183 spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
184 list_del(&pirq_entry->list);
185 spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
193 * pciback will provide device's owner
195 static int (*get_owner)(struct pci_dev *dev);
197 int register_msi_get_owner(int (*func)(struct pci_dev *dev))
200 printk(KERN_WARNING "register msi_get_owner again\n");
206 EXPORT_SYMBOL(register_msi_get_owner);
208 int unregister_msi_get_owner(int (*func)(struct pci_dev *dev))
210 if (get_owner != func)
215 EXPORT_SYMBOL(unregister_msi_get_owner);
217 static int msi_get_dev_owner(struct pci_dev *dev)
221 BUG_ON(!is_initial_xendomain());
222 if (get_owner && (owner = get_owner(dev)) >= 0) {
223 dev_info(&dev->dev, "get owner: %x \n", owner);
230 static int msi_unmap_pirq(struct pci_dev *dev, int pirq)
232 struct physdev_unmap_pirq unmap;
235 unmap.domid = msi_get_dev_owner(dev);
236 /* See comments in msi_map_vector, input parameter pirq means
237 * irq number only if the device belongs to dom0 itself.
239 unmap.pirq = (unmap.domid != DOMID_SELF)
240 ? pirq : evtchn_get_xen_pirq(pirq);
242 if ((rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap)))
243 dev_warn(&dev->dev, "unmap irq %x failed\n", pirq);
248 if (unmap.domid == DOMID_SELF)
249 evtchn_map_pirq(pirq, 0);
254 static u64 find_table_base(struct pci_dev *dev, int pos)
260 pci_read_config_dword(dev, msix_table_offset_reg(pos), ®);
261 bar = reg & PCI_MSIX_FLAGS_BIRMASK;
263 flags = pci_resource_flags(dev, bar);
264 if (flags & (IORESOURCE_DISABLED | IORESOURCE_UNSET | IORESOURCE_BUSY))
267 return pci_resource_start(dev, bar);
271 * Protected by msi_lock
273 static int msi_map_vector(struct pci_dev *dev, int entry_nr, u64 table_base)
275 struct physdev_map_pirq map_irq;
277 domid_t domid = DOMID_SELF;
279 domid = msi_get_dev_owner(dev);
281 map_irq.domid = domid;
282 map_irq.type = MAP_PIRQ_TYPE_MSI;
285 map_irq.bus = dev->bus->number;
286 map_irq.devfn = dev->devfn;
287 map_irq.entry_nr = entry_nr;
288 map_irq.table_base = table_base;
290 if ((rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq)))
291 dev_warn(&dev->dev, "map irq failed\n");
295 /* This happens when MSI support is not enabled in older Xen. */
296 if (rc == 0 && map_irq.pirq < 0)
299 BUG_ON(map_irq.pirq <= 0);
301 /* If mapping of this particular MSI is on behalf of another domain,
302 * we do not need to get an irq in dom0. This also implies:
303 * dev->irq in dom0 will be 'Xen pirq' if this device belongs to
304 * to another domain, and will be 'Linux irq' if it belongs to dom0.
306 if (domid == DOMID_SELF) {
307 rc = evtchn_map_pirq(-1, map_irq.pirq);
308 dev_printk(KERN_DEBUG, &dev->dev,
309 "irq %d (%d) for MSI/MSI-X\n",
313 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for dom%d MSI/MSI-X\n",
314 map_irq.pirq, domid);
318 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
320 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
321 pci_intx(dev, enable);
324 void pci_restore_msi_state(struct pci_dev *dev)
327 struct physdev_restore_msi restore;
329 if (!dev->msi_enabled && !dev->msix_enabled)
332 pci_intx_for_msi(dev, 0);
333 if (dev->msi_enabled) {
334 int pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
336 msi_set_enable(dev, pos, 0);
338 if (dev->msix_enabled)
339 msix_set_enable(dev, 0);
341 restore.bus = dev->bus->number;
342 restore.devfn = dev->devfn;
343 rc = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi, &restore);
344 WARN(rc && rc != -ENOSYS, "restore_msi -> %d\n", rc);
346 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
349 * msi_capability_init - configure device's MSI capability structure
350 * @dev: pointer to the pci_dev data structure of MSI device function
351 * @nvec: number of interrupts to allocate
353 * Setup the MSI capability structure of the device with the requested
354 * number of interrupts. A return value of zero indicates the successful
355 * setup of an entry with the new MSI irq. A negative return value indicates
356 * an error, and a positive return value indicates the number of interrupts
357 * which could have been allocated.
359 static int msi_capability_init(struct pci_dev *dev, int nvec)
364 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
365 msi_set_enable(dev, pos, 0); /* Disable MSI during set up */
367 pci_read_config_word(dev, msi_control_reg(pos), &control);
369 WARN_ON(nvec > 1); /* XXX */
370 pirq = msi_map_vector(dev, 0, 0);
374 /* Set MSI enabled bits */
375 pci_intx_for_msi(dev, 0);
376 msi_set_enable(dev, pos, 1);
377 dev->msi_enabled = 1;
384 * msix_capability_init - configure device's MSI-X capability
385 * @dev: pointer to the pci_dev data structure of MSI-X device function
386 * @entries: pointer to an array of struct msix_entry entries
387 * @nvec: number of @entries
389 * Setup the MSI-X capability structure of device function with a
390 * single MSI-X irq. A return of zero indicates the successful setup of
391 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
393 static int msix_capability_init(struct pci_dev *dev,
394 struct msix_entry *entries, int nvec)
397 int pirq, i, j, mapped, pos;
399 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
400 struct msi_pirq_entry *pirq_entry;
405 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
407 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
408 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
410 /* Ensure MSI-X is disabled while it is set up */
411 control &= ~PCI_MSIX_FLAGS_ENABLE;
412 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
414 table_base = find_table_base(dev, pos);
419 * Some devices require MSI-X to be enabled before we can touch the
420 * MSI-X registers. We need to mask all the vectors to prevent
421 * interrupts coming in before they're fully set up.
423 control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
424 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
426 for (i = 0; i < nvec; i++) {
428 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
429 if (pirq_entry->entry_nr == entries[i].entry) {
431 "msix entry %d was not freed\n",
433 (entries + i)->vector = pirq_entry->pirq;
440 pirq = msi_map_vector(dev, entries[i].entry, table_base);
443 attach_pirq_entry(pirq, entries[i].entry, msi_dev_entry);
444 (entries + i)->vector = pirq;
449 for (j = --i; j >= 0; j--) {
450 msi_unmap_pirq(dev, entries[j].vector);
451 detach_pirq_entry(entries[j].entry, msi_dev_entry);
452 entries[j].vector = 0;
454 /* If we had some success report the number of irqs
455 * we succeeded in setting up.
462 /* Set MSI-X enabled bits and unmask the function */
463 pci_intx_for_msi(dev, 0);
464 dev->msix_enabled = 1;
466 control &= ~PCI_MSIX_FLAGS_MASKALL;
467 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
473 * pci_msi_check_device - check whether MSI may be enabled on a device
474 * @dev: pointer to the pci_dev data structure of MSI device function
475 * @nvec: how many MSIs have been requested ?
476 * @type: are we checking for MSI or MSI-X ?
478 * Look at global flags, the device itself, and its parent busses
479 * to determine if MSI/-X are supported for the device. If MSI/-X is
480 * supported return 0, else return an error code.
482 static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
487 /* MSI must be globally enabled and supported by the device */
488 if (!pci_msi_enable || !dev || dev->no_msi)
492 * You can't ask to have 0 or less MSIs configured.
494 * b) the list manipulation code assumes nvec >= 1.
500 * Any bridge which does NOT route MSI transactions from its
501 * secondary bus to its primary bus must set NO_MSI flag on
502 * the secondary pci_bus.
503 * We expect only arch-specific PCI host bus controller driver
504 * or quirks for specific PCI bridges to be setting NO_MSI.
506 for (bus = dev->bus; bus; bus = bus->parent)
507 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
510 ret = arch_msi_check_device(dev, nvec, type);
514 if (!pci_find_capability(dev, type))
521 * pci_enable_msi_block - configure device's MSI capability structure
522 * @dev: device to configure
523 * @nvec: number of interrupts to configure
525 * Allocate IRQs for a device with the MSI capability.
526 * This function returns a negative errno if an error occurs. If it
527 * is unable to allocate the number of interrupts requested, it returns
528 * the number of interrupts it might be able to allocate. If it successfully
529 * allocates at least the number of interrupts requested, it returns 0 and
530 * updates the @dev's irq member to the lowest new interrupt number; the
531 * other interrupt numbers allocated to this device are consecutive.
533 extern int pci_frontend_enable_msi(struct pci_dev *dev);
534 int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
536 int temp, status, pos, maxvec;
538 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
540 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
543 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
544 maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
548 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
552 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
553 if (!is_initial_xendomain())
558 WARN_ON(nvec > 1); /* XXX */
559 ret = pci_frontend_enable_msi(dev);
563 dev->irq = evtchn_map_pirq(-1, dev->irq);
564 dev->msi_enabled = 1;
565 msi_dev_entry->default_irq = temp;
573 /* Check whether driver already requested MSI-X irqs */
574 if (dev->msix_enabled) {
575 dev_info(&dev->dev, "can't enable MSI "
576 "(MSI-X already enabled)\n");
580 status = msi_capability_init(dev, nvec);
582 msi_dev_entry->default_irq = temp;
586 EXPORT_SYMBOL(pci_enable_msi_block);
588 extern void pci_frontend_disable_msi(struct pci_dev* dev);
589 void pci_msi_shutdown(struct pci_dev *dev)
592 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
594 if (!pci_msi_enable || !dev || !dev->msi_enabled)
597 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
598 if (!is_initial_xendomain()) {
599 evtchn_map_pirq(dev->irq, 0);
600 pci_frontend_disable_msi(dev);
601 dev->irq = msi_dev_entry->default_irq;
602 dev->msi_enabled = 0;
608 /* Restore dev->irq to its default pin-assertion vector */
609 dev->irq = msi_dev_entry->default_irq;
610 msi_unmap_pirq(dev, pirq);
612 /* Disable MSI mode */
613 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
614 msi_set_enable(dev, pos, 0);
615 pci_intx_for_msi(dev, 1);
616 dev->msi_enabled = 0;
619 void pci_disable_msi(struct pci_dev *dev)
621 pci_msi_shutdown(dev);
623 EXPORT_SYMBOL(pci_disable_msi);
626 * pci_msix_table_size - return the number of device's MSI-X table entries
627 * @dev: pointer to the pci_dev data structure of MSI-X device function
629 int pci_msix_table_size(struct pci_dev *dev)
634 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
638 pci_read_config_word(dev, msi_control_reg(pos), &control);
639 return multi_msix_capable(control);
643 * pci_enable_msix - configure device's MSI-X capability structure
644 * @dev: pointer to the pci_dev data structure of MSI-X device function
645 * @entries: pointer to an array of MSI-X entries
646 * @nvec: number of MSI-X irqs requested for allocation by device driver
648 * Setup the MSI-X capability structure of device function with the number
649 * of requested irqs upon its software driver call to request for
650 * MSI-X mode enabled on its hardware device function. A return of zero
651 * indicates the successful configuration of MSI-X capability structure
652 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
653 * Or a return of > 0 indicates that driver request is exceeding the number
654 * of irqs or MSI-X vectors available. Driver should use the returned value to
655 * re-send its request.
657 extern int pci_frontend_enable_msix(struct pci_dev *dev,
658 struct msix_entry *entries, int nvec);
659 int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
661 int status, nr_entries;
663 struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
668 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
669 if (!is_initial_xendomain()) {
670 struct msi_pirq_entry *pirq_entry;
674 ret = pci_frontend_enable_msix(dev, entries, nvec);
677 "got %x from frontend_enable_msix\n", ret);
680 dev->msix_enabled = 1;
681 msi_dev_entry->default_irq = temp;
683 for (i = 0; i < nvec; i++) {
686 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
687 if (pirq_entry->entry_nr == entries[i].entry) {
688 irq = pirq_entry->pirq;
689 BUG_ON(entries[i].vector != evtchn_get_xen_pirq(irq));
690 entries[i].vector = irq;
697 irq = evtchn_map_pirq(-1, entries[i].vector);
698 attach_pirq_entry(irq, entries[i].entry, msi_dev_entry);
699 entries[i].vector = irq;
705 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
709 nr_entries = pci_msix_table_size(dev);
710 if (nvec > nr_entries)
713 /* Check for any invalid entries */
714 for (i = 0; i < nvec; i++) {
715 if (entries[i].entry >= nr_entries)
716 return -EINVAL; /* invalid entry */
717 for (j = i + 1; j < nvec; j++) {
718 if (entries[i].entry == entries[j].entry)
719 return -EINVAL; /* duplicate entry */
724 /* Check whether driver already requested for MSI vector */
725 if (dev->msi_enabled) {
726 dev_info(&dev->dev, "can't enable MSI-X "
727 "(MSI IRQ already assigned)\n");
731 status = msix_capability_init(dev, entries, nvec);
734 msi_dev_entry->default_irq = temp;
738 EXPORT_SYMBOL(pci_enable_msix);
740 extern void pci_frontend_disable_msix(struct pci_dev* dev);
741 void pci_msix_shutdown(struct pci_dev *dev)
743 if (!pci_msi_enable || !dev || !dev->msix_enabled)
746 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
747 if (!is_initial_xendomain()) {
748 struct msi_dev_list *msi_dev_entry;
749 struct msi_pirq_entry *pirq_entry, *tmp;
751 pci_frontend_disable_msix(dev);
753 msi_dev_entry = get_msi_dev_pirq_list(dev);
754 list_for_each_entry_safe(pirq_entry, tmp,
755 &msi_dev_entry->pirq_list_head, list) {
756 evtchn_map_pirq(pirq_entry->pirq, 0);
757 list_del(&pirq_entry->list);
761 dev->irq = msi_dev_entry->default_irq;
762 dev->msix_enabled = 0;
767 msi_remove_pci_irq_vectors(dev);
769 /* Disable MSI mode */
770 msix_set_enable(dev, 0);
771 pci_intx_for_msi(dev, 1);
772 dev->msix_enabled = 0;
775 void pci_disable_msix(struct pci_dev *dev)
777 pci_msix_shutdown(dev);
779 EXPORT_SYMBOL(pci_disable_msix);
782 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
783 * @dev: pointer to the pci_dev data structure of MSI(X) device function
785 * Being called during hotplug remove, from which the device function
786 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
787 * allocated for this device function, are reclaimed to unused state,
788 * which may be used later on.
790 void msi_remove_pci_irq_vectors(struct pci_dev *dev)
793 struct msi_dev_list *msi_dev_entry;
794 struct msi_pirq_entry *pirq_entry, *tmp;
796 if (!pci_msi_enable || !dev)
799 msi_dev_entry = get_msi_dev_pirq_list(dev);
801 spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
802 if (!list_empty(&msi_dev_entry->pirq_list_head))
803 list_for_each_entry_safe(pirq_entry, tmp,
804 &msi_dev_entry->pirq_list_head, list) {
805 msi_unmap_pirq(dev, pirq_entry->pirq);
806 list_del(&pirq_entry->list);
809 spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
810 dev->irq = msi_dev_entry->default_irq;
813 void pci_no_msi(void)
819 * pci_msi_enabled - is MSI enabled?
821 * Returns true if MSI has not been disabled by the command-line option
824 int pci_msi_enabled(void)
826 return pci_msi_enable;
828 EXPORT_SYMBOL(pci_msi_enabled);
830 void pci_msi_init_pci_dev(struct pci_dev *dev)
833 INIT_LIST_HEAD(&dev->msi_list);