Linux 3.12.39
[linux/fpc-iii.git] / drivers / pci / msi.c
blob89237c8eab1dcebc6dff85042f1ee42245048c87
1 /*
2 * File: msi.c
3 * Purpose: PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7 */
9 #include <linux/err.h>
10 #include <linux/mm.h>
11 #include <linux/irq.h>
12 #include <linux/interrupt.h>
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/ioport.h>
16 #include <linux/pci.h>
17 #include <linux/proc_fs.h>
18 #include <linux/msi.h>
19 #include <linux/smp.h>
20 #include <linux/errno.h>
21 #include <linux/io.h>
22 #include <linux/slab.h>
24 #include "pci.h"
26 static int pci_msi_enable = 1;
28 #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
31 /* Arch hooks */
33 int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
35 struct msi_chip *chip = dev->bus->msi;
36 int err;
38 if (!chip || !chip->setup_irq)
39 return -EINVAL;
41 err = chip->setup_irq(chip, dev, desc);
42 if (err < 0)
43 return err;
45 irq_set_chip_data(desc->irq, chip);
47 return 0;
50 void __weak arch_teardown_msi_irq(unsigned int irq)
52 struct msi_chip *chip = irq_get_chip_data(irq);
54 if (!chip || !chip->teardown_irq)
55 return;
57 chip->teardown_irq(chip, irq);
60 int __weak arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
62 struct msi_chip *chip = dev->bus->msi;
64 if (!chip || !chip->check_device)
65 return 0;
67 return chip->check_device(chip, dev, nvec, type);
70 int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
72 struct msi_desc *entry;
73 int ret;
76 * If an architecture wants to support multiple MSI, it needs to
77 * override arch_setup_msi_irqs()
79 if (type == PCI_CAP_ID_MSI && nvec > 1)
80 return 1;
82 list_for_each_entry(entry, &dev->msi_list, list) {
83 ret = arch_setup_msi_irq(dev, entry);
84 if (ret < 0)
85 return ret;
86 if (ret > 0)
87 return -ENOSPC;
90 return 0;
94 * We have a default implementation available as a separate non-weak
95 * function, as it is used by the Xen x86 PCI code
97 void default_teardown_msi_irqs(struct pci_dev *dev)
99 struct msi_desc *entry;
101 list_for_each_entry(entry, &dev->msi_list, list) {
102 int i, nvec;
103 if (entry->irq == 0)
104 continue;
105 if (entry->nvec_used)
106 nvec = entry->nvec_used;
107 else
108 nvec = 1 << entry->msi_attrib.multiple;
109 for (i = 0; i < nvec; i++)
110 arch_teardown_msi_irq(entry->irq + i);
114 void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
116 return default_teardown_msi_irqs(dev);
119 void default_restore_msi_irqs(struct pci_dev *dev, int irq)
121 struct msi_desc *entry;
123 entry = NULL;
124 if (dev->msix_enabled) {
125 list_for_each_entry(entry, &dev->msi_list, list) {
126 if (irq == entry->irq)
127 break;
129 } else if (dev->msi_enabled) {
130 entry = irq_get_msi_desc(irq);
133 if (entry)
134 write_msi_msg(irq, &entry->msg);
137 void __weak arch_restore_msi_irqs(struct pci_dev *dev, int irq)
139 return default_restore_msi_irqs(dev, irq);
142 static void msi_set_enable(struct pci_dev *dev, int enable)
144 u16 control;
146 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
147 control &= ~PCI_MSI_FLAGS_ENABLE;
148 if (enable)
149 control |= PCI_MSI_FLAGS_ENABLE;
150 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
153 static void msix_set_enable(struct pci_dev *dev, int enable)
155 u16 control;
157 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
158 control &= ~PCI_MSIX_FLAGS_ENABLE;
159 if (enable)
160 control |= PCI_MSIX_FLAGS_ENABLE;
161 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
164 static inline __attribute_const__ u32 msi_mask(unsigned x)
166 /* Don't shift by >= width of type */
167 if (x >= 5)
168 return 0xffffffff;
169 return (1 << (1 << x)) - 1;
172 static inline __attribute_const__ u32 msi_capable_mask(u16 control)
174 return msi_mask((control >> 1) & 7);
177 static inline __attribute_const__ u32 msi_enabled_mask(u16 control)
179 return msi_mask((control >> 4) & 7);
183 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
184 * mask all MSI interrupts by clearing the MSI enable bit does not work
185 * reliably as devices without an INTx disable bit will then generate a
186 * level IRQ which will never be cleared.
188 static u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
190 u32 mask_bits = desc->masked;
192 if (!desc->msi_attrib.maskbit)
193 return 0;
195 mask_bits &= ~mask;
196 mask_bits |= flag;
197 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
199 return mask_bits;
202 static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
204 desc->masked = __msi_mask_irq(desc, mask, flag);
208 * This internal function does not flush PCI writes to the device.
209 * All users must ensure that they read from the device before either
210 * assuming that the device state is up to date, or returning out of this
211 * file. This saves a few milliseconds when initialising devices with lots
212 * of MSI-X interrupts.
214 static u32 __msix_mask_irq(struct msi_desc *desc, u32 flag)
216 u32 mask_bits = desc->masked;
217 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
218 PCI_MSIX_ENTRY_VECTOR_CTRL;
219 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
220 if (flag)
221 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
222 writel(mask_bits, desc->mask_base + offset);
224 return mask_bits;
227 static void msix_mask_irq(struct msi_desc *desc, u32 flag)
229 desc->masked = __msix_mask_irq(desc, flag);
232 static void msi_set_mask_bit(struct irq_data *data, u32 flag)
234 struct msi_desc *desc = irq_data_get_msi(data);
236 if (desc->msi_attrib.is_msix) {
237 msix_mask_irq(desc, flag);
238 readl(desc->mask_base); /* Flush write to device */
239 } else {
240 unsigned offset = data->irq - desc->dev->irq;
241 msi_mask_irq(desc, 1 << offset, flag << offset);
245 void mask_msi_irq(struct irq_data *data)
247 msi_set_mask_bit(data, 1);
250 void unmask_msi_irq(struct irq_data *data)
252 msi_set_mask_bit(data, 0);
255 void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
257 BUG_ON(entry->dev->current_state != PCI_D0);
259 if (entry->msi_attrib.is_msix) {
260 void __iomem *base = entry->mask_base +
261 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
263 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
264 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
265 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
266 } else {
267 struct pci_dev *dev = entry->dev;
268 int pos = dev->msi_cap;
269 u16 data;
271 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
272 &msg->address_lo);
273 if (entry->msi_attrib.is_64) {
274 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
275 &msg->address_hi);
276 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
277 } else {
278 msg->address_hi = 0;
279 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
281 msg->data = data;
285 void read_msi_msg(unsigned int irq, struct msi_msg *msg)
287 struct msi_desc *entry = irq_get_msi_desc(irq);
289 __read_msi_msg(entry, msg);
292 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
294 /* Assert that the cache is valid, assuming that
295 * valid messages are not all-zeroes. */
296 BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
297 entry->msg.data));
299 *msg = entry->msg;
302 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
304 struct msi_desc *entry = irq_get_msi_desc(irq);
306 __get_cached_msi_msg(entry, msg);
309 void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
311 if (entry->dev->current_state != PCI_D0) {
312 /* Don't touch the hardware now */
313 } else if (entry->msi_attrib.is_msix) {
314 void __iomem *base;
315 base = entry->mask_base +
316 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
318 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
319 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
320 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
321 } else {
322 struct pci_dev *dev = entry->dev;
323 int pos = dev->msi_cap;
324 u16 msgctl;
326 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
327 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
328 msgctl |= entry->msi_attrib.multiple << 4;
329 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
331 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
332 msg->address_lo);
333 if (entry->msi_attrib.is_64) {
334 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
335 msg->address_hi);
336 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
337 msg->data);
338 } else {
339 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
340 msg->data);
343 entry->msg = *msg;
346 void write_msi_msg(unsigned int irq, struct msi_msg *msg)
348 struct msi_desc *entry = irq_get_msi_desc(irq);
350 __write_msi_msg(entry, msg);
353 static void free_msi_irqs(struct pci_dev *dev)
355 struct msi_desc *entry, *tmp;
357 list_for_each_entry(entry, &dev->msi_list, list) {
358 int i, nvec;
359 if (!entry->irq)
360 continue;
361 if (entry->nvec_used)
362 nvec = entry->nvec_used;
363 else
364 nvec = 1 << entry->msi_attrib.multiple;
365 for (i = 0; i < nvec; i++)
366 BUG_ON(irq_has_action(entry->irq + i));
369 arch_teardown_msi_irqs(dev);
371 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
372 if (entry->msi_attrib.is_msix) {
373 if (list_is_last(&entry->list, &dev->msi_list))
374 iounmap(entry->mask_base);
378 * Its possible that we get into this path
379 * When populate_msi_sysfs fails, which means the entries
380 * were not registered with sysfs. In that case don't
381 * unregister them.
383 if (entry->kobj.parent) {
384 kobject_del(&entry->kobj);
385 kobject_put(&entry->kobj);
388 list_del(&entry->list);
389 kfree(entry);
393 static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
395 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
396 if (!desc)
397 return NULL;
399 INIT_LIST_HEAD(&desc->list);
400 desc->dev = dev;
402 return desc;
405 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
407 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
408 pci_intx(dev, enable);
411 static void __pci_restore_msi_state(struct pci_dev *dev)
413 u16 control;
414 struct msi_desc *entry;
416 if (!dev->msi_enabled)
417 return;
419 entry = irq_get_msi_desc(dev->irq);
421 pci_intx_for_msi(dev, 0);
422 msi_set_enable(dev, 0);
423 arch_restore_msi_irqs(dev, dev->irq);
425 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
426 msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
427 control &= ~PCI_MSI_FLAGS_QSIZE;
428 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
429 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
432 static void __pci_restore_msix_state(struct pci_dev *dev)
434 struct msi_desc *entry;
435 u16 control;
437 if (!dev->msix_enabled)
438 return;
439 BUG_ON(list_empty(&dev->msi_list));
440 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
441 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
443 /* route the table */
444 pci_intx_for_msi(dev, 0);
445 control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
446 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
448 list_for_each_entry(entry, &dev->msi_list, list) {
449 arch_restore_msi_irqs(dev, entry->irq);
450 msix_mask_irq(entry, entry->masked);
453 control &= ~PCI_MSIX_FLAGS_MASKALL;
454 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
457 void pci_restore_msi_state(struct pci_dev *dev)
459 __pci_restore_msi_state(dev);
460 __pci_restore_msix_state(dev);
462 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
465 #define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
466 #define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
468 struct msi_attribute {
469 struct attribute attr;
470 ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
471 char *buf);
472 ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
473 const char *buf, size_t count);
476 static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
477 char *buf)
479 return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
482 static ssize_t msi_irq_attr_show(struct kobject *kobj,
483 struct attribute *attr, char *buf)
485 struct msi_attribute *attribute = to_msi_attr(attr);
486 struct msi_desc *entry = to_msi_desc(kobj);
488 if (!attribute->show)
489 return -EIO;
491 return attribute->show(entry, attribute, buf);
494 static const struct sysfs_ops msi_irq_sysfs_ops = {
495 .show = msi_irq_attr_show,
498 static struct msi_attribute mode_attribute =
499 __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
502 static struct attribute *msi_irq_default_attrs[] = {
503 &mode_attribute.attr,
504 NULL
507 static void msi_kobj_release(struct kobject *kobj)
509 struct msi_desc *entry = to_msi_desc(kobj);
511 pci_dev_put(entry->dev);
514 static struct kobj_type msi_irq_ktype = {
515 .release = msi_kobj_release,
516 .sysfs_ops = &msi_irq_sysfs_ops,
517 .default_attrs = msi_irq_default_attrs,
520 static int populate_msi_sysfs(struct pci_dev *pdev)
522 struct msi_desc *entry;
523 struct kobject *kobj;
524 int ret;
525 int count = 0;
527 pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
528 if (!pdev->msi_kset)
529 return -ENOMEM;
531 list_for_each_entry(entry, &pdev->msi_list, list) {
532 kobj = &entry->kobj;
533 kobj->kset = pdev->msi_kset;
534 pci_dev_get(pdev);
535 ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
536 "%u", entry->irq);
537 if (ret)
538 goto out_unroll;
540 count++;
543 return 0;
545 out_unroll:
546 list_for_each_entry(entry, &pdev->msi_list, list) {
547 if (!count)
548 break;
549 kobject_del(&entry->kobj);
550 kobject_put(&entry->kobj);
551 count--;
553 return ret;
556 static int msi_verify_entries(struct pci_dev *dev)
558 struct msi_desc *entry;
560 list_for_each_entry(entry, &dev->msi_list, list) {
561 if (!dev->no_64bit_msi || !entry->msg.address_hi)
562 continue;
563 dev_err(&dev->dev, "Device has broken 64-bit MSI but arch"
564 " tried to assign one above 4G\n");
565 return -EIO;
567 return 0;
571 * msi_capability_init - configure device's MSI capability structure
572 * @dev: pointer to the pci_dev data structure of MSI device function
573 * @nvec: number of interrupts to allocate
575 * Setup the MSI capability structure of the device with the requested
576 * number of interrupts. A return value of zero indicates the successful
577 * setup of an entry with the new MSI irq. A negative return value indicates
578 * an error, and a positive return value indicates the number of interrupts
579 * which could have been allocated.
581 static int msi_capability_init(struct pci_dev *dev, int nvec)
583 struct msi_desc *entry;
584 int ret;
585 u16 control;
586 unsigned mask;
588 msi_set_enable(dev, 0); /* Disable MSI during set up */
590 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
591 /* MSI Entry Initialization */
592 entry = alloc_msi_entry(dev);
593 if (!entry)
594 return -ENOMEM;
596 entry->msi_attrib.is_msix = 0;
597 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
598 entry->msi_attrib.entry_nr = 0;
599 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
600 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
601 entry->msi_attrib.pos = dev->msi_cap;
603 if (control & PCI_MSI_FLAGS_64BIT)
604 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
605 else
606 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
607 /* All MSIs are unmasked by default, Mask them all */
608 if (entry->msi_attrib.maskbit)
609 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
610 mask = msi_capable_mask(control);
611 msi_mask_irq(entry, mask, mask);
613 list_add_tail(&entry->list, &dev->msi_list);
615 /* Configure MSI capability structure */
616 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
617 if (ret) {
618 msi_mask_irq(entry, mask, ~mask);
619 free_msi_irqs(dev);
620 return ret;
623 ret = msi_verify_entries(dev);
624 if (ret) {
625 msi_mask_irq(entry, mask, ~mask);
626 free_msi_irqs(dev);
627 return ret;
630 ret = populate_msi_sysfs(dev);
631 if (ret) {
632 msi_mask_irq(entry, mask, ~mask);
633 free_msi_irqs(dev);
634 return ret;
637 /* Set MSI enabled bits */
638 pci_intx_for_msi(dev, 0);
639 msi_set_enable(dev, 1);
640 dev->msi_enabled = 1;
642 dev->irq = entry->irq;
643 return 0;
646 static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
648 resource_size_t phys_addr;
649 u32 table_offset;
650 u8 bir;
652 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
653 &table_offset);
654 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
655 table_offset &= PCI_MSIX_TABLE_OFFSET;
656 phys_addr = pci_resource_start(dev, bir) + table_offset;
658 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
661 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
662 struct msix_entry *entries, int nvec)
664 struct msi_desc *entry;
665 int i;
667 for (i = 0; i < nvec; i++) {
668 entry = alloc_msi_entry(dev);
669 if (!entry) {
670 if (!i)
671 iounmap(base);
672 else
673 free_msi_irqs(dev);
674 /* No enough memory. Don't try again */
675 return -ENOMEM;
678 entry->msi_attrib.is_msix = 1;
679 entry->msi_attrib.is_64 = 1;
680 entry->msi_attrib.entry_nr = entries[i].entry;
681 entry->msi_attrib.default_irq = dev->irq;
682 entry->msi_attrib.pos = dev->msix_cap;
683 entry->mask_base = base;
685 list_add_tail(&entry->list, &dev->msi_list);
688 return 0;
691 static void msix_program_entries(struct pci_dev *dev,
692 struct msix_entry *entries)
694 struct msi_desc *entry;
695 int i = 0;
697 list_for_each_entry(entry, &dev->msi_list, list) {
698 int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
699 PCI_MSIX_ENTRY_VECTOR_CTRL;
701 entries[i].vector = entry->irq;
702 irq_set_msi_desc(entry->irq, entry);
703 entry->masked = readl(entry->mask_base + offset);
704 msix_mask_irq(entry, 1);
705 i++;
710 * msix_capability_init - configure device's MSI-X capability
711 * @dev: pointer to the pci_dev data structure of MSI-X device function
712 * @entries: pointer to an array of struct msix_entry entries
713 * @nvec: number of @entries
715 * Setup the MSI-X capability structure of device function with a
716 * single MSI-X irq. A return of zero indicates the successful setup of
717 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
719 static int msix_capability_init(struct pci_dev *dev,
720 struct msix_entry *entries, int nvec)
722 int ret;
723 u16 control;
724 void __iomem *base;
726 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
728 /* Ensure MSI-X is disabled while it is set up */
729 control &= ~PCI_MSIX_FLAGS_ENABLE;
730 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
732 /* Request & Map MSI-X table region */
733 base = msix_map_region(dev, msix_table_size(control));
734 if (!base)
735 return -ENOMEM;
737 ret = msix_setup_entries(dev, base, entries, nvec);
738 if (ret)
739 return ret;
741 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
742 if (ret)
743 goto out_avail;
745 /* Check if all MSI entries honor device restrictions */
746 ret = msi_verify_entries(dev);
747 if (ret)
748 goto out_free;
751 * Some devices require MSI-X to be enabled before we can touch the
752 * MSI-X registers. We need to mask all the vectors to prevent
753 * interrupts coming in before they're fully set up.
755 control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
756 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
758 msix_program_entries(dev, entries);
760 ret = populate_msi_sysfs(dev);
761 if (ret)
762 goto out_free;
764 /* Set MSI-X enabled bits and unmask the function */
765 pci_intx_for_msi(dev, 0);
766 dev->msix_enabled = 1;
768 control &= ~PCI_MSIX_FLAGS_MASKALL;
769 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
771 return 0;
773 out_avail:
774 if (ret < 0) {
776 * If we had some success, report the number of irqs
777 * we succeeded in setting up.
779 struct msi_desc *entry;
780 int avail = 0;
782 list_for_each_entry(entry, &dev->msi_list, list) {
783 if (entry->irq != 0)
784 avail++;
786 if (avail != 0)
787 ret = avail;
790 out_free:
791 free_msi_irqs(dev);
793 return ret;
797 * pci_msi_check_device - check whether MSI may be enabled on a device
798 * @dev: pointer to the pci_dev data structure of MSI device function
799 * @nvec: how many MSIs have been requested ?
800 * @type: are we checking for MSI or MSI-X ?
802 * Look at global flags, the device itself, and its parent busses
803 * to determine if MSI/-X are supported for the device. If MSI/-X is
804 * supported return 0, else return an error code.
806 static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
808 struct pci_bus *bus;
809 int ret;
811 /* MSI must be globally enabled and supported by the device */
812 if (!pci_msi_enable || !dev || dev->no_msi)
813 return -EINVAL;
816 * You can't ask to have 0 or less MSIs configured.
817 * a) it's stupid ..
818 * b) the list manipulation code assumes nvec >= 1.
820 if (nvec < 1)
821 return -ERANGE;
824 * Any bridge which does NOT route MSI transactions from its
825 * secondary bus to its primary bus must set NO_MSI flag on
826 * the secondary pci_bus.
827 * We expect only arch-specific PCI host bus controller driver
828 * or quirks for specific PCI bridges to be setting NO_MSI.
830 for (bus = dev->bus; bus; bus = bus->parent)
831 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
832 return -EINVAL;
834 ret = arch_msi_check_device(dev, nvec, type);
835 if (ret)
836 return ret;
838 return 0;
842 * pci_enable_msi_block - configure device's MSI capability structure
843 * @dev: device to configure
844 * @nvec: number of interrupts to configure
846 * Allocate IRQs for a device with the MSI capability.
847 * This function returns a negative errno if an error occurs. If it
848 * is unable to allocate the number of interrupts requested, it returns
849 * the number of interrupts it might be able to allocate. If it successfully
850 * allocates at least the number of interrupts requested, it returns 0 and
851 * updates the @dev's irq member to the lowest new interrupt number; the
852 * other interrupt numbers allocated to this device are consecutive.
854 int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
856 int status, maxvec;
857 u16 msgctl;
859 if (!dev->msi_cap)
860 return -EINVAL;
862 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
863 maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
864 if (nvec > maxvec)
865 return maxvec;
867 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
868 if (status)
869 return status;
871 WARN_ON(!!dev->msi_enabled);
873 /* Check whether driver already requested MSI-X irqs */
874 if (dev->msix_enabled) {
875 dev_info(&dev->dev, "can't enable MSI "
876 "(MSI-X already enabled)\n");
877 return -EINVAL;
880 status = msi_capability_init(dev, nvec);
881 return status;
883 EXPORT_SYMBOL(pci_enable_msi_block);
885 int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec)
887 int ret, nvec;
888 u16 msgctl;
890 if (!dev->msi_cap)
891 return -EINVAL;
893 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
894 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
896 if (maxvec)
897 *maxvec = ret;
899 do {
900 nvec = ret;
901 ret = pci_enable_msi_block(dev, nvec);
902 } while (ret > 0);
904 if (ret < 0)
905 return ret;
906 return nvec;
908 EXPORT_SYMBOL(pci_enable_msi_block_auto);
910 void pci_msi_shutdown(struct pci_dev *dev)
912 struct msi_desc *desc;
913 u32 mask;
914 u16 ctrl;
916 if (!pci_msi_enable || !dev || !dev->msi_enabled)
917 return;
919 BUG_ON(list_empty(&dev->msi_list));
920 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
922 msi_set_enable(dev, 0);
923 pci_intx_for_msi(dev, 1);
924 dev->msi_enabled = 0;
926 /* Return the device with MSI unmasked as initial states */
927 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
928 mask = msi_capable_mask(ctrl);
929 /* Keep cached state to be restored */
930 __msi_mask_irq(desc, mask, ~mask);
932 /* Restore dev->irq to its default pin-assertion irq */
933 dev->irq = desc->msi_attrib.default_irq;
936 void pci_disable_msi(struct pci_dev *dev)
938 if (!pci_msi_enable || !dev || !dev->msi_enabled)
939 return;
941 pci_msi_shutdown(dev);
942 free_msi_irqs(dev);
943 kset_unregister(dev->msi_kset);
944 dev->msi_kset = NULL;
946 EXPORT_SYMBOL(pci_disable_msi);
949 * pci_msix_table_size - return the number of device's MSI-X table entries
950 * @dev: pointer to the pci_dev data structure of MSI-X device function
952 int pci_msix_table_size(struct pci_dev *dev)
954 u16 control;
956 if (!dev->msix_cap)
957 return 0;
959 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
960 return msix_table_size(control);
964 * pci_enable_msix - configure device's MSI-X capability structure
965 * @dev: pointer to the pci_dev data structure of MSI-X device function
966 * @entries: pointer to an array of MSI-X entries
967 * @nvec: number of MSI-X irqs requested for allocation by device driver
969 * Setup the MSI-X capability structure of device function with the number
970 * of requested irqs upon its software driver call to request for
971 * MSI-X mode enabled on its hardware device function. A return of zero
972 * indicates the successful configuration of MSI-X capability structure
973 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
974 * Or a return of > 0 indicates that driver request is exceeding the number
975 * of irqs or MSI-X vectors available. Driver should use the returned value to
976 * re-send its request.
978 int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
980 int status, nr_entries;
981 int i, j;
983 if (!entries || !dev->msix_cap)
984 return -EINVAL;
986 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
987 if (status)
988 return status;
990 nr_entries = pci_msix_table_size(dev);
991 if (nvec > nr_entries)
992 return nr_entries;
994 /* Check for any invalid entries */
995 for (i = 0; i < nvec; i++) {
996 if (entries[i].entry >= nr_entries)
997 return -EINVAL; /* invalid entry */
998 for (j = i + 1; j < nvec; j++) {
999 if (entries[i].entry == entries[j].entry)
1000 return -EINVAL; /* duplicate entry */
1003 WARN_ON(!!dev->msix_enabled);
1005 /* Check whether driver already requested for MSI irq */
1006 if (dev->msi_enabled) {
1007 dev_info(&dev->dev, "can't enable MSI-X "
1008 "(MSI IRQ already assigned)\n");
1009 return -EINVAL;
1011 status = msix_capability_init(dev, entries, nvec);
1012 return status;
1014 EXPORT_SYMBOL(pci_enable_msix);
1016 void pci_msix_shutdown(struct pci_dev *dev)
1018 struct msi_desc *entry;
1020 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1021 return;
1023 /* Return the device with MSI-X masked as initial states */
1024 list_for_each_entry(entry, &dev->msi_list, list) {
1025 /* Keep cached states to be restored */
1026 __msix_mask_irq(entry, 1);
1029 msix_set_enable(dev, 0);
1030 pci_intx_for_msi(dev, 1);
1031 dev->msix_enabled = 0;
1034 void pci_disable_msix(struct pci_dev *dev)
1036 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1037 return;
1039 pci_msix_shutdown(dev);
1040 free_msi_irqs(dev);
1041 kset_unregister(dev->msi_kset);
1042 dev->msi_kset = NULL;
1044 EXPORT_SYMBOL(pci_disable_msix);
1047 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
1048 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1050 * Being called during hotplug remove, from which the device function
1051 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
1052 * allocated for this device function, are reclaimed to unused state,
1053 * which may be used later on.
1055 void msi_remove_pci_irq_vectors(struct pci_dev *dev)
1057 if (!pci_msi_enable || !dev)
1058 return;
1060 if (dev->msi_enabled || dev->msix_enabled)
1061 free_msi_irqs(dev);
1064 void pci_no_msi(void)
1066 pci_msi_enable = 0;
1070 * pci_msi_enabled - is MSI enabled?
1072 * Returns true if MSI has not been disabled by the command-line option
1073 * pci=nomsi.
1075 int pci_msi_enabled(void)
1077 return pci_msi_enable;
1079 EXPORT_SYMBOL(pci_msi_enabled);
1081 void pci_msi_init_pci_dev(struct pci_dev *dev)
1083 INIT_LIST_HEAD(&dev->msi_list);
1085 /* Disable the msi hardware to avoid screaming interrupts
1086 * during boot. This is the power on reset default so
1087 * usually this should be a noop.
1089 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1090 if (dev->msi_cap)
1091 msi_set_enable(dev, 0);
1093 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1094 if (dev->msix_cap)
1095 msix_set_enable(dev, 0);