14 #include <linux/export.h>
16 #include <linux/pci.h>
20 #include <linux/errno.h>
22 #include <linux/slab.h>
27 static int pci_msi_enable = 1;
31 #ifndef arch_msi_check_device
38 #ifndef arch_setup_msi_irqs
39 # define arch_setup_msi_irqs default_setup_msi_irqs
40 # define HAVE_DEFAULT_MSI_SETUP_IRQS
43 #ifdef HAVE_DEFAULT_MSI_SETUP_IRQS
68 #ifndef arch_teardown_msi_irqs
69 # define arch_teardown_msi_irqs default_teardown_msi_irqs
70 # define HAVE_DEFAULT_MSI_TEARDOWN_IRQS
73 #ifdef HAVE_DEFAULT_MSI_TEARDOWN_IRQS
83 for (i = 0; i < nvec; i++)
89 #ifndef arch_restore_msi_irqs
90 # define arch_restore_msi_irqs default_restore_msi_irqs
91 # define HAVE_DEFAULT_MSI_RESTORE_IRQS
94 #ifdef HAVE_DEFAULT_MSI_RESTORE_IRQS
102 if (irq == entry->
irq)
106 entry = irq_get_msi_desc(irq);
127 static void msix_set_enable(
struct pci_dev *dev,
int enable)
147 return (1 << (1 << x)) - 1;
152 return msi_mask((control >> 1) & 7);
157 return msi_mask((control >> 4) & 7);
175 pci_write_config_dword(desc->
dev, desc->
mask_pos, mask_bits);
180 static void msi_mask_irq(
struct msi_desc *desc,
u32 mask,
u32 flag)
182 desc->
masked = __msi_mask_irq(desc, mask, flag);
205 static void msix_mask_irq(
struct msi_desc *desc,
u32 flag)
207 desc->
masked = __msix_mask_irq(desc, flag);
212 struct msi_desc *desc = irq_data_get_msi(data);
215 msix_mask_irq(desc, flag);
218 unsigned offset = data->
irq - desc->
dev->irq;
219 msi_mask_irq(desc, 1 << offset, flag << offset);
225 msi_set_mask_bit(data, 1);
230 msi_set_mask_bit(data, 0);
274 BUG_ON(!(entry->
msg.address_hi | entry->
msg.address_lo |
289 if (entry->
dev->current_state !=
PCI_D0) {
331 static void free_msi_irqs(
struct pci_dev *dev)
340 for (i = 0; i < nvec; i++)
348 if (list_is_last(&entry->
list, &dev->msi_list))
358 if (entry->
kobj.parent) {
374 INIT_LIST_HEAD(&desc->
list);
380 static void pci_intx_for_msi(
struct pci_dev *dev,
int enable)
386 static void __pci_restore_msi_state(
struct pci_dev *dev)
395 entry = irq_get_msi_desc(dev->
irq);
398 pci_intx_for_msi(dev, 0);
399 msi_set_enable(dev, pos, 0);
403 msi_mask_irq(entry, msi_capable_mask(control), entry->
masked);
409 static void __pci_restore_msix_state(
struct pci_dev *dev)
417 BUG_ON(list_empty(&dev->msi_list));
423 pci_intx_for_msi(dev, 0);
429 msix_mask_irq(entry, entry->
masked);
438 __pci_restore_msi_state(dev);
439 __pci_restore_msix_state(dev);
444 #define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
445 #define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
467 if (!attribute->
show)
470 return attribute->
show(entry, attribute, buf);
473 static const struct sysfs_ops msi_irq_sysfs_ops = {
474 .show = msi_irq_attr_show,
482 &mode_attribute.
attr,
493 static struct kobj_type msi_irq_ktype = {
495 .sysfs_ops = &msi_irq_sysfs_ops,
499 static int populate_msi_sysfs(
struct pci_dev *pdev)
512 kobj->
kset = pdev->msi_kset;
546 static int msi_capability_init(
struct pci_dev *dev,
int nvec)
554 msi_set_enable(dev, pos, 0);
558 entry = alloc_msi_entry(dev);
573 mask = msi_capable_mask(control);
574 msi_mask_irq(entry, mask, mask);
581 msi_mask_irq(entry, mask, ~mask);
586 ret = populate_msi_sysfs(dev);
588 msi_mask_irq(entry, mask, ~mask);
594 pci_intx_for_msi(dev, 0);
595 msi_set_enable(dev, pos, 1);
602 static void __iomem *msix_map_region(
struct pci_dev *dev,
unsigned pos,
611 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
617 static int msix_setup_entries(
struct pci_dev *dev,
unsigned pos,
624 for (i = 0; i < nvec; i++) {
625 entry = alloc_msi_entry(dev);
648 static void msix_program_entries(
struct pci_dev *dev,
649 struct msix_entry *entries)
658 entries[
i].vector = entry->
irq;
661 msix_mask_irq(entry, 1);
676 static int msix_capability_init(
struct pci_dev *dev,
677 struct msix_entry *entries,
int nvec)
695 ret = msix_setup_entries(dev, pos, base, entries, nvec);
711 msix_program_entries(dev, entries);
713 ret = populate_msi_sysfs(dev);
720 pci_intx_for_msi(dev, 0);
760 static int pci_msi_check_device(
struct pci_dev *dev,
int nvec,
int type)
766 if (!pci_msi_enable || !dev || dev->
no_msi)
784 for (bus = dev->
bus; bus; bus = bus->
parent)
833 "(MSI-X already enabled)\n");
837 status = msi_capability_init(dev, nvec);
852 BUG_ON(list_empty(&dev->msi_list));
856 msi_set_enable(dev, pos, 0);
857 pci_intx_for_msi(dev, 1);
862 mask = msi_capable_mask(ctrl);
864 __msi_mask_irq(desc, mask, ~mask);
878 dev->msi_kset =
NULL;
927 if (nvec > nr_entries)
931 for (i = 0; i < nvec; i++) {
932 if (entries[i].entry >= nr_entries)
934 for (j = i + 1; j < nvec; j++) {
935 if (entries[i].entry == entries[j].entry)
944 "(MSI IRQ already assigned)\n");
947 status = msix_capability_init(dev, entries, nvec);
962 __msix_mask_irq(entry, 1);
965 msix_set_enable(dev, 0);
966 pci_intx_for_msi(dev, 1);
978 dev->msi_kset =
NULL;
993 if (!pci_msi_enable || !dev)
1013 return pci_msi_enable;
1020 INIT_LIST_HEAD(&dev->msi_list);
1028 msi_set_enable(dev, pos, 0);
1029 msix_set_enable(dev, 0);