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 msix_flush_writes(unsigned int irq
)
73 struct msi_desc
*entry
;
75 entry
= get_irq_msi(irq
);
76 BUG_ON(!entry
|| !entry
->dev
);
77 switch (entry
->msi_attrib
.type
) {
83 int offset
= entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
+
84 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
;
85 readl(entry
->mask_base
+ offset
);
94 static void msi_set_mask_bit(unsigned int irq
, int flag
)
96 struct msi_desc
*entry
;
98 entry
= get_irq_msi(irq
);
99 BUG_ON(!entry
|| !entry
->dev
);
100 switch (entry
->msi_attrib
.type
) {
102 if (entry
->msi_attrib
.maskbit
) {
106 pos
= (long)entry
->mask_base
;
107 pci_read_config_dword(entry
->dev
, pos
, &mask_bits
);
110 pci_write_config_dword(entry
->dev
, pos
, mask_bits
);
112 msi_set_enable(entry
->dev
, !flag
);
115 case PCI_CAP_ID_MSIX
:
117 int offset
= entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
+
118 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
;
119 writel(flag
, entry
->mask_base
+ offset
);
120 readl(entry
->mask_base
+ offset
);
127 entry
->msi_attrib
.masked
= !!flag
;
130 void read_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
132 struct msi_desc
*entry
= get_irq_msi(irq
);
133 switch(entry
->msi_attrib
.type
) {
136 struct pci_dev
*dev
= entry
->dev
;
137 int pos
= entry
->msi_attrib
.pos
;
140 pci_read_config_dword(dev
, msi_lower_address_reg(pos
),
142 if (entry
->msi_attrib
.is_64
) {
143 pci_read_config_dword(dev
, msi_upper_address_reg(pos
),
145 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
148 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
153 case PCI_CAP_ID_MSIX
:
156 base
= entry
->mask_base
+
157 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
159 msg
->address_lo
= readl(base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
160 msg
->address_hi
= readl(base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
161 msg
->data
= readl(base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
169 void write_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
171 struct msi_desc
*entry
= get_irq_msi(irq
);
172 switch (entry
->msi_attrib
.type
) {
175 struct pci_dev
*dev
= entry
->dev
;
176 int pos
= entry
->msi_attrib
.pos
;
178 pci_write_config_dword(dev
, msi_lower_address_reg(pos
),
180 if (entry
->msi_attrib
.is_64
) {
181 pci_write_config_dword(dev
, msi_upper_address_reg(pos
),
183 pci_write_config_word(dev
, msi_data_reg(pos
, 1),
186 pci_write_config_word(dev
, msi_data_reg(pos
, 0),
191 case PCI_CAP_ID_MSIX
:
194 base
= entry
->mask_base
+
195 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
197 writel(msg
->address_lo
,
198 base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
199 writel(msg
->address_hi
,
200 base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
201 writel(msg
->data
, base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
210 void mask_msi_irq(unsigned int irq
)
212 msi_set_mask_bit(irq
, 1);
213 msix_flush_writes(irq
);
216 void unmask_msi_irq(unsigned int irq
)
218 msi_set_mask_bit(irq
, 0);
219 msix_flush_writes(irq
);
222 static int msi_free_irq(struct pci_dev
* dev
, int irq
);
224 static int msi_init(void)
226 static int status
= -ENOMEM
;
231 status
= msi_cache_init();
234 printk(KERN_WARNING
"PCI: MSI cache init failed\n");
241 static struct msi_desc
* alloc_msi_entry(void)
243 struct msi_desc
*entry
;
245 entry
= kmem_cache_zalloc(msi_cachep
, GFP_KERNEL
);
249 entry
->link
.tail
= entry
->link
.head
= 0; /* single message */
256 static void __pci_restore_msi_state(struct pci_dev
*dev
)
260 struct msi_desc
*entry
;
262 if (!dev
->msi_enabled
)
265 entry
= get_irq_msi(dev
->irq
);
266 pos
= entry
->msi_attrib
.pos
;
268 pci_intx(dev
, 0); /* disable intx */
269 msi_set_enable(dev
, 0);
270 write_msi_msg(dev
->irq
, &entry
->msg
);
271 if (entry
->msi_attrib
.maskbit
)
272 msi_set_mask_bit(dev
->irq
, entry
->msi_attrib
.masked
);
274 pci_read_config_word(dev
, pos
+ PCI_MSI_FLAGS
, &control
);
275 control
&= ~(PCI_MSI_FLAGS_QSIZE
| PCI_MSI_FLAGS_ENABLE
);
276 if (entry
->msi_attrib
.maskbit
|| !entry
->msi_attrib
.masked
)
277 control
|= PCI_MSI_FLAGS_ENABLE
;
278 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
281 static void __pci_restore_msix_state(struct pci_dev
*dev
)
284 int irq
, head
, tail
= 0;
285 struct msi_desc
*entry
;
288 if (!dev
->msix_enabled
)
291 /* route the table */
292 pci_intx(dev
, 0); /* disable intx */
293 msix_set_enable(dev
, 0);
294 irq
= head
= dev
->first_msi_irq
;
295 entry
= get_irq_msi(irq
);
296 pos
= entry
->msi_attrib
.pos
;
297 while (head
!= tail
) {
298 entry
= get_irq_msi(irq
);
299 write_msi_msg(irq
, &entry
->msg
);
300 msi_set_mask_bit(irq
, entry
->msi_attrib
.masked
);
302 tail
= entry
->link
.tail
;
306 pci_read_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, &control
);
307 control
&= ~PCI_MSIX_FLAGS_MASKALL
;
308 control
|= PCI_MSIX_FLAGS_ENABLE
;
309 pci_write_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, control
);
312 void pci_restore_msi_state(struct pci_dev
*dev
)
314 __pci_restore_msi_state(dev
);
315 __pci_restore_msix_state(dev
);
317 #endif /* CONFIG_PM */
320 * msi_capability_init - configure device's MSI capability structure
321 * @dev: pointer to the pci_dev data structure of MSI device function
323 * Setup the MSI capability structure of device function with a single
324 * MSI irq, regardless of device function is capable of handling
325 * multiple messages. A return of zero indicates the successful setup
326 * of an entry zero with the new MSI irq or non-zero for otherwise.
328 static int msi_capability_init(struct pci_dev
*dev
)
330 struct msi_desc
*entry
;
334 msi_set_enable(dev
, 0); /* Ensure msi is disabled as I set it up */
336 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
337 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
338 /* MSI Entry Initialization */
339 entry
= alloc_msi_entry();
343 entry
->msi_attrib
.type
= PCI_CAP_ID_MSI
;
344 entry
->msi_attrib
.is_64
= is_64bit_address(control
);
345 entry
->msi_attrib
.entry_nr
= 0;
346 entry
->msi_attrib
.maskbit
= is_mask_bit_support(control
);
347 entry
->msi_attrib
.masked
= 1;
348 entry
->msi_attrib
.default_irq
= dev
->irq
; /* Save IOAPIC IRQ */
349 entry
->msi_attrib
.pos
= pos
;
350 if (is_mask_bit_support(control
)) {
351 entry
->mask_base
= (void __iomem
*)(long)msi_mask_bits_reg(pos
,
352 is_64bit_address(control
));
355 if (entry
->msi_attrib
.maskbit
) {
356 unsigned int maskbits
, temp
;
357 /* All MSIs are unmasked by default, Mask them all */
358 pci_read_config_dword(dev
,
359 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
361 temp
= (1 << multi_msi_capable(control
));
362 temp
= ((temp
- 1) & ~temp
);
364 pci_write_config_dword(dev
,
365 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
368 /* Configure MSI capability structure */
369 irq
= arch_setup_msi_irq(dev
, entry
);
371 kmem_cache_free(msi_cachep
, entry
);
374 entry
->link
.head
= irq
;
375 entry
->link
.tail
= irq
;
376 dev
->first_msi_irq
= irq
;
377 set_irq_msi(irq
, entry
);
379 /* Set MSI enabled bits */
380 pci_intx(dev
, 0); /* disable intx */
381 msi_set_enable(dev
, 1);
382 dev
->msi_enabled
= 1;
389 * msix_capability_init - configure device's MSI-X capability
390 * @dev: pointer to the pci_dev data structure of MSI-X device function
391 * @entries: pointer to an array of struct msix_entry entries
392 * @nvec: number of @entries
394 * Setup the MSI-X capability structure of device function with a
395 * single MSI-X irq. A return of zero indicates the successful setup of
396 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
398 static int msix_capability_init(struct pci_dev
*dev
,
399 struct msix_entry
*entries
, int nvec
)
401 struct msi_desc
*head
= NULL
, *tail
= NULL
, *entry
= NULL
;
402 int irq
, pos
, i
, j
, nr_entries
, temp
= 0;
403 unsigned long phys_addr
;
409 msix_set_enable(dev
, 0);/* Ensure msix is disabled as I set it up */
411 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
412 /* Request & Map MSI-X table region */
413 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
414 nr_entries
= multi_msix_capable(control
);
416 pci_read_config_dword(dev
, msix_table_offset_reg(pos
), &table_offset
);
417 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
418 table_offset
&= ~PCI_MSIX_FLAGS_BIRMASK
;
419 phys_addr
= pci_resource_start (dev
, bir
) + table_offset
;
420 base
= ioremap_nocache(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
424 /* MSI-X Table Initialization */
425 for (i
= 0; i
< nvec
; i
++) {
426 entry
= alloc_msi_entry();
430 j
= entries
[i
].entry
;
431 entry
->msi_attrib
.type
= PCI_CAP_ID_MSIX
;
432 entry
->msi_attrib
.is_64
= 1;
433 entry
->msi_attrib
.entry_nr
= j
;
434 entry
->msi_attrib
.maskbit
= 1;
435 entry
->msi_attrib
.masked
= 1;
436 entry
->msi_attrib
.default_irq
= dev
->irq
;
437 entry
->msi_attrib
.pos
= pos
;
439 entry
->mask_base
= base
;
441 /* Configure MSI-X capability structure */
442 irq
= arch_setup_msi_irq(dev
, entry
);
444 kmem_cache_free(msi_cachep
, entry
);
447 entries
[i
].vector
= irq
;
449 entry
->link
.head
= irq
;
450 entry
->link
.tail
= irq
;
453 entry
->link
.head
= temp
;
454 entry
->link
.tail
= tail
->link
.tail
;
455 tail
->link
.tail
= irq
;
456 head
->link
.head
= irq
;
461 set_irq_msi(irq
, entry
);
466 for (; i
>= 0; i
--) {
467 irq
= (entries
+ i
)->vector
;
468 msi_free_irq(dev
, irq
);
469 (entries
+ i
)->vector
= 0;
471 /* If we had some success report the number of irqs
472 * we succeeded in setting up.
478 dev
->first_msi_irq
= entries
[0].vector
;
479 /* Set MSI-X enabled bits */
480 pci_intx(dev
, 0); /* disable intx */
481 msix_set_enable(dev
, 1);
482 dev
->msix_enabled
= 1;
488 * pci_msi_supported - check whether MSI may be enabled on device
489 * @dev: pointer to the pci_dev data structure of MSI device function
491 * Look at global flags, the device itself, and its parent busses
492 * to return 0 if MSI are supported for the device.
495 int pci_msi_supported(struct pci_dev
* dev
)
499 /* MSI must be globally enabled and supported by the device */
500 if (!pci_msi_enable
|| !dev
|| dev
->no_msi
)
503 /* Any bridge which does NOT route MSI transactions from it's
504 * secondary bus to it's primary bus must set NO_MSI flag on
505 * the secondary pci_bus.
506 * We expect only arch-specific PCI host bus controller driver
507 * or quirks for specific PCI bridges to be setting NO_MSI.
509 for (bus
= dev
->bus
; bus
; bus
= bus
->parent
)
510 if (bus
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
517 * pci_enable_msi - configure device's MSI capability structure
518 * @dev: pointer to the pci_dev data structure of MSI device function
520 * Setup the MSI capability structure of device function with
521 * a single MSI irq upon its software driver call to request for
522 * MSI mode enabled on its hardware device function. A return of zero
523 * indicates the successful setup of an entry zero with the new MSI
524 * irq or non-zero for otherwise.
526 int pci_enable_msi(struct pci_dev
* dev
)
530 if (pci_msi_supported(dev
) < 0)
537 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
541 WARN_ON(!!dev
->msi_enabled
);
543 /* Check whether driver already requested for MSI-X irqs */
544 if (dev
->msix_enabled
) {
545 printk(KERN_INFO
"PCI: %s: Can't enable MSI. "
546 "Device already has MSI-X enabled\n",
550 status
= msi_capability_init(dev
);
554 void pci_disable_msi(struct pci_dev
* dev
)
556 struct msi_desc
*entry
;
564 if (!dev
->msi_enabled
)
567 msi_set_enable(dev
, 0);
568 pci_intx(dev
, 1); /* enable intx */
569 dev
->msi_enabled
= 0;
571 entry
= get_irq_msi(dev
->first_msi_irq
);
572 if (!entry
|| !entry
->dev
|| entry
->msi_attrib
.type
!= PCI_CAP_ID_MSI
) {
576 BUG_ON(irq_has_action(dev
->first_msi_irq
));
578 default_irq
= entry
->msi_attrib
.default_irq
;
579 msi_free_irq(dev
, dev
->first_msi_irq
);
581 /* Restore dev->irq to its default pin-assertion irq */
582 dev
->irq
= default_irq
;
584 dev
->first_msi_irq
= 0;
587 static int msi_free_irq(struct pci_dev
* dev
, int irq
)
589 struct msi_desc
*entry
;
590 int head
, entry_nr
, type
;
593 entry
= get_irq_msi(irq
);
594 if (!entry
|| entry
->dev
!= dev
) {
597 type
= entry
->msi_attrib
.type
;
598 entry_nr
= entry
->msi_attrib
.entry_nr
;
599 head
= entry
->link
.head
;
600 base
= entry
->mask_base
;
601 get_irq_msi(entry
->link
.head
)->link
.tail
= entry
->link
.tail
;
602 get_irq_msi(entry
->link
.tail
)->link
.head
= entry
->link
.head
;
604 arch_teardown_msi_irq(irq
);
605 kmem_cache_free(msi_cachep
, entry
);
607 if (type
== PCI_CAP_ID_MSIX
) {
608 writel(1, base
+ entry_nr
* PCI_MSIX_ENTRY_SIZE
+
609 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
);
619 * pci_enable_msix - configure device's MSI-X capability structure
620 * @dev: pointer to the pci_dev data structure of MSI-X device function
621 * @entries: pointer to an array of MSI-X entries
622 * @nvec: number of MSI-X irqs requested for allocation by device driver
624 * Setup the MSI-X capability structure of device function with the number
625 * of requested irqs upon its software driver call to request for
626 * MSI-X mode enabled on its hardware device function. A return of zero
627 * indicates the successful configuration of MSI-X capability structure
628 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
629 * Or a return of > 0 indicates that driver request is exceeding the number
630 * of irqs available. Driver should use the returned value to re-send
633 int pci_enable_msix(struct pci_dev
* dev
, struct msix_entry
*entries
, int nvec
)
635 int status
, pos
, nr_entries
;
639 if (!entries
|| pci_msi_supported(dev
) < 0)
646 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
650 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
651 nr_entries
= multi_msix_capable(control
);
652 if (nvec
> nr_entries
)
655 /* Check for any invalid entries */
656 for (i
= 0; i
< nvec
; i
++) {
657 if (entries
[i
].entry
>= nr_entries
)
658 return -EINVAL
; /* invalid entry */
659 for (j
= i
+ 1; j
< nvec
; j
++) {
660 if (entries
[i
].entry
== entries
[j
].entry
)
661 return -EINVAL
; /* duplicate entry */
664 WARN_ON(!!dev
->msix_enabled
);
666 /* Check whether driver already requested for MSI irq */
667 if (dev
->msi_enabled
) {
668 printk(KERN_INFO
"PCI: %s: Can't enable MSI-X. "
669 "Device already has an MSI irq assigned\n",
673 status
= msix_capability_init(dev
, entries
, nvec
);
677 void pci_disable_msix(struct pci_dev
* dev
)
679 int irq
, head
, tail
= 0;
686 if (!dev
->msix_enabled
)
689 msix_set_enable(dev
, 0);
690 pci_intx(dev
, 1); /* enable intx */
691 dev
->msix_enabled
= 0;
693 irq
= head
= dev
->first_msi_irq
;
694 while (head
!= tail
) {
695 tail
= get_irq_msi(irq
)->link
.tail
;
697 BUG_ON(irq_has_action(irq
));
699 if (irq
!= head
) /* Release MSI-X irq */
700 msi_free_irq(dev
, irq
);
703 msi_free_irq(dev
, irq
);
704 dev
->first_msi_irq
= 0;
708 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
709 * @dev: pointer to the pci_dev data structure of MSI(X) device function
711 * Being called during hotplug remove, from which the device function
712 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
713 * allocated for this device function, are reclaimed to unused state,
714 * which may be used later on.
716 void msi_remove_pci_irq_vectors(struct pci_dev
* dev
)
718 if (!pci_msi_enable
|| !dev
)
721 if (dev
->msi_enabled
) {
722 BUG_ON(irq_has_action(dev
->first_msi_irq
));
723 msi_free_irq(dev
, dev
->first_msi_irq
);
725 if (dev
->msix_enabled
) {
726 int irq
, head
, tail
= 0, warning
= 0;
727 void __iomem
*base
= NULL
;
729 irq
= head
= dev
->first_msi_irq
;
730 while (head
!= tail
) {
731 tail
= get_irq_msi(irq
)->link
.tail
;
732 base
= get_irq_msi(irq
)->mask_base
;
733 if (irq_has_action(irq
))
735 else if (irq
!= head
) /* Release MSI-X irq */
736 msi_free_irq(dev
, irq
);
739 msi_free_irq(dev
, irq
);
742 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
743 "called without free_irq() on all MSI-X irqs\n",
750 void pci_no_msi(void)
755 EXPORT_SYMBOL(pci_enable_msi
);
756 EXPORT_SYMBOL(pci_disable_msi
);
757 EXPORT_SYMBOL(pci_enable_msix
);
758 EXPORT_SYMBOL(pci_disable_msix
);